CObjView.tcl 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  1. # -*- mode: TCL; fill-column: 75; tab-width: 8; coding: iso-latin-1-unix -*-
  2. #
  3. # $Id: CObjView.tcl,v 1.3 2001/12/09 05:04:02 idiscovery Exp $
  4. #
  5. # CObjView.tcl --
  6. #
  7. # This file implements the Canvas Object View widget. This is a base
  8. # class of IconView. It implements:
  9. # (1) Automatic placement/adjustment of the scrollbars according
  10. # to the canvas objects inside the canvas subwidget. The
  11. # scrollbars are adjusted so that the canvas is just large
  12. # enough to see all the objects.
  13. #
  14. # (2) D+D bindings of the objects (%% not implemented)
  15. #
  16. # (3) Keyboard traversal of the objects (%% not implemented). By the
  17. # virtual method :SelectObject.
  18. #
  19. #
  20. # Copyright (c) 1993-1999 Ioi Kim Lam.
  21. # Copyright (c) 2000-2001 Tix Project Group.
  22. #
  23. # See the file "license.terms" for information on usage and redistribution
  24. # of this file, and for a DISCLAIMER OF ALL WARRANTIES.
  25. #
  26. tixWidgetClass tixCObjView {
  27. -classname TixCObjView
  28. -superclass tixScrolledWidget
  29. -method {
  30. adjustscrollregion
  31. }
  32. -flag {
  33. -xscrollincrement -yscrollincrement
  34. }
  35. -static {
  36. }
  37. -configspec {
  38. {-xscrollincrement xScrollIncrement ScrollIncrement 10}
  39. {-yscrollincrement yScrollIncrement ScrollIncrement 10}
  40. }
  41. -default {
  42. {.scrollbar auto}
  43. {*borderWidth 1}
  44. {*canvas.background #c3c3c3}
  45. {*canvas.highlightBackground #d9d9d9}
  46. {*canvas.relief sunken}
  47. {*canvas.takeFocus 1}
  48. {*Scrollbar.takeFocus 0}
  49. }
  50. -forcecall {
  51. -scrollbar
  52. }
  53. }
  54. proc tixCObjView:ConstructWidget {w} {
  55. upvar #0 $w data
  56. tixChainMethod $w ConstructWidget
  57. set data(w:canvas) \
  58. [canvas $w.canvas]
  59. set data(w:hsb) \
  60. [scrollbar $w.hsb -orient horizontal]
  61. set data(w:vsb) \
  62. [scrollbar $w.vsb -orient vertical]
  63. set data(pw:client) $data(w:canvas)
  64. set data(xorig) 0
  65. set data(yorig) 0
  66. set data(sx1) 0
  67. set data(sy1) 0
  68. set data(sx2) 0
  69. set data(sy2) 0
  70. }
  71. proc tixCObjView:SetBindings {w} {
  72. upvar #0 $w data
  73. tixChainMethod $w SetBindings
  74. # %% scan/drag of canvas??
  75. #
  76. # $data(w:canvas) config \
  77. # -xscrollcommand "tixCObjView:XScroll $w"\
  78. # -yscrollcommand "tixCObjView:YScroll $w"
  79. $data(w:hsb) config -command "tixCObjView:UserScroll $w x"
  80. $data(w:vsb) config -command "tixCObjView:UserScroll $w y"
  81. }
  82. #----------------------------------------------------------------------
  83. #
  84. # option configs
  85. #----------------------------------------------------------------------
  86. proc tixCObjView:config-takefocus {w value} {
  87. upvar #0 $w data
  88. $data(w:canvas) config -takefocus $value
  89. }
  90. #----------------------------------------------------------------------
  91. #
  92. # Widget commands
  93. #----------------------------------------------------------------------
  94. proc tixCObjView:adjustscrollregion {w} {
  95. upvar #0 $w data
  96. set cW [tixWinWidth $data(w:canvas)]
  97. set cH [tixWinHeight $data(w:canvas)]
  98. tixCObjView:GetScrollRegion $w $cW $cH 1 1
  99. }
  100. #----------------------------------------------------------------------
  101. #
  102. # Private Methods
  103. #----------------------------------------------------------------------
  104. proc tixCObjView:GeometryInfo {w cW cH} {
  105. upvar #0 $w data
  106. set bd \
  107. [expr [$data(w:canvas) cget -bd] + [$data(w:canvas) cget -highlightthickness]]
  108. incr cW -[expr {2*$bd}]
  109. incr cH -[expr {2*$bd}]
  110. return [tixCObjView:GetScrollRegion $w $cW $cH 0 0]
  111. }
  112. proc tixCObjView:PlaceWindow {w} {
  113. upvar #0 $w data
  114. set cW [tixWinWidth $data(w:canvas)]
  115. set cH [tixWinHeight $data(w:canvas)]
  116. tixCObjView:GetScrollRegion $w $cW $cH 1 0
  117. tixChainMethod $w PlaceWindow
  118. }
  119. proc tixCObjView:GetScrollRegion {w cW cH setReg callConfig} {
  120. upvar #0 $w data
  121. set x1max $data(xorig)
  122. set y1max $data(yorig)
  123. set x2min [expr {$x1max + $cW - 1}]
  124. set y2min [expr {$y1max + $cH - 1}]
  125. set bbox [$data(w:canvas) bbox all]
  126. if {$bbox == ""} {
  127. set bbox {0 0 1 1}
  128. }
  129. set x1 [lindex $bbox 0]
  130. set y1 [lindex $bbox 1]
  131. set x2 [lindex $bbox 2]
  132. set y2 [lindex $bbox 3]
  133. set bd \
  134. [expr [$data(w:canvas) cget -bd] + [$data(w:canvas) cget -highlightthickness]]
  135. incr x1 -$bd
  136. incr y1 -$bd
  137. incr x2 -$bd
  138. incr y2 -$bd
  139. if {$x1 > $x1max} {
  140. set x1 $x1max
  141. }
  142. if {$y1 > $y1max} {
  143. set y1 $y1max
  144. }
  145. if {$x2 < $x2min} {
  146. set x2 $x2min
  147. }
  148. if {$y2 < $y2min} {
  149. set y2 $y2min
  150. }
  151. set data(sx1) $x1
  152. set data(sy1) $y1
  153. set data(sx2) $x2
  154. set data(sy2) $y2
  155. set sW [expr {$x2 - $x1 + 1}]
  156. set sH [expr {$y2 - $y1 + 1}]
  157. # puts "sregion = {$x1 $y1 $x2 $y2}; sW=$sW; cW=$cW"
  158. if {$sW > $cW} {
  159. set hsbSpec {0.5 1}
  160. } else {
  161. set hsbSpec {0 1}
  162. }
  163. if {$sH > $cH} {
  164. set vsbSpec {0.5 1}
  165. } else {
  166. set vsbSpec {0 1}
  167. }
  168. if $setReg {
  169. tixCObjView:SetScrollBars $w $cW $cH $sW $sH
  170. }
  171. if $callConfig {
  172. tixWidgetDoWhenIdle tixScrolledWidget:Configure $w
  173. }
  174. return [list $hsbSpec $vsbSpec]
  175. }
  176. #xF = xFirst
  177. #
  178. proc tixCObjView:SetScrollBars {w cW cH sW sH} {
  179. upvar #0 $w data
  180. # puts "$data(xorig) <--> $data(sx1)"
  181. set xF [expr ($data(xorig).0-$data(sx1).0)/$sW.0]
  182. set xL [expr $cW.0/$sW.0 + $xF]
  183. set yF [expr ($data(yorig).0-$data(sy1).0)/$sH.0]
  184. set yL [expr $cH.0/$sH.0 + $yF]
  185. # puts "$xF $xL : $yF $yL"
  186. $data(w:hsb) set $xF $xL
  187. $data(w:vsb) set $yF $yL
  188. }
  189. proc tixCObjView:UserScroll {w dir type args} {
  190. upvar #0 $w data
  191. $data(w:canvas) config -xscrollincrement 1 -yscrollincrement 1
  192. case $dir {
  193. x {
  194. set n $data(xorig)
  195. set orig $data(xorig)
  196. set s1 $data(sx1)
  197. set total [expr {$data(sx2)-$data(sx1)}]
  198. set page [tixWinWidth $data(w:canvas)]
  199. set min $data(sx1)
  200. set max [expr {$data(sx1)+$total-$page}]
  201. set inc $data(-xscrollincrement)
  202. }
  203. y {
  204. set n $data(yorig)
  205. set orig $data(yorig)
  206. set s1 $data(sy1)
  207. set total [expr {$data(sy2)-$data(sy1)}]
  208. set page [tixWinHeight $data(w:canvas)]
  209. set min $data(sy1)
  210. set max [expr {$data(sy1)+$total-$page}]
  211. set inc $data(-yscrollincrement)
  212. }
  213. }
  214. case $type {
  215. scroll {
  216. set amt [lindex $args 0]
  217. set unit [lindex $args 1]
  218. case $unit {
  219. units {
  220. incr n [expr int($inc)*$amt]
  221. }
  222. pages {
  223. incr n [expr {$page*$amt}]
  224. }
  225. }
  226. }
  227. moveto {
  228. set first [lindex $args 0]
  229. set n [expr round($first*$total)+$s1]
  230. }
  231. }
  232. if {$n < $min} {
  233. set n $min
  234. }
  235. if {$n > $max} {
  236. set n $max
  237. }
  238. # puts "n=$n min=$min max=$max"
  239. case $dir {
  240. x {
  241. $data(w:canvas) xview scroll [expr {$n-$orig}] units
  242. set data(xorig) $n
  243. }
  244. y {
  245. $data(w:canvas) yview scroll [expr {$n-$orig}] units
  246. set data(yorig) $n
  247. }
  248. }
  249. set cW [tixWinWidth $data(w:canvas)]
  250. set cH [tixWinHeight $data(w:canvas)]
  251. set sW [expr {$data(sx2)-$data(sx1)+1}]
  252. set sH [expr {$data(sy2)-$data(sy1)+1}]
  253. tixCObjView:SetScrollBars $w $cW $cH $sW $sH
  254. }
  255. # Junk
  256. #
  257. #
  258. proc tixCObjView:XScroll {w first last} {
  259. upvar #0 $w data
  260. set sc [$data(w:canvas) cget -scrollregion]
  261. if {$sc == ""} {
  262. set x1 1
  263. set x2 [tixWinWidth $data(w:canvas)]
  264. } else {
  265. set x1 [lindex $sc 0]
  266. set x2 [lindex $sc 2]
  267. }
  268. set W [expr {$x2 - $x1}]
  269. if {$W < 1} {
  270. set W 1
  271. }
  272. $data(w:hsb) set $first $last
  273. # tixWidgetDoWhenIdle tixScrolledWidget:Configure $w
  274. }
  275. # Junk
  276. #
  277. proc tixCObjView:YScroll {w first last} {
  278. upvar #0 $w data
  279. set sc [$data(w:canvas) cget -scrollregion]
  280. if {$sc == ""} {
  281. set y1 1
  282. set y2 [tixWinHeight $data(w:canvas)]
  283. } else {
  284. set y1 [lindex $sc 1]
  285. set y2 [lindex $sc 3]
  286. }
  287. set H [expr {$y2 - $y1}]
  288. if {$H < 1} {
  289. set H 1
  290. }
  291. $data(w:vsb) set $first $last
  292. # tixWidgetDoWhenIdle tixScrolledWidget:Configure $w
  293. }