Debian Codegraph @

texk/xdvik/gui/xlwradio.c

raw
1 /* adapted from xlwradio.c in the XEmacs distribution.
2 Changes are Copyright (C) 2002-2004 the xdvik development team.
3
4 Note SU: This widget doesn't work with Xaw3d, and I have little
5 inclination to make it work - the design would need to be utterly
6 different, e.g. the radio would need to be a diamond instead of a
7 circle, since 3d-shadowed circles don't look circular; colormap
8 issues would need to be settled, etc. And besides, I don't like Xaw3d ;)
9
10 So instead, the caller should just do:
11
12 myWidget = XtVaCreateManagedWidget("foo",
13 #ifdef XAW
14 radioWidgetClass,
15 #else
16 toggleWidgetClass,
17 #endif
18 ... arglist ...);
19
20 Feel free to submit patches if you want this implemented.
21
22 Original copyright follows:
23 */
24
25 /* Radio Widget for XEmacs.
26 Copyright (C) 1999 Edward A. Falk
27
28 This file is part of XEmacs.
29
30 XEmacs is free software; you can redistribute it and/or modify it
31 under the terms of the GNU General Public License as published by the
32 Free Software Foundation; either version 2, or (at your option) any
33 later version.
34
35 XEmacs is distributed in the hope that it will be useful, but WITHOUT
36 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
37 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
38 for more details.
39
40 You should have received a copy of the GNU General Public License
41 along with XEmacs; see the file COPYING. If not, write to
42 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
43 Boston, MA 02111-1307, USA. */
44
45 /* Synched up with: Radio.c 1.1 */
46
47 /*
48 * Radio.c - Radio button widget
49 *
50 * Author: Edward A. Falk
51 * [email protected]
52 *
53 * Date: June 30, 1997
54 *
55 *
56 * Overview: This widget is identical to the Toggle widget in behavior,
57 * but completely different in appearance. This widget looks like a small
58 * diamond-shaped button with a label to the right.
59 *
60 * To make this work, we subclass the Toggle widget to inherit its behavior
61 * and to inherit the label-drawing function from which Toggle is
62 * subclassed. We then completely replace the Expose, Set, Unset
63 * and Highlight member functions.
64 *
65 * The Set and Unset actions are slightly unorthodox. In Toggle's
66 * ClassInit function, Toggle searches the Command actions list and
67 * "steals" the Set and Unset functions, caching pointers to them in its
68 * class record. It then calls these functions from its own ToggleSet
69 * and Toggle actions.
70 *
71 * We, in turn, override the Set() and Unset() actions in our own ClassRec.
72 */
73
74 #include "xdvi-config.h"
75 #include "xdvi.h"
76 #include "xdvi-debug.h"
77 #include "util.h"
78 #include "xaw_bitmaps.h" /* bitmaps for radio/checkbuttons */
79
80 #include "xlwradioP.h"
81
82 #ifndef MOTIF /* entire file */
83
84 #include <stdio.h>
85
86 #include <X11/IntrinsicP.h>
87 #include <X11/StringDefs.h>
88 #include <X11/Intrinsic.h>
89 #include <X11/StringDefs.h>
90 #include <X11/Xos.h>
91 #include <X11/Xaw/Text.h>
92 #include <X11/Xaw/AsciiText.h>
93 /* #include "../src/xmu.h" */
94
95 #define BOX_SIZE 16
96 #define PIXMAP_OFFSET 2 /* additional space between pixmap and label */
97
98 #ifndef UNUSED
99 # define UNUSED(x) ((void)(x))
100 #endif
101
102 #ifndef MAX
103 # define MAX(i, j) ( (i) > (j) ? (i) : (j) )
104 #endif
105
106
107 #define rclass(w) ((RadioWidgetClass)((w)->core.widget_class))
108
109
110 #ifdef _ThreeDP_h
111 #define swid(rw) ((rw)->threeD.shadow_width)
112 #else
113 #define swid(rw) ((rw)->core.border_width)
114 #endif
115
116 #define bsize(rw) (rclass(rw)->radio_class.dsize)
117 #define bs(rw) (bsize(rw) + PIXMAP_OFFSET + 2*swid(rw))
118
119 /****************************************************************
120 *
121 * Full class record constant
122 *
123 ****************************************************************/
124
125 /* The translations table from Toggle do not need to be
126 * overridden by Radio
127 */
128
129
130 /* Member functions */
131
132 static void RadioInit (Widget, Widget, ArgList, Cardinal *);
133 static void RadioExpose (Widget, XEvent *, Region);
134 static void RadioResize (Widget);
135 static void RadioDestroy (Widget);
136 static void RadioClassInit (void);
137 static void RadioClassPartInit (WidgetClass);
138 static Boolean RadioSetValues (Widget, Widget, Widget, ArgList, Cardinal *);
139 static void DrawDiamond (Widget);
140 static XtGeometryResult RadioQueryGeometry (Widget, XtWidgetGeometry *,
141 XtWidgetGeometry *);
142
143 /* Action procs */
144
145 static void RadioHighlight (Widget, XEvent *, String *, Cardinal *);
146 static void RadioUnhighlight (Widget, XEvent *, String *, Cardinal *);
147
148 /* internal privates */
149
150 static void RadioSize (RadioWidget, Dimension *, Dimension *);
151
152 /* The actions table from Toggle is almost perfect, but we need
153 * to override Highlight, Set, and Unset.
154 */
155
156 static XtActionsRec actionsList[] =
157 {
158 {"highlight", RadioHighlight},
159 {"unhighlight", RadioUnhighlight},
160 };
161
162 #define offset(field) XtOffset(RadioWidget, radio.field)
163
164 static XtResource resources[] = {
165 {XtNisRadio, XtCIsRadio, XtRBoolean, sizeof(Boolean),
166 offset(isRadio), XtRImmediate, (XtPointer)(ptrdiff_t)True },
167 };
168 #undef offset
169
170 #define SuperClass ((ToggleWidgetClass)&toggleClassRec)
171
172 RadioClassRec radioClassRec = {
173 {
174 (WidgetClass) SuperClass, /* superclass */
175 "Radio", /* class_name */
176 sizeof(RadioRec), /* size */
177 RadioClassInit, /* class_initialize */
178 RadioClassPartInit, /* class_part_initialize */
179 FALSE, /* class_inited */
180 RadioInit, /* initialize */
181 NULL, /* initialize_hook */
182 XtInheritRealize, /* realize */
183 actionsList, /* actions */
184 XtNumber(actionsList), /* num_actions */
185 resources, /* resources */
186 XtNumber(resources), /* resource_count */
187 NULLQUARK, /* xrm_class */
188 TRUE, /* compress_motion */
189 TRUE, /* compress_exposure */
190 TRUE, /* compress_enterleave */
191 FALSE, /* visible_interest */
192 RadioDestroy, /* destroy */
193 RadioResize, /* resize */
194 RadioExpose, /* expose */
195 RadioSetValues, /* set_values */
196 NULL, /* set_values_hook */
197 XtInheritSetValuesAlmost, /* set_values_almost */
198 NULL, /* get_values_hook */
199 NULL, /* accept_focus */
200 XtVersion, /* version */
201 NULL, /* callback_private */
202 XtInheritTranslations, /* tm_table */
203 RadioQueryGeometry, /* query_geometry */
204 XtInheritDisplayAccelerator, /* display_accelerator */
205 NULL /* extension */
206 }, /* CoreClass fields initialization */
207 {
208 XtInheritChangeSensitive /* change_sensitive */
209 #ifndef HAVE_OLD_XAW
210 , NULL
211 #endif
212 }, /* SimpleClass fields initialization */
213 #ifdef _ThreeDP_h
214 {
215 XtInheritXaw3dShadowDraw /* field not used */
216 }, /* ThreeDClass fields initialization */
217 #endif
218 {
219 0 /* field not used */
220 }, /* LabelClass fields initialization */
221 {
222 0 /* field not used */
223 }, /* CommandClass fields initialization */
224 {
225 RadioSet, /* Set Procedure. */
226 RadioUnset, /* Unset Procedure. */
227 NULL /* extension. */
228 }, /* ToggleClass fields initialization */
229 {
230 BOX_SIZE,
231 DrawDiamond, /* draw procedure */
232 None, /* selected radiobutton */
233 None, /* unselected radiobutton */
234 None, /* selected menubutton */
235 None, /* unselected menubutton */
236 NULL /* extension. */
237 } /* RadioClass fields initialization */
238 };
239
240 /* for public consumption */
241 WidgetClass radioWidgetClass = (WidgetClass) &radioClassRec;
242
243
244
245
246
247
248 /****************************************************************
249 *
250 * Class Methods
251 *
252 ****************************************************************/
253
254 static void
255 RadioClassInit (void)
256 {
257 XawInitializeWidgetSet();
258 }
259
260 static void
261 RadioClassPartInit (WidgetClass class)
262 {
263 RadioWidgetClass c = (RadioWidgetClass) class;
264 RadioWidgetClass super = (RadioWidgetClass)c->core_class.superclass;
265
266 if( c->radio_class.drawDiamond == NULL ||
267 c->radio_class.drawDiamond == XtInheritDrawDiamond )
268 {
269 c->radio_class.drawDiamond = super->radio_class.drawDiamond;
270 }
271
272 }
273
274
275
276
277 static void
278 RadioInit (Widget request,
279 Widget new,
280 ArgList args,
281 Cardinal *num_args)
282 {
283 RadioWidget rw = (RadioWidget) new;
284 RadioWidget rw_req = (RadioWidget) request;
285 Dimension w,h;
286
287 UNUSED(args);
288 UNUSED(num_args);
289
290 /* FIXME: should pixmap initialization be here?? */
291
292 /* Select initial size for the widget */
293 if( rw_req->core.width == 0 || rw_req->core.height == 0 ) {
294 RadioSize(rw, &w,&h);
295 if( rw_req->core.width == 0 )
296 rw->core.width = w;
297 if( rw_req->core.height == 0 )
298 rw->core.height = h;
299 rw->core.widget_class->core_class.resize(new);
300 }
301 /*
302 FIXME: access to XtWindow(rw) fails in the init method, so
303 I moved the bitmap creation here -- is there a better way??
304 */
305 /* create pixmaps */
306 rclass(rw)->radio_class.sel_radio
307 = XCreateBitmapFromData(XtDisplay(rw), RootWindowOfScreen(XtScreen(rw)),
308 (char *)button_radio_on_bits, BUTTON_BITMAP_W, BUTTON_BITMAP_H);
309 rclass(rw)->radio_class.unsel_radio
310 = XCreateBitmapFromData(XtDisplay(rw), RootWindowOfScreen(XtScreen(rw)),
311 (char *)button_radio_off_bits, BUTTON_BITMAP_W, BUTTON_BITMAP_H);
312 rclass(rw)->radio_class.sel_menu
313 = XCreateBitmapFromData(XtDisplay(rw), RootWindowOfScreen(XtScreen(rw)),
314 (char *)button_check_on_bits, BUTTON_BITMAP_W, BUTTON_BITMAP_H);
315 rclass(rw)->radio_class.unsel_menu
316 = XCreateBitmapFromData(XtDisplay(rw), RootWindowOfScreen(XtScreen(rw)),
317 (char *)button_check_off_bits, BUTTON_BITMAP_W, BUTTON_BITMAP_H);
318 }
319
320 /* Function Name: RadioDestroy
321 * Description: Destroy Callback for radio widget.
322 * Arguments: w - the radio widget that is being destroyed.
323 * Returns: none.
324 */
325
326 static void
327 RadioDestroy (Widget w)
328 {
329 RadioWidget rw = (RadioWidget)w;
330 /* de-allocate bitmaps */
331 XFreePixmap(XtDisplay(w), rclass(rw)->radio_class.sel_radio);
332 XFreePixmap(XtDisplay(w), rclass(rw)->radio_class.unsel_radio);
333 XFreePixmap(XtDisplay(w), rclass(rw)->radio_class.sel_menu);
334 XFreePixmap(XtDisplay(w), rclass(rw)->radio_class.unsel_menu);
335 }
336
337
338 /* React to size change from manager. Label widget will compute some internal
339 * stuff, but we need to override. This code requires knowledge of the
340 * internals of the Label widget.
341 */
342
343 static void
344 RadioResize (Widget w)
345 {
346 RadioWidget rw = (RadioWidget)w;
347
348 /* call parent resize proc */
349 SuperClass->core_class.resize(w);
350
351 /* override label offset */
352
353 switch( rw->label.justify ) {
354 case XtJustifyLeft:
355 rw->label.label_x += (bs(rw) + rw->label.internal_width);
356 break;
357 case XtJustifyRight:
358 break;
359 case XtJustifyCenter:
360 default:
361 rw->label.label_x += (bs(rw) + rw->label.internal_width)/2;
362 break;
363 }
364 }
365
366
367 /*
368 * Repaint the widget window.
369 */
370
371 static void
372 RadioExpose (Widget w,
373 XEvent *event,
374 Region region)
375 {
376 RadioWidget rw = (RadioWidget) w;
377 Display *dpy = XtDisplay(w);
378 Window win = XtWindow(w);
379 GC gc;
380 Pixmap left_bitmap;
381 extern WidgetClass labelWidgetClass;
382
383 /* Note: the Label widget examines the region to decide if anything
384 * needs to be drawn. I'm not sure that this is worth the effort,
385 * but it bears thinking on.
386 */
387
388 /* Command widget may sometimes override the label GC in order
389 * to draw inverse video. We don't use inverse video, so we need
390 * to restore the label's normal GC.
391 */
392 rw->label.normal_GC = rw->command.normal_GC;
393
394
395 /* Let label widget draw the label. If there was an lbm_x
396 * field, we could let Label draw the bitmap too. But there
397 * isn't, so we need to temporarily remove the bitmap and
398 * draw it ourself later.
399 */
400 left_bitmap = rw->label.left_bitmap;
401 rw->label.left_bitmap = None;
402 labelWidgetClass->core_class.expose(w,event,region);
403 rw->label.left_bitmap = left_bitmap;
404
405 /* now manually draw the left bitmap. TODO: 3-d look, xaw-xpm */
406 gc = XtIsSensitive(w) ? rw->label.normal_GC : rw->label.gray_GC;
407 if( left_bitmap != None && rw->label.lbm_width > 0 )
408 {
409 /* TODO: handle pixmaps */
410 XCopyPlane(dpy, left_bitmap, win, gc,
411 0,0, rw->label.lbm_width, rw->label.lbm_height,
412 (int) rw->label.internal_width*2 + bs(rw),
413 (int) rw->label.internal_height + rw->label.lbm_y,
414 1UL);
415 }
416 DrawDiamond(w);
417 /* Finally, the button itself */
418 ((RadioWidgetClass)(w->core.widget_class))->radio_class.drawDiamond(w);
419 }
420
421
422
423
424 /************************************************************
425 *
426 * Set specified arguments into widget
427 *
428 ***********************************************************/
429
430
431 /* ARGSUSED */
432 static Boolean
433 RadioSetValues (Widget current,
434 Widget request,
435 Widget new,
436 ArgList args,
437 Cardinal *num_args)
438 {
439 RadioWidget oldrw = (RadioWidget) current;
440 RadioWidget newrw = (RadioWidget) new;
441
442 UNUSED(request);
443 UNUSED(args);
444 UNUSED(num_args);
445
446 /* Need to find out if the size of the widget changed. Set new size
447 * if it did and resize is permitted. One way to determine of the
448 * widget changed size would be to scan the args list. Another way
449 * is to compare the old and new widgets and see if any of several
450 * size-related fields have been changed. The Label widget chose the
451 * former method, but I choose the latter.
452 */
453
454 if( newrw->label.resize &&
455 ( newrw->core.width != oldrw->core.width ||
456 newrw->core.height != oldrw->core.height ||
457 newrw->core.border_width != oldrw->core.border_width ) )
458 {
459 RadioSize(newrw, &newrw->core.width, &newrw->core.height);
460 }
461
462 /* The label set values routine can resize the widget. We need to
463 * recalculate if this is true.
464 */
465 if (newrw->label.label_x != oldrw->label.label_x)
466 {
467 RadioResize (new);
468 }
469 return FALSE;
470 }
471
472 static XtGeometryResult
473 RadioQueryGeometry (Widget w,
474 XtWidgetGeometry *intended,
475 XtWidgetGeometry *preferred)
476 {
477 RadioWidget rw = (RadioWidget) w;
478
479 preferred->request_mode = CWWidth | CWHeight;
480 RadioSize(rw, &preferred->width, &preferred->height);
481
482 if ( ((intended->request_mode & (CWWidth | CWHeight))
483 == (CWWidth | CWHeight)) &&
484 intended->width == preferred->width &&
485 intended->height == preferred->height)
486 return XtGeometryYes;
487 else if (preferred->width == w->core.width &&
488 preferred->height == w->core.height)
489 return XtGeometryNo;
490 else
491 return XtGeometryAlmost;
492 }
493
494
495
496
497
498 /************************************************************
499 *
500 * Action Procedures
501 *
502 ************************************************************/
503
504 /*
505 * Draw the highlight border around the widget. The Command widget
506 * did this by drawing through a mask. We do it by just drawing the
507 * border.
508 */
509
510 static void
511 DrawHighlight (Widget w,
512 GC gc)
513 {
514 RadioWidget rw = (RadioWidget)w;
515 XRectangle rects[4];
516 Dimension ht = rw->command.highlight_thickness;
517
518 if( ht <= 0 ||
519 ht > rw->core.width/2 ||
520 ht > rw->core.height/2 )
521 return;
522
523 if( ! XtIsRealized(w) )
524 return;
525
526 rects[0].x = 0; rects[0].y = 0;
527 rects[0].width = rw->core.width; rects[0].height = ht;
528 rects[1].x = 0; rects[1].y = rw->core.height - ht;
529 rects[1].width = rw->core.width; rects[1].height = ht;
530 rects[2].x = 0; rects[2].y = ht;
531 rects[2].width = ht; rects[2].height = rw->core.height - ht*2;
532 rects[3].x = rw->core.width - ht; rects[3].y = ht;
533 rects[3].width = ht; rects[3].height = rw->core.height - ht*2;
534 XFillRectangles( XtDisplay(w), XtWindow(w), gc, rects, 4);
535 }
536
537 static void
538 RadioHighlight (Widget w,
539 XEvent *event,
540 String *params,
541 Cardinal *num_params)
542 {
543 RadioWidget rw = (RadioWidget)w;
544 UNUSED(event);
545 UNUSED(params);
546 UNUSED(num_params);
547
548 DrawHighlight(w, rw->command.normal_GC);
549 }
550
551
552 static void
553 RadioUnhighlight (Widget w,
554 XEvent *event,
555 String *params,
556 Cardinal *num_params)
557 {
558 RadioWidget rw = (RadioWidget)w;
559 UNUSED(event);
560 UNUSED(params);
561 UNUSED(num_params);
562
563 DrawHighlight(w, rw->command.inverse_GC);
564 }
565
566
567 void
568 RadioSet (Widget w,
569 XEvent *event,
570 String *params,
571 Cardinal *num_params)
572 {
573 RadioWidget rw = (RadioWidget)w;
574 RadioWidgetClass class = (RadioWidgetClass) w->core.widget_class;
575
576 UNUSED(event);
577 UNUSED(params);
578 UNUSED(num_params);
579
580 if( rw->command.set )
581 return;
582
583 rw->command.set = TRUE;
584 if( XtIsRealized(w) )
585 class->radio_class.drawDiamond(w);
586 }
587
588
589 void
590 RadioUnset (Widget w,
591 XEvent *event,
592 String *params,
593 Cardinal *num_params)
594 {
595 RadioWidget rw = (RadioWidget)w;
596 RadioWidgetClass class = (RadioWidgetClass) w->core.widget_class;
597
598 UNUSED(event);
599 UNUSED(params);
600 UNUSED(num_params);
601
602 if( ! rw->command.set )
603 return;
604
605 rw->command.set = FALSE;
606 if( XtIsRealized(w) )
607 class->radio_class.drawDiamond(w);
608 }
609
610
611
612
613 /************************************************************
614 *
615 * Internal Procedures
616 *
617 ************************************************************/
618
619
620 /* Size of widget. Width is size of box plus width of border around
621 * box plus width of label plus three margins plus the size of the left
622 * bitmap, if any. Height is max(box,bitmap,label) plus two margins.
623 */
624
625 static void
626 RadioSize (RadioWidget rw,
627 Dimension *w,
628 Dimension *h)
629 {
630 *w = rw->label.label_width + bs(rw) + LEFT_OFFSET(rw) +
631 3 * rw->label.internal_width;
632 *h = MAX( rw->label.label_height, bs(rw) ) +
633 2 * rw->label.internal_width;
634 }
635
636
637 static void
638 DrawDiamond(Widget w)
639 {
640 RadioWidget rw = (RadioWidget) w;
641 Display *dpy = XtDisplay(w);
642 Window win = XtWindow(w);
643
644 Position x = rw->label.internal_width;
645 Position y = rw->core.height/2;
646 GC gc = XtIsSensitive(w) ? rw->command.normal_GC : rw->label.gray_GC;
647 Pixmap selected, unselected;
648
649 if (rw->radio.isRadio) { /* it's a radio button */
650 selected = rclass(rw)->radio_class.sel_radio;
651 unselected = rclass(rw)->radio_class.unsel_radio;
652 }
653 else {
654 selected = rclass(rw)->radio_class.sel_menu;
655 unselected = rclass(rw)->radio_class.unsel_menu;
656 }
657
658 ASSERT(selected != None, "");
659 ASSERT(unselected != None, "");
660
661 if(rw->command.set) {
662 XCopyPlane(dpy, selected, win, gc,
663 0, 0, BUTTON_BITMAP_H, BUTTON_BITMAP_W,
664 x, y - BUTTON_BITMAP_H / 2, 1L);
665 }
666 else {
667 XCopyPlane(dpy, unselected, win, gc,
668 0, 0, BUTTON_BITMAP_H, BUTTON_BITMAP_W,
669 x, y - BUTTON_BITMAP_H / 2, 1L);
670 }
671 }
672
673
674 #else
675 /* silence `empty compilation unit' warnings */
676 static void bar(void); static void foo(void) { bar(); } static void bar(void) { foo(); }
677 #endif /* MOTIF */