1 /* Radio Widget for XEmacs.
2 Copyright (C) 1999 Edward A. Falk
4 This file is part of XEmacs.
6 XEmacs is free software; you can redistribute it and/or modify it
7 under the terms of the GNU General Public License as published by the
8 Free Software Foundation; either version 2, or (at your option) any
11 XEmacs is distributed in the hope that it will be useful, but WITHOUT
12 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 You should have received a copy of the GNU General Public License
17 along with XEmacs; see the file COPYING. If not, write to
18 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA. */
21 /* Synched up with: Radio.c 1.1 */
24 * Radio.c - Radio button widget
26 * Author: Edward A. Falk
27 * falk@falconer.vip.best.com
32 * Overview: This widget is identical to the Toggle widget in behavior,
33 * but completely different in appearance. This widget looks like a small
34 * diamond-shaped button with a label to the right.
36 * To make this work, we subclass the Toggle widget to inherit its behavior
37 * and to inherit the label-drawing function from which Toggle is
38 * subclassed. We then completely replace the Expose, Set, Unset
39 * and Highlight member functions.
41 * The Set and Unset actions are slightly unorthodox. In Toggle's
42 * ClassInit function, Toggle searches the Command actions list and
43 * "steals" the Set and Unset functions, caching pointers to them in its
44 * class record. It then calls these functions from its own ToggleSet
47 * We, in turn, override the Set() and Unset() actions in our own ClassRec.
54 #include <X11/IntrinsicP.h>
55 #include <X11/StringDefs.h>
56 #include ATHENA_XawInit_h_
57 #include "../src/xmu.h"
58 #include "xlwradioP.h"
62 #define rclass(w) ((RadioWidgetClass)((w)->core.widget_class))
66 #define swid(rw) ((rw)->threeD.shadow_width)
68 #define swid(rw) ((rw)->core.border_width)
71 #define bsize(rw) (rclass(rw)->radio_class.dsize)
72 #define bs(rw) (bsize(rw) + 2*swid(rw))
76 /****************************************************************
78 * Full class record constant
80 ****************************************************************/
82 /* The translations table from Toggle do not need to be
87 /* Member functions */
89 static void RadioInit (Widget, Widget, ArgList, Cardinal *);
90 static void RadioExpose (Widget, XEvent *, Region);
91 static void RadioResize (Widget);
92 static void RadioDestroy (Widget, XtPointer, XtPointer);
93 static void RadioClassInit (void);
94 static void RadioClassPartInit (WidgetClass);
95 static Boolean RadioSetValues (Widget, Widget, Widget, ArgList, Cardinal *);
96 static void DrawDiamond (Widget);
97 static XtGeometryResult RadioQueryGeometry (Widget, XtWidgetGeometry *,
102 static void RadioHighlight (Widget, XEvent *, String *, Cardinal *);
103 static void RadioUnhighlight (Widget, XEvent *, String *, Cardinal *);
105 /* internal privates */
107 static void RadioSize (RadioWidget, Dimension *, Dimension *);
109 /* The actions table from Toggle is almost perfect, but we need
110 * to override Highlight, Set, and Unset.
113 static XtActionsRec actionsList[] =
115 {"highlight", RadioHighlight},
116 {"unhighlight", RadioUnhighlight},
119 #define SuperClass ((ToggleWidgetClass)&toggleClassRec)
121 RadioClassRec radioClassRec = {
123 (WidgetClass) SuperClass, /* superclass */
124 "Radio", /* class_name */
125 sizeof(RadioRec), /* size */
126 RadioClassInit, /* class_initialize */
127 RadioClassPartInit, /* class_part_initialize */
128 FALSE, /* class_inited */
129 RadioInit, /* initialize */
130 NULL, /* initialize_hook */
131 XtInheritRealize, /* realize */
132 actionsList, /* actions */
133 XtNumber(actionsList), /* num_actions */
134 NULL, /* resources */
135 0, /* resource_count */
136 NULLQUARK, /* xrm_class */
137 TRUE, /* compress_motion */
138 TRUE, /* compress_exposure */
139 TRUE, /* compress_enterleave */
140 FALSE, /* visible_interest */
142 RadioResize, /* resize */
143 RadioExpose, /* expose */
144 RadioSetValues, /* set_values */
145 NULL, /* set_values_hook */
146 XtInheritSetValuesAlmost, /* set_values_almost */
147 NULL, /* get_values_hook */
148 NULL, /* accept_focus */
149 XtVersion, /* version */
150 NULL, /* callback_private */
151 XtInheritTranslations, /* tm_table */
152 RadioQueryGeometry, /* query_geometry */
153 XtInheritDisplayAccelerator, /* display_accelerator */
155 }, /* CoreClass fields initialization */
157 XtInheritChangeSensitive /* change_sensitive */
158 }, /* SimpleClass fields initialization */
161 XtInheritXaw3dShadowDraw /* field not used */
162 }, /* ThreeDClass fields initialization */
165 0 /* field not used */
166 }, /* LabelClass fields initialization */
168 0 /* field not used */
169 }, /* CommandClass fields initialization */
171 RadioSet, /* Set Procedure. */
172 RadioUnset, /* Unset Procedure. */
173 NULL /* extension. */
174 }, /* ToggleClass fields initialization */
177 DrawDiamond, /* draw procedure */
178 NULL /* extension. */
179 } /* RadioClass fields initialization */
182 /* for public consumption */
183 WidgetClass radioWidgetClass = (WidgetClass) &radioClassRec;
190 /****************************************************************
194 ****************************************************************/
197 RadioClassInit (void)
199 XawInitializeWidgetSet();
203 RadioClassPartInit (WidgetClass class)
205 RadioWidgetClass c = (RadioWidgetClass) class ;
206 RadioWidgetClass super = (RadioWidgetClass)c->core_class.superclass ;
208 if( c->radio_class.drawDiamond == NULL ||
209 c->radio_class.drawDiamond == XtInheritDrawDiamond )
211 c->radio_class.drawDiamond = super->radio_class.drawDiamond ;
220 RadioInit (Widget request,
225 RadioWidget rw = (RadioWidget) new;
226 RadioWidget rw_req = (RadioWidget) request;
229 /* Select initial size for the widget */
230 if( rw_req->core.width == 0 || rw_req->core.height == 0 )
232 RadioSize(rw, &w,&h) ;
233 if( rw_req->core.width == 0 )
235 if( rw_req->core.height == 0 )
236 rw->core.height = h ;
237 rw->core.widget_class->core_class.resize(new) ;
241 /* Function Name: RadioDestroy
242 * Description: Destroy Callback for radio widget.
243 * Arguments: w - the radio widget that is being destroyed.
244 * junk, garbage - not used.
250 RadioDestroy (Widget w,
254 /* TODO: get rid of this */
258 /* React to size change from manager. Label widget will compute some internal
259 * stuff, but we need to override. This code requires knowledge of the
260 * internals of the Label widget.
264 RadioResize (Widget w)
266 RadioWidget rw = (RadioWidget)w ;
268 /* call parent resize proc */
269 SuperClass->core_class.resize(w) ;
271 /* override label offset */
273 switch( rw->label.justify ) {
275 rw->label.label_x += (bs(rw) + rw->label.internal_width) ;
279 case XtJustifyCenter:
281 rw->label.label_x += (bs(rw) + rw->label.internal_width)/2;
288 * Repaint the widget window.
292 RadioExpose (Widget w,
296 RadioWidget rw = (RadioWidget) w ;
297 Display *dpy = XtDisplay(w) ;
298 Window win = XtWindow(w) ;
301 extern WidgetClass labelWidgetClass ;
303 /* Note: the Label widget examines the region to decide if anything
304 * needs to be drawn. I'm not sure that this is worth the effort,
305 * but it bears thinking on.
308 /* Command widget may sometimes override the label GC in order
309 * to draw inverse video. We don't use inverse video, so we need
310 * to restore the label's normal GC.
312 rw->label.normal_GC = rw->command.normal_GC ;
315 /* Let label widget draw the label. If there was an lbm_x
316 * field, we could let Label draw the bitmap too. But there
317 * isn't, so we need to temporarily remove the bitmap and
318 * draw it ourself later.
320 left_bitmap = rw->label.left_bitmap ;
321 rw->label.left_bitmap = None ;
322 labelWidgetClass->core_class.expose(w,event,region) ;
323 rw->label.left_bitmap = left_bitmap ;
325 /* now manually draw the left bitmap. TODO: 3-d look, xaw-xpm */
326 gc = XtIsSensitive(w) ? rw->label.normal_GC : rw->label.gray_GC ;
327 if( left_bitmap != None && rw->label.lbm_width > 0 )
329 /* TODO: handle pixmaps */
330 XCopyPlane(dpy, left_bitmap, win, gc,
331 0,0, rw->label.lbm_width, rw->label.lbm_height,
332 (int) rw->label.internal_width*2 + bs(rw),
333 (int) rw->label.internal_height + rw->label.lbm_y,
337 /* Finally, the button itself */
338 ((RadioWidgetClass)(w->core.widget_class))->radio_class.drawDiamond(w) ;
344 /************************************************************
346 * Set specified arguments into widget
348 ***********************************************************/
353 RadioSetValues (Widget current,
359 RadioWidget oldrw = (RadioWidget) current;
360 RadioWidget newrw = (RadioWidget) new;
362 /* Need to find out if the size of the widget changed. Set new size
363 * if it did and resize is permitted. One way to determine of the
364 * widget changed size would be to scan the args list. Another way
365 * is to compare the old and new widgets and see if any of several
366 * size-related fields have been changed. The Label widget chose the
367 * former method, but I choose the latter.
370 if( newrw->label.resize &&
371 ( newrw->core.width != oldrw->core.width ||
372 newrw->core.height != oldrw->core.height ||
373 newrw->core.border_width != oldrw->core.border_width ) )
375 RadioSize(newrw, &newrw->core.width, &newrw->core.height) ;
378 /* The label set values routine can resize the widget. We need to
379 * recalculate if this is true.
381 if (newrw->label.label_x != oldrw->label.label_x)
388 static XtGeometryResult
389 RadioQueryGeometry (Widget w,
390 XtWidgetGeometry *intended,
391 XtWidgetGeometry *preferred)
393 RadioWidget rw = (RadioWidget) w;
395 preferred->request_mode = CWWidth | CWHeight;
396 RadioSize(rw, &preferred->width, &preferred->height) ;
398 if ( ((intended->request_mode & (CWWidth | CWHeight))
399 == (CWWidth | CWHeight)) &&
400 intended->width == preferred->width &&
401 intended->height == preferred->height)
402 return XtGeometryYes;
403 else if (preferred->width == w->core.width &&
404 preferred->height == w->core.height)
407 return XtGeometryAlmost;
414 /************************************************************
418 ************************************************************/
421 * Draw the highlight border around the widget. The Command widget
422 * did this by drawing through a mask. We do it by just drawing the
427 DrawHighlight (Widget w,
430 RadioWidget rw = (RadioWidget)w;
431 XRectangle rects[4] ;
432 Dimension ht = rw->command.highlight_thickness ;
435 ht > rw->core.width/2 ||
436 ht > rw->core.height/2 )
439 if( ! XtIsRealized(w) )
442 rects[0].x = 0 ; rects[0].y = 0 ;
443 rects[0].width = rw->core.width ; rects[0].height = ht ;
444 rects[1].x = 0 ; rects[1].y = rw->core.height - ht ;
445 rects[1].width = rw->core.width ; rects[1].height = ht ;
446 rects[2].x = 0 ; rects[2].y = ht ;
447 rects[2].width = ht ; rects[2].height = rw->core.height - ht*2 ;
448 rects[3].x = rw->core.width - ht ; rects[3].y = ht ;
449 rects[3].width = ht ; rects[3].height = rw->core.height - ht*2 ;
450 XFillRectangles( XtDisplay(w), XtWindow(w), gc, rects, 4) ;
454 RadioHighlight (Widget w,
457 Cardinal *num_params)
459 RadioWidget rw = (RadioWidget)w;
460 DrawHighlight(w, rw->command.normal_GC) ;
465 RadioUnhighlight (Widget w,
468 Cardinal *num_params)
470 RadioWidget rw = (RadioWidget)w;
471 DrawHighlight(w, rw->command.inverse_GC) ;
479 String *params, /* unused */
480 Cardinal *num_params) /* unused */
482 RadioWidget rw = (RadioWidget)w;
483 RadioWidgetClass class = (RadioWidgetClass) w->core.widget_class ;
485 if( rw->command.set )
488 rw->command.set = TRUE ;
489 if( XtIsRealized(w) )
490 class->radio_class.drawDiamond(w) ;
496 RadioUnset (Widget w,
498 String *params, /* unused */
499 Cardinal *num_params) /* unused */
501 RadioWidget rw = (RadioWidget)w;
502 RadioWidgetClass class = (RadioWidgetClass) w->core.widget_class ;
504 if( ! rw->command.set )
507 rw->command.set = FALSE ;
508 if( XtIsRealized(w) )
509 class->radio_class.drawDiamond(w) ;
515 /************************************************************
517 * Internal Procedures
519 ************************************************************/
522 /* Size of widget. Width is size of box plus width of border around
523 * box plus width of label plus three margins plus the size of the left
524 * bitmap, if any. Height is max(box,bitmap,label) plus two margins.
528 RadioSize (RadioWidget rw,
532 *w = rw->label.label_width + bs(rw) + LEFT_OFFSET(rw) +
533 3 * rw->label.internal_width ;
534 *h = Max( rw->label.label_height, bs(rw) ) +
535 2 * rw->label.internal_width ;
540 DrawDiamond (Widget w)
542 RadioWidget rw = (RadioWidget) w ;
543 Display *dpy = XtDisplay(w) ;
544 Window win = XtWindow(w) ;
548 Dimension del = bsize(rw)/2 ;
549 Position x,y ; /* diamond center */
552 Dimension s = swid(rw) ;
555 gc = XtIsSensitive(w) ? rw->command.normal_GC : rw->label.gray_GC ;
557 gci = rw->command.set ? rw->command.normal_GC : rw->command.inverse_GC ;
559 x = rw->label.internal_width + bs(rw)/2 ;
560 y = rw->core.height/2 ;
563 if( ! rw->command.set ) {
564 top = rw->threeD.top_shadow_GC ;
565 bot = rw->threeD.bot_shadow_GC ;
566 ctr = gc ; /* TODO */
568 top = rw->threeD.bot_shadow_GC ;
569 bot = rw->threeD.top_shadow_GC ;
570 ctr = gc ; /* TODO */
583 XFillPolygon(dpy,win,gci, pts,4, Convex, CoordModeOrigin) ;
587 XDrawLine(dpy,win,bot, x-i-del,y, x,y+del+i) ;
588 XDrawLine(dpy,win,bot, x+del+i,y, x,y+del+i) ;
591 XDrawLine(dpy,win,top, x-del-i,y, x,y-del-i) ;
592 XDrawLine(dpy,win,top, x+del+i,y, x,y-del-i) ;
595 XDrawLines(dpy,win,gc, pts,5, CoordModeOrigin) ;