Variable.tcl 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. # -*- mode: TCL; fill-column: 75; tab-width: 8; coding: iso-latin-1-unix -*-
  2. #
  3. # $Id: Variable.tcl,v 1.4 2001/12/09 05:04:02 idiscovery Exp $
  4. #
  5. # Variable.tcl --
  6. #
  7. # Routines in this file are used to set up and operate variables
  8. # for classes that support the -variable option
  9. #
  10. # Copyright (c) 1993-1999 Ioi Kim Lam.
  11. # Copyright (c) 2000-2001 Tix Project Group.
  12. #
  13. # See the file "license.terms" for information on usage and redistribution
  14. # of this file, and for a DISCLAIMER OF ALL WARRANTIES.
  15. #
  16. # tixVariable:ConfigVariable --
  17. #
  18. # Set up the -variable option for the object $w
  19. #
  20. # Side effects:
  21. #
  22. # data(-variable) is changed to the name of the global variable
  23. # if the global variable exists, data(-value) takes the value of this
  24. # variable.
  25. # if the global variable does not exist, it is created with the
  26. # current data(-value)
  27. #
  28. # Return value:
  29. #
  30. # true is data(-value) is changed, indicating that data(-command)
  31. # should be invoked.
  32. #
  33. proc tixVariable:ConfigVariable {w arg} {
  34. upvar #0 $w data
  35. set changed 0
  36. if {$data(-variable) != ""} {
  37. uplevel #0 \
  38. [list trace vdelete $data(-variable) w "tixVariable:TraceProc $w"]
  39. }
  40. if {$arg != ""} {
  41. if {[uplevel #0 info exists [list $arg]]} {
  42. # This global variable exists, we use its value
  43. #
  44. set data(-value) [uplevel #0 set [list $arg]]
  45. set changed 1
  46. } else {
  47. # This global variable does not exist; let's set it
  48. #
  49. uplevel #0 [list set $arg $data(-value)]
  50. }
  51. uplevel #0 \
  52. [list trace variable $arg w "tixVariable:TraceProc $w"]
  53. }
  54. return $changed
  55. }
  56. proc tixVariable:UpdateVariable {w} {
  57. upvar #0 $w data
  58. if {$data(-variable) != ""} {
  59. uplevel #0 \
  60. [list trace vdelete $data(-variable) w "tixVariable:TraceProc $w"]
  61. uplevel #0 \
  62. [list set $data(-variable) $data(-value)]
  63. uplevel #0 \
  64. [list trace variable $data(-variable) w "tixVariable:TraceProc $w"]
  65. # just in case someone has another trace and restricted my change
  66. #
  67. set data(-value) [uplevel #0 set [list $data(-variable)]]
  68. }
  69. }
  70. proc tixVariable:TraceProc {w name1 name2 op} {
  71. upvar #0 $w data
  72. set varname $data(-variable)
  73. if {[catch {$w config -value [uplevel #0 [list set $varname]]} err]} {
  74. uplevel #0 [list set $varname [list [$w cget -value]]]
  75. error $err
  76. }
  77. return
  78. }
  79. proc tixVariable:DeleteVariable {w} {
  80. upvar #0 $w data
  81. # Must delete the trace command of the -variable
  82. #
  83. if {$data(-variable) != ""} {
  84. uplevel #0 \
  85. [list trace vdelete $data(-variable) w "tixVariable:TraceProc $w"]
  86. }
  87. }