- Fixed sloppy focus bug (Pawel S. Veselov <pv76716@druid.SFBay.Sun.COM>)
[wmaker-crm.git] / src / menu.c
blob128b1f8bac6721e0432ff57432fa4cde2013628e
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"
45 #include "xinerama.h"
48 /****** Global Variables ******/
50 extern Cursor wCursor[WCUR_LAST];
52 extern XContext wWinContext;
54 extern WPreferences wPreferences;
56 #define MOD_MASK wPreferences.modifier_mask
58 #define MENU_SCROLL_STEP menuScrollParameters[(int)wPreferences.menu_scroll_speed].steps
59 #define MENU_SCROLL_DELAY menuScrollParameters[(int)wPreferences.menu_scroll_speed].delay
63 #define MENUW(m) ((m)->frame->core->width+2*FRAME_BORDER_WIDTH)
64 #define MENUH(m) ((m)->frame->core->height+2*FRAME_BORDER_WIDTH)
67 /***** Local Stuff ******/
69 static struct {
70 int steps;
71 int delay;
72 } menuScrollParameters[5] = {
73 {MENU_SCROLL_STEPS_UF, MENU_SCROLL_DELAY_UF},
74 {MENU_SCROLL_STEPS_F, MENU_SCROLL_DELAY_F},
75 {MENU_SCROLL_STEPS_M, MENU_SCROLL_DELAY_M},
76 {MENU_SCROLL_STEPS_S, MENU_SCROLL_DELAY_S},
77 {MENU_SCROLL_STEPS_US, MENU_SCROLL_DELAY_US}};
80 static void menuMouseDown(WObjDescriptor *desc, XEvent *event);
81 static void menuExpose(WObjDescriptor *desc, XEvent *event);
83 static void menuTitleDoubleClick(WCoreWindow *sender, void *data, XEvent *event);
84 static void menuTitleMouseDown(WCoreWindow *sender, void *data, XEvent *event);
86 static void menuCloseClick(WCoreWindow *sender, void *data, XEvent *event);
88 static void updateTexture(WMenu *menu);
90 #ifndef LITE
91 static int saveMenuRecurs(WMPropList *menus, WScreen *scr, WMenu *menu);
92 static int restoreMenuRecurs(WScreen *scr, WMPropList *menus, WMenu *menu, char *path);
93 #endif /* !LITE */
95 static void selectEntry(WMenu *menu, int entry_no);
96 static void closeCascade(WMenu *menu);
99 /****** Notification Observers ******/
101 static void
102 appearanceObserver(void *self, WMNotification *notif)
104 WMenu *menu = (WMenu*)self;
105 int flags = (int)WMGetNotificationClientData(notif);
107 if (!menu->flags.realized)
108 return;
110 if (WMGetNotificationName(notif) == WNMenuAppearanceSettingsChanged) {
111 if (flags & WFontSettings) {
112 menu->flags.realized = 0;
113 wMenuRealize(menu);
115 if (flags & WTextureSettings) {
116 if (!menu->flags.brother)
117 updateTexture(menu);
119 if (flags & (WTextureSettings|WColorSettings)) {
120 wMenuPaint(menu);
122 } else if (menu->flags.titled) {
124 if (flags & WFontSettings) {
125 menu->flags.realized = 0;
126 wMenuRealize(menu);
128 if (flags & WTextureSettings) {
129 menu->frame->flags.need_texture_remake = 1;
131 if (flags & (WColorSettings|WTextureSettings)) {
132 wFrameWindowPaint(menu->frame);
137 /************************************/
141 *----------------------------------------------------------------------
142 * wMenuCreate--
143 * Creates a new empty menu with the specified title. If main_menu
144 * is True, the created menu will be a main menu, which has some special
145 * properties such as being placed over other normal menus.
146 * If title is NULL, the menu will have no titlebar.
148 * Returns:
149 * The created menu.
150 *----------------------------------------------------------------------
152 WMenu*
153 wMenuCreate(WScreen *screen, char *title, int main_menu)
155 WMenu *menu;
156 static int brother=0;
157 int tmp, flags;
159 menu = wmalloc(sizeof(WMenu));
161 memset(menu, 0, sizeof(WMenu));
163 #ifdef SINGLE_MENULEVEL
164 tmp = WMSubmenuLevel;
165 #else
166 tmp = (main_menu ? WMMainMenuLevel : WMSubmenuLevel);
167 #endif
169 flags = WFF_SINGLE_STATE|WFF_BORDER;
170 if (title) {
171 flags |= WFF_TITLEBAR|WFF_RIGHT_BUTTON;
172 menu->flags.titled = 1;
174 menu->frame =
175 wFrameWindowCreate(screen, tmp, 8, 2, 1, 1, &wPreferences.menu_title_clearance, flags,
176 screen->menu_title_texture, NULL,
177 screen->menu_title_color,
178 &screen->menu_title_font);
180 menu->frame->core->descriptor.parent = menu;
181 menu->frame->core->descriptor.parent_type = WCLASS_MENU;
182 menu->frame->core->descriptor.handle_mousedown = menuMouseDown;
184 wFrameWindowHideButton(menu->frame, WFF_RIGHT_BUTTON);
186 if (title) {
187 menu->frame->title = wstrdup(title);
190 menu->frame->flags.justification = WTJ_LEFT;
192 menu->frame->rbutton_image = screen->b_pixmaps[WBUT_CLOSE];
194 menu->entry_no = 0;
195 menu->alloced_entries = 0;
196 menu->selected_entry = -1;
197 menu->entries = NULL;
199 menu->frame_x = screen->app_menu_x;
200 menu->frame_y = screen->app_menu_y;
202 menu->frame->child = menu;
204 menu->flags.lowered = 0;
206 /* create borders */
207 if (title) {
208 /* setup object descriptors */
209 menu->frame->on_mousedown_titlebar = menuTitleMouseDown;
210 menu->frame->on_dblclick_titlebar = menuTitleDoubleClick;
213 menu->frame->on_click_right = menuCloseClick;
216 menu->menu = wCoreCreate(menu->frame->core, 0, menu->frame->top_width,
217 menu->frame->core->width, 10);
219 menu->menu->descriptor.parent = menu;
220 menu->menu->descriptor.parent_type = WCLASS_MENU;
221 menu->menu->descriptor.handle_expose = menuExpose;
222 menu->menu->descriptor.handle_mousedown = menuMouseDown;
224 menu->menu_texture_data = None;
226 XMapWindow(dpy, menu->menu->window);
228 XFlush(dpy);
230 if (!brother) {
231 brother = 1;
232 menu->brother = wMenuCreate(screen, title, main_menu);
233 brother = 0;
234 menu->brother->flags.brother = 1;
235 menu->brother->brother = menu;
237 WMAddNotificationObserver(appearanceObserver, menu,
238 WNMenuAppearanceSettingsChanged, menu);
240 WMAddNotificationObserver(appearanceObserver, menu,
241 WNMenuTitleAppearanceSettingsChanged, menu);
244 return menu;
250 WMenu*
251 wMenuCreateForApp(WScreen *screen, char *title, int main_menu)
253 WMenu *menu;
255 menu = wMenuCreate(screen, title, main_menu);
256 if (!menu)
257 return NULL;
258 menu->flags.app_menu = 1;
259 menu->brother->flags.app_menu = 1;
261 return menu;
266 static void
267 insertEntry(WMenu *menu, WMenuEntry *entry, int index)
269 int i;
271 for (i = menu->entry_no-1; i >= index; i--) {
272 menu->entries[i]->order++;
273 menu->entries[i+1] = menu->entries[i];
275 menu->entries[index] = entry;
279 WMenuEntry*
280 wMenuInsertCallback(WMenu *menu, int index, char *text,
281 void (*callback)(WMenu *menu, WMenuEntry *entry),
282 void *clientdata)
284 WMenuEntry *entry;
286 menu->flags.realized = 0;
287 menu->brother->flags.realized = 0;
289 /* reallocate array if it's too small */
290 if (menu->entry_no >= menu->alloced_entries) {
291 void *tmp;
293 tmp = wrealloc(menu->entries,
294 sizeof(WMenuEntry)*(menu->alloced_entries+5));
296 menu->entries = tmp;
297 menu->alloced_entries += 5;
299 menu->brother->entries = tmp;
300 menu->brother->alloced_entries = menu->alloced_entries;
302 entry = wmalloc(sizeof(WMenuEntry));
303 memset(entry, 0, sizeof(WMenuEntry));
304 entry->flags.enabled = 1;
305 entry->text = wstrdup(text);
306 entry->cascade = -1;
307 entry->clientdata = clientdata;
308 entry->callback = callback;
309 if (index<0 || index>=menu->entry_no) {
310 entry->order = menu->entry_no;
311 menu->entries[menu->entry_no] = entry;
312 } else {
313 entry->order = index;
314 insertEntry(menu, entry, index);
317 menu->entry_no++;
318 menu->brother->entry_no = menu->entry_no;
320 return entry;
325 void
326 wMenuEntrySetCascade(WMenu *menu, WMenuEntry *entry, WMenu *cascade)
328 WMenu *brother = menu->brother;
329 int i, done;
331 assert(menu->flags.brother==0);
333 if (entry->cascade>=0) {
334 menu->flags.realized = 0;
335 brother->flags.realized = 0;
338 cascade->parent = menu;
340 cascade->brother->parent = brother;
342 done = 0;
343 for (i=0; i<menu->cascade_no; i++) {
344 if (menu->cascades[i]==NULL) {
345 menu->cascades[i] = cascade;
346 brother->cascades[i] = cascade->brother;
347 done = 1;
348 entry->cascade = i;
349 break;
352 if (!done) {
353 entry->cascade = menu->cascade_no;
355 menu->cascades = wrealloc(menu->cascades,
356 sizeof(WMenu)*(menu->cascade_no+1));
357 menu->cascades[menu->cascade_no++] = cascade;
360 brother->cascades = wrealloc(brother->cascades,
361 sizeof(WMenu)*(brother->cascade_no+1));
362 brother->cascades[brother->cascade_no++] = cascade->brother;
366 if (menu->flags.lowered) {
368 cascade->flags.lowered = 1;
369 ChangeStackingLevel(cascade->frame->core, WMNormalLevel);
371 cascade->brother->flags.lowered = 1;
372 ChangeStackingLevel(cascade->brother->frame->core, WMNormalLevel);
375 if (!menu->flags.realized)
376 wMenuRealize(menu);
380 void
381 wMenuEntryRemoveCascade(WMenu *menu, WMenuEntry *entry)
383 assert(menu->flags.brother==0);
385 /* destroy cascade menu */
386 if (entry->cascade>=0 && menu->cascades
387 && menu->cascades[entry->cascade]!=NULL) {
389 wMenuDestroy(menu->cascades[entry->cascade], True);
391 menu->cascades[entry->cascade] = NULL;
392 menu->brother->cascades[entry->cascade] = NULL;
394 entry->cascade = -1;
399 void
400 wMenuRemoveItem(WMenu *menu, int index)
402 int i;
404 if (menu->flags.brother) {
405 wMenuRemoveItem(menu->brother, index);
406 return;
409 if (index>=menu->entry_no) return;
411 /* destroy cascade menu */
412 wMenuEntryRemoveCascade(menu, menu->entries[index]);
414 /* destroy unshared data */
416 if (menu->entries[index]->text)
417 wfree(menu->entries[index]->text);
419 if (menu->entries[index]->rtext)
420 wfree(menu->entries[index]->rtext);
422 if (menu->entries[index]->free_cdata && menu->entries[index]->clientdata)
423 (*menu->entries[index]->free_cdata)(menu->entries[index]->clientdata);
425 wfree(menu->entries[index]);
427 for (i=index; i<menu->entry_no-1; i++) {
428 menu->entries[i+1]->order--;
429 menu->entries[i]=menu->entries[i+1];
431 menu->entry_no--;
432 menu->brother->entry_no--;
436 static Pixmap
437 renderTexture(WMenu *menu)
439 RImage *img;
440 Pixmap pix;
441 int i;
442 RColor light;
443 RColor dark;
444 RColor mid;
445 WScreen *scr = menu->menu->screen_ptr;
446 WTexture *texture = scr->menu_item_texture;
448 if (wPreferences.menu_style == MS_NORMAL) {
449 img = wTextureRenderImage(texture, menu->menu->width,
450 menu->entry_height, WREL_MENUENTRY);
451 } else {
452 img = wTextureRenderImage(texture, menu->menu->width,
453 menu->menu->height+1, WREL_MENUENTRY);
455 if (!img) {
456 wwarning(_("could not render texture: %s"),
457 RMessageForError(RErrorCode));
459 return None;
462 if (wPreferences.menu_style == MS_SINGLE_TEXTURE) {
463 light.alpha = 0;
464 light.red = light.green = light.blue = 80;
466 dark.alpha = 255;
467 dark.red = dark.green = dark.blue = 0;
469 mid.alpha = 0;
470 mid.red = mid.green = mid.blue = 40;
472 for (i = 1; i < menu->entry_no; i++) {
473 ROperateLine(img, RSubtractOperation, 0, i*menu->entry_height-2,
474 menu->menu->width-1, i*menu->entry_height-2, &mid);
476 RDrawLine(img, 0, i*menu->entry_height-1,
477 menu->menu->width-1, i*menu->entry_height-1, &dark);
479 ROperateLine(img, RAddOperation, 0, i*menu->entry_height,
480 menu->menu->width-1, i*menu->entry_height,
481 &light);
484 if (!RConvertImage(scr->rcontext, img, &pix)) {
485 wwarning(_("error rendering image:%s"), RMessageForError(RErrorCode));
487 RReleaseImage(img);
489 return pix;
493 static void
494 updateTexture(WMenu *menu)
496 WScreen *scr = menu->menu->screen_ptr;
498 /* setup background texture */
499 if (scr->menu_item_texture->any.type != WTEX_SOLID) {
500 if (!menu->flags.brother) {
501 FREE_PIXMAP(menu->menu_texture_data);
503 menu->menu_texture_data = renderTexture(menu);
505 XSetWindowBackgroundPixmap(dpy, menu->menu->window,
506 menu->menu_texture_data);
507 XClearWindow(dpy, menu->menu->window);
509 XSetWindowBackgroundPixmap(dpy, menu->brother->menu->window,
510 menu->menu_texture_data);
511 XClearWindow(dpy, menu->brother->menu->window);
513 } else {
514 XSetWindowBackground(dpy, menu->menu->window,
515 scr->menu_item_texture->any.color.pixel);
516 XClearWindow(dpy, menu->menu->window);
521 void
522 wMenuRealize(WMenu *menu)
524 int i;
525 int width, rwidth, mrwidth, mwidth;
526 int theight, twidth, eheight;
527 WScreen *scr = menu->frame->screen_ptr;
528 static int brother_done=0;
529 int flags;
531 if (!brother_done) {
532 brother_done = 1;
533 wMenuRealize(menu->brother);
534 brother_done = 0;
537 flags = WFF_SINGLE_STATE|WFF_BORDER;
538 if (menu->flags.titled)
539 flags |= WFF_TITLEBAR|WFF_RIGHT_BUTTON;
541 wFrameWindowUpdateBorders(menu->frame, flags);
543 if (menu->flags.titled) {
544 twidth = WMWidthOfString(scr->menu_title_font, menu->frame->title,
545 strlen(menu->frame->title));
546 theight = menu->frame->top_width;
547 twidth += theight + (wPreferences.new_style ? 16 : 8);
548 } else {
549 twidth = 0;
550 theight = 0;
552 eheight = WMFontHeight(scr->menu_entry_font) + 6 + wPreferences.menu_text_clearance * 2;
553 menu->entry_height = eheight;
554 mrwidth = 0;
555 mwidth = 0;
556 for (i=0; i<menu->entry_no; i++) {
557 char *text;
559 /* search widest text */
560 text = menu->entries[i]->text;
561 width = WMWidthOfString(scr->menu_entry_font, text, strlen(text))+10;
563 if (menu->entries[i]->flags.indicator) {
564 width += MENU_INDICATOR_SPACE;
567 if (width > mwidth)
568 mwidth = width;
570 /* search widest text on right */
571 text = menu->entries[i]->rtext;
572 if (text)
573 rwidth = WMWidthOfString(scr->menu_entry_font, text, strlen(text))
574 + 5;
575 else if (menu->entries[i]->cascade>=0)
576 rwidth = 16;
577 else
578 rwidth = 4;
580 if (rwidth > mrwidth)
581 mrwidth = rwidth;
583 mwidth += mrwidth;
585 if (mwidth < twidth)
586 mwidth = twidth;
589 wCoreConfigure(menu->menu, 0, theight, mwidth, menu->entry_no*eheight -1);
591 wFrameWindowResize(menu->frame, mwidth, menu->entry_no*eheight-1
592 + menu->frame->top_width + menu->frame->bottom_width);
595 updateTexture(menu);
597 menu->flags.realized = 1;
599 if (menu->flags.mapped)
600 wMenuPaint(menu);
601 if (menu->brother->flags.mapped)
602 wMenuPaint(menu->brother);
606 void
607 wMenuDestroy(WMenu *menu, int recurse)
609 int i;
611 WMRemoveNotificationObserver(menu);
613 /* remove any pending timers */
614 if (menu->timer)
615 WMDeleteTimerHandler(menu->timer);
616 menu->timer = NULL;
618 /* call destroy handler */
619 if (menu->on_destroy)
620 (*menu->on_destroy)(menu);
622 /* Destroy items if this menu own them. If this is the "brother" menu,
623 * leave them alone as it is shared by them.
625 if (!menu->flags.brother) {
626 for (i=0; i<menu->entry_no; i++) {
628 wfree(menu->entries[i]->text);
630 if (menu->entries[i]->rtext)
631 wfree(menu->entries[i]->rtext);
632 #ifdef USER_MENU
634 if (menu->entries[i]->instances){
635 WMReleasePropList(menu->entries[i]->instances);
637 #endif /* USER_MENU */
639 if (menu->entries[i]->free_cdata && menu->entries[i]->clientdata) {
640 (*menu->entries[i]->free_cdata)(menu->entries[i]->clientdata);
642 wfree(menu->entries[i]);
645 if (recurse) {
646 for (i=0; i<menu->cascade_no; i++) {
647 if (menu->cascades[i]) {
648 if (menu->cascades[i]->flags.brother)
649 wMenuDestroy(menu->cascades[i]->brother, recurse);
650 else
651 wMenuDestroy(menu->cascades[i], recurse);
656 if (menu->entries)
657 wfree(menu->entries);
661 FREE_PIXMAP(menu->menu_texture_data);
663 if (menu->cascades)
664 wfree(menu->cascades);
666 wCoreDestroy(menu->menu);
667 wFrameWindowDestroy(menu->frame);
669 /* destroy copy of this menu */
670 if (!menu->flags.brother && menu->brother)
671 wMenuDestroy(menu->brother, False);
673 wfree(menu);
677 #define F_NORMAL 0
678 #define F_TOP 1
679 #define F_BOTTOM 2
680 #define F_NONE 3
682 static void
683 drawFrame(WScreen *scr, Drawable win, int y, int w, int h, int type)
685 XSegment segs[2];
686 int i;
688 i = 0;
689 segs[i].x1 = segs[i].x2 = w-1;
690 segs[i].y1 = y;
691 segs[i].y2 = y + h - 1;
692 i++;
693 if (type != F_TOP && type != F_NONE) {
694 segs[i].x1 = 1;
695 segs[i].y1 = segs[i].y2 = y + h-2;
696 segs[i].x2 = w-1;
697 i++;
699 XDrawSegments(dpy, win, scr->menu_item_auxtexture->dim_gc, segs, i);
701 i = 0;
702 segs[i].x1 = 0;
703 segs[i].y1 = y;
704 segs[i].x2 = 0;
705 segs[i].y2 = y + h - 1;
706 i++;
707 if (type != F_BOTTOM && type != F_NONE) {
708 segs[i].x1 = 0;
709 segs[i].y1 = y;
710 segs[i].x2 = w-1;
711 segs[i].y2 = y;
712 i++;
714 XDrawSegments(dpy, win, scr->menu_item_auxtexture->light_gc, segs, i);
716 if (type != F_TOP && type != F_NONE)
717 XDrawLine(dpy, win, scr->menu_item_auxtexture->dark_gc, 0, y+h-1,
718 w-1, y+h-1);
722 static void
723 paintEntry(WMenu *menu, int index, int selected)
725 WScreen *scr=menu->frame->screen_ptr;
726 Window win = menu->menu->window;
727 WMenuEntry *entry=menu->entries[index];
728 GC light, dim, dark;
729 WMColor *color;
730 int x, y, w, h, tw;
731 int type;
733 if (!menu->flags.realized) return;
734 h = menu->entry_height;
735 w = menu->menu->width;
736 y = index * h;
738 light = scr->menu_item_auxtexture->light_gc;
739 dim = scr->menu_item_auxtexture->dim_gc;
740 dark = scr->menu_item_auxtexture->dark_gc;
742 if (wPreferences.menu_style == MS_FLAT && menu->entry_no > 1) {
743 if (index == 0)
744 type = F_TOP;
745 else if (index == menu->entry_no - 1)
746 type = F_BOTTOM;
747 else
748 type = F_NONE;
749 } else {
750 type = F_NORMAL;
753 /* paint background */
754 if (selected) {
755 XFillRectangle(dpy, win, WMColorGC(scr->select_color), 1, y+1, w-2, h-3);
756 if (scr->menu_item_texture->any.type == WTEX_SOLID)
757 drawFrame(scr, win, y, w, h, type);
758 } else {
759 if (scr->menu_item_texture->any.type == WTEX_SOLID) {
760 XClearArea(dpy, win, 0, y + 1, w - 1, h - 3, False);
761 /* draw the frame */
762 drawFrame(scr, win, y, w, h, type);
763 } else {
764 XClearArea(dpy, win, 0, y, w, h, False);
768 if (selected) {
769 if (entry->flags.enabled)
770 color = scr->select_text_color;
771 else
772 color = scr->dtext_color;
773 } else if (!entry->flags.enabled) {
774 color = scr->dtext_color;
775 } else {
776 color = scr->mtext_color;
778 /* draw text */
779 x = 5;
780 if (entry->flags.indicator)
781 x += MENU_INDICATOR_SPACE + 2;
783 WMDrawString(scr->wmscreen, win, color, scr->menu_entry_font,
784 x, 3 + y + wPreferences.menu_text_clearance, entry->text, strlen(entry->text));
786 if (entry->cascade>=0) {
787 /* draw the cascade indicator */
788 XDrawLine(dpy,win,dim, w-11, y+6, w-6, y+h/2-1);
789 XDrawLine(dpy,win,light, w-11, y+h-8, w-6, y+h/2-1);
790 XDrawLine(dpy,win,dark, w-12, y+6, w-12, y+h-8);
793 /* draw indicator */
794 if (entry->flags.indicator && entry->flags.indicator_on) {
795 int iw, ih;
796 WPixmap *indicator;
799 switch (entry->flags.indicator_type) {
800 case MI_CHECK:
801 indicator = scr->menu_check_indicator;
802 break;
803 case MI_MINIWINDOW:
804 indicator = scr->menu_mini_indicator;
805 break;
806 case MI_HIDDEN:
807 indicator = scr->menu_hide_indicator;
808 break;
809 case MI_SHADED:
810 indicator = scr->menu_shade_indicator;
811 break;
812 case MI_DIAMOND:
813 default:
814 indicator = scr->menu_radio_indicator;
815 break;
818 iw = indicator->width;
819 ih = indicator->height;
820 XSetClipMask(dpy, scr->copy_gc, indicator->mask);
821 XSetClipOrigin(dpy, scr->copy_gc, 5, y+(h-ih)/2);
822 if (selected) {
823 if (entry->flags.enabled) {
824 XSetForeground(dpy, scr->copy_gc, WMColorPixel(scr->select_text_color));
825 } else {
826 XSetForeground(dpy, scr->copy_gc, WMColorPixel(scr->dtext_color));
828 } else {
829 if (entry->flags.enabled) {
830 XSetForeground(dpy, scr->copy_gc, WMColorPixel(scr->mtext_color));
831 } else {
832 XSetForeground(dpy, scr->copy_gc, WMColorPixel(scr->dtext_color));
835 XFillRectangle(dpy, win, scr->copy_gc, 5, y+(h-ih)/2, iw, ih);
837 XCopyArea(dpy, indicator->image, win, scr->copy_gc, 0, 0,
838 iw, ih, 5, y+(h-ih)/2);
840 XSetClipOrigin(dpy, scr->copy_gc, 0, 0);
843 /* draw right text */
845 if (entry->rtext && entry->cascade<0) {
846 tw = WMWidthOfString(scr->menu_entry_font, entry->rtext,
847 strlen(entry->rtext));
849 WMDrawString(scr->wmscreen, win, color, scr->menu_entry_font, w-6-tw,
850 y + 3 + wPreferences.menu_text_clearance, entry->rtext, strlen(entry->rtext));
855 static void
856 move_menus(WMenu *menu, int x, int y)
858 while (menu->parent) {
859 menu = menu->parent;
860 x -= MENUW(menu);
861 if (!wPreferences.align_menus && menu->selected_entry>=0) {
862 y -= menu->selected_entry*menu->entry_height;
865 wMenuMove(menu, x, y, True);
868 static void
869 makeVisible(WMenu *menu)
871 WScreen *scr = menu->frame->screen_ptr;
872 int x1, y1, x2, y2, new_x, new_y, move;
873 WMRect rect = wGetRectForHead(scr, wGetHeadForPointerLocation(scr));
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;
888 if (x1 < rect.pos.x) {
889 new_x = rect.pos.x;
890 move = 1;
891 } else if (x2 >= rect.pos.x + rect.size.width) {
892 new_x = rect.pos.x + rect.size.width - MENUW(menu) - 1;
893 move = 1;
896 if (y1 < rect.pos.y) {
897 new_y = rect.pos.y;
898 move = 1;
899 } else if (y2 >= rect.pos.y + rect.size.height) {
900 new_y = rect.pos.y + rect.size.height - menu->entry_height - 1;
901 move = 1;
904 new_y = new_y - menu->frame->top_width
905 - menu->selected_entry*menu->entry_height;
906 move_menus(menu, new_x, new_y);
910 static int
911 check_key(WMenu *menu, XKeyEvent *event)
913 int i, ch, s;
914 char buffer[32];
916 if (XLookupString(event, buffer, 32, NULL, NULL)<1)
917 return -1;
919 ch = toupper(buffer[0]);
921 s = (menu->selected_entry>=0 ? menu->selected_entry+1 : 0);
923 again:
924 for (i=s; i<menu->entry_no; i++) {
925 if (ch==toupper(menu->entries[i]->text[0])) {
926 return i;
929 /* no match. Retry from start, if previous started from a selected entry */
930 if (s!=0) {
931 s = 0;
932 goto again;
934 return -1;
938 static int
939 keyboardMenu(WMenu *menu)
941 XEvent event;
942 KeySym ksym=NoSymbol;
943 int done=0;
944 int index;
945 WMenuEntry *entry;
946 int old_pos_x = menu->frame_x;
947 int old_pos_y = menu->frame_y;
948 int new_x = old_pos_x, new_y = old_pos_y;
949 WMRect rect = wGetRectForHead(menu->frame->screen_ptr,
950 wGetHeadForPointerLocation(menu->frame->screen_ptr));
952 if (menu->flags.editing)
953 return False;
956 XGrabKeyboard(dpy, menu->frame->core->window, True, GrabModeAsync,
957 GrabModeAsync, CurrentTime);
960 if (menu->frame_y+menu->frame->top_width >= rect.pos.y + rect.size.height)
961 new_y = rect.pos.y + rect.size.height - menu->frame->top_width;
963 if (menu->frame_x+MENUW(menu) >= rect.pos.x + rect.size.width)
964 new_x = rect.pos.x + rect.size.width - MENUW(menu) - 1;
966 move_menus(menu, new_x, new_y);
968 while (!done && menu->flags.mapped) {
969 XAllowEvents(dpy, AsyncKeyboard, CurrentTime);
970 WMMaskEvent(dpy, ExposureMask|ButtonMotionMask|ButtonPressMask
971 |ButtonReleaseMask|KeyPressMask|KeyReleaseMask
972 |SubstructureNotifyMask, &event);
974 switch (event.type) {
975 case KeyPress:
976 ksym = XLookupKeysym(&event.xkey, 0);
977 switch (ksym) {
978 case XK_Escape:
979 done = 1;
980 break;
982 case XK_Home:
983 #ifdef XK_KP_Home
984 case XK_KP_Home:
985 #endif
986 selectEntry(menu, 0);
987 makeVisible(menu);
988 break;
990 case XK_End:
991 #ifdef XK_KP_End
992 case XK_KP_End:
993 #endif
994 selectEntry(menu, menu->entry_no-1);
995 makeVisible(menu);
996 break;
998 case XK_Up:
999 #ifdef ARROWLESS_KBD
1000 case XK_k:
1001 #endif
1002 #ifdef XK_KP_Up
1003 case XK_KP_Up:
1004 #endif
1005 if (menu->selected_entry <= 0)
1006 selectEntry(menu, menu->entry_no-1);
1007 else
1008 selectEntry(menu, menu->selected_entry-1);
1009 makeVisible(menu);
1010 break;
1012 case XK_Down:
1013 #ifdef ARROWLESS_KBD
1014 case XK_j:
1015 #endif
1016 #ifdef XK_KP_Down
1017 case XK_KP_Down:
1018 #endif
1019 if (menu->selected_entry<0)
1020 selectEntry(menu, 0);
1021 else if (menu->selected_entry == menu->entry_no-1)
1022 selectEntry(menu, 0);
1023 else if (menu->selected_entry < menu->entry_no-1)
1024 selectEntry(menu, menu->selected_entry+1);
1025 makeVisible(menu);
1026 break;
1028 case XK_Right:
1029 #ifdef ARROWLESS_KBD
1030 case XK_l:
1031 #endif
1032 #ifdef XK_KP_Right
1033 case XK_KP_Right:
1034 #endif
1035 if (menu->selected_entry>=0) {
1036 WMenuEntry *entry;
1037 entry = menu->entries[menu->selected_entry];
1039 if (entry->cascade >= 0 && menu->cascades
1040 && menu->cascades[entry->cascade]->entry_no > 0) {
1042 XUngrabKeyboard(dpy, CurrentTime);
1044 selectEntry(menu->cascades[entry->cascade], 0);
1045 if (!keyboardMenu(menu->cascades[entry->cascade]))
1046 done = 1;
1048 XGrabKeyboard(dpy, menu->frame->core->window, True,
1049 GrabModeAsync, GrabModeAsync,
1050 CurrentTime);
1053 break;
1055 case XK_Left:
1056 #ifdef ARROWLESS_KBD
1057 case XK_h:
1058 #endif
1059 #ifdef XK_KP_Left
1060 case XK_KP_Left:
1061 #endif
1062 if (menu->parent!=NULL && menu->parent->selected_entry>=0) {
1063 selectEntry(menu, -1);
1064 move_menus(menu, old_pos_x, old_pos_y);
1065 return True;
1067 break;
1069 case XK_Return:
1070 done = 2;
1071 break;
1073 default:
1074 index = check_key(menu, &event.xkey);
1075 if (index>=0) {
1076 selectEntry(menu, index);
1079 break;
1081 default:
1082 if (event.type==ButtonPress)
1083 done = 1;
1085 WMHandleEvent(&event);
1089 XUngrabKeyboard(dpy, CurrentTime);
1091 if (done==2 && menu->selected_entry>=0) {
1092 entry = menu->entries[menu->selected_entry];
1093 } else {
1094 entry = NULL;
1097 if (entry && entry->callback!=NULL && entry->flags.enabled
1098 && entry->cascade < 0) {
1099 #if (MENU_BLINK_COUNT > 0)
1100 int sel = menu->selected_entry;
1101 int i;
1103 for (i=0; i<MENU_BLINK_COUNT; i++) {
1104 paintEntry(menu, sel, False);
1105 XSync(dpy, 0);
1106 wusleep(MENU_BLINK_DELAY);
1107 paintEntry(menu, sel, True);
1108 XSync(dpy, 0);
1109 wusleep(MENU_BLINK_DELAY);
1111 #endif
1112 selectEntry(menu, -1);
1114 if (!menu->flags.buttoned) {
1115 wMenuUnmap(menu);
1116 move_menus(menu, old_pos_x, old_pos_y);
1118 closeCascade(menu);
1120 (*entry->callback)(menu, entry);
1121 } else {
1122 if (!menu->flags.buttoned) {
1123 wMenuUnmap(menu);
1124 move_menus(menu, old_pos_x, old_pos_y);
1126 selectEntry(menu, -1);
1130 /* returns True if returning from a submenu to a parent menu,
1131 * False if exiting from menu */
1132 return False;
1136 void
1137 wMenuMapAt(WMenu *menu, int x, int y, int keyboard)
1139 WMRect rect = wGetRectForHead(menu->frame->screen_ptr,
1140 wGetHeadForPointerLocation(menu->frame->screen_ptr));
1142 if (!menu->flags.realized) {
1143 menu->flags.realized=1;
1144 wMenuRealize(menu);
1146 if (!menu->flags.mapped) {
1147 if (wPreferences.wrap_menus) {
1148 if (x<rect.pos.x) x = rect.pos.x;
1149 if (y<rect.pos.y) y = rect.pos.y;
1150 if (x+MENUW(menu) > rect.pos.x + rect.size.width)
1151 x = rect.pos.x + rect.size.width - MENUW(menu);
1152 if (y+MENUH(menu) > rect.pos.y + rect.size.height)
1153 y = rect.pos.y + rect.size.height - MENUH(menu);
1156 XMoveWindow(dpy, menu->frame->core->window, x, y);
1157 menu->frame_x = x;
1158 menu->frame_y = y;
1159 XMapWindow(dpy, menu->frame->core->window);
1160 wRaiseFrame(menu->frame->core);
1161 menu->flags.mapped = 1;
1162 } else {
1163 selectEntry(menu, 0);
1166 if (keyboard)
1167 keyboardMenu(menu);
1171 void
1172 wMenuMap(WMenu *menu)
1174 if (!menu->flags.realized) {
1175 menu->flags.realized=1;
1176 wMenuRealize(menu);
1178 if (menu->flags.app_menu && menu->parent==NULL) {
1179 menu->frame_x = menu->frame->screen_ptr->app_menu_x;
1180 menu->frame_y = menu->frame->screen_ptr->app_menu_y;
1181 XMoveWindow(dpy, menu->frame->core->window, menu->frame_x, menu->frame_y);
1183 XMapWindow(dpy, menu->frame->core->window);
1184 wRaiseFrame(menu->frame->core);
1185 menu->flags.mapped = 1;
1189 void
1190 wMenuUnmap(WMenu *menu)
1192 int i;
1194 XUnmapWindow(dpy, menu->frame->core->window);
1195 if (menu->flags.titled && menu->flags.buttoned) {
1196 wFrameWindowHideButton(menu->frame, WFF_RIGHT_BUTTON);
1198 menu->flags.buttoned = 0;
1199 menu->flags.mapped = 0;
1200 menu->flags.open_to_left = 0;
1202 for (i=0; i<menu->cascade_no; i++) {
1203 if (menu->cascades[i]!=NULL
1204 && menu->cascades[i]->flags.mapped
1205 && !menu->cascades[i]->flags.buttoned) {
1207 wMenuUnmap(menu->cascades[i]);
1210 menu->selected_entry = -1;
1215 void
1216 wMenuPaint(WMenu *menu)
1218 int i;
1220 if (!menu->flags.mapped) {
1221 return;
1224 /* paint entries */
1225 for (i=0; i<menu->entry_no; i++) {
1226 paintEntry(menu, i, i==menu->selected_entry);
1231 void
1232 wMenuSetEnabled(WMenu *menu, int index, int enable)
1234 if (index>=menu->entry_no) return;
1235 menu->entries[index]->flags.enabled=enable;
1236 paintEntry(menu, index, index==menu->selected_entry);
1237 paintEntry(menu->brother, index, index==menu->selected_entry);
1241 /* ====================================================================== */
1244 static void
1245 editEntry(WMenu *menu, WMenuEntry *entry)
1247 WTextInput *text;
1248 XEvent event;
1249 WObjDescriptor *desc;
1250 char *t;
1251 int done = 0;
1252 Window old_focus;
1253 int old_revert;
1255 menu->flags.editing = 1;
1257 text = wTextCreate(menu->menu, 1, menu->entry_height * entry->order,
1258 menu->menu->width - 2, menu->entry_height - 1);
1260 wTextPutText(text, entry->text);
1261 XGetInputFocus(dpy, &old_focus, &old_revert);
1262 XSetInputFocus(dpy, text->core->window, RevertToNone, CurrentTime);
1264 if (XGrabKeyboard(dpy, text->core->window, True, GrabModeAsync,
1265 GrabModeAsync, CurrentTime)!=GrabSuccess) {
1266 wwarning(_("could not grab keyboard"));
1267 wTextDestroy(text);
1269 wSetFocusTo(menu->frame->screen_ptr,
1270 menu->frame->screen_ptr->focused_window);
1271 return;
1275 while (!done && !text->done) {
1276 XSync(dpy, 0);
1277 XAllowEvents(dpy, AsyncKeyboard|AsyncPointer, CurrentTime);
1278 XSync(dpy, 0);
1279 WMNextEvent(dpy, &event);
1281 if (XFindContext(dpy, event.xany.window, wWinContext,
1282 (XPointer *)&desc)==XCNOENT)
1283 desc = NULL;
1285 if ((desc != NULL) && (desc->handle_anything != NULL)) {
1287 (*desc->handle_anything)(desc, &event);
1289 } else {
1290 switch (event.type) {
1291 case ButtonPress:
1292 XAllowEvents(dpy, ReplayPointer, CurrentTime);
1293 done = 1;
1295 default:
1296 WMHandleEvent(&event);
1297 break;
1302 XSetInputFocus(dpy, old_focus, old_revert, CurrentTime);
1304 wSetFocusTo(menu->frame->screen_ptr,
1305 menu->frame->screen_ptr->focused_window);
1308 t = wTextGetText(text);
1309 /* if !t, the user has canceled editing */
1310 if (t) {
1311 if (entry->text)
1312 wfree(entry->text);
1313 entry->text = wstrdup(t);
1315 menu->flags.realized = 0;
1317 wTextDestroy(text);
1319 XUngrabKeyboard(dpy, CurrentTime);
1321 if (t && menu->on_edit)
1322 (*menu->on_edit)(menu, entry);
1324 menu->flags.editing = 0;
1326 if (!menu->flags.realized)
1327 wMenuRealize(menu);
1331 static void
1332 selectEntry(WMenu *menu, int entry_no)
1334 WMenuEntry *entry;
1335 WMenu *submenu;
1336 int old_entry;
1338 if (menu->entries==NULL)
1339 return;
1341 if (entry_no >= menu->entry_no)
1342 return;
1344 old_entry = menu->selected_entry;
1345 menu->selected_entry = entry_no;
1347 if (old_entry!=entry_no) {
1349 /* unselect previous entry */
1350 if (old_entry>=0) {
1351 paintEntry(menu, old_entry, False);
1352 entry = menu->entries[old_entry];
1354 /* unmap cascade */
1355 if (entry->cascade>=0 && menu->cascades) {
1356 if (!menu->cascades[entry->cascade]->flags.buttoned) {
1357 wMenuUnmap(menu->cascades[entry->cascade]);
1362 if (entry_no<0) {
1363 menu->selected_entry = -1;
1364 return;
1366 entry = menu->entries[entry_no];
1368 if (entry->cascade>=0 && menu->cascades && entry->flags.enabled) {
1369 /* Callback for when the submenu is opened.
1371 submenu = menu->cascades[entry->cascade];
1372 if (submenu && submenu->flags.brother)
1373 submenu = submenu->brother;
1375 if (entry->callback) {
1376 /* Only call the callback if the submenu is not yet mapped.
1378 if (menu->flags.brother) {
1379 if (!submenu || !submenu->flags.mapped)
1380 (*entry->callback)(menu->brother, entry);
1381 } else {
1382 if (!submenu || !submenu->flags.buttoned)
1383 (*entry->callback)(menu, entry);
1387 /* the submenu menu might have changed */
1388 submenu = menu->cascades[entry->cascade];
1390 /* map cascade */
1391 if (!submenu->flags.mapped) {
1392 int x, y;
1394 if (!submenu->flags.realized)
1395 wMenuRealize(submenu);
1396 if (wPreferences.wrap_menus) {
1397 if (menu->flags.open_to_left)
1398 submenu->flags.open_to_left = 1;
1400 if (submenu->flags.open_to_left) {
1401 x = menu->frame_x - MENUW(submenu);
1402 if (x<0) {
1403 x = 0;
1404 submenu->flags.open_to_left = 0;
1406 } else {
1407 x = menu->frame_x + MENUW(menu);
1409 if (x + MENUW(submenu)
1410 >= menu->frame->screen_ptr->scr_width) {
1412 x = menu->frame_x - MENUW(submenu);
1413 submenu->flags.open_to_left = 1;
1416 } else {
1417 x = menu->frame_x + MENUW(menu);
1420 if (wPreferences.align_menus) {
1421 y = menu->frame_y;
1422 } else {
1423 y = menu->frame_y + menu->entry_height*entry_no;
1424 if (menu->flags.titled)
1425 y += menu->frame->top_width;
1426 if (menu->cascades[entry->cascade]->flags.titled)
1427 y -= menu->cascades[entry->cascade]->frame->top_width;
1430 wMenuMapAt(menu->cascades[entry->cascade], x, y, False);
1431 menu->cascades[entry->cascade]->parent = menu;
1432 } else {
1433 return;
1436 paintEntry(menu, entry_no, True);
1441 static WMenu*
1442 findMenu(WScreen *scr, int *x_ret, int *y_ret)
1444 WMenu *menu;
1445 WObjDescriptor *desc;
1446 Window root_ret, win, junk_win;
1447 int x, y, wx, wy;
1448 unsigned int mask;
1450 XQueryPointer(dpy, scr->root_win, &root_ret, &win, &x, &y, &wx, &wy,
1451 &mask);
1453 if (win==None) return NULL;
1455 if (XFindContext(dpy, win, wWinContext, (XPointer *)&desc)==XCNOENT)
1456 return NULL;
1458 if (desc->parent_type == WCLASS_MENU) {
1459 menu = (WMenu*)desc->parent;
1460 XTranslateCoordinates(dpy, root_ret, menu->menu->window, wx, wy,
1461 x_ret, y_ret, &junk_win);
1462 return menu;
1464 return NULL;
1470 static void
1471 closeCascade(WMenu *menu)
1473 WMenu *parent=menu->parent;
1475 if (menu->flags.brother
1476 || (!menu->flags.buttoned
1477 && (!menu->flags.app_menu||menu->parent!=NULL))) {
1479 selectEntry(menu, -1);
1480 XSync(dpy, 0);
1481 #if (MENU_BLINK_DELAY > 2)
1482 wusleep(MENU_BLINK_DELAY/2);
1483 #endif
1484 wMenuUnmap(menu);
1485 while (parent!=NULL
1486 && (parent->parent!=NULL || !parent->flags.app_menu
1487 || parent->flags.brother)
1488 && !parent->flags.buttoned) {
1489 selectEntry(parent, -1);
1490 wMenuUnmap(parent);
1491 parent = parent->parent;
1493 if (parent)
1494 selectEntry(parent, -1);
1499 static void
1500 closeBrotherCascadesOf(WMenu *menu)
1502 WMenu *tmp;
1503 int i;
1505 for (i=0; i<menu->cascade_no; i++) {
1506 if (menu->cascades[i]->flags.brother) {
1507 tmp = menu->cascades[i];
1508 } else {
1509 tmp = menu->cascades[i]->brother;
1511 if (tmp->flags.mapped) {
1512 selectEntry(tmp->parent, -1);
1513 closeBrotherCascadesOf(tmp);
1514 break;
1520 #define getEntryAt(menu, x, y) ((y)<0 ? -1 : (y)/(menu->entry_height))
1523 static WMenu*
1524 parentMenu(WMenu *menu)
1526 WMenu *parent;
1527 WMenuEntry *entry;
1529 if (menu->flags.buttoned)
1530 return menu;
1532 while (menu->parent && menu->parent->flags.mapped) {
1533 parent = menu->parent;
1534 if (parent->selected_entry < 0)
1535 break;
1536 entry = parent->entries[parent->selected_entry];
1537 if (!entry->flags.enabled || entry->cascade<0 || !parent->cascades ||
1538 parent->cascades[entry->cascade] != menu)
1539 break;
1540 menu = parent;
1541 if (menu->flags.buttoned)
1542 break;
1545 return menu;
1551 * Will raise the passed menu, if submenu = 0
1552 * If submenu > 0 will also raise all mapped submenus
1553 * until the first buttoned one
1554 * If submenu < 0 will also raise all mapped parent menus
1555 * until the first buttoned one
1558 static void
1559 raiseMenus(WMenu *menu, int submenus)
1561 WMenu *submenu;
1562 int i;
1564 if(!menu) return;
1566 wRaiseFrame(menu->frame->core);
1568 if (submenus>0 && menu->selected_entry>=0) {
1569 i = menu->entries[menu->selected_entry]->cascade;
1570 if (i>=0 && menu->cascades) {
1571 submenu = menu->cascades[i];
1572 if (submenu->flags.mapped && !submenu->flags.buttoned)
1573 raiseMenus(submenu, submenus);
1576 if (submenus<0 && !menu->flags.buttoned &&
1577 menu->parent && menu->parent->flags.mapped)
1578 raiseMenus(menu->parent, submenus);
1582 WMenu*
1583 wMenuUnderPointer(WScreen *screen)
1585 WObjDescriptor *desc;
1586 Window root_ret, win;
1587 int dummy;
1588 unsigned int mask;
1590 XQueryPointer(dpy, screen->root_win, &root_ret, &win, &dummy, &dummy,
1591 &dummy, &dummy, &mask);
1593 if (win==None) return NULL;
1595 if (XFindContext(dpy, win, wWinContext, (XPointer *)&desc)==XCNOENT)
1596 return NULL;
1598 if (desc->parent_type == WCLASS_MENU)
1599 return (WMenu *)desc->parent;
1600 return NULL;
1606 static void
1607 getPointerPosition(WScreen *scr, int *x, int *y)
1609 Window root_ret, win;
1610 int wx, wy;
1611 unsigned int mask;
1613 XQueryPointer(dpy, scr->root_win, &root_ret, &win, x, y, &wx, &wy, &mask);
1617 static void
1618 getScrollAmount(WMenu *menu, int *hamount, int *vamount)
1620 WScreen *scr = menu->menu->screen_ptr;
1621 int menuX1 = menu->frame_x;
1622 int menuY1 = menu->frame_y;
1623 int menuX2 = menu->frame_x + MENUW(menu);
1624 int menuY2 = menu->frame_y + MENUH(menu);
1625 int xroot, yroot;
1626 WMRect rect = wGetRectForHead(scr, wGetHeadForPointerLocation(scr));
1629 *hamount = 0;
1630 *vamount = 0;
1632 getPointerPosition(scr, &xroot, &yroot);
1634 if (xroot <= (rect.pos.x + 1) && menuX1 < rect.pos.x) {
1635 /* scroll to the right */
1636 *hamount = WMIN(MENU_SCROLL_STEP, abs(menuX1));
1638 } else if (xroot >= (rect.pos.x + rect.size.width - 2) &&
1639 menuX2 > (rect.pos.x + rect.size.width - 1)) {
1640 /* scroll to the left */
1641 *hamount = WMIN(MENU_SCROLL_STEP, abs(menuX2-rect.pos.x-rect.size.width-1));
1643 if (*hamount==0)
1644 *hamount = 1;
1646 *hamount = -*hamount;
1649 if (yroot <= (rect.pos.y + 1) && menuY1 < rect.pos.y) {
1650 /* scroll down */
1651 *vamount = WMIN(MENU_SCROLL_STEP, abs(menuY1));
1653 } else if (yroot >= (rect.pos.y + rect.size.height - 2) &&
1654 menuY2 > (rect.pos.y + rect.size.height - 1)) {
1655 /* scroll up */
1656 *vamount = WMIN(MENU_SCROLL_STEP, abs(menuY2-rect.pos.y-rect.size.height-2));
1658 *vamount = -*vamount;
1663 static void
1664 dragScrollMenuCallback(void *data)
1666 WMenu *menu = (WMenu*)data;
1667 WScreen *scr = menu->menu->screen_ptr;
1668 WMenu *parent = parentMenu(menu);
1669 int hamount, vamount;
1670 int x, y;
1671 int newSelectedEntry;
1673 getScrollAmount(menu, &hamount, &vamount);
1676 if (hamount != 0 || vamount != 0) {
1677 wMenuMove(parent, parent->frame_x + hamount,
1678 parent->frame_y + vamount, True);
1679 if (findMenu(scr, &x, &y)) {
1680 newSelectedEntry = getEntryAt(menu, x, y);
1681 selectEntry(menu, newSelectedEntry);
1682 } else {
1683 /* Pointer fell outside of menu. If the selected entry is
1684 * not a submenu, unselect it */
1685 if (menu->selected_entry >= 0
1686 && menu->entries[menu->selected_entry]->cascade<0)
1687 selectEntry(menu, -1);
1688 newSelectedEntry = 0;
1691 /* paranoid check */
1692 if (newSelectedEntry >= 0) {
1693 /* keep scrolling */
1694 menu->timer = WMAddTimerHandler(MENU_SCROLL_DELAY,
1695 dragScrollMenuCallback, menu);
1696 } else {
1697 menu->timer = NULL;
1699 } else {
1700 /* don't need to scroll anymore */
1701 menu->timer = NULL;
1702 if (findMenu(scr, &x, &y)) {
1703 newSelectedEntry = getEntryAt(menu, x, y);
1704 selectEntry(menu, newSelectedEntry);
1710 static void
1711 scrollMenuCallback(void *data)
1713 WMenu *menu = (WMenu*)data;
1714 WMenu *parent = parentMenu(menu);
1715 int hamount = 0; /* amount to scroll */
1716 int vamount = 0;
1718 #ifdef VIRTUAL_DESKTOP
1719 /* don't scroll if it is in vdesk mode */
1720 if (!wPreferences.vedge_thickness)
1721 #endif
1722 getScrollAmount(menu, &hamount, &vamount);
1724 if (hamount != 0 || vamount != 0) {
1725 wMenuMove(parent, parent->frame_x + hamount,
1726 parent->frame_y + vamount, True);
1728 /* keep scrolling */
1729 menu->timer = WMAddTimerHandler(MENU_SCROLL_DELAY,
1730 scrollMenuCallback, menu);
1731 } else {
1732 /* don't need to scroll anymore */
1733 menu->timer = NULL;
1739 #define MENU_SCROLL_BORDER 5
1741 static int
1742 isPointNearBoder(WMenu *menu, int x, int y)
1744 int menuX1 = menu->frame_x;
1745 int menuY1 = menu->frame_y;
1746 int menuX2 = menu->frame_x + MENUW(menu);
1747 int menuY2 = menu->frame_y + MENUH(menu);
1748 int flag = 0;
1749 /* XXX: handle screen joins proper !! */
1750 WMRect rect = wGetRectForHead(menu->frame->screen_ptr,
1751 wGetHeadForPoint(menu->frame->screen_ptr, (WMPoint){ x, y}));
1753 if (x >= menuX1 && x <= menuX2 &&
1754 (y < rect.pos.y + MENU_SCROLL_BORDER ||
1755 y >= rect.pos.y + rect.size.height + MENU_SCROLL_BORDER))
1756 flag = 1;
1757 else if (y >= menuY1 && y <= menuY2 &&
1758 (x < rect.pos.x + MENU_SCROLL_BORDER ||
1759 x >= rect.pos.x + rect.size.width + MENU_SCROLL_BORDER))
1760 flag = 1;
1762 return flag;
1766 typedef struct _delay {
1767 WWindow *wwin;
1768 WMenu *menu;
1769 int ox,oy;
1770 } _delay;
1773 static void
1774 _leaving(_delay *dl)
1776 wMenuMove(dl->menu, dl->ox, dl->oy, True);
1777 dl->menu->jump_back=NULL;
1778 dl->menu->menu->screen_ptr->flags.jump_back_pending = 0;
1779 wfree(dl);
1783 void
1784 wMenuScroll(WMenu *menu, XEvent *event)
1786 WMenu *smenu;
1787 WMenu *omenu = parentMenu(menu);
1788 WScreen *scr = menu->frame->screen_ptr;
1789 int done = 0;
1790 int jump_back = 0;
1791 int old_frame_x = omenu->frame_x;
1792 int old_frame_y = omenu->frame_y;
1793 XEvent ev;
1795 if (omenu->jump_back)
1796 WMDeleteTimerWithClientData(omenu->jump_back);
1799 if ((/*omenu->flags.buttoned &&*/ !wPreferences.wrap_menus)
1800 || omenu->flags.app_menu) {
1801 jump_back = 1;
1804 if (!wPreferences.wrap_menus)
1805 raiseMenus(omenu, True);
1806 else
1807 raiseMenus(menu, False);
1809 if (!menu->timer)
1810 scrollMenuCallback(menu);
1812 while(!done) {
1813 int x, y, on_border, on_x_edge, on_y_edge, on_title;
1814 WMRect rect;
1816 WMNextEvent(dpy, &ev);
1817 switch (ev.type) {
1818 case EnterNotify:
1819 WMHandleEvent(&ev);
1820 case MotionNotify:
1821 x = (ev.type==MotionNotify) ? ev.xmotion.x_root : ev.xcrossing.x_root;
1822 y = (ev.type==MotionNotify) ? ev.xmotion.y_root : ev.xcrossing.y_root;
1824 /* on_border is != 0 if the pointer is between the menu
1825 * and the screen border and is close enough to the border */
1826 on_border = isPointNearBoder(menu, x, y);
1828 smenu = wMenuUnderPointer(scr);
1830 if ((smenu==NULL && !on_border) || (smenu && parentMenu(smenu)!=omenu)) {
1831 done = 1;
1832 break;
1835 rect = wGetRectForHead(scr, wGetHeadForPoint(scr, (WMPoint){ x, y }));
1836 on_x_edge = x <= rect.pos.x + 1 || x >= rect.pos.x + rect.size.width - 2;
1837 on_y_edge = y <= rect.pos.y + 1 || y >= rect.pos.y + rect.size.height - 2;
1838 on_border = on_x_edge || on_y_edge;
1840 if (!on_border && !jump_back) {
1841 done = 1;
1842 break;
1845 if (menu->timer && (smenu!=menu || (!on_y_edge && !on_x_edge))) {
1846 WMDeleteTimerHandler(menu->timer);
1847 menu->timer = NULL;
1850 if (smenu != NULL)
1851 menu = smenu;
1853 if (!menu->timer)
1854 scrollMenuCallback(menu);
1855 break;
1856 case ButtonPress:
1857 /* True if we push on title, or drag the omenu to other position */
1858 on_title = ev.xbutton.x_root >= omenu->frame_x &&
1859 ev.xbutton.x_root <= omenu->frame_x + MENUW(omenu) &&
1860 ev.xbutton.y_root >= omenu->frame_y &&
1861 ev.xbutton.y_root <= omenu->frame_y + omenu->frame->top_width;
1862 WMHandleEvent(&ev);
1863 smenu = wMenuUnderPointer(scr);
1864 if (smenu == NULL || (smenu && smenu->flags.buttoned && smenu != omenu))
1865 done = 1;
1866 else if (smenu==omenu && on_title) {
1867 jump_back = 0;
1868 done = 1;
1870 break;
1871 case KeyPress:
1872 done = 1;
1873 default:
1874 WMHandleEvent(&ev);
1875 break;
1879 if (menu->timer) {
1880 WMDeleteTimerHandler(menu->timer);
1881 menu->timer = NULL;
1884 if (jump_back) {
1885 _delay *delayer;
1886 if (!omenu->jump_back) {
1887 delayer=wmalloc(sizeof(_delay));
1888 delayer->menu=omenu;
1889 delayer->ox=old_frame_x;
1890 delayer->oy=old_frame_y;
1891 omenu->jump_back=delayer;
1892 scr->flags.jump_back_pending = 1;
1894 else delayer = omenu->jump_back;
1895 WMAddTimerHandler(MENU_JUMP_BACK_DELAY,(WMCallback*)_leaving, delayer);
1901 static void
1902 menuExpose(WObjDescriptor *desc, XEvent *event)
1904 wMenuPaint(desc->parent);
1907 typedef struct {
1908 int *delayed_select;
1909 WMenu *menu;
1910 WMHandlerID magic;
1911 } delay_data;
1914 static void
1915 delaySelection(void *data)
1917 delay_data *d = (delay_data*)data;
1918 int x, y, entry_no;
1919 WMenu *menu;
1921 d->magic = NULL;
1923 menu = findMenu(d->menu->menu->screen_ptr, &x, &y);
1924 if (menu && (d->menu == menu || d->delayed_select)) {
1925 entry_no = getEntryAt(menu, x, y);
1926 selectEntry(menu, entry_no);
1928 if (d->delayed_select)
1929 *(d->delayed_select) = 0;
1933 static void
1934 menuMouseDown(WObjDescriptor *desc, XEvent *event)
1936 XButtonEvent *bev = &event->xbutton;
1937 WMenu *menu = desc->parent;
1938 WMenu *smenu;
1939 WScreen *scr=menu->frame->screen_ptr;
1940 WMenuEntry *entry=NULL;
1941 XEvent ev;
1942 int close_on_exit=0;
1943 int done=0;
1944 int delayed_select = 0;
1945 int entry_no;
1946 int x, y;
1947 int prevx, prevy;
1948 int old_frame_x = 0;
1949 int old_frame_y = 0;
1950 delay_data d_data = {NULL, NULL, NULL};
1952 if (!wPreferences.wrap_menus) {
1953 smenu = parentMenu(menu);
1954 old_frame_x = smenu->frame_x;
1955 old_frame_y = smenu->frame_y;
1956 } else if (event->xbutton.window == menu->frame->core->window) {
1957 /* This is true if the menu was launched with right click on root window */
1958 delayed_select = 1;
1959 d_data.delayed_select = &delayed_select;
1960 d_data.menu = menu;
1961 d_data.magic = WMAddTimerHandler(wPreferences.dblclick_time,
1962 delaySelection, &d_data);
1965 if (menu->flags.inside_handler) {
1966 return;
1968 menu->flags.inside_handler = 1;
1970 wRaiseFrame(menu->frame->core);
1972 close_on_exit = (bev->send_event || menu->flags.brother);
1974 smenu = findMenu(scr, &x, &y);
1975 if (!smenu) {
1976 x = -1;
1977 y = -1;
1978 } else {
1979 menu = smenu;
1982 if (menu->flags.editing) {
1983 goto byebye;
1985 entry_no = getEntryAt(menu, x, y);
1986 if (entry_no>=0) {
1987 entry = menu->entries[entry_no];
1989 if (!close_on_exit && (bev->state & ControlMask) && smenu
1990 && entry->flags.editable) {
1991 editEntry(smenu, entry);
1992 goto byebye;
1993 } else if (bev->state & ControlMask) {
1994 goto byebye;
1997 if (entry->flags.enabled && entry->cascade>=0 && menu->cascades) {
1998 WMenu *submenu = menu->cascades[entry->cascade];
1999 /* map cascade */
2000 if (submenu->flags.mapped && !submenu->flags.buttoned &&
2001 menu->selected_entry!=entry_no) {
2002 wMenuUnmap(submenu);
2004 if (!submenu->flags.mapped && !delayed_select) {
2005 selectEntry(menu, entry_no);
2006 } else if (!submenu->flags.buttoned) {
2007 selectEntry(menu, -1);
2010 } else if (!delayed_select) {
2011 selectEntry(menu, entry_no);
2014 if (!wPreferences.wrap_menus && !wPreferences.scrollable_menus) {
2015 if (!menu->timer)
2016 dragScrollMenuCallback(menu);
2020 #ifdef VIRTUAL_DESKTOP
2021 if (wPreferences.vedge_thickness) {
2022 wWorkspaceLowerEdge(scr);
2024 #endif
2026 prevx = bev->x_root;
2027 prevy = bev->y_root;
2028 while (!done) {
2029 int x, y;
2031 XAllowEvents(dpy, AsyncPointer|SyncPointer, CurrentTime);
2033 WMMaskEvent(dpy, ExposureMask|ButtonMotionMask|ButtonReleaseMask
2034 |ButtonPressMask, &ev);
2035 switch (ev.type) {
2036 case MotionNotify:
2037 smenu = findMenu(scr, &x, &y);
2039 if (smenu == NULL) {
2040 /* moved mouse out of menu */
2042 if (!delayed_select && d_data.magic) {
2043 WMDeleteTimerHandler(d_data.magic);
2044 d_data.magic = NULL;
2046 if (menu==NULL
2047 || (menu->selected_entry>=0
2048 && menu->entries[menu->selected_entry]->cascade>=0)) {
2049 prevx = ev.xmotion.x_root;
2050 prevy = ev.xmotion.y_root;
2052 break;
2054 selectEntry(menu, -1);
2055 menu = smenu;
2056 prevx = ev.xmotion.x_root;
2057 prevy = ev.xmotion.y_root;
2058 break;
2059 } else if (menu && menu!=smenu
2060 && (menu->selected_entry<0
2061 || menu->entries[menu->selected_entry]->cascade<0)) {
2062 selectEntry(menu, -1);
2064 if (!delayed_select && d_data.magic) {
2065 WMDeleteTimerHandler(d_data.magic);
2066 d_data.magic = NULL;
2068 } else {
2070 /* hysteresis for item selection */
2072 /* check if the motion was to the side, indicating that
2073 * the user may want to cross to a submenu */
2074 if (!delayed_select && menu) {
2075 int dx;
2076 Bool moved_to_submenu;/* moved to direction of submenu */
2078 dx = abs(prevx - ev.xmotion.x_root);
2080 moved_to_submenu = False;
2081 if (dx > 0 /* if moved enough to the side */
2082 /* maybe a open submenu */
2083 && menu->selected_entry>=0
2084 /* moving to the right direction */
2085 && (wPreferences.align_menus
2086 || ev.xmotion.y_root >= prevy)) {
2087 int index;
2089 index = menu->entries[menu->selected_entry]->cascade;
2090 if (index>=0) {
2091 if (menu->cascades[index]->frame_x>menu->frame_x) {
2092 if (prevx < ev.xmotion.x_root)
2093 moved_to_submenu = True;
2094 } else {
2095 if (prevx > ev.xmotion.x_root)
2096 moved_to_submenu = True;
2102 if (menu != smenu) {
2103 if (d_data.magic) {
2104 WMDeleteTimerHandler(d_data.magic);
2106 d_data.magic = NULL;
2107 } else if (moved_to_submenu) {
2108 /* while we are moving, postpone the selection */
2109 if (d_data.magic) {
2110 WMDeleteTimerHandler(d_data.magic);
2112 d_data.delayed_select = NULL;
2113 d_data.menu = menu;
2114 d_data.magic = WMAddTimerHandler(MENU_SELECT_DELAY,
2115 delaySelection,
2116 &d_data);
2117 prevx = ev.xmotion.x_root;
2118 prevy = ev.xmotion.y_root;
2119 break;
2120 } else {
2121 if (d_data.magic)
2122 WMDeleteTimerHandler(d_data.magic);
2123 d_data.magic = NULL;
2127 prevx = ev.xmotion.x_root;
2128 prevy = ev.xmotion.y_root;
2129 if (menu!=smenu) {
2130 /* pointer crossed menus */
2131 if (menu && menu->timer) {
2132 WMDeleteTimerHandler(menu->timer);
2133 menu->timer = NULL;
2135 if (smenu)
2136 dragScrollMenuCallback(smenu);
2138 menu = smenu;
2139 if (!menu->timer)
2140 dragScrollMenuCallback(menu);
2142 if (!delayed_select) {
2143 entry_no = getEntryAt(menu, x, y);
2144 if (entry_no>=0) {
2145 entry = menu->entries[entry_no];
2146 if (entry->flags.enabled && entry->cascade>=0 &&
2147 menu->cascades) {
2148 WMenu *submenu = menu->cascades[entry->cascade];
2149 if (submenu->flags.mapped && !submenu->flags.buttoned
2150 && menu->selected_entry!=entry_no) {
2151 wMenuUnmap(submenu);
2155 selectEntry(menu, entry_no);
2157 break;
2159 case ButtonPress:
2160 break;
2162 case ButtonRelease:
2163 if (ev.xbutton.button == event->xbutton.button)
2164 done=1;
2165 break;
2167 case Expose:
2168 WMHandleEvent(&ev);
2169 #ifdef VIRTUAL_DESKTOP
2170 /* since expose will raise edge up.. I need another ugly hack here */
2171 if (wPreferences.vedge_thickness) {
2172 wWorkspaceLowerEdge(scr);
2174 #endif
2175 break;
2179 if (menu && menu->timer) {
2180 WMDeleteTimerHandler(menu->timer);
2181 menu->timer = NULL;
2183 if (d_data.magic!=NULL)
2184 WMDeleteTimerHandler(d_data.magic);
2186 if (menu && menu->selected_entry>=0) {
2187 entry = menu->entries[menu->selected_entry];
2188 if (entry->callback!=NULL && entry->flags.enabled
2189 && entry->cascade < 0) {
2190 /* blink and erase menu selection */
2191 #if (MENU_BLINK_DELAY > 0)
2192 int sel = menu->selected_entry;
2193 int i;
2195 for (i=0; i<MENU_BLINK_COUNT; i++) {
2196 paintEntry(menu, sel, False);
2197 XSync(dpy, 0);
2198 wusleep(MENU_BLINK_DELAY);
2199 paintEntry(menu, sel, True);
2200 XSync(dpy, 0);
2201 wusleep(MENU_BLINK_DELAY);
2203 #endif
2204 /* unmap the menu, it's parents and call the callback */
2205 if (!menu->flags.buttoned &&
2206 (!menu->flags.app_menu||menu->parent!=NULL)) {
2207 closeCascade(menu);
2208 } else {
2209 selectEntry(menu, -1);
2211 (*entry->callback)(menu, entry);
2213 /* If the user double clicks an entry, the entry will
2214 * be executed twice, which is not good for things like
2215 * the root menu. So, ignore any clicks that were generated
2216 * while the entry was being executed */
2217 while (XCheckTypedWindowEvent(dpy, menu->menu->window,
2218 ButtonPress, &ev));
2219 } else if (entry->callback!=NULL && entry->cascade<0) {
2220 selectEntry(menu, -1);
2221 } else {
2222 if (entry->cascade>=0 && menu->cascades
2223 && menu->cascades[entry->cascade]->flags.brother) {
2224 selectEntry(menu, -1);
2229 if (((WMenu*)desc->parent)->flags.brother || close_on_exit || !smenu)
2230 closeCascade(desc->parent);
2232 /* close the cascade windows that should not remain opened */
2233 closeBrotherCascadesOf(desc->parent);
2235 if (!wPreferences.wrap_menus)
2236 wMenuMove(parentMenu(desc->parent), old_frame_x, old_frame_y, True);
2238 byebye:
2239 ((WMenu*)desc->parent)->flags.inside_handler = 0;
2240 #ifdef VIRTUAL_DESKTOP
2241 if (wPreferences.vedge_thickness) {
2242 wWorkspaceRaiseEdge(scr);
2244 #endif
2248 void
2249 wMenuMove(WMenu *menu, int x, int y, int submenus)
2251 WMenu *submenu;
2252 int i;
2254 if (!menu) return;
2256 menu->frame_x = x;
2257 menu->frame_y = y;
2258 XMoveWindow(dpy, menu->frame->core->window, x, y);
2260 if (submenus>0 && menu->selected_entry>=0) {
2261 i = menu->entries[menu->selected_entry]->cascade;
2263 if (i>=0 && menu->cascades) {
2264 submenu = menu->cascades[i];
2265 if (submenu->flags.mapped && !submenu->flags.buttoned) {
2266 if (wPreferences.align_menus) {
2267 wMenuMove(submenu, x + MENUW(menu), y, submenus);
2268 } else {
2269 wMenuMove(submenu, x+ MENUW(menu),
2270 y + submenu->entry_height*menu->selected_entry,
2271 submenus);
2276 if (submenus<0 && menu->parent!=NULL && menu->parent->flags.mapped &&
2277 !menu->parent->flags.buttoned) {
2278 if (wPreferences.align_menus) {
2279 wMenuMove(menu->parent, x - MENUW(menu->parent), y, submenus);
2280 } else {
2281 wMenuMove(menu->parent, x - MENUW(menu->parent), menu->frame_y
2282 - menu->parent->entry_height*menu->parent->selected_entry,
2283 submenus);
2289 static void
2290 changeMenuLevels(WMenu *menu, int lower)
2292 int i;
2294 if (!lower) {
2295 ChangeStackingLevel(menu->frame->core, (!menu->parent ? WMMainMenuLevel
2296 : WMSubmenuLevel));
2297 wRaiseFrame(menu->frame->core);
2298 menu->flags.lowered = 0;
2299 } else {
2300 ChangeStackingLevel(menu->frame->core, WMNormalLevel);
2301 wLowerFrame(menu->frame->core);
2302 menu->flags.lowered = 1;
2304 for (i=0; i<menu->cascade_no; i++) {
2305 if (menu->cascades[i]
2306 && !menu->cascades[i]->flags.buttoned
2307 && menu->cascades[i]->flags.lowered!=lower) {
2308 changeMenuLevels(menu->cascades[i], lower);
2315 static void
2316 menuTitleDoubleClick(WCoreWindow *sender, void *data, XEvent *event)
2318 WMenu *menu = data;
2319 int lower;
2321 if (event->xbutton.state & MOD_MASK) {
2322 if (menu->flags.lowered) {
2323 lower = 0;
2324 } else {
2325 lower = 1;
2327 changeMenuLevels(menu, lower);
2332 static void
2333 menuTitleMouseDown(WCoreWindow *sender, void *data, XEvent *event)
2335 WMenu *menu = data;
2336 WMenu *tmp;
2337 XEvent ev;
2338 int x=menu->frame_x, y=menu->frame_y;
2339 int dx=event->xbutton.x_root, dy=event->xbutton.y_root;
2340 int i, lower;
2341 Bool started;
2343 /* can't touch the menu copy */
2344 if (menu->flags.brother)
2345 return;
2347 if (event->xbutton.button != Button1 && event->xbutton.button != Button2)
2348 return;
2350 if (event->xbutton.state & MOD_MASK) {
2351 wLowerFrame(menu->frame->core);
2352 lower = 1;
2353 } else {
2354 wRaiseFrame(menu->frame->core);
2355 lower = 0;
2357 tmp = menu;
2359 /* lower/raise all submenus */
2360 while (1) {
2361 if (tmp->selected_entry>=0 && tmp->cascades
2362 && tmp->entries[tmp->selected_entry]->cascade>=0) {
2363 tmp = tmp->cascades[tmp->entries[tmp->selected_entry]->cascade];
2364 if (!tmp || !tmp->flags.mapped)
2365 break;
2366 if (lower)
2367 wLowerFrame(tmp->frame->core);
2368 else
2369 wRaiseFrame(tmp->frame->core);
2370 } else {
2371 break;
2375 /* tear off the menu if it's a root menu or a cascade
2376 application menu */
2377 if (!menu->flags.buttoned && !menu->flags.brother
2378 && (!menu->flags.app_menu||menu->parent!=NULL)) {
2379 menu->flags.buttoned=1;
2380 wFrameWindowShowButton(menu->frame, WFF_RIGHT_BUTTON);
2381 if (menu->parent) {
2382 /* turn off selected menu entry in parent menu */
2383 selectEntry(menu->parent, -1);
2385 /* make parent map the copy in place of the original */
2386 for (i=0; i<menu->parent->cascade_no; i++) {
2387 if (menu->parent->cascades[i] == menu) {
2388 menu->parent->cascades[i] = menu->brother;
2389 break;
2395 started = False;
2396 while(1) {
2397 WMMaskEvent(dpy, ButtonMotionMask|ButtonReleaseMask|ButtonPressMask
2398 |ExposureMask, &ev);
2399 switch (ev.type) {
2400 case MotionNotify:
2401 if (started) {
2402 x += ev.xmotion.x_root - dx;
2403 y += ev.xmotion.y_root - dy;
2404 dx = ev.xmotion.x_root;
2405 dy = ev.xmotion.y_root;
2406 wMenuMove(menu, x, y, True);
2407 } else {
2408 if (abs(ev.xmotion.x_root - dx) > MOVE_THRESHOLD
2409 || abs(ev.xmotion.y_root - dy) > MOVE_THRESHOLD) {
2410 started = True;
2411 XGrabPointer(dpy, menu->frame->titlebar->window, False,
2412 ButtonMotionMask|ButtonReleaseMask
2413 |ButtonPressMask,
2414 GrabModeAsync, GrabModeAsync, None,
2415 wCursor[WCUR_MOVE], CurrentTime);
2418 break;
2420 case ButtonPress:
2421 break;
2423 case ButtonRelease:
2424 if (ev.xbutton.button != event->xbutton.button)
2425 break;
2426 XUngrabPointer(dpy, CurrentTime);
2427 return;
2429 default:
2430 WMHandleEvent(&ev);
2431 break;
2437 *----------------------------------------------------------------------
2438 * menuCloseClick--
2439 * Handles mouse click on the close button of menus. The menu is
2440 * closed when the button is clicked.
2442 * Side effects:
2443 * The closed menu is reinserted at it's parent menus
2444 * cascade list.
2445 *----------------------------------------------------------------------
2447 static void
2448 menuCloseClick(WCoreWindow *sender, void *data, XEvent *event)
2450 WMenu *menu = (WMenu*)data;
2451 WMenu *parent = menu->parent;
2452 int i;
2454 if (parent) {
2455 for (i=0; i<parent->cascade_no; i++) {
2456 /* find the entry that points to the copy */
2457 if (parent->cascades[i] == menu->brother) {
2458 /* make it point to the original */
2459 parent->cascades[i] = menu;
2460 menu->parent = parent;
2461 break;
2465 wMenuUnmap(menu);
2469 static void
2470 saveMenuInfo(WMPropList *dict, WMenu *menu, WMPropList *key)
2472 WMPropList *value, *list;
2473 char buffer[256];
2475 snprintf(buffer, sizeof(buffer), "%i,%i", menu->frame_x, menu->frame_y);
2476 value = WMCreatePLString(buffer);
2477 list = WMCreatePLArray(value, NULL);
2478 if (menu->flags.lowered)
2479 WMAddToPLArray(list, WMCreatePLString("lowered"));
2480 WMPutInPLDictionary(dict, key, list);
2481 WMReleasePropList(value);
2482 WMReleasePropList(list);
2486 void
2487 wMenuSaveState(WScreen *scr)
2489 WMPropList *menus, *key;
2490 int save_menus = 0;
2492 menus = WMCreatePLDictionary(NULL, NULL, NULL);
2494 #ifndef LITE
2495 if (scr->switch_menu && scr->switch_menu->flags.buttoned) {
2496 key = WMCreatePLString("SwitchMenu");
2497 saveMenuInfo(menus, scr->switch_menu, key);
2498 WMReleasePropList(key);
2499 save_menus = 1;
2502 if (saveMenuRecurs(menus, scr, scr->root_menu))
2503 save_menus = 1;
2505 #endif /* !LITE */
2506 if (scr->workspace_menu && scr->workspace_menu->flags.buttoned) {
2507 key = WMCreatePLString("WorkspaceMenu");
2508 saveMenuInfo(menus, scr->workspace_menu, key);
2509 WMReleasePropList(key);
2510 save_menus = 1;
2513 if (save_menus) {
2514 key = WMCreatePLString("Menus");
2515 WMPutInPLDictionary(scr->session_state, key, menus);
2516 WMReleasePropList(key);
2518 WMReleasePropList(menus);
2522 #ifndef LITE
2524 static Bool
2525 getMenuPath(WMenu *menu, char *buffer, int bufSize)
2527 Bool ok = True;
2528 int len = 0;
2530 if (!menu->flags.titled || !menu->frame->title[0])
2531 return False;
2533 len = strlen(menu->frame->title);
2534 if (len >= bufSize)
2535 return False;
2537 if (menu->parent) {
2538 ok = getMenuPath(menu->parent, buffer, bufSize - len - 1);
2539 if (!ok)
2540 return False;
2543 strcat(buffer, "\\");
2544 strcat(buffer, menu->frame->title);
2546 return True;
2550 static Bool
2551 saveMenuRecurs(WMPropList *menus, WScreen *scr, WMenu *menu)
2553 WMPropList *key;
2554 int save_menus = 0, i;
2555 char buffer[512];
2556 Bool ok = True;
2559 if (menu->flags.brother)
2560 menu = menu->brother;
2562 if (menu->flags.buttoned && menu != scr->switch_menu) {
2564 buffer[0] = '\0';
2565 ok = getMenuPath(menu, buffer, 510);
2567 if (ok) {
2568 key = WMCreatePLString(buffer);
2569 saveMenuInfo(menus, menu, key);
2570 WMReleasePropList(key);
2571 save_menus = 1;
2575 if (ok) {
2576 for (i = 0; i < menu->cascade_no; i++) {
2577 if (saveMenuRecurs(menus, scr, menu->cascades[i]))
2578 save_menus = 1;
2581 return save_menus;
2583 #endif /* !LITE */
2586 #define COMPLAIN(key) wwarning(_("bad value in menus state info:%s"), key)
2589 static Bool
2590 getMenuInfo(WMPropList *info, int *x, int *y, Bool *lowered)
2592 WMPropList *pos;
2594 *lowered = False;
2596 if (WMIsPLArray(info)) {
2597 WMPropList *flags;
2598 pos = WMGetFromPLArray(info, 0);
2599 flags = WMGetFromPLArray(info, 1);
2600 if (flags != NULL && WMIsPLString(flags) && WMGetFromPLString(flags) != NULL
2601 && strcmp(WMGetFromPLString(flags), "lowered") == 0) {
2602 *lowered = True;
2604 } else {
2605 pos = info;
2608 if (pos != NULL && WMIsPLString(pos)) {
2609 if (sscanf(WMGetFromPLString(pos), "%i,%i", x, y)!=2)
2610 COMPLAIN("Position");
2611 } else {
2612 COMPLAIN("(position, flags...)");
2613 return False;
2616 return True;
2620 static int
2621 restoreMenu(WScreen *scr, WMPropList *menu, int which)
2623 int x, y;
2624 Bool lowered = False;
2625 WMenu *pmenu = NULL;
2627 if (!menu)
2628 return False;
2630 if (!getMenuInfo(menu, &x, &y, &lowered))
2631 return False;
2634 #ifndef LITE
2635 if (which & WSS_SWITCHMENU) {
2636 OpenSwitchMenu(scr, x, y, False);
2637 pmenu = scr->switch_menu;
2639 #endif /* !LITE */
2641 if (pmenu) {
2642 int width = MENUW(pmenu);
2643 int height = MENUH(pmenu);
2644 WMRect rect = wGetRectForHead(scr, wGetHeadForPointerLocation(scr));
2646 if (lowered) {
2647 changeMenuLevels(pmenu, True);
2650 if (x < rect.pos.x - width) x = rect.pos.x;
2651 if (x > rect.pos.x + rect.size.width) x = rect.pos.x + rect.size.width - width;
2652 if (y < rect.pos.y) y = rect.pos.y;
2653 if (y > rect.pos.y + rect.size.height) y = rect.pos.y + rect.size.height - height;
2655 wMenuMove(pmenu, x, y, True);
2656 pmenu->flags.buttoned = 1;
2657 wFrameWindowShowButton(pmenu->frame, WFF_RIGHT_BUTTON);
2658 return True;
2660 return False;
2664 #ifndef LITE
2665 static int
2666 restoreMenuRecurs(WScreen *scr, WMPropList *menus, WMenu *menu, char *path)
2668 WMPropList *key, *entry;
2669 char buffer[512];
2670 int i, x, y, res;
2671 Bool lowered;
2673 if (strlen(path) + strlen(menu->frame->title) > 510)
2674 return False;
2676 snprintf(buffer, sizeof(buffer), "%s\\%s", path, menu->frame->title);
2677 key = WMCreatePLString(buffer);
2678 entry = WMGetFromPLDictionary(menus, key);
2679 res = False;
2681 if (entry && getMenuInfo(entry, &x, &y, &lowered)) {
2683 if (!menu->flags.mapped) {
2684 int width = MENUW(menu);
2685 int height = MENUH(menu);
2686 WMRect rect = wGetRectForHead(scr, wGetHeadForPointerLocation(scr));
2688 wMenuMapAt(menu, x, y, False);
2690 if (menu->parent) {
2691 /* make parent map the copy in place of the original */
2692 for (i=0; i<menu->parent->cascade_no; i++) {
2693 if (menu->parent->cascades[i] == menu) {
2694 menu->parent->cascades[i] = menu->brother;
2695 break;
2699 if (lowered) {
2700 changeMenuLevels(menu, True);
2703 if (x < rect.pos.x - width) x = rect.pos.x;
2704 if (x > rect.pos.x + rect.size.width) x = rect.pos.x + rect.size.width - width;
2705 if (y < rect.pos.y) y = rect.pos.y;
2706 if (y > rect.pos.y + rect.size.height) y = rect.pos.y + rect.size.height - height;
2708 wMenuMove(menu, x, y, True);
2709 menu->flags.buttoned = 1;
2710 wFrameWindowShowButton(menu->frame, WFF_RIGHT_BUTTON);
2711 res = True;
2715 WMReleasePropList(key);
2717 for (i=0; i<menu->cascade_no; i++) {
2718 if (restoreMenuRecurs(scr, menus, menu->cascades[i], buffer) != False)
2719 res = True;
2722 return res;
2724 #endif /* !LITE */
2727 void
2728 wMenuRestoreState(WScreen *scr)
2730 WMPropList *menus, *menu, *key, *skey;
2732 key = WMCreatePLString("Menus");
2733 menus = WMGetFromPLDictionary(scr->session_state, key);
2734 WMReleasePropList(key);
2736 if (!menus)
2737 return;
2739 /* restore menus */
2741 skey = WMCreatePLString("SwitchMenu");
2742 menu = WMGetFromPLDictionary(menus, skey);
2743 WMReleasePropList(skey);
2744 restoreMenu(scr, menu, WSS_SWITCHMENU);
2746 #ifndef LITE
2747 if (!scr->root_menu) {
2748 OpenRootMenu(scr, scr->scr_width*2, 0, False);
2749 wMenuUnmap(scr->root_menu);
2751 restoreMenuRecurs(scr, menus, scr->root_menu, "");
2752 #endif /* !LITE */
2756 void
2757 OpenWorkspaceMenu(WScreen *scr, int x, int y)
2759 WMenu *menu, *parent;
2760 WMenuEntry *entry;
2762 #ifndef LITE
2763 if (!scr->root_menu) {
2764 OpenRootMenu(scr, scr->scr_width*2, 0, False);
2765 wMenuUnmap(scr->root_menu);
2767 #endif
2768 menu = scr->workspace_menu;
2769 if (menu) {
2770 if (menu->flags.mapped) {
2771 if (!menu->flags.buttoned) {
2772 wMenuUnmap(menu);
2773 parent = menu->parent;
2774 if (parent && parent->selected_entry >= 0) {
2775 entry = parent->entries[parent->selected_entry];
2776 if (parent->cascades[entry->cascade] == menu) {
2777 selectEntry(parent, -1);
2778 wMenuMapAt(menu, x, y, False);
2781 } else {
2782 wRaiseFrame(menu->frame->core);
2783 wMenuMapCopyAt(menu, x, y);
2785 } else {
2786 wMenuMapAt(menu, x, y, False);