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 Alongalong with GNU Emacs; see the file COPYING. If not, write to
18 the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
20 /* Created by devin@lucid.com */
24 #include <sys/types.h>
26 #include <X11/IntrinsicP.h>
27 #include <X11/ObjectP.h>
28 #include <X11/StringDefs.h>
29 #include <X11/cursorfont.h>
30 #include <X11/bitmaps/gray>
33 static int pointer_grabbed
;
34 static XEvent menu_post_event
;
36 XFontStruct
*xlwmenu_default_font
;
39 xlwMenuTranslations
[] =
40 "<BtnDown>: start()\n\
45 #define offset(field) XtOffset(XlwMenuWidget, field)
49 {XtNfont
, XtCFont
, XtRFontStruct
, sizeof(XFontStruct
*),
50 offset(menu
.font
),XtRString
, "XtDefaultFont"},
51 {XtNforeground
, XtCForeground
, XtRPixel
, sizeof(Pixel
),
52 offset(menu
.foreground
), XtRString
, "XtDefaultForeground"},
53 {XtNbuttonForeground
, XtCButtonForeground
, XtRPixel
, sizeof(Pixel
),
54 offset(menu
.button_foreground
), XtRString
, "XtDefaultForeground"},
55 {XtNmargin
, XtCMargin
, XtRDimension
, sizeof(Dimension
),
56 offset(menu
.margin
), XtRImmediate
, (XtPointer
)0},
57 {XtNhorizontalSpacing
, XtCMargin
, XtRDimension
, sizeof(Dimension
),
58 offset(menu
.horizontal_spacing
), XtRImmediate
, (XtPointer
)3},
59 {XtNverticalSpacing
, XtCMargin
, XtRDimension
, sizeof(Dimension
),
60 offset(menu
.vertical_spacing
), XtRImmediate
, (XtPointer
)1},
61 {XtNarrowSpacing
, XtCMargin
, XtRDimension
, sizeof(Dimension
),
62 offset(menu
.arrow_spacing
), XtRImmediate
, (XtPointer
)10},
64 {XmNshadowThickness
, XmCShadowThickness
, XtRDimension
,
65 sizeof (Dimension
), offset (menu
.shadow_thickness
),
66 XtRImmediate
, (XtPointer
) 2},
67 {XmNtopShadowColor
, XmCTopShadowColor
, XtRPixel
, sizeof (Pixel
),
68 offset (menu
.top_shadow_color
), XtRImmediate
, (XtPointer
)-1},
69 {XmNbottomShadowColor
, XmCBottomShadowColor
, XtRPixel
, sizeof (Pixel
),
70 offset (menu
.bottom_shadow_color
), XtRImmediate
, (XtPointer
)-1},
71 {XmNtopShadowPixmap
, XmCTopShadowPixmap
, XtRPixmap
, sizeof (Pixmap
),
72 offset (menu
.top_shadow_pixmap
), XtRImmediate
, (XtPointer
)None
},
73 {XmNbottomShadowPixmap
, XmCBottomShadowPixmap
, XtRPixmap
, sizeof (Pixmap
),
74 offset (menu
.bottom_shadow_pixmap
), XtRImmediate
, (XtPointer
)None
},
76 {XtNopen
, XtCCallback
, XtRCallback
, sizeof(XtPointer
),
77 offset(menu
.open
), XtRCallback
, (XtPointer
)NULL
},
78 {XtNselect
, XtCCallback
, XtRCallback
, sizeof(XtPointer
),
79 offset(menu
.select
), XtRCallback
, (XtPointer
)NULL
},
80 {XtNmenu
, XtCMenu
, XtRPointer
, sizeof(XtPointer
),
81 offset(menu
.contents
), XtRImmediate
, (XtPointer
)NULL
},
82 {XtNcursor
, XtCCursor
, XtRCursor
, sizeof(Cursor
),
83 offset(menu
.cursor_shape
), XtRString
, (XtPointer
)"right_ptr"},
84 {XtNhorizontal
, XtCHorizontal
, XtRInt
, sizeof(int),
85 offset(menu
.horizontal
), XtRImmediate
, (XtPointer
)True
},
89 static Boolean
XlwMenuSetValues();
90 static void XlwMenuRealize();
91 static void XlwMenuRedisplay();
92 static void XlwMenuResize();
93 static void XlwMenuInitialize();
94 static void XlwMenuRedisplay();
95 static void XlwMenuDestroy();
96 static void XlwMenuClassInitialize();
102 xlwMenuActionsList
[] =
109 #define SuperClass ((CoreWidgetClass)&coreClassRec)
111 XlwMenuClassRec xlwMenuClassRec
=
113 { /* CoreClass fields initialization */
114 (WidgetClass
) SuperClass
, /* superclass */
115 "XlwMenu", /* class_name */
116 sizeof(XlwMenuRec
), /* size */
117 XlwMenuClassInitialize
, /* class_initialize */
118 NULL
, /* class_part_initialize */
119 FALSE
, /* class_inited */
120 XlwMenuInitialize
, /* initialize */
121 NULL
, /* initialize_hook */
122 XlwMenuRealize
, /* realize */
123 xlwMenuActionsList
, /* actions */
124 XtNumber(xlwMenuActionsList
), /* num_actions */
125 xlwMenuResources
, /* resources */
126 XtNumber(xlwMenuResources
), /* resource_count */
127 NULLQUARK
, /* xrm_class */
128 TRUE
, /* compress_motion */
129 TRUE
, /* compress_exposure */
130 TRUE
, /* compress_enterleave */
131 FALSE
, /* visible_interest */
132 XlwMenuDestroy
, /* destroy */
133 XlwMenuResize
, /* resize */
134 XlwMenuRedisplay
, /* expose */
135 XlwMenuSetValues
, /* set_values */
136 NULL
, /* set_values_hook */
137 XtInheritSetValuesAlmost
, /* set_values_almost */
138 NULL
, /* get_values_hook */
139 NULL
, /* accept_focus */
140 XtVersion
, /* version */
141 NULL
, /* callback_private */
142 xlwMenuTranslations
, /* tm_table */
143 XtInheritQueryGeometry
, /* query_geometry */
144 XtInheritDisplayAccelerator
, /* display_accelerator */
146 }, /* XlwMenuClass fields initialization */
152 WidgetClass xlwMenuWidgetClass
= (WidgetClass
) &xlwMenuClassRec
;
154 int submenu_destroyed
;
156 static int next_release_must_exit
;
160 push_new_stack (mw
, val
)
164 if (!mw
->menu
.new_stack
)
166 mw
->menu
.new_stack_length
= 10;
168 (widget_value
**)XtCalloc (mw
->menu
.new_stack_length
,
169 sizeof (widget_value
*));
171 else if (mw
->menu
.new_depth
== mw
->menu
.new_stack_length
)
173 mw
->menu
.new_stack_length
*= 2;
175 (widget_value
**)XtRealloc ((char*)mw
->menu
.new_stack
,
176 mw
->menu
.new_stack_length
* sizeof (widget_value
*));
178 mw
->menu
.new_stack
[mw
->menu
.new_depth
++] = val
;
182 pop_new_stack_if_no_contents (mw
)
185 if (mw
->menu
.new_depth
)
187 if (!mw
->menu
.new_stack
[mw
->menu
.new_depth
- 1]->contents
)
188 mw
->menu
.new_depth
-= 1;
193 make_old_stack_space (mw
, n
)
197 if (!mw
->menu
.old_stack
)
199 mw
->menu
.old_stack_length
= 10;
201 (widget_value
**)XtCalloc (mw
->menu
.old_stack_length
,
202 sizeof (widget_value
*));
204 else if (mw
->menu
.old_stack_length
< n
)
206 mw
->menu
.old_stack_length
*= 2;
208 (widget_value
**)XtRealloc ((char*)mw
->menu
.old_stack
,
209 mw
->menu
.old_stack_length
* sizeof (widget_value
*));
219 for (p
= s
; *p
== '-'; p
++);
231 XTextExtents (mw
->menu
.font
, s
, strlen (s
), &drop
, &drop
, &drop
, &xcs
);
239 return mw
->menu
.font
->ascent
/ 2 | 1;
245 {"labelString", "LabelString", XtRString
, sizeof(String
),
250 resource_widget_value (mw
, val
)
254 if (!val
->toolkit_data
)
256 char* resourced_name
= NULL
;
258 XtGetSubresources ((Widget
) mw
,
259 (XtPointer
) &resourced_name
,
260 val
->name
, val
->name
,
261 nameResource
, 1, NULL
, 0);
263 resourced_name
= val
->name
;
266 complete_name
= (char *) XtMalloc (strlen (resourced_name
) + 1);
267 strcpy (complete_name
, resourced_name
);
271 int complete_length
=
272 strlen (resourced_name
) + strlen (val
->value
) + 2;
273 complete_name
= XtMalloc (complete_length
);
275 strcat (complete_name
, resourced_name
);
276 strcat (complete_name
, " ");
277 strcat (complete_name
, val
->value
);
280 val
->toolkit_data
= complete_name
;
281 val
->free_toolkit_data
= True
;
283 return (char*)val
->toolkit_data
;
286 /* Returns the sizes of an item */
288 size_menu_item (mw
, val
, horizontal_p
, label_width
, rest_width
, height
)
296 if (all_dashes_p (val
->name
))
305 mw
->menu
.font
->ascent
+ mw
->menu
.font
->descent
306 + 2 * mw
->menu
.vertical_spacing
+ 2 * mw
->menu
.shadow_thickness
;
309 string_width (mw
, resource_widget_value (mw
, val
))
310 + mw
->menu
.horizontal_spacing
+ mw
->menu
.shadow_thickness
;
312 *rest_width
= mw
->menu
.horizontal_spacing
+ mw
->menu
.shadow_thickness
;
316 *rest_width
+= arrow_width (mw
) + mw
->menu
.arrow_spacing
;
319 string_width (mw
, val
->key
) + mw
->menu
.arrow_spacing
;
325 size_menu (mw
, level
)
331 int max_rest_width
= 0;
333 int horizontal_p
= mw
->menu
.horizontal
&& (level
== 0);
337 if (level
>= mw
->menu
.old_depth
)
340 ws
= &mw
->menu
.windows
[level
];
345 for (val
= mw
->menu
.old_stack
[level
]->contents
; val
; val
= val
->next
)
347 size_menu_item (mw
, val
, horizontal_p
, &label_width
, &rest_width
,
351 ws
->width
+= label_width
+ rest_width
;
352 if (height
> ws
->height
)
357 if (label_width
> ws
->label_width
)
358 ws
->label_width
= label_width
;
359 if (rest_width
> max_rest_width
)
360 max_rest_width
= rest_width
;
361 ws
->height
+= height
;
368 ws
->width
= ws
->label_width
+ max_rest_width
;
370 ws
->width
+= 2 * mw
->menu
.shadow_thickness
;
371 ws
->height
+= 2 * mw
->menu
.shadow_thickness
;
377 draw_arrow (mw
, window
, gc
, x
, y
, width
)
387 points
[0].y
= y
+ mw
->menu
.font
->ascent
;
390 points
[2].x
= x
+ width
;
391 points
[2].y
= y
+ mw
->menu
.font
->ascent
/ 2;
393 XFillPolygon (XtDisplay (mw
), window
, gc
, points
, 3, Convex
,
398 draw_shadow_rectangle (mw
, window
, x
, y
, width
, height
, erase_p
)
407 Display
*dpy
= XtDisplay (mw
);
408 GC top_gc
= !erase_p
? mw
->menu
.shadow_top_gc
: mw
->menu
.background_gc
;
409 GC bottom_gc
= !erase_p
? mw
->menu
.shadow_bottom_gc
: mw
->menu
.background_gc
;
410 int thickness
= mw
->menu
.shadow_thickness
;
414 points
[1].x
= x
+ width
;
416 points
[2].x
= x
+ width
- thickness
;
417 points
[2].y
= y
+ thickness
;
419 points
[3].y
= y
+ thickness
;
420 XFillPolygon (dpy
, window
, top_gc
, points
, 4, Convex
, CoordModeOrigin
);
422 points
[0].y
= y
+ thickness
;
424 points
[1].y
= y
+ height
;
425 points
[2].x
= x
+ thickness
;
426 points
[2].y
= y
+ height
- thickness
;
427 points
[3].x
= x
+ thickness
;
428 points
[3].y
= y
+ thickness
;
429 XFillPolygon (dpy
, window
, top_gc
, points
, 4, Convex
, CoordModeOrigin
);
430 points
[0].x
= x
+ width
;
432 points
[1].x
= x
+ width
- thickness
;
433 points
[1].y
= y
+ thickness
;
434 points
[2].x
= x
+ width
- thickness
;
435 points
[2].y
= y
+ height
- thickness
;
436 points
[3].x
= x
+ width
;
437 points
[3].y
= y
+ height
- thickness
;
438 XFillPolygon (dpy
, window
, bottom_gc
, points
, 4, Convex
, CoordModeOrigin
);
440 points
[0].y
= y
+ height
;
441 points
[1].x
= x
+ width
;
442 points
[1].y
= y
+ height
;
443 points
[2].x
= x
+ width
;
444 points
[2].y
= y
+ height
- thickness
;
445 points
[3].x
= x
+ thickness
;
446 points
[3].y
= y
+ height
- thickness
;
447 XFillPolygon (dpy
, window
, bottom_gc
, points
, 4, Convex
, CoordModeOrigin
);
451 /* Display the menu item and increment where.x and where.y to show how large
452 ** the menu item was.
455 display_menu_item (mw
, val
, ws
, where
, highlighted_p
, horizontal_p
, just_compute_p
)
460 Boolean highlighted_p
;
461 Boolean horizontal_p
;
462 Boolean just_compute_p
;
466 int font_ascent
= mw
->menu
.font
->ascent
;
467 int font_descent
= mw
->menu
.font
->descent
;
468 int shadow
= mw
->menu
.shadow_thickness
;
469 int separator_p
= all_dashes_p (val
->name
);
470 int h_spacing
= mw
->menu
.horizontal_spacing
;
471 int v_spacing
= mw
->menu
.vertical_spacing
;
478 /* compute the sizes of the item */
479 size_menu_item (mw
, val
, horizontal_p
, &label_width
, &rest_width
, &height
);
482 width
= label_width
+ rest_width
;
485 label_width
= ws
->label_width
;
486 width
= ws
->width
- 2 * shadow
;
489 /* see if it should be a button in the menubar */
490 button_p
= horizontal_p
&& val
->call_data
;
492 /* Only highlight an enabled item that has a callback. */
494 if (!val
->enabled
|| !(val
->call_data
|| val
->contents
))
497 /* do the drawing. */
500 /* Add the shadow border of the containing menu */
501 int x
= where
->x
+ shadow
;
502 int y
= where
->y
+ shadow
;
504 /* pick the foreground and background GC. */
506 text_gc
= button_p
? mw
->menu
.button_gc
: mw
->menu
.foreground_gc
;
509 button_p
? mw
->menu
.inactive_button_gc
: mw
->menu
.inactive_gc
;
510 deco_gc
= mw
->menu
.foreground_gc
;
514 XDrawLine (XtDisplay (mw
), ws
->window
, mw
->menu
.shadow_bottom_gc
,
516 XDrawLine (XtDisplay (mw
), ws
->window
, mw
->menu
.shadow_top_gc
,
517 x
, y
+ 1, x
+ width
, y
+ 1);
521 int x_offset
= x
+ h_spacing
+ shadow
;
522 char* display_string
= resource_widget_value (mw
, val
);
523 draw_shadow_rectangle (mw
, ws
->window
, x
, y
, width
, height
, True
);
525 /* Deal with centering a menu title. */
526 if (!horizontal_p
&& !val
->contents
&& !val
->call_data
)
528 int l
= string_width (mw
, display_string
);
531 x_offset
= (width
- l
) >> 1;
533 XDrawString (XtDisplay (mw
), ws
->window
, text_gc
, x_offset
,
534 y
+ v_spacing
+ shadow
+ font_ascent
,
535 display_string
, strlen (display_string
));
541 int a_w
= arrow_width (mw
);
542 draw_arrow (mw
, ws
->window
, deco_gc
,
543 x
+ width
- arrow_width (mw
)
544 - mw
->menu
.horizontal_spacing
545 - mw
->menu
.shadow_thickness
,
546 y
+ v_spacing
+ shadow
, a_w
);
550 XDrawString (XtDisplay (mw
), ws
->window
, text_gc
,
551 x
+ label_width
+ mw
->menu
.arrow_spacing
,
552 y
+ v_spacing
+ shadow
+ font_ascent
,
553 val
->key
, strlen (val
->key
));
560 XDrawRectangle (XtDisplay (mw
), ws
->window
, deco_gc
,
561 x
+ shadow
, y
+ shadow
,
562 label_width
+ h_spacing
- 1,
563 font_ascent
+ font_descent
+ 2 * v_spacing
- 1);
564 draw_shadow_rectangle (mw
, ws
->window
, x
, y
, width
, height
,
567 highlighted_p
= True
;
572 XDrawRectangle (XtDisplay (mw
), ws
->window
,
573 mw
->menu
.background_gc
,
574 x
+ shadow
, y
+ shadow
,
575 label_width
+ h_spacing
- 1,
576 font_ascent
+ font_descent
+ 2 * v_spacing
- 1);
577 draw_shadow_rectangle (mw
, ws
->window
, x
, y
, width
, height
,
582 draw_shadow_rectangle (mw
, ws
->window
, x
, y
, width
, height
, False
);
591 display_menu (mw
, level
, just_compute_p
, highlighted_pos
, hit
, hit_return
,
595 Boolean just_compute_p
;
596 XPoint
* highlighted_pos
;
598 widget_value
** hit_return
;
603 widget_value
* following_item
;
606 int horizontal_p
= mw
->menu
.horizontal
&& (level
== 0);
608 int just_compute_this_one_p
;
610 if (level
>= mw
->menu
.old_depth
)
613 if (level
< mw
->menu
.old_depth
- 1)
614 following_item
= mw
->menu
.old_stack
[level
+ 1];
616 following_item
= NULL
;
624 ws
= &mw
->menu
.windows
[level
];
625 for (val
= mw
->menu
.old_stack
[level
]->contents
; val
; val
= val
->next
)
627 highlighted_p
= val
== following_item
;
628 if (highlighted_p
&& highlighted_pos
)
631 highlighted_pos
->x
= where
.x
;
633 highlighted_pos
->y
= where
.y
;
636 just_compute_this_one_p
=
637 just_compute_p
|| ((this || that
) && val
!= this && val
!= that
);
639 display_menu_item (mw
, val
, ws
, &where
, highlighted_p
, horizontal_p
,
640 just_compute_this_one_p
);
642 if (highlighted_p
&& highlighted_pos
)
645 highlighted_pos
->y
= where
.y
;
647 highlighted_pos
->x
= where
.x
;
652 && (horizontal_p
? hit
->x
< where
.x
: hit
->y
< where
.y
)
653 && !all_dashes_p (val
->name
))
663 draw_shadow_rectangle (mw
, ws
->window
, 0, 0, ws
->width
, ws
->height
, False
);
668 set_new_state (mw
, val
, level
)
675 mw
->menu
.new_depth
= 0;
676 for (i
= 0; i
< level
; i
++)
677 push_new_stack (mw
, mw
->menu
.old_stack
[i
]);
678 push_new_stack (mw
, val
);
682 make_windows_if_needed (mw
, n
)
688 XSetWindowAttributes xswa
;
690 Window root
= RootWindowOfScreen (DefaultScreenOfDisplay (XtDisplay (mw
)));
691 window_state
* windows
;
693 if (mw
->menu
.windows_length
>= n
)
696 xswa
.save_under
= True
;
697 xswa
.override_redirect
= True
;
698 xswa
.background_pixel
= mw
->core
.background_pixel
;
699 xswa
.border_pixel
= mw
->core
.border_pixel
;
701 ExposureMask
| PointerMotionMask
| PointerMotionHintMask
702 | ButtonReleaseMask
| ButtonPressMask
;
703 xswa
.cursor
= mw
->menu
.cursor_shape
;
704 mask
= CWSaveUnder
| CWOverrideRedirect
| CWBackPixel
| CWBorderPixel
705 | CWEventMask
| CWCursor
;
707 if (!mw
->menu
.windows
)
710 (window_state
*)XtMalloc (n
* sizeof (window_state
));
716 (window_state
*)XtRealloc ((char*)mw
->menu
.windows
,
717 n
* sizeof (window_state
));
718 start_at
= mw
->menu
.windows_length
;
720 mw
->menu
.windows_length
= n
;
722 windows
= mw
->menu
.windows
;
724 for (i
= start_at
; i
< n
; i
++)
728 windows
[i
].width
= 1;
729 windows
[i
].height
= 1;
731 XCreateWindow (XtDisplay (mw
), root
, 0, 0, 1, 1,
732 0, 0, CopyFromParent
, CopyFromParent
, mask
, &xswa
);
736 /* Make the window fit in the screen */
738 fit_to_screen (mw
, ws
, previous_ws
, horizontal_p
)
741 window_state
* previous_ws
;
742 Boolean horizontal_p
;
744 int screen_width
= WidthOfScreen (XtScreen (mw
));
745 int screen_height
= HeightOfScreen (XtScreen (mw
));
749 else if (ws
->x
+ ws
->width
> screen_width
)
752 ws
->x
= previous_ws
->x
- ws
->width
;
754 ws
->x
= screen_width
- ws
->width
;
760 else if (ws
->y
+ ws
->height
> screen_height
)
763 ws
->y
= previous_ws
->y
- ws
->height
;
765 ws
->y
= screen_height
- ws
->height
;
771 /* Updates old_stack from new_stack and redisplays. */
778 XPoint selection_position
;
779 int old_depth
= mw
->menu
.old_depth
;
780 int new_depth
= mw
->menu
.new_depth
;
781 widget_value
** old_stack
;
782 widget_value
** new_stack
;
783 window_state
* windows
;
784 widget_value
* old_selection
;
785 widget_value
* new_selection
;
787 /* Check that enough windows and old_stack are ready. */
788 make_windows_if_needed (mw
, new_depth
);
789 make_old_stack_space (mw
, new_depth
);
790 windows
= mw
->menu
.windows
;
791 old_stack
= mw
->menu
.old_stack
;
792 new_stack
= mw
->menu
.new_stack
;
794 /* compute the last identical different entry */
795 for (i
= 1; i
< old_depth
&& i
< new_depth
; i
++)
796 if (old_stack
[i
] != new_stack
[i
])
800 /* Memorize the previously selected item to be able to refresh it */
801 old_selection
= last_same
+ 1 < old_depth
? old_stack
[last_same
+ 1] : NULL
;
802 if (old_selection
&& !old_selection
->enabled
)
803 old_selection
= NULL
;
804 new_selection
= last_same
+ 1 < new_depth
? new_stack
[last_same
+ 1] : NULL
;
805 if (new_selection
&& !new_selection
->enabled
)
806 new_selection
= NULL
;
808 /* updates old_state from new_state. It has to be done now because
809 display_menu (called below) uses the old_stack to know what to display. */
810 for (i
= last_same
+ 1; i
< new_depth
; i
++)
811 old_stack
[i
] = new_stack
[i
];
812 mw
->menu
.old_depth
= new_depth
;
814 /* refresh the last seletion */
815 selection_position
.x
= 0;
816 selection_position
.y
= 0;
817 display_menu (mw
, last_same
, new_selection
== old_selection
,
818 &selection_position
, NULL
, NULL
, old_selection
, new_selection
);
820 /* Now popup the new menus */
821 for (i
= last_same
+ 1; i
< new_depth
&& new_stack
[i
]->contents
; i
++)
823 window_state
* previous_ws
= &windows
[i
- 1];
824 window_state
* ws
= &windows
[i
];
827 previous_ws
->x
+ selection_position
.x
+ mw
->menu
.shadow_thickness
;
828 if (!mw
->menu
.horizontal
|| i
> 1)
829 ws
->x
+= mw
->menu
.shadow_thickness
;
831 previous_ws
->y
+ selection_position
.y
+ mw
->menu
.shadow_thickness
;
835 fit_to_screen (mw
, ws
, previous_ws
, mw
->menu
.horizontal
&& i
== 1);
837 XClearWindow (XtDisplay (mw
), ws
->window
);
838 XMoveResizeWindow (XtDisplay (mw
), ws
->window
, ws
->x
, ws
->y
,
839 ws
->width
, ws
->height
);
840 XMapRaised (XtDisplay (mw
), ws
->window
);
841 display_menu (mw
, i
, False
, &selection_position
, NULL
, NULL
, NULL
, NULL
);
844 /* unmap the menus that popped down */
845 for (i
= new_depth
- 1; i
< old_depth
; i
++)
846 if (i
>= new_depth
|| !new_stack
[i
]->contents
)
847 XUnmapWindow (XtDisplay (mw
), windows
[i
].window
);
851 motion_event_is_in_menu (mw
, ev
, level
, relative_pos
)
855 XPoint
* relative_pos
;
857 window_state
* ws
= &mw
->menu
.windows
[level
];
858 int x
= level
== 0 ? ws
->x
: ws
->x
+ mw
->menu
.shadow_thickness
;
859 int y
= level
== 0 ? ws
->y
: ws
->y
+ mw
->menu
.shadow_thickness
;
860 relative_pos
->x
= ev
->x_root
- x
;
861 relative_pos
->y
= ev
->y_root
- y
;
862 return (x
< ev
->x_root
&& ev
->x_root
< x
+ ws
->width
863 && y
< ev
->y_root
&& ev
->y_root
< y
+ ws
->height
);
867 map_event_to_widget_value (mw
, ev
, val
, level
)
879 /* Find the window */
880 for (i
= mw
->menu
.old_depth
- 1; i
>= 0; i
--)
882 ws
= &mw
->menu
.windows
[i
];
883 if (ws
&& motion_event_is_in_menu (mw
, ev
, i
, &relative_pos
))
885 display_menu (mw
, i
, True
, NULL
, &relative_pos
, val
, NULL
, NULL
);
899 make_drawing_gcs (mw
)
904 xgcv
.font
= mw
->menu
.font
->fid
;
905 xgcv
.foreground
= mw
->menu
.foreground
;
906 xgcv
.background
= mw
->core
.background_pixel
;
907 mw
->menu
.foreground_gc
= XtGetGC ((Widget
)mw
,
908 GCFont
| GCForeground
| GCBackground
,
911 xgcv
.font
= mw
->menu
.font
->fid
;
912 xgcv
.foreground
= mw
->menu
.button_foreground
;
913 xgcv
.background
= mw
->core
.background_pixel
;
914 mw
->menu
.button_gc
= XtGetGC ((Widget
)mw
,
915 GCFont
| GCForeground
| GCBackground
,
918 xgcv
.font
= mw
->menu
.font
->fid
;
919 xgcv
.foreground
= mw
->menu
.foreground
;
920 xgcv
.background
= mw
->core
.background_pixel
;
921 xgcv
.fill_style
= FillStippled
;
922 xgcv
.stipple
= mw
->menu
.gray_pixmap
;
923 mw
->menu
.inactive_gc
= XtGetGC ((Widget
)mw
,
924 (GCFont
| GCForeground
| GCBackground
925 | GCFillStyle
| GCStipple
), &xgcv
);
927 xgcv
.font
= mw
->menu
.font
->fid
;
928 xgcv
.foreground
= mw
->menu
.button_foreground
;
929 xgcv
.background
= mw
->core
.background_pixel
;
930 xgcv
.fill_style
= FillStippled
;
931 xgcv
.stipple
= mw
->menu
.gray_pixmap
;
932 mw
->menu
.inactive_button_gc
= XtGetGC ((Widget
)mw
,
933 (GCFont
| GCForeground
| GCBackground
934 | GCFillStyle
| GCStipple
), &xgcv
);
936 xgcv
.font
= mw
->menu
.font
->fid
;
937 xgcv
.foreground
= mw
->core
.background_pixel
;
938 xgcv
.background
= mw
->menu
.foreground
;
939 mw
->menu
.background_gc
= XtGetGC ((Widget
)mw
,
940 GCFont
| GCForeground
| GCBackground
,
945 release_drawing_gcs (mw
)
948 XtReleaseGC ((Widget
) mw
, mw
->menu
.foreground_gc
);
949 XtReleaseGC ((Widget
) mw
, mw
->menu
.button_gc
);
950 XtReleaseGC ((Widget
) mw
, mw
->menu
.inactive_gc
);
951 XtReleaseGC ((Widget
) mw
, mw
->menu
.inactive_button_gc
);
952 XtReleaseGC ((Widget
) mw
, mw
->menu
.background_gc
);
953 /* let's get some segvs if we try to use these... */
954 mw
->menu
.foreground_gc
= (GC
) -1;
955 mw
->menu
.button_gc
= (GC
) -1;
956 mw
->menu
.inactive_gc
= (GC
) -1;
957 mw
->menu
.inactive_button_gc
= (GC
) -1;
958 mw
->menu
.background_gc
= (GC
) -1;
961 #define MINL(x,y) ((((unsigned long) (x)) < ((unsigned long) (y))) \
962 ? ((unsigned long) (x)) : ((unsigned long) (y)))
969 unsigned long pm
= 0;
970 Display
*dpy
= XtDisplay ((Widget
) mw
);
971 Colormap cmap
= DefaultColormapOfScreen (XtScreen ((Widget
) mw
));
973 int top_frobbed
= 0, bottom_frobbed
= 0;
975 if (mw
->menu
.top_shadow_color
== -1)
976 mw
->menu
.top_shadow_color
= mw
->core
.background_pixel
;
977 if (mw
->menu
.bottom_shadow_color
== -1)
978 mw
->menu
.bottom_shadow_color
= mw
->menu
.foreground
;
980 if (mw
->menu
.top_shadow_color
== mw
->core
.background_pixel
||
981 mw
->menu
.top_shadow_color
== mw
->menu
.foreground
)
983 topc
.pixel
= mw
->core
.background_pixel
;
984 XQueryColor (dpy
, cmap
, &topc
);
985 /* don't overflow/wrap! */
986 topc
.red
= MINL (65535, topc
.red
* 1.2);
987 topc
.green
= MINL (65535, topc
.green
* 1.2);
988 topc
.blue
= MINL (65535, topc
.blue
* 1.2);
989 if (XAllocColor (dpy
, cmap
, &topc
))
991 mw
->menu
.top_shadow_color
= topc
.pixel
;
995 if (mw
->menu
.bottom_shadow_color
== mw
->menu
.foreground
||
996 mw
->menu
.bottom_shadow_color
== mw
->core
.background_pixel
)
998 botc
.pixel
= mw
->core
.background_pixel
;
999 XQueryColor (dpy
, cmap
, &botc
);
1003 if (XAllocColor (dpy
, cmap
, &botc
))
1005 mw
->menu
.bottom_shadow_color
= botc
.pixel
;
1010 if (top_frobbed
&& bottom_frobbed
)
1012 int top_avg
= ((topc
.red
/ 3) + (topc
.green
/ 3) + (topc
.blue
/ 3));
1013 int bot_avg
= ((botc
.red
/ 3) + (botc
.green
/ 3) + (botc
.blue
/ 3));
1014 if (bot_avg
> top_avg
)
1016 Pixel tmp
= mw
->menu
.top_shadow_color
;
1017 mw
->menu
.top_shadow_color
= mw
->menu
.bottom_shadow_color
;
1018 mw
->menu
.bottom_shadow_color
= tmp
;
1020 else if (topc
.pixel
== botc
.pixel
)
1022 if (botc
.pixel
== mw
->menu
.foreground
)
1023 mw
->menu
.top_shadow_color
= mw
->core
.background_pixel
;
1025 mw
->menu
.bottom_shadow_color
= mw
->menu
.foreground
;
1029 if (!mw
->menu
.top_shadow_pixmap
&&
1030 mw
->menu
.top_shadow_color
== mw
->core
.background_pixel
)
1032 mw
->menu
.top_shadow_pixmap
= mw
->menu
.gray_pixmap
;
1033 mw
->menu
.top_shadow_color
= mw
->menu
.foreground
;
1035 if (!mw
->menu
.bottom_shadow_pixmap
&&
1036 mw
->menu
.bottom_shadow_color
== mw
->core
.background_pixel
)
1038 mw
->menu
.bottom_shadow_pixmap
= mw
->menu
.gray_pixmap
;
1039 mw
->menu
.bottom_shadow_color
= mw
->menu
.foreground
;
1042 xgcv
.fill_style
= FillStippled
;
1043 xgcv
.foreground
= mw
->menu
.top_shadow_color
;
1044 xgcv
.stipple
= mw
->menu
.top_shadow_pixmap
;
1045 pm
= (xgcv
.stipple
? GCStipple
|GCFillStyle
: 0);
1046 mw
->menu
.shadow_top_gc
= XtGetGC ((Widget
)mw
, GCForeground
| pm
, &xgcv
);
1048 xgcv
.foreground
= mw
->menu
.bottom_shadow_color
;
1049 xgcv
.stipple
= mw
->menu
.bottom_shadow_pixmap
;
1050 pm
= (xgcv
.stipple
? GCStipple
|GCFillStyle
: 0);
1051 mw
->menu
.shadow_bottom_gc
= XtGetGC ((Widget
)mw
, GCForeground
| pm
, &xgcv
);
1056 release_shadow_gcs (mw
)
1059 XtReleaseGC ((Widget
) mw
, mw
->menu
.shadow_top_gc
);
1060 XtReleaseGC ((Widget
) mw
, mw
->menu
.shadow_bottom_gc
);
1064 XlwMenuInitialize (request
, mw
, args
, num_args
)
1070 /* Get the GCs and the widget size */
1071 XSetWindowAttributes xswa
;
1074 Window window
= RootWindowOfScreen (DefaultScreenOfDisplay (XtDisplay (mw
)));
1075 Display
* display
= XtDisplay (mw
);
1078 widget_value
*tem
= (widget_value
*) XtMalloc (sizeof (widget_value
));
1080 /* _XtCreate is freeing the object that was passed to us,
1081 so make a copy that we will actually keep. */
1082 lwlib_bcopy (mw
->menu
.contents
, tem
, sizeof (widget_value
));
1083 mw
->menu
.contents
= tem
;
1086 /* mw->menu.cursor = XCreateFontCursor (display, mw->menu.cursor_shape); */
1087 mw
->menu
.cursor
= mw
->menu
.cursor_shape
;
1089 mw
->menu
.gray_pixmap
= XCreatePixmapFromBitmapData (display
, window
,
1090 gray_bits
, gray_width
,
1091 gray_height
, 1, 0, 1);
1093 /* I don't understand why this ends up 0 sometimes,
1094 but it does. This kludge works around it.
1095 Can anyone find a real fix? -- rms. */
1096 if (mw
->menu
.font
== 0)
1097 mw
->menu
.font
= xlwmenu_default_font
;
1099 make_drawing_gcs (mw
);
1100 make_shadow_gcs (mw
);
1102 xswa
.background_pixel
= mw
->core
.background_pixel
;
1103 xswa
.border_pixel
= mw
->core
.border_pixel
;
1104 mask
= CWBackPixel
| CWBorderPixel
;
1106 mw
->menu
.popped_up
= False
;
1108 mw
->menu
.old_depth
= 1;
1109 mw
->menu
.old_stack
= (widget_value
**)XtMalloc (sizeof (widget_value
*));
1110 mw
->menu
.old_stack_length
= 1;
1111 mw
->menu
.old_stack
[0] = mw
->menu
.contents
;
1113 mw
->menu
.new_depth
= 0;
1114 mw
->menu
.new_stack
= 0;
1115 mw
->menu
.new_stack_length
= 0;
1116 push_new_stack (mw
, mw
->menu
.contents
);
1118 mw
->menu
.windows
= (window_state
*)XtMalloc (sizeof (window_state
));
1119 mw
->menu
.windows_length
= 1;
1120 mw
->menu
.windows
[0].x
= 0;
1121 mw
->menu
.windows
[0].y
= 0;
1122 mw
->menu
.windows
[0].width
= 0;
1123 mw
->menu
.windows
[0].height
= 0;
1126 mw
->core
.width
= mw
->menu
.windows
[0].width
;
1127 mw
->core
.height
= mw
->menu
.windows
[0].height
;
1131 XlwMenuClassInitialize ()
1136 XlwMenuRealize (w
, valueMask
, attributes
)
1139 XSetWindowAttributes
*attributes
;
1141 XlwMenuWidget mw
= (XlwMenuWidget
)w
;
1142 XSetWindowAttributes xswa
;
1145 (*xlwMenuWidgetClass
->core_class
.superclass
->core_class
.realize
)
1146 (w
, valueMask
, attributes
);
1148 xswa
.save_under
= True
;
1149 xswa
.cursor
= mw
->menu
.cursor_shape
;
1150 mask
= CWSaveUnder
| CWCursor
;
1151 XChangeWindowAttributes (XtDisplay (w
), XtWindow (w
), mask
, &xswa
);
1153 mw
->menu
.windows
[0].window
= XtWindow (w
);
1154 mw
->menu
.windows
[0].x
= w
->core
.x
;
1155 mw
->menu
.windows
[0].y
= w
->core
.y
;
1156 mw
->menu
.windows
[0].width
= w
->core
.width
;
1157 mw
->menu
.windows
[0].height
= w
->core
.height
;
1160 /* Only the toplevel menubar/popup is a widget so it's the only one that
1161 receives expose events through Xt. So we repaint all the other panes
1162 when receiving an Expose event. */
1164 XlwMenuRedisplay (w
, ev
, region
)
1169 XlwMenuWidget mw
= (XlwMenuWidget
)w
;
1172 /* If we have a depth beyond 1, it's because a submenu was displayed.
1173 If the submenu has been destroyed, set the depth back to 1. */
1174 if (submenu_destroyed
)
1176 mw
->menu
.old_depth
= 1;
1177 submenu_destroyed
= 0;
1180 for (i
= 0; i
< mw
->menu
.old_depth
; i
++)
1181 display_menu (mw
, i
, False
, NULL
, NULL
, NULL
, NULL
, NULL
);
1189 XlwMenuWidget mw
= (XlwMenuWidget
) w
;
1191 if (pointer_grabbed
)
1192 XtUngrabPointer ((Widget
)w
, CurrentTime
);
1193 pointer_grabbed
= 0;
1195 submenu_destroyed
= 1;
1197 release_drawing_gcs (mw
);
1198 release_shadow_gcs (mw
);
1200 /* this doesn't come from the resource db but is created explicitly
1201 so we must free it ourselves. */
1202 XFreePixmap (XtDisplay (mw
), mw
->menu
.gray_pixmap
);
1203 mw
->menu
.gray_pixmap
= (Pixmap
) -1;
1206 /* Do free mw->menu.contents because nowadays we copy it
1207 during initialization. */
1208 XtFree (mw
->menu
.contents
);
1211 /* Don't free mw->menu.contents because that comes from our creator.
1212 The `*_stack' elements are just pointers into `contents' so leave
1213 that alone too. But free the stacks themselves. */
1214 if (mw
->menu
.old_stack
) XtFree ((char *) mw
->menu
.old_stack
);
1215 if (mw
->menu
.new_stack
) XtFree ((char *) mw
->menu
.new_stack
);
1217 /* Remember, you can't free anything that came from the resource
1218 database. This includes:
1220 mw->menu.top_shadow_pixmap
1221 mw->menu.bottom_shadow_pixmap
1223 Also the color cells of top_shadow_color, bottom_shadow_color,
1224 foreground, and button_foreground will never be freed until this
1225 client exits. Nice, eh?
1228 /* start from 1 because the one in slot 0 is w->core.window */
1229 for (i
= 1; i
< mw
->menu
.windows_length
; i
++)
1230 XDestroyWindow (XtDisplay (mw
), mw
->menu
.windows
[i
].window
);
1231 if (mw
->menu
.windows
)
1232 XtFree ((char *) mw
->menu
.windows
);
1236 XlwMenuSetValues (current
, request
, new)
1241 XlwMenuWidget oldmw
= (XlwMenuWidget
)current
;
1242 XlwMenuWidget newmw
= (XlwMenuWidget
)new;
1243 Boolean redisplay
= False
;
1246 if (newmw
->menu
.contents
1247 && newmw
->menu
.contents
->contents
1248 && newmw
->menu
.contents
->contents
->change
>= VISIBLE_CHANGE
)
1251 if (newmw
->core
.background_pixel
!= oldmw
->core
.background_pixel
1252 || newmw
->menu
.foreground
!= oldmw
->menu
.foreground
1253 || newmw
->menu
.font
!= oldmw
->menu
.font
)
1255 release_drawing_gcs (newmw
);
1256 make_drawing_gcs (newmw
);
1259 for (i
= 0; i
< oldmw
->menu
.windows_length
; i
++)
1261 XSetWindowBackground (XtDisplay (oldmw
),
1262 oldmw
->menu
.windows
[i
].window
,
1263 newmw
->core
.background_pixel
);
1264 /* clear windows and generate expose events */
1265 XClearArea (XtDisplay (oldmw
), oldmw
->menu
.windows
[i
].window
,
1277 XlwMenuWidget mw
= (XlwMenuWidget
)w
;
1279 if (mw
->menu
.popped_up
)
1281 /* Don't allow the popup menu to resize itself. */
1282 mw
->core
.width
= mw
->menu
.windows
[0].width
;
1283 mw
->core
.height
= mw
->menu
.windows
[0].height
;
1284 mw
->core
.parent
->core
.width
= mw
->core
.width
;
1285 mw
->core
.parent
->core
.height
= mw
->core
.height
;
1289 mw
->menu
.windows
[0].width
= mw
->core
.width
;
1290 mw
->menu
.windows
[0].height
= mw
->core
.height
;
1294 \f/* Action procedures */
1296 handle_single_motion_event (mw
, ev
)
1303 if (!map_event_to_widget_value (mw
, ev
, &val
, &level
))
1304 pop_new_stack_if_no_contents (mw
);
1306 set_new_state (mw
, val
, level
);
1309 /* Sync with the display. Makes it feel better on X terms. */
1310 XSync (XtDisplay (mw
), False
);
1314 handle_motion_event (mw
, ev
)
1320 int state
= ev
->state
;
1322 handle_single_motion_event (mw
, ev
);
1324 /* allow motion events to be generated again */
1326 && XQueryPointer (XtDisplay (mw
), ev
->window
,
1327 &ev
->root
, &ev
->subwindow
,
1328 &ev
->x_root
, &ev
->y_root
,
1331 && ev
->state
== state
1332 && (ev
->x_root
!= x
|| ev
->y_root
!= y
))
1333 handle_single_motion_event (mw
, ev
);
1337 Start (w
, ev
, params
, num_params
)
1341 Cardinal
*num_params
;
1343 XlwMenuWidget mw
= (XlwMenuWidget
)w
;
1345 if (!mw
->menu
.popped_up
)
1347 menu_post_event
= *ev
;
1348 pop_up_menu (mw
, ev
);
1352 /* If we push a button while the menu is posted semipermanently,
1353 releasing the button should always pop the menu down. */
1354 next_release_must_exit
= 1;
1356 /* notes the absolute position of the menubar window */
1357 mw
->menu
.windows
[0].x
= ev
->xmotion
.x_root
- ev
->xmotion
.x
;
1358 mw
->menu
.windows
[0].y
= ev
->xmotion
.y_root
- ev
->xmotion
.y
;
1360 /* handles the down like a move, slots are compatible */
1361 handle_motion_event (mw
, &ev
->xmotion
);
1366 Drag (w
, ev
, params
, num_params
)
1370 Cardinal
*num_params
;
1372 XlwMenuWidget mw
= (XlwMenuWidget
)w
;
1373 handle_motion_event (mw
, &ev
->xmotion
);
1377 Select (w
, ev
, params
, num_params
)
1381 Cardinal
*num_params
;
1383 XlwMenuWidget mw
= (XlwMenuWidget
)w
;
1384 widget_value
* selected_item
= mw
->menu
.old_stack
[mw
->menu
.old_depth
- 1];
1386 /* If user releases the button quickly, without selecting anything,
1387 after the initial down-click that brought the menu up,
1389 if ((selected_item
== 0
1390 || ((widget_value
*) selected_item
)->call_data
== 0)
1391 && !next_release_must_exit
1392 && (ev
->xbutton
.time
- menu_post_event
.xbutton
.time
1393 < XtGetMultiClickTime (XtDisplay (w
))))
1396 /* pop down everything. */
1397 mw
->menu
.new_depth
= 1;
1400 if (mw
->menu
.popped_up
)
1402 mw
->menu
.popped_up
= False
;
1403 XtUngrabPointer ((Widget
)mw
, ev
->xmotion
.time
);
1404 if (XtIsShell (XtParent ((Widget
) mw
)))
1405 XtPopdown (XtParent ((Widget
) mw
));
1408 XtRemoveGrab ((Widget
) mw
);
1409 display_menu (mw
, 0, False
, NULL
, NULL
, NULL
, NULL
, NULL
);
1414 XtCallCallbackList ((Widget
)mw
, mw
->menu
.select
, (XtPointer
)selected_item
);
1418 \f/* Special code to pop-up a menu */
1420 pop_up_menu (mw
, event
)
1422 XButtonPressedEvent
* event
;
1424 int x
= event
->x_root
;
1425 int y
= event
->y_root
;
1428 int borderwidth
= mw
->menu
.shadow_thickness
;
1429 Screen
* screen
= XtScreen (mw
);
1430 Display
*display
= XtDisplay (mw
);
1432 next_release_must_exit
= 0;
1434 XtCallCallbackList ((Widget
)mw
, mw
->menu
.open
, NULL
);
1436 if (XtIsShell (XtParent ((Widget
)mw
)))
1439 w
= mw
->menu
.windows
[0].width
;
1440 h
= mw
->menu
.windows
[0].height
;
1444 if (x
< borderwidth
)
1446 if (x
+ w
+ 2 * borderwidth
> WidthOfScreen (screen
))
1447 x
= WidthOfScreen (screen
) - w
- 2 * borderwidth
;
1448 if (y
< borderwidth
)
1450 if (y
+ h
+ 2 * borderwidth
> HeightOfScreen (screen
))
1451 y
= HeightOfScreen (screen
) - h
- 2 * borderwidth
;
1453 mw
->menu
.popped_up
= True
;
1454 if (XtIsShell (XtParent ((Widget
)mw
)))
1456 XtConfigureWidget (XtParent ((Widget
)mw
), x
, y
, w
, h
,
1457 XtParent ((Widget
)mw
)->core
.border_width
);
1458 XtPopup (XtParent ((Widget
)mw
), XtGrabExclusive
);
1459 display_menu (mw
, 0, False
, NULL
, NULL
, NULL
, NULL
, NULL
);
1460 mw
->menu
.windows
[0].x
= x
+ borderwidth
;
1461 mw
->menu
.windows
[0].y
= y
+ borderwidth
;
1465 XEvent
*ev
= (XEvent
*) event
;
1467 XtAddGrab ((Widget
) mw
, True
, True
);
1469 /* notes the absolute position of the menubar window */
1470 mw
->menu
.windows
[0].x
= ev
->xmotion
.x_root
- ev
->xmotion
.x
;
1471 mw
->menu
.windows
[0].y
= ev
->xmotion
.y_root
- ev
->xmotion
.y
;
1475 x_catch_errors (display
);
1477 XtGrabPointer ((Widget
)mw
, False
,
1479 | PointerMotionHintMask
1482 GrabModeAsync
, GrabModeAsync
, None
,
1483 mw
->menu
.cursor_shape
,
1485 pointer_grabbed
= 1;
1487 if (x_had_errors_p (display
))
1489 pointer_grabbed
= 0;
1490 XtUngrabPointer ((Widget
)mw
, event
->time
);
1492 x_uncatch_errors (display
);
1495 handle_motion_event (mw
, (XMotionEvent
*)event
);