MenuStyle option
[wmaker-crm.git] / src / menu.c
blobd10f445b7bc8bce28941ed1f5bfe2b621abe0de3
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 Pixmap
441 renderTexture(WMenu *menu)
443 RImage *img;
444 Pixmap pix;
445 int i;
446 RColor light;
447 RColor dark;
448 RColor mid;
449 WScreen *scr = menu->menu->screen_ptr;
450 WTexture *texture = scr->menu_item_texture;
452 if (wPreferences.menu_style == MS_NORMAL) {
453 img = wTextureRenderImage(texture, menu->menu->width,
454 menu->entry_height, WREL_MENUENTRY);
455 } else {
456 img = wTextureRenderImage(texture, menu->menu->width,
457 menu->menu->height+1, WREL_MENUENTRY);
459 if (!img) {
460 wwarning(_("could not render texture: %s"),
461 RMessageForError(RErrorCode));
463 return None;
466 if (wPreferences.menu_style == MS_SINGLE_TEXTURE) {
467 light.alpha = 0;
468 light.red = light.green = light.blue = 80;
470 dark.alpha = 255;
471 dark.red = dark.green = dark.blue = 0;
473 mid.alpha = 0;
474 mid.red = mid.green = mid.blue = 40;
476 for (i = 1; i < menu->entry_no; i++) {
477 ROperateLine(img, RSubtractOperation, 0, i*menu->entry_height-2,
478 menu->menu->width-1, i*menu->entry_height-2, &mid);
480 RDrawLine(img, 0, i*menu->entry_height-1,
481 menu->menu->width-1, i*menu->entry_height-1, &dark);
483 ROperateLine(img, RAddOperation, 0, i*menu->entry_height,
484 menu->menu->width-1, i*menu->entry_height,
485 &light);
488 if (!RConvertImage(scr->rcontext, img, &pix)) {
489 wwarning(_("error rendering image:%s"), RMessageForError(RErrorCode));
491 RDestroyImage(img);
493 return pix;
497 static void
498 updateTexture(WMenu *menu)
500 WScreen *scr = menu->menu->screen_ptr;
502 /* setup background texture */
503 if (scr->menu_item_texture->any.type != WTEX_SOLID) {
504 if (!menu->flags.brother) {
505 FREE_PIXMAP(menu->menu_texture_data);
507 menu->menu_texture_data = renderTexture(menu);
509 XSetWindowBackgroundPixmap(dpy, menu->menu->window,
510 menu->menu_texture_data);
511 XClearWindow(dpy, menu->menu->window);
513 XSetWindowBackgroundPixmap(dpy, menu->brother->menu->window,
514 menu->menu_texture_data);
515 XClearWindow(dpy, menu->brother->menu->window);
517 } else {
518 XSetWindowBackground(dpy, menu->menu->window,
519 scr->menu_item_texture->any.color.pixel);
520 XClearWindow(dpy, menu->menu->window);
525 void
526 wMenuRealize(WMenu *menu)
528 int i;
529 int width, rwidth, mrwidth, mwidth;
530 int theight, twidth, eheight;
531 WScreen *scr = menu->frame->screen_ptr;
532 static int brother_done=0;
533 int flags;
535 if (!brother_done) {
536 brother_done = 1;
537 wMenuRealize(menu->brother);
538 brother_done = 0;
541 flags = WFF_SINGLE_STATE;
542 if (menu->flags.titled)
543 flags |= WFF_TITLEBAR|WFF_RIGHT_BUTTON;
545 wFrameWindowUpdateBorders(menu->frame, flags);
547 if (menu->flags.titled) {
548 twidth = wTextWidth(scr->menu_title_font->font, menu->frame->title,
549 strlen(menu->frame->title));
550 theight = menu->frame->top_width;
551 twidth += theight + (wPreferences.new_style ? 16 : 8);
552 } else {
553 twidth = 0;
554 theight = 0;
556 eheight = scr->menu_entry_font->height + 6;
557 menu->entry_height = eheight;
558 mrwidth = 0;
559 mwidth = 0;
560 for (i=0; i<menu->entry_no; i++) {
561 char *text;
563 /* search widest text */
564 text = menu->entries[i]->text;
565 width = wTextWidth(scr->menu_entry_font->font, text, strlen(text))+10;
567 if (menu->entries[i]->flags.indicator) {
568 width += MENU_INDICATOR_SPACE;
571 if (width > mwidth)
572 mwidth = width;
574 /* search widest text on right */
575 text = menu->entries[i]->rtext;
576 if (text)
577 rwidth = wTextWidth(scr->menu_entry_font->font, text,
578 strlen(text)) + 5;
579 else if (menu->entries[i]->cascade>=0)
580 rwidth = 16;
581 else
582 rwidth = 4;
584 if (rwidth > mrwidth)
585 mrwidth = rwidth;
587 mwidth += mrwidth;
589 if (mwidth < twidth)
590 mwidth = twidth;
593 wCoreConfigure(menu->menu, 0, theight, mwidth, menu->entry_no*eheight -1);
595 wFrameWindowResize(menu->frame, mwidth, menu->entry_no*eheight-1
596 + menu->frame->top_width + menu->frame->bottom_width);
599 updateTexture(menu);
601 menu->flags.realized = 1;
603 if (menu->flags.mapped)
604 wMenuPaint(menu);
605 if (menu->brother->flags.mapped)
606 wMenuPaint(menu->brother);
610 void
611 wMenuDestroy(WMenu *menu, int recurse)
613 int i;
615 WMRemoveNotificationObserver(menu);
617 /* remove any pending timers */
618 if (menu->timer)
619 WMDeleteTimerHandler(menu->timer);
620 menu->timer = NULL;
622 /* call destroy handler */
623 if (menu->on_destroy)
624 (*menu->on_destroy)(menu);
626 /* Destroy items if this menu own them. If this is the "brother" menu,
627 * leave them alone as it is shared by them.
629 if (!menu->flags.brother) {
630 for (i=0; i<menu->entry_no; i++) {
632 free(menu->entries[i]->text);
634 if (menu->entries[i]->rtext)
635 free(menu->entries[i]->rtext);
636 #ifdef USER_MENU
638 if (menu->entries[i]->instances){
639 PLRelease(menu->entries[i]->instances);
641 #endif /* USER_MENU */
643 if (menu->entries[i]->free_cdata && menu->entries[i]->clientdata) {
644 (*menu->entries[i]->free_cdata)(menu->entries[i]->clientdata);
646 free(menu->entries[i]);
649 if (recurse) {
650 for (i=0; i<menu->cascade_no; i++) {
651 if (menu->cascades[i]) {
652 if (menu->cascades[i]->flags.brother)
653 wMenuDestroy(menu->cascades[i]->brother, recurse);
654 else
655 wMenuDestroy(menu->cascades[i], recurse);
660 if (menu->entries)
661 free(menu->entries);
665 FREE_PIXMAP(menu->menu_texture_data);
667 if (menu->cascades)
668 free(menu->cascades);
670 wCoreDestroy(menu->menu);
671 wFrameWindowDestroy(menu->frame);
673 /* destroy copy of this menu */
674 if (!menu->flags.brother && menu->brother)
675 wMenuDestroy(menu->brother, False);
677 free(menu);
681 #define F_NORMAL 0
682 #define F_TOP 1
683 #define F_BOTTOM 2
684 #define F_NONE 3
686 static void
687 drawFrame(WScreen *scr, Window win, int y, int w, int h, int type)
689 XSegment segs[2];
690 int i;
692 i = 0;
693 segs[i].x1 = segs[i].x2 = w-1;
694 segs[i].y1 = y;
695 segs[i].y2 = y + h - 1;
696 i++;
697 if (type != F_TOP && type != F_NONE) {
698 segs[i].x1 = 1;
699 segs[i].y1 = segs[i].y2 = y + h-2;
700 segs[i].x2 = w-1;
701 i++;
703 XDrawSegments(dpy, win, scr->menu_item_auxtexture->dim_gc, segs, i);
705 i = 0;
706 segs[i].x1 = 0;
707 segs[i].y1 = y;
708 segs[i].x2 = 0;
709 segs[i].y2 = y + h - 1;
710 i++;
711 if (type != F_BOTTOM && type != F_NONE) {
712 segs[i].x1 = 0;
713 segs[i].y1 = y;
714 segs[i].x2 = w-1;
715 segs[i].y2 = y;
716 i++;
718 XDrawSegments(dpy, win, scr->menu_item_auxtexture->light_gc, segs, i);
720 if (type != F_TOP && type != F_NONE)
721 XDrawLine(dpy, win, scr->menu_item_auxtexture->dark_gc, 0, y+h-1,
722 w-1, y+h-1);
726 static void
727 paintEntry(WMenu *menu, int index, int selected)
729 int x, y, w, h, tw;
730 int type;
731 GC light, dim, dark, textGC;
732 WScreen *scr=menu->frame->screen_ptr;
733 Window win = menu->menu->window;
734 WMenuEntry *entry=menu->entries[index];
736 if (!menu->flags.realized) return;
737 h = menu->entry_height;
738 w = menu->menu->width;
739 y = index * h;
741 light = scr->menu_item_auxtexture->light_gc;
742 dim = scr->menu_item_auxtexture->dim_gc;
743 dark = scr->menu_item_auxtexture->dark_gc;
745 if (wPreferences.menu_style == MS_FLAT && menu->entry_no > 1) {
746 if (index == 0)
747 type = F_TOP;
748 else if (index == menu->entry_no - 1)
749 type = F_BOTTOM;
750 else
751 type = F_NONE;
752 } else {
753 type = F_NORMAL;
756 /* paint background */
757 if (selected) {
758 XSetForeground(dpy, scr->select_menu_gc, scr->select_pixel);
759 XFillRectangle(dpy, win, scr->select_menu_gc, 1, y+1, w-2, h-3);
760 if (scr->menu_item_texture->any.type == WTEX_SOLID)
761 drawFrame(scr, win, y, w, h, type);
762 } else {
763 if (scr->menu_item_texture->any.type == WTEX_SOLID) {
764 XClearArea(dpy, win, 0, y + 1, w - 1, h - 3, False);
765 /* draw the frame */
766 drawFrame(scr, win, y, w, h, type);
767 } else {
768 XClearArea(dpy, win, 0, y, w, h, False);
772 if (selected) {
773 textGC = scr->select_menu_gc;
774 if (entry->flags.enabled)
775 XSetForeground(dpy, textGC, scr->select_text_pixel);
776 else
777 XSetForeground(dpy, textGC, scr->dtext_pixel);
778 } else if (!entry->flags.enabled) {
779 textGC = scr->disabled_menu_entry_gc;
780 } else {
781 textGC = scr->menu_entry_gc;
783 /* draw text */
784 x = 5;
785 if (entry->flags.indicator)
786 x += MENU_INDICATOR_SPACE + 2;
788 wDrawString(win, scr->menu_entry_font,
789 textGC, x, 3+y+scr->menu_entry_font->y, entry->text,
790 strlen(entry->text));
792 if (entry->cascade>=0) {
793 /* draw the cascade indicator */
794 XDrawLine(dpy,win,dim, w-11, y+6, w-6, y+h/2-1);
795 XDrawLine(dpy,win,light, w-11, y+h-8, w-6, y+h/2-1);
796 XDrawLine(dpy,win,dark, w-12, y+6, w-12, y+h-8);
799 /* draw indicator */
800 if (entry->flags.indicator && entry->flags.indicator_on) {
801 int iw, ih;
802 WPixmap *indicator;
805 switch (entry->flags.indicator_type) {
806 case MI_CHECK:
807 indicator = scr->menu_check_indicator;
808 break;
809 case MI_MINIWINDOW:
810 indicator = scr->menu_mini_indicator;
811 break;
812 case MI_HIDDEN:
813 indicator = scr->menu_hide_indicator;
814 break;
815 case MI_SHADED:
816 indicator = scr->menu_shade_indicator;
817 break;
818 case MI_DIAMOND:
819 default:
820 indicator = scr->menu_radio_indicator;
821 break;
824 iw = indicator->width;
825 ih = indicator->height;
826 XSetClipMask(dpy, scr->copy_gc, indicator->mask);
827 XSetClipOrigin(dpy, scr->copy_gc, 5, y+(h-ih)/2);
828 if (selected)
829 XSetForeground(dpy, scr->copy_gc, scr->black_pixel);
830 else
831 XSetForeground(dpy, scr->copy_gc, scr->mtext_pixel);
832 XFillRectangle(dpy, win, scr->copy_gc, 5, y+(h-ih)/2, iw, ih);
834 XCopyArea(dpy, indicator->image, win, scr->copy_gc, 0, 0,
835 iw, ih, 5, y+(h-ih)/2);
837 XSetClipOrigin(dpy, scr->copy_gc, 0, 0);
840 /* draw right text */
842 if (entry->rtext && entry->cascade<0) {
843 tw = wTextWidth(scr->menu_entry_font->font, entry->rtext,
844 strlen(entry->rtext));
846 wDrawString(win, scr->menu_entry_font, textGC, w-6-tw,
847 3+y+scr->menu_entry_font->y, entry->rtext,
848 strlen(entry->rtext));
853 static void
854 move_menus(WMenu *menu, int x, int y)
856 while (menu->parent) {
857 menu = menu->parent;
858 x -= MENUW(menu);
859 if (!wPreferences.align_menus && menu->selected_entry>=0) {
860 y -= menu->selected_entry*menu->entry_height;
863 wMenuMove(menu, x, y, True);
866 static void
867 makeVisible(WMenu *menu)
869 WScreen *scr = menu->frame->screen_ptr;
870 int x1, y1, x2, y2, new_x, new_y, move;
872 if (menu->entry_no<0) return;
874 x1 = menu->frame_x;
875 y1 = menu->frame_y+menu->frame->top_width
876 + menu->selected_entry*menu->entry_height;
877 x2 = x1 + MENUW(menu);
878 y2 = y1 + menu->entry_height;
880 new_x = x1;
881 new_y = y1;
882 move = 0;
884 if (x1 < 0) {
885 new_x = 0;
886 move = 1;
887 } else if (x2 >= scr->scr_width) {
888 new_x = scr->scr_width - MENUW(menu) - 1;
889 move = 1;
892 if (y1 < 0) {
893 new_y = 0;
894 move = 1;
895 } else if (y2 >= scr->scr_height) {
896 new_y = scr->scr_height - menu->entry_height - 1;
897 move = 1;
900 new_y = new_y - menu->frame->top_width
901 - menu->selected_entry*menu->entry_height;
902 move_menus(menu, new_x, new_y);
906 static int
907 check_key(WMenu *menu, XKeyEvent *event)
909 int i, ch, s;
910 char buffer[32];
912 if (XLookupString(event, buffer, 32, NULL, NULL)<1)
913 return -1;
915 ch = toupper(buffer[0]);
917 s = (menu->selected_entry>=0 ? menu->selected_entry+1 : 0);
919 again:
920 for (i=s; i<menu->entry_no; i++) {
921 if (ch==toupper(menu->entries[i]->text[0])) {
922 return i;
925 /* no match. Retry from start, if previous started from a selected entry */
926 if (s!=0) {
927 s = 0;
928 goto again;
930 return -1;
934 static int
935 keyboardMenu(WMenu *menu)
937 XEvent event;
938 KeySym ksym=NoSymbol;
939 int done=0;
940 int index;
941 WMenuEntry *entry;
942 int old_pos_x = menu->frame_x;
943 int old_pos_y = menu->frame_y;
944 int new_x = old_pos_x, new_y = old_pos_y;
945 int scr_width = menu->frame->screen_ptr->scr_width;
946 int scr_height = menu->frame->screen_ptr->scr_height;
948 if (menu->flags.editing)
949 return False;
952 XGrabKeyboard(dpy, menu->frame->core->window, True, GrabModeAsync,
953 GrabModeAsync, CurrentTime);
955 if (menu->frame_y+menu->frame->top_width >= scr_height)
956 new_y = scr_height - menu->frame->top_width;
958 if (menu->frame_x+MENUW(menu) >= scr_width)
959 new_x = scr_width-MENUW(menu)-1;
961 move_menus(menu, new_x, new_y);
963 while (!done && menu->flags.mapped) {
964 XAllowEvents(dpy, AsyncKeyboard, CurrentTime);
965 WMMaskEvent(dpy, ExposureMask|ButtonMotionMask|ButtonPressMask
966 |ButtonReleaseMask|KeyPressMask|KeyReleaseMask
967 |SubstructureNotifyMask, &event);
969 switch (event.type) {
970 case KeyPress:
971 ksym = XLookupKeysym(&event.xkey, 0);
972 switch (ksym) {
973 case XK_Escape:
974 done = 1;
975 break;
977 case XK_Home:
978 case XK_KP_Home:
979 selectEntry(menu, 0);
980 makeVisible(menu);
981 break;
983 case XK_End:
984 case XK_KP_End:
985 selectEntry(menu, menu->entry_no-1);
986 makeVisible(menu);
987 break;
989 case XK_Up:
990 #ifdef ARROWLESS_KBD
991 case XK_k:
992 #endif
993 case XK_KP_Up:
994 if (menu->selected_entry <= 0)
995 selectEntry(menu, menu->entry_no-1);
996 else
997 selectEntry(menu, menu->selected_entry-1);
998 makeVisible(menu);
999 break;
1001 case XK_Down:
1002 #ifdef ARROWLESS_KBD
1003 case XK_j:
1004 #endif
1005 case XK_KP_Down:
1006 if (menu->selected_entry<0)
1007 selectEntry(menu, 0);
1008 else if (menu->selected_entry == menu->entry_no-1)
1009 selectEntry(menu, 0);
1010 else if (menu->selected_entry < menu->entry_no-1)
1011 selectEntry(menu, menu->selected_entry+1);
1012 makeVisible(menu);
1013 break;
1015 case XK_Right:
1016 #ifdef ARROWLESS_KBD
1017 case XK_l:
1018 #endif
1019 case XK_KP_Right:
1020 if (menu->selected_entry>=0) {
1021 WMenuEntry *entry;
1022 entry = menu->entries[menu->selected_entry];
1024 if (entry->cascade >= 0 && menu->cascades
1025 && menu->cascades[entry->cascade]->entry_no > 0) {
1027 XUngrabKeyboard(dpy, CurrentTime);
1029 selectEntry(menu->cascades[entry->cascade], 0);
1030 if (!keyboardMenu(menu->cascades[entry->cascade]))
1031 done = 1;
1033 XGrabKeyboard(dpy, menu->frame->core->window, True,
1034 GrabModeAsync, GrabModeAsync,
1035 CurrentTime);
1038 break;
1040 case XK_Left:
1041 #ifdef ARROWLESS_KBD
1042 case XK_h:
1043 #endif
1044 case XK_KP_Left:
1045 if (menu->parent!=NULL && menu->parent->selected_entry>=0) {
1046 selectEntry(menu, -1);
1047 move_menus(menu, old_pos_x, old_pos_y);
1048 return True;
1050 break;
1052 case XK_Return:
1053 done = 2;
1054 break;
1056 default:
1057 index = check_key(menu, &event.xkey);
1058 if (index>=0) {
1059 selectEntry(menu, index);
1062 break;
1064 default:
1065 if (event.type==ButtonPress)
1066 done = 1;
1068 WMHandleEvent(&event);
1072 XUngrabKeyboard(dpy, CurrentTime);
1074 if (done==2 && menu->selected_entry>=0) {
1075 entry = menu->entries[menu->selected_entry];
1076 } else {
1077 entry = NULL;
1080 if (entry && entry->callback!=NULL && entry->flags.enabled
1081 && entry->cascade < 0) {
1082 #if (MENU_BLINK_COUNT > 0)
1083 int sel = menu->selected_entry;
1084 int i;
1086 for (i=0; i<MENU_BLINK_COUNT; i++) {
1087 paintEntry(menu, sel, False);
1088 XSync(dpy, 0);
1089 wusleep(MENU_BLINK_DELAY);
1090 paintEntry(menu, sel, True);
1091 XSync(dpy, 0);
1092 wusleep(MENU_BLINK_DELAY);
1094 #endif
1095 selectEntry(menu, -1);
1097 if (!menu->flags.buttoned) {
1098 wMenuUnmap(menu);
1099 move_menus(menu, old_pos_x, old_pos_y);
1101 closeCascade(menu);
1103 (*entry->callback)(menu, entry);
1104 } else {
1105 if (!menu->flags.buttoned) {
1106 wMenuUnmap(menu);
1107 move_menus(menu, old_pos_x, old_pos_y);
1109 selectEntry(menu, -1);
1113 /* returns True if returning from a submenu to a parent menu,
1114 * False if exiting from menu */
1115 return False;
1119 void
1120 wMenuMapAt(WMenu *menu, int x, int y, int keyboard)
1122 int scr_width = menu->frame->screen_ptr->scr_width;
1123 int scr_height = menu->frame->screen_ptr->scr_height;
1125 if (!menu->flags.realized) {
1126 menu->flags.realized=1;
1127 wMenuRealize(menu);
1129 if (!menu->flags.mapped) {
1130 if (wPreferences.wrap_menus) {
1131 if (x<0) x = 0;
1132 if (y<0) y = 0;
1133 if (x+MENUW(menu) > scr_width)
1134 x = scr_width - MENUW(menu);
1135 if (y+MENUH(menu) > scr_height)
1136 y = scr_height - MENUH(menu);
1139 XMoveWindow(dpy, menu->frame->core->window, x, y);
1140 menu->frame_x = x;
1141 menu->frame_y = y;
1142 XMapWindow(dpy, menu->frame->core->window);
1143 wRaiseFrame(menu->frame->core);
1144 menu->flags.mapped = 1;
1145 } else {
1146 selectEntry(menu, 0);
1149 if (keyboard)
1150 keyboardMenu(menu);
1154 void
1155 wMenuMap(WMenu *menu)
1157 if (!menu->flags.realized) {
1158 menu->flags.realized=1;
1159 wMenuRealize(menu);
1161 if (menu->flags.app_menu && menu->parent==NULL) {
1162 menu->frame_x = menu->frame->screen_ptr->app_menu_x;
1163 menu->frame_y = menu->frame->screen_ptr->app_menu_y;
1164 XMoveWindow(dpy, menu->frame->core->window, menu->frame_x, menu->frame_y);
1166 XMapWindow(dpy, menu->frame->core->window);
1167 wRaiseFrame(menu->frame->core);
1168 menu->flags.mapped = 1;
1172 void
1173 wMenuUnmap(WMenu *menu)
1175 int i;
1177 XUnmapWindow(dpy, menu->frame->core->window);
1178 if (menu->flags.titled && menu->flags.buttoned) {
1179 wFrameWindowHideButton(menu->frame, WFF_RIGHT_BUTTON);
1181 menu->flags.buttoned = 0;
1182 menu->flags.mapped = 0;
1183 menu->flags.open_to_left = 0;
1185 for (i=0; i<menu->cascade_no; i++) {
1186 if (menu->cascades[i]!=NULL
1187 && menu->cascades[i]->flags.mapped
1188 && !menu->cascades[i]->flags.buttoned) {
1190 wMenuUnmap(menu->cascades[i]);
1193 menu->selected_entry = -1;
1198 void
1199 wMenuPaint(WMenu *menu)
1201 int i;
1203 if (!menu->flags.mapped) {
1204 return;
1207 /* paint entries */
1208 for (i=0; i<menu->entry_no; i++) {
1209 paintEntry(menu, i, i==menu->selected_entry);
1214 void
1215 wMenuSetEnabled(WMenu *menu, int index, int enable)
1217 if (index>=menu->entry_no) return;
1218 menu->entries[index]->flags.enabled=enable;
1219 paintEntry(menu, index, index==menu->selected_entry);
1220 paintEntry(menu->brother, index, index==menu->selected_entry);
1224 /* ====================================================================== */
1227 static void
1228 editEntry(WMenu *menu, WMenuEntry *entry)
1230 WTextInput *text;
1231 XEvent event;
1232 WObjDescriptor *desc;
1233 char *t;
1234 int done = 0;
1235 Window old_focus;
1236 int old_revert;
1238 menu->flags.editing = 1;
1240 text = wTextCreate(menu->menu, 1, menu->entry_height * entry->order,
1241 menu->menu->width - 2, menu->entry_height - 1);
1243 wTextPutText(text, entry->text);
1244 XGetInputFocus(dpy, &old_focus, &old_revert);
1245 XSetInputFocus(dpy, text->core->window, RevertToNone, CurrentTime);
1247 if (XGrabKeyboard(dpy, text->core->window, True, GrabModeAsync,
1248 GrabModeAsync, CurrentTime)!=GrabSuccess) {
1249 wwarning("could not grab keyboard");
1250 wTextDestroy(text);
1252 wSetFocusTo(menu->frame->screen_ptr,
1253 menu->frame->screen_ptr->focused_window);
1254 return;
1258 while (!done && !text->done) {
1259 XSync(dpy, 0);
1260 XAllowEvents(dpy, AsyncKeyboard|AsyncPointer, CurrentTime);
1261 XSync(dpy, 0);
1262 WMNextEvent(dpy, &event);
1264 if (XFindContext(dpy, event.xany.window, wWinContext,
1265 (XPointer *)&desc)==XCNOENT)
1266 desc = NULL;
1268 if ((desc != NULL) && (desc->handle_anything != NULL)) {
1270 (*desc->handle_anything)(desc, &event);
1272 } else {
1273 switch (event.type) {
1274 case ButtonPress:
1275 XAllowEvents(dpy, ReplayPointer, CurrentTime);
1276 done = 1;
1278 default:
1279 WMHandleEvent(&event);
1280 break;
1285 XSetInputFocus(dpy, old_focus, old_revert, CurrentTime);
1287 wSetFocusTo(menu->frame->screen_ptr,
1288 menu->frame->screen_ptr->focused_window);
1291 t = wTextGetText(text);
1292 /* if !t, the user has canceled editing */
1293 if (t) {
1294 if (entry->text)
1295 free(entry->text);
1296 entry->text = wstrdup(t);
1298 menu->flags.realized = 0;
1300 wTextDestroy(text);
1302 XUngrabKeyboard(dpy, CurrentTime);
1304 if (t && menu->on_edit)
1305 (*menu->on_edit)(menu, entry);
1307 menu->flags.editing = 0;
1309 if (!menu->flags.realized)
1310 wMenuRealize(menu);
1314 static void
1315 selectEntry(WMenu *menu, int entry_no)
1317 WMenuEntry *entry;
1318 WMenu *submenu;
1319 int old_entry;
1321 if (menu->entries==NULL)
1322 return;
1324 if (entry_no >= menu->entry_no)
1325 return;
1327 old_entry = menu->selected_entry;
1328 menu->selected_entry = entry_no;
1330 if (old_entry!=entry_no) {
1332 /* unselect previous entry */
1333 if (old_entry>=0) {
1334 paintEntry(menu, old_entry, False);
1335 entry = menu->entries[old_entry];
1337 /* unmap cascade */
1338 if (entry->cascade>=0 && menu->cascades) {
1339 if (!menu->cascades[entry->cascade]->flags.buttoned) {
1340 wMenuUnmap(menu->cascades[entry->cascade]);
1345 if (entry_no<0) {
1346 menu->selected_entry = -1;
1347 return;
1349 entry = menu->entries[entry_no];
1351 if (entry->cascade>=0 && menu->cascades && entry->flags.enabled) {
1352 /* Callback for when the submenu is opened.
1354 submenu = menu->cascades[entry->cascade];
1355 if (submenu && submenu->flags.brother)
1356 submenu = submenu->brother;
1358 if (entry->callback) {
1359 /* Only call the callback if the submenu is not yet mapped.
1361 if (menu->flags.brother) {
1362 if (!submenu || !submenu->flags.mapped)
1363 (*entry->callback)(menu->brother, entry);
1364 } else {
1365 if (!submenu || !submenu->flags.buttoned)
1366 (*entry->callback)(menu, entry);
1370 /* the submenu menu might have changed */
1371 submenu = menu->cascades[entry->cascade];
1373 /* map cascade */
1374 if (!submenu->flags.mapped) {
1375 int x, y;
1377 if (!submenu->flags.realized)
1378 wMenuRealize(submenu);
1379 if (wPreferences.wrap_menus) {
1380 if (menu->flags.open_to_left)
1381 submenu->flags.open_to_left = 1;
1383 if (submenu->flags.open_to_left) {
1384 x = menu->frame_x - MENUW(submenu);
1385 if (x<0) {
1386 x = 0;
1387 submenu->flags.open_to_left = 0;
1389 } else {
1390 x = menu->frame_x + MENUW(menu);
1392 if (x + MENUW(submenu)
1393 >= menu->frame->screen_ptr->scr_width) {
1395 x = menu->frame_x - MENUW(submenu);
1396 submenu->flags.open_to_left = 1;
1399 } else {
1400 x = menu->frame_x + MENUW(menu);
1403 if (wPreferences.align_menus) {
1404 y = menu->frame_y;
1405 } else {
1406 y = menu->frame_y + menu->entry_height*entry_no;
1407 if (menu->flags.titled)
1408 y += menu->frame->top_width;
1409 if (menu->cascades[entry->cascade]->flags.titled)
1410 y -= menu->cascades[entry->cascade]->frame->top_width;
1413 wMenuMapAt(menu->cascades[entry->cascade], x, y, False);
1414 menu->cascades[entry->cascade]->parent = menu;
1415 } else {
1416 return;
1419 paintEntry(menu, entry_no, True);
1424 static WMenu*
1425 findMenu(WScreen *scr, int *x_ret, int *y_ret)
1427 WMenu *menu;
1428 WObjDescriptor *desc;
1429 Window root_ret, win, junk_win;
1430 int x, y, wx, wy;
1431 unsigned int mask;
1433 XQueryPointer(dpy, scr->root_win, &root_ret, &win, &x, &y, &wx, &wy,
1434 &mask);
1436 if (win==None) return NULL;
1438 if (XFindContext(dpy, win, wWinContext, (XPointer *)&desc)==XCNOENT)
1439 return NULL;
1441 if (desc->parent_type == WCLASS_MENU) {
1442 menu = (WMenu*)desc->parent;
1443 XTranslateCoordinates(dpy, root_ret, menu->menu->window, wx, wy,
1444 x_ret, y_ret, &junk_win);
1445 return menu;
1447 return NULL;
1453 static void
1454 closeCascade(WMenu *menu)
1456 WMenu *parent=menu->parent;
1458 if (menu->flags.brother
1459 || (!menu->flags.buttoned
1460 && (!menu->flags.app_menu||menu->parent!=NULL))) {
1462 selectEntry(menu, -1);
1463 XSync(dpy, 0);
1464 #if (MENU_BLINK_DELAY > 2)
1465 wusleep(MENU_BLINK_DELAY/2);
1466 #endif
1467 wMenuUnmap(menu);
1468 while (parent!=NULL
1469 && (parent->parent!=NULL || !parent->flags.app_menu
1470 || parent->flags.brother)
1471 && !parent->flags.buttoned) {
1472 selectEntry(parent, -1);
1473 wMenuUnmap(parent);
1474 parent = parent->parent;
1476 if (parent)
1477 selectEntry(parent, -1);
1482 static void
1483 closeBrotherCascadesOf(WMenu *menu)
1485 WMenu *tmp;
1486 int i;
1488 for (i=0; i<menu->cascade_no; i++) {
1489 if (menu->cascades[i]->flags.brother) {
1490 tmp = menu->cascades[i];
1491 } else {
1492 tmp = menu->cascades[i]->brother;
1494 if (tmp->flags.mapped) {
1495 selectEntry(tmp->parent, -1);
1496 closeBrotherCascadesOf(tmp);
1497 break;
1503 #define getEntryAt(menu, x, y) ((y)<0 ? -1 : (y)/(menu->entry_height))
1506 static WMenu*
1507 parentMenu(WMenu *menu)
1509 WMenu *parent;
1510 WMenuEntry *entry;
1512 if (menu->flags.buttoned)
1513 return menu;
1515 while (menu->parent && menu->parent->flags.mapped) {
1516 parent = menu->parent;
1517 if (parent->selected_entry < 0)
1518 break;
1519 entry = parent->entries[parent->selected_entry];
1520 if (!entry->flags.enabled || entry->cascade<0 || !parent->cascades ||
1521 parent->cascades[entry->cascade] != menu)
1522 break;
1523 menu = parent;
1524 if (menu->flags.buttoned)
1525 break;
1528 return menu;
1534 * Will raise the passed menu, if submenu = 0
1535 * If submenu > 0 will also raise all mapped submenus
1536 * until the first buttoned one
1537 * If submenu < 0 will also raise all mapped parent menus
1538 * until the first buttoned one
1541 static void
1542 raiseMenus(WMenu *menu, int submenus)
1544 WMenu *submenu;
1545 int i;
1547 if(!menu) return;
1549 wRaiseFrame(menu->frame->core);
1551 if (submenus>0 && menu->selected_entry>=0) {
1552 i = menu->entries[menu->selected_entry]->cascade;
1553 if (i>=0 && menu->cascades) {
1554 submenu = menu->cascades[i];
1555 if (submenu->flags.mapped && !submenu->flags.buttoned)
1556 raiseMenus(submenu, submenus);
1559 if (submenus<0 && !menu->flags.buttoned &&
1560 menu->parent && menu->parent->flags.mapped)
1561 raiseMenus(menu->parent, submenus);
1565 WMenu*
1566 wMenuUnderPointer(WScreen *screen)
1568 WObjDescriptor *desc;
1569 Window root_ret, win;
1570 int dummy;
1571 unsigned int mask;
1573 XQueryPointer(dpy, screen->root_win, &root_ret, &win, &dummy, &dummy,
1574 &dummy, &dummy, &mask);
1576 if (win==None) return NULL;
1578 if (XFindContext(dpy, win, wWinContext, (XPointer *)&desc)==XCNOENT)
1579 return NULL;
1581 if (desc->parent_type == WCLASS_MENU)
1582 return (WMenu *)desc->parent;
1583 return NULL;
1588 #define MIN(a,b) (((a) > (b)) ? (b) : (a))
1591 static void
1592 getPointerPosition(WScreen *scr, int *x, int *y)
1594 Window root_ret, win;
1595 int wx, wy;
1596 unsigned int mask;
1598 XQueryPointer(dpy, scr->root_win, &root_ret, &win, x, y, &wx, &wy, &mask);
1602 static void
1603 getScrollAmount(WMenu *menu, int *hamount, int *vamount)
1605 WScreen *scr = menu->menu->screen_ptr;
1606 int menuX1 = menu->frame_x;
1607 int menuY1 = menu->frame_y;
1608 int menuX2 = menu->frame_x + MENUW(menu);
1609 int menuY2 = menu->frame_y + MENUH(menu);
1610 int screenW = scr->scr_width;
1611 int screenH = scr->scr_height;
1612 int xroot, yroot;
1614 *hamount = 0;
1615 *vamount = 0;
1617 getPointerPosition(scr, &xroot, &yroot);
1620 if (xroot <= 1 && menuX1 < 0) {
1621 /* scroll to the right */
1622 *hamount = MIN(MENU_SCROLL_STEP, abs(menuX1));
1624 } else if (xroot >= screenW-2 && menuX2 > screenW-1) {
1625 /* scroll to the left */
1626 *hamount = MIN(MENU_SCROLL_STEP, abs(menuX2-screenW-1));
1628 if (*hamount==0)
1629 *hamount = 1;
1631 *hamount = -*hamount;
1634 if (yroot <= 1 && menuY1 < 0) {
1635 /* scroll down */
1636 *vamount = MIN(MENU_SCROLL_STEP, abs(menuY1));
1638 } else if (yroot >= screenH-2 && menuY2 > screenH-1) {
1639 /* scroll up */
1640 *vamount = MIN(MENU_SCROLL_STEP, abs(menuY2-screenH-2));
1642 *vamount = -*vamount;
1647 static void
1648 dragScrollMenuCallback(void *data)
1650 WMenu *menu = (WMenu*)data;
1651 WScreen *scr = menu->menu->screen_ptr;
1652 WMenu *parent = parentMenu(menu);
1653 int hamount, vamount;
1654 int x, y;
1655 int newSelectedEntry;
1657 getScrollAmount(menu, &hamount, &vamount);
1660 if (hamount != 0 || vamount != 0) {
1661 wMenuMove(parent, parent->frame_x + hamount,
1662 parent->frame_y + vamount, True);
1663 if (findMenu(scr, &x, &y)) {
1664 newSelectedEntry = getEntryAt(menu, x, y);
1665 selectEntry(menu, newSelectedEntry);
1666 } else {
1667 /* Pointer fell outside of menu. If the selected entry is
1668 * not a submenu, unselect it */
1669 if (menu->selected_entry >= 0
1670 && menu->entries[menu->selected_entry]->cascade<0)
1671 selectEntry(menu, -1);
1672 newSelectedEntry = 0;
1675 /* paranoid check */
1676 if (newSelectedEntry >= 0) {
1677 /* keep scrolling */
1678 menu->timer = WMAddTimerHandler(MENU_SCROLL_DELAY,
1679 dragScrollMenuCallback, menu);
1680 } else {
1681 menu->timer = NULL;
1683 } else {
1684 /* don't need to scroll anymore */
1685 menu->timer = NULL;
1686 if (findMenu(scr, &x, &y)) {
1687 newSelectedEntry = getEntryAt(menu, x, y);
1688 selectEntry(menu, newSelectedEntry);
1694 static void
1695 scrollMenuCallback(void *data)
1697 WMenu *menu = (WMenu*)data;
1698 WMenu *parent = parentMenu(menu);
1699 int hamount = 0; /* amount to scroll */
1700 int vamount = 0;
1702 getScrollAmount(menu, &hamount, &vamount);
1704 if (hamount != 0 || vamount != 0) {
1705 wMenuMove(parent, parent->frame_x + hamount,
1706 parent->frame_y + vamount, True);
1708 /* keep scrolling */
1709 menu->timer = WMAddTimerHandler(MENU_SCROLL_DELAY,
1710 scrollMenuCallback, menu);
1711 } else {
1712 /* don't need to scroll anymore */
1713 menu->timer = NULL;
1719 #define MENU_SCROLL_BORDER 5
1721 static int
1722 isPointNearBoder(WMenu *menu, int x, int y)
1724 int menuX1 = menu->frame_x;
1725 int menuY1 = menu->frame_y;
1726 int menuX2 = menu->frame_x + MENUW(menu);
1727 int menuY2 = menu->frame_y + MENUH(menu);
1728 int scrXe = menu->menu->screen_ptr->scr_width-1;
1729 int scrYe = menu->menu->screen_ptr->scr_height-1;
1730 int flag = 0;
1732 if (x >= menuX1 && x <= menuX2 && (y < MENU_SCROLL_BORDER
1733 || y > scrYe-MENU_SCROLL_BORDER))
1734 flag = 1;
1735 else if (y >= menuY1 && y <= menuY2 && (x < MENU_SCROLL_BORDER
1736 || x > scrXe-MENU_SCROLL_BORDER))
1737 flag = 1;
1739 return flag;
1743 void
1744 wMenuScroll(WMenu *menu, XEvent *event)
1746 WMenu *smenu;
1747 WMenu *omenu = parentMenu(menu);
1748 WScreen *scr = menu->frame->screen_ptr;
1749 int done = 0;
1750 int jump_back = 0;
1751 int old_frame_x = omenu->frame_x;
1752 int old_frame_y = omenu->frame_y;
1753 XEvent ev;
1755 #ifdef DEBUG
1756 puts("Entering menu Scroll");
1757 #endif
1759 if ((/*omenu->flags.buttoned &&*/ !wPreferences.wrap_menus)
1760 || omenu->flags.app_menu) {
1761 jump_back = 1;
1764 if (!wPreferences.wrap_menus)
1765 raiseMenus(omenu, True);
1766 else
1767 raiseMenus(menu, False);
1769 if (!menu->timer)
1770 scrollMenuCallback(menu);
1772 while(!done) {
1773 int x, y, on_border, on_x_edge, on_y_edge, on_title;
1775 WMNextEvent(dpy, &ev);
1776 switch (ev.type) {
1777 case EnterNotify:
1778 WMHandleEvent(&ev);
1779 case MotionNotify:
1780 x = (ev.type==MotionNotify) ? ev.xmotion.x_root : ev.xcrossing.x_root;
1781 y = (ev.type==MotionNotify) ? ev.xmotion.y_root : ev.xcrossing.y_root;
1783 /* on_border is != 0 if the pointer is between the menu
1784 * and the screen border and is close enough to the border */
1785 on_border = isPointNearBoder(menu, x, y);
1787 smenu = wMenuUnderPointer(scr);
1789 if ((smenu==NULL && !on_border) || (smenu && parentMenu(smenu)!=omenu)) {
1790 done = 1;
1791 break;
1794 on_x_edge = x <= 1 || x >= scr->scr_width - 2;
1795 on_y_edge = y <= 1 || y >= scr->scr_height - 2;
1796 on_border = on_x_edge || on_y_edge;
1798 if (!on_border && !jump_back) {
1799 done = 1;
1800 break;
1803 if (menu->timer && (smenu!=menu || (!on_y_edge && !on_x_edge))) {
1804 WMDeleteTimerHandler(menu->timer);
1805 menu->timer = NULL;
1808 if (smenu != NULL)
1809 menu = smenu;
1811 if (!menu->timer)
1812 scrollMenuCallback(menu);
1813 break;
1814 case ButtonPress:
1815 /* True if we push on title, or drag the omenu to other position */
1816 on_title = ev.xbutton.x_root >= omenu->frame_x &&
1817 ev.xbutton.x_root <= omenu->frame_x + MENUW(omenu) &&
1818 ev.xbutton.y_root >= omenu->frame_y &&
1819 ev.xbutton.y_root <= omenu->frame_y + omenu->frame->top_width;
1820 WMHandleEvent(&ev);
1821 smenu = wMenuUnderPointer(scr);
1822 if (smenu == NULL || (smenu && smenu->flags.buttoned && smenu != omenu))
1823 done = 1;
1824 else if (smenu==omenu && on_title) {
1825 jump_back = 0;
1826 done = 1;
1828 break;
1829 case KeyPress:
1830 done = 1;
1831 default:
1832 WMHandleEvent(&ev);
1833 break;
1837 if (menu->timer) {
1838 WMDeleteTimerHandler(menu->timer);
1839 menu->timer = NULL;
1842 if (jump_back)
1843 wMenuMove(omenu, old_frame_x, old_frame_y, True);
1845 #ifdef DEBUG
1846 puts("Leaving menu Scroll");
1847 #endif
1852 static void
1853 menuExpose(WObjDescriptor *desc, XEvent *event)
1855 wMenuPaint(desc->parent);
1858 typedef struct {
1859 int *delayed_select;
1860 WMenu *menu;
1861 WMHandlerID magic;
1862 } delay_data;
1865 static void
1866 delaySelection(void *data)
1868 delay_data *d = (delay_data*)data;
1869 int x, y, entry_no;
1870 WMenu *menu;
1872 d->magic = NULL;
1874 menu = findMenu(d->menu->menu->screen_ptr, &x, &y);
1875 if (menu && (d->menu == menu || d->delayed_select)) {
1876 entry_no = getEntryAt(menu, x, y);
1877 selectEntry(menu, entry_no);
1879 if (d->delayed_select)
1880 *(d->delayed_select) = 0;
1884 static void
1885 menuMouseDown(WObjDescriptor *desc, XEvent *event)
1887 XButtonEvent *bev = &event->xbutton;
1888 WMenu *menu = desc->parent;
1889 WMenu *smenu;
1890 WScreen *scr=menu->frame->screen_ptr;
1891 WMenuEntry *entry=NULL;
1892 XEvent ev;
1893 int close_on_exit=0;
1894 int done=0;
1895 int delayed_select = 0;
1896 int entry_no;
1897 int x, y;
1898 int prevx, prevy;
1899 int old_frame_x = 0;
1900 int old_frame_y = 0;
1901 delay_data d_data = {NULL, NULL, NULL};
1903 if (!wPreferences.wrap_menus) {
1904 smenu = parentMenu(menu);
1905 old_frame_x = smenu->frame_x;
1906 old_frame_y = smenu->frame_y;
1908 else if (event->xbutton.window == menu->frame->core->window) {
1909 /* This is true if the menu was launched with right click on root window */
1910 delayed_select = 1;
1911 d_data.delayed_select = &delayed_select;
1912 d_data.menu = menu;
1913 d_data.magic = WMAddTimerHandler(wPreferences.dblclick_time,
1914 delaySelection, &d_data);
1917 wRaiseFrame(menu->frame->core);
1919 close_on_exit = (bev->send_event || menu->flags.brother);
1921 smenu = findMenu(scr, &x, &y);
1922 if (!smenu) {
1923 x = -1;
1924 y = -1;
1925 } else {
1926 menu = smenu;
1929 if (menu->flags.editing) {
1930 return;
1932 entry_no = getEntryAt(menu, x, y);
1933 if (entry_no>=0) {
1934 entry = menu->entries[entry_no];
1936 if (!close_on_exit && (bev->state & ControlMask) && smenu
1937 && entry->flags.editable) {
1938 editEntry(smenu, entry);
1939 return;
1940 } else if (bev->state & ControlMask) {
1941 return;
1944 if (entry->flags.enabled && entry->cascade>=0 && menu->cascades) {
1945 WMenu *submenu = menu->cascades[entry->cascade];
1946 /* map cascade */
1947 if (submenu->flags.mapped && !submenu->flags.buttoned &&
1948 menu->selected_entry!=entry_no) {
1949 wMenuUnmap(submenu);
1951 if (!submenu->flags.mapped && !delayed_select) {
1952 selectEntry(menu, entry_no);
1953 } else if (!submenu->flags.buttoned) {
1954 selectEntry(menu, -1);
1957 } else if (!delayed_select) {
1958 selectEntry(menu, entry_no);
1961 if (!wPreferences.wrap_menus && !wPreferences.scrollable_menus) {
1962 if (!menu->timer)
1963 dragScrollMenuCallback(menu);
1967 prevx = bev->x_root;
1968 prevy = bev->y_root;
1969 while (!done) {
1970 int x, y;
1972 XAllowEvents(dpy, SyncPointer, CurrentTime);
1974 WMMaskEvent(dpy, ExposureMask|ButtonMotionMask|ButtonReleaseMask
1975 |ButtonPressMask, &ev);
1976 switch (ev.type) {
1977 case MotionNotify:
1978 smenu = findMenu(scr, &x, &y);
1980 if (smenu == NULL) {
1981 /* moved mouse out of menu */
1983 if (!delayed_select && d_data.magic) {
1984 WMDeleteTimerHandler(d_data.magic);
1985 d_data.magic = NULL;
1987 if (menu==NULL
1988 || (menu->selected_entry>=0
1989 && menu->entries[menu->selected_entry]->cascade>=0)) {
1990 prevx = ev.xmotion.x_root;
1991 prevy = ev.xmotion.y_root;
1993 break;
1995 selectEntry(menu, -1);
1996 menu = smenu;
1997 prevx = ev.xmotion.x_root;
1998 prevy = ev.xmotion.y_root;
1999 break;
2000 } else if (menu && menu!=smenu
2001 && (menu->selected_entry<0
2002 || menu->entries[menu->selected_entry]->cascade<0)) {
2003 selectEntry(menu, -1);
2005 if (!delayed_select && d_data.magic) {
2006 WMDeleteTimerHandler(d_data.magic);
2007 d_data.magic = NULL;
2009 } else {
2011 /* hysteresis for item selection */
2013 /* check if the motion was to the side, indicating that
2014 * the user may want to cross to a submenu */
2015 if (!delayed_select && menu) {
2016 int dx;
2017 Bool moved_to_submenu;/* moved to direction of submenu */
2019 dx = abs(prevx - ev.xmotion.x_root);
2021 moved_to_submenu = False;
2022 if (dx > 0 /* if moved enough to the side */
2023 /* maybe a open submenu */
2024 && menu->selected_entry>=0
2025 /* moving to the right direction */
2026 && (wPreferences.align_menus
2027 || ev.xmotion.y_root >= prevy)) {
2028 int index;
2030 index = menu->entries[menu->selected_entry]->cascade;
2031 if (index>=0) {
2032 if (menu->cascades[index]->frame_x>menu->frame_x) {
2033 if (prevx < ev.xmotion.x_root)
2034 moved_to_submenu = True;
2035 } else {
2036 if (prevx > ev.xmotion.x_root)
2037 moved_to_submenu = True;
2043 if (menu != smenu) {
2044 if (d_data.magic) {
2045 WMDeleteTimerHandler(d_data.magic);
2047 d_data.magic = NULL;
2048 } else if (moved_to_submenu) {
2049 /* while we are moving, postpone the selection */
2050 if (d_data.magic) {
2051 WMDeleteTimerHandler(d_data.magic);
2053 d_data.delayed_select = NULL;
2054 d_data.menu = menu;
2055 d_data.magic = WMAddTimerHandler(MENU_SELECT_DELAY,
2056 delaySelection,
2057 &d_data);
2058 prevx = ev.xmotion.x_root;
2059 prevy = ev.xmotion.y_root;
2060 break;
2061 } else {
2062 if (d_data.magic)
2063 WMDeleteTimerHandler(d_data.magic);
2064 d_data.magic = NULL;
2068 prevx = ev.xmotion.x_root;
2069 prevy = ev.xmotion.y_root;
2070 if (menu!=smenu) {
2071 /* pointer crossed menus */
2072 if (menu && menu->timer) {
2073 WMDeleteTimerHandler(menu->timer);
2074 menu->timer = NULL;
2076 if (smenu)
2077 dragScrollMenuCallback(smenu);
2079 menu = smenu;
2080 if (!menu->timer)
2081 dragScrollMenuCallback(menu);
2083 if (!delayed_select) {
2084 entry_no = getEntryAt(menu, x, y);
2085 if (entry_no>=0) {
2086 entry = menu->entries[entry_no];
2087 if (entry->flags.enabled && entry->cascade>=0 &&
2088 menu->cascades) {
2089 WMenu *submenu = menu->cascades[entry->cascade];
2090 if (submenu->flags.mapped && !submenu->flags.buttoned
2091 && menu->selected_entry!=entry_no) {
2092 wMenuUnmap(submenu);
2096 selectEntry(menu, entry_no);
2098 break;
2100 case ButtonPress:
2101 break;
2103 case ButtonRelease:
2104 if (ev.xbutton.button == event->xbutton.button)
2105 done=1;
2106 break;
2108 case Expose:
2109 WMHandleEvent(&ev);
2110 break;
2114 if (menu && menu->timer) {
2115 WMDeleteTimerHandler(menu->timer);
2116 menu->timer = NULL;
2118 if (d_data.magic!=NULL)
2119 WMDeleteTimerHandler(d_data.magic);
2121 if (menu && menu->selected_entry>=0) {
2122 entry = menu->entries[menu->selected_entry];
2123 if (entry->callback!=NULL && entry->flags.enabled
2124 && entry->cascade < 0) {
2125 /* blink and erase menu selection */
2126 #if (MENU_BLINK_DELAY > 0)
2127 int sel = menu->selected_entry;
2128 int i;
2130 for (i=0; i<MENU_BLINK_COUNT; i++) {
2131 paintEntry(menu, sel, False);
2132 XSync(dpy, 0);
2133 wusleep(MENU_BLINK_DELAY);
2134 paintEntry(menu, sel, True);
2135 XSync(dpy, 0);
2136 wusleep(MENU_BLINK_DELAY);
2138 #endif
2139 /* unmap the menu, it's parents and call the callback */
2140 if (!menu->flags.buttoned &&
2141 (!menu->flags.app_menu||menu->parent!=NULL)) {
2142 closeCascade(menu);
2143 } else {
2144 selectEntry(menu, -1);
2146 (*entry->callback)(menu, entry);
2148 /* If the user double clicks an entry, the entry will
2149 * be executed twice, which is not good for things like
2150 * the root menu. So, ignore any clicks that were generated
2151 * while the entry was being executed */
2152 while (XCheckTypedWindowEvent(dpy, menu->menu->window,
2153 ButtonPress, &ev));
2154 } else if (entry->callback!=NULL && entry->cascade<0) {
2155 selectEntry(menu, -1);
2156 } else {
2157 if (entry->cascade>=0 && menu->cascades
2158 && menu->cascades[entry->cascade]->flags.brother) {
2159 selectEntry(menu, -1);
2164 if (((WMenu*)desc->parent)->flags.brother || close_on_exit
2165 || !smenu)
2166 closeCascade(desc->parent);
2168 /* close the cascade windows that should not remain opened */
2169 closeBrotherCascadesOf(desc->parent);
2171 if (!wPreferences.wrap_menus)
2172 wMenuMove(parentMenu(desc->parent), old_frame_x, old_frame_y, True);
2176 void
2177 wMenuMove(WMenu *menu, int x, int y, int submenus)
2179 WMenu *submenu;
2180 int i;
2182 if (!menu) return;
2184 menu->frame_x = x;
2185 menu->frame_y = y;
2186 XMoveWindow(dpy, menu->frame->core->window, x, y);
2188 if (submenus>0 && menu->selected_entry>=0) {
2189 i = menu->entries[menu->selected_entry]->cascade;
2191 if (i>=0 && menu->cascades) {
2192 submenu = menu->cascades[i];
2193 if (submenu->flags.mapped && !submenu->flags.buttoned)
2194 if (wPreferences.align_menus) {
2195 wMenuMove(submenu, x + MENUW(menu), y, submenus);
2196 } else {
2197 wMenuMove(submenu, x+ MENUW(menu),
2198 y + submenu->entry_height*menu->selected_entry,
2199 submenus);
2203 if (submenus<0 && menu->parent!=NULL && menu->parent->flags.mapped &&
2204 !menu->parent->flags.buttoned) {
2205 if (wPreferences.align_menus) {
2206 wMenuMove(menu->parent, x - MENUW(menu->parent), y, submenus);
2207 } else {
2208 wMenuMove(menu->parent, x - MENUW(menu->parent), menu->frame_y
2209 - menu->parent->entry_height*menu->parent->selected_entry,
2210 submenus);
2216 static void
2217 changeMenuLevels(WMenu *menu, int lower)
2219 int i;
2221 if (!lower) {
2222 ChangeStackingLevel(menu->frame->core, (!menu->parent ? WMMainMenuLevel
2223 : WMSubmenuLevel));
2224 wRaiseFrame(menu->frame->core);
2225 menu->flags.lowered = 0;
2226 } else {
2227 ChangeStackingLevel(menu->frame->core, WMNormalLevel);
2228 wLowerFrame(menu->frame->core);
2229 menu->flags.lowered = 1;
2231 for (i=0; i<menu->cascade_no; i++) {
2232 if (menu->cascades[i]
2233 && !menu->cascades[i]->flags.buttoned
2234 && menu->cascades[i]->flags.lowered!=lower) {
2235 changeMenuLevels(menu->cascades[i], lower);
2242 static void
2243 menuTitleDoubleClick(WCoreWindow *sender, void *data, XEvent *event)
2245 WMenu *menu = data;
2246 int lower;
2248 if (event->xbutton.state & MOD_MASK) {
2249 if (menu->flags.lowered) {
2250 lower = 0;
2251 } else {
2252 lower = 1;
2254 changeMenuLevels(menu, lower);
2259 static void
2260 menuTitleMouseDown(WCoreWindow *sender, void *data, XEvent *event)
2262 WMenu *menu = data;
2263 WMenu *tmp;
2264 XEvent ev;
2265 int x=menu->frame_x, y=menu->frame_y;
2266 int dx=event->xbutton.x_root, dy=event->xbutton.y_root;
2267 int i, lower;
2268 Bool started;
2270 #ifdef DEBUG
2271 printf("Moving menu\n");
2272 #endif
2274 /* can't touch the menu copy */
2275 if (menu->flags.brother)
2276 return;
2278 if (event->xbutton.button != Button1 && event->xbutton.button != Button2)
2279 return;
2281 if (event->xbutton.state & MOD_MASK) {
2282 wLowerFrame(menu->frame->core);
2283 lower = 1;
2284 } else {
2285 wRaiseFrame(menu->frame->core);
2286 lower = 0;
2288 tmp = menu;
2290 /* lower/raise all submenus */
2291 while (1) {
2292 if (tmp->selected_entry>=0 && tmp->cascades
2293 && tmp->entries[tmp->selected_entry]->cascade>=0) {
2294 tmp = tmp->cascades[tmp->entries[tmp->selected_entry]->cascade];
2295 if (!tmp || !tmp->flags.mapped)
2296 break;
2297 if (lower)
2298 wLowerFrame(tmp->frame->core);
2299 else
2300 wRaiseFrame(tmp->frame->core);
2301 } else {
2302 break;
2306 /* tear off the menu if it's a root menu or a cascade
2307 application menu */
2308 if (!menu->flags.buttoned && !menu->flags.brother
2309 && (!menu->flags.app_menu||menu->parent!=NULL)) {
2310 menu->flags.buttoned=1;
2311 wFrameWindowShowButton(menu->frame, WFF_RIGHT_BUTTON);
2312 if (menu->parent) {
2313 /* turn off selected menu entry in parent menu */
2314 selectEntry(menu->parent, -1);
2316 /* make parent map the copy in place of the original */
2317 for (i=0; i<menu->parent->cascade_no; i++) {
2318 if (menu->parent->cascades[i] == menu) {
2319 menu->parent->cascades[i] = menu->brother;
2320 break;
2326 started = False;
2327 while(1) {
2328 WMMaskEvent(dpy, ButtonMotionMask|ButtonReleaseMask|ButtonPressMask
2329 |ExposureMask, &ev);
2330 switch (ev.type) {
2331 case MotionNotify:
2332 if (started) {
2333 x += ev.xmotion.x_root - dx;
2334 y += ev.xmotion.y_root - dy;
2335 dx = ev.xmotion.x_root;
2336 dy = ev.xmotion.y_root;
2337 wMenuMove(menu, x, y, True);
2338 } else {
2339 if (abs(ev.xmotion.x_root - dx) > MOVE_THRESHOLD
2340 || abs(ev.xmotion.y_root - dy) > MOVE_THRESHOLD) {
2341 started = True;
2342 XGrabPointer(dpy, menu->frame->titlebar->window, False,
2343 ButtonMotionMask|ButtonReleaseMask
2344 |ButtonPressMask,
2345 GrabModeAsync, GrabModeAsync, None,
2346 wCursor[WCUR_MOVE], CurrentTime);
2349 break;
2351 case ButtonPress:
2352 break;
2354 case ButtonRelease:
2355 if (ev.xbutton.button != event->xbutton.button)
2356 break;
2357 #ifdef DEBUG
2358 printf("End menu move\n");
2359 #endif
2360 XUngrabPointer(dpy, CurrentTime);
2361 return;
2363 default:
2364 WMHandleEvent(&ev);
2365 break;
2371 *----------------------------------------------------------------------
2372 * menuCloseClick--
2373 * Handles mouse click on the close button of menus. The menu is
2374 * closed when the button is clicked.
2376 * Side effects:
2377 * The closed menu is reinserted at it's parent menus
2378 * cascade list.
2379 *----------------------------------------------------------------------
2381 static void
2382 menuCloseClick(WCoreWindow *sender, void *data, XEvent *event)
2384 WMenu *menu = (WMenu*)data;
2385 WMenu *parent = menu->parent;
2386 int i;
2388 if (parent) {
2389 for (i=0; i<parent->cascade_no; i++) {
2390 /* find the entry that points to the copy */
2391 if (parent->cascades[i] == menu->brother) {
2392 /* make it point to the original */
2393 parent->cascades[i] = menu;
2394 menu->parent = parent;
2395 break;
2399 wMenuUnmap(menu);
2403 void
2404 wMenuSaveState(WScreen *scr)
2406 proplist_t menus, key, value;
2407 int save_menus = 0;
2408 char buffer[256];
2410 menus = PLMakeDictionaryFromEntries(NULL, NULL, NULL);
2412 #ifndef LITE
2413 if (scr->root_menu && scr->root_menu->flags.buttoned) {
2414 sprintf(buffer, "%i,%i", scr->root_menu->frame_x,
2415 scr->root_menu->frame_y);
2416 key = PLMakeString("RootMenu");
2417 value = PLMakeString(buffer);
2418 PLInsertDictionaryEntry(menus, key, value);
2419 PLRelease(key);
2420 PLRelease(value);
2421 save_menus = 1;
2424 if (scr->switch_menu && scr->switch_menu->flags.buttoned) {
2425 sprintf(buffer, "%i,%i", scr->switch_menu->frame_x,
2426 scr->switch_menu->frame_y);
2427 key = PLMakeString("SwitchMenu");
2428 value = PLMakeString(buffer);
2429 PLInsertDictionaryEntry(menus, key, value);
2430 PLRelease(key);
2431 PLRelease(value);
2432 save_menus = 1;
2434 #endif /* !LITE */
2435 if (scr->workspace_menu && scr->workspace_menu->flags.buttoned) {
2436 sprintf(buffer, "%i,%i", scr->workspace_menu->frame_x,
2437 scr->workspace_menu->frame_y);
2438 key = PLMakeString("WorkspaceMenu");
2439 value = PLMakeString(buffer);
2440 PLInsertDictionaryEntry(menus, key, value);
2441 PLRelease(key);
2442 PLRelease(value);
2443 save_menus = 1;
2446 if (save_menus) {
2447 key = PLMakeString("Menus");
2448 PLInsertDictionaryEntry(scr->session_state, key, menus);
2449 PLRelease(key);
2451 PLRelease(menus);
2455 #define COMPLAIN(key) wwarning(_("bad value in menus state info:%s"), key)
2458 static int
2459 restoreMenu(WScreen *scr, proplist_t menu, int which)
2461 int i, x, y;
2462 WMenu *pmenu = NULL;
2464 if (!menu)
2465 return False;
2467 if (!PLIsString(menu)) {
2468 COMPLAIN("Position");
2469 return False;
2472 if (sscanf(PLGetString(menu), "%i,%i", &x, &y)!=2)
2473 COMPLAIN("Position");
2475 #ifndef LITE
2476 if (which & WSS_ROOTMENU) {
2477 OpenRootMenu(scr, x, y, False);
2478 pmenu = scr->root_menu;
2479 } else if (which & WSS_SWITCHMENU) {
2480 OpenSwitchMenu(scr, x, y, False);
2481 pmenu = scr->switch_menu;
2482 } else
2483 #endif /* !LITE */
2484 if (which & WSS_WSMENU) {
2485 OpenWorkspaceMenu(scr, x, y);
2486 pmenu = scr->workspace_menu;
2487 if (pmenu->parent) {
2488 /* make parent map the copy in place of the original */
2489 for (i=0; i<pmenu->parent->cascade_no; i++) {
2490 if (pmenu->parent->cascades[i] == pmenu) {
2491 pmenu->parent->cascades[i] = pmenu->brother;
2492 break;
2498 if (pmenu) {
2499 int width = MENUW(pmenu);
2500 int height = MENUH(pmenu);
2502 x = (x < -width) ? 0 : x;
2503 x = (x > scr->scr_width) ? scr->scr_width - width : x;
2504 y = (y < 0) ? 0 : y;
2505 y = (y > scr->scr_height) ? scr->scr_height - height : y;
2506 wMenuMove(pmenu, x, y, True);
2507 pmenu->flags.buttoned = 1;
2508 wFrameWindowShowButton(pmenu->frame, WFF_RIGHT_BUTTON);
2509 return True;
2511 return False;
2515 void
2516 wMenuRestoreState(WScreen *scr)
2518 proplist_t menus, menu, key, rkey, skey, wkey;
2520 key = PLMakeString("Menus");
2521 menus = PLGetDictionaryEntry(scr->session_state, key);
2522 PLRelease(key);
2524 if (!menus)
2525 return;
2527 /* restore menus */
2529 rkey = PLMakeString("RootMenu");
2530 skey = PLMakeString("SwitchMenu");
2531 wkey = PLMakeString("WorkspaceMenu");
2532 menu = PLGetDictionaryEntry(menus, rkey);
2533 restoreMenu(scr, menu, WSS_ROOTMENU);
2534 menu = PLGetDictionaryEntry(menus, skey);
2535 restoreMenu(scr, menu, WSS_SWITCHMENU);
2536 menu = PLGetDictionaryEntry(menus, wkey);
2537 restoreMenu(scr, menu, WSS_WSMENU);
2539 PLRelease(rkey);
2540 PLRelease(skey);
2541 PLRelease(wkey);
2545 void
2546 OpenWorkspaceMenu(WScreen *scr, int x, int y)
2548 WMenu *menu, *parent;
2549 WMenuEntry *entry;
2551 #ifndef LITE
2552 if (!scr->root_menu) {
2553 OpenRootMenu(scr, scr->scr_width*2, 0, False);
2554 wMenuUnmap(scr->root_menu);
2556 #endif
2557 menu = scr->workspace_menu;
2558 if (menu) {
2559 if (menu->flags.mapped) {
2560 if (!menu->flags.buttoned) {
2561 wMenuUnmap(menu);
2562 parent = menu->parent;
2563 if (parent && parent->selected_entry >= 0) {
2564 entry = parent->entries[parent->selected_entry];
2565 if (parent->cascades[entry->cascade] == menu) {
2566 selectEntry(parent, -1);
2567 wMenuMapAt(menu, x, y, False);
2570 } else {
2571 wRaiseFrame(menu->frame->core);
2572 wMenuMapCopyAt(menu, x, y);
2575 else {
2576 wMenuMapAt(menu, x, y, False);