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
;
37 xlwMenuTranslations
[] =
38 "<BtnDown>: start()\n\
43 #define offset(field) XtOffset(XlwMenuWidget, field)
47 {XtNfont
, XtCFont
, XtRFontStruct
, sizeof(XFontStruct
*),
48 offset(menu
.font
),XtRString
, "XtDefaultFont"},
49 {XtNforeground
, XtCForeground
, XtRPixel
, sizeof(Pixel
),
50 offset(menu
.foreground
), XtRString
, "XtDefaultForeground"},
51 {XtNbuttonForeground
, XtCButtonForeground
, XtRPixel
, sizeof(Pixel
),
52 offset(menu
.button_foreground
), XtRString
, "XtDefaultForeground"},
53 {XtNmargin
, XtCMargin
, XtRDimension
, sizeof(Dimension
),
54 offset(menu
.margin
), XtRImmediate
, (XtPointer
)0},
55 {XtNhorizontalSpacing
, XtCMargin
, XtRDimension
, sizeof(Dimension
),
56 offset(menu
.horizontal_spacing
), XtRImmediate
, (XtPointer
)3},
57 {XtNverticalSpacing
, XtCMargin
, XtRDimension
, sizeof(Dimension
),
58 offset(menu
.vertical_spacing
), XtRImmediate
, (XtPointer
)1},
59 {XtNarrowSpacing
, XtCMargin
, XtRDimension
, sizeof(Dimension
),
60 offset(menu
.arrow_spacing
), XtRImmediate
, (XtPointer
)10},
62 {XmNshadowThickness
, XmCShadowThickness
, XtRDimension
,
63 sizeof (Dimension
), offset (menu
.shadow_thickness
),
64 XtRImmediate
, (XtPointer
) 2},
65 {XmNtopShadowColor
, XmCTopShadowColor
, XtRPixel
, sizeof (Pixel
),
66 offset (menu
.top_shadow_color
), XtRImmediate
, (XtPointer
)-1},
67 {XmNbottomShadowColor
, XmCBottomShadowColor
, XtRPixel
, sizeof (Pixel
),
68 offset (menu
.bottom_shadow_color
), XtRImmediate
, (XtPointer
)-1},
69 {XmNtopShadowPixmap
, XmCTopShadowPixmap
, XtRPixmap
, sizeof (Pixmap
),
70 offset (menu
.top_shadow_pixmap
), XtRImmediate
, (XtPointer
)None
},
71 {XmNbottomShadowPixmap
, XmCBottomShadowPixmap
, XtRPixmap
, sizeof (Pixmap
),
72 offset (menu
.bottom_shadow_pixmap
), XtRImmediate
, (XtPointer
)None
},
74 {XtNopen
, XtCCallback
, XtRCallback
, sizeof(XtPointer
),
75 offset(menu
.open
), XtRCallback
, (XtPointer
)NULL
},
76 {XtNselect
, XtCCallback
, XtRCallback
, sizeof(XtPointer
),
77 offset(menu
.select
), XtRCallback
, (XtPointer
)NULL
},
78 {XtNmenu
, XtCMenu
, XtRPointer
, sizeof(XtPointer
),
79 offset(menu
.contents
), XtRImmediate
, (XtPointer
)NULL
},
80 {XtNcursor
, XtCCursor
, XtRCursor
, sizeof(Cursor
),
81 offset(menu
.cursor_shape
), XtRString
, (XtPointer
)"right_ptr"},
82 {XtNhorizontal
, XtCHorizontal
, XtRInt
, sizeof(int),
83 offset(menu
.horizontal
), XtRImmediate
, (XtPointer
)True
},
87 static Boolean
XlwMenuSetValues();
88 static void XlwMenuRealize();
89 static void XlwMenuRedisplay();
90 static void XlwMenuResize();
91 static void XlwMenuInitialize();
92 static void XlwMenuRedisplay();
93 static void XlwMenuDestroy();
94 static void XlwMenuClassInitialize();
100 xlwMenuActionsList
[] =
107 #define SuperClass ((CoreWidgetClass)&coreClassRec)
109 XlwMenuClassRec xlwMenuClassRec
=
111 { /* CoreClass fields initialization */
112 (WidgetClass
) SuperClass
, /* superclass */
113 "XlwMenu", /* class_name */
114 sizeof(XlwMenuRec
), /* size */
115 XlwMenuClassInitialize
, /* class_initialize */
116 NULL
, /* class_part_initialize */
117 FALSE
, /* class_inited */
118 XlwMenuInitialize
, /* initialize */
119 NULL
, /* initialize_hook */
120 XlwMenuRealize
, /* realize */
121 xlwMenuActionsList
, /* actions */
122 XtNumber(xlwMenuActionsList
), /* num_actions */
123 xlwMenuResources
, /* resources */
124 XtNumber(xlwMenuResources
), /* resource_count */
125 NULLQUARK
, /* xrm_class */
126 TRUE
, /* compress_motion */
127 TRUE
, /* compress_exposure */
128 TRUE
, /* compress_enterleave */
129 FALSE
, /* visible_interest */
130 XlwMenuDestroy
, /* destroy */
131 XlwMenuResize
, /* resize */
132 XlwMenuRedisplay
, /* expose */
133 XlwMenuSetValues
, /* set_values */
134 NULL
, /* set_values_hook */
135 XtInheritSetValuesAlmost
, /* set_values_almost */
136 NULL
, /* get_values_hook */
137 NULL
, /* accept_focus */
138 XtVersion
, /* version */
139 NULL
, /* callback_private */
140 xlwMenuTranslations
, /* tm_table */
141 XtInheritQueryGeometry
, /* query_geometry */
142 XtInheritDisplayAccelerator
, /* display_accelerator */
144 }, /* XlwMenuClass fields initialization */
150 WidgetClass xlwMenuWidgetClass
= (WidgetClass
) &xlwMenuClassRec
;
152 int submenu_destroyed
;
154 static int next_release_must_exit
;
158 push_new_stack (mw
, val
)
162 if (!mw
->menu
.new_stack
)
164 mw
->menu
.new_stack_length
= 10;
166 (widget_value
**)XtCalloc (mw
->menu
.new_stack_length
,
167 sizeof (widget_value
*));
169 else if (mw
->menu
.new_depth
== mw
->menu
.new_stack_length
)
171 mw
->menu
.new_stack_length
*= 2;
173 (widget_value
**)XtRealloc ((char*)mw
->menu
.new_stack
,
174 mw
->menu
.new_stack_length
* sizeof (widget_value
*));
176 mw
->menu
.new_stack
[mw
->menu
.new_depth
++] = val
;
180 pop_new_stack_if_no_contents (mw
)
183 if (mw
->menu
.new_depth
)
185 if (!mw
->menu
.new_stack
[mw
->menu
.new_depth
- 1]->contents
)
186 mw
->menu
.new_depth
-= 1;
191 make_old_stack_space (mw
, n
)
195 if (!mw
->menu
.old_stack
)
197 mw
->menu
.old_stack_length
= 10;
199 (widget_value
**)XtCalloc (mw
->menu
.old_stack_length
,
200 sizeof (widget_value
*));
202 else if (mw
->menu
.old_stack_length
< n
)
204 mw
->menu
.old_stack_length
*= 2;
206 (widget_value
**)XtRealloc ((char*)mw
->menu
.old_stack
,
207 mw
->menu
.old_stack_length
* sizeof (widget_value
*));
217 for (p
= s
; *p
== '-'; p
++);
229 XTextExtents (mw
->menu
.font
, s
, strlen (s
), &drop
, &drop
, &drop
, &xcs
);
237 return mw
->menu
.font
->ascent
/ 2 | 1;
243 {"labelString", "LabelString", XtRString
, sizeof(String
),
248 resource_widget_value (mw
, val
)
252 if (!val
->toolkit_data
)
254 char* resourced_name
= NULL
;
256 XtGetSubresources ((Widget
) mw
,
257 (XtPointer
) &resourced_name
,
258 val
->name
, val
->name
,
259 nameResource
, 1, NULL
, 0);
261 resourced_name
= val
->name
;
264 complete_name
= (char *) XtMalloc (strlen (resourced_name
) + 1);
265 strcpy (complete_name
, resourced_name
);
269 int complete_length
=
270 strlen (resourced_name
) + strlen (val
->value
) + 2;
271 complete_name
= XtMalloc (complete_length
);
273 strcat (complete_name
, resourced_name
);
274 strcat (complete_name
, " ");
275 strcat (complete_name
, val
->value
);
278 val
->toolkit_data
= complete_name
;
279 val
->free_toolkit_data
= True
;
281 return (char*)val
->toolkit_data
;
284 /* Returns the sizes of an item */
286 size_menu_item (mw
, val
, horizontal_p
, label_width
, rest_width
, height
)
294 if (all_dashes_p (val
->name
))
303 mw
->menu
.font
->ascent
+ mw
->menu
.font
->descent
304 + 2 * mw
->menu
.vertical_spacing
+ 2 * mw
->menu
.shadow_thickness
;
307 string_width (mw
, resource_widget_value (mw
, val
))
308 + mw
->menu
.horizontal_spacing
+ mw
->menu
.shadow_thickness
;
310 *rest_width
= mw
->menu
.horizontal_spacing
+ mw
->menu
.shadow_thickness
;
314 *rest_width
+= arrow_width (mw
) + mw
->menu
.arrow_spacing
;
317 string_width (mw
, val
->key
) + mw
->menu
.arrow_spacing
;
323 size_menu (mw
, level
)
329 int max_rest_width
= 0;
331 int horizontal_p
= mw
->menu
.horizontal
&& (level
== 0);
335 if (level
>= mw
->menu
.old_depth
)
338 ws
= &mw
->menu
.windows
[level
];
343 for (val
= mw
->menu
.old_stack
[level
]->contents
; val
; val
= val
->next
)
345 size_menu_item (mw
, val
, horizontal_p
, &label_width
, &rest_width
,
349 ws
->width
+= label_width
+ rest_width
;
350 if (height
> ws
->height
)
355 if (label_width
> ws
->label_width
)
356 ws
->label_width
= label_width
;
357 if (rest_width
> max_rest_width
)
358 max_rest_width
= rest_width
;
359 ws
->height
+= height
;
366 ws
->width
= ws
->label_width
+ max_rest_width
;
368 ws
->width
+= 2 * mw
->menu
.shadow_thickness
;
369 ws
->height
+= 2 * mw
->menu
.shadow_thickness
;
375 draw_arrow (mw
, window
, gc
, x
, y
, width
)
385 points
[0].y
= y
+ mw
->menu
.font
->ascent
;
388 points
[2].x
= x
+ width
;
389 points
[2].y
= y
+ mw
->menu
.font
->ascent
/ 2;
391 XFillPolygon (XtDisplay (mw
), window
, gc
, points
, 3, Convex
,
396 draw_shadow_rectangle (mw
, window
, x
, y
, width
, height
, erase_p
)
405 Display
*dpy
= XtDisplay (mw
);
406 GC top_gc
= !erase_p
? mw
->menu
.shadow_top_gc
: mw
->menu
.background_gc
;
407 GC bottom_gc
= !erase_p
? mw
->menu
.shadow_bottom_gc
: mw
->menu
.background_gc
;
408 int thickness
= mw
->menu
.shadow_thickness
;
412 points
[1].x
= x
+ width
;
414 points
[2].x
= x
+ width
- thickness
;
415 points
[2].y
= y
+ thickness
;
417 points
[3].y
= y
+ thickness
;
418 XFillPolygon (dpy
, window
, top_gc
, points
, 4, Convex
, CoordModeOrigin
);
420 points
[0].y
= y
+ thickness
;
422 points
[1].y
= y
+ height
;
423 points
[2].x
= x
+ thickness
;
424 points
[2].y
= y
+ height
- thickness
;
425 points
[3].x
= x
+ thickness
;
426 points
[3].y
= y
+ thickness
;
427 XFillPolygon (dpy
, window
, top_gc
, points
, 4, Convex
, CoordModeOrigin
);
428 points
[0].x
= x
+ width
;
430 points
[1].x
= x
+ width
- thickness
;
431 points
[1].y
= y
+ thickness
;
432 points
[2].x
= x
+ width
- thickness
;
433 points
[2].y
= y
+ height
- thickness
;
434 points
[3].x
= x
+ width
;
435 points
[3].y
= y
+ height
- thickness
;
436 XFillPolygon (dpy
, window
, bottom_gc
, points
, 4, Convex
, CoordModeOrigin
);
438 points
[0].y
= y
+ height
;
439 points
[1].x
= x
+ width
;
440 points
[1].y
= y
+ height
;
441 points
[2].x
= x
+ width
;
442 points
[2].y
= y
+ height
- thickness
;
443 points
[3].x
= x
+ thickness
;
444 points
[3].y
= y
+ height
- thickness
;
445 XFillPolygon (dpy
, window
, bottom_gc
, points
, 4, Convex
, CoordModeOrigin
);
449 /* Display the menu item and increment where.x and where.y to show how large
450 ** the menu item was.
453 display_menu_item (mw
, val
, ws
, where
, highlighted_p
, horizontal_p
, just_compute_p
)
458 Boolean highlighted_p
;
459 Boolean horizontal_p
;
460 Boolean just_compute_p
;
464 int font_ascent
= mw
->menu
.font
->ascent
;
465 int font_descent
= mw
->menu
.font
->descent
;
466 int shadow
= mw
->menu
.shadow_thickness
;
467 int separator_p
= all_dashes_p (val
->name
);
468 int h_spacing
= mw
->menu
.horizontal_spacing
;
469 int v_spacing
= mw
->menu
.vertical_spacing
;
476 /* compute the sizes of the item */
477 size_menu_item (mw
, val
, horizontal_p
, &label_width
, &rest_width
, &height
);
480 width
= label_width
+ rest_width
;
483 label_width
= ws
->label_width
;
484 width
= ws
->width
- 2 * shadow
;
487 /* see if it should be a button in the menubar */
488 button_p
= horizontal_p
&& val
->call_data
;
490 /* Only highlight an enabled item that has a callback. */
492 if (!val
->enabled
|| !(val
->call_data
|| val
->contents
))
495 /* do the drawing. */
498 /* Add the shadow border of the containing menu */
499 int x
= where
->x
+ shadow
;
500 int y
= where
->y
+ shadow
;
502 /* pick the foreground and background GC. */
504 text_gc
= button_p
? mw
->menu
.button_gc
: mw
->menu
.foreground_gc
;
507 button_p
? mw
->menu
.inactive_button_gc
: mw
->menu
.inactive_gc
;
508 deco_gc
= mw
->menu
.foreground_gc
;
512 XDrawLine (XtDisplay (mw
), ws
->window
, mw
->menu
.shadow_bottom_gc
,
514 XDrawLine (XtDisplay (mw
), ws
->window
, mw
->menu
.shadow_top_gc
,
515 x
, y
+ 1, x
+ width
, y
+ 1);
519 int x_offset
= x
+ h_spacing
+ shadow
;
520 char* display_string
= resource_widget_value (mw
, val
);
521 draw_shadow_rectangle (mw
, ws
->window
, x
, y
, width
, height
, True
);
523 /* Deal with centering a menu title. */
524 if (!horizontal_p
&& !val
->contents
&& !val
->call_data
)
526 int l
= string_width (mw
, display_string
);
529 x_offset
= (width
- l
) >> 1;
531 XDrawString (XtDisplay (mw
), ws
->window
, text_gc
, x_offset
,
532 y
+ v_spacing
+ shadow
+ font_ascent
,
533 display_string
, strlen (display_string
));
539 int a_w
= arrow_width (mw
);
540 draw_arrow (mw
, ws
->window
, deco_gc
,
541 x
+ width
- arrow_width (mw
)
542 - mw
->menu
.horizontal_spacing
543 - mw
->menu
.shadow_thickness
,
544 y
+ v_spacing
+ shadow
, a_w
);
548 XDrawString (XtDisplay (mw
), ws
->window
, text_gc
,
549 x
+ label_width
+ mw
->menu
.arrow_spacing
,
550 y
+ v_spacing
+ shadow
+ font_ascent
,
551 val
->key
, strlen (val
->key
));
558 XDrawRectangle (XtDisplay (mw
), ws
->window
, deco_gc
,
559 x
+ shadow
, y
+ shadow
,
560 label_width
+ h_spacing
- 1,
561 font_ascent
+ font_descent
+ 2 * v_spacing
- 1);
562 draw_shadow_rectangle (mw
, ws
->window
, x
, y
, width
, height
,
565 highlighted_p
= True
;
570 XDrawRectangle (XtDisplay (mw
), ws
->window
,
571 mw
->menu
.background_gc
,
572 x
+ shadow
, y
+ shadow
,
573 label_width
+ h_spacing
- 1,
574 font_ascent
+ font_descent
+ 2 * v_spacing
- 1);
575 draw_shadow_rectangle (mw
, ws
->window
, x
, y
, width
, height
,
580 draw_shadow_rectangle (mw
, ws
->window
, x
, y
, width
, height
, False
);
589 display_menu (mw
, level
, just_compute_p
, highlighted_pos
, hit
, hit_return
,
593 Boolean just_compute_p
;
594 XPoint
* highlighted_pos
;
596 widget_value
** hit_return
;
601 widget_value
* following_item
;
604 int horizontal_p
= mw
->menu
.horizontal
&& (level
== 0);
606 int just_compute_this_one_p
;
608 if (level
>= mw
->menu
.old_depth
)
611 if (level
< mw
->menu
.old_depth
- 1)
612 following_item
= mw
->menu
.old_stack
[level
+ 1];
614 following_item
= NULL
;
622 ws
= &mw
->menu
.windows
[level
];
623 for (val
= mw
->menu
.old_stack
[level
]->contents
; val
; val
= val
->next
)
625 highlighted_p
= val
== following_item
;
626 if (highlighted_p
&& highlighted_pos
)
629 highlighted_pos
->x
= where
.x
;
631 highlighted_pos
->y
= where
.y
;
634 just_compute_this_one_p
=
635 just_compute_p
|| ((this || that
) && val
!= this && val
!= that
);
637 display_menu_item (mw
, val
, ws
, &where
, highlighted_p
, horizontal_p
,
638 just_compute_this_one_p
);
640 if (highlighted_p
&& highlighted_pos
)
643 highlighted_pos
->y
= where
.y
;
645 highlighted_pos
->x
= where
.x
;
650 && (horizontal_p
? hit
->x
< where
.x
: hit
->y
< where
.y
)
651 && !all_dashes_p (val
->name
))
661 draw_shadow_rectangle (mw
, ws
->window
, 0, 0, ws
->width
, ws
->height
, False
);
666 set_new_state (mw
, val
, level
)
673 mw
->menu
.new_depth
= 0;
674 for (i
= 0; i
< level
; i
++)
675 push_new_stack (mw
, mw
->menu
.old_stack
[i
]);
676 push_new_stack (mw
, val
);
680 make_windows_if_needed (mw
, n
)
686 XSetWindowAttributes xswa
;
688 Window root
= RootWindowOfScreen (DefaultScreenOfDisplay (XtDisplay (mw
)));
689 window_state
* windows
;
691 if (mw
->menu
.windows_length
>= n
)
694 xswa
.save_under
= True
;
695 xswa
.override_redirect
= True
;
696 xswa
.background_pixel
= mw
->core
.background_pixel
;
697 xswa
.border_pixel
= mw
->core
.border_pixel
;
699 ExposureMask
| PointerMotionMask
| PointerMotionHintMask
700 | ButtonReleaseMask
| ButtonPressMask
;
701 xswa
.cursor
= mw
->menu
.cursor_shape
;
702 mask
= CWSaveUnder
| CWOverrideRedirect
| CWBackPixel
| CWBorderPixel
703 | CWEventMask
| CWCursor
;
705 if (!mw
->menu
.windows
)
708 (window_state
*)XtMalloc (n
* sizeof (window_state
));
714 (window_state
*)XtRealloc ((char*)mw
->menu
.windows
,
715 n
* sizeof (window_state
));
716 start_at
= mw
->menu
.windows_length
;
718 mw
->menu
.windows_length
= n
;
720 windows
= mw
->menu
.windows
;
722 for (i
= start_at
; i
< n
; i
++)
726 windows
[i
].width
= 1;
727 windows
[i
].height
= 1;
729 XCreateWindow (XtDisplay (mw
), root
, 0, 0, 1, 1,
730 0, 0, CopyFromParent
, CopyFromParent
, mask
, &xswa
);
734 /* Make the window fit in the screen */
736 fit_to_screen (mw
, ws
, previous_ws
, horizontal_p
)
739 window_state
* previous_ws
;
740 Boolean horizontal_p
;
742 int screen_width
= WidthOfScreen (XtScreen (mw
));
743 int screen_height
= HeightOfScreen (XtScreen (mw
));
747 else if (ws
->x
+ ws
->width
> screen_width
)
750 ws
->x
= previous_ws
->x
- ws
->width
;
752 ws
->x
= screen_width
- ws
->width
;
758 else if (ws
->y
+ ws
->height
> screen_height
)
761 ws
->y
= previous_ws
->y
- ws
->height
;
763 ws
->y
= screen_height
- ws
->height
;
769 /* Updates old_stack from new_stack and redisplays. */
776 XPoint selection_position
;
777 int old_depth
= mw
->menu
.old_depth
;
778 int new_depth
= mw
->menu
.new_depth
;
779 widget_value
** old_stack
;
780 widget_value
** new_stack
;
781 window_state
* windows
;
782 widget_value
* old_selection
;
783 widget_value
* new_selection
;
785 /* Check that enough windows and old_stack are ready. */
786 make_windows_if_needed (mw
, new_depth
);
787 make_old_stack_space (mw
, new_depth
);
788 windows
= mw
->menu
.windows
;
789 old_stack
= mw
->menu
.old_stack
;
790 new_stack
= mw
->menu
.new_stack
;
792 /* compute the last identical different entry */
793 for (i
= 1; i
< old_depth
&& i
< new_depth
; i
++)
794 if (old_stack
[i
] != new_stack
[i
])
798 /* Memorize the previously selected item to be able to refresh it */
799 old_selection
= last_same
+ 1 < old_depth
? old_stack
[last_same
+ 1] : NULL
;
800 if (old_selection
&& !old_selection
->enabled
)
801 old_selection
= NULL
;
802 new_selection
= last_same
+ 1 < new_depth
? new_stack
[last_same
+ 1] : NULL
;
803 if (new_selection
&& !new_selection
->enabled
)
804 new_selection
= NULL
;
806 /* updates old_state from new_state. It has to be done now because
807 display_menu (called below) uses the old_stack to know what to display. */
808 for (i
= last_same
+ 1; i
< new_depth
; i
++)
809 old_stack
[i
] = new_stack
[i
];
810 mw
->menu
.old_depth
= new_depth
;
812 /* refresh the last seletion */
813 selection_position
.x
= 0;
814 selection_position
.y
= 0;
815 display_menu (mw
, last_same
, new_selection
== old_selection
,
816 &selection_position
, NULL
, NULL
, old_selection
, new_selection
);
818 /* Now popup the new menus */
819 for (i
= last_same
+ 1; i
< new_depth
&& new_stack
[i
]->contents
; i
++)
821 window_state
* previous_ws
= &windows
[i
- 1];
822 window_state
* ws
= &windows
[i
];
825 previous_ws
->x
+ selection_position
.x
+ mw
->menu
.shadow_thickness
;
826 if (!mw
->menu
.horizontal
|| i
> 1)
827 ws
->x
+= mw
->menu
.shadow_thickness
;
829 previous_ws
->y
+ selection_position
.y
+ mw
->menu
.shadow_thickness
;
833 fit_to_screen (mw
, ws
, previous_ws
, mw
->menu
.horizontal
&& i
== 1);
835 XClearWindow (XtDisplay (mw
), ws
->window
);
836 XMoveResizeWindow (XtDisplay (mw
), ws
->window
, ws
->x
, ws
->y
,
837 ws
->width
, ws
->height
);
838 XMapRaised (XtDisplay (mw
), ws
->window
);
839 display_menu (mw
, i
, False
, &selection_position
, NULL
, NULL
, NULL
, NULL
);
842 /* unmap the menus that popped down */
843 for (i
= new_depth
- 1; i
< old_depth
; i
++)
844 if (i
>= new_depth
|| !new_stack
[i
]->contents
)
845 XUnmapWindow (XtDisplay (mw
), windows
[i
].window
);
849 motion_event_is_in_menu (mw
, ev
, level
, relative_pos
)
853 XPoint
* relative_pos
;
855 window_state
* ws
= &mw
->menu
.windows
[level
];
856 int x
= level
== 0 ? ws
->x
: ws
->x
+ mw
->menu
.shadow_thickness
;
857 int y
= level
== 0 ? ws
->y
: ws
->y
+ mw
->menu
.shadow_thickness
;
858 relative_pos
->x
= ev
->x_root
- x
;
859 relative_pos
->y
= ev
->y_root
- y
;
860 return (x
< ev
->x_root
&& ev
->x_root
< x
+ ws
->width
861 && y
< ev
->y_root
&& ev
->y_root
< y
+ ws
->height
);
865 map_event_to_widget_value (mw
, ev
, val
, level
)
877 /* Find the window */
878 for (i
= mw
->menu
.old_depth
- 1; i
>= 0; i
--)
880 ws
= &mw
->menu
.windows
[i
];
881 if (ws
&& motion_event_is_in_menu (mw
, ev
, i
, &relative_pos
))
883 display_menu (mw
, i
, True
, NULL
, &relative_pos
, val
, NULL
, NULL
);
897 make_drawing_gcs (mw
)
902 xgcv
.font
= mw
->menu
.font
->fid
;
903 xgcv
.foreground
= mw
->menu
.foreground
;
904 xgcv
.background
= mw
->core
.background_pixel
;
905 mw
->menu
.foreground_gc
= XtGetGC ((Widget
)mw
,
906 GCFont
| GCForeground
| GCBackground
,
909 xgcv
.font
= mw
->menu
.font
->fid
;
910 xgcv
.foreground
= mw
->menu
.button_foreground
;
911 xgcv
.background
= mw
->core
.background_pixel
;
912 mw
->menu
.button_gc
= XtGetGC ((Widget
)mw
,
913 GCFont
| GCForeground
| GCBackground
,
916 xgcv
.font
= mw
->menu
.font
->fid
;
917 xgcv
.foreground
= mw
->menu
.foreground
;
918 xgcv
.background
= mw
->core
.background_pixel
;
919 xgcv
.fill_style
= FillStippled
;
920 xgcv
.stipple
= mw
->menu
.gray_pixmap
;
921 mw
->menu
.inactive_gc
= XtGetGC ((Widget
)mw
,
922 (GCFont
| GCForeground
| GCBackground
923 | GCFillStyle
| GCStipple
), &xgcv
);
925 xgcv
.font
= mw
->menu
.font
->fid
;
926 xgcv
.foreground
= mw
->menu
.button_foreground
;
927 xgcv
.background
= mw
->core
.background_pixel
;
928 xgcv
.fill_style
= FillStippled
;
929 xgcv
.stipple
= mw
->menu
.gray_pixmap
;
930 mw
->menu
.inactive_button_gc
= XtGetGC ((Widget
)mw
,
931 (GCFont
| GCForeground
| GCBackground
932 | GCFillStyle
| GCStipple
), &xgcv
);
934 xgcv
.font
= mw
->menu
.font
->fid
;
935 xgcv
.foreground
= mw
->core
.background_pixel
;
936 xgcv
.background
= mw
->menu
.foreground
;
937 mw
->menu
.background_gc
= XtGetGC ((Widget
)mw
,
938 GCFont
| GCForeground
| GCBackground
,
943 release_drawing_gcs (mw
)
946 XtReleaseGC ((Widget
) mw
, mw
->menu
.foreground_gc
);
947 XtReleaseGC ((Widget
) mw
, mw
->menu
.button_gc
);
948 XtReleaseGC ((Widget
) mw
, mw
->menu
.inactive_gc
);
949 XtReleaseGC ((Widget
) mw
, mw
->menu
.inactive_button_gc
);
950 XtReleaseGC ((Widget
) mw
, mw
->menu
.background_gc
);
951 /* let's get some segvs if we try to use these... */
952 mw
->menu
.foreground_gc
= (GC
) -1;
953 mw
->menu
.button_gc
= (GC
) -1;
954 mw
->menu
.inactive_gc
= (GC
) -1;
955 mw
->menu
.inactive_button_gc
= (GC
) -1;
956 mw
->menu
.background_gc
= (GC
) -1;
959 #define MINL(x,y) ((((unsigned long) (x)) < ((unsigned long) (y))) \
960 ? ((unsigned long) (x)) : ((unsigned long) (y)))
967 unsigned long pm
= 0;
968 Display
*dpy
= XtDisplay ((Widget
) mw
);
969 Colormap cmap
= DefaultColormapOfScreen (XtScreen ((Widget
) mw
));
971 int top_frobbed
= 0, bottom_frobbed
= 0;
973 if (mw
->menu
.top_shadow_color
== -1)
974 mw
->menu
.top_shadow_color
= mw
->core
.background_pixel
;
975 if (mw
->menu
.bottom_shadow_color
== -1)
976 mw
->menu
.bottom_shadow_color
= mw
->menu
.foreground
;
978 if (mw
->menu
.top_shadow_color
== mw
->core
.background_pixel
||
979 mw
->menu
.top_shadow_color
== mw
->menu
.foreground
)
981 topc
.pixel
= mw
->core
.background_pixel
;
982 XQueryColor (dpy
, cmap
, &topc
);
983 /* don't overflow/wrap! */
984 topc
.red
= MINL (65535, topc
.red
* 1.2);
985 topc
.green
= MINL (65535, topc
.green
* 1.2);
986 topc
.blue
= MINL (65535, topc
.blue
* 1.2);
987 if (XAllocColor (dpy
, cmap
, &topc
))
989 mw
->menu
.top_shadow_color
= topc
.pixel
;
993 if (mw
->menu
.bottom_shadow_color
== mw
->menu
.foreground
||
994 mw
->menu
.bottom_shadow_color
== mw
->core
.background_pixel
)
996 botc
.pixel
= mw
->core
.background_pixel
;
997 XQueryColor (dpy
, cmap
, &botc
);
1001 if (XAllocColor (dpy
, cmap
, &botc
))
1003 mw
->menu
.bottom_shadow_color
= botc
.pixel
;
1008 if (top_frobbed
&& bottom_frobbed
)
1010 int top_avg
= ((topc
.red
/ 3) + (topc
.green
/ 3) + (topc
.blue
/ 3));
1011 int bot_avg
= ((botc
.red
/ 3) + (botc
.green
/ 3) + (botc
.blue
/ 3));
1012 if (bot_avg
> top_avg
)
1014 Pixel tmp
= mw
->menu
.top_shadow_color
;
1015 mw
->menu
.top_shadow_color
= mw
->menu
.bottom_shadow_color
;
1016 mw
->menu
.bottom_shadow_color
= tmp
;
1018 else if (topc
.pixel
== botc
.pixel
)
1020 if (botc
.pixel
== mw
->menu
.foreground
)
1021 mw
->menu
.top_shadow_color
= mw
->core
.background_pixel
;
1023 mw
->menu
.bottom_shadow_color
= mw
->menu
.foreground
;
1027 if (!mw
->menu
.top_shadow_pixmap
&&
1028 mw
->menu
.top_shadow_color
== mw
->core
.background_pixel
)
1030 mw
->menu
.top_shadow_pixmap
= mw
->menu
.gray_pixmap
;
1031 mw
->menu
.top_shadow_color
= mw
->menu
.foreground
;
1033 if (!mw
->menu
.bottom_shadow_pixmap
&&
1034 mw
->menu
.bottom_shadow_color
== mw
->core
.background_pixel
)
1036 mw
->menu
.bottom_shadow_pixmap
= mw
->menu
.gray_pixmap
;
1037 mw
->menu
.bottom_shadow_color
= mw
->menu
.foreground
;
1040 xgcv
.fill_style
= FillStippled
;
1041 xgcv
.foreground
= mw
->menu
.top_shadow_color
;
1042 xgcv
.stipple
= mw
->menu
.top_shadow_pixmap
;
1043 pm
= (xgcv
.stipple
? GCStipple
|GCFillStyle
: 0);
1044 mw
->menu
.shadow_top_gc
= XtGetGC ((Widget
)mw
, GCForeground
| pm
, &xgcv
);
1046 xgcv
.foreground
= mw
->menu
.bottom_shadow_color
;
1047 xgcv
.stipple
= mw
->menu
.bottom_shadow_pixmap
;
1048 pm
= (xgcv
.stipple
? GCStipple
|GCFillStyle
: 0);
1049 mw
->menu
.shadow_bottom_gc
= XtGetGC ((Widget
)mw
, GCForeground
| pm
, &xgcv
);
1054 release_shadow_gcs (mw
)
1057 XtReleaseGC ((Widget
) mw
, mw
->menu
.shadow_top_gc
);
1058 XtReleaseGC ((Widget
) mw
, mw
->menu
.shadow_bottom_gc
);
1062 XlwMenuInitialize (request
, new, args
, num_args
)
1068 /* Get the GCs and the widget size */
1069 XlwMenuWidget mw
= (XlwMenuWidget
)new;
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 make_drawing_gcs (mw
);
1094 make_shadow_gcs (mw
);
1096 xswa
.background_pixel
= mw
->core
.background_pixel
;
1097 xswa
.border_pixel
= mw
->core
.border_pixel
;
1098 mask
= CWBackPixel
| CWBorderPixel
;
1100 mw
->menu
.popped_up
= False
;
1102 mw
->menu
.old_depth
= 1;
1103 mw
->menu
.old_stack
= (widget_value
**)XtMalloc (sizeof (widget_value
*));
1104 mw
->menu
.old_stack_length
= 1;
1105 mw
->menu
.old_stack
[0] = mw
->menu
.contents
;
1107 mw
->menu
.new_depth
= 0;
1108 mw
->menu
.new_stack
= 0;
1109 mw
->menu
.new_stack_length
= 0;
1110 push_new_stack (mw
, mw
->menu
.contents
);
1112 mw
->menu
.windows
= (window_state
*)XtMalloc (sizeof (window_state
));
1113 mw
->menu
.windows_length
= 1;
1114 mw
->menu
.windows
[0].x
= 0;
1115 mw
->menu
.windows
[0].y
= 0;
1116 mw
->menu
.windows
[0].width
= 0;
1117 mw
->menu
.windows
[0].height
= 0;
1120 mw
->core
.width
= mw
->menu
.windows
[0].width
;
1121 mw
->core
.height
= mw
->menu
.windows
[0].height
;
1125 XlwMenuClassInitialize ()
1130 XlwMenuRealize (w
, valueMask
, attributes
)
1133 XSetWindowAttributes
*attributes
;
1135 XlwMenuWidget mw
= (XlwMenuWidget
)w
;
1136 XSetWindowAttributes xswa
;
1139 (*xlwMenuWidgetClass
->core_class
.superclass
->core_class
.realize
)
1140 (w
, valueMask
, attributes
);
1142 xswa
.save_under
= True
;
1143 xswa
.cursor
= mw
->menu
.cursor_shape
;
1144 mask
= CWSaveUnder
| CWCursor
;
1145 XChangeWindowAttributes (XtDisplay (w
), XtWindow (w
), mask
, &xswa
);
1147 mw
->menu
.windows
[0].window
= XtWindow (w
);
1148 mw
->menu
.windows
[0].x
= w
->core
.x
;
1149 mw
->menu
.windows
[0].y
= w
->core
.y
;
1150 mw
->menu
.windows
[0].width
= w
->core
.width
;
1151 mw
->menu
.windows
[0].height
= w
->core
.height
;
1154 /* Only the toplevel menubar/popup is a widget so it's the only one that
1155 receives expose events through Xt. So we repaint all the other panes
1156 when receiving an Expose event. */
1158 XlwMenuRedisplay (w
, ev
, region
)
1163 XlwMenuWidget mw
= (XlwMenuWidget
)w
;
1166 /* If we have a depth beyond 1, it's because a submenu was displayed.
1167 If the submenu has been destroyed, set the depth back to 1. */
1168 if (submenu_destroyed
)
1170 mw
->menu
.old_depth
= 1;
1171 submenu_destroyed
= 0;
1174 for (i
= 0; i
< mw
->menu
.old_depth
; i
++)
1175 display_menu (mw
, i
, False
, NULL
, NULL
, NULL
, NULL
, NULL
);
1183 XlwMenuWidget mw
= (XlwMenuWidget
) w
;
1185 if (pointer_grabbed
)
1186 XtUngrabPointer ((Widget
)w
, CurrentTime
);
1187 pointer_grabbed
= 0;
1189 submenu_destroyed
= 1;
1191 release_drawing_gcs (mw
);
1192 release_shadow_gcs (mw
);
1194 /* this doesn't come from the resource db but is created explicitly
1195 so we must free it ourselves. */
1196 XFreePixmap (XtDisplay (mw
), mw
->menu
.gray_pixmap
);
1197 mw
->menu
.gray_pixmap
= (Pixmap
) -1;
1200 /* Do free mw->menu.contents because nowadays we copy it
1201 during initialization. */
1202 XtFree (mw
->menu
.contents
);
1205 /* Don't free mw->menu.contents because that comes from our creator.
1206 The `*_stack' elements are just pointers into `contents' so leave
1207 that alone too. But free the stacks themselves. */
1208 if (mw
->menu
.old_stack
) XtFree ((char *) mw
->menu
.old_stack
);
1209 if (mw
->menu
.new_stack
) XtFree ((char *) mw
->menu
.new_stack
);
1211 /* Remember, you can't free anything that came from the resource
1212 database. This includes:
1214 mw->menu.top_shadow_pixmap
1215 mw->menu.bottom_shadow_pixmap
1217 Also the color cells of top_shadow_color, bottom_shadow_color,
1218 foreground, and button_foreground will never be freed until this
1219 client exits. Nice, eh?
1222 /* start from 1 because the one in slot 0 is w->core.window */
1223 for (i
= 1; i
< mw
->menu
.windows_length
; i
++)
1224 XDestroyWindow (XtDisplay (mw
), mw
->menu
.windows
[i
].window
);
1225 if (mw
->menu
.windows
)
1226 XtFree ((char *) mw
->menu
.windows
);
1230 XlwMenuSetValues (current
, request
, new)
1235 XlwMenuWidget oldmw
= (XlwMenuWidget
)current
;
1236 XlwMenuWidget newmw
= (XlwMenuWidget
)new;
1237 Boolean redisplay
= False
;
1240 if (newmw
->menu
.contents
1241 && newmw
->menu
.contents
->contents
1242 && newmw
->menu
.contents
->contents
->change
>= VISIBLE_CHANGE
)
1245 if (newmw
->core
.background_pixel
!= oldmw
->core
.background_pixel
1246 || newmw
->menu
.foreground
!= oldmw
->menu
.foreground
1247 || newmw
->menu
.font
!= oldmw
->menu
.font
)
1249 release_drawing_gcs (newmw
);
1250 make_drawing_gcs (newmw
);
1253 for (i
= 0; i
< oldmw
->menu
.windows_length
; i
++)
1255 XSetWindowBackground (XtDisplay (oldmw
),
1256 oldmw
->menu
.windows
[i
].window
,
1257 newmw
->core
.background_pixel
);
1258 /* clear windows and generate expose events */
1259 XClearArea (XtDisplay (oldmw
), oldmw
->menu
.windows
[i
].window
,
1271 XlwMenuWidget mw
= (XlwMenuWidget
)w
;
1273 if (mw
->menu
.popped_up
)
1275 /* Don't allow the popup menu to resize itself. */
1276 mw
->core
.width
= mw
->menu
.windows
[0].width
;
1277 mw
->core
.height
= mw
->menu
.windows
[0].height
;
1278 mw
->core
.parent
->core
.width
= mw
->core
.width
;
1279 mw
->core
.parent
->core
.height
= mw
->core
.height
;
1283 mw
->menu
.windows
[0].width
= mw
->core
.width
;
1284 mw
->menu
.windows
[0].height
= mw
->core
.height
;
1288 \f/* Action procedures */
1290 handle_single_motion_event (mw
, ev
)
1297 if (!map_event_to_widget_value (mw
, ev
, &val
, &level
))
1298 pop_new_stack_if_no_contents (mw
);
1300 set_new_state (mw
, val
, level
);
1303 /* Sync with the display. Makes it feel better on X terms. */
1304 XSync (XtDisplay (mw
), False
);
1308 handle_motion_event (mw
, ev
)
1314 int state
= ev
->state
;
1316 handle_single_motion_event (mw
, ev
);
1318 /* allow motion events to be generated again */
1320 && XQueryPointer (XtDisplay (mw
), ev
->window
,
1321 &ev
->root
, &ev
->subwindow
,
1322 &ev
->x_root
, &ev
->y_root
,
1325 && ev
->state
== state
1326 && (ev
->x_root
!= x
|| ev
->y_root
!= y
))
1327 handle_single_motion_event (mw
, ev
);
1331 Start (w
, ev
, params
, num_params
)
1335 Cardinal
*num_params
;
1337 XlwMenuWidget mw
= (XlwMenuWidget
)w
;
1339 if (!mw
->menu
.popped_up
)
1341 menu_post_event
= *ev
;
1342 pop_up_menu (mw
, ev
);
1346 /* If we push a button while the menu is posted semipermanently,
1347 releasing the button should always pop the menu down. */
1348 next_release_must_exit
= 1;
1350 /* notes the absolute position of the menubar window */
1351 mw
->menu
.windows
[0].x
= ev
->xmotion
.x_root
- ev
->xmotion
.x
;
1352 mw
->menu
.windows
[0].y
= ev
->xmotion
.y_root
- ev
->xmotion
.y
;
1354 /* handles the down like a move, slots are compatible */
1355 handle_motion_event (mw
, &ev
->xmotion
);
1360 Drag (w
, ev
, params
, num_params
)
1364 Cardinal
*num_params
;
1366 XlwMenuWidget mw
= (XlwMenuWidget
)w
;
1367 handle_motion_event (mw
, &ev
->xmotion
);
1371 Select (w
, ev
, params
, num_params
)
1375 Cardinal
*num_params
;
1377 XlwMenuWidget mw
= (XlwMenuWidget
)w
;
1378 widget_value
* selected_item
= mw
->menu
.old_stack
[mw
->menu
.old_depth
- 1];
1380 /* If user releases the button quickly, without selecting anything,
1381 after the initial down-click that brought the menu up,
1383 if ((selected_item
== 0
1384 || ((widget_value
*) selected_item
)->call_data
== 0)
1385 && !next_release_must_exit
1386 && (ev
->xbutton
.time
- menu_post_event
.xbutton
.time
1387 < XtGetMultiClickTime (XtDisplay (w
))))
1390 /* pop down everything. */
1391 mw
->menu
.new_depth
= 1;
1394 if (mw
->menu
.popped_up
)
1396 mw
->menu
.popped_up
= False
;
1397 XtUngrabPointer ((Widget
)mw
, ev
->xmotion
.time
);
1398 if (XtIsShell (XtParent ((Widget
) mw
)))
1399 XtPopdown (XtParent ((Widget
) mw
));
1402 XtRemoveGrab ((Widget
) mw
);
1403 display_menu (mw
, 0, False
, NULL
, NULL
, NULL
, NULL
, NULL
);
1408 XtCallCallbackList ((Widget
)mw
, mw
->menu
.select
, (XtPointer
)selected_item
);
1412 \f/* Special code to pop-up a menu */
1414 pop_up_menu (mw
, event
)
1416 XButtonPressedEvent
* event
;
1418 int x
= event
->x_root
;
1419 int y
= event
->y_root
;
1422 int borderwidth
= mw
->menu
.shadow_thickness
;
1423 Screen
* screen
= XtScreen (mw
);
1424 Display
*display
= XtDisplay (mw
);
1426 next_release_must_exit
= 0;
1428 XtCallCallbackList ((Widget
)mw
, mw
->menu
.open
, NULL
);
1430 if (XtIsShell (XtParent ((Widget
)mw
)))
1433 w
= mw
->menu
.windows
[0].width
;
1434 h
= mw
->menu
.windows
[0].height
;
1438 if (x
< borderwidth
)
1440 if (x
+ w
+ 2 * borderwidth
> WidthOfScreen (screen
))
1441 x
= WidthOfScreen (screen
) - w
- 2 * borderwidth
;
1442 if (y
< borderwidth
)
1444 if (y
+ h
+ 2 * borderwidth
> HeightOfScreen (screen
))
1445 y
= HeightOfScreen (screen
) - h
- 2 * borderwidth
;
1447 mw
->menu
.popped_up
= True
;
1448 if (XtIsShell (XtParent ((Widget
)mw
)))
1450 XtConfigureWidget (XtParent ((Widget
)mw
), x
, y
, w
, h
,
1451 XtParent ((Widget
)mw
)->core
.border_width
);
1452 XtPopup (XtParent ((Widget
)mw
), XtGrabExclusive
);
1453 display_menu (mw
, 0, False
, NULL
, NULL
, NULL
, NULL
, NULL
);
1454 mw
->menu
.windows
[0].x
= x
+ borderwidth
;
1455 mw
->menu
.windows
[0].y
= y
+ borderwidth
;
1459 XEvent
*ev
= (XEvent
*) event
;
1461 XtAddGrab ((Widget
) mw
, True
, True
);
1463 /* notes the absolute position of the menubar window */
1464 mw
->menu
.windows
[0].x
= ev
->xmotion
.x_root
- ev
->xmotion
.x
;
1465 mw
->menu
.windows
[0].y
= ev
->xmotion
.y_root
- ev
->xmotion
.y
;
1469 x_catch_errors (display
);
1471 XtGrabPointer ((Widget
)mw
, False
,
1473 | PointerMotionHintMask
1476 GrabModeAsync
, GrabModeAsync
, None
,
1477 mw
->menu
.cursor_shape
,
1479 pointer_grabbed
= 1;
1481 if (x_had_errors_p (display
))
1483 pointer_grabbed
= 0;
1484 XtUngrabPointer ((Widget
)mw
, event
->time
);
1486 x_uncatch_errors (display
);
1489 handle_motion_event (mw
, (XMotionEvent
*)event
);