wmaker: Fix compiler warnings about pointer <--> integer conversion
[wmaker-crm.git] / src / menu.c
blobc6c1837d7c72188ef82d01a58ecee177fef9ba48
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 <stdint.h>
33 #include <unistd.h>
34 #include <ctype.h>
35 #if 0
36 #include <nana.h>
37 #endif
38 #include "WindowMaker.h"
39 #include "wcore.h"
40 #include "framewin.h"
41 #include "menu.h"
42 #include "actions.h"
43 #include "funcs.h"
44 #include "stacking.h"
45 #include "text.h"
46 #include "xinerama.h"
47 #include "workspace.h"
50 /****** Global Variables ******/
52 extern Cursor wCursor[WCUR_LAST];
54 extern XContext wWinContext;
56 extern WPreferences wPreferences;
58 #define MOD_MASK wPreferences.modifier_mask
60 #define MENU_SCROLL_STEP menuScrollParameters[(int)wPreferences.menu_scroll_speed].steps
61 #define MENU_SCROLL_DELAY menuScrollParameters[(int)wPreferences.menu_scroll_speed].delay
65 #define MENUW(m) ((m)->frame->core->width+2*FRAME_BORDER_WIDTH)
66 #define MENUH(m) ((m)->frame->core->height+2*FRAME_BORDER_WIDTH)
69 /***** Local Stuff ******/
72 #define WSS_ROOTMENU (1<<0)
73 #define WSS_SWITCHMENU (1<<1)
74 #define WSS_WSMENU (1<<2)
77 static struct {
78 int steps;
79 int delay;
80 } menuScrollParameters[5] = {
81 {MENU_SCROLL_STEPS_UF, MENU_SCROLL_DELAY_UF},
82 {MENU_SCROLL_STEPS_F, MENU_SCROLL_DELAY_F},
83 {MENU_SCROLL_STEPS_M, MENU_SCROLL_DELAY_M},
84 {MENU_SCROLL_STEPS_S, MENU_SCROLL_DELAY_S},
85 {MENU_SCROLL_STEPS_US, MENU_SCROLL_DELAY_US}};
88 static void menuMouseDown(WObjDescriptor *desc, XEvent *event);
89 static void menuExpose(WObjDescriptor *desc, XEvent *event);
91 static void menuTitleDoubleClick(WCoreWindow *sender, void *data, XEvent *event);
92 static void menuTitleMouseDown(WCoreWindow *sender, void *data, XEvent *event);
94 static void menuCloseClick(WCoreWindow *sender, void *data, XEvent *event);
96 static void updateTexture(WMenu *menu);
98 #ifndef LITE
99 static int saveMenuRecurs(WMPropList *menus, WScreen *scr, WMenu *menu);
100 static int restoreMenuRecurs(WScreen *scr, WMPropList *menus, WMenu *menu, char *path);
101 #endif /* !LITE */
103 static void selectEntry(WMenu *menu, int entry_no);
104 static void closeCascade(WMenu *menu);
107 /****** Notification Observers ******/
109 static void
110 appearanceObserver(void *self, WMNotification *notif)
112 WMenu *menu = (WMenu*)self;
113 int flags = (int)(uintptr_t)WMGetNotificationClientData(notif);
115 if (!menu->flags.realized)
116 return;
118 if (WMGetNotificationName(notif) == WNMenuAppearanceSettingsChanged) {
119 if (flags & WFontSettings) {
120 menu->flags.realized = 0;
121 wMenuRealize(menu);
123 if (flags & WTextureSettings) {
124 if (!menu->flags.brother)
125 updateTexture(menu);
127 if (flags & (WTextureSettings|WColorSettings)) {
128 wMenuPaint(menu);
130 } else if (menu->flags.titled) {
132 if (flags & WFontSettings) {
133 menu->flags.realized = 0;
134 wMenuRealize(menu);
136 if (flags & WTextureSettings) {
137 menu->frame->flags.need_texture_remake = 1;
139 if (flags & (WColorSettings|WTextureSettings)) {
140 wFrameWindowPaint(menu->frame);
145 /************************************/
149 *----------------------------------------------------------------------
150 * wMenuCreate--
151 * Creates a new empty menu with the specified title. If main_menu
152 * is True, the created menu will be a main menu, which has some special
153 * properties such as being placed over other normal menus.
154 * If title is NULL, the menu will have no titlebar.
156 * Returns:
157 * The created menu.
158 *----------------------------------------------------------------------
160 WMenu*
161 wMenuCreate(WScreen *screen, char *title, int main_menu)
163 WMenu *menu;
164 static int brother=0;
165 int tmp, flags;
167 menu = wmalloc(sizeof(WMenu));
169 memset(menu, 0, sizeof(WMenu));
171 #ifdef SINGLE_MENULEVEL
172 tmp = WMSubmenuLevel;
173 #else
174 tmp = (main_menu ? WMMainMenuLevel : WMSubmenuLevel);
175 #endif
177 flags = WFF_SINGLE_STATE|WFF_BORDER;
178 if (title) {
179 flags |= WFF_TITLEBAR|WFF_RIGHT_BUTTON;
180 menu->flags.titled = 1;
182 menu->frame =
183 wFrameWindowCreate(screen, tmp, 8, 2, 1, 1, &wPreferences.menu_title_clearance, flags,
184 screen->menu_title_texture, NULL,
185 screen->menu_title_color,
186 &screen->menu_title_font);
188 menu->frame->core->descriptor.parent = menu;
189 menu->frame->core->descriptor.parent_type = WCLASS_MENU;
190 menu->frame->core->descriptor.handle_mousedown = menuMouseDown;
192 wFrameWindowHideButton(menu->frame, WFF_RIGHT_BUTTON);
194 if (title) {
195 menu->frame->title = wstrdup(title);
198 menu->frame->flags.justification = WTJ_LEFT;
200 menu->frame->rbutton_image = screen->b_pixmaps[WBUT_CLOSE];
202 menu->entry_no = 0;
203 menu->alloced_entries = 0;
204 menu->selected_entry = -1;
205 menu->entries = NULL;
207 menu->frame_x = screen->app_menu_x;
208 menu->frame_y = screen->app_menu_y;
210 menu->frame->child = menu;
212 menu->flags.lowered = 0;
214 /* create borders */
215 if (title) {
216 /* setup object descriptors */
217 menu->frame->on_mousedown_titlebar = menuTitleMouseDown;
218 menu->frame->on_dblclick_titlebar = menuTitleDoubleClick;
221 menu->frame->on_click_right = menuCloseClick;
224 menu->menu = wCoreCreate(menu->frame->core, 0, menu->frame->top_width,
225 menu->frame->core->width, 10);
227 menu->menu->descriptor.parent = menu;
228 menu->menu->descriptor.parent_type = WCLASS_MENU;
229 menu->menu->descriptor.handle_expose = menuExpose;
230 menu->menu->descriptor.handle_mousedown = menuMouseDown;
232 menu->menu_texture_data = None;
234 XMapWindow(dpy, menu->menu->window);
236 XFlush(dpy);
238 if (!brother) {
239 brother = 1;
240 menu->brother = wMenuCreate(screen, title, main_menu);
241 brother = 0;
242 menu->brother->flags.brother = 1;
243 menu->brother->brother = menu;
245 WMAddNotificationObserver(appearanceObserver, menu,
246 WNMenuAppearanceSettingsChanged, menu);
248 WMAddNotificationObserver(appearanceObserver, menu,
249 WNMenuTitleAppearanceSettingsChanged, menu);
252 return menu;
258 WMenu*
259 wMenuCreateForApp(WScreen *screen, char *title, int main_menu)
261 WMenu *menu;
263 menu = wMenuCreate(screen, title, main_menu);
264 if (!menu)
265 return NULL;
266 menu->flags.app_menu = 1;
267 menu->brother->flags.app_menu = 1;
269 return menu;
274 static void
275 insertEntry(WMenu *menu, WMenuEntry *entry, int index)
277 int i;
279 for (i = menu->entry_no-1; i >= index; i--) {
280 menu->entries[i]->order++;
281 menu->entries[i+1] = menu->entries[i];
283 menu->entries[index] = entry;
287 WMenuEntry*
288 wMenuInsertCallback(WMenu *menu, int index, char *text,
289 void (*callback)(WMenu *menu, WMenuEntry *entry),
290 void *clientdata)
292 WMenuEntry *entry;
294 menu->flags.realized = 0;
295 menu->brother->flags.realized = 0;
297 /* reallocate array if it's too small */
298 if (menu->entry_no >= menu->alloced_entries) {
299 void *tmp;
301 tmp = wrealloc(menu->entries,
302 sizeof(WMenuEntry)*(menu->alloced_entries+5));
304 menu->entries = tmp;
305 menu->alloced_entries += 5;
307 menu->brother->entries = tmp;
308 menu->brother->alloced_entries = menu->alloced_entries;
310 entry = wmalloc(sizeof(WMenuEntry));
311 memset(entry, 0, sizeof(WMenuEntry));
312 entry->flags.enabled = 1;
313 entry->text = wstrdup(text);
314 entry->cascade = -1;
315 entry->clientdata = clientdata;
316 entry->callback = callback;
317 if (index<0 || index>=menu->entry_no) {
318 entry->order = menu->entry_no;
319 menu->entries[menu->entry_no] = entry;
320 } else {
321 entry->order = index;
322 insertEntry(menu, entry, index);
325 menu->entry_no++;
326 menu->brother->entry_no = menu->entry_no;
328 return entry;
333 void
334 wMenuEntrySetCascade(WMenu *menu, WMenuEntry *entry, WMenu *cascade)
336 WMenu *brother = menu->brother;
337 int i, done;
339 assert(menu->flags.brother==0);
341 if (entry->cascade>=0) {
342 menu->flags.realized = 0;
343 brother->flags.realized = 0;
346 cascade->parent = menu;
348 cascade->brother->parent = brother;
350 done = 0;
351 for (i=0; i<menu->cascade_no; i++) {
352 if (menu->cascades[i]==NULL) {
353 menu->cascades[i] = cascade;
354 brother->cascades[i] = cascade->brother;
355 done = 1;
356 entry->cascade = i;
357 break;
360 if (!done) {
361 entry->cascade = menu->cascade_no;
363 menu->cascades = wrealloc(menu->cascades,
364 sizeof(WMenu)*(menu->cascade_no+1));
365 menu->cascades[menu->cascade_no++] = cascade;
368 brother->cascades = wrealloc(brother->cascades,
369 sizeof(WMenu)*(brother->cascade_no+1));
370 brother->cascades[brother->cascade_no++] = cascade->brother;
374 if (menu->flags.lowered) {
376 cascade->flags.lowered = 1;
377 ChangeStackingLevel(cascade->frame->core, WMNormalLevel);
379 cascade->brother->flags.lowered = 1;
380 ChangeStackingLevel(cascade->brother->frame->core, WMNormalLevel);
383 if (!menu->flags.realized)
384 wMenuRealize(menu);
388 void
389 wMenuEntryRemoveCascade(WMenu *menu, WMenuEntry *entry)
391 assert(menu->flags.brother==0);
393 /* destroy cascade menu */
394 if (entry->cascade>=0 && menu->cascades
395 && menu->cascades[entry->cascade]!=NULL) {
397 wMenuDestroy(menu->cascades[entry->cascade], True);
399 menu->cascades[entry->cascade] = NULL;
400 menu->brother->cascades[entry->cascade] = NULL;
402 entry->cascade = -1;
407 void
408 wMenuRemoveItem(WMenu *menu, int index)
410 int i;
412 if (menu->flags.brother) {
413 wMenuRemoveItem(menu->brother, index);
414 return;
417 if (index>=menu->entry_no) return;
419 /* destroy cascade menu */
420 wMenuEntryRemoveCascade(menu, menu->entries[index]);
422 /* destroy unshared data */
424 if (menu->entries[index]->text)
425 wfree(menu->entries[index]->text);
427 if (menu->entries[index]->rtext)
428 wfree(menu->entries[index]->rtext);
430 if (menu->entries[index]->free_cdata && menu->entries[index]->clientdata)
431 (*menu->entries[index]->free_cdata)(menu->entries[index]->clientdata);
433 wfree(menu->entries[index]);
435 for (i=index; i<menu->entry_no-1; i++) {
436 menu->entries[i+1]->order--;
437 menu->entries[i]=menu->entries[i+1];
439 menu->entry_no--;
440 menu->brother->entry_no--;
444 static Pixmap
445 renderTexture(WMenu *menu)
447 RImage *img;
448 Pixmap pix;
449 int i;
450 RColor light;
451 RColor dark;
452 RColor mid;
453 WScreen *scr = menu->menu->screen_ptr;
454 WTexture *texture = scr->menu_item_texture;
456 if (wPreferences.menu_style == MS_NORMAL) {
457 img = wTextureRenderImage(texture, menu->menu->width,
458 menu->entry_height, WREL_MENUENTRY);
459 } else {
460 img = wTextureRenderImage(texture, menu->menu->width,
461 menu->menu->height+1, WREL_MENUENTRY);
463 if (!img) {
464 wwarning(_("could not render texture: %s"),
465 RMessageForError(RErrorCode));
467 return None;
470 if (wPreferences.menu_style == MS_SINGLE_TEXTURE) {
471 light.alpha = 0;
472 light.red = light.green = light.blue = 80;
474 dark.alpha = 255;
475 dark.red = dark.green = dark.blue = 0;
477 mid.alpha = 0;
478 mid.red = mid.green = mid.blue = 40;
480 for (i = 1; i < menu->entry_no; i++) {
481 ROperateLine(img, RSubtractOperation, 0, i*menu->entry_height-2,
482 menu->menu->width-1, i*menu->entry_height-2, &mid);
484 RDrawLine(img, 0, i*menu->entry_height-1,
485 menu->menu->width-1, i*menu->entry_height-1, &dark);
487 ROperateLine(img, RAddOperation, 0, i*menu->entry_height,
488 menu->menu->width-1, i*menu->entry_height,
489 &light);
492 if (!RConvertImage(scr->rcontext, img, &pix)) {
493 wwarning(_("error rendering image:%s"), RMessageForError(RErrorCode));
495 RReleaseImage(img);
497 return pix;
501 static void
502 updateTexture(WMenu *menu)
504 WScreen *scr = menu->menu->screen_ptr;
506 /* setup background texture */
507 if (scr->menu_item_texture->any.type != WTEX_SOLID) {
508 if (!menu->flags.brother) {
509 FREE_PIXMAP(menu->menu_texture_data);
511 menu->menu_texture_data = renderTexture(menu);
513 XSetWindowBackgroundPixmap(dpy, menu->menu->window,
514 menu->menu_texture_data);
515 XClearWindow(dpy, menu->menu->window);
517 XSetWindowBackgroundPixmap(dpy, menu->brother->menu->window,
518 menu->menu_texture_data);
519 XClearWindow(dpy, menu->brother->menu->window);
521 } else {
522 XSetWindowBackground(dpy, menu->menu->window,
523 scr->menu_item_texture->any.color.pixel);
524 XClearWindow(dpy, menu->menu->window);
529 void
530 wMenuRealize(WMenu *menu)
532 int i;
533 int width, rwidth, mrwidth, mwidth;
534 int theight, twidth, eheight;
535 WScreen *scr = menu->frame->screen_ptr;
536 static int brother_done=0;
537 int flags;
539 if (!brother_done) {
540 brother_done = 1;
541 wMenuRealize(menu->brother);
542 brother_done = 0;
545 flags = WFF_SINGLE_STATE|WFF_BORDER;
546 if (menu->flags.titled)
547 flags |= WFF_TITLEBAR|WFF_RIGHT_BUTTON;
549 wFrameWindowUpdateBorders(menu->frame, flags);
551 if (menu->flags.titled) {
552 twidth = WMWidthOfString(scr->menu_title_font, menu->frame->title,
553 strlen(menu->frame->title));
554 theight = menu->frame->top_width;
555 twidth += theight + (wPreferences.new_style ? 16 : 8);
556 } else {
557 twidth = 0;
558 theight = 0;
560 eheight = WMFontHeight(scr->menu_entry_font) + 6 + wPreferences.menu_text_clearance * 2;
561 menu->entry_height = eheight;
562 mrwidth = 0;
563 mwidth = 0;
564 for (i=0; i<menu->entry_no; i++) {
565 char *text;
567 /* search widest text */
568 text = menu->entries[i]->text;
569 width = WMWidthOfString(scr->menu_entry_font, text, strlen(text))+10;
571 if (menu->entries[i]->flags.indicator) {
572 width += MENU_INDICATOR_SPACE;
575 if (width > mwidth)
576 mwidth = width;
578 /* search widest text on right */
579 text = menu->entries[i]->rtext;
580 if (text)
581 rwidth = WMWidthOfString(scr->menu_entry_font, text, strlen(text))
582 + 10;
583 else if (menu->entries[i]->cascade>=0)
584 rwidth = 16;
585 else
586 rwidth = 4;
588 if (rwidth > mrwidth)
589 mrwidth = rwidth;
591 mwidth += mrwidth;
593 if (mwidth < twidth)
594 mwidth = twidth;
597 wCoreConfigure(menu->menu, 0, theight, mwidth, menu->entry_no*eheight -1);
599 wFrameWindowResize(menu->frame, mwidth, menu->entry_no*eheight-1
600 + menu->frame->top_width + menu->frame->bottom_width);
603 updateTexture(menu);
605 menu->flags.realized = 1;
607 if (menu->flags.mapped)
608 wMenuPaint(menu);
609 if (menu->brother->flags.mapped)
610 wMenuPaint(menu->brother);
614 void
615 wMenuDestroy(WMenu *menu, int recurse)
617 int i;
619 WMRemoveNotificationObserver(menu);
621 /* remove any pending timers */
622 if (menu->timer)
623 WMDeleteTimerHandler(menu->timer);
624 menu->timer = NULL;
626 /* call destroy handler */
627 if (menu->on_destroy)
628 (*menu->on_destroy)(menu);
630 /* Destroy items if this menu own them. If this is the "brother" menu,
631 * leave them alone as it is shared by them.
633 if (!menu->flags.brother) {
634 for (i=0; i<menu->entry_no; i++) {
636 wfree(menu->entries[i]->text);
638 if (menu->entries[i]->rtext)
639 wfree(menu->entries[i]->rtext);
640 #ifdef USER_MENU
642 if (menu->entries[i]->instances){
643 WMReleasePropList(menu->entries[i]->instances);
645 #endif /* USER_MENU */
647 if (menu->entries[i]->free_cdata && menu->entries[i]->clientdata) {
648 (*menu->entries[i]->free_cdata)(menu->entries[i]->clientdata);
650 wfree(menu->entries[i]);
653 if (recurse) {
654 for (i=0; i<menu->cascade_no; i++) {
655 if (menu->cascades[i]) {
656 if (menu->cascades[i]->flags.brother)
657 wMenuDestroy(menu->cascades[i]->brother, recurse);
658 else
659 wMenuDestroy(menu->cascades[i], recurse);
664 if (menu->entries)
665 wfree(menu->entries);
669 FREE_PIXMAP(menu->menu_texture_data);
671 if (menu->cascades)
672 wfree(menu->cascades);
674 wCoreDestroy(menu->menu);
675 wFrameWindowDestroy(menu->frame);
677 /* destroy copy of this menu */
678 if (!menu->flags.brother && menu->brother)
679 wMenuDestroy(menu->brother, False);
681 wfree(menu);
685 #define F_NORMAL 0
686 #define F_TOP 1
687 #define F_BOTTOM 2
688 #define F_NONE 3
690 static void
691 drawFrame(WScreen *scr, Drawable win, int y, int w, int h, int type)
693 XSegment segs[2];
694 int i;
696 i = 0;
697 segs[i].x1 = segs[i].x2 = w-1;
698 segs[i].y1 = y;
699 segs[i].y2 = y + h - 1;
700 i++;
701 if (type != F_TOP && type != F_NONE) {
702 segs[i].x1 = 1;
703 segs[i].y1 = segs[i].y2 = y + h-2;
704 segs[i].x2 = w-1;
705 i++;
707 XDrawSegments(dpy, win, scr->menu_item_auxtexture->dim_gc, segs, i);
709 i = 0;
710 segs[i].x1 = 0;
711 segs[i].y1 = y;
712 segs[i].x2 = 0;
713 segs[i].y2 = y + h - 1;
714 i++;
715 if (type != F_BOTTOM && type != F_NONE) {
716 segs[i].x1 = 0;
717 segs[i].y1 = y;
718 segs[i].x2 = w-1;
719 segs[i].y2 = y;
720 i++;
722 XDrawSegments(dpy, win, scr->menu_item_auxtexture->light_gc, segs, i);
724 if (type != F_TOP && type != F_NONE)
725 XDrawLine(dpy, win, scr->menu_item_auxtexture->dark_gc, 0, y+h-1,
726 w-1, y+h-1);
730 static void
731 paintEntry(WMenu *menu, int index, int selected)
733 WScreen *scr=menu->frame->screen_ptr;
734 Window win = menu->menu->window;
735 WMenuEntry *entry=menu->entries[index];
736 GC light, dim, dark;
737 WMColor *color;
738 int x, y, w, h, tw;
739 int type;
741 if (!menu->flags.realized) return;
742 h = menu->entry_height;
743 w = menu->menu->width;
744 y = index * h;
746 light = scr->menu_item_auxtexture->light_gc;
747 dim = scr->menu_item_auxtexture->dim_gc;
748 dark = scr->menu_item_auxtexture->dark_gc;
750 if (wPreferences.menu_style == MS_FLAT && menu->entry_no > 1) {
751 if (index == 0)
752 type = F_TOP;
753 else if (index == menu->entry_no - 1)
754 type = F_BOTTOM;
755 else
756 type = F_NONE;
757 } else {
758 type = F_NORMAL;
761 /* paint background */
762 if (selected) {
763 XFillRectangle(dpy, win, WMColorGC(scr->select_color), 1, y+1, w-2, h-3);
764 if (scr->menu_item_texture->any.type == WTEX_SOLID)
765 drawFrame(scr, win, y, w, h, type);
766 } else {
767 if (scr->menu_item_texture->any.type == WTEX_SOLID) {
768 XClearArea(dpy, win, 0, y + 1, w - 1, h - 3, False);
769 /* draw the frame */
770 drawFrame(scr, win, y, w, h, type);
771 } else {
772 XClearArea(dpy, win, 0, y, w, h, False);
776 if (selected) {
777 if (entry->flags.enabled)
778 color = scr->select_text_color;
779 else
780 color = scr->dtext_color;
781 } else if (!entry->flags.enabled) {
782 color = scr->dtext_color;
783 } else {
784 color = scr->mtext_color;
786 /* draw text */
787 x = 5;
788 if (entry->flags.indicator)
789 x += MENU_INDICATOR_SPACE + 2;
791 WMDrawString(scr->wmscreen, win, color, scr->menu_entry_font,
792 x, 3 + y + wPreferences.menu_text_clearance, entry->text, strlen(entry->text));
794 if (entry->cascade>=0) {
795 /* draw the cascade indicator */
796 XDrawLine(dpy, win, dim, w-11, y+6, w-6, y+h/2-1);
797 XDrawLine(dpy, win, light, w-11, y+h-8, w-6, y+h/2-1);
798 XDrawLine(dpy, win, dark, w-12, y+6, w-12, y+h-8);
801 /* draw indicator */
802 if (entry->flags.indicator && entry->flags.indicator_on) {
803 int iw, ih;
804 WPixmap *indicator;
807 switch (entry->flags.indicator_type) {
808 case MI_CHECK:
809 indicator = scr->menu_check_indicator;
810 break;
811 case MI_MINIWINDOW:
812 indicator = scr->menu_mini_indicator;
813 break;
814 case MI_HIDDEN:
815 indicator = scr->menu_hide_indicator;
816 break;
817 case MI_SHADED:
818 indicator = scr->menu_shade_indicator;
819 break;
820 case MI_DIAMOND:
821 default:
822 indicator = scr->menu_radio_indicator;
823 break;
826 iw = indicator->width;
827 ih = indicator->height;
828 XSetClipMask(dpy, scr->copy_gc, indicator->mask);
829 XSetClipOrigin(dpy, scr->copy_gc, 5, y+(h-ih)/2);
830 if (selected) {
831 if (entry->flags.enabled) {
832 XSetForeground(dpy, scr->copy_gc, WMColorPixel(scr->select_text_color));
833 } else {
834 XSetForeground(dpy, scr->copy_gc, WMColorPixel(scr->dtext_color));
836 } else {
837 if (entry->flags.enabled) {
838 XSetForeground(dpy, scr->copy_gc, WMColorPixel(scr->mtext_color));
839 } else {
840 XSetForeground(dpy, scr->copy_gc, WMColorPixel(scr->dtext_color));
843 XFillRectangle(dpy, win, scr->copy_gc, 5, y+(h-ih)/2, iw, ih);
845 XCopyArea(dpy, indicator->image, win, scr->copy_gc, 0, 0,
846 iw, ih, 5, y+(h-ih)/2);
848 XSetClipOrigin(dpy, scr->copy_gc, 0, 0);
851 /* draw right text */
853 if (entry->rtext && entry->cascade<0) {
854 tw = WMWidthOfString(scr->menu_entry_font, entry->rtext,
855 strlen(entry->rtext));
857 WMDrawString(scr->wmscreen, win, color, scr->menu_entry_font, w-6-tw,
858 y + 3 + wPreferences.menu_text_clearance, entry->rtext, strlen(entry->rtext));
863 static void
864 move_menus(WMenu *menu, int x, int y)
866 while (menu->parent) {
867 menu = menu->parent;
868 x -= MENUW(menu);
869 if (!wPreferences.align_menus && menu->selected_entry>=0) {
870 y -= menu->selected_entry*menu->entry_height;
873 wMenuMove(menu, x, y, True);
876 static void
877 makeVisible(WMenu *menu)
879 WScreen *scr = menu->frame->screen_ptr;
880 int x1, y1, x2, y2, new_x, new_y, move;
881 WMRect rect = wGetRectForHead(scr, wGetHeadForPointerLocation(scr));
883 if (menu->entry_no<0) return;
885 x1 = menu->frame_x;
886 y1 = menu->frame_y+menu->frame->top_width
887 + menu->selected_entry*menu->entry_height;
888 x2 = x1 + MENUW(menu);
889 y2 = y1 + menu->entry_height;
891 new_x = x1;
892 new_y = y1;
893 move = 0;
896 if (x1 < rect.pos.x) {
897 new_x = rect.pos.x;
898 move = 1;
899 } else if (x2 >= rect.pos.x + rect.size.width) {
900 new_x = rect.pos.x + rect.size.width - MENUW(menu) - 1;
901 move = 1;
904 if (y1 < rect.pos.y) {
905 new_y = rect.pos.y;
906 move = 1;
907 } else if (y2 >= rect.pos.y + rect.size.height) {
908 new_y = rect.pos.y + rect.size.height - menu->entry_height - 1;
909 move = 1;
912 new_y = new_y - menu->frame->top_width
913 - menu->selected_entry*menu->entry_height;
914 move_menus(menu, new_x, new_y);
918 static int
919 check_key(WMenu *menu, XKeyEvent *event)
921 int i, ch, s;
922 char buffer[32];
924 if (XLookupString(event, buffer, 32, NULL, NULL)<1)
925 return -1;
927 ch = toupper(buffer[0]);
929 s = (menu->selected_entry>=0 ? menu->selected_entry+1 : 0);
931 again:
932 for (i=s; i<menu->entry_no; i++) {
933 if (ch==toupper(menu->entries[i]->text[0])) {
934 return i;
937 /* no match. Retry from start, if previous started from a selected entry */
938 if (s!=0) {
939 s = 0;
940 goto again;
942 return -1;
946 static int
947 keyboardMenu(WMenu *menu)
949 XEvent event;
950 KeySym ksym=NoSymbol;
951 int done=0;
952 int index;
953 WMenuEntry *entry;
954 int old_pos_x = menu->frame_x;
955 int old_pos_y = menu->frame_y;
956 int new_x = old_pos_x, new_y = old_pos_y;
957 WMRect rect = wGetRectForHead(menu->frame->screen_ptr,
958 wGetHeadForPointerLocation(menu->frame->screen_ptr));
960 if (menu->flags.editing)
961 return False;
964 XGrabKeyboard(dpy, menu->frame->core->window, True, GrabModeAsync,
965 GrabModeAsync, CurrentTime);
968 if (menu->frame_y+menu->frame->top_width >= rect.pos.y + rect.size.height)
969 new_y = rect.pos.y + rect.size.height - menu->frame->top_width;
971 if (menu->frame_x+MENUW(menu) >= rect.pos.x + rect.size.width)
972 new_x = rect.pos.x + rect.size.width - MENUW(menu) - 1;
974 move_menus(menu, new_x, new_y);
976 while (!done && menu->flags.mapped) {
977 XAllowEvents(dpy, AsyncKeyboard, CurrentTime);
978 WMMaskEvent(dpy, ExposureMask|ButtonMotionMask|ButtonPressMask
979 |ButtonReleaseMask|KeyPressMask|KeyReleaseMask
980 |SubstructureNotifyMask, &event);
982 switch (event.type) {
983 case KeyPress:
984 ksym = XLookupKeysym(&event.xkey, 0);
985 switch (ksym) {
986 case XK_Escape:
987 done = 1;
988 break;
990 case XK_Home:
991 #ifdef XK_KP_Home
992 case XK_KP_Home:
993 #endif
994 selectEntry(menu, 0);
995 makeVisible(menu);
996 break;
998 case XK_End:
999 #ifdef XK_KP_End
1000 case XK_KP_End:
1001 #endif
1002 selectEntry(menu, menu->entry_no-1);
1003 makeVisible(menu);
1004 break;
1006 case XK_Up:
1007 #ifdef ARROWLESS_KBD
1008 case XK_k:
1009 #endif
1010 #ifdef XK_KP_Up
1011 case XK_KP_Up:
1012 #endif
1013 if (menu->selected_entry <= 0)
1014 selectEntry(menu, menu->entry_no-1);
1015 else
1016 selectEntry(menu, menu->selected_entry-1);
1017 makeVisible(menu);
1018 break;
1020 case XK_Down:
1021 #ifdef ARROWLESS_KBD
1022 case XK_j:
1023 #endif
1024 #ifdef XK_KP_Down
1025 case XK_KP_Down:
1026 #endif
1027 if (menu->selected_entry<0)
1028 selectEntry(menu, 0);
1029 else if (menu->selected_entry == menu->entry_no-1)
1030 selectEntry(menu, 0);
1031 else if (menu->selected_entry < menu->entry_no-1)
1032 selectEntry(menu, menu->selected_entry+1);
1033 makeVisible(menu);
1034 break;
1036 case XK_Right:
1037 #ifdef ARROWLESS_KBD
1038 case XK_l:
1039 #endif
1040 #ifdef XK_KP_Right
1041 case XK_KP_Right:
1042 #endif
1043 if (menu->selected_entry>=0) {
1044 WMenuEntry *entry;
1045 entry = menu->entries[menu->selected_entry];
1047 if (entry->cascade >= 0 && menu->cascades
1048 && menu->cascades[entry->cascade]->entry_no > 0) {
1050 XUngrabKeyboard(dpy, CurrentTime);
1052 selectEntry(menu->cascades[entry->cascade], 0);
1053 if (!keyboardMenu(menu->cascades[entry->cascade]))
1054 done = 1;
1056 XGrabKeyboard(dpy, menu->frame->core->window, True,
1057 GrabModeAsync, GrabModeAsync,
1058 CurrentTime);
1061 break;
1063 case XK_Left:
1064 #ifdef ARROWLESS_KBD
1065 case XK_h:
1066 #endif
1067 #ifdef XK_KP_Left
1068 case XK_KP_Left:
1069 #endif
1070 if (menu->parent!=NULL && menu->parent->selected_entry>=0) {
1071 selectEntry(menu, -1);
1072 move_menus(menu, old_pos_x, old_pos_y);
1073 return True;
1075 break;
1077 case XK_Return:
1078 #ifdef XK_KP_Enter
1079 case XK_KP_Enter:
1080 #endif
1081 done = 2;
1082 break;
1084 default:
1085 index = check_key(menu, &event.xkey);
1086 if (index>=0) {
1087 selectEntry(menu, index);
1090 break;
1092 default:
1093 if (event.type==ButtonPress)
1094 done = 1;
1096 WMHandleEvent(&event);
1100 XUngrabKeyboard(dpy, CurrentTime);
1102 if (done==2 && menu->selected_entry>=0) {
1103 entry = menu->entries[menu->selected_entry];
1104 } else {
1105 entry = NULL;
1108 if (entry && entry->callback!=NULL && entry->flags.enabled
1109 && entry->cascade < 0) {
1110 #if (MENU_BLINK_COUNT > 0)
1111 int sel = menu->selected_entry;
1112 int i;
1114 for (i=0; i<MENU_BLINK_COUNT; i++) {
1115 paintEntry(menu, sel, False);
1116 XSync(dpy, 0);
1117 wusleep(MENU_BLINK_DELAY);
1118 paintEntry(menu, sel, True);
1119 XSync(dpy, 0);
1120 wusleep(MENU_BLINK_DELAY);
1122 #endif
1123 selectEntry(menu, -1);
1125 if (!menu->flags.buttoned) {
1126 wMenuUnmap(menu);
1127 move_menus(menu, old_pos_x, old_pos_y);
1129 closeCascade(menu);
1131 (*entry->callback)(menu, entry);
1132 } else {
1133 if (!menu->flags.buttoned) {
1134 wMenuUnmap(menu);
1135 move_menus(menu, old_pos_x, old_pos_y);
1137 selectEntry(menu, -1);
1141 /* returns True if returning from a submenu to a parent menu,
1142 * False if exiting from menu */
1143 return False;
1147 void
1148 wMenuMapAt(WMenu *menu, int x, int y, int keyboard)
1150 WMRect rect = wGetRectForHead(menu->frame->screen_ptr,
1151 wGetHeadForPointerLocation(menu->frame->screen_ptr));
1153 if (!menu->flags.realized) {
1154 menu->flags.realized=1;
1155 wMenuRealize(menu);
1157 if (!menu->flags.mapped) {
1158 if (wPreferences.wrap_menus) {
1159 if (x<rect.pos.x) x = rect.pos.x;
1160 if (y<rect.pos.y) y = rect.pos.y;
1161 if (x+MENUW(menu) > rect.pos.x + rect.size.width)
1162 x = rect.pos.x + rect.size.width - MENUW(menu);
1163 if (y+MENUH(menu) > rect.pos.y + rect.size.height)
1164 y = rect.pos.y + rect.size.height - MENUH(menu);
1167 XMoveWindow(dpy, menu->frame->core->window, x, y);
1168 menu->frame_x = x;
1169 menu->frame_y = y;
1170 XMapWindow(dpy, menu->frame->core->window);
1171 wRaiseFrame(menu->frame->core);
1172 menu->flags.mapped = 1;
1173 } else {
1174 selectEntry(menu, 0);
1177 if (keyboard)
1178 keyboardMenu(menu);
1182 void
1183 wMenuMap(WMenu *menu)
1185 if (!menu->flags.realized) {
1186 menu->flags.realized=1;
1187 wMenuRealize(menu);
1189 if (menu->flags.app_menu && menu->parent==NULL) {
1190 menu->frame_x = menu->frame->screen_ptr->app_menu_x;
1191 menu->frame_y = menu->frame->screen_ptr->app_menu_y;
1192 XMoveWindow(dpy, menu->frame->core->window, menu->frame_x, menu->frame_y);
1194 XMapWindow(dpy, menu->frame->core->window);
1195 wRaiseFrame(menu->frame->core);
1196 menu->flags.mapped = 1;
1200 void
1201 wMenuUnmap(WMenu *menu)
1203 int i;
1205 XUnmapWindow(dpy, menu->frame->core->window);
1206 if (menu->flags.titled && menu->flags.buttoned) {
1207 wFrameWindowHideButton(menu->frame, WFF_RIGHT_BUTTON);
1209 menu->flags.buttoned = 0;
1210 menu->flags.mapped = 0;
1211 menu->flags.open_to_left = 0;
1213 for (i=0; i<menu->cascade_no; i++) {
1214 if (menu->cascades[i]!=NULL
1215 && menu->cascades[i]->flags.mapped
1216 && !menu->cascades[i]->flags.buttoned) {
1218 wMenuUnmap(menu->cascades[i]);
1221 menu->selected_entry = -1;
1226 void
1227 wMenuPaint(WMenu *menu)
1229 int i;
1231 if (!menu->flags.mapped) {
1232 return;
1235 /* paint entries */
1236 for (i=0; i<menu->entry_no; i++) {
1237 paintEntry(menu, i, i==menu->selected_entry);
1242 void
1243 wMenuSetEnabled(WMenu *menu, int index, int enable)
1245 if (index>=menu->entry_no) return;
1246 menu->entries[index]->flags.enabled=enable;
1247 paintEntry(menu, index, index==menu->selected_entry);
1248 paintEntry(menu->brother, index, index==menu->selected_entry);
1252 /* ====================================================================== */
1255 static void
1256 editEntry(WMenu *menu, WMenuEntry *entry)
1258 WTextInput *text;
1259 XEvent event;
1260 WObjDescriptor *desc;
1261 char *t;
1262 int done = 0;
1263 Window old_focus;
1264 int old_revert;
1266 menu->flags.editing = 1;
1268 text = wTextCreate(menu->menu, 1, menu->entry_height * entry->order,
1269 menu->menu->width - 2, menu->entry_height - 1);
1271 wTextPutText(text, entry->text);
1272 XGetInputFocus(dpy, &old_focus, &old_revert);
1273 XSetInputFocus(dpy, text->core->window, RevertToNone, CurrentTime);
1275 if (XGrabKeyboard(dpy, text->core->window, True, GrabModeAsync,
1276 GrabModeAsync, CurrentTime)!=GrabSuccess) {
1277 wwarning(_("could not grab keyboard"));
1278 wTextDestroy(text);
1280 wSetFocusTo(menu->frame->screen_ptr,
1281 menu->frame->screen_ptr->focused_window);
1282 return;
1286 while (!done && !text->done) {
1287 XSync(dpy, 0);
1288 XAllowEvents(dpy, AsyncKeyboard|AsyncPointer, CurrentTime);
1289 XSync(dpy, 0);
1290 WMNextEvent(dpy, &event);
1292 if (XFindContext(dpy, event.xany.window, wWinContext,
1293 (XPointer *)&desc)==XCNOENT)
1294 desc = NULL;
1296 if ((desc != NULL) && (desc->handle_anything != NULL)) {
1298 (*desc->handle_anything)(desc, &event);
1300 } else {
1301 switch (event.type) {
1302 case ButtonPress:
1303 XAllowEvents(dpy, ReplayPointer, CurrentTime);
1304 done = 1;
1306 default:
1307 WMHandleEvent(&event);
1308 break;
1313 XSetInputFocus(dpy, old_focus, old_revert, CurrentTime);
1315 wSetFocusTo(menu->frame->screen_ptr,
1316 menu->frame->screen_ptr->focused_window);
1319 t = wTextGetText(text);
1320 /* if !t, the user has canceled editing */
1321 if (t) {
1322 if (entry->text)
1323 wfree(entry->text);
1324 entry->text = wstrdup(t);
1326 menu->flags.realized = 0;
1328 wTextDestroy(text);
1330 XUngrabKeyboard(dpy, CurrentTime);
1332 if (t && menu->on_edit)
1333 (*menu->on_edit)(menu, entry);
1335 menu->flags.editing = 0;
1337 if (!menu->flags.realized)
1338 wMenuRealize(menu);
1342 static void
1343 selectEntry(WMenu *menu, int entry_no)
1345 WMenuEntry *entry;
1346 WMenu *submenu;
1347 int old_entry;
1349 if (menu->entries==NULL)
1350 return;
1352 if (entry_no >= menu->entry_no)
1353 return;
1355 old_entry = menu->selected_entry;
1356 menu->selected_entry = entry_no;
1358 if (old_entry!=entry_no) {
1360 /* unselect previous entry */
1361 if (old_entry>=0) {
1362 paintEntry(menu, old_entry, False);
1363 entry = menu->entries[old_entry];
1365 /* unmap cascade */
1366 if (entry->cascade>=0 && menu->cascades) {
1367 if (!menu->cascades[entry->cascade]->flags.buttoned) {
1368 wMenuUnmap(menu->cascades[entry->cascade]);
1373 if (entry_no<0) {
1374 menu->selected_entry = -1;
1375 return;
1377 entry = menu->entries[entry_no];
1379 if (entry->cascade>=0 && menu->cascades && entry->flags.enabled) {
1380 /* Callback for when the submenu is opened.
1382 submenu = menu->cascades[entry->cascade];
1383 if (submenu && submenu->flags.brother)
1384 submenu = submenu->brother;
1386 if (entry->callback) {
1387 /* Only call the callback if the submenu is not yet mapped.
1389 if (menu->flags.brother) {
1390 if (!submenu || !submenu->flags.mapped)
1391 (*entry->callback)(menu->brother, entry);
1392 } else {
1393 if (!submenu || !submenu->flags.buttoned)
1394 (*entry->callback)(menu, entry);
1398 /* the submenu menu might have changed */
1399 submenu = menu->cascades[entry->cascade];
1401 /* map cascade */
1402 if (!submenu->flags.mapped) {
1403 int x, y;
1405 if (!submenu->flags.realized)
1406 wMenuRealize(submenu);
1407 if (wPreferences.wrap_menus) {
1408 if (menu->flags.open_to_left)
1409 submenu->flags.open_to_left = 1;
1411 if (submenu->flags.open_to_left) {
1412 x = menu->frame_x - MENUW(submenu);
1413 if (x<0) {
1414 x = 0;
1415 submenu->flags.open_to_left = 0;
1417 } else {
1418 x = menu->frame_x + MENUW(menu);
1420 if (x + MENUW(submenu)
1421 >= menu->frame->screen_ptr->scr_width) {
1423 x = menu->frame_x - MENUW(submenu);
1424 submenu->flags.open_to_left = 1;
1427 } else {
1428 x = menu->frame_x + MENUW(menu);
1431 if (wPreferences.align_menus) {
1432 y = menu->frame_y;
1433 } else {
1434 y = menu->frame_y + menu->entry_height*entry_no;
1435 if (menu->flags.titled)
1436 y += menu->frame->top_width;
1437 if (menu->cascades[entry->cascade]->flags.titled)
1438 y -= menu->cascades[entry->cascade]->frame->top_width;
1441 wMenuMapAt(menu->cascades[entry->cascade], x, y, False);
1442 menu->cascades[entry->cascade]->parent = menu;
1443 } else {
1444 return;
1447 paintEntry(menu, entry_no, True);
1452 static WMenu*
1453 findMenu(WScreen *scr, int *x_ret, int *y_ret)
1455 WMenu *menu;
1456 WObjDescriptor *desc;
1457 Window root_ret, win, junk_win;
1458 int x, y, wx, wy;
1459 unsigned int mask;
1461 XQueryPointer(dpy, scr->root_win, &root_ret, &win, &x, &y, &wx, &wy,
1462 &mask);
1464 if (win==None) return NULL;
1466 if (XFindContext(dpy, win, wWinContext, (XPointer *)&desc)==XCNOENT)
1467 return NULL;
1469 if (desc->parent_type == WCLASS_MENU) {
1470 menu = (WMenu*)desc->parent;
1471 XTranslateCoordinates(dpy, root_ret, menu->menu->window, wx, wy,
1472 x_ret, y_ret, &junk_win);
1473 return menu;
1475 return NULL;
1481 static void
1482 closeCascade(WMenu *menu)
1484 WMenu *parent=menu->parent;
1486 if (menu->flags.brother
1487 || (!menu->flags.buttoned
1488 && (!menu->flags.app_menu||menu->parent!=NULL))) {
1490 selectEntry(menu, -1);
1491 XSync(dpy, 0);
1492 #if (MENU_BLINK_DELAY > 2)
1493 wusleep(MENU_BLINK_DELAY/2);
1494 #endif
1495 wMenuUnmap(menu);
1496 while (parent!=NULL
1497 && (parent->parent!=NULL || !parent->flags.app_menu
1498 || parent->flags.brother)
1499 && !parent->flags.buttoned) {
1500 selectEntry(parent, -1);
1501 wMenuUnmap(parent);
1502 parent = parent->parent;
1504 if (parent)
1505 selectEntry(parent, -1);
1510 static void
1511 closeBrotherCascadesOf(WMenu *menu)
1513 WMenu *tmp;
1514 int i;
1516 for (i=0; i<menu->cascade_no; i++) {
1517 if (menu->cascades[i]->flags.brother) {
1518 tmp = menu->cascades[i];
1519 } else {
1520 tmp = menu->cascades[i]->brother;
1522 if (tmp->flags.mapped) {
1523 selectEntry(tmp->parent, -1);
1524 closeBrotherCascadesOf(tmp);
1525 break;
1531 #define getEntryAt(menu, x, y) ((y)<0 ? -1 : (y)/(menu->entry_height))
1534 static WMenu*
1535 parentMenu(WMenu *menu)
1537 WMenu *parent;
1538 WMenuEntry *entry;
1540 if (menu->flags.buttoned)
1541 return menu;
1543 while (menu->parent && menu->parent->flags.mapped) {
1544 parent = menu->parent;
1545 if (parent->selected_entry < 0)
1546 break;
1547 entry = parent->entries[parent->selected_entry];
1548 if (!entry->flags.enabled || entry->cascade<0 || !parent->cascades ||
1549 parent->cascades[entry->cascade] != menu)
1550 break;
1551 menu = parent;
1552 if (menu->flags.buttoned)
1553 break;
1556 return menu;
1562 * Will raise the passed menu, if submenu = 0
1563 * If submenu > 0 will also raise all mapped submenus
1564 * until the first buttoned one
1565 * If submenu < 0 will also raise all mapped parent menus
1566 * until the first buttoned one
1569 static void
1570 raiseMenus(WMenu *menu, int submenus)
1572 WMenu *submenu;
1573 int i;
1575 if(!menu) return;
1577 wRaiseFrame(menu->frame->core);
1579 if (submenus>0 && menu->selected_entry>=0) {
1580 i = menu->entries[menu->selected_entry]->cascade;
1581 if (i>=0 && menu->cascades) {
1582 submenu = menu->cascades[i];
1583 if (submenu->flags.mapped && !submenu->flags.buttoned)
1584 raiseMenus(submenu, submenus);
1587 if (submenus<0 && !menu->flags.buttoned &&
1588 menu->parent && menu->parent->flags.mapped)
1589 raiseMenus(menu->parent, submenus);
1593 WMenu*
1594 wMenuUnderPointer(WScreen *screen)
1596 WObjDescriptor *desc;
1597 Window root_ret, win;
1598 int dummy;
1599 unsigned int mask;
1601 XQueryPointer(dpy, screen->root_win, &root_ret, &win, &dummy, &dummy,
1602 &dummy, &dummy, &mask);
1604 if (win==None) return NULL;
1606 if (XFindContext(dpy, win, wWinContext, (XPointer *)&desc)==XCNOENT)
1607 return NULL;
1609 if (desc->parent_type == WCLASS_MENU)
1610 return (WMenu *)desc->parent;
1611 return NULL;
1617 static void
1618 getPointerPosition(WScreen *scr, int *x, int *y)
1620 Window root_ret, win;
1621 int wx, wy;
1622 unsigned int mask;
1624 XQueryPointer(dpy, scr->root_win, &root_ret, &win, x, y, &wx, &wy, &mask);
1628 static void
1629 getScrollAmount(WMenu *menu, int *hamount, int *vamount)
1631 WScreen *scr = menu->menu->screen_ptr;
1632 int menuX1 = menu->frame_x;
1633 int menuY1 = menu->frame_y;
1634 int menuX2 = menu->frame_x + MENUW(menu);
1635 int menuY2 = menu->frame_y + MENUH(menu);
1636 int xroot, yroot;
1637 WMRect rect = wGetRectForHead(scr, wGetHeadForPointerLocation(scr));
1640 *hamount = 0;
1641 *vamount = 0;
1643 getPointerPosition(scr, &xroot, &yroot);
1645 if (xroot <= (rect.pos.x + 1) && menuX1 < rect.pos.x) {
1646 /* scroll to the right */
1647 *hamount = WMIN(MENU_SCROLL_STEP, abs(menuX1));
1649 } else if (xroot >= (rect.pos.x + rect.size.width - 2) &&
1650 menuX2 > (rect.pos.x + rect.size.width - 1)) {
1651 /* scroll to the left */
1652 *hamount = WMIN(MENU_SCROLL_STEP, abs(menuX2-rect.pos.x-rect.size.width-1));
1654 if (*hamount==0)
1655 *hamount = 1;
1657 *hamount = -*hamount;
1660 if (yroot <= (rect.pos.y + 1) && menuY1 < rect.pos.y) {
1661 /* scroll down */
1662 *vamount = WMIN(MENU_SCROLL_STEP, abs(menuY1));
1664 } else if (yroot >= (rect.pos.y + rect.size.height - 2) &&
1665 menuY2 > (rect.pos.y + rect.size.height - 1)) {
1666 /* scroll up */
1667 *vamount = WMIN(MENU_SCROLL_STEP, abs(menuY2-rect.pos.y-rect.size.height-2));
1669 *vamount = -*vamount;
1674 static void
1675 dragScrollMenuCallback(void *data)
1677 WMenu *menu = (WMenu*)data;
1678 WScreen *scr = menu->menu->screen_ptr;
1679 WMenu *parent = parentMenu(menu);
1680 int hamount, vamount;
1681 int x, y;
1682 int newSelectedEntry;
1684 getScrollAmount(menu, &hamount, &vamount);
1687 if (hamount != 0 || vamount != 0) {
1688 wMenuMove(parent, parent->frame_x + hamount,
1689 parent->frame_y + vamount, True);
1690 if (findMenu(scr, &x, &y)) {
1691 newSelectedEntry = getEntryAt(menu, x, y);
1692 selectEntry(menu, newSelectedEntry);
1693 } else {
1694 /* Pointer fell outside of menu. If the selected entry is
1695 * not a submenu, unselect it */
1696 if (menu->selected_entry >= 0
1697 && menu->entries[menu->selected_entry]->cascade<0)
1698 selectEntry(menu, -1);
1699 newSelectedEntry = 0;
1702 /* paranoid check */
1703 if (newSelectedEntry >= 0) {
1704 /* keep scrolling */
1705 menu->timer = WMAddTimerHandler(MENU_SCROLL_DELAY,
1706 dragScrollMenuCallback, menu);
1707 } else {
1708 menu->timer = NULL;
1710 } else {
1711 /* don't need to scroll anymore */
1712 menu->timer = NULL;
1713 if (findMenu(scr, &x, &y)) {
1714 newSelectedEntry = getEntryAt(menu, x, y);
1715 selectEntry(menu, newSelectedEntry);
1721 static void
1722 scrollMenuCallback(void *data)
1724 WMenu *menu = (WMenu*)data;
1725 WMenu *parent = parentMenu(menu);
1726 int hamount = 0; /* amount to scroll */
1727 int vamount = 0;
1729 #ifdef VIRTUAL_DESKTOP
1730 /* don't scroll if it is in vdesk mode */
1731 if (!wPreferences.vdesk_enable)
1732 #endif
1733 getScrollAmount(menu, &hamount, &vamount);
1735 if (hamount != 0 || vamount != 0) {
1736 wMenuMove(parent, parent->frame_x + hamount,
1737 parent->frame_y + vamount, True);
1739 /* keep scrolling */
1740 menu->timer = WMAddTimerHandler(MENU_SCROLL_DELAY,
1741 scrollMenuCallback, menu);
1742 } else {
1743 /* don't need to scroll anymore */
1744 menu->timer = NULL;
1750 #define MENU_SCROLL_BORDER 5
1752 static int
1753 isPointNearBoder(WMenu *menu, int x, int y)
1755 int menuX1 = menu->frame_x;
1756 int menuY1 = menu->frame_y;
1757 int menuX2 = menu->frame_x + MENUW(menu);
1758 int menuY2 = menu->frame_y + MENUH(menu);
1759 int flag = 0;
1760 int head = wGetHeadForPoint(menu->frame->screen_ptr, wmkpoint(x, y));
1761 WMRect rect = wGetRectForHead(menu->frame->screen_ptr, head);
1763 /* XXX: handle screen joins properly !! */
1765 if (x >= menuX1 && x <= menuX2 &&
1766 (y < rect.pos.y + MENU_SCROLL_BORDER ||
1767 y >= rect.pos.y + rect.size.height - MENU_SCROLL_BORDER))
1768 flag = 1;
1769 else if (y >= menuY1 && y <= menuY2 &&
1770 (x < rect.pos.x + MENU_SCROLL_BORDER ||
1771 x >= rect.pos.x + rect.size.width - MENU_SCROLL_BORDER))
1772 flag = 1;
1774 return flag;
1778 typedef struct _delay {
1779 WMenu *menu;
1780 int ox, oy;
1781 } _delay;
1784 static void
1785 leaving(_delay *dl)
1787 wMenuMove(dl->menu, dl->ox, dl->oy, True);
1788 dl->menu->jump_back = NULL;
1789 dl->menu->menu->screen_ptr->flags.jump_back_pending = 0;
1790 wfree(dl);
1794 void
1795 wMenuScroll(WMenu *menu, XEvent *event)
1797 WMenu *smenu;
1798 WMenu *omenu = parentMenu(menu);
1799 WScreen *scr = menu->frame->screen_ptr;
1800 int done = 0;
1801 int jump_back = 0;
1802 int old_frame_x = omenu->frame_x;
1803 int old_frame_y = omenu->frame_y;
1804 XEvent ev;
1806 if (omenu->jump_back)
1807 WMDeleteTimerWithClientData(omenu->jump_back);
1810 if ((/*omenu->flags.buttoned &&*/ !wPreferences.wrap_menus)
1811 || omenu->flags.app_menu) {
1812 jump_back = 1;
1815 if (!wPreferences.wrap_menus)
1816 raiseMenus(omenu, True);
1817 else
1818 raiseMenus(menu, False);
1820 if (!menu->timer)
1821 scrollMenuCallback(menu);
1823 while(!done) {
1824 int x, y, on_border, on_x_edge, on_y_edge, on_title;
1825 WMRect rect;
1827 WMNextEvent(dpy, &ev);
1828 switch (ev.type) {
1829 case EnterNotify:
1830 WMHandleEvent(&ev);
1831 case MotionNotify:
1832 x = (ev.type==MotionNotify) ? ev.xmotion.x_root : ev.xcrossing.x_root;
1833 y = (ev.type==MotionNotify) ? ev.xmotion.y_root : ev.xcrossing.y_root;
1835 /* on_border is != 0 if the pointer is between the menu
1836 * and the screen border and is close enough to the border */
1837 on_border = isPointNearBoder(menu, x, y);
1839 smenu = wMenuUnderPointer(scr);
1841 if ((smenu==NULL && !on_border) || (smenu && parentMenu(smenu)!=omenu)) {
1842 done = 1;
1843 break;
1846 rect = wGetRectForHead(scr, wGetHeadForPoint(scr, wmkpoint(x, y)));
1847 on_x_edge = x <= rect.pos.x + 1 || x >= rect.pos.x + rect.size.width - 2;
1848 on_y_edge = y <= rect.pos.y + 1 || y >= rect.pos.y + rect.size.height - 2;
1849 on_border = on_x_edge || on_y_edge;
1851 if (!on_border && !jump_back) {
1852 done = 1;
1853 break;
1856 if (menu->timer && (smenu!=menu || (!on_y_edge && !on_x_edge))) {
1857 WMDeleteTimerHandler(menu->timer);
1858 menu->timer = NULL;
1861 if (smenu != NULL)
1862 menu = smenu;
1864 if (!menu->timer)
1865 scrollMenuCallback(menu);
1866 break;
1867 case ButtonPress:
1868 /* True if we push on title, or drag the omenu to other position */
1869 on_title = ev.xbutton.x_root >= omenu->frame_x &&
1870 ev.xbutton.x_root <= omenu->frame_x + MENUW(omenu) &&
1871 ev.xbutton.y_root >= omenu->frame_y &&
1872 ev.xbutton.y_root <= omenu->frame_y + omenu->frame->top_width;
1873 WMHandleEvent(&ev);
1874 smenu = wMenuUnderPointer(scr);
1875 if (smenu == NULL || (smenu && smenu->flags.buttoned && smenu != omenu))
1876 done = 1;
1877 else if (smenu==omenu && on_title) {
1878 jump_back = 0;
1879 done = 1;
1881 break;
1882 case KeyPress:
1883 done = 1;
1884 default:
1885 WMHandleEvent(&ev);
1886 break;
1890 if (menu->timer) {
1891 WMDeleteTimerHandler(menu->timer);
1892 menu->timer = NULL;
1895 if (jump_back) {
1896 _delay *delayer;
1897 if (!omenu->jump_back) {
1898 delayer = wmalloc(sizeof(_delay));
1899 delayer->menu=omenu;
1900 delayer->ox=old_frame_x;
1901 delayer->oy=old_frame_y;
1902 omenu->jump_back = delayer;
1903 scr->flags.jump_back_pending = 1;
1905 else delayer = omenu->jump_back;
1906 WMAddTimerHandler(MENU_JUMP_BACK_DELAY,(WMCallback*)leaving, delayer);
1912 static void
1913 menuExpose(WObjDescriptor *desc, XEvent *event)
1915 wMenuPaint(desc->parent);
1918 typedef struct {
1919 int *delayed_select;
1920 WMenu *menu;
1921 WMHandlerID magic;
1922 } delay_data;
1925 static void
1926 delaySelection(void *data)
1928 delay_data *d = (delay_data*)data;
1929 int x, y, entry_no;
1930 WMenu *menu;
1932 d->magic = NULL;
1934 menu = findMenu(d->menu->menu->screen_ptr, &x, &y);
1935 if (menu && (d->menu == menu || d->delayed_select)) {
1936 entry_no = getEntryAt(menu, x, y);
1937 selectEntry(menu, entry_no);
1939 if (d->delayed_select)
1940 *(d->delayed_select) = 0;
1944 static void
1945 menuMouseDown(WObjDescriptor *desc, XEvent *event)
1947 XButtonEvent *bev = &event->xbutton;
1948 WMenu *menu = desc->parent;
1949 WMenu *smenu;
1950 WScreen *scr=menu->frame->screen_ptr;
1951 WMenuEntry *entry=NULL;
1952 XEvent ev;
1953 int close_on_exit=0;
1954 int done=0;
1955 int delayed_select = 0;
1956 int entry_no;
1957 int x, y;
1958 int prevx, prevy;
1959 int old_frame_x = 0;
1960 int old_frame_y = 0;
1961 delay_data d_data = {NULL, NULL, NULL};
1963 /* Doesn't seem to be needed anymore (if delayed selection handler is
1964 * added only if not present). there seem to be no other side effects
1965 * from removing this and it is also possible that it was only added
1966 * to avoid problems with adding the delayed selection timer handler
1967 * multiple times
1969 /*if (menu->flags.inside_handler) {
1970 return;
1972 menu->flags.inside_handler = 1;
1974 if (!wPreferences.wrap_menus) {
1975 smenu = parentMenu(menu);
1976 old_frame_x = smenu->frame_x;
1977 old_frame_y = smenu->frame_y;
1978 } else if (event->xbutton.window == menu->frame->core->window) {
1979 /* This is true if the menu was launched with right click on root window */
1980 if (!d_data.magic) {
1981 delayed_select = 1;
1982 d_data.delayed_select = &delayed_select;
1983 d_data.menu = menu;
1984 d_data.magic = WMAddTimerHandler(wPreferences.dblclick_time,
1985 delaySelection, &d_data);
1989 wRaiseFrame(menu->frame->core);
1991 close_on_exit = (bev->send_event || menu->flags.brother);
1993 smenu = findMenu(scr, &x, &y);
1994 if (!smenu) {
1995 x = -1;
1996 y = -1;
1997 } else {
1998 menu = smenu;
2001 if (menu->flags.editing) {
2002 goto byebye;
2004 entry_no = getEntryAt(menu, x, y);
2005 if (entry_no>=0) {
2006 entry = menu->entries[entry_no];
2008 if (!close_on_exit && (bev->state & ControlMask) && smenu
2009 && entry->flags.editable) {
2010 editEntry(smenu, entry);
2011 goto byebye;
2012 } else if (bev->state & ControlMask) {
2013 goto byebye;
2016 if (entry->flags.enabled && entry->cascade>=0 && menu->cascades) {
2017 WMenu *submenu = menu->cascades[entry->cascade];
2018 /* map cascade */
2019 if (submenu->flags.mapped && !submenu->flags.buttoned &&
2020 menu->selected_entry!=entry_no) {
2021 wMenuUnmap(submenu);
2023 if (!submenu->flags.mapped && !delayed_select) {
2024 selectEntry(menu, entry_no);
2025 } else if (!submenu->flags.buttoned) {
2026 selectEntry(menu, -1);
2029 } else if (!delayed_select) {
2030 selectEntry(menu, entry_no);
2033 if (!wPreferences.wrap_menus && !wPreferences.scrollable_menus) {
2034 if (!menu->timer)
2035 dragScrollMenuCallback(menu);
2039 #ifdef VIRTUAL_DESKTOP
2040 if (wPreferences.vdesk_enable) {
2041 wWorkspaceLowerEdge(scr);
2043 #endif
2045 prevx = bev->x_root;
2046 prevy = bev->y_root;
2047 while (!done) {
2048 int x, y;
2050 XAllowEvents(dpy, AsyncPointer|SyncPointer, CurrentTime);
2052 WMMaskEvent(dpy, ExposureMask|ButtonMotionMask|ButtonReleaseMask
2053 |ButtonPressMask, &ev);
2054 switch (ev.type) {
2055 case MotionNotify:
2056 smenu = findMenu(scr, &x, &y);
2058 if (smenu == NULL) {
2059 /* moved mouse out of menu */
2061 if (!delayed_select && d_data.magic) {
2062 WMDeleteTimerHandler(d_data.magic);
2063 d_data.magic = NULL;
2065 if (menu==NULL
2066 || (menu->selected_entry>=0
2067 && menu->entries[menu->selected_entry]->cascade>=0)) {
2068 prevx = ev.xmotion.x_root;
2069 prevy = ev.xmotion.y_root;
2071 break;
2073 selectEntry(menu, -1);
2074 menu = smenu;
2075 prevx = ev.xmotion.x_root;
2076 prevy = ev.xmotion.y_root;
2077 break;
2078 } else if (menu && menu!=smenu
2079 && (menu->selected_entry<0
2080 || menu->entries[menu->selected_entry]->cascade<0)) {
2081 selectEntry(menu, -1);
2083 if (!delayed_select && d_data.magic) {
2084 WMDeleteTimerHandler(d_data.magic);
2085 d_data.magic = NULL;
2087 } else {
2089 /* hysteresis for item selection */
2091 /* check if the motion was to the side, indicating that
2092 * the user may want to cross to a submenu */
2093 if (!delayed_select && menu) {
2094 int dx;
2095 Bool moved_to_submenu;/* moved to direction of submenu */
2097 dx = abs(prevx - ev.xmotion.x_root);
2099 moved_to_submenu = False;
2100 if (dx > 0 /* if moved enough to the side */
2101 /* maybe a open submenu */
2102 && menu->selected_entry>=0
2103 /* moving to the right direction */
2104 && (wPreferences.align_menus
2105 || ev.xmotion.y_root >= prevy)) {
2106 int index;
2108 index = menu->entries[menu->selected_entry]->cascade;
2109 if (index>=0) {
2110 if (menu->cascades[index]->frame_x>menu->frame_x) {
2111 if (prevx < ev.xmotion.x_root)
2112 moved_to_submenu = True;
2113 } else {
2114 if (prevx > ev.xmotion.x_root)
2115 moved_to_submenu = True;
2121 if (menu != smenu) {
2122 if (d_data.magic) {
2123 WMDeleteTimerHandler(d_data.magic);
2124 d_data.magic = NULL;
2126 } else if (moved_to_submenu) {
2127 /* while we are moving, postpone the selection */
2128 if (d_data.magic) {
2129 WMDeleteTimerHandler(d_data.magic);
2131 d_data.delayed_select = NULL;
2132 d_data.menu = menu;
2133 d_data.magic = WMAddTimerHandler(MENU_SELECT_DELAY,
2134 delaySelection,
2135 &d_data);
2136 prevx = ev.xmotion.x_root;
2137 prevy = ev.xmotion.y_root;
2138 break;
2139 } else {
2140 if (d_data.magic) {
2141 WMDeleteTimerHandler(d_data.magic);
2142 d_data.magic = NULL;
2147 prevx = ev.xmotion.x_root;
2148 prevy = ev.xmotion.y_root;
2149 if (menu!=smenu) {
2150 /* pointer crossed menus */
2151 if (menu && menu->timer) {
2152 WMDeleteTimerHandler(menu->timer);
2153 menu->timer = NULL;
2155 if (smenu)
2156 dragScrollMenuCallback(smenu);
2158 menu = smenu;
2159 if (!menu->timer)
2160 dragScrollMenuCallback(menu);
2162 if (!delayed_select) {
2163 entry_no = getEntryAt(menu, x, y);
2164 if (entry_no>=0) {
2165 entry = menu->entries[entry_no];
2166 if (entry->flags.enabled && entry->cascade>=0 &&
2167 menu->cascades) {
2168 WMenu *submenu = menu->cascades[entry->cascade];
2169 if (submenu->flags.mapped && !submenu->flags.buttoned
2170 && menu->selected_entry!=entry_no) {
2171 wMenuUnmap(submenu);
2175 selectEntry(menu, entry_no);
2177 break;
2179 case ButtonPress:
2180 break;
2182 case ButtonRelease:
2183 if (ev.xbutton.button == event->xbutton.button)
2184 done=1;
2185 break;
2187 case Expose:
2188 WMHandleEvent(&ev);
2189 #ifdef VIRTUAL_DESKTOP
2190 /* since expose will raise edge up.. I need another ugly hack here */
2191 if (wPreferences.vdesk_enable) {
2192 wWorkspaceLowerEdge(scr);
2194 #endif
2195 break;
2199 if (menu && menu->timer) {
2200 WMDeleteTimerHandler(menu->timer);
2201 menu->timer = NULL;
2203 if (d_data.magic!=NULL) {
2204 WMDeleteTimerHandler(d_data.magic);
2205 d_data.magic = NULL;
2208 if (menu && menu->selected_entry>=0) {
2209 entry = menu->entries[menu->selected_entry];
2210 if (entry->callback!=NULL && entry->flags.enabled
2211 && entry->cascade < 0) {
2212 /* blink and erase menu selection */
2213 #if (MENU_BLINK_DELAY > 0)
2214 int sel = menu->selected_entry;
2215 int i;
2217 for (i=0; i<MENU_BLINK_COUNT; i++) {
2218 paintEntry(menu, sel, False);
2219 XSync(dpy, 0);
2220 wusleep(MENU_BLINK_DELAY);
2221 paintEntry(menu, sel, True);
2222 XSync(dpy, 0);
2223 wusleep(MENU_BLINK_DELAY);
2225 #endif
2226 /* unmap the menu, it's parents and call the callback */
2227 if (!menu->flags.buttoned &&
2228 (!menu->flags.app_menu||menu->parent!=NULL)) {
2229 closeCascade(menu);
2230 } else {
2231 selectEntry(menu, -1);
2233 (*entry->callback)(menu, entry);
2235 /* If the user double clicks an entry, the entry will
2236 * be executed twice, which is not good for things like
2237 * the root menu. So, ignore any clicks that were generated
2238 * while the entry was being executed */
2239 while (XCheckTypedWindowEvent(dpy, menu->menu->window,
2240 ButtonPress, &ev));
2241 } else if (entry->callback!=NULL && entry->cascade<0) {
2242 selectEntry(menu, -1);
2243 } else {
2244 if (entry->cascade>=0 && menu->cascades
2245 && menu->cascades[entry->cascade]->flags.brother) {
2246 selectEntry(menu, -1);
2251 if (((WMenu*)desc->parent)->flags.brother || close_on_exit || !smenu)
2252 closeCascade(desc->parent);
2254 /* close the cascade windows that should not remain opened */
2255 closeBrotherCascadesOf(desc->parent);
2257 if (!wPreferences.wrap_menus)
2258 wMenuMove(parentMenu(desc->parent), old_frame_x, old_frame_y, True);
2260 byebye:
2261 /* Just to be sure in case we skip the 2 above because of a goto byebye */
2262 if (menu && menu->timer) {
2263 WMDeleteTimerHandler(menu->timer);
2264 menu->timer = NULL;
2266 if (d_data.magic!=NULL) {
2267 WMDeleteTimerHandler(d_data.magic);
2268 d_data.magic = NULL;
2271 ((WMenu*)desc->parent)->flags.inside_handler = 0;
2272 #ifdef VIRTUAL_DESKTOP
2273 wWorkspaceRaiseEdge(scr);
2274 #endif
2278 void
2279 wMenuMove(WMenu *menu, int x, int y, int submenus)
2281 WMenu *submenu;
2282 int i;
2284 if (!menu) return;
2286 menu->frame_x = x;
2287 menu->frame_y = y;
2288 XMoveWindow(dpy, menu->frame->core->window, x, y);
2290 if (submenus>0 && menu->selected_entry>=0) {
2291 i = menu->entries[menu->selected_entry]->cascade;
2293 if (i>=0 && menu->cascades) {
2294 submenu = menu->cascades[i];
2295 if (submenu->flags.mapped && !submenu->flags.buttoned) {
2296 if (wPreferences.align_menus) {
2297 wMenuMove(submenu, x + MENUW(menu), y, submenus);
2298 } else {
2299 wMenuMove(submenu, x+ MENUW(menu),
2300 y + submenu->entry_height*menu->selected_entry,
2301 submenus);
2306 if (submenus<0 && menu->parent!=NULL && menu->parent->flags.mapped &&
2307 !menu->parent->flags.buttoned) {
2308 if (wPreferences.align_menus) {
2309 wMenuMove(menu->parent, x - MENUW(menu->parent), y, submenus);
2310 } else {
2311 wMenuMove(menu->parent, x - MENUW(menu->parent), menu->frame_y
2312 - menu->parent->entry_height*menu->parent->selected_entry,
2313 submenus);
2319 static void
2320 changeMenuLevels(WMenu *menu, int lower)
2322 int i;
2324 if (!lower) {
2325 ChangeStackingLevel(menu->frame->core, (!menu->parent ? WMMainMenuLevel
2326 : WMSubmenuLevel));
2327 wRaiseFrame(menu->frame->core);
2328 menu->flags.lowered = 0;
2329 } else {
2330 ChangeStackingLevel(menu->frame->core, WMNormalLevel);
2331 wLowerFrame(menu->frame->core);
2332 menu->flags.lowered = 1;
2334 for (i=0; i<menu->cascade_no; i++) {
2335 if (menu->cascades[i]
2336 && !menu->cascades[i]->flags.buttoned
2337 && menu->cascades[i]->flags.lowered!=lower) {
2338 changeMenuLevels(menu->cascades[i], lower);
2345 static void
2346 menuTitleDoubleClick(WCoreWindow *sender, void *data, XEvent *event)
2348 WMenu *menu = data;
2349 int lower;
2351 if (event->xbutton.state & MOD_MASK) {
2352 if (menu->flags.lowered) {
2353 lower = 0;
2354 } else {
2355 lower = 1;
2357 changeMenuLevels(menu, lower);
2362 static void
2363 menuTitleMouseDown(WCoreWindow *sender, void *data, XEvent *event)
2365 WMenu *menu = data;
2366 WMenu *tmp;
2367 XEvent ev;
2368 int x=menu->frame_x, y=menu->frame_y;
2369 int dx=event->xbutton.x_root, dy=event->xbutton.y_root;
2370 int i, lower;
2371 Bool started;
2373 /* can't touch the menu copy */
2374 if (menu->flags.brother)
2375 return;
2377 if (event->xbutton.button != Button1 && event->xbutton.button != Button2)
2378 return;
2380 if (event->xbutton.state & MOD_MASK) {
2381 wLowerFrame(menu->frame->core);
2382 lower = 1;
2383 } else {
2384 wRaiseFrame(menu->frame->core);
2385 lower = 0;
2387 tmp = menu;
2389 /* lower/raise all submenus */
2390 while (1) {
2391 if (tmp->selected_entry>=0 && tmp->cascades
2392 && tmp->entries[tmp->selected_entry]->cascade>=0) {
2393 tmp = tmp->cascades[tmp->entries[tmp->selected_entry]->cascade];
2394 if (!tmp || !tmp->flags.mapped)
2395 break;
2396 if (lower)
2397 wLowerFrame(tmp->frame->core);
2398 else
2399 wRaiseFrame(tmp->frame->core);
2400 } else {
2401 break;
2405 /* tear off the menu if it's a root menu or a cascade
2406 application menu */
2407 if (!menu->flags.buttoned && !menu->flags.brother
2408 && (!menu->flags.app_menu||menu->parent!=NULL)) {
2409 menu->flags.buttoned=1;
2410 wFrameWindowShowButton(menu->frame, WFF_RIGHT_BUTTON);
2411 if (menu->parent) {
2412 /* turn off selected menu entry in parent menu */
2413 selectEntry(menu->parent, -1);
2415 /* make parent map the copy in place of the original */
2416 for (i=0; i<menu->parent->cascade_no; i++) {
2417 if (menu->parent->cascades[i] == menu) {
2418 menu->parent->cascades[i] = menu->brother;
2419 break;
2425 started = False;
2426 while(1) {
2427 WMMaskEvent(dpy, ButtonMotionMask|ButtonReleaseMask|ButtonPressMask
2428 |ExposureMask, &ev);
2429 switch (ev.type) {
2430 case MotionNotify:
2431 if (started) {
2432 x += ev.xmotion.x_root - dx;
2433 y += ev.xmotion.y_root - dy;
2434 dx = ev.xmotion.x_root;
2435 dy = ev.xmotion.y_root;
2436 wMenuMove(menu, x, y, True);
2437 } else {
2438 if (abs(ev.xmotion.x_root - dx) > MOVE_THRESHOLD
2439 || abs(ev.xmotion.y_root - dy) > MOVE_THRESHOLD) {
2440 started = True;
2441 XGrabPointer(dpy, menu->frame->titlebar->window, False,
2442 ButtonMotionMask|ButtonReleaseMask
2443 |ButtonPressMask,
2444 GrabModeAsync, GrabModeAsync, None,
2445 wCursor[WCUR_MOVE], CurrentTime);
2448 break;
2450 case ButtonPress:
2451 break;
2453 case ButtonRelease:
2454 if (ev.xbutton.button != event->xbutton.button)
2455 break;
2456 XUngrabPointer(dpy, CurrentTime);
2457 return;
2459 default:
2460 WMHandleEvent(&ev);
2461 break;
2467 *----------------------------------------------------------------------
2468 * menuCloseClick--
2469 * Handles mouse click on the close button of menus. The menu is
2470 * closed when the button is clicked.
2472 * Side effects:
2473 * The closed menu is reinserted at it's parent menus
2474 * cascade list.
2475 *----------------------------------------------------------------------
2477 static void
2478 menuCloseClick(WCoreWindow *sender, void *data, XEvent *event)
2480 WMenu *menu = (WMenu*)data;
2481 WMenu *parent = menu->parent;
2482 int i;
2484 if (parent) {
2485 for (i=0; i<parent->cascade_no; i++) {
2486 /* find the entry that points to the copy */
2487 if (parent->cascades[i] == menu->brother) {
2488 /* make it point to the original */
2489 parent->cascades[i] = menu;
2490 menu->parent = parent;
2491 break;
2495 wMenuUnmap(menu);
2499 static void
2500 saveMenuInfo(WMPropList *dict, WMenu *menu, WMPropList *key)
2502 WMPropList *value, *list;
2503 char buffer[256];
2505 snprintf(buffer, sizeof(buffer), "%i,%i", menu->frame_x, menu->frame_y);
2506 value = WMCreatePLString(buffer);
2507 list = WMCreatePLArray(value, NULL);
2508 if (menu->flags.lowered)
2509 WMAddToPLArray(list, WMCreatePLString("lowered"));
2510 WMPutInPLDictionary(dict, key, list);
2511 WMReleasePropList(value);
2512 WMReleasePropList(list);
2516 void
2517 wMenuSaveState(WScreen *scr)
2519 WMPropList *menus, *key;
2520 int save_menus = 0;
2522 menus = WMCreatePLDictionary(NULL, NULL);
2524 #ifndef LITE
2525 if (scr->switch_menu && scr->switch_menu->flags.buttoned) {
2526 key = WMCreatePLString("SwitchMenu");
2527 saveMenuInfo(menus, scr->switch_menu, key);
2528 WMReleasePropList(key);
2529 save_menus = 1;
2532 if (saveMenuRecurs(menus, scr, scr->root_menu))
2533 save_menus = 1;
2535 #endif /* !LITE */
2536 if (scr->workspace_menu && scr->workspace_menu->flags.buttoned) {
2537 key = WMCreatePLString("WorkspaceMenu");
2538 saveMenuInfo(menus, scr->workspace_menu, key);
2539 WMReleasePropList(key);
2540 save_menus = 1;
2543 if (save_menus) {
2544 key = WMCreatePLString("Menus");
2545 WMPutInPLDictionary(scr->session_state, key, menus);
2546 WMReleasePropList(key);
2548 WMReleasePropList(menus);
2552 #ifndef LITE
2554 static Bool
2555 getMenuPath(WMenu *menu, char *buffer, int bufSize)
2557 Bool ok = True;
2558 int len = 0;
2560 if (!menu->flags.titled || !menu->frame->title[0])
2561 return False;
2563 len = strlen(menu->frame->title);
2564 if (len >= bufSize)
2565 return False;
2567 if (menu->parent) {
2568 ok = getMenuPath(menu->parent, buffer, bufSize - len - 1);
2569 if (!ok)
2570 return False;
2573 strcat(buffer, "\\");
2574 strcat(buffer, menu->frame->title);
2576 return True;
2580 static Bool
2581 saveMenuRecurs(WMPropList *menus, WScreen *scr, WMenu *menu)
2583 WMPropList *key;
2584 int save_menus = 0, i;
2585 char buffer[512];
2586 Bool ok = True;
2589 if (menu->flags.brother)
2590 menu = menu->brother;
2592 if (menu->flags.buttoned && menu != scr->switch_menu) {
2594 buffer[0] = '\0';
2595 ok = getMenuPath(menu, buffer, 510);
2597 if (ok) {
2598 key = WMCreatePLString(buffer);
2599 saveMenuInfo(menus, menu, key);
2600 WMReleasePropList(key);
2601 save_menus = 1;
2605 if (ok) {
2606 for (i = 0; i < menu->cascade_no; i++) {
2607 if (saveMenuRecurs(menus, scr, menu->cascades[i]))
2608 save_menus = 1;
2611 return save_menus;
2613 #endif /* !LITE */
2616 #define COMPLAIN(key) wwarning(_("bad value in menus state info: %s"), key)
2619 static Bool
2620 getMenuInfo(WMPropList *info, int *x, int *y, Bool *lowered)
2622 WMPropList *pos;
2624 *lowered = False;
2626 if (WMIsPLArray(info)) {
2627 WMPropList *flags;
2628 pos = WMGetFromPLArray(info, 0);
2629 flags = WMGetFromPLArray(info, 1);
2630 if (flags != NULL && WMIsPLString(flags) && WMGetFromPLString(flags) != NULL
2631 && strcmp(WMGetFromPLString(flags), "lowered") == 0) {
2632 *lowered = True;
2634 } else {
2635 pos = info;
2638 if (pos != NULL && WMIsPLString(pos)) {
2639 if (sscanf(WMGetFromPLString(pos), "%i,%i", x, y)!=2)
2640 COMPLAIN("Position");
2641 } else {
2642 COMPLAIN("(position, flags...)");
2643 return False;
2646 return True;
2650 static int
2651 restoreMenu(WScreen *scr, WMPropList *menu, int which)
2653 int x, y;
2654 Bool lowered = False;
2655 WMenu *pmenu = NULL;
2657 if (!menu)
2658 return False;
2660 if (!getMenuInfo(menu, &x, &y, &lowered))
2661 return False;
2664 #ifndef LITE
2665 if (which & WSS_SWITCHMENU) {
2666 OpenSwitchMenu(scr, x, y, False);
2667 pmenu = scr->switch_menu;
2669 #endif /* !LITE */
2671 if (pmenu) {
2672 int width = MENUW(pmenu);
2673 int height = MENUH(pmenu);
2674 WMRect rect = wGetRectForHead(scr, wGetHeadForPointerLocation(scr));
2676 if (lowered) {
2677 changeMenuLevels(pmenu, True);
2680 if (x < rect.pos.x - width) x = rect.pos.x;
2681 if (x > rect.pos.x + rect.size.width) x = rect.pos.x + rect.size.width - width;
2682 if (y < rect.pos.y) y = rect.pos.y;
2683 if (y > rect.pos.y + rect.size.height) y = rect.pos.y + rect.size.height - height;
2685 wMenuMove(pmenu, x, y, True);
2686 pmenu->flags.buttoned = 1;
2687 wFrameWindowShowButton(pmenu->frame, WFF_RIGHT_BUTTON);
2688 return True;
2690 return False;
2694 #ifndef LITE
2695 static int
2696 restoreMenuRecurs(WScreen *scr, WMPropList *menus, WMenu *menu, char *path)
2698 WMPropList *key, *entry;
2699 char buffer[512];
2700 int i, x, y, res;
2701 Bool lowered;
2703 if (strlen(path) + strlen(menu->frame->title) > 510)
2704 return False;
2706 snprintf(buffer, sizeof(buffer), "%s\\%s", path, menu->frame->title);
2707 key = WMCreatePLString(buffer);
2708 entry = WMGetFromPLDictionary(menus, key);
2709 res = False;
2711 if (entry && getMenuInfo(entry, &x, &y, &lowered)) {
2713 if (!menu->flags.mapped) {
2714 int width = MENUW(menu);
2715 int height = MENUH(menu);
2716 WMRect rect = wGetRectForHead(scr, wGetHeadForPointerLocation(scr));
2718 wMenuMapAt(menu, x, y, False);
2720 if (menu->parent) {
2721 /* make parent map the copy in place of the original */
2722 for (i=0; i<menu->parent->cascade_no; i++) {
2723 if (menu->parent->cascades[i] == menu) {
2724 menu->parent->cascades[i] = menu->brother;
2725 break;
2729 if (lowered) {
2730 changeMenuLevels(menu, True);
2733 if (x < rect.pos.x - width) x = rect.pos.x;
2734 if (x > rect.pos.x + rect.size.width) x = rect.pos.x + rect.size.width - width;
2735 if (y < rect.pos.y) y = rect.pos.y;
2736 if (y > rect.pos.y + rect.size.height) y = rect.pos.y + rect.size.height - height;
2738 wMenuMove(menu, x, y, True);
2739 menu->flags.buttoned = 1;
2740 wFrameWindowShowButton(menu->frame, WFF_RIGHT_BUTTON);
2741 res = True;
2745 WMReleasePropList(key);
2747 for (i=0; i<menu->cascade_no; i++) {
2748 if (restoreMenuRecurs(scr, menus, menu->cascades[i], buffer) != False)
2749 res = True;
2752 return res;
2754 #endif /* !LITE */
2757 void
2758 wMenuRestoreState(WScreen *scr)
2760 WMPropList *menus, *menu, *key, *skey;
2762 if (!scr->session_state) {
2763 return;
2766 key = WMCreatePLString("Menus");
2767 menus = WMGetFromPLDictionary(scr->session_state, key);
2768 WMReleasePropList(key);
2770 if (!menus)
2771 return;
2773 /* restore menus */
2775 skey = WMCreatePLString("SwitchMenu");
2776 menu = WMGetFromPLDictionary(menus, skey);
2777 WMReleasePropList(skey);
2778 restoreMenu(scr, menu, WSS_SWITCHMENU);
2780 #ifndef LITE
2781 if (!scr->root_menu) {
2782 OpenRootMenu(scr, scr->scr_width*2, 0, False);
2783 wMenuUnmap(scr->root_menu);
2785 restoreMenuRecurs(scr, menus, scr->root_menu, "");
2786 #endif /* !LITE */
2790 void
2791 OpenWorkspaceMenu(WScreen *scr, int x, int y)
2793 WMenu *menu, *parent;
2794 WMenuEntry *entry;
2796 #ifndef LITE
2797 if (!scr->root_menu) {
2798 OpenRootMenu(scr, scr->scr_width*2, 0, False);
2799 wMenuUnmap(scr->root_menu);
2801 #endif
2802 menu = scr->workspace_menu;
2803 if (menu) {
2804 if (menu->flags.mapped) {
2805 if (!menu->flags.buttoned) {
2806 wMenuUnmap(menu);
2807 parent = menu->parent;
2808 if (parent && parent->selected_entry >= 0) {
2809 entry = parent->entries[parent->selected_entry];
2810 if (parent->cascades[entry->cascade] == menu) {
2811 selectEntry(parent, -1);
2812 wMenuMapAt(menu, x, y, False);
2815 } else {
2816 wRaiseFrame(menu->frame->core);
2817 wMenuMapCopyAt(menu, x, y);
2819 } else {
2820 wMenuMapAt(menu, x, y, False);