Add support for I18N to Lucid menus.
[bpt/emacs.git] / lwlib / xlwmenu.c
CommitLineData
5c520e0a 1/* Implements a lightweight menubar widget.
07bf635f 2 Copyright (C) 1992 Lucid, Inc.
423a3f59 3 Copyright (C) 2002, 2005 Free Software Foundation, Inc.
07bf635f
RS
4
5This file is part of the Lucid Widget Library.
6
5c520e0a 7The Lucid Widget Library is free software; you can redistribute it and/or
07bf635f
RS
8modify it under the terms of the GNU General Public License as published by
9the Free Software Foundation; either version 2, or (at your option)
10any later version.
11
12The Lucid Widget Library is distributed in the hope that it will be useful,
5c520e0a 13but WITHOUT ANY WARRANTY; without even the implied warranty of
07bf635f
RS
14MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15GNU General Public License for more details.
16
17You should have received a copy of the GNU General Public License
1fb87c77
KH
18along with GNU Emacs; see the file COPYING. If not, write to the
19Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20Boston, MA 02111-1307, USA. */
07bf635f
RS
21
22/* Created by devin@lucid.com */
23
0f0912e6
PE
24#ifdef HAVE_CONFIG_H
25#include <config.h>
26#endif
27
404074e2 28#include "lisp.h"
2f96293d 29
07bf635f
RS
30#include <stdio.h>
31
32#include <sys/types.h>
5c520e0a
SS
33#if (defined __sun) && !(defined SUNOS41)
34#define SUNOS41
07bf635f 35#include <X11/Xos.h>
5c520e0a
SS
36#undef SUNOS41
37#else
38#include <X11/Xos.h>
39#endif
07bf635f 40#include <X11/IntrinsicP.h>
2289f3f4 41#include <X11/ObjectP.h>
07bf635f
RS
42#include <X11/StringDefs.h>
43#include <X11/cursorfont.h>
07bf635f 44#include "xlwmenuP.h"
fc8fefed 45
a57a1605 46#ifdef emacs
f1c16db4
GM
47
48/* Defined in xfns.c. When config.h defines `static' as empty, we get
49 redefinition errors when gray_bitmap is included more than once, so
50 we're referring to the one include in xfns.c here. */
51
52extern int gray_bitmap_width;
53extern int gray_bitmap_height;
5916243b 54extern char *gray_bitmap_bits;
f1c16db4 55
a57a1605
DL
56/* Defined in xterm.c. */
57extern int x_alloc_nearest_color_for_widget __P ((Widget, Colormap, XColor*));
65c0ae05
MB
58extern int x_alloc_lighter_color_for_widget __P ((Widget, Display*, Colormap,
59 unsigned long *,
60 double, int));
a57a1605
DL
61extern int x_catch_errors __P ((Display*));
62extern int x_uncatch_errors __P ((Display*, int));
63extern int x_had_errors_p __P ((Display*));
64extern int x_clear_errors __P ((Display*));
fc8fefed
GM
65extern unsigned long x_copy_dpy_color __P ((Display *, Colormap,
66 unsigned long));
67
68/* Defined in xfaces.c. */
69extern void x_free_dpy_colors __P ((Display *, Screen *, Colormap,
70 unsigned long *pixels, int npixels));
f1c16db4
GM
71#else /* not emacs */
72
73#include <X11/bitmaps/gray>
74#define gray_bitmap_width gray_width
75#define gray_bitmap_height gray_height
76#define gray_bitmap_bits gray_bits
77
78#endif /* not emacs */
d398028f
PR
79
80static int pointer_grabbed;
81static XEvent menu_post_event;
07bf635f 82
47d52240
RS
83XFontStruct *xlwmenu_default_font;
84
5c520e0a
SS
85static char
86xlwMenuTranslations [] =
ba624d0f
RS
87"<BtnDown>: start()\n\
88<Motion>: drag()\n\
89<BtnUp>: select()\n\
90<Key>Shift_L: nothing()\n\
91<Key>Shift_R: nothing()\n\
92<Key>Meta_L: nothing()\n\
93<Key>Meta_R: nothing()\n\
94<Key>Control_L: nothing()\n\
95<Key>Control_R: nothing()\n\
96<Key>Hyper_L: nothing()\n\
97<Key>Hyper_R: nothing()\n\
98<Key>Super_L: nothing()\n\
99<Key>Super_R: nothing()\n\
100<Key>Alt_L: nothing()\n\
101<Key>Alt_R: nothing()\n\
102<Key>Caps_Lock: nothing()\n\
103<Key>Shift_Lock: nothing()\n\
104<KeyUp>Shift_L: nothing()\n\
105<KeyUp>Shift_R: nothing()\n\
106<KeyUp>Meta_L: nothing()\n\
107<KeyUp>Meta_R: nothing()\n\
108<KeyUp>Control_L: nothing()\n\
109<KeyUp>Control_R: nothing()\n\
110<KeyUp>Hyper_L: nothing()\n\
111<KeyUp>Hyper_R: nothing()\n\
112<KeyUp>Super_L: nothing()\n\
113<KeyUp>Super_R: nothing()\n\
114<KeyUp>Alt_L: nothing()\n\
115<KeyUp>Alt_R: nothing()\n\
116<KeyUp>Caps_Lock: nothing()\n\
117<KeyUp>Shift_Lock:nothing()\n\
a7e19a26
PJ
118<Key>Return: select()\n\
119<Key>Down: down()\n\
120<Key>Up: up()\n\
121<Key>Left: left()\n\
122<Key>Right: right()\n\
ba624d0f
RS
123<Key>: key()\n\
124<KeyUp>: key()\n\
07bf635f
RS
125";
126
a7e19a26
PJ
127/* FIXME: Space should toggle toggleable menu item but not remove the menu
128 so you can toggle the next one without entering the menu again. */
129
130/* FIXME: Should ESC close one level of menu structure or the complete menu? */
131
2376c841 132/* FIXME: F10 should enter the menu, the first one in the menu-bar. */
a7e19a26 133
07bf635f 134#define offset(field) XtOffset(XlwMenuWidget, field)
5c520e0a 135static XtResource
07bf635f 136xlwMenuResources[] =
5c520e0a 137{
423a3f59
SM
138#ifdef HAVE_X_I18N
139 {XtNfont, XtCFont, XtRFontSet, sizeof(XFontSet),
140 offset(menu.font), XtRString, "XtDefaultFontSet"},
141#else
07bf635f 142 {XtNfont, XtCFont, XtRFontStruct, sizeof(XFontStruct *),
423a3f59
SM
143 offset(menu.font), XtRString, "XtDefaultFont"},
144#endif
07bf635f
RS
145 {XtNforeground, XtCForeground, XtRPixel, sizeof(Pixel),
146 offset(menu.foreground), XtRString, "XtDefaultForeground"},
8b71a9ca
PJ
147 {XtNdisabledForeground, XtCDisabledForeground, XtRPixel, sizeof(Pixel),
148 offset(menu.disabled_foreground), XtRString, (XtPointer)NULL},
07bf635f
RS
149 {XtNbuttonForeground, XtCButtonForeground, XtRPixel, sizeof(Pixel),
150 offset(menu.button_foreground), XtRString, "XtDefaultForeground"},
151 {XtNmargin, XtCMargin, XtRDimension, sizeof(Dimension),
2376c841 152 offset(menu.margin), XtRImmediate, (XtPointer)1},
07bf635f
RS
153 {XtNhorizontalSpacing, XtCMargin, XtRDimension, sizeof(Dimension),
154 offset(menu.horizontal_spacing), XtRImmediate, (XtPointer)3},
155 {XtNverticalSpacing, XtCMargin, XtRDimension, sizeof(Dimension),
2376c841 156 offset(menu.vertical_spacing), XtRImmediate, (XtPointer)2},
07bf635f
RS
157 {XtNarrowSpacing, XtCMargin, XtRDimension, sizeof(Dimension),
158 offset(menu.arrow_spacing), XtRImmediate, (XtPointer)10},
159
d398028f 160 {XmNshadowThickness, XmCShadowThickness, XtRDimension,
07bf635f 161 sizeof (Dimension), offset (menu.shadow_thickness),
2376c841 162 XtRImmediate, (XtPointer)1},
07bf635f
RS
163 {XmNtopShadowColor, XmCTopShadowColor, XtRPixel, sizeof (Pixel),
164 offset (menu.top_shadow_color), XtRImmediate, (XtPointer)-1},
165 {XmNbottomShadowColor, XmCBottomShadowColor, XtRPixel, sizeof (Pixel),
166 offset (menu.bottom_shadow_color), XtRImmediate, (XtPointer)-1},
167 {XmNtopShadowPixmap, XmCTopShadowPixmap, XtRPixmap, sizeof (Pixmap),
168 offset (menu.top_shadow_pixmap), XtRImmediate, (XtPointer)None},
169 {XmNbottomShadowPixmap, XmCBottomShadowPixmap, XtRPixmap, sizeof (Pixmap),
170 offset (menu.bottom_shadow_pixmap), XtRImmediate, (XtPointer)None},
171
5c520e0a 172 {XtNopen, XtCCallback, XtRCallback, sizeof(XtPointer),
07bf635f 173 offset(menu.open), XtRCallback, (XtPointer)NULL},
5c520e0a 174 {XtNselect, XtCCallback, XtRCallback, sizeof(XtPointer),
07bf635f 175 offset(menu.select), XtRCallback, (XtPointer)NULL},
5c520e0a 176 {XtNhighlightCallback, XtCCallback, XtRCallback, sizeof(XtPointer),
d6fd6371 177 offset(menu.highlight), XtRCallback, (XtPointer)NULL},
07bf635f
RS
178 {XtNmenu, XtCMenu, XtRPointer, sizeof(XtPointer),
179 offset(menu.contents), XtRImmediate, (XtPointer)NULL},
180 {XtNcursor, XtCCursor, XtRCursor, sizeof(Cursor),
181 offset(menu.cursor_shape), XtRString, (XtPointer)"right_ptr"},
182 {XtNhorizontal, XtCHorizontal, XtRInt, sizeof(int),
183 offset(menu.horizontal), XtRImmediate, (XtPointer)True},
184};
185#undef offset
186
187static Boolean XlwMenuSetValues();
188static void XlwMenuRealize();
189static void XlwMenuRedisplay();
190static void XlwMenuResize();
191static void XlwMenuInitialize();
192static void XlwMenuRedisplay();
193static void XlwMenuDestroy();
194static void XlwMenuClassInitialize();
195static void Start();
196static void Drag();
a7e19a26
PJ
197static void Down();
198static void Up();
199static void Left();
200static void Right();
07bf635f 201static void Select();
ba624d0f
RS
202static void Key();
203static void Nothing();
8fdc9384 204static int separator_height __P ((enum menu_separator));
244c93fe
JD
205static void pop_up_menu __P ((XlwMenuWidget, XButtonPressedEvent *));
206
07bf635f 207
5c520e0a 208static XtActionsRec
07bf635f
RS
209xlwMenuActionsList [] =
210{
211 {"start", Start},
212 {"drag", Drag},
a7e19a26
PJ
213 {"down", Down},
214 {"up", Up},
215 {"left", Left},
216 {"right", Right},
07bf635f 217 {"select", Select},
ba624d0f 218 {"key", Key},
4220b2a5 219 {"MenuGadgetEscape", Key}, /* Compatibility with Lesstif/Motif. */
ba624d0f 220 {"nothing", Nothing},
07bf635f
RS
221};
222
223#define SuperClass ((CoreWidgetClass)&coreClassRec)
224
225XlwMenuClassRec xlwMenuClassRec =
226{
227 { /* CoreClass fields initialization */
5c520e0a 228 (WidgetClass) SuperClass, /* superclass */
07bf635f
RS
229 "XlwMenu", /* class_name */
230 sizeof(XlwMenuRec), /* size */
231 XlwMenuClassInitialize, /* class_initialize */
232 NULL, /* class_part_initialize */
233 FALSE, /* class_inited */
234 XlwMenuInitialize, /* initialize */
235 NULL, /* initialize_hook */
236 XlwMenuRealize, /* realize */
237 xlwMenuActionsList, /* actions */
238 XtNumber(xlwMenuActionsList), /* num_actions */
239 xlwMenuResources, /* resources */
240 XtNumber(xlwMenuResources), /* resource_count */
241 NULLQUARK, /* xrm_class */
242 TRUE, /* compress_motion */
423a3f59 243 XtExposeCompressMaximal, /* compress_exposure */
07bf635f
RS
244 TRUE, /* compress_enterleave */
245 FALSE, /* visible_interest */
246 XlwMenuDestroy, /* destroy */
247 XlwMenuResize, /* resize */
248 XlwMenuRedisplay, /* expose */
249 XlwMenuSetValues, /* set_values */
250 NULL, /* set_values_hook */
251 XtInheritSetValuesAlmost, /* set_values_almost */
252 NULL, /* get_values_hook */
253 NULL, /* accept_focus */
254 XtVersion, /* version */
255 NULL, /* callback_private */
256 xlwMenuTranslations, /* tm_table */
257 XtInheritQueryGeometry, /* query_geometry */
258 XtInheritDisplayAccelerator, /* display_accelerator */
259 NULL /* extension */
260 }, /* XlwMenuClass fields initialization */
261 {
262 0 /* dummy */
263 },
264};
265
266WidgetClass xlwMenuWidgetClass = (WidgetClass) &xlwMenuClassRec;
267
d398028f
PR
268int submenu_destroyed;
269
005e0d57
JD
270/* For debug, if installation-directory is non-nil this is not an installed
271 Emacs. In that case we do not grab the keyboard to make it easier to
272 debug. */
273#define GRAB_KEYBOARD (EQ (Vinstallation_directory, Qnil))
4db7db7d 274
d398028f
PR
275static int next_release_must_exit;
276
07bf635f 277\f/* Utilities */
be06a3df 278
4db7db7d
JD
279/* Ungrab pointer and keyboard */
280static void
281ungrab_all (w, ungrabtime)
282 Widget w;
283 Time ungrabtime;
284{
285 XtUngrabPointer (w, ungrabtime);
005e0d57 286 if (GRAB_KEYBOARD) XtUngrabKeyboard (w, ungrabtime);
4db7db7d 287}
be06a3df
GM
288
289/* Like abort, but remove grabs from widget W before. */
290
291static void
292abort_gracefully (w)
293 Widget w;
294{
295 if (XtIsShell (XtParent (w)))
296 XtRemoveGrab (w);
4db7db7d 297 ungrab_all (w, CurrentTime);
be06a3df
GM
298 abort ();
299}
300
07bf635f 301static void
d398028f
PR
302push_new_stack (mw, val)
303 XlwMenuWidget mw;
304 widget_value* val;
07bf635f
RS
305{
306 if (!mw->menu.new_stack)
307 {
308 mw->menu.new_stack_length = 10;
309 mw->menu.new_stack =
310 (widget_value**)XtCalloc (mw->menu.new_stack_length,
311 sizeof (widget_value*));
312 }
313 else if (mw->menu.new_depth == mw->menu.new_stack_length)
314 {
315 mw->menu.new_stack_length *= 2;
316 mw->menu.new_stack =
317 (widget_value**)XtRealloc ((char*)mw->menu.new_stack,
318 mw->menu.new_stack_length * sizeof (widget_value*));
319 }
320 mw->menu.new_stack [mw->menu.new_depth++] = val;
321}
322
323static void
d398028f
PR
324pop_new_stack_if_no_contents (mw)
325 XlwMenuWidget mw;
07bf635f 326{
fa74535d 327 if (mw->menu.new_depth > 1)
07bf635f
RS
328 {
329 if (!mw->menu.new_stack [mw->menu.new_depth - 1]->contents)
330 mw->menu.new_depth -= 1;
331 }
332}
333
334static void
d398028f
PR
335make_old_stack_space (mw, n)
336 XlwMenuWidget mw;
337 int n;
07bf635f
RS
338{
339 if (!mw->menu.old_stack)
340 {
341 mw->menu.old_stack_length = 10;
342 mw->menu.old_stack =
343 (widget_value**)XtCalloc (mw->menu.old_stack_length,
344 sizeof (widget_value*));
345 }
346 else if (mw->menu.old_stack_length < n)
347 {
348 mw->menu.old_stack_length *= 2;
349 mw->menu.old_stack =
350 (widget_value**)XtRealloc ((char*)mw->menu.old_stack,
351 mw->menu.old_stack_length * sizeof (widget_value*));
352 }
353}
354
355\f/* Size code */
d398028f
PR
356int
357string_width (mw, s)
358 XlwMenuWidget mw;
359 char *s;
07bf635f 360{
423a3f59
SM
361#ifdef HAVE_X_I18N
362 XRectangle ink, logical;
363 XmbTextExtents (mw->menu.font, s, strlen (s), &ink, &logical);
364 return logical.width;
365#else
07bf635f
RS
366 XCharStruct xcs;
367 int drop;
5c520e0a 368
07bf635f
RS
369 XTextExtents (mw->menu.font, s, strlen (s), &drop, &drop, &drop, &xcs);
370 return xcs.width;
423a3f59 371#endif
07bf635f
RS
372}
373
423a3f59
SM
374#ifdef HAVE_X_I18N
375#define MENU_FONT_HEIGHT(mw) \
376 ((mw)->menu.font_extents->max_logical_extent.height)
377#define MENU_FONT_ASCENT(mw) (MENU_FONT_HEIGHT(mw) * 9 / 10)
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
SM
1051#ifdef HAVE_X_I18N
1052 XmbDrawString (XtDisplay (mw), ws->window, mw->menu.font,
1053#else
1054 XDrawString (XtDisplay (mw), ws->window,
1055#endif
1056 text_gc, x_offset,
07bf635f
RS
1057 y + v_spacing + shadow + font_ascent,
1058 display_string, strlen (display_string));
5c520e0a 1059
07bf635f
RS
1060 if (!horizontal_p)
1061 {
be06a3df
GM
1062 if (val->button_type == BUTTON_TYPE_TOGGLE)
1063 draw_toggle (mw, ws->window, x, y + v_spacing + shadow,
1064 val->selected);
1065 else if (val->button_type == BUTTON_TYPE_RADIO)
1066 draw_radio (mw, ws->window, x, y + v_spacing + shadow,
1067 val->selected);
5c520e0a 1068
07bf635f
RS
1069 if (val->contents)
1070 {
1071 int a_w = arrow_width (mw);
1072 draw_arrow (mw, ws->window, deco_gc,
be06a3df 1073 x + width - a_w
5c520e0a 1074 - mw->menu.horizontal_spacing
d398028f 1075 - mw->menu.shadow_thickness,
be06a3df
GM
1076 y + v_spacing + shadow, a_w,
1077 highlighted_p);
07bf635f
RS
1078 }
1079 else if (val->key)
1080 {
423a3f59
SM
1081#ifdef HAVE_X_I18N
1082 XmbDrawString (XtDisplay (mw), ws->window, mw->menu.font,
1083#else
1084 XDrawString (XtDisplay (mw), ws->window,
1085#endif
1086 text_gc,
07bf635f
RS
1087 x + label_width + mw->menu.arrow_spacing,
1088 y + v_spacing + shadow + font_ascent,
1089 val->key, strlen (val->key));
1090 }
1091 }
d398028f
PR
1092 else
1093 {
5c520e0a 1094 XDrawRectangle (XtDisplay (mw), ws->window,
d398028f
PR
1095 mw->menu.background_gc,
1096 x + shadow, y + shadow,
1097 label_width + h_spacing - 1,
423a3f59 1098 font_height + 2 * v_spacing - 1);
d398028f 1099 draw_shadow_rectangle (mw, ws->window, x, y, width, height,
be06a3df 1100 True, False);
d398028f 1101 }
07bf635f
RS
1102
1103 if (highlighted_p)
be06a3df
GM
1104 draw_shadow_rectangle (mw, ws->window, x, y, width, height, False,
1105 False);
07bf635f
RS
1106 }
1107 }
5c520e0a 1108
07bf635f
RS
1109 where->x += width;
1110 where->y += height;
1111}
1112
1113static void
d398028f
PR
1114display_menu (mw, level, just_compute_p, highlighted_pos, hit, hit_return,
1115 this, that)
1116 XlwMenuWidget mw;
1117 int level;
1118 Boolean just_compute_p;
1119 XPoint* highlighted_pos;
1120 XPoint* hit;
1121 widget_value** hit_return;
1122 widget_value* this;
1123 widget_value* that;
07bf635f
RS
1124{
1125 widget_value* val;
1126 widget_value* following_item;
1127 window_state* ws;
1128 XPoint where;
1129 int horizontal_p = mw->menu.horizontal && (level == 0);
1130 int highlighted_p;
1131 int just_compute_this_one_p;
ccc1cac0
RS
1132 /* This is set nonzero if the element containing HIGHLIGHTED_POS
1133 is disabled, so that we do not return any subsequent element either. */
1134 int no_return = 0;
be06a3df 1135 enum menu_separator separator;
07bf635f
RS
1136
1137 if (level >= mw->menu.old_depth)
be06a3df 1138 abort_gracefully ((Widget) mw);
07bf635f
RS
1139
1140 if (level < mw->menu.old_depth - 1)
1141 following_item = mw->menu.old_stack [level + 1];
5c520e0a 1142 else
07bf635f
RS
1143 following_item = NULL;
1144
1145 if (hit)
1146 *hit_return = NULL;
1147
1148 where.x = 0;
1149 where.y = 0;
1150
1151 ws = &mw->menu.windows [level];
1152 for (val = mw->menu.old_stack [level]->contents; val; val = val->next)
1153 {
1154 highlighted_p = val == following_item;
1155 if (highlighted_p && highlighted_pos)
1156 {
1157 if (horizontal_p)
1158 highlighted_pos->x = where.x;
1159 else
1160 highlighted_pos->y = where.y;
1161 }
5c520e0a 1162
07bf635f
RS
1163 just_compute_this_one_p =
1164 just_compute_p || ((this || that) && val != this && val != that);
1165
1166 display_menu_item (mw, val, ws, &where, highlighted_p, horizontal_p,
1167 just_compute_this_one_p);
1168
1169 if (highlighted_p && highlighted_pos)
1170 {
1171 if (horizontal_p)
1172 highlighted_pos->y = where.y;
1173 else
1174 highlighted_pos->x = where.x;
1175 }
1176
1177 if (hit
1178 && !*hit_return
1179 && (horizontal_p ? hit->x < where.x : hit->y < where.y)
be06a3df 1180 && !lw_separator_p (val->name, &separator, 0)
ccc1cac0
RS
1181 && !no_return)
1182 {
1183 if (val->enabled)
1184 *hit_return = val;
1185 else
1186 no_return = 1;
1187 }
07bf635f
RS
1188
1189 if (horizontal_p)
1190 where.y = 0;
1191 else
1192 where.x = 0;
1193 }
5c520e0a 1194
07bf635f 1195 if (!just_compute_p)
be06a3df
GM
1196 draw_shadow_rectangle (mw, ws->window, 0, 0, ws->width, ws->height,
1197 False, False);
07bf635f
RS
1198}
1199
1200\f/* Motion code */
1201static void
d398028f
PR
1202set_new_state (mw, val, level)
1203 XlwMenuWidget mw;
1204 widget_value* val;
1205 int level;
07bf635f
RS
1206{
1207 int i;
5c520e0a 1208
07bf635f
RS
1209 mw->menu.new_depth = 0;
1210 for (i = 0; i < level; i++)
1211 push_new_stack (mw, mw->menu.old_stack [i]);
1212 push_new_stack (mw, val);
1213}
1214
1215static void
d398028f
PR
1216make_windows_if_needed (mw, n)
1217 XlwMenuWidget mw;
1218 int n;
07bf635f
RS
1219{
1220 int i;
1221 int start_at;
1222 XSetWindowAttributes xswa;
1223 int mask;
1224 Window root = RootWindowOfScreen (DefaultScreenOfDisplay (XtDisplay (mw)));
1225 window_state* windows;
5c520e0a 1226
07bf635f
RS
1227 if (mw->menu.windows_length >= n)
1228 return;
1229
1230 xswa.save_under = True;
1231 xswa.override_redirect = True;
1232 xswa.background_pixel = mw->core.background_pixel;
1233 xswa.border_pixel = mw->core.border_pixel;
1234 xswa.event_mask =
d398028f 1235 ExposureMask | PointerMotionMask | PointerMotionHintMask
07bf635f
RS
1236 | ButtonReleaseMask | ButtonPressMask;
1237 xswa.cursor = mw->menu.cursor_shape;
1238 mask = CWSaveUnder | CWOverrideRedirect | CWBackPixel | CWBorderPixel
1239 | CWEventMask | CWCursor;
5c520e0a 1240
07bf635f
RS
1241 if (!mw->menu.windows)
1242 {
1243 mw->menu.windows =
1244 (window_state*)XtMalloc (n * sizeof (window_state));
1245 start_at = 0;
1246 }
1247 else
1248 {
1249 mw->menu.windows =
1250 (window_state*)XtRealloc ((char*)mw->menu.windows,
1251 n * sizeof (window_state));
1252 start_at = mw->menu.windows_length;
1253 }
1254 mw->menu.windows_length = n;
1255
1256 windows = mw->menu.windows;
1257
1258 for (i = start_at; i < n; i++)
1259 {
1260 windows [i].x = 0;
1261 windows [i].y = 0;
1262 windows [i].width = 1;
1263 windows [i].height = 1;
1264 windows [i].window =
1265 XCreateWindow (XtDisplay (mw), root, 0, 0, 1, 1,
1266 0, 0, CopyFromParent, CopyFromParent, mask, &xswa);
1267 }
1268}
1269
2a692ba4
GM
1270/* Value is non-zero if WINDOW is part of menu bar widget W. */
1271
1272int
1273xlwmenu_window_p (w, window)
1274 Widget w;
1275 Window window;
1276{
1277 XlwMenuWidget mw = (XlwMenuWidget) w;
1278 int i;
177c0ea7 1279
2a692ba4
GM
1280 for (i = 0; i < mw->menu.windows_length; ++i)
1281 if (window == mw->menu.windows[i].window)
1282 break;
1283
1284 return i < mw->menu.windows_length;
1285}
1286
07bf635f
RS
1287/* Make the window fit in the screen */
1288static void
d398028f
PR
1289fit_to_screen (mw, ws, previous_ws, horizontal_p)
1290 XlwMenuWidget mw;
1291 window_state* ws;
1292 window_state* previous_ws;
1293 Boolean horizontal_p;
07bf635f 1294{
ccf589a6
RS
1295 unsigned int screen_width = WidthOfScreen (XtScreen (mw));
1296 unsigned int screen_height = HeightOfScreen (XtScreen (mw));
1ba46f7d
RS
1297 /* 1 if we are unable to avoid an overlap between
1298 this menu and the parent menu in the X dimension. */
1299 int horizontal_overlap = 0;
07bf635f
RS
1300
1301 if (ws->x < 0)
1302 ws->x = 0;
1303 else if (ws->x + ws->width > screen_width)
1304 {
1305 if (!horizontal_p)
b7fa4a06
MB
1306 /* The addition of shadow-thickness for a sub-menu's position is
1307 to reflect a similar adjustment when the menu is displayed to
1308 the right of the invoking menu-item; it makes the sub-menu
1309 look more `attached' to the menu-item. */
1310 ws->x = previous_ws->x - ws->width + mw->menu.shadow_thickness;
07bf635f
RS
1311 else
1312 ws->x = screen_width - ws->width;
525b8232 1313 if (ws->x < 0)
1ba46f7d
RS
1314 {
1315 ws->x = 0;
1316 horizontal_overlap = 1;
1317 }
1318 }
1319 /* If we overlap in X, try to avoid overlap in Y. */
1320 if (horizontal_overlap
1321 && ws->y < previous_ws->y + previous_ws->height
1322 && previous_ws->y < ws->y + ws->height)
1323 {
1324 /* Put this menu right below or right above PREVIOUS_WS
1325 if there's room. */
1326 if (previous_ws->y + previous_ws->height + ws->height < screen_height)
1327 ws->y = previous_ws->y + previous_ws->height;
1328 else if (previous_ws->y - ws->height > 0)
1329 ws->y = previous_ws->y - ws->height;
07bf635f 1330 }
1ba46f7d 1331
07bf635f
RS
1332 if (ws->y < 0)
1333 ws->y = 0;
1334 else if (ws->y + ws->height > screen_height)
1335 {
1336 if (horizontal_p)
1337 ws->y = previous_ws->y - ws->height;
1338 else
1339 ws->y = screen_height - ws->height;
5c520e0a 1340 if (ws->y < 0)
525b8232 1341 ws->y = 0;
07bf635f
RS
1342 }
1343}
1344
1345/* Updates old_stack from new_stack and redisplays. */
1346static void
d398028f
PR
1347remap_menubar (mw)
1348 XlwMenuWidget mw;
07bf635f
RS
1349{
1350 int i;
1351 int last_same;
1352 XPoint selection_position;
1353 int old_depth = mw->menu.old_depth;
1354 int new_depth = mw->menu.new_depth;
1355 widget_value** old_stack;
1356 widget_value** new_stack;
1357 window_state* windows;
1358 widget_value* old_selection;
1359 widget_value* new_selection;
1360
1361 /* Check that enough windows and old_stack are ready. */
1362 make_windows_if_needed (mw, new_depth);
1363 make_old_stack_space (mw, new_depth);
1364 windows = mw->menu.windows;
1365 old_stack = mw->menu.old_stack;
1366 new_stack = mw->menu.new_stack;
1367
1368 /* compute the last identical different entry */
1369 for (i = 1; i < old_depth && i < new_depth; i++)
1370 if (old_stack [i] != new_stack [i])
1371 break;
1372 last_same = i - 1;
1373
1374 /* Memorize the previously selected item to be able to refresh it */
1375 old_selection = last_same + 1 < old_depth ? old_stack [last_same + 1] : NULL;
1376 if (old_selection && !old_selection->enabled)
1377 old_selection = NULL;
1378 new_selection = last_same + 1 < new_depth ? new_stack [last_same + 1] : NULL;
1379 if (new_selection && !new_selection->enabled)
1380 new_selection = NULL;
1381
d6fd6371
GM
1382 /* Call callback when the hightlighted item changes. */
1383 if (old_selection || new_selection)
1384 XtCallCallbackList ((Widget)mw, mw->menu.highlight,
1385 (XtPointer) new_selection);
1386
07bf635f
RS
1387 /* updates old_state from new_state. It has to be done now because
1388 display_menu (called below) uses the old_stack to know what to display. */
1389 for (i = last_same + 1; i < new_depth; i++)
1390 old_stack [i] = new_stack [i];
1391 mw->menu.old_depth = new_depth;
1392
908ff139 1393 /* refresh the last selection */
07bf635f
RS
1394 selection_position.x = 0;
1395 selection_position.y = 0;
1396 display_menu (mw, last_same, new_selection == old_selection,
1397 &selection_position, NULL, NULL, old_selection, new_selection);
1398
1ba46f7d
RS
1399 /* Now place the new menus. */
1400 for (i = last_same + 1; i < new_depth && new_stack[i]->contents; i++)
07bf635f 1401 {
1ba46f7d
RS
1402 window_state *previous_ws = &windows[i - 1];
1403 window_state *ws = &windows[i];
07bf635f 1404
be06a3df
GM
1405 ws->x = (previous_ws->x + selection_position.x
1406 + mw->menu.shadow_thickness);
b7fa4a06 1407 if (mw->menu.horizontal && i == 1)
be06a3df
GM
1408 ws->x += mw->menu.margin;
1409
1410#if 0
07bf635f
RS
1411 if (!mw->menu.horizontal || i > 1)
1412 ws->x += mw->menu.shadow_thickness;
be06a3df 1413#endif
5c520e0a 1414
be06a3df
GM
1415 ws->y = (previous_ws->y + selection_position.y
1416 + mw->menu.shadow_thickness);
b7fa4a06 1417 if (mw->menu.horizontal && i == 1)
be06a3df 1418 ws->y += mw->menu.margin;
07bf635f
RS
1419
1420 size_menu (mw, i);
1421
1422 fit_to_screen (mw, ws, previous_ws, mw->menu.horizontal && i == 1);
1423
1424 XClearWindow (XtDisplay (mw), ws->window);
1425 XMoveResizeWindow (XtDisplay (mw), ws->window, ws->x, ws->y,
1426 ws->width, ws->height);
1427 XMapRaised (XtDisplay (mw), ws->window);
1428 display_menu (mw, i, False, &selection_position, NULL, NULL, NULL, NULL);
1429 }
1430
1431 /* unmap the menus that popped down */
1432 for (i = new_depth - 1; i < old_depth; i++)
fa74535d 1433 if (i >= new_depth || (i > 0 && !new_stack[i]->contents))
1ba46f7d 1434 XUnmapWindow (XtDisplay (mw), windows[i].window);
07bf635f
RS
1435}
1436
1437static Boolean
d398028f
PR
1438motion_event_is_in_menu (mw, ev, level, relative_pos)
1439 XlwMenuWidget mw;
1440 XMotionEvent* ev;
1441 int level;
1442 XPoint* relative_pos;
07bf635f
RS
1443{
1444 window_state* ws = &mw->menu.windows [level];
4f08464c
RS
1445 int shadow = level == 0 ? 0 : mw->menu.shadow_thickness;
1446 int x = ws->x + shadow;
1447 int y = ws->y + shadow;
07bf635f
RS
1448 relative_pos->x = ev->x_root - x;
1449 relative_pos->y = ev->y_root - y;
4f08464c
RS
1450 return (x - shadow < ev->x_root && ev->x_root < x + ws->width
1451 && y - shadow < ev->y_root && ev->y_root < y + ws->height);
07bf635f
RS
1452}
1453
1454static Boolean
d398028f
PR
1455map_event_to_widget_value (mw, ev, val, level)
1456 XlwMenuWidget mw;
1457 XMotionEvent* ev;
1458 widget_value** val;
1459 int* level;
07bf635f
RS
1460{
1461 int i;
1462 XPoint relative_pos;
1463 window_state* ws;
1464
1465 *val = NULL;
5c520e0a 1466
07bf635f
RS
1467 /* Find the window */
1468 for (i = mw->menu.old_depth - 1; i >= 0; i--)
1469 {
1470 ws = &mw->menu.windows [i];
1471 if (ws && motion_event_is_in_menu (mw, ev, i, &relative_pos))
1472 {
1473 display_menu (mw, i, True, NULL, &relative_pos, val, NULL, NULL);
1474
1475 if (*val)
1476 {
1477 *level = i + 1;
1478 return True;
1479 }
1480 }
1481 }
1482 return False;
1483}
1484
1485\f/* Procedures */
1486static void
d398028f
PR
1487make_drawing_gcs (mw)
1488 XlwMenuWidget mw;
07bf635f
RS
1489{
1490 XGCValues xgcv;
ef93458b 1491 float scale;
07bf635f 1492
423a3f59 1493#ifndef HAVE_X_I18N
07bf635f 1494 xgcv.font = mw->menu.font->fid;
423a3f59 1495#endif
07bf635f
RS
1496 xgcv.foreground = mw->menu.foreground;
1497 xgcv.background = mw->core.background_pixel;
1498 mw->menu.foreground_gc = XtGetGC ((Widget)mw,
423a3f59
SM
1499#ifndef HAVE_X_I18N
1500 GCFont |
1501#endif
1502 GCForeground | GCBackground,
07bf635f 1503 &xgcv);
5c520e0a 1504
423a3f59 1505#ifndef HAVE_X_I18N
07bf635f 1506 xgcv.font = mw->menu.font->fid;
423a3f59 1507#endif
07bf635f
RS
1508 xgcv.foreground = mw->menu.button_foreground;
1509 xgcv.background = mw->core.background_pixel;
1510 mw->menu.button_gc = XtGetGC ((Widget)mw,
423a3f59
SM
1511#ifndef HAVE_X_I18N
1512 GCFont |
1513#endif
1514 GCForeground | GCBackground,
07bf635f 1515 &xgcv);
5c520e0a 1516
423a3f59 1517#ifndef HAVE_X_I18N
07bf635f 1518 xgcv.font = mw->menu.font->fid;
423a3f59 1519#endif
07bf635f 1520 xgcv.background = mw->core.background_pixel;
8b71a9ca
PJ
1521
1522#define BRIGHTNESS(color) (((color) & 0xff) + (((color) >> 8) & 0xff) + (((color) >> 16) & 0xff))
1523
1524 /* Allocate color for disabled menu-items. */
ef93458b 1525 mw->menu.disabled_foreground = mw->menu.foreground;
8b71a9ca 1526 if (BRIGHTNESS(mw->menu.foreground) < BRIGHTNESS(mw->core.background_pixel))
ef93458b 1527 scale = 2.3;
8b71a9ca 1528 else
ef93458b 1529 scale = 0.55;
8b71a9ca
PJ
1530
1531 x_alloc_lighter_color_for_widget ((Widget) mw, XtDisplay ((Widget) mw),
1532 mw->core.colormap,
ef93458b
PJ
1533 &mw->menu.disabled_foreground,
1534 scale,
8b71a9ca 1535 0x8000);
8b71a9ca
PJ
1536
1537 if (mw->menu.foreground == mw->menu.disabled_foreground
1538 || mw->core.background_pixel == mw->menu.disabled_foreground)
1539 {
1540 /* Too few colors, use stipple. */
1541 xgcv.foreground = mw->menu.foreground;
1542 xgcv.fill_style = FillStippled;
1543 xgcv.stipple = mw->menu.gray_pixmap;
1544 mw->menu.disabled_gc = XtGetGC ((Widget)mw,
423a3f59
SM
1545#ifndef HAVE_X_I18N
1546 GCFont |
1547#endif
1548 GCForeground | GCBackground
1549 | GCFillStyle | GCStipple, &xgcv);
8b71a9ca
PJ
1550 }
1551 else
1552 {
1553 /* Many colors available, use disabled pixel. */
1554 xgcv.foreground = mw->menu.disabled_foreground;
1555 mw->menu.disabled_gc = XtGetGC ((Widget)mw,
423a3f59
SM
1556#ifndef HAVE_X_I18N
1557 GCFont |
1558#endif
1559 GCForeground | GCBackground, &xgcv);
8b71a9ca 1560 }
5c520e0a 1561
423a3f59 1562#ifndef HAVE_X_I18N
07bf635f 1563 xgcv.font = mw->menu.font->fid;
423a3f59 1564#endif
07bf635f
RS
1565 xgcv.foreground = mw->menu.button_foreground;
1566 xgcv.background = mw->core.background_pixel;
1567 xgcv.fill_style = FillStippled;
1568 xgcv.stipple = mw->menu.gray_pixmap;
1569 mw->menu.inactive_button_gc = XtGetGC ((Widget)mw,
423a3f59
SM
1570#ifndef HAVE_X_I18N
1571 GCFont |
1572#endif
1573 GCForeground | GCBackground
1574 | GCFillStyle | GCStipple, &xgcv);
5c520e0a 1575
423a3f59 1576#ifndef HAVE_X_I18N
07bf635f 1577 xgcv.font = mw->menu.font->fid;
423a3f59 1578#endif
07bf635f
RS
1579 xgcv.foreground = mw->core.background_pixel;
1580 xgcv.background = mw->menu.foreground;
1581 mw->menu.background_gc = XtGetGC ((Widget)mw,
423a3f59
SM
1582#ifndef HAVE_X_I18N
1583 GCFont |
1584#endif
1585 GCForeground | GCBackground,
07bf635f
RS
1586 &xgcv);
1587}
1588
1589static void
d398028f
PR
1590release_drawing_gcs (mw)
1591 XlwMenuWidget mw;
07bf635f
RS
1592{
1593 XtReleaseGC ((Widget) mw, mw->menu.foreground_gc);
1594 XtReleaseGC ((Widget) mw, mw->menu.button_gc);
8b71a9ca 1595 XtReleaseGC ((Widget) mw, mw->menu.disabled_gc);
07bf635f
RS
1596 XtReleaseGC ((Widget) mw, mw->menu.inactive_button_gc);
1597 XtReleaseGC ((Widget) mw, mw->menu.background_gc);
1598 /* let's get some segvs if we try to use these... */
1599 mw->menu.foreground_gc = (GC) -1;
1600 mw->menu.button_gc = (GC) -1;
8b71a9ca 1601 mw->menu.disabled_gc = (GC) -1;
07bf635f
RS
1602 mw->menu.inactive_button_gc = (GC) -1;
1603 mw->menu.background_gc = (GC) -1;
1604}
1605
1606#define MINL(x,y) ((((unsigned long) (x)) < ((unsigned long) (y))) \
1607 ? ((unsigned long) (x)) : ((unsigned long) (y)))
1608
1609static void
d398028f
PR
1610make_shadow_gcs (mw)
1611 XlwMenuWidget mw;
07bf635f
RS
1612{
1613 XGCValues xgcv;
1614 unsigned long pm = 0;
1615 Display *dpy = XtDisplay ((Widget) mw);
be06a3df 1616 Screen *screen = XtScreen ((Widget) mw);
b0404f9f 1617 Colormap cmap = mw->core.colormap;
07bf635f
RS
1618 XColor topc, botc;
1619 int top_frobbed = 0, bottom_frobbed = 0;
1620
bba2a923
GM
1621 mw->menu.free_top_shadow_color_p = 0;
1622 mw->menu.free_bottom_shadow_color_p = 0;
1623
07bf635f
RS
1624 if (mw->menu.top_shadow_color == -1)
1625 mw->menu.top_shadow_color = mw->core.background_pixel;
bba2a923
GM
1626 else
1627 mw->menu.top_shadow_color = mw->menu.top_shadow_color;
5c520e0a 1628
07bf635f
RS
1629 if (mw->menu.bottom_shadow_color == -1)
1630 mw->menu.bottom_shadow_color = mw->menu.foreground;
bba2a923
GM
1631 else
1632 mw->menu.bottom_shadow_color = mw->menu.bottom_shadow_color;
07bf635f
RS
1633
1634 if (mw->menu.top_shadow_color == mw->core.background_pixel ||
1635 mw->menu.top_shadow_color == mw->menu.foreground)
1636 {
1637 topc.pixel = mw->core.background_pixel;
65c0ae05
MB
1638#ifdef emacs
1639 if (x_alloc_lighter_color_for_widget ((Widget) mw, dpy, cmap,
1640 &topc.pixel,
1641 1.2, 0x8000))
1642#else
07bf635f
RS
1643 XQueryColor (dpy, cmap, &topc);
1644 /* don't overflow/wrap! */
1645 topc.red = MINL (65535, topc.red * 1.2);
1646 topc.green = MINL (65535, topc.green * 1.2);
1647 topc.blue = MINL (65535, topc.blue * 1.2);
1648 if (XAllocColor (dpy, cmap, &topc))
be06a3df 1649#endif
07bf635f
RS
1650 {
1651 mw->menu.top_shadow_color = topc.pixel;
bba2a923 1652 mw->menu.free_top_shadow_color_p = 1;
07bf635f
RS
1653 top_frobbed = 1;
1654 }
1655 }
1656 if (mw->menu.bottom_shadow_color == mw->menu.foreground ||
1657 mw->menu.bottom_shadow_color == mw->core.background_pixel)
1658 {
1659 botc.pixel = mw->core.background_pixel;
65c0ae05
MB
1660#ifdef emacs
1661 if (x_alloc_lighter_color_for_widget ((Widget) mw, dpy, cmap,
1662 &botc.pixel,
1663 0.6, 0x4000))
1664#else
07bf635f
RS
1665 XQueryColor (dpy, cmap, &botc);
1666 botc.red *= 0.6;
1667 botc.green *= 0.6;
1668 botc.blue *= 0.6;
1669 if (XAllocColor (dpy, cmap, &botc))
be06a3df 1670#endif
07bf635f
RS
1671 {
1672 mw->menu.bottom_shadow_color = botc.pixel;
bba2a923 1673 mw->menu.free_bottom_shadow_color_p = 1;
07bf635f
RS
1674 bottom_frobbed = 1;
1675 }
1676 }
1677
1678 if (top_frobbed && bottom_frobbed)
1679 {
65c0ae05 1680 if (topc.pixel == botc.pixel)
07bf635f
RS
1681 {
1682 if (botc.pixel == mw->menu.foreground)
fc8fefed 1683 {
bba2a923
GM
1684 if (mw->menu.free_top_shadow_color_p)
1685 {
1686 x_free_dpy_colors (dpy, screen, cmap,
1687 &mw->menu.top_shadow_color, 1);
1688 mw->menu.free_top_shadow_color_p = 0;
1689 }
1690 mw->menu.top_shadow_color = mw->core.background_pixel;
fc8fefed 1691 }
07bf635f 1692 else
fc8fefed 1693 {
bba2a923
GM
1694 if (mw->menu.free_bottom_shadow_color_p)
1695 {
1696 x_free_dpy_colors (dpy, screen, cmap,
1697 &mw->menu.bottom_shadow_color, 1);
1698 mw->menu.free_bottom_shadow_color_p = 0;
1699 }
1700 mw->menu.bottom_shadow_color = mw->menu.foreground;
fc8fefed 1701 }
07bf635f
RS
1702 }
1703 }
1704
1705 if (!mw->menu.top_shadow_pixmap &&
1706 mw->menu.top_shadow_color == mw->core.background_pixel)
1707 {
1708 mw->menu.top_shadow_pixmap = mw->menu.gray_pixmap;
bba2a923
GM
1709 if (mw->menu.free_top_shadow_color_p)
1710 {
1711 x_free_dpy_colors (dpy, screen, cmap, &mw->menu.top_shadow_color, 1);
1712 mw->menu.free_top_shadow_color_p = 0;
1713 }
1714 mw->menu.top_shadow_color = mw->menu.foreground;
07bf635f
RS
1715 }
1716 if (!mw->menu.bottom_shadow_pixmap &&
1717 mw->menu.bottom_shadow_color == mw->core.background_pixel)
1718 {
1719 mw->menu.bottom_shadow_pixmap = mw->menu.gray_pixmap;
bba2a923
GM
1720 if (mw->menu.free_bottom_shadow_color_p)
1721 {
1722 x_free_dpy_colors (dpy, screen, cmap,
1723 &mw->menu.bottom_shadow_color, 1);
1724 mw->menu.free_bottom_shadow_color_p = 0;
1725 }
1726 mw->menu.bottom_shadow_color = mw->menu.foreground;
07bf635f
RS
1727 }
1728
1729 xgcv.fill_style = FillStippled;
1730 xgcv.foreground = mw->menu.top_shadow_color;
1731 xgcv.stipple = mw->menu.top_shadow_pixmap;
1732 pm = (xgcv.stipple ? GCStipple|GCFillStyle : 0);
1733 mw->menu.shadow_top_gc = XtGetGC ((Widget)mw, GCForeground | pm, &xgcv);
1734
1735 xgcv.foreground = mw->menu.bottom_shadow_color;
1736 xgcv.stipple = mw->menu.bottom_shadow_pixmap;
1737 pm = (xgcv.stipple ? GCStipple|GCFillStyle : 0);
1738 mw->menu.shadow_bottom_gc = XtGetGC ((Widget)mw, GCForeground | pm, &xgcv);
1739}
1740
1741
1742static void
d398028f
PR
1743release_shadow_gcs (mw)
1744 XlwMenuWidget mw;
07bf635f 1745{
fc8fefed
GM
1746 Display *dpy = XtDisplay ((Widget) mw);
1747 Screen *screen = XtScreen ((Widget) mw);
1748 Colormap cmap = mw->core.colormap;
1749 Pixel px[2];
bba2a923
GM
1750 int i = 0;
1751
1752 if (mw->menu.free_top_shadow_color_p)
1753 px[i++] = mw->menu.top_shadow_color;
1754 if (mw->menu.free_bottom_shadow_color_p)
1755 px[i++] = mw->menu.bottom_shadow_color;
1756 if (i > 0)
1757 x_free_dpy_colors (dpy, screen, cmap, px, i);
5c520e0a 1758
07bf635f
RS
1759 XtReleaseGC ((Widget) mw, mw->menu.shadow_top_gc);
1760 XtReleaseGC ((Widget) mw, mw->menu.shadow_bottom_gc);
1761}
1762
1763static void
47d52240 1764XlwMenuInitialize (request, mw, args, num_args)
d398028f 1765 Widget request;
47d52240 1766 XlwMenuWidget mw;
d398028f
PR
1767 ArgList args;
1768 Cardinal *num_args;
07bf635f
RS
1769{
1770 /* Get the GCs and the widget size */
5c520e0a 1771
07bf635f
RS
1772 Window window = RootWindowOfScreen (DefaultScreenOfDisplay (XtDisplay (mw)));
1773 Display* display = XtDisplay (mw);
5c520e0a 1774
d398028f
PR
1775#if 0
1776 widget_value *tem = (widget_value *) XtMalloc (sizeof (widget_value));
1777
1778 /* _XtCreate is freeing the object that was passed to us,
1779 so make a copy that we will actually keep. */
1780 lwlib_bcopy (mw->menu.contents, tem, sizeof (widget_value));
1781 mw->menu.contents = tem;
1782#endif
1783
07bf635f
RS
1784/* mw->menu.cursor = XCreateFontCursor (display, mw->menu.cursor_shape); */
1785 mw->menu.cursor = mw->menu.cursor_shape;
5c520e0a 1786
a504c8fa 1787 mw->menu.gray_pixmap
f1c16db4
GM
1788 = XCreatePixmapFromBitmapData (display, window, gray_bitmap_bits,
1789 gray_bitmap_width, gray_bitmap_height,
a504c8fa 1790 (unsigned long)1, (unsigned long)0, 1);
5c520e0a 1791
423a3f59 1792#ifndef HAVE_X_I18N
47d52240
RS
1793 /* I don't understand why this ends up 0 sometimes,
1794 but it does. This kludge works around it.
1795 Can anyone find a real fix? -- rms. */
1796 if (mw->menu.font == 0)
1797 mw->menu.font = xlwmenu_default_font;
423a3f59
SM
1798#else
1799 mw->menu.font_extents = XExtentsOfFontSet (mw->menu.font);
1800#endif
1801
07bf635f
RS
1802 make_drawing_gcs (mw);
1803 make_shadow_gcs (mw);
5c520e0a 1804
07bf635f 1805 mw->menu.popped_up = False;
5c520e0a 1806
07bf635f
RS
1807 mw->menu.old_depth = 1;
1808 mw->menu.old_stack = (widget_value**)XtMalloc (sizeof (widget_value*));
1809 mw->menu.old_stack_length = 1;
1810 mw->menu.old_stack [0] = mw->menu.contents;
5c520e0a 1811
07bf635f
RS
1812 mw->menu.new_depth = 0;
1813 mw->menu.new_stack = 0;
1814 mw->menu.new_stack_length = 0;
1815 push_new_stack (mw, mw->menu.contents);
5c520e0a 1816
07bf635f
RS
1817 mw->menu.windows = (window_state*)XtMalloc (sizeof (window_state));
1818 mw->menu.windows_length = 1;
1819 mw->menu.windows [0].x = 0;
1820 mw->menu.windows [0].y = 0;
1821 mw->menu.windows [0].width = 0;
1822 mw->menu.windows [0].height = 0;
1823 size_menu (mw, 0);
5c520e0a 1824
07bf635f
RS
1825 mw->core.width = mw->menu.windows [0].width;
1826 mw->core.height = mw->menu.windows [0].height;
1827}
1828
1829static void
1830XlwMenuClassInitialize ()
1831{
1832}
1833
1834static void
d398028f
PR
1835XlwMenuRealize (w, valueMask, attributes)
1836 Widget w;
1837 Mask *valueMask;
1838 XSetWindowAttributes *attributes;
07bf635f
RS
1839{
1840 XlwMenuWidget mw = (XlwMenuWidget)w;
1841 XSetWindowAttributes xswa;
1842 int mask;
1843
1844 (*xlwMenuWidgetClass->core_class.superclass->core_class.realize)
1845 (w, valueMask, attributes);
1846
1847 xswa.save_under = True;
1848 xswa.cursor = mw->menu.cursor_shape;
1849 mask = CWSaveUnder | CWCursor;
1850 XChangeWindowAttributes (XtDisplay (w), XtWindow (w), mask, &xswa);
1851
1852 mw->menu.windows [0].window = XtWindow (w);
1853 mw->menu.windows [0].x = w->core.x;
1854 mw->menu.windows [0].y = w->core.y;
1855 mw->menu.windows [0].width = w->core.width;
1856 mw->menu.windows [0].height = w->core.height;
1857}
1858
1859/* Only the toplevel menubar/popup is a widget so it's the only one that
1860 receives expose events through Xt. So we repaint all the other panes
1861 when receiving an Expose event. */
5c520e0a 1862static void
d398028f
PR
1863XlwMenuRedisplay (w, ev, region)
1864 Widget w;
1865 XEvent* ev;
1866 Region region;
07bf635f
RS
1867{
1868 XlwMenuWidget mw = (XlwMenuWidget)w;
1869 int i;
1870
d398028f
PR
1871 /* If we have a depth beyond 1, it's because a submenu was displayed.
1872 If the submenu has been destroyed, set the depth back to 1. */
1873 if (submenu_destroyed)
1874 {
1875 mw->menu.old_depth = 1;
1876 submenu_destroyed = 0;
1877 }
1878
07bf635f
RS
1879 for (i = 0; i < mw->menu.old_depth; i++)
1880 display_menu (mw, i, False, NULL, NULL, NULL, NULL, NULL);
1881}
1882
f3c9e544
GM
1883
1884/* Part of a hack to make the menu redisplay when a tooltip frame
1885 over a menu item is unmapped. */
1886
1887void
1888xlwmenu_redisplay (w)
1889 Widget w;
1890{
1891 XlwMenuRedisplay (w, NULL, None);
1892}
1893
5c520e0a 1894static void
d398028f
PR
1895XlwMenuDestroy (w)
1896 Widget w;
07bf635f
RS
1897{
1898 int i;
1899 XlwMenuWidget mw = (XlwMenuWidget) w;
1900
d398028f 1901 if (pointer_grabbed)
4db7db7d 1902 ungrab_all ((Widget)w, CurrentTime);
d398028f
PR
1903 pointer_grabbed = 0;
1904
1905 submenu_destroyed = 1;
1906
07bf635f
RS
1907 release_drawing_gcs (mw);
1908 release_shadow_gcs (mw);
1909
1910 /* this doesn't come from the resource db but is created explicitly
1911 so we must free it ourselves. */
1912 XFreePixmap (XtDisplay (mw), mw->menu.gray_pixmap);
1913 mw->menu.gray_pixmap = (Pixmap) -1;
1914
d398028f
PR
1915#if 0
1916 /* Do free mw->menu.contents because nowadays we copy it
1917 during initialization. */
1918 XtFree (mw->menu.contents);
1919#endif
1920
07bf635f
RS
1921 /* Don't free mw->menu.contents because that comes from our creator.
1922 The `*_stack' elements are just pointers into `contents' so leave
1923 that alone too. But free the stacks themselves. */
1924 if (mw->menu.old_stack) XtFree ((char *) mw->menu.old_stack);
1925 if (mw->menu.new_stack) XtFree ((char *) mw->menu.new_stack);
1926
1927 /* Remember, you can't free anything that came from the resource
1928 database. This includes:
1929 mw->menu.cursor
1930 mw->menu.top_shadow_pixmap
1931 mw->menu.bottom_shadow_pixmap
1932 mw->menu.font
1933 Also the color cells of top_shadow_color, bottom_shadow_color,
1934 foreground, and button_foreground will never be freed until this
1935 client exits. Nice, eh?
1936 */
1937
1938 /* start from 1 because the one in slot 0 is w->core.window */
1939 for (i = 1; i < mw->menu.windows_length; i++)
1940 XDestroyWindow (XtDisplay (mw), mw->menu.windows [i].window);
1941 if (mw->menu.windows)
1942 XtFree ((char *) mw->menu.windows);
1943}
1944
5c520e0a 1945static Boolean
d398028f
PR
1946XlwMenuSetValues (current, request, new)
1947 Widget current;
1948 Widget request;
1949 Widget new;
07bf635f
RS
1950{
1951 XlwMenuWidget oldmw = (XlwMenuWidget)current;
1952 XlwMenuWidget newmw = (XlwMenuWidget)new;
1953 Boolean redisplay = False;
1954 int i;
1955
1956 if (newmw->menu.contents
1957 && newmw->menu.contents->contents
1958 && newmw->menu.contents->contents->change >= VISIBLE_CHANGE)
1959 redisplay = True;
ba624d0f
RS
1960 /* Do redisplay if the contents are entirely eliminated. */
1961 if (newmw->menu.contents
1962 && newmw->menu.contents->contents == 0
1963 && newmw->menu.contents->change >= VISIBLE_CHANGE)
1964 redisplay = True;
07bf635f
RS
1965
1966 if (newmw->core.background_pixel != oldmw->core.background_pixel
d398028f 1967 || newmw->menu.foreground != oldmw->menu.foreground
423a3f59
SM
1968#ifndef HAVE_X_I18N
1969 || newmw->menu.font != oldmw->menu.font
1970#endif
1971 )
07bf635f
RS
1972 {
1973 release_drawing_gcs (newmw);
1974 make_drawing_gcs (newmw);
90c7e9f0
MB
1975
1976 release_shadow_gcs (newmw);
1977 /* Cause the shadow colors to be recalculated. */
1978 newmw->menu.top_shadow_color = -1;
1979 newmw->menu.bottom_shadow_color = -1;
1980 make_shadow_gcs (newmw);
1981
07bf635f 1982 redisplay = True;
5c520e0a 1983
43aa2f1b 1984 if (XtIsRealized (current))
da353f23
MB
1985 /* If the menu is currently displayed, change the display. */
1986 for (i = 0; i < oldmw->menu.windows_length; i++)
1987 {
1988 XSetWindowBackground (XtDisplay (oldmw),
1989 oldmw->menu.windows [i].window,
1990 newmw->core.background_pixel);
1991 /* clear windows and generate expose events */
1992 XClearArea (XtDisplay (oldmw), oldmw->menu.windows[i].window,
1993 0, 0, 0, 0, True);
1994 }
07bf635f
RS
1995 }
1996
423a3f59
SM
1997#ifdef HAVE_X_I18N
1998 if (newmw->menu.font != oldmw->menu.font)
1999 {
2000 redisplay = True;
2001 newmw->menu.font_extents = XExtentsOfFontSet (newmw->menu.font);
2002 }
2003#endif
2004
07bf635f
RS
2005 return redisplay;
2006}
2007
5c520e0a 2008static void
d398028f
PR
2009XlwMenuResize (w)
2010 Widget w;
07bf635f
RS
2011{
2012 XlwMenuWidget mw = (XlwMenuWidget)w;
2013
d398028f
PR
2014 if (mw->menu.popped_up)
2015 {
2016 /* Don't allow the popup menu to resize itself. */
2017 mw->core.width = mw->menu.windows [0].width;
2018 mw->core.height = mw->menu.windows [0].height;
2019 mw->core.parent->core.width = mw->core.width ;
2020 mw->core.parent->core.height = mw->core.height ;
2021 }
2022 else
2023 {
2024 mw->menu.windows [0].width = mw->core.width;
2025 mw->menu.windows [0].height = mw->core.height;
2026 }
07bf635f
RS
2027}
2028
2029\f/* Action procedures */
2030static void
d398028f
PR
2031handle_single_motion_event (mw, ev)
2032 XlwMenuWidget mw;
2033 XMotionEvent* ev;
07bf635f
RS
2034{
2035 widget_value* val;
2036 int level;
2037
2038 if (!map_event_to_widget_value (mw, ev, &val, &level))
2039 pop_new_stack_if_no_contents (mw);
2040 else
2041 set_new_state (mw, val, level);
2042 remap_menubar (mw);
5c520e0a 2043
07bf635f
RS
2044 /* Sync with the display. Makes it feel better on X terms. */
2045 XSync (XtDisplay (mw), False);
2046}
2047
2048static void
d398028f
PR
2049handle_motion_event (mw, ev)
2050 XlwMenuWidget mw;
2051 XMotionEvent* ev;
07bf635f
RS
2052{
2053 int x = ev->x_root;
2054 int y = ev->y_root;
2055 int state = ev->state;
2056
2057 handle_single_motion_event (mw, ev);
2058
2059 /* allow motion events to be generated again */
2060 if (ev->is_hint
2061 && XQueryPointer (XtDisplay (mw), ev->window,
2062 &ev->root, &ev->subwindow,
2063 &ev->x_root, &ev->y_root,
2064 &ev->x, &ev->y,
2065 &ev->state)
2066 && ev->state == state
2067 && (ev->x_root != x || ev->y_root != y))
2068 handle_single_motion_event (mw, ev);
2069}
2070
5c520e0a 2071static void
d398028f
PR
2072Start (w, ev, params, num_params)
2073 Widget w;
2074 XEvent *ev;
2075 String *params;
2076 Cardinal *num_params;
07bf635f
RS
2077{
2078 XlwMenuWidget mw = (XlwMenuWidget)w;
2079
d398028f
PR
2080 if (!mw->menu.popped_up)
2081 {
2082 menu_post_event = *ev;
244c93fe
JD
2083 /* If event is set to CurrentTime, get the last known time stamp.
2084 This is for calculating if (popup) menus should stay up after
2085 a fast click. */
2086 if (menu_post_event.xbutton.time == CurrentTime)
2087 menu_post_event.xbutton.time
2088 = XtLastTimestampProcessed (XtDisplay (w));
2089
a57a1605 2090 pop_up_menu (mw, (XButtonPressedEvent*) ev);
d398028f
PR
2091 }
2092 else
4cc76151
PR
2093 {
2094 /* If we push a button while the menu is posted semipermanently,
2095 releasing the button should always pop the menu down. */
2096 next_release_must_exit = 1;
d398028f 2097
4cc76151
PR
2098 /* notes the absolute position of the menubar window */
2099 mw->menu.windows [0].x = ev->xmotion.x_root - ev->xmotion.x;
2100 mw->menu.windows [0].y = ev->xmotion.y_root - ev->xmotion.y;
07bf635f 2101
4cc76151
PR
2102 /* handles the down like a move, slots are compatible */
2103 handle_motion_event (mw, &ev->xmotion);
2104 }
07bf635f
RS
2105}
2106
5c520e0a 2107static void
d398028f
PR
2108Drag (w, ev, params, num_params)
2109 Widget w;
2110 XEvent *ev;
2111 String *params;
2112 Cardinal *num_params;
07bf635f
RS
2113{
2114 XlwMenuWidget mw = (XlwMenuWidget)w;
3c9ce1c4
KH
2115 if (mw->menu.popped_up)
2116 handle_motion_event (mw, &ev->xmotion);
07bf635f
RS
2117}
2118
ba624d0f
RS
2119/* Do nothing.
2120 This is how we handle presses and releases of modifier keys. */
2121static void
2122Nothing (w, ev, params, num_params)
2123 Widget w;
2124 XEvent *ev;
2125 String *params;
2126 Cardinal *num_params;
2127{
2128}
2129
244c93fe 2130static widget_value *
03a693b4 2131find_first_selectable (mw, item, skip_titles)
1b8d91ab
PJ
2132 XlwMenuWidget mw;
2133 widget_value *item;
03a693b4 2134 int skip_titles;
1b8d91ab
PJ
2135{
2136 widget_value *current = item;
2137 enum menu_separator separator;
2138
244c93fe 2139 while (lw_separator_p (current->name, &separator, 0) || !current->enabled
03a693b4 2140 || (skip_titles && !current->call_data && !current->contents))
1b8d91ab
PJ
2141 if (current->next)
2142 current=current->next;
2143 else
03a693b4 2144 return NULL;
1b8d91ab
PJ
2145
2146 return current;
2147}
2148
244c93fe 2149static widget_value *
03a693b4 2150find_next_selectable (mw, item, skip_titles)
a7e19a26
PJ
2151 XlwMenuWidget mw;
2152 widget_value *item;
2153{
2154 widget_value *current = item;
2155 enum menu_separator separator;
2156
2157 while (current->next && (current=current->next) &&
244c93fe 2158 (lw_separator_p (current->name, &separator, 0) || !current->enabled
03a693b4 2159 || (skip_titles && !current->call_data && !current->contents)))
a7e19a26
PJ
2160 ;
2161
2162 if (current == item)
2163 {
8b71a9ca
PJ
2164 if (mw->menu.old_depth < 2)
2165 return current;
a7e19a26
PJ
2166 current = mw->menu.old_stack [mw->menu.old_depth - 2]->contents;
2167
244c93fe
JD
2168 while (lw_separator_p (current->name, &separator, 0)
2169 || !current->enabled
03a693b4
JD
2170 || (skip_titles && !current->call_data
2171 && !current->contents))
1b8d91ab
PJ
2172 {
2173 if (current->next)
2174 current=current->next;
2175
2176 if (current == item)
2177 break;
2178 }
2179
a7e19a26
PJ
2180 }
2181
2182 return current;
2183}
2184
244c93fe 2185static widget_value *
03a693b4 2186find_prev_selectable (mw, item, skip_titles)
a7e19a26
PJ
2187 XlwMenuWidget mw;
2188 widget_value *item;
2189{
2190 widget_value *current = item;
2191 widget_value *prev = item;
2192
03a693b4 2193 while ((current=find_next_selectable (mw, current, skip_titles))
244c93fe 2194 != item)
1b8d91ab
PJ
2195 {
2196 if (prev == current)
2197 break;
a7e19a26 2198 prev=current;
1b8d91ab 2199 }
a7e19a26
PJ
2200
2201 return prev;
2202}
2203
2204static void
2205Down (w, ev, params, num_params)
2206 Widget w;
2207 XEvent *ev;
2208 String *params;
2209 Cardinal *num_params;
2210{
2211 XlwMenuWidget mw = (XlwMenuWidget) w;
2212 widget_value* selected_item = mw->menu.old_stack [mw->menu.old_depth - 1];
244c93fe 2213 int popup_menu_p = mw->menu.top_depth == 1;
a7e19a26
PJ
2214
2215 /* Inside top-level menu-bar? */
244c93fe 2216 if (mw->menu.old_depth == mw->menu.top_depth)
a7e19a26 2217 /* When <down> in the menu-bar is pressed, display the corresponding
244c93fe 2218 sub-menu and select the first selectable menu item there.
03a693b4 2219 If this is a popup menu, skip title item of the popup. */
244c93fe
JD
2220 set_new_state (mw,
2221 find_first_selectable (mw,
2222 selected_item->contents,
2223 popup_menu_p),
2224 mw->menu.old_depth);
a7e19a26
PJ
2225 else
2226 /* Highlight next possible (enabled and not separator) menu item. */
244c93fe
JD
2227 set_new_state (mw, find_next_selectable (mw, selected_item, popup_menu_p),
2228 mw->menu.old_depth - 1);
a7e19a26
PJ
2229
2230 remap_menubar (mw);
2231}
2232
2233static void
2234Up (w, ev, params, num_params)
2235 Widget w;
2236 XEvent *ev;
2237 String *params;
2238 Cardinal *num_params;
2239{
2240 XlwMenuWidget mw = (XlwMenuWidget) w;
2241 widget_value* selected_item = mw->menu.old_stack [mw->menu.old_depth - 1];
244c93fe 2242 int popup_menu_p = mw->menu.top_depth == 1;
a7e19a26
PJ
2243
2244 /* Inside top-level menu-bar? */
244c93fe 2245 if (mw->menu.old_depth == mw->menu.top_depth)
a7e19a26
PJ
2246 {
2247 /* FIXME: this is tricky. <up> in the menu-bar should select the
1b8d91ab
PJ
2248 last selectable item in the list. So we select the first
2249 selectable one and find the previous selectable item. Is there
2250 a better way? */
03a693b4 2251 /* If this is a popup menu, skip title item of the popup. */
244c93fe
JD
2252 set_new_state (mw,
2253 find_first_selectable (mw,
2254 selected_item->contents,
2255 popup_menu_p),
2256 mw->menu.old_depth);
a7e19a26
PJ
2257 remap_menubar (mw);
2258 selected_item = mw->menu.old_stack [mw->menu.old_depth - 1];
244c93fe
JD
2259 set_new_state (mw,
2260 find_prev_selectable (mw,
2261 selected_item,
2262 popup_menu_p),
2263 mw->menu.old_depth - 1);
a7e19a26
PJ
2264 }
2265 else
2266 /* Highlight previous (enabled and not separator) menu item. */
244c93fe
JD
2267 set_new_state (mw, find_prev_selectable (mw, selected_item, popup_menu_p),
2268 mw->menu.old_depth - 1);
a7e19a26
PJ
2269
2270 remap_menubar (mw);
2271}
2272
244c93fe 2273void
a7e19a26
PJ
2274Left (w, ev, params, num_params)
2275 Widget w;
2276 XEvent *ev;
2277 String *params;
2278 Cardinal *num_params;
2279{
2280 XlwMenuWidget mw = (XlwMenuWidget) w;
2281 widget_value* selected_item = mw->menu.old_stack [mw->menu.old_depth - 1];
2282
2283 /* Inside top-level menu-bar? */
244c93fe 2284 if (mw->menu.old_depth == mw->menu.top_depth)
a7e19a26
PJ
2285 /* When <left> in the menu-bar is pressed, display the previous item on
2286 the menu-bar. If the current item is the first one, highlight the
2287 last item in the menubar (probably Help). */
244c93fe
JD
2288 set_new_state (mw, find_prev_selectable (mw, selected_item, 0),
2289 mw->menu.old_depth - 1);
fa74535d
RS
2290 else if (mw->menu.old_depth == 1
2291 && selected_item->contents) /* Is this menu item expandable? */
2292 {
2293 set_new_state (mw, selected_item->contents, mw->menu.old_depth);
2294 remap_menubar (mw);
2295 selected_item = mw->menu.old_stack [mw->menu.old_depth - 1];
244c93fe
JD
2296 if (!selected_item->enabled && find_first_selectable (mw,
2297 selected_item,
2298 0))
2299 set_new_state (mw, find_first_selectable (mw, selected_item, 0),
2300 mw->menu.old_depth - 1);
fa74535d
RS
2301 }
2302
a7e19a26
PJ
2303 else
2304 {
2305 pop_new_stack_if_no_contents (mw);
244c93fe
JD
2306 set_new_state (mw, mw->menu.old_stack [mw->menu.old_depth - 2],
2307 mw->menu.old_depth - 2);
a7e19a26
PJ
2308 }
2309
2310 remap_menubar (mw);
2311}
2312
244c93fe 2313void
a7e19a26
PJ
2314Right (w, ev, params, num_params)
2315 Widget w;
2316 XEvent *ev;
2317 String *params;
2318 Cardinal *num_params;
2319{
2320 XlwMenuWidget mw = (XlwMenuWidget) w;
2321 widget_value* selected_item = mw->menu.old_stack [mw->menu.old_depth - 1];
2322
2323 /* Inside top-level menu-bar? */
244c93fe 2324 if (mw->menu.old_depth == mw->menu.top_depth)
a7e19a26
PJ
2325 /* When <right> in the menu-bar is pressed, display the next item on
2326 the menu-bar. If the current item is the last one, highlight the
2327 first item (probably File). */
244c93fe
JD
2328 set_new_state (mw, find_next_selectable (mw, selected_item, 0),
2329 mw->menu.old_depth - 1);
a7e19a26 2330 else if (selected_item->contents) /* Is this menu item expandable? */
1b8d91ab
PJ
2331 {
2332 set_new_state (mw, selected_item->contents, mw->menu.old_depth);
2333 remap_menubar (mw);
2334 selected_item = mw->menu.old_stack [mw->menu.old_depth - 1];
244c93fe
JD
2335 if (!selected_item->enabled && find_first_selectable (mw,
2336 selected_item,
2337 0))
2338 set_new_state (mw, find_first_selectable (mw, selected_item, 0),
2339 mw->menu.old_depth - 1);
1b8d91ab 2340 }
a7e19a26
PJ
2341 else
2342 {
2343 pop_new_stack_if_no_contents (mw);
244c93fe
JD
2344 set_new_state (mw, mw->menu.old_stack [mw->menu.old_depth - 2],
2345 mw->menu.old_depth - 2);
a7e19a26
PJ
2346 }
2347
2348 remap_menubar (mw);
2349}
2350
ba624d0f
RS
2351/* Handle key press and release events while menu is popped up.
2352 Our action is to get rid of the menu. */
2353static void
2354Key (w, ev, params, num_params)
2355 Widget w;
2356 XEvent *ev;
2357 String *params;
2358 Cardinal *num_params;
2359{
2360 XlwMenuWidget mw = (XlwMenuWidget)w;
2361
2362 /* Pop down everything. */
2363 mw->menu.new_depth = 1;
2364 remap_menubar (mw);
2365
2366 if (mw->menu.popped_up)
2367 {
2368 mw->menu.popped_up = False;
4db7db7d 2369 ungrab_all ((Widget)mw, ev->xmotion.time);
ba624d0f
RS
2370 if (XtIsShell (XtParent ((Widget) mw)))
2371 XtPopdown (XtParent ((Widget) mw));
2372 else
2373 {
2374 XtRemoveGrab ((Widget) mw);
2375 display_menu (mw, 0, False, NULL, NULL, NULL, NULL, NULL);
2376 }
2377 }
2378
2379 /* callback */
2380 XtCallCallbackList ((Widget)mw, mw->menu.select, (XtPointer)0);
2381}
2382
2383static void
d398028f
PR
2384Select (w, ev, params, num_params)
2385 Widget w;
2386 XEvent *ev;
2387 String *params;
2388 Cardinal *num_params;
07bf635f
RS
2389{
2390 XlwMenuWidget mw = (XlwMenuWidget)w;
2391 widget_value* selected_item = mw->menu.old_stack [mw->menu.old_depth - 1];
5c520e0a 2392
d398028f
PR
2393 /* If user releases the button quickly, without selecting anything,
2394 after the initial down-click that brought the menu up,
2395 do nothing. */
2396 if ((selected_item == 0
2397 || ((widget_value *) selected_item)->call_data == 0)
2398 && !next_release_must_exit
2399 && (ev->xbutton.time - menu_post_event.xbutton.time
2400 < XtGetMultiClickTime (XtDisplay (w))))
2401 return;
2402
2403 /* pop down everything. */
07bf635f
RS
2404 mw->menu.new_depth = 1;
2405 remap_menubar (mw);
2406
2407 if (mw->menu.popped_up)
2408 {
2409 mw->menu.popped_up = False;
4db7db7d 2410 ungrab_all ((Widget)mw, ev->xmotion.time);
2289f3f4
PR
2411 if (XtIsShell (XtParent ((Widget) mw)))
2412 XtPopdown (XtParent ((Widget) mw));
4cc76151
PR
2413 else
2414 {
2415 XtRemoveGrab ((Widget) mw);
2416 display_menu (mw, 0, False, NULL, NULL, NULL, NULL, NULL);
2417 }
07bf635f
RS
2418 }
2419
2420 /* callback */
2421 XtCallCallbackList ((Widget)mw, mw->menu.select, (XtPointer)selected_item);
07bf635f
RS
2422}
2423
2424
2425\f/* Special code to pop-up a menu */
244c93fe 2426static void
d398028f
PR
2427pop_up_menu (mw, event)
2428 XlwMenuWidget mw;
2429 XButtonPressedEvent* event;
07bf635f
RS
2430{
2431 int x = event->x_root;
2432 int y = event->y_root;
2433 int w;
2434 int h;
2435 int borderwidth = mw->menu.shadow_thickness;
2436 Screen* screen = XtScreen (mw);
8400b9ed 2437 Display *display = XtDisplay (mw);
5efb61c7 2438 int count;
07bf635f 2439
d398028f
PR
2440 next_release_must_exit = 0;
2441
07bf635f
RS
2442 XtCallCallbackList ((Widget)mw, mw->menu.open, NULL);
2443
2289f3f4 2444 if (XtIsShell (XtParent ((Widget)mw)))
4cc76151 2445 size_menu (mw, 0);
07bf635f
RS
2446
2447 w = mw->menu.windows [0].width;
2448 h = mw->menu.windows [0].height;
2449
2450 x -= borderwidth;
2451 y -= borderwidth;
2452 if (x < borderwidth)
2453 x = borderwidth;
2454 if (x + w + 2 * borderwidth > WidthOfScreen (screen))
2455 x = WidthOfScreen (screen) - w - 2 * borderwidth;
2456 if (y < borderwidth)
2457 y = borderwidth;
2458 if (y + h + 2 * borderwidth> HeightOfScreen (screen))
2459 y = HeightOfScreen (screen) - h - 2 * borderwidth;
2460
2461 mw->menu.popped_up = True;
2289f3f4 2462 if (XtIsShell (XtParent ((Widget)mw)))
4cc76151 2463 {
2289f3f4
PR
2464 XtConfigureWidget (XtParent ((Widget)mw), x, y, w, h,
2465 XtParent ((Widget)mw)->core.border_width);
2466 XtPopup (XtParent ((Widget)mw), XtGrabExclusive);
4cc76151
PR
2467 display_menu (mw, 0, False, NULL, NULL, NULL, NULL, NULL);
2468 mw->menu.windows [0].x = x + borderwidth;
2469 mw->menu.windows [0].y = y + borderwidth;
244c93fe 2470 mw->menu.top_depth = 1; /* Popup menus don't have a bar so top is 1 */
4cc76151
PR
2471 }
2472 else
2473 {
2474 XEvent *ev = (XEvent *) event;
2475
87a559bf 2476 XtAddGrab ((Widget) mw, True, True);
4cc76151
PR
2477
2478 /* notes the absolute position of the menubar window */
2479 mw->menu.windows [0].x = ev->xmotion.x_root - ev->xmotion.x;
2480 mw->menu.windows [0].y = ev->xmotion.y_root - ev->xmotion.y;
244c93fe 2481 mw->menu.top_depth = 2;
4cc76151
PR
2482 }
2483
d398028f 2484#ifdef emacs
5efb61c7 2485 count = x_catch_errors (display);
d398028f 2486#endif
4db7db7d
JD
2487 if (XtGrabPointer ((Widget)mw, False,
2488 (PointerMotionMask
2489 | PointerMotionHintMask
2490 | ButtonReleaseMask
2491 | ButtonPressMask),
2492 GrabModeAsync, GrabModeAsync, None,
2493 mw->menu.cursor_shape,
2494 event->time) == Success)
2495 {
005e0d57 2496 if (! GRAB_KEYBOARD
4db7db7d
JD
2497 || XtGrabKeyboard ((Widget)mw, False, GrabModeAsync,
2498 GrabModeAsync, event->time) == Success)
2499 {
2500 XtSetKeyboardFocus((Widget)mw, None);
2501 pointer_grabbed = 1;
2502 }
2503 else
2504 XtUngrabPointer ((Widget)mw, event->time);
2505 }
2506
d398028f 2507#ifdef emacs
8400b9ed 2508 if (x_had_errors_p (display))
d398028f
PR
2509 {
2510 pointer_grabbed = 0;
2511 XtUngrabPointer ((Widget)mw, event->time);
2512 }
5efb61c7 2513 x_uncatch_errors (display, count);
d398028f 2514#endif
07bf635f 2515
07bf635f
RS
2516 handle_motion_event (mw, (XMotionEvent*)event);
2517}
ab5796a9
MB
2518
2519/* arch-tag: 657f43dd-dfd0-4cc9-910c-52935f01176e
2520 (do not change this comment) */