kde 1.1 update
[wmaker-crm.git] / src / menu.c
blob786769d893b71ab654e6b5bc65615d634561765f
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 #ifndef NEWSTUFF
144 /* XXX */
145 menu->frame->rbutton_image = screen->b_pixmaps[WBUT_CLOSE];
146 #endif
148 menu->entry_no = 0;
149 menu->alloced_entries = 0;
150 menu->selected_entry = -1;
151 menu->entries = NULL;
153 menu->frame_x = screen->app_menu_x;
154 menu->frame_y = screen->app_menu_y;
156 menu->frame->child = menu;
158 menu->flags.lowered = 0;
160 /* create borders */
161 if (title) {
162 /* setup object descriptors */
163 menu->frame->on_mousedown_titlebar = menuTitleMouseDown;
164 menu->frame->on_dblclick_titlebar = menuTitleDoubleClick;
167 menu->frame->on_click_right = menuCloseClick;
170 menu->menu = wCoreCreate(menu->frame->core, 0, menu->frame->top_width,
171 menu->frame->core->width, 10);
173 menu->menu->descriptor.parent = menu;
174 menu->menu->descriptor.parent_type = WCLASS_MENU;
175 menu->menu->descriptor.handle_expose = menuExpose;
176 menu->menu->descriptor.handle_mousedown = menuMouseDown;
178 menu->menu_texture_data = None;
180 XMapWindow(dpy, menu->menu->window);
182 XFlush(dpy);
184 if (!brother) {
185 brother = 1;
186 menu->brother = wMenuCreate(screen, title, main_menu);
187 brother = 0;
188 menu->brother->flags.brother = 1;
189 menu->brother->brother = menu;
193 return menu;
199 WMenu*
200 wMenuCreateForApp(WScreen *screen, char *title, int main_menu)
202 WMenu *menu;
204 menu = wMenuCreate(screen, title, main_menu);
205 if (!menu)
206 return NULL;
207 menu->flags.app_menu = 1;
208 menu->brother->flags.app_menu = 1;
210 return menu;
215 static void
216 insertEntry(WMenu *menu, WMenuEntry *entry, int index)
218 int i;
220 for (i = menu->entry_no-1; i >= index; i--) {
221 menu->entries[i]->order++;
222 menu->entries[i+1] = menu->entries[i];
224 menu->entries[index] = entry;
228 void
229 wMenuRefresh(WMenu *menu, int flags)
231 int i;
233 if (flags & MR_TEXT_BACK) {
234 menu->frame->flags.need_texture_remake = 1;
237 if (flags & (MR_RESIZED|MR_TITLE_TEXT)) {
238 menu->flags.realized = 0;
241 wMenuRealize(menu);
243 if (menu->flags.titled)
244 wFrameWindowPaint(menu->frame);
246 if (!menu->flags.brother) {
247 if (menu->brother)
248 wMenuRefresh(menu->brother, flags);
250 for (i=0; i < menu->cascade_no; i++) {
251 if (!menu->cascades[i]->flags.brother)
252 wMenuRefresh(menu->cascades[i], flags);
253 else
254 wMenuRefresh(menu->cascades[i]->brother, flags);
261 WMenuEntry*
262 wMenuInsertCallback(WMenu *menu, int index, char *text,
263 void (*callback)(WMenu *menu, WMenuEntry *entry),
264 void *clientdata)
266 WMenuEntry *entry;
268 #ifdef DEBUG
269 if (!menu) {
270 printf("Passed NULL as menu parameter to wMenuAddCallback() \n");
271 return NULL;
273 #endif
275 assert(menu->flags.brother==0);
276 menu->flags.realized = 0;
277 menu->brother->flags.realized = 0;
279 /* reallocate array if it's too small */
280 if (menu->entry_no >= menu->alloced_entries) {
281 void *tmp;
282 #ifdef DEBUG
283 puts("doing wrealloc()");
284 #endif
285 tmp = wrealloc(menu->entries,
286 sizeof(WMenuEntry)*(menu->alloced_entries+5));
287 if (tmp==NULL) {
288 wwarning(_("wrealloc() failed while trying to add menu item"));
289 return NULL;
292 menu->entries = tmp;
293 menu->alloced_entries += 5;
295 menu->brother->entries = tmp;
296 menu->brother->alloced_entries = menu->alloced_entries;
298 entry = wmalloc(sizeof(WMenuEntry));
299 memset(entry, 0, sizeof(WMenuEntry));
300 entry->flags.enabled = 1;
301 entry->text = wstrdup(text);
302 entry->cascade = -1;
303 entry->clientdata = clientdata;
304 entry->callback = callback;
305 if (index<0 || index>=menu->entry_no) {
306 entry->order = menu->entry_no;
307 menu->entries[menu->entry_no] = entry;
308 } else {
309 entry->order = index;
310 insertEntry(menu, entry, index);
313 menu->entry_no++;
314 menu->brother->entry_no = menu->entry_no;
316 return entry;
321 void
322 wMenuEntrySetCascade(WMenu *menu, WMenuEntry *entry, WMenu *cascade)
324 WMenu *brother = menu->brother;
325 int i, done;
327 assert(menu->flags.brother==0);
329 if (entry->cascade>=0) {
330 menu->flags.realized = 0;
331 brother->flags.realized = 0;
334 cascade->parent = menu;
336 cascade->brother->parent = brother;
338 done = 0;
339 for (i=0; i<menu->cascade_no; i++) {
340 if (menu->cascades[i]==NULL) {
341 menu->cascades[i] = cascade;
342 brother->cascades[i] = cascade->brother;
343 done = 1;
344 entry->cascade = i;
345 break;
348 if (!done) {
349 entry->cascade = menu->cascade_no;
351 menu->cascades = wrealloc(menu->cascades,
352 sizeof(WMenu)*(menu->cascade_no+1));
353 menu->cascades[menu->cascade_no++] = cascade;
356 brother->cascades = wrealloc(brother->cascades,
357 sizeof(WMenu)*(brother->cascade_no+1));
358 brother->cascades[brother->cascade_no++] = cascade->brother;
362 if (menu->flags.lowered) {
364 cascade->flags.lowered = 1;
365 ChangeStackingLevel(cascade->frame->core, WMNormalLevel);
367 cascade->brother->flags.lowered = 1;
368 ChangeStackingLevel(cascade->brother->frame->core, WMNormalLevel);
371 if (!menu->flags.realized)
372 wMenuRealize(menu);
376 void
377 wMenuEntryRemoveCascade(WMenu *menu, WMenuEntry *entry)
379 assert(menu->flags.brother==0);
381 /* destroy cascade menu */
382 if (entry->cascade>=0 && menu->cascades
383 && menu->cascades[entry->cascade]!=NULL) {
385 wMenuDestroy(menu->cascades[entry->cascade], True);
387 menu->cascades[entry->cascade] = NULL;
388 menu->brother->cascades[entry->cascade] = NULL;
390 entry->cascade = -1;
395 void
396 wMenuRemoveItem(WMenu *menu, int index)
398 int i;
400 if (menu->flags.brother) {
401 wMenuRemoveItem(menu->brother, index);
402 return;
405 if (index>=menu->entry_no) return;
407 /* destroy cascade menu */
408 wMenuEntryRemoveCascade(menu, menu->entries[index]);
410 /* destroy unshared data */
412 if (menu->entries[index]->text)
413 free(menu->entries[index]->text);
415 if (menu->entries[index]->rtext)
416 free(menu->entries[index]->rtext);
418 if (menu->entries[index]->free_cdata && menu->entries[index]->clientdata)
419 (*menu->entries[index]->free_cdata)(menu->entries[index]->clientdata);
421 free(menu->entries[index]);
423 for (i=index; i<menu->entry_no-1; i++) {
424 menu->entries[i+1]->order--;
425 menu->entries[i]=menu->entries[i+1];
427 menu->entry_no--;
428 menu->brother->entry_no--;
433 void
434 wMenuRealize(WMenu *menu)
436 int i;
437 int width, rwidth, mrwidth, mwidth;
438 int theight, twidth, eheight;
439 WScreen *scr = menu->frame->screen_ptr;
440 static int brother_done=0;
441 int flags;
443 if (!brother_done) {
444 brother_done = 1;
445 wMenuRealize(menu->brother);
446 brother_done = 0;
449 flags = WFF_SINGLE_STATE;
450 if (menu->flags.titled)
451 flags |= WFF_TITLEBAR|WFF_RIGHT_BUTTON;
453 wFrameWindowUpdateBorders(menu->frame, flags);
455 if (menu->flags.titled) {
456 twidth = wTextWidth(scr->menu_title_font->font, menu->frame->title,
457 strlen(menu->frame->title));
458 theight = menu->frame->top_width;
459 twidth += theight + (wPreferences.new_style ? 16 : 8);
460 } else {
461 twidth = 0;
462 theight = 0;
464 eheight = scr->menu_entry_font->height + 6;
465 menu->entry_height = eheight;
466 mrwidth = 0;
467 mwidth = 0;
468 for (i=0; i<menu->entry_no; i++) {
469 char *text;
471 /* search widest text */
472 text = menu->entries[i]->text;
473 width = wTextWidth(scr->menu_entry_font->font, text, strlen(text))+10;
475 if (menu->entries[i]->flags.indicator) {
476 width += MENU_INDICATOR_SPACE;
479 if (width > mwidth)
480 mwidth = width;
482 /* search widest text on right */
483 text = menu->entries[i]->rtext;
484 if (text)
485 rwidth = wTextWidth(scr->menu_entry_font->font, text,
486 strlen(text)) + 5;
487 else if (menu->entries[i]->cascade>=0)
488 rwidth = 16;
489 else
490 rwidth = 4;
492 if (rwidth > mrwidth)
493 mrwidth = rwidth;
495 mwidth += mrwidth;
497 if (mwidth < twidth)
498 mwidth = twidth;
501 wCoreConfigure(menu->menu, 0, theight, mwidth, menu->entry_no*eheight -1);
503 wFrameWindowResize(menu->frame, mwidth, menu->entry_no*eheight-1
504 + menu->frame->top_width + menu->frame->bottom_width);
507 /* setup background texture */
508 if (scr->menu_item_texture->any.type != WTEX_SOLID) {
509 if (!menu->flags.brother) {
510 FREE_PIXMAP(menu->menu_texture_data);
512 wTextureRender(scr, scr->menu_item_texture,
513 &menu->menu_texture_data, menu->menu->width,
514 menu->entry_height, WREL_MENUENTRY);
516 XSetWindowBackgroundPixmap(dpy, menu->menu->window,
517 menu->menu_texture_data);
518 XClearWindow(dpy, menu->menu->window);
520 XSetWindowBackgroundPixmap(dpy, menu->brother->menu->window,
521 menu->menu_texture_data);
522 XClearWindow(dpy, menu->brother->menu->window);
524 } else {
525 XSetWindowBackground(dpy, menu->menu->window,
526 scr->menu_item_texture->any.color.pixel);
527 XClearWindow(dpy, menu->menu->window);
530 menu->flags.realized = 1;
532 if (menu->flags.mapped)
533 wMenuPaint(menu);
534 if (menu->brother->flags.mapped)
535 wMenuPaint(menu->brother);
539 void
540 wMenuDestroy(WMenu *menu, int recurse)
542 int i;
544 /* remove any pending timers */
545 if (menu->timer)
546 WMDeleteTimerHandler(menu->timer);
547 menu->timer = NULL;
549 /* call destroy handler */
550 if (menu->on_destroy)
551 (*menu->on_destroy)(menu);
553 /* Destroy items if this menu own them. If this is the "brother" menu,
554 * leave them alone as it is shared by them.
556 if (!menu->flags.brother) {
557 for (i=0; i<menu->entry_no; i++) {
559 free(menu->entries[i]->text);
561 if (menu->entries[i]->rtext)
562 free(menu->entries[i]->rtext);
564 if (menu->entries[i]->free_cdata && menu->entries[i]->clientdata) {
565 (*menu->entries[i]->free_cdata)(menu->entries[i]->clientdata);
567 free(menu->entries[i]);
570 if (recurse) {
571 for (i=0; i<menu->cascade_no; i++) {
572 if (menu->cascades[i]) {
573 if (menu->cascades[i]->flags.brother)
574 wMenuDestroy(menu->cascades[i]->brother, recurse);
575 else
576 wMenuDestroy(menu->cascades[i], recurse);
581 if (menu->entries)
582 free(menu->entries);
586 FREE_PIXMAP(menu->menu_texture_data);
588 if (menu->cascades)
589 free(menu->cascades);
591 wCoreDestroy(menu->menu);
592 wFrameWindowDestroy(menu->frame);
594 /* destroy copy of this menu */
595 if (!menu->flags.brother && menu->brother)
596 wMenuDestroy(menu->brother, False);
598 free(menu);
602 static void
603 drawFrame(WScreen *scr, Window win, int y, int w, int h)
605 XSegment segs[2];
607 segs[0].x1 = 0;
608 segs[0].y1 = y;
609 segs[0].x2 = w-1;
610 segs[0].y2 = y;
611 segs[1].x1 = 0;
612 segs[1].y1 = y;
613 segs[1].x2 = 0;
614 segs[1].y2 = y + h - 2;
615 XDrawSegments(dpy, win, scr->menu_item_auxtexture->light_gc, segs, 2);
617 XDrawLine(dpy, win, scr->menu_item_auxtexture->dark_gc, 0, y+h-1,
618 w-1, y+h-1);
620 segs[0].x1 = 1;
621 segs[0].y1 = segs[0].y2 = y + h-2;
622 segs[0].x2 = w-1;
623 segs[1].x1 = segs[1].x2 = w-1;
624 segs[1].y1 = y + 1;
625 segs[1].y2 = y + h-2;
626 XDrawSegments(dpy, win, scr->menu_item_auxtexture->dim_gc, segs, 2);
630 static void
631 paintEntry(WMenu *menu, int index, int selected)
633 int x, y, w, h, tw;
634 GC light, dim, dark, textGC;
635 WScreen *scr=menu->frame->screen_ptr;
636 Window win = menu->menu->window;
637 WMenuEntry *entry=menu->entries[index];
639 if (!menu->flags.realized) return;
640 h = menu->entry_height;
641 w = menu->menu->width;
642 y = index * h;
644 light = scr->menu_item_auxtexture->light_gc;
645 dim = scr->menu_item_auxtexture->dim_gc;
646 dark = scr->menu_item_auxtexture->dark_gc;
648 /* paint background */
649 if (selected) {
650 XSetForeground(dpy, scr->select_menu_gc, scr->select_pixel);
651 XFillRectangle(dpy, win, scr->select_menu_gc, 1, y+1, w-2, h-3);
652 if (scr->menu_item_texture->any.type == WTEX_SOLID)
653 drawFrame(scr, win, y, w, h);
654 } else {
655 if (scr->menu_item_texture->any.type == WTEX_SOLID) {
656 XClearArea(dpy, win, 0, y + 1, w - 1, h - 3, False);
657 /* draw the frame */
658 drawFrame(scr, win, y, w, h);
659 } else {
660 XClearArea(dpy, win, 0, y, w, h, False);
664 if (selected) {
665 textGC = scr->select_menu_gc;
666 if (entry->flags.enabled)
667 XSetForeground(dpy, textGC, scr->select_text_pixel);
668 else
669 XSetForeground(dpy, textGC, scr->dtext_pixel);
670 } else if (!entry->flags.enabled) {
671 textGC = scr->disabled_menu_entry_gc;
672 } else {
673 textGC = scr->menu_entry_gc;
675 /* draw text */
676 x = 5;
677 if (entry->flags.indicator)
678 x += MENU_INDICATOR_SPACE + 2;
680 wDrawString(win, scr->menu_entry_font,
681 textGC, x, 3+y+scr->menu_entry_font->y, entry->text,
682 strlen(entry->text));
684 if (entry->cascade>=0) {
685 /* draw the cascade indicator */
686 XDrawLine(dpy,win,dim, w-11, y+6, w-6, y+h/2-1);
687 XDrawLine(dpy,win,light, w-11, y+h-8, w-6, y+h/2-1);
688 XDrawLine(dpy,win,dark, w-12, y+6, w-12, y+h-8);
691 /* draw indicator */
692 if (entry->flags.indicator && entry->flags.indicator_on) {
693 int iw, ih;
694 WPixmap *indicator;
697 switch (entry->flags.indicator_type) {
698 case MI_CHECK:
699 indicator = scr->menu_check_indicator;
700 break;
701 case MI_MINIWINDOW:
702 indicator = scr->menu_mini_indicator;
703 break;
704 case MI_HIDDEN:
705 indicator = scr->menu_hide_indicator;
706 break;
707 case MI_SHADED:
708 indicator = scr->menu_shade_indicator;
709 break;
710 case MI_DIAMOND:
711 default:
712 indicator = scr->menu_radio_indicator;
713 break;
716 iw = indicator->width;
717 ih = indicator->height;
718 XSetClipMask(dpy, scr->copy_gc, indicator->mask);
719 XSetClipOrigin(dpy, scr->copy_gc, 5, y+(h-ih)/2);
720 if (selected)
721 XSetForeground(dpy, scr->copy_gc, scr->black_pixel);
722 else
723 XSetForeground(dpy, scr->copy_gc, scr->mtext_pixel);
724 XFillRectangle(dpy, win, scr->copy_gc, 5, y+(h-ih)/2, iw, ih);
726 XCopyArea(dpy, indicator->image, win, scr->copy_gc, 0, 0,
727 iw, ih, 5, y+(h-ih)/2);
729 XSetClipOrigin(dpy, scr->copy_gc, 0, 0);
732 /* draw right text */
734 if (entry->rtext && entry->cascade<0) {
735 tw = wTextWidth(scr->menu_entry_font->font, entry->rtext,
736 strlen(entry->rtext));
738 wDrawString(win, scr->menu_entry_font, textGC, w-6-tw,
739 3+y+scr->menu_entry_font->y, entry->rtext,
740 strlen(entry->rtext));
744 static void
745 move_menus(WMenu *menu, int x, int y)
747 while (menu->parent) {
748 menu = menu->parent;
749 x -= MENUW(menu);
750 if (!wPreferences.align_menus && menu->selected_entry>=0) {
751 y -= menu->selected_entry*menu->entry_height;
754 wMenuMove(menu, x, y, True);
757 static void
758 makeVisible(WMenu *menu)
760 WScreen *scr = menu->frame->screen_ptr;
761 int x1, y1, x2, y2, new_x, new_y, move;
763 if (menu->entry_no<0) return;
765 x1 = menu->frame_x;
766 y1 = menu->frame_y+menu->frame->top_width
767 + menu->selected_entry*menu->entry_height;
768 x2 = x1 + MENUW(menu);
769 y2 = y1 + menu->entry_height;
771 new_x = x1;
772 new_y = y1;
773 move = 0;
775 if (x1 < 0) {
776 new_x = 0;
777 move = 1;
778 } else if (x2 >= scr->scr_width) {
779 new_x = scr->scr_width - MENUW(menu) - 1;
780 move = 1;
783 if (y1 < 0) {
784 new_y = 0;
785 move = 1;
786 } else if (y2 >= scr->scr_height) {
787 new_y = scr->scr_height - menu->entry_height - 1;
788 move = 1;
791 new_y = new_y - menu->frame->top_width
792 - menu->selected_entry*menu->entry_height;
793 move_menus(menu, new_x, new_y);
797 static int
798 check_key(WMenu *menu, XKeyEvent *event)
800 int i, ch, s;
801 char buffer[32];
803 if (XLookupString(event, buffer, 32, NULL, NULL)<1)
804 return -1;
806 ch = toupper(buffer[0]);
808 s = (menu->selected_entry>=0 ? menu->selected_entry+1 : 0);
810 again:
811 for (i=s; i<menu->entry_no; i++) {
812 if (ch==toupper(menu->entries[i]->text[0])) {
813 return i;
816 /* no match. Retry from start, if previous started from a selected entry */
817 if (s!=0) {
818 s = 0;
819 goto again;
821 return -1;
825 static int
826 keyboardMenu(WMenu *menu)
828 XEvent event;
829 KeySym ksym=NoSymbol;
830 int done=0;
831 int index;
832 WMenuEntry *entry;
833 int old_pos_x = menu->frame_x;
834 int old_pos_y = menu->frame_y;
835 int new_x = old_pos_x, new_y = old_pos_y;
836 int scr_width = menu->frame->screen_ptr->scr_width;
837 int scr_height = menu->frame->screen_ptr->scr_height;
839 if (menu->flags.editing)
840 return False;
843 XGrabKeyboard(dpy, menu->frame->core->window, True, GrabModeAsync,
844 GrabModeAsync, CurrentTime);
846 if (menu->frame_y+menu->frame->top_width >= scr_height)
847 new_y = scr_height - menu->frame->top_width;
849 if (menu->frame_x+MENUW(menu) >= scr_width)
850 new_x = scr_width-MENUW(menu)-1;
852 move_menus(menu, new_x, new_y);
854 while (!done && menu->flags.mapped) {
855 XAllowEvents(dpy, AsyncKeyboard, CurrentTime);
856 WMMaskEvent(dpy, ExposureMask|ButtonMotionMask|ButtonPressMask
857 |ButtonReleaseMask|KeyPressMask|KeyReleaseMask
858 |SubstructureNotifyMask, &event);
860 switch (event.type) {
861 case KeyPress:
862 ksym = XLookupKeysym(&event.xkey, 0);
863 switch (ksym) {
864 case XK_Escape:
865 done = 1;
866 break;
868 case XK_Home:
869 case XK_KP_Home:
870 selectEntry(menu, 0);
871 makeVisible(menu);
872 break;
874 case XK_End:
875 case XK_KP_End:
876 selectEntry(menu, menu->entry_no-1);
877 makeVisible(menu);
878 break;
880 case XK_Up:
881 #ifdef ARROWLESS_KBD
882 case XK_k:
883 #endif
884 case XK_KP_Up:
885 if (menu->selected_entry <= 0)
886 selectEntry(menu, menu->entry_no-1);
887 else
888 selectEntry(menu, menu->selected_entry-1);
889 makeVisible(menu);
890 break;
892 case XK_Down:
893 #ifdef ARROWLESS_KBD
894 case XK_j:
895 #endif
896 case XK_KP_Down:
897 if (menu->selected_entry<0)
898 selectEntry(menu, 0);
899 else if (menu->selected_entry == menu->entry_no-1)
900 selectEntry(menu, 0);
901 else if (menu->selected_entry < menu->entry_no-1)
902 selectEntry(menu, menu->selected_entry+1);
903 makeVisible(menu);
904 break;
906 case XK_Right:
907 #ifdef ARROWLESS_KBD
908 case XK_l:
909 #endif
910 case XK_KP_Right:
911 if (menu->selected_entry>=0) {
912 WMenuEntry *entry;
913 entry = menu->entries[menu->selected_entry];
915 if (entry->cascade >= 0 && menu->cascades
916 && menu->cascades[entry->cascade]->entry_no > 0) {
918 XUngrabKeyboard(dpy, CurrentTime);
920 selectEntry(menu->cascades[entry->cascade], 0);
921 if (!keyboardMenu(menu->cascades[entry->cascade]))
922 done = 1;
924 XGrabKeyboard(dpy, menu->frame->core->window, True,
925 GrabModeAsync, GrabModeAsync,
926 CurrentTime);
929 break;
931 case XK_Left:
932 #ifdef ARROWLESS_KBD
933 case XK_h:
934 #endif
935 case XK_KP_Left:
936 if (menu->parent!=NULL && menu->parent->selected_entry>=0) {
937 selectEntry(menu, -1);
938 move_menus(menu, old_pos_x, old_pos_y);
939 return True;
941 break;
943 case XK_Return:
944 done = 2;
945 break;
947 default:
948 index = check_key(menu, &event.xkey);
949 if (index>=0) {
950 selectEntry(menu, index);
953 break;
955 default:
956 if (event.type==ButtonPress)
957 done = 1;
959 WMHandleEvent(&event);
963 XUngrabKeyboard(dpy, CurrentTime);
965 if (done==2 && menu->selected_entry>=0) {
966 entry = menu->entries[menu->selected_entry];
967 } else {
968 entry = NULL;
971 if (entry && entry->callback!=NULL && entry->flags.enabled
972 && entry->cascade < 0) {
973 #if (MENU_BLINK_COUNT > 0)
974 int sel = menu->selected_entry;
975 int i;
977 for (i=0; i<MENU_BLINK_COUNT; i++) {
978 paintEntry(menu, sel, False);
979 XSync(dpy, 0);
980 wusleep(MENU_BLINK_DELAY);
981 paintEntry(menu, sel, True);
982 XSync(dpy, 0);
983 wusleep(MENU_BLINK_DELAY);
985 #endif
986 selectEntry(menu, -1);
988 if (!menu->flags.buttoned) {
989 wMenuUnmap(menu);
990 move_menus(menu, old_pos_x, old_pos_y);
992 closeCascade(menu);
994 (*entry->callback)(menu, entry);
995 } else {
996 if (!menu->flags.buttoned) {
997 wMenuUnmap(menu);
998 move_menus(menu, old_pos_x, old_pos_y);
1000 selectEntry(menu, -1);
1004 /* returns True if returning from a submenu to a parent menu,
1005 * False if exiting from menu */
1006 return False;
1010 void
1011 wMenuMapAt(WMenu *menu, int x, int y, int keyboard)
1013 int scr_width = menu->frame->screen_ptr->scr_width;
1014 int scr_height = menu->frame->screen_ptr->scr_height;
1016 if (!menu->flags.realized) {
1017 menu->flags.realized=1;
1018 wMenuRealize(menu);
1020 if (!menu->flags.mapped) {
1021 if (wPreferences.wrap_menus) {
1022 if (x<0) x = 0;
1023 if (y<0) y = 0;
1024 if (x+MENUW(menu) > scr_width)
1025 x = scr_width - MENUW(menu);
1026 if (y+MENUH(menu) > scr_height)
1027 y = scr_height - MENUH(menu);
1030 XMoveWindow(dpy, menu->frame->core->window, x, y);
1031 menu->frame_x = x;
1032 menu->frame_y = y;
1033 XMapWindow(dpy, menu->frame->core->window);
1034 wRaiseFrame(menu->frame->core);
1035 menu->flags.mapped = 1;
1036 } else {
1037 selectEntry(menu, 0);
1040 if (keyboard)
1041 keyboardMenu(menu);
1045 void
1046 wMenuMap(WMenu *menu)
1048 if (!menu->flags.realized) {
1049 menu->flags.realized=1;
1050 wMenuRealize(menu);
1052 if (menu->flags.app_menu && menu->parent==NULL) {
1053 menu->frame_x = menu->frame->screen_ptr->app_menu_x;
1054 menu->frame_y = menu->frame->screen_ptr->app_menu_y;
1055 XMoveWindow(dpy, menu->frame->core->window, menu->frame_x, menu->frame_y);
1057 XMapWindow(dpy, menu->frame->core->window);
1058 wRaiseFrame(menu->frame->core);
1059 menu->flags.mapped = 1;
1063 void
1064 wMenuUnmap(WMenu *menu)
1066 int i;
1068 XUnmapWindow(dpy, menu->frame->core->window);
1069 if (menu->flags.titled && menu->flags.buttoned) {
1070 wFrameWindowHideButton(menu->frame, WFF_RIGHT_BUTTON);
1072 menu->flags.buttoned = 0;
1073 menu->flags.mapped = 0;
1074 menu->flags.open_to_left = 0;
1076 for (i=0; i<menu->cascade_no; i++) {
1077 if (menu->cascades[i]!=NULL
1078 && menu->cascades[i]->flags.mapped
1079 && !menu->cascades[i]->flags.buttoned) {
1081 wMenuUnmap(menu->cascades[i]);
1084 menu->selected_entry = -1;
1089 void
1090 wMenuPaint(WMenu *menu)
1092 int i;
1094 if (!menu->flags.mapped) {
1095 return;
1098 /* paint entries */
1099 for (i=0; i<menu->entry_no; i++) {
1100 paintEntry(menu, i, i==menu->selected_entry);
1105 void
1106 wMenuSetEnabled(WMenu *menu, int index, int enable)
1108 if (index>=menu->entry_no) return;
1109 menu->entries[index]->flags.enabled=enable;
1110 paintEntry(menu, index, index==menu->selected_entry);
1111 paintEntry(menu->brother, index, index==menu->selected_entry);
1115 /* ====================================================================== */
1118 static void
1119 editEntry(WMenu *menu, WMenuEntry *entry)
1121 WTextInput *text;
1122 XEvent event;
1123 WObjDescriptor *desc;
1124 char *t;
1125 int done = 0;
1126 Window old_focus;
1127 int old_revert;
1129 menu->flags.editing = 1;
1131 text = wTextCreate(menu->menu, 1, menu->entry_height * entry->order,
1132 menu->menu->width - 2, menu->entry_height - 1);
1134 wTextPutText(text, entry->text);
1135 XGetInputFocus(dpy, &old_focus, &old_revert);
1136 XSetInputFocus(dpy, text->core->window, RevertToNone, CurrentTime);
1138 if (XGrabKeyboard(dpy, text->core->window, True, GrabModeAsync,
1139 GrabModeAsync, CurrentTime)!=GrabSuccess) {
1140 wwarning("could not grab keyboard");
1141 wTextDestroy(text);
1143 wSetFocusTo(menu->frame->screen_ptr,
1144 menu->frame->screen_ptr->focused_window);
1145 return;
1149 while (!done && !text->done) {
1150 XSync(dpy, 0);
1151 XAllowEvents(dpy, AsyncKeyboard|AsyncPointer, CurrentTime);
1152 XSync(dpy, 0);
1153 WMNextEvent(dpy, &event);
1155 if (XFindContext(dpy, event.xany.window, wWinContext,
1156 (XPointer *)&desc)==XCNOENT)
1157 desc = NULL;
1159 if ((desc != NULL) && (desc->handle_anything != NULL)) {
1161 (*desc->handle_anything)(desc, &event);
1163 } else {
1164 switch (event.type) {
1165 case ButtonPress:
1166 XAllowEvents(dpy, ReplayPointer, CurrentTime);
1167 done = 1;
1169 default:
1170 WMHandleEvent(&event);
1171 break;
1176 XSetInputFocus(dpy, old_focus, old_revert, CurrentTime);
1178 wSetFocusTo(menu->frame->screen_ptr,
1179 menu->frame->screen_ptr->focused_window);
1182 t = wTextGetText(text);
1183 /* if !t, the user has canceled editing */
1184 if (t) {
1185 if (entry->text)
1186 free(entry->text);
1187 entry->text = wstrdup(t);
1189 menu->flags.realized = 0;
1191 wTextDestroy(text);
1193 XUngrabKeyboard(dpy, CurrentTime);
1195 if (t && menu->on_edit)
1196 (*menu->on_edit)(menu, entry);
1198 menu->flags.editing = 0;
1200 if (!menu->flags.realized)
1201 wMenuRealize(menu);
1205 static void
1206 selectEntry(WMenu *menu, int entry_no)
1208 WMenuEntry *entry;
1209 WMenu *submenu;
1210 int old_entry;
1212 if (menu->entries==NULL)
1213 return;
1215 if (entry_no >= menu->entry_no)
1216 return;
1218 old_entry = menu->selected_entry;
1219 menu->selected_entry = entry_no;
1221 if (old_entry!=entry_no) {
1223 /* unselect previous entry */
1224 if (old_entry>=0) {
1225 paintEntry(menu, old_entry, False);
1226 entry = menu->entries[old_entry];
1228 /* unmap cascade */
1229 if (entry->cascade>=0 && menu->cascades) {
1230 if (!menu->cascades[entry->cascade]->flags.buttoned) {
1231 wMenuUnmap(menu->cascades[entry->cascade]);
1236 if (entry_no<0) {
1237 menu->selected_entry = -1;
1238 return;
1240 entry = menu->entries[entry_no];
1242 if (entry->cascade>=0 && menu->cascades && entry->flags.enabled) {
1243 /* Callback for when the submenu is opened.
1245 submenu = menu->cascades[entry->cascade];
1246 if (submenu && submenu->flags.brother)
1247 submenu = submenu->brother;
1249 if (entry->callback) {
1250 /* Only call the callback if the submenu is not yet mapped.
1252 if (menu->flags.brother) {
1253 if (!submenu || !submenu->flags.mapped)
1254 (*entry->callback)(menu->brother, entry);
1255 } else {
1256 if (!submenu || !submenu->flags.buttoned)
1257 (*entry->callback)(menu, entry);
1261 /* the submenu menu might have changed */
1262 submenu = menu->cascades[entry->cascade];
1264 /* map cascade */
1265 if (!submenu->flags.mapped) {
1266 int x, y;
1268 if (!submenu->flags.realized)
1269 wMenuRealize(submenu);
1270 if (wPreferences.wrap_menus) {
1271 if (menu->flags.open_to_left)
1272 submenu->flags.open_to_left = 1;
1274 if (submenu->flags.open_to_left) {
1275 x = menu->frame_x - MENUW(submenu);
1276 if (x<0) {
1277 x = 0;
1278 submenu->flags.open_to_left = 0;
1280 } else {
1281 x = menu->frame_x + MENUW(menu);
1283 if (x + MENUW(submenu)
1284 >= menu->frame->screen_ptr->scr_width) {
1286 x = menu->frame_x - MENUW(submenu);
1287 submenu->flags.open_to_left = 1;
1290 } else {
1291 x = menu->frame_x + MENUW(menu);
1294 if (wPreferences.align_menus) {
1295 y = menu->frame_y;
1296 } else {
1297 y = menu->frame_y + menu->entry_height*entry_no;
1298 if (menu->flags.titled)
1299 y += menu->frame->top_width;
1300 if (menu->cascades[entry->cascade]->flags.titled)
1301 y -= menu->cascades[entry->cascade]->frame->top_width;
1304 wMenuMapAt(menu->cascades[entry->cascade], x, y, False);
1305 menu->cascades[entry->cascade]->parent = menu;
1306 } else {
1307 return;
1310 paintEntry(menu, entry_no, True);
1315 static WMenu*
1316 findMenu(WScreen *scr, int *x_ret, int *y_ret)
1318 WMenu *menu;
1319 WObjDescriptor *desc;
1320 Window root_ret, win, junk_win;
1321 int x, y, wx, wy;
1322 unsigned int mask;
1324 XQueryPointer(dpy, scr->root_win, &root_ret, &win, &x, &y, &wx, &wy,
1325 &mask);
1327 if (win==None) return NULL;
1329 if (XFindContext(dpy, win, wWinContext, (XPointer *)&desc)==XCNOENT)
1330 return NULL;
1332 if (desc->parent_type == WCLASS_MENU) {
1333 menu = (WMenu*)desc->parent;
1334 XTranslateCoordinates(dpy, root_ret, menu->menu->window, wx, wy,
1335 x_ret, y_ret, &junk_win);
1336 return menu;
1338 return NULL;
1344 static void
1345 closeCascade(WMenu *menu)
1347 WMenu *parent=menu->parent;
1349 if (menu->flags.brother
1350 || (!menu->flags.buttoned
1351 && (!menu->flags.app_menu||menu->parent!=NULL))) {
1353 selectEntry(menu, -1);
1354 XSync(dpy, 0);
1355 #if (MENU_BLINK_DELAY > 2)
1356 wusleep(MENU_BLINK_DELAY/2);
1357 #endif
1358 wMenuUnmap(menu);
1359 while (parent!=NULL
1360 && (parent->parent!=NULL || !parent->flags.app_menu
1361 || parent->flags.brother)
1362 && !parent->flags.buttoned) {
1363 selectEntry(parent, -1);
1364 wMenuUnmap(parent);
1365 parent = parent->parent;
1367 if (parent)
1368 selectEntry(parent, -1);
1373 static void
1374 closeBrotherCascadesOf(WMenu *menu)
1376 WMenu *tmp;
1377 int i;
1379 for (i=0; i<menu->cascade_no; i++) {
1380 if (menu->cascades[i]->flags.brother) {
1381 tmp = menu->cascades[i];
1382 } else {
1383 tmp = menu->cascades[i]->brother;
1385 if (tmp->flags.mapped) {
1386 selectEntry(tmp->parent, -1);
1387 closeBrotherCascadesOf(tmp);
1388 break;
1394 #define getEntryAt(menu, x, y) ((y)<0 ? -1 : (y)/(menu->entry_height))
1397 static WMenu*
1398 parentMenu(WMenu *menu)
1400 WMenu *parent;
1401 WMenuEntry *entry;
1403 if (menu->flags.buttoned)
1404 return menu;
1406 while (menu->parent && menu->parent->flags.mapped) {
1407 parent = menu->parent;
1408 if (parent->selected_entry < 0)
1409 break;
1410 entry = parent->entries[parent->selected_entry];
1411 if (!entry->flags.enabled || entry->cascade<0 || !parent->cascades ||
1412 parent->cascades[entry->cascade] != menu)
1413 break;
1414 menu = parent;
1415 if (menu->flags.buttoned)
1416 break;
1419 return menu;
1425 * Will raise the passed menu, if submenu = 0
1426 * If submenu > 0 will also raise all mapped submenus
1427 * until the first buttoned one
1428 * If submenu < 0 will also raise all mapped parent menus
1429 * until the first buttoned one
1432 static void
1433 raiseMenus(WMenu *menu, int submenus)
1435 WMenu *submenu;
1436 int i;
1438 if(!menu) return;
1440 wRaiseFrame(menu->frame->core);
1442 if (submenus>0 && menu->selected_entry>=0) {
1443 i = menu->entries[menu->selected_entry]->cascade;
1444 if (i>=0 && menu->cascades) {
1445 submenu = menu->cascades[i];
1446 if (submenu->flags.mapped && !submenu->flags.buttoned)
1447 raiseMenus(submenu, submenus);
1450 if (submenus<0 && !menu->flags.buttoned &&
1451 menu->parent && menu->parent->flags.mapped)
1452 raiseMenus(menu->parent, submenus);
1456 WMenu*
1457 wMenuUnderPointer(WScreen *screen)
1459 WObjDescriptor *desc;
1460 Window root_ret, win;
1461 int dummy;
1462 unsigned int mask;
1464 XQueryPointer(dpy, screen->root_win, &root_ret, &win, &dummy, &dummy,
1465 &dummy, &dummy, &mask);
1467 if (win==None) return NULL;
1469 if (XFindContext(dpy, win, wWinContext, (XPointer *)&desc)==XCNOENT)
1470 return NULL;
1472 if (desc->parent_type == WCLASS_MENU)
1473 return (WMenu *)desc->parent;
1474 return NULL;
1479 #define MIN(a,b) (((a) > (b)) ? (b) : (a))
1482 static void
1483 getPointerPosition(WScreen *scr, int *x, int *y)
1485 Window root_ret, win;
1486 int wx, wy;
1487 unsigned int mask;
1489 XQueryPointer(dpy, scr->root_win, &root_ret, &win, x, y, &wx, &wy, &mask);
1493 static void
1494 getScrollAmount(WMenu *menu, int *hamount, int *vamount)
1496 WScreen *scr = menu->menu->screen_ptr;
1497 int menuX1 = menu->frame_x;
1498 int menuY1 = menu->frame_y;
1499 int menuX2 = menu->frame_x + MENUW(menu);
1500 int menuY2 = menu->frame_y + MENUH(menu);
1501 int screenW = scr->scr_width;
1502 int screenH = scr->scr_height;
1503 int xroot, yroot;
1505 *hamount = 0;
1506 *vamount = 0;
1508 getPointerPosition(scr, &xroot, &yroot);
1511 if (xroot <= 1 && menuX1 < 0) {
1512 /* scroll to the right */
1513 *hamount = MIN(MENU_SCROLL_STEP, abs(menuX1));
1515 } else if (xroot >= screenW-2 && menuX2 > screenW-1) {
1516 /* scroll to the left */
1517 *hamount = MIN(MENU_SCROLL_STEP, abs(menuX2-screenW-1));
1519 if (*hamount==0)
1520 *hamount = 1;
1522 *hamount = -*hamount;
1525 if (yroot <= 1 && menuY1 < 0) {
1526 /* scroll down */
1527 *vamount = MIN(MENU_SCROLL_STEP, abs(menuY1));
1529 } else if (yroot >= screenH-2 && menuY2 > screenH-1) {
1530 /* scroll up */
1531 *vamount = MIN(MENU_SCROLL_STEP, abs(menuY2-screenH-2));
1533 *vamount = -*vamount;
1538 static void
1539 dragScrollMenuCallback(void *data)
1541 WMenu *menu = (WMenu*)data;
1542 WScreen *scr = menu->menu->screen_ptr;
1543 WMenu *parent = parentMenu(menu);
1544 int hamount, vamount;
1545 int x, y;
1546 int newSelectedEntry;
1548 getScrollAmount(menu, &hamount, &vamount);
1551 if (hamount != 0 || vamount != 0) {
1552 wMenuMove(parent, parent->frame_x + hamount,
1553 parent->frame_y + vamount, True);
1554 if (findMenu(scr, &x, &y)) {
1555 newSelectedEntry = getEntryAt(menu, x, y);
1556 selectEntry(menu, newSelectedEntry);
1557 } else {
1558 /* Pointer fell outside of menu. If the selected entry is
1559 * not a submenu, unselect it */
1560 if (menu->selected_entry >= 0
1561 && menu->entries[menu->selected_entry]->cascade<0)
1562 selectEntry(menu, -1);
1563 newSelectedEntry = 0;
1566 /* paranoid check */
1567 if (newSelectedEntry >= 0) {
1568 /* keep scrolling */
1569 menu->timer = WMAddTimerHandler(MENU_SCROLL_DELAY,
1570 dragScrollMenuCallback, menu);
1571 } else {
1572 menu->timer = NULL;
1574 } else {
1575 /* don't need to scroll anymore */
1576 menu->timer = NULL;
1577 if (findMenu(scr, &x, &y)) {
1578 newSelectedEntry = getEntryAt(menu, x, y);
1579 selectEntry(menu, newSelectedEntry);
1585 static void
1586 scrollMenuCallback(void *data)
1588 WMenu *menu = (WMenu*)data;
1589 WMenu *parent = parentMenu(menu);
1590 int hamount = 0; /* amount to scroll */
1591 int vamount = 0;
1593 getScrollAmount(menu, &hamount, &vamount);
1595 if (hamount != 0 || vamount != 0) {
1596 wMenuMove(parent, parent->frame_x + hamount,
1597 parent->frame_y + vamount, True);
1599 /* keep scrolling */
1600 menu->timer = WMAddTimerHandler(MENU_SCROLL_DELAY,
1601 scrollMenuCallback, menu);
1602 } else {
1603 /* don't need to scroll anymore */
1604 menu->timer = NULL;
1610 #define MENU_SCROLL_BORDER 5
1612 static int
1613 isPointNearBoder(WMenu *menu, int x, int y)
1615 int menuX1 = menu->frame_x;
1616 int menuY1 = menu->frame_y;
1617 int menuX2 = menu->frame_x + MENUW(menu);
1618 int menuY2 = menu->frame_y + MENUH(menu);
1619 int scrXe = menu->menu->screen_ptr->scr_width-1;
1620 int scrYe = menu->menu->screen_ptr->scr_height-1;
1621 int flag = 0;
1623 if (x >= menuX1 && x <= menuX2 && (y < MENU_SCROLL_BORDER
1624 || y > scrYe-MENU_SCROLL_BORDER))
1625 flag = 1;
1626 else if (y >= menuY1 && y <= menuY2 && (x < MENU_SCROLL_BORDER
1627 || x > scrXe-MENU_SCROLL_BORDER))
1628 flag = 1;
1630 return flag;
1634 void
1635 wMenuScroll(WMenu *menu, XEvent *event)
1637 WMenu *smenu;
1638 WMenu *omenu = parentMenu(menu);
1639 WScreen *scr = menu->frame->screen_ptr;
1640 int done = 0;
1641 int jump_back = 0;
1642 int old_frame_x = omenu->frame_x;
1643 int old_frame_y = omenu->frame_y;
1644 XEvent ev;
1646 #ifdef DEBUG
1647 puts("Entering menu Scroll");
1648 #endif
1650 if ((/*omenu->flags.buttoned &&*/ !wPreferences.wrap_menus)
1651 || omenu->flags.app_menu) {
1652 jump_back = 1;
1655 if (!wPreferences.wrap_menus)
1656 raiseMenus(omenu, True);
1657 else
1658 raiseMenus(menu, False);
1660 if (!menu->timer)
1661 scrollMenuCallback(menu);
1663 while(!done) {
1664 int x, y, on_border, on_x_edge, on_y_edge, on_title;
1666 WMNextEvent(dpy, &ev);
1667 switch (ev.type) {
1668 case EnterNotify:
1669 WMHandleEvent(&ev);
1670 case MotionNotify:
1671 x = (ev.type==MotionNotify) ? ev.xmotion.x_root : ev.xcrossing.x_root;
1672 y = (ev.type==MotionNotify) ? ev.xmotion.y_root : ev.xcrossing.y_root;
1674 /* on_border is != 0 if the pointer is between the menu
1675 * and the screen border and is close enough to the border */
1676 on_border = isPointNearBoder(menu, x, y);
1678 smenu = wMenuUnderPointer(scr);
1680 if ((smenu==NULL && !on_border) || (smenu && parentMenu(smenu)!=omenu)) {
1681 done = 1;
1682 break;
1685 on_x_edge = x <= 1 || x >= scr->scr_width - 2;
1686 on_y_edge = y <= 1 || y >= scr->scr_height - 2;
1687 on_border = on_x_edge || on_y_edge;
1689 if (!on_border && !jump_back) {
1690 done = 1;
1691 break;
1694 if (menu->timer && (smenu!=menu || (!on_y_edge && !on_x_edge))) {
1695 WMDeleteTimerHandler(menu->timer);
1696 menu->timer = NULL;
1699 if (smenu != NULL)
1700 menu = smenu;
1702 if (!menu->timer)
1703 scrollMenuCallback(menu);
1704 break;
1705 case ButtonPress:
1706 /* True if we push on title, or drag the omenu to other position */
1707 on_title = ev.xbutton.x_root >= omenu->frame_x &&
1708 ev.xbutton.x_root <= omenu->frame_x + MENUW(omenu) &&
1709 ev.xbutton.y_root >= omenu->frame_y &&
1710 ev.xbutton.y_root <= omenu->frame_y + omenu->frame->top_width;
1711 WMHandleEvent(&ev);
1712 smenu = wMenuUnderPointer(scr);
1713 if (smenu == NULL || (smenu && smenu->flags.buttoned && smenu != omenu))
1714 done = 1;
1715 else if (smenu==omenu && on_title) {
1716 jump_back = 0;
1717 done = 1;
1719 break;
1720 case KeyPress:
1721 done = 1;
1722 default:
1723 WMHandleEvent(&ev);
1724 break;
1728 if (menu->timer) {
1729 WMDeleteTimerHandler(menu->timer);
1730 menu->timer = NULL;
1733 if (jump_back)
1734 wMenuMove(omenu, old_frame_x, old_frame_y, True);
1736 #ifdef DEBUG
1737 puts("Leaving menu Scroll");
1738 #endif
1743 static void
1744 menuExpose(WObjDescriptor *desc, XEvent *event)
1746 wMenuPaint(desc->parent);
1749 typedef struct {
1750 int *delayed_select;
1751 WMenu *menu;
1752 WMHandlerID magic;
1753 } delay_data;
1756 static void
1757 delaySelection(void *data)
1759 delay_data *d = (delay_data*)data;
1760 int x, y, entry_no;
1761 WMenu *menu;
1763 d->magic = NULL;
1765 menu = findMenu(d->menu->menu->screen_ptr, &x, &y);
1766 if (menu && (d->menu == menu || d->delayed_select)) {
1767 entry_no = getEntryAt(menu, x, y);
1768 selectEntry(menu, entry_no);
1770 if (d->delayed_select)
1771 *(d->delayed_select) = 0;
1775 static void
1776 menuMouseDown(WObjDescriptor *desc, XEvent *event)
1778 XButtonEvent *bev = &event->xbutton;
1779 WMenu *menu = desc->parent;
1780 WMenu *smenu;
1781 WScreen *scr=menu->frame->screen_ptr;
1782 WMenuEntry *entry=NULL;
1783 XEvent ev;
1784 int close_on_exit=0;
1785 int done=0;
1786 int delayed_select = 0;
1787 int entry_no;
1788 int x, y;
1789 int prevx, prevy;
1790 int old_frame_x = 0;
1791 int old_frame_y = 0;
1792 delay_data d_data = {NULL, NULL, NULL};
1794 if (!wPreferences.wrap_menus) {
1795 smenu = parentMenu(menu);
1796 old_frame_x = smenu->frame_x;
1797 old_frame_y = smenu->frame_y;
1799 else if (event->xbutton.window == menu->frame->core->window) {
1800 /* This is true if the menu was launched with right click on root window */
1801 delayed_select = 1;
1802 d_data.delayed_select = &delayed_select;
1803 d_data.menu = menu;
1804 d_data.magic = WMAddTimerHandler(wPreferences.dblclick_time,
1805 delaySelection, &d_data);
1808 wRaiseFrame(menu->frame->core);
1810 close_on_exit = (bev->send_event || menu->flags.brother);
1812 smenu = findMenu(scr, &x, &y);
1813 if (!smenu) {
1814 x = -1;
1815 y = -1;
1816 } else {
1817 menu = smenu;
1820 if (menu->flags.editing) {
1821 return;
1823 entry_no = getEntryAt(menu, x, y);
1824 if (entry_no>=0) {
1825 entry = menu->entries[entry_no];
1827 if (!close_on_exit && (bev->state & ControlMask) && smenu
1828 && entry->flags.editable) {
1829 editEntry(smenu, entry);
1830 return;
1831 } else if (bev->state & ControlMask) {
1832 return;
1835 if (entry->flags.enabled && entry->cascade>=0 && menu->cascades) {
1836 WMenu *submenu = menu->cascades[entry->cascade];
1837 /* map cascade */
1838 if (submenu->flags.mapped && !submenu->flags.buttoned &&
1839 menu->selected_entry!=entry_no) {
1840 wMenuUnmap(submenu);
1842 if (!submenu->flags.mapped && !delayed_select) {
1843 selectEntry(menu, entry_no);
1844 } else if (!submenu->flags.buttoned) {
1845 selectEntry(menu, -1);
1848 } else if (!delayed_select) {
1849 selectEntry(menu, entry_no);
1852 if (!wPreferences.wrap_menus && !wPreferences.scrollable_menus) {
1853 if (!menu->timer)
1854 dragScrollMenuCallback(menu);
1858 prevx = bev->x_root;
1859 prevy = bev->y_root;
1860 while (!done) {
1861 int x, y;
1863 XAllowEvents(dpy, SyncPointer, CurrentTime);
1865 WMMaskEvent(dpy, ExposureMask|ButtonMotionMask|ButtonReleaseMask
1866 |ButtonPressMask, &ev);
1867 switch (ev.type) {
1868 case MotionNotify:
1869 smenu = findMenu(scr, &x, &y);
1871 if (smenu == NULL) {
1872 /* moved mouse out of menu */
1874 if (!delayed_select && d_data.magic) {
1875 WMDeleteTimerHandler(d_data.magic);
1876 d_data.magic = NULL;
1878 if (menu==NULL
1879 || (menu->selected_entry>=0
1880 && menu->entries[menu->selected_entry]->cascade>=0)) {
1881 prevx = ev.xmotion.x_root;
1882 prevy = ev.xmotion.y_root;
1884 break;
1886 selectEntry(menu, -1);
1887 menu = smenu;
1888 prevx = ev.xmotion.x_root;
1889 prevy = ev.xmotion.y_root;
1890 break;
1891 } else if (menu && menu!=smenu
1892 && (menu->selected_entry<0
1893 || menu->entries[menu->selected_entry]->cascade<0)) {
1894 selectEntry(menu, -1);
1896 if (!delayed_select && d_data.magic) {
1897 WMDeleteTimerHandler(d_data.magic);
1898 d_data.magic = NULL;
1900 } else {
1902 /* hysteresis for item selection */
1904 /* check if the motion was to the side, indicating that
1905 * the user may want to cross to a submenu */
1906 if (!delayed_select && menu) {
1907 int dx;
1908 Bool moved_to_submenu;/* moved to direction of submenu */
1910 dx = abs(prevx - ev.xmotion.x_root);
1912 moved_to_submenu = False;
1913 if (dx > 0 /* if moved enough to the side */
1914 /* maybe a open submenu */
1915 && menu->selected_entry>=0
1916 /* moving to the right direction */
1917 && (wPreferences.align_menus
1918 || ev.xmotion.y_root >= prevy)) {
1919 int index;
1921 index = menu->entries[menu->selected_entry]->cascade;
1922 if (index>=0) {
1923 if (menu->cascades[index]->frame_x>menu->frame_x) {
1924 if (prevx < ev.xmotion.x_root)
1925 moved_to_submenu = True;
1926 } else {
1927 if (prevx > ev.xmotion.x_root)
1928 moved_to_submenu = True;
1934 if (menu != smenu) {
1935 if (d_data.magic) {
1936 WMDeleteTimerHandler(d_data.magic);
1938 d_data.magic = NULL;
1939 } else if (moved_to_submenu) {
1940 /* while we are moving, postpone the selection */
1941 if (d_data.magic) {
1942 WMDeleteTimerHandler(d_data.magic);
1944 d_data.delayed_select = NULL;
1945 d_data.menu = menu;
1946 d_data.magic = WMAddTimerHandler(MENU_SELECT_DELAY,
1947 delaySelection,
1948 &d_data);
1949 prevx = ev.xmotion.x_root;
1950 prevy = ev.xmotion.y_root;
1951 break;
1952 } else {
1953 if (d_data.magic)
1954 WMDeleteTimerHandler(d_data.magic);
1955 d_data.magic = NULL;
1959 prevx = ev.xmotion.x_root;
1960 prevy = ev.xmotion.y_root;
1961 if (menu!=smenu) {
1962 /* pointer crossed menus */
1963 if (menu && menu->timer) {
1964 WMDeleteTimerHandler(menu->timer);
1965 menu->timer = NULL;
1967 if (smenu)
1968 dragScrollMenuCallback(smenu);
1970 menu = smenu;
1971 if (!menu->timer)
1972 dragScrollMenuCallback(menu);
1974 if (!delayed_select) {
1975 entry_no = getEntryAt(menu, x, y);
1976 if (entry_no>=0) {
1977 entry = menu->entries[entry_no];
1978 if (entry->flags.enabled && entry->cascade>=0 &&
1979 menu->cascades) {
1980 WMenu *submenu = menu->cascades[entry->cascade];
1981 if (submenu->flags.mapped && !submenu->flags.buttoned
1982 && menu->selected_entry!=entry_no) {
1983 wMenuUnmap(submenu);
1987 selectEntry(menu, entry_no);
1989 break;
1991 case ButtonPress:
1992 break;
1994 case ButtonRelease:
1995 if (ev.xbutton.button == event->xbutton.button)
1996 done=1;
1997 break;
1999 case Expose:
2000 WMHandleEvent(&ev);
2001 break;
2005 if (menu && menu->timer) {
2006 WMDeleteTimerHandler(menu->timer);
2007 menu->timer = NULL;
2009 if (d_data.magic!=NULL)
2010 WMDeleteTimerHandler(d_data.magic);
2012 if (menu && menu->selected_entry>=0) {
2013 entry = menu->entries[menu->selected_entry];
2014 if (entry->callback!=NULL && entry->flags.enabled
2015 && entry->cascade < 0) {
2016 /* blink and erase menu selection */
2017 #if (MENU_BLINK_DELAY > 0)
2018 int sel = menu->selected_entry;
2019 int i;
2021 for (i=0; i<MENU_BLINK_COUNT; i++) {
2022 paintEntry(menu, sel, False);
2023 XSync(dpy, 0);
2024 wusleep(MENU_BLINK_DELAY);
2025 paintEntry(menu, sel, True);
2026 XSync(dpy, 0);
2027 wusleep(MENU_BLINK_DELAY);
2029 #endif
2030 /* unmap the menu, it's parents and call the callback */
2031 if (!menu->flags.buttoned &&
2032 (!menu->flags.app_menu||menu->parent!=NULL)) {
2033 closeCascade(menu);
2034 } else {
2035 selectEntry(menu, -1);
2037 (*entry->callback)(menu, entry);
2039 /* If the user double clicks an entry, the entry will
2040 * be executed twice, which is not good for things like
2041 * the root menu. So, ignore any clicks that were generated
2042 * while the entry was being executed */
2043 while (XCheckTypedWindowEvent(dpy, menu->menu->window,
2044 ButtonPress, &ev));
2045 } else if (entry->callback!=NULL && entry->cascade<0) {
2046 selectEntry(menu, -1);
2047 } else {
2048 if (entry->cascade>=0 && menu->cascades
2049 && menu->cascades[entry->cascade]->flags.brother) {
2050 selectEntry(menu, -1);
2055 if (((WMenu*)desc->parent)->flags.brother || close_on_exit
2056 || !smenu)
2057 closeCascade(desc->parent);
2059 /* close the cascade windows that should not remain opened */
2060 closeBrotherCascadesOf(desc->parent);
2062 if (!wPreferences.wrap_menus)
2063 wMenuMove(parentMenu(desc->parent), old_frame_x, old_frame_y, True);
2067 void
2068 wMenuMove(WMenu *menu, int x, int y, int submenus)
2070 WMenu *submenu;
2071 int i;
2073 if (!menu) return;
2075 menu->frame_x = x;
2076 menu->frame_y = y;
2077 XMoveWindow(dpy, menu->frame->core->window, x, y);
2079 if (submenus>0 && menu->selected_entry>=0) {
2080 i = menu->entries[menu->selected_entry]->cascade;
2082 if (i>=0 && menu->cascades) {
2083 submenu = menu->cascades[i];
2084 if (submenu->flags.mapped && !submenu->flags.buttoned)
2085 if (wPreferences.align_menus) {
2086 wMenuMove(submenu, x + MENUW(menu), y, submenus);
2087 } else {
2088 wMenuMove(submenu, x+ MENUW(menu),
2089 y + submenu->entry_height*menu->selected_entry,
2090 submenus);
2094 if (submenus<0 && menu->parent!=NULL && menu->parent->flags.mapped &&
2095 !menu->parent->flags.buttoned) {
2096 if (wPreferences.align_menus) {
2097 wMenuMove(menu->parent, x - MENUW(menu->parent), y, submenus);
2098 } else {
2099 wMenuMove(menu->parent, x - MENUW(menu->parent), menu->frame_y
2100 - menu->parent->entry_height*menu->parent->selected_entry,
2101 submenus);
2107 static void
2108 changeMenuLevels(WMenu *menu, int lower)
2110 int i;
2112 if (!lower) {
2113 ChangeStackingLevel(menu->frame->core, (!menu->parent ? WMMainMenuLevel
2114 : WMSubmenuLevel));
2115 wRaiseFrame(menu->frame->core);
2116 menu->flags.lowered = 0;
2117 } else {
2118 ChangeStackingLevel(menu->frame->core, WMNormalLevel);
2119 wLowerFrame(menu->frame->core);
2120 menu->flags.lowered = 1;
2122 for (i=0; i<menu->cascade_no; i++) {
2123 if (menu->cascades[i]
2124 && !menu->cascades[i]->flags.buttoned
2125 && menu->cascades[i]->flags.lowered!=lower) {
2126 changeMenuLevels(menu->cascades[i], lower);
2133 static void
2134 menuTitleDoubleClick(WCoreWindow *sender, void *data, XEvent *event)
2136 WMenu *menu = data;
2137 int lower;
2139 if (event->xbutton.state & MOD_MASK) {
2140 if (menu->flags.lowered) {
2141 lower = 0;
2142 } else {
2143 lower = 1;
2145 changeMenuLevels(menu, lower);
2150 static void
2151 menuTitleMouseDown(WCoreWindow *sender, void *data, XEvent *event)
2153 WMenu *menu = data;
2154 WMenu *tmp;
2155 XEvent ev;
2156 int x=menu->frame_x, y=menu->frame_y;
2157 int dx=event->xbutton.x_root, dy=event->xbutton.y_root;
2158 int i, lower;
2159 Bool started;
2161 #ifdef DEBUG
2162 printf("Moving menu\n");
2163 #endif
2165 /* can't touch the menu copy */
2166 if (menu->flags.brother)
2167 return;
2169 if (event->xbutton.button != Button1 && event->xbutton.button != Button2)
2170 return;
2172 if (event->xbutton.state & MOD_MASK) {
2173 wLowerFrame(menu->frame->core);
2174 lower = 1;
2175 } else {
2176 wRaiseFrame(menu->frame->core);
2177 lower = 0;
2179 tmp = menu;
2181 /* lower/raise all submenus */
2182 while (1) {
2183 if (tmp->selected_entry>=0 && tmp->cascades
2184 && tmp->entries[tmp->selected_entry]->cascade>=0) {
2185 tmp = tmp->cascades[tmp->entries[tmp->selected_entry]->cascade];
2186 if (!tmp || !tmp->flags.mapped)
2187 break;
2188 if (lower)
2189 wLowerFrame(tmp->frame->core);
2190 else
2191 wRaiseFrame(tmp->frame->core);
2192 } else {
2193 break;
2197 /* tear off the menu if it's a root menu or a cascade
2198 application menu */
2199 if (!menu->flags.buttoned && !menu->flags.brother
2200 && (!menu->flags.app_menu||menu->parent!=NULL)) {
2201 menu->flags.buttoned=1;
2202 wFrameWindowShowButton(menu->frame, WFF_RIGHT_BUTTON);
2203 if (menu->parent) {
2204 /* turn off selected menu entry in parent menu */
2205 selectEntry(menu->parent, -1);
2207 /* make parent map the copy in place of the original */
2208 for (i=0; i<menu->parent->cascade_no; i++) {
2209 if (menu->parent->cascades[i] == menu) {
2210 menu->parent->cascades[i] = menu->brother;
2211 break;
2217 started = False;
2218 while(1) {
2219 WMMaskEvent(dpy, ButtonMotionMask|ButtonReleaseMask|ButtonPressMask
2220 |ExposureMask, &ev);
2221 switch (ev.type) {
2222 case MotionNotify:
2223 if (started) {
2224 x += ev.xmotion.x_root - dx;
2225 y += ev.xmotion.y_root - dy;
2226 dx = ev.xmotion.x_root;
2227 dy = ev.xmotion.y_root;
2228 wMenuMove(menu, x, y, True);
2229 } else {
2230 if (abs(ev.xmotion.x_root - dx) > MOVE_THRESHOLD
2231 || abs(ev.xmotion.y_root - dy) > MOVE_THRESHOLD) {
2232 started = True;
2233 XGrabPointer(dpy, menu->frame->titlebar->window, False,
2234 ButtonMotionMask|ButtonReleaseMask
2235 |ButtonPressMask,
2236 GrabModeAsync, GrabModeAsync, None,
2237 wCursor[WCUR_MOVE], CurrentTime);
2240 break;
2242 case ButtonPress:
2243 break;
2245 case ButtonRelease:
2246 if (ev.xbutton.button != event->xbutton.button)
2247 break;
2248 #ifdef DEBUG
2249 printf("End menu move\n");
2250 #endif
2251 XUngrabPointer(dpy, CurrentTime);
2252 return;
2254 default:
2255 WMHandleEvent(&ev);
2256 break;
2262 *----------------------------------------------------------------------
2263 * menuCloseClick--
2264 * Handles mouse click on the close button of menus. The menu is
2265 * closed when the button is clicked.
2267 * Side effects:
2268 * The closed menu is reinserted at it's parent menus
2269 * cascade list.
2270 *----------------------------------------------------------------------
2272 static void
2273 menuCloseClick(WCoreWindow *sender, void *data, XEvent *event)
2275 WMenu *menu = (WMenu*)data;
2276 WMenu *parent = menu->parent;
2277 int i;
2279 if (parent) {
2280 for (i=0; i<parent->cascade_no; i++) {
2281 /* find the entry that points to the copy */
2282 if (parent->cascades[i] == menu->brother) {
2283 /* make it point to the original */
2284 parent->cascades[i] = menu;
2285 menu->parent = parent;
2286 break;
2290 wMenuUnmap(menu);
2294 void
2295 wMenuSaveState(WScreen *scr)
2297 proplist_t menus, key, value;
2298 int save_menus = 0;
2299 char buffer[256];
2301 menus = PLMakeDictionaryFromEntries(NULL, NULL, NULL);
2303 #ifndef LITE
2304 if (scr->root_menu && scr->root_menu->flags.buttoned) {
2305 sprintf(buffer, "%i,%i", scr->root_menu->frame_x,
2306 scr->root_menu->frame_y);
2307 key = PLMakeString("RootMenu");
2308 value = PLMakeString(buffer);
2309 PLInsertDictionaryEntry(menus, key, value);
2310 PLRelease(key);
2311 PLRelease(value);
2312 save_menus = 1;
2315 if (scr->switch_menu && scr->switch_menu->flags.buttoned) {
2316 sprintf(buffer, "%i,%i", scr->switch_menu->frame_x,
2317 scr->switch_menu->frame_y);
2318 key = PLMakeString("SwitchMenu");
2319 value = PLMakeString(buffer);
2320 PLInsertDictionaryEntry(menus, key, value);
2321 PLRelease(key);
2322 PLRelease(value);
2323 save_menus = 1;
2325 #endif /* !LITE */
2326 if (scr->workspace_menu && scr->workspace_menu->flags.buttoned) {
2327 sprintf(buffer, "%i,%i", scr->workspace_menu->frame_x,
2328 scr->workspace_menu->frame_y);
2329 key = PLMakeString("WorkspaceMenu");
2330 value = PLMakeString(buffer);
2331 PLInsertDictionaryEntry(menus, key, value);
2332 PLRelease(key);
2333 PLRelease(value);
2334 save_menus = 1;
2337 if (save_menus) {
2338 key = PLMakeString("Menus");
2339 PLInsertDictionaryEntry(scr->session_state, key, menus);
2340 PLRelease(key);
2342 PLRelease(menus);
2346 #define COMPLAIN(key) wwarning(_("bad value in menus state info:%s"), key)
2349 static int
2350 restoreMenu(WScreen *scr, proplist_t menu, int which)
2352 int i, x, y;
2353 WMenu *pmenu = NULL;
2355 if (!menu)
2356 return False;
2358 if (!PLIsString(menu)) {
2359 COMPLAIN("Position");
2360 return False;
2363 if (sscanf(PLGetString(menu), "%i,%i", &x, &y)!=2)
2364 COMPLAIN("Position");
2366 #ifndef LITE
2367 if (which & WSS_ROOTMENU) {
2368 OpenRootMenu(scr, x, y, False);
2369 pmenu = scr->root_menu;
2370 } else if (which & WSS_SWITCHMENU) {
2371 OpenSwitchMenu(scr, x, y, False);
2372 pmenu = scr->switch_menu;
2373 } else
2374 #endif /* !LITE */
2375 if (which & WSS_WSMENU) {
2376 OpenWorkspaceMenu(scr, x, y);
2377 pmenu = scr->workspace_menu;
2378 if (pmenu->parent) {
2379 /* make parent map the copy in place of the original */
2380 for (i=0; i<pmenu->parent->cascade_no; i++) {
2381 if (pmenu->parent->cascades[i] == pmenu) {
2382 pmenu->parent->cascades[i] = pmenu->brother;
2383 break;
2389 if (pmenu) {
2390 int width = MENUW(pmenu);
2391 int height = MENUH(pmenu);
2393 x = (x < -width) ? 0 : x;
2394 x = (x > scr->scr_width) ? scr->scr_width - width : x;
2395 y = (y < 0) ? 0 : y;
2396 y = (y > scr->scr_height) ? scr->scr_height - height : y;
2397 wMenuMove(pmenu, x, y, True);
2398 pmenu->flags.buttoned = 1;
2399 wFrameWindowShowButton(pmenu->frame, WFF_RIGHT_BUTTON);
2400 return True;
2402 return False;
2406 void
2407 wMenuRestoreState(WScreen *scr)
2409 proplist_t menus, menu, key, rkey, skey, wkey;
2411 key = PLMakeString("Menus");
2412 menus = PLGetDictionaryEntry(scr->session_state, key);
2413 PLRelease(key);
2415 if (!menus)
2416 return;
2418 /* restore menus */
2420 rkey = PLMakeString("RootMenu");
2421 skey = PLMakeString("SwitchMenu");
2422 wkey = PLMakeString("WorkspaceMenu");
2423 menu = PLGetDictionaryEntry(menus, rkey);
2424 restoreMenu(scr, menu, WSS_ROOTMENU);
2425 menu = PLGetDictionaryEntry(menus, skey);
2426 restoreMenu(scr, menu, WSS_SWITCHMENU);
2427 menu = PLGetDictionaryEntry(menus, wkey);
2428 restoreMenu(scr, menu, WSS_WSMENU);
2430 PLRelease(rkey);
2431 PLRelease(skey);
2432 PLRelease(wkey);
2436 void
2437 OpenWorkspaceMenu(WScreen *scr, int x, int y)
2439 WMenu *menu, *parent;
2440 WMenuEntry *entry;
2442 #ifndef LITE
2443 if (!scr->root_menu) {
2444 OpenRootMenu(scr, scr->scr_width*2, 0, False);
2445 wMenuUnmap(scr->root_menu);
2447 #endif
2448 menu = scr->workspace_menu;
2449 if (menu) {
2450 if (menu->flags.mapped) {
2451 if (!menu->flags.buttoned) {
2452 wMenuUnmap(menu);
2453 parent = menu->parent;
2454 if (parent && parent->selected_entry >= 0) {
2455 entry = parent->entries[parent->selected_entry];
2456 if (parent->cascades[entry->cascade] == menu) {
2457 selectEntry(parent, -1);
2458 wMenuMapAt(menu, x, y, False);
2461 } else {
2462 wRaiseFrame(menu->frame->core);
2463 wMenuMapCopyAt(menu, x, y);
2466 else {
2467 wMenuMapAt(menu, x, y, False);