1 /* Implements a lightweight menubar widget.
2 Copyright (C) 1992 Lucid, Inc.
4 This file is part of the Lucid Widget Library.
6 The Lucid Widget Library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
11 The Lucid Widget Library is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU Emacs; see the file COPYING. If not, write to the
18 Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA. */
21 /* Created by devin@lucid.com */
29 #include <sys/types.h>
31 #include <X11/IntrinsicP.h>
32 #include <X11/ObjectP.h>
33 #include <X11/StringDefs.h>
34 #include <X11/cursorfont.h>
39 /* Defined in xfns.c. When config.h defines `static' as empty, we get
40 redefinition errors when gray_bitmap is included more than once, so
41 we're referring to the one include in xfns.c here. */
43 extern int gray_bitmap_width
;
44 extern int gray_bitmap_height
;
45 extern unsigned char *gray_bitmap_bits
;
47 /* Defined in xterm.c. */
48 extern int x_alloc_nearest_color_for_widget
__P ((Widget
, Colormap
, XColor
*));
49 extern int x_catch_errors
__P ((Display
*));
50 extern int x_uncatch_errors
__P ((Display
*, int));
51 extern int x_had_errors_p
__P ((Display
*));
52 extern int x_clear_errors
__P ((Display
*));
53 extern unsigned long x_copy_dpy_color
__P ((Display
*, Colormap
,
56 /* Defined in xfaces.c. */
57 extern void x_free_dpy_colors
__P ((Display
*, Screen
*, Colormap
,
58 unsigned long *pixels
, int npixels
));
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\
110 #define offset(field) XtOffset(XlwMenuWidget, field)
114 {XtNfont
, XtCFont
, XtRFontStruct
, sizeof(XFontStruct
*),
115 offset(menu
.font
),XtRString
, "XtDefaultFont"},
116 {XtNforeground
, XtCForeground
, XtRPixel
, sizeof(Pixel
),
117 offset(menu
.foreground
), XtRString
, "XtDefaultForeground"},
118 {XtNbuttonForeground
, XtCButtonForeground
, XtRPixel
, sizeof(Pixel
),
119 offset(menu
.button_foreground
), XtRString
, "XtDefaultForeground"},
120 {XtNmargin
, XtCMargin
, XtRDimension
, sizeof(Dimension
),
121 offset(menu
.margin
), XtRImmediate
, (XtPointer
) 4},
122 {XtNhorizontalSpacing
, XtCMargin
, XtRDimension
, sizeof(Dimension
),
123 offset(menu
.horizontal_spacing
), XtRImmediate
, (XtPointer
)3},
124 {XtNverticalSpacing
, XtCMargin
, XtRDimension
, sizeof(Dimension
),
125 offset(menu
.vertical_spacing
), XtRImmediate
, (XtPointer
)1},
126 {XtNarrowSpacing
, XtCMargin
, XtRDimension
, sizeof(Dimension
),
127 offset(menu
.arrow_spacing
), XtRImmediate
, (XtPointer
)10},
129 {XmNshadowThickness
, XmCShadowThickness
, XtRDimension
,
130 sizeof (Dimension
), offset (menu
.shadow_thickness
),
131 XtRImmediate
, (XtPointer
) 2},
132 {XmNtopShadowColor
, XmCTopShadowColor
, XtRPixel
, sizeof (Pixel
),
133 offset (menu
.top_shadow_color
), XtRImmediate
, (XtPointer
)-1},
134 {XmNbottomShadowColor
, XmCBottomShadowColor
, XtRPixel
, sizeof (Pixel
),
135 offset (menu
.bottom_shadow_color
), XtRImmediate
, (XtPointer
)-1},
136 {XmNtopShadowPixmap
, XmCTopShadowPixmap
, XtRPixmap
, sizeof (Pixmap
),
137 offset (menu
.top_shadow_pixmap
), XtRImmediate
, (XtPointer
)None
},
138 {XmNbottomShadowPixmap
, XmCBottomShadowPixmap
, XtRPixmap
, sizeof (Pixmap
),
139 offset (menu
.bottom_shadow_pixmap
), XtRImmediate
, (XtPointer
)None
},
141 {XtNopen
, XtCCallback
, XtRCallback
, sizeof(XtPointer
),
142 offset(menu
.open
), XtRCallback
, (XtPointer
)NULL
},
143 {XtNselect
, XtCCallback
, XtRCallback
, sizeof(XtPointer
),
144 offset(menu
.select
), XtRCallback
, (XtPointer
)NULL
},
145 {XtNhighlightCallback
, XtCCallback
, XtRCallback
, sizeof(XtPointer
),
146 offset(menu
.highlight
), XtRCallback
, (XtPointer
)NULL
},
147 {XtNmenu
, XtCMenu
, XtRPointer
, sizeof(XtPointer
),
148 offset(menu
.contents
), XtRImmediate
, (XtPointer
)NULL
},
149 {XtNcursor
, XtCCursor
, XtRCursor
, sizeof(Cursor
),
150 offset(menu
.cursor_shape
), XtRString
, (XtPointer
)"right_ptr"},
151 {XtNhorizontal
, XtCHorizontal
, XtRInt
, sizeof(int),
152 offset(menu
.horizontal
), XtRImmediate
, (XtPointer
)True
},
156 static Boolean
XlwMenuSetValues();
157 static void XlwMenuRealize();
158 static void XlwMenuRedisplay();
159 static void XlwMenuResize();
160 static void XlwMenuInitialize();
161 static void XlwMenuRedisplay();
162 static void XlwMenuDestroy();
163 static void XlwMenuClassInitialize();
166 static void Select();
168 static void Nothing();
169 static int separator_height ();
172 xlwMenuActionsList
[] =
178 {"nothing", Nothing
},
181 #define SuperClass ((CoreWidgetClass)&coreClassRec)
183 XlwMenuClassRec xlwMenuClassRec
=
185 { /* CoreClass fields initialization */
186 (WidgetClass
) SuperClass
, /* superclass */
187 "XlwMenu", /* class_name */
188 sizeof(XlwMenuRec
), /* size */
189 XlwMenuClassInitialize
, /* class_initialize */
190 NULL
, /* class_part_initialize */
191 FALSE
, /* class_inited */
192 XlwMenuInitialize
, /* initialize */
193 NULL
, /* initialize_hook */
194 XlwMenuRealize
, /* realize */
195 xlwMenuActionsList
, /* actions */
196 XtNumber(xlwMenuActionsList
), /* num_actions */
197 xlwMenuResources
, /* resources */
198 XtNumber(xlwMenuResources
), /* resource_count */
199 NULLQUARK
, /* xrm_class */
200 TRUE
, /* compress_motion */
201 TRUE
, /* compress_exposure */
202 TRUE
, /* compress_enterleave */
203 FALSE
, /* visible_interest */
204 XlwMenuDestroy
, /* destroy */
205 XlwMenuResize
, /* resize */
206 XlwMenuRedisplay
, /* expose */
207 XlwMenuSetValues
, /* set_values */
208 NULL
, /* set_values_hook */
209 XtInheritSetValuesAlmost
, /* set_values_almost */
210 NULL
, /* get_values_hook */
211 NULL
, /* accept_focus */
212 XtVersion
, /* version */
213 NULL
, /* callback_private */
214 xlwMenuTranslations
, /* tm_table */
215 XtInheritQueryGeometry
, /* query_geometry */
216 XtInheritDisplayAccelerator
, /* display_accelerator */
218 }, /* XlwMenuClass fields initialization */
224 WidgetClass xlwMenuWidgetClass
= (WidgetClass
) &xlwMenuClassRec
;
226 int submenu_destroyed
;
228 static int next_release_must_exit
;
233 /* Like abort, but remove grabs from widget W before. */
239 if (XtIsShell (XtParent (w
)))
241 XtUngrabPointer (w
, CurrentTime
);
246 push_new_stack (mw
, val
)
250 if (!mw
->menu
.new_stack
)
252 mw
->menu
.new_stack_length
= 10;
254 (widget_value
**)XtCalloc (mw
->menu
.new_stack_length
,
255 sizeof (widget_value
*));
257 else if (mw
->menu
.new_depth
== mw
->menu
.new_stack_length
)
259 mw
->menu
.new_stack_length
*= 2;
261 (widget_value
**)XtRealloc ((char*)mw
->menu
.new_stack
,
262 mw
->menu
.new_stack_length
* sizeof (widget_value
*));
264 mw
->menu
.new_stack
[mw
->menu
.new_depth
++] = val
;
268 pop_new_stack_if_no_contents (mw
)
271 if (mw
->menu
.new_depth
)
273 if (!mw
->menu
.new_stack
[mw
->menu
.new_depth
- 1]->contents
)
274 mw
->menu
.new_depth
-= 1;
279 make_old_stack_space (mw
, n
)
283 if (!mw
->menu
.old_stack
)
285 mw
->menu
.old_stack_length
= 10;
287 (widget_value
**)XtCalloc (mw
->menu
.old_stack_length
,
288 sizeof (widget_value
*));
290 else if (mw
->menu
.old_stack_length
< n
)
292 mw
->menu
.old_stack_length
*= 2;
294 (widget_value
**)XtRealloc ((char*)mw
->menu
.old_stack
,
295 mw
->menu
.old_stack_length
* sizeof (widget_value
*));
308 XTextExtents (mw
->menu
.font
, s
, strlen (s
), &drop
, &drop
, &drop
, &xcs
);
316 return (mw
->menu
.font
->ascent
* 3/4) | 1;
319 /* Return the width of toggle buttons of widget MW. */
322 toggle_button_width (mw
)
325 return ((mw
->menu
.font
->ascent
+ mw
->menu
.font
->descent
) * 2 / 3) | 1;
329 /* Return the width of radio buttons of widget MW. */
332 radio_button_width (mw
)
335 return toggle_button_width (mw
) * 1.41;
342 {"labelString", "LabelString", XtRString
, sizeof(String
),
347 resource_widget_value (mw
, val
)
351 if (!val
->toolkit_data
)
353 char* resourced_name
= NULL
;
355 XtGetSubresources ((Widget
) mw
,
356 (XtPointer
) &resourced_name
,
357 val
->name
, val
->name
,
358 nameResource
, 1, NULL
, 0);
360 resourced_name
= val
->name
;
363 complete_name
= (char *) XtMalloc (strlen (resourced_name
) + 1);
364 strcpy (complete_name
, resourced_name
);
368 int complete_length
=
369 strlen (resourced_name
) + strlen (val
->value
) + 2;
370 complete_name
= XtMalloc (complete_length
);
372 strcat (complete_name
, resourced_name
);
373 strcat (complete_name
, " ");
374 strcat (complete_name
, val
->value
);
377 val
->toolkit_data
= complete_name
;
378 val
->free_toolkit_data
= True
;
380 return (char*)val
->toolkit_data
;
383 /* Returns the sizes of an item */
385 size_menu_item (mw
, val
, horizontal_p
, label_width
, rest_width
, button_width
,
395 enum menu_separator separator
;
397 if (lw_separator_p (val
->name
, &separator
, 0))
399 *height
= separator_height (separator
);
407 mw
->menu
.font
->ascent
+ mw
->menu
.font
->descent
408 + 2 * mw
->menu
.vertical_spacing
+ 2 * mw
->menu
.shadow_thickness
;
411 string_width (mw
, resource_widget_value (mw
, val
))
412 + mw
->menu
.horizontal_spacing
+ mw
->menu
.shadow_thickness
;
414 *rest_width
= mw
->menu
.horizontal_spacing
+ mw
->menu
.shadow_thickness
;
418 /* Add width of the arrow displayed for submenus. */
419 *rest_width
+= arrow_width (mw
) + mw
->menu
.arrow_spacing
;
421 /* Add width of key equivalent string. */
422 *rest_width
+= (string_width (mw
, val
->key
)
423 + mw
->menu
.arrow_spacing
);
425 if (val
->button_type
== BUTTON_TYPE_TOGGLE
)
426 *button_width
= (toggle_button_width (mw
)
427 + mw
->menu
.horizontal_spacing
);
428 else if (val
->button_type
== BUTTON_TYPE_RADIO
)
429 *button_width
= (radio_button_width (mw
)
430 + mw
->menu
.horizontal_spacing
);
436 size_menu (mw
, level
)
440 unsigned int label_width
= 0;
442 int button_width
= 0;
443 int max_rest_width
= 0;
444 int max_button_width
= 0;
445 unsigned int height
= 0;
446 int horizontal_p
= mw
->menu
.horizontal
&& (level
== 0);
450 if (level
>= mw
->menu
.old_depth
)
451 abort_gracefully ((Widget
) mw
);
453 ws
= &mw
->menu
.windows
[level
];
457 ws
->button_width
= 0;
459 for (val
= mw
->menu
.old_stack
[level
]->contents
; val
; val
= val
->next
)
461 size_menu_item (mw
, val
, horizontal_p
, &label_width
, &rest_width
,
462 &button_width
, &height
);
465 ws
->width
+= label_width
+ rest_width
;
466 if (height
> ws
->height
)
471 if (label_width
> ws
->label_width
)
472 ws
->label_width
= label_width
;
473 if (rest_width
> max_rest_width
)
474 max_rest_width
= rest_width
;
475 if (button_width
> max_button_width
)
476 max_button_width
= button_width
;
477 ws
->height
+= height
;
482 ws
->label_width
= ws
->button_width
= 0;
485 ws
->width
= ws
->label_width
+ max_rest_width
+ max_button_width
;
486 ws
->button_width
= max_button_width
;
489 ws
->width
+= 2 * mw
->menu
.shadow_thickness
;
490 ws
->height
+= 2 * mw
->menu
.shadow_thickness
;
494 ws
->width
+= 2 * mw
->menu
.margin
;
495 ws
->height
+= 2 * mw
->menu
.margin
;
503 draw_arrow (mw
, window
, gc
, x
, y
, width
, down_p
)
512 Display
*dpy
= XtDisplay (mw
);
513 GC top_gc
= mw
->menu
.shadow_top_gc
;
514 GC bottom_gc
= mw
->menu
.shadow_bottom_gc
;
515 int thickness
= mw
->menu
.shadow_thickness
;
518 /* alpha = atan (0.5)
519 factor = (1 + sin (alpha)) / cos (alpha) */
520 double factor
= 1.62;
521 int thickness2
= thickness
* factor
;
523 y
+= (mw
->menu
.font
->ascent
+ mw
->menu
.font
->descent
- height
) / 2;
534 pt
[0].y
= y
+ height
;
535 pt
[1].x
= x
+ thickness
;
536 pt
[1].y
= y
+ height
- thickness2
;
537 pt
[2].x
= x
+ thickness2
;
538 pt
[2].y
= y
+ thickness2
;
541 XFillPolygon (dpy
, window
, top_gc
, pt
, 4, Convex
, CoordModeOrigin
);
545 pt
[1].x
= x
+ thickness
;
546 pt
[1].y
= y
+ thickness2
;
547 pt
[2].x
= x
+ width
- thickness2
;
548 pt
[2].y
= y
+ height
/ 2;
550 pt
[3].y
= y
+ height
/ 2;
551 XFillPolygon (dpy
, window
, top_gc
, pt
, 4, Convex
, CoordModeOrigin
);
554 pt
[0].y
= y
+ height
;
555 pt
[1].x
= x
+ thickness
;
556 pt
[1].y
= y
+ height
- thickness2
;
557 pt
[2].x
= x
+ width
- thickness2
;
558 pt
[2].y
= y
+ height
/ 2;
560 pt
[3].y
= y
+ height
/ 2;
561 XFillPolygon (dpy
, window
, bottom_gc
, pt
, 4, Convex
, CoordModeOrigin
);
567 draw_shadow_rectangle (mw
, window
, x
, y
, width
, height
, erase_p
, down_p
)
577 Display
*dpy
= XtDisplay (mw
);
578 GC top_gc
= !erase_p
? mw
->menu
.shadow_top_gc
: mw
->menu
.background_gc
;
579 GC bottom_gc
= !erase_p
? mw
->menu
.shadow_bottom_gc
: mw
->menu
.background_gc
;
580 int thickness
= mw
->menu
.shadow_thickness
;
583 if (!erase_p
&& down_p
)
593 points
[1].x
= x
+ width
;
595 points
[2].x
= x
+ width
- thickness
;
596 points
[2].y
= y
+ thickness
;
598 points
[3].y
= y
+ thickness
;
599 XFillPolygon (dpy
, window
, top_gc
, points
, 4, Convex
, CoordModeOrigin
);
601 points
[0].y
= y
+ thickness
;
603 points
[1].y
= y
+ height
;
604 points
[2].x
= x
+ thickness
;
605 points
[2].y
= y
+ height
- thickness
;
606 points
[3].x
= x
+ thickness
;
607 points
[3].y
= y
+ thickness
;
608 XFillPolygon (dpy
, window
, top_gc
, points
, 4, Convex
, CoordModeOrigin
);
609 points
[0].x
= x
+ width
;
611 points
[1].x
= x
+ width
- thickness
;
612 points
[1].y
= y
+ thickness
;
613 points
[2].x
= x
+ width
- thickness
;
614 points
[2].y
= y
+ height
- thickness
;
615 points
[3].x
= x
+ width
;
616 points
[3].y
= y
+ height
- thickness
;
617 XFillPolygon (dpy
, window
, bottom_gc
, points
, 4, Convex
, CoordModeOrigin
);
619 points
[0].y
= y
+ height
;
620 points
[1].x
= x
+ width
;
621 points
[1].y
= y
+ height
;
622 points
[2].x
= x
+ width
;
623 points
[2].y
= y
+ height
- thickness
;
624 points
[3].x
= x
+ thickness
;
625 points
[3].y
= y
+ height
- thickness
;
626 XFillPolygon (dpy
, window
, bottom_gc
, points
, 4, Convex
, CoordModeOrigin
);
631 draw_shadow_rhombus (mw
, window
, x
, y
, width
, height
, erase_p
, down_p
)
641 Display
*dpy
= XtDisplay (mw
);
642 GC top_gc
= !erase_p
? mw
->menu
.shadow_top_gc
: mw
->menu
.background_gc
;
643 GC bottom_gc
= !erase_p
? mw
->menu
.shadow_bottom_gc
: mw
->menu
.background_gc
;
644 int thickness
= mw
->menu
.shadow_thickness
;
647 if (!erase_p
&& down_p
)
656 points
[0].y
= y
+ height
/ 2;
657 points
[1].x
= x
+ thickness
;
658 points
[1].y
= y
+ height
/ 2;
659 points
[2].x
= x
+ width
/ 2;
660 points
[2].y
= y
+ thickness
;
661 points
[3].x
= x
+ width
/ 2;
663 XFillPolygon (dpy
, window
, top_gc
, points
, 4, Convex
, CoordModeOrigin
);
664 points
[0].x
= x
+ width
/ 2;
666 points
[1].x
= x
+ width
/ 2;
667 points
[1].y
= y
+ thickness
;
668 points
[2].x
= x
+ width
- thickness
;
669 points
[2].y
= y
+ height
/ 2;
670 points
[3].x
= x
+ width
;
671 points
[3].y
= y
+ height
/ 2;
672 XFillPolygon (dpy
, window
, top_gc
, points
, 4, Convex
, CoordModeOrigin
);
674 points
[0].y
= y
+ height
/ 2;
675 points
[1].x
= x
+ thickness
;
676 points
[1].y
= y
+ height
/ 2;
677 points
[2].x
= x
+ width
/ 2;
678 points
[2].y
= y
+ height
- thickness
;
679 points
[3].x
= x
+ width
/ 2;
680 points
[3].y
= y
+ height
;
681 XFillPolygon (dpy
, window
, bottom_gc
, points
, 4, Convex
, CoordModeOrigin
);
682 points
[0].x
= x
+ width
/ 2;
683 points
[0].y
= y
+ height
;
684 points
[1].x
= x
+ width
/ 2;
685 points
[1].y
= y
+ height
- thickness
;
686 points
[2].x
= x
+ width
- thickness
;
687 points
[2].y
= y
+ height
/ 2;
688 points
[3].x
= x
+ width
;
689 points
[3].y
= y
+ height
/ 2;
690 XFillPolygon (dpy
, window
, bottom_gc
, points
, 4, Convex
, CoordModeOrigin
);
694 /* Draw a toggle button on widget MW, X window WINDOW. X/Y is the
695 top-left corner of the menu item. SELECTED_P non-zero means the
696 toggle button is selected. */
699 draw_toggle (mw
, window
, x
, y
, selected_p
)
702 int x
, y
, selected_p
;
706 width
= toggle_button_width (mw
);
708 x
+= mw
->menu
.horizontal_spacing
;
709 y
+= (mw
->menu
.font
->ascent
- height
) / 2;
710 draw_shadow_rectangle (mw
, window
, x
, y
, width
, height
, False
, selected_p
);
714 /* Draw a radio button on widget MW, X window WINDOW. X/Y is the
715 top-left corner of the menu item. SELECTED_P non-zero means the
716 toggle button is selected. */
719 draw_radio (mw
, window
, x
, y
, selected_p
)
722 int x
, y
, selected_p
;
726 width
= radio_button_width (mw
);
728 x
+= mw
->menu
.horizontal_spacing
;
729 y
+= (mw
->menu
.font
->ascent
- height
) / 2;
730 draw_shadow_rhombus (mw
, window
, x
, y
, width
, height
, False
, selected_p
);
734 /* Draw a menu separator on widget MW, X window WINDOW. X/Y is the
735 top-left corner of the menu item. WIDTH is the width of the
736 separator to draw. TYPE is the separator type. */
739 draw_separator (mw
, window
, x
, y
, width
, type
)
743 enum menu_separator type
;
745 Display
*dpy
= XtDisplay (mw
);
750 case SEPARATOR_NO_LINE
:
753 case SEPARATOR_SINGLE_LINE
:
754 XDrawLine (dpy
, window
, mw
->menu
.foreground_gc
,
758 case SEPARATOR_DOUBLE_LINE
:
759 draw_separator (mw
, window
, x
, y
, width
, SEPARATOR_SINGLE_LINE
);
760 draw_separator (mw
, window
, x
, y
+ 2, width
, SEPARATOR_SINGLE_LINE
);
763 case SEPARATOR_SINGLE_DASHED_LINE
:
764 xgcv
.line_style
= LineOnOffDash
;
765 XChangeGC (dpy
, mw
->menu
.foreground_gc
, GCLineStyle
, &xgcv
);
766 XDrawLine (dpy
, window
, mw
->menu
.foreground_gc
,
768 xgcv
.line_style
= LineSolid
;
769 XChangeGC (dpy
, mw
->menu
.foreground_gc
, GCLineStyle
, &xgcv
);
772 case SEPARATOR_DOUBLE_DASHED_LINE
:
773 draw_separator (mw
, window
, x
, y
, width
,
774 SEPARATOR_SINGLE_DASHED_LINE
);
775 draw_separator (mw
, window
, x
, y
+ 2, width
,
776 SEPARATOR_SINGLE_DASHED_LINE
);
779 case SEPARATOR_SHADOW_ETCHED_IN
:
780 XDrawLine (dpy
, window
, mw
->menu
.shadow_bottom_gc
,
782 XDrawLine (dpy
, window
, mw
->menu
.shadow_top_gc
,
783 x
, y
+ 1, x
+ width
, y
+ 1);
786 case SEPARATOR_SHADOW_ETCHED_OUT
:
787 XDrawLine (dpy
, window
, mw
->menu
.shadow_top_gc
,
789 XDrawLine (dpy
, window
, mw
->menu
.shadow_bottom_gc
,
790 x
, y
+ 1, x
+ width
, y
+ 1);
793 case SEPARATOR_SHADOW_ETCHED_IN_DASH
:
794 xgcv
.line_style
= LineOnOffDash
;
795 XChangeGC (dpy
, mw
->menu
.shadow_bottom_gc
, GCLineStyle
, &xgcv
);
796 XChangeGC (dpy
, mw
->menu
.shadow_top_gc
, GCLineStyle
, &xgcv
);
797 draw_separator (mw
, window
, x
, y
, SEPARATOR_SHADOW_ETCHED_IN
);
798 xgcv
.line_style
= LineSolid
;
799 XChangeGC (dpy
, mw
->menu
.shadow_bottom_gc
, GCLineStyle
, &xgcv
);
800 XChangeGC (dpy
, mw
->menu
.shadow_top_gc
, GCLineStyle
, &xgcv
);
803 case SEPARATOR_SHADOW_ETCHED_OUT_DASH
:
804 xgcv
.line_style
= LineOnOffDash
;
805 XChangeGC (dpy
, mw
->menu
.shadow_bottom_gc
, GCLineStyle
, &xgcv
);
806 XChangeGC (dpy
, mw
->menu
.shadow_top_gc
, GCLineStyle
, &xgcv
);
807 draw_separator (mw
, window
, x
, y
, SEPARATOR_SHADOW_ETCHED_OUT
);
808 xgcv
.line_style
= LineSolid
;
809 XChangeGC (dpy
, mw
->menu
.shadow_bottom_gc
, GCLineStyle
, &xgcv
);
810 XChangeGC (dpy
, mw
->menu
.shadow_top_gc
, GCLineStyle
, &xgcv
);
813 case SEPARATOR_SHADOW_DOUBLE_ETCHED_IN
:
814 draw_separator (mw
, window
, x
, y
, width
, SEPARATOR_SHADOW_ETCHED_IN
);
815 draw_separator (mw
, window
, x
, y
+ 3, width
, SEPARATOR_SHADOW_ETCHED_IN
);
818 case SEPARATOR_SHADOW_DOUBLE_ETCHED_OUT
:
819 draw_separator (mw
, window
, x
, y
, width
,
820 SEPARATOR_SHADOW_ETCHED_OUT
);
821 draw_separator (mw
, window
, x
, y
+ 3, width
,
822 SEPARATOR_SHADOW_ETCHED_OUT
);
825 case SEPARATOR_SHADOW_DOUBLE_ETCHED_IN_DASH
:
826 xgcv
.line_style
= LineOnOffDash
;
827 XChangeGC (dpy
, mw
->menu
.shadow_bottom_gc
, GCLineStyle
, &xgcv
);
828 XChangeGC (dpy
, mw
->menu
.shadow_top_gc
, GCLineStyle
, &xgcv
);
829 draw_separator (mw
, window
, x
, y
, width
,
830 SEPARATOR_SHADOW_DOUBLE_ETCHED_IN
);
831 xgcv
.line_style
= LineSolid
;
832 XChangeGC (dpy
, mw
->menu
.shadow_bottom_gc
, GCLineStyle
, &xgcv
);
833 XChangeGC (dpy
, mw
->menu
.shadow_top_gc
, GCLineStyle
, &xgcv
);
836 case SEPARATOR_SHADOW_DOUBLE_ETCHED_OUT_DASH
:
837 xgcv
.line_style
= LineOnOffDash
;
838 XChangeGC (dpy
, mw
->menu
.shadow_bottom_gc
, GCLineStyle
, &xgcv
);
839 XChangeGC (dpy
, mw
->menu
.shadow_top_gc
, GCLineStyle
, &xgcv
);
840 draw_separator (mw
, window
, x
, y
, width
,
841 SEPARATOR_SHADOW_DOUBLE_ETCHED_OUT
);
842 xgcv
.line_style
= LineSolid
;
843 XChangeGC (dpy
, mw
->menu
.shadow_bottom_gc
, GCLineStyle
, &xgcv
);
844 XChangeGC (dpy
, mw
->menu
.shadow_top_gc
, GCLineStyle
, &xgcv
);
853 /* Return the pixel height of menu separator SEPARATOR. */
856 separator_height (separator
)
857 enum menu_separator separator
;
861 case SEPARATOR_NO_LINE
:
864 case SEPARATOR_SINGLE_LINE
:
865 case SEPARATOR_SINGLE_DASHED_LINE
:
868 case SEPARATOR_DOUBLE_LINE
:
869 case SEPARATOR_DOUBLE_DASHED_LINE
:
872 case SEPARATOR_SHADOW_ETCHED_IN
:
873 case SEPARATOR_SHADOW_ETCHED_OUT
:
874 case SEPARATOR_SHADOW_ETCHED_IN_DASH
:
875 case SEPARATOR_SHADOW_ETCHED_OUT_DASH
:
878 case SEPARATOR_SHADOW_DOUBLE_ETCHED_IN
:
879 case SEPARATOR_SHADOW_DOUBLE_ETCHED_OUT
:
880 case SEPARATOR_SHADOW_DOUBLE_ETCHED_IN_DASH
:
881 case SEPARATOR_SHADOW_DOUBLE_ETCHED_OUT_DASH
:
890 /* Display the menu item and increment where.x and where.y to show how large
891 the menu item was. */
894 display_menu_item (mw
, val
, ws
, where
, highlighted_p
, horizontal_p
,
900 Boolean highlighted_p
;
901 Boolean horizontal_p
;
902 Boolean just_compute_p
;
906 int font_ascent
= mw
->menu
.font
->ascent
;
907 int font_descent
= mw
->menu
.font
->descent
;
908 int shadow
= mw
->menu
.shadow_thickness
;
909 int margin
= mw
->menu
.margin
;
910 int h_spacing
= mw
->menu
.horizontal_spacing
;
911 int v_spacing
= mw
->menu
.vertical_spacing
;
917 enum menu_separator separator
;
918 int separator_p
= lw_separator_p (val
->name
, &separator
, 0);
920 /* compute the sizes of the item */
921 size_menu_item (mw
, val
, horizontal_p
, &label_width
, &rest_width
,
922 &button_width
, &height
);
925 width
= label_width
+ rest_width
;
928 label_width
= ws
->label_width
;
929 width
= ws
->width
- 2 * shadow
;
932 /* Only highlight an enabled item that has a callback. */
934 if (!val
->enabled
|| !(val
->call_data
|| val
->contents
))
937 /* do the drawing. */
940 /* Add the shadow border of the containing menu */
941 int x
= where
->x
+ shadow
;
942 int y
= where
->y
+ shadow
;
950 /* pick the foreground and background GC. */
952 text_gc
= mw
->menu
.foreground_gc
;
954 text_gc
= mw
->menu
.inactive_gc
;
955 deco_gc
= mw
->menu
.foreground_gc
;
959 draw_separator (mw
, ws
->window
, x
, y
, width
, separator
);
963 int x_offset
= x
+ h_spacing
+ shadow
;
964 char* display_string
= resource_widget_value (mw
, val
);
965 draw_shadow_rectangle (mw
, ws
->window
, x
, y
, width
, height
, True
,
968 /* Deal with centering a menu title. */
969 if (!horizontal_p
&& !val
->contents
&& !val
->call_data
)
971 int l
= string_width (mw
, display_string
);
974 x_offset
= (width
- l
) >> 1;
976 else if (!horizontal_p
&& ws
->button_width
)
977 x_offset
+= ws
->button_width
;
980 XDrawString (XtDisplay (mw
), ws
->window
, text_gc
, x_offset
,
981 y
+ v_spacing
+ shadow
+ font_ascent
,
982 display_string
, strlen (display_string
));
986 if (val
->button_type
== BUTTON_TYPE_TOGGLE
)
987 draw_toggle (mw
, ws
->window
, x
, y
+ v_spacing
+ shadow
,
989 else if (val
->button_type
== BUTTON_TYPE_RADIO
)
990 draw_radio (mw
, ws
->window
, x
, y
+ v_spacing
+ shadow
,
995 int a_w
= arrow_width (mw
);
996 draw_arrow (mw
, ws
->window
, deco_gc
,
998 - mw
->menu
.horizontal_spacing
999 - mw
->menu
.shadow_thickness
,
1000 y
+ v_spacing
+ shadow
, a_w
,
1005 XDrawString (XtDisplay (mw
), ws
->window
, text_gc
,
1006 x
+ label_width
+ mw
->menu
.arrow_spacing
,
1007 y
+ v_spacing
+ shadow
+ font_ascent
,
1008 val
->key
, strlen (val
->key
));
1013 XDrawRectangle (XtDisplay (mw
), ws
->window
,
1014 mw
->menu
.background_gc
,
1015 x
+ shadow
, y
+ shadow
,
1016 label_width
+ h_spacing
- 1,
1017 font_ascent
+ font_descent
+ 2 * v_spacing
- 1);
1018 draw_shadow_rectangle (mw
, ws
->window
, x
, y
, width
, height
,
1023 draw_shadow_rectangle (mw
, ws
->window
, x
, y
, width
, height
, False
,
1033 display_menu (mw
, level
, just_compute_p
, highlighted_pos
, hit
, hit_return
,
1037 Boolean just_compute_p
;
1038 XPoint
* highlighted_pos
;
1040 widget_value
** hit_return
;
1045 widget_value
* following_item
;
1048 int horizontal_p
= mw
->menu
.horizontal
&& (level
== 0);
1050 int just_compute_this_one_p
;
1051 /* This is set nonzero if the element containing HIGHLIGHTED_POS
1052 is disabled, so that we do not return any subsequent element either. */
1054 enum menu_separator separator
;
1056 if (level
>= mw
->menu
.old_depth
)
1057 abort_gracefully ((Widget
) mw
);
1059 if (level
< mw
->menu
.old_depth
- 1)
1060 following_item
= mw
->menu
.old_stack
[level
+ 1];
1062 following_item
= NULL
;
1070 ws
= &mw
->menu
.windows
[level
];
1071 for (val
= mw
->menu
.old_stack
[level
]->contents
; val
; val
= val
->next
)
1073 highlighted_p
= val
== following_item
;
1074 if (highlighted_p
&& highlighted_pos
)
1077 highlighted_pos
->x
= where
.x
;
1079 highlighted_pos
->y
= where
.y
;
1082 just_compute_this_one_p
=
1083 just_compute_p
|| ((this || that
) && val
!= this && val
!= that
);
1085 display_menu_item (mw
, val
, ws
, &where
, highlighted_p
, horizontal_p
,
1086 just_compute_this_one_p
);
1088 if (highlighted_p
&& highlighted_pos
)
1091 highlighted_pos
->y
= where
.y
;
1093 highlighted_pos
->x
= where
.x
;
1098 && (horizontal_p
? hit
->x
< where
.x
: hit
->y
< where
.y
)
1099 && !lw_separator_p (val
->name
, &separator
, 0)
1114 if (!just_compute_p
)
1115 draw_shadow_rectangle (mw
, ws
->window
, 0, 0, ws
->width
, ws
->height
,
1121 set_new_state (mw
, val
, level
)
1128 mw
->menu
.new_depth
= 0;
1129 for (i
= 0; i
< level
; i
++)
1130 push_new_stack (mw
, mw
->menu
.old_stack
[i
]);
1131 push_new_stack (mw
, val
);
1135 make_windows_if_needed (mw
, n
)
1141 XSetWindowAttributes xswa
;
1143 Window root
= RootWindowOfScreen (DefaultScreenOfDisplay (XtDisplay (mw
)));
1144 window_state
* windows
;
1146 if (mw
->menu
.windows_length
>= n
)
1149 xswa
.save_under
= True
;
1150 xswa
.override_redirect
= True
;
1151 xswa
.background_pixel
= mw
->core
.background_pixel
;
1152 xswa
.border_pixel
= mw
->core
.border_pixel
;
1154 ExposureMask
| PointerMotionMask
| PointerMotionHintMask
1155 | ButtonReleaseMask
| ButtonPressMask
;
1156 xswa
.cursor
= mw
->menu
.cursor_shape
;
1157 mask
= CWSaveUnder
| CWOverrideRedirect
| CWBackPixel
| CWBorderPixel
1158 | CWEventMask
| CWCursor
;
1160 if (!mw
->menu
.windows
)
1163 (window_state
*)XtMalloc (n
* sizeof (window_state
));
1169 (window_state
*)XtRealloc ((char*)mw
->menu
.windows
,
1170 n
* sizeof (window_state
));
1171 start_at
= mw
->menu
.windows_length
;
1173 mw
->menu
.windows_length
= n
;
1175 windows
= mw
->menu
.windows
;
1177 for (i
= start_at
; i
< n
; i
++)
1181 windows
[i
].width
= 1;
1182 windows
[i
].height
= 1;
1183 windows
[i
].window
=
1184 XCreateWindow (XtDisplay (mw
), root
, 0, 0, 1, 1,
1185 0, 0, CopyFromParent
, CopyFromParent
, mask
, &xswa
);
1189 /* Make the window fit in the screen */
1191 fit_to_screen (mw
, ws
, previous_ws
, horizontal_p
)
1194 window_state
* previous_ws
;
1195 Boolean horizontal_p
;
1197 unsigned int screen_width
= WidthOfScreen (XtScreen (mw
));
1198 unsigned int screen_height
= HeightOfScreen (XtScreen (mw
));
1199 /* 1 if we are unable to avoid an overlap between
1200 this menu and the parent menu in the X dimension. */
1201 int horizontal_overlap
= 0;
1205 else if (ws
->x
+ ws
->width
> screen_width
)
1208 ws
->x
= previous_ws
->x
- ws
->width
;
1210 ws
->x
= screen_width
- ws
->width
;
1214 horizontal_overlap
= 1;
1217 /* If we overlap in X, try to avoid overlap in Y. */
1218 if (horizontal_overlap
1219 && ws
->y
< previous_ws
->y
+ previous_ws
->height
1220 && previous_ws
->y
< ws
->y
+ ws
->height
)
1222 /* Put this menu right below or right above PREVIOUS_WS
1224 if (previous_ws
->y
+ previous_ws
->height
+ ws
->height
< screen_height
)
1225 ws
->y
= previous_ws
->y
+ previous_ws
->height
;
1226 else if (previous_ws
->y
- ws
->height
> 0)
1227 ws
->y
= previous_ws
->y
- ws
->height
;
1232 else if (ws
->y
+ ws
->height
> screen_height
)
1235 ws
->y
= previous_ws
->y
- ws
->height
;
1237 ws
->y
= screen_height
- ws
->height
;
1243 /* Updates old_stack from new_stack and redisplays. */
1250 XPoint selection_position
;
1251 int old_depth
= mw
->menu
.old_depth
;
1252 int new_depth
= mw
->menu
.new_depth
;
1253 widget_value
** old_stack
;
1254 widget_value
** new_stack
;
1255 window_state
* windows
;
1256 widget_value
* old_selection
;
1257 widget_value
* new_selection
;
1259 /* Check that enough windows and old_stack are ready. */
1260 make_windows_if_needed (mw
, new_depth
);
1261 make_old_stack_space (mw
, new_depth
);
1262 windows
= mw
->menu
.windows
;
1263 old_stack
= mw
->menu
.old_stack
;
1264 new_stack
= mw
->menu
.new_stack
;
1266 /* compute the last identical different entry */
1267 for (i
= 1; i
< old_depth
&& i
< new_depth
; i
++)
1268 if (old_stack
[i
] != new_stack
[i
])
1272 /* Memorize the previously selected item to be able to refresh it */
1273 old_selection
= last_same
+ 1 < old_depth
? old_stack
[last_same
+ 1] : NULL
;
1274 if (old_selection
&& !old_selection
->enabled
)
1275 old_selection
= NULL
;
1276 new_selection
= last_same
+ 1 < new_depth
? new_stack
[last_same
+ 1] : NULL
;
1277 if (new_selection
&& !new_selection
->enabled
)
1278 new_selection
= NULL
;
1280 /* Call callback when the hightlighted item changes. */
1281 if (old_selection
|| new_selection
)
1282 XtCallCallbackList ((Widget
)mw
, mw
->menu
.highlight
,
1283 (XtPointer
) new_selection
);
1285 /* updates old_state from new_state. It has to be done now because
1286 display_menu (called below) uses the old_stack to know what to display. */
1287 for (i
= last_same
+ 1; i
< new_depth
; i
++)
1288 old_stack
[i
] = new_stack
[i
];
1289 mw
->menu
.old_depth
= new_depth
;
1291 /* refresh the last selection */
1292 selection_position
.x
= 0;
1293 selection_position
.y
= 0;
1294 display_menu (mw
, last_same
, new_selection
== old_selection
,
1295 &selection_position
, NULL
, NULL
, old_selection
, new_selection
);
1297 /* Now place the new menus. */
1298 for (i
= last_same
+ 1; i
< new_depth
&& new_stack
[i
]->contents
; i
++)
1300 window_state
*previous_ws
= &windows
[i
- 1];
1301 window_state
*ws
= &windows
[i
];
1303 ws
->x
= (previous_ws
->x
+ selection_position
.x
1304 + mw
->menu
.shadow_thickness
);
1306 ws
->x
+= mw
->menu
.margin
;
1309 if (!mw
->menu
.horizontal
|| i
> 1)
1310 ws
->x
+= mw
->menu
.shadow_thickness
;
1313 ws
->y
= (previous_ws
->y
+ selection_position
.y
1314 + mw
->menu
.shadow_thickness
);
1316 ws
->y
+= mw
->menu
.margin
;
1320 fit_to_screen (mw
, ws
, previous_ws
, mw
->menu
.horizontal
&& i
== 1);
1322 XClearWindow (XtDisplay (mw
), ws
->window
);
1323 XMoveResizeWindow (XtDisplay (mw
), ws
->window
, ws
->x
, ws
->y
,
1324 ws
->width
, ws
->height
);
1325 XMapRaised (XtDisplay (mw
), ws
->window
);
1326 display_menu (mw
, i
, False
, &selection_position
, NULL
, NULL
, NULL
, NULL
);
1329 /* unmap the menus that popped down */
1330 for (i
= new_depth
- 1; i
< old_depth
; i
++)
1331 if (i
>= new_depth
|| !new_stack
[i
]->contents
)
1332 XUnmapWindow (XtDisplay (mw
), windows
[i
].window
);
1336 motion_event_is_in_menu (mw
, ev
, level
, relative_pos
)
1340 XPoint
* relative_pos
;
1342 window_state
* ws
= &mw
->menu
.windows
[level
];
1343 int shadow
= level
== 0 ? 0 : mw
->menu
.shadow_thickness
;
1344 int x
= ws
->x
+ shadow
;
1345 int y
= ws
->y
+ shadow
;
1346 relative_pos
->x
= ev
->x_root
- x
;
1347 relative_pos
->y
= ev
->y_root
- y
;
1348 return (x
- shadow
< ev
->x_root
&& ev
->x_root
< x
+ ws
->width
1349 && y
- shadow
< ev
->y_root
&& ev
->y_root
< y
+ ws
->height
);
1353 map_event_to_widget_value (mw
, ev
, val
, level
)
1360 XPoint relative_pos
;
1365 /* Find the window */
1366 for (i
= mw
->menu
.old_depth
- 1; i
>= 0; i
--)
1368 ws
= &mw
->menu
.windows
[i
];
1369 if (ws
&& motion_event_is_in_menu (mw
, ev
, i
, &relative_pos
))
1371 display_menu (mw
, i
, True
, NULL
, &relative_pos
, val
, NULL
, NULL
);
1385 make_drawing_gcs (mw
)
1390 xgcv
.font
= mw
->menu
.font
->fid
;
1391 xgcv
.foreground
= mw
->menu
.foreground
;
1392 xgcv
.background
= mw
->core
.background_pixel
;
1393 mw
->menu
.foreground_gc
= XtGetGC ((Widget
)mw
,
1394 GCFont
| GCForeground
| GCBackground
,
1397 xgcv
.font
= mw
->menu
.font
->fid
;
1398 xgcv
.foreground
= mw
->menu
.button_foreground
;
1399 xgcv
.background
= mw
->core
.background_pixel
;
1400 mw
->menu
.button_gc
= XtGetGC ((Widget
)mw
,
1401 GCFont
| GCForeground
| GCBackground
,
1404 xgcv
.font
= mw
->menu
.font
->fid
;
1405 xgcv
.foreground
= mw
->menu
.foreground
;
1406 xgcv
.background
= mw
->core
.background_pixel
;
1407 xgcv
.fill_style
= FillStippled
;
1408 xgcv
.stipple
= mw
->menu
.gray_pixmap
;
1409 mw
->menu
.inactive_gc
= XtGetGC ((Widget
)mw
,
1410 (GCFont
| GCForeground
| GCBackground
1411 | GCFillStyle
| GCStipple
), &xgcv
);
1413 xgcv
.font
= mw
->menu
.font
->fid
;
1414 xgcv
.foreground
= mw
->menu
.button_foreground
;
1415 xgcv
.background
= mw
->core
.background_pixel
;
1416 xgcv
.fill_style
= FillStippled
;
1417 xgcv
.stipple
= mw
->menu
.gray_pixmap
;
1418 mw
->menu
.inactive_button_gc
= XtGetGC ((Widget
)mw
,
1419 (GCFont
| GCForeground
| GCBackground
1420 | GCFillStyle
| GCStipple
), &xgcv
);
1422 xgcv
.font
= mw
->menu
.font
->fid
;
1423 xgcv
.foreground
= mw
->core
.background_pixel
;
1424 xgcv
.background
= mw
->menu
.foreground
;
1425 mw
->menu
.background_gc
= XtGetGC ((Widget
)mw
,
1426 GCFont
| GCForeground
| GCBackground
,
1431 release_drawing_gcs (mw
)
1434 XtReleaseGC ((Widget
) mw
, mw
->menu
.foreground_gc
);
1435 XtReleaseGC ((Widget
) mw
, mw
->menu
.button_gc
);
1436 XtReleaseGC ((Widget
) mw
, mw
->menu
.inactive_gc
);
1437 XtReleaseGC ((Widget
) mw
, mw
->menu
.inactive_button_gc
);
1438 XtReleaseGC ((Widget
) mw
, mw
->menu
.background_gc
);
1439 /* let's get some segvs if we try to use these... */
1440 mw
->menu
.foreground_gc
= (GC
) -1;
1441 mw
->menu
.button_gc
= (GC
) -1;
1442 mw
->menu
.inactive_gc
= (GC
) -1;
1443 mw
->menu
.inactive_button_gc
= (GC
) -1;
1444 mw
->menu
.background_gc
= (GC
) -1;
1447 #define MINL(x,y) ((((unsigned long) (x)) < ((unsigned long) (y))) \
1448 ? ((unsigned long) (x)) : ((unsigned long) (y)))
1451 make_shadow_gcs (mw
)
1455 unsigned long pm
= 0;
1456 Display
*dpy
= XtDisplay ((Widget
) mw
);
1457 Screen
*screen
= XtScreen ((Widget
) mw
);
1458 Colormap cmap
= mw
->core
.colormap
;
1460 int top_frobbed
= 0, bottom_frobbed
= 0;
1462 mw
->menu
.free_top_shadow_color_p
= 0;
1463 mw
->menu
.free_bottom_shadow_color_p
= 0;
1465 if (mw
->menu
.top_shadow_color
== -1)
1466 mw
->menu
.top_shadow_color
= mw
->core
.background_pixel
;
1468 mw
->menu
.top_shadow_color
= mw
->menu
.top_shadow_color
;
1470 if (mw
->menu
.bottom_shadow_color
== -1)
1471 mw
->menu
.bottom_shadow_color
= mw
->menu
.foreground
;
1473 mw
->menu
.bottom_shadow_color
= mw
->menu
.bottom_shadow_color
;
1475 if (mw
->menu
.top_shadow_color
== mw
->core
.background_pixel
||
1476 mw
->menu
.top_shadow_color
== mw
->menu
.foreground
)
1478 topc
.pixel
= mw
->core
.background_pixel
;
1479 XQueryColor (dpy
, cmap
, &topc
);
1480 /* don't overflow/wrap! */
1481 topc
.red
= MINL (65535, topc
.red
* 1.2);
1482 topc
.green
= MINL (65535, topc
.green
* 1.2);
1483 topc
.blue
= MINL (65535, topc
.blue
* 1.2);
1485 if (x_alloc_nearest_color_for_widget ((Widget
) mw
, cmap
, &topc
))
1487 if (XAllocColor (dpy
, cmap
, &topc
))
1490 mw
->menu
.top_shadow_color
= topc
.pixel
;
1491 mw
->menu
.free_top_shadow_color_p
= 1;
1495 if (mw
->menu
.bottom_shadow_color
== mw
->menu
.foreground
||
1496 mw
->menu
.bottom_shadow_color
== mw
->core
.background_pixel
)
1498 botc
.pixel
= mw
->core
.background_pixel
;
1499 XQueryColor (dpy
, cmap
, &botc
);
1504 if (x_alloc_nearest_color_for_widget ((Widget
) mw
, cmap
, &botc
))
1506 if (XAllocColor (dpy
, cmap
, &botc
))
1509 mw
->menu
.bottom_shadow_color
= botc
.pixel
;
1510 mw
->menu
.free_bottom_shadow_color_p
= 1;
1515 if (top_frobbed
&& bottom_frobbed
)
1517 int top_avg
= ((topc
.red
/ 3) + (topc
.green
/ 3) + (topc
.blue
/ 3));
1518 int bot_avg
= ((botc
.red
/ 3) + (botc
.green
/ 3) + (botc
.blue
/ 3));
1519 if (bot_avg
> top_avg
)
1521 Pixel tmp
= mw
->menu
.top_shadow_color
;
1522 mw
->menu
.top_shadow_color
= mw
->menu
.bottom_shadow_color
;
1523 mw
->menu
.bottom_shadow_color
= tmp
;
1525 else if (topc
.pixel
== botc
.pixel
)
1527 if (botc
.pixel
== mw
->menu
.foreground
)
1529 if (mw
->menu
.free_top_shadow_color_p
)
1531 x_free_dpy_colors (dpy
, screen
, cmap
,
1532 &mw
->menu
.top_shadow_color
, 1);
1533 mw
->menu
.free_top_shadow_color_p
= 0;
1535 mw
->menu
.top_shadow_color
= mw
->core
.background_pixel
;
1539 if (mw
->menu
.free_bottom_shadow_color_p
)
1541 x_free_dpy_colors (dpy
, screen
, cmap
,
1542 &mw
->menu
.bottom_shadow_color
, 1);
1543 mw
->menu
.free_bottom_shadow_color_p
= 0;
1545 mw
->menu
.bottom_shadow_color
= mw
->menu
.foreground
;
1550 if (!mw
->menu
.top_shadow_pixmap
&&
1551 mw
->menu
.top_shadow_color
== mw
->core
.background_pixel
)
1553 mw
->menu
.top_shadow_pixmap
= mw
->menu
.gray_pixmap
;
1554 if (mw
->menu
.free_top_shadow_color_p
)
1556 x_free_dpy_colors (dpy
, screen
, cmap
, &mw
->menu
.top_shadow_color
, 1);
1557 mw
->menu
.free_top_shadow_color_p
= 0;
1559 mw
->menu
.top_shadow_color
= mw
->menu
.foreground
;
1561 if (!mw
->menu
.bottom_shadow_pixmap
&&
1562 mw
->menu
.bottom_shadow_color
== mw
->core
.background_pixel
)
1564 mw
->menu
.bottom_shadow_pixmap
= mw
->menu
.gray_pixmap
;
1565 if (mw
->menu
.free_bottom_shadow_color_p
)
1567 x_free_dpy_colors (dpy
, screen
, cmap
,
1568 &mw
->menu
.bottom_shadow_color
, 1);
1569 mw
->menu
.free_bottom_shadow_color_p
= 0;
1571 mw
->menu
.bottom_shadow_color
= mw
->menu
.foreground
;
1574 xgcv
.fill_style
= FillStippled
;
1575 xgcv
.foreground
= mw
->menu
.top_shadow_color
;
1576 xgcv
.stipple
= mw
->menu
.top_shadow_pixmap
;
1577 pm
= (xgcv
.stipple
? GCStipple
|GCFillStyle
: 0);
1578 mw
->menu
.shadow_top_gc
= XtGetGC ((Widget
)mw
, GCForeground
| pm
, &xgcv
);
1580 xgcv
.foreground
= mw
->menu
.bottom_shadow_color
;
1581 xgcv
.stipple
= mw
->menu
.bottom_shadow_pixmap
;
1582 pm
= (xgcv
.stipple
? GCStipple
|GCFillStyle
: 0);
1583 mw
->menu
.shadow_bottom_gc
= XtGetGC ((Widget
)mw
, GCForeground
| pm
, &xgcv
);
1588 release_shadow_gcs (mw
)
1591 Display
*dpy
= XtDisplay ((Widget
) mw
);
1592 Screen
*screen
= XtScreen ((Widget
) mw
);
1593 Colormap cmap
= mw
->core
.colormap
;
1597 if (mw
->menu
.free_top_shadow_color_p
)
1598 px
[i
++] = mw
->menu
.top_shadow_color
;
1599 if (mw
->menu
.free_bottom_shadow_color_p
)
1600 px
[i
++] = mw
->menu
.bottom_shadow_color
;
1602 x_free_dpy_colors (dpy
, screen
, cmap
, px
, i
);
1604 XtReleaseGC ((Widget
) mw
, mw
->menu
.shadow_top_gc
);
1605 XtReleaseGC ((Widget
) mw
, mw
->menu
.shadow_bottom_gc
);
1609 XlwMenuInitialize (request
, mw
, args
, num_args
)
1615 /* Get the GCs and the widget size */
1616 XSetWindowAttributes xswa
;
1619 Window window
= RootWindowOfScreen (DefaultScreenOfDisplay (XtDisplay (mw
)));
1620 Display
* display
= XtDisplay (mw
);
1623 widget_value
*tem
= (widget_value
*) XtMalloc (sizeof (widget_value
));
1625 /* _XtCreate is freeing the object that was passed to us,
1626 so make a copy that we will actually keep. */
1627 lwlib_bcopy (mw
->menu
.contents
, tem
, sizeof (widget_value
));
1628 mw
->menu
.contents
= tem
;
1631 /* mw->menu.cursor = XCreateFontCursor (display, mw->menu.cursor_shape); */
1632 mw
->menu
.cursor
= mw
->menu
.cursor_shape
;
1634 mw
->menu
.gray_pixmap
1635 = XCreatePixmapFromBitmapData (display
, window
, gray_bitmap_bits
,
1636 gray_bitmap_width
, gray_bitmap_height
,
1637 (unsigned long)1, (unsigned long)0, 1);
1639 /* I don't understand why this ends up 0 sometimes,
1640 but it does. This kludge works around it.
1641 Can anyone find a real fix? -- rms. */
1642 if (mw
->menu
.font
== 0)
1643 mw
->menu
.font
= xlwmenu_default_font
;
1645 make_drawing_gcs (mw
);
1646 make_shadow_gcs (mw
);
1648 xswa
.background_pixel
= mw
->core
.background_pixel
;
1649 xswa
.border_pixel
= mw
->core
.border_pixel
;
1650 mask
= CWBackPixel
| CWBorderPixel
;
1652 mw
->menu
.popped_up
= False
;
1654 mw
->menu
.old_depth
= 1;
1655 mw
->menu
.old_stack
= (widget_value
**)XtMalloc (sizeof (widget_value
*));
1656 mw
->menu
.old_stack_length
= 1;
1657 mw
->menu
.old_stack
[0] = mw
->menu
.contents
;
1659 mw
->menu
.new_depth
= 0;
1660 mw
->menu
.new_stack
= 0;
1661 mw
->menu
.new_stack_length
= 0;
1662 push_new_stack (mw
, mw
->menu
.contents
);
1664 mw
->menu
.windows
= (window_state
*)XtMalloc (sizeof (window_state
));
1665 mw
->menu
.windows_length
= 1;
1666 mw
->menu
.windows
[0].x
= 0;
1667 mw
->menu
.windows
[0].y
= 0;
1668 mw
->menu
.windows
[0].width
= 0;
1669 mw
->menu
.windows
[0].height
= 0;
1672 mw
->core
.width
= mw
->menu
.windows
[0].width
;
1673 mw
->core
.height
= mw
->menu
.windows
[0].height
;
1677 XlwMenuClassInitialize ()
1682 XlwMenuRealize (w
, valueMask
, attributes
)
1685 XSetWindowAttributes
*attributes
;
1687 XlwMenuWidget mw
= (XlwMenuWidget
)w
;
1688 XSetWindowAttributes xswa
;
1691 (*xlwMenuWidgetClass
->core_class
.superclass
->core_class
.realize
)
1692 (w
, valueMask
, attributes
);
1694 xswa
.save_under
= True
;
1695 xswa
.cursor
= mw
->menu
.cursor_shape
;
1696 mask
= CWSaveUnder
| CWCursor
;
1697 XChangeWindowAttributes (XtDisplay (w
), XtWindow (w
), mask
, &xswa
);
1699 mw
->menu
.windows
[0].window
= XtWindow (w
);
1700 mw
->menu
.windows
[0].x
= w
->core
.x
;
1701 mw
->menu
.windows
[0].y
= w
->core
.y
;
1702 mw
->menu
.windows
[0].width
= w
->core
.width
;
1703 mw
->menu
.windows
[0].height
= w
->core
.height
;
1706 /* Only the toplevel menubar/popup is a widget so it's the only one that
1707 receives expose events through Xt. So we repaint all the other panes
1708 when receiving an Expose event. */
1710 XlwMenuRedisplay (w
, ev
, region
)
1715 XlwMenuWidget mw
= (XlwMenuWidget
)w
;
1718 /* If we have a depth beyond 1, it's because a submenu was displayed.
1719 If the submenu has been destroyed, set the depth back to 1. */
1720 if (submenu_destroyed
)
1722 mw
->menu
.old_depth
= 1;
1723 submenu_destroyed
= 0;
1726 for (i
= 0; i
< mw
->menu
.old_depth
; i
++)
1727 display_menu (mw
, i
, False
, NULL
, NULL
, NULL
, NULL
, NULL
);
1735 XlwMenuWidget mw
= (XlwMenuWidget
) w
;
1737 if (pointer_grabbed
)
1738 XtUngrabPointer ((Widget
)w
, CurrentTime
);
1739 pointer_grabbed
= 0;
1741 submenu_destroyed
= 1;
1743 release_drawing_gcs (mw
);
1744 release_shadow_gcs (mw
);
1746 /* this doesn't come from the resource db but is created explicitly
1747 so we must free it ourselves. */
1748 XFreePixmap (XtDisplay (mw
), mw
->menu
.gray_pixmap
);
1749 mw
->menu
.gray_pixmap
= (Pixmap
) -1;
1752 /* Do free mw->menu.contents because nowadays we copy it
1753 during initialization. */
1754 XtFree (mw
->menu
.contents
);
1757 /* Don't free mw->menu.contents because that comes from our creator.
1758 The `*_stack' elements are just pointers into `contents' so leave
1759 that alone too. But free the stacks themselves. */
1760 if (mw
->menu
.old_stack
) XtFree ((char *) mw
->menu
.old_stack
);
1761 if (mw
->menu
.new_stack
) XtFree ((char *) mw
->menu
.new_stack
);
1763 /* Remember, you can't free anything that came from the resource
1764 database. This includes:
1766 mw->menu.top_shadow_pixmap
1767 mw->menu.bottom_shadow_pixmap
1769 Also the color cells of top_shadow_color, bottom_shadow_color,
1770 foreground, and button_foreground will never be freed until this
1771 client exits. Nice, eh?
1774 /* start from 1 because the one in slot 0 is w->core.window */
1775 for (i
= 1; i
< mw
->menu
.windows_length
; i
++)
1776 XDestroyWindow (XtDisplay (mw
), mw
->menu
.windows
[i
].window
);
1777 if (mw
->menu
.windows
)
1778 XtFree ((char *) mw
->menu
.windows
);
1782 XlwMenuSetValues (current
, request
, new)
1787 XlwMenuWidget oldmw
= (XlwMenuWidget
)current
;
1788 XlwMenuWidget newmw
= (XlwMenuWidget
)new;
1789 Boolean redisplay
= False
;
1792 if (newmw
->menu
.contents
1793 && newmw
->menu
.contents
->contents
1794 && newmw
->menu
.contents
->contents
->change
>= VISIBLE_CHANGE
)
1796 /* Do redisplay if the contents are entirely eliminated. */
1797 if (newmw
->menu
.contents
1798 && newmw
->menu
.contents
->contents
== 0
1799 && newmw
->menu
.contents
->change
>= VISIBLE_CHANGE
)
1802 if (newmw
->core
.background_pixel
!= oldmw
->core
.background_pixel
1803 || newmw
->menu
.foreground
!= oldmw
->menu
.foreground
1804 || newmw
->menu
.font
!= oldmw
->menu
.font
)
1806 release_drawing_gcs (newmw
);
1807 make_drawing_gcs (newmw
);
1810 for (i
= 0; i
< oldmw
->menu
.windows_length
; i
++)
1812 XSetWindowBackground (XtDisplay (oldmw
),
1813 oldmw
->menu
.windows
[i
].window
,
1814 newmw
->core
.background_pixel
);
1815 /* clear windows and generate expose events */
1816 XClearArea (XtDisplay (oldmw
), oldmw
->menu
.windows
[i
].window
,
1828 XlwMenuWidget mw
= (XlwMenuWidget
)w
;
1830 if (mw
->menu
.popped_up
)
1832 /* Don't allow the popup menu to resize itself. */
1833 mw
->core
.width
= mw
->menu
.windows
[0].width
;
1834 mw
->core
.height
= mw
->menu
.windows
[0].height
;
1835 mw
->core
.parent
->core
.width
= mw
->core
.width
;
1836 mw
->core
.parent
->core
.height
= mw
->core
.height
;
1840 mw
->menu
.windows
[0].width
= mw
->core
.width
;
1841 mw
->menu
.windows
[0].height
= mw
->core
.height
;
1845 \f/* Action procedures */
1847 handle_single_motion_event (mw
, ev
)
1854 if (!map_event_to_widget_value (mw
, ev
, &val
, &level
))
1855 pop_new_stack_if_no_contents (mw
);
1857 set_new_state (mw
, val
, level
);
1860 /* Sync with the display. Makes it feel better on X terms. */
1861 XSync (XtDisplay (mw
), False
);
1865 handle_motion_event (mw
, ev
)
1871 int state
= ev
->state
;
1873 handle_single_motion_event (mw
, ev
);
1875 /* allow motion events to be generated again */
1877 && XQueryPointer (XtDisplay (mw
), ev
->window
,
1878 &ev
->root
, &ev
->subwindow
,
1879 &ev
->x_root
, &ev
->y_root
,
1882 && ev
->state
== state
1883 && (ev
->x_root
!= x
|| ev
->y_root
!= y
))
1884 handle_single_motion_event (mw
, ev
);
1888 Start (w
, ev
, params
, num_params
)
1892 Cardinal
*num_params
;
1894 XlwMenuWidget mw
= (XlwMenuWidget
)w
;
1896 if (!mw
->menu
.popped_up
)
1898 menu_post_event
= *ev
;
1899 pop_up_menu (mw
, (XButtonPressedEvent
*) ev
);
1903 /* If we push a button while the menu is posted semipermanently,
1904 releasing the button should always pop the menu down. */
1905 next_release_must_exit
= 1;
1907 /* notes the absolute position of the menubar window */
1908 mw
->menu
.windows
[0].x
= ev
->xmotion
.x_root
- ev
->xmotion
.x
;
1909 mw
->menu
.windows
[0].y
= ev
->xmotion
.y_root
- ev
->xmotion
.y
;
1911 /* handles the down like a move, slots are compatible */
1912 handle_motion_event (mw
, &ev
->xmotion
);
1917 Drag (w
, ev
, params
, num_params
)
1921 Cardinal
*num_params
;
1923 XlwMenuWidget mw
= (XlwMenuWidget
)w
;
1924 if (mw
->menu
.popped_up
)
1925 handle_motion_event (mw
, &ev
->xmotion
);
1929 This is how we handle presses and releases of modifier keys. */
1931 Nothing (w
, ev
, params
, num_params
)
1935 Cardinal
*num_params
;
1939 /* Handle key press and release events while menu is popped up.
1940 Our action is to get rid of the menu. */
1942 Key (w
, ev
, params
, num_params
)
1946 Cardinal
*num_params
;
1948 XlwMenuWidget mw
= (XlwMenuWidget
)w
;
1950 /* Pop down everything. */
1951 mw
->menu
.new_depth
= 1;
1954 if (mw
->menu
.popped_up
)
1956 mw
->menu
.popped_up
= False
;
1957 XtUngrabPointer ((Widget
)mw
, ev
->xmotion
.time
);
1958 if (XtIsShell (XtParent ((Widget
) mw
)))
1959 XtPopdown (XtParent ((Widget
) mw
));
1962 XtRemoveGrab ((Widget
) mw
);
1963 display_menu (mw
, 0, False
, NULL
, NULL
, NULL
, NULL
, NULL
);
1968 XtCallCallbackList ((Widget
)mw
, mw
->menu
.select
, (XtPointer
)0);
1972 Select (w
, ev
, params
, num_params
)
1976 Cardinal
*num_params
;
1978 XlwMenuWidget mw
= (XlwMenuWidget
)w
;
1979 widget_value
* selected_item
= mw
->menu
.old_stack
[mw
->menu
.old_depth
- 1];
1981 /* If user releases the button quickly, without selecting anything,
1982 after the initial down-click that brought the menu up,
1984 if ((selected_item
== 0
1985 || ((widget_value
*) selected_item
)->call_data
== 0)
1986 && !next_release_must_exit
1987 && (ev
->xbutton
.time
- menu_post_event
.xbutton
.time
1988 < XtGetMultiClickTime (XtDisplay (w
))))
1991 /* pop down everything. */
1992 mw
->menu
.new_depth
= 1;
1995 if (mw
->menu
.popped_up
)
1997 mw
->menu
.popped_up
= False
;
1998 XtUngrabPointer ((Widget
)mw
, ev
->xmotion
.time
);
1999 if (XtIsShell (XtParent ((Widget
) mw
)))
2000 XtPopdown (XtParent ((Widget
) mw
));
2003 XtRemoveGrab ((Widget
) mw
);
2004 display_menu (mw
, 0, False
, NULL
, NULL
, NULL
, NULL
, NULL
);
2009 XtCallCallbackList ((Widget
)mw
, mw
->menu
.select
, (XtPointer
)selected_item
);
2013 \f/* Special code to pop-up a menu */
2015 pop_up_menu (mw
, event
)
2017 XButtonPressedEvent
* event
;
2019 int x
= event
->x_root
;
2020 int y
= event
->y_root
;
2023 int borderwidth
= mw
->menu
.shadow_thickness
;
2024 Screen
* screen
= XtScreen (mw
);
2025 Display
*display
= XtDisplay (mw
);
2028 next_release_must_exit
= 0;
2030 XtCallCallbackList ((Widget
)mw
, mw
->menu
.open
, NULL
);
2032 if (XtIsShell (XtParent ((Widget
)mw
)))
2035 w
= mw
->menu
.windows
[0].width
;
2036 h
= mw
->menu
.windows
[0].height
;
2040 if (x
< borderwidth
)
2042 if (x
+ w
+ 2 * borderwidth
> WidthOfScreen (screen
))
2043 x
= WidthOfScreen (screen
) - w
- 2 * borderwidth
;
2044 if (y
< borderwidth
)
2046 if (y
+ h
+ 2 * borderwidth
> HeightOfScreen (screen
))
2047 y
= HeightOfScreen (screen
) - h
- 2 * borderwidth
;
2049 mw
->menu
.popped_up
= True
;
2050 if (XtIsShell (XtParent ((Widget
)mw
)))
2052 XtConfigureWidget (XtParent ((Widget
)mw
), x
, y
, w
, h
,
2053 XtParent ((Widget
)mw
)->core
.border_width
);
2054 XtPopup (XtParent ((Widget
)mw
), XtGrabExclusive
);
2055 display_menu (mw
, 0, False
, NULL
, NULL
, NULL
, NULL
, NULL
);
2056 mw
->menu
.windows
[0].x
= x
+ borderwidth
;
2057 mw
->menu
.windows
[0].y
= y
+ borderwidth
;
2061 XEvent
*ev
= (XEvent
*) event
;
2063 XtAddGrab ((Widget
) mw
, True
, True
);
2065 /* notes the absolute position of the menubar window */
2066 mw
->menu
.windows
[0].x
= ev
->xmotion
.x_root
- ev
->xmotion
.x
;
2067 mw
->menu
.windows
[0].y
= ev
->xmotion
.y_root
- ev
->xmotion
.y
;
2071 count
= x_catch_errors (display
);
2073 XtGrabPointer ((Widget
)mw
, False
,
2075 | PointerMotionHintMask
2078 GrabModeAsync
, GrabModeAsync
, None
,
2079 mw
->menu
.cursor_shape
,
2081 pointer_grabbed
= 1;
2083 if (x_had_errors_p (display
))
2085 pointer_grabbed
= 0;
2086 XtUngrabPointer ((Widget
)mw
, event
->time
);
2088 x_uncatch_errors (display
, count
);
2091 handle_motion_event (mw
, (XMotionEvent
*)event
);