new/changelog fix
[wmaker-crm.git] / src / menu.c
blobb7abf244f5603ff7f85930cf6dd882bb4c9d9991
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);
84 static void updateTexture(WMenu *menu);
87 static void selectEntry(WMenu *menu, int entry_no);
88 static void closeCascade(WMenu *menu);
91 /****** Notification Observers ******/
93 static void
94 appearanceObserver(void *self, WMNotification *notif)
96 WMenu *menu = (WMenu*)self;
97 int flags = (int)WMGetNotificationClientData(notif);
99 if (!menu->flags.realized)
100 return;
102 if (WMGetNotificationName(notif) == WNMenuAppearanceSettingsChanged) {
103 if (flags & WFontSettings) {
104 menu->flags.realized = 0;
105 wMenuRealize(menu);
107 if (flags & WTextureSettings) {
108 updateTexture(menu);
110 if (flags & (WTextureSettings|WColorSettings)) {
111 wMenuPaint(menu);
113 } else if (menu->flags.titled) {
115 if (flags & WFontSettings) {
116 menu->flags.realized = 0;
117 wMenuRealize(menu);
119 if (flags & WTextureSettings) {
120 menu->frame->flags.need_texture_remake = 1;
122 if (flags & (WColorSettings|WTextureSettings))
123 wFrameWindowPaint(menu->frame);
127 /************************************/
131 *----------------------------------------------------------------------
132 * wMenuCreate--
133 * Creates a new empty menu with the specified title. If main_menu
134 * is True, the created menu will be a main menu, which has some special
135 * properties such as being placed over other normal menus.
136 * If title is NULL, the menu will have no titlebar.
138 * Returns:
139 * The created menu.
140 *----------------------------------------------------------------------
142 WMenu*
143 wMenuCreate(WScreen *screen, char *title, int main_menu)
145 WMenu *menu;
146 static int brother=0;
147 int tmp, flags;
149 menu = wmalloc(sizeof(WMenu));
151 memset(menu, 0, sizeof(WMenu));
153 #ifdef SINGLE_MENULEVEL
154 tmp = WMSubmenuLevel;
155 #else
156 tmp = (main_menu ? WMMainMenuLevel : WMSubmenuLevel);
157 #endif
159 flags = WFF_SINGLE_STATE;
160 if (title) {
161 flags |= WFF_TITLEBAR|WFF_RIGHT_BUTTON;
162 menu->flags.titled = 1;
164 menu->frame =
165 wFrameWindowCreate(screen, tmp, 8, 2, 1, 1, flags,
166 screen->menu_title_texture, NULL,
167 screen->menu_title_pixel, &screen->menu_title_gc,
168 &screen->menu_title_font);
170 menu->frame->core->descriptor.parent = menu;
171 menu->frame->core->descriptor.parent_type = WCLASS_MENU;
172 menu->frame->core->descriptor.handle_mousedown = menuMouseDown;
174 wFrameWindowHideButton(menu->frame, WFF_RIGHT_BUTTON);
176 if (title) {
177 menu->frame->title = wstrdup(title);
180 menu->frame->flags.justification = WTJ_LEFT;
182 menu->frame->rbutton_image = screen->b_pixmaps[WBUT_CLOSE];
184 menu->entry_no = 0;
185 menu->alloced_entries = 0;
186 menu->selected_entry = -1;
187 menu->entries = NULL;
189 menu->frame_x = screen->app_menu_x;
190 menu->frame_y = screen->app_menu_y;
192 menu->frame->child = menu;
194 menu->flags.lowered = 0;
196 /* create borders */
197 if (title) {
198 /* setup object descriptors */
199 menu->frame->on_mousedown_titlebar = menuTitleMouseDown;
200 menu->frame->on_dblclick_titlebar = menuTitleDoubleClick;
203 menu->frame->on_click_right = menuCloseClick;
206 menu->menu = wCoreCreate(menu->frame->core, 0, menu->frame->top_width,
207 menu->frame->core->width, 10);
209 menu->menu->descriptor.parent = menu;
210 menu->menu->descriptor.parent_type = WCLASS_MENU;
211 menu->menu->descriptor.handle_expose = menuExpose;
212 menu->menu->descriptor.handle_mousedown = menuMouseDown;
214 menu->menu_texture_data = None;
216 XMapWindow(dpy, menu->menu->window);
218 XFlush(dpy);
220 if (!brother) {
221 brother = 1;
222 menu->brother = wMenuCreate(screen, title, main_menu);
223 brother = 0;
224 menu->brother->flags.brother = 1;
225 menu->brother->brother = menu;
227 WMAddNotificationObserver(appearanceObserver, menu,
228 WNMenuTitleAppearanceSettingsChanged, menu);
230 WMAddNotificationObserver(appearanceObserver, menu,
231 WNMenuAppearanceSettingsChanged, menu);
234 return menu;
240 WMenu*
241 wMenuCreateForApp(WScreen *screen, char *title, int main_menu)
243 WMenu *menu;
245 menu = wMenuCreate(screen, title, main_menu);
246 if (!menu)
247 return NULL;
248 menu->flags.app_menu = 1;
249 menu->brother->flags.app_menu = 1;
251 return menu;
256 static void
257 insertEntry(WMenu *menu, WMenuEntry *entry, int index)
259 int i;
261 for (i = menu->entry_no-1; i >= index; i--) {
262 menu->entries[i]->order++;
263 menu->entries[i+1] = menu->entries[i];
265 menu->entries[index] = entry;
270 WMenuEntry*
271 wMenuInsertCallback(WMenu *menu, int index, char *text,
272 void (*callback)(WMenu *menu, WMenuEntry *entry),
273 void *clientdata)
275 WMenuEntry *entry;
277 #ifdef DEBUG
278 if (!menu) {
279 printf("Passed NULL as menu parameter to wMenuAddCallback() \n");
280 return NULL;
282 #endif
284 assert(menu->flags.brother==0);
285 menu->flags.realized = 0;
286 menu->brother->flags.realized = 0;
288 /* reallocate array if it's too small */
289 if (menu->entry_no >= menu->alloced_entries) {
290 void *tmp;
291 #ifdef DEBUG
292 puts("doing wrealloc()");
293 #endif
294 tmp = wrealloc(menu->entries,
295 sizeof(WMenuEntry)*(menu->alloced_entries+5));
296 if (tmp==NULL) {
297 wwarning(_("wrealloc() failed while trying to add menu item"));
298 return NULL;
301 menu->entries = tmp;
302 menu->alloced_entries += 5;
304 menu->brother->entries = tmp;
305 menu->brother->alloced_entries = menu->alloced_entries;
307 entry = wmalloc(sizeof(WMenuEntry));
308 memset(entry, 0, sizeof(WMenuEntry));
309 entry->flags.enabled = 1;
310 entry->text = wstrdup(text);
311 entry->cascade = -1;
312 entry->clientdata = clientdata;
313 entry->callback = callback;
314 if (index<0 || index>=menu->entry_no) {
315 entry->order = menu->entry_no;
316 menu->entries[menu->entry_no] = entry;
317 } else {
318 entry->order = index;
319 insertEntry(menu, entry, index);
322 menu->entry_no++;
323 menu->brother->entry_no = menu->entry_no;
325 return entry;
330 void
331 wMenuEntrySetCascade(WMenu *menu, WMenuEntry *entry, WMenu *cascade)
333 WMenu *brother = menu->brother;
334 int i, done;
336 assert(menu->flags.brother==0);
338 if (entry->cascade>=0) {
339 menu->flags.realized = 0;
340 brother->flags.realized = 0;
343 cascade->parent = menu;
345 cascade->brother->parent = brother;
347 done = 0;
348 for (i=0; i<menu->cascade_no; i++) {
349 if (menu->cascades[i]==NULL) {
350 menu->cascades[i] = cascade;
351 brother->cascades[i] = cascade->brother;
352 done = 1;
353 entry->cascade = i;
354 break;
357 if (!done) {
358 entry->cascade = menu->cascade_no;
360 menu->cascades = wrealloc(menu->cascades,
361 sizeof(WMenu)*(menu->cascade_no+1));
362 menu->cascades[menu->cascade_no++] = cascade;
365 brother->cascades = wrealloc(brother->cascades,
366 sizeof(WMenu)*(brother->cascade_no+1));
367 brother->cascades[brother->cascade_no++] = cascade->brother;
371 if (menu->flags.lowered) {
373 cascade->flags.lowered = 1;
374 ChangeStackingLevel(cascade->frame->core, WMNormalLevel);
376 cascade->brother->flags.lowered = 1;
377 ChangeStackingLevel(cascade->brother->frame->core, WMNormalLevel);
380 if (!menu->flags.realized)
381 wMenuRealize(menu);
385 void
386 wMenuEntryRemoveCascade(WMenu *menu, WMenuEntry *entry)
388 assert(menu->flags.brother==0);
390 /* destroy cascade menu */
391 if (entry->cascade>=0 && menu->cascades
392 && menu->cascades[entry->cascade]!=NULL) {
394 wMenuDestroy(menu->cascades[entry->cascade], True);
396 menu->cascades[entry->cascade] = NULL;
397 menu->brother->cascades[entry->cascade] = NULL;
399 entry->cascade = -1;
404 void
405 wMenuRemoveItem(WMenu *menu, int index)
407 int i;
409 if (menu->flags.brother) {
410 wMenuRemoveItem(menu->brother, index);
411 return;
414 if (index>=menu->entry_no) return;
416 /* destroy cascade menu */
417 wMenuEntryRemoveCascade(menu, menu->entries[index]);
419 /* destroy unshared data */
421 if (menu->entries[index]->text)
422 free(menu->entries[index]->text);
424 if (menu->entries[index]->rtext)
425 free(menu->entries[index]->rtext);
427 if (menu->entries[index]->free_cdata && menu->entries[index]->clientdata)
428 (*menu->entries[index]->free_cdata)(menu->entries[index]->clientdata);
430 free(menu->entries[index]);
432 for (i=index; i<menu->entry_no-1; i++) {
433 menu->entries[i+1]->order--;
434 menu->entries[i]=menu->entries[i+1];
436 menu->entry_no--;
437 menu->brother->entry_no--;
441 static void
442 updateTexture(WMenu *menu)
444 WScreen *scr = menu->menu->screen_ptr;
446 /* setup background texture */
447 if (scr->menu_item_texture->any.type != WTEX_SOLID) {
448 if (!menu->flags.brother) {
449 FREE_PIXMAP(menu->menu_texture_data);
451 if (wPreferences.alt_menu_style) {
452 wTextureRender(scr, scr->menu_item_texture,
453 &menu->menu_texture_data, menu->menu->width,
454 menu->menu->height, WREL_MENUENTRY);
455 } else {
456 wTextureRender(scr, scr->menu_item_texture,
457 &menu->menu_texture_data, menu->menu->width,
458 menu->entry_height, WREL_MENUENTRY);
461 XSetWindowBackgroundPixmap(dpy, menu->menu->window,
462 menu->menu_texture_data);
463 XClearWindow(dpy, menu->menu->window);
465 XSetWindowBackgroundPixmap(dpy, menu->brother->menu->window,
466 menu->menu_texture_data);
467 XClearWindow(dpy, menu->brother->menu->window);
469 } else {
470 XSetWindowBackground(dpy, menu->menu->window,
471 scr->menu_item_texture->any.color.pixel);
472 XClearWindow(dpy, menu->menu->window);
477 void
478 wMenuRealize(WMenu *menu)
480 int i;
481 int width, rwidth, mrwidth, mwidth;
482 int theight, twidth, eheight;
483 WScreen *scr = menu->frame->screen_ptr;
484 static int brother_done=0;
485 int flags;
487 if (!brother_done) {
488 brother_done = 1;
489 wMenuRealize(menu->brother);
490 brother_done = 0;
493 flags = WFF_SINGLE_STATE;
494 if (menu->flags.titled)
495 flags |= WFF_TITLEBAR|WFF_RIGHT_BUTTON;
497 wFrameWindowUpdateBorders(menu->frame, flags);
499 if (menu->flags.titled) {
500 twidth = wTextWidth(scr->menu_title_font->font, menu->frame->title,
501 strlen(menu->frame->title));
502 theight = menu->frame->top_width;
503 twidth += theight + (wPreferences.new_style ? 16 : 8);
504 } else {
505 twidth = 0;
506 theight = 0;
508 eheight = scr->menu_entry_font->height + 6;
509 menu->entry_height = eheight;
510 mrwidth = 0;
511 mwidth = 0;
512 for (i=0; i<menu->entry_no; i++) {
513 char *text;
515 /* search widest text */
516 text = menu->entries[i]->text;
517 width = wTextWidth(scr->menu_entry_font->font, text, strlen(text))+10;
519 if (menu->entries[i]->flags.indicator) {
520 width += MENU_INDICATOR_SPACE;
523 if (width > mwidth)
524 mwidth = width;
526 /* search widest text on right */
527 text = menu->entries[i]->rtext;
528 if (text)
529 rwidth = wTextWidth(scr->menu_entry_font->font, text,
530 strlen(text)) + 5;
531 else if (menu->entries[i]->cascade>=0)
532 rwidth = 16;
533 else
534 rwidth = 4;
536 if (rwidth > mrwidth)
537 mrwidth = rwidth;
539 mwidth += mrwidth;
541 if (mwidth < twidth)
542 mwidth = twidth;
545 wCoreConfigure(menu->menu, 0, theight, mwidth, menu->entry_no*eheight -1);
547 wFrameWindowResize(menu->frame, mwidth, menu->entry_no*eheight-1
548 + menu->frame->top_width + menu->frame->bottom_width);
551 updateTexture(menu);
553 menu->flags.realized = 1;
555 if (menu->flags.mapped)
556 wMenuPaint(menu);
557 if (menu->brother->flags.mapped)
558 wMenuPaint(menu->brother);
562 void
563 wMenuDestroy(WMenu *menu, int recurse)
565 int i;
567 WMRemoveNotificationObserver(menu);
569 /* remove any pending timers */
570 if (menu->timer)
571 WMDeleteTimerHandler(menu->timer);
572 menu->timer = NULL;
574 /* call destroy handler */
575 if (menu->on_destroy)
576 (*menu->on_destroy)(menu);
578 /* Destroy items if this menu own them. If this is the "brother" menu,
579 * leave them alone as it is shared by them.
581 if (!menu->flags.brother) {
582 for (i=0; i<menu->entry_no; i++) {
584 free(menu->entries[i]->text);
586 if (menu->entries[i]->rtext)
587 free(menu->entries[i]->rtext);
589 if (menu->entries[i]->free_cdata && menu->entries[i]->clientdata) {
590 (*menu->entries[i]->free_cdata)(menu->entries[i]->clientdata);
592 free(menu->entries[i]);
595 if (recurse) {
596 for (i=0; i<menu->cascade_no; i++) {
597 if (menu->cascades[i]) {
598 if (menu->cascades[i]->flags.brother)
599 wMenuDestroy(menu->cascades[i]->brother, recurse);
600 else
601 wMenuDestroy(menu->cascades[i], recurse);
606 if (menu->entries)
607 free(menu->entries);
611 FREE_PIXMAP(menu->menu_texture_data);
613 if (menu->cascades)
614 free(menu->cascades);
616 wCoreDestroy(menu->menu);
617 wFrameWindowDestroy(menu->frame);
619 /* destroy copy of this menu */
620 if (!menu->flags.brother && menu->brother)
621 wMenuDestroy(menu->brother, False);
623 free(menu);
627 static void
628 drawFrame(WScreen *scr, Window win, int y, int w, int h)
630 XSegment segs[2];
632 segs[0].x1 = 0;
633 segs[0].y1 = y;
634 segs[0].x2 = w-1;
635 segs[0].y2 = y;
636 segs[1].x1 = 0;
637 segs[1].y1 = y;
638 segs[1].x2 = 0;
639 segs[1].y2 = y + h - 2;
640 XDrawSegments(dpy, win, scr->menu_item_auxtexture->light_gc, segs, 2);
642 XDrawLine(dpy, win, scr->menu_item_auxtexture->dark_gc, 0, y+h-1,
643 w-1, y+h-1);
645 segs[0].x1 = 1;
646 segs[0].y1 = segs[0].y2 = y + h-2;
647 segs[0].x2 = w-1;
648 segs[1].x1 = segs[1].x2 = w-1;
649 segs[1].y1 = y + 1;
650 segs[1].y2 = y + h-2;
651 XDrawSegments(dpy, win, scr->menu_item_auxtexture->dim_gc, segs, 2);
655 static void
656 paintEntry(WMenu *menu, int index, int selected)
658 int x, y, w, h, tw;
659 GC light, dim, dark, textGC;
660 WScreen *scr=menu->frame->screen_ptr;
661 Window win = menu->menu->window;
662 WMenuEntry *entry=menu->entries[index];
664 if (!menu->flags.realized) return;
665 h = menu->entry_height;
666 w = menu->menu->width;
667 y = index * h;
669 light = scr->menu_item_auxtexture->light_gc;
670 dim = scr->menu_item_auxtexture->dim_gc;
671 dark = scr->menu_item_auxtexture->dark_gc;
673 /* paint background */
674 if (selected) {
675 XSetForeground(dpy, scr->select_menu_gc, scr->select_pixel);
676 XFillRectangle(dpy, win, scr->select_menu_gc, 1, y+1, w-2, h-3);
677 if (scr->menu_item_texture->any.type == WTEX_SOLID)
678 drawFrame(scr, win, y, w, h);
679 } else {
680 if (scr->menu_item_texture->any.type == WTEX_SOLID) {
681 XClearArea(dpy, win, 0, y + 1, w - 1, h - 3, False);
682 /* draw the frame */
683 drawFrame(scr, win, y, w, h);
684 } else {
685 XClearArea(dpy, win, 0, y, w, h, False);
689 if (selected) {
690 textGC = scr->select_menu_gc;
691 if (entry->flags.enabled)
692 XSetForeground(dpy, textGC, scr->select_text_pixel);
693 else
694 XSetForeground(dpy, textGC, scr->dtext_pixel);
695 } else if (!entry->flags.enabled) {
696 textGC = scr->disabled_menu_entry_gc;
697 } else {
698 textGC = scr->menu_entry_gc;
700 /* draw text */
701 x = 5;
702 if (entry->flags.indicator)
703 x += MENU_INDICATOR_SPACE + 2;
705 wDrawString(win, scr->menu_entry_font,
706 textGC, x, 3+y+scr->menu_entry_font->y, entry->text,
707 strlen(entry->text));
709 if (entry->cascade>=0) {
710 /* draw the cascade indicator */
711 XDrawLine(dpy,win,dim, w-11, y+6, w-6, y+h/2-1);
712 XDrawLine(dpy,win,light, w-11, y+h-8, w-6, y+h/2-1);
713 XDrawLine(dpy,win,dark, w-12, y+6, w-12, y+h-8);
716 /* draw indicator */
717 if (entry->flags.indicator && entry->flags.indicator_on) {
718 int iw, ih;
719 WPixmap *indicator;
722 switch (entry->flags.indicator_type) {
723 case MI_CHECK:
724 indicator = scr->menu_check_indicator;
725 break;
726 case MI_MINIWINDOW:
727 indicator = scr->menu_mini_indicator;
728 break;
729 case MI_HIDDEN:
730 indicator = scr->menu_hide_indicator;
731 break;
732 case MI_SHADED:
733 indicator = scr->menu_shade_indicator;
734 break;
735 case MI_DIAMOND:
736 default:
737 indicator = scr->menu_radio_indicator;
738 break;
741 iw = indicator->width;
742 ih = indicator->height;
743 XSetClipMask(dpy, scr->copy_gc, indicator->mask);
744 XSetClipOrigin(dpy, scr->copy_gc, 5, y+(h-ih)/2);
745 if (selected)
746 XSetForeground(dpy, scr->copy_gc, scr->black_pixel);
747 else
748 XSetForeground(dpy, scr->copy_gc, scr->mtext_pixel);
749 XFillRectangle(dpy, win, scr->copy_gc, 5, y+(h-ih)/2, iw, ih);
751 XCopyArea(dpy, indicator->image, win, scr->copy_gc, 0, 0,
752 iw, ih, 5, y+(h-ih)/2);
754 XSetClipOrigin(dpy, scr->copy_gc, 0, 0);
757 /* draw right text */
759 if (entry->rtext && entry->cascade<0) {
760 tw = wTextWidth(scr->menu_entry_font->font, entry->rtext,
761 strlen(entry->rtext));
763 wDrawString(win, scr->menu_entry_font, textGC, w-6-tw,
764 3+y+scr->menu_entry_font->y, entry->rtext,
765 strlen(entry->rtext));
770 static void
771 move_menus(WMenu *menu, int x, int y)
773 while (menu->parent) {
774 menu = menu->parent;
775 x -= MENUW(menu);
776 if (!wPreferences.align_menus && menu->selected_entry>=0) {
777 y -= menu->selected_entry*menu->entry_height;
780 wMenuMove(menu, x, y, True);
783 static void
784 makeVisible(WMenu *menu)
786 WScreen *scr = menu->frame->screen_ptr;
787 int x1, y1, x2, y2, new_x, new_y, move;
789 if (menu->entry_no<0) return;
791 x1 = menu->frame_x;
792 y1 = menu->frame_y+menu->frame->top_width
793 + menu->selected_entry*menu->entry_height;
794 x2 = x1 + MENUW(menu);
795 y2 = y1 + menu->entry_height;
797 new_x = x1;
798 new_y = y1;
799 move = 0;
801 if (x1 < 0) {
802 new_x = 0;
803 move = 1;
804 } else if (x2 >= scr->scr_width) {
805 new_x = scr->scr_width - MENUW(menu) - 1;
806 move = 1;
809 if (y1 < 0) {
810 new_y = 0;
811 move = 1;
812 } else if (y2 >= scr->scr_height) {
813 new_y = scr->scr_height - menu->entry_height - 1;
814 move = 1;
817 new_y = new_y - menu->frame->top_width
818 - menu->selected_entry*menu->entry_height;
819 move_menus(menu, new_x, new_y);
823 static int
824 check_key(WMenu *menu, XKeyEvent *event)
826 int i, ch, s;
827 char buffer[32];
829 if (XLookupString(event, buffer, 32, NULL, NULL)<1)
830 return -1;
832 ch = toupper(buffer[0]);
834 s = (menu->selected_entry>=0 ? menu->selected_entry+1 : 0);
836 again:
837 for (i=s; i<menu->entry_no; i++) {
838 if (ch==toupper(menu->entries[i]->text[0])) {
839 return i;
842 /* no match. Retry from start, if previous started from a selected entry */
843 if (s!=0) {
844 s = 0;
845 goto again;
847 return -1;
851 static int
852 keyboardMenu(WMenu *menu)
854 XEvent event;
855 KeySym ksym=NoSymbol;
856 int done=0;
857 int index;
858 WMenuEntry *entry;
859 int old_pos_x = menu->frame_x;
860 int old_pos_y = menu->frame_y;
861 int new_x = old_pos_x, new_y = old_pos_y;
862 int scr_width = menu->frame->screen_ptr->scr_width;
863 int scr_height = menu->frame->screen_ptr->scr_height;
865 if (menu->flags.editing)
866 return False;
869 XGrabKeyboard(dpy, menu->frame->core->window, True, GrabModeAsync,
870 GrabModeAsync, CurrentTime);
872 if (menu->frame_y+menu->frame->top_width >= scr_height)
873 new_y = scr_height - menu->frame->top_width;
875 if (menu->frame_x+MENUW(menu) >= scr_width)
876 new_x = scr_width-MENUW(menu)-1;
878 move_menus(menu, new_x, new_y);
880 while (!done && menu->flags.mapped) {
881 XAllowEvents(dpy, AsyncKeyboard, CurrentTime);
882 WMMaskEvent(dpy, ExposureMask|ButtonMotionMask|ButtonPressMask
883 |ButtonReleaseMask|KeyPressMask|KeyReleaseMask
884 |SubstructureNotifyMask, &event);
886 switch (event.type) {
887 case KeyPress:
888 ksym = XLookupKeysym(&event.xkey, 0);
889 switch (ksym) {
890 case XK_Escape:
891 done = 1;
892 break;
894 case XK_Home:
895 case XK_KP_Home:
896 selectEntry(menu, 0);
897 makeVisible(menu);
898 break;
900 case XK_End:
901 case XK_KP_End:
902 selectEntry(menu, menu->entry_no-1);
903 makeVisible(menu);
904 break;
906 case XK_Up:
907 #ifdef ARROWLESS_KBD
908 case XK_k:
909 #endif
910 case XK_KP_Up:
911 if (menu->selected_entry <= 0)
912 selectEntry(menu, menu->entry_no-1);
913 else
914 selectEntry(menu, menu->selected_entry-1);
915 makeVisible(menu);
916 break;
918 case XK_Down:
919 #ifdef ARROWLESS_KBD
920 case XK_j:
921 #endif
922 case XK_KP_Down:
923 if (menu->selected_entry<0)
924 selectEntry(menu, 0);
925 else if (menu->selected_entry == menu->entry_no-1)
926 selectEntry(menu, 0);
927 else if (menu->selected_entry < menu->entry_no-1)
928 selectEntry(menu, menu->selected_entry+1);
929 makeVisible(menu);
930 break;
932 case XK_Right:
933 #ifdef ARROWLESS_KBD
934 case XK_l:
935 #endif
936 case XK_KP_Right:
937 if (menu->selected_entry>=0) {
938 WMenuEntry *entry;
939 entry = menu->entries[menu->selected_entry];
941 if (entry->cascade >= 0 && menu->cascades
942 && menu->cascades[entry->cascade]->entry_no > 0) {
944 XUngrabKeyboard(dpy, CurrentTime);
946 selectEntry(menu->cascades[entry->cascade], 0);
947 if (!keyboardMenu(menu->cascades[entry->cascade]))
948 done = 1;
950 XGrabKeyboard(dpy, menu->frame->core->window, True,
951 GrabModeAsync, GrabModeAsync,
952 CurrentTime);
955 break;
957 case XK_Left:
958 #ifdef ARROWLESS_KBD
959 case XK_h:
960 #endif
961 case XK_KP_Left:
962 if (menu->parent!=NULL && menu->parent->selected_entry>=0) {
963 selectEntry(menu, -1);
964 move_menus(menu, old_pos_x, old_pos_y);
965 return True;
967 break;
969 case XK_Return:
970 done = 2;
971 break;
973 default:
974 index = check_key(menu, &event.xkey);
975 if (index>=0) {
976 selectEntry(menu, index);
979 break;
981 default:
982 if (event.type==ButtonPress)
983 done = 1;
985 WMHandleEvent(&event);
989 XUngrabKeyboard(dpy, CurrentTime);
991 if (done==2 && menu->selected_entry>=0) {
992 entry = menu->entries[menu->selected_entry];
993 } else {
994 entry = NULL;
997 if (entry && entry->callback!=NULL && entry->flags.enabled
998 && entry->cascade < 0) {
999 #if (MENU_BLINK_COUNT > 0)
1000 int sel = menu->selected_entry;
1001 int i;
1003 for (i=0; i<MENU_BLINK_COUNT; i++) {
1004 paintEntry(menu, sel, False);
1005 XSync(dpy, 0);
1006 wusleep(MENU_BLINK_DELAY);
1007 paintEntry(menu, sel, True);
1008 XSync(dpy, 0);
1009 wusleep(MENU_BLINK_DELAY);
1011 #endif
1012 selectEntry(menu, -1);
1014 if (!menu->flags.buttoned) {
1015 wMenuUnmap(menu);
1016 move_menus(menu, old_pos_x, old_pos_y);
1018 closeCascade(menu);
1020 (*entry->callback)(menu, entry);
1021 } else {
1022 if (!menu->flags.buttoned) {
1023 wMenuUnmap(menu);
1024 move_menus(menu, old_pos_x, old_pos_y);
1026 selectEntry(menu, -1);
1030 /* returns True if returning from a submenu to a parent menu,
1031 * False if exiting from menu */
1032 return False;
1036 void
1037 wMenuMapAt(WMenu *menu, int x, int y, int keyboard)
1039 int scr_width = menu->frame->screen_ptr->scr_width;
1040 int scr_height = menu->frame->screen_ptr->scr_height;
1042 if (!menu->flags.realized) {
1043 menu->flags.realized=1;
1044 wMenuRealize(menu);
1046 if (!menu->flags.mapped) {
1047 if (wPreferences.wrap_menus) {
1048 if (x<0) x = 0;
1049 if (y<0) y = 0;
1050 if (x+MENUW(menu) > scr_width)
1051 x = scr_width - MENUW(menu);
1052 if (y+MENUH(menu) > scr_height)
1053 y = scr_height - MENUH(menu);
1056 XMoveWindow(dpy, menu->frame->core->window, x, y);
1057 menu->frame_x = x;
1058 menu->frame_y = y;
1059 XMapWindow(dpy, menu->frame->core->window);
1060 wRaiseFrame(menu->frame->core);
1061 menu->flags.mapped = 1;
1062 } else {
1063 selectEntry(menu, 0);
1066 if (keyboard)
1067 keyboardMenu(menu);
1071 void
1072 wMenuMap(WMenu *menu)
1074 if (!menu->flags.realized) {
1075 menu->flags.realized=1;
1076 wMenuRealize(menu);
1078 if (menu->flags.app_menu && menu->parent==NULL) {
1079 menu->frame_x = menu->frame->screen_ptr->app_menu_x;
1080 menu->frame_y = menu->frame->screen_ptr->app_menu_y;
1081 XMoveWindow(dpy, menu->frame->core->window, menu->frame_x, menu->frame_y);
1083 XMapWindow(dpy, menu->frame->core->window);
1084 wRaiseFrame(menu->frame->core);
1085 menu->flags.mapped = 1;
1089 void
1090 wMenuUnmap(WMenu *menu)
1092 int i;
1094 XUnmapWindow(dpy, menu->frame->core->window);
1095 if (menu->flags.titled && menu->flags.buttoned) {
1096 wFrameWindowHideButton(menu->frame, WFF_RIGHT_BUTTON);
1098 menu->flags.buttoned = 0;
1099 menu->flags.mapped = 0;
1100 menu->flags.open_to_left = 0;
1102 for (i=0; i<menu->cascade_no; i++) {
1103 if (menu->cascades[i]!=NULL
1104 && menu->cascades[i]->flags.mapped
1105 && !menu->cascades[i]->flags.buttoned) {
1107 wMenuUnmap(menu->cascades[i]);
1110 menu->selected_entry = -1;
1115 void
1116 wMenuPaint(WMenu *menu)
1118 int i;
1120 if (!menu->flags.mapped) {
1121 return;
1124 /* paint entries */
1125 for (i=0; i<menu->entry_no; i++) {
1126 paintEntry(menu, i, i==menu->selected_entry);
1131 void
1132 wMenuSetEnabled(WMenu *menu, int index, int enable)
1134 if (index>=menu->entry_no) return;
1135 menu->entries[index]->flags.enabled=enable;
1136 paintEntry(menu, index, index==menu->selected_entry);
1137 paintEntry(menu->brother, index, index==menu->selected_entry);
1141 /* ====================================================================== */
1144 static void
1145 editEntry(WMenu *menu, WMenuEntry *entry)
1147 WTextInput *text;
1148 XEvent event;
1149 WObjDescriptor *desc;
1150 char *t;
1151 int done = 0;
1152 Window old_focus;
1153 int old_revert;
1155 menu->flags.editing = 1;
1157 text = wTextCreate(menu->menu, 1, menu->entry_height * entry->order,
1158 menu->menu->width - 2, menu->entry_height - 1);
1160 wTextPutText(text, entry->text);
1161 XGetInputFocus(dpy, &old_focus, &old_revert);
1162 XSetInputFocus(dpy, text->core->window, RevertToNone, CurrentTime);
1164 if (XGrabKeyboard(dpy, text->core->window, True, GrabModeAsync,
1165 GrabModeAsync, CurrentTime)!=GrabSuccess) {
1166 wwarning("could not grab keyboard");
1167 wTextDestroy(text);
1169 wSetFocusTo(menu->frame->screen_ptr,
1170 menu->frame->screen_ptr->focused_window);
1171 return;
1175 while (!done && !text->done) {
1176 XSync(dpy, 0);
1177 XAllowEvents(dpy, AsyncKeyboard|AsyncPointer, CurrentTime);
1178 XSync(dpy, 0);
1179 WMNextEvent(dpy, &event);
1181 if (XFindContext(dpy, event.xany.window, wWinContext,
1182 (XPointer *)&desc)==XCNOENT)
1183 desc = NULL;
1185 if ((desc != NULL) && (desc->handle_anything != NULL)) {
1187 (*desc->handle_anything)(desc, &event);
1189 } else {
1190 switch (event.type) {
1191 case ButtonPress:
1192 XAllowEvents(dpy, ReplayPointer, CurrentTime);
1193 done = 1;
1195 default:
1196 WMHandleEvent(&event);
1197 break;
1202 XSetInputFocus(dpy, old_focus, old_revert, CurrentTime);
1204 wSetFocusTo(menu->frame->screen_ptr,
1205 menu->frame->screen_ptr->focused_window);
1208 t = wTextGetText(text);
1209 /* if !t, the user has canceled editing */
1210 if (t) {
1211 if (entry->text)
1212 free(entry->text);
1213 entry->text = wstrdup(t);
1215 menu->flags.realized = 0;
1217 wTextDestroy(text);
1219 XUngrabKeyboard(dpy, CurrentTime);
1221 if (t && menu->on_edit)
1222 (*menu->on_edit)(menu, entry);
1224 menu->flags.editing = 0;
1226 if (!menu->flags.realized)
1227 wMenuRealize(menu);
1231 static void
1232 selectEntry(WMenu *menu, int entry_no)
1234 WMenuEntry *entry;
1235 WMenu *submenu;
1236 int old_entry;
1238 if (menu->entries==NULL)
1239 return;
1241 if (entry_no >= menu->entry_no)
1242 return;
1244 old_entry = menu->selected_entry;
1245 menu->selected_entry = entry_no;
1247 if (old_entry!=entry_no) {
1249 /* unselect previous entry */
1250 if (old_entry>=0) {
1251 paintEntry(menu, old_entry, False);
1252 entry = menu->entries[old_entry];
1254 /* unmap cascade */
1255 if (entry->cascade>=0 && menu->cascades) {
1256 if (!menu->cascades[entry->cascade]->flags.buttoned) {
1257 wMenuUnmap(menu->cascades[entry->cascade]);
1262 if (entry_no<0) {
1263 menu->selected_entry = -1;
1264 return;
1266 entry = menu->entries[entry_no];
1268 if (entry->cascade>=0 && menu->cascades && entry->flags.enabled) {
1269 /* Callback for when the submenu is opened.
1271 submenu = menu->cascades[entry->cascade];
1272 if (submenu && submenu->flags.brother)
1273 submenu = submenu->brother;
1275 if (entry->callback) {
1276 /* Only call the callback if the submenu is not yet mapped.
1278 if (menu->flags.brother) {
1279 if (!submenu || !submenu->flags.mapped)
1280 (*entry->callback)(menu->brother, entry);
1281 } else {
1282 if (!submenu || !submenu->flags.buttoned)
1283 (*entry->callback)(menu, entry);
1287 /* the submenu menu might have changed */
1288 submenu = menu->cascades[entry->cascade];
1290 /* map cascade */
1291 if (!submenu->flags.mapped) {
1292 int x, y;
1294 if (!submenu->flags.realized)
1295 wMenuRealize(submenu);
1296 if (wPreferences.wrap_menus) {
1297 if (menu->flags.open_to_left)
1298 submenu->flags.open_to_left = 1;
1300 if (submenu->flags.open_to_left) {
1301 x = menu->frame_x - MENUW(submenu);
1302 if (x<0) {
1303 x = 0;
1304 submenu->flags.open_to_left = 0;
1306 } else {
1307 x = menu->frame_x + MENUW(menu);
1309 if (x + MENUW(submenu)
1310 >= menu->frame->screen_ptr->scr_width) {
1312 x = menu->frame_x - MENUW(submenu);
1313 submenu->flags.open_to_left = 1;
1316 } else {
1317 x = menu->frame_x + MENUW(menu);
1320 if (wPreferences.align_menus) {
1321 y = menu->frame_y;
1322 } else {
1323 y = menu->frame_y + menu->entry_height*entry_no;
1324 if (menu->flags.titled)
1325 y += menu->frame->top_width;
1326 if (menu->cascades[entry->cascade]->flags.titled)
1327 y -= menu->cascades[entry->cascade]->frame->top_width;
1330 wMenuMapAt(menu->cascades[entry->cascade], x, y, False);
1331 menu->cascades[entry->cascade]->parent = menu;
1332 } else {
1333 return;
1336 paintEntry(menu, entry_no, True);
1341 static WMenu*
1342 findMenu(WScreen *scr, int *x_ret, int *y_ret)
1344 WMenu *menu;
1345 WObjDescriptor *desc;
1346 Window root_ret, win, junk_win;
1347 int x, y, wx, wy;
1348 unsigned int mask;
1350 XQueryPointer(dpy, scr->root_win, &root_ret, &win, &x, &y, &wx, &wy,
1351 &mask);
1353 if (win==None) return NULL;
1355 if (XFindContext(dpy, win, wWinContext, (XPointer *)&desc)==XCNOENT)
1356 return NULL;
1358 if (desc->parent_type == WCLASS_MENU) {
1359 menu = (WMenu*)desc->parent;
1360 XTranslateCoordinates(dpy, root_ret, menu->menu->window, wx, wy,
1361 x_ret, y_ret, &junk_win);
1362 return menu;
1364 return NULL;
1370 static void
1371 closeCascade(WMenu *menu)
1373 WMenu *parent=menu->parent;
1375 if (menu->flags.brother
1376 || (!menu->flags.buttoned
1377 && (!menu->flags.app_menu||menu->parent!=NULL))) {
1379 selectEntry(menu, -1);
1380 XSync(dpy, 0);
1381 #if (MENU_BLINK_DELAY > 2)
1382 wusleep(MENU_BLINK_DELAY/2);
1383 #endif
1384 wMenuUnmap(menu);
1385 while (parent!=NULL
1386 && (parent->parent!=NULL || !parent->flags.app_menu
1387 || parent->flags.brother)
1388 && !parent->flags.buttoned) {
1389 selectEntry(parent, -1);
1390 wMenuUnmap(parent);
1391 parent = parent->parent;
1393 if (parent)
1394 selectEntry(parent, -1);
1399 static void
1400 closeBrotherCascadesOf(WMenu *menu)
1402 WMenu *tmp;
1403 int i;
1405 for (i=0; i<menu->cascade_no; i++) {
1406 if (menu->cascades[i]->flags.brother) {
1407 tmp = menu->cascades[i];
1408 } else {
1409 tmp = menu->cascades[i]->brother;
1411 if (tmp->flags.mapped) {
1412 selectEntry(tmp->parent, -1);
1413 closeBrotherCascadesOf(tmp);
1414 break;
1420 #define getEntryAt(menu, x, y) ((y)<0 ? -1 : (y)/(menu->entry_height))
1423 static WMenu*
1424 parentMenu(WMenu *menu)
1426 WMenu *parent;
1427 WMenuEntry *entry;
1429 if (menu->flags.buttoned)
1430 return menu;
1432 while (menu->parent && menu->parent->flags.mapped) {
1433 parent = menu->parent;
1434 if (parent->selected_entry < 0)
1435 break;
1436 entry = parent->entries[parent->selected_entry];
1437 if (!entry->flags.enabled || entry->cascade<0 || !parent->cascades ||
1438 parent->cascades[entry->cascade] != menu)
1439 break;
1440 menu = parent;
1441 if (menu->flags.buttoned)
1442 break;
1445 return menu;
1451 * Will raise the passed menu, if submenu = 0
1452 * If submenu > 0 will also raise all mapped submenus
1453 * until the first buttoned one
1454 * If submenu < 0 will also raise all mapped parent menus
1455 * until the first buttoned one
1458 static void
1459 raiseMenus(WMenu *menu, int submenus)
1461 WMenu *submenu;
1462 int i;
1464 if(!menu) return;
1466 wRaiseFrame(menu->frame->core);
1468 if (submenus>0 && menu->selected_entry>=0) {
1469 i = menu->entries[menu->selected_entry]->cascade;
1470 if (i>=0 && menu->cascades) {
1471 submenu = menu->cascades[i];
1472 if (submenu->flags.mapped && !submenu->flags.buttoned)
1473 raiseMenus(submenu, submenus);
1476 if (submenus<0 && !menu->flags.buttoned &&
1477 menu->parent && menu->parent->flags.mapped)
1478 raiseMenus(menu->parent, submenus);
1482 WMenu*
1483 wMenuUnderPointer(WScreen *screen)
1485 WObjDescriptor *desc;
1486 Window root_ret, win;
1487 int dummy;
1488 unsigned int mask;
1490 XQueryPointer(dpy, screen->root_win, &root_ret, &win, &dummy, &dummy,
1491 &dummy, &dummy, &mask);
1493 if (win==None) return NULL;
1495 if (XFindContext(dpy, win, wWinContext, (XPointer *)&desc)==XCNOENT)
1496 return NULL;
1498 if (desc->parent_type == WCLASS_MENU)
1499 return (WMenu *)desc->parent;
1500 return NULL;
1505 #define MIN(a,b) (((a) > (b)) ? (b) : (a))
1508 static void
1509 getPointerPosition(WScreen *scr, int *x, int *y)
1511 Window root_ret, win;
1512 int wx, wy;
1513 unsigned int mask;
1515 XQueryPointer(dpy, scr->root_win, &root_ret, &win, x, y, &wx, &wy, &mask);
1519 static void
1520 getScrollAmount(WMenu *menu, int *hamount, int *vamount)
1522 WScreen *scr = menu->menu->screen_ptr;
1523 int menuX1 = menu->frame_x;
1524 int menuY1 = menu->frame_y;
1525 int menuX2 = menu->frame_x + MENUW(menu);
1526 int menuY2 = menu->frame_y + MENUH(menu);
1527 int screenW = scr->scr_width;
1528 int screenH = scr->scr_height;
1529 int xroot, yroot;
1531 *hamount = 0;
1532 *vamount = 0;
1534 getPointerPosition(scr, &xroot, &yroot);
1537 if (xroot <= 1 && menuX1 < 0) {
1538 /* scroll to the right */
1539 *hamount = MIN(MENU_SCROLL_STEP, abs(menuX1));
1541 } else if (xroot >= screenW-2 && menuX2 > screenW-1) {
1542 /* scroll to the left */
1543 *hamount = MIN(MENU_SCROLL_STEP, abs(menuX2-screenW-1));
1545 if (*hamount==0)
1546 *hamount = 1;
1548 *hamount = -*hamount;
1551 if (yroot <= 1 && menuY1 < 0) {
1552 /* scroll down */
1553 *vamount = MIN(MENU_SCROLL_STEP, abs(menuY1));
1555 } else if (yroot >= screenH-2 && menuY2 > screenH-1) {
1556 /* scroll up */
1557 *vamount = MIN(MENU_SCROLL_STEP, abs(menuY2-screenH-2));
1559 *vamount = -*vamount;
1564 static void
1565 dragScrollMenuCallback(void *data)
1567 WMenu *menu = (WMenu*)data;
1568 WScreen *scr = menu->menu->screen_ptr;
1569 WMenu *parent = parentMenu(menu);
1570 int hamount, vamount;
1571 int x, y;
1572 int newSelectedEntry;
1574 getScrollAmount(menu, &hamount, &vamount);
1577 if (hamount != 0 || vamount != 0) {
1578 wMenuMove(parent, parent->frame_x + hamount,
1579 parent->frame_y + vamount, True);
1580 if (findMenu(scr, &x, &y)) {
1581 newSelectedEntry = getEntryAt(menu, x, y);
1582 selectEntry(menu, newSelectedEntry);
1583 } else {
1584 /* Pointer fell outside of menu. If the selected entry is
1585 * not a submenu, unselect it */
1586 if (menu->selected_entry >= 0
1587 && menu->entries[menu->selected_entry]->cascade<0)
1588 selectEntry(menu, -1);
1589 newSelectedEntry = 0;
1592 /* paranoid check */
1593 if (newSelectedEntry >= 0) {
1594 /* keep scrolling */
1595 menu->timer = WMAddTimerHandler(MENU_SCROLL_DELAY,
1596 dragScrollMenuCallback, menu);
1597 } else {
1598 menu->timer = NULL;
1600 } else {
1601 /* don't need to scroll anymore */
1602 menu->timer = NULL;
1603 if (findMenu(scr, &x, &y)) {
1604 newSelectedEntry = getEntryAt(menu, x, y);
1605 selectEntry(menu, newSelectedEntry);
1611 static void
1612 scrollMenuCallback(void *data)
1614 WMenu *menu = (WMenu*)data;
1615 WMenu *parent = parentMenu(menu);
1616 int hamount = 0; /* amount to scroll */
1617 int vamount = 0;
1619 getScrollAmount(menu, &hamount, &vamount);
1621 if (hamount != 0 || vamount != 0) {
1622 wMenuMove(parent, parent->frame_x + hamount,
1623 parent->frame_y + vamount, True);
1625 /* keep scrolling */
1626 menu->timer = WMAddTimerHandler(MENU_SCROLL_DELAY,
1627 scrollMenuCallback, menu);
1628 } else {
1629 /* don't need to scroll anymore */
1630 menu->timer = NULL;
1636 #define MENU_SCROLL_BORDER 5
1638 static int
1639 isPointNearBoder(WMenu *menu, int x, int y)
1641 int menuX1 = menu->frame_x;
1642 int menuY1 = menu->frame_y;
1643 int menuX2 = menu->frame_x + MENUW(menu);
1644 int menuY2 = menu->frame_y + MENUH(menu);
1645 int scrXe = menu->menu->screen_ptr->scr_width-1;
1646 int scrYe = menu->menu->screen_ptr->scr_height-1;
1647 int flag = 0;
1649 if (x >= menuX1 && x <= menuX2 && (y < MENU_SCROLL_BORDER
1650 || y > scrYe-MENU_SCROLL_BORDER))
1651 flag = 1;
1652 else if (y >= menuY1 && y <= menuY2 && (x < MENU_SCROLL_BORDER
1653 || x > scrXe-MENU_SCROLL_BORDER))
1654 flag = 1;
1656 return flag;
1660 void
1661 wMenuScroll(WMenu *menu, XEvent *event)
1663 WMenu *smenu;
1664 WMenu *omenu = parentMenu(menu);
1665 WScreen *scr = menu->frame->screen_ptr;
1666 int done = 0;
1667 int jump_back = 0;
1668 int old_frame_x = omenu->frame_x;
1669 int old_frame_y = omenu->frame_y;
1670 XEvent ev;
1672 #ifdef DEBUG
1673 puts("Entering menu Scroll");
1674 #endif
1676 if ((/*omenu->flags.buttoned &&*/ !wPreferences.wrap_menus)
1677 || omenu->flags.app_menu) {
1678 jump_back = 1;
1681 if (!wPreferences.wrap_menus)
1682 raiseMenus(omenu, True);
1683 else
1684 raiseMenus(menu, False);
1686 if (!menu->timer)
1687 scrollMenuCallback(menu);
1689 while(!done) {
1690 int x, y, on_border, on_x_edge, on_y_edge, on_title;
1692 WMNextEvent(dpy, &ev);
1693 switch (ev.type) {
1694 case EnterNotify:
1695 WMHandleEvent(&ev);
1696 case MotionNotify:
1697 x = (ev.type==MotionNotify) ? ev.xmotion.x_root : ev.xcrossing.x_root;
1698 y = (ev.type==MotionNotify) ? ev.xmotion.y_root : ev.xcrossing.y_root;
1700 /* on_border is != 0 if the pointer is between the menu
1701 * and the screen border and is close enough to the border */
1702 on_border = isPointNearBoder(menu, x, y);
1704 smenu = wMenuUnderPointer(scr);
1706 if ((smenu==NULL && !on_border) || (smenu && parentMenu(smenu)!=omenu)) {
1707 done = 1;
1708 break;
1711 on_x_edge = x <= 1 || x >= scr->scr_width - 2;
1712 on_y_edge = y <= 1 || y >= scr->scr_height - 2;
1713 on_border = on_x_edge || on_y_edge;
1715 if (!on_border && !jump_back) {
1716 done = 1;
1717 break;
1720 if (menu->timer && (smenu!=menu || (!on_y_edge && !on_x_edge))) {
1721 WMDeleteTimerHandler(menu->timer);
1722 menu->timer = NULL;
1725 if (smenu != NULL)
1726 menu = smenu;
1728 if (!menu->timer)
1729 scrollMenuCallback(menu);
1730 break;
1731 case ButtonPress:
1732 /* True if we push on title, or drag the omenu to other position */
1733 on_title = ev.xbutton.x_root >= omenu->frame_x &&
1734 ev.xbutton.x_root <= omenu->frame_x + MENUW(omenu) &&
1735 ev.xbutton.y_root >= omenu->frame_y &&
1736 ev.xbutton.y_root <= omenu->frame_y + omenu->frame->top_width;
1737 WMHandleEvent(&ev);
1738 smenu = wMenuUnderPointer(scr);
1739 if (smenu == NULL || (smenu && smenu->flags.buttoned && smenu != omenu))
1740 done = 1;
1741 else if (smenu==omenu && on_title) {
1742 jump_back = 0;
1743 done = 1;
1745 break;
1746 case KeyPress:
1747 done = 1;
1748 default:
1749 WMHandleEvent(&ev);
1750 break;
1754 if (menu->timer) {
1755 WMDeleteTimerHandler(menu->timer);
1756 menu->timer = NULL;
1759 if (jump_back)
1760 wMenuMove(omenu, old_frame_x, old_frame_y, True);
1762 #ifdef DEBUG
1763 puts("Leaving menu Scroll");
1764 #endif
1769 static void
1770 menuExpose(WObjDescriptor *desc, XEvent *event)
1772 wMenuPaint(desc->parent);
1775 typedef struct {
1776 int *delayed_select;
1777 WMenu *menu;
1778 WMHandlerID magic;
1779 } delay_data;
1782 static void
1783 delaySelection(void *data)
1785 delay_data *d = (delay_data*)data;
1786 int x, y, entry_no;
1787 WMenu *menu;
1789 d->magic = NULL;
1791 menu = findMenu(d->menu->menu->screen_ptr, &x, &y);
1792 if (menu && (d->menu == menu || d->delayed_select)) {
1793 entry_no = getEntryAt(menu, x, y);
1794 selectEntry(menu, entry_no);
1796 if (d->delayed_select)
1797 *(d->delayed_select) = 0;
1801 static void
1802 menuMouseDown(WObjDescriptor *desc, XEvent *event)
1804 XButtonEvent *bev = &event->xbutton;
1805 WMenu *menu = desc->parent;
1806 WMenu *smenu;
1807 WScreen *scr=menu->frame->screen_ptr;
1808 WMenuEntry *entry=NULL;
1809 XEvent ev;
1810 int close_on_exit=0;
1811 int done=0;
1812 int delayed_select = 0;
1813 int entry_no;
1814 int x, y;
1815 int prevx, prevy;
1816 int old_frame_x = 0;
1817 int old_frame_y = 0;
1818 delay_data d_data = {NULL, NULL, NULL};
1820 if (!wPreferences.wrap_menus) {
1821 smenu = parentMenu(menu);
1822 old_frame_x = smenu->frame_x;
1823 old_frame_y = smenu->frame_y;
1825 else if (event->xbutton.window == menu->frame->core->window) {
1826 /* This is true if the menu was launched with right click on root window */
1827 delayed_select = 1;
1828 d_data.delayed_select = &delayed_select;
1829 d_data.menu = menu;
1830 d_data.magic = WMAddTimerHandler(wPreferences.dblclick_time,
1831 delaySelection, &d_data);
1834 wRaiseFrame(menu->frame->core);
1836 close_on_exit = (bev->send_event || menu->flags.brother);
1838 smenu = findMenu(scr, &x, &y);
1839 if (!smenu) {
1840 x = -1;
1841 y = -1;
1842 } else {
1843 menu = smenu;
1846 if (menu->flags.editing) {
1847 return;
1849 entry_no = getEntryAt(menu, x, y);
1850 if (entry_no>=0) {
1851 entry = menu->entries[entry_no];
1853 if (!close_on_exit && (bev->state & ControlMask) && smenu
1854 && entry->flags.editable) {
1855 editEntry(smenu, entry);
1856 return;
1857 } else if (bev->state & ControlMask) {
1858 return;
1861 if (entry->flags.enabled && entry->cascade>=0 && menu->cascades) {
1862 WMenu *submenu = menu->cascades[entry->cascade];
1863 /* map cascade */
1864 if (submenu->flags.mapped && !submenu->flags.buttoned &&
1865 menu->selected_entry!=entry_no) {
1866 wMenuUnmap(submenu);
1868 if (!submenu->flags.mapped && !delayed_select) {
1869 selectEntry(menu, entry_no);
1870 } else if (!submenu->flags.buttoned) {
1871 selectEntry(menu, -1);
1874 } else if (!delayed_select) {
1875 selectEntry(menu, entry_no);
1878 if (!wPreferences.wrap_menus && !wPreferences.scrollable_menus) {
1879 if (!menu->timer)
1880 dragScrollMenuCallback(menu);
1884 prevx = bev->x_root;
1885 prevy = bev->y_root;
1886 while (!done) {
1887 int x, y;
1889 XAllowEvents(dpy, SyncPointer, CurrentTime);
1891 WMMaskEvent(dpy, ExposureMask|ButtonMotionMask|ButtonReleaseMask
1892 |ButtonPressMask, &ev);
1893 switch (ev.type) {
1894 case MotionNotify:
1895 smenu = findMenu(scr, &x, &y);
1897 if (smenu == NULL) {
1898 /* moved mouse out of menu */
1900 if (!delayed_select && d_data.magic) {
1901 WMDeleteTimerHandler(d_data.magic);
1902 d_data.magic = NULL;
1904 if (menu==NULL
1905 || (menu->selected_entry>=0
1906 && menu->entries[menu->selected_entry]->cascade>=0)) {
1907 prevx = ev.xmotion.x_root;
1908 prevy = ev.xmotion.y_root;
1910 break;
1912 selectEntry(menu, -1);
1913 menu = smenu;
1914 prevx = ev.xmotion.x_root;
1915 prevy = ev.xmotion.y_root;
1916 break;
1917 } else if (menu && menu!=smenu
1918 && (menu->selected_entry<0
1919 || menu->entries[menu->selected_entry]->cascade<0)) {
1920 selectEntry(menu, -1);
1922 if (!delayed_select && d_data.magic) {
1923 WMDeleteTimerHandler(d_data.magic);
1924 d_data.magic = NULL;
1926 } else {
1928 /* hysteresis for item selection */
1930 /* check if the motion was to the side, indicating that
1931 * the user may want to cross to a submenu */
1932 if (!delayed_select && menu) {
1933 int dx;
1934 Bool moved_to_submenu;/* moved to direction of submenu */
1936 dx = abs(prevx - ev.xmotion.x_root);
1938 moved_to_submenu = False;
1939 if (dx > 0 /* if moved enough to the side */
1940 /* maybe a open submenu */
1941 && menu->selected_entry>=0
1942 /* moving to the right direction */
1943 && (wPreferences.align_menus
1944 || ev.xmotion.y_root >= prevy)) {
1945 int index;
1947 index = menu->entries[menu->selected_entry]->cascade;
1948 if (index>=0) {
1949 if (menu->cascades[index]->frame_x>menu->frame_x) {
1950 if (prevx < ev.xmotion.x_root)
1951 moved_to_submenu = True;
1952 } else {
1953 if (prevx > ev.xmotion.x_root)
1954 moved_to_submenu = True;
1960 if (menu != smenu) {
1961 if (d_data.magic) {
1962 WMDeleteTimerHandler(d_data.magic);
1964 d_data.magic = NULL;
1965 } else if (moved_to_submenu) {
1966 /* while we are moving, postpone the selection */
1967 if (d_data.magic) {
1968 WMDeleteTimerHandler(d_data.magic);
1970 d_data.delayed_select = NULL;
1971 d_data.menu = menu;
1972 d_data.magic = WMAddTimerHandler(MENU_SELECT_DELAY,
1973 delaySelection,
1974 &d_data);
1975 prevx = ev.xmotion.x_root;
1976 prevy = ev.xmotion.y_root;
1977 break;
1978 } else {
1979 if (d_data.magic)
1980 WMDeleteTimerHandler(d_data.magic);
1981 d_data.magic = NULL;
1985 prevx = ev.xmotion.x_root;
1986 prevy = ev.xmotion.y_root;
1987 if (menu!=smenu) {
1988 /* pointer crossed menus */
1989 if (menu && menu->timer) {
1990 WMDeleteTimerHandler(menu->timer);
1991 menu->timer = NULL;
1993 if (smenu)
1994 dragScrollMenuCallback(smenu);
1996 menu = smenu;
1997 if (!menu->timer)
1998 dragScrollMenuCallback(menu);
2000 if (!delayed_select) {
2001 entry_no = getEntryAt(menu, x, y);
2002 if (entry_no>=0) {
2003 entry = menu->entries[entry_no];
2004 if (entry->flags.enabled && entry->cascade>=0 &&
2005 menu->cascades) {
2006 WMenu *submenu = menu->cascades[entry->cascade];
2007 if (submenu->flags.mapped && !submenu->flags.buttoned
2008 && menu->selected_entry!=entry_no) {
2009 wMenuUnmap(submenu);
2013 selectEntry(menu, entry_no);
2015 break;
2017 case ButtonPress:
2018 break;
2020 case ButtonRelease:
2021 if (ev.xbutton.button == event->xbutton.button)
2022 done=1;
2023 break;
2025 case Expose:
2026 WMHandleEvent(&ev);
2027 break;
2031 if (menu && menu->timer) {
2032 WMDeleteTimerHandler(menu->timer);
2033 menu->timer = NULL;
2035 if (d_data.magic!=NULL)
2036 WMDeleteTimerHandler(d_data.magic);
2038 if (menu && menu->selected_entry>=0) {
2039 entry = menu->entries[menu->selected_entry];
2040 if (entry->callback!=NULL && entry->flags.enabled
2041 && entry->cascade < 0) {
2042 /* blink and erase menu selection */
2043 #if (MENU_BLINK_DELAY > 0)
2044 int sel = menu->selected_entry;
2045 int i;
2047 for (i=0; i<MENU_BLINK_COUNT; i++) {
2048 paintEntry(menu, sel, False);
2049 XSync(dpy, 0);
2050 wusleep(MENU_BLINK_DELAY);
2051 paintEntry(menu, sel, True);
2052 XSync(dpy, 0);
2053 wusleep(MENU_BLINK_DELAY);
2055 #endif
2056 /* unmap the menu, it's parents and call the callback */
2057 if (!menu->flags.buttoned &&
2058 (!menu->flags.app_menu||menu->parent!=NULL)) {
2059 closeCascade(menu);
2060 } else {
2061 selectEntry(menu, -1);
2063 (*entry->callback)(menu, entry);
2065 /* If the user double clicks an entry, the entry will
2066 * be executed twice, which is not good for things like
2067 * the root menu. So, ignore any clicks that were generated
2068 * while the entry was being executed */
2069 while (XCheckTypedWindowEvent(dpy, menu->menu->window,
2070 ButtonPress, &ev));
2071 } else if (entry->callback!=NULL && entry->cascade<0) {
2072 selectEntry(menu, -1);
2073 } else {
2074 if (entry->cascade>=0 && menu->cascades
2075 && menu->cascades[entry->cascade]->flags.brother) {
2076 selectEntry(menu, -1);
2081 if (((WMenu*)desc->parent)->flags.brother || close_on_exit
2082 || !smenu)
2083 closeCascade(desc->parent);
2085 /* close the cascade windows that should not remain opened */
2086 closeBrotherCascadesOf(desc->parent);
2088 if (!wPreferences.wrap_menus)
2089 wMenuMove(parentMenu(desc->parent), old_frame_x, old_frame_y, True);
2093 void
2094 wMenuMove(WMenu *menu, int x, int y, int submenus)
2096 WMenu *submenu;
2097 int i;
2099 if (!menu) return;
2101 menu->frame_x = x;
2102 menu->frame_y = y;
2103 XMoveWindow(dpy, menu->frame->core->window, x, y);
2105 if (submenus>0 && menu->selected_entry>=0) {
2106 i = menu->entries[menu->selected_entry]->cascade;
2108 if (i>=0 && menu->cascades) {
2109 submenu = menu->cascades[i];
2110 if (submenu->flags.mapped && !submenu->flags.buttoned)
2111 if (wPreferences.align_menus) {
2112 wMenuMove(submenu, x + MENUW(menu), y, submenus);
2113 } else {
2114 wMenuMove(submenu, x+ MENUW(menu),
2115 y + submenu->entry_height*menu->selected_entry,
2116 submenus);
2120 if (submenus<0 && menu->parent!=NULL && menu->parent->flags.mapped &&
2121 !menu->parent->flags.buttoned) {
2122 if (wPreferences.align_menus) {
2123 wMenuMove(menu->parent, x - MENUW(menu->parent), y, submenus);
2124 } else {
2125 wMenuMove(menu->parent, x - MENUW(menu->parent), menu->frame_y
2126 - menu->parent->entry_height*menu->parent->selected_entry,
2127 submenus);
2133 static void
2134 changeMenuLevels(WMenu *menu, int lower)
2136 int i;
2138 if (!lower) {
2139 ChangeStackingLevel(menu->frame->core, (!menu->parent ? WMMainMenuLevel
2140 : WMSubmenuLevel));
2141 wRaiseFrame(menu->frame->core);
2142 menu->flags.lowered = 0;
2143 } else {
2144 ChangeStackingLevel(menu->frame->core, WMNormalLevel);
2145 wLowerFrame(menu->frame->core);
2146 menu->flags.lowered = 1;
2148 for (i=0; i<menu->cascade_no; i++) {
2149 if (menu->cascades[i]
2150 && !menu->cascades[i]->flags.buttoned
2151 && menu->cascades[i]->flags.lowered!=lower) {
2152 changeMenuLevels(menu->cascades[i], lower);
2159 static void
2160 menuTitleDoubleClick(WCoreWindow *sender, void *data, XEvent *event)
2162 WMenu *menu = data;
2163 int lower;
2165 if (event->xbutton.state & MOD_MASK) {
2166 if (menu->flags.lowered) {
2167 lower = 0;
2168 } else {
2169 lower = 1;
2171 changeMenuLevels(menu, lower);
2176 static void
2177 menuTitleMouseDown(WCoreWindow *sender, void *data, XEvent *event)
2179 WMenu *menu = data;
2180 WMenu *tmp;
2181 XEvent ev;
2182 int x=menu->frame_x, y=menu->frame_y;
2183 int dx=event->xbutton.x_root, dy=event->xbutton.y_root;
2184 int i, lower;
2185 Bool started;
2187 #ifdef DEBUG
2188 printf("Moving menu\n");
2189 #endif
2191 /* can't touch the menu copy */
2192 if (menu->flags.brother)
2193 return;
2195 if (event->xbutton.button != Button1 && event->xbutton.button != Button2)
2196 return;
2198 if (event->xbutton.state & MOD_MASK) {
2199 wLowerFrame(menu->frame->core);
2200 lower = 1;
2201 } else {
2202 wRaiseFrame(menu->frame->core);
2203 lower = 0;
2205 tmp = menu;
2207 /* lower/raise all submenus */
2208 while (1) {
2209 if (tmp->selected_entry>=0 && tmp->cascades
2210 && tmp->entries[tmp->selected_entry]->cascade>=0) {
2211 tmp = tmp->cascades[tmp->entries[tmp->selected_entry]->cascade];
2212 if (!tmp || !tmp->flags.mapped)
2213 break;
2214 if (lower)
2215 wLowerFrame(tmp->frame->core);
2216 else
2217 wRaiseFrame(tmp->frame->core);
2218 } else {
2219 break;
2223 /* tear off the menu if it's a root menu or a cascade
2224 application menu */
2225 if (!menu->flags.buttoned && !menu->flags.brother
2226 && (!menu->flags.app_menu||menu->parent!=NULL)) {
2227 menu->flags.buttoned=1;
2228 wFrameWindowShowButton(menu->frame, WFF_RIGHT_BUTTON);
2229 if (menu->parent) {
2230 /* turn off selected menu entry in parent menu */
2231 selectEntry(menu->parent, -1);
2233 /* make parent map the copy in place of the original */
2234 for (i=0; i<menu->parent->cascade_no; i++) {
2235 if (menu->parent->cascades[i] == menu) {
2236 menu->parent->cascades[i] = menu->brother;
2237 break;
2243 started = False;
2244 while(1) {
2245 WMMaskEvent(dpy, ButtonMotionMask|ButtonReleaseMask|ButtonPressMask
2246 |ExposureMask, &ev);
2247 switch (ev.type) {
2248 case MotionNotify:
2249 if (started) {
2250 x += ev.xmotion.x_root - dx;
2251 y += ev.xmotion.y_root - dy;
2252 dx = ev.xmotion.x_root;
2253 dy = ev.xmotion.y_root;
2254 wMenuMove(menu, x, y, True);
2255 } else {
2256 if (abs(ev.xmotion.x_root - dx) > MOVE_THRESHOLD
2257 || abs(ev.xmotion.y_root - dy) > MOVE_THRESHOLD) {
2258 started = True;
2259 XGrabPointer(dpy, menu->frame->titlebar->window, False,
2260 ButtonMotionMask|ButtonReleaseMask
2261 |ButtonPressMask,
2262 GrabModeAsync, GrabModeAsync, None,
2263 wCursor[WCUR_MOVE], CurrentTime);
2266 break;
2268 case ButtonPress:
2269 break;
2271 case ButtonRelease:
2272 if (ev.xbutton.button != event->xbutton.button)
2273 break;
2274 #ifdef DEBUG
2275 printf("End menu move\n");
2276 #endif
2277 XUngrabPointer(dpy, CurrentTime);
2278 return;
2280 default:
2281 WMHandleEvent(&ev);
2282 break;
2288 *----------------------------------------------------------------------
2289 * menuCloseClick--
2290 * Handles mouse click on the close button of menus. The menu is
2291 * closed when the button is clicked.
2293 * Side effects:
2294 * The closed menu is reinserted at it's parent menus
2295 * cascade list.
2296 *----------------------------------------------------------------------
2298 static void
2299 menuCloseClick(WCoreWindow *sender, void *data, XEvent *event)
2301 WMenu *menu = (WMenu*)data;
2302 WMenu *parent = menu->parent;
2303 int i;
2305 if (parent) {
2306 for (i=0; i<parent->cascade_no; i++) {
2307 /* find the entry that points to the copy */
2308 if (parent->cascades[i] == menu->brother) {
2309 /* make it point to the original */
2310 parent->cascades[i] = menu;
2311 menu->parent = parent;
2312 break;
2316 wMenuUnmap(menu);
2320 void
2321 wMenuSaveState(WScreen *scr)
2323 proplist_t menus, key, value;
2324 int save_menus = 0;
2325 char buffer[256];
2327 menus = PLMakeDictionaryFromEntries(NULL, NULL, NULL);
2329 #ifndef LITE
2330 if (scr->root_menu && scr->root_menu->flags.buttoned) {
2331 sprintf(buffer, "%i,%i", scr->root_menu->frame_x,
2332 scr->root_menu->frame_y);
2333 key = PLMakeString("RootMenu");
2334 value = PLMakeString(buffer);
2335 PLInsertDictionaryEntry(menus, key, value);
2336 PLRelease(key);
2337 PLRelease(value);
2338 save_menus = 1;
2341 if (scr->switch_menu && scr->switch_menu->flags.buttoned) {
2342 sprintf(buffer, "%i,%i", scr->switch_menu->frame_x,
2343 scr->switch_menu->frame_y);
2344 key = PLMakeString("SwitchMenu");
2345 value = PLMakeString(buffer);
2346 PLInsertDictionaryEntry(menus, key, value);
2347 PLRelease(key);
2348 PLRelease(value);
2349 save_menus = 1;
2351 #endif /* !LITE */
2352 if (scr->workspace_menu && scr->workspace_menu->flags.buttoned) {
2353 sprintf(buffer, "%i,%i", scr->workspace_menu->frame_x,
2354 scr->workspace_menu->frame_y);
2355 key = PLMakeString("WorkspaceMenu");
2356 value = PLMakeString(buffer);
2357 PLInsertDictionaryEntry(menus, key, value);
2358 PLRelease(key);
2359 PLRelease(value);
2360 save_menus = 1;
2363 if (save_menus) {
2364 key = PLMakeString("Menus");
2365 PLInsertDictionaryEntry(scr->session_state, key, menus);
2366 PLRelease(key);
2368 PLRelease(menus);
2372 #define COMPLAIN(key) wwarning(_("bad value in menus state info:%s"), key)
2375 static int
2376 restoreMenu(WScreen *scr, proplist_t menu, int which)
2378 int i, x, y;
2379 WMenu *pmenu = NULL;
2381 if (!menu)
2382 return False;
2384 if (!PLIsString(menu)) {
2385 COMPLAIN("Position");
2386 return False;
2389 if (sscanf(PLGetString(menu), "%i,%i", &x, &y)!=2)
2390 COMPLAIN("Position");
2392 #ifndef LITE
2393 if (which & WSS_ROOTMENU) {
2394 OpenRootMenu(scr, x, y, False);
2395 pmenu = scr->root_menu;
2396 } else if (which & WSS_SWITCHMENU) {
2397 OpenSwitchMenu(scr, x, y, False);
2398 pmenu = scr->switch_menu;
2399 } else
2400 #endif /* !LITE */
2401 if (which & WSS_WSMENU) {
2402 OpenWorkspaceMenu(scr, x, y);
2403 pmenu = scr->workspace_menu;
2404 if (pmenu->parent) {
2405 /* make parent map the copy in place of the original */
2406 for (i=0; i<pmenu->parent->cascade_no; i++) {
2407 if (pmenu->parent->cascades[i] == pmenu) {
2408 pmenu->parent->cascades[i] = pmenu->brother;
2409 break;
2415 if (pmenu) {
2416 int width = MENUW(pmenu);
2417 int height = MENUH(pmenu);
2419 x = (x < -width) ? 0 : x;
2420 x = (x > scr->scr_width) ? scr->scr_width - width : x;
2421 y = (y < 0) ? 0 : y;
2422 y = (y > scr->scr_height) ? scr->scr_height - height : y;
2423 wMenuMove(pmenu, x, y, True);
2424 pmenu->flags.buttoned = 1;
2425 wFrameWindowShowButton(pmenu->frame, WFF_RIGHT_BUTTON);
2426 return True;
2428 return False;
2432 void
2433 wMenuRestoreState(WScreen *scr)
2435 proplist_t menus, menu, key, rkey, skey, wkey;
2437 key = PLMakeString("Menus");
2438 menus = PLGetDictionaryEntry(scr->session_state, key);
2439 PLRelease(key);
2441 if (!menus)
2442 return;
2444 /* restore menus */
2446 rkey = PLMakeString("RootMenu");
2447 skey = PLMakeString("SwitchMenu");
2448 wkey = PLMakeString("WorkspaceMenu");
2449 menu = PLGetDictionaryEntry(menus, rkey);
2450 restoreMenu(scr, menu, WSS_ROOTMENU);
2451 menu = PLGetDictionaryEntry(menus, skey);
2452 restoreMenu(scr, menu, WSS_SWITCHMENU);
2453 menu = PLGetDictionaryEntry(menus, wkey);
2454 restoreMenu(scr, menu, WSS_WSMENU);
2456 PLRelease(rkey);
2457 PLRelease(skey);
2458 PLRelease(wkey);
2462 void
2463 OpenWorkspaceMenu(WScreen *scr, int x, int y)
2465 WMenu *menu, *parent;
2466 WMenuEntry *entry;
2468 #ifndef LITE
2469 if (!scr->root_menu) {
2470 OpenRootMenu(scr, scr->scr_width*2, 0, False);
2471 wMenuUnmap(scr->root_menu);
2473 #endif
2474 menu = scr->workspace_menu;
2475 if (menu) {
2476 if (menu->flags.mapped) {
2477 if (!menu->flags.buttoned) {
2478 wMenuUnmap(menu);
2479 parent = menu->parent;
2480 if (parent && parent->selected_entry >= 0) {
2481 entry = parent->entries[parent->selected_entry];
2482 if (parent->cascades[entry->cascade] == menu) {
2483 selectEntry(parent, -1);
2484 wMenuMapAt(menu, x, y, False);
2487 } else {
2488 wRaiseFrame(menu->frame->core);
2489 wMenuMapCopyAt(menu, x, y);
2492 else {
2493 wMenuMapAt(menu, x, y, False);