]> code.delx.au - gnu-emacs/blob - lwlib/xlwmenu.c
(pop_up_menu): Pass a Display * to x_catch_errors, etc.
[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 Alongalong with GNU Emacs; see the file COPYING. If not, write to
18 the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
19
20 /* Created by devin@lucid.com */
21
22 #include <stdio.h>
23
24 #include <sys/types.h>
25 #include <X11/Xos.h>
26 #include <X11/IntrinsicP.h>
27 #include <X11/ObjectP.h>
28 #include <X11/StringDefs.h>
29 #include <X11/cursorfont.h>
30 #include <X11/bitmaps/gray>
31 #include "xlwmenuP.h"
32
33 static int pointer_grabbed;
34 static XEvent menu_post_event;
35
36 static char
37 xlwMenuTranslations [] =
38 "<BtnDown>: start()\n\
39 <Motion>: drag()\n\
40 <BtnUp>: select()\n\
41 ";
42
43 #define offset(field) XtOffset(XlwMenuWidget, field)
44 static XtResource
45 xlwMenuResources[] =
46 {
47 {XtNfont, XtCFont, XtRFontStruct, sizeof(XFontStruct *),
48 offset(menu.font),XtRString, "XtDefaultFont"},
49 {XtNforeground, XtCForeground, XtRPixel, sizeof(Pixel),
50 offset(menu.foreground), XtRString, "XtDefaultForeground"},
51 {XtNbuttonForeground, XtCButtonForeground, XtRPixel, sizeof(Pixel),
52 offset(menu.button_foreground), XtRString, "XtDefaultForeground"},
53 {XtNmargin, XtCMargin, XtRDimension, sizeof(Dimension),
54 offset(menu.margin), XtRImmediate, (XtPointer)0},
55 {XtNhorizontalSpacing, XtCMargin, XtRDimension, sizeof(Dimension),
56 offset(menu.horizontal_spacing), XtRImmediate, (XtPointer)3},
57 {XtNverticalSpacing, XtCMargin, XtRDimension, sizeof(Dimension),
58 offset(menu.vertical_spacing), XtRImmediate, (XtPointer)1},
59 {XtNarrowSpacing, XtCMargin, XtRDimension, sizeof(Dimension),
60 offset(menu.arrow_spacing), XtRImmediate, (XtPointer)10},
61
62 {XmNshadowThickness, XmCShadowThickness, XtRDimension,
63 sizeof (Dimension), offset (menu.shadow_thickness),
64 XtRImmediate, (XtPointer) 2},
65 {XmNtopShadowColor, XmCTopShadowColor, XtRPixel, sizeof (Pixel),
66 offset (menu.top_shadow_color), XtRImmediate, (XtPointer)-1},
67 {XmNbottomShadowColor, XmCBottomShadowColor, XtRPixel, sizeof (Pixel),
68 offset (menu.bottom_shadow_color), XtRImmediate, (XtPointer)-1},
69 {XmNtopShadowPixmap, XmCTopShadowPixmap, XtRPixmap, sizeof (Pixmap),
70 offset (menu.top_shadow_pixmap), XtRImmediate, (XtPointer)None},
71 {XmNbottomShadowPixmap, XmCBottomShadowPixmap, XtRPixmap, sizeof (Pixmap),
72 offset (menu.bottom_shadow_pixmap), XtRImmediate, (XtPointer)None},
73
74 {XtNopen, XtCCallback, XtRCallback, sizeof(XtPointer),
75 offset(menu.open), XtRCallback, (XtPointer)NULL},
76 {XtNselect, XtCCallback, XtRCallback, sizeof(XtPointer),
77 offset(menu.select), XtRCallback, (XtPointer)NULL},
78 {XtNmenu, XtCMenu, XtRPointer, sizeof(XtPointer),
79 offset(menu.contents), XtRImmediate, (XtPointer)NULL},
80 {XtNcursor, XtCCursor, XtRCursor, sizeof(Cursor),
81 offset(menu.cursor_shape), XtRString, (XtPointer)"right_ptr"},
82 {XtNhorizontal, XtCHorizontal, XtRInt, sizeof(int),
83 offset(menu.horizontal), XtRImmediate, (XtPointer)True},
84 };
85 #undef offset
86
87 static Boolean XlwMenuSetValues();
88 static void XlwMenuRealize();
89 static void XlwMenuRedisplay();
90 static void XlwMenuResize();
91 static void XlwMenuInitialize();
92 static void XlwMenuRedisplay();
93 static void XlwMenuDestroy();
94 static void XlwMenuClassInitialize();
95 static void Start();
96 static void Drag();
97 static void Select();
98
99 static XtActionsRec
100 xlwMenuActionsList [] =
101 {
102 {"start", Start},
103 {"drag", Drag},
104 {"select", Select},
105 };
106
107 #define SuperClass ((CoreWidgetClass)&coreClassRec)
108
109 XlwMenuClassRec xlwMenuClassRec =
110 {
111 { /* CoreClass fields initialization */
112 (WidgetClass) SuperClass, /* superclass */
113 "XlwMenu", /* class_name */
114 sizeof(XlwMenuRec), /* size */
115 XlwMenuClassInitialize, /* class_initialize */
116 NULL, /* class_part_initialize */
117 FALSE, /* class_inited */
118 XlwMenuInitialize, /* initialize */
119 NULL, /* initialize_hook */
120 XlwMenuRealize, /* realize */
121 xlwMenuActionsList, /* actions */
122 XtNumber(xlwMenuActionsList), /* num_actions */
123 xlwMenuResources, /* resources */
124 XtNumber(xlwMenuResources), /* resource_count */
125 NULLQUARK, /* xrm_class */
126 TRUE, /* compress_motion */
127 TRUE, /* compress_exposure */
128 TRUE, /* compress_enterleave */
129 FALSE, /* visible_interest */
130 XlwMenuDestroy, /* destroy */
131 XlwMenuResize, /* resize */
132 XlwMenuRedisplay, /* expose */
133 XlwMenuSetValues, /* set_values */
134 NULL, /* set_values_hook */
135 XtInheritSetValuesAlmost, /* set_values_almost */
136 NULL, /* get_values_hook */
137 NULL, /* accept_focus */
138 XtVersion, /* version */
139 NULL, /* callback_private */
140 xlwMenuTranslations, /* tm_table */
141 XtInheritQueryGeometry, /* query_geometry */
142 XtInheritDisplayAccelerator, /* display_accelerator */
143 NULL /* extension */
144 }, /* XlwMenuClass fields initialization */
145 {
146 0 /* dummy */
147 },
148 };
149
150 WidgetClass xlwMenuWidgetClass = (WidgetClass) &xlwMenuClassRec;
151
152 int submenu_destroyed;
153
154 static int next_release_must_exit;
155
156 \f/* Utilities */
157 static void
158 push_new_stack (mw, val)
159 XlwMenuWidget mw;
160 widget_value* val;
161 {
162 if (!mw->menu.new_stack)
163 {
164 mw->menu.new_stack_length = 10;
165 mw->menu.new_stack =
166 (widget_value**)XtCalloc (mw->menu.new_stack_length,
167 sizeof (widget_value*));
168 }
169 else if (mw->menu.new_depth == mw->menu.new_stack_length)
170 {
171 mw->menu.new_stack_length *= 2;
172 mw->menu.new_stack =
173 (widget_value**)XtRealloc ((char*)mw->menu.new_stack,
174 mw->menu.new_stack_length * sizeof (widget_value*));
175 }
176 mw->menu.new_stack [mw->menu.new_depth++] = val;
177 }
178
179 static void
180 pop_new_stack_if_no_contents (mw)
181 XlwMenuWidget mw;
182 {
183 if (mw->menu.new_depth)
184 {
185 if (!mw->menu.new_stack [mw->menu.new_depth - 1]->contents)
186 mw->menu.new_depth -= 1;
187 }
188 }
189
190 static void
191 make_old_stack_space (mw, n)
192 XlwMenuWidget mw;
193 int n;
194 {
195 if (!mw->menu.old_stack)
196 {
197 mw->menu.old_stack_length = 10;
198 mw->menu.old_stack =
199 (widget_value**)XtCalloc (mw->menu.old_stack_length,
200 sizeof (widget_value*));
201 }
202 else if (mw->menu.old_stack_length < n)
203 {
204 mw->menu.old_stack_length *= 2;
205 mw->menu.old_stack =
206 (widget_value**)XtRealloc ((char*)mw->menu.old_stack,
207 mw->menu.old_stack_length * sizeof (widget_value*));
208 }
209 }
210
211 \f/* Size code */
212 static Boolean
213 all_dashes_p (s)
214 char *s;
215 {
216 char* p;
217 for (p = s; *p == '-'; p++);
218 return !*p;
219 }
220
221 int
222 string_width (mw, s)
223 XlwMenuWidget mw;
224 char *s;
225 {
226 XCharStruct xcs;
227 int drop;
228
229 XTextExtents (mw->menu.font, s, strlen (s), &drop, &drop, &drop, &xcs);
230 return xcs.width;
231 }
232
233 static int
234 arrow_width (mw)
235 XlwMenuWidget mw;
236 {
237 return mw->menu.font->ascent / 2 | 1;
238 }
239
240 static XtResource
241 nameResource[] =
242 {
243 {"labelString", "LabelString", XtRString, sizeof(String),
244 0, XtRImmediate, 0},
245 };
246
247 static char*
248 resource_widget_value (mw, val)
249 XlwMenuWidget mw;
250 widget_value *val;
251 {
252 if (!val->toolkit_data)
253 {
254 char* resourced_name = NULL;
255 char* complete_name;
256 XtGetSubresources ((Widget) mw,
257 (XtPointer) &resourced_name,
258 val->name, val->name,
259 nameResource, 1, NULL, 0);
260 if (!resourced_name)
261 resourced_name = val->name;
262 if (!val->value)
263 {
264 complete_name = (char *) XtMalloc (strlen (resourced_name) + 1);
265 strcpy (complete_name, resourced_name);
266 }
267 else
268 {
269 int complete_length =
270 strlen (resourced_name) + strlen (val->value) + 2;
271 complete_name = XtMalloc (complete_length);
272 *complete_name = 0;
273 strcat (complete_name, resourced_name);
274 strcat (complete_name, " ");
275 strcat (complete_name, val->value);
276 }
277
278 val->toolkit_data = complete_name;
279 val->free_toolkit_data = True;
280 }
281 return (char*)val->toolkit_data;
282 }
283
284 /* Returns the sizes of an item */
285 static void
286 size_menu_item (mw, val, horizontal_p, label_width, rest_width, height)
287 XlwMenuWidget mw;
288 widget_value* val;
289 int horizontal_p;
290 int* label_width;
291 int* rest_width;
292 int* height;
293 {
294 if (all_dashes_p (val->name))
295 {
296 *height = 2;
297 *label_width = 1;
298 *rest_width = 0;
299 }
300 else
301 {
302 *height =
303 mw->menu.font->ascent + mw->menu.font->descent
304 + 2 * mw->menu.vertical_spacing + 2 * mw->menu.shadow_thickness;
305
306 *label_width =
307 string_width (mw, resource_widget_value (mw, val))
308 + mw->menu.horizontal_spacing + mw->menu.shadow_thickness;
309
310 *rest_width = mw->menu.horizontal_spacing + mw->menu.shadow_thickness;
311 if (!horizontal_p)
312 {
313 if (val->contents)
314 *rest_width += arrow_width (mw) + mw->menu.arrow_spacing;
315 else if (val->key)
316 *rest_width +=
317 string_width (mw, val->key) + mw->menu.arrow_spacing;
318 }
319 }
320 }
321
322 static void
323 size_menu (mw, level)
324 XlwMenuWidget mw;
325 int level;
326 {
327 int label_width = 0;
328 int rest_width = 0;
329 int max_rest_width = 0;
330 int height = 0;
331 int horizontal_p = mw->menu.horizontal && (level == 0);
332 widget_value* val;
333 window_state* ws;
334
335 if (level >= mw->menu.old_depth)
336 abort ();
337
338 ws = &mw->menu.windows [level];
339 ws->width = 0;
340 ws->height = 0;
341 ws->label_width = 0;
342
343 for (val = mw->menu.old_stack [level]->contents; val; val = val->next)
344 {
345 size_menu_item (mw, val, horizontal_p, &label_width, &rest_width,
346 &height);
347 if (horizontal_p)
348 {
349 ws->width += label_width + rest_width;
350 if (height > ws->height)
351 ws->height = height;
352 }
353 else
354 {
355 if (label_width > ws->label_width)
356 ws->label_width = label_width;
357 if (rest_width > max_rest_width)
358 max_rest_width = rest_width;
359 ws->height += height;
360 }
361 }
362
363 if (horizontal_p)
364 ws->label_width = 0;
365 else
366 ws->width = ws->label_width + max_rest_width;
367
368 ws->width += 2 * mw->menu.shadow_thickness;
369 ws->height += 2 * mw->menu.shadow_thickness;
370 }
371
372
373 \f/* Display code */
374 static void
375 draw_arrow (mw, window, gc, x, y, width)
376 XlwMenuWidget mw;
377 Window window;
378 GC gc;
379 int x;
380 int y;
381 int width;
382 {
383 XPoint points [3];
384 points [0].x = x;
385 points [0].y = y + mw->menu.font->ascent;
386 points [1].x = x;
387 points [1].y = y;
388 points [2].x = x + width;
389 points [2].y = y + mw->menu.font->ascent / 2;
390
391 XFillPolygon (XtDisplay (mw), window, gc, points, 3, Convex,
392 CoordModeOrigin);
393 }
394
395 static void
396 draw_shadow_rectangle (mw, window, x, y, width, height, erase_p)
397 XlwMenuWidget mw;
398 Window window;
399 int x;
400 int y;
401 int width;
402 int height;
403 int erase_p;
404 {
405 Display *dpy = XtDisplay (mw);
406 GC top_gc = !erase_p ? mw->menu.shadow_top_gc : mw->menu.background_gc;
407 GC bottom_gc = !erase_p ? mw->menu.shadow_bottom_gc : mw->menu.background_gc;
408 int thickness = mw->menu.shadow_thickness;
409 XPoint points [4];
410 points [0].x = x;
411 points [0].y = y;
412 points [1].x = x + width;
413 points [1].y = y;
414 points [2].x = x + width - thickness;
415 points [2].y = y + thickness;
416 points [3].x = x;
417 points [3].y = y + thickness;
418 XFillPolygon (dpy, window, top_gc, points, 4, Convex, CoordModeOrigin);
419 points [0].x = x;
420 points [0].y = y + thickness;
421 points [1].x = x;
422 points [1].y = y + height;
423 points [2].x = x + thickness;
424 points [2].y = y + height - thickness;
425 points [3].x = x + thickness;
426 points [3].y = y + thickness;
427 XFillPolygon (dpy, window, top_gc, points, 4, Convex, CoordModeOrigin);
428 points [0].x = x + width;
429 points [0].y = y;
430 points [1].x = x + width - thickness;
431 points [1].y = y + thickness;
432 points [2].x = x + width - thickness;
433 points [2].y = y + height - thickness;
434 points [3].x = x + width;
435 points [3].y = y + height - thickness;
436 XFillPolygon (dpy, window, bottom_gc, points, 4, Convex, CoordModeOrigin);
437 points [0].x = x;
438 points [0].y = y + height;
439 points [1].x = x + width;
440 points [1].y = y + height;
441 points [2].x = x + width;
442 points [2].y = y + height - thickness;
443 points [3].x = x + thickness;
444 points [3].y = y + height - thickness;
445 XFillPolygon (dpy, window, bottom_gc, points, 4, Convex, CoordModeOrigin);
446 }
447
448
449 /* Display the menu item and increment where.x and where.y to show how large
450 ** the menu item was.
451 */
452 static void
453 display_menu_item (mw, val, ws, where, highlighted_p, horizontal_p, just_compute_p)
454 XlwMenuWidget mw;
455 widget_value* val;
456 window_state* ws;
457 XPoint* where;
458 Boolean highlighted_p;
459 Boolean horizontal_p;
460 Boolean just_compute_p;
461 {
462 GC deco_gc;
463 GC text_gc;
464 int font_ascent = mw->menu.font->ascent;
465 int font_descent = mw->menu.font->descent;
466 int shadow = mw->menu.shadow_thickness;
467 int separator_p = all_dashes_p (val->name);
468 int h_spacing = mw->menu.horizontal_spacing;
469 int v_spacing = mw->menu.vertical_spacing;
470 int label_width;
471 int rest_width;
472 int height;
473 int width;
474 int button_p;
475
476 /* compute the sizes of the item */
477 size_menu_item (mw, val, horizontal_p, &label_width, &rest_width, &height);
478
479 if (horizontal_p)
480 width = label_width + rest_width;
481 else
482 {
483 label_width = ws->label_width;
484 width = ws->width - 2 * shadow;
485 }
486
487 /* see if it should be a button in the menubar */
488 button_p = horizontal_p && val->call_data;
489
490 /* Only highlight an enabled item that has a callback. */
491 if (highlighted_p)
492 if (!val->enabled || !(val->call_data || val->contents))
493 highlighted_p = 0;
494
495 /* do the drawing. */
496 if (!just_compute_p)
497 {
498 /* Add the shadow border of the containing menu */
499 int x = where->x + shadow;
500 int y = where->y + shadow;
501
502 /* pick the foreground and background GC. */
503 if (val->enabled)
504 text_gc = button_p ? mw->menu.button_gc : mw->menu.foreground_gc;
505 else
506 text_gc =
507 button_p ? mw->menu.inactive_button_gc : mw->menu.inactive_gc;
508 deco_gc = mw->menu.foreground_gc;
509
510 if (separator_p)
511 {
512 XDrawLine (XtDisplay (mw), ws->window, mw->menu.shadow_bottom_gc,
513 x, y, x + width, y);
514 XDrawLine (XtDisplay (mw), ws->window, mw->menu.shadow_top_gc,
515 x, y + 1, x + width, y + 1);
516 }
517 else
518 {
519 int x_offset = x + h_spacing + shadow;
520 char* display_string = resource_widget_value (mw, val);
521 draw_shadow_rectangle (mw, ws->window, x, y, width, height, True);
522
523 /* Deal with centering a menu title. */
524 if (!horizontal_p && !val->contents && !val->call_data)
525 {
526 int l = string_width (mw, display_string);
527
528 if (width > l)
529 x_offset = (width - l) >> 1;
530 }
531 XDrawString (XtDisplay (mw), ws->window, text_gc, x_offset,
532 y + v_spacing + shadow + font_ascent,
533 display_string, strlen (display_string));
534
535 if (!horizontal_p)
536 {
537 if (val->contents)
538 {
539 int a_w = arrow_width (mw);
540 draw_arrow (mw, ws->window, deco_gc,
541 x + width - arrow_width (mw)
542 - mw->menu.horizontal_spacing
543 - mw->menu.shadow_thickness,
544 y + v_spacing + shadow, a_w);
545 }
546 else if (val->key)
547 {
548 XDrawString (XtDisplay (mw), ws->window, text_gc,
549 x + label_width + mw->menu.arrow_spacing,
550 y + v_spacing + shadow + font_ascent,
551 val->key, strlen (val->key));
552 }
553 }
554
555 else if (button_p)
556 {
557 #if 1
558 XDrawRectangle (XtDisplay (mw), ws->window, deco_gc,
559 x + shadow, y + shadow,
560 label_width + h_spacing - 1,
561 font_ascent + font_descent + 2 * v_spacing - 1);
562 draw_shadow_rectangle (mw, ws->window, x, y, width, height,
563 False);
564 #else
565 highlighted_p = True;
566 #endif
567 }
568 else
569 {
570 XDrawRectangle (XtDisplay (mw), ws->window,
571 mw->menu.background_gc,
572 x + shadow, y + shadow,
573 label_width + h_spacing - 1,
574 font_ascent + font_descent + 2 * v_spacing - 1);
575 draw_shadow_rectangle (mw, ws->window, x, y, width, height,
576 True);
577 }
578
579 if (highlighted_p)
580 draw_shadow_rectangle (mw, ws->window, x, y, width, height, False);
581 }
582 }
583
584 where->x += width;
585 where->y += height;
586 }
587
588 static void
589 display_menu (mw, level, just_compute_p, highlighted_pos, hit, hit_return,
590 this, that)
591 XlwMenuWidget mw;
592 int level;
593 Boolean just_compute_p;
594 XPoint* highlighted_pos;
595 XPoint* hit;
596 widget_value** hit_return;
597 widget_value* this;
598 widget_value* that;
599 {
600 widget_value* val;
601 widget_value* following_item;
602 window_state* ws;
603 XPoint where;
604 int horizontal_p = mw->menu.horizontal && (level == 0);
605 int highlighted_p;
606 int just_compute_this_one_p;
607
608 if (level >= mw->menu.old_depth)
609 abort ();
610
611 if (level < mw->menu.old_depth - 1)
612 following_item = mw->menu.old_stack [level + 1];
613 else
614 following_item = NULL;
615
616 if (hit)
617 *hit_return = NULL;
618
619 where.x = 0;
620 where.y = 0;
621
622 ws = &mw->menu.windows [level];
623 for (val = mw->menu.old_stack [level]->contents; val; val = val->next)
624 {
625 highlighted_p = val == following_item;
626 if (highlighted_p && highlighted_pos)
627 {
628 if (horizontal_p)
629 highlighted_pos->x = where.x;
630 else
631 highlighted_pos->y = where.y;
632 }
633
634 just_compute_this_one_p =
635 just_compute_p || ((this || that) && val != this && val != that);
636
637 display_menu_item (mw, val, ws, &where, highlighted_p, horizontal_p,
638 just_compute_this_one_p);
639
640 if (highlighted_p && highlighted_pos)
641 {
642 if (horizontal_p)
643 highlighted_pos->y = where.y;
644 else
645 highlighted_pos->x = where.x;
646 }
647
648 if (hit
649 && !*hit_return
650 && (horizontal_p ? hit->x < where.x : hit->y < where.y)
651 && !all_dashes_p (val->name))
652 *hit_return = val;
653
654 if (horizontal_p)
655 where.y = 0;
656 else
657 where.x = 0;
658 }
659
660 if (!just_compute_p)
661 draw_shadow_rectangle (mw, ws->window, 0, 0, ws->width, ws->height, False);
662 }
663
664 \f/* Motion code */
665 static void
666 set_new_state (mw, val, level)
667 XlwMenuWidget mw;
668 widget_value* val;
669 int level;
670 {
671 int i;
672
673 mw->menu.new_depth = 0;
674 for (i = 0; i < level; i++)
675 push_new_stack (mw, mw->menu.old_stack [i]);
676 push_new_stack (mw, val);
677 }
678
679 static void
680 make_windows_if_needed (mw, n)
681 XlwMenuWidget mw;
682 int n;
683 {
684 int i;
685 int start_at;
686 XSetWindowAttributes xswa;
687 int mask;
688 Window root = RootWindowOfScreen (DefaultScreenOfDisplay (XtDisplay (mw)));
689 window_state* windows;
690
691 if (mw->menu.windows_length >= n)
692 return;
693
694 xswa.save_under = True;
695 xswa.override_redirect = True;
696 xswa.background_pixel = mw->core.background_pixel;
697 xswa.border_pixel = mw->core.border_pixel;
698 xswa.event_mask =
699 ExposureMask | PointerMotionMask | PointerMotionHintMask
700 | ButtonReleaseMask | ButtonPressMask;
701 xswa.cursor = mw->menu.cursor_shape;
702 mask = CWSaveUnder | CWOverrideRedirect | CWBackPixel | CWBorderPixel
703 | CWEventMask | CWCursor;
704
705 if (!mw->menu.windows)
706 {
707 mw->menu.windows =
708 (window_state*)XtMalloc (n * sizeof (window_state));
709 start_at = 0;
710 }
711 else
712 {
713 mw->menu.windows =
714 (window_state*)XtRealloc ((char*)mw->menu.windows,
715 n * sizeof (window_state));
716 start_at = mw->menu.windows_length;
717 }
718 mw->menu.windows_length = n;
719
720 windows = mw->menu.windows;
721
722 for (i = start_at; i < n; i++)
723 {
724 windows [i].x = 0;
725 windows [i].y = 0;
726 windows [i].width = 1;
727 windows [i].height = 1;
728 windows [i].window =
729 XCreateWindow (XtDisplay (mw), root, 0, 0, 1, 1,
730 0, 0, CopyFromParent, CopyFromParent, mask, &xswa);
731 }
732 }
733
734 /* Make the window fit in the screen */
735 static void
736 fit_to_screen (mw, ws, previous_ws, horizontal_p)
737 XlwMenuWidget mw;
738 window_state* ws;
739 window_state* previous_ws;
740 Boolean horizontal_p;
741 {
742 int screen_width = WidthOfScreen (XtScreen (mw));
743 int screen_height = HeightOfScreen (XtScreen (mw));
744
745 if (ws->x < 0)
746 ws->x = 0;
747 else if (ws->x + ws->width > screen_width)
748 {
749 if (!horizontal_p)
750 ws->x = previous_ws->x - ws->width;
751 else
752 ws->x = screen_width - ws->width;
753 if (ws->x < 0)
754 ws->x = 0;
755 }
756 if (ws->y < 0)
757 ws->y = 0;
758 else if (ws->y + ws->height > screen_height)
759 {
760 if (horizontal_p)
761 ws->y = previous_ws->y - ws->height;
762 else
763 ws->y = screen_height - ws->height;
764 if (ws->y < 0)
765 ws->y = 0;
766 }
767 }
768
769 /* Updates old_stack from new_stack and redisplays. */
770 static void
771 remap_menubar (mw)
772 XlwMenuWidget mw;
773 {
774 int i;
775 int last_same;
776 XPoint selection_position;
777 int old_depth = mw->menu.old_depth;
778 int new_depth = mw->menu.new_depth;
779 widget_value** old_stack;
780 widget_value** new_stack;
781 window_state* windows;
782 widget_value* old_selection;
783 widget_value* new_selection;
784
785 /* Check that enough windows and old_stack are ready. */
786 make_windows_if_needed (mw, new_depth);
787 make_old_stack_space (mw, new_depth);
788 windows = mw->menu.windows;
789 old_stack = mw->menu.old_stack;
790 new_stack = mw->menu.new_stack;
791
792 /* compute the last identical different entry */
793 for (i = 1; i < old_depth && i < new_depth; i++)
794 if (old_stack [i] != new_stack [i])
795 break;
796 last_same = i - 1;
797
798 /* Memorize the previously selected item to be able to refresh it */
799 old_selection = last_same + 1 < old_depth ? old_stack [last_same + 1] : NULL;
800 if (old_selection && !old_selection->enabled)
801 old_selection = NULL;
802 new_selection = last_same + 1 < new_depth ? new_stack [last_same + 1] : NULL;
803 if (new_selection && !new_selection->enabled)
804 new_selection = NULL;
805
806 /* updates old_state from new_state. It has to be done now because
807 display_menu (called below) uses the old_stack to know what to display. */
808 for (i = last_same + 1; i < new_depth; i++)
809 old_stack [i] = new_stack [i];
810 mw->menu.old_depth = new_depth;
811
812 /* refresh the last seletion */
813 selection_position.x = 0;
814 selection_position.y = 0;
815 display_menu (mw, last_same, new_selection == old_selection,
816 &selection_position, NULL, NULL, old_selection, new_selection);
817
818 /* Now popup the new menus */
819 for (i = last_same + 1; i < new_depth && new_stack [i]->contents; i++)
820 {
821 window_state* previous_ws = &windows [i - 1];
822 window_state* ws = &windows [i];
823
824 ws->x =
825 previous_ws->x + selection_position.x + mw->menu.shadow_thickness;
826 if (!mw->menu.horizontal || i > 1)
827 ws->x += mw->menu.shadow_thickness;
828 ws->y =
829 previous_ws->y + selection_position.y + mw->menu.shadow_thickness;
830
831 size_menu (mw, i);
832
833 fit_to_screen (mw, ws, previous_ws, mw->menu.horizontal && i == 1);
834
835 XClearWindow (XtDisplay (mw), ws->window);
836 XMoveResizeWindow (XtDisplay (mw), ws->window, ws->x, ws->y,
837 ws->width, ws->height);
838 XMapRaised (XtDisplay (mw), ws->window);
839 display_menu (mw, i, False, &selection_position, NULL, NULL, NULL, NULL);
840 }
841
842 /* unmap the menus that popped down */
843 for (i = new_depth - 1; i < old_depth; i++)
844 if (i >= new_depth || !new_stack [i]->contents)
845 XUnmapWindow (XtDisplay (mw), windows [i].window);
846 }
847
848 static Boolean
849 motion_event_is_in_menu (mw, ev, level, relative_pos)
850 XlwMenuWidget mw;
851 XMotionEvent* ev;
852 int level;
853 XPoint* relative_pos;
854 {
855 window_state* ws = &mw->menu.windows [level];
856 int x = level == 0 ? ws->x : ws->x + mw->menu.shadow_thickness;
857 int y = level == 0 ? ws->y : ws->y + mw->menu.shadow_thickness;
858 relative_pos->x = ev->x_root - x;
859 relative_pos->y = ev->y_root - y;
860 return (x < ev->x_root && ev->x_root < x + ws->width
861 && y < ev->y_root && ev->y_root < y + ws->height);
862 }
863
864 static Boolean
865 map_event_to_widget_value (mw, ev, val, level)
866 XlwMenuWidget mw;
867 XMotionEvent* ev;
868 widget_value** val;
869 int* level;
870 {
871 int i;
872 XPoint relative_pos;
873 window_state* ws;
874
875 *val = NULL;
876
877 /* Find the window */
878 for (i = mw->menu.old_depth - 1; i >= 0; i--)
879 {
880 ws = &mw->menu.windows [i];
881 if (ws && motion_event_is_in_menu (mw, ev, i, &relative_pos))
882 {
883 display_menu (mw, i, True, NULL, &relative_pos, val, NULL, NULL);
884
885 if (*val)
886 {
887 *level = i + 1;
888 return True;
889 }
890 }
891 }
892 return False;
893 }
894
895 \f/* Procedures */
896 static void
897 make_drawing_gcs (mw)
898 XlwMenuWidget mw;
899 {
900 XGCValues xgcv;
901
902 xgcv.font = mw->menu.font->fid;
903 xgcv.foreground = mw->menu.foreground;
904 xgcv.background = mw->core.background_pixel;
905 mw->menu.foreground_gc = XtGetGC ((Widget)mw,
906 GCFont | GCForeground | GCBackground,
907 &xgcv);
908
909 xgcv.font = mw->menu.font->fid;
910 xgcv.foreground = mw->menu.button_foreground;
911 xgcv.background = mw->core.background_pixel;
912 mw->menu.button_gc = XtGetGC ((Widget)mw,
913 GCFont | GCForeground | GCBackground,
914 &xgcv);
915
916 xgcv.font = mw->menu.font->fid;
917 xgcv.foreground = mw->menu.foreground;
918 xgcv.background = mw->core.background_pixel;
919 xgcv.fill_style = FillStippled;
920 xgcv.stipple = mw->menu.gray_pixmap;
921 mw->menu.inactive_gc = XtGetGC ((Widget)mw,
922 (GCFont | GCForeground | GCBackground
923 | GCFillStyle | GCStipple), &xgcv);
924
925 xgcv.font = mw->menu.font->fid;
926 xgcv.foreground = mw->menu.button_foreground;
927 xgcv.background = mw->core.background_pixel;
928 xgcv.fill_style = FillStippled;
929 xgcv.stipple = mw->menu.gray_pixmap;
930 mw->menu.inactive_button_gc = XtGetGC ((Widget)mw,
931 (GCFont | GCForeground | GCBackground
932 | GCFillStyle | GCStipple), &xgcv);
933
934 xgcv.font = mw->menu.font->fid;
935 xgcv.foreground = mw->core.background_pixel;
936 xgcv.background = mw->menu.foreground;
937 mw->menu.background_gc = XtGetGC ((Widget)mw,
938 GCFont | GCForeground | GCBackground,
939 &xgcv);
940 }
941
942 static void
943 release_drawing_gcs (mw)
944 XlwMenuWidget mw;
945 {
946 XtReleaseGC ((Widget) mw, mw->menu.foreground_gc);
947 XtReleaseGC ((Widget) mw, mw->menu.button_gc);
948 XtReleaseGC ((Widget) mw, mw->menu.inactive_gc);
949 XtReleaseGC ((Widget) mw, mw->menu.inactive_button_gc);
950 XtReleaseGC ((Widget) mw, mw->menu.background_gc);
951 /* let's get some segvs if we try to use these... */
952 mw->menu.foreground_gc = (GC) -1;
953 mw->menu.button_gc = (GC) -1;
954 mw->menu.inactive_gc = (GC) -1;
955 mw->menu.inactive_button_gc = (GC) -1;
956 mw->menu.background_gc = (GC) -1;
957 }
958
959 #define MINL(x,y) ((((unsigned long) (x)) < ((unsigned long) (y))) \
960 ? ((unsigned long) (x)) : ((unsigned long) (y)))
961
962 static void
963 make_shadow_gcs (mw)
964 XlwMenuWidget mw;
965 {
966 XGCValues xgcv;
967 unsigned long pm = 0;
968 Display *dpy = XtDisplay ((Widget) mw);
969 Colormap cmap = DefaultColormapOfScreen (XtScreen ((Widget) mw));
970 XColor topc, botc;
971 int top_frobbed = 0, bottom_frobbed = 0;
972
973 if (mw->menu.top_shadow_color == -1)
974 mw->menu.top_shadow_color = mw->core.background_pixel;
975 if (mw->menu.bottom_shadow_color == -1)
976 mw->menu.bottom_shadow_color = mw->menu.foreground;
977
978 if (mw->menu.top_shadow_color == mw->core.background_pixel ||
979 mw->menu.top_shadow_color == mw->menu.foreground)
980 {
981 topc.pixel = mw->core.background_pixel;
982 XQueryColor (dpy, cmap, &topc);
983 /* don't overflow/wrap! */
984 topc.red = MINL (65535, topc.red * 1.2);
985 topc.green = MINL (65535, topc.green * 1.2);
986 topc.blue = MINL (65535, topc.blue * 1.2);
987 if (XAllocColor (dpy, cmap, &topc))
988 {
989 mw->menu.top_shadow_color = topc.pixel;
990 top_frobbed = 1;
991 }
992 }
993 if (mw->menu.bottom_shadow_color == mw->menu.foreground ||
994 mw->menu.bottom_shadow_color == mw->core.background_pixel)
995 {
996 botc.pixel = mw->core.background_pixel;
997 XQueryColor (dpy, cmap, &botc);
998 botc.red *= 0.6;
999 botc.green *= 0.6;
1000 botc.blue *= 0.6;
1001 if (XAllocColor (dpy, cmap, &botc))
1002 {
1003 mw->menu.bottom_shadow_color = botc.pixel;
1004 bottom_frobbed = 1;
1005 }
1006 }
1007
1008 if (top_frobbed && bottom_frobbed)
1009 {
1010 int top_avg = ((topc.red / 3) + (topc.green / 3) + (topc.blue / 3));
1011 int bot_avg = ((botc.red / 3) + (botc.green / 3) + (botc.blue / 3));
1012 if (bot_avg > top_avg)
1013 {
1014 Pixel tmp = mw->menu.top_shadow_color;
1015 mw->menu.top_shadow_color = mw->menu.bottom_shadow_color;
1016 mw->menu.bottom_shadow_color = tmp;
1017 }
1018 else if (topc.pixel == botc.pixel)
1019 {
1020 if (botc.pixel == mw->menu.foreground)
1021 mw->menu.top_shadow_color = mw->core.background_pixel;
1022 else
1023 mw->menu.bottom_shadow_color = mw->menu.foreground;
1024 }
1025 }
1026
1027 if (!mw->menu.top_shadow_pixmap &&
1028 mw->menu.top_shadow_color == mw->core.background_pixel)
1029 {
1030 mw->menu.top_shadow_pixmap = mw->menu.gray_pixmap;
1031 mw->menu.top_shadow_color = mw->menu.foreground;
1032 }
1033 if (!mw->menu.bottom_shadow_pixmap &&
1034 mw->menu.bottom_shadow_color == mw->core.background_pixel)
1035 {
1036 mw->menu.bottom_shadow_pixmap = mw->menu.gray_pixmap;
1037 mw->menu.bottom_shadow_color = mw->menu.foreground;
1038 }
1039
1040 xgcv.fill_style = FillStippled;
1041 xgcv.foreground = mw->menu.top_shadow_color;
1042 xgcv.stipple = mw->menu.top_shadow_pixmap;
1043 pm = (xgcv.stipple ? GCStipple|GCFillStyle : 0);
1044 mw->menu.shadow_top_gc = XtGetGC ((Widget)mw, GCForeground | pm, &xgcv);
1045
1046 xgcv.foreground = mw->menu.bottom_shadow_color;
1047 xgcv.stipple = mw->menu.bottom_shadow_pixmap;
1048 pm = (xgcv.stipple ? GCStipple|GCFillStyle : 0);
1049 mw->menu.shadow_bottom_gc = XtGetGC ((Widget)mw, GCForeground | pm, &xgcv);
1050 }
1051
1052
1053 static void
1054 release_shadow_gcs (mw)
1055 XlwMenuWidget mw;
1056 {
1057 XtReleaseGC ((Widget) mw, mw->menu.shadow_top_gc);
1058 XtReleaseGC ((Widget) mw, mw->menu.shadow_bottom_gc);
1059 }
1060
1061 static void
1062 XlwMenuInitialize (request, new, args, num_args)
1063 Widget request;
1064 Widget new;
1065 ArgList args;
1066 Cardinal *num_args;
1067 {
1068 /* Get the GCs and the widget size */
1069 XlwMenuWidget mw = (XlwMenuWidget)new;
1070
1071 XSetWindowAttributes xswa;
1072 int mask;
1073
1074 Window window = RootWindowOfScreen (DefaultScreenOfDisplay (XtDisplay (mw)));
1075 Display* display = XtDisplay (mw);
1076
1077 #if 0
1078 widget_value *tem = (widget_value *) XtMalloc (sizeof (widget_value));
1079
1080 /* _XtCreate is freeing the object that was passed to us,
1081 so make a copy that we will actually keep. */
1082 lwlib_bcopy (mw->menu.contents, tem, sizeof (widget_value));
1083 mw->menu.contents = tem;
1084 #endif
1085
1086 /* mw->menu.cursor = XCreateFontCursor (display, mw->menu.cursor_shape); */
1087 mw->menu.cursor = mw->menu.cursor_shape;
1088
1089 mw->menu.gray_pixmap = XCreatePixmapFromBitmapData (display, window,
1090 gray_bits, gray_width,
1091 gray_height, 1, 0, 1);
1092
1093 make_drawing_gcs (mw);
1094 make_shadow_gcs (mw);
1095
1096 xswa.background_pixel = mw->core.background_pixel;
1097 xswa.border_pixel = mw->core.border_pixel;
1098 mask = CWBackPixel | CWBorderPixel;
1099
1100 mw->menu.popped_up = False;
1101
1102 mw->menu.old_depth = 1;
1103 mw->menu.old_stack = (widget_value**)XtMalloc (sizeof (widget_value*));
1104 mw->menu.old_stack_length = 1;
1105 mw->menu.old_stack [0] = mw->menu.contents;
1106
1107 mw->menu.new_depth = 0;
1108 mw->menu.new_stack = 0;
1109 mw->menu.new_stack_length = 0;
1110 push_new_stack (mw, mw->menu.contents);
1111
1112 mw->menu.windows = (window_state*)XtMalloc (sizeof (window_state));
1113 mw->menu.windows_length = 1;
1114 mw->menu.windows [0].x = 0;
1115 mw->menu.windows [0].y = 0;
1116 mw->menu.windows [0].width = 0;
1117 mw->menu.windows [0].height = 0;
1118 size_menu (mw, 0);
1119
1120 mw->core.width = mw->menu.windows [0].width;
1121 mw->core.height = mw->menu.windows [0].height;
1122 }
1123
1124 static void
1125 XlwMenuClassInitialize ()
1126 {
1127 }
1128
1129 static void
1130 XlwMenuRealize (w, valueMask, attributes)
1131 Widget w;
1132 Mask *valueMask;
1133 XSetWindowAttributes *attributes;
1134 {
1135 XlwMenuWidget mw = (XlwMenuWidget)w;
1136 XSetWindowAttributes xswa;
1137 int mask;
1138
1139 (*xlwMenuWidgetClass->core_class.superclass->core_class.realize)
1140 (w, valueMask, attributes);
1141
1142 xswa.save_under = True;
1143 xswa.cursor = mw->menu.cursor_shape;
1144 mask = CWSaveUnder | CWCursor;
1145 XChangeWindowAttributes (XtDisplay (w), XtWindow (w), mask, &xswa);
1146
1147 mw->menu.windows [0].window = XtWindow (w);
1148 mw->menu.windows [0].x = w->core.x;
1149 mw->menu.windows [0].y = w->core.y;
1150 mw->menu.windows [0].width = w->core.width;
1151 mw->menu.windows [0].height = w->core.height;
1152 }
1153
1154 /* Only the toplevel menubar/popup is a widget so it's the only one that
1155 receives expose events through Xt. So we repaint all the other panes
1156 when receiving an Expose event. */
1157 static void
1158 XlwMenuRedisplay (w, ev, region)
1159 Widget w;
1160 XEvent* ev;
1161 Region region;
1162 {
1163 XlwMenuWidget mw = (XlwMenuWidget)w;
1164 int i;
1165
1166 /* If we have a depth beyond 1, it's because a submenu was displayed.
1167 If the submenu has been destroyed, set the depth back to 1. */
1168 if (submenu_destroyed)
1169 {
1170 mw->menu.old_depth = 1;
1171 submenu_destroyed = 0;
1172 }
1173
1174 for (i = 0; i < mw->menu.old_depth; i++)
1175 display_menu (mw, i, False, NULL, NULL, NULL, NULL, NULL);
1176 }
1177
1178 static void
1179 XlwMenuDestroy (w)
1180 Widget w;
1181 {
1182 int i;
1183 XlwMenuWidget mw = (XlwMenuWidget) w;
1184
1185 if (pointer_grabbed)
1186 XtUngrabPointer ((Widget)w, CurrentTime);
1187 pointer_grabbed = 0;
1188
1189 submenu_destroyed = 1;
1190
1191 release_drawing_gcs (mw);
1192 release_shadow_gcs (mw);
1193
1194 /* this doesn't come from the resource db but is created explicitly
1195 so we must free it ourselves. */
1196 XFreePixmap (XtDisplay (mw), mw->menu.gray_pixmap);
1197 mw->menu.gray_pixmap = (Pixmap) -1;
1198
1199 #if 0
1200 /* Do free mw->menu.contents because nowadays we copy it
1201 during initialization. */
1202 XtFree (mw->menu.contents);
1203 #endif
1204
1205 /* Don't free mw->menu.contents because that comes from our creator.
1206 The `*_stack' elements are just pointers into `contents' so leave
1207 that alone too. But free the stacks themselves. */
1208 if (mw->menu.old_stack) XtFree ((char *) mw->menu.old_stack);
1209 if (mw->menu.new_stack) XtFree ((char *) mw->menu.new_stack);
1210
1211 /* Remember, you can't free anything that came from the resource
1212 database. This includes:
1213 mw->menu.cursor
1214 mw->menu.top_shadow_pixmap
1215 mw->menu.bottom_shadow_pixmap
1216 mw->menu.font
1217 Also the color cells of top_shadow_color, bottom_shadow_color,
1218 foreground, and button_foreground will never be freed until this
1219 client exits. Nice, eh?
1220 */
1221
1222 /* start from 1 because the one in slot 0 is w->core.window */
1223 for (i = 1; i < mw->menu.windows_length; i++)
1224 XDestroyWindow (XtDisplay (mw), mw->menu.windows [i].window);
1225 if (mw->menu.windows)
1226 XtFree ((char *) mw->menu.windows);
1227 }
1228
1229 static Boolean
1230 XlwMenuSetValues (current, request, new)
1231 Widget current;
1232 Widget request;
1233 Widget new;
1234 {
1235 XlwMenuWidget oldmw = (XlwMenuWidget)current;
1236 XlwMenuWidget newmw = (XlwMenuWidget)new;
1237 Boolean redisplay = False;
1238 int i;
1239
1240 if (newmw->menu.contents
1241 && newmw->menu.contents->contents
1242 && newmw->menu.contents->contents->change >= VISIBLE_CHANGE)
1243 redisplay = True;
1244
1245 if (newmw->core.background_pixel != oldmw->core.background_pixel
1246 || newmw->menu.foreground != oldmw->menu.foreground
1247 || newmw->menu.font != oldmw->menu.font)
1248 {
1249 release_drawing_gcs (newmw);
1250 make_drawing_gcs (newmw);
1251 redisplay = True;
1252
1253 for (i = 0; i < oldmw->menu.windows_length; i++)
1254 {
1255 XSetWindowBackground (XtDisplay (oldmw),
1256 oldmw->menu.windows [i].window,
1257 newmw->core.background_pixel);
1258 /* clear windows and generate expose events */
1259 XClearArea (XtDisplay (oldmw), oldmw->menu.windows[i].window,
1260 0, 0, 0, 0, True);
1261 }
1262 }
1263
1264 return redisplay;
1265 }
1266
1267 static void
1268 XlwMenuResize (w)
1269 Widget w;
1270 {
1271 XlwMenuWidget mw = (XlwMenuWidget)w;
1272
1273 if (mw->menu.popped_up)
1274 {
1275 /* Don't allow the popup menu to resize itself. */
1276 mw->core.width = mw->menu.windows [0].width;
1277 mw->core.height = mw->menu.windows [0].height;
1278 mw->core.parent->core.width = mw->core.width ;
1279 mw->core.parent->core.height = mw->core.height ;
1280 }
1281 else
1282 {
1283 mw->menu.windows [0].width = mw->core.width;
1284 mw->menu.windows [0].height = mw->core.height;
1285 }
1286 }
1287
1288 \f/* Action procedures */
1289 static void
1290 handle_single_motion_event (mw, ev)
1291 XlwMenuWidget mw;
1292 XMotionEvent* ev;
1293 {
1294 widget_value* val;
1295 int level;
1296
1297 if (!map_event_to_widget_value (mw, ev, &val, &level))
1298 pop_new_stack_if_no_contents (mw);
1299 else
1300 set_new_state (mw, val, level);
1301 remap_menubar (mw);
1302
1303 /* Sync with the display. Makes it feel better on X terms. */
1304 XSync (XtDisplay (mw), False);
1305 }
1306
1307 static void
1308 handle_motion_event (mw, ev)
1309 XlwMenuWidget mw;
1310 XMotionEvent* ev;
1311 {
1312 int x = ev->x_root;
1313 int y = ev->y_root;
1314 int state = ev->state;
1315
1316 handle_single_motion_event (mw, ev);
1317
1318 /* allow motion events to be generated again */
1319 if (ev->is_hint
1320 && XQueryPointer (XtDisplay (mw), ev->window,
1321 &ev->root, &ev->subwindow,
1322 &ev->x_root, &ev->y_root,
1323 &ev->x, &ev->y,
1324 &ev->state)
1325 && ev->state == state
1326 && (ev->x_root != x || ev->y_root != y))
1327 handle_single_motion_event (mw, ev);
1328 }
1329
1330 static void
1331 Start (w, ev, params, num_params)
1332 Widget w;
1333 XEvent *ev;
1334 String *params;
1335 Cardinal *num_params;
1336 {
1337 XlwMenuWidget mw = (XlwMenuWidget)w;
1338
1339 if (!mw->menu.popped_up)
1340 {
1341 menu_post_event = *ev;
1342 pop_up_menu (mw, ev);
1343 }
1344 else
1345 {
1346 /* If we push a button while the menu is posted semipermanently,
1347 releasing the button should always pop the menu down. */
1348 next_release_must_exit = 1;
1349
1350 /* notes the absolute position of the menubar window */
1351 mw->menu.windows [0].x = ev->xmotion.x_root - ev->xmotion.x;
1352 mw->menu.windows [0].y = ev->xmotion.y_root - ev->xmotion.y;
1353
1354 /* handles the down like a move, slots are compatible */
1355 handle_motion_event (mw, &ev->xmotion);
1356 }
1357 }
1358
1359 static void
1360 Drag (w, ev, params, num_params)
1361 Widget w;
1362 XEvent *ev;
1363 String *params;
1364 Cardinal *num_params;
1365 {
1366 XlwMenuWidget mw = (XlwMenuWidget)w;
1367 handle_motion_event (mw, &ev->xmotion);
1368 }
1369
1370 static void
1371 Select (w, ev, params, num_params)
1372 Widget w;
1373 XEvent *ev;
1374 String *params;
1375 Cardinal *num_params;
1376 {
1377 XlwMenuWidget mw = (XlwMenuWidget)w;
1378 widget_value* selected_item = mw->menu.old_stack [mw->menu.old_depth - 1];
1379
1380 /* If user releases the button quickly, without selecting anything,
1381 after the initial down-click that brought the menu up,
1382 do nothing. */
1383 if ((selected_item == 0
1384 || ((widget_value *) selected_item)->call_data == 0)
1385 && !next_release_must_exit
1386 && (ev->xbutton.time - menu_post_event.xbutton.time
1387 < XtGetMultiClickTime (XtDisplay (w))))
1388 return;
1389
1390 /* pop down everything. */
1391 mw->menu.new_depth = 1;
1392 remap_menubar (mw);
1393
1394 if (mw->menu.popped_up)
1395 {
1396 mw->menu.popped_up = False;
1397 XtUngrabPointer ((Widget)mw, ev->xmotion.time);
1398 if (XtIsShell (XtParent ((Widget) mw)))
1399 XtPopdown (XtParent ((Widget) mw));
1400 else
1401 {
1402 XtRemoveGrab ((Widget) mw);
1403 display_menu (mw, 0, False, NULL, NULL, NULL, NULL, NULL);
1404 }
1405 }
1406
1407 /* callback */
1408 XtCallCallbackList ((Widget)mw, mw->menu.select, (XtPointer)selected_item);
1409 }
1410
1411
1412 \f/* Special code to pop-up a menu */
1413 void
1414 pop_up_menu (mw, event)
1415 XlwMenuWidget mw;
1416 XButtonPressedEvent* event;
1417 {
1418 int x = event->x_root;
1419 int y = event->y_root;
1420 int w;
1421 int h;
1422 int borderwidth = mw->menu.shadow_thickness;
1423 Screen* screen = XtScreen (mw);
1424 Display *display = XtDisplay (mw);
1425
1426 next_release_must_exit = 0;
1427
1428 XtCallCallbackList ((Widget)mw, mw->menu.open, NULL);
1429
1430 if (XtIsShell (XtParent ((Widget)mw)))
1431 size_menu (mw, 0);
1432
1433 w = mw->menu.windows [0].width;
1434 h = mw->menu.windows [0].height;
1435
1436 x -= borderwidth;
1437 y -= borderwidth;
1438 if (x < borderwidth)
1439 x = borderwidth;
1440 if (x + w + 2 * borderwidth > WidthOfScreen (screen))
1441 x = WidthOfScreen (screen) - w - 2 * borderwidth;
1442 if (y < borderwidth)
1443 y = borderwidth;
1444 if (y + h + 2 * borderwidth> HeightOfScreen (screen))
1445 y = HeightOfScreen (screen) - h - 2 * borderwidth;
1446
1447 mw->menu.popped_up = True;
1448 if (XtIsShell (XtParent ((Widget)mw)))
1449 {
1450 XtConfigureWidget (XtParent ((Widget)mw), x, y, w, h,
1451 XtParent ((Widget)mw)->core.border_width);
1452 XtPopup (XtParent ((Widget)mw), XtGrabExclusive);
1453 display_menu (mw, 0, False, NULL, NULL, NULL, NULL, NULL);
1454 mw->menu.windows [0].x = x + borderwidth;
1455 mw->menu.windows [0].y = y + borderwidth;
1456 }
1457 else
1458 {
1459 XEvent *ev = (XEvent *) event;
1460
1461 XtAddGrab ((Widget) mw, True, True);
1462
1463 /* notes the absolute position of the menubar window */
1464 mw->menu.windows [0].x = ev->xmotion.x_root - ev->xmotion.x;
1465 mw->menu.windows [0].y = ev->xmotion.y_root - ev->xmotion.y;
1466 }
1467
1468 #ifdef emacs
1469 x_catch_errors (display);
1470 #endif
1471 XtGrabPointer ((Widget)mw, False,
1472 (PointerMotionMask
1473 | PointerMotionHintMask
1474 | ButtonReleaseMask
1475 | ButtonPressMask),
1476 GrabModeAsync, GrabModeAsync, None,
1477 mw->menu.cursor_shape,
1478 event->time);
1479 pointer_grabbed = 1;
1480 #ifdef emacs
1481 if (x_had_errors_p (display))
1482 {
1483 pointer_grabbed = 0;
1484 XtUngrabPointer ((Widget)mw, event->time);
1485 }
1486 x_uncatch_errors (display);
1487 #endif
1488
1489 handle_motion_event (mw, (XMotionEvent*)event);
1490 }