Scid  4.6.5
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 [toplevel .importWin]
11  wm title $w "Scid: Import PGN game"
12  wm minsize $w 50 5
13  frame $w.b
14  pack $w.b -side bottom
15  set pane [::utils::pane::Create $w.pane edit err 650 300 0.8]
16  pack $pane -side top -expand true -fill both
17  set edit $w.pane.edit
18  text $edit.text -height 12 -width 80 -wrap none -background white \
19  -yscroll "$edit.ybar set" -xscroll "$edit.xbar set" -setgrid 1
20  # Override tab-binding for this widget:
21  bind $edit.text <Key-Tab> "[bind all <Key-Tab>]; break"
22  scrollbar $edit.ybar -command "$edit.text yview" -takefocus 0
23  scrollbar $edit.xbar -orient horizontal -command "$edit.text xview" -takefocus 0
24  grid $edit.text -row 0 -column 0 -sticky nesw
25  grid $edit.ybar -row 0 -column 1 -sticky nesw
26  grid $edit.xbar -row 1 -column 0 -sticky nesw
27  grid rowconfig $edit 0 -weight 1 -minsize 0
28  grid columnconfig $edit 0 -weight 1 -minsize 0
29 
30  # Right-mouse button cut/copy/paste menu:
31  menu $edit.text.rmenu -tearoff 0
32  $edit.text.rmenu add command -label "Cut" -command "tk_textCut $edit.text"
33  $edit.text.rmenu add command -label "Copy" -command "tk_textCopy $edit.text"
34  $edit.text.rmenu add command -label "Paste" -command "tk_textPaste $edit.text"
35  $edit.text.rmenu add command -label "Select all" -command "$edit.text tag add sel 1.0 end"
36  bind $edit.text <ButtonPress-$::MB3> "tk_popup $edit.text.rmenu %X %Y"
37 
38  text $pane.err.text -height 4 -width 75 -wrap word -yscroll "$pane.err.scroll set"
39  $pane.err.text insert end $::tr(ImportHelp1)
40  $pane.err.text insert end "\n"
41  $pane.err.text insert end $::tr(ImportHelp2)
42  $pane.err.text configure -state disabled
43  scrollbar $pane.err.scroll -command "$pane.err.text yview" -takefocus 0
44  pack $pane.err.scroll -side right -fill y
45  pack $pane.err.text -side left -expand true -fill both
46 
47  button $w.b.paste -text "$::tr(PasteCurrentGame) (Alt-P)" -command {
48  .importWin.pane.edit.text delete 1.0 end
49  .importWin.pane.edit.text insert end [sc_game pgn -width 70]
50  .importWin.pane.err.text configure -state normal
51  .importWin.pane.err.text delete 1.0 end
52  .importWin.pane.err.text configure -state disabled
53  }
54  button $w.b.clear -text "$::tr(Clear) (Alt-C)" -command {
55  .importWin.pane.edit.text delete 1.0 end
56  .importWin.pane.err.text configure -state normal
57  .importWin.pane.err.text delete 1.0 end
58  .importWin.pane.err.text configure -state disabled
59  }
60  button $w.b.ok -text "$::tr(Import) (Alt-I)" -command {
61  set err [catch {sc_game import \
62  [.importWin.pane.edit.text get 1.0 end]} result]
63  .importWin.pane.err.text configure -state normal
64  .importWin.pane.err.text delete 1.0 end
65  .importWin.pane.err.text insert end $result
66  .importWin.pane.err.text configure -state disabled
67  if {! $err} {
68  ::notify::GameChanged
69  }
70  }
71  button $w.b.cancel -textvar ::tr(Close) -command {
72  destroy .importWin; focus .
73  }
74  frame $w.b.space -width 20
75  pack $w.b.paste $w.b.clear $w.b.space -side left -padx 2 -pady 2
76  pack $w.b.cancel $w.b.ok -side right -padx 10 -pady 5
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  if {[sc_info gzip]} {
150  set ftypes { { "Portable Game Notation files" {".pgn" ".PGN" ".pgn.gz"} } }
151  } else {
152  set ftypes { { "Portable Game Notation files" {".pgn" ".PGN"} } }
153  }
154  lappend ftypes { "Text files" {".txt" ".TXT"} }
155  lappend ftypes { "All files" {"*"} }
156 
157  set fnames [tk_getOpenFile -multiple 1 -initialdir $::initialDir(pgn) -filetypes $ftypes -title "Import games in [file tail [sc_base filename $base]]"]
158  if {$fnames == ""} { return}
159  set ::initialDir(pgn) [file dirname [lindex $fnames 0]]
160  set autoclose 0
161  } else {
162  set autoclose 1
163  }
164 
165  set w .ipgnWin
166  if {[winfo exists $w]} { destroy $w}
167  toplevel $w
168  wm title $w "Scid: Importing PGN files in [file tail [sc_base filename $base]]"
169  canvas $w.progress -width 400 -height 20 -bg white -relief solid -border 1
170  $w.progress create rectangle 0 0 0 0 -fill blue -outline blue -tags bar
171  $w.progress create text 395 10 -anchor e -font font_Regular -tags time \
172  -fill black -text "0:00 / 0:00"
173 
174  frame $w.buttons
175  button $w.buttons.stop -textvar ::tr(Stop) -command { progressBarCancel}
176  button $w.buttons.close -textvar ::tr(Close) -command "focus .; destroy $w"
177  pack $w.buttons.close $w.buttons.stop -side right -ipadx 5 -padx 5 -pady 2
178 
179  pack [frame $w.tf] -side top -expand yes -fill both
180  text $w.text -height 8 -width 60 -background gray90 \
181  -wrap none -setgrid 1 -yscrollcommand "$w.ybar set"
182  scrollbar $w.ybar -command "$w.text yview"
183  pack $w.ybar -in $w.tf -side right -fill y
184  pack $w.text -in $w.tf -side left -fill both -expand yes
185  pack $w.buttons $w.progress -side right
186 
187  catch {grab $w.buttons.stop}
188  bind $w <Escape> "$w.buttons.stop invoke"
189  $w.buttons.close configure -state disabled
190 
191  busyCursor .
192  foreach fname $fnames {
193  $w.text insert end "Importing from [file tail $fname]...\n"
194  $w.text configure -state disabled
195  progressBarSet $w.progress 401 21
196  set err [catch {sc_base import $base $fname} result]
197  $w.text configure -state normal
198  if {$err == 1} {
199  set autoclose 0
200  $w.text insert end "[ERROR::getErrorMsg]\n$result\n\n"
201  } else {
202  set nImported [lindex $result 0]
203  set warnings [lindex $result 1]
204  set str "Imported $nImported "
205  if {$nImported == 1} { append str "game"} else { append str "games"}
206  if {$warnings == ""} {
207  append str " with no PGN errors or warnings."
208  } else {
209  set autoclose 0
210  append str ".\nPGN errors/warnings:\n$warnings"
211  }
212  $w.text insert end "$str\n\n"
213  if {$err == 3} {
214  $w.text insert end ".\nINTERRUPTED\n"
215  set autoclose 0
216  break
217  }
218  }
219  }
220  unbusyCursor .
221 
222  $w.text configure -state disabled
223  $w.buttons.close configure -state normal
224  $w.buttons.stop configure -state disabled
225  catch {grab release $w.buttons.stop}
226  bind $w <Escape> "$w.buttons.close invoke; break"
227 
228  after idle "::notify::DatabaseModified $base"
229  if { $autoclose } { destroy $w}
230 }
231 
232 ###
233 ### End of file: import.tcl
234 ###
235