1 /* Implements a lightweight menubar widget.
3 Copyright (C) 1992 Lucid, Inc.
4 Copyright (C) 1994-1995, 1997, 1999-2012 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 */
32 #include <sys/types.h>
33 #if (defined __sun) && !(defined SUNOS41)
40 #include <X11/IntrinsicP.h>
41 #include <X11/ObjectP.h>
42 #include <X11/StringDefs.h>
43 #include <X11/cursorfont.h>
44 #include <X11/Shell.h>
50 #include "bitmaps/gray.xbm"
54 #include <X11/bitmaps/gray>
56 #endif /* not emacs */
58 static int pointer_grabbed
;
59 static XEvent menu_post_event
;
62 xlwMenuTranslations
[] =
63 "<BtnDown>: start()\n\
66 <Key>Shift_L: nothing()\n\
67 <Key>Shift_R: nothing()\n\
68 <Key>Meta_L: nothing()\n\
69 <Key>Meta_R: nothing()\n\
70 <Key>Control_L: nothing()\n\
71 <Key>Control_R: nothing()\n\
72 <Key>Hyper_L: nothing()\n\
73 <Key>Hyper_R: nothing()\n\
74 <Key>Super_L: nothing()\n\
75 <Key>Super_R: nothing()\n\
76 <Key>Alt_L: nothing()\n\
77 <Key>Alt_R: nothing()\n\
78 <Key>Caps_Lock: nothing()\n\
79 <Key>Shift_Lock: nothing()\n\
80 <KeyUp>Shift_L: nothing()\n\
81 <KeyUp>Shift_R: nothing()\n\
82 <KeyUp>Meta_L: nothing()\n\
83 <KeyUp>Meta_R: nothing()\n\
84 <KeyUp>Control_L: nothing()\n\
85 <KeyUp>Control_R: nothing()\n\
86 <KeyUp>Hyper_L: nothing()\n\
87 <KeyUp>Hyper_R: nothing()\n\
88 <KeyUp>Super_L: nothing()\n\
89 <KeyUp>Super_R: nothing()\n\
90 <KeyUp>Alt_L: nothing()\n\
91 <KeyUp>Alt_R: nothing()\n\
92 <KeyUp>Caps_Lock: nothing()\n\
93 <KeyUp>Shift_Lock:nothing()\n\
94 <Key>Return: select()\n\
98 <Key>Right: right()\n\
103 /* FIXME: Space should toggle togglable menu item but not remove the menu
104 so you can toggle the next one without entering the menu again. */
106 /* FIXME: Should ESC close one level of menu structure or the complete menu? */
108 /* FIXME: F10 should enter the menu, the first one in the menu-bar. */
110 #define offset(field) XtOffset(XlwMenuWidget, field)
115 {XtNfontSet
, XtCFontSet
, XtRFontSet
, sizeof(XFontSet
),
116 offset(menu
.fontSet
), XtRFontSet
, NULL
},
119 #define DEFAULT_FONTNAME "Sans-10"
121 #define DEFAULT_FONTNAME "XtDefaultFont"
123 {XtNfont
, XtCFont
, XtRString
, sizeof(String
),
124 offset(menu
.fontName
), XtRString
, DEFAULT_FONTNAME
},
125 {XtNforeground
, XtCForeground
, XtRPixel
, sizeof(Pixel
),
126 offset(menu
.foreground
), XtRString
, "XtDefaultForeground"},
127 {XtNdisabledForeground
, XtCDisabledForeground
, XtRPixel
, sizeof(Pixel
),
128 offset(menu
.disabled_foreground
), XtRString
, (XtPointer
)NULL
},
129 {XtNbuttonForeground
, XtCButtonForeground
, XtRPixel
, sizeof(Pixel
),
130 offset(menu
.button_foreground
), XtRString
, "XtDefaultForeground"},
131 {XtNmargin
, XtCMargin
, XtRDimension
, sizeof(Dimension
),
132 offset(menu
.margin
), XtRImmediate
, (XtPointer
)1},
133 {XtNhorizontalSpacing
, XtCMargin
, XtRDimension
, sizeof(Dimension
),
134 offset(menu
.horizontal_spacing
), XtRImmediate
, (XtPointer
)3},
135 {XtNverticalSpacing
, XtCMargin
, XtRDimension
, sizeof(Dimension
),
136 offset(menu
.vertical_spacing
), XtRImmediate
, (XtPointer
)2},
137 {XtNarrowSpacing
, XtCMargin
, XtRDimension
, sizeof(Dimension
),
138 offset(menu
.arrow_spacing
), XtRImmediate
, (XtPointer
)10},
140 {XmNshadowThickness
, XmCShadowThickness
, XtRDimension
,
141 sizeof (Dimension
), offset (menu
.shadow_thickness
),
142 XtRImmediate
, (XtPointer
)1},
143 {XmNtopShadowColor
, XmCTopShadowColor
, XtRPixel
, sizeof (Pixel
),
144 offset (menu
.top_shadow_color
), XtRImmediate
, (XtPointer
)-1},
145 {XmNbottomShadowColor
, XmCBottomShadowColor
, XtRPixel
, sizeof (Pixel
),
146 offset (menu
.bottom_shadow_color
), XtRImmediate
, (XtPointer
)-1},
147 {XmNtopShadowPixmap
, XmCTopShadowPixmap
, XtRPixmap
, sizeof (Pixmap
),
148 offset (menu
.top_shadow_pixmap
), XtRImmediate
, (XtPointer
)None
},
149 {XmNbottomShadowPixmap
, XmCBottomShadowPixmap
, XtRPixmap
, sizeof (Pixmap
),
150 offset (menu
.bottom_shadow_pixmap
), XtRImmediate
, (XtPointer
)None
},
152 {XtNopen
, XtCCallback
, XtRCallback
, sizeof(XtPointer
),
153 offset(menu
.open
), XtRCallback
, (XtPointer
)NULL
},
154 {XtNselect
, XtCCallback
, XtRCallback
, sizeof(XtPointer
),
155 offset(menu
.select
), XtRCallback
, (XtPointer
)NULL
},
156 {XtNhighlightCallback
, XtCCallback
, XtRCallback
, sizeof(XtPointer
),
157 offset(menu
.highlight
), XtRCallback
, (XtPointer
)NULL
},
158 {XtNenterCallback
, XtCCallback
, XtRCallback
, sizeof(XtPointer
),
159 offset(menu
.enter
), XtRCallback
, (XtPointer
)NULL
},
160 {XtNleaveCallback
, XtCCallback
, XtRCallback
, sizeof(XtPointer
),
161 offset(menu
.leave
), XtRCallback
, (XtPointer
)NULL
},
162 {XtNmenu
, XtCMenu
, XtRPointer
, sizeof(XtPointer
),
163 offset(menu
.contents
), XtRImmediate
, (XtPointer
)NULL
},
164 {XtNcursor
, XtCCursor
, XtRCursor
, sizeof(Cursor
),
165 offset(menu
.cursor_shape
), XtRString
, (XtPointer
)"right_ptr"},
166 {XtNhorizontal
, XtCHorizontal
, XtRInt
, sizeof(int),
167 offset(menu
.horizontal
), XtRImmediate
, (XtPointer
)True
},
171 static Boolean
XlwMenuSetValues(Widget current
, Widget request
, Widget
new,
172 ArgList args
, Cardinal
*num_args
);
173 static void XlwMenuRealize(Widget
, Mask
*, XSetWindowAttributes
*);
174 static void XlwMenuResize(Widget w
);
175 static void XlwMenuInitialize(Widget
, Widget
, ArgList
, Cardinal
*);
176 static void XlwMenuRedisplay(Widget w
, XEvent
*ev
, Region region
);
177 static void XlwMenuDestroy(Widget w
);
178 static void XlwMenuClassInitialize(void);
179 static void Start(Widget w
, XEvent
*ev
, String
*params
, Cardinal
*num_params
);
180 static void Drag(Widget w
, XEvent
*ev
, String
*params
, Cardinal
*num_params
);
181 static void Down(Widget w
, XEvent
*ev
, String
*params
, Cardinal
*num_params
);
182 static void Up(Widget w
, XEvent
*ev
, String
*params
, Cardinal
*num_params
);
183 static void Left(Widget w
, XEvent
*ev
, String
*params
, Cardinal
*num_params
);
184 static void Right(Widget w
, XEvent
*ev
, String
*params
, Cardinal
*num_params
);
185 static void Select(Widget w
, XEvent
*ev
, String
*params
, Cardinal
*num_params
);
186 static void Key(Widget w
, XEvent
*ev
, String
*params
, Cardinal
*num_params
);
187 static void Nothing(Widget w
, XEvent
*ev
, String
*params
, Cardinal
*num_params
);
188 static int separator_height (enum menu_separator
);
189 static void pop_up_menu (XlwMenuWidget
, XButtonPressedEvent
*);
192 xlwMenuActionsList
[] =
202 {"MenuGadgetEscape", Key
}, /* Compatibility with Lesstif/Motif. */
203 {"nothing", Nothing
},
206 #define SuperClass ((CoreWidgetClass)&coreClassRec)
208 XlwMenuClassRec xlwMenuClassRec
=
210 { /* CoreClass fields initialization */
211 (WidgetClass
) SuperClass
, /* superclass */
212 "XlwMenu", /* class_name */
213 sizeof(XlwMenuRec
), /* size */
214 XlwMenuClassInitialize
, /* class_initialize */
215 NULL
, /* class_part_initialize */
216 FALSE
, /* class_inited */
217 XlwMenuInitialize
, /* initialize */
218 NULL
, /* initialize_hook */
219 XlwMenuRealize
, /* realize */
220 xlwMenuActionsList
, /* actions */
221 XtNumber(xlwMenuActionsList
), /* num_actions */
222 xlwMenuResources
, /* resources */
223 XtNumber(xlwMenuResources
), /* resource_count */
224 NULLQUARK
, /* xrm_class */
225 TRUE
, /* compress_motion */
226 XtExposeCompressMaximal
, /* compress_exposure */
227 TRUE
, /* compress_enterleave */
228 FALSE
, /* visible_interest */
229 XlwMenuDestroy
, /* destroy */
230 XlwMenuResize
, /* resize */
231 XlwMenuRedisplay
, /* expose */
232 XlwMenuSetValues
, /* set_values */
233 NULL
, /* set_values_hook */
234 XtInheritSetValuesAlmost
, /* set_values_almost */
235 NULL
, /* get_values_hook */
236 NULL
, /* accept_focus */
237 XtVersion
, /* version */
238 NULL
, /* callback_private */
239 xlwMenuTranslations
, /* tm_table */
240 XtInheritQueryGeometry
, /* query_geometry */
241 XtInheritDisplayAccelerator
, /* display_accelerator */
243 }, /* XlwMenuClass fields initialization */
249 WidgetClass xlwMenuWidgetClass
= (WidgetClass
) &xlwMenuClassRec
;
251 int submenu_destroyed
;
253 /* For debug, if installation-directory is non-nil this is not an installed
254 Emacs. In that case we do not grab the keyboard to make it easier to
256 #define GRAB_KEYBOARD (EQ (Vinstallation_directory, Qnil))
258 static int next_release_must_exit
;
262 /* Ungrab pointer and keyboard */
264 ungrab_all (Widget w
, Time ungrabtime
)
266 XtUngrabPointer (w
, ungrabtime
);
267 if (GRAB_KEYBOARD
) XtUngrabKeyboard (w
, ungrabtime
);
270 /* Like abort, but remove grabs from widget W before. */
272 static _Noreturn
void
273 abort_gracefully (Widget w
)
275 if (XtIsShell (XtParent (w
)))
277 ungrab_all (w
, CurrentTime
);
282 push_new_stack (XlwMenuWidget mw
, widget_value
*val
)
284 if (!mw
->menu
.new_stack
)
286 mw
->menu
.new_stack_length
= 10;
288 (widget_value
**)XtCalloc (mw
->menu
.new_stack_length
,
289 sizeof (widget_value
*));
291 else if (mw
->menu
.new_depth
== mw
->menu
.new_stack_length
)
293 mw
->menu
.new_stack_length
*= 2;
295 (widget_value
**)XtRealloc ((char*)mw
->menu
.new_stack
,
296 mw
->menu
.new_stack_length
* sizeof (widget_value
*));
298 mw
->menu
.new_stack
[mw
->menu
.new_depth
++] = val
;
302 pop_new_stack_if_no_contents (XlwMenuWidget mw
)
304 if (mw
->menu
.new_depth
> 1)
306 if (!mw
->menu
.new_stack
[mw
->menu
.new_depth
- 1]->contents
)
307 mw
->menu
.new_depth
-= 1;
312 make_old_stack_space (XlwMenuWidget mw
, int n
)
314 if (!mw
->menu
.old_stack
)
316 mw
->menu
.old_stack_length
= 10;
318 (widget_value
**)XtCalloc (mw
->menu
.old_stack_length
,
319 sizeof (widget_value
*));
321 else if (mw
->menu
.old_stack_length
< n
)
323 mw
->menu
.old_stack_length
*= 2;
325 (widget_value
**)XtRealloc ((char*)mw
->menu
.old_stack
,
326 mw
->menu
.old_stack_length
* sizeof (widget_value
*));
332 string_width (XlwMenuWidget mw
, char *s
)
337 if (mw
->menu
.xft_font
)
340 XftTextExtentsUtf8 (XtDisplay (mw
), mw
->menu
.xft_font
,
347 if (mw
->menu
.fontSet
)
349 XRectangle ink
, logical
;
350 XmbTextExtents (mw
->menu
.fontSet
, s
, strlen (s
), &ink
, &logical
);
351 return logical
.width
;
355 XTextExtents (mw
->menu
.font
, s
, strlen (s
), &drop
, &drop
, &drop
, &xcs
);
361 #define MENU_FONT_HEIGHT(mw) \
362 ((mw)->menu.xft_font != NULL \
363 ? (mw)->menu.xft_font->height \
364 : ((mw)->menu.fontSet != NULL \
365 ? (mw)->menu.font_extents->max_logical_extent.height \
366 : (mw)->menu.font->ascent + (mw)->menu.font->descent))
367 #define MENU_FONT_ASCENT(mw) \
368 ((mw)->menu.xft_font != NULL \
369 ? (mw)->menu.xft_font->ascent \
370 : ((mw)->menu.fontSet != NULL \
371 ? - (mw)->menu.font_extents->max_logical_extent.y \
372 : (mw)->menu.font->ascent))
375 #define MENU_FONT_HEIGHT(mw) \
376 ((mw)->menu.fontSet != NULL \
377 ? (mw)->menu.font_extents->max_logical_extent.height \
378 : (mw)->menu.font->ascent + (mw)->menu.font->descent)
379 #define MENU_FONT_ASCENT(mw) \
380 ((mw)->menu.fontSet != NULL \
381 ? - (mw)->menu.font_extents->max_logical_extent.y \
382 : (mw)->menu.font->ascent)
384 #define MENU_FONT_HEIGHT(mw) \
385 ((mw)->menu.font->ascent + (mw)->menu.font->descent)
386 #define MENU_FONT_ASCENT(mw) ((mw)->menu.font->ascent)
391 arrow_width (XlwMenuWidget mw
)
393 return (MENU_FONT_ASCENT (mw
) * 3/4) | 1;
396 /* Return the width of toggle buttons of widget MW. */
399 toggle_button_width (XlwMenuWidget mw
)
401 return (MENU_FONT_HEIGHT (mw
) * 2 / 3) | 1;
405 /* Return the width of radio buttons of widget MW. */
408 radio_button_width (XlwMenuWidget mw
)
410 return toggle_button_width (mw
) * 1.41;
417 {"labelString", "LabelString", XtRString
, sizeof(String
),
422 resource_widget_value (XlwMenuWidget mw
, widget_value
*val
)
424 if (!val
->toolkit_data
)
426 char* resourced_name
= NULL
;
428 XtGetSubresources ((Widget
) mw
,
429 (XtPointer
) &resourced_name
,
430 val
->name
, val
->name
,
431 nameResource
, 1, NULL
, 0);
433 resourced_name
= val
->name
;
436 complete_name
= (char *) XtMalloc (strlen (resourced_name
) + 1);
437 strcpy (complete_name
, resourced_name
);
441 int complete_length
=
442 strlen (resourced_name
) + strlen (val
->value
) + 2;
443 complete_name
= XtMalloc (complete_length
);
445 strcat (complete_name
, resourced_name
);
446 strcat (complete_name
, " ");
447 strcat (complete_name
, val
->value
);
450 val
->toolkit_data
= complete_name
;
451 val
->free_toolkit_data
= True
;
453 return (char*)val
->toolkit_data
;
456 /* Returns the sizes of an item */
458 size_menu_item (XlwMenuWidget mw
,
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 (XlwMenuWidget mw
, int level
)
510 int button_width
= 0;
511 int max_rest_width
= 0;
512 int max_button_width
= 0;
514 int horizontal_p
= mw
->menu
.horizontal
&& (level
== 0);
518 if (level
>= mw
->menu
.old_depth
)
519 abort_gracefully ((Widget
) mw
);
521 ws
= &mw
->menu
.windows
[level
];
525 ws
->button_width
= 0;
527 for (val
= mw
->menu
.old_stack
[level
]->contents
; val
; val
= val
->next
)
529 size_menu_item (mw
, val
, horizontal_p
, &label_width
, &rest_width
,
530 &button_width
, &height
);
533 ws
->width
+= label_width
+ rest_width
;
534 if (height
> ws
->height
)
539 if (label_width
> ws
->label_width
)
540 ws
->label_width
= label_width
;
541 if (rest_width
> max_rest_width
)
542 max_rest_width
= rest_width
;
543 if (button_width
> max_button_width
)
544 max_button_width
= button_width
;
545 ws
->height
+= height
;
550 ws
->label_width
= ws
->button_width
= 0;
553 ws
->width
= ws
->label_width
+ max_rest_width
+ max_button_width
;
554 ws
->button_width
= max_button_width
;
557 ws
->width
+= 2 * mw
->menu
.shadow_thickness
;
558 ws
->height
+= 2 * mw
->menu
.shadow_thickness
;
559 ws
->max_rest_width
= max_rest_width
;
563 ws
->width
+= 2 * mw
->menu
.margin
;
564 ws
->height
+= 2 * mw
->menu
.margin
;
572 draw_arrow (XlwMenuWidget mw
,
580 Display
*dpy
= XtDisplay (mw
);
581 GC top_gc
= mw
->menu
.shadow_top_gc
;
582 GC bottom_gc
= mw
->menu
.shadow_bottom_gc
;
583 int thickness
= mw
->menu
.shadow_thickness
;
586 /* alpha = atan (0.5)
587 factor = (1 + sin (alpha)) / cos (alpha) */
588 double factor
= 1.62;
589 int thickness2
= thickness
* factor
;
591 y
+= (MENU_FONT_HEIGHT (mw
) - height
) / 2;
602 pt
[0].y
= y
+ height
;
603 pt
[1].x
= x
+ thickness
;
604 pt
[1].y
= y
+ height
- thickness2
;
605 pt
[2].x
= x
+ thickness2
;
606 pt
[2].y
= y
+ thickness2
;
609 XFillPolygon (dpy
, window
, top_gc
, pt
, 4, Convex
, CoordModeOrigin
);
613 pt
[1].x
= x
+ thickness
;
614 pt
[1].y
= y
+ thickness2
;
615 pt
[2].x
= x
+ width
- thickness2
;
616 pt
[2].y
= y
+ height
/ 2;
618 pt
[3].y
= y
+ height
/ 2;
619 XFillPolygon (dpy
, window
, top_gc
, pt
, 4, Convex
, CoordModeOrigin
);
622 pt
[0].y
= y
+ height
;
623 pt
[1].x
= x
+ thickness
;
624 pt
[1].y
= y
+ height
- thickness2
;
625 pt
[2].x
= x
+ width
- thickness2
;
626 pt
[2].y
= y
+ height
/ 2;
628 pt
[3].y
= y
+ height
/ 2;
629 XFillPolygon (dpy
, window
, bottom_gc
, pt
, 4, Convex
, CoordModeOrigin
);
635 draw_shadow_rectangle (XlwMenuWidget mw
,
644 Display
*dpy
= XtDisplay (mw
);
645 GC top_gc
= !erase_p
? mw
->menu
.shadow_top_gc
: mw
->menu
.background_gc
;
646 GC bottom_gc
= !erase_p
? mw
->menu
.shadow_bottom_gc
: mw
->menu
.background_gc
;
647 int thickness
= mw
->menu
.shadow_thickness
;
650 if (!erase_p
&& down_p
)
660 points
[1].x
= x
+ width
;
662 points
[2].x
= x
+ width
- thickness
;
663 points
[2].y
= y
+ thickness
;
665 points
[3].y
= y
+ thickness
;
666 XFillPolygon (dpy
, window
, top_gc
, points
, 4, Convex
, CoordModeOrigin
);
668 points
[0].y
= y
+ thickness
;
670 points
[1].y
= y
+ height
;
671 points
[2].x
= x
+ thickness
;
672 points
[2].y
= y
+ height
- thickness
;
673 points
[3].x
= x
+ thickness
;
674 points
[3].y
= y
+ thickness
;
675 XFillPolygon (dpy
, window
, top_gc
, points
, 4, Convex
, CoordModeOrigin
);
676 points
[0].x
= x
+ width
;
678 points
[1].x
= x
+ width
- thickness
;
679 points
[1].y
= y
+ thickness
;
680 points
[2].x
= x
+ width
- thickness
;
681 points
[2].y
= y
+ height
- thickness
;
682 points
[3].x
= x
+ width
;
683 points
[3].y
= y
+ height
- thickness
;
684 XFillPolygon (dpy
, window
, bottom_gc
, points
, 4, Convex
, CoordModeOrigin
);
686 points
[0].y
= y
+ height
;
687 points
[1].x
= x
+ width
;
688 points
[1].y
= y
+ height
;
689 points
[2].x
= x
+ width
;
690 points
[2].y
= y
+ height
- thickness
;
691 points
[3].x
= x
+ thickness
;
692 points
[3].y
= y
+ height
- thickness
;
693 XFillPolygon (dpy
, window
, bottom_gc
, points
, 4, Convex
, CoordModeOrigin
);
698 draw_shadow_rhombus (XlwMenuWidget mw
,
707 Display
*dpy
= XtDisplay (mw
);
708 GC top_gc
= !erase_p
? mw
->menu
.shadow_top_gc
: mw
->menu
.background_gc
;
709 GC bottom_gc
= !erase_p
? mw
->menu
.shadow_bottom_gc
: mw
->menu
.background_gc
;
710 int thickness
= mw
->menu
.shadow_thickness
;
713 if (!erase_p
&& down_p
)
722 points
[0].y
= y
+ height
/ 2;
723 points
[1].x
= x
+ thickness
;
724 points
[1].y
= y
+ height
/ 2;
725 points
[2].x
= x
+ width
/ 2;
726 points
[2].y
= y
+ thickness
;
727 points
[3].x
= x
+ width
/ 2;
729 XFillPolygon (dpy
, window
, top_gc
, points
, 4, Convex
, CoordModeOrigin
);
730 points
[0].x
= x
+ width
/ 2;
732 points
[1].x
= x
+ width
/ 2;
733 points
[1].y
= y
+ thickness
;
734 points
[2].x
= x
+ width
- thickness
;
735 points
[2].y
= y
+ height
/ 2;
736 points
[3].x
= x
+ width
;
737 points
[3].y
= y
+ height
/ 2;
738 XFillPolygon (dpy
, window
, top_gc
, points
, 4, Convex
, CoordModeOrigin
);
740 points
[0].y
= y
+ height
/ 2;
741 points
[1].x
= x
+ thickness
;
742 points
[1].y
= y
+ height
/ 2;
743 points
[2].x
= x
+ width
/ 2;
744 points
[2].y
= y
+ height
- thickness
;
745 points
[3].x
= x
+ width
/ 2;
746 points
[3].y
= y
+ height
;
747 XFillPolygon (dpy
, window
, bottom_gc
, points
, 4, Convex
, CoordModeOrigin
);
748 points
[0].x
= x
+ width
/ 2;
749 points
[0].y
= y
+ height
;
750 points
[1].x
= x
+ width
/ 2;
751 points
[1].y
= y
+ height
- thickness
;
752 points
[2].x
= x
+ width
- thickness
;
753 points
[2].y
= y
+ height
/ 2;
754 points
[3].x
= x
+ width
;
755 points
[3].y
= y
+ height
/ 2;
756 XFillPolygon (dpy
, window
, bottom_gc
, points
, 4, Convex
, CoordModeOrigin
);
760 /* Draw a toggle button on widget MW, X window WINDOW. X/Y is the
761 top-left corner of the menu item. SELECTED_P non-zero means the
762 toggle button is selected. */
765 draw_toggle (XlwMenuWidget mw
, Window window
, int x
, int y
, int selected_p
)
769 width
= toggle_button_width (mw
);
771 x
+= mw
->menu
.horizontal_spacing
;
772 y
+= (MENU_FONT_ASCENT (mw
) - height
) / 2;
773 draw_shadow_rectangle (mw
, window
, x
, y
, width
, height
, False
, selected_p
);
777 /* Draw a radio button on widget MW, X window WINDOW. X/Y is the
778 top-left corner of the menu item. SELECTED_P non-zero means the
779 toggle button is selected. */
782 draw_radio (XlwMenuWidget mw
, Window window
, int x
, int y
, int selected_p
)
786 width
= radio_button_width (mw
);
788 x
+= mw
->menu
.horizontal_spacing
;
789 y
+= (MENU_FONT_ASCENT (mw
) - height
) / 2;
790 draw_shadow_rhombus (mw
, window
, x
, y
, width
, height
, False
, selected_p
);
794 /* Draw a menu separator on widget MW, X window WINDOW. X/Y is the
795 top-left corner of the menu item. WIDTH is the width of the
796 separator to draw. TYPE is the separator type. */
799 draw_separator (XlwMenuWidget mw
,
804 enum menu_separator type
)
806 Display
*dpy
= XtDisplay (mw
);
811 case SEPARATOR_NO_LINE
:
814 case SEPARATOR_SINGLE_LINE
:
815 XDrawLine (dpy
, window
, mw
->menu
.foreground_gc
,
819 case SEPARATOR_DOUBLE_LINE
:
820 draw_separator (mw
, window
, x
, y
, width
, SEPARATOR_SINGLE_LINE
);
821 draw_separator (mw
, window
, x
, y
+ 2, width
, SEPARATOR_SINGLE_LINE
);
824 case SEPARATOR_SINGLE_DASHED_LINE
:
825 xgcv
.line_style
= LineOnOffDash
;
826 XChangeGC (dpy
, mw
->menu
.foreground_gc
, GCLineStyle
, &xgcv
);
827 XDrawLine (dpy
, window
, mw
->menu
.foreground_gc
,
829 xgcv
.line_style
= LineSolid
;
830 XChangeGC (dpy
, mw
->menu
.foreground_gc
, GCLineStyle
, &xgcv
);
833 case SEPARATOR_DOUBLE_DASHED_LINE
:
834 draw_separator (mw
, window
, x
, y
, width
,
835 SEPARATOR_SINGLE_DASHED_LINE
);
836 draw_separator (mw
, window
, x
, y
+ 2, width
,
837 SEPARATOR_SINGLE_DASHED_LINE
);
840 case SEPARATOR_SHADOW_ETCHED_IN
:
841 XDrawLine (dpy
, window
, mw
->menu
.shadow_bottom_gc
,
843 XDrawLine (dpy
, window
, mw
->menu
.shadow_top_gc
,
844 x
, y
+ 1, x
+ width
, y
+ 1);
847 case SEPARATOR_SHADOW_ETCHED_OUT
:
848 XDrawLine (dpy
, window
, mw
->menu
.shadow_top_gc
,
850 XDrawLine (dpy
, window
, mw
->menu
.shadow_bottom_gc
,
851 x
, y
+ 1, x
+ width
, y
+ 1);
854 case SEPARATOR_SHADOW_ETCHED_IN_DASH
:
855 xgcv
.line_style
= LineOnOffDash
;
856 XChangeGC (dpy
, mw
->menu
.shadow_bottom_gc
, GCLineStyle
, &xgcv
);
857 XChangeGC (dpy
, mw
->menu
.shadow_top_gc
, GCLineStyle
, &xgcv
);
858 draw_separator (mw
, window
, x
, y
, width
, SEPARATOR_SHADOW_ETCHED_IN
);
859 xgcv
.line_style
= LineSolid
;
860 XChangeGC (dpy
, mw
->menu
.shadow_bottom_gc
, GCLineStyle
, &xgcv
);
861 XChangeGC (dpy
, mw
->menu
.shadow_top_gc
, GCLineStyle
, &xgcv
);
864 case SEPARATOR_SHADOW_ETCHED_OUT_DASH
:
865 xgcv
.line_style
= LineOnOffDash
;
866 XChangeGC (dpy
, mw
->menu
.shadow_bottom_gc
, GCLineStyle
, &xgcv
);
867 XChangeGC (dpy
, mw
->menu
.shadow_top_gc
, GCLineStyle
, &xgcv
);
868 draw_separator (mw
, window
, x
, y
, width
, SEPARATOR_SHADOW_ETCHED_OUT
);
869 xgcv
.line_style
= LineSolid
;
870 XChangeGC (dpy
, mw
->menu
.shadow_bottom_gc
, GCLineStyle
, &xgcv
);
871 XChangeGC (dpy
, mw
->menu
.shadow_top_gc
, GCLineStyle
, &xgcv
);
874 case SEPARATOR_SHADOW_DOUBLE_ETCHED_IN
:
875 draw_separator (mw
, window
, x
, y
, width
, SEPARATOR_SHADOW_ETCHED_IN
);
876 draw_separator (mw
, window
, x
, y
+ 3, width
, SEPARATOR_SHADOW_ETCHED_IN
);
879 case SEPARATOR_SHADOW_DOUBLE_ETCHED_OUT
:
880 draw_separator (mw
, window
, x
, y
, width
,
881 SEPARATOR_SHADOW_ETCHED_OUT
);
882 draw_separator (mw
, window
, x
, y
+ 3, width
,
883 SEPARATOR_SHADOW_ETCHED_OUT
);
886 case SEPARATOR_SHADOW_DOUBLE_ETCHED_IN_DASH
:
887 xgcv
.line_style
= LineOnOffDash
;
888 XChangeGC (dpy
, mw
->menu
.shadow_bottom_gc
, GCLineStyle
, &xgcv
);
889 XChangeGC (dpy
, mw
->menu
.shadow_top_gc
, GCLineStyle
, &xgcv
);
890 draw_separator (mw
, window
, x
, y
, width
,
891 SEPARATOR_SHADOW_DOUBLE_ETCHED_IN
);
892 xgcv
.line_style
= LineSolid
;
893 XChangeGC (dpy
, mw
->menu
.shadow_bottom_gc
, GCLineStyle
, &xgcv
);
894 XChangeGC (dpy
, mw
->menu
.shadow_top_gc
, GCLineStyle
, &xgcv
);
897 case SEPARATOR_SHADOW_DOUBLE_ETCHED_OUT_DASH
:
898 xgcv
.line_style
= LineOnOffDash
;
899 XChangeGC (dpy
, mw
->menu
.shadow_bottom_gc
, GCLineStyle
, &xgcv
);
900 XChangeGC (dpy
, mw
->menu
.shadow_top_gc
, GCLineStyle
, &xgcv
);
901 draw_separator (mw
, window
, x
, y
, width
,
902 SEPARATOR_SHADOW_DOUBLE_ETCHED_OUT
);
903 xgcv
.line_style
= LineSolid
;
904 XChangeGC (dpy
, mw
->menu
.shadow_bottom_gc
, GCLineStyle
, &xgcv
);
905 XChangeGC (dpy
, mw
->menu
.shadow_top_gc
, GCLineStyle
, &xgcv
);
914 /* Return the pixel height of menu separator SEPARATOR. */
917 separator_height (enum menu_separator separator
)
921 case SEPARATOR_NO_LINE
:
924 case SEPARATOR_SINGLE_LINE
:
925 case SEPARATOR_SINGLE_DASHED_LINE
:
928 case SEPARATOR_DOUBLE_LINE
:
929 case SEPARATOR_DOUBLE_DASHED_LINE
:
932 case SEPARATOR_SHADOW_ETCHED_IN
:
933 case SEPARATOR_SHADOW_ETCHED_OUT
:
934 case SEPARATOR_SHADOW_ETCHED_IN_DASH
:
935 case SEPARATOR_SHADOW_ETCHED_OUT_DASH
:
938 case SEPARATOR_SHADOW_DOUBLE_ETCHED_IN
:
939 case SEPARATOR_SHADOW_DOUBLE_ETCHED_OUT
:
940 case SEPARATOR_SHADOW_DOUBLE_ETCHED_IN_DASH
:
941 case SEPARATOR_SHADOW_DOUBLE_ETCHED_OUT_DASH
:
950 /* Display the menu item and increment where.x and where.y to show how large
951 the menu item was. */
954 display_menu_item (XlwMenuWidget mw
,
958 Boolean highlighted_p
,
959 Boolean horizontal_p
,
960 Boolean just_compute_p
)
964 int font_height
= MENU_FONT_HEIGHT (mw
);
965 int font_ascent
= MENU_FONT_ASCENT (mw
);
966 int shadow
= mw
->menu
.shadow_thickness
;
967 int margin
= mw
->menu
.margin
;
968 int h_spacing
= mw
->menu
.horizontal_spacing
;
969 int v_spacing
= mw
->menu
.vertical_spacing
;
975 enum menu_separator separator
;
976 int separator_p
= lw_separator_p (val
->name
, &separator
, 0);
981 /* compute the sizes of the item */
982 size_menu_item (mw
, val
, horizontal_p
, &label_width
, &rest_width
,
983 &button_width
, &height
);
986 width
= label_width
+ rest_width
;
989 label_width
= ws
->label_width
;
990 width
= ws
->width
- 2 * shadow
;
993 /* Only highlight an enabled item that has a callback. */
995 if (!val
->enabled
|| !(val
->call_data
|| val
->contents
))
998 /* do the drawing. */
1001 /* Add the shadow border of the containing menu */
1002 int x
= where
->x
+ shadow
;
1003 int y
= where
->y
+ shadow
;
1011 /* pick the foreground and background GC. */
1013 text_gc
= mw
->menu
.foreground_gc
;
1015 text_gc
= mw
->menu
.disabled_gc
;
1016 deco_gc
= mw
->menu
.foreground_gc
;
1018 xftfg
= val
->enabled
? &mw
->menu
.xft_fg
: &mw
->menu
.xft_disabled_fg
;
1023 draw_separator (mw
, ws
->pixmap
, x
, y
, width
, separator
);
1027 int x_offset
= x
+ h_spacing
+ shadow
;
1028 char* display_string
= resource_widget_value (mw
, val
);
1029 draw_shadow_rectangle (mw
, ws
->pixmap
, x
, y
, width
, height
, True
,
1032 /* Deal with centering a menu title. */
1033 if (!horizontal_p
&& !val
->contents
&& !val
->call_data
)
1035 int l
= string_width (mw
, display_string
);
1038 x_offset
= (width
- l
) >> 1;
1040 else if (!horizontal_p
&& ws
->button_width
)
1041 x_offset
+= ws
->button_width
;
1047 int draw_y
= y
+ v_spacing
+ shadow
;
1048 XftDrawStringUtf8 (ws
->xft_draw
, xftfg
,
1050 x_offset
, draw_y
+ font_ascent
,
1051 (unsigned char *) display_string
,
1052 strlen (display_string
));
1057 if (mw
->menu
.fontSet
)
1058 XmbDrawString (XtDisplay (mw
), ws
->pixmap
, mw
->menu
.fontSet
,
1060 y
+ v_spacing
+ shadow
+ font_ascent
,
1061 display_string
, strlen (display_string
));
1064 XDrawString (XtDisplay (mw
), ws
->pixmap
,
1066 y
+ v_spacing
+ shadow
+ font_ascent
,
1067 display_string
, strlen (display_string
));
1071 if (val
->button_type
== BUTTON_TYPE_TOGGLE
)
1072 draw_toggle (mw
, ws
->pixmap
, x
, y
+ v_spacing
+ shadow
,
1074 else if (val
->button_type
== BUTTON_TYPE_RADIO
)
1075 draw_radio (mw
, ws
->pixmap
, x
, y
+ v_spacing
+ shadow
,
1080 int a_w
= arrow_width (mw
);
1081 draw_arrow (mw
, ws
->pixmap
, deco_gc
,
1083 - mw
->menu
.horizontal_spacing
1084 - mw
->menu
.shadow_thickness
,
1085 y
+ v_spacing
+ shadow
, a_w
,
1093 int draw_x
= ws
->width
- ws
->max_rest_width
1094 + mw
->menu
.arrow_spacing
;
1095 int draw_y
= y
+ v_spacing
+ shadow
+ font_ascent
;
1096 XftDrawStringUtf8 (ws
->xft_draw
, xftfg
,
1099 (unsigned char *) val
->key
,
1105 if (mw
->menu
.fontSet
)
1106 XmbDrawString (XtDisplay (mw
), ws
->pixmap
,
1109 x
+ label_width
+ mw
->menu
.arrow_spacing
,
1110 y
+ v_spacing
+ shadow
+ font_ascent
,
1111 val
->key
, strlen (val
->key
));
1114 XDrawString (XtDisplay (mw
), ws
->pixmap
,
1116 x
+ label_width
+ mw
->menu
.arrow_spacing
,
1117 y
+ v_spacing
+ shadow
+ font_ascent
,
1118 val
->key
, strlen (val
->key
));
1123 XDrawRectangle (XtDisplay (mw
), ws
->pixmap
,
1124 mw
->menu
.background_gc
,
1125 x
+ shadow
, y
+ shadow
,
1126 label_width
+ h_spacing
- 1,
1127 font_height
+ 2 * v_spacing
- 1);
1128 draw_shadow_rectangle (mw
, ws
->pixmap
, x
, y
, width
, height
,
1133 draw_shadow_rectangle (mw
, ws
->pixmap
, x
, y
, width
, height
, False
,
1143 display_menu (XlwMenuWidget mw
,
1145 Boolean just_compute_p
,
1146 XPoint
*highlighted_pos
,
1148 widget_value
**hit_return
)
1151 widget_value
* following_item
;
1154 int horizontal_p
= mw
->menu
.horizontal
&& (level
== 0);
1157 enum menu_separator separator
;
1159 if (level
>= mw
->menu
.old_depth
)
1160 abort_gracefully ((Widget
) mw
);
1162 if (level
< mw
->menu
.old_depth
- 1)
1163 following_item
= mw
->menu
.old_stack
[level
+ 1];
1165 following_item
= NULL
;
1173 ws
= &mw
->menu
.windows
[level
];
1175 if (!just_compute_p
)
1176 XFillRectangle (XtDisplay (mw
), ws
->pixmap
, mw
->menu
.background_gc
,
1177 0, 0, ws
->width
, ws
->height
);
1179 for (val
= mw
->menu
.old_stack
[level
]->contents
; val
; val
= val
->next
)
1181 highlighted_p
= val
== following_item
;
1182 if (highlighted_p
&& highlighted_pos
)
1185 highlighted_pos
->x
= where
.x
;
1187 highlighted_pos
->y
= where
.y
;
1190 display_menu_item (mw
, val
, ws
, &where
, highlighted_p
, horizontal_p
,
1193 if (highlighted_p
&& highlighted_pos
)
1196 highlighted_pos
->y
= where
.y
;
1198 highlighted_pos
->x
= where
.x
;
1203 && (horizontal_p
? hit
->x
< where
.x
: hit
->y
< where
.y
)
1204 && !lw_separator_p (val
->name
, &separator
, 0)
1211 if (mw
->menu
.inside_entry
!= val
)
1213 if (mw
->menu
.inside_entry
)
1214 XtCallCallbackList ((Widget
)mw
, mw
->menu
.leave
,
1215 (XtPointer
) mw
->menu
.inside_entry
);
1216 mw
->menu
.inside_entry
= val
;
1217 XtCallCallbackList ((Widget
)mw
, mw
->menu
.enter
,
1218 (XtPointer
) mw
->menu
.inside_entry
);
1228 if (!just_compute_p
)
1230 draw_shadow_rectangle (mw
, ws
->pixmap
, 0, 0, ws
->width
, ws
->height
,
1232 XCopyArea (XtDisplay (mw
), ws
->pixmap
, ws
->window
,
1233 mw
->menu
.foreground_gc
, 0, 0, ws
->width
, ws
->height
, 0, 0);
1239 set_new_state (XlwMenuWidget mw
, widget_value
*val
, int level
)
1243 mw
->menu
.new_depth
= 0;
1244 for (i
= 0; i
< level
; i
++)
1245 push_new_stack (mw
, mw
->menu
.old_stack
[i
]);
1246 push_new_stack (mw
, val
);
1250 expose_cb (Widget widget
,
1253 Boolean
* continue_to_dispatch
)
1255 XlwMenuWidget mw
= (XlwMenuWidget
) closure
;
1258 *continue_to_dispatch
= False
;
1259 for (i
= 0; i
< mw
->menu
.windows_length
; ++i
)
1260 if (mw
->menu
.windows
[i
].w
== widget
) break;
1261 if (i
< mw
->menu
.windows_length
&& i
< mw
->menu
.old_depth
)
1262 display_menu (mw
, i
, False
, NULL
, NULL
, NULL
);
1266 set_window_type (Widget w
, XlwMenuWidget mw
)
1268 int popup_menu_p
= mw
->menu
.top_depth
== 1;
1269 Atom type
= XInternAtom (XtDisplay (w
),
1271 ? "_NET_WM_WINDOW_TYPE_POPUP_MENU"
1272 : "_NET_WM_WINDOW_TYPE_DROPDOWN_MENU",
1275 XChangeProperty (XtDisplay (w
), XtWindow (w
),
1276 XInternAtom (XtDisplay (w
), "_NET_WM_WINDOW_TYPE", False
),
1277 XA_ATOM
, 32, PropModeReplace
,
1278 (unsigned char *)&type
, 1);
1283 make_windows_if_needed (XlwMenuWidget mw
, int n
)
1287 window_state
* windows
;
1289 if (mw
->menu
.windows_length
>= n
)
1292 if (!mw
->menu
.windows
)
1295 (window_state
*)XtMalloc (n
* sizeof (window_state
));
1301 (window_state
*)XtRealloc ((char*)mw
->menu
.windows
,
1302 n
* sizeof (window_state
));
1303 start_at
= mw
->menu
.windows_length
;
1305 mw
->menu
.windows_length
= n
;
1307 windows
= mw
->menu
.windows
;
1309 for (i
= start_at
; i
< n
; i
++)
1315 windows
[i
].width
= 1;
1316 windows
[i
].height
= 1;
1317 windows
[i
].max_rest_width
= 0;
1318 XtSetArg (av
[ac
], XtNwidth
, 1); ++ac
;
1319 XtSetArg (av
[ac
], XtNheight
, 1); ++ac
;
1320 XtSetArg (av
[ac
], XtNsaveUnder
, True
); ++ac
;
1321 XtSetArg (av
[ac
], XtNbackground
, mw
->core
.background_pixel
); ++ac
;
1322 XtSetArg (av
[ac
], XtNborderColor
, mw
->core
.border_pixel
); ++ac
;
1323 XtSetArg (av
[ac
], XtNborderWidth
, mw
->core
.border_width
); ++ac
;
1324 XtSetArg (av
[ac
], XtNcursor
, mw
->menu
.cursor_shape
); ++ac
;
1326 XtCreatePopupShell ("sub", overrideShellWidgetClass
,
1327 (Widget
) mw
, av
, ac
);
1328 XtRealizeWidget (windows
[i
].w
);
1329 XtAddEventHandler (windows
[i
].w
, ExposureMask
, False
, expose_cb
, mw
);
1330 windows
[i
].window
= XtWindow (windows
[i
].w
);
1331 windows
[i
].pixmap
= None
;
1333 windows
[i
].xft_draw
= 0;
1335 set_window_type (windows
[i
].w
, mw
);
1337 XFlush (XtDisplay (mw
));
1340 /* Value is non-zero if WINDOW is part of menu bar widget W. */
1343 xlwmenu_window_p (Widget w
, Window window
)
1345 XlwMenuWidget mw
= (XlwMenuWidget
) w
;
1348 for (i
= 0; i
< mw
->menu
.windows_length
; ++i
)
1349 if (window
== mw
->menu
.windows
[i
].window
)
1352 return i
< mw
->menu
.windows_length
;
1355 /* Make the window fit in the screen */
1357 fit_to_screen (XlwMenuWidget mw
,
1359 window_state
*previous_ws
,
1360 Boolean horizontal_p
)
1362 unsigned int screen_width
= WidthOfScreen (XtScreen (mw
));
1363 unsigned int screen_height
= HeightOfScreen (XtScreen (mw
));
1364 /* 1 if we are unable to avoid an overlap between
1365 this menu and the parent menu in the X dimension. */
1366 int horizontal_overlap
= 0;
1370 else if (ws
->x
+ ws
->width
> screen_width
)
1373 /* The addition of shadow-thickness for a sub-menu's position is
1374 to reflect a similar adjustment when the menu is displayed to
1375 the right of the invoking menu-item; it makes the sub-menu
1376 look more `attached' to the menu-item. */
1377 ws
->x
= previous_ws
->x
- ws
->width
+ mw
->menu
.shadow_thickness
;
1379 ws
->x
= screen_width
- ws
->width
;
1383 horizontal_overlap
= 1;
1386 /* If we overlap in X, try to avoid overlap in Y. */
1387 if (horizontal_overlap
1388 && ws
->y
< previous_ws
->y
+ previous_ws
->height
1389 && previous_ws
->y
< ws
->y
+ ws
->height
)
1391 /* Put this menu right below or right above PREVIOUS_WS
1393 if (previous_ws
->y
+ previous_ws
->height
+ ws
->height
< screen_height
)
1394 ws
->y
= previous_ws
->y
+ previous_ws
->height
;
1395 else if (previous_ws
->y
- ws
->height
> 0)
1396 ws
->y
= previous_ws
->y
- ws
->height
;
1401 else if (ws
->y
+ ws
->height
> screen_height
)
1404 ws
->y
= previous_ws
->y
- ws
->height
;
1406 ws
->y
= screen_height
- ws
->height
;
1413 create_pixmap_for_menu (window_state
* ws
, XlwMenuWidget mw
)
1415 if (ws
->pixmap
!= None
)
1417 XFreePixmap (XtDisplay (ws
->w
), ws
->pixmap
);
1420 ws
->pixmap
= XCreatePixmap (XtDisplay (ws
->w
), ws
->window
,
1421 ws
->width
, ws
->height
,
1422 DefaultDepthOfScreen (XtScreen (ws
->w
)));
1425 XftDrawDestroy (ws
->xft_draw
);
1426 if (mw
->menu
.xft_font
)
1428 int screen
= XScreenNumberOfScreen (mw
->core
.screen
);
1429 ws
->xft_draw
= XftDrawCreate (XtDisplay (ws
->w
),
1431 DefaultVisual (XtDisplay (ws
->w
), screen
),
1439 /* Updates old_stack from new_stack and redisplays. */
1441 remap_menubar (XlwMenuWidget mw
)
1445 XPoint selection_position
;
1446 int old_depth
= mw
->menu
.old_depth
;
1447 int new_depth
= mw
->menu
.new_depth
;
1448 widget_value
** old_stack
;
1449 widget_value
** new_stack
;
1450 window_state
* windows
;
1451 widget_value
* old_selection
;
1452 widget_value
* new_selection
;
1454 /* Check that enough windows and old_stack are ready. */
1455 make_windows_if_needed (mw
, new_depth
);
1456 make_old_stack_space (mw
, new_depth
);
1457 windows
= mw
->menu
.windows
;
1458 old_stack
= mw
->menu
.old_stack
;
1459 new_stack
= mw
->menu
.new_stack
;
1461 /* compute the last identical different entry */
1462 for (i
= 1; i
< old_depth
&& i
< new_depth
; i
++)
1463 if (old_stack
[i
] != new_stack
[i
])
1467 /* Memorize the previously selected item to be able to refresh it */
1468 old_selection
= last_same
+ 1 < old_depth
? old_stack
[last_same
+ 1] : NULL
;
1469 if (old_selection
&& !old_selection
->enabled
)
1470 old_selection
= NULL
;
1471 new_selection
= last_same
+ 1 < new_depth
? new_stack
[last_same
+ 1] : NULL
;
1472 if (new_selection
&& !new_selection
->enabled
)
1473 new_selection
= NULL
;
1475 /* Call callback when the highlighted item changes. */
1476 if (old_selection
|| new_selection
)
1477 XtCallCallbackList ((Widget
)mw
, mw
->menu
.highlight
,
1478 (XtPointer
) new_selection
);
1480 /* updates old_state from new_state. It has to be done now because
1481 display_menu (called below) uses the old_stack to know what to display. */
1482 for (i
= last_same
+ 1; i
< new_depth
; i
++)
1484 XtPopdown (mw
->menu
.windows
[i
].w
);
1485 old_stack
[i
] = new_stack
[i
];
1487 mw
->menu
.old_depth
= new_depth
;
1489 /* refresh the last selection */
1490 selection_position
.x
= 0;
1491 selection_position
.y
= 0;
1492 display_menu (mw
, last_same
, new_selection
== old_selection
,
1493 &selection_position
, NULL
, NULL
);
1495 /* Now place the new menus. */
1496 for (i
= last_same
+ 1; i
< new_depth
&& new_stack
[i
]->contents
; i
++)
1498 window_state
*previous_ws
= &windows
[i
- 1];
1499 window_state
*ws
= &windows
[i
];
1501 ws
->x
= (previous_ws
->x
+ selection_position
.x
1502 + mw
->menu
.shadow_thickness
);
1503 if (mw
->menu
.horizontal
&& i
== 1)
1504 ws
->x
+= mw
->menu
.margin
;
1507 if (!mw
->menu
.horizontal
|| i
> 1)
1508 ws
->x
+= mw
->menu
.shadow_thickness
;
1511 ws
->y
= (previous_ws
->y
+ selection_position
.y
1512 + mw
->menu
.shadow_thickness
);
1513 if (mw
->menu
.horizontal
&& i
== 1)
1514 ws
->y
+= mw
->menu
.margin
;
1518 fit_to_screen (mw
, ws
, previous_ws
, mw
->menu
.horizontal
&& i
== 1);
1520 create_pixmap_for_menu (ws
, mw
);
1521 XtMoveWidget (ws
->w
, ws
->x
, ws
->y
);
1522 XtPopup (ws
->w
, XtGrabNone
);
1523 XtResizeWidget (ws
->w
, ws
->width
, ws
->height
,
1524 mw
->core
.border_width
);
1525 XtResizeWindow (ws
->w
);
1526 display_menu (mw
, i
, False
, &selection_position
, NULL
, NULL
);
1529 /* unmap the menus that popped down */
1530 for (i
= new_depth
- 1; i
< old_depth
; i
++)
1531 if (i
>= new_depth
|| (i
> 0 && !new_stack
[i
]->contents
))
1532 XtPopdown (windows
[i
].w
);
1536 motion_event_is_in_menu (XlwMenuWidget mw
,
1539 XPoint
*relative_pos
)
1541 window_state
* ws
= &mw
->menu
.windows
[level
];
1542 int shadow
= level
== 0 ? 0 : mw
->menu
.shadow_thickness
;
1543 int x
= ws
->x
+ shadow
;
1544 int y
= ws
->y
+ shadow
;
1545 relative_pos
->x
= ev
->x_root
- x
;
1546 relative_pos
->y
= ev
->y_root
- y
;
1547 return (x
- shadow
< ev
->x_root
&& ev
->x_root
< x
+ ws
->width
1548 && y
- shadow
< ev
->y_root
&& ev
->y_root
< y
+ ws
->height
);
1552 map_event_to_widget_value (XlwMenuWidget mw
,
1558 XPoint relative_pos
;
1564 /* Find the window */
1565 for (i
= mw
->menu
.old_depth
- 1; i
>= 0; i
--)
1567 ws
= &mw
->menu
.windows
[i
];
1568 if (ws
&& motion_event_is_in_menu (mw
, ev
, i
, &relative_pos
))
1571 display_menu (mw
, i
, True
, NULL
, &relative_pos
, val
);
1583 if (mw
->menu
.inside_entry
!= NULL
)
1584 XtCallCallbackList ((Widget
)mw
, mw
->menu
.leave
,
1585 (XtPointer
) mw
->menu
.inside_entry
);
1586 mw
->menu
.inside_entry
= NULL
;
1594 make_drawing_gcs (XlwMenuWidget mw
)
1598 XtGCMask mask
= GCForeground
| GCBackground
;
1601 if (!mw
->menu
.fontSet
&& mw
->menu
.font
)
1603 xgcv
.font
= mw
->menu
.font
->fid
;
1609 xgcv
.font
= mw
->menu
.font
->fid
;
1613 xgcv
.foreground
= mw
->menu
.foreground
;
1614 xgcv
.background
= mw
->core
.background_pixel
;
1615 mw
->menu
.foreground_gc
= XtGetGC ((Widget
)mw
, mask
, &xgcv
);
1617 xgcv
.foreground
= mw
->menu
.button_foreground
;
1618 mw
->menu
.button_gc
= XtGetGC ((Widget
)mw
, mask
, &xgcv
);
1620 xgcv
.background
= mw
->core
.background_pixel
;
1622 #define BRIGHTNESS(color) (((color) & 0xff) + (((color) >> 8) & 0xff) + (((color) >> 16) & 0xff))
1624 /* Allocate color for disabled menu-items. */
1625 mw
->menu
.disabled_foreground
= mw
->menu
.foreground
;
1626 if (BRIGHTNESS(mw
->menu
.foreground
) < BRIGHTNESS(mw
->core
.background_pixel
))
1631 x_alloc_lighter_color_for_widget ((Widget
) mw
, XtDisplay ((Widget
) mw
),
1633 &mw
->menu
.disabled_foreground
,
1637 if (mw
->menu
.foreground
== mw
->menu
.disabled_foreground
1638 || mw
->core
.background_pixel
== mw
->menu
.disabled_foreground
)
1640 /* Too few colors, use stipple. */
1641 xgcv
.foreground
= mw
->menu
.foreground
;
1642 xgcv
.fill_style
= FillStippled
;
1643 xgcv
.stipple
= mw
->menu
.gray_pixmap
;
1644 mw
->menu
.disabled_gc
= XtGetGC ((Widget
)mw
, mask
1645 | GCFillStyle
| GCStipple
, &xgcv
);
1649 /* Many colors available, use disabled pixel. */
1650 xgcv
.foreground
= mw
->menu
.disabled_foreground
;
1651 mw
->menu
.disabled_gc
= XtGetGC ((Widget
)mw
, mask
, &xgcv
);
1654 xgcv
.foreground
= mw
->menu
.button_foreground
;
1655 xgcv
.background
= mw
->core
.background_pixel
;
1656 xgcv
.fill_style
= FillStippled
;
1657 xgcv
.stipple
= mw
->menu
.gray_pixmap
;
1658 mw
->menu
.inactive_button_gc
= XtGetGC ((Widget
)mw
, mask
1659 | GCFillStyle
| GCStipple
, &xgcv
);
1661 xgcv
.foreground
= mw
->core
.background_pixel
;
1662 xgcv
.background
= mw
->menu
.foreground
;
1663 mw
->menu
.background_gc
= XtGetGC ((Widget
)mw
, mask
, &xgcv
);
1667 release_drawing_gcs (XlwMenuWidget mw
)
1669 XtReleaseGC ((Widget
) mw
, mw
->menu
.foreground_gc
);
1670 XtReleaseGC ((Widget
) mw
, mw
->menu
.button_gc
);
1671 XtReleaseGC ((Widget
) mw
, mw
->menu
.disabled_gc
);
1672 XtReleaseGC ((Widget
) mw
, mw
->menu
.inactive_button_gc
);
1673 XtReleaseGC ((Widget
) mw
, mw
->menu
.background_gc
);
1674 /* let's get some segvs if we try to use these... */
1675 mw
->menu
.foreground_gc
= (GC
) -1;
1676 mw
->menu
.button_gc
= (GC
) -1;
1677 mw
->menu
.disabled_gc
= (GC
) -1;
1678 mw
->menu
.inactive_button_gc
= (GC
) -1;
1679 mw
->menu
.background_gc
= (GC
) -1;
1683 #define MINL(x,y) ((((unsigned long) (x)) < ((unsigned long) (y))) \
1684 ? ((unsigned long) (x)) : ((unsigned long) (y)))
1688 make_shadow_gcs (XlwMenuWidget mw
)
1691 unsigned long pm
= 0;
1692 Display
*dpy
= XtDisplay ((Widget
) mw
);
1693 Screen
*screen
= XtScreen ((Widget
) mw
);
1694 Colormap cmap
= mw
->core
.colormap
;
1696 int top_frobbed
= 0, bottom_frobbed
= 0;
1698 mw
->menu
.free_top_shadow_color_p
= 0;
1699 mw
->menu
.free_bottom_shadow_color_p
= 0;
1701 if (mw
->menu
.top_shadow_color
== -1)
1702 mw
->menu
.top_shadow_color
= mw
->core
.background_pixel
;
1704 mw
->menu
.top_shadow_color
= mw
->menu
.top_shadow_color
;
1706 if (mw
->menu
.bottom_shadow_color
== -1)
1707 mw
->menu
.bottom_shadow_color
= mw
->menu
.foreground
;
1709 mw
->menu
.bottom_shadow_color
= mw
->menu
.bottom_shadow_color
;
1711 if (mw
->menu
.top_shadow_color
== mw
->core
.background_pixel
||
1712 mw
->menu
.top_shadow_color
== mw
->menu
.foreground
)
1714 topc
.pixel
= mw
->core
.background_pixel
;
1716 if (x_alloc_lighter_color_for_widget ((Widget
) mw
, dpy
, cmap
,
1720 XQueryColor (dpy
, cmap
, &topc
);
1721 /* don't overflow/wrap! */
1722 topc
.red
= MINL (65535, topc
.red
* 1.2);
1723 topc
.green
= MINL (65535, topc
.green
* 1.2);
1724 topc
.blue
= MINL (65535, topc
.blue
* 1.2);
1725 if (XAllocColor (dpy
, cmap
, &topc
))
1728 mw
->menu
.top_shadow_color
= topc
.pixel
;
1729 mw
->menu
.free_top_shadow_color_p
= 1;
1733 if (mw
->menu
.bottom_shadow_color
== mw
->menu
.foreground
||
1734 mw
->menu
.bottom_shadow_color
== mw
->core
.background_pixel
)
1736 botc
.pixel
= mw
->core
.background_pixel
;
1738 if (x_alloc_lighter_color_for_widget ((Widget
) mw
, dpy
, cmap
,
1742 XQueryColor (dpy
, cmap
, &botc
);
1746 if (XAllocColor (dpy
, cmap
, &botc
))
1749 mw
->menu
.bottom_shadow_color
= botc
.pixel
;
1750 mw
->menu
.free_bottom_shadow_color_p
= 1;
1755 if (top_frobbed
&& bottom_frobbed
)
1757 if (topc
.pixel
== botc
.pixel
)
1759 if (botc
.pixel
== mw
->menu
.foreground
)
1761 if (mw
->menu
.free_top_shadow_color_p
)
1763 x_free_dpy_colors (dpy
, screen
, cmap
,
1764 &mw
->menu
.top_shadow_color
, 1);
1765 mw
->menu
.free_top_shadow_color_p
= 0;
1767 mw
->menu
.top_shadow_color
= mw
->core
.background_pixel
;
1771 if (mw
->menu
.free_bottom_shadow_color_p
)
1773 x_free_dpy_colors (dpy
, screen
, cmap
,
1774 &mw
->menu
.bottom_shadow_color
, 1);
1775 mw
->menu
.free_bottom_shadow_color_p
= 0;
1777 mw
->menu
.bottom_shadow_color
= mw
->menu
.foreground
;
1782 if (!mw
->menu
.top_shadow_pixmap
&&
1783 mw
->menu
.top_shadow_color
== mw
->core
.background_pixel
)
1785 mw
->menu
.top_shadow_pixmap
= mw
->menu
.gray_pixmap
;
1786 if (mw
->menu
.free_top_shadow_color_p
)
1788 x_free_dpy_colors (dpy
, screen
, cmap
, &mw
->menu
.top_shadow_color
, 1);
1789 mw
->menu
.free_top_shadow_color_p
= 0;
1791 mw
->menu
.top_shadow_color
= mw
->menu
.foreground
;
1793 if (!mw
->menu
.bottom_shadow_pixmap
&&
1794 mw
->menu
.bottom_shadow_color
== mw
->core
.background_pixel
)
1796 mw
->menu
.bottom_shadow_pixmap
= mw
->menu
.gray_pixmap
;
1797 if (mw
->menu
.free_bottom_shadow_color_p
)
1799 x_free_dpy_colors (dpy
, screen
, cmap
,
1800 &mw
->menu
.bottom_shadow_color
, 1);
1801 mw
->menu
.free_bottom_shadow_color_p
= 0;
1803 mw
->menu
.bottom_shadow_color
= mw
->menu
.foreground
;
1806 xgcv
.fill_style
= FillStippled
;
1807 xgcv
.foreground
= mw
->menu
.top_shadow_color
;
1808 xgcv
.stipple
= mw
->menu
.top_shadow_pixmap
;
1809 pm
= (xgcv
.stipple
? GCStipple
|GCFillStyle
: 0);
1810 mw
->menu
.shadow_top_gc
= XtGetGC ((Widget
)mw
, GCForeground
| pm
, &xgcv
);
1812 xgcv
.foreground
= mw
->menu
.bottom_shadow_color
;
1813 xgcv
.stipple
= mw
->menu
.bottom_shadow_pixmap
;
1814 pm
= (xgcv
.stipple
? GCStipple
|GCFillStyle
: 0);
1815 mw
->menu
.shadow_bottom_gc
= XtGetGC ((Widget
)mw
, GCForeground
| pm
, &xgcv
);
1820 release_shadow_gcs (XlwMenuWidget mw
)
1822 Display
*dpy
= XtDisplay ((Widget
) mw
);
1823 Screen
*screen
= XtScreen ((Widget
) mw
);
1824 Colormap cmap
= mw
->core
.colormap
;
1828 if (mw
->menu
.free_top_shadow_color_p
)
1829 px
[i
++] = mw
->menu
.top_shadow_color
;
1830 if (mw
->menu
.free_bottom_shadow_color_p
)
1831 px
[i
++] = mw
->menu
.bottom_shadow_color
;
1833 x_free_dpy_colors (dpy
, screen
, cmap
, px
, i
);
1835 XtReleaseGC ((Widget
) mw
, mw
->menu
.shadow_top_gc
);
1836 XtReleaseGC ((Widget
) mw
, mw
->menu
.shadow_bottom_gc
);
1841 getDefaultXftFont (XlwMenuWidget mw
)
1843 int screen
= XScreenNumberOfScreen (mw
->core
.screen
);
1844 return XftFontOpenName (XtDisplay (mw
), screen
, DEFAULT_FONTNAME
);
1848 openXftFont (XlwMenuWidget mw
)
1850 char *fname
= mw
->menu
.fontName
;
1852 mw
->menu
.xft_font
= 0;
1853 mw
->menu
.default_face
= fname
&& strcmp (fname
, DEFAULT_FONTNAME
) == 0;
1855 if (fname
&& strcmp (fname
, "none") != 0)
1857 int screen
= XScreenNumberOfScreen (mw
->core
.screen
);
1858 int len
= strlen (fname
), i
= len
-1;
1859 /* Try to convert Gtk-syntax (Sans 9) to Xft syntax Sans-9. */
1860 while (i
> 0 && '0' <= fname
[i
] && fname
[i
] <= '9')
1862 if (fname
[i
] == ' ')
1864 fname
= xstrdup (mw
->menu
.fontName
);
1868 mw
->menu
.font
= XLoadQueryFont (XtDisplay (mw
), fname
);
1871 mw
->menu
.xft_font
= XftFontOpenName (XtDisplay (mw
), screen
, fname
);
1872 if (!mw
->menu
.xft_font
)
1874 fprintf (stderr
, "Can't find font '%s'\n", fname
);
1875 mw
->menu
.xft_font
= getDefaultXftFont (mw
);
1880 if (fname
!= mw
->menu
.fontName
) xfree (fname
);
1882 return mw
->menu
.xft_font
!= 0;
1887 XlwMenuInitialize (Widget request
, Widget w
, ArgList args
, Cardinal
*num_args
)
1889 /* Get the GCs and the widget size */
1890 XlwMenuWidget mw
= (XlwMenuWidget
) w
;
1891 Window window
= RootWindowOfScreen (DefaultScreenOfDisplay (XtDisplay (mw
)));
1892 Display
* display
= XtDisplay (mw
);
1895 widget_value
*tem
= (widget_value
*) XtMalloc (sizeof (widget_value
));
1897 /* _XtCreate is freeing the object that was passed to us,
1898 so make a copy that we will actually keep. */
1899 memcpy (tem
, mw
->menu
.contents
, sizeof (widget_value
));
1900 mw
->menu
.contents
= tem
;
1903 /* mw->menu.cursor = XCreateFontCursor (display, mw->menu.cursor_shape); */
1904 mw
->menu
.cursor
= mw
->menu
.cursor_shape
;
1906 mw
->menu
.gray_pixmap
1907 = XCreatePixmapFromBitmapData (display
, window
, gray_bits
,
1908 gray_width
, gray_height
,
1909 (unsigned long)1, (unsigned long)0, 1);
1912 if (openXftFont (mw
))
1917 mw
->menu
.font
= XLoadQueryFont (display
, mw
->menu
.fontName
);
1920 mw
->menu
.font
= XLoadQueryFont (display
, "fixed");
1923 fprintf (stderr
, "Menu font fixed not found, can't continue.\n");
1930 if (mw
->menu
.fontSet
)
1931 mw
->menu
.font_extents
= XExtentsOfFontSet (mw
->menu
.fontSet
);
1934 make_drawing_gcs (mw
);
1935 make_shadow_gcs (mw
);
1937 mw
->menu
.popped_up
= False
;
1939 mw
->menu
.old_depth
= 1;
1940 mw
->menu
.old_stack
= (widget_value
**)XtMalloc (sizeof (widget_value
*));
1941 mw
->menu
.old_stack_length
= 1;
1942 mw
->menu
.old_stack
[0] = mw
->menu
.contents
;
1944 mw
->menu
.new_depth
= 0;
1945 mw
->menu
.new_stack
= 0;
1946 mw
->menu
.new_stack_length
= 0;
1947 push_new_stack (mw
, mw
->menu
.contents
);
1949 mw
->menu
.windows
= (window_state
*)XtMalloc (sizeof (window_state
));
1950 mw
->menu
.windows_length
= 1;
1951 mw
->menu
.windows
[0].x
= 0;
1952 mw
->menu
.windows
[0].y
= 0;
1953 mw
->menu
.windows
[0].width
= 0;
1954 mw
->menu
.windows
[0].height
= 0;
1955 mw
->menu
.windows
[0].max_rest_width
= 0;
1956 mw
->menu
.windows
[0].pixmap
= None
;
1958 mw
->menu
.windows
[0].xft_draw
= 0;
1962 mw
->core
.width
= mw
->menu
.windows
[0].width
;
1963 mw
->core
.height
= mw
->menu
.windows
[0].height
;
1967 XlwMenuClassInitialize (void)
1972 XlwMenuRealize (Widget w
, Mask
*valueMask
, XSetWindowAttributes
*attributes
)
1974 XlwMenuWidget mw
= (XlwMenuWidget
)w
;
1975 XSetWindowAttributes xswa
;
1978 (*xlwMenuWidgetClass
->core_class
.superclass
->core_class
.realize
)
1979 (w
, valueMask
, attributes
);
1981 xswa
.save_under
= True
;
1982 xswa
.cursor
= mw
->menu
.cursor_shape
;
1983 mask
= CWSaveUnder
| CWCursor
;
1984 /* I sometimes get random BadCursor errors while creating the first
1985 frame on a display. I can not find their reason, but they are
1986 annoying so for now let's ignore any errors here. -- lorentey */
1988 x_catch_errors (XtDisplay (w
));
1990 XChangeWindowAttributes (XtDisplay (w
), XtWindow (w
), mask
, &xswa
);
1992 x_uncatch_errors ();
1995 mw
->menu
.windows
[0].w
= w
;
1996 mw
->menu
.windows
[0].window
= XtWindow (w
);
1997 mw
->menu
.windows
[0].x
= w
->core
.x
;
1998 mw
->menu
.windows
[0].y
= w
->core
.y
;
1999 mw
->menu
.windows
[0].width
= w
->core
.width
;
2000 mw
->menu
.windows
[0].height
= w
->core
.height
;
2002 set_window_type (mw
->menu
.windows
[0].w
, mw
);
2003 create_pixmap_for_menu (&mw
->menu
.windows
[0], mw
);
2006 if (mw
->menu
.xft_font
)
2009 colors
[0].pixel
= mw
->menu
.xft_fg
.pixel
= mw
->menu
.foreground
;
2010 colors
[1].pixel
= mw
->menu
.xft_bg
.pixel
= mw
->core
.background_pixel
;
2011 colors
[2].pixel
= mw
->menu
.xft_disabled_fg
.pixel
2012 = mw
->menu
.disabled_foreground
;
2013 XQueryColors (XtDisplay (mw
), mw
->core
.colormap
, colors
, 3);
2014 mw
->menu
.xft_fg
.color
.alpha
= 0xFFFF;
2015 mw
->menu
.xft_fg
.color
.red
= colors
[0].red
;
2016 mw
->menu
.xft_fg
.color
.green
= colors
[0].green
;
2017 mw
->menu
.xft_fg
.color
.blue
= colors
[0].blue
;
2018 mw
->menu
.xft_bg
.color
.alpha
= 0xFFFF;
2019 mw
->menu
.xft_bg
.color
.red
= colors
[1].red
;
2020 mw
->menu
.xft_bg
.color
.green
= colors
[1].green
;
2021 mw
->menu
.xft_bg
.color
.blue
= colors
[1].blue
;
2022 mw
->menu
.xft_disabled_fg
.color
.alpha
= 0xFFFF;
2023 mw
->menu
.xft_disabled_fg
.color
.red
= colors
[2].red
;
2024 mw
->menu
.xft_disabled_fg
.color
.green
= colors
[2].green
;
2025 mw
->menu
.xft_disabled_fg
.color
.blue
= colors
[2].blue
;
2030 /* Only the toplevel menubar/popup is a widget so it's the only one that
2031 receives expose events through Xt. So we repaint all the other panes
2032 when receiving an Expose event. */
2034 XlwMenuRedisplay (Widget w
, XEvent
*ev
, Region region
)
2036 XlwMenuWidget mw
= (XlwMenuWidget
)w
;
2038 /* If we have a depth beyond 1, it's because a submenu was displayed.
2039 If the submenu has been destroyed, set the depth back to 1. */
2040 if (submenu_destroyed
)
2042 mw
->menu
.old_depth
= 1;
2043 submenu_destroyed
= 0;
2046 display_menu (mw
, 0, False
, NULL
, NULL
, NULL
);
2050 /* Part of a hack to make the menu redisplay when a tooltip frame
2051 over a menu item is unmapped. */
2054 xlwmenu_redisplay (Widget w
)
2056 XlwMenuRedisplay (w
, NULL
, None
);
2060 XlwMenuDestroy (Widget w
)
2063 XlwMenuWidget mw
= (XlwMenuWidget
) w
;
2065 if (pointer_grabbed
)
2066 ungrab_all ((Widget
)w
, CurrentTime
);
2067 pointer_grabbed
= 0;
2069 submenu_destroyed
= 1;
2071 release_drawing_gcs (mw
);
2072 release_shadow_gcs (mw
);
2074 /* this doesn't come from the resource db but is created explicitly
2075 so we must free it ourselves. */
2076 XFreePixmap (XtDisplay (mw
), mw
->menu
.gray_pixmap
);
2077 mw
->menu
.gray_pixmap
= (Pixmap
) -1;
2080 /* Do free mw->menu.contents because nowadays we copy it
2081 during initialization. */
2082 XtFree (mw
->menu
.contents
);
2085 /* Don't free mw->menu.contents because that comes from our creator.
2086 The `*_stack' elements are just pointers into `contents' so leave
2087 that alone too. But free the stacks themselves. */
2088 if (mw
->menu
.old_stack
) XtFree ((char *) mw
->menu
.old_stack
);
2089 if (mw
->menu
.new_stack
) XtFree ((char *) mw
->menu
.new_stack
);
2091 /* Remember, you can't free anything that came from the resource
2092 database. This includes:
2094 mw->menu.top_shadow_pixmap
2095 mw->menu.bottom_shadow_pixmap
2097 Also the color cells of top_shadow_color, bottom_shadow_color,
2098 foreground, and button_foreground will never be freed until this
2099 client exits. Nice, eh?
2103 if (mw
->menu
.windows
[0].xft_draw
)
2104 XftDrawDestroy (mw
->menu
.windows
[0].xft_draw
);
2105 if (mw
->menu
.xft_font
)
2106 XftFontClose (XtDisplay (mw
), mw
->menu
.xft_font
);
2109 if (mw
->menu
.windows
[0].pixmap
!= None
)
2110 XFreePixmap (XtDisplay (mw
), mw
->menu
.windows
[0].pixmap
);
2111 /* start from 1 because the one in slot 0 is w->core.window */
2112 for (i
= 1; i
< mw
->menu
.windows_length
; i
++)
2114 if (mw
->menu
.windows
[i
].pixmap
!= None
)
2115 XFreePixmap (XtDisplay (mw
), mw
->menu
.windows
[i
].pixmap
);
2117 if (mw
->menu
.windows
[i
].xft_draw
)
2118 XftDrawDestroy (mw
->menu
.windows
[i
].xft_draw
);
2122 if (mw
->menu
.windows
)
2123 XtFree ((char *) mw
->menu
.windows
);
2128 fontname_changed (XlwMenuWidget newmw
,
2129 XlwMenuWidget oldmw
)
2131 /* This will force a new XftFont even if the same string is set.
2132 This is good, as rendering parameters may have changed and
2133 we just want to do a redisplay. */
2134 return newmw
->menu
.fontName
!= oldmw
->menu
.fontName
;
2139 XlwMenuSetValues (Widget current
, Widget request
, Widget
new,
2140 ArgList args
, Cardinal
*num_args
)
2142 XlwMenuWidget oldmw
= (XlwMenuWidget
)current
;
2143 XlwMenuWidget newmw
= (XlwMenuWidget
)new;
2144 Boolean do_redisplay
= False
;
2146 if (newmw
->menu
.contents
2147 && newmw
->menu
.contents
->contents
2148 && newmw
->menu
.contents
->contents
->change
>= VISIBLE_CHANGE
)
2149 do_redisplay
= True
;
2150 /* Do redisplay if the contents are entirely eliminated. */
2151 if (newmw
->menu
.contents
2152 && newmw
->menu
.contents
->contents
== 0
2153 && newmw
->menu
.contents
->change
>= VISIBLE_CHANGE
)
2154 do_redisplay
= True
;
2156 if (newmw
->core
.background_pixel
!= oldmw
->core
.background_pixel
2157 || newmw
->menu
.foreground
!= oldmw
->menu
.foreground
2159 || fontname_changed (newmw
, oldmw
)
2162 || newmw
->menu
.fontSet
!= oldmw
->menu
.fontSet
2163 || (newmw
->menu
.fontSet
== NULL
&& newmw
->menu
.font
!= oldmw
->menu
.font
)
2165 || newmw
->menu
.font
!= oldmw
->menu
.font
2170 release_drawing_gcs (newmw
);
2171 make_drawing_gcs (newmw
);
2173 release_shadow_gcs (newmw
);
2174 /* Cause the shadow colors to be recalculated. */
2175 newmw
->menu
.top_shadow_color
= -1;
2176 newmw
->menu
.bottom_shadow_color
= -1;
2177 make_shadow_gcs (newmw
);
2179 do_redisplay
= True
;
2181 if (XtIsRealized (current
))
2182 /* If the menu is currently displayed, change the display. */
2183 for (i
= 0; i
< oldmw
->menu
.windows_length
; i
++)
2185 XSetWindowBackground (XtDisplay (oldmw
),
2186 oldmw
->menu
.windows
[i
].window
,
2187 newmw
->core
.background_pixel
);
2188 /* clear windows and generate expose events */
2189 XClearArea (XtDisplay (oldmw
), oldmw
->menu
.windows
[i
].window
,
2195 if (fontname_changed (newmw
, oldmw
))
2198 int screen
= XScreenNumberOfScreen (newmw
->core
.screen
);
2199 if (newmw
->menu
.xft_font
)
2200 XftFontClose (XtDisplay (newmw
), newmw
->menu
.xft_font
);
2201 openXftFont (newmw
);
2202 for (i
= 0; i
< newmw
->menu
.windows_length
; i
++)
2204 if (newmw
->menu
.windows
[i
].xft_draw
)
2205 XftDrawDestroy (newmw
->menu
.windows
[i
].xft_draw
);
2206 newmw
->menu
.windows
[i
].xft_draw
= 0;
2208 if (newmw
->menu
.xft_font
)
2209 for (i
= 0; i
< newmw
->menu
.windows_length
; i
++)
2210 newmw
->menu
.windows
[i
].xft_draw
2211 = XftDrawCreate (XtDisplay (newmw
),
2212 newmw
->menu
.windows
[i
].window
,
2213 DefaultVisual (XtDisplay (newmw
), screen
),
2214 newmw
->core
.colormap
);
2218 if (newmw
->menu
.fontSet
!= oldmw
->menu
.fontSet
&& newmw
->menu
.fontSet
!= NULL
)
2220 do_redisplay
= True
;
2221 newmw
->menu
.font_extents
= XExtentsOfFontSet (newmw
->menu
.fontSet
);
2225 return do_redisplay
;
2229 XlwMenuResize (Widget w
)
2231 XlwMenuWidget mw
= (XlwMenuWidget
)w
;
2233 if (mw
->menu
.popped_up
)
2235 /* Don't allow the popup menu to resize itself. */
2236 mw
->core
.width
= mw
->menu
.windows
[0].width
;
2237 mw
->core
.height
= mw
->menu
.windows
[0].height
;
2238 mw
->core
.parent
->core
.width
= mw
->core
.width
;
2239 mw
->core
.parent
->core
.height
= mw
->core
.height
;
2243 mw
->menu
.windows
[0].width
= mw
->core
.width
;
2244 mw
->menu
.windows
[0].height
= mw
->core
.height
;
2245 create_pixmap_for_menu (&mw
->menu
.windows
[0], mw
);
2249 \f/* Action procedures */
2251 handle_single_motion_event (XlwMenuWidget mw
, XMotionEvent
*ev
)
2256 if (!map_event_to_widget_value (mw
, ev
, &val
, &level
))
2257 pop_new_stack_if_no_contents (mw
);
2259 set_new_state (mw
, val
, level
);
2262 /* Sync with the display. Makes it feel better on X terms. */
2263 XSync (XtDisplay (mw
), False
);
2267 handle_motion_event (XlwMenuWidget mw
, XMotionEvent
*ev
)
2271 int state
= ev
->state
;
2272 XMotionEvent oldev
= *ev
;
2274 /* allow motion events to be generated again */
2276 && XQueryPointer (XtDisplay (mw
), ev
->window
,
2277 &ev
->root
, &ev
->subwindow
,
2278 &ev
->x_root
, &ev
->y_root
,
2281 && ev
->state
== state
2282 && (ev
->x_root
!= x
|| ev
->y_root
!= y
))
2283 handle_single_motion_event (mw
, ev
);
2285 handle_single_motion_event (mw
, &oldev
);
2289 Start (Widget w
, XEvent
*ev
, String
*params
, Cardinal
*num_params
)
2291 XlwMenuWidget mw
= (XlwMenuWidget
)w
;
2293 if (!mw
->menu
.popped_up
)
2295 menu_post_event
= *ev
;
2296 /* If event is set to CurrentTime, get the last known time stamp.
2297 This is for calculating if (popup) menus should stay up after
2299 if (menu_post_event
.xbutton
.time
== CurrentTime
)
2300 menu_post_event
.xbutton
.time
2301 = XtLastTimestampProcessed (XtDisplay (w
));
2303 pop_up_menu (mw
, (XButtonPressedEvent
*) ev
);
2307 /* If we push a button while the menu is posted semipermanently,
2308 releasing the button should always pop the menu down. */
2309 next_release_must_exit
= 1;
2311 /* notes the absolute position of the menubar window */
2312 mw
->menu
.windows
[0].x
= ev
->xmotion
.x_root
- ev
->xmotion
.x
;
2313 mw
->menu
.windows
[0].y
= ev
->xmotion
.y_root
- ev
->xmotion
.y
;
2315 /* handles the down like a move, slots are compatible */
2316 ev
->xmotion
.is_hint
= 0;
2317 handle_motion_event (mw
, &ev
->xmotion
);
2322 Drag (Widget w
, XEvent
*ev
, String
*params
, Cardinal
*num_params
)
2324 XlwMenuWidget mw
= (XlwMenuWidget
)w
;
2325 if (mw
->menu
.popped_up
)
2326 handle_motion_event (mw
, &ev
->xmotion
);
2330 This is how we handle presses and releases of modifier keys. */
2332 Nothing (Widget w
, XEvent
*ev
, String
*params
, Cardinal
*num_params
)
2336 static widget_value
*
2337 find_first_selectable (XlwMenuWidget mw
, widget_value
*item
, int skip_titles
)
2339 widget_value
*current
= item
;
2340 enum menu_separator separator
;
2342 while (lw_separator_p (current
->name
, &separator
, 0) || !current
->enabled
2343 || (skip_titles
&& !current
->call_data
&& !current
->contents
))
2345 current
=current
->next
;
2352 static widget_value
*
2353 find_next_selectable (XlwMenuWidget mw
, widget_value
*item
, int skip_titles
)
2355 widget_value
*current
= item
;
2356 enum menu_separator separator
;
2358 while (current
->next
&& (current
=current
->next
) &&
2359 (lw_separator_p (current
->name
, &separator
, 0) || !current
->enabled
2360 || (skip_titles
&& !current
->call_data
&& !current
->contents
)))
2363 if (current
== item
)
2365 if (mw
->menu
.old_depth
< 2)
2367 current
= mw
->menu
.old_stack
[mw
->menu
.old_depth
- 2]->contents
;
2369 while (lw_separator_p (current
->name
, &separator
, 0)
2370 || !current
->enabled
2371 || (skip_titles
&& !current
->call_data
2372 && !current
->contents
))
2375 current
=current
->next
;
2377 if (current
== item
)
2386 static widget_value
*
2387 find_prev_selectable (XlwMenuWidget mw
, widget_value
*item
, int skip_titles
)
2389 widget_value
*current
= item
;
2390 widget_value
*prev
= item
;
2392 while ((current
=find_next_selectable (mw
, current
, skip_titles
))
2395 if (prev
== current
)
2404 Down (Widget w
, XEvent
*ev
, String
*params
, Cardinal
*num_params
)
2406 XlwMenuWidget mw
= (XlwMenuWidget
) w
;
2407 widget_value
* selected_item
= mw
->menu
.old_stack
[mw
->menu
.old_depth
- 1];
2408 int popup_menu_p
= mw
->menu
.top_depth
== 1;
2410 /* Inside top-level menu-bar? */
2411 if (mw
->menu
.old_depth
== mw
->menu
.top_depth
)
2412 /* When <down> in the menu-bar is pressed, display the corresponding
2413 sub-menu and select the first selectable menu item there.
2414 If this is a popup menu, skip title item of the popup. */
2416 find_first_selectable (mw
,
2417 selected_item
->contents
,
2419 mw
->menu
.old_depth
);
2421 /* Highlight next possible (enabled and not separator) menu item. */
2422 set_new_state (mw
, find_next_selectable (mw
, selected_item
, popup_menu_p
),
2423 mw
->menu
.old_depth
- 1);
2429 Up (Widget w
, XEvent
*ev
, String
*params
, Cardinal
*num_params
)
2431 XlwMenuWidget mw
= (XlwMenuWidget
) w
;
2432 widget_value
* selected_item
= mw
->menu
.old_stack
[mw
->menu
.old_depth
- 1];
2433 int popup_menu_p
= mw
->menu
.top_depth
== 1;
2435 /* Inside top-level menu-bar? */
2436 if (mw
->menu
.old_depth
== mw
->menu
.top_depth
)
2438 /* FIXME: this is tricky. <up> in the menu-bar should select the
2439 last selectable item in the list. So we select the first
2440 selectable one and find the previous selectable item. Is there
2442 /* If this is a popup menu, skip title item of the popup. */
2444 find_first_selectable (mw
,
2445 selected_item
->contents
,
2447 mw
->menu
.old_depth
);
2449 selected_item
= mw
->menu
.old_stack
[mw
->menu
.old_depth
- 1];
2451 find_prev_selectable (mw
,
2454 mw
->menu
.old_depth
- 1);
2457 /* Highlight previous (enabled and not separator) menu item. */
2458 set_new_state (mw
, find_prev_selectable (mw
, selected_item
, popup_menu_p
),
2459 mw
->menu
.old_depth
- 1);
2465 Left (Widget w
, XEvent
*ev
, String
*params
, Cardinal
*num_params
)
2467 XlwMenuWidget mw
= (XlwMenuWidget
) w
;
2468 widget_value
* selected_item
= mw
->menu
.old_stack
[mw
->menu
.old_depth
- 1];
2470 /* Inside top-level menu-bar? */
2471 if (mw
->menu
.old_depth
== mw
->menu
.top_depth
)
2472 /* When <left> in the menu-bar is pressed, display the previous item on
2473 the menu-bar. If the current item is the first one, highlight the
2474 last item in the menubar (probably Help). */
2475 set_new_state (mw
, find_prev_selectable (mw
, selected_item
, 0),
2476 mw
->menu
.old_depth
- 1);
2477 else if (mw
->menu
.old_depth
== 1
2478 && selected_item
->contents
) /* Is this menu item expandable? */
2480 set_new_state (mw
, selected_item
->contents
, mw
->menu
.old_depth
);
2482 selected_item
= mw
->menu
.old_stack
[mw
->menu
.old_depth
- 1];
2483 if (!selected_item
->enabled
&& find_first_selectable (mw
,
2486 set_new_state (mw
, find_first_selectable (mw
, selected_item
, 0),
2487 mw
->menu
.old_depth
- 1);
2492 pop_new_stack_if_no_contents (mw
);
2493 set_new_state (mw
, mw
->menu
.old_stack
[mw
->menu
.old_depth
- 2],
2494 mw
->menu
.old_depth
- 2);
2501 Right (Widget w
, XEvent
*ev
, String
*params
, Cardinal
*num_params
)
2503 XlwMenuWidget mw
= (XlwMenuWidget
) w
;
2504 widget_value
* selected_item
= mw
->menu
.old_stack
[mw
->menu
.old_depth
- 1];
2506 /* Inside top-level menu-bar? */
2507 if (mw
->menu
.old_depth
== mw
->menu
.top_depth
)
2508 /* When <right> in the menu-bar is pressed, display the next item on
2509 the menu-bar. If the current item is the last one, highlight the
2510 first item (probably File). */
2511 set_new_state (mw
, find_next_selectable (mw
, selected_item
, 0),
2512 mw
->menu
.old_depth
- 1);
2513 else if (selected_item
->contents
) /* Is this menu item expandable? */
2515 set_new_state (mw
, selected_item
->contents
, mw
->menu
.old_depth
);
2517 selected_item
= mw
->menu
.old_stack
[mw
->menu
.old_depth
- 1];
2518 if (!selected_item
->enabled
&& find_first_selectable (mw
,
2521 set_new_state (mw
, find_first_selectable (mw
, selected_item
, 0),
2522 mw
->menu
.old_depth
- 1);
2526 pop_new_stack_if_no_contents (mw
);
2527 set_new_state (mw
, mw
->menu
.old_stack
[mw
->menu
.old_depth
- 2],
2528 mw
->menu
.old_depth
- 2);
2534 /* Handle key press and release events while menu is popped up.
2535 Our action is to get rid of the menu. */
2537 Key (Widget w
, XEvent
*ev
, String
*params
, Cardinal
*num_params
)
2539 XlwMenuWidget mw
= (XlwMenuWidget
)w
;
2541 /* Pop down everything. */
2542 mw
->menu
.new_depth
= 1;
2545 if (mw
->menu
.popped_up
)
2547 mw
->menu
.popped_up
= False
;
2548 ungrab_all ((Widget
)mw
, ev
->xmotion
.time
);
2549 if (XtIsShell (XtParent ((Widget
) mw
)))
2550 XtPopdown (XtParent ((Widget
) mw
));
2553 XtRemoveGrab ((Widget
) mw
);
2554 display_menu (mw
, 0, False
, NULL
, NULL
, NULL
);
2559 XtCallCallbackList ((Widget
)mw
, mw
->menu
.select
, (XtPointer
)0);
2563 Select (Widget w
, XEvent
*ev
, String
*params
, Cardinal
*num_params
)
2565 XlwMenuWidget mw
= (XlwMenuWidget
)w
;
2566 widget_value
* selected_item
= mw
->menu
.old_stack
[mw
->menu
.old_depth
- 1];
2568 /* If user releases the button quickly, without selecting anything,
2569 after the initial down-click that brought the menu up,
2571 if ((selected_item
== 0
2572 || ((widget_value
*) selected_item
)->call_data
== 0)
2573 && !next_release_must_exit
2574 && (ev
->xbutton
.time
- menu_post_event
.xbutton
.time
2575 < XtGetMultiClickTime (XtDisplay (w
))))
2578 /* pop down everything. */
2579 mw
->menu
.new_depth
= 1;
2582 if (mw
->menu
.popped_up
)
2584 mw
->menu
.popped_up
= False
;
2585 ungrab_all ((Widget
)mw
, ev
->xmotion
.time
);
2586 if (XtIsShell (XtParent ((Widget
) mw
)))
2587 XtPopdown (XtParent ((Widget
) mw
));
2590 XtRemoveGrab ((Widget
) mw
);
2591 display_menu (mw
, 0, False
, NULL
, NULL
, NULL
);
2596 XtCallCallbackList ((Widget
)mw
, mw
->menu
.select
, (XtPointer
)selected_item
);
2600 \f/* Special code to pop-up a menu */
2602 pop_up_menu (XlwMenuWidget mw
, XButtonPressedEvent
*event
)
2604 int x
= event
->x_root
;
2605 int y
= event
->y_root
;
2608 int borderwidth
= mw
->menu
.shadow_thickness
;
2609 Screen
* screen
= XtScreen (mw
);
2610 Display
*display
= XtDisplay (mw
);
2612 next_release_must_exit
= 0;
2614 mw
->menu
.inside_entry
= NULL
;
2615 XtCallCallbackList ((Widget
)mw
, mw
->menu
.open
, NULL
);
2617 if (XtIsShell (XtParent ((Widget
)mw
)))
2620 w
= mw
->menu
.windows
[0].width
;
2621 h
= mw
->menu
.windows
[0].height
;
2625 if (x
< borderwidth
)
2627 if (x
+ w
+ 2 * borderwidth
> WidthOfScreen (screen
))
2628 x
= WidthOfScreen (screen
) - w
- 2 * borderwidth
;
2629 if (y
< borderwidth
)
2631 if (y
+ h
+ 2 * borderwidth
> HeightOfScreen (screen
))
2632 y
= HeightOfScreen (screen
) - h
- 2 * borderwidth
;
2634 mw
->menu
.popped_up
= True
;
2635 if (XtIsShell (XtParent ((Widget
)mw
)))
2637 XtConfigureWidget (XtParent ((Widget
)mw
), x
, y
, w
, h
,
2638 XtParent ((Widget
)mw
)->core
.border_width
);
2639 XtPopup (XtParent ((Widget
)mw
), XtGrabExclusive
);
2640 display_menu (mw
, 0, False
, NULL
, NULL
, NULL
);
2641 mw
->menu
.windows
[0].x
= x
+ borderwidth
;
2642 mw
->menu
.windows
[0].y
= y
+ borderwidth
;
2643 mw
->menu
.top_depth
= 1; /* Popup menus don't have a bar so top is 1 */
2647 XEvent
*ev
= (XEvent
*) event
;
2649 XtAddGrab ((Widget
) mw
, True
, True
);
2651 /* notes the absolute position of the menubar window */
2652 mw
->menu
.windows
[0].x
= ev
->xmotion
.x_root
- ev
->xmotion
.x
;
2653 mw
->menu
.windows
[0].y
= ev
->xmotion
.y_root
- ev
->xmotion
.y
;
2654 mw
->menu
.top_depth
= 2;
2658 x_catch_errors (display
);
2660 if (XtGrabPointer ((Widget
)mw
, False
,
2662 | PointerMotionHintMask
2665 GrabModeAsync
, GrabModeAsync
, None
,
2666 mw
->menu
.cursor_shape
,
2667 event
->time
) == Success
)
2670 || XtGrabKeyboard ((Widget
)mw
, False
, GrabModeAsync
,
2671 GrabModeAsync
, event
->time
) == Success
)
2673 XtSetKeyboardFocus((Widget
)mw
, None
);
2674 pointer_grabbed
= 1;
2677 XtUngrabPointer ((Widget
)mw
, event
->time
);
2681 if (x_had_errors_p (display
))
2683 pointer_grabbed
= 0;
2684 XtUngrabPointer ((Widget
)mw
, event
->time
);
2686 x_uncatch_errors ();
2689 ((XMotionEvent
*)event
)->is_hint
= 0;
2690 handle_motion_event (mw
, (XMotionEvent
*)event
);