replaced free() with wfree() everywhere
[wmaker-crm.git] / WINGs / wfontpanel.c
bloba85d80dd147f50875c42efead5506ca6e75c3ad4
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 static void
141 closeWindow(WMWidget *w, void *data)
143 FontPanel *panel = (FontPanel*)data;
145 WMHideFontPanel(panel);
150 WMFontPanel*
151 WMGetFontPanel(WMScreen *scr)
153 FontPanel *panel;
154 WMColor *dark, *white;
155 WMFont *font;
156 int divThickness;
158 if (scr->sharedFontPanel)
159 return scr->sharedFontPanel;
162 panel = wmalloc(sizeof(FontPanel));
163 memset(panel, 0, sizeof(FontPanel));
165 panel->win = WMCreateWindow(scr, "fontPanel");
166 /* WMSetWidgetBackgroundColor(panel->win, WMWhiteColor(scr));*/
167 WMResizeWidget(panel->win, DEF_WIDTH, DEF_HEIGHT);
168 WMSetWindowMinSize(panel->win, MIN_WIDTH, MIN_HEIGHT);
169 WMSetViewNotifySizeChanges(WMWidgetView(panel->win), True);
171 WMSetWindowCloseAction(panel->win, closeWindow, panel);
173 panel->split = WMCreateSplitView(panel->win);
174 WMResizeWidget(panel->split, DEF_WIDTH, DEF_HEIGHT - BUTTON_SPACE_HEIGHT);
175 WMSetSplitViewConstrainProc(panel->split, splitViewConstrainCallback);
177 divThickness = WMGetSplitViewDividerThickness(panel->split);
179 panel->upperF = WMCreateFrame(panel->win);
180 WMSetFrameRelief(panel->upperF, WRFlat);
181 WMSetViewNotifySizeChanges(WMWidgetView(panel->upperF), True);
182 panel->lowerF = WMCreateFrame(panel->win);
183 /* WMSetWidgetBackgroundColor(panel->lowerF, WMBlackColor(scr));*/
184 WMSetFrameRelief(panel->lowerF, WRFlat);
185 WMSetViewNotifySizeChanges(WMWidgetView(panel->lowerF), True);
187 WMAddSplitViewSubview(panel->split, W_VIEW(panel->upperF));
188 WMAddSplitViewSubview(panel->split, W_VIEW(panel->lowerF));
190 WMResizeWidget(panel->upperF, DEF_WIDTH, DEF_UPPER_HEIGHT);
192 WMResizeWidget(panel->lowerF, DEF_WIDTH, DEF_LOWER_HEIGHT);
194 WMMoveWidget(panel->lowerF, 0, 60+divThickness);
196 white = WMWhiteColor(scr);
197 dark = WMDarkGrayColor(scr);
199 panel->sampleT = WMCreateTextField(panel->upperF);
200 WMResizeWidget(panel->sampleT, DEF_WIDTH - 20, 50);
201 WMMoveWidget(panel->sampleT, 10, 10);
202 WMSetTextFieldText(panel->sampleT, "Test!!!");
204 font = WMBoldSystemFontOfSize(scr, 12);
206 panel->famL = WMCreateLabel(panel->lowerF);
207 WMSetWidgetBackgroundColor(panel->famL, dark);
208 WMSetLabelText(panel->famL, "Family");
209 WMSetLabelFont(panel->famL, font);
210 WMSetLabelTextColor(panel->famL, white);
211 WMSetLabelRelief(panel->famL, WRSunken);
212 WMSetLabelTextAlignment(panel->famL, WACenter);
214 panel->famLs = WMCreateList(panel->lowerF);
215 WMSetListAction(panel->famLs, familyClick, panel);
217 panel->typL = WMCreateLabel(panel->lowerF);
218 WMSetWidgetBackgroundColor(panel->typL, dark);
219 WMSetLabelText(panel->typL, "Typeface");
220 WMSetLabelFont(panel->typL, font);
221 WMSetLabelTextColor(panel->typL, white);
222 WMSetLabelRelief(panel->typL, WRSunken);
223 WMSetLabelTextAlignment(panel->typL, WACenter);
225 panel->typLs = WMCreateList(panel->lowerF);
226 WMSetListAction(panel->typLs, typefaceClick, panel);
228 panel->sizL = WMCreateLabel(panel->lowerF);
229 WMSetWidgetBackgroundColor(panel->sizL, dark);
230 WMSetLabelText(panel->sizL, "Size");
231 WMSetLabelFont(panel->sizL, font);
232 WMSetLabelTextColor(panel->sizL, white);
233 WMSetLabelRelief(panel->sizL, WRSunken);
234 WMSetLabelTextAlignment(panel->sizL, WACenter);
236 panel->sizT = WMCreateTextField(panel->lowerF);
237 /* WMSetTextFieldAlignment(panel->sizT, WARight);*/
239 panel->sizLs = WMCreateList(panel->lowerF);
240 WMSetListAction(panel->sizLs, sizeClick, panel);
242 WMReleaseFont(font);
243 WMReleaseColor(white);
244 WMReleaseColor(dark);
246 panel->setB = WMCreateCommandButton(panel->win);
247 WMResizeWidget(panel->setB, 70, 24);
248 WMMoveWidget(panel->setB, 240, DEF_HEIGHT - (BUTTON_SPACE_HEIGHT-5));
249 WMSetButtonText(panel->setB, "Set");
251 panel->revertB = WMCreateCommandButton(panel->win);
252 WMResizeWidget(panel->revertB, 70, 24);
253 WMMoveWidget(panel->revertB, 80, DEF_HEIGHT - (BUTTON_SPACE_HEIGHT-5));
254 WMSetButtonText(panel->revertB, "Revert");
256 WMRealizeWidget(panel->win);
258 WMMapSubwidgets(panel->upperF);
259 WMMapSubwidgets(panel->lowerF);
260 WMMapSubwidgets(panel->split);
261 WMMapSubwidgets(panel->win);
263 WMUnmapWidget(panel->revertB);
265 arrangeLowerFrame(panel);
267 scr->sharedFontPanel = panel;
270 /* register notification observers */
271 WMAddNotificationObserver(notificationObserver, panel,
272 WMViewSizeDidChangeNotification,
273 WMWidgetView(panel->win));
274 WMAddNotificationObserver(notificationObserver, panel,
275 WMViewSizeDidChangeNotification,
276 WMWidgetView(panel->upperF));
277 WMAddNotificationObserver(notificationObserver, panel,
278 WMViewSizeDidChangeNotification,
279 WMWidgetView(panel->lowerF));
282 listFamilies(scr, panel);
285 return panel;
289 void
290 WMFreeFontPanel(WMFontPanel *panel)
292 if (panel == WMWidgetScreen(panel->win)->sharedFontPanel) {
293 WMWidgetScreen(panel->win)->sharedFontPanel = NULL;
295 WMRemoveNotificationObserver(panel);
296 WMUnmapWidget(panel->win);
297 WMDestroyWidget(panel->win);
298 wfree(panel);
302 void
303 WMShowFontPanel(WMFontPanel *panel)
305 WMMapWidget(panel->win);
309 void
310 WMHideFontPanel(WMFontPanel *panel)
312 WMUnmapWidget(panel->win);
316 void
317 WMSetFontPanelFont(WMFontPanel *panel, WMFont *font)
323 Bool
324 WMSetFontPanelFontName(WMFontPanel *panel, char *fontName)
327 return True;
331 WMFont*
332 WMGetFontPanelFont(WMFontPanel *panel)
334 return WMGetTextFieldFont(panel->sampleT);
338 char*
339 WMGetFontPanelFontName(WMFontPanel *panel)
341 char name[256];
343 getSelectedFont(panel, name);
345 return wstrdup(name);
350 static void
351 arrangeLowerFrame(FontPanel *panel)
353 int width = WMWidgetWidth(panel->lowerF) - 55 - 30;
354 int height = WMWidgetHeight(panel->split) - WMWidgetHeight(panel->upperF);
355 int fw, tw, sw;
357 #define LABEL_HEIGHT 20
359 height -= WMGetSplitViewDividerThickness(panel->split);
362 height -= LABEL_HEIGHT + 8;
364 fw = (125*width) / 235;
365 tw = (110*width) / 235;
366 sw = 55;
368 WMMoveWidget(panel->famL, 10, 0);
369 WMResizeWidget(panel->famL, fw, LABEL_HEIGHT);
371 WMMoveWidget(panel->famLs, 10, 23);
372 WMResizeWidget(panel->famLs, fw, height);
374 WMMoveWidget(panel->typL, 10+fw+3, 0);
375 WMResizeWidget(panel->typL, tw, LABEL_HEIGHT);
377 WMMoveWidget(panel->typLs, 10+fw+3, 23);
378 WMResizeWidget(panel->typLs, tw, height);
380 WMMoveWidget(panel->sizL, 10+fw+3+tw+3, 0);
381 WMResizeWidget(panel->sizL, sw+4, LABEL_HEIGHT);
383 WMMoveWidget(panel->sizT, 10+fw+3+tw+3, 23);
384 WMResizeWidget(panel->sizT, sw+4, 20);
386 WMMoveWidget(panel->sizLs, 10+fw+3+tw+3, 46);
387 WMResizeWidget(panel->sizLs, sw+4, height-23);
393 #define ALL_FONTS_MASK "-*-*-*-*-*-*-*-*-*-*-*-*-*-*"
395 #define FOUNDRY 0
396 #define FAMILY 1
397 #define WEIGHT 2
398 #define SLANT 3
399 #define SETWIDTH 4
400 #define ADD_STYLE 5
401 #define PIXEL_SIZE 6
402 #define POINT_SIZE 7
403 #define RES_X 8
404 #define RES_Y 9
405 #define SPACING 10
406 #define AV_WIDTH 11
407 #define REGISTRY 12
408 #define ENCODING 13
410 #define NUM_FIELDS 14
414 static Bool
415 parseFont(char *font, char values[NUM_FIELDS][256])
417 char *ptr;
418 int part;
419 char buffer[256], *bptr;
421 part = FOUNDRY;
422 ptr = font;
423 ptr++; /* skip first - */
424 bptr = buffer;
425 while (*ptr) {
426 if (*ptr == '-') {
427 *bptr = 0;
428 strcpy(values[part], buffer);
429 bptr = buffer;
430 part++;
431 } else {
432 *bptr++ = *ptr;
434 ptr++;
436 *bptr = 0;
437 strcpy(values[part], buffer);
439 return True;
444 static int
445 isXLFD(char *font, int *length_ret)
447 int c = 0;
449 *length_ret = 0;
450 while (*font) {
451 (*length_ret)++;
452 if (*font++ == '-')
453 c++;
456 return c==NUM_FIELDS;
462 typedef struct {
463 char *weight;
464 char *slant;
466 char *setWidth;
467 char *addStyle;
469 char showSetWidth; /* when duplicated */
470 char showAddStyle; /* when duplicated */
472 WMBag *sizes;
473 } Typeface;
476 typedef struct {
477 char *name;
479 char *foundry;
480 char *registry, *encoding;
482 char showFoundry; /* when duplicated */
483 char showRegistry; /* when duplicated */
485 WMBag *typefaces;
486 } Family;
491 static int
492 compare_int(const void *a, const void *b)
494 int i1 = *(int*)a;
495 int i2 = *(int*)b;
497 if (i1 < i2)
498 return -1;
499 else if (i1 > i2)
500 return 1;
501 else
502 return 0;
507 static void
508 addSizeToTypeface(Typeface *face, int size)
510 if (size == 0) {
511 int j;
513 for (j = 0; j < sizeof(scalableFontSizes)/sizeof(int); j++) {
514 size = scalableFontSizes[j];
516 if (!WMCountInBag(face->sizes, (void*)size)) {
517 WMPutInBag(face->sizes, (void*)size);
520 WMSortBag(face->sizes, compare_int);
521 } else {
522 if (!WMCountInBag(face->sizes, (void*)size)) {
523 WMPutInBag(face->sizes, (void*)size);
524 WMSortBag(face->sizes, compare_int);
531 static void
532 addTypefaceToFamily(Family *family, char fontFields[NUM_FIELDS][256])
534 Typeface *face;
535 int i;
537 if (family->typefaces) {
538 for (i = 0; i < WMGetBagItemCount(family->typefaces); i++) {
539 int size;
541 face = WMGetFromBag(family->typefaces, i);
543 if (strcmp(face->weight, fontFields[WEIGHT]) != 0) {
544 continue;
546 if (strcmp(face->slant, fontFields[SLANT]) != 0) {
547 continue;
550 size = atoi(fontFields[PIXEL_SIZE]);
552 addSizeToTypeface(face, size);
554 return;
556 } else {
557 family->typefaces = WMCreateBag(4);
560 face = wmalloc(sizeof(Typeface));
561 memset(face, 0, sizeof(Typeface));
563 face->weight = wstrdup(fontFields[WEIGHT]);
564 face->slant = wstrdup(fontFields[SLANT]);
565 face->setWidth = wstrdup(fontFields[SETWIDTH]);
566 face->addStyle = wstrdup(fontFields[ADD_STYLE]);
568 face->sizes = WMCreateBag(4);
569 addSizeToTypeface(face, atoi(fontFields[PIXEL_SIZE]));
571 WMPutInBag(family->typefaces, face);
577 * families (same family name) (Hashtable of family -> bag)
578 * registries (same family but different registries)
582 static void
583 addFontToFamily(WMHashTable *families, char fontFields[NUM_FIELDS][256])
585 int i;
586 Family *fam;
587 WMBag *family;
590 family = WMHashGet(families, fontFields[FAMILY]);
592 if (family) {
593 /* look for same encoding/registry and foundry */
594 for (i = 0; i < WMGetBagItemCount(family); i++) {
595 int enc, reg, found;
597 fam = WMGetFromBag(family, i);
599 enc = (strcmp(fam->encoding, fontFields[ENCODING]) == 0);
600 reg = (strcmp(fam->registry, fontFields[REGISTRY]) == 0);
601 found = (strcmp(fam->foundry, fontFields[FOUNDRY]) == 0);
603 if (enc && reg && found) {
604 addTypefaceToFamily(fam, fontFields);
605 return;
608 /* look for same encoding/registry */
609 for (i = 0; i < WMGetBagItemCount(family); i++) {
610 int enc, reg;
612 fam = WMGetFromBag(family, i);
614 enc = (strcmp(fam->encoding, fontFields[ENCODING]) == 0);
615 reg = (strcmp(fam->registry, fontFields[REGISTRY]) == 0);
617 if (enc && reg) {
618 /* has the same encoding, but the foundry is different */
619 fam->showFoundry = 1;
621 fam = wmalloc(sizeof(Family));
622 memset(fam, 0, sizeof(Family));
624 fam->name = wstrdup(fontFields[FAMILY]);
625 fam->foundry = wstrdup(fontFields[FOUNDRY]);
626 fam->registry = wstrdup(fontFields[REGISTRY]);
627 fam->encoding = wstrdup(fontFields[ENCODING]);
628 fam->showFoundry = 1;
630 addTypefaceToFamily(fam, fontFields);
632 WMPutInBag(family, fam);
633 return;
636 /* look for same foundry */
637 for (i = 0; i < WMGetBagItemCount(family); i++) {
638 int found;
640 fam = WMGetFromBag(family, i);
642 found = (strcmp(fam->foundry, fontFields[FOUNDRY]) == 0);
644 if (found) {
645 /* has the same foundry, but encoding is different */
646 fam->showRegistry = 1;
648 fam = wmalloc(sizeof(Family));
649 memset(fam, 0, sizeof(Family));
651 fam->name = wstrdup(fontFields[FAMILY]);
652 fam->foundry = wstrdup(fontFields[FOUNDRY]);
653 fam->registry = wstrdup(fontFields[REGISTRY]);
654 fam->encoding = wstrdup(fontFields[ENCODING]);
655 fam->showRegistry = 1;
657 addTypefaceToFamily(fam, fontFields);
659 WMPutInBag(family, fam);
660 return;
663 /* foundry and encoding do not match anything known */
664 fam = wmalloc(sizeof(Family));
665 memset(fam, 0, sizeof(Family));
667 fam->name = wstrdup(fontFields[FAMILY]);
668 fam->foundry = wstrdup(fontFields[FOUNDRY]);
669 fam->registry = wstrdup(fontFields[REGISTRY]);
670 fam->encoding = wstrdup(fontFields[ENCODING]);
671 fam->showFoundry = 1;
672 fam->showRegistry = 1;
674 addTypefaceToFamily(fam, fontFields);
676 WMPutInBag(family, fam);
677 return;
680 family = WMCreateBag(8);
682 fam = wmalloc(sizeof(Family));
683 memset(fam, 0, sizeof(Family));
685 fam->name = wstrdup(fontFields[FAMILY]);
686 fam->foundry = wstrdup(fontFields[FOUNDRY]);
687 fam->registry = wstrdup(fontFields[REGISTRY]);
688 fam->encoding = wstrdup(fontFields[ENCODING]);
690 addTypefaceToFamily(fam, fontFields);
692 WMPutInBag(family, fam);
694 WMHashInsert(families, fam->name, family);
699 static void
700 listFamilies(WMScreen *scr, WMFontPanel *panel)
702 char **fontList;
703 int count;
704 int i;
705 WMHashTable *families = WMCreateHashTable(WMStringPointerHashCallbacks);
706 char fields[NUM_FIELDS][256];
707 WMHashEnumerator enumer;
708 WMBag *bag;
710 fontList = XListFonts(scr->display, ALL_FONTS_MASK, MAX_FONTS_TO_RETRIEVE,
711 &count);
712 if (!fontList) {
713 WMRunAlertPanel(scr, panel->win, "Error",
714 "Could not retrieve font list", "OK", NULL, NULL);
715 return;
718 for (i = 0; i < count; i++) {
719 int fname_len;
721 if (!isXLFD(fontList[i], &fname_len)) {
722 *fontList[i] = '\0';
723 continue;
725 if (fname_len > 255) {
726 wwarning("font name %s is longer than 256, which is invalid.",
727 fontList[i]);
728 *fontList[i] = '\0';
729 continue;
731 if (!parseFont(fontList[i], fields)) {
732 *fontList[i] = '\0';
733 continue;
735 addFontToFamily(families, fields);
738 enumer = WMEnumerateHashTable(families);
740 while ((bag = WMNextHashEnumeratorItem(&enumer))) {
741 int i;
742 Family *fam;
743 char buffer[256];
744 WMListItem *item;
746 for (i = 0; i < WMGetBagItemCount(bag); i++) {
747 fam = WMGetFromBag(bag, i);
749 strcpy(buffer, fam->name);
751 if (fam->showFoundry) {
752 strcat(buffer, " ");
753 strcat(buffer, fam->foundry);
754 strcat(buffer, " ");
756 if (fam->showRegistry) {
757 strcat(buffer, " (");
758 strcat(buffer, fam->registry);
759 strcat(buffer, "-");
760 strcat(buffer, fam->encoding);
761 strcat(buffer, ")");
763 item = WMAddListItem(panel->famLs, buffer);
765 item->clientData = fam;
767 WMFreeBag(bag);
769 WMSortListItems(panel->famLs);
771 WMFreeHashTable(families);
775 static void
776 getSelectedFont(FontPanel *panel, char buffer[])
778 WMListItem *item;
779 Family *family;
780 Typeface *face;
781 char *size;
784 item = WMGetListSelectedItem(panel->famLs);
785 if (!item)
786 return;
787 family = (Family*)item->clientData;
789 item = WMGetListSelectedItem(panel->typLs);
790 if (!item)
791 return;
792 face = (Typeface*)item->clientData;
794 size = WMGetTextFieldText(panel->sizT);
796 sprintf(buffer, "-%s-%s-%s-%s-%s-%s-%s-*-*-*-*-*-%s-%s",
797 family->foundry,
798 family->name,
799 face->weight,
800 face->slant,
801 face->setWidth,
802 face->addStyle,
803 size,
804 family->registry,
805 family->encoding);
810 static void
811 preview(FontPanel *panel)
813 char buffer[256];
814 WMFont *font;
816 getSelectedFont(panel, buffer);
818 font = WMCreateFont(WMWidgetScreen(panel->win), buffer);
819 if (font) {
820 WMSetTextFieldFont(panel->sampleT, font);
821 WMReleaseFont(font);
827 static void
828 familyClick(WMWidget *w, void *data)
830 WMList *lPtr = (WMList*)w;
831 WMListItem *item;
832 Family *family;
833 FontPanel *panel = (FontPanel*)data;
834 Typeface *face;
835 int i;
836 /* current typeface and size */
837 char *oface = NULL;
838 char *osize = NULL;
839 int facei = -1;
840 int sizei = -1;
842 /* must try to keep the same typeface and size for the new family */
843 item = WMGetListSelectedItem(panel->typLs);
844 if (item)
845 oface = wstrdup(item->text);
847 osize = WMGetTextFieldText(panel->sizT);
850 item = WMGetListSelectedItem(lPtr);
851 family = (Family*)item->clientData;
853 WMClearList(panel->typLs);
855 for (i = 0; i < WMGetBagItemCount(family->typefaces); i++) {
856 char buffer[256];
857 int top;
858 WMListItem *fitem;
860 face = WMGetFromBag(family->typefaces, i);
862 if (strcmp(face->weight, "medium") == 0) {
863 buffer[0] = 0;
864 } else {
865 if (*face->weight) {
866 strcpy(buffer, face->weight);
867 buffer[0] = toupper(buffer[0]);
868 strcat(buffer, " ");
869 } else {
870 buffer[0] = 0;
874 if (strcmp(face->slant, "r") == 0) {
875 strcat(buffer, "Roman");
876 top = 1;
877 } else if (strcmp(face->slant, "i") == 0) {
878 strcat(buffer, "Italic");
879 } else if (strcmp(face->slant, "o") == 0) {
880 strcat(buffer, "Oblique");
881 } else if (strcmp(face->slant, "ri") == 0) {
882 strcat(buffer, "Rev Italic");
883 } else if (strcmp(face->slant, "ro") == 0) {
884 strcat(buffer, "Rev Oblique");
885 } else {
886 strcat(buffer, face->slant);
889 if (buffer[0] == 0) {
890 strcpy(buffer, "Normal");
893 if (top)
894 fitem = WMInsertListItem(panel->typLs, 0, buffer);
895 else
896 fitem = WMAddListItem(panel->typLs, buffer);
897 fitem->clientData = face;
900 if (oface) {
901 facei = WMFindRowOfListItemWithTitle(panel->typLs, oface);
902 wfree(oface);
904 if (facei < 0) {
905 facei = 0;
907 WMSelectListItem(panel->typLs, facei);
908 typefaceClick(panel->typLs, panel);
910 if (osize) {
911 sizei = WMFindRowOfListItemWithTitle(panel->sizLs, osize);
913 if (sizei >= 0) {
914 WMSelectListItem(panel->sizLs, sizei);
915 sizeClick(panel->sizLs, panel);
918 if (osize)
919 wfree(osize);
922 preview(panel);
926 static void
927 typefaceClick(WMWidget *w, void *data)
929 FontPanel *panel = (FontPanel*)data;
930 WMListItem *item;
931 Typeface *face;
932 int i;
933 char buffer[32];
935 char *osize = NULL;
936 int sizei = -1;
938 osize = WMGetTextFieldText(panel->sizT);
941 item = WMGetListSelectedItem(panel->typLs);
942 face = (Typeface*)item->clientData;
944 WMClearList(panel->sizLs);
946 for (i = 0; i < WMGetBagItemCount(face->sizes); i++) {
947 int size;
949 size = (int)WMGetFromBag(face->sizes, i);
951 if (size != 0) {
952 sprintf(buffer, "%i", size);
954 WMAddListItem(panel->sizLs, buffer);
958 if (osize) {
959 sizei = WMFindRowOfListItemWithTitle(panel->sizLs, osize);
961 if (sizei < 0) {
962 sizei = WMFindRowOfListItemWithTitle(panel->sizLs, "12");
964 if (sizei < 0) {
965 sizei = 0;
967 WMSelectListItem(panel->sizLs, sizei);
968 WMSetListPosition(panel->sizLs, sizei);
969 sizeClick(panel->sizLs, panel);
971 if (osize)
972 wfree(osize);
974 preview(panel);
978 static void
979 sizeClick(WMWidget *w, void *data)
981 FontPanel *panel = (FontPanel*)data;
982 WMListItem *item;
984 item = WMGetListSelectedItem(panel->sizLs);
986 WMSetTextFieldText(panel->sizT, item->text);
988 WMSelectTextFieldRange(panel->sizT, wmkrange(0, strlen(item->text)));
990 preview(panel);