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
= {
57 static WMDragSourceProcs dragProcs
= {
62 #define DEFAULT_WIDTH 60
63 #define DEFAULT_HEIGHT 30
64 #define DEFAULT_BORDER_WIDTH 6
72 colorChangedObserver(void *data
, WMNotification
*notification
)
74 WMColorPanel
*panel
= (WMColorPanel
*)WMGetNotificationObject(notification
);
75 WMColorWell
*cPtr
= (WMColorWell
*)data
;
78 if (!cPtr
->flags
.active
)
81 color
= WMGetColorPanelColor(panel
);
83 WMSetColorWellColor(cPtr
, color
);
84 WMPostNotificationName(WMColorWellDidChangeNotification
, cPtr
, NULL
);
89 updateColorCallback(void *self
, void *data
)
91 WMColorPanel
*panel
= (WMColorPanel
*)self
;
92 WMColorWell
*cPtr
= (ColorWell
*)data
;
95 color
= WMGetColorPanelColor(panel
);
96 WMSetColorWellColor(cPtr
, color
);
97 WMPostNotificationName(WMColorWellDidChangeNotification
, cPtr
, NULL
);
103 activatedObserver(void *data
, WMNotification
*notification
)
106 WMColorWell *cPtr = (WMColorWell*)data;
108 if (!cPtr->flags.active || WMGetNotificationObject(notification) == cPtr)
111 W_SetViewBackgroundColor(cPtr->view, WMWidgetScreen(cPtr)->gray);
112 paintColorWell(cPtr);
114 cPtr->flags.active = 0;
121 WMCreateColorWell(WMWidget
*parent
)
125 cPtr
= wmalloc(sizeof(ColorWell
));
126 memset(cPtr
, 0, sizeof(ColorWell
));
128 cPtr
->widgetClass
= WC_ColorWell
;
130 cPtr
->view
= W_CreateView(W_VIEW(parent
));
135 cPtr
->view
->self
= cPtr
;
137 cPtr
->view
->delegate
= &_ColorWellViewDelegate
;
139 cPtr
->colorView
= W_CreateView(cPtr
->view
);
140 if (!cPtr
->colorView
) {
141 W_DestroyView(cPtr
->view
);
145 cPtr
->colorView
->self
= cPtr
;
147 WMCreateEventHandler(cPtr
->view
, ExposureMask
|StructureNotifyMask
148 |ClientMessageMask
, handleEvents
, cPtr
);
150 WMCreateEventHandler(cPtr
->colorView
, ExposureMask
, handleEvents
, cPtr
);
152 WMCreateEventHandler(cPtr
->colorView
, ButtonPressMask
|ButtonMotionMask
153 |EnterWindowMask
, handleDragEvents
, cPtr
);
155 WMCreateEventHandler(cPtr
->view
, ButtonPressMask
, handleActionEvents
,
158 cPtr
->colorView
->flags
.mapWhenRealized
= 1;
160 cPtr
->flags
.bordered
= 1;
162 W_ResizeView(cPtr
->view
, DEFAULT_WIDTH
, DEFAULT_HEIGHT
);
164 WMAddNotificationObserver(activatedObserver
, cPtr
,
165 _ColorWellActivatedNotification
, NULL
);
167 cPtr
->color
= WMBlackColor(WMWidgetScreen(cPtr
));
169 WMAddNotificationObserver(colorChangedObserver
, cPtr
,
170 WMColorPanelColorChangedNotification
, NULL
);
177 WMSetColorWellColor(WMColorWell
*cPtr
, WMColor
*color
)
180 WMReleaseColor(cPtr
->color
);
182 cPtr
->color
= WMRetainColor(color
);
184 if (cPtr
->colorView
->flags
.realized
&& cPtr
->colorView
->flags
.mapped
)
185 paintColorWell(cPtr
);
190 WMGetColorWellColor(WMColorWell
*cPtr
)
197 WSetColorWellBordered(WMColorWell
*cPtr
, Bool flag
)
199 if (cPtr
->flags
.bordered
!= flag
) {
200 cPtr
->flags
.bordered
= flag
;
201 W_ResizeView(cPtr
->view
, cPtr
->view
->size
.width
, cPtr
->view
->size
.height
);
206 #define MIN(a,b) ((a) > (b) ? (b) : (a))
209 willResizeColorWell(W_ViewDelegate
*self
, WMView
*view
,
210 unsigned int *width
, unsigned int *height
)
212 WMColorWell
*cPtr
= (WMColorWell
*)view
->self
;
215 if (cPtr
->flags
.bordered
) {
217 if (*width
< MIN_WIDTH
)
219 if (*height
< MIN_HEIGHT
)
220 *height
= MIN_HEIGHT
;
222 bw
= (int)((float)MIN(*width
, *height
)*0.24);
224 W_ResizeView(cPtr
->colorView
, *width
-2*bw
, *height
-2*bw
);
226 if (cPtr
->colorView
->pos
.x
!=bw
|| cPtr
->colorView
->pos
.y
!=bw
)
227 W_MoveView(cPtr
->colorView
, bw
, bw
);
229 W_ResizeView(cPtr
->colorView
, *width
, *height
);
231 W_MoveView(cPtr
->colorView
, 0, 0);
237 paintColorWell(ColorWell
*cPtr
)
239 W_Screen
*scr
= cPtr
->view
->screen
;
241 W_DrawRelief(scr
, cPtr
->view
->window
, 0, 0, cPtr
->view
->size
.width
,
242 cPtr
->view
->size
.height
, WRRaised
);
244 W_DrawRelief(scr
, cPtr
->colorView
->window
, 0, 0,
245 cPtr
->colorView
->size
.width
, cPtr
->colorView
->size
.height
,
249 WMPaintColorSwatch(cPtr
->color
, cPtr
->colorView
->window
,
250 2, 2, cPtr
->colorView
->size
.width
-4,
251 cPtr
->colorView
->size
.height
-4);
257 handleEvents(XEvent
*event
, void *data
)
259 ColorWell
*cPtr
= (ColorWell
*)data
;
261 CHECK_CLASS(data
, WC_ColorWell
);
264 switch (event
->type
) {
266 if (event
->xexpose
.count
!=0)
268 paintColorWell(cPtr
);
272 destroyColorWell(cPtr
);
280 makeDragPixmap(WMColorWell
*cPtr
)
282 WMScreen
*scr
= cPtr
->view
->screen
;
285 pix
= XCreatePixmap(scr
->display
, W_DRAWABLE(scr
), 16, 16, scr
->depth
);
287 XFillRectangle(scr
->display
, pix
, WMColorGC(cPtr
->color
), 0, 0, 15, 15);
289 XDrawRectangle(scr
->display
, pix
, WMColorGC(scr
->black
), 0, 0, 15, 15);
291 return WMCreatePixmapFromXPixmaps(scr
, pix
, None
, 16, 16, scr
->depth
);
296 slideView(WMView
*view
, int srcX
, int srcY
, int dstX
, int dstY
)
309 dx
= (double)(dstX
-srcX
)/20.0;
310 dy
= (double)(dstY
-srcY
)/20.0;
312 for (i
= 0; i
< 20; i
++) {
313 W_MoveView(view
, x
, y
);
314 XFlush(view
->screen
->display
);
323 dragColor(ColorWell
*cPtr
, XEvent
*event
, WMPixmap
*image
)
326 WMScreen
*scr
= cPtr
->view
->screen
;
327 Display
*dpy
= scr
->display
;
328 XColor black
= {0, 0,0,0, DoRed
|DoGreen
|DoBlue
};
329 XColor green
= {0x0045b045, 0x4500,0xb000,0x4500, DoRed
|DoGreen
|DoBlue
};
330 XColor back
= {0, 0xffff,0xffff,0xffff, DoRed
|DoGreen
|DoBlue
};
332 WMColorWell
*activeWell
= NULL
;
334 dragView
= W_CreateTopView(scr
);
336 W_ResizeView(dragView
, 16, 16);
337 dragView
->attribFlags
|= CWOverrideRedirect
| CWSaveUnder
;
338 dragView
->attribs
.event_mask
= StructureNotifyMask
;
339 dragView
->attribs
.override_redirect
= True
;
340 dragView
->attribs
.save_under
= True
;
342 W_MoveView(dragView
, event
->xmotion
.x_root
-8, event
->xmotion
.y_root
-8);
344 W_RealizeView(dragView
);
348 XSetWindowBackgroundPixmap(dpy
, dragView
->window
, WMGetPixmapXID(image
));
349 XClearWindow(dpy
, dragView
->window
);
352 XGrabPointer(dpy
, dragView
->window
, True
,
353 ButtonMotionMask
|ButtonReleaseMask
|EnterWindowMask
,
354 GrabModeSync
, GrabModeAsync
,
355 scr
->rootWin
, scr
->defaultCursor
, CurrentTime
);
361 XAllowEvents(dpy
, SyncPointer
, CurrentTime
);
362 WMNextEvent(dpy
, &ev
);
366 if (activeWell
!= NULL
) {
367 WMSetColorWellColor(activeWell
, cPtr
->color
);
368 WMPostNotificationName(WMColorWellDidChangeNotification
,
371 slideView(dragView
, ev
.xbutton
.x_root
, ev
.xbutton
.y_root
,
372 event
->xmotion
.x_root
, event
->xmotion
.y_root
);
379 view
= W_GetViewForXWindow(dpy
, ev
.xcrossing
.window
);
381 if (view
&& view
->self
&& W_CLASS(view
->self
) == WC_ColorWell
382 && view
->self
!= activeWell
&& view
->self
!= cPtr
) {
384 activeWell
= view
->self
;
385 XRecolorCursor(dpy
, scr
->defaultCursor
, &green
, &back
);
386 } else if (view
->self
!=NULL
&& view
->self
!= activeWell
) {
387 XRecolorCursor(dpy
, scr
->defaultCursor
, &black
, &back
);
393 W_MoveView(dragView
, ev
.xmotion
.x_root
-8, ev
.xmotion
.y_root
-8);
401 XUngrabPointer(dpy
, CurrentTime
);
402 XRecolorCursor(dpy
, scr
->defaultCursor
, &black
, &back
);
404 W_DestroyView(dragView
);
409 handleDragEvents(XEvent
*event
, void *data
)
411 WMColorWell
*cPtr
= (ColorWell
*)data
;
413 switch (event
->type
) {
415 if (event
->xbutton
.button
== Button1
) {
416 cPtr
->ipoint
.x
= event
->xbutton
.x
;
417 cPtr
->ipoint
.y
= event
->xbutton
.y
;
422 if (event
->xmotion
.state
& Button1Mask
) {
423 if (abs(cPtr
->ipoint
.x
- event
->xmotion
.x
) > 4
424 || abs(cPtr
->ipoint
.y
- event
->xmotion
.y
) > 4) {
430 pixmap
= makeDragPixmap(cPtr
);
433 WMDragImageFromView(cPtr->view, pixmap, cPtr->view->pos,
437 dragColor(cPtr
, event
, pixmap
);
439 WMReleasePixmap(pixmap
);
448 handleActionEvents(XEvent
*event
, void *data
)
450 WMColorWell
*cPtr
= (ColorWell
*)data
;
451 WMScreen
*scr
= WMWidgetScreen(cPtr
);
452 WMColorPanel
*cpanel
;
454 if (cPtr
->flags
.active
)
455 W_SetViewBackgroundColor(cPtr
->view
, scr
->gray
);
457 W_SetViewBackgroundColor(cPtr
->view
, scr
->white
);
458 paintColorWell(cPtr
);
460 cPtr
->flags
.active
^= 1;
462 if (cPtr
->flags
.active
) {
463 WMPostNotificationName(_ColorWellActivatedNotification
, cPtr
, NULL
);
465 cpanel
= WMGetColorPanel(scr
);
467 WMSetColorPanelAction(cpanel
, updateColorCallback
, cPtr
);
470 WMSetColorPanelColor(cpanel
, cPtr
->color
);
471 WMShowColorPanel(cpanel
);
476 destroyColorWell(ColorWell
*cPtr
)
478 WMRemoveNotificationObserver(cPtr
);
481 WMReleaseColor(cPtr
->color
);