- Fixed crashing bug in menu.c
[wmaker-crm.git] / WPrefs.app / Font.c
blob212de2a722e346fedcb1a6946974786407a605e9
1 /* Font.c- text/font settings
2 *
3 * WPrefs - Window Maker Preferences Program
4 *
5 * Copyright (c) 1999-2003 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"
27 typedef struct _Panel {
28 WMBox *box;
29 char *sectionName;
31 char *description;
33 CallbackRec callbacks;
35 WMWidget *parent;
38 WMLabel *prevL;
40 WMFrame *langF;
41 WMPopUpButton *langP;
43 /* single byte */
44 WMTextField *fontT;
45 WMButton *changeB;
47 /* multibyte */
48 WMLabel *fsetL;
49 WMList *fsetLs;
51 WMButton *addB;
52 WMButton *editB;
53 WMButton *remB;
56 WMFont *windowTitleFont;
57 WMFont *menuTitleFont;
58 WMFont *menuItemFont;
61 WMColor *white;
62 WMColor *black;
63 WMColor *light;
64 WMColor *dark;
66 WMColor *back;
68 Pixmap preview;
69 WMPixmap *previewPix;
70 } _Panel;
74 #define ICON_FILE "fonts"
78 static WMPropList *DefaultWindowTitleFont = NULL;
79 static WMPropList *DefaultMenuTitleFont = NULL;
80 static WMPropList *DefaultMenuTextFont = NULL;
81 static WMPropList *DefaultIconTitleFont = NULL;
82 static WMPropList *DefaultClipTitleFont = NULL;
83 static WMPropList *DefaultDisplayFont = NULL;
87 static void
88 drawMenuItem(Panel *panel, int x, int y, int w, int h, char *text)
90 WMScreen *scr = WMWidgetScreen(panel->parent);
91 Display *dpy = WMScreenDisplay(scr);
92 GC black = WMColorGC(panel->black);
93 GC white = WMColorGC(panel->white);
94 GC dark = WMColorGC(panel->dark);
95 int fh = WMFontHeight(panel->menuItemFont);
97 XFillRectangle(dpy, panel->preview, WMColorGC(panel->light), x, y, w, h);
99 XDrawLine(dpy, panel->preview, black, x, y, x, y+h);
100 XDrawLine(dpy, panel->preview, black, x+w, y, x+w, y+h);
102 XDrawLine(dpy, panel->preview, white, x+1, y, x+1, y+h-1);
103 XDrawLine(dpy, panel->preview, white, x+1, y, x+w-1, y);
105 XDrawLine(dpy, panel->preview, dark, x+w-1, y+1, x+w-1, y+h-3);
106 XDrawLine(dpy, panel->preview, dark, x+1, y+h-2, x+w-1, y+h-2);
108 XDrawLine(dpy, panel->preview, black, x, y+h-1, x+w, y+h-1);
110 WMDrawString(scr, panel->preview, panel->black, panel->menuItemFont,
111 x + 5, y+(h-fh)/2, text, strlen(text));
116 static void
117 paintPreviewBox(Panel *panel)
119 WMScreen *scr = WMWidgetScreen(panel->parent);
120 Display *dpy = WMScreenDisplay(scr);
121 GC black = WMColorGC(panel->black);
122 GC white = WMColorGC(panel->white);
123 GC dark = WMColorGC(panel->dark);
124 GC light = WMColorGC(panel->light);
127 if (panel->preview == None) {
128 WMPixmap *pix;
130 panel->preview = XCreatePixmap(dpy, WMWidgetXID(panel->parent),
131 240-4, 215-4, WMScreenDepth(scr));
133 pix = WMCreatePixmapFromXPixmaps(scr, panel->preview, None,
134 240-4, 215-4, WMScreenDepth(scr));
136 WMSetLabelImage(panel->prevL, pix);
137 WMReleasePixmap(pix);
140 XFillRectangle(dpy, panel->preview, WMColorGC(panel->back),
141 0, 0, 240-4, 215-4);
143 /* window title */
145 int h, fh;
147 fh = WMFontHeight(panel->windowTitleFont);
148 h = fh+6;
150 XFillRectangle(dpy, panel->preview, black,
151 19, 19, 203, h+3);
153 XDrawLine(dpy, panel->preview, light,
154 20, 20, 220, 20);
155 XDrawLine(dpy, panel->preview, light,
156 20, 20, 20, 20+h);
158 XDrawLine(dpy, panel->preview, dark,
159 20, 20+h, 220, 20+h);
160 XDrawLine(dpy, panel->preview, dark,
161 220, 20, 220, 20+h);
163 WMDrawString(scr, panel->preview, panel->white, panel->windowTitleFont,
164 20+(200-WMWidthOfString(panel->windowTitleFont, "Window Titlebar", 15))/2,
165 20+(h-fh)/2, "Window Titlebar", 15);
168 /* menu title */
170 int h, h2, fh, fh2;
171 int i;
172 const int mx = 20;
173 const int my = 120;
174 const int mw = 100;
177 fh = WMFontHeight(panel->menuTitleFont);
178 h = fh+6;
180 XFillRectangle(dpy, panel->preview, black,
181 mx-1, my-1, mw+3, h+3);
183 XDrawLine(dpy, panel->preview, light,
184 mx, my, mx+mw, my);
185 XDrawLine(dpy, panel->preview, light,
186 mx, my, mx, my+h);
188 XDrawLine(dpy, panel->preview, dark,
189 mx, my+h, mx+mw, my+h);
190 XDrawLine(dpy, panel->preview, dark,
191 mx+mw, my, mx+mw, my+h);
193 WMDrawString(scr, panel->preview, panel->white, panel->menuTitleFont,
194 mx+5, my+(h-fh)/2, "Menu Title", 10);
196 fh2 = WMFontHeight(panel->menuItemFont);
197 h2 = fh2+6;
199 /* menu items */
200 for (i = 0; i < 4; i++) {
201 drawMenuItem(panel, mx-1, my+2+h+i*h2, mw+2, h2, "Menu Item");
206 WMRedisplayWidget(panel->prevL);
211 static void
212 showData(_Panel *panel)
214 WMScreen *scr = WMWidgetScreen(panel->parent);
215 char *str;
217 str = GetStringForKey("WindowTitleFont");
219 panel->windowTitleFont = WMCreateFont(scr, str);
222 str = GetStringForKey("MenuTitleFont");
224 panel->menuTitleFont = WMCreateFont(scr, str);
227 str = GetStringForKey("MenuTextFont");
229 panel->menuItemFont = WMCreateFont(scr, str);
234 paintPreviewBox(panel);
238 static void
239 setLanguageType(Panel *p, Bool multiByte)
241 if (multiByte) {
242 WMMapWidget(p->fsetL);
243 WMMapWidget(p->fsetLs);
244 WMMapWidget(p->addB);
245 WMMapWidget(p->editB);
246 WMMapWidget(p->remB);
248 WMUnmapWidget(p->fontT);
249 WMUnmapWidget(p->changeB);
250 } else {
251 WMUnmapWidget(p->fsetL);
252 WMUnmapWidget(p->fsetLs);
253 WMUnmapWidget(p->addB);
254 WMUnmapWidget(p->editB);
255 WMUnmapWidget(p->remB);
257 WMMapWidget(p->fontT);
258 WMMapWidget(p->changeB);
267 static void
268 readFontEncodings(Panel *panel)
270 WMPropList *pl = NULL;
271 char *path;
272 char *msg;
274 path = WMPathForResourceOfType("font.data", NULL);
275 if (!path) {
276 msg = _("Could not locate font information file WPrefs.app/font.data");
277 goto error;
280 pl = WMReadPropListFromFile(path);
281 if (!pl) {
282 msg = _("Could not read font information file WPrefs.app/font.data");
283 goto error;
284 } else {
285 int i;
286 WMPropList *key = WMCreatePLString("Encodings");
287 WMPropList *array;
288 WMMenuItem *mi;
290 array = WMGetFromPLDictionary(pl, key);
291 WMReleasePropList(key);
292 if (!array || !WMIsPLArray(array)) {
293 msg = _("Invalid data in font information file WPrefs.app/font.data.\n"
294 "Encodings data not found.");
295 goto error;
298 WMAddPopUpButtonItem(panel->langP, _("- Custom -"));
300 for (i = 0; i < WMGetPropListItemCount(array); i++) {
301 WMPropList *item, *str;
303 item = WMGetFromPLArray(array, i);
304 str = WMGetFromPLArray(item, 0);
305 mi = WMAddPopUpButtonItem(panel->langP, WMGetFromPLString(str));
306 WMSetMenuItemRepresentedObject(mi, WMRetainPropList(item));
309 key = WMCreatePLString("WindowTitleFont");
310 DefaultWindowTitleFont = WMGetFromPLDictionary(pl, key);
311 WMReleasePropList(key);
313 key = WMCreatePLString("MenuTitleFont");
314 DefaultMenuTitleFont = WMGetFromPLDictionary(pl, key);
315 WMReleasePropList(key);
317 key = WMCreatePLString("MenuTextFont");
318 DefaultMenuTextFont = WMGetFromPLDictionary(pl, key);
319 WMReleasePropList(key);
322 WMReleasePropList(pl);
323 return;
324 error:
325 if (pl)
326 WMReleasePropList(pl);
328 WMRunAlertPanel(WMWidgetScreen(panel->parent), panel->parent,
329 _("Error"), msg, _("OK"), NULL, NULL);
333 #if 0
334 static void
335 changeLanguageAction(WMWidget *w, void *data)
337 /*Panel *panel = (Panel*)data;*/
338 WMMenuItem *mi;
339 WMPropList *pl;
341 mi = WMGetPopUpButtonMenuItem(w, WMGetPopUpButtonSelectedItem(w));
342 pl = WMGetMenuItemRepresentedObject(mi);
344 if (!pl) {
345 /* custom */
346 } else {
350 #endif
353 static void
354 createPanel(Panel *p)
356 _Panel *panel = (_Panel*)p;
357 WMScreen *scr = WMWidgetScreen(panel->parent);
360 panel->box = WMCreateBox(panel->parent);
361 WMSetViewExpandsToParent(WMWidgetView(panel->box), 2, 2, 2, 2);
364 panel->prevL = WMCreateLabel(panel->box);
365 WMResizeWidget(panel->prevL, 240, FRAME_HEIGHT-20);
366 WMMoveWidget(panel->prevL, 15, 10);
367 WMSetLabelRelief(panel->prevL, WRSunken);
368 WMSetLabelImagePosition(panel->prevL, WIPImageOnly);
371 /* language selection */
373 panel->langF = WMCreateFrame(panel->box);
374 WMResizeWidget(panel->langF, 245, 50);
375 WMMoveWidget(panel->langF, 265, 10);
376 WMSetFrameTitle(panel->langF, _("Default Font Sets"));
378 panel->langP = WMCreatePopUpButton(panel->langF);
379 WMResizeWidget(panel->langP, 215, 20);
380 WMMoveWidget(panel->langP, 15, 20);
382 WMMapSubwidgets(panel->langF);
385 /* multibyte */
386 panel->fsetL = WMCreateLabel(panel->box);
387 WMResizeWidget(panel->fsetL, 205, 20);
388 WMMoveWidget(panel->fsetL, 215, 127);
389 WMSetLabelText(panel->fsetL, _("Font Set"));
390 WMSetLabelRelief(panel->fsetL, WRSunken);
391 WMSetLabelTextAlignment(panel->fsetL, WACenter);
393 WMFont *font;
394 WMColor *color;
396 color = WMDarkGrayColor(scr);
397 font = WMBoldSystemFontOfSize(scr, 12);
399 WMSetWidgetBackgroundColor(panel->fsetL, color);
400 WMSetLabelFont(panel->fsetL, font);
402 WMReleaseFont(font);
403 WMReleaseColor(color);
405 color = WMWhiteColor(scr);
406 WMSetLabelTextColor(panel->fsetL, color);
407 WMReleaseColor(color);
410 panel->fsetLs = WMCreateList(panel->box);
411 WMResizeWidget(panel->fsetLs, 205, 71);
412 WMMoveWidget(panel->fsetLs, 215, 149);
415 panel->addB = WMCreateCommandButton(panel->box);
416 WMResizeWidget(panel->addB, 80, 24);
417 WMMoveWidget(panel->addB, 430, 127);
418 WMSetButtonText(panel->addB, _("Add..."));
420 panel->editB = WMCreateCommandButton(panel->box);
421 WMResizeWidget(panel->editB, 80, 24);
422 WMMoveWidget(panel->editB, 430, 161);
423 WMSetButtonText(panel->editB, _("Change..."));
425 panel->remB = WMCreateCommandButton(panel->box);
426 WMResizeWidget(panel->remB, 80, 24);
427 WMMoveWidget(panel->remB, 430, 195);
428 WMSetButtonText(panel->remB, _("Remove"));
430 /* single byte */
431 panel->fontT = WMCreateTextField(panel->box);
432 WMResizeWidget(panel->fontT, 240, 20);
433 WMMoveWidget(panel->fontT, 265, 130);
435 panel->changeB = WMCreateCommandButton(panel->box);
436 WMResizeWidget(panel->changeB, 104, 24);
437 WMMoveWidget(panel->changeB, 335, 160);
438 WMSetButtonText(panel->changeB, _("Change..."));
441 panel->black = WMBlackColor(scr);
442 panel->white = WMWhiteColor(scr);
443 panel->light = WMGrayColor(scr);
444 panel->dark = WMDarkGrayColor(scr);
445 panel->back = WMCreateRGBColor(scr, 0x5100, 0x5100, 0x7100, True);
447 #if 0
448 for (i = 0; Languages[i].language != NULL; i++) {
449 WMAddPopUpButtonItem(panel->langP, Languages[i].language);
452 for (i = 0; Options[i].description != NULL; i++) {
453 WMAddListItem(panel->settingLs, Options[i].description);
455 #endif
456 WMRealizeWidget(panel->box);
457 WMMapSubwidgets(panel->box);
459 setLanguageType(panel, False);
461 showData(panel);
463 readFontEncodings(panel);
469 Panel*
470 InitFont(WMScreen *scr, WMWidget *parent)
472 _Panel *panel;
474 panel = wmalloc(sizeof(_Panel));
475 memset(panel, 0, sizeof(_Panel));
477 panel->sectionName = _("Font Preferences");
478 panel->description = _("Font Configurations for Windows, Menus etc");
480 panel->parent = parent;
482 panel->callbacks.createWidgets = createPanel;
484 AddSection(panel, ICON_FILE);
486 return panel;