Commit | Line | Data |
---|---|---|
07bf635f RS |
1 | /* Implements a lightweight menubar widget. |
2 | Copyright (C) 1992 Lucid, Inc. | |
3 | ||
4 | This file is part of the Lucid Widget Library. | |
5 | ||
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) | |
9 | any later version. | |
10 | ||
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. | |
15 | ||
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. */ | |
19 | ||
20 | /* Created by devin@lucid.com */ | |
21 | ||
22 | #include <stdlib.h> | |
23 | #include <unistd.h> | |
24 | #include <string.h> | |
25 | #include <stdio.h> | |
26 | ||
27 | #include <sys/types.h> | |
28 | #include <X11/Xos.h> | |
29 | #include <X11/IntrinsicP.h> | |
30 | #include <X11/StringDefs.h> | |
31 | #include <X11/cursorfont.h> | |
32 | #include <X11/bitmaps/gray> | |
33 | #include "xlwmenuP.h" | |
34 | #include <string.h> | |
35 | ||
36 | static char | |
37 | xlwMenuTranslations [] = | |
38 | "<BtnDown>: start()\n\ | |
39 | <BtnMotion>: drag()\n\ | |
40 | <BtnUp>: select()\n\ | |
41 | "; | |
42 | ||
43 | #define offset(field) XtOffset(XlwMenuWidget, field) | |
44 | static XtResource | |
45 | xlwMenuResources[] = | |
46 | { | |
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}, | |
61 | ||
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}, | |
73 | ||
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}, | |
84 | }; | |
85 | #undef offset | |
86 | ||
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(); | |
95 | static void Start(); | |
96 | static void Drag(); | |
97 | static void Select(); | |
98 | ||
99 | static XtActionsRec | |
100 | xlwMenuActionsList [] = | |
101 | { | |
102 | {"start", Start}, | |
103 | {"drag", Drag}, | |
104 | {"select", Select}, | |
105 | }; | |
106 | ||
107 | #define SuperClass ((CoreWidgetClass)&coreClassRec) | |
108 | ||
109 | XlwMenuClassRec xlwMenuClassRec = | |
110 | { | |
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 */ | |
143 | NULL /* extension */ | |
144 | }, /* XlwMenuClass fields initialization */ | |
145 | { | |
146 | 0 /* dummy */ | |
147 | }, | |
148 | }; | |
149 | ||
150 | WidgetClass xlwMenuWidgetClass = (WidgetClass) &xlwMenuClassRec; | |
151 | ||
152 | \f/* Utilities */ | |
153 | static void | |
154 | push_new_stack (XlwMenuWidget mw, widget_value* val) | |
155 | { | |
156 | if (!mw->menu.new_stack) | |
157 | { | |
158 | mw->menu.new_stack_length = 10; | |
159 | mw->menu.new_stack = | |
160 | (widget_value**)XtCalloc (mw->menu.new_stack_length, | |
161 | sizeof (widget_value*)); | |
162 | } | |
163 | else if (mw->menu.new_depth == mw->menu.new_stack_length) | |
164 | { | |
165 | mw->menu.new_stack_length *= 2; | |
166 | mw->menu.new_stack = | |
167 | (widget_value**)XtRealloc ((char*)mw->menu.new_stack, | |
168 | mw->menu.new_stack_length * sizeof (widget_value*)); | |
169 | } | |
170 | mw->menu.new_stack [mw->menu.new_depth++] = val; | |
171 | } | |
172 | ||
173 | static void | |
174 | pop_new_stack_if_no_contents (XlwMenuWidget mw) | |
175 | { | |
176 | if (mw->menu.new_depth) | |
177 | { | |
178 | if (!mw->menu.new_stack [mw->menu.new_depth - 1]->contents) | |
179 | mw->menu.new_depth -= 1; | |
180 | } | |
181 | } | |
182 | ||
183 | static void | |
184 | make_old_stack_space (XlwMenuWidget mw, int n) | |
185 | { | |
186 | if (!mw->menu.old_stack) | |
187 | { | |
188 | mw->menu.old_stack_length = 10; | |
189 | mw->menu.old_stack = | |
190 | (widget_value**)XtCalloc (mw->menu.old_stack_length, | |
191 | sizeof (widget_value*)); | |
192 | } | |
193 | else if (mw->menu.old_stack_length < n) | |
194 | { | |
195 | mw->menu.old_stack_length *= 2; | |
196 | mw->menu.old_stack = | |
197 | (widget_value**)XtRealloc ((char*)mw->menu.old_stack, | |
198 | mw->menu.old_stack_length * sizeof (widget_value*)); | |
199 | } | |
200 | } | |
201 | ||
202 | \f/* Size code */ | |
203 | static Boolean | |
204 | all_dashes_p (char* s) | |
205 | { | |
206 | char* p; | |
207 | for (p = s; *p == '-'; p++); | |
208 | return !*p; | |
209 | } | |
210 | ||
211 | static int | |
212 | string_width (XlwMenuWidget mw, char* s) | |
213 | { | |
214 | XCharStruct xcs; | |
215 | int drop; | |
216 | ||
217 | XTextExtents (mw->menu.font, s, strlen (s), &drop, &drop, &drop, &xcs); | |
218 | return xcs.width; | |
219 | } | |
220 | ||
221 | static int | |
222 | arrow_width (XlwMenuWidget mw) | |
223 | { | |
224 | return mw->menu.font->ascent / 2 | 1; | |
225 | } | |
226 | ||
227 | static XtResource | |
228 | nameResource[] = | |
229 | { | |
230 | {"labelString", "LabelString", XtRString, sizeof(String), | |
231 | 0, XtRImmediate, 0}, | |
232 | }; | |
233 | ||
234 | static char* | |
235 | resource_widget_value (XlwMenuWidget mw, widget_value* val) | |
236 | { | |
237 | if (!val->toolkit_data) | |
238 | { | |
239 | char* resourced_name = NULL; | |
240 | char* complete_name; | |
241 | XtGetSubresources ((Widget) mw, | |
242 | (XtPointer) &resourced_name, | |
243 | val->name, val->name, | |
244 | nameResource, 1, NULL, 0); | |
245 | if (!resourced_name) | |
246 | resourced_name = val->name; | |
247 | if (!val->value) | |
d97342f3 RS |
248 | { |
249 | complete_name = (char *) XtMalloc (strlen (resourced_name) + 1); | |
250 | strcpy (complete_name, resourced_name); | |
251 | } | |
07bf635f RS |
252 | else |
253 | { | |
254 | int complete_length = | |
255 | strlen (resourced_name) + strlen (val->value) + 2; | |
256 | complete_name = XtMalloc (complete_length); | |
257 | *complete_name = 0; | |
258 | strcat (complete_name, resourced_name); | |
259 | strcat (complete_name, " "); | |
260 | strcat (complete_name, val->value); | |
261 | } | |
262 | ||
263 | val->toolkit_data = complete_name; | |
264 | val->free_toolkit_data = True; | |
265 | } | |
266 | return (char*)val->toolkit_data; | |
267 | } | |
268 | ||
269 | /* Returns the sizes of an item */ | |
270 | static void | |
271 | size_menu_item (XlwMenuWidget mw, widget_value* val, int horizontal_p, | |
272 | int* label_width, int* rest_width, int* height) | |
273 | { | |
274 | if (all_dashes_p (val->name)) | |
275 | { | |
276 | *height = 2; | |
277 | *label_width = 1; | |
278 | *rest_width = 0; | |
279 | } | |
280 | else | |
281 | { | |
282 | *height = | |
283 | mw->menu.font->ascent + mw->menu.font->descent | |
284 | + 2 * mw->menu.vertical_spacing + 2 * mw->menu.shadow_thickness; | |
285 | ||
286 | *label_width = | |
287 | string_width (mw, resource_widget_value (mw, val)) | |
288 | + mw->menu.horizontal_spacing + mw->menu.shadow_thickness; | |
289 | ||
290 | *rest_width = mw->menu.horizontal_spacing + mw->menu.shadow_thickness; | |
291 | if (!horizontal_p) | |
292 | { | |
293 | if (val->contents) | |
294 | *rest_width += arrow_width (mw) + mw->menu.arrow_spacing; | |
295 | else if (val->key) | |
296 | *rest_width += | |
297 | string_width (mw, val->key) + mw->menu.arrow_spacing; | |
298 | } | |
299 | } | |
300 | } | |
301 | ||
302 | static void | |
303 | size_menu (XlwMenuWidget mw, int level) | |
304 | { | |
305 | int label_width = 0; | |
306 | int rest_width = 0; | |
307 | int max_rest_width = 0; | |
308 | int height = 0; | |
309 | int horizontal_p = mw->menu.horizontal && (level == 0); | |
310 | widget_value* val; | |
311 | window_state* ws; | |
312 | ||
313 | if (level >= mw->menu.old_depth) | |
314 | abort (); | |
315 | ||
316 | ws = &mw->menu.windows [level]; | |
317 | ws->width = 0; | |
318 | ws->height = 0; | |
319 | ws->label_width = 0; | |
320 | ||
321 | for (val = mw->menu.old_stack [level]->contents; val; val = val->next) | |
322 | { | |
323 | size_menu_item (mw, val, horizontal_p, &label_width, &rest_width, | |
324 | &height); | |
325 | if (horizontal_p) | |
326 | { | |
327 | ws->width += label_width + rest_width; | |
328 | if (height > ws->height) | |
329 | ws->height = height; | |
330 | } | |
331 | else | |
332 | { | |
333 | if (label_width > ws->label_width) | |
334 | ws->label_width = label_width; | |
335 | if (rest_width > max_rest_width) | |
336 | max_rest_width = rest_width; | |
337 | ws->height += height; | |
338 | } | |
339 | } | |
340 | ||
341 | if (horizontal_p) | |
342 | ws->label_width = 0; | |
343 | else | |
344 | ws->width = ws->label_width + max_rest_width; | |
345 | ||
346 | ws->width += 2 * mw->menu.shadow_thickness; | |
347 | ws->height += 2 * mw->menu.shadow_thickness; | |
348 | } | |
349 | ||
350 | ||
351 | \f/* Display code */ | |
352 | static void | |
353 | draw_arrow (XlwMenuWidget mw, Window window, GC gc, int x, int y, int width) | |
354 | { | |
355 | XPoint points [3]; | |
356 | points [0].x = x; | |
357 | points [0].y = y + mw->menu.font->ascent; | |
358 | points [1].x = x; | |
359 | points [1].y = y; | |
360 | points [2].x = x + width; | |
361 | points [2].y = y + mw->menu.font->ascent / 2; | |
362 | ||
363 | XFillPolygon (XtDisplay (mw), window, gc, points, 3, Convex, | |
364 | CoordModeOrigin); | |
365 | } | |
366 | ||
367 | static void | |
368 | draw_shadow_rectangle (XlwMenuWidget mw, Window window, | |
369 | int x, int y, int width, int height, int erase_p) | |
370 | { | |
371 | Display *dpy = XtDisplay (mw); | |
372 | GC top_gc = !erase_p ? mw->menu.shadow_top_gc : mw->menu.background_gc; | |
373 | GC bottom_gc = !erase_p ? mw->menu.shadow_bottom_gc : mw->menu.background_gc; | |
374 | int thickness = mw->menu.shadow_thickness; | |
375 | XPoint points [4]; | |
376 | points [0].x = x; | |
377 | points [0].y = y; | |
378 | points [1].x = x + width; | |
379 | points [1].y = y; | |
380 | points [2].x = x + width - thickness; | |
381 | points [2].y = y + thickness; | |
382 | points [3].x = x; | |
383 | points [3].y = y + thickness; | |
384 | XFillPolygon (dpy, window, top_gc, points, 4, Convex, CoordModeOrigin); | |
385 | points [0].x = x; | |
386 | points [0].y = y + thickness; | |
387 | points [1].x = x; | |
388 | points [1].y = y + height; | |
389 | points [2].x = x + thickness; | |
390 | points [2].y = y + height - thickness; | |
391 | points [3].x = x + thickness; | |
392 | points [3].y = y + thickness; | |
393 | XFillPolygon (dpy, window, top_gc, points, 4, Convex, CoordModeOrigin); | |
394 | points [0].x = x + width; | |
395 | points [0].y = y; | |
396 | points [1].x = x + width - thickness; | |
397 | points [1].y = y + thickness; | |
398 | points [2].x = x + width - thickness; | |
399 | points [2].y = y + height - thickness; | |
400 | points [3].x = x + width; | |
401 | points [3].y = y + height - thickness; | |
402 | XFillPolygon (dpy, window, bottom_gc, points, 4, Convex, CoordModeOrigin); | |
403 | points [0].x = x; | |
404 | points [0].y = y + height; | |
405 | points [1].x = x + width; | |
406 | points [1].y = y + height; | |
407 | points [2].x = x + width; | |
408 | points [2].y = y + height - thickness; | |
409 | points [3].x = x + thickness; | |
410 | points [3].y = y + height - thickness; | |
411 | XFillPolygon (dpy, window, bottom_gc, points, 4, Convex, CoordModeOrigin); | |
412 | } | |
413 | ||
414 | ||
415 | /* Display the menu item and increment where.x and where.y to show how large | |
416 | ** the menu item was. | |
417 | */ | |
418 | static void | |
419 | display_menu_item (XlwMenuWidget mw, widget_value* val, window_state* ws, | |
420 | XPoint* where, Boolean highlighted_p, Boolean horizontal_p, | |
421 | Boolean just_compute_p) | |
422 | { | |
423 | GC deco_gc; | |
424 | GC text_gc; | |
425 | int font_ascent = mw->menu.font->ascent; | |
426 | int font_descent = mw->menu.font->descent; | |
427 | int shadow = mw->menu.shadow_thickness; | |
428 | int separator_p = all_dashes_p (val->name); | |
429 | int h_spacing = mw->menu.horizontal_spacing; | |
430 | int v_spacing = mw->menu.vertical_spacing; | |
431 | int label_width; | |
432 | int rest_width; | |
433 | int height; | |
434 | int width; | |
435 | int button_p; | |
436 | ||
437 | /* compute the sizes of the item */ | |
438 | size_menu_item (mw, val, horizontal_p, &label_width, &rest_width, &height); | |
439 | ||
440 | if (horizontal_p) | |
441 | width = label_width + rest_width; | |
442 | else | |
443 | { | |
444 | label_width = ws->label_width; | |
445 | width = ws->width - 2 * shadow; | |
446 | } | |
447 | ||
448 | /* see if it should be a button in the menubar */ | |
449 | button_p = horizontal_p && val->call_data; | |
450 | ||
451 | /* Only highlight an enabled item that has a callback. */ | |
452 | if (highlighted_p) | |
453 | if (!val->enabled || !(val->call_data || val->contents)) | |
454 | highlighted_p = 0; | |
455 | ||
456 | /* do the drawing. */ | |
457 | if (!just_compute_p) | |
458 | { | |
459 | /* Add the shadow border of the containing menu */ | |
460 | int x = where->x + shadow; | |
461 | int y = where->y + shadow; | |
462 | ||
463 | /* pick the foreground and background GC. */ | |
464 | if (val->enabled) | |
465 | text_gc = button_p ? mw->menu.button_gc : mw->menu.foreground_gc; | |
466 | else | |
467 | text_gc = | |
468 | button_p ? mw->menu.inactive_button_gc : mw->menu.inactive_gc; | |
469 | deco_gc = mw->menu.foreground_gc; | |
470 | ||
471 | if (separator_p) | |
472 | { | |
473 | XDrawLine (XtDisplay (mw), ws->window, mw->menu.shadow_bottom_gc, | |
474 | x, y, x + width, y); | |
475 | XDrawLine (XtDisplay (mw), ws->window, mw->menu.shadow_top_gc, | |
476 | x, y + 1, x + width, y + 1); | |
477 | } | |
478 | else | |
479 | { | |
480 | char* display_string = resource_widget_value (mw, val); | |
481 | draw_shadow_rectangle (mw, ws->window, x, y, width, height, True); | |
482 | XDrawString (XtDisplay (mw), ws->window, text_gc, | |
483 | x + h_spacing + shadow, | |
484 | y + v_spacing + shadow + font_ascent, | |
485 | display_string, strlen (display_string)); | |
486 | ||
487 | if (!horizontal_p) | |
488 | { | |
489 | if (val->contents) | |
490 | { | |
491 | int a_w = arrow_width (mw); | |
492 | draw_arrow (mw, ws->window, deco_gc, | |
493 | x + label_width + mw->menu.arrow_spacing, | |
494 | y + v_spacing + shadow, a_w); | |
495 | } | |
496 | else if (val->key) | |
497 | { | |
498 | XDrawString (XtDisplay (mw), ws->window, text_gc, | |
499 | x + label_width + mw->menu.arrow_spacing, | |
500 | y + v_spacing + shadow + font_ascent, | |
501 | val->key, strlen (val->key)); | |
502 | } | |
503 | } | |
504 | ||
505 | else if (button_p) | |
506 | { | |
507 | #if 1 | |
508 | XDrawRectangle (XtDisplay (mw), ws->window, deco_gc, | |
509 | x + shadow, y + shadow, | |
510 | label_width + h_spacing - 1, | |
511 | font_ascent + font_descent + 2 * v_spacing - 1); | |
512 | #else | |
513 | highlighted_p = True; | |
514 | #endif | |
515 | } | |
516 | ||
517 | if (highlighted_p) | |
518 | draw_shadow_rectangle (mw, ws->window, x, y, width, height, False); | |
519 | } | |
520 | } | |
521 | ||
522 | where->x += width; | |
523 | where->y += height; | |
524 | } | |
525 | ||
526 | static void | |
527 | display_menu (XlwMenuWidget mw, int level, Boolean just_compute_p, | |
528 | XPoint* highlighted_pos, XPoint* hit, widget_value** hit_return, | |
529 | widget_value* this, widget_value* that) | |
530 | { | |
531 | widget_value* val; | |
532 | widget_value* following_item; | |
533 | window_state* ws; | |
534 | XPoint where; | |
535 | int horizontal_p = mw->menu.horizontal && (level == 0); | |
536 | int highlighted_p; | |
537 | int just_compute_this_one_p; | |
538 | ||
539 | if (level >= mw->menu.old_depth) | |
540 | abort (); | |
541 | ||
542 | if (level < mw->menu.old_depth - 1) | |
543 | following_item = mw->menu.old_stack [level + 1]; | |
544 | else | |
545 | following_item = NULL; | |
546 | ||
547 | if (hit) | |
548 | *hit_return = NULL; | |
549 | ||
550 | where.x = 0; | |
551 | where.y = 0; | |
552 | ||
553 | ws = &mw->menu.windows [level]; | |
554 | for (val = mw->menu.old_stack [level]->contents; val; val = val->next) | |
555 | { | |
556 | highlighted_p = val == following_item; | |
557 | if (highlighted_p && highlighted_pos) | |
558 | { | |
559 | if (horizontal_p) | |
560 | highlighted_pos->x = where.x; | |
561 | else | |
562 | highlighted_pos->y = where.y; | |
563 | } | |
564 | ||
565 | just_compute_this_one_p = | |
566 | just_compute_p || ((this || that) && val != this && val != that); | |
567 | ||
568 | display_menu_item (mw, val, ws, &where, highlighted_p, horizontal_p, | |
569 | just_compute_this_one_p); | |
570 | ||
571 | if (highlighted_p && highlighted_pos) | |
572 | { | |
573 | if (horizontal_p) | |
574 | highlighted_pos->y = where.y; | |
575 | else | |
576 | highlighted_pos->x = where.x; | |
577 | } | |
578 | ||
579 | if (hit | |
580 | && !*hit_return | |
581 | && (horizontal_p ? hit->x < where.x : hit->y < where.y) | |
582 | && !all_dashes_p (val->name)) | |
583 | *hit_return = val; | |
584 | ||
585 | if (horizontal_p) | |
586 | where.y = 0; | |
587 | else | |
588 | where.x = 0; | |
589 | } | |
590 | ||
591 | if (!just_compute_p) | |
592 | draw_shadow_rectangle (mw, ws->window, 0, 0, ws->width, ws->height, False); | |
593 | } | |
594 | ||
595 | \f/* Motion code */ | |
596 | static void | |
597 | set_new_state (XlwMenuWidget mw, widget_value* val, int level) | |
598 | { | |
599 | int i; | |
600 | ||
601 | mw->menu.new_depth = 0; | |
602 | for (i = 0; i < level; i++) | |
603 | push_new_stack (mw, mw->menu.old_stack [i]); | |
604 | push_new_stack (mw, val); | |
605 | } | |
606 | ||
607 | static void | |
608 | make_windows_if_needed (XlwMenuWidget mw, int n) | |
609 | { | |
610 | int i; | |
611 | int start_at; | |
612 | XSetWindowAttributes xswa; | |
613 | int mask; | |
614 | Window root = RootWindowOfScreen (DefaultScreenOfDisplay (XtDisplay (mw))); | |
615 | window_state* windows; | |
616 | ||
617 | if (mw->menu.windows_length >= n) | |
618 | return; | |
619 | ||
620 | xswa.save_under = True; | |
621 | xswa.override_redirect = True; | |
622 | xswa.background_pixel = mw->core.background_pixel; | |
623 | xswa.border_pixel = mw->core.border_pixel; | |
624 | xswa.event_mask = | |
625 | ExposureMask | ButtonMotionMask | PointerMotionHintMask | |
626 | | ButtonReleaseMask | ButtonPressMask; | |
627 | xswa.cursor = mw->menu.cursor_shape; | |
628 | mask = CWSaveUnder | CWOverrideRedirect | CWBackPixel | CWBorderPixel | |
629 | | CWEventMask | CWCursor; | |
630 | ||
631 | if (!mw->menu.windows) | |
632 | { | |
633 | mw->menu.windows = | |
634 | (window_state*)XtMalloc (n * sizeof (window_state)); | |
635 | start_at = 0; | |
636 | } | |
637 | else | |
638 | { | |
639 | mw->menu.windows = | |
640 | (window_state*)XtRealloc ((char*)mw->menu.windows, | |
641 | n * sizeof (window_state)); | |
642 | start_at = mw->menu.windows_length; | |
643 | } | |
644 | mw->menu.windows_length = n; | |
645 | ||
646 | windows = mw->menu.windows; | |
647 | ||
648 | for (i = start_at; i < n; i++) | |
649 | { | |
650 | windows [i].x = 0; | |
651 | windows [i].y = 0; | |
652 | windows [i].width = 1; | |
653 | windows [i].height = 1; | |
654 | windows [i].window = | |
655 | XCreateWindow (XtDisplay (mw), root, 0, 0, 1, 1, | |
656 | 0, 0, CopyFromParent, CopyFromParent, mask, &xswa); | |
657 | } | |
658 | } | |
659 | ||
660 | /* Make the window fit in the screen */ | |
661 | static void | |
662 | fit_to_screen (XlwMenuWidget mw, window_state* ws, window_state* previous_ws, | |
663 | Boolean horizontal_p) | |
664 | { | |
665 | int screen_width = WidthOfScreen (XtScreen (mw)); | |
666 | int screen_height = HeightOfScreen (XtScreen (mw)); | |
667 | ||
668 | if (ws->x < 0) | |
669 | ws->x = 0; | |
670 | else if (ws->x + ws->width > screen_width) | |
671 | { | |
672 | if (!horizontal_p) | |
673 | ws->x = previous_ws->x - ws->width; | |
674 | else | |
675 | ws->x = screen_width - ws->width; | |
676 | } | |
677 | if (ws->y < 0) | |
678 | ws->y = 0; | |
679 | else if (ws->y + ws->height > screen_height) | |
680 | { | |
681 | if (horizontal_p) | |
682 | ws->y = previous_ws->y - ws->height; | |
683 | else | |
684 | ws->y = screen_height - ws->height; | |
685 | } | |
686 | } | |
687 | ||
688 | /* Updates old_stack from new_stack and redisplays. */ | |
689 | static void | |
690 | remap_menubar (XlwMenuWidget mw) | |
691 | { | |
692 | int i; | |
693 | int last_same; | |
694 | XPoint selection_position; | |
695 | int old_depth = mw->menu.old_depth; | |
696 | int new_depth = mw->menu.new_depth; | |
697 | widget_value** old_stack; | |
698 | widget_value** new_stack; | |
699 | window_state* windows; | |
700 | widget_value* old_selection; | |
701 | widget_value* new_selection; | |
702 | ||
703 | /* Check that enough windows and old_stack are ready. */ | |
704 | make_windows_if_needed (mw, new_depth); | |
705 | make_old_stack_space (mw, new_depth); | |
706 | windows = mw->menu.windows; | |
707 | old_stack = mw->menu.old_stack; | |
708 | new_stack = mw->menu.new_stack; | |
709 | ||
710 | /* compute the last identical different entry */ | |
711 | for (i = 1; i < old_depth && i < new_depth; i++) | |
712 | if (old_stack [i] != new_stack [i]) | |
713 | break; | |
714 | last_same = i - 1; | |
715 | ||
716 | /* Memorize the previously selected item to be able to refresh it */ | |
717 | old_selection = last_same + 1 < old_depth ? old_stack [last_same + 1] : NULL; | |
718 | if (old_selection && !old_selection->enabled) | |
719 | old_selection = NULL; | |
720 | new_selection = last_same + 1 < new_depth ? new_stack [last_same + 1] : NULL; | |
721 | if (new_selection && !new_selection->enabled) | |
722 | new_selection = NULL; | |
723 | ||
724 | /* updates old_state from new_state. It has to be done now because | |
725 | display_menu (called below) uses the old_stack to know what to display. */ | |
726 | for (i = last_same + 1; i < new_depth; i++) | |
727 | old_stack [i] = new_stack [i]; | |
728 | mw->menu.old_depth = new_depth; | |
729 | ||
730 | /* refresh the last seletion */ | |
731 | selection_position.x = 0; | |
732 | selection_position.y = 0; | |
733 | display_menu (mw, last_same, new_selection == old_selection, | |
734 | &selection_position, NULL, NULL, old_selection, new_selection); | |
735 | ||
736 | /* Now popup the new menus */ | |
737 | for (i = last_same + 1; i < new_depth && new_stack [i]->contents; i++) | |
738 | { | |
739 | window_state* previous_ws = &windows [i - 1]; | |
740 | window_state* ws = &windows [i]; | |
741 | ||
742 | ws->x = | |
743 | previous_ws->x + selection_position.x + mw->menu.shadow_thickness; | |
744 | if (!mw->menu.horizontal || i > 1) | |
745 | ws->x += mw->menu.shadow_thickness; | |
746 | ws->y = | |
747 | previous_ws->y + selection_position.y + mw->menu.shadow_thickness; | |
748 | ||
749 | size_menu (mw, i); | |
750 | ||
751 | fit_to_screen (mw, ws, previous_ws, mw->menu.horizontal && i == 1); | |
752 | ||
753 | XClearWindow (XtDisplay (mw), ws->window); | |
754 | XMoveResizeWindow (XtDisplay (mw), ws->window, ws->x, ws->y, | |
755 | ws->width, ws->height); | |
756 | XMapRaised (XtDisplay (mw), ws->window); | |
757 | display_menu (mw, i, False, &selection_position, NULL, NULL, NULL, NULL); | |
758 | } | |
759 | ||
760 | /* unmap the menus that popped down */ | |
761 | for (i = new_depth - 1; i < old_depth; i++) | |
762 | if (i >= new_depth || !new_stack [i]->contents) | |
763 | XUnmapWindow (XtDisplay (mw), windows [i].window); | |
764 | } | |
765 | ||
766 | static Boolean | |
767 | motion_event_is_in_menu (XlwMenuWidget mw, XMotionEvent* ev, int level, | |
768 | XPoint* relative_pos) | |
769 | { | |
770 | window_state* ws = &mw->menu.windows [level]; | |
771 | int x = level == 0 ? ws->x : ws->x + mw->menu.shadow_thickness; | |
772 | int y = level == 0 ? ws->y : ws->y + mw->menu.shadow_thickness; | |
773 | relative_pos->x = ev->x_root - x; | |
774 | relative_pos->y = ev->y_root - y; | |
775 | return (x < ev->x_root && ev->x_root < x + ws->width | |
776 | && y < ev->y_root && ev->y_root < y + ws->height); | |
777 | } | |
778 | ||
779 | static Boolean | |
780 | map_event_to_widget_value (XlwMenuWidget mw, XMotionEvent* ev, | |
781 | widget_value** val, int* level) | |
782 | { | |
783 | int i; | |
784 | XPoint relative_pos; | |
785 | window_state* ws; | |
786 | ||
787 | *val = NULL; | |
788 | ||
789 | /* Find the window */ | |
790 | for (i = mw->menu.old_depth - 1; i >= 0; i--) | |
791 | { | |
792 | ws = &mw->menu.windows [i]; | |
793 | if (ws && motion_event_is_in_menu (mw, ev, i, &relative_pos)) | |
794 | { | |
795 | display_menu (mw, i, True, NULL, &relative_pos, val, NULL, NULL); | |
796 | ||
797 | if (*val) | |
798 | { | |
799 | *level = i + 1; | |
800 | return True; | |
801 | } | |
802 | } | |
803 | } | |
804 | return False; | |
805 | } | |
806 | ||
807 | \f/* Procedures */ | |
808 | static void | |
809 | make_drawing_gcs (XlwMenuWidget mw) | |
810 | { | |
811 | XGCValues xgcv; | |
812 | ||
813 | xgcv.font = mw->menu.font->fid; | |
814 | xgcv.foreground = mw->menu.foreground; | |
815 | xgcv.background = mw->core.background_pixel; | |
816 | mw->menu.foreground_gc = XtGetGC ((Widget)mw, | |
817 | GCFont | GCForeground | GCBackground, | |
818 | &xgcv); | |
819 | ||
820 | xgcv.font = mw->menu.font->fid; | |
821 | xgcv.foreground = mw->menu.button_foreground; | |
822 | xgcv.background = mw->core.background_pixel; | |
823 | mw->menu.button_gc = XtGetGC ((Widget)mw, | |
824 | GCFont | GCForeground | GCBackground, | |
825 | &xgcv); | |
826 | ||
827 | xgcv.font = mw->menu.font->fid; | |
828 | xgcv.foreground = mw->menu.foreground; | |
829 | xgcv.background = mw->core.background_pixel; | |
830 | xgcv.fill_style = FillStippled; | |
831 | xgcv.stipple = mw->menu.gray_pixmap; | |
832 | mw->menu.inactive_gc = XtGetGC ((Widget)mw, | |
833 | (GCFont | GCForeground | GCBackground | |
834 | | GCFillStyle | GCStipple), &xgcv); | |
835 | ||
836 | xgcv.font = mw->menu.font->fid; | |
837 | xgcv.foreground = mw->menu.button_foreground; | |
838 | xgcv.background = mw->core.background_pixel; | |
839 | xgcv.fill_style = FillStippled; | |
840 | xgcv.stipple = mw->menu.gray_pixmap; | |
841 | mw->menu.inactive_button_gc = XtGetGC ((Widget)mw, | |
842 | (GCFont | GCForeground | GCBackground | |
843 | | GCFillStyle | GCStipple), &xgcv); | |
844 | ||
845 | xgcv.font = mw->menu.font->fid; | |
846 | xgcv.foreground = mw->core.background_pixel; | |
847 | xgcv.background = mw->menu.foreground; | |
848 | mw->menu.background_gc = XtGetGC ((Widget)mw, | |
849 | GCFont | GCForeground | GCBackground, | |
850 | &xgcv); | |
851 | } | |
852 | ||
853 | static void | |
854 | release_drawing_gcs (XlwMenuWidget mw) | |
855 | { | |
856 | XtReleaseGC ((Widget) mw, mw->menu.foreground_gc); | |
857 | XtReleaseGC ((Widget) mw, mw->menu.button_gc); | |
858 | XtReleaseGC ((Widget) mw, mw->menu.inactive_gc); | |
859 | XtReleaseGC ((Widget) mw, mw->menu.inactive_button_gc); | |
860 | XtReleaseGC ((Widget) mw, mw->menu.background_gc); | |
861 | /* let's get some segvs if we try to use these... */ | |
862 | mw->menu.foreground_gc = (GC) -1; | |
863 | mw->menu.button_gc = (GC) -1; | |
864 | mw->menu.inactive_gc = (GC) -1; | |
865 | mw->menu.inactive_button_gc = (GC) -1; | |
866 | mw->menu.background_gc = (GC) -1; | |
867 | } | |
868 | ||
869 | #define MINL(x,y) ((((unsigned long) (x)) < ((unsigned long) (y))) \ | |
870 | ? ((unsigned long) (x)) : ((unsigned long) (y))) | |
871 | ||
872 | static void | |
873 | make_shadow_gcs (XlwMenuWidget mw) | |
874 | { | |
875 | XGCValues xgcv; | |
876 | unsigned long pm = 0; | |
877 | Display *dpy = XtDisplay ((Widget) mw); | |
878 | Colormap cmap = DefaultColormapOfScreen (XtScreen ((Widget) mw)); | |
879 | XColor topc, botc; | |
880 | int top_frobbed = 0, bottom_frobbed = 0; | |
881 | ||
882 | if (mw->menu.top_shadow_color == -1) | |
883 | mw->menu.top_shadow_color = mw->core.background_pixel; | |
884 | if (mw->menu.bottom_shadow_color == -1) | |
885 | mw->menu.bottom_shadow_color = mw->menu.foreground; | |
886 | ||
887 | if (mw->menu.top_shadow_color == mw->core.background_pixel || | |
888 | mw->menu.top_shadow_color == mw->menu.foreground) | |
889 | { | |
890 | topc.pixel = mw->core.background_pixel; | |
891 | XQueryColor (dpy, cmap, &topc); | |
892 | /* don't overflow/wrap! */ | |
893 | topc.red = MINL (65535, topc.red * 1.2); | |
894 | topc.green = MINL (65535, topc.green * 1.2); | |
895 | topc.blue = MINL (65535, topc.blue * 1.2); | |
896 | if (XAllocColor (dpy, cmap, &topc)) | |
897 | { | |
898 | mw->menu.top_shadow_color = topc.pixel; | |
899 | top_frobbed = 1; | |
900 | } | |
901 | } | |
902 | if (mw->menu.bottom_shadow_color == mw->menu.foreground || | |
903 | mw->menu.bottom_shadow_color == mw->core.background_pixel) | |
904 | { | |
905 | botc.pixel = mw->core.background_pixel; | |
906 | XQueryColor (dpy, cmap, &botc); | |
907 | botc.red *= 0.6; | |
908 | botc.green *= 0.6; | |
909 | botc.blue *= 0.6; | |
910 | if (XAllocColor (dpy, cmap, &botc)) | |
911 | { | |
912 | mw->menu.bottom_shadow_color = botc.pixel; | |
913 | bottom_frobbed = 1; | |
914 | } | |
915 | } | |
916 | ||
917 | if (top_frobbed && bottom_frobbed) | |
918 | { | |
919 | int top_avg = ((topc.red / 3) + (topc.green / 3) + (topc.blue / 3)); | |
920 | int bot_avg = ((botc.red / 3) + (botc.green / 3) + (botc.blue / 3)); | |
921 | if (bot_avg > top_avg) | |
922 | { | |
923 | Pixel tmp = mw->menu.top_shadow_color; | |
924 | mw->menu.top_shadow_color = mw->menu.bottom_shadow_color; | |
925 | mw->menu.bottom_shadow_color = tmp; | |
926 | } | |
927 | else if (topc.pixel == botc.pixel) | |
928 | { | |
929 | if (botc.pixel == mw->menu.foreground) | |
930 | mw->menu.top_shadow_color = mw->core.background_pixel; | |
931 | else | |
932 | mw->menu.bottom_shadow_color = mw->menu.foreground; | |
933 | } | |
934 | } | |
935 | ||
936 | if (!mw->menu.top_shadow_pixmap && | |
937 | mw->menu.top_shadow_color == mw->core.background_pixel) | |
938 | { | |
939 | mw->menu.top_shadow_pixmap = mw->menu.gray_pixmap; | |
940 | mw->menu.top_shadow_color = mw->menu.foreground; | |
941 | } | |
942 | if (!mw->menu.bottom_shadow_pixmap && | |
943 | mw->menu.bottom_shadow_color == mw->core.background_pixel) | |
944 | { | |
945 | mw->menu.bottom_shadow_pixmap = mw->menu.gray_pixmap; | |
946 | mw->menu.bottom_shadow_color = mw->menu.foreground; | |
947 | } | |
948 | ||
949 | xgcv.fill_style = FillStippled; | |
950 | xgcv.foreground = mw->menu.top_shadow_color; | |
951 | xgcv.stipple = mw->menu.top_shadow_pixmap; | |
952 | pm = (xgcv.stipple ? GCStipple|GCFillStyle : 0); | |
953 | mw->menu.shadow_top_gc = XtGetGC ((Widget)mw, GCForeground | pm, &xgcv); | |
954 | ||
955 | xgcv.foreground = mw->menu.bottom_shadow_color; | |
956 | xgcv.stipple = mw->menu.bottom_shadow_pixmap; | |
957 | pm = (xgcv.stipple ? GCStipple|GCFillStyle : 0); | |
958 | mw->menu.shadow_bottom_gc = XtGetGC ((Widget)mw, GCForeground | pm, &xgcv); | |
959 | } | |
960 | ||
961 | ||
962 | static void | |
963 | release_shadow_gcs (XlwMenuWidget mw) | |
964 | { | |
965 | XtReleaseGC ((Widget) mw, mw->menu.shadow_top_gc); | |
966 | XtReleaseGC ((Widget) mw, mw->menu.shadow_bottom_gc); | |
967 | } | |
968 | ||
969 | static void | |
970 | XlwMenuInitialize (Widget request, Widget new, ArgList args, | |
971 | Cardinal *num_args) | |
972 | { | |
973 | /* Get the GCs and the widget size */ | |
974 | XlwMenuWidget mw = (XlwMenuWidget)new; | |
975 | ||
976 | XSetWindowAttributes xswa; | |
977 | int mask; | |
978 | ||
979 | Window window = RootWindowOfScreen (DefaultScreenOfDisplay (XtDisplay (mw))); | |
980 | Display* display = XtDisplay (mw); | |
981 | ||
982 | /* mw->menu.cursor = XCreateFontCursor (display, mw->menu.cursor_shape); */ | |
983 | mw->menu.cursor = mw->menu.cursor_shape; | |
984 | ||
985 | mw->menu.gray_pixmap = XCreatePixmapFromBitmapData (display, window, | |
986 | gray_bits, gray_width, | |
987 | gray_height, 1, 0, 1); | |
988 | ||
989 | make_drawing_gcs (mw); | |
990 | make_shadow_gcs (mw); | |
991 | ||
992 | xswa.background_pixel = mw->core.background_pixel; | |
993 | xswa.border_pixel = mw->core.border_pixel; | |
994 | mask = CWBackPixel | CWBorderPixel; | |
995 | ||
996 | mw->menu.popped_up = False; | |
997 | ||
998 | mw->menu.old_depth = 1; | |
999 | mw->menu.old_stack = (widget_value**)XtMalloc (sizeof (widget_value*)); | |
1000 | mw->menu.old_stack_length = 1; | |
1001 | mw->menu.old_stack [0] = mw->menu.contents; | |
1002 | ||
1003 | mw->menu.new_depth = 0; | |
1004 | mw->menu.new_stack = 0; | |
1005 | mw->menu.new_stack_length = 0; | |
1006 | push_new_stack (mw, mw->menu.contents); | |
1007 | ||
1008 | mw->menu.windows = (window_state*)XtMalloc (sizeof (window_state)); | |
1009 | mw->menu.windows_length = 1; | |
1010 | mw->menu.windows [0].x = 0; | |
1011 | mw->menu.windows [0].y = 0; | |
1012 | mw->menu.windows [0].width = 0; | |
1013 | mw->menu.windows [0].height = 0; | |
1014 | size_menu (mw, 0); | |
1015 | ||
1016 | mw->core.width = mw->menu.windows [0].width; | |
1017 | mw->core.height = mw->menu.windows [0].height; | |
1018 | } | |
1019 | ||
1020 | static void | |
1021 | XlwMenuClassInitialize () | |
1022 | { | |
1023 | } | |
1024 | ||
1025 | static void | |
1026 | XlwMenuRealize (Widget w, Mask *valueMask, XSetWindowAttributes *attributes) | |
1027 | { | |
1028 | XlwMenuWidget mw = (XlwMenuWidget)w; | |
1029 | XSetWindowAttributes xswa; | |
1030 | int mask; | |
1031 | ||
1032 | (*xlwMenuWidgetClass->core_class.superclass->core_class.realize) | |
1033 | (w, valueMask, attributes); | |
1034 | ||
1035 | xswa.save_under = True; | |
1036 | xswa.cursor = mw->menu.cursor_shape; | |
1037 | mask = CWSaveUnder | CWCursor; | |
1038 | XChangeWindowAttributes (XtDisplay (w), XtWindow (w), mask, &xswa); | |
1039 | ||
1040 | mw->menu.windows [0].window = XtWindow (w); | |
1041 | mw->menu.windows [0].x = w->core.x; | |
1042 | mw->menu.windows [0].y = w->core.y; | |
1043 | mw->menu.windows [0].width = w->core.width; | |
1044 | mw->menu.windows [0].height = w->core.height; | |
1045 | } | |
1046 | ||
1047 | /* Only the toplevel menubar/popup is a widget so it's the only one that | |
1048 | receives expose events through Xt. So we repaint all the other panes | |
1049 | when receiving an Expose event. */ | |
1050 | static void | |
1051 | XlwMenuRedisplay (Widget w, XEvent* ev, Region region) | |
1052 | { | |
1053 | XlwMenuWidget mw = (XlwMenuWidget)w; | |
1054 | int i; | |
1055 | ||
1056 | for (i = 0; i < mw->menu.old_depth; i++) | |
1057 | display_menu (mw, i, False, NULL, NULL, NULL, NULL, NULL); | |
1058 | } | |
1059 | ||
1060 | static void | |
1061 | XlwMenuDestroy (Widget w) | |
1062 | { | |
1063 | int i; | |
1064 | XlwMenuWidget mw = (XlwMenuWidget) w; | |
1065 | ||
1066 | release_drawing_gcs (mw); | |
1067 | release_shadow_gcs (mw); | |
1068 | ||
1069 | /* this doesn't come from the resource db but is created explicitly | |
1070 | so we must free it ourselves. */ | |
1071 | XFreePixmap (XtDisplay (mw), mw->menu.gray_pixmap); | |
1072 | mw->menu.gray_pixmap = (Pixmap) -1; | |
1073 | ||
1074 | /* Don't free mw->menu.contents because that comes from our creator. | |
1075 | The `*_stack' elements are just pointers into `contents' so leave | |
1076 | that alone too. But free the stacks themselves. */ | |
1077 | if (mw->menu.old_stack) XtFree ((char *) mw->menu.old_stack); | |
1078 | if (mw->menu.new_stack) XtFree ((char *) mw->menu.new_stack); | |
1079 | ||
1080 | /* Remember, you can't free anything that came from the resource | |
1081 | database. This includes: | |
1082 | mw->menu.cursor | |
1083 | mw->menu.top_shadow_pixmap | |
1084 | mw->menu.bottom_shadow_pixmap | |
1085 | mw->menu.font | |
1086 | Also the color cells of top_shadow_color, bottom_shadow_color, | |
1087 | foreground, and button_foreground will never be freed until this | |
1088 | client exits. Nice, eh? | |
1089 | */ | |
1090 | ||
1091 | /* start from 1 because the one in slot 0 is w->core.window */ | |
1092 | for (i = 1; i < mw->menu.windows_length; i++) | |
1093 | XDestroyWindow (XtDisplay (mw), mw->menu.windows [i].window); | |
1094 | if (mw->menu.windows) | |
1095 | XtFree ((char *) mw->menu.windows); | |
1096 | } | |
1097 | ||
1098 | static Boolean | |
1099 | XlwMenuSetValues (Widget current, Widget request, Widget new) | |
1100 | { | |
1101 | XlwMenuWidget oldmw = (XlwMenuWidget)current; | |
1102 | XlwMenuWidget newmw = (XlwMenuWidget)new; | |
1103 | Boolean redisplay = False; | |
1104 | int i; | |
1105 | ||
1106 | if (newmw->menu.contents | |
1107 | && newmw->menu.contents->contents | |
1108 | && newmw->menu.contents->contents->change >= VISIBLE_CHANGE) | |
1109 | redisplay = True; | |
1110 | ||
1111 | if (newmw->core.background_pixel != oldmw->core.background_pixel | |
1112 | || newmw->menu.foreground != oldmw->menu.foreground) | |
1113 | { | |
1114 | release_drawing_gcs (newmw); | |
1115 | make_drawing_gcs (newmw); | |
1116 | redisplay = True; | |
1117 | ||
1118 | for (i = 0; i < oldmw->menu.windows_length; i++) | |
1119 | { | |
1120 | XSetWindowBackground (XtDisplay (oldmw), | |
1121 | oldmw->menu.windows [i].window, | |
1122 | newmw->core.background_pixel); | |
1123 | /* clear windows and generate expose events */ | |
1124 | XClearArea (XtDisplay (oldmw), oldmw->menu.windows[i].window, | |
1125 | 0, 0, 0, 0, True); | |
1126 | } | |
1127 | } | |
1128 | ||
1129 | return redisplay; | |
1130 | } | |
1131 | ||
1132 | static void | |
1133 | XlwMenuResize (Widget w) | |
1134 | { | |
1135 | XlwMenuWidget mw = (XlwMenuWidget)w; | |
1136 | ||
1137 | mw->menu.windows [0].width = mw->core.width; | |
1138 | mw->menu.windows [0].height = mw->core.height; | |
1139 | } | |
1140 | ||
1141 | \f/* Action procedures */ | |
1142 | static void | |
1143 | handle_single_motion_event (XlwMenuWidget mw, XMotionEvent* ev) | |
1144 | { | |
1145 | widget_value* val; | |
1146 | int level; | |
1147 | ||
1148 | if (!map_event_to_widget_value (mw, ev, &val, &level)) | |
1149 | pop_new_stack_if_no_contents (mw); | |
1150 | else | |
1151 | set_new_state (mw, val, level); | |
1152 | remap_menubar (mw); | |
1153 | ||
1154 | /* Sync with the display. Makes it feel better on X terms. */ | |
1155 | XSync (XtDisplay (mw), False); | |
1156 | } | |
1157 | ||
1158 | static void | |
1159 | handle_motion_event (XlwMenuWidget mw, XMotionEvent* ev) | |
1160 | { | |
1161 | int x = ev->x_root; | |
1162 | int y = ev->y_root; | |
1163 | int state = ev->state; | |
1164 | ||
1165 | handle_single_motion_event (mw, ev); | |
1166 | ||
1167 | /* allow motion events to be generated again */ | |
1168 | if (ev->is_hint | |
1169 | && XQueryPointer (XtDisplay (mw), ev->window, | |
1170 | &ev->root, &ev->subwindow, | |
1171 | &ev->x_root, &ev->y_root, | |
1172 | &ev->x, &ev->y, | |
1173 | &ev->state) | |
1174 | && ev->state == state | |
1175 | && (ev->x_root != x || ev->y_root != y)) | |
1176 | handle_single_motion_event (mw, ev); | |
1177 | } | |
1178 | ||
1179 | static void | |
1180 | Start (Widget w, XEvent *ev, String *params, Cardinal *num_params) | |
1181 | { | |
1182 | XlwMenuWidget mw = (XlwMenuWidget)w; | |
1183 | ||
1184 | XtCallCallbackList ((Widget)mw, mw->menu.open, NULL); | |
1185 | ||
1186 | /* notes the absolute position of the menubar window */ | |
1187 | mw->menu.windows [0].x = ev->xmotion.x_root - ev->xmotion.x; | |
1188 | mw->menu.windows [0].y = ev->xmotion.y_root - ev->xmotion.y; | |
1189 | ||
1190 | /* handles the down like a move, slots are compatible */ | |
1191 | handle_motion_event (mw, &ev->xmotion); | |
1192 | } | |
1193 | ||
1194 | static void | |
1195 | Drag (Widget w, XEvent *ev, String *params, Cardinal *num_params) | |
1196 | { | |
1197 | XlwMenuWidget mw = (XlwMenuWidget)w; | |
1198 | handle_motion_event (mw, &ev->xmotion); | |
1199 | } | |
1200 | ||
1201 | static void | |
1202 | Select (Widget w, XEvent *ev, String *params, Cardinal *num_params) | |
1203 | { | |
1204 | XlwMenuWidget mw = (XlwMenuWidget)w; | |
1205 | widget_value* selected_item = mw->menu.old_stack [mw->menu.old_depth - 1]; | |
1206 | ||
1207 | /* pop down everything */ | |
1208 | mw->menu.new_depth = 1; | |
1209 | remap_menubar (mw); | |
1210 | ||
1211 | if (mw->menu.popped_up) | |
1212 | { | |
1213 | mw->menu.popped_up = False; | |
1214 | XtUngrabPointer ((Widget)mw, ev->xmotion.time); | |
1215 | XtPopdown (XtParent (mw)); | |
1216 | } | |
1217 | ||
1218 | /* callback */ | |
1219 | XtCallCallbackList ((Widget)mw, mw->menu.select, (XtPointer)selected_item); | |
1220 | ||
1221 | } | |
1222 | ||
1223 | ||
1224 | \f/* Special code to pop-up a menu */ | |
1225 | void | |
1226 | pop_up_menu (XlwMenuWidget mw, XButtonPressedEvent* event) | |
1227 | { | |
1228 | int x = event->x_root; | |
1229 | int y = event->y_root; | |
1230 | int w; | |
1231 | int h; | |
1232 | int borderwidth = mw->menu.shadow_thickness; | |
1233 | Screen* screen = XtScreen (mw); | |
1234 | ||
1235 | XtCallCallbackList ((Widget)mw, mw->menu.open, NULL); | |
1236 | ||
1237 | size_menu (mw, 0); | |
1238 | ||
1239 | w = mw->menu.windows [0].width; | |
1240 | h = mw->menu.windows [0].height; | |
1241 | ||
1242 | x -= borderwidth; | |
1243 | y -= borderwidth; | |
1244 | if (x < borderwidth) | |
1245 | x = borderwidth; | |
1246 | if (x + w + 2 * borderwidth > WidthOfScreen (screen)) | |
1247 | x = WidthOfScreen (screen) - w - 2 * borderwidth; | |
1248 | if (y < borderwidth) | |
1249 | y = borderwidth; | |
1250 | if (y + h + 2 * borderwidth> HeightOfScreen (screen)) | |
1251 | y = HeightOfScreen (screen) - h - 2 * borderwidth; | |
1252 | ||
1253 | mw->menu.popped_up = True; | |
1254 | XtConfigureWidget (XtParent (mw), x, y, w, h, | |
1255 | XtParent (mw)->core.border_width); | |
1256 | XtPopup (XtParent (mw), XtGrabExclusive); | |
1257 | display_menu (mw, 0, False, NULL, NULL, NULL, NULL, NULL); | |
1258 | XtGrabPointer ((Widget)mw, False, | |
1259 | (ButtonMotionMask | PointerMotionHintMask | ButtonReleaseMask | |
1260 | | ButtonPressMask), | |
1261 | GrabModeAsync, GrabModeAsync, None, mw->menu.cursor_shape, | |
1262 | event->time); | |
1263 | ||
1264 | mw->menu.windows [0].x = x + borderwidth; | |
1265 | mw->menu.windows [0].y = y + borderwidth; | |
1266 | ||
1267 | handle_motion_event (mw, (XMotionEvent*)event); | |
1268 | } |