Put an indicator for the selected item in appearance section
[wmaker-crm.git] / WPrefs.app / Appearance.c
blobea80de31841ff79fef3531701a55e145c2c27402
1 /* Apperance.c- color/texture for titlebar etc.
2 *
3 * WPrefs - Window Maker Preferences Program
4 *
5 * Copyright (c) 1999 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"
26 #include <unistd.h>
27 #include <errno.h>
28 #include <ctype.h>
29 #include <time.h>
30 #include <sys/stat.h>
31 #include <sys/types.h>
32 #include <fcntl.h>
33 #include <unistd.h>
38 #include "TexturePanel.h"
40 typedef struct _Panel {
41 WMFrame *frame;
42 char *sectionName;
44 CallbackRec callbacks;
46 WMWindow *win;
48 WMLabel *prevL;
50 WMPopUpButton *secP;
52 /* texture list */
53 WMLabel *texL;
54 WMList *texLs;
55 WMLabel *texsL;
57 WMButton *newB;
58 WMButton *editB;
59 WMButton *ripB;
60 WMButton *delB;
62 int textureIndex[8];
64 WMFont *smallFont;
65 WMFont *normalFont;
66 WMFont *boldFont;
68 TexturePanel *texturePanel;
70 WMPixmap *onLed;
71 WMPixmap *offLed;
72 WMPixmap *hand;
74 int oldsection;
76 Pixmap preview;
78 char *fprefix;
79 } _Panel;
82 typedef struct {
83 char *title;
84 char *texture;
85 proplist_t prop;
86 Pixmap preview;
88 char *path;
90 char selectedFor;
91 unsigned current:1;
92 unsigned ispixmap:1;
93 } TextureListItem;
96 static void showData(_Panel *panel);
99 static void OpenExtractPanelFor(_Panel *panel, char *path);
101 #define ICON_FILE "appearance"
103 #define TNEW_FILE "tnew"
104 #define TDEL_FILE "tdel"
105 #define TEDIT_FILE "tedit"
106 #define TEXTR_FILE "textr"
110 /* XPM */
111 static char * blueled_xpm[] = {
112 "8 8 17 1",
113 " c None",
114 ". c #020204",
115 "+ c #16B6FC",
116 "@ c #176AFC",
117 "# c #163AFC",
118 "$ c #72D2FC",
119 "% c #224CF4",
120 "& c #76D6FC",
121 "* c #16AAFC",
122 "= c #CEE9FC",
123 "- c #229EFC",
124 "; c #164CFC",
125 "> c #FAFEFC",
126 ", c #2482FC",
127 "' c #1652FC",
128 ") c #1E76FC",
129 "! c #1A60FC",
130 " .... ",
131 " .=>-@. ",
132 ".=>$@@'.",
133 ".=$@!;;.",
134 ".!@*+!#.",
135 ".#'*+*,.",
136 " .@)@,. ",
137 " .... "};
140 /* XPM */
141 static char *blueled2_xpm[] = {
142 /* width height num_colors chars_per_pixel */
143 " 8 8 17 1",
144 /* colors */
145 ". c None",
146 "# c #090909",
147 "a c #4b63a4",
148 "b c #011578",
149 "c c #264194",
150 "d c #04338c",
151 "e c #989dac",
152 "f c #011a7c",
153 "g c #465c9c",
154 "h c #03278a",
155 "i c #6175ac",
156 "j c #011e74",
157 "k c #043a90",
158 "l c #042f94",
159 "m c #0933a4",
160 "n c #022184",
161 "o c #012998",
162 /* pixels */
163 "..####..",
164 ".#aimn#.",
165 "#aechnf#",
166 "#gchnjf#",
167 "#jndknb#",
168 "#bjdddl#",
169 ".#nono#.",
170 "..####.."
173 /* XPM */
174 static char * hand_xpm[] = {
175 "22 21 17 1",
176 " c None",
177 ". c #030305",
178 "+ c #101010",
179 "@ c #535355",
180 "# c #7F7F7E",
181 "$ c #969697",
182 "% c #B5B5B6",
183 "& c #C5C5C6",
184 "* c #D2D2D0",
185 "= c #DCDCDC",
186 "- c #E5E5E4",
187 "; c #ECECEC",
188 "> c #767674",
189 ", c #F2F2F4",
190 "' c #676767",
191 ") c #FDFDFB",
192 "! c #323234",
193 " ",
194 " ..... ",
195 " ..#%&&$. ",
196 " .))),%.......... ",
197 " .)-)),&)))))))))$. ",
198 " .-&))))))))),,,,;;. ",
199 " .=)))))))));-=***&$. ",
200 " .=)))))))),..+..... ",
201 " +=)))))))))-&#. ",
202 " +=)))))))))-%>. ",
203 " +&)))))))))-%'. ",
204 " +$,,))))));... ",
205 " .#%%*;,)),*$>+ ",
206 " .'>$%&&&&$#@. ",
207 " .!'>#$##>'. ",
208 " ..+++++. ",
209 " ",
210 " ##@@@## ",
211 " @@@@@@@@@@@ ",
212 " >>@@@## ",
213 " "};
218 #define FTITLE (1<<0)
219 #define UTITLE (1<<1)
220 #define OTITLE (1<<2)
221 #define MTITLE (1<<3)
222 #define MITEM (1<<4)
223 #define ICON (1<<5)
224 #define BACK (1<<6)
225 #define EVERYTHING 0xff
230 #define TEXPREV_WIDTH 40
231 #define TEXPREV_HEIGHT 24
237 static void
238 str2rcolor(RContext *rc, char *name, RColor *color)
240 XColor xcolor;
242 XParseColor(rc->dpy, rc->cmap, name, &xcolor);
244 color->alpha = 255;
245 color->red = xcolor.red >> 8;
246 color->green = xcolor.green >> 8;
247 color->blue = xcolor.blue >> 8;
252 static void
253 dumpRImage(char *path, RImage *image)
255 FILE *f;
257 f = fopen(path, "w");
258 if (!f) {
259 wsyserror(path);
260 return;
262 fprintf(f, "%02x%02x%1x", image->width, image->height,
263 image->data[3]!=NULL ? 4 : 3);
265 fwrite(image->data[0], 1, image->width * image->height, f);
266 fwrite(image->data[1], 1, image->width * image->height, f);
267 fwrite(image->data[2], 1, image->width * image->height, f);
268 if (image->data[3])
269 fwrite(image->data[3], 1, image->width * image->height, f);
271 if (fclose(f) < 0) {
272 wsyserror(path);
278 static int
279 isPixmap(proplist_t prop)
281 proplist_t p;
282 char *s;
284 p = PLGetArrayElement(prop, 0);
285 s = PLGetString(p);
286 if (strcasecmp(&s[1], "pixmap")==0)
287 return 1;
288 else
289 return 0;
294 static Pixmap
295 renderTexture(WMScreen *scr, proplist_t texture, int width, int height,
296 char *path, int border)
298 char *type;
299 RImage *image;
300 Pixmap pixmap;
301 RContext *rc = WMScreenRContext(scr);
302 char *str;
303 RColor rcolor;
306 type = PLGetString(PLGetArrayElement(texture, 0));
308 image = RCreateImage(width, height, False);
310 if (strcasecmp(type, "solid")==0) {
312 str = PLGetString(PLGetArrayElement(texture, 1));
314 str2rcolor(rc, str, &rcolor);
316 RClearImage(image, &rcolor);
317 } else if (strcasecmp(&type[1], "gradient")==0) {
318 int style;
319 RColor rcolor2;
321 switch (toupper(type[0])) {
322 case 'V':
323 style = RVerticalGradient;
324 break;
325 case 'H':
326 style = RHorizontalGradient;
327 break;
328 default:
329 case 'D':
330 style = RDiagonalGradient;
331 break;
334 str = PLGetString(PLGetArrayElement(texture, 1));
335 str2rcolor(rc, str, &rcolor);
336 str = PLGetString(PLGetArrayElement(texture, 2));
337 str2rcolor(rc, str, &rcolor2);
339 image = RRenderGradient(width, height, &rcolor, &rcolor2, style);
340 } else if (strcasecmp(&type[2], "gradient")==0 && toupper(type[0])=='T') {
341 int style;
342 RColor rcolor2;
343 int i;
344 RImage *grad, *timage;
345 char *path;
347 switch (toupper(type[1])) {
348 case 'V':
349 style = RVerticalGradient;
350 break;
351 case 'H':
352 style = RHorizontalGradient;
353 break;
354 default:
355 case 'D':
356 style = RDiagonalGradient;
357 break;
360 str = PLGetString(PLGetArrayElement(texture, 3));
361 str2rcolor(rc, str, &rcolor);
362 str = PLGetString(PLGetArrayElement(texture, 4));
363 str2rcolor(rc, str, &rcolor2);
365 str = PLGetString(PLGetArrayElement(texture, 1));
367 path = wfindfileinarray(GetObjectForKey("PixmapPath"), str);
368 timage = RLoadImage(rc, path, 0);
370 if (!timage) {
371 wwarning("could not load file '%s': %s", path,
372 RMessageForError(RErrorCode));
373 } else {
374 grad = RRenderGradient(width, height, &rcolor, &rcolor2, style);
376 image = RMakeTiledImage(timage, width, height);
377 RDestroyImage(timage);
379 i = atoi(PLGetString(PLGetArrayElement(texture, 2)));
381 RCombineImagesWithOpaqueness(image, grad, i);
382 RDestroyImage(grad);
384 } else if (strcasecmp(&type[2], "gradient")==0 && toupper(type[0])=='M') {
385 int style;
386 RColor **colors;
387 int i, j;
389 switch (toupper(type[1])) {
390 case 'V':
391 style = RVerticalGradient;
392 break;
393 case 'H':
394 style = RHorizontalGradient;
395 break;
396 default:
397 case 'D':
398 style = RDiagonalGradient;
399 break;
402 j = PLGetNumberOfElements(texture);
404 if (j > 0) {
405 colors = wmalloc(j * sizeof(RColor*));
407 for (i = 2; i < j; i++) {
408 str = PLGetString(PLGetArrayElement(texture, i));
409 colors[i-2] = wmalloc(sizeof(RColor));
410 str2rcolor(rc, str, colors[i-2]);
412 colors[i-2] = NULL;
414 image = RRenderMultiGradient(width, height, colors, style);
416 for (i = 0; colors[i]!=NULL; i++)
417 free(colors[i]);
418 free(colors);
420 } else if (strcasecmp(&type[1], "pixmap")==0) {
421 RImage *timage;
422 int w, h;
423 char *path;
424 RColor color;
426 str = PLGetString(PLGetArrayElement(texture, 1));
428 path = wfindfileinarray(GetObjectForKey("PixmapPath"), str);
429 timage = RLoadImage(rc, path, 0);
431 if (!timage) {
432 wwarning("could not load file '%s': %s", path,
433 RMessageForError(RErrorCode));
434 } else {
435 str = PLGetString(PLGetArrayElement(texture, 2));
436 str2rcolor(rc, str, &color);
438 switch (toupper(type[0])) {
439 case 'T':
440 image = RMakeTiledImage(timage, width, height);
441 RDestroyImage(timage);
442 timage = image;
443 break;
444 case 'C':
445 image = RMakeCenteredImage(timage, width, height, &color);
446 RDestroyImage(timage);
447 timage = image;
448 break;
449 case 'S':
450 case 'M':
451 image = RScaleImage(timage, width, height);
452 RDestroyImage(timage);
453 timage = image;
454 break;
458 free(path);
461 if (!image)
462 return None;
464 if (path) {
465 dumpRImage(path, image);
468 if (border) {
469 RBevelImage(image, border);
472 RConvertImage(rc, image, &pixmap);
473 RDestroyImage(image);
475 return pixmap;
481 static void
482 updatePreviewBox(_Panel *panel, int elements)
484 WMScreen *scr = WMWidgetScreen(panel->win);
485 Display *dpy = WMScreenDisplay(scr);
486 /* RContext *rc = WMScreenRContext(scr);*/
487 int refresh = 0;
488 Pixmap pix;
489 GC gc;
490 WMListItem *item;
491 TextureListItem *titem;
493 gc = XCreateGC(dpy, WMWidgetXID(panel->win), 0, NULL);
496 if (!panel->preview) {
497 WMColor *color;
499 panel->preview = XCreatePixmap(dpy, WMWidgetXID(panel->win),
500 260-4, 165-4, WMScreenDepth(scr));
502 color = WMGrayColor(scr);
503 XFillRectangle(dpy, panel->preview, WMColorGC(color),
504 0, 0, 260-4, 165-4);
505 WMReleaseColor(color);
507 refresh = -1;
510 if (elements & FTITLE) {
511 item = WMGetListItem(panel->texLs, panel->textureIndex[0]);
512 titem = (TextureListItem*)item->clientData;
514 pix = renderTexture(scr, titem->prop, 210, 20, NULL, RBEV_RAISED2);
516 XCopyArea(dpy, pix, panel->preview, gc, 0, 0, 210, 20, 30, 10);
518 XFreePixmap(dpy, pix);
520 if (elements & UTITLE) {
521 item = WMGetListItem(panel->texLs, panel->textureIndex[1]);
522 titem = (TextureListItem*)item->clientData;
524 pix = renderTexture(scr, titem->prop, 210, 20, NULL, RBEV_RAISED2);
526 XCopyArea(dpy, pix, panel->preview, gc, 0, 0, 210, 20, 30, 35);
528 XFreePixmap(dpy, pix);
530 if (elements & OTITLE) {
531 item = WMGetListItem(panel->texLs, panel->textureIndex[2]);
532 titem = (TextureListItem*)item->clientData;
534 pix = renderTexture(scr, titem->prop, 210, 20, NULL, RBEV_RAISED2);
536 XCopyArea(dpy, pix, panel->preview, gc, 0, 0, 210, 20, 30, 60);
538 XFreePixmap(dpy, pix);
540 if (elements & MTITLE) {
541 item = WMGetListItem(panel->texLs, panel->textureIndex[3]);
542 titem = (TextureListItem*)item->clientData;
544 pix = renderTexture(scr, titem->prop, 100, 20, NULL, RBEV_RAISED2);
546 XCopyArea(dpy, pix, panel->preview, gc, 0, 0, 100, 20, 30, 95);
548 XFreePixmap(dpy, pix);
550 if (elements & MITEM) {
551 item = WMGetListItem(panel->texLs, panel->textureIndex[4]);
552 titem = (TextureListItem*)item->clientData;
554 pix = renderTexture(scr, titem->prop, 100, 18, NULL, RBEV_RAISED2);
556 XCopyArea(dpy, pix, panel->preview, gc, 0, 0, 100, 18, 30, 115);
557 XCopyArea(dpy, pix, panel->preview, gc, 0, 0, 100, 18, 30, 115 + 18);
558 XCopyArea(dpy, pix, panel->preview, gc, 0, 0, 100, 18, 30, 115 + 36);
560 XFreePixmap(dpy, pix);
562 if (elements & (MITEM|MTITLE)) {
563 XDrawLine(dpy, panel->preview, gc, 29, 95, 29, 115+36+20);
564 XDrawLine(dpy, panel->preview, gc, 29, 94, 129, 94);
567 if (elements & ICON) {
568 item = WMGetListItem(panel->texLs, panel->textureIndex[5]);
569 titem = (TextureListItem*)item->clientData;
571 pix = renderTexture(scr, titem->prop, 64, 64, NULL,
572 titem->ispixmap ? 0 : RBEV_RAISED3);
574 XCopyArea(dpy, pix, panel->preview, gc, 0, 0, 64, 64, 170, 90);
576 XFreePixmap(dpy, pix);
580 if (refresh < 0) {
581 WMPixmap *p;
582 p = WMCreatePixmapFromXPixmaps(scr, panel->preview, None,
583 260-4, 165-4, WMScreenDepth(scr));
585 WMSetLabelImage(panel->prevL, p);
586 WMReleasePixmap(p);
587 } else {
588 WMRedisplayWidget(panel->prevL);
591 XFreeGC(dpy, gc);
597 static void
598 cancelNewTexture(void *data)
600 _Panel *panel = (_Panel*)data;
602 HideTexturePanel(panel->texturePanel);
608 static char*
609 makeFileName(char *prefix)
611 char *fname;
613 fname = wstrdup(prefix);
615 while (access(fname, F_OK)==0) {
616 char buf[30];
618 free(fname);
619 sprintf(buf, "%08lx.cache", time(NULL));
620 fname = wstrappend(prefix, buf);
623 return fname;
629 static void
630 okNewTexture(void *data)
632 _Panel *panel = (_Panel*)data;
633 WMListItem *item;
634 char *name;
635 char *str;
636 proplist_t prop;
637 TextureListItem *titem;
638 WMScreen *scr = WMWidgetScreen(panel->win);
640 titem = wmalloc(sizeof(TextureListItem));
641 memset(titem, 0, sizeof(TextureListItem));
643 HideTexturePanel(panel->texturePanel);
645 name = GetTexturePanelTextureName(panel->texturePanel);
647 prop = GetTexturePanelTexture(panel->texturePanel);
649 str = PLGetDescription(prop);
651 titem->title = name;
652 titem->prop = prop;
653 titem->texture = str;
654 titem->selectedFor = 0;
656 titem->ispixmap = isPixmap(prop);
658 titem->path = makeFileName(panel->fprefix);
659 titem->preview = renderTexture(scr, prop, TEXPREV_WIDTH, TEXPREV_HEIGHT,
660 titem->path, 0);
662 item = WMAddListItem(panel->texLs, "");
663 item->clientData = titem;
665 WMSetListPosition(panel->texLs, WMGetListNumberOfRows(panel->texLs));
669 static void
670 okEditTexture(void *data)
672 _Panel *panel = (_Panel*)data;
673 WMListItem *item;
674 char *name;
675 char *str;
676 proplist_t prop;
677 TextureListItem *titem;
679 item = WMGetListItem(panel->texLs, WMGetListSelectedItemRow(panel->texLs));
680 titem = (TextureListItem*)item->clientData;
682 HideTexturePanel(panel->texturePanel);
684 if (titem->current) {
685 name = GetTexturePanelTextureName(panel->texturePanel);
687 free(titem->title);
688 titem->title = name;
691 prop = GetTexturePanelTexture(panel->texturePanel);
693 str = PLGetDescription(prop);
695 PLRelease(titem->prop);
696 titem->prop = prop;
698 titem->ispixmap = isPixmap(prop);
700 free(titem->texture);
701 titem->texture = str;
703 XFreePixmap(WMScreenDisplay(WMWidgetScreen(panel->texLs)), titem->preview);
704 titem->preview = renderTexture(WMWidgetScreen(panel->texLs), titem->prop,
705 TEXPREV_WIDTH, TEXPREV_HEIGHT,
706 titem->path, 0);
708 WMRedisplayWidget(panel->texLs);
710 if (titem->selectedFor)
711 updatePreviewBox(panel, titem->selectedFor);
716 static void
717 editTexture(WMWidget *w, void *data)
719 _Panel *panel = (_Panel*)data;
720 WMListItem *item;
721 TextureListItem *titem;
723 item = WMGetListItem(panel->texLs, WMGetListSelectedItemRow(panel->texLs));
724 titem = (TextureListItem*)item->clientData;
726 SetTexturePanelPixmapPath(panel->texturePanel,
727 GetObjectForKey("PixmapPath"));
729 SetTexturePanelTexture(panel->texturePanel, titem->title, titem->prop);
731 SetTexturePanelCancelAction(panel->texturePanel, cancelNewTexture, panel);
732 SetTexturePanelOkAction(panel->texturePanel, okEditTexture, panel);
734 ShowTexturePanel(panel->texturePanel);
739 static void
740 newTexture(WMWidget *w, void *data)
742 _Panel *panel = (_Panel*)data;
744 SetTexturePanelPixmapPath(panel->texturePanel,
745 GetObjectForKey("PixmapPath"));
747 SetTexturePanelTexture(panel->texturePanel, "New Texture", NULL);
749 SetTexturePanelCancelAction(panel->texturePanel, cancelNewTexture, panel);
751 SetTexturePanelOkAction(panel->texturePanel, okNewTexture, panel);
753 ShowTexturePanel(panel->texturePanel);
758 static void
759 deleteTexture(WMWidget *w, void *data)
761 _Panel *panel = (_Panel*)data;
762 WMListItem *item;
763 TextureListItem *titem;
764 int row;
765 int section;
767 section = WMGetPopUpButtonSelectedItem(panel->secP);
768 row = WMGetListSelectedItemRow(panel->texLs);
769 item = WMGetListItem(panel->texLs, row);
770 titem = (TextureListItem*)item->clientData;
772 if (titem->selectedFor & (1 << section)) {
773 TextureListItem *titem2;
775 panel->textureIndex[section] = section;
776 item = WMGetListItem(panel->texLs, section);
777 titem2 = (TextureListItem*)item->clientData;
778 titem2->selectedFor |= 1 << section;
781 free(titem->title);
782 free(titem->texture);
783 PLRelease(titem->prop);
784 if (titem->path) {
785 if (remove(titem->path) < 0 && errno != ENOENT) {
786 wsyserror("could not remove file %s", titem->path);
788 free(titem->path);
791 free(titem);
793 WMRemoveListItem(panel->texLs, row);
794 WMSetButtonEnabled(panel->delB, False);
800 static void
801 extractTexture(WMWidget *w, void *data)
803 _Panel *panel = (_Panel*)data;
804 char *path;
805 WMOpenPanel *opanel;
806 WMScreen *scr = WMWidgetScreen(w);
808 opanel = WMGetOpenPanel(scr);
809 WMSetFilePanelCanChooseDirectories(opanel, False);
810 WMSetFilePanelCanChooseFiles(opanel, True);
812 if (WMRunModalFilePanelForDirectory(opanel, panel->win, wgethomedir(),
813 _("Select File"), NULL)) {
814 path = WMGetFilePanelFileName(opanel);
816 OpenExtractPanelFor(panel, path);
818 free(path);
825 static void
826 changePage(WMWidget *w, void *data)
828 _Panel *panel = (_Panel*)data;
829 int section;
830 WMListItem *item;
831 TextureListItem *titem;
832 char *str;
833 WMScreen *scr = WMWidgetScreen(w);
834 RContext *rc = WMScreenRContext(scr);
835 static WMPoint positions[6] = {
836 {5, 10},
837 {5, 35},
838 {5, 60},
839 {5, 95},
840 {5, 130},
841 {145, 110}
844 section = WMGetPopUpButtonSelectedItem(panel->secP);
846 WMSelectListItem(panel->texLs, panel->textureIndex[section]);
848 WMSetListPosition(panel->texLs, panel->textureIndex[section]
849 - WMGetListNumberOfRows(panel->texLs)/2);
851 item = WMGetListItem(panel->texLs, panel->textureIndex[section]);
853 titem = (TextureListItem*)item->clientData;
855 str = wmalloc(strlen(titem->title) + strlen(titem->texture) + 4);
856 sprintf(str, "%s: %s", titem->title, titem->texture);
857 WMSetLabelText(panel->texsL, str);
858 free(str);
861 WMColor *color;
863 color = WMGrayColor(scr);
864 XFillRectangle(rc->dpy, panel->preview, WMColorGC(color),
865 positions[panel->oldsection].x,
866 positions[panel->oldsection].y, 22, 22);
867 WMReleaseColor(color);
869 panel->oldsection = section;
870 WMDrawPixmap(panel->hand, panel->preview, positions[section].x,
871 positions[section].y);
873 WMRedisplayWidget(panel->prevL);
878 static void
879 textureClick(WMWidget *w, void *data)
881 _Panel *panel = (_Panel*)data;
882 int i;
883 WMListItem *item;
884 TextureListItem *titem;
886 i = WMGetListSelectedItemRow(panel->texLs);
888 item = WMGetListItem(panel->texLs, i);
890 titem = (TextureListItem*)item->clientData;
892 if (titem->current) {
893 WMSetButtonEnabled(panel->delB, False);
894 } else {
895 WMSetButtonEnabled(panel->delB, True);
901 static void
902 textureDoubleClick(WMWidget *w, void *data)
904 _Panel *panel = (_Panel*)data;
905 int i, section;
906 WMListItem *item;
907 TextureListItem *titem;
908 char *str;
910 /* unselect old texture */
911 section = WMGetPopUpButtonSelectedItem(panel->secP);
913 item = WMGetListItem(panel->texLs, panel->textureIndex[section]);
914 titem = (TextureListItem*)item->clientData;
915 titem->selectedFor &= ~(1 << section);
917 /* select new texture */
918 i = WMGetListSelectedItemRow(panel->texLs);
920 item = WMGetListItem(panel->texLs, i);
922 titem = (TextureListItem*)item->clientData;
924 titem->selectedFor |= 1<<section;
926 panel->textureIndex[section] = i;
928 WMRedisplayWidget(panel->texLs);
930 str = wmalloc(strlen(titem->title) + strlen(titem->texture) + 4);
931 sprintf(str, "%s: %s", titem->title, titem->texture);
932 WMSetLabelText(panel->texsL, str);
933 free(str);
935 updatePreviewBox(panel, 1<<section);
941 static void
942 paintListItem(WMList *lPtr, int index, Drawable d, char *text, int state,
943 WMRect *rect)
945 _Panel *panel = (_Panel*)WMGetHangedData(lPtr);
946 WMScreen *scr = WMWidgetScreen(lPtr);
947 int width, height, x, y;
948 Display *dpy = WMScreenDisplay(scr);
949 WMColor *white = WMWhiteColor(scr);
950 WMListItem *item;
951 WMColor *black = WMBlackColor(scr);
952 TextureListItem *titem;
954 width = rect->size.width;
955 height = rect->size.height;
956 x = rect->pos.x;
957 y = rect->pos.y;
959 if (state & WLDSSelected)
960 XFillRectangle(dpy, d, WMColorGC(white), x, y, width, height);
961 else
962 XClearArea(dpy, d, x, y, width, height, False);
964 item = WMGetListItem(lPtr, index);
965 titem = (TextureListItem*)item->clientData;
967 if (titem->preview)
968 XCopyArea(dpy, titem->preview, d, WMColorGC(black), 0, 0, TEXPREV_WIDTH,
969 TEXPREV_HEIGHT, x + 5, y + 5);
971 if ((1 << WMGetPopUpButtonSelectedItem(panel->secP)) & titem->selectedFor)
972 WMDrawPixmap(panel->onLed, d, x + TEXPREV_WIDTH + 10, y + 6);
973 else if (titem->selectedFor)
974 WMDrawPixmap(panel->offLed, d, x + TEXPREV_WIDTH + 10, y + 6);
976 WMDrawString(scr, d, WMColorGC(black), panel->boldFont,
977 x + TEXPREV_WIDTH + 22, y + 2, titem->title,
978 strlen(titem->title));
980 WMDrawString(scr, d, WMColorGC(black), panel->smallFont,
981 x + TEXPREV_WIDTH + 14, y + 18, titem->texture,
982 strlen(titem->texture));
985 WMReleaseColor(white);
986 WMReleaseColor(black);
991 static Pixmap
992 loadRImage(WMScreen *scr, char *path)
994 FILE *f;
995 RImage *image;
996 int w, h, d;
997 int i;
998 Pixmap pixmap;
1000 f = fopen(path, "r");
1001 if (!f)
1002 return None;
1004 fscanf(f, "%02x%02x%1x", &w, &h, &d);
1006 image = RCreateImage(w, h, d == 4);
1007 for (i = 0; i < d; i++) {
1008 fread(image->data[i], 1, w*h, f);
1010 fclose(f);
1012 RConvertImage(WMScreenRContext(scr), image, &pixmap);
1013 RDestroyImage(image);
1015 return pixmap;
1020 static void
1021 fillTextureList(WMList *lPtr)
1023 proplist_t textureList;
1024 proplist_t texture;
1025 WMUserDefaults *udb = WMGetStandardUserDefaults();
1026 int i;
1027 TextureListItem *titem;
1028 WMScreen *scr = WMWidgetScreen(lPtr);
1030 textureList = WMGetUDObjectForKey(udb, "TextureList");
1031 if (!textureList)
1032 return;
1034 for (i = 0; i < PLGetNumberOfElements(textureList); i++) {
1035 WMListItem *item;
1037 texture = PLGetArrayElement(textureList, i);
1039 titem = wmalloc(sizeof(TextureListItem));
1040 memset(titem, 0, sizeof(TextureListItem));
1042 titem->title = wstrdup(PLGetString(PLGetArrayElement(texture, 0)));
1043 titem->prop = PLRetain(PLGetArrayElement(texture, 1));
1044 titem->texture = PLGetDescription(titem->prop);
1045 titem->selectedFor = 0;
1046 titem->path = wstrdup(PLGetString(PLGetArrayElement(texture, 2)));
1048 titem->preview = loadRImage(scr, titem->path);
1049 if (!titem->preview) {
1050 titem->preview = renderTexture(scr, titem->prop, TEXPREV_WIDTH,
1051 TEXPREV_HEIGHT, NULL, 0);
1053 item = WMAddListItem(lPtr, "");
1054 item->clientData = titem;
1059 static void
1060 createPanel(Panel *p)
1062 _Panel *panel = (_Panel*)p;
1063 WMColor *color;
1064 WMFont *font;
1065 WMScreen *scr = WMWidgetScreen(panel->win);
1067 char *tmp;
1068 Bool ok = True;
1070 panel->fprefix = wstrappend(wusergnusteppath(), "/.AppInfo");
1072 if (access(panel->fprefix, F_OK)!=0) {
1073 if (mkdir(panel->fprefix, 0755) < 0) {
1074 wsyserror(panel->fprefix);
1075 ok = False;
1078 if (ok) {
1079 tmp = wstrappend(panel->fprefix, "/WPrefs/");
1080 free(panel->fprefix);
1081 panel->fprefix = tmp;
1082 if (access(panel->fprefix, F_OK)!=0) {
1083 if (mkdir(panel->fprefix, 0755) < 0) {
1084 wsyserror(panel->fprefix);
1089 panel->smallFont = WMSystemFontOfSize(scr, 10);
1090 panel->normalFont = WMSystemFontOfSize(scr, 12);
1091 panel->boldFont = WMBoldSystemFontOfSize(scr, 12);
1093 panel->onLed = WMCreatePixmapFromXPMData(scr, blueled_xpm);
1094 panel->offLed = WMCreatePixmapFromXPMData(scr, blueled2_xpm);
1095 panel->hand = WMCreatePixmapFromXPMData(scr, hand_xpm);
1097 panel->frame = WMCreateFrame(panel->win);
1098 WMResizeWidget(panel->frame, FRAME_WIDTH, FRAME_HEIGHT);
1099 WMMoveWidget(panel->frame, FRAME_LEFT, FRAME_TOP);
1101 /* preview box */
1102 panel->prevL = WMCreateLabel(panel->frame);
1103 WMResizeWidget(panel->prevL, 260, 165);
1104 WMMoveWidget(panel->prevL, 15, 10);
1105 WMSetLabelRelief(panel->prevL, WRSunken);
1106 WMSetLabelImagePosition(panel->prevL, WIPImageOnly);
1108 panel->secP = WMCreatePopUpButton(panel->frame);
1109 WMResizeWidget(panel->secP, 260, 20);
1110 WMMoveWidget(panel->secP, 15, 180);
1111 WMSetPopUpButtonSelectedItem(panel->secP, 0);
1112 WMAddPopUpButtonItem(panel->secP, _("Titlebar of Focused Window"));
1113 WMAddPopUpButtonItem(panel->secP, _("Titlebar of Unfocused Windows"));
1114 WMAddPopUpButtonItem(panel->secP, _("Titlebar of Focused Window's Owner"));
1115 WMAddPopUpButtonItem(panel->secP, _("Titlebar of Menus"));
1116 WMAddPopUpButtonItem(panel->secP, _("Menu Items"));
1117 WMAddPopUpButtonItem(panel->secP, _("Icon Background"));
1118 /* WMAddPopUpButtonItem(panel->secP, _("Workspace Backgrounds"));
1120 WMSetPopUpButtonAction(panel->secP, changePage, panel);
1123 panel->texsL = WMCreateLabel(panel->frame);
1124 WMResizeWidget(panel->texsL, 260, 20);
1125 WMMoveWidget(panel->texsL, 15, 205);
1126 WMSetLabelWraps(panel->texsL, False);
1128 /* texture list */
1129 font = WMBoldSystemFontOfSize(scr, 12);
1131 panel->texL = WMCreateLabel(panel->frame);
1132 WMResizeWidget(panel->texL, 225, 18);
1133 WMMoveWidget(panel->texL, 285, 10);
1134 WMSetLabelFont(panel->texL, font);
1135 WMSetLabelText(panel->texL, _("Textures"));
1136 WMSetLabelRelief(panel->texL, WRSunken);
1137 WMSetLabelTextAlignment(panel->texL, WACenter);
1138 color = WMDarkGrayColor(scr);
1139 WMSetWidgetBackgroundColor(panel->texL, color);
1140 WMReleaseColor(color);
1141 color = WMWhiteColor(scr);
1142 WMSetLabelTextColor(panel->texL, color);
1143 WMReleaseColor(color);
1145 WMReleaseFont(font);
1147 panel->texLs = WMCreateList(panel->frame);
1148 WMResizeWidget(panel->texLs, 225, 144);
1149 WMMoveWidget(panel->texLs, 285, 30);
1150 WMSetListUserDrawItemHeight(panel->texLs, 35);
1151 WMSetListUserDrawProc(panel->texLs, paintListItem);
1152 WMHangData(panel->texLs, panel);
1153 WMSetListAction(panel->texLs, textureClick, panel);
1154 WMSetListDoubleAction(panel->texLs, textureDoubleClick, panel);
1156 /* command buttons */
1158 font = WMSystemFontOfSize(scr, 10);
1161 panel->newB = WMCreateCommandButton(panel->frame);
1162 WMResizeWidget(panel->newB, 56, 48);
1163 WMMoveWidget(panel->newB, 285, 180);
1164 WMSetButtonFont(panel->newB, font);
1165 WMSetButtonImagePosition(panel->newB, WIPAbove);
1166 WMSetButtonText(panel->newB, _("New"));
1167 WMSetButtonAction(panel->newB, newTexture, panel);
1168 SetButtonAlphaImage(scr, panel->newB, TNEW_FILE);
1170 panel->ripB = WMCreateCommandButton(panel->frame);
1171 WMResizeWidget(panel->ripB, 56, 48);
1172 WMMoveWidget(panel->ripB, 341, 180);
1173 WMSetButtonFont(panel->ripB, font);
1174 WMSetButtonImagePosition(panel->ripB, WIPAbove);
1175 WMSetButtonText(panel->ripB, _("Extract..."));
1176 WMSetButtonAction(panel->ripB, extractTexture, panel);
1177 SetButtonAlphaImage(scr, panel->ripB, TEXTR_FILE);
1179 WMSetButtonEnabled(panel->ripB, False);
1181 panel->editB = WMCreateCommandButton(panel->frame);
1182 WMResizeWidget(panel->editB, 56, 48);
1183 WMMoveWidget(panel->editB, 397, 180);
1184 WMSetButtonFont(panel->editB, font);
1185 WMSetButtonImagePosition(panel->editB, WIPAbove);
1186 WMSetButtonText(panel->editB, _("Edit"));
1187 SetButtonAlphaImage(scr, panel->editB, TEDIT_FILE);
1188 WMSetButtonAction(panel->editB, editTexture, panel);
1190 panel->delB = WMCreateCommandButton(panel->frame);
1191 WMResizeWidget(panel->delB, 56, 48);
1192 WMMoveWidget(panel->delB, 453, 180);
1193 WMSetButtonFont(panel->delB, font);
1194 WMSetButtonImagePosition(panel->delB, WIPAbove);
1195 WMSetButtonText(panel->delB, _("Delete"));
1196 SetButtonAlphaImage(scr, panel->delB, TDEL_FILE);
1197 WMSetButtonEnabled(panel->delB, False);
1198 WMSetButtonAction(panel->delB, deleteTexture, panel);
1200 WMReleaseFont(font);
1202 /**/
1204 WMRealizeWidget(panel->frame);
1205 WMMapSubwidgets(panel->frame);
1207 WMSetPopUpButtonSelectedItem(panel->secP, 0);
1209 showData(panel);
1211 changePage(panel->secP, panel);
1213 fillTextureList(panel->texLs);
1215 panel->texturePanel = CreateTexturePanel(panel->win);
1220 static void
1221 setupTextureFor(WMList *list, char *key, char *defValue, char *title,
1222 int index)
1224 WMListItem *item;
1225 TextureListItem *titem;
1227 titem = wmalloc(sizeof(TextureListItem));
1228 memset(titem, 0, sizeof(TextureListItem));
1230 titem->title = wstrdup(title);
1231 titem->prop = GetObjectForKey(key);
1232 if (!titem->prop) {
1233 titem->prop = PLGetProplistWithDescription(defValue);
1234 } else {
1235 PLRetain(titem->prop);
1237 titem->texture = PLGetDescription((proplist_t)titem->prop);
1238 titem->current = 1;
1239 titem->selectedFor = 1<<index;
1241 titem->ispixmap = isPixmap(titem->prop);
1243 titem->preview = renderTexture(WMWidgetScreen(list), titem->prop,
1244 TEXPREV_WIDTH, TEXPREV_HEIGHT, NULL, 0);
1246 item = WMAddListItem(list, "");
1247 item->clientData = titem;
1252 static void
1253 showData(_Panel *panel)
1255 int i = 0;
1257 setupTextureFor(panel->texLs, "FTitleBack", "(solid, black)",
1258 "[Focused]", i);
1259 panel->textureIndex[i] = i++;
1261 setupTextureFor(panel->texLs, "UTitleBack", "(solid, gray)",
1262 "[Unfocused]", i);
1263 panel->textureIndex[i] = i++;
1265 setupTextureFor(panel->texLs, "PTitleBack", "(solid, \"#616161\")",
1266 "[Owner of Focused]", i);
1267 panel->textureIndex[i] = i++;
1269 setupTextureFor(panel->texLs, "MenuTitleBack", "(solid, black)",
1270 "[Menu Title]", i);
1271 panel->textureIndex[i] = i++;
1273 setupTextureFor(panel->texLs, "MenuTextBack", "(solid, gray)",
1274 "[Menu Item]", i);
1275 panel->textureIndex[i] = i++;
1277 setupTextureFor(panel->texLs, "IconBack", "(solid, gray)", "[Icon]", i);
1278 panel->textureIndex[i] = i++;
1280 setupTextureFor(panel->texLs, "WorkspaceBack", "(solid, black)",
1281 "[Workspace]", i);
1282 panel->textureIndex[i] = i++;
1285 updatePreviewBox(panel, EVERYTHING);
1290 static void
1291 storeData(_Panel *panel)
1293 TextureListItem *titem;
1294 WMListItem *item;
1296 item = WMGetListItem(panel->texLs, panel->textureIndex[0]);
1297 titem = (TextureListItem*)item->clientData;
1298 SetObjectForKey(titem->prop, "FTitleBack");
1300 item = WMGetListItem(panel->texLs, panel->textureIndex[1]);
1301 titem = (TextureListItem*)item->clientData;
1302 SetObjectForKey(titem->prop, "UTitleBack");
1304 item = WMGetListItem(panel->texLs, panel->textureIndex[2]);
1305 titem = (TextureListItem*)item->clientData;
1306 SetObjectForKey(titem->prop, "PTitleBack");
1308 item = WMGetListItem(panel->texLs, panel->textureIndex[3]);
1309 titem = (TextureListItem*)item->clientData;
1310 SetObjectForKey(titem->prop, "MenuTitleBack");
1312 item = WMGetListItem(panel->texLs, panel->textureIndex[4]);
1313 titem = (TextureListItem*)item->clientData;
1314 SetObjectForKey(titem->prop, "MenuTextBack");
1316 item = WMGetListItem(panel->texLs, panel->textureIndex[5]);
1317 titem = (TextureListItem*)item->clientData;
1318 SetObjectForKey(titem->prop, "IconBack");
1322 static void
1323 prepareForClose(_Panel *panel)
1325 proplist_t textureList;
1326 proplist_t texture;
1327 int i;
1328 TextureListItem *titem;
1329 WMListItem *item;
1330 WMUserDefaults *udb = WMGetStandardUserDefaults();
1332 textureList = PLMakeArrayFromElements(NULL, NULL);
1334 /* store list of textures */
1335 for (i = 6; i < WMGetListNumberOfRows(panel->texLs); i++) {
1336 item = WMGetListItem(panel->texLs, i);
1337 titem = (TextureListItem*)item->clientData;
1339 texture = PLMakeArrayFromElements(PLMakeString(titem->title),
1340 PLRetain(titem->prop),
1341 PLMakeString(titem->path),
1342 NULL);
1344 PLAppendArrayElement(textureList, texture);
1347 WMSetUDObjectForKey(udb, textureList, "TextureList");
1348 PLRelease(textureList);
1350 WMSynchronizeUserDefaults(udb);
1355 Panel*
1356 InitAppearance(WMScreen *scr, WMWindow *win)
1358 _Panel *panel;
1360 panel = wmalloc(sizeof(_Panel));
1361 memset(panel, 0, sizeof(_Panel));
1363 panel->sectionName = _("Appearance Preferences");
1365 panel->win = win;
1367 panel->callbacks.createWidgets = createPanel;
1368 panel->callbacks.updateDomain = storeData;
1369 panel->callbacks.prepareForClose = prepareForClose;
1371 AddSection(panel, ICON_FILE);
1373 return panel;
1378 /****************************************************************************/
1382 typedef struct ExtractPanel {
1383 WMWindow *win;
1385 WMLabel *label;
1386 WMList *list;
1388 WMButton *closeB;
1389 WMButton *extrB;
1390 } ExtractPanel;
1394 static void
1395 OpenExtractPanelFor(_Panel *panel, char *path)
1397 ExtractPanel *epanel;
1398 WMColor *color;
1399 WMFont *font;
1400 WMScreen *scr = WMWidgetScreen(panel->win);
1402 epanel = wmalloc(sizeof(ExtractPanel));
1403 epanel->win = WMCreatePanelWithStyleForWindow(panel->win, "extract",
1404 WMTitledWindowMask
1405 |WMClosableWindowMask);
1406 WMResizeWidget(epanel->win, 245, 250);
1407 WMSetWindowTitle(epanel->win, _("Extract Texture"));
1409 epanel->label = WMCreateLabel(epanel->win);
1410 WMResizeWidget(epanel->label, 225, 18);
1411 WMMoveWidget(epanel->label, 10, 10);
1412 WMSetLabelTextAlignment(epanel->label, WACenter);
1413 WMSetLabelRelief(epanel->label, WRSunken);
1415 color = WMDarkGrayColor(scr);
1416 WMSetWidgetBackgroundColor(epanel->label, color);
1417 WMReleaseColor(color);
1419 color = WMWhiteColor(scr);
1420 WMSetLabelTextColor(epanel->label, color);
1421 WMReleaseColor(color);
1423 font = WMBoldSystemFontOfSize(scr, 12);
1424 WMSetLabelFont(epanel->label, font);
1425 WMReleaseFont(font);
1427 WMSetLabelText(epanel->label, _("Textures"));
1429 epanel->list = WMCreateList(epanel->win);
1430 WMResizeWidget(epanel->list, 225, 165);
1431 WMMoveWidget(epanel->list, 10, 30);
1435 epanel->closeB = WMCreateCommandButton(epanel->win);
1436 WMResizeWidget(epanel->closeB, 74, 24);
1437 WMMoveWidget(epanel->closeB, 165, 215);
1438 WMSetButtonText(epanel->closeB, _("Close"));
1440 epanel->extrB = WMCreateCommandButton(epanel->win);
1441 WMResizeWidget(epanel->extrB, 74, 24);
1442 WMMoveWidget(epanel->extrB, 80, 215);
1443 WMSetButtonText(epanel->extrB, _("Extract"));
1445 WMMapSubwidgets(epanel->win);
1448 /* take textures from file */
1452 WMRealizeWidget(epanel->win);
1454 WMMapWidget(epanel->win);