replaced free() with wfree() everywhere
[wmaker-crm.git] / WINGs / wcolorwell.c
blob85928b0ac64ffc15cc12f52f0a8edb3f7a10d92b
5 #include "WINGsP.h"
8 char *WMColorWellDidChangeNotification = "WMColorWellDidChangeNotification";
11 typedef struct W_ColorWell {
12 W_Class widgetClass;
13 WMView *view;
15 WMView *colorView;
17 WMColor *color;
19 WMAction *action;
20 void *clientData;
22 WMPoint ipoint;
24 struct {
25 unsigned int active:1;
26 unsigned int bordered:1;
27 } flags;
28 } ColorWell;
30 static char *_ColorWellActivatedNotification = "_ColorWellActivatedNotification";
34 static void destroyColorWell(ColorWell *cPtr);
35 static void paintColorWell(ColorWell *cPtr);
37 static void handleEvents(XEvent *event, void *data);
39 static void handleDragEvents(XEvent *event, void *data);
41 static void handleActionEvents(XEvent *event, void *data);
43 static void willResizeColorWell();
47 W_ViewDelegate _ColorWellViewDelegate = {
48 NULL,
49 NULL,
50 NULL,
51 NULL,
52 willResizeColorWell
56 #if 0
57 static WMDragSourceProcs dragProcs = {
60 #endif
62 #define DEFAULT_WIDTH 60
63 #define DEFAULT_HEIGHT 30
64 #define DEFAULT_BORDER_WIDTH 6
66 #define MIN_WIDTH 16
67 #define MIN_HEIGHT 8
71 static void
72 colorChangedObserver(void *data, WMNotification *notification)
74 WMColorPanel *panel = (WMColorPanel*)WMGetNotificationObject(notification);
75 WMColorWell *cPtr = (WMColorWell*)data;
76 WMColor *color;
78 if (!cPtr->flags.active)
79 return;
81 color = WMGetColorPanelColor(panel);
83 WMSetColorWellColor(cPtr, color);
84 WMPostNotificationName(WMColorWellDidChangeNotification, cPtr, NULL);
88 static void
89 updateColorCallback(void *self, void *data)
91 WMColorPanel *panel = (WMColorPanel*)self;
92 WMColorWell *cPtr = (ColorWell*)data;
93 WMColor *color;
95 color = WMGetColorPanelColor(panel);
96 WMSetColorWellColor(cPtr, color);
97 WMPostNotificationName(WMColorWellDidChangeNotification, cPtr, NULL);
102 static void
103 activatedObserver(void *data, WMNotification *notification)
106 WMColorWell *cPtr = (WMColorWell*)data;
108 if (!cPtr->flags.active || WMGetNotificationObject(notification) == cPtr)
109 return;
111 W_SetViewBackgroundColor(cPtr->view, WMWidgetScreen(cPtr)->gray);
112 paintColorWell(cPtr);
114 cPtr->flags.active = 0;
120 WMColorWell*
121 WMCreateColorWell(WMWidget *parent)
123 ColorWell *cPtr;
125 cPtr = wmalloc(sizeof(ColorWell));
126 memset(cPtr, 0, sizeof(ColorWell));
128 cPtr->widgetClass = WC_ColorWell;
130 cPtr->view = W_CreateView(W_VIEW(parent));
131 if (!cPtr->view) {
132 wfree(cPtr);
133 return NULL;
135 cPtr->view->self = cPtr;
137 cPtr->view->delegate = &_ColorWellViewDelegate;
139 cPtr->colorView = W_CreateView(cPtr->view);
140 if (!cPtr->colorView) {
141 W_DestroyView(cPtr->view);
142 wfree(cPtr);
143 return NULL;
145 cPtr->colorView->self = cPtr;
147 WMCreateEventHandler(cPtr->view, ExposureMask|StructureNotifyMask
148 |ClientMessageMask, handleEvents, cPtr);
150 WMCreateEventHandler(cPtr->colorView, ExposureMask, handleEvents, cPtr);
152 WMCreateEventHandler(cPtr->colorView, ButtonPressMask|ButtonMotionMask
153 |EnterWindowMask, handleDragEvents, cPtr);
155 WMCreateEventHandler(cPtr->view, ButtonPressMask, handleActionEvents,
156 cPtr);
158 cPtr->colorView->flags.mapWhenRealized = 1;
160 cPtr->flags.bordered = 1;
162 W_ResizeView(cPtr->view, DEFAULT_WIDTH, DEFAULT_HEIGHT);
164 WMAddNotificationObserver(activatedObserver, cPtr,
165 _ColorWellActivatedNotification, NULL);
167 cPtr->color = WMBlackColor(WMWidgetScreen(cPtr));
169 WMAddNotificationObserver(colorChangedObserver, cPtr,
170 WMColorPanelColorChangedNotification, NULL);
172 return cPtr;
176 void
177 WMSetColorWellColor(WMColorWell *cPtr, WMColor *color)
179 if (cPtr->color)
180 WMReleaseColor(cPtr->color);
182 cPtr->color = WMRetainColor(color);
184 if (cPtr->colorView->flags.realized && cPtr->colorView->flags.mapped)
185 paintColorWell(cPtr);
189 WMColor*
190 WMGetColorWellColor(WMColorWell *cPtr)
192 return cPtr->color;
196 void
197 WSetColorWellBordered(WMColorWell *cPtr, Bool flag)
199 if (cPtr->flags.bordered != flag) {
200 cPtr->flags.bordered = flag;
201 W_ResizeView(cPtr->view, cPtr->view->size.width, cPtr->view->size.height);
206 #define MIN(a,b) ((a) > (b) ? (b) : (a))
208 static void
209 willResizeColorWell(W_ViewDelegate *self, WMView *view,
210 unsigned int *width, unsigned int *height)
212 WMColorWell *cPtr = (WMColorWell*)view->self;
213 int bw;
215 if (cPtr->flags.bordered) {
217 if (*width < MIN_WIDTH)
218 *width = MIN_WIDTH;
219 if (*height < MIN_HEIGHT)
220 *height = MIN_HEIGHT;
222 bw = (int)((float)MIN(*width, *height)*0.24);
224 W_ResizeView(cPtr->colorView, *width-2*bw, *height-2*bw);
226 if (cPtr->colorView->pos.x!=bw || cPtr->colorView->pos.y!=bw)
227 W_MoveView(cPtr->colorView, bw, bw);
228 } else {
229 W_ResizeView(cPtr->colorView, *width, *height);
231 W_MoveView(cPtr->colorView, 0, 0);
236 static void
237 paintColorWell(ColorWell *cPtr)
239 W_Screen *scr = cPtr->view->screen;
241 W_DrawRelief(scr, cPtr->view->window, 0, 0, cPtr->view->size.width,
242 cPtr->view->size.height, WRRaised);
244 W_DrawRelief(scr, cPtr->colorView->window, 0, 0,
245 cPtr->colorView->size.width, cPtr->colorView->size.height,
246 WRSunken);
248 if (cPtr->color)
249 WMPaintColorSwatch(cPtr->color, cPtr->colorView->window,
250 2, 2, cPtr->colorView->size.width-4,
251 cPtr->colorView->size.height-4);
256 static void
257 handleEvents(XEvent *event, void *data)
259 ColorWell *cPtr = (ColorWell*)data;
261 CHECK_CLASS(data, WC_ColorWell);
264 switch (event->type) {
265 case Expose:
266 if (event->xexpose.count!=0)
267 break;
268 paintColorWell(cPtr);
269 break;
271 case DestroyNotify:
272 destroyColorWell(cPtr);
273 break;
279 static WMPixmap*
280 makeDragPixmap(WMColorWell *cPtr)
282 WMScreen *scr = cPtr->view->screen;
283 Pixmap pix;
285 pix = XCreatePixmap(scr->display, W_DRAWABLE(scr), 16, 16, scr->depth);
287 XFillRectangle(scr->display, pix, WMColorGC(cPtr->color), 0, 0, 15, 15);
289 XDrawRectangle(scr->display, pix, WMColorGC(scr->black), 0, 0, 15, 15);
291 return WMCreatePixmapFromXPixmaps(scr, pix, None, 16, 16, scr->depth);
295 static void
296 slideView(WMView *view, int srcX, int srcY, int dstX, int dstY)
298 double x, y, dx, dy;
299 int i;
301 srcX -= 8;
302 srcY -= 8;
303 dstX -= 8;
304 dstY -= 8;
306 x = srcX;
307 y = srcY;
309 dx = (double)(dstX-srcX)/20.0;
310 dy = (double)(dstY-srcY)/20.0;
312 for (i = 0; i < 20; i++) {
313 W_MoveView(view, x, y);
314 XFlush(view->screen->display);
316 x += dx;
317 y += dy;
322 static void
323 dragColor(ColorWell *cPtr, XEvent *event, WMPixmap *image)
325 WMView *dragView;
326 WMScreen *scr = cPtr->view->screen;
327 Display *dpy = scr->display;
328 XColor black = {0, 0,0,0, DoRed|DoGreen|DoBlue};
329 XColor green = {0x0045b045, 0x4500,0xb000,0x4500, DoRed|DoGreen|DoBlue};
330 XColor back = {0, 0xffff,0xffff,0xffff, DoRed|DoGreen|DoBlue};
331 Bool done = False;
332 WMColorWell *activeWell = NULL;
334 dragView = W_CreateTopView(scr);
336 W_ResizeView(dragView, 16, 16);
337 dragView->attribFlags |= CWOverrideRedirect | CWSaveUnder;
338 dragView->attribs.event_mask = StructureNotifyMask;
339 dragView->attribs.override_redirect = True;
340 dragView->attribs.save_under = True;
342 W_MoveView(dragView, event->xmotion.x_root-8, event->xmotion.y_root-8);
344 W_RealizeView(dragView);
346 W_MapView(dragView);
348 XSetWindowBackgroundPixmap(dpy, dragView->window, WMGetPixmapXID(image));
349 XClearWindow(dpy, dragView->window);
352 XGrabPointer(dpy, dragView->window, True,
353 ButtonMotionMask|ButtonReleaseMask|EnterWindowMask,
354 GrabModeSync, GrabModeAsync,
355 scr->rootWin, scr->defaultCursor, CurrentTime);
357 while (!done) {
358 XEvent ev;
359 WMView *view;
361 XAllowEvents(dpy, SyncPointer, CurrentTime);
362 WMNextEvent(dpy, &ev);
364 switch (ev.type) {
365 case ButtonRelease:
366 if (activeWell != NULL) {
367 WMSetColorWellColor(activeWell, cPtr->color);
368 WMPostNotificationName(WMColorWellDidChangeNotification,
369 activeWell, NULL);
370 } else {
371 slideView(dragView, ev.xbutton.x_root, ev.xbutton.y_root,
372 event->xmotion.x_root, event->xmotion.y_root);
375 done = True;
376 break;
378 case EnterNotify:
379 view = W_GetViewForXWindow(dpy, ev.xcrossing.window);
381 if (view && view->self && W_CLASS(view->self) == WC_ColorWell
382 && view->self != activeWell && view->self != cPtr) {
384 activeWell = view->self;
385 XRecolorCursor(dpy, scr->defaultCursor, &green, &back);
386 } else if (view->self!=NULL && view->self != activeWell) {
387 XRecolorCursor(dpy, scr->defaultCursor, &black, &back);
388 activeWell = NULL;
390 break;
392 case MotionNotify:
393 W_MoveView(dragView, ev.xmotion.x_root-8, ev.xmotion.y_root-8);
394 break;
396 default:
397 WMHandleEvent(&ev);
398 break;
401 XUngrabPointer(dpy, CurrentTime);
402 XRecolorCursor(dpy, scr->defaultCursor, &black, &back);
404 W_DestroyView(dragView);
408 static void
409 handleDragEvents(XEvent *event, void *data)
411 WMColorWell *cPtr = (ColorWell*)data;
413 switch (event->type) {
414 case ButtonPress:
415 if (event->xbutton.button == Button1) {
416 cPtr->ipoint.x = event->xbutton.x;
417 cPtr->ipoint.y = event->xbutton.y;
419 break;
421 case MotionNotify:
422 if (event->xmotion.state & Button1Mask) {
423 if (abs(cPtr->ipoint.x - event->xmotion.x) > 4
424 || abs(cPtr->ipoint.y - event->xmotion.y) > 4) {
425 WMSize offs;
426 WMPixmap *pixmap;
428 offs.width = 2;
429 offs.height = 2;
430 pixmap = makeDragPixmap(cPtr);
433 WMDragImageFromView(cPtr->view, pixmap, cPtr->view->pos,
434 offs, event, True);
435 * */
437 dragColor(cPtr, event, pixmap);
439 WMReleasePixmap(pixmap);
442 break;
447 static void
448 handleActionEvents(XEvent *event, void *data)
450 WMColorWell *cPtr = (ColorWell*)data;
451 WMScreen *scr = WMWidgetScreen(cPtr);
452 WMColorPanel *cpanel;
454 if (cPtr->flags.active)
455 W_SetViewBackgroundColor(cPtr->view, scr->gray);
456 else
457 W_SetViewBackgroundColor(cPtr->view, scr->white);
458 paintColorWell(cPtr);
460 cPtr->flags.active ^= 1;
462 if (cPtr->flags.active) {
463 WMPostNotificationName(_ColorWellActivatedNotification, cPtr, NULL);
465 cpanel = WMGetColorPanel(scr);
467 WMSetColorPanelAction(cpanel, updateColorCallback, cPtr);
469 if (cPtr->color)
470 WMSetColorPanelColor(cpanel, cPtr->color);
471 WMShowColorPanel(cpanel);
475 static void
476 destroyColorWell(ColorWell *cPtr)
478 WMRemoveNotificationObserver(cPtr);
480 if (cPtr->color)
481 WMReleaseColor(cPtr->color);
483 wfree(cPtr);