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 (strcasecmp(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");
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("serif");
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
;
263 getSelectedFont(_Panel
*panel
, char *curfont
)
266 FcPattern
*pat
= FcNameParse(curfont
);
269 item
= WMGetListSelectedItem(panel
->familyL
);
272 FcPatternDel(pat
, FC_FAMILY
);
273 FcPatternAddString(pat
, FC_FAMILY
, item
->text
);
276 item
= WMGetListSelectedItem(panel
->styleL
);
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
);
294 FcPatternDel(pat
, FC_PIXEL_SIZE
);
295 FcPatternAddDouble(pat
, FC_PIXEL_SIZE
, atoi(item
->text
));
298 name
= FcNameUnparse(pat
);
299 FcPatternDestroy(pat
);
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
);
316 WMSetTextFieldFont(panel
->sampleT
, font
);
325 selectedFamily(WMWidget
*w
, void *data
)
327 _Panel
*panel
= (_Panel
*)data
;
328 WMListItem
*item
= WMGetListSelectedItem(panel
->familyL
);
333 FontFamily
*family
= (FontFamily
*)item
->clientData
;
336 WMClearList(panel
->styleL
);
337 for (i
= 0; i
< family
->stylen
; i
++)
340 char *weight
= "", *slant
= "", *width
= "";
343 for (j
= 0; fontWeights
[j
].name
; j
++)
344 if (fontWeights
[j
].weight
== family
->styles
[i
].weight
)
346 weight
= fontWeights
[j
].name
;
349 for (j
= 0; fontWidths
[j
].name
; j
++)
350 if (fontWidths
[j
].width
== family
->styles
[i
].width
)
352 width
= fontWidths
[j
].name
;
355 for (j
= 0; fontSlants
[j
].name
; j
++)
356 if (fontSlants
[j
].slant
== family
->styles
[i
].slant
)
358 slant
= fontSlants
[j
].name
;
361 sprintf(buffer
, "%s%s%s%s%s",
362 weight
, *weight
?" ":"",
363 slant
, (*slant
|| *weight
)?" ":"",
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
);
378 ofont
= (char*)WMGetMenuItemRepresentedObject(item
);
380 nfont
= getSelectedFont(panel
, ofont
);
382 WMSetMenuItemRepresentedObject(item
, nfont
);
384 updateSampleFont(panel
);
390 selected(WMWidget
*w
, void *data
)
392 _Panel
*panel
= (_Panel
*)data
;
393 int index
= WMGetPopUpButtonSelectedItem(panel
->optionP
);
394 WMMenuItem
*item
= WMGetPopUpButtonMenuItem(panel
->optionP
, index
);
397 ofont
= (char*)WMGetMenuItemRepresentedObject(item
);
399 nfont
= getSelectedFont(panel
, ofont
);
401 WMSetMenuItemRepresentedObject(item
, nfont
);
403 updateSampleFont(panel
);
408 selectedOption(WMWidget
*w
, void *data
)
410 _Panel
*panel
= (_Panel
*)data
;
411 int index
= WMGetPopUpButtonSelectedItem(panel
->optionP
);
412 WMMenuItem
*item
= WMGetPopUpButtonMenuItem(panel
->optionP
, index
);
415 font
= (char*)WMGetMenuItemRepresentedObject(item
);
420 pat
= FcNameParse(font
);
424 int weight
, slant
, width
;
429 FcDefaultSubstitute(pat
);
431 if (FcPatternGetString(pat
, FC_FAMILY
, 0, &name
) != FcResultMatch
)
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)
444 WMSelectListItem(panel
->familyL
, i
);
445 WMSetListPosition(panel
->familyL
, i
);
450 WMSelectListItem(panel
->familyL
, -1);
451 selectedFamily(panel
->familyL
, panel
);
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
)
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
)
474 WMSelectListItem(panel
->styleL
, i
);
475 WMSetListPosition(panel
->styleL
, i
);
480 WMSelectListItem(panel
->styleL
, -1);
484 int closest
= 100000, index
= -1;
486 for (i
= 0; i
< WMGetListNumberOfRows(panel
->sizeL
); i
++)
488 WMListItem
*item
= WMGetListItem(panel
->sizeL
, i
);
491 tmp
= atoi(item
->text
);
492 if (abs(tmp
-size
) < abs(tmp
-closest
))
498 WMSelectListItem(panel
->sizeL
, index
);
499 WMSetListPosition(panel
->sizeL
, index
);
502 selected(NULL
, panel
);
505 wwarning("Can't parse font '%s'", font
);
508 updateSampleFont(panel
);
513 createListLabel(WMScreen
*scr
, WMWidget
*parent
, char *text
)
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
);
538 showData(_Panel
*panel
)
543 for (i
= 0; i
< WMGetPopUpButtonNumberOfItems(panel
->optionP
); i
++)
547 item
= WMGetPopUpButtonMenuItem(panel
->optionP
, i
);
549 ofont
= WMGetMenuItemRepresentedObject(item
);
553 font
= GetStringForKey(fontOptions
[i
].option
);
556 WMSetMenuItemRepresentedObject(item
, font
);
559 WMSetPopUpButtonSelectedItem(panel
->optionP
, 0);
560 selectedOption(panel
->optionP
, panel
);
565 storeData(_Panel
*panel
)
569 for (i
= 0; i
< WMGetPopUpButtonNumberOfItems(panel
->optionP
); i
++)
573 item
= WMGetPopUpButtonMenuItem(panel
->optionP
, i
);
575 font
= WMGetMenuItemRepresentedObject(item
);
578 SetStringForKey(font
, fontOptions
[i
].option
);
585 createPanel(Panel
*p
)
587 _Panel
*panel
= (_Panel
*)p
;
588 WMScreen
*scr
= WMWidgetScreen(panel
->parent
);
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);
632 panel
->familyL
= WMCreateList(vbox
);
633 WMAddBoxSubview(vbox
, WMWidgetView(panel
->familyL
), True
, True
, 0, 0, 0);
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
;
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);
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
);
700 InitFontSimple(WMScreen
*scr
, WMWidget
*parent
)
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
);