menubutton.tcl 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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: <Key-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 <ButtonPress> 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 <Key-space> { ttk::menubutton::Popdown %W }
  48. bind TMenubutton <<Invoke>> { ttk::menubutton::Popdown %W }
  49. if {[tk windowingsystem] eq "x11"} {
  50. bind TMenubutton <ButtonPress-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 <ButtonPress-1> \
  55. { %W state pressed ; ttk::menubutton::Popdown %W }
  56. bind TMenubutton <ButtonRelease-1> \
  57. { %W state !pressed }
  58. }
  59. # PostPosition --
  60. # Returns the x and y coordinates where the menu
  61. # should be posted, based on the menubutton and menu size
  62. # and -direction option.
  63. #
  64. # TODO: adjust menu width to be at least as wide as the button
  65. # for -direction above, below.
  66. #
  67. proc ttk::menubutton::PostPosition {mb menu} {
  68. set x [winfo rootx $mb]
  69. set y [winfo rooty $mb]
  70. set dir [$mb cget -direction]
  71. set bw [winfo width $mb]
  72. set bh [winfo height $mb]
  73. set mw [winfo reqwidth $menu]
  74. set mh [winfo reqheight $menu]
  75. set sw [expr {[winfo screenwidth $menu] - $bw - $mw}]
  76. set sh [expr {[winfo screenheight $menu] - $bh - $mh}]
  77. switch -- $dir {
  78. above { if {$y >= $mh} { incr y -$mh } { incr y $bh } }
  79. below { if {$y <= $sh} { incr y $bh } { incr y -$mh } }
  80. left { if {$x >= $mw} { incr x -$mw } { incr x $bw } }
  81. right { if {$x <= $sw} { incr x $bw } { incr x -$mw } }
  82. flush {
  83. # post menu atop menubutton.
  84. # If there's a menu entry whose label matches the
  85. # menubutton -text, assume this is an optionmenu
  86. # and place that entry over the menubutton.
  87. set index [FindMenuEntry $menu [$mb cget -text]]
  88. if {$index ne ""} {
  89. incr y -[$menu yposition $index]
  90. }
  91. }
  92. }
  93. return [list $x $y]
  94. }
  95. # Popdown --
  96. # Post the menu and set a grab on the menu.
  97. #
  98. proc ttk::menubutton::Popdown {mb} {
  99. if {[$mb instate disabled] || [set menu [$mb cget -menu]] eq ""} {
  100. return
  101. }
  102. foreach {x y} [PostPosition $mb $menu] { break }
  103. tk_popup $menu $x $y
  104. }
  105. # Pulldown (X11 only) --
  106. # Called when Button1 is pressed on a menubutton.
  107. # Posts the menu; a subsequent ButtonRelease
  108. # or Leave event will set a grab on the menu.
  109. #
  110. proc ttk::menubutton::Pulldown {mb} {
  111. variable State
  112. if {[$mb instate disabled] || [set menu [$mb cget -menu]] eq ""} {
  113. return
  114. }
  115. foreach {x y} [PostPosition $mb $menu] { break }
  116. set State(pulldown) 1
  117. set State(oldcursor) [$mb cget -cursor]
  118. $mb state pressed
  119. $mb configure -cursor [$menu cget -cursor]
  120. $menu post $x $y
  121. tk_menuSetFocus $menu
  122. }
  123. # TransferGrab (X11 only) --
  124. # Switch from pulldown mode (menubutton has an implicit grab)
  125. # to popdown mode (menu has an explicit grab).
  126. #
  127. proc ttk::menubutton::TransferGrab {mb} {
  128. variable State
  129. if {$State(pulldown)} {
  130. $mb configure -cursor $State(oldcursor)
  131. $mb state {!pressed !active}
  132. set State(pulldown) 0
  133. set menu [$mb cget -menu]
  134. tk_popup $menu [winfo rootx $menu] [winfo rooty $menu]
  135. }
  136. }
  137. # FindMenuEntry --
  138. # Hack to support tk_optionMenus.
  139. # Returns the index of the menu entry with a matching -label,
  140. # -1 if not found.
  141. #
  142. proc ttk::menubutton::FindMenuEntry {menu s} {
  143. set last [$menu index last]
  144. if {$last eq "none"} {
  145. return ""
  146. }
  147. for {set i 0} {$i <= $last} {incr i} {
  148. if {![catch {$menu entrycget $i -label} label]
  149. && ($label eq $s)} {
  150. return $i
  151. }
  152. }
  153. return ""
  154. }
  155. #*EOF*