ArrowBtn.tcl 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. # -*-mode: tcl; fill-column: 75; tab-width: 8; coding: iso-latin-1-unix -*-
  2. #
  3. # $Id: ArrowBtn.tcl,v 1.3 2001/12/09 05:31:07 idiscovery Exp $
  4. #
  5. # Tix Demostration Program
  6. #
  7. # This sample program is structured in such a way so that it can be
  8. # executed from the Tix demo program "widget": it must have a
  9. # procedure called "RunSample". It should also have the "if" statment
  10. # at the end of this file so that it can be run as a standalone
  11. # program using tixwish.
  12. # This file demonstrates how to write a new Tix widget class.
  13. #
  14. # ArrowBtn.tcl --
  15. #
  16. # Arrow Button: a sample Tix widget.
  17. #
  18. set arrow(n) [image create bitmap -data {
  19. #define up_width 15
  20. #define up_height 15
  21. static unsigned char up_bits[] = {
  22. 0x80, 0x00, 0xc0, 0x01, 0xe0, 0x03, 0xf0, 0x07, 0xf8, 0x0f, 0xfc, 0x1f,
  23. 0xfe, 0x3f, 0xc0, 0x01, 0xc0, 0x01, 0xc0, 0x01, 0xc0, 0x01, 0xc0, 0x01,
  24. 0xc0, 0x01, 0xc0, 0x01, 0x00, 0x00};
  25. }]
  26. set arrow(w) [image create bitmap -data {
  27. #define left_width 15
  28. #define left_height 15
  29. static unsigned char left_bits[] = {
  30. 0x00, 0x00, 0x40, 0x00, 0x60, 0x00, 0x70, 0x00, 0x78, 0x00, 0x7c, 0x00,
  31. 0xfe, 0x3f, 0xff, 0x3f, 0xfe, 0x3f, 0x7c, 0x00, 0x78, 0x00, 0x70, 0x00,
  32. 0x60, 0x00, 0x40, 0x00, 0x00, 0x00};
  33. }]
  34. set arrow(s) [image create bitmap -data {
  35. #define down_width 15
  36. #define down_height 15
  37. static unsigned char down_bits[] = {
  38. 0x00, 0x00, 0xc0, 0x01, 0xc0, 0x01, 0xc0, 0x01, 0xc0, 0x01, 0xc0, 0x01,
  39. 0xc0, 0x01, 0xc0, 0x01, 0xfe, 0x3f, 0xfc, 0x1f, 0xf8, 0x0f, 0xf0, 0x07,
  40. 0xe0, 0x03, 0xc0, 0x01, 0x80, 0x00};
  41. }]
  42. set arrow(e) [image create bitmap -data {
  43. #define right_width 15
  44. #define right_height 15
  45. static unsigned char right_bits[] = {
  46. 0x00, 0x00, 0x00, 0x01, 0x00, 0x03, 0x00, 0x07, 0x00, 0x0f, 0x00, 0x1f,
  47. 0xfe, 0x3f, 0xfe, 0x7f, 0xfe, 0x3f, 0x00, 0x1f, 0x00, 0x0f, 0x00, 0x07,
  48. 0x00, 0x03, 0x00, 0x01, 0x00, 0x00};
  49. }]
  50. tixWidgetClass tixArrowButton {
  51. -classname TixArrowButton
  52. -superclass tixPrimitive
  53. -method {
  54. flash invoke invert
  55. }
  56. -flag {
  57. -direction -state
  58. }
  59. -configspec {
  60. {-direction direction Direction e tixArrowButton:CheckDirection}
  61. {-state state State normal}
  62. }
  63. -alias {
  64. {-dir -direction}
  65. }
  66. }
  67. proc tixArrowButton:InitWidgetRec {w} {
  68. upvar #0 $w data
  69. tixChainMethod $w InitWidgetRec
  70. set data(count) 0
  71. }
  72. proc tixArrowButton:ConstructWidget {w} {
  73. upvar #0 $w data
  74. global arrow
  75. tixChainMethod $w ConstructWidget
  76. set data(w:button) [button $w.button -image $arrow($data(-direction))]
  77. pack $data(w:button) -expand yes -fill both
  78. }
  79. proc tixArrowButton:SetBindings {w} {
  80. upvar #0 $w data
  81. tixChainMethod $w SetBindings
  82. bind $data(w:button) <1> "tixArrowButton:IncrCount $w"
  83. }
  84. proc tixArrowButton:IncrCount {w} {
  85. upvar #0 $w data
  86. incr data(count)
  87. }
  88. proc tixArrowButton:CheckDirection {dir} {
  89. if {[lsearch {n w s e} $dir] != -1} {
  90. return $dir
  91. } else {
  92. error "wrong direction value \"$dir\""
  93. }
  94. }
  95. proc tixArrowButton:flash {w} {
  96. upvar #0 $w data
  97. $data(w:button) flash
  98. }
  99. proc tixArrowButton:invoke {w} {
  100. upvar #0 $w data
  101. $data(w:button) invoke
  102. }
  103. proc tixArrowButton:invert {w} {
  104. upvar #0 $w data
  105. set curDirection $data(-direction)
  106. case $curDirection {
  107. n {
  108. set newDirection s
  109. }
  110. s {
  111. set newDirection n
  112. }
  113. e {
  114. set newDirection w
  115. }
  116. w {
  117. set newDirection e
  118. }
  119. }
  120. $w config -direction $newDirection
  121. }
  122. proc tixArrowButton:config-direction {w value} {
  123. upvar #0 $w data
  124. global arrow
  125. $data(w:button) configure -image $arrow($value)
  126. }
  127. proc tixArrowButton:config-state {w value} {
  128. upvar #0 $w data
  129. global arrow
  130. $data(w:button) configure -state $value
  131. }
  132. #----------------------------------------------------------------------
  133. #
  134. # Instantiate several widgets of the tixArrowButton class
  135. #
  136. #----------------------------------------------------------------------
  137. proc RunSample {w} {
  138. set top [frame $w.top -border 1 -relief raised]
  139. tixArrowButton $top.a -dir w
  140. tixArrowButton $top.b -dir e
  141. pack $top.a $top.b -side left -expand yes -fill both -padx 50 -pady 10
  142. tixButtonBox $w.box -orientation horizontal
  143. $w.box add ok -text Ok -underline 0 -command "destroy $w" \
  144. -width 6
  145. pack $w.box -side bottom -fill x
  146. pack $w.top -side top -fill both -expand yes
  147. }
  148. # This "if" statement makes it possible to run this script file inside or
  149. # outside of the main demo program "widget".
  150. #
  151. if {![info exists tix_demo_running]} {
  152. wm withdraw .
  153. set w .demo
  154. toplevel $w; wm transient $w ""
  155. RunSample $w
  156. bind $w <Destroy> "exit"
  157. }