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