fixed some compilation issues with gcc-2.9x
[wmaker-crm.git] / src / menu.c
bloba1a8bf2c75b8f5825c413140113c434dae36827f
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 ******/
71 #define WSS_ROOTMENU (1<<0)
72 #define WSS_SWITCHMENU (1<<1)
73 #define WSS_WSMENU (1<<2)
76 static struct {
77 int steps;
78 int delay;
79 } menuScrollParameters[5] = {
80 {MENU_SCROLL_STEPS_UF, MENU_SCROLL_DELAY_UF},
81 {MENU_SCROLL_STEPS_F, MENU_SCROLL_DELAY_F},
82 {MENU_SCROLL_STEPS_M, MENU_SCROLL_DELAY_M},
83 {MENU_SCROLL_STEPS_S, MENU_SCROLL_DELAY_S},
84 {MENU_SCROLL_STEPS_US, MENU_SCROLL_DELAY_US}};
87 static void menuMouseDown(WObjDescriptor *desc, XEvent *event);
88 static void menuExpose(WObjDescriptor *desc, XEvent *event);
90 static void menuTitleDoubleClick(WCoreWindow *sender, void *data, XEvent *event);
91 static void menuTitleMouseDown(WCoreWindow *sender, void *data, XEvent *event);
93 static void menuCloseClick(WCoreWindow *sender, void *data, XEvent *event);
95 static void updateTexture(WMenu *menu);
97 #ifndef LITE
98 static int saveMenuRecurs(WMPropList *menus, WScreen *scr, WMenu *menu);
99 static int restoreMenuRecurs(WScreen *scr, WMPropList *menus, WMenu *menu, char *path);
100 #endif /* !LITE */
102 static void selectEntry(WMenu *menu, int entry_no);
103 static void closeCascade(WMenu *menu);
106 /****** Notification Observers ******/
108 static void
109 appearanceObserver(void *self, WMNotification *notif)
111 WMenu *menu = (WMenu*)self;
112 int flags = (int)WMGetNotificationClientData(notif);
114 if (!menu->flags.realized)
115 return;
117 if (WMGetNotificationName(notif) == WNMenuAppearanceSettingsChanged) {
118 if (flags & WFontSettings) {
119 menu->flags.realized = 0;
120 wMenuRealize(menu);
122 if (flags & WTextureSettings) {
123 if (!menu->flags.brother)
124 updateTexture(menu);
126 if (flags & (WTextureSettings|WColorSettings)) {
127 wMenuPaint(menu);
129 } else if (menu->flags.titled) {
131 if (flags & WFontSettings) {
132 menu->flags.realized = 0;
133 wMenuRealize(menu);
135 if (flags & WTextureSettings) {
136 menu->frame->flags.need_texture_remake = 1;
138 if (flags & (WColorSettings|WTextureSettings)) {
139 wFrameWindowPaint(menu->frame);
144 /************************************/
148 *----------------------------------------------------------------------
149 * wMenuCreate--
150 * Creates a new empty menu with the specified title. If main_menu
151 * is True, the created menu will be a main menu, which has some special
152 * properties such as being placed over other normal menus.
153 * If title is NULL, the menu will have no titlebar.
155 * Returns:
156 * The created menu.
157 *----------------------------------------------------------------------
159 WMenu*
160 wMenuCreate(WScreen *screen, char *title, int main_menu)
162 WMenu *menu;
163 static int brother=0;
164 int tmp, flags;
166 menu = wmalloc(sizeof(WMenu));
168 memset(menu, 0, sizeof(WMenu));
170 #ifdef SINGLE_MENULEVEL
171 tmp = WMSubmenuLevel;
172 #else
173 tmp = (main_menu ? WMMainMenuLevel : WMSubmenuLevel);
174 #endif
176 flags = WFF_SINGLE_STATE|WFF_BORDER;
177 if (title) {
178 flags |= WFF_TITLEBAR|WFF_RIGHT_BUTTON;
179 menu->flags.titled = 1;
181 menu->frame =
182 wFrameWindowCreate(screen, tmp, 8, 2, 1, 1, &wPreferences.menu_title_clearance, flags,
183 screen->menu_title_texture, NULL,
184 screen->menu_title_color,
185 &screen->menu_title_font);
187 menu->frame->core->descriptor.parent = menu;
188 menu->frame->core->descriptor.parent_type = WCLASS_MENU;
189 menu->frame->core->descriptor.handle_mousedown = menuMouseDown;
191 wFrameWindowHideButton(menu->frame, WFF_RIGHT_BUTTON);
193 if (title) {
194 menu->frame->title = wstrdup(title);
197 menu->frame->flags.justification = WTJ_LEFT;
199 menu->frame->rbutton_image = screen->b_pixmaps[WBUT_CLOSE];
201 menu->entry_no = 0;
202 menu->alloced_entries = 0;
203 menu->selected_entry = -1;
204 menu->entries = NULL;
206 menu->frame_x = screen->app_menu_x;
207 menu->frame_y = screen->app_menu_y;
209 menu->frame->child = menu;
211 menu->flags.lowered = 0;
213 /* create borders */
214 if (title) {
215 /* setup object descriptors */
216 menu->frame->on_mousedown_titlebar = menuTitleMouseDown;
217 menu->frame->on_dblclick_titlebar = menuTitleDoubleClick;
220 menu->frame->on_click_right = menuCloseClick;
223 menu->menu = wCoreCreate(menu->frame->core, 0, menu->frame->top_width,
224 menu->frame->core->width, 10);
226 menu->menu->descriptor.parent = menu;
227 menu->menu->descriptor.parent_type = WCLASS_MENU;
228 menu->menu->descriptor.handle_expose = menuExpose;
229 menu->menu->descriptor.handle_mousedown = menuMouseDown;
231 menu->menu_texture_data = None;
233 XMapWindow(dpy, menu->menu->window);
235 XFlush(dpy);
237 if (!brother) {
238 brother = 1;
239 menu->brother = wMenuCreate(screen, title, main_menu);
240 brother = 0;
241 menu->brother->flags.brother = 1;
242 menu->brother->brother = menu;
244 WMAddNotificationObserver(appearanceObserver, menu,
245 WNMenuAppearanceSettingsChanged, menu);
247 WMAddNotificationObserver(appearanceObserver, menu,
248 WNMenuTitleAppearanceSettingsChanged, menu);
251 return menu;
257 WMenu*
258 wMenuCreateForApp(WScreen *screen, char *title, int main_menu)
260 WMenu *menu;
262 menu = wMenuCreate(screen, title, main_menu);
263 if (!menu)
264 return NULL;
265 menu->flags.app_menu = 1;
266 menu->brother->flags.app_menu = 1;
268 return menu;
273 static void
274 insertEntry(WMenu *menu, WMenuEntry *entry, int index)
276 int i;
278 for (i = menu->entry_no-1; i >= index; i--) {
279 menu->entries[i]->order++;
280 menu->entries[i+1] = menu->entries[i];
282 menu->entries[index] = entry;
286 WMenuEntry*
287 wMenuInsertCallback(WMenu *menu, int index, char *text,
288 void (*callback)(WMenu *menu, WMenuEntry *entry),
289 void *clientdata)
291 WMenuEntry *entry;
293 menu->flags.realized = 0;
294 menu->brother->flags.realized = 0;
296 /* reallocate array if it's too small */
297 if (menu->entry_no >= menu->alloced_entries) {
298 void *tmp;
300 tmp = wrealloc(menu->entries,
301 sizeof(WMenuEntry)*(menu->alloced_entries+5));
303 menu->entries = tmp;
304 menu->alloced_entries += 5;
306 menu->brother->entries = tmp;
307 menu->brother->alloced_entries = menu->alloced_entries;
309 entry = wmalloc(sizeof(WMenuEntry));
310 memset(entry, 0, sizeof(WMenuEntry));
311 entry->flags.enabled = 1;
312 entry->text = wstrdup(text);
313 entry->cascade = -1;
314 entry->clientdata = clientdata;
315 entry->callback = callback;
316 if (index<0 || index>=menu->entry_no) {
317 entry->order = menu->entry_no;
318 menu->entries[menu->entry_no] = entry;
319 } else {
320 entry->order = index;
321 insertEntry(menu, entry, index);
324 menu->entry_no++;
325 menu->brother->entry_no = menu->entry_no;
327 return entry;
332 void
333 wMenuEntrySetCascade(WMenu *menu, WMenuEntry *entry, WMenu *cascade)
335 WMenu *brother = menu->brother;
336 int i, done;
338 assert(menu->flags.brother==0);
340 if (entry->cascade>=0) {
341 menu->flags.realized = 0;
342 brother->flags.realized = 0;
345 cascade->parent = menu;
347 cascade->brother->parent = brother;
349 done = 0;
350 for (i=0; i<menu->cascade_no; i++) {
351 if (menu->cascades[i]==NULL) {
352 menu->cascades[i] = cascade;
353 brother->cascades[i] = cascade->brother;
354 done = 1;
355 entry->cascade = i;
356 break;
359 if (!done) {
360 entry->cascade = menu->cascade_no;
362 menu->cascades = wrealloc(menu->cascades,
363 sizeof(WMenu)*(menu->cascade_no+1));
364 menu->cascades[menu->cascade_no++] = cascade;
367 brother->cascades = wrealloc(brother->cascades,
368 sizeof(WMenu)*(brother->cascade_no+1));
369 brother->cascades[brother->cascade_no++] = cascade->brother;
373 if (menu->flags.lowered) {
375 cascade->flags.lowered = 1;
376 ChangeStackingLevel(cascade->frame->core, WMNormalLevel);
378 cascade->brother->flags.lowered = 1;
379 ChangeStackingLevel(cascade->brother->frame->core, WMNormalLevel);
382 if (!menu->flags.realized)
383 wMenuRealize(menu);
387 void
388 wMenuEntryRemoveCascade(WMenu *menu, WMenuEntry *entry)
390 assert(menu->flags.brother==0);
392 /* destroy cascade menu */
393 if (entry->cascade>=0 && menu->cascades
394 && menu->cascades[entry->cascade]!=NULL) {
396 wMenuDestroy(menu->cascades[entry->cascade], True);
398 menu->cascades[entry->cascade] = NULL;
399 menu->brother->cascades[entry->cascade] = NULL;
401 entry->cascade = -1;
406 void
407 wMenuRemoveItem(WMenu *menu, int index)
409 int i;
411 if (menu->flags.brother) {
412 wMenuRemoveItem(menu->brother, index);
413 return;
416 if (index>=menu->entry_no) return;
418 /* destroy cascade menu */
419 wMenuEntryRemoveCascade(menu, menu->entries[index]);
421 /* destroy unshared data */
423 if (menu->entries[index]->text)
424 wfree(menu->entries[index]->text);
426 if (menu->entries[index]->rtext)
427 wfree(menu->entries[index]->rtext);
429 if (menu->entries[index]->free_cdata && menu->entries[index]->clientdata)
430 (*menu->entries[index]->free_cdata)(menu->entries[index]->clientdata);
432 wfree(menu->entries[index]);
434 for (i=index; i<menu->entry_no-1; i++) {
435 menu->entries[i+1]->order--;
436 menu->entries[i]=menu->entries[i+1];
438 menu->entry_no--;
439 menu->brother->entry_no--;
443 static Pixmap
444 renderTexture(WMenu *menu)
446 RImage *img;
447 Pixmap pix;
448 int i;
449 RColor light;
450 RColor dark;
451 RColor mid;
452 WScreen *scr = menu->menu->screen_ptr;
453 WTexture *texture = scr->menu_item_texture;
455 if (wPreferences.menu_style == MS_NORMAL) {
456 img = wTextureRenderImage(texture, menu->menu->width,
457 menu->entry_height, WREL_MENUENTRY);
458 } else {
459 img = wTextureRenderImage(texture, menu->menu->width,
460 menu->menu->height+1, WREL_MENUENTRY);
462 if (!img) {
463 wwarning(_("could not render texture: %s"),
464 RMessageForError(RErrorCode));
466 return None;
469 if (wPreferences.menu_style == MS_SINGLE_TEXTURE) {
470 light.alpha = 0;
471 light.red = light.green = light.blue = 80;
473 dark.alpha = 255;
474 dark.red = dark.green = dark.blue = 0;
476 mid.alpha = 0;
477 mid.red = mid.green = mid.blue = 40;
479 for (i = 1; i < menu->entry_no; i++) {
480 ROperateLine(img, RSubtractOperation, 0, i*menu->entry_height-2,
481 menu->menu->width-1, i*menu->entry_height-2, &mid);
483 RDrawLine(img, 0, i*menu->entry_height-1,
484 menu->menu->width-1, i*menu->entry_height-1, &dark);
486 ROperateLine(img, RAddOperation, 0, i*menu->entry_height,
487 menu->menu->width-1, i*menu->entry_height,
488 &light);
491 if (!RConvertImage(scr->rcontext, img, &pix)) {
492 wwarning(_("error rendering image:%s"), RMessageForError(RErrorCode));
494 RReleaseImage(img);
496 return pix;
500 static void
501 updateTexture(WMenu *menu)
503 WScreen *scr = menu->menu->screen_ptr;
505 /* setup background texture */
506 if (scr->menu_item_texture->any.type != WTEX_SOLID) {
507 if (!menu->flags.brother) {
508 FREE_PIXMAP(menu->menu_texture_data);
510 menu->menu_texture_data = renderTexture(menu);
512 XSetWindowBackgroundPixmap(dpy, menu->menu->window,
513 menu->menu_texture_data);
514 XClearWindow(dpy, menu->menu->window);
516 XSetWindowBackgroundPixmap(dpy, menu->brother->menu->window,
517 menu->menu_texture_data);
518 XClearWindow(dpy, menu->brother->menu->window);
520 } else {
521 XSetWindowBackground(dpy, menu->menu->window,
522 scr->menu_item_texture->any.color.pixel);
523 XClearWindow(dpy, menu->menu->window);
528 void
529 wMenuRealize(WMenu *menu)
531 int i;
532 int width, rwidth, mrwidth, mwidth;
533 int theight, twidth, eheight;
534 WScreen *scr = menu->frame->screen_ptr;
535 static int brother_done=0;
536 int flags;
538 if (!brother_done) {
539 brother_done = 1;
540 wMenuRealize(menu->brother);
541 brother_done = 0;
544 flags = WFF_SINGLE_STATE|WFF_BORDER;
545 if (menu->flags.titled)
546 flags |= WFF_TITLEBAR|WFF_RIGHT_BUTTON;
548 wFrameWindowUpdateBorders(menu->frame, flags);
550 if (menu->flags.titled) {
551 twidth = WMWidthOfString(scr->menu_title_font, menu->frame->title,
552 strlen(menu->frame->title));
553 theight = menu->frame->top_width;
554 twidth += theight + (wPreferences.new_style ? 16 : 8);
555 } else {
556 twidth = 0;
557 theight = 0;
559 eheight = WMFontHeight(scr->menu_entry_font) + 6 + wPreferences.menu_text_clearance * 2;
560 menu->entry_height = eheight;
561 mrwidth = 0;
562 mwidth = 0;
563 for (i=0; i<menu->entry_no; i++) {
564 char *text;
566 /* search widest text */
567 text = menu->entries[i]->text;
568 width = WMWidthOfString(scr->menu_entry_font, text, strlen(text))+10;
570 if (menu->entries[i]->flags.indicator) {
571 width += MENU_INDICATOR_SPACE;
574 if (width > mwidth)
575 mwidth = width;
577 /* search widest text on right */
578 text = menu->entries[i]->rtext;
579 if (text)
580 rwidth = WMWidthOfString(scr->menu_entry_font, text, strlen(text))
581 + 10;
582 else if (menu->entries[i]->cascade>=0)
583 rwidth = 16;
584 else
585 rwidth = 4;
587 if (rwidth > mrwidth)
588 mrwidth = rwidth;
590 mwidth += mrwidth;
592 if (mwidth < twidth)
593 mwidth = twidth;
596 wCoreConfigure(menu->menu, 0, theight, mwidth, menu->entry_no*eheight -1);
598 wFrameWindowResize(menu->frame, mwidth, menu->entry_no*eheight-1
599 + menu->frame->top_width + menu->frame->bottom_width);
602 updateTexture(menu);
604 menu->flags.realized = 1;
606 if (menu->flags.mapped)
607 wMenuPaint(menu);
608 if (menu->brother->flags.mapped)
609 wMenuPaint(menu->brother);
613 void
614 wMenuDestroy(WMenu *menu, int recurse)
616 int i;
618 WMRemoveNotificationObserver(menu);
620 /* remove any pending timers */
621 if (menu->timer)
622 WMDeleteTimerHandler(menu->timer);
623 menu->timer = NULL;
625 /* call destroy handler */
626 if (menu->on_destroy)
627 (*menu->on_destroy)(menu);
629 /* Destroy items if this menu own them. If this is the "brother" menu,
630 * leave them alone as it is shared by them.
632 if (!menu->flags.brother) {
633 for (i=0; i<menu->entry_no; i++) {
635 wfree(menu->entries[i]->text);
637 if (menu->entries[i]->rtext)
638 wfree(menu->entries[i]->rtext);
639 #ifdef USER_MENU
641 if (menu->entries[i]->instances){
642 WMReleasePropList(menu->entries[i]->instances);
644 #endif /* USER_MENU */
646 if (menu->entries[i]->free_cdata && menu->entries[i]->clientdata) {
647 (*menu->entries[i]->free_cdata)(menu->entries[i]->clientdata);
649 wfree(menu->entries[i]);
652 if (recurse) {
653 for (i=0; i<menu->cascade_no; i++) {
654 if (menu->cascades[i]) {
655 if (menu->cascades[i]->flags.brother)
656 wMenuDestroy(menu->cascades[i]->brother, recurse);
657 else
658 wMenuDestroy(menu->cascades[i], recurse);
663 if (menu->entries)
664 wfree(menu->entries);
668 FREE_PIXMAP(menu->menu_texture_data);
670 if (menu->cascades)
671 wfree(menu->cascades);
673 wCoreDestroy(menu->menu);
674 wFrameWindowDestroy(menu->frame);
676 /* destroy copy of this menu */
677 if (!menu->flags.brother && menu->brother)
678 wMenuDestroy(menu->brother, False);
680 wfree(menu);
684 #define F_NORMAL 0
685 #define F_TOP 1
686 #define F_BOTTOM 2
687 #define F_NONE 3
689 static void
690 drawFrame(WScreen *scr, Drawable win, int y, int w, int h, int type)
692 XSegment segs[2];
693 int i;
695 i = 0;
696 segs[i].x1 = segs[i].x2 = w-1;
697 segs[i].y1 = y;
698 segs[i].y2 = y + h - 1;
699 i++;
700 if (type != F_TOP && type != F_NONE) {
701 segs[i].x1 = 1;
702 segs[i].y1 = segs[i].y2 = y + h-2;
703 segs[i].x2 = w-1;
704 i++;
706 XDrawSegments(dpy, win, scr->menu_item_auxtexture->dim_gc, segs, i);
708 i = 0;
709 segs[i].x1 = 0;
710 segs[i].y1 = y;
711 segs[i].x2 = 0;
712 segs[i].y2 = y + h - 1;
713 i++;
714 if (type != F_BOTTOM && type != F_NONE) {
715 segs[i].x1 = 0;
716 segs[i].y1 = y;
717 segs[i].x2 = w-1;
718 segs[i].y2 = y;
719 i++;
721 XDrawSegments(dpy, win, scr->menu_item_auxtexture->light_gc, segs, i);
723 if (type != F_TOP && type != F_NONE)
724 XDrawLine(dpy, win, scr->menu_item_auxtexture->dark_gc, 0, y+h-1,
725 w-1, y+h-1);
729 static void
730 paintEntry(WMenu *menu, int index, int selected)
732 WScreen *scr=menu->frame->screen_ptr;
733 Window win = menu->menu->window;
734 WMenuEntry *entry=menu->entries[index];
735 GC light, dim, dark;
736 WMColor *color;
737 int x, y, w, h, tw;
738 int type;
740 if (!menu->flags.realized) return;
741 h = menu->entry_height;
742 w = menu->menu->width;
743 y = index * h;
745 light = scr->menu_item_auxtexture->light_gc;
746 dim = scr->menu_item_auxtexture->dim_gc;
747 dark = scr->menu_item_auxtexture->dark_gc;
749 if (wPreferences.menu_style == MS_FLAT && menu->entry_no > 1) {
750 if (index == 0)
751 type = F_TOP;
752 else if (index == menu->entry_no - 1)
753 type = F_BOTTOM;
754 else
755 type = F_NONE;
756 } else {
757 type = F_NORMAL;
760 /* paint background */
761 if (selected) {
762 XFillRectangle(dpy, win, WMColorGC(scr->select_color), 1, y+1, w-2, h-3);
763 if (scr->menu_item_texture->any.type == WTEX_SOLID)
764 drawFrame(scr, win, y, w, h, type);
765 } else {
766 if (scr->menu_item_texture->any.type == WTEX_SOLID) {
767 XClearArea(dpy, win, 0, y + 1, w - 1, h - 3, False);
768 /* draw the frame */
769 drawFrame(scr, win, y, w, h, type);
770 } else {
771 XClearArea(dpy, win, 0, y, w, h, False);
775 if (selected) {
776 if (entry->flags.enabled)
777 color = scr->select_text_color;
778 else
779 color = scr->dtext_color;
780 } else if (!entry->flags.enabled) {
781 color = scr->dtext_color;
782 } else {
783 color = scr->mtext_color;
785 /* draw text */
786 x = 5;
787 if (entry->flags.indicator)
788 x += MENU_INDICATOR_SPACE + 2;
790 WMDrawString(scr->wmscreen, win, color, scr->menu_entry_font,
791 x, 3 + y + wPreferences.menu_text_clearance, entry->text, strlen(entry->text));
793 if (entry->cascade>=0) {
794 /* draw the cascade indicator */
795 XDrawLine(dpy, win, dim, w-11, y+6, w-6, y+h/2-1);
796 XDrawLine(dpy, win, light, w-11, y+h-8, w-6, y+h/2-1);
797 XDrawLine(dpy, win, dark, w-12, y+6, w-12, y+h-8);
800 /* draw indicator */
801 if (entry->flags.indicator && entry->flags.indicator_on) {
802 int iw, ih;
803 WPixmap *indicator;
806 switch (entry->flags.indicator_type) {
807 case MI_CHECK:
808 indicator = scr->menu_check_indicator;
809 break;
810 case MI_MINIWINDOW:
811 indicator = scr->menu_mini_indicator;
812 break;
813 case MI_HIDDEN:
814 indicator = scr->menu_hide_indicator;
815 break;
816 case MI_SHADED:
817 indicator = scr->menu_shade_indicator;
818 break;
819 case MI_DIAMOND:
820 default:
821 indicator = scr->menu_radio_indicator;
822 break;
825 iw = indicator->width;
826 ih = indicator->height;
827 XSetClipMask(dpy, scr->copy_gc, indicator->mask);
828 XSetClipOrigin(dpy, scr->copy_gc, 5, y+(h-ih)/2);
829 if (selected) {
830 if (entry->flags.enabled) {
831 XSetForeground(dpy, scr->copy_gc, WMColorPixel(scr->select_text_color));
832 } else {
833 XSetForeground(dpy, scr->copy_gc, WMColorPixel(scr->dtext_color));
835 } else {
836 if (entry->flags.enabled) {
837 XSetForeground(dpy, scr->copy_gc, WMColorPixel(scr->mtext_color));
838 } else {
839 XSetForeground(dpy, scr->copy_gc, WMColorPixel(scr->dtext_color));
842 XFillRectangle(dpy, win, scr->copy_gc, 5, y+(h-ih)/2, iw, ih);
844 XCopyArea(dpy, indicator->image, win, scr->copy_gc, 0, 0,
845 iw, ih, 5, y+(h-ih)/2);
847 XSetClipOrigin(dpy, scr->copy_gc, 0, 0);
850 /* draw right text */
852 if (entry->rtext && entry->cascade<0) {
853 tw = WMWidthOfString(scr->menu_entry_font, entry->rtext,
854 strlen(entry->rtext));
856 WMDrawString(scr->wmscreen, win, color, scr->menu_entry_font, w-6-tw,
857 y + 3 + wPreferences.menu_text_clearance, entry->rtext, strlen(entry->rtext));
862 static void
863 move_menus(WMenu *menu, int x, int y)
865 while (menu->parent) {
866 menu = menu->parent;
867 x -= MENUW(menu);
868 if (!wPreferences.align_menus && menu->selected_entry>=0) {
869 y -= menu->selected_entry*menu->entry_height;
872 wMenuMove(menu, x, y, True);
875 static void
876 makeVisible(WMenu *menu)
878 WScreen *scr = menu->frame->screen_ptr;
879 int x1, y1, x2, y2, new_x, new_y, move;
880 WMRect rect = wGetRectForHead(scr, wGetHeadForPointerLocation(scr));
882 if (menu->entry_no<0) return;
884 x1 = menu->frame_x;
885 y1 = menu->frame_y+menu->frame->top_width
886 + menu->selected_entry*menu->entry_height;
887 x2 = x1 + MENUW(menu);
888 y2 = y1 + menu->entry_height;
890 new_x = x1;
891 new_y = y1;
892 move = 0;
895 if (x1 < rect.pos.x) {
896 new_x = rect.pos.x;
897 move = 1;
898 } else if (x2 >= rect.pos.x + rect.size.width) {
899 new_x = rect.pos.x + rect.size.width - MENUW(menu) - 1;
900 move = 1;
903 if (y1 < rect.pos.y) {
904 new_y = rect.pos.y;
905 move = 1;
906 } else if (y2 >= rect.pos.y + rect.size.height) {
907 new_y = rect.pos.y + rect.size.height - menu->entry_height - 1;
908 move = 1;
911 new_y = new_y - menu->frame->top_width
912 - menu->selected_entry*menu->entry_height;
913 move_menus(menu, new_x, new_y);
917 static int
918 check_key(WMenu *menu, XKeyEvent *event)
920 int i, ch, s;
921 char buffer[32];
923 if (XLookupString(event, buffer, 32, NULL, NULL)<1)
924 return -1;
926 ch = toupper(buffer[0]);
928 s = (menu->selected_entry>=0 ? menu->selected_entry+1 : 0);
930 again:
931 for (i=s; i<menu->entry_no; i++) {
932 if (ch==toupper(menu->entries[i]->text[0])) {
933 return i;
936 /* no match. Retry from start, if previous started from a selected entry */
937 if (s!=0) {
938 s = 0;
939 goto again;
941 return -1;
945 static int
946 keyboardMenu(WMenu *menu)
948 XEvent event;
949 KeySym ksym=NoSymbol;
950 int done=0;
951 int index;
952 WMenuEntry *entry;
953 int old_pos_x = menu->frame_x;
954 int old_pos_y = menu->frame_y;
955 int new_x = old_pos_x, new_y = old_pos_y;
956 WMRect rect = wGetRectForHead(menu->frame->screen_ptr,
957 wGetHeadForPointerLocation(menu->frame->screen_ptr));
959 if (menu->flags.editing)
960 return False;
963 XGrabKeyboard(dpy, menu->frame->core->window, True, GrabModeAsync,
964 GrabModeAsync, CurrentTime);
967 if (menu->frame_y+menu->frame->top_width >= rect.pos.y + rect.size.height)
968 new_y = rect.pos.y + rect.size.height - menu->frame->top_width;
970 if (menu->frame_x+MENUW(menu) >= rect.pos.x + rect.size.width)
971 new_x = rect.pos.x + rect.size.width - MENUW(menu) - 1;
973 move_menus(menu, new_x, new_y);
975 while (!done && menu->flags.mapped) {
976 XAllowEvents(dpy, AsyncKeyboard, CurrentTime);
977 WMMaskEvent(dpy, ExposureMask|ButtonMotionMask|ButtonPressMask
978 |ButtonReleaseMask|KeyPressMask|KeyReleaseMask
979 |SubstructureNotifyMask, &event);
981 switch (event.type) {
982 case KeyPress:
983 ksym = XLookupKeysym(&event.xkey, 0);
984 switch (ksym) {
985 case XK_Escape:
986 done = 1;
987 break;
989 case XK_Home:
990 #ifdef XK_KP_Home
991 case XK_KP_Home:
992 #endif
993 selectEntry(menu, 0);
994 makeVisible(menu);
995 break;
997 case XK_End:
998 #ifdef XK_KP_End
999 case XK_KP_End:
1000 #endif
1001 selectEntry(menu, menu->entry_no-1);
1002 makeVisible(menu);
1003 break;
1005 case XK_Up:
1006 #ifdef ARROWLESS_KBD
1007 case XK_k:
1008 #endif
1009 #ifdef XK_KP_Up
1010 case XK_KP_Up:
1011 #endif
1012 if (menu->selected_entry <= 0)
1013 selectEntry(menu, menu->entry_no-1);
1014 else
1015 selectEntry(menu, menu->selected_entry-1);
1016 makeVisible(menu);
1017 break;
1019 case XK_Down:
1020 #ifdef ARROWLESS_KBD
1021 case XK_j:
1022 #endif
1023 #ifdef XK_KP_Down
1024 case XK_KP_Down:
1025 #endif
1026 if (menu->selected_entry<0)
1027 selectEntry(menu, 0);
1028 else if (menu->selected_entry == menu->entry_no-1)
1029 selectEntry(menu, 0);
1030 else if (menu->selected_entry < menu->entry_no-1)
1031 selectEntry(menu, menu->selected_entry+1);
1032 makeVisible(menu);
1033 break;
1035 case XK_Right:
1036 #ifdef ARROWLESS_KBD
1037 case XK_l:
1038 #endif
1039 #ifdef XK_KP_Right
1040 case XK_KP_Right:
1041 #endif
1042 if (menu->selected_entry>=0) {
1043 WMenuEntry *entry;
1044 entry = menu->entries[menu->selected_entry];
1046 if (entry->cascade >= 0 && menu->cascades
1047 && menu->cascades[entry->cascade]->entry_no > 0) {
1049 XUngrabKeyboard(dpy, CurrentTime);
1051 selectEntry(menu->cascades[entry->cascade], 0);
1052 if (!keyboardMenu(menu->cascades[entry->cascade]))
1053 done = 1;
1055 XGrabKeyboard(dpy, menu->frame->core->window, True,
1056 GrabModeAsync, GrabModeAsync,
1057 CurrentTime);
1060 break;
1062 case XK_Left:
1063 #ifdef ARROWLESS_KBD
1064 case XK_h:
1065 #endif
1066 #ifdef XK_KP_Left
1067 case XK_KP_Left:
1068 #endif
1069 if (menu->parent!=NULL && menu->parent->selected_entry>=0) {
1070 selectEntry(menu, -1);
1071 move_menus(menu, old_pos_x, old_pos_y);
1072 return True;
1074 break;
1076 case XK_Return:
1077 #ifdef XK_KP_Enter
1078 case XK_KP_Enter:
1079 #endif
1080 done = 2;
1081 break;
1083 default:
1084 index = check_key(menu, &event.xkey);
1085 if (index>=0) {
1086 selectEntry(menu, index);
1089 break;
1091 default:
1092 if (event.type==ButtonPress)
1093 done = 1;
1095 WMHandleEvent(&event);
1099 XUngrabKeyboard(dpy, CurrentTime);
1101 if (done==2 && menu->selected_entry>=0) {
1102 entry = menu->entries[menu->selected_entry];
1103 } else {
1104 entry = NULL;
1107 if (entry && entry->callback!=NULL && entry->flags.enabled
1108 && entry->cascade < 0) {
1109 #if (MENU_BLINK_COUNT > 0)
1110 int sel = menu->selected_entry;
1111 int i;
1113 for (i=0; i<MENU_BLINK_COUNT; i++) {
1114 paintEntry(menu, sel, False);
1115 XSync(dpy, 0);
1116 wusleep(MENU_BLINK_DELAY);
1117 paintEntry(menu, sel, True);
1118 XSync(dpy, 0);
1119 wusleep(MENU_BLINK_DELAY);
1121 #endif
1122 selectEntry(menu, -1);
1124 if (!menu->flags.buttoned) {
1125 wMenuUnmap(menu);
1126 move_menus(menu, old_pos_x, old_pos_y);
1128 closeCascade(menu);
1130 (*entry->callback)(menu, entry);
1131 } else {
1132 if (!menu->flags.buttoned) {
1133 wMenuUnmap(menu);
1134 move_menus(menu, old_pos_x, old_pos_y);
1136 selectEntry(menu, -1);
1140 /* returns True if returning from a submenu to a parent menu,
1141 * False if exiting from menu */
1142 return False;
1146 void
1147 wMenuMapAt(WMenu *menu, int x, int y, int keyboard)
1149 WMRect rect = wGetRectForHead(menu->frame->screen_ptr,
1150 wGetHeadForPointerLocation(menu->frame->screen_ptr));
1152 if (!menu->flags.realized) {
1153 menu->flags.realized=1;
1154 wMenuRealize(menu);
1156 if (!menu->flags.mapped) {
1157 if (wPreferences.wrap_menus) {
1158 if (x<rect.pos.x) x = rect.pos.x;
1159 if (y<rect.pos.y) y = rect.pos.y;
1160 if (x+MENUW(menu) > rect.pos.x + rect.size.width)
1161 x = rect.pos.x + rect.size.width - MENUW(menu);
1162 if (y+MENUH(menu) > rect.pos.y + rect.size.height)
1163 y = rect.pos.y + rect.size.height - MENUH(menu);
1166 XMoveWindow(dpy, menu->frame->core->window, x, y);
1167 menu->frame_x = x;
1168 menu->frame_y = y;
1169 XMapWindow(dpy, menu->frame->core->window);
1170 wRaiseFrame(menu->frame->core);
1171 menu->flags.mapped = 1;
1172 } else {
1173 selectEntry(menu, 0);
1176 if (keyboard)
1177 keyboardMenu(menu);
1181 void
1182 wMenuMap(WMenu *menu)
1184 if (!menu->flags.realized) {
1185 menu->flags.realized=1;
1186 wMenuRealize(menu);
1188 if (menu->flags.app_menu && menu->parent==NULL) {
1189 menu->frame_x = menu->frame->screen_ptr->app_menu_x;
1190 menu->frame_y = menu->frame->screen_ptr->app_menu_y;
1191 XMoveWindow(dpy, menu->frame->core->window, menu->frame_x, menu->frame_y);
1193 XMapWindow(dpy, menu->frame->core->window);
1194 wRaiseFrame(menu->frame->core);
1195 menu->flags.mapped = 1;
1199 void
1200 wMenuUnmap(WMenu *menu)
1202 int i;
1204 XUnmapWindow(dpy, menu->frame->core->window);
1205 if (menu->flags.titled && menu->flags.buttoned) {
1206 wFrameWindowHideButton(menu->frame, WFF_RIGHT_BUTTON);
1208 menu->flags.buttoned = 0;
1209 menu->flags.mapped = 0;
1210 menu->flags.open_to_left = 0;
1212 for (i=0; i<menu->cascade_no; i++) {
1213 if (menu->cascades[i]!=NULL
1214 && menu->cascades[i]->flags.mapped
1215 && !menu->cascades[i]->flags.buttoned) {
1217 wMenuUnmap(menu->cascades[i]);
1220 menu->selected_entry = -1;
1225 void
1226 wMenuPaint(WMenu *menu)
1228 int i;
1230 if (!menu->flags.mapped) {
1231 return;
1234 /* paint entries */
1235 for (i=0; i<menu->entry_no; i++) {
1236 paintEntry(menu, i, i==menu->selected_entry);
1241 void
1242 wMenuSetEnabled(WMenu *menu, int index, int enable)
1244 if (index>=menu->entry_no) return;
1245 menu->entries[index]->flags.enabled=enable;
1246 paintEntry(menu, index, index==menu->selected_entry);
1247 paintEntry(menu->brother, index, index==menu->selected_entry);
1251 /* ====================================================================== */
1254 static void
1255 editEntry(WMenu *menu, WMenuEntry *entry)
1257 WTextInput *text;
1258 XEvent event;
1259 WObjDescriptor *desc;
1260 char *t;
1261 int done = 0;
1262 Window old_focus;
1263 int old_revert;
1265 menu->flags.editing = 1;
1267 text = wTextCreate(menu->menu, 1, menu->entry_height * entry->order,
1268 menu->menu->width - 2, menu->entry_height - 1);
1270 wTextPutText(text, entry->text);
1271 XGetInputFocus(dpy, &old_focus, &old_revert);
1272 XSetInputFocus(dpy, text->core->window, RevertToNone, CurrentTime);
1274 if (XGrabKeyboard(dpy, text->core->window, True, GrabModeAsync,
1275 GrabModeAsync, CurrentTime)!=GrabSuccess) {
1276 wwarning(_("could not grab keyboard"));
1277 wTextDestroy(text);
1279 wSetFocusTo(menu->frame->screen_ptr,
1280 menu->frame->screen_ptr->focused_window);
1281 return;
1285 while (!done && !text->done) {
1286 XSync(dpy, 0);
1287 XAllowEvents(dpy, AsyncKeyboard|AsyncPointer, CurrentTime);
1288 XSync(dpy, 0);
1289 WMNextEvent(dpy, &event);
1291 if (XFindContext(dpy, event.xany.window, wWinContext,
1292 (XPointer *)&desc)==XCNOENT)
1293 desc = NULL;
1295 if ((desc != NULL) && (desc->handle_anything != NULL)) {
1297 (*desc->handle_anything)(desc, &event);
1299 } else {
1300 switch (event.type) {
1301 case ButtonPress:
1302 XAllowEvents(dpy, ReplayPointer, CurrentTime);
1303 done = 1;
1305 default:
1306 WMHandleEvent(&event);
1307 break;
1312 XSetInputFocus(dpy, old_focus, old_revert, CurrentTime);
1314 wSetFocusTo(menu->frame->screen_ptr,
1315 menu->frame->screen_ptr->focused_window);
1318 t = wTextGetText(text);
1319 /* if !t, the user has canceled editing */
1320 if (t) {
1321 if (entry->text)
1322 wfree(entry->text);
1323 entry->text = wstrdup(t);
1325 menu->flags.realized = 0;
1327 wTextDestroy(text);
1329 XUngrabKeyboard(dpy, CurrentTime);
1331 if (t && menu->on_edit)
1332 (*menu->on_edit)(menu, entry);
1334 menu->flags.editing = 0;
1336 if (!menu->flags.realized)
1337 wMenuRealize(menu);
1341 static void
1342 selectEntry(WMenu *menu, int entry_no)
1344 WMenuEntry *entry;
1345 WMenu *submenu;
1346 int old_entry;
1348 if (menu->entries==NULL)
1349 return;
1351 if (entry_no >= menu->entry_no)
1352 return;
1354 old_entry = menu->selected_entry;
1355 menu->selected_entry = entry_no;
1357 if (old_entry!=entry_no) {
1359 /* unselect previous entry */
1360 if (old_entry>=0) {
1361 paintEntry(menu, old_entry, False);
1362 entry = menu->entries[old_entry];
1364 /* unmap cascade */
1365 if (entry->cascade>=0 && menu->cascades) {
1366 if (!menu->cascades[entry->cascade]->flags.buttoned) {
1367 wMenuUnmap(menu->cascades[entry->cascade]);
1372 if (entry_no<0) {
1373 menu->selected_entry = -1;
1374 return;
1376 entry = menu->entries[entry_no];
1378 if (entry->cascade>=0 && menu->cascades && entry->flags.enabled) {
1379 /* Callback for when the submenu is opened.
1381 submenu = menu->cascades[entry->cascade];
1382 if (submenu && submenu->flags.brother)
1383 submenu = submenu->brother;
1385 if (entry->callback) {
1386 /* Only call the callback if the submenu is not yet mapped.
1388 if (menu->flags.brother) {
1389 if (!submenu || !submenu->flags.mapped)
1390 (*entry->callback)(menu->brother, entry);
1391 } else {
1392 if (!submenu || !submenu->flags.buttoned)
1393 (*entry->callback)(menu, entry);
1397 /* the submenu menu might have changed */
1398 submenu = menu->cascades[entry->cascade];
1400 /* map cascade */
1401 if (!submenu->flags.mapped) {
1402 int x, y;
1404 if (!submenu->flags.realized)
1405 wMenuRealize(submenu);
1406 if (wPreferences.wrap_menus) {
1407 if (menu->flags.open_to_left)
1408 submenu->flags.open_to_left = 1;
1410 if (submenu->flags.open_to_left) {
1411 x = menu->frame_x - MENUW(submenu);
1412 if (x<0) {
1413 x = 0;
1414 submenu->flags.open_to_left = 0;
1416 } else {
1417 x = menu->frame_x + MENUW(menu);
1419 if (x + MENUW(submenu)
1420 >= menu->frame->screen_ptr->scr_width) {
1422 x = menu->frame_x - MENUW(submenu);
1423 submenu->flags.open_to_left = 1;
1426 } else {
1427 x = menu->frame_x + MENUW(menu);
1430 if (wPreferences.align_menus) {
1431 y = menu->frame_y;
1432 } else {
1433 y = menu->frame_y + menu->entry_height*entry_no;
1434 if (menu->flags.titled)
1435 y += menu->frame->top_width;
1436 if (menu->cascades[entry->cascade]->flags.titled)
1437 y -= menu->cascades[entry->cascade]->frame->top_width;
1440 wMenuMapAt(menu->cascades[entry->cascade], x, y, False);
1441 menu->cascades[entry->cascade]->parent = menu;
1442 } else {
1443 return;
1446 paintEntry(menu, entry_no, True);
1451 static WMenu*
1452 findMenu(WScreen *scr, int *x_ret, int *y_ret)
1454 WMenu *menu;
1455 WObjDescriptor *desc;
1456 Window root_ret, win, junk_win;
1457 int x, y, wx, wy;
1458 unsigned int mask;
1460 XQueryPointer(dpy, scr->root_win, &root_ret, &win, &x, &y, &wx, &wy,
1461 &mask);
1463 if (win==None) return NULL;
1465 if (XFindContext(dpy, win, wWinContext, (XPointer *)&desc)==XCNOENT)
1466 return NULL;
1468 if (desc->parent_type == WCLASS_MENU) {
1469 menu = (WMenu*)desc->parent;
1470 XTranslateCoordinates(dpy, root_ret, menu->menu->window, wx, wy,
1471 x_ret, y_ret, &junk_win);
1472 return menu;
1474 return NULL;
1480 static void
1481 closeCascade(WMenu *menu)
1483 WMenu *parent=menu->parent;
1485 if (menu->flags.brother
1486 || (!menu->flags.buttoned
1487 && (!menu->flags.app_menu||menu->parent!=NULL))) {
1489 selectEntry(menu, -1);
1490 XSync(dpy, 0);
1491 #if (MENU_BLINK_DELAY > 2)
1492 wusleep(MENU_BLINK_DELAY/2);
1493 #endif
1494 wMenuUnmap(menu);
1495 while (parent!=NULL
1496 && (parent->parent!=NULL || !parent->flags.app_menu
1497 || parent->flags.brother)
1498 && !parent->flags.buttoned) {
1499 selectEntry(parent, -1);
1500 wMenuUnmap(parent);
1501 parent = parent->parent;
1503 if (parent)
1504 selectEntry(parent, -1);
1509 static void
1510 closeBrotherCascadesOf(WMenu *menu)
1512 WMenu *tmp;
1513 int i;
1515 for (i=0; i<menu->cascade_no; i++) {
1516 if (menu->cascades[i]->flags.brother) {
1517 tmp = menu->cascades[i];
1518 } else {
1519 tmp = menu->cascades[i]->brother;
1521 if (tmp->flags.mapped) {
1522 selectEntry(tmp->parent, -1);
1523 closeBrotherCascadesOf(tmp);
1524 break;
1530 #define getEntryAt(menu, x, y) ((y)<0 ? -1 : (y)/(menu->entry_height))
1533 static WMenu*
1534 parentMenu(WMenu *menu)
1536 WMenu *parent;
1537 WMenuEntry *entry;
1539 if (menu->flags.buttoned)
1540 return menu;
1542 while (menu->parent && menu->parent->flags.mapped) {
1543 parent = menu->parent;
1544 if (parent->selected_entry < 0)
1545 break;
1546 entry = parent->entries[parent->selected_entry];
1547 if (!entry->flags.enabled || entry->cascade<0 || !parent->cascades ||
1548 parent->cascades[entry->cascade] != menu)
1549 break;
1550 menu = parent;
1551 if (menu->flags.buttoned)
1552 break;
1555 return menu;
1561 * Will raise the passed menu, if submenu = 0
1562 * If submenu > 0 will also raise all mapped submenus
1563 * until the first buttoned one
1564 * If submenu < 0 will also raise all mapped parent menus
1565 * until the first buttoned one
1568 static void
1569 raiseMenus(WMenu *menu, int submenus)
1571 WMenu *submenu;
1572 int i;
1574 if(!menu) return;
1576 wRaiseFrame(menu->frame->core);
1578 if (submenus>0 && menu->selected_entry>=0) {
1579 i = menu->entries[menu->selected_entry]->cascade;
1580 if (i>=0 && menu->cascades) {
1581 submenu = menu->cascades[i];
1582 if (submenu->flags.mapped && !submenu->flags.buttoned)
1583 raiseMenus(submenu, submenus);
1586 if (submenus<0 && !menu->flags.buttoned &&
1587 menu->parent && menu->parent->flags.mapped)
1588 raiseMenus(menu->parent, submenus);
1592 WMenu*
1593 wMenuUnderPointer(WScreen *screen)
1595 WObjDescriptor *desc;
1596 Window root_ret, win;
1597 int dummy;
1598 unsigned int mask;
1600 XQueryPointer(dpy, screen->root_win, &root_ret, &win, &dummy, &dummy,
1601 &dummy, &dummy, &mask);
1603 if (win==None) return NULL;
1605 if (XFindContext(dpy, win, wWinContext, (XPointer *)&desc)==XCNOENT)
1606 return NULL;
1608 if (desc->parent_type == WCLASS_MENU)
1609 return (WMenu *)desc->parent;
1610 return NULL;
1616 static void
1617 getPointerPosition(WScreen *scr, int *x, int *y)
1619 Window root_ret, win;
1620 int wx, wy;
1621 unsigned int mask;
1623 XQueryPointer(dpy, scr->root_win, &root_ret, &win, x, y, &wx, &wy, &mask);
1627 static void
1628 getScrollAmount(WMenu *menu, int *hamount, int *vamount)
1630 WScreen *scr = menu->menu->screen_ptr;
1631 int menuX1 = menu->frame_x;
1632 int menuY1 = menu->frame_y;
1633 int menuX2 = menu->frame_x + MENUW(menu);
1634 int menuY2 = menu->frame_y + MENUH(menu);
1635 int xroot, yroot;
1636 WMRect rect = wGetRectForHead(scr, wGetHeadForPointerLocation(scr));
1639 *hamount = 0;
1640 *vamount = 0;
1642 getPointerPosition(scr, &xroot, &yroot);
1644 if (xroot <= (rect.pos.x + 1) && menuX1 < rect.pos.x) {
1645 /* scroll to the right */
1646 *hamount = WMIN(MENU_SCROLL_STEP, abs(menuX1));
1648 } else if (xroot >= (rect.pos.x + rect.size.width - 2) &&
1649 menuX2 > (rect.pos.x + rect.size.width - 1)) {
1650 /* scroll to the left */
1651 *hamount = WMIN(MENU_SCROLL_STEP, abs(menuX2-rect.pos.x-rect.size.width-1));
1653 if (*hamount==0)
1654 *hamount = 1;
1656 *hamount = -*hamount;
1659 if (yroot <= (rect.pos.y + 1) && menuY1 < rect.pos.y) {
1660 /* scroll down */
1661 *vamount = WMIN(MENU_SCROLL_STEP, abs(menuY1));
1663 } else if (yroot >= (rect.pos.y + rect.size.height - 2) &&
1664 menuY2 > (rect.pos.y + rect.size.height - 1)) {
1665 /* scroll up */
1666 *vamount = WMIN(MENU_SCROLL_STEP, abs(menuY2-rect.pos.y-rect.size.height-2));
1668 *vamount = -*vamount;
1673 static void
1674 dragScrollMenuCallback(void *data)
1676 WMenu *menu = (WMenu*)data;
1677 WScreen *scr = menu->menu->screen_ptr;
1678 WMenu *parent = parentMenu(menu);
1679 int hamount, vamount;
1680 int x, y;
1681 int newSelectedEntry;
1683 getScrollAmount(menu, &hamount, &vamount);
1686 if (hamount != 0 || vamount != 0) {
1687 wMenuMove(parent, parent->frame_x + hamount,
1688 parent->frame_y + vamount, True);
1689 if (findMenu(scr, &x, &y)) {
1690 newSelectedEntry = getEntryAt(menu, x, y);
1691 selectEntry(menu, newSelectedEntry);
1692 } else {
1693 /* Pointer fell outside of menu. If the selected entry is
1694 * not a submenu, unselect it */
1695 if (menu->selected_entry >= 0
1696 && menu->entries[menu->selected_entry]->cascade<0)
1697 selectEntry(menu, -1);
1698 newSelectedEntry = 0;
1701 /* paranoid check */
1702 if (newSelectedEntry >= 0) {
1703 /* keep scrolling */
1704 menu->timer = WMAddTimerHandler(MENU_SCROLL_DELAY,
1705 dragScrollMenuCallback, menu);
1706 } else {
1707 menu->timer = NULL;
1709 } else {
1710 /* don't need to scroll anymore */
1711 menu->timer = NULL;
1712 if (findMenu(scr, &x, &y)) {
1713 newSelectedEntry = getEntryAt(menu, x, y);
1714 selectEntry(menu, newSelectedEntry);
1720 static void
1721 scrollMenuCallback(void *data)
1723 WMenu *menu = (WMenu*)data;
1724 WMenu *parent = parentMenu(menu);
1725 int hamount = 0; /* amount to scroll */
1726 int vamount = 0;
1728 #ifdef VIRTUAL_DESKTOP
1729 /* don't scroll if it is in vdesk mode */
1730 if (!wPreferences.vdesk_enable)
1731 #endif
1732 getScrollAmount(menu, &hamount, &vamount);
1734 if (hamount != 0 || vamount != 0) {
1735 wMenuMove(parent, parent->frame_x + hamount,
1736 parent->frame_y + vamount, True);
1738 /* keep scrolling */
1739 menu->timer = WMAddTimerHandler(MENU_SCROLL_DELAY,
1740 scrollMenuCallback, menu);
1741 } else {
1742 /* don't need to scroll anymore */
1743 menu->timer = NULL;
1749 #define MENU_SCROLL_BORDER 5
1751 static int
1752 isPointNearBoder(WMenu *menu, int x, int y)
1754 int menuX1 = menu->frame_x;
1755 int menuY1 = menu->frame_y;
1756 int menuX2 = menu->frame_x + MENUW(menu);
1757 int menuY2 = menu->frame_y + MENUH(menu);
1758 int flag = 0;
1759 int head = wGetHeadForPoint(menu->frame->screen_ptr, wmkpoint(x, y));
1760 WMRect rect = wGetRectForHead(menu->frame->screen_ptr, head);
1762 /* XXX: handle screen joins properly !! */
1764 if (x >= menuX1 && x <= menuX2 &&
1765 (y < rect.pos.y + MENU_SCROLL_BORDER ||
1766 y >= rect.pos.y + rect.size.height - MENU_SCROLL_BORDER))
1767 flag = 1;
1768 else if (y >= menuY1 && y <= menuY2 &&
1769 (x < rect.pos.x + MENU_SCROLL_BORDER ||
1770 x >= rect.pos.x + rect.size.width - MENU_SCROLL_BORDER))
1771 flag = 1;
1773 return flag;
1777 typedef struct _delay {
1778 WMenu *menu;
1779 int ox, oy;
1780 } _delay;
1783 static void
1784 leaving(_delay *dl)
1786 wMenuMove(dl->menu, dl->ox, dl->oy, True);
1787 dl->menu->jump_back = NULL;
1788 dl->menu->menu->screen_ptr->flags.jump_back_pending = 0;
1789 wfree(dl);
1793 void
1794 wMenuScroll(WMenu *menu, XEvent *event)
1796 WMenu *smenu;
1797 WMenu *omenu = parentMenu(menu);
1798 WScreen *scr = menu->frame->screen_ptr;
1799 int done = 0;
1800 int jump_back = 0;
1801 int old_frame_x = omenu->frame_x;
1802 int old_frame_y = omenu->frame_y;
1803 XEvent ev;
1805 if (omenu->jump_back)
1806 WMDeleteTimerWithClientData(omenu->jump_back);
1809 if ((/*omenu->flags.buttoned &&*/ !wPreferences.wrap_menus)
1810 || omenu->flags.app_menu) {
1811 jump_back = 1;
1814 if (!wPreferences.wrap_menus)
1815 raiseMenus(omenu, True);
1816 else
1817 raiseMenus(menu, False);
1819 if (!menu->timer)
1820 scrollMenuCallback(menu);
1822 while(!done) {
1823 int x, y, on_border, on_x_edge, on_y_edge, on_title;
1824 WMRect rect;
1826 WMNextEvent(dpy, &ev);
1827 switch (ev.type) {
1828 case EnterNotify:
1829 WMHandleEvent(&ev);
1830 case MotionNotify:
1831 x = (ev.type==MotionNotify) ? ev.xmotion.x_root : ev.xcrossing.x_root;
1832 y = (ev.type==MotionNotify) ? ev.xmotion.y_root : ev.xcrossing.y_root;
1834 /* on_border is != 0 if the pointer is between the menu
1835 * and the screen border and is close enough to the border */
1836 on_border = isPointNearBoder(menu, x, y);
1838 smenu = wMenuUnderPointer(scr);
1840 if ((smenu==NULL && !on_border) || (smenu && parentMenu(smenu)!=omenu)) {
1841 done = 1;
1842 break;
1845 rect = wGetRectForHead(scr, wGetHeadForPoint(scr, wmkpoint(x, y)));
1846 on_x_edge = x <= rect.pos.x + 1 || x >= rect.pos.x + rect.size.width - 2;
1847 on_y_edge = y <= rect.pos.y + 1 || y >= rect.pos.y + rect.size.height - 2;
1848 on_border = on_x_edge || on_y_edge;
1850 if (!on_border && !jump_back) {
1851 done = 1;
1852 break;
1855 if (menu->timer && (smenu!=menu || (!on_y_edge && !on_x_edge))) {
1856 WMDeleteTimerHandler(menu->timer);
1857 menu->timer = NULL;
1860 if (smenu != NULL)
1861 menu = smenu;
1863 if (!menu->timer)
1864 scrollMenuCallback(menu);
1865 break;
1866 case ButtonPress:
1867 /* True if we push on title, or drag the omenu to other position */
1868 on_title = ev.xbutton.x_root >= omenu->frame_x &&
1869 ev.xbutton.x_root <= omenu->frame_x + MENUW(omenu) &&
1870 ev.xbutton.y_root >= omenu->frame_y &&
1871 ev.xbutton.y_root <= omenu->frame_y + omenu->frame->top_width;
1872 WMHandleEvent(&ev);
1873 smenu = wMenuUnderPointer(scr);
1874 if (smenu == NULL || (smenu && smenu->flags.buttoned && smenu != omenu))
1875 done = 1;
1876 else if (smenu==omenu && on_title) {
1877 jump_back = 0;
1878 done = 1;
1880 break;
1881 case KeyPress:
1882 done = 1;
1883 default:
1884 WMHandleEvent(&ev);
1885 break;
1889 if (menu->timer) {
1890 WMDeleteTimerHandler(menu->timer);
1891 menu->timer = NULL;
1894 if (jump_back) {
1895 _delay *delayer;
1896 if (!omenu->jump_back) {
1897 delayer = wmalloc(sizeof(_delay));
1898 delayer->menu=omenu;
1899 delayer->ox=old_frame_x;
1900 delayer->oy=old_frame_y;
1901 omenu->jump_back = delayer;
1902 scr->flags.jump_back_pending = 1;
1904 else delayer = omenu->jump_back;
1905 WMAddTimerHandler(MENU_JUMP_BACK_DELAY,(WMCallback*)leaving, delayer);
1911 static void
1912 menuExpose(WObjDescriptor *desc, XEvent *event)
1914 wMenuPaint(desc->parent);
1917 typedef struct {
1918 int *delayed_select;
1919 WMenu *menu;
1920 WMHandlerID magic;
1921 } delay_data;
1924 static void
1925 delaySelection(void *data)
1927 delay_data *d = (delay_data*)data;
1928 int x, y, entry_no;
1929 WMenu *menu;
1931 d->magic = NULL;
1933 menu = findMenu(d->menu->menu->screen_ptr, &x, &y);
1934 if (menu && (d->menu == menu || d->delayed_select)) {
1935 entry_no = getEntryAt(menu, x, y);
1936 selectEntry(menu, entry_no);
1938 if (d->delayed_select)
1939 *(d->delayed_select) = 0;
1943 static void
1944 menuMouseDown(WObjDescriptor *desc, XEvent *event)
1946 XButtonEvent *bev = &event->xbutton;
1947 WMenu *menu = desc->parent;
1948 WMenu *smenu;
1949 WScreen *scr=menu->frame->screen_ptr;
1950 WMenuEntry *entry=NULL;
1951 XEvent ev;
1952 int close_on_exit=0;
1953 int done=0;
1954 int delayed_select = 0;
1955 int entry_no;
1956 int x, y;
1957 int prevx, prevy;
1958 int old_frame_x = 0;
1959 int old_frame_y = 0;
1960 delay_data d_data = {NULL, NULL, NULL};
1962 /* Doesn't seem to be needed anymore (if delayed selection handler is
1963 * added only if not present). there seem to be no other side effects
1964 * from removing this and it is also possible that it was only added
1965 * to avoid problems with adding the delayed selection timer handler
1966 * multiple times
1968 /*if (menu->flags.inside_handler) {
1969 return;
1971 menu->flags.inside_handler = 1;
1973 if (!wPreferences.wrap_menus) {
1974 smenu = parentMenu(menu);
1975 old_frame_x = smenu->frame_x;
1976 old_frame_y = smenu->frame_y;
1977 } else if (event->xbutton.window == menu->frame->core->window) {
1978 /* This is true if the menu was launched with right click on root window */
1979 if (!d_data.magic) {
1980 delayed_select = 1;
1981 d_data.delayed_select = &delayed_select;
1982 d_data.menu = menu;
1983 d_data.magic = WMAddTimerHandler(wPreferences.dblclick_time,
1984 delaySelection, &d_data);
1988 wRaiseFrame(menu->frame->core);
1990 close_on_exit = (bev->send_event || menu->flags.brother);
1992 smenu = findMenu(scr, &x, &y);
1993 if (!smenu) {
1994 x = -1;
1995 y = -1;
1996 } else {
1997 menu = smenu;
2000 if (menu->flags.editing) {
2001 goto byebye;
2003 entry_no = getEntryAt(menu, x, y);
2004 if (entry_no>=0) {
2005 entry = menu->entries[entry_no];
2007 if (!close_on_exit && (bev->state & ControlMask) && smenu
2008 && entry->flags.editable) {
2009 editEntry(smenu, entry);
2010 goto byebye;
2011 } else if (bev->state & ControlMask) {
2012 goto byebye;
2015 if (entry->flags.enabled && entry->cascade>=0 && menu->cascades) {
2016 WMenu *submenu = menu->cascades[entry->cascade];
2017 /* map cascade */
2018 if (submenu->flags.mapped && !submenu->flags.buttoned &&
2019 menu->selected_entry!=entry_no) {
2020 wMenuUnmap(submenu);
2022 if (!submenu->flags.mapped && !delayed_select) {
2023 selectEntry(menu, entry_no);
2024 } else if (!submenu->flags.buttoned) {
2025 selectEntry(menu, -1);
2028 } else if (!delayed_select) {
2029 selectEntry(menu, entry_no);
2032 if (!wPreferences.wrap_menus && !wPreferences.scrollable_menus) {
2033 if (!menu->timer)
2034 dragScrollMenuCallback(menu);
2038 #ifdef VIRTUAL_DESKTOP
2039 if (wPreferences.vdesk_enable) {
2040 wWorkspaceLowerEdge(scr);
2042 #endif
2044 prevx = bev->x_root;
2045 prevy = bev->y_root;
2046 while (!done) {
2047 int x, y;
2049 XAllowEvents(dpy, AsyncPointer|SyncPointer, CurrentTime);
2051 WMMaskEvent(dpy, ExposureMask|ButtonMotionMask|ButtonReleaseMask
2052 |ButtonPressMask, &ev);
2053 switch (ev.type) {
2054 case MotionNotify:
2055 smenu = findMenu(scr, &x, &y);
2057 if (smenu == NULL) {
2058 /* moved mouse out of menu */
2060 if (!delayed_select && d_data.magic) {
2061 WMDeleteTimerHandler(d_data.magic);
2062 d_data.magic = NULL;
2064 if (menu==NULL
2065 || (menu->selected_entry>=0
2066 && menu->entries[menu->selected_entry]->cascade>=0)) {
2067 prevx = ev.xmotion.x_root;
2068 prevy = ev.xmotion.y_root;
2070 break;
2072 selectEntry(menu, -1);
2073 menu = smenu;
2074 prevx = ev.xmotion.x_root;
2075 prevy = ev.xmotion.y_root;
2076 break;
2077 } else if (menu && menu!=smenu
2078 && (menu->selected_entry<0
2079 || menu->entries[menu->selected_entry]->cascade<0)) {
2080 selectEntry(menu, -1);
2082 if (!delayed_select && d_data.magic) {
2083 WMDeleteTimerHandler(d_data.magic);
2084 d_data.magic = NULL;
2086 } else {
2088 /* hysteresis for item selection */
2090 /* check if the motion was to the side, indicating that
2091 * the user may want to cross to a submenu */
2092 if (!delayed_select && menu) {
2093 int dx;
2094 Bool moved_to_submenu;/* moved to direction of submenu */
2096 dx = abs(prevx - ev.xmotion.x_root);
2098 moved_to_submenu = False;
2099 if (dx > 0 /* if moved enough to the side */
2100 /* maybe a open submenu */
2101 && menu->selected_entry>=0
2102 /* moving to the right direction */
2103 && (wPreferences.align_menus
2104 || ev.xmotion.y_root >= prevy)) {
2105 int index;
2107 index = menu->entries[menu->selected_entry]->cascade;
2108 if (index>=0) {
2109 if (menu->cascades[index]->frame_x>menu->frame_x) {
2110 if (prevx < ev.xmotion.x_root)
2111 moved_to_submenu = True;
2112 } else {
2113 if (prevx > ev.xmotion.x_root)
2114 moved_to_submenu = True;
2120 if (menu != smenu) {
2121 if (d_data.magic) {
2122 WMDeleteTimerHandler(d_data.magic);
2123 d_data.magic = NULL;
2125 } else if (moved_to_submenu) {
2126 /* while we are moving, postpone the selection */
2127 if (d_data.magic) {
2128 WMDeleteTimerHandler(d_data.magic);
2130 d_data.delayed_select = NULL;
2131 d_data.menu = menu;
2132 d_data.magic = WMAddTimerHandler(MENU_SELECT_DELAY,
2133 delaySelection,
2134 &d_data);
2135 prevx = ev.xmotion.x_root;
2136 prevy = ev.xmotion.y_root;
2137 break;
2138 } else {
2139 if (d_data.magic) {
2140 WMDeleteTimerHandler(d_data.magic);
2141 d_data.magic = NULL;
2146 prevx = ev.xmotion.x_root;
2147 prevy = ev.xmotion.y_root;
2148 if (menu!=smenu) {
2149 /* pointer crossed menus */
2150 if (menu && menu->timer) {
2151 WMDeleteTimerHandler(menu->timer);
2152 menu->timer = NULL;
2154 if (smenu)
2155 dragScrollMenuCallback(smenu);
2157 menu = smenu;
2158 if (!menu->timer)
2159 dragScrollMenuCallback(menu);
2161 if (!delayed_select) {
2162 entry_no = getEntryAt(menu, x, y);
2163 if (entry_no>=0) {
2164 entry = menu->entries[entry_no];
2165 if (entry->flags.enabled && entry->cascade>=0 &&
2166 menu->cascades) {
2167 WMenu *submenu = menu->cascades[entry->cascade];
2168 if (submenu->flags.mapped && !submenu->flags.buttoned
2169 && menu->selected_entry!=entry_no) {
2170 wMenuUnmap(submenu);
2174 selectEntry(menu, entry_no);
2176 break;
2178 case ButtonPress:
2179 break;
2181 case ButtonRelease:
2182 if (ev.xbutton.button == event->xbutton.button)
2183 done=1;
2184 break;
2186 case Expose:
2187 WMHandleEvent(&ev);
2188 #ifdef VIRTUAL_DESKTOP
2189 /* since expose will raise edge up.. I need another ugly hack here */
2190 if (wPreferences.vdesk_enable) {
2191 wWorkspaceLowerEdge(scr);
2193 #endif
2194 break;
2198 if (menu && menu->timer) {
2199 WMDeleteTimerHandler(menu->timer);
2200 menu->timer = NULL;
2202 if (d_data.magic!=NULL) {
2203 WMDeleteTimerHandler(d_data.magic);
2204 d_data.magic = NULL;
2207 if (menu && menu->selected_entry>=0) {
2208 entry = menu->entries[menu->selected_entry];
2209 if (entry->callback!=NULL && entry->flags.enabled
2210 && entry->cascade < 0) {
2211 /* blink and erase menu selection */
2212 #if (MENU_BLINK_DELAY > 0)
2213 int sel = menu->selected_entry;
2214 int i;
2216 for (i=0; i<MENU_BLINK_COUNT; i++) {
2217 paintEntry(menu, sel, False);
2218 XSync(dpy, 0);
2219 wusleep(MENU_BLINK_DELAY);
2220 paintEntry(menu, sel, True);
2221 XSync(dpy, 0);
2222 wusleep(MENU_BLINK_DELAY);
2224 #endif
2225 /* unmap the menu, it's parents and call the callback */
2226 if (!menu->flags.buttoned &&
2227 (!menu->flags.app_menu||menu->parent!=NULL)) {
2228 closeCascade(menu);
2229 } else {
2230 selectEntry(menu, -1);
2232 (*entry->callback)(menu, entry);
2234 /* If the user double clicks an entry, the entry will
2235 * be executed twice, which is not good for things like
2236 * the root menu. So, ignore any clicks that were generated
2237 * while the entry was being executed */
2238 while (XCheckTypedWindowEvent(dpy, menu->menu->window,
2239 ButtonPress, &ev));
2240 } else if (entry->callback!=NULL && entry->cascade<0) {
2241 selectEntry(menu, -1);
2242 } else {
2243 if (entry->cascade>=0 && menu->cascades
2244 && menu->cascades[entry->cascade]->flags.brother) {
2245 selectEntry(menu, -1);
2250 if (((WMenu*)desc->parent)->flags.brother || close_on_exit || !smenu)
2251 closeCascade(desc->parent);
2253 /* close the cascade windows that should not remain opened */
2254 closeBrotherCascadesOf(desc->parent);
2256 if (!wPreferences.wrap_menus)
2257 wMenuMove(parentMenu(desc->parent), old_frame_x, old_frame_y, True);
2259 byebye:
2260 /* Just to be sure in case we skip the 2 above because of a goto byebye */
2261 if (menu && menu->timer) {
2262 WMDeleteTimerHandler(menu->timer);
2263 menu->timer = NULL;
2265 if (d_data.magic!=NULL) {
2266 WMDeleteTimerHandler(d_data.magic);
2267 d_data.magic = NULL;
2270 ((WMenu*)desc->parent)->flags.inside_handler = 0;
2271 #ifdef VIRTUAL_DESKTOP
2272 wWorkspaceRaiseEdge(scr);
2273 #endif
2277 void
2278 wMenuMove(WMenu *menu, int x, int y, int submenus)
2280 WMenu *submenu;
2281 int i;
2283 if (!menu) return;
2285 menu->frame_x = x;
2286 menu->frame_y = y;
2287 XMoveWindow(dpy, menu->frame->core->window, x, y);
2289 if (submenus>0 && menu->selected_entry>=0) {
2290 i = menu->entries[menu->selected_entry]->cascade;
2292 if (i>=0 && menu->cascades) {
2293 submenu = menu->cascades[i];
2294 if (submenu->flags.mapped && !submenu->flags.buttoned) {
2295 if (wPreferences.align_menus) {
2296 wMenuMove(submenu, x + MENUW(menu), y, submenus);
2297 } else {
2298 wMenuMove(submenu, x+ MENUW(menu),
2299 y + submenu->entry_height*menu->selected_entry,
2300 submenus);
2305 if (submenus<0 && menu->parent!=NULL && menu->parent->flags.mapped &&
2306 !menu->parent->flags.buttoned) {
2307 if (wPreferences.align_menus) {
2308 wMenuMove(menu->parent, x - MENUW(menu->parent), y, submenus);
2309 } else {
2310 wMenuMove(menu->parent, x - MENUW(menu->parent), menu->frame_y
2311 - menu->parent->entry_height*menu->parent->selected_entry,
2312 submenus);
2318 static void
2319 changeMenuLevels(WMenu *menu, int lower)
2321 int i;
2323 if (!lower) {
2324 ChangeStackingLevel(menu->frame->core, (!menu->parent ? WMMainMenuLevel
2325 : WMSubmenuLevel));
2326 wRaiseFrame(menu->frame->core);
2327 menu->flags.lowered = 0;
2328 } else {
2329 ChangeStackingLevel(menu->frame->core, WMNormalLevel);
2330 wLowerFrame(menu->frame->core);
2331 menu->flags.lowered = 1;
2333 for (i=0; i<menu->cascade_no; i++) {
2334 if (menu->cascades[i]
2335 && !menu->cascades[i]->flags.buttoned
2336 && menu->cascades[i]->flags.lowered!=lower) {
2337 changeMenuLevels(menu->cascades[i], lower);
2344 static void
2345 menuTitleDoubleClick(WCoreWindow *sender, void *data, XEvent *event)
2347 WMenu *menu = data;
2348 int lower;
2350 if (event->xbutton.state & MOD_MASK) {
2351 if (menu->flags.lowered) {
2352 lower = 0;
2353 } else {
2354 lower = 1;
2356 changeMenuLevels(menu, lower);
2361 static void
2362 menuTitleMouseDown(WCoreWindow *sender, void *data, XEvent *event)
2364 WMenu *menu = data;
2365 WMenu *tmp;
2366 XEvent ev;
2367 int x=menu->frame_x, y=menu->frame_y;
2368 int dx=event->xbutton.x_root, dy=event->xbutton.y_root;
2369 int i, lower;
2370 Bool started;
2372 /* can't touch the menu copy */
2373 if (menu->flags.brother)
2374 return;
2376 if (event->xbutton.button != Button1 && event->xbutton.button != Button2)
2377 return;
2379 if (event->xbutton.state & MOD_MASK) {
2380 wLowerFrame(menu->frame->core);
2381 lower = 1;
2382 } else {
2383 wRaiseFrame(menu->frame->core);
2384 lower = 0;
2386 tmp = menu;
2388 /* lower/raise all submenus */
2389 while (1) {
2390 if (tmp->selected_entry>=0 && tmp->cascades
2391 && tmp->entries[tmp->selected_entry]->cascade>=0) {
2392 tmp = tmp->cascades[tmp->entries[tmp->selected_entry]->cascade];
2393 if (!tmp || !tmp->flags.mapped)
2394 break;
2395 if (lower)
2396 wLowerFrame(tmp->frame->core);
2397 else
2398 wRaiseFrame(tmp->frame->core);
2399 } else {
2400 break;
2404 /* tear off the menu if it's a root menu or a cascade
2405 application menu */
2406 if (!menu->flags.buttoned && !menu->flags.brother
2407 && (!menu->flags.app_menu||menu->parent!=NULL)) {
2408 menu->flags.buttoned=1;
2409 wFrameWindowShowButton(menu->frame, WFF_RIGHT_BUTTON);
2410 if (menu->parent) {
2411 /* turn off selected menu entry in parent menu */
2412 selectEntry(menu->parent, -1);
2414 /* make parent map the copy in place of the original */
2415 for (i=0; i<menu->parent->cascade_no; i++) {
2416 if (menu->parent->cascades[i] == menu) {
2417 menu->parent->cascades[i] = menu->brother;
2418 break;
2424 started = False;
2425 while(1) {
2426 WMMaskEvent(dpy, ButtonMotionMask|ButtonReleaseMask|ButtonPressMask
2427 |ExposureMask, &ev);
2428 switch (ev.type) {
2429 case MotionNotify:
2430 if (started) {
2431 x += ev.xmotion.x_root - dx;
2432 y += ev.xmotion.y_root - dy;
2433 dx = ev.xmotion.x_root;
2434 dy = ev.xmotion.y_root;
2435 wMenuMove(menu, x, y, True);
2436 } else {
2437 if (abs(ev.xmotion.x_root - dx) > MOVE_THRESHOLD
2438 || abs(ev.xmotion.y_root - dy) > MOVE_THRESHOLD) {
2439 started = True;
2440 XGrabPointer(dpy, menu->frame->titlebar->window, False,
2441 ButtonMotionMask|ButtonReleaseMask
2442 |ButtonPressMask,
2443 GrabModeAsync, GrabModeAsync, None,
2444 wCursor[WCUR_MOVE], CurrentTime);
2447 break;
2449 case ButtonPress:
2450 break;
2452 case ButtonRelease:
2453 if (ev.xbutton.button != event->xbutton.button)
2454 break;
2455 XUngrabPointer(dpy, CurrentTime);
2456 return;
2458 default:
2459 WMHandleEvent(&ev);
2460 break;
2466 *----------------------------------------------------------------------
2467 * menuCloseClick--
2468 * Handles mouse click on the close button of menus. The menu is
2469 * closed when the button is clicked.
2471 * Side effects:
2472 * The closed menu is reinserted at it's parent menus
2473 * cascade list.
2474 *----------------------------------------------------------------------
2476 static void
2477 menuCloseClick(WCoreWindow *sender, void *data, XEvent *event)
2479 WMenu *menu = (WMenu*)data;
2480 WMenu *parent = menu->parent;
2481 int i;
2483 if (parent) {
2484 for (i=0; i<parent->cascade_no; i++) {
2485 /* find the entry that points to the copy */
2486 if (parent->cascades[i] == menu->brother) {
2487 /* make it point to the original */
2488 parent->cascades[i] = menu;
2489 menu->parent = parent;
2490 break;
2494 wMenuUnmap(menu);
2498 static void
2499 saveMenuInfo(WMPropList *dict, WMenu *menu, WMPropList *key)
2501 WMPropList *value, *list;
2502 char buffer[256];
2504 snprintf(buffer, sizeof(buffer), "%i,%i", menu->frame_x, menu->frame_y);
2505 value = WMCreatePLString(buffer);
2506 list = WMCreatePLArray(value, NULL);
2507 if (menu->flags.lowered)
2508 WMAddToPLArray(list, WMCreatePLString("lowered"));
2509 WMPutInPLDictionary(dict, key, list);
2510 WMReleasePropList(value);
2511 WMReleasePropList(list);
2515 void
2516 wMenuSaveState(WScreen *scr)
2518 WMPropList *menus, *key;
2519 int save_menus = 0;
2521 menus = WMCreatePLDictionary(NULL, NULL);
2523 #ifndef LITE
2524 if (scr->switch_menu && scr->switch_menu->flags.buttoned) {
2525 key = WMCreatePLString("SwitchMenu");
2526 saveMenuInfo(menus, scr->switch_menu, key);
2527 WMReleasePropList(key);
2528 save_menus = 1;
2531 if (saveMenuRecurs(menus, scr, scr->root_menu))
2532 save_menus = 1;
2534 #endif /* !LITE */
2535 if (scr->workspace_menu && scr->workspace_menu->flags.buttoned) {
2536 key = WMCreatePLString("WorkspaceMenu");
2537 saveMenuInfo(menus, scr->workspace_menu, key);
2538 WMReleasePropList(key);
2539 save_menus = 1;
2542 if (save_menus) {
2543 key = WMCreatePLString("Menus");
2544 WMPutInPLDictionary(scr->session_state, key, menus);
2545 WMReleasePropList(key);
2547 WMReleasePropList(menus);
2551 #ifndef LITE
2553 static Bool
2554 getMenuPath(WMenu *menu, char *buffer, int bufSize)
2556 Bool ok = True;
2557 int len = 0;
2559 if (!menu->flags.titled || !menu->frame->title[0])
2560 return False;
2562 len = strlen(menu->frame->title);
2563 if (len >= bufSize)
2564 return False;
2566 if (menu->parent) {
2567 ok = getMenuPath(menu->parent, buffer, bufSize - len - 1);
2568 if (!ok)
2569 return False;
2572 strcat(buffer, "\\");
2573 strcat(buffer, menu->frame->title);
2575 return True;
2579 static Bool
2580 saveMenuRecurs(WMPropList *menus, WScreen *scr, WMenu *menu)
2582 WMPropList *key;
2583 int save_menus = 0, i;
2584 char buffer[512];
2585 Bool ok = True;
2588 if (menu->flags.brother)
2589 menu = menu->brother;
2591 if (menu->flags.buttoned && menu != scr->switch_menu) {
2593 buffer[0] = '\0';
2594 ok = getMenuPath(menu, buffer, 510);
2596 if (ok) {
2597 key = WMCreatePLString(buffer);
2598 saveMenuInfo(menus, menu, key);
2599 WMReleasePropList(key);
2600 save_menus = 1;
2604 if (ok) {
2605 for (i = 0; i < menu->cascade_no; i++) {
2606 if (saveMenuRecurs(menus, scr, menu->cascades[i]))
2607 save_menus = 1;
2610 return save_menus;
2612 #endif /* !LITE */
2615 #define COMPLAIN(key) wwarning(_("bad value in menus state info:%s"), key)
2618 static Bool
2619 getMenuInfo(WMPropList *info, int *x, int *y, Bool *lowered)
2621 WMPropList *pos;
2623 *lowered = False;
2625 if (WMIsPLArray(info)) {
2626 WMPropList *flags;
2627 pos = WMGetFromPLArray(info, 0);
2628 flags = WMGetFromPLArray(info, 1);
2629 if (flags != NULL && WMIsPLString(flags) && WMGetFromPLString(flags) != NULL
2630 && strcmp(WMGetFromPLString(flags), "lowered") == 0) {
2631 *lowered = True;
2633 } else {
2634 pos = info;
2637 if (pos != NULL && WMIsPLString(pos)) {
2638 if (sscanf(WMGetFromPLString(pos), "%i,%i", x, y)!=2)
2639 COMPLAIN("Position");
2640 } else {
2641 COMPLAIN("(position, flags...)");
2642 return False;
2645 return True;
2649 static int
2650 restoreMenu(WScreen *scr, WMPropList *menu, int which)
2652 int x, y;
2653 Bool lowered = False;
2654 WMenu *pmenu = NULL;
2656 if (!menu)
2657 return False;
2659 if (!getMenuInfo(menu, &x, &y, &lowered))
2660 return False;
2663 #ifndef LITE
2664 if (which & WSS_SWITCHMENU) {
2665 OpenSwitchMenu(scr, x, y, False);
2666 pmenu = scr->switch_menu;
2668 #endif /* !LITE */
2670 if (pmenu) {
2671 int width = MENUW(pmenu);
2672 int height = MENUH(pmenu);
2673 WMRect rect = wGetRectForHead(scr, wGetHeadForPointerLocation(scr));
2675 if (lowered) {
2676 changeMenuLevels(pmenu, True);
2679 if (x < rect.pos.x - width) x = rect.pos.x;
2680 if (x > rect.pos.x + rect.size.width) x = rect.pos.x + rect.size.width - width;
2681 if (y < rect.pos.y) y = rect.pos.y;
2682 if (y > rect.pos.y + rect.size.height) y = rect.pos.y + rect.size.height - height;
2684 wMenuMove(pmenu, x, y, True);
2685 pmenu->flags.buttoned = 1;
2686 wFrameWindowShowButton(pmenu->frame, WFF_RIGHT_BUTTON);
2687 return True;
2689 return False;
2693 #ifndef LITE
2694 static int
2695 restoreMenuRecurs(WScreen *scr, WMPropList *menus, WMenu *menu, char *path)
2697 WMPropList *key, *entry;
2698 char buffer[512];
2699 int i, x, y, res;
2700 Bool lowered;
2702 if (strlen(path) + strlen(menu->frame->title) > 510)
2703 return False;
2705 snprintf(buffer, sizeof(buffer), "%s\\%s", path, menu->frame->title);
2706 key = WMCreatePLString(buffer);
2707 entry = WMGetFromPLDictionary(menus, key);
2708 res = False;
2710 if (entry && getMenuInfo(entry, &x, &y, &lowered)) {
2712 if (!menu->flags.mapped) {
2713 int width = MENUW(menu);
2714 int height = MENUH(menu);
2715 WMRect rect = wGetRectForHead(scr, wGetHeadForPointerLocation(scr));
2717 wMenuMapAt(menu, x, y, False);
2719 if (menu->parent) {
2720 /* make parent map the copy in place of the original */
2721 for (i=0; i<menu->parent->cascade_no; i++) {
2722 if (menu->parent->cascades[i] == menu) {
2723 menu->parent->cascades[i] = menu->brother;
2724 break;
2728 if (lowered) {
2729 changeMenuLevels(menu, True);
2732 if (x < rect.pos.x - width) x = rect.pos.x;
2733 if (x > rect.pos.x + rect.size.width) x = rect.pos.x + rect.size.width - width;
2734 if (y < rect.pos.y) y = rect.pos.y;
2735 if (y > rect.pos.y + rect.size.height) y = rect.pos.y + rect.size.height - height;
2737 wMenuMove(menu, x, y, True);
2738 menu->flags.buttoned = 1;
2739 wFrameWindowShowButton(menu->frame, WFF_RIGHT_BUTTON);
2740 res = True;
2744 WMReleasePropList(key);
2746 for (i=0; i<menu->cascade_no; i++) {
2747 if (restoreMenuRecurs(scr, menus, menu->cascades[i], buffer) != False)
2748 res = True;
2751 return res;
2753 #endif /* !LITE */
2756 void
2757 wMenuRestoreState(WScreen *scr)
2759 WMPropList *menus, *menu, *key, *skey;
2761 if (!scr->session_state) {
2762 return;
2765 key = WMCreatePLString("Menus");
2766 menus = WMGetFromPLDictionary(scr->session_state, key);
2767 WMReleasePropList(key);
2769 if (!menus)
2770 return;
2772 /* restore menus */
2774 skey = WMCreatePLString("SwitchMenu");
2775 menu = WMGetFromPLDictionary(menus, skey);
2776 WMReleasePropList(skey);
2777 restoreMenu(scr, menu, WSS_SWITCHMENU);
2779 #ifndef LITE
2780 if (!scr->root_menu) {
2781 OpenRootMenu(scr, scr->scr_width*2, 0, False);
2782 wMenuUnmap(scr->root_menu);
2784 restoreMenuRecurs(scr, menus, scr->root_menu, "");
2785 #endif /* !LITE */
2789 void
2790 OpenWorkspaceMenu(WScreen *scr, int x, int y)
2792 WMenu *menu, *parent;
2793 WMenuEntry *entry;
2795 #ifndef LITE
2796 if (!scr->root_menu) {
2797 OpenRootMenu(scr, scr->scr_width*2, 0, False);
2798 wMenuUnmap(scr->root_menu);
2800 #endif
2801 menu = scr->workspace_menu;
2802 if (menu) {
2803 if (menu->flags.mapped) {
2804 if (!menu->flags.buttoned) {
2805 wMenuUnmap(menu);
2806 parent = menu->parent;
2807 if (parent && parent->selected_entry >= 0) {
2808 entry = parent->entries[parent->selected_entry];
2809 if (parent->cascades[entry->cascade] == menu) {
2810 selectEntry(parent, -1);
2811 wMenuMapAt(menu, x, y, False);
2814 } else {
2815 wRaiseFrame(menu->frame->core);
2816 wMenuMapCopyAt(menu, x, y);
2818 } else {
2819 wMenuMapAt(menu, x, y, False);