Code update for Window Maker version 0.50.0
[wmaker-crm.git] / src / menu.c
blobd285396a20e3c844a3defaf090d0470a81a1ca8b
1 /* menu.c- generic menu, used for root menu, application menus etc.
3 * Window Maker window manager
4 *
5 * Copyright (c) 1997, 1998 Alfredo K. Kojima
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
20 * 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 <unistd.h>
32 #include <ctype.h>
34 #include "WindowMaker.h"
35 #include "wcore.h"
36 #include "framewin.h"
37 #include "menu.h"
38 #include "actions.h"
39 #include "funcs.h"
40 #include "stacking.h"
41 #include "text.h"
44 /****** Global Variables ******/
46 extern Cursor wCursor[WCUR_LAST];
48 extern XContext wWinContext;
50 extern WPreferences wPreferences;
52 #define MOD_MASK wPreferences.modifier_mask
54 #define MENU_SCROLL_STEP menuScrollParameters[(int)wPreferences.menu_scroll_speed].steps
55 #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)
63 /***** Local Stuff ******/
65 static struct {
66 int steps;
67 int delay;
68 } menuScrollParameters[5] = {
69 {MENU_SCROLL_STEPS_UF, MENU_SCROLL_DELAY_UF},
70 {MENU_SCROLL_STEPS_F, MENU_SCROLL_DELAY_F},
71 {MENU_SCROLL_STEPS_M, MENU_SCROLL_DELAY_M},
72 {MENU_SCROLL_STEPS_S, MENU_SCROLL_DELAY_S},
73 {MENU_SCROLL_STEPS_U, MENU_SCROLL_DELAY_U}};
76 static void menuMouseDown(WObjDescriptor *desc, XEvent *event);
77 static void menuExpose(WObjDescriptor *desc, XEvent *event);
79 static void menuTitleDoubleClick(WCoreWindow *sender, void *data, XEvent *event);
80 static void menuTitleMouseDown(WCoreWindow *sender, void *data, XEvent *event);
82 static void menuCloseClick(WCoreWindow *sender, void *data, XEvent *event);
86 static void selectEntry(WMenu *menu, int entry_no);
87 static void closeCascade(WMenu *menu);
92 *----------------------------------------------------------------------
93 * wMenuCreate--
94 * Creates a new empty menu with the specified title. If main_menu
95 * is True, the created menu will be a main menu, which has some special
96 * properties such as being placed over other normal menus.
97 * If title is NULL, the menu will have no titlebar.
99 * Returns:
100 * The created menu.
101 *----------------------------------------------------------------------
103 WMenu*
104 wMenuCreate(WScreen *screen, char *title, int main_menu)
106 WMenu *menu;
107 static int brother=0;
108 int tmp, flags;
110 menu = wmalloc(sizeof(WMenu));
112 memset(menu, 0, sizeof(WMenu));
114 #ifdef SINGLE_MENULEVEL
115 tmp = WMSubmenuLevel;
116 #else
117 tmp = (main_menu ? WMMainMenuLevel : WMSubmenuLevel);
118 #endif
120 flags = WFF_SINGLE_STATE;
121 if (title) {
122 flags |= WFF_TITLEBAR|WFF_RIGHT_BUTTON;
123 menu->flags.titled = 1;
125 menu->frame =
126 wFrameWindowCreate(screen, tmp, 8, 2, 1, 1, flags,
127 screen->menu_title_texture, NULL,
128 screen->menu_title_pixel, &screen->menu_title_gc,
129 &screen->menu_title_font);
131 menu->frame->core->descriptor.parent = menu;
132 menu->frame->core->descriptor.parent_type = WCLASS_MENU;
133 menu->frame->core->descriptor.handle_mousedown = menuMouseDown;
135 wFrameWindowHideButton(menu->frame, WFF_RIGHT_BUTTON);
137 if (title) {
138 menu->frame->title = wstrdup(title);
141 menu->frame->flags.justification = WTJ_LEFT;
143 menu->frame->rbutton_image = screen->b_pixmaps[WBUT_CLOSE];
145 menu->entry_no = 0;
146 menu->alloced_entries = 0;
147 menu->selected_entry = -1;
148 menu->entries = NULL;
150 menu->frame_x = screen->app_menu_x;
151 menu->frame_y = screen->app_menu_y;
153 menu->frame->child = menu;
155 menu->flags.lowered = 0;
157 /* create borders */
158 if (title) {
159 /* setup object descriptors */
160 menu->frame->on_mousedown_titlebar = menuTitleMouseDown;
161 menu->frame->on_dblclick_titlebar = menuTitleDoubleClick;
164 menu->frame->on_click_right = menuCloseClick;
167 menu->menu = wCoreCreate(menu->frame->core, 0, menu->frame->top_width,
168 menu->frame->core->width, 10);
170 menu->menu->descriptor.parent = menu;
171 menu->menu->descriptor.parent_type = WCLASS_MENU;
172 menu->menu->descriptor.handle_expose = menuExpose;
173 menu->menu->descriptor.handle_mousedown = menuMouseDown;
175 menu->menu_texture_data = None;
177 XMapWindow(dpy, menu->menu->window);
179 XFlush(dpy);
181 if (!brother) {
182 brother = 1;
183 menu->brother = wMenuCreate(screen, title, main_menu);
184 brother = 0;
185 menu->brother->flags.brother = 1;
186 menu->brother->brother = menu;
190 return menu;
196 WMenu*
197 wMenuCreateForApp(WScreen *screen, char *title, int main_menu)
199 WMenu *menu;
201 menu = wMenuCreate(screen, title, main_menu);
202 if (!menu)
203 return NULL;
204 menu->flags.app_menu = 1;
205 menu->brother->flags.app_menu = 1;
207 return menu;
212 static void
213 insertEntry(WMenu *menu, WMenuEntry *entry, int index)
215 int i;
217 for (i = menu->entry_no-1; i >= index; i--) {
218 menu->entries[i]->order++;
219 menu->entries[i+1] = menu->entries[i];
221 menu->entries[index] = entry;
225 void
226 wMenuRefresh(WMenu *menu, int flags)
228 int i;
230 if (flags & MR_TEXT_BACK) {
231 menu->frame->flags.need_texture_remake = 1;
234 if (flags & (MR_RESIZED|MR_TITLE_TEXT)) {
235 menu->flags.realized = 0;
238 wMenuRealize(menu);
240 if (menu->flags.titled)
241 wFrameWindowPaint(menu->frame);
243 if (!menu->flags.brother) {
244 if (menu->brother)
245 wMenuRefresh(menu->brother, flags);
247 for (i=0; i < menu->cascade_no; i++) {
248 if (!menu->cascades[i]->flags.brother)
249 wMenuRefresh(menu->cascades[i], flags);
250 else
251 wMenuRefresh(menu->cascades[i]->brother, flags);
258 WMenuEntry*
259 wMenuInsertCallback(WMenu *menu, int index, char *text,
260 void (*callback)(WMenu *menu, WMenuEntry *entry),
261 void *clientdata)
263 WMenuEntry *entry;
265 #ifdef DEBUG
266 if (!menu) {
267 printf("Passed NULL as menu parameter to wMenuAddCallback() \n");
268 return NULL;
270 #endif
272 assert(menu->flags.brother==0);
273 menu->flags.realized = 0;
274 menu->brother->flags.realized = 0;
276 /* reallocate array if it's too small */
277 if (menu->entry_no >= menu->alloced_entries) {
278 void *tmp;
279 #ifdef DEBUG
280 puts("doing wrealloc()");
281 #endif
282 tmp = wrealloc(menu->entries,
283 sizeof(WMenuEntry)*(menu->alloced_entries+5));
284 if (tmp==NULL) {
285 wwarning(_("wrealloc() failed while trying to add menu item"));
286 return NULL;
289 menu->entries = tmp;
290 menu->alloced_entries += 5;
292 menu->brother->entries = tmp;
293 menu->brother->alloced_entries = menu->alloced_entries;
295 entry = wmalloc(sizeof(WMenuEntry));
296 memset(entry, 0, sizeof(WMenuEntry));
297 entry->flags.enabled = 1;
298 entry->text = wstrdup(text);
299 entry->cascade = -1;
300 entry->clientdata = clientdata;
301 entry->callback = callback;
302 if (index<0 || index>=menu->entry_no) {
303 entry->order = menu->entry_no;
304 menu->entries[menu->entry_no] = entry;
305 } else {
306 entry->order = index;
307 insertEntry(menu, entry, index);
310 menu->entry_no++;
311 menu->brother->entry_no = menu->entry_no;
313 return entry;
318 void
319 wMenuEntrySetCascade(WMenu *menu, WMenuEntry *entry, WMenu *cascade)
321 WMenu *brother = menu->brother;
322 int i, done;
324 assert(menu->flags.brother==0);
326 if (entry->cascade>=0) {
327 menu->flags.realized = 0;
328 brother->flags.realized = 0;
331 cascade->parent = menu;
333 cascade->brother->parent = brother;
335 done = 0;
336 for (i=0; i<menu->cascade_no; i++) {
337 if (menu->cascades[i]==NULL) {
338 menu->cascades[i] = cascade;
339 brother->cascades[i] = cascade->brother;
340 done = 1;
341 entry->cascade = i;
342 break;
345 if (!done) {
346 entry->cascade = menu->cascade_no;
348 menu->cascades = wrealloc(menu->cascades,
349 sizeof(WMenu)*(menu->cascade_no+1));
350 menu->cascades[menu->cascade_no++] = cascade;
353 brother->cascades = wrealloc(brother->cascades,
354 sizeof(WMenu)*(brother->cascade_no+1));
355 brother->cascades[brother->cascade_no++] = cascade->brother;
359 if (menu->flags.lowered) {
361 cascade->flags.lowered = 1;
362 ChangeStackingLevel(cascade->frame->core, WMNormalLevel);
364 cascade->brother->flags.lowered = 1;
365 ChangeStackingLevel(cascade->brother->frame->core, WMNormalLevel);
368 if (!menu->flags.realized)
369 wMenuRealize(menu);
373 void
374 wMenuEntryRemoveCascade(WMenu *menu, WMenuEntry *entry)
376 assert(menu->flags.brother==0);
378 /* destroy cascade menu */
379 if (entry->cascade>=0 && menu->cascades
380 && menu->cascades[entry->cascade]!=NULL) {
382 wMenuDestroy(menu->cascades[entry->cascade], True);
384 menu->cascades[entry->cascade] = NULL;
385 menu->brother->cascades[entry->cascade] = NULL;
387 entry->cascade = -1;
392 void
393 wMenuRemoveItem(WMenu *menu, int index)
395 int i;
397 if (menu->flags.brother) {
398 wMenuRemoveItem(menu->brother, index);
399 return;
402 if (index>=menu->entry_no) return;
404 /* destroy cascade menu */
405 wMenuEntryRemoveCascade(menu, menu->entries[index]);
407 /* destroy unshared data */
409 if (menu->entries[index]->text)
410 free(menu->entries[index]->text);
412 if (menu->entries[index]->rtext)
413 free(menu->entries[index]->rtext);
415 if (menu->entries[index]->free_cdata && menu->entries[index]->clientdata)
416 (*menu->entries[index]->free_cdata)(menu->entries[index]->clientdata);
418 free(menu->entries[index]);
420 for (i=index; i<menu->entry_no-1; i++) {
421 menu->entries[i+1]->order--;
422 menu->entries[i]=menu->entries[i+1];
424 menu->entry_no--;
425 menu->brother->entry_no--;
430 void
431 wMenuRealize(WMenu *menu)
433 int i;
434 int width, rwidth, mrwidth, mwidth;
435 int theight, twidth, eheight;
436 WScreen *scr = menu->frame->screen_ptr;
437 static int brother_done=0;
438 int flags;
440 if (!brother_done) {
441 brother_done = 1;
442 wMenuRealize(menu->brother);
443 brother_done = 0;
446 flags = WFF_SINGLE_STATE;
447 if (menu->flags.titled)
448 flags |= WFF_TITLEBAR|WFF_RIGHT_BUTTON;
450 wFrameWindowUpdateBorders(menu->frame, flags);
452 if (menu->flags.titled) {
453 twidth = wTextWidth(scr->menu_title_font->font, menu->frame->title,
454 strlen(menu->frame->title));
455 theight = menu->frame->top_width;
456 twidth += theight + theight/2 + (wPreferences.new_style ? 8 : 0);
457 } else {
458 twidth = 0;
459 theight = 0;
461 eheight = scr->menu_entry_font->height + 6;
462 menu->entry_height = eheight;
463 mrwidth = 0;
464 mwidth = 0;
465 for (i=0; i<menu->entry_no; i++) {
466 char *text;
468 /* search widest text */
469 text = menu->entries[i]->text;
470 width = wTextWidth(scr->menu_entry_font->font, text, strlen(text))+12;
472 if (menu->entries[i]->flags.indicator) {
473 width += MENU_INDICATOR_SPACE;
476 if (width > mwidth)
477 mwidth = width;
479 /* search widest text on right */
480 text = menu->entries[i]->rtext;
481 if (text)
482 rwidth = wTextWidth(scr->menu_entry_font->font, text,
483 strlen(text)) + 5;
484 else if (menu->entries[i]->cascade>=0)
485 rwidth = eheight;
486 else
487 rwidth = 4;
489 if (rwidth > mrwidth)
490 mrwidth = rwidth;
492 mwidth += mrwidth;
494 if (mwidth < twidth)
495 mwidth = twidth;
497 wCoreConfigure(menu->menu, 0, theight, mwidth, menu->entry_no*eheight -1);
499 wFrameWindowResize(menu->frame, mwidth, menu->entry_no*eheight-1
500 + menu->frame->top_width + menu->frame->bottom_width);
503 /* setup background texture */
504 if (scr->menu_item_texture->any.type != WTEX_SOLID) {
505 if (!menu->flags.brother) {
506 FREE_PIXMAP(menu->menu_texture_data);
508 wTextureRender(scr, scr->menu_item_texture,
509 &menu->menu_texture_data, menu->menu->width,
510 menu->entry_height, WREL_MENUENTRY);
512 XSetWindowBackgroundPixmap(dpy, menu->menu->window,
513 menu->menu_texture_data);
514 XClearWindow(dpy, menu->menu->window);
516 XSetWindowBackgroundPixmap(dpy, menu->brother->menu->window,
517 menu->menu_texture_data);
518 XClearWindow(dpy, menu->brother->menu->window);
520 } else {
521 XSetWindowBackground(dpy, menu->menu->window,
522 scr->menu_item_texture->any.color.pixel);
523 XClearWindow(dpy, menu->menu->window);
526 menu->flags.realized = 1;
528 if (menu->flags.mapped)
529 wMenuPaint(menu);
530 if (menu->brother->flags.mapped)
531 wMenuPaint(menu->brother);
535 void
536 wMenuDestroy(WMenu *menu, int recurse)
538 int i;
540 /* remove any pending timers */
541 if (menu->timer)
542 WMDeleteTimerHandler(menu->timer);
543 menu->timer = NULL;
545 /* call destroy handler */
546 if (menu->on_destroy)
547 (*menu->on_destroy)(menu);
549 /* Destroy items if this menu own them. If this is the "brother" menu,
550 * leave them alone as it is shared by them.
552 if (!menu->flags.brother) {
553 for (i=0; i<menu->entry_no; i++) {
555 free(menu->entries[i]->text);
557 if (menu->entries[i]->rtext)
558 free(menu->entries[i]->rtext);
560 if (menu->entries[i]->free_cdata && menu->entries[i]->clientdata) {
561 (*menu->entries[i]->free_cdata)(menu->entries[i]->clientdata);
563 free(menu->entries[i]);
566 if (recurse) {
567 for (i=0; i<menu->cascade_no; i++) {
568 if (menu->cascades[i]) {
569 if (menu->cascades[i]->flags.brother)
570 wMenuDestroy(menu->cascades[i]->brother, recurse);
571 else
572 wMenuDestroy(menu->cascades[i], recurse);
577 if (menu->entries)
578 free(menu->entries);
582 FREE_PIXMAP(menu->menu_texture_data);
584 if (menu->cascades)
585 free(menu->cascades);
587 wCoreDestroy(menu->menu);
588 wFrameWindowDestroy(menu->frame);
590 /* destroy copy of this menu */
591 if (!menu->flags.brother && menu->brother)
592 wMenuDestroy(menu->brother, False);
594 free(menu);
598 static void
599 drawFrame(WScreen *scr, Window win, int y, int w, int h)
601 XSegment segs[2];
603 segs[0].x1 = 0;
604 segs[0].y1 = y;
605 segs[0].x2 = w-1;
606 segs[0].y2 = y;
607 segs[1].x1 = 0;
608 segs[1].y1 = y;
609 segs[1].x2 = 0;
610 segs[1].y2 = y + h - 2;
611 XDrawSegments(dpy, win, scr->menu_item_auxtexture->light_gc, segs, 2);
613 XDrawLine(dpy, win, scr->menu_item_auxtexture->dark_gc, 0, y+h-1,
614 w-1, y+h-1);
616 segs[0].x1 = 1;
617 segs[0].y1 = segs[0].y2 = y + h-2;
618 segs[0].x2 = w-1;
619 segs[1].x1 = segs[1].x2 = w-1;
620 segs[1].y1 = y + 1;
621 segs[1].y2 = y + h-2;
622 XDrawSegments(dpy, win, scr->menu_item_auxtexture->dim_gc, segs, 2);
626 static void
627 paintEntry(WMenu *menu, int index, int selected)
629 int x, y, w, h, tw;
630 GC light, dim, dark, textGC;
631 WScreen *scr=menu->frame->screen_ptr;
632 Window win = menu->menu->window;
633 WMenuEntry *entry=menu->entries[index];
635 if (!menu->flags.realized) return;
636 h = menu->entry_height;
637 w = menu->menu->width;
638 y = index * h;
640 light = scr->menu_item_auxtexture->light_gc;
641 dim = scr->menu_item_auxtexture->dim_gc;
642 dark = scr->menu_item_auxtexture->dark_gc;
644 /* paint background */
645 if (selected) {
646 XSetForeground(dpy, scr->select_menu_gc, scr->select_pixel);
647 XFillRectangle(dpy, win, scr->select_menu_gc, 1, y+1, w-2, h-3);
648 if (scr->menu_item_texture->any.type == WTEX_SOLID)
649 drawFrame(scr, win, y, w, h);
650 } else {
651 if (scr->menu_item_texture->any.type == WTEX_SOLID) {
652 XClearArea(dpy, win, 0, y + 1, w - 1, h - 3, False);
653 /* draw the frame */
654 drawFrame(scr, win, y, w, h);
655 } else {
656 XClearArea(dpy, win, 0, y, w, h, False);
660 if (selected) {
661 textGC = scr->select_menu_gc;
662 if (entry->flags.enabled)
663 XSetForeground(dpy, textGC, scr->select_text_pixel);
664 else
665 XSetForeground(dpy, textGC, scr->dtext_pixel);
666 } else if (!entry->flags.enabled) {
667 textGC = scr->disabled_menu_entry_gc;
668 } else {
669 textGC = scr->menu_entry_gc;
671 /* draw text */
672 x = 5;
673 if (entry->flags.indicator)
674 x += MENU_INDICATOR_SPACE + 2;
676 wDrawString(win, scr->menu_entry_font,
677 textGC, x, 3+y+scr->menu_entry_font->y, entry->text,
678 strlen(entry->text));
680 if (entry->cascade>=0) {
681 /* draw the cascade indicator */
682 XDrawLine(dpy,win,dim, w-11, y+6, w-6, y+h/2-1);
683 XDrawLine(dpy,win,light, w-11, y+h-8, w-6, y+h/2-1);
684 XDrawLine(dpy,win,dark, w-12, y+6, w-12, y+h-8);
687 /* draw indicator */
688 if (entry->flags.indicator && entry->flags.indicator_on) {
689 int iw, ih;
690 WPixmap *indicator;
693 switch (entry->flags.indicator_type) {
694 case MI_CHECK:
695 indicator = scr->menu_check_indicator;
696 break;
697 case MI_MINIWINDOW:
698 indicator = scr->menu_mini_indicator;
699 break;
700 case MI_HIDDEN:
701 indicator = scr->menu_hide_indicator;
702 break;
703 case MI_SHADED:
704 indicator = scr->menu_shade_indicator;
705 break;
706 case MI_DIAMOND:
707 default:
708 indicator = scr->menu_radio_indicator;
709 break;
712 iw = indicator->width;
713 ih = indicator->height;
714 XSetClipMask(dpy, scr->copy_gc, indicator->mask);
715 XSetClipOrigin(dpy, scr->copy_gc, 5, y+(h-ih)/2);
716 if (selected)
717 XSetForeground(dpy, scr->copy_gc, scr->black_pixel);
718 else
719 XSetForeground(dpy, scr->copy_gc, scr->mtext_pixel);
720 XFillRectangle(dpy, win, scr->copy_gc, 5, y+(h-ih)/2, iw, ih);
722 XCopyArea(dpy, indicator->image, win, scr->copy_gc, 0, 0,
723 iw, ih, 5, y+(h-ih)/2);
725 XSetClipOrigin(dpy, scr->copy_gc, 0, 0);
728 /* draw right text */
730 if (entry->rtext && entry->cascade<0) {
731 tw = wTextWidth(scr->menu_entry_font->font, entry->rtext,
732 strlen(entry->rtext));
734 wDrawString(win, scr->menu_entry_font, textGC, w-6-tw,
735 3+y+scr->menu_entry_font->y, entry->rtext,
736 strlen(entry->rtext));
740 static void
741 move_menus(WMenu *menu, int x, int y)
743 while (menu->parent) {
744 menu = menu->parent;
745 x -= MENUW(menu);
746 if (!wPreferences.align_menus && menu->selected_entry>=0) {
747 y -= menu->selected_entry*menu->entry_height;
750 wMenuMove(menu, x, y, True);
753 static void
754 makeVisible(WMenu *menu)
756 WScreen *scr = menu->frame->screen_ptr;
757 int x1, y1, x2, y2, new_x, new_y, move;
759 if (menu->entry_no<0) return;
761 x1 = menu->frame_x;
762 y1 = menu->frame_y+menu->frame->top_width
763 + menu->selected_entry*menu->entry_height;
764 x2 = x1 + MENUW(menu);
765 y2 = y1 + menu->entry_height;
767 new_x = x1;
768 new_y = y1;
769 move = 0;
771 if (x1 < 0) {
772 new_x = 0;
773 move = 1;
774 } else if (x2 >= scr->scr_width) {
775 new_x = scr->scr_width - MENUW(menu) - 1;
776 move = 1;
779 if (y1 < 0) {
780 new_y = 0;
781 move = 1;
782 } else if (y2 >= scr->scr_height) {
783 new_y = scr->scr_height - menu->entry_height - 1;
784 move = 1;
787 new_y = new_y - menu->frame->top_width
788 - menu->selected_entry*menu->entry_height;
789 move_menus(menu, new_x, new_y);
793 static int
794 check_key(WMenu *menu, XKeyEvent *event)
796 int i, ch, s;
797 char buffer[32];
799 if (XLookupString(event, buffer, 32, NULL, NULL)<1)
800 return -1;
802 ch = toupper(buffer[0]);
804 s = (menu->selected_entry>=0 ? menu->selected_entry+1 : 0);
806 again:
807 for (i=s; i<menu->entry_no; i++) {
808 if (ch==toupper(menu->entries[i]->text[0])) {
809 return i;
812 /* no match. Retry from start, if previous started from a selected entry */
813 if (s!=0) {
814 s = 0;
815 goto again;
817 return -1;
821 static int
822 keyboardMenu(WMenu *menu)
824 XEvent event;
825 KeySym ksym=NoSymbol;
826 int done=0;
827 int index;
828 WMenuEntry *entry;
829 int old_pos_x = menu->frame_x;
830 int old_pos_y = menu->frame_y;
831 int new_x = old_pos_x, new_y = old_pos_y;
832 int scr_width = menu->frame->screen_ptr->scr_width;
833 int scr_height = menu->frame->screen_ptr->scr_height;
835 if (menu->flags.editing)
836 return False;
839 XGrabKeyboard(dpy, menu->frame->core->window, True, GrabModeAsync,
840 GrabModeAsync, CurrentTime);
842 if (menu->frame_y+menu->frame->top_width >= scr_height)
843 new_y = scr_height - menu->frame->top_width;
845 if (menu->frame_x+MENUW(menu) >= scr_width)
846 new_x = scr_width-MENUW(menu)-1;
848 move_menus(menu, new_x, new_y);
850 while (!done && menu->flags.mapped) {
851 XAllowEvents(dpy, AsyncKeyboard, CurrentTime);
852 WMMaskEvent(dpy, ExposureMask|ButtonMotionMask|ButtonPressMask
853 |ButtonReleaseMask|KeyPressMask|KeyReleaseMask
854 |SubstructureNotifyMask, &event);
856 switch (event.type) {
857 case KeyPress:
858 ksym = XLookupKeysym(&event.xkey, 0);
859 switch (ksym) {
860 case XK_Escape:
861 done = 1;
862 break;
864 case XK_Home:
865 case XK_KP_Home:
866 selectEntry(menu, 0);
867 makeVisible(menu);
868 break;
870 case XK_End:
871 case XK_KP_End:
872 selectEntry(menu, menu->entry_no-1);
873 makeVisible(menu);
874 break;
876 case XK_Up:
877 #ifdef ARROWLESS_KBD
878 case XK_k:
879 #endif
880 case XK_KP_Up:
881 if (menu->selected_entry <= 0)
882 selectEntry(menu, menu->entry_no-1);
883 else
884 selectEntry(menu, menu->selected_entry-1);
885 makeVisible(menu);
886 break;
888 case XK_Down:
889 #ifdef ARROWLESS_KBD
890 case XK_j:
891 #endif
892 case XK_KP_Down:
893 if (menu->selected_entry<0)
894 selectEntry(menu, 0);
895 else if (menu->selected_entry == menu->entry_no-1)
896 selectEntry(menu, 0);
897 else if (menu->selected_entry < menu->entry_no-1)
898 selectEntry(menu, menu->selected_entry+1);
899 makeVisible(menu);
900 break;
902 case XK_Right:
903 #ifdef ARROWLESS_KBD
904 case XK_l:
905 #endif
906 case XK_KP_Right:
907 if (menu->selected_entry>=0) {
908 WMenuEntry *entry;
909 entry = menu->entries[menu->selected_entry];
911 if (entry->cascade >= 0 && menu->cascades
912 && menu->cascades[entry->cascade]->entry_no > 0) {
914 XUngrabKeyboard(dpy, CurrentTime);
916 selectEntry(menu->cascades[entry->cascade], 0);
917 if (!keyboardMenu(menu->cascades[entry->cascade]))
918 done = 1;
920 XGrabKeyboard(dpy, menu->frame->core->window, True,
921 GrabModeAsync, GrabModeAsync,
922 CurrentTime);
925 break;
927 case XK_Left:
928 #ifdef ARROWLESS_KBD
929 case XK_h:
930 #endif
931 case XK_KP_Left:
932 if (menu->parent!=NULL && menu->parent->selected_entry>=0) {
933 selectEntry(menu, -1);
934 move_menus(menu, old_pos_x, old_pos_y);
935 return True;
937 break;
939 case XK_Return:
940 done = 2;
941 break;
943 default:
944 index = check_key(menu, &event.xkey);
945 if (index>=0) {
946 selectEntry(menu, index);
949 break;
951 default:
952 if (event.type==ButtonPress)
953 done = 1;
955 WMHandleEvent(&event);
959 XUngrabKeyboard(dpy, CurrentTime);
961 if (done==2 && menu->selected_entry>=0) {
962 entry = menu->entries[menu->selected_entry];
963 } else {
964 entry = NULL;
967 if (entry && entry->callback!=NULL && entry->flags.enabled
968 && entry->cascade < 0) {
969 #if (MENU_BLINK_COUNT > 0)
970 int sel = menu->selected_entry;
971 int i;
973 for (i=0; i<MENU_BLINK_COUNT; i++) {
974 paintEntry(menu, sel, False);
975 XSync(dpy, 0);
976 wusleep(MENU_BLINK_DELAY);
977 paintEntry(menu, sel, True);
978 XSync(dpy, 0);
979 wusleep(MENU_BLINK_DELAY);
981 #endif
982 selectEntry(menu, -1);
984 if (!menu->flags.buttoned) {
985 wMenuUnmap(menu);
986 move_menus(menu, old_pos_x, old_pos_y);
988 closeCascade(menu);
990 (*entry->callback)(menu, entry);
991 } else {
992 if (!menu->flags.buttoned) {
993 wMenuUnmap(menu);
994 move_menus(menu, old_pos_x, old_pos_y);
996 selectEntry(menu, -1);
1000 /* returns True if returning from a submenu to a parent menu,
1001 * False if exiting from menu */
1002 return False;
1006 void
1007 wMenuMapAt(WMenu *menu, int x, int y, int keyboard)
1009 int scr_width = menu->frame->screen_ptr->scr_width;
1010 int scr_height = menu->frame->screen_ptr->scr_height;
1012 if (!menu->flags.realized) {
1013 menu->flags.realized=1;
1014 wMenuRealize(menu);
1016 if (!menu->flags.mapped) {
1017 if (wPreferences.wrap_menus) {
1018 if (x<0) x = 0;
1019 if (y<0) y = 0;
1020 if (x+MENUW(menu) > scr_width)
1021 x = scr_width - MENUW(menu);
1022 if (y+MENUH(menu) > scr_height)
1023 y = scr_height - MENUH(menu);
1026 XMoveWindow(dpy, menu->frame->core->window, x, y);
1027 menu->frame_x = x;
1028 menu->frame_y = y;
1029 XMapWindow(dpy, menu->frame->core->window);
1030 wRaiseFrame(menu->frame->core);
1031 menu->flags.mapped = 1;
1032 } else {
1033 selectEntry(menu, 0);
1036 if (keyboard)
1037 keyboardMenu(menu);
1041 void
1042 wMenuMap(WMenu *menu)
1044 if (!menu->flags.realized) {
1045 menu->flags.realized=1;
1046 wMenuRealize(menu);
1048 if (menu->flags.app_menu && menu->parent==NULL) {
1049 menu->frame_x = menu->frame->screen_ptr->app_menu_x;
1050 menu->frame_y = menu->frame->screen_ptr->app_menu_y;
1051 XMoveWindow(dpy, menu->frame->core->window, menu->frame_x, menu->frame_y);
1053 XMapWindow(dpy, menu->frame->core->window);
1054 wRaiseFrame(menu->frame->core);
1055 menu->flags.mapped = 1;
1059 void
1060 wMenuUnmap(WMenu *menu)
1062 int i;
1064 XUnmapWindow(dpy, menu->frame->core->window);
1065 if (menu->flags.titled && menu->flags.buttoned) {
1066 wFrameWindowHideButton(menu->frame, WFF_RIGHT_BUTTON);
1068 menu->flags.buttoned = 0;
1069 menu->flags.mapped = 0;
1070 menu->flags.open_to_left = 0;
1072 for (i=0; i<menu->cascade_no; i++) {
1073 if (menu->cascades[i]!=NULL
1074 && menu->cascades[i]->flags.mapped
1075 && !menu->cascades[i]->flags.buttoned) {
1077 wMenuUnmap(menu->cascades[i]);
1080 menu->selected_entry = -1;
1085 void
1086 wMenuPaint(WMenu *menu)
1088 int i;
1090 if (!menu->flags.mapped) {
1091 return;
1094 /* paint entries */
1095 for (i=0; i<menu->entry_no; i++) {
1096 paintEntry(menu, i, i==menu->selected_entry);
1101 void
1102 wMenuSetEnabled(WMenu *menu, int index, int enable)
1104 if (index>=menu->entry_no) return;
1105 menu->entries[index]->flags.enabled=enable;
1106 paintEntry(menu, index, index==menu->selected_entry);
1107 paintEntry(menu->brother, index, index==menu->selected_entry);
1111 /* ====================================================================== */
1114 static void
1115 editEntry(WMenu *menu, WMenuEntry *entry)
1117 WTextInput *text;
1118 XEvent event;
1119 WObjDescriptor *desc;
1120 char *t;
1121 int done = 0;
1122 Window old_focus;
1123 int old_revert;
1125 menu->flags.editing = 1;
1127 text = wTextCreate(menu->menu, 1, menu->entry_height * entry->order,
1128 menu->menu->width - 2, menu->entry_height - 1);
1130 wTextPutText(text, entry->text);
1131 XGetInputFocus(dpy, &old_focus, &old_revert);
1132 XSetInputFocus(dpy, text->core->window, RevertToNone, CurrentTime);
1134 if (XGrabKeyboard(dpy, text->core->window, True, GrabModeAsync,
1135 GrabModeAsync, CurrentTime)!=GrabSuccess) {
1136 wwarning("could not grab keyboard");
1137 wTextDestroy(text);
1139 wSetFocusTo(menu->frame->screen_ptr,
1140 menu->frame->screen_ptr->focused_window);
1141 return;
1145 while (!done && !text->done) {
1146 XSync(dpy, 0);
1147 XAllowEvents(dpy, AsyncKeyboard|AsyncPointer, CurrentTime);
1148 XSync(dpy, 0);
1149 WMNextEvent(dpy, &event);
1151 if (XFindContext(dpy, event.xany.window, wWinContext,
1152 (XPointer *)&desc)==XCNOENT)
1153 desc = NULL;
1155 if ((desc != NULL) && (desc->handle_anything != NULL)) {
1157 (*desc->handle_anything)(desc, &event);
1159 } else {
1160 switch (event.type) {
1161 case ButtonPress:
1162 XAllowEvents(dpy, ReplayPointer, CurrentTime);
1163 done = 1;
1165 default:
1166 WMHandleEvent(&event);
1171 XSetInputFocus(dpy, old_focus, old_revert, CurrentTime);
1173 wSetFocusTo(menu->frame->screen_ptr,
1174 menu->frame->screen_ptr->focused_window);
1177 t = wTextGetText(text);
1178 /* if !t, the user has canceled editing */
1179 if (t) {
1180 if (entry->text)
1181 free(entry->text);
1182 entry->text = wstrdup(t);
1184 menu->flags.realized = 0;
1186 wTextDestroy(text);
1188 XUngrabKeyboard(dpy, CurrentTime);
1190 if (t && menu->on_edit)
1191 (*menu->on_edit)(menu, entry);
1193 menu->flags.editing = 0;
1195 if (!menu->flags.realized)
1196 wMenuRealize(menu);
1200 static void
1201 selectEntry(WMenu *menu, int entry_no)
1203 WMenuEntry *entry;
1204 WMenu *submenu;
1205 int old_entry;
1207 if (menu->entries==NULL)
1208 return;
1210 if (entry_no >= menu->entry_no)
1211 return;
1213 old_entry = menu->selected_entry;
1214 menu->selected_entry = entry_no;
1216 if (old_entry!=entry_no) {
1218 /* unselect previous entry */
1219 if (old_entry>=0) {
1220 paintEntry(menu, old_entry, False);
1221 entry = menu->entries[old_entry];
1223 /* unmap cascade */
1224 if (entry->cascade>=0 && menu->cascades) {
1225 if (!menu->cascades[entry->cascade]->flags.buttoned) {
1226 wMenuUnmap(menu->cascades[entry->cascade]);
1231 if (entry_no<0) {
1232 menu->selected_entry = -1;
1233 return;
1235 entry = menu->entries[entry_no];
1237 if (entry->cascade>=0 && menu->cascades && entry->flags.enabled) {
1238 /* Callback for when the submenu is opened.
1240 submenu = menu->cascades[entry->cascade];
1241 if (submenu && submenu->flags.brother)
1242 submenu = submenu->brother;
1244 if (entry->callback) {
1245 /* Only call the callback if the submenu is not yet mapped.
1247 if (menu->flags.brother) {
1248 if (!submenu || !submenu->flags.mapped)
1249 (*entry->callback)(menu->brother, entry);
1250 } else {
1251 if (!submenu || !submenu->flags.buttoned)
1252 (*entry->callback)(menu, entry);
1256 /* the submenu menu might have changed */
1257 submenu = menu->cascades[entry->cascade];
1259 /* map cascade */
1260 if (!submenu->flags.mapped) {
1261 int x, y;
1263 if (!submenu->flags.realized)
1264 wMenuRealize(submenu);
1265 if (wPreferences.wrap_menus) {
1266 if (menu->flags.open_to_left)
1267 submenu->flags.open_to_left = 1;
1269 if (submenu->flags.open_to_left) {
1270 x = menu->frame_x - MENUW(submenu);
1271 if (x<0) {
1272 x = 0;
1273 submenu->flags.open_to_left = 0;
1275 } else {
1276 x = menu->frame_x + MENUW(menu);
1278 if (x + MENUW(submenu)
1279 >= menu->frame->screen_ptr->scr_width) {
1281 x = menu->frame_x - MENUW(submenu);
1282 submenu->flags.open_to_left = 1;
1285 } else {
1286 x = menu->frame_x + MENUW(menu);
1289 if (wPreferences.align_menus) {
1290 y = menu->frame_y;
1291 } else {
1292 y = menu->frame_y + menu->entry_height*entry_no;
1293 if (menu->flags.titled)
1294 y += menu->frame->top_width;
1295 if (menu->cascades[entry->cascade]->flags.titled)
1296 y -= menu->cascades[entry->cascade]->frame->top_width;
1299 wMenuMapAt(menu->cascades[entry->cascade], x, y, False);
1300 menu->cascades[entry->cascade]->parent = menu;
1301 } else {
1302 return;
1305 paintEntry(menu, entry_no, True);
1310 static WMenu*
1311 findMenu(WScreen *scr, int *x_ret, int *y_ret)
1313 WMenu *menu;
1314 WObjDescriptor *desc;
1315 Window root_ret, win, junk_win;
1316 int x, y, wx, wy;
1317 unsigned int mask;
1319 XQueryPointer(dpy, scr->root_win, &root_ret, &win, &x, &y, &wx, &wy,
1320 &mask);
1322 if (win==None) return NULL;
1324 if (XFindContext(dpy, win, wWinContext, (XPointer *)&desc)==XCNOENT)
1325 return NULL;
1327 if (desc->parent_type == WCLASS_MENU) {
1328 menu = (WMenu*)desc->parent;
1329 XTranslateCoordinates(dpy, root_ret, menu->menu->window, wx, wy,
1330 x_ret, y_ret, &junk_win);
1331 return menu;
1333 return NULL;
1339 static void
1340 closeCascade(WMenu *menu)
1342 WMenu *parent=menu->parent;
1344 if (menu->flags.brother
1345 || (!menu->flags.buttoned
1346 && (!menu->flags.app_menu||menu->parent!=NULL))) {
1348 selectEntry(menu, -1);
1349 XSync(dpy, 0);
1350 #if (MENU_BLINK_DELAY > 2)
1351 wusleep(MENU_BLINK_DELAY/2);
1352 #endif
1353 wMenuUnmap(menu);
1354 while (parent!=NULL
1355 && (parent->parent!=NULL || !parent->flags.app_menu
1356 || parent->flags.brother)
1357 && !parent->flags.buttoned) {
1358 selectEntry(parent, -1);
1359 wMenuUnmap(parent);
1360 parent = parent->parent;
1362 if (parent)
1363 selectEntry(parent, -1);
1368 static void
1369 closeBrotherCascadesOf(WMenu *menu)
1371 WMenu *tmp;
1372 int i;
1374 for (i=0; i<menu->cascade_no; i++) {
1375 if (menu->cascades[i]->flags.brother) {
1376 tmp = menu->cascades[i];
1377 } else {
1378 tmp = menu->cascades[i]->brother;
1380 if (tmp->flags.mapped) {
1381 selectEntry(tmp->parent, -1);
1382 closeBrotherCascadesOf(tmp);
1383 break;
1389 #define getEntryAt(menu, x, y) ((y)<0 ? -1 : (y)/(menu->entry_height))
1392 static WMenu*
1393 parentMenu(WMenu *menu)
1395 WMenu *parent;
1396 WMenuEntry *entry;
1398 if (menu->flags.buttoned)
1399 return menu;
1401 while (menu->parent && menu->parent->flags.mapped) {
1402 parent = menu->parent;
1403 if (parent->selected_entry < 0)
1404 break;
1405 entry = parent->entries[parent->selected_entry];
1406 if (!entry->flags.enabled || entry->cascade<0 || !parent->cascades ||
1407 parent->cascades[entry->cascade] != menu)
1408 break;
1409 menu = parent;
1410 if (menu->flags.buttoned)
1411 break;
1414 return menu;
1420 * Will raise the passed menu, if submenu = 0
1421 * If submenu > 0 will also raise all mapped submenus
1422 * until the first buttoned one
1423 * If submenu < 0 will also raise all mapped parent menus
1424 * until the first buttoned one
1427 static void
1428 raiseMenus(WMenu *menu, int submenus)
1430 WMenu *submenu;
1431 int i;
1433 if(!menu) return;
1435 wRaiseFrame(menu->frame->core);
1437 if (submenus>0 && menu->selected_entry>=0) {
1438 i = menu->entries[menu->selected_entry]->cascade;
1439 if (i>=0 && menu->cascades) {
1440 submenu = menu->cascades[i];
1441 if (submenu->flags.mapped && !submenu->flags.buttoned)
1442 raiseMenus(submenu, submenus);
1445 if (submenus<0 && !menu->flags.buttoned &&
1446 menu->parent && menu->parent->flags.mapped)
1447 raiseMenus(menu->parent, submenus);
1451 WMenu*
1452 wMenuUnderPointer(WScreen *screen)
1454 WObjDescriptor *desc;
1455 Window root_ret, win;
1456 int dummy;
1457 unsigned int mask;
1459 XQueryPointer(dpy, screen->root_win, &root_ret, &win, &dummy, &dummy,
1460 &dummy, &dummy, &mask);
1462 if (win==None) return NULL;
1464 if (XFindContext(dpy, win, wWinContext, (XPointer *)&desc)==XCNOENT)
1465 return NULL;
1467 if (desc->parent_type == WCLASS_MENU)
1468 return (WMenu *)desc->parent;
1469 return NULL;
1474 #define MIN(a,b) (((a) > (b)) ? (b) : (a))
1477 static void
1478 getPointerPosition(WScreen *scr, int *x, int *y)
1480 Window root_ret, win;
1481 int wx, wy;
1482 unsigned int mask;
1484 XQueryPointer(dpy, scr->root_win, &root_ret, &win, x, y, &wx, &wy, &mask);
1488 static void
1489 getScrollAmount(WMenu *menu, int *hamount, int *vamount)
1491 WScreen *scr = menu->menu->screen_ptr;
1492 int menuX1 = menu->frame_x;
1493 int menuY1 = menu->frame_y;
1494 int menuX2 = menu->frame_x + MENUW(menu);
1495 int menuY2 = menu->frame_y + MENUH(menu);
1496 int screenW = scr->scr_width;
1497 int screenH = scr->scr_height;
1498 int xroot, yroot;
1500 *hamount = 0;
1501 *vamount = 0;
1503 getPointerPosition(scr, &xroot, &yroot);
1506 if (xroot <= 1 && menuX1 < 0) {
1507 /* scroll to the right */
1508 *hamount = MIN(MENU_SCROLL_STEP, abs(menuX1));
1510 } else if (xroot >= screenW-2 && menuX2 > screenW-1) {
1511 /* scroll to the left */
1512 *hamount = MIN(MENU_SCROLL_STEP, abs(menuX2-screenW-1));
1514 if (*hamount==0)
1515 *hamount = 1;
1517 *hamount = -*hamount;
1520 if (yroot <= 1 && menuY1 < 0) {
1521 /* scroll down */
1522 *vamount = MIN(MENU_SCROLL_STEP, abs(menuY1));
1524 } else if (yroot >= screenH-2 && menuY2 > screenH-1) {
1525 /* scroll up */
1526 *vamount = MIN(MENU_SCROLL_STEP, abs(menuY2-screenH-2));
1528 *vamount = -*vamount;
1533 static void
1534 dragScrollMenuCallback(void *data)
1536 WMenu *menu = (WMenu*)data;
1537 WScreen *scr = menu->menu->screen_ptr;
1538 WMenu *parent = parentMenu(menu);
1539 int hamount, vamount;
1540 int x, y;
1541 int newSelectedEntry;
1543 getScrollAmount(menu, &hamount, &vamount);
1546 if (hamount != 0 || vamount != 0) {
1547 wMenuMove(parent, parent->frame_x + hamount,
1548 parent->frame_y + vamount, True);
1549 if (findMenu(scr, &x, &y)) {
1550 newSelectedEntry = getEntryAt(menu, x, y);
1551 selectEntry(menu, newSelectedEntry);
1552 } else {
1553 /* Pointer fell outside of menu. If the selected entry is
1554 * not a submenu, unselect it */
1555 if (menu->selected_entry >= 0
1556 && menu->entries[menu->selected_entry]->cascade<0)
1557 selectEntry(menu, -1);
1558 newSelectedEntry = 0;
1561 /* paranoid check */
1562 if (newSelectedEntry >= 0) {
1563 /* keep scrolling */
1564 menu->timer = WMAddTimerHandler(MENU_SCROLL_DELAY,
1565 dragScrollMenuCallback, menu);
1566 } else {
1567 menu->timer = NULL;
1569 } else {
1570 /* don't need to scroll anymore */
1571 menu->timer = NULL;
1572 if (findMenu(scr, &x, &y)) {
1573 newSelectedEntry = getEntryAt(menu, x, y);
1574 selectEntry(menu, newSelectedEntry);
1580 static void
1581 scrollMenuCallback(void *data)
1583 WMenu *menu = (WMenu*)data;
1584 WMenu *parent = parentMenu(menu);
1585 int hamount = 0; /* amount to scroll */
1586 int vamount = 0;
1588 getScrollAmount(menu, &hamount, &vamount);
1590 if (hamount != 0 || vamount != 0) {
1591 wMenuMove(parent, parent->frame_x + hamount,
1592 parent->frame_y + vamount, True);
1594 /* keep scrolling */
1595 menu->timer = WMAddTimerHandler(MENU_SCROLL_DELAY,
1596 scrollMenuCallback, menu);
1597 } else {
1598 /* don't need to scroll anymore */
1599 menu->timer = NULL;
1605 #define MENU_SCROLL_BORDER 5
1607 static int
1608 isPointNearBoder(WMenu *menu, int x, int y)
1610 int menuX1 = menu->frame_x;
1611 int menuY1 = menu->frame_y;
1612 int menuX2 = menu->frame_x + MENUW(menu);
1613 int menuY2 = menu->frame_y + MENUH(menu);
1614 int scrXe = menu->menu->screen_ptr->scr_width-1;
1615 int scrYe = menu->menu->screen_ptr->scr_height-1;
1616 int flag = 0;
1618 if (x >= menuX1 && x <= menuX2 && (y < MENU_SCROLL_BORDER
1619 || y > scrYe-MENU_SCROLL_BORDER))
1620 flag = 1;
1621 else if (y >= menuY1 && y <= menuY2 && (x < MENU_SCROLL_BORDER
1622 || x > scrXe-MENU_SCROLL_BORDER))
1623 flag = 1;
1625 return flag;
1629 void
1630 wMenuScroll(WMenu *menu, XEvent *event)
1632 WMenu *smenu;
1633 WMenu *omenu = parentMenu(menu);
1634 WScreen *scr = menu->frame->screen_ptr;
1635 int done = 0;
1636 int jump_back = 0;
1637 int old_frame_x = omenu->frame_x;
1638 int old_frame_y = omenu->frame_y;
1639 XEvent ev;
1641 #ifdef DEBUG
1642 puts("Entering menu Scroll");
1643 #endif
1645 if ((/*omenu->flags.buttoned &&*/ !wPreferences.wrap_menus)
1646 || omenu->flags.app_menu) {
1647 jump_back = 1;
1650 if (!wPreferences.wrap_menus)
1651 raiseMenus(omenu, True);
1652 else
1653 raiseMenus(menu, False);
1655 if (!menu->timer)
1656 scrollMenuCallback(menu);
1658 while(!done) {
1659 int x, y, on_border, on_x_edge, on_y_edge, on_title;
1661 WMNextEvent(dpy, &ev);
1662 switch (ev.type) {
1663 case EnterNotify:
1664 WMHandleEvent(&ev);
1665 case MotionNotify:
1666 x = (ev.type==MotionNotify) ? ev.xmotion.x_root : ev.xcrossing.x_root;
1667 y = (ev.type==MotionNotify) ? ev.xmotion.y_root : ev.xcrossing.y_root;
1669 /* on_border is != 0 if the pointer is between the menu
1670 * and the screen border and is close enough to the border */
1671 on_border = isPointNearBoder(menu, x, y);
1673 smenu = wMenuUnderPointer(scr);
1675 if ((smenu==NULL && !on_border) || (smenu && parentMenu(smenu)!=omenu)) {
1676 done = 1;
1677 break;
1680 on_x_edge = x <= 1 || x >= scr->scr_width - 2;
1681 on_y_edge = y <= 1 || y >= scr->scr_height - 2;
1682 on_border = on_x_edge || on_y_edge;
1684 if (!on_border && !jump_back) {
1685 done = 1;
1686 break;
1689 if (menu->timer && (smenu!=menu || (!on_y_edge && !on_x_edge))) {
1690 WMDeleteTimerHandler(menu->timer);
1691 menu->timer = NULL;
1694 if (smenu != NULL)
1695 menu = smenu;
1697 if (!menu->timer)
1698 scrollMenuCallback(menu);
1699 break;
1700 case ButtonPress:
1701 /* True if we push on title, or drag the omenu to other position */
1702 on_title = ev.xbutton.x_root >= omenu->frame_x &&
1703 ev.xbutton.x_root <= omenu->frame_x + MENUW(omenu) &&
1704 ev.xbutton.y_root >= omenu->frame_y &&
1705 ev.xbutton.y_root <= omenu->frame_y + omenu->frame->top_width;
1706 WMHandleEvent(&ev);
1707 smenu = wMenuUnderPointer(scr);
1708 if (smenu == NULL || (smenu && smenu->flags.buttoned && smenu != omenu))
1709 done = 1;
1710 else if (smenu==omenu && on_title) {
1711 jump_back = 0;
1712 done = 1;
1714 break;
1715 case KeyPress:
1716 done = 1;
1717 default:
1718 WMHandleEvent(&ev);
1719 break;
1723 if (menu->timer) {
1724 WMDeleteTimerHandler(menu->timer);
1725 menu->timer = NULL;
1728 if (jump_back)
1729 wMenuMove(omenu, old_frame_x, old_frame_y, True);
1731 #ifdef DEBUG
1732 puts("Leaving menu Scroll");
1733 #endif
1738 static void
1739 menuExpose(WObjDescriptor *desc, XEvent *event)
1741 wMenuPaint(desc->parent);
1744 typedef struct {
1745 int *delayed_select;
1746 WMenu *menu;
1747 WMHandlerID magic;
1748 } delay_data;
1751 static void
1752 delaySelection(void *data)
1754 delay_data *d = (delay_data*)data;
1755 int x, y, entry_no;
1756 WMenu *menu;
1758 d->magic = NULL;
1760 menu = findMenu(d->menu->menu->screen_ptr, &x, &y);
1761 if (menu && (d->menu == menu || d->delayed_select)) {
1762 entry_no = getEntryAt(menu, x, y);
1763 selectEntry(menu, entry_no);
1765 if (d->delayed_select)
1766 *(d->delayed_select) = 0;
1770 static void
1771 menuMouseDown(WObjDescriptor *desc, XEvent *event)
1773 XButtonEvent *bev = &event->xbutton;
1774 WMenu *menu = desc->parent;
1775 WMenu *smenu;
1776 WScreen *scr=menu->frame->screen_ptr;
1777 WMenuEntry *entry=NULL;
1778 XEvent ev;
1779 int close_on_exit=0;
1780 int done=0;
1781 int delayed_select = 0;
1782 int entry_no;
1783 int x, y;
1784 int prevx, prevy;
1785 int old_frame_x = 0;
1786 int old_frame_y = 0;
1787 delay_data d_data = {NULL, NULL, NULL};
1789 if (!wPreferences.wrap_menus) {
1790 smenu = parentMenu(menu);
1791 old_frame_x = smenu->frame_x;
1792 old_frame_y = smenu->frame_y;
1794 else if (event->xbutton.window == menu->frame->core->window) {
1795 /* This is true if the menu was launched with right click on root window */
1796 delayed_select = 1;
1797 d_data.delayed_select = &delayed_select;
1798 d_data.menu = menu;
1799 d_data.magic = WMAddTimerHandler(wPreferences.dblclick_time,
1800 delaySelection, &d_data);
1803 wRaiseFrame(menu->frame->core);
1805 close_on_exit = (bev->send_event || menu->flags.brother);
1807 smenu = findMenu(scr, &x, &y);
1808 if (!smenu) {
1809 x = -1;
1810 y = -1;
1811 } else {
1812 menu = smenu;
1815 if (menu->flags.editing) {
1816 return;
1818 entry_no = getEntryAt(menu, x, y);
1819 if (entry_no>=0) {
1820 entry = menu->entries[entry_no];
1822 if (!close_on_exit && (bev->state & ControlMask) && smenu
1823 && entry->flags.editable) {
1824 editEntry(smenu, entry);
1825 return;
1826 } else if (bev->state & ControlMask) {
1827 return;
1830 if (entry->flags.enabled && entry->cascade>=0 && menu->cascades) {
1831 WMenu *submenu = menu->cascades[entry->cascade];
1832 /* map cascade */
1833 if (submenu->flags.mapped && !submenu->flags.buttoned &&
1834 menu->selected_entry!=entry_no) {
1835 wMenuUnmap(submenu);
1837 if (!submenu->flags.mapped && !delayed_select) {
1838 selectEntry(menu, entry_no);
1839 } else if (!submenu->flags.buttoned) {
1840 selectEntry(menu, -1);
1843 } else if (!delayed_select) {
1844 selectEntry(menu, entry_no);
1847 if (!wPreferences.wrap_menus && !wPreferences.scrollable_menus) {
1848 if (!menu->timer)
1849 dragScrollMenuCallback(menu);
1853 prevx = bev->x_root;
1854 prevy = bev->y_root;
1855 while (!done) {
1856 int x, y;
1858 XAllowEvents(dpy, SyncPointer, CurrentTime);
1860 WMMaskEvent(dpy, ExposureMask|ButtonMotionMask|ButtonReleaseMask
1861 |ButtonPressMask, &ev);
1862 switch (ev.type) {
1863 case MotionNotify:
1864 smenu = findMenu(scr, &x, &y);
1866 if (smenu == NULL) {
1867 /* moved mouse out of menu */
1869 if (!delayed_select && d_data.magic) {
1870 WMDeleteTimerHandler(d_data.magic);
1871 d_data.magic = NULL;
1873 if (menu==NULL
1874 || (menu->selected_entry>=0
1875 && menu->entries[menu->selected_entry]->cascade>=0)) {
1876 prevx = ev.xmotion.x_root;
1877 prevy = ev.xmotion.y_root;
1879 break;
1881 selectEntry(menu, -1);
1882 menu = smenu;
1883 prevx = ev.xmotion.x_root;
1884 prevy = ev.xmotion.y_root;
1885 break;
1886 } else if (menu && menu!=smenu
1887 && (menu->selected_entry<0
1888 || menu->entries[menu->selected_entry]->cascade<0)) {
1889 selectEntry(menu, -1);
1891 if (!delayed_select && d_data.magic) {
1892 WMDeleteTimerHandler(d_data.magic);
1893 d_data.magic = NULL;
1895 } else {
1897 /* hysteresis for item selection */
1899 /* check if the motion was to the side, indicating that
1900 * the user may want to cross to a submenu */
1901 if (!delayed_select && menu) {
1902 int dx;
1903 Bool moved_to_submenu;/* moved to direction of submenu */
1905 dx = abs(prevx - ev.xmotion.x_root);
1907 moved_to_submenu = False;
1908 if (dx > 0 /* if moved enough to the side */
1909 /* maybe a open submenu */
1910 && menu->selected_entry>=0
1911 /* moving to the right direction */
1912 && (wPreferences.align_menus
1913 || ev.xmotion.y_root >= prevy)) {
1914 int index;
1916 index = menu->entries[menu->selected_entry]->cascade;
1917 if (index>=0) {
1918 if (menu->cascades[index]->frame_x>menu->frame_x) {
1919 if (prevx < ev.xmotion.x_root)
1920 moved_to_submenu = True;
1921 } else {
1922 if (prevx > ev.xmotion.x_root)
1923 moved_to_submenu = True;
1929 if (menu != smenu) {
1930 if (d_data.magic) {
1931 WMDeleteTimerHandler(d_data.magic);
1933 d_data.magic = NULL;
1934 } else if (moved_to_submenu) {
1935 /* while we are moving, postpone the selection */
1936 if (d_data.magic) {
1937 WMDeleteTimerHandler(d_data.magic);
1939 d_data.delayed_select = NULL;
1940 d_data.menu = menu;
1941 d_data.magic = WMAddTimerHandler(MENU_SELECT_DELAY,
1942 delaySelection,
1943 &d_data);
1944 prevx = ev.xmotion.x_root;
1945 prevy = ev.xmotion.y_root;
1946 break;
1947 } else {
1948 if (d_data.magic)
1949 WMDeleteTimerHandler(d_data.magic);
1950 d_data.magic = NULL;
1954 prevx = ev.xmotion.x_root;
1955 prevy = ev.xmotion.y_root;
1956 if (menu!=smenu) {
1957 /* pointer crossed menus */
1958 if (menu && menu->timer) {
1959 WMDeleteTimerHandler(menu->timer);
1960 menu->timer = NULL;
1962 if (smenu)
1963 dragScrollMenuCallback(smenu);
1965 menu = smenu;
1966 if (!menu->timer)
1967 dragScrollMenuCallback(menu);
1969 if (!delayed_select) {
1970 entry_no = getEntryAt(menu, x, y);
1971 if (entry_no>=0) {
1972 entry = menu->entries[entry_no];
1973 if (entry->flags.enabled && entry->cascade>=0 &&
1974 menu->cascades) {
1975 WMenu *submenu = menu->cascades[entry->cascade];
1976 if (submenu->flags.mapped && !submenu->flags.buttoned
1977 && menu->selected_entry!=entry_no) {
1978 wMenuUnmap(submenu);
1982 selectEntry(menu, entry_no);
1984 break;
1986 case ButtonPress:
1987 break;
1989 case ButtonRelease:
1990 if (ev.xbutton.button == event->xbutton.button)
1991 done=1;
1992 break;
1994 case Expose:
1995 WMHandleEvent(&ev);
1996 break;
2000 if (menu && menu->timer) {
2001 WMDeleteTimerHandler(menu->timer);
2002 menu->timer = NULL;
2004 if (d_data.magic!=NULL)
2005 WMDeleteTimerHandler(d_data.magic);
2007 if (menu && menu->selected_entry>=0) {
2008 entry = menu->entries[menu->selected_entry];
2009 if (entry->callback!=NULL && entry->flags.enabled
2010 && entry->cascade < 0) {
2011 /* blink and erase menu selection */
2012 #if (MENU_BLINK_DELAY > 0)
2013 int sel = menu->selected_entry;
2014 int i;
2016 for (i=0; i<MENU_BLINK_COUNT; i++) {
2017 paintEntry(menu, sel, False);
2018 XSync(dpy, 0);
2019 wusleep(MENU_BLINK_DELAY);
2020 paintEntry(menu, sel, True);
2021 XSync(dpy, 0);
2022 wusleep(MENU_BLINK_DELAY);
2024 #endif
2025 /* unmap the menu, it's parents and call the callback */
2026 if (!menu->flags.buttoned &&
2027 (!menu->flags.app_menu||menu->parent!=NULL)) {
2028 closeCascade(menu);
2029 } else {
2030 selectEntry(menu, -1);
2032 (*entry->callback)(menu, entry);
2034 /* If the user double clicks an entry, the entry will
2035 * be executed twice, which is not good for things like
2036 * the root menu. So, ignore any clicks that were generated
2037 * while the entry was being executed */
2038 while (XCheckTypedWindowEvent(dpy, menu->menu->window,
2039 ButtonPress, &ev));
2040 } else if (entry->callback!=NULL && entry->cascade<0) {
2041 selectEntry(menu, -1);
2042 } else {
2043 if (entry->cascade>=0 && menu->cascades
2044 && menu->cascades[entry->cascade]->flags.brother) {
2045 selectEntry(menu, -1);
2050 if (((WMenu*)desc->parent)->flags.brother || close_on_exit
2051 || !smenu)
2052 closeCascade(desc->parent);
2054 /* close the cascade windows that should not remain opened */
2055 closeBrotherCascadesOf(desc->parent);
2057 if (!wPreferences.wrap_menus)
2058 wMenuMove(parentMenu(desc->parent), old_frame_x, old_frame_y, True);
2062 void
2063 wMenuMove(WMenu *menu, int x, int y, int submenus)
2065 WMenu *submenu;
2066 int i;
2068 if (!menu) return;
2070 menu->frame_x = x;
2071 menu->frame_y = y;
2072 XMoveWindow(dpy, menu->frame->core->window, x, y);
2074 if (submenus>0 && menu->selected_entry>=0) {
2075 i = menu->entries[menu->selected_entry]->cascade;
2077 if (i>=0 && menu->cascades) {
2078 submenu = menu->cascades[i];
2079 if (submenu->flags.mapped && !submenu->flags.buttoned)
2080 if (wPreferences.align_menus) {
2081 wMenuMove(submenu, x + MENUW(menu), y, submenus);
2082 } else {
2083 wMenuMove(submenu, x+ MENUW(menu),
2084 y + submenu->entry_height*menu->selected_entry,
2085 submenus);
2089 if (submenus<0 && menu->parent!=NULL && menu->parent->flags.mapped &&
2090 !menu->parent->flags.buttoned) {
2091 if (wPreferences.align_menus) {
2092 wMenuMove(menu->parent, x - MENUW(menu->parent), y, submenus);
2093 } else {
2094 wMenuMove(menu->parent, x - MENUW(menu->parent), menu->frame_y
2095 - menu->parent->entry_height*menu->parent->selected_entry,
2096 submenus);
2102 static void
2103 changeMenuLevels(WMenu *menu, int lower)
2105 int i;
2107 if (!lower) {
2108 ChangeStackingLevel(menu->frame->core, (!menu->parent ? WMMainMenuLevel
2109 : WMSubmenuLevel));
2110 wRaiseFrame(menu->frame->core);
2111 menu->flags.lowered = 0;
2112 } else {
2113 ChangeStackingLevel(menu->frame->core, WMNormalLevel);
2114 wLowerFrame(menu->frame->core);
2115 menu->flags.lowered = 1;
2117 for (i=0; i<menu->cascade_no; i++) {
2118 if (menu->cascades[i]
2119 && !menu->cascades[i]->flags.buttoned
2120 && menu->cascades[i]->flags.lowered!=lower) {
2121 changeMenuLevels(menu->cascades[i], lower);
2128 static void
2129 menuTitleDoubleClick(WCoreWindow *sender, void *data, XEvent *event)
2131 WMenu *menu = data;
2132 int lower;
2134 if (event->xbutton.state & MOD_MASK) {
2135 if (menu->flags.lowered) {
2136 lower = 0;
2137 } else {
2138 lower = 1;
2140 changeMenuLevels(menu, lower);
2145 static void
2146 menuTitleMouseDown(WCoreWindow *sender, void *data, XEvent *event)
2148 WMenu *menu = data;
2149 WMenu *tmp;
2150 XEvent ev;
2151 int x=menu->frame_x, y=menu->frame_y;
2152 int dx=event->xbutton.x_root, dy=event->xbutton.y_root;
2153 int i, lower;
2154 Bool started;
2156 #ifdef DEBUG
2157 printf("Moving menu\n");
2158 #endif
2160 /* can't touch the menu copy */
2161 if (menu->flags.brother)
2162 return;
2164 if (event->xbutton.button != Button1 && event->xbutton.button != Button2)
2165 return;
2166 if (XGrabPointer(dpy, menu->frame->titlebar->window, False,
2167 ButtonMotionMask|ButtonReleaseMask|ButtonPressMask,
2168 GrabModeAsync, GrabModeAsync, None,
2169 wCursor[WCUR_MOVE], CurrentTime)!=GrabSuccess) {
2170 #ifdef DEBUG0
2171 wwarning("pointer grab failed for menu move\n");
2172 #endif
2173 return;
2176 if (event->xbutton.state & MOD_MASK) {
2177 wLowerFrame(menu->frame->core);
2178 lower = 1;
2179 } else {
2180 wRaiseFrame(menu->frame->core);
2181 lower = 0;
2183 tmp = menu;
2185 /* lower/raise all submenus */
2186 while (1) {
2187 if (tmp->selected_entry>=0 && tmp->cascades
2188 && tmp->entries[tmp->selected_entry]->cascade>=0) {
2189 tmp = tmp->cascades[tmp->entries[tmp->selected_entry]->cascade];
2190 if (!tmp || !tmp->flags.mapped)
2191 break;
2192 if (lower)
2193 wLowerFrame(tmp->frame->core);
2194 else
2195 wRaiseFrame(tmp->frame->core);
2196 } else {
2197 break;
2201 /* tear off the menu if it's a root menu or a cascade
2202 application menu */
2203 if (!menu->flags.buttoned && !menu->flags.brother
2204 && (!menu->flags.app_menu||menu->parent!=NULL)) {
2205 menu->flags.buttoned=1;
2206 wFrameWindowShowButton(menu->frame, WFF_RIGHT_BUTTON);
2207 if (menu->parent) {
2208 /* turn off selected menu entry in parent menu */
2209 selectEntry(menu->parent, -1);
2211 /* make parent map the copy in place of the original */
2212 for (i=0; i<menu->parent->cascade_no; i++) {
2213 if (menu->parent->cascades[i] == menu) {
2214 menu->parent->cascades[i] = menu->brother;
2215 break;
2221 started = False;
2222 while(1) {
2223 WMMaskEvent(dpy, ButtonMotionMask|ButtonReleaseMask|ButtonPressMask
2224 |ExposureMask, &ev);
2225 switch (ev.type) {
2226 case MotionNotify:
2227 if (started) {
2228 x += ev.xmotion.x_root - dx;
2229 y += ev.xmotion.y_root - dy;
2230 dx = ev.xmotion.x_root;
2231 dy = ev.xmotion.y_root;
2232 wMenuMove(menu, x, y, True);
2233 } else {
2234 if (abs(ev.xmotion.x_root - dx) > MOVE_THRESHOLD
2235 || abs(ev.xmotion.y_root - dy) > MOVE_THRESHOLD)
2236 started = True;
2238 break;
2240 case ButtonPress:
2241 break;
2243 case ButtonRelease:
2244 if (ev.xbutton.button != event->xbutton.button)
2245 break;
2246 #ifdef DEBUG
2247 printf("End menu move\n");
2248 #endif
2249 XUngrabPointer(dpy, CurrentTime);
2250 return;
2252 default:
2253 WMHandleEvent(&ev);
2254 break;
2260 *----------------------------------------------------------------------
2261 * menuCloseClick--
2262 * Handles mouse click on the close button of menus. The menu is
2263 * closed when the button is clicked.
2265 * Side effects:
2266 * The closed menu is reinserted at it's parent menus
2267 * cascade list.
2268 *----------------------------------------------------------------------
2270 static void
2271 menuCloseClick(WCoreWindow *sender, void *data, XEvent *event)
2273 WMenu *menu = (WMenu*)data;
2274 WMenu *parent = menu->parent;
2275 int i;
2277 if (parent) {
2278 for (i=0; i<parent->cascade_no; i++) {
2279 /* find the entry that points to the copy */
2280 if (parent->cascades[i] == menu->brother) {
2281 /* make it point to the original */
2282 parent->cascades[i] = menu;
2283 menu->parent = parent;
2284 break;
2288 wMenuUnmap(menu);
2292 void
2293 wMenuSaveState(WScreen *scr)
2295 proplist_t menus, key, value;
2296 int save_menus = 0;
2297 char buffer[256];
2299 menus = PLMakeDictionaryFromEntries(NULL, NULL, NULL);
2301 #ifndef LITE
2302 if (scr->root_menu && scr->root_menu->flags.buttoned) {
2303 sprintf(buffer, "%i,%i", scr->root_menu->frame_x,
2304 scr->root_menu->frame_y);
2305 key = PLMakeString("RootMenu");
2306 value = PLMakeString(buffer);
2307 PLInsertDictionaryEntry(menus, key, value);
2308 PLRelease(key);
2309 PLRelease(value);
2310 save_menus = 1;
2313 if (scr->switch_menu && scr->switch_menu->flags.buttoned) {
2314 sprintf(buffer, "%i,%i", scr->switch_menu->frame_x,
2315 scr->switch_menu->frame_y);
2316 key = PLMakeString("SwitchMenu");
2317 value = PLMakeString(buffer);
2318 PLInsertDictionaryEntry(menus, key, value);
2319 PLRelease(key);
2320 PLRelease(value);
2321 save_menus = 1;
2323 #endif /* !LITE */
2324 if (scr->workspace_menu && scr->workspace_menu->flags.buttoned) {
2325 sprintf(buffer, "%i,%i", scr->workspace_menu->frame_x,
2326 scr->workspace_menu->frame_y);
2327 key = PLMakeString("WorkspaceMenu");
2328 value = PLMakeString(buffer);
2329 PLInsertDictionaryEntry(menus, key, value);
2330 PLRelease(key);
2331 PLRelease(value);
2332 save_menus = 1;
2335 if (save_menus) {
2336 key = PLMakeString("Menus");
2337 PLInsertDictionaryEntry(scr->session_state, key, menus);
2338 PLRelease(key);
2340 PLRelease(menus);
2344 #define COMPLAIN(key) wwarning(_("bad value in menus state info:%s"), key)
2347 static int
2348 restoreMenu(WScreen *scr, proplist_t menu, int which)
2350 int i, x, y;
2351 WMenu *pmenu = NULL;
2353 if (!menu)
2354 return False;
2356 if (!PLIsString(menu)) {
2357 COMPLAIN("Position");
2358 return False;
2361 if (sscanf(PLGetString(menu), "%i,%i", &x, &y)!=2)
2362 COMPLAIN("Position");
2364 #ifndef LITE
2365 if (which & WSS_ROOTMENU) {
2366 OpenRootMenu(scr, x, y, False);
2367 pmenu = scr->root_menu;
2368 } else if (which & WSS_SWITCHMENU) {
2369 OpenSwitchMenu(scr, x, y, False);
2370 pmenu = scr->switch_menu;
2371 } else
2372 #endif /* !LITE */
2373 if (which & WSS_WSMENU) {
2374 OpenWorkspaceMenu(scr, x, y);
2375 pmenu = scr->workspace_menu;
2376 if (pmenu->parent) {
2377 /* make parent map the copy in place of the original */
2378 for (i=0; i<pmenu->parent->cascade_no; i++) {
2379 if (pmenu->parent->cascades[i] == pmenu) {
2380 pmenu->parent->cascades[i] = pmenu->brother;
2381 break;
2387 if (pmenu) {
2388 int width = MENUW(pmenu);
2389 int height = MENUH(pmenu);
2391 x = (x < -width) ? 0 : x;
2392 x = (x > scr->scr_width) ? scr->scr_width - width : x;
2393 y = (y < 0) ? 0 : y;
2394 y = (y > scr->scr_height) ? scr->scr_height - height : y;
2395 wMenuMove(pmenu, x, y, True);
2396 pmenu->flags.buttoned = 1;
2397 wFrameWindowShowButton(pmenu->frame, WFF_RIGHT_BUTTON);
2398 return True;
2400 return False;
2404 void
2405 wMenuRestoreState(WScreen *scr)
2407 proplist_t menus, menu, key, rkey, skey, wkey;
2409 key = PLMakeString("Menus");
2410 menus = PLGetDictionaryEntry(scr->session_state, key);
2411 PLRelease(key);
2413 if (!menus)
2414 return;
2416 /* restore menus */
2418 rkey = PLMakeString("RootMenu");
2419 skey = PLMakeString("SwitchMenu");
2420 wkey = PLMakeString("WorkspaceMenu");
2421 menu = PLGetDictionaryEntry(menus, rkey);
2422 restoreMenu(scr, menu, WSS_ROOTMENU);
2423 menu = PLGetDictionaryEntry(menus, skey);
2424 restoreMenu(scr, menu, WSS_SWITCHMENU);
2425 menu = PLGetDictionaryEntry(menus, wkey);
2426 restoreMenu(scr, menu, WSS_WSMENU);
2428 PLRelease(rkey);
2429 PLRelease(skey);
2430 PLRelease(wkey);
2434 void
2435 OpenWorkspaceMenu(WScreen *scr, int x, int y)
2437 WMenu *menu, *parent;
2438 WMenuEntry *entry;
2440 #ifndef LITE
2441 if (!scr->root_menu) {
2442 OpenRootMenu(scr, scr->scr_width*2, 0, False);
2443 wMenuUnmap(scr->root_menu);
2445 #endif
2446 menu = scr->workspace_menu;
2447 if (menu) {
2448 if (menu->flags.mapped) {
2449 if (!menu->flags.buttoned) {
2450 wMenuUnmap(menu);
2451 parent = menu->parent;
2452 if (parent && parent->selected_entry >= 0) {
2453 entry = parent->entries[parent->selected_entry];
2454 if (parent->cascades[entry->cascade] == menu) {
2455 selectEntry(parent, -1);
2456 wMenuMapAt(menu, x, y, False);
2459 } else {
2460 wRaiseFrame(menu->frame->core);
2461 wMenuMapCopyAt(menu, x, y);
2464 else {
2465 wMenuMapAt(menu, x, y, False);