📄 tkfbox.tcl
字号:
if {[string equal $rTag ""]} { set rTag $oldRTag } } if {[string compare $rTag ""]} { tkIconList_Select $w $rTag tkIconList_See $w $rTag }}# tkIconList_LeftRight --## Moves the active element left or right by one column## Arguments:# w - The IconList widget.# amount - +1 to move right one column, -1 to move left one column.#proc tkIconList_LeftRight {w amount} { upvar #0 $w data if {![info exists data(list)]} { return } if {[string equal $data(curItem) {}]} { set rTag [lindex [lindex $data(list) 0] 2] } else { set oldRTag [lindex [lindex $data(list) $data(curItem)] 2] set newItem [expr {$data(curItem)+($amount*$data(itemsPerColumn))}] set rTag [lindex [lindex $data(list) $newItem] 2] if {[string equal $rTag ""]} { set rTag $oldRTag } } if {[string compare $rTag ""]} { tkIconList_Select $w $rTag tkIconList_See $w $rTag }}#----------------------------------------------------------------------# Accelerator key bindings#----------------------------------------------------------------------# tkIconList_KeyPress --## Gets called when user enters an arbitrary key in the listbox.#proc tkIconList_KeyPress {w key} { global tkPriv append tkPriv(ILAccel,$w) $key tkIconList_Goto $w $tkPriv(ILAccel,$w) catch { after cancel $tkPriv(ILAccel,$w,afterId) } set tkPriv(ILAccel,$w,afterId) [after 500 [list tkIconList_Reset $w]]}proc tkIconList_Goto {w text} { upvar #0 $w data upvar #0 $w:textList textList global tkPriv if {![info exists data(list)]} { return } if {[string equal {} $text]} { return } if {$data(curItem) == "" || $data(curItem) == 0} { set start 0 } else { set start $data(curItem) } set text [string tolower $text] set theIndex -1 set less 0 set len [string length $text] set len0 [expr {$len-1}] set i $start # Search forward until we find a filename whose prefix is an exact match # with $text while {1} { set sub [string range $textList($i) 0 $len0] if {[string equal $text $sub]} { set theIndex $i break } incr i if {$i == $data(numItems)} { set i 0 } if {$i == $start} { break } } if {$theIndex > -1} { set rTag [lindex [lindex $data(list) $theIndex] 2] tkIconList_Select $w $rTag tkIconList_See $w $rTag }}proc tkIconList_Reset {w} { global tkPriv catch {unset tkPriv(ILAccel,$w)}}#----------------------------------------------------------------------## F I L E D I A L O G##----------------------------------------------------------------------namespace eval ::tk::dialog {}namespace eval ::tk::dialog::file {}# ::tk::dialog::file::tkFDialog --## Implements the TK file selection dialog. This dialog is used when# the tk_strictMotif flag is set to false. This procedure shouldn't# be called directly. Call tk_getOpenFile or tk_getSaveFile instead.## Arguments:# type "open" or "save"# args Options parsed by the procedure.#proc ::tk::dialog::file::tkFDialog {type args} { global tkPriv set dataName __tk_filedialog upvar ::tk::dialog::file::$dataName data ::tk::dialog::file::Config $dataName $type $args if {[string equal $data(-parent) .]} { set w .$dataName } else { set w $data(-parent).$dataName } # (re)create the dialog box if necessary # if {![winfo exists $w]} { ::tk::dialog::file::Create $w TkFDialog } elseif {[string compare [winfo class $w] TkFDialog]} { destroy $w ::tk::dialog::file::Create $w TkFDialog } else { set data(dirMenuBtn) $w.f1.menu set data(dirMenu) $w.f1.menu.menu set data(upBtn) $w.f1.up set data(icons) $w.icons set data(ent) $w.f2.ent set data(typeMenuLab) $w.f3.lab set data(typeMenuBtn) $w.f3.menu set data(typeMenu) $data(typeMenuBtn).m set data(okBtn) $w.f2.ok set data(cancelBtn) $w.f3.cancel } wm transient $w $data(-parent) # Add traces on the selectPath variable # trace variable data(selectPath) w "::tk::dialog::file::SetPath $w" $data(dirMenuBtn) configure \ -textvariable ::tk::dialog::file::${dataName}(selectPath) # Initialize the file types menu # if {[llength $data(-filetypes)]} { $data(typeMenu) delete 0 end foreach type $data(-filetypes) { set title [lindex $type 0] set filter [lindex $type 1] $data(typeMenu) add command -label $title \ -command [list ::tk::dialog::file::SetFilter $w $type] } ::tk::dialog::file::SetFilter $w [lindex $data(-filetypes) 0] $data(typeMenuBtn) config -state normal $data(typeMenuLab) config -state normal } else { set data(filter) "*" $data(typeMenuBtn) config -state disabled -takefocus 0 $data(typeMenuLab) config -state disabled } ::tk::dialog::file::UpdateWhenIdle $w # Withdraw the window, then update all the geometry information # so we know how big it wants to be, then center the window in the # display and de-iconify it. ::tk::PlaceWindow $w widget $data(-parent) wm title $w $data(-title) # Set a grab and claim the focus too. ::tk::SetFocusGrab $w $data(ent) $data(ent) delete 0 end $data(ent) insert 0 $data(selectFile) $data(ent) selection range 0 end $data(ent) icursor end # Wait for the user to respond, then restore the focus and # return the index of the selected button. Restore the focus # before deleting the window, since otherwise the window manager # may take the focus away so we can't redirect it. Finally, # restore any grab that was in effect. tkwait variable tkPriv(selectFilePath) ::tk::RestoreFocusGrab $w $data(ent) withdraw # Cleanup traces on selectPath variable # foreach trace [trace vinfo data(selectPath)] { trace vdelete data(selectPath) [lindex $trace 0] [lindex $trace 1] } $data(dirMenuBtn) configure -textvariable {} return $tkPriv(selectFilePath)}# ::tk::dialog::file::Config --## Configures the TK filedialog according to the argument list#proc ::tk::dialog::file::Config {dataName type argList} { upvar ::tk::dialog::file::$dataName data set data(type) $type # 0: Delete all variable that were set on data(selectPath) the # last time the file dialog is used. The traces may cause troubles # if the dialog is now used with a different -parent option. foreach trace [trace vinfo data(selectPath)] { trace vdelete data(selectPath) [lindex $trace 0] [lindex $trace 1] } # 1: the configuration specs # set specs { {-defaultextension "" "" ""} {-filetypes "" "" ""} {-initialdir "" "" ""} {-initialfile "" "" ""} {-parent "" "" "."} {-title "" "" ""} } # 2: default values depending on the type of the dialog # if {![info exists data(selectPath)]} { # first time the dialog has been popped up set data(selectPath) [pwd] set data(selectFile) "" } # 3: parse the arguments # tclParseConfigSpec ::tk::dialog::file::$dataName $specs "" $argList if {$data(-title) == ""} { if {[string equal $type "open"]} { set data(-title) "Open" } else { set data(-title) "Save As" } } # 4: set the default directory and selection according to the -initial # settings # if {$data(-initialdir) != ""} { # Ensure that initialdir is an absolute path name. if {[file isdirectory $data(-initialdir)]} { set old [pwd] cd $data(-initialdir) set data(selectPath) [pwd] cd $old } else { set data(selectPath) [pwd] } } set data(selectFile) $data(-initialfile) # 5. Parse the -filetypes option # set data(-filetypes) [tkFDGetFileTypes $data(-filetypes)] if {![winfo exists $data(-parent)]} { error "bad window path name \"$data(-parent)\"" }}proc ::tk::dialog::file::Create {w class} { set dataName [lindex [split $w .] end] upvar ::tk::dialog::file::$dataName data global tk_library tkPriv toplevel $w -class $class # f1: the frame with the directory option menu # set f1 [frame $w.f1] label $f1.lab -text "Directory:" -under 0 set data(dirMenuBtn) $f1.menu set data(dirMenu) [tk_optionMenu $f1.menu [format %s(selectPath) ::tk::dialog::file::$dataName] ""] set data(upBtn) [button $f1.up] if {![info exists tkPriv(updirImage)]} { set tkPriv(updirImage) [image create bitmap -data {#define updir_width 28#define updir_height 16static char updir_bits[] = { 0x00, 0x00, 0x00, 0x00, 0x80, 0x1f, 0x00, 0x00, 0x40, 0x20, 0x00, 0x00, 0x20, 0x40, 0x00, 0x00, 0xf0, 0xff, 0xff, 0x01, 0x10, 0x00, 0x00, 0x01, 0x10, 0x02, 0x00, 0x01, 0x10, 0x07, 0x00, 0x01, 0x90, 0x0f, 0x00, 0x01, 0x10, 0x02, 0x00, 0x01, 0x10, 0x02, 0x00, 0x01, 0x10, 0x02, 0x00, 0x01, 0x10, 0xfe, 0x07, 0x01, 0x10, 0x00, 0x00, 0x01, 0x10, 0x00, 0x00, 0x01, 0xf0, 0xff, 0xff, 0x01};}] } $data(upBtn) config -image $tkPriv(updirImage) $f1.menu config -takefocus 1 -highlightthickness 2 pack $data(upBtn) -side right -padx 4 -fill both pack $f1.lab -side left -padx 4 -fill both pack $f1.menu -expand yes -fill both -padx 4 # data(icons): the IconList that list the files and directories. # if { [string equal $class TkFDialog] } { set fNameCaption "File name:" set fNameUnder 5 set iconListCommand [list ::tk::dialog::file::OkCmd $w] } else { set fNameCaption "Selection:" set fNameUnder 0 set iconListCommand [list ::tk::dialog::file::chooseDir::DblClick $w] } set data(icons) [tkIconList $w.icons \ -browsecmd [list ::tk::dialog::file::ListBrowse $w] \ -command $iconListCommand] # f2: the frame with the OK button and the "file name" field # set f2 [frame $w.f2 -bd 0] label $f2.lab -text $fNameCaption -anchor e -width 14 \ -under $fNameUnder -pady 0 set data(ent) [entry $f2.ent] # The font to use for the icons. The default Canvas font on Unix # is just deviant. global $w.icons set $w.icons(font) [$data(ent) cget -font] # f3: the frame with the cancel button and the file types field # set f3 [frame $w.f3 -bd 0] # Make the file types bits only if this is a File Dialog if { [string equal $class TkFDialog] } { # The "File of types:" label needs to be grayed-out when # -filetypes are not specified. The label widget does not support # grayed-out text on monochrome displays. Therefore, we have to # use a button widget to emulate a label widget (by setting its # bindtags) set data(typeMenuLab) [button $f3.lab -text "Files of type:" \ -anchor e -width 14 -under 9 \ -bd [$f2.lab cget -bd] \ -highlightthickness [$f2.lab cget -highlightthickness] \ -relief [$f2.lab cget -relief] \ -padx [$f2.lab cget -padx] \ -pady [$f2.lab cget -pady]] bindtags $data(typeMenuLab) [list $data(typeMenuLab) Label \ [winfo toplevel $data(typeMenuLab)] all] set data(typeMenuBtn) [menubutton $f3.menu -indicatoron 1 \ -menu $f3.menu.m] set data(typeMenu) [menu $data(typeMenuBtn).m -tearoff 0] $data(typeMenuBtn) config -takefocus 1 -highlightthickness 2 \ -relief raised -bd 2 -anchor w } # the okBtn is created after the typeMenu so that the keyboard traversal # is in the right order set data(okBtn) [button $f2.ok -text OK -under 0 -width 6 \ -default active -pady 3] set data(cancelBtn) [button $f3.cancel -text Cancel -under 0 -width 6\ -default normal -pady 3] # pack the widgets in f2 and f3 # pack $data(okBtn) -side right -padx 4 -anchor e pack $f2.lab -side left -padx 4 pack $f2.ent -expand yes -fill x -padx 2 -pady 0 pack $data(cancelBtn) -side right -padx 4 -anchor w if { [string equal $class TkFDialog] } { pack $data(typeMenuLab) -side left -padx 4 pack $data(typeMenuBtn) -expand yes -fill x -side right } # Pack all the frames together. We are done with widget construction. # pack $f1 -side top -fill x -pady 4 pack $f3 -side bottom -fill x pack $f2 -side bottom -fill x pack $data(icons) -expand yes -fill both -padx 4 -pady 1 # Set up the event handlers that are common to Directory and File Dialogs # wm protocol $w WM_DELETE_WINDOW [list ::tk::dialog::file::CancelCmd $w] $data(upBtn) config -command [list ::tk::dialog::file::UpDirCmd $w] $data(cancelBtn) config -command [list ::tk::dialog::file::CancelCmd $w] bind $w <KeyPress-Escape> [list tkButtonInvoke $data(cancelBtn)] bind $w <Alt-c> [list tkButtonInvoke $data(cancelBtn)] bind $w <Alt-d> [list focus $data(dirMenuBtn)] # Set up event handlers specific to File or Directory Dialogs # if { [string equal $class TkFDialog] } { bind $data(ent) <Return> [list ::tk::dialog::file::ActivateEnt $w] $data(okBtn) config -command [list ::tk::dialog::file::OkCmd $w] bind $w <Alt-t> [format { if {[string equal [%s cget -state] "normal"]} { focus %s } } $data(typeMenuBtn) $data(typeMenuBtn)] bind $w <Alt-n> [list focus $data(ent)] bind $w <Alt-o> [list ::tk::dialog::file::InvokeBtn $w Open] bind $w <Alt-s> [list ::tk::dialog::file::InvokeBtn $w Save] } else { set okCmd [list ::tk::dialog::file::chooseDir::OkCmd $w] bind $data(ent) <Return> $okCmd $data(okBtn) config -command $okCmd bind $w <Alt-s> [list focus $data(ent)] bind $w <Alt-o> [list tkButtonInvoke $data(okBtn)] } # Build the focus group for all the entries # tkFocusGroup_Create $w tkFocusGroup_BindIn $w $data(ent) [list ::tk::dialog::file::EntFocusIn $w] tkFocusGroup_BindOut $w $data(ent) [list ::tk::dialog::file::EntFocusOut $w]}# ::tk::dialog::file::UpdateWhenIdle --## Creates an idle event handler which updates the dialog in idle# time. This is important because loading the directory may take a long# time and we don't want to load the same directory for multiple times# due to multiple concurrent events.#proc ::tk::dialog::file::UpdateWhenIdle {w} { upvar ::tk::dialog::file::[winfo name $w] data if {[info exists data(updateId)]} { return } else { set data(updateId) [after idle [list ::tk::dialog::file::Update $w]] }}# ::tk::dialog::file::Update --## Loads the files and directories into the IconList widget. Also# sets up the directory option menu for quick access to parent# directories.#proc ::tk::dialog::file::Update {w} { # This proc may be called within an idle handler. Make sure that the # window has not been destroyed before this proc is called if {![winfo exists $w]} { return } set class [winfo class $w] if { [string compare $class TkFDialog] && \ [string compare $class TkChooseDir] } { return } set dataName [winfo name $w]
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -