titleframe.tcl 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. # ------------------------------------------------------------------------------
  2. # titleframe.tcl
  3. # This file is part of Unifix BWidget Toolkit
  4. # ------------------------------------------------------------------------------
  5. # Index of commands:
  6. # - TitleFrame::create
  7. # - TitleFrame::configure
  8. # - TitleFrame::cget
  9. # - TitleFrame::getframe
  10. # - TitleFrame::_place
  11. # ------------------------------------------------------------------------------
  12. namespace eval TitleFrame {
  13. Widget::declare TitleFrame {
  14. {-relief TkResource groove 0 frame}
  15. {-borderwidth TkResource 2 0 frame}
  16. {-font TkResource "" 0 label}
  17. {-foreground TkResource "" 0 label}
  18. {-background TkResource "" 0 frame}
  19. {-text String "" 0}
  20. {-ipad Int 4 0 {=0 ""}}
  21. {-side Enum left 0 {left center right}}
  22. {-baseline Enum center 0 {top center bottom}}
  23. {-fg Synonym -foreground}
  24. {-bg Synonym -background}
  25. {-bd Synonym -borderwidth}
  26. }
  27. Widget::addmap TitleFrame "" :cmd {-background {}}
  28. Widget::addmap TitleFrame "" .l {-background {} -foreground {} -text {} -font {}}
  29. Widget::addmap TitleFrame "" .p {-background {}}
  30. Widget::addmap TitleFrame "" .b {-background {} -relief {} -borderwidth {}}
  31. Widget::addmap TitleFrame "" .b.p {-background {}}
  32. Widget::addmap TitleFrame "" .f {-background {}}
  33. proc ::TitleFrame { path args } { return [eval TitleFrame::create $path $args] }
  34. proc use {} {}
  35. }
  36. # ------------------------------------------------------------------------------
  37. # Command TitleFrame::create
  38. # ------------------------------------------------------------------------------
  39. proc TitleFrame::create { path args } {
  40. Widget::init TitleFrame $path $args
  41. set bg [Widget::getoption $path -background]
  42. set frame [frame $path -background $bg]
  43. set padtop [frame $path.p -relief flat -borderwidth 0 -background $bg]
  44. set border [eval frame $path.b [Widget::subcget $path .b] -highlightthickness 0]
  45. set label [eval label $path.l [Widget::subcget $path .l] \
  46. -highlightthickness 0 \
  47. -relief flat \
  48. -bd 0 -padx 2 -pady 0]
  49. set padbot [frame $border.p -relief flat -bd 0 -bg $bg -highlightthickness 0]
  50. set frame [frame $path.f -relief flat -bd 0 -bg $bg -highlightthickness 0]
  51. set height [winfo reqheight $label]
  52. switch [Widget::getoption $path -side] {
  53. left { set relx 0.0; set x 5; set anchor nw }
  54. center { set relx 0.5; set x 0; set anchor n }
  55. right { set relx 1.0; set x -5; set anchor ne }
  56. }
  57. set bd [Widget::getoption $path -borderwidth]
  58. switch [Widget::getoption $path -baseline] {
  59. top { set htop $height; set hbot 1; set y 0 }
  60. center { set htop [expr {$height/2}]; set hbot [expr {$height/2+$height%2+1}]; set y 0 }
  61. bottom { set htop 1; set hbot $height; set y [expr {$bd+1}] }
  62. }
  63. $padtop configure -height $htop
  64. $padbot configure -height $hbot
  65. set pad [Widget::getoption $path -ipad]
  66. pack $padbot -side top -fill x
  67. pack $frame -in $border -fill both -expand yes -padx $pad -pady $pad
  68. pack $padtop -side top -fill x
  69. pack $border -fill both -expand yes
  70. place $label -relx $relx -x $x -anchor $anchor -y $y
  71. bind $label <Configure> "TitleFrame::_place $path"
  72. bind $path <Destroy> {Widget::destroy %W; rename %W {}}
  73. rename $path ::$path:cmd
  74. proc ::$path { cmd args } "return \[eval TitleFrame::\$cmd $path \$args\]"
  75. return $path
  76. }
  77. # ------------------------------------------------------------------------------
  78. # Command TitleFrame::configure
  79. # ------------------------------------------------------------------------------
  80. proc TitleFrame::configure { path args } {
  81. set res [Widget::configure $path $args]
  82. if { [Widget::hasChanged $path -ipad pad] } {
  83. pack configure $path.f -padx $pad -pady $pad
  84. }
  85. if { [Widget::hasChanged $path -borderwidth val] |
  86. [Widget::hasChanged $path -font val] |
  87. [Widget::hasChanged $path -side val] |
  88. [Widget::hasChanged $path -baseline val] } {
  89. _place $path
  90. }
  91. return $res
  92. }
  93. # ------------------------------------------------------------------------------
  94. # Command TitleFrame::cget
  95. # ------------------------------------------------------------------------------
  96. proc TitleFrame::cget { path option } {
  97. return [Widget::cget $path $option]
  98. }
  99. # ------------------------------------------------------------------------------
  100. # Command TitleFrame::getframe
  101. # ------------------------------------------------------------------------------
  102. proc TitleFrame::getframe { path } {
  103. return $path.f
  104. }
  105. # ------------------------------------------------------------------------------
  106. # Command TitleFrame::_place
  107. # ------------------------------------------------------------------------------
  108. proc TitleFrame::_place { path } {
  109. set height [winfo height $path.l]
  110. switch [Widget::getoption $path -side] {
  111. left { set relx 0.0; set x 10; set anchor nw }
  112. center { set relx 0.5; set x 0; set anchor n }
  113. right { set relx 1.0; set x -10; set anchor ne }
  114. }
  115. set bd [Widget::getoption $path -borderwidth]
  116. switch [Widget::getoption $path -baseline] {
  117. top { set htop $height; set hbot 1; set y 0 }
  118. center { set htop [expr {$height/2}]; set hbot [expr {$height/2+$height%2+1}]; set y 0 }
  119. bottom { set htop 1; set hbot $height; set y [expr {$bd+1}] }
  120. }
  121. $path.p configure -height $htop
  122. $path.b.p configure -height $hbot
  123. place $path.l -relx $relx -x $x -anchor $anchor -y $y
  124. }