wmgenmenu: Update mrxvt options
[wmaker-crm.git] / src / menu.c
blob449904fd84a391eb398336f41b87bd58f61aae76
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"
46 /****** Global Variables ******/
48 extern Cursor wCursor[WCUR_LAST];
50 extern XContext wWinContext;
52 extern WPreferences wPreferences;
54 #define MOD_MASK wPreferences.modifier_mask
56 #define MENU_SCROLL_STEP menuScrollParameters[(int)wPreferences.menu_scroll_speed].steps
57 #define MENU_SCROLL_DELAY menuScrollParameters[(int)wPreferences.menu_scroll_speed].delay
59 #define MENUW(m) ((m)->frame->core->width+2*FRAME_BORDER_WIDTH)
60 #define MENUH(m) ((m)->frame->core->height+2*FRAME_BORDER_WIDTH)
62 /***** Local Stuff ******/
64 #define WSS_ROOTMENU (1<<0)
65 #define WSS_SWITCHMENU (1<<1)
66 #define WSS_WSMENU (1<<2)
68 static struct {
69 int steps;
70 int delay;
71 } menuScrollParameters[5] = {
73 MENU_SCROLL_STEPS_UF, MENU_SCROLL_DELAY_UF}, {
74 MENU_SCROLL_STEPS_F, MENU_SCROLL_DELAY_F}, {
75 MENU_SCROLL_STEPS_M, MENU_SCROLL_DELAY_M}, {
76 MENU_SCROLL_STEPS_S, MENU_SCROLL_DELAY_S}, {
77 MENU_SCROLL_STEPS_US, MENU_SCROLL_DELAY_US}};
79 static void menuMouseDown(WObjDescriptor * desc, XEvent * event);
80 static void menuExpose(WObjDescriptor * desc, XEvent * event);
81 static void menuTitleDoubleClick(WCoreWindow * sender, void *data, XEvent * event);
82 static void menuTitleMouseDown(WCoreWindow * sender, void *data, XEvent * event);
83 static void menuCloseClick(WCoreWindow * sender, void *data, XEvent * event);
84 static void updateTexture(WMenu * menu);
85 static int saveMenuRecurs(WMPropList * menus, WScreen * scr, WMenu * menu);
86 static int restoreMenuRecurs(WScreen * scr, WMPropList * menus, WMenu * menu, char *path);
87 static void selectEntry(WMenu * menu, int entry_no);
88 static void closeCascade(WMenu * menu);
90 /****** Notification Observers ******/
92 static void appearanceObserver(void *self, WMNotification * notif)
94 WMenu *menu = (WMenu *) self;
95 uintptr_t flags = (uintptr_t)WMGetNotificationClientData(notif);
97 if (!menu->flags.realized)
98 return;
100 if (WMGetNotificationName(notif) == WNMenuAppearanceSettingsChanged) {
101 if (flags & WFontSettings) {
102 menu->flags.realized = 0;
103 wMenuRealize(menu);
105 if (flags & WTextureSettings) {
106 if (!menu->flags.brother)
107 updateTexture(menu);
109 if (flags & (WTextureSettings | WColorSettings)) {
110 wMenuPaint(menu);
112 } else if (menu->flags.titled) {
114 if (flags & WFontSettings) {
115 menu->flags.realized = 0;
116 wMenuRealize(menu);
118 if (flags & WTextureSettings) {
119 menu->frame->flags.need_texture_remake = 1;
121 if (flags & (WColorSettings | WTextureSettings)) {
122 wFrameWindowPaint(menu->frame);
127 /************************************/
130 *----------------------------------------------------------------------
131 * wMenuCreate--
132 * Creates a new empty menu with the specified title. If main_menu
133 * is True, the created menu will be a main menu, which has some special
134 * properties such as being placed over other normal menus.
135 * If title is NULL, the menu will have no titlebar.
137 * Returns:
138 * The created menu.
139 *----------------------------------------------------------------------
141 WMenu *wMenuCreate(WScreen * screen, char *title, int main_menu)
143 WMenu *menu;
144 static int brother = 0;
145 int tmp, flags;
147 menu = wmalloc(sizeof(WMenu));
149 memset(menu, 0, sizeof(WMenu));
151 #ifdef SINGLE_MENULEVEL
152 tmp = WMSubmenuLevel;
153 #else
154 tmp = (main_menu ? WMMainMenuLevel : WMSubmenuLevel);
155 #endif
157 flags = WFF_SINGLE_STATE | WFF_BORDER;
158 if (title) {
159 flags |= WFF_TITLEBAR | WFF_RIGHT_BUTTON;
160 menu->flags.titled = 1;
162 menu->frame =
163 wFrameWindowCreate(screen, tmp, 8, 2, 1, 1, &wPreferences.menu_title_clearance, flags,
164 screen->menu_title_texture, NULL,
165 screen->menu_title_color, &screen->menu_title_font);
167 menu->frame->core->descriptor.parent = menu;
168 menu->frame->core->descriptor.parent_type = WCLASS_MENU;
169 menu->frame->core->descriptor.handle_mousedown = menuMouseDown;
171 wFrameWindowHideButton(menu->frame, WFF_RIGHT_BUTTON);
173 if (title) {
174 menu->frame->title = wstrdup(title);
177 menu->frame->flags.justification = WTJ_LEFT;
179 menu->frame->rbutton_image = screen->b_pixmaps[WBUT_CLOSE];
181 menu->entry_no = 0;
182 menu->alloced_entries = 0;
183 menu->selected_entry = -1;
184 menu->entries = NULL;
186 menu->frame_x = screen->app_menu_x;
187 menu->frame_y = screen->app_menu_y;
189 menu->frame->child = menu;
191 menu->flags.lowered = 0;
193 /* create borders */
194 if (title) {
195 /* setup object descriptors */
196 menu->frame->on_mousedown_titlebar = menuTitleMouseDown;
197 menu->frame->on_dblclick_titlebar = menuTitleDoubleClick;
200 menu->frame->on_click_right = menuCloseClick;
202 menu->menu = wCoreCreate(menu->frame->core, 0, menu->frame->top_width, menu->frame->core->width, 10);
204 menu->menu->descriptor.parent = menu;
205 menu->menu->descriptor.parent_type = WCLASS_MENU;
206 menu->menu->descriptor.handle_expose = menuExpose;
207 menu->menu->descriptor.handle_mousedown = menuMouseDown;
209 menu->menu_texture_data = None;
211 XMapWindow(dpy, menu->menu->window);
213 XFlush(dpy);
215 if (!brother) {
216 brother = 1;
217 menu->brother = wMenuCreate(screen, title, main_menu);
218 brother = 0;
219 menu->brother->flags.brother = 1;
220 menu->brother->brother = menu;
222 WMAddNotificationObserver(appearanceObserver, menu, WNMenuAppearanceSettingsChanged, menu);
224 WMAddNotificationObserver(appearanceObserver, menu, WNMenuTitleAppearanceSettingsChanged, menu);
226 return menu;
229 WMenu *wMenuCreateForApp(WScreen * screen, char *title, int main_menu)
231 WMenu *menu;
233 menu = wMenuCreate(screen, title, main_menu);
234 if (!menu)
235 return NULL;
236 menu->flags.app_menu = 1;
237 menu->brother->flags.app_menu = 1;
239 return menu;
242 static void insertEntry(WMenu * menu, WMenuEntry * entry, int index)
244 int i;
246 for (i = menu->entry_no - 1; i >= index; i--) {
247 menu->entries[i]->order++;
248 menu->entries[i + 1] = menu->entries[i];
250 menu->entries[index] = entry;
253 WMenuEntry *wMenuInsertCallback(WMenu * menu, int index, char *text,
254 void (*callback) (WMenu * menu, WMenuEntry * entry), void *clientdata)
256 WMenuEntry *entry;
258 menu->flags.realized = 0;
259 menu->brother->flags.realized = 0;
261 /* reallocate array if it's too small */
262 if (menu->entry_no >= menu->alloced_entries) {
263 void *tmp;
265 tmp = wrealloc(menu->entries, sizeof(WMenuEntry) * (menu->alloced_entries + 5));
267 menu->entries = tmp;
268 menu->alloced_entries += 5;
270 menu->brother->entries = tmp;
271 menu->brother->alloced_entries = menu->alloced_entries;
273 entry = wmalloc(sizeof(WMenuEntry));
274 memset(entry, 0, sizeof(WMenuEntry));
275 entry->flags.enabled = 1;
276 entry->text = wstrdup(text);
277 entry->cascade = -1;
278 entry->clientdata = clientdata;
279 entry->callback = callback;
280 if (index < 0 || index >= menu->entry_no) {
281 entry->order = menu->entry_no;
282 menu->entries[menu->entry_no] = entry;
283 } else {
284 entry->order = index;
285 insertEntry(menu, entry, index);
288 menu->entry_no++;
289 menu->brother->entry_no = menu->entry_no;
291 return entry;
294 void wMenuEntrySetCascade(WMenu * menu, WMenuEntry * entry, WMenu * cascade)
296 WMenu *brother = menu->brother;
297 int i, done;
299 assert(menu->flags.brother == 0);
301 if (entry->cascade >= 0) {
302 menu->flags.realized = 0;
303 brother->flags.realized = 0;
306 cascade->parent = menu;
308 cascade->brother->parent = brother;
310 done = 0;
311 for (i = 0; i < menu->cascade_no; i++) {
312 if (menu->cascades[i] == NULL) {
313 menu->cascades[i] = cascade;
314 brother->cascades[i] = cascade->brother;
315 done = 1;
316 entry->cascade = i;
317 break;
320 if (!done) {
321 entry->cascade = menu->cascade_no;
323 menu->cascades = wrealloc(menu->cascades, sizeof(WMenu) * (menu->cascade_no + 1));
324 menu->cascades[menu->cascade_no++] = cascade;
326 brother->cascades = wrealloc(brother->cascades, sizeof(WMenu) * (brother->cascade_no + 1));
327 brother->cascades[brother->cascade_no++] = cascade->brother;
330 if (menu->flags.lowered) {
332 cascade->flags.lowered = 1;
333 ChangeStackingLevel(cascade->frame->core, WMNormalLevel);
335 cascade->brother->flags.lowered = 1;
336 ChangeStackingLevel(cascade->brother->frame->core, WMNormalLevel);
339 if (!menu->flags.realized)
340 wMenuRealize(menu);
343 void wMenuEntryRemoveCascade(WMenu * menu, WMenuEntry * entry)
345 assert(menu->flags.brother == 0);
347 /* destroy cascade menu */
348 if (entry->cascade >= 0 && menu->cascades && menu->cascades[entry->cascade] != NULL) {
350 wMenuDestroy(menu->cascades[entry->cascade], True);
352 menu->cascades[entry->cascade] = NULL;
353 menu->brother->cascades[entry->cascade] = NULL;
355 entry->cascade = -1;
359 void wMenuRemoveItem(WMenu * menu, int index)
361 int i;
363 if (menu->flags.brother) {
364 wMenuRemoveItem(menu->brother, index);
365 return;
368 if (index >= menu->entry_no)
369 return;
371 /* destroy cascade menu */
372 wMenuEntryRemoveCascade(menu, menu->entries[index]);
374 /* destroy unshared data */
376 if (menu->entries[index]->text)
377 wfree(menu->entries[index]->text);
379 if (menu->entries[index]->rtext)
380 wfree(menu->entries[index]->rtext);
382 if (menu->entries[index]->free_cdata && menu->entries[index]->clientdata)
383 (*menu->entries[index]->free_cdata) (menu->entries[index]->clientdata);
385 wfree(menu->entries[index]);
387 for (i = index; i < menu->entry_no - 1; i++) {
388 menu->entries[i + 1]->order--;
389 menu->entries[i] = menu->entries[i + 1];
391 menu->entry_no--;
392 menu->brother->entry_no--;
395 static Pixmap renderTexture(WMenu * menu)
397 RImage *img;
398 Pixmap pix;
399 int i;
400 RColor light;
401 RColor dark;
402 RColor mid;
403 WScreen *scr = menu->menu->screen_ptr;
404 WTexture *texture = scr->menu_item_texture;
406 if (wPreferences.menu_style == MS_NORMAL) {
407 img = wTextureRenderImage(texture, menu->menu->width, menu->entry_height, WREL_MENUENTRY);
408 } else {
409 img = wTextureRenderImage(texture, menu->menu->width, menu->menu->height + 1, WREL_MENUENTRY);
411 if (!img) {
412 wwarning(_("could not render texture: %s"), RMessageForError(RErrorCode));
414 return None;
417 if (wPreferences.menu_style == MS_SINGLE_TEXTURE) {
418 light.alpha = 0;
419 light.red = light.green = light.blue = 80;
421 dark.alpha = 255;
422 dark.red = dark.green = dark.blue = 0;
424 mid.alpha = 0;
425 mid.red = mid.green = mid.blue = 40;
427 for (i = 1; i < menu->entry_no; i++) {
428 ROperateLine(img, RSubtractOperation, 0, i * menu->entry_height - 2,
429 menu->menu->width - 1, i * menu->entry_height - 2, &mid);
431 RDrawLine(img, 0, i * menu->entry_height - 1,
432 menu->menu->width - 1, i * menu->entry_height - 1, &dark);
434 ROperateLine(img, RAddOperation, 0, i * menu->entry_height,
435 menu->menu->width - 1, i * menu->entry_height, &light);
438 if (!RConvertImage(scr->rcontext, img, &pix)) {
439 wwarning(_("error rendering image:%s"), RMessageForError(RErrorCode));
441 RReleaseImage(img);
443 return pix;
446 static void updateTexture(WMenu * menu)
448 WScreen *scr = menu->menu->screen_ptr;
450 /* setup background texture */
451 if (scr->menu_item_texture->any.type != WTEX_SOLID) {
452 if (!menu->flags.brother) {
453 FREE_PIXMAP(menu->menu_texture_data);
455 menu->menu_texture_data = renderTexture(menu);
457 XSetWindowBackgroundPixmap(dpy, menu->menu->window, menu->menu_texture_data);
458 XClearWindow(dpy, menu->menu->window);
460 XSetWindowBackgroundPixmap(dpy, menu->brother->menu->window, menu->menu_texture_data);
461 XClearWindow(dpy, menu->brother->menu->window);
463 } else {
464 XSetWindowBackground(dpy, menu->menu->window, scr->menu_item_texture->any.color.pixel);
465 XClearWindow(dpy, menu->menu->window);
469 void wMenuRealize(WMenu * menu)
471 int i;
472 int width, rwidth, mrwidth, mwidth;
473 int theight, twidth, eheight;
474 WScreen *scr = menu->frame->screen_ptr;
475 static int brother_done = 0;
476 int flags;
478 if (!brother_done) {
479 brother_done = 1;
480 wMenuRealize(menu->brother);
481 brother_done = 0;
484 flags = WFF_SINGLE_STATE | WFF_BORDER;
485 if (menu->flags.titled)
486 flags |= WFF_TITLEBAR | WFF_RIGHT_BUTTON;
488 wFrameWindowUpdateBorders(menu->frame, flags);
490 if (menu->flags.titled) {
491 twidth = WMWidthOfString(scr->menu_title_font, menu->frame->title, strlen(menu->frame->title));
492 theight = menu->frame->top_width;
493 twidth += theight + (wPreferences.new_style == TS_NEW ? 16 : 8);
494 } else {
495 twidth = 0;
496 theight = 0;
498 eheight = WMFontHeight(scr->menu_entry_font) + 6 + wPreferences.menu_text_clearance * 2;
499 menu->entry_height = eheight;
500 mrwidth = 0;
501 mwidth = 0;
502 for (i = 0; i < menu->entry_no; i++) {
503 char *text;
505 /* search widest text */
506 text = menu->entries[i]->text;
507 width = WMWidthOfString(scr->menu_entry_font, text, strlen(text)) + 10;
509 if (menu->entries[i]->flags.indicator) {
510 width += MENU_INDICATOR_SPACE;
513 if (width > mwidth)
514 mwidth = width;
516 /* search widest text on right */
517 text = menu->entries[i]->rtext;
518 if (text)
519 rwidth = WMWidthOfString(scr->menu_entry_font, text, strlen(text))
520 + 10;
521 else if (menu->entries[i]->cascade >= 0)
522 rwidth = 16;
523 else
524 rwidth = 4;
526 if (rwidth > mrwidth)
527 mrwidth = rwidth;
529 mwidth += mrwidth;
531 if (mwidth < twidth)
532 mwidth = twidth;
534 wCoreConfigure(menu->menu, 0, theight, mwidth, menu->entry_no * eheight - 1);
536 wFrameWindowResize(menu->frame, mwidth, menu->entry_no * eheight - 1
537 + menu->frame->top_width + menu->frame->bottom_width);
539 updateTexture(menu);
541 menu->flags.realized = 1;
543 if (menu->flags.mapped)
544 wMenuPaint(menu);
545 if (menu->brother->flags.mapped)
546 wMenuPaint(menu->brother);
549 void wMenuDestroy(WMenu * menu, int recurse)
551 int i;
553 WMRemoveNotificationObserver(menu);
555 /* remove any pending timers */
556 if (menu->timer)
557 WMDeleteTimerHandler(menu->timer);
558 menu->timer = NULL;
560 /* call destroy handler */
561 if (menu->on_destroy)
562 (*menu->on_destroy) (menu);
564 /* Destroy items if this menu own them. If this is the "brother" menu,
565 * leave them alone as it is shared by them.
567 if (!menu->flags.brother) {
568 for (i = 0; i < menu->entry_no; i++) {
570 wfree(menu->entries[i]->text);
572 if (menu->entries[i]->rtext)
573 wfree(menu->entries[i]->rtext);
574 #ifdef USER_MENU
576 if (menu->entries[i]->instances) {
577 WMReleasePropList(menu->entries[i]->instances);
579 #endif /* USER_MENU */
581 if (menu->entries[i]->free_cdata && menu->entries[i]->clientdata) {
582 (*menu->entries[i]->free_cdata) (menu->entries[i]->clientdata);
584 wfree(menu->entries[i]);
587 if (recurse) {
588 for (i = 0; i < menu->cascade_no; i++) {
589 if (menu->cascades[i]) {
590 if (menu->cascades[i]->flags.brother)
591 wMenuDestroy(menu->cascades[i]->brother, recurse);
592 else
593 wMenuDestroy(menu->cascades[i], recurse);
598 if (menu->entries)
599 wfree(menu->entries);
603 FREE_PIXMAP(menu->menu_texture_data);
605 if (menu->cascades)
606 wfree(menu->cascades);
608 wCoreDestroy(menu->menu);
609 wFrameWindowDestroy(menu->frame);
611 /* destroy copy of this menu */
612 if (!menu->flags.brother && menu->brother)
613 wMenuDestroy(menu->brother, False);
615 wfree(menu);
618 #define F_NORMAL 0
619 #define F_TOP 1
620 #define F_BOTTOM 2
621 #define F_NONE 3
623 static void drawFrame(WScreen * scr, Drawable win, int y, int w, int h, int type)
625 XSegment segs[2];
626 int i;
628 i = 0;
629 segs[i].x1 = segs[i].x2 = w - 1;
630 segs[i].y1 = y;
631 segs[i].y2 = y + h - 1;
632 i++;
633 if (type != F_TOP && type != F_NONE) {
634 segs[i].x1 = 1;
635 segs[i].y1 = segs[i].y2 = y + h - 2;
636 segs[i].x2 = w - 1;
637 i++;
639 XDrawSegments(dpy, win, scr->menu_item_auxtexture->dim_gc, segs, i);
641 i = 0;
642 segs[i].x1 = 0;
643 segs[i].y1 = y;
644 segs[i].x2 = 0;
645 segs[i].y2 = y + h - 1;
646 i++;
647 if (type != F_BOTTOM && type != F_NONE) {
648 segs[i].x1 = 0;
649 segs[i].y1 = y;
650 segs[i].x2 = w - 1;
651 segs[i].y2 = y;
652 i++;
654 XDrawSegments(dpy, win, scr->menu_item_auxtexture->light_gc, segs, i);
656 if (type != F_TOP && type != F_NONE)
657 XDrawLine(dpy, win, scr->menu_item_auxtexture->dark_gc, 0, y + h - 1, w - 1, y + h - 1);
660 static void paintEntry(WMenu * menu, int index, int selected)
662 WScreen *scr = menu->frame->screen_ptr;
663 Window win = menu->menu->window;
664 WMenuEntry *entry = menu->entries[index];
665 GC light, dim, dark;
666 WMColor *color;
667 int x, y, w, h, tw;
668 int type;
670 if (!menu->flags.realized)
671 return;
672 h = menu->entry_height;
673 w = menu->menu->width;
674 y = index * h;
676 light = scr->menu_item_auxtexture->light_gc;
677 dim = scr->menu_item_auxtexture->dim_gc;
678 dark = scr->menu_item_auxtexture->dark_gc;
680 if (wPreferences.menu_style == MS_FLAT && menu->entry_no > 1) {
681 if (index == 0)
682 type = F_TOP;
683 else if (index == menu->entry_no - 1)
684 type = F_BOTTOM;
685 else
686 type = F_NONE;
687 } else {
688 type = F_NORMAL;
691 /* paint background */
692 if (selected) {
693 XFillRectangle(dpy, win, WMColorGC(scr->select_color), 1, y + 1, w - 2, h - 3);
694 if (scr->menu_item_texture->any.type == WTEX_SOLID)
695 drawFrame(scr, win, y, w, h, type);
696 } else {
697 if (scr->menu_item_texture->any.type == WTEX_SOLID) {
698 XClearArea(dpy, win, 0, y + 1, w - 1, h - 3, False);
699 /* draw the frame */
700 drawFrame(scr, win, y, w, h, type);
701 } else {
702 XClearArea(dpy, win, 0, y, w, h, False);
706 if (selected) {
707 if (entry->flags.enabled)
708 color = scr->select_text_color;
709 else
710 color = scr->dtext_color;
711 } else if (!entry->flags.enabled) {
712 color = scr->dtext_color;
713 } else {
714 color = scr->mtext_color;
716 /* draw text */
717 x = 5;
718 if (entry->flags.indicator)
719 x += MENU_INDICATOR_SPACE + 2;
721 WMDrawString(scr->wmscreen, win, color, scr->menu_entry_font,
722 x, 3 + y + wPreferences.menu_text_clearance, entry->text, strlen(entry->text));
724 if (entry->cascade >= 0) {
725 /* draw the cascade indicator */
726 XDrawLine(dpy, win, dim, w - 11, y + 6, w - 6, y + h / 2 - 1);
727 XDrawLine(dpy, win, light, w - 11, y + h - 8, w - 6, y + h / 2 - 1);
728 XDrawLine(dpy, win, dark, w - 12, y + 6, w - 12, y + h - 8);
731 /* draw indicator */
732 if (entry->flags.indicator && entry->flags.indicator_on) {
733 int iw, ih;
734 WPixmap *indicator;
736 switch (entry->flags.indicator_type) {
737 case MI_CHECK:
738 indicator = scr->menu_check_indicator;
739 break;
740 case MI_MINIWINDOW:
741 indicator = scr->menu_mini_indicator;
742 break;
743 case MI_HIDDEN:
744 indicator = scr->menu_hide_indicator;
745 break;
746 case MI_SHADED:
747 indicator = scr->menu_shade_indicator;
748 break;
749 case MI_DIAMOND:
750 default:
751 indicator = scr->menu_radio_indicator;
752 break;
755 iw = indicator->width;
756 ih = indicator->height;
757 XSetClipMask(dpy, scr->copy_gc, indicator->mask);
758 XSetClipOrigin(dpy, scr->copy_gc, 5, y + (h - ih) / 2);
759 if (selected) {
760 if (entry->flags.enabled) {
761 XSetForeground(dpy, scr->copy_gc, WMColorPixel(scr->select_text_color));
762 } else {
763 XSetForeground(dpy, scr->copy_gc, WMColorPixel(scr->dtext_color));
765 } else {
766 if (entry->flags.enabled) {
767 XSetForeground(dpy, scr->copy_gc, WMColorPixel(scr->mtext_color));
768 } else {
769 XSetForeground(dpy, scr->copy_gc, WMColorPixel(scr->dtext_color));
772 XFillRectangle(dpy, win, scr->copy_gc, 5, y + (h - ih) / 2, iw, ih);
774 XCopyArea(dpy, indicator->image, win, scr->copy_gc, 0, 0,
775 iw, ih, 5, y+(h-ih)/2);
777 XSetClipOrigin(dpy, scr->copy_gc, 0, 0);
780 /* draw right text */
782 if (entry->rtext && entry->cascade < 0) {
783 tw = WMWidthOfString(scr->menu_entry_font, entry->rtext, strlen(entry->rtext));
785 WMDrawString(scr->wmscreen, win, color, scr->menu_entry_font, w - 6 - tw,
786 y + 3 + wPreferences.menu_text_clearance, entry->rtext, strlen(entry->rtext));
790 static void move_menus(WMenu * menu, int x, int y)
792 while (menu->parent) {
793 menu = menu->parent;
794 x -= MENUW(menu);
795 if (!wPreferences.align_menus && menu->selected_entry >= 0) {
796 y -= menu->selected_entry * menu->entry_height;
799 wMenuMove(menu, x, y, True);
802 static void makeVisible(WMenu * menu)
804 WScreen *scr = menu->frame->screen_ptr;
805 int x1, y1, x2, y2, new_x, new_y;
806 WMRect rect = wGetRectForHead(scr, wGetHeadForPointerLocation(scr));
808 if (menu->entry_no < 0)
809 return;
811 x1 = menu->frame_x;
812 y1 = menu->frame_y + menu->frame->top_width + menu->selected_entry * menu->entry_height;
813 x2 = x1 + MENUW(menu);
814 y2 = y1 + menu->entry_height;
816 new_x = x1;
817 new_y = y1;
819 if (x1 < rect.pos.x) {
820 new_x = rect.pos.x;
821 } else if (x2 >= rect.pos.x + rect.size.width) {
822 new_x = rect.pos.x + rect.size.width - MENUW(menu) - 1;
825 if (y1 < rect.pos.y) {
826 new_y = rect.pos.y;
827 } else if (y2 >= rect.pos.y + rect.size.height) {
828 new_y = rect.pos.y + rect.size.height - menu->entry_height - 1;
831 new_y = new_y - menu->frame->top_width - menu->selected_entry * menu->entry_height;
832 move_menus(menu, new_x, new_y);
835 static int check_key(WMenu * menu, XKeyEvent * event)
837 int i, ch, s;
838 char buffer[32];
840 if (XLookupString(event, buffer, 32, NULL, NULL) < 1)
841 return -1;
843 ch = toupper(buffer[0]);
845 s = (menu->selected_entry >= 0 ? menu->selected_entry + 1 : 0);
847 again:
848 for (i = s; i < menu->entry_no; i++) {
849 if (ch == toupper(menu->entries[i]->text[0])) {
850 return i;
853 /* no match. Retry from start, if previous started from a selected entry */
854 if (s != 0) {
855 s = 0;
856 goto again;
858 return -1;
861 static int keyboardMenu(WMenu * menu)
863 XEvent event;
864 KeySym ksym = NoSymbol;
865 int done = 0;
866 int index;
867 WMenuEntry *entry;
868 int old_pos_x = menu->frame_x;
869 int old_pos_y = menu->frame_y;
870 int new_x = old_pos_x, new_y = old_pos_y;
871 WMRect rect = wGetRectForHead(menu->frame->screen_ptr,
872 wGetHeadForPointerLocation(menu->frame->screen_ptr));
874 if (menu->flags.editing)
875 return False;
877 XGrabKeyboard(dpy, menu->frame->core->window, True, GrabModeAsync, GrabModeAsync, CurrentTime);
879 if (menu->frame_y + menu->frame->top_width >= rect.pos.y + rect.size.height)
880 new_y = rect.pos.y + rect.size.height - menu->frame->top_width;
882 if (menu->frame_x + MENUW(menu) >= rect.pos.x + rect.size.width)
883 new_x = rect.pos.x + rect.size.width - MENUW(menu) - 1;
885 move_menus(menu, new_x, new_y);
887 while (!done && menu->flags.mapped) {
888 XAllowEvents(dpy, AsyncKeyboard, CurrentTime);
889 WMMaskEvent(dpy, ExposureMask | ButtonMotionMask | ButtonPressMask
890 | ButtonReleaseMask | KeyPressMask | KeyReleaseMask | SubstructureNotifyMask, &event);
892 switch (event.type) {
893 case KeyPress:
894 ksym = XLookupKeysym(&event.xkey, 0);
895 if (wPreferences.vi_key_menus) {
896 switch (ksym) {
897 case XK_h:
898 ksym = XK_Left;
899 break;
901 case XK_j:
902 ksym = XK_Down;
903 break;
905 case XK_k:
906 ksym = XK_Up;
907 break;
909 case XK_l:
910 ksym = XK_Right;
911 break;
915 switch (ksym) {
916 case XK_Escape:
917 done = 1;
918 break;
920 case XK_Home:
921 #ifdef XK_KP_Home
922 case XK_KP_Home:
923 #endif
924 selectEntry(menu, 0);
925 makeVisible(menu);
926 break;
928 case XK_End:
929 #ifdef XK_KP_End
930 case XK_KP_End:
931 #endif
932 selectEntry(menu, menu->entry_no - 1);
933 makeVisible(menu);
934 break;
936 case XK_Up:
937 #ifdef XK_KP_Up
938 case XK_KP_Up:
939 #endif
940 if (menu->selected_entry <= 0)
941 selectEntry(menu, menu->entry_no - 1);
942 else
943 selectEntry(menu, menu->selected_entry - 1);
944 makeVisible(menu);
945 break;
947 case XK_Down:
948 #ifdef XK_KP_Down
949 case XK_KP_Down:
950 #endif
951 if (menu->selected_entry < 0)
952 selectEntry(menu, 0);
953 else if (menu->selected_entry == menu->entry_no - 1)
954 selectEntry(menu, 0);
955 else if (menu->selected_entry < menu->entry_no - 1)
956 selectEntry(menu, menu->selected_entry + 1);
957 makeVisible(menu);
958 break;
960 case XK_Right:
961 #ifdef XK_KP_Right
962 case XK_KP_Right:
963 #endif
964 if (menu->selected_entry >= 0) {
965 WMenuEntry *entry;
966 entry = menu->entries[menu->selected_entry];
968 if (entry->cascade >= 0 && menu->cascades
969 && menu->cascades[entry->cascade]->entry_no > 0) {
971 XUngrabKeyboard(dpy, CurrentTime);
973 selectEntry(menu->cascades[entry->cascade], 0);
974 if (!keyboardMenu(menu->cascades[entry->cascade]))
975 done = 1;
977 XGrabKeyboard(dpy, menu->frame->core->window, True,
978 GrabModeAsync, GrabModeAsync, CurrentTime);
981 break;
983 case XK_Left:
984 #ifdef XK_KP_Left
985 case XK_KP_Left:
986 #endif
987 if (menu->parent != NULL && menu->parent->selected_entry >= 0) {
988 selectEntry(menu, -1);
989 move_menus(menu, old_pos_x, old_pos_y);
990 return True;
992 break;
994 case XK_Return:
995 #ifdef XK_KP_Enter
996 case XK_KP_Enter:
997 #endif
998 done = 2;
999 break;
1001 default:
1002 index = check_key(menu, &event.xkey);
1003 if (index >= 0) {
1004 selectEntry(menu, index);
1007 break;
1009 default:
1010 if (event.type == ButtonPress)
1011 done = 1;
1013 WMHandleEvent(&event);
1017 XUngrabKeyboard(dpy, CurrentTime);
1019 if (done == 2 && menu->selected_entry >= 0) {
1020 entry = menu->entries[menu->selected_entry];
1021 } else {
1022 entry = NULL;
1025 if (entry && entry->callback != NULL && entry->flags.enabled && entry->cascade < 0) {
1026 #if (MENU_BLINK_COUNT > 0)
1027 int sel = menu->selected_entry;
1028 int i;
1030 for (i = 0; i < MENU_BLINK_COUNT; i++) {
1031 paintEntry(menu, sel, False);
1032 XSync(dpy, 0);
1033 wusleep(MENU_BLINK_DELAY);
1034 paintEntry(menu, sel, True);
1035 XSync(dpy, 0);
1036 wusleep(MENU_BLINK_DELAY);
1038 #endif
1039 selectEntry(menu, -1);
1041 if (!menu->flags.buttoned) {
1042 wMenuUnmap(menu);
1043 move_menus(menu, old_pos_x, old_pos_y);
1045 closeCascade(menu);
1047 (*entry->callback) (menu, entry);
1048 } else {
1049 if (!menu->flags.buttoned) {
1050 wMenuUnmap(menu);
1051 move_menus(menu, old_pos_x, old_pos_y);
1053 selectEntry(menu, -1);
1056 /* returns True if returning from a submenu to a parent menu,
1057 * False if exiting from menu */
1058 return False;
1061 void wMenuMapAt(WMenu * menu, int x, int y, int keyboard)
1063 WMRect rect = wGetRectForHead(menu->frame->screen_ptr,
1064 wGetHeadForPointerLocation(menu->frame->screen_ptr));
1066 if (!menu->flags.realized) {
1067 menu->flags.realized = 1;
1068 wMenuRealize(menu);
1070 if (!menu->flags.mapped) {
1071 if (wPreferences.wrap_menus) {
1072 if (x < rect.pos.x)
1073 x = rect.pos.x;
1074 if (y < rect.pos.y)
1075 y = rect.pos.y;
1076 if (x + MENUW(menu) > rect.pos.x + rect.size.width)
1077 x = rect.pos.x + rect.size.width - MENUW(menu);
1078 if (y + MENUH(menu) > rect.pos.y + rect.size.height)
1079 y = rect.pos.y + rect.size.height - MENUH(menu);
1082 XMoveWindow(dpy, menu->frame->core->window, x, y);
1083 menu->frame_x = x;
1084 menu->frame_y = y;
1085 XMapWindow(dpy, menu->frame->core->window);
1086 wRaiseFrame(menu->frame->core);
1087 menu->flags.mapped = 1;
1088 } else {
1089 selectEntry(menu, 0);
1092 if (keyboard)
1093 keyboardMenu(menu);
1096 void wMenuMap(WMenu * menu)
1098 if (!menu->flags.realized) {
1099 menu->flags.realized = 1;
1100 wMenuRealize(menu);
1102 if (menu->flags.app_menu && menu->parent == NULL) {
1103 menu->frame_x = menu->frame->screen_ptr->app_menu_x;
1104 menu->frame_y = menu->frame->screen_ptr->app_menu_y;
1105 XMoveWindow(dpy, menu->frame->core->window, menu->frame_x, menu->frame_y);
1107 XMapWindow(dpy, menu->frame->core->window);
1108 wRaiseFrame(menu->frame->core);
1109 menu->flags.mapped = 1;
1112 void wMenuUnmap(WMenu * menu)
1114 int i;
1116 XUnmapWindow(dpy, menu->frame->core->window);
1117 if (menu->flags.titled && menu->flags.buttoned) {
1118 wFrameWindowHideButton(menu->frame, WFF_RIGHT_BUTTON);
1120 menu->flags.buttoned = 0;
1121 menu->flags.mapped = 0;
1122 menu->flags.open_to_left = 0;
1124 for (i = 0; i < menu->cascade_no; i++) {
1125 if (menu->cascades[i] != NULL
1126 && menu->cascades[i]->flags.mapped && !menu->cascades[i]->flags.buttoned) {
1128 wMenuUnmap(menu->cascades[i]);
1131 menu->selected_entry = -1;
1134 void wMenuPaint(WMenu * menu)
1136 int i;
1138 if (!menu->flags.mapped) {
1139 return;
1142 /* paint entries */
1143 for (i = 0; i < menu->entry_no; i++) {
1144 paintEntry(menu, i, i == menu->selected_entry);
1148 void wMenuSetEnabled(WMenu * menu, int index, int enable)
1150 if (index >= menu->entry_no)
1151 return;
1152 menu->entries[index]->flags.enabled = enable;
1153 paintEntry(menu, index, index == menu->selected_entry);
1154 paintEntry(menu->brother, index, index == menu->selected_entry);
1158 static void selectEntry(WMenu * menu, int entry_no)
1160 WMenuEntry *entry;
1161 WMenu *submenu;
1162 int old_entry;
1164 if (menu->entries == NULL)
1165 return;
1167 if (entry_no >= menu->entry_no)
1168 return;
1170 old_entry = menu->selected_entry;
1171 menu->selected_entry = entry_no;
1173 if (old_entry != entry_no) {
1175 /* unselect previous entry */
1176 if (old_entry >= 0) {
1177 paintEntry(menu, old_entry, False);
1178 entry = menu->entries[old_entry];
1180 /* unmap cascade */
1181 if (entry->cascade >= 0 && menu->cascades) {
1182 if (!menu->cascades[entry->cascade]->flags.buttoned) {
1183 wMenuUnmap(menu->cascades[entry->cascade]);
1188 if (entry_no < 0) {
1189 menu->selected_entry = -1;
1190 return;
1192 entry = menu->entries[entry_no];
1194 if (entry->cascade >= 0 && menu->cascades && entry->flags.enabled) {
1195 /* Callback for when the submenu is opened.
1197 submenu = menu->cascades[entry->cascade];
1198 if (submenu && submenu->flags.brother)
1199 submenu = submenu->brother;
1201 if (entry->callback) {
1202 /* Only call the callback if the submenu is not yet mapped.
1204 if (menu->flags.brother) {
1205 if (!submenu || !submenu->flags.mapped)
1206 (*entry->callback) (menu->brother, entry);
1207 } else {
1208 if (!submenu || !submenu->flags.buttoned)
1209 (*entry->callback) (menu, entry);
1213 /* the submenu menu might have changed */
1214 submenu = menu->cascades[entry->cascade];
1216 /* map cascade */
1217 if (!submenu->flags.mapped) {
1218 int x, y;
1220 if (!submenu->flags.realized)
1221 wMenuRealize(submenu);
1222 if (wPreferences.wrap_menus) {
1223 if (menu->flags.open_to_left)
1224 submenu->flags.open_to_left = 1;
1226 if (submenu->flags.open_to_left) {
1227 x = menu->frame_x - MENUW(submenu);
1228 if (x < 0) {
1229 x = 0;
1230 submenu->flags.open_to_left = 0;
1232 } else {
1233 x = menu->frame_x + MENUW(menu);
1235 if (x + MENUW(submenu)
1236 >= menu->frame->screen_ptr->scr_width) {
1238 x = menu->frame_x - MENUW(submenu);
1239 submenu->flags.open_to_left = 1;
1242 } else {
1243 x = menu->frame_x + MENUW(menu);
1246 if (wPreferences.align_menus) {
1247 y = menu->frame_y;
1248 } else {
1249 y = menu->frame_y + menu->entry_height * entry_no;
1250 if (menu->flags.titled)
1251 y += menu->frame->top_width;
1252 if (menu->cascades[entry->cascade]->flags.titled)
1253 y -= menu->cascades[entry->cascade]->frame->top_width;
1256 wMenuMapAt(menu->cascades[entry->cascade], x, y, False);
1257 menu->cascades[entry->cascade]->parent = menu;
1258 } else {
1259 return;
1262 paintEntry(menu, entry_no, True);
1266 static WMenu *findMenu(WScreen * scr, int *x_ret, int *y_ret)
1268 WMenu *menu;
1269 WObjDescriptor *desc;
1270 Window root_ret, win, junk_win;
1271 int x, y, wx, wy;
1272 unsigned int mask;
1274 XQueryPointer(dpy, scr->root_win, &root_ret, &win, &x, &y, &wx, &wy, &mask);
1276 if (win == None)
1277 return NULL;
1279 if (XFindContext(dpy, win, wWinContext, (XPointer *) & desc) == XCNOENT)
1280 return NULL;
1282 if (desc->parent_type == WCLASS_MENU) {
1283 menu = (WMenu *) desc->parent;
1284 XTranslateCoordinates(dpy, root_ret, menu->menu->window, wx, wy, x_ret, y_ret, &junk_win);
1285 return menu;
1287 return NULL;
1290 static void closeCascade(WMenu * menu)
1292 WMenu *parent = menu->parent;
1294 if (menu->flags.brother || (!menu->flags.buttoned && (!menu->flags.app_menu || menu->parent != NULL))) {
1296 selectEntry(menu, -1);
1297 XSync(dpy, 0);
1298 #if (MENU_BLINK_DELAY > 2)
1299 wusleep(MENU_BLINK_DELAY / 2);
1300 #endif
1301 wMenuUnmap(menu);
1302 while (parent != NULL
1303 && (parent->parent != NULL || !parent->flags.app_menu || parent->flags.brother)
1304 && !parent->flags.buttoned) {
1305 selectEntry(parent, -1);
1306 wMenuUnmap(parent);
1307 parent = parent->parent;
1309 if (parent)
1310 selectEntry(parent, -1);
1314 static void closeBrotherCascadesOf(WMenu * menu)
1316 WMenu *tmp;
1317 int i;
1319 for (i = 0; i < menu->cascade_no; i++) {
1320 if (menu->cascades[i]->flags.brother) {
1321 tmp = menu->cascades[i];
1322 } else {
1323 tmp = menu->cascades[i]->brother;
1325 if (tmp->flags.mapped) {
1326 selectEntry(tmp->parent, -1);
1327 closeBrotherCascadesOf(tmp);
1328 break;
1333 #define getEntryAt(menu, x, y) ((y)<0 ? -1 : (y)/(menu->entry_height))
1335 static WMenu *parentMenu(WMenu * menu)
1337 WMenu *parent;
1338 WMenuEntry *entry;
1340 if (menu->flags.buttoned)
1341 return menu;
1343 while (menu->parent && menu->parent->flags.mapped) {
1344 parent = menu->parent;
1345 if (parent->selected_entry < 0)
1346 break;
1347 entry = parent->entries[parent->selected_entry];
1348 if (!entry->flags.enabled || entry->cascade < 0 || !parent->cascades ||
1349 parent->cascades[entry->cascade] != menu)
1350 break;
1351 menu = parent;
1352 if (menu->flags.buttoned)
1353 break;
1356 return menu;
1360 * Will raise the passed menu, if submenu = 0
1361 * If submenu > 0 will also raise all mapped submenus
1362 * until the first buttoned one
1363 * If submenu < 0 will also raise all mapped parent menus
1364 * until the first buttoned one
1367 static void raiseMenus(WMenu * menu, int submenus)
1369 WMenu *submenu;
1370 int i;
1372 if (!menu)
1373 return;
1375 wRaiseFrame(menu->frame->core);
1377 if (submenus > 0 && menu->selected_entry >= 0) {
1378 i = menu->entries[menu->selected_entry]->cascade;
1379 if (i >= 0 && menu->cascades) {
1380 submenu = menu->cascades[i];
1381 if (submenu->flags.mapped && !submenu->flags.buttoned)
1382 raiseMenus(submenu, submenus);
1385 if (submenus < 0 && !menu->flags.buttoned && menu->parent && menu->parent->flags.mapped)
1386 raiseMenus(menu->parent, submenus);
1389 WMenu *wMenuUnderPointer(WScreen * screen)
1391 WObjDescriptor *desc;
1392 Window root_ret, win;
1393 int dummy;
1394 unsigned int mask;
1396 XQueryPointer(dpy, screen->root_win, &root_ret, &win, &dummy, &dummy, &dummy, &dummy, &mask);
1398 if (win == None)
1399 return NULL;
1401 if (XFindContext(dpy, win, wWinContext, (XPointer *) & desc) == XCNOENT)
1402 return NULL;
1404 if (desc->parent_type == WCLASS_MENU)
1405 return (WMenu *) desc->parent;
1406 return NULL;
1409 static void getPointerPosition(WScreen * scr, int *x, int *y)
1411 Window root_ret, win;
1412 int wx, wy;
1413 unsigned int mask;
1415 XQueryPointer(dpy, scr->root_win, &root_ret, &win, x, y, &wx, &wy, &mask);
1418 static void getScrollAmount(WMenu * menu, int *hamount, int *vamount)
1420 WScreen *scr = menu->menu->screen_ptr;
1421 int menuX1 = menu->frame_x;
1422 int menuY1 = menu->frame_y;
1423 int menuX2 = menu->frame_x + MENUW(menu);
1424 int menuY2 = menu->frame_y + MENUH(menu);
1425 int xroot, yroot;
1426 WMRect rect = wGetRectForHead(scr, wGetHeadForPointerLocation(scr));
1428 *hamount = 0;
1429 *vamount = 0;
1431 getPointerPosition(scr, &xroot, &yroot);
1433 if (xroot <= (rect.pos.x + 1) && menuX1 < rect.pos.x) {
1434 /* scroll to the right */
1435 *hamount = WMIN(MENU_SCROLL_STEP, abs(menuX1));
1437 } else if (xroot >= (rect.pos.x + rect.size.width - 2) && menuX2 > (rect.pos.x + rect.size.width - 1)) {
1438 /* scroll to the left */
1439 *hamount = WMIN(MENU_SCROLL_STEP, abs(menuX2 - rect.pos.x - rect.size.width - 1));
1441 if (*hamount == 0)
1442 *hamount = 1;
1444 *hamount = -*hamount;
1447 if (yroot <= (rect.pos.y + 1) && menuY1 < rect.pos.y) {
1448 /* scroll down */
1449 *vamount = WMIN(MENU_SCROLL_STEP, abs(menuY1));
1451 } else if (yroot >= (rect.pos.y + rect.size.height - 2) && menuY2 > (rect.pos.y + rect.size.height - 1)) {
1452 /* scroll up */
1453 *vamount = WMIN(MENU_SCROLL_STEP, abs(menuY2 - rect.pos.y - rect.size.height - 2));
1455 *vamount = -*vamount;
1459 static void dragScrollMenuCallback(void *data)
1461 WMenu *menu = (WMenu *) data;
1462 WScreen *scr = menu->menu->screen_ptr;
1463 WMenu *parent = parentMenu(menu);
1464 int hamount, vamount;
1465 int x, y;
1466 int newSelectedEntry;
1468 getScrollAmount(menu, &hamount, &vamount);
1470 if (hamount != 0 || vamount != 0) {
1471 wMenuMove(parent, parent->frame_x + hamount, parent->frame_y + vamount, True);
1472 if (findMenu(scr, &x, &y)) {
1473 newSelectedEntry = getEntryAt(menu, x, y);
1474 selectEntry(menu, newSelectedEntry);
1475 } else {
1476 /* Pointer fell outside of menu. If the selected entry is
1477 * not a submenu, unselect it */
1478 if (menu->selected_entry >= 0 && menu->entries[menu->selected_entry]->cascade < 0)
1479 selectEntry(menu, -1);
1480 newSelectedEntry = 0;
1483 /* paranoid check */
1484 if (newSelectedEntry >= 0) {
1485 /* keep scrolling */
1486 menu->timer = WMAddTimerHandler(MENU_SCROLL_DELAY, dragScrollMenuCallback, menu);
1487 } else {
1488 menu->timer = NULL;
1490 } else {
1491 /* don't need to scroll anymore */
1492 menu->timer = NULL;
1493 if (findMenu(scr, &x, &y)) {
1494 newSelectedEntry = getEntryAt(menu, x, y);
1495 selectEntry(menu, newSelectedEntry);
1500 static void scrollMenuCallback(void *data)
1502 WMenu *menu = (WMenu *) data;
1503 WMenu *parent = parentMenu(menu);
1504 int hamount = 0; /* amount to scroll */
1505 int vamount = 0;
1507 getScrollAmount(menu, &hamount, &vamount);
1509 if (hamount != 0 || vamount != 0) {
1510 wMenuMove(parent, parent->frame_x + hamount, parent->frame_y + vamount, True);
1512 /* keep scrolling */
1513 menu->timer = WMAddTimerHandler(MENU_SCROLL_DELAY, scrollMenuCallback, menu);
1514 } else {
1515 /* don't need to scroll anymore */
1516 menu->timer = NULL;
1520 #define MENU_SCROLL_BORDER 5
1522 static int isPointNearBoder(WMenu * menu, int x, int y)
1524 int menuX1 = menu->frame_x;
1525 int menuY1 = menu->frame_y;
1526 int menuX2 = menu->frame_x + MENUW(menu);
1527 int menuY2 = menu->frame_y + MENUH(menu);
1528 int flag = 0;
1529 int head = wGetHeadForPoint(menu->frame->screen_ptr, wmkpoint(x, y));
1530 WMRect rect = wGetRectForHead(menu->frame->screen_ptr, head);
1532 /* XXX: handle screen joins properly !! */
1534 if (x >= menuX1 && x <= menuX2 &&
1535 (y < rect.pos.y + MENU_SCROLL_BORDER || y >= rect.pos.y + rect.size.height - MENU_SCROLL_BORDER))
1536 flag = 1;
1537 else if (y >= menuY1 && y <= menuY2 &&
1538 (x < rect.pos.x + MENU_SCROLL_BORDER || x >= rect.pos.x + rect.size.width - MENU_SCROLL_BORDER))
1539 flag = 1;
1541 return flag;
1544 typedef struct _delay {
1545 WMenu *menu;
1546 int ox, oy;
1547 } _delay;
1549 static void leaving(_delay * dl)
1551 wMenuMove(dl->menu, dl->ox, dl->oy, True);
1552 dl->menu->jump_back = NULL;
1553 dl->menu->menu->screen_ptr->flags.jump_back_pending = 0;
1554 wfree(dl);
1557 void wMenuScroll(WMenu * menu, XEvent * event)
1559 WMenu *smenu;
1560 WMenu *omenu = parentMenu(menu);
1561 WScreen *scr = menu->frame->screen_ptr;
1562 int done = 0;
1563 int jump_back = 0;
1564 int old_frame_x = omenu->frame_x;
1565 int old_frame_y = omenu->frame_y;
1566 XEvent ev;
1568 if (omenu->jump_back)
1569 WMDeleteTimerWithClientData(omenu->jump_back);
1571 if (( /*omenu->flags.buttoned && */ !wPreferences.wrap_menus)
1572 || omenu->flags.app_menu) {
1573 jump_back = 1;
1576 if (!wPreferences.wrap_menus)
1577 raiseMenus(omenu, True);
1578 else
1579 raiseMenus(menu, False);
1581 if (!menu->timer)
1582 scrollMenuCallback(menu);
1584 while (!done) {
1585 int x, y, on_border, on_x_edge, on_y_edge, on_title;
1586 WMRect rect;
1588 WMNextEvent(dpy, &ev);
1589 switch (ev.type) {
1590 case EnterNotify:
1591 WMHandleEvent(&ev);
1592 case MotionNotify:
1593 x = (ev.type == MotionNotify) ? ev.xmotion.x_root : ev.xcrossing.x_root;
1594 y = (ev.type == MotionNotify) ? ev.xmotion.y_root : ev.xcrossing.y_root;
1596 /* on_border is != 0 if the pointer is between the menu
1597 * and the screen border and is close enough to the border */
1598 on_border = isPointNearBoder(menu, x, y);
1600 smenu = wMenuUnderPointer(scr);
1602 if ((smenu == NULL && !on_border) || (smenu && parentMenu(smenu) != omenu)) {
1603 done = 1;
1604 break;
1607 rect = wGetRectForHead(scr, wGetHeadForPoint(scr, wmkpoint(x, y)));
1608 on_x_edge = x <= rect.pos.x + 1 || x >= rect.pos.x + rect.size.width - 2;
1609 on_y_edge = y <= rect.pos.y + 1 || y >= rect.pos.y + rect.size.height - 2;
1610 on_border = on_x_edge || on_y_edge;
1612 if (!on_border && !jump_back) {
1613 done = 1;
1614 break;
1617 if (menu->timer && (smenu != menu || (!on_y_edge && !on_x_edge))) {
1618 WMDeleteTimerHandler(menu->timer);
1619 menu->timer = NULL;
1622 if (smenu != NULL)
1623 menu = smenu;
1625 if (!menu->timer)
1626 scrollMenuCallback(menu);
1627 break;
1628 case ButtonPress:
1629 /* True if we push on title, or drag the omenu to other position */
1630 on_title = ev.xbutton.x_root >= omenu->frame_x &&
1631 ev.xbutton.x_root <= omenu->frame_x + MENUW(omenu) &&
1632 ev.xbutton.y_root >= omenu->frame_y &&
1633 ev.xbutton.y_root <= omenu->frame_y + omenu->frame->top_width;
1634 WMHandleEvent(&ev);
1635 smenu = wMenuUnderPointer(scr);
1636 if (smenu == NULL || (smenu && smenu->flags.buttoned && smenu != omenu))
1637 done = 1;
1638 else if (smenu == omenu && on_title) {
1639 jump_back = 0;
1640 done = 1;
1642 break;
1643 case KeyPress:
1644 done = 1;
1645 default:
1646 WMHandleEvent(&ev);
1647 break;
1651 if (menu->timer) {
1652 WMDeleteTimerHandler(menu->timer);
1653 menu->timer = NULL;
1656 if (jump_back) {
1657 _delay *delayer;
1658 if (!omenu->jump_back) {
1659 delayer = wmalloc(sizeof(_delay));
1660 delayer->menu = omenu;
1661 delayer->ox = old_frame_x;
1662 delayer->oy = old_frame_y;
1663 omenu->jump_back = delayer;
1664 scr->flags.jump_back_pending = 1;
1665 } else
1666 delayer = omenu->jump_back;
1667 WMAddTimerHandler(MENU_JUMP_BACK_DELAY, (WMCallback *) leaving, delayer);
1671 static void menuExpose(WObjDescriptor * desc, XEvent * 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 - 2; /* Entries "New" and "Destroy Last" appear before workspaces */
1767 name = wstrdup(scr->workspaces[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 wCursor[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 (scr->workspace_menu && scr->workspace_menu->flags.buttoned) {
2271 key = WMCreatePLString("WorkspaceMenu");
2272 saveMenuInfo(menus, scr->workspace_menu, key);
2273 WMReleasePropList(key);
2274 save_menus = 1;
2277 if (save_menus) {
2278 key = WMCreatePLString("Menus");
2279 WMPutInPLDictionary(scr->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, 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 (!scr->session_state) {
2483 return;
2486 key = WMCreatePLString("Menus");
2487 menus = WMGetFromPLDictionary(scr->session_state, key);
2488 WMReleasePropList(key);
2490 if (!menus)
2491 return;
2493 /* restore menus */
2495 skey = WMCreatePLString("SwitchMenu");
2496 menu = WMGetFromPLDictionary(menus, skey);
2497 WMReleasePropList(skey);
2498 restoreMenu(scr, menu, WSS_SWITCHMENU);
2500 if (!scr->root_menu) {
2501 OpenRootMenu(scr, scr->scr_width * 2, 0, False);
2502 wMenuUnmap(scr->root_menu);
2504 restoreMenuRecurs(scr, menus, scr->root_menu, "");