WINGs/Tests: make them compile again
[wmaker-crm.git] / WINGs / wcolorwell.c
blob595531c400bd7e96a1d69151d1273f15684ae79b
2 #include "WINGsP.h"
4 #define XDND_COLOR_DATA_TYPE "application/X-color"
6 char *WMColorWellDidChangeNotification = "WMColorWellDidChangeNotification";
8 typedef struct W_ColorWell {
9 W_Class widgetClass;
10 WMView *view;
12 WMView *colorView;
14 WMColor *color;
16 WMAction *action;
17 void *clientData;
19 WMPoint ipoint;
21 struct {
22 unsigned int active:1;
23 unsigned int bordered:1;
24 } flags;
26 WMArray *xdndTypes;
27 } ColorWell;
29 static char *_ColorWellActivatedNotification = "_ColorWellActivatedNotification";
31 static void destroyColorWell(ColorWell * cPtr);
32 static void paintColorWell(ColorWell * cPtr);
34 static void handleEvents(XEvent * event, void *data);
36 static void handleDragEvents(XEvent * event, void *data);
38 static void handleActionEvents(XEvent * event, void *data);
40 static void willResizeColorWell(W_ViewDelegate * self, WMView * view, unsigned int *width, unsigned int *height);
42 W_ViewDelegate _ColorWellViewDelegate = {
43 NULL,
44 NULL,
45 NULL,
46 NULL,
47 willResizeColorWell
50 static WMArray *dropDataTypes(WMView * self);
51 static WMDragOperationType wantedDropOperation(WMView * self);
52 static Bool acceptDropOperation(WMView * self, WMDragOperationType operation);
53 static WMData *fetchDragData(WMView * self, char *type);
55 static WMDragSourceProcs _DragSourceProcs = {
56 dropDataTypes,
57 wantedDropOperation,
58 NULL,
59 acceptDropOperation,
60 NULL,
61 NULL,
62 fetchDragData
65 static WMArray *requiredDataTypes(WMView * self,
66 WMDragOperationType requestedOperation, WMArray * sourceDataTypes);
67 static WMDragOperationType allowedOperation(WMView * self,
68 WMDragOperationType requestedOperation, WMArray * sourceDataTypes);
69 static void performDragOperation(WMView * self, WMArray * dropDatas,
70 WMArray * operationsList, WMPoint * dropLocation);
72 static WMDragDestinationProcs _DragDestinationProcs = {
73 NULL,
74 requiredDataTypes,
75 allowedOperation,
76 NULL,
77 performDragOperation,
78 NULL
81 #define DEFAULT_WIDTH 60
82 #define DEFAULT_HEIGHT 30
83 #define DEFAULT_BORDER_WIDTH 6
85 #define MIN_WIDTH 16
86 #define MIN_HEIGHT 8
88 static void colorChangedObserver(void *data, WMNotification * notification)
90 WMColorPanel *panel = (WMColorPanel *) WMGetNotificationObject(notification);
91 WMColorWell *cPtr = (WMColorWell *) data;
92 WMColor *color;
94 if (!cPtr->flags.active)
95 return;
97 color = WMGetColorPanelColor(panel);
99 WMSetColorWellColor(cPtr, color);
100 WMPostNotificationName(WMColorWellDidChangeNotification, cPtr, NULL);
103 static void updateColorCallback(void *self, void *data)
105 WMColorPanel *panel = (WMColorPanel *) self;
106 WMColorWell *cPtr = (ColorWell *) data;
107 WMColor *color;
109 color = WMGetColorPanelColor(panel);
110 WMSetColorWellColor(cPtr, color);
111 WMPostNotificationName(WMColorWellDidChangeNotification, cPtr, NULL);
114 static WMArray *getXdndTypeArray(void)
116 WMArray *types = WMCreateArray(1);
117 WMAddToArray(types, XDND_COLOR_DATA_TYPE);
118 return types;
121 WMColorWell *WMCreateColorWell(WMWidget * parent)
123 ColorWell *cPtr;
125 cPtr = wmalloc(sizeof(ColorWell));
127 cPtr->widgetClass = WC_ColorWell;
129 cPtr->view = W_CreateView(W_VIEW(parent));
130 if (!cPtr->view) {
131 wfree(cPtr);
132 return NULL;
134 cPtr->view->self = cPtr;
136 cPtr->view->delegate = &_ColorWellViewDelegate;
138 cPtr->colorView = W_CreateView(cPtr->view);
139 if (!cPtr->colorView) {
140 W_DestroyView(cPtr->view);
141 wfree(cPtr);
142 return NULL;
144 cPtr->colorView->self = cPtr;
146 WMCreateEventHandler(cPtr->view, ExposureMask | StructureNotifyMask
147 | ClientMessageMask, handleEvents, cPtr);
149 WMCreateEventHandler(cPtr->colorView, ExposureMask, handleEvents, cPtr);
151 WMCreateDragHandler(cPtr->colorView, handleDragEvents, cPtr);
153 WMCreateEventHandler(cPtr->view, ButtonPressMask, handleActionEvents, cPtr);
154 WMCreateEventHandler(cPtr->colorView, ButtonPressMask, handleActionEvents, cPtr);
156 cPtr->colorView->flags.mapWhenRealized = 1;
158 cPtr->flags.bordered = 1;
160 W_ResizeView(cPtr->view, DEFAULT_WIDTH, DEFAULT_HEIGHT);
162 cPtr->color = WMBlackColor(WMWidgetScreen(cPtr));
164 WMAddNotificationObserver(colorChangedObserver, cPtr, WMColorPanelColorChangedNotification, NULL);
166 WMSetViewDragSourceProcs(cPtr->colorView, &_DragSourceProcs);
167 WMSetViewDragDestinationProcs(cPtr->colorView, &_DragDestinationProcs);
169 cPtr->xdndTypes = getXdndTypeArray();
170 WMRegisterViewForDraggedTypes(cPtr->colorView, cPtr->xdndTypes);
172 return cPtr;
175 void WMSetColorWellColor(WMColorWell * cPtr, WMColor * color)
177 if (cPtr->color)
178 WMReleaseColor(cPtr->color);
180 cPtr->color = WMRetainColor(color);
182 if (cPtr->colorView->flags.realized && cPtr->colorView->flags.mapped)
183 paintColorWell(cPtr);
186 WMColor *WMGetColorWellColor(WMColorWell * cPtr)
188 return cPtr->color;
191 void WSetColorWellBordered(WMColorWell * cPtr, Bool flag)
193 flag = ((flag == 0) ? 0 : 1);
194 if (cPtr->flags.bordered != flag) {
195 cPtr->flags.bordered = flag;
196 W_ResizeView(cPtr->view, cPtr->view->size.width, cPtr->view->size.height);
200 static void willResizeColorWell(W_ViewDelegate * self, WMView * view, unsigned int *width, unsigned int *height)
202 WMColorWell *cPtr = (WMColorWell *) view->self;
203 int bw;
205 /* Parameter not used, but tell the compiler that it is ok */
206 (void) self;
208 if (cPtr->flags.bordered) {
210 if (*width < MIN_WIDTH)
211 *width = MIN_WIDTH;
212 if (*height < MIN_HEIGHT)
213 *height = MIN_HEIGHT;
215 bw = (int)((float)WMIN(*width, *height) * 0.24);
217 W_ResizeView(cPtr->colorView, *width - 2 * bw, *height - 2 * bw);
219 if (cPtr->colorView->pos.x != bw || cPtr->colorView->pos.y != bw)
220 W_MoveView(cPtr->colorView, bw, bw);
221 } else {
222 W_ResizeView(cPtr->colorView, *width, *height);
224 W_MoveView(cPtr->colorView, 0, 0);
228 static void paintColorWell(ColorWell * cPtr)
230 W_Screen *scr = cPtr->view->screen;
232 W_DrawRelief(scr, cPtr->view->window, 0, 0, cPtr->view->size.width, cPtr->view->size.height, WRRaised);
234 W_DrawRelief(scr, cPtr->colorView->window, 0, 0,
235 cPtr->colorView->size.width, cPtr->colorView->size.height, WRSunken);
237 if (cPtr->color)
238 WMPaintColorSwatch(cPtr->color, cPtr->colorView->window,
239 2, 2, cPtr->colorView->size.width - 4, cPtr->colorView->size.height - 4);
242 static void handleEvents(XEvent * event, void *data)
244 ColorWell *cPtr = (ColorWell *) data;
246 CHECK_CLASS(data, WC_ColorWell);
248 switch (event->type) {
249 case Expose:
250 if (event->xexpose.count != 0)
251 break;
252 paintColorWell(cPtr);
253 break;
255 case DestroyNotify:
256 destroyColorWell(cPtr);
257 break;
262 static WMArray *dropDataTypes(WMView * self)
264 return ((ColorWell *) self->self)->xdndTypes;
267 static WMDragOperationType wantedDropOperation(WMView * self)
269 /* Parameter not used, but tell the compiler that it is ok */
270 (void) self;
272 return WDOperationCopy;
275 static Bool acceptDropOperation(WMView * self, WMDragOperationType operation)
277 /* Parameter not used, but tell the compiler that it is ok */
278 (void) self;
280 return (operation == WDOperationCopy);
283 static WMData *fetchDragData(WMView * self, char *type)
285 char *color = WMGetColorRGBDescription(((WMColorWell *) self->self)->color);
286 WMData *data;
288 /* Parameter not used, but tell the compiler that it is ok */
289 (void) type;
291 data = WMCreateDataWithBytes(color, strlen(color) + 1);
292 wfree(color);
294 return data;
297 static WMPixmap *makeDragPixmap(WMColorWell * cPtr)
299 WMScreen *scr = cPtr->view->screen;
300 Pixmap pix;
302 pix = XCreatePixmap(scr->display, W_DRAWABLE(scr), 16, 16, scr->depth);
304 XFillRectangle(scr->display, pix, WMColorGC(cPtr->color), 0, 0, 15, 15);
306 XDrawRectangle(scr->display, pix, WMColorGC(scr->black), 0, 0, 15, 15);
308 return WMCreatePixmapFromXPixmaps(scr, pix, None, 16, 16, scr->depth);
311 static void handleDragEvents(XEvent * event, void *data)
313 WMColorWell *cPtr = (ColorWell *) data;
315 if (event->type == ButtonPress && event->xbutton.button == Button1) {
316 /* initialise drag icon */
317 WMSetViewDragImage(cPtr->colorView, makeDragPixmap(cPtr));
320 WMDragImageFromView(cPtr->colorView, event);
323 static void handleActionEvents(XEvent * event, void *data)
325 WMColorWell *cPtr = (ColorWell *) data;
326 WMScreen *scr = WMWidgetScreen(cPtr);
327 WMColorPanel *cpanel;
329 /* Parameter not used, but tell the compiler that it is ok */
330 (void) event;
332 if (cPtr->flags.active)
333 W_SetViewBackgroundColor(cPtr->view, scr->gray);
334 else
335 W_SetViewBackgroundColor(cPtr->view, scr->white);
336 paintColorWell(cPtr);
338 cPtr->flags.active ^= 1;
340 if (cPtr->flags.active) {
341 WMPostNotificationName(_ColorWellActivatedNotification, cPtr, NULL);
343 cpanel = WMGetColorPanel(scr);
345 WMSetColorPanelAction(cpanel, updateColorCallback, cPtr);
347 if (cPtr->color)
348 WMSetColorPanelColor(cpanel, cPtr->color);
349 WMShowColorPanel(cpanel);
352 static void destroyColorWell(ColorWell * cPtr)
354 WMRemoveNotificationObserver(cPtr);
356 if (cPtr->color)
357 WMReleaseColor(cPtr->color);
359 WMFreeArray(cPtr->xdndTypes);
361 wfree(cPtr);
364 static Bool dropIsOk(WMDragOperationType request, WMArray * sourceDataTypes)
366 WMArrayIterator iter;
367 char *type;
369 if (request == WDOperationCopy) {
370 WM_ITERATE_ARRAY(sourceDataTypes, type, iter) {
371 if (type != NULL && strcmp(type, XDND_COLOR_DATA_TYPE) == 0) {
372 return True;
377 return False;
380 static WMArray *requiredDataTypes(WMView * self, WMDragOperationType request, WMArray * sourceDataTypes)
382 if (dropIsOk(request, sourceDataTypes))
383 return ((ColorWell *) self->self)->xdndTypes;
384 else
385 return NULL;
388 static WMDragOperationType allowedOperation(WMView * self, WMDragOperationType request, WMArray * sourceDataTypes)
390 /* Parameter not used, but tell the compiler that it is ok */
391 (void) self;
393 if (dropIsOk(request, sourceDataTypes))
394 return WDOperationCopy;
395 else
396 return WDOperationNone;
399 static void performDragOperation(WMView * self, WMArray * dropData, WMArray * operations, WMPoint * dropLocation)
401 char *colorName;
402 WMColor *color;
403 WMData *data;
405 /* Parameter not used, but tell the compiler that it is ok */
406 (void) operations;
407 (void) dropLocation;
409 /* only one operation requested (WDOperationCopy) implies only one data */
410 data = (WMData *) WMGetFromArray(dropData, 0);
412 if (data != NULL) {
413 colorName = (char *)WMDataBytes(data);
414 color = WMCreateNamedColor(W_VIEW_SCREEN(self), colorName, True);
415 WMSetColorWellColor(self->self, color);
416 WMReleaseColor(color);