added Font Configuration
[wmaker-crm.git] / WPrefs.app / FontSimple.c
blob00e131fde46d6e0a23d5ac7c16b5e9bb68bd334d
1 /* FontSimple.c- simplified font configuration panel
2 *
3 * WPrefs - Window Maker Preferences Program
4 *
5 * Copyright (c) 1998-2004 Alfredo K. Kojima
6 *
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,
20 * USA.
24 #include "WPrefs.h"
25 #include <unistd.h>
26 #include <fontconfig/fontconfig.h>
28 #define SAMPLE_TEXT "The Lazy Fox Jumped Ipsum Foobar 1234 - 56789"
31 typedef struct {
32 int weight;
33 int width;
34 int slant;
35 } FontStyle;
37 typedef struct {
38 char *name;
39 int stylen;
40 FontStyle *styles;
41 } FontFamily;
44 typedef struct {
45 int familyn;
46 FontFamily *families;
47 } FontList;
50 typedef struct _Panel {
51 WMBox *box;
52 char *sectionName;
54 char *description;
56 CallbackRec callbacks;
58 WMWidget *parent;
60 WMPopUpButton *optionP;
62 WMList *familyL;
63 WMList *styleL;
65 WMList *sizeL;
67 WMTextField *sampleT;
69 FontList *fonts;
70 } _Panel;
73 #define ICON_FILE "fonts"
76 static struct {
77 char *option;
78 char *label;
79 } fontOptions[]= {
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")},
86 {NULL, NULL},
90 static char *standardSizes[]= {
91 "6",
92 "8",
93 "9",
94 "10",
95 "11",
96 "12",
97 "13",
98 "14",
99 "15",
100 "16",
101 "18",
102 "20",
103 "22",
104 "24",
105 "28",
106 "32",
107 "36",
108 "48",
109 "64",
110 "72",
111 NULL
115 static struct {
116 int weight;
117 char *name;
118 } fontWeights[]= {
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"},
128 {0, NULL}
131 static struct {
132 int slant;
133 char *name;
134 } fontSlants[]= {
135 {FC_SLANT_ROMAN, ""}, /*"Roman"},*/
136 {FC_SLANT_ITALIC, "Italic"},
137 {FC_SLANT_OBLIQUE, "Oblique"},
138 {0, NULL}
141 static struct {
142 int width;
143 char *name;
144 } fontWidths[]= {
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"},
154 {0, NULL}
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);
169 static void
170 lookup_available_fonts(_Panel *panel)
172 FcPattern *pat = FcPatternCreate();
173 FcObjectSet *os;
174 FcFontSet *fonts;
175 FontFamily *family;
177 os = FcObjectSetBuild(FC_FAMILY, FC_WEIGHT, FC_WIDTH, FC_SLANT, NULL);
179 fonts = FcFontList(0, pat, os);
181 if (fonts)
183 int i;
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++)
191 FcChar8 *name;
192 int weight, slant, width;
193 int j, found;
195 if (FcPatternGetString(fonts->fonts[i], FC_FAMILY, 0, &name) != FcResultMatch)
196 continue;
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;
207 found = -1;
208 for (j = 0; j < panel->fonts->familyn && found<0; j++)
209 if (strcmp(panel->fonts->families[j].name, name)==0)
210 found= j;
212 if (found < 0)
214 panel->fonts->families[panel->fonts->familyn++].name= wstrdup(name);
215 family= panel->fonts->families + panel->fonts->familyn-1;
216 family->stylen= 0;
217 family->styles= NULL;
219 else
220 family= panel->fonts->families+found;
222 family->stylen++;
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),
229 compare_family);
231 FcFontSetDestroy(fonts);
233 if (os)
234 FcObjectSetDestroy(os);
235 if (pat)
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);
241 family->stylen= 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);
252 family->stylen= 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);
263 family->stylen= 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;
273 static char*
274 getSelectedFont(_Panel *panel, char *curfont)
276 WMListItem *item;
277 FcPattern *pat= FcNameParse(curfont);
278 char *name;
280 item= WMGetListSelectedItem(panel->familyL);
281 if (item)
283 FcPatternDel(pat, FC_FAMILY);
284 FcPatternAddString(pat, FC_FAMILY, item->text);
287 item= WMGetListSelectedItem(panel->styleL);
288 if (item)
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);
303 if (item)
305 FcPatternDel(pat, FC_PIXEL_SIZE);
306 FcPatternAddDouble(pat, FC_PIXEL_SIZE, atoi(item->text));
309 name= FcNameUnparse(pat);
310 FcPatternDestroy(pat);
312 return name;
317 static void
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);
325 if (font)
327 WMSetTextFieldFont(panel->sampleT, font);
328 WMReleaseFont(font);
335 static void
336 selectedFamily(WMWidget *w, void *data)
338 _Panel *panel= (_Panel*)data;
339 WMListItem *item= WMGetListSelectedItem(panel->familyL);
340 char buffer[1024];
342 if (item)
344 FontFamily *family= (FontFamily*)item->clientData;
345 int i;
347 WMClearList(panel->styleL);
348 for (i = 0; i < family->stylen; i++)
350 int j;
351 char *weight= "", *slant= "", *width= "";
352 WMListItem *item;
354 for (j= 0; fontWeights[j].name; j++)
355 if (fontWeights[j].weight == family->styles[i].weight)
357 weight= fontWeights[j].name;
358 break;
360 for (j= 0; fontWidths[j].name; j++)
361 if (fontWidths[j].width == family->styles[i].width)
363 width= fontWidths[j].name;
364 break;
366 for (j= 0; fontSlants[j].name; j++)
367 if (fontSlants[j].slant == family->styles[i].slant)
369 slant= fontSlants[j].name;
370 break;
372 sprintf(buffer, "%s%s%s%s%s",
373 weight, *weight?" ":"",
374 slant, (*slant || *weight)?" ":"",
375 width);
376 if (!buffer[0])
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);
387 char *ofont, *nfont;
389 ofont= (char*)WMGetMenuItemRepresentedObject(item);
391 nfont= getSelectedFont(panel, ofont);
392 free(ofont);
393 WMSetMenuItemRepresentedObject(item, nfont);
395 updateSampleFont(panel);
400 static void
401 selected(WMWidget *w, void *data)
403 _Panel *panel= (_Panel*)data;
404 int index= WMGetPopUpButtonSelectedItem(panel->optionP);
405 WMMenuItem *item= WMGetPopUpButtonMenuItem(panel->optionP, index);
406 char *ofont, *nfont;
408 ofont= (char*)WMGetMenuItemRepresentedObject(item);
410 nfont= getSelectedFont(panel, ofont);
411 free(ofont);
412 WMSetMenuItemRepresentedObject(item, nfont);
414 updateSampleFont(panel);
418 static void
419 selectedOption(WMWidget *w, void *data)
421 _Panel *panel= (_Panel*)data;
422 int index= WMGetPopUpButtonSelectedItem(panel->optionP);
423 WMMenuItem *item= WMGetPopUpButtonMenuItem(panel->optionP, index);
424 char *font;
426 font= (char*)WMGetMenuItemRepresentedObject(item);
427 if (font)
429 FcPattern *pat;
431 pat= FcNameParse(font);
432 if (pat)
434 FcChar8 *name;
435 int weight, slant, width;
436 double size;
437 int i;
438 int found;
440 FcDefaultSubstitute(pat);
442 if (FcPatternGetString(pat, FC_FAMILY, 0, &name) != FcResultMatch)
443 name= "sans";
445 found= 0;
446 // select family
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)
454 found= 1;
455 WMSelectListItem(panel->familyL, i);
456 WMSetListPosition(panel->familyL, i);
457 break;
460 if (!found)
461 WMSelectListItem(panel->familyL, -1);
462 selectedFamily(panel->familyL, panel);
464 // select style
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)
473 size= 10.0;
475 found= 0;
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)
484 found= 1;
485 WMSelectListItem(panel->styleL, i);
486 WMSetListPosition(panel->styleL, i);
487 break;
490 if (!found)
491 WMSelectListItem(panel->styleL, -1);
493 found= 0;
495 int closest= 100000, index= -1;
497 for (i= 0; i < WMGetListNumberOfRows(panel->sizeL); i++)
499 WMListItem *item= WMGetListItem(panel->sizeL, i);
500 int tmp;
502 tmp= atoi(item->text);
503 if (abs(tmp-size) < abs(tmp-closest))
505 closest= tmp;
506 index= i;
509 WMSelectListItem(panel->sizeL, index);
510 WMSetListPosition(panel->sizeL, index);
513 selected(NULL, panel);
515 else
516 wwarning("Can't parse font '%s'", font);
519 updateSampleFont(panel);
523 static WMLabel *
524 createListLabel(WMScreen *scr, WMWidget *parent, char *text)
526 WMLabel *label;
527 WMColor *color;
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);
544 return label;
548 static void
549 showData(_Panel *panel)
551 int i;
552 WMMenuItem *item;
554 for (i= 0; i < WMGetPopUpButtonNumberOfItems(panel->optionP); i++)
556 char *ofont;
558 item= WMGetPopUpButtonMenuItem(panel->optionP, i);
560 ofont= WMGetMenuItemRepresentedObject(item);
561 if (ofont)
562 wfree(ofont);
564 WMSetMenuItemRepresentedObject(item, GetStringForKey(fontOptions[i].option));
567 WMSetPopUpButtonSelectedItem(panel->optionP, 0);
568 selectedOption(panel->optionP, panel);
572 static void
573 storeData(_Panel *panel)
575 int i;
576 WMMenuItem *item;
577 for (i= 0; i < WMGetPopUpButtonNumberOfItems(panel->optionP); i++)
579 char *font;
581 item= WMGetPopUpButtonMenuItem(panel->optionP, i);
583 font= WMGetMenuItemRepresentedObject(item);
584 if (font)
586 SetStringForKey(font, fontOptions[i].option);
592 static void
593 createPanel(Panel *p)
595 _Panel *panel = (_Panel*)p;
596 WMScreen *scr = WMWidgetScreen(panel->parent);
597 WMLabel *label;
598 WMBox *hbox, *vbox;
599 int i;
601 lookup_available_fonts(panel);
603 panel->box = WMCreateBox(panel->parent);
604 WMSetViewExpandsToParent(WMWidgetView(panel->box), 5, 8, 5, 8);
605 WMSetBoxHorizontal(panel->box, False);
606 WMSetBoxBorderWidth(panel->box, 8);
607 WMMapWidget(panel->box);
609 hbox = WMCreateBox(panel->box);
610 WMSetBoxHorizontal(hbox, True);
611 WMAddBoxSubview(panel->box, WMWidgetView(hbox), False, True, 22, 22, 8);
613 label = WMCreateLabel(hbox);
614 WMAddBoxSubview(hbox, WMWidgetView(label), False, True, 150, 0, 10);
615 WMSetLabelText(label, _("Choose Font For"));
616 WMSetLabelTextAlignment(label, WARight);
618 panel->optionP = WMCreatePopUpButton(hbox);
619 WMAddBoxSubview(hbox, WMWidgetView(panel->optionP), False, True, 200, 0, 10);
620 for (i= 0; fontOptions[i].option; i++)
622 WMAddPopUpButtonItem(panel->optionP, _(fontOptions[i].label));
624 WMSetPopUpButtonAction(panel->optionP, selectedOption, panel);
626 hbox = WMCreateBox(panel->box);
627 WMSetBoxHorizontal(hbox, True);
628 WMAddBoxSubview(panel->box, WMWidgetView(hbox), False, True, 100, 0, 2);
632 vbox = WMCreateBox(hbox);
633 WMSetBoxHorizontal(vbox, False);
634 WMAddBoxSubview(hbox, WMWidgetView(vbox), False, True, 240, 20, 2);
636 label = createListLabel(scr, vbox, _("Family"));
637 WMAddBoxSubview(vbox, WMWidgetView(label), False, True, 20, 0, 2);
639 // family
640 panel->familyL = WMCreateList(vbox);
641 WMAddBoxSubview(vbox, WMWidgetView(panel->familyL), True, True, 0, 0, 4);
642 if (panel->fonts)
644 WMListItem *item;
645 for (i= 0; i < panel->fonts->familyn; i++)
647 item = WMAddListItem(panel->familyL, panel->fonts->families[i].name);
648 item->clientData= panel->fonts->families+i;
651 else
652 WMAddListItem(panel->familyL, "sans");
654 WMSetListAction(panel->familyL, selectedFamily, panel);
657 vbox = WMCreateBox(hbox);
658 WMSetBoxHorizontal(vbox, False);
659 WMAddBoxSubview(hbox, WMWidgetView(vbox), True, True, 60, 0, 2);
661 label = createListLabel(scr, vbox, _("Style"));
662 WMAddBoxSubview(vbox, WMWidgetView(label), False, True, 20, 0, 2);
664 panel->styleL = WMCreateList(vbox);
665 WMAddBoxSubview(vbox, WMWidgetView(panel->styleL), True, True, 0, 0, 4);
666 WMSetListAction(panel->styleL, selected, panel);
670 vbox = WMCreateBox(hbox);
671 WMSetBoxHorizontal(vbox, False);
672 WMAddBoxSubview(hbox, WMWidgetView(vbox), False, True, 70, 0, 2);
674 label = createListLabel(scr, vbox, _("Size"));
675 WMAddBoxSubview(vbox, WMWidgetView(label), False, True, 20, 0, 0);
677 // size
678 panel->sizeL = WMCreateList(vbox);
679 WMAddBoxSubview(vbox, WMWidgetView(panel->sizeL), True, True, 0, 0, 0);
680 for (i= 0; standardSizes[i]; i++)
682 WMAddListItem(panel->sizeL, standardSizes[i]);
684 WMSetListAction(panel->sizeL, selected, panel);
687 WMFrame *frame= WMCreateFrame(panel->box);
688 WMSetFrameTitle(frame, _("Sample"));
690 WMAddBoxSubview(panel->box, WMWidgetView(frame), True, True, 50, 0, 0);
692 panel->sampleT= WMCreateTextField(frame);
693 WMSetViewExpandsToParent(WMWidgetView(panel->sampleT), 10, 18, 10, 10);
694 WMSetTextFieldText(panel->sampleT, SAMPLE_TEXT);
698 WMMapSubwidgets(panel->box);
699 WMMapWidget(panel->box);
700 WMRealizeWidget(panel->box);
702 showData(panel);
707 Panel*
708 InitFontSimple(WMScreen *scr, WMWidget *parent)
710 _Panel *panel;
712 panel = wmalloc(sizeof(_Panel));
713 memset(panel, 0, sizeof(_Panel));
715 panel->sectionName = _("Font Configuration");
717 panel->description = _("Configure fonts for Window Maker titlebars, menus etc.");
719 panel->parent = parent;
721 panel->callbacks.createWidgets = createPanel;
722 panel->callbacks.updateDomain = storeData;
724 AddSection(panel, ICON_FILE);
726 return panel;