gooderp18绿色标准版
Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

235 Zeilen
6.1KB

  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 vrootheight $mb] \
  141. + [winfo rooty $mb] - $mh}]
  142. }
  143. }
  144. left {
  145. incr x -$mw
  146. }
  147. right {
  148. incr x $bw
  149. }
  150. default { # flush
  151. incr x [expr {([winfo width $mb] - [winfo reqwidth $menu])/ 2}]
  152. }
  153. }
  154. return [list $x $y $entry]
  155. }
  156. }
  157. # Popdown --
  158. # Post the menu and set a grab on the menu.
  159. #
  160. proc ttk::menubutton::Popdown {mb} {
  161. if {[$mb instate disabled] || [set menu [$mb cget -menu]] eq ""} {
  162. return
  163. }
  164. foreach {x y entry} [PostPosition $mb $menu] { break }
  165. tk_popup $menu $x $y $entry
  166. }
  167. # Pulldown (X11 only) --
  168. # Called when Button1 is pressed on a menubutton.
  169. # Posts the menu; a subsequent ButtonRelease
  170. # or Leave event will set a grab on the menu.
  171. #
  172. proc ttk::menubutton::Pulldown {mb} {
  173. variable State
  174. if {[$mb instate disabled] || [set menu [$mb cget -menu]] eq ""} {
  175. return
  176. }
  177. set State(pulldown) 1
  178. set State(oldcursor) [$mb cget -cursor]
  179. $mb state pressed
  180. $mb configure -cursor [$menu cget -cursor]
  181. foreach {x y entry} [PostPosition $mb $menu] { break }
  182. if {$entry >= 0} {
  183. $menu post $x $y $entry
  184. } else {
  185. $menu post $x $y
  186. }
  187. tk_menuSetFocus $menu
  188. }
  189. # TransferGrab (X11 only) --
  190. # Switch from pulldown mode (menubutton has an implicit grab)
  191. # to popdown mode (menu has an explicit grab).
  192. #
  193. proc ttk::menubutton::TransferGrab {mb} {
  194. variable State
  195. if {$State(pulldown)} {
  196. $mb configure -cursor $State(oldcursor)
  197. $mb state {!pressed !active}
  198. set State(pulldown) 0
  199. set menu [$mb cget -menu]
  200. foreach {x y entry} [PostPosition $mb $menu] { break }
  201. tk_popup $menu [winfo rootx $menu] [winfo rooty $menu]
  202. }
  203. }
  204. # FindMenuEntry --
  205. # Hack to support tk_optionMenus.
  206. # Returns the index of the menu entry with a matching -label,
  207. # "" if not found.
  208. #
  209. proc ttk::menubutton::FindMenuEntry {menu s} {
  210. set last [$menu index last]
  211. if {$last eq "none" || $last < 0} {
  212. return ""
  213. }
  214. for {set i 0} {$i <= $last} {incr i} {
  215. if {![catch {$menu entrycget $i -label} label]
  216. && ($label eq $s)} {
  217. return $i
  218. }
  219. }
  220. return ""
  221. }
  222. #*EOF*
上海开阖软件有限公司 沪ICP备12045867号-1