label.tcl 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. # ------------------------------------------------------------------------------
  2. # label.tcl
  3. # This file is part of Unifix BWidget Toolkit
  4. # $Id$
  5. # ------------------------------------------------------------------------------
  6. # Index of commands:
  7. # - Label::create
  8. # - Label::configure
  9. # - Label::cget
  10. # - Label::setfocus
  11. # - Label::_drag_cmd
  12. # - Label::_drop_cmd
  13. # - Label::_over_cmd
  14. # ------------------------------------------------------------------------------
  15. namespace eval Label {
  16. Widget::tkinclude Label label :cmd \
  17. remove {-foreground -text -textvariable -underline}
  18. Widget::declare Label {
  19. {-name String "" 0}
  20. {-text String "" 0}
  21. {-textvariable String "" 0}
  22. {-underline Int -1 0 {=-1}}
  23. {-focus String "" 0}
  24. {-foreground TkResource "" 0 label}
  25. {-disabledforeground TkResource "" 0 button}
  26. {-state Enum normal 0 {normal disabled}}
  27. {-fg Synonym -foreground}
  28. }
  29. DynamicHelp::include Label balloon
  30. DragSite::include Label "" 1
  31. DropSite::include Label {
  32. TEXT {move {}}
  33. IMAGE {move {}}
  34. BITMAP {move {}}
  35. FGCOLOR {move {}}
  36. BGCOLOR {move {}}
  37. COLOR {move {}}
  38. }
  39. Widget::syncoptions Label "" :cmd {-text {} -underline {}}
  40. proc ::Label { path args } { return [eval Label::create $path $args] }
  41. proc use {} {}
  42. bind BwLabel <FocusIn> {Label::setfocus %W}
  43. bind BwLabel <Destroy> {Widget::destroy %W; rename %W {}}
  44. }
  45. # ------------------------------------------------------------------------------
  46. # Command Label::create
  47. # ------------------------------------------------------------------------------
  48. proc Label::create { path args } {
  49. Widget::init Label $path $args
  50. if { [Widget::getoption $path -state] == "normal" } {
  51. set fg [Widget::getoption $path -foreground]
  52. } else {
  53. set fg [Widget::getoption $path -disabledforeground]
  54. }
  55. set var [Widget::getoption $path -textvariable]
  56. if { $var == "" &&
  57. [Widget::getoption $path -image] == "" &&
  58. [Widget::getoption $path -bitmap] == ""} {
  59. set desc [BWidget::getname [Widget::getoption $path -name]]
  60. if { $desc != "" } {
  61. set text [lindex $desc 0]
  62. set under [lindex $desc 1]
  63. } else {
  64. set text [Widget::getoption $path -text]
  65. set under [Widget::getoption $path -underline]
  66. }
  67. } else {
  68. set under -1
  69. set text ""
  70. }
  71. eval label $path [Widget::subcget $path :cmd] \
  72. [list -text $text -textvariable $var -underline $under -foreground $fg]
  73. set accel [string tolower [string index $text $under]]
  74. if { $accel != "" } {
  75. bind [winfo toplevel $path] <Alt-$accel> "Label::setfocus $path"
  76. }
  77. bindtags $path [list $path Label BwLabel [winfo toplevel $path] all]
  78. DragSite::setdrag $path $path Label::_init_drag_cmd [Widget::getoption $path -dragendcmd] 1
  79. DropSite::setdrop $path $path Label::_over_cmd Label::_drop_cmd 1
  80. DynamicHelp::sethelp $path $path 1
  81. rename $path ::$path:cmd
  82. proc ::$path { cmd args } "return \[eval Label::\$cmd $path \$args\]"
  83. return $path
  84. }
  85. # ------------------------------------------------------------------------------
  86. # Command Label::configure
  87. # ------------------------------------------------------------------------------
  88. proc Label::configure { path args } {
  89. set oldunder [$path:cmd cget -underline]
  90. if { $oldunder != -1 } {
  91. set oldaccel [string tolower [string index [$path:cmd cget -text] $oldunder]]
  92. } else {
  93. set oldaccel ""
  94. }
  95. set res [Widget::configure $path $args]
  96. set cfg [Widget::hasChanged $path -foreground fg]
  97. set cdfg [Widget::hasChanged $path -disabledforeground dfg]
  98. set cst [Widget::hasChanged $path -state state]
  99. if { $cst || $cfg || $cdfg } {
  100. if { $state == "normal" } {
  101. $path:cmd configure -fg $fg
  102. } else {
  103. $path:cmd configure -fg $dfg
  104. }
  105. }
  106. set cv [Widget::hasChanged $path -textvariable var]
  107. set cb [Widget::hasChanged $path -image img]
  108. set ci [Widget::hasChanged $path -bitmap bmp]
  109. set cn [Widget::hasChanged $path -name name]
  110. set ct [Widget::hasChanged $path -text text]
  111. set cu [Widget::hasChanged $path -underline under]
  112. if { $cv || $cb || $ci || $cn || $ct || $cu } {
  113. if { $var == "" && $img == "" && $bmp == "" } {
  114. set desc [BWidget::getname $name]
  115. if { $desc != "" } {
  116. set text [lindex $desc 0]
  117. set under [lindex $desc 1]
  118. }
  119. } else {
  120. set under -1
  121. set text ""
  122. }
  123. set top [winfo toplevel $path]
  124. if { $oldaccel != "" } {
  125. bind $top <Alt-$oldaccel> {}
  126. }
  127. set accel [string tolower [string index $text $under]]
  128. if { $accel != "" } {
  129. bind $top <Alt-$accel> "Label::setfocus $path"
  130. }
  131. $path:cmd configure -text $text -underline $under -textvariable $var
  132. }
  133. set force [Widget::hasChanged $path -dragendcmd dragend]
  134. DragSite::setdrag $path $path Label::_init_drag_cmd $dragend $force
  135. DropSite::setdrop $path $path Label::_over_cmd Label::_drop_cmd
  136. DynamicHelp::sethelp $path $path
  137. return $res
  138. }
  139. # ------------------------------------------------------------------------------
  140. # Command Label::cget
  141. # ------------------------------------------------------------------------------
  142. proc Label::cget { path option } {
  143. return [Widget::cget $path $option]
  144. }
  145. # ------------------------------------------------------------------------------
  146. # Command Label::setfocus
  147. # ------------------------------------------------------------------------------
  148. proc Label::setfocus { path } {
  149. if { ![string compare [Widget::getoption $path -state] "normal"] } {
  150. set w [Widget::getoption $path -focus]
  151. if { [winfo exists $w] && [Widget::focusOK $w] } {
  152. focus $w
  153. }
  154. }
  155. }
  156. # ------------------------------------------------------------------------------
  157. # Command Label::_init_drag_cmd
  158. # ------------------------------------------------------------------------------
  159. proc Label::_init_drag_cmd { path X Y top } {
  160. if { [set cmd [Widget::getoption $path -draginitcmd]] != "" } {
  161. return [uplevel \#0 $cmd [list $path $X $Y $top]]
  162. }
  163. if { [set data [$path:cmd cget -image]] != "" } {
  164. set type "IMAGE"
  165. pack [label $top.l -image $data]
  166. } elseif { [set data [$path:cmd cget -bitmap]] != "" } {
  167. set type "BITMAP"
  168. pack [label $top.l -bitmap $data]
  169. } else {
  170. set data [$path:cmd cget -text]
  171. set type "TEXT"
  172. }
  173. set usertype [Widget::getoption $path -dragtype]
  174. if { $usertype != "" } {
  175. set type $usertype
  176. }
  177. return [list $type {copy} $data]
  178. }
  179. # ------------------------------------------------------------------------------
  180. # Command Label::_drop_cmd
  181. # ------------------------------------------------------------------------------
  182. proc Label::_drop_cmd { path source X Y op type data } {
  183. if { [set cmd [Widget::getoption $path -dropcmd]] != "" } {
  184. return [uplevel \#0 $cmd [list $path $source $X $Y $op $type $data]]
  185. }
  186. if { $type == "COLOR" || $type == "FGCOLOR" } {
  187. configure $path -foreground $data
  188. } elseif { $type == "BGCOLOR" } {
  189. configure $path -background $data
  190. } else {
  191. set text ""
  192. set image ""
  193. set bitmap ""
  194. switch -- $type {
  195. IMAGE {set image $data}
  196. BITMAP {set bitmap $data}
  197. default {
  198. set text $data
  199. if { [set var [$path:cmd cget -textvariable]] != "" } {
  200. configure $path -image "" -bitmap ""
  201. GlobalVar::setvar $var $data
  202. return
  203. }
  204. }
  205. }
  206. configure $path -text $text -image $image -bitmap $bitmap
  207. }
  208. return 1
  209. }
  210. # ------------------------------------------------------------------------------
  211. # Command Label::_over_cmd
  212. # ------------------------------------------------------------------------------
  213. proc Label::_over_cmd { path source event X Y op type data } {
  214. if { [set cmd [Widget::getoption $path -dropovercmd]] != "" } {
  215. return [uplevel \#0 $cmd [list $path $source $event $X $Y $op $type $data]]
  216. }
  217. if { [Widget::getoption $path -state] == "normal" ||
  218. $type == "COLOR" || $type == "FGCOLOR" || $type == "BGCOLOR" } {
  219. DropSite::setcursor based_arrow_down
  220. return 1
  221. }
  222. DropSite::setcursor dot
  223. return 0
  224. }