]> code.delx.au - gnu-emacs/blob - lwlib/xlwmenu.c
(menu-bar-help-menu): Add finder-by-keyword.
[gnu-emacs] / lwlib / xlwmenu.c
1 /* Implements a lightweight menubar widget.
2 Copyright (C) 1992 Lucid, Inc.
3
4 This file is part of the Lucid Widget Library.
5
6 The Lucid Widget Library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
9 any later version.
10
11 The Lucid Widget Library is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 Alongalong with GNU Emacs; see the file COPYING. If not, write to
18 the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
19
20 /* Created by devin@lucid.com */
21
22 #include <stdio.h>
23
24 #include <sys/types.h>
25 #include <X11/Xos.h>
26 #include <X11/IntrinsicP.h>
27 #include <X11/ObjectP.h>
28 #include <X11/StringDefs.h>
29 #include <X11/cursorfont.h>
30 #include <X11/bitmaps/gray>
31 #include "xlwmenuP.h"
32
33 static int pointer_grabbed;
34 static XEvent menu_post_event;
35
36 XFontStruct *xlwmenu_default_font;
37
38 static char
39 xlwMenuTranslations [] =
40 "<BtnDown>: start()\n\
41 <Motion>: drag()\n\
42 <BtnUp>: select()\n\
43 ";
44
45 #define offset(field) XtOffset(XlwMenuWidget, field)
46 static XtResource
47 xlwMenuResources[] =
48 {
49 {XtNfont, XtCFont, XtRFontStruct, sizeof(XFontStruct *),
50 offset(menu.font),XtRString, "XtDefaultFont"},
51 {XtNforeground, XtCForeground, XtRPixel, sizeof(Pixel),
52 offset(menu.foreground), XtRString, "XtDefaultForeground"},
53 {XtNbuttonForeground, XtCButtonForeground, XtRPixel, sizeof(Pixel),
54 offset(menu.button_foreground), XtRString, "XtDefaultForeground"},
55 {XtNmargin, XtCMargin, XtRDimension, sizeof(Dimension),
56 offset(menu.margin), XtRImmediate, (XtPointer)0},
57 {XtNhorizontalSpacing, XtCMargin, XtRDimension, sizeof(Dimension),
58 offset(menu.horizontal_spacing), XtRImmediate, (XtPointer)3},
59 {XtNverticalSpacing, XtCMargin, XtRDimension, sizeof(Dimension),
60 offset(menu.vertical_spacing), XtRImmediate, (XtPointer)1},
61 {XtNarrowSpacing, XtCMargin, XtRDimension, sizeof(Dimension),
62 offset(menu.arrow_spacing), XtRImmediate, (XtPointer)10},
63
64 {XmNshadowThickness, XmCShadowThickness, XtRDimension,
65 sizeof (Dimension), offset (menu.shadow_thickness),
66 XtRImmediate, (XtPointer) 2},
67 {XmNtopShadowColor, XmCTopShadowColor, XtRPixel, sizeof (Pixel),
68 offset (menu.top_shadow_color), XtRImmediate, (XtPointer)-1},
69 {XmNbottomShadowColor, XmCBottomShadowColor, XtRPixel, sizeof (Pixel),
70 offset (menu.bottom_shadow_color), XtRImmediate, (XtPointer)-1},
71 {XmNtopShadowPixmap, XmCTopShadowPixmap, XtRPixmap, sizeof (Pixmap),
72 offset (menu.top_shadow_pixmap), XtRImmediate, (XtPointer)None},
73 {XmNbottomShadowPixmap, XmCBottomShadowPixmap, XtRPixmap, sizeof (Pixmap),
74 offset (menu.bottom_shadow_pixmap), XtRImmediate, (XtPointer)None},
75
76 {XtNopen, XtCCallback, XtRCallback, sizeof(XtPointer),
77 offset(menu.open), XtRCallback, (XtPointer)NULL},
78 {XtNselect, XtCCallback, XtRCallback, sizeof(XtPointer),
79 offset(menu.select), XtRCallback, (XtPointer)NULL},
80 {XtNmenu, XtCMenu, XtRPointer, sizeof(XtPointer),
81 offset(menu.contents), XtRImmediate, (XtPointer)NULL},
82 {XtNcursor, XtCCursor, XtRCursor, sizeof(Cursor),
83 offset(menu.cursor_shape), XtRString, (XtPointer)"right_ptr"},
84 {XtNhorizontal, XtCHorizontal, XtRInt, sizeof(int),
85 offset(menu.horizontal), XtRImmediate, (XtPointer)True},
86 };
87 #undef offset
88
89 static Boolean XlwMenuSetValues();
90 static void XlwMenuRealize();
91 static void XlwMenuRedisplay();
92 static void XlwMenuResize();
93 static void XlwMenuInitialize();
94 static void XlwMenuRedisplay();
95 static void XlwMenuDestroy();
96 static void XlwMenuClassInitialize();
97 static void Start();
98 static void Drag();
99 static void Select();
100
101 static XtActionsRec
102 xlwMenuActionsList [] =
103 {
104 {"start", Start},
105 {"drag", Drag},
106 {"select", Select},
107 };
108
109 #define SuperClass ((CoreWidgetClass)&coreClassRec)
110
111 XlwMenuClassRec xlwMenuClassRec =
112 {
113 { /* CoreClass fields initialization */
114 (WidgetClass) SuperClass, /* superclass */
115 "XlwMenu", /* class_name */
116 sizeof(XlwMenuRec), /* size */
117 XlwMenuClassInitialize, /* class_initialize */
118 NULL, /* class_part_initialize */
119 FALSE, /* class_inited */
120 XlwMenuInitialize, /* initialize */
121 NULL, /* initialize_hook */
122 XlwMenuRealize, /* realize */
123 xlwMenuActionsList, /* actions */
124 XtNumber(xlwMenuActionsList), /* num_actions */
125 xlwMenuResources, /* resources */
126 XtNumber(xlwMenuResources), /* resource_count */
127 NULLQUARK, /* xrm_class */
128 TRUE, /* compress_motion */
129 TRUE, /* compress_exposure */
130 TRUE, /* compress_enterleave */
131 FALSE, /* visible_interest */
132 XlwMenuDestroy, /* destroy */
133 XlwMenuResize, /* resize */
134 XlwMenuRedisplay, /* expose */
135 XlwMenuSetValues, /* set_values */
136 NULL, /* set_values_hook */
137 XtInheritSetValuesAlmost, /* set_values_almost */
138 NULL, /* get_values_hook */
139 NULL, /* accept_focus */
140 XtVersion, /* version */
141 NULL, /* callback_private */
142 xlwMenuTranslations, /* tm_table */
143 XtInheritQueryGeometry, /* query_geometry */
144 XtInheritDisplayAccelerator, /* display_accelerator */
145 NULL /* extension */
146 }, /* XlwMenuClass fields initialization */
147 {
148 0 /* dummy */
149 },
150 };
151
152 WidgetClass xlwMenuWidgetClass = (WidgetClass) &xlwMenuClassRec;
153
154 int submenu_destroyed;
155
156 static int next_release_must_exit;
157
158 \f/* Utilities */
159 static void
160 push_new_stack (mw, val)
161 XlwMenuWidget mw;
162 widget_value* val;
163 {
164 if (!mw->menu.new_stack)
165 {
166 mw->menu.new_stack_length = 10;
167 mw->menu.new_stack =
168 (widget_value**)XtCalloc (mw->menu.new_stack_length,
169 sizeof (widget_value*));
170 }
171 else if (mw->menu.new_depth == mw->menu.new_stack_length)
172 {
173 mw->menu.new_stack_length *= 2;
174 mw->menu.new_stack =
175 (widget_value**)XtRealloc ((char*)mw->menu.new_stack,
176 mw->menu.new_stack_length * sizeof (widget_value*));
177 }
178 mw->menu.new_stack [mw->menu.new_depth++] = val;
179 }
180
181 static void
182 pop_new_stack_if_no_contents (mw)
183 XlwMenuWidget mw;
184 {
185 if (mw->menu.new_depth)
186 {
187 if (!mw->menu.new_stack [mw->menu.new_depth - 1]->contents)
188 mw->menu.new_depth -= 1;
189 }
190 }
191
192 static void
193 make_old_stack_space (mw, n)
194 XlwMenuWidget mw;
195 int n;
196 {
197 if (!mw->menu.old_stack)
198 {
199 mw->menu.old_stack_length = 10;
200 mw->menu.old_stack =
201 (widget_value**)XtCalloc (mw->menu.old_stack_length,
202 sizeof (widget_value*));
203 }
204 else if (mw->menu.old_stack_length < n)
205 {
206 mw->menu.old_stack_length *= 2;
207 mw->menu.old_stack =
208 (widget_value**)XtRealloc ((char*)mw->menu.old_stack,
209 mw->menu.old_stack_length * sizeof (widget_value*));
210 }
211 }
212
213 \f/* Size code */
214 static Boolean
215 all_dashes_p (s)
216 char *s;
217 {
218 char* p;
219 for (p = s; *p == '-'; p++);
220 return !*p;
221 }
222
223 int
224 string_width (mw, s)
225 XlwMenuWidget mw;
226 char *s;
227 {
228 XCharStruct xcs;
229 int drop;
230
231 XTextExtents (mw->menu.font, s, strlen (s), &drop, &drop, &drop, &xcs);
232 return xcs.width;
233 }
234
235 static int
236 arrow_width (mw)
237 XlwMenuWidget mw;
238 {
239 return mw->menu.font->ascent / 2 | 1;
240 }
241
242 static XtResource
243 nameResource[] =
244 {
245 {"labelString", "LabelString", XtRString, sizeof(String),
246 0, XtRImmediate, 0},
247 };
248
249 static char*
250 resource_widget_value (mw, val)
251 XlwMenuWidget mw;
252 widget_value *val;
253 {
254 if (!val->toolkit_data)
255 {
256 char* resourced_name = NULL;
257 char* complete_name;
258 XtGetSubresources ((Widget) mw,
259 (XtPointer) &resourced_name,
260 val->name, val->name,
261 nameResource, 1, NULL, 0);
262 if (!resourced_name)
263 resourced_name = val->name;
264 if (!val->value)
265 {
266 complete_name = (char *) XtMalloc (strlen (resourced_name) + 1);
267 strcpy (complete_name, resourced_name);
268 }
269 else
270 {
271 int complete_length =
272 strlen (resourced_name) + strlen (val->value) + 2;
273 complete_name = XtMalloc (complete_length);
274 *complete_name = 0;
275 strcat (complete_name, resourced_name);
276 strcat (complete_name, " ");
277 strcat (complete_name, val->value);
278 }
279
280 val->toolkit_data = complete_name;
281 val->free_toolkit_data = True;
282 }
283 return (char*)val->toolkit_data;
284 }
285
286 /* Returns the sizes of an item */
287 static void
288 size_menu_item (mw, val, horizontal_p, label_width, rest_width, height)
289 XlwMenuWidget mw;
290 widget_value* val;
291 int horizontal_p;
292 int* label_width;
293 int* rest_width;
294 int* height;
295 {
296 if (all_dashes_p (val->name))
297 {
298 *height = 2;
299 *label_width = 1;
300 *rest_width = 0;
301 }
302 else
303 {
304 *height =
305 mw->menu.font->ascent + mw->menu.font->descent
306 + 2 * mw->menu.vertical_spacing + 2 * mw->menu.shadow_thickness;
307
308 *label_width =
309 string_width (mw, resource_widget_value (mw, val))
310 + mw->menu.horizontal_spacing + mw->menu.shadow_thickness;
311
312 *rest_width = mw->menu.horizontal_spacing + mw->menu.shadow_thickness;
313 if (!horizontal_p)
314 {
315 if (val->contents)
316 *rest_width += arrow_width (mw) + mw->menu.arrow_spacing;
317 else if (val->key)
318 *rest_width +=
319 string_width (mw, val->key) + mw->menu.arrow_spacing;
320 }
321 }
322 }
323
324 static void
325 size_menu (mw, level)
326 XlwMenuWidget mw;
327 int level;
328 {
329 unsigned int label_width = 0;
330 int rest_width = 0;
331 int max_rest_width = 0;
332 unsigned int height = 0;
333 int horizontal_p = mw->menu.horizontal && (level == 0);
334 widget_value* val;
335 window_state* ws;
336
337 if (level >= mw->menu.old_depth)
338 abort ();
339
340 ws = &mw->menu.windows [level];
341 ws->width = 0;
342 ws->height = 0;
343 ws->label_width = 0;
344
345 for (val = mw->menu.old_stack [level]->contents; val; val = val->next)
346 {
347 size_menu_item (mw, val, horizontal_p, &label_width, &rest_width,
348 &height);
349 if (horizontal_p)
350 {
351 ws->width += label_width + rest_width;
352 if (height > ws->height)
353 ws->height = height;
354 }
355 else
356 {
357 if (label_width > ws->label_width)
358 ws->label_width = label_width;
359 if (rest_width > max_rest_width)
360 max_rest_width = rest_width;
361 ws->height += height;
362 }
363 }
364
365 if (horizontal_p)
366 ws->label_width = 0;
367 else
368 ws->width = ws->label_width + max_rest_width;
369
370 ws->width += 2 * mw->menu.shadow_thickness;
371 ws->height += 2 * mw->menu.shadow_thickness;
372 }
373
374
375 \f/* Display code */
376 static void
377 draw_arrow (mw, window, gc, x, y, width)
378 XlwMenuWidget mw;
379 Window window;
380 GC gc;
381 int x;
382 int y;
383 int width;
384 {
385 XPoint points [3];
386 points [0].x = x;
387 points [0].y = y + mw->menu.font->ascent;
388 points [1].x = x;
389 points [1].y = y;
390 points [2].x = x + width;
391 points [2].y = y + mw->menu.font->ascent / 2;
392
393 XFillPolygon (XtDisplay (mw), window, gc, points, 3, Convex,
394 CoordModeOrigin);
395 }
396
397 static void
398 draw_shadow_rectangle (mw, window, x, y, width, height, erase_p)
399 XlwMenuWidget mw;
400 Window window;
401 int x;
402 int y;
403 int width;
404 int height;
405 int erase_p;
406 {
407 Display *dpy = XtDisplay (mw);
408 GC top_gc = !erase_p ? mw->menu.shadow_top_gc : mw->menu.background_gc;
409 GC bottom_gc = !erase_p ? mw->menu.shadow_bottom_gc : mw->menu.background_gc;
410 int thickness = mw->menu.shadow_thickness;
411 XPoint points [4];
412 points [0].x = x;
413 points [0].y = y;
414 points [1].x = x + width;
415 points [1].y = y;
416 points [2].x = x + width - thickness;
417 points [2].y = y + thickness;
418 points [3].x = x;
419 points [3].y = y + thickness;
420 XFillPolygon (dpy, window, top_gc, points, 4, Convex, CoordModeOrigin);
421 points [0].x = x;
422 points [0].y = y + thickness;
423 points [1].x = x;
424 points [1].y = y + height;
425 points [2].x = x + thickness;
426 points [2].y = y + height - thickness;
427 points [3].x = x + thickness;
428 points [3].y = y + thickness;
429 XFillPolygon (dpy, window, top_gc, points, 4, Convex, CoordModeOrigin);
430 points [0].x = x + width;
431 points [0].y = y;
432 points [1].x = x + width - thickness;
433 points [1].y = y + thickness;
434 points [2].x = x + width - thickness;
435 points [2].y = y + height - thickness;
436 points [3].x = x + width;
437 points [3].y = y + height - thickness;
438 XFillPolygon (dpy, window, bottom_gc, points, 4, Convex, CoordModeOrigin);
439 points [0].x = x;
440 points [0].y = y + height;
441 points [1].x = x + width;
442 points [1].y = y + height;
443 points [2].x = x + width;
444 points [2].y = y + height - thickness;
445 points [3].x = x + thickness;
446 points [3].y = y + height - thickness;
447 XFillPolygon (dpy, window, bottom_gc, points, 4, Convex, CoordModeOrigin);
448 }
449
450
451 /* Display the menu item and increment where.x and where.y to show how large
452 ** the menu item was.
453 */
454 static void
455 display_menu_item (mw, val, ws, where, highlighted_p, horizontal_p, just_compute_p)
456 XlwMenuWidget mw;
457 widget_value* val;
458 window_state* ws;
459 XPoint* where;
460 Boolean highlighted_p;
461 Boolean horizontal_p;
462 Boolean just_compute_p;
463 {
464 GC deco_gc;
465 GC text_gc;
466 int font_ascent = mw->menu.font->ascent;
467 int font_descent = mw->menu.font->descent;
468 int shadow = mw->menu.shadow_thickness;
469 int separator_p = all_dashes_p (val->name);
470 int h_spacing = mw->menu.horizontal_spacing;
471 int v_spacing = mw->menu.vertical_spacing;
472 int label_width;
473 int rest_width;
474 int height;
475 int width;
476 int button_p;
477
478 /* compute the sizes of the item */
479 size_menu_item (mw, val, horizontal_p, &label_width, &rest_width, &height);
480
481 if (horizontal_p)
482 width = label_width + rest_width;
483 else
484 {
485 label_width = ws->label_width;
486 width = ws->width - 2 * shadow;
487 }
488
489 /* see if it should be a button in the menubar */
490 button_p = horizontal_p && val->call_data;
491
492 /* Only highlight an enabled item that has a callback. */
493 if (highlighted_p)
494 if (!val->enabled || !(val->call_data || val->contents))
495 highlighted_p = 0;
496
497 /* do the drawing. */
498 if (!just_compute_p)
499 {
500 /* Add the shadow border of the containing menu */
501 int x = where->x + shadow;
502 int y = where->y + shadow;
503
504 /* pick the foreground and background GC. */
505 if (val->enabled)
506 text_gc = button_p ? mw->menu.button_gc : mw->menu.foreground_gc;
507 else
508 text_gc =
509 button_p ? mw->menu.inactive_button_gc : mw->menu.inactive_gc;
510 deco_gc = mw->menu.foreground_gc;
511
512 if (separator_p)
513 {
514 XDrawLine (XtDisplay (mw), ws->window, mw->menu.shadow_bottom_gc,
515 x, y, x + width, y);
516 XDrawLine (XtDisplay (mw), ws->window, mw->menu.shadow_top_gc,
517 x, y + 1, x + width, y + 1);
518 }
519 else
520 {
521 int x_offset = x + h_spacing + shadow;
522 char* display_string = resource_widget_value (mw, val);
523 draw_shadow_rectangle (mw, ws->window, x, y, width, height, True);
524
525 /* Deal with centering a menu title. */
526 if (!horizontal_p && !val->contents && !val->call_data)
527 {
528 int l = string_width (mw, display_string);
529
530 if (width > l)
531 x_offset = (width - l) >> 1;
532 }
533 XDrawString (XtDisplay (mw), ws->window, text_gc, x_offset,
534 y + v_spacing + shadow + font_ascent,
535 display_string, strlen (display_string));
536
537 if (!horizontal_p)
538 {
539 if (val->contents)
540 {
541 int a_w = arrow_width (mw);
542 draw_arrow (mw, ws->window, deco_gc,
543 x + width - arrow_width (mw)
544 - mw->menu.horizontal_spacing
545 - mw->menu.shadow_thickness,
546 y + v_spacing + shadow, a_w);
547 }
548 else if (val->key)
549 {
550 XDrawString (XtDisplay (mw), ws->window, text_gc,
551 x + label_width + mw->menu.arrow_spacing,
552 y + v_spacing + shadow + font_ascent,
553 val->key, strlen (val->key));
554 }
555 }
556
557 else if (button_p)
558 {
559 #if 1
560 XDrawRectangle (XtDisplay (mw), ws->window, deco_gc,
561 x + shadow, y + shadow,
562 label_width + h_spacing - 1,
563 font_ascent + font_descent + 2 * v_spacing - 1);
564 draw_shadow_rectangle (mw, ws->window, x, y, width, height,
565 False);
566 #else
567 highlighted_p = True;
568 #endif
569 }
570 else
571 {
572 XDrawRectangle (XtDisplay (mw), ws->window,
573 mw->menu.background_gc,
574 x + shadow, y + shadow,
575 label_width + h_spacing - 1,
576 font_ascent + font_descent + 2 * v_spacing - 1);
577 draw_shadow_rectangle (mw, ws->window, x, y, width, height,
578 True);
579 }
580
581 if (highlighted_p)
582 draw_shadow_rectangle (mw, ws->window, x, y, width, height, False);
583 }
584 }
585
586 where->x += width;
587 where->y += height;
588 }
589
590 static void
591 display_menu (mw, level, just_compute_p, highlighted_pos, hit, hit_return,
592 this, that)
593 XlwMenuWidget mw;
594 int level;
595 Boolean just_compute_p;
596 XPoint* highlighted_pos;
597 XPoint* hit;
598 widget_value** hit_return;
599 widget_value* this;
600 widget_value* that;
601 {
602 widget_value* val;
603 widget_value* following_item;
604 window_state* ws;
605 XPoint where;
606 int horizontal_p = mw->menu.horizontal && (level == 0);
607 int highlighted_p;
608 int just_compute_this_one_p;
609
610 if (level >= mw->menu.old_depth)
611 abort ();
612
613 if (level < mw->menu.old_depth - 1)
614 following_item = mw->menu.old_stack [level + 1];
615 else
616 following_item = NULL;
617
618 if (hit)
619 *hit_return = NULL;
620
621 where.x = 0;
622 where.y = 0;
623
624 ws = &mw->menu.windows [level];
625 for (val = mw->menu.old_stack [level]->contents; val; val = val->next)
626 {
627 highlighted_p = val == following_item;
628 if (highlighted_p && highlighted_pos)
629 {
630 if (horizontal_p)
631 highlighted_pos->x = where.x;
632 else
633 highlighted_pos->y = where.y;
634 }
635
636 just_compute_this_one_p =
637 just_compute_p || ((this || that) && val != this && val != that);
638
639 display_menu_item (mw, val, ws, &where, highlighted_p, horizontal_p,
640 just_compute_this_one_p);
641
642 if (highlighted_p && highlighted_pos)
643 {
644 if (horizontal_p)
645 highlighted_pos->y = where.y;
646 else
647 highlighted_pos->x = where.x;
648 }
649
650 if (hit
651 && !*hit_return
652 && (horizontal_p ? hit->x < where.x : hit->y < where.y)
653 && !all_dashes_p (val->name))
654 *hit_return = val;
655
656 if (horizontal_p)
657 where.y = 0;
658 else
659 where.x = 0;
660 }
661
662 if (!just_compute_p)
663 draw_shadow_rectangle (mw, ws->window, 0, 0, ws->width, ws->height, False);
664 }
665
666 \f/* Motion code */
667 static void
668 set_new_state (mw, val, level)
669 XlwMenuWidget mw;
670 widget_value* val;
671 int level;
672 {
673 int i;
674
675 mw->menu.new_depth = 0;
676 for (i = 0; i < level; i++)
677 push_new_stack (mw, mw->menu.old_stack [i]);
678 push_new_stack (mw, val);
679 }
680
681 static void
682 make_windows_if_needed (mw, n)
683 XlwMenuWidget mw;
684 int n;
685 {
686 int i;
687 int start_at;
688 XSetWindowAttributes xswa;
689 int mask;
690 Window root = RootWindowOfScreen (DefaultScreenOfDisplay (XtDisplay (mw)));
691 window_state* windows;
692
693 if (mw->menu.windows_length >= n)
694 return;
695
696 xswa.save_under = True;
697 xswa.override_redirect = True;
698 xswa.background_pixel = mw->core.background_pixel;
699 xswa.border_pixel = mw->core.border_pixel;
700 xswa.event_mask =
701 ExposureMask | PointerMotionMask | PointerMotionHintMask
702 | ButtonReleaseMask | ButtonPressMask;
703 xswa.cursor = mw->menu.cursor_shape;
704 mask = CWSaveUnder | CWOverrideRedirect | CWBackPixel | CWBorderPixel
705 | CWEventMask | CWCursor;
706
707 if (!mw->menu.windows)
708 {
709 mw->menu.windows =
710 (window_state*)XtMalloc (n * sizeof (window_state));
711 start_at = 0;
712 }
713 else
714 {
715 mw->menu.windows =
716 (window_state*)XtRealloc ((char*)mw->menu.windows,
717 n * sizeof (window_state));
718 start_at = mw->menu.windows_length;
719 }
720 mw->menu.windows_length = n;
721
722 windows = mw->menu.windows;
723
724 for (i = start_at; i < n; i++)
725 {
726 windows [i].x = 0;
727 windows [i].y = 0;
728 windows [i].width = 1;
729 windows [i].height = 1;
730 windows [i].window =
731 XCreateWindow (XtDisplay (mw), root, 0, 0, 1, 1,
732 0, 0, CopyFromParent, CopyFromParent, mask, &xswa);
733 }
734 }
735
736 /* Make the window fit in the screen */
737 static void
738 fit_to_screen (mw, ws, previous_ws, horizontal_p)
739 XlwMenuWidget mw;
740 window_state* ws;
741 window_state* previous_ws;
742 Boolean horizontal_p;
743 {
744 unsigned int screen_width = WidthOfScreen (XtScreen (mw));
745 unsigned int screen_height = HeightOfScreen (XtScreen (mw));
746
747 if (ws->x < 0)
748 ws->x = 0;
749 else if (ws->x + ws->width > screen_width)
750 {
751 if (!horizontal_p)
752 ws->x = previous_ws->x - ws->width;
753 else
754 ws->x = screen_width - ws->width;
755 if (ws->x < 0)
756 ws->x = 0;
757 }
758 if (ws->y < 0)
759 ws->y = 0;
760 else if (ws->y + ws->height > screen_height)
761 {
762 if (horizontal_p)
763 ws->y = previous_ws->y - ws->height;
764 else
765 ws->y = screen_height - ws->height;
766 if (ws->y < 0)
767 ws->y = 0;
768 }
769 }
770
771 /* Updates old_stack from new_stack and redisplays. */
772 static void
773 remap_menubar (mw)
774 XlwMenuWidget mw;
775 {
776 int i;
777 int last_same;
778 XPoint selection_position;
779 int old_depth = mw->menu.old_depth;
780 int new_depth = mw->menu.new_depth;
781 widget_value** old_stack;
782 widget_value** new_stack;
783 window_state* windows;
784 widget_value* old_selection;
785 widget_value* new_selection;
786
787 /* Check that enough windows and old_stack are ready. */
788 make_windows_if_needed (mw, new_depth);
789 make_old_stack_space (mw, new_depth);
790 windows = mw->menu.windows;
791 old_stack = mw->menu.old_stack;
792 new_stack = mw->menu.new_stack;
793
794 /* compute the last identical different entry */
795 for (i = 1; i < old_depth && i < new_depth; i++)
796 if (old_stack [i] != new_stack [i])
797 break;
798 last_same = i - 1;
799
800 /* Memorize the previously selected item to be able to refresh it */
801 old_selection = last_same + 1 < old_depth ? old_stack [last_same + 1] : NULL;
802 if (old_selection && !old_selection->enabled)
803 old_selection = NULL;
804 new_selection = last_same + 1 < new_depth ? new_stack [last_same + 1] : NULL;
805 if (new_selection && !new_selection->enabled)
806 new_selection = NULL;
807
808 /* updates old_state from new_state. It has to be done now because
809 display_menu (called below) uses the old_stack to know what to display. */
810 for (i = last_same + 1; i < new_depth; i++)
811 old_stack [i] = new_stack [i];
812 mw->menu.old_depth = new_depth;
813
814 /* refresh the last seletion */
815 selection_position.x = 0;
816 selection_position.y = 0;
817 display_menu (mw, last_same, new_selection == old_selection,
818 &selection_position, NULL, NULL, old_selection, new_selection);
819
820 /* Now popup the new menus */
821 for (i = last_same + 1; i < new_depth && new_stack [i]->contents; i++)
822 {
823 window_state* previous_ws = &windows [i - 1];
824 window_state* ws = &windows [i];
825
826 ws->x =
827 previous_ws->x + selection_position.x + mw->menu.shadow_thickness;
828 if (!mw->menu.horizontal || i > 1)
829 ws->x += mw->menu.shadow_thickness;
830 ws->y =
831 previous_ws->y + selection_position.y + mw->menu.shadow_thickness;
832
833 size_menu (mw, i);
834
835 fit_to_screen (mw, ws, previous_ws, mw->menu.horizontal && i == 1);
836
837 XClearWindow (XtDisplay (mw), ws->window);
838 XMoveResizeWindow (XtDisplay (mw), ws->window, ws->x, ws->y,
839 ws->width, ws->height);
840 XMapRaised (XtDisplay (mw), ws->window);
841 display_menu (mw, i, False, &selection_position, NULL, NULL, NULL, NULL);
842 }
843
844 /* unmap the menus that popped down */
845 for (i = new_depth - 1; i < old_depth; i++)
846 if (i >= new_depth || !new_stack [i]->contents)
847 XUnmapWindow (XtDisplay (mw), windows [i].window);
848 }
849
850 static Boolean
851 motion_event_is_in_menu (mw, ev, level, relative_pos)
852 XlwMenuWidget mw;
853 XMotionEvent* ev;
854 int level;
855 XPoint* relative_pos;
856 {
857 window_state* ws = &mw->menu.windows [level];
858 unsigned int x = level == 0 ? ws->x : ws->x + mw->menu.shadow_thickness;
859 unsigned int y = level == 0 ? ws->y : ws->y + mw->menu.shadow_thickness;
860 relative_pos->x = ev->x_root - x;
861 relative_pos->y = ev->y_root - y;
862 return (x < ev->x_root && ev->x_root < x + ws->width
863 && y < ev->y_root && ev->y_root < y + ws->height);
864 }
865
866 static Boolean
867 map_event_to_widget_value (mw, ev, val, level)
868 XlwMenuWidget mw;
869 XMotionEvent* ev;
870 widget_value** val;
871 int* level;
872 {
873 int i;
874 XPoint relative_pos;
875 window_state* ws;
876
877 *val = NULL;
878
879 /* Find the window */
880 for (i = mw->menu.old_depth - 1; i >= 0; i--)
881 {
882 ws = &mw->menu.windows [i];
883 if (ws && motion_event_is_in_menu (mw, ev, i, &relative_pos))
884 {
885 display_menu (mw, i, True, NULL, &relative_pos, val, NULL, NULL);
886
887 if (*val)
888 {
889 *level = i + 1;
890 return True;
891 }
892 }
893 }
894 return False;
895 }
896
897 \f/* Procedures */
898 static void
899 make_drawing_gcs (mw)
900 XlwMenuWidget mw;
901 {
902 XGCValues xgcv;
903
904 xgcv.font = mw->menu.font->fid;
905 xgcv.foreground = mw->menu.foreground;
906 xgcv.background = mw->core.background_pixel;
907 mw->menu.foreground_gc = XtGetGC ((Widget)mw,
908 GCFont | GCForeground | GCBackground,
909 &xgcv);
910
911 xgcv.font = mw->menu.font->fid;
912 xgcv.foreground = mw->menu.button_foreground;
913 xgcv.background = mw->core.background_pixel;
914 mw->menu.button_gc = XtGetGC ((Widget)mw,
915 GCFont | GCForeground | GCBackground,
916 &xgcv);
917
918 xgcv.font = mw->menu.font->fid;
919 xgcv.foreground = mw->menu.foreground;
920 xgcv.background = mw->core.background_pixel;
921 xgcv.fill_style = FillStippled;
922 xgcv.stipple = mw->menu.gray_pixmap;
923 mw->menu.inactive_gc = XtGetGC ((Widget)mw,
924 (GCFont | GCForeground | GCBackground
925 | GCFillStyle | GCStipple), &xgcv);
926
927 xgcv.font = mw->menu.font->fid;
928 xgcv.foreground = mw->menu.button_foreground;
929 xgcv.background = mw->core.background_pixel;
930 xgcv.fill_style = FillStippled;
931 xgcv.stipple = mw->menu.gray_pixmap;
932 mw->menu.inactive_button_gc = XtGetGC ((Widget)mw,
933 (GCFont | GCForeground | GCBackground
934 | GCFillStyle | GCStipple), &xgcv);
935
936 xgcv.font = mw->menu.font->fid;
937 xgcv.foreground = mw->core.background_pixel;
938 xgcv.background = mw->menu.foreground;
939 mw->menu.background_gc = XtGetGC ((Widget)mw,
940 GCFont | GCForeground | GCBackground,
941 &xgcv);
942 }
943
944 static void
945 release_drawing_gcs (mw)
946 XlwMenuWidget mw;
947 {
948 XtReleaseGC ((Widget) mw, mw->menu.foreground_gc);
949 XtReleaseGC ((Widget) mw, mw->menu.button_gc);
950 XtReleaseGC ((Widget) mw, mw->menu.inactive_gc);
951 XtReleaseGC ((Widget) mw, mw->menu.inactive_button_gc);
952 XtReleaseGC ((Widget) mw, mw->menu.background_gc);
953 /* let's get some segvs if we try to use these... */
954 mw->menu.foreground_gc = (GC) -1;
955 mw->menu.button_gc = (GC) -1;
956 mw->menu.inactive_gc = (GC) -1;
957 mw->menu.inactive_button_gc = (GC) -1;
958 mw->menu.background_gc = (GC) -1;
959 }
960
961 #define MINL(x,y) ((((unsigned long) (x)) < ((unsigned long) (y))) \
962 ? ((unsigned long) (x)) : ((unsigned long) (y)))
963
964 static void
965 make_shadow_gcs (mw)
966 XlwMenuWidget mw;
967 {
968 XGCValues xgcv;
969 unsigned long pm = 0;
970 Display *dpy = XtDisplay ((Widget) mw);
971 Colormap cmap = DefaultColormapOfScreen (XtScreen ((Widget) mw));
972 XColor topc, botc;
973 int top_frobbed = 0, bottom_frobbed = 0;
974
975 if (mw->menu.top_shadow_color == -1)
976 mw->menu.top_shadow_color = mw->core.background_pixel;
977 if (mw->menu.bottom_shadow_color == -1)
978 mw->menu.bottom_shadow_color = mw->menu.foreground;
979
980 if (mw->menu.top_shadow_color == mw->core.background_pixel ||
981 mw->menu.top_shadow_color == mw->menu.foreground)
982 {
983 topc.pixel = mw->core.background_pixel;
984 XQueryColor (dpy, cmap, &topc);
985 /* don't overflow/wrap! */
986 topc.red = MINL (65535, topc.red * 1.2);
987 topc.green = MINL (65535, topc.green * 1.2);
988 topc.blue = MINL (65535, topc.blue * 1.2);
989 if (XAllocColor (dpy, cmap, &topc))
990 {
991 mw->menu.top_shadow_color = topc.pixel;
992 top_frobbed = 1;
993 }
994 }
995 if (mw->menu.bottom_shadow_color == mw->menu.foreground ||
996 mw->menu.bottom_shadow_color == mw->core.background_pixel)
997 {
998 botc.pixel = mw->core.background_pixel;
999 XQueryColor (dpy, cmap, &botc);
1000 botc.red *= 0.6;
1001 botc.green *= 0.6;
1002 botc.blue *= 0.6;
1003 if (XAllocColor (dpy, cmap, &botc))
1004 {
1005 mw->menu.bottom_shadow_color = botc.pixel;
1006 bottom_frobbed = 1;
1007 }
1008 }
1009
1010 if (top_frobbed && bottom_frobbed)
1011 {
1012 int top_avg = ((topc.red / 3) + (topc.green / 3) + (topc.blue / 3));
1013 int bot_avg = ((botc.red / 3) + (botc.green / 3) + (botc.blue / 3));
1014 if (bot_avg > top_avg)
1015 {
1016 Pixel tmp = mw->menu.top_shadow_color;
1017 mw->menu.top_shadow_color = mw->menu.bottom_shadow_color;
1018 mw->menu.bottom_shadow_color = tmp;
1019 }
1020 else if (topc.pixel == botc.pixel)
1021 {
1022 if (botc.pixel == mw->menu.foreground)
1023 mw->menu.top_shadow_color = mw->core.background_pixel;
1024 else
1025 mw->menu.bottom_shadow_color = mw->menu.foreground;
1026 }
1027 }
1028
1029 if (!mw->menu.top_shadow_pixmap &&
1030 mw->menu.top_shadow_color == mw->core.background_pixel)
1031 {
1032 mw->menu.top_shadow_pixmap = mw->menu.gray_pixmap;
1033 mw->menu.top_shadow_color = mw->menu.foreground;
1034 }
1035 if (!mw->menu.bottom_shadow_pixmap &&
1036 mw->menu.bottom_shadow_color == mw->core.background_pixel)
1037 {
1038 mw->menu.bottom_shadow_pixmap = mw->menu.gray_pixmap;
1039 mw->menu.bottom_shadow_color = mw->menu.foreground;
1040 }
1041
1042 xgcv.fill_style = FillStippled;
1043 xgcv.foreground = mw->menu.top_shadow_color;
1044 xgcv.stipple = mw->menu.top_shadow_pixmap;
1045 pm = (xgcv.stipple ? GCStipple|GCFillStyle : 0);
1046 mw->menu.shadow_top_gc = XtGetGC ((Widget)mw, GCForeground | pm, &xgcv);
1047
1048 xgcv.foreground = mw->menu.bottom_shadow_color;
1049 xgcv.stipple = mw->menu.bottom_shadow_pixmap;
1050 pm = (xgcv.stipple ? GCStipple|GCFillStyle : 0);
1051 mw->menu.shadow_bottom_gc = XtGetGC ((Widget)mw, GCForeground | pm, &xgcv);
1052 }
1053
1054
1055 static void
1056 release_shadow_gcs (mw)
1057 XlwMenuWidget mw;
1058 {
1059 XtReleaseGC ((Widget) mw, mw->menu.shadow_top_gc);
1060 XtReleaseGC ((Widget) mw, mw->menu.shadow_bottom_gc);
1061 }
1062
1063 static void
1064 XlwMenuInitialize (request, mw, args, num_args)
1065 Widget request;
1066 XlwMenuWidget mw;
1067 ArgList args;
1068 Cardinal *num_args;
1069 {
1070 /* Get the GCs and the widget size */
1071 XSetWindowAttributes xswa;
1072 int mask;
1073
1074 Window window = RootWindowOfScreen (DefaultScreenOfDisplay (XtDisplay (mw)));
1075 Display* display = XtDisplay (mw);
1076
1077 #if 0
1078 widget_value *tem = (widget_value *) XtMalloc (sizeof (widget_value));
1079
1080 /* _XtCreate is freeing the object that was passed to us,
1081 so make a copy that we will actually keep. */
1082 lwlib_bcopy (mw->menu.contents, tem, sizeof (widget_value));
1083 mw->menu.contents = tem;
1084 #endif
1085
1086 /* mw->menu.cursor = XCreateFontCursor (display, mw->menu.cursor_shape); */
1087 mw->menu.cursor = mw->menu.cursor_shape;
1088
1089 mw->menu.gray_pixmap
1090 = XCreatePixmapFromBitmapData (display, window, gray_bits,
1091 gray_width, gray_height,
1092 (unsigned long)1, (unsigned long)0, 1);
1093
1094 /* I don't understand why this ends up 0 sometimes,
1095 but it does. This kludge works around it.
1096 Can anyone find a real fix? -- rms. */
1097 if (mw->menu.font == 0)
1098 mw->menu.font = xlwmenu_default_font;
1099
1100 make_drawing_gcs (mw);
1101 make_shadow_gcs (mw);
1102
1103 xswa.background_pixel = mw->core.background_pixel;
1104 xswa.border_pixel = mw->core.border_pixel;
1105 mask = CWBackPixel | CWBorderPixel;
1106
1107 mw->menu.popped_up = False;
1108
1109 mw->menu.old_depth = 1;
1110 mw->menu.old_stack = (widget_value**)XtMalloc (sizeof (widget_value*));
1111 mw->menu.old_stack_length = 1;
1112 mw->menu.old_stack [0] = mw->menu.contents;
1113
1114 mw->menu.new_depth = 0;
1115 mw->menu.new_stack = 0;
1116 mw->menu.new_stack_length = 0;
1117 push_new_stack (mw, mw->menu.contents);
1118
1119 mw->menu.windows = (window_state*)XtMalloc (sizeof (window_state));
1120 mw->menu.windows_length = 1;
1121 mw->menu.windows [0].x = 0;
1122 mw->menu.windows [0].y = 0;
1123 mw->menu.windows [0].width = 0;
1124 mw->menu.windows [0].height = 0;
1125 size_menu (mw, 0);
1126
1127 mw->core.width = mw->menu.windows [0].width;
1128 mw->core.height = mw->menu.windows [0].height;
1129 }
1130
1131 static void
1132 XlwMenuClassInitialize ()
1133 {
1134 }
1135
1136 static void
1137 XlwMenuRealize (w, valueMask, attributes)
1138 Widget w;
1139 Mask *valueMask;
1140 XSetWindowAttributes *attributes;
1141 {
1142 XlwMenuWidget mw = (XlwMenuWidget)w;
1143 XSetWindowAttributes xswa;
1144 int mask;
1145
1146 (*xlwMenuWidgetClass->core_class.superclass->core_class.realize)
1147 (w, valueMask, attributes);
1148
1149 xswa.save_under = True;
1150 xswa.cursor = mw->menu.cursor_shape;
1151 mask = CWSaveUnder | CWCursor;
1152 XChangeWindowAttributes (XtDisplay (w), XtWindow (w), mask, &xswa);
1153
1154 mw->menu.windows [0].window = XtWindow (w);
1155 mw->menu.windows [0].x = w->core.x;
1156 mw->menu.windows [0].y = w->core.y;
1157 mw->menu.windows [0].width = w->core.width;
1158 mw->menu.windows [0].height = w->core.height;
1159 }
1160
1161 /* Only the toplevel menubar/popup is a widget so it's the only one that
1162 receives expose events through Xt. So we repaint all the other panes
1163 when receiving an Expose event. */
1164 static void
1165 XlwMenuRedisplay (w, ev, region)
1166 Widget w;
1167 XEvent* ev;
1168 Region region;
1169 {
1170 XlwMenuWidget mw = (XlwMenuWidget)w;
1171 int i;
1172
1173 /* If we have a depth beyond 1, it's because a submenu was displayed.
1174 If the submenu has been destroyed, set the depth back to 1. */
1175 if (submenu_destroyed)
1176 {
1177 mw->menu.old_depth = 1;
1178 submenu_destroyed = 0;
1179 }
1180
1181 for (i = 0; i < mw->menu.old_depth; i++)
1182 display_menu (mw, i, False, NULL, NULL, NULL, NULL, NULL);
1183 }
1184
1185 static void
1186 XlwMenuDestroy (w)
1187 Widget w;
1188 {
1189 int i;
1190 XlwMenuWidget mw = (XlwMenuWidget) w;
1191
1192 if (pointer_grabbed)
1193 XtUngrabPointer ((Widget)w, CurrentTime);
1194 pointer_grabbed = 0;
1195
1196 submenu_destroyed = 1;
1197
1198 release_drawing_gcs (mw);
1199 release_shadow_gcs (mw);
1200
1201 /* this doesn't come from the resource db but is created explicitly
1202 so we must free it ourselves. */
1203 XFreePixmap (XtDisplay (mw), mw->menu.gray_pixmap);
1204 mw->menu.gray_pixmap = (Pixmap) -1;
1205
1206 #if 0
1207 /* Do free mw->menu.contents because nowadays we copy it
1208 during initialization. */
1209 XtFree (mw->menu.contents);
1210 #endif
1211
1212 /* Don't free mw->menu.contents because that comes from our creator.
1213 The `*_stack' elements are just pointers into `contents' so leave
1214 that alone too. But free the stacks themselves. */
1215 if (mw->menu.old_stack) XtFree ((char *) mw->menu.old_stack);
1216 if (mw->menu.new_stack) XtFree ((char *) mw->menu.new_stack);
1217
1218 /* Remember, you can't free anything that came from the resource
1219 database. This includes:
1220 mw->menu.cursor
1221 mw->menu.top_shadow_pixmap
1222 mw->menu.bottom_shadow_pixmap
1223 mw->menu.font
1224 Also the color cells of top_shadow_color, bottom_shadow_color,
1225 foreground, and button_foreground will never be freed until this
1226 client exits. Nice, eh?
1227 */
1228
1229 /* start from 1 because the one in slot 0 is w->core.window */
1230 for (i = 1; i < mw->menu.windows_length; i++)
1231 XDestroyWindow (XtDisplay (mw), mw->menu.windows [i].window);
1232 if (mw->menu.windows)
1233 XtFree ((char *) mw->menu.windows);
1234 }
1235
1236 static Boolean
1237 XlwMenuSetValues (current, request, new)
1238 Widget current;
1239 Widget request;
1240 Widget new;
1241 {
1242 XlwMenuWidget oldmw = (XlwMenuWidget)current;
1243 XlwMenuWidget newmw = (XlwMenuWidget)new;
1244 Boolean redisplay = False;
1245 int i;
1246
1247 if (newmw->menu.contents
1248 && newmw->menu.contents->contents
1249 && newmw->menu.contents->contents->change >= VISIBLE_CHANGE)
1250 redisplay = True;
1251
1252 if (newmw->core.background_pixel != oldmw->core.background_pixel
1253 || newmw->menu.foreground != oldmw->menu.foreground
1254 || newmw->menu.font != oldmw->menu.font)
1255 {
1256 release_drawing_gcs (newmw);
1257 make_drawing_gcs (newmw);
1258 redisplay = True;
1259
1260 for (i = 0; i < oldmw->menu.windows_length; i++)
1261 {
1262 XSetWindowBackground (XtDisplay (oldmw),
1263 oldmw->menu.windows [i].window,
1264 newmw->core.background_pixel);
1265 /* clear windows and generate expose events */
1266 XClearArea (XtDisplay (oldmw), oldmw->menu.windows[i].window,
1267 0, 0, 0, 0, True);
1268 }
1269 }
1270
1271 return redisplay;
1272 }
1273
1274 static void
1275 XlwMenuResize (w)
1276 Widget w;
1277 {
1278 XlwMenuWidget mw = (XlwMenuWidget)w;
1279
1280 if (mw->menu.popped_up)
1281 {
1282 /* Don't allow the popup menu to resize itself. */
1283 mw->core.width = mw->menu.windows [0].width;
1284 mw->core.height = mw->menu.windows [0].height;
1285 mw->core.parent->core.width = mw->core.width ;
1286 mw->core.parent->core.height = mw->core.height ;
1287 }
1288 else
1289 {
1290 mw->menu.windows [0].width = mw->core.width;
1291 mw->menu.windows [0].height = mw->core.height;
1292 }
1293 }
1294
1295 \f/* Action procedures */
1296 static void
1297 handle_single_motion_event (mw, ev)
1298 XlwMenuWidget mw;
1299 XMotionEvent* ev;
1300 {
1301 widget_value* val;
1302 int level;
1303
1304 if (!map_event_to_widget_value (mw, ev, &val, &level))
1305 pop_new_stack_if_no_contents (mw);
1306 else
1307 set_new_state (mw, val, level);
1308 remap_menubar (mw);
1309
1310 /* Sync with the display. Makes it feel better on X terms. */
1311 XSync (XtDisplay (mw), False);
1312 }
1313
1314 static void
1315 handle_motion_event (mw, ev)
1316 XlwMenuWidget mw;
1317 XMotionEvent* ev;
1318 {
1319 int x = ev->x_root;
1320 int y = ev->y_root;
1321 int state = ev->state;
1322
1323 handle_single_motion_event (mw, ev);
1324
1325 /* allow motion events to be generated again */
1326 if (ev->is_hint
1327 && XQueryPointer (XtDisplay (mw), ev->window,
1328 &ev->root, &ev->subwindow,
1329 &ev->x_root, &ev->y_root,
1330 &ev->x, &ev->y,
1331 &ev->state)
1332 && ev->state == state
1333 && (ev->x_root != x || ev->y_root != y))
1334 handle_single_motion_event (mw, ev);
1335 }
1336
1337 static void
1338 Start (w, ev, params, num_params)
1339 Widget w;
1340 XEvent *ev;
1341 String *params;
1342 Cardinal *num_params;
1343 {
1344 XlwMenuWidget mw = (XlwMenuWidget)w;
1345
1346 if (!mw->menu.popped_up)
1347 {
1348 menu_post_event = *ev;
1349 pop_up_menu (mw, ev);
1350 }
1351 else
1352 {
1353 /* If we push a button while the menu is posted semipermanently,
1354 releasing the button should always pop the menu down. */
1355 next_release_must_exit = 1;
1356
1357 /* notes the absolute position of the menubar window */
1358 mw->menu.windows [0].x = ev->xmotion.x_root - ev->xmotion.x;
1359 mw->menu.windows [0].y = ev->xmotion.y_root - ev->xmotion.y;
1360
1361 /* handles the down like a move, slots are compatible */
1362 handle_motion_event (mw, &ev->xmotion);
1363 }
1364 }
1365
1366 static void
1367 Drag (w, ev, params, num_params)
1368 Widget w;
1369 XEvent *ev;
1370 String *params;
1371 Cardinal *num_params;
1372 {
1373 XlwMenuWidget mw = (XlwMenuWidget)w;
1374 if (mw->menu.popped_up)
1375 handle_motion_event (mw, &ev->xmotion);
1376 }
1377
1378 static void
1379 Select (w, ev, params, num_params)
1380 Widget w;
1381 XEvent *ev;
1382 String *params;
1383 Cardinal *num_params;
1384 {
1385 XlwMenuWidget mw = (XlwMenuWidget)w;
1386 widget_value* selected_item = mw->menu.old_stack [mw->menu.old_depth - 1];
1387
1388 /* If user releases the button quickly, without selecting anything,
1389 after the initial down-click that brought the menu up,
1390 do nothing. */
1391 if ((selected_item == 0
1392 || ((widget_value *) selected_item)->call_data == 0)
1393 && !next_release_must_exit
1394 && (ev->xbutton.time - menu_post_event.xbutton.time
1395 < XtGetMultiClickTime (XtDisplay (w))))
1396 return;
1397
1398 /* pop down everything. */
1399 mw->menu.new_depth = 1;
1400 remap_menubar (mw);
1401
1402 if (mw->menu.popped_up)
1403 {
1404 mw->menu.popped_up = False;
1405 XtUngrabPointer ((Widget)mw, ev->xmotion.time);
1406 if (XtIsShell (XtParent ((Widget) mw)))
1407 XtPopdown (XtParent ((Widget) mw));
1408 else
1409 {
1410 XtRemoveGrab ((Widget) mw);
1411 display_menu (mw, 0, False, NULL, NULL, NULL, NULL, NULL);
1412 }
1413 }
1414
1415 /* callback */
1416 XtCallCallbackList ((Widget)mw, mw->menu.select, (XtPointer)selected_item);
1417 }
1418
1419
1420 \f/* Special code to pop-up a menu */
1421 void
1422 pop_up_menu (mw, event)
1423 XlwMenuWidget mw;
1424 XButtonPressedEvent* event;
1425 {
1426 int x = event->x_root;
1427 int y = event->y_root;
1428 int w;
1429 int h;
1430 int borderwidth = mw->menu.shadow_thickness;
1431 Screen* screen = XtScreen (mw);
1432 Display *display = XtDisplay (mw);
1433
1434 next_release_must_exit = 0;
1435
1436 XtCallCallbackList ((Widget)mw, mw->menu.open, NULL);
1437
1438 if (XtIsShell (XtParent ((Widget)mw)))
1439 size_menu (mw, 0);
1440
1441 w = mw->menu.windows [0].width;
1442 h = mw->menu.windows [0].height;
1443
1444 x -= borderwidth;
1445 y -= borderwidth;
1446 if (x < borderwidth)
1447 x = borderwidth;
1448 if (x + w + 2 * borderwidth > WidthOfScreen (screen))
1449 x = WidthOfScreen (screen) - w - 2 * borderwidth;
1450 if (y < borderwidth)
1451 y = borderwidth;
1452 if (y + h + 2 * borderwidth> HeightOfScreen (screen))
1453 y = HeightOfScreen (screen) - h - 2 * borderwidth;
1454
1455 mw->menu.popped_up = True;
1456 if (XtIsShell (XtParent ((Widget)mw)))
1457 {
1458 XtConfigureWidget (XtParent ((Widget)mw), x, y, w, h,
1459 XtParent ((Widget)mw)->core.border_width);
1460 XtPopup (XtParent ((Widget)mw), XtGrabExclusive);
1461 display_menu (mw, 0, False, NULL, NULL, NULL, NULL, NULL);
1462 mw->menu.windows [0].x = x + borderwidth;
1463 mw->menu.windows [0].y = y + borderwidth;
1464 }
1465 else
1466 {
1467 XEvent *ev = (XEvent *) event;
1468
1469 XtAddGrab ((Widget) mw, True, True);
1470
1471 /* notes the absolute position of the menubar window */
1472 mw->menu.windows [0].x = ev->xmotion.x_root - ev->xmotion.x;
1473 mw->menu.windows [0].y = ev->xmotion.y_root - ev->xmotion.y;
1474 }
1475
1476 #ifdef emacs
1477 x_catch_errors (display);
1478 #endif
1479 XtGrabPointer ((Widget)mw, False,
1480 (PointerMotionMask
1481 | PointerMotionHintMask
1482 | ButtonReleaseMask
1483 | ButtonPressMask),
1484 GrabModeAsync, GrabModeAsync, None,
1485 mw->menu.cursor_shape,
1486 event->time);
1487 pointer_grabbed = 1;
1488 #ifdef emacs
1489 if (x_had_errors_p (display))
1490 {
1491 pointer_grabbed = 0;
1492 XtUngrabPointer ((Widget)mw, event->time);
1493 }
1494 x_uncatch_errors (display);
1495 #endif
1496
1497 handle_motion_event (mw, (XMotionEvent*)event);
1498 }