lib / option.tclon commit git-gui: Save geometry before the window layout is damaged (39fa2a9)
   1# git-gui options editor
   2# Copyright (C) 2006, 2007 Shawn Pearce
   3
   4proc save_config {} {
   5        global default_config font_descs
   6        global repo_config global_config
   7        global repo_config_new global_config_new
   8
   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)
  24        }
  25
  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
  34                        }
  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
  39                        }
  40                }
  41        }
  42
  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
  51                        }
  52                        set repo_config($name) $value
  53                }
  54        }
  55}
  56
  57proc do_about {} {
  58        global appvers copyright
  59        global tcl_patchLevel tk_patchLevel
  60
  61        set w .about_dialog
  62        toplevel $w
  63        wm geometry $w "+[winfo rootx .]+[winfo rooty .]"
  64
  65        label $w.header -text "About [appname]" \
  66                -font font_uibold
  67        pack $w.header -side top -fill x
  68
  69        frame $w.buttons
  70        button $w.buttons.close -text {Close} \
  71                -default active \
  72                -command [list destroy $w]
  73        pack $w.buttons.close -side right
  74        pack $w.buttons -side bottom -fill x -pady 10 -padx 10
  75
  76        label $w.desc \
  77                -text "git-gui - a graphical user interface for Git.
  78$copyright" \
  79                -padx 5 -pady 5 \
  80                -justify left \
  81                -anchor w \
  82                -borderwidth 1 \
  83                -relief solid
  84        pack $w.desc -side top -fill x -padx 5 -pady 5
  85
  86        set v {}
  87        append v "git-gui version $appvers\n"
  88        append v "[git version]\n"
  89        append v "\n"
  90        if {$tcl_patchLevel eq $tk_patchLevel} {
  91                append v "Tcl/Tk version $tcl_patchLevel"
  92        } else {
  93                append v "Tcl version $tcl_patchLevel"
  94                append v ", Tk version $tk_patchLevel"
  95        }
  96
  97        label $w.vers \
  98                -text $v \
  99                -padx 5 -pady 5 \
 100                -justify left \
 101                -anchor w \
 102                -borderwidth 1 \
 103                -relief solid
 104        pack $w.vers -side top -fill x -padx 5 -pady 5
 105
 106        menu $w.ctxm -tearoff 0
 107        $w.ctxm add command \
 108                -label {Copy} \
 109                -command "
 110                clipboard clear
 111                clipboard append -format STRING -type STRING -- \[$w.vers cget -text\]
 112        "
 113
 114        bind $w <Visibility> "grab $w; focus $w.buttons.close"
 115        bind $w <Key-Escape> "destroy $w"
 116        bind $w <Key-Return> "destroy $w"
 117        bind_button3 $w.vers "tk_popup $w.ctxm %X %Y; grab $w; focus $w"
 118        wm title $w "About [appname]"
 119        tkwait window $w
 120}
 121
 122proc do_options {} {
 123        global repo_config global_config font_descs
 124        global repo_config_new global_config_new
 125
 126        array unset repo_config_new
 127        array unset global_config_new
 128        foreach name [array names repo_config] {
 129                set repo_config_new($name) $repo_config($name)
 130        }
 131        load_config 1
 132        foreach name [array names repo_config] {
 133                switch -- $name {
 134                gui.diffcontext {continue}
 135                }
 136                set repo_config_new($name) $repo_config($name)
 137        }
 138        foreach name [array names global_config] {
 139                set global_config_new($name) $global_config($name)
 140        }
 141
 142        set w .options_editor
 143        toplevel $w
 144        wm geometry $w "+[winfo rootx .]+[winfo rooty .]"
 145
 146        label $w.header -text "Options" \
 147                -font font_uibold
 148        pack $w.header -side top -fill x
 149
 150        frame $w.buttons
 151        button $w.buttons.restore -text {Restore Defaults} \
 152                -default normal \
 153                -command do_restore_defaults
 154        pack $w.buttons.restore -side left
 155        button $w.buttons.save -text Save \
 156                -default active \
 157                -command [list do_save_config $w]
 158        pack $w.buttons.save -side right
 159        button $w.buttons.cancel -text {Cancel} \
 160                -default normal \
 161                -command [list destroy $w]
 162        pack $w.buttons.cancel -side right -padx 5
 163        pack $w.buttons -side bottom -fill x -pady 10 -padx 10
 164
 165        labelframe $w.repo -text "[reponame] Repository"
 166        labelframe $w.global -text {Global (All Repositories)}
 167        pack $w.repo -side left -fill both -expand 1 -pady 5 -padx 5
 168        pack $w.global -side right -fill both -expand 1 -pady 5 -padx 5
 169
 170        set optid 0
 171        foreach option {
 172                {t user.name {User Name}}
 173                {t user.email {Email Address}}
 174
 175                {b merge.summary {Summarize Merge Commits}}
 176                {i-1..5 merge.verbosity {Merge Verbosity}}
 177
 178                {b gui.trustmtime  {Trust File Modification Timestamps}}
 179                {i-0..99 gui.diffcontext {Number of Diff Context Lines}}
 180                {t gui.newbranchtemplate {New Branch Name Template}}
 181                } {
 182                set type [lindex $option 0]
 183                set name [lindex $option 1]
 184                set text [lindex $option 2]
 185                incr optid
 186                foreach f {repo global} {
 187                        switch -glob -- $type {
 188                        b {
 189                                checkbutton $w.$f.$optid -text $text \
 190                                        -variable ${f}_config_new($name) \
 191                                        -onvalue true \
 192                                        -offvalue false
 193                                pack $w.$f.$optid -side top -anchor w
 194                        }
 195                        i-* {
 196                                regexp -- {-(\d+)\.\.(\d+)$} $type _junk min max
 197                                frame $w.$f.$optid
 198                                label $w.$f.$optid.l -text "$text:"
 199                                pack $w.$f.$optid.l -side left -anchor w -fill x
 200                                spinbox $w.$f.$optid.v \
 201                                        -textvariable ${f}_config_new($name) \
 202                                        -from $min \
 203                                        -to $max \
 204                                        -increment 1 \
 205                                        -width [expr {1 + [string length $max]}]
 206                                bind $w.$f.$optid.v <FocusIn> {%W selection range 0 end}
 207                                pack $w.$f.$optid.v -side right -anchor e -padx 5
 208                                pack $w.$f.$optid -side top -anchor w -fill x
 209                        }
 210                        t {
 211                                frame $w.$f.$optid
 212                                label $w.$f.$optid.l -text "$text:"
 213                                entry $w.$f.$optid.v \
 214                                        -borderwidth 1 \
 215                                        -relief sunken \
 216                                        -width 20 \
 217                                        -textvariable ${f}_config_new($name)
 218                                pack $w.$f.$optid.l -side left -anchor w
 219                                pack $w.$f.$optid.v -side left -anchor w \
 220                                        -fill x -expand 1 \
 221                                        -padx 5
 222                                pack $w.$f.$optid -side top -anchor w -fill x
 223                        }
 224                        }
 225                }
 226        }
 227
 228        set all_fonts [lsort [font families]]
 229        foreach option $font_descs {
 230                set name [lindex $option 0]
 231                set font [lindex $option 1]
 232                set text [lindex $option 2]
 233
 234                set global_config_new(gui.$font^^family) \
 235                        [font configure $font -family]
 236                set global_config_new(gui.$font^^size) \
 237                        [font configure $font -size]
 238
 239                frame $w.global.$name
 240                label $w.global.$name.l -text "$text:"
 241                pack $w.global.$name.l -side left -anchor w -fill x
 242                eval tk_optionMenu $w.global.$name.family \
 243                        global_config_new(gui.$font^^family) \
 244                        $all_fonts
 245                spinbox $w.global.$name.size \
 246                        -textvariable global_config_new(gui.$font^^size) \
 247                        -from 2 -to 80 -increment 1 \
 248                        -width 3
 249                bind $w.global.$name.size <FocusIn> {%W selection range 0 end}
 250                pack $w.global.$name.size -side right -anchor e
 251                pack $w.global.$name.family -side right -anchor e
 252                pack $w.global.$name -side top -anchor w -fill x
 253        }
 254
 255        bind $w <Visibility> "grab $w; focus $w.buttons.save"
 256        bind $w <Key-Escape> "destroy $w"
 257        bind $w <Key-Return> [list do_save_config $w]
 258        wm title $w "[appname] ([reponame]): Options"
 259        tkwait window $w
 260}
 261
 262proc do_restore_defaults {} {
 263        global font_descs default_config repo_config
 264        global repo_config_new global_config_new
 265
 266        foreach name [array names default_config] {
 267                set repo_config_new($name) $default_config($name)
 268                set global_config_new($name) $default_config($name)
 269        }
 270
 271        foreach option $font_descs {
 272                set name [lindex $option 0]
 273                set repo_config(gui.$name) $default_config(gui.$name)
 274        }
 275        apply_config
 276
 277        foreach option $font_descs {
 278                set name [lindex $option 0]
 279                set font [lindex $option 1]
 280                set global_config_new(gui.$font^^family) \
 281                        [font configure $font -family]
 282                set global_config_new(gui.$font^^size) \
 283                        [font configure $font -size]
 284        }
 285}
 286
 287proc do_save_config {w} {
 288        if {[catch {save_config} err]} {
 289                error_popup "Failed to completely save options:\n\n$err"
 290        }
 291        reshow_diff
 292        destroy $w
 293}