@matheusbsbs / matheus-bagliano / commits / 9a7558f348
Fork 0

gitk: Add a font chooser

This adds buttons to the edit preferences window to allow the user to choose the main font, the text font (used for the diff display window) and the UI font. Pressing those buttons pops up a font chooser window that lets the user pick the font family, size, weight (bold/normal) and slant (roman/italic). Signed-off-by: Paul Mackerras <paulus@samba.org>

Paul Mackerras committed Oct 6, 2007 at 20:16 UTC 9a7558f348772ab3c2fb3d4beda3a3a7af1e843a
1 file changed +155 -1
gitk
+155 -1
@@ -7875,6 +7875,130 @@ proc doquit {} {
7875 destroy .
7876 }
7877
7878 +proc mkfontdisp {font top which} {
7879 + global fontattr fontpref $font
7880 +
7881 + set fontpref($font) [set $font]
7882 + button $top.${font}but -text $which -font optionfont \
7883 + -command [list choosefont $font $which]
7884 + label $top.$font -relief flat -font $font \
7885 + -text $fontattr($font,family) -justify left
7886 + grid x $top.${font}but $top.$font -sticky w
7887 +}
7888 +
7889 +proc choosefont {font which} {
7890 + global fontparam fontlist fonttop fontattr
7891 +
7892 + set fontparam(which) $which
7893 + set fontparam(font) $font
7894 + set fontparam(family) [font actual $font -family]
7895 + set fontparam(size) $fontattr($font,size)
7896 + set fontparam(weight) $fontattr($font,weight)
7897 + set fontparam(slant) $fontattr($font,slant)
7898 + set top .gitkfont
7899 + set fonttop $top
7900 + if {![winfo exists $top]} {
7901 + font create sample
7902 + eval font config sample [font actual $font]
7903 + toplevel $top
7904 + wm title $top "Gitk font chooser"
7905 + label $top.l -textvariable fontparam(which) -font uifont
7906 + pack $top.l -side top
7907 + set fontlist [lsort [font families]]
7908 + frame $top.f
7909 + listbox $top.f.fam -listvariable fontlist \
7910 + -yscrollcommand [list $top.f.sb set]
7911 + bind $top.f.fam <<ListboxSelect>> selfontfam
7912 + scrollbar $top.f.sb -command [list $top.f.fam yview]
7913 + pack $top.f.sb -side right -fill y
7914 + pack $top.f.fam -side left -fill both -expand 1
7915 + pack $top.f -side top -fill both -expand 1
7916 + frame $top.g
7917 + spinbox $top.g.size -from 4 -to 40 -width 4 \
7918 + -textvariable fontparam(size) \
7919 + -validatecommand {string is integer -strict %s}
7920 + checkbutton $top.g.bold -padx 5 \
7921 + -font {{Times New Roman} 12 bold} -text "B" -indicatoron 0 \
7922 + -variable fontparam(weight) -onvalue bold -offvalue normal
7923 + checkbutton $top.g.ital -padx 5 \
7924 + -font {{Times New Roman} 12 italic} -text "I" -indicatoron 0 \
7925 + -variable fontparam(slant) -onvalue italic -offvalue roman
7926 + pack $top.g.size $top.g.bold $top.g.ital -side left
7927 + pack $top.g -side top
7928 + canvas $top.c -width 150 -height 50 -border 2 -relief sunk \
7929 + -background white
7930 + $top.c create text 100 25 -anchor center -text $which -font sample \
7931 + -fill black -tags text
7932 + bind $top.c <Configure> [list centertext $top.c]
7933 + pack $top.c -side top -fill x
7934 + frame $top.buts
7935 + button $top.buts.ok -text "OK" -command fontok -default active \
7936 + -font uifont
7937 + button $top.buts.can -text "Cancel" -command fontcan -default normal \
7938 + -font uifont
7939 + grid $top.buts.ok $top.buts.can
7940 + grid columnconfigure $top.buts 0 -weight 1 -uniform a
7941 + grid columnconfigure $top.buts 1 -weight 1 -uniform a
7942 + pack $top.buts -side bottom -fill x
7943 + trace add variable fontparam write chg_fontparam
7944 + } else {
7945 + raise $top
7946 + $top.c itemconf text -text $which
7947 + }
7948 + set i [lsearch -exact $fontlist $fontparam(family)]
7949 + if {$i >= 0} {
7950 + $top.f.fam selection set $i
7951 + $top.f.fam see $i
7952 + }
7953 +}
7954 +
7955 +proc centertext {w} {
7956 + $w coords text [expr {[winfo width $w] / 2}] [expr {[winfo height $w] / 2}]
7957 +}
7958 +
7959 +proc fontok {} {
7960 + global fontparam fontpref prefstop
7961 +
7962 + set f $fontparam(font)
7963 + set fontpref($f) [list $fontparam(family) $fontparam(size)]
7964 + if {$fontparam(weight) eq "bold"} {
7965 + lappend fontpref($f) "bold"
7966 + }
7967 + if {$fontparam(slant) eq "italic"} {
7968 + lappend fontpref($f) "italic"
7969 + }
7970 + set w $prefstop.$f
7971 + $w conf -text $fontparam(family) -font $fontpref($f)
7972 +
7973 + fontcan
7974 +}
7975 +
7976 +proc fontcan {} {
7977 + global fonttop fontparam
7978 +
7979 + if {[info exists fonttop]} {
7980 + catch {destroy $fonttop}
7981 + catch {font delete sample}
7982 + unset fonttop
7983 + unset fontparam
7984 + }
7985 +}
7986 +
7987 +proc selfontfam {} {
7988 + global fonttop fontparam
7989 +
7990 + set i [$fonttop.f.fam curselection]
7991 + if {$i ne {}} {
7992 + set fontparam(family) [$fonttop.f.fam get $i]
7993 + }
7994 +}
7995 +
7996 +proc chg_fontparam {v sub op} {
7997 + global fontparam
7998 +
7999 + font config sample -$sub $fontparam($sub)
8000 +}
8001 +
8002 proc doprefs {} {
8003 global maxwidth maxgraphpct diffopts
8004 global oldprefs prefstop showneartags showlocalchanges
@@ -7958,6 +8082,13 @@ proc doprefs {} {
8082 -command [list choosecolor selectbgcolor 0 $top.selbgsep background setselbg]
8083 grid x $top.selbgbut $top.selbgsep -sticky w
8084
8085 + label $top.cfont -text "Fonts: press to choose"
8086 + $top.cfont configure -font uifont
8087 + grid $top.cfont - -sticky w -pady 10
8088 + mkfontdisp mainfont $top "Main font"
8089 + mkfontdisp textfont $top "Diff display font"
8090 + mkfontdisp uifont $top "User interface font"
8091 +
8092 frame $top.buts
8093 button $top.buts.ok -text "OK" -command prefsok -default active
8094 $top.buts.ok configure -font uifont
@@ -8018,14 +8149,37 @@ proc prefscan {} {
8149 }
8150 catch {destroy $prefstop}
8151 unset prefstop
8152 + fontcan
8153 }
8154
8155 proc prefsok {} {
8156 global maxwidth maxgraphpct
8157 global oldprefs prefstop showneartags showlocalchanges
8158 + global fontpref mainfont textfont uifont
8159
8160 catch {destroy $prefstop}
8161 unset prefstop
8162 + fontcan
8163 + set fontchanged 0
8164 + if {$mainfont ne $fontpref(mainfont)} {
8165 + set mainfont $fontpref(mainfont)
8166 + parsefont mainfont $mainfont
8167 + eval font configure mainfont [fontflags mainfont]
8168 + eval font configure mainfontbold [fontflags mainfont 1]
8169 + setcoords
8170 + set fontchanged 1
8171 + }
8172 + if {$textfont ne $fontpref(textfont)} {
8173 + set textfont $fontpref(textfont)
8174 + parsefont textfont $textfont
8175 + eval font configure textfont [fontflags textfont]
8176 + eval font configure textfontbold [fontflags textfont 1]
8177 + }
8178 + if {$uifont ne $fontpref(uifont)} {
8179 + set uifont $fontpref(uifont)
8180 + parsefont uifont $uifont
8181 + eval font configure uifont [fontflags uifont]
8182 + }
8183 settabs
8184 if {$showlocalchanges != $oldprefs(showlocalchanges)} {
8185 if {$showlocalchanges} {
@@ -8034,7 +8188,7 @@ proc prefsok {} {
8188 dohidelocalchanges
8189 }
8190 }
8037 - if {$maxwidth != $oldprefs(maxwidth)
8191 + if {$fontchanged || $maxwidth != $oldprefs(maxwidth)
8192 || $maxgraphpct != $oldprefs(maxgraphpct)} {
8193 redisplay
8194 } elseif {$showneartags != $oldprefs(showneartags)} {