8 char *WMColorWellDidChangeNotification
= "WMColorWellDidChangeNotification";
11 typedef struct W_ColorWell
{
25 unsigned int active
:1;
26 unsigned int bordered
:1;
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
= {
56 static unsigned draggingSourceOperation(WMView
*self
, Bool local
);
58 static WMData
* fetchDragData(WMView
*self
, char *type
);
60 static WMDragSourceProcs _DragSourceProcs
= {
61 draggingSourceOperation
,
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
,
74 static void concludeDragOperation(WMView
*self
, WMDraggingInfo
*info
);
76 static WMDragDestinationProcs _DragDestinationProcs
= {
80 prepareForDragOperation
,
86 #define DEFAULT_WIDTH 60
87 #define DEFAULT_HEIGHT 30
88 #define DEFAULT_BORDER_WIDTH 6
96 colorChangedObserver(void *data
, WMNotification
*notification
)
98 WMColorPanel
*panel
= (WMColorPanel
*)WMGetNotificationObject(notification
);
99 WMColorWell
*cPtr
= (WMColorWell
*)data
;
102 if (!cPtr
->flags
.active
)
105 color
= WMGetColorPanelColor(panel
);
107 WMSetColorWellColor(cPtr
, color
);
108 WMPostNotificationName(WMColorWellDidChangeNotification
, cPtr
, NULL
);
113 updateColorCallback(void *self
, void *data
)
115 WMColorPanel
*panel
= (WMColorPanel
*)self
;
116 WMColorWell
*cPtr
= (ColorWell
*)data
;
119 color
= WMGetColorPanelColor(panel
);
120 WMSetColorWellColor(cPtr
, color
);
121 WMPostNotificationName(WMColorWellDidChangeNotification
, cPtr
, NULL
);
127 activatedObserver(void *data
, WMNotification
*notification
)
130 WMColorWell *cPtr = (WMColorWell*)data;
132 if (!cPtr->flags.active || WMGetNotificationObject(notification) == cPtr)
135 W_SetViewBackgroundColor(cPtr->view, WMWidgetScreen(cPtr)->gray);
136 paintColorWell(cPtr);
138 cPtr->flags.active = 0;
145 WMCreateColorWell(WMWidget
*parent
)
149 cPtr
= wmalloc(sizeof(ColorWell
));
150 memset(cPtr
, 0, sizeof(ColorWell
));
152 cPtr
->widgetClass
= WC_ColorWell
;
154 cPtr
->view
= W_CreateView(W_VIEW(parent
));
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
);
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
,
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
);
210 WMSetColorWellColor(WMColorWell
*cPtr
, WMColor
*color
)
213 WMReleaseColor(cPtr
->color
);
215 cPtr
->color
= WMRetainColor(color
);
217 if (cPtr
->colorView
->flags
.realized
&& cPtr
->colorView
->flags
.mapped
)
218 paintColorWell(cPtr
);
223 WMGetColorWellColor(WMColorWell
*cPtr
)
230 WSetColorWellBordered(WMColorWell
*cPtr
, Bool flag
)
232 flag
= ((flag
==0) ? 0 : 1);
233 if (cPtr
->flags
.bordered
!= flag
) {
234 cPtr
->flags
.bordered
= flag
;
235 W_ResizeView(cPtr
->view
, cPtr
->view
->size
.width
, cPtr
->view
->size
.height
);
241 willResizeColorWell(W_ViewDelegate
*self
, WMView
*view
,
242 unsigned int *width
, unsigned int *height
)
244 WMColorWell
*cPtr
= (WMColorWell
*)view
->self
;
247 if (cPtr
->flags
.bordered
) {
249 if (*width
< MIN_WIDTH
)
251 if (*height
< MIN_HEIGHT
)
252 *height
= MIN_HEIGHT
;
254 bw
= (int)((float)WMIN(*width
, *height
)*0.24);
256 W_ResizeView(cPtr
->colorView
, *width
-2*bw
, *height
-2*bw
);
258 if (cPtr
->colorView
->pos
.x
!=bw
|| cPtr
->colorView
->pos
.y
!=bw
)
259 W_MoveView(cPtr
->colorView
, bw
, bw
);
261 W_ResizeView(cPtr
->colorView
, *width
, *height
);
263 W_MoveView(cPtr
->colorView
, 0, 0);
269 paintColorWell(ColorWell
*cPtr
)
271 W_Screen
*scr
= cPtr
->view
->screen
;
273 W_DrawRelief(scr
, cPtr
->view
->window
, 0, 0, cPtr
->view
->size
.width
,
274 cPtr
->view
->size
.height
, WRRaised
);
276 W_DrawRelief(scr
, cPtr
->colorView
->window
, 0, 0,
277 cPtr
->colorView
->size
.width
, cPtr
->colorView
->size
.height
,
281 WMPaintColorSwatch(cPtr
->color
, cPtr
->colorView
->window
,
282 2, 2, cPtr
->colorView
->size
.width
-4,
283 cPtr
->colorView
->size
.height
-4);
289 handleEvents(XEvent
*event
, void *data
)
291 ColorWell
*cPtr
= (ColorWell
*)data
;
293 CHECK_CLASS(data
, WC_ColorWell
);
296 switch (event
->type
) {
298 if (event
->xexpose
.count
!=0)
300 paintColorWell(cPtr
);
304 destroyColorWell(cPtr
);
312 draggingSourceOperation(WMView
*self
, Bool local
)
314 return WDOperationCopy
;
319 fetchDragData(WMView
*self
, char *type
)
321 char *color
= WMGetColorRGBDescription(((WMColorWell
*)self
->self
)->color
);
324 data
= WMCreateDataWithBytes(color
, strlen(color
)+1);
333 makeDragPixmap(WMColorWell
*cPtr
)
335 WMScreen
*scr
= cPtr
->view
->screen
;
338 pix
= XCreatePixmap(scr
->display
, W_DRAWABLE(scr
), 16, 16, scr
->depth
);
340 XFillRectangle(scr
->display
, pix
, WMColorGC(cPtr
->color
), 0, 0, 15, 15);
342 XDrawRectangle(scr
->display
, pix
, WMColorGC(scr
->black
), 0, 0, 15, 15);
344 return WMCreatePixmapFromXPixmaps(scr
, pix
, None
, 16, 16, scr
->depth
);
349 handleDragEvents(XEvent
*event
, void *data
)
351 WMColorWell
*cPtr
= (ColorWell
*)data
;
353 switch (event
->type
) {
355 if (event
->xbutton
.button
== Button1
) {
356 cPtr
->ipoint
.x
= event
->xbutton
.x
;
357 cPtr
->ipoint
.y
= event
->xbutton
.y
;
362 if (event
->xmotion
.state
& Button1Mask
) {
363 if (abs(cPtr
->ipoint
.x
- event
->xmotion
.x
) > 4
364 || abs(cPtr
->ipoint
.y
- event
->xmotion
.y
) > 4) {
367 char *types
[2] = {"application/X-color", NULL
};
371 pixmap
= makeDragPixmap(cPtr
);
373 WMDragImageFromView(cPtr
->view
, pixmap
, types
,
374 wmkpoint(event
->xmotion
.x_root
,
375 event
->xmotion
.y_root
),
379 WMReleasePixmap(pixmap
);
388 handleActionEvents(XEvent
*event
, void *data
)
390 WMColorWell
*cPtr
= (ColorWell
*)data
;
391 WMScreen
*scr
= WMWidgetScreen(cPtr
);
392 WMColorPanel
*cpanel
;
394 if (cPtr
->flags
.active
)
395 W_SetViewBackgroundColor(cPtr
->view
, scr
->gray
);
397 W_SetViewBackgroundColor(cPtr
->view
, scr
->white
);
398 paintColorWell(cPtr
);
400 cPtr
->flags
.active
^= 1;
402 if (cPtr
->flags
.active
) {
403 WMPostNotificationName(_ColorWellActivatedNotification
, cPtr
, NULL
);
405 cpanel
= WMGetColorPanel(scr
);
407 WMSetColorPanelAction(cpanel
, updateColorCallback
, cPtr
);
410 WMSetColorPanelColor(cpanel
, cPtr
->color
);
411 WMShowColorPanel(cpanel
);
416 destroyColorWell(ColorWell
*cPtr
)
418 WMRemoveNotificationObserver(cPtr
);
421 WMReleaseColor(cPtr
->color
);
429 draggingEntered(WMView
*self
, WMDraggingInfo
*info
)
431 return WDOperationCopy
;
436 draggingUpdated(WMView
*self
, WMDraggingInfo
*info
)
438 return WDOperationCopy
;
443 draggingExited(WMView
*self
, WMDraggingInfo
*info
)
450 prepareForDragOperation(WMView
*self
, WMDraggingInfo
*info
)
452 return "application/X-color";
457 performDragOperation(WMView
*self
, WMDraggingInfo
*info
, WMData
*data
)
459 char *colorName
= (char*)WMDataBytes(data
);
462 color
= WMCreateNamedColor(W_VIEW_SCREEN(self
), colorName
, True
);
464 WMSetColorWellColor(self
->self
, color
);
466 WMReleaseColor(color
);
473 concludeDragOperation(WMView
*self
, WMDraggingInfo
*info
)