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>
28 #define SAMPLE_TEXT "The Lazy Fox Jumped Ipsum Foobar 1234 - 56789"
50 typedef struct _Panel
{
56 CallbackRec callbacks
;
60 WMPopUpButton
*optionP
;
73 #define ICON_FILE "fonts"
80 {"WindowTitleFont", N_("Window Title")},
81 {"MenuTitleFont", N_("Menu Title")},
82 {"MenuTextFont", N_("Menu Text")},
83 {"IconTitleFont", N_("Icon Title")},
84 {"ClipTitleFont", N_("Clip Title")},
85 {"LargeDisplayFont", N_("Desktop Caption")},
90 static char *standardSizes
[]= {
119 {FC_WEIGHT_THIN
, "Thin"},
120 {FC_WEIGHT_EXTRALIGHT
, "ExtraLight"},
121 {FC_WEIGHT_LIGHT
, "Light"},
122 {FC_WEIGHT_NORMAL
, "Normal"},
123 {FC_WEIGHT_MEDIUM
, ""}, /*"medium"},*/
124 {FC_WEIGHT_DEMIBOLD
, "DemiBold"},
125 {FC_WEIGHT_BOLD
, "Bold"},
126 {FC_WEIGHT_EXTRABOLD
, "ExtraBold"},
127 {FC_WEIGHT_BLACK
, "Black"},
135 {FC_SLANT_ROMAN
, ""}, /*"Roman"},*/
136 {FC_SLANT_ITALIC
, "Italic"},
137 {FC_SLANT_OBLIQUE
, "Oblique"},
145 {FC_WIDTH_ULTRACONDENSED
, "UltraCondensed"},
146 {FC_WIDTH_EXTRACONDENSED
, "ExtraCondensed"},
147 {FC_WIDTH_CONDENSED
, "Condensed"},
148 {FC_WIDTH_SEMICONDENSED
, "SemiCondensed"},
149 {FC_WIDTH_NORMAL
, ""}, /*"normal"},*/
150 {FC_WIDTH_SEMIEXPANDED
, "SemiExpanded"},
151 {FC_WIDTH_EXPANDED
, "Expanded"},
152 {FC_WIDTH_EXTRAEXPANDED
, "ExtraExpanded"},
153 {FC_WIDTH_ULTRAEXPANDED
, "UltraExpanded"},
161 static int compare_family(const void *a
, const void *b
)
163 FontFamily
*fa
= (FontFamily
*)a
;
164 FontFamily
*fb
= (FontFamily
*)b
;
165 return strcasecmp(fa
->name
, fb
->name
);
170 lookup_available_fonts(_Panel
*panel
)
172 FcPattern
*pat
= FcPatternCreate();
177 os
= FcObjectSetBuild(FC_FAMILY
, FC_WEIGHT
, FC_WIDTH
, FC_SLANT
, NULL
);
179 fonts
= FcFontList(0, pat
, os
);
185 panel
->fonts
= wmalloc(sizeof(FontList
));
186 panel
->fonts
->familyn
= 0;
187 panel
->fonts
->families
= wmalloc(sizeof(FontFamily
)*fonts
->nfont
);
189 for (i
= 0; i
< fonts
->nfont
; i
++)
192 int weight
, slant
, width
;
195 if (FcPatternGetString(fonts
->fonts
[i
], FC_FAMILY
, 0, &name
) != FcResultMatch
)
198 if (FcPatternGetInteger(fonts
->fonts
[i
], FC_WEIGHT
, 0, &weight
) != FcResultMatch
)
199 weight
= FC_WEIGHT_MEDIUM
;
201 if (FcPatternGetInteger(fonts
->fonts
[i
], FC_WIDTH
, 0, &width
) != FcResultMatch
)
202 width
= FC_WIDTH_NORMAL
;
204 if (FcPatternGetInteger(fonts
->fonts
[i
], FC_SLANT
, 0, &slant
) != FcResultMatch
)
205 slant
= FC_SLANT_ROMAN
;
208 for (j
= 0; j
< panel
->fonts
->familyn
&& found
<0; j
++)
209 if (strcmp(panel
->fonts
->families
[j
].name
, name
)==0)
214 panel
->fonts
->families
[panel
->fonts
->familyn
++].name
= wstrdup(name
);
215 family
= panel
->fonts
->families
+ panel
->fonts
->familyn
-1;
217 family
->styles
= NULL
;
220 family
= panel
->fonts
->families
+found
;
223 family
->styles
= wrealloc(family
->styles
, sizeof(FontStyle
)*family
->stylen
);
224 family
->styles
[family
->stylen
-1].weight
= weight
;
225 family
->styles
[family
->stylen
-1].slant
= slant
;
226 family
->styles
[family
->stylen
-1].width
= width
;
228 qsort(panel
->fonts
->families
, panel
->fonts
->familyn
, sizeof(FontFamily
),
231 FcFontSetDestroy(fonts
);
234 FcObjectSetDestroy(os
);
236 FcPatternDestroy(pat
);
238 panel
->fonts
->families
[panel
->fonts
->familyn
++].name
= wstrdup("sans-serif");
239 family
= panel
->fonts
->families
+ panel
->fonts
->familyn
-1;
240 family
->styles
= wmalloc(sizeof(FontStyle
)*2);
242 family
->styles
[0].weight
= FC_WEIGHT_MEDIUM
;
243 family
->styles
[0].slant
= FC_SLANT_ROMAN
;
244 family
->styles
[0].width
= FC_WIDTH_NORMAL
;
245 family
->styles
[1].weight
= FC_WEIGHT_BOLD
;
246 family
->styles
[1].slant
= FC_SLANT_ROMAN
;
247 family
->styles
[1].width
= FC_WIDTH_NORMAL
;
249 panel
->fonts
->families
[panel
->fonts
->familyn
++].name
= wstrdup("sans");
250 family
= panel
->fonts
->families
+ panel
->fonts
->familyn
-1;
251 family
->styles
= wmalloc(sizeof(FontStyle
)*2);
253 family
->styles
[0].weight
= FC_WEIGHT_MEDIUM
;
254 family
->styles
[0].slant
= FC_SLANT_ROMAN
;
255 family
->styles
[0].width
= FC_WIDTH_NORMAL
;
256 family
->styles
[1].weight
= FC_WEIGHT_BOLD
;
257 family
->styles
[1].slant
= FC_SLANT_ROMAN
;
258 family
->styles
[1].width
= FC_WIDTH_NORMAL
;
260 panel
->fonts
->families
[panel
->fonts
->familyn
++].name
= wstrdup("serif");
261 family
= panel
->fonts
->families
+ panel
->fonts
->familyn
-1;
262 family
->styles
= wmalloc(sizeof(FontStyle
)*2);
264 family
->styles
[0].weight
= FC_WEIGHT_MEDIUM
;
265 family
->styles
[0].slant
= FC_SLANT_ROMAN
;
266 family
->styles
[0].width
= FC_WIDTH_NORMAL
;
267 family
->styles
[1].weight
= FC_WEIGHT_BOLD
;
268 family
->styles
[1].slant
= FC_SLANT_ROMAN
;
269 family
->styles
[1].width
= FC_WIDTH_NORMAL
;
274 getSelectedFont(_Panel
*panel
, char *curfont
)
277 FcPattern
*pat
= FcNameParse(curfont
);
280 item
= WMGetListSelectedItem(panel
->familyL
);
283 FcPatternDel(pat
, FC_FAMILY
);
284 FcPatternAddString(pat
, FC_FAMILY
, item
->text
);
287 item
= WMGetListSelectedItem(panel
->styleL
);
290 FontStyle
*style
= (FontStyle
*)item
->clientData
;
292 FcPatternDel(pat
, FC_WEIGHT
);
293 FcPatternAddInteger(pat
, FC_WEIGHT
, style
->weight
);
295 FcPatternDel(pat
, FC_WIDTH
);
296 FcPatternAddInteger(pat
, FC_WIDTH
, style
->width
);
298 FcPatternDel(pat
, FC_SLANT
);
299 FcPatternAddInteger(pat
, FC_SLANT
, style
->slant
);
302 item
= WMGetListSelectedItem(panel
->sizeL
);
305 FcPatternDel(pat
, FC_PIXEL_SIZE
);
306 FcPatternAddDouble(pat
, FC_PIXEL_SIZE
, atoi(item
->text
));
309 name
= FcNameUnparse(pat
);
310 FcPatternDestroy(pat
);
318 updateSampleFont(_Panel
*panel
)
320 WMMenuItem
*item
= WMGetPopUpButtonMenuItem(panel
->optionP
,
321 WMGetPopUpButtonSelectedItem(panel
->optionP
));
322 char *fn
= WMGetMenuItemRepresentedObject(item
);
323 WMFont
*font
= WMCreateFont(WMWidgetScreen(panel
->box
), fn
);
327 WMSetTextFieldFont(panel
->sampleT
, font
);
336 selectedFamily(WMWidget
*w
, void *data
)
338 _Panel
*panel
= (_Panel
*)data
;
339 WMListItem
*item
= WMGetListSelectedItem(panel
->familyL
);
344 FontFamily
*family
= (FontFamily
*)item
->clientData
;
347 WMClearList(panel
->styleL
);
348 for (i
= 0; i
< family
->stylen
; i
++)
351 char *weight
= "", *slant
= "", *width
= "";
354 for (j
= 0; fontWeights
[j
].name
; j
++)
355 if (fontWeights
[j
].weight
== family
->styles
[i
].weight
)
357 weight
= fontWeights
[j
].name
;
360 for (j
= 0; fontWidths
[j
].name
; j
++)
361 if (fontWidths
[j
].width
== family
->styles
[i
].width
)
363 width
= fontWidths
[j
].name
;
366 for (j
= 0; fontSlants
[j
].name
; j
++)
367 if (fontSlants
[j
].slant
== family
->styles
[i
].slant
)
369 slant
= fontSlants
[j
].name
;
372 sprintf(buffer
, "%s%s%s%s%s",
373 weight
, *weight
?" ":"",
374 slant
, (*slant
|| *weight
)?" ":"",
377 strcpy(buffer
, "Roman");
379 item
= WMAddListItem(panel
->styleL
, buffer
);
380 item
->clientData
= family
->styles
+i
;
382 WMSelectListItem(panel
->styleL
, 0);
385 int index
= WMGetPopUpButtonSelectedItem(panel
->optionP
);
386 WMMenuItem
*item
= WMGetPopUpButtonMenuItem(panel
->optionP
, index
);
389 ofont
= (char*)WMGetMenuItemRepresentedObject(item
);
391 nfont
= getSelectedFont(panel
, ofont
);
393 WMSetMenuItemRepresentedObject(item
, nfont
);
395 updateSampleFont(panel
);
401 selected(WMWidget
*w
, void *data
)
403 _Panel
*panel
= (_Panel
*)data
;
404 int index
= WMGetPopUpButtonSelectedItem(panel
->optionP
);
405 WMMenuItem
*item
= WMGetPopUpButtonMenuItem(panel
->optionP
, index
);
408 ofont
= (char*)WMGetMenuItemRepresentedObject(item
);
410 nfont
= getSelectedFont(panel
, ofont
);
412 WMSetMenuItemRepresentedObject(item
, nfont
);
414 updateSampleFont(panel
);
419 selectedOption(WMWidget
*w
, void *data
)
421 _Panel
*panel
= (_Panel
*)data
;
422 int index
= WMGetPopUpButtonSelectedItem(panel
->optionP
);
423 WMMenuItem
*item
= WMGetPopUpButtonMenuItem(panel
->optionP
, index
);
426 font
= (char*)WMGetMenuItemRepresentedObject(item
);
431 pat
= FcNameParse(font
);
435 int weight
, slant
, width
;
440 FcDefaultSubstitute(pat
);
442 if (FcPatternGetString(pat
, FC_FAMILY
, 0, &name
) != FcResultMatch
)
447 for (i
= 0; i
< WMGetListNumberOfRows(panel
->familyL
); i
++)
449 WMListItem
*item
= WMGetListItem(panel
->familyL
, i
);
450 FontFamily
*family
= (FontFamily
*)item
->clientData
;
452 if (strcasecmp(family
->name
, name
)==0)
455 WMSelectListItem(panel
->familyL
, i
);
456 WMSetListPosition(panel
->familyL
, i
);
461 WMSelectListItem(panel
->familyL
, -1);
462 selectedFamily(panel
->familyL
, panel
);
465 if (FcPatternGetInteger(pat
, FC_WEIGHT
, 0, &weight
) != FcResultMatch
)
466 weight
= FC_WEIGHT_NORMAL
;
467 if (FcPatternGetInteger(pat
, FC_WIDTH
, 0, &width
) != FcResultMatch
)
468 width
= FC_WIDTH_NORMAL
;
469 if (FcPatternGetInteger(pat
, FC_SLANT
, 0, &slant
) != FcResultMatch
)
470 slant
= FC_SLANT_ROMAN
;
472 if (FcPatternGetDouble(pat
, FC_PIXEL_SIZE
, 0, &size
) != FcResultMatch
)
476 for (i
= 0; i
< WMGetListNumberOfRows(panel
->styleL
); i
++)
478 WMListItem
*item
= WMGetListItem(panel
->styleL
, i
);
479 FontStyle
*style
= (FontStyle
*)item
->clientData
;
480 if (style
->weight
== weight
481 && style
->width
== width
482 && style
->slant
== slant
)
485 WMSelectListItem(panel
->styleL
, i
);
486 WMSetListPosition(panel
->styleL
, i
);
491 WMSelectListItem(panel
->styleL
, -1);
495 int closest
= 100000, index
= -1;
497 for (i
= 0; i
< WMGetListNumberOfRows(panel
->sizeL
); i
++)
499 WMListItem
*item
= WMGetListItem(panel
->sizeL
, i
);
502 tmp
= atoi(item
->text
);
503 if (abs(tmp
-size
) < abs(tmp
-closest
))
509 WMSelectListItem(panel
->sizeL
, index
);
510 WMSetListPosition(panel
->sizeL
, index
);
513 selected(NULL
, panel
);
516 wwarning("Can't parse font '%s'", font
);
519 updateSampleFont(panel
);
524 createListLabel(WMScreen
*scr
, WMWidget
*parent
, char *text
)
528 WMFont
*boldFont
= WMBoldSystemFontOfSize(scr
, 12);
530 label
= WMCreateLabel(parent
);
531 WMSetLabelFont(label
, boldFont
);
532 WMSetLabelText(label
, text
);
533 WMSetLabelRelief(label
, WRSunken
);
534 WMSetLabelTextAlignment(label
, WACenter
);
535 color
= WMDarkGrayColor(scr
);
536 WMSetWidgetBackgroundColor(label
, color
);
537 WMReleaseColor(color
);
538 color
= WMWhiteColor(scr
);
539 WMSetLabelTextColor(label
, color
);
540 WMReleaseColor(color
);
542 WMReleaseFont(boldFont
);
549 showData(_Panel
*panel
)
554 for (i
= 0; i
< WMGetPopUpButtonNumberOfItems(panel
->optionP
); i
++)
558 item
= WMGetPopUpButtonMenuItem(panel
->optionP
, i
);
560 ofont
= WMGetMenuItemRepresentedObject(item
);
564 font
= GetStringForKey(fontOptions
[i
].option
);
567 WMSetMenuItemRepresentedObject(item
, font
);
570 WMSetPopUpButtonSelectedItem(panel
->optionP
, 0);
571 selectedOption(panel
->optionP
, panel
);
576 storeData(_Panel
*panel
)
580 for (i
= 0; i
< WMGetPopUpButtonNumberOfItems(panel
->optionP
); i
++)
584 item
= WMGetPopUpButtonMenuItem(panel
->optionP
, i
);
586 font
= WMGetMenuItemRepresentedObject(item
);
589 SetStringForKey(font
, fontOptions
[i
].option
);
596 createPanel(Panel
*p
)
598 _Panel
*panel
= (_Panel
*)p
;
599 WMScreen
*scr
= WMWidgetScreen(panel
->parent
);
604 lookup_available_fonts(panel
);
606 panel
->box
= WMCreateBox(panel
->parent
);
607 WMSetViewExpandsToParent(WMWidgetView(panel
->box
), 5, 8, 5, 8);
608 WMSetBoxHorizontal(panel
->box
, False
);
609 WMSetBoxBorderWidth(panel
->box
, 8);
610 WMMapWidget(panel
->box
);
612 hbox
= WMCreateBox(panel
->box
);
613 WMSetBoxHorizontal(hbox
, True
);
614 WMAddBoxSubview(panel
->box
, WMWidgetView(hbox
), False
, True
, 22, 22, 8);
616 label
= WMCreateLabel(hbox
);
617 WMAddBoxSubview(hbox
, WMWidgetView(label
), False
, True
, 150, 0, 10);
618 WMSetLabelText(label
, _("Choose Font For"));
619 WMSetLabelTextAlignment(label
, WARight
);
621 panel
->optionP
= WMCreatePopUpButton(hbox
);
622 WMAddBoxSubview(hbox
, WMWidgetView(panel
->optionP
), False
, True
, 200, 0, 10);
623 for (i
= 0; fontOptions
[i
].option
; i
++)
625 WMAddPopUpButtonItem(panel
->optionP
, _(fontOptions
[i
].label
));
627 WMSetPopUpButtonAction(panel
->optionP
, selectedOption
, panel
);
629 hbox
= WMCreateBox(panel
->box
);
630 WMSetBoxHorizontal(hbox
, True
);
631 WMAddBoxSubview(panel
->box
, WMWidgetView(hbox
), False
, True
, 100, 0, 2);
635 vbox
= WMCreateBox(hbox
);
636 WMSetBoxHorizontal(vbox
, False
);
637 WMAddBoxSubview(hbox
, WMWidgetView(vbox
), False
, True
, 240, 20, 2);
639 label
= createListLabel(scr
, vbox
, _("Family"));
640 WMAddBoxSubview(vbox
, WMWidgetView(label
), False
, True
, 20, 0, 2);
643 panel
->familyL
= WMCreateList(vbox
);
644 WMAddBoxSubview(vbox
, WMWidgetView(panel
->familyL
), True
, True
, 0, 0, 4);
648 for (i
= 0; i
< panel
->fonts
->familyn
; i
++)
650 item
= WMAddListItem(panel
->familyL
, panel
->fonts
->families
[i
].name
);
651 item
->clientData
= panel
->fonts
->families
+i
;
655 WMAddListItem(panel
->familyL
, "sans");
657 WMSetListAction(panel
->familyL
, selectedFamily
, panel
);
660 vbox
= WMCreateBox(hbox
);
661 WMSetBoxHorizontal(vbox
, False
);
662 WMAddBoxSubview(hbox
, WMWidgetView(vbox
), True
, True
, 60, 0, 2);
664 label
= createListLabel(scr
, vbox
, _("Style"));
665 WMAddBoxSubview(vbox
, WMWidgetView(label
), False
, True
, 20, 0, 2);
667 panel
->styleL
= WMCreateList(vbox
);
668 WMAddBoxSubview(vbox
, WMWidgetView(panel
->styleL
), True
, True
, 0, 0, 4);
669 WMSetListAction(panel
->styleL
, selected
, panel
);
673 vbox
= WMCreateBox(hbox
);
674 WMSetBoxHorizontal(vbox
, False
);
675 WMAddBoxSubview(hbox
, WMWidgetView(vbox
), False
, True
, 70, 0, 2);
677 label
= createListLabel(scr
, vbox
, _("Size"));
678 WMAddBoxSubview(vbox
, WMWidgetView(label
), False
, True
, 20, 0, 0);
681 panel
->sizeL
= WMCreateList(vbox
);
682 WMAddBoxSubview(vbox
, WMWidgetView(panel
->sizeL
), True
, True
, 0, 0, 0);
683 for (i
= 0; standardSizes
[i
]; i
++)
685 WMAddListItem(panel
->sizeL
, standardSizes
[i
]);
687 WMSetListAction(panel
->sizeL
, selected
, panel
);
690 WMFrame
*frame
= WMCreateFrame(panel
->box
);
691 WMSetFrameTitle(frame
, _("Sample"));
693 WMAddBoxSubview(panel
->box
, WMWidgetView(frame
), True
, True
, 50, 0, 0);
695 panel
->sampleT
= WMCreateTextField(frame
);
696 WMSetViewExpandsToParent(WMWidgetView(panel
->sampleT
), 10, 18, 10, 10);
697 WMSetTextFieldText(panel
->sampleT
, SAMPLE_TEXT
);
701 WMMapSubwidgets(panel
->box
);
702 WMMapWidget(panel
->box
);
703 WMRealizeWidget(panel
->box
);
711 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
);