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