1 /* FontSimple.c- simplified font configuration panel
3 * WPrefs - Window Maker Preferences Program
5 * Copyright (c) 1998-2004 Alfredo K. Kojima
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License along
18 * with this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24 #include <fontconfig/fontconfig.h>
26 /* workaround for older fontconfig, that doesn't define these constants */
27 #ifndef FC_WEIGHT_NORMAL
29 # define FC_WEIGHT_THIN 10
30 # define FC_WEIGHT_EXTRALIGHT 40
31 # define FC_WEIGHT_ULTRALIGHT FC_WEIGHT_EXTRALIGHT
32 # define FC_WEIGHT_REGULAR 80
33 # define FC_WEIGHT_NORMAL FC_WEIGHT_REGULAR
34 # define FC_WEIGHT_SEMIBOLD FC_WEIGHT_DEMIBOLD
35 # define FC_WEIGHT_EXTRABOLD 205
36 # define FC_WEIGHT_ULTRABOLD FC_WEIGHT_EXTRABOLD
37 # define FC_WEIGHT_HEAVY FC_WEIGHT_BLACK
39 # define FC_WIDTH "width"
40 # define FC_WIDTH_ULTRACONDENSED 50
41 # define FC_WIDTH_EXTRACONDENSED 63
42 # define FC_WIDTH_CONDENSED 75
43 # define FC_WIDTH_SEMICONDENSED 87
44 # define FC_WIDTH_NORMAL 100
45 # define FC_WIDTH_SEMIEXPANDED 113
46 # define FC_WIDTH_EXPANDED 125
47 # define FC_WIDTH_EXTRAEXPANDED 150
48 # define FC_WIDTH_ULTRAEXPANDED 200
51 #define SAMPLE_TEXT "The Lazy Fox Jumped Ipsum Foobar 1234 - 56789"
70 typedef struct _Panel
{
76 CallbackRec callbacks
;
80 WMPopUpButton
*optionP
;
92 #define ICON_FILE "fonts"
99 "WindowTitleFont", N_("Window Title")}, {
100 "MenuTitleFont", N_("Menu Title")}, {
101 "MenuTextFont", N_("Menu Text")}, {
102 "IconTitleFont", N_("Icon Title")}, {
103 "ClipTitleFont", N_("Clip Title")}, {
104 "LargeDisplayFont", N_("Desktop Caption")}, {
107 static char *standardSizes
[] = {
136 FC_WEIGHT_THIN
, "Thin"}, {
137 FC_WEIGHT_EXTRALIGHT
, "ExtraLight"}, {
138 FC_WEIGHT_LIGHT
, "Light"}, {
139 FC_WEIGHT_NORMAL
, "Normal"}, {
140 FC_WEIGHT_MEDIUM
, ""}, /*"medium"}, */
142 FC_WEIGHT_DEMIBOLD
, "DemiBold"}, {
143 FC_WEIGHT_BOLD
, "Bold"}, {
144 FC_WEIGHT_EXTRABOLD
, "ExtraBold"}, {
145 FC_WEIGHT_BLACK
, "Black"}, {
154 FC_SLANT_ROMAN
, ""}, /*"Roman"}, */
156 FC_SLANT_ITALIC
, "Italic"}, {
157 FC_SLANT_OBLIQUE
, "Oblique"}, {
166 FC_WIDTH_ULTRACONDENSED
, "UltraCondensed"}, {
167 FC_WIDTH_EXTRACONDENSED
, "ExtraCondensed"}, {
168 FC_WIDTH_CONDENSED
, "Condensed"}, {
169 FC_WIDTH_SEMICONDENSED
, "SemiCondensed"}, {
170 FC_WIDTH_NORMAL
, ""}, /*"normal"}, */
172 FC_WIDTH_SEMIEXPANDED
, "SemiExpanded"}, {
173 FC_WIDTH_EXPANDED
, "Expanded"}, {
174 FC_WIDTH_EXTRAEXPANDED
, "ExtraExpanded"}, {
175 FC_WIDTH_ULTRAEXPANDED
, "UltraExpanded"}, {
179 static int compare_family(const void *a
, const void *b
)
181 FontFamily
*fa
= (FontFamily
*) a
;
182 FontFamily
*fb
= (FontFamily
*) b
;
183 return strcmp(fa
->name
, fb
->name
);
186 static int compare_styles(const void *a
, const void *b
)
188 FontStyle
*sa
= (FontStyle
*) a
;
189 FontStyle
*sb
= (FontStyle
*) b
;
192 compare
= sa
->weight
- sb
->weight
;
195 compare
= sa
->slant
- sb
->slant
;
198 return (sa
->width
- sb
->width
);
201 static void lookup_available_fonts(_Panel
* panel
)
203 FcPattern
*pat
= FcPatternCreate();
208 os
= FcObjectSetBuild(FC_FAMILY
, FC_WEIGHT
, FC_WIDTH
, FC_SLANT
, NULL
);
210 fonts
= FcFontList(0, pat
, os
);
215 panel
->fonts
= wmalloc(sizeof(FontList
));
216 panel
->fonts
->familyn
= 0;
217 panel
->fonts
->families
= wmalloc(sizeof(FontFamily
) * fonts
->nfont
);
219 for (i
= 0; i
< fonts
->nfont
; i
++) {
221 int weight
, slant
, width
;
224 if (FcPatternGetString(fonts
->fonts
[i
], FC_FAMILY
, 0, (FcChar8
**) & name
) !=
228 if (FcPatternGetInteger(fonts
->fonts
[i
], FC_WEIGHT
, 0, &weight
) != FcResultMatch
)
229 weight
= FC_WEIGHT_MEDIUM
;
231 if (FcPatternGetInteger(fonts
->fonts
[i
], FC_WIDTH
, 0, &width
) != FcResultMatch
)
232 width
= FC_WIDTH_NORMAL
;
234 if (FcPatternGetInteger(fonts
->fonts
[i
], FC_SLANT
, 0, &slant
) != FcResultMatch
)
235 slant
= FC_SLANT_ROMAN
;
238 for (j
= 0; j
< panel
->fonts
->familyn
&& found
< 0; j
++)
239 if (strcasecmp(panel
->fonts
->families
[j
].name
, name
) == 0)
243 panel
->fonts
->families
[panel
->fonts
->familyn
++].name
= wstrdup(name
);
244 family
= panel
->fonts
->families
+ panel
->fonts
->familyn
- 1;
246 family
->styles
= NULL
;
248 family
= panel
->fonts
->families
+ found
;
251 family
->styles
= wrealloc(family
->styles
, sizeof(FontStyle
) * family
->stylen
);
252 family
->styles
[family
->stylen
- 1].weight
= weight
;
253 family
->styles
[family
->stylen
- 1].slant
= slant
;
254 family
->styles
[family
->stylen
- 1].width
= width
;
256 qsort(panel
->fonts
->families
, panel
->fonts
->familyn
, sizeof(FontFamily
), compare_family
);
258 for (i
= 0; i
< panel
->fonts
->familyn
; i
++) {
259 qsort(panel
->fonts
->families
[i
].styles
, panel
->fonts
->families
[i
].stylen
,
260 sizeof(FontStyle
), compare_styles
);
263 FcFontSetDestroy(fonts
);
266 FcObjectSetDestroy(os
);
268 FcPatternDestroy(pat
);
270 panel
->fonts
->families
[panel
->fonts
->familyn
++].name
= wstrdup("sans serif");
271 family
= panel
->fonts
->families
+ panel
->fonts
->familyn
- 1;
272 family
->styles
= wmalloc(sizeof(FontStyle
) * 2);
274 family
->styles
[0].weight
= FC_WEIGHT_MEDIUM
;
275 family
->styles
[0].slant
= FC_SLANT_ROMAN
;
276 family
->styles
[0].width
= FC_WIDTH_NORMAL
;
277 family
->styles
[1].weight
= FC_WEIGHT_BOLD
;
278 family
->styles
[1].slant
= FC_SLANT_ROMAN
;
279 family
->styles
[1].width
= FC_WIDTH_NORMAL
;
281 panel
->fonts
->families
[panel
->fonts
->familyn
++].name
= wstrdup("serif");
282 family
= panel
->fonts
->families
+ panel
->fonts
->familyn
- 1;
283 family
->styles
= wmalloc(sizeof(FontStyle
) * 2);
285 family
->styles
[0].weight
= FC_WEIGHT_MEDIUM
;
286 family
->styles
[0].slant
= FC_SLANT_ROMAN
;
287 family
->styles
[0].width
= FC_WIDTH_NORMAL
;
288 family
->styles
[1].weight
= FC_WEIGHT_BOLD
;
289 family
->styles
[1].slant
= FC_SLANT_ROMAN
;
290 family
->styles
[1].width
= FC_WIDTH_NORMAL
;
293 static char *getSelectedFont(_Panel
* panel
, FcChar8
* curfont
)
300 pat
= FcNameParse(curfont
);
302 pat
= FcPatternCreate();
304 item
= WMGetListSelectedItem(panel
->familyL
);
306 FcPatternDel(pat
, FC_FAMILY
);
307 FcPatternAddString(pat
, FC_FAMILY
, (FcChar8
*) item
->text
);
310 item
= WMGetListSelectedItem(panel
->styleL
);
312 FontStyle
*style
= (FontStyle
*) item
->clientData
;
314 FcPatternDel(pat
, FC_WEIGHT
);
315 FcPatternAddInteger(pat
, FC_WEIGHT
, style
->weight
);
317 FcPatternDel(pat
, FC_WIDTH
);
318 FcPatternAddInteger(pat
, FC_WIDTH
, style
->width
);
320 FcPatternDel(pat
, FC_SLANT
);
321 FcPatternAddInteger(pat
, FC_SLANT
, style
->slant
);
324 item
= WMGetListSelectedItem(panel
->sizeL
);
326 FcPatternDel(pat
, FC_PIXEL_SIZE
);
327 FcPatternAddDouble(pat
, FC_PIXEL_SIZE
, atoi(item
->text
));
330 name
= (char *)FcNameUnparse(pat
);
331 FcPatternDestroy(pat
);
336 static void updateSampleFont(_Panel
* panel
)
338 WMMenuItem
*item
= WMGetPopUpButtonMenuItem(panel
->optionP
,
339 WMGetPopUpButtonSelectedItem(panel
->optionP
));
340 char *fn
= WMGetMenuItemRepresentedObject(item
);
343 WMFont
*font
= WMCreateFont(WMWidgetScreen(panel
->box
), fn
);
345 WMSetTextFieldFont(panel
->sampleT
, font
);
351 static void selectedFamily(WMWidget
* w
, void *data
)
353 _Panel
*panel
= (_Panel
*) data
;
355 FontStyle
*oldStyle
= NULL
;
358 /* Parameter not used, but tell the compiler that it is ok */
361 item
= WMGetListSelectedItem(panel
->styleL
);
363 oldStyle
= (FontStyle
*) item
->clientData
;
365 item
= WMGetListSelectedItem(panel
->familyL
);
368 FontFamily
*family
= (FontFamily
*) item
->clientData
;
369 int i
, oldi
= 0, oldscore
= 0;
371 WMClearList(panel
->styleL
);
372 for (i
= 0; i
< family
->stylen
; i
++) {
374 char *weight
= "", *slant
= "", *width
= "";
377 for (j
= 0; fontWeights
[j
].name
; j
++)
378 if (fontWeights
[j
].weight
== family
->styles
[i
].weight
) {
379 weight
= fontWeights
[j
].name
;
382 for (j
= 0; fontWidths
[j
].name
; j
++)
383 if (fontWidths
[j
].width
== family
->styles
[i
].width
) {
384 width
= fontWidths
[j
].name
;
387 for (j
= 0; fontSlants
[j
].name
; j
++)
388 if (fontSlants
[j
].slant
== family
->styles
[i
].slant
) {
389 slant
= fontSlants
[j
].name
;
392 sprintf(buffer
, "%s%s%s%s%s",
393 weight
, *weight
? " " : "", slant
, (*slant
|| *weight
) ? " " : "", width
);
395 strcpy(buffer
, "Roman");
397 item
= WMAddListItem(panel
->styleL
, buffer
);
398 item
->clientData
= family
->styles
+ i
;
403 if (oldStyle
->width
== family
->styles
[i
].width
)
405 if (oldStyle
->weight
== family
->styles
[i
].weight
)
407 if (oldStyle
->slant
== family
->styles
[i
].slant
)
410 if (score
> oldscore
) {
416 WMSelectListItem(panel
->styleL
, oldi
);
419 int index
= WMGetPopUpButtonSelectedItem(panel
->optionP
);
420 WMMenuItem
*item
= WMGetPopUpButtonMenuItem(panel
->optionP
, index
);
424 ofont
= (FcChar8
*) WMGetMenuItemRepresentedObject(item
);
425 nfont
= getSelectedFont(panel
, ofont
);
428 WMSetMenuItemRepresentedObject(item
, nfont
);
430 updateSampleFont(panel
);
434 static void selected(WMWidget
* w
, void *data
)
436 _Panel
*panel
= (_Panel
*) data
;
437 int index
= WMGetPopUpButtonSelectedItem(panel
->optionP
);
438 WMMenuItem
*item
= WMGetPopUpButtonMenuItem(panel
->optionP
, index
);
442 /* Parameter not used, but tell the compiler that it is ok */
445 ofont
= (FcChar8
*) WMGetMenuItemRepresentedObject(item
);
446 nfont
= getSelectedFont(panel
, ofont
);
450 WMSetMenuItemRepresentedObject(item
, nfont
);
452 updateSampleFont(panel
);
455 static void selectedOption(WMWidget
* w
, void *data
)
457 _Panel
*panel
= (_Panel
*) data
;
458 int index
= WMGetPopUpButtonSelectedItem(panel
->optionP
);
459 WMMenuItem
*item
= WMGetPopUpButtonMenuItem(panel
->optionP
, index
);
462 /* Parameter not used, but tell the compiler that it is ok */
465 font
= (char *)WMGetMenuItemRepresentedObject(item
);
469 pat
= FcNameParse((FcChar8
*) font
);
472 int weight
, slant
, width
;
474 int distance
, closest
, found
;
477 FcDefaultSubstitute(pat
);
479 if (FcPatternGetString(pat
, FC_FAMILY
, 0, (FcChar8
**) & name
) != FcResultMatch
)
484 for (i
= 0; i
< WMGetListNumberOfRows(panel
->familyL
); i
++) {
485 WMListItem
*item
= WMGetListItem(panel
->familyL
, i
);
486 FontFamily
*family
= (FontFamily
*) item
->clientData
;
488 if (strcasecmp(family
->name
, name
) == 0) {
490 WMSelectListItem(panel
->familyL
, i
);
491 WMSetListPosition(panel
->familyL
, i
);
496 WMSelectListItem(panel
->familyL
, -1);
497 selectedFamily(panel
->familyL
, panel
);
500 if (FcPatternGetInteger(pat
, FC_WEIGHT
, 0, &weight
) != FcResultMatch
)
501 weight
= FC_WEIGHT_NORMAL
;
502 if (FcPatternGetInteger(pat
, FC_WIDTH
, 0, &width
) != FcResultMatch
)
503 width
= FC_WIDTH_NORMAL
;
504 if (FcPatternGetInteger(pat
, FC_SLANT
, 0, &slant
) != FcResultMatch
)
505 slant
= FC_SLANT_ROMAN
;
507 if (FcPatternGetDouble(pat
, FC_PIXEL_SIZE
, 0, &size
) != FcResultMatch
)
510 for (i
= 0, found
= 0, closest
= 0; i
< WMGetListNumberOfRows(panel
->styleL
); i
++) {
511 WMListItem
*item
= WMGetListItem(panel
->styleL
, i
);
512 FontStyle
*style
= (FontStyle
*) item
->clientData
;
514 distance
= ((abs(style
->weight
- weight
) << 16) +
515 (abs(style
->slant
- slant
) << 8) + (abs(style
->width
- width
)));
517 if (i
== 0 || distance
< closest
) {
521 break; /* perfect match */
525 WMSelectListItem(panel
->styleL
, found
);
526 WMSetListPosition(panel
->styleL
, found
);
528 for (i
= 0, found
= 0, closest
= 0; i
< WMGetListNumberOfRows(panel
->sizeL
); i
++) {
529 WMListItem
*item
= WMGetListItem(panel
->sizeL
, i
);
532 distance
= abs(size
- atoi(item
->text
));
534 if (i
== 0 || distance
< closest
) {
538 break; /* perfect match */
542 WMSelectListItem(panel
->sizeL
, found
);
543 WMSetListPosition(panel
->sizeL
, found
);
545 selected(NULL
, panel
);
547 wwarning("Can't parse font '%s'", font
);
550 updateSampleFont(panel
);
553 static WMLabel
*createListLabel(WMScreen
* scr
, WMWidget
* parent
, const char *text
)
557 WMFont
*boldFont
= WMBoldSystemFontOfSize(scr
, 12);
559 label
= WMCreateLabel(parent
);
560 WMSetLabelFont(label
, boldFont
);
561 WMSetLabelText(label
, text
);
562 WMSetLabelRelief(label
, WRSunken
);
563 WMSetLabelTextAlignment(label
, WACenter
);
564 color
= WMDarkGrayColor(scr
);
565 WMSetWidgetBackgroundColor(label
, color
);
566 WMReleaseColor(color
);
567 color
= WMWhiteColor(scr
);
568 WMSetLabelTextColor(label
, color
);
569 WMReleaseColor(color
);
571 WMReleaseFont(boldFont
);
576 static void showData(_Panel
* panel
)
581 for (i
= 0; i
< WMGetPopUpButtonNumberOfItems(panel
->optionP
); i
++) {
584 item
= WMGetPopUpButtonMenuItem(panel
->optionP
, i
);
586 ofont
= WMGetMenuItemRepresentedObject(item
);
590 font
= GetStringForKey(fontOptions
[i
].option
);
592 font
= wstrdup(font
);
593 WMSetMenuItemRepresentedObject(item
, font
);
596 WMSetPopUpButtonSelectedItem(panel
->optionP
, 0);
597 selectedOption(panel
->optionP
, panel
);
600 static void storeData(_Panel
* panel
)
604 for (i
= 0; i
< WMGetPopUpButtonNumberOfItems(panel
->optionP
); i
++) {
607 item
= WMGetPopUpButtonMenuItem(panel
->optionP
, i
);
609 font
= WMGetMenuItemRepresentedObject(item
);
611 SetStringForKey(font
, fontOptions
[i
].option
);
616 static void createPanel(Panel
* p
)
618 _Panel
*panel
= (_Panel
*) p
;
619 WMScreen
*scr
= WMWidgetScreen(panel
->parent
);
624 lookup_available_fonts(panel
);
626 panel
->box
= WMCreateBox(panel
->parent
);
627 WMSetViewExpandsToParent(WMWidgetView(panel
->box
), 5, 2, 5, 5);
628 WMSetBoxHorizontal(panel
->box
, False
);
629 WMSetBoxBorderWidth(panel
->box
, 8);
630 WMMapWidget(panel
->box
);
632 hbox
= WMCreateBox(panel
->box
);
633 WMSetBoxHorizontal(hbox
, True
);
634 WMAddBoxSubview(panel
->box
, WMWidgetView(hbox
), False
, True
, 40, 22, 8);
636 vbox
= WMCreateBox(hbox
);
637 WMAddBoxSubview(hbox
, WMWidgetView(vbox
), False
, True
, 130, 0, 10);
638 WMSetBoxHorizontal(vbox
, False
);
639 panel
->optionP
= WMCreatePopUpButton(vbox
);
640 WMAddBoxSubviewAtEnd(vbox
, WMWidgetView(panel
->optionP
), False
, True
, 20, 0, 8);
641 for (i
= 0; fontOptions
[i
].option
; i
++) {
642 WMAddPopUpButtonItem(panel
->optionP
, _(fontOptions
[i
].label
));
644 WMSetPopUpButtonAction(panel
->optionP
, selectedOption
, panel
);
646 label
= WMCreateLabel(hbox
);
647 WMSetLabelText(label
, _("Sample Text"));
648 WMSetLabelTextAlignment(label
, WARight
);
649 WMAddBoxSubview(hbox
, WMWidgetView(label
), False
, True
, 80, 0, 2);
651 panel
->sampleT
= WMCreateTextField(hbox
);
652 WMSetViewExpandsToParent(WMWidgetView(panel
->sampleT
), 10, 18, 10, 10);
653 WMSetTextFieldText(panel
->sampleT
, SAMPLE_TEXT
);
654 WMAddBoxSubview(hbox
, WMWidgetView(panel
->sampleT
), True
, True
, 60, 0, 0);
656 hbox
= WMCreateBox(panel
->box
);
657 WMSetBoxHorizontal(hbox
, True
);
658 WMAddBoxSubview(panel
->box
, WMWidgetView(hbox
), True
, True
, 100, 0, 2);
660 vbox
= WMCreateBox(hbox
);
661 WMSetBoxHorizontal(vbox
, False
);
662 WMAddBoxSubview(hbox
, WMWidgetView(vbox
), False
, True
, 240, 20, 4);
664 label
= createListLabel(scr
, vbox
, _("Family"));
665 WMAddBoxSubview(vbox
, WMWidgetView(label
), False
, True
, 20, 0, 2);
668 panel
->familyL
= WMCreateList(vbox
);
669 WMAddBoxSubview(vbox
, WMWidgetView(panel
->familyL
), True
, True
, 0, 0, 0);
672 for (i
= 0; i
< panel
->fonts
->familyn
; i
++) {
673 item
= WMAddListItem(panel
->familyL
, panel
->fonts
->families
[i
].name
);
674 item
->clientData
= panel
->fonts
->families
+ i
;
677 WMAddListItem(panel
->familyL
, "sans serif");
679 WMSetListAction(panel
->familyL
, selectedFamily
, panel
);
681 vbox
= WMCreateBox(hbox
);
682 WMSetBoxHorizontal(vbox
, False
);
683 WMAddBoxSubview(hbox
, WMWidgetView(vbox
), True
, True
, 10, 0, 0);
686 WMBox
*box
= WMCreateBox(vbox
);
687 WMSetBoxHorizontal(box
, True
);
688 WMAddBoxSubview(vbox
, WMWidgetView(box
), False
, True
, 20, 0, 2);
690 label
= createListLabel(scr
, box
, _("Style"));
691 WMAddBoxSubview(box
, WMWidgetView(label
), True
, True
, 20, 0, 4);
693 label
= createListLabel(scr
, box
, _("Size"));
694 WMAddBoxSubview(box
, WMWidgetView(label
), False
, True
, 60, 0, 0);
696 box
= WMCreateBox(vbox
);
697 WMSetBoxHorizontal(box
, True
);
698 WMAddBoxSubview(vbox
, WMWidgetView(box
), True
, True
, 20, 0, 0);
700 panel
->styleL
= WMCreateList(box
);
701 WMAddBoxSubview(box
, WMWidgetView(panel
->styleL
), True
, True
, 0, 0, 4);
702 WMSetListAction(panel
->styleL
, selected
, panel
);
704 panel
->sizeL
= WMCreateList(box
);
705 WMAddBoxSubview(box
, WMWidgetView(panel
->sizeL
), False
, True
, 60, 0, 0);
706 for (i
= 0; standardSizes
[i
]; i
++) {
707 WMAddListItem(panel
->sizeL
, standardSizes
[i
]);
709 WMSetListAction(panel
->sizeL
, selected
, panel
);
712 WMMapSubwidgets(panel
->box
);
713 WMMapWidget(panel
->box
);
714 WMRealizeWidget(panel
->box
);
719 Panel
*InitFontSimple(WMWidget
*parent
)
723 panel
= wmalloc(sizeof(_Panel
));
725 panel
->sectionName
= _("Font Configuration");
727 panel
->description
= _("Configure fonts for Window Maker titlebars, menus etc.");
729 panel
->parent
= parent
;
731 panel
->callbacks
.createWidgets
= createPanel
;
732 panel
->callbacks
.updateDomain
= storeData
;
734 AddSection(panel
, ICON_FILE
);