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