1 /*
2  * Author:      William Chia-Wei Cheng (bill.cheng@acm.org)
3  *
4  * Copyright (C) 2001-2009, William Chia-Wei Cheng.
5  *
6  * This file may be distributed under the terms of the Q Public License
7  * as defined by Trolltech AS of Norway and appearing in the file
8  * LICENSE.QPL included in the packaging of this file.
9  *
10  * THIS FILE IS PROVIDED AS IS WITH NO WARRANTY OF ANY KIND, INCLUDING
11  * THE WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
12  * PURPOSE.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL,
13  * INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING
14  * FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
15  * NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
16  * WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17  *
18  * @(#)$Header: /mm2/home/cvs/bc-src/tgif/tgcwdl.c,v 1.21 2011/05/16 16:22:00 william Exp $
19  */
20 
21 #define _INCLUDE_FROM_TGCWDL_C_
22 
23 #include "tgifdefs.h"
24 #include "tidget.h"
25 
26 #include "auxtext.e"
27 #include "button.e"
28 #include "color.e"
29 #include "cursor.e"
30 #include "dialog.e"
31 #include "file.e"
32 #include "font.e"
33 #include "imgproc.e"
34 #include "mainloop.e"
35 #include "msg.e"
36 #include "raster.e"
37 #include "rect.e"
38 #include "setup.e"
39 #include "strtbl.e"
40 #include "tdgtbase.e"
41 #include "tdgtbmpl.e"
42 #include "tdgtbrow.e"
43 #include "tdgtbtn.e"
44 #include "tdgtdraw.e"
45 #include "tdgtsedt.e"
46 #include "tdgtlist.e"
47 #include "tdgtmsg.e"
48 #include "tidget.e"
49 #include "tgcwdl.e"
50 #include "util.e"
51 #include "xbitmap.e"
52 #include "xpixmap.e"
53 
54 ColorWheelInfo gColorWheelInfo;
55 
56 typedef struct tagColorWheelDlgInfo {
57    Window parent_win; /* should be the rootWindow */
58 
59    TdgtDraw *hs_pixmap_ctl; /* user pick HS values here */
60    TdgtDraw *v_pixmap_ctl; /* user pick V value here */
61    TdgtMsg *r_msg_ctl, *g_msg_ctl, *b_msg_ctl;
62    TdgtMsg *h_msg_ctl, *s_msg_ctl, *v_msg_ctl;
63    TdgtMsg *cname_msg_ctl;
64    TdgtSmplEdit *r_edit_ctl, *g_edit_ctl, *b_edit_ctl;
65    TdgtSmplEdit *h_edit_ctl, *s_edit_ctl, *v_edit_ctl;
66    TdgtSmplEdit *cname_edit_ctl;
67    TdgtDraw *csmpl_ctl; /* display selected color here */
68    TdgtBtnRow *btn_row_ctl;
69 
70    int screen_w, screen_h;
71    int base_win_w, base_win_h;
72 
73    int hs_x, hs_y, v_y;
74 
75    int r_msg_x, r_msg_y, r_msg_w, r_msg_h;
76    int g_msg_x, g_msg_y, g_msg_w, g_msg_h;
77    int b_msg_x, b_msg_y, b_msg_w, b_msg_h;
78    int r_ctl_x, r_ctl_y, r_ctl_w, r_ctl_h;
79    int g_ctl_x, g_ctl_y, g_ctl_w, g_ctl_h;
80    int b_ctl_x, b_ctl_y, b_ctl_w, b_ctl_h;
81 
82    int h_msg_x, h_msg_y, h_msg_w, h_msg_h;
83    int s_msg_x, s_msg_y, s_msg_w, s_msg_h;
84    int v_msg_x, v_msg_y, v_msg_w, v_msg_h;
85    int h_ctl_x, h_ctl_y, h_ctl_w, h_ctl_h;
86    int s_ctl_x, s_ctl_y, s_ctl_w, s_ctl_h;
87    int v_ctl_x, v_ctl_y, v_ctl_w, v_ctl_h;
88 
89    int btn_row_x, btn_row_y, btn_row_w, btn_row_h;
90 
91    char *title_str, *formatted_title_str;
92    int def_btn_id;
93 
94    int hs_pixmap_x, hs_pixmap_y, v_pixmap_x, v_pixmap_y;
95 
96    int red_msg_x, red_msg_y, red_edit_x, red_edit_y;
97    int green_msg_x, green_msg_y, green_edit_x, green_edit_y;
98    int blue_msg_x, blue_msg_y, blue_edit_x, blue_edit_y;
99    int hue_msg_x, hue_msg_y, hue_edit_x, hue_edit_y;
100    int sat_msg_x, sat_msg_y, sat_edit_x, sat_edit_y;
101    int val_msg_x, val_msg_y, val_edit_x, val_edit_y;
102 
103    int cname_msg_x, cname_msg_y, cname_edit_x, cname_edit_y;
104    int csmpl_msg_x, csmpl_msg_y, csmpl_edit_x, csmpl_edit_y;
105 
106    unsigned char r, g, b;
107    int h, s, v; /* 0 <= h,s,v <= 255 */
108    int pixel;
109    char color_str[40];
110 
111    TrueColorInfo tci;
112 } ColorWheelDlgInfo;
113 
114 /* --------------------- Utility Functions --------------------- */
115 
116 #define TDGTARG_COLORHWEEL_DPY_CLEAR 1 /* pv_cmd_userdata is NULL */
117 #define TDGTARG_COLORHWEEL_DPY_APPEND 2 /* pv_cmd_userdata is (ColorWheelDpyAppendInfo*) */
118 
119 static
ClearAllControls(pTdgtBase)120 void ClearAllControls(pTdgtBase)
121    TdgtBase *pTdgtBase;
122 {
123    ColorWheelDlgInfo *pcwdi=(ColorWheelDlgInfo*)(pTdgtBase->pti->userdata);
124 
125    TidgetSendCmd(pcwdi->hs_pixmap_ctl->pti, TDGTCMD_DRAW_USER,
126          TDGTARG_COLORHWEEL_DPY_CLEAR, pTdgtBase);
127    TidgetSendCmd(pcwdi->v_pixmap_ctl->pti, TDGTCMD_DRAW_USER,
128          TDGTARG_COLORHWEEL_DPY_CLEAR, pTdgtBase);
129    TidgetSendCmd(pcwdi->csmpl_ctl->pti, TDGTCMD_DRAW_USER,
130          TDGTARG_COLORHWEEL_DPY_CLEAR, pTdgtBase);
131 
132    TdgtSmplEditSetText(pcwdi->r_edit_ctl, "");
133    TdgtSmplEditSetText(pcwdi->g_edit_ctl, "");
134    TdgtSmplEditSetText(pcwdi->b_edit_ctl, "");
135    TdgtSmplEditSetText(pcwdi->h_edit_ctl, "");
136    TdgtSmplEditSetText(pcwdi->s_edit_ctl, "");
137    TdgtSmplEditSetText(pcwdi->v_edit_ctl, "");
138    TdgtSmplEditSetText(pcwdi->cname_edit_ctl, "");
139 }
140 
141 static
RGBtoPixel(pcwdi,r,g,b)142 int RGBtoPixel(pcwdi, r, g, b)
143    ColorWheelDlgInfo *pcwdi;
144    unsigned int r, g, b;
145 {
146    return (((r << pcwdi->tci.r_shift) & mainVisual->red_mask) |
147       ((g << pcwdi->tci.g_shift) & mainVisual->green_mask) |
148       ((b << pcwdi->tci.b_shift) & mainVisual->blue_mask));
149 }
150 
151 static
PixeltoRGB(pcwdi,pixel,pn_r,pn_g,pn_b)152 void PixeltoRGB(pcwdi, pixel, pn_r, pn_g, pn_b)
153    ColorWheelDlgInfo *pcwdi;
154    int pixel;
155    unsigned int *pn_r, *pn_g, *pn_b;
156    /* 0 <= r,g,b <= 255 */
157 {
158    uint32_t pix=(uint32_t)(unsigned int)pixel;
159    unsigned int r=0, g=0, b=0;
160    double dr=(double)0, dg=(double)0, db=(double)0;
161 
162    r = (pix & pcwdi->tci.r_mask) >> pcwdi->tci.r_shift;
163    g = (pix & pcwdi->tci.g_mask) >> pcwdi->tci.g_shift;
164    b = (pix & pcwdi->tci.b_mask) >> pcwdi->tci.b_shift;
165    dr = ((double)r) / pcwdi->tci.dr_maxval * ((double)255);
166    dg = ((double)g) / pcwdi->tci.dg_maxval * ((double)255);
167    db = ((double)b) / pcwdi->tci.db_maxval * ((double)255);
168    r = (dr < ((double)0)) ? 0 : ((unsigned int)dr);
169    g = (dg < ((double)0)) ? 0 : ((unsigned int)dg);
170    b = (db < ((double)0)) ? 0 : ((unsigned int)db);
171    if (r > 255) r = 255;
172    if (g > 255) g = 255;
173    if (b > 255) b = 255;
174    *pn_r = r;
175    *pn_g = g;
176    *pn_b = b;
177 }
178 
179 static
HStoXY(pcwdi,h,s,pn_x,pn_y)180 void HStoXY(pcwdi, h, s, pn_x, pn_y)
181    ColorWheelDlgInfo *pcwdi;
182    int h, s, *pn_x, *pn_y;
183 {
184    double dval=((double)h)/((double)255)*((double)(HS_WIDTH-1));
185    int x=0, y=0;
186 
187    x = round(dval);
188    if (x < 0) x = 0;
189    if (x > (HS_WIDTH-1)) x = HS_WIDTH-1;
190 
191    dval = ((double)s)/((double)255)*((double)(HS_HEIGHT-1));
192    y = round(dval);
193    if (y < 0) y = 0;
194    if (y > (HS_HEIGHT-1)) y = HS_HEIGHT-1;
195 
196    *pn_x = x;
197    *pn_y = HS_HEIGHT-y;
198 }
199 
200 static
XYtoHS(pcwdi,x,y,pn_h,pn_s)201 void XYtoHS(pcwdi, x, y, pn_h, pn_s)
202    ColorWheelDlgInfo *pcwdi;
203    int x, y, *pn_h, *pn_s;
204 {
205    double dval=((double)x)/((double)(HS_WIDTH-1))*((double)(255));
206    int h=0, s=0;
207 
208    h = round(dval);
209    if (h < 0) h = 0;
210    if (h > 255) h = 255;
211 
212    dval = ((double)(HS_HEIGHT-y))/((double)(HS_HEIGHT-1))*((double)255);
213    s = round(dval);
214    if (s < 0) s = 0;
215    if (s > 255) s = 255;
216 
217    *pn_h = h;
218    *pn_s = s;
219 }
220 
221 static
VtoY(pcwdi,v,pn_y)222 void VtoY(pcwdi, v, pn_y)
223    ColorWheelDlgInfo *pcwdi;
224    int v, *pn_y;
225 {
226    double dval=((double)v)/((double)255)*((double)(HS_HEIGHT-1));
227    int y=round(dval);
228 
229    if (y < 0) y = 0;
230    if (y > (HS_HEIGHT-1)) y = HS_HEIGHT-1;
231 
232    *pn_y = HS_HEIGHT-y;
233 }
234 
235 static
YtoV(pcwdi,y,pn_v)236 void YtoV(pcwdi, y, pn_v)
237    ColorWheelDlgInfo *pcwdi;
238    int y, *pn_v;
239 {
240    double dval=((double)(HS_HEIGHT-y))/((double)(HS_HEIGHT-1))*((double)255);
241    int v=0;
242 
243    v = round(dval);
244    if (v < 0) v = 0;
245    if (v > 255) v = 255;
246 
247    *pn_v = v;
248 }
249 
250 static
SetupVPixmap(pcwdi)251 int SetupVPixmap(pcwdi)
252    ColorWheelDlgInfo *pcwdi;
253 {
254    int row=0;
255    Pixmap pixmap=gColorWheelInfo.v_pixmap;
256    double dh=(((double)pcwdi->h)/((double)255)*((double)360));
257    double ds=(((double)pcwdi->s)/((double)255)*((double)0x0ffff));
258    int hue=round(dh);
259 
260    if (hue < 0) hue = 0;
261    if (hue > 360) hue = 360;
262 
263    if (pixmap == None) return FALSE;
264 
265    for (row=0; row < V_HEIGHT; row++) {
266       int r=0, g=0, b=0;
267       double dr=(double)0, dg=(double)0, db=(double)0;
268       uint32_t pixel=0;
269       double val=(((double)(V_HEIGHT-1-row))/((double)(V_HEIGHT-1)));
270       XPoint vs[2];
271 
272       val *= ((double)0x0ffff);
273 
274       HSVtoRGB(hue, ds, val, &r, &g, &b);
275       dr = ((double)r) / ((double)0x0ffff) * pcwdi->tci.dr_maxval;
276       dg = ((double)g) / ((double)0x0ffff) * pcwdi->tci.dr_maxval;
277       db = ((double)b) / ((double)0x0ffff) * pcwdi->tci.dr_maxval;
278       r = round(dr);
279       g = round(dg);
280       b = round(db);
281       if (r < 0) r = 0;
282       if (g < 0) g = 0;
283       if (b < 0) b = 0;
284       if (r > 255) r = 255;
285       if (g > 255) g = 255;
286       if (b > 255) b = 255;
287       pixel = RGBtoPixel(pcwdi, r, g, b);
288       XSetForeground(mainDisplay, gTidgetManager.gc, pixel);
289 
290       vs[0].x = 0;
291       vs[0].y = row;
292       vs[1].x = V_WIDTH-1;
293       vs[1].y = row;
294       XDrawLines(mainDisplay, pixmap, gTidgetManager.gc, vs, 2,
295             CoordModeOrigin);
296    }
297    TidgetManagerResetGC();
298 
299    return TRUE;
300 }
301 
302 static
SetHSVFromRGB(pcwdi)303 void SetHSVFromRGB(pcwdi)
304    ColorWheelDlgInfo *pcwdi;
305 {
306    unsigned int r=pcwdi->r, g=pcwdi->g, b=pcwdi->b;
307    int h=0, s=0, v=0;
308    double dr=(double)0, dg=(double)0, db=(double)0;
309    double dh=(double)0, ds=(double)0, dv=(double)0;
310 
311    dr = ((double)r) / ((double)255) * ((double)0x0ffff);
312    dg = ((double)g) / ((double)255) * ((double)0x0ffff);
313    db = ((double)b) / ((double)255) * ((double)0x0ffff);
314    r = round(dr);
315    g = round(dg);
316    b = round(db);
317    RGBtoHSV(r, g, b, &h, &ds, &v);
318    dh = ((double)h) / ((double)360) * ((double)255);
319    ds = ds / ((double)0x0ffff) * ((double)255);
320    dv = ((double)v) / ((double)0x0ffff) * ((double)255);
321    h = round(dh);
322    s = round(ds);
323    v = round(dv);
324    if (h < 0) h = 0;
325    if (s < 0) s = 0;
326    if (v < 0) v = 0;
327    if (h > 255) h = 255;
328    if (s > 255) s = 255;
329    if (v > 255) v = 255;
330    pcwdi->h = h;
331    pcwdi->s = s;
332    pcwdi->v = v;
333 }
334 
335 static
SetRGBFromHSV(pcwdi)336 void SetRGBFromHSV(pcwdi)
337    ColorWheelDlgInfo *pcwdi;
338 {
339    double dh=(((double)pcwdi->h)/((double)255)*((double)360));
340    double ds=(((double)pcwdi->s)/((double)255)*((double)0x0ffff));
341    double dv=(((double)pcwdi->v)/((double)255)*((double)0x0ffff));
342    double dr=(double)0, dg=(double)0, db=(double)0;
343    int hue=round(dh), val=round(dv), r=0, g=0, b=0;
344 
345    if (hue < 0) hue = 0;
346    if (hue > 360) hue = 360;
347 
348    if (val < 0) val = 0;
349    if (val > 0x0ffff) val = 0x0ffff;
350 
351    HSVtoRGB(hue, ds, val, &r, &g, &b);
352    dr = ((double)r) / ((double)0x0ffff) * pcwdi->tci.dr_maxval;
353    dg = ((double)g) / ((double)0x0ffff) * pcwdi->tci.dr_maxval;
354    db = ((double)b) / ((double)0x0ffff) * pcwdi->tci.dr_maxval;
355    r = round(dr);
356    g = round(dg);
357    b = round(db);
358    if (r < 0) r = 0;
359    if (g < 0) g = 0;
360    if (b < 0) b = 0;
361    if (r > 255) r = 255;
362    if (g > 255) g = 255;
363    if (b > 255) b = 255;
364    pcwdi->r = r;
365    pcwdi->g = g;
366    pcwdi->b = b;
367 }
368 
369 static
SetColorNameFromRGB(pcwdi)370 void SetColorNameFromRGB(pcwdi)
371    ColorWheelDlgInfo *pcwdi;
372 {
373    sprintf(pcwdi->color_str, "%02x%02x%02x", pcwdi->r, pcwdi->g, pcwdi->b);
374    UtilStrUpper(pcwdi->color_str);
375 }
376 
377 static
SetPixelFromRGB(pcwdi)378 void SetPixelFromRGB(pcwdi)
379    ColorWheelDlgInfo *pcwdi;
380 {
381    pcwdi->pixel = RGBtoPixel(pcwdi, pcwdi->r, pcwdi->g, pcwdi->b);
382 }
383 
384 static
SetupColorsFromCurrentColor(pcwdi)385 int SetupColorsFromCurrentColor(pcwdi)
386    ColorWheelDlgInfo *pcwdi;
387 {
388    PixeltoRGB(pcwdi, colorPixels[colorIndex], &pcwdi->r, &pcwdi->g, &pcwdi->b);
389    SetPixelFromRGB(pcwdi);
390    SetHSVFromRGB(pcwdi);
391    HStoXY(pcwdi, pcwdi->h, pcwdi->s, &pcwdi->hs_x, &pcwdi->hs_y);
392    VtoY(pcwdi, pcwdi->v, &pcwdi->v_y);
393    SetupVPixmap(pcwdi);
394    SetColorNameFromRGB(pcwdi);
395 
396    return TRUE;
397 }
398 
399 static
AllHexChars(buf)400 int AllHexChars(buf)
401    char *buf;
402 {
403    int i=0, len=strlen(buf);
404 
405    for (i=0; i < len; i++) {
406       char ch=buf[i];
407 
408       if ((ch >= '0' && ch <= '9') || (ch >= 'a' && ch <= 'f') ||
409             (ch >= 'A' && ch <= 'F')) {
410          /* keep going */
411       } else {
412          return FALSE;
413       }
414    }
415    return TRUE;
416 }
417 
418 static
SetColorInAppl(pTdgtBase)419 int SetColorInAppl(pTdgtBase)
420    TdgtBase *pTdgtBase;
421 {
422    ColorWheelDlgInfo *pcwdi=(ColorWheelDlgInfo*)(pTdgtBase->pti->userdata);
423    char *c_ptr=TdgtSmplEditGetText(pcwdi->cname_edit_ctl), color_str[40];
424    int new_alloc=FALSE, index=0;
425 
426    if (strlen(c_ptr) == 6 && AllHexChars(c_ptr)) {
427       snprintf(color_str, sizeof(color_str), "#%s", c_ptr);
428    } else {
429       UtilStrCpyN(color_str, sizeof(color_str), c_ptr);
430    }
431    index = QuickFindColorIndex(NULL, color_str, &new_alloc, FALSE);
432    if (index == INVALID) {
433       sprintf(gszMsgBox, TgLoadString(STID_FAIL_ALLOC_NAMED_COLOR), c_ptr);
434       MsgBox(gszMsgBox, TOOL_NAME, INFO_MB);
435       return FALSE;
436    } else {
437       ChangeAllSelColor(index, TRUE);
438    }
439    return TRUE;
440 }
441 
442 /* --------------------- Focus Stuff --------------------- */
443 
444 static
ColorWheelDlgRemoveFocus(pTdgtBase)445 void ColorWheelDlgRemoveFocus(pTdgtBase)
446    TdgtBase *pTdgtBase;
447 {
448    ColorWheelDlgInfo *pcwdi=(ColorWheelDlgInfo*)(pTdgtBase->pti->userdata);
449    Window win=TidgetGetFocusWindow();
450 
451    if (win == None) return;
452    if (win == pcwdi->hs_pixmap_ctl->pti->tci.win) {
453       TidgetDrawFocusRect(pcwdi->hs_pixmap_ctl->pti,
454             &pcwdi->hs_pixmap_ctl->client_area);
455    } else if (win == pcwdi->v_pixmap_ctl->pti->tci.win) {
456       TidgetDrawFocusRect(pcwdi->v_pixmap_ctl->pti,
457             &pcwdi->v_pixmap_ctl->client_area);
458    } else if (win == pcwdi->r_edit_ctl->pti->tci.win) {
459       TidgetDrawFocusRect(pcwdi->r_edit_ctl->pti,
460             &pcwdi->r_edit_ctl->client_area);
461       TdgtSmplEditSetFocus(pcwdi->r_edit_ctl, FALSE);
462    } else if (win == pcwdi->g_edit_ctl->pti->tci.win) {
463       TidgetDrawFocusRect(pcwdi->g_edit_ctl->pti,
464             &pcwdi->g_edit_ctl->client_area);
465       TdgtSmplEditSetFocus(pcwdi->g_edit_ctl, FALSE);
466    } else if (win == pcwdi->b_edit_ctl->pti->tci.win) {
467       TidgetDrawFocusRect(pcwdi->b_edit_ctl->pti,
468             &pcwdi->b_edit_ctl->client_area);
469       TdgtSmplEditSetFocus(pcwdi->b_edit_ctl, FALSE);
470    } else if (win == pcwdi->h_edit_ctl->pti->tci.win) {
471       TidgetDrawFocusRect(pcwdi->h_edit_ctl->pti,
472             &pcwdi->h_edit_ctl->client_area);
473       TdgtSmplEditSetFocus(pcwdi->h_edit_ctl, FALSE);
474    } else if (win == pcwdi->s_edit_ctl->pti->tci.win) {
475       TidgetDrawFocusRect(pcwdi->s_edit_ctl->pti,
476             &pcwdi->s_edit_ctl->client_area);
477       TdgtSmplEditSetFocus(pcwdi->s_edit_ctl, FALSE);
478    } else if (win == pcwdi->v_edit_ctl->pti->tci.win) {
479       TidgetDrawFocusRect(pcwdi->v_edit_ctl->pti,
480             &pcwdi->v_edit_ctl->client_area);
481       TdgtSmplEditSetFocus(pcwdi->v_edit_ctl, FALSE);
482    } else if (win == pcwdi->cname_edit_ctl->pti->tci.win) {
483       TidgetDrawFocusRect(pcwdi->cname_edit_ctl->pti,
484             &pcwdi->cname_edit_ctl->client_area);
485       TdgtSmplEditSetFocus(pcwdi->cname_edit_ctl, FALSE);
486    }
487 }
488 
489 /* --------------------- HSRedrawCallback --------------------- */
490 
491 static
SetRGBControlValues(pTdgtBase)492 void SetRGBControlValues(pTdgtBase)
493    TdgtBase *pTdgtBase;
494 {
495    ColorWheelDlgInfo *pcwdi=(ColorWheelDlgInfo*)(pTdgtBase->pti->userdata);
496    char caption[MAXSTRING];
497 
498    sprintf(caption, "%1d", pcwdi->r);
499    TdgtSmplEditSetText(pcwdi->r_edit_ctl, caption);
500    sprintf(caption, "%1d", pcwdi->g);
501    TdgtSmplEditSetText(pcwdi->g_edit_ctl, caption);
502    sprintf(caption, "%1d", pcwdi->b);
503    TdgtSmplEditSetText(pcwdi->b_edit_ctl, caption);
504 }
505 
506 static
SetHSVControlValues(pTdgtBase)507 void SetHSVControlValues(pTdgtBase)
508    TdgtBase *pTdgtBase;
509 {
510    ColorWheelDlgInfo *pcwdi=(ColorWheelDlgInfo*)(pTdgtBase->pti->userdata);
511    char caption[MAXSTRING];
512 
513    sprintf(caption, "%1d", pcwdi->h);
514    TdgtSmplEditSetText(pcwdi->h_edit_ctl, caption);
515    sprintf(caption, "%1d", pcwdi->s);
516    TdgtSmplEditSetText(pcwdi->s_edit_ctl, caption);
517    sprintf(caption, "%1d", pcwdi->v);
518    TdgtSmplEditSetText(pcwdi->v_edit_ctl, caption);
519 }
520 
521 static
SetColorNameControlValue(pTdgtBase)522 void SetColorNameControlValue(pTdgtBase)
523    TdgtBase *pTdgtBase;
524 {
525    ColorWheelDlgInfo *pcwdi=(ColorWheelDlgInfo*)(pTdgtBase->pti->userdata);
526 
527    TdgtSmplEditSetText(pcwdi->cname_edit_ctl, pcwdi->color_str);
528 }
529 
530 static
SetupControls(pTdgtBase)531 void SetupControls(pTdgtBase)
532    TdgtBase *pTdgtBase;
533 {
534    SetRGBControlValues(pTdgtBase);
535    SetHSVControlValues(pTdgtBase);
536    SetColorNameControlValue(pTdgtBase);
537 }
538 
539 static
RedrawHSPixmapControlValues(pTdgtBase)540 void RedrawHSPixmapControlValues(pTdgtBase)
541    TdgtBase *pTdgtBase;
542 {
543    ColorWheelDlgInfo *pcwdi=(ColorWheelDlgInfo*)(pTdgtBase->pti->userdata);
544 
545    RedrawTidget(pcwdi->hs_pixmap_ctl->pti);
546 }
547 
548 static
RedrawVPixmapControlValues(pTdgtBase)549 void RedrawVPixmapControlValues(pTdgtBase)
550    TdgtBase *pTdgtBase;
551 {
552    ColorWheelDlgInfo *pcwdi=(ColorWheelDlgInfo*)(pTdgtBase->pti->userdata);
553 
554    RedrawTidget(pcwdi->v_pixmap_ctl->pti);
555 }
556 
557 static
RedrawRGBControlValues(pTdgtBase)558 void RedrawRGBControlValues(pTdgtBase)
559    TdgtBase *pTdgtBase;
560 {
561    ColorWheelDlgInfo *pcwdi=(ColorWheelDlgInfo*)(pTdgtBase->pti->userdata);
562 
563    RedrawTidget(pcwdi->r_edit_ctl->pti);
564    RedrawTidget(pcwdi->g_edit_ctl->pti);
565    RedrawTidget(pcwdi->b_edit_ctl->pti);
566 }
567 
568 static
RedrawHSVControlValues(pTdgtBase)569 void RedrawHSVControlValues(pTdgtBase)
570    TdgtBase *pTdgtBase;
571 {
572    ColorWheelDlgInfo *pcwdi=(ColorWheelDlgInfo*)(pTdgtBase->pti->userdata);
573 
574    RedrawTidget(pcwdi->h_edit_ctl->pti);
575    RedrawTidget(pcwdi->s_edit_ctl->pti);
576    RedrawTidget(pcwdi->v_edit_ctl->pti);
577 }
578 
579 static
RedrawColorNameControlValue(pTdgtBase)580 void RedrawColorNameControlValue(pTdgtBase)
581    TdgtBase *pTdgtBase;
582 {
583    ColorWheelDlgInfo *pcwdi=(ColorWheelDlgInfo*)(pTdgtBase->pti->userdata);
584 
585    RedrawTidget(pcwdi->cname_edit_ctl->pti);
586 }
587 
588 static
RedrawColorSampleControlValue(pTdgtBase)589 void RedrawColorSampleControlValue(pTdgtBase)
590    TdgtBase *pTdgtBase;
591 {
592    ColorWheelDlgInfo *pcwdi=(ColorWheelDlgInfo*)(pTdgtBase->pti->userdata);
593 
594    RedrawTidget(pcwdi->csmpl_ctl->pti);
595 }
596 
597 #define SKIP_NONE      0x0
598 #define SKIP_HS_PIXMAP 0x1
599 #define SKIP_V_PIXMAP  0x2
600 
601 static
RedrawControls(pTdgtBase,skip)602 void RedrawControls(pTdgtBase, skip)
603    TdgtBase *pTdgtBase;
604    int skip;
605 {
606    if ((skip & SKIP_HS_PIXMAP) != SKIP_HS_PIXMAP) {
607       RedrawHSPixmapControlValues(pTdgtBase);
608    }
609    if ((skip & SKIP_V_PIXMAP) != SKIP_V_PIXMAP) {
610       RedrawVPixmapControlValues(pTdgtBase);
611    }
612    RedrawRGBControlValues(pTdgtBase);
613    RedrawHSVControlValues(pTdgtBase);
614    RedrawColorNameControlValue(pTdgtBase);
615    RedrawColorSampleControlValue(pTdgtBase);
616 }
617 
618 static
HSDrawSeg(dpy,win,gc,pTdgtDraw,vs)619 void HSDrawSeg(dpy, win, gc, pTdgtDraw, vs)
620    Display *dpy;
621    Window win;
622    GC gc;
623    TdgtDraw *pTdgtDraw;
624    XPoint *vs;
625 {
626    if (vs[0].x < 0) vs[0].x = 0;
627    if (vs[0].x >= HS_WIDTH) vs[0].x = HS_WIDTH-1;
628    if (vs[0].y < 0) vs[0].y = 0;
629    if (vs[0].y >= HS_HEIGHT) vs[0].y = HS_HEIGHT-1;
630 
631    if (vs[1].x < 1) vs[1].x = 1;
632    if (vs[1].x >= HS_WIDTH) vs[1].x = HS_WIDTH-1;
633    if (vs[1].y < 1) vs[1].y = 1;
634    if (vs[1].y >= HS_HEIGHT) vs[1].y = HS_HEIGHT-1;
635 
636    vs[0].x += pTdgtDraw->client_area.x;
637    vs[0].y += pTdgtDraw->client_area.y;
638    vs[1].x += pTdgtDraw->client_area.x;
639    vs[1].y += pTdgtDraw->client_area.y;
640 
641    XDrawLines(dpy, win, gc, vs, 2, CoordModeOrigin);
642 }
643 
644 static
HSRedrawCrosshair(pcwdi,pTdgtDraw)645 void HSRedrawCrosshair(pcwdi, pTdgtDraw)
646    ColorWheelDlgInfo *pcwdi;
647    TdgtDraw *pTdgtDraw;
648 {
649    int seg_len=6;
650    XPoint vs[2];
651    XGCValues values;
652 
653    values.line_width = 3;
654    XChangeGC(mainDisplay, revDefaultGC, GCLineWidth, &values);
655 
656    vs[0].x = pcwdi->hs_x;
657    vs[0].y = pcwdi->hs_y - 2;
658    vs[1].x = pcwdi->hs_x;
659    vs[1].y = pcwdi->hs_y - 2 - seg_len;
660    HSDrawSeg(mainDisplay, pTdgtDraw->pti->tci.win, revDefaultGC, pTdgtDraw, vs);
661 
662    vs[0].x = pcwdi->hs_x;
663    vs[0].y = pcwdi->hs_y + 2;
664    vs[1].x = pcwdi->hs_x;
665    vs[1].y = pcwdi->hs_y + 2 + seg_len;
666    HSDrawSeg(mainDisplay, pTdgtDraw->pti->tci.win, revDefaultGC, pTdgtDraw, vs);
667 
668    vs[0].x = pcwdi->hs_x - 2;
669    vs[0].y = pcwdi->hs_y;
670    vs[1].x = pcwdi->hs_x - 2 - seg_len;
671    vs[1].y = pcwdi->hs_y;
672    HSDrawSeg(mainDisplay, pTdgtDraw->pti->tci.win, revDefaultGC, pTdgtDraw, vs);
673 
674    vs[0].x = pcwdi->hs_x + 2;
675    vs[0].y = pcwdi->hs_y;
676    vs[1].x = pcwdi->hs_x + 2 + seg_len;
677    vs[1].y = pcwdi->hs_y;
678    HSDrawSeg(mainDisplay, pTdgtDraw->pti->tci.win, revDefaultGC, pTdgtDraw, vs);
679 
680    values.line_width = 1;
681    XChangeGC(mainDisplay, revDefaultGC, GCLineWidth, &values);
682 }
683 
684 static
VDrawSeg(dpy,win,gc,pTdgtDraw,vs)685 void VDrawSeg(dpy, win, gc, pTdgtDraw, vs)
686    Display *dpy;
687    Window win;
688    GC gc;
689    TdgtDraw *pTdgtDraw;
690    XPoint *vs;
691 {
692    if (vs[0].x < 0) vs[0].x = 0;
693    if (vs[0].x >= V_WIDTH) vs[0].x = V_WIDTH-1;
694    if (vs[0].y < 0) vs[0].y = 0;
695    if (vs[0].y >= V_HEIGHT) vs[0].y = V_HEIGHT-1;
696 
697    if (vs[1].x < 1) vs[1].x = 1;
698    if (vs[1].x >= V_WIDTH) vs[1].x = V_WIDTH-1;
699    if (vs[1].y < 1) vs[1].y = 1;
700    if (vs[1].y >= V_HEIGHT) vs[1].y = V_HEIGHT-1;
701 
702    vs[0].x += pTdgtDraw->client_area.x;
703    vs[0].y += pTdgtDraw->client_area.y;
704    vs[1].x += pTdgtDraw->client_area.x;
705    vs[1].y += pTdgtDraw->client_area.y;
706 
707    XDrawLines(dpy, win, gc, vs, 2, CoordModeOrigin);
708 }
709 
710 static
VRedrawLine(pcwdi,pTdgtDraw)711 void VRedrawLine(pcwdi, pTdgtDraw)
712    ColorWheelDlgInfo *pcwdi;
713    TdgtDraw *pTdgtDraw;
714 {
715    XPoint vs[2];
716    XGCValues values;
717 
718    values.line_width = 3;
719    XChangeGC(mainDisplay, revDefaultGC, GCLineWidth, &values);
720 
721    vs[0].x = 0;
722    vs[0].y = pcwdi->v_y;
723    vs[1].x = V_WIDTH;
724    vs[1].y = pcwdi->v_y;
725    VDrawSeg(mainDisplay, pTdgtDraw->pti->tci.win, revDefaultGC, pTdgtDraw, vs);
726 
727    values.line_width = 1;
728    XChangeGC(mainDisplay, revDefaultGC, GCLineWidth, &values);
729 
730 }
731 
732 static
HSRedrawCallback(pTdgtDraw)733 int HSRedrawCallback(pTdgtDraw)
734    TdgtDraw *pTdgtDraw;
735 {
736    TdgtBase *pTdgtBase=
737          (TdgtBase*)(pTdgtDraw->pti->tci.parent_tidgetinfo->tidget);
738    ColorWheelDlgInfo *pcwdi=(ColorWheelDlgInfo*)(pTdgtBase->pti->userdata);
739 
740    if (pTdgtDraw == NULL) return FALSE;
741 
742    XCopyArea(mainDisplay, gColorWheelInfo.hs_pixmap, pTdgtDraw->pti->tci.win,
743          gTidgetManager.gc, 0, 0, pTdgtDraw->client_area.w,
744          pTdgtDraw->client_area.h, pTdgtDraw->client_area.x,
745          pTdgtDraw->client_area.y);
746 
747    HSRedrawCrosshair(pcwdi, pTdgtDraw);
748    if (TidgetGetFocusWindow() == pTdgtDraw->pti->tci.win) {
749       TidgetDrawFocusRect(pTdgtDraw->pti, &pTdgtDraw->client_area);
750    }
751    TidgetManagerResetGC();
752 
753    return TRUE;
754 }
755 
756 /* --------------------- HSEvHandlerCallback() --------------------- */
757 
758 static
ClickInColorWheelHSPixmap(pTdgtDraw,button_ev)759 int ClickInColorWheelHSPixmap(pTdgtDraw, button_ev)
760    TdgtDraw *pTdgtDraw;
761    XButtonEvent *button_ev;
762 {
763    TdgtBase *pTdgtBase=
764          (TdgtBase*)(pTdgtDraw->pti->tci.parent_tidgetinfo->tidget);
765    ColorWheelDlgInfo *pcwdi=(ColorWheelDlgInfo*)(pTdgtBase->pti->userdata);
766    int x=button_ev->x-pTdgtDraw->client_area.x;
767    int y=button_ev->y-pTdgtDraw->client_area.y;
768 
769    if (TidgetGetFocusWindow() == pTdgtDraw->pti->tci.win) {
770       /* no need to change focus */
771    } else {
772       /* change focus */
773       TidgetSendCmd(pTdgtBase->pti, TDGTCMD_REMOVE_FOCUS, 0, NULL);
774       TidgetSetFocusWindow(pTdgtDraw->pti->tci.win);
775    }
776    if (x == pcwdi->hs_x && y == pcwdi->hs_y) {
777       /* no change */
778    } else {
779       if (x < 0) x = 0;
780       if (x >= HS_WIDTH) x = HS_WIDTH-1;
781       if (y < 0) y = 0;
782       if (y >= HS_HEIGHT) y = HS_HEIGHT-1;
783       pcwdi->hs_x = x;
784       pcwdi->hs_y = y;
785       XYtoHS(pcwdi, x, y, &pcwdi->h, &pcwdi->s);
786       SetupVPixmap(pcwdi);
787       SetRGBFromHSV(pcwdi);
788       SetPixelFromRGB(pcwdi);
789       SetColorNameFromRGB(pcwdi);
790 
791       SetupControls(pTdgtBase);
792       RedrawControls(pTdgtBase, SKIP_NONE);
793    }
794    return FALSE;
795 }
796 
797 static
HSEvHandlerCallback(pTdgtDraw,pXEv)798 int HSEvHandlerCallback(pTdgtDraw, pXEv)
799    TdgtDraw *pTdgtDraw;
800    XEvent *pXEv;
801 {
802    if (pXEv->xany.window == pTdgtDraw->pti->tci.win) {
803       if (pXEv->type == ButtonPress) {
804          XButtonEvent *button_ev=(&(pXEv->xbutton));
805 
806          return ClickInColorWheelHSPixmap(pTdgtDraw, button_ev);
807       }
808    }
809    return FALSE;
810 }
811 
812 /* --------------------- VRedrawCallback --------------------- */
813 
814 static
VRedrawCallback(pTdgtDraw)815 int VRedrawCallback(pTdgtDraw)
816    TdgtDraw *pTdgtDraw;
817 {
818    TdgtBase *pTdgtBase=
819          (TdgtBase*)(pTdgtDraw->pti->tci.parent_tidgetinfo->tidget);
820    ColorWheelDlgInfo *pcwdi=(ColorWheelDlgInfo*)(pTdgtBase->pti->userdata);
821 
822    if (pTdgtDraw == NULL) return FALSE;
823 
824    XCopyArea(mainDisplay, gColorWheelInfo.v_pixmap, pTdgtDraw->pti->tci.win,
825          gTidgetManager.gc, 0, 0, pTdgtDraw->client_area.w,
826          pTdgtDraw->client_area.h, pTdgtDraw->client_area.x,
827          pTdgtDraw->client_area.y);
828 
829    VRedrawLine(pcwdi, pTdgtDraw);
830    if (TidgetGetFocusWindow() == pTdgtDraw->pti->tci.win) {
831       TidgetDrawFocusRect(pTdgtDraw->pti, &pTdgtDraw->client_area);
832    }
833    TidgetManagerResetGC();
834 
835    return TRUE;
836 }
837 
838 /* --------------------- VEvHandlerCallback() --------------------- */
839 
840 static
ClickInColorWheelVPixmap(pTdgtDraw,button_ev)841 int ClickInColorWheelVPixmap(pTdgtDraw, button_ev)
842    TdgtDraw *pTdgtDraw;
843    XButtonEvent *button_ev;
844 {
845    TdgtBase *pTdgtBase=
846          (TdgtBase*)(pTdgtDraw->pti->tci.parent_tidgetinfo->tidget);
847    ColorWheelDlgInfo *pcwdi=(ColorWheelDlgInfo*)(pTdgtBase->pti->userdata);
848    int y=button_ev->y-pTdgtDraw->client_area.y;
849 
850    if (TidgetGetFocusWindow() == pTdgtDraw->pti->tci.win) {
851       /* no need to change focus */
852    } else {
853       /* change focus */
854       TidgetSendCmd(pTdgtBase->pti, TDGTCMD_REMOVE_FOCUS, 0, NULL);
855       TidgetSetFocusWindow(pTdgtDraw->pti->tci.win);
856    }
857    if (y == pcwdi->v_y) {
858       /* no change */
859    } else {
860       if (y < 0) y = 0;
861       if (y >= HS_HEIGHT) y = HS_HEIGHT-1;
862       pcwdi->v_y = y;
863       YtoV(pcwdi, y, &pcwdi->v);
864       SetRGBFromHSV(pcwdi);
865       SetPixelFromRGB(pcwdi);
866       SetColorNameFromRGB(pcwdi);
867 
868       SetupControls(pTdgtBase);
869       RedrawControls(pTdgtBase, SKIP_HS_PIXMAP);
870    }
871    return FALSE;
872 }
873 
874 static
VEvHandlerCallback(pTdgtDraw,pXEv)875 int VEvHandlerCallback(pTdgtDraw, pXEv)
876    TdgtDraw *pTdgtDraw;
877    XEvent *pXEv;
878 {
879    if (pXEv->xany.window == pTdgtDraw->pti->tci.win) {
880       if (pXEv->type == ButtonPress) {
881          XButtonEvent *button_ev=(&(pXEv->xbutton));
882 
883          return ClickInColorWheelVPixmap(pTdgtDraw, button_ev);
884       }
885    }
886    return FALSE;
887 }
888 
889 /* --------------------- CSampleRedrawCallback --------------------- */
890 
891 static
CSampleRedrawCallback(pTdgtDraw)892 int CSampleRedrawCallback(pTdgtDraw)
893    TdgtDraw *pTdgtDraw;
894 {
895    TdgtBase *pTdgtBase=
896          (TdgtBase*)(pTdgtDraw->pti->tci.parent_tidgetinfo->tidget);
897    ColorWheelDlgInfo *pcwdi=(ColorWheelDlgInfo*)(pTdgtBase->pti->userdata);
898 
899    if (pTdgtDraw == NULL) return FALSE;
900 
901    XSetForeground(mainDisplay, gTidgetManager.gc, pcwdi->pixel);
902    XFillRectangle(mainDisplay, pTdgtDraw->pti->tci.win, gTidgetManager.gc,
903          pTdgtDraw->client_area.x, pTdgtDraw->client_area.y,
904          pTdgtDraw->client_area.w, pTdgtDraw->client_area.h);
905 
906    TidgetManagerResetGC();
907 
908    return TRUE;
909 }
910 
911 /* --------------- KeyPress Event Handlers --------------- */
912 
913 static
ValidateNumber(buf,max,pn_val)914 int ValidateNumber(buf, max, pn_val)
915    char *buf;
916    int max, *pn_val;
917 {
918    int i=0, len=strlen(buf), ival=0;;
919 
920    for (i=0; i < len; i++) {
921       if (buf[i] < '0' || buf[i] > '9') return FALSE;
922    }
923    if (sscanf(buf, "%d", &ival) != 1) return FALSE;
924    if (ival >= 0 && ival <= max) {
925       *pn_val = ival;
926       return TRUE;
927    }
928    return FALSE;
929 }
930 
931 static
ValidateRGB(pcwdi,pn_r,pn_g,pn_b)932 int ValidateRGB(pcwdi, pn_r, pn_g, pn_b)
933    ColorWheelDlgInfo *pcwdi;
934    int *pn_r, *pn_g, *pn_b;
935 {
936    if (!ValidateNumber(TdgtSmplEditGetText(pcwdi->r_edit_ctl), 255, pn_r)) {
937       snprintf(gszMsgBox, sizeof(gszMsgBox),
938             TgLoadString(STID_GIVEN_VAL_MUST_BE_LE), "R", 255);
939       MsgBox(gszMsgBox, TOOL_NAME, INFO_MB);
940       return FALSE;
941    }
942    if (!ValidateNumber(TdgtSmplEditGetText(pcwdi->g_edit_ctl), 255, pn_g)) {
943       snprintf(gszMsgBox, sizeof(gszMsgBox),
944             TgLoadString(STID_GIVEN_VAL_MUST_BE_LE), "G", 255);
945       MsgBox(gszMsgBox, TOOL_NAME, INFO_MB);
946       return FALSE;
947    }
948    if (!ValidateNumber(TdgtSmplEditGetText(pcwdi->b_edit_ctl), 255, pn_b)) {
949       snprintf(gszMsgBox, sizeof(gszMsgBox),
950             TgLoadString(STID_GIVEN_VAL_MUST_BE_LE), "B", 255);
951       MsgBox(gszMsgBox, TOOL_NAME, INFO_MB);
952       return FALSE;
953    }
954    return TRUE;
955 }
956 
957 static
ValidateHSV(pcwdi,pn_h,pn_s,pn_v)958 int ValidateHSV(pcwdi, pn_h, pn_s, pn_v)
959    ColorWheelDlgInfo *pcwdi;
960    int *pn_h, *pn_s, *pn_v;
961 {
962    if (!ValidateNumber(TdgtSmplEditGetText(pcwdi->h_edit_ctl), 255, pn_h)) {
963       snprintf(gszMsgBox, sizeof(gszMsgBox),
964             TgLoadString(STID_GIVEN_VAL_MUST_BE_LE), "H", 255);
965       MsgBox(gszMsgBox, TOOL_NAME, INFO_MB);
966       return FALSE;
967    }
968    if (!ValidateNumber(TdgtSmplEditGetText(pcwdi->s_edit_ctl), 255, pn_s)) {
969       snprintf(gszMsgBox, sizeof(gszMsgBox),
970             TgLoadString(STID_GIVEN_VAL_MUST_BE_LE), "S", 255);
971       MsgBox(gszMsgBox, TOOL_NAME, INFO_MB);
972       return FALSE;
973    }
974    if (!ValidateNumber(TdgtSmplEditGetText(pcwdi->v_edit_ctl), 255, pn_v)) {
975       snprintf(gszMsgBox, sizeof(gszMsgBox),
976             TgLoadString(STID_GIVEN_VAL_MUST_BE_LE), "V", 255);
977       MsgBox(gszMsgBox, TOOL_NAME, INFO_MB);
978       return FALSE;
979    }
980    return TRUE;
981 }
982 
983 static
ValidateColorName(pcwdi,pn_pixel)984 int ValidateColorName(pcwdi, pn_pixel)
985    ColorWheelDlgInfo *pcwdi;
986    int *pn_pixel;
987 {
988    XColor xcolor;
989    char color_str[40];
990    char *c_ptr=TdgtSmplEditGetText(pcwdi->cname_edit_ctl);
991 
992    if (strlen(c_ptr) == 6 && AllHexChars(c_ptr)) {
993       snprintf(color_str, sizeof(color_str), "#%s", c_ptr);
994    } else {
995       UtilStrCpyN(color_str, sizeof(color_str), c_ptr);
996    }
997    if (!TgifParseColor(color_str, &xcolor) || !XAllocColor(mainDisplay,
998          mainColormap, &xcolor)) {
999       snprintf(gszMsgBox, sizeof(gszMsgBox),
1000             TgLoadString(STID_CANNOT_PARSE_NAMED_COLOR), color_str);
1001       MsgBox(gszMsgBox, TOOL_NAME, INFO_MB);
1002       return FALSE;
1003    }
1004    *pn_pixel = xcolor.pixel;
1005 
1006    return TRUE;
1007 }
1008 
1009 static
HandleCRorLF(pTdgtBase)1010 int HandleCRorLF(pTdgtBase)
1011    TdgtBase *pTdgtBase;
1012 {
1013    ColorWheelDlgInfo *pcwdi=(ColorWheelDlgInfo*)(pTdgtBase->pti->userdata);
1014    Window win=TidgetGetFocusWindow();
1015    int r=0, g=0, b=0, h=0, s=0, v=0, pixel=0;
1016    int rgb_changed=FALSE, hsv_changed=FALSE, color_name_changed=FALSE;
1017 
1018    if (win == None) return FALSE;
1019 
1020    if (win == pcwdi->r_edit_ctl->pti->tci.win) {
1021       if (!ValidateRGB(pcwdi, &r, &g, &b)) {
1022          return FALSE;
1023       }
1024       pcwdi->r = r;
1025       pcwdi->g = g;
1026       pcwdi->b = b;
1027       rgb_changed = TRUE;
1028    } else if (win == pcwdi->g_edit_ctl->pti->tci.win) {
1029       if (!ValidateRGB(pcwdi, &r, &g, &b)) {
1030          return FALSE;
1031       }
1032       pcwdi->r = r;
1033       pcwdi->g = g;
1034       pcwdi->b = b;
1035       rgb_changed = TRUE;
1036    } else if (win == pcwdi->b_edit_ctl->pti->tci.win) {
1037       if (!ValidateRGB(pcwdi, &r, &g, &b)) {
1038          return FALSE;
1039       }
1040       pcwdi->r = r;
1041       pcwdi->g = g;
1042       pcwdi->b = b;
1043       rgb_changed = TRUE;
1044    } else if (win == pcwdi->h_edit_ctl->pti->tci.win) {
1045       if (!ValidateHSV(pcwdi, &h, &s, &v)) {
1046          return FALSE;
1047       }
1048       pcwdi->h = h;
1049       pcwdi->s = s;
1050       pcwdi->v = v;
1051       hsv_changed = TRUE;
1052    } else if (win == pcwdi->s_edit_ctl->pti->tci.win) {
1053       if (!ValidateHSV(pcwdi, &h, &s, &v)) {
1054          return FALSE;
1055       }
1056       pcwdi->h = h;
1057       pcwdi->s = s;
1058       pcwdi->v = v;
1059       hsv_changed = TRUE;
1060    } else if (win == pcwdi->v_edit_ctl->pti->tci.win) {
1061       if (!ValidateHSV(pcwdi, &h, &s, &v)) {
1062          return FALSE;
1063       }
1064       pcwdi->h = h;
1065       pcwdi->s = s;
1066       pcwdi->v = v;
1067       hsv_changed = TRUE;
1068    } else if (win == pcwdi->cname_edit_ctl->pti->tci.win) {
1069       if (!ValidateColorName(pcwdi, &pixel)) {
1070          return FALSE;
1071       }
1072       UtilStrCpyN(pcwdi->color_str, sizeof(pcwdi->color_str),
1073             TdgtSmplEditGetText(pcwdi->cname_edit_ctl));
1074       pcwdi->pixel = pixel;
1075       color_name_changed = TRUE;
1076    } else {
1077       return FALSE;
1078    }
1079    if (rgb_changed || hsv_changed) {
1080       if (rgb_changed) {
1081          SetPixelFromRGB(pcwdi);
1082          SetHSVFromRGB(pcwdi);
1083       } else if (hsv_changed) {
1084          SetRGBFromHSV(pcwdi);
1085          SetPixelFromRGB(pcwdi);
1086       } else {
1087          /* should not come here */
1088       }
1089       HStoXY(pcwdi, pcwdi->h, pcwdi->s, &pcwdi->hs_x, &pcwdi->hs_y);
1090       VtoY(pcwdi, pcwdi->v, &pcwdi->v_y);
1091       SetupVPixmap(pcwdi);
1092       SetColorNameFromRGB(pcwdi);
1093    } else if (color_name_changed) {
1094       PixeltoRGB(pcwdi, pcwdi->pixel, &pcwdi->r, &pcwdi->g, &pcwdi->b);
1095       SetHSVFromRGB(pcwdi);
1096       HStoXY(pcwdi, pcwdi->h, pcwdi->s, &pcwdi->hs_x, &pcwdi->hs_y);
1097       VtoY(pcwdi, pcwdi->v, &pcwdi->v_y);
1098       SetupVPixmap(pcwdi);
1099    }
1100    SetupControls(pTdgtBase);
1101    RedrawControls(pTdgtBase, SKIP_NONE);
1102 
1103    return FALSE;
1104 }
1105 
1106 static
HandleTab(pTdgtBase)1107 int HandleTab(pTdgtBase)
1108    TdgtBase *pTdgtBase;
1109 {
1110    ColorWheelDlgInfo *pcwdi=(ColorWheelDlgInfo*)(pTdgtBase->pti->userdata);
1111    Window win=TidgetGetFocusWindow();
1112 
1113    if (win == None) {
1114       TidgetSetFocusWindow(pcwdi->cname_edit_ctl->pti->tci.win);
1115       return FALSE;
1116    }
1117    if (win == pcwdi->hs_pixmap_ctl->pti->tci.win) {
1118       TidgetSendCmd(pTdgtBase->pti, TDGTCMD_REMOVE_FOCUS, 0, NULL);
1119       TidgetSetFocusWindow(pcwdi->v_pixmap_ctl->pti->tci.win);
1120       TidgetSendCmd(pTdgtBase->pti, TDGTCMD_REMOVE_FOCUS, 0, NULL);
1121    } else if (win == pcwdi->v_pixmap_ctl->pti->tci.win) {
1122       TidgetSendCmd(pTdgtBase->pti, TDGTCMD_REMOVE_FOCUS, 0, NULL);
1123       TdgtSmplEditSetFocus(pcwdi->r_edit_ctl, TRUE);
1124    } else if (win == pcwdi->r_edit_ctl->pti->tci.win) {
1125       HandleCRorLF(pTdgtBase);
1126       TidgetSendCmd(pTdgtBase->pti, TDGTCMD_REMOVE_FOCUS, 0, NULL);
1127       TdgtSmplEditSetFocus(pcwdi->h_edit_ctl, TRUE);
1128    } else if (win == pcwdi->g_edit_ctl->pti->tci.win) {
1129       HandleCRorLF(pTdgtBase);
1130       TidgetSendCmd(pTdgtBase->pti, TDGTCMD_REMOVE_FOCUS, 0, NULL);
1131       TdgtSmplEditSetFocus(pcwdi->s_edit_ctl, TRUE);
1132    } else if (win == pcwdi->b_edit_ctl->pti->tci.win) {
1133       HandleCRorLF(pTdgtBase);
1134       TidgetSendCmd(pTdgtBase->pti, TDGTCMD_REMOVE_FOCUS, 0, NULL);
1135       TdgtSmplEditSetFocus(pcwdi->v_edit_ctl, TRUE);
1136    } else if (win == pcwdi->h_edit_ctl->pti->tci.win) {
1137       HandleCRorLF(pTdgtBase);
1138       TidgetSendCmd(pTdgtBase->pti, TDGTCMD_REMOVE_FOCUS, 0, NULL);
1139       TdgtSmplEditSetFocus(pcwdi->g_edit_ctl, TRUE);
1140    } else if (win == pcwdi->s_edit_ctl->pti->tci.win) {
1141       HandleCRorLF(pTdgtBase);
1142       TidgetSendCmd(pTdgtBase->pti, TDGTCMD_REMOVE_FOCUS, 0, NULL);
1143       TdgtSmplEditSetFocus(pcwdi->b_edit_ctl, TRUE);
1144    } else if (win == pcwdi->v_edit_ctl->pti->tci.win) {
1145       HandleCRorLF(pTdgtBase);
1146       TidgetSendCmd(pTdgtBase->pti, TDGTCMD_REMOVE_FOCUS, 0, NULL);
1147       TdgtSmplEditSetFocus(pcwdi->cname_edit_ctl, TRUE);
1148    } else if (win == pcwdi->cname_edit_ctl->pti->tci.win) {
1149       HandleCRorLF(pTdgtBase);
1150       TidgetSendCmd(pTdgtBase->pti, TDGTCMD_REMOVE_FOCUS, 0, NULL);
1151       TidgetSetFocusWindow(pcwdi->hs_pixmap_ctl->pti->tci.win);
1152       TidgetSendCmd(pTdgtBase->pti, TDGTCMD_REMOVE_FOCUS, 0, NULL);
1153    } else {
1154       return FALSE;
1155    }
1156    return FALSE;
1157 }
1158 
1159 static
HandleBackSpace(pTdgtBase)1160 int HandleBackSpace(pTdgtBase)
1161    TdgtBase *pTdgtBase;
1162 {
1163    ColorWheelDlgInfo *pcwdi=(ColorWheelDlgInfo*)(pTdgtBase->pti->userdata);
1164    Window win=TidgetGetFocusWindow();
1165 
1166    if (win == None) return FALSE;
1167 
1168    if (win == pcwdi->r_edit_ctl->pti->tci.win) {
1169       TdgtSmplEditDelLastChar(pcwdi->r_edit_ctl);
1170    } else if (win == pcwdi->g_edit_ctl->pti->tci.win) {
1171       TdgtSmplEditDelLastChar(pcwdi->g_edit_ctl);
1172    } else if (win == pcwdi->b_edit_ctl->pti->tci.win) {
1173       TdgtSmplEditDelLastChar(pcwdi->b_edit_ctl);
1174    } else if (win == pcwdi->h_edit_ctl->pti->tci.win) {
1175       TdgtSmplEditDelLastChar(pcwdi->h_edit_ctl);
1176    } else if (win == pcwdi->s_edit_ctl->pti->tci.win) {
1177       TdgtSmplEditDelLastChar(pcwdi->s_edit_ctl);
1178    } else if (win == pcwdi->v_edit_ctl->pti->tci.win) {
1179       TdgtSmplEditDelLastChar(pcwdi->v_edit_ctl);
1180    } else if (win == pcwdi->cname_edit_ctl->pti->tci.win) {
1181       TdgtSmplEditDelLastChar(pcwdi->cname_edit_ctl);
1182    }
1183    return FALSE;
1184 }
1185 
1186 static
HandleCursorKey(pTdgtBase,key_sym)1187 int HandleCursorKey(pTdgtBase, key_sym)
1188    TdgtBase *pTdgtBase;
1189    KeySym key_sym;
1190 {
1191    ColorWheelDlgInfo *pcwdi=(ColorWheelDlgInfo*)(pTdgtBase->pti->userdata);
1192    TdgtSmplEdit *pTdgtSmplEdit=NULL;
1193    char name[40];
1194    Window win=TidgetGetFocusWindow();
1195    int r=0, g=0, b=0, h=0, s=0, v=0;
1196    int dir=(-1), rgb_may_change=FALSE, hsv_may_change=FALSE;
1197 
1198    *name = '\0';
1199    if (win == None) return FALSE;
1200 
1201    switch (key_sym) {
1202    case XK_Up: dir = SCROLL_UP; break;
1203    case XK_KP_Up: dir = SCROLL_UP; break;
1204    case XK_Down: dir = SCROLL_DOWN; break;
1205    case XK_KP_Down: dir = SCROLL_DOWN; break;
1206    case XK_Left: dir = SCROLL_LEFT; break;
1207    case XK_KP_Left: dir = SCROLL_LEFT; break;
1208    case XK_Right: dir = SCROLL_RIGHT; break;
1209    case XK_KP_Right: dir = SCROLL_RIGHT; break;
1210    default: return FALSE;
1211    }
1212    if (win == pcwdi->hs_pixmap_ctl->pti->tci.win) {
1213       int x=pcwdi->hs_x, y=pcwdi->hs_y;
1214 
1215       switch (dir) {
1216       case SCROLL_UP: y--; break;
1217       case SCROLL_DOWN: y++; break;
1218       case SCROLL_LEFT: x--; break;
1219       case SCROLL_RIGHT: x++; break;
1220       }
1221       if (x < 0) x = 0;
1222       if (x > (HS_WIDTH-1)) x = (HS_WIDTH)-1;
1223       if (y < 0) y = 0;
1224       if (y > (HS_HEIGHT-1)) y = (HS_HEIGHT)-1;
1225 
1226       if (x == pcwdi->hs_x && y == pcwdi->hs_y) {
1227          /* no change */
1228       } else {
1229          pcwdi->hs_x = x;
1230          pcwdi->hs_y = y;
1231          XYtoHS(pcwdi, x, y, &pcwdi->h, &pcwdi->s);
1232          SetupVPixmap(pcwdi);
1233          SetRGBFromHSV(pcwdi);
1234          SetPixelFromRGB(pcwdi);
1235          SetColorNameFromRGB(pcwdi);
1236 
1237          SetupControls(pTdgtBase);
1238          RedrawControls(pTdgtBase, SKIP_NONE);
1239       }
1240    } else if (win == pcwdi->v_pixmap_ctl->pti->tci.win) {
1241       int y=pcwdi->v_y;
1242 
1243       switch (dir) {
1244       case SCROLL_UP: y--; break;
1245       case SCROLL_DOWN: y++; break;
1246       default: return FALSE;
1247       }
1248       if (y < 0) y = 0;
1249       if (y > (V_HEIGHT-1)) y = (V_HEIGHT)-1;
1250 
1251       if (y == pcwdi->v_y) {
1252          /* no change */
1253       } else {
1254          pcwdi->v_y = y;
1255          YtoV(pcwdi, y, &pcwdi->v);
1256          SetRGBFromHSV(pcwdi);
1257          SetPixelFromRGB(pcwdi);
1258          SetColorNameFromRGB(pcwdi);
1259 
1260          SetupControls(pTdgtBase);
1261          RedrawControls(pTdgtBase, SKIP_HS_PIXMAP);
1262       }
1263    } else if (win == pcwdi->r_edit_ctl->pti->tci.win) {
1264       strcpy(name, "R");
1265       rgb_may_change = TRUE;
1266       pTdgtSmplEdit = pcwdi->r_edit_ctl;
1267    } else if (win == pcwdi->g_edit_ctl->pti->tci.win) {
1268       strcpy(name, "G");
1269       rgb_may_change = TRUE;
1270       pTdgtSmplEdit = pcwdi->g_edit_ctl;
1271    } else if (win == pcwdi->b_edit_ctl->pti->tci.win) {
1272       strcpy(name, "B");
1273       rgb_may_change = TRUE;
1274       pTdgtSmplEdit = pcwdi->b_edit_ctl;
1275    } else if (win == pcwdi->h_edit_ctl->pti->tci.win) {
1276       strcpy(name, "H");
1277       hsv_may_change = TRUE;
1278       pTdgtSmplEdit = pcwdi->h_edit_ctl;
1279    } else if (win == pcwdi->s_edit_ctl->pti->tci.win) {
1280       strcpy(name, "S");
1281       hsv_may_change = TRUE;
1282       pTdgtSmplEdit = pcwdi->s_edit_ctl;
1283    } else if (win == pcwdi->v_edit_ctl->pti->tci.win) {
1284       strcpy(name, "V");
1285       hsv_may_change = TRUE;
1286       pTdgtSmplEdit = pcwdi->v_edit_ctl;
1287    } else if (win == pcwdi->cname_edit_ctl->pti->tci.win) {
1288       pTdgtSmplEdit = NULL;
1289    }
1290    if (pTdgtSmplEdit != NULL) {
1291       int ival=0, new_ival=0;
1292 
1293       if (rgb_may_change) {
1294          if (!ValidateRGB(pcwdi, &r, &g, &b)) {
1295             return FALSE;
1296          }
1297       } else if (hsv_may_change) {
1298          if (!ValidateHSV(pcwdi, &h, &s, &v)) {
1299             return FALSE;
1300          }
1301       } else {
1302          /* should not get here */
1303       }
1304       switch (dir) {
1305       case SCROLL_UP: break;
1306       case SCROLL_DOWN: break;
1307       default: return FALSE;
1308       }
1309       if (!ValidateNumber(TdgtSmplEditGetText(pTdgtSmplEdit), 255, &ival)) {
1310          /* this shouldn't happen because validated above already */
1311          return FALSE;
1312       }
1313       new_ival = ival;
1314 
1315       switch (dir) {
1316       case SCROLL_UP: new_ival++; break;
1317       case SCROLL_DOWN: new_ival--; break;
1318       default: return FALSE;
1319       }
1320       if (new_ival < 0) new_ival = 0;
1321       if (new_ival > 255) new_ival = 255;
1322 
1323       if (ival == new_ival) {
1324          /* no change */
1325       } else {
1326          switch (name[0]) {
1327          case 'R': pcwdi->r = new_ival; break;
1328          case 'G': pcwdi->g = new_ival; break;
1329          case 'B': pcwdi->b = new_ival; break;
1330          case 'H': pcwdi->h = new_ival; break;
1331          case 'S': pcwdi->s = new_ival; break;
1332          case 'V': pcwdi->v = new_ival; break;
1333          }
1334          if (rgb_may_change) {
1335             SetPixelFromRGB(pcwdi);
1336             SetHSVFromRGB(pcwdi);
1337          } else if (hsv_may_change) {
1338             SetRGBFromHSV(pcwdi);
1339             SetPixelFromRGB(pcwdi);
1340          } else {
1341             /* should not get here */
1342          }
1343          HStoXY(pcwdi, pcwdi->h, pcwdi->s, &pcwdi->hs_x, &pcwdi->hs_y);
1344          VtoY(pcwdi, pcwdi->v, &pcwdi->v_y);
1345          SetupVPixmap(pcwdi);
1346          SetColorNameFromRGB(pcwdi);
1347 
1348          SetupControls(pTdgtBase);
1349          RedrawControls(pTdgtBase, SKIP_NONE);
1350       }
1351    }
1352    return FALSE;
1353 }
1354 
1355 static
HandleChar(pTdgtBase,key_sym,buf,has_ch)1356 int HandleChar(pTdgtBase, key_sym, buf, has_ch)
1357    TdgtBase *pTdgtBase;
1358    KeySym key_sym;
1359    char *buf;
1360    int has_ch;
1361 {
1362    ColorWheelDlgInfo *pcwdi=(ColorWheelDlgInfo*)(pTdgtBase->pti->userdata);
1363    Window win=TidgetGetFocusWindow();
1364    TdgtSmplEdit *pTdgtSmplEdit=NULL;
1365    int len=0, len_limit=3;
1366    char *caption=NULL;
1367 
1368    if (win == None) return FALSE;
1369 
1370    if (win == pcwdi->r_edit_ctl->pti->tci.win) {
1371       pTdgtSmplEdit = pcwdi->r_edit_ctl;
1372    } else if (win == pcwdi->g_edit_ctl->pti->tci.win) {
1373       pTdgtSmplEdit = pcwdi->g_edit_ctl;
1374    } else if (win == pcwdi->b_edit_ctl->pti->tci.win) {
1375       pTdgtSmplEdit = pcwdi->b_edit_ctl;
1376    } else if (win == pcwdi->h_edit_ctl->pti->tci.win) {
1377       pTdgtSmplEdit = pcwdi->h_edit_ctl;
1378    } else if (win == pcwdi->s_edit_ctl->pti->tci.win) {
1379       pTdgtSmplEdit = pcwdi->s_edit_ctl;
1380    } else if (win == pcwdi->v_edit_ctl->pti->tci.win) {
1381       pTdgtSmplEdit = pcwdi->v_edit_ctl;
1382    } else if (win == pcwdi->cname_edit_ctl->pti->tci.win) {
1383       pTdgtSmplEdit = pcwdi->cname_edit_ctl;
1384       len_limit = 40;
1385    } else {
1386       return FALSE;
1387    }
1388    caption = TdgtSmplEditGetText(pTdgtSmplEdit);
1389    len = strlen(caption);
1390    if (len >= len_limit) return FALSE;
1391    if (len_limit == 3) {
1392       /* rgb or hsv */
1393       if (*buf < '0' || *buf > '9') return FALSE;
1394    } else {
1395       /* anything goes */
1396    }
1397    TdgtSmplEditAppendStr(pTdgtSmplEdit, buf, 1);
1398 
1399    return FALSE;
1400 }
1401 
1402 /* --------------- TdgtColorWheelDlgKeyPressEvHandlerCallback --------------- */
1403 
1404 static XComposeStatus c_stat;
1405 
1406 static
TdgtColorWheelDlgKeyPressEvHandlerCallback(pTdgtBase,pXEv)1407 int TdgtColorWheelDlgKeyPressEvHandlerCallback(pTdgtBase, pXEv)
1408    TdgtBase *pTdgtBase;
1409    XEvent *pXEv;
1410 {
1411    ColorWheelDlgInfo *pcwdi=(ColorWheelDlgInfo*)(pTdgtBase->pti->userdata);
1412    int has_ch=0;
1413    char buf[80];
1414    KeySym key_sym=(KeySym)0;
1415    XKeyEvent *key_ev=NULL;
1416 
1417    if (pXEv->type != KeyPress) return FALSE;
1418    if (pcwdi == NULL) return FALSE;
1419 
1420    key_ev = (&(pXEv->xkey));
1421    has_ch = XLookupString(key_ev, buf, sizeof(buf), &key_sym, &c_stat);
1422    TranslateKeys(buf, &key_sym);
1423 
1424    if (CharIsESC(key_ev, buf, key_sym, &has_ch)) {
1425       HideTdgtColorWheelDialogBox();
1426    } else if (CharIsCRorLF(key_ev, buf, key_sym, &has_ch)) {
1427       return HandleCRorLF(pTdgtBase);
1428    } else if (CharIsTAB(key_ev, buf, key_sym, &has_ch)) {
1429       return HandleTab(pTdgtBase);
1430    } else if (CharIsCntrlSpace(key_ev, buf, key_sym, &has_ch)) {
1431       HideTdgtColorWheelDialogBox();
1432    } else if (CharIsBS(key_ev, buf, key_sym, &has_ch, FALSE)) {
1433       return HandleBackSpace(pTdgtBase);
1434    } else if (key_sym==XK_Up || key_sym==XK_KP_Up || key_sym==XK_Down ||
1435          key_sym==XK_KP_Down || key_sym==XK_Left || key_sym==XK_KP_Left ||
1436          key_sym==XK_Right || key_sym==XK_KP_Right ||
1437          (key_sym>'\040' && key_sym<='\177' &&
1438          (key_ev->state & ControlMask) == ControlMask)) {
1439       return HandleCursorKey(pTdgtBase, key_sym);
1440    } else if (key_sym>='\040' && key_sym<='\177') {
1441       return HandleChar(pTdgtBase, key_sym, buf, has_ch);
1442    }
1443    return FALSE;
1444 }
1445 
1446 /* --------------------- ShowTdgtColorWheelDialogBox --------------------- */
1447 
ShowTdgtColorWheelDialogBox()1448 void ShowTdgtColorWheelDialogBox()
1449 {
1450    TidgetInfo *pti=gColorWheelInfo.dialogbox_tidgetinfo;
1451    ColorWheelDlgInfo *pcwdi=(ColorWheelDlgInfo*)(pti->userdata);
1452 
1453    TidgetManagerSetWantAllKeyPressEvents(pti, TRUE);
1454    gColorWheelInfo.mapped = TRUE;
1455    MapTidget(pti);
1456 
1457    if (pcwdi != NULL) {
1458       ClearAllControls((TdgtBase*)(pti->tidget));
1459       SetupColorsFromCurrentColor(pcwdi);
1460       SetupControls((TdgtBase*)(pti->tidget));
1461    }
1462 }
1463 
1464 /* --------------------- HideTdgtColorWheelDialogBox --------------------- */
1465 
HideTdgtColorWheelDialogBox()1466 void HideTdgtColorWheelDialogBox()
1467 {
1468    TidgetManagerSetWantAllKeyPressEvents(NULL, FALSE);
1469    TidgetSetHasFocus(FALSE);
1470    gColorWheelInfo.mapped = FALSE;
1471    XUnmapWindow(mainDisplay, gColorWheelInfo.dialogbox_tidgetinfo->tci.win);
1472 
1473    if (warpToWinCenter) {
1474       XWarpPointer(mainDisplay, None, drawWindow, 0, 0, 0, 0,
1475             (int)(ZOOMED_SIZE(drawWinW)>>1), (int)(ZOOMED_SIZE(drawWinH)>>1));
1476    }
1477 }
1478 
1479 /* --------------------- CreateTdgtColorWheelDialogBox --------------------- */
1480 
1481 static
TdgtColorWheelDlgSetEntries(pti,dsp_ptr,entries,num_entries,marked_index)1482 int TdgtColorWheelDlgSetEntries(pti, dsp_ptr, entries, num_entries, marked_index)
1483    TidgetInfo *pti;
1484    DspList *dsp_ptr;
1485    char **entries;
1486    int num_entries, marked_index;
1487 {
1488    return TRUE;
1489 }
1490 
1491 static
TdgtColorWheelDlgSetMsg(pti,msg)1492 int TdgtColorWheelDlgSetMsg(pti, msg)
1493    TidgetInfo *pti;
1494    char *msg;
1495 {
1496    return TRUE;
1497 }
1498 
1499 static
InitTdgtColorWheelDlgInfo(pti)1500 void InitTdgtColorWheelDlgInfo(pti)
1501    TidgetInfo *pti;
1502 {
1503    ColorWheelDlgInfo *pcwdi=NULL;
1504 
1505    if (pti == NULL) return;
1506 
1507    pcwdi = (ColorWheelDlgInfo*)(pti->userdata);
1508    if (pcwdi == NULL) return;
1509 
1510    pcwdi->screen_w = DisplayWidth(mainDisplay, mainScreen);
1511    pcwdi->screen_h = DisplayHeight(mainDisplay, mainScreen);
1512    TdgtColorWheelDlgSetMsg(pti, "");
1513 
1514    pcwdi->def_btn_id = ID_CWHEEL_BTN_OK;
1515 }
1516 
1517 typedef struct tagCreateBtnInfo {
1518    char *text;
1519    int ctl_id;
1520 } CreateBtnInfo;
1521 
1522 static CreateBtnInfo cbi[] = {
1523    { "Add to Palette", ID_CWHEEL_BTN_ADD    },
1524    { "Add and Close",  ID_CWHEEL_BTN_OK     },
1525    { "Close",          ID_CWHEEL_BTN_CANCEL },
1526    { NULL, 0 }
1527 };
1528 
1529 static
CreateColorWheelButtons(pTdgtBase)1530 int CreateColorWheelButtons(pTdgtBase)
1531    TdgtBase *pTdgtBase;
1532 {
1533    ColorWheelDlgInfo *pcwdi=(ColorWheelDlgInfo*)(pTdgtBase->pti->userdata);
1534    TdgtBtnRow *pTdgtBtnRow=pcwdi->btn_row_ctl;
1535    int i=0;
1536 
1537    for (i=0; cbi[i].text != NULL; i++) {
1538       TdgtBtn *pTdgtBtn=NULL;
1539       int btn_w=0, btn_h=0;
1540 
1541       CalcTdgtBtnDim(cbi[i].text, 8, TDGTBTN_DEF_H_PAD, TDGTBTN_DEF_V_PAD,
1542             &btn_w, &btn_h);
1543       pTdgtBtn = CreateTdgtBtn(pTdgtBtnRow->pti->tci.win,
1544             pTdgtBtnRow->pti, cbi[i].ctl_id, 0, 0, btn_w, btn_h,
1545             TDGTBTN_DEF_H_PAD, TDGTBTN_DEF_V_PAD, TGMUTYPE_TEXT, TDGTBTN_CLICK,
1546             TGBS_NORMAL, STYLE_NR, cbi[i].text, NULL);
1547       if (pTdgtBtn == NULL) {
1548          DestroyTidget(&pTdgtBase->pti);
1549          return FALSE;
1550       }
1551       if (!TdgtBtnRowAddBtn(pTdgtBtnRow, pTdgtBtn)) {
1552          DestroyTidget(&pTdgtBase->pti);
1553          return FALSE;
1554       }
1555    }
1556    return TRUE;
1557 }
1558 
1559 static
AdjustDialogBoxWidth(dpy,pTdgtBase)1560 void AdjustDialogBoxWidth(dpy, pTdgtBase)
1561    Display *dpy;
1562    TdgtBase *pTdgtBase;
1563 {
1564    ColorWheelDlgInfo *pcwdi=(ColorWheelDlgInfo*)(pTdgtBase->pti->userdata);
1565    int max_w=0, bottom=0, h_pad=pTdgtBase->pti->tci.h_pad, cur_x=0, cur_y=0;
1566    int v_pad=pTdgtBase->pti->tci.v_pad, end_y=0;
1567    SimpleWinInfo *pswi=NULL;
1568 
1569    /* hue edit */
1570    pswi = (&(pcwdi->h_edit_ctl->pti->tci.win_info));
1571    max_w = pcwdi->hue_edit_x + pswi->w - pcwdi->hs_pixmap_x;
1572 
1573    bottom = pcwdi->btn_row_ctl->pti->tci.win_info.y +
1574          pcwdi->btn_row_ctl->pti->tci.win_info.h;
1575 
1576    cur_x = windowPadding + h_pad;
1577    cur_y = windowPadding + v_pad;
1578 
1579    /* hs_pixmap */
1580    pswi = (&(pcwdi->hs_pixmap_ctl->pti->tci.win_info));
1581    end_y = pswi->y+pswi->h;
1582 
1583    /* button row */
1584    pswi = (&(pcwdi->btn_row_ctl->pti->tci.win_info));
1585    pswi->w = max_w;
1586    TidgetMoveResize(pcwdi->btn_row_ctl->pti, pswi->x, pswi->y, pswi->w,
1587          pswi->h);
1588 
1589    pswi = (&(pTdgtBase->pti->tci.win_info));
1590    pswi->w = max_w+(windowPadding<<1)+(h_pad<<1);
1591    pswi->h = bottom+windowPadding+v_pad;
1592    TidgetMoveResize(pTdgtBase->pti, pswi->x, pswi->y, pswi->w, pswi->h);
1593 }
1594 
1595 static
TdgtColorWheelDlgWmDeleteCallback(pTdgtBase)1596 void TdgtColorWheelDlgWmDeleteCallback(pTdgtBase)
1597    TdgtBase *pTdgtBase;
1598 {
1599    ColorWheelDlgInfo *pcwdi=(ColorWheelDlgInfo*)(pTdgtBase->pti->userdata);
1600 
1601    if (pcwdi == NULL) return;
1602 
1603    HideTdgtColorWheelDialogBox();
1604 }
1605 
1606 static
TdgtColorWheelDlgFocusCallback(pTdgtBase,cmd_type,cmd_arg,pv_cmd_userdata)1607 int TdgtColorWheelDlgFocusCallback(pTdgtBase, cmd_type, cmd_arg,
1608       pv_cmd_userdata)
1609    TdgtBase *pTdgtBase;
1610    int cmd_type, cmd_arg;
1611    void *pv_cmd_userdata;
1612 {
1613    ColorWheelDlgInfo *pcwdi=(ColorWheelDlgInfo*)(pTdgtBase->pti->userdata);
1614 
1615    if (pcwdi == NULL) return FALSE;
1616 
1617    switch (cmd_type) {
1618    case TDGTCMD_CAN_HANDLE_FOCUS_CHANGE: return TRUE;
1619    case TDGTCMD_REMOVE_FOCUS: ColorWheelDlgRemoveFocus(pTdgtBase); return TRUE;
1620    }
1621    return FALSE;
1622 }
1623 
1624 static
AdjustDialogBoxSize(dpy,pTdgtBase)1625 void AdjustDialogBoxSize(dpy, pTdgtBase)
1626    Display *dpy;
1627    TdgtBase *pTdgtBase;
1628 {
1629    ColorWheelDlgInfo *pcwdi=(ColorWheelDlgInfo*)(pTdgtBase->pti->userdata);
1630    int base_win_x=0, base_win_y=0, base_win_w=0, base_win_h=0;
1631    XWMHints wmhints;
1632    XSizeHints sizehints;
1633 
1634    if (pTdgtBase == NULL) return;
1635 
1636    AdjustDialogBoxWidth(dpy, pTdgtBase);
1637 
1638    base_win_w = pTdgtBase->pti->tci.win_info.w;
1639    base_win_h = pTdgtBase->pti->tci.win_info.h;
1640 
1641    memset(&wmhints, 0, sizeof(XWMHints));
1642    wmhints.flags = InputHint | StateHint;
1643    wmhints.input = True;
1644    wmhints.initial_state = NormalState;
1645    XSetWMHints(dpy, pTdgtBase->pti->tci.win, &wmhints);
1646 
1647    base_win_x = (base_win_w > pcwdi->screen_w) ? 0 :
1648          ((pcwdi->screen_w-base_win_w)>>1);
1649    base_win_y = (base_win_h > pcwdi->screen_h) ? 0 :
1650          ((pcwdi->screen_h-base_win_h)/3);
1651 
1652    XMoveResizeWindow(dpy, pTdgtBase->pti->tci.win, base_win_x,
1653          base_win_y, base_win_w, base_win_h);
1654 
1655    sizehints.flags = PPosition | PSize | USPosition | PMinSize | PMaxSize;
1656    sizehints.x = base_win_x;
1657    sizehints.y = base_win_y;
1658    sizehints.width = sizehints.min_width = sizehints.max_width = base_win_w;
1659    sizehints.height = sizehints.min_height = sizehints.max_height = base_win_h;
1660 #ifdef NOTR4MODE
1661    XSetNormalHints(dpy, pTdgtBase->pti->tci.win, &sizehints);
1662 #else
1663    XSetWMNormalHints(dpy, pTdgtBase->pti->tci.win, &sizehints);
1664 #endif
1665 
1666    XSetTransientForHint(dpy, pTdgtBase->pti->tci.win, mainWindow);
1667 }
1668 
1669 static
SetupHSPixmap(pcwdi)1670 int SetupHSPixmap(pcwdi)
1671    ColorWheelDlgInfo *pcwdi;
1672 {
1673    int row=0;
1674    Pixmap pixmap=gColorWheelInfo.hs_pixmap;
1675    XImage *image=NULL;
1676 
1677    if (pixmap == None) return FALSE;
1678    image = XGetImage(mainDisplay, pixmap, 0, 0, HS_WIDTH, HS_HEIGHT,
1679          AllPlanes, ZPixmap);
1680    if (image == NULL) {
1681       Msg(TgLoadString(STID_XGETIMAGE_MAY_RUN_OUT_VMEM));
1682       return FALSE;
1683    }
1684    for (row=0; row < HS_HEIGHT; row++) {
1685       int col=0;
1686       double sat=(((double)(HS_HEIGHT-1-row))/((double)(HS_HEIGHT-1)));
1687 
1688       sat *= ((double)0x0ffff);
1689       for (col=0; col < HS_WIDTH; col++) {
1690          int hue=(col*2), r=0, g=0, b=0;
1691          double dr=(double)0, dg=(double)0, db=(double)0;
1692          uint32_t pixel=0;
1693 
1694          HSVtoRGB(hue, sat, 0x0ffff, &r, &g, &b);
1695          dr = ((double)r) / ((double)0x0ffff) * pcwdi->tci.dr_maxval;
1696          dg = ((double)g) / ((double)0x0ffff) * pcwdi->tci.dr_maxval;
1697          db = ((double)b) / ((double)0x0ffff) * pcwdi->tci.dr_maxval;
1698          r = round(dr);
1699          g = round(dg);
1700          b = round(db);
1701          if (r < 0) r = 0;
1702          if (g < 0) g = 0;
1703          if (b < 0) b = 0;
1704          if (r > 255) r = 255;
1705          if (g > 255) g = 255;
1706          if (b > 255) b = 255;
1707          pixel = RGBtoPixel(pcwdi, r, g, b);
1708          XPutPixel(image, col, row, pixel);
1709       }
1710    }
1711    XPutImage(mainDisplay, pixmap, xpmGC, image, 0, 0, 0, 0, HS_WIDTH,
1712          HS_HEIGHT);
1713    XDestroyImage(image);
1714 
1715    return TRUE;
1716 }
1717 
CreateTdgtColorWheelDialogBox(dpy,parent_win)1718 TidgetInfo *CreateTdgtColorWheelDialogBox(dpy, parent_win)
1719    Display *dpy;
1720    Window parent_win; /* should be the rootWindow */
1721 {
1722    int cur_x=0, cur_y=0, v_gap=20, h_pad=0, v_pad=0;
1723    int w=0, h=0, vh_padding=0, h_gap=14, end_y=0, rgbhsv_width=0;
1724    TdgtBase *pTdgtBase=NULL;
1725    ColorWheelDlgInfo *pcwdi=NULL;
1726    char caption[MAXSTRING];
1727 
1728    TidgetSetFocusWindow(None);
1729    if (!colorDisplay || mainVisual->class != TrueColor) {
1730       sprintf(gszMsgBox, TgLoadString(STID_COLOR_WHEEL_NOT_AVAIL));
1731       MsgBox(gszMsgBox, TOOL_NAME, INFO_MB);
1732       free(pcwdi);
1733       return NULL;
1734    }
1735    pcwdi = (ColorWheelDlgInfo*)malloc(sizeof(ColorWheelDlgInfo));
1736    if (pcwdi == NULL) FailAllocMessage();
1737    memset(pcwdi, 0, sizeof(ColorWheelDlgInfo));
1738 
1739    memset(&pcwdi->tci, 0, sizeof(TrueColorInfo));
1740    if (!SetupTrueColorInfo(&pcwdi->tci) || !SetupHSPixmap(pcwdi) ||
1741          !SetupColorsFromCurrentColor(pcwdi)) {
1742       free(pcwdi);
1743       return NULL;
1744    }
1745    if (boldMsgFontSet != NULL || boldMsgFontPtr != NULL) {
1746        rgbhsv_width = BoldMsgTextWidth(boldMsgFontPtr, "9999", 4);
1747    } else if (msgFontSet != NULL || msgFontPtr != NULL) {
1748        rgbhsv_width = MsgTextWidth(msgFontPtr, "9999", 4);
1749    } else {
1750        rgbhsv_width = defaultFontWidth * 4;
1751    }
1752    h_pad = TDGTBASE_DEF_H_PAD;
1753    v_pad = TDGTBASE_DEF_V_PAD;
1754    if (msgFontSet != NULL || msgFontPtr != NULL) {
1755       vh_padding = (msgFontWidth<<1);
1756    } else {
1757       vh_padding = (defaultFontWidth<<1);
1758    }
1759    cur_x = vh_padding+windowPadding;
1760    cur_y = vh_padding+windowPadding;
1761 
1762    sprintf(caption, TgLoadString(STID_TOOL_COLOR_WHEEL), TOOL_NAME);
1763    pTdgtBase = CreateTdgtBase(parent_win, NULL, ID_CWHEEL_DIALOG, 0, 0, 100,
1764          100, vh_padding, vh_padding, (dialogboxUse3DBorder ? TGBS_RAISED :
1765          TGBS_NORMAL), caption);
1766    if (pTdgtBase == NULL) {
1767       free(pcwdi);
1768       return NULL;
1769    }
1770    pTdgtBase->pti->userdata = pcwdi;
1771    InitTdgtColorWheelDlgInfo(pTdgtBase->pti);
1772 
1773    /* hs_pixmap */
1774    pcwdi->hs_pixmap_x = cur_x;
1775    pcwdi->hs_pixmap_y = cur_y;
1776    pcwdi->hs_pixmap_ctl = CreateTdgtDraw(pTdgtBase->pti->tci.win,
1777          pTdgtBase->pti, ID_CWHEEL_HS_AREA, cur_x, cur_y, HS_WIDTH, HS_HEIGHT,
1778          TDGTDRAW_DEF_H_PAD, TDGTDRAW_DEF_V_PAD, TGBS_LOWRED, NULL);
1779    if (pcwdi->hs_pixmap_ctl == NULL) {
1780       DestroyTidget(&pTdgtBase->pti);
1781       return NULL;
1782    }
1783    TdgtDrawSetRedrawCallback(pcwdi->hs_pixmap_ctl, HSRedrawCallback);
1784    TdgtDrawSetEvHandlerCallback(pcwdi->hs_pixmap_ctl, HSEvHandlerCallback);
1785 
1786    cur_x += pcwdi->hs_pixmap_ctl->pti->tci.win_info.w + h_gap;
1787    end_y = cur_y + pcwdi->hs_pixmap_ctl->pti->tci.win_info.h - windowPadding;
1788 
1789    /* v_pixmap */
1790    pcwdi->v_pixmap_x = cur_x;
1791    pcwdi->v_pixmap_y = cur_y;
1792    pcwdi->v_pixmap_ctl = CreateTdgtDraw(pTdgtBase->pti->tci.win, pTdgtBase->pti,
1793          ID_CWHEEL_V_AREA, cur_x, cur_y, V_WIDTH, V_HEIGHT, TDGTDRAW_DEF_H_PAD,
1794          TDGTDRAW_DEF_V_PAD, TGBS_LOWRED, NULL);
1795    if (pcwdi->v_pixmap_ctl == NULL) {
1796       DestroyTidget(&pTdgtBase->pti);
1797       return NULL;
1798    }
1799    TdgtDrawSetRedrawCallback(pcwdi->v_pixmap_ctl, VRedrawCallback);
1800    TdgtDrawSetEvHandlerCallback(pcwdi->v_pixmap_ctl, VEvHandlerCallback);
1801 
1802    cur_x += pcwdi->v_pixmap_ctl->pti->tci.win_info.w + h_gap;
1803 
1804    /* row 1 */
1805 
1806    /* red msg */
1807    pcwdi->red_msg_x = cur_x;
1808    pcwdi->red_msg_y = cur_y;
1809    pcwdi->r_msg_ctl = CreateTdgtMsg(pTdgtBase->pti->tci.win, pTdgtBase->pti,
1810          INVALID, cur_x, cur_y, TDGTMSG_DEF_H_PAD, TDGTMSG_DEF_V_PAD,
1811          TGBS_NORMAL, "R:", JUST_L);
1812    if (pcwdi->r_msg_ctl == NULL) {
1813       DestroyTidget(&pTdgtBase->pti);
1814       return NULL;
1815    }
1816    cur_x += pcwdi->r_msg_ctl->pti->tci.win_info.w;
1817 
1818    /* red edit */
1819    pcwdi->red_edit_x = cur_x;
1820    pcwdi->red_edit_y = cur_y;
1821    pcwdi->r_edit_ctl = CreateTdgtSmplEdit(pTdgtBase->pti->tci.win,
1822          pTdgtBase->pti, ID_CWHEEL_EDIT_R, cur_x, cur_y, rgbhsv_width,
1823          TDGTSEDT_DEF_H_PAD, TDGTSEDT_DEF_V_PAD, "", STYLE_NR, INVALID);
1824    if (pcwdi->r_edit_ctl == NULL) {
1825       DestroyTidget(&pTdgtBase->pti);
1826       return NULL;
1827    }
1828    cur_x += pcwdi->r_edit_ctl->pti->tci.win_info.w + 4;
1829 
1830    /* hue msg */
1831    pcwdi->hue_msg_x = cur_x;
1832    pcwdi->hue_msg_y = cur_y;
1833    pcwdi->h_msg_ctl = CreateTdgtMsg(pTdgtBase->pti->tci.win, pTdgtBase->pti,
1834          INVALID, cur_x, cur_y, TDGTMSG_DEF_H_PAD, TDGTMSG_DEF_V_PAD,
1835          TGBS_NORMAL, "H:", JUST_L);
1836    if (pcwdi->h_msg_ctl == NULL) {
1837       DestroyTidget(&pTdgtBase->pti);
1838       return NULL;
1839    }
1840    cur_x += pcwdi->h_msg_ctl->pti->tci.win_info.w;
1841 
1842    /* hue edit */
1843    pcwdi->hue_edit_x = cur_x;
1844    pcwdi->hue_edit_y = cur_y;
1845    pcwdi->h_edit_ctl = CreateTdgtSmplEdit(pTdgtBase->pti->tci.win,
1846          pTdgtBase->pti, ID_CWHEEL_EDIT_H, cur_x, cur_y, rgbhsv_width,
1847          TDGTSEDT_DEF_H_PAD, TDGTSEDT_DEF_V_PAD, "", STYLE_NR, INVALID);
1848    if (pcwdi->h_edit_ctl == NULL) {
1849       DestroyTidget(&pTdgtBase->pti);
1850       return NULL;
1851    }
1852    cur_x = pcwdi->red_msg_x;
1853    cur_y += pcwdi->h_edit_ctl->pti->tci.win_info.h + 4;
1854 
1855    /* row 2 */
1856 
1857    /* green msg */
1858    pcwdi->green_msg_x = cur_x;
1859    pcwdi->green_msg_y = cur_y;
1860    pcwdi->g_msg_ctl = CreateTdgtMsg(pTdgtBase->pti->tci.win, pTdgtBase->pti,
1861          INVALID, cur_x, cur_y, TDGTMSG_DEF_H_PAD, TDGTMSG_DEF_V_PAD,
1862          TGBS_NORMAL, "G:", JUST_L);
1863    if (pcwdi->g_msg_ctl == NULL) {
1864       DestroyTidget(&pTdgtBase->pti);
1865       return NULL;
1866    }
1867    cur_x = pcwdi->red_edit_x;
1868 
1869    /* green edit */
1870    pcwdi->green_edit_x = cur_x;
1871    pcwdi->green_edit_y = cur_y;
1872    pcwdi->g_edit_ctl = CreateTdgtSmplEdit(pTdgtBase->pti->tci.win,
1873          pTdgtBase->pti, ID_CWHEEL_EDIT_G, cur_x, cur_y, rgbhsv_width,
1874          TDGTSEDT_DEF_H_PAD, TDGTSEDT_DEF_V_PAD, "", STYLE_NR, INVALID);
1875    if (pcwdi->g_edit_ctl == NULL) {
1876       DestroyTidget(&pTdgtBase->pti);
1877       return NULL;
1878    }
1879    cur_x = pcwdi->hue_msg_x;
1880 
1881    /* sat msg */
1882    pcwdi->sat_msg_x = cur_x;
1883    pcwdi->sat_msg_y = cur_y;
1884    pcwdi->s_msg_ctl = CreateTdgtMsg(pTdgtBase->pti->tci.win, pTdgtBase->pti,
1885          INVALID, cur_x, cur_y, TDGTMSG_DEF_H_PAD, TDGTMSG_DEF_V_PAD,
1886          TGBS_NORMAL, "S:", JUST_L);
1887    if (pcwdi->s_msg_ctl == NULL) {
1888       DestroyTidget(&pTdgtBase->pti);
1889       return NULL;
1890    }
1891    cur_x = pcwdi->hue_edit_x;
1892 
1893    /* sat edit */
1894    pcwdi->sat_edit_x = cur_x;
1895    pcwdi->sat_edit_y = cur_y;
1896    pcwdi->s_edit_ctl = CreateTdgtSmplEdit(pTdgtBase->pti->tci.win,
1897          pTdgtBase->pti, ID_CWHEEL_EDIT_S, cur_x, cur_y, rgbhsv_width,
1898          TDGTSEDT_DEF_H_PAD, TDGTSEDT_DEF_V_PAD, "", STYLE_NR, INVALID);
1899    if (pcwdi->s_edit_ctl == NULL) {
1900       DestroyTidget(&pTdgtBase->pti);
1901       return NULL;
1902    }
1903    cur_x = pcwdi->red_msg_x;
1904    cur_y += pcwdi->s_edit_ctl->pti->tci.win_info.h + 4;
1905 
1906    /* row 3 */
1907 
1908    /* blue msg */
1909    pcwdi->blue_msg_x = cur_x;
1910    pcwdi->blue_msg_y = cur_y;
1911    pcwdi->b_msg_ctl = CreateTdgtMsg(pTdgtBase->pti->tci.win, pTdgtBase->pti,
1912          INVALID, cur_x, cur_y, TDGTMSG_DEF_H_PAD, TDGTMSG_DEF_V_PAD,
1913          TGBS_NORMAL, "B:", JUST_L);
1914    if (pcwdi->b_msg_ctl == NULL) {
1915       DestroyTidget(&pTdgtBase->pti);
1916       return NULL;
1917    }
1918    cur_x += pcwdi->b_msg_ctl->pti->tci.win_info.w;
1919 
1920    /* blue edit */
1921    pcwdi->blue_edit_x = cur_x;
1922    pcwdi->blue_edit_y = cur_y;
1923    pcwdi->b_edit_ctl = CreateTdgtSmplEdit(pTdgtBase->pti->tci.win,
1924          pTdgtBase->pti, ID_CWHEEL_EDIT_B, cur_x, cur_y, rgbhsv_width,
1925          TDGTSEDT_DEF_H_PAD, TDGTSEDT_DEF_V_PAD, "", STYLE_NR, INVALID);
1926    if (pcwdi->b_edit_ctl == NULL) {
1927       DestroyTidget(&pTdgtBase->pti);
1928       return NULL;
1929    }
1930    cur_x += pcwdi->b_edit_ctl->pti->tci.win_info.w + 4;
1931 
1932    /* val msg */
1933    pcwdi->val_msg_x = cur_x;
1934    pcwdi->val_msg_y = cur_y;
1935    pcwdi->v_msg_ctl = CreateTdgtMsg(pTdgtBase->pti->tci.win, pTdgtBase->pti,
1936          INVALID, cur_x, cur_y, TDGTMSG_DEF_H_PAD, TDGTMSG_DEF_V_PAD,
1937          TGBS_NORMAL, "V:", JUST_L);
1938    if (pcwdi->v_msg_ctl == NULL) {
1939       DestroyTidget(&pTdgtBase->pti);
1940       return NULL;
1941    }
1942    cur_x += pcwdi->v_msg_ctl->pti->tci.win_info.w;
1943 
1944    /* val edit */
1945    pcwdi->val_edit_x = cur_x;
1946    pcwdi->val_edit_y = cur_y;
1947    pcwdi->v_edit_ctl = CreateTdgtSmplEdit(pTdgtBase->pti->tci.win,
1948          pTdgtBase->pti, ID_CWHEEL_EDIT_V, cur_x, cur_y, rgbhsv_width,
1949          TDGTSEDT_DEF_H_PAD, TDGTSEDT_DEF_V_PAD, "", STYLE_NR, INVALID);
1950    if (pcwdi->v_edit_ctl == NULL) {
1951       DestroyTidget(&pTdgtBase->pti);
1952       return NULL;
1953    }
1954    cur_x = pcwdi->red_msg_x;
1955    cur_y += pcwdi->v_edit_ctl->pti->tci.win_info.h + 4;
1956 
1957    /* row 4 */
1958 
1959    /* cname msg */
1960    pcwdi->cname_msg_x = cur_x;
1961    pcwdi->cname_msg_y = cur_y;
1962    pcwdi->cname_msg_ctl = CreateTdgtMsg(pTdgtBase->pti->tci.win, pTdgtBase->pti,
1963          INVALID, cur_x, cur_y, TDGTMSG_DEF_H_PAD, TDGTMSG_DEF_V_PAD,
1964          TGBS_NORMAL, "Name:", JUST_L);
1965    if (pcwdi->cname_msg_ctl == NULL) {
1966       DestroyTidget(&pTdgtBase->pti);
1967       return NULL;
1968    }
1969    cur_x += pcwdi->cname_msg_ctl->pti->tci.win_info.w;
1970 
1971    /* cname edit */
1972    pcwdi->cname_edit_x = cur_x;
1973    pcwdi->cname_edit_y = cur_y;
1974    w = pcwdi->val_edit_x + pcwdi->v_edit_ctl->pti->tci.win_info.w - cur_x -
1975          (TDGTSEDT_DEF_H_PAD<<1) - (windowPadding<<1);
1976    pcwdi->cname_edit_ctl = CreateTdgtSmplEdit(pTdgtBase->pti->tci.win,
1977          pTdgtBase->pti, ID_CWHEEL_EDIT_B, cur_x, cur_y, w,
1978          TDGTSEDT_DEF_H_PAD, TDGTSEDT_DEF_V_PAD, "", STYLE_NR, INVALID);
1979    if (pcwdi->cname_edit_ctl == NULL) {
1980       DestroyTidget(&pTdgtBase->pti);
1981       return NULL;
1982    }
1983    cur_x = pcwdi->red_msg_x;
1984    cur_y += pcwdi->cname_edit_ctl->pti->tci.win_info.h + 4;
1985 
1986    /* row 5 */
1987 
1988    /* csmpl_ctl */
1989    pcwdi->csmpl_edit_x = cur_x;
1990    pcwdi->csmpl_edit_y = cur_y;
1991    w = pcwdi->val_edit_x + pcwdi->v_edit_ctl->pti->tci.win_info.w - cur_x -
1992          (TDGTSEDT_DEF_H_PAD<<1) - (windowPadding<<1);
1993    h = end_y - cur_y - (TDGTSEDT_DEF_V_PAD<<1) - (windowPadding<<1);
1994    pcwdi->csmpl_ctl = CreateTdgtDraw(pTdgtBase->pti->tci.win,
1995          pTdgtBase->pti, ID_CWHEEL_HS_AREA, cur_x, cur_y, w, h,
1996          TDGTDRAW_DEF_H_PAD, TDGTDRAW_DEF_V_PAD, TGBS_LOWRED, NULL);
1997    if (pcwdi->hs_pixmap_ctl == NULL) {
1998       DestroyTidget(&pTdgtBase->pti);
1999       return NULL;
2000    }
2001    TdgtDrawSetRedrawCallback(pcwdi->csmpl_ctl, CSampleRedrawCallback);
2002 
2003    /* buttons */
2004 
2005    cur_x = vh_padding+windowPadding;
2006    cur_y = end_y + v_gap;
2007    pcwdi->btn_row_ctl = CreateTdgtBtnRow(pTdgtBase->pti->tci.win,
2008          pTdgtBase->pti, INVALID, cur_x, cur_y, TDGTBROW_DEF_H_PAD,
2009          TDGTBROW_DEF_V_PAD, TGBS_NORMAL, defaultFontWidth, JUST_C);
2010    if (pcwdi->btn_row_ctl == NULL) {
2011       DestroyTidget(&pTdgtBase->pti);
2012       return NULL;
2013    }
2014    if (!CreateColorWheelButtons(pTdgtBase)) {
2015       DestroyTidget(&pTdgtBase->pti);
2016       return NULL;
2017    }
2018    TdgtColorWheelDlgSetEntries(pTdgtBase->pti, NULL, NULL, 0, INVALID);
2019 
2020    /*
2021     * Specify that the base tidget will handle all the KeyPress events
2022     *         in all its children tidgets.
2023     */
2024    TdgtBaseWillHandleKeyPressEvents(pTdgtBase,
2025          TdgtColorWheelDlgKeyPressEvHandlerCallback);
2026    /*
2027     * Specify that the base tidget will handle all the KeyPress events
2028     *         in all its children tidgets.
2029     */
2030    TdgtBaseSetWmDeleteCallback(pTdgtBase, TdgtColorWheelDlgWmDeleteCallback);
2031 
2032    /*
2033     * Specify a callback function to answer some focus related questions.
2034     */
2035    TdgtBaseSetFocusCallback(pTdgtBase, TdgtColorWheelDlgFocusCallback);
2036 
2037    AdjustDialogBoxSize(dpy, pTdgtBase);
2038    TidgetSetFocusWindow(pcwdi->cname_edit_ctl->pti->tci.win);
2039 
2040    return pTdgtBase->pti;
2041 }
2042 
2043 /* --------------------- DestroyTdgtColorWheelDialogBox --------------------- */
2044 
DestroyTdgtColorWheelDialogBox(dpy,pti)2045 void DestroyTdgtColorWheelDialogBox(dpy, pti)
2046    Display *dpy;
2047    TidgetInfo *pti;
2048 {
2049    ColorWheelDlgInfo *pcwdi=NULL;
2050 
2051    if (pti == NULL) return;
2052 
2053    pcwdi = (ColorWheelDlgInfo*)(pti->userdata);
2054    if (pcwdi != NULL) {
2055       free(pcwdi);
2056    }
2057    DestroyTidget(&pti);
2058 }
2059 
2060 /* --------------------- TdgtColorWheelDlgLoop --------------------- */
2061 
2062 static
HandleTdgtNotifyEvent(pTdgtBase,pTdgtNtfy)2063 int HandleTdgtNotifyEvent(pTdgtBase, pTdgtNtfy)
2064    TdgtBase *pTdgtBase;
2065    TdgtNtfy *pTdgtNtfy;
2066 {
2067    switch (pTdgtNtfy->ctl_id) {
2068    case ID_CWHEEL_BTN_ADD: SetColorInAppl(pTdgtBase); break;
2069    case ID_CWHEEL_BTN_OK:
2070       if (SetColorInAppl(pTdgtBase)) {
2071          HideTdgtColorWheelDialogBox();
2072       }
2073       break;
2074    case ID_CWHEEL_BTN_CANCEL: HideTdgtColorWheelDialogBox(); break;
2075    }
2076    return FALSE;
2077 }
2078 
TdgtColorWheelDlgLoop(pti)2079 int TdgtColorWheelDlgLoop(pti)
2080    TidgetInfo *pti;
2081 {
2082    TdgtBase *pTdgtBase=(TdgtBase*)(pti->tidget);
2083    int which_btn_clicked=INVALID, first_time_config=TRUE;
2084 
2085    if (pTdgtBase == NULL) return which_btn_clicked;
2086 
2087    while (gColorWheelInfo.mapped) {
2088       XEvent xev;
2089       TdgtNtfy tdgt_notify;
2090       TidgetInfo *handling_pti=NULL;
2091 
2092       XNextEvent(mainDisplay, &xev);
2093 
2094       if (xev.type == MotionNotify) {
2095          /* do nothing */
2096       } else if (xev.type == ConfigureNotify &&
2097             xev.xany.window == pti->tci.win) {
2098          if (first_time_config) {
2099             first_time_config = FALSE;
2100             if (warpToWinCenter) {
2101                /* does not work because the window has been moved */
2102                XWarpPointer(mainDisplay, None, pti->tci.win, 0, 0, 0, 0,
2103                      (pti->tci.win_info.w>>1), (pti->tci.win_info.h>>1));
2104             }
2105          }
2106       } else if (xev.type == Expose) {
2107          ExposeEventHandler(&xev, FALSE);
2108       } else if (xev.type == ConfigureNotify &&
2109             xev.xany.window==mainWindow) {
2110          Reconfigure(FALSE);
2111       } else if (xev.type == KeyPress) {
2112          TidgetManagerHandleAllKeyPressEvent(&xev);
2113       } else if (IsTdgtNotifyEvent(pti, &xev, &tdgt_notify)) {
2114          HandleTdgtNotifyEvent(pTdgtBase, &tdgt_notify);
2115       } else if (IsTidgetEvent(pti, &xev, &handling_pti)) {
2116          TidgetEventHandler(pti, &xev, handling_pti);
2117       } else {
2118          /* anything else? */
2119       }
2120    }
2121    return which_btn_clicked;
2122 }
2123 
2124 /* --------------------- Init & Clean Up --------------------- */
2125 
InitTdgtColorWheelDlg()2126 int InitTdgtColorWheelDlg()
2127 {
2128    return TRUE;
2129 }
2130 
CleanUpTdgtColorWheelDlg()2131 void CleanUpTdgtColorWheelDlg()
2132 {
2133 }
2134 
2135