fixed font bug in WINGs
[wmaker-crm.git] / WINGs / wfontpanel.c
blob46117d4a0c471015d8c24965f64ec9f30946db7f
5 #include "WINGsP.h"
6 #include "WUtil.h"
8 #include <ctype.h>
9 #include <strings.h>
12 typedef struct W_FontPanel {
13 WMWindow *win;
15 WMFrame *upperF;
16 WMTextField *sampleT;
18 WMSplitView *split;
20 WMFrame *lowerF;
21 WMLabel *famL;
22 WMList *famLs;
23 WMLabel *typL;
24 WMList *typLs;
25 WMLabel *sizL;
26 WMTextField *sizT;
27 WMList *sizLs;
29 WMButton *revertB;
30 WMButton *setB;
32 proplist_t fdb;
33 } FontPanel;
36 #define MIN_UPPER_HEIGHT 20
37 #define MIN_LOWER_HEIGHT 140
40 #define MAX_FONTS_TO_RETRIEVE 2000
42 #define BUTTON_SPACE_HEIGHT 40
44 #define MIN_WIDTH 250
45 #define MIN_HEIGHT (MIN_UPPER_HEIGHT+MIN_LOWER_HEIGHT+BUTTON_SPACE_HEIGHT)
47 #define DEF_UPPER_HEIGHT 60
48 #define DEF_LOWER_HEIGHT 310
50 #define DEF_WIDTH 320
51 #define DEF_HEIGHT (DEF_UPPER_HEIGHT+DEF_LOWER_HEIGHT)
56 static int scalableFontSizes[] = {
57 8,
58 10,
59 11,
60 12,
61 14,
62 16,
63 18,
64 20,
65 24,
66 36,
67 48,
73 static void getSelectedFont(FontPanel *panel, char buffer[]);
76 static void arrangeLowerFrame(FontPanel *panel);
78 static void familyClick(WMWidget *, void *);
79 static void typefaceClick(WMWidget *, void *);
80 static void sizeClick(WMWidget *, void *);
83 static void listFamilies(WMScreen *scr, WMFontPanel *panel);
85 static void
86 splitViewConstrainCallback(WMSplitView *sPtr, int divIndex, int *min, int *max)
88 *min = MIN_UPPER_HEIGHT;
89 *max = WMWidgetHeight(sPtr)-BUTTON_SPACE_HEIGHT-MIN_LOWER_HEIGHT;
93 static void
94 notificationObserver(void *self, WMNotification *notif)
96 WMFontPanel *panel = (WMFontPanel*)self;
97 void *object = WMGetNotificationObject(notif);
99 if (WMGetNotificationName(notif) == WMViewSizeDidChangeNotification) {
100 if (object == WMWidgetView(panel->win)) {
101 int h = WMWidgetHeight(panel->win);
102 int w = WMWidgetWidth(panel->win);
104 WMResizeWidget(panel->split, w, h-BUTTON_SPACE_HEIGHT);
106 WMMoveWidget(panel->setB, w-80, h-(BUTTON_SPACE_HEIGHT-5));
108 WMMoveWidget(panel->revertB, w-240, h-(BUTTON_SPACE_HEIGHT-5));
110 } else if (object == WMWidgetView(panel->upperF)) {
112 if (WMWidgetHeight(panel->upperF) < MIN_UPPER_HEIGHT) {
113 WMResizeWidget(panel->upperF, WMWidgetWidth(panel->upperF),
114 MIN_UPPER_HEIGHT);
115 } else {
116 WMResizeWidget(panel->sampleT, WMWidgetWidth(panel->upperF)-20,
117 WMWidgetHeight(panel->upperF)-10);
120 } else if (object == WMWidgetView(panel->lowerF)) {
122 if (WMWidgetHeight(panel->lowerF) < MIN_LOWER_HEIGHT) {
123 WMResizeWidget(panel->upperF, WMWidgetWidth(panel->upperF),
124 MIN_UPPER_HEIGHT);
126 WMMoveWidget(panel->lowerF, 0, WMWidgetHeight(panel->upperF)
127 + WMGetSplitViewDividerThickness(panel->split));
129 WMResizeWidget(panel->lowerF, WMWidgetWidth(panel->lowerF),
130 WMWidgetWidth(panel->split) - MIN_UPPER_HEIGHT
131 - WMGetSplitViewDividerThickness(panel->split));
132 } else {
133 arrangeLowerFrame(panel);
140 WMFontPanel*
141 WMGetFontPanel(WMScreen *scr)
143 FontPanel *panel;
144 WMColor *dark, *white;
145 WMFont *font;
146 int divThickness;
148 if (scr->sharedFontPanel)
149 return scr->sharedFontPanel;
152 panel = wmalloc(sizeof(FontPanel));
153 memset(panel, 0, sizeof(FontPanel));
155 panel->win = WMCreateWindow(scr, "fontPanel");
156 /* WMSetWidgetBackgroundColor(panel->win, WMWhiteColor(scr));*/
157 WMResizeWidget(panel->win, DEF_WIDTH, DEF_HEIGHT);
158 WMSetWindowMinSize(panel->win, MIN_WIDTH, MIN_HEIGHT);
159 WMSetViewNotifySizeChanges(WMWidgetView(panel->win), True);
161 panel->split = WMCreateSplitView(panel->win);
162 WMResizeWidget(panel->split, DEF_WIDTH, DEF_HEIGHT - BUTTON_SPACE_HEIGHT);
163 WMSetSplitViewConstrainProc(panel->split, splitViewConstrainCallback);
165 divThickness = WMGetSplitViewDividerThickness(panel->split);
167 panel->upperF = WMCreateFrame(panel->win);
168 WMSetFrameRelief(panel->upperF, WRFlat);
169 WMSetViewNotifySizeChanges(WMWidgetView(panel->upperF), True);
170 panel->lowerF = WMCreateFrame(panel->win);
171 /* WMSetWidgetBackgroundColor(panel->lowerF, WMBlackColor(scr));*/
172 WMSetFrameRelief(panel->lowerF, WRFlat);
173 WMSetViewNotifySizeChanges(WMWidgetView(panel->lowerF), True);
175 WMAddSplitViewSubview(panel->split, W_VIEW(panel->upperF));
176 WMAddSplitViewSubview(panel->split, W_VIEW(panel->lowerF));
178 WMResizeWidget(panel->upperF, DEF_WIDTH, DEF_UPPER_HEIGHT);
180 WMResizeWidget(panel->lowerF, DEF_WIDTH, DEF_LOWER_HEIGHT);
182 WMMoveWidget(panel->lowerF, 0, 60+divThickness);
184 white = WMWhiteColor(scr);
185 dark = WMDarkGrayColor(scr);
187 panel->sampleT = WMCreateTextField(panel->upperF);
188 WMResizeWidget(panel->sampleT, DEF_WIDTH - 20, 50);
189 WMMoveWidget(panel->sampleT, 10, 10);
190 WMSetTextFieldText(panel->sampleT, "Test!!!");
192 font = WMBoldSystemFontOfSize(scr, 12);
194 panel->famL = WMCreateLabel(panel->lowerF);
195 WMSetWidgetBackgroundColor(panel->famL, dark);
196 WMSetLabelText(panel->famL, "Family");
197 WMSetLabelFont(panel->famL, font);
198 WMSetLabelTextColor(panel->famL, white);
199 WMSetLabelRelief(panel->famL, WRSunken);
200 WMSetLabelTextAlignment(panel->famL, WACenter);
202 panel->famLs = WMCreateList(panel->lowerF);
203 WMSetListAction(panel->famLs, familyClick, panel);
205 panel->typL = WMCreateLabel(panel->lowerF);
206 WMSetWidgetBackgroundColor(panel->typL, dark);
207 WMSetLabelText(panel->typL, "Typeface");
208 WMSetLabelFont(panel->typL, font);
209 WMSetLabelTextColor(panel->typL, white);
210 WMSetLabelRelief(panel->typL, WRSunken);
211 WMSetLabelTextAlignment(panel->typL, WACenter);
213 panel->typLs = WMCreateList(panel->lowerF);
214 WMSetListAction(panel->typLs, typefaceClick, panel);
216 panel->sizL = WMCreateLabel(panel->lowerF);
217 WMSetWidgetBackgroundColor(panel->sizL, dark);
218 WMSetLabelText(panel->sizL, "Size");
219 WMSetLabelFont(panel->sizL, font);
220 WMSetLabelTextColor(panel->sizL, white);
221 WMSetLabelRelief(panel->sizL, WRSunken);
222 WMSetLabelTextAlignment(panel->sizL, WACenter);
224 panel->sizT = WMCreateTextField(panel->lowerF);
225 /* WMSetTextFieldAlignment(panel->sizT, WARight);*/
227 panel->sizLs = WMCreateList(panel->lowerF);
228 WMSetListAction(panel->sizLs, sizeClick, panel);
230 WMReleaseFont(font);
231 WMReleaseColor(white);
232 WMReleaseColor(dark);
234 panel->setB = WMCreateCommandButton(panel->win);
235 WMResizeWidget(panel->setB, 70, 24);
236 WMMoveWidget(panel->setB, 240, DEF_HEIGHT - (BUTTON_SPACE_HEIGHT-5));
237 WMSetButtonText(panel->setB, "Set");
239 panel->revertB = WMCreateCommandButton(panel->win);
240 WMResizeWidget(panel->revertB, 70, 24);
241 WMMoveWidget(panel->revertB, 80, DEF_HEIGHT - (BUTTON_SPACE_HEIGHT-5));
242 WMSetButtonText(panel->revertB, "Revert");
244 WMRealizeWidget(panel->win);
246 WMMapSubwidgets(panel->upperF);
247 WMMapSubwidgets(panel->lowerF);
248 WMMapSubwidgets(panel->split);
249 WMMapSubwidgets(panel->win);
251 WMUnmapWidget(panel->revertB);
253 arrangeLowerFrame(panel);
255 scr->sharedFontPanel = panel;
258 /* register notification observers */
259 WMAddNotificationObserver(notificationObserver, panel,
260 WMViewSizeDidChangeNotification,
261 WMWidgetView(panel->win));
262 WMAddNotificationObserver(notificationObserver, panel,
263 WMViewSizeDidChangeNotification,
264 WMWidgetView(panel->upperF));
265 WMAddNotificationObserver(notificationObserver, panel,
266 WMViewSizeDidChangeNotification,
267 WMWidgetView(panel->lowerF));
270 listFamilies(scr, panel);
273 return panel;
277 void
278 WMFreeFontPanel(WMFontPanel *panel)
280 if (panel == WMWidgetScreen(panel->win)->sharedFontPanel) {
281 WMWidgetScreen(panel->win)->sharedFontPanel = NULL;
283 WMRemoveNotificationObserver(panel);
284 WMUnmapWidget(panel->win);
285 WMDestroyWidget(panel->win);
286 free(panel);
290 void
291 WMShowFontPanel(WMFontPanel *panel)
293 WMMapWidget(panel->win);
297 void
298 WMHideFontPanel(WMFontPanel *panel)
300 WMUnmapWidget(panel->win);
304 void
305 WMSetFontPanelFont(WMFontPanel *panel, WMFont *font)
311 Bool
312 WMSetFontPanelFontName(WMFontPanel *panel, char *fontName)
315 return True;
319 WMFont*
320 WMGetFontPanelFont(WMFontPanel *panel)
322 return WMGetTextFieldFont(panel->sampleT);
326 char*
327 WMGetFontPanelFontName(WMFontPanel *panel)
329 char name[256];
331 getSelectedFont(panel, name);
333 return wstrdup(name);
338 static void
339 arrangeLowerFrame(FontPanel *panel)
341 int width = WMWidgetWidth(panel->lowerF) - 55 - 30;
342 int height = WMWidgetHeight(panel->split) - WMWidgetHeight(panel->upperF);
343 int fw, tw, sw;
345 #define LABEL_HEIGHT 20
347 height -= WMGetSplitViewDividerThickness(panel->split);
350 height -= LABEL_HEIGHT + 8;
352 fw = (125*width) / 235;
353 tw = (110*width) / 235;
354 sw = 55;
356 WMMoveWidget(panel->famL, 10, 0);
357 WMResizeWidget(panel->famL, fw, LABEL_HEIGHT);
359 WMMoveWidget(panel->famLs, 10, 23);
360 WMResizeWidget(panel->famLs, fw, height);
362 WMMoveWidget(panel->typL, 10+fw+3, 0);
363 WMResizeWidget(panel->typL, tw, LABEL_HEIGHT);
365 WMMoveWidget(panel->typLs, 10+fw+3, 23);
366 WMResizeWidget(panel->typLs, tw, height);
368 WMMoveWidget(panel->sizL, 10+fw+3+tw+3, 0);
369 WMResizeWidget(panel->sizL, sw+4, LABEL_HEIGHT);
371 WMMoveWidget(panel->sizT, 10+fw+3+tw+3, 23);
372 WMResizeWidget(panel->sizT, sw+4, 20);
374 WMMoveWidget(panel->sizLs, 10+fw+3+tw+3, 46);
375 WMResizeWidget(panel->sizLs, sw+4, height-23);
381 #define ALL_FONTS_MASK "-*-*-*-*-*-*-*-*-*-*-*-*-*-*"
383 #define FOUNDRY 0
384 #define FAMILY 1
385 #define WEIGHT 2
386 #define SLANT 3
387 #define SETWIDTH 4
388 #define ADD_STYLE 5
389 #define PIXEL_SIZE 6
390 #define POINT_SIZE 7
391 #define RES_X 8
392 #define RES_Y 9
393 #define SPACING 10
394 #define AV_WIDTH 11
395 #define REGISTRY 12
396 #define ENCODING 13
398 #define NUM_FIELDS 14
402 static Bool
403 parseFont(char *font, char values[NUM_FIELDS][256])
405 char *ptr;
406 int part;
407 char buffer[256], *bptr;
409 part = FOUNDRY;
410 ptr = font;
411 ptr++; /* skip first - */
412 bptr = buffer;
413 while (*ptr) {
414 if (*ptr == '-') {
415 *bptr = 0;
416 strcpy(values[part], buffer);
417 bptr = buffer;
418 part++;
419 } else {
420 *bptr++ = *ptr;
422 ptr++;
424 *bptr = 0;
425 strcpy(values[part], buffer);
427 return True;
432 static int
433 isXLFD(char *font, int *length_ret)
435 int c = 0;
437 *length_ret = 0;
438 while (*font) {
439 (*length_ret)++;
440 if (*font++ == '-')
441 c++;
444 return c==NUM_FIELDS;
450 typedef struct {
451 char *weight;
452 char *slant;
454 char *setWidth;
455 char *addStyle;
457 char showSetWidth; /* when duplicated */
458 char showAddStyle; /* when duplicated */
460 WMBag *sizes;
461 } Typeface;
464 typedef struct {
465 char *name;
467 char *foundry;
468 char *registry, *encoding;
470 char showFoundry; /* when duplicated */
471 char showRegistry; /* when duplicated */
473 WMBag *typefaces;
474 } Family;
479 static int
480 compare_int(const void *a, const void *b)
482 int i1 = *(int*)a;
483 int i2 = *(int*)b;
485 if (i1 < i2)
486 return -1;
487 else if (i1 > i2)
488 return 1;
489 else
490 return 0;
495 static void
496 addSizeToTypeface(Typeface *face, int size)
498 if (size == 0) {
499 int j;
501 for (j = 0; j < sizeof(scalableFontSizes)/sizeof(int); j++) {
502 size = scalableFontSizes[j];
504 if (!WMCountInBag(face->sizes, (void*)size)) {
505 WMPutInBag(face->sizes, (void*)size);
508 WMSortBag(face->sizes, compare_int);
509 } else {
510 if (!WMCountInBag(face->sizes, (void*)size)) {
511 WMPutInBag(face->sizes, (void*)size);
512 WMSortBag(face->sizes, compare_int);
519 static void
520 addTypefaceToFamily(Family *family, char fontFields[NUM_FIELDS][256])
522 Typeface *face;
523 int i;
525 if (family->typefaces) {
526 for (i = 0; i < WMGetBagItemCount(family->typefaces); i++) {
527 int size;
529 face = WMGetFromBag(family->typefaces, i);
531 if (strcmp(face->weight, fontFields[WEIGHT]) != 0) {
532 continue;
534 if (strcmp(face->slant, fontFields[SLANT]) != 0) {
535 continue;
538 size = atoi(fontFields[PIXEL_SIZE]);
540 addSizeToTypeface(face, size);
542 return;
544 } else {
545 family->typefaces = WMCreateBag(4);
548 face = wmalloc(sizeof(Typeface));
549 memset(face, 0, sizeof(Typeface));
551 face->weight = wstrdup(fontFields[WEIGHT]);
552 face->slant = wstrdup(fontFields[SLANT]);
553 face->setWidth = wstrdup(fontFields[SETWIDTH]);
554 face->addStyle = wstrdup(fontFields[ADD_STYLE]);
556 face->sizes = WMCreateBag(4);
557 addSizeToTypeface(face, atoi(fontFields[PIXEL_SIZE]));
559 WMPutInBag(family->typefaces, face);
565 * families (same family name) (Hashtable of family -> bag)
566 * registries (same family but different registries)
570 static void
571 addFontToFamily(WMHashTable *families, char fontFields[NUM_FIELDS][256])
573 int i;
574 Family *fam;
575 WMBag *family;
578 family = WMHashGet(families, fontFields[FAMILY]);
580 if (family) {
581 /* look for same encoding/registry and foundry */
582 for (i = 0; i < WMGetBagItemCount(family); i++) {
583 int enc, reg, found;
585 fam = WMGetFromBag(family, i);
587 enc = (strcmp(fam->encoding, fontFields[ENCODING]) == 0);
588 reg = (strcmp(fam->registry, fontFields[REGISTRY]) == 0);
589 found = (strcmp(fam->foundry, fontFields[FOUNDRY]) == 0);
591 if (enc && reg && found) {
592 addTypefaceToFamily(fam, fontFields);
593 return;
596 /* look for same encoding/registry */
597 for (i = 0; i < WMGetBagItemCount(family); i++) {
598 int enc, reg;
600 fam = WMGetFromBag(family, i);
602 enc = (strcmp(fam->encoding, fontFields[ENCODING]) == 0);
603 reg = (strcmp(fam->registry, fontFields[REGISTRY]) == 0);
605 if (enc && reg) {
606 /* has the same encoding, but the foundry is different */
607 fam->showFoundry = 1;
609 fam = wmalloc(sizeof(Family));
610 memset(fam, 0, sizeof(Family));
612 fam->name = wstrdup(fontFields[FAMILY]);
613 fam->foundry = wstrdup(fontFields[FOUNDRY]);
614 fam->registry = wstrdup(fontFields[REGISTRY]);
615 fam->encoding = wstrdup(fontFields[ENCODING]);
616 fam->showFoundry = 1;
618 addTypefaceToFamily(fam, fontFields);
620 WMPutInBag(family, fam);
621 return;
624 /* look for same foundry */
625 for (i = 0; i < WMGetBagItemCount(family); i++) {
626 int found;
628 fam = WMGetFromBag(family, i);
630 found = (strcmp(fam->foundry, fontFields[FOUNDRY]) == 0);
632 if (found) {
633 /* has the same foundry, but encoding is different */
634 fam->showRegistry = 1;
636 fam = wmalloc(sizeof(Family));
637 memset(fam, 0, sizeof(Family));
639 fam->name = wstrdup(fontFields[FAMILY]);
640 fam->foundry = wstrdup(fontFields[FOUNDRY]);
641 fam->registry = wstrdup(fontFields[REGISTRY]);
642 fam->encoding = wstrdup(fontFields[ENCODING]);
643 fam->showRegistry = 1;
645 addTypefaceToFamily(fam, fontFields);
647 WMPutInBag(family, fam);
648 return;
651 /* foundry and encoding do not match anything known */
652 fam = wmalloc(sizeof(Family));
653 memset(fam, 0, sizeof(Family));
655 fam->name = wstrdup(fontFields[FAMILY]);
656 fam->foundry = wstrdup(fontFields[FOUNDRY]);
657 fam->registry = wstrdup(fontFields[REGISTRY]);
658 fam->encoding = wstrdup(fontFields[ENCODING]);
659 fam->showFoundry = 1;
660 fam->showRegistry = 1;
662 addTypefaceToFamily(fam, fontFields);
664 WMPutInBag(family, fam);
665 return;
668 family = WMCreateBag(8);
670 fam = wmalloc(sizeof(Family));
671 memset(fam, 0, sizeof(Family));
673 fam->name = wstrdup(fontFields[FAMILY]);
674 fam->foundry = wstrdup(fontFields[FOUNDRY]);
675 fam->registry = wstrdup(fontFields[REGISTRY]);
676 fam->encoding = wstrdup(fontFields[ENCODING]);
678 addTypefaceToFamily(fam, fontFields);
680 WMPutInBag(family, fam);
682 WMHashInsert(families, fam->name, family);
687 static void
688 listFamilies(WMScreen *scr, WMFontPanel *panel)
690 char **fontList;
691 int count;
692 int i;
693 WMHashTable *families = WMCreateHashTable(WMStringPointerHashCallbacks);
694 char fields[NUM_FIELDS][256];
695 WMHashEnumerator enumer;
696 WMBag *bag;
698 fontList = XListFonts(scr->display, ALL_FONTS_MASK, MAX_FONTS_TO_RETRIEVE,
699 &count);
700 if (!fontList) {
701 WMRunAlertPanel(scr, panel->win, "Error",
702 "Could not retrieve font list", "OK", NULL, NULL);
703 return;
706 for (i = 0; i < count; i++) {
707 int fname_len;
709 if (!isXLFD(fontList[i], &fname_len)) {
710 *fontList[i] = '\0';
711 continue;
713 if (fname_len > 255) {
714 wwarning("font name %s is longer than 256, which is invalid.",
715 fontList[i]);
716 *fontList[i] = '\0';
717 continue;
719 if (!parseFont(fontList[i], fields)) {
720 *fontList[i] = '\0';
721 continue;
723 addFontToFamily(families, fields);
726 enumer = WMEnumerateHashTable(families);
728 while ((bag = WMNextHashEnumeratorItem(&enumer))) {
729 int i;
730 Family *fam;
731 char buffer[256];
732 WMListItem *item;
734 for (i = 0; i < WMGetBagItemCount(bag); i++) {
735 fam = WMGetFromBag(bag, i);
737 strcpy(buffer, fam->name);
739 if (fam->showFoundry) {
740 strcat(buffer, " ");
741 strcat(buffer, fam->foundry);
742 strcat(buffer, " ");
744 if (fam->showRegistry) {
745 strcat(buffer, " (");
746 strcat(buffer, fam->registry);
747 strcat(buffer, "-");
748 strcat(buffer, fam->encoding);
749 strcat(buffer, ")");
751 item = WMAddSortedListItem(panel->famLs, buffer);
753 item->clientData = fam;
755 WMFreeBag(bag);
758 WMFreeHashTable(families);
762 static void
763 getSelectedFont(FontPanel *panel, char buffer[])
765 WMListItem *item;
766 Family *family;
767 Typeface *face;
768 char *size;
771 item = WMGetListSelectedItem(panel->famLs);
772 if (!item)
773 return;
774 family = (Family*)item->clientData;
776 item = WMGetListSelectedItem(panel->typLs);
777 if (!item)
778 return;
779 face = (Typeface*)item->clientData;
781 size = WMGetTextFieldText(panel->sizT);
783 sprintf(buffer, "-%s-%s-%s-%s-%s-%s-%s-*-*-*-*-*-%s-%s",
784 family->foundry,
785 family->name,
786 face->weight,
787 face->slant,
788 face->setWidth,
789 face->addStyle,
790 size,
791 family->registry,
792 family->encoding);
797 static void
798 preview(FontPanel *panel)
800 char buffer[256];
801 WMFont *font;
803 getSelectedFont(panel, buffer);
805 font = WMCreateFont(WMWidgetScreen(panel->win), buffer);
806 if (font) {
807 WMSetTextFieldFont(panel->sampleT, font);
808 WMReleaseFont(font);
814 static void
815 familyClick(WMWidget *w, void *data)
817 WMList *lPtr = (WMList*)w;
818 WMListItem *item;
819 Family *family;
820 FontPanel *panel = (FontPanel*)data;
821 Typeface *face;
822 int i;
823 /* current typeface and size */
824 char *oface = NULL;
825 char *osize = NULL;
826 int facei = -1;
827 int sizei = -1;
829 /* must try to keep the same typeface and size for the new family */
830 item = WMGetListSelectedItem(panel->typLs);
831 if (item)
832 oface = wstrdup(item->text);
834 osize = WMGetTextFieldText(panel->sizT);
837 item = WMGetListSelectedItem(lPtr);
838 family = (Family*)item->clientData;
840 WMClearList(panel->typLs);
842 for (i = 0; i < WMGetBagItemCount(family->typefaces); i++) {
843 char buffer[256];
844 int top;
845 WMListItem *fitem;
847 face = WMGetFromBag(family->typefaces, i);
849 if (strcmp(face->weight, "medium") == 0) {
850 buffer[0] = 0;
851 } else {
852 if (*face->weight) {
853 strcpy(buffer, face->weight);
854 buffer[0] = toupper(buffer[0]);
855 strcat(buffer, " ");
856 } else {
857 buffer[0] = 0;
861 if (strcmp(face->slant, "r") == 0) {
862 strcat(buffer, "Roman");
863 top = 1;
864 } else if (strcmp(face->slant, "i") == 0) {
865 strcat(buffer, "Italic");
866 } else if (strcmp(face->slant, "o") == 0) {
867 strcat(buffer, "Oblique");
868 } else if (strcmp(face->slant, "ri") == 0) {
869 strcat(buffer, "Rev Italic");
870 } else if (strcmp(face->slant, "ro") == 0) {
871 strcat(buffer, "Rev Oblique");
872 } else {
873 strcat(buffer, face->slant);
876 if (buffer[0] == 0) {
877 strcpy(buffer, "Normal");
880 if (top)
881 fitem = WMInsertListItem(panel->typLs, 0, buffer);
882 else
883 fitem = WMAddListItem(panel->typLs, buffer);
884 fitem->clientData = face;
887 if (oface) {
888 facei = WMFindRowOfListItemWithTitle(panel->typLs, oface);
889 free(oface);
891 if (facei < 0) {
892 facei = 0;
894 WMSelectListItem(panel->typLs, facei);
895 typefaceClick(panel->typLs, panel);
897 if (osize) {
898 sizei = WMFindRowOfListItemWithTitle(panel->sizLs, osize);
900 if (sizei >= 0) {
901 WMSelectListItem(panel->sizLs, sizei);
902 sizeClick(panel->sizLs, panel);
905 if (osize)
906 free(osize);
909 preview(panel);
913 static void
914 typefaceClick(WMWidget *w, void *data)
916 FontPanel *panel = (FontPanel*)data;
917 WMListItem *item;
918 Typeface *face;
919 int i;
920 char buffer[32];
922 char *osize = NULL;
923 int sizei = -1;
925 osize = WMGetTextFieldText(panel->sizT);
928 item = WMGetListSelectedItem(panel->typLs);
929 face = (Typeface*)item->clientData;
931 WMClearList(panel->sizLs);
933 for (i = 0; i < WMGetBagItemCount(face->sizes); i++) {
934 int size;
936 size = (int)WMGetFromBag(face->sizes, i);
938 if (size != 0) {
939 sprintf(buffer, "%i", size);
941 WMAddListItem(panel->sizLs, buffer);
945 if (osize) {
946 sizei = WMFindRowOfListItemWithTitle(panel->sizLs, osize);
948 if (sizei < 0) {
949 sizei = WMFindRowOfListItemWithTitle(panel->sizLs, "12");
951 if (sizei < 0) {
952 sizei = 0;
954 WMSelectListItem(panel->sizLs, sizei);
955 WMSetListPosition(panel->sizLs, sizei);
956 sizeClick(panel->sizLs, panel);
958 if (osize)
959 free(osize);
961 preview(panel);
965 static void
966 sizeClick(WMWidget *w, void *data)
968 FontPanel *panel = (FontPanel*)data;
969 WMListItem *item;
971 item = WMGetListSelectedItem(panel->sizLs);
973 WMSetTextFieldText(panel->sizT, item->text);
975 WMSelectTextFieldRange(panel->sizT, wmkrange(0, strlen(item->text));
977 preview(panel);