Change to the linux kernel coding style
[wmaker-crm.git] / WINGs / wcolorwell.c
Commit [+]AuthorDateLineData
9d2e6ef9 scottc1998-09-29 22:36:29 +00001
9d2e6ef9 scottc1998-09-29 22:36:29 +00002#include "WINGsP.h"
3
e7d0c5d9 dan2004-04-07 02:50:52 +00004#define XDND_COLOR_DATA_TYPE "application/X-color"
9d2e6ef9 scottc1998-09-29 22:36:29 +00005
c56756dc dan1999-03-14 22:35:50 +00006char *WMColorWellDidChangeNotification = "WMColorWellDidChangeNotification";
7
9d2e6ef9 scottc1998-09-29 22:36:29 +00008typedef struct W_ColorWell {
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02009 W_Class widgetClass;
10 WMView *view;
e7d0c5d9 dan2004-04-07 02:50:52 +000011
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +020012 WMView *colorView;
9d2e6ef9 scottc1998-09-29 22:36:29 +000013
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +020014 WMColor *color;
9d2e6ef9 scottc1998-09-29 22:36:29 +000015
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +020016 WMAction *action;
17 void *clientData;
9d2e6ef9 scottc1998-09-29 22:36:29 +000018
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +020019 WMPoint ipoint;
9d2e6ef9 scottc1998-09-29 22:36:29 +000020
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +020021 struct {
22 unsigned int active:1;
23 unsigned int bordered:1;
24 } flags;
e7d0c5d9 dan2004-04-07 02:50:52 +000025
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +020026 WMArray *xdndTypes;
9d2e6ef9 scottc1998-09-29 22:36:29 +000027} ColorWell;
28
088c0ac2 dan1999-03-09 14:58:01 +000029static char *_ColorWellActivatedNotification = "_ColorWellActivatedNotification";
30
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +020031static void destroyColorWell(ColorWell * cPtr);
32static void paintColorWell(ColorWell * cPtr);
088c0ac2 dan1999-03-09 14:58:01 +000033
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +020034static void handleEvents(XEvent * event, void *data);
9d2e6ef9 scottc1998-09-29 22:36:29 +000035
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +020036static void handleDragEvents(XEvent * event, void *data);
0261c326 dan1999-01-06 15:22:33 +000037
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +020038static void handleActionEvents(XEvent * event, void *data);
9d2e6ef9 scottc1998-09-29 22:36:29 +000039
5e4625da kojima1999-05-29 21:41:25 +000040static void willResizeColorWell();
9d2e6ef9 scottc1998-09-29 22:36:29 +000041
5e4625da kojima1999-05-29 21:41:25 +000042W_ViewDelegate _ColorWellViewDelegate = {
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +020043 NULL,
44 NULL,
45 NULL,
46 NULL,
47 willResizeColorWell
9d2e6ef9 scottc1998-09-29 22:36:29 +000048};
49
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +020050static WMArray *dropDataTypes(WMView * self);
51static WMDragOperationType wantedDropOperation(WMView * self);
52static Bool acceptDropOperation(WMView * self, WMDragOperationType operation);
53static WMData *fetchDragData(WMView * self, char *type);
60a247f2 kojima2000-04-03 03:10:20 +000054
55static WMDragSourceProcs _DragSourceProcs = {
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +020056 dropDataTypes,
57 wantedDropOperation,
58 NULL,
59 acceptDropOperation,
60 NULL,
61 NULL,
62 fetchDragData
9d2e6ef9 scottc1998-09-29 22:36:29 +000063};
60a247f2 kojima2000-04-03 03:10:20 +000064
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +020065static WMArray *requiredDataTypes(WMView * self,
66 WMDragOperationType requestedOperation, WMArray * sourceDataTypes);
67static WMDragOperationType allowedOperation(WMView * self,
68 WMDragOperationType requestedOperation, WMArray * sourceDataTypes);
69static void performDragOperation(WMView * self, WMArray * dropDatas,
70 WMArray * operationsList, WMPoint * dropLocation);
60a247f2 kojima2000-04-03 03:10:20 +000071
72static WMDragDestinationProcs _DragDestinationProcs = {
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +020073 NULL,
74 requiredDataTypes,
75 allowedOperation,
76 NULL,
77 performDragOperation,
78 NULL
60a247f2 kojima2000-04-03 03:10:20 +000079};
80
9d2e6ef9 scottc1998-09-29 22:36:29 +000081#define DEFAULT_WIDTH 60
82#define DEFAULT_HEIGHT 30
83#define DEFAULT_BORDER_WIDTH 6
84
85#define MIN_WIDTH 16
86#define MIN_HEIGHT 8
87
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +020088static void colorChangedObserver(void *data, WMNotification * notification)
bedc2238 kojima1999-05-16 07:00:01 +000089{
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +020090 WMColorPanel *panel = (WMColorPanel *) WMGetNotificationObject(notification);
91 WMColorWell *cPtr = (WMColorWell *) data;
92 WMColor *color;
bedc2238 kojima1999-05-16 07:00:01 +000093
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +020094 if (!cPtr->flags.active)
95 return;
bedc2238 kojima1999-05-16 07:00:01 +000096
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +020097 color = WMGetColorPanelColor(panel);
e7d0c5d9 dan2004-04-07 02:50:52 +000098
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +020099 WMSetColorWellColor(cPtr, color);
100 WMPostNotificationName(WMColorWellDidChangeNotification, cPtr, NULL);
bedc2238 kojima1999-05-16 07:00:01 +0000101}
102
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200103static void updateColorCallback(void *self, void *data)
bedc2238 kojima1999-05-16 07:00:01 +0000104{
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200105 WMColorPanel *panel = (WMColorPanel *) self;
106 WMColorWell *cPtr = (ColorWell *) data;
107 WMColor *color;
bedc2238 kojima1999-05-16 07:00:01 +0000108
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200109 color = WMGetColorPanelColor(panel);
110 WMSetColorWellColor(cPtr, color);
111 WMPostNotificationName(WMColorWellDidChangeNotification, cPtr, NULL);
bedc2238 kojima1999-05-16 07:00:01 +0000112}
113
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200114static void activatedObserver(void *data, WMNotification * notification)
088c0ac2 dan1999-03-09 14:58:01 +0000115{
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200116 /*
117 WMColorWell *cPtr = (WMColorWell*)data;
088c0ac2 dan1999-03-09 14:58:01 +0000118
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200119 if (!cPtr->flags.active || WMGetNotificationObject(notification) == cPtr)
120 return;
088c0ac2 dan1999-03-09 14:58:01 +0000121
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200122 W_SetViewBackgroundColor(cPtr->view, WMWidgetScreen(cPtr)->gray);
123 paintColorWell(cPtr);
088c0ac2 dan1999-03-09 14:58:01 +0000124
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200125 cPtr->flags.active = 0;
126 */
088c0ac2 dan1999-03-09 14:58:01 +0000127}
128
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200129static WMArray *getXdndTypeArray()
e7d0c5d9 dan2004-04-07 02:50:52 +0000130{
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200131 WMArray *types = WMCreateArray(1);
132 WMAddToArray(types, XDND_COLOR_DATA_TYPE);
133 return types;
e7d0c5d9 dan2004-04-07 02:50:52 +0000134}
135
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200136WMColorWell *WMCreateColorWell(WMWidget * parent)
9d2e6ef9 scottc1998-09-29 22:36:29 +0000137{
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200138 ColorWell *cPtr;
9d2e6ef9 scottc1998-09-29 22:36:29 +0000139
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200140 cPtr = wmalloc(sizeof(ColorWell));
141 memset(cPtr, 0, sizeof(ColorWell));
9d2e6ef9 scottc1998-09-29 22:36:29 +0000142
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200143 cPtr->widgetClass = WC_ColorWell;
e7d0c5d9 dan2004-04-07 02:50:52 +0000144
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200145 cPtr->view = W_CreateView(W_VIEW(parent));
146 if (!cPtr->view) {
147 wfree(cPtr);
148 return NULL;
149 }
150 cPtr->view->self = cPtr;
5e4625da kojima1999-05-29 21:41:25 +0000151
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200152 cPtr->view->delegate = &_ColorWellViewDelegate;
064f79eb dan2002-12-20 17:47:31 +0000153
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200154 cPtr->colorView = W_CreateView(cPtr->view);
155 if (!cPtr->colorView) {
156 W_DestroyView(cPtr->view);
157 wfree(cPtr);
158 return NULL;
159 }
160 cPtr->colorView->self = cPtr;
064f79eb dan2002-12-20 17:47:31 +0000161
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200162 WMCreateEventHandler(cPtr->view, ExposureMask | StructureNotifyMask
163 | ClientMessageMask, handleEvents, cPtr);
064f79eb dan2002-12-20 17:47:31 +0000164
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200165 WMCreateEventHandler(cPtr->colorView, ExposureMask, handleEvents, cPtr);
0261c326 dan1999-01-06 15:22:33 +0000166
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200167 WMCreateDragHandler(cPtr->colorView, handleDragEvents, cPtr);
0261c326 dan1999-01-06 15:22:33 +0000168
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200169 WMCreateEventHandler(cPtr->view, ButtonPressMask, handleActionEvents, cPtr);
9d2e6ef9 scottc1998-09-29 22:36:29 +0000170
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200171 cPtr->colorView->flags.mapWhenRealized = 1;
0261c326 dan1999-01-06 15:22:33 +0000172
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200173 cPtr->flags.bordered = 1;
088c0ac2 dan1999-03-09 14:58:01 +0000174
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200175 W_ResizeView(cPtr->view, DEFAULT_WIDTH, DEFAULT_HEIGHT);
9d2e6ef9 scottc1998-09-29 22:36:29 +0000176
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200177 WMAddNotificationObserver(activatedObserver, cPtr, _ColorWellActivatedNotification, NULL);
088c0ac2 dan1999-03-09 14:58:01 +0000178
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200179 cPtr->color = WMBlackColor(WMWidgetScreen(cPtr));
088c0ac2 dan1999-03-09 14:58:01 +0000180
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200181 WMAddNotificationObserver(colorChangedObserver, cPtr, WMColorPanelColorChangedNotification, NULL);
064f79eb dan2002-12-20 17:47:31 +0000182
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200183 WMSetViewDragSourceProcs(cPtr->colorView, &_DragSourceProcs);
184 WMSetViewDragDestinationProcs(cPtr->colorView, &_DragDestinationProcs);
bedc2238 kojima1999-05-16 07:00:01 +0000185
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200186 cPtr->xdndTypes = getXdndTypeArray();
187 WMRegisterViewForDraggedTypes(cPtr->colorView, cPtr->xdndTypes);
064f79eb dan2002-12-20 17:47:31 +0000188
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200189 return cPtr;
9d2e6ef9 scottc1998-09-29 22:36:29 +0000190}
191
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200192void WMSetColorWellColor(WMColorWell * cPtr, WMColor * color)
9d2e6ef9 scottc1998-09-29 22:36:29 +0000193{
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200194 if (cPtr->color)
195 WMReleaseColor(cPtr->color);
e7d0c5d9 dan2004-04-07 02:50:52 +0000196
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200197 cPtr->color = WMRetainColor(color);
e7d0c5d9 dan2004-04-07 02:50:52 +0000198
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200199 if (cPtr->colorView->flags.realized && cPtr->colorView->flags.mapped)
200 paintColorWell(cPtr);
9d2e6ef9 scottc1998-09-29 22:36:29 +0000201}
202
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200203WMColor *WMGetColorWellColor(WMColorWell * cPtr)
9d2e6ef9 scottc1998-09-29 22:36:29 +0000204{
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200205 return cPtr->color;
9d2e6ef9 scottc1998-09-29 22:36:29 +0000206}
207
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200208void WSetColorWellBordered(WMColorWell * cPtr, Bool flag)
0261c326 dan1999-01-06 15:22:33 +0000209{
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200210 flag = ((flag == 0) ? 0 : 1);
211 if (cPtr->flags.bordered != flag) {
212 cPtr->flags.bordered = flag;
213 W_ResizeView(cPtr->view, cPtr->view->size.width, cPtr->view->size.height);
214 }
0261c326 dan1999-01-06 15:22:33 +0000215}
216
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200217static void willResizeColorWell(W_ViewDelegate * self, WMView * view, unsigned int *width, unsigned int *height)
9d2e6ef9 scottc1998-09-29 22:36:29 +0000218{
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200219 WMColorWell *cPtr = (WMColorWell *) view->self;
220 int bw;
e7d0c5d9 dan2004-04-07 02:50:52 +0000221
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200222 if (cPtr->flags.bordered) {
9d2e6ef9 scottc1998-09-29 22:36:29 +0000223
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200224 if (*width < MIN_WIDTH)
225 *width = MIN_WIDTH;
226 if (*height < MIN_HEIGHT)
227 *height = MIN_HEIGHT;
0261c326 dan1999-01-06 15:22:33 +0000228
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200229 bw = (int)((float)WMIN(*width, *height) * 0.24);
5e4625da kojima1999-05-29 21:41:25 +0000230
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200231 W_ResizeView(cPtr->colorView, *width - 2 * bw, *height - 2 * bw);
0261c326 dan1999-01-06 15:22:33 +0000232
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200233 if (cPtr->colorView->pos.x != bw || cPtr->colorView->pos.y != bw)
234 W_MoveView(cPtr->colorView, bw, bw);
235 } else {
236 W_ResizeView(cPtr->colorView, *width, *height);
9d2e6ef9 scottc1998-09-29 22:36:29 +0000237
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200238 W_MoveView(cPtr->colorView, 0, 0);
239 }
9d2e6ef9 scottc1998-09-29 22:36:29 +0000240}
241
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200242static void paintColorWell(ColorWell * cPtr)
9d2e6ef9 scottc1998-09-29 22:36:29 +0000243{
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200244 W_Screen *scr = cPtr->view->screen;
9d2e6ef9 scottc1998-09-29 22:36:29 +0000245
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200246 W_DrawRelief(scr, cPtr->view->window, 0, 0, cPtr->view->size.width, cPtr->view->size.height, WRRaised);
e7d0c5d9 dan2004-04-07 02:50:52 +0000247
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200248 W_DrawRelief(scr, cPtr->colorView->window, 0, 0,
249 cPtr->colorView->size.width, cPtr->colorView->size.height, WRSunken);
9d2e6ef9 scottc1998-09-29 22:36:29 +0000250
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200251 if (cPtr->color)
252 WMPaintColorSwatch(cPtr->color, cPtr->colorView->window,
253 2, 2, cPtr->colorView->size.width - 4, cPtr->colorView->size.height - 4);
9d2e6ef9 scottc1998-09-29 22:36:29 +0000254}
255
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200256static void handleEvents(XEvent * event, void *data)
9d2e6ef9 scottc1998-09-29 22:36:29 +0000257{
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200258 ColorWell *cPtr = (ColorWell *) data;
9d2e6ef9 scottc1998-09-29 22:36:29 +0000259
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200260 CHECK_CLASS(data, WC_ColorWell);
9d2e6ef9 scottc1998-09-29 22:36:29 +0000261
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200262 switch (event->type) {
263 case Expose:
264 if (event->xexpose.count != 0)
265 break;
266 paintColorWell(cPtr);
267 break;
9d2e6ef9 scottc1998-09-29 22:36:29 +0000268
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200269 case DestroyNotify:
270 destroyColorWell(cPtr);
271 break;
e7d0c5d9 dan2004-04-07 02:50:52 +0000272
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200273 }
9d2e6ef9 scottc1998-09-29 22:36:29 +0000274}
275
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200276static WMArray *dropDataTypes(WMView * self)
e7d0c5d9 dan2004-04-07 02:50:52 +0000277{
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200278 return ((ColorWell *) self->self)->xdndTypes;
e7d0c5d9 dan2004-04-07 02:50:52 +0000279}
280
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200281static WMDragOperationType wantedDropOperation(WMView * self)
9d2e6ef9 scottc1998-09-29 22:36:29 +0000282{
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200283 return WDOperationCopy;
9d2e6ef9 scottc1998-09-29 22:36:29 +0000284}
285
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200286static Bool acceptDropOperation(WMView * self, WMDragOperationType operation)
e7d0c5d9 dan2004-04-07 02:50:52 +0000287{
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200288 return (operation == WDOperationCopy);
e7d0c5d9 dan2004-04-07 02:50:52 +0000289}
290
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200291static WMData *fetchDragData(WMView * self, char *type)
0261c326 dan1999-01-06 15:22:33 +0000292{
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200293 char *color = WMGetColorRGBDescription(((WMColorWell *) self->self)->color);
294 WMData *data;
e7d0c5d9 dan2004-04-07 02:50:52 +0000295
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200296 data = WMCreateDataWithBytes(color, strlen(color) + 1);
297 wfree(color);
e7d0c5d9 dan2004-04-07 02:50:52 +0000298
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200299 return data;
9e45e85d kojima2000-03-29 02:17:39 +0000300}
301
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200302static WMPixmap *makeDragPixmap(WMColorWell * cPtr)
5bd797b0 kojima2000-03-24 01:11:54 +0000303{
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200304 WMScreen *scr = cPtr->view->screen;
305 Pixmap pix;
e7d0c5d9 dan2004-04-07 02:50:52 +0000306
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200307 pix = XCreatePixmap(scr->display, W_DRAWABLE(scr), 16, 16, scr->depth);
e7d0c5d9 dan2004-04-07 02:50:52 +0000308
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200309 XFillRectangle(scr->display, pix, WMColorGC(cPtr->color), 0, 0, 15, 15);
e7d0c5d9 dan2004-04-07 02:50:52 +0000310
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200311 XDrawRectangle(scr->display, pix, WMColorGC(scr->black), 0, 0, 15, 15);
e7d0c5d9 dan2004-04-07 02:50:52 +0000312
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200313 return WMCreatePixmapFromXPixmaps(scr, pix, None, 16, 16, scr->depth);
0261c326 dan1999-01-06 15:22:33 +0000314}
315
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200316static void handleDragEvents(XEvent * event, void *data)
9d2e6ef9 scottc1998-09-29 22:36:29 +0000317{
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200318 WMColorWell *cPtr = (ColorWell *) data;
9d2e6ef9 scottc1998-09-29 22:36:29 +0000319
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200320 if (event->type == ButtonPress && event->xbutton.button == Button1) {
321 /* initialise drag icon */
322 WMSetViewDragImage(cPtr->colorView, makeDragPixmap(cPtr));
323 }
e7d0c5d9 dan2004-04-07 02:50:52 +0000324
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200325 WMDragImageFromView(cPtr->colorView, event);
9d2e6ef9 scottc1998-09-29 22:36:29 +0000326}
0261c326 dan1999-01-06 15:22:33 +0000327
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200328static void handleActionEvents(XEvent * event, void *data)
9d2e6ef9 scottc1998-09-29 22:36:29 +0000329{
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200330 WMColorWell *cPtr = (ColorWell *) data;
331 WMScreen *scr = WMWidgetScreen(cPtr);
332 WMColorPanel *cpanel;
e7d0c5d9 dan2004-04-07 02:50:52 +0000333
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200334 if (cPtr->flags.active)
335 W_SetViewBackgroundColor(cPtr->view, scr->gray);
336 else
337 W_SetViewBackgroundColor(cPtr->view, scr->white);
338 paintColorWell(cPtr);
088c0ac2 dan1999-03-09 14:58:01 +0000339
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200340 cPtr->flags.active ^= 1;
088c0ac2 dan1999-03-09 14:58:01 +0000341
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200342 if (cPtr->flags.active) {
343 WMPostNotificationName(_ColorWellActivatedNotification, cPtr, NULL);
344 }
345 cpanel = WMGetColorPanel(scr);
bedc2238 kojima1999-05-16 07:00:01 +0000346
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200347 WMSetColorPanelAction(cpanel, updateColorCallback, cPtr);
bedc2238 kojima1999-05-16 07:00:01 +0000348
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200349 if (cPtr->color)
350 WMSetColorPanelColor(cpanel, cPtr->color);
351 WMShowColorPanel(cpanel);
9d2e6ef9 scottc1998-09-29 22:36:29 +0000352}
353
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200354static void destroyColorWell(ColorWell * cPtr)
9d2e6ef9 scottc1998-09-29 22:36:29 +0000355{
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200356 WMRemoveNotificationObserver(cPtr);
088c0ac2 dan1999-03-09 14:58:01 +0000357
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200358 if (cPtr->color)
359 WMReleaseColor(cPtr->color);
e7d0c5d9 dan2004-04-07 02:50:52 +0000360
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200361 WMFreeArray(cPtr->xdndTypes);
064f79eb dan2002-12-20 17:47:31 +0000362
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200363 wfree(cPtr);
9d2e6ef9 scottc1998-09-29 22:36:29 +0000364}
365
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200366static Bool dropIsOk(WMDragOperationType request, WMArray * sourceDataTypes)
60a247f2 kojima2000-04-03 03:10:20 +0000367{
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200368 WMArrayIterator iter;
369 char *type;
370
371 if (request == WDOperationCopy) {
372 WM_ITERATE_ARRAY(sourceDataTypes, type, iter) {
373 if (type != NULL && strcmp(type, XDND_COLOR_DATA_TYPE) == 0) {
374 return True;
375 }
376 }
377 }
378
379 return False;
60a247f2 kojima2000-04-03 03:10:20 +0000380}
381
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200382static WMArray *requiredDataTypes(WMView * self, WMDragOperationType request, WMArray * sourceDataTypes)
60a247f2 kojima2000-04-03 03:10:20 +0000383{
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200384 if (dropIsOk(request, sourceDataTypes))
385 return ((ColorWell *) self->self)->xdndTypes;
386 else
387 return NULL;
60a247f2 kojima2000-04-03 03:10:20 +0000388}
389
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200390static WMDragOperationType allowedOperation(WMView * self, WMDragOperationType request, WMArray * sourceDataTypes)
60a247f2 kojima2000-04-03 03:10:20 +0000391{
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200392 if (dropIsOk(request, sourceDataTypes))
393 return WDOperationCopy;
394 else
395 return WDOperationNone;
60a247f2 kojima2000-04-03 03:10:20 +0000396}
397
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200398static void performDragOperation(WMView * self, WMArray * dropData, WMArray * operations, WMPoint * dropLocation)
60a247f2 kojima2000-04-03 03:10:20 +0000399{
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200400 char *colorName;
401 WMColor *color;
402 WMData *data;
403
404 /* only one operation requested (WDOperationCopy) implies only one data */
405 data = (WMData *) WMGetFromArray(dropData, 0);
406
407 if (data != NULL) {
408 colorName = (char *)WMDataBytes(data);
409 color = WMCreateNamedColor(W_VIEW_SCREEN(self), colorName, True);
410 WMSetColorWellColor(self->self, color);
411 WMReleaseColor(color);
412 }
60a247f2 kojima2000-04-03 03:10:20 +0000413}