Update tk to version 8.5.8
[msysgit/mtrensch.git] / mingw / lib / tk8.5 / xmfbox.tcl
blob048713e3c8c334eab290de84dae634bd919288b6
1 # xmfbox.tcl --
3 # Implements the "Motif" style file selection dialog for the
4 # Unix platform. This implementation is used only if the
5 # "::tk_strictMotif" flag is set.
7 # RCS: @(#) $Id: xmfbox.tcl,v 1.31.2.1 2009/10/22 10:27:58 dkf Exp $
9 # Copyright (c) 1996 Sun Microsystems, Inc.
10 # Copyright (c) 1998-2000 Scriptics Corporation
12 # See the file "license.terms" for information on usage and redistribution
13 # of this file, and for a DISCLAIMER OF ALL WARRANTIES.
15 namespace eval ::tk::dialog {}
16 namespace eval ::tk::dialog::file {}
19 # ::tk::MotifFDialog --
21 # Implements a file dialog similar to the standard Motif file
22 # selection box.
24 # Arguments:
25 # type "open" or "save"
26 # args Options parsed by the procedure.
28 # Results:
29 # When -multiple is set to 0, this returns the absolute pathname
30 # of the selected file. (NOTE: This is not the same as a single
31 # element list.)
33 # When -multiple is set to > 0, this returns a Tcl list of absolute
34 # pathnames. The argument for -multiple is ignored, but for consistency
35 # with Windows it defines the maximum amount of memory to allocate for
36 # the returned filenames.
38 proc ::tk::MotifFDialog {type args} {
39 variable ::tk::Priv
40 set dataName __tk_filedialog
41 upvar ::tk::dialog::file::$dataName data
43 set w [MotifFDialog_Create $dataName $type $args]
45 # Set a grab and claim the focus too.
47 ::tk::SetFocusGrab $w $data(sEnt)
48 $data(sEnt) selection range 0 end
50 # Wait for the user to respond, then restore the focus and
51 # return the index of the selected button. Restore the focus
52 # before deleting the window, since otherwise the window manager
53 # may take the focus away so we can't redirect it. Finally,
54 # restore any grab that was in effect.
56 vwait ::tk::Priv(selectFilePath)
57 set result $Priv(selectFilePath)
58 ::tk::RestoreFocusGrab $w $data(sEnt) withdraw
60 return $result
63 # ::tk::MotifFDialog_Create --
65 # Creates the Motif file dialog (if it doesn't exist yet) and
66 # initialize the internal data structure associated with the
67 # dialog.
69 # This procedure is used by ::tk::MotifFDialog to create the
70 # dialog. It's also used by the test suite to test the Motif
71 # file dialog implementation. User code shouldn't call this
72 # procedure directly.
74 # Arguments:
75 # dataName Name of the global "data" array for the file dialog.
76 # type "Save" or "Open"
77 # argList Options parsed by the procedure.
79 # Results:
80 # Pathname of the file dialog.
82 proc ::tk::MotifFDialog_Create {dataName type argList} {
83 upvar ::tk::dialog::file::$dataName data
85 MotifFDialog_Config $dataName $type $argList
87 if {$data(-parent) eq "."} {
88 set w .$dataName
89 } else {
90 set w $data(-parent).$dataName
93 # (re)create the dialog box if necessary
95 if {![winfo exists $w]} {
96 MotifFDialog_BuildUI $w
97 } elseif {[winfo class $w] ne "TkMotifFDialog"} {
98 destroy $w
99 MotifFDialog_BuildUI $w
100 } else {
101 set data(fEnt) $w.top.f1.ent
102 set data(dList) $w.top.f2.a.l
103 set data(fList) $w.top.f2.b.l
104 set data(sEnt) $w.top.f3.ent
105 set data(okBtn) $w.bot.ok
106 set data(filterBtn) $w.bot.filter
107 set data(cancelBtn) $w.bot.cancel
109 MotifFDialog_SetListMode $w
111 # Dialog boxes should be transient with respect to their parent,
112 # so that they will always stay on top of their parent window. However,
113 # some window managers will create the window as withdrawn if the parent
114 # window is withdrawn or iconified. Combined with the grab we put on the
115 # window, this can hang the entire application. Therefore we only make
116 # the dialog transient if the parent is viewable.
118 if {[winfo viewable [winfo toplevel $data(-parent)]] } {
119 wm transient $w $data(-parent)
122 MotifFDialog_FileTypes $w
123 MotifFDialog_Update $w
125 # Withdraw the window, then update all the geometry information
126 # so we know how big it wants to be, then center the window in the
127 # display (Motif style) and de-iconify it.
129 ::tk::PlaceWindow $w
130 wm title $w $data(-title)
132 return $w
135 # ::tk::MotifFDialog_FileTypes --
137 # Checks the -filetypes option. If present this adds a list of radio-
138 # buttons to pick the file types from.
140 # Arguments:
141 # w Pathname of the tk_get*File dialogue.
143 # Results:
144 # none
146 proc ::tk::MotifFDialog_FileTypes {w} {
147 upvar ::tk::dialog::file::[winfo name $w] data
149 set f $w.top.f3.types
150 destroy $f
152 # No file types: use "*" as the filter and display no radio-buttons
153 if {$data(-filetypes) eq ""} {
154 set data(filter) *
155 return
158 # The filetypes radiobuttons
159 # set data(fileType) $data(-defaulttype)
160 # Default type to first entry
161 set initialTypeName [lindex $data(-filetypes) 0 0]
162 if {$data(-typevariable) ne ""} {
163 upvar #0 $data(-typevariable) typeVariable
164 if {[info exist typeVariable]} {
165 set initialTypeName $typeVariable
168 set ix 0
169 set data(fileType) 0
170 foreach fltr $data(-filetypes) {
171 set fname [lindex $fltr 0]
172 if {[string first $initialTypeName $fname] == 0} {
173 set data(fileType) $ix
174 break
176 incr ix
179 MotifFDialog_SetFilter $w [lindex $data(-filetypes) $data(fileType)]
181 #don't produce radiobuttons for only one filetype
182 if {[llength $data(-filetypes)] == 1} {
183 return
186 frame $f
187 set cnt 0
188 if {$data(-filetypes) ne {}} {
189 foreach type $data(-filetypes) {
190 set title [lindex [lindex $type 0] 0]
191 set filter [lindex $type 1]
192 radiobutton $f.b$cnt \
193 -text $title \
194 -variable ::tk::dialog::file::[winfo name $w](fileType) \
195 -value $cnt \
196 -command [list tk::MotifFDialog_SetFilter $w $type]
197 pack $f.b$cnt -side left
198 incr cnt
201 $f.b$data(fileType) invoke
203 pack $f -side bottom -fill both
205 return
208 # This proc gets called whenever data(filter) is set
210 proc ::tk::MotifFDialog_SetFilter {w type} {
211 upvar ::tk::dialog::file::[winfo name $w] data
212 variable ::tk::Priv
214 set data(filter) [lindex $type 1]
215 set Priv(selectFileType) [lindex [lindex $type 0] 0]
217 MotifFDialog_Update $w
220 # ::tk::MotifFDialog_Config --
222 # Iterates over the optional arguments to determine the option
223 # values for the Motif file dialog; gives default values to
224 # unspecified options.
226 # Arguments:
227 # dataName The name of the global variable in which
228 # data for the file dialog is stored.
229 # type "Save" or "Open"
230 # argList Options parsed by the procedure.
232 proc ::tk::MotifFDialog_Config {dataName type argList} {
233 upvar ::tk::dialog::file::$dataName data
235 set data(type) $type
237 # 1: the configuration specs
239 set specs {
240 {-defaultextension "" "" ""}
241 {-filetypes "" "" ""}
242 {-initialdir "" "" ""}
243 {-initialfile "" "" ""}
244 {-parent "" "" "."}
245 {-title "" "" ""}
246 {-typevariable "" "" ""}
248 if {$type eq "open"} {
249 lappend specs {-multiple "" "" "0"}
252 set data(-multiple) 0
253 # 2: default values depending on the type of the dialog
255 if {![info exists data(selectPath)]} {
256 # first time the dialog has been popped up
257 set data(selectPath) [pwd]
258 set data(selectFile) ""
261 # 3: parse the arguments
263 tclParseConfigSpec ::tk::dialog::file::$dataName $specs "" $argList
265 if {$data(-title) eq ""} {
266 if {$type eq "open"} {
267 if {$data(-multiple) != 0} {
268 set data(-title) "[mc {Open Multiple Files}]"
269 } else {
270 set data(-title) [mc "Open"]
272 } else {
273 set data(-title) [mc "Save As"]
277 # 4: set the default directory and selection according to the -initial
278 # settings
280 if {$data(-initialdir) ne ""} {
281 if {[file isdirectory $data(-initialdir)]} {
282 set data(selectPath) [lindex [glob $data(-initialdir)] 0]
283 } else {
284 set data(selectPath) [pwd]
287 # Convert the initialdir to an absolute path name.
289 set old [pwd]
290 cd $data(selectPath)
291 set data(selectPath) [pwd]
292 cd $old
294 set data(selectFile) $data(-initialfile)
296 # 5. Parse the -filetypes option. It is not used by the motif
297 # file dialog, but we check for validity of the value to make sure
298 # the application code also runs fine with the TK file dialog.
300 set data(-filetypes) [::tk::FDGetFileTypes $data(-filetypes)]
302 if {![info exists data(filter)]} {
303 set data(filter) *
305 if {![winfo exists $data(-parent)]} {
306 error "bad window path name \"$data(-parent)\""
310 # ::tk::MotifFDialog_BuildUI --
312 # Builds the UI components of the Motif file dialog.
314 # Arguments:
315 # w Pathname of the dialog to build.
317 # Results:
318 # None.
320 proc ::tk::MotifFDialog_BuildUI {w} {
321 set dataName [lindex [split $w .] end]
322 upvar ::tk::dialog::file::$dataName data
324 # Create the dialog toplevel and internal frames.
326 toplevel $w -class TkMotifFDialog
327 set top [frame $w.top -relief raised -bd 1]
328 set bot [frame $w.bot -relief raised -bd 1]
330 pack $w.bot -side bottom -fill x
331 pack $w.top -side top -expand yes -fill both
333 set f1 [frame $top.f1]
334 set f2 [frame $top.f2]
335 set f3 [frame $top.f3]
337 pack $f1 -side top -fill x
338 pack $f3 -side bottom -fill x
339 pack $f2 -expand yes -fill both
341 set f2a [frame $f2.a]
342 set f2b [frame $f2.b]
344 grid $f2a -row 0 -column 0 -rowspan 1 -columnspan 1 -padx 4 -pady 4 \
345 -sticky news
346 grid $f2b -row 0 -column 1 -rowspan 1 -columnspan 1 -padx 4 -pady 4 \
347 -sticky news
348 grid rowconfigure $f2 0 -minsize 0 -weight 1
349 grid columnconfigure $f2 0 -minsize 0 -weight 1
350 grid columnconfigure $f2 1 -minsize 150 -weight 2
352 # The Filter box
354 bind [::tk::AmpWidget label $f1.lab -text [mc "Fil&ter:"] -anchor w] \
355 <<AltUnderlined>> [list focus $f1.ent]
356 entry $f1.ent
357 pack $f1.lab -side top -fill x -padx 6 -pady 4
358 pack $f1.ent -side top -fill x -padx 4 -pady 0
359 set data(fEnt) $f1.ent
361 # The file and directory lists
363 set data(dList) [MotifFDialog_MakeSList $w $f2a \
364 [mc "&Directory:"] DList]
365 set data(fList) [MotifFDialog_MakeSList $w $f2b \
366 [mc "Fi&les:"] FList]
368 # The Selection box
370 bind [::tk::AmpWidget label $f3.lab -text [mc "&Selection:"] -anchor w] \
371 <<AltUnderlined>> [list focus $f3.ent]
372 entry $f3.ent
373 pack $f3.lab -side top -fill x -padx 6 -pady 0
374 pack $f3.ent -side top -fill x -padx 4 -pady 4
375 set data(sEnt) $f3.ent
377 # The buttons
379 set maxWidth [::tk::mcmaxamp &OK &Filter &Cancel]
380 set maxWidth [expr {$maxWidth<6?6:$maxWidth}]
381 set data(okBtn) [::tk::AmpWidget button $bot.ok -text [mc "&OK"] \
382 -width $maxWidth \
383 -command [list tk::MotifFDialog_OkCmd $w]]
384 set data(filterBtn) [::tk::AmpWidget button $bot.filter -text [mc "&Filter"] \
385 -width $maxWidth \
386 -command [list tk::MotifFDialog_FilterCmd $w]]
387 set data(cancelBtn) [::tk::AmpWidget button $bot.cancel -text [mc "&Cancel"] \
388 -width $maxWidth \
389 -command [list tk::MotifFDialog_CancelCmd $w]]
391 pack $bot.ok $bot.filter $bot.cancel -padx 10 -pady 10 -expand yes \
392 -side left
394 # Create the bindings:
396 bind $w <Alt-Key> [list ::tk::AltKeyInDialog $w %A]
398 bind $data(fEnt) <Return> [list tk::MotifFDialog_ActivateFEnt $w]
399 bind $data(sEnt) <Return> [list tk::MotifFDialog_ActivateSEnt $w]
400 bind $w <Escape> [list tk::MotifFDialog_CancelCmd $w]
401 bind $w.bot <Destroy> {set ::tk::Priv(selectFilePath) {}}
403 wm protocol $w WM_DELETE_WINDOW [list tk::MotifFDialog_CancelCmd $w]
406 proc ::tk::MotifFDialog_SetListMode {w} {
407 upvar ::tk::dialog::file::[winfo name $w] data
409 if {$data(-multiple) != 0} {
410 set selectmode extended
411 } else {
412 set selectmode browse
414 set f $w.top.f2.b
415 $f.l configure -selectmode $selectmode
418 # ::tk::MotifFDialog_MakeSList --
420 # Create a scrolled-listbox and set the keyboard accelerator
421 # bindings so that the list selection follows what the user
422 # types.
424 # Arguments:
425 # w Pathname of the dialog box.
426 # f Frame widget inside which to create the scrolled
427 # listbox. This frame widget already exists.
428 # label The string to display on top of the listbox.
429 # under Sets the -under option of the label.
430 # cmdPrefix Specifies procedures to call when the listbox is
431 # browsed or activated.
433 proc ::tk::MotifFDialog_MakeSList {w f label cmdPrefix} {
434 bind [::tk::AmpWidget label $f.lab -text $label -anchor w] \
435 <<AltUnderlined>> [list focus $f.l]
436 listbox $f.l -width 12 -height 5 -exportselection 0\
437 -xscrollcommand [list $f.h set] -yscrollcommand [list $f.v set]
438 scrollbar $f.v -orient vertical -takefocus 0 -command [list $f.l yview]
439 scrollbar $f.h -orient horizontal -takefocus 0 -command [list $f.l xview]
440 grid $f.lab -row 0 -column 0 -sticky news -rowspan 1 -columnspan 2 \
441 -padx 2 -pady 2
442 grid $f.l -row 1 -column 0 -rowspan 1 -columnspan 1 -sticky news
443 grid $f.v -row 1 -column 1 -rowspan 1 -columnspan 1 -sticky news
444 grid $f.h -row 2 -column 0 -rowspan 1 -columnspan 1 -sticky news
446 grid rowconfigure $f 0 -weight 0 -minsize 0
447 grid rowconfigure $f 1 -weight 1 -minsize 0
448 grid columnconfigure $f 0 -weight 1 -minsize 0
450 # bindings for the listboxes
452 set list $f.l
453 bind $list <<ListboxSelect>> [list tk::MotifFDialog_Browse$cmdPrefix $w]
454 bind $list <Double-ButtonRelease-1> \
455 [list tk::MotifFDialog_Activate$cmdPrefix $w]
456 bind $list <Return> "tk::MotifFDialog_Browse$cmdPrefix [list $w]; \
457 tk::MotifFDialog_Activate$cmdPrefix [list $w]"
459 bindtags $list [list Listbox $list [winfo toplevel $list] all]
460 ListBoxKeyAccel_Set $list
462 return $f.l
465 # ::tk::MotifFDialog_InterpFilter --
467 # Interpret the string in the filter entry into two components:
468 # the directory and the pattern. If the string is a relative
469 # pathname, give a warning to the user and restore the pattern
470 # to original.
472 # Arguments:
473 # w pathname of the dialog box.
475 # Results:
476 # A list of two elements. The first element is the directory
477 # specified # by the filter. The second element is the filter
478 # pattern itself.
480 proc ::tk::MotifFDialog_InterpFilter {w} {
481 upvar ::tk::dialog::file::[winfo name $w] data
483 set text [string trim [$data(fEnt) get]]
485 # Perform tilde substitution
487 set badTilde 0
488 if {[string index $text 0] eq "~"} {
489 set list [file split $text]
490 set tilde [lindex $list 0]
491 if {[catch {set tilde [glob $tilde]}]} {
492 set badTilde 1
493 } else {
494 set text [eval file join [concat $tilde [lrange $list 1 end]]]
498 # If the string is a relative pathname, combine it
499 # with the current selectPath.
501 set relative 0
502 if {[file pathtype $text] eq "relative"} {
503 set relative 1
504 } elseif {$badTilde} {
505 set relative 1
508 if {$relative} {
509 tk_messageBox -icon warning -type ok \
510 -message "\"$text\" must be an absolute pathname"
512 $data(fEnt) delete 0 end
513 $data(fEnt) insert 0 [::tk::dialog::file::JoinFile $data(selectPath) \
514 $data(filter)]
516 return [list $data(selectPath) $data(filter)]
519 set resolved [::tk::dialog::file::JoinFile [file dirname $text] [file tail $text]]
521 if {[file isdirectory $resolved]} {
522 set dir $resolved
523 set fil $data(filter)
524 } else {
525 set dir [file dirname $resolved]
526 set fil [file tail $resolved]
529 return [list $dir $fil]
532 # ::tk::MotifFDialog_Update
534 # Load the files and synchronize the "filter" and "selection" fields
535 # boxes.
537 # Arguments:
538 # w pathname of the dialog box.
540 # Results:
541 # None.
543 proc ::tk::MotifFDialog_Update {w} {
544 upvar ::tk::dialog::file::[winfo name $w] data
546 $data(fEnt) delete 0 end
547 $data(fEnt) insert 0 \
548 [::tk::dialog::file::JoinFile $data(selectPath) $data(filter)]
549 $data(sEnt) delete 0 end
550 $data(sEnt) insert 0 [::tk::dialog::file::JoinFile $data(selectPath) \
551 $data(selectFile)]
553 MotifFDialog_LoadFiles $w
556 # ::tk::MotifFDialog_LoadFiles --
558 # Loads the files and directories into the two listboxes according
559 # to the filter setting.
561 # Arguments:
562 # w pathname of the dialog box.
564 # Results:
565 # None.
567 proc ::tk::MotifFDialog_LoadFiles {w} {
568 upvar ::tk::dialog::file::[winfo name $w] data
570 $data(dList) delete 0 end
571 $data(fList) delete 0 end
573 set appPWD [pwd]
574 if {[catch {cd $data(selectPath)}]} {
575 cd $appPWD
577 $data(dList) insert end ".."
578 return
581 # Make the dir and file lists
583 # For speed we only have one glob, which reduces the file system
584 # calls (good for slow NFS networks).
586 # We also do two smaller sorts (files + dirs) instead of one large sort,
587 # which gives a small speed increase.
589 set top 0
590 set dlist ""
591 set flist ""
592 foreach f [glob -nocomplain .* *] {
593 if {[file isdir ./$f]} {
594 lappend dlist $f
595 } else {
596 foreach pat $data(filter) {
597 if {[string match $pat $f]} {
598 if {[string match .* $f]} {
599 incr top
601 lappend flist $f
602 break
607 eval [list $data(dList) insert end] [lsort -dictionary $dlist]
608 eval [list $data(fList) insert end] [lsort -dictionary $flist]
610 # The user probably doesn't want to see the . files. We adjust the view
611 # so that the listbox displays all the non-dot files
612 $data(fList) yview $top
614 cd $appPWD
617 # ::tk::MotifFDialog_BrowseDList --
619 # This procedure is called when the directory list is browsed
620 # (clicked-over) by the user.
622 # Arguments:
623 # w The pathname of the dialog box.
625 # Results:
626 # None.
628 proc ::tk::MotifFDialog_BrowseDList {w} {
629 upvar ::tk::dialog::file::[winfo name $w] data
631 focus $data(dList)
632 if {[$data(dList) curselection] eq ""} {
633 return
635 set subdir [$data(dList) get [$data(dList) curselection]]
636 if {$subdir eq ""} {
637 return
640 $data(fList) selection clear 0 end
642 set list [MotifFDialog_InterpFilter $w]
643 set data(filter) [lindex $list 1]
645 switch -- $subdir {
647 set newSpec [::tk::dialog::file::JoinFile $data(selectPath) $data(filter)]
649 .. {
650 set newSpec [::tk::dialog::file::JoinFile [file dirname $data(selectPath)] \
651 $data(filter)]
653 default {
654 set newSpec [::tk::dialog::file::JoinFile [::tk::dialog::file::JoinFile \
655 $data(selectPath) $subdir] $data(filter)]
659 $data(fEnt) delete 0 end
660 $data(fEnt) insert 0 $newSpec
663 # ::tk::MotifFDialog_ActivateDList --
665 # This procedure is called when the directory list is activated
666 # (double-clicked) by the user.
668 # Arguments:
669 # w The pathname of the dialog box.
671 # Results:
672 # None.
674 proc ::tk::MotifFDialog_ActivateDList {w} {
675 upvar ::tk::dialog::file::[winfo name $w] data
677 if {[$data(dList) curselection] eq ""} {
678 return
680 set subdir [$data(dList) get [$data(dList) curselection]]
681 if {$subdir eq ""} {
682 return
685 $data(fList) selection clear 0 end
687 switch -- $subdir {
689 set newDir $data(selectPath)
691 .. {
692 set newDir [file dirname $data(selectPath)]
694 default {
695 set newDir [::tk::dialog::file::JoinFile $data(selectPath) $subdir]
699 set data(selectPath) $newDir
700 MotifFDialog_Update $w
702 if {$subdir ne ".."} {
703 $data(dList) selection set 0
704 $data(dList) activate 0
705 } else {
706 $data(dList) selection set 1
707 $data(dList) activate 1
711 # ::tk::MotifFDialog_BrowseFList --
713 # This procedure is called when the file list is browsed
714 # (clicked-over) by the user.
716 # Arguments:
717 # w The pathname of the dialog box.
719 # Results:
720 # None.
722 proc ::tk::MotifFDialog_BrowseFList {w} {
723 upvar ::tk::dialog::file::[winfo name $w] data
725 focus $data(fList)
726 set data(selectFile) ""
727 foreach item [$data(fList) curselection] {
728 lappend data(selectFile) [$data(fList) get $item]
730 if {[llength $data(selectFile)] == 0} {
731 return
734 $data(dList) selection clear 0 end
736 $data(fEnt) delete 0 end
737 $data(fEnt) insert 0 [::tk::dialog::file::JoinFile $data(selectPath) \
738 $data(filter)]
739 $data(fEnt) xview end
741 # if it's a multiple selection box, just put in the filenames
742 # otherwise put in the full path as usual
743 $data(sEnt) delete 0 end
744 if {$data(-multiple) != 0} {
745 $data(sEnt) insert 0 $data(selectFile)
746 } else {
747 $data(sEnt) insert 0 [::tk::dialog::file::JoinFile $data(selectPath) \
748 [lindex $data(selectFile) 0]]
750 $data(sEnt) xview end
753 # ::tk::MotifFDialog_ActivateFList --
755 # This procedure is called when the file list is activated
756 # (double-clicked) by the user.
758 # Arguments:
759 # w The pathname of the dialog box.
761 # Results:
762 # None.
764 proc ::tk::MotifFDialog_ActivateFList {w} {
765 upvar ::tk::dialog::file::[winfo name $w] data
767 if {[$data(fList) curselection] eq ""} {
768 return
770 set data(selectFile) [$data(fList) get [$data(fList) curselection]]
771 if {$data(selectFile) eq ""} {
772 return
773 } else {
774 MotifFDialog_ActivateSEnt $w
778 # ::tk::MotifFDialog_ActivateFEnt --
780 # This procedure is called when the user presses Return inside
781 # the "filter" entry. It updates the dialog according to the
782 # text inside the filter entry.
784 # Arguments:
785 # w The pathname of the dialog box.
787 # Results:
788 # None.
790 proc ::tk::MotifFDialog_ActivateFEnt {w} {
791 upvar ::tk::dialog::file::[winfo name $w] data
793 set list [MotifFDialog_InterpFilter $w]
794 set data(selectPath) [lindex $list 0]
795 set data(filter) [lindex $list 1]
797 MotifFDialog_Update $w
800 # ::tk::MotifFDialog_ActivateSEnt --
802 # This procedure is called when the user presses Return inside
803 # the "selection" entry. It sets the ::tk::Priv(selectFilePath)
804 # variable so that the vwait loop in tk::MotifFDialog will be
805 # terminated.
807 # Arguments:
808 # w The pathname of the dialog box.
810 # Results:
811 # None.
813 proc ::tk::MotifFDialog_ActivateSEnt {w} {
814 variable ::tk::Priv
815 upvar ::tk::dialog::file::[winfo name $w] data
817 set selectFilePath [string trim [$data(sEnt) get]]
819 if {$selectFilePath eq ""} {
820 MotifFDialog_FilterCmd $w
821 return
824 if {$data(-multiple) == 0} {
825 set selectFilePath [list $selectFilePath]
828 if {[file isdirectory [lindex $selectFilePath 0]]} {
829 set data(selectPath) [lindex [glob $selectFilePath] 0]
830 set data(selectFile) ""
831 MotifFDialog_Update $w
832 return
835 set newFileList ""
836 foreach item $selectFilePath {
837 if {[file pathtype $item] ne "absolute"} {
838 set item [file join $data(selectPath) $item]
839 } elseif {![file exists [file dirname $item]]} {
840 tk_messageBox -icon warning -type ok \
841 -message [mc {Directory "%1$s" does not exist.} \
842 [file dirname $item]]
843 return
846 if {![file exists $item]} {
847 if {$data(type) eq "open"} {
848 tk_messageBox -icon warning -type ok \
849 -message [mc {File "%1$s" does not exist.} $item]
850 return
852 } elseif {$data(type) eq "save"} {
853 set message [format %s%s \
854 [mc "File \"%1\$s\" already exists.\n\n" $selectFilePath] \
855 [mc {Replace existing file?}]]
856 set answer [tk_messageBox -icon warning -type yesno \
857 -message $message]
858 if {$answer eq "no"} {
859 return
863 lappend newFileList $item
866 # Return selected filter
867 if {[info exists data(-typevariable)] && $data(-typevariable) ne ""
868 && [info exists data(-filetypes)] && $data(-filetypes) ne ""} {
869 upvar #0 $data(-typevariable) typeVariable
870 set typeVariable [lindex $data(-filetypes) $data(fileType) 0]
873 if {$data(-multiple) != 0} {
874 set Priv(selectFilePath) $newFileList
875 } else {
876 set Priv(selectFilePath) [lindex $newFileList 0]
879 # Set selectFile and selectPath to first item in list
880 set Priv(selectFile) [file tail [lindex $newFileList 0]]
881 set Priv(selectPath) [file dirname [lindex $newFileList 0]]
885 proc ::tk::MotifFDialog_OkCmd {w} {
886 upvar ::tk::dialog::file::[winfo name $w] data
888 MotifFDialog_ActivateSEnt $w
891 proc ::tk::MotifFDialog_FilterCmd {w} {
892 upvar ::tk::dialog::file::[winfo name $w] data
894 MotifFDialog_ActivateFEnt $w
897 proc ::tk::MotifFDialog_CancelCmd {w} {
898 variable ::tk::Priv
900 set Priv(selectFilePath) ""
901 set Priv(selectFile) ""
902 set Priv(selectPath) ""
905 proc ::tk::ListBoxKeyAccel_Set {w} {
906 bind Listbox <Any-KeyPress> ""
907 bind $w <Destroy> [list tk::ListBoxKeyAccel_Unset $w]
908 bind $w <Any-KeyPress> [list tk::ListBoxKeyAccel_Key $w %A]
911 proc ::tk::ListBoxKeyAccel_Unset {w} {
912 variable ::tk::Priv
914 catch {after cancel $Priv(lbAccel,$w,afterId)}
915 unset -nocomplain Priv(lbAccel,$w) Priv(lbAccel,$w,afterId)
918 # ::tk::ListBoxKeyAccel_Key--
920 # This procedure maintains a list of recently entered keystrokes
921 # over a listbox widget. It arranges an idle event to move the
922 # selection of the listbox to the entry that begins with the
923 # keystrokes.
925 # Arguments:
926 # w The pathname of the listbox.
927 # key The key which the user just pressed.
929 # Results:
930 # None.
932 proc ::tk::ListBoxKeyAccel_Key {w key} {
933 variable ::tk::Priv
935 if { $key eq "" } {
936 return
938 append Priv(lbAccel,$w) $key
939 ListBoxKeyAccel_Goto $w $Priv(lbAccel,$w)
940 catch {
941 after cancel $Priv(lbAccel,$w,afterId)
943 set Priv(lbAccel,$w,afterId) [after 500 \
944 [list tk::ListBoxKeyAccel_Reset $w]]
947 proc ::tk::ListBoxKeyAccel_Goto {w string} {
948 variable ::tk::Priv
950 set string [string tolower $string]
951 set end [$w index end]
952 set theIndex -1
954 for {set i 0} {$i < $end} {incr i} {
955 set item [string tolower [$w get $i]]
956 if {[string compare $string $item] >= 0} {
957 set theIndex $i
959 if {[string compare $string $item] <= 0} {
960 set theIndex $i
961 break
965 if {$theIndex >= 0} {
966 $w selection clear 0 end
967 $w selection set $theIndex $theIndex
968 $w activate $theIndex
969 $w see $theIndex
970 event generate $w <<ListboxSelect>>
974 proc ::tk::ListBoxKeyAccel_Reset {w} {
975 variable ::tk::Priv
977 unset -nocomplain Priv(lbAccel,$w)
980 proc ::tk_getFileType {} {
981 variable ::tk::Priv
983 return $Priv(selectFileType)