1 /* Implements a lightweight menubar widget.
2 Copyright (C) 1992 Lucid, Inc.
3 Copyright (C) 1994, 1995, 1997, 1999, 2000, 2001, 2002, 2003, 2004,
4 2005, 2006, 2007, 2008, 2009 Free Software Foundation, Inc.
6 This file is part of the Lucid Widget Library.
8 The Lucid Widget Library is free software; you can redistribute it and/or
9 modify it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2, or (at your option)
13 The Lucid Widget Library is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with GNU Emacs; see the file COPYING. If not, write to the
20 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
21 Boston, MA 02110-1301, USA. */
23 /* Created by devin@lucid.com */
33 #include <sys/types.h>
34 #if (defined __sun) && !(defined SUNOS41)
41 #include <X11/IntrinsicP.h>
42 #include <X11/ObjectP.h>
43 #include <X11/StringDefs.h>
44 #include <X11/cursorfont.h>
49 /* Defined in xfns.c. When config.h defines `static' as empty, we get
50 redefinition errors when gray_bitmap is included more than once, so
51 we're referring to the one include in xfns.c here. */
53 extern int gray_bitmap_width
;
54 extern int gray_bitmap_height
;
55 extern char *gray_bitmap_bits
;
61 #include <X11/bitmaps/gray>
62 #define gray_bitmap_width gray_width
63 #define gray_bitmap_height gray_height
64 #define gray_bitmap_bits gray_bits
66 #endif /* not emacs */
68 static int pointer_grabbed
;
69 static XEvent menu_post_event
;
71 XFontStruct
*xlwmenu_default_font
;
74 xlwMenuTranslations
[] =
75 "<BtnDown>: start()\n\
78 <Key>Shift_L: nothing()\n\
79 <Key>Shift_R: nothing()\n\
80 <Key>Meta_L: nothing()\n\
81 <Key>Meta_R: nothing()\n\
82 <Key>Control_L: nothing()\n\
83 <Key>Control_R: nothing()\n\
84 <Key>Hyper_L: nothing()\n\
85 <Key>Hyper_R: nothing()\n\
86 <Key>Super_L: nothing()\n\
87 <Key>Super_R: nothing()\n\
88 <Key>Alt_L: nothing()\n\
89 <Key>Alt_R: nothing()\n\
90 <Key>Caps_Lock: nothing()\n\
91 <Key>Shift_Lock: nothing()\n\
92 <KeyUp>Shift_L: nothing()\n\
93 <KeyUp>Shift_R: nothing()\n\
94 <KeyUp>Meta_L: nothing()\n\
95 <KeyUp>Meta_R: nothing()\n\
96 <KeyUp>Control_L: nothing()\n\
97 <KeyUp>Control_R: nothing()\n\
98 <KeyUp>Hyper_L: nothing()\n\
99 <KeyUp>Hyper_R: nothing()\n\
100 <KeyUp>Super_L: nothing()\n\
101 <KeyUp>Super_R: nothing()\n\
102 <KeyUp>Alt_L: nothing()\n\
103 <KeyUp>Alt_R: nothing()\n\
104 <KeyUp>Caps_Lock: nothing()\n\
105 <KeyUp>Shift_Lock:nothing()\n\
106 <Key>Return: select()\n\
110 <Key>Right: right()\n\
115 /* FIXME: Space should toggle toggleable menu item but not remove the menu
116 so you can toggle the next one without entering the menu again. */
118 /* FIXME: Should ESC close one level of menu structure or the complete menu? */
120 /* FIXME: F10 should enter the menu, the first one in the menu-bar. */
122 #define offset(field) XtOffset(XlwMenuWidget, field)
127 {XtNfontSet
, XtCFontSet
, XtRFontSet
, sizeof(XFontSet
),
128 offset(menu
.fontSet
), XtRFontSet
, NULL
},
130 {XtNfont
, XtCFont
, XtRFontStruct
, sizeof(XFontStruct
*),
131 offset(menu
.font
), XtRString
, "XtDefaultFont"},
132 {XtNforeground
, XtCForeground
, XtRPixel
, sizeof(Pixel
),
133 offset(menu
.foreground
), XtRString
, "XtDefaultForeground"},
134 {XtNdisabledForeground
, XtCDisabledForeground
, XtRPixel
, sizeof(Pixel
),
135 offset(menu
.disabled_foreground
), XtRString
, (XtPointer
)NULL
},
136 {XtNbuttonForeground
, XtCButtonForeground
, XtRPixel
, sizeof(Pixel
),
137 offset(menu
.button_foreground
), XtRString
, "XtDefaultForeground"},
138 {XtNmargin
, XtCMargin
, XtRDimension
, sizeof(Dimension
),
139 offset(menu
.margin
), XtRImmediate
, (XtPointer
)1},
140 {XtNhorizontalSpacing
, XtCMargin
, XtRDimension
, sizeof(Dimension
),
141 offset(menu
.horizontal_spacing
), XtRImmediate
, (XtPointer
)3},
142 {XtNverticalSpacing
, XtCMargin
, XtRDimension
, sizeof(Dimension
),
143 offset(menu
.vertical_spacing
), XtRImmediate
, (XtPointer
)2},
144 {XtNarrowSpacing
, XtCMargin
, XtRDimension
, sizeof(Dimension
),
145 offset(menu
.arrow_spacing
), XtRImmediate
, (XtPointer
)10},
147 {XmNshadowThickness
, XmCShadowThickness
, XtRDimension
,
148 sizeof (Dimension
), offset (menu
.shadow_thickness
),
149 XtRImmediate
, (XtPointer
)1},
150 {XmNtopShadowColor
, XmCTopShadowColor
, XtRPixel
, sizeof (Pixel
),
151 offset (menu
.top_shadow_color
), XtRImmediate
, (XtPointer
)-1},
152 {XmNbottomShadowColor
, XmCBottomShadowColor
, XtRPixel
, sizeof (Pixel
),
153 offset (menu
.bottom_shadow_color
), XtRImmediate
, (XtPointer
)-1},
154 {XmNtopShadowPixmap
, XmCTopShadowPixmap
, XtRPixmap
, sizeof (Pixmap
),
155 offset (menu
.top_shadow_pixmap
), XtRImmediate
, (XtPointer
)None
},
156 {XmNbottomShadowPixmap
, XmCBottomShadowPixmap
, XtRPixmap
, sizeof (Pixmap
),
157 offset (menu
.bottom_shadow_pixmap
), XtRImmediate
, (XtPointer
)None
},
159 {XtNopen
, XtCCallback
, XtRCallback
, sizeof(XtPointer
),
160 offset(menu
.open
), XtRCallback
, (XtPointer
)NULL
},
161 {XtNselect
, XtCCallback
, XtRCallback
, sizeof(XtPointer
),
162 offset(menu
.select
), XtRCallback
, (XtPointer
)NULL
},
163 {XtNhighlightCallback
, XtCCallback
, XtRCallback
, sizeof(XtPointer
),
164 offset(menu
.highlight
), XtRCallback
, (XtPointer
)NULL
},
165 {XtNenterCallback
, XtCCallback
, XtRCallback
, sizeof(XtPointer
),
166 offset(menu
.enter
), XtRCallback
, (XtPointer
)NULL
},
167 {XtNleaveCallback
, XtCCallback
, XtRCallback
, sizeof(XtPointer
),
168 offset(menu
.leave
), XtRCallback
, (XtPointer
)NULL
},
169 {XtNmenu
, XtCMenu
, XtRPointer
, sizeof(XtPointer
),
170 offset(menu
.contents
), XtRImmediate
, (XtPointer
)NULL
},
171 {XtNcursor
, XtCCursor
, XtRCursor
, sizeof(Cursor
),
172 offset(menu
.cursor_shape
), XtRString
, (XtPointer
)"right_ptr"},
173 {XtNhorizontal
, XtCHorizontal
, XtRInt
, sizeof(int),
174 offset(menu
.horizontal
), XtRImmediate
, (XtPointer
)True
},
178 static Boolean
XlwMenuSetValues();
179 static void XlwMenuRealize();
180 static void XlwMenuRedisplay();
181 static void XlwMenuResize();
182 static void XlwMenuInitialize();
183 static void XlwMenuRedisplay();
184 static void XlwMenuDestroy();
185 static void XlwMenuClassInitialize();
192 static void Select();
194 static void Nothing();
195 static int separator_height
__P ((enum menu_separator
));
196 static void pop_up_menu
__P ((XlwMenuWidget
, XButtonPressedEvent
*));
200 xlwMenuActionsList
[] =
210 {"MenuGadgetEscape", Key
}, /* Compatibility with Lesstif/Motif. */
211 {"nothing", Nothing
},
214 #define SuperClass ((CoreWidgetClass)&coreClassRec)
216 XlwMenuClassRec xlwMenuClassRec
=
218 { /* CoreClass fields initialization */
219 (WidgetClass
) SuperClass
, /* superclass */
220 "XlwMenu", /* class_name */
221 sizeof(XlwMenuRec
), /* size */
222 XlwMenuClassInitialize
, /* class_initialize */
223 NULL
, /* class_part_initialize */
224 FALSE
, /* class_inited */
225 XlwMenuInitialize
, /* initialize */
226 NULL
, /* initialize_hook */
227 XlwMenuRealize
, /* realize */
228 xlwMenuActionsList
, /* actions */
229 XtNumber(xlwMenuActionsList
), /* num_actions */
230 xlwMenuResources
, /* resources */
231 XtNumber(xlwMenuResources
), /* resource_count */
232 NULLQUARK
, /* xrm_class */
233 TRUE
, /* compress_motion */
234 XtExposeCompressMaximal
, /* compress_exposure */
235 TRUE
, /* compress_enterleave */
236 FALSE
, /* visible_interest */
237 XlwMenuDestroy
, /* destroy */
238 XlwMenuResize
, /* resize */
239 XlwMenuRedisplay
, /* expose */
240 XlwMenuSetValues
, /* set_values */
241 NULL
, /* set_values_hook */
242 XtInheritSetValuesAlmost
, /* set_values_almost */
243 NULL
, /* get_values_hook */
244 NULL
, /* accept_focus */
245 XtVersion
, /* version */
246 NULL
, /* callback_private */
247 xlwMenuTranslations
, /* tm_table */
248 XtInheritQueryGeometry
, /* query_geometry */
249 XtInheritDisplayAccelerator
, /* display_accelerator */
251 }, /* XlwMenuClass fields initialization */
257 WidgetClass xlwMenuWidgetClass
= (WidgetClass
) &xlwMenuClassRec
;
259 int submenu_destroyed
;
261 /* For debug, if installation-directory is non-nil this is not an installed
262 Emacs. In that case we do not grab the keyboard to make it easier to
264 #define GRAB_KEYBOARD (EQ (Vinstallation_directory, Qnil))
266 static int next_release_must_exit
;
270 /* Ungrab pointer and keyboard */
272 ungrab_all (w
, ungrabtime
)
276 XtUngrabPointer (w
, ungrabtime
);
277 if (GRAB_KEYBOARD
) XtUngrabKeyboard (w
, ungrabtime
);
280 /* Like abort, but remove grabs from widget W before. */
286 if (XtIsShell (XtParent (w
)))
288 ungrab_all (w
, CurrentTime
);
293 push_new_stack (mw
, val
)
297 if (!mw
->menu
.new_stack
)
299 mw
->menu
.new_stack_length
= 10;
301 (widget_value
**)XtCalloc (mw
->menu
.new_stack_length
,
302 sizeof (widget_value
*));
304 else if (mw
->menu
.new_depth
== mw
->menu
.new_stack_length
)
306 mw
->menu
.new_stack_length
*= 2;
308 (widget_value
**)XtRealloc ((char*)mw
->menu
.new_stack
,
309 mw
->menu
.new_stack_length
* sizeof (widget_value
*));
311 mw
->menu
.new_stack
[mw
->menu
.new_depth
++] = val
;
315 pop_new_stack_if_no_contents (mw
)
318 if (mw
->menu
.new_depth
> 1)
320 if (!mw
->menu
.new_stack
[mw
->menu
.new_depth
- 1]->contents
)
321 mw
->menu
.new_depth
-= 1;
326 make_old_stack_space (mw
, n
)
330 if (!mw
->menu
.old_stack
)
332 mw
->menu
.old_stack_length
= 10;
334 (widget_value
**)XtCalloc (mw
->menu
.old_stack_length
,
335 sizeof (widget_value
*));
337 else if (mw
->menu
.old_stack_length
< n
)
339 mw
->menu
.old_stack_length
*= 2;
341 (widget_value
**)XtRealloc ((char*)mw
->menu
.old_stack
,
342 mw
->menu
.old_stack_length
* sizeof (widget_value
*));
355 XRectangle ink
, logical
;
356 if (mw
->menu
.fontSet
)
358 XmbTextExtents (mw
->menu
.fontSet
, s
, strlen (s
), &ink
, &logical
);
359 return logical
.width
;
363 XTextExtents (mw
->menu
.font
, s
, strlen (s
), &drop
, &drop
, &drop
, &xcs
);
369 #define MENU_FONT_HEIGHT(mw) \
370 ((mw)->menu.fontSet != NULL \
371 ? (mw)->menu.font_extents->max_logical_extent.height \
372 : (mw)->menu.font->ascent + (mw)->menu.font->descent)
373 #define MENU_FONT_ASCENT(mw) \
374 ((mw)->menu.fontSet != NULL \
375 ? - (mw)->menu.font_extents->max_logical_extent.y \
376 : (mw)->menu.font->ascent)
378 #define MENU_FONT_HEIGHT(mw) \
379 ((mw)->menu.font->ascent + (mw)->menu.font->descent)
380 #define MENU_FONT_ASCENT(mw) ((mw)->menu.font->ascent)
387 return (MENU_FONT_ASCENT (mw
) * 3/4) | 1;
390 /* Return the width of toggle buttons of widget MW. */
393 toggle_button_width (mw
)
396 return (MENU_FONT_HEIGHT (mw
) * 2 / 3) | 1;
400 /* Return the width of radio buttons of widget MW. */
403 radio_button_width (mw
)
406 return toggle_button_width (mw
) * 1.41;
413 {"labelString", "LabelString", XtRString
, sizeof(String
),
418 resource_widget_value (mw
, val
)
422 if (!val
->toolkit_data
)
424 char* resourced_name
= NULL
;
426 XtGetSubresources ((Widget
) mw
,
427 (XtPointer
) &resourced_name
,
428 val
->name
, val
->name
,
429 nameResource
, 1, NULL
, 0);
431 resourced_name
= val
->name
;
434 complete_name
= (char *) XtMalloc (strlen (resourced_name
) + 1);
435 strcpy (complete_name
, resourced_name
);
439 int complete_length
=
440 strlen (resourced_name
) + strlen (val
->value
) + 2;
441 complete_name
= XtMalloc (complete_length
);
443 strcat (complete_name
, resourced_name
);
444 strcat (complete_name
, " ");
445 strcat (complete_name
, val
->value
);
448 val
->toolkit_data
= complete_name
;
449 val
->free_toolkit_data
= True
;
451 return (char*)val
->toolkit_data
;
454 /* Returns the sizes of an item */
456 size_menu_item (mw
, val
, horizontal_p
, label_width
, rest_width
, button_width
,
466 enum menu_separator separator
;
468 if (lw_separator_p (val
->name
, &separator
, 0))
470 *height
= separator_height (separator
);
477 *height
= MENU_FONT_HEIGHT (mw
)
478 + 2 * mw
->menu
.vertical_spacing
+ 2 * mw
->menu
.shadow_thickness
;
481 string_width (mw
, resource_widget_value (mw
, val
))
482 + mw
->menu
.horizontal_spacing
+ mw
->menu
.shadow_thickness
;
484 *rest_width
= mw
->menu
.horizontal_spacing
+ mw
->menu
.shadow_thickness
;
488 /* Add width of the arrow displayed for submenus. */
489 *rest_width
+= arrow_width (mw
) + mw
->menu
.arrow_spacing
;
491 /* Add width of key equivalent string. */
492 *rest_width
+= (string_width (mw
, val
->key
)
493 + mw
->menu
.arrow_spacing
);
495 if (val
->button_type
== BUTTON_TYPE_TOGGLE
)
496 *button_width
= (toggle_button_width (mw
)
497 + mw
->menu
.horizontal_spacing
);
498 else if (val
->button_type
== BUTTON_TYPE_RADIO
)
499 *button_width
= (radio_button_width (mw
)
500 + mw
->menu
.horizontal_spacing
);
506 size_menu (mw
, level
)
510 unsigned int label_width
= 0;
512 int button_width
= 0;
513 int max_rest_width
= 0;
514 int max_button_width
= 0;
515 unsigned int height
= 0;
516 int horizontal_p
= mw
->menu
.horizontal
&& (level
== 0);
520 if (level
>= mw
->menu
.old_depth
)
521 abort_gracefully ((Widget
) mw
);
523 ws
= &mw
->menu
.windows
[level
];
527 ws
->button_width
= 0;
529 for (val
= mw
->menu
.old_stack
[level
]->contents
; val
; val
= val
->next
)
531 size_menu_item (mw
, val
, horizontal_p
, &label_width
, &rest_width
,
532 &button_width
, &height
);
535 ws
->width
+= label_width
+ rest_width
;
536 if (height
> ws
->height
)
541 if (label_width
> ws
->label_width
)
542 ws
->label_width
= label_width
;
543 if (rest_width
> max_rest_width
)
544 max_rest_width
= rest_width
;
545 if (button_width
> max_button_width
)
546 max_button_width
= button_width
;
547 ws
->height
+= height
;
552 ws
->label_width
= ws
->button_width
= 0;
555 ws
->width
= ws
->label_width
+ max_rest_width
+ max_button_width
;
556 ws
->button_width
= max_button_width
;
559 ws
->width
+= 2 * mw
->menu
.shadow_thickness
;
560 ws
->height
+= 2 * mw
->menu
.shadow_thickness
;
564 ws
->width
+= 2 * mw
->menu
.margin
;
565 ws
->height
+= 2 * mw
->menu
.margin
;
573 draw_arrow (mw
, window
, gc
, x
, y
, width
, down_p
)
582 Display
*dpy
= XtDisplay (mw
);
583 GC top_gc
= mw
->menu
.shadow_top_gc
;
584 GC bottom_gc
= mw
->menu
.shadow_bottom_gc
;
585 int thickness
= mw
->menu
.shadow_thickness
;
588 /* alpha = atan (0.5)
589 factor = (1 + sin (alpha)) / cos (alpha) */
590 double factor
= 1.62;
591 int thickness2
= thickness
* factor
;
593 y
+= (MENU_FONT_HEIGHT (mw
) - height
) / 2;
604 pt
[0].y
= y
+ height
;
605 pt
[1].x
= x
+ thickness
;
606 pt
[1].y
= y
+ height
- thickness2
;
607 pt
[2].x
= x
+ thickness2
;
608 pt
[2].y
= y
+ thickness2
;
611 XFillPolygon (dpy
, window
, top_gc
, pt
, 4, Convex
, CoordModeOrigin
);
615 pt
[1].x
= x
+ thickness
;
616 pt
[1].y
= y
+ thickness2
;
617 pt
[2].x
= x
+ width
- thickness2
;
618 pt
[2].y
= y
+ height
/ 2;
620 pt
[3].y
= y
+ height
/ 2;
621 XFillPolygon (dpy
, window
, top_gc
, pt
, 4, Convex
, CoordModeOrigin
);
624 pt
[0].y
= y
+ height
;
625 pt
[1].x
= x
+ thickness
;
626 pt
[1].y
= y
+ height
- thickness2
;
627 pt
[2].x
= x
+ width
- thickness2
;
628 pt
[2].y
= y
+ height
/ 2;
630 pt
[3].y
= y
+ height
/ 2;
631 XFillPolygon (dpy
, window
, bottom_gc
, pt
, 4, Convex
, CoordModeOrigin
);
637 draw_shadow_rectangle (mw
, window
, x
, y
, width
, height
, erase_p
, down_p
)
647 Display
*dpy
= XtDisplay (mw
);
648 GC top_gc
= !erase_p
? mw
->menu
.shadow_top_gc
: mw
->menu
.background_gc
;
649 GC bottom_gc
= !erase_p
? mw
->menu
.shadow_bottom_gc
: mw
->menu
.background_gc
;
650 int thickness
= mw
->menu
.shadow_thickness
;
653 if (!erase_p
&& down_p
)
663 points
[1].x
= x
+ width
;
665 points
[2].x
= x
+ width
- thickness
;
666 points
[2].y
= y
+ thickness
;
668 points
[3].y
= y
+ thickness
;
669 XFillPolygon (dpy
, window
, top_gc
, points
, 4, Convex
, CoordModeOrigin
);
671 points
[0].y
= y
+ thickness
;
673 points
[1].y
= y
+ height
;
674 points
[2].x
= x
+ thickness
;
675 points
[2].y
= y
+ height
- thickness
;
676 points
[3].x
= x
+ thickness
;
677 points
[3].y
= y
+ thickness
;
678 XFillPolygon (dpy
, window
, top_gc
, points
, 4, Convex
, CoordModeOrigin
);
679 points
[0].x
= x
+ width
;
681 points
[1].x
= x
+ width
- thickness
;
682 points
[1].y
= y
+ thickness
;
683 points
[2].x
= x
+ width
- thickness
;
684 points
[2].y
= y
+ height
- thickness
;
685 points
[3].x
= x
+ width
;
686 points
[3].y
= y
+ height
- thickness
;
687 XFillPolygon (dpy
, window
, bottom_gc
, points
, 4, Convex
, CoordModeOrigin
);
689 points
[0].y
= y
+ height
;
690 points
[1].x
= x
+ width
;
691 points
[1].y
= y
+ height
;
692 points
[2].x
= x
+ width
;
693 points
[2].y
= y
+ height
- thickness
;
694 points
[3].x
= x
+ thickness
;
695 points
[3].y
= y
+ height
- thickness
;
696 XFillPolygon (dpy
, window
, bottom_gc
, points
, 4, Convex
, CoordModeOrigin
);
701 draw_shadow_rhombus (mw
, window
, x
, y
, width
, height
, erase_p
, down_p
)
711 Display
*dpy
= XtDisplay (mw
);
712 GC top_gc
= !erase_p
? mw
->menu
.shadow_top_gc
: mw
->menu
.background_gc
;
713 GC bottom_gc
= !erase_p
? mw
->menu
.shadow_bottom_gc
: mw
->menu
.background_gc
;
714 int thickness
= mw
->menu
.shadow_thickness
;
717 if (!erase_p
&& down_p
)
726 points
[0].y
= y
+ height
/ 2;
727 points
[1].x
= x
+ thickness
;
728 points
[1].y
= y
+ height
/ 2;
729 points
[2].x
= x
+ width
/ 2;
730 points
[2].y
= y
+ thickness
;
731 points
[3].x
= x
+ width
/ 2;
733 XFillPolygon (dpy
, window
, top_gc
, points
, 4, Convex
, CoordModeOrigin
);
734 points
[0].x
= x
+ width
/ 2;
736 points
[1].x
= x
+ width
/ 2;
737 points
[1].y
= y
+ thickness
;
738 points
[2].x
= x
+ width
- thickness
;
739 points
[2].y
= y
+ height
/ 2;
740 points
[3].x
= x
+ width
;
741 points
[3].y
= y
+ height
/ 2;
742 XFillPolygon (dpy
, window
, top_gc
, points
, 4, Convex
, CoordModeOrigin
);
744 points
[0].y
= y
+ height
/ 2;
745 points
[1].x
= x
+ thickness
;
746 points
[1].y
= y
+ height
/ 2;
747 points
[2].x
= x
+ width
/ 2;
748 points
[2].y
= y
+ height
- thickness
;
749 points
[3].x
= x
+ width
/ 2;
750 points
[3].y
= y
+ height
;
751 XFillPolygon (dpy
, window
, bottom_gc
, points
, 4, Convex
, CoordModeOrigin
);
752 points
[0].x
= x
+ width
/ 2;
753 points
[0].y
= y
+ height
;
754 points
[1].x
= x
+ width
/ 2;
755 points
[1].y
= y
+ height
- thickness
;
756 points
[2].x
= x
+ width
- thickness
;
757 points
[2].y
= y
+ height
/ 2;
758 points
[3].x
= x
+ width
;
759 points
[3].y
= y
+ height
/ 2;
760 XFillPolygon (dpy
, window
, bottom_gc
, points
, 4, Convex
, CoordModeOrigin
);
764 /* Draw a toggle button on widget MW, X window WINDOW. X/Y is the
765 top-left corner of the menu item. SELECTED_P non-zero means the
766 toggle button is selected. */
769 draw_toggle (mw
, window
, x
, y
, selected_p
)
772 int x
, y
, selected_p
;
776 width
= toggle_button_width (mw
);
778 x
+= mw
->menu
.horizontal_spacing
;
779 y
+= (MENU_FONT_ASCENT (mw
) - height
) / 2;
780 draw_shadow_rectangle (mw
, window
, x
, y
, width
, height
, False
, selected_p
);
784 /* Draw a radio button on widget MW, X window WINDOW. X/Y is the
785 top-left corner of the menu item. SELECTED_P non-zero means the
786 toggle button is selected. */
789 draw_radio (mw
, window
, x
, y
, selected_p
)
792 int x
, y
, selected_p
;
796 width
= radio_button_width (mw
);
798 x
+= mw
->menu
.horizontal_spacing
;
799 y
+= (MENU_FONT_ASCENT (mw
) - height
) / 2;
800 draw_shadow_rhombus (mw
, window
, x
, y
, width
, height
, False
, selected_p
);
804 /* Draw a menu separator on widget MW, X window WINDOW. X/Y is the
805 top-left corner of the menu item. WIDTH is the width of the
806 separator to draw. TYPE is the separator type. */
809 draw_separator (mw
, window
, x
, y
, width
, type
)
813 enum menu_separator type
;
815 Display
*dpy
= XtDisplay (mw
);
820 case SEPARATOR_NO_LINE
:
823 case SEPARATOR_SINGLE_LINE
:
824 XDrawLine (dpy
, window
, mw
->menu
.foreground_gc
,
828 case SEPARATOR_DOUBLE_LINE
:
829 draw_separator (mw
, window
, x
, y
, width
, SEPARATOR_SINGLE_LINE
);
830 draw_separator (mw
, window
, x
, y
+ 2, width
, SEPARATOR_SINGLE_LINE
);
833 case SEPARATOR_SINGLE_DASHED_LINE
:
834 xgcv
.line_style
= LineOnOffDash
;
835 XChangeGC (dpy
, mw
->menu
.foreground_gc
, GCLineStyle
, &xgcv
);
836 XDrawLine (dpy
, window
, mw
->menu
.foreground_gc
,
838 xgcv
.line_style
= LineSolid
;
839 XChangeGC (dpy
, mw
->menu
.foreground_gc
, GCLineStyle
, &xgcv
);
842 case SEPARATOR_DOUBLE_DASHED_LINE
:
843 draw_separator (mw
, window
, x
, y
, width
,
844 SEPARATOR_SINGLE_DASHED_LINE
);
845 draw_separator (mw
, window
, x
, y
+ 2, width
,
846 SEPARATOR_SINGLE_DASHED_LINE
);
849 case SEPARATOR_SHADOW_ETCHED_IN
:
850 XDrawLine (dpy
, window
, mw
->menu
.shadow_bottom_gc
,
852 XDrawLine (dpy
, window
, mw
->menu
.shadow_top_gc
,
853 x
, y
+ 1, x
+ width
, y
+ 1);
856 case SEPARATOR_SHADOW_ETCHED_OUT
:
857 XDrawLine (dpy
, window
, mw
->menu
.shadow_top_gc
,
859 XDrawLine (dpy
, window
, mw
->menu
.shadow_bottom_gc
,
860 x
, y
+ 1, x
+ width
, y
+ 1);
863 case SEPARATOR_SHADOW_ETCHED_IN_DASH
:
864 xgcv
.line_style
= LineOnOffDash
;
865 XChangeGC (dpy
, mw
->menu
.shadow_bottom_gc
, GCLineStyle
, &xgcv
);
866 XChangeGC (dpy
, mw
->menu
.shadow_top_gc
, GCLineStyle
, &xgcv
);
867 draw_separator (mw
, window
, x
, y
, width
, SEPARATOR_SHADOW_ETCHED_IN
);
868 xgcv
.line_style
= LineSolid
;
869 XChangeGC (dpy
, mw
->menu
.shadow_bottom_gc
, GCLineStyle
, &xgcv
);
870 XChangeGC (dpy
, mw
->menu
.shadow_top_gc
, GCLineStyle
, &xgcv
);
873 case SEPARATOR_SHADOW_ETCHED_OUT_DASH
:
874 xgcv
.line_style
= LineOnOffDash
;
875 XChangeGC (dpy
, mw
->menu
.shadow_bottom_gc
, GCLineStyle
, &xgcv
);
876 XChangeGC (dpy
, mw
->menu
.shadow_top_gc
, GCLineStyle
, &xgcv
);
877 draw_separator (mw
, window
, x
, y
, width
, SEPARATOR_SHADOW_ETCHED_OUT
);
878 xgcv
.line_style
= LineSolid
;
879 XChangeGC (dpy
, mw
->menu
.shadow_bottom_gc
, GCLineStyle
, &xgcv
);
880 XChangeGC (dpy
, mw
->menu
.shadow_top_gc
, GCLineStyle
, &xgcv
);
883 case SEPARATOR_SHADOW_DOUBLE_ETCHED_IN
:
884 draw_separator (mw
, window
, x
, y
, width
, SEPARATOR_SHADOW_ETCHED_IN
);
885 draw_separator (mw
, window
, x
, y
+ 3, width
, SEPARATOR_SHADOW_ETCHED_IN
);
888 case SEPARATOR_SHADOW_DOUBLE_ETCHED_OUT
:
889 draw_separator (mw
, window
, x
, y
, width
,
890 SEPARATOR_SHADOW_ETCHED_OUT
);
891 draw_separator (mw
, window
, x
, y
+ 3, width
,
892 SEPARATOR_SHADOW_ETCHED_OUT
);
895 case SEPARATOR_SHADOW_DOUBLE_ETCHED_IN_DASH
:
896 xgcv
.line_style
= LineOnOffDash
;
897 XChangeGC (dpy
, mw
->menu
.shadow_bottom_gc
, GCLineStyle
, &xgcv
);
898 XChangeGC (dpy
, mw
->menu
.shadow_top_gc
, GCLineStyle
, &xgcv
);
899 draw_separator (mw
, window
, x
, y
, width
,
900 SEPARATOR_SHADOW_DOUBLE_ETCHED_IN
);
901 xgcv
.line_style
= LineSolid
;
902 XChangeGC (dpy
, mw
->menu
.shadow_bottom_gc
, GCLineStyle
, &xgcv
);
903 XChangeGC (dpy
, mw
->menu
.shadow_top_gc
, GCLineStyle
, &xgcv
);
906 case SEPARATOR_SHADOW_DOUBLE_ETCHED_OUT_DASH
:
907 xgcv
.line_style
= LineOnOffDash
;
908 XChangeGC (dpy
, mw
->menu
.shadow_bottom_gc
, GCLineStyle
, &xgcv
);
909 XChangeGC (dpy
, mw
->menu
.shadow_top_gc
, GCLineStyle
, &xgcv
);
910 draw_separator (mw
, window
, x
, y
, width
,
911 SEPARATOR_SHADOW_DOUBLE_ETCHED_OUT
);
912 xgcv
.line_style
= LineSolid
;
913 XChangeGC (dpy
, mw
->menu
.shadow_bottom_gc
, GCLineStyle
, &xgcv
);
914 XChangeGC (dpy
, mw
->menu
.shadow_top_gc
, GCLineStyle
, &xgcv
);
923 /* Return the pixel height of menu separator SEPARATOR. */
926 separator_height (separator
)
927 enum menu_separator separator
;
931 case SEPARATOR_NO_LINE
:
934 case SEPARATOR_SINGLE_LINE
:
935 case SEPARATOR_SINGLE_DASHED_LINE
:
938 case SEPARATOR_DOUBLE_LINE
:
939 case SEPARATOR_DOUBLE_DASHED_LINE
:
942 case SEPARATOR_SHADOW_ETCHED_IN
:
943 case SEPARATOR_SHADOW_ETCHED_OUT
:
944 case SEPARATOR_SHADOW_ETCHED_IN_DASH
:
945 case SEPARATOR_SHADOW_ETCHED_OUT_DASH
:
948 case SEPARATOR_SHADOW_DOUBLE_ETCHED_IN
:
949 case SEPARATOR_SHADOW_DOUBLE_ETCHED_OUT
:
950 case SEPARATOR_SHADOW_DOUBLE_ETCHED_IN_DASH
:
951 case SEPARATOR_SHADOW_DOUBLE_ETCHED_OUT_DASH
:
960 /* Display the menu item and increment where.x and where.y to show how large
961 the menu item was. */
964 display_menu_item (mw
, val
, ws
, where
, highlighted_p
, horizontal_p
,
970 Boolean highlighted_p
;
971 Boolean horizontal_p
;
972 Boolean just_compute_p
;
976 int font_height
= MENU_FONT_HEIGHT (mw
);
977 int font_ascent
= MENU_FONT_ASCENT (mw
);
978 int shadow
= mw
->menu
.shadow_thickness
;
979 int margin
= mw
->menu
.margin
;
980 int h_spacing
= mw
->menu
.horizontal_spacing
;
981 int v_spacing
= mw
->menu
.vertical_spacing
;
987 enum menu_separator separator
;
988 int separator_p
= lw_separator_p (val
->name
, &separator
, 0);
990 /* compute the sizes of the item */
991 size_menu_item (mw
, val
, horizontal_p
, &label_width
, &rest_width
,
992 &button_width
, &height
);
995 width
= label_width
+ rest_width
;
998 label_width
= ws
->label_width
;
999 width
= ws
->width
- 2 * shadow
;
1002 /* Only highlight an enabled item that has a callback. */
1004 if (!val
->enabled
|| !(val
->call_data
|| val
->contents
))
1007 /* do the drawing. */
1008 if (!just_compute_p
)
1010 /* Add the shadow border of the containing menu */
1011 int x
= where
->x
+ shadow
;
1012 int y
= where
->y
+ shadow
;
1020 /* pick the foreground and background GC. */
1022 text_gc
= mw
->menu
.foreground_gc
;
1024 text_gc
= mw
->menu
.disabled_gc
;
1025 deco_gc
= mw
->menu
.foreground_gc
;
1029 draw_separator (mw
, ws
->window
, x
, y
, width
, separator
);
1033 int x_offset
= x
+ h_spacing
+ shadow
;
1034 char* display_string
= resource_widget_value (mw
, val
);
1035 draw_shadow_rectangle (mw
, ws
->window
, x
, y
, width
, height
, True
,
1038 /* Deal with centering a menu title. */
1039 if (!horizontal_p
&& !val
->contents
&& !val
->call_data
)
1041 int l
= string_width (mw
, display_string
);
1044 x_offset
= (width
- l
) >> 1;
1046 else if (!horizontal_p
&& ws
->button_width
)
1047 x_offset
+= ws
->button_width
;
1051 if (mw
->menu
.fontSet
)
1052 XmbDrawString (XtDisplay (mw
), ws
->window
, mw
->menu
.fontSet
,
1054 y
+ v_spacing
+ shadow
+ font_ascent
,
1055 display_string
, strlen (display_string
));
1058 XDrawString (XtDisplay (mw
), ws
->window
,
1060 y
+ v_spacing
+ shadow
+ font_ascent
,
1061 display_string
, strlen (display_string
));
1065 if (val
->button_type
== BUTTON_TYPE_TOGGLE
)
1066 draw_toggle (mw
, ws
->window
, x
, y
+ v_spacing
+ shadow
,
1068 else if (val
->button_type
== BUTTON_TYPE_RADIO
)
1069 draw_radio (mw
, ws
->window
, x
, y
+ v_spacing
+ shadow
,
1074 int a_w
= arrow_width (mw
);
1075 draw_arrow (mw
, ws
->window
, deco_gc
,
1077 - mw
->menu
.horizontal_spacing
1078 - mw
->menu
.shadow_thickness
,
1079 y
+ v_spacing
+ shadow
, a_w
,
1085 if (mw
->menu
.fontSet
)
1086 XmbDrawString (XtDisplay (mw
), ws
->window
,
1089 x
+ label_width
+ mw
->menu
.arrow_spacing
,
1090 y
+ v_spacing
+ shadow
+ font_ascent
,
1091 val
->key
, strlen (val
->key
));
1094 XDrawString (XtDisplay (mw
), ws
->window
,
1096 x
+ label_width
+ mw
->menu
.arrow_spacing
,
1097 y
+ v_spacing
+ shadow
+ font_ascent
,
1098 val
->key
, strlen (val
->key
));
1103 XDrawRectangle (XtDisplay (mw
), ws
->window
,
1104 mw
->menu
.background_gc
,
1105 x
+ shadow
, y
+ shadow
,
1106 label_width
+ h_spacing
- 1,
1107 font_height
+ 2 * v_spacing
- 1);
1108 draw_shadow_rectangle (mw
, ws
->window
, x
, y
, width
, height
,
1113 draw_shadow_rectangle (mw
, ws
->window
, x
, y
, width
, height
, False
,
1123 display_menu (mw
, level
, just_compute_p
, highlighted_pos
, hit
, hit_return
,
1127 Boolean just_compute_p
;
1128 XPoint
* highlighted_pos
;
1130 widget_value
** hit_return
;
1135 widget_value
* following_item
;
1138 int horizontal_p
= mw
->menu
.horizontal
&& (level
== 0);
1140 int just_compute_this_one_p
;
1141 /* This is set nonzero if the element containing HIGHLIGHTED_POS
1142 is disabled, so that we do not return any subsequent element either. */
1144 enum menu_separator separator
;
1146 if (level
>= mw
->menu
.old_depth
)
1147 abort_gracefully ((Widget
) mw
);
1149 if (level
< mw
->menu
.old_depth
- 1)
1150 following_item
= mw
->menu
.old_stack
[level
+ 1];
1152 following_item
= NULL
;
1160 ws
= &mw
->menu
.windows
[level
];
1161 for (val
= mw
->menu
.old_stack
[level
]->contents
; val
; val
= val
->next
)
1163 highlighted_p
= val
== following_item
;
1164 if (highlighted_p
&& highlighted_pos
)
1167 highlighted_pos
->x
= where
.x
;
1169 highlighted_pos
->y
= where
.y
;
1172 just_compute_this_one_p
=
1173 just_compute_p
|| ((this || that
) && val
!= this && val
!= that
);
1175 display_menu_item (mw
, val
, ws
, &where
, highlighted_p
, horizontal_p
,
1176 just_compute_this_one_p
);
1178 if (highlighted_p
&& highlighted_pos
)
1181 highlighted_pos
->y
= where
.y
;
1183 highlighted_pos
->x
= where
.x
;
1188 && (horizontal_p
? hit
->x
< where
.x
: hit
->y
< where
.y
)
1189 && !lw_separator_p (val
->name
, &separator
, 0)
1196 if (mw
->menu
.inside_entry
!= val
)
1198 if (mw
->menu
.inside_entry
)
1199 XtCallCallbackList ((Widget
)mw
, mw
->menu
.leave
,
1200 (XtPointer
) mw
->menu
.inside_entry
);
1201 mw
->menu
.inside_entry
= val
;
1202 XtCallCallbackList ((Widget
)mw
, mw
->menu
.enter
,
1203 (XtPointer
) mw
->menu
.inside_entry
);
1213 if (!just_compute_p
)
1214 draw_shadow_rectangle (mw
, ws
->window
, 0, 0, ws
->width
, ws
->height
,
1220 set_new_state (mw
, val
, level
)
1227 mw
->menu
.new_depth
= 0;
1228 for (i
= 0; i
< level
; i
++)
1229 push_new_stack (mw
, mw
->menu
.old_stack
[i
]);
1230 push_new_stack (mw
, val
);
1234 make_windows_if_needed (mw
, n
)
1240 XSetWindowAttributes xswa
;
1242 Window root
= RootWindowOfScreen (DefaultScreenOfDisplay (XtDisplay (mw
)));
1243 window_state
* windows
;
1245 if (mw
->menu
.windows_length
>= n
)
1248 xswa
.save_under
= True
;
1249 xswa
.override_redirect
= True
;
1250 xswa
.background_pixel
= mw
->core
.background_pixel
;
1251 xswa
.border_pixel
= mw
->core
.border_pixel
;
1253 ExposureMask
| PointerMotionMask
| PointerMotionHintMask
1254 | ButtonReleaseMask
| ButtonPressMask
;
1255 xswa
.cursor
= mw
->menu
.cursor_shape
;
1256 mask
= CWSaveUnder
| CWOverrideRedirect
| CWBackPixel
| CWBorderPixel
1257 | CWEventMask
| CWCursor
;
1259 if (!mw
->menu
.windows
)
1262 (window_state
*)XtMalloc (n
* sizeof (window_state
));
1268 (window_state
*)XtRealloc ((char*)mw
->menu
.windows
,
1269 n
* sizeof (window_state
));
1270 start_at
= mw
->menu
.windows_length
;
1272 mw
->menu
.windows_length
= n
;
1274 windows
= mw
->menu
.windows
;
1276 for (i
= start_at
; i
< n
; i
++)
1280 windows
[i
].width
= 1;
1281 windows
[i
].height
= 1;
1282 windows
[i
].window
=
1283 XCreateWindow (XtDisplay (mw
), root
, 0, 0, 1, 1,
1284 0, 0, CopyFromParent
, CopyFromParent
, mask
, &xswa
);
1288 /* Value is non-zero if WINDOW is part of menu bar widget W. */
1291 xlwmenu_window_p (w
, window
)
1295 XlwMenuWidget mw
= (XlwMenuWidget
) w
;
1298 for (i
= 0; i
< mw
->menu
.windows_length
; ++i
)
1299 if (window
== mw
->menu
.windows
[i
].window
)
1302 return i
< mw
->menu
.windows_length
;
1305 /* Make the window fit in the screen */
1307 fit_to_screen (mw
, ws
, previous_ws
, horizontal_p
)
1310 window_state
* previous_ws
;
1311 Boolean horizontal_p
;
1313 unsigned int screen_width
= WidthOfScreen (XtScreen (mw
));
1314 unsigned int screen_height
= HeightOfScreen (XtScreen (mw
));
1315 /* 1 if we are unable to avoid an overlap between
1316 this menu and the parent menu in the X dimension. */
1317 int horizontal_overlap
= 0;
1321 else if (ws
->x
+ ws
->width
> screen_width
)
1324 /* The addition of shadow-thickness for a sub-menu's position is
1325 to reflect a similar adjustment when the menu is displayed to
1326 the right of the invoking menu-item; it makes the sub-menu
1327 look more `attached' to the menu-item. */
1328 ws
->x
= previous_ws
->x
- ws
->width
+ mw
->menu
.shadow_thickness
;
1330 ws
->x
= screen_width
- ws
->width
;
1334 horizontal_overlap
= 1;
1337 /* If we overlap in X, try to avoid overlap in Y. */
1338 if (horizontal_overlap
1339 && ws
->y
< previous_ws
->y
+ previous_ws
->height
1340 && previous_ws
->y
< ws
->y
+ ws
->height
)
1342 /* Put this menu right below or right above PREVIOUS_WS
1344 if (previous_ws
->y
+ previous_ws
->height
+ ws
->height
< screen_height
)
1345 ws
->y
= previous_ws
->y
+ previous_ws
->height
;
1346 else if (previous_ws
->y
- ws
->height
> 0)
1347 ws
->y
= previous_ws
->y
- ws
->height
;
1352 else if (ws
->y
+ ws
->height
> screen_height
)
1355 ws
->y
= previous_ws
->y
- ws
->height
;
1357 ws
->y
= screen_height
- ws
->height
;
1363 /* Updates old_stack from new_stack and redisplays. */
1370 XPoint selection_position
;
1371 int old_depth
= mw
->menu
.old_depth
;
1372 int new_depth
= mw
->menu
.new_depth
;
1373 widget_value
** old_stack
;
1374 widget_value
** new_stack
;
1375 window_state
* windows
;
1376 widget_value
* old_selection
;
1377 widget_value
* new_selection
;
1379 /* Check that enough windows and old_stack are ready. */
1380 make_windows_if_needed (mw
, new_depth
);
1381 make_old_stack_space (mw
, new_depth
);
1382 windows
= mw
->menu
.windows
;
1383 old_stack
= mw
->menu
.old_stack
;
1384 new_stack
= mw
->menu
.new_stack
;
1386 /* compute the last identical different entry */
1387 for (i
= 1; i
< old_depth
&& i
< new_depth
; i
++)
1388 if (old_stack
[i
] != new_stack
[i
])
1392 /* Memorize the previously selected item to be able to refresh it */
1393 old_selection
= last_same
+ 1 < old_depth
? old_stack
[last_same
+ 1] : NULL
;
1394 if (old_selection
&& !old_selection
->enabled
)
1395 old_selection
= NULL
;
1396 new_selection
= last_same
+ 1 < new_depth
? new_stack
[last_same
+ 1] : NULL
;
1397 if (new_selection
&& !new_selection
->enabled
)
1398 new_selection
= NULL
;
1400 /* Call callback when the hightlighted item changes. */
1401 if (old_selection
|| new_selection
)
1402 XtCallCallbackList ((Widget
)mw
, mw
->menu
.highlight
,
1403 (XtPointer
) new_selection
);
1405 /* updates old_state from new_state. It has to be done now because
1406 display_menu (called below) uses the old_stack to know what to display. */
1407 for (i
= last_same
+ 1; i
< new_depth
; i
++)
1408 old_stack
[i
] = new_stack
[i
];
1409 mw
->menu
.old_depth
= new_depth
;
1411 /* refresh the last selection */
1412 selection_position
.x
= 0;
1413 selection_position
.y
= 0;
1414 display_menu (mw
, last_same
, new_selection
== old_selection
,
1415 &selection_position
, NULL
, NULL
, old_selection
, new_selection
);
1417 /* Now place the new menus. */
1418 for (i
= last_same
+ 1; i
< new_depth
&& new_stack
[i
]->contents
; i
++)
1420 window_state
*previous_ws
= &windows
[i
- 1];
1421 window_state
*ws
= &windows
[i
];
1423 ws
->x
= (previous_ws
->x
+ selection_position
.x
1424 + mw
->menu
.shadow_thickness
);
1425 if (mw
->menu
.horizontal
&& i
== 1)
1426 ws
->x
+= mw
->menu
.margin
;
1429 if (!mw
->menu
.horizontal
|| i
> 1)
1430 ws
->x
+= mw
->menu
.shadow_thickness
;
1433 ws
->y
= (previous_ws
->y
+ selection_position
.y
1434 + mw
->menu
.shadow_thickness
);
1435 if (mw
->menu
.horizontal
&& i
== 1)
1436 ws
->y
+= mw
->menu
.margin
;
1440 fit_to_screen (mw
, ws
, previous_ws
, mw
->menu
.horizontal
&& i
== 1);
1442 XClearWindow (XtDisplay (mw
), ws
->window
);
1443 XMoveResizeWindow (XtDisplay (mw
), ws
->window
, ws
->x
, ws
->y
,
1444 ws
->width
, ws
->height
);
1445 XMapRaised (XtDisplay (mw
), ws
->window
);
1446 display_menu (mw
, i
, False
, &selection_position
, NULL
, NULL
, NULL
, NULL
);
1449 /* unmap the menus that popped down */
1450 for (i
= new_depth
- 1; i
< old_depth
; i
++)
1451 if (i
>= new_depth
|| (i
> 0 && !new_stack
[i
]->contents
))
1452 XUnmapWindow (XtDisplay (mw
), windows
[i
].window
);
1456 motion_event_is_in_menu (mw
, ev
, level
, relative_pos
)
1460 XPoint
* relative_pos
;
1462 window_state
* ws
= &mw
->menu
.windows
[level
];
1463 int shadow
= level
== 0 ? 0 : mw
->menu
.shadow_thickness
;
1464 int x
= ws
->x
+ shadow
;
1465 int y
= ws
->y
+ shadow
;
1466 relative_pos
->x
= ev
->x_root
- x
;
1467 relative_pos
->y
= ev
->y_root
- y
;
1468 return (x
- shadow
< ev
->x_root
&& ev
->x_root
< x
+ ws
->width
1469 && y
- shadow
< ev
->y_root
&& ev
->y_root
< y
+ ws
->height
);
1473 map_event_to_widget_value (mw
, ev
, val
, level
)
1480 XPoint relative_pos
;
1486 /* Find the window */
1487 for (i
= mw
->menu
.old_depth
- 1; i
>= 0; i
--)
1489 ws
= &mw
->menu
.windows
[i
];
1490 if (ws
&& motion_event_is_in_menu (mw
, ev
, i
, &relative_pos
))
1493 display_menu (mw
, i
, True
, NULL
, &relative_pos
, val
, NULL
, NULL
);
1505 if (mw
->menu
.inside_entry
!= NULL
)
1506 XtCallCallbackList ((Widget
)mw
, mw
->menu
.leave
,
1507 (XtPointer
) mw
->menu
.inside_entry
);
1508 mw
->menu
.inside_entry
= NULL
;
1516 make_drawing_gcs (mw
)
1521 XtGCMask mask
= GCForeground
| GCBackground
;
1524 if (!mw
->menu
.fontSet
)
1526 xgcv
.font
= mw
->menu
.font
->fid
;
1530 xgcv
.font
= mw
->menu
.font
->fid
;
1533 xgcv
.foreground
= mw
->menu
.foreground
;
1534 xgcv
.background
= mw
->core
.background_pixel
;
1535 mw
->menu
.foreground_gc
= XtGetGC ((Widget
)mw
, mask
, &xgcv
);
1537 xgcv
.foreground
= mw
->menu
.button_foreground
;
1538 mw
->menu
.button_gc
= XtGetGC ((Widget
)mw
, mask
, &xgcv
);
1540 xgcv
.background
= mw
->core
.background_pixel
;
1542 #define BRIGHTNESS(color) (((color) & 0xff) + (((color) >> 8) & 0xff) + (((color) >> 16) & 0xff))
1544 /* Allocate color for disabled menu-items. */
1545 mw
->menu
.disabled_foreground
= mw
->menu
.foreground
;
1546 if (BRIGHTNESS(mw
->menu
.foreground
) < BRIGHTNESS(mw
->core
.background_pixel
))
1551 x_alloc_lighter_color_for_widget ((Widget
) mw
, XtDisplay ((Widget
) mw
),
1553 &mw
->menu
.disabled_foreground
,
1557 if (mw
->menu
.foreground
== mw
->menu
.disabled_foreground
1558 || mw
->core
.background_pixel
== mw
->menu
.disabled_foreground
)
1560 /* Too few colors, use stipple. */
1561 xgcv
.foreground
= mw
->menu
.foreground
;
1562 xgcv
.fill_style
= FillStippled
;
1563 xgcv
.stipple
= mw
->menu
.gray_pixmap
;
1564 mw
->menu
.disabled_gc
= XtGetGC ((Widget
)mw
, mask
1565 | GCFillStyle
| GCStipple
, &xgcv
);
1569 /* Many colors available, use disabled pixel. */
1570 xgcv
.foreground
= mw
->menu
.disabled_foreground
;
1571 mw
->menu
.disabled_gc
= XtGetGC ((Widget
)mw
, mask
, &xgcv
);
1574 xgcv
.foreground
= mw
->menu
.button_foreground
;
1575 xgcv
.background
= mw
->core
.background_pixel
;
1576 xgcv
.fill_style
= FillStippled
;
1577 xgcv
.stipple
= mw
->menu
.gray_pixmap
;
1578 mw
->menu
.inactive_button_gc
= XtGetGC ((Widget
)mw
, mask
1579 | GCFillStyle
| GCStipple
, &xgcv
);
1581 xgcv
.foreground
= mw
->core
.background_pixel
;
1582 xgcv
.background
= mw
->menu
.foreground
;
1583 mw
->menu
.background_gc
= XtGetGC ((Widget
)mw
, mask
, &xgcv
);
1587 release_drawing_gcs (mw
)
1590 XtReleaseGC ((Widget
) mw
, mw
->menu
.foreground_gc
);
1591 XtReleaseGC ((Widget
) mw
, mw
->menu
.button_gc
);
1592 XtReleaseGC ((Widget
) mw
, mw
->menu
.disabled_gc
);
1593 XtReleaseGC ((Widget
) mw
, mw
->menu
.inactive_button_gc
);
1594 XtReleaseGC ((Widget
) mw
, mw
->menu
.background_gc
);
1595 /* let's get some segvs if we try to use these... */
1596 mw
->menu
.foreground_gc
= (GC
) -1;
1597 mw
->menu
.button_gc
= (GC
) -1;
1598 mw
->menu
.disabled_gc
= (GC
) -1;
1599 mw
->menu
.inactive_button_gc
= (GC
) -1;
1600 mw
->menu
.background_gc
= (GC
) -1;
1603 #define MINL(x,y) ((((unsigned long) (x)) < ((unsigned long) (y))) \
1604 ? ((unsigned long) (x)) : ((unsigned long) (y)))
1607 make_shadow_gcs (mw
)
1611 unsigned long pm
= 0;
1612 Display
*dpy
= XtDisplay ((Widget
) mw
);
1613 Screen
*screen
= XtScreen ((Widget
) mw
);
1614 Colormap cmap
= mw
->core
.colormap
;
1616 int top_frobbed
= 0, bottom_frobbed
= 0;
1618 mw
->menu
.free_top_shadow_color_p
= 0;
1619 mw
->menu
.free_bottom_shadow_color_p
= 0;
1621 if (mw
->menu
.top_shadow_color
== -1)
1622 mw
->menu
.top_shadow_color
= mw
->core
.background_pixel
;
1624 mw
->menu
.top_shadow_color
= mw
->menu
.top_shadow_color
;
1626 if (mw
->menu
.bottom_shadow_color
== -1)
1627 mw
->menu
.bottom_shadow_color
= mw
->menu
.foreground
;
1629 mw
->menu
.bottom_shadow_color
= mw
->menu
.bottom_shadow_color
;
1631 if (mw
->menu
.top_shadow_color
== mw
->core
.background_pixel
||
1632 mw
->menu
.top_shadow_color
== mw
->menu
.foreground
)
1634 topc
.pixel
= mw
->core
.background_pixel
;
1636 if (x_alloc_lighter_color_for_widget ((Widget
) mw
, dpy
, cmap
,
1640 XQueryColor (dpy
, cmap
, &topc
);
1641 /* don't overflow/wrap! */
1642 topc
.red
= MINL (65535, topc
.red
* 1.2);
1643 topc
.green
= MINL (65535, topc
.green
* 1.2);
1644 topc
.blue
= MINL (65535, topc
.blue
* 1.2);
1645 if (XAllocColor (dpy
, cmap
, &topc
))
1648 mw
->menu
.top_shadow_color
= topc
.pixel
;
1649 mw
->menu
.free_top_shadow_color_p
= 1;
1653 if (mw
->menu
.bottom_shadow_color
== mw
->menu
.foreground
||
1654 mw
->menu
.bottom_shadow_color
== mw
->core
.background_pixel
)
1656 botc
.pixel
= mw
->core
.background_pixel
;
1658 if (x_alloc_lighter_color_for_widget ((Widget
) mw
, dpy
, cmap
,
1662 XQueryColor (dpy
, cmap
, &botc
);
1666 if (XAllocColor (dpy
, cmap
, &botc
))
1669 mw
->menu
.bottom_shadow_color
= botc
.pixel
;
1670 mw
->menu
.free_bottom_shadow_color_p
= 1;
1675 if (top_frobbed
&& bottom_frobbed
)
1677 if (topc
.pixel
== botc
.pixel
)
1679 if (botc
.pixel
== mw
->menu
.foreground
)
1681 if (mw
->menu
.free_top_shadow_color_p
)
1683 x_free_dpy_colors (dpy
, screen
, cmap
,
1684 &mw
->menu
.top_shadow_color
, 1);
1685 mw
->menu
.free_top_shadow_color_p
= 0;
1687 mw
->menu
.top_shadow_color
= mw
->core
.background_pixel
;
1691 if (mw
->menu
.free_bottom_shadow_color_p
)
1693 x_free_dpy_colors (dpy
, screen
, cmap
,
1694 &mw
->menu
.bottom_shadow_color
, 1);
1695 mw
->menu
.free_bottom_shadow_color_p
= 0;
1697 mw
->menu
.bottom_shadow_color
= mw
->menu
.foreground
;
1702 if (!mw
->menu
.top_shadow_pixmap
&&
1703 mw
->menu
.top_shadow_color
== mw
->core
.background_pixel
)
1705 mw
->menu
.top_shadow_pixmap
= mw
->menu
.gray_pixmap
;
1706 if (mw
->menu
.free_top_shadow_color_p
)
1708 x_free_dpy_colors (dpy
, screen
, cmap
, &mw
->menu
.top_shadow_color
, 1);
1709 mw
->menu
.free_top_shadow_color_p
= 0;
1711 mw
->menu
.top_shadow_color
= mw
->menu
.foreground
;
1713 if (!mw
->menu
.bottom_shadow_pixmap
&&
1714 mw
->menu
.bottom_shadow_color
== mw
->core
.background_pixel
)
1716 mw
->menu
.bottom_shadow_pixmap
= mw
->menu
.gray_pixmap
;
1717 if (mw
->menu
.free_bottom_shadow_color_p
)
1719 x_free_dpy_colors (dpy
, screen
, cmap
,
1720 &mw
->menu
.bottom_shadow_color
, 1);
1721 mw
->menu
.free_bottom_shadow_color_p
= 0;
1723 mw
->menu
.bottom_shadow_color
= mw
->menu
.foreground
;
1726 xgcv
.fill_style
= FillStippled
;
1727 xgcv
.foreground
= mw
->menu
.top_shadow_color
;
1728 xgcv
.stipple
= mw
->menu
.top_shadow_pixmap
;
1729 pm
= (xgcv
.stipple
? GCStipple
|GCFillStyle
: 0);
1730 mw
->menu
.shadow_top_gc
= XtGetGC ((Widget
)mw
, GCForeground
| pm
, &xgcv
);
1732 xgcv
.foreground
= mw
->menu
.bottom_shadow_color
;
1733 xgcv
.stipple
= mw
->menu
.bottom_shadow_pixmap
;
1734 pm
= (xgcv
.stipple
? GCStipple
|GCFillStyle
: 0);
1735 mw
->menu
.shadow_bottom_gc
= XtGetGC ((Widget
)mw
, GCForeground
| pm
, &xgcv
);
1740 release_shadow_gcs (mw
)
1743 Display
*dpy
= XtDisplay ((Widget
) mw
);
1744 Screen
*screen
= XtScreen ((Widget
) mw
);
1745 Colormap cmap
= mw
->core
.colormap
;
1749 if (mw
->menu
.free_top_shadow_color_p
)
1750 px
[i
++] = mw
->menu
.top_shadow_color
;
1751 if (mw
->menu
.free_bottom_shadow_color_p
)
1752 px
[i
++] = mw
->menu
.bottom_shadow_color
;
1754 x_free_dpy_colors (dpy
, screen
, cmap
, px
, i
);
1756 XtReleaseGC ((Widget
) mw
, mw
->menu
.shadow_top_gc
);
1757 XtReleaseGC ((Widget
) mw
, mw
->menu
.shadow_bottom_gc
);
1761 XlwMenuInitialize (request
, mw
, args
, num_args
)
1767 /* Get the GCs and the widget size */
1769 Window window
= RootWindowOfScreen (DefaultScreenOfDisplay (XtDisplay (mw
)));
1770 Display
* display
= XtDisplay (mw
);
1773 widget_value
*tem
= (widget_value
*) XtMalloc (sizeof (widget_value
));
1775 /* _XtCreate is freeing the object that was passed to us,
1776 so make a copy that we will actually keep. */
1777 lwlib_bcopy (mw
->menu
.contents
, tem
, sizeof (widget_value
));
1778 mw
->menu
.contents
= tem
;
1781 /* mw->menu.cursor = XCreateFontCursor (display, mw->menu.cursor_shape); */
1782 mw
->menu
.cursor
= mw
->menu
.cursor_shape
;
1784 mw
->menu
.gray_pixmap
1785 = XCreatePixmapFromBitmapData (display
, window
, gray_bitmap_bits
,
1786 gray_bitmap_width
, gray_bitmap_height
,
1787 (unsigned long)1, (unsigned long)0, 1);
1789 /* I don't understand why this ends up 0 sometimes,
1790 but it does. This kludge works around it.
1791 Can anyone find a real fix? -- rms. */
1792 if (mw
->menu
.font
== 0)
1793 mw
->menu
.font
= xlwmenu_default_font
;
1795 if (mw
->menu
.fontSet
)
1796 mw
->menu
.font_extents
= XExtentsOfFontSet (mw
->menu
.fontSet
);
1799 make_drawing_gcs (mw
);
1800 make_shadow_gcs (mw
);
1802 mw
->menu
.popped_up
= False
;
1804 mw
->menu
.old_depth
= 1;
1805 mw
->menu
.old_stack
= (widget_value
**)XtMalloc (sizeof (widget_value
*));
1806 mw
->menu
.old_stack_length
= 1;
1807 mw
->menu
.old_stack
[0] = mw
->menu
.contents
;
1809 mw
->menu
.new_depth
= 0;
1810 mw
->menu
.new_stack
= 0;
1811 mw
->menu
.new_stack_length
= 0;
1812 push_new_stack (mw
, mw
->menu
.contents
);
1814 mw
->menu
.windows
= (window_state
*)XtMalloc (sizeof (window_state
));
1815 mw
->menu
.windows_length
= 1;
1816 mw
->menu
.windows
[0].x
= 0;
1817 mw
->menu
.windows
[0].y
= 0;
1818 mw
->menu
.windows
[0].width
= 0;
1819 mw
->menu
.windows
[0].height
= 0;
1822 mw
->core
.width
= mw
->menu
.windows
[0].width
;
1823 mw
->core
.height
= mw
->menu
.windows
[0].height
;
1827 XlwMenuClassInitialize ()
1832 XlwMenuRealize (w
, valueMask
, attributes
)
1835 XSetWindowAttributes
*attributes
;
1837 XlwMenuWidget mw
= (XlwMenuWidget
)w
;
1838 XSetWindowAttributes xswa
;
1841 (*xlwMenuWidgetClass
->core_class
.superclass
->core_class
.realize
)
1842 (w
, valueMask
, attributes
);
1844 xswa
.save_under
= True
;
1845 xswa
.cursor
= mw
->menu
.cursor_shape
;
1846 mask
= CWSaveUnder
| CWCursor
;
1847 /* I sometimes get random BadCursor errors while creating the first
1848 frame on a display. I can not find their reason, but they are
1849 annoying so for now let's ignore any errors here. -- lorentey */
1851 x_catch_errors (XtDisplay (w
));
1853 XChangeWindowAttributes (XtDisplay (w
), XtWindow (w
), mask
, &xswa
);
1855 x_uncatch_errors ();
1858 mw
->menu
.windows
[0].window
= XtWindow (w
);
1859 mw
->menu
.windows
[0].x
= w
->core
.x
;
1860 mw
->menu
.windows
[0].y
= w
->core
.y
;
1861 mw
->menu
.windows
[0].width
= w
->core
.width
;
1862 mw
->menu
.windows
[0].height
= w
->core
.height
;
1865 /* Only the toplevel menubar/popup is a widget so it's the only one that
1866 receives expose events through Xt. So we repaint all the other panes
1867 when receiving an Expose event. */
1869 XlwMenuRedisplay (w
, ev
, region
)
1874 XlwMenuWidget mw
= (XlwMenuWidget
)w
;
1877 /* If we have a depth beyond 1, it's because a submenu was displayed.
1878 If the submenu has been destroyed, set the depth back to 1. */
1879 if (submenu_destroyed
)
1881 mw
->menu
.old_depth
= 1;
1882 submenu_destroyed
= 0;
1885 for (i
= 0; i
< mw
->menu
.old_depth
; i
++)
1886 display_menu (mw
, i
, False
, NULL
, NULL
, NULL
, NULL
, NULL
);
1890 /* Part of a hack to make the menu redisplay when a tooltip frame
1891 over a menu item is unmapped. */
1894 xlwmenu_redisplay (w
)
1897 XlwMenuRedisplay (w
, NULL
, None
);
1905 XlwMenuWidget mw
= (XlwMenuWidget
) w
;
1907 if (pointer_grabbed
)
1908 ungrab_all ((Widget
)w
, CurrentTime
);
1909 pointer_grabbed
= 0;
1911 submenu_destroyed
= 1;
1913 release_drawing_gcs (mw
);
1914 release_shadow_gcs (mw
);
1916 /* this doesn't come from the resource db but is created explicitly
1917 so we must free it ourselves. */
1918 XFreePixmap (XtDisplay (mw
), mw
->menu
.gray_pixmap
);
1919 mw
->menu
.gray_pixmap
= (Pixmap
) -1;
1922 /* Do free mw->menu.contents because nowadays we copy it
1923 during initialization. */
1924 XtFree (mw
->menu
.contents
);
1927 /* Don't free mw->menu.contents because that comes from our creator.
1928 The `*_stack' elements are just pointers into `contents' so leave
1929 that alone too. But free the stacks themselves. */
1930 if (mw
->menu
.old_stack
) XtFree ((char *) mw
->menu
.old_stack
);
1931 if (mw
->menu
.new_stack
) XtFree ((char *) mw
->menu
.new_stack
);
1933 /* Remember, you can't free anything that came from the resource
1934 database. This includes:
1936 mw->menu.top_shadow_pixmap
1937 mw->menu.bottom_shadow_pixmap
1939 Also the color cells of top_shadow_color, bottom_shadow_color,
1940 foreground, and button_foreground will never be freed until this
1941 client exits. Nice, eh?
1944 /* start from 1 because the one in slot 0 is w->core.window */
1945 for (i
= 1; i
< mw
->menu
.windows_length
; i
++)
1946 XDestroyWindow (XtDisplay (mw
), mw
->menu
.windows
[i
].window
);
1947 if (mw
->menu
.windows
)
1948 XtFree ((char *) mw
->menu
.windows
);
1952 XlwMenuSetValues (current
, request
, new)
1957 XlwMenuWidget oldmw
= (XlwMenuWidget
)current
;
1958 XlwMenuWidget newmw
= (XlwMenuWidget
)new;
1959 Boolean redisplay
= False
;
1962 if (newmw
->menu
.contents
1963 && newmw
->menu
.contents
->contents
1964 && newmw
->menu
.contents
->contents
->change
>= VISIBLE_CHANGE
)
1966 /* Do redisplay if the contents are entirely eliminated. */
1967 if (newmw
->menu
.contents
1968 && newmw
->menu
.contents
->contents
== 0
1969 && newmw
->menu
.contents
->change
>= VISIBLE_CHANGE
)
1972 if (newmw
->core
.background_pixel
!= oldmw
->core
.background_pixel
1973 || newmw
->menu
.foreground
!= oldmw
->menu
.foreground
1975 || newmw
->menu
.fontSet
!= oldmw
->menu
.fontSet
1976 || (newmw
->menu
.fontSet
== NULL
&& newmw
->menu
.font
!= oldmw
->menu
.font
)
1978 || newmw
->menu
.font
!= oldmw
->menu
.font
1982 release_drawing_gcs (newmw
);
1983 make_drawing_gcs (newmw
);
1985 release_shadow_gcs (newmw
);
1986 /* Cause the shadow colors to be recalculated. */
1987 newmw
->menu
.top_shadow_color
= -1;
1988 newmw
->menu
.bottom_shadow_color
= -1;
1989 make_shadow_gcs (newmw
);
1993 if (XtIsRealized (current
))
1994 /* If the menu is currently displayed, change the display. */
1995 for (i
= 0; i
< oldmw
->menu
.windows_length
; i
++)
1997 XSetWindowBackground (XtDisplay (oldmw
),
1998 oldmw
->menu
.windows
[i
].window
,
1999 newmw
->core
.background_pixel
);
2000 /* clear windows and generate expose events */
2001 XClearArea (XtDisplay (oldmw
), oldmw
->menu
.windows
[i
].window
,
2007 if (newmw
->menu
.fontSet
!= oldmw
->menu
.fontSet
&& newmw
->menu
.fontSet
!= NULL
)
2010 newmw
->menu
.font_extents
= XExtentsOfFontSet (newmw
->menu
.fontSet
);
2021 XlwMenuWidget mw
= (XlwMenuWidget
)w
;
2023 if (mw
->menu
.popped_up
)
2025 /* Don't allow the popup menu to resize itself. */
2026 mw
->core
.width
= mw
->menu
.windows
[0].width
;
2027 mw
->core
.height
= mw
->menu
.windows
[0].height
;
2028 mw
->core
.parent
->core
.width
= mw
->core
.width
;
2029 mw
->core
.parent
->core
.height
= mw
->core
.height
;
2033 mw
->menu
.windows
[0].width
= mw
->core
.width
;
2034 mw
->menu
.windows
[0].height
= mw
->core
.height
;
2038 \f/* Action procedures */
2040 handle_single_motion_event (mw
, ev
)
2047 if (!map_event_to_widget_value (mw
, ev
, &val
, &level
))
2048 pop_new_stack_if_no_contents (mw
);
2050 set_new_state (mw
, val
, level
);
2053 /* Sync with the display. Makes it feel better on X terms. */
2054 XSync (XtDisplay (mw
), False
);
2058 handle_motion_event (mw
, ev
)
2064 int state
= ev
->state
;
2066 handle_single_motion_event (mw
, ev
);
2068 /* allow motion events to be generated again */
2070 && XQueryPointer (XtDisplay (mw
), ev
->window
,
2071 &ev
->root
, &ev
->subwindow
,
2072 &ev
->x_root
, &ev
->y_root
,
2075 && ev
->state
== state
2076 && (ev
->x_root
!= x
|| ev
->y_root
!= y
))
2077 handle_single_motion_event (mw
, ev
);
2081 Start (w
, ev
, params
, num_params
)
2085 Cardinal
*num_params
;
2087 XlwMenuWidget mw
= (XlwMenuWidget
)w
;
2089 if (!mw
->menu
.popped_up
)
2091 menu_post_event
= *ev
;
2092 /* If event is set to CurrentTime, get the last known time stamp.
2093 This is for calculating if (popup) menus should stay up after
2095 if (menu_post_event
.xbutton
.time
== CurrentTime
)
2096 menu_post_event
.xbutton
.time
2097 = XtLastTimestampProcessed (XtDisplay (w
));
2099 pop_up_menu (mw
, (XButtonPressedEvent
*) ev
);
2103 /* If we push a button while the menu is posted semipermanently,
2104 releasing the button should always pop the menu down. */
2105 next_release_must_exit
= 1;
2107 /* notes the absolute position of the menubar window */
2108 mw
->menu
.windows
[0].x
= ev
->xmotion
.x_root
- ev
->xmotion
.x
;
2109 mw
->menu
.windows
[0].y
= ev
->xmotion
.y_root
- ev
->xmotion
.y
;
2111 /* handles the down like a move, slots are compatible */
2112 ev
->xmotion
.is_hint
= 0;
2113 handle_motion_event (mw
, &ev
->xmotion
);
2118 Drag (w
, ev
, params
, num_params
)
2122 Cardinal
*num_params
;
2124 XlwMenuWidget mw
= (XlwMenuWidget
)w
;
2125 if (mw
->menu
.popped_up
)
2126 handle_motion_event (mw
, &ev
->xmotion
);
2130 This is how we handle presses and releases of modifier keys. */
2132 Nothing (w
, ev
, params
, num_params
)
2136 Cardinal
*num_params
;
2140 static widget_value
*
2141 find_first_selectable (mw
, item
, skip_titles
)
2146 widget_value
*current
= item
;
2147 enum menu_separator separator
;
2149 while (lw_separator_p (current
->name
, &separator
, 0) || !current
->enabled
2150 || (skip_titles
&& !current
->call_data
&& !current
->contents
))
2152 current
=current
->next
;
2159 static widget_value
*
2160 find_next_selectable (mw
, item
, skip_titles
)
2165 widget_value
*current
= item
;
2166 enum menu_separator separator
;
2168 while (current
->next
&& (current
=current
->next
) &&
2169 (lw_separator_p (current
->name
, &separator
, 0) || !current
->enabled
2170 || (skip_titles
&& !current
->call_data
&& !current
->contents
)))
2173 if (current
== item
)
2175 if (mw
->menu
.old_depth
< 2)
2177 current
= mw
->menu
.old_stack
[mw
->menu
.old_depth
- 2]->contents
;
2179 while (lw_separator_p (current
->name
, &separator
, 0)
2180 || !current
->enabled
2181 || (skip_titles
&& !current
->call_data
2182 && !current
->contents
))
2185 current
=current
->next
;
2187 if (current
== item
)
2196 static widget_value
*
2197 find_prev_selectable (mw
, item
, skip_titles
)
2202 widget_value
*current
= item
;
2203 widget_value
*prev
= item
;
2205 while ((current
=find_next_selectable (mw
, current
, skip_titles
))
2208 if (prev
== current
)
2217 Down (w
, ev
, params
, num_params
)
2221 Cardinal
*num_params
;
2223 XlwMenuWidget mw
= (XlwMenuWidget
) w
;
2224 widget_value
* selected_item
= mw
->menu
.old_stack
[mw
->menu
.old_depth
- 1];
2225 int popup_menu_p
= mw
->menu
.top_depth
== 1;
2227 /* Inside top-level menu-bar? */
2228 if (mw
->menu
.old_depth
== mw
->menu
.top_depth
)
2229 /* When <down> in the menu-bar is pressed, display the corresponding
2230 sub-menu and select the first selectable menu item there.
2231 If this is a popup menu, skip title item of the popup. */
2233 find_first_selectable (mw
,
2234 selected_item
->contents
,
2236 mw
->menu
.old_depth
);
2238 /* Highlight next possible (enabled and not separator) menu item. */
2239 set_new_state (mw
, find_next_selectable (mw
, selected_item
, popup_menu_p
),
2240 mw
->menu
.old_depth
- 1);
2246 Up (w
, ev
, params
, num_params
)
2250 Cardinal
*num_params
;
2252 XlwMenuWidget mw
= (XlwMenuWidget
) w
;
2253 widget_value
* selected_item
= mw
->menu
.old_stack
[mw
->menu
.old_depth
- 1];
2254 int popup_menu_p
= mw
->menu
.top_depth
== 1;
2256 /* Inside top-level menu-bar? */
2257 if (mw
->menu
.old_depth
== mw
->menu
.top_depth
)
2259 /* FIXME: this is tricky. <up> in the menu-bar should select the
2260 last selectable item in the list. So we select the first
2261 selectable one and find the previous selectable item. Is there
2263 /* If this is a popup menu, skip title item of the popup. */
2265 find_first_selectable (mw
,
2266 selected_item
->contents
,
2268 mw
->menu
.old_depth
);
2270 selected_item
= mw
->menu
.old_stack
[mw
->menu
.old_depth
- 1];
2272 find_prev_selectable (mw
,
2275 mw
->menu
.old_depth
- 1);
2278 /* Highlight previous (enabled and not separator) menu item. */
2279 set_new_state (mw
, find_prev_selectable (mw
, selected_item
, popup_menu_p
),
2280 mw
->menu
.old_depth
- 1);
2286 Left (w
, ev
, params
, num_params
)
2290 Cardinal
*num_params
;
2292 XlwMenuWidget mw
= (XlwMenuWidget
) w
;
2293 widget_value
* selected_item
= mw
->menu
.old_stack
[mw
->menu
.old_depth
- 1];
2295 /* Inside top-level menu-bar? */
2296 if (mw
->menu
.old_depth
== mw
->menu
.top_depth
)
2297 /* When <left> in the menu-bar is pressed, display the previous item on
2298 the menu-bar. If the current item is the first one, highlight the
2299 last item in the menubar (probably Help). */
2300 set_new_state (mw
, find_prev_selectable (mw
, selected_item
, 0),
2301 mw
->menu
.old_depth
- 1);
2302 else if (mw
->menu
.old_depth
== 1
2303 && selected_item
->contents
) /* Is this menu item expandable? */
2305 set_new_state (mw
, selected_item
->contents
, mw
->menu
.old_depth
);
2307 selected_item
= mw
->menu
.old_stack
[mw
->menu
.old_depth
- 1];
2308 if (!selected_item
->enabled
&& find_first_selectable (mw
,
2311 set_new_state (mw
, find_first_selectable (mw
, selected_item
, 0),
2312 mw
->menu
.old_depth
- 1);
2317 pop_new_stack_if_no_contents (mw
);
2318 set_new_state (mw
, mw
->menu
.old_stack
[mw
->menu
.old_depth
- 2],
2319 mw
->menu
.old_depth
- 2);
2326 Right (w
, ev
, params
, num_params
)
2330 Cardinal
*num_params
;
2332 XlwMenuWidget mw
= (XlwMenuWidget
) w
;
2333 widget_value
* selected_item
= mw
->menu
.old_stack
[mw
->menu
.old_depth
- 1];
2335 /* Inside top-level menu-bar? */
2336 if (mw
->menu
.old_depth
== mw
->menu
.top_depth
)
2337 /* When <right> in the menu-bar is pressed, display the next item on
2338 the menu-bar. If the current item is the last one, highlight the
2339 first item (probably File). */
2340 set_new_state (mw
, find_next_selectable (mw
, selected_item
, 0),
2341 mw
->menu
.old_depth
- 1);
2342 else if (selected_item
->contents
) /* Is this menu item expandable? */
2344 set_new_state (mw
, selected_item
->contents
, mw
->menu
.old_depth
);
2346 selected_item
= mw
->menu
.old_stack
[mw
->menu
.old_depth
- 1];
2347 if (!selected_item
->enabled
&& find_first_selectable (mw
,
2350 set_new_state (mw
, find_first_selectable (mw
, selected_item
, 0),
2351 mw
->menu
.old_depth
- 1);
2355 pop_new_stack_if_no_contents (mw
);
2356 set_new_state (mw
, mw
->menu
.old_stack
[mw
->menu
.old_depth
- 2],
2357 mw
->menu
.old_depth
- 2);
2363 /* Handle key press and release events while menu is popped up.
2364 Our action is to get rid of the menu. */
2366 Key (w
, ev
, params
, num_params
)
2370 Cardinal
*num_params
;
2372 XlwMenuWidget mw
= (XlwMenuWidget
)w
;
2374 /* Pop down everything. */
2375 mw
->menu
.new_depth
= 1;
2378 if (mw
->menu
.popped_up
)
2380 mw
->menu
.popped_up
= False
;
2381 ungrab_all ((Widget
)mw
, ev
->xmotion
.time
);
2382 if (XtIsShell (XtParent ((Widget
) mw
)))
2383 XtPopdown (XtParent ((Widget
) mw
));
2386 XtRemoveGrab ((Widget
) mw
);
2387 display_menu (mw
, 0, False
, NULL
, NULL
, NULL
, NULL
, NULL
);
2392 XtCallCallbackList ((Widget
)mw
, mw
->menu
.select
, (XtPointer
)0);
2396 Select (w
, ev
, params
, num_params
)
2400 Cardinal
*num_params
;
2402 XlwMenuWidget mw
= (XlwMenuWidget
)w
;
2403 widget_value
* selected_item
= mw
->menu
.old_stack
[mw
->menu
.old_depth
- 1];
2405 /* If user releases the button quickly, without selecting anything,
2406 after the initial down-click that brought the menu up,
2408 if ((selected_item
== 0
2409 || ((widget_value
*) selected_item
)->call_data
== 0)
2410 && !next_release_must_exit
2411 && (ev
->xbutton
.time
- menu_post_event
.xbutton
.time
2412 < XtGetMultiClickTime (XtDisplay (w
))))
2415 /* pop down everything. */
2416 mw
->menu
.new_depth
= 1;
2419 if (mw
->menu
.popped_up
)
2421 mw
->menu
.popped_up
= False
;
2422 ungrab_all ((Widget
)mw
, ev
->xmotion
.time
);
2423 if (XtIsShell (XtParent ((Widget
) mw
)))
2424 XtPopdown (XtParent ((Widget
) mw
));
2427 XtRemoveGrab ((Widget
) mw
);
2428 display_menu (mw
, 0, False
, NULL
, NULL
, NULL
, NULL
, NULL
);
2433 XtCallCallbackList ((Widget
)mw
, mw
->menu
.select
, (XtPointer
)selected_item
);
2437 \f/* Special code to pop-up a menu */
2439 pop_up_menu (mw
, event
)
2441 XButtonPressedEvent
* event
;
2443 int x
= event
->x_root
;
2444 int y
= event
->y_root
;
2447 int borderwidth
= mw
->menu
.shadow_thickness
;
2448 Screen
* screen
= XtScreen (mw
);
2449 Display
*display
= XtDisplay (mw
);
2451 next_release_must_exit
= 0;
2453 mw
->menu
.inside_entry
= NULL
;
2454 XtCallCallbackList ((Widget
)mw
, mw
->menu
.open
, NULL
);
2456 if (XtIsShell (XtParent ((Widget
)mw
)))
2459 w
= mw
->menu
.windows
[0].width
;
2460 h
= mw
->menu
.windows
[0].height
;
2464 if (x
< borderwidth
)
2466 if (x
+ w
+ 2 * borderwidth
> WidthOfScreen (screen
))
2467 x
= WidthOfScreen (screen
) - w
- 2 * borderwidth
;
2468 if (y
< borderwidth
)
2470 if (y
+ h
+ 2 * borderwidth
> HeightOfScreen (screen
))
2471 y
= HeightOfScreen (screen
) - h
- 2 * borderwidth
;
2473 mw
->menu
.popped_up
= True
;
2474 if (XtIsShell (XtParent ((Widget
)mw
)))
2476 XtConfigureWidget (XtParent ((Widget
)mw
), x
, y
, w
, h
,
2477 XtParent ((Widget
)mw
)->core
.border_width
);
2478 XtPopup (XtParent ((Widget
)mw
), XtGrabExclusive
);
2479 display_menu (mw
, 0, False
, NULL
, NULL
, NULL
, NULL
, NULL
);
2480 mw
->menu
.windows
[0].x
= x
+ borderwidth
;
2481 mw
->menu
.windows
[0].y
= y
+ borderwidth
;
2482 mw
->menu
.top_depth
= 1; /* Popup menus don't have a bar so top is 1 */
2486 XEvent
*ev
= (XEvent
*) event
;
2488 XtAddGrab ((Widget
) mw
, True
, True
);
2490 /* notes the absolute position of the menubar window */
2491 mw
->menu
.windows
[0].x
= ev
->xmotion
.x_root
- ev
->xmotion
.x
;
2492 mw
->menu
.windows
[0].y
= ev
->xmotion
.y_root
- ev
->xmotion
.y
;
2493 mw
->menu
.top_depth
= 2;
2497 x_catch_errors (display
);
2499 if (XtGrabPointer ((Widget
)mw
, False
,
2501 | PointerMotionHintMask
2504 GrabModeAsync
, GrabModeAsync
, None
,
2505 mw
->menu
.cursor_shape
,
2506 event
->time
) == Success
)
2509 || XtGrabKeyboard ((Widget
)mw
, False
, GrabModeAsync
,
2510 GrabModeAsync
, event
->time
) == Success
)
2512 XtSetKeyboardFocus((Widget
)mw
, None
);
2513 pointer_grabbed
= 1;
2516 XtUngrabPointer ((Widget
)mw
, event
->time
);
2520 if (x_had_errors_p (display
))
2522 pointer_grabbed
= 0;
2523 XtUngrabPointer ((Widget
)mw
, event
->time
);
2525 x_uncatch_errors ();
2528 ((XMotionEvent
*)event
)->is_hint
= 0;
2529 handle_motion_event (mw
, (XMotionEvent
*)event
);
2532 /* arch-tag: 657f43dd-dfd0-4cc9-910c-52935f01176e
2533 (do not change this comment) */