* alloc.c: Do not define struct catchtag.
[bpt/emacs.git] / lwlib / xlwmenu.c
CommitLineData
5c520e0a 1/* Implements a lightweight menubar widget.
81e32ca3
GM
2Copyright (C) 1992 Lucid, Inc.
3Copyright (C) 1994, 1995, 1997, 1999, 2000, 2001, 2002, 2003, 2004,
f5d0ac07 4 2005, 2006, 2007, 2008, 2009 Free Software Foundation, Inc.
07bf635f
RS
5
6This file is part of the Lucid Widget Library.
7
5c520e0a 8The Lucid Widget Library is free software; you can redistribute it and/or
07bf635f
RS
9modify it under the terms of the GNU General Public License as published by
10the Free Software Foundation; either version 2, or (at your option)
11any later version.
12
13The Lucid Widget Library is distributed in the hope that it will be useful,
5c520e0a 14but WITHOUT ANY WARRANTY; without even the implied warranty of
07bf635f
RS
15MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16GNU General Public License for more details.
17
18You should have received a copy of the GNU General Public License
1fb87c77 19along with GNU Emacs; see the file COPYING. If not, write to the
364c38d3
LK
20Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
21Boston, MA 02110-1301, USA. */
07bf635f
RS
22
23/* Created by devin@lucid.com */
24
0f0912e6
PE
25#ifdef HAVE_CONFIG_H
26#include <config.h>
27#endif
28
d7306fe6 29#include <setjmp.h>
404074e2 30#include "lisp.h"
2f96293d 31
07bf635f
RS
32#include <stdio.h>
33
34#include <sys/types.h>
5c520e0a
SS
35#if (defined __sun) && !(defined SUNOS41)
36#define SUNOS41
07bf635f 37#include <X11/Xos.h>
5c520e0a
SS
38#undef SUNOS41
39#else
40#include <X11/Xos.h>
41#endif
07bf635f 42#include <X11/IntrinsicP.h>
2289f3f4 43#include <X11/ObjectP.h>
07bf635f
RS
44#include <X11/StringDefs.h>
45#include <X11/cursorfont.h>
07bf635f 46#include "xlwmenuP.h"
fc8fefed 47
a57a1605 48#ifdef emacs
f1c16db4
GM
49
50/* Defined in xfns.c. When config.h defines `static' as empty, we get
51 redefinition errors when gray_bitmap is included more than once, so
52 we're referring to the one include in xfns.c here. */
53
54extern int gray_bitmap_width;
55extern int gray_bitmap_height;
5916243b 56extern char *gray_bitmap_bits;
f1c16db4 57
1f5df1f7
JD
58#include "xterm.h"
59
f1c16db4
GM
60#else /* not emacs */
61
62#include <X11/bitmaps/gray>
63#define gray_bitmap_width gray_width
64#define gray_bitmap_height gray_height
65#define gray_bitmap_bits gray_bits
66
67#endif /* not emacs */
d398028f
PR
68
69static int pointer_grabbed;
70static XEvent menu_post_event;
07bf635f 71
47d52240
RS
72XFontStruct *xlwmenu_default_font;
73
5c520e0a
SS
74static char
75xlwMenuTranslations [] =
ba624d0f
RS
76"<BtnDown>: start()\n\
77<Motion>: drag()\n\
78<BtnUp>: select()\n\
79<Key>Shift_L: nothing()\n\
80<Key>Shift_R: nothing()\n\
81<Key>Meta_L: nothing()\n\
82<Key>Meta_R: nothing()\n\
83<Key>Control_L: nothing()\n\
84<Key>Control_R: nothing()\n\
85<Key>Hyper_L: nothing()\n\
86<Key>Hyper_R: nothing()\n\
87<Key>Super_L: nothing()\n\
88<Key>Super_R: nothing()\n\
89<Key>Alt_L: nothing()\n\
90<Key>Alt_R: nothing()\n\
91<Key>Caps_Lock: nothing()\n\
92<Key>Shift_Lock: nothing()\n\
93<KeyUp>Shift_L: nothing()\n\
94<KeyUp>Shift_R: nothing()\n\
95<KeyUp>Meta_L: nothing()\n\
96<KeyUp>Meta_R: nothing()\n\
97<KeyUp>Control_L: nothing()\n\
98<KeyUp>Control_R: nothing()\n\
99<KeyUp>Hyper_L: nothing()\n\
100<KeyUp>Hyper_R: nothing()\n\
101<KeyUp>Super_L: nothing()\n\
102<KeyUp>Super_R: nothing()\n\
103<KeyUp>Alt_L: nothing()\n\
104<KeyUp>Alt_R: nothing()\n\
105<KeyUp>Caps_Lock: nothing()\n\
106<KeyUp>Shift_Lock:nothing()\n\
a7e19a26
PJ
107<Key>Return: select()\n\
108<Key>Down: down()\n\
109<Key>Up: up()\n\
110<Key>Left: left()\n\
111<Key>Right: right()\n\
ba624d0f
RS
112<Key>: key()\n\
113<KeyUp>: key()\n\
07bf635f
RS
114";
115
a7e19a26
PJ
116/* FIXME: Space should toggle toggleable menu item but not remove the menu
117 so you can toggle the next one without entering the menu again. */
118
119/* FIXME: Should ESC close one level of menu structure or the complete menu? */
120
2376c841 121/* FIXME: F10 should enter the menu, the first one in the menu-bar. */
a7e19a26 122
07bf635f 123#define offset(field) XtOffset(XlwMenuWidget, field)
5c520e0a 124static XtResource
07bf635f 125xlwMenuResources[] =
5c520e0a 126{
423a3f59 127#ifdef HAVE_X_I18N
2015c054
JD
128 {XtNfontSet, XtCFontSet, XtRFontSet, sizeof(XFontSet),
129 offset(menu.fontSet), XtRFontSet, NULL},
130#endif
07bf635f 131 {XtNfont, XtCFont, XtRFontStruct, sizeof(XFontStruct *),
423a3f59 132 offset(menu.font), XtRString, "XtDefaultFont"},
07bf635f
RS
133 {XtNforeground, XtCForeground, XtRPixel, sizeof(Pixel),
134 offset(menu.foreground), XtRString, "XtDefaultForeground"},
8b71a9ca
PJ
135 {XtNdisabledForeground, XtCDisabledForeground, XtRPixel, sizeof(Pixel),
136 offset(menu.disabled_foreground), XtRString, (XtPointer)NULL},
07bf635f
RS
137 {XtNbuttonForeground, XtCButtonForeground, XtRPixel, sizeof(Pixel),
138 offset(menu.button_foreground), XtRString, "XtDefaultForeground"},
139 {XtNmargin, XtCMargin, XtRDimension, sizeof(Dimension),
2376c841 140 offset(menu.margin), XtRImmediate, (XtPointer)1},
07bf635f
RS
141 {XtNhorizontalSpacing, XtCMargin, XtRDimension, sizeof(Dimension),
142 offset(menu.horizontal_spacing), XtRImmediate, (XtPointer)3},
143 {XtNverticalSpacing, XtCMargin, XtRDimension, sizeof(Dimension),
2376c841 144 offset(menu.vertical_spacing), XtRImmediate, (XtPointer)2},
07bf635f
RS
145 {XtNarrowSpacing, XtCMargin, XtRDimension, sizeof(Dimension),
146 offset(menu.arrow_spacing), XtRImmediate, (XtPointer)10},
147
d398028f 148 {XmNshadowThickness, XmCShadowThickness, XtRDimension,
07bf635f 149 sizeof (Dimension), offset (menu.shadow_thickness),
2376c841 150 XtRImmediate, (XtPointer)1},
07bf635f
RS
151 {XmNtopShadowColor, XmCTopShadowColor, XtRPixel, sizeof (Pixel),
152 offset (menu.top_shadow_color), XtRImmediate, (XtPointer)-1},
153 {XmNbottomShadowColor, XmCBottomShadowColor, XtRPixel, sizeof (Pixel),
154 offset (menu.bottom_shadow_color), XtRImmediate, (XtPointer)-1},
155 {XmNtopShadowPixmap, XmCTopShadowPixmap, XtRPixmap, sizeof (Pixmap),
156 offset (menu.top_shadow_pixmap), XtRImmediate, (XtPointer)None},
157 {XmNbottomShadowPixmap, XmCBottomShadowPixmap, XtRPixmap, sizeof (Pixmap),
158 offset (menu.bottom_shadow_pixmap), XtRImmediate, (XtPointer)None},
159
5c520e0a 160 {XtNopen, XtCCallback, XtRCallback, sizeof(XtPointer),
07bf635f 161 offset(menu.open), XtRCallback, (XtPointer)NULL},
5c520e0a 162 {XtNselect, XtCCallback, XtRCallback, sizeof(XtPointer),
07bf635f 163 offset(menu.select), XtRCallback, (XtPointer)NULL},
5c520e0a 164 {XtNhighlightCallback, XtCCallback, XtRCallback, sizeof(XtPointer),
d6fd6371 165 offset(menu.highlight), XtRCallback, (XtPointer)NULL},
1ecd5086
JD
166 {XtNenterCallback, XtCCallback, XtRCallback, sizeof(XtPointer),
167 offset(menu.enter), XtRCallback, (XtPointer)NULL},
168 {XtNleaveCallback, XtCCallback, XtRCallback, sizeof(XtPointer),
169 offset(menu.leave), XtRCallback, (XtPointer)NULL},
07bf635f
RS
170 {XtNmenu, XtCMenu, XtRPointer, sizeof(XtPointer),
171 offset(menu.contents), XtRImmediate, (XtPointer)NULL},
172 {XtNcursor, XtCCursor, XtRCursor, sizeof(Cursor),
173 offset(menu.cursor_shape), XtRString, (XtPointer)"right_ptr"},
174 {XtNhorizontal, XtCHorizontal, XtRInt, sizeof(int),
175 offset(menu.horizontal), XtRImmediate, (XtPointer)True},
176};
177#undef offset
178
179static Boolean XlwMenuSetValues();
180static void XlwMenuRealize();
181static void XlwMenuRedisplay();
182static void XlwMenuResize();
183static void XlwMenuInitialize();
184static void XlwMenuRedisplay();
185static void XlwMenuDestroy();
186static void XlwMenuClassInitialize();
187static void Start();
188static void Drag();
a7e19a26
PJ
189static void Down();
190static void Up();
191static void Left();
192static void Right();
07bf635f 193static void Select();
ba624d0f
RS
194static void Key();
195static void Nothing();
8fdc9384 196static int separator_height __P ((enum menu_separator));
244c93fe
JD
197static void pop_up_menu __P ((XlwMenuWidget, XButtonPressedEvent *));
198
07bf635f 199
5c520e0a 200static XtActionsRec
07bf635f
RS
201xlwMenuActionsList [] =
202{
203 {"start", Start},
204 {"drag", Drag},
a7e19a26
PJ
205 {"down", Down},
206 {"up", Up},
207 {"left", Left},
208 {"right", Right},
07bf635f 209 {"select", Select},
ba624d0f 210 {"key", Key},
4220b2a5 211 {"MenuGadgetEscape", Key}, /* Compatibility with Lesstif/Motif. */
ba624d0f 212 {"nothing", Nothing},
07bf635f
RS
213};
214
215#define SuperClass ((CoreWidgetClass)&coreClassRec)
216
217XlwMenuClassRec xlwMenuClassRec =
218{
219 { /* CoreClass fields initialization */
5c520e0a 220 (WidgetClass) SuperClass, /* superclass */
07bf635f
RS
221 "XlwMenu", /* class_name */
222 sizeof(XlwMenuRec), /* size */
223 XlwMenuClassInitialize, /* class_initialize */
224 NULL, /* class_part_initialize */
225 FALSE, /* class_inited */
226 XlwMenuInitialize, /* initialize */
227 NULL, /* initialize_hook */
228 XlwMenuRealize, /* realize */
229 xlwMenuActionsList, /* actions */
230 XtNumber(xlwMenuActionsList), /* num_actions */
231 xlwMenuResources, /* resources */
232 XtNumber(xlwMenuResources), /* resource_count */
233 NULLQUARK, /* xrm_class */
234 TRUE, /* compress_motion */
423a3f59 235 XtExposeCompressMaximal, /* compress_exposure */
07bf635f
RS
236 TRUE, /* compress_enterleave */
237 FALSE, /* visible_interest */
238 XlwMenuDestroy, /* destroy */
239 XlwMenuResize, /* resize */
240 XlwMenuRedisplay, /* expose */
241 XlwMenuSetValues, /* set_values */
242 NULL, /* set_values_hook */
243 XtInheritSetValuesAlmost, /* set_values_almost */
244 NULL, /* get_values_hook */
245 NULL, /* accept_focus */
246 XtVersion, /* version */
247 NULL, /* callback_private */
248 xlwMenuTranslations, /* tm_table */
249 XtInheritQueryGeometry, /* query_geometry */
250 XtInheritDisplayAccelerator, /* display_accelerator */
251 NULL /* extension */
252 }, /* XlwMenuClass fields initialization */
253 {
254 0 /* dummy */
255 },
256};
257
258WidgetClass xlwMenuWidgetClass = (WidgetClass) &xlwMenuClassRec;
259
d398028f
PR
260int submenu_destroyed;
261
005e0d57
JD
262/* For debug, if installation-directory is non-nil this is not an installed
263 Emacs. In that case we do not grab the keyboard to make it easier to
264 debug. */
265#define GRAB_KEYBOARD (EQ (Vinstallation_directory, Qnil))
4db7db7d 266
d398028f
PR
267static int next_release_must_exit;
268
07bf635f 269\f/* Utilities */
be06a3df 270
4db7db7d
JD
271/* Ungrab pointer and keyboard */
272static void
273ungrab_all (w, ungrabtime)
274 Widget w;
275 Time ungrabtime;
276{
277 XtUngrabPointer (w, ungrabtime);
005e0d57 278 if (GRAB_KEYBOARD) XtUngrabKeyboard (w, ungrabtime);
4db7db7d 279}
be06a3df
GM
280
281/* Like abort, but remove grabs from widget W before. */
282
283static void
284abort_gracefully (w)
285 Widget w;
286{
287 if (XtIsShell (XtParent (w)))
288 XtRemoveGrab (w);
4db7db7d 289 ungrab_all (w, CurrentTime);
be06a3df
GM
290 abort ();
291}
292
07bf635f 293static void
d398028f
PR
294push_new_stack (mw, val)
295 XlwMenuWidget mw;
296 widget_value* val;
07bf635f
RS
297{
298 if (!mw->menu.new_stack)
299 {
300 mw->menu.new_stack_length = 10;
301 mw->menu.new_stack =
302 (widget_value**)XtCalloc (mw->menu.new_stack_length,
303 sizeof (widget_value*));
304 }
305 else if (mw->menu.new_depth == mw->menu.new_stack_length)
306 {
307 mw->menu.new_stack_length *= 2;
308 mw->menu.new_stack =
309 (widget_value**)XtRealloc ((char*)mw->menu.new_stack,
310 mw->menu.new_stack_length * sizeof (widget_value*));
311 }
312 mw->menu.new_stack [mw->menu.new_depth++] = val;
313}
314
315static void
d398028f
PR
316pop_new_stack_if_no_contents (mw)
317 XlwMenuWidget mw;
07bf635f 318{
fa74535d 319 if (mw->menu.new_depth > 1)
07bf635f
RS
320 {
321 if (!mw->menu.new_stack [mw->menu.new_depth - 1]->contents)
322 mw->menu.new_depth -= 1;
323 }
324}
325
326static void
d398028f
PR
327make_old_stack_space (mw, n)
328 XlwMenuWidget mw;
329 int n;
07bf635f
RS
330{
331 if (!mw->menu.old_stack)
332 {
333 mw->menu.old_stack_length = 10;
334 mw->menu.old_stack =
335 (widget_value**)XtCalloc (mw->menu.old_stack_length,
336 sizeof (widget_value*));
337 }
338 else if (mw->menu.old_stack_length < n)
339 {
340 mw->menu.old_stack_length *= 2;
341 mw->menu.old_stack =
342 (widget_value**)XtRealloc ((char*)mw->menu.old_stack,
343 mw->menu.old_stack_length * sizeof (widget_value*));
344 }
345}
346
347\f/* Size code */
1f5df1f7 348static int
d398028f
PR
349string_width (mw, s)
350 XlwMenuWidget mw;
351 char *s;
07bf635f
RS
352{
353 XCharStruct xcs;
354 int drop;
2015c054
JD
355#ifdef HAVE_X_I18N
356 XRectangle ink, logical;
357 if (mw->menu.fontSet)
358 {
359 XmbTextExtents (mw->menu.fontSet, s, strlen (s), &ink, &logical);
360 return logical.width;
361 }
362#endif
5c520e0a 363
07bf635f
RS
364 XTextExtents (mw->menu.font, s, strlen (s), &drop, &drop, &drop, &xcs);
365 return xcs.width;
2015c054 366
07bf635f
RS
367}
368
423a3f59
SM
369#ifdef HAVE_X_I18N
370#define MENU_FONT_HEIGHT(mw) \
2015c054
JD
371 ((mw)->menu.fontSet != NULL \
372 ? (mw)->menu.font_extents->max_logical_extent.height \
373 : (mw)->menu.font->ascent + (mw)->menu.font->descent)
15df8e89 374#define MENU_FONT_ASCENT(mw) \
2015c054
JD
375 ((mw)->menu.fontSet != NULL \
376 ? - (mw)->menu.font_extents->max_logical_extent.y \
377 : (mw)->menu.font->ascent)
423a3f59
SM
378#else
379#define MENU_FONT_HEIGHT(mw) \
380 ((mw)->menu.font->ascent + (mw)->menu.font->descent)
381#define MENU_FONT_ASCENT(mw) ((mw)->menu.font->ascent)
382#endif
383
07bf635f 384static int
d398028f
PR
385arrow_width (mw)
386 XlwMenuWidget mw;
07bf635f 387{
423a3f59 388 return (MENU_FONT_ASCENT (mw) * 3/4) | 1;
07bf635f
RS
389}
390
be06a3df
GM
391/* Return the width of toggle buttons of widget MW. */
392
393static int
394toggle_button_width (mw)
395 XlwMenuWidget mw;
396{
423a3f59 397 return (MENU_FONT_HEIGHT (mw) * 2 / 3) | 1;
be06a3df
GM
398}
399
400
401/* Return the width of radio buttons of widget MW. */
402
403static int
404radio_button_width (mw)
405 XlwMenuWidget mw;
406{
407 return toggle_button_width (mw) * 1.41;
408}
409
410
07bf635f
RS
411static XtResource
412nameResource[] =
5c520e0a 413{
07bf635f
RS
414 {"labelString", "LabelString", XtRString, sizeof(String),
415 0, XtRImmediate, 0},
416};
417
418static char*
d398028f
PR
419resource_widget_value (mw, val)
420 XlwMenuWidget mw;
421 widget_value *val;
07bf635f
RS
422{
423 if (!val->toolkit_data)
424 {
425 char* resourced_name = NULL;
426 char* complete_name;
427 XtGetSubresources ((Widget) mw,
428 (XtPointer) &resourced_name,
429 val->name, val->name,
430 nameResource, 1, NULL, 0);
431 if (!resourced_name)
432 resourced_name = val->name;
433 if (!val->value)
d97342f3
RS
434 {
435 complete_name = (char *) XtMalloc (strlen (resourced_name) + 1);
436 strcpy (complete_name, resourced_name);
437 }
07bf635f
RS
438 else
439 {
440 int complete_length =
441 strlen (resourced_name) + strlen (val->value) + 2;
442 complete_name = XtMalloc (complete_length);
443 *complete_name = 0;
444 strcat (complete_name, resourced_name);
445 strcat (complete_name, " ");
446 strcat (complete_name, val->value);
447 }
448
449 val->toolkit_data = complete_name;
450 val->free_toolkit_data = True;
451 }
452 return (char*)val->toolkit_data;
453}
454
455/* Returns the sizes of an item */
456static void
be06a3df
GM
457size_menu_item (mw, val, horizontal_p, label_width, rest_width, button_width,
458 height)
d398028f
PR
459 XlwMenuWidget mw;
460 widget_value* val;
461 int horizontal_p;
462 int* label_width;
463 int* rest_width;
be06a3df 464 int* button_width;
d398028f 465 int* height;
07bf635f 466{
be06a3df 467 enum menu_separator separator;
5c520e0a 468
be06a3df 469 if (lw_separator_p (val->name, &separator, 0))
07bf635f 470 {
be06a3df 471 *height = separator_height (separator);
07bf635f
RS
472 *label_width = 1;
473 *rest_width = 0;
be06a3df 474 *button_width = 0;
07bf635f
RS
475 }
476 else
477 {
423a3f59
SM
478 *height = MENU_FONT_HEIGHT (mw)
479 + 2 * mw->menu.vertical_spacing + 2 * mw->menu.shadow_thickness;
5c520e0a 480
07bf635f
RS
481 *label_width =
482 string_width (mw, resource_widget_value (mw, val))
483 + mw->menu.horizontal_spacing + mw->menu.shadow_thickness;
5c520e0a 484
07bf635f
RS
485 *rest_width = mw->menu.horizontal_spacing + mw->menu.shadow_thickness;
486 if (!horizontal_p)
487 {
488 if (val->contents)
be06a3df 489 /* Add width of the arrow displayed for submenus. */
07bf635f
RS
490 *rest_width += arrow_width (mw) + mw->menu.arrow_spacing;
491 else if (val->key)
be06a3df
GM
492 /* Add width of key equivalent string. */
493 *rest_width += (string_width (mw, val->key)
494 + mw->menu.arrow_spacing);
495
496 if (val->button_type == BUTTON_TYPE_TOGGLE)
497 *button_width = (toggle_button_width (mw)
498 + mw->menu.horizontal_spacing);
499 else if (val->button_type == BUTTON_TYPE_RADIO)
500 *button_width = (radio_button_width (mw)
501 + mw->menu.horizontal_spacing);
07bf635f
RS
502 }
503 }
504}
505
506static void
d398028f
PR
507size_menu (mw, level)
508 XlwMenuWidget mw;
509 int level;
07bf635f 510{
ccf589a6 511 unsigned int label_width = 0;
07bf635f 512 int rest_width = 0;
be06a3df 513 int button_width = 0;
07bf635f 514 int max_rest_width = 0;
be06a3df 515 int max_button_width = 0;
ccf589a6 516 unsigned int height = 0;
07bf635f
RS
517 int horizontal_p = mw->menu.horizontal && (level == 0);
518 widget_value* val;
519 window_state* ws;
520
521 if (level >= mw->menu.old_depth)
be06a3df 522 abort_gracefully ((Widget) mw);
07bf635f 523
5c520e0a 524 ws = &mw->menu.windows [level];
07bf635f
RS
525 ws->width = 0;
526 ws->height = 0;
527 ws->label_width = 0;
be06a3df 528 ws->button_width = 0;
07bf635f
RS
529
530 for (val = mw->menu.old_stack [level]->contents; val; val = val->next)
531 {
532 size_menu_item (mw, val, horizontal_p, &label_width, &rest_width,
be06a3df 533 &button_width, &height);
07bf635f
RS
534 if (horizontal_p)
535 {
536 ws->width += label_width + rest_width;
537 if (height > ws->height)
538 ws->height = height;
539 }
540 else
541 {
542 if (label_width > ws->label_width)
543 ws->label_width = label_width;
544 if (rest_width > max_rest_width)
545 max_rest_width = rest_width;
be06a3df
GM
546 if (button_width > max_button_width)
547 max_button_width = button_width;
07bf635f
RS
548 ws->height += height;
549 }
550 }
5c520e0a 551
07bf635f 552 if (horizontal_p)
be06a3df 553 ws->label_width = ws->button_width = 0;
07bf635f 554 else
be06a3df
GM
555 {
556 ws->width = ws->label_width + max_rest_width + max_button_width;
557 ws->button_width = max_button_width;
558 }
07bf635f
RS
559
560 ws->width += 2 * mw->menu.shadow_thickness;
561 ws->height += 2 * mw->menu.shadow_thickness;
be06a3df
GM
562
563 if (horizontal_p)
564 {
565 ws->width += 2 * mw->menu.margin;
566 ws->height += 2 * mw->menu.margin;
567 }
07bf635f
RS
568}
569
570
571\f/* Display code */
be06a3df 572
07bf635f 573static void
be06a3df 574draw_arrow (mw, window, gc, x, y, width, down_p)
d398028f
PR
575 XlwMenuWidget mw;
576 Window window;
577 GC gc;
578 int x;
579 int y;
580 int width;
be06a3df 581 int down_p;
07bf635f 582{
be06a3df
GM
583 Display *dpy = XtDisplay (mw);
584 GC top_gc = mw->menu.shadow_top_gc;
585 GC bottom_gc = mw->menu.shadow_bottom_gc;
586 int thickness = mw->menu.shadow_thickness;
587 int height = width;
588 XPoint pt[10];
589 /* alpha = atan (0.5)
590 factor = (1 + sin (alpha)) / cos (alpha) */
591 double factor = 1.62;
592 int thickness2 = thickness * factor;
5c520e0a 593
423a3f59 594 y += (MENU_FONT_HEIGHT (mw) - height) / 2;
5c520e0a 595
be06a3df
GM
596 if (down_p)
597 {
598 GC temp;
599 temp = top_gc;
600 top_gc = bottom_gc;
601 bottom_gc = temp;
602 }
603
604 pt[0].x = x;
605 pt[0].y = y + height;
606 pt[1].x = x + thickness;
607 pt[1].y = y + height - thickness2;
608 pt[2].x = x + thickness2;
609 pt[2].y = y + thickness2;
610 pt[3].x = x;
611 pt[3].y = y;
612 XFillPolygon (dpy, window, top_gc, pt, 4, Convex, CoordModeOrigin);
5c520e0a 613
be06a3df
GM
614 pt[0].x = x;
615 pt[0].y = y;
616 pt[1].x = x + thickness;
617 pt[1].y = y + thickness2;
618 pt[2].x = x + width - thickness2;
619 pt[2].y = y + height / 2;
620 pt[3].x = x + width;
621 pt[3].y = y + height / 2;
622 XFillPolygon (dpy, window, top_gc, pt, 4, Convex, CoordModeOrigin);
5c520e0a 623
be06a3df
GM
624 pt[0].x = x;
625 pt[0].y = y + height;
626 pt[1].x = x + thickness;
627 pt[1].y = y + height - thickness2;
628 pt[2].x = x + width - thickness2;
629 pt[2].y = y + height / 2;
630 pt[3].x = x + width;
631 pt[3].y = y + height / 2;
632 XFillPolygon (dpy, window, bottom_gc, pt, 4, Convex, CoordModeOrigin);
07bf635f
RS
633}
634
be06a3df
GM
635
636
07bf635f 637static void
be06a3df 638draw_shadow_rectangle (mw, window, x, y, width, height, erase_p, down_p)
d398028f
PR
639 XlwMenuWidget mw;
640 Window window;
641 int x;
642 int y;
643 int width;
644 int height;
645 int erase_p;
be06a3df 646 int down_p;
07bf635f
RS
647{
648 Display *dpy = XtDisplay (mw);
649 GC top_gc = !erase_p ? mw->menu.shadow_top_gc : mw->menu.background_gc;
650 GC bottom_gc = !erase_p ? mw->menu.shadow_bottom_gc : mw->menu.background_gc;
651 int thickness = mw->menu.shadow_thickness;
652 XPoint points [4];
be06a3df
GM
653
654 if (!erase_p && down_p)
655 {
656 GC temp;
657 temp = top_gc;
658 top_gc = bottom_gc;
659 bottom_gc = temp;
660 }
5c520e0a 661
07bf635f
RS
662 points [0].x = x;
663 points [0].y = y;
664 points [1].x = x + width;
665 points [1].y = y;
666 points [2].x = x + width - thickness;
667 points [2].y = y + thickness;
668 points [3].x = x;
669 points [3].y = y + thickness;
670 XFillPolygon (dpy, window, top_gc, points, 4, Convex, CoordModeOrigin);
671 points [0].x = x;
672 points [0].y = y + thickness;
673 points [1].x = x;
674 points [1].y = y + height;
675 points [2].x = x + thickness;
676 points [2].y = y + height - thickness;
677 points [3].x = x + thickness;
678 points [3].y = y + thickness;
679 XFillPolygon (dpy, window, top_gc, points, 4, Convex, CoordModeOrigin);
680 points [0].x = x + width;
681 points [0].y = y;
682 points [1].x = x + width - thickness;
683 points [1].y = y + thickness;
684 points [2].x = x + width - thickness;
685 points [2].y = y + height - thickness;
686 points [3].x = x + width;
687 points [3].y = y + height - thickness;
688 XFillPolygon (dpy, window, bottom_gc, points, 4, Convex, CoordModeOrigin);
689 points [0].x = x;
690 points [0].y = y + height;
691 points [1].x = x + width;
692 points [1].y = y + height;
693 points [2].x = x + width;
694 points [2].y = y + height - thickness;
695 points [3].x = x + thickness;
696 points [3].y = y + height - thickness;
697 XFillPolygon (dpy, window, bottom_gc, points, 4, Convex, CoordModeOrigin);
698}
699
700
be06a3df
GM
701static void
702draw_shadow_rhombus (mw, window, x, y, width, height, erase_p, down_p)
703 XlwMenuWidget mw;
704 Window window;
705 int x;
706 int y;
707 int width;
708 int height;
709 int erase_p;
710 int down_p;
711{
712 Display *dpy = XtDisplay (mw);
713 GC top_gc = !erase_p ? mw->menu.shadow_top_gc : mw->menu.background_gc;
714 GC bottom_gc = !erase_p ? mw->menu.shadow_bottom_gc : mw->menu.background_gc;
715 int thickness = mw->menu.shadow_thickness;
716 XPoint points [4];
be06a3df
GM
717
718 if (!erase_p && down_p)
719 {
720 GC temp;
721 temp = top_gc;
722 top_gc = bottom_gc;
723 bottom_gc = temp;
724 }
725
726 points [0].x = x;
727 points [0].y = y + height / 2;
728 points [1].x = x + thickness;
729 points [1].y = y + height / 2;
730 points [2].x = x + width / 2;
731 points [2].y = y + thickness;
732 points [3].x = x + width / 2;
733 points [3].y = y;
734 XFillPolygon (dpy, window, top_gc, points, 4, Convex, CoordModeOrigin);
735 points [0].x = x + width / 2;
736 points [0].y = y;
737 points [1].x = x + width / 2;
738 points [1].y = y + thickness;
739 points [2].x = x + width - thickness;
740 points [2].y = y + height / 2;
741 points [3].x = x + width;
742 points [3].y = y + height / 2;
743 XFillPolygon (dpy, window, top_gc, points, 4, Convex, CoordModeOrigin);
744 points [0].x = x;
745 points [0].y = y + height / 2;
746 points [1].x = x + thickness;
747 points [1].y = y + height / 2;
748 points [2].x = x + width / 2;
749 points [2].y = y + height - thickness;
750 points [3].x = x + width / 2;
751 points [3].y = y + height;
752 XFillPolygon (dpy, window, bottom_gc, points, 4, Convex, CoordModeOrigin);
753 points [0].x = x + width / 2;
754 points [0].y = y + height;
755 points [1].x = x + width / 2;
756 points [1].y = y + height - thickness;
757 points [2].x = x + width - thickness;
758 points [2].y = y + height / 2;
759 points [3].x = x + width;
760 points [3].y = y + height / 2;
761 XFillPolygon (dpy, window, bottom_gc, points, 4, Convex, CoordModeOrigin);
762}
763
764
765/* Draw a toggle button on widget MW, X window WINDOW. X/Y is the
766 top-left corner of the menu item. SELECTED_P non-zero means the
767 toggle button is selected. */
768
769static void
770draw_toggle (mw, window, x, y, selected_p)
771 XlwMenuWidget mw;
772 Window window;
773 int x, y, selected_p;
774{
775 int width, height;
776
777 width = toggle_button_width (mw);
778 height = width;
779 x += mw->menu.horizontal_spacing;
423a3f59 780 y += (MENU_FONT_ASCENT (mw) - height) / 2;
be06a3df
GM
781 draw_shadow_rectangle (mw, window, x, y, width, height, False, selected_p);
782}
783
784
785/* Draw a radio button on widget MW, X window WINDOW. X/Y is the
786 top-left corner of the menu item. SELECTED_P non-zero means the
787 toggle button is selected. */
788
789static void
790draw_radio (mw, window, x, y, selected_p)
791 XlwMenuWidget mw;
792 Window window;
793 int x, y, selected_p;
794{
795 int width, height;
796
797 width = radio_button_width (mw);
798 height = width;
799 x += mw->menu.horizontal_spacing;
423a3f59 800 y += (MENU_FONT_ASCENT (mw) - height) / 2;
be06a3df
GM
801 draw_shadow_rhombus (mw, window, x, y, width, height, False, selected_p);
802}
803
804
805/* Draw a menu separator on widget MW, X window WINDOW. X/Y is the
806 top-left corner of the menu item. WIDTH is the width of the
807 separator to draw. TYPE is the separator type. */
808
809static void
810draw_separator (mw, window, x, y, width, type)
811 XlwMenuWidget mw;
812 Window window;
813 int x, y, width;
814 enum menu_separator type;
815{
816 Display *dpy = XtDisplay (mw);
817 XGCValues xgcv;
818
819 switch (type)
820 {
821 case SEPARATOR_NO_LINE:
822 break;
5c520e0a 823
be06a3df
GM
824 case SEPARATOR_SINGLE_LINE:
825 XDrawLine (dpy, window, mw->menu.foreground_gc,
826 x, y, x + width, y);
827 break;
5c520e0a 828
be06a3df
GM
829 case SEPARATOR_DOUBLE_LINE:
830 draw_separator (mw, window, x, y, width, SEPARATOR_SINGLE_LINE);
831 draw_separator (mw, window, x, y + 2, width, SEPARATOR_SINGLE_LINE);
832 break;
5c520e0a 833
be06a3df
GM
834 case SEPARATOR_SINGLE_DASHED_LINE:
835 xgcv.line_style = LineOnOffDash;
836 XChangeGC (dpy, mw->menu.foreground_gc, GCLineStyle, &xgcv);
837 XDrawLine (dpy, window, mw->menu.foreground_gc,
838 x, y, x + width, y);
839 xgcv.line_style = LineSolid;
840 XChangeGC (dpy, mw->menu.foreground_gc, GCLineStyle, &xgcv);
841 break;
5c520e0a 842
be06a3df
GM
843 case SEPARATOR_DOUBLE_DASHED_LINE:
844 draw_separator (mw, window, x, y, width,
845 SEPARATOR_SINGLE_DASHED_LINE);
846 draw_separator (mw, window, x, y + 2, width,
847 SEPARATOR_SINGLE_DASHED_LINE);
848 break;
5c520e0a 849
be06a3df
GM
850 case SEPARATOR_SHADOW_ETCHED_IN:
851 XDrawLine (dpy, window, mw->menu.shadow_bottom_gc,
852 x, y, x + width, y);
853 XDrawLine (dpy, window, mw->menu.shadow_top_gc,
854 x, y + 1, x + width, y + 1);
855 break;
856
857 case SEPARATOR_SHADOW_ETCHED_OUT:
858 XDrawLine (dpy, window, mw->menu.shadow_top_gc,
859 x, y, x + width, y);
860 XDrawLine (dpy, window, mw->menu.shadow_bottom_gc,
861 x, y + 1, x + width, y + 1);
862 break;
5c520e0a 863
be06a3df
GM
864 case SEPARATOR_SHADOW_ETCHED_IN_DASH:
865 xgcv.line_style = LineOnOffDash;
866 XChangeGC (dpy, mw->menu.shadow_bottom_gc, GCLineStyle, &xgcv);
867 XChangeGC (dpy, mw->menu.shadow_top_gc, GCLineStyle, &xgcv);
2f90328f 868 draw_separator (mw, window, x, y, width, SEPARATOR_SHADOW_ETCHED_IN);
be06a3df
GM
869 xgcv.line_style = LineSolid;
870 XChangeGC (dpy, mw->menu.shadow_bottom_gc, GCLineStyle, &xgcv);
871 XChangeGC (dpy, mw->menu.shadow_top_gc, GCLineStyle, &xgcv);
872 break;
5c520e0a 873
be06a3df
GM
874 case SEPARATOR_SHADOW_ETCHED_OUT_DASH:
875 xgcv.line_style = LineOnOffDash;
876 XChangeGC (dpy, mw->menu.shadow_bottom_gc, GCLineStyle, &xgcv);
877 XChangeGC (dpy, mw->menu.shadow_top_gc, GCLineStyle, &xgcv);
2f90328f 878 draw_separator (mw, window, x, y, width, SEPARATOR_SHADOW_ETCHED_OUT);
be06a3df
GM
879 xgcv.line_style = LineSolid;
880 XChangeGC (dpy, mw->menu.shadow_bottom_gc, GCLineStyle, &xgcv);
881 XChangeGC (dpy, mw->menu.shadow_top_gc, GCLineStyle, &xgcv);
882 break;
883
884 case SEPARATOR_SHADOW_DOUBLE_ETCHED_IN:
885 draw_separator (mw, window, x, y, width, SEPARATOR_SHADOW_ETCHED_IN);
886 draw_separator (mw, window, x, y + 3, width, SEPARATOR_SHADOW_ETCHED_IN);
887 break;
5c520e0a 888
be06a3df
GM
889 case SEPARATOR_SHADOW_DOUBLE_ETCHED_OUT:
890 draw_separator (mw, window, x, y, width,
891 SEPARATOR_SHADOW_ETCHED_OUT);
892 draw_separator (mw, window, x, y + 3, width,
893 SEPARATOR_SHADOW_ETCHED_OUT);
894 break;
5c520e0a 895
be06a3df
GM
896 case SEPARATOR_SHADOW_DOUBLE_ETCHED_IN_DASH:
897 xgcv.line_style = LineOnOffDash;
898 XChangeGC (dpy, mw->menu.shadow_bottom_gc, GCLineStyle, &xgcv);
899 XChangeGC (dpy, mw->menu.shadow_top_gc, GCLineStyle, &xgcv);
900 draw_separator (mw, window, x, y, width,
901 SEPARATOR_SHADOW_DOUBLE_ETCHED_IN);
902 xgcv.line_style = LineSolid;
903 XChangeGC (dpy, mw->menu.shadow_bottom_gc, GCLineStyle, &xgcv);
904 XChangeGC (dpy, mw->menu.shadow_top_gc, GCLineStyle, &xgcv);
905 break;
906
907 case SEPARATOR_SHADOW_DOUBLE_ETCHED_OUT_DASH:
908 xgcv.line_style = LineOnOffDash;
909 XChangeGC (dpy, mw->menu.shadow_bottom_gc, GCLineStyle, &xgcv);
910 XChangeGC (dpy, mw->menu.shadow_top_gc, GCLineStyle, &xgcv);
911 draw_separator (mw, window, x, y, width,
912 SEPARATOR_SHADOW_DOUBLE_ETCHED_OUT);
913 xgcv.line_style = LineSolid;
914 XChangeGC (dpy, mw->menu.shadow_bottom_gc, GCLineStyle, &xgcv);
915 XChangeGC (dpy, mw->menu.shadow_top_gc, GCLineStyle, &xgcv);
916 break;
917
918 default:
919 abort ();
920 }
921}
922
923
924/* Return the pixel height of menu separator SEPARATOR. */
925
926static int
927separator_height (separator)
928 enum menu_separator separator;
929{
930 switch (separator)
931 {
932 case SEPARATOR_NO_LINE:
933 return 2;
5c520e0a 934
be06a3df
GM
935 case SEPARATOR_SINGLE_LINE:
936 case SEPARATOR_SINGLE_DASHED_LINE:
937 return 1;
5c520e0a 938
be06a3df
GM
939 case SEPARATOR_DOUBLE_LINE:
940 case SEPARATOR_DOUBLE_DASHED_LINE:
941 return 3;
5c520e0a 942
be06a3df
GM
943 case SEPARATOR_SHADOW_ETCHED_IN:
944 case SEPARATOR_SHADOW_ETCHED_OUT:
945 case SEPARATOR_SHADOW_ETCHED_IN_DASH:
946 case SEPARATOR_SHADOW_ETCHED_OUT_DASH:
947 return 2;
5c520e0a 948
be06a3df
GM
949 case SEPARATOR_SHADOW_DOUBLE_ETCHED_IN:
950 case SEPARATOR_SHADOW_DOUBLE_ETCHED_OUT:
951 case SEPARATOR_SHADOW_DOUBLE_ETCHED_IN_DASH:
952 case SEPARATOR_SHADOW_DOUBLE_ETCHED_OUT_DASH:
953 return 5;
5c520e0a 954
be06a3df
GM
955 default:
956 abort ();
957 }
958}
959
960
07bf635f 961/* Display the menu item and increment where.x and where.y to show how large
be06a3df
GM
962 the menu item was. */
963
07bf635f 964static void
be06a3df
GM
965display_menu_item (mw, val, ws, where, highlighted_p, horizontal_p,
966 just_compute_p)
d398028f
PR
967 XlwMenuWidget mw;
968 widget_value* val;
969 window_state* ws;
970 XPoint* where;
971 Boolean highlighted_p;
972 Boolean horizontal_p;
973 Boolean just_compute_p;
07bf635f
RS
974{
975 GC deco_gc;
976 GC text_gc;
423a3f59
SM
977 int font_height = MENU_FONT_HEIGHT (mw);
978 int font_ascent = MENU_FONT_ASCENT (mw);
07bf635f 979 int shadow = mw->menu.shadow_thickness;
be06a3df 980 int margin = mw->menu.margin;
07bf635f
RS
981 int h_spacing = mw->menu.horizontal_spacing;
982 int v_spacing = mw->menu.vertical_spacing;
983 int label_width;
984 int rest_width;
be06a3df 985 int button_width;
07bf635f
RS
986 int height;
987 int width;
be06a3df
GM
988 enum menu_separator separator;
989 int separator_p = lw_separator_p (val->name, &separator, 0);
07bf635f
RS
990
991 /* compute the sizes of the item */
be06a3df
GM
992 size_menu_item (mw, val, horizontal_p, &label_width, &rest_width,
993 &button_width, &height);
07bf635f
RS
994
995 if (horizontal_p)
996 width = label_width + rest_width;
997 else
998 {
999 label_width = ws->label_width;
1000 width = ws->width - 2 * shadow;
1001 }
1002
07bf635f
RS
1003 /* Only highlight an enabled item that has a callback. */
1004 if (highlighted_p)
1005 if (!val->enabled || !(val->call_data || val->contents))
1006 highlighted_p = 0;
1007
1008 /* do the drawing. */
1009 if (!just_compute_p)
1010 {
1011 /* Add the shadow border of the containing menu */
1012 int x = where->x + shadow;
1013 int y = where->y + shadow;
1014
be06a3df
GM
1015 if (horizontal_p)
1016 {
1017 x += margin;
1018 y += margin;
1019 }
1020
07bf635f
RS
1021 /* pick the foreground and background GC. */
1022 if (val->enabled)
be06a3df 1023 text_gc = mw->menu.foreground_gc;
07bf635f 1024 else
8b71a9ca 1025 text_gc = mw->menu.disabled_gc;
07bf635f
RS
1026 deco_gc = mw->menu.foreground_gc;
1027
1028 if (separator_p)
1029 {
be06a3df 1030 draw_separator (mw, ws->window, x, y, width, separator);
07bf635f 1031 }
5c520e0a 1032 else
07bf635f 1033 {
84593cae 1034 int x_offset = x + h_spacing + shadow;
07bf635f 1035 char* display_string = resource_widget_value (mw, val);
be06a3df
GM
1036 draw_shadow_rectangle (mw, ws->window, x, y, width, height, True,
1037 False);
84593cae
PR
1038
1039 /* Deal with centering a menu title. */
1040 if (!horizontal_p && !val->contents && !val->call_data)
1041 {
1042 int l = string_width (mw, display_string);
1043
1044 if (width > l)
1045 x_offset = (width - l) >> 1;
1046 }
be06a3df
GM
1047 else if (!horizontal_p && ws->button_width)
1048 x_offset += ws->button_width;
5c520e0a
SS
1049
1050
423a3f59 1051#ifdef HAVE_X_I18N
2015c054
JD
1052 if (mw->menu.fontSet)
1053 XmbDrawString (XtDisplay (mw), ws->window, mw->menu.fontSet,
1054 text_gc, x_offset,
1055 y + v_spacing + shadow + font_ascent,
1056 display_string, strlen (display_string));
1057 else
423a3f59 1058#endif
2015c054 1059 XDrawString (XtDisplay (mw), ws->window,
423a3f59 1060 text_gc, x_offset,
07bf635f
RS
1061 y + v_spacing + shadow + font_ascent,
1062 display_string, strlen (display_string));
5c520e0a 1063
07bf635f
RS
1064 if (!horizontal_p)
1065 {
be06a3df
GM
1066 if (val->button_type == BUTTON_TYPE_TOGGLE)
1067 draw_toggle (mw, ws->window, x, y + v_spacing + shadow,
1068 val->selected);
1069 else if (val->button_type == BUTTON_TYPE_RADIO)
1070 draw_radio (mw, ws->window, x, y + v_spacing + shadow,
1071 val->selected);
5c520e0a 1072
07bf635f
RS
1073 if (val->contents)
1074 {
1075 int a_w = arrow_width (mw);
1076 draw_arrow (mw, ws->window, deco_gc,
be06a3df 1077 x + width - a_w
5c520e0a 1078 - mw->menu.horizontal_spacing
d398028f 1079 - mw->menu.shadow_thickness,
be06a3df
GM
1080 y + v_spacing + shadow, a_w,
1081 highlighted_p);
07bf635f
RS
1082 }
1083 else if (val->key)
1084 {
423a3f59 1085#ifdef HAVE_X_I18N
2015c054
JD
1086 if (mw->menu.fontSet)
1087 XmbDrawString (XtDisplay (mw), ws->window,
1088 mw->menu.fontSet,
1089 text_gc,
1090 x + label_width + mw->menu.arrow_spacing,
1091 y + v_spacing + shadow + font_ascent,
1092 val->key, strlen (val->key));
1093 else
423a3f59 1094#endif
2015c054 1095 XDrawString (XtDisplay (mw), ws->window,
423a3f59 1096 text_gc,
07bf635f
RS
1097 x + label_width + mw->menu.arrow_spacing,
1098 y + v_spacing + shadow + font_ascent,
1099 val->key, strlen (val->key));
1100 }
1101 }
d398028f
PR
1102 else
1103 {
5c520e0a 1104 XDrawRectangle (XtDisplay (mw), ws->window,
d398028f
PR
1105 mw->menu.background_gc,
1106 x + shadow, y + shadow,
1107 label_width + h_spacing - 1,
423a3f59 1108 font_height + 2 * v_spacing - 1);
d398028f 1109 draw_shadow_rectangle (mw, ws->window, x, y, width, height,
be06a3df 1110 True, False);
d398028f 1111 }
07bf635f
RS
1112
1113 if (highlighted_p)
be06a3df
GM
1114 draw_shadow_rectangle (mw, ws->window, x, y, width, height, False,
1115 False);
07bf635f
RS
1116 }
1117 }
5c520e0a 1118
07bf635f
RS
1119 where->x += width;
1120 where->y += height;
1121}
1122
1123static void
d398028f
PR
1124display_menu (mw, level, just_compute_p, highlighted_pos, hit, hit_return,
1125 this, that)
1126 XlwMenuWidget mw;
1127 int level;
1128 Boolean just_compute_p;
1129 XPoint* highlighted_pos;
1130 XPoint* hit;
1131 widget_value** hit_return;
1132 widget_value* this;
1133 widget_value* that;
07bf635f
RS
1134{
1135 widget_value* val;
1136 widget_value* following_item;
1137 window_state* ws;
1138 XPoint where;
1139 int horizontal_p = mw->menu.horizontal && (level == 0);
1140 int highlighted_p;
1141 int just_compute_this_one_p;
ccc1cac0
RS
1142 /* This is set nonzero if the element containing HIGHLIGHTED_POS
1143 is disabled, so that we do not return any subsequent element either. */
1144 int no_return = 0;
be06a3df 1145 enum menu_separator separator;
07bf635f
RS
1146
1147 if (level >= mw->menu.old_depth)
be06a3df 1148 abort_gracefully ((Widget) mw);
07bf635f
RS
1149
1150 if (level < mw->menu.old_depth - 1)
1151 following_item = mw->menu.old_stack [level + 1];
5c520e0a 1152 else
07bf635f
RS
1153 following_item = NULL;
1154
1155 if (hit)
1156 *hit_return = NULL;
1157
1158 where.x = 0;
1159 where.y = 0;
1160
1161 ws = &mw->menu.windows [level];
1162 for (val = mw->menu.old_stack [level]->contents; val; val = val->next)
1163 {
1164 highlighted_p = val == following_item;
1165 if (highlighted_p && highlighted_pos)
1166 {
1167 if (horizontal_p)
1168 highlighted_pos->x = where.x;
1169 else
1170 highlighted_pos->y = where.y;
1171 }
5c520e0a 1172
07bf635f
RS
1173 just_compute_this_one_p =
1174 just_compute_p || ((this || that) && val != this && val != that);
1175
1176 display_menu_item (mw, val, ws, &where, highlighted_p, horizontal_p,
1177 just_compute_this_one_p);
1178
1179 if (highlighted_p && highlighted_pos)
1180 {
1181 if (horizontal_p)
1182 highlighted_pos->y = where.y;
1183 else
1184 highlighted_pos->x = where.x;
1185 }
1186
1187 if (hit
1188 && !*hit_return
1189 && (horizontal_p ? hit->x < where.x : hit->y < where.y)
be06a3df 1190 && !lw_separator_p (val->name, &separator, 0)
ccc1cac0
RS
1191 && !no_return)
1192 {
1193 if (val->enabled)
1194 *hit_return = val;
1ecd5086
JD
1195 else
1196 no_return = 1;
1197 if (mw->menu.inside_entry != val)
1198 {
1199 if (mw->menu.inside_entry)
1200 XtCallCallbackList ((Widget)mw, mw->menu.leave,
1201 (XtPointer) mw->menu.inside_entry);
1202 mw->menu.inside_entry = val;
1203 XtCallCallbackList ((Widget)mw, mw->menu.enter,
1204 (XtPointer) mw->menu.inside_entry);
1205 }
ccc1cac0 1206 }
07bf635f
RS
1207
1208 if (horizontal_p)
1209 where.y = 0;
1210 else
1211 where.x = 0;
1212 }
5c520e0a 1213
07bf635f 1214 if (!just_compute_p)
be06a3df
GM
1215 draw_shadow_rectangle (mw, ws->window, 0, 0, ws->width, ws->height,
1216 False, False);
07bf635f
RS
1217}
1218
1219\f/* Motion code */
1220static void
d398028f
PR
1221set_new_state (mw, val, level)
1222 XlwMenuWidget mw;
1223 widget_value* val;
1224 int level;
07bf635f
RS
1225{
1226 int i;
5c520e0a 1227
07bf635f
RS
1228 mw->menu.new_depth = 0;
1229 for (i = 0; i < level; i++)
1230 push_new_stack (mw, mw->menu.old_stack [i]);
1231 push_new_stack (mw, val);
1232}
1233
1234static void
d398028f
PR
1235make_windows_if_needed (mw, n)
1236 XlwMenuWidget mw;
1237 int n;
07bf635f
RS
1238{
1239 int i;
1240 int start_at;
1241 XSetWindowAttributes xswa;
1242 int mask;
1243 Window root = RootWindowOfScreen (DefaultScreenOfDisplay (XtDisplay (mw)));
1244 window_state* windows;
5c520e0a 1245
07bf635f
RS
1246 if (mw->menu.windows_length >= n)
1247 return;
1248
1249 xswa.save_under = True;
1250 xswa.override_redirect = True;
1251 xswa.background_pixel = mw->core.background_pixel;
1252 xswa.border_pixel = mw->core.border_pixel;
1253 xswa.event_mask =
d398028f 1254 ExposureMask | PointerMotionMask | PointerMotionHintMask
07bf635f
RS
1255 | ButtonReleaseMask | ButtonPressMask;
1256 xswa.cursor = mw->menu.cursor_shape;
1257 mask = CWSaveUnder | CWOverrideRedirect | CWBackPixel | CWBorderPixel
1258 | CWEventMask | CWCursor;
5c520e0a 1259
07bf635f
RS
1260 if (!mw->menu.windows)
1261 {
1262 mw->menu.windows =
1263 (window_state*)XtMalloc (n * sizeof (window_state));
1264 start_at = 0;
1265 }
1266 else
1267 {
1268 mw->menu.windows =
1269 (window_state*)XtRealloc ((char*)mw->menu.windows,
1270 n * sizeof (window_state));
1271 start_at = mw->menu.windows_length;
1272 }
1273 mw->menu.windows_length = n;
1274
1275 windows = mw->menu.windows;
1276
1277 for (i = start_at; i < n; i++)
1278 {
1279 windows [i].x = 0;
1280 windows [i].y = 0;
1281 windows [i].width = 1;
1282 windows [i].height = 1;
1283 windows [i].window =
1284 XCreateWindow (XtDisplay (mw), root, 0, 0, 1, 1,
1285 0, 0, CopyFromParent, CopyFromParent, mask, &xswa);
1286 }
1287}
1288
2a692ba4
GM
1289/* Value is non-zero if WINDOW is part of menu bar widget W. */
1290
1291int
1292xlwmenu_window_p (w, window)
1293 Widget w;
1294 Window window;
1295{
1296 XlwMenuWidget mw = (XlwMenuWidget) w;
1297 int i;
177c0ea7 1298
2a692ba4
GM
1299 for (i = 0; i < mw->menu.windows_length; ++i)
1300 if (window == mw->menu.windows[i].window)
1301 break;
1302
1303 return i < mw->menu.windows_length;
1304}
1305
07bf635f
RS
1306/* Make the window fit in the screen */
1307static void
d398028f
PR
1308fit_to_screen (mw, ws, previous_ws, horizontal_p)
1309 XlwMenuWidget mw;
1310 window_state* ws;
1311 window_state* previous_ws;
1312 Boolean horizontal_p;
07bf635f 1313{
ccf589a6
RS
1314 unsigned int screen_width = WidthOfScreen (XtScreen (mw));
1315 unsigned int screen_height = HeightOfScreen (XtScreen (mw));
1ba46f7d
RS
1316 /* 1 if we are unable to avoid an overlap between
1317 this menu and the parent menu in the X dimension. */
1318 int horizontal_overlap = 0;
07bf635f
RS
1319
1320 if (ws->x < 0)
1321 ws->x = 0;
1322 else if (ws->x + ws->width > screen_width)
1323 {
1324 if (!horizontal_p)
b7fa4a06
MB
1325 /* The addition of shadow-thickness for a sub-menu's position is
1326 to reflect a similar adjustment when the menu is displayed to
1327 the right of the invoking menu-item; it makes the sub-menu
1328 look more `attached' to the menu-item. */
1329 ws->x = previous_ws->x - ws->width + mw->menu.shadow_thickness;
07bf635f
RS
1330 else
1331 ws->x = screen_width - ws->width;
525b8232 1332 if (ws->x < 0)
1ba46f7d
RS
1333 {
1334 ws->x = 0;
1335 horizontal_overlap = 1;
1336 }
1337 }
1338 /* If we overlap in X, try to avoid overlap in Y. */
1339 if (horizontal_overlap
1340 && ws->y < previous_ws->y + previous_ws->height
1341 && previous_ws->y < ws->y + ws->height)
1342 {
1343 /* Put this menu right below or right above PREVIOUS_WS
1344 if there's room. */
1345 if (previous_ws->y + previous_ws->height + ws->height < screen_height)
1346 ws->y = previous_ws->y + previous_ws->height;
1347 else if (previous_ws->y - ws->height > 0)
1348 ws->y = previous_ws->y - ws->height;
07bf635f 1349 }
1ba46f7d 1350
07bf635f
RS
1351 if (ws->y < 0)
1352 ws->y = 0;
1353 else if (ws->y + ws->height > screen_height)
1354 {
1355 if (horizontal_p)
1356 ws->y = previous_ws->y - ws->height;
1357 else
1358 ws->y = screen_height - ws->height;
5c520e0a 1359 if (ws->y < 0)
525b8232 1360 ws->y = 0;
07bf635f
RS
1361 }
1362}
1363
1364/* Updates old_stack from new_stack and redisplays. */
1365static void
d398028f
PR
1366remap_menubar (mw)
1367 XlwMenuWidget mw;
07bf635f
RS
1368{
1369 int i;
1370 int last_same;
1371 XPoint selection_position;
1372 int old_depth = mw->menu.old_depth;
1373 int new_depth = mw->menu.new_depth;
1374 widget_value** old_stack;
1375 widget_value** new_stack;
1376 window_state* windows;
1377 widget_value* old_selection;
1378 widget_value* new_selection;
1379
1380 /* Check that enough windows and old_stack are ready. */
1381 make_windows_if_needed (mw, new_depth);
1382 make_old_stack_space (mw, new_depth);
1383 windows = mw->menu.windows;
1384 old_stack = mw->menu.old_stack;
1385 new_stack = mw->menu.new_stack;
1386
1387 /* compute the last identical different entry */
1388 for (i = 1; i < old_depth && i < new_depth; i++)
1389 if (old_stack [i] != new_stack [i])
1390 break;
1391 last_same = i - 1;
1392
1393 /* Memorize the previously selected item to be able to refresh it */
1394 old_selection = last_same + 1 < old_depth ? old_stack [last_same + 1] : NULL;
1395 if (old_selection && !old_selection->enabled)
1396 old_selection = NULL;
1397 new_selection = last_same + 1 < new_depth ? new_stack [last_same + 1] : NULL;
1398 if (new_selection && !new_selection->enabled)
1399 new_selection = NULL;
1400
d6fd6371
GM
1401 /* Call callback when the hightlighted item changes. */
1402 if (old_selection || new_selection)
1403 XtCallCallbackList ((Widget)mw, mw->menu.highlight,
1404 (XtPointer) new_selection);
1405
07bf635f
RS
1406 /* updates old_state from new_state. It has to be done now because
1407 display_menu (called below) uses the old_stack to know what to display. */
1408 for (i = last_same + 1; i < new_depth; i++)
1409 old_stack [i] = new_stack [i];
1410 mw->menu.old_depth = new_depth;
1411
908ff139 1412 /* refresh the last selection */
07bf635f
RS
1413 selection_position.x = 0;
1414 selection_position.y = 0;
1415 display_menu (mw, last_same, new_selection == old_selection,
1416 &selection_position, NULL, NULL, old_selection, new_selection);
1417
1ba46f7d
RS
1418 /* Now place the new menus. */
1419 for (i = last_same + 1; i < new_depth && new_stack[i]->contents; i++)
07bf635f 1420 {
1ba46f7d
RS
1421 window_state *previous_ws = &windows[i - 1];
1422 window_state *ws = &windows[i];
07bf635f 1423
be06a3df
GM
1424 ws->x = (previous_ws->x + selection_position.x
1425 + mw->menu.shadow_thickness);
b7fa4a06 1426 if (mw->menu.horizontal && i == 1)
be06a3df
GM
1427 ws->x += mw->menu.margin;
1428
1429#if 0
07bf635f
RS
1430 if (!mw->menu.horizontal || i > 1)
1431 ws->x += mw->menu.shadow_thickness;
be06a3df 1432#endif
5c520e0a 1433
be06a3df
GM
1434 ws->y = (previous_ws->y + selection_position.y
1435 + mw->menu.shadow_thickness);
b7fa4a06 1436 if (mw->menu.horizontal && i == 1)
be06a3df 1437 ws->y += mw->menu.margin;
07bf635f
RS
1438
1439 size_menu (mw, i);
1440
1441 fit_to_screen (mw, ws, previous_ws, mw->menu.horizontal && i == 1);
1442
1443 XClearWindow (XtDisplay (mw), ws->window);
1444 XMoveResizeWindow (XtDisplay (mw), ws->window, ws->x, ws->y,
1445 ws->width, ws->height);
1446 XMapRaised (XtDisplay (mw), ws->window);
1447 display_menu (mw, i, False, &selection_position, NULL, NULL, NULL, NULL);
1448 }
1449
1450 /* unmap the menus that popped down */
1451 for (i = new_depth - 1; i < old_depth; i++)
fa74535d 1452 if (i >= new_depth || (i > 0 && !new_stack[i]->contents))
1ba46f7d 1453 XUnmapWindow (XtDisplay (mw), windows[i].window);
07bf635f
RS
1454}
1455
1456static Boolean
d398028f
PR
1457motion_event_is_in_menu (mw, ev, level, relative_pos)
1458 XlwMenuWidget mw;
1459 XMotionEvent* ev;
1460 int level;
1461 XPoint* relative_pos;
07bf635f
RS
1462{
1463 window_state* ws = &mw->menu.windows [level];
4f08464c
RS
1464 int shadow = level == 0 ? 0 : mw->menu.shadow_thickness;
1465 int x = ws->x + shadow;
1466 int y = ws->y + shadow;
07bf635f
RS
1467 relative_pos->x = ev->x_root - x;
1468 relative_pos->y = ev->y_root - y;
4f08464c 1469 return (x - shadow < ev->x_root && ev->x_root < x + ws->width
1ecd5086 1470 && y - shadow < ev->y_root && ev->y_root < y + ws->height);
07bf635f
RS
1471}
1472
1473static Boolean
d398028f
PR
1474map_event_to_widget_value (mw, ev, val, level)
1475 XlwMenuWidget mw;
1476 XMotionEvent* ev;
1477 widget_value** val;
1478 int* level;
07bf635f
RS
1479{
1480 int i;
1481 XPoint relative_pos;
1482 window_state* ws;
1ecd5086 1483 int inside = 0;
07bf635f
RS
1484
1485 *val = NULL;
5c520e0a 1486
07bf635f
RS
1487 /* Find the window */
1488 for (i = mw->menu.old_depth - 1; i >= 0; i--)
1489 {
1490 ws = &mw->menu.windows [i];
1491 if (ws && motion_event_is_in_menu (mw, ev, i, &relative_pos))
1492 {
1ecd5086 1493 inside = 1;
07bf635f
RS
1494 display_menu (mw, i, True, NULL, &relative_pos, val, NULL, NULL);
1495
1496 if (*val)
1497 {
1498 *level = i + 1;
1499 return True;
1500 }
1501 }
1502 }
1ecd5086
JD
1503
1504 if (!inside)
1505 {
1506 if (mw->menu.inside_entry != NULL)
1507 XtCallCallbackList ((Widget)mw, mw->menu.leave,
1508 (XtPointer) mw->menu.inside_entry);
1509 mw->menu.inside_entry = NULL;
1510 }
1511
07bf635f
RS
1512 return False;
1513}
1514
1515\f/* Procedures */
1516static void
d398028f
PR
1517make_drawing_gcs (mw)
1518 XlwMenuWidget mw;
07bf635f
RS
1519{
1520 XGCValues xgcv;
ef93458b 1521 float scale;
2015c054 1522 XtGCMask mask = GCForeground | GCBackground;
07bf635f 1523
2015c054
JD
1524#ifdef HAVE_X_I18N
1525 if (!mw->menu.fontSet)
1526 {
1527 xgcv.font = mw->menu.font->fid;
1528 mask |= GCFont;
1529 }
1530#else
07bf635f 1531 xgcv.font = mw->menu.font->fid;
2015c054 1532 mask |= GCFont;
423a3f59 1533#endif
07bf635f
RS
1534 xgcv.foreground = mw->menu.foreground;
1535 xgcv.background = mw->core.background_pixel;
2015c054 1536 mw->menu.foreground_gc = XtGetGC ((Widget)mw, mask, &xgcv);
5c520e0a 1537
07bf635f 1538 xgcv.foreground = mw->menu.button_foreground;
2015c054 1539 mw->menu.button_gc = XtGetGC ((Widget)mw, mask, &xgcv);
5c520e0a 1540
07bf635f 1541 xgcv.background = mw->core.background_pixel;
8b71a9ca
PJ
1542
1543#define BRIGHTNESS(color) (((color) & 0xff) + (((color) >> 8) & 0xff) + (((color) >> 16) & 0xff))
1544
1545 /* Allocate color for disabled menu-items. */
ef93458b 1546 mw->menu.disabled_foreground = mw->menu.foreground;
8b71a9ca 1547 if (BRIGHTNESS(mw->menu.foreground) < BRIGHTNESS(mw->core.background_pixel))
ef93458b 1548 scale = 2.3;
8b71a9ca 1549 else
ef93458b 1550 scale = 0.55;
8b71a9ca
PJ
1551
1552 x_alloc_lighter_color_for_widget ((Widget) mw, XtDisplay ((Widget) mw),
1553 mw->core.colormap,
ef93458b
PJ
1554 &mw->menu.disabled_foreground,
1555 scale,
8b71a9ca 1556 0x8000);
8b71a9ca
PJ
1557
1558 if (mw->menu.foreground == mw->menu.disabled_foreground
1559 || mw->core.background_pixel == mw->menu.disabled_foreground)
1560 {
1561 /* Too few colors, use stipple. */
1562 xgcv.foreground = mw->menu.foreground;
1563 xgcv.fill_style = FillStippled;
1564 xgcv.stipple = mw->menu.gray_pixmap;
2015c054 1565 mw->menu.disabled_gc = XtGetGC ((Widget)mw, mask
423a3f59 1566 | GCFillStyle | GCStipple, &xgcv);
8b71a9ca
PJ
1567 }
1568 else
1569 {
1570 /* Many colors available, use disabled pixel. */
1571 xgcv.foreground = mw->menu.disabled_foreground;
2015c054 1572 mw->menu.disabled_gc = XtGetGC ((Widget)mw, mask, &xgcv);
8b71a9ca 1573 }
5c520e0a 1574
07bf635f
RS
1575 xgcv.foreground = mw->menu.button_foreground;
1576 xgcv.background = mw->core.background_pixel;
1577 xgcv.fill_style = FillStippled;
1578 xgcv.stipple = mw->menu.gray_pixmap;
2015c054 1579 mw->menu.inactive_button_gc = XtGetGC ((Widget)mw, mask
423a3f59 1580 | GCFillStyle | GCStipple, &xgcv);
5c520e0a 1581
07bf635f
RS
1582 xgcv.foreground = mw->core.background_pixel;
1583 xgcv.background = mw->menu.foreground;
2015c054 1584 mw->menu.background_gc = XtGetGC ((Widget)mw, mask, &xgcv);
07bf635f
RS
1585}
1586
1587static void
d398028f
PR
1588release_drawing_gcs (mw)
1589 XlwMenuWidget mw;
07bf635f
RS
1590{
1591 XtReleaseGC ((Widget) mw, mw->menu.foreground_gc);
1592 XtReleaseGC ((Widget) mw, mw->menu.button_gc);
8b71a9ca 1593 XtReleaseGC ((Widget) mw, mw->menu.disabled_gc);
07bf635f
RS
1594 XtReleaseGC ((Widget) mw, mw->menu.inactive_button_gc);
1595 XtReleaseGC ((Widget) mw, mw->menu.background_gc);
1596 /* let's get some segvs if we try to use these... */
1597 mw->menu.foreground_gc = (GC) -1;
1598 mw->menu.button_gc = (GC) -1;
8b71a9ca 1599 mw->menu.disabled_gc = (GC) -1;
07bf635f
RS
1600 mw->menu.inactive_button_gc = (GC) -1;
1601 mw->menu.background_gc = (GC) -1;
1602}
1603
1604#define MINL(x,y) ((((unsigned long) (x)) < ((unsigned long) (y))) \
1605 ? ((unsigned long) (x)) : ((unsigned long) (y)))
1606
1607static void
d398028f
PR
1608make_shadow_gcs (mw)
1609 XlwMenuWidget mw;
07bf635f
RS
1610{
1611 XGCValues xgcv;
1612 unsigned long pm = 0;
1613 Display *dpy = XtDisplay ((Widget) mw);
be06a3df 1614 Screen *screen = XtScreen ((Widget) mw);
b0404f9f 1615 Colormap cmap = mw->core.colormap;
07bf635f
RS
1616 XColor topc, botc;
1617 int top_frobbed = 0, bottom_frobbed = 0;
1618
bba2a923
GM
1619 mw->menu.free_top_shadow_color_p = 0;
1620 mw->menu.free_bottom_shadow_color_p = 0;
1621
07bf635f
RS
1622 if (mw->menu.top_shadow_color == -1)
1623 mw->menu.top_shadow_color = mw->core.background_pixel;
bba2a923
GM
1624 else
1625 mw->menu.top_shadow_color = mw->menu.top_shadow_color;
5c520e0a 1626
07bf635f
RS
1627 if (mw->menu.bottom_shadow_color == -1)
1628 mw->menu.bottom_shadow_color = mw->menu.foreground;
bba2a923
GM
1629 else
1630 mw->menu.bottom_shadow_color = mw->menu.bottom_shadow_color;
07bf635f
RS
1631
1632 if (mw->menu.top_shadow_color == mw->core.background_pixel ||
1633 mw->menu.top_shadow_color == mw->menu.foreground)
1634 {
1635 topc.pixel = mw->core.background_pixel;
65c0ae05
MB
1636#ifdef emacs
1637 if (x_alloc_lighter_color_for_widget ((Widget) mw, dpy, cmap,
1638 &topc.pixel,
1639 1.2, 0x8000))
1640#else
07bf635f
RS
1641 XQueryColor (dpy, cmap, &topc);
1642 /* don't overflow/wrap! */
1643 topc.red = MINL (65535, topc.red * 1.2);
1644 topc.green = MINL (65535, topc.green * 1.2);
1645 topc.blue = MINL (65535, topc.blue * 1.2);
1646 if (XAllocColor (dpy, cmap, &topc))
be06a3df 1647#endif
07bf635f
RS
1648 {
1649 mw->menu.top_shadow_color = topc.pixel;
bba2a923 1650 mw->menu.free_top_shadow_color_p = 1;
07bf635f
RS
1651 top_frobbed = 1;
1652 }
1653 }
1654 if (mw->menu.bottom_shadow_color == mw->menu.foreground ||
1655 mw->menu.bottom_shadow_color == mw->core.background_pixel)
1656 {
1657 botc.pixel = mw->core.background_pixel;
65c0ae05
MB
1658#ifdef emacs
1659 if (x_alloc_lighter_color_for_widget ((Widget) mw, dpy, cmap,
1660 &botc.pixel,
1661 0.6, 0x4000))
1662#else
07bf635f
RS
1663 XQueryColor (dpy, cmap, &botc);
1664 botc.red *= 0.6;
1665 botc.green *= 0.6;
1666 botc.blue *= 0.6;
1667 if (XAllocColor (dpy, cmap, &botc))
be06a3df 1668#endif
07bf635f
RS
1669 {
1670 mw->menu.bottom_shadow_color = botc.pixel;
bba2a923 1671 mw->menu.free_bottom_shadow_color_p = 1;
07bf635f
RS
1672 bottom_frobbed = 1;
1673 }
1674 }
1675
1676 if (top_frobbed && bottom_frobbed)
1677 {
65c0ae05 1678 if (topc.pixel == botc.pixel)
07bf635f
RS
1679 {
1680 if (botc.pixel == mw->menu.foreground)
fc8fefed 1681 {
bba2a923
GM
1682 if (mw->menu.free_top_shadow_color_p)
1683 {
1684 x_free_dpy_colors (dpy, screen, cmap,
1685 &mw->menu.top_shadow_color, 1);
1686 mw->menu.free_top_shadow_color_p = 0;
1687 }
1688 mw->menu.top_shadow_color = mw->core.background_pixel;
fc8fefed 1689 }
07bf635f 1690 else
fc8fefed 1691 {
bba2a923
GM
1692 if (mw->menu.free_bottom_shadow_color_p)
1693 {
1694 x_free_dpy_colors (dpy, screen, cmap,
1695 &mw->menu.bottom_shadow_color, 1);
1696 mw->menu.free_bottom_shadow_color_p = 0;
1697 }
1698 mw->menu.bottom_shadow_color = mw->menu.foreground;
fc8fefed 1699 }
07bf635f
RS
1700 }
1701 }
1702
1703 if (!mw->menu.top_shadow_pixmap &&
1704 mw->menu.top_shadow_color == mw->core.background_pixel)
1705 {
1706 mw->menu.top_shadow_pixmap = mw->menu.gray_pixmap;
bba2a923
GM
1707 if (mw->menu.free_top_shadow_color_p)
1708 {
1709 x_free_dpy_colors (dpy, screen, cmap, &mw->menu.top_shadow_color, 1);
1710 mw->menu.free_top_shadow_color_p = 0;
1711 }
1712 mw->menu.top_shadow_color = mw->menu.foreground;
07bf635f
RS
1713 }
1714 if (!mw->menu.bottom_shadow_pixmap &&
1715 mw->menu.bottom_shadow_color == mw->core.background_pixel)
1716 {
1717 mw->menu.bottom_shadow_pixmap = mw->menu.gray_pixmap;
bba2a923
GM
1718 if (mw->menu.free_bottom_shadow_color_p)
1719 {
1720 x_free_dpy_colors (dpy, screen, cmap,
1721 &mw->menu.bottom_shadow_color, 1);
1722 mw->menu.free_bottom_shadow_color_p = 0;
1723 }
1724 mw->menu.bottom_shadow_color = mw->menu.foreground;
07bf635f
RS
1725 }
1726
1727 xgcv.fill_style = FillStippled;
1728 xgcv.foreground = mw->menu.top_shadow_color;
1729 xgcv.stipple = mw->menu.top_shadow_pixmap;
1730 pm = (xgcv.stipple ? GCStipple|GCFillStyle : 0);
1731 mw->menu.shadow_top_gc = XtGetGC ((Widget)mw, GCForeground | pm, &xgcv);
1732
1733 xgcv.foreground = mw->menu.bottom_shadow_color;
1734 xgcv.stipple = mw->menu.bottom_shadow_pixmap;
1735 pm = (xgcv.stipple ? GCStipple|GCFillStyle : 0);
1736 mw->menu.shadow_bottom_gc = XtGetGC ((Widget)mw, GCForeground | pm, &xgcv);
1737}
1738
1739
1740static void
d398028f
PR
1741release_shadow_gcs (mw)
1742 XlwMenuWidget mw;
07bf635f 1743{
fc8fefed
GM
1744 Display *dpy = XtDisplay ((Widget) mw);
1745 Screen *screen = XtScreen ((Widget) mw);
1746 Colormap cmap = mw->core.colormap;
1747 Pixel px[2];
bba2a923
GM
1748 int i = 0;
1749
1750 if (mw->menu.free_top_shadow_color_p)
1751 px[i++] = mw->menu.top_shadow_color;
1752 if (mw->menu.free_bottom_shadow_color_p)
1753 px[i++] = mw->menu.bottom_shadow_color;
1754 if (i > 0)
1755 x_free_dpy_colors (dpy, screen, cmap, px, i);
5c520e0a 1756
07bf635f
RS
1757 XtReleaseGC ((Widget) mw, mw->menu.shadow_top_gc);
1758 XtReleaseGC ((Widget) mw, mw->menu.shadow_bottom_gc);
1759}
1760
1761static void
47d52240 1762XlwMenuInitialize (request, mw, args, num_args)
d398028f 1763 Widget request;
47d52240 1764 XlwMenuWidget mw;
d398028f
PR
1765 ArgList args;
1766 Cardinal *num_args;
07bf635f
RS
1767{
1768 /* Get the GCs and the widget size */
5c520e0a 1769
07bf635f
RS
1770 Window window = RootWindowOfScreen (DefaultScreenOfDisplay (XtDisplay (mw)));
1771 Display* display = XtDisplay (mw);
5c520e0a 1772
d398028f
PR
1773#if 0
1774 widget_value *tem = (widget_value *) XtMalloc (sizeof (widget_value));
1775
1776 /* _XtCreate is freeing the object that was passed to us,
1777 so make a copy that we will actually keep. */
1778 lwlib_bcopy (mw->menu.contents, tem, sizeof (widget_value));
1779 mw->menu.contents = tem;
1780#endif
1781
07bf635f
RS
1782/* mw->menu.cursor = XCreateFontCursor (display, mw->menu.cursor_shape); */
1783 mw->menu.cursor = mw->menu.cursor_shape;
5c520e0a 1784
a504c8fa 1785 mw->menu.gray_pixmap
f1c16db4
GM
1786 = XCreatePixmapFromBitmapData (display, window, gray_bitmap_bits,
1787 gray_bitmap_width, gray_bitmap_height,
a504c8fa 1788 (unsigned long)1, (unsigned long)0, 1);
5c520e0a 1789
47d52240
RS
1790 /* I don't understand why this ends up 0 sometimes,
1791 but it does. This kludge works around it.
1792 Can anyone find a real fix? -- rms. */
1793 if (mw->menu.font == 0)
1794 mw->menu.font = xlwmenu_default_font;
2015c054
JD
1795#ifdef HAVE_X_I18N
1796 if (mw->menu.fontSet)
1797 mw->menu.font_extents = XExtentsOfFontSet (mw->menu.fontSet);
423a3f59
SM
1798#endif
1799
07bf635f
RS
1800 make_drawing_gcs (mw);
1801 make_shadow_gcs (mw);
5c520e0a 1802
07bf635f 1803 mw->menu.popped_up = False;
5c520e0a 1804
07bf635f
RS
1805 mw->menu.old_depth = 1;
1806 mw->menu.old_stack = (widget_value**)XtMalloc (sizeof (widget_value*));
1807 mw->menu.old_stack_length = 1;
1808 mw->menu.old_stack [0] = mw->menu.contents;
5c520e0a 1809
07bf635f
RS
1810 mw->menu.new_depth = 0;
1811 mw->menu.new_stack = 0;
1812 mw->menu.new_stack_length = 0;
1813 push_new_stack (mw, mw->menu.contents);
5c520e0a 1814
07bf635f
RS
1815 mw->menu.windows = (window_state*)XtMalloc (sizeof (window_state));
1816 mw->menu.windows_length = 1;
1817 mw->menu.windows [0].x = 0;
1818 mw->menu.windows [0].y = 0;
1819 mw->menu.windows [0].width = 0;
1820 mw->menu.windows [0].height = 0;
1821 size_menu (mw, 0);
5c520e0a 1822
07bf635f
RS
1823 mw->core.width = mw->menu.windows [0].width;
1824 mw->core.height = mw->menu.windows [0].height;
1825}
1826
1827static void
1828XlwMenuClassInitialize ()
1829{
1830}
1831
1832static void
d398028f
PR
1833XlwMenuRealize (w, valueMask, attributes)
1834 Widget w;
1835 Mask *valueMask;
1836 XSetWindowAttributes *attributes;
07bf635f
RS
1837{
1838 XlwMenuWidget mw = (XlwMenuWidget)w;
1839 XSetWindowAttributes xswa;
1840 int mask;
1841
1842 (*xlwMenuWidgetClass->core_class.superclass->core_class.realize)
1843 (w, valueMask, attributes);
1844
1845 xswa.save_under = True;
1846 xswa.cursor = mw->menu.cursor_shape;
1847 mask = CWSaveUnder | CWCursor;
2dfa0fac
KL
1848 /* I sometimes get random BadCursor errors while creating the first
1849 frame on a display. I can not find their reason, but they are
1850 annoying so for now let's ignore any errors here. -- lorentey */
1851#ifdef emacs
16b55533 1852 x_catch_errors (XtDisplay (w));
2dfa0fac 1853#endif
07bf635f 1854 XChangeWindowAttributes (XtDisplay (w), XtWindow (w), mask, &xswa);
2dfa0fac 1855#ifdef emacs
16b55533 1856 x_uncatch_errors ();
2dfa0fac 1857#endif
07bf635f
RS
1858
1859 mw->menu.windows [0].window = XtWindow (w);
1860 mw->menu.windows [0].x = w->core.x;
1861 mw->menu.windows [0].y = w->core.y;
1862 mw->menu.windows [0].width = w->core.width;
1863 mw->menu.windows [0].height = w->core.height;
1864}
1865
1866/* Only the toplevel menubar/popup is a widget so it's the only one that
1867 receives expose events through Xt. So we repaint all the other panes
1868 when receiving an Expose event. */
5c520e0a 1869static void
d398028f
PR
1870XlwMenuRedisplay (w, ev, region)
1871 Widget w;
1872 XEvent* ev;
1873 Region region;
07bf635f
RS
1874{
1875 XlwMenuWidget mw = (XlwMenuWidget)w;
1876 int i;
1877
d398028f
PR
1878 /* If we have a depth beyond 1, it's because a submenu was displayed.
1879 If the submenu has been destroyed, set the depth back to 1. */
1880 if (submenu_destroyed)
1881 {
1882 mw->menu.old_depth = 1;
1883 submenu_destroyed = 0;
1884 }
1885
07bf635f
RS
1886 for (i = 0; i < mw->menu.old_depth; i++)
1887 display_menu (mw, i, False, NULL, NULL, NULL, NULL, NULL);
1888}
1889
f3c9e544
GM
1890
1891/* Part of a hack to make the menu redisplay when a tooltip frame
1892 over a menu item is unmapped. */
1893
1894void
1895xlwmenu_redisplay (w)
1896 Widget w;
1897{
1898 XlwMenuRedisplay (w, NULL, None);
1899}
1900
5c520e0a 1901static void
d398028f
PR
1902XlwMenuDestroy (w)
1903 Widget w;
07bf635f
RS
1904{
1905 int i;
1906 XlwMenuWidget mw = (XlwMenuWidget) w;
1907
d398028f 1908 if (pointer_grabbed)
4db7db7d 1909 ungrab_all ((Widget)w, CurrentTime);
d398028f
PR
1910 pointer_grabbed = 0;
1911
1912 submenu_destroyed = 1;
1913
07bf635f
RS
1914 release_drawing_gcs (mw);
1915 release_shadow_gcs (mw);
1916
1917 /* this doesn't come from the resource db but is created explicitly
1918 so we must free it ourselves. */
1919 XFreePixmap (XtDisplay (mw), mw->menu.gray_pixmap);
1920 mw->menu.gray_pixmap = (Pixmap) -1;
1921
d398028f
PR
1922#if 0
1923 /* Do free mw->menu.contents because nowadays we copy it
1924 during initialization. */
1925 XtFree (mw->menu.contents);
1926#endif
1927
07bf635f
RS
1928 /* Don't free mw->menu.contents because that comes from our creator.
1929 The `*_stack' elements are just pointers into `contents' so leave
1930 that alone too. But free the stacks themselves. */
1931 if (mw->menu.old_stack) XtFree ((char *) mw->menu.old_stack);
1932 if (mw->menu.new_stack) XtFree ((char *) mw->menu.new_stack);
1933
1934 /* Remember, you can't free anything that came from the resource
1935 database. This includes:
1936 mw->menu.cursor
1937 mw->menu.top_shadow_pixmap
1938 mw->menu.bottom_shadow_pixmap
1939 mw->menu.font
1940 Also the color cells of top_shadow_color, bottom_shadow_color,
1941 foreground, and button_foreground will never be freed until this
1942 client exits. Nice, eh?
1943 */
1944
1945 /* start from 1 because the one in slot 0 is w->core.window */
1946 for (i = 1; i < mw->menu.windows_length; i++)
1947 XDestroyWindow (XtDisplay (mw), mw->menu.windows [i].window);
1948 if (mw->menu.windows)
1949 XtFree ((char *) mw->menu.windows);
1950}
1951
5c520e0a 1952static Boolean
d398028f
PR
1953XlwMenuSetValues (current, request, new)
1954 Widget current;
1955 Widget request;
1956 Widget new;
07bf635f
RS
1957{
1958 XlwMenuWidget oldmw = (XlwMenuWidget)current;
1959 XlwMenuWidget newmw = (XlwMenuWidget)new;
1960 Boolean redisplay = False;
1961 int i;
1962
1963 if (newmw->menu.contents
1964 && newmw->menu.contents->contents
1965 && newmw->menu.contents->contents->change >= VISIBLE_CHANGE)
1966 redisplay = True;
ba624d0f
RS
1967 /* Do redisplay if the contents are entirely eliminated. */
1968 if (newmw->menu.contents
1969 && newmw->menu.contents->contents == 0
1970 && newmw->menu.contents->change >= VISIBLE_CHANGE)
1971 redisplay = True;
07bf635f
RS
1972
1973 if (newmw->core.background_pixel != oldmw->core.background_pixel
d398028f 1974 || newmw->menu.foreground != oldmw->menu.foreground
2015c054
JD
1975#ifdef HAVE_X_I18N
1976 || newmw->menu.fontSet != oldmw->menu.fontSet
1977 || (newmw->menu.fontSet == NULL && newmw->menu.font != oldmw->menu.font)
1978#else
423a3f59
SM
1979 || newmw->menu.font != oldmw->menu.font
1980#endif
1981 )
07bf635f
RS
1982 {
1983 release_drawing_gcs (newmw);
1984 make_drawing_gcs (newmw);
90c7e9f0
MB
1985
1986 release_shadow_gcs (newmw);
1987 /* Cause the shadow colors to be recalculated. */
1988 newmw->menu.top_shadow_color = -1;
1989 newmw->menu.bottom_shadow_color = -1;
1990 make_shadow_gcs (newmw);
1991
07bf635f 1992 redisplay = True;
5c520e0a 1993
43aa2f1b 1994 if (XtIsRealized (current))
da353f23
MB
1995 /* If the menu is currently displayed, change the display. */
1996 for (i = 0; i < oldmw->menu.windows_length; i++)
1997 {
1998 XSetWindowBackground (XtDisplay (oldmw),
1999 oldmw->menu.windows [i].window,
2000 newmw->core.background_pixel);
2001 /* clear windows and generate expose events */
2002 XClearArea (XtDisplay (oldmw), oldmw->menu.windows[i].window,
2003 0, 0, 0, 0, True);
2004 }
07bf635f
RS
2005 }
2006
423a3f59 2007#ifdef HAVE_X_I18N
2015c054 2008 if (newmw->menu.fontSet != oldmw->menu.fontSet && newmw->menu.fontSet != NULL)
423a3f59
SM
2009 {
2010 redisplay = True;
2015c054 2011 newmw->menu.font_extents = XExtentsOfFontSet (newmw->menu.fontSet);
423a3f59
SM
2012 }
2013#endif
2014
07bf635f
RS
2015 return redisplay;
2016}
2017
5c520e0a 2018static void
d398028f
PR
2019XlwMenuResize (w)
2020 Widget w;
07bf635f
RS
2021{
2022 XlwMenuWidget mw = (XlwMenuWidget)w;
2023
d398028f
PR
2024 if (mw->menu.popped_up)
2025 {
2026 /* Don't allow the popup menu to resize itself. */
2027 mw->core.width = mw->menu.windows [0].width;
2028 mw->core.height = mw->menu.windows [0].height;
2029 mw->core.parent->core.width = mw->core.width ;
2030 mw->core.parent->core.height = mw->core.height ;
2031 }
2032 else
2033 {
2034 mw->menu.windows [0].width = mw->core.width;
2035 mw->menu.windows [0].height = mw->core.height;
2036 }
07bf635f
RS
2037}
2038
2039\f/* Action procedures */
2040static void
d398028f
PR
2041handle_single_motion_event (mw, ev)
2042 XlwMenuWidget mw;
2043 XMotionEvent* ev;
07bf635f
RS
2044{
2045 widget_value* val;
2046 int level;
2047
2048 if (!map_event_to_widget_value (mw, ev, &val, &level))
2049 pop_new_stack_if_no_contents (mw);
2050 else
2051 set_new_state (mw, val, level);
2052 remap_menubar (mw);
5c520e0a 2053
07bf635f
RS
2054 /* Sync with the display. Makes it feel better on X terms. */
2055 XSync (XtDisplay (mw), False);
2056}
2057
2058static void
d398028f
PR
2059handle_motion_event (mw, ev)
2060 XlwMenuWidget mw;
2061 XMotionEvent* ev;
07bf635f
RS
2062{
2063 int x = ev->x_root;
2064 int y = ev->y_root;
2065 int state = ev->state;
2066
2067 handle_single_motion_event (mw, ev);
2068
2069 /* allow motion events to be generated again */
2070 if (ev->is_hint
2071 && XQueryPointer (XtDisplay (mw), ev->window,
2072 &ev->root, &ev->subwindow,
2073 &ev->x_root, &ev->y_root,
2074 &ev->x, &ev->y,
2075 &ev->state)
2076 && ev->state == state
2077 && (ev->x_root != x || ev->y_root != y))
2078 handle_single_motion_event (mw, ev);
2079}
2080
5c520e0a 2081static void
d398028f
PR
2082Start (w, ev, params, num_params)
2083 Widget w;
2084 XEvent *ev;
2085 String *params;
2086 Cardinal *num_params;
07bf635f
RS
2087{
2088 XlwMenuWidget mw = (XlwMenuWidget)w;
2089
d398028f
PR
2090 if (!mw->menu.popped_up)
2091 {
2092 menu_post_event = *ev;
244c93fe
JD
2093 /* If event is set to CurrentTime, get the last known time stamp.
2094 This is for calculating if (popup) menus should stay up after
2095 a fast click. */
2096 if (menu_post_event.xbutton.time == CurrentTime)
2097 menu_post_event.xbutton.time
2098 = XtLastTimestampProcessed (XtDisplay (w));
2099
a57a1605 2100 pop_up_menu (mw, (XButtonPressedEvent*) ev);
d398028f
PR
2101 }
2102 else
4cc76151
PR
2103 {
2104 /* If we push a button while the menu is posted semipermanently,
2105 releasing the button should always pop the menu down. */
2106 next_release_must_exit = 1;
d398028f 2107
4cc76151
PR
2108 /* notes the absolute position of the menubar window */
2109 mw->menu.windows [0].x = ev->xmotion.x_root - ev->xmotion.x;
2110 mw->menu.windows [0].y = ev->xmotion.y_root - ev->xmotion.y;
07bf635f 2111
4cc76151 2112 /* handles the down like a move, slots are compatible */
1f5df1f7 2113 ev->xmotion.is_hint = 0;
4cc76151
PR
2114 handle_motion_event (mw, &ev->xmotion);
2115 }
07bf635f
RS
2116}
2117
5c520e0a 2118static void
d398028f
PR
2119Drag (w, ev, params, num_params)
2120 Widget w;
2121 XEvent *ev;
2122 String *params;
2123 Cardinal *num_params;
07bf635f
RS
2124{
2125 XlwMenuWidget mw = (XlwMenuWidget)w;
3c9ce1c4
KH
2126 if (mw->menu.popped_up)
2127 handle_motion_event (mw, &ev->xmotion);
07bf635f
RS
2128}
2129
ba624d0f
RS
2130/* Do nothing.
2131 This is how we handle presses and releases of modifier keys. */
2132static void
2133Nothing (w, ev, params, num_params)
2134 Widget w;
2135 XEvent *ev;
2136 String *params;
2137 Cardinal *num_params;
2138{
2139}
2140
244c93fe 2141static widget_value *
03a693b4 2142find_first_selectable (mw, item, skip_titles)
1b8d91ab
PJ
2143 XlwMenuWidget mw;
2144 widget_value *item;
03a693b4 2145 int skip_titles;
1b8d91ab
PJ
2146{
2147 widget_value *current = item;
2148 enum menu_separator separator;
2149
244c93fe 2150 while (lw_separator_p (current->name, &separator, 0) || !current->enabled
03a693b4 2151 || (skip_titles && !current->call_data && !current->contents))
1b8d91ab
PJ
2152 if (current->next)
2153 current=current->next;
2154 else
03a693b4 2155 return NULL;
1b8d91ab
PJ
2156
2157 return current;
2158}
2159
244c93fe 2160static widget_value *
03a693b4 2161find_next_selectable (mw, item, skip_titles)
a7e19a26
PJ
2162 XlwMenuWidget mw;
2163 widget_value *item;
43f15d4a 2164 int skip_titles;
a7e19a26
PJ
2165{
2166 widget_value *current = item;
2167 enum menu_separator separator;
2168
2169 while (current->next && (current=current->next) &&
244c93fe 2170 (lw_separator_p (current->name, &separator, 0) || !current->enabled
03a693b4 2171 || (skip_titles && !current->call_data && !current->contents)))
a7e19a26
PJ
2172 ;
2173
2174 if (current == item)
2175 {
8b71a9ca
PJ
2176 if (mw->menu.old_depth < 2)
2177 return current;
a7e19a26
PJ
2178 current = mw->menu.old_stack [mw->menu.old_depth - 2]->contents;
2179
244c93fe
JD
2180 while (lw_separator_p (current->name, &separator, 0)
2181 || !current->enabled
03a693b4
JD
2182 || (skip_titles && !current->call_data
2183 && !current->contents))
1b8d91ab
PJ
2184 {
2185 if (current->next)
2186 current=current->next;
2187
2188 if (current == item)
2189 break;
2190 }
2191
a7e19a26
PJ
2192 }
2193
2194 return current;
2195}
2196
244c93fe 2197static widget_value *
03a693b4 2198find_prev_selectable (mw, item, skip_titles)
a7e19a26
PJ
2199 XlwMenuWidget mw;
2200 widget_value *item;
43f15d4a 2201 int skip_titles;
a7e19a26
PJ
2202{
2203 widget_value *current = item;
2204 widget_value *prev = item;
2205
03a693b4 2206 while ((current=find_next_selectable (mw, current, skip_titles))
244c93fe 2207 != item)
1b8d91ab
PJ
2208 {
2209 if (prev == current)
2210 break;
a7e19a26 2211 prev=current;
1b8d91ab 2212 }
a7e19a26
PJ
2213
2214 return prev;
2215}
2216
2217static void
2218Down (w, ev, params, num_params)
2219 Widget w;
2220 XEvent *ev;
2221 String *params;
2222 Cardinal *num_params;
2223{
2224 XlwMenuWidget mw = (XlwMenuWidget) w;
2225 widget_value* selected_item = mw->menu.old_stack [mw->menu.old_depth - 1];
244c93fe 2226 int popup_menu_p = mw->menu.top_depth == 1;
a7e19a26
PJ
2227
2228 /* Inside top-level menu-bar? */
244c93fe 2229 if (mw->menu.old_depth == mw->menu.top_depth)
a7e19a26 2230 /* When <down> in the menu-bar is pressed, display the corresponding
244c93fe 2231 sub-menu and select the first selectable menu item there.
03a693b4 2232 If this is a popup menu, skip title item of the popup. */
244c93fe
JD
2233 set_new_state (mw,
2234 find_first_selectable (mw,
2235 selected_item->contents,
2236 popup_menu_p),
2237 mw->menu.old_depth);
a7e19a26
PJ
2238 else
2239 /* Highlight next possible (enabled and not separator) menu item. */
244c93fe
JD
2240 set_new_state (mw, find_next_selectable (mw, selected_item, popup_menu_p),
2241 mw->menu.old_depth - 1);
a7e19a26
PJ
2242
2243 remap_menubar (mw);
2244}
2245
2246static void
2247Up (w, ev, params, num_params)
2248 Widget w;
2249 XEvent *ev;
2250 String *params;
2251 Cardinal *num_params;
2252{
2253 XlwMenuWidget mw = (XlwMenuWidget) w;
2254 widget_value* selected_item = mw->menu.old_stack [mw->menu.old_depth - 1];
244c93fe 2255 int popup_menu_p = mw->menu.top_depth == 1;
a7e19a26
PJ
2256
2257 /* Inside top-level menu-bar? */
244c93fe 2258 if (mw->menu.old_depth == mw->menu.top_depth)
a7e19a26
PJ
2259 {
2260 /* FIXME: this is tricky. <up> in the menu-bar should select the
1b8d91ab
PJ
2261 last selectable item in the list. So we select the first
2262 selectable one and find the previous selectable item. Is there
2263 a better way? */
03a693b4 2264 /* If this is a popup menu, skip title item of the popup. */
244c93fe
JD
2265 set_new_state (mw,
2266 find_first_selectable (mw,
2267 selected_item->contents,
2268 popup_menu_p),
2269 mw->menu.old_depth);
a7e19a26
PJ
2270 remap_menubar (mw);
2271 selected_item = mw->menu.old_stack [mw->menu.old_depth - 1];
244c93fe
JD
2272 set_new_state (mw,
2273 find_prev_selectable (mw,
2274 selected_item,
2275 popup_menu_p),
2276 mw->menu.old_depth - 1);
a7e19a26
PJ
2277 }
2278 else
2279 /* Highlight previous (enabled and not separator) menu item. */
244c93fe
JD
2280 set_new_state (mw, find_prev_selectable (mw, selected_item, popup_menu_p),
2281 mw->menu.old_depth - 1);
a7e19a26
PJ
2282
2283 remap_menubar (mw);
2284}
2285
244c93fe 2286void
a7e19a26
PJ
2287Left (w, ev, params, num_params)
2288 Widget w;
2289 XEvent *ev;
2290 String *params;
2291 Cardinal *num_params;
2292{
2293 XlwMenuWidget mw = (XlwMenuWidget) w;
2294 widget_value* selected_item = mw->menu.old_stack [mw->menu.old_depth - 1];
2295
2296 /* Inside top-level menu-bar? */
244c93fe 2297 if (mw->menu.old_depth == mw->menu.top_depth)
a7e19a26
PJ
2298 /* When <left> in the menu-bar is pressed, display the previous item on
2299 the menu-bar. If the current item is the first one, highlight the
2300 last item in the menubar (probably Help). */
244c93fe
JD
2301 set_new_state (mw, find_prev_selectable (mw, selected_item, 0),
2302 mw->menu.old_depth - 1);
fa74535d
RS
2303 else if (mw->menu.old_depth == 1
2304 && selected_item->contents) /* Is this menu item expandable? */
2305 {
2306 set_new_state (mw, selected_item->contents, mw->menu.old_depth);
2307 remap_menubar (mw);
2308 selected_item = mw->menu.old_stack [mw->menu.old_depth - 1];
244c93fe
JD
2309 if (!selected_item->enabled && find_first_selectable (mw,
2310 selected_item,
2311 0))
2312 set_new_state (mw, find_first_selectable (mw, selected_item, 0),
2313 mw->menu.old_depth - 1);
fa74535d
RS
2314 }
2315
a7e19a26
PJ
2316 else
2317 {
2318 pop_new_stack_if_no_contents (mw);
244c93fe
JD
2319 set_new_state (mw, mw->menu.old_stack [mw->menu.old_depth - 2],
2320 mw->menu.old_depth - 2);
a7e19a26
PJ
2321 }
2322
2323 remap_menubar (mw);
2324}
2325
244c93fe 2326void
a7e19a26
PJ
2327Right (w, ev, params, num_params)
2328 Widget w;
2329 XEvent *ev;
2330 String *params;
2331 Cardinal *num_params;
2332{
2333 XlwMenuWidget mw = (XlwMenuWidget) w;
2334 widget_value* selected_item = mw->menu.old_stack [mw->menu.old_depth - 1];
2335
2336 /* Inside top-level menu-bar? */
244c93fe 2337 if (mw->menu.old_depth == mw->menu.top_depth)
a7e19a26
PJ
2338 /* When <right> in the menu-bar is pressed, display the next item on
2339 the menu-bar. If the current item is the last one, highlight the
2340 first item (probably File). */
244c93fe
JD
2341 set_new_state (mw, find_next_selectable (mw, selected_item, 0),
2342 mw->menu.old_depth - 1);
a7e19a26 2343 else if (selected_item->contents) /* Is this menu item expandable? */
1b8d91ab
PJ
2344 {
2345 set_new_state (mw, selected_item->contents, mw->menu.old_depth);
2346 remap_menubar (mw);
2347 selected_item = mw->menu.old_stack [mw->menu.old_depth - 1];
244c93fe
JD
2348 if (!selected_item->enabled && find_first_selectable (mw,
2349 selected_item,
2350 0))
2351 set_new_state (mw, find_first_selectable (mw, selected_item, 0),
2352 mw->menu.old_depth - 1);
1b8d91ab 2353 }
a7e19a26
PJ
2354 else
2355 {
2356 pop_new_stack_if_no_contents (mw);
244c93fe
JD
2357 set_new_state (mw, mw->menu.old_stack [mw->menu.old_depth - 2],
2358 mw->menu.old_depth - 2);
a7e19a26
PJ
2359 }
2360
2361 remap_menubar (mw);
2362}
2363
ba624d0f
RS
2364/* Handle key press and release events while menu is popped up.
2365 Our action is to get rid of the menu. */
2366static void
2367Key (w, ev, params, num_params)
2368 Widget w;
2369 XEvent *ev;
2370 String *params;
2371 Cardinal *num_params;
2372{
2373 XlwMenuWidget mw = (XlwMenuWidget)w;
2374
2375 /* Pop down everything. */
2376 mw->menu.new_depth = 1;
2377 remap_menubar (mw);
2378
2379 if (mw->menu.popped_up)
2380 {
2381 mw->menu.popped_up = False;
4db7db7d 2382 ungrab_all ((Widget)mw, ev->xmotion.time);
ba624d0f
RS
2383 if (XtIsShell (XtParent ((Widget) mw)))
2384 XtPopdown (XtParent ((Widget) mw));
2385 else
2386 {
2387 XtRemoveGrab ((Widget) mw);
2388 display_menu (mw, 0, False, NULL, NULL, NULL, NULL, NULL);
2389 }
2390 }
2391
2392 /* callback */
2393 XtCallCallbackList ((Widget)mw, mw->menu.select, (XtPointer)0);
2394}
2395
2396static void
d398028f
PR
2397Select (w, ev, params, num_params)
2398 Widget w;
2399 XEvent *ev;
2400 String *params;
2401 Cardinal *num_params;
07bf635f
RS
2402{
2403 XlwMenuWidget mw = (XlwMenuWidget)w;
2404 widget_value* selected_item = mw->menu.old_stack [mw->menu.old_depth - 1];
5c520e0a 2405
d398028f
PR
2406 /* If user releases the button quickly, without selecting anything,
2407 after the initial down-click that brought the menu up,
2408 do nothing. */
2409 if ((selected_item == 0
2410 || ((widget_value *) selected_item)->call_data == 0)
2411 && !next_release_must_exit
2412 && (ev->xbutton.time - menu_post_event.xbutton.time
2413 < XtGetMultiClickTime (XtDisplay (w))))
2414 return;
2415
2416 /* pop down everything. */
07bf635f
RS
2417 mw->menu.new_depth = 1;
2418 remap_menubar (mw);
2419
2420 if (mw->menu.popped_up)
2421 {
2422 mw->menu.popped_up = False;
4db7db7d 2423 ungrab_all ((Widget)mw, ev->xmotion.time);
2289f3f4
PR
2424 if (XtIsShell (XtParent ((Widget) mw)))
2425 XtPopdown (XtParent ((Widget) mw));
4cc76151
PR
2426 else
2427 {
2428 XtRemoveGrab ((Widget) mw);
2429 display_menu (mw, 0, False, NULL, NULL, NULL, NULL, NULL);
2430 }
07bf635f
RS
2431 }
2432
2433 /* callback */
2434 XtCallCallbackList ((Widget)mw, mw->menu.select, (XtPointer)selected_item);
07bf635f
RS
2435}
2436
2437
2438\f/* Special code to pop-up a menu */
244c93fe 2439static void
d398028f
PR
2440pop_up_menu (mw, event)
2441 XlwMenuWidget mw;
2442 XButtonPressedEvent* event;
07bf635f
RS
2443{
2444 int x = event->x_root;
2445 int y = event->y_root;
2446 int w;
2447 int h;
2448 int borderwidth = mw->menu.shadow_thickness;
2449 Screen* screen = XtScreen (mw);
8400b9ed 2450 Display *display = XtDisplay (mw);
07bf635f 2451
d398028f
PR
2452 next_release_must_exit = 0;
2453
1ecd5086 2454 mw->menu.inside_entry = NULL;
07bf635f
RS
2455 XtCallCallbackList ((Widget)mw, mw->menu.open, NULL);
2456
2289f3f4 2457 if (XtIsShell (XtParent ((Widget)mw)))
4cc76151 2458 size_menu (mw, 0);
07bf635f
RS
2459
2460 w = mw->menu.windows [0].width;
2461 h = mw->menu.windows [0].height;
2462
2463 x -= borderwidth;
2464 y -= borderwidth;
2465 if (x < borderwidth)
2466 x = borderwidth;
2467 if (x + w + 2 * borderwidth > WidthOfScreen (screen))
2468 x = WidthOfScreen (screen) - w - 2 * borderwidth;
2469 if (y < borderwidth)
2470 y = borderwidth;
2471 if (y + h + 2 * borderwidth> HeightOfScreen (screen))
2472 y = HeightOfScreen (screen) - h - 2 * borderwidth;
2473
2474 mw->menu.popped_up = True;
2289f3f4 2475 if (XtIsShell (XtParent ((Widget)mw)))
4cc76151 2476 {
2289f3f4
PR
2477 XtConfigureWidget (XtParent ((Widget)mw), x, y, w, h,
2478 XtParent ((Widget)mw)->core.border_width);
2479 XtPopup (XtParent ((Widget)mw), XtGrabExclusive);
4cc76151
PR
2480 display_menu (mw, 0, False, NULL, NULL, NULL, NULL, NULL);
2481 mw->menu.windows [0].x = x + borderwidth;
2482 mw->menu.windows [0].y = y + borderwidth;
244c93fe 2483 mw->menu.top_depth = 1; /* Popup menus don't have a bar so top is 1 */
4cc76151
PR
2484 }
2485 else
2486 {
2487 XEvent *ev = (XEvent *) event;
2488
87a559bf 2489 XtAddGrab ((Widget) mw, True, True);
4cc76151
PR
2490
2491 /* notes the absolute position of the menubar window */
2492 mw->menu.windows [0].x = ev->xmotion.x_root - ev->xmotion.x;
2493 mw->menu.windows [0].y = ev->xmotion.y_root - ev->xmotion.y;
244c93fe 2494 mw->menu.top_depth = 2;
4cc76151
PR
2495 }
2496
d398028f 2497#ifdef emacs
1f5df1f7 2498 x_catch_errors (display);
d398028f 2499#endif
4db7db7d
JD
2500 if (XtGrabPointer ((Widget)mw, False,
2501 (PointerMotionMask
2502 | PointerMotionHintMask
2503 | ButtonReleaseMask
2504 | ButtonPressMask),
2505 GrabModeAsync, GrabModeAsync, None,
2506 mw->menu.cursor_shape,
2507 event->time) == Success)
2508 {
005e0d57 2509 if (! GRAB_KEYBOARD
4db7db7d
JD
2510 || XtGrabKeyboard ((Widget)mw, False, GrabModeAsync,
2511 GrabModeAsync, event->time) == Success)
2512 {
2513 XtSetKeyboardFocus((Widget)mw, None);
2514 pointer_grabbed = 1;
2515 }
2516 else
2517 XtUngrabPointer ((Widget)mw, event->time);
2518 }
2519
d398028f 2520#ifdef emacs
8400b9ed 2521 if (x_had_errors_p (display))
d398028f
PR
2522 {
2523 pointer_grabbed = 0;
2524 XtUngrabPointer ((Widget)mw, event->time);
2525 }
1f5df1f7 2526 x_uncatch_errors ();
d398028f 2527#endif
07bf635f 2528
1f5df1f7 2529 ((XMotionEvent*)event)->is_hint = 0;
07bf635f
RS
2530 handle_motion_event (mw, (XMotionEvent*)event);
2531}
ab5796a9
MB
2532
2533/* arch-tag: 657f43dd-dfd0-4cc9-910c-52935f01176e
2534 (do not change this comment) */