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
)
329 unsigned int label_width
= 0;
331 int max_rest_width
= 0;
332 unsigned int height
= 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
;
490 /* see if it should be a button in the menubar */
491 button_p
= horizontal_p
&& val
->call_data
;
495 /* Only highlight an enabled item that has a callback. */
497 if (!val
->enabled
|| !(val
->call_data
|| val
->contents
))
500 /* do the drawing. */
503 /* Add the shadow border of the containing menu */
504 int x
= where
->x
+ shadow
;
505 int y
= where
->y
+ shadow
;
507 /* pick the foreground and background GC. */
509 text_gc
= button_p
? mw
->menu
.button_gc
: mw
->menu
.foreground_gc
;
512 button_p
? mw
->menu
.inactive_button_gc
: mw
->menu
.inactive_gc
;
513 deco_gc
= mw
->menu
.foreground_gc
;
517 XDrawLine (XtDisplay (mw
), ws
->window
, mw
->menu
.shadow_bottom_gc
,
519 XDrawLine (XtDisplay (mw
), ws
->window
, mw
->menu
.shadow_top_gc
,
520 x
, y
+ 1, x
+ width
, y
+ 1);
524 int x_offset
= x
+ h_spacing
+ shadow
;
525 char* display_string
= resource_widget_value (mw
, val
);
526 draw_shadow_rectangle (mw
, ws
->window
, x
, y
, width
, height
, True
);
528 /* Deal with centering a menu title. */
529 if (!horizontal_p
&& !val
->contents
&& !val
->call_data
)
531 int l
= string_width (mw
, display_string
);
534 x_offset
= (width
- l
) >> 1;
536 XDrawString (XtDisplay (mw
), ws
->window
, text_gc
, x_offset
,
537 y
+ v_spacing
+ shadow
+ font_ascent
,
538 display_string
, strlen (display_string
));
544 int a_w
= arrow_width (mw
);
545 draw_arrow (mw
, ws
->window
, deco_gc
,
546 x
+ width
- arrow_width (mw
)
547 - mw
->menu
.horizontal_spacing
548 - mw
->menu
.shadow_thickness
,
549 y
+ v_spacing
+ shadow
, a_w
);
553 XDrawString (XtDisplay (mw
), ws
->window
, text_gc
,
554 x
+ label_width
+ mw
->menu
.arrow_spacing
,
555 y
+ v_spacing
+ shadow
+ font_ascent
,
556 val
->key
, strlen (val
->key
));
563 XDrawRectangle (XtDisplay (mw
), ws
->window
, deco_gc
,
564 x
+ shadow
, y
+ shadow
,
565 label_width
+ h_spacing
- 1,
566 font_ascent
+ font_descent
+ 2 * v_spacing
- 1);
567 draw_shadow_rectangle (mw
, ws
->window
, x
, y
, width
, height
,
570 highlighted_p
= True
;
575 XDrawRectangle (XtDisplay (mw
), ws
->window
,
576 mw
->menu
.background_gc
,
577 x
+ shadow
, y
+ shadow
,
578 label_width
+ h_spacing
- 1,
579 font_ascent
+ font_descent
+ 2 * v_spacing
- 1);
580 draw_shadow_rectangle (mw
, ws
->window
, x
, y
, width
, height
,
585 draw_shadow_rectangle (mw
, ws
->window
, x
, y
, width
, height
, False
);
594 display_menu (mw
, level
, just_compute_p
, highlighted_pos
, hit
, hit_return
,
598 Boolean just_compute_p
;
599 XPoint
* highlighted_pos
;
601 widget_value
** hit_return
;
606 widget_value
* following_item
;
609 int horizontal_p
= mw
->menu
.horizontal
&& (level
== 0);
611 int just_compute_this_one_p
;
613 if (level
>= mw
->menu
.old_depth
)
616 if (level
< mw
->menu
.old_depth
- 1)
617 following_item
= mw
->menu
.old_stack
[level
+ 1];
619 following_item
= NULL
;
627 ws
= &mw
->menu
.windows
[level
];
628 for (val
= mw
->menu
.old_stack
[level
]->contents
; val
; val
= val
->next
)
630 highlighted_p
= val
== following_item
;
631 if (highlighted_p
&& highlighted_pos
)
634 highlighted_pos
->x
= where
.x
;
636 highlighted_pos
->y
= where
.y
;
639 just_compute_this_one_p
=
640 just_compute_p
|| ((this || that
) && val
!= this && val
!= that
);
642 display_menu_item (mw
, val
, ws
, &where
, highlighted_p
, horizontal_p
,
643 just_compute_this_one_p
);
645 if (highlighted_p
&& highlighted_pos
)
648 highlighted_pos
->y
= where
.y
;
650 highlighted_pos
->x
= where
.x
;
655 && (horizontal_p
? hit
->x
< where
.x
: hit
->y
< where
.y
)
656 && !all_dashes_p (val
->name
))
666 draw_shadow_rectangle (mw
, ws
->window
, 0, 0, ws
->width
, ws
->height
, False
);
671 set_new_state (mw
, val
, level
)
678 mw
->menu
.new_depth
= 0;
679 for (i
= 0; i
< level
; i
++)
680 push_new_stack (mw
, mw
->menu
.old_stack
[i
]);
681 push_new_stack (mw
, val
);
685 make_windows_if_needed (mw
, n
)
691 XSetWindowAttributes xswa
;
693 Window root
= RootWindowOfScreen (DefaultScreenOfDisplay (XtDisplay (mw
)));
694 window_state
* windows
;
696 if (mw
->menu
.windows_length
>= n
)
699 xswa
.save_under
= True
;
700 xswa
.override_redirect
= True
;
701 xswa
.background_pixel
= mw
->core
.background_pixel
;
702 xswa
.border_pixel
= mw
->core
.border_pixel
;
704 ExposureMask
| PointerMotionMask
| PointerMotionHintMask
705 | ButtonReleaseMask
| ButtonPressMask
;
706 xswa
.cursor
= mw
->menu
.cursor_shape
;
707 mask
= CWSaveUnder
| CWOverrideRedirect
| CWBackPixel
| CWBorderPixel
708 | CWEventMask
| CWCursor
;
710 if (!mw
->menu
.windows
)
713 (window_state
*)XtMalloc (n
* sizeof (window_state
));
719 (window_state
*)XtRealloc ((char*)mw
->menu
.windows
,
720 n
* sizeof (window_state
));
721 start_at
= mw
->menu
.windows_length
;
723 mw
->menu
.windows_length
= n
;
725 windows
= mw
->menu
.windows
;
727 for (i
= start_at
; i
< n
; i
++)
731 windows
[i
].width
= 1;
732 windows
[i
].height
= 1;
734 XCreateWindow (XtDisplay (mw
), root
, 0, 0, 1, 1,
735 0, 0, CopyFromParent
, CopyFromParent
, mask
, &xswa
);
739 /* Make the window fit in the screen */
741 fit_to_screen (mw
, ws
, previous_ws
, horizontal_p
)
744 window_state
* previous_ws
;
745 Boolean horizontal_p
;
747 unsigned int screen_width
= WidthOfScreen (XtScreen (mw
));
748 unsigned int screen_height
= HeightOfScreen (XtScreen (mw
));
752 else if (ws
->x
+ ws
->width
> screen_width
)
755 ws
->x
= previous_ws
->x
- ws
->width
;
757 ws
->x
= screen_width
- ws
->width
;
763 else if (ws
->y
+ ws
->height
> screen_height
)
766 ws
->y
= previous_ws
->y
- ws
->height
;
768 ws
->y
= screen_height
- ws
->height
;
774 /* Updates old_stack from new_stack and redisplays. */
781 XPoint selection_position
;
782 int old_depth
= mw
->menu
.old_depth
;
783 int new_depth
= mw
->menu
.new_depth
;
784 widget_value
** old_stack
;
785 widget_value
** new_stack
;
786 window_state
* windows
;
787 widget_value
* old_selection
;
788 widget_value
* new_selection
;
790 /* Check that enough windows and old_stack are ready. */
791 make_windows_if_needed (mw
, new_depth
);
792 make_old_stack_space (mw
, new_depth
);
793 windows
= mw
->menu
.windows
;
794 old_stack
= mw
->menu
.old_stack
;
795 new_stack
= mw
->menu
.new_stack
;
797 /* compute the last identical different entry */
798 for (i
= 1; i
< old_depth
&& i
< new_depth
; i
++)
799 if (old_stack
[i
] != new_stack
[i
])
803 /* Memorize the previously selected item to be able to refresh it */
804 old_selection
= last_same
+ 1 < old_depth
? old_stack
[last_same
+ 1] : NULL
;
805 if (old_selection
&& !old_selection
->enabled
)
806 old_selection
= NULL
;
807 new_selection
= last_same
+ 1 < new_depth
? new_stack
[last_same
+ 1] : NULL
;
808 if (new_selection
&& !new_selection
->enabled
)
809 new_selection
= NULL
;
811 /* updates old_state from new_state. It has to be done now because
812 display_menu (called below) uses the old_stack to know what to display. */
813 for (i
= last_same
+ 1; i
< new_depth
; i
++)
814 old_stack
[i
] = new_stack
[i
];
815 mw
->menu
.old_depth
= new_depth
;
817 /* refresh the last seletion */
818 selection_position
.x
= 0;
819 selection_position
.y
= 0;
820 display_menu (mw
, last_same
, new_selection
== old_selection
,
821 &selection_position
, NULL
, NULL
, old_selection
, new_selection
);
823 /* Now popup the new menus */
824 for (i
= last_same
+ 1; i
< new_depth
&& new_stack
[i
]->contents
; i
++)
826 window_state
* previous_ws
= &windows
[i
- 1];
827 window_state
* ws
= &windows
[i
];
830 previous_ws
->x
+ selection_position
.x
+ mw
->menu
.shadow_thickness
;
831 if (!mw
->menu
.horizontal
|| i
> 1)
832 ws
->x
+= mw
->menu
.shadow_thickness
;
834 previous_ws
->y
+ selection_position
.y
+ mw
->menu
.shadow_thickness
;
838 fit_to_screen (mw
, ws
, previous_ws
, mw
->menu
.horizontal
&& i
== 1);
840 XClearWindow (XtDisplay (mw
), ws
->window
);
841 XMoveResizeWindow (XtDisplay (mw
), ws
->window
, ws
->x
, ws
->y
,
842 ws
->width
, ws
->height
);
843 XMapRaised (XtDisplay (mw
), ws
->window
);
844 display_menu (mw
, i
, False
, &selection_position
, NULL
, NULL
, NULL
, NULL
);
847 /* unmap the menus that popped down */
848 for (i
= new_depth
- 1; i
< old_depth
; i
++)
849 if (i
>= new_depth
|| !new_stack
[i
]->contents
)
850 XUnmapWindow (XtDisplay (mw
), windows
[i
].window
);
854 motion_event_is_in_menu (mw
, ev
, level
, relative_pos
)
858 XPoint
* relative_pos
;
860 window_state
* ws
= &mw
->menu
.windows
[level
];
861 unsigned int x
= level
== 0 ? ws
->x
: ws
->x
+ mw
->menu
.shadow_thickness
;
862 unsigned int y
= level
== 0 ? ws
->y
: ws
->y
+ mw
->menu
.shadow_thickness
;
863 relative_pos
->x
= ev
->x_root
- x
;
864 relative_pos
->y
= ev
->y_root
- y
;
865 return (x
< ev
->x_root
&& ev
->x_root
< x
+ ws
->width
866 && y
< ev
->y_root
&& ev
->y_root
< y
+ ws
->height
);
870 map_event_to_widget_value (mw
, ev
, val
, level
)
882 /* Find the window */
883 for (i
= mw
->menu
.old_depth
- 1; i
>= 0; i
--)
885 ws
= &mw
->menu
.windows
[i
];
886 if (ws
&& motion_event_is_in_menu (mw
, ev
, i
, &relative_pos
))
888 display_menu (mw
, i
, True
, NULL
, &relative_pos
, val
, NULL
, NULL
);
902 make_drawing_gcs (mw
)
907 xgcv
.font
= mw
->menu
.font
->fid
;
908 xgcv
.foreground
= mw
->menu
.foreground
;
909 xgcv
.background
= mw
->core
.background_pixel
;
910 mw
->menu
.foreground_gc
= XtGetGC ((Widget
)mw
,
911 GCFont
| GCForeground
| GCBackground
,
914 xgcv
.font
= mw
->menu
.font
->fid
;
915 xgcv
.foreground
= mw
->menu
.button_foreground
;
916 xgcv
.background
= mw
->core
.background_pixel
;
917 mw
->menu
.button_gc
= XtGetGC ((Widget
)mw
,
918 GCFont
| GCForeground
| GCBackground
,
921 xgcv
.font
= mw
->menu
.font
->fid
;
922 xgcv
.foreground
= mw
->menu
.foreground
;
923 xgcv
.background
= mw
->core
.background_pixel
;
924 xgcv
.fill_style
= FillStippled
;
925 xgcv
.stipple
= mw
->menu
.gray_pixmap
;
926 mw
->menu
.inactive_gc
= XtGetGC ((Widget
)mw
,
927 (GCFont
| GCForeground
| GCBackground
928 | GCFillStyle
| GCStipple
), &xgcv
);
930 xgcv
.font
= mw
->menu
.font
->fid
;
931 xgcv
.foreground
= mw
->menu
.button_foreground
;
932 xgcv
.background
= mw
->core
.background_pixel
;
933 xgcv
.fill_style
= FillStippled
;
934 xgcv
.stipple
= mw
->menu
.gray_pixmap
;
935 mw
->menu
.inactive_button_gc
= XtGetGC ((Widget
)mw
,
936 (GCFont
| GCForeground
| GCBackground
937 | GCFillStyle
| GCStipple
), &xgcv
);
939 xgcv
.font
= mw
->menu
.font
->fid
;
940 xgcv
.foreground
= mw
->core
.background_pixel
;
941 xgcv
.background
= mw
->menu
.foreground
;
942 mw
->menu
.background_gc
= XtGetGC ((Widget
)mw
,
943 GCFont
| GCForeground
| GCBackground
,
948 release_drawing_gcs (mw
)
951 XtReleaseGC ((Widget
) mw
, mw
->menu
.foreground_gc
);
952 XtReleaseGC ((Widget
) mw
, mw
->menu
.button_gc
);
953 XtReleaseGC ((Widget
) mw
, mw
->menu
.inactive_gc
);
954 XtReleaseGC ((Widget
) mw
, mw
->menu
.inactive_button_gc
);
955 XtReleaseGC ((Widget
) mw
, mw
->menu
.background_gc
);
956 /* let's get some segvs if we try to use these... */
957 mw
->menu
.foreground_gc
= (GC
) -1;
958 mw
->menu
.button_gc
= (GC
) -1;
959 mw
->menu
.inactive_gc
= (GC
) -1;
960 mw
->menu
.inactive_button_gc
= (GC
) -1;
961 mw
->menu
.background_gc
= (GC
) -1;
964 #define MINL(x,y) ((((unsigned long) (x)) < ((unsigned long) (y))) \
965 ? ((unsigned long) (x)) : ((unsigned long) (y)))
972 unsigned long pm
= 0;
973 Display
*dpy
= XtDisplay ((Widget
) mw
);
974 Colormap cmap
= DefaultColormapOfScreen (XtScreen ((Widget
) mw
));
976 int top_frobbed
= 0, bottom_frobbed
= 0;
978 if (mw
->menu
.top_shadow_color
== -1)
979 mw
->menu
.top_shadow_color
= mw
->core
.background_pixel
;
980 if (mw
->menu
.bottom_shadow_color
== -1)
981 mw
->menu
.bottom_shadow_color
= mw
->menu
.foreground
;
983 if (mw
->menu
.top_shadow_color
== mw
->core
.background_pixel
||
984 mw
->menu
.top_shadow_color
== mw
->menu
.foreground
)
986 topc
.pixel
= mw
->core
.background_pixel
;
987 XQueryColor (dpy
, cmap
, &topc
);
988 /* don't overflow/wrap! */
989 topc
.red
= MINL (65535, topc
.red
* 1.2);
990 topc
.green
= MINL (65535, topc
.green
* 1.2);
991 topc
.blue
= MINL (65535, topc
.blue
* 1.2);
992 if (XAllocColor (dpy
, cmap
, &topc
))
994 mw
->menu
.top_shadow_color
= topc
.pixel
;
998 if (mw
->menu
.bottom_shadow_color
== mw
->menu
.foreground
||
999 mw
->menu
.bottom_shadow_color
== mw
->core
.background_pixel
)
1001 botc
.pixel
= mw
->core
.background_pixel
;
1002 XQueryColor (dpy
, cmap
, &botc
);
1006 if (XAllocColor (dpy
, cmap
, &botc
))
1008 mw
->menu
.bottom_shadow_color
= botc
.pixel
;
1013 if (top_frobbed
&& bottom_frobbed
)
1015 int top_avg
= ((topc
.red
/ 3) + (topc
.green
/ 3) + (topc
.blue
/ 3));
1016 int bot_avg
= ((botc
.red
/ 3) + (botc
.green
/ 3) + (botc
.blue
/ 3));
1017 if (bot_avg
> top_avg
)
1019 Pixel tmp
= mw
->menu
.top_shadow_color
;
1020 mw
->menu
.top_shadow_color
= mw
->menu
.bottom_shadow_color
;
1021 mw
->menu
.bottom_shadow_color
= tmp
;
1023 else if (topc
.pixel
== botc
.pixel
)
1025 if (botc
.pixel
== mw
->menu
.foreground
)
1026 mw
->menu
.top_shadow_color
= mw
->core
.background_pixel
;
1028 mw
->menu
.bottom_shadow_color
= mw
->menu
.foreground
;
1032 if (!mw
->menu
.top_shadow_pixmap
&&
1033 mw
->menu
.top_shadow_color
== mw
->core
.background_pixel
)
1035 mw
->menu
.top_shadow_pixmap
= mw
->menu
.gray_pixmap
;
1036 mw
->menu
.top_shadow_color
= mw
->menu
.foreground
;
1038 if (!mw
->menu
.bottom_shadow_pixmap
&&
1039 mw
->menu
.bottom_shadow_color
== mw
->core
.background_pixel
)
1041 mw
->menu
.bottom_shadow_pixmap
= mw
->menu
.gray_pixmap
;
1042 mw
->menu
.bottom_shadow_color
= mw
->menu
.foreground
;
1045 xgcv
.fill_style
= FillStippled
;
1046 xgcv
.foreground
= mw
->menu
.top_shadow_color
;
1047 xgcv
.stipple
= mw
->menu
.top_shadow_pixmap
;
1048 pm
= (xgcv
.stipple
? GCStipple
|GCFillStyle
: 0);
1049 mw
->menu
.shadow_top_gc
= XtGetGC ((Widget
)mw
, GCForeground
| pm
, &xgcv
);
1051 xgcv
.foreground
= mw
->menu
.bottom_shadow_color
;
1052 xgcv
.stipple
= mw
->menu
.bottom_shadow_pixmap
;
1053 pm
= (xgcv
.stipple
? GCStipple
|GCFillStyle
: 0);
1054 mw
->menu
.shadow_bottom_gc
= XtGetGC ((Widget
)mw
, GCForeground
| pm
, &xgcv
);
1059 release_shadow_gcs (mw
)
1062 XtReleaseGC ((Widget
) mw
, mw
->menu
.shadow_top_gc
);
1063 XtReleaseGC ((Widget
) mw
, mw
->menu
.shadow_bottom_gc
);
1067 XlwMenuInitialize (request
, mw
, args
, num_args
)
1073 /* Get the GCs and the widget size */
1074 XSetWindowAttributes xswa
;
1077 Window window
= RootWindowOfScreen (DefaultScreenOfDisplay (XtDisplay (mw
)));
1078 Display
* display
= XtDisplay (mw
);
1081 widget_value
*tem
= (widget_value
*) XtMalloc (sizeof (widget_value
));
1083 /* _XtCreate is freeing the object that was passed to us,
1084 so make a copy that we will actually keep. */
1085 lwlib_bcopy (mw
->menu
.contents
, tem
, sizeof (widget_value
));
1086 mw
->menu
.contents
= tem
;
1089 /* mw->menu.cursor = XCreateFontCursor (display, mw->menu.cursor_shape); */
1090 mw
->menu
.cursor
= mw
->menu
.cursor_shape
;
1092 mw
->menu
.gray_pixmap
1093 = XCreatePixmapFromBitmapData (display
, window
, gray_bits
,
1094 gray_width
, gray_height
,
1095 (unsigned long)1, (unsigned long)0, 1);
1097 /* I don't understand why this ends up 0 sometimes,
1098 but it does. This kludge works around it.
1099 Can anyone find a real fix? -- rms. */
1100 if (mw
->menu
.font
== 0)
1101 mw
->menu
.font
= xlwmenu_default_font
;
1103 make_drawing_gcs (mw
);
1104 make_shadow_gcs (mw
);
1106 xswa
.background_pixel
= mw
->core
.background_pixel
;
1107 xswa
.border_pixel
= mw
->core
.border_pixel
;
1108 mask
= CWBackPixel
| CWBorderPixel
;
1110 mw
->menu
.popped_up
= False
;
1112 mw
->menu
.old_depth
= 1;
1113 mw
->menu
.old_stack
= (widget_value
**)XtMalloc (sizeof (widget_value
*));
1114 mw
->menu
.old_stack_length
= 1;
1115 mw
->menu
.old_stack
[0] = mw
->menu
.contents
;
1117 mw
->menu
.new_depth
= 0;
1118 mw
->menu
.new_stack
= 0;
1119 mw
->menu
.new_stack_length
= 0;
1120 push_new_stack (mw
, mw
->menu
.contents
);
1122 mw
->menu
.windows
= (window_state
*)XtMalloc (sizeof (window_state
));
1123 mw
->menu
.windows_length
= 1;
1124 mw
->menu
.windows
[0].x
= 0;
1125 mw
->menu
.windows
[0].y
= 0;
1126 mw
->menu
.windows
[0].width
= 0;
1127 mw
->menu
.windows
[0].height
= 0;
1130 mw
->core
.width
= mw
->menu
.windows
[0].width
;
1131 mw
->core
.height
= mw
->menu
.windows
[0].height
;
1135 XlwMenuClassInitialize ()
1140 XlwMenuRealize (w
, valueMask
, attributes
)
1143 XSetWindowAttributes
*attributes
;
1145 XlwMenuWidget mw
= (XlwMenuWidget
)w
;
1146 XSetWindowAttributes xswa
;
1149 (*xlwMenuWidgetClass
->core_class
.superclass
->core_class
.realize
)
1150 (w
, valueMask
, attributes
);
1152 xswa
.save_under
= True
;
1153 xswa
.cursor
= mw
->menu
.cursor_shape
;
1154 mask
= CWSaveUnder
| CWCursor
;
1155 XChangeWindowAttributes (XtDisplay (w
), XtWindow (w
), mask
, &xswa
);
1157 mw
->menu
.windows
[0].window
= XtWindow (w
);
1158 mw
->menu
.windows
[0].x
= w
->core
.x
;
1159 mw
->menu
.windows
[0].y
= w
->core
.y
;
1160 mw
->menu
.windows
[0].width
= w
->core
.width
;
1161 mw
->menu
.windows
[0].height
= w
->core
.height
;
1164 /* Only the toplevel menubar/popup is a widget so it's the only one that
1165 receives expose events through Xt. So we repaint all the other panes
1166 when receiving an Expose event. */
1168 XlwMenuRedisplay (w
, ev
, region
)
1173 XlwMenuWidget mw
= (XlwMenuWidget
)w
;
1176 /* If we have a depth beyond 1, it's because a submenu was displayed.
1177 If the submenu has been destroyed, set the depth back to 1. */
1178 if (submenu_destroyed
)
1180 mw
->menu
.old_depth
= 1;
1181 submenu_destroyed
= 0;
1184 for (i
= 0; i
< mw
->menu
.old_depth
; i
++)
1185 display_menu (mw
, i
, False
, NULL
, NULL
, NULL
, NULL
, NULL
);
1193 XlwMenuWidget mw
= (XlwMenuWidget
) w
;
1195 if (pointer_grabbed
)
1196 XtUngrabPointer ((Widget
)w
, CurrentTime
);
1197 pointer_grabbed
= 0;
1199 submenu_destroyed
= 1;
1201 release_drawing_gcs (mw
);
1202 release_shadow_gcs (mw
);
1204 /* this doesn't come from the resource db but is created explicitly
1205 so we must free it ourselves. */
1206 XFreePixmap (XtDisplay (mw
), mw
->menu
.gray_pixmap
);
1207 mw
->menu
.gray_pixmap
= (Pixmap
) -1;
1210 /* Do free mw->menu.contents because nowadays we copy it
1211 during initialization. */
1212 XtFree (mw
->menu
.contents
);
1215 /* Don't free mw->menu.contents because that comes from our creator.
1216 The `*_stack' elements are just pointers into `contents' so leave
1217 that alone too. But free the stacks themselves. */
1218 if (mw
->menu
.old_stack
) XtFree ((char *) mw
->menu
.old_stack
);
1219 if (mw
->menu
.new_stack
) XtFree ((char *) mw
->menu
.new_stack
);
1221 /* Remember, you can't free anything that came from the resource
1222 database. This includes:
1224 mw->menu.top_shadow_pixmap
1225 mw->menu.bottom_shadow_pixmap
1227 Also the color cells of top_shadow_color, bottom_shadow_color,
1228 foreground, and button_foreground will never be freed until this
1229 client exits. Nice, eh?
1232 /* start from 1 because the one in slot 0 is w->core.window */
1233 for (i
= 1; i
< mw
->menu
.windows_length
; i
++)
1234 XDestroyWindow (XtDisplay (mw
), mw
->menu
.windows
[i
].window
);
1235 if (mw
->menu
.windows
)
1236 XtFree ((char *) mw
->menu
.windows
);
1240 XlwMenuSetValues (current
, request
, new)
1245 XlwMenuWidget oldmw
= (XlwMenuWidget
)current
;
1246 XlwMenuWidget newmw
= (XlwMenuWidget
)new;
1247 Boolean redisplay
= False
;
1250 if (newmw
->menu
.contents
1251 && newmw
->menu
.contents
->contents
1252 && newmw
->menu
.contents
->contents
->change
>= VISIBLE_CHANGE
)
1255 if (newmw
->core
.background_pixel
!= oldmw
->core
.background_pixel
1256 || newmw
->menu
.foreground
!= oldmw
->menu
.foreground
1257 || newmw
->menu
.font
!= oldmw
->menu
.font
)
1259 release_drawing_gcs (newmw
);
1260 make_drawing_gcs (newmw
);
1263 for (i
= 0; i
< oldmw
->menu
.windows_length
; i
++)
1265 XSetWindowBackground (XtDisplay (oldmw
),
1266 oldmw
->menu
.windows
[i
].window
,
1267 newmw
->core
.background_pixel
);
1268 /* clear windows and generate expose events */
1269 XClearArea (XtDisplay (oldmw
), oldmw
->menu
.windows
[i
].window
,
1281 XlwMenuWidget mw
= (XlwMenuWidget
)w
;
1283 if (mw
->menu
.popped_up
)
1285 /* Don't allow the popup menu to resize itself. */
1286 mw
->core
.width
= mw
->menu
.windows
[0].width
;
1287 mw
->core
.height
= mw
->menu
.windows
[0].height
;
1288 mw
->core
.parent
->core
.width
= mw
->core
.width
;
1289 mw
->core
.parent
->core
.height
= mw
->core
.height
;
1293 mw
->menu
.windows
[0].width
= mw
->core
.width
;
1294 mw
->menu
.windows
[0].height
= mw
->core
.height
;
1298 \f/* Action procedures */
1300 handle_single_motion_event (mw
, ev
)
1307 if (!map_event_to_widget_value (mw
, ev
, &val
, &level
))
1308 pop_new_stack_if_no_contents (mw
);
1310 set_new_state (mw
, val
, level
);
1313 /* Sync with the display. Makes it feel better on X terms. */
1314 XSync (XtDisplay (mw
), False
);
1318 handle_motion_event (mw
, ev
)
1324 int state
= ev
->state
;
1326 handle_single_motion_event (mw
, ev
);
1328 /* allow motion events to be generated again */
1330 && XQueryPointer (XtDisplay (mw
), ev
->window
,
1331 &ev
->root
, &ev
->subwindow
,
1332 &ev
->x_root
, &ev
->y_root
,
1335 && ev
->state
== state
1336 && (ev
->x_root
!= x
|| ev
->y_root
!= y
))
1337 handle_single_motion_event (mw
, ev
);
1341 Start (w
, ev
, params
, num_params
)
1345 Cardinal
*num_params
;
1347 XlwMenuWidget mw
= (XlwMenuWidget
)w
;
1349 if (!mw
->menu
.popped_up
)
1351 menu_post_event
= *ev
;
1352 pop_up_menu (mw
, ev
);
1356 /* If we push a button while the menu is posted semipermanently,
1357 releasing the button should always pop the menu down. */
1358 next_release_must_exit
= 1;
1360 /* notes the absolute position of the menubar window */
1361 mw
->menu
.windows
[0].x
= ev
->xmotion
.x_root
- ev
->xmotion
.x
;
1362 mw
->menu
.windows
[0].y
= ev
->xmotion
.y_root
- ev
->xmotion
.y
;
1364 /* handles the down like a move, slots are compatible */
1365 handle_motion_event (mw
, &ev
->xmotion
);
1370 Drag (w
, ev
, params
, num_params
)
1374 Cardinal
*num_params
;
1376 XlwMenuWidget mw
= (XlwMenuWidget
)w
;
1377 if (mw
->menu
.popped_up
)
1378 handle_motion_event (mw
, &ev
->xmotion
);
1382 Select (w
, ev
, params
, num_params
)
1386 Cardinal
*num_params
;
1388 XlwMenuWidget mw
= (XlwMenuWidget
)w
;
1389 widget_value
* selected_item
= mw
->menu
.old_stack
[mw
->menu
.old_depth
- 1];
1391 /* If user releases the button quickly, without selecting anything,
1392 after the initial down-click that brought the menu up,
1394 if ((selected_item
== 0
1395 || ((widget_value
*) selected_item
)->call_data
== 0)
1396 && !next_release_must_exit
1397 && (ev
->xbutton
.time
- menu_post_event
.xbutton
.time
1398 < XtGetMultiClickTime (XtDisplay (w
))))
1401 /* pop down everything. */
1402 mw
->menu
.new_depth
= 1;
1405 if (mw
->menu
.popped_up
)
1407 mw
->menu
.popped_up
= False
;
1408 XtUngrabPointer ((Widget
)mw
, ev
->xmotion
.time
);
1409 if (XtIsShell (XtParent ((Widget
) mw
)))
1410 XtPopdown (XtParent ((Widget
) mw
));
1413 XtRemoveGrab ((Widget
) mw
);
1414 display_menu (mw
, 0, False
, NULL
, NULL
, NULL
, NULL
, NULL
);
1419 XtCallCallbackList ((Widget
)mw
, mw
->menu
.select
, (XtPointer
)selected_item
);
1423 \f/* Special code to pop-up a menu */
1425 pop_up_menu (mw
, event
)
1427 XButtonPressedEvent
* event
;
1429 int x
= event
->x_root
;
1430 int y
= event
->y_root
;
1433 int borderwidth
= mw
->menu
.shadow_thickness
;
1434 Screen
* screen
= XtScreen (mw
);
1435 Display
*display
= XtDisplay (mw
);
1437 next_release_must_exit
= 0;
1439 XtCallCallbackList ((Widget
)mw
, mw
->menu
.open
, NULL
);
1441 if (XtIsShell (XtParent ((Widget
)mw
)))
1444 w
= mw
->menu
.windows
[0].width
;
1445 h
= mw
->menu
.windows
[0].height
;
1449 if (x
< borderwidth
)
1451 if (x
+ w
+ 2 * borderwidth
> WidthOfScreen (screen
))
1452 x
= WidthOfScreen (screen
) - w
- 2 * borderwidth
;
1453 if (y
< borderwidth
)
1455 if (y
+ h
+ 2 * borderwidth
> HeightOfScreen (screen
))
1456 y
= HeightOfScreen (screen
) - h
- 2 * borderwidth
;
1458 mw
->menu
.popped_up
= True
;
1459 if (XtIsShell (XtParent ((Widget
)mw
)))
1461 XtConfigureWidget (XtParent ((Widget
)mw
), x
, y
, w
, h
,
1462 XtParent ((Widget
)mw
)->core
.border_width
);
1463 XtPopup (XtParent ((Widget
)mw
), XtGrabExclusive
);
1464 display_menu (mw
, 0, False
, NULL
, NULL
, NULL
, NULL
, NULL
);
1465 mw
->menu
.windows
[0].x
= x
+ borderwidth
;
1466 mw
->menu
.windows
[0].y
= y
+ borderwidth
;
1470 XEvent
*ev
= (XEvent
*) event
;
1472 XtAddGrab ((Widget
) mw
, True
, True
);
1474 /* notes the absolute position of the menubar window */
1475 mw
->menu
.windows
[0].x
= ev
->xmotion
.x_root
- ev
->xmotion
.x
;
1476 mw
->menu
.windows
[0].y
= ev
->xmotion
.y_root
- ev
->xmotion
.y
;
1480 x_catch_errors (display
);
1482 XtGrabPointer ((Widget
)mw
, False
,
1484 | PointerMotionHintMask
1487 GrabModeAsync
, GrabModeAsync
, None
,
1488 mw
->menu
.cursor_shape
,
1490 pointer_grabbed
= 1;
1492 if (x_had_errors_p (display
))
1494 pointer_grabbed
= 0;
1495 XtUngrabPointer ((Widget
)mw
, event
->time
);
1497 x_uncatch_errors (display
);
1500 handle_motion_event (mw
, (XMotionEvent
*)event
);