- Replaced all free() with wfree() where appropriate
[wmaker-crm.git] / WINGs / wcolorwell.c
blob25934352e060f6aca5b5de87f0748a7e47da8265
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 static unsigned draggingSourceOperation(WMView *self, Bool local);
58 static WMData* fetchDragData(WMView *self, char *type);
60 static WMDragSourceProcs _DragSourceProcs = {
61 draggingSourceOperation,
62 NULL,
63 NULL,
64 fetchDragData
68 static unsigned draggingEntered(WMView *self, WMDraggingInfo *info);
69 static unsigned draggingUpdated(WMView *self, WMDraggingInfo *info);
70 static void draggingExited(WMView *self, WMDraggingInfo *info);
71 static char *prepareForDragOperation(WMView *self, WMDraggingInfo *info);
72 static Bool performDragOperation(WMView *self, WMDraggingInfo *info,
73 WMData *data);
74 static void concludeDragOperation(WMView *self, WMDraggingInfo *info);
76 static WMDragDestinationProcs _DragDestinationProcs = {
77 draggingEntered,
78 draggingUpdated,
79 draggingExited,
80 prepareForDragOperation,
81 performDragOperation,
82 concludeDragOperation
86 #define DEFAULT_WIDTH 60
87 #define DEFAULT_HEIGHT 30
88 #define DEFAULT_BORDER_WIDTH 6
90 #define MIN_WIDTH 16
91 #define MIN_HEIGHT 8
95 static void
96 colorChangedObserver(void *data, WMNotification *notification)
98 WMColorPanel *panel = (WMColorPanel*)WMGetNotificationObject(notification);
99 WMColorWell *cPtr = (WMColorWell*)data;
100 WMColor *color;
102 if (!cPtr->flags.active)
103 return;
105 color = WMGetColorPanelColor(panel);
107 WMSetColorWellColor(cPtr, color);
108 WMPostNotificationName(WMColorWellDidChangeNotification, cPtr, NULL);
112 static void
113 updateColorCallback(void *self, void *data)
115 WMColorPanel *panel = (WMColorPanel*)self;
116 WMColorWell *cPtr = (ColorWell*)data;
117 WMColor *color;
119 color = WMGetColorPanelColor(panel);
120 WMSetColorWellColor(cPtr, color);
121 WMPostNotificationName(WMColorWellDidChangeNotification, cPtr, NULL);
126 static void
127 activatedObserver(void *data, WMNotification *notification)
130 WMColorWell *cPtr = (WMColorWell*)data;
132 if (!cPtr->flags.active || WMGetNotificationObject(notification) == cPtr)
133 return;
135 W_SetViewBackgroundColor(cPtr->view, WMWidgetScreen(cPtr)->gray);
136 paintColorWell(cPtr);
138 cPtr->flags.active = 0;
144 WMColorWell*
145 WMCreateColorWell(WMWidget *parent)
147 ColorWell *cPtr;
149 cPtr = wmalloc(sizeof(ColorWell));
150 memset(cPtr, 0, sizeof(ColorWell));
152 cPtr->widgetClass = WC_ColorWell;
154 cPtr->view = W_CreateView(W_VIEW(parent));
155 if (!cPtr->view) {
156 wfree(cPtr);
157 return NULL;
159 cPtr->view->self = cPtr;
161 cPtr->view->delegate = &_ColorWellViewDelegate;
163 cPtr->colorView = W_CreateView(cPtr->view);
164 if (!cPtr->colorView) {
165 W_DestroyView(cPtr->view);
166 wfree(cPtr);
167 return NULL;
169 cPtr->colorView->self = cPtr;
171 WMCreateEventHandler(cPtr->view, ExposureMask|StructureNotifyMask
172 |ClientMessageMask, handleEvents, cPtr);
174 WMCreateEventHandler(cPtr->colorView, ExposureMask, handleEvents, cPtr);
176 WMCreateEventHandler(cPtr->colorView, ButtonPressMask|ButtonMotionMask
177 |EnterWindowMask, handleDragEvents, cPtr);
179 WMCreateEventHandler(cPtr->view, ButtonPressMask, handleActionEvents,
180 cPtr);
182 cPtr->colorView->flags.mapWhenRealized = 1;
184 cPtr->flags.bordered = 1;
186 W_ResizeView(cPtr->view, DEFAULT_WIDTH, DEFAULT_HEIGHT);
188 WMAddNotificationObserver(activatedObserver, cPtr,
189 _ColorWellActivatedNotification, NULL);
191 cPtr->color = WMBlackColor(WMWidgetScreen(cPtr));
193 WMAddNotificationObserver(colorChangedObserver, cPtr,
194 WMColorPanelColorChangedNotification, NULL);
196 WMSetViewDragSourceProcs(cPtr->view, &_DragSourceProcs);
197 WMSetViewDragDestinationProcs(cPtr->view, &_DragDestinationProcs);
200 char *types[2] = {"application/X-color", NULL};
202 WMRegisterViewForDraggedTypes(cPtr->view, types);
205 return cPtr;
209 void
210 WMSetColorWellColor(WMColorWell *cPtr, WMColor *color)
212 if (cPtr->color)
213 WMReleaseColor(cPtr->color);
215 cPtr->color = WMRetainColor(color);
217 if (cPtr->colorView->flags.realized && cPtr->colorView->flags.mapped)
218 paintColorWell(cPtr);
222 WMColor*
223 WMGetColorWellColor(WMColorWell *cPtr)
225 return cPtr->color;
229 void
230 WSetColorWellBordered(WMColorWell *cPtr, Bool flag)
232 if (cPtr->flags.bordered != flag) {
233 cPtr->flags.bordered = flag;
234 W_ResizeView(cPtr->view, cPtr->view->size.width, cPtr->view->size.height);
239 #define MIN(a,b) ((a) > (b) ? (b) : (a))
241 static void
242 willResizeColorWell(W_ViewDelegate *self, WMView *view,
243 unsigned int *width, unsigned int *height)
245 WMColorWell *cPtr = (WMColorWell*)view->self;
246 int bw;
248 if (cPtr->flags.bordered) {
250 if (*width < MIN_WIDTH)
251 *width = MIN_WIDTH;
252 if (*height < MIN_HEIGHT)
253 *height = MIN_HEIGHT;
255 bw = (int)((float)MIN(*width, *height)*0.24);
257 W_ResizeView(cPtr->colorView, *width-2*bw, *height-2*bw);
259 if (cPtr->colorView->pos.x!=bw || cPtr->colorView->pos.y!=bw)
260 W_MoveView(cPtr->colorView, bw, bw);
261 } else {
262 W_ResizeView(cPtr->colorView, *width, *height);
264 W_MoveView(cPtr->colorView, 0, 0);
269 static void
270 paintColorWell(ColorWell *cPtr)
272 W_Screen *scr = cPtr->view->screen;
274 W_DrawRelief(scr, cPtr->view->window, 0, 0, cPtr->view->size.width,
275 cPtr->view->size.height, WRRaised);
277 W_DrawRelief(scr, cPtr->colorView->window, 0, 0,
278 cPtr->colorView->size.width, cPtr->colorView->size.height,
279 WRSunken);
281 if (cPtr->color)
282 WMPaintColorSwatch(cPtr->color, cPtr->colorView->window,
283 2, 2, cPtr->colorView->size.width-4,
284 cPtr->colorView->size.height-4);
289 static void
290 handleEvents(XEvent *event, void *data)
292 ColorWell *cPtr = (ColorWell*)data;
294 CHECK_CLASS(data, WC_ColorWell);
297 switch (event->type) {
298 case Expose:
299 if (event->xexpose.count!=0)
300 break;
301 paintColorWell(cPtr);
302 break;
304 case DestroyNotify:
305 destroyColorWell(cPtr);
306 break;
312 static unsigned
313 draggingSourceOperation(WMView *self, Bool local)
315 return WDOperationCopy;
319 static WMData*
320 fetchDragData(WMView *self, char *type)
322 char *color = WMGetColorRGBDescription(((WMColorWell*)self->self)->color);
323 WMData *data;
325 data = WMCreateDataWithBytes(color, strlen(color)+1);
327 wfree(color);
329 return data;
333 static WMPixmap*
334 makeDragPixmap(WMColorWell *cPtr)
336 WMScreen *scr = cPtr->view->screen;
337 Pixmap pix;
339 pix = XCreatePixmap(scr->display, W_DRAWABLE(scr), 16, 16, scr->depth);
341 XFillRectangle(scr->display, pix, WMColorGC(cPtr->color), 0, 0, 15, 15);
343 XDrawRectangle(scr->display, pix, WMColorGC(scr->black), 0, 0, 15, 15);
345 return WMCreatePixmapFromXPixmaps(scr, pix, None, 16, 16, scr->depth);
349 static void
350 handleDragEvents(XEvent *event, void *data)
352 WMColorWell *cPtr = (ColorWell*)data;
354 switch (event->type) {
355 case ButtonPress:
356 if (event->xbutton.button == Button1) {
357 cPtr->ipoint.x = event->xbutton.x;
358 cPtr->ipoint.y = event->xbutton.y;
360 break;
362 case MotionNotify:
363 if (event->xmotion.state & Button1Mask) {
364 if (abs(cPtr->ipoint.x - event->xmotion.x) > 4
365 || abs(cPtr->ipoint.y - event->xmotion.y) > 4) {
366 WMSize offs;
367 WMPixmap *pixmap;
368 char *types[2] = {"application/X-color", NULL};
370 offs.width = 2;
371 offs.height = 2;
372 pixmap = makeDragPixmap(cPtr);
374 WMDragImageFromView(cPtr->view, pixmap, types,
375 wmkpoint(event->xmotion.x_root,
376 event->xmotion.y_root),
377 offs, event, True);
380 WMReleasePixmap(pixmap);
383 break;
388 static void
389 handleActionEvents(XEvent *event, void *data)
391 WMColorWell *cPtr = (ColorWell*)data;
392 WMScreen *scr = WMWidgetScreen(cPtr);
393 WMColorPanel *cpanel;
395 if (cPtr->flags.active)
396 W_SetViewBackgroundColor(cPtr->view, scr->gray);
397 else
398 W_SetViewBackgroundColor(cPtr->view, scr->white);
399 paintColorWell(cPtr);
401 cPtr->flags.active ^= 1;
403 if (cPtr->flags.active) {
404 WMPostNotificationName(_ColorWellActivatedNotification, cPtr, NULL);
406 cpanel = WMGetColorPanel(scr);
408 WMSetColorPanelAction(cpanel, updateColorCallback, cPtr);
410 if (cPtr->color)
411 WMSetColorPanelColor(cpanel, cPtr->color);
412 WMShowColorPanel(cpanel);
416 static void
417 destroyColorWell(ColorWell *cPtr)
419 WMRemoveNotificationObserver(cPtr);
421 if (cPtr->color)
422 WMReleaseColor(cPtr->color);
424 wfree(cPtr);
429 static unsigned
430 draggingEntered(WMView *self, WMDraggingInfo *info)
432 return WDOperationCopy;
436 static unsigned
437 draggingUpdated(WMView *self, WMDraggingInfo *info)
439 return WDOperationCopy;
443 static void
444 draggingExited(WMView *self, WMDraggingInfo *info)
450 static char*
451 prepareForDragOperation(WMView *self, WMDraggingInfo *info)
453 return "application/X-color";
457 static Bool
458 performDragOperation(WMView *self, WMDraggingInfo *info, WMData *data)
460 char *colorName = (char*)WMDataBytes(data);
461 WMColor *color;
463 color = WMCreateNamedColor(W_VIEW_SCREEN(self), colorName, True);
465 WMSetColorWellColor(self->self, color);
467 WMReleaseColor(color);
469 return True;
473 static void
474 concludeDragOperation(WMView *self, WMDraggingInfo *info)