WPrefs appearance stuff update (shows preview)
[wmaker-crm.git] / WPrefs.app / TexturePanel.c
blob637b7bac4842b1607000ce043c14d92be72b1dc2
1 /* TexturePanel.c- texture editting panel
2 *
3 * WPrefs - WindowMaker Preferences Program
4 *
5 * Copyright (c) 1998, 1999 Alfredo K. Kojima
6 * Copyright (c) 1998 James Thompson
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
21 * USA.
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <string.h>
28 #include <unistd.h>
29 #include <ctype.h>
31 #include <X11/Xlib.h>
34 #include <WINGs.h>
35 #include <WUtil.h>
37 #include "WPrefs.h"
39 #include "TexturePanel.h"
42 #define MAX_SECTION_PARTS 4
44 typedef struct _TexturePanel {
45 WMWindow *win;
47 /* texture name */
48 WMFrame *nameF;
49 WMTextField *nameT;
51 /* texture type */
52 WMPopUpButton *typeP;
54 /* default color */
55 WMFrame *defcF;
56 WMColorWell *defcW;
58 WMFont *listFont;
60 /*-- Gradient --*/
62 Pixmap gimage;
64 /* colors */
65 WMFrame *gcolF;
66 WMList *gcolL;
67 WMButton *gcolaB;
68 WMButton *gcoldB;
69 WMSlider *ghueS;
70 WMSlider *gsatS;
71 WMSlider *gvalS;
73 WMSlider *gbriS;
74 WMSlider *gconS;
76 /* direction (common) */
77 WMFrame *dirF;
78 WMButton *dirhB;
79 WMButton *dirvB;
80 WMButton *dirdB;
82 /*-- Simple Gradient --*/
85 /*-- Textured Gradient --*/
87 WMFrame *tcolF;
88 WMColorWell *tcol1W;
89 WMColorWell *tcol2W;
91 WMFrame *topaF;
92 WMSlider *topaS;
94 /*-- Image --*/
95 WMFrame *imageF;
96 WMScrollView *imageV;
97 WMTextField *imageT;
98 WMLabel *imageL;
99 WMButton *browB;
100 WMButton *dispB;
101 WMPopUpButton *arrP;
103 char *imageFile;
105 /*****/
107 WMButton *okB;
108 WMButton *cancelB;
111 WMCallback *okAction;
112 void *okData;
114 WMCallback *cancelAction;
115 void *cancelData;
117 /****/
118 WMWidget *sectionParts[5][MAX_SECTION_PARTS];
120 int currentType;
123 proplist_t pathList;
125 } _TexturePanel;
129 #define TYPE_SOLID 0
130 #define TYPE_GRADIENT 1
131 #define TYPE_SGRADIENT 2
132 #define TYPE_TGRADIENT 3
133 #define TYPE_PIXMAP 4
136 #define PTYPE_TILE 0
137 #define PTYPE_SCALE 1
138 #define PTYPE_CENTER 2
139 #define PTYPE_MAXIMIZE 3
144 *--------------------------------------------------------------------------
145 * Private Functions
146 *--------------------------------------------------------------------------
149 /************/
151 static void
152 updateGradButtons(TexturePanel *panel)
154 RImage *image;
155 WMPixmap *pixmap;
156 int colorn;
157 RColor **colors;
159 colorn = WMGetListNumberOfRows(panel->gcolL);
160 if (colorn < 1) {
161 pixmap = NULL;
162 } else {
163 int i;
164 WMListItem *item;
166 colors = wmalloc(sizeof(RColor*)*(colorn+1));
168 for (i = 0; i < colorn; i++) {
169 item = WMGetListItem(panel->gcolL, i);
170 colors[i] = (RColor*)item->clientData;
172 colors[i] = NULL;
174 image = RRenderMultiGradient(80, 30, colors, RHorizontalGradient);
175 pixmap = WMCreatePixmapFromRImage(WMWidgetScreen(panel->gcolL),
176 image, 128);
177 RDestroyImage(image);
178 WMSetButtonImage(panel->dirhB, pixmap);
179 WMReleasePixmap(pixmap);
181 image = RRenderMultiGradient(80, 30, colors, RVerticalGradient);
182 pixmap = WMCreatePixmapFromRImage(WMWidgetScreen(panel->gcolL),
183 image, 128);
184 RDestroyImage(image);
185 WMSetButtonImage(panel->dirvB, pixmap);
186 WMReleasePixmap(pixmap);
188 image = RRenderMultiGradient(80, 30, colors, RDiagonalGradient);
189 pixmap = WMCreatePixmapFromRImage(WMWidgetScreen(panel->gcolL),
190 image, 128);
191 RDestroyImage(image);
192 WMSetButtonImage(panel->dirdB, pixmap);
193 WMReleasePixmap(pixmap);
195 free(colors);
200 static void
201 updateSGradButtons(TexturePanel *panel)
203 RImage *image;
204 WMPixmap *pixmap;
205 RColor from;
206 RColor to;
207 WMColor *color;
209 color = WMGetColorWellColor(panel->tcol1W);
210 from.red = WMRedComponentOfColor(color)>>8;
211 from.green = WMGreenComponentOfColor(color)>>8;
212 from.blue = WMBlueComponentOfColor(color)>>8;
214 color = WMGetColorWellColor(panel->tcol2W);
215 to.red = WMRedComponentOfColor(color)>>8;
216 to.green = WMGreenComponentOfColor(color)>>8;
217 to.blue = WMBlueComponentOfColor(color)>>8;
219 image = RRenderGradient(80, 30, &from, &to, RHorizontalGradient);
220 pixmap = WMCreatePixmapFromRImage(WMWidgetScreen(panel->gcolL),
221 image, 128);
222 RDestroyImage(image);
223 WMSetButtonImage(panel->dirhB, pixmap);
224 WMReleasePixmap(pixmap);
226 image = RRenderGradient(80, 30, &from, &to, RVerticalGradient);
227 pixmap = WMCreatePixmapFromRImage(WMWidgetScreen(panel->gcolL),
228 image, 128);
229 RDestroyImage(image);
230 WMSetButtonImage(panel->dirvB, pixmap);
231 WMReleasePixmap(pixmap);
233 image = RRenderGradient(80, 30, &from, &to, RDiagonalGradient);
234 pixmap = WMCreatePixmapFromRImage(WMWidgetScreen(panel->gcolL),
235 image, 128);
236 RDestroyImage(image);
237 WMSetButtonImage(panel->dirdB, pixmap);
238 WMReleasePixmap(pixmap);
242 static void
243 changeTypeCallback(WMWidget *w, void *data)
245 TexturePanel *panel = (TexturePanel*)data;
246 int newType;
247 int i;
249 newType = WMGetPopUpButtonSelectedItem(w);
250 if (newType == panel->currentType)
251 return;
253 if (panel->currentType >= 0) {
254 for (i = 0; i < MAX_SECTION_PARTS; i++) {
255 if (panel->sectionParts[panel->currentType][i] == NULL)
256 break;
257 WMUnmapWidget(panel->sectionParts[panel->currentType][i]);
261 for (i = 0; i < MAX_SECTION_PARTS; i++) {
262 if (panel->sectionParts[newType][i] == NULL)
263 break;
264 WMMapWidget(panel->sectionParts[newType][i]);
266 panel->currentType = newType;
268 switch (newType) {
269 case TYPE_SGRADIENT:
270 updateSGradButtons(panel);
271 break;
272 case TYPE_GRADIENT:
273 updateGradButtons(panel);
274 break;
279 /*********** Gradient ************/
281 static void
282 updateSVSlider(WMSlider *sPtr, Bool saturation, WMFont *font, RHSVColor *hsv)
284 RImage *image;
285 WMPixmap *pixmap;
286 WMScreen *scr = WMWidgetScreen(sPtr);
287 RColor from, to;
288 RHSVColor tmp;
290 tmp = *hsv;
291 if (saturation) {
292 tmp.saturation = 0;
293 RHSVtoRGB(&tmp, &from);
294 tmp.saturation = 255;
295 RHSVtoRGB(&tmp, &to);
296 } else {
297 tmp.value = 0;
298 RHSVtoRGB(&tmp, &from);
299 tmp.value = 255;
300 RHSVtoRGB(&tmp, &to);
302 image = RRenderGradient(130, 16, &from, &to, RHorizontalGradient);
303 pixmap = WMCreatePixmapFromRImage(scr, image, 128);
304 RDestroyImage(image);
306 if (hsv->value < 128 || !saturation) {
307 WMColor *col = WMWhiteColor(scr);
309 WMDrawString(scr, WMGetPixmapXID(pixmap), WMColorGC(col), font, 2,
310 (16 - WMFontHeight(font))/2 - 1,
311 saturation ? "Saturation" : "Brightness", 10);
312 WMReleaseColor(col);
313 } else {
314 WMColor *col = WMBlackColor(scr);
316 WMDrawString(scr, WMGetPixmapXID(pixmap), WMColorGC(col), font, 2,
317 (16 - WMFontHeight(font))/2 - 1,
318 saturation ? "Saturation" : "Brightness", 10);
319 WMReleaseColor(col);
321 WMSetSliderImage(sPtr, pixmap);
322 WMReleasePixmap(pixmap);
326 static void
327 updateHueSlider(WMSlider *sPtr, WMFont *font, RHSVColor *hsv)
329 RColor *colors[8];
330 RImage *image;
331 WMPixmap *pixmap;
332 WMScreen *scr = WMWidgetScreen(sPtr);
333 RHSVColor thsv;
334 int i;
336 thsv = *hsv;
337 for (i = 0; i <= 6; i++) {
338 thsv.hue = (360*i)/6;
339 colors[i] = wmalloc(sizeof(RColor));
340 RHSVtoRGB(&thsv, colors[i]);
342 colors[i] = NULL;
344 image = RRenderMultiGradient(130, 16, colors, RGRD_HORIZONTAL);
345 pixmap = WMCreatePixmapFromRImage(scr, image, 128);
346 RDestroyImage(image);
348 if (hsv->value < 128) {
349 WMColor *col = WMWhiteColor(scr);
351 WMDrawString(scr, WMGetPixmapXID(pixmap), WMColorGC(col), font, 2,
352 (16 - WMFontHeight(font))/2 - 1, "Hue", 3);
353 WMReleaseColor(col);
354 } else {
355 WMColor *col = WMBlackColor(scr);
357 WMDrawString(scr, WMGetPixmapXID(pixmap), WMColorGC(col), font, 2,
358 (16 - WMFontHeight(font))/2 - 1, "Hue", 3);
359 WMReleaseColor(col);
361 WMSetSliderImage(sPtr, pixmap);
362 WMReleasePixmap(pixmap);
364 for (i = 0; i <= 6; i++)
365 free(colors[i]);
370 static void
371 sliderChangeCallback(WMWidget *w, void *data)
373 TexturePanel *panel = (TexturePanel*)data;
374 RHSVColor hsv, *hsvp;
375 int row, rows;
376 WMListItem *item;
377 RColor **colors;
378 int i;
379 RImage *image;
380 WMScreen *scr = WMWidgetScreen(w);
382 hsv.hue = WMGetSliderValue(panel->ghueS);
383 hsv.saturation = WMGetSliderValue(panel->gsatS);
384 hsv.value = WMGetSliderValue(panel->gvalS);
386 row = WMGetListSelectedItemRow(panel->gcolL);
387 if (row >= 0) {
388 RColor *rgb;
390 item = WMGetListItem(panel->gcolL, row);
392 rgb = (RColor*)item->clientData;
394 RHSVtoRGB(&hsv, rgb);
396 sprintf(item->text, "%02x,%02x,%02x", rgb->red, rgb->green, rgb->blue);
399 if (w == panel->ghueS) {
400 updateSVSlider(panel->gsatS, True, panel->listFont, &hsv);
401 updateSVSlider(panel->gvalS, False, panel->listFont, &hsv);
402 } else if (w == panel->gsatS) {
403 updateHueSlider(panel->ghueS, panel->listFont, &hsv);
404 updateSVSlider(panel->gvalS, False, panel->listFont, &hsv);
405 } else {
406 updateHueSlider(panel->ghueS, panel->listFont, &hsv);
407 updateSVSlider(panel->gsatS, True, panel->listFont, &hsv);
410 rows = WMGetListNumberOfRows(panel->gcolL);
411 if (rows == 0)
412 return;
414 colors = wmalloc(sizeof(RColor*)*(rows+1));
416 for (i = 0; i < rows; i++) {
417 item = WMGetListItem(panel->gcolL, i);
419 colors[i] = (RColor*)item->clientData;
421 colors[i] = NULL;
423 if (panel->gimage != None) {
424 XFreePixmap(WMScreenDisplay(scr), panel->gimage);
427 image = RRenderMultiGradient(30, i*WMGetListItemHeight(panel->gcolL),
428 colors, RVerticalGradient);
429 RConvertImage(WMScreenRContext(scr), image, &panel->gimage);
430 RDestroyImage(image);
432 free(colors);
434 WMRedisplayWidget(panel->gcolL);
436 updateGradButtons(panel);
440 static void
441 paintGradListItem(WMList *lPtr, int index, Drawable d, char *text, int state,
442 WMRect *rect)
444 TexturePanel *panel = (TexturePanel*)WMGetHangedData(lPtr);
445 WMScreen *scr = WMWidgetScreen(lPtr);
446 int width, height, x, y;
447 Display *dpy;
448 WMColor *white = WMWhiteColor(scr);
449 WMListItem *item;
450 WMColor *black = WMBlackColor(scr);
452 dpy = WMScreenDisplay(scr);
454 width = rect->size.width;
455 height = rect->size.height;
456 x = rect->pos.x;
457 y = rect->pos.y;
459 if (state & WLDSSelected)
460 XFillRectangle(dpy, d, WMColorGC(white), x, y, width, height);
461 else
462 XClearArea(dpy, d, x, y, width, height, False);
464 item = WMGetListItem(lPtr, index);
466 if (panel->gimage) {
467 XCopyArea(WMScreenDisplay(scr), panel->gimage, d, WMColorGC(white),
468 0, height*index, 30, height, x + 5, y);
470 WMDrawString(scr, d, WMColorGC(black), panel->listFont,
471 x + 40, y + 1, text, strlen(text));
473 WMReleaseColor(white);
474 WMReleaseColor(black);
479 static void
480 gradAddCallback(WMWidget *w, void *data)
482 TexturePanel *panel = (TexturePanel*)data;
483 WMListItem *item;
484 int row;
485 RColor *rgb;
487 row = WMGetListSelectedItemRow(panel->gcolL) + 1;
488 item = WMInsertListItem(panel->gcolL, row, "00,00,00");
489 rgb = wmalloc(sizeof(RColor));
490 memset(rgb, 0, sizeof(RColor));
491 item->clientData = rgb;
493 WMSelectListItem(panel->gcolL, row);
495 updateGradButtons(panel);
497 sliderChangeCallback(panel->ghueS, panel);
502 static void
503 gradClickCallback(WMWidget *w, void *data)
505 TexturePanel *panel = (TexturePanel*)data;
506 WMListItem *item;
507 int row;
508 RHSVColor hsv;
510 row = WMGetListSelectedItemRow(w);
511 if (row < 0)
512 return;
514 item = WMGetListItem(panel->gcolL, row);
515 RRGBtoHSV((RColor*)item->clientData, &hsv);
517 WMSetSliderValue(panel->ghueS, hsv.hue);
518 WMSetSliderValue(panel->gsatS, hsv.saturation);
519 WMSetSliderValue(panel->gvalS, hsv.value);
521 sliderChangeCallback(panel->ghueS, panel);
522 sliderChangeCallback(panel->gsatS, panel);
526 static void
527 gradDeleteCallback(WMWidget *w, void *data)
529 TexturePanel *panel = (TexturePanel*)data;
530 WMListItem *item;
531 int row;
533 row = WMGetListSelectedItemRow(panel->gcolL);
534 if (row < 0)
535 return;
537 item = WMGetListItem(panel->gcolL, row);
538 free(item->clientData);
540 WMRemoveListItem(panel->gcolL, row);
542 WMSelectListItem(panel->gcolL, row - 1);
544 updateGradButtons(panel);
546 gradClickCallback(panel->gcolL, panel);
550 /*************** Simple Gradient ***************/
552 static void
553 colorWellObserver(void *self, WMNotification *n)
555 updateSGradButtons(self);
559 /****************** Image ******************/
561 static void
562 browseImageCallback(WMWidget *w, void *data)
564 TexturePanel *panel = (TexturePanel*)data;
565 WMOpenPanel *opanel;
566 WMScreen *scr = WMWidgetScreen(w);
568 opanel = WMGetOpenPanel(scr);
569 WMSetFilePanelCanChooseDirectories(opanel, False);
570 WMSetFilePanelCanChooseFiles(opanel, True);
572 if (WMRunModalFilePanelForDirectory(opanel, panel->win, wgethomedir(),
573 "Open Image", NULL)) {
574 char *path, *fullpath;
575 char *tmp, *tmp2;
577 fullpath = WMGetFilePanelFileName(opanel);
578 if (!fullpath)
579 return;
580 path = wstrdup(fullpath);
582 tmp2 = strrchr(fullpath, '/');
583 if (tmp2)
584 tmp2++;
586 tmp = wfindfileinarray(panel->pathList, tmp2);
588 if (tmp) {
589 if (strcmp(fullpath, tmp)==0) {
590 free(path);
591 path = tmp2;
593 free(tmp);
596 if (!RGetImageFileFormat(fullpath)) {
597 WMRunAlertPanel(scr, panel->win, _("Error"),
598 _("The selected file does not contain a supported image."),
599 _("OK"), NULL, NULL);
600 free(path);
601 free(fullpath);
602 } else {
603 RImage *image, *scaled;
604 WMPixmap *pixmap;
605 WMSize size;
607 image = RLoadImage(WMScreenRContext(scr), fullpath, 0);
608 if (!image) {
609 char *message;
611 message = wstrappend(_("Could not load the selected file: "),
612 (char*)RMessageForError(RErrorCode));
614 WMRunAlertPanel(scr, panel->win, _("Error"), message,
615 _("OK"), NULL, NULL);
616 free(message);
617 free(path);
618 free(fullpath);
619 return;
622 pixmap = WMCreatePixmapFromRImage(scr, image, 128);
623 RDestroyImage(image);
625 size = WMGetPixmapSize(pixmap);
626 WMSetLabelImage(panel->imageL, pixmap);
627 WMResizeWidget(panel->imageL, size.width, size.height);
629 WMReleasePixmap(pixmap);
631 panel->imageFile = path;
633 WMSetTextFieldText(panel->imageT, path);
635 free(fullpath);
642 static void
643 buttonCallback(WMWidget *w, void *data)
645 TexturePanel *panel = (TexturePanel*)data;
647 if (w == panel->okB) {
648 (*panel->okAction)(panel->okData);
649 } else {
650 (*panel->cancelAction)(panel->cancelData);
656 *--------------------------------------------------------------------------
657 * Public functions
658 *--------------------------------------------------------------------------
660 void
661 DestroyTexturePanel(TexturePanel *panel)
667 void
668 ShowTexturePanel(TexturePanel *panel)
670 WMMapWidget(panel->win);
674 void
675 HideTexturePanel(TexturePanel *panel)
677 WMUnmapWidget(panel->win);
681 void
682 SetTexturePanelOkAction(TexturePanel *panel, WMCallback *action, void *clientData)
684 panel->okAction = action;
685 panel->okData = clientData;
689 void
690 SetTexturePanelCancelAction(TexturePanel *panel, WMCallback *action, void *clientData)
692 panel->cancelAction = action;
693 panel->cancelData = clientData;
697 void
698 SetTexturePanelTexture(TexturePanel *panel, char *name, proplist_t texture)
700 WMScreen *scr = WMWidgetScreen(panel->win);
701 char *str, *type;
702 proplist_t p;
703 WMColor *color;
704 int i;
705 char buffer[64];
706 int gradient = 0;
708 WMSetTextFieldText(panel->nameT, name);
710 if (!texture)
711 return;
713 p = PLGetArrayElement(texture, 0);
714 if (!p) {
715 goto bad_texture;
717 type = PLGetString(p);
719 /*...............................................*/
720 if (strcasecmp(type, "solid")==0) {
722 WMSetPopUpButtonSelectedItem(panel->typeP, TYPE_SOLID);
724 p = PLGetArrayElement(texture, 1);
725 if (!p) {
726 goto bad_texture;
729 str = PLGetString(p);
730 color = WMCreateNamedColor(scr, str, False);
732 WMSetColorWellColor(panel->defcW, color);
734 WMReleaseColor(color);
735 /*...............................................*/
736 } else if (strcasecmp(type, "hgradient")==0
737 || strcasecmp(type, "vgradient")==0
738 || strcasecmp(type, "dgradient")==0) {
740 WMSetPopUpButtonSelectedItem(panel->typeP, TYPE_SGRADIENT);
742 p = PLGetArrayElement(texture, 1);
743 if (!p) {
744 goto bad_texture;
746 str = PLGetString(p);
747 color = WMCreateNamedColor(scr, str, False);
749 WMSetColorWellColor(panel->tcol1W, color);
751 WMReleaseColor(color);
753 p = PLGetArrayElement(texture, 2);
754 if (!p) {
755 goto bad_texture;
757 str = PLGetString(p);
758 color = WMCreateNamedColor(scr, str, False);
760 WMSetColorWellColor(panel->tcol2W, color);
762 WMReleaseColor(color);
764 gradient = type[0];
765 /*...............................................*/
766 } else if (strcasecmp(type, "thgradient")==0
767 || strcasecmp(type, "tvgradient")==0
768 || strcasecmp(type, "tdgradient")==0) {
770 WMSetPopUpButtonSelectedItem(panel->typeP, TYPE_TGRADIENT);
772 /****** TODO: setador de textura apartir desse */
773 gradient = type[1];
774 /*...............................................*/
775 } else if (strcasecmp(type, "mhgradient")==0
776 || strcasecmp(type, "mvgradient")==0
777 || strcasecmp(type, "mdgradient")==0) {
778 WMListItem *item;
780 for (i = 0; i < WMGetListNumberOfRows(panel->gcolL); i++) {
781 item = WMGetListItem(panel->gcolL, i);
782 free(item->clientData);
784 WMClearList(panel->gcolL);
786 WMSetPopUpButtonSelectedItem(panel->typeP, TYPE_GRADIENT);
788 p = PLGetArrayElement(texture, 1);
789 if (!p) {
790 goto bad_texture;
793 str = PLGetString(p);
794 color = WMCreateNamedColor(scr, str, False);
796 WMSetColorWellColor(panel->defcW, color);
798 WMReleaseColor(color);
800 for (i = 2; i < PLGetNumberOfElements(texture); i++) {
801 RColor *rgb;
802 XColor xcolor;
804 p = PLGetArrayElement(texture, i);
805 if (!p) {
806 goto bad_texture;
808 str = PLGetString(p);
810 XParseColor(WMScreenDisplay(scr), WMScreenRContext(scr)->cmap,
811 str, &xcolor);
813 rgb = wmalloc(sizeof(RColor));
814 rgb->red = xcolor.red >> 8;
815 rgb->green = xcolor.green >> 8;
816 rgb->blue = xcolor.blue >> 8;
817 sprintf(buffer, "%02x,%02x,%02x", rgb->red, rgb->green, rgb->blue);
819 item = WMAddListItem(panel->gcolL, buffer);
820 item->clientData = rgb;
823 sliderChangeCallback(panel->ghueS, panel);
825 gradient = type[1];
826 /*...............................................*/
827 } else if (strcasecmp(type, "cpixmap")==0
828 || strcasecmp(type, "spixmap")==0
829 || strcasecmp(type, "mpixmap")==0
830 || strcasecmp(type, "tpixmap")==0) {
832 WMSetPopUpButtonSelectedItem(panel->typeP, TYPE_PIXMAP);
834 switch (toupper(type[0])) {
835 case 'C':
836 WMSetPopUpButtonSelectedItem(panel->arrP, PTYPE_CENTER);
837 break;
838 case 'S':
839 WMSetPopUpButtonSelectedItem(panel->arrP, PTYPE_SCALE);
840 break;
841 case 'M':
842 WMSetPopUpButtonSelectedItem(panel->arrP, PTYPE_MAXIMIZE);
843 break;
844 default:
845 case 'T':
846 WMSetPopUpButtonSelectedItem(panel->arrP, PTYPE_TILE);
847 break;
850 WMSetTextFieldText(panel->imageT,
851 PLGetString(PLGetArrayElement(texture, 1)));
854 changeTypeCallback(panel->typeP, panel);
856 if (gradient > 0) {
857 updateGradButtons(panel);
859 switch (toupper(gradient)) {
860 case 'H':
861 WMPerformButtonClick(panel->dirhB);
862 break;
863 case 'V':
864 WMPerformButtonClick(panel->dirvB);
865 break;
866 default:
867 case 'D':
868 WMPerformButtonClick(panel->dirdB);
869 break;
873 return;
875 bad_texture:
876 str = PLGetDescription(texture);
877 wwarning("error creating texture %s", str);
878 free(str);
884 char*
885 GetTexturePanelTextureName(TexturePanel *panel)
887 return WMGetTextFieldText(panel->nameT);
893 proplist_t
894 GetTexturePanelTexture(TexturePanel *panel)
896 proplist_t prop = NULL;
897 WMColor *color;
898 char *str, *str2;
899 char buff[32];
900 int i;
903 switch (WMGetPopUpButtonSelectedItem(panel->typeP)) {
905 case TYPE_SOLID:
906 color = WMGetColorWellColor(panel->defcW);
907 str = WMGetColorRGBDescription(color);
908 prop = PLMakeArrayFromElements(PLMakeString("solid"),
909 PLMakeString(str), NULL);
910 free(str);
912 break;
914 case TYPE_PIXMAP:
915 color = WMGetColorWellColor(panel->defcW);
916 str = WMGetColorRGBDescription(color);
918 switch (WMGetPopUpButtonSelectedItem(panel->arrP)) {
919 case PTYPE_SCALE:
920 prop = PLMakeArrayFromElements(PLMakeString("spixmap"),
921 PLMakeString(panel->imageFile),
922 PLMakeString(str), NULL);
923 break;
924 case PTYPE_MAXIMIZE:
925 prop = PLMakeArrayFromElements(PLMakeString("mpixmap"),
926 PLMakeString(panel->imageFile),
927 PLMakeString(str), NULL);
928 break;
929 case PTYPE_CENTER:
930 prop = PLMakeArrayFromElements(PLMakeString("cpixmap"),
931 PLMakeString(panel->imageFile),
932 PLMakeString(str), NULL);
933 break;
934 case PTYPE_TILE:
935 prop = PLMakeArrayFromElements(PLMakeString("tpixmap"),
936 PLMakeString(panel->imageFile),
937 PLMakeString(str), NULL);
938 break;
940 free(str);
941 break;
943 case TYPE_SGRADIENT:
944 color = WMGetColorWellColor(panel->tcol1W);
945 str = WMGetColorRGBDescription(color);
947 color = WMGetColorWellColor(panel->tcol2W);
948 str2 = WMGetColorRGBDescription(color);
950 if (WMGetButtonSelected(panel->dirdB)) {
951 prop = PLMakeArrayFromElements(PLMakeString("dgradient"),
952 PLMakeString(str),
953 PLMakeString(str2), NULL);
954 } else if (WMGetButtonSelected(panel->dirvB)) {
955 prop = PLMakeArrayFromElements(PLMakeString("vgradient"),
956 PLMakeString(str),
957 PLMakeString(str2), NULL);
958 } else {
959 prop = PLMakeArrayFromElements(PLMakeString("hgradient"),
960 PLMakeString(str),
961 PLMakeString(str2), NULL);
963 free(str);
964 free(str2);
965 break;
967 case TYPE_GRADIENT:
968 color = WMGetColorWellColor(panel->defcW);
969 str = WMGetColorRGBDescription(color);
971 if (WMGetButtonSelected(panel->dirdB)) {
972 prop = PLMakeArrayFromElements(PLMakeString("mdgradient"),
973 PLMakeString(str), NULL);
974 } else if (WMGetButtonSelected(panel->dirvB)) {
975 prop = PLMakeArrayFromElements(PLMakeString("mvgradient"),
976 PLMakeString(str), NULL);
977 } else {
978 prop = PLMakeArrayFromElements(PLMakeString("mhgradient"),
979 PLMakeString(str), NULL);
981 free(str);
983 for (i = 0; i < WMGetListNumberOfRows(panel->gcolL); i++) {
984 RColor *rgb;
985 WMListItem *item;
987 item = WMGetListItem(panel->gcolL, i);
989 rgb = (RColor*)item->clientData;
991 sprintf(buff, "#%02x%02x%02x", rgb->red, rgb->green, rgb->blue);
993 PLAppendArrayElement(prop, PLMakeString(buff));
995 break;
999 return prop;
1004 void
1005 SetTexturePanelPixmapPath(TexturePanel *panel, proplist_t array)
1007 panel->pathList = array;
1012 TexturePanel*
1013 CreateTexturePanel(WMWindow *keyWindow)
1014 /*CreateTexturePanel(WMScreen *scr)*/
1016 TexturePanel *panel;
1017 WMScreen *scr = WMWidgetScreen(keyWindow);
1019 panel = wmalloc(sizeof(TexturePanel));
1020 memset(panel, 0, sizeof(TexturePanel));
1022 panel->listFont = WMSystemFontOfSize(scr, 12);
1025 panel->win = WMCreatePanelWithStyleForWindow(keyWindow, "texturePanel",
1026 WMTitledWindowMask
1027 |WMClosableWindowMask);
1029 panel->win = WMCreateWindowWithStyle(scr, "texturePanel",
1030 WMTitledWindowMask
1031 |WMClosableWindowMask);
1034 WMResizeWidget(panel->win, 325, 423);
1035 WMSetWindowTitle(panel->win, _("Texture Panel"));
1038 /* texture name */
1039 panel->nameF = WMCreateFrame(panel->win);
1040 WMResizeWidget(panel->nameF, 185, 50);
1041 WMMoveWidget(panel->nameF, 15, 10);
1042 WMSetFrameTitle(panel->nameF, _("Texture Name"));
1044 panel->nameT = WMCreateTextField(panel->nameF);
1045 WMResizeWidget(panel->nameT, 160, 20);
1046 WMMoveWidget(panel->nameT, 12, 18);
1048 WMMapSubwidgets(panel->nameF);
1050 /* texture types */
1051 panel->typeP = WMCreatePopUpButton(panel->win);
1052 WMResizeWidget(panel->typeP, 185, 20);
1053 WMMoveWidget(panel->typeP, 15, 65);
1054 WMAddPopUpButtonItem(panel->typeP, _("Solid Color"));
1055 WMAddPopUpButtonItem(panel->typeP, _("Gradient Texture"));
1056 WMAddPopUpButtonItem(panel->typeP, _("Simple Gradient Texture"));
1057 WMAddPopUpButtonItem(panel->typeP, _("Textured Gradient"));
1058 WMAddPopUpButtonItem(panel->typeP, _("Image Texture"));
1059 WMSetPopUpButtonSelectedItem(panel->typeP, 0);
1060 WMSetPopUpButtonAction(panel->typeP, changeTypeCallback, panel);
1062 WMSetPopUpButtonItemEnabled(panel->typeP, TYPE_TGRADIENT, False);
1064 /* color */
1065 panel->defcF = WMCreateFrame(panel->win);
1066 WMResizeWidget(panel->defcF, 100, 75);
1067 WMMoveWidget(panel->defcF, 210, 10);
1068 WMSetFrameTitle(panel->defcF, _("Default Color"));
1070 panel->defcW = WMCreateColorWell(panel->defcF);
1071 WMResizeWidget(panel->defcW, 60, 45);
1072 WMMoveWidget(panel->defcW, 20, 20);
1074 WMMapSubwidgets(panel->defcF);
1076 /****** Gradient ******/
1077 panel->gcolF = WMCreateFrame(panel->win);
1078 WMResizeWidget(panel->gcolF, 295, 205);
1079 WMMoveWidget(panel->gcolF, 15, 95);
1080 WMSetFrameTitle(panel->gcolF, _("Gradient Colors"));
1082 panel->gcolL = WMCreateList(panel->gcolF);
1083 WMResizeWidget(panel->gcolL, 130, 140);
1084 WMMoveWidget(panel->gcolL, 10, 25);
1085 WMHangData(panel->gcolL, panel);
1086 WMSetListUserDrawProc(panel->gcolL, paintGradListItem);
1087 WMSetListAction(panel->gcolL, gradClickCallback, panel);
1089 panel->gcolaB = WMCreateCommandButton(panel->gcolF);
1090 WMResizeWidget(panel->gcolaB, 64, 24);
1091 WMMoveWidget(panel->gcolaB, 10, 170);
1092 WMSetButtonText(panel->gcolaB, _("Add"));
1093 WMSetButtonAction(panel->gcolaB, gradAddCallback, panel);
1095 panel->gcoldB = WMCreateCommandButton(panel->gcolF);
1096 WMResizeWidget(panel->gcoldB, 64, 24);
1097 WMMoveWidget(panel->gcoldB, 75, 170);
1098 WMSetButtonText(panel->gcoldB, _("Delete"));
1099 WMSetButtonAction(panel->gcoldB, gradDeleteCallback, panel);
1101 #if 0
1102 panel->gbriS = WMCreateSlider(panel->gcolF);
1103 WMResizeWidget(panel->gbriS, 130, 16);
1104 WMMoveWidget(panel->gbriS, 150, 25);
1105 WMSetSliderKnobThickness(panel->gbriS, 8);
1106 WMSetSliderMaxValue(panel->gbriS, 100);
1107 WMSetSliderAction(panel->gbriS, sliderChangeCallback, panel);
1109 WMPixmap *pixmap;
1110 WMColor *color;
1112 pixmap = WMCreatePixmap(scr, 130, 16, WMScreenDepth(scr), False);
1113 color = WMDarkGrayColor(scr);
1114 XFillRectangle(WMScreenDisplay(scr), WMGetPixmapXID(pixmap),
1115 WMColorGC(color), 0, 0, 130, 16);
1116 WMReleaseColor(color);
1117 color = WMWhiteColor(color);
1118 WMDrawString(scr, WMGetPixmapXID(pixmap), WMColorGC(color),
1119 panel->listFont, 2,
1120 (16 - WMFontHeight(panel->listFont))/2 - 1,
1121 "Brightness", 10);
1122 WMSetSliderImage(panel->gbriS, pixmap);
1123 WMReleasePixmap(pixmap);
1126 panel->gconS = WMCreateSlider(panel->gcolF);
1127 WMResizeWidget(panel->gconS, 130, 16);
1128 WMMoveWidget(panel->gconS, 150, 50);
1129 WMSetSliderKnobThickness(panel->gconS, 8);
1130 WMSetSliderMaxValue(panel->gconS, 100);
1131 WMSetSliderAction(panel->gconS, sliderChangeCallback, panel);
1133 WMPixmap *pixmap;
1134 WMColor *color;
1136 pixmap = WMCreatePixmap(scr, 130, 16, WMScreenDepth(scr), False);
1137 color = WMDarkGrayColor(scr);
1138 XFillRectangle(WMScreenDisplay(scr), WMGetPixmapXID(pixmap),
1139 WMColorGC(color), 0, 0, 130, 16);
1140 WMReleaseColor(color);
1141 color = WMWhiteColor(scr);
1142 WMDrawString(scr, WMGetPixmapXID(pixmap), WMColorGC(color),
1143 panel->listFont, 2,
1144 (16 - WMFontHeight(panel->listFont))/2 - 1,
1145 "Contrast", 8);
1146 WMSetSliderImage(panel->gconS, pixmap);
1147 WMReleasePixmap(pixmap);
1149 #endif
1150 panel->ghueS = WMCreateSlider(panel->gcolF);
1151 WMResizeWidget(panel->ghueS, 130, 16);
1152 WMMoveWidget(panel->ghueS, 150, 100);
1153 WMSetSliderKnobThickness(panel->ghueS, 8);
1154 WMSetSliderMaxValue(panel->ghueS, 359);
1155 WMSetSliderAction(panel->ghueS, sliderChangeCallback, panel);
1157 panel->gsatS = WMCreateSlider(panel->gcolF);
1158 WMResizeWidget(panel->gsatS, 130, 16);
1159 WMMoveWidget(panel->gsatS, 150, 125);
1160 WMSetSliderKnobThickness(panel->gsatS, 8);
1161 WMSetSliderMaxValue(panel->gsatS, 255);
1162 WMSetSliderAction(panel->gsatS, sliderChangeCallback, panel);
1164 panel->gvalS = WMCreateSlider(panel->gcolF);
1165 WMResizeWidget(panel->gvalS, 130, 16);
1166 WMMoveWidget(panel->gvalS, 150, 150);
1167 WMSetSliderKnobThickness(panel->gvalS, 8);
1168 WMSetSliderMaxValue(panel->gvalS, 255);
1169 WMSetSliderAction(panel->gvalS, sliderChangeCallback, panel);
1172 WMMapSubwidgets(panel->gcolF);
1174 /** Direction **/
1175 panel->dirF = WMCreateFrame(panel->win);
1176 WMSetFrameTitle(panel->dirF, _("Direction"));
1177 WMResizeWidget(panel->dirF, 295, 75);
1178 WMMoveWidget(panel->dirF, 15, 305);
1180 panel->dirvB = WMCreateButton(panel->dirF, WBTOnOff);
1181 WMSetButtonImagePosition(panel->dirvB, WIPImageOnly);
1182 WMResizeWidget(panel->dirvB, 90, 40);
1183 WMMoveWidget(panel->dirvB, 10, 20);
1185 panel->dirhB = WMCreateButton(panel->dirF, WBTOnOff);
1186 WMSetButtonImagePosition(panel->dirhB, WIPImageOnly);
1187 WMResizeWidget(panel->dirhB, 90, 40);
1188 WMMoveWidget(panel->dirhB, 102, 20);
1190 panel->dirdB = WMCreateButton(panel->dirF, WBTOnOff);
1191 WMSetButtonImagePosition(panel->dirdB, WIPImageOnly);
1192 WMResizeWidget(panel->dirdB, 90, 40);
1193 WMMoveWidget(panel->dirdB, 194, 20);
1195 WMGroupButtons(panel->dirvB, panel->dirhB);
1196 WMGroupButtons(panel->dirvB, panel->dirdB);
1198 WMMapSubwidgets(panel->dirF);
1200 /****************** Textured Gradient ******************/
1201 panel->tcolF = WMCreateFrame(panel->win);
1202 WMResizeWidget(panel->tcolF, 100, 135);
1203 WMMoveWidget(panel->tcolF, 210, 10);
1204 WMSetFrameTitle(panel->tcolF, _("Gradient"));
1206 panel->tcol1W = WMCreateColorWell(panel->tcolF);
1207 WMResizeWidget(panel->tcol1W, 60, 45);
1208 WMMoveWidget(panel->tcol1W, 20, 25);
1209 WMAddNotificationObserver(colorWellObserver, panel,
1210 WMColorWellDidChangeNotification, panel->tcol1W);
1212 panel->tcol2W = WMCreateColorWell(panel->tcolF);
1213 WMResizeWidget(panel->tcol2W, 60, 45);
1214 WMMoveWidget(panel->tcol2W, 20, 75);
1215 WMAddNotificationObserver(colorWellObserver, panel,
1216 WMColorWellDidChangeNotification, panel->tcol2W);
1218 /** Opacity */
1219 panel->topaF = WMCreateFrame(panel->win);
1220 WMResizeWidget(panel->topaF, 185, 50);
1221 WMMoveWidget(panel->topaF, 15, 95);
1222 WMSetFrameTitle(panel->topaF, _("Gradient Opacity"));
1224 panel->topaS = WMCreateSlider(panel->topaF);
1225 WMResizeWidget(panel->topaS, 155, 18);
1226 WMMoveWidget(panel->topaS, 15, 20);
1227 WMSetSliderMaxValue(panel->topaS, 255);
1228 WMSetSliderValue(panel->topaS, 200);
1229 WMMapSubwidgets(panel->topaF);
1232 WMPixmap *pixmap;
1233 Pixmap p;
1234 WMColor *color;
1236 pixmap = WMCreatePixmap(scr, 155, 18, WMScreenDepth(scr), False);
1237 p = WMGetPixmapXID(pixmap);
1239 color = WMDarkGrayColor(scr);
1240 XFillRectangle(WMScreenDisplay(scr), p, WMColorGC(color),
1241 0, 0, 155, 18);
1242 WMReleaseColor(color);
1244 color = WMWhiteColor(scr);
1245 WMDrawString(scr, p, WMColorGC(color), panel->listFont,
1246 2, 1, "0%", 2);
1247 WMDrawString(scr, p, WMColorGC(color), panel->listFont,
1248 153 - WMWidthOfString(panel->listFont, "100%", 4), 1,
1249 "100%", 4);
1250 WMReleaseColor(color);
1252 WMSetSliderImage(panel->topaS, pixmap);
1253 WMReleasePixmap(pixmap);
1256 WMMapSubwidgets(panel->tcolF);
1258 /****************** Image ******************/
1259 panel->imageF = WMCreateFrame(panel->win);
1260 WMResizeWidget(panel->imageF, 295, 150);
1261 WMMoveWidget(panel->imageF, 15, 150);
1262 WMSetFrameTitle(panel->imageF, _("Image"));
1264 panel->imageL = WMCreateLabel(panel->imageF);
1265 WMSetLabelImagePosition(panel->imageL, WIPImageOnly);
1267 panel->imageT = WMCreateTextField(panel->imageF);
1268 WMResizeWidget(panel->imageT, 90, 20);
1269 WMMoveWidget(panel->imageT, 190, 25);
1271 panel->imageV = WMCreateScrollView(panel->imageF);
1272 WMResizeWidget(panel->imageV, 165, 115);
1273 WMMoveWidget(panel->imageV, 15, 20);
1274 WMSetScrollViewRelief(panel->imageV, WRSunken);
1275 WMSetScrollViewHasHorizontalScroller(panel->imageV, True);
1276 WMSetScrollViewHasVerticalScroller(panel->imageV, True);
1277 WMSetScrollViewContentView(panel->imageV, WMWidgetView(panel->imageL));
1279 panel->browB = WMCreateCommandButton(panel->imageF);
1280 WMResizeWidget(panel->browB, 90, 24);
1281 WMMoveWidget(panel->browB, 190, 50);
1282 WMSetButtonText(panel->browB, _("Browse..."));
1283 WMSetButtonAction(panel->browB, browseImageCallback, panel);
1285 panel->dispB = WMCreateCommandButton(panel->imageF);
1286 WMResizeWidget(panel->dispB, 90, 24);
1287 WMMoveWidget(panel->dispB, 190, 80);
1288 WMSetButtonText(panel->dispB, _("Show"));
1290 panel->arrP = WMCreatePopUpButton(panel->imageF);
1291 WMResizeWidget(panel->arrP, 90, 20);
1292 WMMoveWidget(panel->arrP, 190, 120);
1293 WMAddPopUpButtonItem(panel->arrP, _("Tile"));
1294 WMAddPopUpButtonItem(panel->arrP, _("Scale"));
1295 WMAddPopUpButtonItem(panel->arrP, _("Center"));
1296 WMAddPopUpButtonItem(panel->arrP, _("Maximize"));
1297 WMSetPopUpButtonSelectedItem(panel->arrP, 0);
1299 WMMapSubwidgets(panel->imageF);
1301 /****/
1303 panel->okB = WMCreateCommandButton(panel->win);
1304 WMResizeWidget(panel->okB, 84, 24);
1305 WMMoveWidget(panel->okB, 225, 390);
1306 WMSetButtonText(panel->okB, _("OK"));
1307 WMSetButtonAction(panel->okB, buttonCallback, panel);
1309 panel->cancelB = WMCreateCommandButton(panel->win);
1310 WMResizeWidget(panel->cancelB, 84, 24);
1311 WMMoveWidget(panel->cancelB, 130, 390);
1312 WMSetButtonText(panel->cancelB, _("Cancel"));
1313 WMSetButtonAction(panel->cancelB, buttonCallback, panel);
1315 WMMapWidget(panel->nameF);
1316 WMMapWidget(panel->typeP);
1317 WMMapWidget(panel->okB);
1318 WMMapWidget(panel->cancelB);
1320 WMUnmapWidget(panel->arrP);
1322 WMRealizeWidget(panel->win);
1324 panel->currentType = -1;
1326 panel->sectionParts[TYPE_SOLID][0] = panel->defcF;
1328 panel->sectionParts[TYPE_GRADIENT][0] = panel->defcF;
1329 panel->sectionParts[TYPE_GRADIENT][1] = panel->gcolF;
1330 panel->sectionParts[TYPE_GRADIENT][2] = panel->dirF;
1332 panel->sectionParts[TYPE_SGRADIENT][0] = panel->tcolF;
1333 panel->sectionParts[TYPE_SGRADIENT][1] = panel->dirF;
1335 panel->sectionParts[TYPE_TGRADIENT][0] = panel->tcolF;
1336 panel->sectionParts[TYPE_TGRADIENT][1] = panel->dirF;
1337 panel->sectionParts[TYPE_TGRADIENT][2] = panel->imageF;
1338 panel->sectionParts[TYPE_TGRADIENT][3] = panel->topaF;
1339 panel->sectionParts[TYPE_TGRADIENT][4] = panel->arrP;
1341 panel->sectionParts[TYPE_PIXMAP][0] = panel->defcF;
1342 panel->sectionParts[TYPE_PIXMAP][1] = panel->imageF;
1343 panel->sectionParts[TYPE_PIXMAP][2] = panel->arrP;
1346 /* setup for first time */
1348 changeTypeCallback(panel->typeP, panel);
1350 sliderChangeCallback(panel->ghueS, panel);
1351 sliderChangeCallback(panel->gsatS, panel);
1353 return panel;
1359 *--------------------------------------------------------------------------
1360 * Test stuff
1361 *--------------------------------------------------------------------------
1364 #if 0
1366 char *ProgName = "test";
1368 void
1369 testOKButton(WMWidget *self, void *data)
1371 char *test;
1372 Display *dpy;
1373 Window win;
1374 Pixmap pix;
1375 RImage *image;
1377 TexturePanel *panel = (TexturePanel*)data;
1378 // test = GetTexturePanelTextureString(panel);
1380 wwarning(test);
1382 dpy = WMScreenDisplay(WMWidgetScreen(panel->okB));
1383 win = XCreateSimpleWindow(dpy, DefaultRootWindow(dpy), 10, 10, 250, 250,
1384 0, 0, 0);
1385 XMapRaised(dpy, win);
1386 XFlush(dpy);
1388 // image = RenderTexturePanelTexture(panel, 250, 250);
1390 RConvertImage(WMScreenRContext(WMWidgetScreen(panel->okB)), image, &pix);
1392 XCopyArea(dpy, pix, win, (WMScreenRContext(WMWidgetScreen(panel->okB)))->copy_gc, 0, 0, image->width, image->height,
1393 0, 0);
1395 free (test);
1399 void testCancelButton(WMWidget *self, void *data){
1400 wwarning("Exiting test....");
1401 exit(0);
1404 void wAbort()
1406 exit(1);
1409 int main(int argc, char **argv)
1411 TexturePanel *panel;
1413 Display *dpy = XOpenDisplay("");
1414 WMScreen *scr;
1416 /* char *test; */
1418 WMInitializeApplication("Test", &argc, argv);
1420 if (!dpy) {
1421 wfatal("could not open display");
1422 exit(1);
1425 scr = WMCreateSimpleApplicationScreen(dpy);
1427 panel = CreateTexturePanel(scr);
1429 SetTexturePanelOkAction(panel,(WMAction*)testOKButton,panel);
1430 SetTexturePanelCancelAction(panel,(WMAction*)testCancelButton,panel);
1432 SetTexturePanelTexture(panel, "pinky",
1433 PLGetProplistWithDescription("(mdgradient, pink, red, blue, yellow)"));
1435 ShowTexturePanel(panel);
1437 WMScreenMainLoop(scr);
1438 return 0;
1440 #endif