added cback for set button in fontpanel
[wmaker-crm.git] / WINGs / wfontpanel.c
blob868522a429d36907eba811e62bbecb6656dfe692
5 #include "WINGsP.h"
6 #include "WUtil.h"
7 #include "wconfig.h"
9 #include <ctype.h>
10 #include <string.h>
14 /* XXX TODO */
15 char *WMFontPanelFontChangedNotification = "WMFontPanelFontChangedNotification";
20 typedef struct W_FontPanel {
21 WMWindow *win;
23 WMFrame *upperF;
24 WMTextField *sampleT;
26 WMSplitView *split;
28 WMFrame *lowerF;
29 WMLabel *famL;
30 WMList *famLs;
31 WMLabel *typL;
32 WMList *typLs;
33 WMLabel *sizL;
34 WMTextField *sizT;
35 WMList *sizLs;
37 WMAction2 *action;
38 void *data;
40 WMButton *revertB;
41 WMButton *setB;
43 WMPropList *fdb;
44 } FontPanel;
47 #define MIN_UPPER_HEIGHT 20
48 #define MIN_LOWER_HEIGHT 140
51 #define MAX_FONTS_TO_RETRIEVE 2000
53 #define BUTTON_SPACE_HEIGHT 40
55 #define MIN_WIDTH 250
56 #define MIN_HEIGHT (MIN_UPPER_HEIGHT+MIN_LOWER_HEIGHT+BUTTON_SPACE_HEIGHT)
58 #define DEF_UPPER_HEIGHT 60
59 #define DEF_LOWER_HEIGHT 310
61 #define DEF_WIDTH 320
62 #define DEF_HEIGHT (DEF_UPPER_HEIGHT+DEF_LOWER_HEIGHT)
67 static int scalableFontSizes[] = {
68 8,
69 10,
70 11,
71 12,
72 14,
73 16,
74 18,
75 20,
76 24,
77 36,
78 48,
84 static void getSelectedFont(FontPanel *panel, char buffer[], int bufsize);
87 static void arrangeLowerFrame(FontPanel *panel);
89 static void familyClick(WMWidget *, void *);
90 static void typefaceClick(WMWidget *, void *);
91 static void sizeClick(WMWidget *, void *);
94 static void listFamilies(WMScreen *scr, WMFontPanel *panel);
97 static void
98 splitViewConstrainCallback(WMSplitView *sPtr, int indView, int *min, int *max)
100 if (indView == 0)
101 *min = MIN_UPPER_HEIGHT;
102 else
103 *min = MIN_LOWER_HEIGHT;
106 static void
107 notificationObserver(void *self, WMNotification *notif)
109 WMFontPanel *panel = (WMFontPanel*)self;
110 void *object = WMGetNotificationObject(notif);
112 if (WMGetNotificationName(notif) == WMViewSizeDidChangeNotification) {
113 if (object == WMWidgetView(panel->win)) {
114 int h = WMWidgetHeight(panel->win);
115 int w = WMWidgetWidth(panel->win);
117 WMResizeWidget(panel->split, w, h-BUTTON_SPACE_HEIGHT);
119 WMMoveWidget(panel->setB, w-80, h-(BUTTON_SPACE_HEIGHT-5));
121 WMMoveWidget(panel->revertB, w-240, h-(BUTTON_SPACE_HEIGHT-5));
123 } else if (object == WMWidgetView(panel->upperF)) {
125 if (WMWidgetHeight(panel->upperF) < MIN_UPPER_HEIGHT) {
126 WMResizeWidget(panel->upperF, WMWidgetWidth(panel->upperF),
127 MIN_UPPER_HEIGHT);
128 } else {
129 WMResizeWidget(panel->sampleT, WMWidgetWidth(panel->upperF)-20,
130 WMWidgetHeight(panel->upperF)-10);
133 } else if (object == WMWidgetView(panel->lowerF)) {
135 if (WMWidgetHeight(panel->lowerF) < MIN_LOWER_HEIGHT) {
136 WMResizeWidget(panel->upperF, WMWidgetWidth(panel->upperF),
137 MIN_UPPER_HEIGHT);
139 WMMoveWidget(panel->lowerF, 0, WMWidgetHeight(panel->upperF)
140 + WMGetSplitViewDividerThickness(panel->split));
142 WMResizeWidget(panel->lowerF, WMWidgetWidth(panel->lowerF),
143 WMWidgetWidth(panel->split) - MIN_UPPER_HEIGHT
144 - WMGetSplitViewDividerThickness(panel->split));
145 } else {
146 arrangeLowerFrame(panel);
153 static void
154 closeWindow(WMWidget *w, void *data)
156 FontPanel *panel = (FontPanel*)data;
158 WMHideFontPanel(panel);
164 static void
165 setClickedAction(WMWidget *w, void *data)
167 FontPanel *panel = (FontPanel*)data;
169 if (panel->action)
170 (*panel->action)(panel, panel->data);
174 static void
175 revertClickedAction(WMWidget *w, void *data)
177 FontPanel *panel = (FontPanel*)data;
178 /* XXX TODO */
183 WMFontPanel*
184 WMGetFontPanel(WMScreen *scr)
186 FontPanel *panel;
187 WMColor *dark, *white;
188 WMFont *font;
189 int divThickness;
191 if (scr->sharedFontPanel)
192 return scr->sharedFontPanel;
195 panel = wmalloc(sizeof(FontPanel));
196 memset(panel, 0, sizeof(FontPanel));
198 panel->win = WMCreateWindow(scr, "fontPanel");
199 /* WMSetWidgetBackgroundColor(panel->win, WMWhiteColor(scr));*/
200 WMResizeWidget(panel->win, DEF_WIDTH, DEF_HEIGHT);
201 WMSetWindowMinSize(panel->win, MIN_WIDTH, MIN_HEIGHT);
202 WMSetViewNotifySizeChanges(WMWidgetView(panel->win), True);
204 WMSetWindowCloseAction(panel->win, closeWindow, panel);
206 panel->split = WMCreateSplitView(panel->win);
207 WMResizeWidget(panel->split, DEF_WIDTH, DEF_HEIGHT - BUTTON_SPACE_HEIGHT);
208 WMSetSplitViewConstrainProc(panel->split, splitViewConstrainCallback);
210 divThickness = WMGetSplitViewDividerThickness(panel->split);
212 panel->upperF = WMCreateFrame(panel->win);
213 WMSetFrameRelief(panel->upperF, WRFlat);
214 WMSetViewNotifySizeChanges(WMWidgetView(panel->upperF), True);
215 panel->lowerF = WMCreateFrame(panel->win);
216 /* WMSetWidgetBackgroundColor(panel->lowerF, WMBlackColor(scr));*/
217 WMSetFrameRelief(panel->lowerF, WRFlat);
218 WMSetViewNotifySizeChanges(WMWidgetView(panel->lowerF), True);
220 WMAddSplitViewSubview(panel->split, W_VIEW(panel->upperF));
221 WMAddSplitViewSubview(panel->split, W_VIEW(panel->lowerF));
223 WMResizeWidget(panel->upperF, DEF_WIDTH, DEF_UPPER_HEIGHT);
225 WMResizeWidget(panel->lowerF, DEF_WIDTH, DEF_LOWER_HEIGHT);
227 WMMoveWidget(panel->lowerF, 0, 60+divThickness);
229 white = WMWhiteColor(scr);
230 dark = WMDarkGrayColor(scr);
232 panel->sampleT = WMCreateTextField(panel->upperF);
233 WMResizeWidget(panel->sampleT, DEF_WIDTH - 20, 50);
234 WMMoveWidget(panel->sampleT, 10, 10);
235 WMSetTextFieldText(panel->sampleT, _("Test!!!"));
237 font = WMBoldSystemFontOfSize(scr, 12);
239 panel->famL = WMCreateLabel(panel->lowerF);
240 WMSetWidgetBackgroundColor(panel->famL, dark);
241 WMSetLabelText(panel->famL, _("Family"));
242 WMSetLabelFont(panel->famL, font);
243 WMSetLabelTextColor(panel->famL, white);
244 WMSetLabelRelief(panel->famL, WRSunken);
245 WMSetLabelTextAlignment(panel->famL, WACenter);
247 panel->famLs = WMCreateList(panel->lowerF);
248 WMSetListAction(panel->famLs, familyClick, panel);
250 panel->typL = WMCreateLabel(panel->lowerF);
251 WMSetWidgetBackgroundColor(panel->typL, dark);
252 WMSetLabelText(panel->typL, _("Typeface"));
253 WMSetLabelFont(panel->typL, font);
254 WMSetLabelTextColor(panel->typL, white);
255 WMSetLabelRelief(panel->typL, WRSunken);
256 WMSetLabelTextAlignment(panel->typL, WACenter);
258 panel->typLs = WMCreateList(panel->lowerF);
259 WMSetListAction(panel->typLs, typefaceClick, panel);
261 panel->sizL = WMCreateLabel(panel->lowerF);
262 WMSetWidgetBackgroundColor(panel->sizL, dark);
263 WMSetLabelText(panel->sizL, _("Size"));
264 WMSetLabelFont(panel->sizL, font);
265 WMSetLabelTextColor(panel->sizL, white);
266 WMSetLabelRelief(panel->sizL, WRSunken);
267 WMSetLabelTextAlignment(panel->sizL, WACenter);
269 panel->sizT = WMCreateTextField(panel->lowerF);
270 /* WMSetTextFieldAlignment(panel->sizT, WARight);*/
272 panel->sizLs = WMCreateList(panel->lowerF);
273 WMSetListAction(panel->sizLs, sizeClick, panel);
275 WMReleaseFont(font);
276 WMReleaseColor(white);
277 WMReleaseColor(dark);
279 panel->setB = WMCreateCommandButton(panel->win);
280 WMResizeWidget(panel->setB, 70, 24);
281 WMMoveWidget(panel->setB, 240, DEF_HEIGHT - (BUTTON_SPACE_HEIGHT-5));
282 WMSetButtonText(panel->setB, _("Set"));
283 WMSetButtonAction(panel->setB, setClickedAction, panel);
285 panel->revertB = WMCreateCommandButton(panel->win);
286 WMResizeWidget(panel->revertB, 70, 24);
287 WMMoveWidget(panel->revertB, 80, DEF_HEIGHT - (BUTTON_SPACE_HEIGHT-5));
288 WMSetButtonText(panel->revertB, _("Revert"));
289 WMSetButtonAction(panel->revertB, revertClickedAction, panel);
292 WMRealizeWidget(panel->win);
294 WMMapSubwidgets(panel->upperF);
295 WMMapSubwidgets(panel->lowerF);
296 WMMapSubwidgets(panel->split);
297 WMMapSubwidgets(panel->win);
299 WMUnmapWidget(panel->revertB);
301 arrangeLowerFrame(panel);
303 scr->sharedFontPanel = panel;
306 /* register notification observers */
307 WMAddNotificationObserver(notificationObserver, panel,
308 WMViewSizeDidChangeNotification,
309 WMWidgetView(panel->win));
310 WMAddNotificationObserver(notificationObserver, panel,
311 WMViewSizeDidChangeNotification,
312 WMWidgetView(panel->upperF));
313 WMAddNotificationObserver(notificationObserver, panel,
314 WMViewSizeDidChangeNotification,
315 WMWidgetView(panel->lowerF));
318 listFamilies(scr, panel);
321 return panel;
325 void
326 WMFreeFontPanel(WMFontPanel *panel)
328 if (panel == WMWidgetScreen(panel->win)->sharedFontPanel) {
329 WMWidgetScreen(panel->win)->sharedFontPanel = NULL;
331 WMRemoveNotificationObserver(panel);
332 WMUnmapWidget(panel->win);
333 WMDestroyWidget(panel->win);
334 wfree(panel);
338 void
339 WMShowFontPanel(WMFontPanel *panel)
341 WMMapWidget(panel->win);
345 void
346 WMHideFontPanel(WMFontPanel *panel)
348 WMUnmapWidget(panel->win);
352 void
353 WMSetFontPanelFont(WMFontPanel *panel, WMFont *font)
359 Bool
360 WMSetFontPanelFontName(WMFontPanel *panel, char *fontName)
363 return True;
367 WMFont*
368 WMGetFontPanelFont(WMFontPanel *panel)
370 return WMGetTextFieldFont(panel->sampleT);
374 char*
375 WMGetFontPanelFontName(WMFontPanel *panel)
377 char name[512];
379 getSelectedFont(panel, name, sizeof(name));
381 return wstrdup(name);
386 void
387 WMSetFontPanelAction(WMFontPanel *panel, WMAction2 *action, void *data)
389 panel->action = action;
390 panel->actionData = data;
397 static void
398 arrangeLowerFrame(FontPanel *panel)
400 int width = WMWidgetWidth(panel->lowerF) - 55 - 30;
401 int height = WMWidgetHeight(panel->split) - WMWidgetHeight(panel->upperF);
402 int fw, tw, sw;
404 #define LABEL_HEIGHT 20
406 height -= WMGetSplitViewDividerThickness(panel->split);
409 height -= LABEL_HEIGHT + 8;
411 fw = (125*width) / 235;
412 tw = (110*width) / 235;
413 sw = 55;
415 WMMoveWidget(panel->famL, 10, 0);
416 WMResizeWidget(panel->famL, fw, LABEL_HEIGHT);
418 WMMoveWidget(panel->famLs, 10, 23);
419 WMResizeWidget(panel->famLs, fw, height);
421 WMMoveWidget(panel->typL, 10+fw+3, 0);
422 WMResizeWidget(panel->typL, tw, LABEL_HEIGHT);
424 WMMoveWidget(panel->typLs, 10+fw+3, 23);
425 WMResizeWidget(panel->typLs, tw, height);
427 WMMoveWidget(panel->sizL, 10+fw+3+tw+3, 0);
428 WMResizeWidget(panel->sizL, sw+4, LABEL_HEIGHT);
430 WMMoveWidget(panel->sizT, 10+fw+3+tw+3, 23);
431 WMResizeWidget(panel->sizT, sw+4, 20);
433 WMMoveWidget(panel->sizLs, 10+fw+3+tw+3, 46);
434 WMResizeWidget(panel->sizLs, sw+4, height-23);
440 #define ALL_FONTS_MASK "-*-*-*-*-*-*-*-*-*-*-*-*-*-*"
442 #define FOUNDRY 0
443 #define FAMILY 1
444 #define WEIGHT 2
445 #define SLANT 3
446 #define SETWIDTH 4
447 #define ADD_STYLE 5
448 #define PIXEL_SIZE 6
449 #define POINT_SIZE 7
450 #define RES_X 8
451 #define RES_Y 9
452 #define SPACING 10
453 #define AV_WIDTH 11
454 #define REGISTRY 12
455 #define ENCODING 13
457 #define NUM_FIELDS 14
461 static Bool
462 parseFont(char *font, char values[NUM_FIELDS][256])
464 char *ptr;
465 int part;
466 char buffer[256], *bptr;
468 part = FOUNDRY;
469 ptr = font;
470 ptr++; /* skip first - */
471 bptr = buffer;
472 while (*ptr) {
473 if (*ptr == '-') {
474 *bptr = 0;
475 strcpy(values[part], buffer);
476 bptr = buffer;
477 part++;
478 } else {
479 *bptr++ = *ptr;
481 ptr++;
483 *bptr = 0;
484 strcpy(values[part], buffer);
486 return True;
491 static int
492 isXLFD(char *font, int *length_ret)
494 int c = 0;
496 *length_ret = 0;
497 while (*font) {
498 (*length_ret)++;
499 if (*font++ == '-')
500 c++;
503 return c==NUM_FIELDS;
509 typedef struct {
510 char *weight;
511 char *slant;
513 char *setWidth;
514 char *addStyle;
516 char showSetWidth; /* when duplicated */
517 char showAddStyle; /* when duplicated */
519 WMArray *sizes;
520 } Typeface;
523 typedef struct {
524 char *name;
526 char *foundry;
527 char *registry, *encoding;
529 char showFoundry; /* when duplicated */
530 char showRegistry; /* when duplicated */
532 WMArray *typefaces;
533 } Family;
538 static int
539 compare_int(const void *a, const void *b)
541 int i1 = *(int*)a;
542 int i2 = *(int*)b;
544 if (i1 < i2)
545 return -1;
546 else if (i1 > i2)
547 return 1;
548 else
549 return 0;
554 static void
555 addSizeToTypeface(Typeface *face, int size)
557 if (size == 0) {
558 int j;
560 for (j = 0; j < sizeof(scalableFontSizes)/sizeof(int); j++) {
561 size = scalableFontSizes[j];
563 if (!WMCountInArray(face->sizes, (void*)size)) {
564 WMAddToArray(face->sizes, (void*)size);
567 WMSortArray(face->sizes, compare_int);
568 } else {
569 if (!WMCountInArray(face->sizes, (void*)size)) {
570 WMAddToArray(face->sizes, (void*)size);
571 WMSortArray(face->sizes, compare_int);
578 static void
579 addTypefaceToFamily(Family *family, char fontFields[NUM_FIELDS][256])
581 Typeface *face;
582 WMArrayIterator i;
584 if (family->typefaces) {
585 WM_ITERATE_ARRAY(family->typefaces, face, i) {
586 int size;
588 if (strcmp(face->weight, fontFields[WEIGHT]) != 0) {
589 continue;
591 if (strcmp(face->slant, fontFields[SLANT]) != 0) {
592 continue;
595 size = atoi(fontFields[PIXEL_SIZE]);
597 addSizeToTypeface(face, size);
599 return;
601 } else {
602 family->typefaces = WMCreateArray(4);
605 face = wmalloc(sizeof(Typeface));
606 memset(face, 0, sizeof(Typeface));
608 face->weight = wstrdup(fontFields[WEIGHT]);
609 face->slant = wstrdup(fontFields[SLANT]);
610 face->setWidth = wstrdup(fontFields[SETWIDTH]);
611 face->addStyle = wstrdup(fontFields[ADD_STYLE]);
613 face->sizes = WMCreateArray(4);
614 addSizeToTypeface(face, atoi(fontFields[PIXEL_SIZE]));
616 WMAddToArray(family->typefaces, face);
621 * families (same family name) (Hashtable of family -> array)
622 * registries (same family but different registries)
626 static void
627 addFontToFamily(WMHashTable *families, char fontFields[NUM_FIELDS][256])
629 WMArrayIterator i;
630 Family *fam;
631 WMArray *family;
634 family = WMHashGet(families, fontFields[FAMILY]);
636 if (family) {
637 /* look for same encoding/registry and foundry */
638 WM_ITERATE_ARRAY(family, fam, i) {
639 int enc, reg, found;
641 enc = (strcmp(fam->encoding, fontFields[ENCODING]) == 0);
642 reg = (strcmp(fam->registry, fontFields[REGISTRY]) == 0);
643 found = (strcmp(fam->foundry, fontFields[FOUNDRY]) == 0);
645 if (enc && reg && found) {
646 addTypefaceToFamily(fam, fontFields);
647 return;
650 /* look for same encoding/registry */
651 WM_ITERATE_ARRAY(family, fam, i) {
652 int enc, reg;
654 enc = (strcmp(fam->encoding, fontFields[ENCODING]) == 0);
655 reg = (strcmp(fam->registry, fontFields[REGISTRY]) == 0);
657 if (enc && reg) {
658 /* has the same encoding, but the foundry is different */
659 fam->showFoundry = 1;
661 fam = wmalloc(sizeof(Family));
662 memset(fam, 0, sizeof(Family));
664 fam->name = wstrdup(fontFields[FAMILY]);
665 fam->foundry = wstrdup(fontFields[FOUNDRY]);
666 fam->registry = wstrdup(fontFields[REGISTRY]);
667 fam->encoding = wstrdup(fontFields[ENCODING]);
668 fam->showFoundry = 1;
670 addTypefaceToFamily(fam, fontFields);
672 WMAddToArray(family, fam);
673 return;
676 /* look for same foundry */
677 WM_ITERATE_ARRAY(family, fam, i) {
678 int found;
680 found = (strcmp(fam->foundry, fontFields[FOUNDRY]) == 0);
682 if (found) {
683 /* has the same foundry, but encoding is different */
684 fam->showRegistry = 1;
686 fam = wmalloc(sizeof(Family));
687 memset(fam, 0, sizeof(Family));
689 fam->name = wstrdup(fontFields[FAMILY]);
690 fam->foundry = wstrdup(fontFields[FOUNDRY]);
691 fam->registry = wstrdup(fontFields[REGISTRY]);
692 fam->encoding = wstrdup(fontFields[ENCODING]);
693 fam->showRegistry = 1;
695 addTypefaceToFamily(fam, fontFields);
697 WMAddToArray(family, fam);
698 return;
701 /* foundry and encoding do not match anything known */
702 fam = wmalloc(sizeof(Family));
703 memset(fam, 0, sizeof(Family));
705 fam->name = wstrdup(fontFields[FAMILY]);
706 fam->foundry = wstrdup(fontFields[FOUNDRY]);
707 fam->registry = wstrdup(fontFields[REGISTRY]);
708 fam->encoding = wstrdup(fontFields[ENCODING]);
709 fam->showFoundry = 1;
710 fam->showRegistry = 1;
712 addTypefaceToFamily(fam, fontFields);
714 WMAddToArray(family, fam);
715 return;
718 family = WMCreateArray(8);
720 fam = wmalloc(sizeof(Family));
721 memset(fam, 0, sizeof(Family));
723 fam->name = wstrdup(fontFields[FAMILY]);
724 fam->foundry = wstrdup(fontFields[FOUNDRY]);
725 fam->registry = wstrdup(fontFields[REGISTRY]);
726 fam->encoding = wstrdup(fontFields[ENCODING]);
728 addTypefaceToFamily(fam, fontFields);
730 WMAddToArray(family, fam);
732 WMHashInsert(families, fam->name, family);
737 static void
738 listFamilies(WMScreen *scr, WMFontPanel *panel)
740 char **fontList;
741 WMHashTable *families;
742 char fields[NUM_FIELDS][256];
743 WMHashEnumerator enumer;
744 WMArray *array;
745 int i, count;
747 fontList = XListFonts(scr->display, ALL_FONTS_MASK, MAX_FONTS_TO_RETRIEVE,
748 &count);
749 if (!fontList) {
750 WMRunAlertPanel(scr, panel->win, _("Error"),
751 _("Could not retrieve font list"), _("OK"), NULL, NULL);
752 return;
755 families = WMCreateHashTable(WMStringPointerHashCallbacks);
757 for (i = 0; i < count; i++) {
758 int fname_len;
760 if (!isXLFD(fontList[i], &fname_len)) {
761 *fontList[i] = '\0';
762 continue;
764 if (fname_len > 255) {
765 wwarning(_("font name %s is longer than 256, which is invalid."),
766 fontList[i]);
767 *fontList[i] = '\0';
768 continue;
770 if (!parseFont(fontList[i], fields)) {
771 *fontList[i] = '\0';
772 continue;
774 addFontToFamily(families, fields);
777 enumer = WMEnumerateHashTable(families);
779 while ((array = WMNextHashEnumeratorItem(&enumer))) {
780 WMArrayIterator i;
781 Family *fam;
782 char buffer[256];
783 WMListItem *item;
785 WM_ITERATE_ARRAY(array, fam, i) {
786 strcpy(buffer, fam->name);
788 if (fam->showFoundry) {
789 strcat(buffer, " ");
790 strcat(buffer, fam->foundry);
791 strcat(buffer, " ");
793 if (fam->showRegistry) {
794 strcat(buffer, " (");
795 strcat(buffer, fam->registry);
796 strcat(buffer, "-");
797 strcat(buffer, fam->encoding);
798 strcat(buffer, ")");
800 item = WMAddListItem(panel->famLs, buffer);
802 item->clientData = fam;
804 WMFreeArray(array);
806 WMSortListItems(panel->famLs);
808 WMFreeHashTable(families);
812 static void
813 getSelectedFont(FontPanel *panel, char buffer[], int bufsize)
815 WMListItem *item;
816 Family *family;
817 Typeface *face;
818 char *size;
821 item = WMGetListSelectedItem(panel->famLs);
822 if (!item)
823 return;
824 family = (Family*)item->clientData;
826 item = WMGetListSelectedItem(panel->typLs);
827 if (!item)
828 return;
829 face = (Typeface*)item->clientData;
831 size = WMGetTextFieldText(panel->sizT);
833 snprintf(buffer, bufsize, "-%s-%s-%s-%s-%s-%s-%s-*-*-*-*-*-%s-%s",
834 family->foundry,
835 family->name,
836 face->weight,
837 face->slant,
838 face->setWidth,
839 face->addStyle,
840 size,
841 family->registry,
842 family->encoding);
847 static void
848 preview(FontPanel *panel)
850 char buffer[512];
851 WMFont *font;
853 getSelectedFont(panel, buffer, sizeof(buffer));
855 font = WMCreateFont(WMWidgetScreen(panel->win), buffer);
856 if (font) {
857 WMSetTextFieldFont(panel->sampleT, font);
858 WMReleaseFont(font);
864 static void
865 familyClick(WMWidget *w, void *data)
867 WMList *lPtr = (WMList*)w;
868 WMListItem *item;
869 Family *family;
870 FontPanel *panel = (FontPanel*)data;
871 Typeface *face;
872 WMArrayIterator i;
873 /* current typeface and size */
874 char *oface = NULL;
875 char *osize = NULL;
876 int facei = -1;
877 int sizei = -1;
879 /* must try to keep the same typeface and size for the new family */
880 item = WMGetListSelectedItem(panel->typLs);
881 if (item)
882 oface = wstrdup(item->text);
884 osize = WMGetTextFieldText(panel->sizT);
887 item = WMGetListSelectedItem(lPtr);
888 family = (Family*)item->clientData;
890 WMClearList(panel->typLs);
893 WM_ITERATE_ARRAY(family->typefaces, face, i) {
894 char buffer[256];
895 int top=0;
896 WMListItem *fitem;
898 if (strcmp(face->weight, "medium") == 0) {
899 buffer[0] = 0;
900 } else {
901 if (*face->weight) {
902 strcpy(buffer, face->weight);
903 buffer[0] = toupper(buffer[0]);
904 strcat(buffer, " ");
905 } else {
906 buffer[0] = 0;
910 if (strcmp(face->slant, "r") == 0) {
911 strcat(buffer, _("Roman"));
912 top = 1;
913 } else if (strcmp(face->slant, "i") == 0) {
914 strcat(buffer, _("Italic"));
915 } else if (strcmp(face->slant, "o") == 0) {
916 strcat(buffer, _("Oblique"));
917 } else if (strcmp(face->slant, "ri") == 0) {
918 strcat(buffer, _("Rev Italic"));
919 } else if (strcmp(face->slant, "ro") == 0) {
920 strcat(buffer, _("Rev Oblique"));
921 } else {
922 strcat(buffer, face->slant);
925 if (buffer[0] == 0) {
926 strcpy(buffer, _("Normal"));
929 if (top)
930 fitem = WMInsertListItem(panel->typLs, 0, buffer);
931 else
932 fitem = WMAddListItem(panel->typLs, buffer);
933 fitem->clientData = face;
936 if (oface) {
937 facei = WMFindRowOfListItemWithTitle(panel->typLs, oface);
938 wfree(oface);
940 if (facei < 0) {
941 facei = 0;
943 WMSelectListItem(panel->typLs, facei);
944 typefaceClick(panel->typLs, panel);
946 if (osize) {
947 sizei = WMFindRowOfListItemWithTitle(panel->sizLs, osize);
949 if (sizei >= 0) {
950 WMSelectListItem(panel->sizLs, sizei);
951 sizeClick(panel->sizLs, panel);
954 if (osize)
955 wfree(osize);
958 preview(panel);
962 static void
963 typefaceClick(WMWidget *w, void *data)
965 FontPanel *panel = (FontPanel*)data;
966 WMListItem *item;
967 Typeface *face;
968 WMArrayIterator i;
969 char buffer[32];
971 char *osize = NULL;
972 int sizei = -1;
973 void *size;
975 osize = WMGetTextFieldText(panel->sizT);
978 item = WMGetListSelectedItem(panel->typLs);
979 face = (Typeface*)item->clientData;
981 WMClearList(panel->sizLs);
983 WM_ITERATE_ARRAY(face->sizes, size, i) {
984 if ((int)size != 0) {
985 sprintf(buffer, "%i", (int)size);
987 WMAddListItem(panel->sizLs, buffer);
991 if (osize) {
992 sizei = WMFindRowOfListItemWithTitle(panel->sizLs, osize);
994 if (sizei < 0) {
995 sizei = WMFindRowOfListItemWithTitle(panel->sizLs, "12");
997 if (sizei < 0) {
998 sizei = 0;
1000 WMSelectListItem(panel->sizLs, sizei);
1001 WMSetListPosition(panel->sizLs, sizei);
1002 sizeClick(panel->sizLs, panel);
1004 if (osize)
1005 wfree(osize);
1007 preview(panel);
1011 static void
1012 sizeClick(WMWidget *w, void *data)
1014 FontPanel *panel = (FontPanel*)data;
1015 WMListItem *item;
1017 item = WMGetListSelectedItem(panel->sizLs);
1019 WMSetTextFieldText(panel->sizT, item->text);
1021 WMSelectTextFieldRange(panel->sizT, wmkrange(0, strlen(item->text)));
1023 preview(panel);