- Fixed wrong colors for text and tile with selected menu items
[wmaker-crm.git] / src / menu.c
blob6f65590441a8f86e780978f0d1033f3d6d3296fa
1 /* menu.c- generic menu, used for root menu, application menus etc.
3 * Window Maker window manager
5 * Copyright (c) 1997-2002 Alfredo K. Kojima
6 * Copyright (c) 1998-2002 Dan Pascu
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
21 * USA.
24 #include "wconfig.h"
26 #include <X11/Xlib.h>
27 #include <X11/Xutil.h>
28 #include <X11/keysym.h>
29 #include <stdlib.h>
30 #include <string.h>
31 #include <stdio.h>
32 #include <unistd.h>
33 #include <ctype.h>
34 #if 0
35 #include <nana.h>
36 #endif
37 #include "WindowMaker.h"
38 #include "wcore.h"
39 #include "framewin.h"
40 #include "menu.h"
41 #include "actions.h"
42 #include "funcs.h"
43 #include "stacking.h"
44 #include "text.h"
47 /****** Global Variables ******/
49 extern Cursor wCursor[WCUR_LAST];
51 extern XContext wWinContext;
53 extern WPreferences wPreferences;
55 #define MOD_MASK wPreferences.modifier_mask
57 #define MENU_SCROLL_STEP menuScrollParameters[(int)wPreferences.menu_scroll_speed].steps
58 #define MENU_SCROLL_DELAY menuScrollParameters[(int)wPreferences.menu_scroll_speed].delay
62 #define MENUW(m) ((m)->frame->core->width+2*FRAME_BORDER_WIDTH)
63 #define MENUH(m) ((m)->frame->core->height+2*FRAME_BORDER_WIDTH)
66 /***** Local Stuff ******/
68 static struct {
69 int steps;
70 int delay;
71 } menuScrollParameters[5] = {
72 {MENU_SCROLL_STEPS_UF, MENU_SCROLL_DELAY_UF},
73 {MENU_SCROLL_STEPS_F, MENU_SCROLL_DELAY_F},
74 {MENU_SCROLL_STEPS_M, MENU_SCROLL_DELAY_M},
75 {MENU_SCROLL_STEPS_S, MENU_SCROLL_DELAY_S},
76 {MENU_SCROLL_STEPS_US, MENU_SCROLL_DELAY_US}};
79 static void menuMouseDown(WObjDescriptor *desc, XEvent *event);
80 static void menuExpose(WObjDescriptor *desc, XEvent *event);
82 static void menuTitleDoubleClick(WCoreWindow *sender, void *data, XEvent *event);
83 static void menuTitleMouseDown(WCoreWindow *sender, void *data, XEvent *event);
85 static void menuCloseClick(WCoreWindow *sender, void *data, XEvent *event);
87 static void updateTexture(WMenu *menu);
89 #ifndef LITE
90 static int saveMenuRecurs(WMPropList *menus, WScreen *scr, WMenu *menu);
91 static int restoreMenuRecurs(WScreen *scr, WMPropList *menus, WMenu *menu, char *path);
92 #endif /* !LITE */
94 static void selectEntry(WMenu *menu, int entry_no);
95 static void closeCascade(WMenu *menu);
98 /****** Notification Observers ******/
100 static void
101 appearanceObserver(void *self, WMNotification *notif)
103 WMenu *menu = (WMenu*)self;
104 int flags = (int)WMGetNotificationClientData(notif);
106 if (!menu->flags.realized)
107 return;
109 if (WMGetNotificationName(notif) == WNMenuAppearanceSettingsChanged) {
110 if (flags & WFontSettings) {
111 menu->flags.realized = 0;
112 wMenuRealize(menu);
114 if (flags & WTextureSettings) {
115 if (!menu->flags.brother)
116 updateTexture(menu);
118 if (flags & (WTextureSettings|WColorSettings)) {
119 wMenuPaint(menu);
121 } else if (menu->flags.titled) {
123 if (flags & WFontSettings) {
124 menu->flags.realized = 0;
125 wMenuRealize(menu);
127 if (flags & WTextureSettings) {
128 menu->frame->flags.need_texture_remake = 1;
130 if (flags & (WColorSettings|WTextureSettings)) {
131 wFrameWindowPaint(menu->frame);
136 /************************************/
140 *----------------------------------------------------------------------
141 * wMenuCreate--
142 * Creates a new empty menu with the specified title. If main_menu
143 * is True, the created menu will be a main menu, which has some special
144 * properties such as being placed over other normal menus.
145 * If title is NULL, the menu will have no titlebar.
147 * Returns:
148 * The created menu.
149 *----------------------------------------------------------------------
151 WMenu*
152 wMenuCreate(WScreen *screen, char *title, int main_menu)
154 WMenu *menu;
155 static int brother=0;
156 int tmp, flags;
158 menu = wmalloc(sizeof(WMenu));
160 memset(menu, 0, sizeof(WMenu));
162 #ifdef SINGLE_MENULEVEL
163 tmp = WMSubmenuLevel;
164 #else
165 tmp = (main_menu ? WMMainMenuLevel : WMSubmenuLevel);
166 #endif
168 flags = WFF_SINGLE_STATE|WFF_BORDER;
169 if (title) {
170 flags |= WFF_TITLEBAR|WFF_RIGHT_BUTTON;
171 menu->flags.titled = 1;
173 menu->frame =
174 wFrameWindowCreate(screen, tmp, 8, 2, 1, 1, &wPreferences.menu_title_clearance, flags,
175 screen->menu_title_texture, NULL,
176 screen->menu_title_color,
177 &screen->menu_title_font);
179 menu->frame->core->descriptor.parent = menu;
180 menu->frame->core->descriptor.parent_type = WCLASS_MENU;
181 menu->frame->core->descriptor.handle_mousedown = menuMouseDown;
183 wFrameWindowHideButton(menu->frame, WFF_RIGHT_BUTTON);
185 if (title) {
186 menu->frame->title = wstrdup(title);
189 menu->frame->flags.justification = WTJ_LEFT;
191 menu->frame->rbutton_image = screen->b_pixmaps[WBUT_CLOSE];
193 menu->entry_no = 0;
194 menu->alloced_entries = 0;
195 menu->selected_entry = -1;
196 menu->entries = NULL;
198 menu->frame_x = screen->app_menu_x;
199 menu->frame_y = screen->app_menu_y;
201 menu->frame->child = menu;
203 menu->flags.lowered = 0;
205 /* create borders */
206 if (title) {
207 /* setup object descriptors */
208 menu->frame->on_mousedown_titlebar = menuTitleMouseDown;
209 menu->frame->on_dblclick_titlebar = menuTitleDoubleClick;
212 menu->frame->on_click_right = menuCloseClick;
215 menu->menu = wCoreCreate(menu->frame->core, 0, menu->frame->top_width,
216 menu->frame->core->width, 10);
218 menu->menu->descriptor.parent = menu;
219 menu->menu->descriptor.parent_type = WCLASS_MENU;
220 menu->menu->descriptor.handle_expose = menuExpose;
221 menu->menu->descriptor.handle_mousedown = menuMouseDown;
223 menu->menu_texture_data = None;
225 XMapWindow(dpy, menu->menu->window);
227 XFlush(dpy);
229 if (!brother) {
230 brother = 1;
231 menu->brother = wMenuCreate(screen, title, main_menu);
232 brother = 0;
233 menu->brother->flags.brother = 1;
234 menu->brother->brother = menu;
236 WMAddNotificationObserver(appearanceObserver, menu,
237 WNMenuAppearanceSettingsChanged, menu);
239 WMAddNotificationObserver(appearanceObserver, menu,
240 WNMenuTitleAppearanceSettingsChanged, menu);
243 return menu;
249 WMenu*
250 wMenuCreateForApp(WScreen *screen, char *title, int main_menu)
252 WMenu *menu;
254 menu = wMenuCreate(screen, title, main_menu);
255 if (!menu)
256 return NULL;
257 menu->flags.app_menu = 1;
258 menu->brother->flags.app_menu = 1;
260 return menu;
265 static void
266 insertEntry(WMenu *menu, WMenuEntry *entry, int index)
268 int i;
270 for (i = menu->entry_no-1; i >= index; i--) {
271 menu->entries[i]->order++;
272 menu->entries[i+1] = menu->entries[i];
274 menu->entries[index] = entry;
278 WMenuEntry*
279 wMenuInsertCallback(WMenu *menu, int index, char *text,
280 void (*callback)(WMenu *menu, WMenuEntry *entry),
281 void *clientdata)
283 WMenuEntry *entry;
285 menu->flags.realized = 0;
286 menu->brother->flags.realized = 0;
288 /* reallocate array if it's too small */
289 if (menu->entry_no >= menu->alloced_entries) {
290 void *tmp;
292 tmp = wrealloc(menu->entries,
293 sizeof(WMenuEntry)*(menu->alloced_entries+5));
295 menu->entries = tmp;
296 menu->alloced_entries += 5;
298 menu->brother->entries = tmp;
299 menu->brother->alloced_entries = menu->alloced_entries;
301 entry = wmalloc(sizeof(WMenuEntry));
302 memset(entry, 0, sizeof(WMenuEntry));
303 entry->flags.enabled = 1;
304 entry->text = wstrdup(text);
305 entry->cascade = -1;
306 entry->clientdata = clientdata;
307 entry->callback = callback;
308 if (index<0 || index>=menu->entry_no) {
309 entry->order = menu->entry_no;
310 menu->entries[menu->entry_no] = entry;
311 } else {
312 entry->order = index;
313 insertEntry(menu, entry, index);
316 menu->entry_no++;
317 menu->brother->entry_no = menu->entry_no;
319 return entry;
324 void
325 wMenuEntrySetCascade(WMenu *menu, WMenuEntry *entry, WMenu *cascade)
327 WMenu *brother = menu->brother;
328 int i, done;
330 assert(menu->flags.brother==0);
332 if (entry->cascade>=0) {
333 menu->flags.realized = 0;
334 brother->flags.realized = 0;
337 cascade->parent = menu;
339 cascade->brother->parent = brother;
341 done = 0;
342 for (i=0; i<menu->cascade_no; i++) {
343 if (menu->cascades[i]==NULL) {
344 menu->cascades[i] = cascade;
345 brother->cascades[i] = cascade->brother;
346 done = 1;
347 entry->cascade = i;
348 break;
351 if (!done) {
352 entry->cascade = menu->cascade_no;
354 menu->cascades = wrealloc(menu->cascades,
355 sizeof(WMenu)*(menu->cascade_no+1));
356 menu->cascades[menu->cascade_no++] = cascade;
359 brother->cascades = wrealloc(brother->cascades,
360 sizeof(WMenu)*(brother->cascade_no+1));
361 brother->cascades[brother->cascade_no++] = cascade->brother;
365 if (menu->flags.lowered) {
367 cascade->flags.lowered = 1;
368 ChangeStackingLevel(cascade->frame->core, WMNormalLevel);
370 cascade->brother->flags.lowered = 1;
371 ChangeStackingLevel(cascade->brother->frame->core, WMNormalLevel);
374 if (!menu->flags.realized)
375 wMenuRealize(menu);
379 void
380 wMenuEntryRemoveCascade(WMenu *menu, WMenuEntry *entry)
382 assert(menu->flags.brother==0);
384 /* destroy cascade menu */
385 if (entry->cascade>=0 && menu->cascades
386 && menu->cascades[entry->cascade]!=NULL) {
388 wMenuDestroy(menu->cascades[entry->cascade], True);
390 menu->cascades[entry->cascade] = NULL;
391 menu->brother->cascades[entry->cascade] = NULL;
393 entry->cascade = -1;
398 void
399 wMenuRemoveItem(WMenu *menu, int index)
401 int i;
403 if (menu->flags.brother) {
404 wMenuRemoveItem(menu->brother, index);
405 return;
408 if (index>=menu->entry_no) return;
410 /* destroy cascade menu */
411 wMenuEntryRemoveCascade(menu, menu->entries[index]);
413 /* destroy unshared data */
415 if (menu->entries[index]->text)
416 wfree(menu->entries[index]->text);
418 if (menu->entries[index]->rtext)
419 wfree(menu->entries[index]->rtext);
421 if (menu->entries[index]->free_cdata && menu->entries[index]->clientdata)
422 (*menu->entries[index]->free_cdata)(menu->entries[index]->clientdata);
424 wfree(menu->entries[index]);
426 for (i=index; i<menu->entry_no-1; i++) {
427 menu->entries[i+1]->order--;
428 menu->entries[i]=menu->entries[i+1];
430 menu->entry_no--;
431 menu->brother->entry_no--;
435 static Pixmap
436 renderTexture(WMenu *menu)
438 RImage *img;
439 Pixmap pix;
440 int i;
441 RColor light;
442 RColor dark;
443 RColor mid;
444 WScreen *scr = menu->menu->screen_ptr;
445 WTexture *texture = scr->menu_item_texture;
447 if (wPreferences.menu_style == MS_NORMAL) {
448 img = wTextureRenderImage(texture, menu->menu->width,
449 menu->entry_height, WREL_MENUENTRY);
450 } else {
451 img = wTextureRenderImage(texture, menu->menu->width,
452 menu->menu->height+1, WREL_MENUENTRY);
454 if (!img) {
455 wwarning(_("could not render texture: %s"),
456 RMessageForError(RErrorCode));
458 return None;
461 if (wPreferences.menu_style == MS_SINGLE_TEXTURE) {
462 light.alpha = 0;
463 light.red = light.green = light.blue = 80;
465 dark.alpha = 255;
466 dark.red = dark.green = dark.blue = 0;
468 mid.alpha = 0;
469 mid.red = mid.green = mid.blue = 40;
471 for (i = 1; i < menu->entry_no; i++) {
472 ROperateLine(img, RSubtractOperation, 0, i*menu->entry_height-2,
473 menu->menu->width-1, i*menu->entry_height-2, &mid);
475 RDrawLine(img, 0, i*menu->entry_height-1,
476 menu->menu->width-1, i*menu->entry_height-1, &dark);
478 ROperateLine(img, RAddOperation, 0, i*menu->entry_height,
479 menu->menu->width-1, i*menu->entry_height,
480 &light);
483 if (!RConvertImage(scr->rcontext, img, &pix)) {
484 wwarning(_("error rendering image:%s"), RMessageForError(RErrorCode));
486 RReleaseImage(img);
488 return pix;
492 static void
493 updateTexture(WMenu *menu)
495 WScreen *scr = menu->menu->screen_ptr;
497 /* setup background texture */
498 if (scr->menu_item_texture->any.type != WTEX_SOLID) {
499 if (!menu->flags.brother) {
500 FREE_PIXMAP(menu->menu_texture_data);
502 menu->menu_texture_data = renderTexture(menu);
504 XSetWindowBackgroundPixmap(dpy, menu->menu->window,
505 menu->menu_texture_data);
506 XClearWindow(dpy, menu->menu->window);
508 XSetWindowBackgroundPixmap(dpy, menu->brother->menu->window,
509 menu->menu_texture_data);
510 XClearWindow(dpy, menu->brother->menu->window);
512 } else {
513 XSetWindowBackground(dpy, menu->menu->window,
514 scr->menu_item_texture->any.color.pixel);
515 XClearWindow(dpy, menu->menu->window);
520 void
521 wMenuRealize(WMenu *menu)
523 int i;
524 int width, rwidth, mrwidth, mwidth;
525 int theight, twidth, eheight;
526 WScreen *scr = menu->frame->screen_ptr;
527 static int brother_done=0;
528 int flags;
530 if (!brother_done) {
531 brother_done = 1;
532 wMenuRealize(menu->brother);
533 brother_done = 0;
536 flags = WFF_SINGLE_STATE|WFF_BORDER;
537 if (menu->flags.titled)
538 flags |= WFF_TITLEBAR|WFF_RIGHT_BUTTON;
540 wFrameWindowUpdateBorders(menu->frame, flags);
542 if (menu->flags.titled) {
543 twidth = WMWidthOfString(scr->menu_title_font, menu->frame->title,
544 strlen(menu->frame->title));
545 theight = menu->frame->top_width;
546 twidth += theight + (wPreferences.new_style ? 16 : 8);
547 } else {
548 twidth = 0;
549 theight = 0;
551 eheight = WMFontHeight(scr->menu_entry_font) + 6 + wPreferences.menu_text_clearance * 2;
552 menu->entry_height = eheight;
553 mrwidth = 0;
554 mwidth = 0;
555 for (i=0; i<menu->entry_no; i++) {
556 char *text;
558 /* search widest text */
559 text = menu->entries[i]->text;
560 width = WMWidthOfString(scr->menu_entry_font, text, strlen(text))+10;
562 if (menu->entries[i]->flags.indicator) {
563 width += MENU_INDICATOR_SPACE;
566 if (width > mwidth)
567 mwidth = width;
569 /* search widest text on right */
570 text = menu->entries[i]->rtext;
571 if (text)
572 rwidth = WMWidthOfString(scr->menu_entry_font, text, strlen(text))
573 + 5;
574 else if (menu->entries[i]->cascade>=0)
575 rwidth = 16;
576 else
577 rwidth = 4;
579 if (rwidth > mrwidth)
580 mrwidth = rwidth;
582 mwidth += mrwidth;
584 if (mwidth < twidth)
585 mwidth = twidth;
588 wCoreConfigure(menu->menu, 0, theight, mwidth, menu->entry_no*eheight -1);
590 wFrameWindowResize(menu->frame, mwidth, menu->entry_no*eheight-1
591 + menu->frame->top_width + menu->frame->bottom_width);
594 updateTexture(menu);
596 menu->flags.realized = 1;
598 if (menu->flags.mapped)
599 wMenuPaint(menu);
600 if (menu->brother->flags.mapped)
601 wMenuPaint(menu->brother);
605 void
606 wMenuDestroy(WMenu *menu, int recurse)
608 int i;
610 WMRemoveNotificationObserver(menu);
612 /* remove any pending timers */
613 if (menu->timer)
614 WMDeleteTimerHandler(menu->timer);
615 menu->timer = NULL;
617 /* call destroy handler */
618 if (menu->on_destroy)
619 (*menu->on_destroy)(menu);
621 /* Destroy items if this menu own them. If this is the "brother" menu,
622 * leave them alone as it is shared by them.
624 if (!menu->flags.brother) {
625 for (i=0; i<menu->entry_no; i++) {
627 wfree(menu->entries[i]->text);
629 if (menu->entries[i]->rtext)
630 wfree(menu->entries[i]->rtext);
631 #ifdef USER_MENU
633 if (menu->entries[i]->instances){
634 WMReleasePropList(menu->entries[i]->instances);
636 #endif /* USER_MENU */
638 if (menu->entries[i]->free_cdata && menu->entries[i]->clientdata) {
639 (*menu->entries[i]->free_cdata)(menu->entries[i]->clientdata);
641 wfree(menu->entries[i]);
644 if (recurse) {
645 for (i=0; i<menu->cascade_no; i++) {
646 if (menu->cascades[i]) {
647 if (menu->cascades[i]->flags.brother)
648 wMenuDestroy(menu->cascades[i]->brother, recurse);
649 else
650 wMenuDestroy(menu->cascades[i], recurse);
655 if (menu->entries)
656 wfree(menu->entries);
660 FREE_PIXMAP(menu->menu_texture_data);
662 if (menu->cascades)
663 wfree(menu->cascades);
665 wCoreDestroy(menu->menu);
666 wFrameWindowDestroy(menu->frame);
668 /* destroy copy of this menu */
669 if (!menu->flags.brother && menu->brother)
670 wMenuDestroy(menu->brother, False);
672 wfree(menu);
676 #define F_NORMAL 0
677 #define F_TOP 1
678 #define F_BOTTOM 2
679 #define F_NONE 3
681 static void
682 drawFrame(WScreen *scr, Drawable win, int y, int w, int h, int type)
684 XSegment segs[2];
685 int i;
687 i = 0;
688 segs[i].x1 = segs[i].x2 = w-1;
689 segs[i].y1 = y;
690 segs[i].y2 = y + h - 1;
691 i++;
692 if (type != F_TOP && type != F_NONE) {
693 segs[i].x1 = 1;
694 segs[i].y1 = segs[i].y2 = y + h-2;
695 segs[i].x2 = w-1;
696 i++;
698 XDrawSegments(dpy, win, scr->menu_item_auxtexture->dim_gc, segs, i);
700 i = 0;
701 segs[i].x1 = 0;
702 segs[i].y1 = y;
703 segs[i].x2 = 0;
704 segs[i].y2 = y + h - 1;
705 i++;
706 if (type != F_BOTTOM && type != F_NONE) {
707 segs[i].x1 = 0;
708 segs[i].y1 = y;
709 segs[i].x2 = w-1;
710 segs[i].y2 = y;
711 i++;
713 XDrawSegments(dpy, win, scr->menu_item_auxtexture->light_gc, segs, i);
715 if (type != F_TOP && type != F_NONE)
716 XDrawLine(dpy, win, scr->menu_item_auxtexture->dark_gc, 0, y+h-1,
717 w-1, y+h-1);
721 static void
722 paintEntry(WMenu *menu, int index, int selected)
724 WScreen *scr=menu->frame->screen_ptr;
725 Window win = menu->menu->window;
726 WMenuEntry *entry=menu->entries[index];
727 GC light, dim, dark;
728 WMColor *color;
729 int x, y, w, h, tw;
730 int type;
732 if (!menu->flags.realized) return;
733 h = menu->entry_height;
734 w = menu->menu->width;
735 y = index * h;
737 light = scr->menu_item_auxtexture->light_gc;
738 dim = scr->menu_item_auxtexture->dim_gc;
739 dark = scr->menu_item_auxtexture->dark_gc;
741 if (wPreferences.menu_style == MS_FLAT && menu->entry_no > 1) {
742 if (index == 0)
743 type = F_TOP;
744 else if (index == menu->entry_no - 1)
745 type = F_BOTTOM;
746 else
747 type = F_NONE;
748 } else {
749 type = F_NORMAL;
752 /* paint background */
753 if (selected) {
754 XFillRectangle(dpy, win, WMColorGC(scr->select_color), 1, y+1, w-2, h-3);
755 if (scr->menu_item_texture->any.type == WTEX_SOLID)
756 drawFrame(scr, win, y, w, h, type);
757 } else {
758 if (scr->menu_item_texture->any.type == WTEX_SOLID) {
759 XClearArea(dpy, win, 0, y + 1, w - 1, h - 3, False);
760 /* draw the frame */
761 drawFrame(scr, win, y, w, h, type);
762 } else {
763 XClearArea(dpy, win, 0, y, w, h, False);
767 if (selected) {
768 if (entry->flags.enabled)
769 color = scr->select_text_color;
770 else
771 color = scr->dtext_color;
772 } else if (!entry->flags.enabled) {
773 color = scr->dtext_color;
774 } else {
775 color = scr->mtext_color;
777 /* draw text */
778 x = 5;
779 if (entry->flags.indicator)
780 x += MENU_INDICATOR_SPACE + 2;
782 WMDrawString(scr->wmscreen, win, color, scr->menu_entry_font,
783 x, 3 + y + wPreferences.menu_text_clearance, entry->text, strlen(entry->text));
785 if (entry->cascade>=0) {
786 /* draw the cascade indicator */
787 XDrawLine(dpy,win,dim, w-11, y+6, w-6, y+h/2-1);
788 XDrawLine(dpy,win,light, w-11, y+h-8, w-6, y+h/2-1);
789 XDrawLine(dpy,win,dark, w-12, y+6, w-12, y+h-8);
792 /* draw indicator */
793 if (entry->flags.indicator && entry->flags.indicator_on) {
794 int iw, ih;
795 WPixmap *indicator;
798 switch (entry->flags.indicator_type) {
799 case MI_CHECK:
800 indicator = scr->menu_check_indicator;
801 break;
802 case MI_MINIWINDOW:
803 indicator = scr->menu_mini_indicator;
804 break;
805 case MI_HIDDEN:
806 indicator = scr->menu_hide_indicator;
807 break;
808 case MI_SHADED:
809 indicator = scr->menu_shade_indicator;
810 break;
811 case MI_DIAMOND:
812 default:
813 indicator = scr->menu_radio_indicator;
814 break;
817 iw = indicator->width;
818 ih = indicator->height;
819 XSetClipMask(dpy, scr->copy_gc, indicator->mask);
820 XSetClipOrigin(dpy, scr->copy_gc, 5, y+(h-ih)/2);
821 if (selected) {
822 if (entry->flags.enabled) {
823 XSetForeground(dpy, scr->copy_gc, WMColorPixel(scr->select_text_color));
824 } else {
825 XSetForeground(dpy, scr->copy_gc, WMColorPixel(scr->dtext_color));
827 } else {
828 if (entry->flags.enabled) {
829 XSetForeground(dpy, scr->copy_gc, WMColorPixel(scr->mtext_color));
830 } else {
831 XSetForeground(dpy, scr->copy_gc, WMColorPixel(scr->dtext_color));
834 XFillRectangle(dpy, win, scr->copy_gc, 5, y+(h-ih)/2, iw, ih);
836 XCopyArea(dpy, indicator->image, win, scr->copy_gc, 0, 0,
837 iw, ih, 5, y+(h-ih)/2);
839 XSetClipOrigin(dpy, scr->copy_gc, 0, 0);
842 /* draw right text */
844 if (entry->rtext && entry->cascade<0) {
845 tw = WMWidthOfString(scr->menu_entry_font, entry->rtext,
846 strlen(entry->rtext));
848 WMDrawString(scr->wmscreen, win, color, scr->menu_entry_font, w-6-tw,
849 y + 3 + wPreferences.menu_text_clearance, entry->rtext, strlen(entry->rtext));
854 static void
855 move_menus(WMenu *menu, int x, int y)
857 while (menu->parent) {
858 menu = menu->parent;
859 x -= MENUW(menu);
860 if (!wPreferences.align_menus && menu->selected_entry>=0) {
861 y -= menu->selected_entry*menu->entry_height;
864 wMenuMove(menu, x, y, True);
867 static void
868 makeVisible(WMenu *menu)
870 WScreen *scr = menu->frame->screen_ptr;
871 int x1, y1, x2, y2, new_x, new_y, move;
873 if (menu->entry_no<0) return;
875 x1 = menu->frame_x;
876 y1 = menu->frame_y+menu->frame->top_width
877 + menu->selected_entry*menu->entry_height;
878 x2 = x1 + MENUW(menu);
879 y2 = y1 + menu->entry_height;
881 new_x = x1;
882 new_y = y1;
883 move = 0;
885 if (x1 < 0) {
886 new_x = 0;
887 move = 1;
888 } else if (x2 >= scr->scr_width) {
889 new_x = scr->scr_width - MENUW(menu) - 1;
890 move = 1;
893 if (y1 < 0) {
894 new_y = 0;
895 move = 1;
896 } else if (y2 >= scr->scr_height) {
897 new_y = scr->scr_height - menu->entry_height - 1;
898 move = 1;
901 new_y = new_y - menu->frame->top_width
902 - menu->selected_entry*menu->entry_height;
903 move_menus(menu, new_x, new_y);
907 static int
908 check_key(WMenu *menu, XKeyEvent *event)
910 int i, ch, s;
911 char buffer[32];
913 if (XLookupString(event, buffer, 32, NULL, NULL)<1)
914 return -1;
916 ch = toupper(buffer[0]);
918 s = (menu->selected_entry>=0 ? menu->selected_entry+1 : 0);
920 again:
921 for (i=s; i<menu->entry_no; i++) {
922 if (ch==toupper(menu->entries[i]->text[0])) {
923 return i;
926 /* no match. Retry from start, if previous started from a selected entry */
927 if (s!=0) {
928 s = 0;
929 goto again;
931 return -1;
935 static int
936 keyboardMenu(WMenu *menu)
938 XEvent event;
939 KeySym ksym=NoSymbol;
940 int done=0;
941 int index;
942 WMenuEntry *entry;
943 int old_pos_x = menu->frame_x;
944 int old_pos_y = menu->frame_y;
945 int new_x = old_pos_x, new_y = old_pos_y;
946 int scr_width = menu->frame->screen_ptr->scr_width;
947 int scr_height = menu->frame->screen_ptr->scr_height;
949 if (menu->flags.editing)
950 return False;
953 XGrabKeyboard(dpy, menu->frame->core->window, True, GrabModeAsync,
954 GrabModeAsync, CurrentTime);
956 if (menu->frame_y+menu->frame->top_width >= scr_height)
957 new_y = scr_height - menu->frame->top_width;
959 if (menu->frame_x+MENUW(menu) >= scr_width)
960 new_x = scr_width-MENUW(menu)-1;
962 move_menus(menu, new_x, new_y);
964 while (!done && menu->flags.mapped) {
965 XAllowEvents(dpy, AsyncKeyboard, CurrentTime);
966 WMMaskEvent(dpy, ExposureMask|ButtonMotionMask|ButtonPressMask
967 |ButtonReleaseMask|KeyPressMask|KeyReleaseMask
968 |SubstructureNotifyMask, &event);
970 switch (event.type) {
971 case KeyPress:
972 ksym = XLookupKeysym(&event.xkey, 0);
973 switch (ksym) {
974 case XK_Escape:
975 done = 1;
976 break;
978 case XK_Home:
979 #ifdef XK_KP_Home
980 case XK_KP_Home:
981 #endif
982 selectEntry(menu, 0);
983 makeVisible(menu);
984 break;
986 case XK_End:
987 #ifdef XK_KP_End
988 case XK_KP_End:
989 #endif
990 selectEntry(menu, menu->entry_no-1);
991 makeVisible(menu);
992 break;
994 case XK_Up:
995 #ifdef ARROWLESS_KBD
996 case XK_k:
997 #endif
998 #ifdef XK_KP_Up
999 case XK_KP_Up:
1000 #endif
1001 if (menu->selected_entry <= 0)
1002 selectEntry(menu, menu->entry_no-1);
1003 else
1004 selectEntry(menu, menu->selected_entry-1);
1005 makeVisible(menu);
1006 break;
1008 case XK_Down:
1009 #ifdef ARROWLESS_KBD
1010 case XK_j:
1011 #endif
1012 #ifdef XK_KP_Down
1013 case XK_KP_Down:
1014 #endif
1015 if (menu->selected_entry<0)
1016 selectEntry(menu, 0);
1017 else if (menu->selected_entry == menu->entry_no-1)
1018 selectEntry(menu, 0);
1019 else if (menu->selected_entry < menu->entry_no-1)
1020 selectEntry(menu, menu->selected_entry+1);
1021 makeVisible(menu);
1022 break;
1024 case XK_Right:
1025 #ifdef ARROWLESS_KBD
1026 case XK_l:
1027 #endif
1028 #ifdef XK_KP_Right
1029 case XK_KP_Right:
1030 #endif
1031 if (menu->selected_entry>=0) {
1032 WMenuEntry *entry;
1033 entry = menu->entries[menu->selected_entry];
1035 if (entry->cascade >= 0 && menu->cascades
1036 && menu->cascades[entry->cascade]->entry_no > 0) {
1038 XUngrabKeyboard(dpy, CurrentTime);
1040 selectEntry(menu->cascades[entry->cascade], 0);
1041 if (!keyboardMenu(menu->cascades[entry->cascade]))
1042 done = 1;
1044 XGrabKeyboard(dpy, menu->frame->core->window, True,
1045 GrabModeAsync, GrabModeAsync,
1046 CurrentTime);
1049 break;
1051 case XK_Left:
1052 #ifdef ARROWLESS_KBD
1053 case XK_h:
1054 #endif
1055 #ifdef XK_KP_Left
1056 case XK_KP_Left:
1057 #endif
1058 if (menu->parent!=NULL && menu->parent->selected_entry>=0) {
1059 selectEntry(menu, -1);
1060 move_menus(menu, old_pos_x, old_pos_y);
1061 return True;
1063 break;
1065 case XK_Return:
1066 done = 2;
1067 break;
1069 default:
1070 index = check_key(menu, &event.xkey);
1071 if (index>=0) {
1072 selectEntry(menu, index);
1075 break;
1077 default:
1078 if (event.type==ButtonPress)
1079 done = 1;
1081 WMHandleEvent(&event);
1085 XUngrabKeyboard(dpy, CurrentTime);
1087 if (done==2 && menu->selected_entry>=0) {
1088 entry = menu->entries[menu->selected_entry];
1089 } else {
1090 entry = NULL;
1093 if (entry && entry->callback!=NULL && entry->flags.enabled
1094 && entry->cascade < 0) {
1095 #if (MENU_BLINK_COUNT > 0)
1096 int sel = menu->selected_entry;
1097 int i;
1099 for (i=0; i<MENU_BLINK_COUNT; i++) {
1100 paintEntry(menu, sel, False);
1101 XSync(dpy, 0);
1102 wusleep(MENU_BLINK_DELAY);
1103 paintEntry(menu, sel, True);
1104 XSync(dpy, 0);
1105 wusleep(MENU_BLINK_DELAY);
1107 #endif
1108 selectEntry(menu, -1);
1110 if (!menu->flags.buttoned) {
1111 wMenuUnmap(menu);
1112 move_menus(menu, old_pos_x, old_pos_y);
1114 closeCascade(menu);
1116 (*entry->callback)(menu, entry);
1117 } else {
1118 if (!menu->flags.buttoned) {
1119 wMenuUnmap(menu);
1120 move_menus(menu, old_pos_x, old_pos_y);
1122 selectEntry(menu, -1);
1126 /* returns True if returning from a submenu to a parent menu,
1127 * False if exiting from menu */
1128 return False;
1132 void
1133 wMenuMapAt(WMenu *menu, int x, int y, int keyboard)
1135 int scr_width = menu->frame->screen_ptr->scr_width;
1136 int scr_height = menu->frame->screen_ptr->scr_height;
1138 if (!menu->flags.realized) {
1139 menu->flags.realized=1;
1140 wMenuRealize(menu);
1142 if (!menu->flags.mapped) {
1143 if (wPreferences.wrap_menus) {
1144 if (x<0) x = 0;
1145 if (y<0) y = 0;
1146 if (x+MENUW(menu) > scr_width)
1147 x = scr_width - MENUW(menu);
1148 if (y+MENUH(menu) > scr_height)
1149 y = scr_height - MENUH(menu);
1152 XMoveWindow(dpy, menu->frame->core->window, x, y);
1153 menu->frame_x = x;
1154 menu->frame_y = y;
1155 XMapWindow(dpy, menu->frame->core->window);
1156 wRaiseFrame(menu->frame->core);
1157 menu->flags.mapped = 1;
1158 } else {
1159 selectEntry(menu, 0);
1162 if (keyboard)
1163 keyboardMenu(menu);
1167 void
1168 wMenuMap(WMenu *menu)
1170 if (!menu->flags.realized) {
1171 menu->flags.realized=1;
1172 wMenuRealize(menu);
1174 if (menu->flags.app_menu && menu->parent==NULL) {
1175 menu->frame_x = menu->frame->screen_ptr->app_menu_x;
1176 menu->frame_y = menu->frame->screen_ptr->app_menu_y;
1177 XMoveWindow(dpy, menu->frame->core->window, menu->frame_x, menu->frame_y);
1179 XMapWindow(dpy, menu->frame->core->window);
1180 wRaiseFrame(menu->frame->core);
1181 menu->flags.mapped = 1;
1185 void
1186 wMenuUnmap(WMenu *menu)
1188 int i;
1190 XUnmapWindow(dpy, menu->frame->core->window);
1191 if (menu->flags.titled && menu->flags.buttoned) {
1192 wFrameWindowHideButton(menu->frame, WFF_RIGHT_BUTTON);
1194 menu->flags.buttoned = 0;
1195 menu->flags.mapped = 0;
1196 menu->flags.open_to_left = 0;
1198 for (i=0; i<menu->cascade_no; i++) {
1199 if (menu->cascades[i]!=NULL
1200 && menu->cascades[i]->flags.mapped
1201 && !menu->cascades[i]->flags.buttoned) {
1203 wMenuUnmap(menu->cascades[i]);
1206 menu->selected_entry = -1;
1211 void
1212 wMenuPaint(WMenu *menu)
1214 int i;
1216 if (!menu->flags.mapped) {
1217 return;
1220 /* paint entries */
1221 for (i=0; i<menu->entry_no; i++) {
1222 paintEntry(menu, i, i==menu->selected_entry);
1227 void
1228 wMenuSetEnabled(WMenu *menu, int index, int enable)
1230 if (index>=menu->entry_no) return;
1231 menu->entries[index]->flags.enabled=enable;
1232 paintEntry(menu, index, index==menu->selected_entry);
1233 paintEntry(menu->brother, index, index==menu->selected_entry);
1237 /* ====================================================================== */
1240 static void
1241 editEntry(WMenu *menu, WMenuEntry *entry)
1243 WTextInput *text;
1244 XEvent event;
1245 WObjDescriptor *desc;
1246 char *t;
1247 int done = 0;
1248 Window old_focus;
1249 int old_revert;
1251 menu->flags.editing = 1;
1253 text = wTextCreate(menu->menu, 1, menu->entry_height * entry->order,
1254 menu->menu->width - 2, menu->entry_height - 1);
1256 wTextPutText(text, entry->text);
1257 XGetInputFocus(dpy, &old_focus, &old_revert);
1258 XSetInputFocus(dpy, text->core->window, RevertToNone, CurrentTime);
1260 if (XGrabKeyboard(dpy, text->core->window, True, GrabModeAsync,
1261 GrabModeAsync, CurrentTime)!=GrabSuccess) {
1262 wwarning(_("could not grab keyboard"));
1263 wTextDestroy(text);
1265 wSetFocusTo(menu->frame->screen_ptr,
1266 menu->frame->screen_ptr->focused_window);
1267 return;
1271 while (!done && !text->done) {
1272 XSync(dpy, 0);
1273 XAllowEvents(dpy, AsyncKeyboard|AsyncPointer, CurrentTime);
1274 XSync(dpy, 0);
1275 WMNextEvent(dpy, &event);
1277 if (XFindContext(dpy, event.xany.window, wWinContext,
1278 (XPointer *)&desc)==XCNOENT)
1279 desc = NULL;
1281 if ((desc != NULL) && (desc->handle_anything != NULL)) {
1283 (*desc->handle_anything)(desc, &event);
1285 } else {
1286 switch (event.type) {
1287 case ButtonPress:
1288 XAllowEvents(dpy, ReplayPointer, CurrentTime);
1289 done = 1;
1291 default:
1292 WMHandleEvent(&event);
1293 break;
1298 XSetInputFocus(dpy, old_focus, old_revert, CurrentTime);
1300 wSetFocusTo(menu->frame->screen_ptr,
1301 menu->frame->screen_ptr->focused_window);
1304 t = wTextGetText(text);
1305 /* if !t, the user has canceled editing */
1306 if (t) {
1307 if (entry->text)
1308 wfree(entry->text);
1309 entry->text = wstrdup(t);
1311 menu->flags.realized = 0;
1313 wTextDestroy(text);
1315 XUngrabKeyboard(dpy, CurrentTime);
1317 if (t && menu->on_edit)
1318 (*menu->on_edit)(menu, entry);
1320 menu->flags.editing = 0;
1322 if (!menu->flags.realized)
1323 wMenuRealize(menu);
1327 static void
1328 selectEntry(WMenu *menu, int entry_no)
1330 WMenuEntry *entry;
1331 WMenu *submenu;
1332 int old_entry;
1334 if (menu->entries==NULL)
1335 return;
1337 if (entry_no >= menu->entry_no)
1338 return;
1340 old_entry = menu->selected_entry;
1341 menu->selected_entry = entry_no;
1343 if (old_entry!=entry_no) {
1345 /* unselect previous entry */
1346 if (old_entry>=0) {
1347 paintEntry(menu, old_entry, False);
1348 entry = menu->entries[old_entry];
1350 /* unmap cascade */
1351 if (entry->cascade>=0 && menu->cascades) {
1352 if (!menu->cascades[entry->cascade]->flags.buttoned) {
1353 wMenuUnmap(menu->cascades[entry->cascade]);
1358 if (entry_no<0) {
1359 menu->selected_entry = -1;
1360 return;
1362 entry = menu->entries[entry_no];
1364 if (entry->cascade>=0 && menu->cascades && entry->flags.enabled) {
1365 /* Callback for when the submenu is opened.
1367 submenu = menu->cascades[entry->cascade];
1368 if (submenu && submenu->flags.brother)
1369 submenu = submenu->brother;
1371 if (entry->callback) {
1372 /* Only call the callback if the submenu is not yet mapped.
1374 if (menu->flags.brother) {
1375 if (!submenu || !submenu->flags.mapped)
1376 (*entry->callback)(menu->brother, entry);
1377 } else {
1378 if (!submenu || !submenu->flags.buttoned)
1379 (*entry->callback)(menu, entry);
1383 /* the submenu menu might have changed */
1384 submenu = menu->cascades[entry->cascade];
1386 /* map cascade */
1387 if (!submenu->flags.mapped) {
1388 int x, y;
1390 if (!submenu->flags.realized)
1391 wMenuRealize(submenu);
1392 if (wPreferences.wrap_menus) {
1393 if (menu->flags.open_to_left)
1394 submenu->flags.open_to_left = 1;
1396 if (submenu->flags.open_to_left) {
1397 x = menu->frame_x - MENUW(submenu);
1398 if (x<0) {
1399 x = 0;
1400 submenu->flags.open_to_left = 0;
1402 } else {
1403 x = menu->frame_x + MENUW(menu);
1405 if (x + MENUW(submenu)
1406 >= menu->frame->screen_ptr->scr_width) {
1408 x = menu->frame_x - MENUW(submenu);
1409 submenu->flags.open_to_left = 1;
1412 } else {
1413 x = menu->frame_x + MENUW(menu);
1416 if (wPreferences.align_menus) {
1417 y = menu->frame_y;
1418 } else {
1419 y = menu->frame_y + menu->entry_height*entry_no;
1420 if (menu->flags.titled)
1421 y += menu->frame->top_width;
1422 if (menu->cascades[entry->cascade]->flags.titled)
1423 y -= menu->cascades[entry->cascade]->frame->top_width;
1426 wMenuMapAt(menu->cascades[entry->cascade], x, y, False);
1427 menu->cascades[entry->cascade]->parent = menu;
1428 } else {
1429 return;
1432 paintEntry(menu, entry_no, True);
1437 static WMenu*
1438 findMenu(WScreen *scr, int *x_ret, int *y_ret)
1440 WMenu *menu;
1441 WObjDescriptor *desc;
1442 Window root_ret, win, junk_win;
1443 int x, y, wx, wy;
1444 unsigned int mask;
1446 XQueryPointer(dpy, scr->root_win, &root_ret, &win, &x, &y, &wx, &wy,
1447 &mask);
1449 if (win==None) return NULL;
1451 if (XFindContext(dpy, win, wWinContext, (XPointer *)&desc)==XCNOENT)
1452 return NULL;
1454 if (desc->parent_type == WCLASS_MENU) {
1455 menu = (WMenu*)desc->parent;
1456 XTranslateCoordinates(dpy, root_ret, menu->menu->window, wx, wy,
1457 x_ret, y_ret, &junk_win);
1458 return menu;
1460 return NULL;
1466 static void
1467 closeCascade(WMenu *menu)
1469 WMenu *parent=menu->parent;
1471 if (menu->flags.brother
1472 || (!menu->flags.buttoned
1473 && (!menu->flags.app_menu||menu->parent!=NULL))) {
1475 selectEntry(menu, -1);
1476 XSync(dpy, 0);
1477 #if (MENU_BLINK_DELAY > 2)
1478 wusleep(MENU_BLINK_DELAY/2);
1479 #endif
1480 wMenuUnmap(menu);
1481 while (parent!=NULL
1482 && (parent->parent!=NULL || !parent->flags.app_menu
1483 || parent->flags.brother)
1484 && !parent->flags.buttoned) {
1485 selectEntry(parent, -1);
1486 wMenuUnmap(parent);
1487 parent = parent->parent;
1489 if (parent)
1490 selectEntry(parent, -1);
1495 static void
1496 closeBrotherCascadesOf(WMenu *menu)
1498 WMenu *tmp;
1499 int i;
1501 for (i=0; i<menu->cascade_no; i++) {
1502 if (menu->cascades[i]->flags.brother) {
1503 tmp = menu->cascades[i];
1504 } else {
1505 tmp = menu->cascades[i]->brother;
1507 if (tmp->flags.mapped) {
1508 selectEntry(tmp->parent, -1);
1509 closeBrotherCascadesOf(tmp);
1510 break;
1516 #define getEntryAt(menu, x, y) ((y)<0 ? -1 : (y)/(menu->entry_height))
1519 static WMenu*
1520 parentMenu(WMenu *menu)
1522 WMenu *parent;
1523 WMenuEntry *entry;
1525 if (menu->flags.buttoned)
1526 return menu;
1528 while (menu->parent && menu->parent->flags.mapped) {
1529 parent = menu->parent;
1530 if (parent->selected_entry < 0)
1531 break;
1532 entry = parent->entries[parent->selected_entry];
1533 if (!entry->flags.enabled || entry->cascade<0 || !parent->cascades ||
1534 parent->cascades[entry->cascade] != menu)
1535 break;
1536 menu = parent;
1537 if (menu->flags.buttoned)
1538 break;
1541 return menu;
1547 * Will raise the passed menu, if submenu = 0
1548 * If submenu > 0 will also raise all mapped submenus
1549 * until the first buttoned one
1550 * If submenu < 0 will also raise all mapped parent menus
1551 * until the first buttoned one
1554 static void
1555 raiseMenus(WMenu *menu, int submenus)
1557 WMenu *submenu;
1558 int i;
1560 if(!menu) return;
1562 wRaiseFrame(menu->frame->core);
1564 if (submenus>0 && menu->selected_entry>=0) {
1565 i = menu->entries[menu->selected_entry]->cascade;
1566 if (i>=0 && menu->cascades) {
1567 submenu = menu->cascades[i];
1568 if (submenu->flags.mapped && !submenu->flags.buttoned)
1569 raiseMenus(submenu, submenus);
1572 if (submenus<0 && !menu->flags.buttoned &&
1573 menu->parent && menu->parent->flags.mapped)
1574 raiseMenus(menu->parent, submenus);
1578 WMenu*
1579 wMenuUnderPointer(WScreen *screen)
1581 WObjDescriptor *desc;
1582 Window root_ret, win;
1583 int dummy;
1584 unsigned int mask;
1586 XQueryPointer(dpy, screen->root_win, &root_ret, &win, &dummy, &dummy,
1587 &dummy, &dummy, &mask);
1589 if (win==None) return NULL;
1591 if (XFindContext(dpy, win, wWinContext, (XPointer *)&desc)==XCNOENT)
1592 return NULL;
1594 if (desc->parent_type == WCLASS_MENU)
1595 return (WMenu *)desc->parent;
1596 return NULL;
1602 static void
1603 getPointerPosition(WScreen *scr, int *x, int *y)
1605 Window root_ret, win;
1606 int wx, wy;
1607 unsigned int mask;
1609 XQueryPointer(dpy, scr->root_win, &root_ret, &win, x, y, &wx, &wy, &mask);
1613 static void
1614 getScrollAmount(WMenu *menu, int *hamount, int *vamount)
1616 WScreen *scr = menu->menu->screen_ptr;
1617 int menuX1 = menu->frame_x;
1618 int menuY1 = menu->frame_y;
1619 int menuX2 = menu->frame_x + MENUW(menu);
1620 int menuY2 = menu->frame_y + MENUH(menu);
1621 int screenW = scr->scr_width;
1622 int screenH = scr->scr_height;
1623 int xroot, yroot;
1625 *hamount = 0;
1626 *vamount = 0;
1628 getPointerPosition(scr, &xroot, &yroot);
1631 if (xroot <= 1 && menuX1 < 0) {
1632 /* scroll to the right */
1633 *hamount = WMIN(MENU_SCROLL_STEP, abs(menuX1));
1635 } else if (xroot >= screenW-2 && menuX2 > screenW-1) {
1636 /* scroll to the left */
1637 *hamount = WMIN(MENU_SCROLL_STEP, abs(menuX2-screenW-1));
1639 if (*hamount==0)
1640 *hamount = 1;
1642 *hamount = -*hamount;
1645 if (yroot <= 1 && menuY1 < 0) {
1646 /* scroll down */
1647 *vamount = WMIN(MENU_SCROLL_STEP, abs(menuY1));
1649 } else if (yroot >= screenH-2 && menuY2 > screenH-1) {
1650 /* scroll up */
1651 *vamount = WMIN(MENU_SCROLL_STEP, abs(menuY2-screenH-2));
1653 *vamount = -*vamount;
1658 static void
1659 dragScrollMenuCallback(void *data)
1661 WMenu *menu = (WMenu*)data;
1662 WScreen *scr = menu->menu->screen_ptr;
1663 WMenu *parent = parentMenu(menu);
1664 int hamount, vamount;
1665 int x, y;
1666 int newSelectedEntry;
1668 getScrollAmount(menu, &hamount, &vamount);
1671 if (hamount != 0 || vamount != 0) {
1672 wMenuMove(parent, parent->frame_x + hamount,
1673 parent->frame_y + vamount, True);
1674 if (findMenu(scr, &x, &y)) {
1675 newSelectedEntry = getEntryAt(menu, x, y);
1676 selectEntry(menu, newSelectedEntry);
1677 } else {
1678 /* Pointer fell outside of menu. If the selected entry is
1679 * not a submenu, unselect it */
1680 if (menu->selected_entry >= 0
1681 && menu->entries[menu->selected_entry]->cascade<0)
1682 selectEntry(menu, -1);
1683 newSelectedEntry = 0;
1686 /* paranoid check */
1687 if (newSelectedEntry >= 0) {
1688 /* keep scrolling */
1689 menu->timer = WMAddTimerHandler(MENU_SCROLL_DELAY,
1690 dragScrollMenuCallback, menu);
1691 } else {
1692 menu->timer = NULL;
1694 } else {
1695 /* don't need to scroll anymore */
1696 menu->timer = NULL;
1697 if (findMenu(scr, &x, &y)) {
1698 newSelectedEntry = getEntryAt(menu, x, y);
1699 selectEntry(menu, newSelectedEntry);
1705 static void
1706 scrollMenuCallback(void *data)
1708 WMenu *menu = (WMenu*)data;
1709 WMenu *parent = parentMenu(menu);
1710 int hamount = 0; /* amount to scroll */
1711 int vamount = 0;
1713 #ifdef VIRTUAL_DESKTOP
1714 /* don't scroll if it is in vdesk mode */
1715 if (!wPreferences.vedge_thickness)
1716 #endif
1717 getScrollAmount(menu, &hamount, &vamount);
1719 if (hamount != 0 || vamount != 0) {
1720 wMenuMove(parent, parent->frame_x + hamount,
1721 parent->frame_y + vamount, True);
1723 /* keep scrolling */
1724 menu->timer = WMAddTimerHandler(MENU_SCROLL_DELAY,
1725 scrollMenuCallback, menu);
1726 } else {
1727 /* don't need to scroll anymore */
1728 menu->timer = NULL;
1734 #define MENU_SCROLL_BORDER 5
1736 static int
1737 isPointNearBoder(WMenu *menu, int x, int y)
1739 int menuX1 = menu->frame_x;
1740 int menuY1 = menu->frame_y;
1741 int menuX2 = menu->frame_x + MENUW(menu);
1742 int menuY2 = menu->frame_y + MENUH(menu);
1743 int scrXe = menu->menu->screen_ptr->scr_width-1;
1744 int scrYe = menu->menu->screen_ptr->scr_height-1;
1745 int flag = 0;
1747 if (x >= menuX1 && x <= menuX2 && (y < MENU_SCROLL_BORDER
1748 || y > scrYe-MENU_SCROLL_BORDER))
1749 flag = 1;
1750 else if (y >= menuY1 && y <= menuY2 && (x < MENU_SCROLL_BORDER
1751 || x > scrXe-MENU_SCROLL_BORDER))
1752 flag = 1;
1754 return flag;
1758 typedef struct _delay {
1759 WWindow *wwin;
1760 WMenu *menu;
1761 int ox,oy;
1762 } _delay;
1765 static void
1766 _leaving(_delay *dl)
1768 wMenuMove(dl->menu, dl->ox, dl->oy, True);
1769 dl->menu->jump_back=NULL;
1770 dl->menu->menu->screen_ptr->flags.jump_back_pending = 0;
1771 wfree(dl);
1775 void
1776 wMenuScroll(WMenu *menu, XEvent *event)
1778 WMenu *smenu;
1779 WMenu *omenu = parentMenu(menu);
1780 WScreen *scr = menu->frame->screen_ptr;
1781 int done = 0;
1782 int jump_back = 0;
1783 int old_frame_x = omenu->frame_x;
1784 int old_frame_y = omenu->frame_y;
1785 XEvent ev;
1787 if (omenu->jump_back)
1788 WMDeleteTimerWithClientData(omenu->jump_back);
1791 if ((/*omenu->flags.buttoned &&*/ !wPreferences.wrap_menus)
1792 || omenu->flags.app_menu) {
1793 jump_back = 1;
1796 if (!wPreferences.wrap_menus)
1797 raiseMenus(omenu, True);
1798 else
1799 raiseMenus(menu, False);
1801 if (!menu->timer)
1802 scrollMenuCallback(menu);
1804 while(!done) {
1805 int x, y, on_border, on_x_edge, on_y_edge, on_title;
1807 WMNextEvent(dpy, &ev);
1808 switch (ev.type) {
1809 case EnterNotify:
1810 WMHandleEvent(&ev);
1811 case MotionNotify:
1812 x = (ev.type==MotionNotify) ? ev.xmotion.x_root : ev.xcrossing.x_root;
1813 y = (ev.type==MotionNotify) ? ev.xmotion.y_root : ev.xcrossing.y_root;
1815 /* on_border is != 0 if the pointer is between the menu
1816 * and the screen border and is close enough to the border */
1817 on_border = isPointNearBoder(menu, x, y);
1819 smenu = wMenuUnderPointer(scr);
1821 if ((smenu==NULL && !on_border) || (smenu && parentMenu(smenu)!=omenu)) {
1822 done = 1;
1823 break;
1826 on_x_edge = x <= 1 || x >= scr->scr_width - 2;
1827 on_y_edge = y <= 1 || y >= scr->scr_height - 2;
1828 on_border = on_x_edge || on_y_edge;
1830 if (!on_border && !jump_back) {
1831 done = 1;
1832 break;
1835 if (menu->timer && (smenu!=menu || (!on_y_edge && !on_x_edge))) {
1836 WMDeleteTimerHandler(menu->timer);
1837 menu->timer = NULL;
1840 if (smenu != NULL)
1841 menu = smenu;
1843 if (!menu->timer)
1844 scrollMenuCallback(menu);
1845 break;
1846 case ButtonPress:
1847 /* True if we push on title, or drag the omenu to other position */
1848 on_title = ev.xbutton.x_root >= omenu->frame_x &&
1849 ev.xbutton.x_root <= omenu->frame_x + MENUW(omenu) &&
1850 ev.xbutton.y_root >= omenu->frame_y &&
1851 ev.xbutton.y_root <= omenu->frame_y + omenu->frame->top_width;
1852 WMHandleEvent(&ev);
1853 smenu = wMenuUnderPointer(scr);
1854 if (smenu == NULL || (smenu && smenu->flags.buttoned && smenu != omenu))
1855 done = 1;
1856 else if (smenu==omenu && on_title) {
1857 jump_back = 0;
1858 done = 1;
1860 break;
1861 case KeyPress:
1862 done = 1;
1863 default:
1864 WMHandleEvent(&ev);
1865 break;
1869 if (menu->timer) {
1870 WMDeleteTimerHandler(menu->timer);
1871 menu->timer = NULL;
1874 if (jump_back) {
1875 _delay *delayer;
1876 if (!omenu->jump_back) {
1877 delayer=wmalloc(sizeof(_delay));
1878 delayer->menu=omenu;
1879 delayer->ox=old_frame_x;
1880 delayer->oy=old_frame_y;
1881 omenu->jump_back=delayer;
1882 scr->flags.jump_back_pending = 1;
1884 else delayer = omenu->jump_back;
1885 WMAddTimerHandler(MENU_JUMP_BACK_DELAY,(WMCallback*)_leaving, delayer);
1891 static void
1892 menuExpose(WObjDescriptor *desc, XEvent *event)
1894 wMenuPaint(desc->parent);
1897 typedef struct {
1898 int *delayed_select;
1899 WMenu *menu;
1900 WMHandlerID magic;
1901 } delay_data;
1904 static void
1905 delaySelection(void *data)
1907 delay_data *d = (delay_data*)data;
1908 int x, y, entry_no;
1909 WMenu *menu;
1911 d->magic = NULL;
1913 menu = findMenu(d->menu->menu->screen_ptr, &x, &y);
1914 if (menu && (d->menu == menu || d->delayed_select)) {
1915 entry_no = getEntryAt(menu, x, y);
1916 selectEntry(menu, entry_no);
1918 if (d->delayed_select)
1919 *(d->delayed_select) = 0;
1923 static void
1924 menuMouseDown(WObjDescriptor *desc, XEvent *event)
1926 XButtonEvent *bev = &event->xbutton;
1927 WMenu *menu = desc->parent;
1928 WMenu *smenu;
1929 WScreen *scr=menu->frame->screen_ptr;
1930 WMenuEntry *entry=NULL;
1931 XEvent ev;
1932 int close_on_exit=0;
1933 int done=0;
1934 int delayed_select = 0;
1935 int entry_no;
1936 int x, y;
1937 int prevx, prevy;
1938 int old_frame_x = 0;
1939 int old_frame_y = 0;
1940 delay_data d_data = {NULL, NULL, NULL};
1942 if (!wPreferences.wrap_menus) {
1943 smenu = parentMenu(menu);
1944 old_frame_x = smenu->frame_x;
1945 old_frame_y = smenu->frame_y;
1946 } else if (event->xbutton.window == menu->frame->core->window) {
1947 /* This is true if the menu was launched with right click on root window */
1948 delayed_select = 1;
1949 d_data.delayed_select = &delayed_select;
1950 d_data.menu = menu;
1951 d_data.magic = WMAddTimerHandler(wPreferences.dblclick_time,
1952 delaySelection, &d_data);
1955 if (menu->flags.inside_handler) {
1956 return;
1958 menu->flags.inside_handler = 1;
1960 wRaiseFrame(menu->frame->core);
1962 close_on_exit = (bev->send_event || menu->flags.brother);
1964 smenu = findMenu(scr, &x, &y);
1965 if (!smenu) {
1966 x = -1;
1967 y = -1;
1968 } else {
1969 menu = smenu;
1972 if (menu->flags.editing) {
1973 goto byebye;
1975 entry_no = getEntryAt(menu, x, y);
1976 if (entry_no>=0) {
1977 entry = menu->entries[entry_no];
1979 if (!close_on_exit && (bev->state & ControlMask) && smenu
1980 && entry->flags.editable) {
1981 editEntry(smenu, entry);
1982 goto byebye;
1983 } else if (bev->state & ControlMask) {
1984 goto byebye;
1987 if (entry->flags.enabled && entry->cascade>=0 && menu->cascades) {
1988 WMenu *submenu = menu->cascades[entry->cascade];
1989 /* map cascade */
1990 if (submenu->flags.mapped && !submenu->flags.buttoned &&
1991 menu->selected_entry!=entry_no) {
1992 wMenuUnmap(submenu);
1994 if (!submenu->flags.mapped && !delayed_select) {
1995 selectEntry(menu, entry_no);
1996 } else if (!submenu->flags.buttoned) {
1997 selectEntry(menu, -1);
2000 } else if (!delayed_select) {
2001 selectEntry(menu, entry_no);
2004 if (!wPreferences.wrap_menus && !wPreferences.scrollable_menus) {
2005 if (!menu->timer)
2006 dragScrollMenuCallback(menu);
2010 #ifdef VIRTUAL_DESKTOP
2011 if (wPreferences.vedge_thickness) {
2012 wWorkspaceLowerEdge(scr);
2014 #endif
2016 prevx = bev->x_root;
2017 prevy = bev->y_root;
2018 while (!done) {
2019 int x, y;
2021 XAllowEvents(dpy, AsyncPointer|SyncPointer, CurrentTime);
2023 WMMaskEvent(dpy, ExposureMask|ButtonMotionMask|ButtonReleaseMask
2024 |ButtonPressMask, &ev);
2025 switch (ev.type) {
2026 case MotionNotify:
2027 smenu = findMenu(scr, &x, &y);
2029 if (smenu == NULL) {
2030 /* moved mouse out of menu */
2032 if (!delayed_select && d_data.magic) {
2033 WMDeleteTimerHandler(d_data.magic);
2034 d_data.magic = NULL;
2036 if (menu==NULL
2037 || (menu->selected_entry>=0
2038 && menu->entries[menu->selected_entry]->cascade>=0)) {
2039 prevx = ev.xmotion.x_root;
2040 prevy = ev.xmotion.y_root;
2042 break;
2044 selectEntry(menu, -1);
2045 menu = smenu;
2046 prevx = ev.xmotion.x_root;
2047 prevy = ev.xmotion.y_root;
2048 break;
2049 } else if (menu && menu!=smenu
2050 && (menu->selected_entry<0
2051 || menu->entries[menu->selected_entry]->cascade<0)) {
2052 selectEntry(menu, -1);
2054 if (!delayed_select && d_data.magic) {
2055 WMDeleteTimerHandler(d_data.magic);
2056 d_data.magic = NULL;
2058 } else {
2060 /* hysteresis for item selection */
2062 /* check if the motion was to the side, indicating that
2063 * the user may want to cross to a submenu */
2064 if (!delayed_select && menu) {
2065 int dx;
2066 Bool moved_to_submenu;/* moved to direction of submenu */
2068 dx = abs(prevx - ev.xmotion.x_root);
2070 moved_to_submenu = False;
2071 if (dx > 0 /* if moved enough to the side */
2072 /* maybe a open submenu */
2073 && menu->selected_entry>=0
2074 /* moving to the right direction */
2075 && (wPreferences.align_menus
2076 || ev.xmotion.y_root >= prevy)) {
2077 int index;
2079 index = menu->entries[menu->selected_entry]->cascade;
2080 if (index>=0) {
2081 if (menu->cascades[index]->frame_x>menu->frame_x) {
2082 if (prevx < ev.xmotion.x_root)
2083 moved_to_submenu = True;
2084 } else {
2085 if (prevx > ev.xmotion.x_root)
2086 moved_to_submenu = True;
2092 if (menu != smenu) {
2093 if (d_data.magic) {
2094 WMDeleteTimerHandler(d_data.magic);
2096 d_data.magic = NULL;
2097 } else if (moved_to_submenu) {
2098 /* while we are moving, postpone the selection */
2099 if (d_data.magic) {
2100 WMDeleteTimerHandler(d_data.magic);
2102 d_data.delayed_select = NULL;
2103 d_data.menu = menu;
2104 d_data.magic = WMAddTimerHandler(MENU_SELECT_DELAY,
2105 delaySelection,
2106 &d_data);
2107 prevx = ev.xmotion.x_root;
2108 prevy = ev.xmotion.y_root;
2109 break;
2110 } else {
2111 if (d_data.magic)
2112 WMDeleteTimerHandler(d_data.magic);
2113 d_data.magic = NULL;
2117 prevx = ev.xmotion.x_root;
2118 prevy = ev.xmotion.y_root;
2119 if (menu!=smenu) {
2120 /* pointer crossed menus */
2121 if (menu && menu->timer) {
2122 WMDeleteTimerHandler(menu->timer);
2123 menu->timer = NULL;
2125 if (smenu)
2126 dragScrollMenuCallback(smenu);
2128 menu = smenu;
2129 if (!menu->timer)
2130 dragScrollMenuCallback(menu);
2132 if (!delayed_select) {
2133 entry_no = getEntryAt(menu, x, y);
2134 if (entry_no>=0) {
2135 entry = menu->entries[entry_no];
2136 if (entry->flags.enabled && entry->cascade>=0 &&
2137 menu->cascades) {
2138 WMenu *submenu = menu->cascades[entry->cascade];
2139 if (submenu->flags.mapped && !submenu->flags.buttoned
2140 && menu->selected_entry!=entry_no) {
2141 wMenuUnmap(submenu);
2145 selectEntry(menu, entry_no);
2147 break;
2149 case ButtonPress:
2150 break;
2152 case ButtonRelease:
2153 if (ev.xbutton.button == event->xbutton.button)
2154 done=1;
2155 break;
2157 case Expose:
2158 WMHandleEvent(&ev);
2159 #ifdef VIRTUAL_DESKTOP
2160 /* since expose will raise edge up.. I need another ugly hack here */
2161 if (wPreferences.vedge_thickness) {
2162 wWorkspaceLowerEdge(scr);
2164 #endif
2165 break;
2169 if (menu && menu->timer) {
2170 WMDeleteTimerHandler(menu->timer);
2171 menu->timer = NULL;
2173 if (d_data.magic!=NULL)
2174 WMDeleteTimerHandler(d_data.magic);
2176 if (menu && menu->selected_entry>=0) {
2177 entry = menu->entries[menu->selected_entry];
2178 if (entry->callback!=NULL && entry->flags.enabled
2179 && entry->cascade < 0) {
2180 /* blink and erase menu selection */
2181 #if (MENU_BLINK_DELAY > 0)
2182 int sel = menu->selected_entry;
2183 int i;
2185 for (i=0; i<MENU_BLINK_COUNT; i++) {
2186 paintEntry(menu, sel, False);
2187 XSync(dpy, 0);
2188 wusleep(MENU_BLINK_DELAY);
2189 paintEntry(menu, sel, True);
2190 XSync(dpy, 0);
2191 wusleep(MENU_BLINK_DELAY);
2193 #endif
2194 /* unmap the menu, it's parents and call the callback */
2195 if (!menu->flags.buttoned &&
2196 (!menu->flags.app_menu||menu->parent!=NULL)) {
2197 closeCascade(menu);
2198 } else {
2199 selectEntry(menu, -1);
2201 (*entry->callback)(menu, entry);
2203 /* If the user double clicks an entry, the entry will
2204 * be executed twice, which is not good for things like
2205 * the root menu. So, ignore any clicks that were generated
2206 * while the entry was being executed */
2207 while (XCheckTypedWindowEvent(dpy, menu->menu->window,
2208 ButtonPress, &ev));
2209 } else if (entry->callback!=NULL && entry->cascade<0) {
2210 selectEntry(menu, -1);
2211 } else {
2212 if (entry->cascade>=0 && menu->cascades
2213 && menu->cascades[entry->cascade]->flags.brother) {
2214 selectEntry(menu, -1);
2219 if (((WMenu*)desc->parent)->flags.brother || close_on_exit || !smenu)
2220 closeCascade(desc->parent);
2222 /* close the cascade windows that should not remain opened */
2223 closeBrotherCascadesOf(desc->parent);
2225 if (!wPreferences.wrap_menus)
2226 wMenuMove(parentMenu(desc->parent), old_frame_x, old_frame_y, True);
2228 byebye:
2229 ((WMenu*)desc->parent)->flags.inside_handler = 0;
2230 #ifdef VIRTUAL_DESKTOP
2231 if (wPreferences.vedge_thickness) {
2232 wWorkspaceRaiseEdge(scr);
2234 #endif
2238 void
2239 wMenuMove(WMenu *menu, int x, int y, int submenus)
2241 WMenu *submenu;
2242 int i;
2244 if (!menu) return;
2246 menu->frame_x = x;
2247 menu->frame_y = y;
2248 XMoveWindow(dpy, menu->frame->core->window, x, y);
2250 if (submenus>0 && menu->selected_entry>=0) {
2251 i = menu->entries[menu->selected_entry]->cascade;
2253 if (i>=0 && menu->cascades) {
2254 submenu = menu->cascades[i];
2255 if (submenu->flags.mapped && !submenu->flags.buttoned) {
2256 if (wPreferences.align_menus) {
2257 wMenuMove(submenu, x + MENUW(menu), y, submenus);
2258 } else {
2259 wMenuMove(submenu, x+ MENUW(menu),
2260 y + submenu->entry_height*menu->selected_entry,
2261 submenus);
2266 if (submenus<0 && menu->parent!=NULL && menu->parent->flags.mapped &&
2267 !menu->parent->flags.buttoned) {
2268 if (wPreferences.align_menus) {
2269 wMenuMove(menu->parent, x - MENUW(menu->parent), y, submenus);
2270 } else {
2271 wMenuMove(menu->parent, x - MENUW(menu->parent), menu->frame_y
2272 - menu->parent->entry_height*menu->parent->selected_entry,
2273 submenus);
2279 static void
2280 changeMenuLevels(WMenu *menu, int lower)
2282 int i;
2284 if (!lower) {
2285 ChangeStackingLevel(menu->frame->core, (!menu->parent ? WMMainMenuLevel
2286 : WMSubmenuLevel));
2287 wRaiseFrame(menu->frame->core);
2288 menu->flags.lowered = 0;
2289 } else {
2290 ChangeStackingLevel(menu->frame->core, WMNormalLevel);
2291 wLowerFrame(menu->frame->core);
2292 menu->flags.lowered = 1;
2294 for (i=0; i<menu->cascade_no; i++) {
2295 if (menu->cascades[i]
2296 && !menu->cascades[i]->flags.buttoned
2297 && menu->cascades[i]->flags.lowered!=lower) {
2298 changeMenuLevels(menu->cascades[i], lower);
2305 static void
2306 menuTitleDoubleClick(WCoreWindow *sender, void *data, XEvent *event)
2308 WMenu *menu = data;
2309 int lower;
2311 if (event->xbutton.state & MOD_MASK) {
2312 if (menu->flags.lowered) {
2313 lower = 0;
2314 } else {
2315 lower = 1;
2317 changeMenuLevels(menu, lower);
2322 static void
2323 menuTitleMouseDown(WCoreWindow *sender, void *data, XEvent *event)
2325 WMenu *menu = data;
2326 WMenu *tmp;
2327 XEvent ev;
2328 int x=menu->frame_x, y=menu->frame_y;
2329 int dx=event->xbutton.x_root, dy=event->xbutton.y_root;
2330 int i, lower;
2331 Bool started;
2333 /* can't touch the menu copy */
2334 if (menu->flags.brother)
2335 return;
2337 if (event->xbutton.button != Button1 && event->xbutton.button != Button2)
2338 return;
2340 if (event->xbutton.state & MOD_MASK) {
2341 wLowerFrame(menu->frame->core);
2342 lower = 1;
2343 } else {
2344 wRaiseFrame(menu->frame->core);
2345 lower = 0;
2347 tmp = menu;
2349 /* lower/raise all submenus */
2350 while (1) {
2351 if (tmp->selected_entry>=0 && tmp->cascades
2352 && tmp->entries[tmp->selected_entry]->cascade>=0) {
2353 tmp = tmp->cascades[tmp->entries[tmp->selected_entry]->cascade];
2354 if (!tmp || !tmp->flags.mapped)
2355 break;
2356 if (lower)
2357 wLowerFrame(tmp->frame->core);
2358 else
2359 wRaiseFrame(tmp->frame->core);
2360 } else {
2361 break;
2365 /* tear off the menu if it's a root menu or a cascade
2366 application menu */
2367 if (!menu->flags.buttoned && !menu->flags.brother
2368 && (!menu->flags.app_menu||menu->parent!=NULL)) {
2369 menu->flags.buttoned=1;
2370 wFrameWindowShowButton(menu->frame, WFF_RIGHT_BUTTON);
2371 if (menu->parent) {
2372 /* turn off selected menu entry in parent menu */
2373 selectEntry(menu->parent, -1);
2375 /* make parent map the copy in place of the original */
2376 for (i=0; i<menu->parent->cascade_no; i++) {
2377 if (menu->parent->cascades[i] == menu) {
2378 menu->parent->cascades[i] = menu->brother;
2379 break;
2385 started = False;
2386 while(1) {
2387 WMMaskEvent(dpy, ButtonMotionMask|ButtonReleaseMask|ButtonPressMask
2388 |ExposureMask, &ev);
2389 switch (ev.type) {
2390 case MotionNotify:
2391 if (started) {
2392 x += ev.xmotion.x_root - dx;
2393 y += ev.xmotion.y_root - dy;
2394 dx = ev.xmotion.x_root;
2395 dy = ev.xmotion.y_root;
2396 wMenuMove(menu, x, y, True);
2397 } else {
2398 if (abs(ev.xmotion.x_root - dx) > MOVE_THRESHOLD
2399 || abs(ev.xmotion.y_root - dy) > MOVE_THRESHOLD) {
2400 started = True;
2401 XGrabPointer(dpy, menu->frame->titlebar->window, False,
2402 ButtonMotionMask|ButtonReleaseMask
2403 |ButtonPressMask,
2404 GrabModeAsync, GrabModeAsync, None,
2405 wCursor[WCUR_MOVE], CurrentTime);
2408 break;
2410 case ButtonPress:
2411 break;
2413 case ButtonRelease:
2414 if (ev.xbutton.button != event->xbutton.button)
2415 break;
2416 XUngrabPointer(dpy, CurrentTime);
2417 return;
2419 default:
2420 WMHandleEvent(&ev);
2421 break;
2427 *----------------------------------------------------------------------
2428 * menuCloseClick--
2429 * Handles mouse click on the close button of menus. The menu is
2430 * closed when the button is clicked.
2432 * Side effects:
2433 * The closed menu is reinserted at it's parent menus
2434 * cascade list.
2435 *----------------------------------------------------------------------
2437 static void
2438 menuCloseClick(WCoreWindow *sender, void *data, XEvent *event)
2440 WMenu *menu = (WMenu*)data;
2441 WMenu *parent = menu->parent;
2442 int i;
2444 if (parent) {
2445 for (i=0; i<parent->cascade_no; i++) {
2446 /* find the entry that points to the copy */
2447 if (parent->cascades[i] == menu->brother) {
2448 /* make it point to the original */
2449 parent->cascades[i] = menu;
2450 menu->parent = parent;
2451 break;
2455 wMenuUnmap(menu);
2459 static void
2460 saveMenuInfo(WMPropList *dict, WMenu *menu, WMPropList *key)
2462 WMPropList *value, *list;
2463 char buffer[256];
2465 snprintf(buffer, sizeof(buffer), "%i,%i", menu->frame_x, menu->frame_y);
2466 value = WMCreatePLString(buffer);
2467 list = WMCreatePLArray(value, NULL);
2468 if (menu->flags.lowered)
2469 WMAddToPLArray(list, WMCreatePLString("lowered"));
2470 WMPutInPLDictionary(dict, key, list);
2471 WMReleasePropList(value);
2472 WMReleasePropList(list);
2476 void
2477 wMenuSaveState(WScreen *scr)
2479 WMPropList *menus, *key;
2480 int save_menus = 0;
2482 menus = WMCreatePLDictionary(NULL, NULL, NULL);
2484 #ifndef LITE
2485 if (scr->switch_menu && scr->switch_menu->flags.buttoned) {
2486 key = WMCreatePLString("SwitchMenu");
2487 saveMenuInfo(menus, scr->switch_menu, key);
2488 WMReleasePropList(key);
2489 save_menus = 1;
2492 if (saveMenuRecurs(menus, scr, scr->root_menu))
2493 save_menus = 1;
2495 #endif /* !LITE */
2496 if (scr->workspace_menu && scr->workspace_menu->flags.buttoned) {
2497 key = WMCreatePLString("WorkspaceMenu");
2498 saveMenuInfo(menus, scr->workspace_menu, key);
2499 WMReleasePropList(key);
2500 save_menus = 1;
2503 if (save_menus) {
2504 key = WMCreatePLString("Menus");
2505 WMPutInPLDictionary(scr->session_state, key, menus);
2506 WMReleasePropList(key);
2508 WMReleasePropList(menus);
2512 #ifndef LITE
2514 static Bool
2515 getMenuPath(WMenu *menu, char *buffer, int bufSize)
2517 Bool ok = True;
2518 int len = 0;
2520 if (!menu->flags.titled || !menu->frame->title[0])
2521 return False;
2523 len = strlen(menu->frame->title);
2524 if (len >= bufSize)
2525 return False;
2527 if (menu->parent) {
2528 ok = getMenuPath(menu->parent, buffer, bufSize - len - 1);
2529 if (!ok)
2530 return False;
2533 strcat(buffer, "\\");
2534 strcat(buffer, menu->frame->title);
2536 return True;
2540 static Bool
2541 saveMenuRecurs(WMPropList *menus, WScreen *scr, WMenu *menu)
2543 WMPropList *key;
2544 int save_menus = 0, i;
2545 char buffer[512];
2546 Bool ok = True;
2549 if (menu->flags.brother)
2550 menu = menu->brother;
2552 if (menu->flags.buttoned && menu != scr->switch_menu) {
2554 buffer[0] = '\0';
2555 ok = getMenuPath(menu, buffer, 510);
2557 if (ok) {
2558 key = WMCreatePLString(buffer);
2559 saveMenuInfo(menus, menu, key);
2560 WMReleasePropList(key);
2561 save_menus = 1;
2565 if (ok) {
2566 for (i = 0; i < menu->cascade_no; i++) {
2567 if (saveMenuRecurs(menus, scr, menu->cascades[i]))
2568 save_menus = 1;
2571 return save_menus;
2573 #endif /* !LITE */
2576 #define COMPLAIN(key) wwarning(_("bad value in menus state info:%s"), key)
2579 static Bool
2580 getMenuInfo(WMPropList *info, int *x, int *y, Bool *lowered)
2582 WMPropList *pos;
2584 *lowered = False;
2586 if (WMIsPLArray(info)) {
2587 WMPropList *flags;
2588 pos = WMGetFromPLArray(info, 0);
2589 flags = WMGetFromPLArray(info, 1);
2590 if (flags != NULL && WMIsPLString(flags) && WMGetFromPLString(flags) != NULL
2591 && strcmp(WMGetFromPLString(flags), "lowered") == 0) {
2592 *lowered = True;
2594 } else {
2595 pos = info;
2598 if (pos != NULL && WMIsPLString(pos)) {
2599 if (sscanf(WMGetFromPLString(pos), "%i,%i", x, y)!=2)
2600 COMPLAIN("Position");
2601 } else {
2602 COMPLAIN("(position, flags...)");
2603 return False;
2606 return True;
2610 static int
2611 restoreMenu(WScreen *scr, WMPropList *menu, int which)
2613 int x, y;
2614 Bool lowered = False;
2615 WMenu *pmenu = NULL;
2617 if (!menu)
2618 return False;
2620 if (!getMenuInfo(menu, &x, &y, &lowered))
2621 return False;
2624 #ifndef LITE
2625 if (which & WSS_SWITCHMENU) {
2626 OpenSwitchMenu(scr, x, y, False);
2627 pmenu = scr->switch_menu;
2629 #endif /* !LITE */
2631 if (pmenu) {
2632 int width = MENUW(pmenu);
2633 int height = MENUH(pmenu);
2635 if (lowered) {
2636 changeMenuLevels(pmenu, True);
2639 x = (x < -width) ? 0 : x;
2640 x = (x > scr->scr_width) ? scr->scr_width - width : x;
2641 y = (y < 0) ? 0 : y;
2642 y = (y > scr->scr_height) ? scr->scr_height - height : y;
2643 wMenuMove(pmenu, x, y, True);
2644 pmenu->flags.buttoned = 1;
2645 wFrameWindowShowButton(pmenu->frame, WFF_RIGHT_BUTTON);
2646 return True;
2648 return False;
2652 #ifndef LITE
2653 static int
2654 restoreMenuRecurs(WScreen *scr, WMPropList *menus, WMenu *menu, char *path)
2656 WMPropList *key, *entry;
2657 char buffer[512];
2658 int i, x, y, res;
2659 Bool lowered;
2661 if (strlen(path) + strlen(menu->frame->title) > 510)
2662 return False;
2664 snprintf(buffer, sizeof(buffer), "%s\\%s", path, menu->frame->title);
2665 key = WMCreatePLString(buffer);
2666 entry = WMGetFromPLDictionary(menus, key);
2667 res = False;
2669 if (entry && getMenuInfo(entry, &x, &y, &lowered)) {
2671 if (!menu->flags.mapped) {
2672 int width = MENUW(menu);
2673 int height = MENUH(menu);
2675 wMenuMapAt(menu, x, y, False);
2677 if (menu->parent) {
2678 /* make parent map the copy in place of the original */
2679 for (i=0; i<menu->parent->cascade_no; i++) {
2680 if (menu->parent->cascades[i] == menu) {
2681 menu->parent->cascades[i] = menu->brother;
2682 break;
2686 if (lowered) {
2687 changeMenuLevels(menu, True);
2689 x = (x < -width) ? 0 : x;
2690 x = (x > scr->scr_width) ? scr->scr_width - width : x;
2691 y = (y < 0) ? 0 : y;
2692 y = (y > scr->scr_height) ? scr->scr_height - height : y;
2693 wMenuMove(menu, x, y, True);
2694 menu->flags.buttoned = 1;
2695 wFrameWindowShowButton(menu->frame, WFF_RIGHT_BUTTON);
2696 res = True;
2700 WMReleasePropList(key);
2702 for (i=0; i<menu->cascade_no; i++) {
2703 if (restoreMenuRecurs(scr, menus, menu->cascades[i], buffer) != False)
2704 res = True;
2707 return res;
2709 #endif /* !LITE */
2712 void
2713 wMenuRestoreState(WScreen *scr)
2715 WMPropList *menus, *menu, *key, *skey;
2717 key = WMCreatePLString("Menus");
2718 menus = WMGetFromPLDictionary(scr->session_state, key);
2719 WMReleasePropList(key);
2721 if (!menus)
2722 return;
2724 /* restore menus */
2726 skey = WMCreatePLString("SwitchMenu");
2727 menu = WMGetFromPLDictionary(menus, skey);
2728 WMReleasePropList(skey);
2729 restoreMenu(scr, menu, WSS_SWITCHMENU);
2731 #ifndef LITE
2732 if (!scr->root_menu) {
2733 OpenRootMenu(scr, scr->scr_width*2, 0, False);
2734 wMenuUnmap(scr->root_menu);
2736 restoreMenuRecurs(scr, menus, scr->root_menu, "");
2737 #endif /* !LITE */
2741 void
2742 OpenWorkspaceMenu(WScreen *scr, int x, int y)
2744 WMenu *menu, *parent;
2745 WMenuEntry *entry;
2747 #ifndef LITE
2748 if (!scr->root_menu) {
2749 OpenRootMenu(scr, scr->scr_width*2, 0, False);
2750 wMenuUnmap(scr->root_menu);
2752 #endif
2753 menu = scr->workspace_menu;
2754 if (menu) {
2755 if (menu->flags.mapped) {
2756 if (!menu->flags.buttoned) {
2757 wMenuUnmap(menu);
2758 parent = menu->parent;
2759 if (parent && parent->selected_entry >= 0) {
2760 entry = parent->entries[parent->selected_entry];
2761 if (parent->cascades[entry->cascade] == menu) {
2762 selectEntry(parent, -1);
2763 wMenuMapAt(menu, x, y, False);
2766 } else {
2767 wRaiseFrame(menu->frame->core);
2768 wMenuMapCopyAt(menu, x, y);
2770 } else {
2771 wMenuMapAt(menu, x, y, False);