Debian Codegraph @

src/Command.c

raw
1 /***********************************************************
2
3 Copyright 1987, 1988, 1994, 1998 The Open Group
4
5 Permission to use, copy, modify, distribute, and sell this software and its
6 documentation for any purpose is hereby granted without fee, provided that
7 the above copyright notice appear in all copies and that both that
8 copyright notice and this permission notice appear in supporting
9 documentation.
10
11 The above copyright notice and this permission notice shall be included in
12 all copies or substantial portions of the Software.
13
14 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 OPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
18 AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
19 CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
20
21 Except as contained in this notice, the name of The Open Group shall not be
22 used in advertising or otherwise to promote the sale, use or other dealings
23 in this Software without prior written authorization from The Open Group.
24
25
26 Copyright 1987, 1988 by Digital Equipment Corporation, Maynard, Massachusetts.
27
28 All Rights Reserved
29
30 Permission to use, copy, modify, and distribute this software and its
31 documentation for any purpose and without fee is hereby granted,
32 provided that the above copyright notice appear in all copies and that
33 both that copyright notice and this permission notice appear in
34 supporting documentation, and that the name of Digital not be
35 used in advertising or publicity pertaining to distribution of the
36 software without specific, written prior permission.
37
38 DIGITAL DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
39 ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL
40 DIGITAL BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR
41 ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
42 WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
43 ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
44 SOFTWARE.
45
46 ******************************************************************/
47
48 /*
49 * Command.c - Command button widget
50 */
51
52 #ifdef HAVE_CONFIG_H
53 #include <config.h>
54 #endif
55 #include <stdio.h>
56 #include <X11/IntrinsicP.h>
57 #include <X11/StringDefs.h>
58 #include <X11/extensions/shape.h>
59 #include <X11/Xmu/Converters.h>
60 #include <X11/Xmu/Drawing.h>
61 #include <X11/Xmu/Misc.h>
62 #include <X11/Xaw/CommandP.h>
63 #include <X11/Xaw/XawInit.h>
64 #include "Private.h"
65
66 #define DEFAULT_HIGHLIGHT_THICKNESS 2
67 #define DEFAULT_SHAPE_HIGHLIGHT 32767
68 #define STR_EQUAL(str1, str2) (str1 == str2 || strcmp(str1, str2) == 0)
69
70 /*
71 * Class Methods
72 */
73 static void XawCommandClassInitialize(void);
74 static void XawCommandDestroy(Widget);
75 static void XawCommandInitialize(Widget, Widget, ArgList, Cardinal*);
76 static void XawCommandRealize(Widget, Mask*, XSetWindowAttributes*);
77 static void XawCommandResize(Widget);
78 static void XawCommandRedisplay(Widget, XEvent*, Region);
79 static Boolean XawCommandSetValues(Widget, Widget, Widget, ArgList, Cardinal*);
80 static void XawCommandGetValuesHook(Widget, ArgList, Cardinal*);
81 static Bool ChangeSensitive(Widget);
82
83 /*
84 * Prototypes
85 */
86 static GC Get_GC(CommandWidget, Pixel, Pixel);
87 static void PaintCommandWidget(Widget, XEvent*, Region, Bool);
88 static Region HighlightRegion(CommandWidget);
89 static Bool ShapeButton(CommandWidget, Bool);
90 static void XawCommandToggle(Widget);
91
92 /*
93 * Actions
94 */
95 static void Highlight(Widget, XEvent*, String*, Cardinal*);
96 static void Notify(Widget, XEvent*, String*, Cardinal*);
97 static void Reset(Widget, XEvent*, String*, Cardinal*);
98 static void Set(Widget, XEvent*, String*, Cardinal*);
99 static void Unhighlight(Widget, XEvent*, String*, Cardinal*);
100 static void Unset(Widget, XEvent*, String*, Cardinal*);
101
102 /*
103 * Initialization
104 */
105 static char defaultTranslations[] =
106 "<Enter>:" "highlight()\n"
107 "<Leave>:" "reset()\n"
108 "<Btn1Down>:" "set()\n"
109 "<Btn1Up>:" "notify() unset()\n"
110 ;
111
112 #define offset(field) XtOffsetOf(CommandRec, field)
113 static XtResource resources[] = {
114 {
115 XtNcallback,
116 XtCCallback,
117 XtRCallback,
118 sizeof(XtPointer),
119 offset(command.callbacks),
120 XtRCallback,
121 NULL
122 },
123 {
124 XtNhighlightThickness,
125 XtCThickness,
126 XtRDimension,
127 sizeof(Dimension),
128 offset(command.highlight_thickness),
129 XtRImmediate,
130 (XtPointer)DEFAULT_SHAPE_HIGHLIGHT
131 },
132 {
133 XtNshapeStyle,
134 XtCShapeStyle,
135 XtRShapeStyle,
136 sizeof(int),
137 offset(command.shape_style),
138 XtRImmediate,
139 (XtPointer)XawShapeRectangle
140 },
141 {
142 XtNcornerRoundPercent,
143 XtCCornerRoundPercent,
144 XtRDimension,
145 sizeof(Dimension),
146 offset(command.corner_round),
147 XtRImmediate,
148 (XtPointer)25
149 },
150 };
151 #undef offset
152
153 static XtActionsRec actionsList[] = {
154 {"set", Set},
155 {"notify", Notify},
156 {"highlight", Highlight},
157 {"reset", Reset},
158 {"unset", Unset},
159 {"unhighlight", Unhighlight}
160 };
161
162 #define SuperClass ((LabelWidgetClass)&labelClassRec)
163
164 CommandClassRec commandClassRec = {
165 /* core */
166 {
167 (WidgetClass)SuperClass, /* superclass */
168 "Command", /* class_name */
169 sizeof(CommandRec), /* size */
170 XawCommandClassInitialize, /* class_initialize */
171 NULL, /* class_part_initialize */
172 False, /* class_inited */
173 XawCommandInitialize, /* initialize */
174 NULL, /* initialize_hook */
175 XawCommandRealize, /* realize */
176 actionsList, /* actions */
177 XtNumber(actionsList), /* num_actions */
178 resources, /* resources */
179 XtNumber(resources), /* num_resources */
180 NULLQUARK, /* xrm_class */
181 False, /* compress_motion */
182 True, /* compress_exposure */
183 True, /* compress_enterleave */
184 False, /* visible_interest */
185 XawCommandDestroy, /* destroy */
186 XawCommandResize, /* resize */
187 XawCommandRedisplay, /* expose */
188 XawCommandSetValues, /* set_values */
189 NULL, /* set_values_hook */
190 XtInheritSetValuesAlmost, /* set_values_almost */
191 XawCommandGetValuesHook, /* get_values_hook */
192 NULL, /* accept_focus */
193 XtVersion, /* version */
194 NULL, /* callback_private */
195 defaultTranslations, /* tm_table */
196 XtInheritQueryGeometry, /* query_geometry */
197 XtInheritDisplayAccelerator, /* display_accelerator */
198 NULL, /* extension */
199 },
200 /* simple */
201 {
202 ChangeSensitive, /* change_sensitive */
203 #ifndef OLDXAW
204 NULL,
205 #endif
206 },
207 /* label */
208 {
209 NULL, /* not used */
210 },
211 /* command */
212 {
213 NULL, /* not used */
214 },
215 };
216
217 WidgetClass commandWidgetClass = (WidgetClass)&commandClassRec;
218
219 /*
220 * Implementation
221 */
222 static GC
223 Get_GC(CommandWidget cbw, Pixel fg, Pixel bg)
224 {
225 XGCValues values = {
226 .foreground = fg,
227 .background = bg,
228 .font = cbw->label.font->fid,
229 .cap_style = CapProjecting,
230 .line_width = 0
231 };
232
233 if (cbw->command.highlight_thickness > 1)
234 values.line_width = cbw->command.highlight_thickness;
235
236 if (cbw->simple.international == True)
237 return (XtAllocateGC((Widget)cbw, 0,
238 GCForeground | GCBackground | GCLineWidth |
239 GCCapStyle, &values, GCFont, 0));
240 else
241 return (XtGetGC((Widget)cbw,
242 GCForeground | GCBackground | GCFont | GCLineWidth |
243 GCCapStyle, &values));
244 }
245
246 /*ARGSUSED*/
247 static void
248 XawCommandInitialize(Widget request _X_UNUSED, Widget cnew,
249 ArgList args _X_UNUSED, Cardinal *num_args _X_UNUSED)
250 {
251 CommandWidget cbw = (CommandWidget)cnew;
252 int shape_event_base, shape_error_base;
253
254 if (!cbw->label.font) XtError("Aborting: no font found\n");
255
256 if (cbw->command.shape_style != XawShapeRectangle &&
257 !XShapeQueryExtension(XtDisplay(cnew), &shape_event_base,
258 &shape_error_base))
259 cbw->command.shape_style = XawShapeRectangle;
260
261 if (cbw->command.highlight_thickness == DEFAULT_SHAPE_HIGHLIGHT) {
262 if (cbw->command.shape_style != XawShapeRectangle)
263 cbw->command.highlight_thickness = 0;
264 else
265 cbw->command.highlight_thickness = DEFAULT_HIGHLIGHT_THICKNESS;
266 }
267
268 cbw->command.normal_GC = Get_GC(cbw, cbw->label.foreground,
269 cbw->core.background_pixel);
270 cbw->command.inverse_GC = Get_GC(cbw, cbw->core.background_pixel,
271 cbw->label.foreground);
272 XtReleaseGC(cnew, cbw->label.normal_GC);
273 cbw->label.normal_GC = cbw->command.normal_GC;
274
275 cbw->command.set = False;
276 cbw->command.highlighted = HighlightNone;
277 }
278
279 static Region
280 HighlightRegion(CommandWidget cbw)
281 {
282 static Region outerRegion = NULL, innerRegion, emptyRegion;
283 XRectangle rect;
284
285 if (cbw->command.highlight_thickness == 0 ||
286 cbw->command.highlight_thickness > Min(XtWidth(cbw), XtHeight(cbw)) / 2)
287 return (NULL);
288
289 if (outerRegion == NULL) {
290 /* save time by allocating scratch regions only once. */
291 outerRegion = XCreateRegion();
292 innerRegion = XCreateRegion();
293 emptyRegion = XCreateRegion();
294 }
295
296 rect.x = rect.y = 0;
297 rect.width = XtWidth(cbw);
298 rect.height = XtHeight(cbw);
299 XUnionRectWithRegion(&rect, emptyRegion, outerRegion);
300 rect.x = rect.y = (short)cbw->command.highlight_thickness;
301 rect.width = (unsigned short)(rect.width - cbw->command.highlight_thickness * 2);
302 rect.height = (unsigned short)(rect.height - cbw->command.highlight_thickness * 2);
303 XUnionRectWithRegion(&rect, emptyRegion, innerRegion);
304 XSubtractRegion(outerRegion, innerRegion, outerRegion);
305
306 return (outerRegion);
307 }
308
309 /***************************
310 * Action Procedures
311 ***************************/
312 static void
313 XawCommandToggle(Widget w)
314 {
315 CommandWidget xaw = (CommandWidget)w;
316 Arg args[2];
317 Cardinal num_args;
318
319 num_args = 0;
320 XtSetArg(args[num_args], XtNbackground,
321 xaw->label.foreground); ++num_args;
322 XtSetArg(args[num_args], XtNforeground,
323 xaw->core.background_pixel); ++num_args;
324 XtSetValues(w, args, num_args);
325 }
326
327 /*ARGSUSED*/
328 static void
329 Set(Widget w, XEvent *event _X_UNUSED, String *params _X_UNUSED, Cardinal *num_params _X_UNUSED)
330 {
331 CommandWidget cbw = (CommandWidget)w;
332
333 if (cbw->command.set)
334 return;
335
336 XawCommandToggle(w);
337 cbw->command.set= True;
338 }
339
340 /*ARGSUSED*/
341 static void
342 Unset(Widget w, XEvent *event _X_UNUSED, String *params _X_UNUSED, Cardinal *num_params _X_UNUSED)
343 {
344 CommandWidget cbw = (CommandWidget)w;
345
346 if (!cbw->command.set)
347 return;
348
349 cbw->command.set = False;
350 XawCommandToggle(w);
351 }
352
353 /*ARGSUSED*/
354 static void
355 Reset(Widget w, XEvent *event, String *params, Cardinal *num_params)
356 {
357 CommandWidget cbw = (CommandWidget)w;
358
359 if (cbw->command.set) {
360 cbw->command.highlighted = HighlightNone;
361 Unset(w, event, params, num_params);
362 }
363 else
364 Unhighlight(w, event, params, num_params);
365 }
366
367 /*ARGSUSED*/
368 static void
369 Highlight(Widget w, XEvent *event, String *params, Cardinal *num_params)
370 {
371 CommandWidget cbw = (CommandWidget)w;
372
373 if (*num_params == (Cardinal)0)
374 cbw->command.highlighted = HighlightWhenUnset;
375 else {
376 if (*num_params != (Cardinal)1)
377 XtWarning("Too many parameters passed to highlight action table.");
378 switch (params[0][0]) {
379 case 'A':
380 case 'a':
381 cbw->command.highlighted = HighlightAlways;
382 break;
383 default:
384 cbw->command.highlighted = HighlightWhenUnset;
385 break;
386 }
387 }
388
389 if (XtIsRealized(w))
390 PaintCommandWidget(w, event, HighlightRegion(cbw), True);
391 }
392
393 /*ARGSUSED*/
394 static void
395 Unhighlight(Widget w, XEvent *event, String *params _X_UNUSED, Cardinal *num_params _X_UNUSED)
396 {
397 CommandWidget cbw = (CommandWidget)w;
398
399 cbw->command.highlighted = HighlightNone;
400 if (XtIsRealized(w))
401 PaintCommandWidget(w, event, HighlightRegion(cbw), True);
402 }
403
404 /*ARGSUSED*/
405 static void
406 Notify(Widget w, XEvent *event _X_UNUSED, String *params _X_UNUSED, Cardinal *num_params _X_UNUSED)
407 {
408 CommandWidget cbw = (CommandWidget)w;
409
410 /* check to be sure state is still Set so that user can cancel
411 the action (e.g. by moving outside the window, in the default
412 bindings.
413 */
414 if (cbw->command.set)
415 XtCallCallbackList(w, cbw->command.callbacks, (XtPointer) NULL);
416 }
417
418 static void
419 XawCommandRedisplay(Widget w, XEvent *event, Region region)
420 {
421 PaintCommandWidget(w, event, region, False);
422 }
423
424 /*
425 * Function:
426 * PaintCommandWidget
427 * Parameters:
428 * w - command widget
429 * region - region to paint (passed to the superclass)
430 * change - did it change either set or highlight state?
431 */
432 static void
433 PaintCommandWidget(Widget w, XEvent *event, Region region, Bool change)
434 {
435 CommandWidget cbw = (CommandWidget)w;
436 Bool very_thick;
437 GC rev_gc;
438
439 very_thick = cbw->command.highlight_thickness
440 > Min(XtWidth(cbw), XtHeight(cbw)) / 2;
441
442 if (cbw->command.highlight_thickness == 0) {
443 (*SuperClass->core_class.expose) (w, event, region);
444 return;
445 }
446
447 /*
448 * If we are set then use the same colors as if we are not highlighted
449 */
450
451 if (cbw->command.highlighted != HighlightNone) {
452 rev_gc = cbw->command.normal_GC;
453 }
454 else {
455 rev_gc = cbw->command.inverse_GC;
456 }
457
458 if (!((!change && cbw->command.highlighted == HighlightNone)
459 || (cbw->command.highlighted == HighlightWhenUnset
460 && cbw->command.set))) {
461 if (very_thick)
462 XFillRectangle(XtDisplay(w),XtWindow(w), rev_gc,
463 0, 0, XtWidth(cbw), XtHeight(cbw));
464 else {
465 /* wide lines are centered on the path, so indent it */
466 if (cbw->core.background_pixmap != XtUnspecifiedPixmap &&
467 rev_gc == cbw->command.inverse_GC) {
468 XClearArea(XtDisplay(w), XtWindow(w),
469 0, 0, XtWidth(cbw), cbw->command.highlight_thickness,
470 False);
471 XClearArea(XtDisplay(w), XtWindow(w),
472 0, cbw->command.highlight_thickness,
473 cbw->command.highlight_thickness,
474 (unsigned)(XtHeight(cbw) - (cbw->command.highlight_thickness<<1)),
475 False);
476 XClearArea(XtDisplay(w), XtWindow(w),
477 XtWidth(cbw) - cbw->command.highlight_thickness,
478 cbw->command.highlight_thickness,
479 cbw->command.highlight_thickness,
480 (unsigned)(XtHeight(cbw) - (cbw->command.highlight_thickness<<1)),
481 False);
482 XClearArea(XtDisplay(w), XtWindow(w),
483 0, XtHeight(cbw) - cbw->command.highlight_thickness,
484 XtWidth(cbw), cbw->command.highlight_thickness,
485 False);
486 }
487 else {
488 int offset = cbw->command.highlight_thickness / 2;
489
490 XDrawRectangle(XtDisplay(w),XtWindow(w), rev_gc, offset, offset,
491 (unsigned)(XtWidth(cbw) - cbw->command.highlight_thickness),
492 (unsigned)(XtHeight(cbw) - cbw->command.highlight_thickness));
493 }
494 }
495 }
496
497 (*SuperClass->core_class.expose)(w, event, region);
498 }
499
500 static void
501 XawCommandDestroy(Widget w)
502 {
503 CommandWidget cbw = (CommandWidget)w;
504
505 /* Label will release cbw->command.normal_GC */
506 XtReleaseGC(w, cbw->command.inverse_GC);
507 }
508
509 /*ARGSUSED*/
510 static Boolean
511 XawCommandSetValues(Widget current, Widget request _X_UNUSED, Widget cnew,
512 ArgList args, Cardinal *num_args)
513 {
514 CommandWidget oldcbw = (CommandWidget)current;
515 CommandWidget cbw = (CommandWidget)cnew;
516 Boolean redisplay = False;
517
518 if (oldcbw->core.sensitive != cbw->core.sensitive && !cbw->core.sensitive) {
519 cbw->command.highlighted = HighlightNone;
520 redisplay = True;
521 }
522
523 if (cbw->command.set) {
524 unsigned int i;
525 Pixel foreground, background;
526
527 foreground = oldcbw->label.foreground;
528 background = oldcbw->core.background_pixel;
529 for (i = 0; i < *num_args; i++) {
530 if (STR_EQUAL(args[i].name, XtNforeground))
531 background = cbw->label.foreground;
532 else if (STR_EQUAL(args[i].name, XtNbackground))
533 foreground = cbw->core.background_pixel;
534 }
535 cbw->label.foreground = foreground;
536 cbw->core.background_pixel = background;
537 }
538
539 if (oldcbw->label.foreground != cbw->label.foreground
540 || oldcbw->core.background_pixel != cbw->core.background_pixel
541 || oldcbw->command.highlight_thickness
542 != cbw->command.highlight_thickness
543 || oldcbw->label.font != cbw->label.font) {
544 XtReleaseGC(cnew, cbw->command.inverse_GC);
545
546 cbw->command.normal_GC = Get_GC(cbw, cbw->label.foreground,
547 cbw->core.background_pixel);
548 cbw->command.inverse_GC = Get_GC(cbw, cbw->core.background_pixel,
549 cbw->label.foreground);
550 XtReleaseGC(cnew, cbw->label.normal_GC);
551 cbw->label.normal_GC = cbw->command.normal_GC;
552
553 redisplay = True;
554 }
555
556 if (XtIsRealized(cnew)
557 && oldcbw->command.shape_style != cbw->command.shape_style
558 && !ShapeButton(cbw, True))
559 cbw->command.shape_style = oldcbw->command.shape_style;
560
561 return (redisplay);
562 }
563
564 static void
565 XawCommandGetValuesHook(Widget w, ArgList args, Cardinal *num_args)
566 {
567 CommandWidget cbw = (CommandWidget)w;
568 Cardinal i;
569
570 for (i = 0; i < *num_args; i++) {
571 if (STR_EQUAL(args[i].name, XtNforeground))
572 *((String*)args[i].value) = cbw->command.set ?
573 (String)cbw->core.background_pixel : (String)cbw->label.foreground;
574 else if (STR_EQUAL(args[i].name, XtNbackground))
575 *((String*)args[i].value) = cbw->command.set ?
576 (String)cbw->label.foreground : (String)cbw->core.background_pixel;
577 }
578 }
579
580 static void
581 XawCommandClassInitialize(void)
582 {
583 XawInitializeWidgetSet();
584 XtSetTypeConverter(XtRString, XtRShapeStyle, XmuCvtStringToShapeStyle,
585 NULL, 0, XtCacheNone, NULL);
586 XtSetTypeConverter(XtRShapeStyle, XtRString, XmuCvtShapeStyleToString,
587 NULL, 0, XtCacheNone, NULL);
588 }
589
590 static Bool
591 ShapeButton(CommandWidget cbw, Bool checkRectangular)
592 {
593 Dimension corner_size = 0;
594
595 if (cbw->command.shape_style == XawShapeRoundedRectangle) {
596 corner_size = XtWidth(cbw) < XtHeight(cbw) ?
597 XtWidth(cbw) : XtHeight(cbw);
598 corner_size = (Dimension)((corner_size * cbw->command.corner_round) / 100);
599 }
600
601 if (checkRectangular || cbw->command.shape_style != XawShapeRectangle) {
602 if (!XmuReshapeWidget((Widget)cbw, cbw->command.shape_style,
603 corner_size, corner_size)) {
604 cbw->command.shape_style = XawShapeRectangle;
605 return (False);
606 }
607 }
608
609 return (True);
610 }
611
612 static void
613 XawCommandRealize(Widget w, Mask *valueMask, XSetWindowAttributes *attributes)
614 {
615 (*commandWidgetClass->core_class.superclass->core_class.realize)
616 (w, valueMask, attributes);
617
618 ShapeButton((CommandWidget)w, False);
619 }
620
621 static void
622 XawCommandResize(Widget w)
623 {
624 if (XtIsRealized(w))
625 ShapeButton((CommandWidget)w, False);
626
627 (*commandWidgetClass->core_class.superclass->core_class.resize)(w);
628 }
629
630 static Bool
631 ChangeSensitive(Widget w)
632 {
633 CommandWidget cbw = (CommandWidget)w;
634
635 if (XtIsRealized(w)) {
636 if (XtIsSensitive(w)) {
637 if (w->core.border_pixmap != XtUnspecifiedPixmap)
638 XSetWindowBorderPixmap(XtDisplay(w), XtWindow(w),
639 w->core.border_pixmap);
640 else
641 XSetWindowBorder(XtDisplay(w), XtWindow(w),
642 w->core.border_pixel);
643 }
644 else {
645 if (cbw->simple.insensitive_border == None)
646 cbw->simple.insensitive_border =
647 XmuCreateStippledPixmap(XtScreen(w),
648 w->core.border_pixel,
649 cbw->command.set ?
650 cbw->label.foreground :
651 w->core.background_pixel,
652 w->core.depth);
653 XSetWindowBorderPixmap(XtDisplay(w), XtWindow(w),
654 cbw->simple.insensitive_border);
655 }
656 }
657
658 return (False);
659 }