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,
26 #include <fontconfig/fontconfig.h>
29 /* workaround for older fontconfig, that doesn't define these constants */
30 #ifndef FC_WEIGHT_NORMAL
32 # define FC_WEIGHT_THIN 10
33 # define FC_WEIGHT_EXTRALIGHT 40
34 # define FC_WEIGHT_ULTRALIGHT FC_WEIGHT_EXTRALIGHT
35 # define FC_WEIGHT_REGULAR 80
36 # define FC_WEIGHT_NORMAL FC_WEIGHT_REGULAR
37 # define FC_WEIGHT_SEMIBOLD FC_WEIGHT_DEMIBOLD
38 # define FC_WEIGHT_EXTRABOLD 205
39 # define FC_WEIGHT_ULTRABOLD FC_WEIGHT_EXTRABOLD
40 # define FC_WEIGHT_HEAVY FC_WEIGHT_BLACK
42 # define FC_WIDTH "width"
43 # define FC_WIDTH_ULTRACONDENSED 50
44 # define FC_WIDTH_EXTRACONDENSED 63
45 # define FC_WIDTH_CONDENSED 75
46 # define FC_WIDTH_SEMICONDENSED 87
47 # define FC_WIDTH_NORMAL 100
48 # define FC_WIDTH_SEMIEXPANDED 113
49 # define FC_WIDTH_EXPANDED 125
50 # define FC_WIDTH_EXTRAEXPANDED 150
51 # define FC_WIDTH_ULTRAEXPANDED 200
55 #define SAMPLE_TEXT "The Lazy Fox Jumped Ipsum Foobar 1234 - 56789"
77 typedef struct _Panel
{
83 CallbackRec callbacks
;
87 WMPopUpButton
*optionP
;
100 #define ICON_FILE "fonts"
107 {"WindowTitleFont", N_("Window Title")},
108 {"MenuTitleFont", N_("Menu Title")},
109 {"MenuTextFont", N_("Menu Text")},
110 {"IconTitleFont", N_("Icon Title")},
111 {"ClipTitleFont", N_("Clip Title")},
112 {"LargeDisplayFont", N_("Desktop Caption")},
117 static char *standardSizes
[]= {
146 {FC_WEIGHT_THIN
, "Thin"},
147 {FC_WEIGHT_EXTRALIGHT
, "ExtraLight"},
148 {FC_WEIGHT_LIGHT
, "Light"},
149 {FC_WEIGHT_NORMAL
, "Normal"},
150 {FC_WEIGHT_MEDIUM
, ""}, /*"medium"},*/
151 {FC_WEIGHT_DEMIBOLD
, "DemiBold"},
152 {FC_WEIGHT_BOLD
, "Bold"},
153 {FC_WEIGHT_EXTRABOLD
, "ExtraBold"},
154 {FC_WEIGHT_BLACK
, "Black"},
162 {FC_SLANT_ROMAN
, ""}, /*"Roman"},*/
163 {FC_SLANT_ITALIC
, "Italic"},
164 {FC_SLANT_OBLIQUE
, "Oblique"},
172 {FC_WIDTH_ULTRACONDENSED
, "UltraCondensed"},
173 {FC_WIDTH_EXTRACONDENSED
, "ExtraCondensed"},
174 {FC_WIDTH_CONDENSED
, "Condensed"},
175 {FC_WIDTH_SEMICONDENSED
, "SemiCondensed"},
176 {FC_WIDTH_NORMAL
, ""}, /*"normal"},*/
177 {FC_WIDTH_SEMIEXPANDED
, "SemiExpanded"},
178 {FC_WIDTH_EXPANDED
, "Expanded"},
179 {FC_WIDTH_EXTRAEXPANDED
, "ExtraExpanded"},
180 {FC_WIDTH_ULTRAEXPANDED
, "UltraExpanded"},
188 static int compare_family(const void *a
, const void *b
)
190 FontFamily
*fa
= (FontFamily
*)a
;
191 FontFamily
*fb
= (FontFamily
*)b
;
192 return strcmp(fa
->name
, fb
->name
);
196 static int compare_styles(const void *a
, const void *b
)
198 FontStyle
*sa
= (FontStyle
*)a
;
199 FontStyle
*sb
= (FontStyle
*)b
;
202 compare
= sa
->weight
- sb
->weight
;
205 compare
= sa
->slant
- sb
->slant
;
208 return (sa
->width
- sb
->width
);
213 lookup_available_fonts(_Panel
*panel
)
215 FcPattern
*pat
= FcPatternCreate();
220 os
= FcObjectSetBuild(FC_FAMILY
, FC_WEIGHT
, FC_WIDTH
, FC_SLANT
, NULL
);
222 fonts
= FcFontList(0, pat
, os
);
228 panel
->fonts
= wmalloc(sizeof(FontList
));
229 panel
->fonts
->familyn
= 0;
230 panel
->fonts
->families
= wmalloc(sizeof(FontFamily
)*fonts
->nfont
);
232 for (i
= 0; i
< fonts
->nfont
; i
++)
235 int weight
, slant
, width
;
238 if (FcPatternGetString(fonts
->fonts
[i
], FC_FAMILY
, 0, (FcChar8
**)&name
) != FcResultMatch
)
241 if (FcPatternGetInteger(fonts
->fonts
[i
], FC_WEIGHT
, 0, &weight
) != FcResultMatch
)
242 weight
= FC_WEIGHT_MEDIUM
;
244 if (FcPatternGetInteger(fonts
->fonts
[i
], FC_WIDTH
, 0, &width
) != FcResultMatch
)
245 width
= FC_WIDTH_NORMAL
;
247 if (FcPatternGetInteger(fonts
->fonts
[i
], FC_SLANT
, 0, &slant
) != FcResultMatch
)
248 slant
= FC_SLANT_ROMAN
;
251 for (j
= 0; j
< panel
->fonts
->familyn
&& found
<0; j
++)
252 if (strcasecmp(panel
->fonts
->families
[j
].name
, name
)==0)
257 panel
->fonts
->families
[panel
->fonts
->familyn
++].name
= wstrdup(name
);
258 family
= panel
->fonts
->families
+ panel
->fonts
->familyn
-1;
260 family
->styles
= NULL
;
263 family
= panel
->fonts
->families
+found
;
266 family
->styles
= wrealloc(family
->styles
, sizeof(FontStyle
)*family
->stylen
);
267 family
->styles
[family
->stylen
-1].weight
= weight
;
268 family
->styles
[family
->stylen
-1].slant
= slant
;
269 family
->styles
[family
->stylen
-1].width
= width
;
271 qsort(panel
->fonts
->families
, panel
->fonts
->familyn
, sizeof(FontFamily
),
274 for (i
=0; i
< panel
->fonts
->familyn
; i
++)
276 qsort(panel
->fonts
->families
[i
].styles
, panel
->fonts
->families
[i
].stylen
,
277 sizeof(FontStyle
), compare_styles
);
280 FcFontSetDestroy(fonts
);
283 FcObjectSetDestroy(os
);
285 FcPatternDestroy(pat
);
287 panel
->fonts
->families
[panel
->fonts
->familyn
++].name
= wstrdup("sans serif");
288 family
= panel
->fonts
->families
+ panel
->fonts
->familyn
-1;
289 family
->styles
= wmalloc(sizeof(FontStyle
)*2);
291 family
->styles
[0].weight
= FC_WEIGHT_MEDIUM
;
292 family
->styles
[0].slant
= FC_SLANT_ROMAN
;
293 family
->styles
[0].width
= FC_WIDTH_NORMAL
;
294 family
->styles
[1].weight
= FC_WEIGHT_BOLD
;
295 family
->styles
[1].slant
= FC_SLANT_ROMAN
;
296 family
->styles
[1].width
= FC_WIDTH_NORMAL
;
298 panel
->fonts
->families
[panel
->fonts
->familyn
++].name
= wstrdup("serif");
299 family
= panel
->fonts
->families
+ panel
->fonts
->familyn
-1;
300 family
->styles
= wmalloc(sizeof(FontStyle
)*2);
302 family
->styles
[0].weight
= FC_WEIGHT_MEDIUM
;
303 family
->styles
[0].slant
= FC_SLANT_ROMAN
;
304 family
->styles
[0].width
= FC_WIDTH_NORMAL
;
305 family
->styles
[1].weight
= FC_WEIGHT_BOLD
;
306 family
->styles
[1].slant
= FC_SLANT_ROMAN
;
307 family
->styles
[1].width
= FC_WIDTH_NORMAL
;
312 getSelectedFont(_Panel
*panel
, FcChar8
*curfont
)
315 FcPattern
*pat
= FcNameParse(curfont
);
318 item
= WMGetListSelectedItem(panel
->familyL
);
321 FcPatternDel(pat
, FC_FAMILY
);
322 FcPatternAddString(pat
, FC_FAMILY
, (FcChar8
*)item
->text
);
325 item
= WMGetListSelectedItem(panel
->styleL
);
328 FontStyle
*style
= (FontStyle
*)item
->clientData
;
330 FcPatternDel(pat
, FC_WEIGHT
);
331 FcPatternAddInteger(pat
, FC_WEIGHT
, style
->weight
);
333 FcPatternDel(pat
, FC_WIDTH
);
334 FcPatternAddInteger(pat
, FC_WIDTH
, style
->width
);
336 FcPatternDel(pat
, FC_SLANT
);
337 FcPatternAddInteger(pat
, FC_SLANT
, style
->slant
);
340 item
= WMGetListSelectedItem(panel
->sizeL
);
343 FcPatternDel(pat
, FC_PIXEL_SIZE
);
344 FcPatternAddDouble(pat
, FC_PIXEL_SIZE
, atoi(item
->text
));
347 name
= (char*)FcNameUnparse(pat
);
348 FcPatternDestroy(pat
);
356 updateSampleFont(_Panel
*panel
)
358 WMMenuItem
*item
= WMGetPopUpButtonMenuItem(panel
->optionP
,
359 WMGetPopUpButtonSelectedItem(panel
->optionP
));
360 char *fn
= WMGetMenuItemRepresentedObject(item
);
361 WMFont
*font
= WMCreateFont(WMWidgetScreen(panel
->box
), fn
);
365 WMSetTextFieldFont(panel
->sampleT
, font
);
374 selectedFamily(WMWidget
*w
, void *data
)
376 _Panel
*panel
= (_Panel
*)data
;
378 FontStyle
*oldStyle
= NULL
;
381 item
= WMGetListSelectedItem(panel
->styleL
);
383 oldStyle
= (FontStyle
*)item
->clientData
;
385 item
= WMGetListSelectedItem(panel
->familyL
);
389 FontFamily
*family
= (FontFamily
*)item
->clientData
;
390 int i
, oldi
= 0, oldscore
= 0;
392 WMClearList(panel
->styleL
);
393 for (i
= 0; i
< family
->stylen
; i
++)
396 char *weight
= "", *slant
= "", *width
= "";
399 for (j
= 0; fontWeights
[j
].name
; j
++)
400 if (fontWeights
[j
].weight
== family
->styles
[i
].weight
)
402 weight
= fontWeights
[j
].name
;
405 for (j
= 0; fontWidths
[j
].name
; j
++)
406 if (fontWidths
[j
].width
== family
->styles
[i
].width
)
408 width
= fontWidths
[j
].name
;
411 for (j
= 0; fontSlants
[j
].name
; j
++)
412 if (fontSlants
[j
].slant
== family
->styles
[i
].slant
)
414 slant
= fontSlants
[j
].name
;
417 sprintf(buffer
, "%s%s%s%s%s",
418 weight
, *weight
?" ":"",
419 slant
, (*slant
|| *weight
)?" ":"",
422 strcpy(buffer
, "Roman");
424 item
= WMAddListItem(panel
->styleL
, buffer
);
425 item
->clientData
= family
->styles
+i
;
430 if (oldStyle
->width
== family
->styles
[i
].width
)
432 if (oldStyle
->weight
== family
->styles
[i
].weight
)
434 if (oldStyle
->slant
== family
->styles
[i
].slant
)
437 if (score
> oldscore
)
444 WMSelectListItem(panel
->styleL
, oldi
);
447 int index
= WMGetPopUpButtonSelectedItem(panel
->optionP
);
448 WMMenuItem
*item
= WMGetPopUpButtonMenuItem(panel
->optionP
, index
);
452 ofont
= (FcChar8
*)WMGetMenuItemRepresentedObject(item
);
453 nfont
= getSelectedFont(panel
, ofont
);
456 WMSetMenuItemRepresentedObject(item
, nfont
);
458 updateSampleFont(panel
);
464 selected(WMWidget
*w
, void *data
)
466 _Panel
*panel
= (_Panel
*)data
;
467 int index
= WMGetPopUpButtonSelectedItem(panel
->optionP
);
468 WMMenuItem
*item
= WMGetPopUpButtonMenuItem(panel
->optionP
, index
);
472 ofont
= (FcChar8
*)WMGetMenuItemRepresentedObject(item
);
473 nfont
= getSelectedFont(panel
, ofont
);
476 WMSetMenuItemRepresentedObject(item
, nfont
);
478 updateSampleFont(panel
);
483 selectedOption(WMWidget
*w
, void *data
)
485 _Panel
*panel
= (_Panel
*)data
;
486 int index
= WMGetPopUpButtonSelectedItem(panel
->optionP
);
487 WMMenuItem
*item
= WMGetPopUpButtonMenuItem(panel
->optionP
, index
);
490 font
= (char*)WMGetMenuItemRepresentedObject(item
);
495 pat
= FcNameParse((FcChar8
*)font
);
499 int weight
, slant
, width
;
501 int distance
, closest
, found
;
504 FcDefaultSubstitute(pat
);
506 if (FcPatternGetString(pat
, FC_FAMILY
, 0, (FcChar8
**)&name
) != FcResultMatch
)
511 for (i
= 0; i
< WMGetListNumberOfRows(panel
->familyL
); i
++)
513 WMListItem
*item
= WMGetListItem(panel
->familyL
, i
);
514 FontFamily
*family
= (FontFamily
*)item
->clientData
;
516 if (strcasecmp(family
->name
, name
)==0)
519 WMSelectListItem(panel
->familyL
, i
);
520 WMSetListPosition(panel
->familyL
, i
);
525 WMSelectListItem(panel
->familyL
, -1);
526 selectedFamily(panel
->familyL
, panel
);
529 if (FcPatternGetInteger(pat
, FC_WEIGHT
, 0, &weight
) != FcResultMatch
)
530 weight
= FC_WEIGHT_NORMAL
;
531 if (FcPatternGetInteger(pat
, FC_WIDTH
, 0, &width
) != FcResultMatch
)
532 width
= FC_WIDTH_NORMAL
;
533 if (FcPatternGetInteger(pat
, FC_SLANT
, 0, &slant
) != FcResultMatch
)
534 slant
= FC_SLANT_ROMAN
;
536 if (FcPatternGetDouble(pat
, FC_PIXEL_SIZE
, 0, &size
) != FcResultMatch
)
539 for (i
=0, found
=0, closest
=0; i
< WMGetListNumberOfRows(panel
->styleL
); i
++)
541 WMListItem
*item
= WMGetListItem(panel
->styleL
, i
);
542 FontStyle
*style
= (FontStyle
*)item
->clientData
;
544 distance
= ((abs(style
->weight
- weight
) << 16) +
545 (abs(style
->slant
- slant
) << 8) +
546 (abs(style
->width
- width
)));
548 if (i
==0 || distance
< closest
) {
552 break; /* perfect match */
556 WMSelectListItem(panel
->styleL
, found
);
557 WMSetListPosition(panel
->styleL
, found
);
559 for (i
=0, found
=0, closest
=0; i
< WMGetListNumberOfRows(panel
->sizeL
); i
++)
561 WMListItem
*item
= WMGetListItem(panel
->sizeL
, i
);
564 distance
= abs(size
- atoi(item
->text
));
566 if (i
==0 || distance
< closest
) {
570 break; /* perfect match */
574 WMSelectListItem(panel
->sizeL
, found
);
575 WMSetListPosition(panel
->sizeL
, found
);
577 selected(NULL
, panel
);
580 wwarning("Can't parse font '%s'", font
);
583 updateSampleFont(panel
);
588 createListLabel(WMScreen
*scr
, WMWidget
*parent
, char *text
)
592 WMFont
*boldFont
= WMBoldSystemFontOfSize(scr
, 12);
594 label
= WMCreateLabel(parent
);
595 WMSetLabelFont(label
, boldFont
);
596 WMSetLabelText(label
, text
);
597 WMSetLabelRelief(label
, WRSunken
);
598 WMSetLabelTextAlignment(label
, WACenter
);
599 color
= WMDarkGrayColor(scr
);
600 WMSetWidgetBackgroundColor(label
, color
);
601 WMReleaseColor(color
);
602 color
= WMWhiteColor(scr
);
603 WMSetLabelTextColor(label
, color
);
604 WMReleaseColor(color
);
606 WMReleaseFont(boldFont
);
613 showData(_Panel
*panel
)
618 for (i
= 0; i
< WMGetPopUpButtonNumberOfItems(panel
->optionP
); i
++)
622 item
= WMGetPopUpButtonMenuItem(panel
->optionP
, i
);
624 ofont
= WMGetMenuItemRepresentedObject(item
);
628 font
= GetStringForKey(fontOptions
[i
].option
);
631 WMSetMenuItemRepresentedObject(item
, font
);
634 WMSetPopUpButtonSelectedItem(panel
->optionP
, 0);
635 selectedOption(panel
->optionP
, panel
);
640 storeData(_Panel
*panel
)
644 for (i
= 0; i
< WMGetPopUpButtonNumberOfItems(panel
->optionP
); i
++)
648 item
= WMGetPopUpButtonMenuItem(panel
->optionP
, i
);
650 font
= WMGetMenuItemRepresentedObject(item
);
653 SetStringForKey(font
, fontOptions
[i
].option
);
660 createPanel(Panel
*p
)
662 _Panel
*panel
= (_Panel
*)p
;
663 WMScreen
*scr
= WMWidgetScreen(panel
->parent
);
668 lookup_available_fonts(panel
);
670 panel
->box
= WMCreateBox(panel
->parent
);
671 WMSetViewExpandsToParent(WMWidgetView(panel
->box
), 5, 2, 5, 5);
672 WMSetBoxHorizontal(panel
->box
, False
);
673 WMSetBoxBorderWidth(panel
->box
, 8);
674 WMMapWidget(panel
->box
);
676 hbox
= WMCreateBox(panel
->box
);
677 WMSetBoxHorizontal(hbox
, True
);
678 WMAddBoxSubview(panel
->box
, WMWidgetView(hbox
), False
, True
, 40, 22, 8);
680 vbox
= WMCreateBox(hbox
);
681 WMAddBoxSubview(hbox
, WMWidgetView(vbox
), False
, True
, 130, 0, 10);
682 WMSetBoxHorizontal(vbox
, False
);
683 panel
->optionP
= WMCreatePopUpButton(vbox
);
684 WMAddBoxSubviewAtEnd(vbox
, WMWidgetView(panel
->optionP
), False
, True
, 20, 0, 8);
685 for (i
= 0; fontOptions
[i
].option
; i
++)
687 WMAddPopUpButtonItem(panel
->optionP
, _(fontOptions
[i
].label
));
689 WMSetPopUpButtonAction(panel
->optionP
, selectedOption
, panel
);
691 label
= WMCreateLabel(hbox
);
692 WMSetLabelText(label
, _("Sample Text"));
693 WMSetLabelTextAlignment(label
, WARight
);
694 WMAddBoxSubview(hbox
, WMWidgetView(label
), False
, True
, 80, 0, 2);
696 panel
->sampleT
= WMCreateTextField(hbox
);
697 WMSetViewExpandsToParent(WMWidgetView(panel
->sampleT
), 10, 18, 10, 10);
698 WMSetTextFieldText(panel
->sampleT
, SAMPLE_TEXT
);
699 WMAddBoxSubview(hbox
, WMWidgetView(panel
->sampleT
), True
, True
, 60, 0, 0);
702 hbox
= WMCreateBox(panel
->box
);
703 WMSetBoxHorizontal(hbox
, True
);
704 WMAddBoxSubview(panel
->box
, WMWidgetView(hbox
), True
, True
, 100, 0, 2);
706 vbox
= WMCreateBox(hbox
);
707 WMSetBoxHorizontal(vbox
, False
);
708 WMAddBoxSubview(hbox
, WMWidgetView(vbox
), False
, True
, 240, 20, 4);
710 label
= createListLabel(scr
, vbox
, _("Family"));
711 WMAddBoxSubview(vbox
, WMWidgetView(label
), False
, True
, 20, 0, 2);
714 panel
->familyL
= WMCreateList(vbox
);
715 WMAddBoxSubview(vbox
, WMWidgetView(panel
->familyL
), True
, True
, 0, 0, 0);
719 for (i
= 0; i
< panel
->fonts
->familyn
; i
++)
721 item
= WMAddListItem(panel
->familyL
, panel
->fonts
->families
[i
].name
);
722 item
->clientData
= panel
->fonts
->families
+i
;
726 WMAddListItem(panel
->familyL
, "sans serif");
728 WMSetListAction(panel
->familyL
, selectedFamily
, panel
);
730 vbox
= WMCreateBox(hbox
);
731 WMSetBoxHorizontal(vbox
, False
);
732 WMAddBoxSubview(hbox
, WMWidgetView(vbox
), True
, True
, 10, 0, 0);
735 WMBox
*box
= WMCreateBox(vbox
);
736 WMSetBoxHorizontal(box
, True
);
737 WMAddBoxSubview(vbox
, WMWidgetView(box
), False
, True
, 20, 0, 2);
739 label
= createListLabel(scr
, box
, _("Style"));
740 WMAddBoxSubview(box
, WMWidgetView(label
), True
, True
, 20, 0, 4);
742 label
= createListLabel(scr
, box
, _("Size"));
743 WMAddBoxSubview(box
, WMWidgetView(label
), False
, True
, 60, 0, 0);
745 box
= WMCreateBox(vbox
);
746 WMSetBoxHorizontal(box
, True
);
747 WMAddBoxSubview(vbox
, WMWidgetView(box
), True
, True
, 20, 0, 0);
749 panel
->styleL
= WMCreateList(box
);
750 WMAddBoxSubview(box
, WMWidgetView(panel
->styleL
), True
, True
, 0, 0, 4);
751 WMSetListAction(panel
->styleL
, selected
, panel
);
753 panel
->sizeL
= WMCreateList(box
);
754 WMAddBoxSubview(box
, WMWidgetView(panel
->sizeL
), False
, True
, 60, 0, 0);
755 for (i
= 0; standardSizes
[i
]; i
++)
757 WMAddListItem(panel
->sizeL
, standardSizes
[i
]);
759 WMSetListAction(panel
->sizeL
, selected
, panel
);
763 WMMapSubwidgets(panel
->box
);
764 WMMapWidget(panel
->box
);
765 WMRealizeWidget(panel
->box
);
773 InitFontSimple(WMScreen
*scr
, WMWidget
*parent
)
777 panel
= wmalloc(sizeof(_Panel
));
778 memset(panel
, 0, sizeof(_Panel
));
780 panel
->sectionName
= _("Font Configuration");
782 panel
->description
= _("Configure fonts for Window Maker titlebars, menus etc.");
784 panel
->parent
= parent
;
786 panel
->callbacks
.createWidgets
= createPanel
;
787 panel
->callbacks
.updateDomain
= storeData
;
789 AddSection(panel
, ICON_FILE
);