(add-log-tcl-defun): Don't use tcl-beginning-of-defun; just go to end
[emacs.git] / lwlib / xlwmenu.c
blob2f5066130d93ffbea7cd2e698cf12eb39fb566d0
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\
45 #define offset(field) XtOffset(XlwMenuWidget, field)
46 static XtResource
47 xlwMenuResources[] =
49 {XtNfont, XtCFont, XtRFontStruct, sizeof(XFontStruct *),
50 offset(menu.font),XtRString, "XtDefaultFont"},
51 {XtNforeground, XtCForeground, XtRPixel, sizeof(Pixel),
52 offset(menu.foreground), XtRString, "XtDefaultForeground"},
53 {XtNbuttonForeground, XtCButtonForeground, XtRPixel, sizeof(Pixel),
54 offset(menu.button_foreground), XtRString, "XtDefaultForeground"},
55 {XtNmargin, XtCMargin, XtRDimension, sizeof(Dimension),
56 offset(menu.margin), XtRImmediate, (XtPointer)0},
57 {XtNhorizontalSpacing, XtCMargin, XtRDimension, sizeof(Dimension),
58 offset(menu.horizontal_spacing), XtRImmediate, (XtPointer)3},
59 {XtNverticalSpacing, XtCMargin, XtRDimension, sizeof(Dimension),
60 offset(menu.vertical_spacing), XtRImmediate, (XtPointer)1},
61 {XtNarrowSpacing, XtCMargin, XtRDimension, sizeof(Dimension),
62 offset(menu.arrow_spacing), XtRImmediate, (XtPointer)10},
64 {XmNshadowThickness, XmCShadowThickness, XtRDimension,
65 sizeof (Dimension), offset (menu.shadow_thickness),
66 XtRImmediate, (XtPointer) 2},
67 {XmNtopShadowColor, XmCTopShadowColor, XtRPixel, sizeof (Pixel),
68 offset (menu.top_shadow_color), XtRImmediate, (XtPointer)-1},
69 {XmNbottomShadowColor, XmCBottomShadowColor, XtRPixel, sizeof (Pixel),
70 offset (menu.bottom_shadow_color), XtRImmediate, (XtPointer)-1},
71 {XmNtopShadowPixmap, XmCTopShadowPixmap, XtRPixmap, sizeof (Pixmap),
72 offset (menu.top_shadow_pixmap), XtRImmediate, (XtPointer)None},
73 {XmNbottomShadowPixmap, XmCBottomShadowPixmap, XtRPixmap, sizeof (Pixmap),
74 offset (menu.bottom_shadow_pixmap), XtRImmediate, (XtPointer)None},
76 {XtNopen, XtCCallback, XtRCallback, sizeof(XtPointer),
77 offset(menu.open), XtRCallback, (XtPointer)NULL},
78 {XtNselect, XtCCallback, XtRCallback, sizeof(XtPointer),
79 offset(menu.select), XtRCallback, (XtPointer)NULL},
80 {XtNmenu, XtCMenu, XtRPointer, sizeof(XtPointer),
81 offset(menu.contents), XtRImmediate, (XtPointer)NULL},
82 {XtNcursor, XtCCursor, XtRCursor, sizeof(Cursor),
83 offset(menu.cursor_shape), XtRString, (XtPointer)"right_ptr"},
84 {XtNhorizontal, XtCHorizontal, XtRInt, sizeof(int),
85 offset(menu.horizontal), XtRImmediate, (XtPointer)True},
87 #undef offset
89 static Boolean XlwMenuSetValues();
90 static void XlwMenuRealize();
91 static void XlwMenuRedisplay();
92 static void XlwMenuResize();
93 static void XlwMenuInitialize();
94 static void XlwMenuRedisplay();
95 static void XlwMenuDestroy();
96 static void XlwMenuClassInitialize();
97 static void Start();
98 static void Drag();
99 static void Select();
101 static XtActionsRec
102 xlwMenuActionsList [] =
104 {"start", Start},
105 {"drag", Drag},
106 {"select", Select},
109 #define SuperClass ((CoreWidgetClass)&coreClassRec)
111 XlwMenuClassRec xlwMenuClassRec =
113 { /* CoreClass fields initialization */
114 (WidgetClass) SuperClass, /* superclass */
115 "XlwMenu", /* class_name */
116 sizeof(XlwMenuRec), /* size */
117 XlwMenuClassInitialize, /* class_initialize */
118 NULL, /* class_part_initialize */
119 FALSE, /* class_inited */
120 XlwMenuInitialize, /* initialize */
121 NULL, /* initialize_hook */
122 XlwMenuRealize, /* realize */
123 xlwMenuActionsList, /* actions */
124 XtNumber(xlwMenuActionsList), /* num_actions */
125 xlwMenuResources, /* resources */
126 XtNumber(xlwMenuResources), /* resource_count */
127 NULLQUARK, /* xrm_class */
128 TRUE, /* compress_motion */
129 TRUE, /* compress_exposure */
130 TRUE, /* compress_enterleave */
131 FALSE, /* visible_interest */
132 XlwMenuDestroy, /* destroy */
133 XlwMenuResize, /* resize */
134 XlwMenuRedisplay, /* expose */
135 XlwMenuSetValues, /* set_values */
136 NULL, /* set_values_hook */
137 XtInheritSetValuesAlmost, /* set_values_almost */
138 NULL, /* get_values_hook */
139 NULL, /* accept_focus */
140 XtVersion, /* version */
141 NULL, /* callback_private */
142 xlwMenuTranslations, /* tm_table */
143 XtInheritQueryGeometry, /* query_geometry */
144 XtInheritDisplayAccelerator, /* display_accelerator */
145 NULL /* extension */
146 }, /* XlwMenuClass fields initialization */
148 0 /* dummy */
152 WidgetClass xlwMenuWidgetClass = (WidgetClass) &xlwMenuClassRec;
154 int submenu_destroyed;
156 static int next_release_must_exit;
158 \f/* Utilities */
159 static void
160 push_new_stack (mw, val)
161 XlwMenuWidget mw;
162 widget_value* val;
164 if (!mw->menu.new_stack)
166 mw->menu.new_stack_length = 10;
167 mw->menu.new_stack =
168 (widget_value**)XtCalloc (mw->menu.new_stack_length,
169 sizeof (widget_value*));
171 else if (mw->menu.new_depth == mw->menu.new_stack_length)
173 mw->menu.new_stack_length *= 2;
174 mw->menu.new_stack =
175 (widget_value**)XtRealloc ((char*)mw->menu.new_stack,
176 mw->menu.new_stack_length * sizeof (widget_value*));
178 mw->menu.new_stack [mw->menu.new_depth++] = val;
181 static void
182 pop_new_stack_if_no_contents (mw)
183 XlwMenuWidget mw;
185 if (mw->menu.new_depth)
187 if (!mw->menu.new_stack [mw->menu.new_depth - 1]->contents)
188 mw->menu.new_depth -= 1;
192 static void
193 make_old_stack_space (mw, n)
194 XlwMenuWidget mw;
195 int n;
197 if (!mw->menu.old_stack)
199 mw->menu.old_stack_length = 10;
200 mw->menu.old_stack =
201 (widget_value**)XtCalloc (mw->menu.old_stack_length,
202 sizeof (widget_value*));
204 else if (mw->menu.old_stack_length < n)
206 mw->menu.old_stack_length *= 2;
207 mw->menu.old_stack =
208 (widget_value**)XtRealloc ((char*)mw->menu.old_stack,
209 mw->menu.old_stack_length * sizeof (widget_value*));
213 \f/* Size code */
214 static Boolean
215 all_dashes_p (s)
216 char *s;
218 char* p;
219 for (p = s; *p == '-'; p++);
220 return !*p;
224 string_width (mw, s)
225 XlwMenuWidget mw;
226 char *s;
228 XCharStruct xcs;
229 int drop;
231 XTextExtents (mw->menu.font, s, strlen (s), &drop, &drop, &drop, &xcs);
232 return xcs.width;
235 static int
236 arrow_width (mw)
237 XlwMenuWidget mw;
239 return mw->menu.font->ascent / 2 | 1;
242 static XtResource
243 nameResource[] =
245 {"labelString", "LabelString", XtRString, sizeof(String),
246 0, XtRImmediate, 0},
249 static char*
250 resource_widget_value (mw, val)
251 XlwMenuWidget mw;
252 widget_value *val;
254 if (!val->toolkit_data)
256 char* resourced_name = NULL;
257 char* complete_name;
258 XtGetSubresources ((Widget) mw,
259 (XtPointer) &resourced_name,
260 val->name, val->name,
261 nameResource, 1, NULL, 0);
262 if (!resourced_name)
263 resourced_name = val->name;
264 if (!val->value)
266 complete_name = (char *) XtMalloc (strlen (resourced_name) + 1);
267 strcpy (complete_name, resourced_name);
269 else
271 int complete_length =
272 strlen (resourced_name) + strlen (val->value) + 2;
273 complete_name = XtMalloc (complete_length);
274 *complete_name = 0;
275 strcat (complete_name, resourced_name);
276 strcat (complete_name, " ");
277 strcat (complete_name, val->value);
280 val->toolkit_data = complete_name;
281 val->free_toolkit_data = True;
283 return (char*)val->toolkit_data;
286 /* Returns the sizes of an item */
287 static void
288 size_menu_item (mw, val, horizontal_p, label_width, rest_width, height)
289 XlwMenuWidget mw;
290 widget_value* val;
291 int horizontal_p;
292 int* label_width;
293 int* rest_width;
294 int* height;
296 if (all_dashes_p (val->name))
298 *height = 2;
299 *label_width = 1;
300 *rest_width = 0;
302 else
304 *height =
305 mw->menu.font->ascent + mw->menu.font->descent
306 + 2 * mw->menu.vertical_spacing + 2 * mw->menu.shadow_thickness;
308 *label_width =
309 string_width (mw, resource_widget_value (mw, val))
310 + mw->menu.horizontal_spacing + mw->menu.shadow_thickness;
312 *rest_width = mw->menu.horizontal_spacing + mw->menu.shadow_thickness;
313 if (!horizontal_p)
315 if (val->contents)
316 *rest_width += arrow_width (mw) + mw->menu.arrow_spacing;
317 else if (val->key)
318 *rest_width +=
319 string_width (mw, val->key) + mw->menu.arrow_spacing;
324 static void
325 size_menu (mw, level)
326 XlwMenuWidget mw;
327 int level;
329 unsigned int label_width = 0;
330 int rest_width = 0;
331 int max_rest_width = 0;
332 unsigned int height = 0;
333 int horizontal_p = mw->menu.horizontal && (level == 0);
334 widget_value* val;
335 window_state* ws;
337 if (level >= mw->menu.old_depth)
338 abort ();
340 ws = &mw->menu.windows [level];
341 ws->width = 0;
342 ws->height = 0;
343 ws->label_width = 0;
345 for (val = mw->menu.old_stack [level]->contents; val; val = val->next)
347 size_menu_item (mw, val, horizontal_p, &label_width, &rest_width,
348 &height);
349 if (horizontal_p)
351 ws->width += label_width + rest_width;
352 if (height > ws->height)
353 ws->height = height;
355 else
357 if (label_width > ws->label_width)
358 ws->label_width = label_width;
359 if (rest_width > max_rest_width)
360 max_rest_width = rest_width;
361 ws->height += height;
365 if (horizontal_p)
366 ws->label_width = 0;
367 else
368 ws->width = ws->label_width + max_rest_width;
370 ws->width += 2 * mw->menu.shadow_thickness;
371 ws->height += 2 * mw->menu.shadow_thickness;
375 \f/* Display code */
376 static void
377 draw_arrow (mw, window, gc, x, y, width)
378 XlwMenuWidget mw;
379 Window window;
380 GC gc;
381 int x;
382 int y;
383 int width;
385 XPoint points [3];
386 points [0].x = x;
387 points [0].y = y + mw->menu.font->ascent;
388 points [1].x = x;
389 points [1].y = y;
390 points [2].x = x + width;
391 points [2].y = y + mw->menu.font->ascent / 2;
393 XFillPolygon (XtDisplay (mw), window, gc, points, 3, Convex,
394 CoordModeOrigin);
397 static void
398 draw_shadow_rectangle (mw, window, x, y, width, height, erase_p)
399 XlwMenuWidget mw;
400 Window window;
401 int x;
402 int y;
403 int width;
404 int height;
405 int erase_p;
407 Display *dpy = XtDisplay (mw);
408 GC top_gc = !erase_p ? mw->menu.shadow_top_gc : mw->menu.background_gc;
409 GC bottom_gc = !erase_p ? mw->menu.shadow_bottom_gc : mw->menu.background_gc;
410 int thickness = mw->menu.shadow_thickness;
411 XPoint points [4];
412 points [0].x = x;
413 points [0].y = y;
414 points [1].x = x + width;
415 points [1].y = y;
416 points [2].x = x + width - thickness;
417 points [2].y = y + thickness;
418 points [3].x = x;
419 points [3].y = y + thickness;
420 XFillPolygon (dpy, window, top_gc, points, 4, Convex, CoordModeOrigin);
421 points [0].x = x;
422 points [0].y = y + thickness;
423 points [1].x = x;
424 points [1].y = y + height;
425 points [2].x = x + thickness;
426 points [2].y = y + height - thickness;
427 points [3].x = x + thickness;
428 points [3].y = y + thickness;
429 XFillPolygon (dpy, window, top_gc, points, 4, Convex, CoordModeOrigin);
430 points [0].x = x + width;
431 points [0].y = y;
432 points [1].x = x + width - thickness;
433 points [1].y = y + thickness;
434 points [2].x = x + width - thickness;
435 points [2].y = y + height - thickness;
436 points [3].x = x + width;
437 points [3].y = y + height - thickness;
438 XFillPolygon (dpy, window, bottom_gc, points, 4, Convex, CoordModeOrigin);
439 points [0].x = x;
440 points [0].y = y + height;
441 points [1].x = x + width;
442 points [1].y = y + height;
443 points [2].x = x + width;
444 points [2].y = y + height - thickness;
445 points [3].x = x + thickness;
446 points [3].y = y + height - thickness;
447 XFillPolygon (dpy, window, bottom_gc, points, 4, Convex, CoordModeOrigin);
451 /* Display the menu item and increment where.x and where.y to show how large
452 ** the menu item was.
454 static void
455 display_menu_item (mw, val, ws, where, highlighted_p, horizontal_p, just_compute_p)
456 XlwMenuWidget mw;
457 widget_value* val;
458 window_state* ws;
459 XPoint* where;
460 Boolean highlighted_p;
461 Boolean horizontal_p;
462 Boolean just_compute_p;
464 GC deco_gc;
465 GC text_gc;
466 int font_ascent = mw->menu.font->ascent;
467 int font_descent = mw->menu.font->descent;
468 int shadow = mw->menu.shadow_thickness;
469 int separator_p = all_dashes_p (val->name);
470 int h_spacing = mw->menu.horizontal_spacing;
471 int v_spacing = mw->menu.vertical_spacing;
472 int label_width;
473 int rest_width;
474 int height;
475 int width;
476 int button_p;
478 /* compute the sizes of the item */
479 size_menu_item (mw, val, horizontal_p, &label_width, &rest_width, &height);
481 if (horizontal_p)
482 width = label_width + rest_width;
483 else
485 label_width = ws->label_width;
486 width = ws->width - 2 * shadow;
489 #if 0
490 /* see if it should be a button in the menubar */
491 button_p = horizontal_p && val->call_data;
492 #endif
493 button_p = 0;
495 /* Only highlight an enabled item that has a callback. */
496 if (highlighted_p)
497 if (!val->enabled || !(val->call_data || val->contents))
498 highlighted_p = 0;
500 /* do the drawing. */
501 if (!just_compute_p)
503 /* Add the shadow border of the containing menu */
504 int x = where->x + shadow;
505 int y = where->y + shadow;
507 /* pick the foreground and background GC. */
508 if (val->enabled)
509 text_gc = button_p ? mw->menu.button_gc : mw->menu.foreground_gc;
510 else
511 text_gc =
512 button_p ? mw->menu.inactive_button_gc : mw->menu.inactive_gc;
513 deco_gc = mw->menu.foreground_gc;
515 if (separator_p)
517 XDrawLine (XtDisplay (mw), ws->window, mw->menu.shadow_bottom_gc,
518 x, y, x + width, y);
519 XDrawLine (XtDisplay (mw), ws->window, mw->menu.shadow_top_gc,
520 x, y + 1, x + width, y + 1);
522 else
524 int x_offset = x + h_spacing + shadow;
525 char* display_string = resource_widget_value (mw, val);
526 draw_shadow_rectangle (mw, ws->window, x, y, width, height, True);
528 /* Deal with centering a menu title. */
529 if (!horizontal_p && !val->contents && !val->call_data)
531 int l = string_width (mw, display_string);
533 if (width > l)
534 x_offset = (width - l) >> 1;
536 XDrawString (XtDisplay (mw), ws->window, text_gc, x_offset,
537 y + v_spacing + shadow + font_ascent,
538 display_string, strlen (display_string));
540 if (!horizontal_p)
542 if (val->contents)
544 int a_w = arrow_width (mw);
545 draw_arrow (mw, ws->window, deco_gc,
546 x + width - arrow_width (mw)
547 - mw->menu.horizontal_spacing
548 - mw->menu.shadow_thickness,
549 y + v_spacing + shadow, a_w);
551 else if (val->key)
553 XDrawString (XtDisplay (mw), ws->window, text_gc,
554 x + label_width + mw->menu.arrow_spacing,
555 y + v_spacing + shadow + font_ascent,
556 val->key, strlen (val->key));
560 else if (button_p)
562 #if 1
563 XDrawRectangle (XtDisplay (mw), ws->window, deco_gc,
564 x + shadow, y + shadow,
565 label_width + h_spacing - 1,
566 font_ascent + font_descent + 2 * v_spacing - 1);
567 draw_shadow_rectangle (mw, ws->window, x, y, width, height,
568 False);
569 #else
570 highlighted_p = True;
571 #endif
573 else
575 XDrawRectangle (XtDisplay (mw), ws->window,
576 mw->menu.background_gc,
577 x + shadow, y + shadow,
578 label_width + h_spacing - 1,
579 font_ascent + font_descent + 2 * v_spacing - 1);
580 draw_shadow_rectangle (mw, ws->window, x, y, width, height,
581 True);
584 if (highlighted_p)
585 draw_shadow_rectangle (mw, ws->window, x, y, width, height, False);
589 where->x += width;
590 where->y += height;
593 static void
594 display_menu (mw, level, just_compute_p, highlighted_pos, hit, hit_return,
595 this, that)
596 XlwMenuWidget mw;
597 int level;
598 Boolean just_compute_p;
599 XPoint* highlighted_pos;
600 XPoint* hit;
601 widget_value** hit_return;
602 widget_value* this;
603 widget_value* that;
605 widget_value* val;
606 widget_value* following_item;
607 window_state* ws;
608 XPoint where;
609 int horizontal_p = mw->menu.horizontal && (level == 0);
610 int highlighted_p;
611 int just_compute_this_one_p;
613 if (level >= mw->menu.old_depth)
614 abort ();
616 if (level < mw->menu.old_depth - 1)
617 following_item = mw->menu.old_stack [level + 1];
618 else
619 following_item = NULL;
621 if (hit)
622 *hit_return = NULL;
624 where.x = 0;
625 where.y = 0;
627 ws = &mw->menu.windows [level];
628 for (val = mw->menu.old_stack [level]->contents; val; val = val->next)
630 highlighted_p = val == following_item;
631 if (highlighted_p && highlighted_pos)
633 if (horizontal_p)
634 highlighted_pos->x = where.x;
635 else
636 highlighted_pos->y = where.y;
639 just_compute_this_one_p =
640 just_compute_p || ((this || that) && val != this && val != that);
642 display_menu_item (mw, val, ws, &where, highlighted_p, horizontal_p,
643 just_compute_this_one_p);
645 if (highlighted_p && highlighted_pos)
647 if (horizontal_p)
648 highlighted_pos->y = where.y;
649 else
650 highlighted_pos->x = where.x;
653 if (hit
654 && !*hit_return
655 && (horizontal_p ? hit->x < where.x : hit->y < where.y)
656 && !all_dashes_p (val->name))
657 *hit_return = val;
659 if (horizontal_p)
660 where.y = 0;
661 else
662 where.x = 0;
665 if (!just_compute_p)
666 draw_shadow_rectangle (mw, ws->window, 0, 0, ws->width, ws->height, False);
669 \f/* Motion code */
670 static void
671 set_new_state (mw, val, level)
672 XlwMenuWidget mw;
673 widget_value* val;
674 int level;
676 int i;
678 mw->menu.new_depth = 0;
679 for (i = 0; i < level; i++)
680 push_new_stack (mw, mw->menu.old_stack [i]);
681 push_new_stack (mw, val);
684 static void
685 make_windows_if_needed (mw, n)
686 XlwMenuWidget mw;
687 int n;
689 int i;
690 int start_at;
691 XSetWindowAttributes xswa;
692 int mask;
693 Window root = RootWindowOfScreen (DefaultScreenOfDisplay (XtDisplay (mw)));
694 window_state* windows;
696 if (mw->menu.windows_length >= n)
697 return;
699 xswa.save_under = True;
700 xswa.override_redirect = True;
701 xswa.background_pixel = mw->core.background_pixel;
702 xswa.border_pixel = mw->core.border_pixel;
703 xswa.event_mask =
704 ExposureMask | PointerMotionMask | PointerMotionHintMask
705 | ButtonReleaseMask | ButtonPressMask;
706 xswa.cursor = mw->menu.cursor_shape;
707 mask = CWSaveUnder | CWOverrideRedirect | CWBackPixel | CWBorderPixel
708 | CWEventMask | CWCursor;
710 if (!mw->menu.windows)
712 mw->menu.windows =
713 (window_state*)XtMalloc (n * sizeof (window_state));
714 start_at = 0;
716 else
718 mw->menu.windows =
719 (window_state*)XtRealloc ((char*)mw->menu.windows,
720 n * sizeof (window_state));
721 start_at = mw->menu.windows_length;
723 mw->menu.windows_length = n;
725 windows = mw->menu.windows;
727 for (i = start_at; i < n; i++)
729 windows [i].x = 0;
730 windows [i].y = 0;
731 windows [i].width = 1;
732 windows [i].height = 1;
733 windows [i].window =
734 XCreateWindow (XtDisplay (mw), root, 0, 0, 1, 1,
735 0, 0, CopyFromParent, CopyFromParent, mask, &xswa);
739 /* Make the window fit in the screen */
740 static void
741 fit_to_screen (mw, ws, previous_ws, horizontal_p)
742 XlwMenuWidget mw;
743 window_state* ws;
744 window_state* previous_ws;
745 Boolean horizontal_p;
747 unsigned int screen_width = WidthOfScreen (XtScreen (mw));
748 unsigned int screen_height = HeightOfScreen (XtScreen (mw));
750 if (ws->x < 0)
751 ws->x = 0;
752 else if (ws->x + ws->width > screen_width)
754 if (!horizontal_p)
755 ws->x = previous_ws->x - ws->width;
756 else
757 ws->x = screen_width - ws->width;
758 if (ws->x < 0)
759 ws->x = 0;
761 if (ws->y < 0)
762 ws->y = 0;
763 else if (ws->y + ws->height > screen_height)
765 if (horizontal_p)
766 ws->y = previous_ws->y - ws->height;
767 else
768 ws->y = screen_height - ws->height;
769 if (ws->y < 0)
770 ws->y = 0;
774 /* Updates old_stack from new_stack and redisplays. */
775 static void
776 remap_menubar (mw)
777 XlwMenuWidget mw;
779 int i;
780 int last_same;
781 XPoint selection_position;
782 int old_depth = mw->menu.old_depth;
783 int new_depth = mw->menu.new_depth;
784 widget_value** old_stack;
785 widget_value** new_stack;
786 window_state* windows;
787 widget_value* old_selection;
788 widget_value* new_selection;
790 /* Check that enough windows and old_stack are ready. */
791 make_windows_if_needed (mw, new_depth);
792 make_old_stack_space (mw, new_depth);
793 windows = mw->menu.windows;
794 old_stack = mw->menu.old_stack;
795 new_stack = mw->menu.new_stack;
797 /* compute the last identical different entry */
798 for (i = 1; i < old_depth && i < new_depth; i++)
799 if (old_stack [i] != new_stack [i])
800 break;
801 last_same = i - 1;
803 /* Memorize the previously selected item to be able to refresh it */
804 old_selection = last_same + 1 < old_depth ? old_stack [last_same + 1] : NULL;
805 if (old_selection && !old_selection->enabled)
806 old_selection = NULL;
807 new_selection = last_same + 1 < new_depth ? new_stack [last_same + 1] : NULL;
808 if (new_selection && !new_selection->enabled)
809 new_selection = NULL;
811 /* updates old_state from new_state. It has to be done now because
812 display_menu (called below) uses the old_stack to know what to display. */
813 for (i = last_same + 1; i < new_depth; i++)
814 old_stack [i] = new_stack [i];
815 mw->menu.old_depth = new_depth;
817 /* refresh the last seletion */
818 selection_position.x = 0;
819 selection_position.y = 0;
820 display_menu (mw, last_same, new_selection == old_selection,
821 &selection_position, NULL, NULL, old_selection, new_selection);
823 /* Now popup the new menus */
824 for (i = last_same + 1; i < new_depth && new_stack [i]->contents; i++)
826 window_state* previous_ws = &windows [i - 1];
827 window_state* ws = &windows [i];
829 ws->x =
830 previous_ws->x + selection_position.x + mw->menu.shadow_thickness;
831 if (!mw->menu.horizontal || i > 1)
832 ws->x += mw->menu.shadow_thickness;
833 ws->y =
834 previous_ws->y + selection_position.y + mw->menu.shadow_thickness;
836 size_menu (mw, i);
838 fit_to_screen (mw, ws, previous_ws, mw->menu.horizontal && i == 1);
840 XClearWindow (XtDisplay (mw), ws->window);
841 XMoveResizeWindow (XtDisplay (mw), ws->window, ws->x, ws->y,
842 ws->width, ws->height);
843 XMapRaised (XtDisplay (mw), ws->window);
844 display_menu (mw, i, False, &selection_position, NULL, NULL, NULL, NULL);
847 /* unmap the menus that popped down */
848 for (i = new_depth - 1; i < old_depth; i++)
849 if (i >= new_depth || !new_stack [i]->contents)
850 XUnmapWindow (XtDisplay (mw), windows [i].window);
853 static Boolean
854 motion_event_is_in_menu (mw, ev, level, relative_pos)
855 XlwMenuWidget mw;
856 XMotionEvent* ev;
857 int level;
858 XPoint* relative_pos;
860 window_state* ws = &mw->menu.windows [level];
861 unsigned int x = level == 0 ? ws->x : ws->x + mw->menu.shadow_thickness;
862 unsigned int y = level == 0 ? ws->y : ws->y + mw->menu.shadow_thickness;
863 relative_pos->x = ev->x_root - x;
864 relative_pos->y = ev->y_root - y;
865 return (x < ev->x_root && ev->x_root < x + ws->width
866 && y < ev->y_root && ev->y_root < y + ws->height);
869 static Boolean
870 map_event_to_widget_value (mw, ev, val, level)
871 XlwMenuWidget mw;
872 XMotionEvent* ev;
873 widget_value** val;
874 int* level;
876 int i;
877 XPoint relative_pos;
878 window_state* ws;
880 *val = NULL;
882 /* Find the window */
883 for (i = mw->menu.old_depth - 1; i >= 0; i--)
885 ws = &mw->menu.windows [i];
886 if (ws && motion_event_is_in_menu (mw, ev, i, &relative_pos))
888 display_menu (mw, i, True, NULL, &relative_pos, val, NULL, NULL);
890 if (*val)
892 *level = i + 1;
893 return True;
897 return False;
900 \f/* Procedures */
901 static void
902 make_drawing_gcs (mw)
903 XlwMenuWidget mw;
905 XGCValues xgcv;
907 xgcv.font = mw->menu.font->fid;
908 xgcv.foreground = mw->menu.foreground;
909 xgcv.background = mw->core.background_pixel;
910 mw->menu.foreground_gc = XtGetGC ((Widget)mw,
911 GCFont | GCForeground | GCBackground,
912 &xgcv);
914 xgcv.font = mw->menu.font->fid;
915 xgcv.foreground = mw->menu.button_foreground;
916 xgcv.background = mw->core.background_pixel;
917 mw->menu.button_gc = XtGetGC ((Widget)mw,
918 GCFont | GCForeground | GCBackground,
919 &xgcv);
921 xgcv.font = mw->menu.font->fid;
922 xgcv.foreground = mw->menu.foreground;
923 xgcv.background = mw->core.background_pixel;
924 xgcv.fill_style = FillStippled;
925 xgcv.stipple = mw->menu.gray_pixmap;
926 mw->menu.inactive_gc = XtGetGC ((Widget)mw,
927 (GCFont | GCForeground | GCBackground
928 | GCFillStyle | GCStipple), &xgcv);
930 xgcv.font = mw->menu.font->fid;
931 xgcv.foreground = mw->menu.button_foreground;
932 xgcv.background = mw->core.background_pixel;
933 xgcv.fill_style = FillStippled;
934 xgcv.stipple = mw->menu.gray_pixmap;
935 mw->menu.inactive_button_gc = XtGetGC ((Widget)mw,
936 (GCFont | GCForeground | GCBackground
937 | GCFillStyle | GCStipple), &xgcv);
939 xgcv.font = mw->menu.font->fid;
940 xgcv.foreground = mw->core.background_pixel;
941 xgcv.background = mw->menu.foreground;
942 mw->menu.background_gc = XtGetGC ((Widget)mw,
943 GCFont | GCForeground | GCBackground,
944 &xgcv);
947 static void
948 release_drawing_gcs (mw)
949 XlwMenuWidget mw;
951 XtReleaseGC ((Widget) mw, mw->menu.foreground_gc);
952 XtReleaseGC ((Widget) mw, mw->menu.button_gc);
953 XtReleaseGC ((Widget) mw, mw->menu.inactive_gc);
954 XtReleaseGC ((Widget) mw, mw->menu.inactive_button_gc);
955 XtReleaseGC ((Widget) mw, mw->menu.background_gc);
956 /* let's get some segvs if we try to use these... */
957 mw->menu.foreground_gc = (GC) -1;
958 mw->menu.button_gc = (GC) -1;
959 mw->menu.inactive_gc = (GC) -1;
960 mw->menu.inactive_button_gc = (GC) -1;
961 mw->menu.background_gc = (GC) -1;
964 #define MINL(x,y) ((((unsigned long) (x)) < ((unsigned long) (y))) \
965 ? ((unsigned long) (x)) : ((unsigned long) (y)))
967 static void
968 make_shadow_gcs (mw)
969 XlwMenuWidget mw;
971 XGCValues xgcv;
972 unsigned long pm = 0;
973 Display *dpy = XtDisplay ((Widget) mw);
974 Colormap cmap = DefaultColormapOfScreen (XtScreen ((Widget) mw));
975 XColor topc, botc;
976 int top_frobbed = 0, bottom_frobbed = 0;
978 if (mw->menu.top_shadow_color == -1)
979 mw->menu.top_shadow_color = mw->core.background_pixel;
980 if (mw->menu.bottom_shadow_color == -1)
981 mw->menu.bottom_shadow_color = mw->menu.foreground;
983 if (mw->menu.top_shadow_color == mw->core.background_pixel ||
984 mw->menu.top_shadow_color == mw->menu.foreground)
986 topc.pixel = mw->core.background_pixel;
987 XQueryColor (dpy, cmap, &topc);
988 /* don't overflow/wrap! */
989 topc.red = MINL (65535, topc.red * 1.2);
990 topc.green = MINL (65535, topc.green * 1.2);
991 topc.blue = MINL (65535, topc.blue * 1.2);
992 if (XAllocColor (dpy, cmap, &topc))
994 mw->menu.top_shadow_color = topc.pixel;
995 top_frobbed = 1;
998 if (mw->menu.bottom_shadow_color == mw->menu.foreground ||
999 mw->menu.bottom_shadow_color == mw->core.background_pixel)
1001 botc.pixel = mw->core.background_pixel;
1002 XQueryColor (dpy, cmap, &botc);
1003 botc.red *= 0.6;
1004 botc.green *= 0.6;
1005 botc.blue *= 0.6;
1006 if (XAllocColor (dpy, cmap, &botc))
1008 mw->menu.bottom_shadow_color = botc.pixel;
1009 bottom_frobbed = 1;
1013 if (top_frobbed && bottom_frobbed)
1015 int top_avg = ((topc.red / 3) + (topc.green / 3) + (topc.blue / 3));
1016 int bot_avg = ((botc.red / 3) + (botc.green / 3) + (botc.blue / 3));
1017 if (bot_avg > top_avg)
1019 Pixel tmp = mw->menu.top_shadow_color;
1020 mw->menu.top_shadow_color = mw->menu.bottom_shadow_color;
1021 mw->menu.bottom_shadow_color = tmp;
1023 else if (topc.pixel == botc.pixel)
1025 if (botc.pixel == mw->menu.foreground)
1026 mw->menu.top_shadow_color = mw->core.background_pixel;
1027 else
1028 mw->menu.bottom_shadow_color = mw->menu.foreground;
1032 if (!mw->menu.top_shadow_pixmap &&
1033 mw->menu.top_shadow_color == mw->core.background_pixel)
1035 mw->menu.top_shadow_pixmap = mw->menu.gray_pixmap;
1036 mw->menu.top_shadow_color = mw->menu.foreground;
1038 if (!mw->menu.bottom_shadow_pixmap &&
1039 mw->menu.bottom_shadow_color == mw->core.background_pixel)
1041 mw->menu.bottom_shadow_pixmap = mw->menu.gray_pixmap;
1042 mw->menu.bottom_shadow_color = mw->menu.foreground;
1045 xgcv.fill_style = FillStippled;
1046 xgcv.foreground = mw->menu.top_shadow_color;
1047 xgcv.stipple = mw->menu.top_shadow_pixmap;
1048 pm = (xgcv.stipple ? GCStipple|GCFillStyle : 0);
1049 mw->menu.shadow_top_gc = XtGetGC ((Widget)mw, GCForeground | pm, &xgcv);
1051 xgcv.foreground = mw->menu.bottom_shadow_color;
1052 xgcv.stipple = mw->menu.bottom_shadow_pixmap;
1053 pm = (xgcv.stipple ? GCStipple|GCFillStyle : 0);
1054 mw->menu.shadow_bottom_gc = XtGetGC ((Widget)mw, GCForeground | pm, &xgcv);
1058 static void
1059 release_shadow_gcs (mw)
1060 XlwMenuWidget mw;
1062 XtReleaseGC ((Widget) mw, mw->menu.shadow_top_gc);
1063 XtReleaseGC ((Widget) mw, mw->menu.shadow_bottom_gc);
1066 static void
1067 XlwMenuInitialize (request, mw, args, num_args)
1068 Widget request;
1069 XlwMenuWidget mw;
1070 ArgList args;
1071 Cardinal *num_args;
1073 /* Get the GCs and the widget size */
1074 XSetWindowAttributes xswa;
1075 int mask;
1077 Window window = RootWindowOfScreen (DefaultScreenOfDisplay (XtDisplay (mw)));
1078 Display* display = XtDisplay (mw);
1080 #if 0
1081 widget_value *tem = (widget_value *) XtMalloc (sizeof (widget_value));
1083 /* _XtCreate is freeing the object that was passed to us,
1084 so make a copy that we will actually keep. */
1085 lwlib_bcopy (mw->menu.contents, tem, sizeof (widget_value));
1086 mw->menu.contents = tem;
1087 #endif
1089 /* mw->menu.cursor = XCreateFontCursor (display, mw->menu.cursor_shape); */
1090 mw->menu.cursor = mw->menu.cursor_shape;
1092 mw->menu.gray_pixmap
1093 = XCreatePixmapFromBitmapData (display, window, gray_bits,
1094 gray_width, gray_height,
1095 (unsigned long)1, (unsigned long)0, 1);
1097 /* I don't understand why this ends up 0 sometimes,
1098 but it does. This kludge works around it.
1099 Can anyone find a real fix? -- rms. */
1100 if (mw->menu.font == 0)
1101 mw->menu.font = xlwmenu_default_font;
1103 make_drawing_gcs (mw);
1104 make_shadow_gcs (mw);
1106 xswa.background_pixel = mw->core.background_pixel;
1107 xswa.border_pixel = mw->core.border_pixel;
1108 mask = CWBackPixel | CWBorderPixel;
1110 mw->menu.popped_up = False;
1112 mw->menu.old_depth = 1;
1113 mw->menu.old_stack = (widget_value**)XtMalloc (sizeof (widget_value*));
1114 mw->menu.old_stack_length = 1;
1115 mw->menu.old_stack [0] = mw->menu.contents;
1117 mw->menu.new_depth = 0;
1118 mw->menu.new_stack = 0;
1119 mw->menu.new_stack_length = 0;
1120 push_new_stack (mw, mw->menu.contents);
1122 mw->menu.windows = (window_state*)XtMalloc (sizeof (window_state));
1123 mw->menu.windows_length = 1;
1124 mw->menu.windows [0].x = 0;
1125 mw->menu.windows [0].y = 0;
1126 mw->menu.windows [0].width = 0;
1127 mw->menu.windows [0].height = 0;
1128 size_menu (mw, 0);
1130 mw->core.width = mw->menu.windows [0].width;
1131 mw->core.height = mw->menu.windows [0].height;
1134 static void
1135 XlwMenuClassInitialize ()
1139 static void
1140 XlwMenuRealize (w, valueMask, attributes)
1141 Widget w;
1142 Mask *valueMask;
1143 XSetWindowAttributes *attributes;
1145 XlwMenuWidget mw = (XlwMenuWidget)w;
1146 XSetWindowAttributes xswa;
1147 int mask;
1149 (*xlwMenuWidgetClass->core_class.superclass->core_class.realize)
1150 (w, valueMask, attributes);
1152 xswa.save_under = True;
1153 xswa.cursor = mw->menu.cursor_shape;
1154 mask = CWSaveUnder | CWCursor;
1155 XChangeWindowAttributes (XtDisplay (w), XtWindow (w), mask, &xswa);
1157 mw->menu.windows [0].window = XtWindow (w);
1158 mw->menu.windows [0].x = w->core.x;
1159 mw->menu.windows [0].y = w->core.y;
1160 mw->menu.windows [0].width = w->core.width;
1161 mw->menu.windows [0].height = w->core.height;
1164 /* Only the toplevel menubar/popup is a widget so it's the only one that
1165 receives expose events through Xt. So we repaint all the other panes
1166 when receiving an Expose event. */
1167 static void
1168 XlwMenuRedisplay (w, ev, region)
1169 Widget w;
1170 XEvent* ev;
1171 Region region;
1173 XlwMenuWidget mw = (XlwMenuWidget)w;
1174 int i;
1176 /* If we have a depth beyond 1, it's because a submenu was displayed.
1177 If the submenu has been destroyed, set the depth back to 1. */
1178 if (submenu_destroyed)
1180 mw->menu.old_depth = 1;
1181 submenu_destroyed = 0;
1184 for (i = 0; i < mw->menu.old_depth; i++)
1185 display_menu (mw, i, False, NULL, NULL, NULL, NULL, NULL);
1188 static void
1189 XlwMenuDestroy (w)
1190 Widget w;
1192 int i;
1193 XlwMenuWidget mw = (XlwMenuWidget) w;
1195 if (pointer_grabbed)
1196 XtUngrabPointer ((Widget)w, CurrentTime);
1197 pointer_grabbed = 0;
1199 submenu_destroyed = 1;
1201 release_drawing_gcs (mw);
1202 release_shadow_gcs (mw);
1204 /* this doesn't come from the resource db but is created explicitly
1205 so we must free it ourselves. */
1206 XFreePixmap (XtDisplay (mw), mw->menu.gray_pixmap);
1207 mw->menu.gray_pixmap = (Pixmap) -1;
1209 #if 0
1210 /* Do free mw->menu.contents because nowadays we copy it
1211 during initialization. */
1212 XtFree (mw->menu.contents);
1213 #endif
1215 /* Don't free mw->menu.contents because that comes from our creator.
1216 The `*_stack' elements are just pointers into `contents' so leave
1217 that alone too. But free the stacks themselves. */
1218 if (mw->menu.old_stack) XtFree ((char *) mw->menu.old_stack);
1219 if (mw->menu.new_stack) XtFree ((char *) mw->menu.new_stack);
1221 /* Remember, you can't free anything that came from the resource
1222 database. This includes:
1223 mw->menu.cursor
1224 mw->menu.top_shadow_pixmap
1225 mw->menu.bottom_shadow_pixmap
1226 mw->menu.font
1227 Also the color cells of top_shadow_color, bottom_shadow_color,
1228 foreground, and button_foreground will never be freed until this
1229 client exits. Nice, eh?
1232 /* start from 1 because the one in slot 0 is w->core.window */
1233 for (i = 1; i < mw->menu.windows_length; i++)
1234 XDestroyWindow (XtDisplay (mw), mw->menu.windows [i].window);
1235 if (mw->menu.windows)
1236 XtFree ((char *) mw->menu.windows);
1239 static Boolean
1240 XlwMenuSetValues (current, request, new)
1241 Widget current;
1242 Widget request;
1243 Widget new;
1245 XlwMenuWidget oldmw = (XlwMenuWidget)current;
1246 XlwMenuWidget newmw = (XlwMenuWidget)new;
1247 Boolean redisplay = False;
1248 int i;
1250 if (newmw->menu.contents
1251 && newmw->menu.contents->contents
1252 && newmw->menu.contents->contents->change >= VISIBLE_CHANGE)
1253 redisplay = True;
1255 if (newmw->core.background_pixel != oldmw->core.background_pixel
1256 || newmw->menu.foreground != oldmw->menu.foreground
1257 || newmw->menu.font != oldmw->menu.font)
1259 release_drawing_gcs (newmw);
1260 make_drawing_gcs (newmw);
1261 redisplay = True;
1263 for (i = 0; i < oldmw->menu.windows_length; i++)
1265 XSetWindowBackground (XtDisplay (oldmw),
1266 oldmw->menu.windows [i].window,
1267 newmw->core.background_pixel);
1268 /* clear windows and generate expose events */
1269 XClearArea (XtDisplay (oldmw), oldmw->menu.windows[i].window,
1270 0, 0, 0, 0, True);
1274 return redisplay;
1277 static void
1278 XlwMenuResize (w)
1279 Widget w;
1281 XlwMenuWidget mw = (XlwMenuWidget)w;
1283 if (mw->menu.popped_up)
1285 /* Don't allow the popup menu to resize itself. */
1286 mw->core.width = mw->menu.windows [0].width;
1287 mw->core.height = mw->menu.windows [0].height;
1288 mw->core.parent->core.width = mw->core.width ;
1289 mw->core.parent->core.height = mw->core.height ;
1291 else
1293 mw->menu.windows [0].width = mw->core.width;
1294 mw->menu.windows [0].height = mw->core.height;
1298 \f/* Action procedures */
1299 static void
1300 handle_single_motion_event (mw, ev)
1301 XlwMenuWidget mw;
1302 XMotionEvent* ev;
1304 widget_value* val;
1305 int level;
1307 if (!map_event_to_widget_value (mw, ev, &val, &level))
1308 pop_new_stack_if_no_contents (mw);
1309 else
1310 set_new_state (mw, val, level);
1311 remap_menubar (mw);
1313 /* Sync with the display. Makes it feel better on X terms. */
1314 XSync (XtDisplay (mw), False);
1317 static void
1318 handle_motion_event (mw, ev)
1319 XlwMenuWidget mw;
1320 XMotionEvent* ev;
1322 int x = ev->x_root;
1323 int y = ev->y_root;
1324 int state = ev->state;
1326 handle_single_motion_event (mw, ev);
1328 /* allow motion events to be generated again */
1329 if (ev->is_hint
1330 && XQueryPointer (XtDisplay (mw), ev->window,
1331 &ev->root, &ev->subwindow,
1332 &ev->x_root, &ev->y_root,
1333 &ev->x, &ev->y,
1334 &ev->state)
1335 && ev->state == state
1336 && (ev->x_root != x || ev->y_root != y))
1337 handle_single_motion_event (mw, ev);
1340 static void
1341 Start (w, ev, params, num_params)
1342 Widget w;
1343 XEvent *ev;
1344 String *params;
1345 Cardinal *num_params;
1347 XlwMenuWidget mw = (XlwMenuWidget)w;
1349 if (!mw->menu.popped_up)
1351 menu_post_event = *ev;
1352 pop_up_menu (mw, ev);
1354 else
1356 /* If we push a button while the menu is posted semipermanently,
1357 releasing the button should always pop the menu down. */
1358 next_release_must_exit = 1;
1360 /* notes the absolute position of the menubar window */
1361 mw->menu.windows [0].x = ev->xmotion.x_root - ev->xmotion.x;
1362 mw->menu.windows [0].y = ev->xmotion.y_root - ev->xmotion.y;
1364 /* handles the down like a move, slots are compatible */
1365 handle_motion_event (mw, &ev->xmotion);
1369 static void
1370 Drag (w, ev, params, num_params)
1371 Widget w;
1372 XEvent *ev;
1373 String *params;
1374 Cardinal *num_params;
1376 XlwMenuWidget mw = (XlwMenuWidget)w;
1377 if (mw->menu.popped_up)
1378 handle_motion_event (mw, &ev->xmotion);
1381 static void
1382 Select (w, ev, params, num_params)
1383 Widget w;
1384 XEvent *ev;
1385 String *params;
1386 Cardinal *num_params;
1388 XlwMenuWidget mw = (XlwMenuWidget)w;
1389 widget_value* selected_item = mw->menu.old_stack [mw->menu.old_depth - 1];
1391 /* If user releases the button quickly, without selecting anything,
1392 after the initial down-click that brought the menu up,
1393 do nothing. */
1394 if ((selected_item == 0
1395 || ((widget_value *) selected_item)->call_data == 0)
1396 && !next_release_must_exit
1397 && (ev->xbutton.time - menu_post_event.xbutton.time
1398 < XtGetMultiClickTime (XtDisplay (w))))
1399 return;
1401 /* pop down everything. */
1402 mw->menu.new_depth = 1;
1403 remap_menubar (mw);
1405 if (mw->menu.popped_up)
1407 mw->menu.popped_up = False;
1408 XtUngrabPointer ((Widget)mw, ev->xmotion.time);
1409 if (XtIsShell (XtParent ((Widget) mw)))
1410 XtPopdown (XtParent ((Widget) mw));
1411 else
1413 XtRemoveGrab ((Widget) mw);
1414 display_menu (mw, 0, False, NULL, NULL, NULL, NULL, NULL);
1418 /* callback */
1419 XtCallCallbackList ((Widget)mw, mw->menu.select, (XtPointer)selected_item);
1423 \f/* Special code to pop-up a menu */
1424 void
1425 pop_up_menu (mw, event)
1426 XlwMenuWidget mw;
1427 XButtonPressedEvent* event;
1429 int x = event->x_root;
1430 int y = event->y_root;
1431 int w;
1432 int h;
1433 int borderwidth = mw->menu.shadow_thickness;
1434 Screen* screen = XtScreen (mw);
1435 Display *display = XtDisplay (mw);
1437 next_release_must_exit = 0;
1439 XtCallCallbackList ((Widget)mw, mw->menu.open, NULL);
1441 if (XtIsShell (XtParent ((Widget)mw)))
1442 size_menu (mw, 0);
1444 w = mw->menu.windows [0].width;
1445 h = mw->menu.windows [0].height;
1447 x -= borderwidth;
1448 y -= borderwidth;
1449 if (x < borderwidth)
1450 x = borderwidth;
1451 if (x + w + 2 * borderwidth > WidthOfScreen (screen))
1452 x = WidthOfScreen (screen) - w - 2 * borderwidth;
1453 if (y < borderwidth)
1454 y = borderwidth;
1455 if (y + h + 2 * borderwidth> HeightOfScreen (screen))
1456 y = HeightOfScreen (screen) - h - 2 * borderwidth;
1458 mw->menu.popped_up = True;
1459 if (XtIsShell (XtParent ((Widget)mw)))
1461 XtConfigureWidget (XtParent ((Widget)mw), x, y, w, h,
1462 XtParent ((Widget)mw)->core.border_width);
1463 XtPopup (XtParent ((Widget)mw), XtGrabExclusive);
1464 display_menu (mw, 0, False, NULL, NULL, NULL, NULL, NULL);
1465 mw->menu.windows [0].x = x + borderwidth;
1466 mw->menu.windows [0].y = y + borderwidth;
1468 else
1470 XEvent *ev = (XEvent *) event;
1472 XtAddGrab ((Widget) mw, True, True);
1474 /* notes the absolute position of the menubar window */
1475 mw->menu.windows [0].x = ev->xmotion.x_root - ev->xmotion.x;
1476 mw->menu.windows [0].y = ev->xmotion.y_root - ev->xmotion.y;
1479 #ifdef emacs
1480 x_catch_errors (display);
1481 #endif
1482 XtGrabPointer ((Widget)mw, False,
1483 (PointerMotionMask
1484 | PointerMotionHintMask
1485 | ButtonReleaseMask
1486 | ButtonPressMask),
1487 GrabModeAsync, GrabModeAsync, None,
1488 mw->menu.cursor_shape,
1489 event->time);
1490 pointer_grabbed = 1;
1491 #ifdef emacs
1492 if (x_had_errors_p (display))
1494 pointer_grabbed = 0;
1495 XtUngrabPointer ((Widget)mw, event->time);
1497 x_uncatch_errors (display);
1498 #endif
1500 handle_motion_event (mw, (XMotionEvent*)event);