4 #define XDND_COLOR_DATA_TYPE "application/X-color"
6 const char *WMColorWellDidChangeNotification
= "WMColorWellDidChangeNotification";
8 typedef struct W_ColorWell
{
22 unsigned int active
:1;
23 unsigned int bordered
:1;
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
= {
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
= {
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
= {
81 #define DEFAULT_WIDTH 60
82 #define DEFAULT_HEIGHT 30
87 static void colorChangedObserver(void *data
, WMNotification
* notification
)
89 WMColorPanel
*panel
= (WMColorPanel
*) WMGetNotificationObject(notification
);
90 WMColorWell
*cPtr
= (WMColorWell
*) data
;
93 if (!cPtr
->flags
.active
)
96 color
= WMGetColorPanelColor(panel
);
98 WMSetColorWellColor(cPtr
, color
);
99 WMPostNotificationName(WMColorWellDidChangeNotification
, cPtr
, NULL
);
102 static void updateColorCallback(void *self
, void *data
)
104 WMColorPanel
*panel
= (WMColorPanel
*) self
;
105 WMColorWell
*cPtr
= (ColorWell
*) data
;
108 color
= WMGetColorPanelColor(panel
);
109 WMSetColorWellColor(cPtr
, color
);
110 WMPostNotificationName(WMColorWellDidChangeNotification
, cPtr
, NULL
);
113 static WMArray
*getXdndTypeArray(void)
115 WMArray
*types
= WMCreateArray(1);
116 WMAddToArray(types
, XDND_COLOR_DATA_TYPE
);
120 WMColorWell
*WMCreateColorWell(WMWidget
* parent
)
124 cPtr
= wmalloc(sizeof(ColorWell
));
126 cPtr
->widgetClass
= WC_ColorWell
;
128 cPtr
->view
= W_CreateView(W_VIEW(parent
));
133 cPtr
->view
->self
= cPtr
;
135 cPtr
->view
->delegate
= &_ColorWellViewDelegate
;
137 cPtr
->colorView
= W_CreateView(cPtr
->view
);
138 if (!cPtr
->colorView
) {
139 W_DestroyView(cPtr
->view
);
143 cPtr
->colorView
->self
= cPtr
;
145 WMCreateEventHandler(cPtr
->view
, ExposureMask
| StructureNotifyMask
146 | ClientMessageMask
, handleEvents
, cPtr
);
148 WMCreateEventHandler(cPtr
->colorView
, ExposureMask
, handleEvents
, cPtr
);
150 WMCreateDragHandler(cPtr
->colorView
, handleDragEvents
, cPtr
);
152 WMCreateEventHandler(cPtr
->view
, ButtonPressMask
, handleActionEvents
, cPtr
);
153 WMCreateEventHandler(cPtr
->colorView
, ButtonPressMask
, handleActionEvents
, cPtr
);
155 cPtr
->colorView
->flags
.mapWhenRealized
= 1;
157 cPtr
->flags
.bordered
= 1;
159 W_ResizeView(cPtr
->view
, DEFAULT_WIDTH
, DEFAULT_HEIGHT
);
161 cPtr
->color
= WMBlackColor(WMWidgetScreen(cPtr
));
163 WMAddNotificationObserver(colorChangedObserver
, cPtr
, WMColorPanelColorChangedNotification
, NULL
);
165 WMSetViewDragSourceProcs(cPtr
->colorView
, &_DragSourceProcs
);
166 WMSetViewDragDestinationProcs(cPtr
->colorView
, &_DragDestinationProcs
);
168 cPtr
->xdndTypes
= getXdndTypeArray();
169 WMRegisterViewForDraggedTypes(cPtr
->colorView
, cPtr
->xdndTypes
);
174 void WMSetColorWellColor(WMColorWell
* cPtr
, WMColor
* color
)
176 if (cPtr
->color
&& cPtr
->color
!= color
) {
177 WMReleaseColor(cPtr
->color
);
178 cPtr
->color
= WMRetainColor(color
);
181 if (cPtr
->colorView
->flags
.realized
&& cPtr
->colorView
->flags
.mapped
)
182 paintColorWell(cPtr
);
185 WMColor
*WMGetColorWellColor(WMColorWell
* cPtr
)
190 void WSetColorWellBordered(WMColorWell
* cPtr
, Bool flag
)
192 flag
= ((flag
== 0) ? 0 : 1);
193 if (cPtr
->flags
.bordered
!= flag
) {
194 cPtr
->flags
.bordered
= flag
;
195 W_ResizeView(cPtr
->view
, cPtr
->view
->size
.width
, cPtr
->view
->size
.height
);
199 static void willResizeColorWell(W_ViewDelegate
* self
, WMView
* view
, unsigned int *width
, unsigned int *height
)
201 WMColorWell
*cPtr
= (WMColorWell
*) view
->self
;
204 /* Parameter not used, but tell the compiler that it is ok */
207 if (cPtr
->flags
.bordered
) {
209 if (*width
< MIN_WIDTH
)
211 if (*height
< MIN_HEIGHT
)
212 *height
= MIN_HEIGHT
;
214 bw
= (int)((float)WMIN(*width
, *height
) * 0.24F
);
216 W_ResizeView(cPtr
->colorView
, *width
- 2 * bw
, *height
- 2 * bw
);
218 if (cPtr
->colorView
->pos
.x
!= bw
|| cPtr
->colorView
->pos
.y
!= bw
)
219 W_MoveView(cPtr
->colorView
, bw
, bw
);
221 W_ResizeView(cPtr
->colorView
, *width
, *height
);
223 W_MoveView(cPtr
->colorView
, 0, 0);
227 static void paintColorWell(ColorWell
* cPtr
)
229 W_Screen
*scr
= cPtr
->view
->screen
;
231 W_DrawRelief(scr
, cPtr
->view
->window
, 0, 0, cPtr
->view
->size
.width
, cPtr
->view
->size
.height
, WRRaised
);
233 W_DrawRelief(scr
, cPtr
->colorView
->window
, 0, 0,
234 cPtr
->colorView
->size
.width
, cPtr
->colorView
->size
.height
, WRSunken
);
237 WMPaintColorSwatch(cPtr
->color
, cPtr
->colorView
->window
,
238 2, 2, cPtr
->colorView
->size
.width
- 4, cPtr
->colorView
->size
.height
- 4);
241 static void handleEvents(XEvent
* event
, void *data
)
243 ColorWell
*cPtr
= (ColorWell
*) data
;
245 CHECK_CLASS(data
, WC_ColorWell
);
247 switch (event
->type
) {
249 if (event
->xexpose
.count
!= 0)
251 paintColorWell(cPtr
);
255 destroyColorWell(cPtr
);
261 static WMArray
*dropDataTypes(WMView
* self
)
263 return ((ColorWell
*) self
->self
)->xdndTypes
;
266 static WMDragOperationType
wantedDropOperation(WMView
* self
)
268 /* Parameter not used, but tell the compiler that it is ok */
271 return WDOperationCopy
;
274 static Bool
acceptDropOperation(WMView
* self
, WMDragOperationType operation
)
276 /* Parameter not used, but tell the compiler that it is ok */
279 return (operation
== WDOperationCopy
);
282 static WMData
*fetchDragData(WMView
* self
, char *type
)
284 char *color
= WMGetColorRGBDescription(((WMColorWell
*) self
->self
)->color
);
287 /* Parameter not used, but tell the compiler that it is ok */
290 data
= WMCreateDataWithBytes(color
, strlen(color
) + 1);
296 static WMPixmap
*makeDragPixmap(WMColorWell
* cPtr
)
298 WMScreen
*scr
= cPtr
->view
->screen
;
301 pix
= XCreatePixmap(scr
->display
, W_DRAWABLE(scr
), 16, 16, scr
->depth
);
303 XFillRectangle(scr
->display
, pix
, WMColorGC(cPtr
->color
), 0, 0, 15, 15);
305 XDrawRectangle(scr
->display
, pix
, WMColorGC(scr
->black
), 0, 0, 15, 15);
307 return WMCreatePixmapFromXPixmaps(scr
, pix
, None
, 16, 16, scr
->depth
);
310 static void handleDragEvents(XEvent
* event
, void *data
)
312 WMColorWell
*cPtr
= (ColorWell
*) data
;
314 if (event
->type
== ButtonPress
&& event
->xbutton
.button
== Button1
) {
315 /* initialise drag icon */
316 WMSetViewDragImage(cPtr
->colorView
, makeDragPixmap(cPtr
));
319 WMDragImageFromView(cPtr
->colorView
, event
);
322 static void handleActionEvents(XEvent
* event
, void *data
)
324 WMColorWell
*cPtr
= (ColorWell
*) data
;
325 WMScreen
*scr
= WMWidgetScreen(cPtr
);
326 WMColorPanel
*cpanel
;
328 /* Parameter not used, but tell the compiler that it is ok */
331 if (cPtr
->flags
.active
)
332 W_SetViewBackgroundColor(cPtr
->view
, scr
->gray
);
334 W_SetViewBackgroundColor(cPtr
->view
, scr
->white
);
335 paintColorWell(cPtr
);
337 cPtr
->flags
.active
^= 1;
339 if (cPtr
->flags
.active
) {
340 WMPostNotificationName(_ColorWellActivatedNotification
, cPtr
, NULL
);
342 cpanel
= WMGetColorPanel(scr
);
344 WMSetColorPanelAction(cpanel
, updateColorCallback
, cPtr
);
347 WMSetColorPanelColor(cpanel
, cPtr
->color
);
348 WMShowColorPanel(cpanel
);
351 static void destroyColorWell(ColorWell
* cPtr
)
353 WMRemoveNotificationObserver(cPtr
);
356 WMReleaseColor(cPtr
->color
);
358 WMFreeArray(cPtr
->xdndTypes
);
363 static Bool
dropIsOk(WMDragOperationType request
, WMArray
* sourceDataTypes
)
365 WMArrayIterator iter
;
368 if (request
== WDOperationCopy
) {
369 WM_ITERATE_ARRAY(sourceDataTypes
, type
, iter
) {
370 if (type
!= NULL
&& strcmp(type
, XDND_COLOR_DATA_TYPE
) == 0) {
379 static WMArray
*requiredDataTypes(WMView
* self
, WMDragOperationType request
, WMArray
* sourceDataTypes
)
381 if (dropIsOk(request
, sourceDataTypes
))
382 return ((ColorWell
*) self
->self
)->xdndTypes
;
387 static WMDragOperationType
allowedOperation(WMView
* self
, WMDragOperationType request
, WMArray
* sourceDataTypes
)
389 /* Parameter not used, but tell the compiler that it is ok */
392 if (dropIsOk(request
, sourceDataTypes
))
393 return WDOperationCopy
;
395 return WDOperationNone
;
398 static void performDragOperation(WMView
* self
, WMArray
* dropData
, WMArray
* operations
, WMPoint
* dropLocation
)
404 /* Parameter not used, but tell the compiler that it is ok */
408 /* only one operation requested (WDOperationCopy) implies only one data */
409 data
= (WMData
*) WMGetFromArray(dropData
, 0);
412 colorName
= (char *)WMDataBytes(data
);
413 color
= WMCreateNamedColor(W_VIEW_SCREEN(self
), colorName
, True
);
414 WMSetColorWellColor(self
->self
, color
);
415 WMReleaseColor(color
);