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