cursors.tcl 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. #
  2. # Map symbolic cursor names to platform-appropriate cursors.
  3. #
  4. # The following cursors are defined:
  5. #
  6. # standard -- default cursor for most controls
  7. # "" -- inherit cursor from parent window
  8. # none -- no cursor
  9. #
  10. # text -- editable widgets (entry, text)
  11. # link -- hyperlinks within text
  12. # crosshair -- graphic selection, fine control
  13. # busy -- operation in progress
  14. # forbidden -- action not allowed
  15. #
  16. # hresize -- horizontal resizing
  17. # vresize -- vertical resizing
  18. #
  19. # Also resize cursors for each of the compass points,
  20. # {nw,n,ne,w,e,sw,s,se}resize.
  21. #
  22. # Platform notes:
  23. #
  24. # Windows doesn't distinguish resizing at the 8 compass points,
  25. # only horizontal, vertical, and the two diagonals.
  26. #
  27. # OSX doesn't have resize cursors for nw, ne, sw, or se corners.
  28. # We use the Tk-defined X11 fallbacks for these.
  29. #
  30. # X11 doesn't have a "forbidden" cursor (usually a slashed circle);
  31. # "pirate" seems to be the conventional cursor for this purpose.
  32. #
  33. # Windows has an IDC_HELP cursor, but it's not available from Tk.
  34. #
  35. # Tk does not support "none" on Windows.
  36. #
  37. namespace eval ttk {
  38. variable Cursors
  39. # Use X11 cursor names as defaults, since Tk supplies these
  40. # on all platforms.
  41. #
  42. array set Cursors {
  43. "" ""
  44. none none
  45. standard left_ptr
  46. text xterm
  47. link hand2
  48. crosshair crosshair
  49. busy watch
  50. forbidden pirate
  51. hresize sb_h_double_arrow
  52. vresize sb_v_double_arrow
  53. nresize top_side
  54. sresize bottom_side
  55. wresize left_side
  56. eresize right_side
  57. nwresize top_left_corner
  58. neresize top_right_corner
  59. swresize bottom_left_corner
  60. seresize bottom_right_corner
  61. move fleur
  62. }
  63. # Platform-specific overrides for Windows and OSX.
  64. #
  65. switch [tk windowingsystem] {
  66. "win32" {
  67. array set Cursors {
  68. none {}
  69. standard arrow
  70. text ibeam
  71. link hand2
  72. crosshair crosshair
  73. busy wait
  74. forbidden no
  75. vresize size_ns
  76. nresize size_ns
  77. sresize size_ns
  78. wresize size_we
  79. eresize size_we
  80. hresize size_we
  81. nwresize size_nw_se
  82. swresize size_ne_sw
  83. neresize size_ne_sw
  84. seresize size_nw_se
  85. }
  86. }
  87. "aqua" {
  88. array set Cursors {
  89. standard arrow
  90. text ibeam
  91. link pointinghand
  92. crosshair crosshair
  93. busy watch
  94. forbidden notallowed
  95. hresize resizeleftright
  96. vresize resizeupdown
  97. nresize resizeup
  98. sresize resizedown
  99. wresize resizeleft
  100. eresize resizeright
  101. }
  102. }
  103. }
  104. }
  105. ## ttk::cursor $cursor --
  106. # Return platform-specific cursor for specified symbolic cursor.
  107. #
  108. proc ttk::cursor {name} {
  109. variable Cursors
  110. return $Cursors($name)
  111. }
  112. ## ttk::setCursor $w $cursor --
  113. # Set the cursor for specified window.
  114. #
  115. # [ttk::setCursor] should be used in <Motion> bindings
  116. # instead of directly calling [$w configure -cursor ...],
  117. # as the latter always incurs a server round-trip and
  118. # can lead to high CPU load (see [#1184746])
  119. #
  120. proc ttk::setCursor {w name} {
  121. variable Cursors
  122. if {[info exists Cursors($name)]} {
  123. set cursorname $Cursors($name)
  124. } else {
  125. set cursorname $name
  126. }
  127. if {[$w cget -cursor] ne $cursorname} {
  128. $w configure -cursor $cursorname
  129. }
  130. }
  131. ## ttk::saveCursor $w $saveVar $excludeList --
  132. # Set variable $saveVar to the -cursor value from widget $w,
  133. # if either:
  134. # a. $saveVar does not yet exist
  135. # b. the currently user-specified cursor for $w is not in
  136. # $excludeList
  137. proc ttk::saveCursor {w saveVar excludeList} {
  138. upvar $saveVar sv
  139. if {![info exists sv]} {
  140. set sv [$w cget -cursor]
  141. }
  142. if {[$w cget -cursor] ni $excludeList} {
  143. set sv [$w cget -cursor]
  144. }
  145. }
  146. ## Interactive test harness:
  147. #
  148. proc ttk::CursorSampler {f} {
  149. ttk::frame $f
  150. set r 0
  151. foreach row {
  152. {nwresize nresize neresize}
  153. { wresize move eresize}
  154. {swresize sresize seresize}
  155. {text link crosshair}
  156. {hresize vresize ""}
  157. {busy forbidden ""}
  158. {none standard ""}
  159. } {
  160. set c 0
  161. foreach cursor $row {
  162. set w $f.${r}${c}
  163. ttk::label $w -text $cursor -cursor [ttk::cursor $cursor] \
  164. -relief solid -borderwidth 1 -padding 3
  165. grid $w -row $r -column $c -sticky nswe
  166. grid columnconfigure $f $c -uniform cols -weight 1
  167. incr c
  168. }
  169. grid rowconfigure $f $r -uniform rows -weight 1
  170. incr r
  171. }
  172. return $f
  173. }
  174. if {[info exists argv0] && $argv0 eq [info script]} {
  175. wm title . "[array size ::ttk::Cursors] cursors"
  176. pack [ttk::CursorSampler .f] -expand true -fill both
  177. bind . <Escape> [list destroy .]
  178. focus .f
  179. }
  180. #*EOF*