menubutton.tcl 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  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 dF [expr {[winfo width $mb] - [winfo reqwidth $menu] - $menuPad}]
  81. set entry [::tk::MenuFindName $menu [$mb cget -text]]
  82. if {$entry < 0} {
  83. set entry 0
  84. }
  85. set x [winfo rootx $mb]
  86. set y [winfo rooty $mb]
  87. switch [$mb cget -direction] {
  88. above {
  89. set entry ""
  90. incr y [expr {-$mh + 2 * $menuPad}]
  91. }
  92. below {
  93. set entry ""
  94. incr y $bh
  95. }
  96. left {
  97. incr y $menuPad
  98. incr x -$mw
  99. }
  100. right {
  101. incr y $menuPad
  102. incr x $bw
  103. }
  104. default {
  105. incr y $bbh
  106. }
  107. }
  108. return [list $x $y $entry]
  109. }
  110. } else {
  111. proc ::ttk::menubutton::PostPosition {mb menu} {
  112. set mh [expr {[winfo reqheight $menu]}]
  113. set bh [expr {[winfo height $mb]}]
  114. set mw [expr {[winfo reqwidth $menu]}]
  115. set bw [expr {[winfo width $mb]}]
  116. set dF [expr {[winfo width $mb] - [winfo reqwidth $menu]}]
  117. if {[tk windowingsystem] eq "win32"} {
  118. incr mh 6
  119. incr mw 16
  120. }
  121. set entry [::tk::MenuFindName $menu [$mb cget -text]]
  122. if {$entry < 0} {
  123. set entry 0
  124. }
  125. set x [winfo rootx $mb]
  126. set y [winfo rooty $mb]
  127. switch [$mb cget -direction] {
  128. above {
  129. set entry ""
  130. incr y -$mh
  131. # if we go offscreen to the top, show as 'below'
  132. if {$y < [winfo vrooty $mb]} {
  133. set y [expr {[winfo vrooty $mb] + [winfo rooty $mb]\
  134. + [winfo reqheight $mb]}]
  135. }
  136. }
  137. below {
  138. set entry ""
  139. incr y $bh
  140. # if we go offscreen to the bottom, show as 'above'
  141. if {($y + $mh) > ([winfo vrooty $mb] + [winfo vrootheight $mb])} {
  142. set y [expr {[winfo vrooty $mb] + [winfo vrootheight $mb] \
  143. + [winfo rooty $mb] - $mh}]
  144. }
  145. }
  146. left {
  147. incr x -$mw
  148. }
  149. right {
  150. incr x $bw
  151. }
  152. default {
  153. if {[$mb cget -style] eq ""} {
  154. incr x [expr {([winfo width $mb] - \
  155. [winfo reqwidth $menu])/ 2}]
  156. } else {
  157. incr y $bh
  158. }
  159. }
  160. }
  161. return [list $x $y $entry]
  162. }
  163. }
  164. # Popdown --
  165. # Post the menu and set a grab on the menu.
  166. #
  167. proc ttk::menubutton::Popdown {mb} {
  168. if {[$mb instate disabled] || [set menu [$mb cget -menu]] eq ""} {
  169. return
  170. }
  171. foreach {x y entry} [PostPosition $mb $menu] { break }
  172. tk_popup $menu $x $y $entry
  173. }
  174. # Pulldown (X11 only) --
  175. # Called when Button1 is pressed on a menubutton.
  176. # Posts the menu; a subsequent ButtonRelease
  177. # or Leave event will set a grab on the menu.
  178. #
  179. proc ttk::menubutton::Pulldown {mb} {
  180. variable State
  181. if {[$mb instate disabled] || [set menu [$mb cget -menu]] eq ""} {
  182. return
  183. }
  184. set State(pulldown) 1
  185. set State(oldcursor) [$mb cget -cursor]
  186. $mb state pressed
  187. $mb configure -cursor [$menu cget -cursor]
  188. foreach {x y entry} [PostPosition $mb $menu] { break }
  189. if {$entry >= 0} {
  190. $menu post $x $y $entry
  191. } else {
  192. $menu post $x $y
  193. }
  194. tk_menuSetFocus $menu
  195. }
  196. # TransferGrab (X11 only) --
  197. # Switch from pulldown mode (menubutton has an implicit grab)
  198. # to popdown mode (menu has an explicit grab).
  199. #
  200. proc ttk::menubutton::TransferGrab {mb} {
  201. variable State
  202. if {$State(pulldown)} {
  203. $mb configure -cursor $State(oldcursor)
  204. $mb state {!pressed !active}
  205. set State(pulldown) 0
  206. set menu [$mb cget -menu]
  207. foreach {x y entry} [PostPosition $mb $menu] { break }
  208. tk_popup $menu [winfo rootx $menu] [winfo rooty $menu]
  209. }
  210. }
  211. # FindMenuEntry --
  212. # Hack to support tk_optionMenus.
  213. # Returns the index of the menu entry with a matching -label,
  214. # "" if not found.
  215. #
  216. proc ttk::menubutton::FindMenuEntry {menu s} {
  217. set last [$menu index last]
  218. if {$last eq "none" || $last < 0} {
  219. return ""
  220. }
  221. for {set i 0} {$i <= $last} {incr i} {
  222. if {![catch {$menu entrycget $i -label} label]
  223. && ($label eq $s)} {
  224. return $i
  225. }
  226. }
  227. return ""
  228. }
  229. #*EOF*