1 /* Implements a lightweight menubar widget.
2 Copyright (C) 1992 Lucid, Inc.
4 This file is part of the Lucid Widget Library.
6 The Lucid Widget Library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
11 The Lucid Widget Library is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU Emacs; see the file COPYING. If not, write to
18 the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
20 /* Created by devin@lucid.com */
27 #include <sys/types.h>
29 #include <X11/IntrinsicP.h>
30 #include <X11/StringDefs.h>
31 #include <X11/cursorfont.h>
32 #include <X11/bitmaps/gray>
37 xlwMenuTranslations
[] =
38 "<BtnDown>: start()\n\
39 <BtnMotion>: drag()\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
, XmRHorizontalDimension
,
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
;
154 push_new_stack (XlwMenuWidget mw
, widget_value
* val
)
156 if (!mw
->menu
.new_stack
)
158 mw
->menu
.new_stack_length
= 10;
160 (widget_value
**)XtCalloc (mw
->menu
.new_stack_length
,
161 sizeof (widget_value
*));
163 else if (mw
->menu
.new_depth
== mw
->menu
.new_stack_length
)
165 mw
->menu
.new_stack_length
*= 2;
167 (widget_value
**)XtRealloc ((char*)mw
->menu
.new_stack
,
168 mw
->menu
.new_stack_length
* sizeof (widget_value
*));
170 mw
->menu
.new_stack
[mw
->menu
.new_depth
++] = val
;
174 pop_new_stack_if_no_contents (XlwMenuWidget mw
)
176 if (mw
->menu
.new_depth
)
178 if (!mw
->menu
.new_stack
[mw
->menu
.new_depth
- 1]->contents
)
179 mw
->menu
.new_depth
-= 1;
184 make_old_stack_space (XlwMenuWidget mw
, int n
)
186 if (!mw
->menu
.old_stack
)
188 mw
->menu
.old_stack_length
= 10;
190 (widget_value
**)XtCalloc (mw
->menu
.old_stack_length
,
191 sizeof (widget_value
*));
193 else if (mw
->menu
.old_stack_length
< n
)
195 mw
->menu
.old_stack_length
*= 2;
197 (widget_value
**)XtRealloc ((char*)mw
->menu
.old_stack
,
198 mw
->menu
.old_stack_length
* sizeof (widget_value
*));
204 all_dashes_p (char* s
)
207 for (p
= s
; *p
== '-'; p
++);
212 string_width (XlwMenuWidget mw
, char* s
)
217 XTextExtents (mw
->menu
.font
, s
, strlen (s
), &drop
, &drop
, &drop
, &xcs
);
222 arrow_width (XlwMenuWidget mw
)
224 return mw
->menu
.font
->ascent
/ 2 | 1;
230 {"labelString", "LabelString", XtRString
, sizeof(String
),
235 resource_widget_value (XlwMenuWidget mw
, widget_value
* val
)
237 if (!val
->toolkit_data
)
239 char* resourced_name
= NULL
;
241 XtGetSubresources ((Widget
) mw
,
242 (XtPointer
) &resourced_name
,
243 val
->name
, val
->name
,
244 nameResource
, 1, NULL
, 0);
246 resourced_name
= val
->name
;
248 complete_name
= (char *) strdup (resourced_name
);
251 int complete_length
=
252 strlen (resourced_name
) + strlen (val
->value
) + 2;
253 complete_name
= XtMalloc (complete_length
);
255 strcat (complete_name
, resourced_name
);
256 strcat (complete_name
, " ");
257 strcat (complete_name
, val
->value
);
260 val
->toolkit_data
= complete_name
;
261 val
->free_toolkit_data
= True
;
263 return (char*)val
->toolkit_data
;
266 /* Returns the sizes of an item */
268 size_menu_item (XlwMenuWidget mw
, widget_value
* val
, int horizontal_p
,
269 int* label_width
, int* rest_width
, int* height
)
271 if (all_dashes_p (val
->name
))
280 mw
->menu
.font
->ascent
+ mw
->menu
.font
->descent
281 + 2 * mw
->menu
.vertical_spacing
+ 2 * mw
->menu
.shadow_thickness
;
284 string_width (mw
, resource_widget_value (mw
, val
))
285 + mw
->menu
.horizontal_spacing
+ mw
->menu
.shadow_thickness
;
287 *rest_width
= mw
->menu
.horizontal_spacing
+ mw
->menu
.shadow_thickness
;
291 *rest_width
+= arrow_width (mw
) + mw
->menu
.arrow_spacing
;
294 string_width (mw
, val
->key
) + mw
->menu
.arrow_spacing
;
300 size_menu (XlwMenuWidget mw
, int level
)
304 int max_rest_width
= 0;
306 int horizontal_p
= mw
->menu
.horizontal
&& (level
== 0);
310 if (level
>= mw
->menu
.old_depth
)
313 ws
= &mw
->menu
.windows
[level
];
318 for (val
= mw
->menu
.old_stack
[level
]->contents
; val
; val
= val
->next
)
320 size_menu_item (mw
, val
, horizontal_p
, &label_width
, &rest_width
,
324 ws
->width
+= label_width
+ rest_width
;
325 if (height
> ws
->height
)
330 if (label_width
> ws
->label_width
)
331 ws
->label_width
= label_width
;
332 if (rest_width
> max_rest_width
)
333 max_rest_width
= rest_width
;
334 ws
->height
+= height
;
341 ws
->width
= ws
->label_width
+ max_rest_width
;
343 ws
->width
+= 2 * mw
->menu
.shadow_thickness
;
344 ws
->height
+= 2 * mw
->menu
.shadow_thickness
;
350 draw_arrow (XlwMenuWidget mw
, Window window
, GC gc
, int x
, int y
, int width
)
354 points
[0].y
= y
+ mw
->menu
.font
->ascent
;
357 points
[2].x
= x
+ width
;
358 points
[2].y
= y
+ mw
->menu
.font
->ascent
/ 2;
360 XFillPolygon (XtDisplay (mw
), window
, gc
, points
, 3, Convex
,
365 draw_shadow_rectangle (XlwMenuWidget mw
, Window window
,
366 int x
, int y
, int width
, int height
, int erase_p
)
368 Display
*dpy
= XtDisplay (mw
);
369 GC top_gc
= !erase_p
? mw
->menu
.shadow_top_gc
: mw
->menu
.background_gc
;
370 GC bottom_gc
= !erase_p
? mw
->menu
.shadow_bottom_gc
: mw
->menu
.background_gc
;
371 int thickness
= mw
->menu
.shadow_thickness
;
375 points
[1].x
= x
+ width
;
377 points
[2].x
= x
+ width
- thickness
;
378 points
[2].y
= y
+ thickness
;
380 points
[3].y
= y
+ thickness
;
381 XFillPolygon (dpy
, window
, top_gc
, points
, 4, Convex
, CoordModeOrigin
);
383 points
[0].y
= y
+ thickness
;
385 points
[1].y
= y
+ height
;
386 points
[2].x
= x
+ thickness
;
387 points
[2].y
= y
+ height
- thickness
;
388 points
[3].x
= x
+ thickness
;
389 points
[3].y
= y
+ thickness
;
390 XFillPolygon (dpy
, window
, top_gc
, points
, 4, Convex
, CoordModeOrigin
);
391 points
[0].x
= x
+ width
;
393 points
[1].x
= x
+ width
- thickness
;
394 points
[1].y
= y
+ thickness
;
395 points
[2].x
= x
+ width
- thickness
;
396 points
[2].y
= y
+ height
- thickness
;
397 points
[3].x
= x
+ width
;
398 points
[3].y
= y
+ height
- thickness
;
399 XFillPolygon (dpy
, window
, bottom_gc
, points
, 4, Convex
, CoordModeOrigin
);
401 points
[0].y
= y
+ height
;
402 points
[1].x
= x
+ width
;
403 points
[1].y
= y
+ height
;
404 points
[2].x
= x
+ width
;
405 points
[2].y
= y
+ height
- thickness
;
406 points
[3].x
= x
+ thickness
;
407 points
[3].y
= y
+ height
- thickness
;
408 XFillPolygon (dpy
, window
, bottom_gc
, points
, 4, Convex
, CoordModeOrigin
);
412 /* Display the menu item and increment where.x and where.y to show how large
413 ** the menu item was.
416 display_menu_item (XlwMenuWidget mw
, widget_value
* val
, window_state
* ws
,
417 XPoint
* where
, Boolean highlighted_p
, Boolean horizontal_p
,
418 Boolean just_compute_p
)
422 int font_ascent
= mw
->menu
.font
->ascent
;
423 int font_descent
= mw
->menu
.font
->descent
;
424 int shadow
= mw
->menu
.shadow_thickness
;
425 int separator_p
= all_dashes_p (val
->name
);
426 int h_spacing
= mw
->menu
.horizontal_spacing
;
427 int v_spacing
= mw
->menu
.vertical_spacing
;
434 /* compute the sizes of the item */
435 size_menu_item (mw
, val
, horizontal_p
, &label_width
, &rest_width
, &height
);
438 width
= label_width
+ rest_width
;
441 label_width
= ws
->label_width
;
442 width
= ws
->width
- 2 * shadow
;
445 /* see if it should be a button in the menubar */
446 button_p
= horizontal_p
&& val
->call_data
;
448 /* Only highlight an enabled item that has a callback. */
450 if (!val
->enabled
|| !(val
->call_data
|| val
->contents
))
453 /* do the drawing. */
456 /* Add the shadow border of the containing menu */
457 int x
= where
->x
+ shadow
;
458 int y
= where
->y
+ shadow
;
460 /* pick the foreground and background GC. */
462 text_gc
= button_p
? mw
->menu
.button_gc
: mw
->menu
.foreground_gc
;
465 button_p
? mw
->menu
.inactive_button_gc
: mw
->menu
.inactive_gc
;
466 deco_gc
= mw
->menu
.foreground_gc
;
470 XDrawLine (XtDisplay (mw
), ws
->window
, mw
->menu
.shadow_bottom_gc
,
472 XDrawLine (XtDisplay (mw
), ws
->window
, mw
->menu
.shadow_top_gc
,
473 x
, y
+ 1, x
+ width
, y
+ 1);
477 char* display_string
= resource_widget_value (mw
, val
);
478 draw_shadow_rectangle (mw
, ws
->window
, x
, y
, width
, height
, True
);
479 XDrawString (XtDisplay (mw
), ws
->window
, text_gc
,
480 x
+ h_spacing
+ shadow
,
481 y
+ v_spacing
+ shadow
+ font_ascent
,
482 display_string
, strlen (display_string
));
488 int a_w
= arrow_width (mw
);
489 draw_arrow (mw
, ws
->window
, deco_gc
,
490 x
+ label_width
+ mw
->menu
.arrow_spacing
,
491 y
+ v_spacing
+ shadow
, a_w
);
495 XDrawString (XtDisplay (mw
), ws
->window
, text_gc
,
496 x
+ label_width
+ mw
->menu
.arrow_spacing
,
497 y
+ v_spacing
+ shadow
+ font_ascent
,
498 val
->key
, strlen (val
->key
));
505 XDrawRectangle (XtDisplay (mw
), ws
->window
, deco_gc
,
506 x
+ shadow
, y
+ shadow
,
507 label_width
+ h_spacing
- 1,
508 font_ascent
+ font_descent
+ 2 * v_spacing
- 1);
510 highlighted_p
= True
;
515 draw_shadow_rectangle (mw
, ws
->window
, x
, y
, width
, height
, False
);
524 display_menu (XlwMenuWidget mw
, int level
, Boolean just_compute_p
,
525 XPoint
* highlighted_pos
, XPoint
* hit
, widget_value
** hit_return
,
526 widget_value
* this, widget_value
* that
)
529 widget_value
* following_item
;
532 int horizontal_p
= mw
->menu
.horizontal
&& (level
== 0);
534 int just_compute_this_one_p
;
536 if (level
>= mw
->menu
.old_depth
)
539 if (level
< mw
->menu
.old_depth
- 1)
540 following_item
= mw
->menu
.old_stack
[level
+ 1];
542 following_item
= NULL
;
550 ws
= &mw
->menu
.windows
[level
];
551 for (val
= mw
->menu
.old_stack
[level
]->contents
; val
; val
= val
->next
)
553 highlighted_p
= val
== following_item
;
554 if (highlighted_p
&& highlighted_pos
)
557 highlighted_pos
->x
= where
.x
;
559 highlighted_pos
->y
= where
.y
;
562 just_compute_this_one_p
=
563 just_compute_p
|| ((this || that
) && val
!= this && val
!= that
);
565 display_menu_item (mw
, val
, ws
, &where
, highlighted_p
, horizontal_p
,
566 just_compute_this_one_p
);
568 if (highlighted_p
&& highlighted_pos
)
571 highlighted_pos
->y
= where
.y
;
573 highlighted_pos
->x
= where
.x
;
578 && (horizontal_p
? hit
->x
< where
.x
: hit
->y
< where
.y
)
579 && !all_dashes_p (val
->name
))
589 draw_shadow_rectangle (mw
, ws
->window
, 0, 0, ws
->width
, ws
->height
, False
);
594 set_new_state (XlwMenuWidget mw
, widget_value
* val
, int level
)
598 mw
->menu
.new_depth
= 0;
599 for (i
= 0; i
< level
; i
++)
600 push_new_stack (mw
, mw
->menu
.old_stack
[i
]);
601 push_new_stack (mw
, val
);
605 make_windows_if_needed (XlwMenuWidget mw
, int n
)
609 XSetWindowAttributes xswa
;
611 Window root
= RootWindowOfScreen (DefaultScreenOfDisplay (XtDisplay (mw
)));
612 window_state
* windows
;
614 if (mw
->menu
.windows_length
>= n
)
617 xswa
.save_under
= True
;
618 xswa
.override_redirect
= True
;
619 xswa
.background_pixel
= mw
->core
.background_pixel
;
620 xswa
.border_pixel
= mw
->core
.border_pixel
;
622 ExposureMask
| ButtonMotionMask
| PointerMotionHintMask
623 | ButtonReleaseMask
| ButtonPressMask
;
624 xswa
.cursor
= mw
->menu
.cursor_shape
;
625 mask
= CWSaveUnder
| CWOverrideRedirect
| CWBackPixel
| CWBorderPixel
626 | CWEventMask
| CWCursor
;
628 if (!mw
->menu
.windows
)
631 (window_state
*)XtMalloc (n
* sizeof (window_state
));
637 (window_state
*)XtRealloc ((char*)mw
->menu
.windows
,
638 n
* sizeof (window_state
));
639 start_at
= mw
->menu
.windows_length
;
641 mw
->menu
.windows_length
= n
;
643 windows
= mw
->menu
.windows
;
645 for (i
= start_at
; i
< n
; i
++)
649 windows
[i
].width
= 1;
650 windows
[i
].height
= 1;
652 XCreateWindow (XtDisplay (mw
), root
, 0, 0, 1, 1,
653 0, 0, CopyFromParent
, CopyFromParent
, mask
, &xswa
);
657 /* Make the window fit in the screen */
659 fit_to_screen (XlwMenuWidget mw
, window_state
* ws
, window_state
* previous_ws
,
660 Boolean horizontal_p
)
662 int screen_width
= WidthOfScreen (XtScreen (mw
));
663 int screen_height
= HeightOfScreen (XtScreen (mw
));
667 else if (ws
->x
+ ws
->width
> screen_width
)
670 ws
->x
= previous_ws
->x
- ws
->width
;
672 ws
->x
= screen_width
- ws
->width
;
676 else if (ws
->y
+ ws
->height
> screen_height
)
679 ws
->y
= previous_ws
->y
- ws
->height
;
681 ws
->y
= screen_height
- ws
->height
;
685 /* Updates old_stack from new_stack and redisplays. */
687 remap_menubar (XlwMenuWidget mw
)
691 XPoint selection_position
;
692 int old_depth
= mw
->menu
.old_depth
;
693 int new_depth
= mw
->menu
.new_depth
;
694 widget_value
** old_stack
;
695 widget_value
** new_stack
;
696 window_state
* windows
;
697 widget_value
* old_selection
;
698 widget_value
* new_selection
;
700 /* Check that enough windows and old_stack are ready. */
701 make_windows_if_needed (mw
, new_depth
);
702 make_old_stack_space (mw
, new_depth
);
703 windows
= mw
->menu
.windows
;
704 old_stack
= mw
->menu
.old_stack
;
705 new_stack
= mw
->menu
.new_stack
;
707 /* compute the last identical different entry */
708 for (i
= 1; i
< old_depth
&& i
< new_depth
; i
++)
709 if (old_stack
[i
] != new_stack
[i
])
713 /* Memorize the previously selected item to be able to refresh it */
714 old_selection
= last_same
+ 1 < old_depth
? old_stack
[last_same
+ 1] : NULL
;
715 if (old_selection
&& !old_selection
->enabled
)
716 old_selection
= NULL
;
717 new_selection
= last_same
+ 1 < new_depth
? new_stack
[last_same
+ 1] : NULL
;
718 if (new_selection
&& !new_selection
->enabled
)
719 new_selection
= NULL
;
721 /* updates old_state from new_state. It has to be done now because
722 display_menu (called below) uses the old_stack to know what to display. */
723 for (i
= last_same
+ 1; i
< new_depth
; i
++)
724 old_stack
[i
] = new_stack
[i
];
725 mw
->menu
.old_depth
= new_depth
;
727 /* refresh the last seletion */
728 selection_position
.x
= 0;
729 selection_position
.y
= 0;
730 display_menu (mw
, last_same
, new_selection
== old_selection
,
731 &selection_position
, NULL
, NULL
, old_selection
, new_selection
);
733 /* Now popup the new menus */
734 for (i
= last_same
+ 1; i
< new_depth
&& new_stack
[i
]->contents
; i
++)
736 window_state
* previous_ws
= &windows
[i
- 1];
737 window_state
* ws
= &windows
[i
];
740 previous_ws
->x
+ selection_position
.x
+ mw
->menu
.shadow_thickness
;
741 if (!mw
->menu
.horizontal
|| i
> 1)
742 ws
->x
+= mw
->menu
.shadow_thickness
;
744 previous_ws
->y
+ selection_position
.y
+ mw
->menu
.shadow_thickness
;
748 fit_to_screen (mw
, ws
, previous_ws
, mw
->menu
.horizontal
&& i
== 1);
750 XClearWindow (XtDisplay (mw
), ws
->window
);
751 XMoveResizeWindow (XtDisplay (mw
), ws
->window
, ws
->x
, ws
->y
,
752 ws
->width
, ws
->height
);
753 XMapRaised (XtDisplay (mw
), ws
->window
);
754 display_menu (mw
, i
, False
, &selection_position
, NULL
, NULL
, NULL
, NULL
);
757 /* unmap the menus that popped down */
758 for (i
= new_depth
- 1; i
< old_depth
; i
++)
759 if (i
>= new_depth
|| !new_stack
[i
]->contents
)
760 XUnmapWindow (XtDisplay (mw
), windows
[i
].window
);
764 motion_event_is_in_menu (XlwMenuWidget mw
, XMotionEvent
* ev
, int level
,
765 XPoint
* relative_pos
)
767 window_state
* ws
= &mw
->menu
.windows
[level
];
768 int x
= level
== 0 ? ws
->x
: ws
->x
+ mw
->menu
.shadow_thickness
;
769 int y
= level
== 0 ? ws
->y
: ws
->y
+ mw
->menu
.shadow_thickness
;
770 relative_pos
->x
= ev
->x_root
- x
;
771 relative_pos
->y
= ev
->y_root
- y
;
772 return (x
< ev
->x_root
&& ev
->x_root
< x
+ ws
->width
773 && y
< ev
->y_root
&& ev
->y_root
< y
+ ws
->height
);
777 map_event_to_widget_value (XlwMenuWidget mw
, XMotionEvent
* ev
,
778 widget_value
** val
, int* level
)
786 /* Find the window */
787 for (i
= mw
->menu
.old_depth
- 1; i
>= 0; i
--)
789 ws
= &mw
->menu
.windows
[i
];
790 if (ws
&& motion_event_is_in_menu (mw
, ev
, i
, &relative_pos
))
792 display_menu (mw
, i
, True
, NULL
, &relative_pos
, val
, NULL
, NULL
);
806 make_drawing_gcs (XlwMenuWidget mw
)
810 xgcv
.font
= mw
->menu
.font
->fid
;
811 xgcv
.foreground
= mw
->menu
.foreground
;
812 xgcv
.background
= mw
->core
.background_pixel
;
813 mw
->menu
.foreground_gc
= XtGetGC ((Widget
)mw
,
814 GCFont
| GCForeground
| GCBackground
,
817 xgcv
.font
= mw
->menu
.font
->fid
;
818 xgcv
.foreground
= mw
->menu
.button_foreground
;
819 xgcv
.background
= mw
->core
.background_pixel
;
820 mw
->menu
.button_gc
= XtGetGC ((Widget
)mw
,
821 GCFont
| GCForeground
| GCBackground
,
824 xgcv
.font
= mw
->menu
.font
->fid
;
825 xgcv
.foreground
= mw
->menu
.foreground
;
826 xgcv
.background
= mw
->core
.background_pixel
;
827 xgcv
.fill_style
= FillStippled
;
828 xgcv
.stipple
= mw
->menu
.gray_pixmap
;
829 mw
->menu
.inactive_gc
= XtGetGC ((Widget
)mw
,
830 (GCFont
| GCForeground
| GCBackground
831 | GCFillStyle
| GCStipple
), &xgcv
);
833 xgcv
.font
= mw
->menu
.font
->fid
;
834 xgcv
.foreground
= mw
->menu
.button_foreground
;
835 xgcv
.background
= mw
->core
.background_pixel
;
836 xgcv
.fill_style
= FillStippled
;
837 xgcv
.stipple
= mw
->menu
.gray_pixmap
;
838 mw
->menu
.inactive_button_gc
= XtGetGC ((Widget
)mw
,
839 (GCFont
| GCForeground
| GCBackground
840 | GCFillStyle
| GCStipple
), &xgcv
);
842 xgcv
.font
= mw
->menu
.font
->fid
;
843 xgcv
.foreground
= mw
->core
.background_pixel
;
844 xgcv
.background
= mw
->menu
.foreground
;
845 mw
->menu
.background_gc
= XtGetGC ((Widget
)mw
,
846 GCFont
| GCForeground
| GCBackground
,
851 release_drawing_gcs (XlwMenuWidget mw
)
853 XtReleaseGC ((Widget
) mw
, mw
->menu
.foreground_gc
);
854 XtReleaseGC ((Widget
) mw
, mw
->menu
.button_gc
);
855 XtReleaseGC ((Widget
) mw
, mw
->menu
.inactive_gc
);
856 XtReleaseGC ((Widget
) mw
, mw
->menu
.inactive_button_gc
);
857 XtReleaseGC ((Widget
) mw
, mw
->menu
.background_gc
);
858 /* let's get some segvs if we try to use these... */
859 mw
->menu
.foreground_gc
= (GC
) -1;
860 mw
->menu
.button_gc
= (GC
) -1;
861 mw
->menu
.inactive_gc
= (GC
) -1;
862 mw
->menu
.inactive_button_gc
= (GC
) -1;
863 mw
->menu
.background_gc
= (GC
) -1;
866 #define MINL(x,y) ((((unsigned long) (x)) < ((unsigned long) (y))) \
867 ? ((unsigned long) (x)) : ((unsigned long) (y)))
870 make_shadow_gcs (XlwMenuWidget mw
)
873 unsigned long pm
= 0;
874 Display
*dpy
= XtDisplay ((Widget
) mw
);
875 Colormap cmap
= DefaultColormapOfScreen (XtScreen ((Widget
) mw
));
877 int top_frobbed
= 0, bottom_frobbed
= 0;
879 if (mw
->menu
.top_shadow_color
== -1)
880 mw
->menu
.top_shadow_color
= mw
->core
.background_pixel
;
881 if (mw
->menu
.bottom_shadow_color
== -1)
882 mw
->menu
.bottom_shadow_color
= mw
->menu
.foreground
;
884 if (mw
->menu
.top_shadow_color
== mw
->core
.background_pixel
||
885 mw
->menu
.top_shadow_color
== mw
->menu
.foreground
)
887 topc
.pixel
= mw
->core
.background_pixel
;
888 XQueryColor (dpy
, cmap
, &topc
);
889 /* don't overflow/wrap! */
890 topc
.red
= MINL (65535, topc
.red
* 1.2);
891 topc
.green
= MINL (65535, topc
.green
* 1.2);
892 topc
.blue
= MINL (65535, topc
.blue
* 1.2);
893 if (XAllocColor (dpy
, cmap
, &topc
))
895 mw
->menu
.top_shadow_color
= topc
.pixel
;
899 if (mw
->menu
.bottom_shadow_color
== mw
->menu
.foreground
||
900 mw
->menu
.bottom_shadow_color
== mw
->core
.background_pixel
)
902 botc
.pixel
= mw
->core
.background_pixel
;
903 XQueryColor (dpy
, cmap
, &botc
);
907 if (XAllocColor (dpy
, cmap
, &botc
))
909 mw
->menu
.bottom_shadow_color
= botc
.pixel
;
914 if (top_frobbed
&& bottom_frobbed
)
916 int top_avg
= ((topc
.red
/ 3) + (topc
.green
/ 3) + (topc
.blue
/ 3));
917 int bot_avg
= ((botc
.red
/ 3) + (botc
.green
/ 3) + (botc
.blue
/ 3));
918 if (bot_avg
> top_avg
)
920 Pixel tmp
= mw
->menu
.top_shadow_color
;
921 mw
->menu
.top_shadow_color
= mw
->menu
.bottom_shadow_color
;
922 mw
->menu
.bottom_shadow_color
= tmp
;
924 else if (topc
.pixel
== botc
.pixel
)
926 if (botc
.pixel
== mw
->menu
.foreground
)
927 mw
->menu
.top_shadow_color
= mw
->core
.background_pixel
;
929 mw
->menu
.bottom_shadow_color
= mw
->menu
.foreground
;
933 if (!mw
->menu
.top_shadow_pixmap
&&
934 mw
->menu
.top_shadow_color
== mw
->core
.background_pixel
)
936 mw
->menu
.top_shadow_pixmap
= mw
->menu
.gray_pixmap
;
937 mw
->menu
.top_shadow_color
= mw
->menu
.foreground
;
939 if (!mw
->menu
.bottom_shadow_pixmap
&&
940 mw
->menu
.bottom_shadow_color
== mw
->core
.background_pixel
)
942 mw
->menu
.bottom_shadow_pixmap
= mw
->menu
.gray_pixmap
;
943 mw
->menu
.bottom_shadow_color
= mw
->menu
.foreground
;
946 xgcv
.fill_style
= FillStippled
;
947 xgcv
.foreground
= mw
->menu
.top_shadow_color
;
948 xgcv
.stipple
= mw
->menu
.top_shadow_pixmap
;
949 pm
= (xgcv
.stipple
? GCStipple
|GCFillStyle
: 0);
950 mw
->menu
.shadow_top_gc
= XtGetGC ((Widget
)mw
, GCForeground
| pm
, &xgcv
);
952 xgcv
.foreground
= mw
->menu
.bottom_shadow_color
;
953 xgcv
.stipple
= mw
->menu
.bottom_shadow_pixmap
;
954 pm
= (xgcv
.stipple
? GCStipple
|GCFillStyle
: 0);
955 mw
->menu
.shadow_bottom_gc
= XtGetGC ((Widget
)mw
, GCForeground
| pm
, &xgcv
);
960 release_shadow_gcs (XlwMenuWidget mw
)
962 XtReleaseGC ((Widget
) mw
, mw
->menu
.shadow_top_gc
);
963 XtReleaseGC ((Widget
) mw
, mw
->menu
.shadow_bottom_gc
);
967 XlwMenuInitialize (Widget request
, Widget
new, ArgList args
,
970 /* Get the GCs and the widget size */
971 XlwMenuWidget mw
= (XlwMenuWidget
)new;
973 XSetWindowAttributes xswa
;
976 Window window
= RootWindowOfScreen (DefaultScreenOfDisplay (XtDisplay (mw
)));
977 Display
* display
= XtDisplay (mw
);
979 /* mw->menu.cursor = XCreateFontCursor (display, mw->menu.cursor_shape); */
980 mw
->menu
.cursor
= mw
->menu
.cursor_shape
;
982 mw
->menu
.gray_pixmap
= XCreatePixmapFromBitmapData (display
, window
,
983 gray_bits
, gray_width
,
984 gray_height
, 1, 0, 1);
986 make_drawing_gcs (mw
);
987 make_shadow_gcs (mw
);
989 xswa
.background_pixel
= mw
->core
.background_pixel
;
990 xswa
.border_pixel
= mw
->core
.border_pixel
;
991 mask
= CWBackPixel
| CWBorderPixel
;
993 mw
->menu
.popped_up
= False
;
995 mw
->menu
.old_depth
= 1;
996 mw
->menu
.old_stack
= (widget_value
**)XtMalloc (sizeof (widget_value
*));
997 mw
->menu
.old_stack_length
= 1;
998 mw
->menu
.old_stack
[0] = mw
->menu
.contents
;
1000 mw
->menu
.new_depth
= 0;
1001 mw
->menu
.new_stack
= 0;
1002 mw
->menu
.new_stack_length
= 0;
1003 push_new_stack (mw
, mw
->menu
.contents
);
1005 mw
->menu
.windows
= (window_state
*)XtMalloc (sizeof (window_state
));
1006 mw
->menu
.windows_length
= 1;
1007 mw
->menu
.windows
[0].x
= 0;
1008 mw
->menu
.windows
[0].y
= 0;
1009 mw
->menu
.windows
[0].width
= 0;
1010 mw
->menu
.windows
[0].height
= 0;
1013 mw
->core
.width
= mw
->menu
.windows
[0].width
;
1014 mw
->core
.height
= mw
->menu
.windows
[0].height
;
1018 XlwMenuClassInitialize ()
1023 XlwMenuRealize (Widget w
, Mask
*valueMask
, XSetWindowAttributes
*attributes
)
1025 XlwMenuWidget mw
= (XlwMenuWidget
)w
;
1026 XSetWindowAttributes xswa
;
1029 (*xlwMenuWidgetClass
->core_class
.superclass
->core_class
.realize
)
1030 (w
, valueMask
, attributes
);
1032 xswa
.save_under
= True
;
1033 xswa
.cursor
= mw
->menu
.cursor_shape
;
1034 mask
= CWSaveUnder
| CWCursor
;
1035 XChangeWindowAttributes (XtDisplay (w
), XtWindow (w
), mask
, &xswa
);
1037 mw
->menu
.windows
[0].window
= XtWindow (w
);
1038 mw
->menu
.windows
[0].x
= w
->core
.x
;
1039 mw
->menu
.windows
[0].y
= w
->core
.y
;
1040 mw
->menu
.windows
[0].width
= w
->core
.width
;
1041 mw
->menu
.windows
[0].height
= w
->core
.height
;
1044 /* Only the toplevel menubar/popup is a widget so it's the only one that
1045 receives expose events through Xt. So we repaint all the other panes
1046 when receiving an Expose event. */
1048 XlwMenuRedisplay (Widget w
, XEvent
* ev
, Region region
)
1050 XlwMenuWidget mw
= (XlwMenuWidget
)w
;
1053 for (i
= 0; i
< mw
->menu
.old_depth
; i
++)
1054 display_menu (mw
, i
, False
, NULL
, NULL
, NULL
, NULL
, NULL
);
1058 XlwMenuDestroy (Widget w
)
1061 XlwMenuWidget mw
= (XlwMenuWidget
) w
;
1063 release_drawing_gcs (mw
);
1064 release_shadow_gcs (mw
);
1066 /* this doesn't come from the resource db but is created explicitly
1067 so we must free it ourselves. */
1068 XFreePixmap (XtDisplay (mw
), mw
->menu
.gray_pixmap
);
1069 mw
->menu
.gray_pixmap
= (Pixmap
) -1;
1071 /* Don't free mw->menu.contents because that comes from our creator.
1072 The `*_stack' elements are just pointers into `contents' so leave
1073 that alone too. But free the stacks themselves. */
1074 if (mw
->menu
.old_stack
) XtFree ((char *) mw
->menu
.old_stack
);
1075 if (mw
->menu
.new_stack
) XtFree ((char *) mw
->menu
.new_stack
);
1077 /* Remember, you can't free anything that came from the resource
1078 database. This includes:
1080 mw->menu.top_shadow_pixmap
1081 mw->menu.bottom_shadow_pixmap
1083 Also the color cells of top_shadow_color, bottom_shadow_color,
1084 foreground, and button_foreground will never be freed until this
1085 client exits. Nice, eh?
1088 /* start from 1 because the one in slot 0 is w->core.window */
1089 for (i
= 1; i
< mw
->menu
.windows_length
; i
++)
1090 XDestroyWindow (XtDisplay (mw
), mw
->menu
.windows
[i
].window
);
1091 if (mw
->menu
.windows
)
1092 XtFree ((char *) mw
->menu
.windows
);
1096 XlwMenuSetValues (Widget current
, Widget request
, Widget
new)
1098 XlwMenuWidget oldmw
= (XlwMenuWidget
)current
;
1099 XlwMenuWidget newmw
= (XlwMenuWidget
)new;
1100 Boolean redisplay
= False
;
1103 if (newmw
->menu
.contents
1104 && newmw
->menu
.contents
->contents
1105 && newmw
->menu
.contents
->contents
->change
>= VISIBLE_CHANGE
)
1108 if (newmw
->core
.background_pixel
!= oldmw
->core
.background_pixel
1109 || newmw
->menu
.foreground
!= oldmw
->menu
.foreground
)
1111 release_drawing_gcs (newmw
);
1112 make_drawing_gcs (newmw
);
1115 for (i
= 0; i
< oldmw
->menu
.windows_length
; i
++)
1117 XSetWindowBackground (XtDisplay (oldmw
),
1118 oldmw
->menu
.windows
[i
].window
,
1119 newmw
->core
.background_pixel
);
1120 /* clear windows and generate expose events */
1121 XClearArea (XtDisplay (oldmw
), oldmw
->menu
.windows
[i
].window
,
1130 XlwMenuResize (Widget w
)
1132 XlwMenuWidget mw
= (XlwMenuWidget
)w
;
1134 mw
->menu
.windows
[0].width
= mw
->core
.width
;
1135 mw
->menu
.windows
[0].height
= mw
->core
.height
;
1138 \f/* Action procedures */
1140 handle_single_motion_event (XlwMenuWidget mw
, XMotionEvent
* ev
)
1145 if (!map_event_to_widget_value (mw
, ev
, &val
, &level
))
1146 pop_new_stack_if_no_contents (mw
);
1148 set_new_state (mw
, val
, level
);
1151 /* Sync with the display. Makes it feel better on X terms. */
1152 XSync (XtDisplay (mw
), False
);
1156 handle_motion_event (XlwMenuWidget mw
, XMotionEvent
* ev
)
1160 int state
= ev
->state
;
1162 handle_single_motion_event (mw
, ev
);
1164 /* allow motion events to be generated again */
1166 && XQueryPointer (XtDisplay (mw
), ev
->window
,
1167 &ev
->root
, &ev
->subwindow
,
1168 &ev
->x_root
, &ev
->y_root
,
1171 && ev
->state
== state
1172 && (ev
->x_root
!= x
|| ev
->y_root
!= y
))
1173 handle_single_motion_event (mw
, ev
);
1177 Start (Widget w
, XEvent
*ev
, String
*params
, Cardinal
*num_params
)
1179 XlwMenuWidget mw
= (XlwMenuWidget
)w
;
1181 XtCallCallbackList ((Widget
)mw
, mw
->menu
.open
, NULL
);
1183 /* notes the absolute position of the menubar window */
1184 mw
->menu
.windows
[0].x
= ev
->xmotion
.x_root
- ev
->xmotion
.x
;
1185 mw
->menu
.windows
[0].y
= ev
->xmotion
.y_root
- ev
->xmotion
.y
;
1187 /* handles the down like a move, slots are compatible */
1188 handle_motion_event (mw
, &ev
->xmotion
);
1192 Drag (Widget w
, XEvent
*ev
, String
*params
, Cardinal
*num_params
)
1194 XlwMenuWidget mw
= (XlwMenuWidget
)w
;
1195 handle_motion_event (mw
, &ev
->xmotion
);
1199 Select (Widget w
, XEvent
*ev
, String
*params
, Cardinal
*num_params
)
1201 XlwMenuWidget mw
= (XlwMenuWidget
)w
;
1202 widget_value
* selected_item
= mw
->menu
.old_stack
[mw
->menu
.old_depth
- 1];
1204 /* pop down everything */
1205 mw
->menu
.new_depth
= 1;
1208 if (mw
->menu
.popped_up
)
1210 mw
->menu
.popped_up
= False
;
1211 XtUngrabPointer ((Widget
)mw
, ev
->xmotion
.time
);
1212 XtPopdown (XtParent (mw
));
1216 XtCallCallbackList ((Widget
)mw
, mw
->menu
.select
, (XtPointer
)selected_item
);
1221 \f/* Special code to pop-up a menu */
1223 pop_up_menu (XlwMenuWidget mw
, XButtonPressedEvent
* event
)
1225 int x
= event
->x_root
;
1226 int y
= event
->y_root
;
1229 int borderwidth
= mw
->menu
.shadow_thickness
;
1230 Screen
* screen
= XtScreen (mw
);
1232 XtCallCallbackList ((Widget
)mw
, mw
->menu
.open
, NULL
);
1236 w
= mw
->menu
.windows
[0].width
;
1237 h
= mw
->menu
.windows
[0].height
;
1241 if (x
< borderwidth
)
1243 if (x
+ w
+ 2 * borderwidth
> WidthOfScreen (screen
))
1244 x
= WidthOfScreen (screen
) - w
- 2 * borderwidth
;
1245 if (y
< borderwidth
)
1247 if (y
+ h
+ 2 * borderwidth
> HeightOfScreen (screen
))
1248 y
= HeightOfScreen (screen
) - h
- 2 * borderwidth
;
1250 mw
->menu
.popped_up
= True
;
1251 XtConfigureWidget (XtParent (mw
), x
, y
, w
, h
,
1252 XtParent (mw
)->core
.border_width
);
1253 XtPopup (XtParent (mw
), XtGrabExclusive
);
1254 display_menu (mw
, 0, False
, NULL
, NULL
, NULL
, NULL
, NULL
);
1255 XtGrabPointer ((Widget
)mw
, False
,
1256 (ButtonMotionMask
| PointerMotionHintMask
| ButtonReleaseMask
1258 GrabModeAsync
, GrabModeAsync
, None
, mw
->menu
.cursor_shape
,
1261 mw
->menu
.windows
[0].x
= x
+ borderwidth
;
1262 mw
->menu
.windows
[0].y
= y
+ borderwidth
;
1264 handle_motion_event (mw
, (XMotionEvent
*)event
);