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