IconView.tcl 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. # -*- mode: TCL; fill-column: 75; tab-width: 8; coding: iso-latin-1-unix -*-
  2. #
  3. # $Id: IconView.tcl,v 1.2 2001/12/09 05:04:02 idiscovery Exp $
  4. #
  5. # IconView.tcl --
  6. #
  7. # This file implements the Icon View widget: the "icon" view mode of
  8. # the MultiView widget. It implements:
  9. #
  10. # (1) Creation of the icons in the canvas subwidget.
  11. # (2) Automatic arrangement of the objects
  12. #
  13. # Copyright (c) 1993-1999 Ioi Kim Lam.
  14. # Copyright (c) 2000-2001 Tix Project Group.
  15. #
  16. # See the file "license.terms" for information on usage and redistribution
  17. # of this file, and for a DISCLAIMER OF ALL WARRANTIES.
  18. #
  19. tixWidgetClass tixIconView {
  20. -classname TixIconView
  21. -superclass tixCObjView
  22. -method {
  23. add arrange
  24. }
  25. -flag {
  26. -autoarrange
  27. }
  28. -static {
  29. }
  30. -configspec {
  31. {-autoarrange autoArrange AutoArrange 0 tixVerifyBoolean}
  32. }
  33. -default {
  34. {.scrollbar auto}
  35. {*borderWidth 1}
  36. {*canvas.background #c3c3c3}
  37. {*canvas.highlightBackground #d9d9d9}
  38. {*canvas.relief sunken}
  39. {*canvas.takeFocus 1}
  40. {*Scrollbar.takeFocus 0}
  41. }
  42. -forcecall {
  43. }
  44. }
  45. proc tixIconView:ConstructWidget {w} {
  46. upvar #0 $w data
  47. tixChainMethod $w ConstructWidget
  48. }
  49. proc tixIconView:SetBindings {w} {
  50. upvar #0 $w data
  51. tixChainMethod $w SetBindings
  52. set c $data(w:canvas)
  53. bind $c <1> "tixIconView:StartDrag $w %x %y"
  54. bind $c <B1-Motion> "tixIconView:Drag $w %x %y"
  55. bind $c <ButtonRelease-1> "tixIconView:EndDrag $w"
  56. }
  57. proc tixIconView:StartDrag {w x y} {
  58. upvar #0 $w data
  59. global lastX lastY
  60. set c $data(w:canvas)
  61. $c raise current
  62. set lastX [$c canvasx $x]
  63. set lastY [$c canvasy $y]
  64. }
  65. proc tixIconView:Drag {w x y} {
  66. upvar #0 $w data
  67. global lastX lastY
  68. set c $data(w:canvas)
  69. set x [$c canvasx $x]
  70. set y [$c canvasy $y]
  71. $c move current [expr $x-$lastX] [expr $y-$lastY]
  72. set lastX $x
  73. set lastY $y
  74. }
  75. proc tixIconView:EndDrag {w} {
  76. upvar #0 $w data
  77. tixCallMethod $w adjustscrollregion
  78. }
  79. #----------------------------------------------------------------------
  80. #
  81. # option configs
  82. #----------------------------------------------------------------------
  83. proc tixIconView:add {w tag image text} {
  84. upvar #0 $w data
  85. set cmp [image create compound -window $data(w:canvas)]
  86. $cmp add image -image $image
  87. $cmp add line
  88. $cmp add text -text $text
  89. set id [$data(w:canvas) create image 0 0 -image $cmp -anchor nw]
  90. $data(w:canvas) addtag $tag withtag $id
  91. if {$data(-autoarrange)} {
  92. tixWidgetDoWhenIdle tixIconView:Arrange $w 1
  93. }
  94. }
  95. # Do it in an idle handler, so that Arrange is not called before the window
  96. # is properly mapped.
  97. #
  98. proc tixIconView:arrange {w} {
  99. tixWidgetDoWhenIdle tixIconView:Arrange $w 1
  100. }
  101. proc tixIconView:PackOneRow {w row y maxH bd padX padY} {
  102. upvar #0 $w data
  103. set iX [expr $bd+$padX]
  104. foreach i $row {
  105. set box [$data(w:canvas) bbox $i]
  106. set W [expr [lindex $box 2]-[lindex $box 0]+1]
  107. set H [expr [lindex $box 3]-[lindex $box 1]+1]
  108. set iY [expr $y + $maxH - $H]
  109. $data(w:canvas) coords $i $iX $iY
  110. incr iX [expr $W+$padX]
  111. }
  112. }
  113. # virtual method
  114. #
  115. proc tixIconView:PlaceWindow {w} {
  116. upvar #0 $w data
  117. if {$data(-autoarrange)} {
  118. tixWidgetDoWhenIdle tixIconView:Arrange $w 0
  119. }
  120. tixChainMethod $w PlaceWindow
  121. }
  122. proc tixIconView:Arrange {w adjust} {
  123. upvar #0 $w data
  124. set padX 2
  125. set padY 2
  126. tixIconView:ArrangeGrid $w $padX $padY
  127. if {$adjust} {
  128. tixCallMethod $w adjustscrollregion
  129. }
  130. }
  131. # the items are not packed
  132. #
  133. proc tixIconView:ArrangeGrid {w padX padY} {
  134. upvar #0 $w data
  135. set maxW 0
  136. set maxH 0
  137. foreach item [$data(w:canvas) find all] {
  138. set box [$data(w:canvas) bbox $item]
  139. set itemW [expr [lindex $box 2]-[lindex $box 0]+1]
  140. set itemH [expr [lindex $box 3]-[lindex $box 1]+1]
  141. if {$maxW < $itemW} {
  142. set maxW $itemW
  143. }
  144. if {$maxH < $itemH} {
  145. set maxH $itemH
  146. }
  147. }
  148. if {$maxW == 0 || $maxH == 0} {
  149. return
  150. }
  151. set winW [tixWinWidth $data(w:canvas)]
  152. set bd [expr [$data(w:canvas) cget -bd]+\
  153. [$data(w:canvas) cget -highlightthickness]]
  154. set cols [expr $winW / ($maxW+$padX)]
  155. if {$cols < 1} {
  156. set cols 1
  157. }
  158. set y $bd
  159. set c 0
  160. set x $bd
  161. foreach item [$data(w:canvas) find all] {
  162. set box [$data(w:canvas) bbox $item]
  163. set itemW [expr [lindex $box 2]-[lindex $box 0]+1]
  164. set itemH [expr [lindex $box 3]-[lindex $box 1]+1]
  165. set iX [expr $x + $padX + ($maxW-$itemW)/2]
  166. set iY [expr $y + $padY + ($maxH-$itemH) ]
  167. $data(w:canvas) coords $item $iX $iY
  168. incr c
  169. incr x [expr $maxW + $padY]
  170. if {$c == $cols} {
  171. set c 0
  172. incr y [expr $maxH + $padY]
  173. set x $bd
  174. }
  175. }
  176. }
  177. # the items are packed
  178. #
  179. proc tixIconView:ArrangePack {w padX padY} {
  180. upvar #0 $w data
  181. set winW [tixWinWidth $data(w:canvas)]
  182. set bd [expr [$data(w:canvas) cget -bd]+\
  183. [$data(w:canvas) cget -highlightthickness]]
  184. set y [expr $bd + $padY]
  185. set maxH 0
  186. set usedW $padX
  187. set row ""
  188. foreach item [$data(w:canvas) find all] {
  189. set box [$data(w:canvas) bbox $item]
  190. set itemW [expr [lindex $box 2]-[lindex $box 0]+1]
  191. set itemH [expr [lindex $box 3]-[lindex $box 1]+1]
  192. if {[expr $usedW + $itemW] > $winW} {
  193. if {$row == ""} {
  194. # only one item in this row
  195. #
  196. $data(w:canvas) coords $item [expr $bd + $padX] $y
  197. incr y [expr $itemH+$padY]
  198. continue
  199. } else {
  200. # this item is not in this row. Arrange the previous items
  201. # first
  202. #
  203. tixIconView:PackOneRow $w $row $y $maxH $bd $padX $padY
  204. incr y $maxH
  205. set row ""
  206. set maxH 0
  207. set usedW $padX
  208. }
  209. }
  210. lappend row $item
  211. if {$maxH < $itemH} {
  212. set maxH $itemH
  213. }
  214. incr usedW [expr $padX+$itemW]
  215. }
  216. if {$row != ""} {
  217. tixIconView:PackOneRow $w $row $y $maxH $bd $padX $padY
  218. }
  219. }
  220. #----------------------------------------------------------------------
  221. #
  222. # Widget commands
  223. #----------------------------------------------------------------------
  224. #----------------------------------------------------------------------
  225. #
  226. # Private Methods
  227. #----------------------------------------------------------------------