- s/sprintf/snprintf
[wmaker-crm.git] / WINGs / wcolorpanel.c
blob2870434d55b35ae39d9ec48365bda2be61d58182
1 /*
2 * ColorPanel for WINGs
4 * by ]d : Original idea and basic initial code
5 * Pascal Hofstee : Code for wheeldrawing and calculating
6 * colors from it.
7 * Primary coder of this Color Panel.
8 * Alban Hertroys : Optimizations for algorithms for color-
9 * wheel. Also custom ColorPalettes and
10 * 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 ;-)
18 /* TODO:
19 * - Look at further optimization of colorWheel matrix calculation.
20 * It appears to be rather symmetric in angles of 60 degrees,
21 * while it is optimized in angles of 90 degrees.
22 * - Custom color-lists and custom colors in custom color-lists.
23 * - Stored colors
24 * - Resizing
27 #include "../src/config.h"
28 #include "wconfig.h"
29 #include "WINGsP.h"
30 #include <math.h>
31 #include <unistd.h>
32 #include <ctype.h>
33 #include <fcntl.h>
34 #include <sys/types.h>
35 #include <sys/stat.h>
36 #include <dirent.h>
37 #include <errno.h>
39 /* BUG There's something fishy with shaped windows */
40 #if 1
41 #ifdef SHAPE
42 #define SHAPE_WAS_DEFINED
43 #undef SHAPE
44 #endif
45 #endif
48 #ifdef SHAPE
49 #include <X11/extensions/shape.h>
50 #endif
53 #ifndef PATH_MAX
54 # define PATH_MAX 1024
55 #endif
58 char *WMColorPanelColorChangedNotification = "WMColorPanelColorChangedNotification";
62 * Bitmaps for magnifying glass cursor
65 /* Cursor */
66 #define Cursor_x_hot 11
67 #define Cursor_y_hot 11
68 #define Cursor_width 32
69 #define Cursor_height 32
70 static unsigned char Cursor_bits[] = {
71 0x00,0x7e,0x00,0x00,0xc0,0x81,0x03,0x00,0x20,0x00,0x04,0x00,0x10,0x00,0x08,
72 0x00,0x08,0x00,0x10,0x00,0x04,0x00,0x20,0x00,0x02,0x00,0x40,0x00,0x02,0x00,
73 0x40,0x00,0x02,0x00,0x40,0x00,0x01,0x42,0x80,0x00,0x01,0x24,0x80,0x00,0x01,
74 0x00,0x80,0x00,0x01,0x00,0x80,0x00,0x01,0x24,0x80,0x00,0x01,0x42,0x80,0x00,
75 0x02,0x00,0x40,0x00,0x02,0x00,0x40,0x00,0x02,0x00,0x40,0x00,0x04,0x00,0x20,
76 0x00,0x08,0x00,0x50,0x00,0x10,0x00,0x88,0x00,0x20,0x00,0x5c,0x01,0xc0,0x81,
77 0x3b,0x02,0x00,0x7e,0x70,0x05,0x00,0x00,0xe0,0x08,0x00,0x00,0xc0,0x15,0x00,
78 0x00,0x80,0x23,0x00,0x00,0x00,0x57,0x00,0x00,0x00,0x8e,0x00,0x00,0x00,0x5c,
79 0x00,0x00,0x00,0xb8,0x00,0x00,0x00,0x70};
81 /* Cursor shape-mask */
82 #define Cursor_shape_width 32
83 #define Cursor_shape_height 32
84 static unsigned char Cursor_shape_bits[] = {
85 0x00,0x7e,0x00,0x00,0xc0,0x81,0x03,0x00,0x20,0x00,0x04,0x00,0x10,0x00,0x08,
86 0x00,0x08,0x00,0x10,0x00,0x04,0x00,0x20,0x00,0x02,0x00,0x40,0x00,0x02,0x00,
87 0x40,0x00,0x02,0x00,0x40,0x00,0x01,0x42,0x80,0x00,0x01,0x24,0x80,0x00,0x01,
88 0x00,0x80,0x00,0x01,0x00,0x80,0x00,0x01,0x24,0x80,0x00,0x01,0x42,0x80,0x00,
89 0x02,0x00,0x40,0x00,0x02,0x00,0x40,0x00,0x02,0x00,0x40,0x00,0x04,0x00,0x20,
90 0x00,0x08,0x00,0x70,0x00,0x10,0x00,0xf8,0x00,0x20,0x00,0xfc,0x01,0xc0,0x81,
91 0xfb,0x03,0x00,0x7e,0xf0,0x07,0x00,0x00,0xe0,0x0f,0x00,0x00,0xc0,0x1f,0x00,
92 0x00,0x80,0x3f,0x00,0x00,0x00,0x7f,0x00,0x00,0x00,0xfe,0x00,0x00,0x00,0xfc,
93 0x00,0x00,0x00,0xf8,0x00,0x00,0x00,0x70};
95 /* Clip-mask for magnified pixels */
96 #define Cursor_mask_width 24
97 #define Cursor_mask_height 24
98 static unsigned char Cursor_mask_bits[] = {
99 0x00, 0x00, 0x00, 0x00, 0x7e, 0x00, 0xc0, 0xff, 0x03, 0xe0, 0xff, 0x07,
100 0xf0, 0xff, 0x0f, 0xf8, 0xff, 0x1f, 0xfc, 0xff, 0x3f, 0xfc, 0xff, 0x3f,
101 0xfc, 0xff, 0x3f, 0xfe, 0xff, 0x7f, 0xfe, 0xff, 0x7f, 0xfe, 0xff, 0x7f,
102 0xfe, 0xff, 0x7f, 0xfe, 0xff, 0x7f, 0xfe, 0xff, 0x7f, 0xfc, 0xff, 0x3f,
103 0xfc, 0xff, 0x3f, 0xfc, 0xff, 0x3f, 0xf8, 0xff, 0x1f, 0xf0, 0xff, 0x0f,
104 0xe0, 0xff, 0x07, 0xc0, 0xff, 0x03, 0x00, 0x7e, 0x00, 0x00, 0x00, 0x00};
107 typedef struct MovingView {
108 WMView *view; /* The view this is all about */
109 XImage *image; /* What's under the view */
110 XImage *dirtyRect; /* Storage of overlapped image area */
111 Pixmap magPix; /* Magnified part of pixmap */
112 RColor color; /* Color of a pixel in the image */
113 int x, y; /* Position of view */
114 } MovingView;
116 typedef struct CPColor {
117 RColor rgb; /* The RGB values of the color */
118 RHSVColor hsv; /* The HSV values of the color */
119 enum { /* Which one was last set ? */
120 cpNone,
121 cpRGB,
122 cpHSV
123 } set;
124 } CPColor;
127 typedef struct WheelMatrix {
128 unsigned int width, height; /* Size of the colorwheel */
129 unsigned char *data[3]; /* Wheel data (R,G,B) */
130 unsigned char values[256]; /* Precalculated values R,G & B = 0-255 */
131 } wheelMatrix;
133 typedef struct W_ColorPanel {
134 WMWindow *win;
135 WMFont *font8;
136 WMFont *font12;
137 void *clientData;
138 WMAction2 *action;
140 /* Common Stuff */
141 WMColorWell *colorWell;
142 WMButton *magnifyBtn;
143 WMButton *wheelBtn;
144 WMButton *slidersBtn;
145 WMButton *customPaletteBtn;
146 WMButton *colorListBtn;
148 /* Magnifying Glass */
149 MovingView *magnifyGlass;
151 /* ColorWheel Panel */
152 WMFrame *wheelFrm;
153 WMSlider *wheelBrightnessS;
154 WMView *wheelView;
156 /* Slider Panels */
157 WMFrame *slidersFrm;
158 WMFrame *seperatorFrm;
159 WMButton *grayBtn;
160 WMButton *rgbBtn;
161 WMButton *cmykBtn;
162 WMButton *hsbBtn;
163 /* Gray Scale Panel */
164 WMFrame *grayFrm;
165 WMLabel *grayMinL;
166 WMLabel *grayMaxL;
167 WMSlider *grayBrightnessS;
168 WMTextField *grayBrightnessT;
169 WMButton *grayPresetBtn[7];
171 /* RGB Panel */
172 WMFrame *rgbFrm;
173 WMLabel *rgbMinL;
174 WMLabel *rgbMaxL;
175 WMSlider *rgbRedS;
176 WMSlider *rgbGreenS;
177 WMSlider *rgbBlueS;
178 WMTextField *rgbRedT;
179 WMTextField *rgbGreenT;
180 WMTextField *rgbBlueT;
182 /* CMYK Panel */
183 WMFrame *cmykFrm;
184 WMLabel *cmykMinL;
185 WMLabel *cmykMaxL;
186 WMSlider *cmykCyanS;
187 WMSlider *cmykMagentaS;
188 WMSlider *cmykYellowS;
189 WMSlider *cmykBlackS;
190 WMTextField *cmykCyanT;
191 WMTextField *cmykMagentaT;
192 WMTextField *cmykYellowT;
193 WMTextField *cmykBlackT;
195 /* HSB Panel */
196 WMFrame *hsbFrm;
197 WMSlider *hsbHueS;
198 WMSlider *hsbSaturationS;
199 WMSlider *hsbBrightnessS;
200 WMTextField *hsbHueT;
201 WMTextField *hsbSaturationT;
202 WMTextField *hsbBrightnessT;
204 /* Custom Palette Panel*/
205 WMFrame *customPaletteFrm;
206 WMPopUpButton *customPaletteHistoryBtn;
207 WMFrame *customPaletteContentFrm;
208 WMPopUpButton *customPaletteMenuBtn;
209 WMView *customPaletteContentView;
211 /* Color List Panel */
212 WMFrame *colorListFrm;
213 WMPopUpButton *colorListHistoryBtn;
214 WMList *colorListContentLst;
215 WMPopUpButton *colorListColorMenuBtn;
216 WMPopUpButton *colorListListMenuBtn;
218 /* Look-Up Tables and Images */
219 wheelMatrix *wheelMtrx;
220 Pixmap wheelImg;
221 Pixmap selectionImg;
222 Pixmap selectionBackImg;
223 RImage *customPaletteImg;
224 char *lastBrowseDir;
226 /* Common Data Fields */
227 CPColor color; /* Current color */
228 WMColorPanelMode mode; /* Current color selection mode */
229 WMColorPanelMode slidersmode; /* Current color sel. mode sliders panel */
230 WMColorPanelMode lastChanged; /* Panel that last changed the color */
231 int colx, coly; /* (x,y) of sel.-marker in WheelMode */
232 int palx, paly; /* (x,y) of sel.-marker in
233 CustomPaletteMode */
234 double palXRatio, palYRatio; /* Ratios in x & y between
235 original and scaled
236 palettesize */
237 int currentPalette;
238 char *configurationPath;
240 struct {
241 unsigned int continuous:1;
242 unsigned int dragging:1;
243 } flags;
244 } W_ColorPanel;
246 enum {
247 CPmenuNewFromFile,
248 CPmenuRename,
249 CPmenuRemove,
250 CPmenuCopy,
251 CPmenuNewFromClipboard
252 } customPaletteMenuItem;
254 enum {
255 CLmenuAdd,
256 CLmenuRename,
257 CLmenuRemove
258 } colorListMenuItem;
261 #define PWIDTH 194
262 #define PHEIGHT 266
263 #define colorWheelSize 150
264 #define customPaletteWidth 182
265 #define customPaletteHeight 106
266 #define knobThickness 8
268 #define SPECTRUM_WIDTH 511
269 #define SPECTRUM_HEIGHT 360
271 #define COLORWHEEL_PART 1
272 #define CUSTOMPALETTE_PART 2
273 #define BUFSIZE 1024
275 #ifndef RGBTXT
276 #define RGBTXT "/usr/X11R6/lib/X11/rgb.txt"
277 #endif
279 #define MAX_LENGTH 1024
282 #ifndef M_PI
283 #define M_PI 3.14159265358979323846
284 #endif
287 static int fetchFile(char* toPath, char *imageSrcFile,
288 char *imageDestFileName);
289 char *generateNewFilename(char *curName);
290 void convertCPColor(CPColor *color);
291 RColor ulongToRColor(WMScreen *scr, unsigned long value);
292 unsigned char getShift(unsigned char value);
294 static void modeButtonCallback(WMWidget *w, void *data);
295 static int getPickerPart(W_ColorPanel *panel, int x, int y);
296 static void readConfiguration(W_ColorPanel *panel);
297 static void readXColors(W_ColorPanel *panel);
299 static void closeWindowCallback(WMWidget *w, void *data);
301 static Cursor magnifyGrabPointer(W_ColorPanel *panel);
302 static WMPoint magnifyInitialize(W_ColorPanel *panel);
303 static void magnifyPutCursor(WMWidget *w, void *data);
304 static Pixmap magnifyCreatePixmap(WMColorPanel *panel);
305 static void magnifyGetImageStored(W_ColorPanel *panel, int x1, int y1,
306 int x2, int y2);
307 static XImage* magnifyGetImage(WMScreen *scr, XImage *image, int x, int y,
308 int w, int h);
310 static wheelMatrix* wheelCreateMatrix(unsigned int width , unsigned int height);
311 static void wheelDestroyMatrix(wheelMatrix *matrix);
312 static void wheelInitMatrix(W_ColorPanel *panel);
313 static void wheelCalculateValues(W_ColorPanel *panel, int maxvalue);
314 static void wheelRender(W_ColorPanel *panel);
315 static Bool wheelInsideColorWheel(W_ColorPanel *panel, unsigned long ofs);
316 static void wheelPaint(W_ColorPanel *panel);
318 static void wheelHandleEvents(XEvent *event, void *data);
319 static void wheelHandleActionEvents(XEvent *event, void *data);
320 static void wheelBrightnessSliderCallback(WMWidget *w, void *data);
321 static void wheelUpdateSelection(W_ColorPanel *panel);
322 static void wheelUndrawSelection(W_ColorPanel *panel);
324 static void wheelPositionSelection(W_ColorPanel *panel, int x, int y);
325 static void wheelPositionSelectionOutBounds(W_ColorPanel *panel, int x, int y);
326 static void wheelUpdateBrightnessGradientFromLocation (W_ColorPanel *panel);
327 static void wheelUpdateBrightnessGradient(W_ColorPanel *panel, CPColor topColor);
329 static void grayBrightnessSliderCallback(WMWidget *w, void *data);
330 static void grayPresetButtonCallback(WMWidget *w, void *data);
331 static void grayBrightnessTextFieldCallback(void *observerData,
332 WMNotification *notification);
334 static void rgbSliderCallback(WMWidget *w, void *data);
335 static void rgbTextFieldCallback(void *observerData,
336 WMNotification *notification);
338 static void cmykSliderCallback(WMWidget *w, void *data);
339 static void cmykTextFieldCallback(void *observerData,
340 WMNotification *notification);
342 static void hsbSliderCallback(WMWidget *w, void *data);
343 static void hsbTextFieldCallback(void *observerData,
344 WMNotification *notification);
345 static void hsbUpdateBrightnessGradient(W_ColorPanel *panel);
346 static void hsbUpdateSaturationGradient(W_ColorPanel *panel);
347 static void hsbUpdateHueGradient(W_ColorPanel *panel);
349 static void customRenderSpectrum(W_ColorPanel *panel);
350 static void customSetPalette(W_ColorPanel *panel);
351 static void customPaletteHandleEvents(XEvent *event, void *data);
352 static void customPaletteHandleActionEvents(XEvent *event, void *data);
353 static void customPalettePositionSelection(W_ColorPanel *panel, int x, int y);
354 static void customPalettePositionSelectionOutBounds(W_ColorPanel *panel,
355 int x, int y);
356 static void customPaletteMenuCallback(WMWidget *w, void *data);
357 static void customPaletteHistoryCallback(WMWidget *w, void *data);
359 static void customPaletteMenuNewFromFile(W_ColorPanel *panel);
360 static void customPaletteMenuRename(W_ColorPanel *panel);
361 static void customPaletteMenuRemove(W_ColorPanel *panel);
363 static void colorListPaintItem(WMList *lPtr, int index, Drawable d, char *text,
364 int state, WMRect *rect);
365 static void colorListSelect(WMWidget *w, void *data);
366 static void colorListColorMenuCallback(WMWidget *w, void *data);
367 static void colorListListMenuCallback(WMWidget *w, void *data);
368 static void colorListListMenuNew(W_ColorPanel *panel);
370 static void wheelInit(W_ColorPanel *panel);
371 static void grayInit(W_ColorPanel *panel);
372 static void rgbInit(W_ColorPanel *panel);
373 static void cmykInit(W_ColorPanel *panel);
374 static void hsbInit(W_ColorPanel *panel);
378 void
379 WMSetColorPanelAction(WMColorPanel *panel, WMAction2 *action, void *data)
381 panel->action = action;
382 panel->clientData = data;
385 static WMColorPanel*
386 makeColorPanel(WMScreen *scrPtr, char *name)
388 WMColorPanel *panel;
389 RImage *image;
390 WMPixmap *pixmap;
391 RColor from;
392 RColor to;
393 WMColor *textcolor, *graybuttoncolor;
394 int i;
395 GC bgc = WMColorGC(scrPtr->black);
396 GC wgc = WMColorGC(scrPtr->white);
399 panel = wmalloc(sizeof(WMColorPanel));
400 memset(panel, 0, sizeof(WMColorPanel));
402 panel->color.rgb.red = 0;
403 panel->color.rgb.green = 0;
404 panel->color.rgb.blue = 0;
405 panel->color.hsv.hue = 0;
406 panel->color.hsv.saturation = 0;
407 panel->color.hsv.value = 0;
408 panel->color.set = cpNone; /* Color has not been set yet */
410 panel->font8 = WMSystemFontOfSize(scrPtr, 8);
411 panel->font12 = WMSystemFontOfSize(scrPtr, 12);
413 panel->win = WMCreateWindowWithStyle(scrPtr, name,
414 WMTitledWindowMask | WMClosableWindowMask | WMResizableWindowMask);
415 WMResizeWidget(panel->win, PWIDTH, PHEIGHT);
416 WMSetWindowTitle(panel->win, _("Colors"));
417 WMSetWindowCloseAction(panel->win, closeWindowCallback, panel);
420 /* Set Default ColorPanel Mode(s) */
421 panel->mode = WMWheelModeColorPanel;
422 panel->lastChanged = 0;
423 panel->slidersmode = WMRGBModeColorPanel;
424 panel->configurationPath = wstrconcat(wusergnusteppath(),
425 "/Library/Colors/");
427 /* Some General Purpose Widgets */
428 panel->colorWell = WMCreateColorWell(panel->win);
429 WMResizeWidget(panel->colorWell, 134, 36);
430 WSetColorWellBordered(panel->colorWell, False);
431 WMMoveWidget(panel->colorWell, 56, 4);
433 panel->magnifyBtn = WMCreateCustomButton(panel->win,
434 WBBStateLightMask|WBBStateChangeMask);
435 WMResizeWidget(panel->magnifyBtn, 46, 36);
436 WMMoveWidget(panel->magnifyBtn, 6,4);
437 WMSetButtonAction(panel->magnifyBtn, magnifyPutCursor, panel);
438 WMSetButtonImagePosition(panel->magnifyBtn, WIPImageOnly);
439 WMSetButtonImage(panel->magnifyBtn, scrPtr->magnifyIcon);
441 panel->wheelBtn = WMCreateCustomButton(panel->win,
442 WBBStateLightMask|WBBStateChangeMask);
443 WMResizeWidget(panel->wheelBtn, 46, 32);
444 WMMoveWidget(panel->wheelBtn, 6, 44);
445 WMSetButtonAction(panel->wheelBtn, modeButtonCallback, panel);
446 WMSetButtonImagePosition(panel->wheelBtn, WIPImageOnly);
447 WMSetButtonImage(panel->wheelBtn, scrPtr->wheelIcon);
449 panel->slidersBtn = WMCreateCustomButton(panel->win,
450 WBBStateLightMask|WBBStateChangeMask);
451 WMResizeWidget(panel->slidersBtn, 46, 32);
452 WMMoveWidget(panel->slidersBtn, 52, 44);
453 WMSetButtonAction(panel->slidersBtn, modeButtonCallback, panel);
454 WMSetButtonImagePosition(panel->slidersBtn, WIPImageOnly);
455 WMSetButtonImage(panel->slidersBtn, scrPtr->rgbIcon);
457 panel->customPaletteBtn = WMCreateCustomButton(panel->win,
458 WBBStateLightMask|WBBStateChangeMask);
459 WMResizeWidget(panel->customPaletteBtn, 46, 32);
460 WMMoveWidget(panel->customPaletteBtn, 98, 44);
461 WMSetButtonAction(panel->customPaletteBtn, modeButtonCallback, panel);
462 WMSetButtonImagePosition(panel->customPaletteBtn, WIPImageOnly);
463 WMSetButtonImage(panel->customPaletteBtn, scrPtr->customPaletteIcon);
465 panel->colorListBtn = WMCreateCustomButton(panel->win,
466 WBBStateLightMask|WBBStateChangeMask);
467 WMResizeWidget(panel->colorListBtn, 46, 32);
468 WMMoveWidget(panel->colorListBtn, 144, 44);
469 WMSetButtonAction(panel->colorListBtn, modeButtonCallback, panel);
470 WMSetButtonImagePosition(panel->colorListBtn, WIPImageOnly);
471 WMSetButtonImage(panel->colorListBtn, scrPtr->colorListIcon);
473 /* Let's Group some of them together */
474 WMGroupButtons(panel->wheelBtn, panel->slidersBtn);
475 WMGroupButtons(panel->wheelBtn, panel->customPaletteBtn);
476 WMGroupButtons(panel->wheelBtn, panel->colorListBtn);
478 /* Widgets for the ColorWheel Panel */
479 panel->wheelFrm = WMCreateFrame(panel->win);
480 WMSetFrameRelief(panel->wheelFrm, WRFlat);
481 WMResizeWidget(panel->wheelFrm, PWIDTH - 8, PHEIGHT - 80 - 26);
482 WMMoveWidget(panel->wheelFrm, 5, 80);
484 panel->wheelView = W_CreateView(W_VIEW(panel->wheelFrm));
485 /* XXX Can we create a view ? */
486 W_ResizeView(panel->wheelView, colorWheelSize+4, colorWheelSize+4);
487 W_MoveView(panel->wheelView, 0, 0);
489 /* Create an event handler to handle expose/click events in ColorWheel */
490 WMCreateEventHandler(panel->wheelView,
491 ButtonPressMask|ButtonReleaseMask|EnterWindowMask|
492 LeaveWindowMask|ButtonMotionMask, wheelHandleActionEvents, panel);
494 WMCreateEventHandler(panel->wheelView, ExposureMask, wheelHandleEvents,
495 panel);
497 panel->wheelBrightnessS = WMCreateSlider(panel->wheelFrm);
498 WMResizeWidget(panel->wheelBrightnessS, 16, 153);
499 WMMoveWidget(panel->wheelBrightnessS, 5+colorWheelSize+14, 1);
500 WMSetSliderMinValue(panel->wheelBrightnessS, 0);
501 WMSetSliderMaxValue(panel->wheelBrightnessS, 255);
502 WMSetSliderAction(panel->wheelBrightnessS, wheelBrightnessSliderCallback,
503 panel);
504 WMSetSliderKnobThickness(panel->wheelBrightnessS, knobThickness);
506 panel->wheelMtrx = wheelCreateMatrix(colorWheelSize+4, colorWheelSize+4);
507 wheelInitMatrix(panel);
510 /* Widgets for the Slider Panels */
511 panel->slidersFrm = WMCreateFrame(panel->win);
512 WMSetFrameRelief(panel->slidersFrm, WRFlat);
513 WMResizeWidget(panel->slidersFrm, PWIDTH - 8, PHEIGHT - 80 - 26);
514 WMMoveWidget(panel->slidersFrm, 4, 80);
516 panel->seperatorFrm = WMCreateFrame(panel->slidersFrm);
517 WMSetFrameRelief(panel->seperatorFrm, WRPushed);
518 WMResizeWidget(panel->seperatorFrm, PWIDTH - 8, 2);
519 WMMoveWidget(panel->seperatorFrm, 0, 1);
521 panel->grayBtn = WMCreateCustomButton(panel->slidersFrm,
522 WBBStateLightMask|WBBStateChangeMask);
523 WMResizeWidget(panel->grayBtn, 46, 24);
524 WMMoveWidget(panel->grayBtn, 1, 8);
525 WMSetButtonAction(panel->grayBtn, modeButtonCallback, panel);
526 WMSetButtonImagePosition(panel->grayBtn, WIPImageOnly);
527 WMSetButtonImage(panel->grayBtn, scrPtr->grayIcon);
529 panel->rgbBtn = WMCreateCustomButton(panel->slidersFrm,
530 WBBStateLightMask|WBBStateChangeMask);
531 WMResizeWidget(panel->rgbBtn, 46, 24);
532 WMMoveWidget(panel->rgbBtn, 47, 8);
533 WMSetButtonAction(panel->rgbBtn, modeButtonCallback, panel);
534 WMSetButtonImagePosition(panel->rgbBtn, WIPImageOnly);
535 WMSetButtonImage(panel->rgbBtn, scrPtr->rgbIcon);
537 panel->cmykBtn = WMCreateCustomButton(panel->slidersFrm,
538 WBBStateLightMask|WBBStateChangeMask);
539 WMResizeWidget(panel->cmykBtn, 46, 24);
540 WMMoveWidget(panel->cmykBtn, 93, 8);
541 WMSetButtonAction(panel->cmykBtn, modeButtonCallback, panel);
542 WMSetButtonImagePosition(panel->cmykBtn, WIPImageOnly);
543 WMSetButtonImage(panel->cmykBtn, scrPtr->cmykIcon);
545 panel->hsbBtn = WMCreateCustomButton(panel->slidersFrm,
546 WBBStateLightMask|WBBStateChangeMask);
547 WMResizeWidget(panel->hsbBtn, 46, 24);
548 WMMoveWidget(panel->hsbBtn, 139, 8);
549 WMSetButtonAction(panel->hsbBtn, modeButtonCallback, panel);
550 WMSetButtonImagePosition(panel->hsbBtn, WIPImageOnly);
551 WMSetButtonImage(panel->hsbBtn, scrPtr->hsbIcon);
553 /* Let's Group the Slider Panel Buttons Together */
554 WMGroupButtons(panel->grayBtn, panel->rgbBtn);
555 WMGroupButtons(panel->grayBtn, panel->cmykBtn);
556 WMGroupButtons(panel->grayBtn, panel->hsbBtn);
558 textcolor = WMDarkGrayColor(scrPtr);
560 /* Widgets for GrayScale Panel */
561 panel->grayFrm = WMCreateFrame(panel->slidersFrm);
562 WMSetFrameRelief(panel->grayFrm, WRFlat);
563 WMResizeWidget(panel->grayFrm, PWIDTH - 8, PHEIGHT - 80 - 26 - 32);
564 WMMoveWidget(panel->grayFrm, 0, 34);
566 panel->grayMinL = WMCreateLabel(panel->grayFrm);
567 WMResizeWidget(panel->grayMinL, 20, 10);
568 WMMoveWidget(panel->grayMinL, 2, 2);
569 WMSetLabelText(panel->grayMinL, "0");
570 WMSetLabelTextAlignment(panel->grayMinL, WALeft);
571 WMSetLabelTextColor(panel->grayMinL, textcolor);
572 WMSetLabelFont(panel->grayMinL, panel->font8);
574 panel->grayMaxL = WMCreateLabel(panel->grayFrm);
575 WMResizeWidget(panel->grayMaxL, 40, 10);
576 WMMoveWidget(panel->grayMaxL, 104, 2);
577 WMSetLabelText(panel->grayMaxL, "100");
578 WMSetLabelTextAlignment(panel->grayMaxL, WARight);
579 WMSetLabelTextColor(panel->grayMaxL, textcolor);
580 WMSetLabelFont(panel->grayMaxL, panel->font8);
582 panel->grayBrightnessS = WMCreateSlider(panel->grayFrm);
583 WMResizeWidget(panel->grayBrightnessS, 141, 16);
584 WMMoveWidget(panel->grayBrightnessS, 2, 14);
585 WMSetSliderMinValue(panel->grayBrightnessS, 0);
586 WMSetSliderMaxValue(panel->grayBrightnessS, 100);
587 WMSetSliderKnobThickness(panel->grayBrightnessS, knobThickness);
588 WMSetSliderAction(panel->grayBrightnessS, grayBrightnessSliderCallback,
589 panel);
591 from.red = 0;
592 from.green = 0;
593 from.blue = 0;
595 to.red = 255;
596 to.green = 255;
597 to.blue = 255;
599 image = RRenderGradient(141, 16, &from, &to, RGRD_HORIZONTAL);
600 pixmap = WMCreatePixmapFromRImage(scrPtr, image, 0);
601 RReleaseImage(image);
603 if (pixmap)
604 W_PaintText(W_VIEW(panel->grayBrightnessS), pixmap->pixmap,
605 panel->font12, 2, 0, 100, WALeft, WMColorGC(scrPtr->white),
606 False, _("Brightness"), strlen(_("Brightness")));
607 else
608 wwarning(_("Color Panel: Could not allocate memory"));
610 WMSetSliderImage(panel->grayBrightnessS, pixmap);
611 WMReleasePixmap(pixmap);
613 panel->grayBrightnessT = WMCreateTextField(panel->grayFrm);
614 WMResizeWidget(panel->grayBrightnessT, 40, 18);
615 WMMoveWidget(panel->grayBrightnessT, 146, 13);
616 WMSetTextFieldAlignment(panel->grayBrightnessT, WALeft);
617 WMAddNotificationObserver(grayBrightnessTextFieldCallback, panel,
618 WMTextDidEndEditingNotification, panel->grayBrightnessT);
620 for (i=0; i < 7; i++) {
621 pixmap = WMCreatePixmap(scrPtr, 13, 13, scrPtr->depth, False);
623 graybuttoncolor = WMCreateRGBColor(scrPtr, (255/6)*i << 8,
624 (255/6)*i << 8, (255/6)*i << 8, True);
625 WMPaintColorSwatch(graybuttoncolor, pixmap->pixmap, 0, 0, 15, 15);
626 WMReleaseColor(graybuttoncolor);
628 panel->grayPresetBtn[i] = WMCreateCommandButton(panel->grayFrm);
629 WMResizeWidget(panel->grayPresetBtn[i], 20, 24);
630 WMMoveWidget(panel->grayPresetBtn[i], 2+(i*20), 34);
631 WMSetButtonAction(panel->grayPresetBtn[i],
632 grayPresetButtonCallback, panel);
633 WMSetButtonImage(panel->grayPresetBtn[i], pixmap);
634 WMSetButtonImagePosition(panel->grayPresetBtn[i], WIPImageOnly);
635 WMReleasePixmap(pixmap);
639 /* End of GrayScale Panel */
641 /* Widgets for RGB Panel */
642 panel->rgbFrm = WMCreateFrame(panel->slidersFrm);
643 WMSetFrameRelief(panel->rgbFrm, WRFlat);
644 WMResizeWidget(panel->rgbFrm, PWIDTH - 8, PHEIGHT - 80 - 26 - 32);
645 WMMoveWidget(panel->rgbFrm, 0, 34);
647 panel->rgbMinL = WMCreateLabel(panel->rgbFrm);
648 WMResizeWidget(panel->rgbMinL, 20, 10);
649 WMMoveWidget(panel->rgbMinL, 2, 2);
650 WMSetLabelText(panel->rgbMinL, "0");
651 WMSetLabelTextAlignment(panel->rgbMinL, WALeft);
652 WMSetLabelTextColor(panel->rgbMinL, textcolor);
653 WMSetLabelFont(panel->rgbMinL, panel->font8);
655 panel->rgbMaxL = WMCreateLabel(panel->rgbFrm);
656 WMResizeWidget(panel->rgbMaxL, 40, 10);
657 WMMoveWidget(panel->rgbMaxL, 104, 2);
658 WMSetLabelText(panel->rgbMaxL, "255");
659 WMSetLabelTextAlignment(panel->rgbMaxL, WARight);
660 WMSetLabelTextColor(panel->rgbMaxL, textcolor);
661 WMSetLabelFont(panel->rgbMaxL, panel->font8);
663 panel->rgbRedS = WMCreateSlider(panel->rgbFrm);
664 WMResizeWidget(panel->rgbRedS, 141, 16);
665 WMMoveWidget(panel->rgbRedS, 2, 14);
666 WMSetSliderMinValue(panel->rgbRedS, 0);
667 WMSetSliderMaxValue(panel->rgbRedS, 255);
668 WMSetSliderKnobThickness(panel->rgbRedS, knobThickness);
669 WMSetSliderAction(panel->rgbRedS, rgbSliderCallback, panel);
671 to.red = 255;
672 to.green = 0;
673 to.blue = 0;
675 image = RRenderGradient(141, 16, &from, &to, RGRD_HORIZONTAL);
676 pixmap = WMCreatePixmapFromRImage(scrPtr, image, 0);
677 RReleaseImage(image);
679 if (pixmap)
680 W_PaintText(W_VIEW(panel->rgbRedS), pixmap->pixmap, panel->font12,
681 2, 0, 100, WALeft, WMColorGC(scrPtr->white), False, _("Red"),
682 strlen(_("Red")));
683 else
684 wwarning(_("Color Panel: Could not allocate memory"));
686 WMSetSliderImage(panel->rgbRedS, pixmap);
687 WMReleasePixmap(pixmap);
689 panel->rgbRedT = WMCreateTextField(panel->rgbFrm);
690 WMResizeWidget(panel->rgbRedT, 40, 18);
691 WMMoveWidget(panel->rgbRedT, 146, 13);
692 WMSetTextFieldAlignment(panel->rgbRedT, WALeft);
693 WMAddNotificationObserver(rgbTextFieldCallback, panel,
694 WMTextDidEndEditingNotification, panel->rgbRedT);
696 panel->rgbGreenS = WMCreateSlider(panel->rgbFrm);
697 WMResizeWidget(panel->rgbGreenS, 141, 16);
698 WMMoveWidget(panel->rgbGreenS, 2, 36);
699 WMSetSliderMinValue(panel->rgbGreenS, 0);
700 WMSetSliderMaxValue(panel->rgbGreenS, 255);
701 WMSetSliderKnobThickness(panel->rgbGreenS, knobThickness);
702 WMSetSliderAction(panel->rgbGreenS, rgbSliderCallback, panel);
704 to.red = 0;
705 to.green = 255;
706 to.blue = 0;
708 image = RRenderGradient(141, 16, &from, &to, RGRD_HORIZONTAL);
709 pixmap = WMCreatePixmapFromRImage(scrPtr, image, 0);
710 RReleaseImage(image);
712 if (pixmap)
713 W_PaintText(W_VIEW(panel->rgbGreenS), pixmap->pixmap, panel->font12,
714 2, 0, 100, WALeft, WMColorGC(scrPtr->white), False, _("Green"),
715 strlen(_("Green")));
716 else
717 wwarning(_("Color Panel: Could not allocate memory"));
719 WMSetSliderImage(panel->rgbGreenS, pixmap);
720 WMReleasePixmap(pixmap);
722 panel->rgbGreenT = WMCreateTextField(panel->rgbFrm);
723 WMResizeWidget(panel->rgbGreenT, 40, 18);
724 WMMoveWidget(panel->rgbGreenT, 146, 35);
725 WMSetTextFieldAlignment(panel->rgbGreenT, WALeft);
726 WMAddNotificationObserver(rgbTextFieldCallback, panel,
727 WMTextDidEndEditingNotification, panel->rgbGreenT);
730 panel->rgbBlueS = WMCreateSlider(panel->rgbFrm);
731 WMResizeWidget(panel->rgbBlueS, 141, 16);
732 WMMoveWidget(panel->rgbBlueS, 2, 58);
733 WMSetSliderMinValue(panel->rgbBlueS, 0);
734 WMSetSliderMaxValue(panel->rgbBlueS, 255);
735 WMSetSliderKnobThickness(panel->rgbBlueS, knobThickness);
736 WMSetSliderAction(panel->rgbBlueS, rgbSliderCallback, panel);
738 to.red = 0;
739 to.green = 0;
740 to.blue = 255;
742 image = RRenderGradient(141, 16, &from, &to, RGRD_HORIZONTAL);
743 pixmap = WMCreatePixmapFromRImage(scrPtr, image, 0);
744 RReleaseImage(image);
746 if (pixmap)
747 W_PaintText(W_VIEW(panel->rgbBlueS), pixmap->pixmap, panel->font12,
748 2, 0, 100, WALeft, WMColorGC(scrPtr->white), False, _("Blue"),
749 strlen(_("Blue")));
750 else
751 wwarning(_("Color Panel: Could not allocate memory"));
753 WMSetSliderImage(panel->rgbBlueS, pixmap);
754 WMReleasePixmap(pixmap);
756 panel->rgbBlueT = WMCreateTextField(panel->rgbFrm);
757 WMResizeWidget(panel->rgbBlueT, 40, 18);
758 WMMoveWidget(panel->rgbBlueT, 146, 57);
759 WMSetTextFieldAlignment(panel->rgbBlueT, WALeft);
760 WMAddNotificationObserver(rgbTextFieldCallback, panel,
761 WMTextDidEndEditingNotification, panel->rgbBlueT);
762 /* End of RGB Panel */
764 /* Widgets for CMYK Panel */
765 panel->cmykFrm = WMCreateFrame(panel->slidersFrm);
766 WMSetFrameRelief(panel->cmykFrm, WRFlat);
767 WMResizeWidget(panel->cmykFrm, PWIDTH - 8, PHEIGHT - 80 - 26 - 32);
768 WMMoveWidget(panel->cmykFrm, 0, 34);
770 panel->cmykMinL = WMCreateLabel(panel->cmykFrm);
771 WMResizeWidget(panel->cmykMinL, 20, 10);
772 WMMoveWidget(panel->cmykMinL, 2, 2);
773 WMSetLabelText(panel->cmykMinL, "0");
774 WMSetLabelTextAlignment(panel->cmykMinL, WALeft);
775 WMSetLabelTextColor(panel->cmykMinL, textcolor);
776 WMSetLabelFont(panel->cmykMinL, panel->font8);
778 panel->cmykMaxL = WMCreateLabel(panel->cmykFrm);
779 WMResizeWidget(panel->cmykMaxL, 40, 10);
780 WMMoveWidget(panel->cmykMaxL, 104, 2);
781 WMSetLabelText(panel->cmykMaxL, "100");
782 WMSetLabelTextAlignment(panel->cmykMaxL, WARight);
783 WMSetLabelTextColor(panel->cmykMaxL, textcolor);
784 WMSetLabelFont(panel->cmykMaxL, panel->font8);
786 panel->cmykCyanS = WMCreateSlider(panel->cmykFrm);
787 WMResizeWidget(panel->cmykCyanS, 141, 16);
788 WMMoveWidget(panel->cmykCyanS, 2, 14);
789 WMSetSliderMinValue(panel->cmykCyanS, 0);
790 WMSetSliderMaxValue(panel->cmykCyanS, 100);
791 WMSetSliderKnobThickness(panel->cmykCyanS, knobThickness);
792 WMSetSliderAction(panel->cmykCyanS, cmykSliderCallback, panel);
794 from.red = 255;
795 from.green = 255;
796 from.blue = 255;
798 to.red = 0;
799 to.green = 255;
800 to.blue = 255;
802 image = RRenderGradient(141, 16, &from, &to, RGRD_HORIZONTAL);
803 pixmap = WMCreatePixmapFromRImage(scrPtr, image, 0);
804 RReleaseImage(image);
806 if (pixmap)
807 W_PaintText(W_VIEW(panel->cmykCyanS), pixmap->pixmap, panel->font12,
808 2, 0, 100, WALeft, WMColorGC(scrPtr->black), False, _("Cyan"),
809 strlen(_("Cyan")));
810 else
811 wwarning(_("Color Panel: Could not allocate memory"));
813 WMSetSliderImage(panel->cmykCyanS, pixmap);
814 WMReleasePixmap(pixmap);
816 panel->cmykCyanT = WMCreateTextField(panel->cmykFrm);
817 WMResizeWidget(panel->cmykCyanT, 40, 18);
818 WMMoveWidget(panel->cmykCyanT, 146, 13);
819 WMSetTextFieldAlignment(panel->cmykCyanT, WALeft);
820 WMAddNotificationObserver(cmykTextFieldCallback, panel,
821 WMTextDidEndEditingNotification, panel->cmykCyanT);
824 panel->cmykMagentaS = WMCreateSlider(panel->cmykFrm);
825 WMResizeWidget(panel->cmykMagentaS, 141, 16);
826 WMMoveWidget(panel->cmykMagentaS, 2, 36);
827 WMSetSliderMinValue(panel->cmykMagentaS, 0);
828 WMSetSliderMaxValue(panel->cmykMagentaS, 100);
829 WMSetSliderKnobThickness(panel->cmykMagentaS, knobThickness);
830 WMSetSliderAction(panel->cmykMagentaS, cmykSliderCallback, panel);
832 to.red = 255;
833 to.green = 0;
834 to.blue = 255;
836 image = RRenderGradient(141, 16, &from, &to, RGRD_HORIZONTAL);
837 pixmap = WMCreatePixmapFromRImage(scrPtr, image, 0);
838 RReleaseImage(image);
840 if (pixmap)
841 W_PaintText(W_VIEW(panel->cmykMagentaS), pixmap->pixmap, panel->font12,
842 2, 0, 100, WALeft, WMColorGC(scrPtr->black), False, _("Magenta"),
843 strlen(_("Magenta")));
844 else
845 wwarning(_("Color Panel: Could not allocate memory"));
847 WMSetSliderImage(panel->cmykMagentaS, pixmap);
848 WMReleasePixmap(pixmap);
850 panel->cmykMagentaT = WMCreateTextField(panel->cmykFrm);
851 WMResizeWidget(panel->cmykMagentaT, 40, 18);
852 WMMoveWidget(panel->cmykMagentaT, 146, 35);
853 WMSetTextFieldAlignment(panel->cmykMagentaT, WALeft);
854 WMAddNotificationObserver(cmykTextFieldCallback, panel,
855 WMTextDidEndEditingNotification, panel->cmykMagentaT);
858 panel->cmykYellowS = WMCreateSlider(panel->cmykFrm);
859 WMResizeWidget(panel->cmykYellowS, 141, 16);
860 WMMoveWidget(panel->cmykYellowS, 2, 58);
861 WMSetSliderMinValue(panel->cmykYellowS, 0);
862 WMSetSliderMaxValue(panel->cmykYellowS, 100);
863 WMSetSliderKnobThickness(panel->cmykYellowS, knobThickness);
864 WMSetSliderAction(panel->cmykYellowS, cmykSliderCallback, panel);
866 to.red = 255;
867 to.green = 255;
868 to.blue = 0;
870 image = RRenderGradient(141, 16, &from, &to, RGRD_HORIZONTAL);
871 pixmap = WMCreatePixmapFromRImage(scrPtr, image, 0);
872 RReleaseImage(image);
874 if (pixmap)
875 W_PaintText(W_VIEW(panel->cmykYellowS), pixmap->pixmap, panel->font12,
876 2, 0, 100, WALeft, WMColorGC(scrPtr->black), False, _("Yellow"),
877 strlen(_("Yellow")));
878 else
879 wwarning(_("Color Panel: Could not allocate memory"));
881 WMSetSliderImage(panel->cmykYellowS, pixmap);
882 WMReleasePixmap(pixmap);
884 panel->cmykYellowT = WMCreateTextField(panel->cmykFrm);
885 WMResizeWidget(panel->cmykYellowT, 40, 18);
886 WMMoveWidget(panel->cmykYellowT, 146, 57);
887 WMSetTextFieldAlignment(panel->cmykYellowT, WALeft);
888 WMAddNotificationObserver(cmykTextFieldCallback, panel,
889 WMTextDidEndEditingNotification, panel->cmykYellowT);
892 panel->cmykBlackS = WMCreateSlider(panel->cmykFrm);
893 WMResizeWidget(panel->cmykBlackS, 141, 16);
894 WMMoveWidget(panel->cmykBlackS, 2, 80);
895 WMSetSliderMinValue(panel->cmykBlackS, 0);
896 WMSetSliderMaxValue(panel->cmykBlackS, 100);
897 WMSetSliderValue(panel->cmykBlackS, 0);
898 WMSetSliderKnobThickness(panel->cmykBlackS, knobThickness);
899 WMSetSliderAction(panel->cmykBlackS, cmykSliderCallback, panel);
901 to.red = 0;
902 to.green = 0;
903 to.blue = 0;
905 image = RRenderGradient(141, 16, &from, &to, RGRD_HORIZONTAL);
906 pixmap = WMCreatePixmapFromRImage(scrPtr, image, 0);
907 RReleaseImage(image);
909 if (pixmap)
910 W_PaintText(W_VIEW(panel->cmykBlackS), pixmap->pixmap, panel->font12,
911 2, 0, 100, WALeft, WMColorGC(scrPtr->black), False, _("Black"),
912 strlen(_("Black")));
913 else
914 wwarning(_("Color Panel: Could not allocate memory"));
916 WMSetSliderImage(panel->cmykBlackS, pixmap);
917 WMReleasePixmap(pixmap);
919 panel->cmykBlackT = WMCreateTextField(panel->cmykFrm);
920 WMResizeWidget(panel->cmykBlackT, 40, 18);
921 WMMoveWidget(panel->cmykBlackT, 146, 79);
922 WMSetTextFieldAlignment(panel->cmykBlackT, WALeft);
923 WMAddNotificationObserver(cmykTextFieldCallback, panel,
924 WMTextDidEndEditingNotification, panel->cmykBlackT);
925 /* End of CMYK Panel */
927 /* Widgets for HSB Panel */
928 panel->hsbFrm = WMCreateFrame(panel->slidersFrm);
929 WMSetFrameRelief(panel->hsbFrm, WRFlat);
930 WMResizeWidget(panel->hsbFrm, PWIDTH - 8, PHEIGHT - 80 - 26 - 32);
931 WMMoveWidget(panel->hsbFrm, 0, 34);
933 panel->hsbHueS = WMCreateSlider(panel->hsbFrm);
934 WMResizeWidget(panel->hsbHueS, 141, 16);
935 WMMoveWidget(panel->hsbHueS, 2, 14);
936 WMSetSliderMinValue(panel->hsbHueS, 0);
937 WMSetSliderMaxValue(panel->hsbHueS, 359);
938 WMSetSliderKnobThickness(panel->hsbHueS, knobThickness);
939 WMSetSliderAction(panel->hsbHueS, hsbSliderCallback, panel);
941 panel->hsbHueT = WMCreateTextField(panel->hsbFrm);
942 WMResizeWidget(panel->hsbHueT, 40, 18);
943 WMMoveWidget(panel->hsbHueT, 146, 13);
944 WMSetTextFieldAlignment(panel->hsbHueT, WALeft);
945 WMAddNotificationObserver(hsbTextFieldCallback, panel,
946 WMTextDidEndEditingNotification, panel->hsbHueT);
949 panel->hsbSaturationS = WMCreateSlider(panel->hsbFrm);
950 WMResizeWidget(panel->hsbSaturationS, 141, 16);
951 WMMoveWidget(panel->hsbSaturationS, 2, 36);
952 WMSetSliderMinValue(panel->hsbSaturationS, 0);
953 WMSetSliderMaxValue(panel->hsbSaturationS, 100);
954 WMSetSliderKnobThickness(panel->hsbSaturationS, knobThickness);
955 WMSetSliderAction(panel->hsbSaturationS, hsbSliderCallback, panel);
957 panel->hsbSaturationT = WMCreateTextField(panel->hsbFrm);
958 WMResizeWidget(panel->hsbSaturationT, 40, 18);
959 WMMoveWidget(panel->hsbSaturationT, 146, 35);
960 WMSetTextFieldAlignment(panel->hsbSaturationT, WALeft);
961 WMAddNotificationObserver(hsbTextFieldCallback, panel,
962 WMTextDidEndEditingNotification, panel->hsbSaturationT);
965 panel->hsbBrightnessS = WMCreateSlider(panel->hsbFrm);
966 WMResizeWidget(panel->hsbBrightnessS, 141, 16);
967 WMMoveWidget(panel->hsbBrightnessS, 2, 58);
968 WMSetSliderMinValue(panel->hsbBrightnessS, 0);
969 WMSetSliderMaxValue(panel->hsbBrightnessS, 100);
970 WMSetSliderKnobThickness(panel->hsbBrightnessS, knobThickness);
971 WMSetSliderAction(panel->hsbBrightnessS, hsbSliderCallback, panel);
973 panel->hsbBrightnessT = WMCreateTextField(panel->hsbFrm);
974 WMResizeWidget(panel->hsbBrightnessT, 40, 18);
975 WMMoveWidget(panel->hsbBrightnessT, 146, 57);
976 WMSetTextFieldAlignment(panel->hsbBrightnessT, WALeft);
977 WMAddNotificationObserver(hsbTextFieldCallback, panel,
978 WMTextDidEndEditingNotification, panel->hsbBrightnessT);
979 /* End of HSB Panel */
982 WMReleaseColor(textcolor);
984 /* Widgets for the CustomPalette Panel */
985 panel->customPaletteFrm = WMCreateFrame(panel->win);
986 WMSetFrameRelief(panel->customPaletteFrm, WRFlat);
987 WMResizeWidget(panel->customPaletteFrm, PWIDTH - 8, PHEIGHT - 80 - 26);
988 WMMoveWidget(panel->customPaletteFrm, 5, 80);
990 panel->customPaletteHistoryBtn = WMCreatePopUpButton(
991 panel->customPaletteFrm);
992 WMAddPopUpButtonItem(panel->customPaletteHistoryBtn, _("Spectrum"));
993 WMSetPopUpButtonSelectedItem(panel->customPaletteHistoryBtn,
994 WMGetPopUpButtonNumberOfItems(panel->customPaletteHistoryBtn)-1);
995 WMSetPopUpButtonAction(panel->customPaletteHistoryBtn,
996 customPaletteHistoryCallback, panel);
997 WMResizeWidget(panel->customPaletteHistoryBtn, PWIDTH - 8, 20);
998 WMMoveWidget(panel->customPaletteHistoryBtn, 0, 0);
1000 panel->customPaletteContentFrm = WMCreateFrame(panel->customPaletteFrm);
1001 WMSetFrameRelief(panel->customPaletteContentFrm, WRSunken);
1002 WMResizeWidget(panel->customPaletteContentFrm, PWIDTH - 8, PHEIGHT - 156);
1003 WMMoveWidget(panel->customPaletteContentFrm, 0, 23);
1005 panel->customPaletteContentView = W_CreateView(
1006 W_VIEW(panel->customPaletteContentFrm));
1007 /* XXX Test if we can create a view */
1008 W_ResizeView(panel->customPaletteContentView, customPaletteWidth,
1009 customPaletteHeight);
1010 W_MoveView(panel->customPaletteContentView, 2, 2);
1012 /* Create event handler to handle expose/click events in CustomPalette */
1013 WMCreateEventHandler(panel->customPaletteContentView,
1014 ButtonPressMask|ButtonReleaseMask|EnterWindowMask| LeaveWindowMask |
1015 ButtonMotionMask, customPaletteHandleActionEvents, panel);
1017 WMCreateEventHandler(panel->customPaletteContentView, ExposureMask,
1018 customPaletteHandleEvents, panel);
1020 panel->customPaletteMenuBtn = WMCreatePopUpButton(panel->customPaletteFrm);
1021 WMSetPopUpButtonPullsDown(panel->customPaletteMenuBtn, 1);
1022 WMSetPopUpButtonText(panel->customPaletteMenuBtn, _("Palette"));
1023 WMSetPopUpButtonAction(panel->customPaletteMenuBtn,
1024 customPaletteMenuCallback, panel);
1025 WMResizeWidget(panel->customPaletteMenuBtn, PWIDTH - 8, 20);
1026 WMMoveWidget(panel->customPaletteMenuBtn, 0, PHEIGHT - 130);
1028 WMAddPopUpButtonItem(panel->customPaletteMenuBtn, _("New from File..."));
1029 WMAddPopUpButtonItem(panel->customPaletteMenuBtn, _("Rename..."));
1030 WMAddPopUpButtonItem(panel->customPaletteMenuBtn, _("Remove"));
1031 WMAddPopUpButtonItem(panel->customPaletteMenuBtn, _("Copy"));
1032 WMAddPopUpButtonItem(panel->customPaletteMenuBtn, _("New from Clipboard"));
1034 WMSetPopUpButtonItemEnabled(panel->customPaletteMenuBtn, CPmenuRename, 0);
1035 WMSetPopUpButtonItemEnabled(panel->customPaletteMenuBtn, CPmenuRemove, 0);
1036 WMSetPopUpButtonItemEnabled(panel->customPaletteMenuBtn, CPmenuCopy, 0);
1037 WMSetPopUpButtonItemEnabled(panel->customPaletteMenuBtn,
1038 CPmenuNewFromClipboard, 0);
1040 customRenderSpectrum(panel);
1041 panel->currentPalette = 0;
1042 panel->palx = customPaletteWidth/2;
1043 panel->paly = customPaletteHeight/2;
1046 /* Widgets for the ColorList Panel */
1047 panel->colorListFrm = WMCreateFrame(panel->win);
1048 WMSetFrameRelief(panel->colorListFrm, WRFlat);
1049 WMResizeWidget(panel->colorListFrm, PWIDTH - 8, PHEIGHT - 80 - 26);
1050 WMMoveWidget(panel->colorListFrm, 5, 80);
1052 panel->colorListHistoryBtn = WMCreatePopUpButton(panel->colorListFrm);
1053 WMAddPopUpButtonItem(panel->colorListHistoryBtn, _("X11-Colors"));
1054 WMSetPopUpButtonSelectedItem(panel->colorListHistoryBtn,
1055 WMGetPopUpButtonNumberOfItems(panel->colorListHistoryBtn)-1);
1056 /* WMSetPopUpButtonAction(panel->colorListHistoryBtn,
1057 * colorListHistoryCallback, panel); */
1058 WMResizeWidget(panel->colorListHistoryBtn, PWIDTH - 8, 20);
1059 WMMoveWidget(panel->colorListHistoryBtn, 0, 0);
1061 panel->colorListContentLst = WMCreateList(panel->colorListFrm);
1062 WMSetListAction(panel->colorListContentLst, colorListSelect, panel);
1063 WMSetListUserDrawProc(panel->colorListContentLst, colorListPaintItem);
1064 WMResizeWidget(panel->colorListContentLst, PWIDTH - 8, PHEIGHT - 156);
1065 WMMoveWidget(panel->colorListContentLst, 0, 23);
1066 WMHangData(panel->colorListContentLst, panel);
1068 panel->colorListColorMenuBtn = WMCreatePopUpButton(panel->colorListFrm);
1069 WMSetPopUpButtonPullsDown(panel->colorListColorMenuBtn, 1);
1070 WMSetPopUpButtonText(panel->colorListColorMenuBtn, _("Color"));
1071 WMSetPopUpButtonAction(panel->colorListColorMenuBtn,
1072 colorListColorMenuCallback, panel);
1073 WMResizeWidget(panel->colorListColorMenuBtn, (PWIDTH - 16)/2, 20);
1074 WMMoveWidget(panel->colorListColorMenuBtn, 0, PHEIGHT - 130);
1076 WMAddPopUpButtonItem(panel->colorListColorMenuBtn, _("Add..."));
1077 WMAddPopUpButtonItem(panel->colorListColorMenuBtn, _("Rename..."));
1078 WMAddPopUpButtonItem(panel->colorListColorMenuBtn, _("Remove"));
1080 WMSetPopUpButtonItemEnabled(panel->colorListColorMenuBtn, CLmenuAdd, 0);
1081 WMSetPopUpButtonItemEnabled(panel->colorListColorMenuBtn, CLmenuRename, 0);
1082 WMSetPopUpButtonItemEnabled(panel->colorListColorMenuBtn, CLmenuRemove, 0);
1084 panel->colorListListMenuBtn = WMCreatePopUpButton(panel->colorListFrm);
1085 WMSetPopUpButtonPullsDown(panel->colorListListMenuBtn, 1);
1086 WMSetPopUpButtonText(panel->colorListListMenuBtn, _("List"));
1087 WMSetPopUpButtonAction(panel->colorListListMenuBtn,
1088 colorListListMenuCallback, panel);
1089 WMResizeWidget(panel->colorListListMenuBtn, (PWIDTH - 16)/2, 20);
1090 WMMoveWidget(panel->colorListListMenuBtn, (PWIDTH - 16)/2 + 8,
1091 PHEIGHT - 130);
1093 WMAddPopUpButtonItem(panel->colorListListMenuBtn, _("New..."));
1094 WMAddPopUpButtonItem(panel->colorListListMenuBtn, _("Rename..."));
1095 WMAddPopUpButtonItem(panel->colorListListMenuBtn, _("Remove"));
1097 WMSetPopUpButtonItemEnabled(panel->colorListListMenuBtn, CLmenuAdd, 0);
1098 WMSetPopUpButtonItemEnabled(panel->colorListListMenuBtn, CLmenuRename, 0);
1099 WMSetPopUpButtonItemEnabled(panel->colorListListMenuBtn, CLmenuRemove, 0);
1101 WMRealizeWidget(panel->win);
1102 WMMapSubwidgets(panel->win);
1104 WMMapSubwidgets(panel->wheelFrm);
1105 WMMapSubwidgets(panel->slidersFrm);
1106 WMMapSubwidgets(panel->grayFrm);
1107 WMMapSubwidgets(panel->rgbFrm);
1108 WMMapSubwidgets(panel->cmykFrm);
1109 WMMapSubwidgets(panel->hsbFrm);
1110 WMMapSubwidgets(panel->customPaletteFrm);
1111 WMMapSubwidgets(panel->customPaletteContentFrm);
1112 WMMapSubwidgets(panel->colorListFrm);
1114 /* Pixmap to indicate selection positions
1115 * wheelframe MUST be mapped.
1117 panel->selectionImg = XCreatePixmap(scrPtr->display,
1118 WMWidgetXID(panel->win), 4, 4, scrPtr->depth);
1119 XFillRectangle(scrPtr->display, panel->selectionImg, bgc, 0, 0, 4, 4);
1120 XFillRectangle(scrPtr->display, panel->selectionImg, wgc, 1, 1, 2, 2);
1122 readConfiguration(panel);
1123 readXColors(panel);
1125 return panel;
1129 WMColorPanel*
1130 WMGetColorPanel(WMScreen *scrPtr)
1132 WMColorPanel *panel;
1134 if (scrPtr->sharedColorPanel)
1135 return scrPtr->sharedColorPanel;
1137 panel = makeColorPanel(scrPtr, "colorPanel");
1139 scrPtr->sharedColorPanel = panel;
1141 return panel;
1145 void
1146 WMFreeColorPanel(WMColorPanel *panel)
1148 W_Screen *scr = WMWidgetScreen(panel->win);
1150 if (panel == scr->sharedColorPanel) {
1151 scr->sharedColorPanel = NULL;
1154 if (!panel)
1155 return;
1157 WMRemoveNotificationObserver(panel);
1158 WMUnmapWidget(panel->win);
1160 /* fonts */
1161 WMReleaseFont(panel->font8);
1162 WMReleaseFont(panel->font12);
1164 /* pixmaps */
1165 wheelDestroyMatrix(panel->wheelMtrx);
1166 if (panel->wheelImg)
1167 XFreePixmap(scr->display, panel->wheelImg);
1168 if (panel->selectionImg)
1169 XFreePixmap(scr->display, panel->selectionImg);
1170 if (panel->selectionBackImg)
1171 XFreePixmap(scr->display, panel->selectionBackImg);
1172 RReleaseImage(panel->customPaletteImg);
1174 /* structs */
1175 if (panel->lastBrowseDir)
1176 wfree(panel->lastBrowseDir);
1177 if (panel->configurationPath)
1178 wfree(panel->configurationPath);
1180 WMDestroyWidget(panel->win);
1182 wfree(panel);
1186 void
1187 WMCloseColorPanel(WMColorPanel *panel)
1189 WMFreeColorPanel(panel);
1193 void
1194 WMShowColorPanel(WMColorPanel *panel)
1196 WMScreen *scr = WMWidgetScreen(panel->win);
1197 WMColor *white = WMWhiteColor(scr);
1199 if (panel->color.set == cpNone)
1200 WMSetColorPanelColor(panel, white);
1201 WMReleaseColor(white);
1203 if (panel->mode != WMWheelModeColorPanel)
1204 WMPerformButtonClick(panel->wheelBtn);
1206 WMMapWidget(panel->win);
1210 static void
1211 closeWindowCallback(WMWidget *w, void *data)
1213 W_ColorPanel *panel = (W_ColorPanel*)data;
1215 WMCloseColorPanel(panel);
1219 static void
1220 readConfiguration(W_ColorPanel *panel)
1222 /* XXX Doesn't take care of "invalid" files */
1224 DIR *dPtr;
1225 struct dirent *dp;
1226 struct stat stat_buf;
1227 int item;
1229 if (stat(panel->configurationPath, &stat_buf)!=0) {
1230 if (mkdir(panel->configurationPath,
1231 S_IRWXU|S_IRGRP|S_IROTH|S_IXGRP|S_IXOTH)!=0) {
1232 wsyserror(_("Color Panel: Could not create directory %s needed"
1233 " to store configurations"), panel->configurationPath);
1234 WMSetPopUpButtonEnabled(panel->customPaletteMenuBtn, False);
1235 WMSetPopUpButtonEnabled(panel->colorListColorMenuBtn, False);
1236 WMSetPopUpButtonEnabled(panel->colorListListMenuBtn, False);
1237 WMRunAlertPanel(WMWidgetScreen(panel->win), panel->win,
1238 _("File Error"),
1239 _("Could not create ColorPanel configuration directory"),
1240 _("OK"), NULL, NULL);
1242 return;
1245 if (!(dPtr = opendir(panel->configurationPath))) {
1246 wwarning(_("Color Panel: Could not find file"), "%s", panel->configurationPath);
1247 return;
1250 while ((dp = readdir(dPtr)) != NULL) {
1251 unsigned int perm_mask;
1252 char *path = wstrconcat(panel->configurationPath,
1253 dp->d_name);
1255 if (dp->d_name[0] != '.') {
1256 item = WMGetPopUpButtonNumberOfItems(
1257 panel->customPaletteHistoryBtn);
1258 WMAddPopUpButtonItem(panel->customPaletteHistoryBtn, dp->d_name);
1260 perm_mask = (access(path, R_OK) == 0);
1261 WMSetPopUpButtonItemEnabled(panel->customPaletteHistoryBtn,
1262 item, perm_mask);
1264 wfree(path);
1266 (void)closedir(dPtr);
1270 static void
1271 readXColors(W_ColorPanel *panel)
1273 struct stat stat_buf;
1274 FILE *rgbtxt;
1275 char line[MAX_LENGTH];
1276 int red, green, blue;
1277 char name[48];
1278 RColor *color;
1279 WMListItem *item;
1281 if (stat(RGBTXT, &stat_buf) != 0) {
1282 wsyserror(_("Color Panel: Could not find file"), " %s", RGBTXT);
1283 return;
1285 else {
1286 if ((rgbtxt = fopen(RGBTXT, "r"))) {
1287 while (fgets(line, MAX_LENGTH, rgbtxt)) {
1288 if (sscanf(line, "%d%d%d %[^\n]", &red, &green, &blue, name)) {
1289 color = wmalloc(sizeof(RColor));
1290 color->red = (unsigned char)red;
1291 color->green = (unsigned char)green;
1292 color->blue = (unsigned char)blue;
1293 item = WMAddListItem(panel->colorListContentLst, name);
1294 item->clientData = (void *)color;
1297 fclose(rgbtxt);
1299 else {
1300 wsyserror(_("Color Panel: Could not find file"), "%s", RGBTXT);
1306 void
1307 WMSetColorPanelPickerMode(WMColorPanel *panel, WMColorPanelMode mode)
1309 W_Screen *scr = WMWidgetScreen(panel->win);
1311 if (mode != WMWheelModeColorPanel) {
1312 WMUnmapWidget(panel->wheelFrm);
1313 if (panel->selectionBackImg) {
1314 XFreePixmap(WMWidgetScreen(panel->win)->display,
1315 panel->selectionBackImg);
1316 panel->selectionBackImg = None;
1319 if (mode != WMGrayModeColorPanel)
1320 WMUnmapWidget(panel->grayFrm);
1321 if (mode != WMRGBModeColorPanel)
1322 WMUnmapWidget(panel->rgbFrm);
1323 if (mode != WMCMYKModeColorPanel)
1324 WMUnmapWidget(panel->cmykFrm);
1325 if (mode != WMHSBModeColorPanel)
1326 WMUnmapWidget(panel->hsbFrm);
1327 if (mode != WMCustomPaletteModeColorPanel) {
1328 WMUnmapWidget(panel->customPaletteFrm);
1329 if (panel->selectionBackImg) {
1330 XFreePixmap(WMWidgetScreen(panel->win)->display,
1331 panel->selectionBackImg);
1332 panel->selectionBackImg = None;
1335 if (mode != WMColorListModeColorPanel)
1336 WMUnmapWidget(panel->colorListFrm);
1337 if ((mode != WMGrayModeColorPanel) && (mode != WMRGBModeColorPanel) &&
1338 (mode != WMCMYKModeColorPanel) && (mode != WMHSBModeColorPanel))
1339 WMUnmapWidget(panel->slidersFrm);
1340 else
1341 panel->slidersmode = mode;
1343 if (mode == WMWheelModeColorPanel) {
1344 WMMapWidget(panel->wheelFrm);
1345 WMSetButtonSelected(panel->wheelBtn, True);
1346 if (panel->lastChanged != WMWheelModeColorPanel)
1347 wheelInit(panel);
1348 wheelRender(panel);
1349 wheelPaint(panel);
1350 } else if (mode == WMGrayModeColorPanel) {
1351 WMMapWidget(panel->slidersFrm);
1352 WMSetButtonSelected(panel->slidersBtn, True);
1353 WMMapWidget(panel->grayFrm);
1354 WMSetButtonSelected(panel->grayBtn, True);
1355 WMSetButtonImage(panel->slidersBtn, scr->grayIcon);
1356 if (panel->lastChanged != WMGrayModeColorPanel)
1357 grayInit(panel);
1358 } else if (mode == WMRGBModeColorPanel) {
1359 WMMapWidget(panel->slidersFrm);
1360 WMSetButtonSelected(panel->slidersBtn, True);
1361 WMMapWidget(panel->rgbFrm);
1362 WMSetButtonSelected(panel->rgbBtn, True);
1363 WMSetButtonImage(panel->slidersBtn, scr->rgbIcon);
1364 if (panel->lastChanged != WMRGBModeColorPanel)
1365 rgbInit(panel);
1366 } else if (mode == WMCMYKModeColorPanel) {
1367 WMMapWidget(panel->slidersFrm);
1368 WMSetButtonSelected(panel->slidersBtn, True);
1369 WMMapWidget(panel->cmykFrm);
1370 WMSetButtonSelected(panel->cmykBtn, True);
1371 WMSetButtonImage(panel->slidersBtn, scr->cmykIcon);
1372 if (panel->lastChanged != WMCMYKModeColorPanel)
1373 cmykInit(panel);
1374 } else if (mode == WMHSBModeColorPanel) {
1375 WMMapWidget(panel->slidersFrm);
1376 WMSetButtonSelected(panel->slidersBtn, True);
1377 WMMapWidget(panel->hsbFrm);
1378 WMSetButtonSelected(panel->hsbBtn, True);
1379 WMSetButtonImage(panel->slidersBtn, scr->hsbIcon);
1380 if (panel->lastChanged != WMHSBModeColorPanel)
1381 hsbInit(panel);
1382 } else if (mode == WMCustomPaletteModeColorPanel) {
1383 WMMapWidget(panel->customPaletteFrm);
1384 WMSetButtonSelected(panel->customPaletteBtn, True);
1385 customSetPalette(panel);
1386 } else if (mode == WMColorListModeColorPanel) {
1387 WMMapWidget(panel->colorListFrm);
1388 WMSetButtonSelected(panel->colorListBtn, True);
1391 panel->mode = mode;
1395 WMColor*
1396 WMGetColorPanelColor(WMColorPanel *panel)
1398 return WMGetColorWellColor(panel->colorWell);
1402 void
1403 WMSetColorPanelColor(WMColorPanel *panel, WMColor *color)
1405 WMSetColorWellColor(panel->colorWell, color);
1407 panel->color.rgb.red = color->color.red >> 8;
1408 panel->color.rgb.green = color->color.green >> 8;
1409 panel->color.rgb.blue = color->color.blue >> 8;
1410 panel->color.set = cpRGB;
1412 if (panel->mode == panel->lastChanged)
1413 panel->lastChanged = 0;
1415 WMSetColorPanelPickerMode(panel, panel->mode);
1419 static void
1420 updateSwatch(WMColorPanel *panel, CPColor color)
1422 WMScreen *scr = WMWidgetScreen(panel->win);
1423 WMColor *wellcolor;
1425 if (color.set != cpRGB)
1426 convertCPColor(&color);
1428 panel->color = color;
1430 wellcolor = WMCreateRGBColor(scr, color.rgb.red << 8,
1431 color.rgb.green << 8,
1432 color.rgb.blue << 8, True);
1434 WMSetColorWellColor(panel->colorWell, wellcolor);
1435 WMReleaseColor(wellcolor);
1437 if (!panel->flags.dragging || panel->flags.continuous) {
1438 if (panel->action)
1439 (*panel->action)(panel, panel->clientData);
1441 WMPostNotificationName(WMColorPanelColorChangedNotification, panel,
1442 NULL);
1446 static void
1447 modeButtonCallback(WMWidget *w, void *data)
1449 W_ColorPanel *panel = (W_ColorPanel*)(data);
1451 if (w == panel->wheelBtn)
1452 WMSetColorPanelPickerMode(panel, WMWheelModeColorPanel);
1453 else if (w == panel->slidersBtn)
1454 WMSetColorPanelPickerMode(panel, panel->slidersmode);
1455 else if (w == panel->customPaletteBtn)
1456 WMSetColorPanelPickerMode(panel, WMCustomPaletteModeColorPanel);
1457 else if (w == panel->colorListBtn)
1458 WMSetColorPanelPickerMode(panel, WMColorListModeColorPanel);
1459 else if (w == panel->grayBtn)
1460 WMSetColorPanelPickerMode(panel, WMGrayModeColorPanel);
1461 else if (w == panel->rgbBtn)
1462 WMSetColorPanelPickerMode(panel, WMRGBModeColorPanel);
1463 else if (w == panel->cmykBtn)
1464 WMSetColorPanelPickerMode(panel, WMCMYKModeColorPanel);
1465 else if (w == panel->hsbBtn)
1466 WMSetColorPanelPickerMode(panel, WMHSBModeColorPanel);
1470 /****************** Magnifying Cursor Functions *******************/
1472 static XImage*
1473 magnifyGetImage(WMScreen *scr, XImage *image, int x, int y, int w, int h)
1475 int x0 = 0, y0 = 0, w0 = w, h0 = h;
1476 const int displayWidth = DisplayWidth(scr->display, scr->screen),
1477 displayHeight = DisplayHeight(scr->display, scr->screen);
1479 if (!(image && image->data)) {
1480 /* The image in panel->magnifyGlass->image does not exist yet.
1481 * Grab one from the screen (not beyond) and use it from now on.
1483 if (!(image = XGetImage(scr->display, scr->rootWin,
1484 x - Cursor_x_hot,
1485 y - Cursor_y_hot,
1486 w, h, AllPlanes, ZPixmap)))
1487 wwarning(_("Color Panel: X failed request"));
1489 return image;
1492 /* Coordinate correction for back pixmap
1493 * if magnifying glass is at screen-borders
1496 /* Figure 1: Shifting of rectangle-to-grab at top/left screen borders
1497 * Hatched area is beyond screen border.
1499 * |<-Cursor_x_hot->|
1500 * ________________|_____
1501 * |/ / / / / / /| | |
1502 * | / / / / / / |(x,y) |
1503 * |/_/_/_/_/_/_/|________|
1504 * |<----x0----->|<--w0-->|
1508 /* Figure 2: Shifting of rectangle-to-grab at bottom/right
1509 * screen borders
1510 * Hatched area is beyond screen border
1512 * |<-Cursor_x_hot->|
1513 * ________________|_______________
1514 * | | | / / / / / /|
1515 * | (x,y)|/ / / / / / |
1516 * |___________________|_/_/_/_/_/_/|
1517 * |<-------w0-------->| |
1518 * |<---------------w--|----------->|
1519 * | |
1520 * x0 Displaywidth-1
1523 if (x < Cursor_x_hot) { /* see fig. 1 */
1524 x0 = Cursor_x_hot - x;
1525 w0 = w - x0;
1528 if (displayWidth -1 < x - Cursor_x_hot + w) { /* see fig. 2 */
1529 w0 = (displayWidth) - (x - Cursor_x_hot);
1532 if (y < Cursor_y_hot) { /* see fig. 1 */
1533 y0 = Cursor_y_hot - y;
1534 h0 = h - y0;
1537 if (displayHeight -1 < y - Cursor_y_hot + h) { /* see fig. 2 */
1538 h0 = (displayHeight) - (y - Cursor_y_hot);
1540 /* end of coordinate correction */
1543 /* Grab an image from the screen, clipped if necessary,
1544 * and put it in the existing panel->magnifyGlass->image
1545 * with the corresponding clipping offset.
1547 if (!XGetSubImage(scr->display, scr->rootWin,
1548 x - Cursor_x_hot + x0,
1549 y - Cursor_y_hot + y0,
1550 w0, h0, AllPlanes, ZPixmap,
1551 image, x0, y0))
1552 wwarning(_("Color Panel: X failed request"));
1554 return NULL;
1558 static void
1559 magnifyGetImageStored(WMColorPanel *panel, int x1, int y1, int x2, int y2)
1561 /* (x1, y1) = topleft corner of existing rectangle
1562 * (x2, y2) = topleft corner of new position
1565 W_Screen *scr = WMWidgetScreen(panel->win);
1566 int xa = 0, ya = 0, xb = 0, yb = 0;
1567 int width, height;
1568 const int dx = abs(x2 - x1),
1569 dy = abs(y2 - y1);
1570 XImage *image;
1571 const int x_min = Cursor_x_hot,
1572 y_min = Cursor_y_hot,
1573 x_max = DisplayWidth(scr->display, scr->screen) -1 -
1574 (Cursor_mask_width - Cursor_x_hot),
1575 y_max = DisplayHeight(scr->display, scr->screen) -1 -
1576 (Cursor_mask_height - Cursor_y_hot);
1578 if ((dx == 0) && (dy == 0) && panel->magnifyGlass->image)
1579 return; /* No movement */
1581 if (x1 < x2)
1582 xa = dx;
1583 else
1584 xb = dx;
1586 if (y1 < y2)
1587 ya = dy;
1588 else
1589 yb = dy;
1591 width = Cursor_mask_width - dx;
1592 height = Cursor_mask_height - dy;
1594 /* If the traversed distance is larger than the size of the magnifying
1595 * glass contents, there is no need to do dirty rectangles. A whole new
1596 * rectangle can be grabbed (unless that rectangle falls partially
1597 * off screen).
1598 * Destroying the image and setting it to NULL will achieve that later on.
1600 * Of course, grabbing an XImage beyond the borders of the screen will
1601 * cause trouble, this is considdered a special case. Part of the screen
1602 * is grabbed, but there is no need for dirty rectangles.
1604 if ((width <= 0) || (height <= 0)) {
1605 if ((x2 >= x_min) && (y2 >= y_min) && (x2 <= x_max) && (y2 <= y_max)) {
1606 if (panel->magnifyGlass->image)
1607 XDestroyImage(panel->magnifyGlass->image);
1608 panel->magnifyGlass->image = NULL;
1610 } else {
1611 if (panel->magnifyGlass->image) {
1612 /* Get dirty rectangle from panel->magnifyGlass->image */
1613 panel->magnifyGlass->dirtyRect =
1614 XSubImage(panel->magnifyGlass->image, xa, ya, width, height);
1615 if (!panel->magnifyGlass->dirtyRect) {
1616 wwarning(_("Color Panel: X failed request"));
1617 return; /* X returned a NULL from XSubImage */
1622 /* Get image from screen */
1623 image = magnifyGetImage(scr, panel->magnifyGlass->image, x2, y2,
1624 Cursor_mask_width, Cursor_mask_height);
1625 if (image) { /* Only reassign if a *new* image was grabbed */
1626 panel->magnifyGlass->image = image;
1627 return;
1630 /* Copy previously stored rectangle on covered part of image */
1631 if (panel->magnifyGlass->image && panel->magnifyGlass->dirtyRect) {
1632 int old_height;
1634 /* "width" and "height" are used as coordinates here,
1635 * and run from [0...width-1] and [0...height-1] respectively.
1637 width--;
1638 height--;
1639 old_height = height;
1641 for (; width >= 0; width--)
1642 for (height = old_height; height >= 0; height--)
1643 XPutPixel(panel->magnifyGlass->image, xb + width, yb + height,
1644 XGetPixel(panel->magnifyGlass->dirtyRect, width, height));
1645 XDestroyImage(panel->magnifyGlass->dirtyRect);
1646 panel->magnifyGlass->dirtyRect = NULL;
1649 return;
1653 static Pixmap
1654 magnifyCreatePixmap(WMColorPanel *panel)
1656 W_Screen *scr = WMWidgetScreen(panel->win);
1657 int u, v;
1658 #ifndef SHAPE
1659 Pixmap pixmap;
1660 #endif
1661 unsigned long color;
1663 if (!panel->magnifyGlass->image)
1664 return None;
1666 if (!panel->magnifyGlass->magPix)
1667 return None;
1670 * Copy an area of only 5x5 pixels from the center of the image.
1672 for (u = 0; u < 5; u++) {
1673 for (v = 0; v < 5; v++) {
1674 color = XGetPixel(panel->magnifyGlass->image, u + 9, v + 9);
1676 XSetForeground(scr->display, scr->copyGC, color);
1678 if ((u == 2) && (v == 2)) /* (2,2) is center pixel (unmagn.) */
1679 panel->magnifyGlass->color = ulongToRColor(scr, color);
1681 /* The center square must eventually be centered around the
1682 * hotspot. The image needs shifting to achieve this. The amount of
1683 * shifting is (Cursor_mask_width/2 - 2 * square_size) = 11-10 = 1
1684 * _ _ _ _ _
1685 * |_|_|_|_|_|
1686 * ^------- center of center square == Cursor_x_hot
1688 XFillRectangle(scr->display, panel->magnifyGlass->magPix,
1689 scr->copyGC,
1690 u * 5 + (u == 0 ? 0 : -1), v * 5 + (v == 0 ? 0 : -1),
1691 (u == 0 ? 4 : 5), (v == 0 ? 4 : 5));
1695 #ifdef SHAPE
1696 return panel->magnifyGlass->magPix;
1697 #else
1698 pixmap = XCreatePixmap(scr->display, W_DRAWABLE(scr), Cursor_mask_width,
1699 Cursor_mask_height, scr->depth);
1700 if (!pixmap)
1701 return None;
1703 XPutImage(scr->display, pixmap, scr->copyGC, panel->magnifyGlass->image,
1704 0, 0, 0, 0, Cursor_mask_width, Cursor_mask_height);
1706 /* Copy the magnified pixmap, with the clip mask, to background pixmap */
1707 XCopyArea(scr->display, panel->magnifyGlass->magPix, pixmap,
1708 scr->clipGC, 0, 0, Cursor_mask_width, Cursor_mask_height, 0, 0);
1709 /* (2,2) puts center pixel on center of glass */
1711 return pixmap;
1712 #endif
1717 static WMView*
1718 magnifyCreateView(W_ColorPanel *panel)
1720 W_Screen *scr = WMWidgetScreen(panel->win);
1721 WMView *magView;
1723 magView = W_CreateTopView(scr);
1724 if (!magView)
1725 return NULL;
1727 magView->self = panel->win;
1728 magView->flags.topLevel = 1;
1729 magView->attribFlags |= CWOverrideRedirect | CWSaveUnder;
1730 magView->attribs.override_redirect = True;
1731 magView->attribs.save_under = True;
1733 W_ResizeView(magView, Cursor_mask_width, Cursor_mask_height);
1735 W_RealizeView(magView);
1737 return magView;
1741 static Cursor
1742 magnifyGrabPointer(W_ColorPanel *panel)
1744 W_Screen *scr = WMWidgetScreen(panel->win);
1745 Pixmap magPixmap, magPixmap2;
1746 Cursor magCursor;
1747 XColor fgColor = {0, 0,0,0, DoRed|DoGreen|DoBlue};
1748 XColor bgColor = {0, 0xbf00, 0xa000, 0x5000, DoRed|DoGreen|DoBlue};
1750 /* Cursor creation stuff */
1751 magPixmap = XCreatePixmapFromBitmapData(scr->display, W_DRAWABLE(scr),
1752 (char *)Cursor_bits, Cursor_width, Cursor_height, 1, 0, 1);
1753 magPixmap2 = XCreatePixmapFromBitmapData(scr->display, W_DRAWABLE(scr),
1754 (char *)Cursor_shape_bits, Cursor_width, Cursor_height, 1, 0, 1);
1756 magCursor = XCreatePixmapCursor(scr->display, magPixmap, magPixmap2,
1757 &fgColor, &bgColor, Cursor_x_hot, Cursor_y_hot);
1759 XFreePixmap(scr->display, magPixmap);
1760 XFreePixmap(scr->display, magPixmap2);
1762 XRecolorCursor(scr->display, magCursor, &fgColor, &bgColor);
1764 /* Set up Pointer */
1765 XGrabPointer (scr->display, panel->magnifyGlass->view->window, True,
1766 PointerMotionMask | ButtonPressMask,
1767 GrabModeAsync, GrabModeAsync,
1768 scr->rootWin, magCursor, CurrentTime);
1770 return magCursor;
1774 static WMPoint
1775 magnifyInitialize(W_ColorPanel *panel)
1777 W_Screen *scr = WMWidgetScreen(panel->win);
1778 int x, y, u, v;
1779 unsigned int mask;
1780 Pixmap pixmap, clip_mask;
1781 WMPoint point;
1782 Window root_return, child_return;
1784 clip_mask = XCreatePixmapFromBitmapData(scr->display, W_DRAWABLE(scr),
1785 (char *)Cursor_mask_bits, Cursor_mask_width, Cursor_mask_height,
1786 1, 0, 1);
1787 panel->magnifyGlass->magPix = XCreatePixmap(scr->display, W_DRAWABLE(scr),
1788 5*5 -1, 5*5 -1, scr->depth);
1790 XQueryPointer(scr->display, scr->rootWin, &root_return, &child_return,
1791 &x, &y, &u, &v, &mask);
1793 panel->magnifyGlass->image = NULL;
1795 /* Clipmask to make magnified view-contents circular */
1796 #ifdef SHAPE
1797 XShapeCombineMask(scr->display, WMViewXID(panel->magnifyGlass->view),
1798 ShapeBounding, 0, 0, clip_mask, ShapeSet);
1799 #else
1800 /* Clip circle in glass cursor */
1801 XSetClipMask(scr->display, scr->clipGC, clip_mask);
1802 XSetClipOrigin(scr->display, scr->clipGC, 0, 0);
1803 #endif
1805 XFreePixmap(scr->display, clip_mask);
1807 /* Draw initial magnifying glass contents */
1808 magnifyGetImageStored(panel, x, y, x, y);
1810 pixmap = magnifyCreatePixmap(panel);
1811 XSetWindowBackgroundPixmap(scr->display,
1812 WMViewXID(panel->magnifyGlass->view),
1813 pixmap);
1814 XClearWindow(scr->display, WMViewXID(panel->magnifyGlass->view));
1815 XFlush(scr->display);
1817 #ifndef SHAPE
1818 XFreePixmap(scr->display, pixmap);
1819 #endif
1821 point.x = x;
1822 point.y = y;
1824 return point;
1828 static void
1829 magnifyPutCursor(WMWidget *w, void *data)
1831 W_ColorPanel *panel = (W_ColorPanel*)(data);
1832 W_Screen *scr = WMWidgetScreen(panel->win);
1833 Cursor magCursor;
1834 Pixmap pixmap;
1835 XEvent event;
1836 WMPoint initialPosition;
1838 /* Destroy wheelBackImg, so it'll update properly */
1839 if (panel->selectionBackImg) {
1840 XFreePixmap(WMWidgetScreen(panel->win)->display,
1841 panel->selectionBackImg);
1842 panel->selectionBackImg = None;
1845 /* Create magnifying glass */
1846 panel->magnifyGlass = wmalloc(sizeof(MovingView));
1847 panel->magnifyGlass->view = magnifyCreateView(panel);
1848 if (!panel->magnifyGlass->view)
1849 return;
1851 initialPosition = magnifyInitialize(panel);
1852 panel->magnifyGlass->x = initialPosition.x;
1853 panel->magnifyGlass->y = initialPosition.y;
1855 W_MoveView(panel->magnifyGlass->view,
1856 panel->magnifyGlass->x - Cursor_x_hot,
1857 panel->magnifyGlass->y - Cursor_y_hot);
1858 W_MapView(panel->magnifyGlass->view);
1860 magCursor = magnifyGrabPointer(panel);
1862 while (panel->magnifyGlass->image)
1864 WMNextEvent(scr->display, &event);
1866 /* Pack motion events */
1867 while (XCheckTypedEvent(scr->display, MotionNotify, &event)) {
1870 switch (event.type)
1872 case ButtonPress:
1873 XDestroyImage(panel->magnifyGlass->image);
1874 panel->magnifyGlass->image = NULL;
1876 if (event.xbutton.button == Button1) {
1877 panel->color.rgb = panel->magnifyGlass->color;
1878 panel->color.set = cpRGB;
1879 updateSwatch(panel, panel->color);
1881 switch (panel->mode) {
1882 case WMWheelModeColorPanel:
1883 wheelInit(panel);
1884 wheelRender(panel);
1885 wheelPaint(panel);
1886 break;
1887 case WMGrayModeColorPanel:
1888 grayInit(panel);
1889 break;
1890 case WMRGBModeColorPanel:
1891 rgbInit(panel);
1892 break;
1893 case WMCMYKModeColorPanel:
1894 cmykInit(panel);
1895 break;
1896 case WMHSBModeColorPanel:
1897 hsbInit(panel);
1898 break;
1899 default:
1900 break;
1902 panel->lastChanged = panel->mode;
1904 WMSetButtonSelected(panel->magnifyBtn, False);
1905 break;
1907 case MotionNotify:
1908 while (XPending(event.xmotion.display)) {
1909 XEvent ev;
1910 XPeekEvent(event.xmotion.display, &ev);
1911 if (ev.type == MotionNotify)
1912 XNextEvent(event.xmotion.display, &event);
1913 else
1914 break;
1917 /* Get a "dirty rectangle" */
1918 magnifyGetImageStored( panel,
1919 panel->magnifyGlass->x, panel->magnifyGlass->y,
1920 event.xmotion.x_root, event.xmotion.y_root);
1922 /* Update coordinates */
1923 panel->magnifyGlass->x = event.xmotion.x_root;
1924 panel->magnifyGlass->y = event.xmotion.y_root;
1926 /* Move view */
1927 W_MoveView(panel->magnifyGlass->view,
1928 panel->magnifyGlass->x - Cursor_x_hot,
1929 panel->magnifyGlass->y - Cursor_y_hot);
1931 /* Put new image (with magn.) in view */
1932 pixmap = magnifyCreatePixmap(panel);
1933 if (pixmap != None) {
1934 /* Change the window background */
1935 XSetWindowBackgroundPixmap(scr->display,
1936 WMViewXID(panel->magnifyGlass->view), pixmap);
1937 /* Force an Expose (handled by X) */
1938 XClearWindow(scr->display,
1939 WMViewXID(panel->magnifyGlass->view));
1940 /* Synchronize the event queue, so the Expose is handled NOW */
1941 XFlush(scr->display);
1942 #ifndef SHAPE
1943 XFreePixmap(scr->display, pixmap);
1944 #endif
1946 break;
1948 /* Try XQueryPointer for this !!! It returns windows that the pointer
1949 * is over. Note: We found this solving the invisible donkey cap bug
1951 #if 0 /* As it is impossible to make this work in all cases,
1952 * we consider it confusing. Therefore we disabled it.
1954 case FocusOut: /* fall through */
1955 case FocusIn:
1957 * Color Panel window (panel->win) lost or received focus.
1958 * We need to update the pixmap in the magnifying glass.
1960 * BUG Doesn't work with focus switches between two windows
1961 * if none of them is the color panel.
1963 XUngrabPointer(scr->display, CurrentTime);
1964 W_UnmapView(panel->magnifyGlass->view);
1966 magnifyInitialize(panel);
1968 W_MapView(panel->magnifyGlass->view);
1969 XGrabPointer (scr->display, panel->magnifyGlass->view->window,
1970 True, PointerMotionMask | ButtonPressMask,
1971 GrabModeAsync, GrabModeAsync,
1972 scr->rootWin, magCursor, CurrentTime);
1973 break;
1974 #endif
1975 default:
1976 WMHandleEvent(&event);
1977 break;
1978 } /* of switch */
1981 XUngrabPointer(scr->display, CurrentTime);
1982 XFreeCursor(scr->display, magCursor);
1984 XFreePixmap(scr->display, panel->magnifyGlass->magPix);
1985 panel->magnifyGlass->magPix = None;
1987 W_UnmapView(panel->magnifyGlass->view);
1988 W_DestroyView(panel->magnifyGlass->view);
1989 panel->magnifyGlass->view = NULL;
1991 wfree(panel->magnifyGlass);
1996 /****************** ColorWheel Functions ************************/
1998 static wheelMatrix*
1999 wheelCreateMatrix(unsigned int width, unsigned int height)
2001 wheelMatrix *matrix = NULL;
2002 int i;
2004 assert((width > 0) && (height > 0));
2006 matrix = wmalloc(sizeof(wheelMatrix));
2007 memset(matrix, 0, sizeof(wheelMatrix));
2008 matrix->width = width;
2009 matrix->height = height;
2011 for (i = 0; i < 3; i++) {
2012 matrix->data[i] = wmalloc(width*height*sizeof(unsigned char));
2015 return matrix;
2019 static void
2020 wheelDestroyMatrix(wheelMatrix *matrix)
2022 int i;
2024 if (!matrix)
2025 return;
2027 for (i = 0; i < 3; i++) {
2028 if (matrix->data[i])
2029 wfree(matrix->data[i]);
2031 wfree(matrix);
2035 static void
2036 wheelInitMatrix(W_ColorPanel *panel)
2038 int i;
2039 int x,y;
2040 unsigned char *rp, *gp, *bp;
2041 CPColor cpColor;
2042 long ofs[4];
2043 int xcor, ycor;
2044 unsigned short sat;
2045 int dhue[4];
2046 const int cw_halfsize = (colorWheelSize + 4)/2,
2047 cw_sqsize = (colorWheelSize +4) * (colorWheelSize +4),
2048 uchar_shift = getShift(sizeof(unsigned char));
2050 if (!panel->wheelMtrx)
2051 return;
2053 cpColor.hsv.value = 255;
2054 cpColor.set = cpHSV;
2056 ofs[0] = -1;
2057 ofs[1] = -(colorWheelSize + 4);
2059 /* offsets are counterclockwise (in triangles).
2061 * ofs[0] ---->
2062 * _______________________________________
2063 * [1] |_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_| o
2064 * s |_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_| f
2065 * f |_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_| s
2066 * o | | | | | | | | | | | | | | | | | | | | | [3]
2068 * <---- ofs[2]
2069 * ____
2070 * |\ /| <-- triangles
2071 * | \/ |
2072 * | /\ |
2073 * |/__\|
2076 for (y = 0; y < cw_halfsize; y++) {
2077 for (x = y; x < (colorWheelSize+4-y); x++) {
2078 /* (xcor, ycor) is (x,y) relative to center of matrix */
2079 xcor = 2 * x - 4 - colorWheelSize;
2080 ycor = 2 * y - 4 - colorWheelSize;
2082 /* RColor.saturation is unsigned char and will wrap after 255 */
2083 sat = rint(255.0 * sqrt(xcor*xcor + ycor*ycor) / colorWheelSize);
2085 cpColor.hsv.saturation = (unsigned char)sat;
2087 ofs[0]++; /* top quarter of matrix*/
2088 ofs[1] += colorWheelSize + 4; /* left quarter */
2089 ofs[2] = cw_sqsize - 1 - ofs[0]; /* bottom quarter */
2090 ofs[3] = cw_sqsize - 1 - ofs[1]; /* right quarter */
2092 if (sat < 256) {
2093 if (xcor != 0)
2094 dhue[0] = rint(atan((double)ycor / (double)xcor) *
2095 (180.0 / M_PI)) + (xcor < 0 ? 180.0 : 0.0);
2096 else
2097 dhue[0] = 270;
2099 dhue[0] = 360 - dhue[0]; /* Reverse direction of ColorWheel */
2100 dhue[1] = 270 - dhue[0] + (dhue[0] > 270 ? 360 : 0);
2101 dhue[2] = dhue[0] - 180 + (dhue[0] < 180 ? 360 : 0);
2102 dhue[3] = 90 - dhue[0] + (dhue[0] > 90 ? 360 : 0);
2104 for (i = 0; i < 4; i++) {
2105 rp = panel->wheelMtrx->data[0] + (ofs[i] << uchar_shift);
2106 gp = panel->wheelMtrx->data[1] + (ofs[i] << uchar_shift);
2107 bp = panel->wheelMtrx->data[2] + (ofs[i] << uchar_shift);
2109 cpColor.hsv.hue = dhue[i];
2110 convertCPColor(&cpColor);
2112 *rp = (unsigned char)(cpColor.rgb.red);
2113 *gp = (unsigned char)(cpColor.rgb.green);
2114 *bp = (unsigned char)(cpColor.rgb.blue);
2117 else {
2118 for (i = 0; i < 4; i++) {
2119 rp = panel->wheelMtrx->data[0] + (ofs[i] << uchar_shift);
2120 gp = panel->wheelMtrx->data[1] + (ofs[i] << uchar_shift);
2121 bp = panel->wheelMtrx->data[2] + (ofs[i] << uchar_shift);
2123 *rp = (unsigned char)0;
2124 *gp = (unsigned char)0;
2125 *bp = (unsigned char)0;
2130 ofs[0] += 2*y+1;
2131 ofs[1] += 1 - (colorWheelSize + 4) * (colorWheelSize + 4 - 1 - 2*y);
2136 static void
2137 wheelCalculateValues(W_ColorPanel *panel, int maxvalue)
2139 unsigned int i;
2140 unsigned int v;
2142 for (i = 0; i < 256; i++) {
2143 /* We divide by 128 in advance, and check whether that number divides
2144 * by 2 properly. If not, we add one to round the number correctly
2146 v = (i*maxvalue) >> 7;
2147 panel->wheelMtrx->values[i] = (unsigned char)((v >> 1) +(v & 0x01));
2152 static void
2153 wheelRender(W_ColorPanel *panel)
2155 W_Screen *scr = WMWidgetScreen(panel->win);
2156 int x,y;
2157 RImage *image;
2158 unsigned char *ptr;
2159 RColor gray;
2160 unsigned long ofs = 0;
2161 /*unsigned char shift = getShift(sizeof(unsigned char));*/
2163 image = RCreateImage(colorWheelSize+4, colorWheelSize+4, True);
2164 if (!image) {
2165 wwarning(_("Color Panel: Could not allocate memory"));
2166 return;
2169 ptr = image->data;
2171 /* TODO Make this transparent istead of gray */
2172 gray.red = gray.blue = 0xae; gray.green = 0xaa;
2174 for (y = 0; y < colorWheelSize+4; y++) {
2175 for (x = 0; x < colorWheelSize+4; x++) {
2176 if (wheelInsideColorWheel(panel, ofs)) {
2177 *(ptr++) = (unsigned char)(panel->wheelMtrx->values[
2178 panel->wheelMtrx->data[0][ofs] ]);
2179 *(ptr++) = (unsigned char)(panel->wheelMtrx->values[
2180 panel->wheelMtrx->data[1][ofs] ]);
2181 *(ptr++) = (unsigned char)(panel->wheelMtrx->values[
2182 panel->wheelMtrx->data[2][ofs] ]);
2183 *(ptr++) = 0;
2185 else {
2186 *(ptr++) = (unsigned char)(gray.red);
2187 *(ptr++) = (unsigned char)(gray.green);
2188 *(ptr++) = (unsigned char)(gray.blue);
2189 *(ptr++) = 255;
2191 ofs++;
2195 if (panel->wheelImg)
2196 XFreePixmap(scr->display, panel->wheelImg);
2198 RConvertImage(scr->rcontext, image, &panel->wheelImg);
2199 RReleaseImage(image);
2201 /* Check if backimage exists. If it doesn't, allocate and fill it */
2202 if (!panel->selectionBackImg) {
2203 panel->selectionBackImg = XCreatePixmap(scr->display,
2204 W_VIEW(panel->wheelFrm)->window, 4, 4, scr->depth);
2205 XCopyArea(scr->display, panel->wheelImg, panel->selectionBackImg,
2206 scr->copyGC, panel->colx -2, panel->coly -2, 4, 4, 0, 0);
2207 /* -2 is hot spot correction */
2212 static Bool
2213 wheelInsideColorWheel(W_ColorPanel *panel, unsigned long ofs)
2215 return ((panel->wheelMtrx->data[0][ofs] != 0) &&
2216 (panel->wheelMtrx->data[1][ofs] != 0) &&
2217 (panel->wheelMtrx->data[2][ofs] != 0));
2221 static void
2222 wheelPaint (W_ColorPanel *panel)
2224 W_Screen *scr = WMWidgetScreen(panel->win);
2226 XCopyArea(scr->display, panel->wheelImg, panel->wheelView->window,
2227 scr->copyGC, 0, 0, colorWheelSize+4, colorWheelSize+4, 0, 0);
2229 /* Draw selection image */
2230 XCopyArea(scr->display, panel->selectionImg, panel->wheelView->window,
2231 scr->copyGC, 0, 0, 4, 4, panel->colx -2, panel->coly -2);
2235 static void
2236 wheelHandleEvents(XEvent *event, void *data)
2238 W_ColorPanel *panel = (W_ColorPanel*)data;
2240 switch (event->type) {
2241 case Expose:
2242 if (event->xexpose.count != 0) /* TODO Improve */
2243 break;
2244 wheelPaint(panel);
2245 break;
2250 static void
2251 wheelHandleActionEvents(XEvent *event, void *data)
2253 W_ColorPanel *panel = (W_ColorPanel*)data;
2255 switch (event->type) {
2256 case ButtonPress:
2257 if (getPickerPart(panel, event->xbutton.x, event->xbutton.y) ==
2258 COLORWHEEL_PART) {
2260 panel->lastChanged = WMWheelModeColorPanel;
2261 panel->flags.dragging = 1;
2263 wheelPositionSelection(panel, event->xbutton.x, event->xbutton.y);
2265 break;
2267 case ButtonRelease:
2268 panel->flags.dragging = 0;
2269 if (!panel->flags.continuous) {
2270 if (panel->action)
2271 (*panel->action)(panel, panel->clientData);
2273 break;
2275 case MotionNotify:
2276 if (panel->flags.dragging) {
2277 if (getPickerPart(panel, event->xmotion.x, event->xmotion.y) ==
2278 COLORWHEEL_PART) {
2279 wheelPositionSelection(panel, event->xmotion.x,
2280 event->xmotion.y);
2282 else
2283 wheelPositionSelectionOutBounds(panel, event->xmotion.x,
2284 event->xmotion.y);
2286 break;
2291 static int
2292 getPickerPart(W_ColorPanel *panel, int x, int y)
2294 int lx, ly;
2295 unsigned long ofs;
2297 lx = x;
2298 ly = y;
2300 if (panel->mode == WMWheelModeColorPanel) {
2301 if ((lx >= 2) && (lx <= 2+colorWheelSize) && (ly >= 2) &&
2302 (ly <= 2+colorWheelSize)) {
2304 ofs = ly*panel->wheelMtrx->width+lx;
2306 if (wheelInsideColorWheel(panel, ofs))
2307 return COLORWHEEL_PART;
2311 if (panel->mode == WMCustomPaletteModeColorPanel) {
2312 if ((lx >= 2) && (lx < customPaletteWidth-2) && (ly >= 2) &&
2313 (ly < customPaletteHeight-2)) {
2314 return CUSTOMPALETTE_PART;
2318 return 0;
2322 static void
2323 wheelBrightnessSliderCallback(WMWidget *w, void *data)
2325 int value;
2327 W_ColorPanel *panel = (W_ColorPanel*)data;
2329 value = 255-WMGetSliderValue(panel->wheelBrightnessS);
2331 wheelCalculateValues(panel, value);
2333 if (panel->color.set == cpRGB) {
2334 convertCPColor(&panel->color);
2335 panel->color.set = cpHSV;
2338 panel->color.hsv.value = value;
2340 wheelRender(panel);
2341 wheelPaint(panel);
2342 wheelUpdateSelection(panel);
2346 static void
2347 wheelUpdateSelection(W_ColorPanel *panel)
2349 W_Screen *scr = WMWidgetScreen(panel->win);
2351 updateSwatch(panel, panel->color);
2352 panel->lastChanged = WMWheelModeColorPanel;
2354 /* Redraw color selector (and make a backup of the part it will cover) */
2355 XCopyArea(scr->display, panel->wheelImg, panel->selectionBackImg,
2356 scr->copyGC, panel->colx -2, panel->coly -2, 4, 4, 0, 0);
2357 /* "-2" is correction for hotspot location */
2358 XCopyArea(scr->display, panel->selectionImg, panel->wheelView->window,
2359 scr->copyGC, 0, 0, 4, 4, panel->colx -2, panel->coly -2);
2360 /* see above */
2364 static void
2365 wheelUndrawSelection(W_ColorPanel *panel)
2367 W_Screen *scr = WMWidgetScreen(panel->win);
2369 XCopyArea(scr->display, panel->selectionBackImg, panel->wheelView->window,
2370 scr->copyGC, 0, 0, 4, 4, panel->colx -2, panel->coly -2);
2371 /* see above */
2374 static void
2375 wheelPositionSelection(W_ColorPanel *panel, int x, int y)
2377 unsigned long ofs = (y * panel->wheelMtrx->width)+ x;
2379 panel->color.rgb.red = panel->wheelMtrx->values[
2380 panel->wheelMtrx->data[0][ofs] ];
2382 panel->color.rgb.green = panel->wheelMtrx->values[
2383 panel->wheelMtrx->data[1][ofs] ];
2385 panel->color.rgb.blue = panel->wheelMtrx->values[
2386 panel->wheelMtrx->data[2][ofs] ];
2387 panel->color.set = cpRGB;
2389 wheelUndrawSelection(panel);
2391 panel->colx = x;
2392 panel->coly = y;
2394 wheelUpdateSelection(panel);
2395 wheelUpdateBrightnessGradientFromLocation(panel);
2398 static void
2399 wheelPositionSelectionOutBounds(W_ColorPanel *panel, int x, int y)
2401 int hue;
2402 int xcor, ycor;
2403 CPColor cpColor;
2405 xcor = x * 2 - colorWheelSize - 4;
2406 ycor = y * 2 - colorWheelSize - 4;
2408 panel->color.hsv.saturation = 255;
2409 panel->color.hsv.value = 255 - WMGetSliderValue(panel->wheelBrightnessS);
2411 if (xcor != 0)
2412 hue = rint(atan(- (double)ycor / (double)xcor) * (180.0/M_PI));
2413 else {
2414 if (ycor < 0)
2415 hue = 90;
2416 else
2417 hue = 270;
2420 if (xcor < 0)
2421 hue += 180;
2423 if ((xcor > 0) && (ycor > 0))
2424 hue += 360;
2426 panel->color.hsv.hue = hue;
2427 panel->color.set = cpHSV;
2428 convertCPColor(&panel->color);
2430 wheelUndrawSelection(panel);
2432 panel->colx = 2 + rint((colorWheelSize * (1.0 +
2433 cos( panel->color.hsv.hue * (M_PI/180.0) ))) / 2.0);
2434 /* "+2" because of "colorWheelSize + 4" */
2435 panel->coly = 2 + rint((colorWheelSize * (1.0 +
2436 sin(- panel->color.hsv.hue * (M_PI/180.0) ))) / 2.0);
2438 wheelUpdateSelection(panel);
2439 cpColor = panel->color;
2440 wheelUpdateBrightnessGradient(panel, cpColor);
2443 static void
2444 wheelUpdateBrightnessGradientFromLocation(W_ColorPanel *panel)
2446 CPColor from;
2447 unsigned long ofs;
2449 ofs = panel->coly * panel->wheelMtrx->width + panel->colx;
2451 from.rgb.red = panel->wheelMtrx->data[0][ofs];
2452 from.rgb.green = panel->wheelMtrx->data[1][ofs];
2453 from.rgb.blue = panel->wheelMtrx->data[2][ofs];
2454 from.set = cpRGB;
2456 wheelUpdateBrightnessGradient(panel, from);
2459 static void
2460 wheelUpdateBrightnessGradient(W_ColorPanel *panel, CPColor topColor)
2462 RColor to;
2463 RImage *sliderImg;
2464 WMPixmap *sliderPxmp;
2466 to.red = to.green = to.blue = 0;
2468 if (topColor.set == cpHSV)
2469 convertCPColor(&topColor);
2471 sliderImg = RRenderGradient(16, 153, &(topColor.rgb), &to, RGRD_VERTICAL);
2472 sliderPxmp = WMCreatePixmapFromRImage(WMWidgetScreen(panel->win),
2473 sliderImg, 0);
2474 RReleaseImage(sliderImg);
2475 WMSetSliderImage(panel->wheelBrightnessS, sliderPxmp);
2476 WMReleasePixmap(sliderPxmp);
2479 /****************** Grayscale Panel Functions ***************/
2481 static void
2482 grayBrightnessSliderCallback(WMWidget *w, void *data)
2484 CPColor cpColor;
2485 int value;
2486 char tmp[4];
2487 W_ColorPanel *panel = (W_ColorPanel*)data;
2489 value = WMGetSliderValue(panel->grayBrightnessS);
2491 sprintf(tmp, "%d", value);
2493 WMSetTextFieldText(panel->grayBrightnessT, tmp);
2494 cpColor.rgb.red = cpColor.rgb.green = cpColor.rgb.blue = rint(2.55*value);
2495 cpColor.set = cpRGB;
2497 updateSwatch(panel, cpColor);
2498 panel->lastChanged = WMGrayModeColorPanel;
2501 static void
2502 grayPresetButtonCallback(WMWidget *w, void *data)
2504 CPColor cpColor;
2505 char tmp[4];
2506 int value;
2507 int i=0;
2508 W_ColorPanel *panel = (W_ColorPanel*)data;
2510 while (i < 7) {
2511 if (w == panel->grayPresetBtn[i])
2512 break;
2513 i++;
2516 value = rint((100.0*i)/6.0);
2517 sprintf(tmp, "%d", value);
2519 WMSetTextFieldText(panel->grayBrightnessT, tmp);
2520 cpColor.rgb.red = cpColor.rgb.green = cpColor.rgb.blue =
2521 rint((255.0*i)/6.0);
2522 cpColor.set = cpRGB;
2524 WMSetSliderValue(panel->grayBrightnessS, rint((100.0*i)/6.0));
2526 updateSwatch(panel, cpColor);
2527 panel->lastChanged = WMGrayModeColorPanel;
2530 static void
2531 grayBrightnessTextFieldCallback(void *observerData,
2532 WMNotification *notification)
2534 CPColor cpColor;
2535 char tmp[4];
2536 int value;
2537 W_ColorPanel *panel = (W_ColorPanel*)observerData;
2539 value = atoi(WMGetTextFieldText(panel->grayBrightnessT));
2540 if (value > 100)
2541 value = 100;
2542 if (value < 0)
2543 value = 0;
2545 sprintf(tmp, "%d", value);
2546 WMSetTextFieldText(panel->grayBrightnessT, tmp);
2547 WMSetSliderValue(panel->grayBrightnessS, value);
2549 cpColor.rgb.red = cpColor.rgb.green = cpColor.rgb.blue =
2550 rint((255.0*value)/100.0);
2551 cpColor.set = cpRGB;
2553 updateSwatch(panel, cpColor);
2554 panel->lastChanged = WMGrayModeColorPanel;
2557 /******************* RGB Panel Functions *****************/
2559 static void
2560 rgbSliderCallback(WMWidget *w, void *data)
2562 CPColor cpColor;
2563 int value[3];
2564 char tmp[4];
2565 W_ColorPanel *panel = (W_ColorPanel*)data;
2567 value[0] = WMGetSliderValue(panel->rgbRedS);
2568 value[1] = WMGetSliderValue(panel->rgbGreenS);
2569 value[2] = WMGetSliderValue(panel->rgbBlueS);
2571 sprintf(tmp, "%d", value[0]);
2572 WMSetTextFieldText(panel->rgbRedT, tmp);
2573 sprintf(tmp, "%d", value[1]);
2574 WMSetTextFieldText(panel->rgbGreenT, tmp);
2575 sprintf(tmp, "%d", value[2]);
2576 WMSetTextFieldText(panel->rgbBlueT, tmp);
2578 cpColor.rgb.red = value[0];
2579 cpColor.rgb.green = value[1];
2580 cpColor.rgb.blue = value[2];
2581 cpColor.set = cpRGB;
2583 updateSwatch(panel, cpColor);
2584 panel->lastChanged = WMRGBModeColorPanel;
2587 static void
2588 rgbTextFieldCallback(void *observerData, WMNotification *notification)
2590 CPColor cpColor;
2591 int value[3];
2592 char tmp[4];
2593 int n;
2594 W_ColorPanel *panel = (W_ColorPanel*)observerData;
2596 value[0] = atoi(WMGetTextFieldText(panel->rgbRedT));
2597 value[1] = atoi(WMGetTextFieldText(panel->rgbGreenT));
2598 value[2] = atoi(WMGetTextFieldText(panel->rgbBlueT));
2600 for (n=0; n < 3; n++) {
2601 if (value[n] > 255)
2602 value[n] = 255;
2603 if (value[n] < 0)
2604 value[n] = 0;
2607 sprintf(tmp, "%d", value[0]);
2608 WMSetTextFieldText(panel->rgbRedT, tmp);
2609 sprintf(tmp, "%d", value[1]);
2610 WMSetTextFieldText(panel->rgbGreenT, tmp);
2611 sprintf(tmp, "%d", value[2]);
2612 WMSetTextFieldText(panel->rgbBlueT, tmp);
2614 WMSetSliderValue(panel->rgbRedS, value[0]);
2615 WMSetSliderValue(panel->rgbGreenS, value[1]);
2616 WMSetSliderValue(panel->rgbBlueS, value[2]);
2618 cpColor.rgb.red = value[0];
2619 cpColor.rgb.green = value[1];
2620 cpColor.rgb.blue = value[2];
2621 cpColor.set = cpRGB;
2623 updateSwatch(panel, cpColor);
2624 panel->lastChanged = WMRGBModeColorPanel;
2628 /******************* CMYK Panel Functions *****************/
2630 static void
2631 cmykSliderCallback(WMWidget *w, void *data)
2633 CPColor cpColor;
2634 int value[4];
2635 char tmp[4];
2636 W_ColorPanel *panel = (W_ColorPanel*)data;
2637 double scale;
2639 value[0] = WMGetSliderValue(panel->cmykCyanS);
2640 value[1] = WMGetSliderValue(panel->cmykMagentaS);
2641 value[2] = WMGetSliderValue(panel->cmykYellowS);
2642 value[3] = WMGetSliderValue(panel->cmykBlackS);
2644 sprintf(tmp, "%d", value[0]);
2645 WMSetTextFieldText(panel->cmykCyanT, tmp);
2646 sprintf(tmp, "%d", value[1]);
2647 WMSetTextFieldText(panel->cmykMagentaT, tmp);
2648 sprintf(tmp, "%d", value[2]);
2649 WMSetTextFieldText(panel->cmykYellowT, tmp);
2650 sprintf(tmp, "%d", value[3]);
2651 WMSetTextFieldText(panel->cmykBlackT, tmp);
2653 scale = 2.55 * (1.0 - (value[3] / 100.0));
2654 cpColor.rgb.red = rint((100.0 - value[0]) * scale);
2655 cpColor.rgb.green = rint((100.0 - value[1]) * scale);
2656 cpColor.rgb.blue = rint((100.0 - value[2]) * scale);
2657 cpColor.set = cpRGB;
2659 updateSwatch(panel, cpColor);
2660 panel->lastChanged = WMCMYKModeColorPanel;
2663 static void
2664 cmykTextFieldCallback(void *observerData, WMNotification *notification)
2666 CPColor cpColor;
2667 int value[4];
2668 char tmp[4];
2669 int n;
2670 double scale;
2671 W_ColorPanel *panel = (W_ColorPanel*)observerData;
2673 value[0] = atoi(WMGetTextFieldText(panel->cmykCyanT));
2674 value[1] = atoi(WMGetTextFieldText(panel->cmykMagentaT));
2675 value[2] = atoi(WMGetTextFieldText(panel->cmykYellowT));
2676 value[3] = atoi(WMGetTextFieldText(panel->cmykBlackT));
2678 for (n=0; n < 4; n++) {
2679 if (value[n] > 100)
2680 value[n] = 100;
2681 if (value[n] < 0)
2682 value[n] = 0;
2685 sprintf(tmp, "%d", value[0]);
2686 WMSetTextFieldText(panel->cmykCyanT, tmp);
2688 sprintf(tmp, "%d", value[1]);
2689 WMSetTextFieldText(panel->cmykMagentaT, tmp);
2691 sprintf(tmp, "%d", value[2]);
2692 WMSetTextFieldText(panel->cmykYellowT, tmp);
2694 sprintf(tmp, "%d", value[3]);
2695 WMSetTextFieldText(panel->cmykBlackT, tmp);
2697 WMSetSliderValue(panel->cmykCyanS, value[0]);
2698 WMSetSliderValue(panel->cmykMagentaS, value[1]);
2699 WMSetSliderValue(panel->cmykYellowS, value[2]);
2700 WMSetSliderValue(panel->cmykBlackS, value[3]);
2702 scale = 2.55 * (1.0 - (value[3] / 100.0));
2703 cpColor.rgb.red = rint((100.0 - value[0]) * scale);
2704 cpColor.rgb.green = rint((100.0 - value[1]) * scale);
2705 cpColor.rgb.blue = rint((100.0 - value[2]) * scale);
2706 cpColor.set = cpRGB;
2708 updateSwatch(panel, cpColor);
2709 panel->lastChanged = WMCMYKModeColorPanel;
2712 /********************** HSB Panel Functions ***********************/
2714 static void
2715 hsbSliderCallback(WMWidget *w, void *data)
2717 CPColor cpColor;
2718 int value[3];
2719 char tmp[4];
2720 W_ColorPanel *panel = (W_ColorPanel*)data;
2722 value[0] = WMGetSliderValue(panel->hsbHueS);
2723 value[1] = WMGetSliderValue(panel->hsbSaturationS);
2724 value[2] = WMGetSliderValue(panel->hsbBrightnessS);
2726 sprintf(tmp, "%d", value[0]);
2727 WMSetTextFieldText(panel->hsbHueT, tmp);
2728 sprintf(tmp, "%d", value[1]);
2729 WMSetTextFieldText(panel->hsbSaturationT, tmp);
2730 sprintf(tmp, "%d", value[2]);
2731 WMSetTextFieldText(panel->hsbBrightnessT, tmp);
2733 cpColor.hsv.hue = value[0];
2734 cpColor.hsv.saturation = value[1]*2.55;
2735 cpColor.hsv.value = value[2]*2.55;
2736 cpColor.set = cpHSV;
2738 convertCPColor(&cpColor);
2740 panel->lastChanged = WMHSBModeColorPanel;
2741 updateSwatch(panel, cpColor);
2743 if (w != panel->hsbBrightnessS)
2744 hsbUpdateBrightnessGradient(panel);
2745 if (w != panel->hsbSaturationS)
2746 hsbUpdateSaturationGradient(panel);
2747 if (w != panel->hsbHueS)
2748 hsbUpdateHueGradient(panel);
2751 static void
2752 hsbTextFieldCallback(void *observerData, WMNotification *notification)
2754 CPColor cpColor;
2755 int value[3];
2756 char tmp[4];
2757 int n;
2758 W_ColorPanel *panel = (W_ColorPanel*)observerData;
2760 value[0] = atoi(WMGetTextFieldText(panel->hsbHueT));
2761 value[1] = atoi(WMGetTextFieldText(panel->hsbSaturationT));
2762 value[2] = atoi(WMGetTextFieldText(panel->hsbBrightnessT));
2764 if (value[0] > 359)
2765 value[0] = 359;
2766 if (value[0] < 0)
2767 value[0] = 0;
2769 for (n=1; n < 3; n++) {
2770 if (value[n] > 100)
2771 value[n] = 100;
2772 if (value[n] < 0)
2773 value[n] = 0;
2776 sprintf(tmp, "%d", value[0]);
2777 WMSetTextFieldText(panel->hsbHueT, tmp);
2778 sprintf(tmp, "%d", value[1]);
2779 WMSetTextFieldText(panel->hsbSaturationT, tmp);
2780 sprintf(tmp, "%d", value[2]);
2781 WMSetTextFieldText(panel->hsbBrightnessT, tmp);
2783 WMSetSliderValue(panel->hsbHueS, value[0]);
2784 WMSetSliderValue(panel->hsbSaturationS, value[1]);
2785 WMSetSliderValue(panel->hsbBrightnessS, value[2]);
2787 cpColor.hsv.hue = value[0];
2788 cpColor.hsv.saturation = value[1]*2.55;
2789 cpColor.hsv.value = value[2]*2.55;
2790 cpColor.set = cpHSV;
2792 convertCPColor(&cpColor);
2794 panel->lastChanged = WMHSBModeColorPanel;
2795 updateSwatch(panel, cpColor);
2797 hsbUpdateBrightnessGradient(panel);
2798 hsbUpdateSaturationGradient(panel);
2799 hsbUpdateHueGradient(panel);
2802 static void
2803 hsbUpdateBrightnessGradient(W_ColorPanel *panel)
2805 W_Screen *scr = WMWidgetScreen(panel->win);
2806 RColor from;
2807 CPColor to;
2808 RImage *sliderImg;
2809 WMPixmap *sliderPxmp;
2811 from.red = from.green = from.blue = 0;
2812 to.hsv = panel->color.hsv;
2813 to.hsv.value = 255;
2814 to.set = cpHSV;
2816 convertCPColor(&to);
2818 sliderImg = RRenderGradient(141, 16, &from, &(to.rgb), RGRD_HORIZONTAL);
2819 sliderPxmp = WMCreatePixmapFromRImage(scr, sliderImg, 0);
2820 RReleaseImage(sliderImg);
2822 if (sliderPxmp)
2823 W_PaintText(W_VIEW(panel->hsbBrightnessS), sliderPxmp->pixmap,
2824 panel->font12, 2, 0, 100, WALeft, WMColorGC(scr->white),
2825 False, _("Brightness"), strlen(_("Brightness")));
2826 else
2827 wwarning(_("Color Panel: Could not allocate memory"));
2829 WMSetSliderImage(panel->hsbBrightnessS, sliderPxmp);
2830 WMReleasePixmap(sliderPxmp);
2833 static void
2834 hsbUpdateSaturationGradient(W_ColorPanel *panel)
2836 W_Screen *scr = WMWidgetScreen(panel->win);
2837 CPColor from;
2838 CPColor to;
2839 RImage *sliderImg;
2840 WMPixmap *sliderPxmp;
2842 from.hsv = panel->color.hsv;
2843 from.hsv.saturation = 0;
2844 from.set = cpHSV;
2845 convertCPColor(&from);
2847 to.hsv = panel->color.hsv;
2848 to.hsv.saturation = 255;
2849 to.set = cpHSV;
2850 convertCPColor(&to);
2852 sliderImg = RRenderGradient(141, 16, &(from.rgb), &(to.rgb),
2853 RGRD_HORIZONTAL);
2854 sliderPxmp = WMCreatePixmapFromRImage(scr, sliderImg, 0);
2855 RReleaseImage(sliderImg);
2857 if (sliderPxmp)
2858 W_PaintText(W_VIEW(panel->hsbSaturationS), sliderPxmp->pixmap,
2859 panel->font12, 2, 0, 100, WALeft,
2860 WMColorGC(from.hsv.value < 128 ? scr->white : scr->black), False,
2861 _("Saturation"), strlen(_("Saturation")));
2862 else
2863 wwarning(_("Color Panel: Could not allocate memory"));
2865 WMSetSliderImage(panel->hsbSaturationS, sliderPxmp);
2866 WMReleasePixmap(sliderPxmp);
2869 static void
2870 hsbUpdateHueGradient(W_ColorPanel *panel)
2872 W_Screen *scr = WMWidgetScreen(panel->win);
2873 RColor **colors = NULL;
2874 RHSVColor hsvcolor;
2875 RImage *sliderImg;
2876 WMPixmap *sliderPxmp;
2877 int i;
2879 hsvcolor = panel->color.hsv;
2881 colors = wmalloc(sizeof(RColor*)*(8));
2882 for (i=0; i<7; i++) {
2883 hsvcolor.hue = (360*i)/6;
2884 colors[i] = wmalloc(sizeof(RColor));
2885 RHSVtoRGB(&hsvcolor, colors[i]);
2887 colors[7] = NULL;
2889 sliderImg = RRenderMultiGradient(141, 16, colors, RGRD_HORIZONTAL);
2890 sliderPxmp = WMCreatePixmapFromRImage(scr, sliderImg, 0);
2891 RReleaseImage(sliderImg);
2893 if (sliderPxmp)
2894 W_PaintText(W_VIEW(panel->hsbHueS), sliderPxmp->pixmap,
2895 panel->font12, 2, 0, 100, WALeft,
2896 WMColorGC(hsvcolor.value < 128 ? scr->white : scr->black), False,
2897 _("Hue"), strlen(_("Hue")));
2898 else
2899 wwarning(_("Color Panel: Could not allocate memory"));
2901 WMSetSliderImage(panel->hsbHueS, sliderPxmp);
2902 WMReleasePixmap(sliderPxmp);
2904 for (i=0; i<7; i++)
2905 wfree(colors[i]);
2907 wfree(colors);
2910 /*************** Custom Palette Functions ****************/
2912 static void
2913 customRenderSpectrum(W_ColorPanel *panel)
2915 RImage *spectrum;
2916 int x,y;
2917 unsigned char *ptr;
2918 CPColor cpColor;
2920 spectrum = RCreateImage(SPECTRUM_WIDTH, SPECTRUM_HEIGHT, False);
2922 ptr = spectrum->data;
2924 for (y = 0; y < SPECTRUM_HEIGHT; y++) {
2925 cpColor.hsv.hue = y;
2926 cpColor.hsv.saturation = 0;
2927 cpColor.hsv.value = 255;
2928 cpColor.set = cpHSV;
2930 for (x = 0; x < SPECTRUM_WIDTH; x++) {
2931 convertCPColor(&cpColor);
2933 *(ptr++) = (unsigned char)cpColor.rgb.red;
2934 *(ptr++) = (unsigned char)cpColor.rgb.green;
2935 *(ptr++) = (unsigned char)cpColor.rgb.blue;
2937 if (x < (SPECTRUM_WIDTH/2))
2938 cpColor.hsv.saturation++;
2940 if (x > (SPECTRUM_WIDTH/2))
2941 cpColor.hsv.value--;
2944 if (panel->customPaletteImg) {
2945 RReleaseImage(panel->customPaletteImg);
2946 panel->customPaletteImg = NULL;
2948 panel->customPaletteImg = spectrum;
2953 static void
2954 customSetPalette(W_ColorPanel *panel)
2956 W_Screen *scr = WMWidgetScreen(panel->win);
2957 RImage *scaledImg;
2958 Pixmap image;
2959 int item;
2961 image = XCreatePixmap(scr->display, W_DRAWABLE(scr), customPaletteWidth,
2962 customPaletteHeight, scr->depth);
2963 scaledImg = RScaleImage(panel->customPaletteImg, customPaletteWidth,
2964 customPaletteHeight);
2965 RConvertImage(scr->rcontext, scaledImg, &image);
2966 RReleaseImage(scaledImg);
2968 XCopyArea(scr->display, image, panel->customPaletteContentView->window,
2969 scr->copyGC, 0, 0, customPaletteWidth, customPaletteHeight, 0, 0);
2971 /* Check backimage exists. If it doesn't, allocate and fill it */
2972 if (!panel->selectionBackImg) {
2973 panel->selectionBackImg = XCreatePixmap(scr->display,
2974 panel->customPaletteContentView->window, 4, 4, scr->depth);
2977 XCopyArea(scr->display, image, panel->selectionBackImg, scr->copyGC,
2978 panel->palx-2, panel->paly-2, 4, 4, 0, 0);
2979 XCopyArea(scr->display, panel->selectionImg,
2980 panel->customPaletteContentView->window, scr->copyGC, 0 , 0, 4, 4,
2981 panel->palx-2, panel->paly-2);
2982 XFreePixmap(scr->display, image);
2984 panel->palXRatio = (double)(panel->customPaletteImg->width) /
2985 (double)(customPaletteWidth);
2986 panel->palYRatio = (double)(panel->customPaletteImg->height) /
2987 (double)(customPaletteHeight);
2989 item = WMGetPopUpButtonSelectedItem (panel->customPaletteHistoryBtn);
2993 static void
2994 customPalettePositionSelection(W_ColorPanel *panel, int x, int y)
2996 W_Screen *scr = WMWidgetScreen(panel->win);
2997 unsigned long ofs;
3000 /* undraw selection */
3001 XCopyArea(scr->display, panel->selectionBackImg,
3002 panel->customPaletteContentView->window, scr->copyGC, 0, 0, 4, 4,
3003 panel->palx-2, panel->paly-2);
3005 panel->palx = x;
3006 panel->paly = y;
3008 ofs = (rint(x * panel->palXRatio) + rint(y * panel->palYRatio) *
3009 panel->customPaletteImg->width) * 3;
3011 panel->color.rgb.red = panel->customPaletteImg->data[ofs];
3012 panel->color.rgb.green = panel->customPaletteImg->data[ofs+1];
3013 panel->color.rgb.blue = panel->customPaletteImg->data[ofs+2];
3014 panel->color.set = cpRGB;
3016 updateSwatch(panel, panel->color);
3017 panel->lastChanged = WMCustomPaletteModeColorPanel;
3019 /* Redraw color selector (and make a backup of the part it will cover) */
3020 XCopyArea(scr->display, panel->customPaletteContentView->window,
3021 panel->selectionBackImg, scr->copyGC, panel->palx-2, panel->paly-2,
3022 4, 4, 0, 0); /* "-2" is correction for hotspot location */
3023 XCopyArea(scr->display, panel->selectionImg,
3024 panel->customPaletteContentView->window, scr->copyGC, 0, 0, 4, 4,
3025 panel->palx-2, panel->paly-2); /* see above */
3029 static void
3030 customPalettePositionSelectionOutBounds(W_ColorPanel *panel, int x, int y)
3032 if (x < 2)
3033 x = 2;
3034 if (y < 2)
3035 y = 2;
3036 if (x >= customPaletteWidth)
3037 x = customPaletteWidth -2;
3038 if (y >= customPaletteHeight)
3039 y = customPaletteHeight -2;
3041 customPalettePositionSelection(panel, x, y);
3045 static void
3046 customPaletteHandleEvents(XEvent *event, void *data)
3048 W_ColorPanel *panel = (W_ColorPanel*)data;
3050 switch (event->type) {
3051 case Expose:
3052 if (event->xexpose.count != 0) /* TODO Improve. */
3053 break;
3054 customSetPalette(panel);
3055 break;
3059 static void
3060 customPaletteHandleActionEvents(XEvent *event, void *data)
3062 W_ColorPanel *panel = (W_ColorPanel*)data;
3063 int x, y;
3065 switch (event->type) {
3066 case ButtonPress:
3067 x = event->xbutton.x;
3068 y = event->xbutton.y;
3070 if (getPickerPart(panel, x, y) == CUSTOMPALETTE_PART) {
3071 panel->flags.dragging = 1;
3072 customPalettePositionSelection(panel, x, y);
3074 break;
3076 case ButtonRelease:
3077 panel->flags.dragging = 0;
3078 if (!panel->flags.continuous) {
3079 if (panel->action)
3080 (*panel->action)(panel, panel->clientData);
3082 break;
3084 case MotionNotify:
3085 x = event->xmotion.x;
3086 y = event->xmotion.y;
3088 if (panel->flags.dragging) {
3089 if (getPickerPart(panel, x, y) == CUSTOMPALETTE_PART) {
3090 customPalettePositionSelection(panel, x, y);
3092 else
3093 customPalettePositionSelectionOutBounds(panel, x, y);
3095 break;
3100 static void
3101 customPaletteMenuCallback(WMWidget *w, void *data)
3103 W_ColorPanel *panel = (W_ColorPanel*)data;
3104 int item = WMGetPopUpButtonSelectedItem(panel->customPaletteMenuBtn);
3106 switch (item) {
3107 case CPmenuNewFromFile:
3108 customPaletteMenuNewFromFile(panel);
3109 break;
3110 case CPmenuRename:
3111 customPaletteMenuRename(panel);
3112 break;
3113 case CPmenuRemove:
3114 customPaletteMenuRemove(panel);
3115 break;
3116 case CPmenuCopy:
3117 break;
3118 case CPmenuNewFromClipboard:
3119 break;
3124 static void
3125 customPaletteMenuNewFromFile(W_ColorPanel *panel)
3127 W_Screen *scr = WMWidgetScreen(panel->win);
3128 WMOpenPanel *browseP;
3129 char *filepath;
3130 char *filename = NULL;
3131 char *spath;
3132 char *tmp;
3133 int i;
3134 RImage *tmpImg = NULL;
3136 if ((!panel->lastBrowseDir) || (strcmp(panel->lastBrowseDir,"\0") == 0))
3137 spath = wexpandpath(wgethomedir());
3138 else
3139 spath = wexpandpath(panel->lastBrowseDir);
3141 browseP = WMGetOpenPanel(scr);
3142 WMSetFilePanelCanChooseDirectories(browseP, 0);
3143 WMSetFilePanelCanChooseFiles(browseP, 1);
3145 /* Get a filename */
3146 if (WMRunModalFilePanelForDirectory(browseP, panel->win, spath,
3147 _("Open Palette"), RSupportedFileFormats()) ) {
3148 filepath = WMGetFilePanelFileName(browseP);
3150 /* Get seperation position between path and filename */
3151 i = strrchr(filepath, '/') - filepath + 1;
3152 if (i > strlen(filepath))
3153 i = strlen(filepath);
3155 /* Store last browsed path */
3156 if (panel->lastBrowseDir)
3157 wfree(panel->lastBrowseDir);
3158 panel->lastBrowseDir = wmalloc((i+1)*sizeof(char));
3159 strncpy(panel->lastBrowseDir, filepath, i);
3160 panel->lastBrowseDir[i] = '\0';
3162 /* Get filename from path */
3163 filename = wstrdup(filepath + i);
3165 /* Check for duplicate files, and rename it if there are any */
3166 tmp = wstrconcat(panel->configurationPath, filename);
3167 while (access (tmp, F_OK) == 0) {
3168 char *newName;
3170 wfree(tmp);
3172 newName = generateNewFilename(filename);
3173 wfree(filename);
3174 filename = newName;
3176 tmp = wstrconcat(panel->configurationPath, filename);
3178 wfree(tmp);
3180 /* Copy image to $(gnustepdir)/Library/Colors/ &
3181 * Add filename to history menu */
3182 if (fetchFile (panel->configurationPath, filepath, filename) == 0) {
3184 /* filepath is a "local" path now the file has been copied */
3185 wfree(filepath);
3186 filepath = wstrconcat(panel->configurationPath, filename);
3188 /* load the image & add menu entries */
3189 tmpImg = RLoadImage(scr->rcontext, filepath, 0);
3190 if (tmpImg) {
3191 if (panel->customPaletteImg)
3192 RReleaseImage(panel->customPaletteImg);
3193 panel->customPaletteImg = tmpImg;
3195 customSetPalette(panel);
3196 WMAddPopUpButtonItem(panel->customPaletteHistoryBtn, filename);
3198 panel->currentPalette = WMGetPopUpButtonNumberOfItems(
3199 panel->customPaletteHistoryBtn)-1;
3201 WMSetPopUpButtonSelectedItem(panel->customPaletteHistoryBtn,
3202 panel->currentPalette);
3204 } else {
3205 tmp = wstrconcat(panel->configurationPath, filename);
3207 i = remove(tmp); /* Delete the file, it doesn't belong here */
3208 WMRunAlertPanel(scr, panel->win, _("File Error"),
3209 _("Invalid file format !"), _("OK"), NULL, NULL);
3210 if (i != 0) {
3211 wsyserror(_("can't remove file %s"), tmp);
3212 WMRunAlertPanel(scr, panel->win, _("File Error"),
3213 _("Couldn't remove file from Configuration Directory !"),
3214 _("OK"), NULL, NULL);
3216 wfree(tmp);
3218 wfree(filepath);
3219 wfree(filename);
3221 WMFreeFilePanel(browseP);
3223 wfree(spath);
3227 static void
3228 customPaletteMenuRename(W_ColorPanel *panel)
3230 W_Screen *scr = WMWidgetScreen(panel->win);
3231 char *toName = NULL;
3232 char *fromName;
3233 char *toPath, *fromPath;
3234 int item;
3235 int index;
3237 item = WMGetPopUpButtonSelectedItem(panel->customPaletteHistoryBtn);
3238 fromName = WMGetPopUpButtonItem(panel->customPaletteHistoryBtn, item);
3240 toName = WMRunInputPanel(scr, panel->win, _("Rename"), _("Rename palette to:"),
3241 fromName, _("OK"), _("Cancel"));
3243 if (toName) {
3245 /* As some people do certain stupid things... */
3246 if (strcmp(toName, fromName) == 0) {
3247 wfree(toName);
3248 return;
3251 /* For normal people */
3252 fromPath = wstrconcat(panel->configurationPath, fromName);
3253 toPath = wstrconcat(panel->configurationPath, toName);
3255 if (access (toPath, F_OK) == 0) {
3256 /* Careful, this palette exists already */
3257 if (WMRunAlertPanel(scr, panel->win, _("Warning"),
3258 _("Palette already exists !\n\nOverwrite ?"), _("No"), _("Yes"),
3259 NULL) == 1) {
3260 /* "No" = 0, "Yes" = 1 */
3261 int items = WMGetPopUpButtonNumberOfItems(
3262 panel->customPaletteHistoryBtn);
3264 remove(toPath);
3266 /* Remove from History list too */
3267 index = 1;
3268 while ((index < items) && (strcmp(WMGetPopUpButtonItem(
3269 panel->customPaletteHistoryBtn, index),
3270 toName) != 0 ))
3271 index++;
3273 if (index < items) {
3274 WMRemovePopUpButtonItem(panel->customPaletteHistoryBtn,
3275 index);
3276 if (index < item)
3277 item--;
3280 } else {
3281 wfree(fromPath);
3282 wfree(toName);
3283 wfree(toPath);
3285 return;
3289 if ( rename(fromPath, toPath) != 0)
3290 wsyserror(_("Couldn't rename palette %s to %s\n"), fromName, toName);
3291 else {
3292 WMRemovePopUpButtonItem(panel->customPaletteHistoryBtn, item);
3293 WMInsertPopUpButtonItem(panel->customPaletteHistoryBtn, item,
3294 toName);
3296 WMSetPopUpButtonSelectedItem(panel->customPaletteHistoryBtn, item);
3298 wfree(fromPath);
3299 wfree(toPath);
3300 wfree(toName);
3305 static void
3306 customPaletteMenuRemove(W_ColorPanel *panel)
3308 W_Screen *scr = WMWidgetScreen(panel->win);
3309 char *text;
3310 char *tmp;
3311 int choice;
3312 int item;
3314 item = WMGetPopUpButtonSelectedItem(panel->customPaletteHistoryBtn);
3316 tmp = wstrconcat( _("This will permanently remove the palette "),
3317 WMGetPopUpButtonItem(panel->customPaletteHistoryBtn, item ));
3318 text = wstrconcat( tmp,
3319 _(".\n\nAre you sure you want to remove this palette ?"));
3320 wfree(tmp);
3322 choice = WMRunAlertPanel(scr, panel->win, _("Remove"), text, _("Yes"), _("No"),
3323 NULL);
3324 /* returns 0 (= "Yes") or 1 (="No") */
3325 wfree(text);
3327 if (choice == 0) {
3329 tmp = wstrconcat(panel->configurationPath,
3330 WMGetPopUpButtonItem(panel->customPaletteHistoryBtn, item ));
3332 if ( remove(tmp) == 0) {
3333 /* item-1 always exists */
3334 WMSetPopUpButtonSelectedItem(panel->customPaletteHistoryBtn,
3335 item-1);
3337 customPaletteHistoryCallback(panel->customPaletteHistoryBtn,
3338 panel);
3339 customSetPalette(panel);
3341 WMRemovePopUpButtonItem(panel->customPaletteHistoryBtn, item);
3343 } else {
3344 wsyserror(_("Couldn't remove palette %s\n"), tmp);
3347 wfree(tmp);
3352 static void
3353 customPaletteHistoryCallback(WMWidget *w, void *data)
3355 W_ColorPanel *panel = (W_ColorPanel*)data;
3356 W_Screen *scr = WMWidgetScreen(panel->win);
3357 int item;
3358 char *filename;
3359 RImage *tmp = NULL;
3360 unsigned char perm_mask;
3362 item = WMGetPopUpButtonSelectedItem(panel->customPaletteHistoryBtn);
3363 if (item == panel->currentPalette)
3364 return;
3366 if (item == 0) {
3367 customRenderSpectrum(panel);
3369 WMSetPopUpButtonItemEnabled(panel->customPaletteMenuBtn, CPmenuRename,
3370 False );
3371 WMSetPopUpButtonItemEnabled(panel->customPaletteMenuBtn, CPmenuRemove,
3372 False );
3373 } else {
3374 /* Load file from configpath */
3375 filename = wstrconcat( panel->configurationPath,
3376 WMGetPopUpButtonItem(panel->customPaletteHistoryBtn, item) );
3378 /* If the file corresponding to the item does not exist,
3379 * remove it from the history list and select the next one.
3381 perm_mask = (access(filename, F_OK) == 0);
3382 if (!perm_mask) {
3383 /* File does not exist */
3384 wfree(filename);
3385 WMSetPopUpButtonSelectedItem(panel->customPaletteHistoryBtn,
3386 item-1);
3387 WMRemovePopUpButtonItem(panel->customPaletteHistoryBtn, item);
3388 customPaletteHistoryCallback(w, data);
3389 return;
3392 /* Get the image */
3393 tmp = RLoadImage(scr->rcontext, filename, 0);
3394 if (tmp) {
3395 if (panel->customPaletteImg) {
3396 RReleaseImage(panel->customPaletteImg);
3397 panel->customPaletteImg = NULL;
3399 panel->customPaletteImg = tmp;
3402 /* If the image is not writable, don't allow removing/renaming */
3403 perm_mask = (access(filename, W_OK) == 0);
3404 WMSetPopUpButtonItemEnabled(panel->customPaletteMenuBtn, CPmenuRename,
3405 perm_mask);
3406 WMSetPopUpButtonItemEnabled(panel->customPaletteMenuBtn, CPmenuRemove,
3407 perm_mask);
3409 wfree(filename);
3411 customSetPalette(panel);
3413 panel->currentPalette = item;
3416 /************************* ColorList Panel Functions **********************/
3418 static void
3419 colorListPaintItem(WMList *lPtr, int index, Drawable d, char *text,
3420 int state, WMRect *rect)
3422 int width, height, x, y;
3423 RColor color = *((RColor *)WMGetListItem(lPtr, index)->clientData);
3424 WMScreen *scr = WMWidgetScreen(lPtr);
3425 Display *dpy = WMScreenDisplay(scr);
3426 W_ColorPanel *panel = WMGetHangedData(lPtr);
3427 WMColor *white = WMWhiteColor(scr);
3428 WMColor *black = WMBlackColor(scr);
3429 WMColor *fillColor;
3431 width = rect->size.width;
3432 height = rect->size.height;
3433 x = rect->pos.x;
3434 y = rect->pos.y;
3436 if (state & WLDSSelected)
3437 WMPaintColorSwatch(white, d, x +15, y, width -15, height);
3438 else
3439 XClearArea(dpy, d, x +15, y, width -15, height, False);
3441 fillColor = WMCreateRGBColor(scr, color.red*256, color.green*256,
3442 color.blue*256, False);
3444 WMSetColorInGC(fillColor, WMColorGC(fillColor));
3445 WMPaintColorSwatch(fillColor, d, x, y, 15, 15);
3446 WMReleaseColor(fillColor);
3448 WMDrawString(scr, d, WMColorGC(black), panel->font12, x+18, y, text,
3449 strlen(text));
3451 WMReleaseColor(white);
3452 WMReleaseColor(black);
3456 static void
3457 colorListSelect(WMWidget *w, void *data)
3459 W_ColorPanel *panel = (W_ColorPanel *)data;
3460 CPColor cpColor;
3462 cpColor.rgb = *((RColor *)WMGetListSelectedItem(w)->clientData);
3463 cpColor.set = cpRGB;
3465 panel->lastChanged = WMColorListModeColorPanel;
3466 updateSwatch(panel, cpColor);
3470 static void
3471 colorListColorMenuCallback(WMWidget *w, void *data)
3473 W_ColorPanel *panel = (W_ColorPanel *)data;
3474 int item = WMGetPopUpButtonSelectedItem(panel->colorListColorMenuBtn);
3476 switch (item) {
3477 case CLmenuAdd:
3478 break;
3479 case CLmenuRename:
3480 break;
3481 case CLmenuRemove:
3482 break;
3487 static void
3488 colorListListMenuCallback(WMWidget *w, void *data)
3490 W_ColorPanel *panel = (W_ColorPanel *)data;
3491 int item = WMGetPopUpButtonSelectedItem(panel->colorListListMenuBtn);
3493 switch (item) {
3494 case CLmenuAdd:
3495 /* New Color List */
3496 colorListListMenuNew(panel);
3497 break;
3498 case CLmenuRename:
3499 break;
3500 case CLmenuRemove:
3501 break;
3506 static void
3507 colorListListMenuNew(W_ColorPanel *panel)
3513 /*************** Panel Initialisation Functions *****************/
3515 static void
3516 wheelInit(W_ColorPanel *panel)
3518 CPColor cpColor;
3520 if (panel->color.set != cpHSV)
3521 convertCPColor(&panel->color);
3523 WMSetSliderValue(panel->wheelBrightnessS, 255 - panel->color.hsv.value);
3525 panel->colx = 2 + rint((colorWheelSize / 2.0) *
3526 (1 + ( panel->color.hsv.saturation/255.0) *
3527 cos( panel->color.hsv.hue * M_PI/180.0)));
3528 panel->coly = 2 + rint((colorWheelSize / 2.0) *
3529 (1 + ( panel->color.hsv.saturation/255.0) *
3530 sin(- panel->color.hsv.hue*M_PI/180.0)));
3532 wheelCalculateValues(panel, panel->color.hsv.value);
3534 cpColor = panel->color;
3535 cpColor.hsv.value = 255;
3536 cpColor.set = cpHSV;
3537 wheelUpdateBrightnessGradient(panel, cpColor);
3541 static void
3542 grayInit(W_ColorPanel *panel)
3544 int value;
3545 char tmp[4];
3547 if (panel->color.set != cpHSV)
3548 convertCPColor(&panel->color);
3550 value = rint(panel->color.hsv.value/2.55);
3551 WMSetSliderValue(panel->grayBrightnessS, value);
3553 sprintf(tmp, "%d", value);
3554 WMSetTextFieldText(panel->grayBrightnessT, tmp);
3558 static void
3559 rgbInit(W_ColorPanel *panel)
3561 char tmp[4];
3563 if (panel->color.set != cpRGB)
3564 convertCPColor(&panel->color);
3566 WMSetSliderValue(panel->rgbRedS, panel->color.rgb.red);
3567 WMSetSliderValue(panel->rgbGreenS, panel->color.rgb.green);
3568 WMSetSliderValue(panel->rgbBlueS, panel->color.rgb.blue);
3570 sprintf(tmp, "%d", panel->color.rgb.red);
3571 WMSetTextFieldText(panel->rgbRedT, tmp);
3572 sprintf(tmp, "%d", panel->color.rgb.green);
3573 WMSetTextFieldText(panel->rgbGreenT, tmp);
3574 sprintf(tmp, "%d", panel->color.rgb.blue);
3575 WMSetTextFieldText(panel->rgbBlueT, tmp);
3579 static void
3580 cmykInit(W_ColorPanel *panel)
3582 int value[3];
3583 char tmp[4];
3585 if (panel->color.set != cpRGB)
3586 convertCPColor(&panel->color);
3588 value[0] = rint((255-panel->color.rgb.red)/2.55);
3589 value[1] = rint((255-panel->color.rgb.green)/2.55);
3590 value[2] = rint((255-panel->color.rgb.blue)/2.55);
3592 WMSetSliderValue(panel->cmykCyanS, value[0]);
3593 WMSetSliderValue(panel->cmykMagentaS, value[1]);
3594 WMSetSliderValue(panel->cmykYellowS, value[2]);
3595 WMSetSliderValue(panel->cmykBlackS, 0);
3597 sprintf(tmp, "%d", value[0]);
3598 WMSetTextFieldText(panel->cmykCyanT, tmp);
3599 sprintf(tmp, "%d", value[1]);
3600 WMSetTextFieldText(panel->cmykMagentaT, tmp);
3601 sprintf(tmp, "%d", value[2]);
3602 WMSetTextFieldText(panel->cmykYellowT, tmp);
3603 WMSetTextFieldText(panel->cmykBlackT, "0");
3607 static void
3608 hsbInit(W_ColorPanel *panel)
3610 int value[3];
3611 char tmp[4];
3613 if (panel->color.set != cpHSV)
3614 convertCPColor(&panel->color);
3616 value[0] = panel->color.hsv.hue;
3617 value[1] = rint(panel->color.hsv.saturation/2.55);
3618 value[2] = rint(panel->color.hsv.value/2.55);
3620 WMSetSliderValue(panel->hsbHueS,value[0]);
3621 WMSetSliderValue(panel->hsbSaturationS,value[1]);
3622 WMSetSliderValue(panel->hsbBrightnessS,value[2]);
3624 sprintf(tmp, "%d", value[0]);
3625 WMSetTextFieldText(panel->hsbHueT, tmp);
3626 sprintf(tmp, "%d", value[1]);
3627 WMSetTextFieldText(panel->hsbSaturationT, tmp);
3628 sprintf(tmp, "%d", value[2]);
3629 WMSetTextFieldText(panel->hsbBrightnessT, tmp);
3631 hsbUpdateBrightnessGradient(panel);
3632 hsbUpdateSaturationGradient(panel);
3633 hsbUpdateHueGradient(panel);
3638 /************************** Common utility functions ************************/
3640 static int
3641 fetchFile(char *toPath, char *srcFile, char *destFile)
3643 int src, dest;
3644 int n;
3645 char *tmp;
3646 char buf[BUFSIZE];
3648 if ((src = open(srcFile, O_RDONLY)) == 0) {
3649 wsyserror(_("Could not open %s"), srcFile);
3650 return -1;
3653 tmp = wstrconcat(toPath, destFile);
3654 if ((dest = open( tmp, O_RDWR|O_CREAT, S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH))
3655 == 0) {
3656 wsyserror(_("Could not create %s"), tmp);
3657 wfree(tmp);
3658 return -1;
3660 wfree(tmp);
3663 /* Copy the file */
3664 while ((n = read(src, buf, BUFSIZE)) > 0)
3666 if (write (dest, buf, n) != n) {
3667 wsyserror(_("Write error on file %s"), destFile);
3668 return -1;
3672 return 0;
3676 char*
3677 generateNewFilename(char *curName)
3679 int n;
3680 char c;
3681 int baseLen;
3682 char *ptr;
3683 char *newName;
3686 assert(curName);
3688 ptr = curName;
3690 if (((ptr = strrchr(ptr, '{'))==0) || sscanf(ptr, "{%i}%c", &n, &c)!=1)
3691 return wstrconcat(curName, " {1}");
3693 baseLen = ptr - curName -1;
3695 newName = wmalloc(baseLen + 16);
3696 strncpy(newName, curName, baseLen);
3697 newName[baseLen] = 0;
3699 sprintf(&newName[baseLen], " {%i}", n+1);
3701 return newName;
3705 void
3706 convertCPColor(CPColor *color)
3708 unsigned short old_hue = 0;
3710 switch (color->set) {
3711 case cpNone:
3712 wwarning(_("Color Panel: Color unspecified"));
3713 return;
3714 case cpRGB:
3715 old_hue = color->hsv.hue;
3716 RRGBtoHSV(&(color->rgb), &(color->hsv));
3718 /* In black the hue is undefined, and may change by conversion
3719 * Same for white. */
3720 if (
3721 ((color->rgb.red == 0) &&
3722 (color->rgb.green == 0) &&
3723 (color->rgb.blue == 0)) ||
3724 ((color->rgb.red == 0) &&
3725 (color->rgb.green == 0) &&
3726 (color->rgb.blue == 255))
3728 color->hsv.hue = old_hue;
3729 break;
3730 case cpHSV:
3731 RHSVtoRGB(&(color->hsv), &(color->rgb));
3732 break;
3737 #define ABS_SHIFT(val, shift) \
3738 (((shift) > 0) ? (val) >> (shift) : (val) << -(shift))
3740 RColor
3741 ulongToRColor(WMScreen *scr, unsigned long value)
3743 RColor color;
3744 XColor *xcolor = NULL;
3746 if (!(xcolor = wmalloc(sizeof(XColor)) )) {
3747 wwarning(_("Color Panel: Could not allocate memory"));
3748 color.red = 0;
3749 color.green = 0;
3750 color.blue = 0;
3751 return color;
3754 xcolor->pixel = value;
3755 XQueryColor(scr->display, scr->rcontext->cmap, xcolor);
3757 color.red = xcolor->red >> 8;
3758 color.green = xcolor->green >> 8;
3759 color.blue = xcolor->blue >> 8;
3761 wfree(xcolor);
3763 return color;
3767 unsigned char
3768 getShift(unsigned char value)
3770 unsigned char i = -1;
3772 if (value == 0)
3773 return 0;
3775 while (value) {
3776 value >>= 1;
3777 i++;
3780 return i;
3785 #ifdef SHAPE_WAS_DEFINED
3786 #undef SHAPE_WAS_DEFINED
3787 #define SHAPE
3788 #endif