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 item
= WMGetListSelectedItem(panel
->styleL
);
360 oldStyle
= (FontStyle
*) item
->clientData
;
362 item
= WMGetListSelectedItem(panel
->familyL
);
365 FontFamily
*family
= (FontFamily
*) item
->clientData
;
366 int i
, oldi
= 0, oldscore
= 0;
368 WMClearList(panel
->styleL
);
369 for (i
= 0; i
< family
->stylen
; i
++) {
371 char *weight
= "", *slant
= "", *width
= "";
374 for (j
= 0; fontWeights
[j
].name
; j
++)
375 if (fontWeights
[j
].weight
== family
->styles
[i
].weight
) {
376 weight
= fontWeights
[j
].name
;
379 for (j
= 0; fontWidths
[j
].name
; j
++)
380 if (fontWidths
[j
].width
== family
->styles
[i
].width
) {
381 width
= fontWidths
[j
].name
;
384 for (j
= 0; fontSlants
[j
].name
; j
++)
385 if (fontSlants
[j
].slant
== family
->styles
[i
].slant
) {
386 slant
= fontSlants
[j
].name
;
389 sprintf(buffer
, "%s%s%s%s%s",
390 weight
, *weight
? " " : "", slant
, (*slant
|| *weight
) ? " " : "", width
);
392 strcpy(buffer
, "Roman");
394 item
= WMAddListItem(panel
->styleL
, buffer
);
395 item
->clientData
= family
->styles
+ i
;
400 if (oldStyle
->width
== family
->styles
[i
].width
)
402 if (oldStyle
->weight
== family
->styles
[i
].weight
)
404 if (oldStyle
->slant
== family
->styles
[i
].slant
)
407 if (score
> oldscore
) {
413 WMSelectListItem(panel
->styleL
, oldi
);
416 int index
= WMGetPopUpButtonSelectedItem(panel
->optionP
);
417 WMMenuItem
*item
= WMGetPopUpButtonMenuItem(panel
->optionP
, index
);
421 ofont
= (FcChar8
*) WMGetMenuItemRepresentedObject(item
);
422 nfont
= getSelectedFont(panel
, ofont
);
425 WMSetMenuItemRepresentedObject(item
, nfont
);
427 updateSampleFont(panel
);
431 static void selected(WMWidget
* w
, void *data
)
433 _Panel
*panel
= (_Panel
*) data
;
434 int index
= WMGetPopUpButtonSelectedItem(panel
->optionP
);
435 WMMenuItem
*item
= WMGetPopUpButtonMenuItem(panel
->optionP
, index
);
439 ofont
= (FcChar8
*) WMGetMenuItemRepresentedObject(item
);
440 nfont
= getSelectedFont(panel
, ofont
);
444 WMSetMenuItemRepresentedObject(item
, nfont
);
446 updateSampleFont(panel
);
449 static void selectedOption(WMWidget
* w
, void *data
)
451 _Panel
*panel
= (_Panel
*) data
;
452 int index
= WMGetPopUpButtonSelectedItem(panel
->optionP
);
453 WMMenuItem
*item
= WMGetPopUpButtonMenuItem(panel
->optionP
, index
);
456 font
= (char *)WMGetMenuItemRepresentedObject(item
);
460 pat
= FcNameParse((FcChar8
*) font
);
463 int weight
, slant
, width
;
465 int distance
, closest
, found
;
468 FcDefaultSubstitute(pat
);
470 if (FcPatternGetString(pat
, FC_FAMILY
, 0, (FcChar8
**) & name
) != FcResultMatch
)
475 for (i
= 0; i
< WMGetListNumberOfRows(panel
->familyL
); i
++) {
476 WMListItem
*item
= WMGetListItem(panel
->familyL
, i
);
477 FontFamily
*family
= (FontFamily
*) item
->clientData
;
479 if (strcasecmp(family
->name
, name
) == 0) {
481 WMSelectListItem(panel
->familyL
, i
);
482 WMSetListPosition(panel
->familyL
, i
);
487 WMSelectListItem(panel
->familyL
, -1);
488 selectedFamily(panel
->familyL
, panel
);
491 if (FcPatternGetInteger(pat
, FC_WEIGHT
, 0, &weight
) != FcResultMatch
)
492 weight
= FC_WEIGHT_NORMAL
;
493 if (FcPatternGetInteger(pat
, FC_WIDTH
, 0, &width
) != FcResultMatch
)
494 width
= FC_WIDTH_NORMAL
;
495 if (FcPatternGetInteger(pat
, FC_SLANT
, 0, &slant
) != FcResultMatch
)
496 slant
= FC_SLANT_ROMAN
;
498 if (FcPatternGetDouble(pat
, FC_PIXEL_SIZE
, 0, &size
) != FcResultMatch
)
501 for (i
= 0, found
= 0, closest
= 0; i
< WMGetListNumberOfRows(panel
->styleL
); i
++) {
502 WMListItem
*item
= WMGetListItem(panel
->styleL
, i
);
503 FontStyle
*style
= (FontStyle
*) item
->clientData
;
505 distance
= ((abs(style
->weight
- weight
) << 16) +
506 (abs(style
->slant
- slant
) << 8) + (abs(style
->width
- width
)));
508 if (i
== 0 || distance
< closest
) {
512 break; /* perfect match */
516 WMSelectListItem(panel
->styleL
, found
);
517 WMSetListPosition(panel
->styleL
, found
);
519 for (i
= 0, found
= 0, closest
= 0; i
< WMGetListNumberOfRows(panel
->sizeL
); i
++) {
520 WMListItem
*item
= WMGetListItem(panel
->sizeL
, i
);
523 distance
= abs(size
- atoi(item
->text
));
525 if (i
== 0 || distance
< closest
) {
529 break; /* perfect match */
533 WMSelectListItem(panel
->sizeL
, found
);
534 WMSetListPosition(panel
->sizeL
, found
);
536 selected(NULL
, panel
);
538 wwarning("Can't parse font '%s'", font
);
541 updateSampleFont(panel
);
544 static WMLabel
*createListLabel(WMScreen
* scr
, WMWidget
* parent
, char *text
)
548 WMFont
*boldFont
= WMBoldSystemFontOfSize(scr
, 12);
550 label
= WMCreateLabel(parent
);
551 WMSetLabelFont(label
, boldFont
);
552 WMSetLabelText(label
, text
);
553 WMSetLabelRelief(label
, WRSunken
);
554 WMSetLabelTextAlignment(label
, WACenter
);
555 color
= WMDarkGrayColor(scr
);
556 WMSetWidgetBackgroundColor(label
, color
);
557 WMReleaseColor(color
);
558 color
= WMWhiteColor(scr
);
559 WMSetLabelTextColor(label
, color
);
560 WMReleaseColor(color
);
562 WMReleaseFont(boldFont
);
567 static void showData(_Panel
* panel
)
572 for (i
= 0; i
< WMGetPopUpButtonNumberOfItems(panel
->optionP
); i
++) {
575 item
= WMGetPopUpButtonMenuItem(panel
->optionP
, i
);
577 ofont
= WMGetMenuItemRepresentedObject(item
);
581 font
= GetStringForKey(fontOptions
[i
].option
);
583 font
= wstrdup(font
);
584 WMSetMenuItemRepresentedObject(item
, font
);
587 WMSetPopUpButtonSelectedItem(panel
->optionP
, 0);
588 selectedOption(panel
->optionP
, panel
);
591 static void storeData(_Panel
* panel
)
595 for (i
= 0; i
< WMGetPopUpButtonNumberOfItems(panel
->optionP
); i
++) {
598 item
= WMGetPopUpButtonMenuItem(panel
->optionP
, i
);
600 font
= WMGetMenuItemRepresentedObject(item
);
602 SetStringForKey(font
, fontOptions
[i
].option
);
607 static void createPanel(Panel
* p
)
609 _Panel
*panel
= (_Panel
*) p
;
610 WMScreen
*scr
= WMWidgetScreen(panel
->parent
);
615 lookup_available_fonts(panel
);
617 panel
->box
= WMCreateBox(panel
->parent
);
618 WMSetViewExpandsToParent(WMWidgetView(panel
->box
), 5, 2, 5, 5);
619 WMSetBoxHorizontal(panel
->box
, False
);
620 WMSetBoxBorderWidth(panel
->box
, 8);
621 WMMapWidget(panel
->box
);
623 hbox
= WMCreateBox(panel
->box
);
624 WMSetBoxHorizontal(hbox
, True
);
625 WMAddBoxSubview(panel
->box
, WMWidgetView(hbox
), False
, True
, 40, 22, 8);
627 vbox
= WMCreateBox(hbox
);
628 WMAddBoxSubview(hbox
, WMWidgetView(vbox
), False
, True
, 130, 0, 10);
629 WMSetBoxHorizontal(vbox
, False
);
630 panel
->optionP
= WMCreatePopUpButton(vbox
);
631 WMAddBoxSubviewAtEnd(vbox
, WMWidgetView(panel
->optionP
), False
, True
, 20, 0, 8);
632 for (i
= 0; fontOptions
[i
].option
; i
++) {
633 WMAddPopUpButtonItem(panel
->optionP
, _(fontOptions
[i
].label
));
635 WMSetPopUpButtonAction(panel
->optionP
, selectedOption
, panel
);
637 label
= WMCreateLabel(hbox
);
638 WMSetLabelText(label
, _("Sample Text"));
639 WMSetLabelTextAlignment(label
, WARight
);
640 WMAddBoxSubview(hbox
, WMWidgetView(label
), False
, True
, 80, 0, 2);
642 panel
->sampleT
= WMCreateTextField(hbox
);
643 WMSetViewExpandsToParent(WMWidgetView(panel
->sampleT
), 10, 18, 10, 10);
644 WMSetTextFieldText(panel
->sampleT
, SAMPLE_TEXT
);
645 WMAddBoxSubview(hbox
, WMWidgetView(panel
->sampleT
), True
, True
, 60, 0, 0);
647 hbox
= WMCreateBox(panel
->box
);
648 WMSetBoxHorizontal(hbox
, True
);
649 WMAddBoxSubview(panel
->box
, WMWidgetView(hbox
), True
, True
, 100, 0, 2);
651 vbox
= WMCreateBox(hbox
);
652 WMSetBoxHorizontal(vbox
, False
);
653 WMAddBoxSubview(hbox
, WMWidgetView(vbox
), False
, True
, 240, 20, 4);
655 label
= createListLabel(scr
, vbox
, _("Family"));
656 WMAddBoxSubview(vbox
, WMWidgetView(label
), False
, True
, 20, 0, 2);
659 panel
->familyL
= WMCreateList(vbox
);
660 WMAddBoxSubview(vbox
, WMWidgetView(panel
->familyL
), True
, True
, 0, 0, 0);
663 for (i
= 0; i
< panel
->fonts
->familyn
; i
++) {
664 item
= WMAddListItem(panel
->familyL
, panel
->fonts
->families
[i
].name
);
665 item
->clientData
= panel
->fonts
->families
+ i
;
668 WMAddListItem(panel
->familyL
, "sans serif");
670 WMSetListAction(panel
->familyL
, selectedFamily
, panel
);
672 vbox
= WMCreateBox(hbox
);
673 WMSetBoxHorizontal(vbox
, False
);
674 WMAddBoxSubview(hbox
, WMWidgetView(vbox
), True
, True
, 10, 0, 0);
677 WMBox
*box
= WMCreateBox(vbox
);
678 WMSetBoxHorizontal(box
, True
);
679 WMAddBoxSubview(vbox
, WMWidgetView(box
), False
, True
, 20, 0, 2);
681 label
= createListLabel(scr
, box
, _("Style"));
682 WMAddBoxSubview(box
, WMWidgetView(label
), True
, True
, 20, 0, 4);
684 label
= createListLabel(scr
, box
, _("Size"));
685 WMAddBoxSubview(box
, WMWidgetView(label
), False
, True
, 60, 0, 0);
687 box
= WMCreateBox(vbox
);
688 WMSetBoxHorizontal(box
, True
);
689 WMAddBoxSubview(vbox
, WMWidgetView(box
), True
, True
, 20, 0, 0);
691 panel
->styleL
= WMCreateList(box
);
692 WMAddBoxSubview(box
, WMWidgetView(panel
->styleL
), True
, True
, 0, 0, 4);
693 WMSetListAction(panel
->styleL
, selected
, panel
);
695 panel
->sizeL
= WMCreateList(box
);
696 WMAddBoxSubview(box
, WMWidgetView(panel
->sizeL
), False
, True
, 60, 0, 0);
697 for (i
= 0; standardSizes
[i
]; i
++) {
698 WMAddListItem(panel
->sizeL
, standardSizes
[i
]);
700 WMSetListAction(panel
->sizeL
, selected
, panel
);
703 WMMapSubwidgets(panel
->box
);
704 WMMapWidget(panel
->box
);
705 WMRealizeWidget(panel
->box
);
710 Panel
*InitFontSimple(WMScreen
* scr
, WMWidget
* parent
)
714 panel
= wmalloc(sizeof(_Panel
));
715 memset(panel
, 0, sizeof(_Panel
));
717 panel
->sectionName
= _("Font Configuration");
719 panel
->description
= _("Configure fonts for Window Maker titlebars, menus etc.");
721 panel
->parent
= parent
;
723 panel
->callbacks
.createWidgets
= createPanel
;
724 panel
->callbacks
.updateDomain
= storeData
;
726 AddSection(panel
, ICON_FILE
);