- Added support for NET_WM_NAME/NET_WM_ICON_NAME
[wmaker-crm.git] / WPrefs.app / FontSimple.c
blob3c10db9084d526aa04ea05cc6cbceda84e02fc5a
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 (strcasecmp(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");
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("serif");
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;
262 static char*
263 getSelectedFont(_Panel *panel, char *curfont)
265 WMListItem *item;
266 FcPattern *pat= FcNameParse(curfont);
267 char *name;
269 item= WMGetListSelectedItem(panel->familyL);
270 if (item)
272 FcPatternDel(pat, FC_FAMILY);
273 FcPatternAddString(pat, FC_FAMILY, item->text);
276 item= WMGetListSelectedItem(panel->styleL);
277 if (item)
279 FontStyle *style= (FontStyle*)item->clientData;
281 FcPatternDel(pat, FC_WEIGHT);
282 FcPatternAddInteger(pat, FC_WEIGHT, style->weight);
284 FcPatternDel(pat, FC_WIDTH);
285 FcPatternAddInteger(pat, FC_WIDTH, style->width);
287 FcPatternDel(pat, FC_SLANT);
288 FcPatternAddInteger(pat, FC_SLANT, style->slant);
291 item= WMGetListSelectedItem(panel->sizeL);
292 if (item)
294 FcPatternDel(pat, FC_PIXEL_SIZE);
295 FcPatternAddDouble(pat, FC_PIXEL_SIZE, atoi(item->text));
298 name= FcNameUnparse(pat);
299 FcPatternDestroy(pat);
301 return name;
306 static void
307 updateSampleFont(_Panel *panel)
309 WMMenuItem *item= WMGetPopUpButtonMenuItem(panel->optionP,
310 WMGetPopUpButtonSelectedItem(panel->optionP));
311 char *fn= WMGetMenuItemRepresentedObject(item);
312 WMFont *font= WMCreateFont(WMWidgetScreen(panel->box), fn);
314 if (font)
316 WMSetTextFieldFont(panel->sampleT, font);
317 WMReleaseFont(font);
324 static void
325 selectedFamily(WMWidget *w, void *data)
327 _Panel *panel= (_Panel*)data;
328 WMListItem *item= WMGetListSelectedItem(panel->familyL);
329 char buffer[1024];
331 if (item)
333 FontFamily *family= (FontFamily*)item->clientData;
334 int i;
336 WMClearList(panel->styleL);
337 for (i = 0; i < family->stylen; i++)
339 int j;
340 char *weight= "", *slant= "", *width= "";
341 WMListItem *item;
343 for (j= 0; fontWeights[j].name; j++)
344 if (fontWeights[j].weight == family->styles[i].weight)
346 weight= fontWeights[j].name;
347 break;
349 for (j= 0; fontWidths[j].name; j++)
350 if (fontWidths[j].width == family->styles[i].width)
352 width= fontWidths[j].name;
353 break;
355 for (j= 0; fontSlants[j].name; j++)
356 if (fontSlants[j].slant == family->styles[i].slant)
358 slant= fontSlants[j].name;
359 break;
361 sprintf(buffer, "%s%s%s%s%s",
362 weight, *weight?" ":"",
363 slant, (*slant || *weight)?" ":"",
364 width);
365 if (!buffer[0])
366 strcpy(buffer, "Roman");
368 item= WMAddListItem(panel->styleL, buffer);
369 item->clientData= family->styles+i;
371 WMSelectListItem(panel->styleL, 0);
374 int index= WMGetPopUpButtonSelectedItem(panel->optionP);
375 WMMenuItem *item= WMGetPopUpButtonMenuItem(panel->optionP, index);
376 char *ofont, *nfont;
378 ofont= (char*)WMGetMenuItemRepresentedObject(item);
380 nfont= getSelectedFont(panel, ofont);
381 free(ofont);
382 WMSetMenuItemRepresentedObject(item, nfont);
384 updateSampleFont(panel);
389 static void
390 selected(WMWidget *w, void *data)
392 _Panel *panel= (_Panel*)data;
393 int index= WMGetPopUpButtonSelectedItem(panel->optionP);
394 WMMenuItem *item= WMGetPopUpButtonMenuItem(panel->optionP, index);
395 char *ofont, *nfont;
397 ofont= (char*)WMGetMenuItemRepresentedObject(item);
399 nfont= getSelectedFont(panel, ofont);
400 free(ofont);
401 WMSetMenuItemRepresentedObject(item, nfont);
403 updateSampleFont(panel);
407 static void
408 selectedOption(WMWidget *w, void *data)
410 _Panel *panel= (_Panel*)data;
411 int index = WMGetPopUpButtonSelectedItem(panel->optionP);
412 WMMenuItem *item= WMGetPopUpButtonMenuItem(panel->optionP, index);
413 char *font;
415 font= (char*)WMGetMenuItemRepresentedObject(item);
416 if (font)
418 FcPattern *pat;
420 pat= FcNameParse(font);
421 if (pat)
423 FcChar8 *name;
424 int weight, slant, width;
425 double size;
426 int i;
427 int found;
429 FcDefaultSubstitute(pat);
431 if (FcPatternGetString(pat, FC_FAMILY, 0, &name) != FcResultMatch)
432 name= "sans";
434 found= 0;
435 // select family
436 for (i= 0; i < WMGetListNumberOfRows(panel->familyL); i++)
438 WMListItem *item= WMGetListItem(panel->familyL, i);
439 FontFamily *family= (FontFamily*)item->clientData;
441 if (strcasecmp(family->name, name)==0)
443 found= 1;
444 WMSelectListItem(panel->familyL, i);
445 WMSetListPosition(panel->familyL, i);
446 break;
449 if (!found)
450 WMSelectListItem(panel->familyL, -1);
451 selectedFamily(panel->familyL, panel);
453 // select style
454 if (FcPatternGetInteger(pat, FC_WEIGHT, 0, &weight) != FcResultMatch)
455 weight= FC_WEIGHT_NORMAL;
456 if (FcPatternGetInteger(pat, FC_WIDTH, 0, &width) != FcResultMatch)
457 width= FC_WIDTH_NORMAL;
458 if (FcPatternGetInteger(pat, FC_SLANT, 0, &slant) != FcResultMatch)
459 slant= FC_SLANT_ROMAN;
461 if (FcPatternGetDouble(pat, FC_PIXEL_SIZE, 0, &size) != FcResultMatch)
462 size= 10.0;
464 found= 0;
465 for (i= 0; i < WMGetListNumberOfRows(panel->styleL); i++)
467 WMListItem *item= WMGetListItem(panel->styleL, i);
468 FontStyle *style= (FontStyle*)item->clientData;
469 if (style->weight == weight
470 && style->width == width
471 && style->slant == slant)
473 found= 1;
474 WMSelectListItem(panel->styleL, i);
475 WMSetListPosition(panel->styleL, i);
476 break;
479 if (!found)
480 WMSelectListItem(panel->styleL, -1);
482 found= 0;
484 int closest= 100000, index= -1;
486 for (i= 0; i < WMGetListNumberOfRows(panel->sizeL); i++)
488 WMListItem *item= WMGetListItem(panel->sizeL, i);
489 int tmp;
491 tmp= atoi(item->text);
492 if (abs(tmp-size) < abs(tmp-closest))
494 closest= tmp;
495 index= i;
498 WMSelectListItem(panel->sizeL, index);
499 WMSetListPosition(panel->sizeL, index);
502 selected(NULL, panel);
504 else
505 wwarning("Can't parse font '%s'", font);
508 updateSampleFont(panel);
512 static WMLabel *
513 createListLabel(WMScreen *scr, WMWidget *parent, char *text)
515 WMLabel *label;
516 WMColor *color;
517 WMFont *boldFont= WMBoldSystemFontOfSize(scr, 12);
519 label = WMCreateLabel(parent);
520 WMSetLabelFont(label, boldFont);
521 WMSetLabelText(label, text);
522 WMSetLabelRelief(label, WRSunken);
523 WMSetLabelTextAlignment(label, WACenter);
524 color = WMDarkGrayColor(scr);
525 WMSetWidgetBackgroundColor(label, color);
526 WMReleaseColor(color);
527 color = WMWhiteColor(scr);
528 WMSetLabelTextColor(label, color);
529 WMReleaseColor(color);
531 WMReleaseFont(boldFont);
533 return label;
537 static void
538 showData(_Panel *panel)
540 int i;
541 WMMenuItem *item;
543 for (i= 0; i < WMGetPopUpButtonNumberOfItems(panel->optionP); i++)
545 char *ofont, *font;
547 item= WMGetPopUpButtonMenuItem(panel->optionP, i);
549 ofont= WMGetMenuItemRepresentedObject(item);
550 if (ofont)
551 wfree(ofont);
553 font= GetStringForKey(fontOptions[i].option);
554 if (font)
555 font= wstrdup(font);
556 WMSetMenuItemRepresentedObject(item, font);
559 WMSetPopUpButtonSelectedItem(panel->optionP, 0);
560 selectedOption(panel->optionP, panel);
564 static void
565 storeData(_Panel *panel)
567 int i;
568 WMMenuItem *item;
569 for (i= 0; i < WMGetPopUpButtonNumberOfItems(panel->optionP); i++)
571 char *font;
573 item= WMGetPopUpButtonMenuItem(panel->optionP, i);
575 font= WMGetMenuItemRepresentedObject(item);
576 if (font && *font)
578 SetStringForKey(font, fontOptions[i].option);
584 static void
585 createPanel(Panel *p)
587 _Panel *panel = (_Panel*)p;
588 WMScreen *scr = WMWidgetScreen(panel->parent);
589 WMLabel *label;
590 WMBox *hbox, *vbox;
591 int i;
593 lookup_available_fonts(panel);
595 panel->box = WMCreateBox(panel->parent);
596 WMSetViewExpandsToParent(WMWidgetView(panel->box), 5, 8, 5, 8);
597 WMSetBoxHorizontal(panel->box, False);
598 WMSetBoxBorderWidth(panel->box, 8);
599 WMMapWidget(panel->box);
601 hbox = WMCreateBox(panel->box);
602 WMSetBoxHorizontal(hbox, True);
603 WMAddBoxSubview(panel->box, WMWidgetView(hbox), False, True, 22, 22, 8);
605 label = WMCreateLabel(hbox);
606 WMAddBoxSubview(hbox, WMWidgetView(label), False, True, 150, 0, 10);
607 WMSetLabelText(label, _("Choose Font For"));
608 WMSetLabelTextAlignment(label, WARight);
610 panel->optionP = WMCreatePopUpButton(hbox);
611 WMAddBoxSubview(hbox, WMWidgetView(panel->optionP), False, True, 200, 0, 10);
612 for (i= 0; fontOptions[i].option; i++)
614 WMAddPopUpButtonItem(panel->optionP, _(fontOptions[i].label));
616 WMSetPopUpButtonAction(panel->optionP, selectedOption, panel);
618 hbox = WMCreateBox(panel->box);
619 WMSetBoxHorizontal(hbox, True);
620 WMAddBoxSubview(panel->box, WMWidgetView(hbox), False, True, 100, 0, 2);
624 vbox = WMCreateBox(hbox);
625 WMSetBoxHorizontal(vbox, False);
626 WMAddBoxSubview(hbox, WMWidgetView(vbox), False, True, 240, 20, 2);
628 label = createListLabel(scr, vbox, _("Family"));
629 WMAddBoxSubview(vbox, WMWidgetView(label), False, True, 20, 0, 2);
631 // family
632 panel->familyL = WMCreateList(vbox);
633 WMAddBoxSubview(vbox, WMWidgetView(panel->familyL), True, True, 0, 0, 0);
634 if (panel->fonts)
636 WMListItem *item;
637 for (i= 0; i < panel->fonts->familyn; i++)
639 item = WMAddListItem(panel->familyL, panel->fonts->families[i].name);
640 item->clientData= panel->fonts->families+i;
643 else
644 WMAddListItem(panel->familyL, "sans");
646 WMSetListAction(panel->familyL, selectedFamily, panel);
649 vbox = WMCreateBox(hbox);
650 WMSetBoxHorizontal(vbox, False);
651 WMAddBoxSubview(hbox, WMWidgetView(vbox), True, True, 60, 0, 2);
653 label = createListLabel(scr, vbox, _("Style"));
654 WMAddBoxSubview(vbox, WMWidgetView(label), False, True, 20, 0, 2);
656 panel->styleL = WMCreateList(vbox);
657 WMAddBoxSubview(vbox, WMWidgetView(panel->styleL), True, True, 0, 0, 0);
658 WMSetListAction(panel->styleL, selected, panel);
662 vbox = WMCreateBox(hbox);
663 WMSetBoxHorizontal(vbox, False);
664 WMAddBoxSubview(hbox, WMWidgetView(vbox), False, True, 70, 0, 0);
666 label = createListLabel(scr, vbox, _("Size"));
667 WMAddBoxSubview(vbox, WMWidgetView(label), False, True, 20, 0, 2);
669 // size
670 panel->sizeL = WMCreateList(vbox);
671 WMAddBoxSubview(vbox, WMWidgetView(panel->sizeL), True, True, 0, 0, 0);
672 for (i= 0; standardSizes[i]; i++)
674 WMAddListItem(panel->sizeL, standardSizes[i]);
676 WMSetListAction(panel->sizeL, selected, panel);
679 WMFrame *frame= WMCreateFrame(panel->box);
680 WMSetFrameTitle(frame, _("Sample"));
682 WMAddBoxSubview(panel->box, WMWidgetView(frame), True, True, 50, 0, 0);
684 panel->sampleT= WMCreateTextField(frame);
685 WMSetViewExpandsToParent(WMWidgetView(panel->sampleT), 10, 18, 10, 10);
686 WMSetTextFieldText(panel->sampleT, SAMPLE_TEXT);
690 WMMapSubwidgets(panel->box);
691 WMMapWidget(panel->box);
692 WMRealizeWidget(panel->box);
694 showData(panel);
699 Panel*
700 InitFontSimple(WMScreen *scr, WMWidget *parent)
702 _Panel *panel;
704 panel = wmalloc(sizeof(_Panel));
705 memset(panel, 0, sizeof(_Panel));
707 panel->sectionName = _("Font Configuration");
709 panel->description = _("Configure fonts for Window Maker titlebars, menus etc.");
711 panel->parent = parent;
713 panel->callbacks.createWidgets = createPanel;
714 panel->callbacks.updateDomain = storeData;
716 AddSection(panel, ICON_FILE);
718 return panel;