(XlwMenuSetValues): Pass the correct pointer type to XtIsRealized.
[bpt/emacs.git] / lwlib / xlwmenu.c
CommitLineData
07bf635f
RS
1/* Implements a lightweight menubar widget.
2 Copyright (C) 1992 Lucid, Inc.
3
4This file is part of the Lucid Widget Library.
5
6The Lucid Widget Library is free software; you can redistribute it and/or
7modify it under the terms of the GNU General Public License as published by
8the Free Software Foundation; either version 2, or (at your option)
9any later version.
10
11The Lucid Widget Library is distributed in the hope that it will be useful,
12but WITHOUT ANY WARRANTY; without even the implied warranty of
13MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14GNU General Public License for more details.
15
16You should have received a copy of the GNU General Public License
1fb87c77
KH
17along with GNU Emacs; see the file COPYING. If not, write to the
18Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19Boston, MA 02111-1307, USA. */
07bf635f
RS
20
21/* Created by devin@lucid.com */
22
0f0912e6
PE
23#ifdef HAVE_CONFIG_H
24#include <config.h>
25#endif
26
07bf635f
RS
27#include <stdio.h>
28
29#include <sys/types.h>
30#include <X11/Xos.h>
31#include <X11/IntrinsicP.h>
2289f3f4 32#include <X11/ObjectP.h>
07bf635f
RS
33#include <X11/StringDefs.h>
34#include <X11/cursorfont.h>
07bf635f 35#include "xlwmenuP.h"
fc8fefed 36
a57a1605 37#ifdef emacs
f1c16db4
GM
38
39/* Defined in xfns.c. When config.h defines `static' as empty, we get
40 redefinition errors when gray_bitmap is included more than once, so
41 we're referring to the one include in xfns.c here. */
42
43extern int gray_bitmap_width;
44extern int gray_bitmap_height;
45extern unsigned char *gray_bitmap_bits;
46
a57a1605
DL
47/* Defined in xterm.c. */
48extern int x_alloc_nearest_color_for_widget __P ((Widget, Colormap, XColor*));
65c0ae05
MB
49extern int x_alloc_lighter_color_for_widget __P ((Widget, Display*, Colormap,
50 unsigned long *,
51 double, int));
a57a1605
DL
52extern int x_catch_errors __P ((Display*));
53extern int x_uncatch_errors __P ((Display*, int));
54extern int x_had_errors_p __P ((Display*));
55extern int x_clear_errors __P ((Display*));
fc8fefed
GM
56extern unsigned long x_copy_dpy_color __P ((Display *, Colormap,
57 unsigned long));
58
59/* Defined in xfaces.c. */
60extern void x_free_dpy_colors __P ((Display *, Screen *, Colormap,
61 unsigned long *pixels, int npixels));
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
47d52240
RS
74XFontStruct *xlwmenu_default_font;
75
07bf635f
RS
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\
109<Key>: key()\n\
110<KeyUp>: key()\n\
07bf635f
RS
111";
112
113#define offset(field) XtOffset(XlwMenuWidget, field)
114static XtResource
115xlwMenuResources[] =
116{
117 {XtNfont, XtCFont, XtRFontStruct, sizeof(XFontStruct *),
118 offset(menu.font),XtRString, "XtDefaultFont"},
119 {XtNforeground, XtCForeground, XtRPixel, sizeof(Pixel),
120 offset(menu.foreground), XtRString, "XtDefaultForeground"},
121 {XtNbuttonForeground, XtCButtonForeground, XtRPixel, sizeof(Pixel),
122 offset(menu.button_foreground), XtRString, "XtDefaultForeground"},
123 {XtNmargin, XtCMargin, XtRDimension, sizeof(Dimension),
be06a3df 124 offset(menu.margin), XtRImmediate, (XtPointer) 4},
07bf635f
RS
125 {XtNhorizontalSpacing, XtCMargin, XtRDimension, sizeof(Dimension),
126 offset(menu.horizontal_spacing), XtRImmediate, (XtPointer)3},
127 {XtNverticalSpacing, XtCMargin, XtRDimension, sizeof(Dimension),
128 offset(menu.vertical_spacing), XtRImmediate, (XtPointer)1},
129 {XtNarrowSpacing, XtCMargin, XtRDimension, sizeof(Dimension),
130 offset(menu.arrow_spacing), XtRImmediate, (XtPointer)10},
131
d398028f 132 {XmNshadowThickness, XmCShadowThickness, XtRDimension,
07bf635f
RS
133 sizeof (Dimension), offset (menu.shadow_thickness),
134 XtRImmediate, (XtPointer) 2},
135 {XmNtopShadowColor, XmCTopShadowColor, XtRPixel, sizeof (Pixel),
136 offset (menu.top_shadow_color), XtRImmediate, (XtPointer)-1},
137 {XmNbottomShadowColor, XmCBottomShadowColor, XtRPixel, sizeof (Pixel),
138 offset (menu.bottom_shadow_color), XtRImmediate, (XtPointer)-1},
139 {XmNtopShadowPixmap, XmCTopShadowPixmap, XtRPixmap, sizeof (Pixmap),
140 offset (menu.top_shadow_pixmap), XtRImmediate, (XtPointer)None},
141 {XmNbottomShadowPixmap, XmCBottomShadowPixmap, XtRPixmap, sizeof (Pixmap),
142 offset (menu.bottom_shadow_pixmap), XtRImmediate, (XtPointer)None},
143
144 {XtNopen, XtCCallback, XtRCallback, sizeof(XtPointer),
145 offset(menu.open), XtRCallback, (XtPointer)NULL},
146 {XtNselect, XtCCallback, XtRCallback, sizeof(XtPointer),
147 offset(menu.select), XtRCallback, (XtPointer)NULL},
d6fd6371
GM
148 {XtNhighlightCallback, XtCCallback, XtRCallback, sizeof(XtPointer),
149 offset(menu.highlight), XtRCallback, (XtPointer)NULL},
07bf635f
RS
150 {XtNmenu, XtCMenu, XtRPointer, sizeof(XtPointer),
151 offset(menu.contents), XtRImmediate, (XtPointer)NULL},
152 {XtNcursor, XtCCursor, XtRCursor, sizeof(Cursor),
153 offset(menu.cursor_shape), XtRString, (XtPointer)"right_ptr"},
154 {XtNhorizontal, XtCHorizontal, XtRInt, sizeof(int),
155 offset(menu.horizontal), XtRImmediate, (XtPointer)True},
156};
157#undef offset
158
159static Boolean XlwMenuSetValues();
160static void XlwMenuRealize();
161static void XlwMenuRedisplay();
162static void XlwMenuResize();
163static void XlwMenuInitialize();
164static void XlwMenuRedisplay();
165static void XlwMenuDestroy();
166static void XlwMenuClassInitialize();
167static void Start();
168static void Drag();
169static void Select();
ba624d0f
RS
170static void Key();
171static void Nothing();
be06a3df 172static int separator_height ();
07bf635f
RS
173
174static XtActionsRec
175xlwMenuActionsList [] =
176{
177 {"start", Start},
178 {"drag", Drag},
179 {"select", Select},
ba624d0f
RS
180 {"key", Key},
181 {"nothing", Nothing},
07bf635f
RS
182};
183
184#define SuperClass ((CoreWidgetClass)&coreClassRec)
185
186XlwMenuClassRec xlwMenuClassRec =
187{
188 { /* CoreClass fields initialization */
189 (WidgetClass) SuperClass, /* superclass */
190 "XlwMenu", /* class_name */
191 sizeof(XlwMenuRec), /* size */
192 XlwMenuClassInitialize, /* class_initialize */
193 NULL, /* class_part_initialize */
194 FALSE, /* class_inited */
195 XlwMenuInitialize, /* initialize */
196 NULL, /* initialize_hook */
197 XlwMenuRealize, /* realize */
198 xlwMenuActionsList, /* actions */
199 XtNumber(xlwMenuActionsList), /* num_actions */
200 xlwMenuResources, /* resources */
201 XtNumber(xlwMenuResources), /* resource_count */
202 NULLQUARK, /* xrm_class */
203 TRUE, /* compress_motion */
204 TRUE, /* compress_exposure */
205 TRUE, /* compress_enterleave */
206 FALSE, /* visible_interest */
207 XlwMenuDestroy, /* destroy */
208 XlwMenuResize, /* resize */
209 XlwMenuRedisplay, /* expose */
210 XlwMenuSetValues, /* set_values */
211 NULL, /* set_values_hook */
212 XtInheritSetValuesAlmost, /* set_values_almost */
213 NULL, /* get_values_hook */
214 NULL, /* accept_focus */
215 XtVersion, /* version */
216 NULL, /* callback_private */
217 xlwMenuTranslations, /* tm_table */
218 XtInheritQueryGeometry, /* query_geometry */
219 XtInheritDisplayAccelerator, /* display_accelerator */
220 NULL /* extension */
221 }, /* XlwMenuClass fields initialization */
222 {
223 0 /* dummy */
224 },
225};
226
227WidgetClass xlwMenuWidgetClass = (WidgetClass) &xlwMenuClassRec;
228
d398028f
PR
229int submenu_destroyed;
230
231static int next_release_must_exit;
232
07bf635f 233\f/* Utilities */
be06a3df
GM
234
235
236/* Like abort, but remove grabs from widget W before. */
237
238static void
239abort_gracefully (w)
240 Widget w;
241{
242 if (XtIsShell (XtParent (w)))
243 XtRemoveGrab (w);
244 XtUngrabPointer (w, CurrentTime);
245 abort ();
246}
247
07bf635f 248static void
d398028f
PR
249push_new_stack (mw, val)
250 XlwMenuWidget mw;
251 widget_value* val;
07bf635f
RS
252{
253 if (!mw->menu.new_stack)
254 {
255 mw->menu.new_stack_length = 10;
256 mw->menu.new_stack =
257 (widget_value**)XtCalloc (mw->menu.new_stack_length,
258 sizeof (widget_value*));
259 }
260 else if (mw->menu.new_depth == mw->menu.new_stack_length)
261 {
262 mw->menu.new_stack_length *= 2;
263 mw->menu.new_stack =
264 (widget_value**)XtRealloc ((char*)mw->menu.new_stack,
265 mw->menu.new_stack_length * sizeof (widget_value*));
266 }
267 mw->menu.new_stack [mw->menu.new_depth++] = val;
268}
269
270static void
d398028f
PR
271pop_new_stack_if_no_contents (mw)
272 XlwMenuWidget mw;
07bf635f
RS
273{
274 if (mw->menu.new_depth)
275 {
276 if (!mw->menu.new_stack [mw->menu.new_depth - 1]->contents)
277 mw->menu.new_depth -= 1;
278 }
279}
280
281static void
d398028f
PR
282make_old_stack_space (mw, n)
283 XlwMenuWidget mw;
284 int n;
07bf635f
RS
285{
286 if (!mw->menu.old_stack)
287 {
288 mw->menu.old_stack_length = 10;
289 mw->menu.old_stack =
290 (widget_value**)XtCalloc (mw->menu.old_stack_length,
291 sizeof (widget_value*));
292 }
293 else if (mw->menu.old_stack_length < n)
294 {
295 mw->menu.old_stack_length *= 2;
296 mw->menu.old_stack =
297 (widget_value**)XtRealloc ((char*)mw->menu.old_stack,
298 mw->menu.old_stack_length * sizeof (widget_value*));
299 }
300}
301
302\f/* Size code */
d398028f
PR
303int
304string_width (mw, s)
305 XlwMenuWidget mw;
306 char *s;
07bf635f
RS
307{
308 XCharStruct xcs;
309 int drop;
310
311 XTextExtents (mw->menu.font, s, strlen (s), &drop, &drop, &drop, &xcs);
312 return xcs.width;
313}
314
315static int
d398028f
PR
316arrow_width (mw)
317 XlwMenuWidget mw;
07bf635f 318{
be06a3df 319 return (mw->menu.font->ascent * 3/4) | 1;
07bf635f
RS
320}
321
be06a3df
GM
322/* Return the width of toggle buttons of widget MW. */
323
324static int
325toggle_button_width (mw)
326 XlwMenuWidget mw;
327{
328 return ((mw->menu.font->ascent + mw->menu.font->descent) * 2 / 3) | 1;
329}
330
331
332/* Return the width of radio buttons of widget MW. */
333
334static int
335radio_button_width (mw)
336 XlwMenuWidget mw;
337{
338 return toggle_button_width (mw) * 1.41;
339}
340
341
07bf635f
RS
342static XtResource
343nameResource[] =
344{
345 {"labelString", "LabelString", XtRString, sizeof(String),
346 0, XtRImmediate, 0},
347};
348
349static char*
d398028f
PR
350resource_widget_value (mw, val)
351 XlwMenuWidget mw;
352 widget_value *val;
07bf635f
RS
353{
354 if (!val->toolkit_data)
355 {
356 char* resourced_name = NULL;
357 char* complete_name;
358 XtGetSubresources ((Widget) mw,
359 (XtPointer) &resourced_name,
360 val->name, val->name,
361 nameResource, 1, NULL, 0);
362 if (!resourced_name)
363 resourced_name = val->name;
364 if (!val->value)
d97342f3
RS
365 {
366 complete_name = (char *) XtMalloc (strlen (resourced_name) + 1);
367 strcpy (complete_name, resourced_name);
368 }
07bf635f
RS
369 else
370 {
371 int complete_length =
372 strlen (resourced_name) + strlen (val->value) + 2;
373 complete_name = XtMalloc (complete_length);
374 *complete_name = 0;
375 strcat (complete_name, resourced_name);
376 strcat (complete_name, " ");
377 strcat (complete_name, val->value);
378 }
379
380 val->toolkit_data = complete_name;
381 val->free_toolkit_data = True;
382 }
383 return (char*)val->toolkit_data;
384}
385
386/* Returns the sizes of an item */
387static void
be06a3df
GM
388size_menu_item (mw, val, horizontal_p, label_width, rest_width, button_width,
389 height)
d398028f
PR
390 XlwMenuWidget mw;
391 widget_value* val;
392 int horizontal_p;
393 int* label_width;
394 int* rest_width;
be06a3df 395 int* button_width;
d398028f 396 int* height;
07bf635f 397{
be06a3df
GM
398 enum menu_separator separator;
399
400 if (lw_separator_p (val->name, &separator, 0))
07bf635f 401 {
be06a3df 402 *height = separator_height (separator);
07bf635f
RS
403 *label_width = 1;
404 *rest_width = 0;
be06a3df 405 *button_width = 0;
07bf635f
RS
406 }
407 else
408 {
409 *height =
410 mw->menu.font->ascent + mw->menu.font->descent
411 + 2 * mw->menu.vertical_spacing + 2 * mw->menu.shadow_thickness;
412
413 *label_width =
414 string_width (mw, resource_widget_value (mw, val))
415 + mw->menu.horizontal_spacing + mw->menu.shadow_thickness;
416
417 *rest_width = mw->menu.horizontal_spacing + mw->menu.shadow_thickness;
418 if (!horizontal_p)
419 {
420 if (val->contents)
be06a3df 421 /* Add width of the arrow displayed for submenus. */
07bf635f
RS
422 *rest_width += arrow_width (mw) + mw->menu.arrow_spacing;
423 else if (val->key)
be06a3df
GM
424 /* Add width of key equivalent string. */
425 *rest_width += (string_width (mw, val->key)
426 + mw->menu.arrow_spacing);
427
428 if (val->button_type == BUTTON_TYPE_TOGGLE)
429 *button_width = (toggle_button_width (mw)
430 + mw->menu.horizontal_spacing);
431 else if (val->button_type == BUTTON_TYPE_RADIO)
432 *button_width = (radio_button_width (mw)
433 + mw->menu.horizontal_spacing);
07bf635f
RS
434 }
435 }
436}
437
438static void
d398028f
PR
439size_menu (mw, level)
440 XlwMenuWidget mw;
441 int level;
07bf635f 442{
ccf589a6 443 unsigned int label_width = 0;
07bf635f 444 int rest_width = 0;
be06a3df 445 int button_width = 0;
07bf635f 446 int max_rest_width = 0;
be06a3df 447 int max_button_width = 0;
ccf589a6 448 unsigned int height = 0;
07bf635f
RS
449 int horizontal_p = mw->menu.horizontal && (level == 0);
450 widget_value* val;
451 window_state* ws;
452
453 if (level >= mw->menu.old_depth)
be06a3df 454 abort_gracefully ((Widget) mw);
07bf635f
RS
455
456 ws = &mw->menu.windows [level];
457 ws->width = 0;
458 ws->height = 0;
459 ws->label_width = 0;
be06a3df 460 ws->button_width = 0;
07bf635f
RS
461
462 for (val = mw->menu.old_stack [level]->contents; val; val = val->next)
463 {
464 size_menu_item (mw, val, horizontal_p, &label_width, &rest_width,
be06a3df 465 &button_width, &height);
07bf635f
RS
466 if (horizontal_p)
467 {
468 ws->width += label_width + rest_width;
469 if (height > ws->height)
470 ws->height = height;
471 }
472 else
473 {
474 if (label_width > ws->label_width)
475 ws->label_width = label_width;
476 if (rest_width > max_rest_width)
477 max_rest_width = rest_width;
be06a3df
GM
478 if (button_width > max_button_width)
479 max_button_width = button_width;
07bf635f
RS
480 ws->height += height;
481 }
482 }
483
484 if (horizontal_p)
be06a3df 485 ws->label_width = ws->button_width = 0;
07bf635f 486 else
be06a3df
GM
487 {
488 ws->width = ws->label_width + max_rest_width + max_button_width;
489 ws->button_width = max_button_width;
490 }
07bf635f
RS
491
492 ws->width += 2 * mw->menu.shadow_thickness;
493 ws->height += 2 * mw->menu.shadow_thickness;
be06a3df
GM
494
495 if (horizontal_p)
496 {
497 ws->width += 2 * mw->menu.margin;
498 ws->height += 2 * mw->menu.margin;
499 }
07bf635f
RS
500}
501
502
503\f/* Display code */
be06a3df 504
07bf635f 505static void
be06a3df 506draw_arrow (mw, window, gc, x, y, width, down_p)
d398028f
PR
507 XlwMenuWidget mw;
508 Window window;
509 GC gc;
510 int x;
511 int y;
512 int width;
be06a3df 513 int down_p;
07bf635f 514{
be06a3df
GM
515 Display *dpy = XtDisplay (mw);
516 GC top_gc = mw->menu.shadow_top_gc;
517 GC bottom_gc = mw->menu.shadow_bottom_gc;
518 int thickness = mw->menu.shadow_thickness;
519 int height = width;
520 XPoint pt[10];
521 /* alpha = atan (0.5)
522 factor = (1 + sin (alpha)) / cos (alpha) */
523 double factor = 1.62;
524 int thickness2 = thickness * factor;
525
526 y += (mw->menu.font->ascent + mw->menu.font->descent - height) / 2;
07bf635f 527
be06a3df
GM
528 if (down_p)
529 {
530 GC temp;
531 temp = top_gc;
532 top_gc = bottom_gc;
533 bottom_gc = temp;
534 }
535
536 pt[0].x = x;
537 pt[0].y = y + height;
538 pt[1].x = x + thickness;
539 pt[1].y = y + height - thickness2;
540 pt[2].x = x + thickness2;
541 pt[2].y = y + thickness2;
542 pt[3].x = x;
543 pt[3].y = y;
544 XFillPolygon (dpy, window, top_gc, pt, 4, Convex, CoordModeOrigin);
545
546 pt[0].x = x;
547 pt[0].y = y;
548 pt[1].x = x + thickness;
549 pt[1].y = y + thickness2;
550 pt[2].x = x + width - thickness2;
551 pt[2].y = y + height / 2;
552 pt[3].x = x + width;
553 pt[3].y = y + height / 2;
554 XFillPolygon (dpy, window, top_gc, pt, 4, Convex, CoordModeOrigin);
555
556 pt[0].x = x;
557 pt[0].y = y + height;
558 pt[1].x = x + thickness;
559 pt[1].y = y + height - thickness2;
560 pt[2].x = x + width - thickness2;
561 pt[2].y = y + height / 2;
562 pt[3].x = x + width;
563 pt[3].y = y + height / 2;
564 XFillPolygon (dpy, window, bottom_gc, pt, 4, Convex, CoordModeOrigin);
07bf635f
RS
565}
566
be06a3df
GM
567
568
07bf635f 569static void
be06a3df 570draw_shadow_rectangle (mw, window, x, y, width, height, erase_p, down_p)
d398028f
PR
571 XlwMenuWidget mw;
572 Window window;
573 int x;
574 int y;
575 int width;
576 int height;
577 int erase_p;
be06a3df 578 int down_p;
07bf635f
RS
579{
580 Display *dpy = XtDisplay (mw);
581 GC top_gc = !erase_p ? mw->menu.shadow_top_gc : mw->menu.background_gc;
582 GC bottom_gc = !erase_p ? mw->menu.shadow_bottom_gc : mw->menu.background_gc;
583 int thickness = mw->menu.shadow_thickness;
584 XPoint points [4];
be06a3df
GM
585
586 if (!erase_p && down_p)
587 {
588 GC temp;
589 temp = top_gc;
590 top_gc = bottom_gc;
591 bottom_gc = temp;
592 }
593
07bf635f
RS
594 points [0].x = x;
595 points [0].y = y;
596 points [1].x = x + width;
597 points [1].y = y;
598 points [2].x = x + width - thickness;
599 points [2].y = y + thickness;
600 points [3].x = x;
601 points [3].y = y + thickness;
602 XFillPolygon (dpy, window, top_gc, points, 4, Convex, CoordModeOrigin);
603 points [0].x = x;
604 points [0].y = y + thickness;
605 points [1].x = x;
606 points [1].y = y + height;
607 points [2].x = x + thickness;
608 points [2].y = y + height - thickness;
609 points [3].x = x + thickness;
610 points [3].y = y + thickness;
611 XFillPolygon (dpy, window, top_gc, points, 4, Convex, CoordModeOrigin);
612 points [0].x = x + width;
613 points [0].y = y;
614 points [1].x = x + width - thickness;
615 points [1].y = y + thickness;
616 points [2].x = x + width - thickness;
617 points [2].y = y + height - thickness;
618 points [3].x = x + width;
619 points [3].y = y + height - thickness;
620 XFillPolygon (dpy, window, bottom_gc, points, 4, Convex, CoordModeOrigin);
621 points [0].x = x;
622 points [0].y = y + height;
623 points [1].x = x + width;
624 points [1].y = y + height;
625 points [2].x = x + width;
626 points [2].y = y + height - thickness;
627 points [3].x = x + thickness;
628 points [3].y = y + height - thickness;
629 XFillPolygon (dpy, window, bottom_gc, points, 4, Convex, CoordModeOrigin);
630}
631
632
be06a3df
GM
633static void
634draw_shadow_rhombus (mw, window, x, y, width, height, erase_p, down_p)
635 XlwMenuWidget mw;
636 Window window;
637 int x;
638 int y;
639 int width;
640 int height;
641 int erase_p;
642 int down_p;
643{
644 Display *dpy = XtDisplay (mw);
645 GC top_gc = !erase_p ? mw->menu.shadow_top_gc : mw->menu.background_gc;
646 GC bottom_gc = !erase_p ? mw->menu.shadow_bottom_gc : mw->menu.background_gc;
647 int thickness = mw->menu.shadow_thickness;
648 XPoint points [4];
be06a3df
GM
649
650 if (!erase_p && down_p)
651 {
652 GC temp;
653 temp = top_gc;
654 top_gc = bottom_gc;
655 bottom_gc = temp;
656 }
657
658 points [0].x = x;
659 points [0].y = y + height / 2;
660 points [1].x = x + thickness;
661 points [1].y = y + height / 2;
662 points [2].x = x + width / 2;
663 points [2].y = y + thickness;
664 points [3].x = x + width / 2;
665 points [3].y = y;
666 XFillPolygon (dpy, window, top_gc, points, 4, Convex, CoordModeOrigin);
667 points [0].x = x + width / 2;
668 points [0].y = y;
669 points [1].x = x + width / 2;
670 points [1].y = y + thickness;
671 points [2].x = x + width - thickness;
672 points [2].y = y + height / 2;
673 points [3].x = x + width;
674 points [3].y = y + height / 2;
675 XFillPolygon (dpy, window, top_gc, points, 4, Convex, CoordModeOrigin);
676 points [0].x = x;
677 points [0].y = y + height / 2;
678 points [1].x = x + thickness;
679 points [1].y = y + height / 2;
680 points [2].x = x + width / 2;
681 points [2].y = y + height - thickness;
682 points [3].x = x + width / 2;
683 points [3].y = y + height;
684 XFillPolygon (dpy, window, bottom_gc, points, 4, Convex, CoordModeOrigin);
685 points [0].x = x + width / 2;
686 points [0].y = y + height;
687 points [1].x = x + width / 2;
688 points [1].y = y + height - thickness;
689 points [2].x = x + width - thickness;
690 points [2].y = y + height / 2;
691 points [3].x = x + width;
692 points [3].y = y + height / 2;
693 XFillPolygon (dpy, window, bottom_gc, points, 4, Convex, CoordModeOrigin);
694}
695
696
697/* Draw a toggle button on widget MW, X window WINDOW. X/Y is the
698 top-left corner of the menu item. SELECTED_P non-zero means the
699 toggle button is selected. */
700
701static void
702draw_toggle (mw, window, x, y, selected_p)
703 XlwMenuWidget mw;
704 Window window;
705 int x, y, selected_p;
706{
707 int width, height;
708
709 width = toggle_button_width (mw);
710 height = width;
711 x += mw->menu.horizontal_spacing;
712 y += (mw->menu.font->ascent - height) / 2;
713 draw_shadow_rectangle (mw, window, x, y, width, height, False, selected_p);
714}
715
716
717/* Draw a radio button on widget MW, X window WINDOW. X/Y is the
718 top-left corner of the menu item. SELECTED_P non-zero means the
719 toggle button is selected. */
720
721static void
722draw_radio (mw, window, x, y, selected_p)
723 XlwMenuWidget mw;
724 Window window;
725 int x, y, selected_p;
726{
727 int width, height;
728
729 width = radio_button_width (mw);
730 height = width;
731 x += mw->menu.horizontal_spacing;
732 y += (mw->menu.font->ascent - height) / 2;
733 draw_shadow_rhombus (mw, window, x, y, width, height, False, selected_p);
734}
735
736
737/* Draw a menu separator on widget MW, X window WINDOW. X/Y is the
738 top-left corner of the menu item. WIDTH is the width of the
739 separator to draw. TYPE is the separator type. */
740
741static void
742draw_separator (mw, window, x, y, width, type)
743 XlwMenuWidget mw;
744 Window window;
745 int x, y, width;
746 enum menu_separator type;
747{
748 Display *dpy = XtDisplay (mw);
749 XGCValues xgcv;
750
751 switch (type)
752 {
753 case SEPARATOR_NO_LINE:
754 break;
755
756 case SEPARATOR_SINGLE_LINE:
757 XDrawLine (dpy, window, mw->menu.foreground_gc,
758 x, y, x + width, y);
759 break;
760
761 case SEPARATOR_DOUBLE_LINE:
762 draw_separator (mw, window, x, y, width, SEPARATOR_SINGLE_LINE);
763 draw_separator (mw, window, x, y + 2, width, SEPARATOR_SINGLE_LINE);
764 break;
765
766 case SEPARATOR_SINGLE_DASHED_LINE:
767 xgcv.line_style = LineOnOffDash;
768 XChangeGC (dpy, mw->menu.foreground_gc, GCLineStyle, &xgcv);
769 XDrawLine (dpy, window, mw->menu.foreground_gc,
770 x, y, x + width, y);
771 xgcv.line_style = LineSolid;
772 XChangeGC (dpy, mw->menu.foreground_gc, GCLineStyle, &xgcv);
773 break;
774
775 case SEPARATOR_DOUBLE_DASHED_LINE:
776 draw_separator (mw, window, x, y, width,
777 SEPARATOR_SINGLE_DASHED_LINE);
778 draw_separator (mw, window, x, y + 2, width,
779 SEPARATOR_SINGLE_DASHED_LINE);
780 break;
781
782 case SEPARATOR_SHADOW_ETCHED_IN:
783 XDrawLine (dpy, window, mw->menu.shadow_bottom_gc,
784 x, y, x + width, y);
785 XDrawLine (dpy, window, mw->menu.shadow_top_gc,
786 x, y + 1, x + width, y + 1);
787 break;
788
789 case SEPARATOR_SHADOW_ETCHED_OUT:
790 XDrawLine (dpy, window, mw->menu.shadow_top_gc,
791 x, y, x + width, y);
792 XDrawLine (dpy, window, mw->menu.shadow_bottom_gc,
793 x, y + 1, x + width, y + 1);
794 break;
795
796 case SEPARATOR_SHADOW_ETCHED_IN_DASH:
797 xgcv.line_style = LineOnOffDash;
798 XChangeGC (dpy, mw->menu.shadow_bottom_gc, GCLineStyle, &xgcv);
799 XChangeGC (dpy, mw->menu.shadow_top_gc, GCLineStyle, &xgcv);
800 draw_separator (mw, window, x, y, SEPARATOR_SHADOW_ETCHED_IN);
801 xgcv.line_style = LineSolid;
802 XChangeGC (dpy, mw->menu.shadow_bottom_gc, GCLineStyle, &xgcv);
803 XChangeGC (dpy, mw->menu.shadow_top_gc, GCLineStyle, &xgcv);
804 break;
805
806 case SEPARATOR_SHADOW_ETCHED_OUT_DASH:
807 xgcv.line_style = LineOnOffDash;
808 XChangeGC (dpy, mw->menu.shadow_bottom_gc, GCLineStyle, &xgcv);
809 XChangeGC (dpy, mw->menu.shadow_top_gc, GCLineStyle, &xgcv);
810 draw_separator (mw, window, x, y, SEPARATOR_SHADOW_ETCHED_OUT);
811 xgcv.line_style = LineSolid;
812 XChangeGC (dpy, mw->menu.shadow_bottom_gc, GCLineStyle, &xgcv);
813 XChangeGC (dpy, mw->menu.shadow_top_gc, GCLineStyle, &xgcv);
814 break;
815
816 case SEPARATOR_SHADOW_DOUBLE_ETCHED_IN:
817 draw_separator (mw, window, x, y, width, SEPARATOR_SHADOW_ETCHED_IN);
818 draw_separator (mw, window, x, y + 3, width, SEPARATOR_SHADOW_ETCHED_IN);
819 break;
820
821 case SEPARATOR_SHADOW_DOUBLE_ETCHED_OUT:
822 draw_separator (mw, window, x, y, width,
823 SEPARATOR_SHADOW_ETCHED_OUT);
824 draw_separator (mw, window, x, y + 3, width,
825 SEPARATOR_SHADOW_ETCHED_OUT);
826 break;
827
828 case SEPARATOR_SHADOW_DOUBLE_ETCHED_IN_DASH:
829 xgcv.line_style = LineOnOffDash;
830 XChangeGC (dpy, mw->menu.shadow_bottom_gc, GCLineStyle, &xgcv);
831 XChangeGC (dpy, mw->menu.shadow_top_gc, GCLineStyle, &xgcv);
832 draw_separator (mw, window, x, y, width,
833 SEPARATOR_SHADOW_DOUBLE_ETCHED_IN);
834 xgcv.line_style = LineSolid;
835 XChangeGC (dpy, mw->menu.shadow_bottom_gc, GCLineStyle, &xgcv);
836 XChangeGC (dpy, mw->menu.shadow_top_gc, GCLineStyle, &xgcv);
837 break;
838
839 case SEPARATOR_SHADOW_DOUBLE_ETCHED_OUT_DASH:
840 xgcv.line_style = LineOnOffDash;
841 XChangeGC (dpy, mw->menu.shadow_bottom_gc, GCLineStyle, &xgcv);
842 XChangeGC (dpy, mw->menu.shadow_top_gc, GCLineStyle, &xgcv);
843 draw_separator (mw, window, x, y, width,
844 SEPARATOR_SHADOW_DOUBLE_ETCHED_OUT);
845 xgcv.line_style = LineSolid;
846 XChangeGC (dpy, mw->menu.shadow_bottom_gc, GCLineStyle, &xgcv);
847 XChangeGC (dpy, mw->menu.shadow_top_gc, GCLineStyle, &xgcv);
848 break;
849
850 default:
851 abort ();
852 }
853}
854
855
856/* Return the pixel height of menu separator SEPARATOR. */
857
858static int
859separator_height (separator)
860 enum menu_separator separator;
861{
862 switch (separator)
863 {
864 case SEPARATOR_NO_LINE:
865 return 2;
866
867 case SEPARATOR_SINGLE_LINE:
868 case SEPARATOR_SINGLE_DASHED_LINE:
869 return 1;
870
871 case SEPARATOR_DOUBLE_LINE:
872 case SEPARATOR_DOUBLE_DASHED_LINE:
873 return 3;
874
875 case SEPARATOR_SHADOW_ETCHED_IN:
876 case SEPARATOR_SHADOW_ETCHED_OUT:
877 case SEPARATOR_SHADOW_ETCHED_IN_DASH:
878 case SEPARATOR_SHADOW_ETCHED_OUT_DASH:
879 return 2;
880
881 case SEPARATOR_SHADOW_DOUBLE_ETCHED_IN:
882 case SEPARATOR_SHADOW_DOUBLE_ETCHED_OUT:
883 case SEPARATOR_SHADOW_DOUBLE_ETCHED_IN_DASH:
884 case SEPARATOR_SHADOW_DOUBLE_ETCHED_OUT_DASH:
885 return 5;
886
887 default:
888 abort ();
889 }
890}
891
892
07bf635f 893/* Display the menu item and increment where.x and where.y to show how large
be06a3df
GM
894 the menu item was. */
895
07bf635f 896static void
be06a3df
GM
897display_menu_item (mw, val, ws, where, highlighted_p, horizontal_p,
898 just_compute_p)
d398028f
PR
899 XlwMenuWidget mw;
900 widget_value* val;
901 window_state* ws;
902 XPoint* where;
903 Boolean highlighted_p;
904 Boolean horizontal_p;
905 Boolean just_compute_p;
07bf635f
RS
906{
907 GC deco_gc;
908 GC text_gc;
909 int font_ascent = mw->menu.font->ascent;
910 int font_descent = mw->menu.font->descent;
911 int shadow = mw->menu.shadow_thickness;
be06a3df 912 int margin = mw->menu.margin;
07bf635f
RS
913 int h_spacing = mw->menu.horizontal_spacing;
914 int v_spacing = mw->menu.vertical_spacing;
915 int label_width;
916 int rest_width;
be06a3df 917 int button_width;
07bf635f
RS
918 int height;
919 int width;
be06a3df
GM
920 enum menu_separator separator;
921 int separator_p = lw_separator_p (val->name, &separator, 0);
07bf635f
RS
922
923 /* compute the sizes of the item */
be06a3df
GM
924 size_menu_item (mw, val, horizontal_p, &label_width, &rest_width,
925 &button_width, &height);
07bf635f
RS
926
927 if (horizontal_p)
928 width = label_width + rest_width;
929 else
930 {
931 label_width = ws->label_width;
932 width = ws->width - 2 * shadow;
933 }
934
07bf635f
RS
935 /* Only highlight an enabled item that has a callback. */
936 if (highlighted_p)
937 if (!val->enabled || !(val->call_data || val->contents))
938 highlighted_p = 0;
939
940 /* do the drawing. */
941 if (!just_compute_p)
942 {
943 /* Add the shadow border of the containing menu */
944 int x = where->x + shadow;
945 int y = where->y + shadow;
946
be06a3df
GM
947 if (horizontal_p)
948 {
949 x += margin;
950 y += margin;
951 }
952
07bf635f
RS
953 /* pick the foreground and background GC. */
954 if (val->enabled)
be06a3df 955 text_gc = mw->menu.foreground_gc;
07bf635f 956 else
be06a3df 957 text_gc = mw->menu.inactive_gc;
07bf635f
RS
958 deco_gc = mw->menu.foreground_gc;
959
960 if (separator_p)
961 {
be06a3df 962 draw_separator (mw, ws->window, x, y, width, separator);
07bf635f
RS
963 }
964 else
965 {
84593cae 966 int x_offset = x + h_spacing + shadow;
07bf635f 967 char* display_string = resource_widget_value (mw, val);
be06a3df
GM
968 draw_shadow_rectangle (mw, ws->window, x, y, width, height, True,
969 False);
84593cae
PR
970
971 /* Deal with centering a menu title. */
972 if (!horizontal_p && !val->contents && !val->call_data)
973 {
974 int l = string_width (mw, display_string);
975
976 if (width > l)
977 x_offset = (width - l) >> 1;
978 }
be06a3df
GM
979 else if (!horizontal_p && ws->button_width)
980 x_offset += ws->button_width;
981
982
84593cae 983 XDrawString (XtDisplay (mw), ws->window, text_gc, x_offset,
07bf635f
RS
984 y + v_spacing + shadow + font_ascent,
985 display_string, strlen (display_string));
986
987 if (!horizontal_p)
988 {
be06a3df
GM
989 if (val->button_type == BUTTON_TYPE_TOGGLE)
990 draw_toggle (mw, ws->window, x, y + v_spacing + shadow,
991 val->selected);
992 else if (val->button_type == BUTTON_TYPE_RADIO)
993 draw_radio (mw, ws->window, x, y + v_spacing + shadow,
994 val->selected);
995
07bf635f
RS
996 if (val->contents)
997 {
998 int a_w = arrow_width (mw);
999 draw_arrow (mw, ws->window, deco_gc,
be06a3df 1000 x + width - a_w
d398028f
PR
1001 - mw->menu.horizontal_spacing
1002 - mw->menu.shadow_thickness,
be06a3df
GM
1003 y + v_spacing + shadow, a_w,
1004 highlighted_p);
07bf635f
RS
1005 }
1006 else if (val->key)
1007 {
1008 XDrawString (XtDisplay (mw), ws->window, text_gc,
1009 x + label_width + mw->menu.arrow_spacing,
1010 y + v_spacing + shadow + font_ascent,
1011 val->key, strlen (val->key));
1012 }
1013 }
d398028f
PR
1014 else
1015 {
1016 XDrawRectangle (XtDisplay (mw), ws->window,
1017 mw->menu.background_gc,
1018 x + shadow, y + shadow,
1019 label_width + h_spacing - 1,
1020 font_ascent + font_descent + 2 * v_spacing - 1);
1021 draw_shadow_rectangle (mw, ws->window, x, y, width, height,
be06a3df 1022 True, False);
d398028f 1023 }
07bf635f
RS
1024
1025 if (highlighted_p)
be06a3df
GM
1026 draw_shadow_rectangle (mw, ws->window, x, y, width, height, False,
1027 False);
07bf635f
RS
1028 }
1029 }
1030
1031 where->x += width;
1032 where->y += height;
1033}
1034
1035static void
d398028f
PR
1036display_menu (mw, level, just_compute_p, highlighted_pos, hit, hit_return,
1037 this, that)
1038 XlwMenuWidget mw;
1039 int level;
1040 Boolean just_compute_p;
1041 XPoint* highlighted_pos;
1042 XPoint* hit;
1043 widget_value** hit_return;
1044 widget_value* this;
1045 widget_value* that;
07bf635f
RS
1046{
1047 widget_value* val;
1048 widget_value* following_item;
1049 window_state* ws;
1050 XPoint where;
1051 int horizontal_p = mw->menu.horizontal && (level == 0);
1052 int highlighted_p;
1053 int just_compute_this_one_p;
ccc1cac0
RS
1054 /* This is set nonzero if the element containing HIGHLIGHTED_POS
1055 is disabled, so that we do not return any subsequent element either. */
1056 int no_return = 0;
be06a3df 1057 enum menu_separator separator;
07bf635f
RS
1058
1059 if (level >= mw->menu.old_depth)
be06a3df 1060 abort_gracefully ((Widget) mw);
07bf635f
RS
1061
1062 if (level < mw->menu.old_depth - 1)
1063 following_item = mw->menu.old_stack [level + 1];
1064 else
1065 following_item = NULL;
1066
1067 if (hit)
1068 *hit_return = NULL;
1069
1070 where.x = 0;
1071 where.y = 0;
1072
1073 ws = &mw->menu.windows [level];
1074 for (val = mw->menu.old_stack [level]->contents; val; val = val->next)
1075 {
1076 highlighted_p = val == following_item;
1077 if (highlighted_p && highlighted_pos)
1078 {
1079 if (horizontal_p)
1080 highlighted_pos->x = where.x;
1081 else
1082 highlighted_pos->y = where.y;
1083 }
1084
1085 just_compute_this_one_p =
1086 just_compute_p || ((this || that) && val != this && val != that);
1087
1088 display_menu_item (mw, val, ws, &where, highlighted_p, horizontal_p,
1089 just_compute_this_one_p);
1090
1091 if (highlighted_p && highlighted_pos)
1092 {
1093 if (horizontal_p)
1094 highlighted_pos->y = where.y;
1095 else
1096 highlighted_pos->x = where.x;
1097 }
1098
1099 if (hit
1100 && !*hit_return
1101 && (horizontal_p ? hit->x < where.x : hit->y < where.y)
be06a3df 1102 && !lw_separator_p (val->name, &separator, 0)
ccc1cac0
RS
1103 && !no_return)
1104 {
1105 if (val->enabled)
1106 *hit_return = val;
1107 else
1108 no_return = 1;
1109 }
07bf635f
RS
1110
1111 if (horizontal_p)
1112 where.y = 0;
1113 else
1114 where.x = 0;
1115 }
1116
1117 if (!just_compute_p)
be06a3df
GM
1118 draw_shadow_rectangle (mw, ws->window, 0, 0, ws->width, ws->height,
1119 False, False);
07bf635f
RS
1120}
1121
1122\f/* Motion code */
1123static void
d398028f
PR
1124set_new_state (mw, val, level)
1125 XlwMenuWidget mw;
1126 widget_value* val;
1127 int level;
07bf635f
RS
1128{
1129 int i;
1130
1131 mw->menu.new_depth = 0;
1132 for (i = 0; i < level; i++)
1133 push_new_stack (mw, mw->menu.old_stack [i]);
1134 push_new_stack (mw, val);
1135}
1136
1137static void
d398028f
PR
1138make_windows_if_needed (mw, n)
1139 XlwMenuWidget mw;
1140 int n;
07bf635f
RS
1141{
1142 int i;
1143 int start_at;
1144 XSetWindowAttributes xswa;
1145 int mask;
1146 Window root = RootWindowOfScreen (DefaultScreenOfDisplay (XtDisplay (mw)));
1147 window_state* windows;
1148
1149 if (mw->menu.windows_length >= n)
1150 return;
1151
1152 xswa.save_under = True;
1153 xswa.override_redirect = True;
1154 xswa.background_pixel = mw->core.background_pixel;
1155 xswa.border_pixel = mw->core.border_pixel;
1156 xswa.event_mask =
d398028f 1157 ExposureMask | PointerMotionMask | PointerMotionHintMask
07bf635f
RS
1158 | ButtonReleaseMask | ButtonPressMask;
1159 xswa.cursor = mw->menu.cursor_shape;
1160 mask = CWSaveUnder | CWOverrideRedirect | CWBackPixel | CWBorderPixel
1161 | CWEventMask | CWCursor;
1162
1163 if (!mw->menu.windows)
1164 {
1165 mw->menu.windows =
1166 (window_state*)XtMalloc (n * sizeof (window_state));
1167 start_at = 0;
1168 }
1169 else
1170 {
1171 mw->menu.windows =
1172 (window_state*)XtRealloc ((char*)mw->menu.windows,
1173 n * sizeof (window_state));
1174 start_at = mw->menu.windows_length;
1175 }
1176 mw->menu.windows_length = n;
1177
1178 windows = mw->menu.windows;
1179
1180 for (i = start_at; i < n; i++)
1181 {
1182 windows [i].x = 0;
1183 windows [i].y = 0;
1184 windows [i].width = 1;
1185 windows [i].height = 1;
1186 windows [i].window =
1187 XCreateWindow (XtDisplay (mw), root, 0, 0, 1, 1,
1188 0, 0, CopyFromParent, CopyFromParent, mask, &xswa);
1189 }
1190}
1191
1192/* Make the window fit in the screen */
1193static void
d398028f
PR
1194fit_to_screen (mw, ws, previous_ws, horizontal_p)
1195 XlwMenuWidget mw;
1196 window_state* ws;
1197 window_state* previous_ws;
1198 Boolean horizontal_p;
07bf635f 1199{
ccf589a6
RS
1200 unsigned int screen_width = WidthOfScreen (XtScreen (mw));
1201 unsigned int screen_height = HeightOfScreen (XtScreen (mw));
1ba46f7d
RS
1202 /* 1 if we are unable to avoid an overlap between
1203 this menu and the parent menu in the X dimension. */
1204 int horizontal_overlap = 0;
07bf635f
RS
1205
1206 if (ws->x < 0)
1207 ws->x = 0;
1208 else if (ws->x + ws->width > screen_width)
1209 {
1210 if (!horizontal_p)
1211 ws->x = previous_ws->x - ws->width;
1212 else
1213 ws->x = screen_width - ws->width;
525b8232 1214 if (ws->x < 0)
1ba46f7d
RS
1215 {
1216 ws->x = 0;
1217 horizontal_overlap = 1;
1218 }
1219 }
1220 /* If we overlap in X, try to avoid overlap in Y. */
1221 if (horizontal_overlap
1222 && ws->y < previous_ws->y + previous_ws->height
1223 && previous_ws->y < ws->y + ws->height)
1224 {
1225 /* Put this menu right below or right above PREVIOUS_WS
1226 if there's room. */
1227 if (previous_ws->y + previous_ws->height + ws->height < screen_height)
1228 ws->y = previous_ws->y + previous_ws->height;
1229 else if (previous_ws->y - ws->height > 0)
1230 ws->y = previous_ws->y - ws->height;
07bf635f 1231 }
1ba46f7d 1232
07bf635f
RS
1233 if (ws->y < 0)
1234 ws->y = 0;
1235 else if (ws->y + ws->height > screen_height)
1236 {
1237 if (horizontal_p)
1238 ws->y = previous_ws->y - ws->height;
1239 else
1240 ws->y = screen_height - ws->height;
525b8232
RS
1241 if (ws->y < 0)
1242 ws->y = 0;
07bf635f
RS
1243 }
1244}
1245
1246/* Updates old_stack from new_stack and redisplays. */
1247static void
d398028f
PR
1248remap_menubar (mw)
1249 XlwMenuWidget mw;
07bf635f
RS
1250{
1251 int i;
1252 int last_same;
1253 XPoint selection_position;
1254 int old_depth = mw->menu.old_depth;
1255 int new_depth = mw->menu.new_depth;
1256 widget_value** old_stack;
1257 widget_value** new_stack;
1258 window_state* windows;
1259 widget_value* old_selection;
1260 widget_value* new_selection;
1261
1262 /* Check that enough windows and old_stack are ready. */
1263 make_windows_if_needed (mw, new_depth);
1264 make_old_stack_space (mw, new_depth);
1265 windows = mw->menu.windows;
1266 old_stack = mw->menu.old_stack;
1267 new_stack = mw->menu.new_stack;
1268
1269 /* compute the last identical different entry */
1270 for (i = 1; i < old_depth && i < new_depth; i++)
1271 if (old_stack [i] != new_stack [i])
1272 break;
1273 last_same = i - 1;
1274
1275 /* Memorize the previously selected item to be able to refresh it */
1276 old_selection = last_same + 1 < old_depth ? old_stack [last_same + 1] : NULL;
1277 if (old_selection && !old_selection->enabled)
1278 old_selection = NULL;
1279 new_selection = last_same + 1 < new_depth ? new_stack [last_same + 1] : NULL;
1280 if (new_selection && !new_selection->enabled)
1281 new_selection = NULL;
1282
d6fd6371
GM
1283 /* Call callback when the hightlighted item changes. */
1284 if (old_selection || new_selection)
1285 XtCallCallbackList ((Widget)mw, mw->menu.highlight,
1286 (XtPointer) new_selection);
1287
07bf635f
RS
1288 /* updates old_state from new_state. It has to be done now because
1289 display_menu (called below) uses the old_stack to know what to display. */
1290 for (i = last_same + 1; i < new_depth; i++)
1291 old_stack [i] = new_stack [i];
1292 mw->menu.old_depth = new_depth;
1293
908ff139 1294 /* refresh the last selection */
07bf635f
RS
1295 selection_position.x = 0;
1296 selection_position.y = 0;
1297 display_menu (mw, last_same, new_selection == old_selection,
1298 &selection_position, NULL, NULL, old_selection, new_selection);
1299
1ba46f7d
RS
1300 /* Now place the new menus. */
1301 for (i = last_same + 1; i < new_depth && new_stack[i]->contents; i++)
07bf635f 1302 {
1ba46f7d
RS
1303 window_state *previous_ws = &windows[i - 1];
1304 window_state *ws = &windows[i];
07bf635f 1305
be06a3df
GM
1306 ws->x = (previous_ws->x + selection_position.x
1307 + mw->menu.shadow_thickness);
1308 if (i == 1)
1309 ws->x += mw->menu.margin;
1310
1311#if 0
07bf635f
RS
1312 if (!mw->menu.horizontal || i > 1)
1313 ws->x += mw->menu.shadow_thickness;
be06a3df
GM
1314#endif
1315
1316 ws->y = (previous_ws->y + selection_position.y
1317 + mw->menu.shadow_thickness);
1318 if (i == 1)
1319 ws->y += mw->menu.margin;
07bf635f
RS
1320
1321 size_menu (mw, i);
1322
1323 fit_to_screen (mw, ws, previous_ws, mw->menu.horizontal && i == 1);
1324
1325 XClearWindow (XtDisplay (mw), ws->window);
1326 XMoveResizeWindow (XtDisplay (mw), ws->window, ws->x, ws->y,
1327 ws->width, ws->height);
1328 XMapRaised (XtDisplay (mw), ws->window);
1329 display_menu (mw, i, False, &selection_position, NULL, NULL, NULL, NULL);
1330 }
1331
1332 /* unmap the menus that popped down */
1333 for (i = new_depth - 1; i < old_depth; i++)
1ba46f7d
RS
1334 if (i >= new_depth || !new_stack[i]->contents)
1335 XUnmapWindow (XtDisplay (mw), windows[i].window);
07bf635f
RS
1336}
1337
1338static Boolean
d398028f
PR
1339motion_event_is_in_menu (mw, ev, level, relative_pos)
1340 XlwMenuWidget mw;
1341 XMotionEvent* ev;
1342 int level;
1343 XPoint* relative_pos;
07bf635f
RS
1344{
1345 window_state* ws = &mw->menu.windows [level];
4f08464c
RS
1346 int shadow = level == 0 ? 0 : mw->menu.shadow_thickness;
1347 int x = ws->x + shadow;
1348 int y = ws->y + shadow;
07bf635f
RS
1349 relative_pos->x = ev->x_root - x;
1350 relative_pos->y = ev->y_root - y;
4f08464c
RS
1351 return (x - shadow < ev->x_root && ev->x_root < x + ws->width
1352 && y - shadow < ev->y_root && ev->y_root < y + ws->height);
07bf635f
RS
1353}
1354
1355static Boolean
d398028f
PR
1356map_event_to_widget_value (mw, ev, val, level)
1357 XlwMenuWidget mw;
1358 XMotionEvent* ev;
1359 widget_value** val;
1360 int* level;
07bf635f
RS
1361{
1362 int i;
1363 XPoint relative_pos;
1364 window_state* ws;
1365
1366 *val = NULL;
1367
1368 /* Find the window */
1369 for (i = mw->menu.old_depth - 1; i >= 0; i--)
1370 {
1371 ws = &mw->menu.windows [i];
1372 if (ws && motion_event_is_in_menu (mw, ev, i, &relative_pos))
1373 {
1374 display_menu (mw, i, True, NULL, &relative_pos, val, NULL, NULL);
1375
1376 if (*val)
1377 {
1378 *level = i + 1;
1379 return True;
1380 }
1381 }
1382 }
1383 return False;
1384}
1385
1386\f/* Procedures */
1387static void
d398028f
PR
1388make_drawing_gcs (mw)
1389 XlwMenuWidget mw;
07bf635f
RS
1390{
1391 XGCValues xgcv;
1392
1393 xgcv.font = mw->menu.font->fid;
1394 xgcv.foreground = mw->menu.foreground;
1395 xgcv.background = mw->core.background_pixel;
1396 mw->menu.foreground_gc = XtGetGC ((Widget)mw,
1397 GCFont | GCForeground | GCBackground,
1398 &xgcv);
1399
1400 xgcv.font = mw->menu.font->fid;
1401 xgcv.foreground = mw->menu.button_foreground;
1402 xgcv.background = mw->core.background_pixel;
1403 mw->menu.button_gc = XtGetGC ((Widget)mw,
1404 GCFont | GCForeground | GCBackground,
1405 &xgcv);
1406
1407 xgcv.font = mw->menu.font->fid;
1408 xgcv.foreground = mw->menu.foreground;
1409 xgcv.background = mw->core.background_pixel;
1410 xgcv.fill_style = FillStippled;
1411 xgcv.stipple = mw->menu.gray_pixmap;
1412 mw->menu.inactive_gc = XtGetGC ((Widget)mw,
1413 (GCFont | GCForeground | GCBackground
1414 | GCFillStyle | GCStipple), &xgcv);
1415
1416 xgcv.font = mw->menu.font->fid;
1417 xgcv.foreground = mw->menu.button_foreground;
1418 xgcv.background = mw->core.background_pixel;
1419 xgcv.fill_style = FillStippled;
1420 xgcv.stipple = mw->menu.gray_pixmap;
1421 mw->menu.inactive_button_gc = XtGetGC ((Widget)mw,
1422 (GCFont | GCForeground | GCBackground
1423 | GCFillStyle | GCStipple), &xgcv);
1424
1425 xgcv.font = mw->menu.font->fid;
1426 xgcv.foreground = mw->core.background_pixel;
1427 xgcv.background = mw->menu.foreground;
1428 mw->menu.background_gc = XtGetGC ((Widget)mw,
1429 GCFont | GCForeground | GCBackground,
1430 &xgcv);
1431}
1432
1433static void
d398028f
PR
1434release_drawing_gcs (mw)
1435 XlwMenuWidget mw;
07bf635f
RS
1436{
1437 XtReleaseGC ((Widget) mw, mw->menu.foreground_gc);
1438 XtReleaseGC ((Widget) mw, mw->menu.button_gc);
1439 XtReleaseGC ((Widget) mw, mw->menu.inactive_gc);
1440 XtReleaseGC ((Widget) mw, mw->menu.inactive_button_gc);
1441 XtReleaseGC ((Widget) mw, mw->menu.background_gc);
1442 /* let's get some segvs if we try to use these... */
1443 mw->menu.foreground_gc = (GC) -1;
1444 mw->menu.button_gc = (GC) -1;
1445 mw->menu.inactive_gc = (GC) -1;
1446 mw->menu.inactive_button_gc = (GC) -1;
1447 mw->menu.background_gc = (GC) -1;
1448}
1449
1450#define MINL(x,y) ((((unsigned long) (x)) < ((unsigned long) (y))) \
1451 ? ((unsigned long) (x)) : ((unsigned long) (y)))
1452
1453static void
d398028f
PR
1454make_shadow_gcs (mw)
1455 XlwMenuWidget mw;
07bf635f
RS
1456{
1457 XGCValues xgcv;
1458 unsigned long pm = 0;
1459 Display *dpy = XtDisplay ((Widget) mw);
be06a3df 1460 Screen *screen = XtScreen ((Widget) mw);
b0404f9f 1461 Colormap cmap = mw->core.colormap;
07bf635f
RS
1462 XColor topc, botc;
1463 int top_frobbed = 0, bottom_frobbed = 0;
1464
bba2a923
GM
1465 mw->menu.free_top_shadow_color_p = 0;
1466 mw->menu.free_bottom_shadow_color_p = 0;
1467
07bf635f
RS
1468 if (mw->menu.top_shadow_color == -1)
1469 mw->menu.top_shadow_color = mw->core.background_pixel;
bba2a923
GM
1470 else
1471 mw->menu.top_shadow_color = mw->menu.top_shadow_color;
1472
07bf635f
RS
1473 if (mw->menu.bottom_shadow_color == -1)
1474 mw->menu.bottom_shadow_color = mw->menu.foreground;
bba2a923
GM
1475 else
1476 mw->menu.bottom_shadow_color = mw->menu.bottom_shadow_color;
07bf635f
RS
1477
1478 if (mw->menu.top_shadow_color == mw->core.background_pixel ||
1479 mw->menu.top_shadow_color == mw->menu.foreground)
1480 {
1481 topc.pixel = mw->core.background_pixel;
65c0ae05
MB
1482#ifdef emacs
1483 if (x_alloc_lighter_color_for_widget ((Widget) mw, dpy, cmap,
1484 &topc.pixel,
1485 1.2, 0x8000))
1486#else
07bf635f
RS
1487 XQueryColor (dpy, cmap, &topc);
1488 /* don't overflow/wrap! */
1489 topc.red = MINL (65535, topc.red * 1.2);
1490 topc.green = MINL (65535, topc.green * 1.2);
1491 topc.blue = MINL (65535, topc.blue * 1.2);
1492 if (XAllocColor (dpy, cmap, &topc))
be06a3df 1493#endif
07bf635f
RS
1494 {
1495 mw->menu.top_shadow_color = topc.pixel;
bba2a923 1496 mw->menu.free_top_shadow_color_p = 1;
07bf635f
RS
1497 top_frobbed = 1;
1498 }
1499 }
1500 if (mw->menu.bottom_shadow_color == mw->menu.foreground ||
1501 mw->menu.bottom_shadow_color == mw->core.background_pixel)
1502 {
1503 botc.pixel = mw->core.background_pixel;
65c0ae05
MB
1504#ifdef emacs
1505 if (x_alloc_lighter_color_for_widget ((Widget) mw, dpy, cmap,
1506 &botc.pixel,
1507 0.6, 0x4000))
1508#else
07bf635f
RS
1509 XQueryColor (dpy, cmap, &botc);
1510 botc.red *= 0.6;
1511 botc.green *= 0.6;
1512 botc.blue *= 0.6;
1513 if (XAllocColor (dpy, cmap, &botc))
be06a3df 1514#endif
07bf635f
RS
1515 {
1516 mw->menu.bottom_shadow_color = botc.pixel;
bba2a923 1517 mw->menu.free_bottom_shadow_color_p = 1;
07bf635f
RS
1518 bottom_frobbed = 1;
1519 }
1520 }
1521
1522 if (top_frobbed && bottom_frobbed)
1523 {
65c0ae05 1524 if (topc.pixel == botc.pixel)
07bf635f
RS
1525 {
1526 if (botc.pixel == mw->menu.foreground)
fc8fefed 1527 {
bba2a923
GM
1528 if (mw->menu.free_top_shadow_color_p)
1529 {
1530 x_free_dpy_colors (dpy, screen, cmap,
1531 &mw->menu.top_shadow_color, 1);
1532 mw->menu.free_top_shadow_color_p = 0;
1533 }
1534 mw->menu.top_shadow_color = mw->core.background_pixel;
fc8fefed 1535 }
07bf635f 1536 else
fc8fefed 1537 {
bba2a923
GM
1538 if (mw->menu.free_bottom_shadow_color_p)
1539 {
1540 x_free_dpy_colors (dpy, screen, cmap,
1541 &mw->menu.bottom_shadow_color, 1);
1542 mw->menu.free_bottom_shadow_color_p = 0;
1543 }
1544 mw->menu.bottom_shadow_color = mw->menu.foreground;
fc8fefed 1545 }
07bf635f
RS
1546 }
1547 }
1548
1549 if (!mw->menu.top_shadow_pixmap &&
1550 mw->menu.top_shadow_color == mw->core.background_pixel)
1551 {
1552 mw->menu.top_shadow_pixmap = mw->menu.gray_pixmap;
bba2a923
GM
1553 if (mw->menu.free_top_shadow_color_p)
1554 {
1555 x_free_dpy_colors (dpy, screen, cmap, &mw->menu.top_shadow_color, 1);
1556 mw->menu.free_top_shadow_color_p = 0;
1557 }
1558 mw->menu.top_shadow_color = mw->menu.foreground;
07bf635f
RS
1559 }
1560 if (!mw->menu.bottom_shadow_pixmap &&
1561 mw->menu.bottom_shadow_color == mw->core.background_pixel)
1562 {
1563 mw->menu.bottom_shadow_pixmap = mw->menu.gray_pixmap;
bba2a923
GM
1564 if (mw->menu.free_bottom_shadow_color_p)
1565 {
1566 x_free_dpy_colors (dpy, screen, cmap,
1567 &mw->menu.bottom_shadow_color, 1);
1568 mw->menu.free_bottom_shadow_color_p = 0;
1569 }
1570 mw->menu.bottom_shadow_color = mw->menu.foreground;
07bf635f
RS
1571 }
1572
1573 xgcv.fill_style = FillStippled;
1574 xgcv.foreground = mw->menu.top_shadow_color;
1575 xgcv.stipple = mw->menu.top_shadow_pixmap;
1576 pm = (xgcv.stipple ? GCStipple|GCFillStyle : 0);
1577 mw->menu.shadow_top_gc = XtGetGC ((Widget)mw, GCForeground | pm, &xgcv);
1578
1579 xgcv.foreground = mw->menu.bottom_shadow_color;
1580 xgcv.stipple = mw->menu.bottom_shadow_pixmap;
1581 pm = (xgcv.stipple ? GCStipple|GCFillStyle : 0);
1582 mw->menu.shadow_bottom_gc = XtGetGC ((Widget)mw, GCForeground | pm, &xgcv);
1583}
1584
1585
1586static void
d398028f
PR
1587release_shadow_gcs (mw)
1588 XlwMenuWidget mw;
07bf635f 1589{
fc8fefed
GM
1590 Display *dpy = XtDisplay ((Widget) mw);
1591 Screen *screen = XtScreen ((Widget) mw);
1592 Colormap cmap = mw->core.colormap;
1593 Pixel px[2];
bba2a923
GM
1594 int i = 0;
1595
1596 if (mw->menu.free_top_shadow_color_p)
1597 px[i++] = mw->menu.top_shadow_color;
1598 if (mw->menu.free_bottom_shadow_color_p)
1599 px[i++] = mw->menu.bottom_shadow_color;
1600 if (i > 0)
1601 x_free_dpy_colors (dpy, screen, cmap, px, i);
1602
07bf635f
RS
1603 XtReleaseGC ((Widget) mw, mw->menu.shadow_top_gc);
1604 XtReleaseGC ((Widget) mw, mw->menu.shadow_bottom_gc);
1605}
1606
1607static void
47d52240 1608XlwMenuInitialize (request, mw, args, num_args)
d398028f 1609 Widget request;
47d52240 1610 XlwMenuWidget mw;
d398028f
PR
1611 ArgList args;
1612 Cardinal *num_args;
07bf635f
RS
1613{
1614 /* Get the GCs and the widget size */
07bf635f
RS
1615 XSetWindowAttributes xswa;
1616 int mask;
1617
1618 Window window = RootWindowOfScreen (DefaultScreenOfDisplay (XtDisplay (mw)));
1619 Display* display = XtDisplay (mw);
1620
d398028f
PR
1621#if 0
1622 widget_value *tem = (widget_value *) XtMalloc (sizeof (widget_value));
1623
1624 /* _XtCreate is freeing the object that was passed to us,
1625 so make a copy that we will actually keep. */
1626 lwlib_bcopy (mw->menu.contents, tem, sizeof (widget_value));
1627 mw->menu.contents = tem;
1628#endif
1629
07bf635f
RS
1630/* mw->menu.cursor = XCreateFontCursor (display, mw->menu.cursor_shape); */
1631 mw->menu.cursor = mw->menu.cursor_shape;
1632
a504c8fa 1633 mw->menu.gray_pixmap
f1c16db4
GM
1634 = XCreatePixmapFromBitmapData (display, window, gray_bitmap_bits,
1635 gray_bitmap_width, gray_bitmap_height,
a504c8fa 1636 (unsigned long)1, (unsigned long)0, 1);
07bf635f 1637
47d52240
RS
1638 /* I don't understand why this ends up 0 sometimes,
1639 but it does. This kludge works around it.
1640 Can anyone find a real fix? -- rms. */
1641 if (mw->menu.font == 0)
1642 mw->menu.font = xlwmenu_default_font;
1643
07bf635f
RS
1644 make_drawing_gcs (mw);
1645 make_shadow_gcs (mw);
1646
1647 xswa.background_pixel = mw->core.background_pixel;
1648 xswa.border_pixel = mw->core.border_pixel;
1649 mask = CWBackPixel | CWBorderPixel;
1650
1651 mw->menu.popped_up = False;
1652
1653 mw->menu.old_depth = 1;
1654 mw->menu.old_stack = (widget_value**)XtMalloc (sizeof (widget_value*));
1655 mw->menu.old_stack_length = 1;
1656 mw->menu.old_stack [0] = mw->menu.contents;
1657
1658 mw->menu.new_depth = 0;
1659 mw->menu.new_stack = 0;
1660 mw->menu.new_stack_length = 0;
1661 push_new_stack (mw, mw->menu.contents);
1662
1663 mw->menu.windows = (window_state*)XtMalloc (sizeof (window_state));
1664 mw->menu.windows_length = 1;
1665 mw->menu.windows [0].x = 0;
1666 mw->menu.windows [0].y = 0;
1667 mw->menu.windows [0].width = 0;
1668 mw->menu.windows [0].height = 0;
1669 size_menu (mw, 0);
1670
1671 mw->core.width = mw->menu.windows [0].width;
1672 mw->core.height = mw->menu.windows [0].height;
1673}
1674
1675static void
1676XlwMenuClassInitialize ()
1677{
1678}
1679
1680static void
d398028f
PR
1681XlwMenuRealize (w, valueMask, attributes)
1682 Widget w;
1683 Mask *valueMask;
1684 XSetWindowAttributes *attributes;
07bf635f
RS
1685{
1686 XlwMenuWidget mw = (XlwMenuWidget)w;
1687 XSetWindowAttributes xswa;
1688 int mask;
1689
1690 (*xlwMenuWidgetClass->core_class.superclass->core_class.realize)
1691 (w, valueMask, attributes);
1692
1693 xswa.save_under = True;
1694 xswa.cursor = mw->menu.cursor_shape;
1695 mask = CWSaveUnder | CWCursor;
1696 XChangeWindowAttributes (XtDisplay (w), XtWindow (w), mask, &xswa);
1697
1698 mw->menu.windows [0].window = XtWindow (w);
1699 mw->menu.windows [0].x = w->core.x;
1700 mw->menu.windows [0].y = w->core.y;
1701 mw->menu.windows [0].width = w->core.width;
1702 mw->menu.windows [0].height = w->core.height;
1703}
1704
1705/* Only the toplevel menubar/popup is a widget so it's the only one that
1706 receives expose events through Xt. So we repaint all the other panes
1707 when receiving an Expose event. */
1708static void
d398028f
PR
1709XlwMenuRedisplay (w, ev, region)
1710 Widget w;
1711 XEvent* ev;
1712 Region region;
07bf635f
RS
1713{
1714 XlwMenuWidget mw = (XlwMenuWidget)w;
1715 int i;
1716
d398028f
PR
1717 /* If we have a depth beyond 1, it's because a submenu was displayed.
1718 If the submenu has been destroyed, set the depth back to 1. */
1719 if (submenu_destroyed)
1720 {
1721 mw->menu.old_depth = 1;
1722 submenu_destroyed = 0;
1723 }
1724
07bf635f
RS
1725 for (i = 0; i < mw->menu.old_depth; i++)
1726 display_menu (mw, i, False, NULL, NULL, NULL, NULL, NULL);
1727}
1728
1729static void
d398028f
PR
1730XlwMenuDestroy (w)
1731 Widget w;
07bf635f
RS
1732{
1733 int i;
1734 XlwMenuWidget mw = (XlwMenuWidget) w;
1735
d398028f
PR
1736 if (pointer_grabbed)
1737 XtUngrabPointer ((Widget)w, CurrentTime);
1738 pointer_grabbed = 0;
1739
1740 submenu_destroyed = 1;
1741
07bf635f
RS
1742 release_drawing_gcs (mw);
1743 release_shadow_gcs (mw);
1744
1745 /* this doesn't come from the resource db but is created explicitly
1746 so we must free it ourselves. */
1747 XFreePixmap (XtDisplay (mw), mw->menu.gray_pixmap);
1748 mw->menu.gray_pixmap = (Pixmap) -1;
1749
d398028f
PR
1750#if 0
1751 /* Do free mw->menu.contents because nowadays we copy it
1752 during initialization. */
1753 XtFree (mw->menu.contents);
1754#endif
1755
07bf635f
RS
1756 /* Don't free mw->menu.contents because that comes from our creator.
1757 The `*_stack' elements are just pointers into `contents' so leave
1758 that alone too. But free the stacks themselves. */
1759 if (mw->menu.old_stack) XtFree ((char *) mw->menu.old_stack);
1760 if (mw->menu.new_stack) XtFree ((char *) mw->menu.new_stack);
1761
1762 /* Remember, you can't free anything that came from the resource
1763 database. This includes:
1764 mw->menu.cursor
1765 mw->menu.top_shadow_pixmap
1766 mw->menu.bottom_shadow_pixmap
1767 mw->menu.font
1768 Also the color cells of top_shadow_color, bottom_shadow_color,
1769 foreground, and button_foreground will never be freed until this
1770 client exits. Nice, eh?
1771 */
1772
1773 /* start from 1 because the one in slot 0 is w->core.window */
1774 for (i = 1; i < mw->menu.windows_length; i++)
1775 XDestroyWindow (XtDisplay (mw), mw->menu.windows [i].window);
1776 if (mw->menu.windows)
1777 XtFree ((char *) mw->menu.windows);
1778}
1779
1780static Boolean
d398028f
PR
1781XlwMenuSetValues (current, request, new)
1782 Widget current;
1783 Widget request;
1784 Widget new;
07bf635f
RS
1785{
1786 XlwMenuWidget oldmw = (XlwMenuWidget)current;
1787 XlwMenuWidget newmw = (XlwMenuWidget)new;
1788 Boolean redisplay = False;
1789 int i;
1790
1791 if (newmw->menu.contents
1792 && newmw->menu.contents->contents
1793 && newmw->menu.contents->contents->change >= VISIBLE_CHANGE)
1794 redisplay = True;
ba624d0f
RS
1795 /* Do redisplay if the contents are entirely eliminated. */
1796 if (newmw->menu.contents
1797 && newmw->menu.contents->contents == 0
1798 && newmw->menu.contents->change >= VISIBLE_CHANGE)
1799 redisplay = True;
07bf635f
RS
1800
1801 if (newmw->core.background_pixel != oldmw->core.background_pixel
d398028f
PR
1802 || newmw->menu.foreground != oldmw->menu.foreground
1803 || newmw->menu.font != oldmw->menu.font)
07bf635f
RS
1804 {
1805 release_drawing_gcs (newmw);
1806 make_drawing_gcs (newmw);
90c7e9f0
MB
1807
1808 release_shadow_gcs (newmw);
1809 /* Cause the shadow colors to be recalculated. */
1810 newmw->menu.top_shadow_color = -1;
1811 newmw->menu.bottom_shadow_color = -1;
1812 make_shadow_gcs (newmw);
1813
07bf635f
RS
1814 redisplay = True;
1815
43aa2f1b 1816 if (XtIsRealized (current))
da353f23
MB
1817 /* If the menu is currently displayed, change the display. */
1818 for (i = 0; i < oldmw->menu.windows_length; i++)
1819 {
1820 XSetWindowBackground (XtDisplay (oldmw),
1821 oldmw->menu.windows [i].window,
1822 newmw->core.background_pixel);
1823 /* clear windows and generate expose events */
1824 XClearArea (XtDisplay (oldmw), oldmw->menu.windows[i].window,
1825 0, 0, 0, 0, True);
1826 }
07bf635f
RS
1827 }
1828
1829 return redisplay;
1830}
1831
1832static void
d398028f
PR
1833XlwMenuResize (w)
1834 Widget w;
07bf635f
RS
1835{
1836 XlwMenuWidget mw = (XlwMenuWidget)w;
1837
d398028f
PR
1838 if (mw->menu.popped_up)
1839 {
1840 /* Don't allow the popup menu to resize itself. */
1841 mw->core.width = mw->menu.windows [0].width;
1842 mw->core.height = mw->menu.windows [0].height;
1843 mw->core.parent->core.width = mw->core.width ;
1844 mw->core.parent->core.height = mw->core.height ;
1845 }
1846 else
1847 {
1848 mw->menu.windows [0].width = mw->core.width;
1849 mw->menu.windows [0].height = mw->core.height;
1850 }
07bf635f
RS
1851}
1852
1853\f/* Action procedures */
1854static void
d398028f
PR
1855handle_single_motion_event (mw, ev)
1856 XlwMenuWidget mw;
1857 XMotionEvent* ev;
07bf635f
RS
1858{
1859 widget_value* val;
1860 int level;
1861
1862 if (!map_event_to_widget_value (mw, ev, &val, &level))
1863 pop_new_stack_if_no_contents (mw);
1864 else
1865 set_new_state (mw, val, level);
1866 remap_menubar (mw);
1867
1868 /* Sync with the display. Makes it feel better on X terms. */
1869 XSync (XtDisplay (mw), False);
1870}
1871
1872static void
d398028f
PR
1873handle_motion_event (mw, ev)
1874 XlwMenuWidget mw;
1875 XMotionEvent* ev;
07bf635f
RS
1876{
1877 int x = ev->x_root;
1878 int y = ev->y_root;
1879 int state = ev->state;
1880
1881 handle_single_motion_event (mw, ev);
1882
1883 /* allow motion events to be generated again */
1884 if (ev->is_hint
1885 && XQueryPointer (XtDisplay (mw), ev->window,
1886 &ev->root, &ev->subwindow,
1887 &ev->x_root, &ev->y_root,
1888 &ev->x, &ev->y,
1889 &ev->state)
1890 && ev->state == state
1891 && (ev->x_root != x || ev->y_root != y))
1892 handle_single_motion_event (mw, ev);
1893}
1894
1895static void
d398028f
PR
1896Start (w, ev, params, num_params)
1897 Widget w;
1898 XEvent *ev;
1899 String *params;
1900 Cardinal *num_params;
07bf635f
RS
1901{
1902 XlwMenuWidget mw = (XlwMenuWidget)w;
1903
d398028f
PR
1904 if (!mw->menu.popped_up)
1905 {
1906 menu_post_event = *ev;
a57a1605 1907 pop_up_menu (mw, (XButtonPressedEvent*) ev);
d398028f
PR
1908 }
1909 else
4cc76151
PR
1910 {
1911 /* If we push a button while the menu is posted semipermanently,
1912 releasing the button should always pop the menu down. */
1913 next_release_must_exit = 1;
d398028f 1914
4cc76151
PR
1915 /* notes the absolute position of the menubar window */
1916 mw->menu.windows [0].x = ev->xmotion.x_root - ev->xmotion.x;
1917 mw->menu.windows [0].y = ev->xmotion.y_root - ev->xmotion.y;
07bf635f 1918
4cc76151
PR
1919 /* handles the down like a move, slots are compatible */
1920 handle_motion_event (mw, &ev->xmotion);
1921 }
07bf635f
RS
1922}
1923
1924static void
d398028f
PR
1925Drag (w, ev, params, num_params)
1926 Widget w;
1927 XEvent *ev;
1928 String *params;
1929 Cardinal *num_params;
07bf635f
RS
1930{
1931 XlwMenuWidget mw = (XlwMenuWidget)w;
3c9ce1c4
KH
1932 if (mw->menu.popped_up)
1933 handle_motion_event (mw, &ev->xmotion);
07bf635f
RS
1934}
1935
ba624d0f
RS
1936/* Do nothing.
1937 This is how we handle presses and releases of modifier keys. */
1938static void
1939Nothing (w, ev, params, num_params)
1940 Widget w;
1941 XEvent *ev;
1942 String *params;
1943 Cardinal *num_params;
1944{
1945}
1946
1947/* Handle key press and release events while menu is popped up.
1948 Our action is to get rid of the menu. */
1949static void
1950Key (w, ev, params, num_params)
1951 Widget w;
1952 XEvent *ev;
1953 String *params;
1954 Cardinal *num_params;
1955{
1956 XlwMenuWidget mw = (XlwMenuWidget)w;
1957
1958 /* Pop down everything. */
1959 mw->menu.new_depth = 1;
1960 remap_menubar (mw);
1961
1962 if (mw->menu.popped_up)
1963 {
1964 mw->menu.popped_up = False;
1965 XtUngrabPointer ((Widget)mw, ev->xmotion.time);
1966 if (XtIsShell (XtParent ((Widget) mw)))
1967 XtPopdown (XtParent ((Widget) mw));
1968 else
1969 {
1970 XtRemoveGrab ((Widget) mw);
1971 display_menu (mw, 0, False, NULL, NULL, NULL, NULL, NULL);
1972 }
1973 }
1974
1975 /* callback */
1976 XtCallCallbackList ((Widget)mw, mw->menu.select, (XtPointer)0);
1977}
1978
1979static void
d398028f
PR
1980Select (w, ev, params, num_params)
1981 Widget w;
1982 XEvent *ev;
1983 String *params;
1984 Cardinal *num_params;
07bf635f
RS
1985{
1986 XlwMenuWidget mw = (XlwMenuWidget)w;
1987 widget_value* selected_item = mw->menu.old_stack [mw->menu.old_depth - 1];
1988
d398028f
PR
1989 /* If user releases the button quickly, without selecting anything,
1990 after the initial down-click that brought the menu up,
1991 do nothing. */
1992 if ((selected_item == 0
1993 || ((widget_value *) selected_item)->call_data == 0)
1994 && !next_release_must_exit
1995 && (ev->xbutton.time - menu_post_event.xbutton.time
1996 < XtGetMultiClickTime (XtDisplay (w))))
1997 return;
1998
1999 /* pop down everything. */
07bf635f
RS
2000 mw->menu.new_depth = 1;
2001 remap_menubar (mw);
2002
2003 if (mw->menu.popped_up)
2004 {
2005 mw->menu.popped_up = False;
2006 XtUngrabPointer ((Widget)mw, ev->xmotion.time);
2289f3f4
PR
2007 if (XtIsShell (XtParent ((Widget) mw)))
2008 XtPopdown (XtParent ((Widget) mw));
4cc76151
PR
2009 else
2010 {
2011 XtRemoveGrab ((Widget) mw);
2012 display_menu (mw, 0, False, NULL, NULL, NULL, NULL, NULL);
2013 }
07bf635f
RS
2014 }
2015
2016 /* callback */
2017 XtCallCallbackList ((Widget)mw, mw->menu.select, (XtPointer)selected_item);
07bf635f
RS
2018}
2019
2020
2021\f/* Special code to pop-up a menu */
2022void
d398028f
PR
2023pop_up_menu (mw, event)
2024 XlwMenuWidget mw;
2025 XButtonPressedEvent* event;
07bf635f
RS
2026{
2027 int x = event->x_root;
2028 int y = event->y_root;
2029 int w;
2030 int h;
2031 int borderwidth = mw->menu.shadow_thickness;
2032 Screen* screen = XtScreen (mw);
8400b9ed 2033 Display *display = XtDisplay (mw);
5efb61c7 2034 int count;
07bf635f 2035
d398028f
PR
2036 next_release_must_exit = 0;
2037
07bf635f
RS
2038 XtCallCallbackList ((Widget)mw, mw->menu.open, NULL);
2039
2289f3f4 2040 if (XtIsShell (XtParent ((Widget)mw)))
4cc76151 2041 size_menu (mw, 0);
07bf635f
RS
2042
2043 w = mw->menu.windows [0].width;
2044 h = mw->menu.windows [0].height;
2045
2046 x -= borderwidth;
2047 y -= borderwidth;
2048 if (x < borderwidth)
2049 x = borderwidth;
2050 if (x + w + 2 * borderwidth > WidthOfScreen (screen))
2051 x = WidthOfScreen (screen) - w - 2 * borderwidth;
2052 if (y < borderwidth)
2053 y = borderwidth;
2054 if (y + h + 2 * borderwidth> HeightOfScreen (screen))
2055 y = HeightOfScreen (screen) - h - 2 * borderwidth;
2056
2057 mw->menu.popped_up = True;
2289f3f4 2058 if (XtIsShell (XtParent ((Widget)mw)))
4cc76151 2059 {
2289f3f4
PR
2060 XtConfigureWidget (XtParent ((Widget)mw), x, y, w, h,
2061 XtParent ((Widget)mw)->core.border_width);
2062 XtPopup (XtParent ((Widget)mw), XtGrabExclusive);
4cc76151
PR
2063 display_menu (mw, 0, False, NULL, NULL, NULL, NULL, NULL);
2064 mw->menu.windows [0].x = x + borderwidth;
2065 mw->menu.windows [0].y = y + borderwidth;
2066 }
2067 else
2068 {
2069 XEvent *ev = (XEvent *) event;
2070
87a559bf 2071 XtAddGrab ((Widget) mw, True, True);
4cc76151
PR
2072
2073 /* notes the absolute position of the menubar window */
2074 mw->menu.windows [0].x = ev->xmotion.x_root - ev->xmotion.x;
2075 mw->menu.windows [0].y = ev->xmotion.y_root - ev->xmotion.y;
2076 }
2077
d398028f 2078#ifdef emacs
5efb61c7 2079 count = x_catch_errors (display);
d398028f 2080#endif
87a559bf
PR
2081 XtGrabPointer ((Widget)mw, False,
2082 (PointerMotionMask
2083 | PointerMotionHintMask
2084 | ButtonReleaseMask
2085 | ButtonPressMask),
2086 GrabModeAsync, GrabModeAsync, None,
2087 mw->menu.cursor_shape,
2088 event->time);
2089 pointer_grabbed = 1;
d398028f 2090#ifdef emacs
8400b9ed 2091 if (x_had_errors_p (display))
d398028f
PR
2092 {
2093 pointer_grabbed = 0;
2094 XtUngrabPointer ((Widget)mw, event->time);
2095 }
5efb61c7 2096 x_uncatch_errors (display, count);
d398028f 2097#endif
07bf635f 2098
07bf635f
RS
2099 handle_motion_event (mw, (XMotionEvent*)event);
2100}