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