progressbar.tcl 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. # ------------------------------------------------------------------------------
  2. # progressbar.tcl
  3. # This file is part of Unifix BWidget Toolkit
  4. # ------------------------------------------------------------------------------
  5. # Index of commands:
  6. # - ProgressBar::create
  7. # - ProgressBar::configure
  8. # - ProgressBar::cget
  9. # - ProgressBar::_destroy
  10. # - ProgressBar::_modify
  11. # ------------------------------------------------------------------------------
  12. namespace eval ProgressBar {
  13. Widget::declare ProgressBar {
  14. {-type Enum normal 0 {normal incremental infinite}}
  15. {-maximum Int 100 0 {>0}}
  16. {-background TkResource "" 0 frame}
  17. {-foreground TkResource blue 0 label}
  18. {-borderwidth TkResource 2 0 frame}
  19. {-troughcolor TkResource "" 0 scrollbar}
  20. {-relief TkResource sunken 0 label}
  21. {-orient Enum horizontal 1 {horizontal vertical}}
  22. {-variable String "" 0}
  23. {-width TkResource 100 0 frame}
  24. {-height TkResource 4m 0 frame}
  25. {-bg Synonym -background}
  26. {-fg Synonym -foreground}
  27. {-bd Synonym -borderwidth}
  28. }
  29. Widget::addmap ProgressBar "" :cmd {-background {} -width {} -height {}}
  30. Widget::addmap ProgressBar "" .bar {-troughcolor -background -borderwidth {} -relief {}}
  31. variable _widget
  32. proc ::ProgressBar { path args } { return [eval ProgressBar::create $path $args] }
  33. proc use {} {}
  34. }
  35. # ------------------------------------------------------------------------------
  36. # Command ProgressBar::create
  37. # ------------------------------------------------------------------------------
  38. proc ProgressBar::create { path args } {
  39. variable _widget
  40. Widget::init ProgressBar $path $args
  41. eval frame $path [Widget::subcget $path :cmd]
  42. set c [eval canvas $path.bar [Widget::subcget $path .bar] -highlightthickness 0]
  43. set fg [Widget::getoption $path -foreground]
  44. if { ![string compare [Widget::getoption $path -orient] "horizontal"] } {
  45. $path.bar create rectangle -1 0 0 0 -fill $fg -outline $fg -tags rect
  46. } else {
  47. $path.bar create rectangle 0 1 0 0 -fill $fg -outline $fg -tags rect
  48. }
  49. set _widget($path,val) 0
  50. set _widget($path,dir) 1
  51. if { [set _widget($path,var) [Widget::getoption $path -variable]] != "" } {
  52. GlobalVar::tracevar variable $_widget($path,var) w "ProgressBar::_modify $path"
  53. after idle ProgressBar::_modify $path
  54. }
  55. bind $path.bar <Destroy> "ProgressBar::_destroy $path"
  56. bind $path.bar <Configure> "ProgressBar::_modify $path"
  57. rename $path ::$path:cmd
  58. proc ::$path { cmd args } "return \[eval ProgressBar::\$cmd $path \$args\]"
  59. return $path
  60. }
  61. # ------------------------------------------------------------------------------
  62. # Command ProgressBar::configure
  63. # ------------------------------------------------------------------------------
  64. proc ProgressBar::configure { path args } {
  65. variable _widget
  66. set res [Widget::configure $path $args]
  67. if { [Widget::hasChanged $path -variable newv] } {
  68. if { $_widget($path,var) != "" } {
  69. GlobalVar::tracevar vdelete $_widget($path,var) w "ProgressBar::_modify $path"
  70. }
  71. if { $newv != "" } {
  72. set _widget($path,var) $newv
  73. GlobalVar::tracevar variable $newv w "ProgressBar::_modify $path"
  74. after idle ProgressBar::_modify $path
  75. } else {
  76. set _widget($path,var) ""
  77. }
  78. }
  79. if { [Widget::hasChanged $path -borderwidth v] ||
  80. [Widget::hasChanged $path -orient v] } {
  81. after idle ProgressBar::_modify $path
  82. }
  83. if { [Widget::hasChanged $path -foreground fg] } {
  84. $path.bar itemconfigure rect -fill $fg -outline $fg
  85. }
  86. return $res
  87. }
  88. # ------------------------------------------------------------------------------
  89. # Command ProgressBar::cget
  90. # ------------------------------------------------------------------------------
  91. proc ProgressBar::cget { path option } {
  92. return [Widget::cget $path $option]
  93. }
  94. # ------------------------------------------------------------------------------
  95. # Command ProgressBar::_destroy
  96. # ------------------------------------------------------------------------------
  97. proc ProgressBar::_destroy { path } {
  98. variable _widget
  99. if { $_widget($path,var) != "" } {
  100. GlobalVar::tracevar vdelete $_widget($path,var) w "ProgressBar::_modify $path"
  101. }
  102. unset _widget($path,var)
  103. unset _widget($path,dir)
  104. Widget::destroy $path
  105. rename $path {}
  106. }
  107. # ------------------------------------------------------------------------------
  108. # Command ProgressBar::_modify
  109. # ------------------------------------------------------------------------------
  110. proc ProgressBar::_modify { path args } {
  111. variable _widget
  112. if { ![GlobalVar::exists $_widget($path,var)] ||
  113. [set val [GlobalVar::getvar $_widget($path,var)]] < 0 } {
  114. catch {place forget $path.bar}
  115. } else {
  116. place $path.bar -relx 0 -rely 0 -relwidth 1 -relheight 1
  117. set type [Widget::getoption $path -type]
  118. if { $val != 0 && [string compare $type "normal"] } {
  119. set val [expr {$val+$_widget($path,val)}]
  120. }
  121. set _widget($path,val) $val
  122. set max [Widget::getoption $path -maximum]
  123. set bd [expr {2*[$path.bar cget -bd]}]
  124. set w [winfo width $path.bar]
  125. set h [winfo height $path.bar]
  126. if { ![string compare $type "infinite"] } {
  127. if { $val > $max } {
  128. set _widget($path,dir) [expr {-$_widget($path,dir)}]
  129. set val 0
  130. set _widget($path,val) 0
  131. }
  132. if { $val <= $max/2.0 } {
  133. set dx0 0.0
  134. set dx1 [expr {double($val)/$max}]
  135. } else {
  136. set dx1 [expr {double($val)/$max}]
  137. set dx0 [expr {$dx1-0.5}]
  138. }
  139. if { $_widget($path,dir) == 1 } {
  140. set x0 $dx0
  141. set x1 $dx1
  142. } else {
  143. set x0 [expr {1-$dx1}]
  144. set x1 [expr {1-$dx0}]
  145. }
  146. if { ![string compare [Widget::getoption $path -orient] "horizontal"] } {
  147. $path.bar coords rect [expr {$x0*$w}] 0 [expr {$x1*$w}] $h
  148. } else {
  149. $path.bar coords rect 0 [expr {$h-$x0*$h}] $w [expr {$x1*$h}]
  150. }
  151. } else {
  152. if { $val > $max } {set val $max}
  153. if { ![string compare [Widget::getoption $path -orient] "horizontal"] } {
  154. $path.bar coords rect -1 0 [expr {$val*$w/$max}] $h
  155. } else {
  156. $path.bar coords rect 0 [expr {$h+1}] $w [expr {$h*($max-$val)}]
  157. }
  158. }
  159. }
  160. update
  161. }