]> code.delx.au - gnu-emacs/blob - lwlib/xlwmenu.c
*** empty log message ***
[gnu-emacs] / lwlib / xlwmenu.c
1 /* Implements a lightweight menubar widget.
2 Copyright (C) 1992 Lucid, Inc.
3
4 This file is part of the Lucid Widget Library.
5
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.
10
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.
15
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. */
20
21 /* Created by devin@lucid.com */
22
23 #ifdef HAVE_CONFIG_H
24 #include <config.h>
25 #endif
26
27 #include "../src/lisp.h"
28
29 #include <stdio.h>
30
31 #include <sys/types.h>
32 #if (defined __sun) && !(defined SUNOS41)
33 #define SUNOS41
34 #include <X11/Xos.h>
35 #undef SUNOS41
36 #else
37 #include <X11/Xos.h>
38 #endif
39 #include <X11/IntrinsicP.h>
40 #include <X11/ObjectP.h>
41 #include <X11/StringDefs.h>
42 #include <X11/cursorfont.h>
43 #include "xlwmenuP.h"
44
45 #ifdef emacs
46
47 /* Defined in xfns.c. When config.h defines `static' as empty, we get
48 redefinition errors when gray_bitmap is included more than once, so
49 we're referring to the one include in xfns.c here. */
50
51 extern int gray_bitmap_width;
52 extern int gray_bitmap_height;
53 extern char *gray_bitmap_bits;
54
55 /* Defined in xterm.c. */
56 extern int x_alloc_nearest_color_for_widget __P ((Widget, Colormap, XColor*));
57 extern int x_alloc_lighter_color_for_widget __P ((Widget, Display*, Colormap,
58 unsigned long *,
59 double, int));
60 extern int x_catch_errors __P ((Display*));
61 extern int x_uncatch_errors __P ((Display*, int));
62 extern int x_had_errors_p __P ((Display*));
63 extern int x_clear_errors __P ((Display*));
64 extern unsigned long x_copy_dpy_color __P ((Display *, Colormap,
65 unsigned long));
66
67 /* Defined in xfaces.c. */
68 extern void x_free_dpy_colors __P ((Display *, Screen *, Colormap,
69 unsigned long *pixels, int npixels));
70 #else /* not emacs */
71
72 #include <X11/bitmaps/gray>
73 #define gray_bitmap_width gray_width
74 #define gray_bitmap_height gray_height
75 #define gray_bitmap_bits gray_bits
76
77 #endif /* not emacs */
78
79 static int pointer_grabbed;
80 static XEvent menu_post_event;
81
82 XFontStruct *xlwmenu_default_font;
83
84 static char
85 xlwMenuTranslations [] =
86 "<BtnDown>: start()\n\
87 <Motion>: drag()\n\
88 <BtnUp>: select()\n\
89 <Key>Shift_L: nothing()\n\
90 <Key>Shift_R: nothing()\n\
91 <Key>Meta_L: nothing()\n\
92 <Key>Meta_R: nothing()\n\
93 <Key>Control_L: nothing()\n\
94 <Key>Control_R: nothing()\n\
95 <Key>Hyper_L: nothing()\n\
96 <Key>Hyper_R: nothing()\n\
97 <Key>Super_L: nothing()\n\
98 <Key>Super_R: nothing()\n\
99 <Key>Alt_L: nothing()\n\
100 <Key>Alt_R: nothing()\n\
101 <Key>Caps_Lock: nothing()\n\
102 <Key>Shift_Lock: nothing()\n\
103 <KeyUp>Shift_L: nothing()\n\
104 <KeyUp>Shift_R: nothing()\n\
105 <KeyUp>Meta_L: nothing()\n\
106 <KeyUp>Meta_R: nothing()\n\
107 <KeyUp>Control_L: nothing()\n\
108 <KeyUp>Control_R: nothing()\n\
109 <KeyUp>Hyper_L: nothing()\n\
110 <KeyUp>Hyper_R: nothing()\n\
111 <KeyUp>Super_L: nothing()\n\
112 <KeyUp>Super_R: nothing()\n\
113 <KeyUp>Alt_L: nothing()\n\
114 <KeyUp>Alt_R: nothing()\n\
115 <KeyUp>Caps_Lock: nothing()\n\
116 <KeyUp>Shift_Lock:nothing()\n\
117 <Key>: key()\n\
118 <KeyUp>: key()\n\
119 ";
120
121 #define offset(field) XtOffset(XlwMenuWidget, field)
122 static XtResource
123 xlwMenuResources[] =
124 {
125 {XtNfont, XtCFont, XtRFontStruct, sizeof(XFontStruct *),
126 offset(menu.font),XtRString, "XtDefaultFont"},
127 {XtNforeground, XtCForeground, XtRPixel, sizeof(Pixel),
128 offset(menu.foreground), XtRString, "XtDefaultForeground"},
129 {XtNbuttonForeground, XtCButtonForeground, XtRPixel, sizeof(Pixel),
130 offset(menu.button_foreground), XtRString, "XtDefaultForeground"},
131 {XtNmargin, XtCMargin, XtRDimension, sizeof(Dimension),
132 offset(menu.margin), XtRImmediate, (XtPointer) 4},
133 {XtNhorizontalSpacing, XtCMargin, XtRDimension, sizeof(Dimension),
134 offset(menu.horizontal_spacing), XtRImmediate, (XtPointer)3},
135 {XtNverticalSpacing, XtCMargin, XtRDimension, sizeof(Dimension),
136 offset(menu.vertical_spacing), XtRImmediate, (XtPointer)1},
137 {XtNarrowSpacing, XtCMargin, XtRDimension, sizeof(Dimension),
138 offset(menu.arrow_spacing), XtRImmediate, (XtPointer)10},
139
140 {XmNshadowThickness, XmCShadowThickness, XtRDimension,
141 sizeof (Dimension), offset (menu.shadow_thickness),
142 XtRImmediate, (XtPointer) 2},
143 {XmNtopShadowColor, XmCTopShadowColor, XtRPixel, sizeof (Pixel),
144 offset (menu.top_shadow_color), XtRImmediate, (XtPointer)-1},
145 {XmNbottomShadowColor, XmCBottomShadowColor, XtRPixel, sizeof (Pixel),
146 offset (menu.bottom_shadow_color), XtRImmediate, (XtPointer)-1},
147 {XmNtopShadowPixmap, XmCTopShadowPixmap, XtRPixmap, sizeof (Pixmap),
148 offset (menu.top_shadow_pixmap), XtRImmediate, (XtPointer)None},
149 {XmNbottomShadowPixmap, XmCBottomShadowPixmap, XtRPixmap, sizeof (Pixmap),
150 offset (menu.bottom_shadow_pixmap), XtRImmediate, (XtPointer)None},
151
152 {XtNopen, XtCCallback, XtRCallback, sizeof(XtPointer),
153 offset(menu.open), XtRCallback, (XtPointer)NULL},
154 {XtNselect, XtCCallback, XtRCallback, sizeof(XtPointer),
155 offset(menu.select), XtRCallback, (XtPointer)NULL},
156 {XtNhighlightCallback, XtCCallback, XtRCallback, sizeof(XtPointer),
157 offset(menu.highlight), XtRCallback, (XtPointer)NULL},
158 {XtNmenu, XtCMenu, XtRPointer, sizeof(XtPointer),
159 offset(menu.contents), XtRImmediate, (XtPointer)NULL},
160 {XtNcursor, XtCCursor, XtRCursor, sizeof(Cursor),
161 offset(menu.cursor_shape), XtRString, (XtPointer)"right_ptr"},
162 {XtNhorizontal, XtCHorizontal, XtRInt, sizeof(int),
163 offset(menu.horizontal), XtRImmediate, (XtPointer)True},
164 };
165 #undef offset
166
167 static Boolean XlwMenuSetValues();
168 static void XlwMenuRealize();
169 static void XlwMenuRedisplay();
170 static void XlwMenuResize();
171 static void XlwMenuInitialize();
172 static void XlwMenuRedisplay();
173 static void XlwMenuDestroy();
174 static void XlwMenuClassInitialize();
175 static void Start();
176 static void Drag();
177 static void Select();
178 static void Key();
179 static void Nothing();
180 static int separator_height ();
181
182 static XtActionsRec
183 xlwMenuActionsList [] =
184 {
185 {"start", Start},
186 {"drag", Drag},
187 {"select", Select},
188 {"key", Key},
189 {"nothing", Nothing},
190 };
191
192 #define SuperClass ((CoreWidgetClass)&coreClassRec)
193
194 XlwMenuClassRec xlwMenuClassRec =
195 {
196 { /* CoreClass fields initialization */
197 (WidgetClass) SuperClass, /* superclass */
198 "XlwMenu", /* class_name */
199 sizeof(XlwMenuRec), /* size */
200 XlwMenuClassInitialize, /* class_initialize */
201 NULL, /* class_part_initialize */
202 FALSE, /* class_inited */
203 XlwMenuInitialize, /* initialize */
204 NULL, /* initialize_hook */
205 XlwMenuRealize, /* realize */
206 xlwMenuActionsList, /* actions */
207 XtNumber(xlwMenuActionsList), /* num_actions */
208 xlwMenuResources, /* resources */
209 XtNumber(xlwMenuResources), /* resource_count */
210 NULLQUARK, /* xrm_class */
211 TRUE, /* compress_motion */
212 TRUE, /* compress_exposure */
213 TRUE, /* compress_enterleave */
214 FALSE, /* visible_interest */
215 XlwMenuDestroy, /* destroy */
216 XlwMenuResize, /* resize */
217 XlwMenuRedisplay, /* expose */
218 XlwMenuSetValues, /* set_values */
219 NULL, /* set_values_hook */
220 XtInheritSetValuesAlmost, /* set_values_almost */
221 NULL, /* get_values_hook */
222 NULL, /* accept_focus */
223 XtVersion, /* version */
224 NULL, /* callback_private */
225 xlwMenuTranslations, /* tm_table */
226 XtInheritQueryGeometry, /* query_geometry */
227 XtInheritDisplayAccelerator, /* display_accelerator */
228 NULL /* extension */
229 }, /* XlwMenuClass fields initialization */
230 {
231 0 /* dummy */
232 },
233 };
234
235 WidgetClass xlwMenuWidgetClass = (WidgetClass) &xlwMenuClassRec;
236
237 int submenu_destroyed;
238
239 static int next_release_must_exit;
240
241 \f/* Utilities */
242
243
244 /* Like abort, but remove grabs from widget W before. */
245
246 static void
247 abort_gracefully (w)
248 Widget w;
249 {
250 if (XtIsShell (XtParent (w)))
251 XtRemoveGrab (w);
252 XtUngrabPointer (w, CurrentTime);
253 abort ();
254 }
255
256 static void
257 push_new_stack (mw, val)
258 XlwMenuWidget mw;
259 widget_value* val;
260 {
261 if (!mw->menu.new_stack)
262 {
263 mw->menu.new_stack_length = 10;
264 mw->menu.new_stack =
265 (widget_value**)XtCalloc (mw->menu.new_stack_length,
266 sizeof (widget_value*));
267 }
268 else if (mw->menu.new_depth == mw->menu.new_stack_length)
269 {
270 mw->menu.new_stack_length *= 2;
271 mw->menu.new_stack =
272 (widget_value**)XtRealloc ((char*)mw->menu.new_stack,
273 mw->menu.new_stack_length * sizeof (widget_value*));
274 }
275 mw->menu.new_stack [mw->menu.new_depth++] = val;
276 }
277
278 static void
279 pop_new_stack_if_no_contents (mw)
280 XlwMenuWidget mw;
281 {
282 if (mw->menu.new_depth)
283 {
284 if (!mw->menu.new_stack [mw->menu.new_depth - 1]->contents)
285 mw->menu.new_depth -= 1;
286 }
287 }
288
289 static void
290 make_old_stack_space (mw, n)
291 XlwMenuWidget mw;
292 int n;
293 {
294 if (!mw->menu.old_stack)
295 {
296 mw->menu.old_stack_length = 10;
297 mw->menu.old_stack =
298 (widget_value**)XtCalloc (mw->menu.old_stack_length,
299 sizeof (widget_value*));
300 }
301 else if (mw->menu.old_stack_length < n)
302 {
303 mw->menu.old_stack_length *= 2;
304 mw->menu.old_stack =
305 (widget_value**)XtRealloc ((char*)mw->menu.old_stack,
306 mw->menu.old_stack_length * sizeof (widget_value*));
307 }
308 }
309
310 \f/* Size code */
311 int
312 string_width (mw, s)
313 XlwMenuWidget mw;
314 char *s;
315 {
316 XCharStruct xcs;
317 int drop;
318
319 XTextExtents (mw->menu.font, s, strlen (s), &drop, &drop, &drop, &xcs);
320 return xcs.width;
321 }
322
323 static int
324 arrow_width (mw)
325 XlwMenuWidget mw;
326 {
327 return (mw->menu.font->ascent * 3/4) | 1;
328 }
329
330 /* Return the width of toggle buttons of widget MW. */
331
332 static int
333 toggle_button_width (mw)
334 XlwMenuWidget mw;
335 {
336 return ((mw->menu.font->ascent + mw->menu.font->descent) * 2 / 3) | 1;
337 }
338
339
340 /* Return the width of radio buttons of widget MW. */
341
342 static int
343 radio_button_width (mw)
344 XlwMenuWidget mw;
345 {
346 return toggle_button_width (mw) * 1.41;
347 }
348
349
350 static XtResource
351 nameResource[] =
352 {
353 {"labelString", "LabelString", XtRString, sizeof(String),
354 0, XtRImmediate, 0},
355 };
356
357 static char*
358 resource_widget_value (mw, val)
359 XlwMenuWidget mw;
360 widget_value *val;
361 {
362 if (!val->toolkit_data)
363 {
364 char* resourced_name = NULL;
365 char* complete_name;
366 XtGetSubresources ((Widget) mw,
367 (XtPointer) &resourced_name,
368 val->name, val->name,
369 nameResource, 1, NULL, 0);
370 if (!resourced_name)
371 resourced_name = val->name;
372 if (!val->value)
373 {
374 complete_name = (char *) XtMalloc (strlen (resourced_name) + 1);
375 strcpy (complete_name, resourced_name);
376 }
377 else
378 {
379 int complete_length =
380 strlen (resourced_name) + strlen (val->value) + 2;
381 complete_name = XtMalloc (complete_length);
382 *complete_name = 0;
383 strcat (complete_name, resourced_name);
384 strcat (complete_name, " ");
385 strcat (complete_name, val->value);
386 }
387
388 val->toolkit_data = complete_name;
389 val->free_toolkit_data = True;
390 }
391 return (char*)val->toolkit_data;
392 }
393
394 /* Returns the sizes of an item */
395 static void
396 size_menu_item (mw, val, horizontal_p, label_width, rest_width, button_width,
397 height)
398 XlwMenuWidget mw;
399 widget_value* val;
400 int horizontal_p;
401 int* label_width;
402 int* rest_width;
403 int* button_width;
404 int* height;
405 {
406 enum menu_separator separator;
407
408 if (lw_separator_p (val->name, &separator, 0))
409 {
410 *height = separator_height (separator);
411 *label_width = 1;
412 *rest_width = 0;
413 *button_width = 0;
414 }
415 else
416 {
417 *height =
418 mw->menu.font->ascent + mw->menu.font->descent
419 + 2 * mw->menu.vertical_spacing + 2 * mw->menu.shadow_thickness;
420
421 *label_width =
422 string_width (mw, resource_widget_value (mw, val))
423 + mw->menu.horizontal_spacing + mw->menu.shadow_thickness;
424
425 *rest_width = mw->menu.horizontal_spacing + mw->menu.shadow_thickness;
426 if (!horizontal_p)
427 {
428 if (val->contents)
429 /* Add width of the arrow displayed for submenus. */
430 *rest_width += arrow_width (mw) + mw->menu.arrow_spacing;
431 else if (val->key)
432 /* Add width of key equivalent string. */
433 *rest_width += (string_width (mw, val->key)
434 + mw->menu.arrow_spacing);
435
436 if (val->button_type == BUTTON_TYPE_TOGGLE)
437 *button_width = (toggle_button_width (mw)
438 + mw->menu.horizontal_spacing);
439 else if (val->button_type == BUTTON_TYPE_RADIO)
440 *button_width = (radio_button_width (mw)
441 + mw->menu.horizontal_spacing);
442 }
443 }
444 }
445
446 static void
447 size_menu (mw, level)
448 XlwMenuWidget mw;
449 int level;
450 {
451 unsigned int label_width = 0;
452 int rest_width = 0;
453 int button_width = 0;
454 int max_rest_width = 0;
455 int max_button_width = 0;
456 unsigned int height = 0;
457 int horizontal_p = mw->menu.horizontal && (level == 0);
458 widget_value* val;
459 window_state* ws;
460
461 if (level >= mw->menu.old_depth)
462 abort_gracefully ((Widget) mw);
463
464 ws = &mw->menu.windows [level];
465 ws->width = 0;
466 ws->height = 0;
467 ws->label_width = 0;
468 ws->button_width = 0;
469
470 for (val = mw->menu.old_stack [level]->contents; val; val = val->next)
471 {
472 size_menu_item (mw, val, horizontal_p, &label_width, &rest_width,
473 &button_width, &height);
474 if (horizontal_p)
475 {
476 ws->width += label_width + rest_width;
477 if (height > ws->height)
478 ws->height = height;
479 }
480 else
481 {
482 if (label_width > ws->label_width)
483 ws->label_width = label_width;
484 if (rest_width > max_rest_width)
485 max_rest_width = rest_width;
486 if (button_width > max_button_width)
487 max_button_width = button_width;
488 ws->height += height;
489 }
490 }
491
492 if (horizontal_p)
493 ws->label_width = ws->button_width = 0;
494 else
495 {
496 ws->width = ws->label_width + max_rest_width + max_button_width;
497 ws->button_width = max_button_width;
498 }
499
500 ws->width += 2 * mw->menu.shadow_thickness;
501 ws->height += 2 * mw->menu.shadow_thickness;
502
503 if (horizontal_p)
504 {
505 ws->width += 2 * mw->menu.margin;
506 ws->height += 2 * mw->menu.margin;
507 }
508 }
509
510
511 \f/* Display code */
512
513 static void
514 draw_arrow (mw, window, gc, x, y, width, down_p)
515 XlwMenuWidget mw;
516 Window window;
517 GC gc;
518 int x;
519 int y;
520 int width;
521 int down_p;
522 {
523 Display *dpy = XtDisplay (mw);
524 GC top_gc = mw->menu.shadow_top_gc;
525 GC bottom_gc = mw->menu.shadow_bottom_gc;
526 int thickness = mw->menu.shadow_thickness;
527 int height = width;
528 XPoint pt[10];
529 /* alpha = atan (0.5)
530 factor = (1 + sin (alpha)) / cos (alpha) */
531 double factor = 1.62;
532 int thickness2 = thickness * factor;
533
534 y += (mw->menu.font->ascent + mw->menu.font->descent - height) / 2;
535
536 if (down_p)
537 {
538 GC temp;
539 temp = top_gc;
540 top_gc = bottom_gc;
541 bottom_gc = temp;
542 }
543
544 pt[0].x = x;
545 pt[0].y = y + height;
546 pt[1].x = x + thickness;
547 pt[1].y = y + height - thickness2;
548 pt[2].x = x + thickness2;
549 pt[2].y = y + thickness2;
550 pt[3].x = x;
551 pt[3].y = y;
552 XFillPolygon (dpy, window, top_gc, pt, 4, Convex, CoordModeOrigin);
553
554 pt[0].x = x;
555 pt[0].y = y;
556 pt[1].x = x + thickness;
557 pt[1].y = y + thickness2;
558 pt[2].x = x + width - thickness2;
559 pt[2].y = y + height / 2;
560 pt[3].x = x + width;
561 pt[3].y = y + height / 2;
562 XFillPolygon (dpy, window, top_gc, pt, 4, Convex, CoordModeOrigin);
563
564 pt[0].x = x;
565 pt[0].y = y + height;
566 pt[1].x = x + thickness;
567 pt[1].y = y + height - thickness2;
568 pt[2].x = x + width - thickness2;
569 pt[2].y = y + height / 2;
570 pt[3].x = x + width;
571 pt[3].y = y + height / 2;
572 XFillPolygon (dpy, window, bottom_gc, pt, 4, Convex, CoordModeOrigin);
573 }
574
575
576
577 static void
578 draw_shadow_rectangle (mw, window, x, y, width, height, erase_p, down_p)
579 XlwMenuWidget mw;
580 Window window;
581 int x;
582 int y;
583 int width;
584 int height;
585 int erase_p;
586 int down_p;
587 {
588 Display *dpy = XtDisplay (mw);
589 GC top_gc = !erase_p ? mw->menu.shadow_top_gc : mw->menu.background_gc;
590 GC bottom_gc = !erase_p ? mw->menu.shadow_bottom_gc : mw->menu.background_gc;
591 int thickness = mw->menu.shadow_thickness;
592 XPoint points [4];
593
594 if (!erase_p && down_p)
595 {
596 GC temp;
597 temp = top_gc;
598 top_gc = bottom_gc;
599 bottom_gc = temp;
600 }
601
602 points [0].x = x;
603 points [0].y = y;
604 points [1].x = x + width;
605 points [1].y = y;
606 points [2].x = x + width - thickness;
607 points [2].y = y + thickness;
608 points [3].x = x;
609 points [3].y = y + thickness;
610 XFillPolygon (dpy, window, top_gc, points, 4, Convex, CoordModeOrigin);
611 points [0].x = x;
612 points [0].y = y + thickness;
613 points [1].x = x;
614 points [1].y = y + height;
615 points [2].x = x + thickness;
616 points [2].y = y + height - thickness;
617 points [3].x = x + thickness;
618 points [3].y = y + thickness;
619 XFillPolygon (dpy, window, top_gc, points, 4, Convex, CoordModeOrigin);
620 points [0].x = x + width;
621 points [0].y = y;
622 points [1].x = x + width - thickness;
623 points [1].y = y + thickness;
624 points [2].x = x + width - thickness;
625 points [2].y = y + height - thickness;
626 points [3].x = x + width;
627 points [3].y = y + height - thickness;
628 XFillPolygon (dpy, window, bottom_gc, points, 4, Convex, CoordModeOrigin);
629 points [0].x = x;
630 points [0].y = y + height;
631 points [1].x = x + width;
632 points [1].y = y + height;
633 points [2].x = x + width;
634 points [2].y = y + height - thickness;
635 points [3].x = x + thickness;
636 points [3].y = y + height - thickness;
637 XFillPolygon (dpy, window, bottom_gc, points, 4, Convex, CoordModeOrigin);
638 }
639
640
641 static void
642 draw_shadow_rhombus (mw, window, x, y, width, height, erase_p, down_p)
643 XlwMenuWidget mw;
644 Window window;
645 int x;
646 int y;
647 int width;
648 int height;
649 int erase_p;
650 int down_p;
651 {
652 Display *dpy = XtDisplay (mw);
653 GC top_gc = !erase_p ? mw->menu.shadow_top_gc : mw->menu.background_gc;
654 GC bottom_gc = !erase_p ? mw->menu.shadow_bottom_gc : mw->menu.background_gc;
655 int thickness = mw->menu.shadow_thickness;
656 XPoint points [4];
657
658 if (!erase_p && down_p)
659 {
660 GC temp;
661 temp = top_gc;
662 top_gc = bottom_gc;
663 bottom_gc = temp;
664 }
665
666 points [0].x = x;
667 points [0].y = y + height / 2;
668 points [1].x = x + thickness;
669 points [1].y = y + height / 2;
670 points [2].x = x + width / 2;
671 points [2].y = y + thickness;
672 points [3].x = x + width / 2;
673 points [3].y = y;
674 XFillPolygon (dpy, window, top_gc, points, 4, Convex, CoordModeOrigin);
675 points [0].x = x + width / 2;
676 points [0].y = y;
677 points [1].x = x + width / 2;
678 points [1].y = y + thickness;
679 points [2].x = x + width - thickness;
680 points [2].y = y + height / 2;
681 points [3].x = x + width;
682 points [3].y = y + height / 2;
683 XFillPolygon (dpy, window, top_gc, points, 4, Convex, CoordModeOrigin);
684 points [0].x = x;
685 points [0].y = y + height / 2;
686 points [1].x = x + thickness;
687 points [1].y = y + height / 2;
688 points [2].x = x + width / 2;
689 points [2].y = y + height - thickness;
690 points [3].x = x + width / 2;
691 points [3].y = y + height;
692 XFillPolygon (dpy, window, bottom_gc, points, 4, Convex, CoordModeOrigin);
693 points [0].x = x + width / 2;
694 points [0].y = y + height;
695 points [1].x = x + width / 2;
696 points [1].y = y + height - thickness;
697 points [2].x = x + width - thickness;
698 points [2].y = y + height / 2;
699 points [3].x = x + width;
700 points [3].y = y + height / 2;
701 XFillPolygon (dpy, window, bottom_gc, points, 4, Convex, CoordModeOrigin);
702 }
703
704
705 /* Draw a toggle button on widget MW, X window WINDOW. X/Y is the
706 top-left corner of the menu item. SELECTED_P non-zero means the
707 toggle button is selected. */
708
709 static void
710 draw_toggle (mw, window, x, y, selected_p)
711 XlwMenuWidget mw;
712 Window window;
713 int x, y, selected_p;
714 {
715 int width, height;
716
717 width = toggle_button_width (mw);
718 height = width;
719 x += mw->menu.horizontal_spacing;
720 y += (mw->menu.font->ascent - height) / 2;
721 draw_shadow_rectangle (mw, window, x, y, width, height, False, selected_p);
722 }
723
724
725 /* Draw a radio button on widget MW, X window WINDOW. X/Y is the
726 top-left corner of the menu item. SELECTED_P non-zero means the
727 toggle button is selected. */
728
729 static void
730 draw_radio (mw, window, x, y, selected_p)
731 XlwMenuWidget mw;
732 Window window;
733 int x, y, selected_p;
734 {
735 int width, height;
736
737 width = radio_button_width (mw);
738 height = width;
739 x += mw->menu.horizontal_spacing;
740 y += (mw->menu.font->ascent - height) / 2;
741 draw_shadow_rhombus (mw, window, x, y, width, height, False, selected_p);
742 }
743
744
745 /* Draw a menu separator on widget MW, X window WINDOW. X/Y is the
746 top-left corner of the menu item. WIDTH is the width of the
747 separator to draw. TYPE is the separator type. */
748
749 static void
750 draw_separator (mw, window, x, y, width, type)
751 XlwMenuWidget mw;
752 Window window;
753 int x, y, width;
754 enum menu_separator type;
755 {
756 Display *dpy = XtDisplay (mw);
757 XGCValues xgcv;
758
759 switch (type)
760 {
761 case SEPARATOR_NO_LINE:
762 break;
763
764 case SEPARATOR_SINGLE_LINE:
765 XDrawLine (dpy, window, mw->menu.foreground_gc,
766 x, y, x + width, y);
767 break;
768
769 case SEPARATOR_DOUBLE_LINE:
770 draw_separator (mw, window, x, y, width, SEPARATOR_SINGLE_LINE);
771 draw_separator (mw, window, x, y + 2, width, SEPARATOR_SINGLE_LINE);
772 break;
773
774 case SEPARATOR_SINGLE_DASHED_LINE:
775 xgcv.line_style = LineOnOffDash;
776 XChangeGC (dpy, mw->menu.foreground_gc, GCLineStyle, &xgcv);
777 XDrawLine (dpy, window, mw->menu.foreground_gc,
778 x, y, x + width, y);
779 xgcv.line_style = LineSolid;
780 XChangeGC (dpy, mw->menu.foreground_gc, GCLineStyle, &xgcv);
781 break;
782
783 case SEPARATOR_DOUBLE_DASHED_LINE:
784 draw_separator (mw, window, x, y, width,
785 SEPARATOR_SINGLE_DASHED_LINE);
786 draw_separator (mw, window, x, y + 2, width,
787 SEPARATOR_SINGLE_DASHED_LINE);
788 break;
789
790 case SEPARATOR_SHADOW_ETCHED_IN:
791 XDrawLine (dpy, window, mw->menu.shadow_bottom_gc,
792 x, y, x + width, y);
793 XDrawLine (dpy, window, mw->menu.shadow_top_gc,
794 x, y + 1, x + width, y + 1);
795 break;
796
797 case SEPARATOR_SHADOW_ETCHED_OUT:
798 XDrawLine (dpy, window, mw->menu.shadow_top_gc,
799 x, y, x + width, y);
800 XDrawLine (dpy, window, mw->menu.shadow_bottom_gc,
801 x, y + 1, x + width, y + 1);
802 break;
803
804 case SEPARATOR_SHADOW_ETCHED_IN_DASH:
805 xgcv.line_style = LineOnOffDash;
806 XChangeGC (dpy, mw->menu.shadow_bottom_gc, GCLineStyle, &xgcv);
807 XChangeGC (dpy, mw->menu.shadow_top_gc, GCLineStyle, &xgcv);
808 draw_separator (mw, window, x, y, width, SEPARATOR_SHADOW_ETCHED_IN);
809 xgcv.line_style = LineSolid;
810 XChangeGC (dpy, mw->menu.shadow_bottom_gc, GCLineStyle, &xgcv);
811 XChangeGC (dpy, mw->menu.shadow_top_gc, GCLineStyle, &xgcv);
812 break;
813
814 case SEPARATOR_SHADOW_ETCHED_OUT_DASH:
815 xgcv.line_style = LineOnOffDash;
816 XChangeGC (dpy, mw->menu.shadow_bottom_gc, GCLineStyle, &xgcv);
817 XChangeGC (dpy, mw->menu.shadow_top_gc, GCLineStyle, &xgcv);
818 draw_separator (mw, window, x, y, width, SEPARATOR_SHADOW_ETCHED_OUT);
819 xgcv.line_style = LineSolid;
820 XChangeGC (dpy, mw->menu.shadow_bottom_gc, GCLineStyle, &xgcv);
821 XChangeGC (dpy, mw->menu.shadow_top_gc, GCLineStyle, &xgcv);
822 break;
823
824 case SEPARATOR_SHADOW_DOUBLE_ETCHED_IN:
825 draw_separator (mw, window, x, y, width, SEPARATOR_SHADOW_ETCHED_IN);
826 draw_separator (mw, window, x, y + 3, width, SEPARATOR_SHADOW_ETCHED_IN);
827 break;
828
829 case SEPARATOR_SHADOW_DOUBLE_ETCHED_OUT:
830 draw_separator (mw, window, x, y, width,
831 SEPARATOR_SHADOW_ETCHED_OUT);
832 draw_separator (mw, window, x, y + 3, width,
833 SEPARATOR_SHADOW_ETCHED_OUT);
834 break;
835
836 case SEPARATOR_SHADOW_DOUBLE_ETCHED_IN_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_IN);
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;
846
847 case SEPARATOR_SHADOW_DOUBLE_ETCHED_OUT_DASH:
848 xgcv.line_style = LineOnOffDash;
849 XChangeGC (dpy, mw->menu.shadow_bottom_gc, GCLineStyle, &xgcv);
850 XChangeGC (dpy, mw->menu.shadow_top_gc, GCLineStyle, &xgcv);
851 draw_separator (mw, window, x, y, width,
852 SEPARATOR_SHADOW_DOUBLE_ETCHED_OUT);
853 xgcv.line_style = LineSolid;
854 XChangeGC (dpy, mw->menu.shadow_bottom_gc, GCLineStyle, &xgcv);
855 XChangeGC (dpy, mw->menu.shadow_top_gc, GCLineStyle, &xgcv);
856 break;
857
858 default:
859 abort ();
860 }
861 }
862
863
864 /* Return the pixel height of menu separator SEPARATOR. */
865
866 static int
867 separator_height (separator)
868 enum menu_separator separator;
869 {
870 switch (separator)
871 {
872 case SEPARATOR_NO_LINE:
873 return 2;
874
875 case SEPARATOR_SINGLE_LINE:
876 case SEPARATOR_SINGLE_DASHED_LINE:
877 return 1;
878
879 case SEPARATOR_DOUBLE_LINE:
880 case SEPARATOR_DOUBLE_DASHED_LINE:
881 return 3;
882
883 case SEPARATOR_SHADOW_ETCHED_IN:
884 case SEPARATOR_SHADOW_ETCHED_OUT:
885 case SEPARATOR_SHADOW_ETCHED_IN_DASH:
886 case SEPARATOR_SHADOW_ETCHED_OUT_DASH:
887 return 2;
888
889 case SEPARATOR_SHADOW_DOUBLE_ETCHED_IN:
890 case SEPARATOR_SHADOW_DOUBLE_ETCHED_OUT:
891 case SEPARATOR_SHADOW_DOUBLE_ETCHED_IN_DASH:
892 case SEPARATOR_SHADOW_DOUBLE_ETCHED_OUT_DASH:
893 return 5;
894
895 default:
896 abort ();
897 }
898 }
899
900
901 /* Display the menu item and increment where.x and where.y to show how large
902 the menu item was. */
903
904 static void
905 display_menu_item (mw, val, ws, where, highlighted_p, horizontal_p,
906 just_compute_p)
907 XlwMenuWidget mw;
908 widget_value* val;
909 window_state* ws;
910 XPoint* where;
911 Boolean highlighted_p;
912 Boolean horizontal_p;
913 Boolean just_compute_p;
914 {
915 GC deco_gc;
916 GC text_gc;
917 int font_ascent = mw->menu.font->ascent;
918 int font_descent = mw->menu.font->descent;
919 int shadow = mw->menu.shadow_thickness;
920 int margin = mw->menu.margin;
921 int h_spacing = mw->menu.horizontal_spacing;
922 int v_spacing = mw->menu.vertical_spacing;
923 int label_width;
924 int rest_width;
925 int button_width;
926 int height;
927 int width;
928 enum menu_separator separator;
929 int separator_p = lw_separator_p (val->name, &separator, 0);
930
931 /* compute the sizes of the item */
932 size_menu_item (mw, val, horizontal_p, &label_width, &rest_width,
933 &button_width, &height);
934
935 if (horizontal_p)
936 width = label_width + rest_width;
937 else
938 {
939 label_width = ws->label_width;
940 width = ws->width - 2 * shadow;
941 }
942
943 /* Only highlight an enabled item that has a callback. */
944 if (highlighted_p)
945 if (!val->enabled || !(val->call_data || val->contents))
946 highlighted_p = 0;
947
948 /* do the drawing. */
949 if (!just_compute_p)
950 {
951 /* Add the shadow border of the containing menu */
952 int x = where->x + shadow;
953 int y = where->y + shadow;
954
955 if (horizontal_p)
956 {
957 x += margin;
958 y += margin;
959 }
960
961 /* pick the foreground and background GC. */
962 if (val->enabled)
963 text_gc = mw->menu.foreground_gc;
964 else
965 text_gc = mw->menu.inactive_gc;
966 deco_gc = mw->menu.foreground_gc;
967
968 if (separator_p)
969 {
970 draw_separator (mw, ws->window, x, y, width, separator);
971 }
972 else
973 {
974 int x_offset = x + h_spacing + shadow;
975 char* display_string = resource_widget_value (mw, val);
976 draw_shadow_rectangle (mw, ws->window, x, y, width, height, True,
977 False);
978
979 /* Deal with centering a menu title. */
980 if (!horizontal_p && !val->contents && !val->call_data)
981 {
982 int l = string_width (mw, display_string);
983
984 if (width > l)
985 x_offset = (width - l) >> 1;
986 }
987 else if (!horizontal_p && ws->button_width)
988 x_offset += ws->button_width;
989
990
991 XDrawString (XtDisplay (mw), ws->window, text_gc, x_offset,
992 y + v_spacing + shadow + font_ascent,
993 display_string, strlen (display_string));
994
995 if (!horizontal_p)
996 {
997 if (val->button_type == BUTTON_TYPE_TOGGLE)
998 draw_toggle (mw, ws->window, x, y + v_spacing + shadow,
999 val->selected);
1000 else if (val->button_type == BUTTON_TYPE_RADIO)
1001 draw_radio (mw, ws->window, x, y + v_spacing + shadow,
1002 val->selected);
1003
1004 if (val->contents)
1005 {
1006 int a_w = arrow_width (mw);
1007 draw_arrow (mw, ws->window, deco_gc,
1008 x + width - a_w
1009 - mw->menu.horizontal_spacing
1010 - mw->menu.shadow_thickness,
1011 y + v_spacing + shadow, a_w,
1012 highlighted_p);
1013 }
1014 else if (val->key)
1015 {
1016 XDrawString (XtDisplay (mw), ws->window, text_gc,
1017 x + label_width + mw->menu.arrow_spacing,
1018 y + v_spacing + shadow + font_ascent,
1019 val->key, strlen (val->key));
1020 }
1021 }
1022 else
1023 {
1024 XDrawRectangle (XtDisplay (mw), ws->window,
1025 mw->menu.background_gc,
1026 x + shadow, y + shadow,
1027 label_width + h_spacing - 1,
1028 font_ascent + font_descent + 2 * v_spacing - 1);
1029 draw_shadow_rectangle (mw, ws->window, x, y, width, height,
1030 True, False);
1031 }
1032
1033 if (highlighted_p)
1034 draw_shadow_rectangle (mw, ws->window, x, y, width, height, False,
1035 False);
1036 }
1037 }
1038
1039 where->x += width;
1040 where->y += height;
1041 }
1042
1043 static void
1044 display_menu (mw, level, just_compute_p, highlighted_pos, hit, hit_return,
1045 this, that)
1046 XlwMenuWidget mw;
1047 int level;
1048 Boolean just_compute_p;
1049 XPoint* highlighted_pos;
1050 XPoint* hit;
1051 widget_value** hit_return;
1052 widget_value* this;
1053 widget_value* that;
1054 {
1055 widget_value* val;
1056 widget_value* following_item;
1057 window_state* ws;
1058 XPoint where;
1059 int horizontal_p = mw->menu.horizontal && (level == 0);
1060 int highlighted_p;
1061 int just_compute_this_one_p;
1062 /* This is set nonzero if the element containing HIGHLIGHTED_POS
1063 is disabled, so that we do not return any subsequent element either. */
1064 int no_return = 0;
1065 enum menu_separator separator;
1066
1067 if (level >= mw->menu.old_depth)
1068 abort_gracefully ((Widget) mw);
1069
1070 if (level < mw->menu.old_depth - 1)
1071 following_item = mw->menu.old_stack [level + 1];
1072 else
1073 following_item = NULL;
1074
1075 if (hit)
1076 *hit_return = NULL;
1077
1078 where.x = 0;
1079 where.y = 0;
1080
1081 ws = &mw->menu.windows [level];
1082 for (val = mw->menu.old_stack [level]->contents; val; val = val->next)
1083 {
1084 highlighted_p = val == following_item;
1085 if (highlighted_p && highlighted_pos)
1086 {
1087 if (horizontal_p)
1088 highlighted_pos->x = where.x;
1089 else
1090 highlighted_pos->y = where.y;
1091 }
1092
1093 just_compute_this_one_p =
1094 just_compute_p || ((this || that) && val != this && val != that);
1095
1096 display_menu_item (mw, val, ws, &where, highlighted_p, horizontal_p,
1097 just_compute_this_one_p);
1098
1099 if (highlighted_p && highlighted_pos)
1100 {
1101 if (horizontal_p)
1102 highlighted_pos->y = where.y;
1103 else
1104 highlighted_pos->x = where.x;
1105 }
1106
1107 if (hit
1108 && !*hit_return
1109 && (horizontal_p ? hit->x < where.x : hit->y < where.y)
1110 && !lw_separator_p (val->name, &separator, 0)
1111 && !no_return)
1112 {
1113 if (val->enabled)
1114 *hit_return = val;
1115 else
1116 no_return = 1;
1117 }
1118
1119 if (horizontal_p)
1120 where.y = 0;
1121 else
1122 where.x = 0;
1123 }
1124
1125 if (!just_compute_p)
1126 draw_shadow_rectangle (mw, ws->window, 0, 0, ws->width, ws->height,
1127 False, False);
1128 }
1129
1130 \f/* Motion code */
1131 static void
1132 set_new_state (mw, val, level)
1133 XlwMenuWidget mw;
1134 widget_value* val;
1135 int level;
1136 {
1137 int i;
1138
1139 mw->menu.new_depth = 0;
1140 for (i = 0; i < level; i++)
1141 push_new_stack (mw, mw->menu.old_stack [i]);
1142 push_new_stack (mw, val);
1143 }
1144
1145 static void
1146 make_windows_if_needed (mw, n)
1147 XlwMenuWidget mw;
1148 int n;
1149 {
1150 int i;
1151 int start_at;
1152 XSetWindowAttributes xswa;
1153 int mask;
1154 Window root = RootWindowOfScreen (DefaultScreenOfDisplay (XtDisplay (mw)));
1155 window_state* windows;
1156
1157 if (mw->menu.windows_length >= n)
1158 return;
1159
1160 xswa.save_under = True;
1161 xswa.override_redirect = True;
1162 xswa.background_pixel = mw->core.background_pixel;
1163 xswa.border_pixel = mw->core.border_pixel;
1164 xswa.event_mask =
1165 ExposureMask | PointerMotionMask | PointerMotionHintMask
1166 | ButtonReleaseMask | ButtonPressMask;
1167 xswa.cursor = mw->menu.cursor_shape;
1168 mask = CWSaveUnder | CWOverrideRedirect | CWBackPixel | CWBorderPixel
1169 | CWEventMask | CWCursor;
1170
1171 if (!mw->menu.windows)
1172 {
1173 mw->menu.windows =
1174 (window_state*)XtMalloc (n * sizeof (window_state));
1175 start_at = 0;
1176 }
1177 else
1178 {
1179 mw->menu.windows =
1180 (window_state*)XtRealloc ((char*)mw->menu.windows,
1181 n * sizeof (window_state));
1182 start_at = mw->menu.windows_length;
1183 }
1184 mw->menu.windows_length = n;
1185
1186 windows = mw->menu.windows;
1187
1188 for (i = start_at; i < n; i++)
1189 {
1190 windows [i].x = 0;
1191 windows [i].y = 0;
1192 windows [i].width = 1;
1193 windows [i].height = 1;
1194 windows [i].window =
1195 XCreateWindow (XtDisplay (mw), root, 0, 0, 1, 1,
1196 0, 0, CopyFromParent, CopyFromParent, mask, &xswa);
1197 }
1198 }
1199
1200 /* Value is non-zero if WINDOW is part of menu bar widget W. */
1201
1202 int
1203 xlwmenu_window_p (w, window)
1204 Widget w;
1205 Window window;
1206 {
1207 XlwMenuWidget mw = (XlwMenuWidget) w;
1208 int i;
1209
1210 for (i = 0; i < mw->menu.windows_length; ++i)
1211 if (window == mw->menu.windows[i].window)
1212 break;
1213
1214 return i < mw->menu.windows_length;
1215 }
1216
1217 /* Make the window fit in the screen */
1218 static void
1219 fit_to_screen (mw, ws, previous_ws, horizontal_p)
1220 XlwMenuWidget mw;
1221 window_state* ws;
1222 window_state* previous_ws;
1223 Boolean horizontal_p;
1224 {
1225 unsigned int screen_width = WidthOfScreen (XtScreen (mw));
1226 unsigned int screen_height = HeightOfScreen (XtScreen (mw));
1227 /* 1 if we are unable to avoid an overlap between
1228 this menu and the parent menu in the X dimension. */
1229 int horizontal_overlap = 0;
1230
1231 if (ws->x < 0)
1232 ws->x = 0;
1233 else if (ws->x + ws->width > screen_width)
1234 {
1235 if (!horizontal_p)
1236 /* The addition of shadow-thickness for a sub-menu's position is
1237 to reflect a similar adjustment when the menu is displayed to
1238 the right of the invoking menu-item; it makes the sub-menu
1239 look more `attached' to the menu-item. */
1240 ws->x = previous_ws->x - ws->width + mw->menu.shadow_thickness;
1241 else
1242 ws->x = screen_width - ws->width;
1243 if (ws->x < 0)
1244 {
1245 ws->x = 0;
1246 horizontal_overlap = 1;
1247 }
1248 }
1249 /* If we overlap in X, try to avoid overlap in Y. */
1250 if (horizontal_overlap
1251 && ws->y < previous_ws->y + previous_ws->height
1252 && previous_ws->y < ws->y + ws->height)
1253 {
1254 /* Put this menu right below or right above PREVIOUS_WS
1255 if there's room. */
1256 if (previous_ws->y + previous_ws->height + ws->height < screen_height)
1257 ws->y = previous_ws->y + previous_ws->height;
1258 else if (previous_ws->y - ws->height > 0)
1259 ws->y = previous_ws->y - ws->height;
1260 }
1261
1262 if (ws->y < 0)
1263 ws->y = 0;
1264 else if (ws->y + ws->height > screen_height)
1265 {
1266 if (horizontal_p)
1267 ws->y = previous_ws->y - ws->height;
1268 else
1269 ws->y = screen_height - ws->height;
1270 if (ws->y < 0)
1271 ws->y = 0;
1272 }
1273 }
1274
1275 /* Updates old_stack from new_stack and redisplays. */
1276 static void
1277 remap_menubar (mw)
1278 XlwMenuWidget mw;
1279 {
1280 int i;
1281 int last_same;
1282 XPoint selection_position;
1283 int old_depth = mw->menu.old_depth;
1284 int new_depth = mw->menu.new_depth;
1285 widget_value** old_stack;
1286 widget_value** new_stack;
1287 window_state* windows;
1288 widget_value* old_selection;
1289 widget_value* new_selection;
1290
1291 /* Check that enough windows and old_stack are ready. */
1292 make_windows_if_needed (mw, new_depth);
1293 make_old_stack_space (mw, new_depth);
1294 windows = mw->menu.windows;
1295 old_stack = mw->menu.old_stack;
1296 new_stack = mw->menu.new_stack;
1297
1298 /* compute the last identical different entry */
1299 for (i = 1; i < old_depth && i < new_depth; i++)
1300 if (old_stack [i] != new_stack [i])
1301 break;
1302 last_same = i - 1;
1303
1304 /* Memorize the previously selected item to be able to refresh it */
1305 old_selection = last_same + 1 < old_depth ? old_stack [last_same + 1] : NULL;
1306 if (old_selection && !old_selection->enabled)
1307 old_selection = NULL;
1308 new_selection = last_same + 1 < new_depth ? new_stack [last_same + 1] : NULL;
1309 if (new_selection && !new_selection->enabled)
1310 new_selection = NULL;
1311
1312 /* Call callback when the hightlighted item changes. */
1313 if (old_selection || new_selection)
1314 XtCallCallbackList ((Widget)mw, mw->menu.highlight,
1315 (XtPointer) new_selection);
1316
1317 /* updates old_state from new_state. It has to be done now because
1318 display_menu (called below) uses the old_stack to know what to display. */
1319 for (i = last_same + 1; i < new_depth; i++)
1320 old_stack [i] = new_stack [i];
1321 mw->menu.old_depth = new_depth;
1322
1323 /* refresh the last selection */
1324 selection_position.x = 0;
1325 selection_position.y = 0;
1326 display_menu (mw, last_same, new_selection == old_selection,
1327 &selection_position, NULL, NULL, old_selection, new_selection);
1328
1329 /* Now place the new menus. */
1330 for (i = last_same + 1; i < new_depth && new_stack[i]->contents; i++)
1331 {
1332 window_state *previous_ws = &windows[i - 1];
1333 window_state *ws = &windows[i];
1334
1335 ws->x = (previous_ws->x + selection_position.x
1336 + mw->menu.shadow_thickness);
1337 if (mw->menu.horizontal && i == 1)
1338 ws->x += mw->menu.margin;
1339
1340 #if 0
1341 if (!mw->menu.horizontal || i > 1)
1342 ws->x += mw->menu.shadow_thickness;
1343 #endif
1344
1345 ws->y = (previous_ws->y + selection_position.y
1346 + mw->menu.shadow_thickness);
1347 if (mw->menu.horizontal && i == 1)
1348 ws->y += mw->menu.margin;
1349
1350 size_menu (mw, i);
1351
1352 fit_to_screen (mw, ws, previous_ws, mw->menu.horizontal && i == 1);
1353
1354 XClearWindow (XtDisplay (mw), ws->window);
1355 XMoveResizeWindow (XtDisplay (mw), ws->window, ws->x, ws->y,
1356 ws->width, ws->height);
1357 XMapRaised (XtDisplay (mw), ws->window);
1358 display_menu (mw, i, False, &selection_position, NULL, NULL, NULL, NULL);
1359 }
1360
1361 /* unmap the menus that popped down */
1362 for (i = new_depth - 1; i < old_depth; i++)
1363 if (i >= new_depth || !new_stack[i]->contents)
1364 XUnmapWindow (XtDisplay (mw), windows[i].window);
1365 }
1366
1367 static Boolean
1368 motion_event_is_in_menu (mw, ev, level, relative_pos)
1369 XlwMenuWidget mw;
1370 XMotionEvent* ev;
1371 int level;
1372 XPoint* relative_pos;
1373 {
1374 window_state* ws = &mw->menu.windows [level];
1375 int shadow = level == 0 ? 0 : mw->menu.shadow_thickness;
1376 int x = ws->x + shadow;
1377 int y = ws->y + shadow;
1378 relative_pos->x = ev->x_root - x;
1379 relative_pos->y = ev->y_root - y;
1380 return (x - shadow < ev->x_root && ev->x_root < x + ws->width
1381 && y - shadow < ev->y_root && ev->y_root < y + ws->height);
1382 }
1383
1384 static Boolean
1385 map_event_to_widget_value (mw, ev, val, level)
1386 XlwMenuWidget mw;
1387 XMotionEvent* ev;
1388 widget_value** val;
1389 int* level;
1390 {
1391 int i;
1392 XPoint relative_pos;
1393 window_state* ws;
1394
1395 *val = NULL;
1396
1397 /* Find the window */
1398 for (i = mw->menu.old_depth - 1; i >= 0; i--)
1399 {
1400 ws = &mw->menu.windows [i];
1401 if (ws && motion_event_is_in_menu (mw, ev, i, &relative_pos))
1402 {
1403 display_menu (mw, i, True, NULL, &relative_pos, val, NULL, NULL);
1404
1405 if (*val)
1406 {
1407 *level = i + 1;
1408 return True;
1409 }
1410 }
1411 }
1412 return False;
1413 }
1414
1415 \f/* Procedures */
1416 static void
1417 make_drawing_gcs (mw)
1418 XlwMenuWidget mw;
1419 {
1420 XGCValues xgcv;
1421
1422 xgcv.font = mw->menu.font->fid;
1423 xgcv.foreground = mw->menu.foreground;
1424 xgcv.background = mw->core.background_pixel;
1425 mw->menu.foreground_gc = XtGetGC ((Widget)mw,
1426 GCFont | GCForeground | GCBackground,
1427 &xgcv);
1428
1429 xgcv.font = mw->menu.font->fid;
1430 xgcv.foreground = mw->menu.button_foreground;
1431 xgcv.background = mw->core.background_pixel;
1432 mw->menu.button_gc = XtGetGC ((Widget)mw,
1433 GCFont | GCForeground | GCBackground,
1434 &xgcv);
1435
1436 xgcv.font = mw->menu.font->fid;
1437 xgcv.foreground = mw->menu.foreground;
1438 xgcv.background = mw->core.background_pixel;
1439 xgcv.fill_style = FillStippled;
1440 xgcv.stipple = mw->menu.gray_pixmap;
1441 mw->menu.inactive_gc = XtGetGC ((Widget)mw,
1442 (GCFont | GCForeground | GCBackground
1443 | GCFillStyle | GCStipple), &xgcv);
1444
1445 xgcv.font = mw->menu.font->fid;
1446 xgcv.foreground = mw->menu.button_foreground;
1447 xgcv.background = mw->core.background_pixel;
1448 xgcv.fill_style = FillStippled;
1449 xgcv.stipple = mw->menu.gray_pixmap;
1450 mw->menu.inactive_button_gc = XtGetGC ((Widget)mw,
1451 (GCFont | GCForeground | GCBackground
1452 | GCFillStyle | GCStipple), &xgcv);
1453
1454 xgcv.font = mw->menu.font->fid;
1455 xgcv.foreground = mw->core.background_pixel;
1456 xgcv.background = mw->menu.foreground;
1457 mw->menu.background_gc = XtGetGC ((Widget)mw,
1458 GCFont | GCForeground | GCBackground,
1459 &xgcv);
1460 }
1461
1462 static void
1463 release_drawing_gcs (mw)
1464 XlwMenuWidget mw;
1465 {
1466 XtReleaseGC ((Widget) mw, mw->menu.foreground_gc);
1467 XtReleaseGC ((Widget) mw, mw->menu.button_gc);
1468 XtReleaseGC ((Widget) mw, mw->menu.inactive_gc);
1469 XtReleaseGC ((Widget) mw, mw->menu.inactive_button_gc);
1470 XtReleaseGC ((Widget) mw, mw->menu.background_gc);
1471 /* let's get some segvs if we try to use these... */
1472 mw->menu.foreground_gc = (GC) -1;
1473 mw->menu.button_gc = (GC) -1;
1474 mw->menu.inactive_gc = (GC) -1;
1475 mw->menu.inactive_button_gc = (GC) -1;
1476 mw->menu.background_gc = (GC) -1;
1477 }
1478
1479 #define MINL(x,y) ((((unsigned long) (x)) < ((unsigned long) (y))) \
1480 ? ((unsigned long) (x)) : ((unsigned long) (y)))
1481
1482 static void
1483 make_shadow_gcs (mw)
1484 XlwMenuWidget mw;
1485 {
1486 XGCValues xgcv;
1487 unsigned long pm = 0;
1488 Display *dpy = XtDisplay ((Widget) mw);
1489 Screen *screen = XtScreen ((Widget) mw);
1490 Colormap cmap = mw->core.colormap;
1491 XColor topc, botc;
1492 int top_frobbed = 0, bottom_frobbed = 0;
1493
1494 mw->menu.free_top_shadow_color_p = 0;
1495 mw->menu.free_bottom_shadow_color_p = 0;
1496
1497 if (mw->menu.top_shadow_color == -1)
1498 mw->menu.top_shadow_color = mw->core.background_pixel;
1499 else
1500 mw->menu.top_shadow_color = mw->menu.top_shadow_color;
1501
1502 if (mw->menu.bottom_shadow_color == -1)
1503 mw->menu.bottom_shadow_color = mw->menu.foreground;
1504 else
1505 mw->menu.bottom_shadow_color = mw->menu.bottom_shadow_color;
1506
1507 if (mw->menu.top_shadow_color == mw->core.background_pixel ||
1508 mw->menu.top_shadow_color == mw->menu.foreground)
1509 {
1510 topc.pixel = mw->core.background_pixel;
1511 #ifdef emacs
1512 if (x_alloc_lighter_color_for_widget ((Widget) mw, dpy, cmap,
1513 &topc.pixel,
1514 1.2, 0x8000))
1515 #else
1516 XQueryColor (dpy, cmap, &topc);
1517 /* don't overflow/wrap! */
1518 topc.red = MINL (65535, topc.red * 1.2);
1519 topc.green = MINL (65535, topc.green * 1.2);
1520 topc.blue = MINL (65535, topc.blue * 1.2);
1521 if (XAllocColor (dpy, cmap, &topc))
1522 #endif
1523 {
1524 mw->menu.top_shadow_color = topc.pixel;
1525 mw->menu.free_top_shadow_color_p = 1;
1526 top_frobbed = 1;
1527 }
1528 }
1529 if (mw->menu.bottom_shadow_color == mw->menu.foreground ||
1530 mw->menu.bottom_shadow_color == mw->core.background_pixel)
1531 {
1532 botc.pixel = mw->core.background_pixel;
1533 #ifdef emacs
1534 if (x_alloc_lighter_color_for_widget ((Widget) mw, dpy, cmap,
1535 &botc.pixel,
1536 0.6, 0x4000))
1537 #else
1538 XQueryColor (dpy, cmap, &botc);
1539 botc.red *= 0.6;
1540 botc.green *= 0.6;
1541 botc.blue *= 0.6;
1542 if (XAllocColor (dpy, cmap, &botc))
1543 #endif
1544 {
1545 mw->menu.bottom_shadow_color = botc.pixel;
1546 mw->menu.free_bottom_shadow_color_p = 1;
1547 bottom_frobbed = 1;
1548 }
1549 }
1550
1551 if (top_frobbed && bottom_frobbed)
1552 {
1553 if (topc.pixel == botc.pixel)
1554 {
1555 if (botc.pixel == mw->menu.foreground)
1556 {
1557 if (mw->menu.free_top_shadow_color_p)
1558 {
1559 x_free_dpy_colors (dpy, screen, cmap,
1560 &mw->menu.top_shadow_color, 1);
1561 mw->menu.free_top_shadow_color_p = 0;
1562 }
1563 mw->menu.top_shadow_color = mw->core.background_pixel;
1564 }
1565 else
1566 {
1567 if (mw->menu.free_bottom_shadow_color_p)
1568 {
1569 x_free_dpy_colors (dpy, screen, cmap,
1570 &mw->menu.bottom_shadow_color, 1);
1571 mw->menu.free_bottom_shadow_color_p = 0;
1572 }
1573 mw->menu.bottom_shadow_color = mw->menu.foreground;
1574 }
1575 }
1576 }
1577
1578 if (!mw->menu.top_shadow_pixmap &&
1579 mw->menu.top_shadow_color == mw->core.background_pixel)
1580 {
1581 mw->menu.top_shadow_pixmap = mw->menu.gray_pixmap;
1582 if (mw->menu.free_top_shadow_color_p)
1583 {
1584 x_free_dpy_colors (dpy, screen, cmap, &mw->menu.top_shadow_color, 1);
1585 mw->menu.free_top_shadow_color_p = 0;
1586 }
1587 mw->menu.top_shadow_color = mw->menu.foreground;
1588 }
1589 if (!mw->menu.bottom_shadow_pixmap &&
1590 mw->menu.bottom_shadow_color == mw->core.background_pixel)
1591 {
1592 mw->menu.bottom_shadow_pixmap = mw->menu.gray_pixmap;
1593 if (mw->menu.free_bottom_shadow_color_p)
1594 {
1595 x_free_dpy_colors (dpy, screen, cmap,
1596 &mw->menu.bottom_shadow_color, 1);
1597 mw->menu.free_bottom_shadow_color_p = 0;
1598 }
1599 mw->menu.bottom_shadow_color = mw->menu.foreground;
1600 }
1601
1602 xgcv.fill_style = FillStippled;
1603 xgcv.foreground = mw->menu.top_shadow_color;
1604 xgcv.stipple = mw->menu.top_shadow_pixmap;
1605 pm = (xgcv.stipple ? GCStipple|GCFillStyle : 0);
1606 mw->menu.shadow_top_gc = XtGetGC ((Widget)mw, GCForeground | pm, &xgcv);
1607
1608 xgcv.foreground = mw->menu.bottom_shadow_color;
1609 xgcv.stipple = mw->menu.bottom_shadow_pixmap;
1610 pm = (xgcv.stipple ? GCStipple|GCFillStyle : 0);
1611 mw->menu.shadow_bottom_gc = XtGetGC ((Widget)mw, GCForeground | pm, &xgcv);
1612 }
1613
1614
1615 static void
1616 release_shadow_gcs (mw)
1617 XlwMenuWidget mw;
1618 {
1619 Display *dpy = XtDisplay ((Widget) mw);
1620 Screen *screen = XtScreen ((Widget) mw);
1621 Colormap cmap = mw->core.colormap;
1622 Pixel px[2];
1623 int i = 0;
1624
1625 if (mw->menu.free_top_shadow_color_p)
1626 px[i++] = mw->menu.top_shadow_color;
1627 if (mw->menu.free_bottom_shadow_color_p)
1628 px[i++] = mw->menu.bottom_shadow_color;
1629 if (i > 0)
1630 x_free_dpy_colors (dpy, screen, cmap, px, i);
1631
1632 XtReleaseGC ((Widget) mw, mw->menu.shadow_top_gc);
1633 XtReleaseGC ((Widget) mw, mw->menu.shadow_bottom_gc);
1634 }
1635
1636 static void
1637 XlwMenuInitialize (request, mw, args, num_args)
1638 Widget request;
1639 XlwMenuWidget mw;
1640 ArgList args;
1641 Cardinal *num_args;
1642 {
1643 /* Get the GCs and the widget size */
1644 XSetWindowAttributes xswa;
1645 int mask;
1646
1647 Window window = RootWindowOfScreen (DefaultScreenOfDisplay (XtDisplay (mw)));
1648 Display* display = XtDisplay (mw);
1649
1650 #if 0
1651 widget_value *tem = (widget_value *) XtMalloc (sizeof (widget_value));
1652
1653 /* _XtCreate is freeing the object that was passed to us,
1654 so make a copy that we will actually keep. */
1655 lwlib_bcopy (mw->menu.contents, tem, sizeof (widget_value));
1656 mw->menu.contents = tem;
1657 #endif
1658
1659 /* mw->menu.cursor = XCreateFontCursor (display, mw->menu.cursor_shape); */
1660 mw->menu.cursor = mw->menu.cursor_shape;
1661
1662 mw->menu.gray_pixmap
1663 = XCreatePixmapFromBitmapData (display, window, gray_bitmap_bits,
1664 gray_bitmap_width, gray_bitmap_height,
1665 (unsigned long)1, (unsigned long)0, 1);
1666
1667 /* I don't understand why this ends up 0 sometimes,
1668 but it does. This kludge works around it.
1669 Can anyone find a real fix? -- rms. */
1670 if (mw->menu.font == 0)
1671 mw->menu.font = xlwmenu_default_font;
1672
1673 make_drawing_gcs (mw);
1674 make_shadow_gcs (mw);
1675
1676 xswa.background_pixel = mw->core.background_pixel;
1677 xswa.border_pixel = mw->core.border_pixel;
1678 mask = CWBackPixel | CWBorderPixel;
1679
1680 mw->menu.popped_up = False;
1681
1682 mw->menu.old_depth = 1;
1683 mw->menu.old_stack = (widget_value**)XtMalloc (sizeof (widget_value*));
1684 mw->menu.old_stack_length = 1;
1685 mw->menu.old_stack [0] = mw->menu.contents;
1686
1687 mw->menu.new_depth = 0;
1688 mw->menu.new_stack = 0;
1689 mw->menu.new_stack_length = 0;
1690 push_new_stack (mw, mw->menu.contents);
1691
1692 mw->menu.windows = (window_state*)XtMalloc (sizeof (window_state));
1693 mw->menu.windows_length = 1;
1694 mw->menu.windows [0].x = 0;
1695 mw->menu.windows [0].y = 0;
1696 mw->menu.windows [0].width = 0;
1697 mw->menu.windows [0].height = 0;
1698 size_menu (mw, 0);
1699
1700 mw->core.width = mw->menu.windows [0].width;
1701 mw->core.height = mw->menu.windows [0].height;
1702 }
1703
1704 static void
1705 XlwMenuClassInitialize ()
1706 {
1707 }
1708
1709 static void
1710 XlwMenuRealize (w, valueMask, attributes)
1711 Widget w;
1712 Mask *valueMask;
1713 XSetWindowAttributes *attributes;
1714 {
1715 XlwMenuWidget mw = (XlwMenuWidget)w;
1716 XSetWindowAttributes xswa;
1717 int mask;
1718
1719 (*xlwMenuWidgetClass->core_class.superclass->core_class.realize)
1720 (w, valueMask, attributes);
1721
1722 xswa.save_under = True;
1723 xswa.cursor = mw->menu.cursor_shape;
1724 mask = CWSaveUnder | CWCursor;
1725 XChangeWindowAttributes (XtDisplay (w), XtWindow (w), mask, &xswa);
1726
1727 mw->menu.windows [0].window = XtWindow (w);
1728 mw->menu.windows [0].x = w->core.x;
1729 mw->menu.windows [0].y = w->core.y;
1730 mw->menu.windows [0].width = w->core.width;
1731 mw->menu.windows [0].height = w->core.height;
1732 }
1733
1734 /* Only the toplevel menubar/popup is a widget so it's the only one that
1735 receives expose events through Xt. So we repaint all the other panes
1736 when receiving an Expose event. */
1737 static void
1738 XlwMenuRedisplay (w, ev, region)
1739 Widget w;
1740 XEvent* ev;
1741 Region region;
1742 {
1743 XlwMenuWidget mw = (XlwMenuWidget)w;
1744 int i;
1745
1746 /* If we have a depth beyond 1, it's because a submenu was displayed.
1747 If the submenu has been destroyed, set the depth back to 1. */
1748 if (submenu_destroyed)
1749 {
1750 mw->menu.old_depth = 1;
1751 submenu_destroyed = 0;
1752 }
1753
1754 for (i = 0; i < mw->menu.old_depth; i++)
1755 display_menu (mw, i, False, NULL, NULL, NULL, NULL, NULL);
1756 }
1757
1758
1759 /* Part of a hack to make the menu redisplay when a tooltip frame
1760 over a menu item is unmapped. */
1761
1762 void
1763 xlwmenu_redisplay (w)
1764 Widget w;
1765 {
1766 XlwMenuRedisplay (w, NULL, None);
1767 }
1768
1769 static void
1770 XlwMenuDestroy (w)
1771 Widget w;
1772 {
1773 int i;
1774 XlwMenuWidget mw = (XlwMenuWidget) w;
1775
1776 if (pointer_grabbed)
1777 XtUngrabPointer ((Widget)w, CurrentTime);
1778 pointer_grabbed = 0;
1779
1780 submenu_destroyed = 1;
1781
1782 release_drawing_gcs (mw);
1783 release_shadow_gcs (mw);
1784
1785 /* this doesn't come from the resource db but is created explicitly
1786 so we must free it ourselves. */
1787 XFreePixmap (XtDisplay (mw), mw->menu.gray_pixmap);
1788 mw->menu.gray_pixmap = (Pixmap) -1;
1789
1790 #if 0
1791 /* Do free mw->menu.contents because nowadays we copy it
1792 during initialization. */
1793 XtFree (mw->menu.contents);
1794 #endif
1795
1796 /* Don't free mw->menu.contents because that comes from our creator.
1797 The `*_stack' elements are just pointers into `contents' so leave
1798 that alone too. But free the stacks themselves. */
1799 if (mw->menu.old_stack) XtFree ((char *) mw->menu.old_stack);
1800 if (mw->menu.new_stack) XtFree ((char *) mw->menu.new_stack);
1801
1802 /* Remember, you can't free anything that came from the resource
1803 database. This includes:
1804 mw->menu.cursor
1805 mw->menu.top_shadow_pixmap
1806 mw->menu.bottom_shadow_pixmap
1807 mw->menu.font
1808 Also the color cells of top_shadow_color, bottom_shadow_color,
1809 foreground, and button_foreground will never be freed until this
1810 client exits. Nice, eh?
1811 */
1812
1813 /* start from 1 because the one in slot 0 is w->core.window */
1814 for (i = 1; i < mw->menu.windows_length; i++)
1815 XDestroyWindow (XtDisplay (mw), mw->menu.windows [i].window);
1816 if (mw->menu.windows)
1817 XtFree ((char *) mw->menu.windows);
1818 }
1819
1820 static Boolean
1821 XlwMenuSetValues (current, request, new)
1822 Widget current;
1823 Widget request;
1824 Widget new;
1825 {
1826 XlwMenuWidget oldmw = (XlwMenuWidget)current;
1827 XlwMenuWidget newmw = (XlwMenuWidget)new;
1828 Boolean redisplay = False;
1829 int i;
1830
1831 if (newmw->menu.contents
1832 && newmw->menu.contents->contents
1833 && newmw->menu.contents->contents->change >= VISIBLE_CHANGE)
1834 redisplay = True;
1835 /* Do redisplay if the contents are entirely eliminated. */
1836 if (newmw->menu.contents
1837 && newmw->menu.contents->contents == 0
1838 && newmw->menu.contents->change >= VISIBLE_CHANGE)
1839 redisplay = True;
1840
1841 if (newmw->core.background_pixel != oldmw->core.background_pixel
1842 || newmw->menu.foreground != oldmw->menu.foreground
1843 || newmw->menu.font != oldmw->menu.font)
1844 {
1845 release_drawing_gcs (newmw);
1846 make_drawing_gcs (newmw);
1847
1848 release_shadow_gcs (newmw);
1849 /* Cause the shadow colors to be recalculated. */
1850 newmw->menu.top_shadow_color = -1;
1851 newmw->menu.bottom_shadow_color = -1;
1852 make_shadow_gcs (newmw);
1853
1854 redisplay = True;
1855
1856 if (XtIsRealized (current))
1857 /* If the menu is currently displayed, change the display. */
1858 for (i = 0; i < oldmw->menu.windows_length; i++)
1859 {
1860 XSetWindowBackground (XtDisplay (oldmw),
1861 oldmw->menu.windows [i].window,
1862 newmw->core.background_pixel);
1863 /* clear windows and generate expose events */
1864 XClearArea (XtDisplay (oldmw), oldmw->menu.windows[i].window,
1865 0, 0, 0, 0, True);
1866 }
1867 }
1868
1869 return redisplay;
1870 }
1871
1872 static void
1873 XlwMenuResize (w)
1874 Widget w;
1875 {
1876 XlwMenuWidget mw = (XlwMenuWidget)w;
1877
1878 if (mw->menu.popped_up)
1879 {
1880 /* Don't allow the popup menu to resize itself. */
1881 mw->core.width = mw->menu.windows [0].width;
1882 mw->core.height = mw->menu.windows [0].height;
1883 mw->core.parent->core.width = mw->core.width ;
1884 mw->core.parent->core.height = mw->core.height ;
1885 }
1886 else
1887 {
1888 mw->menu.windows [0].width = mw->core.width;
1889 mw->menu.windows [0].height = mw->core.height;
1890 }
1891 }
1892
1893 \f/* Action procedures */
1894 static void
1895 handle_single_motion_event (mw, ev)
1896 XlwMenuWidget mw;
1897 XMotionEvent* ev;
1898 {
1899 widget_value* val;
1900 int level;
1901
1902 if (!map_event_to_widget_value (mw, ev, &val, &level))
1903 pop_new_stack_if_no_contents (mw);
1904 else
1905 set_new_state (mw, val, level);
1906 remap_menubar (mw);
1907
1908 /* Sync with the display. Makes it feel better on X terms. */
1909 XSync (XtDisplay (mw), False);
1910 }
1911
1912 static void
1913 handle_motion_event (mw, ev)
1914 XlwMenuWidget mw;
1915 XMotionEvent* ev;
1916 {
1917 int x = ev->x_root;
1918 int y = ev->y_root;
1919 int state = ev->state;
1920
1921 handle_single_motion_event (mw, ev);
1922
1923 /* allow motion events to be generated again */
1924 if (ev->is_hint
1925 && XQueryPointer (XtDisplay (mw), ev->window,
1926 &ev->root, &ev->subwindow,
1927 &ev->x_root, &ev->y_root,
1928 &ev->x, &ev->y,
1929 &ev->state)
1930 && ev->state == state
1931 && (ev->x_root != x || ev->y_root != y))
1932 handle_single_motion_event (mw, ev);
1933 }
1934
1935 static void
1936 Start (w, ev, params, num_params)
1937 Widget w;
1938 XEvent *ev;
1939 String *params;
1940 Cardinal *num_params;
1941 {
1942 XlwMenuWidget mw = (XlwMenuWidget)w;
1943
1944 if (!mw->menu.popped_up)
1945 {
1946 menu_post_event = *ev;
1947 pop_up_menu (mw, (XButtonPressedEvent*) ev);
1948 }
1949 else
1950 {
1951 /* If we push a button while the menu is posted semipermanently,
1952 releasing the button should always pop the menu down. */
1953 next_release_must_exit = 1;
1954
1955 /* notes the absolute position of the menubar window */
1956 mw->menu.windows [0].x = ev->xmotion.x_root - ev->xmotion.x;
1957 mw->menu.windows [0].y = ev->xmotion.y_root - ev->xmotion.y;
1958
1959 /* handles the down like a move, slots are compatible */
1960 handle_motion_event (mw, &ev->xmotion);
1961 }
1962 }
1963
1964 static void
1965 Drag (w, ev, params, num_params)
1966 Widget w;
1967 XEvent *ev;
1968 String *params;
1969 Cardinal *num_params;
1970 {
1971 XlwMenuWidget mw = (XlwMenuWidget)w;
1972 if (mw->menu.popped_up)
1973 handle_motion_event (mw, &ev->xmotion);
1974 }
1975
1976 /* Do nothing.
1977 This is how we handle presses and releases of modifier keys. */
1978 static void
1979 Nothing (w, ev, params, num_params)
1980 Widget w;
1981 XEvent *ev;
1982 String *params;
1983 Cardinal *num_params;
1984 {
1985 }
1986
1987 /* Handle key press and release events while menu is popped up.
1988 Our action is to get rid of the menu. */
1989 static void
1990 Key (w, ev, params, num_params)
1991 Widget w;
1992 XEvent *ev;
1993 String *params;
1994 Cardinal *num_params;
1995 {
1996 XlwMenuWidget mw = (XlwMenuWidget)w;
1997
1998 /* Pop down everything. */
1999 mw->menu.new_depth = 1;
2000 remap_menubar (mw);
2001
2002 if (mw->menu.popped_up)
2003 {
2004 mw->menu.popped_up = False;
2005 XtUngrabPointer ((Widget)mw, ev->xmotion.time);
2006 if (XtIsShell (XtParent ((Widget) mw)))
2007 XtPopdown (XtParent ((Widget) mw));
2008 else
2009 {
2010 XtRemoveGrab ((Widget) mw);
2011 display_menu (mw, 0, False, NULL, NULL, NULL, NULL, NULL);
2012 }
2013 }
2014
2015 /* callback */
2016 XtCallCallbackList ((Widget)mw, mw->menu.select, (XtPointer)0);
2017 }
2018
2019 static void
2020 Select (w, ev, params, num_params)
2021 Widget w;
2022 XEvent *ev;
2023 String *params;
2024 Cardinal *num_params;
2025 {
2026 XlwMenuWidget mw = (XlwMenuWidget)w;
2027 widget_value* selected_item = mw->menu.old_stack [mw->menu.old_depth - 1];
2028
2029 /* If user releases the button quickly, without selecting anything,
2030 after the initial down-click that brought the menu up,
2031 do nothing. */
2032 if ((selected_item == 0
2033 || ((widget_value *) selected_item)->call_data == 0)
2034 && !next_release_must_exit
2035 && (ev->xbutton.time - menu_post_event.xbutton.time
2036 < XtGetMultiClickTime (XtDisplay (w))))
2037 return;
2038
2039 /* pop down everything. */
2040 mw->menu.new_depth = 1;
2041 remap_menubar (mw);
2042
2043 if (mw->menu.popped_up)
2044 {
2045 mw->menu.popped_up = False;
2046 XtUngrabPointer ((Widget)mw, ev->xmotion.time);
2047 if (XtIsShell (XtParent ((Widget) mw)))
2048 XtPopdown (XtParent ((Widget) mw));
2049 else
2050 {
2051 XtRemoveGrab ((Widget) mw);
2052 display_menu (mw, 0, False, NULL, NULL, NULL, NULL, NULL);
2053 }
2054 }
2055
2056 /* callback */
2057 XtCallCallbackList ((Widget)mw, mw->menu.select, (XtPointer)selected_item);
2058 }
2059
2060
2061 \f/* Special code to pop-up a menu */
2062 void
2063 pop_up_menu (mw, event)
2064 XlwMenuWidget mw;
2065 XButtonPressedEvent* event;
2066 {
2067 int x = event->x_root;
2068 int y = event->y_root;
2069 int w;
2070 int h;
2071 int borderwidth = mw->menu.shadow_thickness;
2072 Screen* screen = XtScreen (mw);
2073 Display *display = XtDisplay (mw);
2074 int count;
2075
2076 next_release_must_exit = 0;
2077
2078 XtCallCallbackList ((Widget)mw, mw->menu.open, NULL);
2079
2080 if (XtIsShell (XtParent ((Widget)mw)))
2081 size_menu (mw, 0);
2082
2083 w = mw->menu.windows [0].width;
2084 h = mw->menu.windows [0].height;
2085
2086 x -= borderwidth;
2087 y -= borderwidth;
2088 if (x < borderwidth)
2089 x = borderwidth;
2090 if (x + w + 2 * borderwidth > WidthOfScreen (screen))
2091 x = WidthOfScreen (screen) - w - 2 * borderwidth;
2092 if (y < borderwidth)
2093 y = borderwidth;
2094 if (y + h + 2 * borderwidth> HeightOfScreen (screen))
2095 y = HeightOfScreen (screen) - h - 2 * borderwidth;
2096
2097 mw->menu.popped_up = True;
2098 if (XtIsShell (XtParent ((Widget)mw)))
2099 {
2100 XtConfigureWidget (XtParent ((Widget)mw), x, y, w, h,
2101 XtParent ((Widget)mw)->core.border_width);
2102 XtPopup (XtParent ((Widget)mw), XtGrabExclusive);
2103 display_menu (mw, 0, False, NULL, NULL, NULL, NULL, NULL);
2104 mw->menu.windows [0].x = x + borderwidth;
2105 mw->menu.windows [0].y = y + borderwidth;
2106 }
2107 else
2108 {
2109 XEvent *ev = (XEvent *) event;
2110
2111 XtAddGrab ((Widget) mw, True, True);
2112
2113 /* notes the absolute position of the menubar window */
2114 mw->menu.windows [0].x = ev->xmotion.x_root - ev->xmotion.x;
2115 mw->menu.windows [0].y = ev->xmotion.y_root - ev->xmotion.y;
2116 }
2117
2118 #ifdef emacs
2119 count = x_catch_errors (display);
2120 #endif
2121 XtGrabPointer ((Widget)mw, False,
2122 (PointerMotionMask
2123 | PointerMotionHintMask
2124 | ButtonReleaseMask
2125 | ButtonPressMask),
2126 GrabModeAsync, GrabModeAsync, None,
2127 mw->menu.cursor_shape,
2128 event->time);
2129 pointer_grabbed = 1;
2130 #ifdef emacs
2131 if (x_had_errors_p (display))
2132 {
2133 pointer_grabbed = 0;
2134 XtUngrabPointer ((Widget)mw, event->time);
2135 }
2136 x_uncatch_errors (display, count);
2137 #endif
2138
2139 handle_motion_event (mw, (XMotionEvent*)event);
2140 }