Initial revision
[wmaker-crm.git] / WINGs / wcolorwell.c
blob9ac2203bfab8fc52d549ee09bbe2ab9f720630c9
5 #include "WINGsP.h"
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;
25 } ColorWell;
28 static void destroyColorWell(ColorWell *cPtr);
29 static void paintColorWell(ColorWell *cPtr);
31 static void handleEvents(XEvent *event, void *data);
32 #if 0
33 static void handleDragEvents(XEvent *event, void *data);
34 #endif
35 static void handleActionEvents(XEvent *event, void *data);
37 static void resizeColorWell();
39 W_ViewProcedureTable _ColorWellViewProcedures = {
40 NULL,
41 resizeColorWell,
42 NULL
46 #if 0
47 static WMDragSourceProcs dragProcs = {
50 #endif
52 #define DEFAULT_WIDTH 60
53 #define DEFAULT_HEIGHT 30
54 #define DEFAULT_BORDER_WIDTH 6
56 #define MIN_WIDTH 16
57 #define MIN_HEIGHT 8
61 WMColorWell*
62 WMCreateColorWell(WMWidget *parent)
64 ColorWell *cPtr;
66 cPtr = wmalloc(sizeof(ColorWell));
67 memset(cPtr, 0, sizeof(ColorWell));
69 cPtr->widgetClass = WC_ColorWell;
71 cPtr->view = W_CreateView(W_VIEW(parent));
72 if (!cPtr->view) {
73 free(cPtr);
74 return NULL;
77 cPtr->colorView = W_CreateView(cPtr->view);
78 if (!cPtr->colorView) {
79 W_DestroyView(cPtr->view);
80 free(cPtr);
81 return NULL;
84 WMCreateEventHandler(cPtr->view, ExposureMask|StructureNotifyMask
85 |ClientMessageMask, handleEvents, cPtr);
87 WMCreateEventHandler(cPtr->colorView, ExposureMask, handleEvents, cPtr);
88 #if 0
89 WMCreateEventHandler(cPtr->colorView, ButtonPressMask|Button1MotionMask,
90 handleDragEvents, cPtr);
91 #endif
92 WMCreateEventHandler(cPtr->view, ButtonPressMask, handleActionEvents,
93 cPtr);
95 cPtr->colorView->flags.mapWhenRealized = 1;
97 resizeColorWell(cPtr, DEFAULT_WIDTH, DEFAULT_HEIGHT);
99 return cPtr;
103 void
104 WMSetColorWellColor(WMColorWell *cPtr, WMColor *color)
106 if (cPtr->color)
107 WMReleaseColor(cPtr->color);
109 cPtr->color = WMRetainColor(color);
111 if (cPtr->colorView->flags.realized && cPtr->colorView->flags.mapped)
112 paintColorWell(cPtr);
116 WMColor*
117 WMGetColorWellColor(WMColorWell *cPtr)
119 return cPtr->color;
122 #define MIN(a,b) ((a) > (b) ? (b) : (a))
124 static void
125 resizeColorWell(WMColorWell *cPtr, unsigned int width, unsigned int height)
127 int bw;
129 if (width < MIN_WIDTH)
130 width = MIN_WIDTH;
131 if (height < MIN_HEIGHT)
132 height = MIN_HEIGHT;
134 bw = (int)((float)MIN(width, height)*0.24);
136 W_ResizeView(cPtr->view, width, height);
138 W_ResizeView(cPtr->colorView, width-2*bw, height-2*bw);
140 if (cPtr->colorView->pos.x!=bw || cPtr->colorView->pos.y!=bw)
141 W_MoveView(cPtr->colorView, bw, bw);
145 static void
146 paintColorWell(ColorWell *cPtr)
148 W_Screen *scr = cPtr->view->screen;
150 W_DrawRelief(scr, cPtr->view->window, 0, 0, cPtr->view->size.width,
151 cPtr->view->size.height, WRRaised);
153 W_DrawRelief(scr, cPtr->colorView->window, 0, 0,
154 cPtr->colorView->size.width, cPtr->colorView->size.height,
155 WRSunken);
157 if (cPtr->color)
158 WMPaintColorSwatch(cPtr->color, cPtr->colorView->window,
159 2, 2, cPtr->colorView->size.width-4,
160 cPtr->colorView->size.height-4);
165 static void
166 handleEvents(XEvent *event, void *data)
168 ColorWell *cPtr = (ColorWell*)data;
170 CHECK_CLASS(data, WC_ColorWell);
173 switch (event->type) {
174 case Expose:
175 if (event->xexpose.count!=0)
176 break;
177 paintColorWell(cPtr);
178 break;
180 case DestroyNotify:
181 destroyColorWell(cPtr);
182 break;
187 #if 0
188 static WMPixmap*
189 makeDragPixmap(WMColorWell *cPtr)
191 WMScreen *scr = cPtr->view->screen;
192 Pixmap pix;
194 pix = XCreatePixmap(scr->display, W_DRAWABLE(scr), 16, 16, scr->depth);
196 XFillRectangle(scr->display, pix, WMColorGC(cPtr->color), 0, 0, 15, 15);
198 XDrawRectangle(scr->display, pix, WMColorGC(scr->black), 0, 0, 15, 15);
200 return WMCreatePixmapFromXPixmaps(scr, pix, None, 16, 16, scr->depth);
205 static void
206 handleDragEvents(XEvent *event, void *data)
208 WMColorWell *cPtr = (ColorWell*)data;
210 switch (event->type) {
211 case ButtonPress:
212 if (event->xbutton.button == Button1) {
213 cPtr->ipoint.x = event->xbutton.x;
214 cPtr->ipoint.y = event->xbutton.y;
216 break;
218 case MotionNotify:
219 if (event->xmotion.state & Button1Mask) {
220 if (abs(cPtr->ipoint.x - event->xmotion.x) > 4
221 || abs(cPtr->ipoint.y - event->xmotion.y) > 4) {
222 WMSize offs;
223 WMPixmap *pixmap;
225 offs.width = 2;
226 offs.height = 2;
227 pixmap = makeDragPixmap(cPtr);
229 WMDragImageFromView(cPtr->view, pixmap, cPtr->view->pos,
230 offs, event, True);
232 WMReleasePixmap(pixmap);
235 break;
238 #endif
241 static void
242 handleActionEvents(XEvent *event, void *data)
244 /* WMColorWell *cPtr = (ColorWell*)data;*/
249 static void
250 destroyColorWell(ColorWell *cPtr)
252 if (cPtr->color)
253 WMReleaseColor(cPtr->color);
255 free(cPtr);