Change to the linux kernel coding style
[wmaker-crm.git] / WINGs / wcolorpanel.c
Commit [+]AuthorDateLineData
416e3a82 dan1999-01-25 19:06:50 +00001/*
2 * ColorPanel for WINGs
3 *
95e71d50 id1999-12-29 11:59:30 +00004 * by ]d : Original idea and basic initial code
6830b057 dan2004-10-12 21:28:27 +00005 * Pascal Hofstee : Code for wheeldrawing and calculating
95e71d50 id1999-12-29 11:59:30 +00006 * colors from it.
7 * Primary coder of this Color Panel.
8 * Alban Hertroys : Optimizations for algorithms for color-
6830b057 dan2004-10-12 21:28:27 +00009 * wheel. Also custom ColorPalettes and
95e71d50 id1999-12-29 11:59:30 +000010 * magnifying glass. Secondary coder ;)
11 * Alfredo K. Kojima : For pointing out memory-allocation
12 * problems and similair code-issues
13 * Marco van Hylckama-Vlieg : For once again doing the artwork ;-)
088c0ac2 dan1999-03-09 14:58:01 +000014 *
e7495baf dan1999-02-17 11:06:40 +000015 */
16
37e669d1 dan1999-05-04 00:05:56 +000017/* TODO:
95e71d50 id1999-12-29 11:59:30 +000018 * - Look at further optimization of colorWheel matrix calculation.
19 * It appears to be rather symmetric in angles of 60 degrees,
503c1e88 dan2000-01-07 22:08:35 +000020 * while it is optimized in angles of 90 degrees.
21 * - Custom color-lists and custom colors in custom color-lists.
37e669d1 dan1999-05-04 00:05:56 +000022 * - Stored colors
23 * - Resizing
24 */
25
882b9a8e kojima2001-07-23 20:31:32 +000026#include "wconfig.h"
416e3a82 dan1999-01-25 19:06:50 +000027#include "WINGsP.h"
577b3ee9 dan2006-04-26 20:28:24 +000028#include "rgb.h"
416e3a82 dan1999-01-25 19:06:50 +000029#include <math.h>
30#include <unistd.h>
31#include <ctype.h>
32#include <fcntl.h>
33#include <sys/types.h>
34#include <sys/stat.h>
35#include <dirent.h>
36#include <errno.h>
37
95e71d50 id1999-12-29 11:59:30 +000038/* BUG There's something fishy with shaped windows */
dd1ccd2f dan2002-12-01 05:49:13 +000039/* Whithout shape extension the magnified image is completely broken -Dan */
40#if 0
6830b057 dan2004-10-12 21:28:27 +000041# ifdef SHAPE
42# define SHAPE_WAS_DEFINED
43# undef SHAPE
44# endif
95e71d50 id1999-12-29 11:59:30 +000045#endif
46
95e71d50 id1999-12-29 11:59:30 +000047#ifdef SHAPE
6830b057 dan2004-10-12 21:28:27 +000048# include <X11/extensions/shape.h>
95e71d50 id1999-12-29 11:59:30 +000049#endif
50
bedc2238 kojima1999-05-16 07:00:01 +000051char *WMColorPanelColorChangedNotification = "WMColorPanelColorChangedNotification";
52
088c0ac2 dan1999-03-09 14:58:01 +000053/*
54 * Bitmaps for magnifying glass cursor
55 */
56
57/* Cursor */
58#define Cursor_x_hot 11
59#define Cursor_y_hot 11
60#define Cursor_width 32
61#define Cursor_height 32
62static unsigned char Cursor_bits[] = {
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +020063 0x00, 0x7e, 0x00, 0x00, 0xc0, 0x81, 0x03, 0x00, 0x20, 0x00, 0x04, 0x00, 0x10, 0x00, 0x08,
64 0x00, 0x08, 0x00, 0x10, 0x00, 0x04, 0x00, 0x20, 0x00, 0x02, 0x00, 0x40, 0x00, 0x02, 0x00,
65 0x40, 0x00, 0x02, 0x00, 0x40, 0x00, 0x01, 0x42, 0x80, 0x00, 0x01, 0x24, 0x80, 0x00, 0x01,
66 0x00, 0x80, 0x00, 0x01, 0x00, 0x80, 0x00, 0x01, 0x24, 0x80, 0x00, 0x01, 0x42, 0x80, 0x00,
67 0x02, 0x00, 0x40, 0x00, 0x02, 0x00, 0x40, 0x00, 0x02, 0x00, 0x40, 0x00, 0x04, 0x00, 0x20,
68 0x00, 0x08, 0x00, 0x50, 0x00, 0x10, 0x00, 0x88, 0x00, 0x20, 0x00, 0x5c, 0x01, 0xc0, 0x81,
69 0x3b, 0x02, 0x00, 0x7e, 0x70, 0x05, 0x00, 0x00, 0xe0, 0x08, 0x00, 0x00, 0xc0, 0x15, 0x00,
70 0x00, 0x80, 0x23, 0x00, 0x00, 0x00, 0x57, 0x00, 0x00, 0x00, 0x8e, 0x00, 0x00, 0x00, 0x5c,
71 0x00, 0x00, 0x00, 0xb8, 0x00, 0x00, 0x00, 0x70
72};
088c0ac2 dan1999-03-09 14:58:01 +000073
74/* Cursor shape-mask */
75#define Cursor_shape_width 32
76#define Cursor_shape_height 32
77static unsigned char Cursor_shape_bits[] = {
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +020078 0x00, 0x7e, 0x00, 0x00, 0xc0, 0x81, 0x03, 0x00, 0x20, 0x00, 0x04, 0x00, 0x10, 0x00, 0x08,
79 0x00, 0x08, 0x00, 0x10, 0x00, 0x04, 0x00, 0x20, 0x00, 0x02, 0x00, 0x40, 0x00, 0x02, 0x00,
80 0x40, 0x00, 0x02, 0x00, 0x40, 0x00, 0x01, 0x42, 0x80, 0x00, 0x01, 0x24, 0x80, 0x00, 0x01,
81 0x00, 0x80, 0x00, 0x01, 0x00, 0x80, 0x00, 0x01, 0x24, 0x80, 0x00, 0x01, 0x42, 0x80, 0x00,
82 0x02, 0x00, 0x40, 0x00, 0x02, 0x00, 0x40, 0x00, 0x02, 0x00, 0x40, 0x00, 0x04, 0x00, 0x20,
83 0x00, 0x08, 0x00, 0x70, 0x00, 0x10, 0x00, 0xf8, 0x00, 0x20, 0x00, 0xfc, 0x01, 0xc0, 0x81,
84 0xfb, 0x03, 0x00, 0x7e, 0xf0, 0x07, 0x00, 0x00, 0xe0, 0x0f, 0x00, 0x00, 0xc0, 0x1f, 0x00,
85 0x00, 0x80, 0x3f, 0x00, 0x00, 0x00, 0x7f, 0x00, 0x00, 0x00, 0xfe, 0x00, 0x00, 0x00, 0xfc,
86 0x00, 0x00, 0x00, 0xf8, 0x00, 0x00, 0x00, 0x70
87};
088c0ac2 dan1999-03-09 14:58:01 +000088
89/* Clip-mask for magnified pixels */
95e71d50 id1999-12-29 11:59:30 +000090#define Cursor_mask_width 24
91#define Cursor_mask_height 24
088c0ac2 dan1999-03-09 14:58:01 +000092static unsigned char Cursor_mask_bits[] = {
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +020093 0x00, 0x00, 0x00, 0x00, 0x7e, 0x00, 0xc0, 0xff, 0x03, 0xe0, 0xff, 0x07,
94 0xf0, 0xff, 0x0f, 0xf8, 0xff, 0x1f, 0xfc, 0xff, 0x3f, 0xfc, 0xff, 0x3f,
95 0xfc, 0xff, 0x3f, 0xfe, 0xff, 0x7f, 0xfe, 0xff, 0x7f, 0xfe, 0xff, 0x7f,
96 0xfe, 0xff, 0x7f, 0xfe, 0xff, 0x7f, 0xfe, 0xff, 0x7f, 0xfc, 0xff, 0x3f,
97 0xfc, 0xff, 0x3f, 0xfc, 0xff, 0x3f, 0xf8, 0xff, 0x1f, 0xf0, 0xff, 0x0f,
98 0xe0, 0xff, 0x07, 0xc0, 0xff, 0x03, 0x00, 0x7e, 0x00, 0x00, 0x00, 0x00
99};
e7495baf dan1999-02-17 11:06:40 +0000100
101typedef struct MovingView {
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200102 WMView *view; /* The view this is all about */
103 XImage *image; /* What's under the view */
104 XImage *dirtyRect; /* Storage of overlapped image area */
105 Pixmap magPix; /* Magnified part of pixmap */
106 RColor color; /* Color of a pixel in the image */
107 int x, y; /* Position of view */
e7495baf dan1999-02-17 11:06:40 +0000108} MovingView;
416e3a82 dan1999-01-25 19:06:50 +0000109
d98f1fa6 kojima2000-01-06 21:56:39 +0000110typedef struct CPColor {
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200111 RColor rgb; /* The RGB values of the color */
112 RHSVColor hsv; /* The HSV values of the color */
113 enum { /* Which one was last set ? */
114 cpNone,
115 cpRGB,
116 cpHSV
117 } set;
d98f1fa6 kojima2000-01-06 21:56:39 +0000118} CPColor;
6830b057 dan2004-10-12 21:28:27 +0000119
416e3a82 dan1999-01-25 19:06:50 +0000120typedef struct WheelMatrix {
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200121 unsigned int width, height; /* Size of the colorwheel */
122 unsigned char *data[3]; /* Wheel data (R,G,B) */
123 unsigned char values[256]; /* Precalculated values R,G & B = 0-255 */
416e3a82 dan1999-01-25 19:06:50 +0000124} wheelMatrix;
125
126typedef struct W_ColorPanel {
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200127 WMWindow *win;
128 WMFont *font8;
129 WMFont *font12;
130 void *clientData;
131 WMAction2 *action;
132
133 /* Common Stuff */
134 WMColorWell *colorWell;
135 WMButton *magnifyBtn;
136 WMButton *wheelBtn;
137 WMButton *slidersBtn;
138 WMButton *customPaletteBtn;
139 WMButton *colorListBtn;
140
141 /* Magnifying Glass */
142 MovingView *magnifyGlass;
143
144 /* ColorWheel Panel */
145 WMFrame *wheelFrm;
146 WMSlider *wheelBrightnessS;
147 WMView *wheelView;
148
149 /* Slider Panels */
150 WMFrame *slidersFrm;
151 WMFrame *seperatorFrm;
152 WMButton *grayBtn;
153 WMButton *rgbBtn;
154 WMButton *cmykBtn;
155 WMButton *hsbBtn;
156 /* Gray Scale Panel */
157 WMFrame *grayFrm;
158 WMLabel *grayMinL;
159 WMLabel *grayMaxL;
160 WMSlider *grayBrightnessS;
161 WMTextField *grayBrightnessT;
162 WMButton *grayPresetBtn[7];
163
164 /* RGB Panel */
165 WMFrame *rgbFrm;
166 WMLabel *rgbMinL;
167 WMLabel *rgbMaxL;
168 WMSlider *rgbRedS;
169 WMSlider *rgbGreenS;
170 WMSlider *rgbBlueS;
171 WMTextField *rgbRedT;
172 WMTextField *rgbGreenT;
173 WMTextField *rgbBlueT;
174
175 /* CMYK Panel */
176 WMFrame *cmykFrm;
177 WMLabel *cmykMinL;
178 WMLabel *cmykMaxL;
179 WMSlider *cmykCyanS;
180 WMSlider *cmykMagentaS;
181 WMSlider *cmykYellowS;
182 WMSlider *cmykBlackS;
183 WMTextField *cmykCyanT;
184 WMTextField *cmykMagentaT;
185 WMTextField *cmykYellowT;
186 WMTextField *cmykBlackT;
187
188 /* HSB Panel */
189 WMFrame *hsbFrm;
190 WMSlider *hsbHueS;
191 WMSlider *hsbSaturationS;
192 WMSlider *hsbBrightnessS;
193 WMTextField *hsbHueT;
194 WMTextField *hsbSaturationT;
195 WMTextField *hsbBrightnessT;
196
197 /* Custom Palette Panel */
198 WMFrame *customPaletteFrm;
199 WMPopUpButton *customPaletteHistoryBtn;
200 WMFrame *customPaletteContentFrm;
201 WMPopUpButton *customPaletteMenuBtn;
202 WMView *customPaletteContentView;
203
204 /* Color List Panel */
205 WMFrame *colorListFrm;
206 WMPopUpButton *colorListHistoryBtn;
207 WMList *colorListContentLst;
208 WMPopUpButton *colorListColorMenuBtn;
209 WMPopUpButton *colorListListMenuBtn;
210
211 /* Look-Up Tables and Images */
212 wheelMatrix *wheelMtrx;
213 Pixmap wheelImg;
214 Pixmap selectionImg;
215 Pixmap selectionBackImg;
216 RImage *customPaletteImg;
217 char *lastBrowseDir;
218
219 /* Common Data Fields */
220 CPColor color; /* Current color */
221 WMColorPanelMode mode; /* Current color selection mode */
222 WMColorPanelMode slidersmode; /* Current color sel. mode sliders panel */
223 WMColorPanelMode lastChanged; /* Panel that last changed the color */
224 int colx, coly; /* (x,y) of sel.-marker in WheelMode */
225 int palx, paly; /* (x,y) of sel.-marker in
226 CustomPaletteMode */
227 double palXRatio, palYRatio; /* Ratios in x & y between
228 original and scaled
229 palettesize */
230 int currentPalette;
231 char *configurationPath;
232
233 struct {
234 unsigned int continuous:1;
235 unsigned int dragging:1;
236 } flags;
416e3a82 dan1999-01-25 19:06:50 +0000237} W_ColorPanel;
238
239enum {
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200240 CPmenuNewFromFile,
241 CPmenuRename,
242 CPmenuRemove,
243 CPmenuCopy,
244 CPmenuNewFromClipboard
416e3a82 dan1999-01-25 19:06:50 +0000245} customPaletteMenuItem;
246
088c0ac2 dan1999-03-09 14:58:01 +0000247enum {
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200248 CLmenuAdd,
249 CLmenuRename,
250 CLmenuRemove
088c0ac2 dan1999-03-09 14:58:01 +0000251} colorListMenuItem;
252
577b3ee9 dan2006-04-26 20:28:24 +0000253#define PWIDTH 194
254#define PHEIGHT 266
255#define colorWheelSize 150
256#define customPaletteWidth 182
257#define customPaletteHeight 106
258#define knobThickness 8
416e3a82 dan1999-01-25 19:06:50 +0000259
577b3ee9 dan2006-04-26 20:28:24 +0000260#define SPECTRUM_WIDTH 511
261#define SPECTRUM_HEIGHT 360
416e3a82 dan1999-01-25 19:06:50 +0000262
577b3ee9 dan2006-04-26 20:28:24 +0000263#define COLORWHEEL_PART 1
264#define CUSTOMPALETTE_PART 2
265#define BUFSIZE 1024
fbd9035f kojima1999-05-30 04:52:05 +0000266
577b3ee9 dan2006-04-26 20:28:24 +0000267#define MAX_LENGTH 1024
416e3a82 dan1999-01-25 19:06:50 +0000268
648d75c2 dan2001-04-19 01:04:48 +0000269#ifndef M_PI
270#define M_PI 3.14159265358979323846
271#endif
272
92b012e8 dan2002-09-15 20:37:41 +0000273/* Silly hack for Windows systems with cygwin */
274#ifndef O_BINARY
275# define O_BINARY 0
276#endif
648d75c2 dan2001-04-19 01:04:48 +0000277
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200278static int fetchFile(char *toPath, char *imageSrcFile, char *imageDestFileName);
e7495baf dan1999-02-17 11:06:40 +0000279char *generateNewFilename(char *curName);
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200280void convertCPColor(CPColor * color);
281RColor ulongToRColor(WMScreen * scr, unsigned long value);
d98f1fa6 kojima2000-01-06 21:56:39 +0000282unsigned char getShift(unsigned char value);
416e3a82 dan1999-01-25 19:06:50 +0000283
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200284static void modeButtonCallback(WMWidget * w, void *data);
285static int getPickerPart(W_ColorPanel * panel, int x, int y);
286static void readConfiguration(W_ColorPanel * panel);
287static void readXColors(W_ColorPanel * panel);
288
289static void closeWindowCallback(WMWidget * w, void *data);
290
291static Cursor magnifyGrabPointer(W_ColorPanel * panel);
292static WMPoint magnifyInitialize(W_ColorPanel * panel);
293static void magnifyPutCursor(WMWidget * w, void *data);
294static Pixmap magnifyCreatePixmap(WMColorPanel * panel);
295static void magnifyGetImageStored(W_ColorPanel * panel, int x1, int y1, int x2, int y2);
296static XImage *magnifyGetImage(WMScreen * scr, XImage * image, int x, int y, int w, int h);
297
298static wheelMatrix *wheelCreateMatrix(unsigned int width, unsigned int height);
299static void wheelDestroyMatrix(wheelMatrix * matrix);
300static void wheelInitMatrix(W_ColorPanel * panel);
301static void wheelCalculateValues(W_ColorPanel * panel, int maxvalue);
302static void wheelRender(W_ColorPanel * panel);
303static Bool wheelInsideColorWheel(W_ColorPanel * panel, unsigned long ofs);
304static void wheelPaint(W_ColorPanel * panel);
305
306static void wheelHandleEvents(XEvent * event, void *data);
307static void wheelHandleActionEvents(XEvent * event, void *data);
308static void wheelBrightnessSliderCallback(WMWidget * w, void *data);
309static void wheelUpdateSelection(W_ColorPanel * panel);
310static void wheelUndrawSelection(W_ColorPanel * panel);
311
312static void wheelPositionSelection(W_ColorPanel * panel, int x, int y);
313static void wheelPositionSelectionOutBounds(W_ColorPanel * panel, int x, int y);
314static void wheelUpdateBrightnessGradientFromLocation(W_ColorPanel * panel);
315static void wheelUpdateBrightnessGradient(W_ColorPanel * panel, CPColor topColor);
316
317static void grayBrightnessSliderCallback(WMWidget * w, void *data);
318static void grayPresetButtonCallback(WMWidget * w, void *data);
319static void grayBrightnessTextFieldCallback(void *observerData, WMNotification * notification);
320
321static void rgbSliderCallback(WMWidget * w, void *data);
322static void rgbTextFieldCallback(void *observerData, WMNotification * notification);
323
324static void cmykSliderCallback(WMWidget * w, void *data);
325static void cmykTextFieldCallback(void *observerData, WMNotification * notification);
326
327static void hsbSliderCallback(WMWidget * w, void *data);
328static void hsbTextFieldCallback(void *observerData, WMNotification * notification);
329static void hsbUpdateBrightnessGradient(W_ColorPanel * panel);
330static void hsbUpdateSaturationGradient(W_ColorPanel * panel);
331static void hsbUpdateHueGradient(W_ColorPanel * panel);
332
333static void customRenderSpectrum(W_ColorPanel * panel);
334static void customSetPalette(W_ColorPanel * panel);
335static void customPaletteHandleEvents(XEvent * event, void *data);
336static void customPaletteHandleActionEvents(XEvent * event, void *data);
337static void customPalettePositionSelection(W_ColorPanel * panel, int x, int y);
338static void customPalettePositionSelectionOutBounds(W_ColorPanel * panel, int x, int y);
339static void customPaletteMenuCallback(WMWidget * w, void *data);
340static void customPaletteHistoryCallback(WMWidget * w, void *data);
341
342static void customPaletteMenuNewFromFile(W_ColorPanel * panel);
343static void customPaletteMenuRename(W_ColorPanel * panel);
344static void customPaletteMenuRemove(W_ColorPanel * panel);
345
346static void colorListPaintItem(WMList * lPtr, int index, Drawable d, char *text, int state, WMRect * rect);
347static void colorListSelect(WMWidget * w, void *data);
348static void colorListColorMenuCallback(WMWidget * w, void *data);
349static void colorListListMenuCallback(WMWidget * w, void *data);
350static void colorListListMenuNew(W_ColorPanel * panel);
351
352static void wheelInit(W_ColorPanel * panel);
353static void grayInit(W_ColorPanel * panel);
354static void rgbInit(W_ColorPanel * panel);
355static void cmykInit(W_ColorPanel * panel);
356static void hsbInit(W_ColorPanel * panel);
357
358void WMSetColorPanelAction(WMColorPanel * panel, WMAction2 * action, void *data)
359{
360 panel->action = action;
361 panel->clientData = data;
362}
363
364static WMColorPanel *makeColorPanel(WMScreen * scrPtr, char *name)
365{
366 WMColorPanel *panel;
367 RImage *image;
368 WMPixmap *pixmap;
369 RColor from;
370 RColor to;
371 WMColor *textcolor, *graybuttoncolor;
372 int i;
373 GC bgc = WMColorGC(scrPtr->black);
374 GC wgc = WMColorGC(scrPtr->white);
375
376 panel = wmalloc(sizeof(WMColorPanel));
377 memset(panel, 0, sizeof(WMColorPanel));
378
379 panel->color.rgb.red = 0;
380 panel->color.rgb.green = 0;
381 panel->color.rgb.blue = 0;
382 panel->color.hsv.hue = 0;
383 panel->color.hsv.saturation = 0;
384 panel->color.hsv.value = 0;
385 panel->color.set = cpNone; /* Color has not been set yet */
386
387 panel->font8 = WMSystemFontOfSize(scrPtr, 8);
388 panel->font12 = WMSystemFontOfSize(scrPtr, 12);
389
390 panel->win = WMCreateWindowWithStyle(scrPtr, name,
391 WMTitledWindowMask | WMClosableWindowMask | WMResizableWindowMask);
392 WMResizeWidget(panel->win, PWIDTH, PHEIGHT);
393 WMSetWindowTitle(panel->win, _("Colors"));
394 WMSetWindowCloseAction(panel->win, closeWindowCallback, panel);
395
396 /* Set Default ColorPanel Mode(s) */
397 panel->mode = WMWheelModeColorPanel;
398 panel->lastChanged = 0;
399 panel->slidersmode = WMRGBModeColorPanel;
400 panel->configurationPath = wstrconcat(wusergnusteppath(), "/Library/Colors/");
401
402 /* Some General Purpose Widgets */
403 panel->colorWell = WMCreateColorWell(panel->win);
404 WMResizeWidget(panel->colorWell, 134, 36);
405 WSetColorWellBordered(panel->colorWell, False);
406 WMMoveWidget(panel->colorWell, 56, 4);
407
408 panel->magnifyBtn = WMCreateCustomButton(panel->win, WBBStateLightMask | WBBStateChangeMask);
409 WMResizeWidget(panel->magnifyBtn, 46, 36);
410 WMMoveWidget(panel->magnifyBtn, 6, 4);
411 WMSetButtonAction(panel->magnifyBtn, magnifyPutCursor, panel);
412 WMSetButtonImagePosition(panel->magnifyBtn, WIPImageOnly);
413 WMSetButtonImage(panel->magnifyBtn, scrPtr->magnifyIcon);
414
415 panel->wheelBtn = WMCreateCustomButton(panel->win, WBBStateLightMask | WBBStateChangeMask);
416 WMResizeWidget(panel->wheelBtn, 46, 32);
417 WMMoveWidget(panel->wheelBtn, 6, 44);
418 WMSetButtonAction(panel->wheelBtn, modeButtonCallback, panel);
419 WMSetButtonImagePosition(panel->wheelBtn, WIPImageOnly);
420 WMSetButtonImage(panel->wheelBtn, scrPtr->wheelIcon);
421
422 panel->slidersBtn = WMCreateCustomButton(panel->win, WBBStateLightMask | WBBStateChangeMask);
423 WMResizeWidget(panel->slidersBtn, 46, 32);
424 WMMoveWidget(panel->slidersBtn, 52, 44);
425 WMSetButtonAction(panel->slidersBtn, modeButtonCallback, panel);
426 WMSetButtonImagePosition(panel->slidersBtn, WIPImageOnly);
427 WMSetButtonImage(panel->slidersBtn, scrPtr->rgbIcon);
428
429 panel->customPaletteBtn = WMCreateCustomButton(panel->win, WBBStateLightMask | WBBStateChangeMask);
430 WMResizeWidget(panel->customPaletteBtn, 46, 32);
431 WMMoveWidget(panel->customPaletteBtn, 98, 44);
432 WMSetButtonAction(panel->customPaletteBtn, modeButtonCallback, panel);
433 WMSetButtonImagePosition(panel->customPaletteBtn, WIPImageOnly);
434 WMSetButtonImage(panel->customPaletteBtn, scrPtr->customPaletteIcon);
435
436 panel->colorListBtn = WMCreateCustomButton(panel->win, WBBStateLightMask | WBBStateChangeMask);
437 WMResizeWidget(panel->colorListBtn, 46, 32);
438 WMMoveWidget(panel->colorListBtn, 144, 44);
439 WMSetButtonAction(panel->colorListBtn, modeButtonCallback, panel);
440 WMSetButtonImagePosition(panel->colorListBtn, WIPImageOnly);
441 WMSetButtonImage(panel->colorListBtn, scrPtr->colorListIcon);
442
443 /* Let's Group some of them together */
444 WMGroupButtons(panel->wheelBtn, panel->slidersBtn);
445 WMGroupButtons(panel->wheelBtn, panel->customPaletteBtn);
446 WMGroupButtons(panel->wheelBtn, panel->colorListBtn);
447
448 /* Widgets for the ColorWheel Panel */
449 panel->wheelFrm = WMCreateFrame(panel->win);
450 WMSetFrameRelief(panel->wheelFrm, WRFlat);
451 WMResizeWidget(panel->wheelFrm, PWIDTH - 8, PHEIGHT - 80 - 26);
452 WMMoveWidget(panel->wheelFrm, 5, 80);
453
454 panel->wheelView = W_CreateView(W_VIEW(panel->wheelFrm));
455 /* XXX Can we create a view ? */
456 W_ResizeView(panel->wheelView, colorWheelSize + 4, colorWheelSize + 4);
457 W_MoveView(panel->wheelView, 0, 0);
458
459 /* Create an event handler to handle expose/click events in ColorWheel */
460 WMCreateEventHandler(panel->wheelView,
461 ButtonPressMask | ButtonReleaseMask | EnterWindowMask |
462 LeaveWindowMask | ButtonMotionMask, wheelHandleActionEvents, panel);
463
464 WMCreateEventHandler(panel->wheelView, ExposureMask, wheelHandleEvents, panel);
465
466 panel->wheelBrightnessS = WMCreateSlider(panel->wheelFrm);
467 WMResizeWidget(panel->wheelBrightnessS, 16, 153);
468 WMMoveWidget(panel->wheelBrightnessS, 5 + colorWheelSize + 14, 1);
469 WMSetSliderMinValue(panel->wheelBrightnessS, 0);
470 WMSetSliderMaxValue(panel->wheelBrightnessS, 255);
471 WMSetSliderAction(panel->wheelBrightnessS, wheelBrightnessSliderCallback, panel);
472 WMSetSliderKnobThickness(panel->wheelBrightnessS, knobThickness);
473
474 panel->wheelMtrx = wheelCreateMatrix(colorWheelSize + 4, colorWheelSize + 4);
475 wheelInitMatrix(panel);
476
477 /* Widgets for the Slider Panels */
478 panel->slidersFrm = WMCreateFrame(panel->win);
479 WMSetFrameRelief(panel->slidersFrm, WRFlat);
480 WMResizeWidget(panel->slidersFrm, PWIDTH - 8, PHEIGHT - 80 - 26);
481 WMMoveWidget(panel->slidersFrm, 4, 80);
482
483 panel->seperatorFrm = WMCreateFrame(panel->slidersFrm);
484 WMSetFrameRelief(panel->seperatorFrm, WRPushed);
485 WMResizeWidget(panel->seperatorFrm, PWIDTH - 8, 2);
486 WMMoveWidget(panel->seperatorFrm, 0, 1);
487
488 panel->grayBtn = WMCreateCustomButton(panel->slidersFrm, WBBStateLightMask | WBBStateChangeMask);
489 WMResizeWidget(panel->grayBtn, 46, 24);
490 WMMoveWidget(panel->grayBtn, 1, 8);
491 WMSetButtonAction(panel->grayBtn, modeButtonCallback, panel);
492 WMSetButtonImagePosition(panel->grayBtn, WIPImageOnly);
493 WMSetButtonImage(panel->grayBtn, scrPtr->grayIcon);
494
495 panel->rgbBtn = WMCreateCustomButton(panel->slidersFrm, WBBStateLightMask | WBBStateChangeMask);
496 WMResizeWidget(panel->rgbBtn, 46, 24);
497 WMMoveWidget(panel->rgbBtn, 47, 8);
498 WMSetButtonAction(panel->rgbBtn, modeButtonCallback, panel);
499 WMSetButtonImagePosition(panel->rgbBtn, WIPImageOnly);
500 WMSetButtonImage(panel->rgbBtn, scrPtr->rgbIcon);
501
502 panel->cmykBtn = WMCreateCustomButton(panel->slidersFrm, WBBStateLightMask | WBBStateChangeMask);
503 WMResizeWidget(panel->cmykBtn, 46, 24);
504 WMMoveWidget(panel->cmykBtn, 93, 8);
505 WMSetButtonAction(panel->cmykBtn, modeButtonCallback, panel);
506 WMSetButtonImagePosition(panel->cmykBtn, WIPImageOnly);
507 WMSetButtonImage(panel->cmykBtn, scrPtr->cmykIcon);
508
509 panel->hsbBtn = WMCreateCustomButton(panel->slidersFrm, WBBStateLightMask | WBBStateChangeMask);
510 WMResizeWidget(panel->hsbBtn, 46, 24);
511 WMMoveWidget(panel->hsbBtn, 139, 8);
512 WMSetButtonAction(panel->hsbBtn, modeButtonCallback, panel);
513 WMSetButtonImagePosition(panel->hsbBtn, WIPImageOnly);
514 WMSetButtonImage(panel->hsbBtn, scrPtr->hsbIcon);
515
516 /* Let's Group the Slider Panel Buttons Together */
517 WMGroupButtons(panel->grayBtn, panel->rgbBtn);
518 WMGroupButtons(panel->grayBtn, panel->cmykBtn);
519 WMGroupButtons(panel->grayBtn, panel->hsbBtn);
520
521 textcolor = WMDarkGrayColor(scrPtr);
522
523 /* Widgets for GrayScale Panel */
524 panel->grayFrm = WMCreateFrame(panel->slidersFrm);
525 WMSetFrameRelief(panel->grayFrm, WRFlat);
526 WMResizeWidget(panel->grayFrm, PWIDTH - 8, PHEIGHT - 80 - 26 - 32);
527 WMMoveWidget(panel->grayFrm, 0, 34);
528
529 panel->grayMinL = WMCreateLabel(panel->grayFrm);
530 WMResizeWidget(panel->grayMinL, 20, 10);
531 WMMoveWidget(panel->grayMinL, 2, 2);
532 WMSetLabelText(panel->grayMinL, "0");
533 WMSetLabelTextAlignment(panel->grayMinL, WALeft);
534 WMSetLabelTextColor(panel->grayMinL, textcolor);
535 WMSetLabelFont(panel->grayMinL, panel->font8);
536
537 panel->grayMaxL = WMCreateLabel(panel->grayFrm);
538 WMResizeWidget(panel->grayMaxL, 40, 10);
539 WMMoveWidget(panel->grayMaxL, 104, 2);
540 WMSetLabelText(panel->grayMaxL, "100");
541 WMSetLabelTextAlignment(panel->grayMaxL, WARight);
542 WMSetLabelTextColor(panel->grayMaxL, textcolor);
543 WMSetLabelFont(panel->grayMaxL, panel->font8);
544
545 panel->grayBrightnessS = WMCreateSlider(panel->grayFrm);
546 WMResizeWidget(panel->grayBrightnessS, 141, 16);
547 WMMoveWidget(panel->grayBrightnessS, 2, 14);
548 WMSetSliderMinValue(panel->grayBrightnessS, 0);
549 WMSetSliderMaxValue(panel->grayBrightnessS, 100);
550 WMSetSliderKnobThickness(panel->grayBrightnessS, knobThickness);
551 WMSetSliderAction(panel->grayBrightnessS, grayBrightnessSliderCallback, panel);
552
553 from.red = 0;
554 from.green = 0;
555 from.blue = 0;
556
557 to.red = 255;
558 to.green = 255;
559 to.blue = 255;
560
561 image = RRenderGradient(141, 16, &from, &to, RGRD_HORIZONTAL);
562 pixmap = WMCreatePixmapFromRImage(scrPtr, image, 0);
563 RReleaseImage(image);
564
565 if (pixmap)
566 W_PaintText(W_VIEW(panel->grayBrightnessS), pixmap->pixmap,
567 panel->font12, 2, 0, 100, WALeft, scrPtr->white,
568 False, _("Brightness"), strlen(_("Brightness")));
569 else
570 wwarning(_("Color Panel: Could not allocate memory"));
571
572 WMSetSliderImage(panel->grayBrightnessS, pixmap);
573 WMReleasePixmap(pixmap);
574
575 panel->grayBrightnessT = WMCreateTextField(panel->grayFrm);
576 WMResizeWidget(panel->grayBrightnessT, 40, 18);
577 WMMoveWidget(panel->grayBrightnessT, 146, 13);
578 WMSetTextFieldAlignment(panel->grayBrightnessT, WALeft);
579 WMAddNotificationObserver(grayBrightnessTextFieldCallback, panel,
580 WMTextDidEndEditingNotification, panel->grayBrightnessT);
581
582 for (i = 0; i < 7; i++) {
583 pixmap = WMCreatePixmap(scrPtr, 13, 13, scrPtr->depth, False);
584
585 graybuttoncolor = WMCreateRGBColor(scrPtr, (255 / 6) * i << 8,
586 (255 / 6) * i << 8, (255 / 6) * i << 8, True);
587 WMPaintColorSwatch(graybuttoncolor, pixmap->pixmap, 0, 0, 15, 15);
588 WMReleaseColor(graybuttoncolor);
589
590 panel->grayPresetBtn[i] = WMCreateCommandButton(panel->grayFrm);
591 WMResizeWidget(panel->grayPresetBtn[i], 20, 24);
592 WMMoveWidget(panel->grayPresetBtn[i], 2 + (i * 20), 34);
593 WMSetButtonAction(panel->grayPresetBtn[i], grayPresetButtonCallback, panel);
594 WMSetButtonImage(panel->grayPresetBtn[i], pixmap);
595 WMSetButtonImagePosition(panel->grayPresetBtn[i], WIPImageOnly);
596 WMReleasePixmap(pixmap);
597
598 }
599
600 /* End of GrayScale Panel */
601
602 /* Widgets for RGB Panel */
603 panel->rgbFrm = WMCreateFrame(panel->slidersFrm);
604 WMSetFrameRelief(panel->rgbFrm, WRFlat);
605 WMResizeWidget(panel->rgbFrm, PWIDTH - 8, PHEIGHT - 80 - 26 - 32);
606 WMMoveWidget(panel->rgbFrm, 0, 34);
607
608 panel->rgbMinL = WMCreateLabel(panel->rgbFrm);
609 WMResizeWidget(panel->rgbMinL, 20, 10);
610 WMMoveWidget(panel->rgbMinL, 2, 2);
611 WMSetLabelText(panel->rgbMinL, "0");
612 WMSetLabelTextAlignment(panel->rgbMinL, WALeft);
613 WMSetLabelTextColor(panel->rgbMinL, textcolor);
614 WMSetLabelFont(panel->rgbMinL, panel->font8);
615
616 panel->rgbMaxL = WMCreateLabel(panel->rgbFrm);
617 WMResizeWidget(panel->rgbMaxL, 40, 10);
618 WMMoveWidget(panel->rgbMaxL, 104, 2);
619 WMSetLabelText(panel->rgbMaxL, "255");
620 WMSetLabelTextAlignment(panel->rgbMaxL, WARight);
621 WMSetLabelTextColor(panel->rgbMaxL, textcolor);
622 WMSetLabelFont(panel->rgbMaxL, panel->font8);
623
624 panel->rgbRedS = WMCreateSlider(panel->rgbFrm);
625 WMResizeWidget(panel->rgbRedS, 141, 16);
626 WMMoveWidget(panel->rgbRedS, 2, 14);
627 WMSetSliderMinValue(panel->rgbRedS, 0);
628 WMSetSliderMaxValue(panel->rgbRedS, 255);
629 WMSetSliderKnobThickness(panel->rgbRedS, knobThickness);
630 WMSetSliderAction(panel->rgbRedS, rgbSliderCallback, panel);
631
632 to.red = 255;
633 to.green = 0;
634 to.blue = 0;
635
636 image = RRenderGradient(141, 16, &from, &to, RGRD_HORIZONTAL);
637 pixmap = WMCreatePixmapFromRImage(scrPtr, image, 0);
638 RReleaseImage(image);
639
640 if (pixmap)
641 W_PaintText(W_VIEW(panel->rgbRedS), pixmap->pixmap, panel->font12,
642 2, 0, 100, WALeft, scrPtr->white, False, _("Red"), strlen(_("Red")));
643 else
644 wwarning(_("Color Panel: Could not allocate memory"));
645
646 WMSetSliderImage(panel->rgbRedS, pixmap);
647 WMReleasePixmap(pixmap);
648
649 panel->rgbRedT = WMCreateTextField(panel->rgbFrm);
650 WMResizeWidget(panel->rgbRedT, 40, 18);
651 WMMoveWidget(panel->rgbRedT, 146, 13);
652 WMSetTextFieldAlignment(panel->rgbRedT, WALeft);
653 WMAddNotificationObserver(rgbTextFieldCallback, panel, WMTextDidEndEditingNotification, panel->rgbRedT);
654
655 panel->rgbGreenS = WMCreateSlider(panel->rgbFrm);
656 WMResizeWidget(panel->rgbGreenS, 141, 16);
657 WMMoveWidget(panel->rgbGreenS, 2, 36);
658 WMSetSliderMinValue(panel->rgbGreenS, 0);
659 WMSetSliderMaxValue(panel->rgbGreenS, 255);
660 WMSetSliderKnobThickness(panel->rgbGreenS, knobThickness);
661 WMSetSliderAction(panel->rgbGreenS, rgbSliderCallback, panel);
662
663 to.red = 0;
664 to.green = 255;
665 to.blue = 0;
666
667 image = RRenderGradient(141, 16, &from, &to, RGRD_HORIZONTAL);
668 pixmap = WMCreatePixmapFromRImage(scrPtr, image, 0);
669 RReleaseImage(image);
670
671 if (pixmap)
672 W_PaintText(W_VIEW(panel->rgbGreenS), pixmap->pixmap, panel->font12,
673 2, 0, 100, WALeft, scrPtr->white, False, _("Green"), strlen(_("Green")));
674 else
675 wwarning(_("Color Panel: Could not allocate memory"));
676
677 WMSetSliderImage(panel->rgbGreenS, pixmap);
678 WMReleasePixmap(pixmap);
679
680 panel->rgbGreenT = WMCreateTextField(panel->rgbFrm);
681 WMResizeWidget(panel->rgbGreenT, 40, 18);
682 WMMoveWidget(panel->rgbGreenT, 146, 35);
683 WMSetTextFieldAlignment(panel->rgbGreenT, WALeft);
684 WMAddNotificationObserver(rgbTextFieldCallback, panel, WMTextDidEndEditingNotification, panel->rgbGreenT);
685
686 panel->rgbBlueS = WMCreateSlider(panel->rgbFrm);
687 WMResizeWidget(panel->rgbBlueS, 141, 16);
688 WMMoveWidget(panel->rgbBlueS, 2, 58);
689 WMSetSliderMinValue(panel->rgbBlueS, 0);
690 WMSetSliderMaxValue(panel->rgbBlueS, 255);
691 WMSetSliderKnobThickness(panel->rgbBlueS, knobThickness);
692 WMSetSliderAction(panel->rgbBlueS, rgbSliderCallback, panel);
693
694 to.red = 0;
695 to.green = 0;
696 to.blue = 255;
697
698 image = RRenderGradient(141, 16, &from, &to, RGRD_HORIZONTAL);
699 pixmap = WMCreatePixmapFromRImage(scrPtr, image, 0);
700 RReleaseImage(image);
701
702 if (pixmap)
703 W_PaintText(W_VIEW(panel->rgbBlueS), pixmap->pixmap, panel->font12,
704 2, 0, 100, WALeft, scrPtr->white, False, _("Blue"), strlen(_("Blue")));
705 else
706 wwarning(_("Color Panel: Could not allocate memory"));
707
708 WMSetSliderImage(panel->rgbBlueS, pixmap);
709 WMReleasePixmap(pixmap);
710
711 panel->rgbBlueT = WMCreateTextField(panel->rgbFrm);
712 WMResizeWidget(panel->rgbBlueT, 40, 18);
713 WMMoveWidget(panel->rgbBlueT, 146, 57);
714 WMSetTextFieldAlignment(panel->rgbBlueT, WALeft);
715 WMAddNotificationObserver(rgbTextFieldCallback, panel, WMTextDidEndEditingNotification, panel->rgbBlueT);
716 /* End of RGB Panel */
717
718 /* Widgets for CMYK Panel */
719 panel->cmykFrm = WMCreateFrame(panel->slidersFrm);
720 WMSetFrameRelief(panel->cmykFrm, WRFlat);
721 WMResizeWidget(panel->cmykFrm, PWIDTH - 8, PHEIGHT - 80 - 26 - 32);
722 WMMoveWidget(panel->cmykFrm, 0, 34);
723
724 panel->cmykMinL = WMCreateLabel(panel->cmykFrm);
725 WMResizeWidget(panel->cmykMinL, 20, 10);
726 WMMoveWidget(panel->cmykMinL, 2, 2);
727 WMSetLabelText(panel->cmykMinL, "0");
728 WMSetLabelTextAlignment(panel->cmykMinL, WALeft);
729 WMSetLabelTextColor(panel->cmykMinL, textcolor);
730 WMSetLabelFont(panel->cmykMinL, panel->font8);
731
732 panel->cmykMaxL = WMCreateLabel(panel->cmykFrm);
733 WMResizeWidget(panel->cmykMaxL, 40, 10);
734 WMMoveWidget(panel->cmykMaxL, 104, 2);
735 WMSetLabelText(panel->cmykMaxL, "100");
736 WMSetLabelTextAlignment(panel->cmykMaxL, WARight);
737 WMSetLabelTextColor(panel->cmykMaxL, textcolor);
738 WMSetLabelFont(panel->cmykMaxL, panel->font8);
739
740 panel->cmykCyanS = WMCreateSlider(panel->cmykFrm);
741 WMResizeWidget(panel->cmykCyanS, 141, 16);
742 WMMoveWidget(panel->cmykCyanS, 2, 14);
743 WMSetSliderMinValue(panel->cmykCyanS, 0);
744 WMSetSliderMaxValue(panel->cmykCyanS, 100);
745 WMSetSliderKnobThickness(panel->cmykCyanS, knobThickness);
746 WMSetSliderAction(panel->cmykCyanS, cmykSliderCallback, panel);
747
748 from.red = 255;
749 from.green = 255;
750 from.blue = 255;
751
752 to.red = 0;
753 to.green = 255;
754 to.blue = 255;
755
756 image = RRenderGradient(141, 16, &from, &to, RGRD_HORIZONTAL);
757 pixmap = WMCreatePixmapFromRImage(scrPtr, image, 0);
758 RReleaseImage(image);
759
760 if (pixmap)
761 W_PaintText(W_VIEW(panel->cmykCyanS), pixmap->pixmap, panel->font12,
762 2, 0, 100, WALeft, scrPtr->black, False, _("Cyan"), strlen(_("Cyan")));
763 else
764 wwarning(_("Color Panel: Could not allocate memory"));
765
766 WMSetSliderImage(panel->cmykCyanS, pixmap);
767 WMReleasePixmap(pixmap);
768
769 panel->cmykCyanT = WMCreateTextField(panel->cmykFrm);
770 WMResizeWidget(panel->cmykCyanT, 40, 18);
771 WMMoveWidget(panel->cmykCyanT, 146, 13);
772 WMSetTextFieldAlignment(panel->cmykCyanT, WALeft);
773 WMAddNotificationObserver(cmykTextFieldCallback, panel, WMTextDidEndEditingNotification, panel->cmykCyanT);
774
775 panel->cmykMagentaS = WMCreateSlider(panel->cmykFrm);
776 WMResizeWidget(panel->cmykMagentaS, 141, 16);
777 WMMoveWidget(panel->cmykMagentaS, 2, 36);
778 WMSetSliderMinValue(panel->cmykMagentaS, 0);
779 WMSetSliderMaxValue(panel->cmykMagentaS, 100);
780 WMSetSliderKnobThickness(panel->cmykMagentaS, knobThickness);
781 WMSetSliderAction(panel->cmykMagentaS, cmykSliderCallback, panel);
782
783 to.red = 255;
784 to.green = 0;
785 to.blue = 255;
786
787 image = RRenderGradient(141, 16, &from, &to, RGRD_HORIZONTAL);
788 pixmap = WMCreatePixmapFromRImage(scrPtr, image, 0);
789 RReleaseImage(image);
790
791 if (pixmap)
792 W_PaintText(W_VIEW(panel->cmykMagentaS), pixmap->pixmap, panel->font12,
793 2, 0, 100, WALeft, scrPtr->black, False, _("Magenta"), strlen(_("Magenta")));
794 else
795 wwarning(_("Color Panel: Could not allocate memory"));
796
797 WMSetSliderImage(panel->cmykMagentaS, pixmap);
798 WMReleasePixmap(pixmap);
799
800 panel->cmykMagentaT = WMCreateTextField(panel->cmykFrm);
801 WMResizeWidget(panel->cmykMagentaT, 40, 18);
802 WMMoveWidget(panel->cmykMagentaT, 146, 35);
803 WMSetTextFieldAlignment(panel->cmykMagentaT, WALeft);
804 WMAddNotificationObserver(cmykTextFieldCallback, panel,
805 WMTextDidEndEditingNotification, panel->cmykMagentaT);
806
807 panel->cmykYellowS = WMCreateSlider(panel->cmykFrm);
808 WMResizeWidget(panel->cmykYellowS, 141, 16);
809 WMMoveWidget(panel->cmykYellowS, 2, 58);
810 WMSetSliderMinValue(panel->cmykYellowS, 0);
811 WMSetSliderMaxValue(panel->cmykYellowS, 100);
812 WMSetSliderKnobThickness(panel->cmykYellowS, knobThickness);
813 WMSetSliderAction(panel->cmykYellowS, cmykSliderCallback, panel);
814
815 to.red = 255;
816 to.green = 255;
817 to.blue = 0;
818
819 image = RRenderGradient(141, 16, &from, &to, RGRD_HORIZONTAL);
820 pixmap = WMCreatePixmapFromRImage(scrPtr, image, 0);
821 RReleaseImage(image);
822
823 if (pixmap)
824 W_PaintText(W_VIEW(panel->cmykYellowS), pixmap->pixmap, panel->font12,
825 2, 0, 100, WALeft, scrPtr->black, False, _("Yellow"), strlen(_("Yellow")));
826 else
827 wwarning(_("Color Panel: Could not allocate memory"));
828
829 WMSetSliderImage(panel->cmykYellowS, pixmap);
830 WMReleasePixmap(pixmap);
831
832 panel->cmykYellowT = WMCreateTextField(panel->cmykFrm);
833 WMResizeWidget(panel->cmykYellowT, 40, 18);
834 WMMoveWidget(panel->cmykYellowT, 146, 57);
835 WMSetTextFieldAlignment(panel->cmykYellowT, WALeft);
836 WMAddNotificationObserver(cmykTextFieldCallback, panel,
837 WMTextDidEndEditingNotification, panel->cmykYellowT);
838
839 panel->cmykBlackS = WMCreateSlider(panel->cmykFrm);
840 WMResizeWidget(panel->cmykBlackS, 141, 16);
841 WMMoveWidget(panel->cmykBlackS, 2, 80);
842 WMSetSliderMinValue(panel->cmykBlackS, 0);
843 WMSetSliderMaxValue(panel->cmykBlackS, 100);
844 WMSetSliderValue(panel->cmykBlackS, 0);
845 WMSetSliderKnobThickness(panel->cmykBlackS, knobThickness);
846 WMSetSliderAction(panel->cmykBlackS, cmykSliderCallback, panel);
847
848 to.red = 0;
849 to.green = 0;
850 to.blue = 0;
851
852 image = RRenderGradient(141, 16, &from, &to, RGRD_HORIZONTAL);
853 pixmap = WMCreatePixmapFromRImage(scrPtr, image, 0);
854 RReleaseImage(image);
855
856 if (pixmap)
857 W_PaintText(W_VIEW(panel->cmykBlackS), pixmap->pixmap, panel->font12,
858 2, 0, 100, WALeft, scrPtr->black, False, _("Black"), strlen(_("Black")));
859 else
860 wwarning(_("Color Panel: Could not allocate memory"));
861
862 WMSetSliderImage(panel->cmykBlackS, pixmap);
863 WMReleasePixmap(pixmap);
864
865 panel->cmykBlackT = WMCreateTextField(panel->cmykFrm);
866 WMResizeWidget(panel->cmykBlackT, 40, 18);
867 WMMoveWidget(panel->cmykBlackT, 146, 79);
868 WMSetTextFieldAlignment(panel->cmykBlackT, WALeft);
869 WMAddNotificationObserver(cmykTextFieldCallback, panel,
870 WMTextDidEndEditingNotification, panel->cmykBlackT);
871 /* End of CMYK Panel */
872
873 /* Widgets for HSB Panel */
874 panel->hsbFrm = WMCreateFrame(panel->slidersFrm);
875 WMSetFrameRelief(panel->hsbFrm, WRFlat);
876 WMResizeWidget(panel->hsbFrm, PWIDTH - 8, PHEIGHT - 80 - 26 - 32);
877 WMMoveWidget(panel->hsbFrm, 0, 34);
878
879 panel->hsbHueS = WMCreateSlider(panel->hsbFrm);
880 WMResizeWidget(panel->hsbHueS, 141, 16);
881 WMMoveWidget(panel->hsbHueS, 2, 14);
882 WMSetSliderMinValue(panel->hsbHueS, 0);
883 WMSetSliderMaxValue(panel->hsbHueS, 359);
884 WMSetSliderKnobThickness(panel->hsbHueS, knobThickness);
885 WMSetSliderAction(panel->hsbHueS, hsbSliderCallback, panel);
886
887 panel->hsbHueT = WMCreateTextField(panel->hsbFrm);
888 WMResizeWidget(panel->hsbHueT, 40, 18);
889 WMMoveWidget(panel->hsbHueT, 146, 13);
890 WMSetTextFieldAlignment(panel->hsbHueT, WALeft);
891 WMAddNotificationObserver(hsbTextFieldCallback, panel, WMTextDidEndEditingNotification, panel->hsbHueT);
892
893 panel->hsbSaturationS = WMCreateSlider(panel->hsbFrm);
894 WMResizeWidget(panel->hsbSaturationS, 141, 16);
895 WMMoveWidget(panel->hsbSaturationS, 2, 36);
896 WMSetSliderMinValue(panel->hsbSaturationS, 0);
897 WMSetSliderMaxValue(panel->hsbSaturationS, 100);
898 WMSetSliderKnobThickness(panel->hsbSaturationS, knobThickness);
899 WMSetSliderAction(panel->hsbSaturationS, hsbSliderCallback, panel);
900
901 panel->hsbSaturationT = WMCreateTextField(panel->hsbFrm);
902 WMResizeWidget(panel->hsbSaturationT, 40, 18);
903 WMMoveWidget(panel->hsbSaturationT, 146, 35);
904 WMSetTextFieldAlignment(panel->hsbSaturationT, WALeft);
905 WMAddNotificationObserver(hsbTextFieldCallback, panel,
906 WMTextDidEndEditingNotification, panel->hsbSaturationT);
907
908 panel->hsbBrightnessS = WMCreateSlider(panel->hsbFrm);
909 WMResizeWidget(panel->hsbBrightnessS, 141, 16);
910 WMMoveWidget(panel->hsbBrightnessS, 2, 58);
911 WMSetSliderMinValue(panel->hsbBrightnessS, 0);
912 WMSetSliderMaxValue(panel->hsbBrightnessS, 100);
913 WMSetSliderKnobThickness(panel->hsbBrightnessS, knobThickness);
914 WMSetSliderAction(panel->hsbBrightnessS, hsbSliderCallback, panel);
915
916 panel->hsbBrightnessT = WMCreateTextField(panel->hsbFrm);
917 WMResizeWidget(panel->hsbBrightnessT, 40, 18);
918 WMMoveWidget(panel->hsbBrightnessT, 146, 57);
919 WMSetTextFieldAlignment(panel->hsbBrightnessT, WALeft);
920 WMAddNotificationObserver(hsbTextFieldCallback, panel,
921 WMTextDidEndEditingNotification, panel->hsbBrightnessT);
922 /* End of HSB Panel */
923
924 WMReleaseColor(textcolor);
925
926 /* Widgets for the CustomPalette Panel */
927 panel->customPaletteFrm = WMCreateFrame(panel->win);
928 WMSetFrameRelief(panel->customPaletteFrm, WRFlat);
929 WMResizeWidget(panel->customPaletteFrm, PWIDTH - 8, PHEIGHT - 80 - 26);
930 WMMoveWidget(panel->customPaletteFrm, 5, 80);
931
932 panel->customPaletteHistoryBtn = WMCreatePopUpButton(panel->customPaletteFrm);
933 WMAddPopUpButtonItem(panel->customPaletteHistoryBtn, _("Spectrum"));
934 WMSetPopUpButtonSelectedItem(panel->customPaletteHistoryBtn,
935 WMGetPopUpButtonNumberOfItems(panel->customPaletteHistoryBtn) - 1);
936 WMSetPopUpButtonAction(panel->customPaletteHistoryBtn, customPaletteHistoryCallback, panel);
937 WMResizeWidget(panel->customPaletteHistoryBtn, PWIDTH - 8, 20);
938 WMMoveWidget(panel->customPaletteHistoryBtn, 0, 0);
939
940 panel->customPaletteContentFrm = WMCreateFrame(panel->customPaletteFrm);
941 WMSetFrameRelief(panel->customPaletteContentFrm, WRSunken);
942 WMResizeWidget(panel->customPaletteContentFrm, PWIDTH - 8, PHEIGHT - 156);
943 WMMoveWidget(panel->customPaletteContentFrm, 0, 23);
944
945 panel->customPaletteContentView = W_CreateView(W_VIEW(panel->customPaletteContentFrm));
946 /* XXX Test if we can create a view */
947 W_ResizeView(panel->customPaletteContentView, customPaletteWidth, customPaletteHeight);
948 W_MoveView(panel->customPaletteContentView, 2, 2);
949
950 /* Create event handler to handle expose/click events in CustomPalette */
951 WMCreateEventHandler(panel->customPaletteContentView,
952 ButtonPressMask | ButtonReleaseMask | EnterWindowMask | LeaveWindowMask |
953 ButtonMotionMask, customPaletteHandleActionEvents, panel);
954
955 WMCreateEventHandler(panel->customPaletteContentView, ExposureMask, customPaletteHandleEvents, panel);
956
957 panel->customPaletteMenuBtn = WMCreatePopUpButton(panel->customPaletteFrm);
958 WMSetPopUpButtonPullsDown(panel->customPaletteMenuBtn, 1);
959 WMSetPopUpButtonText(panel->customPaletteMenuBtn, _("Palette"));
960 WMSetPopUpButtonAction(panel->customPaletteMenuBtn, customPaletteMenuCallback, panel);
961 WMResizeWidget(panel->customPaletteMenuBtn, PWIDTH - 8, 20);
962 WMMoveWidget(panel->customPaletteMenuBtn, 0, PHEIGHT - 130);
963
964 WMAddPopUpButtonItem(panel->customPaletteMenuBtn, _("New from File..."));
965 WMAddPopUpButtonItem(panel->customPaletteMenuBtn, _("Rename..."));
966 WMAddPopUpButtonItem(panel->customPaletteMenuBtn, _("Remove"));
967 WMAddPopUpButtonItem(panel->customPaletteMenuBtn, _("Copy"));
968 WMAddPopUpButtonItem(panel->customPaletteMenuBtn, _("New from Clipboard"));
969
970 WMSetPopUpButtonItemEnabled(panel->customPaletteMenuBtn, CPmenuRename, 0);
971 WMSetPopUpButtonItemEnabled(panel->customPaletteMenuBtn, CPmenuRemove, 0);
972 WMSetPopUpButtonItemEnabled(panel->customPaletteMenuBtn, CPmenuCopy, 0);
973 WMSetPopUpButtonItemEnabled(panel->customPaletteMenuBtn, CPmenuNewFromClipboard, 0);
974
975 customRenderSpectrum(panel);
976 panel->currentPalette = 0;
977 panel->palx = customPaletteWidth / 2;
978 panel->paly = customPaletteHeight / 2;
979
980 /* Widgets for the ColorList Panel */
981 panel->colorListFrm = WMCreateFrame(panel->win);
982 WMSetFrameRelief(panel->colorListFrm, WRFlat);
983 WMResizeWidget(panel->colorListFrm, PWIDTH - 8, PHEIGHT - 80 - 26);
984 WMMoveWidget(panel->colorListFrm, 5, 80);
985
986 panel->colorListHistoryBtn = WMCreatePopUpButton(panel->colorListFrm);
987 WMAddPopUpButtonItem(panel->colorListHistoryBtn, _("X11-Colors"));
988 WMSetPopUpButtonSelectedItem(panel->colorListHistoryBtn,
989 WMGetPopUpButtonNumberOfItems(panel->colorListHistoryBtn) - 1);
990 /* WMSetPopUpButtonAction(panel->colorListHistoryBtn,
991 * colorListHistoryCallback, panel); */
992 WMResizeWidget(panel->colorListHistoryBtn, PWIDTH - 8, 20);
993 WMMoveWidget(panel->colorListHistoryBtn, 0, 0);
994
995 panel->colorListContentLst = WMCreateList(panel->colorListFrm);
996 WMSetListAction(panel->colorListContentLst, colorListSelect, panel);
997 WMSetListUserDrawProc(panel->colorListContentLst, colorListPaintItem);
998 WMResizeWidget(panel->colorListContentLst, PWIDTH - 8, PHEIGHT - 156);
999 WMMoveWidget(panel->colorListContentLst, 0, 23);
1000 WMHangData(panel->colorListContentLst, panel);
1001
1002 panel->colorListColorMenuBtn = WMCreatePopUpButton(panel->colorListFrm);
1003 WMSetPopUpButtonPullsDown(panel->colorListColorMenuBtn, 1);
1004 WMSetPopUpButtonText(panel->colorListColorMenuBtn, _("Color"));
1005 WMSetPopUpButtonAction(panel->colorListColorMenuBtn, colorListColorMenuCallback, panel);
1006 WMResizeWidget(panel->colorListColorMenuBtn, (PWIDTH - 16) / 2, 20);
1007 WMMoveWidget(panel->colorListColorMenuBtn, 0, PHEIGHT - 130);
1008
1009 WMAddPopUpButtonItem(panel->colorListColorMenuBtn, _("Add..."));
1010 WMAddPopUpButtonItem(panel->colorListColorMenuBtn, _("Rename..."));
1011 WMAddPopUpButtonItem(panel->colorListColorMenuBtn, _("Remove"));
1012
1013 WMSetPopUpButtonItemEnabled(panel->colorListColorMenuBtn, CLmenuAdd, 0);
1014 WMSetPopUpButtonItemEnabled(panel->colorListColorMenuBtn, CLmenuRename, 0);
1015 WMSetPopUpButtonItemEnabled(panel->colorListColorMenuBtn, CLmenuRemove, 0);
1016
1017 panel->colorListListMenuBtn = WMCreatePopUpButton(panel->colorListFrm);
1018 WMSetPopUpButtonPullsDown(panel->colorListListMenuBtn, 1);
1019 WMSetPopUpButtonText(panel->colorListListMenuBtn, _("List"));
1020 WMSetPopUpButtonAction(panel->colorListListMenuBtn, colorListListMenuCallback, panel);
1021 WMResizeWidget(panel->colorListListMenuBtn, (PWIDTH - 16) / 2, 20);
1022 WMMoveWidget(panel->colorListListMenuBtn, (PWIDTH - 16) / 2 + 8, PHEIGHT - 130);
6830b057 dan2004-10-12 21:28:27 +00001023
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02001024 WMAddPopUpButtonItem(panel->colorListListMenuBtn, _("New..."));
1025 WMAddPopUpButtonItem(panel->colorListListMenuBtn, _("Rename..."));
1026 WMAddPopUpButtonItem(panel->colorListListMenuBtn, _("Remove"));
6830b057 dan2004-10-12 21:28:27 +00001027
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02001028 WMSetPopUpButtonItemEnabled(panel->colorListListMenuBtn, CLmenuAdd, 0);
1029 WMSetPopUpButtonItemEnabled(panel->colorListListMenuBtn, CLmenuRename, 0);
1030 WMSetPopUpButtonItemEnabled(panel->colorListListMenuBtn, CLmenuRemove, 0);
6830b057 dan2004-10-12 21:28:27 +00001031
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02001032 WMRealizeWidget(panel->win);
1033 WMMapSubwidgets(panel->win);
6830b057 dan2004-10-12 21:28:27 +00001034
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02001035 WMMapSubwidgets(panel->wheelFrm);
1036 WMMapSubwidgets(panel->slidersFrm);
1037 WMMapSubwidgets(panel->grayFrm);
1038 WMMapSubwidgets(panel->rgbFrm);
1039 WMMapSubwidgets(panel->cmykFrm);
1040 WMMapSubwidgets(panel->hsbFrm);
1041 WMMapSubwidgets(panel->customPaletteFrm);
1042 WMMapSubwidgets(panel->customPaletteContentFrm);
1043 WMMapSubwidgets(panel->colorListFrm);
6830b057 dan2004-10-12 21:28:27 +00001044
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02001045 /* Pixmap to indicate selection positions
1046 * wheelframe MUST be mapped.
1047 */
1048 panel->selectionImg = XCreatePixmap(scrPtr->display, WMWidgetXID(panel->win), 4, 4, scrPtr->depth);
1049 XFillRectangle(scrPtr->display, panel->selectionImg, bgc, 0, 0, 4, 4);
1050 XFillRectangle(scrPtr->display, panel->selectionImg, wgc, 1, 1, 2, 2);
6830b057 dan2004-10-12 21:28:27 +00001051
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02001052 readConfiguration(panel);
1053 readXColors(panel);
6830b057 dan2004-10-12 21:28:27 +00001054
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +02001055 return panel;
416e3a82 dan1999-01-25 19:06:50 +00001056}
1057
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +02001058WMColorPanel *WMGetColorPanel(WMScreen * scrPtr)
416e3a82 dan1999-01-25 19:06:50 +00001059{
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +02001060 WMColorPanel *panel;
6830b057 dan2004-10-12 21:28:27 +00001061
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02001062 if (scrPtr->sharedColorPanel)
1063 return scrPtr->sharedColorPanel;
6830b057 dan2004-10-12 21:28:27 +00001064
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +02001065 panel = makeColorPanel(scrPtr, "colorPanel");
6830b057 dan2004-10-12 21:28:27 +00001066
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +02001067 scrPtr->sharedColorPanel = panel;
6830b057 dan2004-10-12 21:28:27 +00001068
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +02001069 return panel;
416e3a82 dan1999-01-25 19:06:50 +00001070}
1071
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +02001072void WMFreeColorPanel(WMColorPanel * panel)
416e3a82 dan1999-01-25 19:06:50 +00001073{
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +02001074 W_Screen *scr = WMWidgetScreen(panel->win);
6830b057 dan2004-10-12 21:28:27 +00001075
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02001076 if (panel == scr->sharedColorPanel) {
1077 scr->sharedColorPanel = NULL;
1078 }
6830b057 dan2004-10-12 21:28:27 +00001079
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02001080 if (!panel)
1081 return;
6830b057 dan2004-10-12 21:28:27 +00001082
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02001083 WMRemoveNotificationObserver(panel);
1084 WMUnmapWidget(panel->win);
6830b057 dan2004-10-12 21:28:27 +00001085
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02001086 /* fonts */
1087 WMReleaseFont(panel->font8);
1088 WMReleaseFont(panel->font12);
6830b057 dan2004-10-12 21:28:27 +00001089
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02001090 /* pixmaps */
1091 wheelDestroyMatrix(panel->wheelMtrx);
1092 if (panel->wheelImg)
1093 XFreePixmap(scr->display, panel->wheelImg);
1094 if (panel->selectionImg)
1095 XFreePixmap(scr->display, panel->selectionImg);
1096 if (panel->selectionBackImg)
1097 XFreePixmap(scr->display, panel->selectionBackImg);
1098 RReleaseImage(panel->customPaletteImg);
6830b057 dan2004-10-12 21:28:27 +00001099
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02001100 /* structs */
1101 if (panel->lastBrowseDir)
1102 wfree(panel->lastBrowseDir);
1103 if (panel->configurationPath)
1104 wfree(panel->configurationPath);
6830b057 dan2004-10-12 21:28:27 +00001105
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +02001106 WMDestroyWidget(panel->win);
6830b057 dan2004-10-12 21:28:27 +00001107
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +02001108 wfree(panel);
416e3a82 dan1999-01-25 19:06:50 +00001109}
1110
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +02001111void WMCloseColorPanel(WMColorPanel * panel)
088c0ac2 dan1999-03-09 14:58:01 +00001112{
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +02001113 WMFreeColorPanel(panel);
7e07b7b5 kojima1999-04-10 19:32:34 +00001114}
088c0ac2 dan1999-03-09 14:58:01 +00001115
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +02001116void WMShowColorPanel(WMColorPanel * panel)
7e07b7b5 kojima1999-04-10 19:32:34 +00001117{
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02001118 WMScreen *scr = WMWidgetScreen(panel->win);
1119 WMColor *white = WMWhiteColor(scr);
6830b057 dan2004-10-12 21:28:27 +00001120
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02001121 if (panel->color.set == cpNone)
1122 WMSetColorPanelColor(panel, white);
1123 WMReleaseColor(white);
6830b057 dan2004-10-12 21:28:27 +00001124
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02001125 if (panel->mode != WMWheelModeColorPanel)
1126 WMPerformButtonClick(panel->wheelBtn);
d98f1fa6 kojima2000-01-06 21:56:39 +00001127
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +02001128 WMMapWidget(panel->win);
416e3a82 dan1999-01-25 19:06:50 +00001129}
1130
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +02001131static void closeWindowCallback(WMWidget * w, void *data)
7e07b7b5 kojima1999-04-10 19:32:34 +00001132{
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +02001133 W_ColorPanel *panel = (W_ColorPanel *) data;
6830b057 dan2004-10-12 21:28:27 +00001134
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +02001135 WMCloseColorPanel(panel);
7e07b7b5 kojima1999-04-10 19:32:34 +00001136}
1137
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +02001138static void readConfiguration(W_ColorPanel * panel)
416e3a82 dan1999-01-25 19:06:50 +00001139{
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +02001140 /* XXX Doesn't take care of "invalid" files */
6830b057 dan2004-10-12 21:28:27 +00001141
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02001142 DIR *dPtr;
1143 struct dirent *dp;
1144 struct stat stat_buf;
1145 int item;
6830b057 dan2004-10-12 21:28:27 +00001146
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02001147 if (stat(panel->configurationPath, &stat_buf) != 0) {
1148 if (mkdir(panel->configurationPath, S_IRWXU | S_IRGRP | S_IROTH | S_IXGRP | S_IXOTH) != 0) {
1149 wsyserror(_("Color Panel: Could not create directory %s needed"
1150 " to store configurations"), panel->configurationPath);
1151 WMSetPopUpButtonEnabled(panel->customPaletteMenuBtn, False);
1152 WMSetPopUpButtonEnabled(panel->colorListColorMenuBtn, False);
1153 WMSetPopUpButtonEnabled(panel->colorListListMenuBtn, False);
1154 WMRunAlertPanel(WMWidgetScreen(panel->win), panel->win,
1155 _("File Error"),
1156 _("Could not create ColorPanel configuration directory"),
1157 _("OK"), NULL, NULL);
1158 }
1159 return;
1160 }
1161
1162 if (!(dPtr = opendir(panel->configurationPath))) {
1163 wwarning(_("Color Panel: Could not find file"), "%s", panel->configurationPath);
1164 return;
1165 }
1166
1167 while ((dp = readdir(dPtr)) != NULL) {
1168 unsigned int perm_mask;
1169 char *path = wstrconcat(panel->configurationPath,
1170 dp->d_name);
1171
1172 if (dp->d_name[0] != '.') {
1173 item = WMGetPopUpButtonNumberOfItems(panel->customPaletteHistoryBtn);
1174 WMAddPopUpButtonItem(panel->customPaletteHistoryBtn, dp->d_name);
1175
1176 perm_mask = (access(path, R_OK) == 0);
1177 WMSetPopUpButtonItemEnabled(panel->customPaletteHistoryBtn, item, perm_mask);
1178 }
1179 wfree(path);
1180 }
1181 closedir(dPtr);
1182}
1183
1184static void readXColors(W_ColorPanel * panel)
1185{
1186 WMListItem *item;
1187 RGBColor *entry;
1188
1189 for (entry = rgbColors; entry->name != NULL; entry++) {
1190 item = WMAddListItem(panel->colorListContentLst, entry->name);
1191 item->clientData = (void *)&(entry->color);
1192 }
1193}
1194
1195void WMSetColorPanelPickerMode(WMColorPanel * panel, WMColorPanelMode mode)
1196{
1197 W_Screen *scr = WMWidgetScreen(panel->win);
1198
1199 if (mode != WMWheelModeColorPanel) {
1200 WMUnmapWidget(panel->wheelFrm);
1201 if (panel->selectionBackImg) {
1202 XFreePixmap(WMWidgetScreen(panel->win)->display, panel->selectionBackImg);
1203 panel->selectionBackImg = None;
1204 }
1205 }
1206 if (mode != WMGrayModeColorPanel)
1207 WMUnmapWidget(panel->grayFrm);
1208 if (mode != WMRGBModeColorPanel)
1209 WMUnmapWidget(panel->rgbFrm);
1210 if (mode != WMCMYKModeColorPanel)
1211 WMUnmapWidget(panel->cmykFrm);
1212 if (mode != WMHSBModeColorPanel)
1213 WMUnmapWidget(panel->hsbFrm);
1214 if (mode != WMCustomPaletteModeColorPanel) {
1215 WMUnmapWidget(panel->customPaletteFrm);
1216 if (panel->selectionBackImg) {
1217 XFreePixmap(WMWidgetScreen(panel->win)->display, panel->selectionBackImg);
1218 panel->selectionBackImg = None;
1219 }
1220 }
1221 if (mode != WMColorListModeColorPanel)
1222 WMUnmapWidget(panel->colorListFrm);
1223 if ((mode != WMGrayModeColorPanel) && (mode != WMRGBModeColorPanel) &&
1224 (mode != WMCMYKModeColorPanel) && (mode != WMHSBModeColorPanel))
1225 WMUnmapWidget(panel->slidersFrm);
1226 else
1227 panel->slidersmode = mode;
1228
1229 if (mode == WMWheelModeColorPanel) {
1230 WMMapWidget(panel->wheelFrm);
1231 WMSetButtonSelected(panel->wheelBtn, True);
1232 if (panel->lastChanged != WMWheelModeColorPanel)
1233 wheelInit(panel);
1234 wheelRender(panel);
1235 wheelPaint(panel);
1236 } else if (mode == WMGrayModeColorPanel) {
1237 WMMapWidget(panel->slidersFrm);
1238 WMSetButtonSelected(panel->slidersBtn, True);
1239 WMMapWidget(panel->grayFrm);
1240 WMSetButtonSelected(panel->grayBtn, True);
1241 WMSetButtonImage(panel->slidersBtn, scr->grayIcon);
1242 if (panel->lastChanged != WMGrayModeColorPanel)
1243 grayInit(panel);
1244 } else if (mode == WMRGBModeColorPanel) {
1245 WMMapWidget(panel->slidersFrm);
1246 WMSetButtonSelected(panel->slidersBtn, True);
1247 WMMapWidget(panel->rgbFrm);
1248 WMSetButtonSelected(panel->rgbBtn, True);
1249 WMSetButtonImage(panel->slidersBtn, scr->rgbIcon);
1250 if (panel->lastChanged != WMRGBModeColorPanel)
1251 rgbInit(panel);
1252 } else if (mode == WMCMYKModeColorPanel) {
1253 WMMapWidget(panel->slidersFrm);
1254 WMSetButtonSelected(panel->slidersBtn, True);
1255 WMMapWidget(panel->cmykFrm);
1256 WMSetButtonSelected(panel->cmykBtn, True);
1257 WMSetButtonImage(panel->slidersBtn, scr->cmykIcon);
1258 if (panel->lastChanged != WMCMYKModeColorPanel)
1259 cmykInit(panel);
1260 } else if (mode == WMHSBModeColorPanel) {
1261 WMMapWidget(panel->slidersFrm);
1262 WMSetButtonSelected(panel->slidersBtn, True);
1263 WMMapWidget(panel->hsbFrm);
1264 WMSetButtonSelected(panel->hsbBtn, True);
1265 WMSetButtonImage(panel->slidersBtn, scr->hsbIcon);
1266 if (panel->lastChanged != WMHSBModeColorPanel)
1267 hsbInit(panel);
1268 } else if (mode == WMCustomPaletteModeColorPanel) {
1269 WMMapWidget(panel->customPaletteFrm);
1270 WMSetButtonSelected(panel->customPaletteBtn, True);
1271 customSetPalette(panel);
1272 } else if (mode == WMColorListModeColorPanel) {
1273 WMMapWidget(panel->colorListFrm);
1274 WMSetButtonSelected(panel->colorListBtn, True);
1275 }
1276
1277 panel->mode = mode;
1278}
1279
1280WMColor *WMGetColorPanelColor(WMColorPanel * panel)
1281{
1282 return WMGetColorWellColor(panel->colorWell);
1283}
1284
1285void WMSetColorPanelColor(WMColorPanel * panel, WMColor * color)
1286{
1287 WMSetColorWellColor(panel->colorWell, color);
1288
1289 panel->color.rgb.red = color->color.red >> 8;
1290 panel->color.rgb.green = color->color.green >> 8;
1291 panel->color.rgb.blue = color->color.blue >> 8;
1292 panel->color.set = cpRGB;
1293
1294 if (panel->mode == panel->lastChanged)
1295 panel->lastChanged = 0;
1296
1297 WMSetColorPanelPickerMode(panel, panel->mode);
1298}
1299
1300static void updateSwatch(WMColorPanel * panel, CPColor color)
1301{
1302 WMScreen *scr = WMWidgetScreen(panel->win);
1303 WMColor *wellcolor;
1304
1305 if (color.set != cpRGB)
1306 convertCPColor(&color);
1307
1308 panel->color = color;
1309
1310 wellcolor = WMCreateRGBColor(scr, color.rgb.red << 8, color.rgb.green << 8, color.rgb.blue << 8, True);
1311
1312 WMSetColorWellColor(panel->colorWell, wellcolor);
1313 WMReleaseColor(wellcolor);
1314
1315 if (!panel->flags.dragging || panel->flags.continuous) {
1316 if (panel->action)
1317 (*panel->action) (panel, panel->clientData);
1318
1319 WMPostNotificationName(WMColorPanelColorChangedNotification, panel, NULL);
1320 }
1321}
1322
1323static void modeButtonCallback(WMWidget * w, void *data)
1324{
1325 W_ColorPanel *panel = (W_ColorPanel *) (data);
1326
1327 if (w == panel->wheelBtn)
1328 WMSetColorPanelPickerMode(panel, WMWheelModeColorPanel);
1329 else if (w == panel->slidersBtn)
1330 WMSetColorPanelPickerMode(panel, panel->slidersmode);
1331 else if (w == panel->customPaletteBtn)
1332 WMSetColorPanelPickerMode(panel, WMCustomPaletteModeColorPanel);
1333 else if (w == panel->colorListBtn)
1334 WMSetColorPanelPickerMode(panel, WMColorListModeColorPanel);
1335 else if (w == panel->grayBtn)
1336 WMSetColorPanelPickerMode(panel, WMGrayModeColorPanel);
1337 else if (w == panel->rgbBtn)
1338 WMSetColorPanelPickerMode(panel, WMRGBModeColorPanel);
1339 else if (w == panel->cmykBtn)
1340 WMSetColorPanelPickerMode(panel, WMCMYKModeColorPanel);
1341 else if (w == panel->hsbBtn)
1342 WMSetColorPanelPickerMode(panel, WMHSBModeColorPanel);
e7495baf dan1999-02-17 11:06:40 +00001343}
1344
e7495baf dan1999-02-17 11:06:40 +00001345/****************** Magnifying Cursor Functions *******************/
95e71d50 id1999-12-29 11:59:30 +00001346
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02001347static XImage *magnifyGetImage(WMScreen * scr, XImage * image, int x, int y, int w, int h)
1348{
1349 int x0 = 0, y0 = 0, w0 = w, h0 = h;
1350 const int displayWidth = DisplayWidth(scr->display, scr->screen),
1351 displayHeight = DisplayHeight(scr->display, scr->screen);
1352
1353 if (!(image && image->data)) {
1354 /* The image in panel->magnifyGlass->image does not exist yet.
1355 * Grab one from the screen (not beyond) and use it from now on.
1356 */
1357 if (!(image = XGetImage(scr->display, scr->rootWin,
1358 x - Cursor_x_hot, y - Cursor_y_hot, w, h, AllPlanes, ZPixmap)))
1359 wwarning(_("Color Panel: X failed request"));
1360
1361 return image;
1362 }
1363
1364 /* Coordinate correction for back pixmap
1365 * if magnifying glass is at screen-borders
1366 */
1367
1368 /* Figure 1: Shifting of rectangle-to-grab at top/left screen borders
1369 * Hatched area is beyond screen border.
1370 *
1371 * |<-Cursor_x_hot->|
1372 * ________________|_____
1373 * |/ / / / / / /| | |
1374 * | / / / / / / |(x,y) |
1375 * |/_/_/_/_/_/_/|________|
1376 * |<----x0----->|<--w0-->|
1377 * 0
1378 */
1379
1380 /* Figure 2: Shifting of rectangle-to-grab at bottom/right
1381 * screen borders
1382 * Hatched area is beyond screen border
1383 *
1384 * |<-Cursor_x_hot->|
1385 * ________________|_______________
1386 * | | | / / / / / /|
1387 * | (x,y)|/ / / / / / |
1388 * |___________________|_/_/_/_/_/_/|
1389 * |<-------w0-------->| |
1390 * |<---------------w--|----------->|
1391 * | |
1392 * x0 Displaywidth-1
1393 */
1394
1395 if (x < Cursor_x_hot) { /* see fig. 1 */
1396 x0 = Cursor_x_hot - x;
1397 w0 = w - x0;
1398 }
1399
1400 if (displayWidth - 1 < x - Cursor_x_hot + w) { /* see fig. 2 */
1401 w0 = (displayWidth) - (x - Cursor_x_hot);
1402 }
1403
1404 if (y < Cursor_y_hot) { /* see fig. 1 */
1405 y0 = Cursor_y_hot - y;
1406 h0 = h - y0;
1407 }
1408
1409 if (displayHeight - 1 < y - Cursor_y_hot + h) { /* see fig. 2 */
1410 h0 = (displayHeight) - (y - Cursor_y_hot);
1411 }
1412 /* end of coordinate correction */
1413
1414 /* Grab an image from the screen, clipped if necessary,
1415 * and put it in the existing panel->magnifyGlass->image
1416 * with the corresponding clipping offset.
1417 */
1418 if (!XGetSubImage(scr->display, scr->rootWin,
1419 x - Cursor_x_hot + x0, y - Cursor_y_hot + y0, w0, h0, AllPlanes, ZPixmap, image, x0, y0))
1420 wwarning(_("Color Panel: X failed request"));
1421
1422 return NULL;
1423}
1424
1425static void magnifyGetImageStored(WMColorPanel * panel, int x1, int y1, int x2, int y2)
1426{
1427 /* (x1, y1) = topleft corner of existing rectangle
1428 * (x2, y2) = topleft corner of new position
1429 */
1430
1431 W_Screen *scr = WMWidgetScreen(panel->win);
1432 int xa = 0, ya = 0, xb = 0, yb = 0;
1433 int width, height;
1434 const int dx = abs(x2 - x1), dy = abs(y2 - y1);
1435 XImage *image;
1436 const int x_min = Cursor_x_hot,
1437 y_min = Cursor_y_hot,
1438 x_max = DisplayWidth(scr->display, scr->screen) - 1 -
1439 (Cursor_mask_width - Cursor_x_hot),
1440 y_max = DisplayHeight(scr->display, scr->screen) - 1 - (Cursor_mask_height - Cursor_y_hot);
1441
1442 if ((dx == 0) && (dy == 0) && panel->magnifyGlass->image)
1443 return; /* No movement */
1444
1445 if (x1 < x2)
1446 xa = dx;
1447 else
1448 xb = dx;
1449
1450 if (y1 < y2)
1451 ya = dy;
1452 else
1453 yb = dy;
1454
1455 width = Cursor_mask_width - dx;
1456 height = Cursor_mask_height - dy;
1457
1458 /* If the traversed distance is larger than the size of the magnifying
1459 * glass contents, there is no need to do dirty rectangles. A whole new
1460 * rectangle can be grabbed (unless that rectangle falls partially
1461 * off screen).
1462 * Destroying the image and setting it to NULL will achieve that later on.
1463 *
1464 * Of course, grabbing an XImage beyond the borders of the screen will
1465 * cause trouble, this is considdered a special case. Part of the screen
1466 * is grabbed, but there is no need for dirty rectangles.
1467 */
1468 if ((width <= 0) || (height <= 0)) {
1469 if ((x2 >= x_min) && (y2 >= y_min) && (x2 <= x_max) && (y2 <= y_max)) {
1470 if (panel->magnifyGlass->image)
1471 XDestroyImage(panel->magnifyGlass->image);
1472 panel->magnifyGlass->image = NULL;
1473 }
1474 } else {
1475 if (panel->magnifyGlass->image) {
1476 /* Get dirty rectangle from panel->magnifyGlass->image */
1477 panel->magnifyGlass->dirtyRect =
1478 XSubImage(panel->magnifyGlass->image, xa, ya, width, height);
1479 if (!panel->magnifyGlass->dirtyRect) {
1480 wwarning(_("Color Panel: X failed request"));
1481 return; /* X returned a NULL from XSubImage */
1482 }
1483 }
1484 }
1485
1486 /* Get image from screen */
1487 image = magnifyGetImage(scr, panel->magnifyGlass->image, x2, y2, Cursor_mask_width, Cursor_mask_height);
1488 if (image) { /* Only reassign if a *new* image was grabbed */
1489 panel->magnifyGlass->image = image;
1490 return;
1491 }
1492
1493 /* Copy previously stored rectangle on covered part of image */
1494 if (panel->magnifyGlass->image && panel->magnifyGlass->dirtyRect) {
1495 int old_height;
1496
1497 /* "width" and "height" are used as coordinates here,
1498 * and run from [0...width-1] and [0...height-1] respectively.
1499 */
1500 width--;
1501 height--;
1502 old_height = height;
1503
1504 for (; width >= 0; width--)
1505 for (height = old_height; height >= 0; height--)
1506 XPutPixel(panel->magnifyGlass->image, xb + width, yb + height,
1507 XGetPixel(panel->magnifyGlass->dirtyRect, width, height));
1508 XDestroyImage(panel->magnifyGlass->dirtyRect);
1509 panel->magnifyGlass->dirtyRect = NULL;
1510 }
1511
1512 return;
1513}
1514
1515static Pixmap magnifyCreatePixmap(WMColorPanel * panel)
1516{
1517 W_Screen *scr = WMWidgetScreen(panel->win);
1518 int u, v;
d98f1fa6 kojima2000-01-06 21:56:39 +00001519#ifndef SHAPE
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +02001520 Pixmap pixmap;
d98f1fa6 kojima2000-01-06 21:56:39 +00001521#endif
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02001522 unsigned long color;
1523
1524 if (!panel->magnifyGlass->image)
1525 return None;
1526
1527 if (!panel->magnifyGlass->magPix)
1528 return None;
1529
1530 /*
1531 * Copy an area of only 5x5 pixels from the center of the image.
1532 */
1533 for (u = 0; u < 5; u++) {
1534 for (v = 0; v < 5; v++) {
1535 color = XGetPixel(panel->magnifyGlass->image, u + 9, v + 9);
1536
1537 XSetForeground(scr->display, scr->copyGC, color);
1538
1539 if ((u == 2) && (v == 2)) /* (2,2) is center pixel (unmagn.) */
1540 panel->magnifyGlass->color = ulongToRColor(scr, color);
1541
1542 /* The center square must eventually be centered around the
1543 * hotspot. The image needs shifting to achieve this. The amount of
1544 * shifting is (Cursor_mask_width/2 - 2 * square_size) = 11-10 = 1
1545 * _ _ _ _ _
1546 * |_|_|_|_|_|
1547 * ^------- center of center square == Cursor_x_hot
1548 */
1549 XFillRectangle(scr->display, panel->magnifyGlass->magPix,
1550 scr->copyGC,
1551 u * 5 + (u == 0 ? 0 : -1), v * 5 + (v == 0 ? 0 : -1),
1552 (u == 0 ? 4 : 5), (v == 0 ? 4 : 5));
1553 }
1554 }
6830b057 dan2004-10-12 21:28:27 +00001555
d98f1fa6 kojima2000-01-06 21:56:39 +00001556#ifdef SHAPE
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +02001557 return panel->magnifyGlass->magPix;
d98f1fa6 kojima2000-01-06 21:56:39 +00001558#else
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02001559 pixmap = XCreatePixmap(scr->display, W_DRAWABLE(scr), Cursor_mask_width, Cursor_mask_height, scr->depth);
1560 if (!pixmap)
1561 return None;
6830b057 dan2004-10-12 21:28:27 +00001562
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02001563 XPutImage(scr->display, pixmap, scr->copyGC, panel->magnifyGlass->image,
1564 0, 0, 0, 0, Cursor_mask_width, Cursor_mask_height);
95e71d50 id1999-12-29 11:59:30 +00001565
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02001566 /* Copy the magnified pixmap, with the clip mask, to background pixmap */
1567 XCopyArea(scr->display, panel->magnifyGlass->magPix, pixmap,
1568 scr->clipGC, 0, 0, Cursor_mask_width, Cursor_mask_height, 0, 0);
1569 /* (2,2) puts center pixel on center of glass */
6830b057 dan2004-10-12 21:28:27 +00001570
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +02001571 return pixmap;
d98f1fa6 kojima2000-01-06 21:56:39 +00001572#endif
1573
e7495baf dan1999-02-17 11:06:40 +00001574}
1575
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +02001576static WMView *magnifyCreateView(W_ColorPanel * panel)
e7495baf dan1999-02-17 11:06:40 +00001577{
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02001578 W_Screen *scr = WMWidgetScreen(panel->win);
1579 WMView *magView;
6830b057 dan2004-10-12 21:28:27 +00001580
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02001581 magView = W_CreateTopView(scr);
1582 if (!magView)
1583 return NULL;
6830b057 dan2004-10-12 21:28:27 +00001584
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02001585 magView->self = panel->win;
1586 magView->flags.topLevel = 1;
1587 magView->attribFlags |= CWOverrideRedirect | CWSaveUnder;
1588 magView->attribs.override_redirect = True;
1589 magView->attribs.save_under = True;
6830b057 dan2004-10-12 21:28:27 +00001590
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +02001591 W_ResizeView(magView, Cursor_mask_width, Cursor_mask_height);
6830b057 dan2004-10-12 21:28:27 +00001592
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +02001593 W_RealizeView(magView);
6830b057 dan2004-10-12 21:28:27 +00001594
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +02001595 return magView;
e7495baf dan1999-02-17 11:06:40 +00001596}
1597
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +02001598static Cursor magnifyGrabPointer(W_ColorPanel * panel)
e7495baf dan1999-02-17 11:06:40 +00001599{
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02001600 W_Screen *scr = WMWidgetScreen(panel->win);
1601 Pixmap magPixmap, magPixmap2;
1602 Cursor magCursor;
1603 XColor fgColor = { 0, 0, 0, 0, DoRed | DoGreen | DoBlue };
1604 XColor bgColor = { 0, 0xbf00, 0xa000, 0x5000, DoRed | DoGreen | DoBlue };
6830b057 dan2004-10-12 21:28:27 +00001605
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02001606 /* Cursor creation stuff */
1607 magPixmap = XCreatePixmapFromBitmapData(scr->display, W_DRAWABLE(scr),
1608 (char *)Cursor_bits, Cursor_width, Cursor_height, 1, 0, 1);
1609 magPixmap2 = XCreatePixmapFromBitmapData(scr->display, W_DRAWABLE(scr),
1610 (char *)Cursor_shape_bits, Cursor_width, Cursor_height, 1, 0, 1);
6830b057 dan2004-10-12 21:28:27 +00001611
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02001612 magCursor = XCreatePixmapCursor(scr->display, magPixmap, magPixmap2,
1613 &fgColor, &bgColor, Cursor_x_hot, Cursor_y_hot);
6830b057 dan2004-10-12 21:28:27 +00001614
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02001615 XFreePixmap(scr->display, magPixmap);
1616 XFreePixmap(scr->display, magPixmap2);
6830b057 dan2004-10-12 21:28:27 +00001617
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +02001618 XRecolorCursor(scr->display, magCursor, &fgColor, &bgColor);
6830b057 dan2004-10-12 21:28:27 +00001619
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02001620 /* Set up Pointer */
1621 XGrabPointer(scr->display, panel->magnifyGlass->view->window, True,
1622 PointerMotionMask | ButtonPressMask,
1623 GrabModeAsync, GrabModeAsync, scr->rootWin, magCursor, CurrentTime);
6830b057 dan2004-10-12 21:28:27 +00001624
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +02001625 return magCursor;
e7495baf dan1999-02-17 11:06:40 +00001626}
1627
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +02001628static WMPoint magnifyInitialize(W_ColorPanel * panel)
e7495baf dan1999-02-17 11:06:40 +00001629{
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02001630 W_Screen *scr = WMWidgetScreen(panel->win);
1631 int x, y, u, v;
1632 unsigned int mask;
1633 Pixmap pixmap, clip_mask;
1634 WMPoint point;
1635 Window root_return, child_return;
95e71d50 id1999-12-29 11:59:30 +00001636
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02001637 clip_mask = XCreatePixmapFromBitmapData(scr->display, W_DRAWABLE(scr),
1638 (char *)Cursor_mask_bits, Cursor_mask_width, Cursor_mask_height,
1639 1, 0, 1);
1640 panel->magnifyGlass->magPix = XCreatePixmap(scr->display, W_DRAWABLE(scr),
1641 5 * 5 - 1, 5 * 5 - 1, scr->depth);
6830b057 dan2004-10-12 21:28:27 +00001642
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +02001643 XQueryPointer(scr->display, scr->rootWin, &root_return, &child_return, &x, &y, &u, &v, &mask);
6830b057 dan2004-10-12 21:28:27 +00001644
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +02001645 panel->magnifyGlass->image = NULL;
6830b057 dan2004-10-12 21:28:27 +00001646
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +02001647 /* Clipmask to make magnified view-contents circular */
95e71d50 id1999-12-29 11:59:30 +00001648#ifdef SHAPE
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02001649 XShapeCombineMask(scr->display, WMViewXID(panel->magnifyGlass->view),
1650 ShapeBounding, 0, 0, clip_mask, ShapeSet);
95e71d50 id1999-12-29 11:59:30 +00001651#else
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02001652 /* Clip circle in glass cursor */
1653 XSetClipMask(scr->display, scr->clipGC, clip_mask);
1654 XSetClipOrigin(scr->display, scr->clipGC, 0, 0);
95e71d50 id1999-12-29 11:59:30 +00001655#endif
1656
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +02001657 XFreePixmap(scr->display, clip_mask);
6830b057 dan2004-10-12 21:28:27 +00001658
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02001659 /* Draw initial magnifying glass contents */
1660 magnifyGetImageStored(panel, x, y, x, y);
6830b057 dan2004-10-12 21:28:27 +00001661
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02001662 pixmap = magnifyCreatePixmap(panel);
1663 XSetWindowBackgroundPixmap(scr->display, WMViewXID(panel->magnifyGlass->view), pixmap);
1664 XClearWindow(scr->display, WMViewXID(panel->magnifyGlass->view));
1665 XFlush(scr->display);
6830b057 dan2004-10-12 21:28:27 +00001666
1667#ifndef SHAPE
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +02001668 XFreePixmap(scr->display, pixmap);
d98f1fa6 kojima2000-01-06 21:56:39 +00001669#endif
6830b057 dan2004-10-12 21:28:27 +00001670
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02001671 point.x = x;
1672 point.y = y;
1673
1674 return point;
1675}
1676
1677static void magnifyPutCursor(WMWidget * w, void *data)
1678{
1679 W_ColorPanel *panel = (W_ColorPanel *) (data);
1680 W_Screen *scr = WMWidgetScreen(panel->win);
1681 Cursor magCursor;
1682 Pixmap pixmap;
1683 XEvent event;
1684 WMPoint initialPosition;
1685
1686 /* Destroy wheelBackImg, so it'll update properly */
1687 if (panel->selectionBackImg) {
1688 XFreePixmap(WMWidgetScreen(panel->win)->display, panel->selectionBackImg);
1689 panel->selectionBackImg = None;
1690 }
1691
1692 /* Create magnifying glass */
1693 panel->magnifyGlass = wmalloc(sizeof(MovingView));
1694 panel->magnifyGlass->view = magnifyCreateView(panel);
1695 if (!panel->magnifyGlass->view)
1696 return;
1697
1698 initialPosition = magnifyInitialize(panel);
1699 panel->magnifyGlass->x = initialPosition.x;
1700 panel->magnifyGlass->y = initialPosition.y;
1701
1702 W_MoveView(panel->magnifyGlass->view,
1703 panel->magnifyGlass->x - Cursor_x_hot, panel->magnifyGlass->y - Cursor_y_hot);
1704 W_MapView(panel->magnifyGlass->view);
1705
1706 magCursor = magnifyGrabPointer(panel);
1707
1708 while (panel->magnifyGlass->image) {
1709 WMNextEvent(scr->display, &event);
1710
1711 /* Pack motion events */
1712 while (XCheckTypedEvent(scr->display, MotionNotify, &event)) {
1713 }
1714
1715 switch (event.type) {
1716 case ButtonPress:
1717 XDestroyImage(panel->magnifyGlass->image);
1718 panel->magnifyGlass->image = NULL;
1719
1720 if (event.xbutton.button == Button1) {
1721 panel->color.rgb = panel->magnifyGlass->color;
1722 panel->color.set = cpRGB;
1723 updateSwatch(panel, panel->color);
1724 }
1725 switch (panel->mode) {
1726 case WMWheelModeColorPanel:
1727 wheelInit(panel);
1728 wheelRender(panel);
1729 wheelPaint(panel);
1730 break;
1731 case WMGrayModeColorPanel:
1732 grayInit(panel);
1733 break;
1734 case WMRGBModeColorPanel:
1735 rgbInit(panel);
1736 break;
1737 case WMCMYKModeColorPanel:
1738 cmykInit(panel);
1739 break;
1740 case WMHSBModeColorPanel:
1741 hsbInit(panel);
1742 break;
1743 default:
1744 break;
1745 }
1746 panel->lastChanged = panel->mode;
1747
1748 WMSetButtonSelected(panel->magnifyBtn, False);
1749 break;
1750
1751 case MotionNotify:
1752 while (XPending(event.xmotion.display)) {
1753 XEvent ev;
1754 XPeekEvent(event.xmotion.display, &ev);
1755 if (ev.type == MotionNotify)
1756 XNextEvent(event.xmotion.display, &event);
1757 else
1758 break;
1759 }
1760
1761 /* Get a "dirty rectangle" */
1762 magnifyGetImageStored(panel,
1763 panel->magnifyGlass->x, panel->magnifyGlass->y,
1764 event.xmotion.x_root, event.xmotion.y_root);
1765
1766 /* Update coordinates */
1767 panel->magnifyGlass->x = event.xmotion.x_root;
1768 panel->magnifyGlass->y = event.xmotion.y_root;
1769
1770 /* Move view */
1771 W_MoveView(panel->magnifyGlass->view,
1772 panel->magnifyGlass->x - Cursor_x_hot, panel->magnifyGlass->y - Cursor_y_hot);
1773
1774 /* Put new image (with magn.) in view */
1775 pixmap = magnifyCreatePixmap(panel);
1776 if (pixmap != None) {
1777 /* Change the window background */
1778 XSetWindowBackgroundPixmap(scr->display,
1779 WMViewXID(panel->magnifyGlass->view), pixmap);
1780 /* Force an Expose (handled by X) */
1781 XClearWindow(scr->display, WMViewXID(panel->magnifyGlass->view));
1782 /* Synchronize the event queue, so the Expose is handled NOW */
1783 XFlush(scr->display);
6830b057 dan2004-10-12 21:28:27 +00001784#ifndef SHAPE
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +02001785 XFreePixmap(scr->display, pixmap);
d98f1fa6 kojima2000-01-06 21:56:39 +00001786#endif
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02001787 }
1788 break;
1789
1790 /* Try XQueryPointer for this !!! It returns windows that the pointer
1791 * is over. Note: We found this solving the invisible donkey cap bug
1792 */
1793#if 0 /* As it is impossible to make this work in all cases,
1794 * we consider it confusing. Therefore we disabled it.
1795 */
1796 case FocusOut: /* fall through */
1797 case FocusIn:
1798 /*
1799 * Color Panel window (panel->win) lost or received focus.
1800 * We need to update the pixmap in the magnifying glass.
1801 *
1802 * BUG Doesn't work with focus switches between two windows
1803 * if none of them is the color panel.
1804 */
1805 XUngrabPointer(scr->display, CurrentTime);
1806 W_UnmapView(panel->magnifyGlass->view);
1807
1808 magnifyInitialize(panel);
1809
1810 W_MapView(panel->magnifyGlass->view);
1811 XGrabPointer(scr->display, panel->magnifyGlass->view->window,
1812 True, PointerMotionMask | ButtonPressMask,
1813 GrabModeAsync, GrabModeAsync, scr->rootWin, magCursor, CurrentTime);
1814 break;
6830b057 dan2004-10-12 21:28:27 +00001815#endif
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02001816 default:
1817 WMHandleEvent(&event);
1818 break;
1819 } /* of switch */
1820 }
6830b057 dan2004-10-12 21:28:27 +00001821
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02001822 XUngrabPointer(scr->display, CurrentTime);
1823 XFreeCursor(scr->display, magCursor);
6830b057 dan2004-10-12 21:28:27 +00001824
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02001825 XFreePixmap(scr->display, panel->magnifyGlass->magPix);
1826 panel->magnifyGlass->magPix = None;
95e71d50 id1999-12-29 11:59:30 +00001827
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02001828 W_UnmapView(panel->magnifyGlass->view);
1829 W_DestroyView(panel->magnifyGlass->view);
1830 panel->magnifyGlass->view = NULL;
6830b057 dan2004-10-12 21:28:27 +00001831
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +02001832 wfree(panel->magnifyGlass);
e7495baf dan1999-02-17 11:06:40 +00001833}
1834
d98f1fa6 kojima2000-01-06 21:56:39 +00001835/****************** ColorWheel Functions ************************/
416e3a82 dan1999-01-25 19:06:50 +00001836
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +02001837static wheelMatrix *wheelCreateMatrix(unsigned int width, unsigned int height)
416e3a82 dan1999-01-25 19:06:50 +00001838{
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02001839 wheelMatrix *matrix = NULL;
1840 int i;
d98f1fa6 kojima2000-01-06 21:56:39 +00001841
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02001842 assert((width > 0) && (height > 0));
1843
1844 matrix = wmalloc(sizeof(wheelMatrix));
1845 memset(matrix, 0, sizeof(wheelMatrix));
1846 matrix->width = width;
1847 matrix->height = height;
1848
1849 for (i = 0; i < 3; i++) {
1850 matrix->data[i] = wmalloc(width * height * sizeof(unsigned char));
1851 }
1852
1853 return matrix;
1854}
1855
1856static void wheelDestroyMatrix(wheelMatrix * matrix)
1857{
1858 int i;
1859
1860 if (!matrix)
1861 return;
1862
1863 for (i = 0; i < 3; i++) {
1864 if (matrix->data[i])
1865 wfree(matrix->data[i]);
1866 }
1867 wfree(matrix);
1868}
1869
1870static void wheelInitMatrix(W_ColorPanel * panel)
1871{
1872 int i;
1873 int x, y;
1874 unsigned char *rp, *gp, *bp;
1875 CPColor cpColor;
1876 long ofs[4];
1877 int xcor, ycor;
1878 unsigned short sat;
1879 int dhue[4];
1880 const int cw_halfsize = (colorWheelSize + 4) / 2,
1881 cw_sqsize = (colorWheelSize + 4) * (colorWheelSize + 4), uchar_shift = getShift(sizeof(unsigned char));
6830b057 dan2004-10-12 21:28:27 +00001882
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02001883 if (!panel->wheelMtrx)
1884 return;
1885
1886 cpColor.hsv.value = 255;
1887 cpColor.set = cpHSV;
1888
1889 ofs[0] = -1;
1890 ofs[1] = -(colorWheelSize + 4);
1891
1892 /* offsets are counterclockwise (in triangles).
1893 *
1894 * ofs[0] ---->
1895 * _______________________________________
1896 * [1] |_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_| o
1897 * s |_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_| f
1898 * f |_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_| s
1899 * o | | | | | | | | | | | | | | | | | | | | | [3]
1900 *
1901 * <---- ofs[2]
1902 * ____
1903 * |\ /| <-- triangles
1904 * | \/ |
1905 * | /\ |
1906 * |/__\|
1907 */
1908
1909 for (y = 0; y < cw_halfsize; y++) {
1910 for (x = y; x < (colorWheelSize + 4 - y); x++) {
1911 /* (xcor, ycor) is (x,y) relative to center of matrix */
1912 xcor = 2 * x - 4 - colorWheelSize;
1913 ycor = 2 * y - 4 - colorWheelSize;
1914
1915 /* RColor.saturation is unsigned char and will wrap after 255 */
1916 sat = rint(255.0 * sqrt(xcor * xcor + ycor * ycor) / colorWheelSize);
1917
1918 cpColor.hsv.saturation = (unsigned char)sat;
1919
1920 ofs[0]++; /* top quarter of matrix */
1921 ofs[1] += colorWheelSize + 4; /* left quarter */
1922 ofs[2] = cw_sqsize - 1 - ofs[0]; /* bottom quarter */
1923 ofs[3] = cw_sqsize - 1 - ofs[1]; /* right quarter */
1924
1925 if (sat < 256) {
1926 if (xcor != 0)
1927 dhue[0] = rint(atan((double)ycor / (double)xcor) *
1928 (180.0 / M_PI)) + (xcor < 0 ? 180.0 : 0.0);
1929 else
1930 dhue[0] = 270;
1931
1932 dhue[0] = 360 - dhue[0]; /* Reverse direction of ColorWheel */
1933 dhue[1] = 270 - dhue[0] + (dhue[0] > 270 ? 360 : 0);
1934 dhue[2] = dhue[0] - 180 + (dhue[0] < 180 ? 360 : 0);
1935 dhue[3] = 90 - dhue[0] + (dhue[0] > 90 ? 360 : 0);
1936
1937 for (i = 0; i < 4; i++) {
1938 rp = panel->wheelMtrx->data[0] + (ofs[i] << uchar_shift);
1939 gp = panel->wheelMtrx->data[1] + (ofs[i] << uchar_shift);
1940 bp = panel->wheelMtrx->data[2] + (ofs[i] << uchar_shift);
1941
1942 cpColor.hsv.hue = dhue[i];
1943 convertCPColor(&cpColor);
1944
1945 *rp = (unsigned char)(cpColor.rgb.red);
1946 *gp = (unsigned char)(cpColor.rgb.green);
1947 *bp = (unsigned char)(cpColor.rgb.blue);
1948 }
1949 } else {
1950 for (i = 0; i < 4; i++) {
1951 rp = panel->wheelMtrx->data[0] + (ofs[i] << uchar_shift);
1952 gp = panel->wheelMtrx->data[1] + (ofs[i] << uchar_shift);
1953 bp = panel->wheelMtrx->data[2] + (ofs[i] << uchar_shift);
1954
1955 *rp = (unsigned char)0;
1956 *gp = (unsigned char)0;
1957 *bp = (unsigned char)0;
1958 }
1959 }
1960 }
1961
1962 ofs[0] += 2 * y + 1;
1963 ofs[1] += 1 - (colorWheelSize + 4) * (colorWheelSize + 4 - 1 - 2 * y);
1964 }
1965}
1966
1967static void wheelCalculateValues(W_ColorPanel * panel, int maxvalue)
1968{
1969 unsigned int i;
1970 unsigned int v;
1971
1972 for (i = 0; i < 256; i++) {
1973 /* We divide by 128 in advance, and check whether that number divides
1974 * by 2 properly. If not, we add one to round the number correctly
1975 */
1976 v = (i * maxvalue) >> 7;
1977 panel->wheelMtrx->values[i] = (unsigned char)((v >> 1) + (v & 0x01));
1978 }
1979}
1980
1981static void wheelRender(W_ColorPanel * panel)
1982{
1983 W_Screen *scr = WMWidgetScreen(panel->win);
1984 int x, y;
1985 RImage *image;
1986 unsigned char *ptr;
1987 RColor gray;
1988 unsigned long ofs = 0;
1989 /*unsigned char shift = getShift(sizeof(unsigned char)); */
1990
1991 image = RCreateImage(colorWheelSize + 4, colorWheelSize + 4, True);
1992 if (!image) {
1993 wwarning(_("Color Panel: Could not allocate memory"));
1994 return;
1995 }
1996
1997 ptr = image->data;
1998
1999 /* TODO Make this transparent istead of gray */
2000 gray.red = gray.blue = 0xae;
2001 gray.green = 0xaa;
2002
2003 for (y = 0; y < colorWheelSize + 4; y++) {
2004 for (x = 0; x < colorWheelSize + 4; x++) {
2005 if (wheelInsideColorWheel(panel, ofs)) {
2006 *(ptr++) =
2007 (unsigned char)(panel->wheelMtrx->values[panel->wheelMtrx->data[0][ofs]]);
2008 *(ptr++) =
2009 (unsigned char)(panel->wheelMtrx->values[panel->wheelMtrx->data[1][ofs]]);
2010 *(ptr++) =
2011 (unsigned char)(panel->wheelMtrx->values[panel->wheelMtrx->data[2][ofs]]);
2012 *(ptr++) = 0;
2013 } else {
2014 *(ptr++) = (unsigned char)(gray.red);
2015 *(ptr++) = (unsigned char)(gray.green);
2016 *(ptr++) = (unsigned char)(gray.blue);
2017 *(ptr++) = 255;
2018 }
2019 ofs++;
2020 }
2021 }
2022
2023 if (panel->wheelImg)
2024 XFreePixmap(scr->display, panel->wheelImg);
2025
2026 RConvertImage(scr->rcontext, image, &panel->wheelImg);
2027 RReleaseImage(image);
2028
2029 /* Check if backimage exists. If it doesn't, allocate and fill it */
2030 if (!panel->selectionBackImg) {
2031 panel->selectionBackImg = XCreatePixmap(scr->display,
2032 W_VIEW(panel->wheelFrm)->window, 4, 4, scr->depth);
2033 XCopyArea(scr->display, panel->wheelImg, panel->selectionBackImg,
2034 scr->copyGC, panel->colx - 2, panel->coly - 2, 4, 4, 0, 0);
2035 /* -2 is hot spot correction */
2036 }
416e3a82 dan1999-01-25 19:06:50 +00002037}
2038
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +02002039static Bool wheelInsideColorWheel(W_ColorPanel * panel, unsigned long ofs)
416e3a82 dan1999-01-25 19:06:50 +00002040{
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02002041 return ((panel->wheelMtrx->data[0][ofs] != 0) &&
2042 (panel->wheelMtrx->data[1][ofs] != 0) && (panel->wheelMtrx->data[2][ofs] != 0));
416e3a82 dan1999-01-25 19:06:50 +00002043}
2044
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +02002045static void wheelPaint(W_ColorPanel * panel)
416e3a82 dan1999-01-25 19:06:50 +00002046{
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +02002047 W_Screen *scr = WMWidgetScreen(panel->win);
6830b057 dan2004-10-12 21:28:27 +00002048
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02002049 XCopyArea(scr->display, panel->wheelImg, panel->wheelView->window,
2050 scr->copyGC, 0, 0, colorWheelSize + 4, colorWheelSize + 4, 0, 0);
6830b057 dan2004-10-12 21:28:27 +00002051
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02002052 /* Draw selection image */
2053 XCopyArea(scr->display, panel->selectionImg, panel->wheelView->window,
2054 scr->copyGC, 0, 0, 4, 4, panel->colx - 2, panel->coly - 2);
416e3a82 dan1999-01-25 19:06:50 +00002055}
2056
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +02002057static void wheelHandleEvents(XEvent * event, void *data)
416e3a82 dan1999-01-25 19:06:50 +00002058{
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +02002059 W_ColorPanel *panel = (W_ColorPanel *) data;
6830b057 dan2004-10-12 21:28:27 +00002060
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02002061 switch (event->type) {
2062 case Expose:
2063 if (event->xexpose.count != 0) /* TODO Improve */
2064 break;
2065 wheelPaint(panel);
2066 break;
2067 }
416e3a82 dan1999-01-25 19:06:50 +00002068}
2069
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +02002070static void wheelHandleActionEvents(XEvent * event, void *data)
416e3a82 dan1999-01-25 19:06:50 +00002071{
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +02002072 W_ColorPanel *panel = (W_ColorPanel *) data;
6830b057 dan2004-10-12 21:28:27 +00002073
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02002074 switch (event->type) {
2075 case ButtonPress:
2076 if (getPickerPart(panel, event->xbutton.x, event->xbutton.y) == COLORWHEEL_PART) {
6830b057 dan2004-10-12 21:28:27 +00002077
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02002078 panel->lastChanged = WMWheelModeColorPanel;
2079 panel->flags.dragging = 1;
6830b057 dan2004-10-12 21:28:27 +00002080
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02002081 wheelPositionSelection(panel, event->xbutton.x, event->xbutton.y);
2082 }
2083 break;
6830b057 dan2004-10-12 21:28:27 +00002084
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02002085 case ButtonRelease:
2086 panel->flags.dragging = 0;
2087 if (!panel->flags.continuous) {
2088 if (panel->action)
2089 (*panel->action) (panel, panel->clientData);
2090 }
2091 break;
6830b057 dan2004-10-12 21:28:27 +00002092
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02002093 case MotionNotify:
2094 if (panel->flags.dragging) {
2095 if (getPickerPart(panel, event->xmotion.x, event->xmotion.y) == COLORWHEEL_PART) {
2096 wheelPositionSelection(panel, event->xmotion.x, event->xmotion.y);
2097 } else
2098 wheelPositionSelectionOutBounds(panel, event->xmotion.x, event->xmotion.y);
2099 }
2100 break;
2101 }
416e3a82 dan1999-01-25 19:06:50 +00002102}
2103
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +02002104static int getPickerPart(W_ColorPanel * panel, int x, int y)
416e3a82 dan1999-01-25 19:06:50 +00002105{
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02002106 int lx, ly;
2107 unsigned long ofs;
6830b057 dan2004-10-12 21:28:27 +00002108
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02002109 lx = x;
2110 ly = y;
6830b057 dan2004-10-12 21:28:27 +00002111
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02002112 if (panel->mode == WMWheelModeColorPanel) {
2113 if ((lx >= 2) && (lx <= 2 + colorWheelSize) && (ly >= 2) && (ly <= 2 + colorWheelSize)) {
6830b057 dan2004-10-12 21:28:27 +00002114
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +02002115 ofs = ly * panel->wheelMtrx->width + lx;
6830b057 dan2004-10-12 21:28:27 +00002116
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02002117 if (wheelInsideColorWheel(panel, ofs))
2118 return COLORWHEEL_PART;
2119 }
2120 }
6830b057 dan2004-10-12 21:28:27 +00002121
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02002122 if (panel->mode == WMCustomPaletteModeColorPanel) {
2123 if ((lx >= 2) && (lx < customPaletteWidth - 2) && (ly >= 2) && (ly < customPaletteHeight - 2)) {
2124 return CUSTOMPALETTE_PART;
2125 }
2126 }
6830b057 dan2004-10-12 21:28:27 +00002127
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +02002128 return 0;
416e3a82 dan1999-01-25 19:06:50 +00002129}
2130
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +02002131static void wheelBrightnessSliderCallback(WMWidget * w, void *data)
416e3a82 dan1999-01-25 19:06:50 +00002132{
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +02002133 int value;
6830b057 dan2004-10-12 21:28:27 +00002134
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +02002135 W_ColorPanel *panel = (W_ColorPanel *) data;
6830b057 dan2004-10-12 21:28:27 +00002136
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +02002137 value = 255 - WMGetSliderValue(panel->wheelBrightnessS);
6830b057 dan2004-10-12 21:28:27 +00002138
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +02002139 wheelCalculateValues(panel, value);
6830b057 dan2004-10-12 21:28:27 +00002140
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02002141 if (panel->color.set == cpRGB) {
2142 convertCPColor(&panel->color);
2143 panel->color.set = cpHSV;
2144 }
6830b057 dan2004-10-12 21:28:27 +00002145
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +02002146 panel->color.hsv.value = value;
6830b057 dan2004-10-12 21:28:27 +00002147
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02002148 wheelRender(panel);
2149 wheelPaint(panel);
2150 wheelUpdateSelection(panel);
416e3a82 dan1999-01-25 19:06:50 +00002151}
2152
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +02002153static void wheelUpdateSelection(W_ColorPanel * panel)
416e3a82 dan1999-01-25 19:06:50 +00002154{
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +02002155 W_Screen *scr = WMWidgetScreen(panel->win);
6830b057 dan2004-10-12 21:28:27 +00002156
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02002157 updateSwatch(panel, panel->color);
2158 panel->lastChanged = WMWheelModeColorPanel;
6830b057 dan2004-10-12 21:28:27 +00002159
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02002160 /* Redraw color selector (and make a backup of the part it will cover) */
2161 XCopyArea(scr->display, panel->wheelImg, panel->selectionBackImg,
2162 scr->copyGC, panel->colx - 2, panel->coly - 2, 4, 4, 0, 0);
2163 /* "-2" is correction for hotspot location */
2164 XCopyArea(scr->display, panel->selectionImg, panel->wheelView->window,
2165 scr->copyGC, 0, 0, 4, 4, panel->colx - 2, panel->coly - 2);
2166 /* see above */
416e3a82 dan1999-01-25 19:06:50 +00002167}
2168
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +02002169static void wheelUndrawSelection(W_ColorPanel * panel)
416e3a82 dan1999-01-25 19:06:50 +00002170{
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +02002171 W_Screen *scr = WMWidgetScreen(panel->win);
6830b057 dan2004-10-12 21:28:27 +00002172
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02002173 XCopyArea(scr->display, panel->selectionBackImg, panel->wheelView->window,
2174 scr->copyGC, 0, 0, 4, 4, panel->colx - 2, panel->coly - 2);
2175 /* see above */
416e3a82 dan1999-01-25 19:06:50 +00002176}
2177
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +02002178static void wheelPositionSelection(W_ColorPanel * panel, int x, int y)
416e3a82 dan1999-01-25 19:06:50 +00002179{
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +02002180 unsigned long ofs = (y * panel->wheelMtrx->width) + x;
6830b057 dan2004-10-12 21:28:27 +00002181
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +02002182 panel->color.rgb.red = panel->wheelMtrx->values[panel->wheelMtrx->data[0][ofs]];
6830b057 dan2004-10-12 21:28:27 +00002183
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +02002184 panel->color.rgb.green = panel->wheelMtrx->values[panel->wheelMtrx->data[1][ofs]];
6830b057 dan2004-10-12 21:28:27 +00002185
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02002186 panel->color.rgb.blue = panel->wheelMtrx->values[panel->wheelMtrx->data[2][ofs]];
2187 panel->color.set = cpRGB;
6830b057 dan2004-10-12 21:28:27 +00002188
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +02002189 wheelUndrawSelection(panel);
6830b057 dan2004-10-12 21:28:27 +00002190
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02002191 panel->colx = x;
2192 panel->coly = y;
6830b057 dan2004-10-12 21:28:27 +00002193
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02002194 wheelUpdateSelection(panel);
2195 wheelUpdateBrightnessGradientFromLocation(panel);
416e3a82 dan1999-01-25 19:06:50 +00002196}
2197
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +02002198static void wheelPositionSelectionOutBounds(W_ColorPanel * panel, int x, int y)
416e3a82 dan1999-01-25 19:06:50 +00002199{
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02002200 int hue;
2201 int xcor, ycor;
2202 CPColor cpColor;
6830b057 dan2004-10-12 21:28:27 +00002203
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02002204 xcor = x * 2 - colorWheelSize - 4;
2205 ycor = y * 2 - colorWheelSize - 4;
6830b057 dan2004-10-12 21:28:27 +00002206
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02002207 panel->color.hsv.saturation = 255;
2208 panel->color.hsv.value = 255 - WMGetSliderValue(panel->wheelBrightnessS);
6830b057 dan2004-10-12 21:28:27 +00002209
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02002210 if (xcor != 0)
2211 hue = rint(atan(-(double)ycor / (double)xcor) * (180.0 / M_PI));
2212 else {
2213 if (ycor < 0)
2214 hue = 90;
2215 else
2216 hue = 270;
2217 }
d98f1fa6 kojima2000-01-06 21:56:39 +00002218
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02002219 if (xcor < 0)
2220 hue += 180;
d98f1fa6 kojima2000-01-06 21:56:39 +00002221
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02002222 if ((xcor > 0) && (ycor > 0))
2223 hue += 360;
6830b057 dan2004-10-12 21:28:27 +00002224
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02002225 panel->color.hsv.hue = hue;
2226 panel->color.set = cpHSV;
2227 convertCPColor(&panel->color);
6830b057 dan2004-10-12 21:28:27 +00002228
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +02002229 wheelUndrawSelection(panel);
6830b057 dan2004-10-12 21:28:27 +00002230
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02002231 panel->colx = 2 + rint((colorWheelSize * (1.0 + cos(panel->color.hsv.hue * (M_PI / 180.0)))) / 2.0);
2232 /* "+2" because of "colorWheelSize + 4" */
2233 panel->coly = 2 + rint((colorWheelSize * (1.0 + sin(-panel->color.hsv.hue * (M_PI / 180.0)))) / 2.0);
6830b057 dan2004-10-12 21:28:27 +00002234
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02002235 wheelUpdateSelection(panel);
2236 cpColor = panel->color;
2237 wheelUpdateBrightnessGradient(panel, cpColor);
416e3a82 dan1999-01-25 19:06:50 +00002238}
2239
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +02002240static void wheelUpdateBrightnessGradientFromLocation(W_ColorPanel * panel)
416e3a82 dan1999-01-25 19:06:50 +00002241{
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02002242 CPColor from;
2243 unsigned long ofs;
6830b057 dan2004-10-12 21:28:27 +00002244
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +02002245 ofs = panel->coly * panel->wheelMtrx->width + panel->colx;
6830b057 dan2004-10-12 21:28:27 +00002246
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02002247 from.rgb.red = panel->wheelMtrx->data[0][ofs];
2248 from.rgb.green = panel->wheelMtrx->data[1][ofs];
2249 from.rgb.blue = panel->wheelMtrx->data[2][ofs];
2250 from.set = cpRGB;
6830b057 dan2004-10-12 21:28:27 +00002251
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +02002252 wheelUpdateBrightnessGradient(panel, from);
416e3a82 dan1999-01-25 19:06:50 +00002253}
2254
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +02002255static void wheelUpdateBrightnessGradient(W_ColorPanel * panel, CPColor topColor)
416e3a82 dan1999-01-25 19:06:50 +00002256{
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02002257 RColor to;
2258 RImage *sliderImg;
2259 WMPixmap *sliderPxmp;
6830b057 dan2004-10-12 21:28:27 +00002260
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +02002261 to.red = to.green = to.blue = 0;
d98f1fa6 kojima2000-01-06 21:56:39 +00002262
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02002263 if (topColor.set == cpHSV)
2264 convertCPColor(&topColor);
6830b057 dan2004-10-12 21:28:27 +00002265
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02002266 sliderImg = RRenderGradient(16, 153, &(topColor.rgb), &to, RGRD_VERTICAL);
2267 sliderPxmp = WMCreatePixmapFromRImage(WMWidgetScreen(panel->win), sliderImg, 0);
2268 RReleaseImage(sliderImg);
2269 WMSetSliderImage(panel->wheelBrightnessS, sliderPxmp);
2270 WMReleasePixmap(sliderPxmp);
416e3a82 dan1999-01-25 19:06:50 +00002271}
2272
2273/****************** Grayscale Panel Functions ***************/
2274
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +02002275static void grayBrightnessSliderCallback(WMWidget * w, void *data)
416e3a82 dan1999-01-25 19:06:50 +00002276{
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02002277 CPColor cpColor;
2278 int value;
2279 char tmp[4];
2280 W_ColorPanel *panel = (W_ColorPanel *) data;
6830b057 dan2004-10-12 21:28:27 +00002281
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +02002282 value = WMGetSliderValue(panel->grayBrightnessS);
6830b057 dan2004-10-12 21:28:27 +00002283
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +02002284 sprintf(tmp, "%d", value);
6830b057 dan2004-10-12 21:28:27 +00002285
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02002286 WMSetTextFieldText(panel->grayBrightnessT, tmp);
2287 cpColor.rgb.red = cpColor.rgb.green = cpColor.rgb.blue = rint(2.55 * value);
2288 cpColor.set = cpRGB;
6830b057 dan2004-10-12 21:28:27 +00002289
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02002290 updateSwatch(panel, cpColor);
2291 panel->lastChanged = WMGrayModeColorPanel;
416e3a82 dan1999-01-25 19:06:50 +00002292}
2293
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +02002294static void grayPresetButtonCallback(WMWidget * w, void *data)
416e3a82 dan1999-01-25 19:06:50 +00002295{
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02002296 CPColor cpColor;
2297 char tmp[4];
2298 int value;
2299 int i = 0;
2300 W_ColorPanel *panel = (W_ColorPanel *) data;
6830b057 dan2004-10-12 21:28:27 +00002301
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02002302 while (i < 7) {
2303 if (w == panel->grayPresetBtn[i])
2304 break;
2305 i++;
2306 }
6830b057 dan2004-10-12 21:28:27 +00002307
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02002308 value = rint((100.0 * i) / 6.0);
2309 sprintf(tmp, "%d", value);
6830b057 dan2004-10-12 21:28:27 +00002310
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02002311 WMSetTextFieldText(panel->grayBrightnessT, tmp);
2312 cpColor.rgb.red = cpColor.rgb.green = cpColor.rgb.blue = rint((255.0 * i) / 6.0);
2313 cpColor.set = cpRGB;
6830b057 dan2004-10-12 21:28:27 +00002314
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +02002315 WMSetSliderValue(panel->grayBrightnessS, rint((100.0 * i) / 6.0));
6830b057 dan2004-10-12 21:28:27 +00002316
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02002317 updateSwatch(panel, cpColor);
2318 panel->lastChanged = WMGrayModeColorPanel;
416e3a82 dan1999-01-25 19:06:50 +00002319}
2320
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +02002321static void grayBrightnessTextFieldCallback(void *observerData, WMNotification * notification)
bedc2238 kojima1999-05-16 07:00:01 +00002322{
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02002323 CPColor cpColor;
2324 char tmp[4];
2325 int value;
2326 W_ColorPanel *panel = (W_ColorPanel *) observerData;
6830b057 dan2004-10-12 21:28:27 +00002327
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02002328 value = atoi(WMGetTextFieldText(panel->grayBrightnessT));
2329 if (value > 100)
2330 value = 100;
2331 if (value < 0)
2332 value = 0;
6830b057 dan2004-10-12 21:28:27 +00002333
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02002334 sprintf(tmp, "%d", value);
2335 WMSetTextFieldText(panel->grayBrightnessT, tmp);
2336 WMSetSliderValue(panel->grayBrightnessS, value);
6830b057 dan2004-10-12 21:28:27 +00002337
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02002338 cpColor.rgb.red = cpColor.rgb.green = cpColor.rgb.blue = rint((255.0 * value) / 100.0);
2339 cpColor.set = cpRGB;
6830b057 dan2004-10-12 21:28:27 +00002340
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02002341 updateSwatch(panel, cpColor);
2342 panel->lastChanged = WMGrayModeColorPanel;
416e3a82 dan1999-01-25 19:06:50 +00002343}
2344
2345/******************* RGB Panel Functions *****************/
2346
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +02002347static void rgbSliderCallback(WMWidget * w, void *data)
416e3a82 dan1999-01-25 19:06:50 +00002348{
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02002349 CPColor cpColor;
2350 int value[3];
2351 char tmp[4];
2352 W_ColorPanel *panel = (W_ColorPanel *) data;
6830b057 dan2004-10-12 21:28:27 +00002353
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02002354 value[0] = WMGetSliderValue(panel->rgbRedS);
2355 value[1] = WMGetSliderValue(panel->rgbGreenS);
2356 value[2] = WMGetSliderValue(panel->rgbBlueS);
6830b057 dan2004-10-12 21:28:27 +00002357
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02002358 sprintf(tmp, "%d", value[0]);
2359 WMSetTextFieldText(panel->rgbRedT, tmp);
2360 sprintf(tmp, "%d", value[1]);
2361 WMSetTextFieldText(panel->rgbGreenT, tmp);
2362 sprintf(tmp, "%d", value[2]);
2363 WMSetTextFieldText(panel->rgbBlueT, tmp);
6830b057 dan2004-10-12 21:28:27 +00002364
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02002365 cpColor.rgb.red = value[0];
2366 cpColor.rgb.green = value[1];
2367 cpColor.rgb.blue = value[2];
2368 cpColor.set = cpRGB;
6830b057 dan2004-10-12 21:28:27 +00002369
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02002370 updateSwatch(panel, cpColor);
2371 panel->lastChanged = WMRGBModeColorPanel;
416e3a82 dan1999-01-25 19:06:50 +00002372}
2373
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +02002374static void rgbTextFieldCallback(void *observerData, WMNotification * notification)
416e3a82 dan1999-01-25 19:06:50 +00002375{
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02002376 CPColor cpColor;
2377 int value[3];
2378 char tmp[4];
2379 int n;
2380 W_ColorPanel *panel = (W_ColorPanel *) observerData;
6830b057 dan2004-10-12 21:28:27 +00002381
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02002382 value[0] = atoi(WMGetTextFieldText(panel->rgbRedT));
2383 value[1] = atoi(WMGetTextFieldText(panel->rgbGreenT));
2384 value[2] = atoi(WMGetTextFieldText(panel->rgbBlueT));
6830b057 dan2004-10-12 21:28:27 +00002385
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02002386 for (n = 0; n < 3; n++) {
2387 if (value[n] > 255)
2388 value[n] = 255;
2389 if (value[n] < 0)
2390 value[n] = 0;
2391 }
6830b057 dan2004-10-12 21:28:27 +00002392
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02002393 sprintf(tmp, "%d", value[0]);
2394 WMSetTextFieldText(panel->rgbRedT, tmp);
2395 sprintf(tmp, "%d", value[1]);
2396 WMSetTextFieldText(panel->rgbGreenT, tmp);
2397 sprintf(tmp, "%d", value[2]);
2398 WMSetTextFieldText(panel->rgbBlueT, tmp);
6830b057 dan2004-10-12 21:28:27 +00002399
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02002400 WMSetSliderValue(panel->rgbRedS, value[0]);
2401 WMSetSliderValue(panel->rgbGreenS, value[1]);
2402 WMSetSliderValue(panel->rgbBlueS, value[2]);
6830b057 dan2004-10-12 21:28:27 +00002403
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02002404 cpColor.rgb.red = value[0];
2405 cpColor.rgb.green = value[1];
2406 cpColor.rgb.blue = value[2];
2407 cpColor.set = cpRGB;
6830b057 dan2004-10-12 21:28:27 +00002408
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02002409 updateSwatch(panel, cpColor);
2410 panel->lastChanged = WMRGBModeColorPanel;
416e3a82 dan1999-01-25 19:06:50 +00002411}
2412
416e3a82 dan1999-01-25 19:06:50 +00002413/******************* CMYK Panel Functions *****************/
2414
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +02002415static void cmykSliderCallback(WMWidget * w, void *data)
416e3a82 dan1999-01-25 19:06:50 +00002416{
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02002417 CPColor cpColor;
2418 int value[4];
2419 char tmp[4];
2420 W_ColorPanel *panel = (W_ColorPanel *) data;
2421 double scale;
6830b057 dan2004-10-12 21:28:27 +00002422
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02002423 value[0] = WMGetSliderValue(panel->cmykCyanS);
2424 value[1] = WMGetSliderValue(panel->cmykMagentaS);
2425 value[2] = WMGetSliderValue(panel->cmykYellowS);
2426 value[3] = WMGetSliderValue(panel->cmykBlackS);
6830b057 dan2004-10-12 21:28:27 +00002427
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02002428 sprintf(tmp, "%d", value[0]);
2429 WMSetTextFieldText(panel->cmykCyanT, tmp);
2430 sprintf(tmp, "%d", value[1]);
2431 WMSetTextFieldText(panel->cmykMagentaT, tmp);
2432 sprintf(tmp, "%d", value[2]);
2433 WMSetTextFieldText(panel->cmykYellowT, tmp);
2434 sprintf(tmp, "%d", value[3]);
2435 WMSetTextFieldText(panel->cmykBlackT, tmp);
6830b057 dan2004-10-12 21:28:27 +00002436
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02002437 scale = 2.55 * (1.0 - (value[3] / 100.0));
2438 cpColor.rgb.red = rint((100.0 - value[0]) * scale);
2439 cpColor.rgb.green = rint((100.0 - value[1]) * scale);
2440 cpColor.rgb.blue = rint((100.0 - value[2]) * scale);
2441 cpColor.set = cpRGB;
6830b057 dan2004-10-12 21:28:27 +00002442
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02002443 updateSwatch(panel, cpColor);
2444 panel->lastChanged = WMCMYKModeColorPanel;
416e3a82 dan1999-01-25 19:06:50 +00002445}
2446
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +02002447static void cmykTextFieldCallback(void *observerData, WMNotification * notification)
416e3a82 dan1999-01-25 19:06:50 +00002448{
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02002449 CPColor cpColor;
2450 int value[4];
2451 char tmp[4];
2452 int n;
2453 double scale;
2454 W_ColorPanel *panel = (W_ColorPanel *) observerData;
6830b057 dan2004-10-12 21:28:27 +00002455
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02002456 value[0] = atoi(WMGetTextFieldText(panel->cmykCyanT));
2457 value[1] = atoi(WMGetTextFieldText(panel->cmykMagentaT));
2458 value[2] = atoi(WMGetTextFieldText(panel->cmykYellowT));
2459 value[3] = atoi(WMGetTextFieldText(panel->cmykBlackT));
6830b057 dan2004-10-12 21:28:27 +00002460
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02002461 for (n = 0; n < 4; n++) {
2462 if (value[n] > 100)
2463 value[n] = 100;
2464 if (value[n] < 0)
2465 value[n] = 0;
2466 }
6830b057 dan2004-10-12 21:28:27 +00002467
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02002468 sprintf(tmp, "%d", value[0]);
2469 WMSetTextFieldText(panel->cmykCyanT, tmp);
6830b057 dan2004-10-12 21:28:27 +00002470
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02002471 sprintf(tmp, "%d", value[1]);
2472 WMSetTextFieldText(panel->cmykMagentaT, tmp);
6830b057 dan2004-10-12 21:28:27 +00002473
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02002474 sprintf(tmp, "%d", value[2]);
2475 WMSetTextFieldText(panel->cmykYellowT, tmp);
95e71d50 id1999-12-29 11:59:30 +00002476
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02002477 sprintf(tmp, "%d", value[3]);
2478 WMSetTextFieldText(panel->cmykBlackT, tmp);
6830b057 dan2004-10-12 21:28:27 +00002479
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02002480 WMSetSliderValue(panel->cmykCyanS, value[0]);
2481 WMSetSliderValue(panel->cmykMagentaS, value[1]);
2482 WMSetSliderValue(panel->cmykYellowS, value[2]);
2483 WMSetSliderValue(panel->cmykBlackS, value[3]);
6830b057 dan2004-10-12 21:28:27 +00002484
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02002485 scale = 2.55 * (1.0 - (value[3] / 100.0));
2486 cpColor.rgb.red = rint((100.0 - value[0]) * scale);
2487 cpColor.rgb.green = rint((100.0 - value[1]) * scale);
2488 cpColor.rgb.blue = rint((100.0 - value[2]) * scale);
2489 cpColor.set = cpRGB;
6830b057 dan2004-10-12 21:28:27 +00002490
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02002491 updateSwatch(panel, cpColor);
2492 panel->lastChanged = WMCMYKModeColorPanel;
416e3a82 dan1999-01-25 19:06:50 +00002493}
2494
2495/********************** HSB Panel Functions ***********************/
2496
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02002497static void hsbSliderCallback(WMWidget * w, void *data)
2498{
2499 CPColor cpColor;
2500 int value[3];
2501 char tmp[4];
2502 W_ColorPanel *panel = (W_ColorPanel *) data;
6830b057 dan2004-10-12 21:28:27 +00002503
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02002504 value[0] = WMGetSliderValue(panel->hsbHueS);
2505 value[1] = WMGetSliderValue(panel->hsbSaturationS);
2506 value[2] = WMGetSliderValue(panel->hsbBrightnessS);
2507
2508 sprintf(tmp, "%d", value[0]);
2509 WMSetTextFieldText(panel->hsbHueT, tmp);
2510 sprintf(tmp, "%d", value[1]);
2511 WMSetTextFieldText(panel->hsbSaturationT, tmp);
2512 sprintf(tmp, "%d", value[2]);
2513 WMSetTextFieldText(panel->hsbBrightnessT, tmp);
6830b057 dan2004-10-12 21:28:27 +00002514
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02002515 cpColor.hsv.hue = value[0];
2516 cpColor.hsv.saturation = value[1] * 2.55;
2517 cpColor.hsv.value = value[2] * 2.55;
2518 cpColor.set = cpHSV;
6830b057 dan2004-10-12 21:28:27 +00002519
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +02002520 convertCPColor(&cpColor);
6830b057 dan2004-10-12 21:28:27 +00002521
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02002522 panel->lastChanged = WMHSBModeColorPanel;
2523 updateSwatch(panel, cpColor);
6830b057 dan2004-10-12 21:28:27 +00002524
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02002525 if (w != panel->hsbBrightnessS)
2526 hsbUpdateBrightnessGradient(panel);
2527 if (w != panel->hsbSaturationS)
2528 hsbUpdateSaturationGradient(panel);
2529 if (w != panel->hsbHueS)
2530 hsbUpdateHueGradient(panel);
416e3a82 dan1999-01-25 19:06:50 +00002531}
2532
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +02002533static void hsbTextFieldCallback(void *observerData, WMNotification * notification)
416e3a82 dan1999-01-25 19:06:50 +00002534{
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02002535 CPColor cpColor;
2536 int value[3];
2537 char tmp[4];
2538 int n;
2539 W_ColorPanel *panel = (W_ColorPanel *) observerData;
2540
2541 value[0] = atoi(WMGetTextFieldText(panel->hsbHueT));
2542 value[1] = atoi(WMGetTextFieldText(panel->hsbSaturationT));
2543 value[2] = atoi(WMGetTextFieldText(panel->hsbBrightnessT));
2544
2545 if (value[0] > 359)
2546 value[0] = 359;
2547 if (value[0] < 0)
2548 value[0] = 0;
416e3a82 dan1999-01-25 19:06:50 +00002549
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02002550 for (n = 1; n < 3; n++) {
2551 if (value[n] > 100)
2552 value[n] = 100;
2553 if (value[n] < 0)
2554 value[n] = 0;
2555 }
6830b057 dan2004-10-12 21:28:27 +00002556
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02002557 sprintf(tmp, "%d", value[0]);
2558 WMSetTextFieldText(panel->hsbHueT, tmp);
2559 sprintf(tmp, "%d", value[1]);
2560 WMSetTextFieldText(panel->hsbSaturationT, tmp);
2561 sprintf(tmp, "%d", value[2]);
2562 WMSetTextFieldText(panel->hsbBrightnessT, tmp);
2563
2564 WMSetSliderValue(panel->hsbHueS, value[0]);
2565 WMSetSliderValue(panel->hsbSaturationS, value[1]);
2566 WMSetSliderValue(panel->hsbBrightnessS, value[2]);
2567
2568 cpColor.hsv.hue = value[0];
2569 cpColor.hsv.saturation = value[1] * 2.55;
2570 cpColor.hsv.value = value[2] * 2.55;
2571 cpColor.set = cpHSV;
2572
2573 convertCPColor(&cpColor);
2574
2575 panel->lastChanged = WMHSBModeColorPanel;
2576 updateSwatch(panel, cpColor);
2577
2578 hsbUpdateBrightnessGradient(panel);
2579 hsbUpdateSaturationGradient(panel);
2580 hsbUpdateHueGradient(panel);
2581}
6830b057 dan2004-10-12 21:28:27 +00002582
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02002583static void hsbUpdateBrightnessGradient(W_ColorPanel * panel)
2584{
2585 W_Screen *scr = WMWidgetScreen(panel->win);
2586 RColor from;
2587 CPColor to;
2588 RImage *sliderImg;
2589 WMPixmap *sliderPxmp;
2590
2591 from.red = from.green = from.blue = 0;
2592 to.hsv = panel->color.hsv;
2593 to.hsv.value = 255;
2594 to.set = cpHSV;
6830b057 dan2004-10-12 21:28:27 +00002595
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +02002596 convertCPColor(&to);
d98f1fa6 kojima2000-01-06 21:56:39 +00002597
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02002598 sliderImg = RRenderGradient(141, 16, &from, &(to.rgb), RGRD_HORIZONTAL);
2599 sliderPxmp = WMCreatePixmapFromRImage(scr, sliderImg, 0);
2600 RReleaseImage(sliderImg);
6830b057 dan2004-10-12 21:28:27 +00002601
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02002602 if (sliderPxmp)
2603 W_PaintText(W_VIEW(panel->hsbBrightnessS), sliderPxmp->pixmap,
2604 panel->font12, 2, 0, 100, WALeft, scr->white,
2605 False, _("Brightness"), strlen(_("Brightness")));
2606 else
2607 wwarning(_("Color Panel: Could not allocate memory"));
6830b057 dan2004-10-12 21:28:27 +00002608
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02002609 WMSetSliderImage(panel->hsbBrightnessS, sliderPxmp);
2610 WMReleasePixmap(sliderPxmp);
416e3a82 dan1999-01-25 19:06:50 +00002611}
2612
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02002613static void hsbUpdateSaturationGradient(W_ColorPanel * panel)
2614{
2615 W_Screen *scr = WMWidgetScreen(panel->win);
2616 CPColor from;
2617 CPColor to;
2618 RImage *sliderImg;
2619 WMPixmap *sliderPxmp;
2620
2621 from.hsv = panel->color.hsv;
2622 from.hsv.saturation = 0;
2623 from.set = cpHSV;
2624 convertCPColor(&from);
2625
2626 to.hsv = panel->color.hsv;
2627 to.hsv.saturation = 255;
2628 to.set = cpHSV;
2629 convertCPColor(&to);
416e3a82 dan1999-01-25 19:06:50 +00002630
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02002631 sliderImg = RRenderGradient(141, 16, &(from.rgb), &(to.rgb), RGRD_HORIZONTAL);
2632 sliderPxmp = WMCreatePixmapFromRImage(scr, sliderImg, 0);
2633 RReleaseImage(sliderImg);
2634
2635 if (sliderPxmp)
2636 W_PaintText(W_VIEW(panel->hsbSaturationS), sliderPxmp->pixmap,
2637 panel->font12, 2, 0, 100, WALeft,
2638 from.hsv.value < 128 ? scr->white : scr->black, False,
2639 _("Saturation"), strlen(_("Saturation")));
2640 else
2641 wwarning(_("Color Panel: Could not allocate memory"));
2642
2643 WMSetSliderImage(panel->hsbSaturationS, sliderPxmp);
2644 WMReleasePixmap(sliderPxmp);
2645}
2646
2647static void hsbUpdateHueGradient(W_ColorPanel * panel)
416e3a82 dan1999-01-25 19:06:50 +00002648{
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02002649 W_Screen *scr = WMWidgetScreen(panel->win);
2650 RColor **colors = NULL;
2651 RHSVColor hsvcolor;
2652 RImage *sliderImg;
2653 WMPixmap *sliderPxmp;
2654 int i;
6830b057 dan2004-10-12 21:28:27 +00002655
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +02002656 hsvcolor = panel->color.hsv;
6830b057 dan2004-10-12 21:28:27 +00002657
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02002658 colors = wmalloc(sizeof(RColor *) * (8));
2659 for (i = 0; i < 7; i++) {
2660 hsvcolor.hue = (360 * i) / 6;
2661 colors[i] = wmalloc(sizeof(RColor));
2662 RHSVtoRGB(&hsvcolor, colors[i]);
2663 }
2664 colors[7] = NULL;
6830b057 dan2004-10-12 21:28:27 +00002665
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02002666 sliderImg = RRenderMultiGradient(141, 16, colors, RGRD_HORIZONTAL);
2667 sliderPxmp = WMCreatePixmapFromRImage(scr, sliderImg, 0);
2668 RReleaseImage(sliderImg);
6830b057 dan2004-10-12 21:28:27 +00002669
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02002670 if (sliderPxmp)
2671 W_PaintText(W_VIEW(panel->hsbHueS), sliderPxmp->pixmap,
2672 panel->font12, 2, 0, 100, WALeft,
2673 hsvcolor.value < 128 ? scr->white : scr->black, False, _("Hue"), strlen(_("Hue")));
2674 else
2675 wwarning(_("Color Panel: Could not allocate memory"));
6830b057 dan2004-10-12 21:28:27 +00002676
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02002677 WMSetSliderImage(panel->hsbHueS, sliderPxmp);
2678 WMReleasePixmap(sliderPxmp);
6830b057 dan2004-10-12 21:28:27 +00002679
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02002680 for (i = 0; i < 7; i++)
2681 wfree(colors[i]);
6830b057 dan2004-10-12 21:28:27 +00002682
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +02002683 wfree(colors);
416e3a82 dan1999-01-25 19:06:50 +00002684}
2685
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +02002686/*************** Custom Palette Functions ****************/
416e3a82 dan1999-01-25 19:06:50 +00002687
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +02002688static void customRenderSpectrum(W_ColorPanel * panel)
416e3a82 dan1999-01-25 19:06:50 +00002689{
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02002690 RImage *spectrum;
2691 int x, y;
2692 unsigned char *ptr;
2693 CPColor cpColor;
6830b057 dan2004-10-12 21:28:27 +00002694
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +02002695 spectrum = RCreateImage(SPECTRUM_WIDTH, SPECTRUM_HEIGHT, False);
6830b057 dan2004-10-12 21:28:27 +00002696
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +02002697 ptr = spectrum->data;
6830b057 dan2004-10-12 21:28:27 +00002698
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02002699 for (y = 0; y < SPECTRUM_HEIGHT; y++) {
2700 cpColor.hsv.hue = y;
2701 cpColor.hsv.saturation = 0;
2702 cpColor.hsv.value = 255;
2703 cpColor.set = cpHSV;
6830b057 dan2004-10-12 21:28:27 +00002704
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02002705 for (x = 0; x < SPECTRUM_WIDTH; x++) {
2706 convertCPColor(&cpColor);
6830b057 dan2004-10-12 21:28:27 +00002707
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02002708 *(ptr++) = (unsigned char)cpColor.rgb.red;
2709 *(ptr++) = (unsigned char)cpColor.rgb.green;
2710 *(ptr++) = (unsigned char)cpColor.rgb.blue;
6830b057 dan2004-10-12 21:28:27 +00002711
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02002712 if (x < (SPECTRUM_WIDTH / 2))
2713 cpColor.hsv.saturation++;
416e3a82 dan1999-01-25 19:06:50 +00002714
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02002715 if (x > (SPECTRUM_WIDTH / 2))
2716 cpColor.hsv.value--;
2717 }
2718 }
2719 if (panel->customPaletteImg) {
2720 RReleaseImage(panel->customPaletteImg);
2721 panel->customPaletteImg = NULL;
2722 }
2723 panel->customPaletteImg = spectrum;
2724}
416e3a82 dan1999-01-25 19:06:50 +00002725
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +02002726static void customSetPalette(W_ColorPanel * panel)
416e3a82 dan1999-01-25 19:06:50 +00002727{
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02002728 W_Screen *scr = WMWidgetScreen(panel->win);
2729 RImage *scaledImg;
2730 Pixmap image;
2731 int item;
6830b057 dan2004-10-12 21:28:27 +00002732
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02002733 image = XCreatePixmap(scr->display, W_DRAWABLE(scr), customPaletteWidth, customPaletteHeight, scr->depth);
2734 scaledImg = RScaleImage(panel->customPaletteImg, customPaletteWidth, customPaletteHeight);
2735 RConvertImage(scr->rcontext, scaledImg, &image);
2736 RReleaseImage(scaledImg);
6830b057 dan2004-10-12 21:28:27 +00002737
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02002738 XCopyArea(scr->display, image, panel->customPaletteContentView->window,
2739 scr->copyGC, 0, 0, customPaletteWidth, customPaletteHeight, 0, 0);
6830b057 dan2004-10-12 21:28:27 +00002740
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02002741 /* Check backimage exists. If it doesn't, allocate and fill it */
2742 if (!panel->selectionBackImg) {
2743 panel->selectionBackImg = XCreatePixmap(scr->display,
2744 panel->customPaletteContentView->window, 4, 4, scr->depth);
2745 }
6830b057 dan2004-10-12 21:28:27 +00002746
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02002747 XCopyArea(scr->display, image, panel->selectionBackImg, scr->copyGC,
2748 panel->palx - 2, panel->paly - 2, 4, 4, 0, 0);
2749 XCopyArea(scr->display, panel->selectionImg,
2750 panel->customPaletteContentView->window, scr->copyGC, 0, 0, 4, 4,
2751 panel->palx - 2, panel->paly - 2);
2752 XFreePixmap(scr->display, image);
6830b057 dan2004-10-12 21:28:27 +00002753
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02002754 panel->palXRatio = (double)(panel->customPaletteImg->width) / (double)(customPaletteWidth);
2755 panel->palYRatio = (double)(panel->customPaletteImg->height) / (double)(customPaletteHeight);
6830b057 dan2004-10-12 21:28:27 +00002756
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +02002757 item = WMGetPopUpButtonSelectedItem(panel->customPaletteHistoryBtn);
416e3a82 dan1999-01-25 19:06:50 +00002758}
2759
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +02002760static void customPalettePositionSelection(W_ColorPanel * panel, int x, int y)
416e3a82 dan1999-01-25 19:06:50 +00002761{
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02002762 W_Screen *scr = WMWidgetScreen(panel->win);
2763 unsigned long ofs;
6830b057 dan2004-10-12 21:28:27 +00002764
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02002765 /* undraw selection */
2766 XCopyArea(scr->display, panel->selectionBackImg,
2767 panel->customPaletteContentView->window, scr->copyGC, 0, 0, 4, 4,
2768 panel->palx - 2, panel->paly - 2);
416e3a82 dan1999-01-25 19:06:50 +00002769
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02002770 panel->palx = x;
2771 panel->paly = y;
416e3a82 dan1999-01-25 19:06:50 +00002772
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +02002773 ofs = (rint(x * panel->palXRatio) + rint(y * panel->palYRatio) * panel->customPaletteImg->width) * 3;
6830b057 dan2004-10-12 21:28:27 +00002774
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02002775 panel->color.rgb.red = panel->customPaletteImg->data[ofs];
2776 panel->color.rgb.green = panel->customPaletteImg->data[ofs + 1];
2777 panel->color.rgb.blue = panel->customPaletteImg->data[ofs + 2];
2778 panel->color.set = cpRGB;
6830b057 dan2004-10-12 21:28:27 +00002779
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02002780 updateSwatch(panel, panel->color);
2781 panel->lastChanged = WMCustomPaletteModeColorPanel;
6830b057 dan2004-10-12 21:28:27 +00002782
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02002783 /* Redraw color selector (and make a backup of the part it will cover) */
2784 XCopyArea(scr->display, panel->customPaletteContentView->window, panel->selectionBackImg, scr->copyGC, panel->palx - 2, panel->paly - 2, 4, 4, 0, 0); /* "-2" is correction for hotspot location */
2785 XCopyArea(scr->display, panel->selectionImg, panel->customPaletteContentView->window, scr->copyGC, 0, 0, 4, 4, panel->palx - 2, panel->paly - 2); /* see above */
2786}
6830b057 dan2004-10-12 21:28:27 +00002787
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02002788static void customPalettePositionSelectionOutBounds(W_ColorPanel * panel, int x, int y)
2789{
2790 if (x < 2)
2791 x = 2;
2792 if (y < 2)
2793 y = 2;
2794 if (x >= customPaletteWidth)
2795 x = customPaletteWidth - 2;
2796 if (y >= customPaletteHeight)
2797 y = customPaletteHeight - 2;
2798
2799 customPalettePositionSelection(panel, x, y);
2800}
2801
2802static void customPaletteHandleEvents(XEvent * event, void *data)
2803{
2804 W_ColorPanel *panel = (W_ColorPanel *) data;
2805
2806 switch (event->type) {
2807 case Expose:
2808 if (event->xexpose.count != 0) /* TODO Improve. */
2809 break;
2810 customSetPalette(panel);
2811 break;
2812 }
2813}
2814
2815static void customPaletteHandleActionEvents(XEvent * event, void *data)
2816{
2817 W_ColorPanel *panel = (W_ColorPanel *) data;
2818 int x, y;
2819
2820 switch (event->type) {
2821 case ButtonPress:
2822 x = event->xbutton.x;
2823 y = event->xbutton.y;
2824
2825 if (getPickerPart(panel, x, y) == CUSTOMPALETTE_PART) {
2826 panel->flags.dragging = 1;
2827 customPalettePositionSelection(panel, x, y);
2828 }
2829 break;
2830
2831 case ButtonRelease:
2832 panel->flags.dragging = 0;
2833 if (!panel->flags.continuous) {
2834 if (panel->action)
2835 (*panel->action) (panel, panel->clientData);
2836 }
2837 break;
2838
2839 case MotionNotify:
2840 x = event->xmotion.x;
2841 y = event->xmotion.y;
2842
2843 if (panel->flags.dragging) {
2844 if (getPickerPart(panel, x, y) == CUSTOMPALETTE_PART) {
2845 customPalettePositionSelection(panel, x, y);
2846 } else
2847 customPalettePositionSelectionOutBounds(panel, x, y);
2848 }
2849 break;
2850 }
2851}
6830b057 dan2004-10-12 21:28:27 +00002852
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02002853static void customPaletteMenuCallback(WMWidget * w, void *data)
2854{
2855 W_ColorPanel *panel = (W_ColorPanel *) data;
2856 int item = WMGetPopUpButtonSelectedItem(panel->customPaletteMenuBtn);
2857
2858 switch (item) {
2859 case CPmenuNewFromFile:
2860 customPaletteMenuNewFromFile(panel);
2861 break;
2862 case CPmenuRename:
2863 customPaletteMenuRename(panel);
2864 break;
2865 case CPmenuRemove:
2866 customPaletteMenuRemove(panel);
2867 break;
2868 case CPmenuCopy:
2869 break;
2870 case CPmenuNewFromClipboard:
2871 break;
2872 }
2873}
2874
2875static void customPaletteMenuNewFromFile(W_ColorPanel * panel)
2876{
2877 W_Screen *scr = WMWidgetScreen(panel->win);
2878 WMOpenPanel *browseP;
2879 char *filepath;
2880 char *filename = NULL;
2881 char *spath;
2882 char *tmp;
2883 int i;
2884 RImage *tmpImg = NULL;
2885
2886 if ((!panel->lastBrowseDir) || (strcmp(panel->lastBrowseDir, "\0") == 0))
2887 spath = wexpandpath(wgethomedir());
2888 else
2889 spath = wexpandpath(panel->lastBrowseDir);
2890
2891 browseP = WMGetOpenPanel(scr);
2892 WMSetFilePanelCanChooseDirectories(browseP, 0);
2893 WMSetFilePanelCanChooseFiles(browseP, 1);
2894
2895 /* Get a filename */
2896 if (WMRunModalFilePanelForDirectory(browseP, panel->win, spath,
2897 _("Open Palette"), RSupportedFileFormats())) {
2898 filepath = WMGetFilePanelFileName(browseP);
2899
2900 /* Get seperation position between path and filename */
2901 i = strrchr(filepath, '/') - filepath + 1;
2902 if (i > strlen(filepath))
2903 i = strlen(filepath);
2904
2905 /* Store last browsed path */
2906 if (panel->lastBrowseDir)
2907 wfree(panel->lastBrowseDir);
2908 panel->lastBrowseDir = wmalloc((i + 1) * sizeof(char));
2909 strncpy(panel->lastBrowseDir, filepath, i);
2910 panel->lastBrowseDir[i] = '\0';
2911
2912 /* Get filename from path */
2913 filename = wstrdup(filepath + i);
2914
2915 /* Check for duplicate files, and rename it if there are any */
2916 tmp = wstrconcat(panel->configurationPath, filename);
2917 while (access(tmp, F_OK) == 0) {
2918 char *newName;
2919
2920 wfree(tmp);
2921
2922 newName = generateNewFilename(filename);
2923 wfree(filename);
2924 filename = newName;
2925
2926 tmp = wstrconcat(panel->configurationPath, filename);
2927 }
2928 wfree(tmp);
2929
2930 /* Copy image to $(gnustepdir)/Library/Colors/ &
2931 * Add filename to history menu */
2932 if (fetchFile(panel->configurationPath, filepath, filename) == 0) {
2933
2934 /* filepath is a "local" path now the file has been copied */
2935 wfree(filepath);
2936 filepath = wstrconcat(panel->configurationPath, filename);
2937
2938 /* load the image & add menu entries */
2939 tmpImg = RLoadImage(scr->rcontext, filepath, 0);
2940 if (tmpImg) {
2941 if (panel->customPaletteImg)
2942 RReleaseImage(panel->customPaletteImg);
2943 panel->customPaletteImg = tmpImg;
2944
2945 customSetPalette(panel);
2946 WMAddPopUpButtonItem(panel->customPaletteHistoryBtn, filename);
2947
2948 panel->currentPalette =
2949 WMGetPopUpButtonNumberOfItems(panel->customPaletteHistoryBtn) - 1;
2950
2951 WMSetPopUpButtonSelectedItem(panel->customPaletteHistoryBtn,
2952 panel->currentPalette);
2953 }
2954 } else {
2955 tmp = wstrconcat(panel->configurationPath, filename);
2956
2957 i = remove(tmp); /* Delete the file, it doesn't belong here */
2958 WMRunAlertPanel(scr, panel->win, _("File Error"),
2959 _("Invalid file format !"), _("OK"), NULL, NULL);
2960 if (i != 0) {
2961 wsyserror(_("can't remove file %s"), tmp);
2962 WMRunAlertPanel(scr, panel->win, _("File Error"),
2963 _("Couldn't remove file from Configuration Directory !"),
2964 _("OK"), NULL, NULL);
2965 }
2966 wfree(tmp);
2967 }
2968 wfree(filepath);
2969 wfree(filename);
2970 }
2971 WMFreeFilePanel(browseP);
2972
2973 wfree(spath);
2974}
2975
2976static void customPaletteMenuRename(W_ColorPanel * panel)
2977{
2978 W_Screen *scr = WMWidgetScreen(panel->win);
2979 char *toName = NULL;
2980 char *fromName;
2981 char *toPath, *fromPath;
2982 int item;
2983 int index;
2984
2985 item = WMGetPopUpButtonSelectedItem(panel->customPaletteHistoryBtn);
2986 fromName = WMGetPopUpButtonItem(panel->customPaletteHistoryBtn, item);
2987
2988 toName = WMRunInputPanel(scr, panel->win, _("Rename"), _("Rename palette to:"),
2989 fromName, _("OK"), _("Cancel"));
2990
2991 if (toName) {
2992
2993 /* As some people do certain stupid things... */
2994 if (strcmp(toName, fromName) == 0) {
2995 wfree(toName);
2996 return;
2997 }
2998
2999 /* For normal people */
3000 fromPath = wstrconcat(panel->configurationPath, fromName);
3001 toPath = wstrconcat(panel->configurationPath, toName);
3002
3003 if (access(toPath, F_OK) == 0) {
3004 /* Careful, this palette exists already */
3005 if (WMRunAlertPanel(scr, panel->win, _("Warning"),
3006 _("Palette already exists !\n\nOverwrite ?"), _("No"), _("Yes"),
3007 NULL) == 1) {
3008 /* "No" = 0, "Yes" = 1 */
3009 int items = WMGetPopUpButtonNumberOfItems(panel->customPaletteHistoryBtn);
3010
3011 remove(toPath);
3012
3013 /* Remove from History list too */
3014 index = 1;
3015 while ((index < items)
3016 &&
3017 (strcmp(WMGetPopUpButtonItem(panel->customPaletteHistoryBtn, index), toName)
3018 != 0))
3019 index++;
3020
3021 if (index < items) {
3022 WMRemovePopUpButtonItem(panel->customPaletteHistoryBtn, index);
3023 if (index < item)
3024 item--;
3025 }
3026
3027 } else {
3028 wfree(fromPath);
3029 wfree(toName);
3030 wfree(toPath);
3031
3032 return;
3033 }
3034 }
3035
3036 if (rename(fromPath, toPath) != 0)
3037 wsyserror(_("Couldn't rename palette %s to %s\n"), fromName, toName);
3038 else {
3039 WMRemovePopUpButtonItem(panel->customPaletteHistoryBtn, item);
3040 WMInsertPopUpButtonItem(panel->customPaletteHistoryBtn, item, toName);
3041
3042 WMSetPopUpButtonSelectedItem(panel->customPaletteHistoryBtn, item);
3043 }
3044 wfree(fromPath);
3045 wfree(toPath);
3046 wfree(toName);
3047 }
3048}
3049
3050static void customPaletteMenuRemove(W_ColorPanel * panel)
3051{
3052 W_Screen *scr = WMWidgetScreen(panel->win);
3053 char *text;
3054 char *tmp;
3055 int choice;
3056 int item;
3057
3058 item = WMGetPopUpButtonSelectedItem(panel->customPaletteHistoryBtn);
3059
3060 tmp = wstrconcat(_("This will permanently remove the palette "),
3061 WMGetPopUpButtonItem(panel->customPaletteHistoryBtn, item));
3062 text = wstrconcat(tmp, _(".\n\nAre you sure you want to remove this palette ?"));
3063 wfree(tmp);
3064
3065 choice = WMRunAlertPanel(scr, panel->win, _("Remove"), text, _("Yes"), _("No"), NULL);
3066 /* returns 0 (= "Yes") or 1 (="No") */
3067 wfree(text);
3068
3069 if (choice == 0) {
3070
3071 tmp = wstrconcat(panel->configurationPath,
3072 WMGetPopUpButtonItem(panel->customPaletteHistoryBtn, item));
3073
3074 if (remove(tmp) == 0) {
3075 /* item-1 always exists */
3076 WMSetPopUpButtonSelectedItem(panel->customPaletteHistoryBtn, item - 1);
3077
3078 customPaletteHistoryCallback(panel->customPaletteHistoryBtn, panel);
3079 customSetPalette(panel);
3080
3081 WMRemovePopUpButtonItem(panel->customPaletteHistoryBtn, item);
3082
3083 } else {
3084 wsyserror(_("Couldn't remove palette %s\n"), tmp);
3085 }
3086
3087 wfree(tmp);
3088 }
3089}
3090
3091static void customPaletteHistoryCallback(WMWidget * w, void *data)
3092{
3093 W_ColorPanel *panel = (W_ColorPanel *) data;
3094 W_Screen *scr = WMWidgetScreen(panel->win);
3095 int item;
3096 char *filename;
3097 RImage *tmp = NULL;
3098 unsigned char perm_mask;
3099
3100 item = WMGetPopUpButtonSelectedItem(panel->customPaletteHistoryBtn);
3101 if (item == panel->currentPalette)
3102 return;
3103
3104 if (item == 0) {
3105 customRenderSpectrum(panel);
3106
3107 WMSetPopUpButtonItemEnabled(panel->customPaletteMenuBtn, CPmenuRename, False);
3108 WMSetPopUpButtonItemEnabled(panel->customPaletteMenuBtn, CPmenuRemove, False);
3109 } else {
3110 /* Load file from configpath */
3111 filename = wstrconcat(panel->configurationPath,
3112 WMGetPopUpButtonItem(panel->customPaletteHistoryBtn, item));
3113
3114 /* If the file corresponding to the item does not exist,
3115 * remove it from the history list and select the next one.
3116 */
3117 perm_mask = (access(filename, F_OK) == 0);
3118 if (!perm_mask) {
3119 /* File does not exist */
3120 wfree(filename);
3121 WMSetPopUpButtonSelectedItem(panel->customPaletteHistoryBtn, item - 1);
3122 WMRemovePopUpButtonItem(panel->customPaletteHistoryBtn, item);
3123 customPaletteHistoryCallback(w, data);
3124 return;
3125 }
6830b057 dan2004-10-12 21:28:27 +00003126
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02003127 /* Get the image */
3128 tmp = RLoadImage(scr->rcontext, filename, 0);
3129 if (tmp) {
3130 if (panel->customPaletteImg) {
3131 RReleaseImage(panel->customPaletteImg);
3132 panel->customPaletteImg = NULL;
3133 }
3134 panel->customPaletteImg = tmp;
3135 }
3136
3137 /* If the image is not writable, don't allow removing/renaming */
3138 perm_mask = (access(filename, W_OK) == 0);
3139 WMSetPopUpButtonItemEnabled(panel->customPaletteMenuBtn, CPmenuRename, perm_mask);
3140 WMSetPopUpButtonItemEnabled(panel->customPaletteMenuBtn, CPmenuRemove, perm_mask);
6830b057 dan2004-10-12 21:28:27 +00003141
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02003142 wfree(filename);
3143 }
3144 customSetPalette(panel);
3145
3146 panel->currentPalette = item;
416e3a82 dan1999-01-25 19:06:50 +00003147}
3148
416e3a82 dan1999-01-25 19:06:50 +00003149/************************* ColorList Panel Functions **********************/
3150
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +02003151static void colorListPaintItem(WMList * lPtr, int index, Drawable d, char *text, int state, WMRect * rect)
bedc2238 kojima1999-05-16 07:00:01 +00003152{
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02003153 WMScreen *scr = WMWidgetScreen(lPtr);
3154 Display *dpy = WMScreenDisplay(scr);
3155 WMView *view = W_VIEW(lPtr);
3156 RColor *color = (RColor *) WMGetListItem(lPtr, index)->clientData;
3157 W_ColorPanel *panel = WMGetHangedData(lPtr);
3158 int width, height, x, y;
3159 WMColor *fillColor;
6830b057 dan2004-10-12 21:28:27 +00003160
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02003161 width = rect->size.width;
3162 height = rect->size.height;
3163 x = rect->pos.x;
3164 y = rect->pos.y;
6830b057 dan2004-10-12 21:28:27 +00003165
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02003166 if (state & WLDSSelected)
3167 XFillRectangle(dpy, d, WMColorGC(scr->white), x, y, width, height);
3168 else
3169 XFillRectangle(dpy, d, WMColorGC(view->backColor), x, y, width, height);
6830b057 dan2004-10-12 21:28:27 +00003170
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +02003171 fillColor = WMCreateRGBColor(scr, color->red << 8, color->green << 8, color->blue << 8, True);
6830b057 dan2004-10-12 21:28:27 +00003172
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02003173 XFillRectangle(dpy, d, WMColorGC(fillColor), x, y, 15, height);
3174 WMReleaseColor(fillColor);
6830b057 dan2004-10-12 21:28:27 +00003175
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +02003176 WMDrawString(scr, d, scr->black, panel->font12, x + 18, y, text, strlen(text));
416e3a82 dan1999-01-25 19:06:50 +00003177}
3178
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +02003179static void colorListSelect(WMWidget * w, void *data)
416e3a82 dan1999-01-25 19:06:50 +00003180{
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02003181 W_ColorPanel *panel = (W_ColorPanel *) data;
3182 CPColor cpColor;
6830b057 dan2004-10-12 21:28:27 +00003183
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02003184 cpColor.rgb = *((RColor *) WMGetListSelectedItem(w)->clientData);
3185 cpColor.set = cpRGB;
6830b057 dan2004-10-12 21:28:27 +00003186
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02003187 panel->lastChanged = WMColorListModeColorPanel;
3188 updateSwatch(panel, cpColor);
416e3a82 dan1999-01-25 19:06:50 +00003189}
3190
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +02003191static void colorListColorMenuCallback(WMWidget * w, void *data)
088c0ac2 dan1999-03-09 14:58:01 +00003192{
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02003193 W_ColorPanel *panel = (W_ColorPanel *) data;
3194 int item = WMGetPopUpButtonSelectedItem(panel->colorListColorMenuBtn);
6830b057 dan2004-10-12 21:28:27 +00003195
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02003196 switch (item) {
3197 case CLmenuAdd:
3198 break;
3199 case CLmenuRename:
3200 break;
3201 case CLmenuRemove:
3202 break;
3203 }
088c0ac2 dan1999-03-09 14:58:01 +00003204}
3205
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +02003206static void colorListListMenuCallback(WMWidget * w, void *data)
088c0ac2 dan1999-03-09 14:58:01 +00003207{
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02003208 W_ColorPanel *panel = (W_ColorPanel *) data;
3209 int item = WMGetPopUpButtonSelectedItem(panel->colorListListMenuBtn);
6830b057 dan2004-10-12 21:28:27 +00003210
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02003211 switch (item) {
3212 case CLmenuAdd:
3213 /* New Color List */
3214 colorListListMenuNew(panel);
3215 break;
3216 case CLmenuRename:
3217 break;
3218 case CLmenuRemove:
3219 break;
3220 }
088c0ac2 dan1999-03-09 14:58:01 +00003221}
3222
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +02003223static void colorListListMenuNew(W_ColorPanel * panel)
088c0ac2 dan1999-03-09 14:58:01 +00003224{
6830b057 dan2004-10-12 21:28:27 +00003225
088c0ac2 dan1999-03-09 14:58:01 +00003226}
416e3a82 dan1999-01-25 19:06:50 +00003227
d98f1fa6 kojima2000-01-06 21:56:39 +00003228/*************** Panel Initialisation Functions *****************/
3229
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +02003230static void wheelInit(W_ColorPanel * panel)
d98f1fa6 kojima2000-01-06 21:56:39 +00003231{
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +02003232 CPColor cpColor;
6830b057 dan2004-10-12 21:28:27 +00003233
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02003234 if (panel->color.set != cpHSV)
3235 convertCPColor(&panel->color);
d98f1fa6 kojima2000-01-06 21:56:39 +00003236
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +02003237 WMSetSliderValue(panel->wheelBrightnessS, 255 - panel->color.hsv.value);
6830b057 dan2004-10-12 21:28:27 +00003238
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02003239 panel->colx = 2 + rint((colorWheelSize / 2.0) *
3240 (1 + (panel->color.hsv.saturation / 255.0) *
3241 cos(panel->color.hsv.hue * M_PI / 180.0)));
3242 panel->coly = 2 + rint((colorWheelSize / 2.0) *
3243 (1 + (panel->color.hsv.saturation / 255.0) *
3244 sin(-panel->color.hsv.hue * M_PI / 180.0)));
6830b057 dan2004-10-12 21:28:27 +00003245
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +02003246 wheelCalculateValues(panel, panel->color.hsv.value);
6830b057 dan2004-10-12 21:28:27 +00003247
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02003248 cpColor = panel->color;
3249 cpColor.hsv.value = 255;
3250 cpColor.set = cpHSV;
3251 wheelUpdateBrightnessGradient(panel, cpColor);
d98f1fa6 kojima2000-01-06 21:56:39 +00003252}
3253
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +02003254static void grayInit(W_ColorPanel * panel)
d98f1fa6 kojima2000-01-06 21:56:39 +00003255{
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02003256 int value;
3257 char tmp[4];
6830b057 dan2004-10-12 21:28:27 +00003258
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02003259 if (panel->color.set != cpHSV)
3260 convertCPColor(&panel->color);
d98f1fa6 kojima2000-01-06 21:56:39 +00003261
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02003262 value = rint(panel->color.hsv.value / 2.55);
3263 WMSetSliderValue(panel->grayBrightnessS, value);
6830b057 dan2004-10-12 21:28:27 +00003264
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02003265 sprintf(tmp, "%d", value);
3266 WMSetTextFieldText(panel->grayBrightnessT, tmp);
d98f1fa6 kojima2000-01-06 21:56:39 +00003267}
3268
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +02003269static void rgbInit(W_ColorPanel * panel)
d98f1fa6 kojima2000-01-06 21:56:39 +00003270{
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +02003271 char tmp[4];
6830b057 dan2004-10-12 21:28:27 +00003272
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02003273 if (panel->color.set != cpRGB)
3274 convertCPColor(&panel->color);
6830b057 dan2004-10-12 21:28:27 +00003275
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02003276 WMSetSliderValue(panel->rgbRedS, panel->color.rgb.red);
3277 WMSetSliderValue(panel->rgbGreenS, panel->color.rgb.green);
3278 WMSetSliderValue(panel->rgbBlueS, panel->color.rgb.blue);
6830b057 dan2004-10-12 21:28:27 +00003279
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02003280 sprintf(tmp, "%d", panel->color.rgb.red);
3281 WMSetTextFieldText(panel->rgbRedT, tmp);
3282 sprintf(tmp, "%d", panel->color.rgb.green);
3283 WMSetTextFieldText(panel->rgbGreenT, tmp);
3284 sprintf(tmp, "%d", panel->color.rgb.blue);
3285 WMSetTextFieldText(panel->rgbBlueT, tmp);
d98f1fa6 kojima2000-01-06 21:56:39 +00003286}
3287
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +02003288static void cmykInit(W_ColorPanel * panel)
d98f1fa6 kojima2000-01-06 21:56:39 +00003289{
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02003290 int value[3];
3291 char tmp[4];
6830b057 dan2004-10-12 21:28:27 +00003292
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02003293 if (panel->color.set != cpRGB)
3294 convertCPColor(&panel->color);
6830b057 dan2004-10-12 21:28:27 +00003295
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02003296 value[0] = rint((255 - panel->color.rgb.red) / 2.55);
3297 value[1] = rint((255 - panel->color.rgb.green) / 2.55);
3298 value[2] = rint((255 - panel->color.rgb.blue) / 2.55);
6830b057 dan2004-10-12 21:28:27 +00003299
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02003300 WMSetSliderValue(panel->cmykCyanS, value[0]);
3301 WMSetSliderValue(panel->cmykMagentaS, value[1]);
3302 WMSetSliderValue(panel->cmykYellowS, value[2]);
3303 WMSetSliderValue(panel->cmykBlackS, 0);
6830b057 dan2004-10-12 21:28:27 +00003304
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02003305 sprintf(tmp, "%d", value[0]);
3306 WMSetTextFieldText(panel->cmykCyanT, tmp);
3307 sprintf(tmp, "%d", value[1]);
3308 WMSetTextFieldText(panel->cmykMagentaT, tmp);
3309 sprintf(tmp, "%d", value[2]);
3310 WMSetTextFieldText(panel->cmykYellowT, tmp);
3311 WMSetTextFieldText(panel->cmykBlackT, "0");
d98f1fa6 kojima2000-01-06 21:56:39 +00003312}
3313
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +02003314static void hsbInit(W_ColorPanel * panel)
d98f1fa6 kojima2000-01-06 21:56:39 +00003315{
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02003316 int value[3];
3317 char tmp[4];
6830b057 dan2004-10-12 21:28:27 +00003318
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02003319 if (panel->color.set != cpHSV)
3320 convertCPColor(&panel->color);
6830b057 dan2004-10-12 21:28:27 +00003321
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02003322 value[0] = panel->color.hsv.hue;
3323 value[1] = rint(panel->color.hsv.saturation / 2.55);
3324 value[2] = rint(panel->color.hsv.value / 2.55);
6830b057 dan2004-10-12 21:28:27 +00003325
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02003326 WMSetSliderValue(panel->hsbHueS, value[0]);
3327 WMSetSliderValue(panel->hsbSaturationS, value[1]);
3328 WMSetSliderValue(panel->hsbBrightnessS, value[2]);
6830b057 dan2004-10-12 21:28:27 +00003329
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02003330 sprintf(tmp, "%d", value[0]);
3331 WMSetTextFieldText(panel->hsbHueT, tmp);
3332 sprintf(tmp, "%d", value[1]);
3333 WMSetTextFieldText(panel->hsbSaturationT, tmp);
3334 sprintf(tmp, "%d", value[2]);
3335 WMSetTextFieldText(panel->hsbBrightnessT, tmp);
6830b057 dan2004-10-12 21:28:27 +00003336
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02003337 hsbUpdateBrightnessGradient(panel);
3338 hsbUpdateSaturationGradient(panel);
3339 hsbUpdateHueGradient(panel);
d98f1fa6 kojima2000-01-06 21:56:39 +00003340}
3341
416e3a82 dan1999-01-25 19:06:50 +00003342/************************** Common utility functions ************************/
3343
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +02003344static int fetchFile(char *toPath, char *srcFile, char *destFile)
416e3a82 dan1999-01-25 19:06:50 +00003345{
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02003346 int src, dest;
3347 int n;
3348 char *tmp;
3349 char buf[BUFSIZE];
6830b057 dan2004-10-12 21:28:27 +00003350
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02003351 if ((src = open(srcFile, O_RDONLY | O_BINARY)) == 0) {
3352 wsyserror(_("Could not open %s"), srcFile);
3353 return -1;
3354 }
6830b057 dan2004-10-12 21:28:27 +00003355
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02003356 tmp = wstrconcat(toPath, destFile);
3357 if ((dest = open(tmp, O_RDWR | O_CREAT | O_BINARY, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH))
3358 == 0) {
3359 wsyserror(_("Could not create %s"), tmp);
3360 wfree(tmp);
3361 return -1;
3362 }
3363 wfree(tmp);
6830b057 dan2004-10-12 21:28:27 +00003364
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02003365 /* Copy the file */
3366 while ((n = read(src, buf, BUFSIZE)) > 0) {
3367 if (write(dest, buf, n) != n) {
3368 wsyserror(_("Write error on file %s"), destFile);
3369 return -1;
3370 }
3371 }
6830b057 dan2004-10-12 21:28:27 +00003372
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +02003373 return 0;
416e3a82 dan1999-01-25 19:06:50 +00003374}
3375
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +02003376char *generateNewFilename(char *curName)
416e3a82 dan1999-01-25 19:06:50 +00003377{
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02003378 int n;
3379 char c;
3380 int baseLen;
3381 char *ptr;
3382 char *newName;
6830b057 dan2004-10-12 21:28:27 +00003383
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +02003384 assert(curName);
6830b057 dan2004-10-12 21:28:27 +00003385
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +02003386 ptr = curName;
6830b057 dan2004-10-12 21:28:27 +00003387
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02003388 if (((ptr = strrchr(ptr, '{')) == 0) || sscanf(ptr, "{%i}%c", &n, &c) != 1)
3389 return wstrconcat(curName, " {1}");
6830b057 dan2004-10-12 21:28:27 +00003390
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +02003391 baseLen = ptr - curName - 1;
6830b057 dan2004-10-12 21:28:27 +00003392
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02003393 newName = wmalloc(baseLen + 16);
3394 strncpy(newName, curName, baseLen);
3395 newName[baseLen] = 0;
6830b057 dan2004-10-12 21:28:27 +00003396
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +02003397 sprintf(&newName[baseLen], " {%i}", n + 1);
6830b057 dan2004-10-12 21:28:27 +00003398
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +02003399 return newName;
416e3a82 dan1999-01-25 19:06:50 +00003400}
95e71d50 id1999-12-29 11:59:30 +00003401
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +02003402void convertCPColor(CPColor * color)
95e71d50 id1999-12-29 11:59:30 +00003403{