wmaker: Marked args as unused for compiler in XExposeEvent callback code
[wmaker-crm.git] / src / menu.c
blobe0a32a668960c9cb5a3b1cdf2efeda2de9478ae5
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 "winmenu.h"
41 #include "stacking.h"
42 #include "xinerama.h"
43 #include "workspace.h"
44 #include "dialog.h"
45 #include "rootmenu.h"
46 #include "switchmenu.h"
49 #define MOD_MASK wPreferences.modifier_mask
51 #define MENU_SCROLL_STEP menuScrollParameters[(int)wPreferences.menu_scroll_speed].steps
52 #define MENU_SCROLL_DELAY menuScrollParameters[(int)wPreferences.menu_scroll_speed].delay
54 #define MENUW(m) ((m)->frame->core->width+2*(m)->frame->screen_ptr->frame_border_width)
55 #define MENUH(m) ((m)->frame->core->height+2*(m)->frame->screen_ptr->frame_border_width)
57 /***** Local Stuff ******/
59 #define WSS_ROOTMENU (1<<0)
60 #define WSS_SWITCHMENU (1<<1)
61 #define WSS_WSMENU (1<<2)
63 static struct {
64 int steps;
65 int delay;
66 } menuScrollParameters[5] = {
68 MENU_SCROLL_STEPS_UF, MENU_SCROLL_DELAY_UF}, {
69 MENU_SCROLL_STEPS_F, MENU_SCROLL_DELAY_F}, {
70 MENU_SCROLL_STEPS_M, MENU_SCROLL_DELAY_M}, {
71 MENU_SCROLL_STEPS_S, MENU_SCROLL_DELAY_S}, {
72 MENU_SCROLL_STEPS_US, MENU_SCROLL_DELAY_US}};
74 static void menuMouseDown(WObjDescriptor * desc, XEvent * event);
75 static void menuExpose(WObjDescriptor * desc, XEvent * event);
76 static void menuTitleDoubleClick(WCoreWindow * sender, void *data, XEvent * event);
77 static void menuTitleMouseDown(WCoreWindow * sender, void *data, XEvent * event);
78 static void menuCloseClick(WCoreWindow * sender, void *data, XEvent * event);
79 static void updateTexture(WMenu * menu);
80 static int saveMenuRecurs(WMPropList * menus, WScreen * scr, WMenu * menu);
81 static int restoreMenuRecurs(WScreen *scr, WMPropList *menus, WMenu *menu, const char *path);
82 static void selectEntry(WMenu * menu, int entry_no);
83 static void closeCascade(WMenu * menu);
85 /****** Notification Observers ******/
87 static void appearanceObserver(void *self, WMNotification * notif)
89 WMenu *menu = (WMenu *) self;
90 uintptr_t flags = (uintptr_t)WMGetNotificationClientData(notif);
92 if (!menu->flags.realized)
93 return;
95 if (WMGetNotificationName(notif) == WNMenuAppearanceSettingsChanged) {
96 if (flags & WFontSettings) {
97 menu->flags.realized = 0;
98 wMenuRealize(menu);
100 if (flags & WTextureSettings) {
101 if (!menu->flags.brother)
102 updateTexture(menu);
104 if (flags & (WTextureSettings | WColorSettings)) {
105 wMenuPaint(menu);
107 } else if (menu->flags.titled) {
109 if (flags & WFontSettings) {
110 menu->flags.realized = 0;
111 wMenuRealize(menu);
113 if (flags & WTextureSettings) {
114 menu->frame->flags.need_texture_remake = 1;
116 if (flags & (WColorSettings | WTextureSettings)) {
117 wFrameWindowPaint(menu->frame);
122 /************************************/
125 *----------------------------------------------------------------------
126 * wMenuCreate--
127 * Creates a new empty menu with the specified title. If main_menu
128 * is True, the created menu will be a main menu, which has some special
129 * properties such as being placed over other normal menus.
130 * If title is NULL, the menu will have no titlebar.
132 * Returns:
133 * The created menu.
134 *----------------------------------------------------------------------
136 WMenu *wMenuCreate(WScreen *screen, const char *title, int main_menu)
138 WMenu *menu;
139 static int brother = 0;
140 int tmp, flags;
142 menu = wmalloc(sizeof(WMenu));
144 #ifdef SINGLE_MENULEVEL
145 tmp = WMSubmenuLevel;
146 #else
147 tmp = (main_menu ? WMMainMenuLevel : WMSubmenuLevel);
148 #endif
150 flags = WFF_SINGLE_STATE | WFF_BORDER;
151 if (title) {
152 flags |= WFF_TITLEBAR | WFF_RIGHT_BUTTON;
153 menu->flags.titled = 1;
155 menu->frame =
156 wFrameWindowCreate(screen, tmp, 8, 2, 1, 1, &wPreferences.menu_title_clearance,
157 &wPreferences.menu_title_min_height,
158 &wPreferences.menu_title_max_height,
159 flags,
160 screen->menu_title_texture, NULL,
161 screen->menu_title_color, &screen->menu_title_font,
162 screen->w_depth, screen->w_visual, screen->w_colormap);
164 menu->frame->core->descriptor.parent = menu;
165 menu->frame->core->descriptor.parent_type = WCLASS_MENU;
166 menu->frame->core->descriptor.handle_mousedown = menuMouseDown;
168 wFrameWindowHideButton(menu->frame, WFF_RIGHT_BUTTON);
170 if (title) {
171 menu->frame->title = wstrdup(title);
174 menu->frame->flags.justification = WTJ_LEFT;
176 menu->frame->rbutton_image = screen->b_pixmaps[WBUT_CLOSE];
178 menu->entry_no = 0;
179 menu->alloced_entries = 0;
180 menu->selected_entry = -1;
181 menu->entries = NULL;
183 menu->frame_x = screen->app_menu_x;
184 menu->frame_y = screen->app_menu_y;
186 menu->frame->child = menu;
188 menu->flags.lowered = 0;
190 /* create borders */
191 if (title) {
192 /* setup object descriptors */
193 menu->frame->on_mousedown_titlebar = menuTitleMouseDown;
194 menu->frame->on_dblclick_titlebar = menuTitleDoubleClick;
197 menu->frame->on_click_right = menuCloseClick;
199 menu->menu = wCoreCreate(menu->frame->core, 0, menu->frame->top_width, menu->frame->core->width, 10);
201 menu->menu->descriptor.parent = menu;
202 menu->menu->descriptor.parent_type = WCLASS_MENU;
203 menu->menu->descriptor.handle_expose = menuExpose;
204 menu->menu->descriptor.handle_mousedown = menuMouseDown;
206 menu->menu_texture_data = None;
208 XMapWindow(dpy, menu->menu->window);
210 XFlush(dpy);
212 if (!brother) {
213 brother = 1;
214 menu->brother = wMenuCreate(screen, title, main_menu);
215 brother = 0;
216 menu->brother->flags.brother = 1;
217 menu->brother->brother = menu;
219 WMAddNotificationObserver(appearanceObserver, menu, WNMenuAppearanceSettingsChanged, menu);
221 WMAddNotificationObserver(appearanceObserver, menu, WNMenuTitleAppearanceSettingsChanged, menu);
223 return menu;
226 WMenu *wMenuCreateForApp(WScreen *screen, const char *title, int main_menu)
228 WMenu *menu;
230 menu = wMenuCreate(screen, title, main_menu);
231 if (!menu)
232 return NULL;
233 menu->flags.app_menu = 1;
234 menu->brother->flags.app_menu = 1;
236 return menu;
239 static void insertEntry(WMenu * menu, WMenuEntry * entry, int index)
241 int i;
243 for (i = menu->entry_no - 1; i >= index; i--) {
244 menu->entries[i]->order++;
245 menu->entries[i + 1] = menu->entries[i];
247 menu->entries[index] = entry;
250 WMenuEntry *wMenuInsertCallback(WMenu *menu, int index, const char *text,
251 void (*callback) (WMenu * menu, WMenuEntry * entry), void *clientdata)
253 WMenuEntry *entry;
255 menu->flags.realized = 0;
256 menu->brother->flags.realized = 0;
258 /* reallocate array if it's too small */
259 if (menu->entry_no >= menu->alloced_entries) {
260 void *tmp;
262 tmp = wrealloc(menu->entries, sizeof(WMenuEntry) * (menu->alloced_entries + 5));
264 menu->entries = tmp;
265 menu->alloced_entries += 5;
267 menu->brother->entries = tmp;
268 menu->brother->alloced_entries = menu->alloced_entries;
270 entry = wmalloc(sizeof(WMenuEntry));
271 entry->flags.enabled = 1;
272 entry->text = wstrdup(text);
273 entry->cascade = -1;
274 entry->clientdata = clientdata;
275 entry->callback = callback;
276 if (index < 0 || index >= menu->entry_no) {
277 entry->order = menu->entry_no;
278 menu->entries[menu->entry_no] = entry;
279 } else {
280 entry->order = index;
281 insertEntry(menu, entry, index);
284 menu->entry_no++;
285 menu->brother->entry_no = menu->entry_no;
287 return entry;
290 void wMenuEntrySetCascade(WMenu * menu, WMenuEntry * entry, WMenu * cascade)
292 WMenu *brother = menu->brother;
293 int i, done;
295 assert(menu->flags.brother == 0);
297 if (entry->cascade >= 0) {
298 menu->flags.realized = 0;
299 brother->flags.realized = 0;
302 cascade->parent = menu;
304 cascade->brother->parent = brother;
306 done = 0;
307 for (i = 0; i < menu->cascade_no; i++) {
308 if (menu->cascades[i] == NULL) {
309 menu->cascades[i] = cascade;
310 brother->cascades[i] = cascade->brother;
311 done = 1;
312 entry->cascade = i;
313 break;
316 if (!done) {
317 entry->cascade = menu->cascade_no;
319 menu->cascades = wrealloc(menu->cascades, sizeof(WMenu) * (menu->cascade_no + 1));
320 menu->cascades[menu->cascade_no++] = cascade;
322 brother->cascades = wrealloc(brother->cascades, sizeof(WMenu) * (brother->cascade_no + 1));
323 brother->cascades[brother->cascade_no++] = cascade->brother;
326 if (menu->flags.lowered) {
328 cascade->flags.lowered = 1;
329 ChangeStackingLevel(cascade->frame->core, WMNormalLevel);
331 cascade->brother->flags.lowered = 1;
332 ChangeStackingLevel(cascade->brother->frame->core, WMNormalLevel);
335 if (!menu->flags.realized)
336 wMenuRealize(menu);
339 void wMenuEntryRemoveCascade(WMenu * menu, WMenuEntry * entry)
341 assert(menu->flags.brother == 0);
343 /* destroy cascade menu */
344 if (entry->cascade >= 0 && menu->cascades && menu->cascades[entry->cascade] != NULL) {
346 wMenuDestroy(menu->cascades[entry->cascade], True);
348 menu->cascades[entry->cascade] = NULL;
349 menu->brother->cascades[entry->cascade] = NULL;
351 entry->cascade = -1;
355 void wMenuRemoveItem(WMenu * menu, int index)
357 int i;
359 if (menu->flags.brother) {
360 wMenuRemoveItem(menu->brother, index);
361 return;
364 if (index >= menu->entry_no)
365 return;
367 /* destroy cascade menu */
368 wMenuEntryRemoveCascade(menu, menu->entries[index]);
370 /* destroy unshared data */
372 if (menu->entries[index]->text)
373 wfree(menu->entries[index]->text);
375 if (menu->entries[index]->rtext)
376 wfree(menu->entries[index]->rtext);
378 if (menu->entries[index]->free_cdata && menu->entries[index]->clientdata)
379 (*menu->entries[index]->free_cdata) (menu->entries[index]->clientdata);
381 wfree(menu->entries[index]);
383 for (i = index; i < menu->entry_no - 1; i++) {
384 menu->entries[i + 1]->order--;
385 menu->entries[i] = menu->entries[i + 1];
387 menu->entry_no--;
388 menu->brother->entry_no--;
391 static Pixmap renderTexture(WMenu * menu)
393 RImage *img;
394 Pixmap pix;
395 int i;
396 RColor light;
397 RColor dark;
398 RColor mid;
399 WScreen *scr = menu->menu->screen_ptr;
400 WTexture *texture = scr->menu_item_texture;
402 if (wPreferences.menu_style == MS_NORMAL) {
403 img = wTextureRenderImage(texture, menu->menu->width, menu->entry_height, WREL_MENUENTRY);
404 } else {
405 img = wTextureRenderImage(texture, menu->menu->width, menu->menu->height + 1, WREL_MENUENTRY);
407 if (!img) {
408 wwarning(_("could not render texture: %s"), RMessageForError(RErrorCode));
410 return None;
413 if (wPreferences.menu_style == MS_SINGLE_TEXTURE) {
414 light.alpha = 0;
415 light.red = light.green = light.blue = 80;
417 dark.alpha = 255;
418 dark.red = dark.green = dark.blue = 0;
420 mid.alpha = 0;
421 mid.red = mid.green = mid.blue = 40;
423 for (i = 1; i < menu->entry_no; i++) {
424 ROperateLine(img, RSubtractOperation, 0, i * menu->entry_height - 2,
425 menu->menu->width - 1, i * menu->entry_height - 2, &mid);
427 RDrawLine(img, 0, i * menu->entry_height - 1,
428 menu->menu->width - 1, i * menu->entry_height - 1, &dark);
430 ROperateLine(img, RAddOperation, 0, i * menu->entry_height,
431 menu->menu->width - 1, i * menu->entry_height, &light);
434 if (!RConvertImage(scr->rcontext, img, &pix)) {
435 wwarning(_("error rendering image:%s"), RMessageForError(RErrorCode));
437 RReleaseImage(img);
439 return pix;
442 static void updateTexture(WMenu * menu)
444 WScreen *scr = menu->menu->screen_ptr;
446 /* setup background texture */
447 if (scr->menu_item_texture->any.type != WTEX_SOLID) {
448 if (!menu->flags.brother) {
449 FREE_PIXMAP(menu->menu_texture_data);
451 menu->menu_texture_data = renderTexture(menu);
453 XSetWindowBackgroundPixmap(dpy, menu->menu->window, menu->menu_texture_data);
454 XClearWindow(dpy, menu->menu->window);
456 XSetWindowBackgroundPixmap(dpy, menu->brother->menu->window, menu->menu_texture_data);
457 XClearWindow(dpy, menu->brother->menu->window);
459 } else {
460 XSetWindowBackground(dpy, menu->menu->window, scr->menu_item_texture->any.color.pixel);
461 XClearWindow(dpy, menu->menu->window);
465 void wMenuRealize(WMenu * menu)
467 int i;
468 int width, rwidth, mrwidth, mwidth;
469 int theight, twidth, eheight;
470 WScreen *scr = menu->frame->screen_ptr;
471 static int brother_done = 0;
472 int flags;
474 if (!brother_done) {
475 brother_done = 1;
476 wMenuRealize(menu->brother);
477 brother_done = 0;
480 flags = WFF_SINGLE_STATE | WFF_BORDER;
481 if (menu->flags.titled)
482 flags |= WFF_TITLEBAR | WFF_RIGHT_BUTTON;
484 wFrameWindowUpdateBorders(menu->frame, flags);
486 if (menu->flags.titled) {
487 twidth = WMWidthOfString(scr->menu_title_font, menu->frame->title, strlen(menu->frame->title));
488 theight = menu->frame->top_width;
489 twidth += theight + (wPreferences.new_style == TS_NEW ? 16 : 8);
490 } else {
491 twidth = 0;
492 theight = 0;
494 eheight = WMFontHeight(scr->menu_entry_font) + 6 + wPreferences.menu_text_clearance * 2;
495 menu->entry_height = eheight;
496 mrwidth = 0;
497 mwidth = 0;
498 for (i = 0; i < menu->entry_no; i++) {
499 char *text;
501 /* search widest text */
502 text = menu->entries[i]->text;
503 width = WMWidthOfString(scr->menu_entry_font, text, strlen(text)) + 10;
505 if (menu->entries[i]->flags.indicator) {
506 width += MENU_INDICATOR_SPACE;
509 if (width > mwidth)
510 mwidth = width;
512 /* search widest text on right */
513 text = menu->entries[i]->rtext;
514 if (text)
515 rwidth = WMWidthOfString(scr->menu_entry_font, text, strlen(text))
516 + 10;
517 else if (menu->entries[i]->cascade >= 0)
518 rwidth = 16;
519 else
520 rwidth = 4;
522 if (rwidth > mrwidth)
523 mrwidth = rwidth;
525 mwidth += mrwidth;
527 if (mwidth < twidth)
528 mwidth = twidth;
530 wCoreConfigure(menu->menu, 0, theight, mwidth, menu->entry_no * eheight - 1);
532 wFrameWindowResize(menu->frame, mwidth, menu->entry_no * eheight - 1
533 + menu->frame->top_width + menu->frame->bottom_width);
535 updateTexture(menu);
537 menu->flags.realized = 1;
539 if (menu->flags.mapped)
540 wMenuPaint(menu);
541 if (menu->brother->flags.mapped)
542 wMenuPaint(menu->brother);
545 void wMenuDestroy(WMenu * menu, int recurse)
547 int i;
549 WMRemoveNotificationObserver(menu);
551 /* remove any pending timers */
552 if (menu->timer)
553 WMDeleteTimerHandler(menu->timer);
554 menu->timer = NULL;
556 /* call destroy handler */
557 if (menu->on_destroy)
558 (*menu->on_destroy) (menu);
560 /* Destroy items if this menu own them. If this is the "brother" menu,
561 * leave them alone as it is shared by them.
563 if (!menu->flags.brother) {
564 for (i = 0; i < menu->entry_no; i++) {
566 wfree(menu->entries[i]->text);
568 if (menu->entries[i]->rtext)
569 wfree(menu->entries[i]->rtext);
570 #ifdef USER_MENU
572 if (menu->entries[i]->instances) {
573 WMReleasePropList(menu->entries[i]->instances);
575 #endif /* USER_MENU */
577 if (menu->entries[i]->free_cdata && menu->entries[i]->clientdata) {
578 (*menu->entries[i]->free_cdata) (menu->entries[i]->clientdata);
580 wfree(menu->entries[i]);
583 if (recurse) {
584 for (i = 0; i < menu->cascade_no; i++) {
585 if (menu->cascades[i]) {
586 if (menu->cascades[i]->flags.brother)
587 wMenuDestroy(menu->cascades[i]->brother, recurse);
588 else
589 wMenuDestroy(menu->cascades[i], recurse);
594 if (menu->entries)
595 wfree(menu->entries);
599 FREE_PIXMAP(menu->menu_texture_data);
601 if (menu->cascades)
602 wfree(menu->cascades);
604 wCoreDestroy(menu->menu);
605 wFrameWindowDestroy(menu->frame);
607 /* destroy copy of this menu */
608 if (!menu->flags.brother && menu->brother)
609 wMenuDestroy(menu->brother, False);
611 wfree(menu);
614 #define F_NORMAL 0
615 #define F_TOP 1
616 #define F_BOTTOM 2
617 #define F_NONE 3
619 static void drawFrame(WScreen * scr, Drawable win, int y, int w, int h, int type)
621 XSegment segs[2];
622 int i;
624 i = 0;
625 segs[i].x1 = segs[i].x2 = w - 1;
626 segs[i].y1 = y;
627 segs[i].y2 = y + h - 1;
628 i++;
629 if (type != F_TOP && type != F_NONE) {
630 segs[i].x1 = 1;
631 segs[i].y1 = segs[i].y2 = y + h - 2;
632 segs[i].x2 = w - 1;
633 i++;
635 XDrawSegments(dpy, win, scr->menu_item_auxtexture->dim_gc, segs, i);
637 i = 0;
638 segs[i].x1 = 0;
639 segs[i].y1 = y;
640 segs[i].x2 = 0;
641 segs[i].y2 = y + h - 1;
642 i++;
643 if (type != F_BOTTOM && type != F_NONE) {
644 segs[i].x1 = 0;
645 segs[i].y1 = y;
646 segs[i].x2 = w - 1;
647 segs[i].y2 = y;
648 i++;
650 XDrawSegments(dpy, win, scr->menu_item_auxtexture->light_gc, segs, i);
652 if (type != F_TOP && type != F_NONE)
653 XDrawLine(dpy, win, scr->menu_item_auxtexture->dark_gc, 0, y + h - 1, w - 1, y + h - 1);
656 static void paintEntry(WMenu * menu, int index, int selected)
658 WScreen *scr = menu->frame->screen_ptr;
659 Window win = menu->menu->window;
660 WMenuEntry *entry = menu->entries[index];
661 GC light, dim, dark;
662 WMColor *color;
663 int x, y, w, h, tw;
664 int type;
666 if (!menu->flags.realized)
667 return;
668 h = menu->entry_height;
669 w = menu->menu->width;
670 y = index * h;
672 light = scr->menu_item_auxtexture->light_gc;
673 dim = scr->menu_item_auxtexture->dim_gc;
674 dark = scr->menu_item_auxtexture->dark_gc;
676 if (wPreferences.menu_style == MS_FLAT && menu->entry_no > 1) {
677 if (index == 0)
678 type = F_TOP;
679 else if (index == menu->entry_no - 1)
680 type = F_BOTTOM;
681 else
682 type = F_NONE;
683 } else {
684 type = F_NORMAL;
687 /* paint background */
688 if (selected) {
689 XFillRectangle(dpy, win, WMColorGC(scr->select_color), 1, y + 1, w - 2, h - 3);
690 if (scr->menu_item_texture->any.type == WTEX_SOLID)
691 drawFrame(scr, win, y, w, h, type);
692 } else {
693 if (scr->menu_item_texture->any.type == WTEX_SOLID) {
694 XClearArea(dpy, win, 0, y + 1, w - 1, h - 3, False);
695 /* draw the frame */
696 drawFrame(scr, win, y, w, h, type);
697 } else {
698 XClearArea(dpy, win, 0, y, w, h, False);
702 if (selected) {
703 if (entry->flags.enabled)
704 color = scr->select_text_color;
705 else
706 color = scr->dtext_color;
707 } else if (!entry->flags.enabled) {
708 color = scr->dtext_color;
709 } else {
710 color = scr->mtext_color;
712 /* draw text */
713 x = 5;
714 if (entry->flags.indicator)
715 x += MENU_INDICATOR_SPACE + 2;
717 WMDrawString(scr->wmscreen, win, color, scr->menu_entry_font,
718 x, 3 + y + wPreferences.menu_text_clearance, entry->text, strlen(entry->text));
720 if (entry->cascade >= 0) {
721 /* draw the cascade indicator */
722 XDrawLine(dpy, win, dim, w - 11, y + 6, w - 6, y + h / 2 - 1);
723 XDrawLine(dpy, win, light, w - 11, y + h - 8, w - 6, y + h / 2 - 1);
724 XDrawLine(dpy, win, dark, w - 12, y + 6, w - 12, y + h - 8);
727 /* draw indicator */
728 if (entry->flags.indicator && entry->flags.indicator_on) {
729 int iw, ih;
730 WPixmap *indicator;
732 switch (entry->flags.indicator_type) {
733 case MI_CHECK:
734 indicator = scr->menu_check_indicator;
735 break;
736 case MI_MINIWINDOW:
737 indicator = scr->menu_mini_indicator;
738 break;
739 case MI_HIDDEN:
740 indicator = scr->menu_hide_indicator;
741 break;
742 case MI_SHADED:
743 indicator = scr->menu_shade_indicator;
744 break;
745 case MI_DIAMOND:
746 default:
747 indicator = scr->menu_radio_indicator;
748 break;
751 iw = indicator->width;
752 ih = indicator->height;
753 XSetClipMask(dpy, scr->copy_gc, indicator->mask);
754 XSetClipOrigin(dpy, scr->copy_gc, 5, y + (h - ih) / 2);
755 if (selected) {
756 if (entry->flags.enabled) {
757 XSetForeground(dpy, scr->copy_gc, WMColorPixel(scr->select_text_color));
758 } else {
759 XSetForeground(dpy, scr->copy_gc, WMColorPixel(scr->dtext_color));
761 } else {
762 if (entry->flags.enabled) {
763 XSetForeground(dpy, scr->copy_gc, WMColorPixel(scr->mtext_color));
764 } else {
765 XSetForeground(dpy, scr->copy_gc, WMColorPixel(scr->dtext_color));
768 XFillRectangle(dpy, win, scr->copy_gc, 5, y + (h - ih) / 2, iw, ih);
770 XCopyArea(dpy, indicator->image, win, scr->copy_gc, 0, 0,
771 iw, ih, 5, y+(h-ih)/2);
773 XSetClipOrigin(dpy, scr->copy_gc, 0, 0);
776 /* draw right text */
778 if (entry->rtext && entry->cascade < 0) {
779 tw = WMWidthOfString(scr->menu_entry_font, entry->rtext, strlen(entry->rtext));
781 WMDrawString(scr->wmscreen, win, color, scr->menu_entry_font, w - 6 - tw,
782 y + 3 + wPreferences.menu_text_clearance, entry->rtext, strlen(entry->rtext));
786 static void move_menus(WMenu * menu, int x, int y)
788 while (menu->parent) {
789 menu = menu->parent;
790 x -= MENUW(menu);
791 if (!wPreferences.align_menus && menu->selected_entry >= 0) {
792 y -= menu->selected_entry * menu->entry_height;
795 wMenuMove(menu, x, y, True);
798 static void makeVisible(WMenu * menu)
800 WScreen *scr = menu->frame->screen_ptr;
801 int x1, y1, x2, y2, new_x, new_y;
802 WMRect rect = wGetRectForHead(scr, wGetHeadForPointerLocation(scr));
804 if (menu->entry_no < 0)
805 return;
807 x1 = menu->frame_x;
808 y1 = menu->frame_y + menu->frame->top_width + menu->selected_entry * menu->entry_height;
809 x2 = x1 + MENUW(menu);
810 y2 = y1 + menu->entry_height;
812 new_x = x1;
813 new_y = y1;
815 if (x1 < rect.pos.x) {
816 new_x = rect.pos.x;
817 } else if (x2 >= rect.pos.x + rect.size.width) {
818 new_x = rect.pos.x + rect.size.width - MENUW(menu) - 1;
821 if (y1 < rect.pos.y) {
822 new_y = rect.pos.y;
823 } else if (y2 >= rect.pos.y + rect.size.height) {
824 new_y = rect.pos.y + rect.size.height - menu->entry_height - 1;
827 new_y = new_y - menu->frame->top_width - menu->selected_entry * menu->entry_height;
828 move_menus(menu, new_x, new_y);
831 static int check_key(WMenu * menu, XKeyEvent * event)
833 int i, ch, s;
834 char buffer[32];
836 if (XLookupString(event, buffer, 32, NULL, NULL) < 1)
837 return -1;
839 ch = toupper(buffer[0]);
841 s = (menu->selected_entry >= 0 ? menu->selected_entry + 1 : 0);
843 again:
844 for (i = s; i < menu->entry_no; i++) {
845 if (ch == toupper(menu->entries[i]->text[0])) {
846 return i;
849 /* no match. Retry from start, if previous started from a selected entry */
850 if (s != 0) {
851 s = 0;
852 goto again;
854 return -1;
857 static int keyboardMenu(WMenu * menu)
859 XEvent event;
860 KeySym ksym = NoSymbol;
861 int done = 0;
862 int index;
863 WMenuEntry *entry;
864 int old_pos_x = menu->frame_x;
865 int old_pos_y = menu->frame_y;
866 int new_x = old_pos_x, new_y = old_pos_y;
867 WMRect rect = wGetRectForHead(menu->frame->screen_ptr,
868 wGetHeadForPointerLocation(menu->frame->screen_ptr));
870 if (menu->flags.editing)
871 return False;
873 XGrabKeyboard(dpy, menu->frame->core->window, True, GrabModeAsync, GrabModeAsync, CurrentTime);
875 if (menu->frame_y + menu->frame->top_width >= rect.pos.y + rect.size.height)
876 new_y = rect.pos.y + rect.size.height - menu->frame->top_width;
878 if (menu->frame_x + MENUW(menu) >= rect.pos.x + rect.size.width)
879 new_x = rect.pos.x + rect.size.width - MENUW(menu) - 1;
881 move_menus(menu, new_x, new_y);
883 while (!done && menu->flags.mapped) {
884 XAllowEvents(dpy, AsyncKeyboard, CurrentTime);
885 WMMaskEvent(dpy, ExposureMask | ButtonMotionMask | ButtonPressMask
886 | ButtonReleaseMask | KeyPressMask | KeyReleaseMask | SubstructureNotifyMask, &event);
888 switch (event.type) {
889 case KeyPress:
890 ksym = XLookupKeysym(&event.xkey, 0);
891 if (wPreferences.vi_key_menus) {
892 switch (ksym) {
893 case XK_h:
894 ksym = XK_Left;
895 break;
897 case XK_j:
898 ksym = XK_Down;
899 break;
901 case XK_k:
902 ksym = XK_Up;
903 break;
905 case XK_l:
906 ksym = XK_Right;
907 break;
911 switch (ksym) {
912 case XK_Escape:
913 done = 1;
914 break;
916 case XK_Home:
917 #ifdef XK_KP_Home
918 case XK_KP_Home:
919 #endif
920 selectEntry(menu, 0);
921 makeVisible(menu);
922 break;
924 case XK_End:
925 #ifdef XK_KP_End
926 case XK_KP_End:
927 #endif
928 selectEntry(menu, menu->entry_no - 1);
929 makeVisible(menu);
930 break;
932 case XK_Up:
933 #ifdef XK_KP_Up
934 case XK_KP_Up:
935 #endif
936 if (menu->selected_entry <= 0)
937 selectEntry(menu, menu->entry_no - 1);
938 else
939 selectEntry(menu, menu->selected_entry - 1);
940 makeVisible(menu);
941 break;
943 case XK_Down:
944 #ifdef XK_KP_Down
945 case XK_KP_Down:
946 #endif
947 if (menu->selected_entry < 0)
948 selectEntry(menu, 0);
949 else if (menu->selected_entry == menu->entry_no - 1)
950 selectEntry(menu, 0);
951 else if (menu->selected_entry < menu->entry_no - 1)
952 selectEntry(menu, menu->selected_entry + 1);
953 makeVisible(menu);
954 break;
956 case XK_Right:
957 #ifdef XK_KP_Right
958 case XK_KP_Right:
959 #endif
960 if (menu->selected_entry >= 0) {
961 WMenuEntry *entry;
962 entry = menu->entries[menu->selected_entry];
964 if (entry->cascade >= 0 && menu->cascades
965 && menu->cascades[entry->cascade]->entry_no > 0) {
967 XUngrabKeyboard(dpy, CurrentTime);
969 selectEntry(menu->cascades[entry->cascade], 0);
970 if (!keyboardMenu(menu->cascades[entry->cascade]))
971 done = 1;
973 XGrabKeyboard(dpy, menu->frame->core->window, True,
974 GrabModeAsync, GrabModeAsync, CurrentTime);
977 break;
979 case XK_Left:
980 #ifdef XK_KP_Left
981 case XK_KP_Left:
982 #endif
983 if (menu->parent != NULL && menu->parent->selected_entry >= 0) {
984 selectEntry(menu, -1);
985 move_menus(menu, old_pos_x, old_pos_y);
986 return True;
988 break;
990 case XK_Return:
991 #ifdef XK_KP_Enter
992 case XK_KP_Enter:
993 #endif
994 done = 2;
995 break;
997 default:
998 index = check_key(menu, &event.xkey);
999 if (index >= 0) {
1000 selectEntry(menu, index);
1003 break;
1005 default:
1006 if (event.type == ButtonPress)
1007 done = 1;
1009 WMHandleEvent(&event);
1013 XUngrabKeyboard(dpy, CurrentTime);
1015 if (done == 2 && menu->selected_entry >= 0) {
1016 entry = menu->entries[menu->selected_entry];
1017 } else {
1018 entry = NULL;
1021 if (entry && entry->callback != NULL && entry->flags.enabled && entry->cascade < 0) {
1022 #if (MENU_BLINK_COUNT > 0)
1023 int sel = menu->selected_entry;
1024 int i;
1026 for (i = 0; i < MENU_BLINK_COUNT; i++) {
1027 paintEntry(menu, sel, False);
1028 XSync(dpy, 0);
1029 wusleep(MENU_BLINK_DELAY);
1030 paintEntry(menu, sel, True);
1031 XSync(dpy, 0);
1032 wusleep(MENU_BLINK_DELAY);
1034 #endif
1035 selectEntry(menu, -1);
1037 if (!menu->flags.buttoned) {
1038 wMenuUnmap(menu);
1039 move_menus(menu, old_pos_x, old_pos_y);
1041 closeCascade(menu);
1043 (*entry->callback) (menu, entry);
1044 } else {
1045 if (!menu->flags.buttoned) {
1046 wMenuUnmap(menu);
1047 move_menus(menu, old_pos_x, old_pos_y);
1049 selectEntry(menu, -1);
1052 /* returns True if returning from a submenu to a parent menu,
1053 * False if exiting from menu */
1054 return False;
1057 void wMenuMapAt(WMenu * menu, int x, int y, int keyboard)
1059 if (!menu->flags.realized) {
1060 menu->flags.realized = 1;
1061 wMenuRealize(menu);
1064 if (!menu->flags.mapped) {
1065 if (wPreferences.wrap_menus) {
1066 WScreen *scr = menu->frame->screen_ptr;
1067 WMRect rect = wGetRectForHead(scr, wGetHeadForPointerLocation(scr));
1069 if (x < rect.pos.x)
1070 x = rect.pos.x;
1071 if (y < rect.pos.y)
1072 y = rect.pos.y;
1073 if (x + MENUW(menu) > rect.pos.x + rect.size.width)
1074 x = rect.pos.x + rect.size.width - MENUW(menu);
1075 if (y + MENUH(menu) > rect.pos.y + rect.size.height)
1076 y = rect.pos.y + rect.size.height - MENUH(menu);
1079 XMoveWindow(dpy, menu->frame->core->window, x, y);
1080 menu->frame_x = x;
1081 menu->frame_y = y;
1082 XMapWindow(dpy, menu->frame->core->window);
1083 wRaiseFrame(menu->frame->core);
1084 menu->flags.mapped = 1;
1085 } else {
1086 selectEntry(menu, 0);
1089 if (keyboard)
1090 keyboardMenu(menu);
1093 void wMenuMap(WMenu * menu)
1095 if (!menu->flags.realized) {
1096 menu->flags.realized = 1;
1097 wMenuRealize(menu);
1099 if (menu->flags.app_menu && menu->parent == NULL) {
1100 menu->frame_x = menu->frame->screen_ptr->app_menu_x;
1101 menu->frame_y = menu->frame->screen_ptr->app_menu_y;
1102 XMoveWindow(dpy, menu->frame->core->window, menu->frame_x, menu->frame_y);
1104 XMapWindow(dpy, menu->frame->core->window);
1105 wRaiseFrame(menu->frame->core);
1106 menu->flags.mapped = 1;
1109 void wMenuUnmap(WMenu * menu)
1111 int i;
1113 XUnmapWindow(dpy, menu->frame->core->window);
1114 if (menu->flags.titled && menu->flags.buttoned) {
1115 wFrameWindowHideButton(menu->frame, WFF_RIGHT_BUTTON);
1117 menu->flags.buttoned = 0;
1118 menu->flags.mapped = 0;
1119 menu->flags.open_to_left = 0;
1121 for (i = 0; i < menu->cascade_no; i++) {
1122 if (menu->cascades[i] != NULL
1123 && menu->cascades[i]->flags.mapped && !menu->cascades[i]->flags.buttoned) {
1125 wMenuUnmap(menu->cascades[i]);
1128 menu->selected_entry = -1;
1131 void wMenuPaint(WMenu * menu)
1133 int i;
1135 if (!menu->flags.mapped) {
1136 return;
1139 /* paint entries */
1140 for (i = 0; i < menu->entry_no; i++) {
1141 paintEntry(menu, i, i == menu->selected_entry);
1145 void wMenuSetEnabled(WMenu * menu, int index, int enable)
1147 if (index >= menu->entry_no)
1148 return;
1149 menu->entries[index]->flags.enabled = enable;
1150 paintEntry(menu, index, index == menu->selected_entry);
1151 paintEntry(menu->brother, index, index == menu->selected_entry);
1155 static void selectEntry(WMenu * menu, int entry_no)
1157 WMenuEntry *entry;
1158 WMenu *submenu;
1159 int old_entry;
1161 if (menu->entries == NULL)
1162 return;
1164 if (entry_no >= menu->entry_no)
1165 return;
1167 old_entry = menu->selected_entry;
1168 menu->selected_entry = entry_no;
1170 if (old_entry != entry_no) {
1172 /* unselect previous entry */
1173 if (old_entry >= 0) {
1174 paintEntry(menu, old_entry, False);
1175 entry = menu->entries[old_entry];
1177 /* unmap cascade */
1178 if (entry->cascade >= 0 && menu->cascades) {
1179 if (!menu->cascades[entry->cascade]->flags.buttoned) {
1180 wMenuUnmap(menu->cascades[entry->cascade]);
1185 if (entry_no < 0) {
1186 menu->selected_entry = -1;
1187 return;
1189 entry = menu->entries[entry_no];
1191 if (entry->cascade >= 0 && menu->cascades && entry->flags.enabled) {
1192 /* Callback for when the submenu is opened.
1194 submenu = menu->cascades[entry->cascade];
1195 if (submenu && submenu->flags.brother)
1196 submenu = submenu->brother;
1198 if (entry->callback) {
1199 /* Only call the callback if the submenu is not yet mapped.
1201 if (menu->flags.brother) {
1202 if (!submenu || !submenu->flags.mapped)
1203 (*entry->callback) (menu->brother, entry);
1204 } else {
1205 if (!submenu || !submenu->flags.buttoned)
1206 (*entry->callback) (menu, entry);
1210 /* the submenu menu might have changed */
1211 submenu = menu->cascades[entry->cascade];
1213 /* map cascade */
1214 if (!submenu->flags.mapped) {
1215 int x, y;
1217 if (!submenu->flags.realized)
1218 wMenuRealize(submenu);
1219 if (wPreferences.wrap_menus) {
1220 if (menu->flags.open_to_left)
1221 submenu->flags.open_to_left = 1;
1223 if (submenu->flags.open_to_left) {
1224 x = menu->frame_x - MENUW(submenu);
1225 if (x < 0) {
1226 x = 0;
1227 submenu->flags.open_to_left = 0;
1229 } else {
1230 x = menu->frame_x + MENUW(menu);
1232 if (x + MENUW(submenu)
1233 >= menu->frame->screen_ptr->scr_width) {
1235 x = menu->frame_x - MENUW(submenu);
1236 submenu->flags.open_to_left = 1;
1239 } else {
1240 x = menu->frame_x + MENUW(menu);
1243 if (wPreferences.align_menus) {
1244 y = menu->frame_y;
1245 } else {
1246 y = menu->frame_y + menu->entry_height * entry_no;
1247 if (menu->flags.titled)
1248 y += menu->frame->top_width;
1249 if (menu->cascades[entry->cascade]->flags.titled)
1250 y -= menu->cascades[entry->cascade]->frame->top_width;
1253 wMenuMapAt(menu->cascades[entry->cascade], x, y, False);
1254 menu->cascades[entry->cascade]->parent = menu;
1255 } else {
1256 return;
1259 paintEntry(menu, entry_no, True);
1263 static WMenu *findMenu(WScreen * scr, int *x_ret, int *y_ret)
1265 WMenu *menu;
1266 WObjDescriptor *desc;
1267 Window root_ret, win, junk_win;
1268 int x, y, wx, wy;
1269 unsigned int mask;
1271 XQueryPointer(dpy, scr->root_win, &root_ret, &win, &x, &y, &wx, &wy, &mask);
1273 if (win == None)
1274 return NULL;
1276 if (XFindContext(dpy, win, w_global.context.client_win, (XPointer *) & desc) == XCNOENT)
1277 return NULL;
1279 if (desc->parent_type == WCLASS_MENU) {
1280 menu = (WMenu *) desc->parent;
1281 XTranslateCoordinates(dpy, root_ret, menu->menu->window, wx, wy, x_ret, y_ret, &junk_win);
1282 return menu;
1284 return NULL;
1287 static void closeCascade(WMenu * menu)
1289 WMenu *parent = menu->parent;
1291 if (menu->flags.brother || (!menu->flags.buttoned && (!menu->flags.app_menu || menu->parent != NULL))) {
1293 selectEntry(menu, -1);
1294 XSync(dpy, 0);
1295 #if (MENU_BLINK_DELAY > 2)
1296 wusleep(MENU_BLINK_DELAY / 2);
1297 #endif
1298 wMenuUnmap(menu);
1299 while (parent != NULL
1300 && (parent->parent != NULL || !parent->flags.app_menu || parent->flags.brother)
1301 && !parent->flags.buttoned) {
1302 selectEntry(parent, -1);
1303 wMenuUnmap(parent);
1304 parent = parent->parent;
1306 if (parent)
1307 selectEntry(parent, -1);
1311 static void closeBrotherCascadesOf(WMenu * menu)
1313 WMenu *tmp;
1314 int i;
1316 for (i = 0; i < menu->cascade_no; i++) {
1317 if (menu->cascades[i]->flags.brother) {
1318 tmp = menu->cascades[i];
1319 } else {
1320 tmp = menu->cascades[i]->brother;
1322 if (tmp->flags.mapped) {
1323 selectEntry(tmp->parent, -1);
1324 closeBrotherCascadesOf(tmp);
1325 break;
1330 #define getEntryAt(menu, x, y) ((y)<0 ? -1 : (y)/(menu->entry_height))
1332 static WMenu *parentMenu(WMenu * menu)
1334 WMenu *parent;
1335 WMenuEntry *entry;
1337 if (menu->flags.buttoned)
1338 return menu;
1340 while (menu->parent && menu->parent->flags.mapped) {
1341 parent = menu->parent;
1342 if (parent->selected_entry < 0)
1343 break;
1344 entry = parent->entries[parent->selected_entry];
1345 if (!entry->flags.enabled || entry->cascade < 0 || !parent->cascades ||
1346 parent->cascades[entry->cascade] != menu)
1347 break;
1348 menu = parent;
1349 if (menu->flags.buttoned)
1350 break;
1353 return menu;
1357 * Will raise the passed menu, if submenu = 0
1358 * If submenu > 0 will also raise all mapped submenus
1359 * until the first buttoned one
1360 * If submenu < 0 will also raise all mapped parent menus
1361 * until the first buttoned one
1364 static void raiseMenus(WMenu * menu, int submenus)
1366 WMenu *submenu;
1367 int i;
1369 if (!menu)
1370 return;
1372 wRaiseFrame(menu->frame->core);
1374 if (submenus > 0 && menu->selected_entry >= 0) {
1375 i = menu->entries[menu->selected_entry]->cascade;
1376 if (i >= 0 && menu->cascades) {
1377 submenu = menu->cascades[i];
1378 if (submenu->flags.mapped && !submenu->flags.buttoned)
1379 raiseMenus(submenu, submenus);
1382 if (submenus < 0 && !menu->flags.buttoned && menu->parent && menu->parent->flags.mapped)
1383 raiseMenus(menu->parent, submenus);
1386 WMenu *wMenuUnderPointer(WScreen * screen)
1388 WObjDescriptor *desc;
1389 Window root_ret, win;
1390 int dummy;
1391 unsigned int mask;
1393 XQueryPointer(dpy, screen->root_win, &root_ret, &win, &dummy, &dummy, &dummy, &dummy, &mask);
1395 if (win == None)
1396 return NULL;
1398 if (XFindContext(dpy, win, w_global.context.client_win, (XPointer *) & desc) == XCNOENT)
1399 return NULL;
1401 if (desc->parent_type == WCLASS_MENU)
1402 return (WMenu *) desc->parent;
1403 return NULL;
1406 static void getPointerPosition(WScreen * scr, int *x, int *y)
1408 Window root_ret, win;
1409 int wx, wy;
1410 unsigned int mask;
1412 XQueryPointer(dpy, scr->root_win, &root_ret, &win, x, y, &wx, &wy, &mask);
1415 static void getScrollAmount(WMenu * menu, int *hamount, int *vamount)
1417 WScreen *scr = menu->menu->screen_ptr;
1418 int menuX1 = menu->frame_x;
1419 int menuY1 = menu->frame_y;
1420 int menuX2 = menu->frame_x + MENUW(menu);
1421 int menuY2 = menu->frame_y + MENUH(menu);
1422 int xroot, yroot;
1423 WMRect rect = wGetRectForHead(scr, wGetHeadForPointerLocation(scr));
1425 *hamount = 0;
1426 *vamount = 0;
1428 getPointerPosition(scr, &xroot, &yroot);
1430 if (xroot <= (rect.pos.x + 1) && menuX1 < rect.pos.x) {
1431 /* scroll to the right */
1432 *hamount = WMIN(MENU_SCROLL_STEP, abs(menuX1));
1434 } else if (xroot >= (rect.pos.x + rect.size.width - 2) && menuX2 > (rect.pos.x + rect.size.width - 1)) {
1435 /* scroll to the left */
1436 *hamount = WMIN(MENU_SCROLL_STEP, abs(menuX2 - rect.pos.x - rect.size.width - 1));
1438 if (*hamount == 0)
1439 *hamount = 1;
1441 *hamount = -*hamount;
1444 if (yroot <= (rect.pos.y + 1) && menuY1 < rect.pos.y) {
1445 /* scroll down */
1446 *vamount = WMIN(MENU_SCROLL_STEP, abs(menuY1));
1448 } else if (yroot >= (rect.pos.y + rect.size.height - 2) && menuY2 > (rect.pos.y + rect.size.height - 1)) {
1449 /* scroll up */
1450 *vamount = WMIN(MENU_SCROLL_STEP, abs(menuY2 - rect.pos.y - rect.size.height - 2));
1452 *vamount = -*vamount;
1456 static void dragScrollMenuCallback(void *data)
1458 WMenu *menu = (WMenu *) data;
1459 WScreen *scr = menu->menu->screen_ptr;
1460 WMenu *parent = parentMenu(menu);
1461 int hamount, vamount;
1462 int x, y;
1463 int newSelectedEntry;
1465 getScrollAmount(menu, &hamount, &vamount);
1467 if (hamount != 0 || vamount != 0) {
1468 wMenuMove(parent, parent->frame_x + hamount, parent->frame_y + vamount, True);
1469 if (findMenu(scr, &x, &y)) {
1470 newSelectedEntry = getEntryAt(menu, x, y);
1471 selectEntry(menu, newSelectedEntry);
1472 } else {
1473 /* Pointer fell outside of menu. If the selected entry is
1474 * not a submenu, unselect it */
1475 if (menu->selected_entry >= 0 && menu->entries[menu->selected_entry]->cascade < 0)
1476 selectEntry(menu, -1);
1477 newSelectedEntry = 0;
1480 /* paranoid check */
1481 if (newSelectedEntry >= 0) {
1482 /* keep scrolling */
1483 menu->timer = WMAddTimerHandler(MENU_SCROLL_DELAY, dragScrollMenuCallback, menu);
1484 } else {
1485 menu->timer = NULL;
1487 } else {
1488 /* don't need to scroll anymore */
1489 menu->timer = NULL;
1490 if (findMenu(scr, &x, &y)) {
1491 newSelectedEntry = getEntryAt(menu, x, y);
1492 selectEntry(menu, newSelectedEntry);
1497 static void scrollMenuCallback(void *data)
1499 WMenu *menu = (WMenu *) data;
1500 WMenu *parent = parentMenu(menu);
1501 int hamount = 0; /* amount to scroll */
1502 int vamount = 0;
1504 getScrollAmount(menu, &hamount, &vamount);
1506 if (hamount != 0 || vamount != 0) {
1507 wMenuMove(parent, parent->frame_x + hamount, parent->frame_y + vamount, True);
1509 /* keep scrolling */
1510 menu->timer = WMAddTimerHandler(MENU_SCROLL_DELAY, scrollMenuCallback, menu);
1511 } else {
1512 /* don't need to scroll anymore */
1513 menu->timer = NULL;
1517 #define MENU_SCROLL_BORDER 5
1519 static int isPointNearBoder(WMenu * menu, int x, int y)
1521 int menuX1 = menu->frame_x;
1522 int menuY1 = menu->frame_y;
1523 int menuX2 = menu->frame_x + MENUW(menu);
1524 int menuY2 = menu->frame_y + MENUH(menu);
1525 int flag = 0;
1526 int head = wGetHeadForPoint(menu->frame->screen_ptr, wmkpoint(x, y));
1527 WMRect rect = wGetRectForHead(menu->frame->screen_ptr, head);
1529 /* XXX: handle screen joins properly !! */
1531 if (x >= menuX1 && x <= menuX2 &&
1532 (y < rect.pos.y + MENU_SCROLL_BORDER || y >= rect.pos.y + rect.size.height - MENU_SCROLL_BORDER))
1533 flag = 1;
1534 else if (y >= menuY1 && y <= menuY2 &&
1535 (x < rect.pos.x + MENU_SCROLL_BORDER || x >= rect.pos.x + rect.size.width - MENU_SCROLL_BORDER))
1536 flag = 1;
1538 return flag;
1541 typedef struct _delay {
1542 WMenu *menu;
1543 int ox, oy;
1544 } _delay;
1546 static void leaving(_delay * dl)
1548 wMenuMove(dl->menu, dl->ox, dl->oy, True);
1549 dl->menu->jump_back = NULL;
1550 dl->menu->menu->screen_ptr->flags.jump_back_pending = 0;
1551 wfree(dl);
1554 void wMenuScroll(WMenu * menu, XEvent * event)
1556 WMenu *smenu;
1557 WMenu *omenu = parentMenu(menu);
1558 WScreen *scr = menu->frame->screen_ptr;
1559 int done = 0;
1560 int jump_back = 0;
1561 int old_frame_x = omenu->frame_x;
1562 int old_frame_y = omenu->frame_y;
1563 XEvent ev;
1565 if (omenu->jump_back)
1566 WMDeleteTimerWithClientData(omenu->jump_back);
1568 if (( /*omenu->flags.buttoned && */ !wPreferences.wrap_menus)
1569 || omenu->flags.app_menu) {
1570 jump_back = 1;
1573 if (!wPreferences.wrap_menus)
1574 raiseMenus(omenu, True);
1575 else
1576 raiseMenus(menu, False);
1578 if (!menu->timer)
1579 scrollMenuCallback(menu);
1581 while (!done) {
1582 int x, y, on_border, on_x_edge, on_y_edge, on_title;
1583 WMRect rect;
1585 WMNextEvent(dpy, &ev);
1586 switch (ev.type) {
1587 case EnterNotify:
1588 WMHandleEvent(&ev);
1589 case MotionNotify:
1590 x = (ev.type == MotionNotify) ? ev.xmotion.x_root : ev.xcrossing.x_root;
1591 y = (ev.type == MotionNotify) ? ev.xmotion.y_root : ev.xcrossing.y_root;
1593 /* on_border is != 0 if the pointer is between the menu
1594 * and the screen border and is close enough to the border */
1595 on_border = isPointNearBoder(menu, x, y);
1597 smenu = wMenuUnderPointer(scr);
1599 if ((smenu == NULL && !on_border) || (smenu && parentMenu(smenu) != omenu)) {
1600 done = 1;
1601 break;
1604 rect = wGetRectForHead(scr, wGetHeadForPoint(scr, wmkpoint(x, y)));
1605 on_x_edge = x <= rect.pos.x + 1 || x >= rect.pos.x + rect.size.width - 2;
1606 on_y_edge = y <= rect.pos.y + 1 || y >= rect.pos.y + rect.size.height - 2;
1607 on_border = on_x_edge || on_y_edge;
1609 if (!on_border && !jump_back) {
1610 done = 1;
1611 break;
1614 if (menu->timer && (smenu != menu || (!on_y_edge && !on_x_edge))) {
1615 WMDeleteTimerHandler(menu->timer);
1616 menu->timer = NULL;
1619 if (smenu != NULL)
1620 menu = smenu;
1622 if (!menu->timer)
1623 scrollMenuCallback(menu);
1624 break;
1625 case ButtonPress:
1626 /* True if we push on title, or drag the omenu to other position */
1627 on_title = ev.xbutton.x_root >= omenu->frame_x &&
1628 ev.xbutton.x_root <= omenu->frame_x + MENUW(omenu) &&
1629 ev.xbutton.y_root >= omenu->frame_y &&
1630 ev.xbutton.y_root <= omenu->frame_y + omenu->frame->top_width;
1631 WMHandleEvent(&ev);
1632 smenu = wMenuUnderPointer(scr);
1633 if (smenu == NULL || (smenu && smenu->flags.buttoned && smenu != omenu))
1634 done = 1;
1635 else if (smenu == omenu && on_title) {
1636 jump_back = 0;
1637 done = 1;
1639 break;
1640 case KeyPress:
1641 done = 1;
1642 default:
1643 WMHandleEvent(&ev);
1644 break;
1648 if (menu->timer) {
1649 WMDeleteTimerHandler(menu->timer);
1650 menu->timer = NULL;
1653 if (jump_back) {
1654 _delay *delayer;
1655 if (!omenu->jump_back) {
1656 delayer = wmalloc(sizeof(_delay));
1657 delayer->menu = omenu;
1658 delayer->ox = old_frame_x;
1659 delayer->oy = old_frame_y;
1660 omenu->jump_back = delayer;
1661 scr->flags.jump_back_pending = 1;
1662 } else
1663 delayer = omenu->jump_back;
1664 WMAddTimerHandler(MENU_JUMP_BACK_DELAY, (WMCallback *) leaving, delayer);
1668 static void menuExpose(WObjDescriptor * desc, XEvent * event)
1670 /* Parameter not used, but tell the compiler that it is ok */
1671 (void) event;
1673 wMenuPaint(desc->parent);
1676 typedef struct {
1677 int *delayed_select;
1678 WMenu *menu;
1679 WMHandlerID magic;
1680 } delay_data;
1682 static void delaySelection(void *data)
1684 delay_data *d = (delay_data *) data;
1685 int x, y, entry_no;
1686 WMenu *menu;
1688 d->magic = NULL;
1690 menu = findMenu(d->menu->menu->screen_ptr, &x, &y);
1691 if (menu && (d->menu == menu || d->delayed_select)) {
1692 entry_no = getEntryAt(menu, x, y);
1693 selectEntry(menu, entry_no);
1695 if (d->delayed_select)
1696 *(d->delayed_select) = 0;
1699 static void menuMouseDown(WObjDescriptor * desc, XEvent * event)
1701 WWindow *wwin;
1702 XButtonEvent *bev = &event->xbutton;
1703 WMenu *menu = desc->parent;
1704 WMenu *smenu;
1705 WScreen *scr = menu->frame->screen_ptr;
1706 WMenuEntry *entry = NULL;
1707 XEvent ev;
1708 int close_on_exit = 0;
1709 int done = 0;
1710 int delayed_select = 0;
1711 int entry_no;
1712 int x, y;
1713 int prevx, prevy;
1714 int old_frame_x = 0;
1715 int old_frame_y = 0;
1716 delay_data d_data = { NULL, NULL, NULL };
1718 /* Doesn't seem to be needed anymore (if delayed selection handler is
1719 * added only if not present). there seem to be no other side effects
1720 * from removing this and it is also possible that it was only added
1721 * to avoid problems with adding the delayed selection timer handler
1722 * multiple times
1724 /*if (menu->flags.inside_handler) {
1725 return;
1726 } */
1727 menu->flags.inside_handler = 1;
1729 if (!wPreferences.wrap_menus) {
1730 smenu = parentMenu(menu);
1731 old_frame_x = smenu->frame_x;
1732 old_frame_y = smenu->frame_y;
1733 } else if (event->xbutton.window == menu->frame->core->window) {
1734 /* This is true if the menu was launched with right click on root window */
1735 if (!d_data.magic) {
1736 delayed_select = 1;
1737 d_data.delayed_select = &delayed_select;
1738 d_data.menu = menu;
1739 d_data.magic = WMAddTimerHandler(wPreferences.dblclick_time, delaySelection, &d_data);
1743 wRaiseFrame(menu->frame->core);
1745 close_on_exit = (bev->send_event || menu->flags.brother);
1747 smenu = findMenu(scr, &x, &y);
1748 if (!smenu) {
1749 x = -1;
1750 y = -1;
1751 } else {
1752 menu = smenu;
1755 if (menu->flags.editing) {
1756 goto byebye;
1758 entry_no = getEntryAt(menu, x, y);
1759 if (entry_no >= 0) {
1760 entry = menu->entries[entry_no];
1762 if (!close_on_exit && (bev->state & ControlMask) && smenu && entry->flags.editable) {
1763 char buffer[128];
1764 char *name;
1765 int number = entry_no - 3; /* Entries "New", "Destroy Last" and "Last Used" appear before workspaces */
1767 name = wstrdup(w_global.workspace.array[number]->name);
1768 snprintf(buffer, sizeof(buffer), _("Type the name for workspace %i:"), number + 1);
1770 wMenuUnmap(scr->root_menu);
1772 if (wInputDialog(scr, _("Rename Workspace"), buffer, &name))
1773 wWorkspaceRename(scr, number, name);
1775 if (name)
1776 wfree(name);
1778 goto byebye;
1779 } else if (bev->state & ControlMask) {
1780 goto byebye;
1783 if (entry->flags.enabled && entry->cascade >= 0 && menu->cascades) {
1784 WMenu *submenu = menu->cascades[entry->cascade];
1785 /* map cascade */
1786 if (submenu->flags.mapped && !submenu->flags.buttoned && menu->selected_entry != entry_no) {
1787 wMenuUnmap(submenu);
1789 if (!submenu->flags.mapped && !delayed_select) {
1790 selectEntry(menu, entry_no);
1791 } else if (!submenu->flags.buttoned) {
1792 selectEntry(menu, -1);
1795 } else if (!delayed_select) {
1796 if (menu == scr->switch_menu && event->xbutton.button == Button3) {
1797 selectEntry(menu, entry_no);
1798 OpenWindowMenu2((WWindow *)entry->clientdata,
1799 event->xbutton.x_root,
1800 event->xbutton.y_root, False);
1801 wwin = (WWindow *)entry->clientdata;
1802 desc = &wwin->screen_ptr->window_menu->menu->descriptor;
1803 event->xany.send_event = True;
1804 (*desc->handle_mousedown)(desc, event);
1806 XUngrabPointer(dpy, CurrentTime);
1807 selectEntry(menu, -1);
1808 return;
1809 } else {
1810 selectEntry(menu, entry_no);
1814 if (!wPreferences.wrap_menus && !wPreferences.scrollable_menus) {
1815 if (!menu->timer)
1816 dragScrollMenuCallback(menu);
1820 prevx = bev->x_root;
1821 prevy = bev->y_root;
1822 while (!done) {
1823 int x, y;
1825 XAllowEvents(dpy, AsyncPointer | SyncPointer, CurrentTime);
1827 WMMaskEvent(dpy, ExposureMask | ButtonMotionMask | ButtonReleaseMask | ButtonPressMask, &ev);
1828 switch (ev.type) {
1829 case MotionNotify:
1830 smenu = findMenu(scr, &x, &y);
1832 if (smenu == NULL) {
1833 /* moved mouse out of menu */
1835 if (!delayed_select && d_data.magic) {
1836 WMDeleteTimerHandler(d_data.magic);
1837 d_data.magic = NULL;
1839 if (menu == NULL
1840 || (menu->selected_entry >= 0
1841 && menu->entries[menu->selected_entry]->cascade >= 0)) {
1842 prevx = ev.xmotion.x_root;
1843 prevy = ev.xmotion.y_root;
1845 break;
1847 selectEntry(menu, -1);
1848 menu = smenu;
1849 prevx = ev.xmotion.x_root;
1850 prevy = ev.xmotion.y_root;
1851 break;
1852 } else if (menu && menu != smenu
1853 && (menu->selected_entry < 0
1854 || menu->entries[menu->selected_entry]->cascade < 0)) {
1855 selectEntry(menu, -1);
1857 if (!delayed_select && d_data.magic) {
1858 WMDeleteTimerHandler(d_data.magic);
1859 d_data.magic = NULL;
1861 } else {
1863 /* hysteresis for item selection */
1865 /* check if the motion was to the side, indicating that
1866 * the user may want to cross to a submenu */
1867 if (!delayed_select && menu) {
1868 int dx;
1869 Bool moved_to_submenu; /* moved to direction of submenu */
1871 dx = abs(prevx - ev.xmotion.x_root);
1873 moved_to_submenu = False;
1874 if (dx > 0 /* if moved enough to the side */
1875 /* maybe a open submenu */
1876 && menu->selected_entry >= 0
1877 /* moving to the right direction */
1878 && (wPreferences.align_menus || ev.xmotion.y_root >= prevy)) {
1879 int index;
1881 index = menu->entries[menu->selected_entry]->cascade;
1882 if (index >= 0) {
1883 if (menu->cascades[index]->frame_x > menu->frame_x) {
1884 if (prevx < ev.xmotion.x_root)
1885 moved_to_submenu = True;
1886 } else {
1887 if (prevx > ev.xmotion.x_root)
1888 moved_to_submenu = True;
1893 if (menu != smenu) {
1894 if (d_data.magic) {
1895 WMDeleteTimerHandler(d_data.magic);
1896 d_data.magic = NULL;
1898 } else if (moved_to_submenu) {
1899 /* while we are moving, postpone the selection */
1900 if (d_data.magic) {
1901 WMDeleteTimerHandler(d_data.magic);
1903 d_data.delayed_select = NULL;
1904 d_data.menu = menu;
1905 d_data.magic = WMAddTimerHandler(MENU_SELECT_DELAY,
1906 delaySelection, &d_data);
1907 prevx = ev.xmotion.x_root;
1908 prevy = ev.xmotion.y_root;
1909 break;
1910 } else {
1911 if (d_data.magic) {
1912 WMDeleteTimerHandler(d_data.magic);
1913 d_data.magic = NULL;
1918 prevx = ev.xmotion.x_root;
1919 prevy = ev.xmotion.y_root;
1920 if (menu != smenu) {
1921 /* pointer crossed menus */
1922 if (menu && menu->timer) {
1923 WMDeleteTimerHandler(menu->timer);
1924 menu->timer = NULL;
1926 if (smenu)
1927 dragScrollMenuCallback(smenu);
1929 menu = smenu;
1930 if (!menu->timer)
1931 dragScrollMenuCallback(menu);
1933 if (!delayed_select) {
1934 entry_no = getEntryAt(menu, x, y);
1935 if (entry_no >= 0) {
1936 entry = menu->entries[entry_no];
1937 if (entry->flags.enabled && entry->cascade >= 0 && menu->cascades) {
1938 WMenu *submenu = menu->cascades[entry->cascade];
1939 if (submenu->flags.mapped && !submenu->flags.buttoned
1940 && menu->selected_entry != entry_no) {
1941 wMenuUnmap(submenu);
1945 selectEntry(menu, entry_no);
1947 break;
1949 case ButtonPress:
1950 break;
1952 case ButtonRelease:
1953 if (ev.xbutton.button == event->xbutton.button)
1954 done = 1;
1955 break;
1957 case Expose:
1958 WMHandleEvent(&ev);
1959 break;
1963 if (menu && menu->timer) {
1964 WMDeleteTimerHandler(menu->timer);
1965 menu->timer = NULL;
1967 if (d_data.magic != NULL) {
1968 WMDeleteTimerHandler(d_data.magic);
1969 d_data.magic = NULL;
1972 if (menu && menu->selected_entry >= 0) {
1973 entry = menu->entries[menu->selected_entry];
1974 if (entry->callback != NULL && entry->flags.enabled && entry->cascade < 0) {
1975 /* blink and erase menu selection */
1976 #if (MENU_BLINK_DELAY > 0)
1977 int sel = menu->selected_entry;
1978 int i;
1980 for (i = 0; i < MENU_BLINK_COUNT; i++) {
1981 paintEntry(menu, sel, False);
1982 XSync(dpy, 0);
1983 wusleep(MENU_BLINK_DELAY);
1984 paintEntry(menu, sel, True);
1985 XSync(dpy, 0);
1986 wusleep(MENU_BLINK_DELAY);
1988 #endif
1989 /* unmap the menu, it's parents and call the callback */
1990 if (!menu->flags.buttoned && (!menu->flags.app_menu || menu->parent != NULL)) {
1991 closeCascade(menu);
1992 } else {
1993 selectEntry(menu, -1);
1995 (*entry->callback) (menu, entry);
1997 /* If the user double clicks an entry, the entry will
1998 * be executed twice, which is not good for things like
1999 * the root menu. So, ignore any clicks that were generated
2000 * while the entry was being executed */
2001 while (XCheckTypedWindowEvent(dpy, menu->menu->window, ButtonPress, &ev)) ;
2002 } else if (entry->callback != NULL && entry->cascade < 0) {
2003 selectEntry(menu, -1);
2004 } else {
2005 if (entry->cascade >= 0 && menu->cascades && menu->cascades[entry->cascade]->flags.brother) {
2006 selectEntry(menu, -1);
2011 if (((WMenu *) desc->parent)->flags.brother || close_on_exit || !smenu)
2012 closeCascade(desc->parent);
2014 /* close the cascade windows that should not remain opened */
2015 closeBrotherCascadesOf(desc->parent);
2017 if (!wPreferences.wrap_menus)
2018 wMenuMove(parentMenu(desc->parent), old_frame_x, old_frame_y, True);
2020 byebye:
2021 /* Just to be sure in case we skip the 2 above because of a goto byebye */
2022 if (menu && menu->timer) {
2023 WMDeleteTimerHandler(menu->timer);
2024 menu->timer = NULL;
2026 if (d_data.magic != NULL) {
2027 WMDeleteTimerHandler(d_data.magic);
2028 d_data.magic = NULL;
2031 ((WMenu *) desc->parent)->flags.inside_handler = 0;
2034 void wMenuMove(WMenu * menu, int x, int y, int submenus)
2036 WMenu *submenu;
2037 int i;
2039 if (!menu)
2040 return;
2042 menu->frame_x = x;
2043 menu->frame_y = y;
2044 XMoveWindow(dpy, menu->frame->core->window, x, y);
2046 if (submenus > 0 && menu->selected_entry >= 0) {
2047 i = menu->entries[menu->selected_entry]->cascade;
2049 if (i >= 0 && menu->cascades) {
2050 submenu = menu->cascades[i];
2051 if (submenu->flags.mapped && !submenu->flags.buttoned) {
2052 if (wPreferences.align_menus) {
2053 wMenuMove(submenu, x + MENUW(menu), y, submenus);
2054 } else {
2055 wMenuMove(submenu, x + MENUW(menu),
2056 y + submenu->entry_height * menu->selected_entry, submenus);
2061 if (submenus < 0 && menu->parent != NULL && menu->parent->flags.mapped && !menu->parent->flags.buttoned) {
2062 if (wPreferences.align_menus) {
2063 wMenuMove(menu->parent, x - MENUW(menu->parent), y, submenus);
2064 } else {
2065 wMenuMove(menu->parent, x - MENUW(menu->parent), menu->frame_y
2066 - menu->parent->entry_height * menu->parent->selected_entry, submenus);
2071 static void changeMenuLevels(WMenu * menu, int lower)
2073 int i;
2075 if (!lower) {
2076 ChangeStackingLevel(menu->frame->core, (!menu->parent ? WMMainMenuLevel : WMSubmenuLevel));
2077 wRaiseFrame(menu->frame->core);
2078 menu->flags.lowered = 0;
2079 } else {
2080 ChangeStackingLevel(menu->frame->core, WMNormalLevel);
2081 wLowerFrame(menu->frame->core);
2082 menu->flags.lowered = 1;
2084 for (i = 0; i < menu->cascade_no; i++) {
2085 if (menu->cascades[i]
2086 && !menu->cascades[i]->flags.buttoned && menu->cascades[i]->flags.lowered != lower) {
2087 changeMenuLevels(menu->cascades[i], lower);
2092 static void menuTitleDoubleClick(WCoreWindow * sender, void *data, XEvent * event)
2094 WMenu *menu = data;
2095 int lower;
2097 if (event->xbutton.state & MOD_MASK) {
2098 if (menu->flags.lowered) {
2099 lower = 0;
2100 } else {
2101 lower = 1;
2103 changeMenuLevels(menu, lower);
2107 static void menuTitleMouseDown(WCoreWindow * sender, void *data, XEvent * event)
2109 WMenu *menu = data;
2110 WMenu *tmp;
2111 XEvent ev;
2112 int x = menu->frame_x, y = menu->frame_y;
2113 int dx = event->xbutton.x_root, dy = event->xbutton.y_root;
2114 int i, lower;
2115 Bool started;
2117 /* can't touch the menu copy */
2118 if (menu->flags.brother)
2119 return;
2121 if (event->xbutton.button != Button1 && event->xbutton.button != Button2)
2122 return;
2124 if (event->xbutton.state & MOD_MASK) {
2125 wLowerFrame(menu->frame->core);
2126 lower = 1;
2127 } else {
2128 wRaiseFrame(menu->frame->core);
2129 lower = 0;
2131 tmp = menu;
2133 /* lower/raise all submenus */
2134 while (1) {
2135 if (tmp->selected_entry >= 0 && tmp->cascades && tmp->entries[tmp->selected_entry]->cascade >= 0) {
2136 tmp = tmp->cascades[tmp->entries[tmp->selected_entry]->cascade];
2137 if (!tmp || !tmp->flags.mapped)
2138 break;
2139 if (lower)
2140 wLowerFrame(tmp->frame->core);
2141 else
2142 wRaiseFrame(tmp->frame->core);
2143 } else {
2144 break;
2148 /* tear off the menu if it's a root menu or a cascade
2149 application menu */
2150 if (!menu->flags.buttoned && !menu->flags.brother && (!menu->flags.app_menu || menu->parent != NULL)) {
2151 menu->flags.buttoned = 1;
2152 wFrameWindowShowButton(menu->frame, WFF_RIGHT_BUTTON);
2153 if (menu->parent) {
2154 /* turn off selected menu entry in parent menu */
2155 selectEntry(menu->parent, -1);
2157 /* make parent map the copy in place of the original */
2158 for (i = 0; i < menu->parent->cascade_no; i++) {
2159 if (menu->parent->cascades[i] == menu) {
2160 menu->parent->cascades[i] = menu->brother;
2161 break;
2167 started = False;
2168 while (1) {
2169 WMMaskEvent(dpy, ButtonMotionMask | ButtonReleaseMask | ButtonPressMask | ExposureMask, &ev);
2170 switch (ev.type) {
2171 case MotionNotify:
2172 if (started) {
2173 x += ev.xmotion.x_root - dx;
2174 y += ev.xmotion.y_root - dy;
2175 dx = ev.xmotion.x_root;
2176 dy = ev.xmotion.y_root;
2177 wMenuMove(menu, x, y, True);
2178 } else {
2179 if (abs(ev.xmotion.x_root - dx) > MOVE_THRESHOLD
2180 || abs(ev.xmotion.y_root - dy) > MOVE_THRESHOLD) {
2181 started = True;
2182 XGrabPointer(dpy, menu->frame->titlebar->window, False,
2183 ButtonMotionMask | ButtonReleaseMask
2184 | ButtonPressMask,
2185 GrabModeAsync, GrabModeAsync, None,
2186 wPreferences.cursor[WCUR_MOVE], CurrentTime);
2189 break;
2191 case ButtonPress:
2192 break;
2194 case ButtonRelease:
2195 if (ev.xbutton.button != event->xbutton.button)
2196 break;
2197 XUngrabPointer(dpy, CurrentTime);
2198 return;
2200 default:
2201 WMHandleEvent(&ev);
2202 break;
2208 *----------------------------------------------------------------------
2209 * menuCloseClick--
2210 * Handles mouse click on the close button of menus. The menu is
2211 * closed when the button is clicked.
2213 * Side effects:
2214 * The closed menu is reinserted at it's parent menus
2215 * cascade list.
2216 *----------------------------------------------------------------------
2218 static void menuCloseClick(WCoreWindow * sender, void *data, XEvent * event)
2220 WMenu *menu = (WMenu *) data;
2221 WMenu *parent = menu->parent;
2222 int i;
2224 if (parent) {
2225 for (i = 0; i < parent->cascade_no; i++) {
2226 /* find the entry that points to the copy */
2227 if (parent->cascades[i] == menu->brother) {
2228 /* make it point to the original */
2229 parent->cascades[i] = menu;
2230 menu->parent = parent;
2231 break;
2235 wMenuUnmap(menu);
2238 static void saveMenuInfo(WMPropList * dict, WMenu * menu, WMPropList * key)
2240 WMPropList *value, *list;
2241 char buffer[256];
2243 snprintf(buffer, sizeof(buffer), "%i,%i", menu->frame_x, menu->frame_y);
2244 value = WMCreatePLString(buffer);
2245 list = WMCreatePLArray(value, NULL);
2246 if (menu->flags.lowered)
2247 WMAddToPLArray(list, WMCreatePLString("lowered"));
2248 WMPutInPLDictionary(dict, key, list);
2249 WMReleasePropList(value);
2250 WMReleasePropList(list);
2253 void wMenuSaveState(WScreen * scr)
2255 WMPropList *menus, *key;
2256 int save_menus = 0;
2258 menus = WMCreatePLDictionary(NULL, NULL);
2260 if (scr->switch_menu && scr->switch_menu->flags.buttoned) {
2261 key = WMCreatePLString("SwitchMenu");
2262 saveMenuInfo(menus, scr->switch_menu, key);
2263 WMReleasePropList(key);
2264 save_menus = 1;
2267 if (saveMenuRecurs(menus, scr, scr->root_menu))
2268 save_menus = 1;
2270 if (w_global.workspace.menu && w_global.workspace.menu->flags.buttoned) {
2271 key = WMCreatePLString("WorkspaceMenu");
2272 saveMenuInfo(menus, w_global.workspace.menu, key);
2273 WMReleasePropList(key);
2274 save_menus = 1;
2277 if (save_menus) {
2278 key = WMCreatePLString("Menus");
2279 WMPutInPLDictionary(w_global.session_state, key, menus);
2280 WMReleasePropList(key);
2282 WMReleasePropList(menus);
2285 static Bool getMenuPath(WMenu * menu, char *buffer, int bufSize)
2287 Bool ok = True;
2288 int len = 0;
2290 if (!menu->flags.titled || !menu->frame->title[0])
2291 return False;
2293 len = strlen(menu->frame->title);
2294 if (len >= bufSize)
2295 return False;
2297 if (menu->parent) {
2298 ok = getMenuPath(menu->parent, buffer, bufSize - len - 1);
2299 if (!ok)
2300 return False;
2303 strcat(buffer, "\\");
2304 strcat(buffer, menu->frame->title);
2306 return True;
2309 static Bool saveMenuRecurs(WMPropList * menus, WScreen * scr, WMenu * menu)
2311 WMPropList *key;
2312 int save_menus = 0, i;
2313 char buffer[512];
2314 Bool ok = True;
2316 if (menu->flags.brother)
2317 menu = menu->brother;
2319 if (menu->flags.buttoned && menu != scr->switch_menu) {
2321 buffer[0] = '\0';
2322 ok = getMenuPath(menu, buffer, 510);
2324 if (ok) {
2325 key = WMCreatePLString(buffer);
2326 saveMenuInfo(menus, menu, key);
2327 WMReleasePropList(key);
2328 save_menus = 1;
2332 if (ok) {
2333 for (i = 0; i < menu->cascade_no; i++) {
2334 if (saveMenuRecurs(menus, scr, menu->cascades[i]))
2335 save_menus = 1;
2338 return save_menus;
2341 #define COMPLAIN(key) wwarning(_("bad value in menus state info: %s"), key)
2343 static Bool getMenuInfo(WMPropList * info, int *x, int *y, Bool * lowered)
2345 WMPropList *pos;
2347 *lowered = False;
2349 if (WMIsPLArray(info)) {
2350 WMPropList *flags;
2351 pos = WMGetFromPLArray(info, 0);
2352 flags = WMGetFromPLArray(info, 1);
2353 if (flags != NULL && WMIsPLString(flags) && WMGetFromPLString(flags) != NULL
2354 && strcmp(WMGetFromPLString(flags), "lowered") == 0) {
2355 *lowered = True;
2357 } else {
2358 pos = info;
2361 if (pos != NULL && WMIsPLString(pos)) {
2362 if (sscanf(WMGetFromPLString(pos), "%i,%i", x, y) != 2)
2363 COMPLAIN("Position");
2364 } else {
2365 COMPLAIN("(position, flags...)");
2366 return False;
2369 return True;
2372 static int restoreMenu(WScreen * scr, WMPropList * menu, int which)
2374 int x, y;
2375 Bool lowered = False;
2376 WMenu *pmenu = NULL;
2378 if (!menu)
2379 return False;
2381 if (!getMenuInfo(menu, &x, &y, &lowered))
2382 return False;
2384 if (which & WSS_SWITCHMENU) {
2385 OpenSwitchMenu(scr, x, y, False);
2386 pmenu = scr->switch_menu;
2389 if (pmenu) {
2390 int width = MENUW(pmenu);
2391 int height = MENUH(pmenu);
2392 WMRect rect = wGetRectForHead(scr, wGetHeadForPointerLocation(scr));
2394 if (lowered) {
2395 changeMenuLevels(pmenu, True);
2398 if (x < rect.pos.x - width)
2399 x = rect.pos.x;
2400 if (x > rect.pos.x + rect.size.width)
2401 x = rect.pos.x + rect.size.width - width;
2402 if (y < rect.pos.y)
2403 y = rect.pos.y;
2404 if (y > rect.pos.y + rect.size.height)
2405 y = rect.pos.y + rect.size.height - height;
2407 wMenuMove(pmenu, x, y, True);
2408 pmenu->flags.buttoned = 1;
2409 wFrameWindowShowButton(pmenu->frame, WFF_RIGHT_BUTTON);
2410 return True;
2412 return False;
2415 static int restoreMenuRecurs(WScreen *scr, WMPropList *menus, WMenu *menu, const char *path)
2417 WMPropList *key, *entry;
2418 char buffer[512];
2419 int i, x, y, res;
2420 Bool lowered;
2422 if (strlen(path) + strlen(menu->frame->title) > 510)
2423 return False;
2425 snprintf(buffer, sizeof(buffer), "%s\\%s", path, menu->frame->title);
2426 key = WMCreatePLString(buffer);
2427 entry = WMGetFromPLDictionary(menus, key);
2428 res = False;
2430 if (entry && getMenuInfo(entry, &x, &y, &lowered)) {
2432 if (!menu->flags.mapped) {
2433 int width = MENUW(menu);
2434 int height = MENUH(menu);
2435 WMRect rect = wGetRectForHead(scr, wGetHeadForPointerLocation(scr));
2437 wMenuMapAt(menu, x, y, False);
2439 if (menu->parent) {
2440 /* make parent map the copy in place of the original */
2441 for (i = 0; i < menu->parent->cascade_no; i++) {
2442 if (menu->parent->cascades[i] == menu) {
2443 menu->parent->cascades[i] = menu->brother;
2444 break;
2448 if (lowered) {
2449 changeMenuLevels(menu, True);
2452 if (x < rect.pos.x - width)
2453 x = rect.pos.x;
2454 if (x > rect.pos.x + rect.size.width)
2455 x = rect.pos.x + rect.size.width - width;
2456 if (y < rect.pos.y)
2457 y = rect.pos.y;
2458 if (y > rect.pos.y + rect.size.height)
2459 y = rect.pos.y + rect.size.height - height;
2461 wMenuMove(menu, x, y, True);
2462 menu->flags.buttoned = 1;
2463 wFrameWindowShowButton(menu->frame, WFF_RIGHT_BUTTON);
2464 res = True;
2468 WMReleasePropList(key);
2470 for (i = 0; i < menu->cascade_no; i++) {
2471 if (restoreMenuRecurs(scr, menus, menu->cascades[i], buffer) != False)
2472 res = True;
2475 return res;
2478 void wMenuRestoreState(WScreen * scr)
2480 WMPropList *menus, *menu, *key, *skey;
2482 if (!w_global.session_state)
2483 return;
2485 key = WMCreatePLString("Menus");
2486 menus = WMGetFromPLDictionary(w_global.session_state, key);
2487 WMReleasePropList(key);
2489 if (!menus)
2490 return;
2492 /* restore menus */
2494 skey = WMCreatePLString("SwitchMenu");
2495 menu = WMGetFromPLDictionary(menus, skey);
2496 WMReleasePropList(skey);
2497 restoreMenu(scr, menu, WSS_SWITCHMENU);
2499 if (!scr->root_menu) {
2500 OpenRootMenu(scr, scr->scr_width * 2, 0, False);
2501 wMenuUnmap(scr->root_menu);
2503 restoreMenuRecurs(scr, menus, scr->root_menu, "");