menubutton.tcl 6.3 KB

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