- fixes for AA fonts
[wmaker-crm.git] / src / menu.c
blob54d679ba1d2b26b438c41e2a438df8eabf25022d
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"
46 #include <WINGs/WINGsP.h>
49 /****** Global Variables ******/
51 extern Cursor wCursor[WCUR_LAST];
53 extern XContext wWinContext;
55 extern WPreferences wPreferences;
57 #define MOD_MASK wPreferences.modifier_mask
59 #define MENU_SCROLL_STEP menuScrollParameters[(int)wPreferences.menu_scroll_speed].steps
60 #define MENU_SCROLL_DELAY menuScrollParameters[(int)wPreferences.menu_scroll_speed].delay
64 #define MENUW(m) ((m)->frame->core->width+2*FRAME_BORDER_WIDTH)
65 #define MENUH(m) ((m)->frame->core->height+2*FRAME_BORDER_WIDTH)
68 /***** Local Stuff ******/
70 static struct {
71 int steps;
72 int delay;
73 } menuScrollParameters[5] = {
74 {MENU_SCROLL_STEPS_UF, MENU_SCROLL_DELAY_UF},
75 {MENU_SCROLL_STEPS_F, MENU_SCROLL_DELAY_F},
76 {MENU_SCROLL_STEPS_M, MENU_SCROLL_DELAY_M},
77 {MENU_SCROLL_STEPS_S, MENU_SCROLL_DELAY_S},
78 {MENU_SCROLL_STEPS_US, MENU_SCROLL_DELAY_US}};
81 static void menuMouseDown(WObjDescriptor *desc, XEvent *event);
82 static void menuExpose(WObjDescriptor *desc, XEvent *event);
84 static void menuTitleDoubleClick(WCoreWindow *sender, void *data, XEvent *event);
85 static void menuTitleMouseDown(WCoreWindow *sender, void *data, XEvent *event);
87 static void menuCloseClick(WCoreWindow *sender, void *data, XEvent *event);
89 static void updateTexture(WMenu *menu);
91 #ifndef LITE
92 static int saveMenuRecurs(WMPropList *menus, WScreen *scr, WMenu *menu);
93 static int restoreMenuRecurs(WScreen *scr, WMPropList *menus, WMenu *menu, char *path);
94 #endif /* !LITE */
96 static void selectEntry(WMenu *menu, int entry_no);
97 static void closeCascade(WMenu *menu);
100 /****** Notification Observers ******/
102 static void
103 appearanceObserver(void *self, WMNotification *notif)
105 WMenu *menu = (WMenu*)self;
106 int flags = (int)WMGetNotificationClientData(notif);
108 if (!menu->flags.realized)
109 return;
111 if (WMGetNotificationName(notif) == WNMenuAppearanceSettingsChanged) {
112 if (flags & WFontSettings) {
113 menu->flags.realized = 0;
114 wMenuRealize(menu);
116 if (flags & WTextureSettings) {
117 if (!menu->flags.brother)
118 updateTexture(menu);
120 if (flags & (WTextureSettings|WColorSettings)) {
121 wMenuPaint(menu);
123 } else if (menu->flags.titled) {
125 if (flags & WFontSettings) {
126 menu->flags.realized = 0;
127 wMenuRealize(menu);
129 if (flags & WTextureSettings) {
130 menu->frame->flags.need_texture_remake = 1;
132 if (flags & (WColorSettings|WTextureSettings)) {
133 wFrameWindowPaint(menu->frame);
138 /************************************/
142 *----------------------------------------------------------------------
143 * wMenuCreate--
144 * Creates a new empty menu with the specified title. If main_menu
145 * is True, the created menu will be a main menu, which has some special
146 * properties such as being placed over other normal menus.
147 * If title is NULL, the menu will have no titlebar.
149 * Returns:
150 * The created menu.
151 *----------------------------------------------------------------------
153 WMenu*
154 wMenuCreate(WScreen *screen, char *title, int main_menu)
156 WMenu *menu;
157 static int brother=0;
158 int tmp, flags;
160 menu = wmalloc(sizeof(WMenu));
162 memset(menu, 0, sizeof(WMenu));
164 #ifdef SINGLE_MENULEVEL
165 tmp = WMSubmenuLevel;
166 #else
167 tmp = (main_menu ? WMMainMenuLevel : WMSubmenuLevel);
168 #endif
170 flags = WFF_SINGLE_STATE|WFF_BORDER;
171 if (title) {
172 flags |= WFF_TITLEBAR|WFF_RIGHT_BUTTON;
173 menu->flags.titled = 1;
175 menu->frame =
176 wFrameWindowCreate(screen, tmp, 8, 2, 1, 1, &wPreferences.menu_title_clearance, flags,
177 screen->menu_title_texture, NULL,
178 screen->menu_title_color,
179 &screen->menu_title_font);
181 menu->frame->core->descriptor.parent = menu;
182 menu->frame->core->descriptor.parent_type = WCLASS_MENU;
183 menu->frame->core->descriptor.handle_mousedown = menuMouseDown;
185 wFrameWindowHideButton(menu->frame, WFF_RIGHT_BUTTON);
187 if (title) {
188 menu->frame->title = wstrdup(title);
191 menu->frame->flags.justification = WTJ_LEFT;
193 menu->frame->rbutton_image = screen->b_pixmaps[WBUT_CLOSE];
195 menu->entry_no = 0;
196 menu->alloced_entries = 0;
197 menu->selected_entry = -1;
198 menu->entries = NULL;
200 menu->frame_x = screen->app_menu_x;
201 menu->frame_y = screen->app_menu_y;
203 menu->frame->child = menu;
205 menu->flags.lowered = 0;
207 /* create borders */
208 if (title) {
209 /* setup object descriptors */
210 menu->frame->on_mousedown_titlebar = menuTitleMouseDown;
211 menu->frame->on_dblclick_titlebar = menuTitleDoubleClick;
214 menu->frame->on_click_right = menuCloseClick;
217 menu->menu = wCoreCreate(menu->frame->core, 0, menu->frame->top_width,
218 menu->frame->core->width, 10);
220 menu->menu->descriptor.parent = menu;
221 menu->menu->descriptor.parent_type = WCLASS_MENU;
222 menu->menu->descriptor.handle_expose = menuExpose;
223 menu->menu->descriptor.handle_mousedown = menuMouseDown;
225 menu->menu_texture_data = None;
227 XMapWindow(dpy, menu->menu->window);
229 XFlush(dpy);
231 if (!brother) {
232 brother = 1;
233 menu->brother = wMenuCreate(screen, title, main_menu);
234 brother = 0;
235 menu->brother->flags.brother = 1;
236 menu->brother->brother = menu;
238 WMAddNotificationObserver(appearanceObserver, menu,
239 WNMenuAppearanceSettingsChanged, menu);
241 WMAddNotificationObserver(appearanceObserver, menu,
242 WNMenuTitleAppearanceSettingsChanged, menu);
245 return menu;
251 WMenu*
252 wMenuCreateForApp(WScreen *screen, char *title, int main_menu)
254 WMenu *menu;
256 menu = wMenuCreate(screen, title, main_menu);
257 if (!menu)
258 return NULL;
259 menu->flags.app_menu = 1;
260 menu->brother->flags.app_menu = 1;
262 return menu;
267 static void
268 insertEntry(WMenu *menu, WMenuEntry *entry, int index)
270 int i;
272 for (i = menu->entry_no-1; i >= index; i--) {
273 menu->entries[i]->order++;
274 menu->entries[i+1] = menu->entries[i];
276 menu->entries[index] = entry;
280 WMenuEntry*
281 wMenuInsertCallback(WMenu *menu, int index, char *text,
282 void (*callback)(WMenu *menu, WMenuEntry *entry),
283 void *clientdata)
285 WMenuEntry *entry;
287 menu->flags.realized = 0;
288 menu->brother->flags.realized = 0;
290 /* reallocate array if it's too small */
291 if (menu->entry_no >= menu->alloced_entries) {
292 void *tmp;
294 tmp = wrealloc(menu->entries,
295 sizeof(WMenuEntry)*(menu->alloced_entries+5));
297 menu->entries = tmp;
298 menu->alloced_entries += 5;
300 menu->brother->entries = tmp;
301 menu->brother->alloced_entries = menu->alloced_entries;
303 entry = wmalloc(sizeof(WMenuEntry));
304 memset(entry, 0, sizeof(WMenuEntry));
305 entry->flags.enabled = 1;
306 entry->text = wstrdup(text);
307 entry->cascade = -1;
308 entry->clientdata = clientdata;
309 entry->callback = callback;
310 if (index<0 || index>=menu->entry_no) {
311 entry->order = menu->entry_no;
312 menu->entries[menu->entry_no] = entry;
313 } else {
314 entry->order = index;
315 insertEntry(menu, entry, index);
318 menu->entry_no++;
319 menu->brother->entry_no = menu->entry_no;
321 return entry;
326 void
327 wMenuEntrySetCascade(WMenu *menu, WMenuEntry *entry, WMenu *cascade)
329 WMenu *brother = menu->brother;
330 int i, done;
332 assert(menu->flags.brother==0);
334 if (entry->cascade>=0) {
335 menu->flags.realized = 0;
336 brother->flags.realized = 0;
339 cascade->parent = menu;
341 cascade->brother->parent = brother;
343 done = 0;
344 for (i=0; i<menu->cascade_no; i++) {
345 if (menu->cascades[i]==NULL) {
346 menu->cascades[i] = cascade;
347 brother->cascades[i] = cascade->brother;
348 done = 1;
349 entry->cascade = i;
350 break;
353 if (!done) {
354 entry->cascade = menu->cascade_no;
356 menu->cascades = wrealloc(menu->cascades,
357 sizeof(WMenu)*(menu->cascade_no+1));
358 menu->cascades[menu->cascade_no++] = cascade;
361 brother->cascades = wrealloc(brother->cascades,
362 sizeof(WMenu)*(brother->cascade_no+1));
363 brother->cascades[brother->cascade_no++] = cascade->brother;
367 if (menu->flags.lowered) {
369 cascade->flags.lowered = 1;
370 ChangeStackingLevel(cascade->frame->core, WMNormalLevel);
372 cascade->brother->flags.lowered = 1;
373 ChangeStackingLevel(cascade->brother->frame->core, WMNormalLevel);
376 if (!menu->flags.realized)
377 wMenuRealize(menu);
381 void
382 wMenuEntryRemoveCascade(WMenu *menu, WMenuEntry *entry)
384 assert(menu->flags.brother==0);
386 /* destroy cascade menu */
387 if (entry->cascade>=0 && menu->cascades
388 && menu->cascades[entry->cascade]!=NULL) {
390 wMenuDestroy(menu->cascades[entry->cascade], True);
392 menu->cascades[entry->cascade] = NULL;
393 menu->brother->cascades[entry->cascade] = NULL;
395 entry->cascade = -1;
400 void
401 wMenuRemoveItem(WMenu *menu, int index)
403 int i;
405 if (menu->flags.brother) {
406 wMenuRemoveItem(menu->brother, index);
407 return;
410 if (index>=menu->entry_no) return;
412 /* destroy cascade menu */
413 wMenuEntryRemoveCascade(menu, menu->entries[index]);
415 /* destroy unshared data */
417 if (menu->entries[index]->text)
418 wfree(menu->entries[index]->text);
420 if (menu->entries[index]->rtext)
421 wfree(menu->entries[index]->rtext);
423 if (menu->entries[index]->free_cdata && menu->entries[index]->clientdata)
424 (*menu->entries[index]->free_cdata)(menu->entries[index]->clientdata);
426 wfree(menu->entries[index]);
428 for (i=index; i<menu->entry_no-1; i++) {
429 menu->entries[i+1]->order--;
430 menu->entries[i]=menu->entries[i+1];
432 menu->entry_no--;
433 menu->brother->entry_no--;
437 static Pixmap
438 renderTexture(WMenu *menu)
440 RImage *img;
441 Pixmap pix;
442 int i;
443 RColor light;
444 RColor dark;
445 RColor mid;
446 WScreen *scr = menu->menu->screen_ptr;
447 WTexture *texture = scr->menu_item_texture;
449 if (wPreferences.menu_style == MS_NORMAL) {
450 img = wTextureRenderImage(texture, menu->menu->width,
451 menu->entry_height, WREL_MENUENTRY);
452 } else {
453 img = wTextureRenderImage(texture, menu->menu->width,
454 menu->menu->height+1, WREL_MENUENTRY);
456 if (!img) {
457 wwarning(_("could not render texture: %s"),
458 RMessageForError(RErrorCode));
460 return None;
463 if (wPreferences.menu_style == MS_SINGLE_TEXTURE) {
464 light.alpha = 0;
465 light.red = light.green = light.blue = 80;
467 dark.alpha = 255;
468 dark.red = dark.green = dark.blue = 0;
470 mid.alpha = 0;
471 mid.red = mid.green = mid.blue = 40;
473 for (i = 1; i < menu->entry_no; i++) {
474 ROperateLine(img, RSubtractOperation, 0, i*menu->entry_height-2,
475 menu->menu->width-1, i*menu->entry_height-2, &mid);
477 RDrawLine(img, 0, i*menu->entry_height-1,
478 menu->menu->width-1, i*menu->entry_height-1, &dark);
480 ROperateLine(img, RAddOperation, 0, i*menu->entry_height,
481 menu->menu->width-1, i*menu->entry_height,
482 &light);
485 if (!RConvertImage(scr->rcontext, img, &pix)) {
486 wwarning(_("error rendering image:%s"), RMessageForError(RErrorCode));
488 RReleaseImage(img);
490 return pix;
494 static void
495 updateTexture(WMenu *menu)
497 WScreen *scr = menu->menu->screen_ptr;
499 /* setup background texture */
500 if (scr->menu_item_texture->any.type != WTEX_SOLID) {
501 if (!menu->flags.brother) {
502 FREE_PIXMAP(menu->menu_texture_data);
504 menu->menu_texture_data = renderTexture(menu);
506 XSetWindowBackgroundPixmap(dpy, menu->menu->window,
507 menu->menu_texture_data);
508 XClearWindow(dpy, menu->menu->window);
510 XSetWindowBackgroundPixmap(dpy, menu->brother->menu->window,
511 menu->menu_texture_data);
512 XClearWindow(dpy, menu->brother->menu->window);
514 } else {
515 XSetWindowBackground(dpy, menu->menu->window,
516 scr->menu_item_texture->any.color.pixel);
517 XClearWindow(dpy, menu->menu->window);
522 void
523 wMenuRealize(WMenu *menu)
525 int i;
526 int width, rwidth, mrwidth, mwidth;
527 int theight, twidth, eheight;
528 WScreen *scr = menu->frame->screen_ptr;
529 static int brother_done=0;
530 int flags;
532 if (!brother_done) {
533 brother_done = 1;
534 wMenuRealize(menu->brother);
535 brother_done = 0;
538 flags = WFF_SINGLE_STATE|WFF_BORDER;
539 if (menu->flags.titled)
540 flags |= WFF_TITLEBAR|WFF_RIGHT_BUTTON;
542 wFrameWindowUpdateBorders(menu->frame, flags);
544 if (menu->flags.titled) {
545 twidth = WMWidthOfString(scr->menu_title_font, menu->frame->title,
546 strlen(menu->frame->title));
547 theight = menu->frame->top_width;
548 twidth += theight + (wPreferences.new_style ? 16 : 8);
549 } else {
550 twidth = 0;
551 theight = 0;
553 eheight = WMFontHeight(scr->menu_entry_font) + 6 + wPreferences.menu_text_clearance * 2;
554 menu->entry_height = eheight;
555 mrwidth = 0;
556 mwidth = 0;
557 for (i=0; i<menu->entry_no; i++) {
558 char *text;
560 /* search widest text */
561 text = menu->entries[i]->text;
562 width = WMWidthOfString(scr->menu_entry_font, text, strlen(text))+10;
564 if (menu->entries[i]->flags.indicator) {
565 width += MENU_INDICATOR_SPACE;
568 if (width > mwidth)
569 mwidth = width;
571 /* search widest text on right */
572 text = menu->entries[i]->rtext;
573 if (text)
574 rwidth = WMWidthOfString(scr->menu_entry_font, text, strlen(text))
575 + 5;
576 else if (menu->entries[i]->cascade>=0)
577 rwidth = 16;
578 else
579 rwidth = 4;
581 if (rwidth > mrwidth)
582 mrwidth = rwidth;
584 mwidth += mrwidth;
586 if (mwidth < twidth)
587 mwidth = twidth;
590 wCoreConfigure(menu->menu, 0, theight, mwidth, menu->entry_no*eheight -1);
592 wFrameWindowResize(menu->frame, mwidth, menu->entry_no*eheight-1
593 + menu->frame->top_width + menu->frame->bottom_width);
596 updateTexture(menu);
598 menu->flags.realized = 1;
600 if (menu->flags.mapped)
601 wMenuPaint(menu);
602 if (menu->brother->flags.mapped)
603 wMenuPaint(menu->brother);
607 void
608 wMenuDestroy(WMenu *menu, int recurse)
610 int i;
612 WMRemoveNotificationObserver(menu);
614 /* remove any pending timers */
615 if (menu->timer)
616 WMDeleteTimerHandler(menu->timer);
617 menu->timer = NULL;
619 /* call destroy handler */
620 if (menu->on_destroy)
621 (*menu->on_destroy)(menu);
623 /* Destroy items if this menu own them. If this is the "brother" menu,
624 * leave them alone as it is shared by them.
626 if (!menu->flags.brother) {
627 for (i=0; i<menu->entry_no; i++) {
629 wfree(menu->entries[i]->text);
631 if (menu->entries[i]->rtext)
632 wfree(menu->entries[i]->rtext);
633 #ifdef USER_MENU
635 if (menu->entries[i]->instances){
636 WMReleasePropList(menu->entries[i]->instances);
638 #endif /* USER_MENU */
640 if (menu->entries[i]->free_cdata && menu->entries[i]->clientdata) {
641 (*menu->entries[i]->free_cdata)(menu->entries[i]->clientdata);
643 wfree(menu->entries[i]);
646 if (recurse) {
647 for (i=0; i<menu->cascade_no; i++) {
648 if (menu->cascades[i]) {
649 if (menu->cascades[i]->flags.brother)
650 wMenuDestroy(menu->cascades[i]->brother, recurse);
651 else
652 wMenuDestroy(menu->cascades[i], recurse);
657 if (menu->entries)
658 wfree(menu->entries);
662 FREE_PIXMAP(menu->menu_texture_data);
664 if (menu->cascades)
665 wfree(menu->cascades);
667 wCoreDestroy(menu->menu);
668 wFrameWindowDestroy(menu->frame);
670 /* destroy copy of this menu */
671 if (!menu->flags.brother && menu->brother)
672 wMenuDestroy(menu->brother, False);
674 wfree(menu);
678 #define F_NORMAL 0
679 #define F_TOP 1
680 #define F_BOTTOM 2
681 #define F_NONE 3
683 static void
684 drawFrame(WScreen *scr, Drawable win, int y, int w, int h, int type)
686 XSegment segs[2];
687 int i;
689 i = 0;
690 segs[i].x1 = segs[i].x2 = w-1;
691 segs[i].y1 = y;
692 segs[i].y2 = y + h - 1;
693 i++;
694 if (type != F_TOP && type != F_NONE) {
695 segs[i].x1 = 1;
696 segs[i].y1 = segs[i].y2 = y + h-2;
697 segs[i].x2 = w-1;
698 i++;
700 XDrawSegments(dpy, win, scr->menu_item_auxtexture->dim_gc, segs, i);
702 i = 0;
703 segs[i].x1 = 0;
704 segs[i].y1 = y;
705 segs[i].x2 = 0;
706 segs[i].y2 = y + h - 1;
707 i++;
708 if (type != F_BOTTOM && type != F_NONE) {
709 segs[i].x1 = 0;
710 segs[i].y1 = y;
711 segs[i].x2 = w-1;
712 segs[i].y2 = y;
713 i++;
715 XDrawSegments(dpy, win, scr->menu_item_auxtexture->light_gc, segs, i);
717 if (type != F_TOP && type != F_NONE)
718 XDrawLine(dpy, win, scr->menu_item_auxtexture->dark_gc, 0, y+h-1,
719 w-1, y+h-1);
723 static void
724 paintEntry(WMenu *menu, int index, int selected)
726 WScreen *scr=menu->frame->screen_ptr;
727 Window win = menu->menu->window;
728 WMenuEntry *entry=menu->entries[index];
729 GC light, dim, dark;
730 WMColor *color;
731 int x, y, w, h, tw;
732 int type;
734 if (!menu->flags.realized) return;
735 h = menu->entry_height;
736 w = menu->menu->width;
737 y = index * h;
739 light = scr->menu_item_auxtexture->light_gc;
740 dim = scr->menu_item_auxtexture->dim_gc;
741 dark = scr->menu_item_auxtexture->dark_gc;
743 if (wPreferences.menu_style == MS_FLAT && menu->entry_no > 1) {
744 if (index == 0)
745 type = F_TOP;
746 else if (index == menu->entry_no - 1)
747 type = F_BOTTOM;
748 else
749 type = F_NONE;
750 } else {
751 type = F_NORMAL;
754 /* paint background */
755 if (selected) {
756 XFillRectangle(dpy, win, WMColorGC(scr->white), 1, y+1, w-2, h-3);
757 if (scr->menu_item_texture->any.type == WTEX_SOLID)
758 drawFrame(scr, win, y, w, h, type);
759 } else {
760 if (scr->menu_item_texture->any.type == WTEX_SOLID) {
761 XClearArea(dpy, win, 0, y + 1, w - 1, h - 3, False);
762 /* draw the frame */
763 drawFrame(scr, win, y, w, h, type);
764 } else {
765 XClearArea(dpy, win, 0, y, w, h, False);
769 if (selected) {
770 if (entry->flags.enabled)
771 color = scr->select_text_color;
772 else
773 color = scr->dtext_color;
774 } else if (!entry->flags.enabled) {
775 color = scr->dtext_color;
776 } else {
777 color = scr->mtext_color;
779 /* draw text */
780 x = 5;
781 if (entry->flags.indicator)
782 x += MENU_INDICATOR_SPACE + 2;
784 WMDrawString(scr->wmscreen, win, color, scr->menu_entry_font,
785 x, 3 + y + wPreferences.menu_text_clearance, entry->text, strlen(entry->text));
787 if (entry->cascade>=0) {
788 /* draw the cascade indicator */
789 XDrawLine(dpy,win,dim, w-11, y+6, w-6, y+h/2-1);
790 XDrawLine(dpy,win,light, w-11, y+h-8, w-6, y+h/2-1);
791 XDrawLine(dpy,win,dark, w-12, y+6, w-12, y+h-8);
794 /* draw indicator */
795 if (entry->flags.indicator && entry->flags.indicator_on) {
796 int iw, ih;
797 WPixmap *indicator;
800 switch (entry->flags.indicator_type) {
801 case MI_CHECK:
802 indicator = scr->menu_check_indicator;
803 break;
804 case MI_MINIWINDOW:
805 indicator = scr->menu_mini_indicator;
806 break;
807 case MI_HIDDEN:
808 indicator = scr->menu_hide_indicator;
809 break;
810 case MI_SHADED:
811 indicator = scr->menu_shade_indicator;
812 break;
813 case MI_DIAMOND:
814 default:
815 indicator = scr->menu_radio_indicator;
816 break;
819 iw = indicator->width;
820 ih = indicator->height;
821 XSetClipMask(dpy, scr->copy_gc, indicator->mask);
822 XSetClipOrigin(dpy, scr->copy_gc, 5, y+(h-ih)/2);
823 if (selected) {
824 if (entry->flags.enabled) {
825 XSetForeground(dpy, scr->copy_gc, scr->black_pixel);
826 } else {
827 XSetForeground(dpy, scr->copy_gc, WMColorPixel(scr->dtext_color));
829 } else {
830 if (entry->flags.enabled) {
831 XSetForeground(dpy, scr->copy_gc, WMColorPixel(scr->mtext_color));
832 } else {
833 XSetForeground(dpy, scr->copy_gc, WMColorPixel(scr->dtext_color));
836 XFillRectangle(dpy, win, scr->copy_gc, 5, y+(h-ih)/2, iw, ih);
838 XCopyArea(dpy, indicator->image, win, scr->copy_gc, 0, 0,
839 iw, ih, 5, y+(h-ih)/2);
841 XSetClipOrigin(dpy, scr->copy_gc, 0, 0);
844 /* draw right text */
846 if (entry->rtext && entry->cascade<0) {
847 tw = WMWidthOfString(scr->menu_entry_font, entry->rtext,
848 strlen(entry->rtext));
850 WMDrawString(scr->wmscreen, win, color, scr->menu_entry_font, w-6-tw,
851 y + 3 + wPreferences.menu_text_clearance, entry->rtext, strlen(entry->rtext));
856 static void
857 move_menus(WMenu *menu, int x, int y)
859 while (menu->parent) {
860 menu = menu->parent;
861 x -= MENUW(menu);
862 if (!wPreferences.align_menus && menu->selected_entry>=0) {
863 y -= menu->selected_entry*menu->entry_height;
866 wMenuMove(menu, x, y, True);
869 static void
870 makeVisible(WMenu *menu)
872 WScreen *scr = menu->frame->screen_ptr;
873 int x1, y1, x2, y2, new_x, new_y, move;
875 if (menu->entry_no<0) return;
877 x1 = menu->frame_x;
878 y1 = menu->frame_y+menu->frame->top_width
879 + menu->selected_entry*menu->entry_height;
880 x2 = x1 + MENUW(menu);
881 y2 = y1 + menu->entry_height;
883 new_x = x1;
884 new_y = y1;
885 move = 0;
887 if (x1 < 0) {
888 new_x = 0;
889 move = 1;
890 } else if (x2 >= scr->scr_width) {
891 new_x = scr->scr_width - MENUW(menu) - 1;
892 move = 1;
895 if (y1 < 0) {
896 new_y = 0;
897 move = 1;
898 } else if (y2 >= scr->scr_height) {
899 new_y = scr->scr_height - menu->entry_height - 1;
900 move = 1;
903 new_y = new_y - menu->frame->top_width
904 - menu->selected_entry*menu->entry_height;
905 move_menus(menu, new_x, new_y);
909 static int
910 check_key(WMenu *menu, XKeyEvent *event)
912 int i, ch, s;
913 char buffer[32];
915 if (XLookupString(event, buffer, 32, NULL, NULL)<1)
916 return -1;
918 ch = toupper(buffer[0]);
920 s = (menu->selected_entry>=0 ? menu->selected_entry+1 : 0);
922 again:
923 for (i=s; i<menu->entry_no; i++) {
924 if (ch==toupper(menu->entries[i]->text[0])) {
925 return i;
928 /* no match. Retry from start, if previous started from a selected entry */
929 if (s!=0) {
930 s = 0;
931 goto again;
933 return -1;
937 static int
938 keyboardMenu(WMenu *menu)
940 XEvent event;
941 KeySym ksym=NoSymbol;
942 int done=0;
943 int index;
944 WMenuEntry *entry;
945 int old_pos_x = menu->frame_x;
946 int old_pos_y = menu->frame_y;
947 int new_x = old_pos_x, new_y = old_pos_y;
948 int scr_width = menu->frame->screen_ptr->scr_width;
949 int scr_height = menu->frame->screen_ptr->scr_height;
951 if (menu->flags.editing)
952 return False;
955 XGrabKeyboard(dpy, menu->frame->core->window, True, GrabModeAsync,
956 GrabModeAsync, CurrentTime);
958 if (menu->frame_y+menu->frame->top_width >= scr_height)
959 new_y = scr_height - menu->frame->top_width;
961 if (menu->frame_x+MENUW(menu) >= scr_width)
962 new_x = scr_width-MENUW(menu)-1;
964 move_menus(menu, new_x, new_y);
966 while (!done && menu->flags.mapped) {
967 XAllowEvents(dpy, AsyncKeyboard, CurrentTime);
968 WMMaskEvent(dpy, ExposureMask|ButtonMotionMask|ButtonPressMask
969 |ButtonReleaseMask|KeyPressMask|KeyReleaseMask
970 |SubstructureNotifyMask, &event);
972 switch (event.type) {
973 case KeyPress:
974 ksym = XLookupKeysym(&event.xkey, 0);
975 switch (ksym) {
976 case XK_Escape:
977 done = 1;
978 break;
980 case XK_Home:
981 #ifdef XK_KP_Home
982 case XK_KP_Home:
983 #endif
984 selectEntry(menu, 0);
985 makeVisible(menu);
986 break;
988 case XK_End:
989 #ifdef XK_KP_End
990 case XK_KP_End:
991 #endif
992 selectEntry(menu, menu->entry_no-1);
993 makeVisible(menu);
994 break;
996 case XK_Up:
997 #ifdef ARROWLESS_KBD
998 case XK_k:
999 #endif
1000 #ifdef XK_KP_Up
1001 case XK_KP_Up:
1002 #endif
1003 if (menu->selected_entry <= 0)
1004 selectEntry(menu, menu->entry_no-1);
1005 else
1006 selectEntry(menu, menu->selected_entry-1);
1007 makeVisible(menu);
1008 break;
1010 case XK_Down:
1011 #ifdef ARROWLESS_KBD
1012 case XK_j:
1013 #endif
1014 #ifdef XK_KP_Down
1015 case XK_KP_Down:
1016 #endif
1017 if (menu->selected_entry<0)
1018 selectEntry(menu, 0);
1019 else if (menu->selected_entry == menu->entry_no-1)
1020 selectEntry(menu, 0);
1021 else if (menu->selected_entry < menu->entry_no-1)
1022 selectEntry(menu, menu->selected_entry+1);
1023 makeVisible(menu);
1024 break;
1026 case XK_Right:
1027 #ifdef ARROWLESS_KBD
1028 case XK_l:
1029 #endif
1030 #ifdef XK_KP_Right
1031 case XK_KP_Right:
1032 #endif
1033 if (menu->selected_entry>=0) {
1034 WMenuEntry *entry;
1035 entry = menu->entries[menu->selected_entry];
1037 if (entry->cascade >= 0 && menu->cascades
1038 && menu->cascades[entry->cascade]->entry_no > 0) {
1040 XUngrabKeyboard(dpy, CurrentTime);
1042 selectEntry(menu->cascades[entry->cascade], 0);
1043 if (!keyboardMenu(menu->cascades[entry->cascade]))
1044 done = 1;
1046 XGrabKeyboard(dpy, menu->frame->core->window, True,
1047 GrabModeAsync, GrabModeAsync,
1048 CurrentTime);
1051 break;
1053 case XK_Left:
1054 #ifdef ARROWLESS_KBD
1055 case XK_h:
1056 #endif
1057 #ifdef XK_KP_Left
1058 case XK_KP_Left:
1059 #endif
1060 if (menu->parent!=NULL && menu->parent->selected_entry>=0) {
1061 selectEntry(menu, -1);
1062 move_menus(menu, old_pos_x, old_pos_y);
1063 return True;
1065 break;
1067 case XK_Return:
1068 done = 2;
1069 break;
1071 default:
1072 index = check_key(menu, &event.xkey);
1073 if (index>=0) {
1074 selectEntry(menu, index);
1077 break;
1079 default:
1080 if (event.type==ButtonPress)
1081 done = 1;
1083 WMHandleEvent(&event);
1087 XUngrabKeyboard(dpy, CurrentTime);
1089 if (done==2 && menu->selected_entry>=0) {
1090 entry = menu->entries[menu->selected_entry];
1091 } else {
1092 entry = NULL;
1095 if (entry && entry->callback!=NULL && entry->flags.enabled
1096 && entry->cascade < 0) {
1097 #if (MENU_BLINK_COUNT > 0)
1098 int sel = menu->selected_entry;
1099 int i;
1101 for (i=0; i<MENU_BLINK_COUNT; i++) {
1102 paintEntry(menu, sel, False);
1103 XSync(dpy, 0);
1104 wusleep(MENU_BLINK_DELAY);
1105 paintEntry(menu, sel, True);
1106 XSync(dpy, 0);
1107 wusleep(MENU_BLINK_DELAY);
1109 #endif
1110 selectEntry(menu, -1);
1112 if (!menu->flags.buttoned) {
1113 wMenuUnmap(menu);
1114 move_menus(menu, old_pos_x, old_pos_y);
1116 closeCascade(menu);
1118 (*entry->callback)(menu, entry);
1119 } else {
1120 if (!menu->flags.buttoned) {
1121 wMenuUnmap(menu);
1122 move_menus(menu, old_pos_x, old_pos_y);
1124 selectEntry(menu, -1);
1128 /* returns True if returning from a submenu to a parent menu,
1129 * False if exiting from menu */
1130 return False;
1134 void
1135 wMenuMapAt(WMenu *menu, int x, int y, int keyboard)
1137 int scr_width = menu->frame->screen_ptr->scr_width;
1138 int scr_height = menu->frame->screen_ptr->scr_height;
1140 if (!menu->flags.realized) {
1141 menu->flags.realized=1;
1142 wMenuRealize(menu);
1144 if (!menu->flags.mapped) {
1145 if (wPreferences.wrap_menus) {
1146 if (x<0) x = 0;
1147 if (y<0) y = 0;
1148 if (x+MENUW(menu) > scr_width)
1149 x = scr_width - MENUW(menu);
1150 if (y+MENUH(menu) > scr_height)
1151 y = scr_height - MENUH(menu);
1154 XMoveWindow(dpy, menu->frame->core->window, x, y);
1155 menu->frame_x = x;
1156 menu->frame_y = y;
1157 XMapWindow(dpy, menu->frame->core->window);
1158 wRaiseFrame(menu->frame->core);
1159 menu->flags.mapped = 1;
1160 } else {
1161 selectEntry(menu, 0);
1164 if (keyboard)
1165 keyboardMenu(menu);
1169 void
1170 wMenuMap(WMenu *menu)
1172 if (!menu->flags.realized) {
1173 menu->flags.realized=1;
1174 wMenuRealize(menu);
1176 if (menu->flags.app_menu && menu->parent==NULL) {
1177 menu->frame_x = menu->frame->screen_ptr->app_menu_x;
1178 menu->frame_y = menu->frame->screen_ptr->app_menu_y;
1179 XMoveWindow(dpy, menu->frame->core->window, menu->frame_x, menu->frame_y);
1181 XMapWindow(dpy, menu->frame->core->window);
1182 wRaiseFrame(menu->frame->core);
1183 menu->flags.mapped = 1;
1187 void
1188 wMenuUnmap(WMenu *menu)
1190 int i;
1192 XUnmapWindow(dpy, menu->frame->core->window);
1193 if (menu->flags.titled && menu->flags.buttoned) {
1194 wFrameWindowHideButton(menu->frame, WFF_RIGHT_BUTTON);
1196 menu->flags.buttoned = 0;
1197 menu->flags.mapped = 0;
1198 menu->flags.open_to_left = 0;
1200 for (i=0; i<menu->cascade_no; i++) {
1201 if (menu->cascades[i]!=NULL
1202 && menu->cascades[i]->flags.mapped
1203 && !menu->cascades[i]->flags.buttoned) {
1205 wMenuUnmap(menu->cascades[i]);
1208 menu->selected_entry = -1;
1213 void
1214 wMenuPaint(WMenu *menu)
1216 int i;
1218 if (!menu->flags.mapped) {
1219 return;
1222 /* paint entries */
1223 for (i=0; i<menu->entry_no; i++) {
1224 paintEntry(menu, i, i==menu->selected_entry);
1229 void
1230 wMenuSetEnabled(WMenu *menu, int index, int enable)
1232 if (index>=menu->entry_no) return;
1233 menu->entries[index]->flags.enabled=enable;
1234 paintEntry(menu, index, index==menu->selected_entry);
1235 paintEntry(menu->brother, index, index==menu->selected_entry);
1239 /* ====================================================================== */
1242 static void
1243 editEntry(WMenu *menu, WMenuEntry *entry)
1245 WTextInput *text;
1246 XEvent event;
1247 WObjDescriptor *desc;
1248 char *t;
1249 int done = 0;
1250 Window old_focus;
1251 int old_revert;
1253 menu->flags.editing = 1;
1255 text = wTextCreate(menu->menu, 1, menu->entry_height * entry->order,
1256 menu->menu->width - 2, menu->entry_height - 1);
1258 wTextPutText(text, entry->text);
1259 XGetInputFocus(dpy, &old_focus, &old_revert);
1260 XSetInputFocus(dpy, text->core->window, RevertToNone, CurrentTime);
1262 if (XGrabKeyboard(dpy, text->core->window, True, GrabModeAsync,
1263 GrabModeAsync, CurrentTime)!=GrabSuccess) {
1264 wwarning(_("could not grab keyboard"));
1265 wTextDestroy(text);
1267 wSetFocusTo(menu->frame->screen_ptr,
1268 menu->frame->screen_ptr->focused_window);
1269 return;
1273 while (!done && !text->done) {
1274 XSync(dpy, 0);
1275 XAllowEvents(dpy, AsyncKeyboard|AsyncPointer, CurrentTime);
1276 XSync(dpy, 0);
1277 WMNextEvent(dpy, &event);
1279 if (XFindContext(dpy, event.xany.window, wWinContext,
1280 (XPointer *)&desc)==XCNOENT)
1281 desc = NULL;
1283 if ((desc != NULL) && (desc->handle_anything != NULL)) {
1285 (*desc->handle_anything)(desc, &event);
1287 } else {
1288 switch (event.type) {
1289 case ButtonPress:
1290 XAllowEvents(dpy, ReplayPointer, CurrentTime);
1291 done = 1;
1293 default:
1294 WMHandleEvent(&event);
1295 break;
1300 XSetInputFocus(dpy, old_focus, old_revert, CurrentTime);
1302 wSetFocusTo(menu->frame->screen_ptr,
1303 menu->frame->screen_ptr->focused_window);
1306 t = wTextGetText(text);
1307 /* if !t, the user has canceled editing */
1308 if (t) {
1309 if (entry->text)
1310 wfree(entry->text);
1311 entry->text = wstrdup(t);
1313 menu->flags.realized = 0;
1315 wTextDestroy(text);
1317 XUngrabKeyboard(dpy, CurrentTime);
1319 if (t && menu->on_edit)
1320 (*menu->on_edit)(menu, entry);
1322 menu->flags.editing = 0;
1324 if (!menu->flags.realized)
1325 wMenuRealize(menu);
1329 static void
1330 selectEntry(WMenu *menu, int entry_no)
1332 WMenuEntry *entry;
1333 WMenu *submenu;
1334 int old_entry;
1336 if (menu->entries==NULL)
1337 return;
1339 if (entry_no >= menu->entry_no)
1340 return;
1342 old_entry = menu->selected_entry;
1343 menu->selected_entry = entry_no;
1345 if (old_entry!=entry_no) {
1347 /* unselect previous entry */
1348 if (old_entry>=0) {
1349 paintEntry(menu, old_entry, False);
1350 entry = menu->entries[old_entry];
1352 /* unmap cascade */
1353 if (entry->cascade>=0 && menu->cascades) {
1354 if (!menu->cascades[entry->cascade]->flags.buttoned) {
1355 wMenuUnmap(menu->cascades[entry->cascade]);
1360 if (entry_no<0) {
1361 menu->selected_entry = -1;
1362 return;
1364 entry = menu->entries[entry_no];
1366 if (entry->cascade>=0 && menu->cascades && entry->flags.enabled) {
1367 /* Callback for when the submenu is opened.
1369 submenu = menu->cascades[entry->cascade];
1370 if (submenu && submenu->flags.brother)
1371 submenu = submenu->brother;
1373 if (entry->callback) {
1374 /* Only call the callback if the submenu is not yet mapped.
1376 if (menu->flags.brother) {
1377 if (!submenu || !submenu->flags.mapped)
1378 (*entry->callback)(menu->brother, entry);
1379 } else {
1380 if (!submenu || !submenu->flags.buttoned)
1381 (*entry->callback)(menu, entry);
1385 /* the submenu menu might have changed */
1386 submenu = menu->cascades[entry->cascade];
1388 /* map cascade */
1389 if (!submenu->flags.mapped) {
1390 int x, y;
1392 if (!submenu->flags.realized)
1393 wMenuRealize(submenu);
1394 if (wPreferences.wrap_menus) {
1395 if (menu->flags.open_to_left)
1396 submenu->flags.open_to_left = 1;
1398 if (submenu->flags.open_to_left) {
1399 x = menu->frame_x - MENUW(submenu);
1400 if (x<0) {
1401 x = 0;
1402 submenu->flags.open_to_left = 0;
1404 } else {
1405 x = menu->frame_x + MENUW(menu);
1407 if (x + MENUW(submenu)
1408 >= menu->frame->screen_ptr->scr_width) {
1410 x = menu->frame_x - MENUW(submenu);
1411 submenu->flags.open_to_left = 1;
1414 } else {
1415 x = menu->frame_x + MENUW(menu);
1418 if (wPreferences.align_menus) {
1419 y = menu->frame_y;
1420 } else {
1421 y = menu->frame_y + menu->entry_height*entry_no;
1422 if (menu->flags.titled)
1423 y += menu->frame->top_width;
1424 if (menu->cascades[entry->cascade]->flags.titled)
1425 y -= menu->cascades[entry->cascade]->frame->top_width;
1428 wMenuMapAt(menu->cascades[entry->cascade], x, y, False);
1429 menu->cascades[entry->cascade]->parent = menu;
1430 } else {
1431 return;
1434 paintEntry(menu, entry_no, True);
1439 static WMenu*
1440 findMenu(WScreen *scr, int *x_ret, int *y_ret)
1442 WMenu *menu;
1443 WObjDescriptor *desc;
1444 Window root_ret, win, junk_win;
1445 int x, y, wx, wy;
1446 unsigned int mask;
1448 XQueryPointer(dpy, scr->root_win, &root_ret, &win, &x, &y, &wx, &wy,
1449 &mask);
1451 if (win==None) return NULL;
1453 if (XFindContext(dpy, win, wWinContext, (XPointer *)&desc)==XCNOENT)
1454 return NULL;
1456 if (desc->parent_type == WCLASS_MENU) {
1457 menu = (WMenu*)desc->parent;
1458 XTranslateCoordinates(dpy, root_ret, menu->menu->window, wx, wy,
1459 x_ret, y_ret, &junk_win);
1460 return menu;
1462 return NULL;
1468 static void
1469 closeCascade(WMenu *menu)
1471 WMenu *parent=menu->parent;
1473 if (menu->flags.brother
1474 || (!menu->flags.buttoned
1475 && (!menu->flags.app_menu||menu->parent!=NULL))) {
1477 selectEntry(menu, -1);
1478 XSync(dpy, 0);
1479 #if (MENU_BLINK_DELAY > 2)
1480 wusleep(MENU_BLINK_DELAY/2);
1481 #endif
1482 wMenuUnmap(menu);
1483 while (parent!=NULL
1484 && (parent->parent!=NULL || !parent->flags.app_menu
1485 || parent->flags.brother)
1486 && !parent->flags.buttoned) {
1487 selectEntry(parent, -1);
1488 wMenuUnmap(parent);
1489 parent = parent->parent;
1491 if (parent)
1492 selectEntry(parent, -1);
1497 static void
1498 closeBrotherCascadesOf(WMenu *menu)
1500 WMenu *tmp;
1501 int i;
1503 for (i=0; i<menu->cascade_no; i++) {
1504 if (menu->cascades[i]->flags.brother) {
1505 tmp = menu->cascades[i];
1506 } else {
1507 tmp = menu->cascades[i]->brother;
1509 if (tmp->flags.mapped) {
1510 selectEntry(tmp->parent, -1);
1511 closeBrotherCascadesOf(tmp);
1512 break;
1518 #define getEntryAt(menu, x, y) ((y)<0 ? -1 : (y)/(menu->entry_height))
1521 static WMenu*
1522 parentMenu(WMenu *menu)
1524 WMenu *parent;
1525 WMenuEntry *entry;
1527 if (menu->flags.buttoned)
1528 return menu;
1530 while (menu->parent && menu->parent->flags.mapped) {
1531 parent = menu->parent;
1532 if (parent->selected_entry < 0)
1533 break;
1534 entry = parent->entries[parent->selected_entry];
1535 if (!entry->flags.enabled || entry->cascade<0 || !parent->cascades ||
1536 parent->cascades[entry->cascade] != menu)
1537 break;
1538 menu = parent;
1539 if (menu->flags.buttoned)
1540 break;
1543 return menu;
1549 * Will raise the passed menu, if submenu = 0
1550 * If submenu > 0 will also raise all mapped submenus
1551 * until the first buttoned one
1552 * If submenu < 0 will also raise all mapped parent menus
1553 * until the first buttoned one
1556 static void
1557 raiseMenus(WMenu *menu, int submenus)
1559 WMenu *submenu;
1560 int i;
1562 if(!menu) return;
1564 wRaiseFrame(menu->frame->core);
1566 if (submenus>0 && menu->selected_entry>=0) {
1567 i = menu->entries[menu->selected_entry]->cascade;
1568 if (i>=0 && menu->cascades) {
1569 submenu = menu->cascades[i];
1570 if (submenu->flags.mapped && !submenu->flags.buttoned)
1571 raiseMenus(submenu, submenus);
1574 if (submenus<0 && !menu->flags.buttoned &&
1575 menu->parent && menu->parent->flags.mapped)
1576 raiseMenus(menu->parent, submenus);
1580 WMenu*
1581 wMenuUnderPointer(WScreen *screen)
1583 WObjDescriptor *desc;
1584 Window root_ret, win;
1585 int dummy;
1586 unsigned int mask;
1588 XQueryPointer(dpy, screen->root_win, &root_ret, &win, &dummy, &dummy,
1589 &dummy, &dummy, &mask);
1591 if (win==None) return NULL;
1593 if (XFindContext(dpy, win, wWinContext, (XPointer *)&desc)==XCNOENT)
1594 return NULL;
1596 if (desc->parent_type == WCLASS_MENU)
1597 return (WMenu *)desc->parent;
1598 return NULL;
1604 static void
1605 getPointerPosition(WScreen *scr, int *x, int *y)
1607 Window root_ret, win;
1608 int wx, wy;
1609 unsigned int mask;
1611 XQueryPointer(dpy, scr->root_win, &root_ret, &win, x, y, &wx, &wy, &mask);
1615 static void
1616 getScrollAmount(WMenu *menu, int *hamount, int *vamount)
1618 WScreen *scr = menu->menu->screen_ptr;
1619 int menuX1 = menu->frame_x;
1620 int menuY1 = menu->frame_y;
1621 int menuX2 = menu->frame_x + MENUW(menu);
1622 int menuY2 = menu->frame_y + MENUH(menu);
1623 int screenW = scr->scr_width;
1624 int screenH = scr->scr_height;
1625 int xroot, yroot;
1627 *hamount = 0;
1628 *vamount = 0;
1630 getPointerPosition(scr, &xroot, &yroot);
1633 if (xroot <= 1 && menuX1 < 0) {
1634 /* scroll to the right */
1635 *hamount = WMIN(MENU_SCROLL_STEP, abs(menuX1));
1637 } else if (xroot >= screenW-2 && menuX2 > screenW-1) {
1638 /* scroll to the left */
1639 *hamount = WMIN(MENU_SCROLL_STEP, abs(menuX2-screenW-1));
1641 if (*hamount==0)
1642 *hamount = 1;
1644 *hamount = -*hamount;
1647 if (yroot <= 1 && menuY1 < 0) {
1648 /* scroll down */
1649 *vamount = WMIN(MENU_SCROLL_STEP, abs(menuY1));
1651 } else if (yroot >= screenH-2 && menuY2 > screenH-1) {
1652 /* scroll up */
1653 *vamount = WMIN(MENU_SCROLL_STEP, abs(menuY2-screenH-2));
1655 *vamount = -*vamount;
1660 static void
1661 dragScrollMenuCallback(void *data)
1663 WMenu *menu = (WMenu*)data;
1664 WScreen *scr = menu->menu->screen_ptr;
1665 WMenu *parent = parentMenu(menu);
1666 int hamount, vamount;
1667 int x, y;
1668 int newSelectedEntry;
1670 getScrollAmount(menu, &hamount, &vamount);
1673 if (hamount != 0 || vamount != 0) {
1674 wMenuMove(parent, parent->frame_x + hamount,
1675 parent->frame_y + vamount, True);
1676 if (findMenu(scr, &x, &y)) {
1677 newSelectedEntry = getEntryAt(menu, x, y);
1678 selectEntry(menu, newSelectedEntry);
1679 } else {
1680 /* Pointer fell outside of menu. If the selected entry is
1681 * not a submenu, unselect it */
1682 if (menu->selected_entry >= 0
1683 && menu->entries[menu->selected_entry]->cascade<0)
1684 selectEntry(menu, -1);
1685 newSelectedEntry = 0;
1688 /* paranoid check */
1689 if (newSelectedEntry >= 0) {
1690 /* keep scrolling */
1691 menu->timer = WMAddTimerHandler(MENU_SCROLL_DELAY,
1692 dragScrollMenuCallback, menu);
1693 } else {
1694 menu->timer = NULL;
1696 } else {
1697 /* don't need to scroll anymore */
1698 menu->timer = NULL;
1699 if (findMenu(scr, &x, &y)) {
1700 newSelectedEntry = getEntryAt(menu, x, y);
1701 selectEntry(menu, newSelectedEntry);
1707 static void
1708 scrollMenuCallback(void *data)
1710 WMenu *menu = (WMenu*)data;
1711 WMenu *parent = parentMenu(menu);
1712 int hamount = 0; /* amount to scroll */
1713 int vamount = 0;
1715 #ifdef VIRTUAL_DESKTOP
1716 /* don't scroll if it is in vdesk mode */
1717 if (!wPreferences.vedge_thickness)
1718 #endif
1719 getScrollAmount(menu, &hamount, &vamount);
1721 if (hamount != 0 || vamount != 0) {
1722 wMenuMove(parent, parent->frame_x + hamount,
1723 parent->frame_y + vamount, True);
1725 /* keep scrolling */
1726 menu->timer = WMAddTimerHandler(MENU_SCROLL_DELAY,
1727 scrollMenuCallback, menu);
1728 } else {
1729 /* don't need to scroll anymore */
1730 menu->timer = NULL;
1736 #define MENU_SCROLL_BORDER 5
1738 static int
1739 isPointNearBoder(WMenu *menu, int x, int y)
1741 int menuX1 = menu->frame_x;
1742 int menuY1 = menu->frame_y;
1743 int menuX2 = menu->frame_x + MENUW(menu);
1744 int menuY2 = menu->frame_y + MENUH(menu);
1745 int scrXe = menu->menu->screen_ptr->scr_width-1;
1746 int scrYe = menu->menu->screen_ptr->scr_height-1;
1747 int flag = 0;
1749 if (x >= menuX1 && x <= menuX2 && (y < MENU_SCROLL_BORDER
1750 || y > scrYe-MENU_SCROLL_BORDER))
1751 flag = 1;
1752 else if (y >= menuY1 && y <= menuY2 && (x < MENU_SCROLL_BORDER
1753 || x > scrXe-MENU_SCROLL_BORDER))
1754 flag = 1;
1756 return flag;
1760 typedef struct _delay {
1761 WWindow *wwin;
1762 WMenu *menu;
1763 int ox,oy;
1764 } _delay;
1767 static void
1768 _leaving(_delay *dl)
1770 wMenuMove(dl->menu, dl->ox, dl->oy, True);
1771 dl->menu->jump_back=NULL;
1772 dl->menu->menu->screen_ptr->flags.jump_back_pending = 0;
1773 wfree(dl);
1777 void
1778 wMenuScroll(WMenu *menu, XEvent *event)
1780 WMenu *smenu;
1781 WMenu *omenu = parentMenu(menu);
1782 WScreen *scr = menu->frame->screen_ptr;
1783 int done = 0;
1784 int jump_back = 0;
1785 int old_frame_x = omenu->frame_x;
1786 int old_frame_y = omenu->frame_y;
1787 XEvent ev;
1789 if (omenu->jump_back)
1790 WMDeleteTimerWithClientData(omenu->jump_back);
1793 if ((/*omenu->flags.buttoned &&*/ !wPreferences.wrap_menus)
1794 || omenu->flags.app_menu) {
1795 jump_back = 1;
1798 if (!wPreferences.wrap_menus)
1799 raiseMenus(omenu, True);
1800 else
1801 raiseMenus(menu, False);
1803 if (!menu->timer)
1804 scrollMenuCallback(menu);
1806 while(!done) {
1807 int x, y, on_border, on_x_edge, on_y_edge, on_title;
1809 WMNextEvent(dpy, &ev);
1810 switch (ev.type) {
1811 case EnterNotify:
1812 WMHandleEvent(&ev);
1813 case MotionNotify:
1814 x = (ev.type==MotionNotify) ? ev.xmotion.x_root : ev.xcrossing.x_root;
1815 y = (ev.type==MotionNotify) ? ev.xmotion.y_root : ev.xcrossing.y_root;
1817 /* on_border is != 0 if the pointer is between the menu
1818 * and the screen border and is close enough to the border */
1819 on_border = isPointNearBoder(menu, x, y);
1821 smenu = wMenuUnderPointer(scr);
1823 if ((smenu==NULL && !on_border) || (smenu && parentMenu(smenu)!=omenu)) {
1824 done = 1;
1825 break;
1828 on_x_edge = x <= 1 || x >= scr->scr_width - 2;
1829 on_y_edge = y <= 1 || y >= scr->scr_height - 2;
1830 on_border = on_x_edge || on_y_edge;
1832 if (!on_border && !jump_back) {
1833 done = 1;
1834 break;
1837 if (menu->timer && (smenu!=menu || (!on_y_edge && !on_x_edge))) {
1838 WMDeleteTimerHandler(menu->timer);
1839 menu->timer = NULL;
1842 if (smenu != NULL)
1843 menu = smenu;
1845 if (!menu->timer)
1846 scrollMenuCallback(menu);
1847 break;
1848 case ButtonPress:
1849 /* True if we push on title, or drag the omenu to other position */
1850 on_title = ev.xbutton.x_root >= omenu->frame_x &&
1851 ev.xbutton.x_root <= omenu->frame_x + MENUW(omenu) &&
1852 ev.xbutton.y_root >= omenu->frame_y &&
1853 ev.xbutton.y_root <= omenu->frame_y + omenu->frame->top_width;
1854 WMHandleEvent(&ev);
1855 smenu = wMenuUnderPointer(scr);
1856 if (smenu == NULL || (smenu && smenu->flags.buttoned && smenu != omenu))
1857 done = 1;
1858 else if (smenu==omenu && on_title) {
1859 jump_back = 0;
1860 done = 1;
1862 break;
1863 case KeyPress:
1864 done = 1;
1865 default:
1866 WMHandleEvent(&ev);
1867 break;
1871 if (menu->timer) {
1872 WMDeleteTimerHandler(menu->timer);
1873 menu->timer = NULL;
1876 if (jump_back) {
1877 _delay *delayer;
1878 if (!omenu->jump_back) {
1879 delayer=wmalloc(sizeof(_delay));
1880 delayer->menu=omenu;
1881 delayer->ox=old_frame_x;
1882 delayer->oy=old_frame_y;
1883 omenu->jump_back=delayer;
1884 scr->flags.jump_back_pending = 1;
1886 else delayer = omenu->jump_back;
1887 WMAddTimerHandler(MENU_JUMP_BACK_DELAY,(WMCallback*)_leaving, delayer);
1893 static void
1894 menuExpose(WObjDescriptor *desc, XEvent *event)
1896 wMenuPaint(desc->parent);
1899 typedef struct {
1900 int *delayed_select;
1901 WMenu *menu;
1902 WMHandlerID magic;
1903 } delay_data;
1906 static void
1907 delaySelection(void *data)
1909 delay_data *d = (delay_data*)data;
1910 int x, y, entry_no;
1911 WMenu *menu;
1913 d->magic = NULL;
1915 menu = findMenu(d->menu->menu->screen_ptr, &x, &y);
1916 if (menu && (d->menu == menu || d->delayed_select)) {
1917 entry_no = getEntryAt(menu, x, y);
1918 selectEntry(menu, entry_no);
1920 if (d->delayed_select)
1921 *(d->delayed_select) = 0;
1925 static void
1926 menuMouseDown(WObjDescriptor *desc, XEvent *event)
1928 XButtonEvent *bev = &event->xbutton;
1929 WMenu *menu = desc->parent;
1930 WMenu *smenu;
1931 WScreen *scr=menu->frame->screen_ptr;
1932 WMenuEntry *entry=NULL;
1933 XEvent ev;
1934 int close_on_exit=0;
1935 int done=0;
1936 int delayed_select = 0;
1937 int entry_no;
1938 int x, y;
1939 int prevx, prevy;
1940 int old_frame_x = 0;
1941 int old_frame_y = 0;
1942 delay_data d_data = {NULL, NULL, NULL};
1944 if (!wPreferences.wrap_menus) {
1945 smenu = parentMenu(menu);
1946 old_frame_x = smenu->frame_x;
1947 old_frame_y = smenu->frame_y;
1948 } else if (event->xbutton.window == menu->frame->core->window) {
1949 /* This is true if the menu was launched with right click on root window */
1950 delayed_select = 1;
1951 d_data.delayed_select = &delayed_select;
1952 d_data.menu = menu;
1953 d_data.magic = WMAddTimerHandler(wPreferences.dblclick_time,
1954 delaySelection, &d_data);
1957 if (menu->flags.inside_handler) {
1958 return;
1960 menu->flags.inside_handler = 1;
1962 wRaiseFrame(menu->frame->core);
1964 close_on_exit = (bev->send_event || menu->flags.brother);
1966 smenu = findMenu(scr, &x, &y);
1967 if (!smenu) {
1968 x = -1;
1969 y = -1;
1970 } else {
1971 menu = smenu;
1974 if (menu->flags.editing) {
1975 goto byebye;
1977 entry_no = getEntryAt(menu, x, y);
1978 if (entry_no>=0) {
1979 entry = menu->entries[entry_no];
1981 if (!close_on_exit && (bev->state & ControlMask) && smenu
1982 && entry->flags.editable) {
1983 editEntry(smenu, entry);
1984 goto byebye;
1985 } else if (bev->state & ControlMask) {
1986 goto byebye;
1989 if (entry->flags.enabled && entry->cascade>=0 && menu->cascades) {
1990 WMenu *submenu = menu->cascades[entry->cascade];
1991 /* map cascade */
1992 if (submenu->flags.mapped && !submenu->flags.buttoned &&
1993 menu->selected_entry!=entry_no) {
1994 wMenuUnmap(submenu);
1996 if (!submenu->flags.mapped && !delayed_select) {
1997 selectEntry(menu, entry_no);
1998 } else if (!submenu->flags.buttoned) {
1999 selectEntry(menu, -1);
2002 } else if (!delayed_select) {
2003 selectEntry(menu, entry_no);
2006 if (!wPreferences.wrap_menus && !wPreferences.scrollable_menus) {
2007 if (!menu->timer)
2008 dragScrollMenuCallback(menu);
2012 #ifdef VIRTUAL_DESKTOP
2013 if (wPreferences.vedge_thickness) {
2014 wWorkspaceLowerEdge(scr);
2016 #endif
2018 prevx = bev->x_root;
2019 prevy = bev->y_root;
2020 while (!done) {
2021 int x, y;
2023 XAllowEvents(dpy, AsyncPointer|SyncPointer, CurrentTime);
2025 WMMaskEvent(dpy, ExposureMask|ButtonMotionMask|ButtonReleaseMask
2026 |ButtonPressMask, &ev);
2027 switch (ev.type) {
2028 case MotionNotify:
2029 smenu = findMenu(scr, &x, &y);
2031 if (smenu == NULL) {
2032 /* moved mouse out of menu */
2034 if (!delayed_select && d_data.magic) {
2035 WMDeleteTimerHandler(d_data.magic);
2036 d_data.magic = NULL;
2038 if (menu==NULL
2039 || (menu->selected_entry>=0
2040 && menu->entries[menu->selected_entry]->cascade>=0)) {
2041 prevx = ev.xmotion.x_root;
2042 prevy = ev.xmotion.y_root;
2044 break;
2046 selectEntry(menu, -1);
2047 menu = smenu;
2048 prevx = ev.xmotion.x_root;
2049 prevy = ev.xmotion.y_root;
2050 break;
2051 } else if (menu && menu!=smenu
2052 && (menu->selected_entry<0
2053 || menu->entries[menu->selected_entry]->cascade<0)) {
2054 selectEntry(menu, -1);
2056 if (!delayed_select && d_data.magic) {
2057 WMDeleteTimerHandler(d_data.magic);
2058 d_data.magic = NULL;
2060 } else {
2062 /* hysteresis for item selection */
2064 /* check if the motion was to the side, indicating that
2065 * the user may want to cross to a submenu */
2066 if (!delayed_select && menu) {
2067 int dx;
2068 Bool moved_to_submenu;/* moved to direction of submenu */
2070 dx = abs(prevx - ev.xmotion.x_root);
2072 moved_to_submenu = False;
2073 if (dx > 0 /* if moved enough to the side */
2074 /* maybe a open submenu */
2075 && menu->selected_entry>=0
2076 /* moving to the right direction */
2077 && (wPreferences.align_menus
2078 || ev.xmotion.y_root >= prevy)) {
2079 int index;
2081 index = menu->entries[menu->selected_entry]->cascade;
2082 if (index>=0) {
2083 if (menu->cascades[index]->frame_x>menu->frame_x) {
2084 if (prevx < ev.xmotion.x_root)
2085 moved_to_submenu = True;
2086 } else {
2087 if (prevx > ev.xmotion.x_root)
2088 moved_to_submenu = True;
2094 if (menu != smenu) {
2095 if (d_data.magic) {
2096 WMDeleteTimerHandler(d_data.magic);
2098 d_data.magic = NULL;
2099 } else if (moved_to_submenu) {
2100 /* while we are moving, postpone the selection */
2101 if (d_data.magic) {
2102 WMDeleteTimerHandler(d_data.magic);
2104 d_data.delayed_select = NULL;
2105 d_data.menu = menu;
2106 d_data.magic = WMAddTimerHandler(MENU_SELECT_DELAY,
2107 delaySelection,
2108 &d_data);
2109 prevx = ev.xmotion.x_root;
2110 prevy = ev.xmotion.y_root;
2111 break;
2112 } else {
2113 if (d_data.magic)
2114 WMDeleteTimerHandler(d_data.magic);
2115 d_data.magic = NULL;
2119 prevx = ev.xmotion.x_root;
2120 prevy = ev.xmotion.y_root;
2121 if (menu!=smenu) {
2122 /* pointer crossed menus */
2123 if (menu && menu->timer) {
2124 WMDeleteTimerHandler(menu->timer);
2125 menu->timer = NULL;
2127 if (smenu)
2128 dragScrollMenuCallback(smenu);
2130 menu = smenu;
2131 if (!menu->timer)
2132 dragScrollMenuCallback(menu);
2134 if (!delayed_select) {
2135 entry_no = getEntryAt(menu, x, y);
2136 if (entry_no>=0) {
2137 entry = menu->entries[entry_no];
2138 if (entry->flags.enabled && entry->cascade>=0 &&
2139 menu->cascades) {
2140 WMenu *submenu = menu->cascades[entry->cascade];
2141 if (submenu->flags.mapped && !submenu->flags.buttoned
2142 && menu->selected_entry!=entry_no) {
2143 wMenuUnmap(submenu);
2147 selectEntry(menu, entry_no);
2149 break;
2151 case ButtonPress:
2152 break;
2154 case ButtonRelease:
2155 if (ev.xbutton.button == event->xbutton.button)
2156 done=1;
2157 break;
2159 case Expose:
2160 WMHandleEvent(&ev);
2161 #ifdef VIRTUAL_DESKTOP
2162 /* since expose will raise edge up.. I need another ugly hack here */
2163 if (wPreferences.vedge_thickness) {
2164 wWorkspaceLowerEdge(scr);
2166 #endif
2167 break;
2171 if (menu && menu->timer) {
2172 WMDeleteTimerHandler(menu->timer);
2173 menu->timer = NULL;
2175 if (d_data.magic!=NULL)
2176 WMDeleteTimerHandler(d_data.magic);
2178 if (menu && menu->selected_entry>=0) {
2179 entry = menu->entries[menu->selected_entry];
2180 if (entry->callback!=NULL && entry->flags.enabled
2181 && entry->cascade < 0) {
2182 /* blink and erase menu selection */
2183 #if (MENU_BLINK_DELAY > 0)
2184 int sel = menu->selected_entry;
2185 int i;
2187 for (i=0; i<MENU_BLINK_COUNT; i++) {
2188 paintEntry(menu, sel, False);
2189 XSync(dpy, 0);
2190 wusleep(MENU_BLINK_DELAY);
2191 paintEntry(menu, sel, True);
2192 XSync(dpy, 0);
2193 wusleep(MENU_BLINK_DELAY);
2195 #endif
2196 /* unmap the menu, it's parents and call the callback */
2197 if (!menu->flags.buttoned &&
2198 (!menu->flags.app_menu||menu->parent!=NULL)) {
2199 closeCascade(menu);
2200 } else {
2201 selectEntry(menu, -1);
2203 (*entry->callback)(menu, entry);
2205 /* If the user double clicks an entry, the entry will
2206 * be executed twice, which is not good for things like
2207 * the root menu. So, ignore any clicks that were generated
2208 * while the entry was being executed */
2209 while (XCheckTypedWindowEvent(dpy, menu->menu->window,
2210 ButtonPress, &ev));
2211 } else if (entry->callback!=NULL && entry->cascade<0) {
2212 selectEntry(menu, -1);
2213 } else {
2214 if (entry->cascade>=0 && menu->cascades
2215 && menu->cascades[entry->cascade]->flags.brother) {
2216 selectEntry(menu, -1);
2221 if (((WMenu*)desc->parent)->flags.brother || close_on_exit || !smenu)
2222 closeCascade(desc->parent);
2224 /* close the cascade windows that should not remain opened */
2225 closeBrotherCascadesOf(desc->parent);
2227 if (!wPreferences.wrap_menus)
2228 wMenuMove(parentMenu(desc->parent), old_frame_x, old_frame_y, True);
2230 byebye:
2231 ((WMenu*)desc->parent)->flags.inside_handler = 0;
2232 #ifdef VIRTUAL_DESKTOP
2233 if (wPreferences.vedge_thickness) {
2234 wWorkspaceRaiseEdge(scr);
2236 #endif
2240 void
2241 wMenuMove(WMenu *menu, int x, int y, int submenus)
2243 WMenu *submenu;
2244 int i;
2246 if (!menu) return;
2248 menu->frame_x = x;
2249 menu->frame_y = y;
2250 XMoveWindow(dpy, menu->frame->core->window, x, y);
2252 if (submenus>0 && menu->selected_entry>=0) {
2253 i = menu->entries[menu->selected_entry]->cascade;
2255 if (i>=0 && menu->cascades) {
2256 submenu = menu->cascades[i];
2257 if (submenu->flags.mapped && !submenu->flags.buttoned) {
2258 if (wPreferences.align_menus) {
2259 wMenuMove(submenu, x + MENUW(menu), y, submenus);
2260 } else {
2261 wMenuMove(submenu, x+ MENUW(menu),
2262 y + submenu->entry_height*menu->selected_entry,
2263 submenus);
2268 if (submenus<0 && menu->parent!=NULL && menu->parent->flags.mapped &&
2269 !menu->parent->flags.buttoned) {
2270 if (wPreferences.align_menus) {
2271 wMenuMove(menu->parent, x - MENUW(menu->parent), y, submenus);
2272 } else {
2273 wMenuMove(menu->parent, x - MENUW(menu->parent), menu->frame_y
2274 - menu->parent->entry_height*menu->parent->selected_entry,
2275 submenus);
2281 static void
2282 changeMenuLevels(WMenu *menu, int lower)
2284 int i;
2286 if (!lower) {
2287 ChangeStackingLevel(menu->frame->core, (!menu->parent ? WMMainMenuLevel
2288 : WMSubmenuLevel));
2289 wRaiseFrame(menu->frame->core);
2290 menu->flags.lowered = 0;
2291 } else {
2292 ChangeStackingLevel(menu->frame->core, WMNormalLevel);
2293 wLowerFrame(menu->frame->core);
2294 menu->flags.lowered = 1;
2296 for (i=0; i<menu->cascade_no; i++) {
2297 if (menu->cascades[i]
2298 && !menu->cascades[i]->flags.buttoned
2299 && menu->cascades[i]->flags.lowered!=lower) {
2300 changeMenuLevels(menu->cascades[i], lower);
2307 static void
2308 menuTitleDoubleClick(WCoreWindow *sender, void *data, XEvent *event)
2310 WMenu *menu = data;
2311 int lower;
2313 if (event->xbutton.state & MOD_MASK) {
2314 if (menu->flags.lowered) {
2315 lower = 0;
2316 } else {
2317 lower = 1;
2319 changeMenuLevels(menu, lower);
2324 static void
2325 menuTitleMouseDown(WCoreWindow *sender, void *data, XEvent *event)
2327 WMenu *menu = data;
2328 WMenu *tmp;
2329 XEvent ev;
2330 int x=menu->frame_x, y=menu->frame_y;
2331 int dx=event->xbutton.x_root, dy=event->xbutton.y_root;
2332 int i, lower;
2333 Bool started;
2335 /* can't touch the menu copy */
2336 if (menu->flags.brother)
2337 return;
2339 if (event->xbutton.button != Button1 && event->xbutton.button != Button2)
2340 return;
2342 if (event->xbutton.state & MOD_MASK) {
2343 wLowerFrame(menu->frame->core);
2344 lower = 1;
2345 } else {
2346 wRaiseFrame(menu->frame->core);
2347 lower = 0;
2349 tmp = menu;
2351 /* lower/raise all submenus */
2352 while (1) {
2353 if (tmp->selected_entry>=0 && tmp->cascades
2354 && tmp->entries[tmp->selected_entry]->cascade>=0) {
2355 tmp = tmp->cascades[tmp->entries[tmp->selected_entry]->cascade];
2356 if (!tmp || !tmp->flags.mapped)
2357 break;
2358 if (lower)
2359 wLowerFrame(tmp->frame->core);
2360 else
2361 wRaiseFrame(tmp->frame->core);
2362 } else {
2363 break;
2367 /* tear off the menu if it's a root menu or a cascade
2368 application menu */
2369 if (!menu->flags.buttoned && !menu->flags.brother
2370 && (!menu->flags.app_menu||menu->parent!=NULL)) {
2371 menu->flags.buttoned=1;
2372 wFrameWindowShowButton(menu->frame, WFF_RIGHT_BUTTON);
2373 if (menu->parent) {
2374 /* turn off selected menu entry in parent menu */
2375 selectEntry(menu->parent, -1);
2377 /* make parent map the copy in place of the original */
2378 for (i=0; i<menu->parent->cascade_no; i++) {
2379 if (menu->parent->cascades[i] == menu) {
2380 menu->parent->cascades[i] = menu->brother;
2381 break;
2387 started = False;
2388 while(1) {
2389 WMMaskEvent(dpy, ButtonMotionMask|ButtonReleaseMask|ButtonPressMask
2390 |ExposureMask, &ev);
2391 switch (ev.type) {
2392 case MotionNotify:
2393 if (started) {
2394 x += ev.xmotion.x_root - dx;
2395 y += ev.xmotion.y_root - dy;
2396 dx = ev.xmotion.x_root;
2397 dy = ev.xmotion.y_root;
2398 wMenuMove(menu, x, y, True);
2399 } else {
2400 if (abs(ev.xmotion.x_root - dx) > MOVE_THRESHOLD
2401 || abs(ev.xmotion.y_root - dy) > MOVE_THRESHOLD) {
2402 started = True;
2403 XGrabPointer(dpy, menu->frame->titlebar->window, False,
2404 ButtonMotionMask|ButtonReleaseMask
2405 |ButtonPressMask,
2406 GrabModeAsync, GrabModeAsync, None,
2407 wCursor[WCUR_MOVE], CurrentTime);
2410 break;
2412 case ButtonPress:
2413 break;
2415 case ButtonRelease:
2416 if (ev.xbutton.button != event->xbutton.button)
2417 break;
2418 XUngrabPointer(dpy, CurrentTime);
2419 return;
2421 default:
2422 WMHandleEvent(&ev);
2423 break;
2429 *----------------------------------------------------------------------
2430 * menuCloseClick--
2431 * Handles mouse click on the close button of menus. The menu is
2432 * closed when the button is clicked.
2434 * Side effects:
2435 * The closed menu is reinserted at it's parent menus
2436 * cascade list.
2437 *----------------------------------------------------------------------
2439 static void
2440 menuCloseClick(WCoreWindow *sender, void *data, XEvent *event)
2442 WMenu *menu = (WMenu*)data;
2443 WMenu *parent = menu->parent;
2444 int i;
2446 if (parent) {
2447 for (i=0; i<parent->cascade_no; i++) {
2448 /* find the entry that points to the copy */
2449 if (parent->cascades[i] == menu->brother) {
2450 /* make it point to the original */
2451 parent->cascades[i] = menu;
2452 menu->parent = parent;
2453 break;
2457 wMenuUnmap(menu);
2461 static void
2462 saveMenuInfo(WMPropList *dict, WMenu *menu, WMPropList *key)
2464 WMPropList *value, *list;
2465 char buffer[256];
2467 snprintf(buffer, sizeof(buffer), "%i,%i", menu->frame_x, menu->frame_y);
2468 value = WMCreatePLString(buffer);
2469 list = WMCreatePLArray(value, NULL);
2470 if (menu->flags.lowered)
2471 WMAddToPLArray(list, WMCreatePLString("lowered"));
2472 WMPutInPLDictionary(dict, key, list);
2473 WMReleasePropList(value);
2474 WMReleasePropList(list);
2478 void
2479 wMenuSaveState(WScreen *scr)
2481 WMPropList *menus, *key;
2482 int save_menus = 0;
2484 menus = WMCreatePLDictionary(NULL, NULL, NULL);
2486 #ifndef LITE
2487 if (scr->switch_menu && scr->switch_menu->flags.buttoned) {
2488 key = WMCreatePLString("SwitchMenu");
2489 saveMenuInfo(menus, scr->switch_menu, key);
2490 WMReleasePropList(key);
2491 save_menus = 1;
2494 if (saveMenuRecurs(menus, scr, scr->root_menu))
2495 save_menus = 1;
2497 #endif /* !LITE */
2498 if (scr->workspace_menu && scr->workspace_menu->flags.buttoned) {
2499 key = WMCreatePLString("WorkspaceMenu");
2500 saveMenuInfo(menus, scr->workspace_menu, key);
2501 WMReleasePropList(key);
2502 save_menus = 1;
2505 if (save_menus) {
2506 key = WMCreatePLString("Menus");
2507 WMPutInPLDictionary(scr->session_state, key, menus);
2508 WMReleasePropList(key);
2510 WMReleasePropList(menus);
2514 #ifndef LITE
2516 static Bool
2517 getMenuPath(WMenu *menu, char *buffer, int bufSize)
2519 Bool ok = True;
2520 int len = 0;
2522 if (!menu->flags.titled || !menu->frame->title[0])
2523 return False;
2525 len = strlen(menu->frame->title);
2526 if (len >= bufSize)
2527 return False;
2529 if (menu->parent) {
2530 ok = getMenuPath(menu->parent, buffer, bufSize - len - 1);
2531 if (!ok)
2532 return False;
2535 strcat(buffer, "\\");
2536 strcat(buffer, menu->frame->title);
2538 return True;
2542 static Bool
2543 saveMenuRecurs(WMPropList *menus, WScreen *scr, WMenu *menu)
2545 WMPropList *key;
2546 int save_menus = 0, i;
2547 char buffer[512];
2548 Bool ok = True;
2551 if (menu->flags.brother)
2552 menu = menu->brother;
2554 if (menu->flags.buttoned && menu != scr->switch_menu) {
2556 buffer[0] = '\0';
2557 ok = getMenuPath(menu, buffer, 510);
2559 if (ok) {
2560 key = WMCreatePLString(buffer);
2561 saveMenuInfo(menus, menu, key);
2562 WMReleasePropList(key);
2563 save_menus = 1;
2567 if (ok) {
2568 for (i = 0; i < menu->cascade_no; i++) {
2569 if (saveMenuRecurs(menus, scr, menu->cascades[i]))
2570 save_menus = 1;
2573 return save_menus;
2575 #endif /* !LITE */
2578 #define COMPLAIN(key) wwarning(_("bad value in menus state info:%s"), key)
2581 static Bool
2582 getMenuInfo(WMPropList *info, int *x, int *y, Bool *lowered)
2584 WMPropList *pos;
2586 *lowered = False;
2588 if (WMIsPLArray(info)) {
2589 WMPropList *flags;
2590 pos = WMGetFromPLArray(info, 0);
2591 flags = WMGetFromPLArray(info, 1);
2592 if (flags != NULL && WMIsPLString(flags) && WMGetFromPLString(flags) != NULL
2593 && strcmp(WMGetFromPLString(flags), "lowered") == 0) {
2594 *lowered = True;
2596 } else {
2597 pos = info;
2600 if (pos != NULL && WMIsPLString(pos)) {
2601 if (sscanf(WMGetFromPLString(pos), "%i,%i", x, y)!=2)
2602 COMPLAIN("Position");
2603 } else {
2604 COMPLAIN("(position, flags...)");
2605 return False;
2608 return True;
2612 static int
2613 restoreMenu(WScreen *scr, WMPropList *menu, int which)
2615 int x, y;
2616 Bool lowered = False;
2617 WMenu *pmenu = NULL;
2619 if (!menu)
2620 return False;
2622 if (!getMenuInfo(menu, &x, &y, &lowered))
2623 return False;
2626 #ifndef LITE
2627 if (which & WSS_SWITCHMENU) {
2628 OpenSwitchMenu(scr, x, y, False);
2629 pmenu = scr->switch_menu;
2631 #endif /* !LITE */
2633 if (pmenu) {
2634 int width = MENUW(pmenu);
2635 int height = MENUH(pmenu);
2637 if (lowered) {
2638 changeMenuLevels(pmenu, True);
2641 x = (x < -width) ? 0 : x;
2642 x = (x > scr->scr_width) ? scr->scr_width - width : x;
2643 y = (y < 0) ? 0 : y;
2644 y = (y > scr->scr_height) ? scr->scr_height - height : y;
2645 wMenuMove(pmenu, x, y, True);
2646 pmenu->flags.buttoned = 1;
2647 wFrameWindowShowButton(pmenu->frame, WFF_RIGHT_BUTTON);
2648 return True;
2650 return False;
2654 #ifndef LITE
2655 static int
2656 restoreMenuRecurs(WScreen *scr, WMPropList *menus, WMenu *menu, char *path)
2658 WMPropList *key, *entry;
2659 char buffer[512];
2660 int i, x, y, res;
2661 Bool lowered;
2663 if (strlen(path) + strlen(menu->frame->title) > 510)
2664 return False;
2666 snprintf(buffer, sizeof(buffer), "%s\\%s", path, menu->frame->title);
2667 key = WMCreatePLString(buffer);
2668 entry = WMGetFromPLDictionary(menus, key);
2669 res = False;
2671 if (entry && getMenuInfo(entry, &x, &y, &lowered)) {
2673 if (!menu->flags.mapped) {
2674 int width = MENUW(menu);
2675 int height = MENUH(menu);
2677 wMenuMapAt(menu, x, y, False);
2679 if (menu->parent) {
2680 /* make parent map the copy in place of the original */
2681 for (i=0; i<menu->parent->cascade_no; i++) {
2682 if (menu->parent->cascades[i] == menu) {
2683 menu->parent->cascades[i] = menu->brother;
2684 break;
2688 if (lowered) {
2689 changeMenuLevels(menu, True);
2691 x = (x < -width) ? 0 : x;
2692 x = (x > scr->scr_width) ? scr->scr_width - width : x;
2693 y = (y < 0) ? 0 : y;
2694 y = (y > scr->scr_height) ? scr->scr_height - height : y;
2695 wMenuMove(menu, x, y, True);
2696 menu->flags.buttoned = 1;
2697 wFrameWindowShowButton(menu->frame, WFF_RIGHT_BUTTON);
2698 res = True;
2702 WMReleasePropList(key);
2704 for (i=0; i<menu->cascade_no; i++) {
2705 if (restoreMenuRecurs(scr, menus, menu->cascades[i], buffer) != False)
2706 res = True;
2709 return res;
2711 #endif /* !LITE */
2714 void
2715 wMenuRestoreState(WScreen *scr)
2717 WMPropList *menus, *menu, *key, *skey;
2719 key = WMCreatePLString("Menus");
2720 menus = WMGetFromPLDictionary(scr->session_state, key);
2721 WMReleasePropList(key);
2723 if (!menus)
2724 return;
2726 /* restore menus */
2728 skey = WMCreatePLString("SwitchMenu");
2729 menu = WMGetFromPLDictionary(menus, skey);
2730 WMReleasePropList(skey);
2731 restoreMenu(scr, menu, WSS_SWITCHMENU);
2733 #ifndef LITE
2734 if (!scr->root_menu) {
2735 OpenRootMenu(scr, scr->scr_width*2, 0, False);
2736 wMenuUnmap(scr->root_menu);
2738 restoreMenuRecurs(scr, menus, scr->root_menu, "");
2739 #endif /* !LITE */
2743 void
2744 OpenWorkspaceMenu(WScreen *scr, int x, int y)
2746 WMenu *menu, *parent;
2747 WMenuEntry *entry;
2749 #ifndef LITE
2750 if (!scr->root_menu) {
2751 OpenRootMenu(scr, scr->scr_width*2, 0, False);
2752 wMenuUnmap(scr->root_menu);
2754 #endif
2755 menu = scr->workspace_menu;
2756 if (menu) {
2757 if (menu->flags.mapped) {
2758 if (!menu->flags.buttoned) {
2759 wMenuUnmap(menu);
2760 parent = menu->parent;
2761 if (parent && parent->selected_entry >= 0) {
2762 entry = parent->entries[parent->selected_entry];
2763 if (parent->cascades[entry->cascade] == menu) {
2764 selectEntry(parent, -1);
2765 wMenuMapAt(menu, x, y, False);
2768 } else {
2769 wRaiseFrame(menu->frame->core);
2770 wMenuMapCopyAt(menu, x, y);
2772 } else {
2773 wMenuMapAt(menu, x, y, False);