fontchooser.tcl 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449
  1. # fontchooser.tcl -
  2. #
  3. # A themeable Tk font selection dialog. See TIP #324.
  4. #
  5. # Copyright (C) 2008 Keith Vetter
  6. # Copyright (C) 2008 Pat Thoyts <patthoyts@users.sourceforge.net>
  7. #
  8. # See the file "license.terms" for information on usage and redistribution
  9. # of this file, and for a DISCLAIMER OF ALL WARRANTIES.
  10. namespace eval ::tk::fontchooser {
  11. variable S
  12. set S(W) .__tk__fontchooser
  13. set S(fonts) [lsort -dictionary [font families]]
  14. set S(styles) [list \
  15. [::msgcat::mc "Regular"] \
  16. [::msgcat::mc "Italic"] \
  17. [::msgcat::mc "Bold"] \
  18. [::msgcat::mc "Bold Italic"] \
  19. ]
  20. set S(sizes) {8 9 10 11 12 14 16 18 20 22 24 26 28 36 48 72}
  21. set S(strike) 0
  22. set S(under) 0
  23. set S(first) 1
  24. set S(sampletext) [::msgcat::mc "AaBbYyZz01"]
  25. set S(-parent) .
  26. set S(-title) [::msgcat::mc "Font"]
  27. set S(-command) ""
  28. set S(-font) TkDefaultFont
  29. }
  30. proc ::tk::fontchooser::Setup {} {
  31. variable S
  32. # Canonical versions of font families, styles, etc. for easier searching
  33. set S(fonts,lcase) {}
  34. foreach font $S(fonts) { lappend S(fonts,lcase) [string tolower $font]}
  35. set S(styles,lcase) {}
  36. foreach style $S(styles) { lappend S(styles,lcase) [string tolower $style]}
  37. set S(sizes,lcase) $S(sizes)
  38. ::ttk::style layout FontchooserFrame {
  39. Entry.field -sticky news -border true -children {
  40. FontchooserFrame.padding -sticky news
  41. }
  42. }
  43. bind [winfo class .] <<ThemeChanged>> \
  44. [list +ttk::style layout FontchooserFrame \
  45. [ttk::style layout FontchooserFrame]]
  46. namespace ensemble create -map {
  47. show ::tk::fontchooser::Show
  48. hide ::tk::fontchooser::Hide
  49. configure ::tk::fontchooser::Configure
  50. }
  51. }
  52. ::tk::fontchooser::Setup
  53. proc ::tk::fontchooser::Show {} {
  54. variable S
  55. if {![winfo exists $S(W)]} {
  56. Create
  57. wm transient $S(W) [winfo toplevel $S(-parent)]
  58. tk::PlaceWindow $S(W) widget $S(-parent)
  59. }
  60. wm deiconify $S(W)
  61. }
  62. proc ::tk::fontchooser::Hide {} {
  63. variable S
  64. wm withdraw $S(W)
  65. }
  66. proc ::tk::fontchooser::Configure {args} {
  67. variable S
  68. set specs {
  69. {-parent "" "" . }
  70. {-title "" "" ""}
  71. {-font "" "" ""}
  72. {-command "" "" ""}
  73. }
  74. if {[llength $args] == 0} {
  75. set result {}
  76. foreach spec $specs {
  77. foreach {name xx yy default} $spec break
  78. lappend result $name \
  79. [expr {[info exists S($name)] ? $S($name) : $default}]
  80. }
  81. lappend result -visible \
  82. [expr {[winfo exists $S(W)] && [winfo ismapped $S(W)]}]
  83. return $result
  84. }
  85. if {[llength $args] == 1} {
  86. set option [lindex $args 0]
  87. if {[string equal $option "-visible"]} {
  88. return [expr {[winfo exists $S(W)] && [winfo ismapped $S(W)]}]
  89. } elseif {[info exists S($option)]} {
  90. return $S($option)
  91. }
  92. return -code error -errorcode [list TK LOOKUP OPTION $option] \
  93. "bad option \"$option\": must be\
  94. -command, -font, -parent, -title or -visible"
  95. }
  96. set cache [dict create -parent $S(-parent) -title $S(-title) \
  97. -font $S(-font) -command $S(-command)]
  98. set r [tclParseConfigSpec [namespace which -variable S] $specs "" $args]
  99. if {![winfo exists $S(-parent)]} {
  100. set code [list TK LOOKUP WINDOW $S(-parent)]
  101. set err "bad window path name \"$S(-parent)\""
  102. array set S $cache
  103. return -code error -errorcode $code $err
  104. }
  105. if {[string trim $S(-title)] eq ""} {
  106. set S(-title) [::msgcat::mc "Font"]
  107. }
  108. if {[winfo exists $S(W)] && [lsearch $args -font] != -1} {
  109. Init $S(-font)
  110. event generate $S(-parent) <<TkFontchooserFontChanged>>
  111. }
  112. return $r
  113. }
  114. proc ::tk::fontchooser::Create {} {
  115. variable S
  116. set windowName __tk__fontchooser
  117. if {$S(-parent) eq "."} {
  118. set S(W) .$windowName
  119. } else {
  120. set S(W) $S(-parent).$windowName
  121. }
  122. # Now build the dialog
  123. if {![winfo exists $S(W)]} {
  124. toplevel $S(W) -class TkFontDialog
  125. if {[package provide tcltest] ne {}} {set ::tk_dialog $S(W)}
  126. wm withdraw $S(W)
  127. wm title $S(W) $S(-title)
  128. wm transient $S(W) [winfo toplevel $S(-parent)]
  129. set outer [::ttk::frame $S(W).outer -padding {10 10}]
  130. ::tk::AmpWidget ::ttk::label $S(W).font -text [::msgcat::mc "&Font:"]
  131. ::tk::AmpWidget ::ttk::label $S(W).style -text [::msgcat::mc "Font st&yle:"]
  132. ::tk::AmpWidget ::ttk::label $S(W).size -text [::msgcat::mc "&Size:"]
  133. ttk::entry $S(W).efont -width 18 \
  134. -textvariable [namespace which -variable S](font)
  135. ttk::entry $S(W).estyle -width 10 \
  136. -textvariable [namespace which -variable S](style)
  137. ttk::entry $S(W).esize -textvariable [namespace which -variable S](size) \
  138. -width 3 -validate key -validatecommand {string is double %P}
  139. ttk_slistbox $S(W).lfonts -height 7 -exportselection 0 \
  140. -selectmode browse -activestyle none \
  141. -listvariable [namespace which -variable S](fonts)
  142. ttk_slistbox $S(W).lstyles -width 5 -height 7 -exportselection 0 \
  143. -selectmode browse -activestyle none \
  144. -listvariable [namespace which -variable S](styles)
  145. ttk_slistbox $S(W).lsizes -width 4 -height 7 -exportselection 0 \
  146. -selectmode browse -activestyle none \
  147. -listvariable [namespace which -variable S](sizes)
  148. set WE $S(W).effects
  149. ::ttk::labelframe $WE -text [::msgcat::mc "Effects"]
  150. ::tk::AmpWidget ::ttk::checkbutton $WE.strike \
  151. -variable [namespace which -variable S](strike) \
  152. -text [::msgcat::mc "Stri&keout"] \
  153. -command [namespace code [list Click strike]]
  154. ::tk::AmpWidget ::ttk::checkbutton $WE.under \
  155. -variable [namespace which -variable S](under) \
  156. -text [::msgcat::mc "&Underline"] \
  157. -command [namespace code [list Click under]]
  158. set bbox [::ttk::frame $S(W).bbox]
  159. ::ttk::button $S(W).ok -text [::msgcat::mc OK] -default active\
  160. -command [namespace code [list Done 1]]
  161. ::ttk::button $S(W).cancel -text [::msgcat::mc Cancel] \
  162. -command [namespace code [list Done 0]]
  163. ::tk::AmpWidget ::ttk::button $S(W).apply -text [::msgcat::mc "&Apply"] \
  164. -command [namespace code [list Apply]]
  165. wm protocol $S(W) WM_DELETE_WINDOW [namespace code [list Done 0]]
  166. # Calculate minimum sizes
  167. ttk::scrollbar $S(W).tmpvs
  168. set scroll_width [winfo reqwidth $S(W).tmpvs]
  169. destroy $S(W).tmpvs
  170. set minsize(gap) 10
  171. set minsize(bbox) [winfo reqwidth $S(W).ok]
  172. set minsize(fonts) \
  173. [expr {[font measure TkDefaultFont "Helvetica"] + $scroll_width}]
  174. set minsize(styles) \
  175. [expr {[font measure TkDefaultFont "Bold Italic"] + $scroll_width}]
  176. set minsize(sizes) \
  177. [expr {[font measure TkDefaultFont "-99"] + $scroll_width}]
  178. set min [expr {$minsize(gap) * 4}]
  179. foreach {what width} [array get minsize] { incr min $width }
  180. wm minsize $S(W) $min 260
  181. bind $S(W) <Return> [namespace code [list Done 1]]
  182. bind $S(W) <Escape> [namespace code [list Done 0]]
  183. bind $S(W) <Map> [namespace code [list Visibility %W 1]]
  184. bind $S(W) <Unmap> [namespace code [list Visibility %W 0]]
  185. bind $S(W) <Destroy> [namespace code [list Visibility %W 0]]
  186. bind $S(W).lfonts.list <<ListboxSelect>> [namespace code [list Click font]]
  187. bind $S(W).lstyles.list <<ListboxSelect>> [namespace code [list Click style]]
  188. bind $S(W).lsizes.list <<ListboxSelect>> [namespace code [list Click size]]
  189. bind $S(W) <Alt-Key> [list ::tk::AltKeyInDialog $S(W) %A]
  190. bind $S(W).font <<AltUnderlined>> [list ::focus $S(W).efont]
  191. bind $S(W).style <<AltUnderlined>> [list ::focus $S(W).estyle]
  192. bind $S(W).size <<AltUnderlined>> [list ::focus $S(W).esize]
  193. bind $S(W).apply <<AltUnderlined>> [namespace code [list Apply]]
  194. bind $WE.strike <<AltUnderlined>> [list $WE.strike invoke]
  195. bind $WE.under <<AltUnderlined>> [list $WE.under invoke]
  196. set WS $S(W).sample
  197. ::ttk::labelframe $WS -text [::msgcat::mc "Sample"]
  198. ::ttk::label $WS.sample -relief sunken -anchor center \
  199. -textvariable [namespace which -variable S](sampletext)
  200. set S(sample) $WS.sample
  201. grid $WS.sample -sticky news -padx 6 -pady 4
  202. grid rowconfigure $WS 0 -weight 1
  203. grid columnconfigure $WS 0 -weight 1
  204. grid propagate $WS 0
  205. grid $S(W).ok -in $bbox -sticky new -pady {0 2}
  206. grid $S(W).cancel -in $bbox -sticky new -pady 2
  207. if {$S(-command) ne ""} {
  208. grid $S(W).apply -in $bbox -sticky new -pady 2
  209. }
  210. grid columnconfigure $bbox 0 -weight 1
  211. grid $WE.strike -sticky w -padx 10
  212. grid $WE.under -sticky w -padx 10 -pady {0 30}
  213. grid columnconfigure $WE 1 -weight 1
  214. grid $S(W).font x $S(W).style x $S(W).size x -in $outer -sticky w
  215. grid $S(W).efont x $S(W).estyle x $S(W).esize x $bbox -in $outer -sticky ew
  216. grid $S(W).lfonts x $S(W).lstyles x $S(W).lsizes x ^ -in $outer -sticky news
  217. grid $WE x $WS - - x ^ -in $outer -sticky news -pady {15 30}
  218. grid configure $bbox -sticky n
  219. grid columnconfigure $outer {1 3 5} -minsize $minsize(gap)
  220. grid columnconfigure $outer {0 2 4} -weight 1
  221. grid columnconfigure $outer 0 -minsize $minsize(fonts)
  222. grid columnconfigure $outer 2 -minsize $minsize(styles)
  223. grid columnconfigure $outer 4 -minsize $minsize(sizes)
  224. grid columnconfigure $outer 6 -minsize $minsize(bbox)
  225. grid $outer -sticky news
  226. grid rowconfigure $S(W) 0 -weight 1
  227. grid columnconfigure $S(W) 0 -weight 1
  228. Init $S(-font)
  229. trace add variable [namespace which -variable S](size) \
  230. write [namespace code [list Tracer]]
  231. trace add variable [namespace which -variable S](style) \
  232. write [namespace code [list Tracer]]
  233. trace add variable [namespace which -variable S](font) \
  234. write [namespace code [list Tracer]]
  235. } else {
  236. Init $S(-font)
  237. }
  238. return
  239. }
  240. # ::tk::fontchooser::Done --
  241. #
  242. # Handles teardown of the dialog, calling -command if needed
  243. #
  244. # Arguments:
  245. # ok true if user pressed OK
  246. #
  247. proc ::tk::::fontchooser::Done {ok} {
  248. variable S
  249. if {! $ok} {
  250. set S(result) ""
  251. }
  252. trace vdelete S(size) w [namespace code [list Tracer]]
  253. trace vdelete S(style) w [namespace code [list Tracer]]
  254. trace vdelete S(font) w [namespace code [list Tracer]]
  255. destroy $S(W)
  256. if {$ok && $S(-command) ne ""} {
  257. uplevel #0 $S(-command) [list $S(result)]
  258. }
  259. }
  260. # ::tk::fontchooser::Apply --
  261. #
  262. # Call the -command procedure appending the current font
  263. # Errors are reported via the background error mechanism
  264. #
  265. proc ::tk::fontchooser::Apply {} {
  266. variable S
  267. if {$S(-command) ne ""} {
  268. if {[catch {uplevel #0 $S(-command) [list $S(result)]} err]} {
  269. ::bgerror $err
  270. }
  271. }
  272. event generate $S(-parent) <<TkFontchooserFontChanged>>
  273. }
  274. # ::tk::fontchooser::Init --
  275. #
  276. # Initializes dialog to a default font
  277. #
  278. # Arguments:
  279. # defaultFont font to use as the default
  280. #
  281. proc ::tk::fontchooser::Init {{defaultFont ""}} {
  282. variable S
  283. if {$S(first) || $defaultFont ne ""} {
  284. if {$defaultFont eq ""} {
  285. set defaultFont [[entry .___e] cget -font]
  286. destroy .___e
  287. }
  288. array set F [font actual $defaultFont]
  289. set S(font) $F(-family)
  290. set S(size) $F(-size)
  291. set S(strike) $F(-overstrike)
  292. set S(under) $F(-underline)
  293. set S(style) "Regular"
  294. if {$F(-weight) eq "bold" && $F(-slant) eq "italic"} {
  295. set S(style) "Bold Italic"
  296. } elseif {$F(-weight) eq "bold"} {
  297. set S(style) "Bold"
  298. } elseif {$F(-slant) eq "italic"} {
  299. set S(style) "Italic"
  300. }
  301. set S(first) 0
  302. }
  303. Tracer a b c
  304. Update
  305. }
  306. # ::tk::fontchooser::Click --
  307. #
  308. # Handles all button clicks, updating the appropriate widgets
  309. #
  310. # Arguments:
  311. # who which widget got pressed
  312. #
  313. proc ::tk::fontchooser::Click {who} {
  314. variable S
  315. if {$who eq "font"} {
  316. set S(font) [$S(W).lfonts get [$S(W).lfonts curselection]]
  317. } elseif {$who eq "style"} {
  318. set S(style) [$S(W).lstyles get [$S(W).lstyles curselection]]
  319. } elseif {$who eq "size"} {
  320. set S(size) [$S(W).lsizes get [$S(W).lsizes curselection]]
  321. }
  322. Update
  323. }
  324. # ::tk::fontchooser::Tracer --
  325. #
  326. # Handles traces on key variables, updating the appropriate widgets
  327. #
  328. # Arguments:
  329. # standard trace arguments (not used)
  330. #
  331. proc ::tk::fontchooser::Tracer {var1 var2 op} {
  332. variable S
  333. set bad 0
  334. set nstate normal
  335. # Make selection in each listbox
  336. foreach var {font style size} {
  337. set value [string tolower $S($var)]
  338. $S(W).l${var}s selection clear 0 end
  339. set n [lsearch -exact $S(${var}s,lcase) $value]
  340. $S(W).l${var}s selection set $n
  341. if {$n != -1} {
  342. set S($var) [lindex $S(${var}s) $n]
  343. $S(W).e$var icursor end
  344. $S(W).e$var selection clear
  345. } else { ;# No match, try prefix
  346. # Size is weird: valid numbers are legal but don't display
  347. # unless in the font size list
  348. set n [lsearch -glob $S(${var}s,lcase) "$value*"]
  349. set bad 1
  350. if {$var ne "size" || ! [string is double -strict $value]} {
  351. set nstate disabled
  352. }
  353. }
  354. $S(W).l${var}s see $n
  355. }
  356. if {!$bad} { Update }
  357. $S(W).ok configure -state $nstate
  358. }
  359. # ::tk::fontchooser::Update --
  360. #
  361. # Shows a sample of the currently selected font
  362. #
  363. proc ::tk::fontchooser::Update {} {
  364. variable S
  365. set S(result) [list $S(font) $S(size)]
  366. if {$S(style) eq "Bold"} { lappend S(result) bold }
  367. if {$S(style) eq "Italic"} { lappend S(result) italic }
  368. if {$S(style) eq "Bold Italic"} { lappend S(result) bold italic}
  369. if {$S(strike)} { lappend S(result) overstrike}
  370. if {$S(under)} { lappend S(result) underline}
  371. $S(sample) configure -font $S(result)
  372. }
  373. # ::tk::fontchooser::Visibility --
  374. #
  375. # Notify the parent when the dialog visibility changes
  376. #
  377. proc ::tk::fontchooser::Visibility {w visible} {
  378. variable S
  379. if {$w eq $S(W)} {
  380. event generate $S(-parent) <<TkFontchooserVisibility>>
  381. }
  382. }
  383. # ::tk::fontchooser::ttk_listbox --
  384. #
  385. # Create a properly themed scrolled listbox.
  386. # This is exactly right on XP but may need adjusting on other platforms.
  387. #
  388. proc ::tk::fontchooser::ttk_slistbox {w args} {
  389. set f [ttk::frame $w -style FontchooserFrame -padding 2]
  390. if {[catch {
  391. listbox $f.list -relief flat -highlightthickness 0 -borderwidth 0 {*}$args
  392. ttk::scrollbar $f.vs -command [list $f.list yview]
  393. $f.list configure -yscrollcommand [list $f.vs set]
  394. grid $f.list $f.vs -sticky news
  395. grid rowconfigure $f 0 -weight 1
  396. grid columnconfigure $f 0 -weight 1
  397. interp hide {} $w
  398. interp alias {} $w {} $f.list
  399. } err opt]} {
  400. destroy $f
  401. return -options $opt $err
  402. }
  403. return $w
  404. }