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 */
27 #include "../src/lisp.h"
31 #include <sys/types.h>
32 #if (defined __sun) && !(defined SUNOS41)
39 #include <X11/IntrinsicP.h>
40 #include <X11/ObjectP.h>
41 #include <X11/StringDefs.h>
42 #include <X11/cursorfont.h>
47 /* Defined in xfns.c. When config.h defines `static' as empty, we get
48 redefinition errors when gray_bitmap is included more than once, so
49 we're referring to the one include in xfns.c here. */
51 extern int gray_bitmap_width
;
52 extern int gray_bitmap_height
;
53 extern char *gray_bitmap_bits
;
55 /* Defined in xterm.c. */
56 extern int x_alloc_nearest_color_for_widget
__P ((Widget
, Colormap
, XColor
*));
57 extern int x_alloc_lighter_color_for_widget
__P ((Widget
, Display
*, Colormap
,
60 extern int x_catch_errors
__P ((Display
*));
61 extern int x_uncatch_errors
__P ((Display
*, int));
62 extern int x_had_errors_p
__P ((Display
*));
63 extern int x_clear_errors
__P ((Display
*));
64 extern unsigned long x_copy_dpy_color
__P ((Display
*, Colormap
,
67 /* Defined in xfaces.c. */
68 extern void x_free_dpy_colors
__P ((Display
*, Screen
*, Colormap
,
69 unsigned long *pixels
, int npixels
));
72 #include <X11/bitmaps/gray>
73 #define gray_bitmap_width gray_width
74 #define gray_bitmap_height gray_height
75 #define gray_bitmap_bits gray_bits
77 #endif /* not emacs */
79 static int pointer_grabbed
;
80 static XEvent menu_post_event
;
82 XFontStruct
*xlwmenu_default_font
;
85 xlwMenuTranslations
[] =
86 "<BtnDown>: start()\n\
89 <Key>Shift_L: nothing()\n\
90 <Key>Shift_R: nothing()\n\
91 <Key>Meta_L: nothing()\n\
92 <Key>Meta_R: nothing()\n\
93 <Key>Control_L: nothing()\n\
94 <Key>Control_R: nothing()\n\
95 <Key>Hyper_L: nothing()\n\
96 <Key>Hyper_R: nothing()\n\
97 <Key>Super_L: nothing()\n\
98 <Key>Super_R: nothing()\n\
99 <Key>Alt_L: nothing()\n\
100 <Key>Alt_R: nothing()\n\
101 <Key>Caps_Lock: nothing()\n\
102 <Key>Shift_Lock: nothing()\n\
103 <KeyUp>Shift_L: nothing()\n\
104 <KeyUp>Shift_R: nothing()\n\
105 <KeyUp>Meta_L: nothing()\n\
106 <KeyUp>Meta_R: nothing()\n\
107 <KeyUp>Control_L: nothing()\n\
108 <KeyUp>Control_R: nothing()\n\
109 <KeyUp>Hyper_L: nothing()\n\
110 <KeyUp>Hyper_R: nothing()\n\
111 <KeyUp>Super_L: nothing()\n\
112 <KeyUp>Super_R: nothing()\n\
113 <KeyUp>Alt_L: nothing()\n\
114 <KeyUp>Alt_R: nothing()\n\
115 <KeyUp>Caps_Lock: nothing()\n\
116 <KeyUp>Shift_Lock:nothing()\n\
121 #define offset(field) XtOffset(XlwMenuWidget, field)
125 {XtNfont
, XtCFont
, XtRFontStruct
, sizeof(XFontStruct
*),
126 offset(menu
.font
),XtRString
, "XtDefaultFont"},
127 {XtNforeground
, XtCForeground
, XtRPixel
, sizeof(Pixel
),
128 offset(menu
.foreground
), XtRString
, "XtDefaultForeground"},
129 {XtNbuttonForeground
, XtCButtonForeground
, XtRPixel
, sizeof(Pixel
),
130 offset(menu
.button_foreground
), XtRString
, "XtDefaultForeground"},
131 {XtNmargin
, XtCMargin
, XtRDimension
, sizeof(Dimension
),
132 offset(menu
.margin
), XtRImmediate
, (XtPointer
) 4},
133 {XtNhorizontalSpacing
, XtCMargin
, XtRDimension
, sizeof(Dimension
),
134 offset(menu
.horizontal_spacing
), XtRImmediate
, (XtPointer
)3},
135 {XtNverticalSpacing
, XtCMargin
, XtRDimension
, sizeof(Dimension
),
136 offset(menu
.vertical_spacing
), XtRImmediate
, (XtPointer
)1},
137 {XtNarrowSpacing
, XtCMargin
, XtRDimension
, sizeof(Dimension
),
138 offset(menu
.arrow_spacing
), XtRImmediate
, (XtPointer
)10},
140 {XmNshadowThickness
, XmCShadowThickness
, XtRDimension
,
141 sizeof (Dimension
), offset (menu
.shadow_thickness
),
142 XtRImmediate
, (XtPointer
) 2},
143 {XmNtopShadowColor
, XmCTopShadowColor
, XtRPixel
, sizeof (Pixel
),
144 offset (menu
.top_shadow_color
), XtRImmediate
, (XtPointer
)-1},
145 {XmNbottomShadowColor
, XmCBottomShadowColor
, XtRPixel
, sizeof (Pixel
),
146 offset (menu
.bottom_shadow_color
), XtRImmediate
, (XtPointer
)-1},
147 {XmNtopShadowPixmap
, XmCTopShadowPixmap
, XtRPixmap
, sizeof (Pixmap
),
148 offset (menu
.top_shadow_pixmap
), XtRImmediate
, (XtPointer
)None
},
149 {XmNbottomShadowPixmap
, XmCBottomShadowPixmap
, XtRPixmap
, sizeof (Pixmap
),
150 offset (menu
.bottom_shadow_pixmap
), XtRImmediate
, (XtPointer
)None
},
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 {XtNmenu
, XtCMenu
, XtRPointer
, sizeof(XtPointer
),
159 offset(menu
.contents
), XtRImmediate
, (XtPointer
)NULL
},
160 {XtNcursor
, XtCCursor
, XtRCursor
, sizeof(Cursor
),
161 offset(menu
.cursor_shape
), XtRString
, (XtPointer
)"right_ptr"},
162 {XtNhorizontal
, XtCHorizontal
, XtRInt
, sizeof(int),
163 offset(menu
.horizontal
), XtRImmediate
, (XtPointer
)True
},
167 static Boolean
XlwMenuSetValues();
168 static void XlwMenuRealize();
169 static void XlwMenuRedisplay();
170 static void XlwMenuResize();
171 static void XlwMenuInitialize();
172 static void XlwMenuRedisplay();
173 static void XlwMenuDestroy();
174 static void XlwMenuClassInitialize();
177 static void Select();
179 static void Nothing();
180 static int separator_height ();
183 xlwMenuActionsList
[] =
189 {"nothing", Nothing
},
192 #define SuperClass ((CoreWidgetClass)&coreClassRec)
194 XlwMenuClassRec xlwMenuClassRec
=
196 { /* CoreClass fields initialization */
197 (WidgetClass
) SuperClass
, /* superclass */
198 "XlwMenu", /* class_name */
199 sizeof(XlwMenuRec
), /* size */
200 XlwMenuClassInitialize
, /* class_initialize */
201 NULL
, /* class_part_initialize */
202 FALSE
, /* class_inited */
203 XlwMenuInitialize
, /* initialize */
204 NULL
, /* initialize_hook */
205 XlwMenuRealize
, /* realize */
206 xlwMenuActionsList
, /* actions */
207 XtNumber(xlwMenuActionsList
), /* num_actions */
208 xlwMenuResources
, /* resources */
209 XtNumber(xlwMenuResources
), /* resource_count */
210 NULLQUARK
, /* xrm_class */
211 TRUE
, /* compress_motion */
212 TRUE
, /* compress_exposure */
213 TRUE
, /* compress_enterleave */
214 FALSE
, /* visible_interest */
215 XlwMenuDestroy
, /* destroy */
216 XlwMenuResize
, /* resize */
217 XlwMenuRedisplay
, /* expose */
218 XlwMenuSetValues
, /* set_values */
219 NULL
, /* set_values_hook */
220 XtInheritSetValuesAlmost
, /* set_values_almost */
221 NULL
, /* get_values_hook */
222 NULL
, /* accept_focus */
223 XtVersion
, /* version */
224 NULL
, /* callback_private */
225 xlwMenuTranslations
, /* tm_table */
226 XtInheritQueryGeometry
, /* query_geometry */
227 XtInheritDisplayAccelerator
, /* display_accelerator */
229 }, /* XlwMenuClass fields initialization */
235 WidgetClass xlwMenuWidgetClass
= (WidgetClass
) &xlwMenuClassRec
;
237 int submenu_destroyed
;
239 static int next_release_must_exit
;
244 /* Like abort, but remove grabs from widget W before. */
250 if (XtIsShell (XtParent (w
)))
252 XtUngrabPointer (w
, CurrentTime
);
257 push_new_stack (mw
, val
)
261 if (!mw
->menu
.new_stack
)
263 mw
->menu
.new_stack_length
= 10;
265 (widget_value
**)XtCalloc (mw
->menu
.new_stack_length
,
266 sizeof (widget_value
*));
268 else if (mw
->menu
.new_depth
== mw
->menu
.new_stack_length
)
270 mw
->menu
.new_stack_length
*= 2;
272 (widget_value
**)XtRealloc ((char*)mw
->menu
.new_stack
,
273 mw
->menu
.new_stack_length
* sizeof (widget_value
*));
275 mw
->menu
.new_stack
[mw
->menu
.new_depth
++] = val
;
279 pop_new_stack_if_no_contents (mw
)
282 if (mw
->menu
.new_depth
)
284 if (!mw
->menu
.new_stack
[mw
->menu
.new_depth
- 1]->contents
)
285 mw
->menu
.new_depth
-= 1;
290 make_old_stack_space (mw
, n
)
294 if (!mw
->menu
.old_stack
)
296 mw
->menu
.old_stack_length
= 10;
298 (widget_value
**)XtCalloc (mw
->menu
.old_stack_length
,
299 sizeof (widget_value
*));
301 else if (mw
->menu
.old_stack_length
< n
)
303 mw
->menu
.old_stack_length
*= 2;
305 (widget_value
**)XtRealloc ((char*)mw
->menu
.old_stack
,
306 mw
->menu
.old_stack_length
* sizeof (widget_value
*));
319 XTextExtents (mw
->menu
.font
, s
, strlen (s
), &drop
, &drop
, &drop
, &xcs
);
327 return (mw
->menu
.font
->ascent
* 3/4) | 1;
330 /* Return the width of toggle buttons of widget MW. */
333 toggle_button_width (mw
)
336 return ((mw
->menu
.font
->ascent
+ mw
->menu
.font
->descent
) * 2 / 3) | 1;
340 /* Return the width of radio buttons of widget MW. */
343 radio_button_width (mw
)
346 return toggle_button_width (mw
) * 1.41;
353 {"labelString", "LabelString", XtRString
, sizeof(String
),
358 resource_widget_value (mw
, val
)
362 if (!val
->toolkit_data
)
364 char* resourced_name
= NULL
;
366 XtGetSubresources ((Widget
) mw
,
367 (XtPointer
) &resourced_name
,
368 val
->name
, val
->name
,
369 nameResource
, 1, NULL
, 0);
371 resourced_name
= val
->name
;
374 complete_name
= (char *) XtMalloc (strlen (resourced_name
) + 1);
375 strcpy (complete_name
, resourced_name
);
379 int complete_length
=
380 strlen (resourced_name
) + strlen (val
->value
) + 2;
381 complete_name
= XtMalloc (complete_length
);
383 strcat (complete_name
, resourced_name
);
384 strcat (complete_name
, " ");
385 strcat (complete_name
, val
->value
);
388 val
->toolkit_data
= complete_name
;
389 val
->free_toolkit_data
= True
;
391 return (char*)val
->toolkit_data
;
394 /* Returns the sizes of an item */
396 size_menu_item (mw
, val
, horizontal_p
, label_width
, rest_width
, button_width
,
406 enum menu_separator separator
;
408 if (lw_separator_p (val
->name
, &separator
, 0))
410 *height
= separator_height (separator
);
418 mw
->menu
.font
->ascent
+ mw
->menu
.font
->descent
419 + 2 * mw
->menu
.vertical_spacing
+ 2 * mw
->menu
.shadow_thickness
;
422 string_width (mw
, resource_widget_value (mw
, val
))
423 + mw
->menu
.horizontal_spacing
+ mw
->menu
.shadow_thickness
;
425 *rest_width
= mw
->menu
.horizontal_spacing
+ mw
->menu
.shadow_thickness
;
429 /* Add width of the arrow displayed for submenus. */
430 *rest_width
+= arrow_width (mw
) + mw
->menu
.arrow_spacing
;
432 /* Add width of key equivalent string. */
433 *rest_width
+= (string_width (mw
, val
->key
)
434 + mw
->menu
.arrow_spacing
);
436 if (val
->button_type
== BUTTON_TYPE_TOGGLE
)
437 *button_width
= (toggle_button_width (mw
)
438 + mw
->menu
.horizontal_spacing
);
439 else if (val
->button_type
== BUTTON_TYPE_RADIO
)
440 *button_width
= (radio_button_width (mw
)
441 + mw
->menu
.horizontal_spacing
);
447 size_menu (mw
, level
)
451 unsigned int label_width
= 0;
453 int button_width
= 0;
454 int max_rest_width
= 0;
455 int max_button_width
= 0;
456 unsigned int height
= 0;
457 int horizontal_p
= mw
->menu
.horizontal
&& (level
== 0);
461 if (level
>= mw
->menu
.old_depth
)
462 abort_gracefully ((Widget
) mw
);
464 ws
= &mw
->menu
.windows
[level
];
468 ws
->button_width
= 0;
470 for (val
= mw
->menu
.old_stack
[level
]->contents
; val
; val
= val
->next
)
472 size_menu_item (mw
, val
, horizontal_p
, &label_width
, &rest_width
,
473 &button_width
, &height
);
476 ws
->width
+= label_width
+ rest_width
;
477 if (height
> ws
->height
)
482 if (label_width
> ws
->label_width
)
483 ws
->label_width
= label_width
;
484 if (rest_width
> max_rest_width
)
485 max_rest_width
= rest_width
;
486 if (button_width
> max_button_width
)
487 max_button_width
= button_width
;
488 ws
->height
+= height
;
493 ws
->label_width
= ws
->button_width
= 0;
496 ws
->width
= ws
->label_width
+ max_rest_width
+ max_button_width
;
497 ws
->button_width
= max_button_width
;
500 ws
->width
+= 2 * mw
->menu
.shadow_thickness
;
501 ws
->height
+= 2 * mw
->menu
.shadow_thickness
;
505 ws
->width
+= 2 * mw
->menu
.margin
;
506 ws
->height
+= 2 * mw
->menu
.margin
;
514 draw_arrow (mw
, window
, gc
, x
, y
, width
, down_p
)
523 Display
*dpy
= XtDisplay (mw
);
524 GC top_gc
= mw
->menu
.shadow_top_gc
;
525 GC bottom_gc
= mw
->menu
.shadow_bottom_gc
;
526 int thickness
= mw
->menu
.shadow_thickness
;
529 /* alpha = atan (0.5)
530 factor = (1 + sin (alpha)) / cos (alpha) */
531 double factor
= 1.62;
532 int thickness2
= thickness
* factor
;
534 y
+= (mw
->menu
.font
->ascent
+ mw
->menu
.font
->descent
- height
) / 2;
545 pt
[0].y
= y
+ height
;
546 pt
[1].x
= x
+ thickness
;
547 pt
[1].y
= y
+ height
- thickness2
;
548 pt
[2].x
= x
+ thickness2
;
549 pt
[2].y
= y
+ thickness2
;
552 XFillPolygon (dpy
, window
, top_gc
, pt
, 4, Convex
, CoordModeOrigin
);
556 pt
[1].x
= x
+ thickness
;
557 pt
[1].y
= y
+ thickness2
;
558 pt
[2].x
= x
+ width
- thickness2
;
559 pt
[2].y
= y
+ height
/ 2;
561 pt
[3].y
= y
+ height
/ 2;
562 XFillPolygon (dpy
, window
, top_gc
, pt
, 4, Convex
, CoordModeOrigin
);
565 pt
[0].y
= y
+ height
;
566 pt
[1].x
= x
+ thickness
;
567 pt
[1].y
= y
+ height
- thickness2
;
568 pt
[2].x
= x
+ width
- thickness2
;
569 pt
[2].y
= y
+ height
/ 2;
571 pt
[3].y
= y
+ height
/ 2;
572 XFillPolygon (dpy
, window
, bottom_gc
, pt
, 4, Convex
, CoordModeOrigin
);
578 draw_shadow_rectangle (mw
, window
, x
, y
, width
, height
, erase_p
, down_p
)
588 Display
*dpy
= XtDisplay (mw
);
589 GC top_gc
= !erase_p
? mw
->menu
.shadow_top_gc
: mw
->menu
.background_gc
;
590 GC bottom_gc
= !erase_p
? mw
->menu
.shadow_bottom_gc
: mw
->menu
.background_gc
;
591 int thickness
= mw
->menu
.shadow_thickness
;
594 if (!erase_p
&& down_p
)
604 points
[1].x
= x
+ width
;
606 points
[2].x
= x
+ width
- thickness
;
607 points
[2].y
= y
+ thickness
;
609 points
[3].y
= y
+ thickness
;
610 XFillPolygon (dpy
, window
, top_gc
, points
, 4, Convex
, CoordModeOrigin
);
612 points
[0].y
= y
+ thickness
;
614 points
[1].y
= y
+ height
;
615 points
[2].x
= x
+ thickness
;
616 points
[2].y
= y
+ height
- thickness
;
617 points
[3].x
= x
+ thickness
;
618 points
[3].y
= y
+ thickness
;
619 XFillPolygon (dpy
, window
, top_gc
, points
, 4, Convex
, CoordModeOrigin
);
620 points
[0].x
= x
+ width
;
622 points
[1].x
= x
+ width
- thickness
;
623 points
[1].y
= y
+ thickness
;
624 points
[2].x
= x
+ width
- thickness
;
625 points
[2].y
= y
+ height
- thickness
;
626 points
[3].x
= x
+ width
;
627 points
[3].y
= y
+ height
- thickness
;
628 XFillPolygon (dpy
, window
, bottom_gc
, points
, 4, Convex
, CoordModeOrigin
);
630 points
[0].y
= y
+ height
;
631 points
[1].x
= x
+ width
;
632 points
[1].y
= y
+ height
;
633 points
[2].x
= x
+ width
;
634 points
[2].y
= y
+ height
- thickness
;
635 points
[3].x
= x
+ thickness
;
636 points
[3].y
= y
+ height
- thickness
;
637 XFillPolygon (dpy
, window
, bottom_gc
, points
, 4, Convex
, CoordModeOrigin
);
642 draw_shadow_rhombus (mw
, window
, x
, y
, width
, height
, erase_p
, down_p
)
652 Display
*dpy
= XtDisplay (mw
);
653 GC top_gc
= !erase_p
? mw
->menu
.shadow_top_gc
: mw
->menu
.background_gc
;
654 GC bottom_gc
= !erase_p
? mw
->menu
.shadow_bottom_gc
: mw
->menu
.background_gc
;
655 int thickness
= mw
->menu
.shadow_thickness
;
658 if (!erase_p
&& down_p
)
667 points
[0].y
= y
+ height
/ 2;
668 points
[1].x
= x
+ thickness
;
669 points
[1].y
= y
+ height
/ 2;
670 points
[2].x
= x
+ width
/ 2;
671 points
[2].y
= y
+ thickness
;
672 points
[3].x
= x
+ width
/ 2;
674 XFillPolygon (dpy
, window
, top_gc
, points
, 4, Convex
, CoordModeOrigin
);
675 points
[0].x
= x
+ width
/ 2;
677 points
[1].x
= x
+ width
/ 2;
678 points
[1].y
= y
+ thickness
;
679 points
[2].x
= x
+ width
- thickness
;
680 points
[2].y
= y
+ height
/ 2;
681 points
[3].x
= x
+ width
;
682 points
[3].y
= y
+ height
/ 2;
683 XFillPolygon (dpy
, window
, top_gc
, points
, 4, Convex
, CoordModeOrigin
);
685 points
[0].y
= y
+ height
/ 2;
686 points
[1].x
= x
+ thickness
;
687 points
[1].y
= y
+ height
/ 2;
688 points
[2].x
= x
+ width
/ 2;
689 points
[2].y
= y
+ height
- thickness
;
690 points
[3].x
= x
+ width
/ 2;
691 points
[3].y
= y
+ height
;
692 XFillPolygon (dpy
, window
, bottom_gc
, points
, 4, Convex
, CoordModeOrigin
);
693 points
[0].x
= x
+ width
/ 2;
694 points
[0].y
= y
+ height
;
695 points
[1].x
= x
+ width
/ 2;
696 points
[1].y
= y
+ height
- thickness
;
697 points
[2].x
= x
+ width
- thickness
;
698 points
[2].y
= y
+ height
/ 2;
699 points
[3].x
= x
+ width
;
700 points
[3].y
= y
+ height
/ 2;
701 XFillPolygon (dpy
, window
, bottom_gc
, points
, 4, Convex
, CoordModeOrigin
);
705 /* Draw a toggle button on widget MW, X window WINDOW. X/Y is the
706 top-left corner of the menu item. SELECTED_P non-zero means the
707 toggle button is selected. */
710 draw_toggle (mw
, window
, x
, y
, selected_p
)
713 int x
, y
, selected_p
;
717 width
= toggle_button_width (mw
);
719 x
+= mw
->menu
.horizontal_spacing
;
720 y
+= (mw
->menu
.font
->ascent
- height
) / 2;
721 draw_shadow_rectangle (mw
, window
, x
, y
, width
, height
, False
, selected_p
);
725 /* Draw a radio button on widget MW, X window WINDOW. X/Y is the
726 top-left corner of the menu item. SELECTED_P non-zero means the
727 toggle button is selected. */
730 draw_radio (mw
, window
, x
, y
, selected_p
)
733 int x
, y
, selected_p
;
737 width
= radio_button_width (mw
);
739 x
+= mw
->menu
.horizontal_spacing
;
740 y
+= (mw
->menu
.font
->ascent
- height
) / 2;
741 draw_shadow_rhombus (mw
, window
, x
, y
, width
, height
, False
, selected_p
);
745 /* Draw a menu separator on widget MW, X window WINDOW. X/Y is the
746 top-left corner of the menu item. WIDTH is the width of the
747 separator to draw. TYPE is the separator type. */
750 draw_separator (mw
, window
, x
, y
, width
, type
)
754 enum menu_separator type
;
756 Display
*dpy
= XtDisplay (mw
);
761 case SEPARATOR_NO_LINE
:
764 case SEPARATOR_SINGLE_LINE
:
765 XDrawLine (dpy
, window
, mw
->menu
.foreground_gc
,
769 case SEPARATOR_DOUBLE_LINE
:
770 draw_separator (mw
, window
, x
, y
, width
, SEPARATOR_SINGLE_LINE
);
771 draw_separator (mw
, window
, x
, y
+ 2, width
, SEPARATOR_SINGLE_LINE
);
774 case SEPARATOR_SINGLE_DASHED_LINE
:
775 xgcv
.line_style
= LineOnOffDash
;
776 XChangeGC (dpy
, mw
->menu
.foreground_gc
, GCLineStyle
, &xgcv
);
777 XDrawLine (dpy
, window
, mw
->menu
.foreground_gc
,
779 xgcv
.line_style
= LineSolid
;
780 XChangeGC (dpy
, mw
->menu
.foreground_gc
, GCLineStyle
, &xgcv
);
783 case SEPARATOR_DOUBLE_DASHED_LINE
:
784 draw_separator (mw
, window
, x
, y
, width
,
785 SEPARATOR_SINGLE_DASHED_LINE
);
786 draw_separator (mw
, window
, x
, y
+ 2, width
,
787 SEPARATOR_SINGLE_DASHED_LINE
);
790 case SEPARATOR_SHADOW_ETCHED_IN
:
791 XDrawLine (dpy
, window
, mw
->menu
.shadow_bottom_gc
,
793 XDrawLine (dpy
, window
, mw
->menu
.shadow_top_gc
,
794 x
, y
+ 1, x
+ width
, y
+ 1);
797 case SEPARATOR_SHADOW_ETCHED_OUT
:
798 XDrawLine (dpy
, window
, mw
->menu
.shadow_top_gc
,
800 XDrawLine (dpy
, window
, mw
->menu
.shadow_bottom_gc
,
801 x
, y
+ 1, x
+ width
, y
+ 1);
804 case SEPARATOR_SHADOW_ETCHED_IN_DASH
:
805 xgcv
.line_style
= LineOnOffDash
;
806 XChangeGC (dpy
, mw
->menu
.shadow_bottom_gc
, GCLineStyle
, &xgcv
);
807 XChangeGC (dpy
, mw
->menu
.shadow_top_gc
, GCLineStyle
, &xgcv
);
808 draw_separator (mw
, window
, x
, y
, width
, SEPARATOR_SHADOW_ETCHED_IN
);
809 xgcv
.line_style
= LineSolid
;
810 XChangeGC (dpy
, mw
->menu
.shadow_bottom_gc
, GCLineStyle
, &xgcv
);
811 XChangeGC (dpy
, mw
->menu
.shadow_top_gc
, GCLineStyle
, &xgcv
);
814 case SEPARATOR_SHADOW_ETCHED_OUT_DASH
:
815 xgcv
.line_style
= LineOnOffDash
;
816 XChangeGC (dpy
, mw
->menu
.shadow_bottom_gc
, GCLineStyle
, &xgcv
);
817 XChangeGC (dpy
, mw
->menu
.shadow_top_gc
, GCLineStyle
, &xgcv
);
818 draw_separator (mw
, window
, x
, y
, width
, SEPARATOR_SHADOW_ETCHED_OUT
);
819 xgcv
.line_style
= LineSolid
;
820 XChangeGC (dpy
, mw
->menu
.shadow_bottom_gc
, GCLineStyle
, &xgcv
);
821 XChangeGC (dpy
, mw
->menu
.shadow_top_gc
, GCLineStyle
, &xgcv
);
824 case SEPARATOR_SHADOW_DOUBLE_ETCHED_IN
:
825 draw_separator (mw
, window
, x
, y
, width
, SEPARATOR_SHADOW_ETCHED_IN
);
826 draw_separator (mw
, window
, x
, y
+ 3, width
, SEPARATOR_SHADOW_ETCHED_IN
);
829 case SEPARATOR_SHADOW_DOUBLE_ETCHED_OUT
:
830 draw_separator (mw
, window
, x
, y
, width
,
831 SEPARATOR_SHADOW_ETCHED_OUT
);
832 draw_separator (mw
, window
, x
, y
+ 3, width
,
833 SEPARATOR_SHADOW_ETCHED_OUT
);
836 case SEPARATOR_SHADOW_DOUBLE_ETCHED_IN_DASH
:
837 xgcv
.line_style
= LineOnOffDash
;
838 XChangeGC (dpy
, mw
->menu
.shadow_bottom_gc
, GCLineStyle
, &xgcv
);
839 XChangeGC (dpy
, mw
->menu
.shadow_top_gc
, GCLineStyle
, &xgcv
);
840 draw_separator (mw
, window
, x
, y
, width
,
841 SEPARATOR_SHADOW_DOUBLE_ETCHED_IN
);
842 xgcv
.line_style
= LineSolid
;
843 XChangeGC (dpy
, mw
->menu
.shadow_bottom_gc
, GCLineStyle
, &xgcv
);
844 XChangeGC (dpy
, mw
->menu
.shadow_top_gc
, GCLineStyle
, &xgcv
);
847 case SEPARATOR_SHADOW_DOUBLE_ETCHED_OUT_DASH
:
848 xgcv
.line_style
= LineOnOffDash
;
849 XChangeGC (dpy
, mw
->menu
.shadow_bottom_gc
, GCLineStyle
, &xgcv
);
850 XChangeGC (dpy
, mw
->menu
.shadow_top_gc
, GCLineStyle
, &xgcv
);
851 draw_separator (mw
, window
, x
, y
, width
,
852 SEPARATOR_SHADOW_DOUBLE_ETCHED_OUT
);
853 xgcv
.line_style
= LineSolid
;
854 XChangeGC (dpy
, mw
->menu
.shadow_bottom_gc
, GCLineStyle
, &xgcv
);
855 XChangeGC (dpy
, mw
->menu
.shadow_top_gc
, GCLineStyle
, &xgcv
);
864 /* Return the pixel height of menu separator SEPARATOR. */
867 separator_height (separator
)
868 enum menu_separator separator
;
872 case SEPARATOR_NO_LINE
:
875 case SEPARATOR_SINGLE_LINE
:
876 case SEPARATOR_SINGLE_DASHED_LINE
:
879 case SEPARATOR_DOUBLE_LINE
:
880 case SEPARATOR_DOUBLE_DASHED_LINE
:
883 case SEPARATOR_SHADOW_ETCHED_IN
:
884 case SEPARATOR_SHADOW_ETCHED_OUT
:
885 case SEPARATOR_SHADOW_ETCHED_IN_DASH
:
886 case SEPARATOR_SHADOW_ETCHED_OUT_DASH
:
889 case SEPARATOR_SHADOW_DOUBLE_ETCHED_IN
:
890 case SEPARATOR_SHADOW_DOUBLE_ETCHED_OUT
:
891 case SEPARATOR_SHADOW_DOUBLE_ETCHED_IN_DASH
:
892 case SEPARATOR_SHADOW_DOUBLE_ETCHED_OUT_DASH
:
901 /* Display the menu item and increment where.x and where.y to show how large
902 the menu item was. */
905 display_menu_item (mw
, val
, ws
, where
, highlighted_p
, horizontal_p
,
911 Boolean highlighted_p
;
912 Boolean horizontal_p
;
913 Boolean just_compute_p
;
917 int font_ascent
= mw
->menu
.font
->ascent
;
918 int font_descent
= mw
->menu
.font
->descent
;
919 int shadow
= mw
->menu
.shadow_thickness
;
920 int margin
= mw
->menu
.margin
;
921 int h_spacing
= mw
->menu
.horizontal_spacing
;
922 int v_spacing
= mw
->menu
.vertical_spacing
;
928 enum menu_separator separator
;
929 int separator_p
= lw_separator_p (val
->name
, &separator
, 0);
931 /* compute the sizes of the item */
932 size_menu_item (mw
, val
, horizontal_p
, &label_width
, &rest_width
,
933 &button_width
, &height
);
936 width
= label_width
+ rest_width
;
939 label_width
= ws
->label_width
;
940 width
= ws
->width
- 2 * shadow
;
943 /* Only highlight an enabled item that has a callback. */
945 if (!val
->enabled
|| !(val
->call_data
|| val
->contents
))
948 /* do the drawing. */
951 /* Add the shadow border of the containing menu */
952 int x
= where
->x
+ shadow
;
953 int y
= where
->y
+ shadow
;
961 /* pick the foreground and background GC. */
963 text_gc
= mw
->menu
.foreground_gc
;
965 text_gc
= mw
->menu
.inactive_gc
;
966 deco_gc
= mw
->menu
.foreground_gc
;
970 draw_separator (mw
, ws
->window
, x
, y
, width
, separator
);
974 int x_offset
= x
+ h_spacing
+ shadow
;
975 char* display_string
= resource_widget_value (mw
, val
);
976 draw_shadow_rectangle (mw
, ws
->window
, x
, y
, width
, height
, True
,
979 /* Deal with centering a menu title. */
980 if (!horizontal_p
&& !val
->contents
&& !val
->call_data
)
982 int l
= string_width (mw
, display_string
);
985 x_offset
= (width
- l
) >> 1;
987 else if (!horizontal_p
&& ws
->button_width
)
988 x_offset
+= ws
->button_width
;
991 XDrawString (XtDisplay (mw
), ws
->window
, text_gc
, x_offset
,
992 y
+ v_spacing
+ shadow
+ font_ascent
,
993 display_string
, strlen (display_string
));
997 if (val
->button_type
== BUTTON_TYPE_TOGGLE
)
998 draw_toggle (mw
, ws
->window
, x
, y
+ v_spacing
+ shadow
,
1000 else if (val
->button_type
== BUTTON_TYPE_RADIO
)
1001 draw_radio (mw
, ws
->window
, x
, y
+ v_spacing
+ shadow
,
1006 int a_w
= arrow_width (mw
);
1007 draw_arrow (mw
, ws
->window
, deco_gc
,
1009 - mw
->menu
.horizontal_spacing
1010 - mw
->menu
.shadow_thickness
,
1011 y
+ v_spacing
+ shadow
, a_w
,
1016 XDrawString (XtDisplay (mw
), ws
->window
, text_gc
,
1017 x
+ label_width
+ mw
->menu
.arrow_spacing
,
1018 y
+ v_spacing
+ shadow
+ font_ascent
,
1019 val
->key
, strlen (val
->key
));
1024 XDrawRectangle (XtDisplay (mw
), ws
->window
,
1025 mw
->menu
.background_gc
,
1026 x
+ shadow
, y
+ shadow
,
1027 label_width
+ h_spacing
- 1,
1028 font_ascent
+ font_descent
+ 2 * v_spacing
- 1);
1029 draw_shadow_rectangle (mw
, ws
->window
, x
, y
, width
, height
,
1034 draw_shadow_rectangle (mw
, ws
->window
, x
, y
, width
, height
, False
,
1044 display_menu (mw
, level
, just_compute_p
, highlighted_pos
, hit
, hit_return
,
1048 Boolean just_compute_p
;
1049 XPoint
* highlighted_pos
;
1051 widget_value
** hit_return
;
1056 widget_value
* following_item
;
1059 int horizontal_p
= mw
->menu
.horizontal
&& (level
== 0);
1061 int just_compute_this_one_p
;
1062 /* This is set nonzero if the element containing HIGHLIGHTED_POS
1063 is disabled, so that we do not return any subsequent element either. */
1065 enum menu_separator separator
;
1067 if (level
>= mw
->menu
.old_depth
)
1068 abort_gracefully ((Widget
) mw
);
1070 if (level
< mw
->menu
.old_depth
- 1)
1071 following_item
= mw
->menu
.old_stack
[level
+ 1];
1073 following_item
= NULL
;
1081 ws
= &mw
->menu
.windows
[level
];
1082 for (val
= mw
->menu
.old_stack
[level
]->contents
; val
; val
= val
->next
)
1084 highlighted_p
= val
== following_item
;
1085 if (highlighted_p
&& highlighted_pos
)
1088 highlighted_pos
->x
= where
.x
;
1090 highlighted_pos
->y
= where
.y
;
1093 just_compute_this_one_p
=
1094 just_compute_p
|| ((this || that
) && val
!= this && val
!= that
);
1096 display_menu_item (mw
, val
, ws
, &where
, highlighted_p
, horizontal_p
,
1097 just_compute_this_one_p
);
1099 if (highlighted_p
&& highlighted_pos
)
1102 highlighted_pos
->y
= where
.y
;
1104 highlighted_pos
->x
= where
.x
;
1109 && (horizontal_p
? hit
->x
< where
.x
: hit
->y
< where
.y
)
1110 && !lw_separator_p (val
->name
, &separator
, 0)
1125 if (!just_compute_p
)
1126 draw_shadow_rectangle (mw
, ws
->window
, 0, 0, ws
->width
, ws
->height
,
1132 set_new_state (mw
, val
, level
)
1139 mw
->menu
.new_depth
= 0;
1140 for (i
= 0; i
< level
; i
++)
1141 push_new_stack (mw
, mw
->menu
.old_stack
[i
]);
1142 push_new_stack (mw
, val
);
1146 make_windows_if_needed (mw
, n
)
1152 XSetWindowAttributes xswa
;
1154 Window root
= RootWindowOfScreen (DefaultScreenOfDisplay (XtDisplay (mw
)));
1155 window_state
* windows
;
1157 if (mw
->menu
.windows_length
>= n
)
1160 xswa
.save_under
= True
;
1161 xswa
.override_redirect
= True
;
1162 xswa
.background_pixel
= mw
->core
.background_pixel
;
1163 xswa
.border_pixel
= mw
->core
.border_pixel
;
1165 ExposureMask
| PointerMotionMask
| PointerMotionHintMask
1166 | ButtonReleaseMask
| ButtonPressMask
;
1167 xswa
.cursor
= mw
->menu
.cursor_shape
;
1168 mask
= CWSaveUnder
| CWOverrideRedirect
| CWBackPixel
| CWBorderPixel
1169 | CWEventMask
| CWCursor
;
1171 if (!mw
->menu
.windows
)
1174 (window_state
*)XtMalloc (n
* sizeof (window_state
));
1180 (window_state
*)XtRealloc ((char*)mw
->menu
.windows
,
1181 n
* sizeof (window_state
));
1182 start_at
= mw
->menu
.windows_length
;
1184 mw
->menu
.windows_length
= n
;
1186 windows
= mw
->menu
.windows
;
1188 for (i
= start_at
; i
< n
; i
++)
1192 windows
[i
].width
= 1;
1193 windows
[i
].height
= 1;
1194 windows
[i
].window
=
1195 XCreateWindow (XtDisplay (mw
), root
, 0, 0, 1, 1,
1196 0, 0, CopyFromParent
, CopyFromParent
, mask
, &xswa
);
1200 /* Value is non-zero if WINDOW is part of menu bar widget W. */
1203 xlwmenu_window_p (w
, window
)
1207 XlwMenuWidget mw
= (XlwMenuWidget
) w
;
1210 for (i
= 0; i
< mw
->menu
.windows_length
; ++i
)
1211 if (window
== mw
->menu
.windows
[i
].window
)
1214 return i
< mw
->menu
.windows_length
;
1217 /* Make the window fit in the screen */
1219 fit_to_screen (mw
, ws
, previous_ws
, horizontal_p
)
1222 window_state
* previous_ws
;
1223 Boolean horizontal_p
;
1225 unsigned int screen_width
= WidthOfScreen (XtScreen (mw
));
1226 unsigned int screen_height
= HeightOfScreen (XtScreen (mw
));
1227 /* 1 if we are unable to avoid an overlap between
1228 this menu and the parent menu in the X dimension. */
1229 int horizontal_overlap
= 0;
1233 else if (ws
->x
+ ws
->width
> screen_width
)
1236 /* The addition of shadow-thickness for a sub-menu's position is
1237 to reflect a similar adjustment when the menu is displayed to
1238 the right of the invoking menu-item; it makes the sub-menu
1239 look more `attached' to the menu-item. */
1240 ws
->x
= previous_ws
->x
- ws
->width
+ mw
->menu
.shadow_thickness
;
1242 ws
->x
= screen_width
- ws
->width
;
1246 horizontal_overlap
= 1;
1249 /* If we overlap in X, try to avoid overlap in Y. */
1250 if (horizontal_overlap
1251 && ws
->y
< previous_ws
->y
+ previous_ws
->height
1252 && previous_ws
->y
< ws
->y
+ ws
->height
)
1254 /* Put this menu right below or right above PREVIOUS_WS
1256 if (previous_ws
->y
+ previous_ws
->height
+ ws
->height
< screen_height
)
1257 ws
->y
= previous_ws
->y
+ previous_ws
->height
;
1258 else if (previous_ws
->y
- ws
->height
> 0)
1259 ws
->y
= previous_ws
->y
- ws
->height
;
1264 else if (ws
->y
+ ws
->height
> screen_height
)
1267 ws
->y
= previous_ws
->y
- ws
->height
;
1269 ws
->y
= screen_height
- ws
->height
;
1275 /* Updates old_stack from new_stack and redisplays. */
1282 XPoint selection_position
;
1283 int old_depth
= mw
->menu
.old_depth
;
1284 int new_depth
= mw
->menu
.new_depth
;
1285 widget_value
** old_stack
;
1286 widget_value
** new_stack
;
1287 window_state
* windows
;
1288 widget_value
* old_selection
;
1289 widget_value
* new_selection
;
1291 /* Check that enough windows and old_stack are ready. */
1292 make_windows_if_needed (mw
, new_depth
);
1293 make_old_stack_space (mw
, new_depth
);
1294 windows
= mw
->menu
.windows
;
1295 old_stack
= mw
->menu
.old_stack
;
1296 new_stack
= mw
->menu
.new_stack
;
1298 /* compute the last identical different entry */
1299 for (i
= 1; i
< old_depth
&& i
< new_depth
; i
++)
1300 if (old_stack
[i
] != new_stack
[i
])
1304 /* Memorize the previously selected item to be able to refresh it */
1305 old_selection
= last_same
+ 1 < old_depth
? old_stack
[last_same
+ 1] : NULL
;
1306 if (old_selection
&& !old_selection
->enabled
)
1307 old_selection
= NULL
;
1308 new_selection
= last_same
+ 1 < new_depth
? new_stack
[last_same
+ 1] : NULL
;
1309 if (new_selection
&& !new_selection
->enabled
)
1310 new_selection
= NULL
;
1312 /* Call callback when the hightlighted item changes. */
1313 if (old_selection
|| new_selection
)
1314 XtCallCallbackList ((Widget
)mw
, mw
->menu
.highlight
,
1315 (XtPointer
) new_selection
);
1317 /* updates old_state from new_state. It has to be done now because
1318 display_menu (called below) uses the old_stack to know what to display. */
1319 for (i
= last_same
+ 1; i
< new_depth
; i
++)
1320 old_stack
[i
] = new_stack
[i
];
1321 mw
->menu
.old_depth
= new_depth
;
1323 /* refresh the last selection */
1324 selection_position
.x
= 0;
1325 selection_position
.y
= 0;
1326 display_menu (mw
, last_same
, new_selection
== old_selection
,
1327 &selection_position
, NULL
, NULL
, old_selection
, new_selection
);
1329 /* Now place the new menus. */
1330 for (i
= last_same
+ 1; i
< new_depth
&& new_stack
[i
]->contents
; i
++)
1332 window_state
*previous_ws
= &windows
[i
- 1];
1333 window_state
*ws
= &windows
[i
];
1335 ws
->x
= (previous_ws
->x
+ selection_position
.x
1336 + mw
->menu
.shadow_thickness
);
1337 if (mw
->menu
.horizontal
&& i
== 1)
1338 ws
->x
+= mw
->menu
.margin
;
1341 if (!mw
->menu
.horizontal
|| i
> 1)
1342 ws
->x
+= mw
->menu
.shadow_thickness
;
1345 ws
->y
= (previous_ws
->y
+ selection_position
.y
1346 + mw
->menu
.shadow_thickness
);
1347 if (mw
->menu
.horizontal
&& i
== 1)
1348 ws
->y
+= mw
->menu
.margin
;
1352 fit_to_screen (mw
, ws
, previous_ws
, mw
->menu
.horizontal
&& i
== 1);
1354 XClearWindow (XtDisplay (mw
), ws
->window
);
1355 XMoveResizeWindow (XtDisplay (mw
), ws
->window
, ws
->x
, ws
->y
,
1356 ws
->width
, ws
->height
);
1357 XMapRaised (XtDisplay (mw
), ws
->window
);
1358 display_menu (mw
, i
, False
, &selection_position
, NULL
, NULL
, NULL
, NULL
);
1361 /* unmap the menus that popped down */
1362 for (i
= new_depth
- 1; i
< old_depth
; i
++)
1363 if (i
>= new_depth
|| !new_stack
[i
]->contents
)
1364 XUnmapWindow (XtDisplay (mw
), windows
[i
].window
);
1368 motion_event_is_in_menu (mw
, ev
, level
, relative_pos
)
1372 XPoint
* relative_pos
;
1374 window_state
* ws
= &mw
->menu
.windows
[level
];
1375 int shadow
= level
== 0 ? 0 : mw
->menu
.shadow_thickness
;
1376 int x
= ws
->x
+ shadow
;
1377 int y
= ws
->y
+ shadow
;
1378 relative_pos
->x
= ev
->x_root
- x
;
1379 relative_pos
->y
= ev
->y_root
- y
;
1380 return (x
- shadow
< ev
->x_root
&& ev
->x_root
< x
+ ws
->width
1381 && y
- shadow
< ev
->y_root
&& ev
->y_root
< y
+ ws
->height
);
1385 map_event_to_widget_value (mw
, ev
, val
, level
)
1392 XPoint relative_pos
;
1397 /* Find the window */
1398 for (i
= mw
->menu
.old_depth
- 1; i
>= 0; i
--)
1400 ws
= &mw
->menu
.windows
[i
];
1401 if (ws
&& motion_event_is_in_menu (mw
, ev
, i
, &relative_pos
))
1403 display_menu (mw
, i
, True
, NULL
, &relative_pos
, val
, NULL
, NULL
);
1417 make_drawing_gcs (mw
)
1422 xgcv
.font
= mw
->menu
.font
->fid
;
1423 xgcv
.foreground
= mw
->menu
.foreground
;
1424 xgcv
.background
= mw
->core
.background_pixel
;
1425 mw
->menu
.foreground_gc
= XtGetGC ((Widget
)mw
,
1426 GCFont
| GCForeground
| GCBackground
,
1429 xgcv
.font
= mw
->menu
.font
->fid
;
1430 xgcv
.foreground
= mw
->menu
.button_foreground
;
1431 xgcv
.background
= mw
->core
.background_pixel
;
1432 mw
->menu
.button_gc
= XtGetGC ((Widget
)mw
,
1433 GCFont
| GCForeground
| GCBackground
,
1436 xgcv
.font
= mw
->menu
.font
->fid
;
1437 xgcv
.foreground
= mw
->menu
.foreground
;
1438 xgcv
.background
= mw
->core
.background_pixel
;
1439 xgcv
.fill_style
= FillStippled
;
1440 xgcv
.stipple
= mw
->menu
.gray_pixmap
;
1441 mw
->menu
.inactive_gc
= XtGetGC ((Widget
)mw
,
1442 (GCFont
| GCForeground
| GCBackground
1443 | GCFillStyle
| GCStipple
), &xgcv
);
1445 xgcv
.font
= mw
->menu
.font
->fid
;
1446 xgcv
.foreground
= mw
->menu
.button_foreground
;
1447 xgcv
.background
= mw
->core
.background_pixel
;
1448 xgcv
.fill_style
= FillStippled
;
1449 xgcv
.stipple
= mw
->menu
.gray_pixmap
;
1450 mw
->menu
.inactive_button_gc
= XtGetGC ((Widget
)mw
,
1451 (GCFont
| GCForeground
| GCBackground
1452 | GCFillStyle
| GCStipple
), &xgcv
);
1454 xgcv
.font
= mw
->menu
.font
->fid
;
1455 xgcv
.foreground
= mw
->core
.background_pixel
;
1456 xgcv
.background
= mw
->menu
.foreground
;
1457 mw
->menu
.background_gc
= XtGetGC ((Widget
)mw
,
1458 GCFont
| GCForeground
| GCBackground
,
1463 release_drawing_gcs (mw
)
1466 XtReleaseGC ((Widget
) mw
, mw
->menu
.foreground_gc
);
1467 XtReleaseGC ((Widget
) mw
, mw
->menu
.button_gc
);
1468 XtReleaseGC ((Widget
) mw
, mw
->menu
.inactive_gc
);
1469 XtReleaseGC ((Widget
) mw
, mw
->menu
.inactive_button_gc
);
1470 XtReleaseGC ((Widget
) mw
, mw
->menu
.background_gc
);
1471 /* let's get some segvs if we try to use these... */
1472 mw
->menu
.foreground_gc
= (GC
) -1;
1473 mw
->menu
.button_gc
= (GC
) -1;
1474 mw
->menu
.inactive_gc
= (GC
) -1;
1475 mw
->menu
.inactive_button_gc
= (GC
) -1;
1476 mw
->menu
.background_gc
= (GC
) -1;
1479 #define MINL(x,y) ((((unsigned long) (x)) < ((unsigned long) (y))) \
1480 ? ((unsigned long) (x)) : ((unsigned long) (y)))
1483 make_shadow_gcs (mw
)
1487 unsigned long pm
= 0;
1488 Display
*dpy
= XtDisplay ((Widget
) mw
);
1489 Screen
*screen
= XtScreen ((Widget
) mw
);
1490 Colormap cmap
= mw
->core
.colormap
;
1492 int top_frobbed
= 0, bottom_frobbed
= 0;
1494 mw
->menu
.free_top_shadow_color_p
= 0;
1495 mw
->menu
.free_bottom_shadow_color_p
= 0;
1497 if (mw
->menu
.top_shadow_color
== -1)
1498 mw
->menu
.top_shadow_color
= mw
->core
.background_pixel
;
1500 mw
->menu
.top_shadow_color
= mw
->menu
.top_shadow_color
;
1502 if (mw
->menu
.bottom_shadow_color
== -1)
1503 mw
->menu
.bottom_shadow_color
= mw
->menu
.foreground
;
1505 mw
->menu
.bottom_shadow_color
= mw
->menu
.bottom_shadow_color
;
1507 if (mw
->menu
.top_shadow_color
== mw
->core
.background_pixel
||
1508 mw
->menu
.top_shadow_color
== mw
->menu
.foreground
)
1510 topc
.pixel
= mw
->core
.background_pixel
;
1512 if (x_alloc_lighter_color_for_widget ((Widget
) mw
, dpy
, cmap
,
1516 XQueryColor (dpy
, cmap
, &topc
);
1517 /* don't overflow/wrap! */
1518 topc
.red
= MINL (65535, topc
.red
* 1.2);
1519 topc
.green
= MINL (65535, topc
.green
* 1.2);
1520 topc
.blue
= MINL (65535, topc
.blue
* 1.2);
1521 if (XAllocColor (dpy
, cmap
, &topc
))
1524 mw
->menu
.top_shadow_color
= topc
.pixel
;
1525 mw
->menu
.free_top_shadow_color_p
= 1;
1529 if (mw
->menu
.bottom_shadow_color
== mw
->menu
.foreground
||
1530 mw
->menu
.bottom_shadow_color
== mw
->core
.background_pixel
)
1532 botc
.pixel
= mw
->core
.background_pixel
;
1534 if (x_alloc_lighter_color_for_widget ((Widget
) mw
, dpy
, cmap
,
1538 XQueryColor (dpy
, cmap
, &botc
);
1542 if (XAllocColor (dpy
, cmap
, &botc
))
1545 mw
->menu
.bottom_shadow_color
= botc
.pixel
;
1546 mw
->menu
.free_bottom_shadow_color_p
= 1;
1551 if (top_frobbed
&& bottom_frobbed
)
1553 if (topc
.pixel
== botc
.pixel
)
1555 if (botc
.pixel
== mw
->menu
.foreground
)
1557 if (mw
->menu
.free_top_shadow_color_p
)
1559 x_free_dpy_colors (dpy
, screen
, cmap
,
1560 &mw
->menu
.top_shadow_color
, 1);
1561 mw
->menu
.free_top_shadow_color_p
= 0;
1563 mw
->menu
.top_shadow_color
= mw
->core
.background_pixel
;
1567 if (mw
->menu
.free_bottom_shadow_color_p
)
1569 x_free_dpy_colors (dpy
, screen
, cmap
,
1570 &mw
->menu
.bottom_shadow_color
, 1);
1571 mw
->menu
.free_bottom_shadow_color_p
= 0;
1573 mw
->menu
.bottom_shadow_color
= mw
->menu
.foreground
;
1578 if (!mw
->menu
.top_shadow_pixmap
&&
1579 mw
->menu
.top_shadow_color
== mw
->core
.background_pixel
)
1581 mw
->menu
.top_shadow_pixmap
= mw
->menu
.gray_pixmap
;
1582 if (mw
->menu
.free_top_shadow_color_p
)
1584 x_free_dpy_colors (dpy
, screen
, cmap
, &mw
->menu
.top_shadow_color
, 1);
1585 mw
->menu
.free_top_shadow_color_p
= 0;
1587 mw
->menu
.top_shadow_color
= mw
->menu
.foreground
;
1589 if (!mw
->menu
.bottom_shadow_pixmap
&&
1590 mw
->menu
.bottom_shadow_color
== mw
->core
.background_pixel
)
1592 mw
->menu
.bottom_shadow_pixmap
= mw
->menu
.gray_pixmap
;
1593 if (mw
->menu
.free_bottom_shadow_color_p
)
1595 x_free_dpy_colors (dpy
, screen
, cmap
,
1596 &mw
->menu
.bottom_shadow_color
, 1);
1597 mw
->menu
.free_bottom_shadow_color_p
= 0;
1599 mw
->menu
.bottom_shadow_color
= mw
->menu
.foreground
;
1602 xgcv
.fill_style
= FillStippled
;
1603 xgcv
.foreground
= mw
->menu
.top_shadow_color
;
1604 xgcv
.stipple
= mw
->menu
.top_shadow_pixmap
;
1605 pm
= (xgcv
.stipple
? GCStipple
|GCFillStyle
: 0);
1606 mw
->menu
.shadow_top_gc
= XtGetGC ((Widget
)mw
, GCForeground
| pm
, &xgcv
);
1608 xgcv
.foreground
= mw
->menu
.bottom_shadow_color
;
1609 xgcv
.stipple
= mw
->menu
.bottom_shadow_pixmap
;
1610 pm
= (xgcv
.stipple
? GCStipple
|GCFillStyle
: 0);
1611 mw
->menu
.shadow_bottom_gc
= XtGetGC ((Widget
)mw
, GCForeground
| pm
, &xgcv
);
1616 release_shadow_gcs (mw
)
1619 Display
*dpy
= XtDisplay ((Widget
) mw
);
1620 Screen
*screen
= XtScreen ((Widget
) mw
);
1621 Colormap cmap
= mw
->core
.colormap
;
1625 if (mw
->menu
.free_top_shadow_color_p
)
1626 px
[i
++] = mw
->menu
.top_shadow_color
;
1627 if (mw
->menu
.free_bottom_shadow_color_p
)
1628 px
[i
++] = mw
->menu
.bottom_shadow_color
;
1630 x_free_dpy_colors (dpy
, screen
, cmap
, px
, i
);
1632 XtReleaseGC ((Widget
) mw
, mw
->menu
.shadow_top_gc
);
1633 XtReleaseGC ((Widget
) mw
, mw
->menu
.shadow_bottom_gc
);
1637 XlwMenuInitialize (request
, mw
, args
, num_args
)
1643 /* Get the GCs and the widget size */
1644 XSetWindowAttributes xswa
;
1647 Window window
= RootWindowOfScreen (DefaultScreenOfDisplay (XtDisplay (mw
)));
1648 Display
* display
= XtDisplay (mw
);
1651 widget_value
*tem
= (widget_value
*) XtMalloc (sizeof (widget_value
));
1653 /* _XtCreate is freeing the object that was passed to us,
1654 so make a copy that we will actually keep. */
1655 lwlib_bcopy (mw
->menu
.contents
, tem
, sizeof (widget_value
));
1656 mw
->menu
.contents
= tem
;
1659 /* mw->menu.cursor = XCreateFontCursor (display, mw->menu.cursor_shape); */
1660 mw
->menu
.cursor
= mw
->menu
.cursor_shape
;
1662 mw
->menu
.gray_pixmap
1663 = XCreatePixmapFromBitmapData (display
, window
, gray_bitmap_bits
,
1664 gray_bitmap_width
, gray_bitmap_height
,
1665 (unsigned long)1, (unsigned long)0, 1);
1667 /* I don't understand why this ends up 0 sometimes,
1668 but it does. This kludge works around it.
1669 Can anyone find a real fix? -- rms. */
1670 if (mw
->menu
.font
== 0)
1671 mw
->menu
.font
= xlwmenu_default_font
;
1673 make_drawing_gcs (mw
);
1674 make_shadow_gcs (mw
);
1676 xswa
.background_pixel
= mw
->core
.background_pixel
;
1677 xswa
.border_pixel
= mw
->core
.border_pixel
;
1678 mask
= CWBackPixel
| CWBorderPixel
;
1680 mw
->menu
.popped_up
= False
;
1682 mw
->menu
.old_depth
= 1;
1683 mw
->menu
.old_stack
= (widget_value
**)XtMalloc (sizeof (widget_value
*));
1684 mw
->menu
.old_stack_length
= 1;
1685 mw
->menu
.old_stack
[0] = mw
->menu
.contents
;
1687 mw
->menu
.new_depth
= 0;
1688 mw
->menu
.new_stack
= 0;
1689 mw
->menu
.new_stack_length
= 0;
1690 push_new_stack (mw
, mw
->menu
.contents
);
1692 mw
->menu
.windows
= (window_state
*)XtMalloc (sizeof (window_state
));
1693 mw
->menu
.windows_length
= 1;
1694 mw
->menu
.windows
[0].x
= 0;
1695 mw
->menu
.windows
[0].y
= 0;
1696 mw
->menu
.windows
[0].width
= 0;
1697 mw
->menu
.windows
[0].height
= 0;
1700 mw
->core
.width
= mw
->menu
.windows
[0].width
;
1701 mw
->core
.height
= mw
->menu
.windows
[0].height
;
1705 XlwMenuClassInitialize ()
1710 XlwMenuRealize (w
, valueMask
, attributes
)
1713 XSetWindowAttributes
*attributes
;
1715 XlwMenuWidget mw
= (XlwMenuWidget
)w
;
1716 XSetWindowAttributes xswa
;
1719 (*xlwMenuWidgetClass
->core_class
.superclass
->core_class
.realize
)
1720 (w
, valueMask
, attributes
);
1722 xswa
.save_under
= True
;
1723 xswa
.cursor
= mw
->menu
.cursor_shape
;
1724 mask
= CWSaveUnder
| CWCursor
;
1725 XChangeWindowAttributes (XtDisplay (w
), XtWindow (w
), mask
, &xswa
);
1727 mw
->menu
.windows
[0].window
= XtWindow (w
);
1728 mw
->menu
.windows
[0].x
= w
->core
.x
;
1729 mw
->menu
.windows
[0].y
= w
->core
.y
;
1730 mw
->menu
.windows
[0].width
= w
->core
.width
;
1731 mw
->menu
.windows
[0].height
= w
->core
.height
;
1734 /* Only the toplevel menubar/popup is a widget so it's the only one that
1735 receives expose events through Xt. So we repaint all the other panes
1736 when receiving an Expose event. */
1738 XlwMenuRedisplay (w
, ev
, region
)
1743 XlwMenuWidget mw
= (XlwMenuWidget
)w
;
1746 /* If we have a depth beyond 1, it's because a submenu was displayed.
1747 If the submenu has been destroyed, set the depth back to 1. */
1748 if (submenu_destroyed
)
1750 mw
->menu
.old_depth
= 1;
1751 submenu_destroyed
= 0;
1754 for (i
= 0; i
< mw
->menu
.old_depth
; i
++)
1755 display_menu (mw
, i
, False
, NULL
, NULL
, NULL
, NULL
, NULL
);
1759 /* Part of a hack to make the menu redisplay when a tooltip frame
1760 over a menu item is unmapped. */
1763 xlwmenu_redisplay (w
)
1766 XlwMenuRedisplay (w
, NULL
, None
);
1774 XlwMenuWidget mw
= (XlwMenuWidget
) w
;
1776 if (pointer_grabbed
)
1777 XtUngrabPointer ((Widget
)w
, CurrentTime
);
1778 pointer_grabbed
= 0;
1780 submenu_destroyed
= 1;
1782 release_drawing_gcs (mw
);
1783 release_shadow_gcs (mw
);
1785 /* this doesn't come from the resource db but is created explicitly
1786 so we must free it ourselves. */
1787 XFreePixmap (XtDisplay (mw
), mw
->menu
.gray_pixmap
);
1788 mw
->menu
.gray_pixmap
= (Pixmap
) -1;
1791 /* Do free mw->menu.contents because nowadays we copy it
1792 during initialization. */
1793 XtFree (mw
->menu
.contents
);
1796 /* Don't free mw->menu.contents because that comes from our creator.
1797 The `*_stack' elements are just pointers into `contents' so leave
1798 that alone too. But free the stacks themselves. */
1799 if (mw
->menu
.old_stack
) XtFree ((char *) mw
->menu
.old_stack
);
1800 if (mw
->menu
.new_stack
) XtFree ((char *) mw
->menu
.new_stack
);
1802 /* Remember, you can't free anything that came from the resource
1803 database. This includes:
1805 mw->menu.top_shadow_pixmap
1806 mw->menu.bottom_shadow_pixmap
1808 Also the color cells of top_shadow_color, bottom_shadow_color,
1809 foreground, and button_foreground will never be freed until this
1810 client exits. Nice, eh?
1813 /* start from 1 because the one in slot 0 is w->core.window */
1814 for (i
= 1; i
< mw
->menu
.windows_length
; i
++)
1815 XDestroyWindow (XtDisplay (mw
), mw
->menu
.windows
[i
].window
);
1816 if (mw
->menu
.windows
)
1817 XtFree ((char *) mw
->menu
.windows
);
1821 XlwMenuSetValues (current
, request
, new)
1826 XlwMenuWidget oldmw
= (XlwMenuWidget
)current
;
1827 XlwMenuWidget newmw
= (XlwMenuWidget
)new;
1828 Boolean redisplay
= False
;
1831 if (newmw
->menu
.contents
1832 && newmw
->menu
.contents
->contents
1833 && newmw
->menu
.contents
->contents
->change
>= VISIBLE_CHANGE
)
1835 /* Do redisplay if the contents are entirely eliminated. */
1836 if (newmw
->menu
.contents
1837 && newmw
->menu
.contents
->contents
== 0
1838 && newmw
->menu
.contents
->change
>= VISIBLE_CHANGE
)
1841 if (newmw
->core
.background_pixel
!= oldmw
->core
.background_pixel
1842 || newmw
->menu
.foreground
!= oldmw
->menu
.foreground
1843 || newmw
->menu
.font
!= oldmw
->menu
.font
)
1845 release_drawing_gcs (newmw
);
1846 make_drawing_gcs (newmw
);
1848 release_shadow_gcs (newmw
);
1849 /* Cause the shadow colors to be recalculated. */
1850 newmw
->menu
.top_shadow_color
= -1;
1851 newmw
->menu
.bottom_shadow_color
= -1;
1852 make_shadow_gcs (newmw
);
1856 if (XtIsRealized (current
))
1857 /* If the menu is currently displayed, change the display. */
1858 for (i
= 0; i
< oldmw
->menu
.windows_length
; i
++)
1860 XSetWindowBackground (XtDisplay (oldmw
),
1861 oldmw
->menu
.windows
[i
].window
,
1862 newmw
->core
.background_pixel
);
1863 /* clear windows and generate expose events */
1864 XClearArea (XtDisplay (oldmw
), oldmw
->menu
.windows
[i
].window
,
1876 XlwMenuWidget mw
= (XlwMenuWidget
)w
;
1878 if (mw
->menu
.popped_up
)
1880 /* Don't allow the popup menu to resize itself. */
1881 mw
->core
.width
= mw
->menu
.windows
[0].width
;
1882 mw
->core
.height
= mw
->menu
.windows
[0].height
;
1883 mw
->core
.parent
->core
.width
= mw
->core
.width
;
1884 mw
->core
.parent
->core
.height
= mw
->core
.height
;
1888 mw
->menu
.windows
[0].width
= mw
->core
.width
;
1889 mw
->menu
.windows
[0].height
= mw
->core
.height
;
1893 \f/* Action procedures */
1895 handle_single_motion_event (mw
, ev
)
1902 if (!map_event_to_widget_value (mw
, ev
, &val
, &level
))
1903 pop_new_stack_if_no_contents (mw
);
1905 set_new_state (mw
, val
, level
);
1908 /* Sync with the display. Makes it feel better on X terms. */
1909 XSync (XtDisplay (mw
), False
);
1913 handle_motion_event (mw
, ev
)
1919 int state
= ev
->state
;
1921 handle_single_motion_event (mw
, ev
);
1923 /* allow motion events to be generated again */
1925 && XQueryPointer (XtDisplay (mw
), ev
->window
,
1926 &ev
->root
, &ev
->subwindow
,
1927 &ev
->x_root
, &ev
->y_root
,
1930 && ev
->state
== state
1931 && (ev
->x_root
!= x
|| ev
->y_root
!= y
))
1932 handle_single_motion_event (mw
, ev
);
1936 Start (w
, ev
, params
, num_params
)
1940 Cardinal
*num_params
;
1942 XlwMenuWidget mw
= (XlwMenuWidget
)w
;
1944 if (!mw
->menu
.popped_up
)
1946 menu_post_event
= *ev
;
1947 pop_up_menu (mw
, (XButtonPressedEvent
*) ev
);
1951 /* If we push a button while the menu is posted semipermanently,
1952 releasing the button should always pop the menu down. */
1953 next_release_must_exit
= 1;
1955 /* notes the absolute position of the menubar window */
1956 mw
->menu
.windows
[0].x
= ev
->xmotion
.x_root
- ev
->xmotion
.x
;
1957 mw
->menu
.windows
[0].y
= ev
->xmotion
.y_root
- ev
->xmotion
.y
;
1959 /* handles the down like a move, slots are compatible */
1960 handle_motion_event (mw
, &ev
->xmotion
);
1965 Drag (w
, ev
, params
, num_params
)
1969 Cardinal
*num_params
;
1971 XlwMenuWidget mw
= (XlwMenuWidget
)w
;
1972 if (mw
->menu
.popped_up
)
1973 handle_motion_event (mw
, &ev
->xmotion
);
1977 This is how we handle presses and releases of modifier keys. */
1979 Nothing (w
, ev
, params
, num_params
)
1983 Cardinal
*num_params
;
1987 /* Handle key press and release events while menu is popped up.
1988 Our action is to get rid of the menu. */
1990 Key (w
, ev
, params
, num_params
)
1994 Cardinal
*num_params
;
1996 XlwMenuWidget mw
= (XlwMenuWidget
)w
;
1998 /* Pop down everything. */
1999 mw
->menu
.new_depth
= 1;
2002 if (mw
->menu
.popped_up
)
2004 mw
->menu
.popped_up
= False
;
2005 XtUngrabPointer ((Widget
)mw
, ev
->xmotion
.time
);
2006 if (XtIsShell (XtParent ((Widget
) mw
)))
2007 XtPopdown (XtParent ((Widget
) mw
));
2010 XtRemoveGrab ((Widget
) mw
);
2011 display_menu (mw
, 0, False
, NULL
, NULL
, NULL
, NULL
, NULL
);
2016 XtCallCallbackList ((Widget
)mw
, mw
->menu
.select
, (XtPointer
)0);
2020 Select (w
, ev
, params
, num_params
)
2024 Cardinal
*num_params
;
2026 XlwMenuWidget mw
= (XlwMenuWidget
)w
;
2027 widget_value
* selected_item
= mw
->menu
.old_stack
[mw
->menu
.old_depth
- 1];
2029 /* If user releases the button quickly, without selecting anything,
2030 after the initial down-click that brought the menu up,
2032 if ((selected_item
== 0
2033 || ((widget_value
*) selected_item
)->call_data
== 0)
2034 && !next_release_must_exit
2035 && (ev
->xbutton
.time
- menu_post_event
.xbutton
.time
2036 < XtGetMultiClickTime (XtDisplay (w
))))
2039 /* pop down everything. */
2040 mw
->menu
.new_depth
= 1;
2043 if (mw
->menu
.popped_up
)
2045 mw
->menu
.popped_up
= False
;
2046 XtUngrabPointer ((Widget
)mw
, ev
->xmotion
.time
);
2047 if (XtIsShell (XtParent ((Widget
) mw
)))
2048 XtPopdown (XtParent ((Widget
) mw
));
2051 XtRemoveGrab ((Widget
) mw
);
2052 display_menu (mw
, 0, False
, NULL
, NULL
, NULL
, NULL
, NULL
);
2057 XtCallCallbackList ((Widget
)mw
, mw
->menu
.select
, (XtPointer
)selected_item
);
2061 \f/* Special code to pop-up a menu */
2063 pop_up_menu (mw
, event
)
2065 XButtonPressedEvent
* event
;
2067 int x
= event
->x_root
;
2068 int y
= event
->y_root
;
2071 int borderwidth
= mw
->menu
.shadow_thickness
;
2072 Screen
* screen
= XtScreen (mw
);
2073 Display
*display
= XtDisplay (mw
);
2076 next_release_must_exit
= 0;
2078 XtCallCallbackList ((Widget
)mw
, mw
->menu
.open
, NULL
);
2080 if (XtIsShell (XtParent ((Widget
)mw
)))
2083 w
= mw
->menu
.windows
[0].width
;
2084 h
= mw
->menu
.windows
[0].height
;
2088 if (x
< borderwidth
)
2090 if (x
+ w
+ 2 * borderwidth
> WidthOfScreen (screen
))
2091 x
= WidthOfScreen (screen
) - w
- 2 * borderwidth
;
2092 if (y
< borderwidth
)
2094 if (y
+ h
+ 2 * borderwidth
> HeightOfScreen (screen
))
2095 y
= HeightOfScreen (screen
) - h
- 2 * borderwidth
;
2097 mw
->menu
.popped_up
= True
;
2098 if (XtIsShell (XtParent ((Widget
)mw
)))
2100 XtConfigureWidget (XtParent ((Widget
)mw
), x
, y
, w
, h
,
2101 XtParent ((Widget
)mw
)->core
.border_width
);
2102 XtPopup (XtParent ((Widget
)mw
), XtGrabExclusive
);
2103 display_menu (mw
, 0, False
, NULL
, NULL
, NULL
, NULL
, NULL
);
2104 mw
->menu
.windows
[0].x
= x
+ borderwidth
;
2105 mw
->menu
.windows
[0].y
= y
+ borderwidth
;
2109 XEvent
*ev
= (XEvent
*) event
;
2111 XtAddGrab ((Widget
) mw
, True
, True
);
2113 /* notes the absolute position of the menubar window */
2114 mw
->menu
.windows
[0].x
= ev
->xmotion
.x_root
- ev
->xmotion
.x
;
2115 mw
->menu
.windows
[0].y
= ev
->xmotion
.y_root
- ev
->xmotion
.y
;
2119 count
= x_catch_errors (display
);
2121 XtGrabPointer ((Widget
)mw
, False
,
2123 | PointerMotionHintMask
2126 GrabModeAsync
, GrabModeAsync
, None
,
2127 mw
->menu
.cursor_shape
,
2129 pointer_grabbed
= 1;
2131 if (x_had_errors_p (display
))
2133 pointer_grabbed
= 0;
2134 XtUngrabPointer ((Widget
)mw
, event
->time
);
2136 x_uncatch_errors (display
, count
);
2139 handle_motion_event (mw
, (XMotionEvent
*)event
);