Update for 0.52.0. This is a test version, which brings the Appearance
[wmaker-crm.git] / WPrefs.app / TexturePanel.c
blob75493767229ac0e9ca92327c72112843c0206a8c
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"
41 typedef struct _TexturePanel TexturePanel;
43 #define MAX_SECTION_PARTS 4
45 typedef struct _TexturePanel {
46 WMWindow *win;
48 /* texture name */
49 WMFrame *nameF;
50 WMTextField *nameT;
52 /* texture type */
53 WMPopUpButton *typeP;
55 /* default color */
56 WMFrame *defcF;
57 WMColorWell *defcW;
59 WMFont *listFont;
61 /*-- Gradient --*/
63 Pixmap gimage;
65 /* colors */
66 WMFrame *gcolF;
67 WMList *gcolL;
68 WMButton *gcolaB;
69 WMButton *gcoldB;
70 WMSlider *ghueS;
71 WMSlider *gsatS;
72 WMSlider *gvalS;
74 WMSlider *gbriS;
75 WMSlider *gconS;
77 /* direction (common) */
78 WMFrame *dirF;
79 WMButton *dirhB;
80 WMButton *dirvB;
81 WMButton *dirdB;
83 /*-- Simple Gradient --*/
86 /*-- Textured Gradient --*/
88 WMFrame *tcolF;
89 WMColorWell *tcol1W;
90 WMColorWell *tcol2W;
92 WMFrame *topaF;
93 WMSlider *topaS;
95 /*-- Image --*/
96 WMFrame *imageF;
97 WMScrollView *imageV;
98 WMTextField *imageT;
99 WMLabel *imageL;
100 WMButton *browB;
101 WMButton *dispB;
102 WMPopUpButton *arrP;
104 char *imageFile;
106 /*****/
108 WMButton *okB;
109 WMButton *cancelB;
112 WMCallback *okAction;
113 void *okData;
115 WMCallback *cancelAction;
116 void *cancelData;
118 /****/
119 WMWidget *sectionParts[5][MAX_SECTION_PARTS];
121 int currentType;
124 proplist_t pathList;
126 } _TexturePanel;
130 #define TYPE_SOLID 0
131 #define TYPE_GRADIENT 1
132 #define TYPE_SGRADIENT 2
133 #define TYPE_TGRADIENT 3
134 #define TYPE_PIXMAP 4
137 #define PTYPE_TILE 0
138 #define PTYPE_SCALE 1
139 #define PTYPE_CENTER 2
140 #define PTYPE_MAXIMIZE 3
145 *--------------------------------------------------------------------------
146 * Private Functions
147 *--------------------------------------------------------------------------
150 /************/
152 static void
153 updateGradButtons(TexturePanel *panel)
155 RImage *image;
156 WMPixmap *pixmap;
157 int colorn;
158 RColor **colors;
160 colorn = WMGetListNumberOfRows(panel->gcolL);
161 if (colorn < 1) {
162 pixmap = NULL;
163 } else {
164 int i;
165 WMListItem *item;
167 colors = wmalloc(sizeof(RColor*)*(colorn+1));
169 for (i = 0; i < colorn; i++) {
170 item = WMGetListItem(panel->gcolL, i);
171 colors[i] = (RColor*)item->clientData;
173 colors[i] = NULL;
175 image = RRenderMultiGradient(80, 30, colors, RHorizontalGradient);
176 pixmap = WMCreatePixmapFromRImage(WMWidgetScreen(panel->gcolL),
177 image, 128);
178 RDestroyImage(image);
179 WMSetButtonImage(panel->dirhB, pixmap);
180 WMReleasePixmap(pixmap);
182 image = RRenderMultiGradient(80, 30, colors, RVerticalGradient);
183 pixmap = WMCreatePixmapFromRImage(WMWidgetScreen(panel->gcolL),
184 image, 128);
185 RDestroyImage(image);
186 WMSetButtonImage(panel->dirvB, pixmap);
187 WMReleasePixmap(pixmap);
189 image = RRenderMultiGradient(80, 30, colors, RDiagonalGradient);
190 pixmap = WMCreatePixmapFromRImage(WMWidgetScreen(panel->gcolL),
191 image, 128);
192 RDestroyImage(image);
193 WMSetButtonImage(panel->dirdB, pixmap);
194 WMReleasePixmap(pixmap);
196 free(colors);
201 static void
202 updateSGradButtons(TexturePanel *panel)
204 RImage *image;
205 WMPixmap *pixmap;
206 RColor from;
207 RColor to;
208 WMColor *color;
210 color = WMGetColorWellColor(panel->tcol1W);
211 from.red = WMRedComponentOfColor(color)>>8;
212 from.green = WMGreenComponentOfColor(color)>>8;
213 from.blue = WMBlueComponentOfColor(color)>>8;
215 color = WMGetColorWellColor(panel->tcol2W);
216 to.red = WMRedComponentOfColor(color)>>8;
217 to.green = WMGreenComponentOfColor(color)>>8;
218 to.blue = WMBlueComponentOfColor(color)>>8;
220 image = RRenderGradient(80, 30, &from, &to, RHorizontalGradient);
221 pixmap = WMCreatePixmapFromRImage(WMWidgetScreen(panel->gcolL),
222 image, 128);
223 RDestroyImage(image);
224 WMSetButtonImage(panel->dirhB, pixmap);
225 WMReleasePixmap(pixmap);
227 image = RRenderGradient(80, 30, &from, &to, RVerticalGradient);
228 pixmap = WMCreatePixmapFromRImage(WMWidgetScreen(panel->gcolL),
229 image, 128);
230 RDestroyImage(image);
231 WMSetButtonImage(panel->dirvB, pixmap);
232 WMReleasePixmap(pixmap);
234 image = RRenderGradient(80, 30, &from, &to, RDiagonalGradient);
235 pixmap = WMCreatePixmapFromRImage(WMWidgetScreen(panel->gcolL),
236 image, 128);
237 RDestroyImage(image);
238 WMSetButtonImage(panel->dirdB, pixmap);
239 WMReleasePixmap(pixmap);
243 static void
244 changeTypeCallback(WMWidget *w, void *data)
246 TexturePanel *panel = (TexturePanel*)data;
247 int newType;
248 int i;
250 newType = WMGetPopUpButtonSelectedItem(w);
251 if (newType == panel->currentType)
252 return;
254 if (panel->currentType >= 0) {
255 for (i = 0; i < MAX_SECTION_PARTS; i++) {
256 if (panel->sectionParts[panel->currentType][i] == NULL)
257 break;
258 WMUnmapWidget(panel->sectionParts[panel->currentType][i]);
262 for (i = 0; i < MAX_SECTION_PARTS; i++) {
263 if (panel->sectionParts[newType][i] == NULL)
264 break;
265 WMMapWidget(panel->sectionParts[newType][i]);
267 panel->currentType = newType;
269 switch (newType) {
270 case TYPE_SGRADIENT:
271 updateSGradButtons(panel);
272 break;
273 case TYPE_GRADIENT:
274 updateGradButtons(panel);
275 break;
280 /*********** Gradient ************/
282 static void
283 updateSVSlider(WMSlider *sPtr, Bool saturation, WMFont *font, RHSVColor *hsv)
285 RImage *image;
286 WMPixmap *pixmap;
287 WMScreen *scr = WMWidgetScreen(sPtr);
288 RColor from, to;
289 RHSVColor tmp;
291 tmp = *hsv;
292 if (saturation) {
293 tmp.saturation = 0;
294 RHSVtoRGB(&tmp, &from);
295 tmp.saturation = 255;
296 RHSVtoRGB(&tmp, &to);
297 } else {
298 tmp.value = 0;
299 RHSVtoRGB(&tmp, &from);
300 tmp.value = 255;
301 RHSVtoRGB(&tmp, &to);
303 image = RRenderGradient(130, 16, &from, &to, RHorizontalGradient);
304 pixmap = WMCreatePixmapFromRImage(scr, image, 128);
305 RDestroyImage(image);
307 if (hsv->value < 128 || !saturation) {
308 WMColor *col = WMWhiteColor(scr);
310 WMDrawString(scr, WMGetPixmapXID(pixmap), WMColorGC(col), font, 2,
311 (16 - WMFontHeight(font))/2 - 1,
312 saturation ? "Saturation" : "Brightness", 10);
313 WMReleaseColor(col);
314 } else {
315 WMColor *col = WMBlackColor(scr);
317 WMDrawString(scr, WMGetPixmapXID(pixmap), WMColorGC(col), font, 2,
318 (16 - WMFontHeight(font))/2 - 1,
319 saturation ? "Saturation" : "Brightness", 10);
320 WMReleaseColor(col);
322 WMSetSliderImage(sPtr, pixmap);
323 WMReleasePixmap(pixmap);
327 static void
328 updateHueSlider(WMSlider *sPtr, WMFont *font, RHSVColor *hsv)
330 RColor *colors[8];
331 RImage *image;
332 WMPixmap *pixmap;
333 WMScreen *scr = WMWidgetScreen(sPtr);
334 RHSVColor thsv;
335 int i;
337 thsv = *hsv;
338 for (i = 0; i <= 6; i++) {
339 thsv.hue = (360*i)/6;
340 colors[i] = wmalloc(sizeof(RColor));
341 RHSVtoRGB(&thsv, colors[i]);
343 colors[i] = NULL;
345 image = RRenderMultiGradient(130, 16, colors, RGRD_HORIZONTAL);
346 pixmap = WMCreatePixmapFromRImage(scr, image, 128);
347 RDestroyImage(image);
349 if (hsv->value < 128) {
350 WMColor *col = WMWhiteColor(scr);
352 WMDrawString(scr, WMGetPixmapXID(pixmap), WMColorGC(col), font, 2,
353 (16 - WMFontHeight(font))/2 - 1, "Hue", 3);
354 WMReleaseColor(col);
355 } else {
356 WMColor *col = WMBlackColor(scr);
358 WMDrawString(scr, WMGetPixmapXID(pixmap), WMColorGC(col), font, 2,
359 (16 - WMFontHeight(font))/2 - 1, "Hue", 3);
360 WMReleaseColor(col);
362 WMSetSliderImage(sPtr, pixmap);
363 WMReleasePixmap(pixmap);
365 for (i = 0; i <= 6; i++)
366 free(colors[i]);
371 static void
372 sliderChangeCallback(WMWidget *w, void *data)
374 TexturePanel *panel = (TexturePanel*)data;
375 RHSVColor hsv, *hsvp;
376 int row, rows;
377 WMListItem *item;
378 RColor **colors;
379 int i;
380 RImage *image;
381 WMScreen *scr = WMWidgetScreen(w);
383 hsv.hue = WMGetSliderValue(panel->ghueS);
384 hsv.saturation = WMGetSliderValue(panel->gsatS);
385 hsv.value = WMGetSliderValue(panel->gvalS);
387 row = WMGetListSelectedItemRow(panel->gcolL);
388 if (row >= 0) {
389 RColor *rgb;
391 item = WMGetListItem(panel->gcolL, row);
393 rgb = (RColor*)item->clientData;
395 RHSVtoRGB(&hsv, rgb);
397 sprintf(item->text, "%02x,%02x,%02x", rgb->red, rgb->green, rgb->blue);
400 if (w == panel->ghueS) {
401 updateSVSlider(panel->gsatS, True, panel->listFont, &hsv);
402 updateSVSlider(panel->gvalS, False, panel->listFont, &hsv);
403 } else if (w == panel->gsatS) {
404 updateHueSlider(panel->ghueS, panel->listFont, &hsv);
405 updateSVSlider(panel->gvalS, False, panel->listFont, &hsv);
406 } else {
407 updateHueSlider(panel->ghueS, panel->listFont, &hsv);
408 updateSVSlider(panel->gsatS, True, panel->listFont, &hsv);
411 rows = WMGetListNumberOfRows(panel->gcolL);
412 if (rows == 0)
413 return;
415 colors = wmalloc(sizeof(RColor*)*(rows+1));
417 for (i = 0; i < rows; i++) {
418 item = WMGetListItem(panel->gcolL, i);
420 colors[i] = (RColor*)item->clientData;
422 colors[i] = NULL;
424 if (panel->gimage != None) {
425 XFreePixmap(WMScreenDisplay(scr), panel->gimage);
428 image = RRenderMultiGradient(30, i*WMGetListItemHeight(panel->gcolL),
429 colors, RVerticalGradient);
430 RConvertImage(WMScreenRContext(scr), image, &panel->gimage);
431 RDestroyImage(image);
433 free(colors);
435 WMRedisplayWidget(panel->gcolL);
437 updateGradButtons(panel);
441 static void
442 paintGradListItem(WMList *lPtr, int index, Drawable d, char *text, int state,
443 WMRect *rect)
445 TexturePanel *panel = (TexturePanel*)WMGetHangedData(lPtr);
446 WMScreen *scr = WMWidgetScreen(lPtr);
447 int width, height, x, y;
448 Display *dpy;
449 WMColor *white = WMWhiteColor(scr);
450 WMListItem *item;
451 WMColor *black = WMBlackColor(scr);
453 dpy = WMScreenDisplay(scr);
455 width = rect->size.width;
456 height = rect->size.height;
457 x = rect->pos.x;
458 y = rect->pos.y;
460 if (state & WLDSSelected)
461 XFillRectangle(dpy, d, WMColorGC(white), x, y, width, height);
462 else
463 XClearArea(dpy, d, x, y, width, height, False);
465 item = WMGetListItem(lPtr, index);
467 if (panel->gimage) {
468 XCopyArea(WMScreenDisplay(scr), panel->gimage, d, WMColorGC(white),
469 0, height*index, 30, height, x + 5, y);
471 WMDrawString(scr, d, WMColorGC(black), panel->listFont,
472 x + 40, y + 1, text, strlen(text));
474 WMReleaseColor(white);
475 WMReleaseColor(black);
480 static void
481 gradAddCallback(WMWidget *w, void *data)
483 TexturePanel *panel = (TexturePanel*)data;
484 WMListItem *item;
485 int row;
486 RColor *rgb;
488 row = WMGetListSelectedItemRow(panel->gcolL) + 1;
489 item = WMInsertListItem(panel->gcolL, row, "00,00,00");
490 rgb = wmalloc(sizeof(RColor));
491 memset(rgb, 0, sizeof(RColor));
492 item->clientData = rgb;
494 WMSelectListItem(panel->gcolL, row);
496 updateGradButtons(panel);
498 sliderChangeCallback(panel->ghueS, panel);
503 static void
504 gradClickCallback(WMWidget *w, void *data)
506 TexturePanel *panel = (TexturePanel*)data;
507 WMListItem *item;
508 int row;
509 RHSVColor hsv;
511 row = WMGetListSelectedItemRow(w);
512 if (row < 0)
513 return;
515 item = WMGetListItem(panel->gcolL, row);
516 RRGBtoHSV((RColor*)item->clientData, &hsv);
518 WMSetSliderValue(panel->ghueS, hsv.hue);
519 WMSetSliderValue(panel->gsatS, hsv.saturation);
520 WMSetSliderValue(panel->gvalS, hsv.value);
522 sliderChangeCallback(panel->ghueS, panel);
523 sliderChangeCallback(panel->gsatS, panel);
527 static void
528 gradDeleteCallback(WMWidget *w, void *data)
530 TexturePanel *panel = (TexturePanel*)data;
531 WMListItem *item;
532 int row;
534 row = WMGetListSelectedItemRow(panel->gcolL);
535 if (row < 0)
536 return;
538 item = WMGetListItem(panel->gcolL, row);
539 free(item->clientData);
541 WMRemoveListItem(panel->gcolL, row);
543 WMSelectListItem(panel->gcolL, row - 1);
545 updateGradButtons(panel);
547 gradClickCallback(panel->gcolL, panel);
551 /*************** Simple Gradient ***************/
553 static void
554 colorWellObserver(void *self, WMNotification *n)
556 updateSGradButtons(self);
560 /****************** Image ******************/
562 static void
563 browseImageCallback(WMWidget *w, void *data)
565 TexturePanel *panel = (TexturePanel*)data;
566 WMOpenPanel *opanel;
567 WMScreen *scr = WMWidgetScreen(w);
569 opanel = WMGetOpenPanel(scr);
570 WMSetFilePanelCanChooseDirectories(opanel, False);
571 WMSetFilePanelCanChooseFiles(opanel, True);
573 if (WMRunModalFilePanelForDirectory(opanel, panel->win, "/home",
574 "Open Image", NULL)) {
575 char *path, *fullpath;
576 char *tmp, *tmp2;
578 fullpath = WMGetFilePanelFileName(opanel);
579 if (!fullpath)
580 return;
581 path = wstrdup(fullpath);
583 tmp2 = strrchr(fullpath, '/');
584 if (tmp2)
585 tmp2++;
587 tmp = wfindfileinarray(panel->pathList, tmp2);
589 if (tmp) {
590 if (strcmp(fullpath, tmp)==0) {
591 free(path);
592 path = tmp2;
594 free(tmp);
597 if (!RGetImageFileFormat(fullpath)) {
598 WMRunAlertPanel(scr, panel->win, _("Error"),
599 _("The selected file does not contain a supported image."),
600 _("OK"), NULL, NULL);
601 free(path);
602 free(fullpath);
603 } else {
604 RImage *image, *scaled;
605 WMPixmap *pixmap;
606 WMSize size;
608 image = RLoadImage(WMScreenRContext(scr), fullpath, 0);
609 if (!image) {
610 char *message;
612 message = wstrappend(_("Could not load the selected file: "),
613 (char*)RMessageForError(RErrorCode));
615 WMRunAlertPanel(scr, panel->win, _("Error"), message,
616 _("OK"), NULL, NULL);
617 free(message);
618 free(path);
619 free(fullpath);
620 return;
623 pixmap = WMCreatePixmapFromRImage(scr, image, 128);
624 RDestroyImage(image);
626 size = WMGetPixmapSize(pixmap);
627 WMSetLabelImage(panel->imageL, pixmap);
628 WMResizeWidget(panel->imageL, size.width, size.height);
630 WMReleasePixmap(pixmap);
632 panel->imageFile = path;
634 WMSetTextFieldText(panel->imageT, path);
636 free(fullpath);
643 static void
644 buttonCallback(WMWidget *w, void *data)
646 TexturePanel *panel = (TexturePanel*)data;
648 if (w == panel->okB) {
649 (*panel->okAction)(panel->okData);
650 } else {
651 (*panel->cancelAction)(panel->cancelData);
657 *--------------------------------------------------------------------------
658 * Public functions
659 *--------------------------------------------------------------------------
661 void
662 DestroyTexturePanel(TexturePanel *panel)
668 void
669 ShowTexturePanel(TexturePanel *panel)
671 WMMapWidget(panel->win);
675 void
676 HideTexturePanel(TexturePanel *panel)
678 WMUnmapWidget(panel->win);
682 void
683 SetTexturePanelOkAction(TexturePanel *panel, WMCallback *action, void *clientData)
685 panel->okAction = action;
686 panel->okData = clientData;
690 void
691 SetTexturePanelCancelAction(TexturePanel *panel, WMCallback *action, void *clientData)
693 panel->cancelAction = action;
694 panel->cancelData = clientData;
698 void
699 SetTexturePanelTexture(TexturePanel *panel, char *name, proplist_t texture)
701 WMScreen *scr = WMWidgetScreen(panel->win);
702 char *str, *type;
703 proplist_t p;
704 WMColor *color;
705 int i;
706 char buffer[64];
707 int gradient = 0;
709 WMSetTextFieldText(panel->nameT, name);
711 if (!texture)
712 return;
714 p = PLGetArrayElement(texture, 0);
715 if (!p) {
716 goto bad_texture;
718 type = PLGetString(p);
720 /*...............................................*/
721 if (strcasecmp(type, "solid")==0) {
723 WMSetPopUpButtonSelectedItem(panel->typeP, TYPE_SOLID);
725 p = PLGetArrayElement(texture, 1);
726 if (!p) {
727 goto bad_texture;
730 str = PLGetString(p);
731 color = WMCreateNamedColor(scr, str, False);
733 WMSetColorWellColor(panel->defcW, color);
735 WMReleaseColor(color);
736 /*...............................................*/
737 } else if (strcasecmp(type, "hgradient")==0
738 || strcasecmp(type, "vgradient")==0
739 || strcasecmp(type, "dgradient")==0) {
741 WMSetPopUpButtonSelectedItem(panel->typeP, TYPE_SGRADIENT);
743 p = PLGetArrayElement(texture, 1);
744 if (!p) {
745 goto bad_texture;
747 str = PLGetString(p);
748 color = WMCreateNamedColor(scr, str, False);
750 WMSetColorWellColor(panel->tcol1W, color);
752 WMReleaseColor(color);
754 p = PLGetArrayElement(texture, 2);
755 if (!p) {
756 goto bad_texture;
758 str = PLGetString(p);
759 color = WMCreateNamedColor(scr, str, False);
761 WMSetColorWellColor(panel->tcol2W, color);
763 WMReleaseColor(color);
765 gradient = type[0];
766 /*...............................................*/
767 } else if (strcasecmp(type, "thgradient")==0
768 || strcasecmp(type, "tvgradient")==0
769 || strcasecmp(type, "tdgradient")==0) {
771 WMSetPopUpButtonSelectedItem(panel->typeP, TYPE_TGRADIENT);
773 /****** TODO: setador de textura apartir desse */
774 gradient = type[1];
775 /*...............................................*/
776 } else if (strcasecmp(type, "mhgradient")==0
777 || strcasecmp(type, "mvgradient")==0
778 || strcasecmp(type, "mdgradient")==0) {
779 WMListItem *item;
781 for (i = 0; i < WMGetListNumberOfRows(panel->gcolL); i++) {
782 item = WMGetListItem(panel->gcolL, i);
783 free(item->clientData);
785 WMClearList(panel->gcolL);
787 WMSetPopUpButtonSelectedItem(panel->typeP, TYPE_GRADIENT);
789 p = PLGetArrayElement(texture, 1);
790 if (!p) {
791 goto bad_texture;
794 str = PLGetString(p);
795 color = WMCreateNamedColor(scr, str, False);
797 WMSetColorWellColor(panel->defcW, color);
799 WMReleaseColor(color);
801 for (i = 2; i < PLGetNumberOfElements(texture); i++) {
802 RColor *rgb;
803 XColor xcolor;
805 p = PLGetArrayElement(texture, i);
806 if (!p) {
807 goto bad_texture;
809 str = PLGetString(p);
811 XParseColor(WMScreenDisplay(scr), WMScreenRContext(scr)->cmap,
812 str, &xcolor);
814 rgb = wmalloc(sizeof(RColor));
815 rgb->red = xcolor.red >> 8;
816 rgb->green = xcolor.green >> 8;
817 rgb->blue = xcolor.blue >> 8;
818 sprintf(buffer, "%02x,%02x,%02x", rgb->red, rgb->green, rgb->blue);
820 item = WMAddListItem(panel->gcolL, buffer);
821 item->clientData = rgb;
824 sliderChangeCallback(panel->ghueS, panel);
826 gradient = type[1];
827 /*...............................................*/
828 } else if (strcasecmp(type, "cpixmap")==0
829 || strcasecmp(type, "spixmap")==0
830 || strcasecmp(type, "mpixmap")==0
831 || strcasecmp(type, "tpixmap")==0) {
833 WMSetPopUpButtonSelectedItem(panel->typeP, TYPE_PIXMAP);
835 switch (toupper(type[0])) {
836 case 'C':
837 WMSetPopUpButtonSelectedItem(panel->arrP, PTYPE_CENTER);
838 break;
839 case 'S':
840 WMSetPopUpButtonSelectedItem(panel->arrP, PTYPE_SCALE);
841 break;
842 case 'M':
843 WMSetPopUpButtonSelectedItem(panel->arrP, PTYPE_MAXIMIZE);
844 break;
845 default:
846 case 'T':
847 WMSetPopUpButtonSelectedItem(panel->arrP, PTYPE_TILE);
848 break;
851 WMSetTextFieldText(panel->imageT,
852 PLGetString(PLGetArrayElement(texture, 1)));
855 changeTypeCallback(panel->typeP, panel);
857 if (gradient > 0) {
858 updateGradButtons(panel);
860 switch (toupper(gradient)) {
861 case 'H':
862 WMPerformButtonClick(panel->dirhB);
863 break;
864 case 'V':
865 WMPerformButtonClick(panel->dirvB);
866 break;
867 default:
868 case 'D':
869 WMPerformButtonClick(panel->dirdB);
870 break;
874 return;
876 bad_texture:
877 str = PLGetDescription(texture);
878 wwarning("error creating texture %s", str);
879 free(str);
885 char*
886 GetTexturePanelTextureName(TexturePanel *panel)
888 return WMGetTextFieldText(panel->nameT);
894 proplist_t
895 GetTexturePanelTexture(TexturePanel *panel)
897 proplist_t prop = NULL;
898 WMColor *color;
899 char *str, *str2;
900 char buff[32];
901 int i;
904 switch (WMGetPopUpButtonSelectedItem(panel->typeP)) {
906 case TYPE_SOLID:
907 color = WMGetColorWellColor(panel->defcW);
908 str = WMGetColorRGBDescription(color);
909 prop = PLMakeArrayFromElements(PLMakeString("solid"),
910 PLMakeString(str), NULL);
911 free(str);
913 break;
915 case TYPE_PIXMAP:
916 color = WMGetColorWellColor(panel->defcW);
917 str = WMGetColorRGBDescription(color);
919 switch (WMGetPopUpButtonSelectedItem(panel->arrP)) {
920 case PTYPE_SCALE:
921 prop = PLMakeArrayFromElements(PLMakeString("spixmap"),
922 PLMakeString(panel->imageFile),
923 PLMakeString(str), NULL);
924 break;
925 case PTYPE_MAXIMIZE:
926 prop = PLMakeArrayFromElements(PLMakeString("mpixmap"),
927 PLMakeString(panel->imageFile),
928 PLMakeString(str), NULL);
929 break;
930 case PTYPE_CENTER:
931 prop = PLMakeArrayFromElements(PLMakeString("cpixmap"),
932 PLMakeString(panel->imageFile),
933 PLMakeString(str), NULL);
934 break;
935 case PTYPE_TILE:
936 prop = PLMakeArrayFromElements(PLMakeString("tpixmap"),
937 PLMakeString(panel->imageFile),
938 PLMakeString(str), NULL);
939 break;
941 free(str);
942 break;
944 case TYPE_SGRADIENT:
945 color = WMGetColorWellColor(panel->tcol1W);
946 str = WMGetColorRGBDescription(color);
948 color = WMGetColorWellColor(panel->tcol2W);
949 str2 = WMGetColorRGBDescription(color);
951 if (WMGetButtonSelected(panel->dirdB)) {
952 prop = PLMakeArrayFromElements(PLMakeString("dgradient"),
953 PLMakeString(str),
954 PLMakeString(str2), NULL);
955 } else if (WMGetButtonSelected(panel->dirvB)) {
956 prop = PLMakeArrayFromElements(PLMakeString("vgradient"),
957 PLMakeString(str),
958 PLMakeString(str2), NULL);
959 } else {
960 prop = PLMakeArrayFromElements(PLMakeString("hgradient"),
961 PLMakeString(str),
962 PLMakeString(str2), NULL);
964 free(str);
965 free(str2);
966 break;
968 case TYPE_GRADIENT:
969 color = WMGetColorWellColor(panel->defcW);
970 str = WMGetColorRGBDescription(color);
972 if (WMGetButtonSelected(panel->dirdB)) {
973 prop = PLMakeArrayFromElements(PLMakeString("mdgradient"),
974 PLMakeString(str), NULL);
975 } else if (WMGetButtonSelected(panel->dirvB)) {
976 prop = PLMakeArrayFromElements(PLMakeString("mvgradient"),
977 PLMakeString(str), NULL);
978 } else {
979 prop = PLMakeArrayFromElements(PLMakeString("mhgradient"),
980 PLMakeString(str), NULL);
982 free(str);
984 for (i = 0; i < WMGetListNumberOfRows(panel->gcolL); i++) {
985 RColor *rgb;
986 WMListItem *item;
988 item = WMGetListItem(panel->gcolL, i);
990 rgb = (RColor*)item->clientData;
992 sprintf(buff, "#%02x%02x%02x", rgb->red, rgb->green, rgb->blue);
994 PLAppendArrayElement(prop, PLMakeString(buff));
996 break;
1000 return prop;
1005 void
1006 SetTexturePanelPixmapPath(TexturePanel *panel, proplist_t array)
1008 panel->pathList = array;
1013 TexturePanel*
1014 CreateTexturePanel(WMWindow *keyWindow)
1015 //CreateTexturePanel(WMScreen *scr)
1017 TexturePanel *panel;
1018 WMScreen *scr = WMWidgetScreen(keyWindow);
1020 panel = wmalloc(sizeof(TexturePanel));
1021 memset(panel, 0, sizeof(TexturePanel));
1023 panel->listFont = WMSystemFontOfSize(scr, 12);
1026 panel->win = WMCreatePanelWithStyleForWindow(keyWindow, "texturePanel",
1027 WMTitledWindowMask
1028 |WMClosableWindowMask);
1030 panel->win = WMCreateWindowWithStyle(scr, "texturePanel",
1031 WMTitledWindowMask
1032 |WMClosableWindowMask);
1035 WMResizeWidget(panel->win, 325, 423);
1036 WMSetWindowTitle(panel->win, _("Texture Panel"));
1039 /* texture name */
1040 panel->nameF = WMCreateFrame(panel->win);
1041 WMResizeWidget(panel->nameF, 185, 50);
1042 WMMoveWidget(panel->nameF, 15, 10);
1043 WMSetFrameTitle(panel->nameF, _("Texture Name"));
1045 panel->nameT = WMCreateTextField(panel->nameF);
1046 WMResizeWidget(panel->nameT, 160, 20);
1047 WMMoveWidget(panel->nameT, 12, 18);
1049 WMMapSubwidgets(panel->nameF);
1051 /* texture types */
1052 panel->typeP = WMCreatePopUpButton(panel->win);
1053 WMResizeWidget(panel->typeP, 185, 20);
1054 WMMoveWidget(panel->typeP, 15, 65);
1055 WMAddPopUpButtonItem(panel->typeP, _("Solid Color"));
1056 WMAddPopUpButtonItem(panel->typeP, _("Gradient Texture"));
1057 WMAddPopUpButtonItem(panel->typeP, _("Simple Gradient Texture"));
1058 WMAddPopUpButtonItem(panel->typeP, _("Textured Gradient"));
1059 WMAddPopUpButtonItem(panel->typeP, _("Image Texture"));
1060 WMSetPopUpButtonSelectedItem(panel->typeP, 0);
1061 WMSetPopUpButtonAction(panel->typeP, changeTypeCallback, panel);
1063 WMSetPopUpButtonItemEnabled(panel->typeP, TYPE_TGRADIENT, False);
1065 /* color */
1066 panel->defcF = WMCreateFrame(panel->win);
1067 WMResizeWidget(panel->defcF, 100, 75);
1068 WMMoveWidget(panel->defcF, 210, 10);
1069 WMSetFrameTitle(panel->defcF, _("Default Color"));
1071 panel->defcW = WMCreateColorWell(panel->defcF);
1072 WMResizeWidget(panel->defcW, 60, 45);
1073 WMMoveWidget(panel->defcW, 20, 20);
1075 WMMapSubwidgets(panel->defcF);
1077 /****** Gradient ******/
1078 panel->gcolF = WMCreateFrame(panel->win);
1079 WMResizeWidget(panel->gcolF, 295, 205);
1080 WMMoveWidget(panel->gcolF, 15, 95);
1081 WMSetFrameTitle(panel->gcolF, _("Gradient Colors"));
1083 panel->gcolL = WMCreateList(panel->gcolF);
1084 WMResizeWidget(panel->gcolL, 130, 140);
1085 WMMoveWidget(panel->gcolL, 10, 25);
1086 WMHangData(panel->gcolL, panel);
1087 WMSetListUserDrawProc(panel->gcolL, paintGradListItem);
1088 WMSetListAction(panel->gcolL, gradClickCallback, panel);
1090 panel->gcolaB = WMCreateCommandButton(panel->gcolF);
1091 WMResizeWidget(panel->gcolaB, 64, 24);
1092 WMMoveWidget(panel->gcolaB, 10, 170);
1093 WMSetButtonText(panel->gcolaB, _("Add"));
1094 WMSetButtonAction(panel->gcolaB, gradAddCallback, panel);
1096 panel->gcoldB = WMCreateCommandButton(panel->gcolF);
1097 WMResizeWidget(panel->gcoldB, 64, 24);
1098 WMMoveWidget(panel->gcoldB, 75, 170);
1099 WMSetButtonText(panel->gcoldB, _("Delete"));
1100 WMSetButtonAction(panel->gcoldB, gradDeleteCallback, panel);
1102 #if 0
1103 panel->gbriS = WMCreateSlider(panel->gcolF);
1104 WMResizeWidget(panel->gbriS, 130, 16);
1105 WMMoveWidget(panel->gbriS, 150, 25);
1106 WMSetSliderKnobThickness(panel->gbriS, 8);
1107 WMSetSliderMaxValue(panel->gbriS, 100);
1108 WMSetSliderAction(panel->gbriS, sliderChangeCallback, panel);
1110 WMPixmap *pixmap;
1111 WMColor *color;
1113 pixmap = WMCreatePixmap(scr, 130, 16, WMScreenDepth(scr), False);
1114 color = WMDarkGrayColor(scr);
1115 XFillRectangle(WMScreenDisplay(scr), WMGetPixmapXID(pixmap),
1116 WMColorGC(color), 0, 0, 130, 16);
1117 WMReleaseColor(color);
1118 color = WMWhiteColor(color);
1119 WMDrawString(scr, WMGetPixmapXID(pixmap), WMColorGC(color),
1120 panel->listFont, 2,
1121 (16 - WMFontHeight(panel->listFont))/2 - 1,
1122 "Brightness", 10);
1123 WMSetSliderImage(panel->gbriS, pixmap);
1124 WMReleasePixmap(pixmap);
1127 panel->gconS = WMCreateSlider(panel->gcolF);
1128 WMResizeWidget(panel->gconS, 130, 16);
1129 WMMoveWidget(panel->gconS, 150, 50);
1130 WMSetSliderKnobThickness(panel->gconS, 8);
1131 WMSetSliderMaxValue(panel->gconS, 100);
1132 WMSetSliderAction(panel->gconS, sliderChangeCallback, panel);
1134 WMPixmap *pixmap;
1135 WMColor *color;
1137 pixmap = WMCreatePixmap(scr, 130, 16, WMScreenDepth(scr), False);
1138 color = WMDarkGrayColor(scr);
1139 XFillRectangle(WMScreenDisplay(scr), WMGetPixmapXID(pixmap),
1140 WMColorGC(color), 0, 0, 130, 16);
1141 WMReleaseColor(color);
1142 color = WMWhiteColor(scr);
1143 WMDrawString(scr, WMGetPixmapXID(pixmap), WMColorGC(color),
1144 panel->listFont, 2,
1145 (16 - WMFontHeight(panel->listFont))/2 - 1,
1146 "Contrast", 8);
1147 WMSetSliderImage(panel->gconS, pixmap);
1148 WMReleasePixmap(pixmap);
1150 #endif
1151 panel->ghueS = WMCreateSlider(panel->gcolF);
1152 WMResizeWidget(panel->ghueS, 130, 16);
1153 WMMoveWidget(panel->ghueS, 150, 100);
1154 WMSetSliderKnobThickness(panel->ghueS, 8);
1155 WMSetSliderMaxValue(panel->ghueS, 359);
1156 WMSetSliderAction(panel->ghueS, sliderChangeCallback, panel);
1158 panel->gsatS = WMCreateSlider(panel->gcolF);
1159 WMResizeWidget(panel->gsatS, 130, 16);
1160 WMMoveWidget(panel->gsatS, 150, 125);
1161 WMSetSliderKnobThickness(panel->gsatS, 8);
1162 WMSetSliderMaxValue(panel->gsatS, 255);
1163 WMSetSliderAction(panel->gsatS, sliderChangeCallback, panel);
1165 panel->gvalS = WMCreateSlider(panel->gcolF);
1166 WMResizeWidget(panel->gvalS, 130, 16);
1167 WMMoveWidget(panel->gvalS, 150, 150);
1168 WMSetSliderKnobThickness(panel->gvalS, 8);
1169 WMSetSliderMaxValue(panel->gvalS, 255);
1170 WMSetSliderAction(panel->gvalS, sliderChangeCallback, panel);
1173 WMMapSubwidgets(panel->gcolF);
1175 /** Direction **/
1176 panel->dirF = WMCreateFrame(panel->win);
1177 WMSetFrameTitle(panel->dirF, _("Direction"));
1178 WMResizeWidget(panel->dirF, 295, 75);
1179 WMMoveWidget(panel->dirF, 15, 305);
1181 panel->dirvB = WMCreateButton(panel->dirF, WBTOnOff);
1182 WMSetButtonImagePosition(panel->dirvB, WIPImageOnly);
1183 WMResizeWidget(panel->dirvB, 90, 40);
1184 WMMoveWidget(panel->dirvB, 10, 20);
1186 panel->dirhB = WMCreateButton(panel->dirF, WBTOnOff);
1187 WMSetButtonImagePosition(panel->dirhB, WIPImageOnly);
1188 WMResizeWidget(panel->dirhB, 90, 40);
1189 WMMoveWidget(panel->dirhB, 102, 20);
1191 panel->dirdB = WMCreateButton(panel->dirF, WBTOnOff);
1192 WMSetButtonImagePosition(panel->dirdB, WIPImageOnly);
1193 WMResizeWidget(panel->dirdB, 90, 40);
1194 WMMoveWidget(panel->dirdB, 194, 20);
1196 WMGroupButtons(panel->dirvB, panel->dirhB);
1197 WMGroupButtons(panel->dirvB, panel->dirdB);
1199 WMMapSubwidgets(panel->dirF);
1201 /****************** Textured Gradient ******************/
1202 panel->tcolF = WMCreateFrame(panel->win);
1203 WMResizeWidget(panel->tcolF, 100, 135);
1204 WMMoveWidget(panel->tcolF, 210, 10);
1205 WMSetFrameTitle(panel->tcolF, _("Gradient"));
1207 panel->tcol1W = WMCreateColorWell(panel->tcolF);
1208 WMResizeWidget(panel->tcol1W, 60, 45);
1209 WMMoveWidget(panel->tcol1W, 20, 25);
1210 WMAddNotificationObserver(colorWellObserver, panel,
1211 WMColorWellDidChangeNotification, panel->tcol1W);
1213 panel->tcol2W = WMCreateColorWell(panel->tcolF);
1214 WMResizeWidget(panel->tcol2W, 60, 45);
1215 WMMoveWidget(panel->tcol2W, 20, 75);
1216 WMAddNotificationObserver(colorWellObserver, panel,
1217 WMColorWellDidChangeNotification, panel->tcol2W);
1219 /** Opacity */
1220 panel->topaF = WMCreateFrame(panel->win);
1221 WMResizeWidget(panel->topaF, 185, 50);
1222 WMMoveWidget(panel->topaF, 15, 95);
1223 WMSetFrameTitle(panel->topaF, _("Gradient Opacity"));
1225 panel->topaS = WMCreateSlider(panel->topaF);
1226 WMResizeWidget(panel->topaS, 155, 18);
1227 WMMoveWidget(panel->topaS, 15, 20);
1228 WMSetSliderMaxValue(panel->topaS, 255);
1229 WMSetSliderValue(panel->topaS, 200);
1230 WMMapSubwidgets(panel->topaF);
1233 WMPixmap *pixmap;
1234 Pixmap p;
1235 WMColor *color;
1237 pixmap = WMCreatePixmap(scr, 155, 18, WMScreenDepth(scr), False);
1238 p = WMGetPixmapXID(pixmap);
1240 color = WMDarkGrayColor(scr);
1241 XFillRectangle(WMScreenDisplay(scr), p, WMColorGC(color),
1242 0, 0, 155, 18);
1243 WMReleaseColor(color);
1245 color = WMWhiteColor(scr);
1246 WMDrawString(scr, p, WMColorGC(color), panel->listFont,
1247 2, 1, "0%", 2);
1248 WMDrawString(scr, p, WMColorGC(color), panel->listFont,
1249 153 - WMWidthOfString(panel->listFont, "100%", 4), 1,
1250 "100%", 4);
1251 WMReleaseColor(color);
1253 WMSetSliderImage(panel->topaS, pixmap);
1254 WMReleasePixmap(pixmap);
1257 WMMapSubwidgets(panel->tcolF);
1259 /****************** Image ******************/
1260 panel->imageF = WMCreateFrame(panel->win);
1261 WMResizeWidget(panel->imageF, 295, 150);
1262 WMMoveWidget(panel->imageF, 15, 150);
1263 WMSetFrameTitle(panel->imageF, _("Image"));
1265 panel->imageL = WMCreateLabel(panel->imageF);
1266 WMSetLabelImagePosition(panel->imageL, WIPImageOnly);
1268 panel->imageT = WMCreateTextField(panel->imageF);
1269 WMResizeWidget(panel->imageT, 90, 20);
1270 WMMoveWidget(panel->imageT, 190, 25);
1272 panel->imageV = WMCreateScrollView(panel->imageF);
1273 WMResizeWidget(panel->imageV, 165, 115);
1274 WMMoveWidget(panel->imageV, 15, 20);
1275 WMSetScrollViewRelief(panel->imageV, WRSunken);
1276 WMSetScrollViewHasHorizontalScroller(panel->imageV, True);
1277 WMSetScrollViewHasVerticalScroller(panel->imageV, True);
1278 WMSetScrollViewContentView(panel->imageV, WMWidgetView(panel->imageL));
1280 panel->browB = WMCreateCommandButton(panel->imageF);
1281 WMResizeWidget(panel->browB, 90, 24);
1282 WMMoveWidget(panel->browB, 190, 50);
1283 WMSetButtonText(panel->browB, _("Browse..."));
1284 WMSetButtonAction(panel->browB, browseImageCallback, panel);
1286 panel->dispB = WMCreateCommandButton(panel->imageF);
1287 WMResizeWidget(panel->dispB, 90, 24);
1288 WMMoveWidget(panel->dispB, 190, 80);
1289 WMSetButtonText(panel->dispB, _("Show"));
1291 panel->arrP = WMCreatePopUpButton(panel->imageF);
1292 WMResizeWidget(panel->arrP, 90, 20);
1293 WMMoveWidget(panel->arrP, 190, 120);
1294 WMAddPopUpButtonItem(panel->arrP, _("Tile"));
1295 WMAddPopUpButtonItem(panel->arrP, _("Scale"));
1296 WMAddPopUpButtonItem(panel->arrP, _("Center"));
1297 WMAddPopUpButtonItem(panel->arrP, _("Maximize"));
1298 WMSetPopUpButtonSelectedItem(panel->arrP, 0);
1300 WMMapSubwidgets(panel->imageF);
1302 /****/
1304 panel->okB = WMCreateCommandButton(panel->win);
1305 WMResizeWidget(panel->okB, 84, 24);
1306 WMMoveWidget(panel->okB, 225, 390);
1307 WMSetButtonText(panel->okB, _("OK"));
1308 WMSetButtonAction(panel->okB, buttonCallback, panel);
1310 panel->cancelB = WMCreateCommandButton(panel->win);
1311 WMResizeWidget(panel->cancelB, 84, 24);
1312 WMMoveWidget(panel->cancelB, 130, 390);
1313 WMSetButtonText(panel->cancelB, _("Cancel"));
1314 WMSetButtonAction(panel->cancelB, buttonCallback, panel);
1316 WMMapWidget(panel->nameF);
1317 WMMapWidget(panel->typeP);
1318 WMMapWidget(panel->okB);
1319 WMMapWidget(panel->cancelB);
1321 WMUnmapWidget(panel->arrP);
1323 WMRealizeWidget(panel->win);
1325 panel->currentType = -1;
1327 panel->sectionParts[TYPE_SOLID][0] = panel->defcF;
1329 panel->sectionParts[TYPE_GRADIENT][0] = panel->defcF;
1330 panel->sectionParts[TYPE_GRADIENT][1] = panel->gcolF;
1331 panel->sectionParts[TYPE_GRADIENT][2] = panel->dirF;
1333 panel->sectionParts[TYPE_SGRADIENT][0] = panel->tcolF;
1334 panel->sectionParts[TYPE_SGRADIENT][1] = panel->dirF;
1336 panel->sectionParts[TYPE_TGRADIENT][0] = panel->tcolF;
1337 panel->sectionParts[TYPE_TGRADIENT][1] = panel->dirF;
1338 panel->sectionParts[TYPE_TGRADIENT][2] = panel->imageF;
1339 panel->sectionParts[TYPE_TGRADIENT][3] = panel->topaF;
1340 panel->sectionParts[TYPE_TGRADIENT][4] = panel->arrP;
1342 panel->sectionParts[TYPE_PIXMAP][0] = panel->defcF;
1343 panel->sectionParts[TYPE_PIXMAP][1] = panel->imageF;
1344 panel->sectionParts[TYPE_PIXMAP][2] = panel->arrP;
1347 /* setup for first time */
1349 changeTypeCallback(panel->typeP, panel);
1351 sliderChangeCallback(panel->ghueS, panel);
1352 sliderChangeCallback(panel->gsatS, panel);
1354 return panel;
1360 *--------------------------------------------------------------------------
1361 * Test stuff
1362 *--------------------------------------------------------------------------
1365 #if 0
1367 char *ProgName = "test";
1369 void
1370 testOKButton(WMWidget *self, void *data)
1372 char *test;
1373 Display *dpy;
1374 Window win;
1375 Pixmap pix;
1376 RImage *image;
1378 TexturePanel *panel = (TexturePanel*)data;
1379 // test = GetTexturePanelTextureString(panel);
1381 wwarning(test);
1383 dpy = WMScreenDisplay(WMWidgetScreen(panel->okB));
1384 win = XCreateSimpleWindow(dpy, DefaultRootWindow(dpy), 10, 10, 250, 250,
1385 0, 0, 0);
1386 XMapRaised(dpy, win);
1387 XFlush(dpy);
1389 // image = RenderTexturePanelTexture(panel, 250, 250);
1391 RConvertImage(WMScreenRContext(WMWidgetScreen(panel->okB)), image, &pix);
1393 XCopyArea(dpy, pix, win, (WMScreenRContext(WMWidgetScreen(panel->okB)))->copy_gc, 0, 0, image->width, image->height,
1394 0, 0);
1396 free (test);
1400 void testCancelButton(WMWidget *self, void *data){
1401 wwarning("Exiting test....");
1402 exit(0);
1405 void wAbort()
1407 exit(1);
1410 int main(int argc, char **argv)
1412 TexturePanel *panel;
1414 Display *dpy = XOpenDisplay("");
1415 WMScreen *scr;
1417 /* char *test; */
1419 WMInitializeApplication("Test", &argc, argv);
1421 if (!dpy) {
1422 wfatal("could not open display");
1423 exit(1);
1426 scr = WMCreateSimpleApplicationScreen(dpy);
1428 panel = CreateTexturePanel(scr);
1430 SetTexturePanelOkAction(panel,(WMAction*)testOKButton,panel);
1431 SetTexturePanelCancelAction(panel,(WMAction*)testCancelButton,panel);
1433 SetTexturePanelTexture(panel, "pinky",
1434 PLGetProplistWithDescription("(mdgradient, pink, red, blue, yellow)"));
1436 ShowTexturePanel(panel);
1438 WMScreenMainLoop(scr);
1439 return 0;
1441 #endif