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