1# 2# $Id: toolbutton.tcl 31689 2011-05-22 09:26:02Z nobu $ 3# 4# Demonstration of custom widget styles. 5# 6 7# 8# ~ BACKGROUND 9# 10# Checkbuttons in toolbars have a very different appearance 11# than regular checkbuttons: there's no indicator, they 12# "pop up" when the mouse is over them, and they appear sunken 13# when selected. 14# 15# Tk added partial support for toolbar-style buttons in 8.4 16# with the "-overrelief" option, and TIP #82 added further 17# support with the "-offrelief" option. So to get a toolbar-style 18# checkbutton, you can configure it with: 19# 20# checkbutton .cb \ 21# -indicatoron false -selectcolor {} -relief flat -overrelief raised 22# 23# Behind the scenes, Tk has a lot of rather complicated logic 24# to implement this checkbutton style; see library/button.tcl, 25# generic/tkButton.c, and the platform-specific files unix/tkUnixButton.c 26# et al. for the full details. 27# 28# The tile widget set has a better way: custom styles. 29# Since the appearance is completely controlled by the theme engine, 30# we can define a new "Toolbutton" style and just use: 31# 32# checkbutton .cb -style Toolbutton 33# 34# 35# ~ DEMONSTRATION 36# 37# The tile built-in themes (default, "alt", windows, and XP) 38# already include Toolbutton styles. This script will add 39# them to the "step" and "blue" themes as a demonstration. 40# 41# (Note: Pushbuttons and radiobuttons can also use the "Toolbutton" 42# style; see demo.tcl.) 43# 44 45style theme settings "step" { 46 47# 48# First, we use [style layout] to define what elements to 49# use and how they're arranged. Toolbuttons are pretty 50# simple, consisting of a border, some internal padding, 51# and a label. (See also the TScrollbar layout definition 52# in demos/blue.tcl for a more complicated layout spec.) 53# 54 style layout Toolbutton { 55 Toolbutton.background 56 Toolbutton.border -children { 57 Toolbutton.padding -children { 58 Toolbutton.label 59 } 60 } 61 } 62 63# (Actually the above isn't strictly necessary, since the same layout 64# is defined in the default theme; we could have inherited it 65# instead.) 66# 67# Next, specify default values for element options. 68# For many options (like -background), the defaults 69# inherited from the parent style are sufficient. 70# 71 style default Toolbutton -width 0 -padding 1 -relief flat -borderwidth 2 72 73# 74# Finally, use [style map] to specify state-specific 75# resource values. We want a flat relief if the widget is 76# disabled, sunken if it's selected (on) or pressed, 77# and raised when it's active (the mouse pointer is 78# over the widget). Each state-value pair is checked 79# in order, and the first matching state takes precedence. 80# 81 style map Toolbutton -relief { 82 disabled flat 83 selected sunken 84 pressed sunken 85 active raised 86 } 87} 88 89# 90# Now for the "blue" theme. (Since the purpose of this 91# theme is to show what *can* be done, not necessarily what 92# *should* be done, the following makes some questionable 93# design decisions from an aesthetic standpoint.) 94# 95if {![catch {package require tile::theme::blue}]} { 96style theme settings "blue" { 97 98 # 99 # Default values: 100 # 101 style default Toolbutton \ 102 -width 0 -relief flat -borderwidth 2 \ 103 -background #6699CC -foreground #000000 ; 104 105 # 106 # Configure state-specific values for -relief, as before: 107 # 108 style map Toolbutton -relief { 109 disabled flat 110 selected sunken 111 pressed sunken 112 active raised 113 } 114 115 # 116 # Adjust the -padding at the same time, to enhance 117 # the raised/sunken illusion: 118 # 119 style default Toolbutton -padding 4 120 style map Toolbutton -padding { 121 disabled {4} 122 selected {6 6 2 2} 123 pressed {6 6 2 2} 124 active {2 2 6 6} 125 } 126 127 # 128 # ... and change the foreground and background colors 129 # when the mouse cursor is over the widget: 130 # 131 style map Toolbutton -background { 132 active #008800 133 } -foreground { 134 active #FFFFFF 135 } 136} 137 138} 139 140# 141# ~ A final note: 142# 143# TIP #82 also says: "When -indicatoron is off and the button itself 144# is on, the relief continues to be hard-coded to sunken. For symmetry, 145# we might consider adding another -onrelief option to cover this 146# case. But it is difficult to imagine ever wanting to change the 147# value of -onrelief so it has been omitted from this TIP. 148# If there as strong desire to have -onrelief, it can be added later." 149# ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 150# 151# The Tile project aims to make sure that this never needs to happen. 152# 153