Scid  4.7.0
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
import.tcl
Go to the documentation of this file.
1 ###
2 ### import.tcl: part of Scid.
3 ### Copyright (C) 2000 Shane Hudson.
4 ###
5 
6 ### Import game window
7 
8 proc importPgnGame {} {
9  if {[winfo exists .importWin]} { return}
10  set w .importWin
12  wm title $w "Scid: $::tr(ImportPGN)"
13  wm minsize $w 50 5
14  ttk::frame $w.b
15  pack $w.b -side bottom -fill x
16  set pane [::utils::pane::Create $w.pane edit err 580 300 0.8]
17  pack $pane -side top -expand true -fill both
18  set edit $w.pane.edit
19  text $edit.text -height 12 -width 80 -wrap none -background white \
20  -yscroll "$edit.ybar set" -xscroll "$edit.xbar set" -setgrid 1
21  # Override tab-binding for this widget:
22  bind $edit.text <Key-Tab> "[bind all <Key-Tab>]; break"
23  ttk::scrollbar $edit.ybar -command "$edit.text yview" -takefocus 0
24  ttk::scrollbar $edit.xbar -orient horizontal -command "$edit.text xview" -takefocus 0
25  grid $edit.text -row 0 -column 0 -sticky nesw
26  grid $edit.ybar -row 0 -column 1 -sticky nesw
27  grid $edit.xbar -row 1 -column 0 -sticky nesw
28  grid rowconfig $edit 0 -weight 1 -minsize 0
29  grid columnconfig $edit 0 -weight 1 -minsize 0
30 
31  # Right-mouse button cut/copy/paste menu:
32  menu $edit.text.rmenu -tearoff 0
33  $edit.text.rmenu add command -label "Cut" -command "tk_textCut $edit.text"
34  $edit.text.rmenu add command -label "Copy" -command "tk_textCopy $edit.text"
35  $edit.text.rmenu add command -label "Paste" -command "tk_textPaste $edit.text"
36  $edit.text.rmenu add command -label "Select all" -command "$edit.text tag add sel 1.0 end"
37  bind $edit.text <ButtonPress-$::MB3> "tk_popup $edit.text.rmenu %X %Y"
38 
39  text $pane.err.text -height 4 -width 75 -wrap word -yscroll "$pane.err.scroll set"
40  $pane.err.text insert end $::tr(ImportHelp1)
41  $pane.err.text insert end "\n"
42  $pane.err.text insert end $::tr(ImportHelp2)
43  $pane.err.text configure -state disabled
44  ttk::scrollbar $pane.err.scroll -command "$pane.err.text yview" -takefocus 0
45  pack $pane.err.scroll -side right -fill y
46  pack $pane.err.text -side left -expand true -fill both
47 
48  ttk::button $w.b.paste -text "$::tr(PasteCurrentGame) (Alt-P)" -command {
49  .importWin.pane.edit.text delete 1.0 end
50  .importWin.pane.edit.text insert end [sc_game pgn -width 70]
51  .importWin.pane.err.text configure -state normal
52  .importWin.pane.err.text delete 1.0 end
53  .importWin.pane.err.text configure -state disabled
54  }
55  ttk::button $w.b.clear -text "$::tr(Clear) (Alt-C)" -command {
56  .importWin.pane.edit.text delete 1.0 end
57  .importWin.pane.err.text configure -state normal
58  .importWin.pane.err.text delete 1.0 end
59  .importWin.pane.err.text configure -state disabled
60  }
61  ttk::button $w.b.ok -text "$::tr(Import) (Alt-I)" -command {
62  set err [catch {sc_game import \
63  [.importWin.pane.edit.text get 1.0 end]} result]
64  .importWin.pane.err.text configure -state normal
65  .importWin.pane.err.text delete 1.0 end
66  .importWin.pane.err.text insert end $result
67  .importWin.pane.err.text configure -state disabled
68  if {! $err} {
69  ::notify::GameChanged
70  }
71  }
72  ttk::button $w.b.cancel -textvar ::tr(Close) -command {
73  destroy .importWin; focus .
74  }
75  pack $w.b.paste $w.b.clear -side left -padx 5 -pady "15 5"
76  packdlgbuttons $w.b.cancel $w.b.ok
77  # Paste the current selected text automatically:
78  # if {[catch {$w.pane.edit.text insert end [selection get]}]} {
79  # ?
80  # }
81  # Select all of the pasted text:
82  $w.pane.edit.text tag add sel 1.0 end
83 
84  bind $w <F1> { helpWindow Import }
85  bind $w <Alt-i> { .importWin.b.ok invoke }
86  bind $w <Alt-p> { .importWin.b.paste invoke }
87  bind $w <Alt-c> { .importWin.b.clear invoke }
88  bind $w <Escape> { .importWin.b.cancel invoke }
89  # bind $w.pane.edit.text <Any-KeyRelease> { .importWin.b.ok invoke }
90  focus $w.pane.edit.text
91 }
92 
93 
94 proc importClipboardGame {} {
96  catch {event generate .importWin.pane.edit.text <<Paste>>}
97  # Paste the current selected text automatically if no data was pasted from clipboard:
98  if { [ .importWin.pane.edit.text get 1.0 end] == "\n" } {
99  catch { .importWin.pane.edit.text insert end [selection get]}
100  }
101 }
102 
103 proc importPgnLine {line} {
105  set w .importWin.pane.edit.text
106  $w delete 1.0 end
107  $w insert end $line
108  $w tag add sel 1.0 end
109  focus $w
110 }
111 
112 ################################################################################
113 #
114 ################################################################################
115 proc importMoveList {line} {
116  sc_move start
117  sc_move addSan $line
118  updateBoard -pgn
119 }
120 ################################################################################
121 #
122 ################################################################################
123 proc importMoveListTrans {line} {
124 
125  set doImport 0
126 
127  if { $::askToReplaceMoves } {
128  if {[llength [sc_game firstMoves 1]] == 0} {
129  set doImport 1
130  } elseif {[tk_messageBox -message [::tr "OverwriteExistingMoves"] -type yesno -icon question] == yes} {
131  set doImport 1
132  }
133  } else {
134  set doImport 1
135  }
136  if {$doImport} {
137  set line [untrans $line]
138  sc_move start
139  sc_move addSan $line
140  updateBoard -pgn
141  }
142 
143 }
144 
145 
146 ### Import file of Pgn games:
147 proc importPgnFile {{base} {fnames ""}} {
148  if {$fnames == ""} {
149  set ftypes { { "Portable Game Notation files" {".pgn" ".PGN"} } }
150  lappend ftypes { "Text files" {".txt" ".TXT"} }
151  lappend ftypes { "All files" {"*"} }
152 
153  set fnames [tk_getOpenFile -multiple 1 -initialdir $::initialDir(pgn) -filetypes $ftypes -title "$::tr(ImportingIn) [file tail [sc_base filename $base]]"]
154  if {$fnames == ""} { return}
155  set ::initialDir(pgn) [file dirname [lindex $fnames 0]]
156  set autoclose 0
157  } else {
158  set autoclose 1
159  }
160 
161  set w .ipgnWin
162  if {[winfo exists $w]} { destroy $w}
163  toplevel $w
164  wm title $w "Scid: $::tr(ImportingFiles) [file tail [sc_base filename $base]]"
165  canvas $w.progress -width 400 -height 20 -bg white -relief solid -border 1
166  $w.progress create rectangle 0 0 0 0 -fill blue -outline blue -tags bar
167  $w.progress create text 395 10 -anchor e -font font_Regular -tags time \
168  -fill black -text "0:00 / 0:00"
169 
170  ttk::frame $w.buttons
171  ttk::button $w.buttons.stop -textvar ::tr(Stop) -command { progressBarCancel}
172  ttk::button $w.buttons.close -textvar ::tr(Close) -command "focus .; destroy $w"
173  pack $w.buttons.close $w.buttons.stop -side right -ipadx 5 -padx 5 -pady 2
174 
175  pack [ttk::frame $w.tf] -side top -expand yes -fill both
176  text $w.text -height 8 -width 60 -background gray90 \
177  -wrap none -setgrid 1 -yscrollcommand "$w.ybar set"
178  ttk::scrollbar $w.ybar -command "$w.text yview"
179  pack $w.ybar -in $w.tf -side right -fill y
180  pack $w.text -in $w.tf -side left -fill both -expand yes
181  pack $w.buttons $w.progress -side right
182 
183  catch {grab $w.buttons.stop}
184  bind $w <Escape> "$w.buttons.stop invoke"
185  $w.buttons.close configure -state disabled
186 
187  busyCursor .
188  foreach fname $fnames {
189  $w.text insert end "$::tr(ImportingFrom) [file tail $fname]...\n"
190  $w.text configure -state disabled
191  progressBarSet $w.progress 401 21
192  set err [catch {sc_base import $base $fname} result]
193  $w.text configure -state normal
194  if {$err == 1} {
195  set autoclose 0
196  $w.text insert end "[ERROR::getErrorMsg]\n$result\n\n"
197  } else {
198  set nImported [lindex $result 0]
199  set warnings [lindex $result 1]
200  set str "Imported $nImported "
201  if {$nImported == 1} { append str "game"} else { append str "games"}
202  if {$warnings == ""} {
203  append str " with no PGN errors or warnings."
204  } else {
205  set autoclose 0
206  append str ".\nPGN errors/warnings:\n$warnings"
207  }
208  $w.text insert end "$str\n\n"
209  if {$err == 3} {
210  $w.text insert end ".\nINTERRUPTED\n"
211  set autoclose 0
212  break
213  }
214  }
215  }
216  unbusyCursor .
217 
218  $w.text configure -state disabled
219  $w.buttons.close configure -state normal
220  $w.buttons.stop configure -state disabled
221  catch {grab release $w.buttons.stop}
222  bind $w <Escape> "$w.buttons.close invoke; break"
223 
224  after idle "::notify::DatabaseModified $base"
225  if { $autoclose } { destroy $w}
226 }
227 
228 ###
229 ### End of file: import.tcl
230 ###
231