git-gui: Refactor about dialog code into its own module
[git-gui/kampbell.git] / lib / option.tcl
blobde6bd90edc150f878dd2e78a6279bc0679de71f7
1 # git-gui options editor
2 # Copyright (C) 2006, 2007 Shawn Pearce
4 proc save_config {} {
5 global default_config font_descs
6 global repo_config global_config
7 global repo_config_new global_config_new
9 foreach option $font_descs {
10 set name [lindex $option 0]
11 set font [lindex $option 1]
12 font configure $font \
13 -family $global_config_new(gui.$font^^family) \
14 -size $global_config_new(gui.$font^^size)
15 font configure ${font}bold \
16 -family $global_config_new(gui.$font^^family) \
17 -size $global_config_new(gui.$font^^size)
18 font configure ${font}italic \
19 -family $global_config_new(gui.$font^^family) \
20 -size $global_config_new(gui.$font^^size)
21 set global_config_new(gui.$name) [font configure $font]
22 unset global_config_new(gui.$font^^family)
23 unset global_config_new(gui.$font^^size)
26 foreach name [array names default_config] {
27 set value $global_config_new($name)
28 if {$value ne $global_config($name)} {
29 if {$value eq $default_config($name)} {
30 catch {git config --global --unset $name}
31 } else {
32 regsub -all "\[{}\]" $value {"} value
33 git config --global $name $value
35 set global_config($name) $value
36 if {$value eq $repo_config($name)} {
37 catch {git config --unset $name}
38 set repo_config($name) $value
43 foreach name [array names default_config] {
44 set value $repo_config_new($name)
45 if {$value ne $repo_config($name)} {
46 if {$value eq $global_config($name)} {
47 catch {git config --unset $name}
48 } else {
49 regsub -all "\[{}\]" $value {"} value
50 git config $name $value
52 set repo_config($name) $value
57 proc do_options {} {
58 global repo_config global_config font_descs
59 global repo_config_new global_config_new
61 array unset repo_config_new
62 array unset global_config_new
63 foreach name [array names repo_config] {
64 set repo_config_new($name) $repo_config($name)
66 load_config 1
67 foreach name [array names repo_config] {
68 switch -- $name {
69 gui.diffcontext {continue}
71 set repo_config_new($name) $repo_config($name)
73 foreach name [array names global_config] {
74 set global_config_new($name) $global_config($name)
77 set w .options_editor
78 toplevel $w
79 wm geometry $w "+[winfo rootx .]+[winfo rooty .]"
81 label $w.header -text [mc "Options"] \
82 -font font_uibold
83 pack $w.header -side top -fill x
85 frame $w.buttons
86 button $w.buttons.restore -text [mc "Restore Defaults"] \
87 -default normal \
88 -command do_restore_defaults
89 pack $w.buttons.restore -side left
90 button $w.buttons.save -text [mc Save] \
91 -default active \
92 -command [list do_save_config $w]
93 pack $w.buttons.save -side right
94 button $w.buttons.cancel -text [mc "Cancel"] \
95 -default normal \
96 -command [list destroy $w]
97 pack $w.buttons.cancel -side right -padx 5
98 pack $w.buttons -side bottom -fill x -pady 10 -padx 10
100 labelframe $w.repo -text [mc "%s Repository" [reponame]]
101 labelframe $w.global -text [mc "Global (All Repositories)"]
102 pack $w.repo -side left -fill both -expand 1 -pady 5 -padx 5
103 pack $w.global -side right -fill both -expand 1 -pady 5 -padx 5
105 set optid 0
106 foreach option {
107 {t user.name {mc "User Name"}}
108 {t user.email {mc "Email Address"}}
110 {b merge.summary {mc "Summarize Merge Commits"}}
111 {i-1..5 merge.verbosity {mc "Merge Verbosity"}}
112 {b merge.diffstat {mc "Show Diffstat After Merge"}}
114 {b gui.trustmtime {mc "Trust File Modification Timestamps"}}
115 {b gui.pruneduringfetch {mc "Prune Tracking Branches During Fetch"}}
116 {b gui.matchtrackingbranch {mc "Match Tracking Branches"}}
117 {i-0..99 gui.diffcontext {mc "Number of Diff Context Lines"}}
118 {t gui.newbranchtemplate {mc "New Branch Name Template"}}
120 set type [lindex $option 0]
121 set name [lindex $option 1]
122 set text [eval [lindex $option 2]]
123 incr optid
124 foreach f {repo global} {
125 switch -glob -- $type {
127 checkbutton $w.$f.$optid -text $text \
128 -variable ${f}_config_new($name) \
129 -onvalue true \
130 -offvalue false
131 pack $w.$f.$optid -side top -anchor w
133 i-* {
134 regexp -- {-(\d+)\.\.(\d+)$} $type _junk min max
135 frame $w.$f.$optid
136 label $w.$f.$optid.l -text "$text:"
137 pack $w.$f.$optid.l -side left -anchor w -fill x
138 spinbox $w.$f.$optid.v \
139 -textvariable ${f}_config_new($name) \
140 -from $min \
141 -to $max \
142 -increment 1 \
143 -width [expr {1 + [string length $max]}]
144 bind $w.$f.$optid.v <FocusIn> {%W selection range 0 end}
145 pack $w.$f.$optid.v -side right -anchor e -padx 5
146 pack $w.$f.$optid -side top -anchor w -fill x
149 frame $w.$f.$optid
150 label $w.$f.$optid.l -text "$text:"
151 entry $w.$f.$optid.v \
152 -borderwidth 1 \
153 -relief sunken \
154 -width 20 \
155 -textvariable ${f}_config_new($name)
156 pack $w.$f.$optid.l -side left -anchor w
157 pack $w.$f.$optid.v -side left -anchor w \
158 -fill x -expand 1 \
159 -padx 5
160 pack $w.$f.$optid -side top -anchor w -fill x
166 set all_fonts [lsort [font families]]
167 foreach option $font_descs {
168 set name [lindex $option 0]
169 set font [lindex $option 1]
170 set text [eval [lindex $option 2]]
172 set global_config_new(gui.$font^^family) \
173 [font configure $font -family]
174 set global_config_new(gui.$font^^size) \
175 [font configure $font -size]
177 frame $w.global.$name
178 label $w.global.$name.l -text "$text:"
179 button $w.global.$name.b \
180 -text [mc "Change Font"] \
181 -command [list \
182 choose_font::pick \
183 $w \
184 [mc "Choose %s" $text] \
185 global_config_new(gui.$font^^family) \
186 global_config_new(gui.$font^^size) \
188 label $w.global.$name.f -textvariable global_config_new(gui.$font^^family)
189 label $w.global.$name.s -textvariable global_config_new(gui.$font^^size)
190 label $w.global.$name.pt -text [mc "pt."]
191 pack $w.global.$name.l -side left -anchor w
192 pack $w.global.$name.b -side right -anchor e
193 pack $w.global.$name.pt -side right -anchor w
194 pack $w.global.$name.s -side right -anchor w
195 pack $w.global.$name.f -side right -anchor w
196 pack $w.global.$name -side top -anchor w -fill x
199 bind $w <Visibility> "grab $w; focus $w.buttons.save"
200 bind $w <Key-Escape> "destroy $w"
201 bind $w <Key-Return> [list do_save_config $w]
202 wm title $w [append "[appname] ([reponame]): " [mc "Options"]]
203 tkwait window $w
206 proc do_restore_defaults {} {
207 global font_descs default_config repo_config
208 global repo_config_new global_config_new
210 foreach name [array names default_config] {
211 set repo_config_new($name) $default_config($name)
212 set global_config_new($name) $default_config($name)
215 foreach option $font_descs {
216 set name [lindex $option 0]
217 set repo_config(gui.$name) $default_config(gui.$name)
219 apply_config
221 foreach option $font_descs {
222 set name [lindex $option 0]
223 set font [lindex $option 1]
224 set global_config_new(gui.$font^^family) \
225 [font configure $font -family]
226 set global_config_new(gui.$font^^size) \
227 [font configure $font -size]
231 proc do_save_config {w} {
232 if {[catch {save_config} err]} {
233 error_popup [strcat [mc "Failed to completely save options:"] "\n\n$err"]
235 reshow_diff
236 destroy $w