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 ttk_get_current_theme
{} {
5 # Handle either current Tk or older versions of 8.5
6 if {[catch {set theme
[ttk
::style theme use
]}]} {
7 set theme
$::ttk::currentTheme
13 # Create a color label style (bg can be overridden by widget option)
14 ttk
::style layout Color.TLabel
{
15 Color.Label.border
-sticky news
-children {
16 Color.
label.fill
-sticky news
-children {
17 Color.Label.padding
-sticky news
-children {
18 Color.Label.
label -sticky news
}}}}
19 eval [linsert [ttk
::style configure TLabel
] 0 \
20 ttk
::style configure Color.TLabel
]
21 ttk
::style configure Color.TLabel
\
22 -borderwidth 0 -relief flat
-padding 2
23 ttk
::style map Color.TLabel
-background {{} gold
}
24 # We also need a padded label.
25 ttk
::style configure Padded.TLabel
\
26 -padding {5 5} -borderwidth 1 -relief solid
27 # We need a gold frame.
28 ttk
::style layout Gold.TFrame
{
29 Gold.Frame.border
-sticky nswe
-children {
30 Gold.Frame.fill
-sticky nswe
}}
31 ttk
::style configure Gold.TFrame
-background gold
-relief flat
32 # listboxes should have a theme border so embed in ttk::frame
33 ttk
::style layout SListbox.TFrame
{
34 SListbox.Frame.Entry.field
-sticky news
-border true
-children {
35 SListbox.Frame.padding
-sticky news
39 set theme
[ttk_get_current_theme
]
41 if {[lsearch -exact {default alt classic clam
} $theme] != -1} {
42 # Simple override of standard ttk::entry to change the field
43 # packground according to a state flag. We should use 'user1'
44 # but not all versions of 8.5 support that so make use of 'pressed'
45 # which is not normally in use for entry widgets.
46 ttk
::style layout Edged.Entry
[ttk
::style layout TEntry
]
47 ttk
::style map Edged.Entry
{*}[ttk
::style map TEntry
]
48 ttk
::style configure Edged.Entry
{*}[ttk
::style configure TEntry
] \
49 -fieldbackground lightgreen
50 ttk
::style map Edged.Entry
-fieldbackground {
51 {pressed
!disabled
} lightpink
54 # For fancier themes, in particular the Windows ones, the field
55 # element may not support changing the background color. So instead
56 # override the fill using the default fill element. If we overrode
57 # the vista theme field element we would loose the themed border
60 ttk
::style element create color.fill from
default
63 ttk
::style layout Edged.Entry
{
64 Edged.Entry.field
-sticky nswe
-border 0 -children {
65 Edged.Entry.border
-sticky nswe
-border 1 -children {
66 Edged.Entry.padding
-sticky nswe
-children {
67 Edged.Entry.color.fill
-sticky nswe
-children {
68 Edged.Entry.textarea
-sticky nswe
75 ttk
::style configure Edged.Entry
{*}[ttk
::style configure TEntry
] \
76 -background lightgreen
-padding 0 -borderwidth 0
77 ttk
::style map Edged.Entry
{*}[ttk
::style map TEntry
] \
78 -background {{pressed
!disabled
} lightpink
}
81 if {[lsearch [bind .
<<ThemeChanged
>>] InitTheme
] == -1} {
82 bind .
<<ThemeChanged
>> +[namespace code
[list InitTheme
]]
86 # Define a style used for the surround of text widgets.
87 proc InitEntryFrame
{} {
88 ttk
::style theme settings
default {
89 ttk
::style layout EntryFrame
{
90 EntryFrame.field
-sticky nswe
-border 0 -children {
91 EntryFrame.fill
-sticky nswe
-children {
92 EntryFrame.padding
-sticky nswe
96 ttk
::style configure EntryFrame
-padding 1 -relief sunken
97 ttk
::style map EntryFrame
-background {}
99 ttk
::style theme settings classic
{
100 ttk
::style configure EntryFrame
-padding 2 -relief sunken
101 ttk
::style map EntryFrame
-background {}
103 ttk
::style theme settings alt
{
104 ttk
::style configure EntryFrame
-padding 2
105 ttk
::style map EntryFrame
-background {}
107 ttk
::style theme settings clam
{
108 ttk
::style configure EntryFrame
-padding 2
109 ttk
::style map EntryFrame
-background {}
112 # Ignore errors for missing native themes
114 ttk
::style theme settings winnative
{
115 ttk
::style configure EntryFrame
-padding 2
117 ttk
::style theme settings xpnative
{
118 ttk
::style configure EntryFrame
-padding 1
119 ttk
::style element create EntryFrame.field vsapi
\
120 EDIT
1 {disabled
4 focus 3 active
2 {} 1} -padding 1
122 ttk
::style theme settings vista
{
123 ttk
::style configure EntryFrame
-padding 2
124 ttk
::style element create EntryFrame.field vsapi
\
125 EDIT
6 {disabled
4 focus 3 active
2 {} 1} -padding 2
129 bind EntryFrame
<Enter
> {%W instate
!disabled
{%W state active
}}
130 bind EntryFrame
<Leave
> {%W state
!active
}
131 bind EntryFrame
<<ThemeChanged
>> {
132 set pad
[ttk
::style lookup EntryFrame
-padding]
133 %W configure
-padding [expr {$pad eq
{} ?
1 : $pad}]
137 proc gold_frame
{w args
} {
140 eval [linsert $args 0 ttk
::frame $w -style Gold.TFrame
]
142 eval [linsert $args 0 frame $w -background gold
]
146 proc tlabel
{w args
} {
149 set cmd
[list ttk
::label $w -style Color.TLabel
]
150 foreach {k v
} $args {
153 default { lappend cmd
$k $v }
158 eval [linsert $args 0 label $w]
162 # The padded label gets used in the about class.
163 proc paddedlabel
{w args
} {
166 eval [linsert $args 0 ttk
::label $w -style Padded.TLabel
]
168 eval [linsert $args 0 label $w \
177 # Create a toplevel for use as a dialog.
178 # If available, sets the EWMH dialog hint and if ttk is enabled
179 # place a themed frame over the surface.
180 proc Dialog
{w args
} {
181 eval [linsert $args 0 toplevel $w -class Dialog
]
182 catch {wm attributes
$w -type dialog
}
187 # Tk toplevels are not themed - so pave it over with a themed frame to get
188 # the base color correct per theme.
189 proc pave_toplevel
{w
} {
191 if {$use_ttk && ![winfo exists
$w.
!paving
]} {
192 set paving
[ttk
::frame $w.
!paving
]
193 place $paving -x 0 -y 0 -relwidth 1 -relheight 1
198 # Create a scrolled listbox with appropriate border for the current theme.
199 # On many themes the border for a scrolled listbox needs to go around the
200 # listbox and the scrollbar.
201 proc slistbox
{w args
} {
204 set f
[ttk
::frame $w -style SListbox.TFrame
-padding 2]
206 set f
[frame $w -relief flat
]
210 eval [linsert $args 0 listbox $f.
list -relief flat
\
211 -highlightthickness 0 -borderwidth 0]
213 eval [linsert $args 0 listbox $f.
list]
215 ${NS
}::scrollbar $f.vs
-command [list $f.
list yview
]
216 $f.
list configure
-yscrollcommand [list $f.vs
set]
217 grid $f.
list $f.vs
-sticky news
218 grid rowconfigure
$f 0 -weight 1
219 grid columnconfigure
$f 0 -weight 1
220 bind $f.
list <<ListboxSelect
>> \
221 [list event generate
$w <<ListboxSelect
>>]
223 interp alias
{} $w {} $f.
list
226 return -code error $err
231 # fetch the background color from a widget.
232 proc get_bg_color
{w
} {
235 set bg
[ttk
::style lookup
[winfo class
$w] -background]
237 set bg
[$w cget
-background]
242 # ttk::spinbox didn't get added until 8.6
243 proc tspinbox
{w args
} {
245 if {$use_ttk && [llength [info commands ttk
::spinbox]] > 0} {
246 eval [linsert $args 0 ttk
::spinbox $w]
248 eval [linsert $args 0 spinbox $w]
252 # Create a text widget with any theme specific properties.
253 proc ttext
{w args
} {
256 switch -- [ttk_get_current_theme
] {
257 "vista" - "xpnative" {
258 lappend args
-highlightthickness 0 -borderwidth 0
262 set w
[eval [linsert $args 0 text $w]]
264 if {[winfo class
[winfo parent
$w]] eq
"EntryFrame"} {
265 bind $w <FocusIn
> {[winfo parent
%W
] state
focus}
266 bind $w <FocusOut
> {[winfo parent
%W
] state
!focus}
272 # themed frame suitable for surrounding a text field.
273 proc textframe
{w args
} {
276 if {[catch {ttk
::style layout EntryFrame
}]} {
279 eval [linsert $args 0 ttk
::frame $w -class EntryFrame
-style EntryFrame
]
281 eval [linsert $args 0 frame $w]
286 proc tentry
{w args
} {
290 ttk
::entry $w -style Edged.Entry
296 interp alias
{} $w {} tentry_widgetproc
$w
297 eval [linsert $args 0 tentry_widgetproc
$w configure
]
300 proc tentry_widgetproc
{w cmd args
} {
305 return [uplevel 1 [list _
$w $cmd] $args]
307 if {[lsearch -exact $args pressed
] != -1} {
308 _
$w configure
-background lightpink
310 _
$w configure
-background lightgreen
316 if {[set n
[lsearch -exact $args -background]] != -1} {
317 set args
[lreplace $args $n [incr n
]]
318 if {[llength $args] == 0} {return}
321 return [uplevel 1 [list _
$w $cmd] $args]
323 default { return [uplevel 1 [list _
$w $cmd] $args] }
327 # Tk 8.6 provides a standard font selection dialog. This uses the native
328 # dialogs on Windows and MacOSX or a standard Tk dialog on X11.
329 proc tchoosefont
{w title familyvar sizevar
} {
330 if {[package vsatisfies
[package provide Tk
] 8.6]} {
331 upvar #0 $familyvar family
332 upvar #0 $sizevar size
333 tk fontchooser configure
-parent $w -title $title \
334 -font [list $family $size] \
335 -command [list on_choosefont
$familyvar $sizevar]
338 choose_font
::pick $w $title $familyvar $sizevar
342 # Called when the Tk 8.6 fontchooser selects a font.
343 proc on_choosefont
{familyvar sizevar
font} {
344 upvar #0 $familyvar family
345 upvar #0 $sizevar size
346 set font [font actual
$font]
347 set family
[dict get
$font -family]
348 set size
[dict get
$font -size]
353 # indent-tabs-mode: t