Meter.tcl 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. # -*- mode: TCL; fill-column: 75; tab-width: 8; coding: iso-latin-1-unix -*-
  2. #
  3. # $Id: Meter.tcl,v 1.3 2001/12/09 05:04:02 idiscovery Exp $
  4. #
  5. # Meter.tcl --
  6. #
  7. # Implements the tixMeter widget
  8. #
  9. # Copyright (c) 1993-1999 Ioi Kim Lam.
  10. # Copyright (c) 2000-2001 Tix Project Group.
  11. #
  12. # See the file "license.terms" for information on usage and redistribution
  13. # of this file, and for a DISCLAIMER OF ALL WARRANTIES.
  14. #
  15. tixWidgetClass tixMeter {
  16. -classname TixMeter
  17. -superclass tixPrimitive
  18. -method {
  19. }
  20. -flag {
  21. -foreground -text -value
  22. }
  23. -configspec {
  24. {-fillcolor fillColor FillColor #8080ff}
  25. {-foreground foreground Foreground black}
  26. {-text text Text ""}
  27. {-value value Value 0}
  28. }
  29. -default {
  30. {.relief sunken}
  31. {.borderWidth 2}
  32. {.width 150}
  33. }
  34. }
  35. proc tixMeter:InitWidgetRec {w} {
  36. upvar #0 $w data
  37. global env
  38. tixChainMethod $w InitWidgetRec
  39. }
  40. #----------------------------------------------------------------------
  41. # Construct widget
  42. #----------------------------------------------------------------------
  43. proc tixMeter:ConstructWidget {w} {
  44. upvar #0 $w data
  45. tixChainMethod $w ConstructWidget
  46. set data(w:canvas) [canvas $w.canvas]
  47. pack $data(w:canvas) -expand yes -fill both
  48. tixMeter:Update $w
  49. }
  50. proc tixMeter:SetBindings {w} {
  51. upvar #0 $w data
  52. tixChainMethod $w SetBindings
  53. }
  54. proc tixMeter:Update {w} {
  55. upvar #0 $w data
  56. # set the width of the canvas
  57. set W [expr $data(-width)-\
  58. ([$data(w:root) cget -bd]+[$data(w:root) cget -highlightthickness]*2)]
  59. $data(w:canvas) config -width $W
  60. if {$data(-text) == ""} {
  61. set text [format "%d%%" [expr int($data(-value)*100)]]
  62. } else {
  63. set text $data(-text)
  64. }
  65. # (Create/Modify) the text item.
  66. #
  67. if {![info exists data(text)]} {
  68. set data(text) [$data(w:canvas) create text 0 0 -text $text]
  69. } else {
  70. $data(w:canvas) itemconfig $data(text) -text $text
  71. }
  72. set bbox [$data(w:canvas) bbox $data(text)]
  73. set itemW [expr [lindex $bbox 2]-[lindex $bbox 0]]
  74. set itemH [expr [lindex $bbox 3]-[lindex $bbox 1]]
  75. $data(w:canvas) coord $data(text) [expr $W/2] [expr $itemH/2+4]
  76. set H [expr $itemH + 4]
  77. $data(w:canvas) config -height [expr $H]
  78. set rectW [expr int($W*$data(-value))]
  79. if {![info exists data(rect)]} {
  80. set data(rect) [$data(w:canvas) create rectangle 0 0 $rectW 1000]
  81. } else {
  82. $data(w:canvas) coord $data(rect) 0 0 $rectW 1000
  83. }
  84. $data(w:canvas) itemconfig $data(rect) \
  85. -fill $data(-fillcolor) -outline $data(-fillcolor)
  86. $data(w:canvas) raise $data(text)
  87. }
  88. #----------------------------------------------------------------------
  89. # Configuration
  90. #----------------------------------------------------------------------
  91. proc tixMeter:config-value {w value} {
  92. upvar #0 $w data
  93. set data(-value) $value
  94. tixMeter:Update $w
  95. }
  96. proc tixMeter:config-text {w value} {
  97. upvar #0 $w data
  98. set data(-text) $value
  99. tixMeter:Update $w
  100. }
  101. proc tixMeter:config-fillcolor {w value} {
  102. upvar #0 $w data
  103. set data(-fillcolor) $value
  104. tixMeter:Update $w
  105. }