menu.tcl 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. # menu.tcl --
  2. #
  3. # This demonstration script creates a window with a bunch of menus
  4. # and cascaded menus using menubars.
  5. if {![info exists widgetDemo]} {
  6. error "This script should be run from the \"widget\" demo."
  7. }
  8. package require Tk
  9. set w .menu
  10. catch {destroy $w}
  11. toplevel $w
  12. wm title $w "Menu Demonstration"
  13. wm iconname $w "menu"
  14. positionWindow $w
  15. label $w.msg -font $font -wraplength 4i -justify left
  16. if {[tk windowingsystem] eq "aqua"} {
  17. catch {set origUseCustomMDEF $::tk::mac::useCustomMDEF; set ::tk::mac::useCustomMDEF 1}
  18. $w.msg configure -text "This window has a menubar with cascaded menus. You can invoke entries with an accelerator by typing Command+x, where \"x\" is the character next to the command key symbol. The rightmost menu can be torn off into a palette by selecting the first item in the menu."
  19. } else {
  20. $w.msg configure -text "This window contains a menubar with cascaded menus. You can post a menu from the keyboard by typing Alt+x, where \"x\" is the character underlined on the menu. You can then traverse among the menus using the arrow keys. When a menu is posted, you can invoke the current entry by typing space, or you can invoke any entry by typing its underlined character. If a menu entry has an accelerator, you can invoke the entry without posting the menu just by typing the accelerator. The rightmost menu can be torn off into a palette by selecting the first item in the menu."
  21. }
  22. pack $w.msg -side top
  23. set menustatus " "
  24. frame $w.statusBar
  25. label $w.statusBar.label -textvariable menustatus -relief sunken -bd 1 -font "Helvetica 10" -anchor w
  26. pack $w.statusBar.label -side left -padx 2 -expand yes -fill both
  27. pack $w.statusBar -side bottom -fill x -pady 2
  28. ## See Code / Dismiss buttons
  29. set btns [addSeeDismiss $w.buttons $w]
  30. pack $btns -side bottom -fill x
  31. menu $w.menu -tearoff 0
  32. set m $w.menu.file
  33. menu $m -tearoff 0
  34. $w.menu add cascade -label "File" -menu $m -underline 0
  35. $m add command -label "Open..." -command {error "this is just a demo: no action has been defined for the \"Open...\" entry"}
  36. $m add command -label "New" -command {error "this is just a demo: no action has been defined for the \"New\" entry"}
  37. $m add command -label "Save" -command {error "this is just a demo: no action has been defined for the \"Save\" entry"}
  38. $m add command -label "Save As..." -command {error "this is just a demo: no action has been defined for the \"Save As...\" entry"}
  39. $m add separator
  40. $m add command -label "Print Setup..." -command {error "this is just a demo: no action has been defined for the \"Print Setup...\" entry"}
  41. $m add command -label "Print..." -command {error "this is just a demo: no action has been defined for the \"Print...\" entry"}
  42. $m add separator
  43. $m add command -label "Dismiss Menus Demo" -command "destroy $w"
  44. set m $w.menu.basic
  45. $w.menu add cascade -label "Basic" -menu $m -underline 0
  46. menu $m -tearoff 0
  47. $m add command -label "Long entry that does nothing"
  48. if {[tk windowingsystem] eq "aqua"} {
  49. set modifier Command
  50. } elseif {[tk windowingsystem] == "win32"} {
  51. set modifier Control
  52. } else {
  53. set modifier Meta
  54. }
  55. foreach i {A B C D E F} {
  56. $m add command -label "Print letter \"$i\"" -underline 14 \
  57. -accelerator $modifier+$i -command "puts $i"
  58. bind $w <$modifier-[string tolower $i]> "puts $i"
  59. }
  60. set m $w.menu.cascade
  61. $w.menu add cascade -label "Cascades" -menu $m -underline 0
  62. menu $m -tearoff 0
  63. $m add command -label "Print hello" \
  64. -command {puts stdout "Hello"} -accelerator $modifier+H -underline 6
  65. bind $w <$modifier-h> {puts stdout "Hello"}
  66. $m add command -label "Print goodbye" -command {\
  67. puts stdout "Goodbye"} -accelerator $modifier+G -underline 6
  68. bind $w <$modifier-g> {puts stdout "Goodbye"}
  69. $m add cascade -label "Check buttons" \
  70. -menu $w.menu.cascade.check -underline 0
  71. $m add cascade -label "Radio buttons" \
  72. -menu $w.menu.cascade.radio -underline 0
  73. set m $w.menu.cascade.check
  74. menu $m -tearoff 0
  75. $m add check -label "Oil checked" -variable oil
  76. $m add check -label "Transmission checked" -variable trans
  77. $m add check -label "Brakes checked" -variable brakes
  78. $m add check -label "Lights checked" -variable lights
  79. $m add separator
  80. $m add command -label "Show current values" \
  81. -command "showVars $w.menu.cascade.dialog oil trans brakes lights"
  82. $m invoke 1
  83. $m invoke 3
  84. set m $w.menu.cascade.radio
  85. menu $m -tearoff 0
  86. $m add radio -label "10 point" -variable pointSize -value 10
  87. $m add radio -label "14 point" -variable pointSize -value 14
  88. $m add radio -label "18 point" -variable pointSize -value 18
  89. $m add radio -label "24 point" -variable pointSize -value 24
  90. $m add radio -label "32 point" -variable pointSize -value 32
  91. $m add sep
  92. $m add radio -label "Roman" -variable style -value roman
  93. $m add radio -label "Bold" -variable style -value bold
  94. $m add radio -label "Italic" -variable style -value italic
  95. $m add sep
  96. $m add command -label "Show current values" \
  97. -command "showVars $w.menu.cascade.dialog pointSize style"
  98. $m invoke 1
  99. $m invoke 7
  100. set m $w.menu.icon
  101. $w.menu add cascade -label "Icons" -menu $m -underline 0
  102. menu $m -tearoff 0
  103. # Main widget program sets variable tk_demoDirectory
  104. image create photo lilearth -file [file join $tk_demoDirectory \
  105. images earthmenu.png]
  106. $m add command -image lilearth \
  107. -hidemargin 1 -command [list \
  108. tk_dialog $w.pattern {Bitmap Menu Entry} \
  109. "The menu entry you invoked displays a photoimage rather than\
  110. a text string. Other than this, it is just like any other\
  111. menu entry." {} 0 OK ]
  112. foreach i {info questhead error} {
  113. $m add command -bitmap $i -hidemargin 1 -command [list \
  114. puts "You invoked the $i bitmap" ]
  115. }
  116. $m entryconfigure 2 -columnbreak 1
  117. set m $w.menu.more
  118. $w.menu add cascade -label "More" -menu $m -underline 0
  119. menu $m -tearoff 0
  120. foreach i {{An entry} {Another entry} {Does nothing} {Does almost nothing} {Make life meaningful}} {
  121. $m add command -label $i -command [list puts "You invoked \"$i\""]
  122. }
  123. set emojiLabel [encoding convertfrom utf-8 "\xF0\x9F\x98\x8D Make friends"]
  124. $m add command -label $emojiLabel -command [list puts "Menu labels can include non-BMP characters."]
  125. $m entryconfigure "Does almost nothing" -bitmap questhead -compound left \
  126. -command [list \
  127. tk_dialog $w.compound {Compound Menu Entry} \
  128. "The menu entry you invoked displays both a bitmap and a\
  129. text string. Other than this, it is just like any other\
  130. menu entry." {} 0 OK ]
  131. set m $w.menu.colors
  132. $w.menu add cascade -label "Colors" -menu $m -underline 1
  133. menu $m -tearoff 1
  134. if {[tk windowingsystem] eq "aqua"} {
  135. # Aqua ignores the -background and -foreground options, but a compound
  136. # button can be used for selecting colors.
  137. foreach i {red orange yellow green blue} {
  138. image create photo image_$i -height 16 -width 16
  139. image_$i put black -to 0 0 16 1
  140. image_$i put black -to 0 1 1 16
  141. image_$i put black -to 0 15 16 16
  142. image_$i put black -to 15 1 16 16
  143. image_$i put $i -to 1 1 15 15
  144. $m add command -label $i -image image_$i -compound left -command [list \
  145. puts "You invoked \"$i\"" ]
  146. }
  147. } else {
  148. foreach i {red orange yellow green blue} {
  149. $m add command -label $i -background $i -command [list \
  150. puts "You invoked \"$i\"" ]
  151. }
  152. }
  153. $w configure -menu $w.menu
  154. bind Menu <<MenuSelect>> {
  155. global $menustatus
  156. if {[catch {%W entrycget active -label} label]} {
  157. set label " "
  158. }
  159. set menustatus $label
  160. update idletasks
  161. }
  162. if {[tk windowingsystem] eq "aqua"} {catch {set ::tk::mac::useCustomMDEF $origUseCustomMDEF}}