filebox.tcl 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. # filebox.tcl --
  2. #
  3. # This demonstration script prompts the user to select a file.
  4. if {![info exists widgetDemo]} {
  5. error "This script should be run from the \"widget\" demo."
  6. }
  7. package require Tk
  8. set w .filebox
  9. catch {destroy $w}
  10. toplevel $w
  11. wm title $w "File Selection Dialogs"
  12. wm iconname $w "filebox"
  13. positionWindow $w
  14. label $w.msg -font $font -wraplength 4i -justify left -text "Enter a file name in the entry box or click on the \"Browse\" buttons to select a file name using the file selection dialog."
  15. pack $w.msg -side top
  16. ## See Code / Dismiss buttons
  17. set btns [addSeeDismiss $w.buttons $w]
  18. pack $btns -side bottom -fill x
  19. foreach i {open save} {
  20. set f [frame $w.$i]
  21. label $f.lab -text "Select a file to $i: " -anchor e
  22. entry $f.ent -width 20
  23. button $f.but -text "Browse ..." -command "fileDialog $w $f.ent $i"
  24. pack $f.lab -side left
  25. pack $f.ent -side left -expand yes -fill x
  26. pack $f.but -side left
  27. pack $f -fill x -padx 1c -pady 3
  28. }
  29. if {[tk windowingsystem] eq "x11"} {
  30. checkbutton $w.strict -text "Use Motif Style Dialog" \
  31. -variable tk_strictMotif -onvalue 1 -offvalue 0
  32. pack $w.strict -anchor c
  33. # This binding ensures that we don't run the rest of the demos
  34. # with motif style interactions
  35. bind $w.strict <Destroy> {set tk_strictMotif 0}
  36. }
  37. proc fileDialog {w ent operation} {
  38. # Type names Extension(s) Mac File Type(s)
  39. #
  40. #---------------------------------------------------------
  41. set types {
  42. {"Text files" {.txt .doc} }
  43. {"Text files" {} TEXT}
  44. {"Tcl Scripts" {.tcl} TEXT}
  45. {"C Source Files" {.c .h} }
  46. {"All Source Files" {.tcl .c .h} }
  47. {"Image Files" {.gif} }
  48. {"Image Files" {.jpeg .jpg} }
  49. {"Image Files" "" {GIFF JPEG}}
  50. {"All files" *}
  51. }
  52. if {$operation == "open"} {
  53. global selected_type
  54. if {![info exists selected_type]} {
  55. set selected_type "Tcl Scripts"
  56. }
  57. set file [tk_getOpenFile -filetypes $types -parent $w \
  58. -typevariable selected_type]
  59. puts "You selected filetype \"$selected_type\""
  60. } else {
  61. set file [tk_getSaveFile -filetypes $types -parent $w \
  62. -initialfile Untitled -defaultextension .txt]
  63. }
  64. if {[string compare $file ""]} {
  65. $ent delete 0 end
  66. $ent insert 0 $file
  67. $ent xview end
  68. }
  69. }