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