fontchooser.tcl 15 KB

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