VResize.tcl 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. # -*- mode: TCL; fill-column: 75; tab-width: 8; coding: iso-latin-1-unix -*-
  2. #
  3. # $Id: VResize.tcl,v 1.3 2004/03/28 02:44:57 hobbs Exp $
  4. #
  5. # VResize.tcl --
  6. #
  7. # tixVResize:
  8. # Virtual base class for all classes that provide resize capability,
  9. # such as the resize handle and the MDI client window.
  10. #
  11. # Copyright (c) 1993-1999 Ioi Kim Lam.
  12. # Copyright (c) 2000-2001 Tix Project Group.
  13. # Copyright (c) 2004 ActiveState
  14. #
  15. # See the file "license.terms" for information on usage and redistribution
  16. # of this file, and for a DISCLAIMER OF ALL WARRANTIES.
  17. #
  18. tixWidgetClass tixVResize {
  19. -virtual true
  20. -classname TixVResize
  21. -superclass tixPrimitive
  22. -method {
  23. drag dragend dragstart
  24. }
  25. -flag {
  26. -gridded -gridx -gridy -minwidth -minheight
  27. }
  28. -configspec {
  29. {-gridded gridded Gridded false}
  30. {-gridx gridX Grid 10}
  31. {-gridy gridY Grid 10}
  32. {-minwidth minWidth MinWidth 0}
  33. {-minheight minHeight MinHeight 0}
  34. }
  35. }
  36. proc tixVResize:InitWidgetRec {w} {
  37. upvar #0 $w data
  38. tixChainMethod $w InitWidgetRec
  39. set data(movePending) 0
  40. set data(aborted) 0
  41. set data(depress) 0
  42. }
  43. #----------------------------------------------------------------------
  44. # Public methods
  45. #----------------------------------------------------------------------
  46. # Start dragging a window
  47. #
  48. proc tixVResize:dragstart {w win depress rootx rooty wrect mrect} {
  49. upvar #0 $w data
  50. set data(rootx) $rootx
  51. set data(rooty) $rooty
  52. set data(mx) [lindex $mrect 0]
  53. set data(my) [lindex $mrect 1]
  54. set data(mw) [lindex $mrect 2]
  55. set data(mh) [lindex $mrect 3]
  56. set data(fx) [lindex $wrect 0]
  57. set data(fy) [lindex $wrect 1]
  58. set data(fw) [lindex $wrect 2]
  59. set data(fh) [lindex $wrect 3]
  60. set data(old_x) [lindex $wrect 0]
  61. set data(old_y) [lindex $wrect 1]
  62. set data(old_w) [lindex $wrect 2]
  63. set data(old_h) [lindex $wrect 3]
  64. if {$data(mw) < 0} {
  65. set data(maxx) [expr {$data(fx) + $data(old_w) - $data(-minwidth)}]
  66. } else {
  67. set data(maxx) 32000
  68. }
  69. if {$data(mh) < 0} {
  70. set data(maxy) [expr {$data(fy) + $data(old_h) - $data(-minheight)}]
  71. } else {
  72. set data(maxy) 32000
  73. }
  74. set data(aborted) 0
  75. tixCallMethod $w ShowHintFrame
  76. tixCallMethod $w SetHintFrame $data(fx) $data(fy) $data(fw) $data(fh)
  77. # Grab so that all button events are captured
  78. #
  79. grab $win
  80. focus $win
  81. set data(depress) $depress
  82. if {$depress} {
  83. set data(oldRelief) [$win cget -relief]
  84. $win config -relief sunken
  85. }
  86. }
  87. proc tixVResize:drag {w rootx rooty} {
  88. upvar #0 $w data
  89. if {$data(aborted) == 0} {
  90. set data(newrootx) $rootx
  91. set data(newrooty) $rooty
  92. if {$data(movePending) == 0} {
  93. set data(movePending) 1
  94. after 2 tixVResize:DragCompressed $w
  95. }
  96. }
  97. }
  98. proc tixVResize:dragend {w win isAbort rootx rooty} {
  99. upvar #0 $w data
  100. if {$data(aborted)} {
  101. if {$isAbort == 0} {
  102. grab release $win
  103. }
  104. return
  105. }
  106. # Just in case some draggings are not applied.
  107. #
  108. update
  109. tixCallMethod $w HideHintFrame
  110. if {$isAbort} {
  111. set data(aborted) 1
  112. } else {
  113. # Apply the changes
  114. #
  115. tixCallMethod $w UpdateSize $data(fx) $data(fy) $data(fw) $data(fh)
  116. # Release the grab
  117. #
  118. grab release $win
  119. }
  120. if {$data(depress)} {
  121. $win config -relief $data(oldRelief)
  122. }
  123. }
  124. #----------------------------------------------------------------------
  125. # Internal methods
  126. #----------------------------------------------------------------------
  127. proc tixVResize:DragCompressed {w} {
  128. if {![winfo exists $w]} {
  129. return
  130. }
  131. upvar #0 $w data
  132. if {$data(aborted) == 1 || $data(movePending) == 0} {
  133. return
  134. }
  135. set dx [expr {$data(newrootx) - $data(rootx)}]
  136. set dy [expr {$data(newrooty) - $data(rooty)}]
  137. set data(fx) [expr {$data(old_x) + ($dx * $data(mx))}]
  138. set data(fy) [expr {$data(old_y) + ($dy * $data(my))}]
  139. set data(fw) [expr {$data(old_w) + ($dx * $data(mw))}]
  140. set data(fh) [expr {$data(old_h) + ($dy * $data(mh))}]
  141. if {$data(fw) < $data(-minwidth)} {
  142. set data(fw) $data(-minwidth)
  143. }
  144. if {$data(fh) < $data(-minheight)} {
  145. set data(fh) $data(-minheight)
  146. }
  147. if {$data(fx) > $data(maxx)} {
  148. set data(fx) $data(maxx)
  149. }
  150. if {$data(fy) > $data(maxy)} {
  151. set data(fy) $data(maxy)
  152. }
  153. # If we need grid, set x,y,w,h to fit the grid
  154. #
  155. # *note* grid overrides minwidth and maxwidth ...
  156. #
  157. if {$data(-gridded)} {
  158. set data(fx) [expr {round(double($data(fx))/$data(-gridx)) * $data(-gridx)}]
  159. set data(fy) [expr {round(double($data(fy))/$data(-gridy)) * $data(-gridy)}]
  160. set fx2 [expr {$data(fx) + $data(fw) - 2}]
  161. set fy2 [expr {$data(fy) + $data(fh) - 2}]
  162. set fx2 [expr {round(double($fx2)/$data(-gridx)) * $data(-gridx)}]
  163. set fy2 [expr {round(double($fy2)/$data(-gridy)) * $data(-gridy)}]
  164. set data(fw) [expr {$fx2 - $data(fx) + 1}]
  165. set data(fh) [expr {$fy2 - $data(fy) + 1}]
  166. }
  167. tixCallMethod $w SetHintFrame $data(fx) $data(fy) $data(fw) $data(fh)
  168. update idletasks
  169. set data(movePending) 0
  170. }