git-gui: add build tab
[git-gui/bertw.git] / lib / themed.tcl
blob6c22d2441632050b27d6766bb3828c029b5eb445
1 # Functions for supporting the use of themed Tk widgets in git-gui.
2 # Copyright (C) 2009 Pat Thoyts <patthoyts@users.sourceforge.net>
4 proc InitTheme {} {
5 # Create a color label style (bg can be overridden by widget option)
6 ttk::style layout Color.TLabel {
7 Color.Label.border -sticky news -children {
8 Color.label.fill -sticky news -children {
9 Color.Label.padding -sticky news -children {
10 Color.Label.label -sticky news}}}}
11 eval [linsert [ttk::style configure TLabel] 0 \
12 ttk::style configure Color.TLabel]
13 ttk::style configure Color.TLabel \
14 -borderwidth 0 -relief flat -padding 2
15 ttk::style map Color.TLabel -background {{} gold}
16 # We also need a padded label.
17 ttk::style configure Padded.TLabel \
18 -padding {5 5} -borderwidth 1 -relief solid
19 # We need a gold frame.
20 ttk::style layout Gold.TFrame {
21 Gold.Frame.border -sticky nswe -children {
22 Gold.Frame.fill -sticky nswe}}
23 ttk::style configure Gold.TFrame -background gold -relief flat
24 # listboxes should have a theme border so embed in ttk::frame
25 ttk::style layout SListbox.TFrame {
26 SListbox.Frame.Entry.field -sticky news -border true -children {
27 SListbox.Frame.padding -sticky news
31 ttk::style layout Color.TCombobox {
32 Color.Combobox.field -sticky nswe -children {
33 Color.Combobox.downarrow -side right -sticky ns \
34 Color.Combobox.padding -expand 1 -sticky nswe -children {
35 Color.Combobox.textarea -sticky nswe}}}
36 eval [linsert [ttk::style configure TCombobox] 0 \
37 ttk::style configure Color.TCombobox]
38 ttk::style configure Color.TCombobox \
39 -borderwidth 0 \
40 -relief flat \
41 -padding 0
42 ttk::style map Color.TCombobox -fieldbackground {{} lightsalmon}
43 ttk::style map Color.TCombobox -background {{} lightsalmon}
45 # Handle either current Tk or older versions of 8.5
46 if {[catch {set theme [ttk::style theme use]}]} {
47 set theme $::ttk::currentTheme
50 if {[lsearch -exact {default alt classic clam} $theme] != -1} {
51 # Simple override of standard ttk::entry to change the field
52 # packground according to a state flag. We should use 'user1'
53 # but not all versions of 8.5 support that so make use of 'pressed'
54 # which is not normally in use for entry widgets.
55 ttk::style layout Edged.Entry [ttk::style layout TEntry]
56 ttk::style map Edged.Entry {*}[ttk::style map TEntry]
57 ttk::style configure Edged.Entry {*}[ttk::style configure TEntry] \
58 -fieldbackground lightgreen
59 ttk::style map Edged.Entry -fieldbackground {
60 {pressed !disabled} lightpink
62 } else {
63 # For fancier themes, in particular the Windows ones, the field
64 # element may not support changing the background color. So instead
65 # override the fill using the default fill element. If we overrode
66 # the vista theme field element we would loose the themed border
67 # of the widget.
68 catch {
69 ttk::style element create color.fill from default
72 ttk::style layout Edged.Entry {
73 Edged.Entry.field -sticky nswe -border 0 -children {
74 Edged.Entry.border -sticky nswe -border 1 -children {
75 Edged.Entry.padding -sticky nswe -children {
76 Edged.Entry.color.fill -sticky nswe -children {
77 Edged.Entry.textarea -sticky nswe
84 ttk::style configure Edged.Entry {*}[ttk::style configure TEntry] \
85 -background lightgreen -padding 0 -borderwidth 0
86 ttk::style map Edged.Entry {*}[ttk::style map TEntry] \
87 -background {{pressed !disabled} lightpink}
90 if {[lsearch [bind . <<ThemeChanged>>] InitTheme] == -1} {
91 bind . <<ThemeChanged>> +[namespace code [list InitTheme]]
95 proc gold_frame {w args} {
96 global use_ttk
97 if {$use_ttk} {
98 eval [linsert $args 0 ttk::frame $w -style Gold.TFrame]
99 } else {
100 eval [linsert $args 0 frame $w -background gold]
104 proc tlabel {w args} {
105 global use_ttk
106 if {$use_ttk} {
107 set cmd [list ttk::label $w -style Color.TLabel]
108 foreach {k v} $args {
109 switch -glob -- $k {
110 -activebackground {}
111 default { lappend cmd $k $v }
114 eval $cmd
115 } else {
116 eval [linsert $args 0 label $w]
120 # The padded label gets used in the about class.
121 proc paddedlabel {w args} {
122 global use_ttk
123 if {$use_ttk} {
124 eval [linsert $args 0 ttk::label $w -style Padded.TLabel]
125 } else {
126 eval [linsert $args 0 label $w \
127 -padx 5 -pady 5 \
128 -justify left \
129 -anchor w \
130 -borderwidth 1 \
131 -relief solid]
135 # Create a toplevel for use as a dialog.
136 # If available, sets the EWMH dialog hint and if ttk is enabled
137 # place a themed frame over the surface.
138 proc Dialog {w args} {
139 eval [linsert $args 0 toplevel $w -class Dialog]
140 catch {wm attributes $w -type dialog}
141 pave_toplevel $w
142 return $w
145 # Tk toplevels are not themed - so pave it over with a themed frame to get
146 # the base color correct per theme.
147 proc pave_toplevel {w} {
148 global use_ttk
149 if {$use_ttk && ![winfo exists $w.!paving]} {
150 set paving [ttk::frame $w.!paving]
151 place $paving -x 0 -y 0 -relwidth 1 -relheight 1
152 lower $paving
156 # Create a scrolled listbox with appropriate border for the current theme.
157 # On many themes the border for a scrolled listbox needs to go around the
158 # listbox and the scrollbar.
159 proc slistbox {w args} {
160 global use_ttk NS
161 if {$use_ttk} {
162 set f [ttk::frame $w -style SListbox.TFrame -padding 2]
163 } else {
164 set f [frame $w -relief flat]
166 if {[catch {
167 if {$use_ttk} {
168 eval [linsert $args 0 listbox $f.list -relief flat \
169 -highlightthickness 0 -borderwidth 0]
170 } else {
171 eval [linsert $args 0 listbox $f.list]
173 ${NS}::scrollbar $f.vs -command [list $f.list yview]
174 $f.list configure -yscrollcommand [list $f.vs set]
175 grid $f.list $f.vs -sticky news
176 grid rowconfigure $f 0 -weight 1
177 grid columnconfigure $f 0 -weight 1
178 bind $f.list <<ListboxSelect>> \
179 [list event generate $w <<ListboxSelect>>]
180 interp hide {} $w
181 interp alias {} $w {} $f.list
182 } err]} {
183 destroy $f
184 return -code error $err
186 return $w
189 # fetch the background color from a widget.
190 proc get_bg_color {w} {
191 global use_ttk
192 if {$use_ttk} {
193 set bg [ttk::style lookup [winfo class $w] -background]
194 } else {
195 set bg [$w cget -background]
197 return $bg
200 # ttk::spinbox didn't get added until 8.6
201 proc tspinbox {w args} {
202 global use_ttk
203 if {$use_ttk && [llength [info commands ttk::spinbox]] > 0} {
204 eval [linsert $args 0 ttk::spinbox $w]
205 } else {
206 eval [linsert $args 0 spinbox $w]
210 proc tentry {w args} {
211 global use_ttk
212 if {$use_ttk} {
213 InitTheme
214 ttk::entry $w -style Edged.Entry
215 } else {
216 entry $w
219 rename $w _$w
220 interp alias {} $w {} tentry_widgetproc $w
221 eval [linsert $args 0 tentry_widgetproc $w configure]
222 return $w
224 proc tentry_widgetproc {w cmd args} {
225 global use_ttk
226 switch -- $cmd {
227 state {
228 if {$use_ttk} {
229 return [uplevel 1 [list _$w $cmd] $args]
230 } else {
231 if {[lsearch -exact $args pressed] != -1} {
232 _$w configure -background lightpink
233 } else {
234 _$w configure -background lightgreen
238 configure {
239 if {$use_ttk} {
240 if {[set n [lsearch -exact $args -background]] != -1} {
241 set args [lreplace $args $n [incr n]]
242 if {[llength $args] == 0} {return}
245 return [uplevel 1 [list _$w $cmd] $args]
247 default { return [uplevel 1 [list _$w $cmd] $args] }
251 # Tk 8.6 provides a standard font selection dialog. This uses the native
252 # dialogs on Windows and MacOSX or a standard Tk dialog on X11.
253 proc tchoosefont {w title familyvar sizevar} {
254 if {[package vsatisfies [package provide Tk] 8.6]} {
255 upvar #0 $familyvar family
256 upvar #0 $sizevar size
257 tk fontchooser configure -parent $w -title $title \
258 -font [list $family $size] \
259 -command [list on_choosefont $familyvar $sizevar]
260 tk fontchooser show
261 } else {
262 choose_font::pick $w $title $familyvar $sizevar
266 # Called when the Tk 8.6 fontchooser selects a font.
267 proc on_choosefont {familyvar sizevar font} {
268 upvar #0 $familyvar family
269 upvar #0 $sizevar size
270 set font [font actual $font]
271 set family [dict get $font -family]
272 set size [dict get $font -size]
275 # Local variables:
276 # mode: tcl
277 # indent-tabs-mode: t
278 # tab-width: 4
279 # End: