labelframe.tcl 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. # ------------------------------------------------------------------------------
  2. # labelframe.tcl
  3. # This file is part of Unifix BWidget Toolkit
  4. # $Id$
  5. # ------------------------------------------------------------------------------
  6. # Index of commands:
  7. # - LabelFrame::create
  8. # - LabelFrame::getframe
  9. # - LabelFrame::configure
  10. # - LabelFrame::cget
  11. # - LabelFrame::align
  12. # ------------------------------------------------------------------------------
  13. namespace eval LabelFrame {
  14. Label::use
  15. Widget::bwinclude LabelFrame Label .l \
  16. remove {
  17. -highlightthickness -highlightcolor -highlightbackground
  18. -takefocus -relief -borderwidth
  19. -bitmap -image -cursor -textvariable
  20. -dragenabled -draginitcmd -dragendcmd -dragevent -dragtype
  21. -dropenabled -droptypes -dropovercmd -dropcmd} \
  22. initialize {-anchor w}
  23. Widget::declare LabelFrame {
  24. {-relief TkResource flat 0 frame}
  25. {-borderwidth TkResource 0 0 frame}
  26. {-side Enum left 1 {left right top bottom}}
  27. {-bd Synonym -borderwidth}
  28. }
  29. Widget::addmap LabelFrame "" :cmd {-background {}}
  30. Widget::addmap LabelFrame "" .f {-background {} -relief {} -borderwidth {}}
  31. Widget::syncoptions LabelFrame Label .l {-text {} -underline {}}
  32. bind BwLabelFrame <FocusIn> {Label::setfocus %W.l}
  33. bind BwLabelFrame <Destroy> {Widget::destroy %W; rename %W {}}
  34. proc ::LabelFrame { path args } { return [eval LabelFrame::create $path $args] }
  35. proc use {} {}
  36. }
  37. # ------------------------------------------------------------------------------
  38. # Command LabelFrame::create
  39. # ------------------------------------------------------------------------------
  40. proc LabelFrame::create { path args } {
  41. Widget::init LabelFrame $path $args
  42. set path [frame $path -background [Widget::getoption $path -background] \
  43. -relief flat -bd 0 -takefocus 0 -highlightthickness 0]
  44. set label [eval Label::create $path.l [Widget::subcget $path .l] \
  45. -takefocus 0 -highlightthickness 0 -relief flat -borderwidth 0 \
  46. -dropenabled 0 -dragenabled 0]
  47. set frame [eval frame $path.f [Widget::subcget $path .f] \
  48. -highlightthickness 0 -takefocus 0]
  49. switch [Widget::getoption $path -side] {
  50. left {set packopt "-side left"}
  51. right {set packopt "-side right"}
  52. top {set packopt "-side top -fill x"}
  53. bottom {set packopt "-side bottom -fill x"}
  54. }
  55. eval pack $label $packopt
  56. pack $frame -fill both -expand yes
  57. bindtags $path [list $path BwLabelFrame [winfo toplevel $path] all]
  58. rename $path ::$path:cmd
  59. proc ::$path { cmd args } "return \[eval LabelFrame::\$cmd $path \$args\]"
  60. return $path
  61. }
  62. # ------------------------------------------------------------------------------
  63. # Command LabelFrame::getframe
  64. # ------------------------------------------------------------------------------
  65. proc LabelFrame::getframe { path } {
  66. return $path.f
  67. }
  68. # ------------------------------------------------------------------------------
  69. # Command LabelFrame::configure
  70. # ------------------------------------------------------------------------------
  71. proc LabelFrame::configure { path args } {
  72. return [Widget::configure $path $args]
  73. }
  74. # ------------------------------------------------------------------------------
  75. # Command LabelFrame::cget
  76. # ------------------------------------------------------------------------------
  77. proc LabelFrame::cget { path option } {
  78. return [Widget::cget $path $option]
  79. }
  80. # ------------------------------------------------------------------------------
  81. # Command LabelFrame::align
  82. # This command align label of all widget given by args of class LabelFrame
  83. # (or "derived") by setting their width to the max one +1
  84. # ------------------------------------------------------------------------------
  85. proc LabelFrame::align { args } {
  86. set maxlen 0
  87. set wlist {}
  88. foreach wl $args {
  89. foreach w $wl {
  90. if { ![info exists Widget::_class($w)] } {
  91. continue
  92. }
  93. set class $Widget::_class($w)
  94. if { ![string compare $class "LabelFrame"] } {
  95. set textopt -text
  96. set widthopt -width
  97. } else {
  98. upvar 0 Widget::${class}::map classmap
  99. set textopt ""
  100. set widthopt ""
  101. set notdone 2
  102. foreach {option lmap} [array get classmap] {
  103. foreach {subpath subclass realopt} $lmap {
  104. if { ![string compare $subclass "LabelFrame"] } {
  105. if { ![string compare $realopt "-text"] } {
  106. set textopt $option
  107. incr notdone -1
  108. break
  109. }
  110. if { ![string compare $realopt "-width"] } {
  111. set widthopt $option
  112. incr notdone -1
  113. break
  114. }
  115. }
  116. }
  117. if { !$notdone } {
  118. break
  119. }
  120. }
  121. if { $notdone } {
  122. continue
  123. }
  124. }
  125. set len [string length [$w cget $textopt]]
  126. if { $len > $maxlen } {
  127. set maxlen $len
  128. }
  129. lappend wlist $w $widthopt
  130. }
  131. }
  132. incr maxlen
  133. foreach {w widthopt} $wlist {
  134. $w configure $widthopt $maxlen
  135. }
  136. }