changed indentation to use spaces only
[wmaker-crm.git] / src / menu.c
blob02968a3642bee0ab3a31c4e245a09e301c8f3531
1 /* menu.c- generic menu, used for root menu, application menus etc.
3 * Window Maker window manager
5 * Copyright (c) 1997-2003 Alfredo K. Kojima
6 * Copyright (c) 1998-2003 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"
46 #include "workspace.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 + 10;
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->select_color), 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, WMColorPixel(scr->select_text_color));
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;
874 WMRect rect = wGetRectForHead(scr, wGetHeadForPointerLocation(scr));
876 if (menu->entry_no<0) return;
878 x1 = menu->frame_x;
879 y1 = menu->frame_y+menu->frame->top_width
880 + menu->selected_entry*menu->entry_height;
881 x2 = x1 + MENUW(menu);
882 y2 = y1 + menu->entry_height;
884 new_x = x1;
885 new_y = y1;
886 move = 0;
889 if (x1 < rect.pos.x) {
890 new_x = rect.pos.x;
891 move = 1;
892 } else if (x2 >= rect.pos.x + rect.size.width) {
893 new_x = rect.pos.x + rect.size.width - MENUW(menu) - 1;
894 move = 1;
897 if (y1 < rect.pos.y) {
898 new_y = rect.pos.y;
899 move = 1;
900 } else if (y2 >= rect.pos.y + rect.size.height) {
901 new_y = rect.pos.y + rect.size.height - menu->entry_height - 1;
902 move = 1;
905 new_y = new_y - menu->frame->top_width
906 - menu->selected_entry*menu->entry_height;
907 move_menus(menu, new_x, new_y);
911 static int
912 check_key(WMenu *menu, XKeyEvent *event)
914 int i, ch, s;
915 char buffer[32];
917 if (XLookupString(event, buffer, 32, NULL, NULL)<1)
918 return -1;
920 ch = toupper(buffer[0]);
922 s = (menu->selected_entry>=0 ? menu->selected_entry+1 : 0);
924 again:
925 for (i=s; i<menu->entry_no; i++) {
926 if (ch==toupper(menu->entries[i]->text[0])) {
927 return i;
930 /* no match. Retry from start, if previous started from a selected entry */
931 if (s!=0) {
932 s = 0;
933 goto again;
935 return -1;
939 static int
940 keyboardMenu(WMenu *menu)
942 XEvent event;
943 KeySym ksym=NoSymbol;
944 int done=0;
945 int index;
946 WMenuEntry *entry;
947 int old_pos_x = menu->frame_x;
948 int old_pos_y = menu->frame_y;
949 int new_x = old_pos_x, new_y = old_pos_y;
950 WMRect rect = wGetRectForHead(menu->frame->screen_ptr,
951 wGetHeadForPointerLocation(menu->frame->screen_ptr));
953 if (menu->flags.editing)
954 return False;
957 XGrabKeyboard(dpy, menu->frame->core->window, True, GrabModeAsync,
958 GrabModeAsync, CurrentTime);
961 if (menu->frame_y+menu->frame->top_width >= rect.pos.y + rect.size.height)
962 new_y = rect.pos.y + rect.size.height - menu->frame->top_width;
964 if (menu->frame_x+MENUW(menu) >= rect.pos.x + rect.size.width)
965 new_x = rect.pos.x + rect.size.width - MENUW(menu) - 1;
967 move_menus(menu, new_x, new_y);
969 while (!done && menu->flags.mapped) {
970 XAllowEvents(dpy, AsyncKeyboard, CurrentTime);
971 WMMaskEvent(dpy, ExposureMask|ButtonMotionMask|ButtonPressMask
972 |ButtonReleaseMask|KeyPressMask|KeyReleaseMask
973 |SubstructureNotifyMask, &event);
975 switch (event.type) {
976 case KeyPress:
977 ksym = XLookupKeysym(&event.xkey, 0);
978 switch (ksym) {
979 case XK_Escape:
980 done = 1;
981 break;
983 case XK_Home:
984 #ifdef XK_KP_Home
985 case XK_KP_Home:
986 #endif
987 selectEntry(menu, 0);
988 makeVisible(menu);
989 break;
991 case XK_End:
992 #ifdef XK_KP_End
993 case XK_KP_End:
994 #endif
995 selectEntry(menu, menu->entry_no-1);
996 makeVisible(menu);
997 break;
999 case XK_Up:
1000 #ifdef ARROWLESS_KBD
1001 case XK_k:
1002 #endif
1003 #ifdef XK_KP_Up
1004 case XK_KP_Up:
1005 #endif
1006 if (menu->selected_entry <= 0)
1007 selectEntry(menu, menu->entry_no-1);
1008 else
1009 selectEntry(menu, menu->selected_entry-1);
1010 makeVisible(menu);
1011 break;
1013 case XK_Down:
1014 #ifdef ARROWLESS_KBD
1015 case XK_j:
1016 #endif
1017 #ifdef XK_KP_Down
1018 case XK_KP_Down:
1019 #endif
1020 if (menu->selected_entry<0)
1021 selectEntry(menu, 0);
1022 else if (menu->selected_entry == menu->entry_no-1)
1023 selectEntry(menu, 0);
1024 else if (menu->selected_entry < menu->entry_no-1)
1025 selectEntry(menu, menu->selected_entry+1);
1026 makeVisible(menu);
1027 break;
1029 case XK_Right:
1030 #ifdef ARROWLESS_KBD
1031 case XK_l:
1032 #endif
1033 #ifdef XK_KP_Right
1034 case XK_KP_Right:
1035 #endif
1036 if (menu->selected_entry>=0) {
1037 WMenuEntry *entry;
1038 entry = menu->entries[menu->selected_entry];
1040 if (entry->cascade >= 0 && menu->cascades
1041 && menu->cascades[entry->cascade]->entry_no > 0) {
1043 XUngrabKeyboard(dpy, CurrentTime);
1045 selectEntry(menu->cascades[entry->cascade], 0);
1046 if (!keyboardMenu(menu->cascades[entry->cascade]))
1047 done = 1;
1049 XGrabKeyboard(dpy, menu->frame->core->window, True,
1050 GrabModeAsync, GrabModeAsync,
1051 CurrentTime);
1054 break;
1056 case XK_Left:
1057 #ifdef ARROWLESS_KBD
1058 case XK_h:
1059 #endif
1060 #ifdef XK_KP_Left
1061 case XK_KP_Left:
1062 #endif
1063 if (menu->parent!=NULL && menu->parent->selected_entry>=0) {
1064 selectEntry(menu, -1);
1065 move_menus(menu, old_pos_x, old_pos_y);
1066 return True;
1068 break;
1070 case XK_Return:
1071 done = 2;
1072 break;
1074 default:
1075 index = check_key(menu, &event.xkey);
1076 if (index>=0) {
1077 selectEntry(menu, index);
1080 break;
1082 default:
1083 if (event.type==ButtonPress)
1084 done = 1;
1086 WMHandleEvent(&event);
1090 XUngrabKeyboard(dpy, CurrentTime);
1092 if (done==2 && menu->selected_entry>=0) {
1093 entry = menu->entries[menu->selected_entry];
1094 } else {
1095 entry = NULL;
1098 if (entry && entry->callback!=NULL && entry->flags.enabled
1099 && entry->cascade < 0) {
1100 #if (MENU_BLINK_COUNT > 0)
1101 int sel = menu->selected_entry;
1102 int i;
1104 for (i=0; i<MENU_BLINK_COUNT; i++) {
1105 paintEntry(menu, sel, False);
1106 XSync(dpy, 0);
1107 wusleep(MENU_BLINK_DELAY);
1108 paintEntry(menu, sel, True);
1109 XSync(dpy, 0);
1110 wusleep(MENU_BLINK_DELAY);
1112 #endif
1113 selectEntry(menu, -1);
1115 if (!menu->flags.buttoned) {
1116 wMenuUnmap(menu);
1117 move_menus(menu, old_pos_x, old_pos_y);
1119 closeCascade(menu);
1121 (*entry->callback)(menu, entry);
1122 } else {
1123 if (!menu->flags.buttoned) {
1124 wMenuUnmap(menu);
1125 move_menus(menu, old_pos_x, old_pos_y);
1127 selectEntry(menu, -1);
1131 /* returns True if returning from a submenu to a parent menu,
1132 * False if exiting from menu */
1133 return False;
1137 void
1138 wMenuMapAt(WMenu *menu, int x, int y, int keyboard)
1140 WMRect rect = wGetRectForHead(menu->frame->screen_ptr,
1141 wGetHeadForPointerLocation(menu->frame->screen_ptr));
1143 if (!menu->flags.realized) {
1144 menu->flags.realized=1;
1145 wMenuRealize(menu);
1147 if (!menu->flags.mapped) {
1148 if (wPreferences.wrap_menus) {
1149 if (x<rect.pos.x) x = rect.pos.x;
1150 if (y<rect.pos.y) y = rect.pos.y;
1151 if (x+MENUW(menu) > rect.pos.x + rect.size.width)
1152 x = rect.pos.x + rect.size.width - MENUW(menu);
1153 if (y+MENUH(menu) > rect.pos.y + rect.size.height)
1154 y = rect.pos.y + rect.size.height - MENUH(menu);
1157 XMoveWindow(dpy, menu->frame->core->window, x, y);
1158 menu->frame_x = x;
1159 menu->frame_y = y;
1160 XMapWindow(dpy, menu->frame->core->window);
1161 wRaiseFrame(menu->frame->core);
1162 menu->flags.mapped = 1;
1163 } else {
1164 selectEntry(menu, 0);
1167 if (keyboard)
1168 keyboardMenu(menu);
1172 void
1173 wMenuMap(WMenu *menu)
1175 if (!menu->flags.realized) {
1176 menu->flags.realized=1;
1177 wMenuRealize(menu);
1179 if (menu->flags.app_menu && menu->parent==NULL) {
1180 menu->frame_x = menu->frame->screen_ptr->app_menu_x;
1181 menu->frame_y = menu->frame->screen_ptr->app_menu_y;
1182 XMoveWindow(dpy, menu->frame->core->window, menu->frame_x, menu->frame_y);
1184 XMapWindow(dpy, menu->frame->core->window);
1185 wRaiseFrame(menu->frame->core);
1186 menu->flags.mapped = 1;
1190 void
1191 wMenuUnmap(WMenu *menu)
1193 int i;
1195 XUnmapWindow(dpy, menu->frame->core->window);
1196 if (menu->flags.titled && menu->flags.buttoned) {
1197 wFrameWindowHideButton(menu->frame, WFF_RIGHT_BUTTON);
1199 menu->flags.buttoned = 0;
1200 menu->flags.mapped = 0;
1201 menu->flags.open_to_left = 0;
1203 for (i=0; i<menu->cascade_no; i++) {
1204 if (menu->cascades[i]!=NULL
1205 && menu->cascades[i]->flags.mapped
1206 && !menu->cascades[i]->flags.buttoned) {
1208 wMenuUnmap(menu->cascades[i]);
1211 menu->selected_entry = -1;
1216 void
1217 wMenuPaint(WMenu *menu)
1219 int i;
1221 if (!menu->flags.mapped) {
1222 return;
1225 /* paint entries */
1226 for (i=0; i<menu->entry_no; i++) {
1227 paintEntry(menu, i, i==menu->selected_entry);
1232 void
1233 wMenuSetEnabled(WMenu *menu, int index, int enable)
1235 if (index>=menu->entry_no) return;
1236 menu->entries[index]->flags.enabled=enable;
1237 paintEntry(menu, index, index==menu->selected_entry);
1238 paintEntry(menu->brother, index, index==menu->selected_entry);
1242 /* ====================================================================== */
1245 static void
1246 editEntry(WMenu *menu, WMenuEntry *entry)
1248 WTextInput *text;
1249 XEvent event;
1250 WObjDescriptor *desc;
1251 char *t;
1252 int done = 0;
1253 Window old_focus;
1254 int old_revert;
1256 menu->flags.editing = 1;
1258 text = wTextCreate(menu->menu, 1, menu->entry_height * entry->order,
1259 menu->menu->width - 2, menu->entry_height - 1);
1261 wTextPutText(text, entry->text);
1262 XGetInputFocus(dpy, &old_focus, &old_revert);
1263 XSetInputFocus(dpy, text->core->window, RevertToNone, CurrentTime);
1265 if (XGrabKeyboard(dpy, text->core->window, True, GrabModeAsync,
1266 GrabModeAsync, CurrentTime)!=GrabSuccess) {
1267 wwarning(_("could not grab keyboard"));
1268 wTextDestroy(text);
1270 wSetFocusTo(menu->frame->screen_ptr,
1271 menu->frame->screen_ptr->focused_window);
1272 return;
1276 while (!done && !text->done) {
1277 XSync(dpy, 0);
1278 XAllowEvents(dpy, AsyncKeyboard|AsyncPointer, CurrentTime);
1279 XSync(dpy, 0);
1280 WMNextEvent(dpy, &event);
1282 if (XFindContext(dpy, event.xany.window, wWinContext,
1283 (XPointer *)&desc)==XCNOENT)
1284 desc = NULL;
1286 if ((desc != NULL) && (desc->handle_anything != NULL)) {
1288 (*desc->handle_anything)(desc, &event);
1290 } else {
1291 switch (event.type) {
1292 case ButtonPress:
1293 XAllowEvents(dpy, ReplayPointer, CurrentTime);
1294 done = 1;
1296 default:
1297 WMHandleEvent(&event);
1298 break;
1303 XSetInputFocus(dpy, old_focus, old_revert, CurrentTime);
1305 wSetFocusTo(menu->frame->screen_ptr,
1306 menu->frame->screen_ptr->focused_window);
1309 t = wTextGetText(text);
1310 /* if !t, the user has canceled editing */
1311 if (t) {
1312 if (entry->text)
1313 wfree(entry->text);
1314 entry->text = wstrdup(t);
1316 menu->flags.realized = 0;
1318 wTextDestroy(text);
1320 XUngrabKeyboard(dpy, CurrentTime);
1322 if (t && menu->on_edit)
1323 (*menu->on_edit)(menu, entry);
1325 menu->flags.editing = 0;
1327 if (!menu->flags.realized)
1328 wMenuRealize(menu);
1332 static void
1333 selectEntry(WMenu *menu, int entry_no)
1335 WMenuEntry *entry;
1336 WMenu *submenu;
1337 int old_entry;
1339 if (menu->entries==NULL)
1340 return;
1342 if (entry_no >= menu->entry_no)
1343 return;
1345 old_entry = menu->selected_entry;
1346 menu->selected_entry = entry_no;
1348 if (old_entry!=entry_no) {
1350 /* unselect previous entry */
1351 if (old_entry>=0) {
1352 paintEntry(menu, old_entry, False);
1353 entry = menu->entries[old_entry];
1355 /* unmap cascade */
1356 if (entry->cascade>=0 && menu->cascades) {
1357 if (!menu->cascades[entry->cascade]->flags.buttoned) {
1358 wMenuUnmap(menu->cascades[entry->cascade]);
1363 if (entry_no<0) {
1364 menu->selected_entry = -1;
1365 return;
1367 entry = menu->entries[entry_no];
1369 if (entry->cascade>=0 && menu->cascades && entry->flags.enabled) {
1370 /* Callback for when the submenu is opened.
1372 submenu = menu->cascades[entry->cascade];
1373 if (submenu && submenu->flags.brother)
1374 submenu = submenu->brother;
1376 if (entry->callback) {
1377 /* Only call the callback if the submenu is not yet mapped.
1379 if (menu->flags.brother) {
1380 if (!submenu || !submenu->flags.mapped)
1381 (*entry->callback)(menu->brother, entry);
1382 } else {
1383 if (!submenu || !submenu->flags.buttoned)
1384 (*entry->callback)(menu, entry);
1388 /* the submenu menu might have changed */
1389 submenu = menu->cascades[entry->cascade];
1391 /* map cascade */
1392 if (!submenu->flags.mapped) {
1393 int x, y;
1395 if (!submenu->flags.realized)
1396 wMenuRealize(submenu);
1397 if (wPreferences.wrap_menus) {
1398 if (menu->flags.open_to_left)
1399 submenu->flags.open_to_left = 1;
1401 if (submenu->flags.open_to_left) {
1402 x = menu->frame_x - MENUW(submenu);
1403 if (x<0) {
1404 x = 0;
1405 submenu->flags.open_to_left = 0;
1407 } else {
1408 x = menu->frame_x + MENUW(menu);
1410 if (x + MENUW(submenu)
1411 >= menu->frame->screen_ptr->scr_width) {
1413 x = menu->frame_x - MENUW(submenu);
1414 submenu->flags.open_to_left = 1;
1417 } else {
1418 x = menu->frame_x + MENUW(menu);
1421 if (wPreferences.align_menus) {
1422 y = menu->frame_y;
1423 } else {
1424 y = menu->frame_y + menu->entry_height*entry_no;
1425 if (menu->flags.titled)
1426 y += menu->frame->top_width;
1427 if (menu->cascades[entry->cascade]->flags.titled)
1428 y -= menu->cascades[entry->cascade]->frame->top_width;
1431 wMenuMapAt(menu->cascades[entry->cascade], x, y, False);
1432 menu->cascades[entry->cascade]->parent = menu;
1433 } else {
1434 return;
1437 paintEntry(menu, entry_no, True);
1442 static WMenu*
1443 findMenu(WScreen *scr, int *x_ret, int *y_ret)
1445 WMenu *menu;
1446 WObjDescriptor *desc;
1447 Window root_ret, win, junk_win;
1448 int x, y, wx, wy;
1449 unsigned int mask;
1451 XQueryPointer(dpy, scr->root_win, &root_ret, &win, &x, &y, &wx, &wy,
1452 &mask);
1454 if (win==None) return NULL;
1456 if (XFindContext(dpy, win, wWinContext, (XPointer *)&desc)==XCNOENT)
1457 return NULL;
1459 if (desc->parent_type == WCLASS_MENU) {
1460 menu = (WMenu*)desc->parent;
1461 XTranslateCoordinates(dpy, root_ret, menu->menu->window, wx, wy,
1462 x_ret, y_ret, &junk_win);
1463 return menu;
1465 return NULL;
1471 static void
1472 closeCascade(WMenu *menu)
1474 WMenu *parent=menu->parent;
1476 if (menu->flags.brother
1477 || (!menu->flags.buttoned
1478 && (!menu->flags.app_menu||menu->parent!=NULL))) {
1480 selectEntry(menu, -1);
1481 XSync(dpy, 0);
1482 #if (MENU_BLINK_DELAY > 2)
1483 wusleep(MENU_BLINK_DELAY/2);
1484 #endif
1485 wMenuUnmap(menu);
1486 while (parent!=NULL
1487 && (parent->parent!=NULL || !parent->flags.app_menu
1488 || parent->flags.brother)
1489 && !parent->flags.buttoned) {
1490 selectEntry(parent, -1);
1491 wMenuUnmap(parent);
1492 parent = parent->parent;
1494 if (parent)
1495 selectEntry(parent, -1);
1500 static void
1501 closeBrotherCascadesOf(WMenu *menu)
1503 WMenu *tmp;
1504 int i;
1506 for (i=0; i<menu->cascade_no; i++) {
1507 if (menu->cascades[i]->flags.brother) {
1508 tmp = menu->cascades[i];
1509 } else {
1510 tmp = menu->cascades[i]->brother;
1512 if (tmp->flags.mapped) {
1513 selectEntry(tmp->parent, -1);
1514 closeBrotherCascadesOf(tmp);
1515 break;
1521 #define getEntryAt(menu, x, y) ((y)<0 ? -1 : (y)/(menu->entry_height))
1524 static WMenu*
1525 parentMenu(WMenu *menu)
1527 WMenu *parent;
1528 WMenuEntry *entry;
1530 if (menu->flags.buttoned)
1531 return menu;
1533 while (menu->parent && menu->parent->flags.mapped) {
1534 parent = menu->parent;
1535 if (parent->selected_entry < 0)
1536 break;
1537 entry = parent->entries[parent->selected_entry];
1538 if (!entry->flags.enabled || entry->cascade<0 || !parent->cascades ||
1539 parent->cascades[entry->cascade] != menu)
1540 break;
1541 menu = parent;
1542 if (menu->flags.buttoned)
1543 break;
1546 return menu;
1552 * Will raise the passed menu, if submenu = 0
1553 * If submenu > 0 will also raise all mapped submenus
1554 * until the first buttoned one
1555 * If submenu < 0 will also raise all mapped parent menus
1556 * until the first buttoned one
1559 static void
1560 raiseMenus(WMenu *menu, int submenus)
1562 WMenu *submenu;
1563 int i;
1565 if(!menu) return;
1567 wRaiseFrame(menu->frame->core);
1569 if (submenus>0 && menu->selected_entry>=0) {
1570 i = menu->entries[menu->selected_entry]->cascade;
1571 if (i>=0 && menu->cascades) {
1572 submenu = menu->cascades[i];
1573 if (submenu->flags.mapped && !submenu->flags.buttoned)
1574 raiseMenus(submenu, submenus);
1577 if (submenus<0 && !menu->flags.buttoned &&
1578 menu->parent && menu->parent->flags.mapped)
1579 raiseMenus(menu->parent, submenus);
1583 WMenu*
1584 wMenuUnderPointer(WScreen *screen)
1586 WObjDescriptor *desc;
1587 Window root_ret, win;
1588 int dummy;
1589 unsigned int mask;
1591 XQueryPointer(dpy, screen->root_win, &root_ret, &win, &dummy, &dummy,
1592 &dummy, &dummy, &mask);
1594 if (win==None) return NULL;
1596 if (XFindContext(dpy, win, wWinContext, (XPointer *)&desc)==XCNOENT)
1597 return NULL;
1599 if (desc->parent_type == WCLASS_MENU)
1600 return (WMenu *)desc->parent;
1601 return NULL;
1607 static void
1608 getPointerPosition(WScreen *scr, int *x, int *y)
1610 Window root_ret, win;
1611 int wx, wy;
1612 unsigned int mask;
1614 XQueryPointer(dpy, scr->root_win, &root_ret, &win, x, y, &wx, &wy, &mask);
1618 static void
1619 getScrollAmount(WMenu *menu, int *hamount, int *vamount)
1621 WScreen *scr = menu->menu->screen_ptr;
1622 int menuX1 = menu->frame_x;
1623 int menuY1 = menu->frame_y;
1624 int menuX2 = menu->frame_x + MENUW(menu);
1625 int menuY2 = menu->frame_y + MENUH(menu);
1626 int xroot, yroot;
1627 WMRect rect = wGetRectForHead(scr, wGetHeadForPointerLocation(scr));
1630 *hamount = 0;
1631 *vamount = 0;
1633 getPointerPosition(scr, &xroot, &yroot);
1635 if (xroot <= (rect.pos.x + 1) && menuX1 < rect.pos.x) {
1636 /* scroll to the right */
1637 *hamount = WMIN(MENU_SCROLL_STEP, abs(menuX1));
1639 } else if (xroot >= (rect.pos.x + rect.size.width - 2) &&
1640 menuX2 > (rect.pos.x + rect.size.width - 1)) {
1641 /* scroll to the left */
1642 *hamount = WMIN(MENU_SCROLL_STEP, abs(menuX2-rect.pos.x-rect.size.width-1));
1644 if (*hamount==0)
1645 *hamount = 1;
1647 *hamount = -*hamount;
1650 if (yroot <= (rect.pos.y + 1) && menuY1 < rect.pos.y) {
1651 /* scroll down */
1652 *vamount = WMIN(MENU_SCROLL_STEP, abs(menuY1));
1654 } else if (yroot >= (rect.pos.y + rect.size.height - 2) &&
1655 menuY2 > (rect.pos.y + rect.size.height - 1)) {
1656 /* scroll up */
1657 *vamount = WMIN(MENU_SCROLL_STEP, abs(menuY2-rect.pos.y-rect.size.height-2));
1659 *vamount = -*vamount;
1664 static void
1665 dragScrollMenuCallback(void *data)
1667 WMenu *menu = (WMenu*)data;
1668 WScreen *scr = menu->menu->screen_ptr;
1669 WMenu *parent = parentMenu(menu);
1670 int hamount, vamount;
1671 int x, y;
1672 int newSelectedEntry;
1674 getScrollAmount(menu, &hamount, &vamount);
1677 if (hamount != 0 || vamount != 0) {
1678 wMenuMove(parent, parent->frame_x + hamount,
1679 parent->frame_y + vamount, True);
1680 if (findMenu(scr, &x, &y)) {
1681 newSelectedEntry = getEntryAt(menu, x, y);
1682 selectEntry(menu, newSelectedEntry);
1683 } else {
1684 /* Pointer fell outside of menu. If the selected entry is
1685 * not a submenu, unselect it */
1686 if (menu->selected_entry >= 0
1687 && menu->entries[menu->selected_entry]->cascade<0)
1688 selectEntry(menu, -1);
1689 newSelectedEntry = 0;
1692 /* paranoid check */
1693 if (newSelectedEntry >= 0) {
1694 /* keep scrolling */
1695 menu->timer = WMAddTimerHandler(MENU_SCROLL_DELAY,
1696 dragScrollMenuCallback, menu);
1697 } else {
1698 menu->timer = NULL;
1700 } else {
1701 /* don't need to scroll anymore */
1702 menu->timer = NULL;
1703 if (findMenu(scr, &x, &y)) {
1704 newSelectedEntry = getEntryAt(menu, x, y);
1705 selectEntry(menu, newSelectedEntry);
1711 static void
1712 scrollMenuCallback(void *data)
1714 WMenu *menu = (WMenu*)data;
1715 WMenu *parent = parentMenu(menu);
1716 int hamount = 0; /* amount to scroll */
1717 int vamount = 0;
1719 #ifdef VIRTUAL_DESKTOP
1720 /* don't scroll if it is in vdesk mode */
1721 if (!wPreferences.vedge_thickness)
1722 #endif
1723 getScrollAmount(menu, &hamount, &vamount);
1725 if (hamount != 0 || vamount != 0) {
1726 wMenuMove(parent, parent->frame_x + hamount,
1727 parent->frame_y + vamount, True);
1729 /* keep scrolling */
1730 menu->timer = WMAddTimerHandler(MENU_SCROLL_DELAY,
1731 scrollMenuCallback, menu);
1732 } else {
1733 /* don't need to scroll anymore */
1734 menu->timer = NULL;
1740 #define MENU_SCROLL_BORDER 5
1742 static int
1743 isPointNearBoder(WMenu *menu, int x, int y)
1745 int menuX1 = menu->frame_x;
1746 int menuY1 = menu->frame_y;
1747 int menuX2 = menu->frame_x + MENUW(menu);
1748 int menuY2 = menu->frame_y + MENUH(menu);
1749 int flag = 0;
1750 int head = wGetHeadForPoint(menu->frame->screen_ptr, wmkpoint(x, y));
1751 WMRect rect = wGetRectForHead(menu->frame->screen_ptr, head);
1753 /* XXX: handle screen joins properly !! */
1755 if (x >= menuX1 && x <= menuX2 &&
1756 (y < rect.pos.y + MENU_SCROLL_BORDER ||
1757 y >= rect.pos.y + rect.size.height - MENU_SCROLL_BORDER))
1758 flag = 1;
1759 else if (y >= menuY1 && y <= menuY2 &&
1760 (x < rect.pos.x + MENU_SCROLL_BORDER ||
1761 x >= rect.pos.x + rect.size.width - MENU_SCROLL_BORDER))
1762 flag = 1;
1764 return flag;
1768 typedef struct _delay {
1769 WMenu *menu;
1770 int ox, oy;
1771 } _delay;
1774 static void
1775 leaving(_delay *dl)
1777 wMenuMove(dl->menu, dl->ox, dl->oy, True);
1778 dl->menu->jump_back = NULL;
1779 dl->menu->menu->screen_ptr->flags.jump_back_pending = 0;
1780 wfree(dl);
1784 void
1785 wMenuScroll(WMenu *menu, XEvent *event)
1787 WMenu *smenu;
1788 WMenu *omenu = parentMenu(menu);
1789 WScreen *scr = menu->frame->screen_ptr;
1790 int done = 0;
1791 int jump_back = 0;
1792 int old_frame_x = omenu->frame_x;
1793 int old_frame_y = omenu->frame_y;
1794 XEvent ev;
1796 if (omenu->jump_back)
1797 WMDeleteTimerWithClientData(omenu->jump_back);
1800 if ((/*omenu->flags.buttoned &&*/ !wPreferences.wrap_menus)
1801 || omenu->flags.app_menu) {
1802 jump_back = 1;
1805 if (!wPreferences.wrap_menus)
1806 raiseMenus(omenu, True);
1807 else
1808 raiseMenus(menu, False);
1810 if (!menu->timer)
1811 scrollMenuCallback(menu);
1813 while(!done) {
1814 int x, y, on_border, on_x_edge, on_y_edge, on_title;
1815 WMRect rect;
1817 WMNextEvent(dpy, &ev);
1818 switch (ev.type) {
1819 case EnterNotify:
1820 WMHandleEvent(&ev);
1821 case MotionNotify:
1822 x = (ev.type==MotionNotify) ? ev.xmotion.x_root : ev.xcrossing.x_root;
1823 y = (ev.type==MotionNotify) ? ev.xmotion.y_root : ev.xcrossing.y_root;
1825 /* on_border is != 0 if the pointer is between the menu
1826 * and the screen border and is close enough to the border */
1827 on_border = isPointNearBoder(menu, x, y);
1829 smenu = wMenuUnderPointer(scr);
1831 if ((smenu==NULL && !on_border) || (smenu && parentMenu(smenu)!=omenu)) {
1832 done = 1;
1833 break;
1836 rect = wGetRectForHead(scr, wGetHeadForPoint(scr, wmkpoint(x, y)));
1837 on_x_edge = x <= rect.pos.x + 1 || x >= rect.pos.x + rect.size.width - 2;
1838 on_y_edge = y <= rect.pos.y + 1 || y >= rect.pos.y + rect.size.height - 2;
1839 on_border = on_x_edge || on_y_edge;
1841 if (!on_border && !jump_back) {
1842 done = 1;
1843 break;
1846 if (menu->timer && (smenu!=menu || (!on_y_edge && !on_x_edge))) {
1847 WMDeleteTimerHandler(menu->timer);
1848 menu->timer = NULL;
1851 if (smenu != NULL)
1852 menu = smenu;
1854 if (!menu->timer)
1855 scrollMenuCallback(menu);
1856 break;
1857 case ButtonPress:
1858 /* True if we push on title, or drag the omenu to other position */
1859 on_title = ev.xbutton.x_root >= omenu->frame_x &&
1860 ev.xbutton.x_root <= omenu->frame_x + MENUW(omenu) &&
1861 ev.xbutton.y_root >= omenu->frame_y &&
1862 ev.xbutton.y_root <= omenu->frame_y + omenu->frame->top_width;
1863 WMHandleEvent(&ev);
1864 smenu = wMenuUnderPointer(scr);
1865 if (smenu == NULL || (smenu && smenu->flags.buttoned && smenu != omenu))
1866 done = 1;
1867 else if (smenu==omenu && on_title) {
1868 jump_back = 0;
1869 done = 1;
1871 break;
1872 case KeyPress:
1873 done = 1;
1874 default:
1875 WMHandleEvent(&ev);
1876 break;
1880 if (menu->timer) {
1881 WMDeleteTimerHandler(menu->timer);
1882 menu->timer = NULL;
1885 if (jump_back) {
1886 _delay *delayer;
1887 if (!omenu->jump_back) {
1888 delayer = wmalloc(sizeof(_delay));
1889 delayer->menu=omenu;
1890 delayer->ox=old_frame_x;
1891 delayer->oy=old_frame_y;
1892 omenu->jump_back = delayer;
1893 scr->flags.jump_back_pending = 1;
1895 else delayer = omenu->jump_back;
1896 WMAddTimerHandler(MENU_JUMP_BACK_DELAY,(WMCallback*)leaving, delayer);
1902 static void
1903 menuExpose(WObjDescriptor *desc, XEvent *event)
1905 wMenuPaint(desc->parent);
1908 typedef struct {
1909 int *delayed_select;
1910 WMenu *menu;
1911 WMHandlerID magic;
1912 } delay_data;
1915 static void
1916 delaySelection(void *data)
1918 delay_data *d = (delay_data*)data;
1919 int x, y, entry_no;
1920 WMenu *menu;
1922 d->magic = NULL;
1924 menu = findMenu(d->menu->menu->screen_ptr, &x, &y);
1925 if (menu && (d->menu == menu || d->delayed_select)) {
1926 entry_no = getEntryAt(menu, x, y);
1927 selectEntry(menu, entry_no);
1929 if (d->delayed_select)
1930 *(d->delayed_select) = 0;
1934 static void
1935 menuMouseDown(WObjDescriptor *desc, XEvent *event)
1937 XButtonEvent *bev = &event->xbutton;
1938 WMenu *menu = desc->parent;
1939 WMenu *smenu;
1940 WScreen *scr=menu->frame->screen_ptr;
1941 WMenuEntry *entry=NULL;
1942 XEvent ev;
1943 int close_on_exit=0;
1944 int done=0;
1945 int delayed_select = 0;
1946 int entry_no;
1947 int x, y;
1948 int prevx, prevy;
1949 int old_frame_x = 0;
1950 int old_frame_y = 0;
1951 delay_data d_data = {NULL, NULL, NULL};
1953 /* Doesn't seem to be needed anymore (if delayed selection handler is
1954 * added only if not present). there seem to be no other side effects
1955 * from removing this and it is also possible that it was only added
1956 * to avoid problems with adding the delayed selection timer handler
1957 * multiple times
1959 /*if (menu->flags.inside_handler) {
1960 return;
1962 menu->flags.inside_handler = 1;
1964 if (!wPreferences.wrap_menus) {
1965 smenu = parentMenu(menu);
1966 old_frame_x = smenu->frame_x;
1967 old_frame_y = smenu->frame_y;
1968 } else if (event->xbutton.window == menu->frame->core->window) {
1969 /* This is true if the menu was launched with right click on root window */
1970 if (!d_data.magic) {
1971 delayed_select = 1;
1972 d_data.delayed_select = &delayed_select;
1973 d_data.menu = menu;
1974 d_data.magic = WMAddTimerHandler(wPreferences.dblclick_time,
1975 delaySelection, &d_data);
1979 wRaiseFrame(menu->frame->core);
1981 close_on_exit = (bev->send_event || menu->flags.brother);
1983 smenu = findMenu(scr, &x, &y);
1984 if (!smenu) {
1985 x = -1;
1986 y = -1;
1987 } else {
1988 menu = smenu;
1991 if (menu->flags.editing) {
1992 goto byebye;
1994 entry_no = getEntryAt(menu, x, y);
1995 if (entry_no>=0) {
1996 entry = menu->entries[entry_no];
1998 if (!close_on_exit && (bev->state & ControlMask) && smenu
1999 && entry->flags.editable) {
2000 editEntry(smenu, entry);
2001 goto byebye;
2002 } else if (bev->state & ControlMask) {
2003 goto byebye;
2006 if (entry->flags.enabled && entry->cascade>=0 && menu->cascades) {
2007 WMenu *submenu = menu->cascades[entry->cascade];
2008 /* map cascade */
2009 if (submenu->flags.mapped && !submenu->flags.buttoned &&
2010 menu->selected_entry!=entry_no) {
2011 wMenuUnmap(submenu);
2013 if (!submenu->flags.mapped && !delayed_select) {
2014 selectEntry(menu, entry_no);
2015 } else if (!submenu->flags.buttoned) {
2016 selectEntry(menu, -1);
2019 } else if (!delayed_select) {
2020 selectEntry(menu, entry_no);
2023 if (!wPreferences.wrap_menus && !wPreferences.scrollable_menus) {
2024 if (!menu->timer)
2025 dragScrollMenuCallback(menu);
2029 #ifdef VIRTUAL_DESKTOP
2030 if (wPreferences.vedge_thickness) {
2031 wWorkspaceLowerEdge(scr);
2033 #endif
2035 prevx = bev->x_root;
2036 prevy = bev->y_root;
2037 while (!done) {
2038 int x, y;
2040 XAllowEvents(dpy, AsyncPointer|SyncPointer, CurrentTime);
2042 WMMaskEvent(dpy, ExposureMask|ButtonMotionMask|ButtonReleaseMask
2043 |ButtonPressMask, &ev);
2044 switch (ev.type) {
2045 case MotionNotify:
2046 smenu = findMenu(scr, &x, &y);
2048 if (smenu == NULL) {
2049 /* moved mouse out of menu */
2051 if (!delayed_select && d_data.magic) {
2052 WMDeleteTimerHandler(d_data.magic);
2053 d_data.magic = NULL;
2055 if (menu==NULL
2056 || (menu->selected_entry>=0
2057 && menu->entries[menu->selected_entry]->cascade>=0)) {
2058 prevx = ev.xmotion.x_root;
2059 prevy = ev.xmotion.y_root;
2061 break;
2063 selectEntry(menu, -1);
2064 menu = smenu;
2065 prevx = ev.xmotion.x_root;
2066 prevy = ev.xmotion.y_root;
2067 break;
2068 } else if (menu && menu!=smenu
2069 && (menu->selected_entry<0
2070 || menu->entries[menu->selected_entry]->cascade<0)) {
2071 selectEntry(menu, -1);
2073 if (!delayed_select && d_data.magic) {
2074 WMDeleteTimerHandler(d_data.magic);
2075 d_data.magic = NULL;
2077 } else {
2079 /* hysteresis for item selection */
2081 /* check if the motion was to the side, indicating that
2082 * the user may want to cross to a submenu */
2083 if (!delayed_select && menu) {
2084 int dx;
2085 Bool moved_to_submenu;/* moved to direction of submenu */
2087 dx = abs(prevx - ev.xmotion.x_root);
2089 moved_to_submenu = False;
2090 if (dx > 0 /* if moved enough to the side */
2091 /* maybe a open submenu */
2092 && menu->selected_entry>=0
2093 /* moving to the right direction */
2094 && (wPreferences.align_menus
2095 || ev.xmotion.y_root >= prevy)) {
2096 int index;
2098 index = menu->entries[menu->selected_entry]->cascade;
2099 if (index>=0) {
2100 if (menu->cascades[index]->frame_x>menu->frame_x) {
2101 if (prevx < ev.xmotion.x_root)
2102 moved_to_submenu = True;
2103 } else {
2104 if (prevx > ev.xmotion.x_root)
2105 moved_to_submenu = True;
2111 if (menu != smenu) {
2112 if (d_data.magic) {
2113 WMDeleteTimerHandler(d_data.magic);
2114 d_data.magic = NULL;
2116 } else if (moved_to_submenu) {
2117 /* while we are moving, postpone the selection */
2118 if (d_data.magic) {
2119 WMDeleteTimerHandler(d_data.magic);
2121 d_data.delayed_select = NULL;
2122 d_data.menu = menu;
2123 d_data.magic = WMAddTimerHandler(MENU_SELECT_DELAY,
2124 delaySelection,
2125 &d_data);
2126 prevx = ev.xmotion.x_root;
2127 prevy = ev.xmotion.y_root;
2128 break;
2129 } else {
2130 if (d_data.magic) {
2131 WMDeleteTimerHandler(d_data.magic);
2132 d_data.magic = NULL;
2137 prevx = ev.xmotion.x_root;
2138 prevy = ev.xmotion.y_root;
2139 if (menu!=smenu) {
2140 /* pointer crossed menus */
2141 if (menu && menu->timer) {
2142 WMDeleteTimerHandler(menu->timer);
2143 menu->timer = NULL;
2145 if (smenu)
2146 dragScrollMenuCallback(smenu);
2148 menu = smenu;
2149 if (!menu->timer)
2150 dragScrollMenuCallback(menu);
2152 if (!delayed_select) {
2153 entry_no = getEntryAt(menu, x, y);
2154 if (entry_no>=0) {
2155 entry = menu->entries[entry_no];
2156 if (entry->flags.enabled && entry->cascade>=0 &&
2157 menu->cascades) {
2158 WMenu *submenu = menu->cascades[entry->cascade];
2159 if (submenu->flags.mapped && !submenu->flags.buttoned
2160 && menu->selected_entry!=entry_no) {
2161 wMenuUnmap(submenu);
2165 selectEntry(menu, entry_no);
2167 break;
2169 case ButtonPress:
2170 break;
2172 case ButtonRelease:
2173 if (ev.xbutton.button == event->xbutton.button)
2174 done=1;
2175 break;
2177 case Expose:
2178 WMHandleEvent(&ev);
2179 #ifdef VIRTUAL_DESKTOP
2180 /* since expose will raise edge up.. I need another ugly hack here */
2181 if (wPreferences.vedge_thickness) {
2182 wWorkspaceLowerEdge(scr);
2184 #endif
2185 break;
2189 if (menu && menu->timer) {
2190 WMDeleteTimerHandler(menu->timer);
2191 menu->timer = NULL;
2193 if (d_data.magic!=NULL) {
2194 WMDeleteTimerHandler(d_data.magic);
2195 d_data.magic = NULL;
2198 if (menu && menu->selected_entry>=0) {
2199 entry = menu->entries[menu->selected_entry];
2200 if (entry->callback!=NULL && entry->flags.enabled
2201 && entry->cascade < 0) {
2202 /* blink and erase menu selection */
2203 #if (MENU_BLINK_DELAY > 0)
2204 int sel = menu->selected_entry;
2205 int i;
2207 for (i=0; i<MENU_BLINK_COUNT; i++) {
2208 paintEntry(menu, sel, False);
2209 XSync(dpy, 0);
2210 wusleep(MENU_BLINK_DELAY);
2211 paintEntry(menu, sel, True);
2212 XSync(dpy, 0);
2213 wusleep(MENU_BLINK_DELAY);
2215 #endif
2216 /* unmap the menu, it's parents and call the callback */
2217 if (!menu->flags.buttoned &&
2218 (!menu->flags.app_menu||menu->parent!=NULL)) {
2219 closeCascade(menu);
2220 } else {
2221 selectEntry(menu, -1);
2223 (*entry->callback)(menu, entry);
2225 /* If the user double clicks an entry, the entry will
2226 * be executed twice, which is not good for things like
2227 * the root menu. So, ignore any clicks that were generated
2228 * while the entry was being executed */
2229 while (XCheckTypedWindowEvent(dpy, menu->menu->window,
2230 ButtonPress, &ev));
2231 } else if (entry->callback!=NULL && entry->cascade<0) {
2232 selectEntry(menu, -1);
2233 } else {
2234 if (entry->cascade>=0 && menu->cascades
2235 && menu->cascades[entry->cascade]->flags.brother) {
2236 selectEntry(menu, -1);
2241 if (((WMenu*)desc->parent)->flags.brother || close_on_exit || !smenu)
2242 closeCascade(desc->parent);
2244 /* close the cascade windows that should not remain opened */
2245 closeBrotherCascadesOf(desc->parent);
2247 if (!wPreferences.wrap_menus)
2248 wMenuMove(parentMenu(desc->parent), old_frame_x, old_frame_y, True);
2250 byebye:
2251 /* Just to be sure in case we skip the 2 above because of a goto byebye */
2252 if (menu && menu->timer) {
2253 WMDeleteTimerHandler(menu->timer);
2254 menu->timer = NULL;
2256 if (d_data.magic!=NULL) {
2257 WMDeleteTimerHandler(d_data.magic);
2258 d_data.magic = NULL;
2261 ((WMenu*)desc->parent)->flags.inside_handler = 0;
2262 #ifdef VIRTUAL_DESKTOP
2263 wWorkspaceRaiseEdge(scr);
2264 #endif
2268 void
2269 wMenuMove(WMenu *menu, int x, int y, int submenus)
2271 WMenu *submenu;
2272 int i;
2274 if (!menu) return;
2276 menu->frame_x = x;
2277 menu->frame_y = y;
2278 XMoveWindow(dpy, menu->frame->core->window, x, y);
2280 if (submenus>0 && menu->selected_entry>=0) {
2281 i = menu->entries[menu->selected_entry]->cascade;
2283 if (i>=0 && menu->cascades) {
2284 submenu = menu->cascades[i];
2285 if (submenu->flags.mapped && !submenu->flags.buttoned) {
2286 if (wPreferences.align_menus) {
2287 wMenuMove(submenu, x + MENUW(menu), y, submenus);
2288 } else {
2289 wMenuMove(submenu, x+ MENUW(menu),
2290 y + submenu->entry_height*menu->selected_entry,
2291 submenus);
2296 if (submenus<0 && menu->parent!=NULL && menu->parent->flags.mapped &&
2297 !menu->parent->flags.buttoned) {
2298 if (wPreferences.align_menus) {
2299 wMenuMove(menu->parent, x - MENUW(menu->parent), y, submenus);
2300 } else {
2301 wMenuMove(menu->parent, x - MENUW(menu->parent), menu->frame_y
2302 - menu->parent->entry_height*menu->parent->selected_entry,
2303 submenus);
2309 static void
2310 changeMenuLevels(WMenu *menu, int lower)
2312 int i;
2314 if (!lower) {
2315 ChangeStackingLevel(menu->frame->core, (!menu->parent ? WMMainMenuLevel
2316 : WMSubmenuLevel));
2317 wRaiseFrame(menu->frame->core);
2318 menu->flags.lowered = 0;
2319 } else {
2320 ChangeStackingLevel(menu->frame->core, WMNormalLevel);
2321 wLowerFrame(menu->frame->core);
2322 menu->flags.lowered = 1;
2324 for (i=0; i<menu->cascade_no; i++) {
2325 if (menu->cascades[i]
2326 && !menu->cascades[i]->flags.buttoned
2327 && menu->cascades[i]->flags.lowered!=lower) {
2328 changeMenuLevels(menu->cascades[i], lower);
2335 static void
2336 menuTitleDoubleClick(WCoreWindow *sender, void *data, XEvent *event)
2338 WMenu *menu = data;
2339 int lower;
2341 if (event->xbutton.state & MOD_MASK) {
2342 if (menu->flags.lowered) {
2343 lower = 0;
2344 } else {
2345 lower = 1;
2347 changeMenuLevels(menu, lower);
2352 static void
2353 menuTitleMouseDown(WCoreWindow *sender, void *data, XEvent *event)
2355 WMenu *menu = data;
2356 WMenu *tmp;
2357 XEvent ev;
2358 int x=menu->frame_x, y=menu->frame_y;
2359 int dx=event->xbutton.x_root, dy=event->xbutton.y_root;
2360 int i, lower;
2361 Bool started;
2363 /* can't touch the menu copy */
2364 if (menu->flags.brother)
2365 return;
2367 if (event->xbutton.button != Button1 && event->xbutton.button != Button2)
2368 return;
2370 if (event->xbutton.state & MOD_MASK) {
2371 wLowerFrame(menu->frame->core);
2372 lower = 1;
2373 } else {
2374 wRaiseFrame(menu->frame->core);
2375 lower = 0;
2377 tmp = menu;
2379 /* lower/raise all submenus */
2380 while (1) {
2381 if (tmp->selected_entry>=0 && tmp->cascades
2382 && tmp->entries[tmp->selected_entry]->cascade>=0) {
2383 tmp = tmp->cascades[tmp->entries[tmp->selected_entry]->cascade];
2384 if (!tmp || !tmp->flags.mapped)
2385 break;
2386 if (lower)
2387 wLowerFrame(tmp->frame->core);
2388 else
2389 wRaiseFrame(tmp->frame->core);
2390 } else {
2391 break;
2395 /* tear off the menu if it's a root menu or a cascade
2396 application menu */
2397 if (!menu->flags.buttoned && !menu->flags.brother
2398 && (!menu->flags.app_menu||menu->parent!=NULL)) {
2399 menu->flags.buttoned=1;
2400 wFrameWindowShowButton(menu->frame, WFF_RIGHT_BUTTON);
2401 if (menu->parent) {
2402 /* turn off selected menu entry in parent menu */
2403 selectEntry(menu->parent, -1);
2405 /* make parent map the copy in place of the original */
2406 for (i=0; i<menu->parent->cascade_no; i++) {
2407 if (menu->parent->cascades[i] == menu) {
2408 menu->parent->cascades[i] = menu->brother;
2409 break;
2415 started = False;
2416 while(1) {
2417 WMMaskEvent(dpy, ButtonMotionMask|ButtonReleaseMask|ButtonPressMask
2418 |ExposureMask, &ev);
2419 switch (ev.type) {
2420 case MotionNotify:
2421 if (started) {
2422 x += ev.xmotion.x_root - dx;
2423 y += ev.xmotion.y_root - dy;
2424 dx = ev.xmotion.x_root;
2425 dy = ev.xmotion.y_root;
2426 wMenuMove(menu, x, y, True);
2427 } else {
2428 if (abs(ev.xmotion.x_root - dx) > MOVE_THRESHOLD
2429 || abs(ev.xmotion.y_root - dy) > MOVE_THRESHOLD) {
2430 started = True;
2431 XGrabPointer(dpy, menu->frame->titlebar->window, False,
2432 ButtonMotionMask|ButtonReleaseMask
2433 |ButtonPressMask,
2434 GrabModeAsync, GrabModeAsync, None,
2435 wCursor[WCUR_MOVE], CurrentTime);
2438 break;
2440 case ButtonPress:
2441 break;
2443 case ButtonRelease:
2444 if (ev.xbutton.button != event->xbutton.button)
2445 break;
2446 XUngrabPointer(dpy, CurrentTime);
2447 return;
2449 default:
2450 WMHandleEvent(&ev);
2451 break;
2457 *----------------------------------------------------------------------
2458 * menuCloseClick--
2459 * Handles mouse click on the close button of menus. The menu is
2460 * closed when the button is clicked.
2462 * Side effects:
2463 * The closed menu is reinserted at it's parent menus
2464 * cascade list.
2465 *----------------------------------------------------------------------
2467 static void
2468 menuCloseClick(WCoreWindow *sender, void *data, XEvent *event)
2470 WMenu *menu = (WMenu*)data;
2471 WMenu *parent = menu->parent;
2472 int i;
2474 if (parent) {
2475 for (i=0; i<parent->cascade_no; i++) {
2476 /* find the entry that points to the copy */
2477 if (parent->cascades[i] == menu->brother) {
2478 /* make it point to the original */
2479 parent->cascades[i] = menu;
2480 menu->parent = parent;
2481 break;
2485 wMenuUnmap(menu);
2489 static void
2490 saveMenuInfo(WMPropList *dict, WMenu *menu, WMPropList *key)
2492 WMPropList *value, *list;
2493 char buffer[256];
2495 snprintf(buffer, sizeof(buffer), "%i,%i", menu->frame_x, menu->frame_y);
2496 value = WMCreatePLString(buffer);
2497 list = WMCreatePLArray(value, NULL);
2498 if (menu->flags.lowered)
2499 WMAddToPLArray(list, WMCreatePLString("lowered"));
2500 WMPutInPLDictionary(dict, key, list);
2501 WMReleasePropList(value);
2502 WMReleasePropList(list);
2506 void
2507 wMenuSaveState(WScreen *scr)
2509 WMPropList *menus, *key;
2510 int save_menus = 0;
2512 menus = WMCreatePLDictionary(NULL, NULL);
2514 #ifndef LITE
2515 if (scr->switch_menu && scr->switch_menu->flags.buttoned) {
2516 key = WMCreatePLString("SwitchMenu");
2517 saveMenuInfo(menus, scr->switch_menu, key);
2518 WMReleasePropList(key);
2519 save_menus = 1;
2522 if (saveMenuRecurs(menus, scr, scr->root_menu))
2523 save_menus = 1;
2525 #endif /* !LITE */
2526 if (scr->workspace_menu && scr->workspace_menu->flags.buttoned) {
2527 key = WMCreatePLString("WorkspaceMenu");
2528 saveMenuInfo(menus, scr->workspace_menu, key);
2529 WMReleasePropList(key);
2530 save_menus = 1;
2533 if (save_menus) {
2534 key = WMCreatePLString("Menus");
2535 WMPutInPLDictionary(scr->session_state, key, menus);
2536 WMReleasePropList(key);
2538 WMReleasePropList(menus);
2542 #ifndef LITE
2544 static Bool
2545 getMenuPath(WMenu *menu, char *buffer, int bufSize)
2547 Bool ok = True;
2548 int len = 0;
2550 if (!menu->flags.titled || !menu->frame->title[0])
2551 return False;
2553 len = strlen(menu->frame->title);
2554 if (len >= bufSize)
2555 return False;
2557 if (menu->parent) {
2558 ok = getMenuPath(menu->parent, buffer, bufSize - len - 1);
2559 if (!ok)
2560 return False;
2563 strcat(buffer, "\\");
2564 strcat(buffer, menu->frame->title);
2566 return True;
2570 static Bool
2571 saveMenuRecurs(WMPropList *menus, WScreen *scr, WMenu *menu)
2573 WMPropList *key;
2574 int save_menus = 0, i;
2575 char buffer[512];
2576 Bool ok = True;
2579 if (menu->flags.brother)
2580 menu = menu->brother;
2582 if (menu->flags.buttoned && menu != scr->switch_menu) {
2584 buffer[0] = '\0';
2585 ok = getMenuPath(menu, buffer, 510);
2587 if (ok) {
2588 key = WMCreatePLString(buffer);
2589 saveMenuInfo(menus, menu, key);
2590 WMReleasePropList(key);
2591 save_menus = 1;
2595 if (ok) {
2596 for (i = 0; i < menu->cascade_no; i++) {
2597 if (saveMenuRecurs(menus, scr, menu->cascades[i]))
2598 save_menus = 1;
2601 return save_menus;
2603 #endif /* !LITE */
2606 #define COMPLAIN(key) wwarning(_("bad value in menus state info:%s"), key)
2609 static Bool
2610 getMenuInfo(WMPropList *info, int *x, int *y, Bool *lowered)
2612 WMPropList *pos;
2614 *lowered = False;
2616 if (WMIsPLArray(info)) {
2617 WMPropList *flags;
2618 pos = WMGetFromPLArray(info, 0);
2619 flags = WMGetFromPLArray(info, 1);
2620 if (flags != NULL && WMIsPLString(flags) && WMGetFromPLString(flags) != NULL
2621 && strcmp(WMGetFromPLString(flags), "lowered") == 0) {
2622 *lowered = True;
2624 } else {
2625 pos = info;
2628 if (pos != NULL && WMIsPLString(pos)) {
2629 if (sscanf(WMGetFromPLString(pos), "%i,%i", x, y)!=2)
2630 COMPLAIN("Position");
2631 } else {
2632 COMPLAIN("(position, flags...)");
2633 return False;
2636 return True;
2640 static int
2641 restoreMenu(WScreen *scr, WMPropList *menu, int which)
2643 int x, y;
2644 Bool lowered = False;
2645 WMenu *pmenu = NULL;
2647 if (!menu)
2648 return False;
2650 if (!getMenuInfo(menu, &x, &y, &lowered))
2651 return False;
2654 #ifndef LITE
2655 if (which & WSS_SWITCHMENU) {
2656 OpenSwitchMenu(scr, x, y, False);
2657 pmenu = scr->switch_menu;
2659 #endif /* !LITE */
2661 if (pmenu) {
2662 int width = MENUW(pmenu);
2663 int height = MENUH(pmenu);
2664 WMRect rect = wGetRectForHead(scr, wGetHeadForPointerLocation(scr));
2666 if (lowered) {
2667 changeMenuLevels(pmenu, True);
2670 if (x < rect.pos.x - width) x = rect.pos.x;
2671 if (x > rect.pos.x + rect.size.width) x = rect.pos.x + rect.size.width - width;
2672 if (y < rect.pos.y) y = rect.pos.y;
2673 if (y > rect.pos.y + rect.size.height) y = rect.pos.y + rect.size.height - height;
2675 wMenuMove(pmenu, x, y, True);
2676 pmenu->flags.buttoned = 1;
2677 wFrameWindowShowButton(pmenu->frame, WFF_RIGHT_BUTTON);
2678 return True;
2680 return False;
2684 #ifndef LITE
2685 static int
2686 restoreMenuRecurs(WScreen *scr, WMPropList *menus, WMenu *menu, char *path)
2688 WMPropList *key, *entry;
2689 char buffer[512];
2690 int i, x, y, res;
2691 Bool lowered;
2693 if (strlen(path) + strlen(menu->frame->title) > 510)
2694 return False;
2696 snprintf(buffer, sizeof(buffer), "%s\\%s", path, menu->frame->title);
2697 key = WMCreatePLString(buffer);
2698 entry = WMGetFromPLDictionary(menus, key);
2699 res = False;
2701 if (entry && getMenuInfo(entry, &x, &y, &lowered)) {
2703 if (!menu->flags.mapped) {
2704 int width = MENUW(menu);
2705 int height = MENUH(menu);
2706 WMRect rect = wGetRectForHead(scr, wGetHeadForPointerLocation(scr));
2708 wMenuMapAt(menu, x, y, False);
2710 if (menu->parent) {
2711 /* make parent map the copy in place of the original */
2712 for (i=0; i<menu->parent->cascade_no; i++) {
2713 if (menu->parent->cascades[i] == menu) {
2714 menu->parent->cascades[i] = menu->brother;
2715 break;
2719 if (lowered) {
2720 changeMenuLevels(menu, True);
2723 if (x < rect.pos.x - width) x = rect.pos.x;
2724 if (x > rect.pos.x + rect.size.width) x = rect.pos.x + rect.size.width - width;
2725 if (y < rect.pos.y) y = rect.pos.y;
2726 if (y > rect.pos.y + rect.size.height) y = rect.pos.y + rect.size.height - height;
2728 wMenuMove(menu, x, y, True);
2729 menu->flags.buttoned = 1;
2730 wFrameWindowShowButton(menu->frame, WFF_RIGHT_BUTTON);
2731 res = True;
2735 WMReleasePropList(key);
2737 for (i=0; i<menu->cascade_no; i++) {
2738 if (restoreMenuRecurs(scr, menus, menu->cascades[i], buffer) != False)
2739 res = True;
2742 return res;
2744 #endif /* !LITE */
2747 void
2748 wMenuRestoreState(WScreen *scr)
2750 WMPropList *menus, *menu, *key, *skey;
2752 if (!scr->session_state) {
2753 return;
2756 key = WMCreatePLString("Menus");
2757 menus = WMGetFromPLDictionary(scr->session_state, key);
2758 WMReleasePropList(key);
2760 if (!menus)
2761 return;
2763 /* restore menus */
2765 skey = WMCreatePLString("SwitchMenu");
2766 menu = WMGetFromPLDictionary(menus, skey);
2767 WMReleasePropList(skey);
2768 restoreMenu(scr, menu, WSS_SWITCHMENU);
2770 #ifndef LITE
2771 if (!scr->root_menu) {
2772 OpenRootMenu(scr, scr->scr_width*2, 0, False);
2773 wMenuUnmap(scr->root_menu);
2775 restoreMenuRecurs(scr, menus, scr->root_menu, "");
2776 #endif /* !LITE */
2780 void
2781 OpenWorkspaceMenu(WScreen *scr, int x, int y)
2783 WMenu *menu, *parent;
2784 WMenuEntry *entry;
2786 #ifndef LITE
2787 if (!scr->root_menu) {
2788 OpenRootMenu(scr, scr->scr_width*2, 0, False);
2789 wMenuUnmap(scr->root_menu);
2791 #endif
2792 menu = scr->workspace_menu;
2793 if (menu) {
2794 if (menu->flags.mapped) {
2795 if (!menu->flags.buttoned) {
2796 wMenuUnmap(menu);
2797 parent = menu->parent;
2798 if (parent && parent->selected_entry >= 0) {
2799 entry = parent->entries[parent->selected_entry];
2800 if (parent->cascades[entry->cascade] == menu) {
2801 selectEntry(parent, -1);
2802 wMenuMapAt(menu, x, y, False);
2805 } else {
2806 wRaiseFrame(menu->frame->core);
2807 wMenuMapCopyAt(menu, x, y);
2809 } else {
2810 wMenuMapAt(menu, x, y, False);