menubutton.tcl 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. #
  2. # Bindings for Menubuttons.
  3. #
  4. # Menubuttons have three interaction modes:
  5. #
  6. # Pulldown: Press menubutton, drag over menu, release to activate menu entry
  7. # Popdown: Click menubutton to post menu
  8. # Keyboard: <space> or accelerator key to post menu
  9. #
  10. # (In addition, when menu system is active, "dropdown" -- menu posts
  11. # on mouse-over. Ttk menubuttons don't implement this).
  12. #
  13. # For keyboard and popdown mode, we hand off to tk_popup and let
  14. # the built-in Tk bindings handle the rest of the interaction.
  15. #
  16. # ON X11:
  17. #
  18. # Standard Tk menubuttons use a global grab on the menubutton.
  19. # This won't work for Ttk menubuttons in pulldown mode,
  20. # since we need to process the final <ButtonRelease> event,
  21. # and this might be delivered to the menu. So instead we
  22. # rely on the passive grab that occurs on <Button> events,
  23. # and transition to popdown mode when the mouse is released
  24. # or dragged outside the menubutton.
  25. #
  26. # ON WINDOWS:
  27. #
  28. # I'm not sure what the hell is going on here. [$menu post] apparently
  29. # sets up some kind of internal grab for native menus.
  30. # On this platform, just use [tk_popup] for all menu actions.
  31. #
  32. # ON MACOS:
  33. #
  34. # Same probably applies here.
  35. #
  36. namespace eval ttk {
  37. namespace eval menubutton {
  38. variable State
  39. array set State {
  40. pulldown 0
  41. oldcursor {}
  42. }
  43. }
  44. }
  45. bind TMenubutton <Enter> { %W instate !disabled {%W state active } }
  46. bind TMenubutton <Leave> { %W state !active }
  47. bind TMenubutton <space> { ttk::menubutton::Popdown %W }
  48. bind TMenubutton <<Invoke>> { ttk::menubutton::Popdown %W }
  49. if {[tk windowingsystem] eq "x11"} {
  50. bind TMenubutton <Button-1> { ttk::menubutton::Pulldown %W }
  51. bind TMenubutton <ButtonRelease-1> { ttk::menubutton::TransferGrab %W }
  52. bind TMenubutton <B1-Leave> { ttk::menubutton::TransferGrab %W }
  53. } else {
  54. bind TMenubutton <Button-1> \
  55. { %W state pressed ; ttk::menubutton::Popdown %W }
  56. bind TMenubutton <ButtonRelease-1> \
  57. { if {[winfo exists %W]} { %W state !pressed } }
  58. }
  59. # PostPosition --
  60. # Returns x and y coordinates and a menu item index.
  61. # If the index is not an empty string the menu should
  62. # be posted so that the upper left corner of the indexed
  63. # menu item is located at the point (x, y). Otherwise
  64. # the top left corner of the menu itself should be located
  65. # at that point.
  66. #
  67. # TODO: adjust menu width to be at least as wide as the button
  68. # for -direction above, below.
  69. #
  70. if {[tk windowingsystem] eq "aqua"} {
  71. proc ::ttk::menubutton::PostPosition {mb menu} {
  72. set menuPad 5
  73. set buttonPad 1
  74. set bevelPad 4
  75. set mh [winfo reqheight $menu]
  76. set bh [expr {[winfo height $mb]} + $buttonPad]
  77. set bbh [expr {[winfo height $mb]} + $bevelPad]
  78. set mw [winfo reqwidth $menu]
  79. set bw [winfo width $mb]
  80. set entry [::tk::MenuFindName $menu [$mb cget -text]]
  81. if {$entry < 0} {
  82. set entry 0
  83. }
  84. set x [winfo rootx $mb]
  85. set y [winfo rooty $mb]
  86. switch [$mb cget -direction] {
  87. above {
  88. set entry ""
  89. incr y [expr {-$mh + 2 * $menuPad}]
  90. }
  91. below {
  92. set entry ""
  93. incr y $bh
  94. }
  95. left {
  96. incr y $menuPad
  97. incr x -$mw
  98. }
  99. right {
  100. incr y $menuPad
  101. incr x $bw
  102. }
  103. default { # flush
  104. incr y $bbh
  105. }
  106. }
  107. return [list $x $y $entry]
  108. }
  109. } else {
  110. proc ::ttk::menubutton::PostPosition {mb menu} {
  111. set mh [expr {[winfo reqheight $menu]}]
  112. set bh [expr {[winfo height $mb]}]
  113. set mw [expr {[winfo reqwidth $menu]}]
  114. set bw [expr {[winfo width $mb]}]
  115. if {[tk windowingsystem] eq "win32"} {
  116. incr mh 6
  117. incr mw 16
  118. }
  119. set entry [::tk::MenuFindName $menu [$mb cget -text]]
  120. if {$entry < 0} {
  121. set entry 0
  122. }
  123. set x [winfo rootx $mb]
  124. set y [winfo rooty $mb]
  125. switch [$mb cget -direction] {
  126. above {
  127. set entry ""
  128. incr y -$mh
  129. # if we go offscreen to the top, show as 'below'
  130. if {$y < [winfo vrooty $mb]} {
  131. set y [expr {[winfo vrooty $mb] + [winfo rooty $mb]\
  132. + [winfo reqheight $mb]}]
  133. }
  134. }
  135. below {
  136. set entry ""
  137. incr y $bh
  138. # if we go offscreen to the bottom, show as 'above'
  139. if {($y + $mh) > ([winfo vrooty $mb] + [winfo vrootheight $mb])} {
  140. set y [expr {[winfo vrooty $mb] + [winfo rooty $mb] - $mh}]
  141. }
  142. }
  143. left {
  144. incr x -$mw
  145. }
  146. right {
  147. incr x $bw
  148. }
  149. default { # flush
  150. incr x [expr {([winfo width $mb] - [winfo reqwidth $menu])/ 2}]
  151. }
  152. }
  153. return [list $x $y $entry]
  154. }
  155. }
  156. # Popdown --
  157. # Post the menu and set a grab on the menu.
  158. #
  159. proc ttk::menubutton::Popdown {mb} {
  160. if {[$mb instate disabled] || [set menu [$mb cget -menu]] eq ""} {
  161. return
  162. }
  163. foreach {x y entry} [PostPosition $mb $menu] { break }
  164. tk_popup $menu $x $y $entry
  165. }
  166. # Pulldown (X11 only) --
  167. # Called when Button1 is pressed on a menubutton.
  168. # Posts the menu; a subsequent ButtonRelease
  169. # or Leave event will set a grab on the menu.
  170. #
  171. proc ttk::menubutton::Pulldown {mb} {
  172. variable State
  173. if {[$mb instate disabled] || [set menu [$mb cget -menu]] eq ""} {
  174. return
  175. }
  176. set State(pulldown) 1
  177. set State(oldcursor) [$mb cget -cursor]
  178. $mb state pressed
  179. $mb configure -cursor [$menu cget -cursor]
  180. foreach {x y entry} [PostPosition $mb $menu] { break }
  181. if {$entry >= 0} {
  182. $menu post $x $y $entry
  183. } else {
  184. $menu post $x $y
  185. }
  186. tk_menuSetFocus $menu
  187. }
  188. # TransferGrab (X11 only) --
  189. # Switch from pulldown mode (menubutton has an implicit grab)
  190. # to popdown mode (menu has an explicit grab).
  191. #
  192. proc ttk::menubutton::TransferGrab {mb} {
  193. variable State
  194. if {$State(pulldown)} {
  195. $mb configure -cursor $State(oldcursor)
  196. $mb state {!pressed !active}
  197. set State(pulldown) 0
  198. set menu [$mb cget -menu]
  199. foreach {x y entry} [PostPosition $mb $menu] { break }
  200. tk_popup $menu [winfo rootx $menu] [winfo rooty $menu]
  201. }
  202. }
  203. # FindMenuEntry --
  204. # Hack to support tk_optionMenus.
  205. # Returns the index of the menu entry with a matching -label,
  206. # "" if not found.
  207. #
  208. proc ttk::menubutton::FindMenuEntry {menu s} {
  209. set last [$menu index last]
  210. if {$last eq "none" || $last < 0} {
  211. return ""
  212. }
  213. for {set i 0} {$i <= $last} {incr i} {
  214. if {![catch {$menu entrycget $i -label} label]
  215. && ($label eq $s)} {
  216. return $i
  217. }
  218. }
  219. return ""
  220. }
  221. #*EOF*