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
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
25 #include <fontconfig/fontconfig.h>
27 /* workaround for older fontconfig, that doesn't define these constants */
28 #ifndef FC_WEIGHT_NORMAL
30 # define FC_WEIGHT_THIN 10
31 # define FC_WEIGHT_EXTRALIGHT 40
32 # define FC_WEIGHT_ULTRALIGHT FC_WEIGHT_EXTRALIGHT
33 # define FC_WEIGHT_REGULAR 80
34 # define FC_WEIGHT_NORMAL FC_WEIGHT_REGULAR
35 # define FC_WEIGHT_SEMIBOLD FC_WEIGHT_DEMIBOLD
36 # define FC_WEIGHT_EXTRABOLD 205
37 # define FC_WEIGHT_ULTRABOLD FC_WEIGHT_EXTRABOLD
38 # define FC_WEIGHT_HEAVY FC_WEIGHT_BLACK
40 # define FC_WIDTH "width"
41 # define FC_WIDTH_ULTRACONDENSED 50
42 # define FC_WIDTH_EXTRACONDENSED 63
43 # define FC_WIDTH_CONDENSED 75
44 # define FC_WIDTH_SEMICONDENSED 87
45 # define FC_WIDTH_NORMAL 100
46 # define FC_WIDTH_SEMIEXPANDED 113
47 # define FC_WIDTH_EXPANDED 125
48 # define FC_WIDTH_EXTRAEXPANDED 150
49 # define FC_WIDTH_ULTRAEXPANDED 200
52 #define SAMPLE_TEXT "The Lazy Fox Jumped Ipsum Foobar 1234 - 56789"
71 typedef struct _Panel
{
77 CallbackRec callbacks
;
81 WMPopUpButton
*optionP
;
93 #define ICON_FILE "fonts"
100 "WindowTitleFont", N_("Window Title")}, {
101 "MenuTitleFont", N_("Menu Title")}, {
102 "MenuTextFont", N_("Menu Text")}, {
103 "IconTitleFont", N_("Icon Title")}, {
104 "ClipTitleFont", N_("Clip Title")}, {
105 "LargeDisplayFont", N_("Desktop Caption")}, {
108 static char *standardSizes
[] = {
137 FC_WEIGHT_THIN
, "Thin"}, {
138 FC_WEIGHT_EXTRALIGHT
, "ExtraLight"}, {
139 FC_WEIGHT_LIGHT
, "Light"}, {
140 FC_WEIGHT_NORMAL
, "Normal"}, {
141 FC_WEIGHT_MEDIUM
, ""}, /*"medium"}, */
143 FC_WEIGHT_DEMIBOLD
, "DemiBold"}, {
144 FC_WEIGHT_BOLD
, "Bold"}, {
145 FC_WEIGHT_EXTRABOLD
, "ExtraBold"}, {
146 FC_WEIGHT_BLACK
, "Black"}, {
155 FC_SLANT_ROMAN
, ""}, /*"Roman"}, */
157 FC_SLANT_ITALIC
, "Italic"}, {
158 FC_SLANT_OBLIQUE
, "Oblique"}, {
167 FC_WIDTH_ULTRACONDENSED
, "UltraCondensed"}, {
168 FC_WIDTH_EXTRACONDENSED
, "ExtraCondensed"}, {
169 FC_WIDTH_CONDENSED
, "Condensed"}, {
170 FC_WIDTH_SEMICONDENSED
, "SemiCondensed"}, {
171 FC_WIDTH_NORMAL
, ""}, /*"normal"}, */
173 FC_WIDTH_SEMIEXPANDED
, "SemiExpanded"}, {
174 FC_WIDTH_EXPANDED
, "Expanded"}, {
175 FC_WIDTH_EXTRAEXPANDED
, "ExtraExpanded"}, {
176 FC_WIDTH_ULTRAEXPANDED
, "UltraExpanded"}, {
180 static int compare_family(const void *a
, const void *b
)
182 FontFamily
*fa
= (FontFamily
*) a
;
183 FontFamily
*fb
= (FontFamily
*) b
;
184 return strcmp(fa
->name
, fb
->name
);
187 static int compare_styles(const void *a
, const void *b
)
189 FontStyle
*sa
= (FontStyle
*) a
;
190 FontStyle
*sb
= (FontStyle
*) b
;
193 compare
= sa
->weight
- sb
->weight
;
196 compare
= sa
->slant
- sb
->slant
;
199 return (sa
->width
- sb
->width
);
202 static void lookup_available_fonts(_Panel
* panel
)
204 FcPattern
*pat
= FcPatternCreate();
209 os
= FcObjectSetBuild(FC_FAMILY
, FC_WEIGHT
, FC_WIDTH
, FC_SLANT
, NULL
);
211 fonts
= FcFontList(0, pat
, os
);
216 panel
->fonts
= wmalloc(sizeof(FontList
));
217 panel
->fonts
->familyn
= 0;
218 panel
->fonts
->families
= wmalloc(sizeof(FontFamily
) * fonts
->nfont
);
220 for (i
= 0; i
< fonts
->nfont
; i
++) {
222 int weight
, slant
, width
;
225 if (FcPatternGetString(fonts
->fonts
[i
], FC_FAMILY
, 0, (FcChar8
**) & name
) !=
229 if (FcPatternGetInteger(fonts
->fonts
[i
], FC_WEIGHT
, 0, &weight
) != FcResultMatch
)
230 weight
= FC_WEIGHT_MEDIUM
;
232 if (FcPatternGetInteger(fonts
->fonts
[i
], FC_WIDTH
, 0, &width
) != FcResultMatch
)
233 width
= FC_WIDTH_NORMAL
;
235 if (FcPatternGetInteger(fonts
->fonts
[i
], FC_SLANT
, 0, &slant
) != FcResultMatch
)
236 slant
= FC_SLANT_ROMAN
;
239 for (j
= 0; j
< panel
->fonts
->familyn
&& found
< 0; j
++)
240 if (strcasecmp(panel
->fonts
->families
[j
].name
, name
) == 0)
244 panel
->fonts
->families
[panel
->fonts
->familyn
++].name
= wstrdup(name
);
245 family
= panel
->fonts
->families
+ panel
->fonts
->familyn
- 1;
247 family
->styles
= NULL
;
249 family
= panel
->fonts
->families
+ found
;
252 family
->styles
= wrealloc(family
->styles
, sizeof(FontStyle
) * family
->stylen
);
253 family
->styles
[family
->stylen
- 1].weight
= weight
;
254 family
->styles
[family
->stylen
- 1].slant
= slant
;
255 family
->styles
[family
->stylen
- 1].width
= width
;
257 qsort(panel
->fonts
->families
, panel
->fonts
->familyn
, sizeof(FontFamily
), compare_family
);
259 for (i
= 0; i
< panel
->fonts
->familyn
; i
++) {
260 qsort(panel
->fonts
->families
[i
].styles
, panel
->fonts
->families
[i
].stylen
,
261 sizeof(FontStyle
), compare_styles
);
264 FcFontSetDestroy(fonts
);
267 FcObjectSetDestroy(os
);
269 FcPatternDestroy(pat
);
271 panel
->fonts
->families
[panel
->fonts
->familyn
++].name
= wstrdup("sans serif");
272 family
= panel
->fonts
->families
+ panel
->fonts
->familyn
- 1;
273 family
->styles
= wmalloc(sizeof(FontStyle
) * 2);
275 family
->styles
[0].weight
= FC_WEIGHT_MEDIUM
;
276 family
->styles
[0].slant
= FC_SLANT_ROMAN
;
277 family
->styles
[0].width
= FC_WIDTH_NORMAL
;
278 family
->styles
[1].weight
= FC_WEIGHT_BOLD
;
279 family
->styles
[1].slant
= FC_SLANT_ROMAN
;
280 family
->styles
[1].width
= FC_WIDTH_NORMAL
;
282 panel
->fonts
->families
[panel
->fonts
->familyn
++].name
= wstrdup("serif");
283 family
= panel
->fonts
->families
+ panel
->fonts
->familyn
- 1;
284 family
->styles
= wmalloc(sizeof(FontStyle
) * 2);
286 family
->styles
[0].weight
= FC_WEIGHT_MEDIUM
;
287 family
->styles
[0].slant
= FC_SLANT_ROMAN
;
288 family
->styles
[0].width
= FC_WIDTH_NORMAL
;
289 family
->styles
[1].weight
= FC_WEIGHT_BOLD
;
290 family
->styles
[1].slant
= FC_SLANT_ROMAN
;
291 family
->styles
[1].width
= FC_WIDTH_NORMAL
;
294 static char *getSelectedFont(_Panel
* panel
, FcChar8
* curfont
)
301 pat
= FcNameParse(curfont
);
303 pat
= FcPatternCreate();
305 item
= WMGetListSelectedItem(panel
->familyL
);
307 FcPatternDel(pat
, FC_FAMILY
);
308 FcPatternAddString(pat
, FC_FAMILY
, (FcChar8
*) item
->text
);
311 item
= WMGetListSelectedItem(panel
->styleL
);
313 FontStyle
*style
= (FontStyle
*) item
->clientData
;
315 FcPatternDel(pat
, FC_WEIGHT
);
316 FcPatternAddInteger(pat
, FC_WEIGHT
, style
->weight
);
318 FcPatternDel(pat
, FC_WIDTH
);
319 FcPatternAddInteger(pat
, FC_WIDTH
, style
->width
);
321 FcPatternDel(pat
, FC_SLANT
);
322 FcPatternAddInteger(pat
, FC_SLANT
, style
->slant
);
325 item
= WMGetListSelectedItem(panel
->sizeL
);
327 FcPatternDel(pat
, FC_PIXEL_SIZE
);
328 FcPatternAddDouble(pat
, FC_PIXEL_SIZE
, atoi(item
->text
));
331 name
= (char *)FcNameUnparse(pat
);
332 FcPatternDestroy(pat
);
337 static void updateSampleFont(_Panel
* panel
)
339 WMMenuItem
*item
= WMGetPopUpButtonMenuItem(panel
->optionP
,
340 WMGetPopUpButtonSelectedItem(panel
->optionP
));
341 char *fn
= WMGetMenuItemRepresentedObject(item
);
344 WMFont
*font
= WMCreateFont(WMWidgetScreen(panel
->box
), fn
);
346 WMSetTextFieldFont(panel
->sampleT
, font
);
352 static void selectedFamily(WMWidget
* w
, void *data
)
354 _Panel
*panel
= (_Panel
*) data
;
356 FontStyle
*oldStyle
= NULL
;
359 item
= WMGetListSelectedItem(panel
->styleL
);
361 oldStyle
= (FontStyle
*) item
->clientData
;
363 item
= WMGetListSelectedItem(panel
->familyL
);
366 FontFamily
*family
= (FontFamily
*) item
->clientData
;
367 int i
, oldi
= 0, oldscore
= 0;
369 WMClearList(panel
->styleL
);
370 for (i
= 0; i
< family
->stylen
; i
++) {
372 char *weight
= "", *slant
= "", *width
= "";
375 for (j
= 0; fontWeights
[j
].name
; j
++)
376 if (fontWeights
[j
].weight
== family
->styles
[i
].weight
) {
377 weight
= fontWeights
[j
].name
;
380 for (j
= 0; fontWidths
[j
].name
; j
++)
381 if (fontWidths
[j
].width
== family
->styles
[i
].width
) {
382 width
= fontWidths
[j
].name
;
385 for (j
= 0; fontSlants
[j
].name
; j
++)
386 if (fontSlants
[j
].slant
== family
->styles
[i
].slant
) {
387 slant
= fontSlants
[j
].name
;
390 sprintf(buffer
, "%s%s%s%s%s",
391 weight
, *weight
? " " : "", slant
, (*slant
|| *weight
) ? " " : "", width
);
393 strcpy(buffer
, "Roman");
395 item
= WMAddListItem(panel
->styleL
, buffer
);
396 item
->clientData
= family
->styles
+ i
;
401 if (oldStyle
->width
== family
->styles
[i
].width
)
403 if (oldStyle
->weight
== family
->styles
[i
].weight
)
405 if (oldStyle
->slant
== family
->styles
[i
].slant
)
408 if (score
> oldscore
) {
414 WMSelectListItem(panel
->styleL
, oldi
);
417 int index
= WMGetPopUpButtonSelectedItem(panel
->optionP
);
418 WMMenuItem
*item
= WMGetPopUpButtonMenuItem(panel
->optionP
, index
);
422 ofont
= (FcChar8
*) WMGetMenuItemRepresentedObject(item
);
423 nfont
= getSelectedFont(panel
, ofont
);
426 WMSetMenuItemRepresentedObject(item
, nfont
);
428 updateSampleFont(panel
);
432 static void selected(WMWidget
* w
, void *data
)
434 _Panel
*panel
= (_Panel
*) data
;
435 int index
= WMGetPopUpButtonSelectedItem(panel
->optionP
);
436 WMMenuItem
*item
= WMGetPopUpButtonMenuItem(panel
->optionP
, index
);
440 ofont
= (FcChar8
*) WMGetMenuItemRepresentedObject(item
);
441 nfont
= getSelectedFont(panel
, ofont
);
445 WMSetMenuItemRepresentedObject(item
, nfont
);
447 updateSampleFont(panel
);
450 static void selectedOption(WMWidget
* w
, void *data
)
452 _Panel
*panel
= (_Panel
*) data
;
453 int index
= WMGetPopUpButtonSelectedItem(panel
->optionP
);
454 WMMenuItem
*item
= WMGetPopUpButtonMenuItem(panel
->optionP
, index
);
457 font
= (char *)WMGetMenuItemRepresentedObject(item
);
461 pat
= FcNameParse((FcChar8
*) font
);
464 int weight
, slant
, width
;
466 int distance
, closest
, found
;
469 FcDefaultSubstitute(pat
);
471 if (FcPatternGetString(pat
, FC_FAMILY
, 0, (FcChar8
**) & name
) != FcResultMatch
)
476 for (i
= 0; i
< WMGetListNumberOfRows(panel
->familyL
); i
++) {
477 WMListItem
*item
= WMGetListItem(panel
->familyL
, i
);
478 FontFamily
*family
= (FontFamily
*) item
->clientData
;
480 if (strcasecmp(family
->name
, name
) == 0) {
482 WMSelectListItem(panel
->familyL
, i
);
483 WMSetListPosition(panel
->familyL
, i
);
488 WMSelectListItem(panel
->familyL
, -1);
489 selectedFamily(panel
->familyL
, panel
);
492 if (FcPatternGetInteger(pat
, FC_WEIGHT
, 0, &weight
) != FcResultMatch
)
493 weight
= FC_WEIGHT_NORMAL
;
494 if (FcPatternGetInteger(pat
, FC_WIDTH
, 0, &width
) != FcResultMatch
)
495 width
= FC_WIDTH_NORMAL
;
496 if (FcPatternGetInteger(pat
, FC_SLANT
, 0, &slant
) != FcResultMatch
)
497 slant
= FC_SLANT_ROMAN
;
499 if (FcPatternGetDouble(pat
, FC_PIXEL_SIZE
, 0, &size
) != FcResultMatch
)
502 for (i
= 0, found
= 0, closest
= 0; i
< WMGetListNumberOfRows(panel
->styleL
); i
++) {
503 WMListItem
*item
= WMGetListItem(panel
->styleL
, i
);
504 FontStyle
*style
= (FontStyle
*) item
->clientData
;
506 distance
= ((abs(style
->weight
- weight
) << 16) +
507 (abs(style
->slant
- slant
) << 8) + (abs(style
->width
- width
)));
509 if (i
== 0 || distance
< closest
) {
513 break; /* perfect match */
517 WMSelectListItem(panel
->styleL
, found
);
518 WMSetListPosition(panel
->styleL
, found
);
520 for (i
= 0, found
= 0, closest
= 0; i
< WMGetListNumberOfRows(panel
->sizeL
); i
++) {
521 WMListItem
*item
= WMGetListItem(panel
->sizeL
, i
);
524 distance
= abs(size
- atoi(item
->text
));
526 if (i
== 0 || distance
< closest
) {
530 break; /* perfect match */
534 WMSelectListItem(panel
->sizeL
, found
);
535 WMSetListPosition(panel
->sizeL
, found
);
537 selected(NULL
, panel
);
539 wwarning("Can't parse font '%s'", font
);
542 updateSampleFont(panel
);
545 static WMLabel
*createListLabel(WMScreen
* scr
, WMWidget
* parent
, char *text
)
549 WMFont
*boldFont
= WMBoldSystemFontOfSize(scr
, 12);
551 label
= WMCreateLabel(parent
);
552 WMSetLabelFont(label
, boldFont
);
553 WMSetLabelText(label
, text
);
554 WMSetLabelRelief(label
, WRSunken
);
555 WMSetLabelTextAlignment(label
, WACenter
);
556 color
= WMDarkGrayColor(scr
);
557 WMSetWidgetBackgroundColor(label
, color
);
558 WMReleaseColor(color
);
559 color
= WMWhiteColor(scr
);
560 WMSetLabelTextColor(label
, color
);
561 WMReleaseColor(color
);
563 WMReleaseFont(boldFont
);
568 static void showData(_Panel
* panel
)
573 for (i
= 0; i
< WMGetPopUpButtonNumberOfItems(panel
->optionP
); i
++) {
576 item
= WMGetPopUpButtonMenuItem(panel
->optionP
, i
);
578 ofont
= WMGetMenuItemRepresentedObject(item
);
582 font
= GetStringForKey(fontOptions
[i
].option
);
584 font
= wstrdup(font
);
585 WMSetMenuItemRepresentedObject(item
, font
);
588 WMSetPopUpButtonSelectedItem(panel
->optionP
, 0);
589 selectedOption(panel
->optionP
, panel
);
592 static void storeData(_Panel
* panel
)
596 for (i
= 0; i
< WMGetPopUpButtonNumberOfItems(panel
->optionP
); i
++) {
599 item
= WMGetPopUpButtonMenuItem(panel
->optionP
, i
);
601 font
= WMGetMenuItemRepresentedObject(item
);
603 SetStringForKey(font
, fontOptions
[i
].option
);
608 static void createPanel(Panel
* p
)
610 _Panel
*panel
= (_Panel
*) p
;
611 WMScreen
*scr
= WMWidgetScreen(panel
->parent
);
616 lookup_available_fonts(panel
);
618 panel
->box
= WMCreateBox(panel
->parent
);
619 WMSetViewExpandsToParent(WMWidgetView(panel
->box
), 5, 2, 5, 5);
620 WMSetBoxHorizontal(panel
->box
, False
);
621 WMSetBoxBorderWidth(panel
->box
, 8);
622 WMMapWidget(panel
->box
);
624 hbox
= WMCreateBox(panel
->box
);
625 WMSetBoxHorizontal(hbox
, True
);
626 WMAddBoxSubview(panel
->box
, WMWidgetView(hbox
), False
, True
, 40, 22, 8);
628 vbox
= WMCreateBox(hbox
);
629 WMAddBoxSubview(hbox
, WMWidgetView(vbox
), False
, True
, 130, 0, 10);
630 WMSetBoxHorizontal(vbox
, False
);
631 panel
->optionP
= WMCreatePopUpButton(vbox
);
632 WMAddBoxSubviewAtEnd(vbox
, WMWidgetView(panel
->optionP
), False
, True
, 20, 0, 8);
633 for (i
= 0; fontOptions
[i
].option
; i
++) {
634 WMAddPopUpButtonItem(panel
->optionP
, _(fontOptions
[i
].label
));
636 WMSetPopUpButtonAction(panel
->optionP
, selectedOption
, panel
);
638 label
= WMCreateLabel(hbox
);
639 WMSetLabelText(label
, _("Sample Text"));
640 WMSetLabelTextAlignment(label
, WARight
);
641 WMAddBoxSubview(hbox
, WMWidgetView(label
), False
, True
, 80, 0, 2);
643 panel
->sampleT
= WMCreateTextField(hbox
);
644 WMSetViewExpandsToParent(WMWidgetView(panel
->sampleT
), 10, 18, 10, 10);
645 WMSetTextFieldText(panel
->sampleT
, SAMPLE_TEXT
);
646 WMAddBoxSubview(hbox
, WMWidgetView(panel
->sampleT
), True
, True
, 60, 0, 0);
648 hbox
= WMCreateBox(panel
->box
);
649 WMSetBoxHorizontal(hbox
, True
);
650 WMAddBoxSubview(panel
->box
, WMWidgetView(hbox
), True
, True
, 100, 0, 2);
652 vbox
= WMCreateBox(hbox
);
653 WMSetBoxHorizontal(vbox
, False
);
654 WMAddBoxSubview(hbox
, WMWidgetView(vbox
), False
, True
, 240, 20, 4);
656 label
= createListLabel(scr
, vbox
, _("Family"));
657 WMAddBoxSubview(vbox
, WMWidgetView(label
), False
, True
, 20, 0, 2);
660 panel
->familyL
= WMCreateList(vbox
);
661 WMAddBoxSubview(vbox
, WMWidgetView(panel
->familyL
), True
, True
, 0, 0, 0);
664 for (i
= 0; i
< panel
->fonts
->familyn
; i
++) {
665 item
= WMAddListItem(panel
->familyL
, panel
->fonts
->families
[i
].name
);
666 item
->clientData
= panel
->fonts
->families
+ i
;
669 WMAddListItem(panel
->familyL
, "sans serif");
671 WMSetListAction(panel
->familyL
, selectedFamily
, panel
);
673 vbox
= WMCreateBox(hbox
);
674 WMSetBoxHorizontal(vbox
, False
);
675 WMAddBoxSubview(hbox
, WMWidgetView(vbox
), True
, True
, 10, 0, 0);
678 WMBox
*box
= WMCreateBox(vbox
);
679 WMSetBoxHorizontal(box
, True
);
680 WMAddBoxSubview(vbox
, WMWidgetView(box
), False
, True
, 20, 0, 2);
682 label
= createListLabel(scr
, box
, _("Style"));
683 WMAddBoxSubview(box
, WMWidgetView(label
), True
, True
, 20, 0, 4);
685 label
= createListLabel(scr
, box
, _("Size"));
686 WMAddBoxSubview(box
, WMWidgetView(label
), False
, True
, 60, 0, 0);
688 box
= WMCreateBox(vbox
);
689 WMSetBoxHorizontal(box
, True
);
690 WMAddBoxSubview(vbox
, WMWidgetView(box
), True
, True
, 20, 0, 0);
692 panel
->styleL
= WMCreateList(box
);
693 WMAddBoxSubview(box
, WMWidgetView(panel
->styleL
), True
, True
, 0, 0, 4);
694 WMSetListAction(panel
->styleL
, selected
, panel
);
696 panel
->sizeL
= WMCreateList(box
);
697 WMAddBoxSubview(box
, WMWidgetView(panel
->sizeL
), False
, True
, 60, 0, 0);
698 for (i
= 0; standardSizes
[i
]; i
++) {
699 WMAddListItem(panel
->sizeL
, standardSizes
[i
]);
701 WMSetListAction(panel
->sizeL
, selected
, panel
);
704 WMMapSubwidgets(panel
->box
);
705 WMMapWidget(panel
->box
);
706 WMRealizeWidget(panel
->box
);
711 Panel
*InitFontSimple(WMScreen
* scr
, WMWidget
* parent
)
715 panel
= wmalloc(sizeof(_Panel
));
716 memset(panel
, 0, sizeof(_Panel
));
718 panel
->sectionName
= _("Font Configuration");
720 panel
->description
= _("Configure fonts for Window Maker titlebars, menus etc.");
722 panel
->parent
= parent
;
724 panel
->callbacks
.createWidgets
= createPanel
;
725 panel
->callbacks
.updateDomain
= storeData
;
727 AddSection(panel
, ICON_FILE
);