Commit | Line | Data |
---|---|---|
5c520e0a | 1 | /* Implements a lightweight menubar widget. |
07bf635f | 2 | Copyright (C) 1992 Lucid, Inc. |
a7e19a26 | 3 | Copyright (C) 2002 Free Software Foundation, Inc. |
07bf635f RS |
4 | |
5 | This file is part of the Lucid Widget Library. | |
6 | ||
5c520e0a | 7 | The Lucid Widget Library is free software; you can redistribute it and/or |
07bf635f RS |
8 | modify it under the terms of the GNU General Public License as published by |
9 | the Free Software Foundation; either version 2, or (at your option) | |
10 | any later version. | |
11 | ||
12 | The Lucid Widget Library is distributed in the hope that it will be useful, | |
5c520e0a | 13 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
07bf635f RS |
14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
15 | GNU General Public License for more details. | |
16 | ||
17 | You should have received a copy of the GNU General Public License | |
1fb87c77 KH |
18 | along with GNU Emacs; see the file COPYING. If not, write to the |
19 | Free Software Foundation, Inc., 59 Temple Place - Suite 330, | |
20 | Boston, MA 02111-1307, USA. */ | |
07bf635f RS |
21 | |
22 | /* Created by devin@lucid.com */ | |
23 | ||
0f0912e6 PE |
24 | #ifdef HAVE_CONFIG_H |
25 | #include <config.h> | |
26 | #endif | |
27 | ||
2f96293d RS |
28 | #include "../src/lisp.h" |
29 | ||
07bf635f RS |
30 | #include <stdio.h> |
31 | ||
32 | #include <sys/types.h> | |
5c520e0a SS |
33 | #if (defined __sun) && !(defined SUNOS41) |
34 | #define SUNOS41 | |
07bf635f | 35 | #include <X11/Xos.h> |
5c520e0a SS |
36 | #undef SUNOS41 |
37 | #else | |
38 | #include <X11/Xos.h> | |
39 | #endif | |
07bf635f | 40 | #include <X11/IntrinsicP.h> |
2289f3f4 | 41 | #include <X11/ObjectP.h> |
07bf635f RS |
42 | #include <X11/StringDefs.h> |
43 | #include <X11/cursorfont.h> | |
07bf635f | 44 | #include "xlwmenuP.h" |
fc8fefed | 45 | |
a57a1605 | 46 | #ifdef emacs |
f1c16db4 GM |
47 | |
48 | /* Defined in xfns.c. When config.h defines `static' as empty, we get | |
49 | redefinition errors when gray_bitmap is included more than once, so | |
50 | we're referring to the one include in xfns.c here. */ | |
51 | ||
52 | extern int gray_bitmap_width; | |
53 | extern int gray_bitmap_height; | |
5916243b | 54 | extern char *gray_bitmap_bits; |
f1c16db4 | 55 | |
a57a1605 DL |
56 | /* Defined in xterm.c. */ |
57 | extern int x_alloc_nearest_color_for_widget __P ((Widget, Colormap, XColor*)); | |
65c0ae05 MB |
58 | extern int x_alloc_lighter_color_for_widget __P ((Widget, Display*, Colormap, |
59 | unsigned long *, | |
60 | double, int)); | |
a57a1605 DL |
61 | extern int x_catch_errors __P ((Display*)); |
62 | extern int x_uncatch_errors __P ((Display*, int)); | |
63 | extern int x_had_errors_p __P ((Display*)); | |
64 | extern int x_clear_errors __P ((Display*)); | |
fc8fefed GM |
65 | extern unsigned long x_copy_dpy_color __P ((Display *, Colormap, |
66 | unsigned long)); | |
67 | ||
68 | /* Defined in xfaces.c. */ | |
69 | extern void x_free_dpy_colors __P ((Display *, Screen *, Colormap, | |
70 | unsigned long *pixels, int npixels)); | |
f1c16db4 GM |
71 | #else /* not emacs */ |
72 | ||
73 | #include <X11/bitmaps/gray> | |
74 | #define gray_bitmap_width gray_width | |
75 | #define gray_bitmap_height gray_height | |
76 | #define gray_bitmap_bits gray_bits | |
77 | ||
78 | #endif /* not emacs */ | |
d398028f PR |
79 | |
80 | static int pointer_grabbed; | |
81 | static XEvent menu_post_event; | |
07bf635f | 82 | |
47d52240 RS |
83 | XFontStruct *xlwmenu_default_font; |
84 | ||
5c520e0a SS |
85 | static char |
86 | xlwMenuTranslations [] = | |
ba624d0f RS |
87 | "<BtnDown>: start()\n\ |
88 | <Motion>: drag()\n\ | |
89 | <BtnUp>: select()\n\ | |
90 | <Key>Shift_L: nothing()\n\ | |
91 | <Key>Shift_R: nothing()\n\ | |
92 | <Key>Meta_L: nothing()\n\ | |
93 | <Key>Meta_R: nothing()\n\ | |
94 | <Key>Control_L: nothing()\n\ | |
95 | <Key>Control_R: nothing()\n\ | |
96 | <Key>Hyper_L: nothing()\n\ | |
97 | <Key>Hyper_R: nothing()\n\ | |
98 | <Key>Super_L: nothing()\n\ | |
99 | <Key>Super_R: nothing()\n\ | |
100 | <Key>Alt_L: nothing()\n\ | |
101 | <Key>Alt_R: nothing()\n\ | |
102 | <Key>Caps_Lock: nothing()\n\ | |
103 | <Key>Shift_Lock: nothing()\n\ | |
104 | <KeyUp>Shift_L: nothing()\n\ | |
105 | <KeyUp>Shift_R: nothing()\n\ | |
106 | <KeyUp>Meta_L: nothing()\n\ | |
107 | <KeyUp>Meta_R: nothing()\n\ | |
108 | <KeyUp>Control_L: nothing()\n\ | |
109 | <KeyUp>Control_R: nothing()\n\ | |
110 | <KeyUp>Hyper_L: nothing()\n\ | |
111 | <KeyUp>Hyper_R: nothing()\n\ | |
112 | <KeyUp>Super_L: nothing()\n\ | |
113 | <KeyUp>Super_R: nothing()\n\ | |
114 | <KeyUp>Alt_L: nothing()\n\ | |
115 | <KeyUp>Alt_R: nothing()\n\ | |
116 | <KeyUp>Caps_Lock: nothing()\n\ | |
117 | <KeyUp>Shift_Lock:nothing()\n\ | |
a7e19a26 PJ |
118 | <Key>Return: select()\n\ |
119 | <Key>Down: down()\n\ | |
120 | <Key>Up: up()\n\ | |
121 | <Key>Left: left()\n\ | |
122 | <Key>Right: right()\n\ | |
ba624d0f RS |
123 | <Key>: key()\n\ |
124 | <KeyUp>: key()\n\ | |
07bf635f RS |
125 | "; |
126 | ||
a7e19a26 PJ |
127 | /* FIXME: Space should toggle toggleable menu item but not remove the menu |
128 | so you can toggle the next one without entering the menu again. */ | |
129 | ||
130 | /* FIXME: Should ESC close one level of menu structure or the complete menu? */ | |
131 | ||
2376c841 | 132 | /* FIXME: F10 should enter the menu, the first one in the menu-bar. */ |
a7e19a26 | 133 | |
07bf635f | 134 | #define offset(field) XtOffset(XlwMenuWidget, field) |
5c520e0a | 135 | static XtResource |
07bf635f | 136 | xlwMenuResources[] = |
5c520e0a | 137 | { |
07bf635f RS |
138 | {XtNfont, XtCFont, XtRFontStruct, sizeof(XFontStruct *), |
139 | offset(menu.font),XtRString, "XtDefaultFont"}, | |
140 | {XtNforeground, XtCForeground, XtRPixel, sizeof(Pixel), | |
141 | offset(menu.foreground), XtRString, "XtDefaultForeground"}, | |
8b71a9ca PJ |
142 | {XtNdisabledForeground, XtCDisabledForeground, XtRPixel, sizeof(Pixel), |
143 | offset(menu.disabled_foreground), XtRString, (XtPointer)NULL}, | |
07bf635f RS |
144 | {XtNbuttonForeground, XtCButtonForeground, XtRPixel, sizeof(Pixel), |
145 | offset(menu.button_foreground), XtRString, "XtDefaultForeground"}, | |
146 | {XtNmargin, XtCMargin, XtRDimension, sizeof(Dimension), | |
2376c841 | 147 | offset(menu.margin), XtRImmediate, (XtPointer)1}, |
07bf635f RS |
148 | {XtNhorizontalSpacing, XtCMargin, XtRDimension, sizeof(Dimension), |
149 | offset(menu.horizontal_spacing), XtRImmediate, (XtPointer)3}, | |
150 | {XtNverticalSpacing, XtCMargin, XtRDimension, sizeof(Dimension), | |
2376c841 | 151 | offset(menu.vertical_spacing), XtRImmediate, (XtPointer)2}, |
07bf635f RS |
152 | {XtNarrowSpacing, XtCMargin, XtRDimension, sizeof(Dimension), |
153 | offset(menu.arrow_spacing), XtRImmediate, (XtPointer)10}, | |
154 | ||
d398028f | 155 | {XmNshadowThickness, XmCShadowThickness, XtRDimension, |
07bf635f | 156 | sizeof (Dimension), offset (menu.shadow_thickness), |
2376c841 | 157 | XtRImmediate, (XtPointer)1}, |
07bf635f RS |
158 | {XmNtopShadowColor, XmCTopShadowColor, XtRPixel, sizeof (Pixel), |
159 | offset (menu.top_shadow_color), XtRImmediate, (XtPointer)-1}, | |
160 | {XmNbottomShadowColor, XmCBottomShadowColor, XtRPixel, sizeof (Pixel), | |
161 | offset (menu.bottom_shadow_color), XtRImmediate, (XtPointer)-1}, | |
162 | {XmNtopShadowPixmap, XmCTopShadowPixmap, XtRPixmap, sizeof (Pixmap), | |
163 | offset (menu.top_shadow_pixmap), XtRImmediate, (XtPointer)None}, | |
164 | {XmNbottomShadowPixmap, XmCBottomShadowPixmap, XtRPixmap, sizeof (Pixmap), | |
165 | offset (menu.bottom_shadow_pixmap), XtRImmediate, (XtPointer)None}, | |
166 | ||
5c520e0a | 167 | {XtNopen, XtCCallback, XtRCallback, sizeof(XtPointer), |
07bf635f | 168 | offset(menu.open), XtRCallback, (XtPointer)NULL}, |
5c520e0a | 169 | {XtNselect, XtCCallback, XtRCallback, sizeof(XtPointer), |
07bf635f | 170 | offset(menu.select), XtRCallback, (XtPointer)NULL}, |
5c520e0a | 171 | {XtNhighlightCallback, XtCCallback, XtRCallback, sizeof(XtPointer), |
d6fd6371 | 172 | offset(menu.highlight), XtRCallback, (XtPointer)NULL}, |
07bf635f RS |
173 | {XtNmenu, XtCMenu, XtRPointer, sizeof(XtPointer), |
174 | offset(menu.contents), XtRImmediate, (XtPointer)NULL}, | |
175 | {XtNcursor, XtCCursor, XtRCursor, sizeof(Cursor), | |
176 | offset(menu.cursor_shape), XtRString, (XtPointer)"right_ptr"}, | |
177 | {XtNhorizontal, XtCHorizontal, XtRInt, sizeof(int), | |
178 | offset(menu.horizontal), XtRImmediate, (XtPointer)True}, | |
179 | }; | |
180 | #undef offset | |
181 | ||
182 | static Boolean XlwMenuSetValues(); | |
183 | static void XlwMenuRealize(); | |
184 | static void XlwMenuRedisplay(); | |
185 | static void XlwMenuResize(); | |
186 | static void XlwMenuInitialize(); | |
187 | static void XlwMenuRedisplay(); | |
188 | static void XlwMenuDestroy(); | |
189 | static void XlwMenuClassInitialize(); | |
190 | static void Start(); | |
191 | static void Drag(); | |
a7e19a26 PJ |
192 | static void Down(); |
193 | static void Up(); | |
194 | static void Left(); | |
195 | static void Right(); | |
07bf635f | 196 | static void Select(); |
ba624d0f RS |
197 | static void Key(); |
198 | static void Nothing(); | |
be06a3df | 199 | static int separator_height (); |
07bf635f | 200 | |
5c520e0a | 201 | static XtActionsRec |
07bf635f RS |
202 | xlwMenuActionsList [] = |
203 | { | |
204 | {"start", Start}, | |
205 | {"drag", Drag}, | |
a7e19a26 PJ |
206 | {"down", Down}, |
207 | {"up", Up}, | |
208 | {"left", Left}, | |
209 | {"right", Right}, | |
07bf635f | 210 | {"select", Select}, |
ba624d0f RS |
211 | {"key", Key}, |
212 | {"nothing", Nothing}, | |
07bf635f RS |
213 | }; |
214 | ||
215 | #define SuperClass ((CoreWidgetClass)&coreClassRec) | |
216 | ||
217 | XlwMenuClassRec xlwMenuClassRec = | |
218 | { | |
219 | { /* CoreClass fields initialization */ | |
5c520e0a | 220 | (WidgetClass) SuperClass, /* superclass */ |
07bf635f RS |
221 | "XlwMenu", /* class_name */ |
222 | sizeof(XlwMenuRec), /* size */ | |
223 | XlwMenuClassInitialize, /* class_initialize */ | |
224 | NULL, /* class_part_initialize */ | |
225 | FALSE, /* class_inited */ | |
226 | XlwMenuInitialize, /* initialize */ | |
227 | NULL, /* initialize_hook */ | |
228 | XlwMenuRealize, /* realize */ | |
229 | xlwMenuActionsList, /* actions */ | |
230 | XtNumber(xlwMenuActionsList), /* num_actions */ | |
231 | xlwMenuResources, /* resources */ | |
232 | XtNumber(xlwMenuResources), /* resource_count */ | |
233 | NULLQUARK, /* xrm_class */ | |
234 | TRUE, /* compress_motion */ | |
235 | TRUE, /* compress_exposure */ | |
236 | TRUE, /* compress_enterleave */ | |
237 | FALSE, /* visible_interest */ | |
238 | XlwMenuDestroy, /* destroy */ | |
239 | XlwMenuResize, /* resize */ | |
240 | XlwMenuRedisplay, /* expose */ | |
241 | XlwMenuSetValues, /* set_values */ | |
242 | NULL, /* set_values_hook */ | |
243 | XtInheritSetValuesAlmost, /* set_values_almost */ | |
244 | NULL, /* get_values_hook */ | |
245 | NULL, /* accept_focus */ | |
246 | XtVersion, /* version */ | |
247 | NULL, /* callback_private */ | |
248 | xlwMenuTranslations, /* tm_table */ | |
249 | XtInheritQueryGeometry, /* query_geometry */ | |
250 | XtInheritDisplayAccelerator, /* display_accelerator */ | |
251 | NULL /* extension */ | |
252 | }, /* XlwMenuClass fields initialization */ | |
253 | { | |
254 | 0 /* dummy */ | |
255 | }, | |
256 | }; | |
257 | ||
258 | WidgetClass xlwMenuWidgetClass = (WidgetClass) &xlwMenuClassRec; | |
259 | ||
d398028f PR |
260 | int submenu_destroyed; |
261 | ||
005e0d57 JD |
262 | /* For debug, if installation-directory is non-nil this is not an installed |
263 | Emacs. In that case we do not grab the keyboard to make it easier to | |
264 | debug. */ | |
265 | #define GRAB_KEYBOARD (EQ (Vinstallation_directory, Qnil)) | |
4db7db7d | 266 | |
d398028f PR |
267 | static int next_release_must_exit; |
268 | ||
07bf635f | 269 | \f/* Utilities */ |
be06a3df | 270 | |
4db7db7d JD |
271 | /* Ungrab pointer and keyboard */ |
272 | static void | |
273 | ungrab_all (w, ungrabtime) | |
274 | Widget w; | |
275 | Time ungrabtime; | |
276 | { | |
277 | XtUngrabPointer (w, ungrabtime); | |
005e0d57 | 278 | if (GRAB_KEYBOARD) XtUngrabKeyboard (w, ungrabtime); |
4db7db7d | 279 | } |
be06a3df GM |
280 | |
281 | /* Like abort, but remove grabs from widget W before. */ | |
282 | ||
283 | static void | |
284 | abort_gracefully (w) | |
285 | Widget w; | |
286 | { | |
287 | if (XtIsShell (XtParent (w))) | |
288 | XtRemoveGrab (w); | |
4db7db7d | 289 | ungrab_all (w, CurrentTime); |
be06a3df GM |
290 | abort (); |
291 | } | |
292 | ||
07bf635f | 293 | static void |
d398028f PR |
294 | push_new_stack (mw, val) |
295 | XlwMenuWidget mw; | |
296 | widget_value* val; | |
07bf635f RS |
297 | { |
298 | if (!mw->menu.new_stack) | |
299 | { | |
300 | mw->menu.new_stack_length = 10; | |
301 | mw->menu.new_stack = | |
302 | (widget_value**)XtCalloc (mw->menu.new_stack_length, | |
303 | sizeof (widget_value*)); | |
304 | } | |
305 | else if (mw->menu.new_depth == mw->menu.new_stack_length) | |
306 | { | |
307 | mw->menu.new_stack_length *= 2; | |
308 | mw->menu.new_stack = | |
309 | (widget_value**)XtRealloc ((char*)mw->menu.new_stack, | |
310 | mw->menu.new_stack_length * sizeof (widget_value*)); | |
311 | } | |
312 | mw->menu.new_stack [mw->menu.new_depth++] = val; | |
313 | } | |
314 | ||
315 | static void | |
d398028f PR |
316 | pop_new_stack_if_no_contents (mw) |
317 | XlwMenuWidget mw; | |
07bf635f RS |
318 | { |
319 | if (mw->menu.new_depth) | |
320 | { | |
321 | if (!mw->menu.new_stack [mw->menu.new_depth - 1]->contents) | |
322 | mw->menu.new_depth -= 1; | |
323 | } | |
324 | } | |
325 | ||
326 | static void | |
d398028f PR |
327 | make_old_stack_space (mw, n) |
328 | XlwMenuWidget mw; | |
329 | int n; | |
07bf635f RS |
330 | { |
331 | if (!mw->menu.old_stack) | |
332 | { | |
333 | mw->menu.old_stack_length = 10; | |
334 | mw->menu.old_stack = | |
335 | (widget_value**)XtCalloc (mw->menu.old_stack_length, | |
336 | sizeof (widget_value*)); | |
337 | } | |
338 | else if (mw->menu.old_stack_length < n) | |
339 | { | |
340 | mw->menu.old_stack_length *= 2; | |
341 | mw->menu.old_stack = | |
342 | (widget_value**)XtRealloc ((char*)mw->menu.old_stack, | |
343 | mw->menu.old_stack_length * sizeof (widget_value*)); | |
344 | } | |
345 | } | |
346 | ||
347 | \f/* Size code */ | |
d398028f PR |
348 | int |
349 | string_width (mw, s) | |
350 | XlwMenuWidget mw; | |
351 | char *s; | |
07bf635f RS |
352 | { |
353 | XCharStruct xcs; | |
354 | int drop; | |
5c520e0a | 355 | |
07bf635f RS |
356 | XTextExtents (mw->menu.font, s, strlen (s), &drop, &drop, &drop, &xcs); |
357 | return xcs.width; | |
358 | } | |
359 | ||
360 | static int | |
d398028f PR |
361 | arrow_width (mw) |
362 | XlwMenuWidget mw; | |
07bf635f | 363 | { |
be06a3df | 364 | return (mw->menu.font->ascent * 3/4) | 1; |
07bf635f RS |
365 | } |
366 | ||
be06a3df GM |
367 | /* Return the width of toggle buttons of widget MW. */ |
368 | ||
369 | static int | |
370 | toggle_button_width (mw) | |
371 | XlwMenuWidget mw; | |
372 | { | |
373 | return ((mw->menu.font->ascent + mw->menu.font->descent) * 2 / 3) | 1; | |
374 | } | |
375 | ||
376 | ||
377 | /* Return the width of radio buttons of widget MW. */ | |
378 | ||
379 | static int | |
380 | radio_button_width (mw) | |
381 | XlwMenuWidget mw; | |
382 | { | |
383 | return toggle_button_width (mw) * 1.41; | |
384 | } | |
385 | ||
386 | ||
07bf635f RS |
387 | static XtResource |
388 | nameResource[] = | |
5c520e0a | 389 | { |
07bf635f RS |
390 | {"labelString", "LabelString", XtRString, sizeof(String), |
391 | 0, XtRImmediate, 0}, | |
392 | }; | |
393 | ||
394 | static char* | |
d398028f PR |
395 | resource_widget_value (mw, val) |
396 | XlwMenuWidget mw; | |
397 | widget_value *val; | |
07bf635f RS |
398 | { |
399 | if (!val->toolkit_data) | |
400 | { | |
401 | char* resourced_name = NULL; | |
402 | char* complete_name; | |
403 | XtGetSubresources ((Widget) mw, | |
404 | (XtPointer) &resourced_name, | |
405 | val->name, val->name, | |
406 | nameResource, 1, NULL, 0); | |
407 | if (!resourced_name) | |
408 | resourced_name = val->name; | |
409 | if (!val->value) | |
d97342f3 RS |
410 | { |
411 | complete_name = (char *) XtMalloc (strlen (resourced_name) + 1); | |
412 | strcpy (complete_name, resourced_name); | |
413 | } | |
07bf635f RS |
414 | else |
415 | { | |
416 | int complete_length = | |
417 | strlen (resourced_name) + strlen (val->value) + 2; | |
418 | complete_name = XtMalloc (complete_length); | |
419 | *complete_name = 0; | |
420 | strcat (complete_name, resourced_name); | |
421 | strcat (complete_name, " "); | |
422 | strcat (complete_name, val->value); | |
423 | } | |
424 | ||
425 | val->toolkit_data = complete_name; | |
426 | val->free_toolkit_data = True; | |
427 | } | |
428 | return (char*)val->toolkit_data; | |
429 | } | |
430 | ||
431 | /* Returns the sizes of an item */ | |
432 | static void | |
be06a3df GM |
433 | size_menu_item (mw, val, horizontal_p, label_width, rest_width, button_width, |
434 | height) | |
d398028f PR |
435 | XlwMenuWidget mw; |
436 | widget_value* val; | |
437 | int horizontal_p; | |
438 | int* label_width; | |
439 | int* rest_width; | |
be06a3df | 440 | int* button_width; |
d398028f | 441 | int* height; |
07bf635f | 442 | { |
be06a3df | 443 | enum menu_separator separator; |
5c520e0a | 444 | |
be06a3df | 445 | if (lw_separator_p (val->name, &separator, 0)) |
07bf635f | 446 | { |
be06a3df | 447 | *height = separator_height (separator); |
07bf635f RS |
448 | *label_width = 1; |
449 | *rest_width = 0; | |
be06a3df | 450 | *button_width = 0; |
07bf635f RS |
451 | } |
452 | else | |
453 | { | |
454 | *height = | |
455 | mw->menu.font->ascent + mw->menu.font->descent | |
456 | + 2 * mw->menu.vertical_spacing + 2 * mw->menu.shadow_thickness; | |
5c520e0a | 457 | |
07bf635f RS |
458 | *label_width = |
459 | string_width (mw, resource_widget_value (mw, val)) | |
460 | + mw->menu.horizontal_spacing + mw->menu.shadow_thickness; | |
5c520e0a | 461 | |
07bf635f RS |
462 | *rest_width = mw->menu.horizontal_spacing + mw->menu.shadow_thickness; |
463 | if (!horizontal_p) | |
464 | { | |
465 | if (val->contents) | |
be06a3df | 466 | /* Add width of the arrow displayed for submenus. */ |
07bf635f RS |
467 | *rest_width += arrow_width (mw) + mw->menu.arrow_spacing; |
468 | else if (val->key) | |
be06a3df GM |
469 | /* Add width of key equivalent string. */ |
470 | *rest_width += (string_width (mw, val->key) | |
471 | + mw->menu.arrow_spacing); | |
472 | ||
473 | if (val->button_type == BUTTON_TYPE_TOGGLE) | |
474 | *button_width = (toggle_button_width (mw) | |
475 | + mw->menu.horizontal_spacing); | |
476 | else if (val->button_type == BUTTON_TYPE_RADIO) | |
477 | *button_width = (radio_button_width (mw) | |
478 | + mw->menu.horizontal_spacing); | |
07bf635f RS |
479 | } |
480 | } | |
481 | } | |
482 | ||
483 | static void | |
d398028f PR |
484 | size_menu (mw, level) |
485 | XlwMenuWidget mw; | |
486 | int level; | |
07bf635f | 487 | { |
ccf589a6 | 488 | unsigned int label_width = 0; |
07bf635f | 489 | int rest_width = 0; |
be06a3df | 490 | int button_width = 0; |
07bf635f | 491 | int max_rest_width = 0; |
be06a3df | 492 | int max_button_width = 0; |
ccf589a6 | 493 | unsigned int height = 0; |
07bf635f RS |
494 | int horizontal_p = mw->menu.horizontal && (level == 0); |
495 | widget_value* val; | |
496 | window_state* ws; | |
497 | ||
498 | if (level >= mw->menu.old_depth) | |
be06a3df | 499 | abort_gracefully ((Widget) mw); |
07bf635f | 500 | |
5c520e0a | 501 | ws = &mw->menu.windows [level]; |
07bf635f RS |
502 | ws->width = 0; |
503 | ws->height = 0; | |
504 | ws->label_width = 0; | |
be06a3df | 505 | ws->button_width = 0; |
07bf635f RS |
506 | |
507 | for (val = mw->menu.old_stack [level]->contents; val; val = val->next) | |
508 | { | |
509 | size_menu_item (mw, val, horizontal_p, &label_width, &rest_width, | |
be06a3df | 510 | &button_width, &height); |
07bf635f RS |
511 | if (horizontal_p) |
512 | { | |
513 | ws->width += label_width + rest_width; | |
514 | if (height > ws->height) | |
515 | ws->height = height; | |
516 | } | |
517 | else | |
518 | { | |
519 | if (label_width > ws->label_width) | |
520 | ws->label_width = label_width; | |
521 | if (rest_width > max_rest_width) | |
522 | max_rest_width = rest_width; | |
be06a3df GM |
523 | if (button_width > max_button_width) |
524 | max_button_width = button_width; | |
07bf635f RS |
525 | ws->height += height; |
526 | } | |
527 | } | |
5c520e0a | 528 | |
07bf635f | 529 | if (horizontal_p) |
be06a3df | 530 | ws->label_width = ws->button_width = 0; |
07bf635f | 531 | else |
be06a3df GM |
532 | { |
533 | ws->width = ws->label_width + max_rest_width + max_button_width; | |
534 | ws->button_width = max_button_width; | |
535 | } | |
07bf635f RS |
536 | |
537 | ws->width += 2 * mw->menu.shadow_thickness; | |
538 | ws->height += 2 * mw->menu.shadow_thickness; | |
be06a3df GM |
539 | |
540 | if (horizontal_p) | |
541 | { | |
542 | ws->width += 2 * mw->menu.margin; | |
543 | ws->height += 2 * mw->menu.margin; | |
544 | } | |
07bf635f RS |
545 | } |
546 | ||
547 | ||
548 | \f/* Display code */ | |
be06a3df | 549 | |
07bf635f | 550 | static void |
be06a3df | 551 | draw_arrow (mw, window, gc, x, y, width, down_p) |
d398028f PR |
552 | XlwMenuWidget mw; |
553 | Window window; | |
554 | GC gc; | |
555 | int x; | |
556 | int y; | |
557 | int width; | |
be06a3df | 558 | int down_p; |
07bf635f | 559 | { |
be06a3df GM |
560 | Display *dpy = XtDisplay (mw); |
561 | GC top_gc = mw->menu.shadow_top_gc; | |
562 | GC bottom_gc = mw->menu.shadow_bottom_gc; | |
563 | int thickness = mw->menu.shadow_thickness; | |
564 | int height = width; | |
565 | XPoint pt[10]; | |
566 | /* alpha = atan (0.5) | |
567 | factor = (1 + sin (alpha)) / cos (alpha) */ | |
568 | double factor = 1.62; | |
569 | int thickness2 = thickness * factor; | |
5c520e0a | 570 | |
be06a3df | 571 | y += (mw->menu.font->ascent + mw->menu.font->descent - height) / 2; |
5c520e0a | 572 | |
be06a3df GM |
573 | if (down_p) |
574 | { | |
575 | GC temp; | |
576 | temp = top_gc; | |
577 | top_gc = bottom_gc; | |
578 | bottom_gc = temp; | |
579 | } | |
580 | ||
581 | pt[0].x = x; | |
582 | pt[0].y = y + height; | |
583 | pt[1].x = x + thickness; | |
584 | pt[1].y = y + height - thickness2; | |
585 | pt[2].x = x + thickness2; | |
586 | pt[2].y = y + thickness2; | |
587 | pt[3].x = x; | |
588 | pt[3].y = y; | |
589 | XFillPolygon (dpy, window, top_gc, pt, 4, Convex, CoordModeOrigin); | |
5c520e0a | 590 | |
be06a3df GM |
591 | pt[0].x = x; |
592 | pt[0].y = y; | |
593 | pt[1].x = x + thickness; | |
594 | pt[1].y = y + thickness2; | |
595 | pt[2].x = x + width - thickness2; | |
596 | pt[2].y = y + height / 2; | |
597 | pt[3].x = x + width; | |
598 | pt[3].y = y + height / 2; | |
599 | XFillPolygon (dpy, window, top_gc, pt, 4, Convex, CoordModeOrigin); | |
5c520e0a | 600 | |
be06a3df GM |
601 | pt[0].x = x; |
602 | pt[0].y = y + height; | |
603 | pt[1].x = x + thickness; | |
604 | pt[1].y = y + height - thickness2; | |
605 | pt[2].x = x + width - thickness2; | |
606 | pt[2].y = y + height / 2; | |
607 | pt[3].x = x + width; | |
608 | pt[3].y = y + height / 2; | |
609 | XFillPolygon (dpy, window, bottom_gc, pt, 4, Convex, CoordModeOrigin); | |
07bf635f RS |
610 | } |
611 | ||
be06a3df GM |
612 | |
613 | ||
07bf635f | 614 | static void |
be06a3df | 615 | draw_shadow_rectangle (mw, window, x, y, width, height, erase_p, down_p) |
d398028f PR |
616 | XlwMenuWidget mw; |
617 | Window window; | |
618 | int x; | |
619 | int y; | |
620 | int width; | |
621 | int height; | |
622 | int erase_p; | |
be06a3df | 623 | int down_p; |
07bf635f RS |
624 | { |
625 | Display *dpy = XtDisplay (mw); | |
626 | GC top_gc = !erase_p ? mw->menu.shadow_top_gc : mw->menu.background_gc; | |
627 | GC bottom_gc = !erase_p ? mw->menu.shadow_bottom_gc : mw->menu.background_gc; | |
628 | int thickness = mw->menu.shadow_thickness; | |
629 | XPoint points [4]; | |
be06a3df GM |
630 | |
631 | if (!erase_p && down_p) | |
632 | { | |
633 | GC temp; | |
634 | temp = top_gc; | |
635 | top_gc = bottom_gc; | |
636 | bottom_gc = temp; | |
637 | } | |
5c520e0a | 638 | |
07bf635f RS |
639 | points [0].x = x; |
640 | points [0].y = y; | |
641 | points [1].x = x + width; | |
642 | points [1].y = y; | |
643 | points [2].x = x + width - thickness; | |
644 | points [2].y = y + thickness; | |
645 | points [3].x = x; | |
646 | points [3].y = y + thickness; | |
647 | XFillPolygon (dpy, window, top_gc, points, 4, Convex, CoordModeOrigin); | |
648 | points [0].x = x; | |
649 | points [0].y = y + thickness; | |
650 | points [1].x = x; | |
651 | points [1].y = y + height; | |
652 | points [2].x = x + thickness; | |
653 | points [2].y = y + height - thickness; | |
654 | points [3].x = x + thickness; | |
655 | points [3].y = y + thickness; | |
656 | XFillPolygon (dpy, window, top_gc, points, 4, Convex, CoordModeOrigin); | |
657 | points [0].x = x + width; | |
658 | points [0].y = y; | |
659 | points [1].x = x + width - thickness; | |
660 | points [1].y = y + thickness; | |
661 | points [2].x = x + width - thickness; | |
662 | points [2].y = y + height - thickness; | |
663 | points [3].x = x + width; | |
664 | points [3].y = y + height - thickness; | |
665 | XFillPolygon (dpy, window, bottom_gc, points, 4, Convex, CoordModeOrigin); | |
666 | points [0].x = x; | |
667 | points [0].y = y + height; | |
668 | points [1].x = x + width; | |
669 | points [1].y = y + height; | |
670 | points [2].x = x + width; | |
671 | points [2].y = y + height - thickness; | |
672 | points [3].x = x + thickness; | |
673 | points [3].y = y + height - thickness; | |
674 | XFillPolygon (dpy, window, bottom_gc, points, 4, Convex, CoordModeOrigin); | |
675 | } | |
676 | ||
677 | ||
be06a3df GM |
678 | static void |
679 | draw_shadow_rhombus (mw, window, x, y, width, height, erase_p, down_p) | |
680 | XlwMenuWidget mw; | |
681 | Window window; | |
682 | int x; | |
683 | int y; | |
684 | int width; | |
685 | int height; | |
686 | int erase_p; | |
687 | int down_p; | |
688 | { | |
689 | Display *dpy = XtDisplay (mw); | |
690 | GC top_gc = !erase_p ? mw->menu.shadow_top_gc : mw->menu.background_gc; | |
691 | GC bottom_gc = !erase_p ? mw->menu.shadow_bottom_gc : mw->menu.background_gc; | |
692 | int thickness = mw->menu.shadow_thickness; | |
693 | XPoint points [4]; | |
be06a3df GM |
694 | |
695 | if (!erase_p && down_p) | |
696 | { | |
697 | GC temp; | |
698 | temp = top_gc; | |
699 | top_gc = bottom_gc; | |
700 | bottom_gc = temp; | |
701 | } | |
702 | ||
703 | points [0].x = x; | |
704 | points [0].y = y + height / 2; | |
705 | points [1].x = x + thickness; | |
706 | points [1].y = y + height / 2; | |
707 | points [2].x = x + width / 2; | |
708 | points [2].y = y + thickness; | |
709 | points [3].x = x + width / 2; | |
710 | points [3].y = y; | |
711 | XFillPolygon (dpy, window, top_gc, points, 4, Convex, CoordModeOrigin); | |
712 | points [0].x = x + width / 2; | |
713 | points [0].y = y; | |
714 | points [1].x = x + width / 2; | |
715 | points [1].y = y + thickness; | |
716 | points [2].x = x + width - thickness; | |
717 | points [2].y = y + height / 2; | |
718 | points [3].x = x + width; | |
719 | points [3].y = y + height / 2; | |
720 | XFillPolygon (dpy, window, top_gc, points, 4, Convex, CoordModeOrigin); | |
721 | points [0].x = x; | |
722 | points [0].y = y + height / 2; | |
723 | points [1].x = x + thickness; | |
724 | points [1].y = y + height / 2; | |
725 | points [2].x = x + width / 2; | |
726 | points [2].y = y + height - thickness; | |
727 | points [3].x = x + width / 2; | |
728 | points [3].y = y + height; | |
729 | XFillPolygon (dpy, window, bottom_gc, points, 4, Convex, CoordModeOrigin); | |
730 | points [0].x = x + width / 2; | |
731 | points [0].y = y + height; | |
732 | points [1].x = x + width / 2; | |
733 | points [1].y = y + height - thickness; | |
734 | points [2].x = x + width - thickness; | |
735 | points [2].y = y + height / 2; | |
736 | points [3].x = x + width; | |
737 | points [3].y = y + height / 2; | |
738 | XFillPolygon (dpy, window, bottom_gc, points, 4, Convex, CoordModeOrigin); | |
739 | } | |
740 | ||
741 | ||
742 | /* Draw a toggle button on widget MW, X window WINDOW. X/Y is the | |
743 | top-left corner of the menu item. SELECTED_P non-zero means the | |
744 | toggle button is selected. */ | |
745 | ||
746 | static void | |
747 | draw_toggle (mw, window, x, y, selected_p) | |
748 | XlwMenuWidget mw; | |
749 | Window window; | |
750 | int x, y, selected_p; | |
751 | { | |
752 | int width, height; | |
753 | ||
754 | width = toggle_button_width (mw); | |
755 | height = width; | |
756 | x += mw->menu.horizontal_spacing; | |
757 | y += (mw->menu.font->ascent - height) / 2; | |
758 | draw_shadow_rectangle (mw, window, x, y, width, height, False, selected_p); | |
759 | } | |
760 | ||
761 | ||
762 | /* Draw a radio button on widget MW, X window WINDOW. X/Y is the | |
763 | top-left corner of the menu item. SELECTED_P non-zero means the | |
764 | toggle button is selected. */ | |
765 | ||
766 | static void | |
767 | draw_radio (mw, window, x, y, selected_p) | |
768 | XlwMenuWidget mw; | |
769 | Window window; | |
770 | int x, y, selected_p; | |
771 | { | |
772 | int width, height; | |
773 | ||
774 | width = radio_button_width (mw); | |
775 | height = width; | |
776 | x += mw->menu.horizontal_spacing; | |
777 | y += (mw->menu.font->ascent - height) / 2; | |
778 | draw_shadow_rhombus (mw, window, x, y, width, height, False, selected_p); | |
779 | } | |
780 | ||
781 | ||
782 | /* Draw a menu separator on widget MW, X window WINDOW. X/Y is the | |
783 | top-left corner of the menu item. WIDTH is the width of the | |
784 | separator to draw. TYPE is the separator type. */ | |
785 | ||
786 | static void | |
787 | draw_separator (mw, window, x, y, width, type) | |
788 | XlwMenuWidget mw; | |
789 | Window window; | |
790 | int x, y, width; | |
791 | enum menu_separator type; | |
792 | { | |
793 | Display *dpy = XtDisplay (mw); | |
794 | XGCValues xgcv; | |
795 | ||
796 | switch (type) | |
797 | { | |
798 | case SEPARATOR_NO_LINE: | |
799 | break; | |
5c520e0a | 800 | |
be06a3df GM |
801 | case SEPARATOR_SINGLE_LINE: |
802 | XDrawLine (dpy, window, mw->menu.foreground_gc, | |
803 | x, y, x + width, y); | |
804 | break; | |
5c520e0a | 805 | |
be06a3df GM |
806 | case SEPARATOR_DOUBLE_LINE: |
807 | draw_separator (mw, window, x, y, width, SEPARATOR_SINGLE_LINE); | |
808 | draw_separator (mw, window, x, y + 2, width, SEPARATOR_SINGLE_LINE); | |
809 | break; | |
5c520e0a | 810 | |
be06a3df GM |
811 | case SEPARATOR_SINGLE_DASHED_LINE: |
812 | xgcv.line_style = LineOnOffDash; | |
813 | XChangeGC (dpy, mw->menu.foreground_gc, GCLineStyle, &xgcv); | |
814 | XDrawLine (dpy, window, mw->menu.foreground_gc, | |
815 | x, y, x + width, y); | |
816 | xgcv.line_style = LineSolid; | |
817 | XChangeGC (dpy, mw->menu.foreground_gc, GCLineStyle, &xgcv); | |
818 | break; | |
5c520e0a | 819 | |
be06a3df GM |
820 | case SEPARATOR_DOUBLE_DASHED_LINE: |
821 | draw_separator (mw, window, x, y, width, | |
822 | SEPARATOR_SINGLE_DASHED_LINE); | |
823 | draw_separator (mw, window, x, y + 2, width, | |
824 | SEPARATOR_SINGLE_DASHED_LINE); | |
825 | break; | |
5c520e0a | 826 | |
be06a3df GM |
827 | case SEPARATOR_SHADOW_ETCHED_IN: |
828 | XDrawLine (dpy, window, mw->menu.shadow_bottom_gc, | |
829 | x, y, x + width, y); | |
830 | XDrawLine (dpy, window, mw->menu.shadow_top_gc, | |
831 | x, y + 1, x + width, y + 1); | |
832 | break; | |
833 | ||
834 | case SEPARATOR_SHADOW_ETCHED_OUT: | |
835 | XDrawLine (dpy, window, mw->menu.shadow_top_gc, | |
836 | x, y, x + width, y); | |
837 | XDrawLine (dpy, window, mw->menu.shadow_bottom_gc, | |
838 | x, y + 1, x + width, y + 1); | |
839 | break; | |
5c520e0a | 840 | |
be06a3df GM |
841 | case SEPARATOR_SHADOW_ETCHED_IN_DASH: |
842 | xgcv.line_style = LineOnOffDash; | |
843 | XChangeGC (dpy, mw->menu.shadow_bottom_gc, GCLineStyle, &xgcv); | |
844 | XChangeGC (dpy, mw->menu.shadow_top_gc, GCLineStyle, &xgcv); | |
2f90328f | 845 | draw_separator (mw, window, x, y, width, SEPARATOR_SHADOW_ETCHED_IN); |
be06a3df GM |
846 | xgcv.line_style = LineSolid; |
847 | XChangeGC (dpy, mw->menu.shadow_bottom_gc, GCLineStyle, &xgcv); | |
848 | XChangeGC (dpy, mw->menu.shadow_top_gc, GCLineStyle, &xgcv); | |
849 | break; | |
5c520e0a | 850 | |
be06a3df GM |
851 | case SEPARATOR_SHADOW_ETCHED_OUT_DASH: |
852 | xgcv.line_style = LineOnOffDash; | |
853 | XChangeGC (dpy, mw->menu.shadow_bottom_gc, GCLineStyle, &xgcv); | |
854 | XChangeGC (dpy, mw->menu.shadow_top_gc, GCLineStyle, &xgcv); | |
2f90328f | 855 | draw_separator (mw, window, x, y, width, SEPARATOR_SHADOW_ETCHED_OUT); |
be06a3df GM |
856 | xgcv.line_style = LineSolid; |
857 | XChangeGC (dpy, mw->menu.shadow_bottom_gc, GCLineStyle, &xgcv); | |
858 | XChangeGC (dpy, mw->menu.shadow_top_gc, GCLineStyle, &xgcv); | |
859 | break; | |
860 | ||
861 | case SEPARATOR_SHADOW_DOUBLE_ETCHED_IN: | |
862 | draw_separator (mw, window, x, y, width, SEPARATOR_SHADOW_ETCHED_IN); | |
863 | draw_separator (mw, window, x, y + 3, width, SEPARATOR_SHADOW_ETCHED_IN); | |
864 | break; | |
5c520e0a | 865 | |
be06a3df GM |
866 | case SEPARATOR_SHADOW_DOUBLE_ETCHED_OUT: |
867 | draw_separator (mw, window, x, y, width, | |
868 | SEPARATOR_SHADOW_ETCHED_OUT); | |
869 | draw_separator (mw, window, x, y + 3, width, | |
870 | SEPARATOR_SHADOW_ETCHED_OUT); | |
871 | break; | |
5c520e0a | 872 | |
be06a3df GM |
873 | case SEPARATOR_SHADOW_DOUBLE_ETCHED_IN_DASH: |
874 | xgcv.line_style = LineOnOffDash; | |
875 | XChangeGC (dpy, mw->menu.shadow_bottom_gc, GCLineStyle, &xgcv); | |
876 | XChangeGC (dpy, mw->menu.shadow_top_gc, GCLineStyle, &xgcv); | |
877 | draw_separator (mw, window, x, y, width, | |
878 | SEPARATOR_SHADOW_DOUBLE_ETCHED_IN); | |
879 | xgcv.line_style = LineSolid; | |
880 | XChangeGC (dpy, mw->menu.shadow_bottom_gc, GCLineStyle, &xgcv); | |
881 | XChangeGC (dpy, mw->menu.shadow_top_gc, GCLineStyle, &xgcv); | |
882 | break; | |
883 | ||
884 | case SEPARATOR_SHADOW_DOUBLE_ETCHED_OUT_DASH: | |
885 | xgcv.line_style = LineOnOffDash; | |
886 | XChangeGC (dpy, mw->menu.shadow_bottom_gc, GCLineStyle, &xgcv); | |
887 | XChangeGC (dpy, mw->menu.shadow_top_gc, GCLineStyle, &xgcv); | |
888 | draw_separator (mw, window, x, y, width, | |
889 | SEPARATOR_SHADOW_DOUBLE_ETCHED_OUT); | |
890 | xgcv.line_style = LineSolid; | |
891 | XChangeGC (dpy, mw->menu.shadow_bottom_gc, GCLineStyle, &xgcv); | |
892 | XChangeGC (dpy, mw->menu.shadow_top_gc, GCLineStyle, &xgcv); | |
893 | break; | |
894 | ||
895 | default: | |
896 | abort (); | |
897 | } | |
898 | } | |
899 | ||
900 | ||
901 | /* Return the pixel height of menu separator SEPARATOR. */ | |
902 | ||
903 | static int | |
904 | separator_height (separator) | |
905 | enum menu_separator separator; | |
906 | { | |
907 | switch (separator) | |
908 | { | |
909 | case SEPARATOR_NO_LINE: | |
910 | return 2; | |
5c520e0a | 911 | |
be06a3df GM |
912 | case SEPARATOR_SINGLE_LINE: |
913 | case SEPARATOR_SINGLE_DASHED_LINE: | |
914 | return 1; | |
5c520e0a | 915 | |
be06a3df GM |
916 | case SEPARATOR_DOUBLE_LINE: |
917 | case SEPARATOR_DOUBLE_DASHED_LINE: | |
918 | return 3; | |
5c520e0a | 919 | |
be06a3df GM |
920 | case SEPARATOR_SHADOW_ETCHED_IN: |
921 | case SEPARATOR_SHADOW_ETCHED_OUT: | |
922 | case SEPARATOR_SHADOW_ETCHED_IN_DASH: | |
923 | case SEPARATOR_SHADOW_ETCHED_OUT_DASH: | |
924 | return 2; | |
5c520e0a | 925 | |
be06a3df GM |
926 | case SEPARATOR_SHADOW_DOUBLE_ETCHED_IN: |
927 | case SEPARATOR_SHADOW_DOUBLE_ETCHED_OUT: | |
928 | case SEPARATOR_SHADOW_DOUBLE_ETCHED_IN_DASH: | |
929 | case SEPARATOR_SHADOW_DOUBLE_ETCHED_OUT_DASH: | |
930 | return 5; | |
5c520e0a | 931 | |
be06a3df GM |
932 | default: |
933 | abort (); | |
934 | } | |
935 | } | |
936 | ||
937 | ||
07bf635f | 938 | /* Display the menu item and increment where.x and where.y to show how large |
be06a3df GM |
939 | the menu item was. */ |
940 | ||
07bf635f | 941 | static void |
be06a3df GM |
942 | display_menu_item (mw, val, ws, where, highlighted_p, horizontal_p, |
943 | just_compute_p) | |
d398028f PR |
944 | XlwMenuWidget mw; |
945 | widget_value* val; | |
946 | window_state* ws; | |
947 | XPoint* where; | |
948 | Boolean highlighted_p; | |
949 | Boolean horizontal_p; | |
950 | Boolean just_compute_p; | |
07bf635f RS |
951 | { |
952 | GC deco_gc; | |
953 | GC text_gc; | |
954 | int font_ascent = mw->menu.font->ascent; | |
955 | int font_descent = mw->menu.font->descent; | |
956 | int shadow = mw->menu.shadow_thickness; | |
be06a3df | 957 | int margin = mw->menu.margin; |
07bf635f RS |
958 | int h_spacing = mw->menu.horizontal_spacing; |
959 | int v_spacing = mw->menu.vertical_spacing; | |
960 | int label_width; | |
961 | int rest_width; | |
be06a3df | 962 | int button_width; |
07bf635f RS |
963 | int height; |
964 | int width; | |
be06a3df GM |
965 | enum menu_separator separator; |
966 | int separator_p = lw_separator_p (val->name, &separator, 0); | |
07bf635f RS |
967 | |
968 | /* compute the sizes of the item */ | |
be06a3df GM |
969 | size_menu_item (mw, val, horizontal_p, &label_width, &rest_width, |
970 | &button_width, &height); | |
07bf635f RS |
971 | |
972 | if (horizontal_p) | |
973 | width = label_width + rest_width; | |
974 | else | |
975 | { | |
976 | label_width = ws->label_width; | |
977 | width = ws->width - 2 * shadow; | |
978 | } | |
979 | ||
07bf635f RS |
980 | /* Only highlight an enabled item that has a callback. */ |
981 | if (highlighted_p) | |
982 | if (!val->enabled || !(val->call_data || val->contents)) | |
983 | highlighted_p = 0; | |
984 | ||
985 | /* do the drawing. */ | |
986 | if (!just_compute_p) | |
987 | { | |
988 | /* Add the shadow border of the containing menu */ | |
989 | int x = where->x + shadow; | |
990 | int y = where->y + shadow; | |
991 | ||
be06a3df GM |
992 | if (horizontal_p) |
993 | { | |
994 | x += margin; | |
995 | y += margin; | |
996 | } | |
997 | ||
07bf635f RS |
998 | /* pick the foreground and background GC. */ |
999 | if (val->enabled) | |
be06a3df | 1000 | text_gc = mw->menu.foreground_gc; |
07bf635f | 1001 | else |
8b71a9ca | 1002 | text_gc = mw->menu.disabled_gc; |
07bf635f RS |
1003 | deco_gc = mw->menu.foreground_gc; |
1004 | ||
1005 | if (separator_p) | |
1006 | { | |
be06a3df | 1007 | draw_separator (mw, ws->window, x, y, width, separator); |
07bf635f | 1008 | } |
5c520e0a | 1009 | else |
07bf635f | 1010 | { |
84593cae | 1011 | int x_offset = x + h_spacing + shadow; |
07bf635f | 1012 | char* display_string = resource_widget_value (mw, val); |
be06a3df GM |
1013 | draw_shadow_rectangle (mw, ws->window, x, y, width, height, True, |
1014 | False); | |
84593cae PR |
1015 | |
1016 | /* Deal with centering a menu title. */ | |
1017 | if (!horizontal_p && !val->contents && !val->call_data) | |
1018 | { | |
1019 | int l = string_width (mw, display_string); | |
1020 | ||
1021 | if (width > l) | |
1022 | x_offset = (width - l) >> 1; | |
1023 | } | |
be06a3df GM |
1024 | else if (!horizontal_p && ws->button_width) |
1025 | x_offset += ws->button_width; | |
5c520e0a SS |
1026 | |
1027 | ||
84593cae | 1028 | XDrawString (XtDisplay (mw), ws->window, text_gc, x_offset, |
07bf635f RS |
1029 | y + v_spacing + shadow + font_ascent, |
1030 | display_string, strlen (display_string)); | |
5c520e0a | 1031 | |
07bf635f RS |
1032 | if (!horizontal_p) |
1033 | { | |
be06a3df GM |
1034 | if (val->button_type == BUTTON_TYPE_TOGGLE) |
1035 | draw_toggle (mw, ws->window, x, y + v_spacing + shadow, | |
1036 | val->selected); | |
1037 | else if (val->button_type == BUTTON_TYPE_RADIO) | |
1038 | draw_radio (mw, ws->window, x, y + v_spacing + shadow, | |
1039 | val->selected); | |
5c520e0a | 1040 | |
07bf635f RS |
1041 | if (val->contents) |
1042 | { | |
1043 | int a_w = arrow_width (mw); | |
1044 | draw_arrow (mw, ws->window, deco_gc, | |
be06a3df | 1045 | x + width - a_w |
5c520e0a | 1046 | - mw->menu.horizontal_spacing |
d398028f | 1047 | - mw->menu.shadow_thickness, |
be06a3df GM |
1048 | y + v_spacing + shadow, a_w, |
1049 | highlighted_p); | |
07bf635f RS |
1050 | } |
1051 | else if (val->key) | |
1052 | { | |
1053 | XDrawString (XtDisplay (mw), ws->window, text_gc, | |
1054 | x + label_width + mw->menu.arrow_spacing, | |
1055 | y + v_spacing + shadow + font_ascent, | |
1056 | val->key, strlen (val->key)); | |
1057 | } | |
1058 | } | |
d398028f PR |
1059 | else |
1060 | { | |
5c520e0a | 1061 | XDrawRectangle (XtDisplay (mw), ws->window, |
d398028f PR |
1062 | mw->menu.background_gc, |
1063 | x + shadow, y + shadow, | |
1064 | label_width + h_spacing - 1, | |
1065 | font_ascent + font_descent + 2 * v_spacing - 1); | |
1066 | draw_shadow_rectangle (mw, ws->window, x, y, width, height, | |
be06a3df | 1067 | True, False); |
d398028f | 1068 | } |
07bf635f RS |
1069 | |
1070 | if (highlighted_p) | |
be06a3df GM |
1071 | draw_shadow_rectangle (mw, ws->window, x, y, width, height, False, |
1072 | False); | |
07bf635f RS |
1073 | } |
1074 | } | |
5c520e0a | 1075 | |
07bf635f RS |
1076 | where->x += width; |
1077 | where->y += height; | |
1078 | } | |
1079 | ||
1080 | static void | |
d398028f PR |
1081 | display_menu (mw, level, just_compute_p, highlighted_pos, hit, hit_return, |
1082 | this, that) | |
1083 | XlwMenuWidget mw; | |
1084 | int level; | |
1085 | Boolean just_compute_p; | |
1086 | XPoint* highlighted_pos; | |
1087 | XPoint* hit; | |
1088 | widget_value** hit_return; | |
1089 | widget_value* this; | |
1090 | widget_value* that; | |
07bf635f RS |
1091 | { |
1092 | widget_value* val; | |
1093 | widget_value* following_item; | |
1094 | window_state* ws; | |
1095 | XPoint where; | |
1096 | int horizontal_p = mw->menu.horizontal && (level == 0); | |
1097 | int highlighted_p; | |
1098 | int just_compute_this_one_p; | |
ccc1cac0 RS |
1099 | /* This is set nonzero if the element containing HIGHLIGHTED_POS |
1100 | is disabled, so that we do not return any subsequent element either. */ | |
1101 | int no_return = 0; | |
be06a3df | 1102 | enum menu_separator separator; |
07bf635f RS |
1103 | |
1104 | if (level >= mw->menu.old_depth) | |
be06a3df | 1105 | abort_gracefully ((Widget) mw); |
07bf635f RS |
1106 | |
1107 | if (level < mw->menu.old_depth - 1) | |
1108 | following_item = mw->menu.old_stack [level + 1]; | |
5c520e0a | 1109 | else |
07bf635f RS |
1110 | following_item = NULL; |
1111 | ||
1112 | if (hit) | |
1113 | *hit_return = NULL; | |
1114 | ||
1115 | where.x = 0; | |
1116 | where.y = 0; | |
1117 | ||
1118 | ws = &mw->menu.windows [level]; | |
1119 | for (val = mw->menu.old_stack [level]->contents; val; val = val->next) | |
1120 | { | |
1121 | highlighted_p = val == following_item; | |
1122 | if (highlighted_p && highlighted_pos) | |
1123 | { | |
1124 | if (horizontal_p) | |
1125 | highlighted_pos->x = where.x; | |
1126 | else | |
1127 | highlighted_pos->y = where.y; | |
1128 | } | |
5c520e0a | 1129 | |
07bf635f RS |
1130 | just_compute_this_one_p = |
1131 | just_compute_p || ((this || that) && val != this && val != that); | |
1132 | ||
1133 | display_menu_item (mw, val, ws, &where, highlighted_p, horizontal_p, | |
1134 | just_compute_this_one_p); | |
1135 | ||
1136 | if (highlighted_p && highlighted_pos) | |
1137 | { | |
1138 | if (horizontal_p) | |
1139 | highlighted_pos->y = where.y; | |
1140 | else | |
1141 | highlighted_pos->x = where.x; | |
1142 | } | |
1143 | ||
1144 | if (hit | |
1145 | && !*hit_return | |
1146 | && (horizontal_p ? hit->x < where.x : hit->y < where.y) | |
be06a3df | 1147 | && !lw_separator_p (val->name, &separator, 0) |
ccc1cac0 RS |
1148 | && !no_return) |
1149 | { | |
1150 | if (val->enabled) | |
1151 | *hit_return = val; | |
1152 | else | |
1153 | no_return = 1; | |
1154 | } | |
07bf635f RS |
1155 | |
1156 | if (horizontal_p) | |
1157 | where.y = 0; | |
1158 | else | |
1159 | where.x = 0; | |
1160 | } | |
5c520e0a | 1161 | |
07bf635f | 1162 | if (!just_compute_p) |
be06a3df GM |
1163 | draw_shadow_rectangle (mw, ws->window, 0, 0, ws->width, ws->height, |
1164 | False, False); | |
07bf635f RS |
1165 | } |
1166 | ||
1167 | \f/* Motion code */ | |
1168 | static void | |
d398028f PR |
1169 | set_new_state (mw, val, level) |
1170 | XlwMenuWidget mw; | |
1171 | widget_value* val; | |
1172 | int level; | |
07bf635f RS |
1173 | { |
1174 | int i; | |
5c520e0a | 1175 | |
07bf635f RS |
1176 | mw->menu.new_depth = 0; |
1177 | for (i = 0; i < level; i++) | |
1178 | push_new_stack (mw, mw->menu.old_stack [i]); | |
1179 | push_new_stack (mw, val); | |
1180 | } | |
1181 | ||
1182 | static void | |
d398028f PR |
1183 | make_windows_if_needed (mw, n) |
1184 | XlwMenuWidget mw; | |
1185 | int n; | |
07bf635f RS |
1186 | { |
1187 | int i; | |
1188 | int start_at; | |
1189 | XSetWindowAttributes xswa; | |
1190 | int mask; | |
1191 | Window root = RootWindowOfScreen (DefaultScreenOfDisplay (XtDisplay (mw))); | |
1192 | window_state* windows; | |
5c520e0a | 1193 | |
07bf635f RS |
1194 | if (mw->menu.windows_length >= n) |
1195 | return; | |
1196 | ||
1197 | xswa.save_under = True; | |
1198 | xswa.override_redirect = True; | |
1199 | xswa.background_pixel = mw->core.background_pixel; | |
1200 | xswa.border_pixel = mw->core.border_pixel; | |
1201 | xswa.event_mask = | |
d398028f | 1202 | ExposureMask | PointerMotionMask | PointerMotionHintMask |
07bf635f RS |
1203 | | ButtonReleaseMask | ButtonPressMask; |
1204 | xswa.cursor = mw->menu.cursor_shape; | |
1205 | mask = CWSaveUnder | CWOverrideRedirect | CWBackPixel | CWBorderPixel | |
1206 | | CWEventMask | CWCursor; | |
5c520e0a | 1207 | |
07bf635f RS |
1208 | if (!mw->menu.windows) |
1209 | { | |
1210 | mw->menu.windows = | |
1211 | (window_state*)XtMalloc (n * sizeof (window_state)); | |
1212 | start_at = 0; | |
1213 | } | |
1214 | else | |
1215 | { | |
1216 | mw->menu.windows = | |
1217 | (window_state*)XtRealloc ((char*)mw->menu.windows, | |
1218 | n * sizeof (window_state)); | |
1219 | start_at = mw->menu.windows_length; | |
1220 | } | |
1221 | mw->menu.windows_length = n; | |
1222 | ||
1223 | windows = mw->menu.windows; | |
1224 | ||
1225 | for (i = start_at; i < n; i++) | |
1226 | { | |
1227 | windows [i].x = 0; | |
1228 | windows [i].y = 0; | |
1229 | windows [i].width = 1; | |
1230 | windows [i].height = 1; | |
1231 | windows [i].window = | |
1232 | XCreateWindow (XtDisplay (mw), root, 0, 0, 1, 1, | |
1233 | 0, 0, CopyFromParent, CopyFromParent, mask, &xswa); | |
1234 | } | |
1235 | } | |
1236 | ||
2a692ba4 GM |
1237 | /* Value is non-zero if WINDOW is part of menu bar widget W. */ |
1238 | ||
1239 | int | |
1240 | xlwmenu_window_p (w, window) | |
1241 | Widget w; | |
1242 | Window window; | |
1243 | { | |
1244 | XlwMenuWidget mw = (XlwMenuWidget) w; | |
1245 | int i; | |
1246 | ||
1247 | for (i = 0; i < mw->menu.windows_length; ++i) | |
1248 | if (window == mw->menu.windows[i].window) | |
1249 | break; | |
1250 | ||
1251 | return i < mw->menu.windows_length; | |
1252 | } | |
1253 | ||
07bf635f RS |
1254 | /* Make the window fit in the screen */ |
1255 | static void | |
d398028f PR |
1256 | fit_to_screen (mw, ws, previous_ws, horizontal_p) |
1257 | XlwMenuWidget mw; | |
1258 | window_state* ws; | |
1259 | window_state* previous_ws; | |
1260 | Boolean horizontal_p; | |
07bf635f | 1261 | { |
ccf589a6 RS |
1262 | unsigned int screen_width = WidthOfScreen (XtScreen (mw)); |
1263 | unsigned int screen_height = HeightOfScreen (XtScreen (mw)); | |
1ba46f7d RS |
1264 | /* 1 if we are unable to avoid an overlap between |
1265 | this menu and the parent menu in the X dimension. */ | |
1266 | int horizontal_overlap = 0; | |
07bf635f RS |
1267 | |
1268 | if (ws->x < 0) | |
1269 | ws->x = 0; | |
1270 | else if (ws->x + ws->width > screen_width) | |
1271 | { | |
1272 | if (!horizontal_p) | |
b7fa4a06 MB |
1273 | /* The addition of shadow-thickness for a sub-menu's position is |
1274 | to reflect a similar adjustment when the menu is displayed to | |
1275 | the right of the invoking menu-item; it makes the sub-menu | |
1276 | look more `attached' to the menu-item. */ | |
1277 | ws->x = previous_ws->x - ws->width + mw->menu.shadow_thickness; | |
07bf635f RS |
1278 | else |
1279 | ws->x = screen_width - ws->width; | |
525b8232 | 1280 | if (ws->x < 0) |
1ba46f7d RS |
1281 | { |
1282 | ws->x = 0; | |
1283 | horizontal_overlap = 1; | |
1284 | } | |
1285 | } | |
1286 | /* If we overlap in X, try to avoid overlap in Y. */ | |
1287 | if (horizontal_overlap | |
1288 | && ws->y < previous_ws->y + previous_ws->height | |
1289 | && previous_ws->y < ws->y + ws->height) | |
1290 | { | |
1291 | /* Put this menu right below or right above PREVIOUS_WS | |
1292 | if there's room. */ | |
1293 | if (previous_ws->y + previous_ws->height + ws->height < screen_height) | |
1294 | ws->y = previous_ws->y + previous_ws->height; | |
1295 | else if (previous_ws->y - ws->height > 0) | |
1296 | ws->y = previous_ws->y - ws->height; | |
07bf635f | 1297 | } |
1ba46f7d | 1298 | |
07bf635f RS |
1299 | if (ws->y < 0) |
1300 | ws->y = 0; | |
1301 | else if (ws->y + ws->height > screen_height) | |
1302 | { | |
1303 | if (horizontal_p) | |
1304 | ws->y = previous_ws->y - ws->height; | |
1305 | else | |
1306 | ws->y = screen_height - ws->height; | |
5c520e0a | 1307 | if (ws->y < 0) |
525b8232 | 1308 | ws->y = 0; |
07bf635f RS |
1309 | } |
1310 | } | |
1311 | ||
1312 | /* Updates old_stack from new_stack and redisplays. */ | |
1313 | static void | |
d398028f PR |
1314 | remap_menubar (mw) |
1315 | XlwMenuWidget mw; | |
07bf635f RS |
1316 | { |
1317 | int i; | |
1318 | int last_same; | |
1319 | XPoint selection_position; | |
1320 | int old_depth = mw->menu.old_depth; | |
1321 | int new_depth = mw->menu.new_depth; | |
1322 | widget_value** old_stack; | |
1323 | widget_value** new_stack; | |
1324 | window_state* windows; | |
1325 | widget_value* old_selection; | |
1326 | widget_value* new_selection; | |
1327 | ||
1328 | /* Check that enough windows and old_stack are ready. */ | |
1329 | make_windows_if_needed (mw, new_depth); | |
1330 | make_old_stack_space (mw, new_depth); | |
1331 | windows = mw->menu.windows; | |
1332 | old_stack = mw->menu.old_stack; | |
1333 | new_stack = mw->menu.new_stack; | |
1334 | ||
1335 | /* compute the last identical different entry */ | |
1336 | for (i = 1; i < old_depth && i < new_depth; i++) | |
1337 | if (old_stack [i] != new_stack [i]) | |
1338 | break; | |
1339 | last_same = i - 1; | |
1340 | ||
1341 | /* Memorize the previously selected item to be able to refresh it */ | |
1342 | old_selection = last_same + 1 < old_depth ? old_stack [last_same + 1] : NULL; | |
1343 | if (old_selection && !old_selection->enabled) | |
1344 | old_selection = NULL; | |
1345 | new_selection = last_same + 1 < new_depth ? new_stack [last_same + 1] : NULL; | |
1346 | if (new_selection && !new_selection->enabled) | |
1347 | new_selection = NULL; | |
1348 | ||
d6fd6371 GM |
1349 | /* Call callback when the hightlighted item changes. */ |
1350 | if (old_selection || new_selection) | |
1351 | XtCallCallbackList ((Widget)mw, mw->menu.highlight, | |
1352 | (XtPointer) new_selection); | |
1353 | ||
07bf635f RS |
1354 | /* updates old_state from new_state. It has to be done now because |
1355 | display_menu (called below) uses the old_stack to know what to display. */ | |
1356 | for (i = last_same + 1; i < new_depth; i++) | |
1357 | old_stack [i] = new_stack [i]; | |
1358 | mw->menu.old_depth = new_depth; | |
1359 | ||
908ff139 | 1360 | /* refresh the last selection */ |
07bf635f RS |
1361 | selection_position.x = 0; |
1362 | selection_position.y = 0; | |
1363 | display_menu (mw, last_same, new_selection == old_selection, | |
1364 | &selection_position, NULL, NULL, old_selection, new_selection); | |
1365 | ||
1ba46f7d RS |
1366 | /* Now place the new menus. */ |
1367 | for (i = last_same + 1; i < new_depth && new_stack[i]->contents; i++) | |
07bf635f | 1368 | { |
1ba46f7d RS |
1369 | window_state *previous_ws = &windows[i - 1]; |
1370 | window_state *ws = &windows[i]; | |
07bf635f | 1371 | |
be06a3df GM |
1372 | ws->x = (previous_ws->x + selection_position.x |
1373 | + mw->menu.shadow_thickness); | |
b7fa4a06 | 1374 | if (mw->menu.horizontal && i == 1) |
be06a3df GM |
1375 | ws->x += mw->menu.margin; |
1376 | ||
1377 | #if 0 | |
07bf635f RS |
1378 | if (!mw->menu.horizontal || i > 1) |
1379 | ws->x += mw->menu.shadow_thickness; | |
be06a3df | 1380 | #endif |
5c520e0a | 1381 | |
be06a3df GM |
1382 | ws->y = (previous_ws->y + selection_position.y |
1383 | + mw->menu.shadow_thickness); | |
b7fa4a06 | 1384 | if (mw->menu.horizontal && i == 1) |
be06a3df | 1385 | ws->y += mw->menu.margin; |
07bf635f RS |
1386 | |
1387 | size_menu (mw, i); | |
1388 | ||
1389 | fit_to_screen (mw, ws, previous_ws, mw->menu.horizontal && i == 1); | |
1390 | ||
1391 | XClearWindow (XtDisplay (mw), ws->window); | |
1392 | XMoveResizeWindow (XtDisplay (mw), ws->window, ws->x, ws->y, | |
1393 | ws->width, ws->height); | |
1394 | XMapRaised (XtDisplay (mw), ws->window); | |
1395 | display_menu (mw, i, False, &selection_position, NULL, NULL, NULL, NULL); | |
1396 | } | |
1397 | ||
1398 | /* unmap the menus that popped down */ | |
1399 | for (i = new_depth - 1; i < old_depth; i++) | |
1ba46f7d RS |
1400 | if (i >= new_depth || !new_stack[i]->contents) |
1401 | XUnmapWindow (XtDisplay (mw), windows[i].window); | |
07bf635f RS |
1402 | } |
1403 | ||
1404 | static Boolean | |
d398028f PR |
1405 | motion_event_is_in_menu (mw, ev, level, relative_pos) |
1406 | XlwMenuWidget mw; | |
1407 | XMotionEvent* ev; | |
1408 | int level; | |
1409 | XPoint* relative_pos; | |
07bf635f RS |
1410 | { |
1411 | window_state* ws = &mw->menu.windows [level]; | |
4f08464c RS |
1412 | int shadow = level == 0 ? 0 : mw->menu.shadow_thickness; |
1413 | int x = ws->x + shadow; | |
1414 | int y = ws->y + shadow; | |
07bf635f RS |
1415 | relative_pos->x = ev->x_root - x; |
1416 | relative_pos->y = ev->y_root - y; | |
4f08464c RS |
1417 | return (x - shadow < ev->x_root && ev->x_root < x + ws->width |
1418 | && y - shadow < ev->y_root && ev->y_root < y + ws->height); | |
07bf635f RS |
1419 | } |
1420 | ||
1421 | static Boolean | |
d398028f PR |
1422 | map_event_to_widget_value (mw, ev, val, level) |
1423 | XlwMenuWidget mw; | |
1424 | XMotionEvent* ev; | |
1425 | widget_value** val; | |
1426 | int* level; | |
07bf635f RS |
1427 | { |
1428 | int i; | |
1429 | XPoint relative_pos; | |
1430 | window_state* ws; | |
1431 | ||
1432 | *val = NULL; | |
5c520e0a | 1433 | |
07bf635f RS |
1434 | /* Find the window */ |
1435 | for (i = mw->menu.old_depth - 1; i >= 0; i--) | |
1436 | { | |
1437 | ws = &mw->menu.windows [i]; | |
1438 | if (ws && motion_event_is_in_menu (mw, ev, i, &relative_pos)) | |
1439 | { | |
1440 | display_menu (mw, i, True, NULL, &relative_pos, val, NULL, NULL); | |
1441 | ||
1442 | if (*val) | |
1443 | { | |
1444 | *level = i + 1; | |
1445 | return True; | |
1446 | } | |
1447 | } | |
1448 | } | |
1449 | return False; | |
1450 | } | |
1451 | ||
1452 | \f/* Procedures */ | |
1453 | static void | |
d398028f PR |
1454 | make_drawing_gcs (mw) |
1455 | XlwMenuWidget mw; | |
07bf635f RS |
1456 | { |
1457 | XGCValues xgcv; | |
8b71a9ca PJ |
1458 | XColor temp; |
1459 | int delta; | |
07bf635f RS |
1460 | |
1461 | xgcv.font = mw->menu.font->fid; | |
1462 | xgcv.foreground = mw->menu.foreground; | |
1463 | xgcv.background = mw->core.background_pixel; | |
1464 | mw->menu.foreground_gc = XtGetGC ((Widget)mw, | |
1465 | GCFont | GCForeground | GCBackground, | |
1466 | &xgcv); | |
5c520e0a | 1467 | |
07bf635f RS |
1468 | xgcv.font = mw->menu.font->fid; |
1469 | xgcv.foreground = mw->menu.button_foreground; | |
1470 | xgcv.background = mw->core.background_pixel; | |
1471 | mw->menu.button_gc = XtGetGC ((Widget)mw, | |
1472 | GCFont | GCForeground | GCBackground, | |
1473 | &xgcv); | |
5c520e0a | 1474 | |
07bf635f | 1475 | xgcv.font = mw->menu.font->fid; |
07bf635f | 1476 | xgcv.background = mw->core.background_pixel; |
8b71a9ca PJ |
1477 | |
1478 | #define BRIGHTNESS(color) (((color) & 0xff) + (((color) >> 8) & 0xff) + (((color) >> 16) & 0xff)) | |
1479 | ||
1480 | /* Allocate color for disabled menu-items. */ | |
1481 | if (BRIGHTNESS(mw->menu.foreground) < BRIGHTNESS(mw->core.background_pixel)) | |
1482 | { | |
1483 | delta = 2.3; | |
1484 | temp.pixel = mw->menu.foreground; | |
1485 | } | |
1486 | else | |
1487 | { | |
1488 | delta = 1.2; | |
1489 | temp.pixel = mw->core.background_pixel; | |
1490 | } | |
1491 | ||
1492 | x_alloc_lighter_color_for_widget ((Widget) mw, XtDisplay ((Widget) mw), | |
1493 | mw->core.colormap, | |
1494 | &temp.pixel, | |
1495 | delta, | |
1496 | 0x8000); | |
1497 | mw->menu.disabled_foreground = temp.pixel; | |
1498 | ||
1499 | if (mw->menu.foreground == mw->menu.disabled_foreground | |
1500 | || mw->core.background_pixel == mw->menu.disabled_foreground) | |
1501 | { | |
1502 | /* Too few colors, use stipple. */ | |
1503 | xgcv.foreground = mw->menu.foreground; | |
1504 | xgcv.fill_style = FillStippled; | |
1505 | xgcv.stipple = mw->menu.gray_pixmap; | |
1506 | mw->menu.disabled_gc = XtGetGC ((Widget)mw, | |
1507 | (GCFont | GCForeground | GCBackground | |
1508 | | GCFillStyle | GCStipple), &xgcv); | |
1509 | } | |
1510 | else | |
1511 | { | |
1512 | /* Many colors available, use disabled pixel. */ | |
1513 | xgcv.foreground = mw->menu.disabled_foreground; | |
1514 | mw->menu.disabled_gc = XtGetGC ((Widget)mw, | |
1515 | (GCFont | GCForeground | GCBackground), &xgcv); | |
1516 | } | |
5c520e0a | 1517 | |
07bf635f RS |
1518 | xgcv.font = mw->menu.font->fid; |
1519 | xgcv.foreground = mw->menu.button_foreground; | |
1520 | xgcv.background = mw->core.background_pixel; | |
1521 | xgcv.fill_style = FillStippled; | |
1522 | xgcv.stipple = mw->menu.gray_pixmap; | |
1523 | mw->menu.inactive_button_gc = XtGetGC ((Widget)mw, | |
1524 | (GCFont | GCForeground | GCBackground | |
1525 | | GCFillStyle | GCStipple), &xgcv); | |
5c520e0a | 1526 | |
07bf635f RS |
1527 | xgcv.font = mw->menu.font->fid; |
1528 | xgcv.foreground = mw->core.background_pixel; | |
1529 | xgcv.background = mw->menu.foreground; | |
1530 | mw->menu.background_gc = XtGetGC ((Widget)mw, | |
1531 | GCFont | GCForeground | GCBackground, | |
1532 | &xgcv); | |
1533 | } | |
1534 | ||
1535 | static void | |
d398028f PR |
1536 | release_drawing_gcs (mw) |
1537 | XlwMenuWidget mw; | |
07bf635f RS |
1538 | { |
1539 | XtReleaseGC ((Widget) mw, mw->menu.foreground_gc); | |
1540 | XtReleaseGC ((Widget) mw, mw->menu.button_gc); | |
8b71a9ca | 1541 | XtReleaseGC ((Widget) mw, mw->menu.disabled_gc); |
07bf635f RS |
1542 | XtReleaseGC ((Widget) mw, mw->menu.inactive_button_gc); |
1543 | XtReleaseGC ((Widget) mw, mw->menu.background_gc); | |
1544 | /* let's get some segvs if we try to use these... */ | |
1545 | mw->menu.foreground_gc = (GC) -1; | |
1546 | mw->menu.button_gc = (GC) -1; | |
8b71a9ca | 1547 | mw->menu.disabled_gc = (GC) -1; |
07bf635f RS |
1548 | mw->menu.inactive_button_gc = (GC) -1; |
1549 | mw->menu.background_gc = (GC) -1; | |
1550 | } | |
1551 | ||
1552 | #define MINL(x,y) ((((unsigned long) (x)) < ((unsigned long) (y))) \ | |
1553 | ? ((unsigned long) (x)) : ((unsigned long) (y))) | |
1554 | ||
1555 | static void | |
d398028f PR |
1556 | make_shadow_gcs (mw) |
1557 | XlwMenuWidget mw; | |
07bf635f RS |
1558 | { |
1559 | XGCValues xgcv; | |
1560 | unsigned long pm = 0; | |
1561 | Display *dpy = XtDisplay ((Widget) mw); | |
be06a3df | 1562 | Screen *screen = XtScreen ((Widget) mw); |
b0404f9f | 1563 | Colormap cmap = mw->core.colormap; |
07bf635f RS |
1564 | XColor topc, botc; |
1565 | int top_frobbed = 0, bottom_frobbed = 0; | |
1566 | ||
bba2a923 GM |
1567 | mw->menu.free_top_shadow_color_p = 0; |
1568 | mw->menu.free_bottom_shadow_color_p = 0; | |
1569 | ||
07bf635f RS |
1570 | if (mw->menu.top_shadow_color == -1) |
1571 | mw->menu.top_shadow_color = mw->core.background_pixel; | |
bba2a923 GM |
1572 | else |
1573 | mw->menu.top_shadow_color = mw->menu.top_shadow_color; | |
5c520e0a | 1574 | |
07bf635f RS |
1575 | if (mw->menu.bottom_shadow_color == -1) |
1576 | mw->menu.bottom_shadow_color = mw->menu.foreground; | |
bba2a923 GM |
1577 | else |
1578 | mw->menu.bottom_shadow_color = mw->menu.bottom_shadow_color; | |
07bf635f RS |
1579 | |
1580 | if (mw->menu.top_shadow_color == mw->core.background_pixel || | |
1581 | mw->menu.top_shadow_color == mw->menu.foreground) | |
1582 | { | |
1583 | topc.pixel = mw->core.background_pixel; | |
65c0ae05 MB |
1584 | #ifdef emacs |
1585 | if (x_alloc_lighter_color_for_widget ((Widget) mw, dpy, cmap, | |
1586 | &topc.pixel, | |
1587 | 1.2, 0x8000)) | |
1588 | #else | |
07bf635f RS |
1589 | XQueryColor (dpy, cmap, &topc); |
1590 | /* don't overflow/wrap! */ | |
1591 | topc.red = MINL (65535, topc.red * 1.2); | |
1592 | topc.green = MINL (65535, topc.green * 1.2); | |
1593 | topc.blue = MINL (65535, topc.blue * 1.2); | |
1594 | if (XAllocColor (dpy, cmap, &topc)) | |
be06a3df | 1595 | #endif |
07bf635f RS |
1596 | { |
1597 | mw->menu.top_shadow_color = topc.pixel; | |
bba2a923 | 1598 | mw->menu.free_top_shadow_color_p = 1; |
07bf635f RS |
1599 | top_frobbed = 1; |
1600 | } | |
1601 | } | |
1602 | if (mw->menu.bottom_shadow_color == mw->menu.foreground || | |
1603 | mw->menu.bottom_shadow_color == mw->core.background_pixel) | |
1604 | { | |
1605 | botc.pixel = mw->core.background_pixel; | |
65c0ae05 MB |
1606 | #ifdef emacs |
1607 | if (x_alloc_lighter_color_for_widget ((Widget) mw, dpy, cmap, | |
1608 | &botc.pixel, | |
1609 | 0.6, 0x4000)) | |
1610 | #else | |
07bf635f RS |
1611 | XQueryColor (dpy, cmap, &botc); |
1612 | botc.red *= 0.6; | |
1613 | botc.green *= 0.6; | |
1614 | botc.blue *= 0.6; | |
1615 | if (XAllocColor (dpy, cmap, &botc)) | |
be06a3df | 1616 | #endif |
07bf635f RS |
1617 | { |
1618 | mw->menu.bottom_shadow_color = botc.pixel; | |
bba2a923 | 1619 | mw->menu.free_bottom_shadow_color_p = 1; |
07bf635f RS |
1620 | bottom_frobbed = 1; |
1621 | } | |
1622 | } | |
1623 | ||
1624 | if (top_frobbed && bottom_frobbed) | |
1625 | { | |
65c0ae05 | 1626 | if (topc.pixel == botc.pixel) |
07bf635f RS |
1627 | { |
1628 | if (botc.pixel == mw->menu.foreground) | |
fc8fefed | 1629 | { |
bba2a923 GM |
1630 | if (mw->menu.free_top_shadow_color_p) |
1631 | { | |
1632 | x_free_dpy_colors (dpy, screen, cmap, | |
1633 | &mw->menu.top_shadow_color, 1); | |
1634 | mw->menu.free_top_shadow_color_p = 0; | |
1635 | } | |
1636 | mw->menu.top_shadow_color = mw->core.background_pixel; | |
fc8fefed | 1637 | } |
07bf635f | 1638 | else |
fc8fefed | 1639 | { |
bba2a923 GM |
1640 | if (mw->menu.free_bottom_shadow_color_p) |
1641 | { | |
1642 | x_free_dpy_colors (dpy, screen, cmap, | |
1643 | &mw->menu.bottom_shadow_color, 1); | |
1644 | mw->menu.free_bottom_shadow_color_p = 0; | |
1645 | } | |
1646 | mw->menu.bottom_shadow_color = mw->menu.foreground; | |
fc8fefed | 1647 | } |
07bf635f RS |
1648 | } |
1649 | } | |
1650 | ||
1651 | if (!mw->menu.top_shadow_pixmap && | |
1652 | mw->menu.top_shadow_color == mw->core.background_pixel) | |
1653 | { | |
1654 | mw->menu.top_shadow_pixmap = mw->menu.gray_pixmap; | |
bba2a923 GM |
1655 | if (mw->menu.free_top_shadow_color_p) |
1656 | { | |
1657 | x_free_dpy_colors (dpy, screen, cmap, &mw->menu.top_shadow_color, 1); | |
1658 | mw->menu.free_top_shadow_color_p = 0; | |
1659 | } | |
1660 | mw->menu.top_shadow_color = mw->menu.foreground; | |
07bf635f RS |
1661 | } |
1662 | if (!mw->menu.bottom_shadow_pixmap && | |
1663 | mw->menu.bottom_shadow_color == mw->core.background_pixel) | |
1664 | { | |
1665 | mw->menu.bottom_shadow_pixmap = mw->menu.gray_pixmap; | |
bba2a923 GM |
1666 | if (mw->menu.free_bottom_shadow_color_p) |
1667 | { | |
1668 | x_free_dpy_colors (dpy, screen, cmap, | |
1669 | &mw->menu.bottom_shadow_color, 1); | |
1670 | mw->menu.free_bottom_shadow_color_p = 0; | |
1671 | } | |
1672 | mw->menu.bottom_shadow_color = mw->menu.foreground; | |
07bf635f RS |
1673 | } |
1674 | ||
1675 | xgcv.fill_style = FillStippled; | |
1676 | xgcv.foreground = mw->menu.top_shadow_color; | |
1677 | xgcv.stipple = mw->menu.top_shadow_pixmap; | |
1678 | pm = (xgcv.stipple ? GCStipple|GCFillStyle : 0); | |
1679 | mw->menu.shadow_top_gc = XtGetGC ((Widget)mw, GCForeground | pm, &xgcv); | |
1680 | ||
1681 | xgcv.foreground = mw->menu.bottom_shadow_color; | |
1682 | xgcv.stipple = mw->menu.bottom_shadow_pixmap; | |
1683 | pm = (xgcv.stipple ? GCStipple|GCFillStyle : 0); | |
1684 | mw->menu.shadow_bottom_gc = XtGetGC ((Widget)mw, GCForeground | pm, &xgcv); | |
1685 | } | |
1686 | ||
1687 | ||
1688 | static void | |
d398028f PR |
1689 | release_shadow_gcs (mw) |
1690 | XlwMenuWidget mw; | |
07bf635f | 1691 | { |
fc8fefed GM |
1692 | Display *dpy = XtDisplay ((Widget) mw); |
1693 | Screen *screen = XtScreen ((Widget) mw); | |
1694 | Colormap cmap = mw->core.colormap; | |
1695 | Pixel px[2]; | |
bba2a923 GM |
1696 | int i = 0; |
1697 | ||
1698 | if (mw->menu.free_top_shadow_color_p) | |
1699 | px[i++] = mw->menu.top_shadow_color; | |
1700 | if (mw->menu.free_bottom_shadow_color_p) | |
1701 | px[i++] = mw->menu.bottom_shadow_color; | |
1702 | if (i > 0) | |
1703 | x_free_dpy_colors (dpy, screen, cmap, px, i); | |
5c520e0a | 1704 | |
07bf635f RS |
1705 | XtReleaseGC ((Widget) mw, mw->menu.shadow_top_gc); |
1706 | XtReleaseGC ((Widget) mw, mw->menu.shadow_bottom_gc); | |
1707 | } | |
1708 | ||
1709 | static void | |
47d52240 | 1710 | XlwMenuInitialize (request, mw, args, num_args) |
d398028f | 1711 | Widget request; |
47d52240 | 1712 | XlwMenuWidget mw; |
d398028f PR |
1713 | ArgList args; |
1714 | Cardinal *num_args; | |
07bf635f RS |
1715 | { |
1716 | /* Get the GCs and the widget size */ | |
07bf635f RS |
1717 | XSetWindowAttributes xswa; |
1718 | int mask; | |
5c520e0a | 1719 | |
07bf635f RS |
1720 | Window window = RootWindowOfScreen (DefaultScreenOfDisplay (XtDisplay (mw))); |
1721 | Display* display = XtDisplay (mw); | |
5c520e0a | 1722 | |
d398028f PR |
1723 | #if 0 |
1724 | widget_value *tem = (widget_value *) XtMalloc (sizeof (widget_value)); | |
1725 | ||
1726 | /* _XtCreate is freeing the object that was passed to us, | |
1727 | so make a copy that we will actually keep. */ | |
1728 | lwlib_bcopy (mw->menu.contents, tem, sizeof (widget_value)); | |
1729 | mw->menu.contents = tem; | |
1730 | #endif | |
1731 | ||
07bf635f RS |
1732 | /* mw->menu.cursor = XCreateFontCursor (display, mw->menu.cursor_shape); */ |
1733 | mw->menu.cursor = mw->menu.cursor_shape; | |
5c520e0a | 1734 | |
a504c8fa | 1735 | mw->menu.gray_pixmap |
f1c16db4 GM |
1736 | = XCreatePixmapFromBitmapData (display, window, gray_bitmap_bits, |
1737 | gray_bitmap_width, gray_bitmap_height, | |
a504c8fa | 1738 | (unsigned long)1, (unsigned long)0, 1); |
5c520e0a | 1739 | |
47d52240 RS |
1740 | /* I don't understand why this ends up 0 sometimes, |
1741 | but it does. This kludge works around it. | |
1742 | Can anyone find a real fix? -- rms. */ | |
1743 | if (mw->menu.font == 0) | |
1744 | mw->menu.font = xlwmenu_default_font; | |
1745 | ||
07bf635f RS |
1746 | make_drawing_gcs (mw); |
1747 | make_shadow_gcs (mw); | |
5c520e0a | 1748 | |
07bf635f RS |
1749 | xswa.background_pixel = mw->core.background_pixel; |
1750 | xswa.border_pixel = mw->core.border_pixel; | |
1751 | mask = CWBackPixel | CWBorderPixel; | |
5c520e0a | 1752 | |
07bf635f | 1753 | mw->menu.popped_up = False; |
5c520e0a | 1754 | |
07bf635f RS |
1755 | mw->menu.old_depth = 1; |
1756 | mw->menu.old_stack = (widget_value**)XtMalloc (sizeof (widget_value*)); | |
1757 | mw->menu.old_stack_length = 1; | |
1758 | mw->menu.old_stack [0] = mw->menu.contents; | |
5c520e0a | 1759 | |
07bf635f RS |
1760 | mw->menu.new_depth = 0; |
1761 | mw->menu.new_stack = 0; | |
1762 | mw->menu.new_stack_length = 0; | |
1763 | push_new_stack (mw, mw->menu.contents); | |
5c520e0a | 1764 | |
07bf635f RS |
1765 | mw->menu.windows = (window_state*)XtMalloc (sizeof (window_state)); |
1766 | mw->menu.windows_length = 1; | |
1767 | mw->menu.windows [0].x = 0; | |
1768 | mw->menu.windows [0].y = 0; | |
1769 | mw->menu.windows [0].width = 0; | |
1770 | mw->menu.windows [0].height = 0; | |
1771 | size_menu (mw, 0); | |
5c520e0a | 1772 | |
07bf635f RS |
1773 | mw->core.width = mw->menu.windows [0].width; |
1774 | mw->core.height = mw->menu.windows [0].height; | |
1775 | } | |
1776 | ||
1777 | static void | |
1778 | XlwMenuClassInitialize () | |
1779 | { | |
1780 | } | |
1781 | ||
1782 | static void | |
d398028f PR |
1783 | XlwMenuRealize (w, valueMask, attributes) |
1784 | Widget w; | |
1785 | Mask *valueMask; | |
1786 | XSetWindowAttributes *attributes; | |
07bf635f RS |
1787 | { |
1788 | XlwMenuWidget mw = (XlwMenuWidget)w; | |
1789 | XSetWindowAttributes xswa; | |
1790 | int mask; | |
1791 | ||
1792 | (*xlwMenuWidgetClass->core_class.superclass->core_class.realize) | |
1793 | (w, valueMask, attributes); | |
1794 | ||
1795 | xswa.save_under = True; | |
1796 | xswa.cursor = mw->menu.cursor_shape; | |
1797 | mask = CWSaveUnder | CWCursor; | |
1798 | XChangeWindowAttributes (XtDisplay (w), XtWindow (w), mask, &xswa); | |
1799 | ||
1800 | mw->menu.windows [0].window = XtWindow (w); | |
1801 | mw->menu.windows [0].x = w->core.x; | |
1802 | mw->menu.windows [0].y = w->core.y; | |
1803 | mw->menu.windows [0].width = w->core.width; | |
1804 | mw->menu.windows [0].height = w->core.height; | |
1805 | } | |
1806 | ||
1807 | /* Only the toplevel menubar/popup is a widget so it's the only one that | |
1808 | receives expose events through Xt. So we repaint all the other panes | |
1809 | when receiving an Expose event. */ | |
5c520e0a | 1810 | static void |
d398028f PR |
1811 | XlwMenuRedisplay (w, ev, region) |
1812 | Widget w; | |
1813 | XEvent* ev; | |
1814 | Region region; | |
07bf635f RS |
1815 | { |
1816 | XlwMenuWidget mw = (XlwMenuWidget)w; | |
1817 | int i; | |
1818 | ||
d398028f PR |
1819 | /* If we have a depth beyond 1, it's because a submenu was displayed. |
1820 | If the submenu has been destroyed, set the depth back to 1. */ | |
1821 | if (submenu_destroyed) | |
1822 | { | |
1823 | mw->menu.old_depth = 1; | |
1824 | submenu_destroyed = 0; | |
1825 | } | |
1826 | ||
07bf635f RS |
1827 | for (i = 0; i < mw->menu.old_depth; i++) |
1828 | display_menu (mw, i, False, NULL, NULL, NULL, NULL, NULL); | |
1829 | } | |
1830 | ||
f3c9e544 GM |
1831 | |
1832 | /* Part of a hack to make the menu redisplay when a tooltip frame | |
1833 | over a menu item is unmapped. */ | |
1834 | ||
1835 | void | |
1836 | xlwmenu_redisplay (w) | |
1837 | Widget w; | |
1838 | { | |
1839 | XlwMenuRedisplay (w, NULL, None); | |
1840 | } | |
1841 | ||
5c520e0a | 1842 | static void |
d398028f PR |
1843 | XlwMenuDestroy (w) |
1844 | Widget w; | |
07bf635f RS |
1845 | { |
1846 | int i; | |
1847 | XlwMenuWidget mw = (XlwMenuWidget) w; | |
1848 | ||
d398028f | 1849 | if (pointer_grabbed) |
4db7db7d | 1850 | ungrab_all ((Widget)w, CurrentTime); |
d398028f PR |
1851 | pointer_grabbed = 0; |
1852 | ||
1853 | submenu_destroyed = 1; | |
1854 | ||
07bf635f RS |
1855 | release_drawing_gcs (mw); |
1856 | release_shadow_gcs (mw); | |
1857 | ||
1858 | /* this doesn't come from the resource db but is created explicitly | |
1859 | so we must free it ourselves. */ | |
1860 | XFreePixmap (XtDisplay (mw), mw->menu.gray_pixmap); | |
1861 | mw->menu.gray_pixmap = (Pixmap) -1; | |
1862 | ||
d398028f PR |
1863 | #if 0 |
1864 | /* Do free mw->menu.contents because nowadays we copy it | |
1865 | during initialization. */ | |
1866 | XtFree (mw->menu.contents); | |
1867 | #endif | |
1868 | ||
07bf635f RS |
1869 | /* Don't free mw->menu.contents because that comes from our creator. |
1870 | The `*_stack' elements are just pointers into `contents' so leave | |
1871 | that alone too. But free the stacks themselves. */ | |
1872 | if (mw->menu.old_stack) XtFree ((char *) mw->menu.old_stack); | |
1873 | if (mw->menu.new_stack) XtFree ((char *) mw->menu.new_stack); | |
1874 | ||
1875 | /* Remember, you can't free anything that came from the resource | |
1876 | database. This includes: | |
1877 | mw->menu.cursor | |
1878 | mw->menu.top_shadow_pixmap | |
1879 | mw->menu.bottom_shadow_pixmap | |
1880 | mw->menu.font | |
1881 | Also the color cells of top_shadow_color, bottom_shadow_color, | |
1882 | foreground, and button_foreground will never be freed until this | |
1883 | client exits. Nice, eh? | |
1884 | */ | |
1885 | ||
1886 | /* start from 1 because the one in slot 0 is w->core.window */ | |
1887 | for (i = 1; i < mw->menu.windows_length; i++) | |
1888 | XDestroyWindow (XtDisplay (mw), mw->menu.windows [i].window); | |
1889 | if (mw->menu.windows) | |
1890 | XtFree ((char *) mw->menu.windows); | |
1891 | } | |
1892 | ||
5c520e0a | 1893 | static Boolean |
d398028f PR |
1894 | XlwMenuSetValues (current, request, new) |
1895 | Widget current; | |
1896 | Widget request; | |
1897 | Widget new; | |
07bf635f RS |
1898 | { |
1899 | XlwMenuWidget oldmw = (XlwMenuWidget)current; | |
1900 | XlwMenuWidget newmw = (XlwMenuWidget)new; | |
1901 | Boolean redisplay = False; | |
1902 | int i; | |
1903 | ||
1904 | if (newmw->menu.contents | |
1905 | && newmw->menu.contents->contents | |
1906 | && newmw->menu.contents->contents->change >= VISIBLE_CHANGE) | |
1907 | redisplay = True; | |
ba624d0f RS |
1908 | /* Do redisplay if the contents are entirely eliminated. */ |
1909 | if (newmw->menu.contents | |
1910 | && newmw->menu.contents->contents == 0 | |
1911 | && newmw->menu.contents->change >= VISIBLE_CHANGE) | |
1912 | redisplay = True; | |
07bf635f RS |
1913 | |
1914 | if (newmw->core.background_pixel != oldmw->core.background_pixel | |
d398028f PR |
1915 | || newmw->menu.foreground != oldmw->menu.foreground |
1916 | || newmw->menu.font != oldmw->menu.font) | |
07bf635f RS |
1917 | { |
1918 | release_drawing_gcs (newmw); | |
1919 | make_drawing_gcs (newmw); | |
90c7e9f0 MB |
1920 | |
1921 | release_shadow_gcs (newmw); | |
1922 | /* Cause the shadow colors to be recalculated. */ | |
1923 | newmw->menu.top_shadow_color = -1; | |
1924 | newmw->menu.bottom_shadow_color = -1; | |
1925 | make_shadow_gcs (newmw); | |
1926 | ||
07bf635f | 1927 | redisplay = True; |
5c520e0a | 1928 | |
43aa2f1b | 1929 | if (XtIsRealized (current)) |
da353f23 MB |
1930 | /* If the menu is currently displayed, change the display. */ |
1931 | for (i = 0; i < oldmw->menu.windows_length; i++) | |
1932 | { | |
1933 | XSetWindowBackground (XtDisplay (oldmw), | |
1934 | oldmw->menu.windows [i].window, | |
1935 | newmw->core.background_pixel); | |
1936 | /* clear windows and generate expose events */ | |
1937 | XClearArea (XtDisplay (oldmw), oldmw->menu.windows[i].window, | |
1938 | 0, 0, 0, 0, True); | |
1939 | } | |
07bf635f RS |
1940 | } |
1941 | ||
1942 | return redisplay; | |
1943 | } | |
1944 | ||
5c520e0a | 1945 | static void |
d398028f PR |
1946 | XlwMenuResize (w) |
1947 | Widget w; | |
07bf635f RS |
1948 | { |
1949 | XlwMenuWidget mw = (XlwMenuWidget)w; | |
1950 | ||
d398028f PR |
1951 | if (mw->menu.popped_up) |
1952 | { | |
1953 | /* Don't allow the popup menu to resize itself. */ | |
1954 | mw->core.width = mw->menu.windows [0].width; | |
1955 | mw->core.height = mw->menu.windows [0].height; | |
1956 | mw->core.parent->core.width = mw->core.width ; | |
1957 | mw->core.parent->core.height = mw->core.height ; | |
1958 | } | |
1959 | else | |
1960 | { | |
1961 | mw->menu.windows [0].width = mw->core.width; | |
1962 | mw->menu.windows [0].height = mw->core.height; | |
1963 | } | |
07bf635f RS |
1964 | } |
1965 | ||
1966 | \f/* Action procedures */ | |
1967 | static void | |
d398028f PR |
1968 | handle_single_motion_event (mw, ev) |
1969 | XlwMenuWidget mw; | |
1970 | XMotionEvent* ev; | |
07bf635f RS |
1971 | { |
1972 | widget_value* val; | |
1973 | int level; | |
1974 | ||
1975 | if (!map_event_to_widget_value (mw, ev, &val, &level)) | |
1976 | pop_new_stack_if_no_contents (mw); | |
1977 | else | |
1978 | set_new_state (mw, val, level); | |
1979 | remap_menubar (mw); | |
5c520e0a | 1980 | |
07bf635f RS |
1981 | /* Sync with the display. Makes it feel better on X terms. */ |
1982 | XSync (XtDisplay (mw), False); | |
1983 | } | |
1984 | ||
1985 | static void | |
d398028f PR |
1986 | handle_motion_event (mw, ev) |
1987 | XlwMenuWidget mw; | |
1988 | XMotionEvent* ev; | |
07bf635f RS |
1989 | { |
1990 | int x = ev->x_root; | |
1991 | int y = ev->y_root; | |
1992 | int state = ev->state; | |
1993 | ||
1994 | handle_single_motion_event (mw, ev); | |
1995 | ||
1996 | /* allow motion events to be generated again */ | |
1997 | if (ev->is_hint | |
1998 | && XQueryPointer (XtDisplay (mw), ev->window, | |
1999 | &ev->root, &ev->subwindow, | |
2000 | &ev->x_root, &ev->y_root, | |
2001 | &ev->x, &ev->y, | |
2002 | &ev->state) | |
2003 | && ev->state == state | |
2004 | && (ev->x_root != x || ev->y_root != y)) | |
2005 | handle_single_motion_event (mw, ev); | |
2006 | } | |
2007 | ||
5c520e0a | 2008 | static void |
d398028f PR |
2009 | Start (w, ev, params, num_params) |
2010 | Widget w; | |
2011 | XEvent *ev; | |
2012 | String *params; | |
2013 | Cardinal *num_params; | |
07bf635f RS |
2014 | { |
2015 | XlwMenuWidget mw = (XlwMenuWidget)w; | |
2016 | ||
d398028f PR |
2017 | if (!mw->menu.popped_up) |
2018 | { | |
2019 | menu_post_event = *ev; | |
a57a1605 | 2020 | pop_up_menu (mw, (XButtonPressedEvent*) ev); |
d398028f PR |
2021 | } |
2022 | else | |
4cc76151 PR |
2023 | { |
2024 | /* If we push a button while the menu is posted semipermanently, | |
2025 | releasing the button should always pop the menu down. */ | |
2026 | next_release_must_exit = 1; | |
d398028f | 2027 | |
4cc76151 PR |
2028 | /* notes the absolute position of the menubar window */ |
2029 | mw->menu.windows [0].x = ev->xmotion.x_root - ev->xmotion.x; | |
2030 | mw->menu.windows [0].y = ev->xmotion.y_root - ev->xmotion.y; | |
07bf635f | 2031 | |
4cc76151 PR |
2032 | /* handles the down like a move, slots are compatible */ |
2033 | handle_motion_event (mw, &ev->xmotion); | |
2034 | } | |
07bf635f RS |
2035 | } |
2036 | ||
5c520e0a | 2037 | static void |
d398028f PR |
2038 | Drag (w, ev, params, num_params) |
2039 | Widget w; | |
2040 | XEvent *ev; | |
2041 | String *params; | |
2042 | Cardinal *num_params; | |
07bf635f RS |
2043 | { |
2044 | XlwMenuWidget mw = (XlwMenuWidget)w; | |
3c9ce1c4 KH |
2045 | if (mw->menu.popped_up) |
2046 | handle_motion_event (mw, &ev->xmotion); | |
07bf635f RS |
2047 | } |
2048 | ||
ba624d0f RS |
2049 | /* Do nothing. |
2050 | This is how we handle presses and releases of modifier keys. */ | |
2051 | static void | |
2052 | Nothing (w, ev, params, num_params) | |
2053 | Widget w; | |
2054 | XEvent *ev; | |
2055 | String *params; | |
2056 | Cardinal *num_params; | |
2057 | { | |
2058 | } | |
2059 | ||
1b8d91ab PJ |
2060 | widget_value * |
2061 | find_first_selectable (mw, item) | |
2062 | XlwMenuWidget mw; | |
2063 | widget_value *item; | |
2064 | { | |
2065 | widget_value *current = item; | |
2066 | enum menu_separator separator; | |
2067 | ||
2068 | while (lw_separator_p (current->name, &separator, 0) || !current->enabled) | |
2069 | if (current->next) | |
2070 | current=current->next; | |
2071 | else | |
2072 | return NULL; | |
2073 | ||
2074 | return current; | |
2075 | } | |
2076 | ||
a7e19a26 PJ |
2077 | widget_value * |
2078 | find_next_selectable (mw, item) | |
2079 | XlwMenuWidget mw; | |
2080 | widget_value *item; | |
2081 | { | |
2082 | widget_value *current = item; | |
2083 | enum menu_separator separator; | |
2084 | ||
2085 | while (current->next && (current=current->next) && | |
2086 | (lw_separator_p (current->name, &separator, 0) || !current->enabled)) | |
2087 | ; | |
2088 | ||
2089 | if (current == item) | |
2090 | { | |
8b71a9ca PJ |
2091 | if (mw->menu.old_depth < 2) |
2092 | return current; | |
a7e19a26 PJ |
2093 | current = mw->menu.old_stack [mw->menu.old_depth - 2]->contents; |
2094 | ||
2095 | while (lw_separator_p (current->name, &separator, 0) || !current->enabled) | |
1b8d91ab PJ |
2096 | { |
2097 | if (current->next) | |
2098 | current=current->next; | |
2099 | ||
2100 | if (current == item) | |
2101 | break; | |
2102 | } | |
2103 | ||
a7e19a26 PJ |
2104 | } |
2105 | ||
2106 | return current; | |
2107 | } | |
2108 | ||
2109 | widget_value * | |
2110 | find_prev_selectable (mw, item) | |
2111 | XlwMenuWidget mw; | |
2112 | widget_value *item; | |
2113 | { | |
2114 | widget_value *current = item; | |
2115 | widget_value *prev = item; | |
2116 | ||
2117 | while ((current=find_next_selectable (mw, current)) != item) | |
1b8d91ab PJ |
2118 | { |
2119 | if (prev == current) | |
2120 | break; | |
a7e19a26 | 2121 | prev=current; |
1b8d91ab | 2122 | } |
a7e19a26 PJ |
2123 | |
2124 | return prev; | |
2125 | } | |
2126 | ||
2127 | static void | |
2128 | Down (w, ev, params, num_params) | |
2129 | Widget w; | |
2130 | XEvent *ev; | |
2131 | String *params; | |
2132 | Cardinal *num_params; | |
2133 | { | |
2134 | XlwMenuWidget mw = (XlwMenuWidget) w; | |
2135 | widget_value* selected_item = mw->menu.old_stack [mw->menu.old_depth - 1]; | |
2136 | ||
2137 | /* Inside top-level menu-bar? */ | |
2138 | if (mw->menu.old_depth == 2) | |
2139 | /* When <down> in the menu-bar is pressed, display the corresponding | |
1b8d91ab PJ |
2140 | sub-menu and select the first selectable menu item there. */ |
2141 | set_new_state (mw, find_first_selectable (mw, selected_item->contents), mw->menu.old_depth); | |
a7e19a26 PJ |
2142 | else |
2143 | /* Highlight next possible (enabled and not separator) menu item. */ | |
2144 | set_new_state (mw, find_next_selectable (mw, selected_item), mw->menu.old_depth - 1); | |
2145 | ||
2146 | remap_menubar (mw); | |
2147 | } | |
2148 | ||
2149 | static void | |
2150 | Up (w, ev, params, num_params) | |
2151 | Widget w; | |
2152 | XEvent *ev; | |
2153 | String *params; | |
2154 | Cardinal *num_params; | |
2155 | { | |
2156 | XlwMenuWidget mw = (XlwMenuWidget) w; | |
2157 | widget_value* selected_item = mw->menu.old_stack [mw->menu.old_depth - 1]; | |
2158 | ||
2159 | /* Inside top-level menu-bar? */ | |
2160 | if (mw->menu.old_depth == 2) | |
2161 | { | |
2162 | /* FIXME: this is tricky. <up> in the menu-bar should select the | |
1b8d91ab PJ |
2163 | last selectable item in the list. So we select the first |
2164 | selectable one and find the previous selectable item. Is there | |
2165 | a better way? */ | |
2166 | set_new_state (mw, find_first_selectable (mw, selected_item->contents), mw->menu.old_depth); | |
a7e19a26 PJ |
2167 | remap_menubar (mw); |
2168 | selected_item = mw->menu.old_stack [mw->menu.old_depth - 1]; | |
2169 | set_new_state (mw, find_prev_selectable (mw, selected_item), mw->menu.old_depth - 1); | |
2170 | } | |
2171 | else | |
2172 | /* Highlight previous (enabled and not separator) menu item. */ | |
2173 | set_new_state (mw, find_prev_selectable (mw, selected_item), mw->menu.old_depth - 1); | |
2174 | ||
2175 | remap_menubar (mw); | |
2176 | } | |
2177 | ||
2178 | static void | |
2179 | Left (w, ev, params, num_params) | |
2180 | Widget w; | |
2181 | XEvent *ev; | |
2182 | String *params; | |
2183 | Cardinal *num_params; | |
2184 | { | |
2185 | XlwMenuWidget mw = (XlwMenuWidget) w; | |
2186 | widget_value* selected_item = mw->menu.old_stack [mw->menu.old_depth - 1]; | |
2187 | ||
2188 | /* Inside top-level menu-bar? */ | |
2189 | if (mw->menu.old_depth == 2) | |
2190 | /* When <left> in the menu-bar is pressed, display the previous item on | |
2191 | the menu-bar. If the current item is the first one, highlight the | |
2192 | last item in the menubar (probably Help). */ | |
2193 | set_new_state (mw, find_prev_selectable (mw, selected_item), mw->menu.old_depth - 1); | |
2194 | else | |
2195 | { | |
2196 | pop_new_stack_if_no_contents (mw); | |
2197 | set_new_state (mw, mw->menu.old_stack [mw->menu.old_depth - 2], mw->menu.old_depth - 2); | |
2198 | } | |
2199 | ||
2200 | remap_menubar (mw); | |
2201 | } | |
2202 | ||
2203 | static void | |
2204 | Right (w, ev, params, num_params) | |
2205 | Widget w; | |
2206 | XEvent *ev; | |
2207 | String *params; | |
2208 | Cardinal *num_params; | |
2209 | { | |
2210 | XlwMenuWidget mw = (XlwMenuWidget) w; | |
2211 | widget_value* selected_item = mw->menu.old_stack [mw->menu.old_depth - 1]; | |
2212 | ||
2213 | /* Inside top-level menu-bar? */ | |
2214 | if (mw->menu.old_depth == 2) | |
2215 | /* When <right> in the menu-bar is pressed, display the next item on | |
2216 | the menu-bar. If the current item is the last one, highlight the | |
2217 | first item (probably File). */ | |
2218 | set_new_state (mw, find_next_selectable (mw, selected_item), mw->menu.old_depth - 1); | |
2219 | else if (selected_item->contents) /* Is this menu item expandable? */ | |
1b8d91ab PJ |
2220 | { |
2221 | set_new_state (mw, selected_item->contents, mw->menu.old_depth); | |
2222 | remap_menubar (mw); | |
2223 | selected_item = mw->menu.old_stack [mw->menu.old_depth - 1]; | |
2224 | if (!selected_item->enabled && find_first_selectable (mw, selected_item)) | |
2225 | set_new_state (mw, find_first_selectable (mw, selected_item), mw->menu.old_depth - 1); | |
2226 | } | |
a7e19a26 PJ |
2227 | else |
2228 | { | |
2229 | pop_new_stack_if_no_contents (mw); | |
2230 | set_new_state (mw, mw->menu.old_stack [mw->menu.old_depth - 2], mw->menu.old_depth - 2); | |
2231 | } | |
2232 | ||
2233 | remap_menubar (mw); | |
2234 | } | |
2235 | ||
ba624d0f RS |
2236 | /* Handle key press and release events while menu is popped up. |
2237 | Our action is to get rid of the menu. */ | |
2238 | static void | |
2239 | Key (w, ev, params, num_params) | |
2240 | Widget w; | |
2241 | XEvent *ev; | |
2242 | String *params; | |
2243 | Cardinal *num_params; | |
2244 | { | |
2245 | XlwMenuWidget mw = (XlwMenuWidget)w; | |
2246 | ||
2247 | /* Pop down everything. */ | |
2248 | mw->menu.new_depth = 1; | |
2249 | remap_menubar (mw); | |
2250 | ||
2251 | if (mw->menu.popped_up) | |
2252 | { | |
2253 | mw->menu.popped_up = False; | |
4db7db7d | 2254 | ungrab_all ((Widget)mw, ev->xmotion.time); |
ba624d0f RS |
2255 | if (XtIsShell (XtParent ((Widget) mw))) |
2256 | XtPopdown (XtParent ((Widget) mw)); | |
2257 | else | |
2258 | { | |
2259 | XtRemoveGrab ((Widget) mw); | |
2260 | display_menu (mw, 0, False, NULL, NULL, NULL, NULL, NULL); | |
2261 | } | |
2262 | } | |
2263 | ||
2264 | /* callback */ | |
2265 | XtCallCallbackList ((Widget)mw, mw->menu.select, (XtPointer)0); | |
2266 | } | |
2267 | ||
2268 | static void | |
d398028f PR |
2269 | Select (w, ev, params, num_params) |
2270 | Widget w; | |
2271 | XEvent *ev; | |
2272 | String *params; | |
2273 | Cardinal *num_params; | |
07bf635f RS |
2274 | { |
2275 | XlwMenuWidget mw = (XlwMenuWidget)w; | |
2276 | widget_value* selected_item = mw->menu.old_stack [mw->menu.old_depth - 1]; | |
5c520e0a | 2277 | |
d398028f PR |
2278 | /* If user releases the button quickly, without selecting anything, |
2279 | after the initial down-click that brought the menu up, | |
2280 | do nothing. */ | |
2281 | if ((selected_item == 0 | |
2282 | || ((widget_value *) selected_item)->call_data == 0) | |
2283 | && !next_release_must_exit | |
2284 | && (ev->xbutton.time - menu_post_event.xbutton.time | |
2285 | < XtGetMultiClickTime (XtDisplay (w)))) | |
2286 | return; | |
2287 | ||
2288 | /* pop down everything. */ | |
07bf635f RS |
2289 | mw->menu.new_depth = 1; |
2290 | remap_menubar (mw); | |
2291 | ||
2292 | if (mw->menu.popped_up) | |
2293 | { | |
2294 | mw->menu.popped_up = False; | |
4db7db7d | 2295 | ungrab_all ((Widget)mw, ev->xmotion.time); |
2289f3f4 PR |
2296 | if (XtIsShell (XtParent ((Widget) mw))) |
2297 | XtPopdown (XtParent ((Widget) mw)); | |
4cc76151 PR |
2298 | else |
2299 | { | |
2300 | XtRemoveGrab ((Widget) mw); | |
2301 | display_menu (mw, 0, False, NULL, NULL, NULL, NULL, NULL); | |
2302 | } | |
07bf635f RS |
2303 | } |
2304 | ||
2305 | /* callback */ | |
2306 | XtCallCallbackList ((Widget)mw, mw->menu.select, (XtPointer)selected_item); | |
07bf635f RS |
2307 | } |
2308 | ||
2309 | ||
2310 | \f/* Special code to pop-up a menu */ | |
2311 | void | |
d398028f PR |
2312 | pop_up_menu (mw, event) |
2313 | XlwMenuWidget mw; | |
2314 | XButtonPressedEvent* event; | |
07bf635f RS |
2315 | { |
2316 | int x = event->x_root; | |
2317 | int y = event->y_root; | |
2318 | int w; | |
2319 | int h; | |
2320 | int borderwidth = mw->menu.shadow_thickness; | |
2321 | Screen* screen = XtScreen (mw); | |
8400b9ed | 2322 | Display *display = XtDisplay (mw); |
5efb61c7 | 2323 | int count; |
07bf635f | 2324 | |
d398028f PR |
2325 | next_release_must_exit = 0; |
2326 | ||
07bf635f RS |
2327 | XtCallCallbackList ((Widget)mw, mw->menu.open, NULL); |
2328 | ||
2289f3f4 | 2329 | if (XtIsShell (XtParent ((Widget)mw))) |
4cc76151 | 2330 | size_menu (mw, 0); |
07bf635f RS |
2331 | |
2332 | w = mw->menu.windows [0].width; | |
2333 | h = mw->menu.windows [0].height; | |
2334 | ||
2335 | x -= borderwidth; | |
2336 | y -= borderwidth; | |
2337 | if (x < borderwidth) | |
2338 | x = borderwidth; | |
2339 | if (x + w + 2 * borderwidth > WidthOfScreen (screen)) | |
2340 | x = WidthOfScreen (screen) - w - 2 * borderwidth; | |
2341 | if (y < borderwidth) | |
2342 | y = borderwidth; | |
2343 | if (y + h + 2 * borderwidth> HeightOfScreen (screen)) | |
2344 | y = HeightOfScreen (screen) - h - 2 * borderwidth; | |
2345 | ||
2346 | mw->menu.popped_up = True; | |
2289f3f4 | 2347 | if (XtIsShell (XtParent ((Widget)mw))) |
4cc76151 | 2348 | { |
2289f3f4 PR |
2349 | XtConfigureWidget (XtParent ((Widget)mw), x, y, w, h, |
2350 | XtParent ((Widget)mw)->core.border_width); | |
2351 | XtPopup (XtParent ((Widget)mw), XtGrabExclusive); | |
4cc76151 PR |
2352 | display_menu (mw, 0, False, NULL, NULL, NULL, NULL, NULL); |
2353 | mw->menu.windows [0].x = x + borderwidth; | |
2354 | mw->menu.windows [0].y = y + borderwidth; | |
2355 | } | |
2356 | else | |
2357 | { | |
2358 | XEvent *ev = (XEvent *) event; | |
2359 | ||
87a559bf | 2360 | XtAddGrab ((Widget) mw, True, True); |
4cc76151 PR |
2361 | |
2362 | /* notes the absolute position of the menubar window */ | |
2363 | mw->menu.windows [0].x = ev->xmotion.x_root - ev->xmotion.x; | |
2364 | mw->menu.windows [0].y = ev->xmotion.y_root - ev->xmotion.y; | |
2365 | } | |
2366 | ||
d398028f | 2367 | #ifdef emacs |
5efb61c7 | 2368 | count = x_catch_errors (display); |
d398028f | 2369 | #endif |
4db7db7d JD |
2370 | if (XtGrabPointer ((Widget)mw, False, |
2371 | (PointerMotionMask | |
2372 | | PointerMotionHintMask | |
2373 | | ButtonReleaseMask | |
2374 | | ButtonPressMask), | |
2375 | GrabModeAsync, GrabModeAsync, None, | |
2376 | mw->menu.cursor_shape, | |
2377 | event->time) == Success) | |
2378 | { | |
005e0d57 | 2379 | if (! GRAB_KEYBOARD |
4db7db7d JD |
2380 | || XtGrabKeyboard ((Widget)mw, False, GrabModeAsync, |
2381 | GrabModeAsync, event->time) == Success) | |
2382 | { | |
2383 | XtSetKeyboardFocus((Widget)mw, None); | |
2384 | pointer_grabbed = 1; | |
2385 | } | |
2386 | else | |
2387 | XtUngrabPointer ((Widget)mw, event->time); | |
2388 | } | |
2389 | ||
d398028f | 2390 | #ifdef emacs |
8400b9ed | 2391 | if (x_had_errors_p (display)) |
d398028f PR |
2392 | { |
2393 | pointer_grabbed = 0; | |
2394 | XtUngrabPointer ((Widget)mw, event->time); | |
2395 | } | |
5efb61c7 | 2396 | x_uncatch_errors (display, count); |
d398028f | 2397 | #endif |
07bf635f | 2398 | |
07bf635f RS |
2399 | handle_motion_event (mw, (XMotionEvent*)event); |
2400 | } |