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