scrollview.tcl 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. # ------------------------------------------------------------------------------
  2. # scrollview.tcl
  3. # This file is part of Unifix BWidget Toolkit
  4. # $Id$
  5. # ------------------------------------------------------------------------------
  6. # Index of commands:
  7. # - ScrolledWindow::create
  8. # - ScrolledWindow::configure
  9. # - ScrolledWindow::cget
  10. # - ScrolledWindow::_set_hscroll
  11. # - ScrolledWindow::_set_vscroll
  12. # - ScrolledWindow::_update_scroll
  13. # - ScrolledWindow::_set_view
  14. # - ScrolledWindow::_resize
  15. # ------------------------------------------------------------------------------
  16. namespace eval ScrollView {
  17. Widget::declare ScrollView {
  18. {-width TkResource 30 0 canvas}
  19. {-height TkResource 30 0 canvas}
  20. {-background TkResource "" 0 canvas}
  21. {-foreground String black 0}
  22. {-fill String "" 0}
  23. {-relief TkResource flat 0 canvas}
  24. {-borderwidth TkResource 0 0 canvas}
  25. {-cursor TkResource crosshair 0 canvas}
  26. {-window String "" 0}
  27. {-fg Synonym -foreground}
  28. {-bg Synonym -background}
  29. {-bd Synonym -borderwidth}
  30. }
  31. Widget::addmap ScrollView "" :cmd \
  32. {-relief {} -borderwidth {} -background {} -width {} -height {} -cursor {}}
  33. bind BwScrollView <ButtonPress-3> {ScrollView::_set_view %W set %x %y}
  34. bind BwScrollView <ButtonPress-1> {ScrollView::_set_view %W start %x %y}
  35. bind BwScrollView <B1-Motion> {ScrollView::_set_view %W motion %x %y}
  36. bind BwScrollView <Configure> {ScrollView::_resize %W}
  37. bind BwScrollView <Destroy> {ScrollView::_destroy %W}
  38. proc ::ScrollView { path args } { return [eval ScrollView::create $path $args] }
  39. proc use {} {}
  40. variable _widget
  41. }
  42. # ------------------------------------------------------------------------------
  43. # Command ScrollView::create
  44. # ------------------------------------------------------------------------------
  45. proc ScrollView::create { path args } {
  46. variable _widget
  47. Widget::init ScrollView $path $args
  48. set w [Widget::getoption $path -window]
  49. set _widget($path,bd) [Widget::getoption $path -borderwidth]
  50. set _widget($path,width) [Widget::getoption $path -width]
  51. set _widget($path,height) [Widget::getoption $path -height]
  52. if { [winfo exists $w] } {
  53. set _widget($path,oldxscroll) [$w cget -xscrollcommand]
  54. set _widget($path,oldyscroll) [$w cget -yscrollcommand]
  55. $w configure \
  56. -xscrollcommand "ScrollView::_set_hscroll $path" \
  57. -yscrollcommand "ScrollView::_set_vscroll $path"
  58. }
  59. eval canvas $path [Widget::subcget $path :cmd] -highlightthickness 0
  60. $path create rectangle -2 -2 -2 -2 \
  61. -fill [Widget::getoption $path -fill] \
  62. -outline [Widget::getoption $path -foreground] \
  63. -tags view
  64. bindtags $path [list $path BwScrollView [winfo toplevel $path] all]
  65. rename $path ::$path:cmd
  66. proc ::$path { cmd args } "return \[eval ScrollView::\$cmd $path \$args\]"
  67. return $path
  68. }
  69. # ------------------------------------------------------------------------------
  70. # Command ScrollView::configure
  71. # ------------------------------------------------------------------------------
  72. proc ScrollView::configure { path args } {
  73. variable _widget
  74. set oldw [Widget::getoption $path -window]
  75. set res [Widget::configure $path $args]
  76. if { [Widget::hasChanged $path -window w] } {
  77. if { [winfo exists $oldw] } {
  78. $oldw configure \
  79. -xscrollcommand $_widget($path,oldxscroll) \
  80. -yscrollcommand $_widget($path,oldyscroll)
  81. }
  82. if { [winfo exists $w] } {
  83. set _widget($path,oldxscroll) [$w cget -xscrollcommand]
  84. set _widget($path,oldyscroll) [$w cget -yscrollcommand]
  85. $w configure \
  86. -xscrollcommand "ScrollView::_set_hscroll $path" \
  87. -yscrollcommand "ScrollView::_set_vscroll $path"
  88. } else {
  89. $path:cmd coords view -2 -2 -2 -2
  90. set _widget($path,oldxscroll) {}
  91. set _widget($path,oldyscroll) {}
  92. }
  93. }
  94. if { [Widget::hasChanged $path -fill fill] |
  95. [Widget::hasChanged $path -foreground fg] } {
  96. $path:cmd itemconfigure view \
  97. -fill $fill \
  98. -outline $fg
  99. }
  100. return $res
  101. }
  102. # ------------------------------------------------------------------------------
  103. # Command ScrollView::cget
  104. # ------------------------------------------------------------------------------
  105. proc ScrollView::cget { path option } {
  106. return [Widget::cget $path $option]
  107. }
  108. # ------------------------------------------------------------------------------
  109. # Command ScrollView::_destroy
  110. # ------------------------------------------------------------------------------
  111. proc ScrollView::_destroy { path } {
  112. variable _widget
  113. set w [Widget::getoption $path -window]
  114. if { [winfo exists $w] } {
  115. $w configure \
  116. -xscrollcommand $_widget($path,oldxscroll) \
  117. -yscrollcommand $_widget($path,oldyscroll)
  118. }
  119. unset _widget($path,oldxscroll)
  120. unset _widget($path,oldyscroll)
  121. unset _widget($path,bd)
  122. unset _widget($path,width)
  123. unset _widget($path,height)
  124. catch {unset _widget($path,dx)}
  125. catch {unset _widget($path,dy)}
  126. Widget::destroy $path
  127. rename $path {}
  128. }
  129. # ------------------------------------------------------------------------------
  130. # Command ScrollView::_set_hscroll
  131. # ------------------------------------------------------------------------------
  132. proc ScrollView::_set_hscroll { path vmin vmax } {
  133. variable _widget
  134. set c [$path:cmd coords view]
  135. set x0 [expr {$vmin*$_widget($path,width)+$_widget($path,bd)}]
  136. set x1 [expr {$vmax*$_widget($path,width)+$_widget($path,bd)-1}]
  137. $path:cmd coords view $x0 [lindex $c 1] $x1 [lindex $c 3]
  138. if { $_widget($path,oldxscroll) != "" } {
  139. uplevel \#0 $_widget($path,oldxscroll) $vmin $vmax
  140. }
  141. }
  142. # ------------------------------------------------------------------------------
  143. # Command ScrollView::_set_vscroll
  144. # ------------------------------------------------------------------------------
  145. proc ScrollView::_set_vscroll { path vmin vmax } {
  146. variable _widget
  147. set c [$path:cmd coords view]
  148. set y0 [expr {$vmin*$_widget($path,height)+$_widget($path,bd)}]
  149. set y1 [expr {$vmax*$_widget($path,height)+$_widget($path,bd)-1}]
  150. $path:cmd coords view [lindex $c 0] $y0 [lindex $c 2] $y1
  151. if { $_widget($path,oldyscroll) != "" } {
  152. uplevel \#0 $_widget($path,oldyscroll) $vmin $vmax
  153. }
  154. }
  155. # ------------------------------------------------------------------------------
  156. # Command ScrollView::_update_scroll
  157. # ------------------------------------------------------------------------------
  158. proc ScrollView::_update_scroll { path callscroll hminmax vminmax } {
  159. variable _widget
  160. set c [$path:cmd coords view]
  161. set hmin [lindex $hminmax 0]
  162. set hmax [lindex $hminmax 1]
  163. set vmin [lindex $vminmax 0]
  164. set vmax [lindex $vminmax 1]
  165. set x0 [expr {$hmin*$_widget($path,width)+$_widget($path,bd)}]
  166. set x1 [expr {$hmax*$_widget($path,width)+$_widget($path,bd)-1}]
  167. set y0 [expr {$vmin*$_widget($path,height)+$_widget($path,bd)}]
  168. set y1 [expr {$vmax*$_widget($path,height)+$_widget($path,bd)-1}]
  169. $path:cmd coords view $x0 $y0 $x1 $y1
  170. if { $callscroll } {
  171. if { $_widget($path,oldxscroll) != "" } {
  172. uplevel \#0 $_widget($path,oldxscroll) $hmin $hmax
  173. }
  174. if { $_widget($path,oldyscroll) != "" } {
  175. uplevel \#0 $_widget($path,oldyscroll) $vmin $vmax
  176. }
  177. }
  178. }
  179. # ------------------------------------------------------------------------------
  180. # Command ScrollView::_set_view
  181. # ------------------------------------------------------------------------------
  182. proc ScrollView::_set_view { path cmd x y } {
  183. variable _widget
  184. set w [Widget::getoption $path -window]
  185. if { [winfo exists $w] } {
  186. if { ![string compare $cmd "start"] } {
  187. set c [$path:cmd coords view]
  188. set x0 [lindex $c 0]
  189. set y0 [lindex $c 1]
  190. set _widget($path,dx) [expr {$x-$x0}]
  191. set _widget($path,dy) [expr {$y-$y0}]
  192. } else {
  193. if { ![string compare $cmd "motion"] } {
  194. set vh [expr {double($x-$_widget($path,dx)-$_widget($path,bd))/$_widget($path,width)}]
  195. set vv [expr {double($y-$_widget($path,dy)-$_widget($path,bd))/$_widget($path,height)}]
  196. } else {
  197. set vh [expr {double($x-$_widget($path,bd))/$_widget($path,width)}]
  198. set vv [expr {double($y-$_widget($path,bd))/$_widget($path,height)}]
  199. }
  200. $w xview moveto $vh
  201. $w yview moveto $vv
  202. _update_scroll $path 1 [$w xview] [$w yview]
  203. }
  204. }
  205. }
  206. # ------------------------------------------------------------------------------
  207. # Command ScrollView::_resize
  208. # ------------------------------------------------------------------------------
  209. proc ScrollView::_resize { path } {
  210. variable _widget
  211. set _widget($path,bd) [Widget::getoption $path -borderwidth]
  212. set _widget($path,width) [expr {[winfo width $path]-2*$_widget($path,bd)}]
  213. set _widget($path,height) [expr {[winfo height $path]-2*$_widget($path,bd)}]
  214. set w [Widget::getoption $path -window]
  215. if { [winfo exists $w] } {
  216. _update_scroll $path 0 [$w xview] [$w yview]
  217. }
  218. }