*** empty log message ***
[wmaker-crm.git] / src / menu.c
blob94736dc140747211f81c445468f3ee3cef60b591
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;
269 WMenuEntry*
270 wMenuInsertCallback(WMenu *menu, int index, char *text,
271 void (*callback)(WMenu *menu, WMenuEntry *entry),
272 void *clientdata)
274 WMenuEntry *entry;
276 #ifdef DEBUG
277 if (!menu) {
278 printf("Passed NULL as menu parameter to wMenuAddCallback() \n");
279 return NULL;
281 #endif
283 assert(menu->flags.brother==0);
284 menu->flags.realized = 0;
285 menu->brother->flags.realized = 0;
287 /* reallocate array if it's too small */
288 if (menu->entry_no >= menu->alloced_entries) {
289 void *tmp;
290 #ifdef DEBUG
291 puts("doing wrealloc()");
292 #endif
293 tmp = wrealloc(menu->entries,
294 sizeof(WMenuEntry)*(menu->alloced_entries+5));
295 if (tmp==NULL) {
296 wwarning(_("wrealloc() failed while trying to add menu item"));
297 return NULL;
300 menu->entries = tmp;
301 menu->alloced_entries += 5;
303 menu->brother->entries = tmp;
304 menu->brother->alloced_entries = menu->alloced_entries;
306 entry = wmalloc(sizeof(WMenuEntry));
307 memset(entry, 0, sizeof(WMenuEntry));
308 entry->flags.enabled = 1;
309 entry->text = wstrdup(text);
310 entry->cascade = -1;
311 entry->clientdata = clientdata;
312 entry->callback = callback;
313 if (index<0 || index>=menu->entry_no) {
314 entry->order = menu->entry_no;
315 menu->entries[menu->entry_no] = entry;
316 } else {
317 entry->order = index;
318 insertEntry(menu, entry, index);
321 menu->entry_no++;
322 menu->brother->entry_no = menu->entry_no;
324 return entry;
329 void
330 wMenuEntrySetCascade(WMenu *menu, WMenuEntry *entry, WMenu *cascade)
332 WMenu *brother = menu->brother;
333 int i, done;
335 assert(menu->flags.brother==0);
337 if (entry->cascade>=0) {
338 menu->flags.realized = 0;
339 brother->flags.realized = 0;
342 cascade->parent = menu;
344 cascade->brother->parent = brother;
346 done = 0;
347 for (i=0; i<menu->cascade_no; i++) {
348 if (menu->cascades[i]==NULL) {
349 menu->cascades[i] = cascade;
350 brother->cascades[i] = cascade->brother;
351 done = 1;
352 entry->cascade = i;
353 break;
356 if (!done) {
357 entry->cascade = menu->cascade_no;
359 menu->cascades = wrealloc(menu->cascades,
360 sizeof(WMenu)*(menu->cascade_no+1));
361 menu->cascades[menu->cascade_no++] = cascade;
364 brother->cascades = wrealloc(brother->cascades,
365 sizeof(WMenu)*(brother->cascade_no+1));
366 brother->cascades[brother->cascade_no++] = cascade->brother;
370 if (menu->flags.lowered) {
372 cascade->flags.lowered = 1;
373 ChangeStackingLevel(cascade->frame->core, WMNormalLevel);
375 cascade->brother->flags.lowered = 1;
376 ChangeStackingLevel(cascade->brother->frame->core, WMNormalLevel);
379 if (!menu->flags.realized)
380 wMenuRealize(menu);
384 void
385 wMenuEntryRemoveCascade(WMenu *menu, WMenuEntry *entry)
387 assert(menu->flags.brother==0);
389 /* destroy cascade menu */
390 if (entry->cascade>=0 && menu->cascades
391 && menu->cascades[entry->cascade]!=NULL) {
393 wMenuDestroy(menu->cascades[entry->cascade], True);
395 menu->cascades[entry->cascade] = NULL;
396 menu->brother->cascades[entry->cascade] = NULL;
398 entry->cascade = -1;
403 void
404 wMenuRemoveItem(WMenu *menu, int index)
406 int i;
408 if (menu->flags.brother) {
409 wMenuRemoveItem(menu->brother, index);
410 return;
413 if (index>=menu->entry_no) return;
415 /* destroy cascade menu */
416 wMenuEntryRemoveCascade(menu, menu->entries[index]);
418 /* destroy unshared data */
420 if (menu->entries[index]->text)
421 free(menu->entries[index]->text);
423 if (menu->entries[index]->rtext)
424 free(menu->entries[index]->rtext);
426 if (menu->entries[index]->free_cdata && menu->entries[index]->clientdata)
427 (*menu->entries[index]->free_cdata)(menu->entries[index]->clientdata);
429 free(menu->entries[index]);
431 for (i=index; i<menu->entry_no-1; i++) {
432 menu->entries[i+1]->order--;
433 menu->entries[i]=menu->entries[i+1];
435 menu->entry_no--;
436 menu->brother->entry_no--;
440 static void
441 updateTexture(WMenu *menu)
443 WScreen *scr = menu->menu->screen_ptr;
445 /* setup background texture */
446 if (scr->menu_item_texture->any.type != WTEX_SOLID) {
447 if (!menu->flags.brother) {
448 FREE_PIXMAP(menu->menu_texture_data);
450 if (wPreferences.alt_menu_style) {
451 wTextureRender(scr, scr->menu_item_texture,
452 &menu->menu_texture_data, menu->menu->width,
453 menu->menu->height, WREL_MENUENTRY);
454 } else {
455 wTextureRender(scr, scr->menu_item_texture,
456 &menu->menu_texture_data, menu->menu->width,
457 menu->entry_height, WREL_MENUENTRY);
460 XSetWindowBackgroundPixmap(dpy, menu->menu->window,
461 menu->menu_texture_data);
462 XClearWindow(dpy, menu->menu->window);
464 XSetWindowBackgroundPixmap(dpy, menu->brother->menu->window,
465 menu->menu_texture_data);
466 XClearWindow(dpy, menu->brother->menu->window);
468 } else {
469 XSetWindowBackground(dpy, menu->menu->window,
470 scr->menu_item_texture->any.color.pixel);
471 XClearWindow(dpy, menu->menu->window);
476 void
477 wMenuRealize(WMenu *menu)
479 int i;
480 int width, rwidth, mrwidth, mwidth;
481 int theight, twidth, eheight;
482 WScreen *scr = menu->frame->screen_ptr;
483 static int brother_done=0;
484 int flags;
486 if (!brother_done) {
487 brother_done = 1;
488 wMenuRealize(menu->brother);
489 brother_done = 0;
492 flags = WFF_SINGLE_STATE;
493 if (menu->flags.titled)
494 flags |= WFF_TITLEBAR|WFF_RIGHT_BUTTON;
496 wFrameWindowUpdateBorders(menu->frame, flags);
498 if (menu->flags.titled) {
499 twidth = wTextWidth(scr->menu_title_font->font, menu->frame->title,
500 strlen(menu->frame->title));
501 theight = menu->frame->top_width;
502 twidth += theight + (wPreferences.new_style ? 16 : 8);
503 } else {
504 twidth = 0;
505 theight = 0;
507 eheight = scr->menu_entry_font->height + 6;
508 menu->entry_height = eheight;
509 mrwidth = 0;
510 mwidth = 0;
511 for (i=0; i<menu->entry_no; i++) {
512 char *text;
514 /* search widest text */
515 text = menu->entries[i]->text;
516 width = wTextWidth(scr->menu_entry_font->font, text, strlen(text))+10;
518 if (menu->entries[i]->flags.indicator) {
519 width += MENU_INDICATOR_SPACE;
522 if (width > mwidth)
523 mwidth = width;
525 /* search widest text on right */
526 text = menu->entries[i]->rtext;
527 if (text)
528 rwidth = wTextWidth(scr->menu_entry_font->font, text,
529 strlen(text)) + 5;
530 else if (menu->entries[i]->cascade>=0)
531 rwidth = 16;
532 else
533 rwidth = 4;
535 if (rwidth > mrwidth)
536 mrwidth = rwidth;
538 mwidth += mrwidth;
540 if (mwidth < twidth)
541 mwidth = twidth;
544 wCoreConfigure(menu->menu, 0, theight, mwidth, menu->entry_no*eheight -1);
546 wFrameWindowResize(menu->frame, mwidth, menu->entry_no*eheight-1
547 + menu->frame->top_width + menu->frame->bottom_width);
550 updateTexture(menu);
552 menu->flags.realized = 1;
554 if (menu->flags.mapped)
555 wMenuPaint(menu);
556 if (menu->brother->flags.mapped)
557 wMenuPaint(menu->brother);
561 void
562 wMenuDestroy(WMenu *menu, int recurse)
564 int i;
566 WMRemoveNotificationObserver(menu);
568 /* remove any pending timers */
569 if (menu->timer)
570 WMDeleteTimerHandler(menu->timer);
571 menu->timer = NULL;
573 /* call destroy handler */
574 if (menu->on_destroy)
575 (*menu->on_destroy)(menu);
577 /* Destroy items if this menu own them. If this is the "brother" menu,
578 * leave them alone as it is shared by them.
580 if (!menu->flags.brother) {
581 for (i=0; i<menu->entry_no; i++) {
583 free(menu->entries[i]->text);
585 if (menu->entries[i]->rtext)
586 free(menu->entries[i]->rtext);
587 #ifdef USER_MENU
589 if (menu->entries[i]->instances){
590 PLRelease(menu->entries[i]->instances);
592 #endif /* USER_MENU */
594 if (menu->entries[i]->free_cdata && menu->entries[i]->clientdata) {
595 (*menu->entries[i]->free_cdata)(menu->entries[i]->clientdata);
597 free(menu->entries[i]);
600 if (recurse) {
601 for (i=0; i<menu->cascade_no; i++) {
602 if (menu->cascades[i]) {
603 if (menu->cascades[i]->flags.brother)
604 wMenuDestroy(menu->cascades[i]->brother, recurse);
605 else
606 wMenuDestroy(menu->cascades[i], recurse);
611 if (menu->entries)
612 free(menu->entries);
616 FREE_PIXMAP(menu->menu_texture_data);
618 if (menu->cascades)
619 free(menu->cascades);
621 wCoreDestroy(menu->menu);
622 wFrameWindowDestroy(menu->frame);
624 /* destroy copy of this menu */
625 if (!menu->flags.brother && menu->brother)
626 wMenuDestroy(menu->brother, False);
628 free(menu);
632 static void
633 drawFrame(WScreen *scr, Window win, int y, int w, int h)
635 XSegment segs[2];
637 segs[0].x1 = 0;
638 segs[0].y1 = y;
639 segs[0].x2 = w-1;
640 segs[0].y2 = y;
641 segs[1].x1 = 0;
642 segs[1].y1 = y;
643 segs[1].x2 = 0;
644 segs[1].y2 = y + h - 2;
645 XDrawSegments(dpy, win, scr->menu_item_auxtexture->light_gc, segs, 2);
647 XDrawLine(dpy, win, scr->menu_item_auxtexture->dark_gc, 0, y+h-1,
648 w-1, y+h-1);
650 segs[0].x1 = 1;
651 segs[0].y1 = segs[0].y2 = y + h-2;
652 segs[0].x2 = w-1;
653 segs[1].x1 = segs[1].x2 = w-1;
654 segs[1].y1 = y + 1;
655 segs[1].y2 = y + h-2;
656 XDrawSegments(dpy, win, scr->menu_item_auxtexture->dim_gc, segs, 2);
660 static void
661 paintEntry(WMenu *menu, int index, int selected)
663 int x, y, w, h, tw;
664 GC light, dim, dark, textGC;
665 WScreen *scr=menu->frame->screen_ptr;
666 Window win = menu->menu->window;
667 WMenuEntry *entry=menu->entries[index];
669 if (!menu->flags.realized) return;
670 h = menu->entry_height;
671 w = menu->menu->width;
672 y = index * h;
674 light = scr->menu_item_auxtexture->light_gc;
675 dim = scr->menu_item_auxtexture->dim_gc;
676 dark = scr->menu_item_auxtexture->dark_gc;
678 /* paint background */
679 if (selected) {
680 XSetForeground(dpy, scr->select_menu_gc, scr->select_pixel);
681 XFillRectangle(dpy, win, scr->select_menu_gc, 1, y+1, w-2, h-3);
682 if (scr->menu_item_texture->any.type == WTEX_SOLID)
683 drawFrame(scr, win, y, w, h);
684 } else {
685 if (scr->menu_item_texture->any.type == WTEX_SOLID) {
686 XClearArea(dpy, win, 0, y + 1, w - 1, h - 3, False);
687 /* draw the frame */
688 drawFrame(scr, win, y, w, h);
689 } else {
690 XClearArea(dpy, win, 0, y, w, h, False);
694 if (selected) {
695 textGC = scr->select_menu_gc;
696 if (entry->flags.enabled)
697 XSetForeground(dpy, textGC, scr->select_text_pixel);
698 else
699 XSetForeground(dpy, textGC, scr->dtext_pixel);
700 } else if (!entry->flags.enabled) {
701 textGC = scr->disabled_menu_entry_gc;
702 } else {
703 textGC = scr->menu_entry_gc;
705 /* draw text */
706 x = 5;
707 if (entry->flags.indicator)
708 x += MENU_INDICATOR_SPACE + 2;
710 wDrawString(win, scr->menu_entry_font,
711 textGC, x, 3+y+scr->menu_entry_font->y, entry->text,
712 strlen(entry->text));
714 if (entry->cascade>=0) {
715 /* draw the cascade indicator */
716 XDrawLine(dpy,win,dim, w-11, y+6, w-6, y+h/2-1);
717 XDrawLine(dpy,win,light, w-11, y+h-8, w-6, y+h/2-1);
718 XDrawLine(dpy,win,dark, w-12, y+6, w-12, y+h-8);
721 /* draw indicator */
722 if (entry->flags.indicator && entry->flags.indicator_on) {
723 int iw, ih;
724 WPixmap *indicator;
727 switch (entry->flags.indicator_type) {
728 case MI_CHECK:
729 indicator = scr->menu_check_indicator;
730 break;
731 case MI_MINIWINDOW:
732 indicator = scr->menu_mini_indicator;
733 break;
734 case MI_HIDDEN:
735 indicator = scr->menu_hide_indicator;
736 break;
737 case MI_SHADED:
738 indicator = scr->menu_shade_indicator;
739 break;
740 case MI_DIAMOND:
741 default:
742 indicator = scr->menu_radio_indicator;
743 break;
746 iw = indicator->width;
747 ih = indicator->height;
748 XSetClipMask(dpy, scr->copy_gc, indicator->mask);
749 XSetClipOrigin(dpy, scr->copy_gc, 5, y+(h-ih)/2);
750 if (selected)
751 XSetForeground(dpy, scr->copy_gc, scr->black_pixel);
752 else
753 XSetForeground(dpy, scr->copy_gc, scr->mtext_pixel);
754 XFillRectangle(dpy, win, scr->copy_gc, 5, y+(h-ih)/2, iw, ih);
756 XCopyArea(dpy, indicator->image, win, scr->copy_gc, 0, 0,
757 iw, ih, 5, y+(h-ih)/2);
759 XSetClipOrigin(dpy, scr->copy_gc, 0, 0);
762 /* draw right text */
764 if (entry->rtext && entry->cascade<0) {
765 tw = wTextWidth(scr->menu_entry_font->font, entry->rtext,
766 strlen(entry->rtext));
768 wDrawString(win, scr->menu_entry_font, textGC, w-6-tw,
769 3+y+scr->menu_entry_font->y, entry->rtext,
770 strlen(entry->rtext));
775 static void
776 move_menus(WMenu *menu, int x, int y)
778 while (menu->parent) {
779 menu = menu->parent;
780 x -= MENUW(menu);
781 if (!wPreferences.align_menus && menu->selected_entry>=0) {
782 y -= menu->selected_entry*menu->entry_height;
785 wMenuMove(menu, x, y, True);
788 static void
789 makeVisible(WMenu *menu)
791 WScreen *scr = menu->frame->screen_ptr;
792 int x1, y1, x2, y2, new_x, new_y, move;
794 if (menu->entry_no<0) return;
796 x1 = menu->frame_x;
797 y1 = menu->frame_y+menu->frame->top_width
798 + menu->selected_entry*menu->entry_height;
799 x2 = x1 + MENUW(menu);
800 y2 = y1 + menu->entry_height;
802 new_x = x1;
803 new_y = y1;
804 move = 0;
806 if (x1 < 0) {
807 new_x = 0;
808 move = 1;
809 } else if (x2 >= scr->scr_width) {
810 new_x = scr->scr_width - MENUW(menu) - 1;
811 move = 1;
814 if (y1 < 0) {
815 new_y = 0;
816 move = 1;
817 } else if (y2 >= scr->scr_height) {
818 new_y = scr->scr_height - menu->entry_height - 1;
819 move = 1;
822 new_y = new_y - menu->frame->top_width
823 - menu->selected_entry*menu->entry_height;
824 move_menus(menu, new_x, new_y);
828 static int
829 check_key(WMenu *menu, XKeyEvent *event)
831 int i, ch, s;
832 char buffer[32];
834 if (XLookupString(event, buffer, 32, NULL, NULL)<1)
835 return -1;
837 ch = toupper(buffer[0]);
839 s = (menu->selected_entry>=0 ? menu->selected_entry+1 : 0);
841 again:
842 for (i=s; i<menu->entry_no; i++) {
843 if (ch==toupper(menu->entries[i]->text[0])) {
844 return i;
847 /* no match. Retry from start, if previous started from a selected entry */
848 if (s!=0) {
849 s = 0;
850 goto again;
852 return -1;
856 static int
857 keyboardMenu(WMenu *menu)
859 XEvent event;
860 KeySym ksym=NoSymbol;
861 int done=0;
862 int index;
863 WMenuEntry *entry;
864 int old_pos_x = menu->frame_x;
865 int old_pos_y = menu->frame_y;
866 int new_x = old_pos_x, new_y = old_pos_y;
867 int scr_width = menu->frame->screen_ptr->scr_width;
868 int scr_height = menu->frame->screen_ptr->scr_height;
870 if (menu->flags.editing)
871 return False;
874 XGrabKeyboard(dpy, menu->frame->core->window, True, GrabModeAsync,
875 GrabModeAsync, CurrentTime);
877 if (menu->frame_y+menu->frame->top_width >= scr_height)
878 new_y = scr_height - menu->frame->top_width;
880 if (menu->frame_x+MENUW(menu) >= scr_width)
881 new_x = scr_width-MENUW(menu)-1;
883 move_menus(menu, new_x, new_y);
885 while (!done && menu->flags.mapped) {
886 XAllowEvents(dpy, AsyncKeyboard, CurrentTime);
887 WMMaskEvent(dpy, ExposureMask|ButtonMotionMask|ButtonPressMask
888 |ButtonReleaseMask|KeyPressMask|KeyReleaseMask
889 |SubstructureNotifyMask, &event);
891 switch (event.type) {
892 case KeyPress:
893 ksym = XLookupKeysym(&event.xkey, 0);
894 switch (ksym) {
895 case XK_Escape:
896 done = 1;
897 break;
899 case XK_Home:
900 case XK_KP_Home:
901 selectEntry(menu, 0);
902 makeVisible(menu);
903 break;
905 case XK_End:
906 case XK_KP_End:
907 selectEntry(menu, menu->entry_no-1);
908 makeVisible(menu);
909 break;
911 case XK_Up:
912 #ifdef ARROWLESS_KBD
913 case XK_k:
914 #endif
915 case XK_KP_Up:
916 if (menu->selected_entry <= 0)
917 selectEntry(menu, menu->entry_no-1);
918 else
919 selectEntry(menu, menu->selected_entry-1);
920 makeVisible(menu);
921 break;
923 case XK_Down:
924 #ifdef ARROWLESS_KBD
925 case XK_j:
926 #endif
927 case XK_KP_Down:
928 if (menu->selected_entry<0)
929 selectEntry(menu, 0);
930 else if (menu->selected_entry == menu->entry_no-1)
931 selectEntry(menu, 0);
932 else if (menu->selected_entry < menu->entry_no-1)
933 selectEntry(menu, menu->selected_entry+1);
934 makeVisible(menu);
935 break;
937 case XK_Right:
938 #ifdef ARROWLESS_KBD
939 case XK_l:
940 #endif
941 case XK_KP_Right:
942 if (menu->selected_entry>=0) {
943 WMenuEntry *entry;
944 entry = menu->entries[menu->selected_entry];
946 if (entry->cascade >= 0 && menu->cascades
947 && menu->cascades[entry->cascade]->entry_no > 0) {
949 XUngrabKeyboard(dpy, CurrentTime);
951 selectEntry(menu->cascades[entry->cascade], 0);
952 if (!keyboardMenu(menu->cascades[entry->cascade]))
953 done = 1;
955 XGrabKeyboard(dpy, menu->frame->core->window, True,
956 GrabModeAsync, GrabModeAsync,
957 CurrentTime);
960 break;
962 case XK_Left:
963 #ifdef ARROWLESS_KBD
964 case XK_h:
965 #endif
966 case XK_KP_Left:
967 if (menu->parent!=NULL && menu->parent->selected_entry>=0) {
968 selectEntry(menu, -1);
969 move_menus(menu, old_pos_x, old_pos_y);
970 return True;
972 break;
974 case XK_Return:
975 done = 2;
976 break;
978 default:
979 index = check_key(menu, &event.xkey);
980 if (index>=0) {
981 selectEntry(menu, index);
984 break;
986 default:
987 if (event.type==ButtonPress)
988 done = 1;
990 WMHandleEvent(&event);
994 XUngrabKeyboard(dpy, CurrentTime);
996 if (done==2 && menu->selected_entry>=0) {
997 entry = menu->entries[menu->selected_entry];
998 } else {
999 entry = NULL;
1002 if (entry && entry->callback!=NULL && entry->flags.enabled
1003 && entry->cascade < 0) {
1004 #if (MENU_BLINK_COUNT > 0)
1005 int sel = menu->selected_entry;
1006 int i;
1008 for (i=0; i<MENU_BLINK_COUNT; i++) {
1009 paintEntry(menu, sel, False);
1010 XSync(dpy, 0);
1011 wusleep(MENU_BLINK_DELAY);
1012 paintEntry(menu, sel, True);
1013 XSync(dpy, 0);
1014 wusleep(MENU_BLINK_DELAY);
1016 #endif
1017 selectEntry(menu, -1);
1019 if (!menu->flags.buttoned) {
1020 wMenuUnmap(menu);
1021 move_menus(menu, old_pos_x, old_pos_y);
1023 closeCascade(menu);
1025 (*entry->callback)(menu, entry);
1026 } else {
1027 if (!menu->flags.buttoned) {
1028 wMenuUnmap(menu);
1029 move_menus(menu, old_pos_x, old_pos_y);
1031 selectEntry(menu, -1);
1035 /* returns True if returning from a submenu to a parent menu,
1036 * False if exiting from menu */
1037 return False;
1041 void
1042 wMenuMapAt(WMenu *menu, int x, int y, int keyboard)
1044 int scr_width = menu->frame->screen_ptr->scr_width;
1045 int scr_height = menu->frame->screen_ptr->scr_height;
1047 if (!menu->flags.realized) {
1048 menu->flags.realized=1;
1049 wMenuRealize(menu);
1051 if (!menu->flags.mapped) {
1052 if (wPreferences.wrap_menus) {
1053 if (x<0) x = 0;
1054 if (y<0) y = 0;
1055 if (x+MENUW(menu) > scr_width)
1056 x = scr_width - MENUW(menu);
1057 if (y+MENUH(menu) > scr_height)
1058 y = scr_height - MENUH(menu);
1061 XMoveWindow(dpy, menu->frame->core->window, x, y);
1062 menu->frame_x = x;
1063 menu->frame_y = y;
1064 XMapWindow(dpy, menu->frame->core->window);
1065 wRaiseFrame(menu->frame->core);
1066 menu->flags.mapped = 1;
1067 } else {
1068 selectEntry(menu, 0);
1071 if (keyboard)
1072 keyboardMenu(menu);
1076 void
1077 wMenuMap(WMenu *menu)
1079 if (!menu->flags.realized) {
1080 menu->flags.realized=1;
1081 wMenuRealize(menu);
1083 if (menu->flags.app_menu && menu->parent==NULL) {
1084 menu->frame_x = menu->frame->screen_ptr->app_menu_x;
1085 menu->frame_y = menu->frame->screen_ptr->app_menu_y;
1086 XMoveWindow(dpy, menu->frame->core->window, menu->frame_x, menu->frame_y);
1088 XMapWindow(dpy, menu->frame->core->window);
1089 wRaiseFrame(menu->frame->core);
1090 menu->flags.mapped = 1;
1094 void
1095 wMenuUnmap(WMenu *menu)
1097 int i;
1099 XUnmapWindow(dpy, menu->frame->core->window);
1100 if (menu->flags.titled && menu->flags.buttoned) {
1101 wFrameWindowHideButton(menu->frame, WFF_RIGHT_BUTTON);
1103 menu->flags.buttoned = 0;
1104 menu->flags.mapped = 0;
1105 menu->flags.open_to_left = 0;
1107 for (i=0; i<menu->cascade_no; i++) {
1108 if (menu->cascades[i]!=NULL
1109 && menu->cascades[i]->flags.mapped
1110 && !menu->cascades[i]->flags.buttoned) {
1112 wMenuUnmap(menu->cascades[i]);
1115 menu->selected_entry = -1;
1120 void
1121 wMenuPaint(WMenu *menu)
1123 int i;
1125 if (!menu->flags.mapped) {
1126 return;
1129 /* paint entries */
1130 for (i=0; i<menu->entry_no; i++) {
1131 paintEntry(menu, i, i==menu->selected_entry);
1136 void
1137 wMenuSetEnabled(WMenu *menu, int index, int enable)
1139 if (index>=menu->entry_no) return;
1140 menu->entries[index]->flags.enabled=enable;
1141 paintEntry(menu, index, index==menu->selected_entry);
1142 paintEntry(menu->brother, index, index==menu->selected_entry);
1146 /* ====================================================================== */
1149 static void
1150 editEntry(WMenu *menu, WMenuEntry *entry)
1152 WTextInput *text;
1153 XEvent event;
1154 WObjDescriptor *desc;
1155 char *t;
1156 int done = 0;
1157 Window old_focus;
1158 int old_revert;
1160 menu->flags.editing = 1;
1162 text = wTextCreate(menu->menu, 1, menu->entry_height * entry->order,
1163 menu->menu->width - 2, menu->entry_height - 1);
1165 wTextPutText(text, entry->text);
1166 XGetInputFocus(dpy, &old_focus, &old_revert);
1167 XSetInputFocus(dpy, text->core->window, RevertToNone, CurrentTime);
1169 if (XGrabKeyboard(dpy, text->core->window, True, GrabModeAsync,
1170 GrabModeAsync, CurrentTime)!=GrabSuccess) {
1171 wwarning("could not grab keyboard");
1172 wTextDestroy(text);
1174 wSetFocusTo(menu->frame->screen_ptr,
1175 menu->frame->screen_ptr->focused_window);
1176 return;
1180 while (!done && !text->done) {
1181 XSync(dpy, 0);
1182 XAllowEvents(dpy, AsyncKeyboard|AsyncPointer, CurrentTime);
1183 XSync(dpy, 0);
1184 WMNextEvent(dpy, &event);
1186 if (XFindContext(dpy, event.xany.window, wWinContext,
1187 (XPointer *)&desc)==XCNOENT)
1188 desc = NULL;
1190 if ((desc != NULL) && (desc->handle_anything != NULL)) {
1192 (*desc->handle_anything)(desc, &event);
1194 } else {
1195 switch (event.type) {
1196 case ButtonPress:
1197 XAllowEvents(dpy, ReplayPointer, CurrentTime);
1198 done = 1;
1200 default:
1201 WMHandleEvent(&event);
1202 break;
1207 XSetInputFocus(dpy, old_focus, old_revert, CurrentTime);
1209 wSetFocusTo(menu->frame->screen_ptr,
1210 menu->frame->screen_ptr->focused_window);
1213 t = wTextGetText(text);
1214 /* if !t, the user has canceled editing */
1215 if (t) {
1216 if (entry->text)
1217 free(entry->text);
1218 entry->text = wstrdup(t);
1220 menu->flags.realized = 0;
1222 wTextDestroy(text);
1224 XUngrabKeyboard(dpy, CurrentTime);
1226 if (t && menu->on_edit)
1227 (*menu->on_edit)(menu, entry);
1229 menu->flags.editing = 0;
1231 if (!menu->flags.realized)
1232 wMenuRealize(menu);
1236 static void
1237 selectEntry(WMenu *menu, int entry_no)
1239 WMenuEntry *entry;
1240 WMenu *submenu;
1241 int old_entry;
1243 if (menu->entries==NULL)
1244 return;
1246 if (entry_no >= menu->entry_no)
1247 return;
1249 old_entry = menu->selected_entry;
1250 menu->selected_entry = entry_no;
1252 if (old_entry!=entry_no) {
1254 /* unselect previous entry */
1255 if (old_entry>=0) {
1256 paintEntry(menu, old_entry, False);
1257 entry = menu->entries[old_entry];
1259 /* unmap cascade */
1260 if (entry->cascade>=0 && menu->cascades) {
1261 if (!menu->cascades[entry->cascade]->flags.buttoned) {
1262 wMenuUnmap(menu->cascades[entry->cascade]);
1267 if (entry_no<0) {
1268 menu->selected_entry = -1;
1269 return;
1271 entry = menu->entries[entry_no];
1273 if (entry->cascade>=0 && menu->cascades && entry->flags.enabled) {
1274 /* Callback for when the submenu is opened.
1276 submenu = menu->cascades[entry->cascade];
1277 if (submenu && submenu->flags.brother)
1278 submenu = submenu->brother;
1280 if (entry->callback) {
1281 /* Only call the callback if the submenu is not yet mapped.
1283 if (menu->flags.brother) {
1284 if (!submenu || !submenu->flags.mapped)
1285 (*entry->callback)(menu->brother, entry);
1286 } else {
1287 if (!submenu || !submenu->flags.buttoned)
1288 (*entry->callback)(menu, entry);
1292 /* the submenu menu might have changed */
1293 submenu = menu->cascades[entry->cascade];
1295 /* map cascade */
1296 if (!submenu->flags.mapped) {
1297 int x, y;
1299 if (!submenu->flags.realized)
1300 wMenuRealize(submenu);
1301 if (wPreferences.wrap_menus) {
1302 if (menu->flags.open_to_left)
1303 submenu->flags.open_to_left = 1;
1305 if (submenu->flags.open_to_left) {
1306 x = menu->frame_x - MENUW(submenu);
1307 if (x<0) {
1308 x = 0;
1309 submenu->flags.open_to_left = 0;
1311 } else {
1312 x = menu->frame_x + MENUW(menu);
1314 if (x + MENUW(submenu)
1315 >= menu->frame->screen_ptr->scr_width) {
1317 x = menu->frame_x - MENUW(submenu);
1318 submenu->flags.open_to_left = 1;
1321 } else {
1322 x = menu->frame_x + MENUW(menu);
1325 if (wPreferences.align_menus) {
1326 y = menu->frame_y;
1327 } else {
1328 y = menu->frame_y + menu->entry_height*entry_no;
1329 if (menu->flags.titled)
1330 y += menu->frame->top_width;
1331 if (menu->cascades[entry->cascade]->flags.titled)
1332 y -= menu->cascades[entry->cascade]->frame->top_width;
1335 wMenuMapAt(menu->cascades[entry->cascade], x, y, False);
1336 menu->cascades[entry->cascade]->parent = menu;
1337 } else {
1338 return;
1341 paintEntry(menu, entry_no, True);
1346 static WMenu*
1347 findMenu(WScreen *scr, int *x_ret, int *y_ret)
1349 WMenu *menu;
1350 WObjDescriptor *desc;
1351 Window root_ret, win, junk_win;
1352 int x, y, wx, wy;
1353 unsigned int mask;
1355 XQueryPointer(dpy, scr->root_win, &root_ret, &win, &x, &y, &wx, &wy,
1356 &mask);
1358 if (win==None) return NULL;
1360 if (XFindContext(dpy, win, wWinContext, (XPointer *)&desc)==XCNOENT)
1361 return NULL;
1363 if (desc->parent_type == WCLASS_MENU) {
1364 menu = (WMenu*)desc->parent;
1365 XTranslateCoordinates(dpy, root_ret, menu->menu->window, wx, wy,
1366 x_ret, y_ret, &junk_win);
1367 return menu;
1369 return NULL;
1375 static void
1376 closeCascade(WMenu *menu)
1378 WMenu *parent=menu->parent;
1380 if (menu->flags.brother
1381 || (!menu->flags.buttoned
1382 && (!menu->flags.app_menu||menu->parent!=NULL))) {
1384 selectEntry(menu, -1);
1385 XSync(dpy, 0);
1386 #if (MENU_BLINK_DELAY > 2)
1387 wusleep(MENU_BLINK_DELAY/2);
1388 #endif
1389 wMenuUnmap(menu);
1390 while (parent!=NULL
1391 && (parent->parent!=NULL || !parent->flags.app_menu
1392 || parent->flags.brother)
1393 && !parent->flags.buttoned) {
1394 selectEntry(parent, -1);
1395 wMenuUnmap(parent);
1396 parent = parent->parent;
1398 if (parent)
1399 selectEntry(parent, -1);
1404 static void
1405 closeBrotherCascadesOf(WMenu *menu)
1407 WMenu *tmp;
1408 int i;
1410 for (i=0; i<menu->cascade_no; i++) {
1411 if (menu->cascades[i]->flags.brother) {
1412 tmp = menu->cascades[i];
1413 } else {
1414 tmp = menu->cascades[i]->brother;
1416 if (tmp->flags.mapped) {
1417 selectEntry(tmp->parent, -1);
1418 closeBrotherCascadesOf(tmp);
1419 break;
1425 #define getEntryAt(menu, x, y) ((y)<0 ? -1 : (y)/(menu->entry_height))
1428 static WMenu*
1429 parentMenu(WMenu *menu)
1431 WMenu *parent;
1432 WMenuEntry *entry;
1434 if (menu->flags.buttoned)
1435 return menu;
1437 while (menu->parent && menu->parent->flags.mapped) {
1438 parent = menu->parent;
1439 if (parent->selected_entry < 0)
1440 break;
1441 entry = parent->entries[parent->selected_entry];
1442 if (!entry->flags.enabled || entry->cascade<0 || !parent->cascades ||
1443 parent->cascades[entry->cascade] != menu)
1444 break;
1445 menu = parent;
1446 if (menu->flags.buttoned)
1447 break;
1450 return menu;
1456 * Will raise the passed menu, if submenu = 0
1457 * If submenu > 0 will also raise all mapped submenus
1458 * until the first buttoned one
1459 * If submenu < 0 will also raise all mapped parent menus
1460 * until the first buttoned one
1463 static void
1464 raiseMenus(WMenu *menu, int submenus)
1466 WMenu *submenu;
1467 int i;
1469 if(!menu) return;
1471 wRaiseFrame(menu->frame->core);
1473 if (submenus>0 && menu->selected_entry>=0) {
1474 i = menu->entries[menu->selected_entry]->cascade;
1475 if (i>=0 && menu->cascades) {
1476 submenu = menu->cascades[i];
1477 if (submenu->flags.mapped && !submenu->flags.buttoned)
1478 raiseMenus(submenu, submenus);
1481 if (submenus<0 && !menu->flags.buttoned &&
1482 menu->parent && menu->parent->flags.mapped)
1483 raiseMenus(menu->parent, submenus);
1487 WMenu*
1488 wMenuUnderPointer(WScreen *screen)
1490 WObjDescriptor *desc;
1491 Window root_ret, win;
1492 int dummy;
1493 unsigned int mask;
1495 XQueryPointer(dpy, screen->root_win, &root_ret, &win, &dummy, &dummy,
1496 &dummy, &dummy, &mask);
1498 if (win==None) return NULL;
1500 if (XFindContext(dpy, win, wWinContext, (XPointer *)&desc)==XCNOENT)
1501 return NULL;
1503 if (desc->parent_type == WCLASS_MENU)
1504 return (WMenu *)desc->parent;
1505 return NULL;
1510 #define MIN(a,b) (((a) > (b)) ? (b) : (a))
1513 static void
1514 getPointerPosition(WScreen *scr, int *x, int *y)
1516 Window root_ret, win;
1517 int wx, wy;
1518 unsigned int mask;
1520 XQueryPointer(dpy, scr->root_win, &root_ret, &win, x, y, &wx, &wy, &mask);
1524 static void
1525 getScrollAmount(WMenu *menu, int *hamount, int *vamount)
1527 WScreen *scr = menu->menu->screen_ptr;
1528 int menuX1 = menu->frame_x;
1529 int menuY1 = menu->frame_y;
1530 int menuX2 = menu->frame_x + MENUW(menu);
1531 int menuY2 = menu->frame_y + MENUH(menu);
1532 int screenW = scr->scr_width;
1533 int screenH = scr->scr_height;
1534 int xroot, yroot;
1536 *hamount = 0;
1537 *vamount = 0;
1539 getPointerPosition(scr, &xroot, &yroot);
1542 if (xroot <= 1 && menuX1 < 0) {
1543 /* scroll to the right */
1544 *hamount = MIN(MENU_SCROLL_STEP, abs(menuX1));
1546 } else if (xroot >= screenW-2 && menuX2 > screenW-1) {
1547 /* scroll to the left */
1548 *hamount = MIN(MENU_SCROLL_STEP, abs(menuX2-screenW-1));
1550 if (*hamount==0)
1551 *hamount = 1;
1553 *hamount = -*hamount;
1556 if (yroot <= 1 && menuY1 < 0) {
1557 /* scroll down */
1558 *vamount = MIN(MENU_SCROLL_STEP, abs(menuY1));
1560 } else if (yroot >= screenH-2 && menuY2 > screenH-1) {
1561 /* scroll up */
1562 *vamount = MIN(MENU_SCROLL_STEP, abs(menuY2-screenH-2));
1564 *vamount = -*vamount;
1569 static void
1570 dragScrollMenuCallback(void *data)
1572 WMenu *menu = (WMenu*)data;
1573 WScreen *scr = menu->menu->screen_ptr;
1574 WMenu *parent = parentMenu(menu);
1575 int hamount, vamount;
1576 int x, y;
1577 int newSelectedEntry;
1579 getScrollAmount(menu, &hamount, &vamount);
1582 if (hamount != 0 || vamount != 0) {
1583 wMenuMove(parent, parent->frame_x + hamount,
1584 parent->frame_y + vamount, True);
1585 if (findMenu(scr, &x, &y)) {
1586 newSelectedEntry = getEntryAt(menu, x, y);
1587 selectEntry(menu, newSelectedEntry);
1588 } else {
1589 /* Pointer fell outside of menu. If the selected entry is
1590 * not a submenu, unselect it */
1591 if (menu->selected_entry >= 0
1592 && menu->entries[menu->selected_entry]->cascade<0)
1593 selectEntry(menu, -1);
1594 newSelectedEntry = 0;
1597 /* paranoid check */
1598 if (newSelectedEntry >= 0) {
1599 /* keep scrolling */
1600 menu->timer = WMAddTimerHandler(MENU_SCROLL_DELAY,
1601 dragScrollMenuCallback, menu);
1602 } else {
1603 menu->timer = NULL;
1605 } else {
1606 /* don't need to scroll anymore */
1607 menu->timer = NULL;
1608 if (findMenu(scr, &x, &y)) {
1609 newSelectedEntry = getEntryAt(menu, x, y);
1610 selectEntry(menu, newSelectedEntry);
1616 static void
1617 scrollMenuCallback(void *data)
1619 WMenu *menu = (WMenu*)data;
1620 WMenu *parent = parentMenu(menu);
1621 int hamount = 0; /* amount to scroll */
1622 int vamount = 0;
1624 getScrollAmount(menu, &hamount, &vamount);
1626 if (hamount != 0 || vamount != 0) {
1627 wMenuMove(parent, parent->frame_x + hamount,
1628 parent->frame_y + vamount, True);
1630 /* keep scrolling */
1631 menu->timer = WMAddTimerHandler(MENU_SCROLL_DELAY,
1632 scrollMenuCallback, menu);
1633 } else {
1634 /* don't need to scroll anymore */
1635 menu->timer = NULL;
1641 #define MENU_SCROLL_BORDER 5
1643 static int
1644 isPointNearBoder(WMenu *menu, int x, int y)
1646 int menuX1 = menu->frame_x;
1647 int menuY1 = menu->frame_y;
1648 int menuX2 = menu->frame_x + MENUW(menu);
1649 int menuY2 = menu->frame_y + MENUH(menu);
1650 int scrXe = menu->menu->screen_ptr->scr_width-1;
1651 int scrYe = menu->menu->screen_ptr->scr_height-1;
1652 int flag = 0;
1654 if (x >= menuX1 && x <= menuX2 && (y < MENU_SCROLL_BORDER
1655 || y > scrYe-MENU_SCROLL_BORDER))
1656 flag = 1;
1657 else if (y >= menuY1 && y <= menuY2 && (x < MENU_SCROLL_BORDER
1658 || x > scrXe-MENU_SCROLL_BORDER))
1659 flag = 1;
1661 return flag;
1665 void
1666 wMenuScroll(WMenu *menu, XEvent *event)
1668 WMenu *smenu;
1669 WMenu *omenu = parentMenu(menu);
1670 WScreen *scr = menu->frame->screen_ptr;
1671 int done = 0;
1672 int jump_back = 0;
1673 int old_frame_x = omenu->frame_x;
1674 int old_frame_y = omenu->frame_y;
1675 XEvent ev;
1677 #ifdef DEBUG
1678 puts("Entering menu Scroll");
1679 #endif
1681 if ((/*omenu->flags.buttoned &&*/ !wPreferences.wrap_menus)
1682 || omenu->flags.app_menu) {
1683 jump_back = 1;
1686 if (!wPreferences.wrap_menus)
1687 raiseMenus(omenu, True);
1688 else
1689 raiseMenus(menu, False);
1691 if (!menu->timer)
1692 scrollMenuCallback(menu);
1694 while(!done) {
1695 int x, y, on_border, on_x_edge, on_y_edge, on_title;
1697 WMNextEvent(dpy, &ev);
1698 switch (ev.type) {
1699 case EnterNotify:
1700 WMHandleEvent(&ev);
1701 case MotionNotify:
1702 x = (ev.type==MotionNotify) ? ev.xmotion.x_root : ev.xcrossing.x_root;
1703 y = (ev.type==MotionNotify) ? ev.xmotion.y_root : ev.xcrossing.y_root;
1705 /* on_border is != 0 if the pointer is between the menu
1706 * and the screen border and is close enough to the border */
1707 on_border = isPointNearBoder(menu, x, y);
1709 smenu = wMenuUnderPointer(scr);
1711 if ((smenu==NULL && !on_border) || (smenu && parentMenu(smenu)!=omenu)) {
1712 done = 1;
1713 break;
1716 on_x_edge = x <= 1 || x >= scr->scr_width - 2;
1717 on_y_edge = y <= 1 || y >= scr->scr_height - 2;
1718 on_border = on_x_edge || on_y_edge;
1720 if (!on_border && !jump_back) {
1721 done = 1;
1722 break;
1725 if (menu->timer && (smenu!=menu || (!on_y_edge && !on_x_edge))) {
1726 WMDeleteTimerHandler(menu->timer);
1727 menu->timer = NULL;
1730 if (smenu != NULL)
1731 menu = smenu;
1733 if (!menu->timer)
1734 scrollMenuCallback(menu);
1735 break;
1736 case ButtonPress:
1737 /* True if we push on title, or drag the omenu to other position */
1738 on_title = ev.xbutton.x_root >= omenu->frame_x &&
1739 ev.xbutton.x_root <= omenu->frame_x + MENUW(omenu) &&
1740 ev.xbutton.y_root >= omenu->frame_y &&
1741 ev.xbutton.y_root <= omenu->frame_y + omenu->frame->top_width;
1742 WMHandleEvent(&ev);
1743 smenu = wMenuUnderPointer(scr);
1744 if (smenu == NULL || (smenu && smenu->flags.buttoned && smenu != omenu))
1745 done = 1;
1746 else if (smenu==omenu && on_title) {
1747 jump_back = 0;
1748 done = 1;
1750 break;
1751 case KeyPress:
1752 done = 1;
1753 default:
1754 WMHandleEvent(&ev);
1755 break;
1759 if (menu->timer) {
1760 WMDeleteTimerHandler(menu->timer);
1761 menu->timer = NULL;
1764 if (jump_back)
1765 wMenuMove(omenu, old_frame_x, old_frame_y, True);
1767 #ifdef DEBUG
1768 puts("Leaving menu Scroll");
1769 #endif
1774 static void
1775 menuExpose(WObjDescriptor *desc, XEvent *event)
1777 wMenuPaint(desc->parent);
1780 typedef struct {
1781 int *delayed_select;
1782 WMenu *menu;
1783 WMHandlerID magic;
1784 } delay_data;
1787 static void
1788 delaySelection(void *data)
1790 delay_data *d = (delay_data*)data;
1791 int x, y, entry_no;
1792 WMenu *menu;
1794 d->magic = NULL;
1796 menu = findMenu(d->menu->menu->screen_ptr, &x, &y);
1797 if (menu && (d->menu == menu || d->delayed_select)) {
1798 entry_no = getEntryAt(menu, x, y);
1799 selectEntry(menu, entry_no);
1801 if (d->delayed_select)
1802 *(d->delayed_select) = 0;
1806 static void
1807 menuMouseDown(WObjDescriptor *desc, XEvent *event)
1809 XButtonEvent *bev = &event->xbutton;
1810 WMenu *menu = desc->parent;
1811 WMenu *smenu;
1812 WScreen *scr=menu->frame->screen_ptr;
1813 WMenuEntry *entry=NULL;
1814 XEvent ev;
1815 int close_on_exit=0;
1816 int done=0;
1817 int delayed_select = 0;
1818 int entry_no;
1819 int x, y;
1820 int prevx, prevy;
1821 int old_frame_x = 0;
1822 int old_frame_y = 0;
1823 delay_data d_data = {NULL, NULL, NULL};
1825 if (!wPreferences.wrap_menus) {
1826 smenu = parentMenu(menu);
1827 old_frame_x = smenu->frame_x;
1828 old_frame_y = smenu->frame_y;
1830 else if (event->xbutton.window == menu->frame->core->window) {
1831 /* This is true if the menu was launched with right click on root window */
1832 delayed_select = 1;
1833 d_data.delayed_select = &delayed_select;
1834 d_data.menu = menu;
1835 d_data.magic = WMAddTimerHandler(wPreferences.dblclick_time,
1836 delaySelection, &d_data);
1839 wRaiseFrame(menu->frame->core);
1841 close_on_exit = (bev->send_event || menu->flags.brother);
1843 smenu = findMenu(scr, &x, &y);
1844 if (!smenu) {
1845 x = -1;
1846 y = -1;
1847 } else {
1848 menu = smenu;
1851 if (menu->flags.editing) {
1852 return;
1854 entry_no = getEntryAt(menu, x, y);
1855 if (entry_no>=0) {
1856 entry = menu->entries[entry_no];
1858 if (!close_on_exit && (bev->state & ControlMask) && smenu
1859 && entry->flags.editable) {
1860 editEntry(smenu, entry);
1861 return;
1862 } else if (bev->state & ControlMask) {
1863 return;
1866 if (entry->flags.enabled && entry->cascade>=0 && menu->cascades) {
1867 WMenu *submenu = menu->cascades[entry->cascade];
1868 /* map cascade */
1869 if (submenu->flags.mapped && !submenu->flags.buttoned &&
1870 menu->selected_entry!=entry_no) {
1871 wMenuUnmap(submenu);
1873 if (!submenu->flags.mapped && !delayed_select) {
1874 selectEntry(menu, entry_no);
1875 } else if (!submenu->flags.buttoned) {
1876 selectEntry(menu, -1);
1879 } else if (!delayed_select) {
1880 selectEntry(menu, entry_no);
1883 if (!wPreferences.wrap_menus && !wPreferences.scrollable_menus) {
1884 if (!menu->timer)
1885 dragScrollMenuCallback(menu);
1889 prevx = bev->x_root;
1890 prevy = bev->y_root;
1891 while (!done) {
1892 int x, y;
1894 XAllowEvents(dpy, SyncPointer, CurrentTime);
1896 WMMaskEvent(dpy, ExposureMask|ButtonMotionMask|ButtonReleaseMask
1897 |ButtonPressMask, &ev);
1898 switch (ev.type) {
1899 case MotionNotify:
1900 smenu = findMenu(scr, &x, &y);
1902 if (smenu == NULL) {
1903 /* moved mouse out of menu */
1905 if (!delayed_select && d_data.magic) {
1906 WMDeleteTimerHandler(d_data.magic);
1907 d_data.magic = NULL;
1909 if (menu==NULL
1910 || (menu->selected_entry>=0
1911 && menu->entries[menu->selected_entry]->cascade>=0)) {
1912 prevx = ev.xmotion.x_root;
1913 prevy = ev.xmotion.y_root;
1915 break;
1917 selectEntry(menu, -1);
1918 menu = smenu;
1919 prevx = ev.xmotion.x_root;
1920 prevy = ev.xmotion.y_root;
1921 break;
1922 } else if (menu && menu!=smenu
1923 && (menu->selected_entry<0
1924 || menu->entries[menu->selected_entry]->cascade<0)) {
1925 selectEntry(menu, -1);
1927 if (!delayed_select && d_data.magic) {
1928 WMDeleteTimerHandler(d_data.magic);
1929 d_data.magic = NULL;
1931 } else {
1933 /* hysteresis for item selection */
1935 /* check if the motion was to the side, indicating that
1936 * the user may want to cross to a submenu */
1937 if (!delayed_select && menu) {
1938 int dx;
1939 Bool moved_to_submenu;/* moved to direction of submenu */
1941 dx = abs(prevx - ev.xmotion.x_root);
1943 moved_to_submenu = False;
1944 if (dx > 0 /* if moved enough to the side */
1945 /* maybe a open submenu */
1946 && menu->selected_entry>=0
1947 /* moving to the right direction */
1948 && (wPreferences.align_menus
1949 || ev.xmotion.y_root >= prevy)) {
1950 int index;
1952 index = menu->entries[menu->selected_entry]->cascade;
1953 if (index>=0) {
1954 if (menu->cascades[index]->frame_x>menu->frame_x) {
1955 if (prevx < ev.xmotion.x_root)
1956 moved_to_submenu = True;
1957 } else {
1958 if (prevx > ev.xmotion.x_root)
1959 moved_to_submenu = True;
1965 if (menu != smenu) {
1966 if (d_data.magic) {
1967 WMDeleteTimerHandler(d_data.magic);
1969 d_data.magic = NULL;
1970 } else if (moved_to_submenu) {
1971 /* while we are moving, postpone the selection */
1972 if (d_data.magic) {
1973 WMDeleteTimerHandler(d_data.magic);
1975 d_data.delayed_select = NULL;
1976 d_data.menu = menu;
1977 d_data.magic = WMAddTimerHandler(MENU_SELECT_DELAY,
1978 delaySelection,
1979 &d_data);
1980 prevx = ev.xmotion.x_root;
1981 prevy = ev.xmotion.y_root;
1982 break;
1983 } else {
1984 if (d_data.magic)
1985 WMDeleteTimerHandler(d_data.magic);
1986 d_data.magic = NULL;
1990 prevx = ev.xmotion.x_root;
1991 prevy = ev.xmotion.y_root;
1992 if (menu!=smenu) {
1993 /* pointer crossed menus */
1994 if (menu && menu->timer) {
1995 WMDeleteTimerHandler(menu->timer);
1996 menu->timer = NULL;
1998 if (smenu)
1999 dragScrollMenuCallback(smenu);
2001 menu = smenu;
2002 if (!menu->timer)
2003 dragScrollMenuCallback(menu);
2005 if (!delayed_select) {
2006 entry_no = getEntryAt(menu, x, y);
2007 if (entry_no>=0) {
2008 entry = menu->entries[entry_no];
2009 if (entry->flags.enabled && entry->cascade>=0 &&
2010 menu->cascades) {
2011 WMenu *submenu = menu->cascades[entry->cascade];
2012 if (submenu->flags.mapped && !submenu->flags.buttoned
2013 && menu->selected_entry!=entry_no) {
2014 wMenuUnmap(submenu);
2018 selectEntry(menu, entry_no);
2020 break;
2022 case ButtonPress:
2023 break;
2025 case ButtonRelease:
2026 if (ev.xbutton.button == event->xbutton.button)
2027 done=1;
2028 break;
2030 case Expose:
2031 WMHandleEvent(&ev);
2032 break;
2036 if (menu && menu->timer) {
2037 WMDeleteTimerHandler(menu->timer);
2038 menu->timer = NULL;
2040 if (d_data.magic!=NULL)
2041 WMDeleteTimerHandler(d_data.magic);
2043 if (menu && menu->selected_entry>=0) {
2044 entry = menu->entries[menu->selected_entry];
2045 if (entry->callback!=NULL && entry->flags.enabled
2046 && entry->cascade < 0) {
2047 /* blink and erase menu selection */
2048 #if (MENU_BLINK_DELAY > 0)
2049 int sel = menu->selected_entry;
2050 int i;
2052 for (i=0; i<MENU_BLINK_COUNT; i++) {
2053 paintEntry(menu, sel, False);
2054 XSync(dpy, 0);
2055 wusleep(MENU_BLINK_DELAY);
2056 paintEntry(menu, sel, True);
2057 XSync(dpy, 0);
2058 wusleep(MENU_BLINK_DELAY);
2060 #endif
2061 /* unmap the menu, it's parents and call the callback */
2062 if (!menu->flags.buttoned &&
2063 (!menu->flags.app_menu||menu->parent!=NULL)) {
2064 closeCascade(menu);
2065 } else {
2066 selectEntry(menu, -1);
2068 (*entry->callback)(menu, entry);
2070 /* If the user double clicks an entry, the entry will
2071 * be executed twice, which is not good for things like
2072 * the root menu. So, ignore any clicks that were generated
2073 * while the entry was being executed */
2074 while (XCheckTypedWindowEvent(dpy, menu->menu->window,
2075 ButtonPress, &ev));
2076 } else if (entry->callback!=NULL && entry->cascade<0) {
2077 selectEntry(menu, -1);
2078 } else {
2079 if (entry->cascade>=0 && menu->cascades
2080 && menu->cascades[entry->cascade]->flags.brother) {
2081 selectEntry(menu, -1);
2086 if (((WMenu*)desc->parent)->flags.brother || close_on_exit
2087 || !smenu)
2088 closeCascade(desc->parent);
2090 /* close the cascade windows that should not remain opened */
2091 closeBrotherCascadesOf(desc->parent);
2093 if (!wPreferences.wrap_menus)
2094 wMenuMove(parentMenu(desc->parent), old_frame_x, old_frame_y, True);
2098 void
2099 wMenuMove(WMenu *menu, int x, int y, int submenus)
2101 WMenu *submenu;
2102 int i;
2104 if (!menu) return;
2106 menu->frame_x = x;
2107 menu->frame_y = y;
2108 XMoveWindow(dpy, menu->frame->core->window, x, y);
2110 if (submenus>0 && menu->selected_entry>=0) {
2111 i = menu->entries[menu->selected_entry]->cascade;
2113 if (i>=0 && menu->cascades) {
2114 submenu = menu->cascades[i];
2115 if (submenu->flags.mapped && !submenu->flags.buttoned)
2116 if (wPreferences.align_menus) {
2117 wMenuMove(submenu, x + MENUW(menu), y, submenus);
2118 } else {
2119 wMenuMove(submenu, x+ MENUW(menu),
2120 y + submenu->entry_height*menu->selected_entry,
2121 submenus);
2125 if (submenus<0 && menu->parent!=NULL && menu->parent->flags.mapped &&
2126 !menu->parent->flags.buttoned) {
2127 if (wPreferences.align_menus) {
2128 wMenuMove(menu->parent, x - MENUW(menu->parent), y, submenus);
2129 } else {
2130 wMenuMove(menu->parent, x - MENUW(menu->parent), menu->frame_y
2131 - menu->parent->entry_height*menu->parent->selected_entry,
2132 submenus);
2138 static void
2139 changeMenuLevels(WMenu *menu, int lower)
2141 int i;
2143 if (!lower) {
2144 ChangeStackingLevel(menu->frame->core, (!menu->parent ? WMMainMenuLevel
2145 : WMSubmenuLevel));
2146 wRaiseFrame(menu->frame->core);
2147 menu->flags.lowered = 0;
2148 } else {
2149 ChangeStackingLevel(menu->frame->core, WMNormalLevel);
2150 wLowerFrame(menu->frame->core);
2151 menu->flags.lowered = 1;
2153 for (i=0; i<menu->cascade_no; i++) {
2154 if (menu->cascades[i]
2155 && !menu->cascades[i]->flags.buttoned
2156 && menu->cascades[i]->flags.lowered!=lower) {
2157 changeMenuLevels(menu->cascades[i], lower);
2164 static void
2165 menuTitleDoubleClick(WCoreWindow *sender, void *data, XEvent *event)
2167 WMenu *menu = data;
2168 int lower;
2170 if (event->xbutton.state & MOD_MASK) {
2171 if (menu->flags.lowered) {
2172 lower = 0;
2173 } else {
2174 lower = 1;
2176 changeMenuLevels(menu, lower);
2181 static void
2182 menuTitleMouseDown(WCoreWindow *sender, void *data, XEvent *event)
2184 WMenu *menu = data;
2185 WMenu *tmp;
2186 XEvent ev;
2187 int x=menu->frame_x, y=menu->frame_y;
2188 int dx=event->xbutton.x_root, dy=event->xbutton.y_root;
2189 int i, lower;
2190 Bool started;
2192 #ifdef DEBUG
2193 printf("Moving menu\n");
2194 #endif
2196 /* can't touch the menu copy */
2197 if (menu->flags.brother)
2198 return;
2200 if (event->xbutton.button != Button1 && event->xbutton.button != Button2)
2201 return;
2203 if (event->xbutton.state & MOD_MASK) {
2204 wLowerFrame(menu->frame->core);
2205 lower = 1;
2206 } else {
2207 wRaiseFrame(menu->frame->core);
2208 lower = 0;
2210 tmp = menu;
2212 /* lower/raise all submenus */
2213 while (1) {
2214 if (tmp->selected_entry>=0 && tmp->cascades
2215 && tmp->entries[tmp->selected_entry]->cascade>=0) {
2216 tmp = tmp->cascades[tmp->entries[tmp->selected_entry]->cascade];
2217 if (!tmp || !tmp->flags.mapped)
2218 break;
2219 if (lower)
2220 wLowerFrame(tmp->frame->core);
2221 else
2222 wRaiseFrame(tmp->frame->core);
2223 } else {
2224 break;
2228 /* tear off the menu if it's a root menu or a cascade
2229 application menu */
2230 if (!menu->flags.buttoned && !menu->flags.brother
2231 && (!menu->flags.app_menu||menu->parent!=NULL)) {
2232 menu->flags.buttoned=1;
2233 wFrameWindowShowButton(menu->frame, WFF_RIGHT_BUTTON);
2234 if (menu->parent) {
2235 /* turn off selected menu entry in parent menu */
2236 selectEntry(menu->parent, -1);
2238 /* make parent map the copy in place of the original */
2239 for (i=0; i<menu->parent->cascade_no; i++) {
2240 if (menu->parent->cascades[i] == menu) {
2241 menu->parent->cascades[i] = menu->brother;
2242 break;
2248 started = False;
2249 while(1) {
2250 WMMaskEvent(dpy, ButtonMotionMask|ButtonReleaseMask|ButtonPressMask
2251 |ExposureMask, &ev);
2252 switch (ev.type) {
2253 case MotionNotify:
2254 if (started) {
2255 x += ev.xmotion.x_root - dx;
2256 y += ev.xmotion.y_root - dy;
2257 dx = ev.xmotion.x_root;
2258 dy = ev.xmotion.y_root;
2259 wMenuMove(menu, x, y, True);
2260 } else {
2261 if (abs(ev.xmotion.x_root - dx) > MOVE_THRESHOLD
2262 || abs(ev.xmotion.y_root - dy) > MOVE_THRESHOLD) {
2263 started = True;
2264 XGrabPointer(dpy, menu->frame->titlebar->window, False,
2265 ButtonMotionMask|ButtonReleaseMask
2266 |ButtonPressMask,
2267 GrabModeAsync, GrabModeAsync, None,
2268 wCursor[WCUR_MOVE], CurrentTime);
2271 break;
2273 case ButtonPress:
2274 break;
2276 case ButtonRelease:
2277 if (ev.xbutton.button != event->xbutton.button)
2278 break;
2279 #ifdef DEBUG
2280 printf("End menu move\n");
2281 #endif
2282 XUngrabPointer(dpy, CurrentTime);
2283 return;
2285 default:
2286 WMHandleEvent(&ev);
2287 break;
2293 *----------------------------------------------------------------------
2294 * menuCloseClick--
2295 * Handles mouse click on the close button of menus. The menu is
2296 * closed when the button is clicked.
2298 * Side effects:
2299 * The closed menu is reinserted at it's parent menus
2300 * cascade list.
2301 *----------------------------------------------------------------------
2303 static void
2304 menuCloseClick(WCoreWindow *sender, void *data, XEvent *event)
2306 WMenu *menu = (WMenu*)data;
2307 WMenu *parent = menu->parent;
2308 int i;
2310 if (parent) {
2311 for (i=0; i<parent->cascade_no; i++) {
2312 /* find the entry that points to the copy */
2313 if (parent->cascades[i] == menu->brother) {
2314 /* make it point to the original */
2315 parent->cascades[i] = menu;
2316 menu->parent = parent;
2317 break;
2321 wMenuUnmap(menu);
2325 void
2326 wMenuSaveState(WScreen *scr)
2328 proplist_t menus, key, value;
2329 int save_menus = 0;
2330 char buffer[256];
2332 menus = PLMakeDictionaryFromEntries(NULL, NULL, NULL);
2334 #ifndef LITE
2335 if (scr->root_menu && scr->root_menu->flags.buttoned) {
2336 sprintf(buffer, "%i,%i", scr->root_menu->frame_x,
2337 scr->root_menu->frame_y);
2338 key = PLMakeString("RootMenu");
2339 value = PLMakeString(buffer);
2340 PLInsertDictionaryEntry(menus, key, value);
2341 PLRelease(key);
2342 PLRelease(value);
2343 save_menus = 1;
2346 if (scr->switch_menu && scr->switch_menu->flags.buttoned) {
2347 sprintf(buffer, "%i,%i", scr->switch_menu->frame_x,
2348 scr->switch_menu->frame_y);
2349 key = PLMakeString("SwitchMenu");
2350 value = PLMakeString(buffer);
2351 PLInsertDictionaryEntry(menus, key, value);
2352 PLRelease(key);
2353 PLRelease(value);
2354 save_menus = 1;
2356 #endif /* !LITE */
2357 if (scr->workspace_menu && scr->workspace_menu->flags.buttoned) {
2358 sprintf(buffer, "%i,%i", scr->workspace_menu->frame_x,
2359 scr->workspace_menu->frame_y);
2360 key = PLMakeString("WorkspaceMenu");
2361 value = PLMakeString(buffer);
2362 PLInsertDictionaryEntry(menus, key, value);
2363 PLRelease(key);
2364 PLRelease(value);
2365 save_menus = 1;
2368 if (save_menus) {
2369 key = PLMakeString("Menus");
2370 PLInsertDictionaryEntry(scr->session_state, key, menus);
2371 PLRelease(key);
2373 PLRelease(menus);
2377 #define COMPLAIN(key) wwarning(_("bad value in menus state info:%s"), key)
2380 static int
2381 restoreMenu(WScreen *scr, proplist_t menu, int which)
2383 int i, x, y;
2384 WMenu *pmenu = NULL;
2386 if (!menu)
2387 return False;
2389 if (!PLIsString(menu)) {
2390 COMPLAIN("Position");
2391 return False;
2394 if (sscanf(PLGetString(menu), "%i,%i", &x, &y)!=2)
2395 COMPLAIN("Position");
2397 #ifndef LITE
2398 if (which & WSS_ROOTMENU) {
2399 OpenRootMenu(scr, x, y, False);
2400 pmenu = scr->root_menu;
2401 } else if (which & WSS_SWITCHMENU) {
2402 OpenSwitchMenu(scr, x, y, False);
2403 pmenu = scr->switch_menu;
2404 } else
2405 #endif /* !LITE */
2406 if (which & WSS_WSMENU) {
2407 OpenWorkspaceMenu(scr, x, y);
2408 pmenu = scr->workspace_menu;
2409 if (pmenu->parent) {
2410 /* make parent map the copy in place of the original */
2411 for (i=0; i<pmenu->parent->cascade_no; i++) {
2412 if (pmenu->parent->cascades[i] == pmenu) {
2413 pmenu->parent->cascades[i] = pmenu->brother;
2414 break;
2420 if (pmenu) {
2421 int width = MENUW(pmenu);
2422 int height = MENUH(pmenu);
2424 x = (x < -width) ? 0 : x;
2425 x = (x > scr->scr_width) ? scr->scr_width - width : x;
2426 y = (y < 0) ? 0 : y;
2427 y = (y > scr->scr_height) ? scr->scr_height - height : y;
2428 wMenuMove(pmenu, x, y, True);
2429 pmenu->flags.buttoned = 1;
2430 wFrameWindowShowButton(pmenu->frame, WFF_RIGHT_BUTTON);
2431 return True;
2433 return False;
2437 void
2438 wMenuRestoreState(WScreen *scr)
2440 proplist_t menus, menu, key, rkey, skey, wkey;
2442 key = PLMakeString("Menus");
2443 menus = PLGetDictionaryEntry(scr->session_state, key);
2444 PLRelease(key);
2446 if (!menus)
2447 return;
2449 /* restore menus */
2451 rkey = PLMakeString("RootMenu");
2452 skey = PLMakeString("SwitchMenu");
2453 wkey = PLMakeString("WorkspaceMenu");
2454 menu = PLGetDictionaryEntry(menus, rkey);
2455 restoreMenu(scr, menu, WSS_ROOTMENU);
2456 menu = PLGetDictionaryEntry(menus, skey);
2457 restoreMenu(scr, menu, WSS_SWITCHMENU);
2458 menu = PLGetDictionaryEntry(menus, wkey);
2459 restoreMenu(scr, menu, WSS_WSMENU);
2461 PLRelease(rkey);
2462 PLRelease(skey);
2463 PLRelease(wkey);
2467 void
2468 OpenWorkspaceMenu(WScreen *scr, int x, int y)
2470 WMenu *menu, *parent;
2471 WMenuEntry *entry;
2473 #ifndef LITE
2474 if (!scr->root_menu) {
2475 OpenRootMenu(scr, scr->scr_width*2, 0, False);
2476 wMenuUnmap(scr->root_menu);
2478 #endif
2479 menu = scr->workspace_menu;
2480 if (menu) {
2481 if (menu->flags.mapped) {
2482 if (!menu->flags.buttoned) {
2483 wMenuUnmap(menu);
2484 parent = menu->parent;
2485 if (parent && parent->selected_entry >= 0) {
2486 entry = parent->entries[parent->selected_entry];
2487 if (parent->cascades[entry->cascade] == menu) {
2488 selectEntry(parent, -1);
2489 wMenuMapAt(menu, x, y, False);
2492 } else {
2493 wRaiseFrame(menu->frame->core);
2494 wMenuMapCopyAt(menu, x, y);
2497 else {
2498 wMenuMapAt(menu, x, y, False);