Add support for PowerPC Linux
[emacs/old-mirror.git] / lwlib / xlwmenu.c
blobc2aae8d0e61cbe07e5b1fad274cf08217af61495
1 /* Implements a lightweight menubar widget.
2 Copyright (C) 1992 Lucid, Inc.
4 This file is part of the Lucid Widget Library.
6 The Lucid Widget Library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
9 any later version.
11 The Lucid Widget Library is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 Alongalong with GNU Emacs; see the file COPYING. If not, write to
18 the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
20 /* Created by devin@lucid.com */
22 #include <stdio.h>
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"
33 static int pointer_grabbed;
34 static XEvent menu_post_event;
36 XFontStruct *xlwmenu_default_font;
38 static char
39 xlwMenuTranslations [] =
40 "<BtnDown>: start()\n\
41 <Motion>: drag()\n\
42 <BtnUp>: select()\n\
43 <Key>Shift_L: nothing()\n\
44 <Key>Shift_R: nothing()\n\
45 <Key>Meta_L: nothing()\n\
46 <Key>Meta_R: nothing()\n\
47 <Key>Control_L: nothing()\n\
48 <Key>Control_R: nothing()\n\
49 <Key>Hyper_L: nothing()\n\
50 <Key>Hyper_R: nothing()\n\
51 <Key>Super_L: nothing()\n\
52 <Key>Super_R: nothing()\n\
53 <Key>Alt_L: nothing()\n\
54 <Key>Alt_R: nothing()\n\
55 <Key>Caps_Lock: nothing()\n\
56 <Key>Shift_Lock: nothing()\n\
57 <KeyUp>Shift_L: nothing()\n\
58 <KeyUp>Shift_R: nothing()\n\
59 <KeyUp>Meta_L: nothing()\n\
60 <KeyUp>Meta_R: nothing()\n\
61 <KeyUp>Control_L: nothing()\n\
62 <KeyUp>Control_R: nothing()\n\
63 <KeyUp>Hyper_L: nothing()\n\
64 <KeyUp>Hyper_R: nothing()\n\
65 <KeyUp>Super_L: nothing()\n\
66 <KeyUp>Super_R: nothing()\n\
67 <KeyUp>Alt_L: nothing()\n\
68 <KeyUp>Alt_R: nothing()\n\
69 <KeyUp>Caps_Lock: nothing()\n\
70 <KeyUp>Shift_Lock:nothing()\n\
71 <Key>: key()\n\
72 <KeyUp>: key()\n\
75 #define offset(field) XtOffset(XlwMenuWidget, field)
76 static XtResource
77 xlwMenuResources[] =
79 {XtNfont, XtCFont, XtRFontStruct, sizeof(XFontStruct *),
80 offset(menu.font),XtRString, "XtDefaultFont"},
81 {XtNforeground, XtCForeground, XtRPixel, sizeof(Pixel),
82 offset(menu.foreground), XtRString, "XtDefaultForeground"},
83 {XtNbuttonForeground, XtCButtonForeground, XtRPixel, sizeof(Pixel),
84 offset(menu.button_foreground), XtRString, "XtDefaultForeground"},
85 {XtNmargin, XtCMargin, XtRDimension, sizeof(Dimension),
86 offset(menu.margin), XtRImmediate, (XtPointer)0},
87 {XtNhorizontalSpacing, XtCMargin, XtRDimension, sizeof(Dimension),
88 offset(menu.horizontal_spacing), XtRImmediate, (XtPointer)3},
89 {XtNverticalSpacing, XtCMargin, XtRDimension, sizeof(Dimension),
90 offset(menu.vertical_spacing), XtRImmediate, (XtPointer)1},
91 {XtNarrowSpacing, XtCMargin, XtRDimension, sizeof(Dimension),
92 offset(menu.arrow_spacing), XtRImmediate, (XtPointer)10},
94 {XmNshadowThickness, XmCShadowThickness, XtRDimension,
95 sizeof (Dimension), offset (menu.shadow_thickness),
96 XtRImmediate, (XtPointer) 2},
97 {XmNtopShadowColor, XmCTopShadowColor, XtRPixel, sizeof (Pixel),
98 offset (menu.top_shadow_color), XtRImmediate, (XtPointer)-1},
99 {XmNbottomShadowColor, XmCBottomShadowColor, XtRPixel, sizeof (Pixel),
100 offset (menu.bottom_shadow_color), XtRImmediate, (XtPointer)-1},
101 {XmNtopShadowPixmap, XmCTopShadowPixmap, XtRPixmap, sizeof (Pixmap),
102 offset (menu.top_shadow_pixmap), XtRImmediate, (XtPointer)None},
103 {XmNbottomShadowPixmap, XmCBottomShadowPixmap, XtRPixmap, sizeof (Pixmap),
104 offset (menu.bottom_shadow_pixmap), XtRImmediate, (XtPointer)None},
106 {XtNopen, XtCCallback, XtRCallback, sizeof(XtPointer),
107 offset(menu.open), XtRCallback, (XtPointer)NULL},
108 {XtNselect, XtCCallback, XtRCallback, sizeof(XtPointer),
109 offset(menu.select), XtRCallback, (XtPointer)NULL},
110 {XtNmenu, XtCMenu, XtRPointer, sizeof(XtPointer),
111 offset(menu.contents), XtRImmediate, (XtPointer)NULL},
112 {XtNcursor, XtCCursor, XtRCursor, sizeof(Cursor),
113 offset(menu.cursor_shape), XtRString, (XtPointer)"right_ptr"},
114 {XtNhorizontal, XtCHorizontal, XtRInt, sizeof(int),
115 offset(menu.horizontal), XtRImmediate, (XtPointer)True},
117 #undef offset
119 static Boolean XlwMenuSetValues();
120 static void XlwMenuRealize();
121 static void XlwMenuRedisplay();
122 static void XlwMenuResize();
123 static void XlwMenuInitialize();
124 static void XlwMenuRedisplay();
125 static void XlwMenuDestroy();
126 static void XlwMenuClassInitialize();
127 static void Start();
128 static void Drag();
129 static void Select();
130 static void Key();
131 static void Nothing();
133 static XtActionsRec
134 xlwMenuActionsList [] =
136 {"start", Start},
137 {"drag", Drag},
138 {"select", Select},
139 {"key", Key},
140 {"nothing", Nothing},
143 #define SuperClass ((CoreWidgetClass)&coreClassRec)
145 XlwMenuClassRec xlwMenuClassRec =
147 { /* CoreClass fields initialization */
148 (WidgetClass) SuperClass, /* superclass */
149 "XlwMenu", /* class_name */
150 sizeof(XlwMenuRec), /* size */
151 XlwMenuClassInitialize, /* class_initialize */
152 NULL, /* class_part_initialize */
153 FALSE, /* class_inited */
154 XlwMenuInitialize, /* initialize */
155 NULL, /* initialize_hook */
156 XlwMenuRealize, /* realize */
157 xlwMenuActionsList, /* actions */
158 XtNumber(xlwMenuActionsList), /* num_actions */
159 xlwMenuResources, /* resources */
160 XtNumber(xlwMenuResources), /* resource_count */
161 NULLQUARK, /* xrm_class */
162 TRUE, /* compress_motion */
163 TRUE, /* compress_exposure */
164 TRUE, /* compress_enterleave */
165 FALSE, /* visible_interest */
166 XlwMenuDestroy, /* destroy */
167 XlwMenuResize, /* resize */
168 XlwMenuRedisplay, /* expose */
169 XlwMenuSetValues, /* set_values */
170 NULL, /* set_values_hook */
171 XtInheritSetValuesAlmost, /* set_values_almost */
172 NULL, /* get_values_hook */
173 NULL, /* accept_focus */
174 XtVersion, /* version */
175 NULL, /* callback_private */
176 xlwMenuTranslations, /* tm_table */
177 XtInheritQueryGeometry, /* query_geometry */
178 XtInheritDisplayAccelerator, /* display_accelerator */
179 NULL /* extension */
180 }, /* XlwMenuClass fields initialization */
182 0 /* dummy */
186 WidgetClass xlwMenuWidgetClass = (WidgetClass) &xlwMenuClassRec;
188 int submenu_destroyed;
190 static int next_release_must_exit;
192 \f/* Utilities */
193 static void
194 push_new_stack (mw, val)
195 XlwMenuWidget mw;
196 widget_value* val;
198 if (!mw->menu.new_stack)
200 mw->menu.new_stack_length = 10;
201 mw->menu.new_stack =
202 (widget_value**)XtCalloc (mw->menu.new_stack_length,
203 sizeof (widget_value*));
205 else if (mw->menu.new_depth == mw->menu.new_stack_length)
207 mw->menu.new_stack_length *= 2;
208 mw->menu.new_stack =
209 (widget_value**)XtRealloc ((char*)mw->menu.new_stack,
210 mw->menu.new_stack_length * sizeof (widget_value*));
212 mw->menu.new_stack [mw->menu.new_depth++] = val;
215 static void
216 pop_new_stack_if_no_contents (mw)
217 XlwMenuWidget mw;
219 if (mw->menu.new_depth)
221 if (!mw->menu.new_stack [mw->menu.new_depth - 1]->contents)
222 mw->menu.new_depth -= 1;
226 static void
227 make_old_stack_space (mw, n)
228 XlwMenuWidget mw;
229 int n;
231 if (!mw->menu.old_stack)
233 mw->menu.old_stack_length = 10;
234 mw->menu.old_stack =
235 (widget_value**)XtCalloc (mw->menu.old_stack_length,
236 sizeof (widget_value*));
238 else if (mw->menu.old_stack_length < n)
240 mw->menu.old_stack_length *= 2;
241 mw->menu.old_stack =
242 (widget_value**)XtRealloc ((char*)mw->menu.old_stack,
243 mw->menu.old_stack_length * sizeof (widget_value*));
247 \f/* Size code */
248 static Boolean
249 all_dashes_p (s)
250 char *s;
252 char* p;
253 for (p = s; *p == '-'; p++);
254 return !*p;
258 string_width (mw, s)
259 XlwMenuWidget mw;
260 char *s;
262 XCharStruct xcs;
263 int drop;
265 XTextExtents (mw->menu.font, s, strlen (s), &drop, &drop, &drop, &xcs);
266 return xcs.width;
269 static int
270 arrow_width (mw)
271 XlwMenuWidget mw;
273 return mw->menu.font->ascent / 2 | 1;
276 static XtResource
277 nameResource[] =
279 {"labelString", "LabelString", XtRString, sizeof(String),
280 0, XtRImmediate, 0},
283 static char*
284 resource_widget_value (mw, val)
285 XlwMenuWidget mw;
286 widget_value *val;
288 if (!val->toolkit_data)
290 char* resourced_name = NULL;
291 char* complete_name;
292 XtGetSubresources ((Widget) mw,
293 (XtPointer) &resourced_name,
294 val->name, val->name,
295 nameResource, 1, NULL, 0);
296 if (!resourced_name)
297 resourced_name = val->name;
298 if (!val->value)
300 complete_name = (char *) XtMalloc (strlen (resourced_name) + 1);
301 strcpy (complete_name, resourced_name);
303 else
305 int complete_length =
306 strlen (resourced_name) + strlen (val->value) + 2;
307 complete_name = XtMalloc (complete_length);
308 *complete_name = 0;
309 strcat (complete_name, resourced_name);
310 strcat (complete_name, " ");
311 strcat (complete_name, val->value);
314 val->toolkit_data = complete_name;
315 val->free_toolkit_data = True;
317 return (char*)val->toolkit_data;
320 /* Returns the sizes of an item */
321 static void
322 size_menu_item (mw, val, horizontal_p, label_width, rest_width, height)
323 XlwMenuWidget mw;
324 widget_value* val;
325 int horizontal_p;
326 int* label_width;
327 int* rest_width;
328 int* height;
330 if (all_dashes_p (val->name))
332 *height = 2;
333 *label_width = 1;
334 *rest_width = 0;
336 else
338 *height =
339 mw->menu.font->ascent + mw->menu.font->descent
340 + 2 * mw->menu.vertical_spacing + 2 * mw->menu.shadow_thickness;
342 *label_width =
343 string_width (mw, resource_widget_value (mw, val))
344 + mw->menu.horizontal_spacing + mw->menu.shadow_thickness;
346 *rest_width = mw->menu.horizontal_spacing + mw->menu.shadow_thickness;
347 if (!horizontal_p)
349 if (val->contents)
350 *rest_width += arrow_width (mw) + mw->menu.arrow_spacing;
351 else if (val->key)
352 *rest_width +=
353 string_width (mw, val->key) + mw->menu.arrow_spacing;
358 static void
359 size_menu (mw, level)
360 XlwMenuWidget mw;
361 int level;
363 unsigned int label_width = 0;
364 int rest_width = 0;
365 int max_rest_width = 0;
366 unsigned int height = 0;
367 int horizontal_p = mw->menu.horizontal && (level == 0);
368 widget_value* val;
369 window_state* ws;
371 if (level >= mw->menu.old_depth)
372 abort ();
374 ws = &mw->menu.windows [level];
375 ws->width = 0;
376 ws->height = 0;
377 ws->label_width = 0;
379 for (val = mw->menu.old_stack [level]->contents; val; val = val->next)
381 size_menu_item (mw, val, horizontal_p, &label_width, &rest_width,
382 &height);
383 if (horizontal_p)
385 ws->width += label_width + rest_width;
386 if (height > ws->height)
387 ws->height = height;
389 else
391 if (label_width > ws->label_width)
392 ws->label_width = label_width;
393 if (rest_width > max_rest_width)
394 max_rest_width = rest_width;
395 ws->height += height;
399 if (horizontal_p)
400 ws->label_width = 0;
401 else
402 ws->width = ws->label_width + max_rest_width;
404 ws->width += 2 * mw->menu.shadow_thickness;
405 ws->height += 2 * mw->menu.shadow_thickness;
409 \f/* Display code */
410 static void
411 draw_arrow (mw, window, gc, x, y, width)
412 XlwMenuWidget mw;
413 Window window;
414 GC gc;
415 int x;
416 int y;
417 int width;
419 XPoint points [3];
420 points [0].x = x;
421 points [0].y = y + mw->menu.font->ascent;
422 points [1].x = x;
423 points [1].y = y;
424 points [2].x = x + width;
425 points [2].y = y + mw->menu.font->ascent / 2;
427 XFillPolygon (XtDisplay (mw), window, gc, points, 3, Convex,
428 CoordModeOrigin);
431 static void
432 draw_shadow_rectangle (mw, window, x, y, width, height, erase_p)
433 XlwMenuWidget mw;
434 Window window;
435 int x;
436 int y;
437 int width;
438 int height;
439 int erase_p;
441 Display *dpy = XtDisplay (mw);
442 GC top_gc = !erase_p ? mw->menu.shadow_top_gc : mw->menu.background_gc;
443 GC bottom_gc = !erase_p ? mw->menu.shadow_bottom_gc : mw->menu.background_gc;
444 int thickness = mw->menu.shadow_thickness;
445 XPoint points [4];
446 points [0].x = x;
447 points [0].y = y;
448 points [1].x = x + width;
449 points [1].y = y;
450 points [2].x = x + width - thickness;
451 points [2].y = y + thickness;
452 points [3].x = x;
453 points [3].y = y + thickness;
454 XFillPolygon (dpy, window, top_gc, points, 4, Convex, CoordModeOrigin);
455 points [0].x = x;
456 points [0].y = y + thickness;
457 points [1].x = x;
458 points [1].y = y + height;
459 points [2].x = x + thickness;
460 points [2].y = y + height - thickness;
461 points [3].x = x + thickness;
462 points [3].y = y + thickness;
463 XFillPolygon (dpy, window, top_gc, points, 4, Convex, CoordModeOrigin);
464 points [0].x = x + width;
465 points [0].y = y;
466 points [1].x = x + width - thickness;
467 points [1].y = y + thickness;
468 points [2].x = x + width - thickness;
469 points [2].y = y + height - thickness;
470 points [3].x = x + width;
471 points [3].y = y + height - thickness;
472 XFillPolygon (dpy, window, bottom_gc, points, 4, Convex, CoordModeOrigin);
473 points [0].x = x;
474 points [0].y = y + height;
475 points [1].x = x + width;
476 points [1].y = y + height;
477 points [2].x = x + width;
478 points [2].y = y + height - thickness;
479 points [3].x = x + thickness;
480 points [3].y = y + height - thickness;
481 XFillPolygon (dpy, window, bottom_gc, points, 4, Convex, CoordModeOrigin);
485 /* Display the menu item and increment where.x and where.y to show how large
486 ** the menu item was.
488 static void
489 display_menu_item (mw, val, ws, where, highlighted_p, horizontal_p, just_compute_p)
490 XlwMenuWidget mw;
491 widget_value* val;
492 window_state* ws;
493 XPoint* where;
494 Boolean highlighted_p;
495 Boolean horizontal_p;
496 Boolean just_compute_p;
498 GC deco_gc;
499 GC text_gc;
500 int font_ascent = mw->menu.font->ascent;
501 int font_descent = mw->menu.font->descent;
502 int shadow = mw->menu.shadow_thickness;
503 int separator_p = all_dashes_p (val->name);
504 int h_spacing = mw->menu.horizontal_spacing;
505 int v_spacing = mw->menu.vertical_spacing;
506 int label_width;
507 int rest_width;
508 int height;
509 int width;
510 int button_p;
512 /* compute the sizes of the item */
513 size_menu_item (mw, val, horizontal_p, &label_width, &rest_width, &height);
515 if (horizontal_p)
516 width = label_width + rest_width;
517 else
519 label_width = ws->label_width;
520 width = ws->width - 2 * shadow;
523 #if 0
524 /* see if it should be a button in the menubar */
525 button_p = horizontal_p && val->call_data;
526 #endif
527 button_p = 0;
529 /* Only highlight an enabled item that has a callback. */
530 if (highlighted_p)
531 if (!val->enabled || !(val->call_data || val->contents))
532 highlighted_p = 0;
534 /* do the drawing. */
535 if (!just_compute_p)
537 /* Add the shadow border of the containing menu */
538 int x = where->x + shadow;
539 int y = where->y + shadow;
541 /* pick the foreground and background GC. */
542 if (val->enabled)
543 text_gc = button_p ? mw->menu.button_gc : mw->menu.foreground_gc;
544 else
545 text_gc =
546 button_p ? mw->menu.inactive_button_gc : mw->menu.inactive_gc;
547 deco_gc = mw->menu.foreground_gc;
549 if (separator_p)
551 XDrawLine (XtDisplay (mw), ws->window, mw->menu.shadow_bottom_gc,
552 x, y, x + width, y);
553 XDrawLine (XtDisplay (mw), ws->window, mw->menu.shadow_top_gc,
554 x, y + 1, x + width, y + 1);
556 else
558 int x_offset = x + h_spacing + shadow;
559 char* display_string = resource_widget_value (mw, val);
560 draw_shadow_rectangle (mw, ws->window, x, y, width, height, True);
562 /* Deal with centering a menu title. */
563 if (!horizontal_p && !val->contents && !val->call_data)
565 int l = string_width (mw, display_string);
567 if (width > l)
568 x_offset = (width - l) >> 1;
570 XDrawString (XtDisplay (mw), ws->window, text_gc, x_offset,
571 y + v_spacing + shadow + font_ascent,
572 display_string, strlen (display_string));
574 if (!horizontal_p)
576 if (val->contents)
578 int a_w = arrow_width (mw);
579 draw_arrow (mw, ws->window, deco_gc,
580 x + width - arrow_width (mw)
581 - mw->menu.horizontal_spacing
582 - mw->menu.shadow_thickness,
583 y + v_spacing + shadow, a_w);
585 else if (val->key)
587 XDrawString (XtDisplay (mw), ws->window, text_gc,
588 x + label_width + mw->menu.arrow_spacing,
589 y + v_spacing + shadow + font_ascent,
590 val->key, strlen (val->key));
594 else if (button_p)
596 #if 1
597 XDrawRectangle (XtDisplay (mw), ws->window, deco_gc,
598 x + shadow, y + shadow,
599 label_width + h_spacing - 1,
600 font_ascent + font_descent + 2 * v_spacing - 1);
601 draw_shadow_rectangle (mw, ws->window, x, y, width, height,
602 False);
603 #else
604 highlighted_p = True;
605 #endif
607 else
609 XDrawRectangle (XtDisplay (mw), ws->window,
610 mw->menu.background_gc,
611 x + shadow, y + shadow,
612 label_width + h_spacing - 1,
613 font_ascent + font_descent + 2 * v_spacing - 1);
614 draw_shadow_rectangle (mw, ws->window, x, y, width, height,
615 True);
618 if (highlighted_p)
619 draw_shadow_rectangle (mw, ws->window, x, y, width, height, False);
623 where->x += width;
624 where->y += height;
627 static void
628 display_menu (mw, level, just_compute_p, highlighted_pos, hit, hit_return,
629 this, that)
630 XlwMenuWidget mw;
631 int level;
632 Boolean just_compute_p;
633 XPoint* highlighted_pos;
634 XPoint* hit;
635 widget_value** hit_return;
636 widget_value* this;
637 widget_value* that;
639 widget_value* val;
640 widget_value* following_item;
641 window_state* ws;
642 XPoint where;
643 int horizontal_p = mw->menu.horizontal && (level == 0);
644 int highlighted_p;
645 int just_compute_this_one_p;
647 if (level >= mw->menu.old_depth)
648 abort ();
650 if (level < mw->menu.old_depth - 1)
651 following_item = mw->menu.old_stack [level + 1];
652 else
653 following_item = NULL;
655 if (hit)
656 *hit_return = NULL;
658 where.x = 0;
659 where.y = 0;
661 ws = &mw->menu.windows [level];
662 for (val = mw->menu.old_stack [level]->contents; val; val = val->next)
664 highlighted_p = val == following_item;
665 if (highlighted_p && highlighted_pos)
667 if (horizontal_p)
668 highlighted_pos->x = where.x;
669 else
670 highlighted_pos->y = where.y;
673 just_compute_this_one_p =
674 just_compute_p || ((this || that) && val != this && val != that);
676 display_menu_item (mw, val, ws, &where, highlighted_p, horizontal_p,
677 just_compute_this_one_p);
679 if (highlighted_p && highlighted_pos)
681 if (horizontal_p)
682 highlighted_pos->y = where.y;
683 else
684 highlighted_pos->x = where.x;
687 if (hit
688 && !*hit_return
689 && (horizontal_p ? hit->x < where.x : hit->y < where.y)
690 && !all_dashes_p (val->name))
691 *hit_return = val;
693 if (horizontal_p)
694 where.y = 0;
695 else
696 where.x = 0;
699 if (!just_compute_p)
700 draw_shadow_rectangle (mw, ws->window, 0, 0, ws->width, ws->height, False);
703 \f/* Motion code */
704 static void
705 set_new_state (mw, val, level)
706 XlwMenuWidget mw;
707 widget_value* val;
708 int level;
710 int i;
712 mw->menu.new_depth = 0;
713 for (i = 0; i < level; i++)
714 push_new_stack (mw, mw->menu.old_stack [i]);
715 push_new_stack (mw, val);
718 static void
719 make_windows_if_needed (mw, n)
720 XlwMenuWidget mw;
721 int n;
723 int i;
724 int start_at;
725 XSetWindowAttributes xswa;
726 int mask;
727 Window root = RootWindowOfScreen (DefaultScreenOfDisplay (XtDisplay (mw)));
728 window_state* windows;
730 if (mw->menu.windows_length >= n)
731 return;
733 xswa.save_under = True;
734 xswa.override_redirect = True;
735 xswa.background_pixel = mw->core.background_pixel;
736 xswa.border_pixel = mw->core.border_pixel;
737 xswa.event_mask =
738 ExposureMask | PointerMotionMask | PointerMotionHintMask
739 | ButtonReleaseMask | ButtonPressMask;
740 xswa.cursor = mw->menu.cursor_shape;
741 mask = CWSaveUnder | CWOverrideRedirect | CWBackPixel | CWBorderPixel
742 | CWEventMask | CWCursor;
744 if (!mw->menu.windows)
746 mw->menu.windows =
747 (window_state*)XtMalloc (n * sizeof (window_state));
748 start_at = 0;
750 else
752 mw->menu.windows =
753 (window_state*)XtRealloc ((char*)mw->menu.windows,
754 n * sizeof (window_state));
755 start_at = mw->menu.windows_length;
757 mw->menu.windows_length = n;
759 windows = mw->menu.windows;
761 for (i = start_at; i < n; i++)
763 windows [i].x = 0;
764 windows [i].y = 0;
765 windows [i].width = 1;
766 windows [i].height = 1;
767 windows [i].window =
768 XCreateWindow (XtDisplay (mw), root, 0, 0, 1, 1,
769 0, 0, CopyFromParent, CopyFromParent, mask, &xswa);
773 /* Make the window fit in the screen */
774 static void
775 fit_to_screen (mw, ws, previous_ws, horizontal_p)
776 XlwMenuWidget mw;
777 window_state* ws;
778 window_state* previous_ws;
779 Boolean horizontal_p;
781 unsigned int screen_width = WidthOfScreen (XtScreen (mw));
782 unsigned int screen_height = HeightOfScreen (XtScreen (mw));
784 if (ws->x < 0)
785 ws->x = 0;
786 else if (ws->x + ws->width > screen_width)
788 if (!horizontal_p)
789 ws->x = previous_ws->x - ws->width;
790 else
791 ws->x = screen_width - ws->width;
792 if (ws->x < 0)
793 ws->x = 0;
795 if (ws->y < 0)
796 ws->y = 0;
797 else if (ws->y + ws->height > screen_height)
799 if (horizontal_p)
800 ws->y = previous_ws->y - ws->height;
801 else
802 ws->y = screen_height - ws->height;
803 if (ws->y < 0)
804 ws->y = 0;
808 /* Updates old_stack from new_stack and redisplays. */
809 static void
810 remap_menubar (mw)
811 XlwMenuWidget mw;
813 int i;
814 int last_same;
815 XPoint selection_position;
816 int old_depth = mw->menu.old_depth;
817 int new_depth = mw->menu.new_depth;
818 widget_value** old_stack;
819 widget_value** new_stack;
820 window_state* windows;
821 widget_value* old_selection;
822 widget_value* new_selection;
824 /* Check that enough windows and old_stack are ready. */
825 make_windows_if_needed (mw, new_depth);
826 make_old_stack_space (mw, new_depth);
827 windows = mw->menu.windows;
828 old_stack = mw->menu.old_stack;
829 new_stack = mw->menu.new_stack;
831 /* compute the last identical different entry */
832 for (i = 1; i < old_depth && i < new_depth; i++)
833 if (old_stack [i] != new_stack [i])
834 break;
835 last_same = i - 1;
837 /* Memorize the previously selected item to be able to refresh it */
838 old_selection = last_same + 1 < old_depth ? old_stack [last_same + 1] : NULL;
839 if (old_selection && !old_selection->enabled)
840 old_selection = NULL;
841 new_selection = last_same + 1 < new_depth ? new_stack [last_same + 1] : NULL;
842 if (new_selection && !new_selection->enabled)
843 new_selection = NULL;
845 /* updates old_state from new_state. It has to be done now because
846 display_menu (called below) uses the old_stack to know what to display. */
847 for (i = last_same + 1; i < new_depth; i++)
848 old_stack [i] = new_stack [i];
849 mw->menu.old_depth = new_depth;
851 /* refresh the last selection */
852 selection_position.x = 0;
853 selection_position.y = 0;
854 display_menu (mw, last_same, new_selection == old_selection,
855 &selection_position, NULL, NULL, old_selection, new_selection);
857 /* Now popup the new menus */
858 for (i = last_same + 1; i < new_depth && new_stack [i]->contents; i++)
860 window_state* previous_ws = &windows [i - 1];
861 window_state* ws = &windows [i];
863 ws->x =
864 previous_ws->x + selection_position.x + mw->menu.shadow_thickness;
865 if (!mw->menu.horizontal || i > 1)
866 ws->x += mw->menu.shadow_thickness;
867 ws->y =
868 previous_ws->y + selection_position.y + mw->menu.shadow_thickness;
870 size_menu (mw, i);
872 fit_to_screen (mw, ws, previous_ws, mw->menu.horizontal && i == 1);
874 XClearWindow (XtDisplay (mw), ws->window);
875 XMoveResizeWindow (XtDisplay (mw), ws->window, ws->x, ws->y,
876 ws->width, ws->height);
877 XMapRaised (XtDisplay (mw), ws->window);
878 display_menu (mw, i, False, &selection_position, NULL, NULL, NULL, NULL);
881 /* unmap the menus that popped down */
882 for (i = new_depth - 1; i < old_depth; i++)
883 if (i >= new_depth || !new_stack [i]->contents)
884 XUnmapWindow (XtDisplay (mw), windows [i].window);
887 static Boolean
888 motion_event_is_in_menu (mw, ev, level, relative_pos)
889 XlwMenuWidget mw;
890 XMotionEvent* ev;
891 int level;
892 XPoint* relative_pos;
894 window_state* ws = &mw->menu.windows [level];
895 int x = level == 0 ? ws->x : ws->x + mw->menu.shadow_thickness;
896 int y = level == 0 ? ws->y : ws->y + mw->menu.shadow_thickness;
897 relative_pos->x = ev->x_root - x;
898 relative_pos->y = ev->y_root - y;
899 return (x < ev->x_root && ev->x_root < x + ws->width
900 && y < ev->y_root && ev->y_root < y + ws->height);
903 static Boolean
904 map_event_to_widget_value (mw, ev, val, level)
905 XlwMenuWidget mw;
906 XMotionEvent* ev;
907 widget_value** val;
908 int* level;
910 int i;
911 XPoint relative_pos;
912 window_state* ws;
914 *val = NULL;
916 /* Find the window */
917 for (i = mw->menu.old_depth - 1; i >= 0; i--)
919 ws = &mw->menu.windows [i];
920 if (ws && motion_event_is_in_menu (mw, ev, i, &relative_pos))
922 display_menu (mw, i, True, NULL, &relative_pos, val, NULL, NULL);
924 if (*val)
926 *level = i + 1;
927 return True;
931 return False;
934 \f/* Procedures */
935 static void
936 make_drawing_gcs (mw)
937 XlwMenuWidget mw;
939 XGCValues xgcv;
941 xgcv.font = mw->menu.font->fid;
942 xgcv.foreground = mw->menu.foreground;
943 xgcv.background = mw->core.background_pixel;
944 mw->menu.foreground_gc = XtGetGC ((Widget)mw,
945 GCFont | GCForeground | GCBackground,
946 &xgcv);
948 xgcv.font = mw->menu.font->fid;
949 xgcv.foreground = mw->menu.button_foreground;
950 xgcv.background = mw->core.background_pixel;
951 mw->menu.button_gc = XtGetGC ((Widget)mw,
952 GCFont | GCForeground | GCBackground,
953 &xgcv);
955 xgcv.font = mw->menu.font->fid;
956 xgcv.foreground = mw->menu.foreground;
957 xgcv.background = mw->core.background_pixel;
958 xgcv.fill_style = FillStippled;
959 xgcv.stipple = mw->menu.gray_pixmap;
960 mw->menu.inactive_gc = XtGetGC ((Widget)mw,
961 (GCFont | GCForeground | GCBackground
962 | GCFillStyle | GCStipple), &xgcv);
964 xgcv.font = mw->menu.font->fid;
965 xgcv.foreground = mw->menu.button_foreground;
966 xgcv.background = mw->core.background_pixel;
967 xgcv.fill_style = FillStippled;
968 xgcv.stipple = mw->menu.gray_pixmap;
969 mw->menu.inactive_button_gc = XtGetGC ((Widget)mw,
970 (GCFont | GCForeground | GCBackground
971 | GCFillStyle | GCStipple), &xgcv);
973 xgcv.font = mw->menu.font->fid;
974 xgcv.foreground = mw->core.background_pixel;
975 xgcv.background = mw->menu.foreground;
976 mw->menu.background_gc = XtGetGC ((Widget)mw,
977 GCFont | GCForeground | GCBackground,
978 &xgcv);
981 static void
982 release_drawing_gcs (mw)
983 XlwMenuWidget mw;
985 XtReleaseGC ((Widget) mw, mw->menu.foreground_gc);
986 XtReleaseGC ((Widget) mw, mw->menu.button_gc);
987 XtReleaseGC ((Widget) mw, mw->menu.inactive_gc);
988 XtReleaseGC ((Widget) mw, mw->menu.inactive_button_gc);
989 XtReleaseGC ((Widget) mw, mw->menu.background_gc);
990 /* let's get some segvs if we try to use these... */
991 mw->menu.foreground_gc = (GC) -1;
992 mw->menu.button_gc = (GC) -1;
993 mw->menu.inactive_gc = (GC) -1;
994 mw->menu.inactive_button_gc = (GC) -1;
995 mw->menu.background_gc = (GC) -1;
998 #define MINL(x,y) ((((unsigned long) (x)) < ((unsigned long) (y))) \
999 ? ((unsigned long) (x)) : ((unsigned long) (y)))
1001 static void
1002 make_shadow_gcs (mw)
1003 XlwMenuWidget mw;
1005 XGCValues xgcv;
1006 unsigned long pm = 0;
1007 Display *dpy = XtDisplay ((Widget) mw);
1008 Colormap cmap = DefaultColormapOfScreen (XtScreen ((Widget) mw));
1009 XColor topc, botc;
1010 int top_frobbed = 0, bottom_frobbed = 0;
1012 if (mw->menu.top_shadow_color == -1)
1013 mw->menu.top_shadow_color = mw->core.background_pixel;
1014 if (mw->menu.bottom_shadow_color == -1)
1015 mw->menu.bottom_shadow_color = mw->menu.foreground;
1017 if (mw->menu.top_shadow_color == mw->core.background_pixel ||
1018 mw->menu.top_shadow_color == mw->menu.foreground)
1020 topc.pixel = mw->core.background_pixel;
1021 XQueryColor (dpy, cmap, &topc);
1022 /* don't overflow/wrap! */
1023 topc.red = MINL (65535, topc.red * 1.2);
1024 topc.green = MINL (65535, topc.green * 1.2);
1025 topc.blue = MINL (65535, topc.blue * 1.2);
1026 if (XAllocColor (dpy, cmap, &topc))
1028 mw->menu.top_shadow_color = topc.pixel;
1029 top_frobbed = 1;
1032 if (mw->menu.bottom_shadow_color == mw->menu.foreground ||
1033 mw->menu.bottom_shadow_color == mw->core.background_pixel)
1035 botc.pixel = mw->core.background_pixel;
1036 XQueryColor (dpy, cmap, &botc);
1037 botc.red *= 0.6;
1038 botc.green *= 0.6;
1039 botc.blue *= 0.6;
1040 if (XAllocColor (dpy, cmap, &botc))
1042 mw->menu.bottom_shadow_color = botc.pixel;
1043 bottom_frobbed = 1;
1047 if (top_frobbed && bottom_frobbed)
1049 int top_avg = ((topc.red / 3) + (topc.green / 3) + (topc.blue / 3));
1050 int bot_avg = ((botc.red / 3) + (botc.green / 3) + (botc.blue / 3));
1051 if (bot_avg > top_avg)
1053 Pixel tmp = mw->menu.top_shadow_color;
1054 mw->menu.top_shadow_color = mw->menu.bottom_shadow_color;
1055 mw->menu.bottom_shadow_color = tmp;
1057 else if (topc.pixel == botc.pixel)
1059 if (botc.pixel == mw->menu.foreground)
1060 mw->menu.top_shadow_color = mw->core.background_pixel;
1061 else
1062 mw->menu.bottom_shadow_color = mw->menu.foreground;
1066 if (!mw->menu.top_shadow_pixmap &&
1067 mw->menu.top_shadow_color == mw->core.background_pixel)
1069 mw->menu.top_shadow_pixmap = mw->menu.gray_pixmap;
1070 mw->menu.top_shadow_color = mw->menu.foreground;
1072 if (!mw->menu.bottom_shadow_pixmap &&
1073 mw->menu.bottom_shadow_color == mw->core.background_pixel)
1075 mw->menu.bottom_shadow_pixmap = mw->menu.gray_pixmap;
1076 mw->menu.bottom_shadow_color = mw->menu.foreground;
1079 xgcv.fill_style = FillStippled;
1080 xgcv.foreground = mw->menu.top_shadow_color;
1081 xgcv.stipple = mw->menu.top_shadow_pixmap;
1082 pm = (xgcv.stipple ? GCStipple|GCFillStyle : 0);
1083 mw->menu.shadow_top_gc = XtGetGC ((Widget)mw, GCForeground | pm, &xgcv);
1085 xgcv.foreground = mw->menu.bottom_shadow_color;
1086 xgcv.stipple = mw->menu.bottom_shadow_pixmap;
1087 pm = (xgcv.stipple ? GCStipple|GCFillStyle : 0);
1088 mw->menu.shadow_bottom_gc = XtGetGC ((Widget)mw, GCForeground | pm, &xgcv);
1092 static void
1093 release_shadow_gcs (mw)
1094 XlwMenuWidget mw;
1096 XtReleaseGC ((Widget) mw, mw->menu.shadow_top_gc);
1097 XtReleaseGC ((Widget) mw, mw->menu.shadow_bottom_gc);
1100 static void
1101 XlwMenuInitialize (request, mw, args, num_args)
1102 Widget request;
1103 XlwMenuWidget mw;
1104 ArgList args;
1105 Cardinal *num_args;
1107 /* Get the GCs and the widget size */
1108 XSetWindowAttributes xswa;
1109 int mask;
1111 Window window = RootWindowOfScreen (DefaultScreenOfDisplay (XtDisplay (mw)));
1112 Display* display = XtDisplay (mw);
1114 #if 0
1115 widget_value *tem = (widget_value *) XtMalloc (sizeof (widget_value));
1117 /* _XtCreate is freeing the object that was passed to us,
1118 so make a copy that we will actually keep. */
1119 lwlib_bcopy (mw->menu.contents, tem, sizeof (widget_value));
1120 mw->menu.contents = tem;
1121 #endif
1123 /* mw->menu.cursor = XCreateFontCursor (display, mw->menu.cursor_shape); */
1124 mw->menu.cursor = mw->menu.cursor_shape;
1126 mw->menu.gray_pixmap
1127 = XCreatePixmapFromBitmapData (display, window, gray_bits,
1128 gray_width, gray_height,
1129 (unsigned long)1, (unsigned long)0, 1);
1131 /* I don't understand why this ends up 0 sometimes,
1132 but it does. This kludge works around it.
1133 Can anyone find a real fix? -- rms. */
1134 if (mw->menu.font == 0)
1135 mw->menu.font = xlwmenu_default_font;
1137 make_drawing_gcs (mw);
1138 make_shadow_gcs (mw);
1140 xswa.background_pixel = mw->core.background_pixel;
1141 xswa.border_pixel = mw->core.border_pixel;
1142 mask = CWBackPixel | CWBorderPixel;
1144 mw->menu.popped_up = False;
1146 mw->menu.old_depth = 1;
1147 mw->menu.old_stack = (widget_value**)XtMalloc (sizeof (widget_value*));
1148 mw->menu.old_stack_length = 1;
1149 mw->menu.old_stack [0] = mw->menu.contents;
1151 mw->menu.new_depth = 0;
1152 mw->menu.new_stack = 0;
1153 mw->menu.new_stack_length = 0;
1154 push_new_stack (mw, mw->menu.contents);
1156 mw->menu.windows = (window_state*)XtMalloc (sizeof (window_state));
1157 mw->menu.windows_length = 1;
1158 mw->menu.windows [0].x = 0;
1159 mw->menu.windows [0].y = 0;
1160 mw->menu.windows [0].width = 0;
1161 mw->menu.windows [0].height = 0;
1162 size_menu (mw, 0);
1164 mw->core.width = mw->menu.windows [0].width;
1165 mw->core.height = mw->menu.windows [0].height;
1168 static void
1169 XlwMenuClassInitialize ()
1173 static void
1174 XlwMenuRealize (w, valueMask, attributes)
1175 Widget w;
1176 Mask *valueMask;
1177 XSetWindowAttributes *attributes;
1179 XlwMenuWidget mw = (XlwMenuWidget)w;
1180 XSetWindowAttributes xswa;
1181 int mask;
1183 (*xlwMenuWidgetClass->core_class.superclass->core_class.realize)
1184 (w, valueMask, attributes);
1186 xswa.save_under = True;
1187 xswa.cursor = mw->menu.cursor_shape;
1188 mask = CWSaveUnder | CWCursor;
1189 XChangeWindowAttributes (XtDisplay (w), XtWindow (w), mask, &xswa);
1191 mw->menu.windows [0].window = XtWindow (w);
1192 mw->menu.windows [0].x = w->core.x;
1193 mw->menu.windows [0].y = w->core.y;
1194 mw->menu.windows [0].width = w->core.width;
1195 mw->menu.windows [0].height = w->core.height;
1198 /* Only the toplevel menubar/popup is a widget so it's the only one that
1199 receives expose events through Xt. So we repaint all the other panes
1200 when receiving an Expose event. */
1201 static void
1202 XlwMenuRedisplay (w, ev, region)
1203 Widget w;
1204 XEvent* ev;
1205 Region region;
1207 XlwMenuWidget mw = (XlwMenuWidget)w;
1208 int i;
1210 /* If we have a depth beyond 1, it's because a submenu was displayed.
1211 If the submenu has been destroyed, set the depth back to 1. */
1212 if (submenu_destroyed)
1214 mw->menu.old_depth = 1;
1215 submenu_destroyed = 0;
1218 for (i = 0; i < mw->menu.old_depth; i++)
1219 display_menu (mw, i, False, NULL, NULL, NULL, NULL, NULL);
1222 static void
1223 XlwMenuDestroy (w)
1224 Widget w;
1226 int i;
1227 XlwMenuWidget mw = (XlwMenuWidget) w;
1229 if (pointer_grabbed)
1230 XtUngrabPointer ((Widget)w, CurrentTime);
1231 pointer_grabbed = 0;
1233 submenu_destroyed = 1;
1235 release_drawing_gcs (mw);
1236 release_shadow_gcs (mw);
1238 /* this doesn't come from the resource db but is created explicitly
1239 so we must free it ourselves. */
1240 XFreePixmap (XtDisplay (mw), mw->menu.gray_pixmap);
1241 mw->menu.gray_pixmap = (Pixmap) -1;
1243 #if 0
1244 /* Do free mw->menu.contents because nowadays we copy it
1245 during initialization. */
1246 XtFree (mw->menu.contents);
1247 #endif
1249 /* Don't free mw->menu.contents because that comes from our creator.
1250 The `*_stack' elements are just pointers into `contents' so leave
1251 that alone too. But free the stacks themselves. */
1252 if (mw->menu.old_stack) XtFree ((char *) mw->menu.old_stack);
1253 if (mw->menu.new_stack) XtFree ((char *) mw->menu.new_stack);
1255 /* Remember, you can't free anything that came from the resource
1256 database. This includes:
1257 mw->menu.cursor
1258 mw->menu.top_shadow_pixmap
1259 mw->menu.bottom_shadow_pixmap
1260 mw->menu.font
1261 Also the color cells of top_shadow_color, bottom_shadow_color,
1262 foreground, and button_foreground will never be freed until this
1263 client exits. Nice, eh?
1266 /* start from 1 because the one in slot 0 is w->core.window */
1267 for (i = 1; i < mw->menu.windows_length; i++)
1268 XDestroyWindow (XtDisplay (mw), mw->menu.windows [i].window);
1269 if (mw->menu.windows)
1270 XtFree ((char *) mw->menu.windows);
1273 static Boolean
1274 XlwMenuSetValues (current, request, new)
1275 Widget current;
1276 Widget request;
1277 Widget new;
1279 XlwMenuWidget oldmw = (XlwMenuWidget)current;
1280 XlwMenuWidget newmw = (XlwMenuWidget)new;
1281 Boolean redisplay = False;
1282 int i;
1284 if (newmw->menu.contents
1285 && newmw->menu.contents->contents
1286 && newmw->menu.contents->contents->change >= VISIBLE_CHANGE)
1287 redisplay = True;
1288 /* Do redisplay if the contents are entirely eliminated. */
1289 if (newmw->menu.contents
1290 && newmw->menu.contents->contents == 0
1291 && newmw->menu.contents->change >= VISIBLE_CHANGE)
1292 redisplay = True;
1294 if (newmw->core.background_pixel != oldmw->core.background_pixel
1295 || newmw->menu.foreground != oldmw->menu.foreground
1296 || newmw->menu.font != oldmw->menu.font)
1298 release_drawing_gcs (newmw);
1299 make_drawing_gcs (newmw);
1300 redisplay = True;
1302 for (i = 0; i < oldmw->menu.windows_length; i++)
1304 XSetWindowBackground (XtDisplay (oldmw),
1305 oldmw->menu.windows [i].window,
1306 newmw->core.background_pixel);
1307 /* clear windows and generate expose events */
1308 XClearArea (XtDisplay (oldmw), oldmw->menu.windows[i].window,
1309 0, 0, 0, 0, True);
1313 return redisplay;
1316 static void
1317 XlwMenuResize (w)
1318 Widget w;
1320 XlwMenuWidget mw = (XlwMenuWidget)w;
1322 if (mw->menu.popped_up)
1324 /* Don't allow the popup menu to resize itself. */
1325 mw->core.width = mw->menu.windows [0].width;
1326 mw->core.height = mw->menu.windows [0].height;
1327 mw->core.parent->core.width = mw->core.width ;
1328 mw->core.parent->core.height = mw->core.height ;
1330 else
1332 mw->menu.windows [0].width = mw->core.width;
1333 mw->menu.windows [0].height = mw->core.height;
1337 \f/* Action procedures */
1338 static void
1339 handle_single_motion_event (mw, ev)
1340 XlwMenuWidget mw;
1341 XMotionEvent* ev;
1343 widget_value* val;
1344 int level;
1346 if (!map_event_to_widget_value (mw, ev, &val, &level))
1347 pop_new_stack_if_no_contents (mw);
1348 else
1349 set_new_state (mw, val, level);
1350 remap_menubar (mw);
1352 /* Sync with the display. Makes it feel better on X terms. */
1353 XSync (XtDisplay (mw), False);
1356 static void
1357 handle_motion_event (mw, ev)
1358 XlwMenuWidget mw;
1359 XMotionEvent* ev;
1361 int x = ev->x_root;
1362 int y = ev->y_root;
1363 int state = ev->state;
1365 handle_single_motion_event (mw, ev);
1367 /* allow motion events to be generated again */
1368 if (ev->is_hint
1369 && XQueryPointer (XtDisplay (mw), ev->window,
1370 &ev->root, &ev->subwindow,
1371 &ev->x_root, &ev->y_root,
1372 &ev->x, &ev->y,
1373 &ev->state)
1374 && ev->state == state
1375 && (ev->x_root != x || ev->y_root != y))
1376 handle_single_motion_event (mw, ev);
1379 static void
1380 Start (w, ev, params, num_params)
1381 Widget w;
1382 XEvent *ev;
1383 String *params;
1384 Cardinal *num_params;
1386 XlwMenuWidget mw = (XlwMenuWidget)w;
1388 if (!mw->menu.popped_up)
1390 menu_post_event = *ev;
1391 pop_up_menu (mw, ev);
1393 else
1395 /* If we push a button while the menu is posted semipermanently,
1396 releasing the button should always pop the menu down. */
1397 next_release_must_exit = 1;
1399 /* notes the absolute position of the menubar window */
1400 mw->menu.windows [0].x = ev->xmotion.x_root - ev->xmotion.x;
1401 mw->menu.windows [0].y = ev->xmotion.y_root - ev->xmotion.y;
1403 /* handles the down like a move, slots are compatible */
1404 handle_motion_event (mw, &ev->xmotion);
1408 static void
1409 Drag (w, ev, params, num_params)
1410 Widget w;
1411 XEvent *ev;
1412 String *params;
1413 Cardinal *num_params;
1415 XlwMenuWidget mw = (XlwMenuWidget)w;
1416 if (mw->menu.popped_up)
1417 handle_motion_event (mw, &ev->xmotion);
1420 /* Do nothing.
1421 This is how we handle presses and releases of modifier keys. */
1422 static void
1423 Nothing (w, ev, params, num_params)
1424 Widget w;
1425 XEvent *ev;
1426 String *params;
1427 Cardinal *num_params;
1431 /* Handle key press and release events while menu is popped up.
1432 Our action is to get rid of the menu. */
1433 static void
1434 Key (w, ev, params, num_params)
1435 Widget w;
1436 XEvent *ev;
1437 String *params;
1438 Cardinal *num_params;
1440 XlwMenuWidget mw = (XlwMenuWidget)w;
1442 /* Pop down everything. */
1443 mw->menu.new_depth = 1;
1444 remap_menubar (mw);
1446 if (mw->menu.popped_up)
1448 mw->menu.popped_up = False;
1449 XtUngrabPointer ((Widget)mw, ev->xmotion.time);
1450 if (XtIsShell (XtParent ((Widget) mw)))
1451 XtPopdown (XtParent ((Widget) mw));
1452 else
1454 XtRemoveGrab ((Widget) mw);
1455 display_menu (mw, 0, False, NULL, NULL, NULL, NULL, NULL);
1459 /* callback */
1460 XtCallCallbackList ((Widget)mw, mw->menu.select, (XtPointer)0);
1463 static void
1464 Select (w, ev, params, num_params)
1465 Widget w;
1466 XEvent *ev;
1467 String *params;
1468 Cardinal *num_params;
1470 XlwMenuWidget mw = (XlwMenuWidget)w;
1471 widget_value* selected_item = mw->menu.old_stack [mw->menu.old_depth - 1];
1473 /* If user releases the button quickly, without selecting anything,
1474 after the initial down-click that brought the menu up,
1475 do nothing. */
1476 if ((selected_item == 0
1477 || ((widget_value *) selected_item)->call_data == 0)
1478 && !next_release_must_exit
1479 && (ev->xbutton.time - menu_post_event.xbutton.time
1480 < XtGetMultiClickTime (XtDisplay (w))))
1481 return;
1483 /* pop down everything. */
1484 mw->menu.new_depth = 1;
1485 remap_menubar (mw);
1487 if (mw->menu.popped_up)
1489 mw->menu.popped_up = False;
1490 XtUngrabPointer ((Widget)mw, ev->xmotion.time);
1491 if (XtIsShell (XtParent ((Widget) mw)))
1492 XtPopdown (XtParent ((Widget) mw));
1493 else
1495 XtRemoveGrab ((Widget) mw);
1496 display_menu (mw, 0, False, NULL, NULL, NULL, NULL, NULL);
1500 /* callback */
1501 XtCallCallbackList ((Widget)mw, mw->menu.select, (XtPointer)selected_item);
1505 \f/* Special code to pop-up a menu */
1506 void
1507 pop_up_menu (mw, event)
1508 XlwMenuWidget mw;
1509 XButtonPressedEvent* event;
1511 int x = event->x_root;
1512 int y = event->y_root;
1513 int w;
1514 int h;
1515 int borderwidth = mw->menu.shadow_thickness;
1516 Screen* screen = XtScreen (mw);
1517 Display *display = XtDisplay (mw);
1519 next_release_must_exit = 0;
1521 XtCallCallbackList ((Widget)mw, mw->menu.open, NULL);
1523 if (XtIsShell (XtParent ((Widget)mw)))
1524 size_menu (mw, 0);
1526 w = mw->menu.windows [0].width;
1527 h = mw->menu.windows [0].height;
1529 x -= borderwidth;
1530 y -= borderwidth;
1531 if (x < borderwidth)
1532 x = borderwidth;
1533 if (x + w + 2 * borderwidth > WidthOfScreen (screen))
1534 x = WidthOfScreen (screen) - w - 2 * borderwidth;
1535 if (y < borderwidth)
1536 y = borderwidth;
1537 if (y + h + 2 * borderwidth> HeightOfScreen (screen))
1538 y = HeightOfScreen (screen) - h - 2 * borderwidth;
1540 mw->menu.popped_up = True;
1541 if (XtIsShell (XtParent ((Widget)mw)))
1543 XtConfigureWidget (XtParent ((Widget)mw), x, y, w, h,
1544 XtParent ((Widget)mw)->core.border_width);
1545 XtPopup (XtParent ((Widget)mw), XtGrabExclusive);
1546 display_menu (mw, 0, False, NULL, NULL, NULL, NULL, NULL);
1547 mw->menu.windows [0].x = x + borderwidth;
1548 mw->menu.windows [0].y = y + borderwidth;
1550 else
1552 XEvent *ev = (XEvent *) event;
1554 XtAddGrab ((Widget) mw, True, True);
1556 /* notes the absolute position of the menubar window */
1557 mw->menu.windows [0].x = ev->xmotion.x_root - ev->xmotion.x;
1558 mw->menu.windows [0].y = ev->xmotion.y_root - ev->xmotion.y;
1561 #ifdef emacs
1562 x_catch_errors (display);
1563 #endif
1564 XtGrabPointer ((Widget)mw, False,
1565 (PointerMotionMask
1566 | PointerMotionHintMask
1567 | ButtonReleaseMask
1568 | ButtonPressMask),
1569 GrabModeAsync, GrabModeAsync, None,
1570 mw->menu.cursor_shape,
1571 event->time);
1572 pointer_grabbed = 1;
1573 #ifdef emacs
1574 if (x_had_errors_p (display))
1576 pointer_grabbed = 0;
1577 XtUngrabPointer ((Widget)mw, event->time);
1579 x_uncatch_errors (display);
1580 #endif
1582 handle_motion_event (mw, (XMotionEvent*)event);