KDE enhancements, menu appearance enhancement
[wmaker-crm.git] / src / menu.c
blob6c81022b92a74e74fa8ead56c7b30b102d8ab197
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 + (wPreferences.new_style ? 16 : 8);
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))+10;
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 = 16;
486 else
487 rwidth = 4;
489 if (rwidth > mrwidth)
490 mrwidth = rwidth;
492 mwidth += mrwidth;
494 if (mwidth < twidth)
495 mwidth = twidth;
498 wCoreConfigure(menu->menu, 0, theight, mwidth, menu->entry_no*eheight -1);
500 wFrameWindowResize(menu->frame, mwidth, menu->entry_no*eheight-1
501 + menu->frame->top_width + menu->frame->bottom_width);
504 /* setup background texture */
505 if (scr->menu_item_texture->any.type != WTEX_SOLID) {
506 if (!menu->flags.brother) {
507 FREE_PIXMAP(menu->menu_texture_data);
509 wTextureRender(scr, scr->menu_item_texture,
510 &menu->menu_texture_data, menu->menu->width,
511 menu->entry_height, WREL_MENUENTRY);
513 XSetWindowBackgroundPixmap(dpy, menu->menu->window,
514 menu->menu_texture_data);
515 XClearWindow(dpy, menu->menu->window);
517 XSetWindowBackgroundPixmap(dpy, menu->brother->menu->window,
518 menu->menu_texture_data);
519 XClearWindow(dpy, menu->brother->menu->window);
521 } else {
522 XSetWindowBackground(dpy, menu->menu->window,
523 scr->menu_item_texture->any.color.pixel);
524 XClearWindow(dpy, menu->menu->window);
527 menu->flags.realized = 1;
529 if (menu->flags.mapped)
530 wMenuPaint(menu);
531 if (menu->brother->flags.mapped)
532 wMenuPaint(menu->brother);
536 void
537 wMenuDestroy(WMenu *menu, int recurse)
539 int i;
541 /* remove any pending timers */
542 if (menu->timer)
543 WMDeleteTimerHandler(menu->timer);
544 menu->timer = NULL;
546 /* call destroy handler */
547 if (menu->on_destroy)
548 (*menu->on_destroy)(menu);
550 /* Destroy items if this menu own them. If this is the "brother" menu,
551 * leave them alone as it is shared by them.
553 if (!menu->flags.brother) {
554 for (i=0; i<menu->entry_no; i++) {
556 free(menu->entries[i]->text);
558 if (menu->entries[i]->rtext)
559 free(menu->entries[i]->rtext);
561 if (menu->entries[i]->free_cdata && menu->entries[i]->clientdata) {
562 (*menu->entries[i]->free_cdata)(menu->entries[i]->clientdata);
564 free(menu->entries[i]);
567 if (recurse) {
568 for (i=0; i<menu->cascade_no; i++) {
569 if (menu->cascades[i]) {
570 if (menu->cascades[i]->flags.brother)
571 wMenuDestroy(menu->cascades[i]->brother, recurse);
572 else
573 wMenuDestroy(menu->cascades[i], recurse);
578 if (menu->entries)
579 free(menu->entries);
583 FREE_PIXMAP(menu->menu_texture_data);
585 if (menu->cascades)
586 free(menu->cascades);
588 wCoreDestroy(menu->menu);
589 wFrameWindowDestroy(menu->frame);
591 /* destroy copy of this menu */
592 if (!menu->flags.brother && menu->brother)
593 wMenuDestroy(menu->brother, False);
595 free(menu);
599 static void
600 drawFrame(WScreen *scr, Window win, int y, int w, int h)
602 XSegment segs[2];
604 segs[0].x1 = 0;
605 segs[0].y1 = y;
606 segs[0].x2 = w-1;
607 segs[0].y2 = y;
608 segs[1].x1 = 0;
609 segs[1].y1 = y;
610 segs[1].x2 = 0;
611 segs[1].y2 = y + h - 2;
612 XDrawSegments(dpy, win, scr->menu_item_auxtexture->light_gc, segs, 2);
614 XDrawLine(dpy, win, scr->menu_item_auxtexture->dark_gc, 0, y+h-1,
615 w-1, y+h-1);
617 segs[0].x1 = 1;
618 segs[0].y1 = segs[0].y2 = y + h-2;
619 segs[0].x2 = w-1;
620 segs[1].x1 = segs[1].x2 = w-1;
621 segs[1].y1 = y + 1;
622 segs[1].y2 = y + h-2;
623 XDrawSegments(dpy, win, scr->menu_item_auxtexture->dim_gc, segs, 2);
627 static void
628 paintEntry(WMenu *menu, int index, int selected)
630 int x, y, w, h, tw;
631 GC light, dim, dark, textGC;
632 WScreen *scr=menu->frame->screen_ptr;
633 Window win = menu->menu->window;
634 WMenuEntry *entry=menu->entries[index];
636 if (!menu->flags.realized) return;
637 h = menu->entry_height;
638 w = menu->menu->width;
639 y = index * h;
641 light = scr->menu_item_auxtexture->light_gc;
642 dim = scr->menu_item_auxtexture->dim_gc;
643 dark = scr->menu_item_auxtexture->dark_gc;
645 /* paint background */
646 if (selected) {
647 XSetForeground(dpy, scr->select_menu_gc, scr->select_pixel);
648 XFillRectangle(dpy, win, scr->select_menu_gc, 1, y+1, w-2, h-3);
649 if (scr->menu_item_texture->any.type == WTEX_SOLID)
650 drawFrame(scr, win, y, w, h);
651 } else {
652 if (scr->menu_item_texture->any.type == WTEX_SOLID) {
653 XClearArea(dpy, win, 0, y + 1, w - 1, h - 3, False);
654 /* draw the frame */
655 drawFrame(scr, win, y, w, h);
656 } else {
657 XClearArea(dpy, win, 0, y, w, h, False);
661 if (selected) {
662 textGC = scr->select_menu_gc;
663 if (entry->flags.enabled)
664 XSetForeground(dpy, textGC, scr->select_text_pixel);
665 else
666 XSetForeground(dpy, textGC, scr->dtext_pixel);
667 } else if (!entry->flags.enabled) {
668 textGC = scr->disabled_menu_entry_gc;
669 } else {
670 textGC = scr->menu_entry_gc;
672 /* draw text */
673 x = 5;
674 if (entry->flags.indicator)
675 x += MENU_INDICATOR_SPACE + 2;
677 wDrawString(win, scr->menu_entry_font,
678 textGC, x, 3+y+scr->menu_entry_font->y, entry->text,
679 strlen(entry->text));
681 if (entry->cascade>=0) {
682 /* draw the cascade indicator */
683 XDrawLine(dpy,win,dim, w-11, y+6, w-6, y+h/2-1);
684 XDrawLine(dpy,win,light, w-11, y+h-8, w-6, y+h/2-1);
685 XDrawLine(dpy,win,dark, w-12, y+6, w-12, y+h-8);
688 /* draw indicator */
689 if (entry->flags.indicator && entry->flags.indicator_on) {
690 int iw, ih;
691 WPixmap *indicator;
694 switch (entry->flags.indicator_type) {
695 case MI_CHECK:
696 indicator = scr->menu_check_indicator;
697 break;
698 case MI_MINIWINDOW:
699 indicator = scr->menu_mini_indicator;
700 break;
701 case MI_HIDDEN:
702 indicator = scr->menu_hide_indicator;
703 break;
704 case MI_SHADED:
705 indicator = scr->menu_shade_indicator;
706 break;
707 case MI_DIAMOND:
708 default:
709 indicator = scr->menu_radio_indicator;
710 break;
713 iw = indicator->width;
714 ih = indicator->height;
715 XSetClipMask(dpy, scr->copy_gc, indicator->mask);
716 XSetClipOrigin(dpy, scr->copy_gc, 5, y+(h-ih)/2);
717 if (selected)
718 XSetForeground(dpy, scr->copy_gc, scr->black_pixel);
719 else
720 XSetForeground(dpy, scr->copy_gc, scr->mtext_pixel);
721 XFillRectangle(dpy, win, scr->copy_gc, 5, y+(h-ih)/2, iw, ih);
723 XCopyArea(dpy, indicator->image, win, scr->copy_gc, 0, 0,
724 iw, ih, 5, y+(h-ih)/2);
726 XSetClipOrigin(dpy, scr->copy_gc, 0, 0);
729 /* draw right text */
731 if (entry->rtext && entry->cascade<0) {
732 tw = wTextWidth(scr->menu_entry_font->font, entry->rtext,
733 strlen(entry->rtext));
735 wDrawString(win, scr->menu_entry_font, textGC, w-6-tw,
736 3+y+scr->menu_entry_font->y, entry->rtext,
737 strlen(entry->rtext));
741 static void
742 move_menus(WMenu *menu, int x, int y)
744 while (menu->parent) {
745 menu = menu->parent;
746 x -= MENUW(menu);
747 if (!wPreferences.align_menus && menu->selected_entry>=0) {
748 y -= menu->selected_entry*menu->entry_height;
751 wMenuMove(menu, x, y, True);
754 static void
755 makeVisible(WMenu *menu)
757 WScreen *scr = menu->frame->screen_ptr;
758 int x1, y1, x2, y2, new_x, new_y, move;
760 if (menu->entry_no<0) return;
762 x1 = menu->frame_x;
763 y1 = menu->frame_y+menu->frame->top_width
764 + menu->selected_entry*menu->entry_height;
765 x2 = x1 + MENUW(menu);
766 y2 = y1 + menu->entry_height;
768 new_x = x1;
769 new_y = y1;
770 move = 0;
772 if (x1 < 0) {
773 new_x = 0;
774 move = 1;
775 } else if (x2 >= scr->scr_width) {
776 new_x = scr->scr_width - MENUW(menu) - 1;
777 move = 1;
780 if (y1 < 0) {
781 new_y = 0;
782 move = 1;
783 } else if (y2 >= scr->scr_height) {
784 new_y = scr->scr_height - menu->entry_height - 1;
785 move = 1;
788 new_y = new_y - menu->frame->top_width
789 - menu->selected_entry*menu->entry_height;
790 move_menus(menu, new_x, new_y);
794 static int
795 check_key(WMenu *menu, XKeyEvent *event)
797 int i, ch, s;
798 char buffer[32];
800 if (XLookupString(event, buffer, 32, NULL, NULL)<1)
801 return -1;
803 ch = toupper(buffer[0]);
805 s = (menu->selected_entry>=0 ? menu->selected_entry+1 : 0);
807 again:
808 for (i=s; i<menu->entry_no; i++) {
809 if (ch==toupper(menu->entries[i]->text[0])) {
810 return i;
813 /* no match. Retry from start, if previous started from a selected entry */
814 if (s!=0) {
815 s = 0;
816 goto again;
818 return -1;
822 static int
823 keyboardMenu(WMenu *menu)
825 XEvent event;
826 KeySym ksym=NoSymbol;
827 int done=0;
828 int index;
829 WMenuEntry *entry;
830 int old_pos_x = menu->frame_x;
831 int old_pos_y = menu->frame_y;
832 int new_x = old_pos_x, new_y = old_pos_y;
833 int scr_width = menu->frame->screen_ptr->scr_width;
834 int scr_height = menu->frame->screen_ptr->scr_height;
836 if (menu->flags.editing)
837 return False;
840 XGrabKeyboard(dpy, menu->frame->core->window, True, GrabModeAsync,
841 GrabModeAsync, CurrentTime);
843 if (menu->frame_y+menu->frame->top_width >= scr_height)
844 new_y = scr_height - menu->frame->top_width;
846 if (menu->frame_x+MENUW(menu) >= scr_width)
847 new_x = scr_width-MENUW(menu)-1;
849 move_menus(menu, new_x, new_y);
851 while (!done && menu->flags.mapped) {
852 XAllowEvents(dpy, AsyncKeyboard, CurrentTime);
853 WMMaskEvent(dpy, ExposureMask|ButtonMotionMask|ButtonPressMask
854 |ButtonReleaseMask|KeyPressMask|KeyReleaseMask
855 |SubstructureNotifyMask, &event);
857 switch (event.type) {
858 case KeyPress:
859 ksym = XLookupKeysym(&event.xkey, 0);
860 switch (ksym) {
861 case XK_Escape:
862 done = 1;
863 break;
865 case XK_Home:
866 case XK_KP_Home:
867 selectEntry(menu, 0);
868 makeVisible(menu);
869 break;
871 case XK_End:
872 case XK_KP_End:
873 selectEntry(menu, menu->entry_no-1);
874 makeVisible(menu);
875 break;
877 case XK_Up:
878 #ifdef ARROWLESS_KBD
879 case XK_k:
880 #endif
881 case XK_KP_Up:
882 if (menu->selected_entry <= 0)
883 selectEntry(menu, menu->entry_no-1);
884 else
885 selectEntry(menu, menu->selected_entry-1);
886 makeVisible(menu);
887 break;
889 case XK_Down:
890 #ifdef ARROWLESS_KBD
891 case XK_j:
892 #endif
893 case XK_KP_Down:
894 if (menu->selected_entry<0)
895 selectEntry(menu, 0);
896 else if (menu->selected_entry == menu->entry_no-1)
897 selectEntry(menu, 0);
898 else if (menu->selected_entry < menu->entry_no-1)
899 selectEntry(menu, menu->selected_entry+1);
900 makeVisible(menu);
901 break;
903 case XK_Right:
904 #ifdef ARROWLESS_KBD
905 case XK_l:
906 #endif
907 case XK_KP_Right:
908 if (menu->selected_entry>=0) {
909 WMenuEntry *entry;
910 entry = menu->entries[menu->selected_entry];
912 if (entry->cascade >= 0 && menu->cascades
913 && menu->cascades[entry->cascade]->entry_no > 0) {
915 XUngrabKeyboard(dpy, CurrentTime);
917 selectEntry(menu->cascades[entry->cascade], 0);
918 if (!keyboardMenu(menu->cascades[entry->cascade]))
919 done = 1;
921 XGrabKeyboard(dpy, menu->frame->core->window, True,
922 GrabModeAsync, GrabModeAsync,
923 CurrentTime);
926 break;
928 case XK_Left:
929 #ifdef ARROWLESS_KBD
930 case XK_h:
931 #endif
932 case XK_KP_Left:
933 if (menu->parent!=NULL && menu->parent->selected_entry>=0) {
934 selectEntry(menu, -1);
935 move_menus(menu, old_pos_x, old_pos_y);
936 return True;
938 break;
940 case XK_Return:
941 done = 2;
942 break;
944 default:
945 index = check_key(menu, &event.xkey);
946 if (index>=0) {
947 selectEntry(menu, index);
950 break;
952 default:
953 if (event.type==ButtonPress)
954 done = 1;
956 WMHandleEvent(&event);
960 XUngrabKeyboard(dpy, CurrentTime);
962 if (done==2 && menu->selected_entry>=0) {
963 entry = menu->entries[menu->selected_entry];
964 } else {
965 entry = NULL;
968 if (entry && entry->callback!=NULL && entry->flags.enabled
969 && entry->cascade < 0) {
970 #if (MENU_BLINK_COUNT > 0)
971 int sel = menu->selected_entry;
972 int i;
974 for (i=0; i<MENU_BLINK_COUNT; i++) {
975 paintEntry(menu, sel, False);
976 XSync(dpy, 0);
977 wusleep(MENU_BLINK_DELAY);
978 paintEntry(menu, sel, True);
979 XSync(dpy, 0);
980 wusleep(MENU_BLINK_DELAY);
982 #endif
983 selectEntry(menu, -1);
985 if (!menu->flags.buttoned) {
986 wMenuUnmap(menu);
987 move_menus(menu, old_pos_x, old_pos_y);
989 closeCascade(menu);
991 (*entry->callback)(menu, entry);
992 } else {
993 if (!menu->flags.buttoned) {
994 wMenuUnmap(menu);
995 move_menus(menu, old_pos_x, old_pos_y);
997 selectEntry(menu, -1);
1001 /* returns True if returning from a submenu to a parent menu,
1002 * False if exiting from menu */
1003 return False;
1007 void
1008 wMenuMapAt(WMenu *menu, int x, int y, int keyboard)
1010 int scr_width = menu->frame->screen_ptr->scr_width;
1011 int scr_height = menu->frame->screen_ptr->scr_height;
1013 if (!menu->flags.realized) {
1014 menu->flags.realized=1;
1015 wMenuRealize(menu);
1017 if (!menu->flags.mapped) {
1018 if (wPreferences.wrap_menus) {
1019 if (x<0) x = 0;
1020 if (y<0) y = 0;
1021 if (x+MENUW(menu) > scr_width)
1022 x = scr_width - MENUW(menu);
1023 if (y+MENUH(menu) > scr_height)
1024 y = scr_height - MENUH(menu);
1027 XMoveWindow(dpy, menu->frame->core->window, x, y);
1028 menu->frame_x = x;
1029 menu->frame_y = y;
1030 XMapWindow(dpy, menu->frame->core->window);
1031 wRaiseFrame(menu->frame->core);
1032 menu->flags.mapped = 1;
1033 } else {
1034 selectEntry(menu, 0);
1037 if (keyboard)
1038 keyboardMenu(menu);
1042 void
1043 wMenuMap(WMenu *menu)
1045 if (!menu->flags.realized) {
1046 menu->flags.realized=1;
1047 wMenuRealize(menu);
1049 if (menu->flags.app_menu && menu->parent==NULL) {
1050 menu->frame_x = menu->frame->screen_ptr->app_menu_x;
1051 menu->frame_y = menu->frame->screen_ptr->app_menu_y;
1052 XMoveWindow(dpy, menu->frame->core->window, menu->frame_x, menu->frame_y);
1054 XMapWindow(dpy, menu->frame->core->window);
1055 wRaiseFrame(menu->frame->core);
1056 menu->flags.mapped = 1;
1060 void
1061 wMenuUnmap(WMenu *menu)
1063 int i;
1065 XUnmapWindow(dpy, menu->frame->core->window);
1066 if (menu->flags.titled && menu->flags.buttoned) {
1067 wFrameWindowHideButton(menu->frame, WFF_RIGHT_BUTTON);
1069 menu->flags.buttoned = 0;
1070 menu->flags.mapped = 0;
1071 menu->flags.open_to_left = 0;
1073 for (i=0; i<menu->cascade_no; i++) {
1074 if (menu->cascades[i]!=NULL
1075 && menu->cascades[i]->flags.mapped
1076 && !menu->cascades[i]->flags.buttoned) {
1078 wMenuUnmap(menu->cascades[i]);
1081 menu->selected_entry = -1;
1086 void
1087 wMenuPaint(WMenu *menu)
1089 int i;
1091 if (!menu->flags.mapped) {
1092 return;
1095 /* paint entries */
1096 for (i=0; i<menu->entry_no; i++) {
1097 paintEntry(menu, i, i==menu->selected_entry);
1102 void
1103 wMenuSetEnabled(WMenu *menu, int index, int enable)
1105 if (index>=menu->entry_no) return;
1106 menu->entries[index]->flags.enabled=enable;
1107 paintEntry(menu, index, index==menu->selected_entry);
1108 paintEntry(menu->brother, index, index==menu->selected_entry);
1112 /* ====================================================================== */
1115 static void
1116 editEntry(WMenu *menu, WMenuEntry *entry)
1118 WTextInput *text;
1119 XEvent event;
1120 WObjDescriptor *desc;
1121 char *t;
1122 int done = 0;
1123 Window old_focus;
1124 int old_revert;
1126 menu->flags.editing = 1;
1128 text = wTextCreate(menu->menu, 1, menu->entry_height * entry->order,
1129 menu->menu->width - 2, menu->entry_height - 1);
1131 wTextPutText(text, entry->text);
1132 XGetInputFocus(dpy, &old_focus, &old_revert);
1133 XSetInputFocus(dpy, text->core->window, RevertToNone, CurrentTime);
1135 if (XGrabKeyboard(dpy, text->core->window, True, GrabModeAsync,
1136 GrabModeAsync, CurrentTime)!=GrabSuccess) {
1137 wwarning("could not grab keyboard");
1138 wTextDestroy(text);
1140 wSetFocusTo(menu->frame->screen_ptr,
1141 menu->frame->screen_ptr->focused_window);
1142 return;
1146 while (!done && !text->done) {
1147 XSync(dpy, 0);
1148 XAllowEvents(dpy, AsyncKeyboard|AsyncPointer, CurrentTime);
1149 XSync(dpy, 0);
1150 WMNextEvent(dpy, &event);
1152 if (XFindContext(dpy, event.xany.window, wWinContext,
1153 (XPointer *)&desc)==XCNOENT)
1154 desc = NULL;
1156 if ((desc != NULL) && (desc->handle_anything != NULL)) {
1158 (*desc->handle_anything)(desc, &event);
1160 } else {
1161 switch (event.type) {
1162 case ButtonPress:
1163 XAllowEvents(dpy, ReplayPointer, CurrentTime);
1164 done = 1;
1166 default:
1167 WMHandleEvent(&event);
1168 break;
1173 XSetInputFocus(dpy, old_focus, old_revert, CurrentTime);
1175 wSetFocusTo(menu->frame->screen_ptr,
1176 menu->frame->screen_ptr->focused_window);
1179 t = wTextGetText(text);
1180 /* if !t, the user has canceled editing */
1181 if (t) {
1182 if (entry->text)
1183 free(entry->text);
1184 entry->text = wstrdup(t);
1186 menu->flags.realized = 0;
1188 wTextDestroy(text);
1190 XUngrabKeyboard(dpy, CurrentTime);
1192 if (t && menu->on_edit)
1193 (*menu->on_edit)(menu, entry);
1195 menu->flags.editing = 0;
1197 if (!menu->flags.realized)
1198 wMenuRealize(menu);
1202 static void
1203 selectEntry(WMenu *menu, int entry_no)
1205 WMenuEntry *entry;
1206 WMenu *submenu;
1207 int old_entry;
1209 if (menu->entries==NULL)
1210 return;
1212 if (entry_no >= menu->entry_no)
1213 return;
1215 old_entry = menu->selected_entry;
1216 menu->selected_entry = entry_no;
1218 if (old_entry!=entry_no) {
1220 /* unselect previous entry */
1221 if (old_entry>=0) {
1222 paintEntry(menu, old_entry, False);
1223 entry = menu->entries[old_entry];
1225 /* unmap cascade */
1226 if (entry->cascade>=0 && menu->cascades) {
1227 if (!menu->cascades[entry->cascade]->flags.buttoned) {
1228 wMenuUnmap(menu->cascades[entry->cascade]);
1233 if (entry_no<0) {
1234 menu->selected_entry = -1;
1235 return;
1237 entry = menu->entries[entry_no];
1239 if (entry->cascade>=0 && menu->cascades && entry->flags.enabled) {
1240 /* Callback for when the submenu is opened.
1242 submenu = menu->cascades[entry->cascade];
1243 if (submenu && submenu->flags.brother)
1244 submenu = submenu->brother;
1246 if (entry->callback) {
1247 /* Only call the callback if the submenu is not yet mapped.
1249 if (menu->flags.brother) {
1250 if (!submenu || !submenu->flags.mapped)
1251 (*entry->callback)(menu->brother, entry);
1252 } else {
1253 if (!submenu || !submenu->flags.buttoned)
1254 (*entry->callback)(menu, entry);
1258 /* the submenu menu might have changed */
1259 submenu = menu->cascades[entry->cascade];
1261 /* map cascade */
1262 if (!submenu->flags.mapped) {
1263 int x, y;
1265 if (!submenu->flags.realized)
1266 wMenuRealize(submenu);
1267 if (wPreferences.wrap_menus) {
1268 if (menu->flags.open_to_left)
1269 submenu->flags.open_to_left = 1;
1271 if (submenu->flags.open_to_left) {
1272 x = menu->frame_x - MENUW(submenu);
1273 if (x<0) {
1274 x = 0;
1275 submenu->flags.open_to_left = 0;
1277 } else {
1278 x = menu->frame_x + MENUW(menu);
1280 if (x + MENUW(submenu)
1281 >= menu->frame->screen_ptr->scr_width) {
1283 x = menu->frame_x - MENUW(submenu);
1284 submenu->flags.open_to_left = 1;
1287 } else {
1288 x = menu->frame_x + MENUW(menu);
1291 if (wPreferences.align_menus) {
1292 y = menu->frame_y;
1293 } else {
1294 y = menu->frame_y + menu->entry_height*entry_no;
1295 if (menu->flags.titled)
1296 y += menu->frame->top_width;
1297 if (menu->cascades[entry->cascade]->flags.titled)
1298 y -= menu->cascades[entry->cascade]->frame->top_width;
1301 wMenuMapAt(menu->cascades[entry->cascade], x, y, False);
1302 menu->cascades[entry->cascade]->parent = menu;
1303 } else {
1304 return;
1307 paintEntry(menu, entry_no, True);
1312 static WMenu*
1313 findMenu(WScreen *scr, int *x_ret, int *y_ret)
1315 WMenu *menu;
1316 WObjDescriptor *desc;
1317 Window root_ret, win, junk_win;
1318 int x, y, wx, wy;
1319 unsigned int mask;
1321 XQueryPointer(dpy, scr->root_win, &root_ret, &win, &x, &y, &wx, &wy,
1322 &mask);
1324 if (win==None) return NULL;
1326 if (XFindContext(dpy, win, wWinContext, (XPointer *)&desc)==XCNOENT)
1327 return NULL;
1329 if (desc->parent_type == WCLASS_MENU) {
1330 menu = (WMenu*)desc->parent;
1331 XTranslateCoordinates(dpy, root_ret, menu->menu->window, wx, wy,
1332 x_ret, y_ret, &junk_win);
1333 return menu;
1335 return NULL;
1341 static void
1342 closeCascade(WMenu *menu)
1344 WMenu *parent=menu->parent;
1346 if (menu->flags.brother
1347 || (!menu->flags.buttoned
1348 && (!menu->flags.app_menu||menu->parent!=NULL))) {
1350 selectEntry(menu, -1);
1351 XSync(dpy, 0);
1352 #if (MENU_BLINK_DELAY > 2)
1353 wusleep(MENU_BLINK_DELAY/2);
1354 #endif
1355 wMenuUnmap(menu);
1356 while (parent!=NULL
1357 && (parent->parent!=NULL || !parent->flags.app_menu
1358 || parent->flags.brother)
1359 && !parent->flags.buttoned) {
1360 selectEntry(parent, -1);
1361 wMenuUnmap(parent);
1362 parent = parent->parent;
1364 if (parent)
1365 selectEntry(parent, -1);
1370 static void
1371 closeBrotherCascadesOf(WMenu *menu)
1373 WMenu *tmp;
1374 int i;
1376 for (i=0; i<menu->cascade_no; i++) {
1377 if (menu->cascades[i]->flags.brother) {
1378 tmp = menu->cascades[i];
1379 } else {
1380 tmp = menu->cascades[i]->brother;
1382 if (tmp->flags.mapped) {
1383 selectEntry(tmp->parent, -1);
1384 closeBrotherCascadesOf(tmp);
1385 break;
1391 #define getEntryAt(menu, x, y) ((y)<0 ? -1 : (y)/(menu->entry_height))
1394 static WMenu*
1395 parentMenu(WMenu *menu)
1397 WMenu *parent;
1398 WMenuEntry *entry;
1400 if (menu->flags.buttoned)
1401 return menu;
1403 while (menu->parent && menu->parent->flags.mapped) {
1404 parent = menu->parent;
1405 if (parent->selected_entry < 0)
1406 break;
1407 entry = parent->entries[parent->selected_entry];
1408 if (!entry->flags.enabled || entry->cascade<0 || !parent->cascades ||
1409 parent->cascades[entry->cascade] != menu)
1410 break;
1411 menu = parent;
1412 if (menu->flags.buttoned)
1413 break;
1416 return menu;
1422 * Will raise the passed menu, if submenu = 0
1423 * If submenu > 0 will also raise all mapped submenus
1424 * until the first buttoned one
1425 * If submenu < 0 will also raise all mapped parent menus
1426 * until the first buttoned one
1429 static void
1430 raiseMenus(WMenu *menu, int submenus)
1432 WMenu *submenu;
1433 int i;
1435 if(!menu) return;
1437 wRaiseFrame(menu->frame->core);
1439 if (submenus>0 && menu->selected_entry>=0) {
1440 i = menu->entries[menu->selected_entry]->cascade;
1441 if (i>=0 && menu->cascades) {
1442 submenu = menu->cascades[i];
1443 if (submenu->flags.mapped && !submenu->flags.buttoned)
1444 raiseMenus(submenu, submenus);
1447 if (submenus<0 && !menu->flags.buttoned &&
1448 menu->parent && menu->parent->flags.mapped)
1449 raiseMenus(menu->parent, submenus);
1453 WMenu*
1454 wMenuUnderPointer(WScreen *screen)
1456 WObjDescriptor *desc;
1457 Window root_ret, win;
1458 int dummy;
1459 unsigned int mask;
1461 XQueryPointer(dpy, screen->root_win, &root_ret, &win, &dummy, &dummy,
1462 &dummy, &dummy, &mask);
1464 if (win==None) return NULL;
1466 if (XFindContext(dpy, win, wWinContext, (XPointer *)&desc)==XCNOENT)
1467 return NULL;
1469 if (desc->parent_type == WCLASS_MENU)
1470 return (WMenu *)desc->parent;
1471 return NULL;
1476 #define MIN(a,b) (((a) > (b)) ? (b) : (a))
1479 static void
1480 getPointerPosition(WScreen *scr, int *x, int *y)
1482 Window root_ret, win;
1483 int wx, wy;
1484 unsigned int mask;
1486 XQueryPointer(dpy, scr->root_win, &root_ret, &win, x, y, &wx, &wy, &mask);
1490 static void
1491 getScrollAmount(WMenu *menu, int *hamount, int *vamount)
1493 WScreen *scr = menu->menu->screen_ptr;
1494 int menuX1 = menu->frame_x;
1495 int menuY1 = menu->frame_y;
1496 int menuX2 = menu->frame_x + MENUW(menu);
1497 int menuY2 = menu->frame_y + MENUH(menu);
1498 int screenW = scr->scr_width;
1499 int screenH = scr->scr_height;
1500 int xroot, yroot;
1502 *hamount = 0;
1503 *vamount = 0;
1505 getPointerPosition(scr, &xroot, &yroot);
1508 if (xroot <= 1 && menuX1 < 0) {
1509 /* scroll to the right */
1510 *hamount = MIN(MENU_SCROLL_STEP, abs(menuX1));
1512 } else if (xroot >= screenW-2 && menuX2 > screenW-1) {
1513 /* scroll to the left */
1514 *hamount = MIN(MENU_SCROLL_STEP, abs(menuX2-screenW-1));
1516 if (*hamount==0)
1517 *hamount = 1;
1519 *hamount = -*hamount;
1522 if (yroot <= 1 && menuY1 < 0) {
1523 /* scroll down */
1524 *vamount = MIN(MENU_SCROLL_STEP, abs(menuY1));
1526 } else if (yroot >= screenH-2 && menuY2 > screenH-1) {
1527 /* scroll up */
1528 *vamount = MIN(MENU_SCROLL_STEP, abs(menuY2-screenH-2));
1530 *vamount = -*vamount;
1535 static void
1536 dragScrollMenuCallback(void *data)
1538 WMenu *menu = (WMenu*)data;
1539 WScreen *scr = menu->menu->screen_ptr;
1540 WMenu *parent = parentMenu(menu);
1541 int hamount, vamount;
1542 int x, y;
1543 int newSelectedEntry;
1545 getScrollAmount(menu, &hamount, &vamount);
1548 if (hamount != 0 || vamount != 0) {
1549 wMenuMove(parent, parent->frame_x + hamount,
1550 parent->frame_y + vamount, True);
1551 if (findMenu(scr, &x, &y)) {
1552 newSelectedEntry = getEntryAt(menu, x, y);
1553 selectEntry(menu, newSelectedEntry);
1554 } else {
1555 /* Pointer fell outside of menu. If the selected entry is
1556 * not a submenu, unselect it */
1557 if (menu->selected_entry >= 0
1558 && menu->entries[menu->selected_entry]->cascade<0)
1559 selectEntry(menu, -1);
1560 newSelectedEntry = 0;
1563 /* paranoid check */
1564 if (newSelectedEntry >= 0) {
1565 /* keep scrolling */
1566 menu->timer = WMAddTimerHandler(MENU_SCROLL_DELAY,
1567 dragScrollMenuCallback, menu);
1568 } else {
1569 menu->timer = NULL;
1571 } else {
1572 /* don't need to scroll anymore */
1573 menu->timer = NULL;
1574 if (findMenu(scr, &x, &y)) {
1575 newSelectedEntry = getEntryAt(menu, x, y);
1576 selectEntry(menu, newSelectedEntry);
1582 static void
1583 scrollMenuCallback(void *data)
1585 WMenu *menu = (WMenu*)data;
1586 WMenu *parent = parentMenu(menu);
1587 int hamount = 0; /* amount to scroll */
1588 int vamount = 0;
1590 getScrollAmount(menu, &hamount, &vamount);
1592 if (hamount != 0 || vamount != 0) {
1593 wMenuMove(parent, parent->frame_x + hamount,
1594 parent->frame_y + vamount, True);
1596 /* keep scrolling */
1597 menu->timer = WMAddTimerHandler(MENU_SCROLL_DELAY,
1598 scrollMenuCallback, menu);
1599 } else {
1600 /* don't need to scroll anymore */
1601 menu->timer = NULL;
1607 #define MENU_SCROLL_BORDER 5
1609 static int
1610 isPointNearBoder(WMenu *menu, int x, int y)
1612 int menuX1 = menu->frame_x;
1613 int menuY1 = menu->frame_y;
1614 int menuX2 = menu->frame_x + MENUW(menu);
1615 int menuY2 = menu->frame_y + MENUH(menu);
1616 int scrXe = menu->menu->screen_ptr->scr_width-1;
1617 int scrYe = menu->menu->screen_ptr->scr_height-1;
1618 int flag = 0;
1620 if (x >= menuX1 && x <= menuX2 && (y < MENU_SCROLL_BORDER
1621 || y > scrYe-MENU_SCROLL_BORDER))
1622 flag = 1;
1623 else if (y >= menuY1 && y <= menuY2 && (x < MENU_SCROLL_BORDER
1624 || x > scrXe-MENU_SCROLL_BORDER))
1625 flag = 1;
1627 return flag;
1631 void
1632 wMenuScroll(WMenu *menu, XEvent *event)
1634 WMenu *smenu;
1635 WMenu *omenu = parentMenu(menu);
1636 WScreen *scr = menu->frame->screen_ptr;
1637 int done = 0;
1638 int jump_back = 0;
1639 int old_frame_x = omenu->frame_x;
1640 int old_frame_y = omenu->frame_y;
1641 XEvent ev;
1643 #ifdef DEBUG
1644 puts("Entering menu Scroll");
1645 #endif
1647 if ((/*omenu->flags.buttoned &&*/ !wPreferences.wrap_menus)
1648 || omenu->flags.app_menu) {
1649 jump_back = 1;
1652 if (!wPreferences.wrap_menus)
1653 raiseMenus(omenu, True);
1654 else
1655 raiseMenus(menu, False);
1657 if (!menu->timer)
1658 scrollMenuCallback(menu);
1660 while(!done) {
1661 int x, y, on_border, on_x_edge, on_y_edge, on_title;
1663 WMNextEvent(dpy, &ev);
1664 switch (ev.type) {
1665 case EnterNotify:
1666 WMHandleEvent(&ev);
1667 case MotionNotify:
1668 x = (ev.type==MotionNotify) ? ev.xmotion.x_root : ev.xcrossing.x_root;
1669 y = (ev.type==MotionNotify) ? ev.xmotion.y_root : ev.xcrossing.y_root;
1671 /* on_border is != 0 if the pointer is between the menu
1672 * and the screen border and is close enough to the border */
1673 on_border = isPointNearBoder(menu, x, y);
1675 smenu = wMenuUnderPointer(scr);
1677 if ((smenu==NULL && !on_border) || (smenu && parentMenu(smenu)!=omenu)) {
1678 done = 1;
1679 break;
1682 on_x_edge = x <= 1 || x >= scr->scr_width - 2;
1683 on_y_edge = y <= 1 || y >= scr->scr_height - 2;
1684 on_border = on_x_edge || on_y_edge;
1686 if (!on_border && !jump_back) {
1687 done = 1;
1688 break;
1691 if (menu->timer && (smenu!=menu || (!on_y_edge && !on_x_edge))) {
1692 WMDeleteTimerHandler(menu->timer);
1693 menu->timer = NULL;
1696 if (smenu != NULL)
1697 menu = smenu;
1699 if (!menu->timer)
1700 scrollMenuCallback(menu);
1701 break;
1702 case ButtonPress:
1703 /* True if we push on title, or drag the omenu to other position */
1704 on_title = ev.xbutton.x_root >= omenu->frame_x &&
1705 ev.xbutton.x_root <= omenu->frame_x + MENUW(omenu) &&
1706 ev.xbutton.y_root >= omenu->frame_y &&
1707 ev.xbutton.y_root <= omenu->frame_y + omenu->frame->top_width;
1708 WMHandleEvent(&ev);
1709 smenu = wMenuUnderPointer(scr);
1710 if (smenu == NULL || (smenu && smenu->flags.buttoned && smenu != omenu))
1711 done = 1;
1712 else if (smenu==omenu && on_title) {
1713 jump_back = 0;
1714 done = 1;
1716 break;
1717 case KeyPress:
1718 done = 1;
1719 default:
1720 WMHandleEvent(&ev);
1721 break;
1725 if (menu->timer) {
1726 WMDeleteTimerHandler(menu->timer);
1727 menu->timer = NULL;
1730 if (jump_back)
1731 wMenuMove(omenu, old_frame_x, old_frame_y, True);
1733 #ifdef DEBUG
1734 puts("Leaving menu Scroll");
1735 #endif
1740 static void
1741 menuExpose(WObjDescriptor *desc, XEvent *event)
1743 wMenuPaint(desc->parent);
1746 typedef struct {
1747 int *delayed_select;
1748 WMenu *menu;
1749 WMHandlerID magic;
1750 } delay_data;
1753 static void
1754 delaySelection(void *data)
1756 delay_data *d = (delay_data*)data;
1757 int x, y, entry_no;
1758 WMenu *menu;
1760 d->magic = NULL;
1762 menu = findMenu(d->menu->menu->screen_ptr, &x, &y);
1763 if (menu && (d->menu == menu || d->delayed_select)) {
1764 entry_no = getEntryAt(menu, x, y);
1765 selectEntry(menu, entry_no);
1767 if (d->delayed_select)
1768 *(d->delayed_select) = 0;
1772 static void
1773 menuMouseDown(WObjDescriptor *desc, XEvent *event)
1775 XButtonEvent *bev = &event->xbutton;
1776 WMenu *menu = desc->parent;
1777 WMenu *smenu;
1778 WScreen *scr=menu->frame->screen_ptr;
1779 WMenuEntry *entry=NULL;
1780 XEvent ev;
1781 int close_on_exit=0;
1782 int done=0;
1783 int delayed_select = 0;
1784 int entry_no;
1785 int x, y;
1786 int prevx, prevy;
1787 int old_frame_x = 0;
1788 int old_frame_y = 0;
1789 delay_data d_data = {NULL, NULL, NULL};
1791 if (!wPreferences.wrap_menus) {
1792 smenu = parentMenu(menu);
1793 old_frame_x = smenu->frame_x;
1794 old_frame_y = smenu->frame_y;
1796 else if (event->xbutton.window == menu->frame->core->window) {
1797 /* This is true if the menu was launched with right click on root window */
1798 delayed_select = 1;
1799 d_data.delayed_select = &delayed_select;
1800 d_data.menu = menu;
1801 d_data.magic = WMAddTimerHandler(wPreferences.dblclick_time,
1802 delaySelection, &d_data);
1805 wRaiseFrame(menu->frame->core);
1807 close_on_exit = (bev->send_event || menu->flags.brother);
1809 smenu = findMenu(scr, &x, &y);
1810 if (!smenu) {
1811 x = -1;
1812 y = -1;
1813 } else {
1814 menu = smenu;
1817 if (menu->flags.editing) {
1818 return;
1820 entry_no = getEntryAt(menu, x, y);
1821 if (entry_no>=0) {
1822 entry = menu->entries[entry_no];
1824 if (!close_on_exit && (bev->state & ControlMask) && smenu
1825 && entry->flags.editable) {
1826 editEntry(smenu, entry);
1827 return;
1828 } else if (bev->state & ControlMask) {
1829 return;
1832 if (entry->flags.enabled && entry->cascade>=0 && menu->cascades) {
1833 WMenu *submenu = menu->cascades[entry->cascade];
1834 /* map cascade */
1835 if (submenu->flags.mapped && !submenu->flags.buttoned &&
1836 menu->selected_entry!=entry_no) {
1837 wMenuUnmap(submenu);
1839 if (!submenu->flags.mapped && !delayed_select) {
1840 selectEntry(menu, entry_no);
1841 } else if (!submenu->flags.buttoned) {
1842 selectEntry(menu, -1);
1845 } else if (!delayed_select) {
1846 selectEntry(menu, entry_no);
1849 if (!wPreferences.wrap_menus && !wPreferences.scrollable_menus) {
1850 if (!menu->timer)
1851 dragScrollMenuCallback(menu);
1855 prevx = bev->x_root;
1856 prevy = bev->y_root;
1857 while (!done) {
1858 int x, y;
1860 XAllowEvents(dpy, SyncPointer, CurrentTime);
1862 WMMaskEvent(dpy, ExposureMask|ButtonMotionMask|ButtonReleaseMask
1863 |ButtonPressMask, &ev);
1864 switch (ev.type) {
1865 case MotionNotify:
1866 smenu = findMenu(scr, &x, &y);
1868 if (smenu == NULL) {
1869 /* moved mouse out of menu */
1871 if (!delayed_select && d_data.magic) {
1872 WMDeleteTimerHandler(d_data.magic);
1873 d_data.magic = NULL;
1875 if (menu==NULL
1876 || (menu->selected_entry>=0
1877 && menu->entries[menu->selected_entry]->cascade>=0)) {
1878 prevx = ev.xmotion.x_root;
1879 prevy = ev.xmotion.y_root;
1881 break;
1883 selectEntry(menu, -1);
1884 menu = smenu;
1885 prevx = ev.xmotion.x_root;
1886 prevy = ev.xmotion.y_root;
1887 break;
1888 } else if (menu && menu!=smenu
1889 && (menu->selected_entry<0
1890 || menu->entries[menu->selected_entry]->cascade<0)) {
1891 selectEntry(menu, -1);
1893 if (!delayed_select && d_data.magic) {
1894 WMDeleteTimerHandler(d_data.magic);
1895 d_data.magic = NULL;
1897 } else {
1899 /* hysteresis for item selection */
1901 /* check if the motion was to the side, indicating that
1902 * the user may want to cross to a submenu */
1903 if (!delayed_select && menu) {
1904 int dx;
1905 Bool moved_to_submenu;/* moved to direction of submenu */
1907 dx = abs(prevx - ev.xmotion.x_root);
1909 moved_to_submenu = False;
1910 if (dx > 0 /* if moved enough to the side */
1911 /* maybe a open submenu */
1912 && menu->selected_entry>=0
1913 /* moving to the right direction */
1914 && (wPreferences.align_menus
1915 || ev.xmotion.y_root >= prevy)) {
1916 int index;
1918 index = menu->entries[menu->selected_entry]->cascade;
1919 if (index>=0) {
1920 if (menu->cascades[index]->frame_x>menu->frame_x) {
1921 if (prevx < ev.xmotion.x_root)
1922 moved_to_submenu = True;
1923 } else {
1924 if (prevx > ev.xmotion.x_root)
1925 moved_to_submenu = True;
1931 if (menu != smenu) {
1932 if (d_data.magic) {
1933 WMDeleteTimerHandler(d_data.magic);
1935 d_data.magic = NULL;
1936 } else if (moved_to_submenu) {
1937 /* while we are moving, postpone the selection */
1938 if (d_data.magic) {
1939 WMDeleteTimerHandler(d_data.magic);
1941 d_data.delayed_select = NULL;
1942 d_data.menu = menu;
1943 d_data.magic = WMAddTimerHandler(MENU_SELECT_DELAY,
1944 delaySelection,
1945 &d_data);
1946 prevx = ev.xmotion.x_root;
1947 prevy = ev.xmotion.y_root;
1948 break;
1949 } else {
1950 if (d_data.magic)
1951 WMDeleteTimerHandler(d_data.magic);
1952 d_data.magic = NULL;
1956 prevx = ev.xmotion.x_root;
1957 prevy = ev.xmotion.y_root;
1958 if (menu!=smenu) {
1959 /* pointer crossed menus */
1960 if (menu && menu->timer) {
1961 WMDeleteTimerHandler(menu->timer);
1962 menu->timer = NULL;
1964 if (smenu)
1965 dragScrollMenuCallback(smenu);
1967 menu = smenu;
1968 if (!menu->timer)
1969 dragScrollMenuCallback(menu);
1971 if (!delayed_select) {
1972 entry_no = getEntryAt(menu, x, y);
1973 if (entry_no>=0) {
1974 entry = menu->entries[entry_no];
1975 if (entry->flags.enabled && entry->cascade>=0 &&
1976 menu->cascades) {
1977 WMenu *submenu = menu->cascades[entry->cascade];
1978 if (submenu->flags.mapped && !submenu->flags.buttoned
1979 && menu->selected_entry!=entry_no) {
1980 wMenuUnmap(submenu);
1984 selectEntry(menu, entry_no);
1986 break;
1988 case ButtonPress:
1989 break;
1991 case ButtonRelease:
1992 if (ev.xbutton.button == event->xbutton.button)
1993 done=1;
1994 break;
1996 case Expose:
1997 WMHandleEvent(&ev);
1998 break;
2002 if (menu && menu->timer) {
2003 WMDeleteTimerHandler(menu->timer);
2004 menu->timer = NULL;
2006 if (d_data.magic!=NULL)
2007 WMDeleteTimerHandler(d_data.magic);
2009 if (menu && menu->selected_entry>=0) {
2010 entry = menu->entries[menu->selected_entry];
2011 if (entry->callback!=NULL && entry->flags.enabled
2012 && entry->cascade < 0) {
2013 /* blink and erase menu selection */
2014 #if (MENU_BLINK_DELAY > 0)
2015 int sel = menu->selected_entry;
2016 int i;
2018 for (i=0; i<MENU_BLINK_COUNT; i++) {
2019 paintEntry(menu, sel, False);
2020 XSync(dpy, 0);
2021 wusleep(MENU_BLINK_DELAY);
2022 paintEntry(menu, sel, True);
2023 XSync(dpy, 0);
2024 wusleep(MENU_BLINK_DELAY);
2026 #endif
2027 /* unmap the menu, it's parents and call the callback */
2028 if (!menu->flags.buttoned &&
2029 (!menu->flags.app_menu||menu->parent!=NULL)) {
2030 closeCascade(menu);
2031 } else {
2032 selectEntry(menu, -1);
2034 (*entry->callback)(menu, entry);
2036 /* If the user double clicks an entry, the entry will
2037 * be executed twice, which is not good for things like
2038 * the root menu. So, ignore any clicks that were generated
2039 * while the entry was being executed */
2040 while (XCheckTypedWindowEvent(dpy, menu->menu->window,
2041 ButtonPress, &ev));
2042 } else if (entry->callback!=NULL && entry->cascade<0) {
2043 selectEntry(menu, -1);
2044 } else {
2045 if (entry->cascade>=0 && menu->cascades
2046 && menu->cascades[entry->cascade]->flags.brother) {
2047 selectEntry(menu, -1);
2052 if (((WMenu*)desc->parent)->flags.brother || close_on_exit
2053 || !smenu)
2054 closeCascade(desc->parent);
2056 /* close the cascade windows that should not remain opened */
2057 closeBrotherCascadesOf(desc->parent);
2059 if (!wPreferences.wrap_menus)
2060 wMenuMove(parentMenu(desc->parent), old_frame_x, old_frame_y, True);
2064 void
2065 wMenuMove(WMenu *menu, int x, int y, int submenus)
2067 WMenu *submenu;
2068 int i;
2070 if (!menu) return;
2072 menu->frame_x = x;
2073 menu->frame_y = y;
2074 XMoveWindow(dpy, menu->frame->core->window, x, y);
2076 if (submenus>0 && menu->selected_entry>=0) {
2077 i = menu->entries[menu->selected_entry]->cascade;
2079 if (i>=0 && menu->cascades) {
2080 submenu = menu->cascades[i];
2081 if (submenu->flags.mapped && !submenu->flags.buttoned)
2082 if (wPreferences.align_menus) {
2083 wMenuMove(submenu, x + MENUW(menu), y, submenus);
2084 } else {
2085 wMenuMove(submenu, x+ MENUW(menu),
2086 y + submenu->entry_height*menu->selected_entry,
2087 submenus);
2091 if (submenus<0 && menu->parent!=NULL && menu->parent->flags.mapped &&
2092 !menu->parent->flags.buttoned) {
2093 if (wPreferences.align_menus) {
2094 wMenuMove(menu->parent, x - MENUW(menu->parent), y, submenus);
2095 } else {
2096 wMenuMove(menu->parent, x - MENUW(menu->parent), menu->frame_y
2097 - menu->parent->entry_height*menu->parent->selected_entry,
2098 submenus);
2104 static void
2105 changeMenuLevels(WMenu *menu, int lower)
2107 int i;
2109 if (!lower) {
2110 ChangeStackingLevel(menu->frame->core, (!menu->parent ? WMMainMenuLevel
2111 : WMSubmenuLevel));
2112 wRaiseFrame(menu->frame->core);
2113 menu->flags.lowered = 0;
2114 } else {
2115 ChangeStackingLevel(menu->frame->core, WMNormalLevel);
2116 wLowerFrame(menu->frame->core);
2117 menu->flags.lowered = 1;
2119 for (i=0; i<menu->cascade_no; i++) {
2120 if (menu->cascades[i]
2121 && !menu->cascades[i]->flags.buttoned
2122 && menu->cascades[i]->flags.lowered!=lower) {
2123 changeMenuLevels(menu->cascades[i], lower);
2130 static void
2131 menuTitleDoubleClick(WCoreWindow *sender, void *data, XEvent *event)
2133 WMenu *menu = data;
2134 int lower;
2136 if (event->xbutton.state & MOD_MASK) {
2137 if (menu->flags.lowered) {
2138 lower = 0;
2139 } else {
2140 lower = 1;
2142 changeMenuLevels(menu, lower);
2147 static void
2148 menuTitleMouseDown(WCoreWindow *sender, void *data, XEvent *event)
2150 WMenu *menu = data;
2151 WMenu *tmp;
2152 XEvent ev;
2153 int x=menu->frame_x, y=menu->frame_y;
2154 int dx=event->xbutton.x_root, dy=event->xbutton.y_root;
2155 int i, lower;
2156 Bool started;
2158 #ifdef DEBUG
2159 printf("Moving menu\n");
2160 #endif
2162 /* can't touch the menu copy */
2163 if (menu->flags.brother)
2164 return;
2166 if (event->xbutton.button != Button1 && event->xbutton.button != Button2)
2167 return;
2169 if (event->xbutton.state & MOD_MASK) {
2170 wLowerFrame(menu->frame->core);
2171 lower = 1;
2172 } else {
2173 wRaiseFrame(menu->frame->core);
2174 lower = 0;
2176 tmp = menu;
2178 /* lower/raise all submenus */
2179 while (1) {
2180 if (tmp->selected_entry>=0 && tmp->cascades
2181 && tmp->entries[tmp->selected_entry]->cascade>=0) {
2182 tmp = tmp->cascades[tmp->entries[tmp->selected_entry]->cascade];
2183 if (!tmp || !tmp->flags.mapped)
2184 break;
2185 if (lower)
2186 wLowerFrame(tmp->frame->core);
2187 else
2188 wRaiseFrame(tmp->frame->core);
2189 } else {
2190 break;
2194 /* tear off the menu if it's a root menu or a cascade
2195 application menu */
2196 if (!menu->flags.buttoned && !menu->flags.brother
2197 && (!menu->flags.app_menu||menu->parent!=NULL)) {
2198 menu->flags.buttoned=1;
2199 wFrameWindowShowButton(menu->frame, WFF_RIGHT_BUTTON);
2200 if (menu->parent) {
2201 /* turn off selected menu entry in parent menu */
2202 selectEntry(menu->parent, -1);
2204 /* make parent map the copy in place of the original */
2205 for (i=0; i<menu->parent->cascade_no; i++) {
2206 if (menu->parent->cascades[i] == menu) {
2207 menu->parent->cascades[i] = menu->brother;
2208 break;
2214 started = False;
2215 while(1) {
2216 WMMaskEvent(dpy, ButtonMotionMask|ButtonReleaseMask|ButtonPressMask
2217 |ExposureMask, &ev);
2218 switch (ev.type) {
2219 case MotionNotify:
2220 if (started) {
2221 x += ev.xmotion.x_root - dx;
2222 y += ev.xmotion.y_root - dy;
2223 dx = ev.xmotion.x_root;
2224 dy = ev.xmotion.y_root;
2225 wMenuMove(menu, x, y, True);
2226 } else {
2227 if (abs(ev.xmotion.x_root - dx) > MOVE_THRESHOLD
2228 || abs(ev.xmotion.y_root - dy) > MOVE_THRESHOLD) {
2229 started = True;
2230 XGrabPointer(dpy, menu->frame->titlebar->window, False,
2231 ButtonMotionMask|ButtonReleaseMask
2232 |ButtonPressMask,
2233 GrabModeAsync, GrabModeAsync, None,
2234 wCursor[WCUR_MOVE], CurrentTime);
2237 break;
2239 case ButtonPress:
2240 break;
2242 case ButtonRelease:
2243 if (ev.xbutton.button != event->xbutton.button)
2244 break;
2245 #ifdef DEBUG
2246 printf("End menu move\n");
2247 #endif
2248 XUngrabPointer(dpy, CurrentTime);
2249 return;
2251 default:
2252 WMHandleEvent(&ev);
2253 break;
2259 *----------------------------------------------------------------------
2260 * menuCloseClick--
2261 * Handles mouse click on the close button of menus. The menu is
2262 * closed when the button is clicked.
2264 * Side effects:
2265 * The closed menu is reinserted at it's parent menus
2266 * cascade list.
2267 *----------------------------------------------------------------------
2269 static void
2270 menuCloseClick(WCoreWindow *sender, void *data, XEvent *event)
2272 WMenu *menu = (WMenu*)data;
2273 WMenu *parent = menu->parent;
2274 int i;
2276 if (parent) {
2277 for (i=0; i<parent->cascade_no; i++) {
2278 /* find the entry that points to the copy */
2279 if (parent->cascades[i] == menu->brother) {
2280 /* make it point to the original */
2281 parent->cascades[i] = menu;
2282 menu->parent = parent;
2283 break;
2287 wMenuUnmap(menu);
2291 void
2292 wMenuSaveState(WScreen *scr)
2294 proplist_t menus, key, value;
2295 int save_menus = 0;
2296 char buffer[256];
2298 menus = PLMakeDictionaryFromEntries(NULL, NULL, NULL);
2300 #ifndef LITE
2301 if (scr->root_menu && scr->root_menu->flags.buttoned) {
2302 sprintf(buffer, "%i,%i", scr->root_menu->frame_x,
2303 scr->root_menu->frame_y);
2304 key = PLMakeString("RootMenu");
2305 value = PLMakeString(buffer);
2306 PLInsertDictionaryEntry(menus, key, value);
2307 PLRelease(key);
2308 PLRelease(value);
2309 save_menus = 1;
2312 if (scr->switch_menu && scr->switch_menu->flags.buttoned) {
2313 sprintf(buffer, "%i,%i", scr->switch_menu->frame_x,
2314 scr->switch_menu->frame_y);
2315 key = PLMakeString("SwitchMenu");
2316 value = PLMakeString(buffer);
2317 PLInsertDictionaryEntry(menus, key, value);
2318 PLRelease(key);
2319 PLRelease(value);
2320 save_menus = 1;
2322 #endif /* !LITE */
2323 if (scr->workspace_menu && scr->workspace_menu->flags.buttoned) {
2324 sprintf(buffer, "%i,%i", scr->workspace_menu->frame_x,
2325 scr->workspace_menu->frame_y);
2326 key = PLMakeString("WorkspaceMenu");
2327 value = PLMakeString(buffer);
2328 PLInsertDictionaryEntry(menus, key, value);
2329 PLRelease(key);
2330 PLRelease(value);
2331 save_menus = 1;
2334 if (save_menus) {
2335 key = PLMakeString("Menus");
2336 PLInsertDictionaryEntry(scr->session_state, key, menus);
2337 PLRelease(key);
2339 PLRelease(menus);
2343 #define COMPLAIN(key) wwarning(_("bad value in menus state info:%s"), key)
2346 static int
2347 restoreMenu(WScreen *scr, proplist_t menu, int which)
2349 int i, x, y;
2350 WMenu *pmenu = NULL;
2352 if (!menu)
2353 return False;
2355 if (!PLIsString(menu)) {
2356 COMPLAIN("Position");
2357 return False;
2360 if (sscanf(PLGetString(menu), "%i,%i", &x, &y)!=2)
2361 COMPLAIN("Position");
2363 #ifndef LITE
2364 if (which & WSS_ROOTMENU) {
2365 OpenRootMenu(scr, x, y, False);
2366 pmenu = scr->root_menu;
2367 } else if (which & WSS_SWITCHMENU) {
2368 OpenSwitchMenu(scr, x, y, False);
2369 pmenu = scr->switch_menu;
2370 } else
2371 #endif /* !LITE */
2372 if (which & WSS_WSMENU) {
2373 OpenWorkspaceMenu(scr, x, y);
2374 pmenu = scr->workspace_menu;
2375 if (pmenu->parent) {
2376 /* make parent map the copy in place of the original */
2377 for (i=0; i<pmenu->parent->cascade_no; i++) {
2378 if (pmenu->parent->cascades[i] == pmenu) {
2379 pmenu->parent->cascades[i] = pmenu->brother;
2380 break;
2386 if (pmenu) {
2387 int width = MENUW(pmenu);
2388 int height = MENUH(pmenu);
2390 x = (x < -width) ? 0 : x;
2391 x = (x > scr->scr_width) ? scr->scr_width - width : x;
2392 y = (y < 0) ? 0 : y;
2393 y = (y > scr->scr_height) ? scr->scr_height - height : y;
2394 wMenuMove(pmenu, x, y, True);
2395 pmenu->flags.buttoned = 1;
2396 wFrameWindowShowButton(pmenu->frame, WFF_RIGHT_BUTTON);
2397 return True;
2399 return False;
2403 void
2404 wMenuRestoreState(WScreen *scr)
2406 proplist_t menus, menu, key, rkey, skey, wkey;
2408 key = PLMakeString("Menus");
2409 menus = PLGetDictionaryEntry(scr->session_state, key);
2410 PLRelease(key);
2412 if (!menus)
2413 return;
2415 /* restore menus */
2417 rkey = PLMakeString("RootMenu");
2418 skey = PLMakeString("SwitchMenu");
2419 wkey = PLMakeString("WorkspaceMenu");
2420 menu = PLGetDictionaryEntry(menus, rkey);
2421 restoreMenu(scr, menu, WSS_ROOTMENU);
2422 menu = PLGetDictionaryEntry(menus, skey);
2423 restoreMenu(scr, menu, WSS_SWITCHMENU);
2424 menu = PLGetDictionaryEntry(menus, wkey);
2425 restoreMenu(scr, menu, WSS_WSMENU);
2427 PLRelease(rkey);
2428 PLRelease(skey);
2429 PLRelease(wkey);
2433 void
2434 OpenWorkspaceMenu(WScreen *scr, int x, int y)
2436 WMenu *menu, *parent;
2437 WMenuEntry *entry;
2439 #ifndef LITE
2440 if (!scr->root_menu) {
2441 OpenRootMenu(scr, scr->scr_width*2, 0, False);
2442 wMenuUnmap(scr->root_menu);
2444 #endif
2445 menu = scr->workspace_menu;
2446 if (menu) {
2447 if (menu->flags.mapped) {
2448 if (!menu->flags.buttoned) {
2449 wMenuUnmap(menu);
2450 parent = menu->parent;
2451 if (parent && parent->selected_entry >= 0) {
2452 entry = parent->entries[parent->selected_entry];
2453 if (parent->cascades[entry->cascade] == menu) {
2454 selectEntry(parent, -1);
2455 wMenuMapAt(menu, x, y, False);
2458 } else {
2459 wRaiseFrame(menu->frame->core);
2460 wMenuMapCopyAt(menu, x, y);
2463 else {
2464 wMenuMapAt(menu, x, y, False);