Allow windows to specify their own depth.
[wmaker-crm.git] / src / menu.c
blob7bb0639d9057dfc584e103dc4d8b8a714aa0fa57
1 /* menu.c- generic menu, used for root menu, application menus etc.
3 * Window Maker window manager
5 * Copyright (c) 1997-2003 Alfredo K. Kojima
6 * Copyright (c) 1998-2003 Dan Pascu
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
23 #include "wconfig.h"
25 #include <X11/Xlib.h>
26 #include <X11/Xutil.h>
27 #include <X11/keysym.h>
28 #include <stdlib.h>
29 #include <string.h>
30 #include <stdio.h>
31 #include <stdint.h>
32 #include <unistd.h>
33 #include <ctype.h>
35 #include "WindowMaker.h"
36 #include "wcore.h"
37 #include "framewin.h"
38 #include "menu.h"
39 #include "actions.h"
40 #include "funcs.h"
41 #include "stacking.h"
42 #include "xinerama.h"
43 #include "workspace.h"
44 #include "dialog.h"
45 #include "rootmenu.h"
47 /****** Global Variables ******/
49 extern Cursor wCursor[WCUR_LAST];
51 extern XContext wWinContext;
53 extern WPreferences wPreferences;
55 #define MOD_MASK wPreferences.modifier_mask
57 #define MENU_SCROLL_STEP menuScrollParameters[(int)wPreferences.menu_scroll_speed].steps
58 #define MENU_SCROLL_DELAY menuScrollParameters[(int)wPreferences.menu_scroll_speed].delay
60 #define MENUW(m) ((m)->frame->core->width+2*FRAME_BORDER_WIDTH)
61 #define MENUH(m) ((m)->frame->core->height+2*FRAME_BORDER_WIDTH)
63 /***** Local Stuff ******/
65 #define WSS_ROOTMENU (1<<0)
66 #define WSS_SWITCHMENU (1<<1)
67 #define WSS_WSMENU (1<<2)
69 static struct {
70 int steps;
71 int delay;
72 } menuScrollParameters[5] = {
74 MENU_SCROLL_STEPS_UF, MENU_SCROLL_DELAY_UF}, {
75 MENU_SCROLL_STEPS_F, MENU_SCROLL_DELAY_F}, {
76 MENU_SCROLL_STEPS_M, MENU_SCROLL_DELAY_M}, {
77 MENU_SCROLL_STEPS_S, MENU_SCROLL_DELAY_S}, {
78 MENU_SCROLL_STEPS_US, MENU_SCROLL_DELAY_US}};
80 static void menuMouseDown(WObjDescriptor * desc, XEvent * event);
81 static void menuExpose(WObjDescriptor * desc, XEvent * event);
82 static void menuTitleDoubleClick(WCoreWindow * sender, void *data, XEvent * event);
83 static void menuTitleMouseDown(WCoreWindow * sender, void *data, XEvent * event);
84 static void menuCloseClick(WCoreWindow * sender, void *data, XEvent * event);
85 static void updateTexture(WMenu * menu);
86 static int saveMenuRecurs(WMPropList * menus, WScreen * scr, WMenu * menu);
87 static int restoreMenuRecurs(WScreen * scr, WMPropList * menus, WMenu * menu, char *path);
88 static void selectEntry(WMenu * menu, int entry_no);
89 static void closeCascade(WMenu * menu);
91 /****** Notification Observers ******/
93 static void appearanceObserver(void *self, WMNotification * notif)
95 WMenu *menu = (WMenu *) self;
96 uintptr_t flags = (uintptr_t)WMGetNotificationClientData(notif);
98 if (!menu->flags.realized)
99 return;
101 if (WMGetNotificationName(notif) == WNMenuAppearanceSettingsChanged) {
102 if (flags & WFontSettings) {
103 menu->flags.realized = 0;
104 wMenuRealize(menu);
106 if (flags & WTextureSettings) {
107 if (!menu->flags.brother)
108 updateTexture(menu);
110 if (flags & (WTextureSettings | WColorSettings)) {
111 wMenuPaint(menu);
113 } else if (menu->flags.titled) {
115 if (flags & WFontSettings) {
116 menu->flags.realized = 0;
117 wMenuRealize(menu);
119 if (flags & WTextureSettings) {
120 menu->frame->flags.need_texture_remake = 1;
122 if (flags & (WColorSettings | WTextureSettings)) {
123 wFrameWindowPaint(menu->frame);
128 /************************************/
131 *----------------------------------------------------------------------
132 * wMenuCreate--
133 * Creates a new empty menu with the specified title. If main_menu
134 * is True, the created menu will be a main menu, which has some special
135 * properties such as being placed over other normal menus.
136 * If title is NULL, the menu will have no titlebar.
138 * Returns:
139 * The created menu.
140 *----------------------------------------------------------------------
142 WMenu *wMenuCreate(WScreen * screen, char *title, int main_menu)
144 WMenu *menu;
145 static int brother = 0;
146 int tmp, flags;
148 menu = wmalloc(sizeof(WMenu));
150 #ifdef SINGLE_MENULEVEL
151 tmp = WMSubmenuLevel;
152 #else
153 tmp = (main_menu ? WMMainMenuLevel : WMSubmenuLevel);
154 #endif
156 flags = WFF_SINGLE_STATE | WFF_BORDER;
157 if (title) {
158 flags |= WFF_TITLEBAR | WFF_RIGHT_BUTTON;
159 menu->flags.titled = 1;
161 menu->frame =
162 wFrameWindowCreate(screen, tmp, 8, 2, 1, 1, &wPreferences.menu_title_clearance,
163 &wPreferences.menu_title_min_height,
164 &wPreferences.menu_title_max_height,
165 flags,
166 screen->menu_title_texture, NULL,
167 screen->menu_title_color, &screen->menu_title_font,
168 screen->w_depth, screen->w_visual, screen->w_colormap);
170 menu->frame->core->descriptor.parent = menu;
171 menu->frame->core->descriptor.parent_type = WCLASS_MENU;
172 menu->frame->core->descriptor.handle_mousedown = menuMouseDown;
174 wFrameWindowHideButton(menu->frame, WFF_RIGHT_BUTTON);
176 if (title) {
177 menu->frame->title = wstrdup(title);
180 menu->frame->flags.justification = WTJ_LEFT;
182 menu->frame->rbutton_image = screen->b_pixmaps[WBUT_CLOSE];
184 menu->entry_no = 0;
185 menu->alloced_entries = 0;
186 menu->selected_entry = -1;
187 menu->entries = NULL;
189 menu->frame_x = screen->app_menu_x;
190 menu->frame_y = screen->app_menu_y;
192 menu->frame->child = menu;
194 menu->flags.lowered = 0;
196 /* create borders */
197 if (title) {
198 /* setup object descriptors */
199 menu->frame->on_mousedown_titlebar = menuTitleMouseDown;
200 menu->frame->on_dblclick_titlebar = menuTitleDoubleClick;
203 menu->frame->on_click_right = menuCloseClick;
205 menu->menu = wCoreCreate(menu->frame->core, 0, menu->frame->top_width, menu->frame->core->width, 10);
207 menu->menu->descriptor.parent = menu;
208 menu->menu->descriptor.parent_type = WCLASS_MENU;
209 menu->menu->descriptor.handle_expose = menuExpose;
210 menu->menu->descriptor.handle_mousedown = menuMouseDown;
212 menu->menu_texture_data = None;
214 XMapWindow(dpy, menu->menu->window);
216 XFlush(dpy);
218 if (!brother) {
219 brother = 1;
220 menu->brother = wMenuCreate(screen, title, main_menu);
221 brother = 0;
222 menu->brother->flags.brother = 1;
223 menu->brother->brother = menu;
225 WMAddNotificationObserver(appearanceObserver, menu, WNMenuAppearanceSettingsChanged, menu);
227 WMAddNotificationObserver(appearanceObserver, menu, WNMenuTitleAppearanceSettingsChanged, menu);
229 return menu;
232 WMenu *wMenuCreateForApp(WScreen * screen, char *title, int main_menu)
234 WMenu *menu;
236 menu = wMenuCreate(screen, title, main_menu);
237 if (!menu)
238 return NULL;
239 menu->flags.app_menu = 1;
240 menu->brother->flags.app_menu = 1;
242 return menu;
245 static void insertEntry(WMenu * menu, WMenuEntry * entry, int index)
247 int i;
249 for (i = menu->entry_no - 1; i >= index; i--) {
250 menu->entries[i]->order++;
251 menu->entries[i + 1] = menu->entries[i];
253 menu->entries[index] = entry;
256 WMenuEntry *wMenuInsertCallback(WMenu * menu, int index, char *text,
257 void (*callback) (WMenu * menu, WMenuEntry * entry), void *clientdata)
259 WMenuEntry *entry;
261 menu->flags.realized = 0;
262 menu->brother->flags.realized = 0;
264 /* reallocate array if it's too small */
265 if (menu->entry_no >= menu->alloced_entries) {
266 void *tmp;
268 tmp = wrealloc(menu->entries, sizeof(WMenuEntry) * (menu->alloced_entries + 5));
270 menu->entries = tmp;
271 menu->alloced_entries += 5;
273 menu->brother->entries = tmp;
274 menu->brother->alloced_entries = menu->alloced_entries;
276 entry = wmalloc(sizeof(WMenuEntry));
277 entry->flags.enabled = 1;
278 entry->text = wstrdup(text);
279 entry->cascade = -1;
280 entry->clientdata = clientdata;
281 entry->callback = callback;
282 if (index < 0 || index >= menu->entry_no) {
283 entry->order = menu->entry_no;
284 menu->entries[menu->entry_no] = entry;
285 } else {
286 entry->order = index;
287 insertEntry(menu, entry, index);
290 menu->entry_no++;
291 menu->brother->entry_no = menu->entry_no;
293 return entry;
296 void wMenuEntrySetCascade(WMenu * menu, WMenuEntry * entry, WMenu * cascade)
298 WMenu *brother = menu->brother;
299 int i, done;
301 assert(menu->flags.brother == 0);
303 if (entry->cascade >= 0) {
304 menu->flags.realized = 0;
305 brother->flags.realized = 0;
308 cascade->parent = menu;
310 cascade->brother->parent = brother;
312 done = 0;
313 for (i = 0; i < menu->cascade_no; i++) {
314 if (menu->cascades[i] == NULL) {
315 menu->cascades[i] = cascade;
316 brother->cascades[i] = cascade->brother;
317 done = 1;
318 entry->cascade = i;
319 break;
322 if (!done) {
323 entry->cascade = menu->cascade_no;
325 menu->cascades = wrealloc(menu->cascades, sizeof(WMenu) * (menu->cascade_no + 1));
326 menu->cascades[menu->cascade_no++] = cascade;
328 brother->cascades = wrealloc(brother->cascades, sizeof(WMenu) * (brother->cascade_no + 1));
329 brother->cascades[brother->cascade_no++] = cascade->brother;
332 if (menu->flags.lowered) {
334 cascade->flags.lowered = 1;
335 ChangeStackingLevel(cascade->frame->core, WMNormalLevel);
337 cascade->brother->flags.lowered = 1;
338 ChangeStackingLevel(cascade->brother->frame->core, WMNormalLevel);
341 if (!menu->flags.realized)
342 wMenuRealize(menu);
345 void wMenuEntryRemoveCascade(WMenu * menu, WMenuEntry * entry)
347 assert(menu->flags.brother == 0);
349 /* destroy cascade menu */
350 if (entry->cascade >= 0 && menu->cascades && menu->cascades[entry->cascade] != NULL) {
352 wMenuDestroy(menu->cascades[entry->cascade], True);
354 menu->cascades[entry->cascade] = NULL;
355 menu->brother->cascades[entry->cascade] = NULL;
357 entry->cascade = -1;
361 void wMenuRemoveItem(WMenu * menu, int index)
363 int i;
365 if (menu->flags.brother) {
366 wMenuRemoveItem(menu->brother, index);
367 return;
370 if (index >= menu->entry_no)
371 return;
373 /* destroy cascade menu */
374 wMenuEntryRemoveCascade(menu, menu->entries[index]);
376 /* destroy unshared data */
378 if (menu->entries[index]->text)
379 wfree(menu->entries[index]->text);
381 if (menu->entries[index]->rtext)
382 wfree(menu->entries[index]->rtext);
384 if (menu->entries[index]->free_cdata && menu->entries[index]->clientdata)
385 (*menu->entries[index]->free_cdata) (menu->entries[index]->clientdata);
387 wfree(menu->entries[index]);
389 for (i = index; i < menu->entry_no - 1; i++) {
390 menu->entries[i + 1]->order--;
391 menu->entries[i] = menu->entries[i + 1];
393 menu->entry_no--;
394 menu->brother->entry_no--;
397 static Pixmap renderTexture(WMenu * menu)
399 RImage *img;
400 Pixmap pix;
401 int i;
402 RColor light;
403 RColor dark;
404 RColor mid;
405 WScreen *scr = menu->menu->screen_ptr;
406 WTexture *texture = scr->menu_item_texture;
408 if (wPreferences.menu_style == MS_NORMAL) {
409 img = wTextureRenderImage(texture, menu->menu->width, menu->entry_height, WREL_MENUENTRY);
410 } else {
411 img = wTextureRenderImage(texture, menu->menu->width, menu->menu->height + 1, WREL_MENUENTRY);
413 if (!img) {
414 wwarning(_("could not render texture: %s"), RMessageForError(RErrorCode));
416 return None;
419 if (wPreferences.menu_style == MS_SINGLE_TEXTURE) {
420 light.alpha = 0;
421 light.red = light.green = light.blue = 80;
423 dark.alpha = 255;
424 dark.red = dark.green = dark.blue = 0;
426 mid.alpha = 0;
427 mid.red = mid.green = mid.blue = 40;
429 for (i = 1; i < menu->entry_no; i++) {
430 ROperateLine(img, RSubtractOperation, 0, i * menu->entry_height - 2,
431 menu->menu->width - 1, i * menu->entry_height - 2, &mid);
433 RDrawLine(img, 0, i * menu->entry_height - 1,
434 menu->menu->width - 1, i * menu->entry_height - 1, &dark);
436 ROperateLine(img, RAddOperation, 0, i * menu->entry_height,
437 menu->menu->width - 1, i * menu->entry_height, &light);
440 if (!RConvertImage(scr->rcontext, img, &pix)) {
441 wwarning(_("error rendering image:%s"), RMessageForError(RErrorCode));
443 RReleaseImage(img);
445 return pix;
448 static void updateTexture(WMenu * menu)
450 WScreen *scr = menu->menu->screen_ptr;
452 /* setup background texture */
453 if (scr->menu_item_texture->any.type != WTEX_SOLID) {
454 if (!menu->flags.brother) {
455 FREE_PIXMAP(menu->menu_texture_data);
457 menu->menu_texture_data = renderTexture(menu);
459 XSetWindowBackgroundPixmap(dpy, menu->menu->window, menu->menu_texture_data);
460 XClearWindow(dpy, menu->menu->window);
462 XSetWindowBackgroundPixmap(dpy, menu->brother->menu->window, menu->menu_texture_data);
463 XClearWindow(dpy, menu->brother->menu->window);
465 } else {
466 XSetWindowBackground(dpy, menu->menu->window, scr->menu_item_texture->any.color.pixel);
467 XClearWindow(dpy, menu->menu->window);
471 void wMenuRealize(WMenu * menu)
473 int i;
474 int width, rwidth, mrwidth, mwidth;
475 int theight, twidth, eheight;
476 WScreen *scr = menu->frame->screen_ptr;
477 static int brother_done = 0;
478 int flags;
480 if (!brother_done) {
481 brother_done = 1;
482 wMenuRealize(menu->brother);
483 brother_done = 0;
486 flags = WFF_SINGLE_STATE | WFF_BORDER;
487 if (menu->flags.titled)
488 flags |= WFF_TITLEBAR | WFF_RIGHT_BUTTON;
490 wFrameWindowUpdateBorders(menu->frame, flags);
492 if (menu->flags.titled) {
493 twidth = WMWidthOfString(scr->menu_title_font, menu->frame->title, strlen(menu->frame->title));
494 theight = menu->frame->top_width;
495 twidth += theight + (wPreferences.new_style == TS_NEW ? 16 : 8);
496 } else {
497 twidth = 0;
498 theight = 0;
500 eheight = WMFontHeight(scr->menu_entry_font) + 6 + wPreferences.menu_text_clearance * 2;
501 menu->entry_height = eheight;
502 mrwidth = 0;
503 mwidth = 0;
504 for (i = 0; i < menu->entry_no; i++) {
505 char *text;
507 /* search widest text */
508 text = menu->entries[i]->text;
509 width = WMWidthOfString(scr->menu_entry_font, text, strlen(text)) + 10;
511 if (menu->entries[i]->flags.indicator) {
512 width += MENU_INDICATOR_SPACE;
515 if (width > mwidth)
516 mwidth = width;
518 /* search widest text on right */
519 text = menu->entries[i]->rtext;
520 if (text)
521 rwidth = WMWidthOfString(scr->menu_entry_font, text, strlen(text))
522 + 10;
523 else if (menu->entries[i]->cascade >= 0)
524 rwidth = 16;
525 else
526 rwidth = 4;
528 if (rwidth > mrwidth)
529 mrwidth = rwidth;
531 mwidth += mrwidth;
533 if (mwidth < twidth)
534 mwidth = twidth;
536 wCoreConfigure(menu->menu, 0, theight, mwidth, menu->entry_no * eheight - 1);
538 wFrameWindowResize(menu->frame, mwidth, menu->entry_no * eheight - 1
539 + menu->frame->top_width + menu->frame->bottom_width);
541 updateTexture(menu);
543 menu->flags.realized = 1;
545 if (menu->flags.mapped)
546 wMenuPaint(menu);
547 if (menu->brother->flags.mapped)
548 wMenuPaint(menu->brother);
551 void wMenuDestroy(WMenu * menu, int recurse)
553 int i;
555 WMRemoveNotificationObserver(menu);
557 /* remove any pending timers */
558 if (menu->timer)
559 WMDeleteTimerHandler(menu->timer);
560 menu->timer = NULL;
562 /* call destroy handler */
563 if (menu->on_destroy)
564 (*menu->on_destroy) (menu);
566 /* Destroy items if this menu own them. If this is the "brother" menu,
567 * leave them alone as it is shared by them.
569 if (!menu->flags.brother) {
570 for (i = 0; i < menu->entry_no; i++) {
572 wfree(menu->entries[i]->text);
574 if (menu->entries[i]->rtext)
575 wfree(menu->entries[i]->rtext);
576 #ifdef USER_MENU
578 if (menu->entries[i]->instances) {
579 WMReleasePropList(menu->entries[i]->instances);
581 #endif /* USER_MENU */
583 if (menu->entries[i]->free_cdata && menu->entries[i]->clientdata) {
584 (*menu->entries[i]->free_cdata) (menu->entries[i]->clientdata);
586 wfree(menu->entries[i]);
589 if (recurse) {
590 for (i = 0; i < menu->cascade_no; i++) {
591 if (menu->cascades[i]) {
592 if (menu->cascades[i]->flags.brother)
593 wMenuDestroy(menu->cascades[i]->brother, recurse);
594 else
595 wMenuDestroy(menu->cascades[i], recurse);
600 if (menu->entries)
601 wfree(menu->entries);
605 FREE_PIXMAP(menu->menu_texture_data);
607 if (menu->cascades)
608 wfree(menu->cascades);
610 wCoreDestroy(menu->menu);
611 wFrameWindowDestroy(menu->frame);
613 /* destroy copy of this menu */
614 if (!menu->flags.brother && menu->brother)
615 wMenuDestroy(menu->brother, False);
617 wfree(menu);
620 #define F_NORMAL 0
621 #define F_TOP 1
622 #define F_BOTTOM 2
623 #define F_NONE 3
625 static void drawFrame(WScreen * scr, Drawable win, int y, int w, int h, int type)
627 XSegment segs[2];
628 int i;
630 i = 0;
631 segs[i].x1 = segs[i].x2 = w - 1;
632 segs[i].y1 = y;
633 segs[i].y2 = y + h - 1;
634 i++;
635 if (type != F_TOP && type != F_NONE) {
636 segs[i].x1 = 1;
637 segs[i].y1 = segs[i].y2 = y + h - 2;
638 segs[i].x2 = w - 1;
639 i++;
641 XDrawSegments(dpy, win, scr->menu_item_auxtexture->dim_gc, segs, i);
643 i = 0;
644 segs[i].x1 = 0;
645 segs[i].y1 = y;
646 segs[i].x2 = 0;
647 segs[i].y2 = y + h - 1;
648 i++;
649 if (type != F_BOTTOM && type != F_NONE) {
650 segs[i].x1 = 0;
651 segs[i].y1 = y;
652 segs[i].x2 = w - 1;
653 segs[i].y2 = y;
654 i++;
656 XDrawSegments(dpy, win, scr->menu_item_auxtexture->light_gc, segs, i);
658 if (type != F_TOP && type != F_NONE)
659 XDrawLine(dpy, win, scr->menu_item_auxtexture->dark_gc, 0, y + h - 1, w - 1, y + h - 1);
662 static void paintEntry(WMenu * menu, int index, int selected)
664 WScreen *scr = menu->frame->screen_ptr;
665 Window win = menu->menu->window;
666 WMenuEntry *entry = menu->entries[index];
667 GC light, dim, dark;
668 WMColor *color;
669 int x, y, w, h, tw;
670 int type;
672 if (!menu->flags.realized)
673 return;
674 h = menu->entry_height;
675 w = menu->menu->width;
676 y = index * h;
678 light = scr->menu_item_auxtexture->light_gc;
679 dim = scr->menu_item_auxtexture->dim_gc;
680 dark = scr->menu_item_auxtexture->dark_gc;
682 if (wPreferences.menu_style == MS_FLAT && menu->entry_no > 1) {
683 if (index == 0)
684 type = F_TOP;
685 else if (index == menu->entry_no - 1)
686 type = F_BOTTOM;
687 else
688 type = F_NONE;
689 } else {
690 type = F_NORMAL;
693 /* paint background */
694 if (selected) {
695 XFillRectangle(dpy, win, WMColorGC(scr->select_color), 1, y + 1, w - 2, h - 3);
696 if (scr->menu_item_texture->any.type == WTEX_SOLID)
697 drawFrame(scr, win, y, w, h, type);
698 } else {
699 if (scr->menu_item_texture->any.type == WTEX_SOLID) {
700 XClearArea(dpy, win, 0, y + 1, w - 1, h - 3, False);
701 /* draw the frame */
702 drawFrame(scr, win, y, w, h, type);
703 } else {
704 XClearArea(dpy, win, 0, y, w, h, False);
708 if (selected) {
709 if (entry->flags.enabled)
710 color = scr->select_text_color;
711 else
712 color = scr->dtext_color;
713 } else if (!entry->flags.enabled) {
714 color = scr->dtext_color;
715 } else {
716 color = scr->mtext_color;
718 /* draw text */
719 x = 5;
720 if (entry->flags.indicator)
721 x += MENU_INDICATOR_SPACE + 2;
723 WMDrawString(scr->wmscreen, win, color, scr->menu_entry_font,
724 x, 3 + y + wPreferences.menu_text_clearance, entry->text, strlen(entry->text));
726 if (entry->cascade >= 0) {
727 /* draw the cascade indicator */
728 XDrawLine(dpy, win, dim, w - 11, y + 6, w - 6, y + h / 2 - 1);
729 XDrawLine(dpy, win, light, w - 11, y + h - 8, w - 6, y + h / 2 - 1);
730 XDrawLine(dpy, win, dark, w - 12, y + 6, w - 12, y + h - 8);
733 /* draw indicator */
734 if (entry->flags.indicator && entry->flags.indicator_on) {
735 int iw, ih;
736 WPixmap *indicator;
738 switch (entry->flags.indicator_type) {
739 case MI_CHECK:
740 indicator = scr->menu_check_indicator;
741 break;
742 case MI_MINIWINDOW:
743 indicator = scr->menu_mini_indicator;
744 break;
745 case MI_HIDDEN:
746 indicator = scr->menu_hide_indicator;
747 break;
748 case MI_SHADED:
749 indicator = scr->menu_shade_indicator;
750 break;
751 case MI_DIAMOND:
752 default:
753 indicator = scr->menu_radio_indicator;
754 break;
757 iw = indicator->width;
758 ih = indicator->height;
759 XSetClipMask(dpy, scr->copy_gc, indicator->mask);
760 XSetClipOrigin(dpy, scr->copy_gc, 5, y + (h - ih) / 2);
761 if (selected) {
762 if (entry->flags.enabled) {
763 XSetForeground(dpy, scr->copy_gc, WMColorPixel(scr->select_text_color));
764 } else {
765 XSetForeground(dpy, scr->copy_gc, WMColorPixel(scr->dtext_color));
767 } else {
768 if (entry->flags.enabled) {
769 XSetForeground(dpy, scr->copy_gc, WMColorPixel(scr->mtext_color));
770 } else {
771 XSetForeground(dpy, scr->copy_gc, WMColorPixel(scr->dtext_color));
774 XFillRectangle(dpy, win, scr->copy_gc, 5, y + (h - ih) / 2, iw, ih);
776 XCopyArea(dpy, indicator->image, win, scr->copy_gc, 0, 0,
777 iw, ih, 5, y+(h-ih)/2);
779 XSetClipOrigin(dpy, scr->copy_gc, 0, 0);
782 /* draw right text */
784 if (entry->rtext && entry->cascade < 0) {
785 tw = WMWidthOfString(scr->menu_entry_font, entry->rtext, strlen(entry->rtext));
787 WMDrawString(scr->wmscreen, win, color, scr->menu_entry_font, w - 6 - tw,
788 y + 3 + wPreferences.menu_text_clearance, entry->rtext, strlen(entry->rtext));
792 static void move_menus(WMenu * menu, int x, int y)
794 while (menu->parent) {
795 menu = menu->parent;
796 x -= MENUW(menu);
797 if (!wPreferences.align_menus && menu->selected_entry >= 0) {
798 y -= menu->selected_entry * menu->entry_height;
801 wMenuMove(menu, x, y, True);
804 static void makeVisible(WMenu * menu)
806 WScreen *scr = menu->frame->screen_ptr;
807 int x1, y1, x2, y2, new_x, new_y;
808 WMRect rect = wGetRectForHead(scr, wGetHeadForPointerLocation(scr));
810 if (menu->entry_no < 0)
811 return;
813 x1 = menu->frame_x;
814 y1 = menu->frame_y + menu->frame->top_width + menu->selected_entry * menu->entry_height;
815 x2 = x1 + MENUW(menu);
816 y2 = y1 + menu->entry_height;
818 new_x = x1;
819 new_y = y1;
821 if (x1 < rect.pos.x) {
822 new_x = rect.pos.x;
823 } else if (x2 >= rect.pos.x + rect.size.width) {
824 new_x = rect.pos.x + rect.size.width - MENUW(menu) - 1;
827 if (y1 < rect.pos.y) {
828 new_y = rect.pos.y;
829 } else if (y2 >= rect.pos.y + rect.size.height) {
830 new_y = rect.pos.y + rect.size.height - menu->entry_height - 1;
833 new_y = new_y - menu->frame->top_width - menu->selected_entry * menu->entry_height;
834 move_menus(menu, new_x, new_y);
837 static int check_key(WMenu * menu, XKeyEvent * event)
839 int i, ch, s;
840 char buffer[32];
842 if (XLookupString(event, buffer, 32, NULL, NULL) < 1)
843 return -1;
845 ch = toupper(buffer[0]);
847 s = (menu->selected_entry >= 0 ? menu->selected_entry + 1 : 0);
849 again:
850 for (i = s; i < menu->entry_no; i++) {
851 if (ch == toupper(menu->entries[i]->text[0])) {
852 return i;
855 /* no match. Retry from start, if previous started from a selected entry */
856 if (s != 0) {
857 s = 0;
858 goto again;
860 return -1;
863 static int keyboardMenu(WMenu * menu)
865 XEvent event;
866 KeySym ksym = NoSymbol;
867 int done = 0;
868 int index;
869 WMenuEntry *entry;
870 int old_pos_x = menu->frame_x;
871 int old_pos_y = menu->frame_y;
872 int new_x = old_pos_x, new_y = old_pos_y;
873 WMRect rect = wGetRectForHead(menu->frame->screen_ptr,
874 wGetHeadForPointerLocation(menu->frame->screen_ptr));
876 if (menu->flags.editing)
877 return False;
879 XGrabKeyboard(dpy, menu->frame->core->window, True, GrabModeAsync, GrabModeAsync, CurrentTime);
881 if (menu->frame_y + menu->frame->top_width >= rect.pos.y + rect.size.height)
882 new_y = rect.pos.y + rect.size.height - menu->frame->top_width;
884 if (menu->frame_x + MENUW(menu) >= rect.pos.x + rect.size.width)
885 new_x = rect.pos.x + rect.size.width - MENUW(menu) - 1;
887 move_menus(menu, new_x, new_y);
889 while (!done && menu->flags.mapped) {
890 XAllowEvents(dpy, AsyncKeyboard, CurrentTime);
891 WMMaskEvent(dpy, ExposureMask | ButtonMotionMask | ButtonPressMask
892 | ButtonReleaseMask | KeyPressMask | KeyReleaseMask | SubstructureNotifyMask, &event);
894 switch (event.type) {
895 case KeyPress:
896 ksym = XLookupKeysym(&event.xkey, 0);
897 if (wPreferences.vi_key_menus) {
898 switch (ksym) {
899 case XK_h:
900 ksym = XK_Left;
901 break;
903 case XK_j:
904 ksym = XK_Down;
905 break;
907 case XK_k:
908 ksym = XK_Up;
909 break;
911 case XK_l:
912 ksym = XK_Right;
913 break;
917 switch (ksym) {
918 case XK_Escape:
919 done = 1;
920 break;
922 case XK_Home:
923 #ifdef XK_KP_Home
924 case XK_KP_Home:
925 #endif
926 selectEntry(menu, 0);
927 makeVisible(menu);
928 break;
930 case XK_End:
931 #ifdef XK_KP_End
932 case XK_KP_End:
933 #endif
934 selectEntry(menu, menu->entry_no - 1);
935 makeVisible(menu);
936 break;
938 case XK_Up:
939 #ifdef XK_KP_Up
940 case XK_KP_Up:
941 #endif
942 if (menu->selected_entry <= 0)
943 selectEntry(menu, menu->entry_no - 1);
944 else
945 selectEntry(menu, menu->selected_entry - 1);
946 makeVisible(menu);
947 break;
949 case XK_Down:
950 #ifdef XK_KP_Down
951 case XK_KP_Down:
952 #endif
953 if (menu->selected_entry < 0)
954 selectEntry(menu, 0);
955 else if (menu->selected_entry == menu->entry_no - 1)
956 selectEntry(menu, 0);
957 else if (menu->selected_entry < menu->entry_no - 1)
958 selectEntry(menu, menu->selected_entry + 1);
959 makeVisible(menu);
960 break;
962 case XK_Right:
963 #ifdef XK_KP_Right
964 case XK_KP_Right:
965 #endif
966 if (menu->selected_entry >= 0) {
967 WMenuEntry *entry;
968 entry = menu->entries[menu->selected_entry];
970 if (entry->cascade >= 0 && menu->cascades
971 && menu->cascades[entry->cascade]->entry_no > 0) {
973 XUngrabKeyboard(dpy, CurrentTime);
975 selectEntry(menu->cascades[entry->cascade], 0);
976 if (!keyboardMenu(menu->cascades[entry->cascade]))
977 done = 1;
979 XGrabKeyboard(dpy, menu->frame->core->window, True,
980 GrabModeAsync, GrabModeAsync, CurrentTime);
983 break;
985 case XK_Left:
986 #ifdef XK_KP_Left
987 case XK_KP_Left:
988 #endif
989 if (menu->parent != NULL && menu->parent->selected_entry >= 0) {
990 selectEntry(menu, -1);
991 move_menus(menu, old_pos_x, old_pos_y);
992 return True;
994 break;
996 case XK_Return:
997 #ifdef XK_KP_Enter
998 case XK_KP_Enter:
999 #endif
1000 done = 2;
1001 break;
1003 default:
1004 index = check_key(menu, &event.xkey);
1005 if (index >= 0) {
1006 selectEntry(menu, index);
1009 break;
1011 default:
1012 if (event.type == ButtonPress)
1013 done = 1;
1015 WMHandleEvent(&event);
1019 XUngrabKeyboard(dpy, CurrentTime);
1021 if (done == 2 && menu->selected_entry >= 0) {
1022 entry = menu->entries[menu->selected_entry];
1023 } else {
1024 entry = NULL;
1027 if (entry && entry->callback != NULL && entry->flags.enabled && entry->cascade < 0) {
1028 #if (MENU_BLINK_COUNT > 0)
1029 int sel = menu->selected_entry;
1030 int i;
1032 for (i = 0; i < MENU_BLINK_COUNT; i++) {
1033 paintEntry(menu, sel, False);
1034 XSync(dpy, 0);
1035 wusleep(MENU_BLINK_DELAY);
1036 paintEntry(menu, sel, True);
1037 XSync(dpy, 0);
1038 wusleep(MENU_BLINK_DELAY);
1040 #endif
1041 selectEntry(menu, -1);
1043 if (!menu->flags.buttoned) {
1044 wMenuUnmap(menu);
1045 move_menus(menu, old_pos_x, old_pos_y);
1047 closeCascade(menu);
1049 (*entry->callback) (menu, entry);
1050 } else {
1051 if (!menu->flags.buttoned) {
1052 wMenuUnmap(menu);
1053 move_menus(menu, old_pos_x, old_pos_y);
1055 selectEntry(menu, -1);
1058 /* returns True if returning from a submenu to a parent menu,
1059 * False if exiting from menu */
1060 return False;
1063 void wMenuMapAt(WMenu * menu, int x, int y, int keyboard)
1065 WMRect rect = wGetRectForHead(menu->frame->screen_ptr,
1066 wGetHeadForPointerLocation(menu->frame->screen_ptr));
1068 if (!menu->flags.realized) {
1069 menu->flags.realized = 1;
1070 wMenuRealize(menu);
1072 if (!menu->flags.mapped) {
1073 if (wPreferences.wrap_menus) {
1074 if (x < rect.pos.x)
1075 x = rect.pos.x;
1076 if (y < rect.pos.y)
1077 y = rect.pos.y;
1078 if (x + MENUW(menu) > rect.pos.x + rect.size.width)
1079 x = rect.pos.x + rect.size.width - MENUW(menu);
1080 if (y + MENUH(menu) > rect.pos.y + rect.size.height)
1081 y = rect.pos.y + rect.size.height - MENUH(menu);
1084 XMoveWindow(dpy, menu->frame->core->window, x, y);
1085 menu->frame_x = x;
1086 menu->frame_y = y;
1087 XMapWindow(dpy, menu->frame->core->window);
1088 wRaiseFrame(menu->frame->core);
1089 menu->flags.mapped = 1;
1090 } else {
1091 selectEntry(menu, 0);
1094 if (keyboard)
1095 keyboardMenu(menu);
1098 void wMenuMap(WMenu * menu)
1100 if (!menu->flags.realized) {
1101 menu->flags.realized = 1;
1102 wMenuRealize(menu);
1104 if (menu->flags.app_menu && menu->parent == NULL) {
1105 menu->frame_x = menu->frame->screen_ptr->app_menu_x;
1106 menu->frame_y = menu->frame->screen_ptr->app_menu_y;
1107 XMoveWindow(dpy, menu->frame->core->window, menu->frame_x, menu->frame_y);
1109 XMapWindow(dpy, menu->frame->core->window);
1110 wRaiseFrame(menu->frame->core);
1111 menu->flags.mapped = 1;
1114 void wMenuUnmap(WMenu * menu)
1116 int i;
1118 XUnmapWindow(dpy, menu->frame->core->window);
1119 if (menu->flags.titled && menu->flags.buttoned) {
1120 wFrameWindowHideButton(menu->frame, WFF_RIGHT_BUTTON);
1122 menu->flags.buttoned = 0;
1123 menu->flags.mapped = 0;
1124 menu->flags.open_to_left = 0;
1126 for (i = 0; i < menu->cascade_no; i++) {
1127 if (menu->cascades[i] != NULL
1128 && menu->cascades[i]->flags.mapped && !menu->cascades[i]->flags.buttoned) {
1130 wMenuUnmap(menu->cascades[i]);
1133 menu->selected_entry = -1;
1136 void wMenuPaint(WMenu * menu)
1138 int i;
1140 if (!menu->flags.mapped) {
1141 return;
1144 /* paint entries */
1145 for (i = 0; i < menu->entry_no; i++) {
1146 paintEntry(menu, i, i == menu->selected_entry);
1150 void wMenuSetEnabled(WMenu * menu, int index, int enable)
1152 if (index >= menu->entry_no)
1153 return;
1154 menu->entries[index]->flags.enabled = enable;
1155 paintEntry(menu, index, index == menu->selected_entry);
1156 paintEntry(menu->brother, index, index == menu->selected_entry);
1160 static void selectEntry(WMenu * menu, int entry_no)
1162 WMenuEntry *entry;
1163 WMenu *submenu;
1164 int old_entry;
1166 if (menu->entries == NULL)
1167 return;
1169 if (entry_no >= menu->entry_no)
1170 return;
1172 old_entry = menu->selected_entry;
1173 menu->selected_entry = entry_no;
1175 if (old_entry != entry_no) {
1177 /* unselect previous entry */
1178 if (old_entry >= 0) {
1179 paintEntry(menu, old_entry, False);
1180 entry = menu->entries[old_entry];
1182 /* unmap cascade */
1183 if (entry->cascade >= 0 && menu->cascades) {
1184 if (!menu->cascades[entry->cascade]->flags.buttoned) {
1185 wMenuUnmap(menu->cascades[entry->cascade]);
1190 if (entry_no < 0) {
1191 menu->selected_entry = -1;
1192 return;
1194 entry = menu->entries[entry_no];
1196 if (entry->cascade >= 0 && menu->cascades && entry->flags.enabled) {
1197 /* Callback for when the submenu is opened.
1199 submenu = menu->cascades[entry->cascade];
1200 if (submenu && submenu->flags.brother)
1201 submenu = submenu->brother;
1203 if (entry->callback) {
1204 /* Only call the callback if the submenu is not yet mapped.
1206 if (menu->flags.brother) {
1207 if (!submenu || !submenu->flags.mapped)
1208 (*entry->callback) (menu->brother, entry);
1209 } else {
1210 if (!submenu || !submenu->flags.buttoned)
1211 (*entry->callback) (menu, entry);
1215 /* the submenu menu might have changed */
1216 submenu = menu->cascades[entry->cascade];
1218 /* map cascade */
1219 if (!submenu->flags.mapped) {
1220 int x, y;
1222 if (!submenu->flags.realized)
1223 wMenuRealize(submenu);
1224 if (wPreferences.wrap_menus) {
1225 if (menu->flags.open_to_left)
1226 submenu->flags.open_to_left = 1;
1228 if (submenu->flags.open_to_left) {
1229 x = menu->frame_x - MENUW(submenu);
1230 if (x < 0) {
1231 x = 0;
1232 submenu->flags.open_to_left = 0;
1234 } else {
1235 x = menu->frame_x + MENUW(menu);
1237 if (x + MENUW(submenu)
1238 >= menu->frame->screen_ptr->scr_width) {
1240 x = menu->frame_x - MENUW(submenu);
1241 submenu->flags.open_to_left = 1;
1244 } else {
1245 x = menu->frame_x + MENUW(menu);
1248 if (wPreferences.align_menus) {
1249 y = menu->frame_y;
1250 } else {
1251 y = menu->frame_y + menu->entry_height * entry_no;
1252 if (menu->flags.titled)
1253 y += menu->frame->top_width;
1254 if (menu->cascades[entry->cascade]->flags.titled)
1255 y -= menu->cascades[entry->cascade]->frame->top_width;
1258 wMenuMapAt(menu->cascades[entry->cascade], x, y, False);
1259 menu->cascades[entry->cascade]->parent = menu;
1260 } else {
1261 return;
1264 paintEntry(menu, entry_no, True);
1268 static WMenu *findMenu(WScreen * scr, int *x_ret, int *y_ret)
1270 WMenu *menu;
1271 WObjDescriptor *desc;
1272 Window root_ret, win, junk_win;
1273 int x, y, wx, wy;
1274 unsigned int mask;
1276 XQueryPointer(dpy, scr->root_win, &root_ret, &win, &x, &y, &wx, &wy, &mask);
1278 if (win == None)
1279 return NULL;
1281 if (XFindContext(dpy, win, wWinContext, (XPointer *) & desc) == XCNOENT)
1282 return NULL;
1284 if (desc->parent_type == WCLASS_MENU) {
1285 menu = (WMenu *) desc->parent;
1286 XTranslateCoordinates(dpy, root_ret, menu->menu->window, wx, wy, x_ret, y_ret, &junk_win);
1287 return menu;
1289 return NULL;
1292 static void closeCascade(WMenu * menu)
1294 WMenu *parent = menu->parent;
1296 if (menu->flags.brother || (!menu->flags.buttoned && (!menu->flags.app_menu || menu->parent != NULL))) {
1298 selectEntry(menu, -1);
1299 XSync(dpy, 0);
1300 #if (MENU_BLINK_DELAY > 2)
1301 wusleep(MENU_BLINK_DELAY / 2);
1302 #endif
1303 wMenuUnmap(menu);
1304 while (parent != NULL
1305 && (parent->parent != NULL || !parent->flags.app_menu || parent->flags.brother)
1306 && !parent->flags.buttoned) {
1307 selectEntry(parent, -1);
1308 wMenuUnmap(parent);
1309 parent = parent->parent;
1311 if (parent)
1312 selectEntry(parent, -1);
1316 static void closeBrotherCascadesOf(WMenu * menu)
1318 WMenu *tmp;
1319 int i;
1321 for (i = 0; i < menu->cascade_no; i++) {
1322 if (menu->cascades[i]->flags.brother) {
1323 tmp = menu->cascades[i];
1324 } else {
1325 tmp = menu->cascades[i]->brother;
1327 if (tmp->flags.mapped) {
1328 selectEntry(tmp->parent, -1);
1329 closeBrotherCascadesOf(tmp);
1330 break;
1335 #define getEntryAt(menu, x, y) ((y)<0 ? -1 : (y)/(menu->entry_height))
1337 static WMenu *parentMenu(WMenu * menu)
1339 WMenu *parent;
1340 WMenuEntry *entry;
1342 if (menu->flags.buttoned)
1343 return menu;
1345 while (menu->parent && menu->parent->flags.mapped) {
1346 parent = menu->parent;
1347 if (parent->selected_entry < 0)
1348 break;
1349 entry = parent->entries[parent->selected_entry];
1350 if (!entry->flags.enabled || entry->cascade < 0 || !parent->cascades ||
1351 parent->cascades[entry->cascade] != menu)
1352 break;
1353 menu = parent;
1354 if (menu->flags.buttoned)
1355 break;
1358 return menu;
1362 * Will raise the passed menu, if submenu = 0
1363 * If submenu > 0 will also raise all mapped submenus
1364 * until the first buttoned one
1365 * If submenu < 0 will also raise all mapped parent menus
1366 * until the first buttoned one
1369 static void raiseMenus(WMenu * menu, int submenus)
1371 WMenu *submenu;
1372 int i;
1374 if (!menu)
1375 return;
1377 wRaiseFrame(menu->frame->core);
1379 if (submenus > 0 && menu->selected_entry >= 0) {
1380 i = menu->entries[menu->selected_entry]->cascade;
1381 if (i >= 0 && menu->cascades) {
1382 submenu = menu->cascades[i];
1383 if (submenu->flags.mapped && !submenu->flags.buttoned)
1384 raiseMenus(submenu, submenus);
1387 if (submenus < 0 && !menu->flags.buttoned && menu->parent && menu->parent->flags.mapped)
1388 raiseMenus(menu->parent, submenus);
1391 WMenu *wMenuUnderPointer(WScreen * screen)
1393 WObjDescriptor *desc;
1394 Window root_ret, win;
1395 int dummy;
1396 unsigned int mask;
1398 XQueryPointer(dpy, screen->root_win, &root_ret, &win, &dummy, &dummy, &dummy, &dummy, &mask);
1400 if (win == None)
1401 return NULL;
1403 if (XFindContext(dpy, win, wWinContext, (XPointer *) & desc) == XCNOENT)
1404 return NULL;
1406 if (desc->parent_type == WCLASS_MENU)
1407 return (WMenu *) desc->parent;
1408 return NULL;
1411 static void getPointerPosition(WScreen * scr, int *x, int *y)
1413 Window root_ret, win;
1414 int wx, wy;
1415 unsigned int mask;
1417 XQueryPointer(dpy, scr->root_win, &root_ret, &win, x, y, &wx, &wy, &mask);
1420 static void getScrollAmount(WMenu * menu, int *hamount, int *vamount)
1422 WScreen *scr = menu->menu->screen_ptr;
1423 int menuX1 = menu->frame_x;
1424 int menuY1 = menu->frame_y;
1425 int menuX2 = menu->frame_x + MENUW(menu);
1426 int menuY2 = menu->frame_y + MENUH(menu);
1427 int xroot, yroot;
1428 WMRect rect = wGetRectForHead(scr, wGetHeadForPointerLocation(scr));
1430 *hamount = 0;
1431 *vamount = 0;
1433 getPointerPosition(scr, &xroot, &yroot);
1435 if (xroot <= (rect.pos.x + 1) && menuX1 < rect.pos.x) {
1436 /* scroll to the right */
1437 *hamount = WMIN(MENU_SCROLL_STEP, abs(menuX1));
1439 } else if (xroot >= (rect.pos.x + rect.size.width - 2) && menuX2 > (rect.pos.x + rect.size.width - 1)) {
1440 /* scroll to the left */
1441 *hamount = WMIN(MENU_SCROLL_STEP, abs(menuX2 - rect.pos.x - rect.size.width - 1));
1443 if (*hamount == 0)
1444 *hamount = 1;
1446 *hamount = -*hamount;
1449 if (yroot <= (rect.pos.y + 1) && menuY1 < rect.pos.y) {
1450 /* scroll down */
1451 *vamount = WMIN(MENU_SCROLL_STEP, abs(menuY1));
1453 } else if (yroot >= (rect.pos.y + rect.size.height - 2) && menuY2 > (rect.pos.y + rect.size.height - 1)) {
1454 /* scroll up */
1455 *vamount = WMIN(MENU_SCROLL_STEP, abs(menuY2 - rect.pos.y - rect.size.height - 2));
1457 *vamount = -*vamount;
1461 static void dragScrollMenuCallback(void *data)
1463 WMenu *menu = (WMenu *) data;
1464 WScreen *scr = menu->menu->screen_ptr;
1465 WMenu *parent = parentMenu(menu);
1466 int hamount, vamount;
1467 int x, y;
1468 int newSelectedEntry;
1470 getScrollAmount(menu, &hamount, &vamount);
1472 if (hamount != 0 || vamount != 0) {
1473 wMenuMove(parent, parent->frame_x + hamount, parent->frame_y + vamount, True);
1474 if (findMenu(scr, &x, &y)) {
1475 newSelectedEntry = getEntryAt(menu, x, y);
1476 selectEntry(menu, newSelectedEntry);
1477 } else {
1478 /* Pointer fell outside of menu. If the selected entry is
1479 * not a submenu, unselect it */
1480 if (menu->selected_entry >= 0 && menu->entries[menu->selected_entry]->cascade < 0)
1481 selectEntry(menu, -1);
1482 newSelectedEntry = 0;
1485 /* paranoid check */
1486 if (newSelectedEntry >= 0) {
1487 /* keep scrolling */
1488 menu->timer = WMAddTimerHandler(MENU_SCROLL_DELAY, dragScrollMenuCallback, menu);
1489 } else {
1490 menu->timer = NULL;
1492 } else {
1493 /* don't need to scroll anymore */
1494 menu->timer = NULL;
1495 if (findMenu(scr, &x, &y)) {
1496 newSelectedEntry = getEntryAt(menu, x, y);
1497 selectEntry(menu, newSelectedEntry);
1502 static void scrollMenuCallback(void *data)
1504 WMenu *menu = (WMenu *) data;
1505 WMenu *parent = parentMenu(menu);
1506 int hamount = 0; /* amount to scroll */
1507 int vamount = 0;
1509 getScrollAmount(menu, &hamount, &vamount);
1511 if (hamount != 0 || vamount != 0) {
1512 wMenuMove(parent, parent->frame_x + hamount, parent->frame_y + vamount, True);
1514 /* keep scrolling */
1515 menu->timer = WMAddTimerHandler(MENU_SCROLL_DELAY, scrollMenuCallback, menu);
1516 } else {
1517 /* don't need to scroll anymore */
1518 menu->timer = NULL;
1522 #define MENU_SCROLL_BORDER 5
1524 static int isPointNearBoder(WMenu * menu, int x, int y)
1526 int menuX1 = menu->frame_x;
1527 int menuY1 = menu->frame_y;
1528 int menuX2 = menu->frame_x + MENUW(menu);
1529 int menuY2 = menu->frame_y + MENUH(menu);
1530 int flag = 0;
1531 int head = wGetHeadForPoint(menu->frame->screen_ptr, wmkpoint(x, y));
1532 WMRect rect = wGetRectForHead(menu->frame->screen_ptr, head);
1534 /* XXX: handle screen joins properly !! */
1536 if (x >= menuX1 && x <= menuX2 &&
1537 (y < rect.pos.y + MENU_SCROLL_BORDER || y >= rect.pos.y + rect.size.height - MENU_SCROLL_BORDER))
1538 flag = 1;
1539 else if (y >= menuY1 && y <= menuY2 &&
1540 (x < rect.pos.x + MENU_SCROLL_BORDER || x >= rect.pos.x + rect.size.width - MENU_SCROLL_BORDER))
1541 flag = 1;
1543 return flag;
1546 typedef struct _delay {
1547 WMenu *menu;
1548 int ox, oy;
1549 } _delay;
1551 static void leaving(_delay * dl)
1553 wMenuMove(dl->menu, dl->ox, dl->oy, True);
1554 dl->menu->jump_back = NULL;
1555 dl->menu->menu->screen_ptr->flags.jump_back_pending = 0;
1556 wfree(dl);
1559 void wMenuScroll(WMenu * menu, XEvent * event)
1561 WMenu *smenu;
1562 WMenu *omenu = parentMenu(menu);
1563 WScreen *scr = menu->frame->screen_ptr;
1564 int done = 0;
1565 int jump_back = 0;
1566 int old_frame_x = omenu->frame_x;
1567 int old_frame_y = omenu->frame_y;
1568 XEvent ev;
1570 if (omenu->jump_back)
1571 WMDeleteTimerWithClientData(omenu->jump_back);
1573 if (( /*omenu->flags.buttoned && */ !wPreferences.wrap_menus)
1574 || omenu->flags.app_menu) {
1575 jump_back = 1;
1578 if (!wPreferences.wrap_menus)
1579 raiseMenus(omenu, True);
1580 else
1581 raiseMenus(menu, False);
1583 if (!menu->timer)
1584 scrollMenuCallback(menu);
1586 while (!done) {
1587 int x, y, on_border, on_x_edge, on_y_edge, on_title;
1588 WMRect rect;
1590 WMNextEvent(dpy, &ev);
1591 switch (ev.type) {
1592 case EnterNotify:
1593 WMHandleEvent(&ev);
1594 case MotionNotify:
1595 x = (ev.type == MotionNotify) ? ev.xmotion.x_root : ev.xcrossing.x_root;
1596 y = (ev.type == MotionNotify) ? ev.xmotion.y_root : ev.xcrossing.y_root;
1598 /* on_border is != 0 if the pointer is between the menu
1599 * and the screen border and is close enough to the border */
1600 on_border = isPointNearBoder(menu, x, y);
1602 smenu = wMenuUnderPointer(scr);
1604 if ((smenu == NULL && !on_border) || (smenu && parentMenu(smenu) != omenu)) {
1605 done = 1;
1606 break;
1609 rect = wGetRectForHead(scr, wGetHeadForPoint(scr, wmkpoint(x, y)));
1610 on_x_edge = x <= rect.pos.x + 1 || x >= rect.pos.x + rect.size.width - 2;
1611 on_y_edge = y <= rect.pos.y + 1 || y >= rect.pos.y + rect.size.height - 2;
1612 on_border = on_x_edge || on_y_edge;
1614 if (!on_border && !jump_back) {
1615 done = 1;
1616 break;
1619 if (menu->timer && (smenu != menu || (!on_y_edge && !on_x_edge))) {
1620 WMDeleteTimerHandler(menu->timer);
1621 menu->timer = NULL;
1624 if (smenu != NULL)
1625 menu = smenu;
1627 if (!menu->timer)
1628 scrollMenuCallback(menu);
1629 break;
1630 case ButtonPress:
1631 /* True if we push on title, or drag the omenu to other position */
1632 on_title = ev.xbutton.x_root >= omenu->frame_x &&
1633 ev.xbutton.x_root <= omenu->frame_x + MENUW(omenu) &&
1634 ev.xbutton.y_root >= omenu->frame_y &&
1635 ev.xbutton.y_root <= omenu->frame_y + omenu->frame->top_width;
1636 WMHandleEvent(&ev);
1637 smenu = wMenuUnderPointer(scr);
1638 if (smenu == NULL || (smenu && smenu->flags.buttoned && smenu != omenu))
1639 done = 1;
1640 else if (smenu == omenu && on_title) {
1641 jump_back = 0;
1642 done = 1;
1644 break;
1645 case KeyPress:
1646 done = 1;
1647 default:
1648 WMHandleEvent(&ev);
1649 break;
1653 if (menu->timer) {
1654 WMDeleteTimerHandler(menu->timer);
1655 menu->timer = NULL;
1658 if (jump_back) {
1659 _delay *delayer;
1660 if (!omenu->jump_back) {
1661 delayer = wmalloc(sizeof(_delay));
1662 delayer->menu = omenu;
1663 delayer->ox = old_frame_x;
1664 delayer->oy = old_frame_y;
1665 omenu->jump_back = delayer;
1666 scr->flags.jump_back_pending = 1;
1667 } else
1668 delayer = omenu->jump_back;
1669 WMAddTimerHandler(MENU_JUMP_BACK_DELAY, (WMCallback *) leaving, delayer);
1673 static void menuExpose(WObjDescriptor * desc, XEvent * event)
1675 wMenuPaint(desc->parent);
1678 typedef struct {
1679 int *delayed_select;
1680 WMenu *menu;
1681 WMHandlerID magic;
1682 } delay_data;
1684 static void delaySelection(void *data)
1686 delay_data *d = (delay_data *) data;
1687 int x, y, entry_no;
1688 WMenu *menu;
1690 d->magic = NULL;
1692 menu = findMenu(d->menu->menu->screen_ptr, &x, &y);
1693 if (menu && (d->menu == menu || d->delayed_select)) {
1694 entry_no = getEntryAt(menu, x, y);
1695 selectEntry(menu, entry_no);
1697 if (d->delayed_select)
1698 *(d->delayed_select) = 0;
1701 static void menuMouseDown(WObjDescriptor * desc, XEvent * event)
1703 WWindow *wwin;
1704 XButtonEvent *bev = &event->xbutton;
1705 WMenu *menu = desc->parent;
1706 WMenu *smenu;
1707 WScreen *scr = menu->frame->screen_ptr;
1708 WMenuEntry *entry = NULL;
1709 XEvent ev;
1710 int close_on_exit = 0;
1711 int done = 0;
1712 int delayed_select = 0;
1713 int entry_no;
1714 int x, y;
1715 int prevx, prevy;
1716 int old_frame_x = 0;
1717 int old_frame_y = 0;
1718 delay_data d_data = { NULL, NULL, NULL };
1720 /* Doesn't seem to be needed anymore (if delayed selection handler is
1721 * added only if not present). there seem to be no other side effects
1722 * from removing this and it is also possible that it was only added
1723 * to avoid problems with adding the delayed selection timer handler
1724 * multiple times
1726 /*if (menu->flags.inside_handler) {
1727 return;
1728 } */
1729 menu->flags.inside_handler = 1;
1731 if (!wPreferences.wrap_menus) {
1732 smenu = parentMenu(menu);
1733 old_frame_x = smenu->frame_x;
1734 old_frame_y = smenu->frame_y;
1735 } else if (event->xbutton.window == menu->frame->core->window) {
1736 /* This is true if the menu was launched with right click on root window */
1737 if (!d_data.magic) {
1738 delayed_select = 1;
1739 d_data.delayed_select = &delayed_select;
1740 d_data.menu = menu;
1741 d_data.magic = WMAddTimerHandler(wPreferences.dblclick_time, delaySelection, &d_data);
1745 wRaiseFrame(menu->frame->core);
1747 close_on_exit = (bev->send_event || menu->flags.brother);
1749 smenu = findMenu(scr, &x, &y);
1750 if (!smenu) {
1751 x = -1;
1752 y = -1;
1753 } else {
1754 menu = smenu;
1757 if (menu->flags.editing) {
1758 goto byebye;
1760 entry_no = getEntryAt(menu, x, y);
1761 if (entry_no >= 0) {
1762 entry = menu->entries[entry_no];
1764 if (!close_on_exit && (bev->state & ControlMask) && smenu && entry->flags.editable) {
1765 char buffer[128];
1766 char *name;
1767 int number = entry_no - 2; /* Entries "New" and "Destroy Last" appear before workspaces */
1769 name = wstrdup(scr->workspaces[number]->name);
1770 snprintf(buffer, sizeof(buffer), _("Type the name for workspace %i:"), number + 1);
1772 wMenuUnmap(scr->root_menu);
1774 if (wInputDialog(scr, _("Rename Workspace"), buffer, &name))
1775 wWorkspaceRename(scr, number, name);
1777 if (name)
1778 wfree(name);
1780 goto byebye;
1781 } else if (bev->state & ControlMask) {
1782 goto byebye;
1785 if (entry->flags.enabled && entry->cascade >= 0 && menu->cascades) {
1786 WMenu *submenu = menu->cascades[entry->cascade];
1787 /* map cascade */
1788 if (submenu->flags.mapped && !submenu->flags.buttoned && menu->selected_entry != entry_no) {
1789 wMenuUnmap(submenu);
1791 if (!submenu->flags.mapped && !delayed_select) {
1792 selectEntry(menu, entry_no);
1793 } else if (!submenu->flags.buttoned) {
1794 selectEntry(menu, -1);
1797 } else if (!delayed_select) {
1798 if (menu == scr->switch_menu && event->xbutton.button == Button3) {
1799 selectEntry(menu, entry_no);
1800 OpenWindowMenu2((WWindow *)entry->clientdata,
1801 event->xbutton.x_root,
1802 event->xbutton.y_root, False);
1803 wwin = (WWindow *)entry->clientdata;
1804 desc = &wwin->screen_ptr->window_menu->menu->descriptor;
1805 event->xany.send_event = True;
1806 (*desc->handle_mousedown)(desc, event);
1808 XUngrabPointer(dpy, CurrentTime);
1809 selectEntry(menu, -1);
1810 return;
1811 } else {
1812 selectEntry(menu, entry_no);
1816 if (!wPreferences.wrap_menus && !wPreferences.scrollable_menus) {
1817 if (!menu->timer)
1818 dragScrollMenuCallback(menu);
1822 prevx = bev->x_root;
1823 prevy = bev->y_root;
1824 while (!done) {
1825 int x, y;
1827 XAllowEvents(dpy, AsyncPointer | SyncPointer, CurrentTime);
1829 WMMaskEvent(dpy, ExposureMask | ButtonMotionMask | ButtonReleaseMask | ButtonPressMask, &ev);
1830 switch (ev.type) {
1831 case MotionNotify:
1832 smenu = findMenu(scr, &x, &y);
1834 if (smenu == NULL) {
1835 /* moved mouse out of menu */
1837 if (!delayed_select && d_data.magic) {
1838 WMDeleteTimerHandler(d_data.magic);
1839 d_data.magic = NULL;
1841 if (menu == NULL
1842 || (menu->selected_entry >= 0
1843 && menu->entries[menu->selected_entry]->cascade >= 0)) {
1844 prevx = ev.xmotion.x_root;
1845 prevy = ev.xmotion.y_root;
1847 break;
1849 selectEntry(menu, -1);
1850 menu = smenu;
1851 prevx = ev.xmotion.x_root;
1852 prevy = ev.xmotion.y_root;
1853 break;
1854 } else if (menu && menu != smenu
1855 && (menu->selected_entry < 0
1856 || menu->entries[menu->selected_entry]->cascade < 0)) {
1857 selectEntry(menu, -1);
1859 if (!delayed_select && d_data.magic) {
1860 WMDeleteTimerHandler(d_data.magic);
1861 d_data.magic = NULL;
1863 } else {
1865 /* hysteresis for item selection */
1867 /* check if the motion was to the side, indicating that
1868 * the user may want to cross to a submenu */
1869 if (!delayed_select && menu) {
1870 int dx;
1871 Bool moved_to_submenu; /* moved to direction of submenu */
1873 dx = abs(prevx - ev.xmotion.x_root);
1875 moved_to_submenu = False;
1876 if (dx > 0 /* if moved enough to the side */
1877 /* maybe a open submenu */
1878 && menu->selected_entry >= 0
1879 /* moving to the right direction */
1880 && (wPreferences.align_menus || ev.xmotion.y_root >= prevy)) {
1881 int index;
1883 index = menu->entries[menu->selected_entry]->cascade;
1884 if (index >= 0) {
1885 if (menu->cascades[index]->frame_x > menu->frame_x) {
1886 if (prevx < ev.xmotion.x_root)
1887 moved_to_submenu = True;
1888 } else {
1889 if (prevx > ev.xmotion.x_root)
1890 moved_to_submenu = True;
1895 if (menu != smenu) {
1896 if (d_data.magic) {
1897 WMDeleteTimerHandler(d_data.magic);
1898 d_data.magic = NULL;
1900 } else if (moved_to_submenu) {
1901 /* while we are moving, postpone the selection */
1902 if (d_data.magic) {
1903 WMDeleteTimerHandler(d_data.magic);
1905 d_data.delayed_select = NULL;
1906 d_data.menu = menu;
1907 d_data.magic = WMAddTimerHandler(MENU_SELECT_DELAY,
1908 delaySelection, &d_data);
1909 prevx = ev.xmotion.x_root;
1910 prevy = ev.xmotion.y_root;
1911 break;
1912 } else {
1913 if (d_data.magic) {
1914 WMDeleteTimerHandler(d_data.magic);
1915 d_data.magic = NULL;
1920 prevx = ev.xmotion.x_root;
1921 prevy = ev.xmotion.y_root;
1922 if (menu != smenu) {
1923 /* pointer crossed menus */
1924 if (menu && menu->timer) {
1925 WMDeleteTimerHandler(menu->timer);
1926 menu->timer = NULL;
1928 if (smenu)
1929 dragScrollMenuCallback(smenu);
1931 menu = smenu;
1932 if (!menu->timer)
1933 dragScrollMenuCallback(menu);
1935 if (!delayed_select) {
1936 entry_no = getEntryAt(menu, x, y);
1937 if (entry_no >= 0) {
1938 entry = menu->entries[entry_no];
1939 if (entry->flags.enabled && entry->cascade >= 0 && menu->cascades) {
1940 WMenu *submenu = menu->cascades[entry->cascade];
1941 if (submenu->flags.mapped && !submenu->flags.buttoned
1942 && menu->selected_entry != entry_no) {
1943 wMenuUnmap(submenu);
1947 selectEntry(menu, entry_no);
1949 break;
1951 case ButtonPress:
1952 break;
1954 case ButtonRelease:
1955 if (ev.xbutton.button == event->xbutton.button)
1956 done = 1;
1957 break;
1959 case Expose:
1960 WMHandleEvent(&ev);
1961 break;
1965 if (menu && menu->timer) {
1966 WMDeleteTimerHandler(menu->timer);
1967 menu->timer = NULL;
1969 if (d_data.magic != NULL) {
1970 WMDeleteTimerHandler(d_data.magic);
1971 d_data.magic = NULL;
1974 if (menu && menu->selected_entry >= 0) {
1975 entry = menu->entries[menu->selected_entry];
1976 if (entry->callback != NULL && entry->flags.enabled && entry->cascade < 0) {
1977 /* blink and erase menu selection */
1978 #if (MENU_BLINK_DELAY > 0)
1979 int sel = menu->selected_entry;
1980 int i;
1982 for (i = 0; i < MENU_BLINK_COUNT; i++) {
1983 paintEntry(menu, sel, False);
1984 XSync(dpy, 0);
1985 wusleep(MENU_BLINK_DELAY);
1986 paintEntry(menu, sel, True);
1987 XSync(dpy, 0);
1988 wusleep(MENU_BLINK_DELAY);
1990 #endif
1991 /* unmap the menu, it's parents and call the callback */
1992 if (!menu->flags.buttoned && (!menu->flags.app_menu || menu->parent != NULL)) {
1993 closeCascade(menu);
1994 } else {
1995 selectEntry(menu, -1);
1997 (*entry->callback) (menu, entry);
1999 /* If the user double clicks an entry, the entry will
2000 * be executed twice, which is not good for things like
2001 * the root menu. So, ignore any clicks that were generated
2002 * while the entry was being executed */
2003 while (XCheckTypedWindowEvent(dpy, menu->menu->window, ButtonPress, &ev)) ;
2004 } else if (entry->callback != NULL && entry->cascade < 0) {
2005 selectEntry(menu, -1);
2006 } else {
2007 if (entry->cascade >= 0 && menu->cascades && menu->cascades[entry->cascade]->flags.brother) {
2008 selectEntry(menu, -1);
2013 if (((WMenu *) desc->parent)->flags.brother || close_on_exit || !smenu)
2014 closeCascade(desc->parent);
2016 /* close the cascade windows that should not remain opened */
2017 closeBrotherCascadesOf(desc->parent);
2019 if (!wPreferences.wrap_menus)
2020 wMenuMove(parentMenu(desc->parent), old_frame_x, old_frame_y, True);
2022 byebye:
2023 /* Just to be sure in case we skip the 2 above because of a goto byebye */
2024 if (menu && menu->timer) {
2025 WMDeleteTimerHandler(menu->timer);
2026 menu->timer = NULL;
2028 if (d_data.magic != NULL) {
2029 WMDeleteTimerHandler(d_data.magic);
2030 d_data.magic = NULL;
2033 ((WMenu *) desc->parent)->flags.inside_handler = 0;
2036 void wMenuMove(WMenu * menu, int x, int y, int submenus)
2038 WMenu *submenu;
2039 int i;
2041 if (!menu)
2042 return;
2044 menu->frame_x = x;
2045 menu->frame_y = y;
2046 XMoveWindow(dpy, menu->frame->core->window, x, y);
2048 if (submenus > 0 && menu->selected_entry >= 0) {
2049 i = menu->entries[menu->selected_entry]->cascade;
2051 if (i >= 0 && menu->cascades) {
2052 submenu = menu->cascades[i];
2053 if (submenu->flags.mapped && !submenu->flags.buttoned) {
2054 if (wPreferences.align_menus) {
2055 wMenuMove(submenu, x + MENUW(menu), y, submenus);
2056 } else {
2057 wMenuMove(submenu, x + MENUW(menu),
2058 y + submenu->entry_height * menu->selected_entry, submenus);
2063 if (submenus < 0 && menu->parent != NULL && menu->parent->flags.mapped && !menu->parent->flags.buttoned) {
2064 if (wPreferences.align_menus) {
2065 wMenuMove(menu->parent, x - MENUW(menu->parent), y, submenus);
2066 } else {
2067 wMenuMove(menu->parent, x - MENUW(menu->parent), menu->frame_y
2068 - menu->parent->entry_height * menu->parent->selected_entry, submenus);
2073 static void changeMenuLevels(WMenu * menu, int lower)
2075 int i;
2077 if (!lower) {
2078 ChangeStackingLevel(menu->frame->core, (!menu->parent ? WMMainMenuLevel : WMSubmenuLevel));
2079 wRaiseFrame(menu->frame->core);
2080 menu->flags.lowered = 0;
2081 } else {
2082 ChangeStackingLevel(menu->frame->core, WMNormalLevel);
2083 wLowerFrame(menu->frame->core);
2084 menu->flags.lowered = 1;
2086 for (i = 0; i < menu->cascade_no; i++) {
2087 if (menu->cascades[i]
2088 && !menu->cascades[i]->flags.buttoned && menu->cascades[i]->flags.lowered != lower) {
2089 changeMenuLevels(menu->cascades[i], lower);
2094 static void menuTitleDoubleClick(WCoreWindow * sender, void *data, XEvent * event)
2096 WMenu *menu = data;
2097 int lower;
2099 if (event->xbutton.state & MOD_MASK) {
2100 if (menu->flags.lowered) {
2101 lower = 0;
2102 } else {
2103 lower = 1;
2105 changeMenuLevels(menu, lower);
2109 static void menuTitleMouseDown(WCoreWindow * sender, void *data, XEvent * event)
2111 WMenu *menu = data;
2112 WMenu *tmp;
2113 XEvent ev;
2114 int x = menu->frame_x, y = menu->frame_y;
2115 int dx = event->xbutton.x_root, dy = event->xbutton.y_root;
2116 int i, lower;
2117 Bool started;
2119 /* can't touch the menu copy */
2120 if (menu->flags.brother)
2121 return;
2123 if (event->xbutton.button != Button1 && event->xbutton.button != Button2)
2124 return;
2126 if (event->xbutton.state & MOD_MASK) {
2127 wLowerFrame(menu->frame->core);
2128 lower = 1;
2129 } else {
2130 wRaiseFrame(menu->frame->core);
2131 lower = 0;
2133 tmp = menu;
2135 /* lower/raise all submenus */
2136 while (1) {
2137 if (tmp->selected_entry >= 0 && tmp->cascades && tmp->entries[tmp->selected_entry]->cascade >= 0) {
2138 tmp = tmp->cascades[tmp->entries[tmp->selected_entry]->cascade];
2139 if (!tmp || !tmp->flags.mapped)
2140 break;
2141 if (lower)
2142 wLowerFrame(tmp->frame->core);
2143 else
2144 wRaiseFrame(tmp->frame->core);
2145 } else {
2146 break;
2150 /* tear off the menu if it's a root menu or a cascade
2151 application menu */
2152 if (!menu->flags.buttoned && !menu->flags.brother && (!menu->flags.app_menu || menu->parent != NULL)) {
2153 menu->flags.buttoned = 1;
2154 wFrameWindowShowButton(menu->frame, WFF_RIGHT_BUTTON);
2155 if (menu->parent) {
2156 /* turn off selected menu entry in parent menu */
2157 selectEntry(menu->parent, -1);
2159 /* make parent map the copy in place of the original */
2160 for (i = 0; i < menu->parent->cascade_no; i++) {
2161 if (menu->parent->cascades[i] == menu) {
2162 menu->parent->cascades[i] = menu->brother;
2163 break;
2169 started = False;
2170 while (1) {
2171 WMMaskEvent(dpy, ButtonMotionMask | ButtonReleaseMask | ButtonPressMask | ExposureMask, &ev);
2172 switch (ev.type) {
2173 case MotionNotify:
2174 if (started) {
2175 x += ev.xmotion.x_root - dx;
2176 y += ev.xmotion.y_root - dy;
2177 dx = ev.xmotion.x_root;
2178 dy = ev.xmotion.y_root;
2179 wMenuMove(menu, x, y, True);
2180 } else {
2181 if (abs(ev.xmotion.x_root - dx) > MOVE_THRESHOLD
2182 || abs(ev.xmotion.y_root - dy) > MOVE_THRESHOLD) {
2183 started = True;
2184 XGrabPointer(dpy, menu->frame->titlebar->window, False,
2185 ButtonMotionMask | ButtonReleaseMask
2186 | ButtonPressMask,
2187 GrabModeAsync, GrabModeAsync, None,
2188 wCursor[WCUR_MOVE], CurrentTime);
2191 break;
2193 case ButtonPress:
2194 break;
2196 case ButtonRelease:
2197 if (ev.xbutton.button != event->xbutton.button)
2198 break;
2199 XUngrabPointer(dpy, CurrentTime);
2200 return;
2202 default:
2203 WMHandleEvent(&ev);
2204 break;
2210 *----------------------------------------------------------------------
2211 * menuCloseClick--
2212 * Handles mouse click on the close button of menus. The menu is
2213 * closed when the button is clicked.
2215 * Side effects:
2216 * The closed menu is reinserted at it's parent menus
2217 * cascade list.
2218 *----------------------------------------------------------------------
2220 static void menuCloseClick(WCoreWindow * sender, void *data, XEvent * event)
2222 WMenu *menu = (WMenu *) data;
2223 WMenu *parent = menu->parent;
2224 int i;
2226 if (parent) {
2227 for (i = 0; i < parent->cascade_no; i++) {
2228 /* find the entry that points to the copy */
2229 if (parent->cascades[i] == menu->brother) {
2230 /* make it point to the original */
2231 parent->cascades[i] = menu;
2232 menu->parent = parent;
2233 break;
2237 wMenuUnmap(menu);
2240 static void saveMenuInfo(WMPropList * dict, WMenu * menu, WMPropList * key)
2242 WMPropList *value, *list;
2243 char buffer[256];
2245 snprintf(buffer, sizeof(buffer), "%i,%i", menu->frame_x, menu->frame_y);
2246 value = WMCreatePLString(buffer);
2247 list = WMCreatePLArray(value, NULL);
2248 if (menu->flags.lowered)
2249 WMAddToPLArray(list, WMCreatePLString("lowered"));
2250 WMPutInPLDictionary(dict, key, list);
2251 WMReleasePropList(value);
2252 WMReleasePropList(list);
2255 void wMenuSaveState(WScreen * scr)
2257 WMPropList *menus, *key;
2258 int save_menus = 0;
2260 menus = WMCreatePLDictionary(NULL, NULL);
2262 if (scr->switch_menu && scr->switch_menu->flags.buttoned) {
2263 key = WMCreatePLString("SwitchMenu");
2264 saveMenuInfo(menus, scr->switch_menu, key);
2265 WMReleasePropList(key);
2266 save_menus = 1;
2269 if (saveMenuRecurs(menus, scr, scr->root_menu))
2270 save_menus = 1;
2272 if (scr->workspace_menu && scr->workspace_menu->flags.buttoned) {
2273 key = WMCreatePLString("WorkspaceMenu");
2274 saveMenuInfo(menus, scr->workspace_menu, key);
2275 WMReleasePropList(key);
2276 save_menus = 1;
2279 if (save_menus) {
2280 key = WMCreatePLString("Menus");
2281 WMPutInPLDictionary(scr->session_state, key, menus);
2282 WMReleasePropList(key);
2284 WMReleasePropList(menus);
2287 static Bool getMenuPath(WMenu * menu, char *buffer, int bufSize)
2289 Bool ok = True;
2290 int len = 0;
2292 if (!menu->flags.titled || !menu->frame->title[0])
2293 return False;
2295 len = strlen(menu->frame->title);
2296 if (len >= bufSize)
2297 return False;
2299 if (menu->parent) {
2300 ok = getMenuPath(menu->parent, buffer, bufSize - len - 1);
2301 if (!ok)
2302 return False;
2305 strcat(buffer, "\\");
2306 strcat(buffer, menu->frame->title);
2308 return True;
2311 static Bool saveMenuRecurs(WMPropList * menus, WScreen * scr, WMenu * menu)
2313 WMPropList *key;
2314 int save_menus = 0, i;
2315 char buffer[512];
2316 Bool ok = True;
2318 if (menu->flags.brother)
2319 menu = menu->brother;
2321 if (menu->flags.buttoned && menu != scr->switch_menu) {
2323 buffer[0] = '\0';
2324 ok = getMenuPath(menu, buffer, 510);
2326 if (ok) {
2327 key = WMCreatePLString(buffer);
2328 saveMenuInfo(menus, menu, key);
2329 WMReleasePropList(key);
2330 save_menus = 1;
2334 if (ok) {
2335 for (i = 0; i < menu->cascade_no; i++) {
2336 if (saveMenuRecurs(menus, scr, menu->cascades[i]))
2337 save_menus = 1;
2340 return save_menus;
2343 #define COMPLAIN(key) wwarning(_("bad value in menus state info: %s"), key)
2345 static Bool getMenuInfo(WMPropList * info, int *x, int *y, Bool * lowered)
2347 WMPropList *pos;
2349 *lowered = False;
2351 if (WMIsPLArray(info)) {
2352 WMPropList *flags;
2353 pos = WMGetFromPLArray(info, 0);
2354 flags = WMGetFromPLArray(info, 1);
2355 if (flags != NULL && WMIsPLString(flags) && WMGetFromPLString(flags) != NULL
2356 && strcmp(WMGetFromPLString(flags), "lowered") == 0) {
2357 *lowered = True;
2359 } else {
2360 pos = info;
2363 if (pos != NULL && WMIsPLString(pos)) {
2364 if (sscanf(WMGetFromPLString(pos), "%i,%i", x, y) != 2)
2365 COMPLAIN("Position");
2366 } else {
2367 COMPLAIN("(position, flags...)");
2368 return False;
2371 return True;
2374 static int restoreMenu(WScreen * scr, WMPropList * menu, int which)
2376 int x, y;
2377 Bool lowered = False;
2378 WMenu *pmenu = NULL;
2380 if (!menu)
2381 return False;
2383 if (!getMenuInfo(menu, &x, &y, &lowered))
2384 return False;
2386 if (which & WSS_SWITCHMENU) {
2387 OpenSwitchMenu(scr, x, y, False);
2388 pmenu = scr->switch_menu;
2391 if (pmenu) {
2392 int width = MENUW(pmenu);
2393 int height = MENUH(pmenu);
2394 WMRect rect = wGetRectForHead(scr, wGetHeadForPointerLocation(scr));
2396 if (lowered) {
2397 changeMenuLevels(pmenu, True);
2400 if (x < rect.pos.x - width)
2401 x = rect.pos.x;
2402 if (x > rect.pos.x + rect.size.width)
2403 x = rect.pos.x + rect.size.width - width;
2404 if (y < rect.pos.y)
2405 y = rect.pos.y;
2406 if (y > rect.pos.y + rect.size.height)
2407 y = rect.pos.y + rect.size.height - height;
2409 wMenuMove(pmenu, x, y, True);
2410 pmenu->flags.buttoned = 1;
2411 wFrameWindowShowButton(pmenu->frame, WFF_RIGHT_BUTTON);
2412 return True;
2414 return False;
2417 static int restoreMenuRecurs(WScreen * scr, WMPropList * menus, WMenu * menu, char *path)
2419 WMPropList *key, *entry;
2420 char buffer[512];
2421 int i, x, y, res;
2422 Bool lowered;
2424 if (strlen(path) + strlen(menu->frame->title) > 510)
2425 return False;
2427 snprintf(buffer, sizeof(buffer), "%s\\%s", path, menu->frame->title);
2428 key = WMCreatePLString(buffer);
2429 entry = WMGetFromPLDictionary(menus, key);
2430 res = False;
2432 if (entry && getMenuInfo(entry, &x, &y, &lowered)) {
2434 if (!menu->flags.mapped) {
2435 int width = MENUW(menu);
2436 int height = MENUH(menu);
2437 WMRect rect = wGetRectForHead(scr, wGetHeadForPointerLocation(scr));
2439 wMenuMapAt(menu, x, y, False);
2441 if (menu->parent) {
2442 /* make parent map the copy in place of the original */
2443 for (i = 0; i < menu->parent->cascade_no; i++) {
2444 if (menu->parent->cascades[i] == menu) {
2445 menu->parent->cascades[i] = menu->brother;
2446 break;
2450 if (lowered) {
2451 changeMenuLevels(menu, True);
2454 if (x < rect.pos.x - width)
2455 x = rect.pos.x;
2456 if (x > rect.pos.x + rect.size.width)
2457 x = rect.pos.x + rect.size.width - width;
2458 if (y < rect.pos.y)
2459 y = rect.pos.y;
2460 if (y > rect.pos.y + rect.size.height)
2461 y = rect.pos.y + rect.size.height - height;
2463 wMenuMove(menu, x, y, True);
2464 menu->flags.buttoned = 1;
2465 wFrameWindowShowButton(menu->frame, WFF_RIGHT_BUTTON);
2466 res = True;
2470 WMReleasePropList(key);
2472 for (i = 0; i < menu->cascade_no; i++) {
2473 if (restoreMenuRecurs(scr, menus, menu->cascades[i], buffer) != False)
2474 res = True;
2477 return res;
2480 void wMenuRestoreState(WScreen * scr)
2482 WMPropList *menus, *menu, *key, *skey;
2484 if (!scr->session_state) {
2485 return;
2488 key = WMCreatePLString("Menus");
2489 menus = WMGetFromPLDictionary(scr->session_state, key);
2490 WMReleasePropList(key);
2492 if (!menus)
2493 return;
2495 /* restore menus */
2497 skey = WMCreatePLString("SwitchMenu");
2498 menu = WMGetFromPLDictionary(menus, skey);
2499 WMReleasePropList(skey);
2500 restoreMenu(scr, menu, WSS_SWITCHMENU);
2502 if (!scr->root_menu) {
2503 OpenRootMenu(scr, scr->scr_width * 2, 0, False);
2504 wMenuUnmap(scr->root_menu);
2506 restoreMenuRecurs(scr, menus, scr->root_menu, "");