~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~ [ freetext search ] ~ [ file search ] ~

Open Mash Cross Reference
mash/tcl/mb/ui-import.tcl

Component: ~ [ mash ] ~ [ apps ] ~ [ gsm ] ~ [ lib ] ~ [ otcl ] ~ [ srm ] ~ [ tcl8.3 ] ~ [ tclcl ] ~ [ tk8.3 ] ~ [ tutorials ] ~

  1 # ui-import.tcl --
  2 #
  3 #       Handles UI for importing external objects like GIFs and PostScript
  4 #       MBImportTool is formerly known as ImportTool (from windows.tcl)
  5 #
  6 # Copyright (c) 1997-2002 The Regents of the University of California.
  7 # All rights reserved.
  8 #
  9 # Redistribution and use in source and binary forms, with or without
 10 # modification, are permitted provided that the following conditions are met:
 11 #
 12 # A. Redistributions of source code must retain the above copyright notice,
 13 #    this list of conditions and the following disclaimer.
 14 # B. Redistributions in binary form must reproduce the above copyright notice,
 15 #    this list of conditions and the following disclaimer in the documentation
 16 #    and/or other materials provided with the distribution.
 17 # C. Neither the names of the copyright holders nor the names of its
 18 #    contributors may be used to endorse or promote products derived from this
 19 #    software without specific prior written permission.
 20 #
 21 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS IS''
 22 # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 23 # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 24 # ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR
 25 # ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 26 # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 27 # SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
 28 # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
 29 # OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 30 # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 31 #
 32 #   @(#) $Header: /usr/mash/src/repository/mash/mash-1/tcl/mb/ui-import.tcl,v 1.24 2002/02/03 04:27:17 lim Exp $
 33 
 34 
 35 import MBTool Dialog ProgressBox Configuration
 36 
 37 Class MBImportTool -superclass MBTool -configuration {
 38         imageOffset {0 0}
 39 }
 40 
 41 MBImportTool instproc init {toolbar mgr sender} {
 42         $self next $toolbar $mgr $sender
 43         $self instvar filetypes_ fileNames_
 44         global tcl_platform
 45         if { $tcl_platform(platform) != "windows" } {
 46                 set filetypes_ {
 47                         { {Graphics Files}
 48                         { .GIF .gif .ps .PS .eps .EPS} }
 49                         { {GIF Files} {.gif .GIF} }
 50                         { {Postscript Files} {.ps .PS .eps .EPS} }
 51                 }
 52         } else {
 53                 set filetypes_ {
 54                         { {GIF Files} {.gif .GIF} }
 55                 }
 56         }
 57         set fileNames_ ""
 58         $self set lastdir_ [pwd]
 59 }
 60 
 61 #
 62 # Prompts to load image
 63 #
 64 MBImportTool instproc PromptForFile {canvas page_id} {
 65         $self instvar fileNames_ filetypes_ lastdir_
 66         set fileNames_ [Dialog transient Dialog/MBImport \
 67                                 -directory "$lastdir_" \
 68                                 -title {Select file to import} \
 69                                 -filetypes $filetypes_]
 70         if {$fileNames_!=""} {
 71                 set lastdir_ [file dirname [lindex $fileNames_ 0]]
 72                 #puts "lastdir_ set to $lastdir_"
 73                 return 0
 74         } else {
 75                 return -1
 76         }
 77 }
 78 
 79 MBImportTool instproc CreateFromFile {filename canvas pt1 pt2} {
 80         global tcl_platform
 81         $self instvar mgr_
 82 
 83         # REVIEW: use a more intelligent way to guess the file type?
 84         # for now, we use the extension to determine the file type
 85         set type [$self getType $filename]
 86         set sender [$mgr_ sender]
 87         if { [string compare $type gif] == 0} {
 88                 set lastImg_ [eval $sender create_item image \
 89                                 $pt1 [list $filename]]
 90         } elseif { ![string compare $type ps] || \
 91                         ![string compare $type eps] } {
 92                 if { $tcl_platform(platform) != "windows" } {
 93                         set lastImg_ [eval $sender create_item \
 94                                         pscript $pt1 $pt2 \
 95                                         -file $filename]
 96                 } else {
 97                         #puts stderr "Postcript files are not supported under windows"
 98                          Dialog transient MessageBox \
 99                                          -image Icons(warning) -type ok \
100                                          -text "Postscript files are not supported under windows"
101                         return -1
102                 }
103         } else {
104                 DbgOut "Unknown format"
105                 return -1
106         }
107         $self instvar mgr_ toolbar_
108         $toolbar_ add_item [[$mgr_ page_manager] current_page] $lastImg_
109 
110         return 1
111 }
112 
113 MBImportTool instproc getType { fileName } {
114     # REVIEW: use a more intelligent way to guess the file type?
115     # for now, we use the extension to determine the file type
116     return [string tolower [lindex [split $fileName "."] end]]
117 }
118 
119 MBImportTool instproc restore {} {
120         set first_ ""
121         set canv_mark_ ""
122         set fileNames_ ""
123 }
124 
125 # define the first corner for image types with fixed size, the
126 # image is placed, otherwise it would be placed the second corner is defined
127 #
128 # this instproc assumes fileNames_ has only one item
129 MBImportTool instproc defineFirst {canvas pt} {
130         $self instvar first_ canv_mark_ fileNames_
131 
132         #puts stderr "defineFirst $fileNames_"
133         if {$fileNames_==""} {
134                 return
135         }
136 
137         set w [$canvas get_win]
138         # since we cannot resize gif (for now) insert the picture immediately
139         if {[string compare [$self getType $fileNames_] "gif"] == 0} {
140  puts "foo"
141                 $self CreateFromFile $fileNames_ $canvas $pt $pt
142  puts "af foo"
143                 $self restore
144                 return
145         }
146         # no postscript for now
147         global tcl_platform
148         if {$tcl_platform(platform)=="windows"} {
149                 Dialog transient MessageBox -type ok \
150                                 -text "postscript not supported for windows yet"
151                 $self restore
152                 return
153         }
154         # otherwise, by default allow the user to sketch the rectangle ($can_mark_)
155         # to specify the bounding box.
156         set first_ $pt
157         set canv_mark_ [eval $canvas create rect $pt $pt]
158         bind $w <B1-Motion> \
159                     "$self stretch $canvas \[$canvas canvasxy %x %y\]"
160         bind $w <ButtonRelease-1> \
161                     "$self end $canvas \[$canvas canvasxy %x %y\]"
162 }
163 
164 MBImportTool instproc stretch {canvas pt} {
165         $self instvar first_ canv_mark_
166         eval $canvas coord $canv_mark_ $first_ $pt
167 }
168 
169 MBImportTool instproc end {canvas pt} {
170         $self instvar first_ canv_mark_ fileNames_
171         $self CreateFromFile [list $fileNames_] $canvas $first_ $pt
172         $canvas delete $canv_mark_
173 
174         set w [$canvas get_win]
175         # don't want further clicking to have any effect
176         # user should click the image button for new images
177         $self restore
178 }
179 
180 MBImportTool instproc activate {page_id args} {
181         puts "in $class activate $page_id <$args>"
182         $self instvar first_ canv_mark_
183         set first_ ""
184         set canv_mark_ ""
185 
186         $self next page_id
187         $self instvar lastImg_ mgr_
188         set canv [[$mgr_ page_manager] page2canv $page_id]
189         set w [$canv get_win]
190 
191         $canv resetBindings
192         $canv config -cursor cross
193 
194         if {$args=={}} {
195                 if {-1 == [$self PromptForFile $canv $page_id]} {
196                         $self restore
197                         return
198                 }
199         }
200 
201         $self instvar fileNames_
202         set theRest [lrange $fileNames_ 1 end]
203         set firstCorner [string trim [$self get_option imageOffset]]
204         if {$firstCorner=="none"} {
205                 set firstCorner [list 0.0 0.0]
206         }
207         set fileNames_ [lindex $fileNames_ 0]
208 
209         # put each of them on a new page with name equals slide name
210         # fill up the full scrollregion
211         if {$theRest != ""} {
212                 set progbox [ProgressBox .mbimportprogress \
213                                 -text "Importing Files ..." \
214                                 -min 0 -max [expr [llength $theRest] + 1] \
215                                 -value 0 ]
216                 $progbox center
217                 update idletasks
218         }
219         set i 0
220         foreach path $theRest {
221                 set pagename [file rootname [file tail $path]]
222                 #FIXME: should be using pageMgr to create page...
223                 $self instvar toolbar_
224                 set menu [[$toolbar_ set mbui_] set menu_]
225                 set pgid [$menu create_new_page $pagename]
226                 eval [$mgr_ sender] -page $pgid \
227                         create_item image $firstCorner [list $path]
228                 incr i
229                 $progbox configure -text "Importing $path"
230                 $progbox configure -value $i
231                 update idletasks
232         }
233         incr i
234         # puts gifs at 0,0 by default
235         if { ([$self getType $fileNames_]=="gif") && \
236                         ("none"!=[string trim \
237                         [$self get_option imageOffset]])} {
238                 $self defineFirst $canv $firstCorner
239                 if {$theRest!={}} {
240                         $progbox configure -text "Importing $path"
241                         $progbox configure -value $i
242                         update idletasks
243                         destroy $progbox
244                 }
245                 $self restore
246                 return
247         }
248 
249         bind $w <Button-1> \
250                         "$self defineFirst $canv \[$canv canvasxy %x %y\]"
251         bind $w <B1-Motion> ""
252         bind $w <ButtonRelease-1> ""
253 }
254 
255 # --
256 #  FileDialog combo for selecting files to import
257 #    result values are:
258 #               1) "" if cancel pressed
259 #               2) a list of files selected if "import all" pressed
260 #               3) a list of one file if "import" pressed or double click
261 # --
262 
263 WidgetClass Dialog/MBImport -superclass Dialog -configspec {
264         {-filetypes fileTypes FileTypes "" config_filetypes cget_filetypes }
265         {-directory directory Directory "" config_directory cget_directory }
266 } -default {
267         { *background WidgetDefault }
268 }
269 
270 Dialog/MBImport instproc config_filetypes { args } {
271         return [eval [$self subwidget filebox] config_filetypes $args]
272 }
273 
274 Dialog/MBImport instproc cget_filetypes { args } {
275         return [eval [$self subwidget filebox] cget_filetypes $args]
276 }
277 
278 Dialog/MBImport instproc config_directory { args } {
279         return [eval [$self subwidget filebox] config_directory $args]
280 }
281 
282 Dialog/MBImport instproc cget_directory { args } {
283         return [eval [$self subwidget filebox] cget_directory $args]
284 }
285 
286 Dialog/MBImport instproc build_widget { path } {
287         $self next $path
288 
289         frame   $path.frame
290         set filebox [FileBox $path.filebox -command "$self import; $self ignore_args" -browsecmd "$self ignore_args"]
291 
292 #       $self subwidget filebox configure \
293 #                       -browsecmd "$self browse; $self ignore_args"
294         set butbox [frame $path.buttonbox]
295         button $butbox.imp -underline 0 -text "Import" \
296                         -command "$self import; $self ignore_args"
297         button $butbox.all -underline 8 -text "Import All" -command \
298                         "$self import_all"
299         button $butbox.cancel -underline 0 -text "Cancel" \
300                         -command "$self cancel"
301 
302         bind $path <KeyPress-Escape> "$self cancel"
303         pack $butbox.imp $butbox.all $butbox.cancel -side left \
304                         -padx 5 -pady 2
305         pack $butbox -side bottom -in $path.frame -anchor e
306         pack $path.filebox -side top -fill both -expand 1 -in $path.frame
307         pack $path.frame -side left -fill both -expand 1
308 }
309 
310 Dialog/MBImport instproc import {} {
311         set filebox [$self subwidget filebox]
312         set file [$filebox cget -filename]
313         if { $file=="" } return
314         set path [file join [$filebox cget -directory] $file]
315         if {![file exists $path]} {
316                 #puts stderr "File \"$path\" does not exist."
317                 Dialog transient MessageBox -image Icons(warning) -type ok \
318                                 -text "File \"$path\" does not exist."
319                 return
320         }
321         $self tkvar result_
322         $self config -result [list $path]
323 }
324 
325 # used to compare strings that have a number at the end
326 # this is dead slow, but presumably we don't do this often
327 proc alphaNumericCmp {str1 str2} {
328         if {$str1==$str2} { return 0 }
329         if {[ regexp {[a-zA-Z]*0*([0-9]*)\.[a-zA-Z0-9]*} $str1 {} n1 ] && \
330                         [ regexp {[a-zA-Z]*0*([0-9]*)\.[a-zA-Z0-9]*} $str2 {} n2]} {
331                 #puts "1: $str1 2: $str2, n1:$n1 n2:$n2 ret [expr $n1 - $n2]"
332                 return [expr {$n1 - $n2}]
333         }
334 }
335 
336 Dialog/MBImport instproc import_all {} {
337         set appPWD [pwd]
338         set filebox [$self subwidget filebox]
339         set dir [$filebox cget -directory]
340         if [catch {
341                 cd $dir
342         }] {
343                 # We cannot change directory to $dir.
344                 # give an error and abort action.
345                 Dialog transient MessageBox -type ok -text \
346                                 "Cannot change to the directory \"$dir\".\
347                                 \nPermission denied." -image Icons(warning)
348                 cd $appPWD
349                 return
350         }
351         set filter [$filebox current_filter]
352         set files [lsort -command alphaNumericCmp [eval glob -nocomplain $filter]]
353         if {0==[llength $files]} { return }
354         # The glob pattern might result in multiple matches, esp. if the
355         #  file system is case insensitive, like win95
356         #  make sure each file is listed only once
357         #puts "before uniquify: $files"
358         set prevFile ""
359         foreach f $files {
360                 if {$f!=$prevFile} {
361                         lappend tmplist $f
362                 }
363                 set prevFile $f
364         }
365         if [info exists tmplist] { puts "after uniquify: $tmplist" }
366         # glob patten have no path names, add them back
367         foreach f $tmplist {
368                 lappend paths [file join $dir $f]
369         }
370         cd $appPWD
371         # get out only when there are 1 or more files
372         if {$paths==""} {
373                 return
374         }
375         #puts "import_all setting result to $paths"
376         $self tkvar result_
377         $self config -result $paths
378 }
379 
380 Dialog/MBImport instproc cancel { } {
381         $self tkvar result_
382         $self config -result ""
383 }
384 

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~ [ freetext search ] ~ [ file search ] ~

This page was automatically generated by the LXR engine.
Visit the LXR main site for more information.