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