("latin-1-prefix"): Change ~s to give \e,A'\e(B and
[emacs.git] / lwlib / xlwmenu.c
bloba5687d9de50477e4d90301e2bd0c4d948383efbb
1 /* Implements a lightweight menubar widget.
2 Copyright (C) 1992 Lucid, Inc.
4 This file is part of the Lucid Widget Library.
6 The Lucid Widget Library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
9 any later version.
11 The Lucid Widget Library is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU Emacs; see the file COPYING. If not, write to the
18 Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA. */
21 /* Created by devin@lucid.com */
23 #ifdef HAVE_CONFIG_H
24 #include <config.h>
25 #endif
27 #include <stdio.h>
29 #include <sys/types.h>
30 #include <X11/Xos.h>
31 #include <X11/IntrinsicP.h>
32 #include <X11/ObjectP.h>
33 #include <X11/StringDefs.h>
34 #include <X11/cursorfont.h>
35 #include "xlwmenuP.h"
37 #ifdef emacs
39 /* Defined in xfns.c. When config.h defines `static' as empty, we get
40 redefinition errors when gray_bitmap is included more than once, so
41 we're referring to the one include in xfns.c here. */
43 extern int gray_bitmap_width;
44 extern int gray_bitmap_height;
45 extern unsigned char *gray_bitmap_bits;
47 /* Defined in xterm.c. */
48 extern int x_alloc_nearest_color_for_widget __P ((Widget, Colormap, XColor*));
49 extern int x_catch_errors __P ((Display*));
50 extern int x_uncatch_errors __P ((Display*, int));
51 extern int x_had_errors_p __P ((Display*));
52 extern int x_clear_errors __P ((Display*));
53 extern unsigned long x_copy_dpy_color __P ((Display *, Colormap,
54 unsigned long));
56 /* Defined in xfaces.c. */
57 extern void x_free_dpy_colors __P ((Display *, Screen *, Colormap,
58 unsigned long *pixels, int npixels));
59 #else /* not emacs */
61 #include <X11/bitmaps/gray>
62 #define gray_bitmap_width gray_width
63 #define gray_bitmap_height gray_height
64 #define gray_bitmap_bits gray_bits
66 #endif /* not emacs */
68 static int pointer_grabbed;
69 static XEvent menu_post_event;
71 XFontStruct *xlwmenu_default_font;
73 static char
74 xlwMenuTranslations [] =
75 "<BtnDown>: start()\n\
76 <Motion>: drag()\n\
77 <BtnUp>: select()\n\
78 <Key>Shift_L: nothing()\n\
79 <Key>Shift_R: nothing()\n\
80 <Key>Meta_L: nothing()\n\
81 <Key>Meta_R: nothing()\n\
82 <Key>Control_L: nothing()\n\
83 <Key>Control_R: nothing()\n\
84 <Key>Hyper_L: nothing()\n\
85 <Key>Hyper_R: nothing()\n\
86 <Key>Super_L: nothing()\n\
87 <Key>Super_R: nothing()\n\
88 <Key>Alt_L: nothing()\n\
89 <Key>Alt_R: nothing()\n\
90 <Key>Caps_Lock: nothing()\n\
91 <Key>Shift_Lock: nothing()\n\
92 <KeyUp>Shift_L: nothing()\n\
93 <KeyUp>Shift_R: nothing()\n\
94 <KeyUp>Meta_L: nothing()\n\
95 <KeyUp>Meta_R: nothing()\n\
96 <KeyUp>Control_L: nothing()\n\
97 <KeyUp>Control_R: nothing()\n\
98 <KeyUp>Hyper_L: nothing()\n\
99 <KeyUp>Hyper_R: nothing()\n\
100 <KeyUp>Super_L: nothing()\n\
101 <KeyUp>Super_R: nothing()\n\
102 <KeyUp>Alt_L: nothing()\n\
103 <KeyUp>Alt_R: nothing()\n\
104 <KeyUp>Caps_Lock: nothing()\n\
105 <KeyUp>Shift_Lock:nothing()\n\
106 <Key>: key()\n\
107 <KeyUp>: key()\n\
110 #define offset(field) XtOffset(XlwMenuWidget, field)
111 static XtResource
112 xlwMenuResources[] =
114 {XtNfont, XtCFont, XtRFontStruct, sizeof(XFontStruct *),
115 offset(menu.font),XtRString, "XtDefaultFont"},
116 {XtNforeground, XtCForeground, XtRPixel, sizeof(Pixel),
117 offset(menu.foreground), XtRString, "XtDefaultForeground"},
118 {XtNbuttonForeground, XtCButtonForeground, XtRPixel, sizeof(Pixel),
119 offset(menu.button_foreground), XtRString, "XtDefaultForeground"},
120 {XtNmargin, XtCMargin, XtRDimension, sizeof(Dimension),
121 offset(menu.margin), XtRImmediate, (XtPointer) 4},
122 {XtNhorizontalSpacing, XtCMargin, XtRDimension, sizeof(Dimension),
123 offset(menu.horizontal_spacing), XtRImmediate, (XtPointer)3},
124 {XtNverticalSpacing, XtCMargin, XtRDimension, sizeof(Dimension),
125 offset(menu.vertical_spacing), XtRImmediate, (XtPointer)1},
126 {XtNarrowSpacing, XtCMargin, XtRDimension, sizeof(Dimension),
127 offset(menu.arrow_spacing), XtRImmediate, (XtPointer)10},
129 {XmNshadowThickness, XmCShadowThickness, XtRDimension,
130 sizeof (Dimension), offset (menu.shadow_thickness),
131 XtRImmediate, (XtPointer) 2},
132 {XmNtopShadowColor, XmCTopShadowColor, XtRPixel, sizeof (Pixel),
133 offset (menu.top_shadow_color), XtRImmediate, (XtPointer)-1},
134 {XmNbottomShadowColor, XmCBottomShadowColor, XtRPixel, sizeof (Pixel),
135 offset (menu.bottom_shadow_color), XtRImmediate, (XtPointer)-1},
136 {XmNtopShadowPixmap, XmCTopShadowPixmap, XtRPixmap, sizeof (Pixmap),
137 offset (menu.top_shadow_pixmap), XtRImmediate, (XtPointer)None},
138 {XmNbottomShadowPixmap, XmCBottomShadowPixmap, XtRPixmap, sizeof (Pixmap),
139 offset (menu.bottom_shadow_pixmap), XtRImmediate, (XtPointer)None},
141 {XtNopen, XtCCallback, XtRCallback, sizeof(XtPointer),
142 offset(menu.open), XtRCallback, (XtPointer)NULL},
143 {XtNselect, XtCCallback, XtRCallback, sizeof(XtPointer),
144 offset(menu.select), XtRCallback, (XtPointer)NULL},
145 {XtNhighlightCallback, XtCCallback, XtRCallback, sizeof(XtPointer),
146 offset(menu.highlight), XtRCallback, (XtPointer)NULL},
147 {XtNmenu, XtCMenu, XtRPointer, sizeof(XtPointer),
148 offset(menu.contents), XtRImmediate, (XtPointer)NULL},
149 {XtNcursor, XtCCursor, XtRCursor, sizeof(Cursor),
150 offset(menu.cursor_shape), XtRString, (XtPointer)"right_ptr"},
151 {XtNhorizontal, XtCHorizontal, XtRInt, sizeof(int),
152 offset(menu.horizontal), XtRImmediate, (XtPointer)True},
154 #undef offset
156 static Boolean XlwMenuSetValues();
157 static void XlwMenuRealize();
158 static void XlwMenuRedisplay();
159 static void XlwMenuResize();
160 static void XlwMenuInitialize();
161 static void XlwMenuRedisplay();
162 static void XlwMenuDestroy();
163 static void XlwMenuClassInitialize();
164 static void Start();
165 static void Drag();
166 static void Select();
167 static void Key();
168 static void Nothing();
169 static int separator_height ();
171 static XtActionsRec
172 xlwMenuActionsList [] =
174 {"start", Start},
175 {"drag", Drag},
176 {"select", Select},
177 {"key", Key},
178 {"nothing", Nothing},
181 #define SuperClass ((CoreWidgetClass)&coreClassRec)
183 XlwMenuClassRec xlwMenuClassRec =
185 { /* CoreClass fields initialization */
186 (WidgetClass) SuperClass, /* superclass */
187 "XlwMenu", /* class_name */
188 sizeof(XlwMenuRec), /* size */
189 XlwMenuClassInitialize, /* class_initialize */
190 NULL, /* class_part_initialize */
191 FALSE, /* class_inited */
192 XlwMenuInitialize, /* initialize */
193 NULL, /* initialize_hook */
194 XlwMenuRealize, /* realize */
195 xlwMenuActionsList, /* actions */
196 XtNumber(xlwMenuActionsList), /* num_actions */
197 xlwMenuResources, /* resources */
198 XtNumber(xlwMenuResources), /* resource_count */
199 NULLQUARK, /* xrm_class */
200 TRUE, /* compress_motion */
201 TRUE, /* compress_exposure */
202 TRUE, /* compress_enterleave */
203 FALSE, /* visible_interest */
204 XlwMenuDestroy, /* destroy */
205 XlwMenuResize, /* resize */
206 XlwMenuRedisplay, /* expose */
207 XlwMenuSetValues, /* set_values */
208 NULL, /* set_values_hook */
209 XtInheritSetValuesAlmost, /* set_values_almost */
210 NULL, /* get_values_hook */
211 NULL, /* accept_focus */
212 XtVersion, /* version */
213 NULL, /* callback_private */
214 xlwMenuTranslations, /* tm_table */
215 XtInheritQueryGeometry, /* query_geometry */
216 XtInheritDisplayAccelerator, /* display_accelerator */
217 NULL /* extension */
218 }, /* XlwMenuClass fields initialization */
220 0 /* dummy */
224 WidgetClass xlwMenuWidgetClass = (WidgetClass) &xlwMenuClassRec;
226 int submenu_destroyed;
228 static int next_release_must_exit;
230 \f/* Utilities */
233 /* Like abort, but remove grabs from widget W before. */
235 static void
236 abort_gracefully (w)
237 Widget w;
239 if (XtIsShell (XtParent (w)))
240 XtRemoveGrab (w);
241 XtUngrabPointer (w, CurrentTime);
242 abort ();
245 static void
246 push_new_stack (mw, val)
247 XlwMenuWidget mw;
248 widget_value* val;
250 if (!mw->menu.new_stack)
252 mw->menu.new_stack_length = 10;
253 mw->menu.new_stack =
254 (widget_value**)XtCalloc (mw->menu.new_stack_length,
255 sizeof (widget_value*));
257 else if (mw->menu.new_depth == mw->menu.new_stack_length)
259 mw->menu.new_stack_length *= 2;
260 mw->menu.new_stack =
261 (widget_value**)XtRealloc ((char*)mw->menu.new_stack,
262 mw->menu.new_stack_length * sizeof (widget_value*));
264 mw->menu.new_stack [mw->menu.new_depth++] = val;
267 static void
268 pop_new_stack_if_no_contents (mw)
269 XlwMenuWidget mw;
271 if (mw->menu.new_depth)
273 if (!mw->menu.new_stack [mw->menu.new_depth - 1]->contents)
274 mw->menu.new_depth -= 1;
278 static void
279 make_old_stack_space (mw, n)
280 XlwMenuWidget mw;
281 int n;
283 if (!mw->menu.old_stack)
285 mw->menu.old_stack_length = 10;
286 mw->menu.old_stack =
287 (widget_value**)XtCalloc (mw->menu.old_stack_length,
288 sizeof (widget_value*));
290 else if (mw->menu.old_stack_length < n)
292 mw->menu.old_stack_length *= 2;
293 mw->menu.old_stack =
294 (widget_value**)XtRealloc ((char*)mw->menu.old_stack,
295 mw->menu.old_stack_length * sizeof (widget_value*));
299 \f/* Size code */
301 string_width (mw, s)
302 XlwMenuWidget mw;
303 char *s;
305 XCharStruct xcs;
306 int drop;
308 XTextExtents (mw->menu.font, s, strlen (s), &drop, &drop, &drop, &xcs);
309 return xcs.width;
312 static int
313 arrow_width (mw)
314 XlwMenuWidget mw;
316 return (mw->menu.font->ascent * 3/4) | 1;
319 /* Return the width of toggle buttons of widget MW. */
321 static int
322 toggle_button_width (mw)
323 XlwMenuWidget mw;
325 return ((mw->menu.font->ascent + mw->menu.font->descent) * 2 / 3) | 1;
329 /* Return the width of radio buttons of widget MW. */
331 static int
332 radio_button_width (mw)
333 XlwMenuWidget mw;
335 return toggle_button_width (mw) * 1.41;
339 static XtResource
340 nameResource[] =
342 {"labelString", "LabelString", XtRString, sizeof(String),
343 0, XtRImmediate, 0},
346 static char*
347 resource_widget_value (mw, val)
348 XlwMenuWidget mw;
349 widget_value *val;
351 if (!val->toolkit_data)
353 char* resourced_name = NULL;
354 char* complete_name;
355 XtGetSubresources ((Widget) mw,
356 (XtPointer) &resourced_name,
357 val->name, val->name,
358 nameResource, 1, NULL, 0);
359 if (!resourced_name)
360 resourced_name = val->name;
361 if (!val->value)
363 complete_name = (char *) XtMalloc (strlen (resourced_name) + 1);
364 strcpy (complete_name, resourced_name);
366 else
368 int complete_length =
369 strlen (resourced_name) + strlen (val->value) + 2;
370 complete_name = XtMalloc (complete_length);
371 *complete_name = 0;
372 strcat (complete_name, resourced_name);
373 strcat (complete_name, " ");
374 strcat (complete_name, val->value);
377 val->toolkit_data = complete_name;
378 val->free_toolkit_data = True;
380 return (char*)val->toolkit_data;
383 /* Returns the sizes of an item */
384 static void
385 size_menu_item (mw, val, horizontal_p, label_width, rest_width, button_width,
386 height)
387 XlwMenuWidget mw;
388 widget_value* val;
389 int horizontal_p;
390 int* label_width;
391 int* rest_width;
392 int* button_width;
393 int* height;
395 enum menu_separator separator;
397 if (lw_separator_p (val->name, &separator, 0))
399 *height = separator_height (separator);
400 *label_width = 1;
401 *rest_width = 0;
402 *button_width = 0;
404 else
406 *height =
407 mw->menu.font->ascent + mw->menu.font->descent
408 + 2 * mw->menu.vertical_spacing + 2 * mw->menu.shadow_thickness;
410 *label_width =
411 string_width (mw, resource_widget_value (mw, val))
412 + mw->menu.horizontal_spacing + mw->menu.shadow_thickness;
414 *rest_width = mw->menu.horizontal_spacing + mw->menu.shadow_thickness;
415 if (!horizontal_p)
417 if (val->contents)
418 /* Add width of the arrow displayed for submenus. */
419 *rest_width += arrow_width (mw) + mw->menu.arrow_spacing;
420 else if (val->key)
421 /* Add width of key equivalent string. */
422 *rest_width += (string_width (mw, val->key)
423 + mw->menu.arrow_spacing);
425 if (val->button_type == BUTTON_TYPE_TOGGLE)
426 *button_width = (toggle_button_width (mw)
427 + mw->menu.horizontal_spacing);
428 else if (val->button_type == BUTTON_TYPE_RADIO)
429 *button_width = (radio_button_width (mw)
430 + mw->menu.horizontal_spacing);
435 static void
436 size_menu (mw, level)
437 XlwMenuWidget mw;
438 int level;
440 unsigned int label_width = 0;
441 int rest_width = 0;
442 int button_width = 0;
443 int max_rest_width = 0;
444 int max_button_width = 0;
445 unsigned int height = 0;
446 int horizontal_p = mw->menu.horizontal && (level == 0);
447 widget_value* val;
448 window_state* ws;
450 if (level >= mw->menu.old_depth)
451 abort_gracefully ((Widget) mw);
453 ws = &mw->menu.windows [level];
454 ws->width = 0;
455 ws->height = 0;
456 ws->label_width = 0;
457 ws->button_width = 0;
459 for (val = mw->menu.old_stack [level]->contents; val; val = val->next)
461 size_menu_item (mw, val, horizontal_p, &label_width, &rest_width,
462 &button_width, &height);
463 if (horizontal_p)
465 ws->width += label_width + rest_width;
466 if (height > ws->height)
467 ws->height = height;
469 else
471 if (label_width > ws->label_width)
472 ws->label_width = label_width;
473 if (rest_width > max_rest_width)
474 max_rest_width = rest_width;
475 if (button_width > max_button_width)
476 max_button_width = button_width;
477 ws->height += height;
481 if (horizontal_p)
482 ws->label_width = ws->button_width = 0;
483 else
485 ws->width = ws->label_width + max_rest_width + max_button_width;
486 ws->button_width = max_button_width;
489 ws->width += 2 * mw->menu.shadow_thickness;
490 ws->height += 2 * mw->menu.shadow_thickness;
492 if (horizontal_p)
494 ws->width += 2 * mw->menu.margin;
495 ws->height += 2 * mw->menu.margin;
500 \f/* Display code */
502 static void
503 draw_arrow (mw, window, gc, x, y, width, down_p)
504 XlwMenuWidget mw;
505 Window window;
506 GC gc;
507 int x;
508 int y;
509 int width;
510 int down_p;
512 Display *dpy = XtDisplay (mw);
513 GC top_gc = mw->menu.shadow_top_gc;
514 GC bottom_gc = mw->menu.shadow_bottom_gc;
515 int thickness = mw->menu.shadow_thickness;
516 int height = width;
517 XPoint pt[10];
518 /* alpha = atan (0.5)
519 factor = (1 + sin (alpha)) / cos (alpha) */
520 double factor = 1.62;
521 int thickness2 = thickness * factor;
523 y += (mw->menu.font->ascent + mw->menu.font->descent - height) / 2;
525 if (down_p)
527 GC temp;
528 temp = top_gc;
529 top_gc = bottom_gc;
530 bottom_gc = temp;
533 pt[0].x = x;
534 pt[0].y = y + height;
535 pt[1].x = x + thickness;
536 pt[1].y = y + height - thickness2;
537 pt[2].x = x + thickness2;
538 pt[2].y = y + thickness2;
539 pt[3].x = x;
540 pt[3].y = y;
541 XFillPolygon (dpy, window, top_gc, pt, 4, Convex, CoordModeOrigin);
543 pt[0].x = x;
544 pt[0].y = y;
545 pt[1].x = x + thickness;
546 pt[1].y = y + thickness2;
547 pt[2].x = x + width - thickness2;
548 pt[2].y = y + height / 2;
549 pt[3].x = x + width;
550 pt[3].y = y + height / 2;
551 XFillPolygon (dpy, window, top_gc, pt, 4, Convex, CoordModeOrigin);
553 pt[0].x = x;
554 pt[0].y = y + height;
555 pt[1].x = x + thickness;
556 pt[1].y = y + height - thickness2;
557 pt[2].x = x + width - thickness2;
558 pt[2].y = y + height / 2;
559 pt[3].x = x + width;
560 pt[3].y = y + height / 2;
561 XFillPolygon (dpy, window, bottom_gc, pt, 4, Convex, CoordModeOrigin);
566 static void
567 draw_shadow_rectangle (mw, window, x, y, width, height, erase_p, down_p)
568 XlwMenuWidget mw;
569 Window window;
570 int x;
571 int y;
572 int width;
573 int height;
574 int erase_p;
575 int down_p;
577 Display *dpy = XtDisplay (mw);
578 GC top_gc = !erase_p ? mw->menu.shadow_top_gc : mw->menu.background_gc;
579 GC bottom_gc = !erase_p ? mw->menu.shadow_bottom_gc : mw->menu.background_gc;
580 int thickness = mw->menu.shadow_thickness;
581 XPoint points [4];
583 if (!erase_p && down_p)
585 GC temp;
586 temp = top_gc;
587 top_gc = bottom_gc;
588 bottom_gc = temp;
591 points [0].x = x;
592 points [0].y = y;
593 points [1].x = x + width;
594 points [1].y = y;
595 points [2].x = x + width - thickness;
596 points [2].y = y + thickness;
597 points [3].x = x;
598 points [3].y = y + thickness;
599 XFillPolygon (dpy, window, top_gc, points, 4, Convex, CoordModeOrigin);
600 points [0].x = x;
601 points [0].y = y + thickness;
602 points [1].x = x;
603 points [1].y = y + height;
604 points [2].x = x + thickness;
605 points [2].y = y + height - thickness;
606 points [3].x = x + thickness;
607 points [3].y = y + thickness;
608 XFillPolygon (dpy, window, top_gc, points, 4, Convex, CoordModeOrigin);
609 points [0].x = x + width;
610 points [0].y = y;
611 points [1].x = x + width - thickness;
612 points [1].y = y + thickness;
613 points [2].x = x + width - thickness;
614 points [2].y = y + height - thickness;
615 points [3].x = x + width;
616 points [3].y = y + height - thickness;
617 XFillPolygon (dpy, window, bottom_gc, points, 4, Convex, CoordModeOrigin);
618 points [0].x = x;
619 points [0].y = y + height;
620 points [1].x = x + width;
621 points [1].y = y + height;
622 points [2].x = x + width;
623 points [2].y = y + height - thickness;
624 points [3].x = x + thickness;
625 points [3].y = y + height - thickness;
626 XFillPolygon (dpy, window, bottom_gc, points, 4, Convex, CoordModeOrigin);
630 static void
631 draw_shadow_rhombus (mw, window, x, y, width, height, erase_p, down_p)
632 XlwMenuWidget mw;
633 Window window;
634 int x;
635 int y;
636 int width;
637 int height;
638 int erase_p;
639 int down_p;
641 Display *dpy = XtDisplay (mw);
642 GC top_gc = !erase_p ? mw->menu.shadow_top_gc : mw->menu.background_gc;
643 GC bottom_gc = !erase_p ? mw->menu.shadow_bottom_gc : mw->menu.background_gc;
644 int thickness = mw->menu.shadow_thickness;
645 XPoint points [4];
647 if (!erase_p && down_p)
649 GC temp;
650 temp = top_gc;
651 top_gc = bottom_gc;
652 bottom_gc = temp;
655 points [0].x = x;
656 points [0].y = y + height / 2;
657 points [1].x = x + thickness;
658 points [1].y = y + height / 2;
659 points [2].x = x + width / 2;
660 points [2].y = y + thickness;
661 points [3].x = x + width / 2;
662 points [3].y = y;
663 XFillPolygon (dpy, window, top_gc, points, 4, Convex, CoordModeOrigin);
664 points [0].x = x + width / 2;
665 points [0].y = y;
666 points [1].x = x + width / 2;
667 points [1].y = y + thickness;
668 points [2].x = x + width - thickness;
669 points [2].y = y + height / 2;
670 points [3].x = x + width;
671 points [3].y = y + height / 2;
672 XFillPolygon (dpy, window, top_gc, points, 4, Convex, CoordModeOrigin);
673 points [0].x = x;
674 points [0].y = y + height / 2;
675 points [1].x = x + thickness;
676 points [1].y = y + height / 2;
677 points [2].x = x + width / 2;
678 points [2].y = y + height - thickness;
679 points [3].x = x + width / 2;
680 points [3].y = y + height;
681 XFillPolygon (dpy, window, bottom_gc, points, 4, Convex, CoordModeOrigin);
682 points [0].x = x + width / 2;
683 points [0].y = y + height;
684 points [1].x = x + width / 2;
685 points [1].y = y + height - thickness;
686 points [2].x = x + width - thickness;
687 points [2].y = y + height / 2;
688 points [3].x = x + width;
689 points [3].y = y + height / 2;
690 XFillPolygon (dpy, window, bottom_gc, points, 4, Convex, CoordModeOrigin);
694 /* Draw a toggle button on widget MW, X window WINDOW. X/Y is the
695 top-left corner of the menu item. SELECTED_P non-zero means the
696 toggle button is selected. */
698 static void
699 draw_toggle (mw, window, x, y, selected_p)
700 XlwMenuWidget mw;
701 Window window;
702 int x, y, selected_p;
704 int width, height;
706 width = toggle_button_width (mw);
707 height = width;
708 x += mw->menu.horizontal_spacing;
709 y += (mw->menu.font->ascent - height) / 2;
710 draw_shadow_rectangle (mw, window, x, y, width, height, False, selected_p);
714 /* Draw a radio button on widget MW, X window WINDOW. X/Y is the
715 top-left corner of the menu item. SELECTED_P non-zero means the
716 toggle button is selected. */
718 static void
719 draw_radio (mw, window, x, y, selected_p)
720 XlwMenuWidget mw;
721 Window window;
722 int x, y, selected_p;
724 int width, height;
726 width = radio_button_width (mw);
727 height = width;
728 x += mw->menu.horizontal_spacing;
729 y += (mw->menu.font->ascent - height) / 2;
730 draw_shadow_rhombus (mw, window, x, y, width, height, False, selected_p);
734 /* Draw a menu separator on widget MW, X window WINDOW. X/Y is the
735 top-left corner of the menu item. WIDTH is the width of the
736 separator to draw. TYPE is the separator type. */
738 static void
739 draw_separator (mw, window, x, y, width, type)
740 XlwMenuWidget mw;
741 Window window;
742 int x, y, width;
743 enum menu_separator type;
745 Display *dpy = XtDisplay (mw);
746 XGCValues xgcv;
748 switch (type)
750 case SEPARATOR_NO_LINE:
751 break;
753 case SEPARATOR_SINGLE_LINE:
754 XDrawLine (dpy, window, mw->menu.foreground_gc,
755 x, y, x + width, y);
756 break;
758 case SEPARATOR_DOUBLE_LINE:
759 draw_separator (mw, window, x, y, width, SEPARATOR_SINGLE_LINE);
760 draw_separator (mw, window, x, y + 2, width, SEPARATOR_SINGLE_LINE);
761 break;
763 case SEPARATOR_SINGLE_DASHED_LINE:
764 xgcv.line_style = LineOnOffDash;
765 XChangeGC (dpy, mw->menu.foreground_gc, GCLineStyle, &xgcv);
766 XDrawLine (dpy, window, mw->menu.foreground_gc,
767 x, y, x + width, y);
768 xgcv.line_style = LineSolid;
769 XChangeGC (dpy, mw->menu.foreground_gc, GCLineStyle, &xgcv);
770 break;
772 case SEPARATOR_DOUBLE_DASHED_LINE:
773 draw_separator (mw, window, x, y, width,
774 SEPARATOR_SINGLE_DASHED_LINE);
775 draw_separator (mw, window, x, y + 2, width,
776 SEPARATOR_SINGLE_DASHED_LINE);
777 break;
779 case SEPARATOR_SHADOW_ETCHED_IN:
780 XDrawLine (dpy, window, mw->menu.shadow_bottom_gc,
781 x, y, x + width, y);
782 XDrawLine (dpy, window, mw->menu.shadow_top_gc,
783 x, y + 1, x + width, y + 1);
784 break;
786 case SEPARATOR_SHADOW_ETCHED_OUT:
787 XDrawLine (dpy, window, mw->menu.shadow_top_gc,
788 x, y, x + width, y);
789 XDrawLine (dpy, window, mw->menu.shadow_bottom_gc,
790 x, y + 1, x + width, y + 1);
791 break;
793 case SEPARATOR_SHADOW_ETCHED_IN_DASH:
794 xgcv.line_style = LineOnOffDash;
795 XChangeGC (dpy, mw->menu.shadow_bottom_gc, GCLineStyle, &xgcv);
796 XChangeGC (dpy, mw->menu.shadow_top_gc, GCLineStyle, &xgcv);
797 draw_separator (mw, window, x, y, SEPARATOR_SHADOW_ETCHED_IN);
798 xgcv.line_style = LineSolid;
799 XChangeGC (dpy, mw->menu.shadow_bottom_gc, GCLineStyle, &xgcv);
800 XChangeGC (dpy, mw->menu.shadow_top_gc, GCLineStyle, &xgcv);
801 break;
803 case SEPARATOR_SHADOW_ETCHED_OUT_DASH:
804 xgcv.line_style = LineOnOffDash;
805 XChangeGC (dpy, mw->menu.shadow_bottom_gc, GCLineStyle, &xgcv);
806 XChangeGC (dpy, mw->menu.shadow_top_gc, GCLineStyle, &xgcv);
807 draw_separator (mw, window, x, y, SEPARATOR_SHADOW_ETCHED_OUT);
808 xgcv.line_style = LineSolid;
809 XChangeGC (dpy, mw->menu.shadow_bottom_gc, GCLineStyle, &xgcv);
810 XChangeGC (dpy, mw->menu.shadow_top_gc, GCLineStyle, &xgcv);
811 break;
813 case SEPARATOR_SHADOW_DOUBLE_ETCHED_IN:
814 draw_separator (mw, window, x, y, width, SEPARATOR_SHADOW_ETCHED_IN);
815 draw_separator (mw, window, x, y + 3, width, SEPARATOR_SHADOW_ETCHED_IN);
816 break;
818 case SEPARATOR_SHADOW_DOUBLE_ETCHED_OUT:
819 draw_separator (mw, window, x, y, width,
820 SEPARATOR_SHADOW_ETCHED_OUT);
821 draw_separator (mw, window, x, y + 3, width,
822 SEPARATOR_SHADOW_ETCHED_OUT);
823 break;
825 case SEPARATOR_SHADOW_DOUBLE_ETCHED_IN_DASH:
826 xgcv.line_style = LineOnOffDash;
827 XChangeGC (dpy, mw->menu.shadow_bottom_gc, GCLineStyle, &xgcv);
828 XChangeGC (dpy, mw->menu.shadow_top_gc, GCLineStyle, &xgcv);
829 draw_separator (mw, window, x, y, width,
830 SEPARATOR_SHADOW_DOUBLE_ETCHED_IN);
831 xgcv.line_style = LineSolid;
832 XChangeGC (dpy, mw->menu.shadow_bottom_gc, GCLineStyle, &xgcv);
833 XChangeGC (dpy, mw->menu.shadow_top_gc, GCLineStyle, &xgcv);
834 break;
836 case SEPARATOR_SHADOW_DOUBLE_ETCHED_OUT_DASH:
837 xgcv.line_style = LineOnOffDash;
838 XChangeGC (dpy, mw->menu.shadow_bottom_gc, GCLineStyle, &xgcv);
839 XChangeGC (dpy, mw->menu.shadow_top_gc, GCLineStyle, &xgcv);
840 draw_separator (mw, window, x, y, width,
841 SEPARATOR_SHADOW_DOUBLE_ETCHED_OUT);
842 xgcv.line_style = LineSolid;
843 XChangeGC (dpy, mw->menu.shadow_bottom_gc, GCLineStyle, &xgcv);
844 XChangeGC (dpy, mw->menu.shadow_top_gc, GCLineStyle, &xgcv);
845 break;
847 default:
848 abort ();
853 /* Return the pixel height of menu separator SEPARATOR. */
855 static int
856 separator_height (separator)
857 enum menu_separator separator;
859 switch (separator)
861 case SEPARATOR_NO_LINE:
862 return 2;
864 case SEPARATOR_SINGLE_LINE:
865 case SEPARATOR_SINGLE_DASHED_LINE:
866 return 1;
868 case SEPARATOR_DOUBLE_LINE:
869 case SEPARATOR_DOUBLE_DASHED_LINE:
870 return 3;
872 case SEPARATOR_SHADOW_ETCHED_IN:
873 case SEPARATOR_SHADOW_ETCHED_OUT:
874 case SEPARATOR_SHADOW_ETCHED_IN_DASH:
875 case SEPARATOR_SHADOW_ETCHED_OUT_DASH:
876 return 2;
878 case SEPARATOR_SHADOW_DOUBLE_ETCHED_IN:
879 case SEPARATOR_SHADOW_DOUBLE_ETCHED_OUT:
880 case SEPARATOR_SHADOW_DOUBLE_ETCHED_IN_DASH:
881 case SEPARATOR_SHADOW_DOUBLE_ETCHED_OUT_DASH:
882 return 5;
884 default:
885 abort ();
890 /* Display the menu item and increment where.x and where.y to show how large
891 the menu item was. */
893 static void
894 display_menu_item (mw, val, ws, where, highlighted_p, horizontal_p,
895 just_compute_p)
896 XlwMenuWidget mw;
897 widget_value* val;
898 window_state* ws;
899 XPoint* where;
900 Boolean highlighted_p;
901 Boolean horizontal_p;
902 Boolean just_compute_p;
904 GC deco_gc;
905 GC text_gc;
906 int font_ascent = mw->menu.font->ascent;
907 int font_descent = mw->menu.font->descent;
908 int shadow = mw->menu.shadow_thickness;
909 int margin = mw->menu.margin;
910 int h_spacing = mw->menu.horizontal_spacing;
911 int v_spacing = mw->menu.vertical_spacing;
912 int label_width;
913 int rest_width;
914 int button_width;
915 int height;
916 int width;
917 enum menu_separator separator;
918 int separator_p = lw_separator_p (val->name, &separator, 0);
920 /* compute the sizes of the item */
921 size_menu_item (mw, val, horizontal_p, &label_width, &rest_width,
922 &button_width, &height);
924 if (horizontal_p)
925 width = label_width + rest_width;
926 else
928 label_width = ws->label_width;
929 width = ws->width - 2 * shadow;
932 /* Only highlight an enabled item that has a callback. */
933 if (highlighted_p)
934 if (!val->enabled || !(val->call_data || val->contents))
935 highlighted_p = 0;
937 /* do the drawing. */
938 if (!just_compute_p)
940 /* Add the shadow border of the containing menu */
941 int x = where->x + shadow;
942 int y = where->y + shadow;
944 if (horizontal_p)
946 x += margin;
947 y += margin;
950 /* pick the foreground and background GC. */
951 if (val->enabled)
952 text_gc = mw->menu.foreground_gc;
953 else
954 text_gc = mw->menu.inactive_gc;
955 deco_gc = mw->menu.foreground_gc;
957 if (separator_p)
959 draw_separator (mw, ws->window, x, y, width, separator);
961 else
963 int x_offset = x + h_spacing + shadow;
964 char* display_string = resource_widget_value (mw, val);
965 draw_shadow_rectangle (mw, ws->window, x, y, width, height, True,
966 False);
968 /* Deal with centering a menu title. */
969 if (!horizontal_p && !val->contents && !val->call_data)
971 int l = string_width (mw, display_string);
973 if (width > l)
974 x_offset = (width - l) >> 1;
976 else if (!horizontal_p && ws->button_width)
977 x_offset += ws->button_width;
980 XDrawString (XtDisplay (mw), ws->window, text_gc, x_offset,
981 y + v_spacing + shadow + font_ascent,
982 display_string, strlen (display_string));
984 if (!horizontal_p)
986 if (val->button_type == BUTTON_TYPE_TOGGLE)
987 draw_toggle (mw, ws->window, x, y + v_spacing + shadow,
988 val->selected);
989 else if (val->button_type == BUTTON_TYPE_RADIO)
990 draw_radio (mw, ws->window, x, y + v_spacing + shadow,
991 val->selected);
993 if (val->contents)
995 int a_w = arrow_width (mw);
996 draw_arrow (mw, ws->window, deco_gc,
997 x + width - a_w
998 - mw->menu.horizontal_spacing
999 - mw->menu.shadow_thickness,
1000 y + v_spacing + shadow, a_w,
1001 highlighted_p);
1003 else if (val->key)
1005 XDrawString (XtDisplay (mw), ws->window, text_gc,
1006 x + label_width + mw->menu.arrow_spacing,
1007 y + v_spacing + shadow + font_ascent,
1008 val->key, strlen (val->key));
1011 else
1013 XDrawRectangle (XtDisplay (mw), ws->window,
1014 mw->menu.background_gc,
1015 x + shadow, y + shadow,
1016 label_width + h_spacing - 1,
1017 font_ascent + font_descent + 2 * v_spacing - 1);
1018 draw_shadow_rectangle (mw, ws->window, x, y, width, height,
1019 True, False);
1022 if (highlighted_p)
1023 draw_shadow_rectangle (mw, ws->window, x, y, width, height, False,
1024 False);
1028 where->x += width;
1029 where->y += height;
1032 static void
1033 display_menu (mw, level, just_compute_p, highlighted_pos, hit, hit_return,
1034 this, that)
1035 XlwMenuWidget mw;
1036 int level;
1037 Boolean just_compute_p;
1038 XPoint* highlighted_pos;
1039 XPoint* hit;
1040 widget_value** hit_return;
1041 widget_value* this;
1042 widget_value* that;
1044 widget_value* val;
1045 widget_value* following_item;
1046 window_state* ws;
1047 XPoint where;
1048 int horizontal_p = mw->menu.horizontal && (level == 0);
1049 int highlighted_p;
1050 int just_compute_this_one_p;
1051 /* This is set nonzero if the element containing HIGHLIGHTED_POS
1052 is disabled, so that we do not return any subsequent element either. */
1053 int no_return = 0;
1054 enum menu_separator separator;
1056 if (level >= mw->menu.old_depth)
1057 abort_gracefully ((Widget) mw);
1059 if (level < mw->menu.old_depth - 1)
1060 following_item = mw->menu.old_stack [level + 1];
1061 else
1062 following_item = NULL;
1064 if (hit)
1065 *hit_return = NULL;
1067 where.x = 0;
1068 where.y = 0;
1070 ws = &mw->menu.windows [level];
1071 for (val = mw->menu.old_stack [level]->contents; val; val = val->next)
1073 highlighted_p = val == following_item;
1074 if (highlighted_p && highlighted_pos)
1076 if (horizontal_p)
1077 highlighted_pos->x = where.x;
1078 else
1079 highlighted_pos->y = where.y;
1082 just_compute_this_one_p =
1083 just_compute_p || ((this || that) && val != this && val != that);
1085 display_menu_item (mw, val, ws, &where, highlighted_p, horizontal_p,
1086 just_compute_this_one_p);
1088 if (highlighted_p && highlighted_pos)
1090 if (horizontal_p)
1091 highlighted_pos->y = where.y;
1092 else
1093 highlighted_pos->x = where.x;
1096 if (hit
1097 && !*hit_return
1098 && (horizontal_p ? hit->x < where.x : hit->y < where.y)
1099 && !lw_separator_p (val->name, &separator, 0)
1100 && !no_return)
1102 if (val->enabled)
1103 *hit_return = val;
1104 else
1105 no_return = 1;
1108 if (horizontal_p)
1109 where.y = 0;
1110 else
1111 where.x = 0;
1114 if (!just_compute_p)
1115 draw_shadow_rectangle (mw, ws->window, 0, 0, ws->width, ws->height,
1116 False, False);
1119 \f/* Motion code */
1120 static void
1121 set_new_state (mw, val, level)
1122 XlwMenuWidget mw;
1123 widget_value* val;
1124 int level;
1126 int i;
1128 mw->menu.new_depth = 0;
1129 for (i = 0; i < level; i++)
1130 push_new_stack (mw, mw->menu.old_stack [i]);
1131 push_new_stack (mw, val);
1134 static void
1135 make_windows_if_needed (mw, n)
1136 XlwMenuWidget mw;
1137 int n;
1139 int i;
1140 int start_at;
1141 XSetWindowAttributes xswa;
1142 int mask;
1143 Window root = RootWindowOfScreen (DefaultScreenOfDisplay (XtDisplay (mw)));
1144 window_state* windows;
1146 if (mw->menu.windows_length >= n)
1147 return;
1149 xswa.save_under = True;
1150 xswa.override_redirect = True;
1151 xswa.background_pixel = mw->core.background_pixel;
1152 xswa.border_pixel = mw->core.border_pixel;
1153 xswa.event_mask =
1154 ExposureMask | PointerMotionMask | PointerMotionHintMask
1155 | ButtonReleaseMask | ButtonPressMask;
1156 xswa.cursor = mw->menu.cursor_shape;
1157 mask = CWSaveUnder | CWOverrideRedirect | CWBackPixel | CWBorderPixel
1158 | CWEventMask | CWCursor;
1160 if (!mw->menu.windows)
1162 mw->menu.windows =
1163 (window_state*)XtMalloc (n * sizeof (window_state));
1164 start_at = 0;
1166 else
1168 mw->menu.windows =
1169 (window_state*)XtRealloc ((char*)mw->menu.windows,
1170 n * sizeof (window_state));
1171 start_at = mw->menu.windows_length;
1173 mw->menu.windows_length = n;
1175 windows = mw->menu.windows;
1177 for (i = start_at; i < n; i++)
1179 windows [i].x = 0;
1180 windows [i].y = 0;
1181 windows [i].width = 1;
1182 windows [i].height = 1;
1183 windows [i].window =
1184 XCreateWindow (XtDisplay (mw), root, 0, 0, 1, 1,
1185 0, 0, CopyFromParent, CopyFromParent, mask, &xswa);
1189 /* Make the window fit in the screen */
1190 static void
1191 fit_to_screen (mw, ws, previous_ws, horizontal_p)
1192 XlwMenuWidget mw;
1193 window_state* ws;
1194 window_state* previous_ws;
1195 Boolean horizontal_p;
1197 unsigned int screen_width = WidthOfScreen (XtScreen (mw));
1198 unsigned int screen_height = HeightOfScreen (XtScreen (mw));
1199 /* 1 if we are unable to avoid an overlap between
1200 this menu and the parent menu in the X dimension. */
1201 int horizontal_overlap = 0;
1203 if (ws->x < 0)
1204 ws->x = 0;
1205 else if (ws->x + ws->width > screen_width)
1207 if (!horizontal_p)
1208 ws->x = previous_ws->x - ws->width;
1209 else
1210 ws->x = screen_width - ws->width;
1211 if (ws->x < 0)
1213 ws->x = 0;
1214 horizontal_overlap = 1;
1217 /* If we overlap in X, try to avoid overlap in Y. */
1218 if (horizontal_overlap
1219 && ws->y < previous_ws->y + previous_ws->height
1220 && previous_ws->y < ws->y + ws->height)
1222 /* Put this menu right below or right above PREVIOUS_WS
1223 if there's room. */
1224 if (previous_ws->y + previous_ws->height + ws->height < screen_height)
1225 ws->y = previous_ws->y + previous_ws->height;
1226 else if (previous_ws->y - ws->height > 0)
1227 ws->y = previous_ws->y - ws->height;
1230 if (ws->y < 0)
1231 ws->y = 0;
1232 else if (ws->y + ws->height > screen_height)
1234 if (horizontal_p)
1235 ws->y = previous_ws->y - ws->height;
1236 else
1237 ws->y = screen_height - ws->height;
1238 if (ws->y < 0)
1239 ws->y = 0;
1243 /* Updates old_stack from new_stack and redisplays. */
1244 static void
1245 remap_menubar (mw)
1246 XlwMenuWidget mw;
1248 int i;
1249 int last_same;
1250 XPoint selection_position;
1251 int old_depth = mw->menu.old_depth;
1252 int new_depth = mw->menu.new_depth;
1253 widget_value** old_stack;
1254 widget_value** new_stack;
1255 window_state* windows;
1256 widget_value* old_selection;
1257 widget_value* new_selection;
1259 /* Check that enough windows and old_stack are ready. */
1260 make_windows_if_needed (mw, new_depth);
1261 make_old_stack_space (mw, new_depth);
1262 windows = mw->menu.windows;
1263 old_stack = mw->menu.old_stack;
1264 new_stack = mw->menu.new_stack;
1266 /* compute the last identical different entry */
1267 for (i = 1; i < old_depth && i < new_depth; i++)
1268 if (old_stack [i] != new_stack [i])
1269 break;
1270 last_same = i - 1;
1272 /* Memorize the previously selected item to be able to refresh it */
1273 old_selection = last_same + 1 < old_depth ? old_stack [last_same + 1] : NULL;
1274 if (old_selection && !old_selection->enabled)
1275 old_selection = NULL;
1276 new_selection = last_same + 1 < new_depth ? new_stack [last_same + 1] : NULL;
1277 if (new_selection && !new_selection->enabled)
1278 new_selection = NULL;
1280 /* Call callback when the hightlighted item changes. */
1281 if (old_selection || new_selection)
1282 XtCallCallbackList ((Widget)mw, mw->menu.highlight,
1283 (XtPointer) new_selection);
1285 /* updates old_state from new_state. It has to be done now because
1286 display_menu (called below) uses the old_stack to know what to display. */
1287 for (i = last_same + 1; i < new_depth; i++)
1288 old_stack [i] = new_stack [i];
1289 mw->menu.old_depth = new_depth;
1291 /* refresh the last selection */
1292 selection_position.x = 0;
1293 selection_position.y = 0;
1294 display_menu (mw, last_same, new_selection == old_selection,
1295 &selection_position, NULL, NULL, old_selection, new_selection);
1297 /* Now place the new menus. */
1298 for (i = last_same + 1; i < new_depth && new_stack[i]->contents; i++)
1300 window_state *previous_ws = &windows[i - 1];
1301 window_state *ws = &windows[i];
1303 ws->x = (previous_ws->x + selection_position.x
1304 + mw->menu.shadow_thickness);
1305 if (i == 1)
1306 ws->x += mw->menu.margin;
1308 #if 0
1309 if (!mw->menu.horizontal || i > 1)
1310 ws->x += mw->menu.shadow_thickness;
1311 #endif
1313 ws->y = (previous_ws->y + selection_position.y
1314 + mw->menu.shadow_thickness);
1315 if (i == 1)
1316 ws->y += mw->menu.margin;
1318 size_menu (mw, i);
1320 fit_to_screen (mw, ws, previous_ws, mw->menu.horizontal && i == 1);
1322 XClearWindow (XtDisplay (mw), ws->window);
1323 XMoveResizeWindow (XtDisplay (mw), ws->window, ws->x, ws->y,
1324 ws->width, ws->height);
1325 XMapRaised (XtDisplay (mw), ws->window);
1326 display_menu (mw, i, False, &selection_position, NULL, NULL, NULL, NULL);
1329 /* unmap the menus that popped down */
1330 for (i = new_depth - 1; i < old_depth; i++)
1331 if (i >= new_depth || !new_stack[i]->contents)
1332 XUnmapWindow (XtDisplay (mw), windows[i].window);
1335 static Boolean
1336 motion_event_is_in_menu (mw, ev, level, relative_pos)
1337 XlwMenuWidget mw;
1338 XMotionEvent* ev;
1339 int level;
1340 XPoint* relative_pos;
1342 window_state* ws = &mw->menu.windows [level];
1343 int shadow = level == 0 ? 0 : mw->menu.shadow_thickness;
1344 int x = ws->x + shadow;
1345 int y = ws->y + shadow;
1346 relative_pos->x = ev->x_root - x;
1347 relative_pos->y = ev->y_root - y;
1348 return (x - shadow < ev->x_root && ev->x_root < x + ws->width
1349 && y - shadow < ev->y_root && ev->y_root < y + ws->height);
1352 static Boolean
1353 map_event_to_widget_value (mw, ev, val, level)
1354 XlwMenuWidget mw;
1355 XMotionEvent* ev;
1356 widget_value** val;
1357 int* level;
1359 int i;
1360 XPoint relative_pos;
1361 window_state* ws;
1363 *val = NULL;
1365 /* Find the window */
1366 for (i = mw->menu.old_depth - 1; i >= 0; i--)
1368 ws = &mw->menu.windows [i];
1369 if (ws && motion_event_is_in_menu (mw, ev, i, &relative_pos))
1371 display_menu (mw, i, True, NULL, &relative_pos, val, NULL, NULL);
1373 if (*val)
1375 *level = i + 1;
1376 return True;
1380 return False;
1383 \f/* Procedures */
1384 static void
1385 make_drawing_gcs (mw)
1386 XlwMenuWidget mw;
1388 XGCValues xgcv;
1390 xgcv.font = mw->menu.font->fid;
1391 xgcv.foreground = mw->menu.foreground;
1392 xgcv.background = mw->core.background_pixel;
1393 mw->menu.foreground_gc = XtGetGC ((Widget)mw,
1394 GCFont | GCForeground | GCBackground,
1395 &xgcv);
1397 xgcv.font = mw->menu.font->fid;
1398 xgcv.foreground = mw->menu.button_foreground;
1399 xgcv.background = mw->core.background_pixel;
1400 mw->menu.button_gc = XtGetGC ((Widget)mw,
1401 GCFont | GCForeground | GCBackground,
1402 &xgcv);
1404 xgcv.font = mw->menu.font->fid;
1405 xgcv.foreground = mw->menu.foreground;
1406 xgcv.background = mw->core.background_pixel;
1407 xgcv.fill_style = FillStippled;
1408 xgcv.stipple = mw->menu.gray_pixmap;
1409 mw->menu.inactive_gc = XtGetGC ((Widget)mw,
1410 (GCFont | GCForeground | GCBackground
1411 | GCFillStyle | GCStipple), &xgcv);
1413 xgcv.font = mw->menu.font->fid;
1414 xgcv.foreground = mw->menu.button_foreground;
1415 xgcv.background = mw->core.background_pixel;
1416 xgcv.fill_style = FillStippled;
1417 xgcv.stipple = mw->menu.gray_pixmap;
1418 mw->menu.inactive_button_gc = XtGetGC ((Widget)mw,
1419 (GCFont | GCForeground | GCBackground
1420 | GCFillStyle | GCStipple), &xgcv);
1422 xgcv.font = mw->menu.font->fid;
1423 xgcv.foreground = mw->core.background_pixel;
1424 xgcv.background = mw->menu.foreground;
1425 mw->menu.background_gc = XtGetGC ((Widget)mw,
1426 GCFont | GCForeground | GCBackground,
1427 &xgcv);
1430 static void
1431 release_drawing_gcs (mw)
1432 XlwMenuWidget mw;
1434 XtReleaseGC ((Widget) mw, mw->menu.foreground_gc);
1435 XtReleaseGC ((Widget) mw, mw->menu.button_gc);
1436 XtReleaseGC ((Widget) mw, mw->menu.inactive_gc);
1437 XtReleaseGC ((Widget) mw, mw->menu.inactive_button_gc);
1438 XtReleaseGC ((Widget) mw, mw->menu.background_gc);
1439 /* let's get some segvs if we try to use these... */
1440 mw->menu.foreground_gc = (GC) -1;
1441 mw->menu.button_gc = (GC) -1;
1442 mw->menu.inactive_gc = (GC) -1;
1443 mw->menu.inactive_button_gc = (GC) -1;
1444 mw->menu.background_gc = (GC) -1;
1447 #define MINL(x,y) ((((unsigned long) (x)) < ((unsigned long) (y))) \
1448 ? ((unsigned long) (x)) : ((unsigned long) (y)))
1450 static void
1451 make_shadow_gcs (mw)
1452 XlwMenuWidget mw;
1454 XGCValues xgcv;
1455 unsigned long pm = 0;
1456 Display *dpy = XtDisplay ((Widget) mw);
1457 Screen *screen = XtScreen ((Widget) mw);
1458 Colormap cmap = mw->core.colormap;
1459 XColor topc, botc;
1460 int top_frobbed = 0, bottom_frobbed = 0;
1462 mw->menu.free_top_shadow_color_p = 0;
1463 mw->menu.free_bottom_shadow_color_p = 0;
1465 if (mw->menu.top_shadow_color == -1)
1466 mw->menu.top_shadow_color = mw->core.background_pixel;
1467 else
1468 mw->menu.top_shadow_color = mw->menu.top_shadow_color;
1470 if (mw->menu.bottom_shadow_color == -1)
1471 mw->menu.bottom_shadow_color = mw->menu.foreground;
1472 else
1473 mw->menu.bottom_shadow_color = mw->menu.bottom_shadow_color;
1475 if (mw->menu.top_shadow_color == mw->core.background_pixel ||
1476 mw->menu.top_shadow_color == mw->menu.foreground)
1478 topc.pixel = mw->core.background_pixel;
1479 XQueryColor (dpy, cmap, &topc);
1480 /* don't overflow/wrap! */
1481 topc.red = MINL (65535, topc.red * 1.2);
1482 topc.green = MINL (65535, topc.green * 1.2);
1483 topc.blue = MINL (65535, topc.blue * 1.2);
1484 #ifdef emacs
1485 if (x_alloc_nearest_color_for_widget ((Widget) mw, cmap, &topc))
1486 #else
1487 if (XAllocColor (dpy, cmap, &topc))
1488 #endif
1490 mw->menu.top_shadow_color = topc.pixel;
1491 mw->menu.free_top_shadow_color_p = 1;
1492 top_frobbed = 1;
1495 if (mw->menu.bottom_shadow_color == mw->menu.foreground ||
1496 mw->menu.bottom_shadow_color == mw->core.background_pixel)
1498 botc.pixel = mw->core.background_pixel;
1499 XQueryColor (dpy, cmap, &botc);
1500 botc.red *= 0.6;
1501 botc.green *= 0.6;
1502 botc.blue *= 0.6;
1503 #ifdef emacs
1504 if (x_alloc_nearest_color_for_widget ((Widget) mw, cmap, &botc))
1505 #else
1506 if (XAllocColor (dpy, cmap, &botc))
1507 #endif
1509 mw->menu.bottom_shadow_color = botc.pixel;
1510 mw->menu.free_bottom_shadow_color_p = 1;
1511 bottom_frobbed = 1;
1515 if (top_frobbed && bottom_frobbed)
1517 int top_avg = ((topc.red / 3) + (topc.green / 3) + (topc.blue / 3));
1518 int bot_avg = ((botc.red / 3) + (botc.green / 3) + (botc.blue / 3));
1519 if (bot_avg > top_avg)
1521 Pixel tmp = mw->menu.top_shadow_color;
1522 mw->menu.top_shadow_color = mw->menu.bottom_shadow_color;
1523 mw->menu.bottom_shadow_color = tmp;
1525 else if (topc.pixel == botc.pixel)
1527 if (botc.pixel == mw->menu.foreground)
1529 if (mw->menu.free_top_shadow_color_p)
1531 x_free_dpy_colors (dpy, screen, cmap,
1532 &mw->menu.top_shadow_color, 1);
1533 mw->menu.free_top_shadow_color_p = 0;
1535 mw->menu.top_shadow_color = mw->core.background_pixel;
1537 else
1539 if (mw->menu.free_bottom_shadow_color_p)
1541 x_free_dpy_colors (dpy, screen, cmap,
1542 &mw->menu.bottom_shadow_color, 1);
1543 mw->menu.free_bottom_shadow_color_p = 0;
1545 mw->menu.bottom_shadow_color = mw->menu.foreground;
1550 if (!mw->menu.top_shadow_pixmap &&
1551 mw->menu.top_shadow_color == mw->core.background_pixel)
1553 mw->menu.top_shadow_pixmap = mw->menu.gray_pixmap;
1554 if (mw->menu.free_top_shadow_color_p)
1556 x_free_dpy_colors (dpy, screen, cmap, &mw->menu.top_shadow_color, 1);
1557 mw->menu.free_top_shadow_color_p = 0;
1559 mw->menu.top_shadow_color = mw->menu.foreground;
1561 if (!mw->menu.bottom_shadow_pixmap &&
1562 mw->menu.bottom_shadow_color == mw->core.background_pixel)
1564 mw->menu.bottom_shadow_pixmap = mw->menu.gray_pixmap;
1565 if (mw->menu.free_bottom_shadow_color_p)
1567 x_free_dpy_colors (dpy, screen, cmap,
1568 &mw->menu.bottom_shadow_color, 1);
1569 mw->menu.free_bottom_shadow_color_p = 0;
1571 mw->menu.bottom_shadow_color = mw->menu.foreground;
1574 xgcv.fill_style = FillStippled;
1575 xgcv.foreground = mw->menu.top_shadow_color;
1576 xgcv.stipple = mw->menu.top_shadow_pixmap;
1577 pm = (xgcv.stipple ? GCStipple|GCFillStyle : 0);
1578 mw->menu.shadow_top_gc = XtGetGC ((Widget)mw, GCForeground | pm, &xgcv);
1580 xgcv.foreground = mw->menu.bottom_shadow_color;
1581 xgcv.stipple = mw->menu.bottom_shadow_pixmap;
1582 pm = (xgcv.stipple ? GCStipple|GCFillStyle : 0);
1583 mw->menu.shadow_bottom_gc = XtGetGC ((Widget)mw, GCForeground | pm, &xgcv);
1587 static void
1588 release_shadow_gcs (mw)
1589 XlwMenuWidget mw;
1591 Display *dpy = XtDisplay ((Widget) mw);
1592 Screen *screen = XtScreen ((Widget) mw);
1593 Colormap cmap = mw->core.colormap;
1594 Pixel px[2];
1595 int i = 0;
1597 if (mw->menu.free_top_shadow_color_p)
1598 px[i++] = mw->menu.top_shadow_color;
1599 if (mw->menu.free_bottom_shadow_color_p)
1600 px[i++] = mw->menu.bottom_shadow_color;
1601 if (i > 0)
1602 x_free_dpy_colors (dpy, screen, cmap, px, i);
1604 XtReleaseGC ((Widget) mw, mw->menu.shadow_top_gc);
1605 XtReleaseGC ((Widget) mw, mw->menu.shadow_bottom_gc);
1608 static void
1609 XlwMenuInitialize (request, mw, args, num_args)
1610 Widget request;
1611 XlwMenuWidget mw;
1612 ArgList args;
1613 Cardinal *num_args;
1615 /* Get the GCs and the widget size */
1616 XSetWindowAttributes xswa;
1617 int mask;
1619 Window window = RootWindowOfScreen (DefaultScreenOfDisplay (XtDisplay (mw)));
1620 Display* display = XtDisplay (mw);
1622 #if 0
1623 widget_value *tem = (widget_value *) XtMalloc (sizeof (widget_value));
1625 /* _XtCreate is freeing the object that was passed to us,
1626 so make a copy that we will actually keep. */
1627 lwlib_bcopy (mw->menu.contents, tem, sizeof (widget_value));
1628 mw->menu.contents = tem;
1629 #endif
1631 /* mw->menu.cursor = XCreateFontCursor (display, mw->menu.cursor_shape); */
1632 mw->menu.cursor = mw->menu.cursor_shape;
1634 mw->menu.gray_pixmap
1635 = XCreatePixmapFromBitmapData (display, window, gray_bitmap_bits,
1636 gray_bitmap_width, gray_bitmap_height,
1637 (unsigned long)1, (unsigned long)0, 1);
1639 /* I don't understand why this ends up 0 sometimes,
1640 but it does. This kludge works around it.
1641 Can anyone find a real fix? -- rms. */
1642 if (mw->menu.font == 0)
1643 mw->menu.font = xlwmenu_default_font;
1645 make_drawing_gcs (mw);
1646 make_shadow_gcs (mw);
1648 xswa.background_pixel = mw->core.background_pixel;
1649 xswa.border_pixel = mw->core.border_pixel;
1650 mask = CWBackPixel | CWBorderPixel;
1652 mw->menu.popped_up = False;
1654 mw->menu.old_depth = 1;
1655 mw->menu.old_stack = (widget_value**)XtMalloc (sizeof (widget_value*));
1656 mw->menu.old_stack_length = 1;
1657 mw->menu.old_stack [0] = mw->menu.contents;
1659 mw->menu.new_depth = 0;
1660 mw->menu.new_stack = 0;
1661 mw->menu.new_stack_length = 0;
1662 push_new_stack (mw, mw->menu.contents);
1664 mw->menu.windows = (window_state*)XtMalloc (sizeof (window_state));
1665 mw->menu.windows_length = 1;
1666 mw->menu.windows [0].x = 0;
1667 mw->menu.windows [0].y = 0;
1668 mw->menu.windows [0].width = 0;
1669 mw->menu.windows [0].height = 0;
1670 size_menu (mw, 0);
1672 mw->core.width = mw->menu.windows [0].width;
1673 mw->core.height = mw->menu.windows [0].height;
1676 static void
1677 XlwMenuClassInitialize ()
1681 static void
1682 XlwMenuRealize (w, valueMask, attributes)
1683 Widget w;
1684 Mask *valueMask;
1685 XSetWindowAttributes *attributes;
1687 XlwMenuWidget mw = (XlwMenuWidget)w;
1688 XSetWindowAttributes xswa;
1689 int mask;
1691 (*xlwMenuWidgetClass->core_class.superclass->core_class.realize)
1692 (w, valueMask, attributes);
1694 xswa.save_under = True;
1695 xswa.cursor = mw->menu.cursor_shape;
1696 mask = CWSaveUnder | CWCursor;
1697 XChangeWindowAttributes (XtDisplay (w), XtWindow (w), mask, &xswa);
1699 mw->menu.windows [0].window = XtWindow (w);
1700 mw->menu.windows [0].x = w->core.x;
1701 mw->menu.windows [0].y = w->core.y;
1702 mw->menu.windows [0].width = w->core.width;
1703 mw->menu.windows [0].height = w->core.height;
1706 /* Only the toplevel menubar/popup is a widget so it's the only one that
1707 receives expose events through Xt. So we repaint all the other panes
1708 when receiving an Expose event. */
1709 static void
1710 XlwMenuRedisplay (w, ev, region)
1711 Widget w;
1712 XEvent* ev;
1713 Region region;
1715 XlwMenuWidget mw = (XlwMenuWidget)w;
1716 int i;
1718 /* If we have a depth beyond 1, it's because a submenu was displayed.
1719 If the submenu has been destroyed, set the depth back to 1. */
1720 if (submenu_destroyed)
1722 mw->menu.old_depth = 1;
1723 submenu_destroyed = 0;
1726 for (i = 0; i < mw->menu.old_depth; i++)
1727 display_menu (mw, i, False, NULL, NULL, NULL, NULL, NULL);
1730 static void
1731 XlwMenuDestroy (w)
1732 Widget w;
1734 int i;
1735 XlwMenuWidget mw = (XlwMenuWidget) w;
1737 if (pointer_grabbed)
1738 XtUngrabPointer ((Widget)w, CurrentTime);
1739 pointer_grabbed = 0;
1741 submenu_destroyed = 1;
1743 release_drawing_gcs (mw);
1744 release_shadow_gcs (mw);
1746 /* this doesn't come from the resource db but is created explicitly
1747 so we must free it ourselves. */
1748 XFreePixmap (XtDisplay (mw), mw->menu.gray_pixmap);
1749 mw->menu.gray_pixmap = (Pixmap) -1;
1751 #if 0
1752 /* Do free mw->menu.contents because nowadays we copy it
1753 during initialization. */
1754 XtFree (mw->menu.contents);
1755 #endif
1757 /* Don't free mw->menu.contents because that comes from our creator.
1758 The `*_stack' elements are just pointers into `contents' so leave
1759 that alone too. But free the stacks themselves. */
1760 if (mw->menu.old_stack) XtFree ((char *) mw->menu.old_stack);
1761 if (mw->menu.new_stack) XtFree ((char *) mw->menu.new_stack);
1763 /* Remember, you can't free anything that came from the resource
1764 database. This includes:
1765 mw->menu.cursor
1766 mw->menu.top_shadow_pixmap
1767 mw->menu.bottom_shadow_pixmap
1768 mw->menu.font
1769 Also the color cells of top_shadow_color, bottom_shadow_color,
1770 foreground, and button_foreground will never be freed until this
1771 client exits. Nice, eh?
1774 /* start from 1 because the one in slot 0 is w->core.window */
1775 for (i = 1; i < mw->menu.windows_length; i++)
1776 XDestroyWindow (XtDisplay (mw), mw->menu.windows [i].window);
1777 if (mw->menu.windows)
1778 XtFree ((char *) mw->menu.windows);
1781 static Boolean
1782 XlwMenuSetValues (current, request, new)
1783 Widget current;
1784 Widget request;
1785 Widget new;
1787 XlwMenuWidget oldmw = (XlwMenuWidget)current;
1788 XlwMenuWidget newmw = (XlwMenuWidget)new;
1789 Boolean redisplay = False;
1790 int i;
1792 if (newmw->menu.contents
1793 && newmw->menu.contents->contents
1794 && newmw->menu.contents->contents->change >= VISIBLE_CHANGE)
1795 redisplay = True;
1796 /* Do redisplay if the contents are entirely eliminated. */
1797 if (newmw->menu.contents
1798 && newmw->menu.contents->contents == 0
1799 && newmw->menu.contents->change >= VISIBLE_CHANGE)
1800 redisplay = True;
1802 if (newmw->core.background_pixel != oldmw->core.background_pixel
1803 || newmw->menu.foreground != oldmw->menu.foreground
1804 || newmw->menu.font != oldmw->menu.font)
1806 release_drawing_gcs (newmw);
1807 make_drawing_gcs (newmw);
1808 redisplay = True;
1810 for (i = 0; i < oldmw->menu.windows_length; i++)
1812 XSetWindowBackground (XtDisplay (oldmw),
1813 oldmw->menu.windows [i].window,
1814 newmw->core.background_pixel);
1815 /* clear windows and generate expose events */
1816 XClearArea (XtDisplay (oldmw), oldmw->menu.windows[i].window,
1817 0, 0, 0, 0, True);
1821 return redisplay;
1824 static void
1825 XlwMenuResize (w)
1826 Widget w;
1828 XlwMenuWidget mw = (XlwMenuWidget)w;
1830 if (mw->menu.popped_up)
1832 /* Don't allow the popup menu to resize itself. */
1833 mw->core.width = mw->menu.windows [0].width;
1834 mw->core.height = mw->menu.windows [0].height;
1835 mw->core.parent->core.width = mw->core.width ;
1836 mw->core.parent->core.height = mw->core.height ;
1838 else
1840 mw->menu.windows [0].width = mw->core.width;
1841 mw->menu.windows [0].height = mw->core.height;
1845 \f/* Action procedures */
1846 static void
1847 handle_single_motion_event (mw, ev)
1848 XlwMenuWidget mw;
1849 XMotionEvent* ev;
1851 widget_value* val;
1852 int level;
1854 if (!map_event_to_widget_value (mw, ev, &val, &level))
1855 pop_new_stack_if_no_contents (mw);
1856 else
1857 set_new_state (mw, val, level);
1858 remap_menubar (mw);
1860 /* Sync with the display. Makes it feel better on X terms. */
1861 XSync (XtDisplay (mw), False);
1864 static void
1865 handle_motion_event (mw, ev)
1866 XlwMenuWidget mw;
1867 XMotionEvent* ev;
1869 int x = ev->x_root;
1870 int y = ev->y_root;
1871 int state = ev->state;
1873 handle_single_motion_event (mw, ev);
1875 /* allow motion events to be generated again */
1876 if (ev->is_hint
1877 && XQueryPointer (XtDisplay (mw), ev->window,
1878 &ev->root, &ev->subwindow,
1879 &ev->x_root, &ev->y_root,
1880 &ev->x, &ev->y,
1881 &ev->state)
1882 && ev->state == state
1883 && (ev->x_root != x || ev->y_root != y))
1884 handle_single_motion_event (mw, ev);
1887 static void
1888 Start (w, ev, params, num_params)
1889 Widget w;
1890 XEvent *ev;
1891 String *params;
1892 Cardinal *num_params;
1894 XlwMenuWidget mw = (XlwMenuWidget)w;
1896 if (!mw->menu.popped_up)
1898 menu_post_event = *ev;
1899 pop_up_menu (mw, (XButtonPressedEvent*) ev);
1901 else
1903 /* If we push a button while the menu is posted semipermanently,
1904 releasing the button should always pop the menu down. */
1905 next_release_must_exit = 1;
1907 /* notes the absolute position of the menubar window */
1908 mw->menu.windows [0].x = ev->xmotion.x_root - ev->xmotion.x;
1909 mw->menu.windows [0].y = ev->xmotion.y_root - ev->xmotion.y;
1911 /* handles the down like a move, slots are compatible */
1912 handle_motion_event (mw, &ev->xmotion);
1916 static void
1917 Drag (w, ev, params, num_params)
1918 Widget w;
1919 XEvent *ev;
1920 String *params;
1921 Cardinal *num_params;
1923 XlwMenuWidget mw = (XlwMenuWidget)w;
1924 if (mw->menu.popped_up)
1925 handle_motion_event (mw, &ev->xmotion);
1928 /* Do nothing.
1929 This is how we handle presses and releases of modifier keys. */
1930 static void
1931 Nothing (w, ev, params, num_params)
1932 Widget w;
1933 XEvent *ev;
1934 String *params;
1935 Cardinal *num_params;
1939 /* Handle key press and release events while menu is popped up.
1940 Our action is to get rid of the menu. */
1941 static void
1942 Key (w, ev, params, num_params)
1943 Widget w;
1944 XEvent *ev;
1945 String *params;
1946 Cardinal *num_params;
1948 XlwMenuWidget mw = (XlwMenuWidget)w;
1950 /* Pop down everything. */
1951 mw->menu.new_depth = 1;
1952 remap_menubar (mw);
1954 if (mw->menu.popped_up)
1956 mw->menu.popped_up = False;
1957 XtUngrabPointer ((Widget)mw, ev->xmotion.time);
1958 if (XtIsShell (XtParent ((Widget) mw)))
1959 XtPopdown (XtParent ((Widget) mw));
1960 else
1962 XtRemoveGrab ((Widget) mw);
1963 display_menu (mw, 0, False, NULL, NULL, NULL, NULL, NULL);
1967 /* callback */
1968 XtCallCallbackList ((Widget)mw, mw->menu.select, (XtPointer)0);
1971 static void
1972 Select (w, ev, params, num_params)
1973 Widget w;
1974 XEvent *ev;
1975 String *params;
1976 Cardinal *num_params;
1978 XlwMenuWidget mw = (XlwMenuWidget)w;
1979 widget_value* selected_item = mw->menu.old_stack [mw->menu.old_depth - 1];
1981 /* If user releases the button quickly, without selecting anything,
1982 after the initial down-click that brought the menu up,
1983 do nothing. */
1984 if ((selected_item == 0
1985 || ((widget_value *) selected_item)->call_data == 0)
1986 && !next_release_must_exit
1987 && (ev->xbutton.time - menu_post_event.xbutton.time
1988 < XtGetMultiClickTime (XtDisplay (w))))
1989 return;
1991 /* pop down everything. */
1992 mw->menu.new_depth = 1;
1993 remap_menubar (mw);
1995 if (mw->menu.popped_up)
1997 mw->menu.popped_up = False;
1998 XtUngrabPointer ((Widget)mw, ev->xmotion.time);
1999 if (XtIsShell (XtParent ((Widget) mw)))
2000 XtPopdown (XtParent ((Widget) mw));
2001 else
2003 XtRemoveGrab ((Widget) mw);
2004 display_menu (mw, 0, False, NULL, NULL, NULL, NULL, NULL);
2008 /* callback */
2009 XtCallCallbackList ((Widget)mw, mw->menu.select, (XtPointer)selected_item);
2013 \f/* Special code to pop-up a menu */
2014 void
2015 pop_up_menu (mw, event)
2016 XlwMenuWidget mw;
2017 XButtonPressedEvent* event;
2019 int x = event->x_root;
2020 int y = event->y_root;
2021 int w;
2022 int h;
2023 int borderwidth = mw->menu.shadow_thickness;
2024 Screen* screen = XtScreen (mw);
2025 Display *display = XtDisplay (mw);
2026 int count;
2028 next_release_must_exit = 0;
2030 XtCallCallbackList ((Widget)mw, mw->menu.open, NULL);
2032 if (XtIsShell (XtParent ((Widget)mw)))
2033 size_menu (mw, 0);
2035 w = mw->menu.windows [0].width;
2036 h = mw->menu.windows [0].height;
2038 x -= borderwidth;
2039 y -= borderwidth;
2040 if (x < borderwidth)
2041 x = borderwidth;
2042 if (x + w + 2 * borderwidth > WidthOfScreen (screen))
2043 x = WidthOfScreen (screen) - w - 2 * borderwidth;
2044 if (y < borderwidth)
2045 y = borderwidth;
2046 if (y + h + 2 * borderwidth> HeightOfScreen (screen))
2047 y = HeightOfScreen (screen) - h - 2 * borderwidth;
2049 mw->menu.popped_up = True;
2050 if (XtIsShell (XtParent ((Widget)mw)))
2052 XtConfigureWidget (XtParent ((Widget)mw), x, y, w, h,
2053 XtParent ((Widget)mw)->core.border_width);
2054 XtPopup (XtParent ((Widget)mw), XtGrabExclusive);
2055 display_menu (mw, 0, False, NULL, NULL, NULL, NULL, NULL);
2056 mw->menu.windows [0].x = x + borderwidth;
2057 mw->menu.windows [0].y = y + borderwidth;
2059 else
2061 XEvent *ev = (XEvent *) event;
2063 XtAddGrab ((Widget) mw, True, True);
2065 /* notes the absolute position of the menubar window */
2066 mw->menu.windows [0].x = ev->xmotion.x_root - ev->xmotion.x;
2067 mw->menu.windows [0].y = ev->xmotion.y_root - ev->xmotion.y;
2070 #ifdef emacs
2071 count = x_catch_errors (display);
2072 #endif
2073 XtGrabPointer ((Widget)mw, False,
2074 (PointerMotionMask
2075 | PointerMotionHintMask
2076 | ButtonReleaseMask
2077 | ButtonPressMask),
2078 GrabModeAsync, GrabModeAsync, None,
2079 mw->menu.cursor_shape,
2080 event->time);
2081 pointer_grabbed = 1;
2082 #ifdef emacs
2083 if (x_had_errors_p (display))
2085 pointer_grabbed = 0;
2086 XtUngrabPointer ((Widget)mw, event->time);
2088 x_uncatch_errors (display, count);
2089 #endif
2091 handle_motion_event (mw, (XMotionEvent*)event);