For libwraster:
[wmaker-crm.git] / src / menu.c
blobaf69cabc2484a8823ae52070470d2d2fbb047b19
1 /* menu.c- generic menu, used for root menu, application menus etc.
3 * Window Maker window manager
4 *
5 * Copyright (c) 1997, 1998 Alfredo K. Kojima
6 * Copyright (c) 1997, 1998 Dan Pascu
7 *
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"
47 /****** Global Variables ******/
49 extern Cursor wCursor[WCUR_LAST];
51 extern XContext wWinContext;
53 extern WPreferences wPreferences;
55 #define MOD_MASK wPreferences.modifier_mask
57 #define MENU_SCROLL_STEP menuScrollParameters[(int)wPreferences.menu_scroll_speed].steps
58 #define MENU_SCROLL_DELAY menuScrollParameters[(int)wPreferences.menu_scroll_speed].delay
62 #define MENUW(m) ((m)->frame->core->width+2*FRAME_BORDER_WIDTH)
63 #define MENUH(m) ((m)->frame->core->height+2*FRAME_BORDER_WIDTH)
66 /***** Local Stuff ******/
68 static struct {
69 int steps;
70 int delay;
71 } menuScrollParameters[5] = {
72 {MENU_SCROLL_STEPS_UF, MENU_SCROLL_DELAY_UF},
73 {MENU_SCROLL_STEPS_F, MENU_SCROLL_DELAY_F},
74 {MENU_SCROLL_STEPS_M, MENU_SCROLL_DELAY_M},
75 {MENU_SCROLL_STEPS_S, MENU_SCROLL_DELAY_S},
76 {MENU_SCROLL_STEPS_U, MENU_SCROLL_DELAY_U}};
79 static void menuMouseDown(WObjDescriptor *desc, XEvent *event);
80 static void menuExpose(WObjDescriptor *desc, XEvent *event);
82 static void menuTitleDoubleClick(WCoreWindow *sender, void *data, XEvent *event);
83 static void menuTitleMouseDown(WCoreWindow *sender, void *data, XEvent *event);
85 static void menuCloseClick(WCoreWindow *sender, void *data, XEvent *event);
87 static void updateTexture(WMenu *menu);
89 #ifndef LITE
90 static int saveMenuRecurs(proplist_t menus, WScreen *scr, WMenu *menu);
91 static int restoreMenuRecurs(WScreen *scr, proplist_t menus, WMenu *menu, char *path);
92 #endif /* !LITE */
94 static void selectEntry(WMenu *menu, int entry_no);
95 static void closeCascade(WMenu *menu);
98 /****** Notification Observers ******/
100 static void
101 appearanceObserver(void *self, WMNotification *notif)
103 WMenu *menu = (WMenu*)self;
104 int flags = (int)WMGetNotificationClientData(notif);
106 if (!menu->flags.realized)
107 return;
109 if (WMGetNotificationName(notif) == WNMenuAppearanceSettingsChanged) {
110 if (flags & WFontSettings) {
111 menu->flags.realized = 0;
112 wMenuRealize(menu);
114 if (flags & WTextureSettings) {
115 if (!menu->flags.brother)
116 updateTexture(menu);
118 if (flags & (WTextureSettings|WColorSettings)) {
119 wMenuPaint(menu);
121 } else if (menu->flags.titled) {
123 if (flags & WFontSettings) {
124 menu->flags.realized = 0;
125 wMenuRealize(menu);
127 if (flags & WTextureSettings) {
128 menu->frame->flags.need_texture_remake = 1;
130 if (flags & (WColorSettings|WTextureSettings)) {
131 wFrameWindowPaint(menu->frame);
136 /************************************/
140 *----------------------------------------------------------------------
141 * wMenuCreate--
142 * Creates a new empty menu with the specified title. If main_menu
143 * is True, the created menu will be a main menu, which has some special
144 * properties such as being placed over other normal menus.
145 * If title is NULL, the menu will have no titlebar.
147 * Returns:
148 * The created menu.
149 *----------------------------------------------------------------------
151 WMenu*
152 wMenuCreate(WScreen *screen, char *title, int main_menu)
154 WMenu *menu;
155 static int brother=0;
156 int tmp, flags;
158 menu = wmalloc(sizeof(WMenu));
160 memset(menu, 0, sizeof(WMenu));
162 #ifdef SINGLE_MENULEVEL
163 tmp = WMSubmenuLevel;
164 #else
165 tmp = (main_menu ? WMMainMenuLevel : WMSubmenuLevel);
166 #endif
168 flags = WFF_SINGLE_STATE|WFF_BORDER;
169 if (title) {
170 flags |= WFF_TITLEBAR|WFF_RIGHT_BUTTON;
171 menu->flags.titled = 1;
173 menu->frame =
174 wFrameWindowCreate(screen, tmp, 8, 2, 1, 1, &wPreferences.menu_title_clearance, flags,
175 screen->menu_title_texture, NULL,
176 screen->menu_title_pixel,
177 &screen->menu_title_gc,
178 &screen->menu_title_font);
180 menu->frame->core->descriptor.parent = menu;
181 menu->frame->core->descriptor.parent_type = WCLASS_MENU;
182 menu->frame->core->descriptor.handle_mousedown = menuMouseDown;
184 wFrameWindowHideButton(menu->frame, WFF_RIGHT_BUTTON);
186 if (title) {
187 menu->frame->title = wstrdup(title);
190 menu->frame->flags.justification = WTJ_LEFT;
192 menu->frame->rbutton_image = screen->b_pixmaps[WBUT_CLOSE];
194 menu->entry_no = 0;
195 menu->alloced_entries = 0;
196 menu->selected_entry = -1;
197 menu->entries = NULL;
199 menu->frame_x = screen->app_menu_x;
200 menu->frame_y = screen->app_menu_y;
202 menu->frame->child = menu;
204 menu->flags.lowered = 0;
206 /* create borders */
207 if (title) {
208 /* setup object descriptors */
209 menu->frame->on_mousedown_titlebar = menuTitleMouseDown;
210 menu->frame->on_dblclick_titlebar = menuTitleDoubleClick;
213 menu->frame->on_click_right = menuCloseClick;
216 menu->menu = wCoreCreate(menu->frame->core, 0, menu->frame->top_width,
217 menu->frame->core->width, 10);
219 menu->menu->descriptor.parent = menu;
220 menu->menu->descriptor.parent_type = WCLASS_MENU;
221 menu->menu->descriptor.handle_expose = menuExpose;
222 menu->menu->descriptor.handle_mousedown = menuMouseDown;
224 menu->menu_texture_data = None;
226 XMapWindow(dpy, menu->menu->window);
228 XFlush(dpy);
230 if (!brother) {
231 brother = 1;
232 menu->brother = wMenuCreate(screen, title, main_menu);
233 brother = 0;
234 menu->brother->flags.brother = 1;
235 menu->brother->brother = menu;
237 WMAddNotificationObserver(appearanceObserver, menu,
238 WNMenuAppearanceSettingsChanged, menu);
240 WMAddNotificationObserver(appearanceObserver, menu,
241 WNMenuTitleAppearanceSettingsChanged, menu);
244 return menu;
250 WMenu*
251 wMenuCreateForApp(WScreen *screen, char *title, int main_menu)
253 WMenu *menu;
255 menu = wMenuCreate(screen, title, main_menu);
256 if (!menu)
257 return NULL;
258 menu->flags.app_menu = 1;
259 menu->brother->flags.app_menu = 1;
261 return menu;
266 static void
267 insertEntry(WMenu *menu, WMenuEntry *entry, int index)
269 int i;
271 for (i = menu->entry_no-1; i >= index; i--) {
272 menu->entries[i]->order++;
273 menu->entries[i+1] = menu->entries[i];
275 menu->entries[index] = entry;
279 WMenuEntry*
280 wMenuInsertCallback(WMenu *menu, int index, char *text,
281 void (*callback)(WMenu *menu, WMenuEntry *entry),
282 void *clientdata)
284 WMenuEntry *entry;
286 menu->flags.realized = 0;
287 menu->brother->flags.realized = 0;
289 /* reallocate array if it's too small */
290 if (menu->entry_no >= menu->alloced_entries) {
291 void *tmp;
293 tmp = wrealloc(menu->entries,
294 sizeof(WMenuEntry)*(menu->alloced_entries+5));
296 menu->entries = tmp;
297 menu->alloced_entries += 5;
299 menu->brother->entries = tmp;
300 menu->brother->alloced_entries = menu->alloced_entries;
302 entry = wmalloc(sizeof(WMenuEntry));
303 memset(entry, 0, sizeof(WMenuEntry));
304 entry->flags.enabled = 1;
305 entry->text = wstrdup(text);
306 entry->cascade = -1;
307 entry->clientdata = clientdata;
308 entry->callback = callback;
309 if (index<0 || index>=menu->entry_no) {
310 entry->order = menu->entry_no;
311 menu->entries[menu->entry_no] = entry;
312 } else {
313 entry->order = index;
314 insertEntry(menu, entry, index);
317 menu->entry_no++;
318 menu->brother->entry_no = menu->entry_no;
320 return entry;
325 void
326 wMenuEntrySetCascade(WMenu *menu, WMenuEntry *entry, WMenu *cascade)
328 WMenu *brother = menu->brother;
329 int i, done;
331 assert(menu->flags.brother==0);
333 if (entry->cascade>=0) {
334 menu->flags.realized = 0;
335 brother->flags.realized = 0;
338 cascade->parent = menu;
340 cascade->brother->parent = brother;
342 done = 0;
343 for (i=0; i<menu->cascade_no; i++) {
344 if (menu->cascades[i]==NULL) {
345 menu->cascades[i] = cascade;
346 brother->cascades[i] = cascade->brother;
347 done = 1;
348 entry->cascade = i;
349 break;
352 if (!done) {
353 entry->cascade = menu->cascade_no;
355 menu->cascades = wrealloc(menu->cascades,
356 sizeof(WMenu)*(menu->cascade_no+1));
357 menu->cascades[menu->cascade_no++] = cascade;
360 brother->cascades = wrealloc(brother->cascades,
361 sizeof(WMenu)*(brother->cascade_no+1));
362 brother->cascades[brother->cascade_no++] = cascade->brother;
366 if (menu->flags.lowered) {
368 cascade->flags.lowered = 1;
369 ChangeStackingLevel(cascade->frame->core, WMNormalLevel);
371 cascade->brother->flags.lowered = 1;
372 ChangeStackingLevel(cascade->brother->frame->core, WMNormalLevel);
375 if (!menu->flags.realized)
376 wMenuRealize(menu);
380 void
381 wMenuEntryRemoveCascade(WMenu *menu, WMenuEntry *entry)
383 assert(menu->flags.brother==0);
385 /* destroy cascade menu */
386 if (entry->cascade>=0 && menu->cascades
387 && menu->cascades[entry->cascade]!=NULL) {
389 wMenuDestroy(menu->cascades[entry->cascade], True);
391 menu->cascades[entry->cascade] = NULL;
392 menu->brother->cascades[entry->cascade] = NULL;
394 entry->cascade = -1;
399 void
400 wMenuRemoveItem(WMenu *menu, int index)
402 int i;
404 if (menu->flags.brother) {
405 wMenuRemoveItem(menu->brother, index);
406 return;
409 if (index>=menu->entry_no) return;
411 /* destroy cascade menu */
412 wMenuEntryRemoveCascade(menu, menu->entries[index]);
414 /* destroy unshared data */
416 if (menu->entries[index]->text)
417 wfree(menu->entries[index]->text);
419 if (menu->entries[index]->rtext)
420 wfree(menu->entries[index]->rtext);
422 if (menu->entries[index]->free_cdata && menu->entries[index]->clientdata)
423 (*menu->entries[index]->free_cdata)(menu->entries[index]->clientdata);
425 wfree(menu->entries[index]);
427 for (i=index; i<menu->entry_no-1; i++) {
428 menu->entries[i+1]->order--;
429 menu->entries[i]=menu->entries[i+1];
431 menu->entry_no--;
432 menu->brother->entry_no--;
436 static Pixmap
437 renderTexture(WMenu *menu)
439 RImage *img;
440 Pixmap pix;
441 int i;
442 RColor light;
443 RColor dark;
444 RColor mid;
445 WScreen *scr = menu->menu->screen_ptr;
446 WTexture *texture = scr->menu_item_texture;
448 if (wPreferences.menu_style == MS_NORMAL) {
449 img = wTextureRenderImage(texture, menu->menu->width,
450 menu->entry_height, WREL_MENUENTRY);
451 } else {
452 img = wTextureRenderImage(texture, menu->menu->width,
453 menu->menu->height+1, WREL_MENUENTRY);
455 if (!img) {
456 wwarning(_("could not render texture: %s"),
457 RMessageForError(RErrorCode));
459 return None;
462 if (wPreferences.menu_style == MS_SINGLE_TEXTURE) {
463 light.alpha = 0;
464 light.red = light.green = light.blue = 80;
466 dark.alpha = 255;
467 dark.red = dark.green = dark.blue = 0;
469 mid.alpha = 0;
470 mid.red = mid.green = mid.blue = 40;
472 for (i = 1; i < menu->entry_no; i++) {
473 ROperateLine(img, RSubtractOperation, 0, i*menu->entry_height-2,
474 menu->menu->width-1, i*menu->entry_height-2, &mid);
476 RDrawLine(img, 0, i*menu->entry_height-1,
477 menu->menu->width-1, i*menu->entry_height-1, &dark);
479 ROperateLine(img, RAddOperation, 0, i*menu->entry_height,
480 menu->menu->width-1, i*menu->entry_height,
481 &light);
484 if (!RConvertImage(scr->rcontext, img, &pix)) {
485 wwarning(_("error rendering image:%s"), RMessageForError(RErrorCode));
487 RReleaseImage(img);
489 return pix;
493 static void
494 updateTexture(WMenu *menu)
496 WScreen *scr = menu->menu->screen_ptr;
498 /* setup background texture */
499 if (scr->menu_item_texture->any.type != WTEX_SOLID) {
500 if (!menu->flags.brother) {
501 FREE_PIXMAP(menu->menu_texture_data);
503 menu->menu_texture_data = renderTexture(menu);
505 XSetWindowBackgroundPixmap(dpy, menu->menu->window,
506 menu->menu_texture_data);
507 XClearWindow(dpy, menu->menu->window);
509 XSetWindowBackgroundPixmap(dpy, menu->brother->menu->window,
510 menu->menu_texture_data);
511 XClearWindow(dpy, menu->brother->menu->window);
513 } else {
514 XSetWindowBackground(dpy, menu->menu->window,
515 scr->menu_item_texture->any.color.pixel);
516 XClearWindow(dpy, menu->menu->window);
521 void
522 wMenuRealize(WMenu *menu)
524 int i;
525 int width, rwidth, mrwidth, mwidth;
526 int theight, twidth, eheight;
527 WScreen *scr = menu->frame->screen_ptr;
528 static int brother_done=0;
529 int flags;
531 if (!brother_done) {
532 brother_done = 1;
533 wMenuRealize(menu->brother);
534 brother_done = 0;
537 flags = WFF_SINGLE_STATE|WFF_BORDER;
538 if (menu->flags.titled)
539 flags |= WFF_TITLEBAR|WFF_RIGHT_BUTTON;
541 wFrameWindowUpdateBorders(menu->frame, flags);
543 if (menu->flags.titled) {
544 twidth = WMWidthOfString(scr->menu_title_font, menu->frame->title,
545 strlen(menu->frame->title));
546 theight = menu->frame->top_width;
547 twidth += theight + (wPreferences.new_style ? 16 : 8);
548 } else {
549 twidth = 0;
550 theight = 0;
552 eheight = WMFontHeight(scr->menu_entry_font) + 6 + wPreferences.menu_text_clearance * 2;
553 menu->entry_height = eheight;
554 mrwidth = 0;
555 mwidth = 0;
556 for (i=0; i<menu->entry_no; i++) {
557 char *text;
559 /* search widest text */
560 text = menu->entries[i]->text;
561 width = WMWidthOfString(scr->menu_entry_font, text, strlen(text))+10;
563 if (menu->entries[i]->flags.indicator) {
564 width += MENU_INDICATOR_SPACE;
567 if (width > mwidth)
568 mwidth = width;
570 /* search widest text on right */
571 text = menu->entries[i]->rtext;
572 if (text)
573 rwidth = WMWidthOfString(scr->menu_entry_font, text, strlen(text))
574 + 5;
575 else if (menu->entries[i]->cascade>=0)
576 rwidth = 16;
577 else
578 rwidth = 4;
580 if (rwidth > mrwidth)
581 mrwidth = rwidth;
583 mwidth += mrwidth;
585 if (mwidth < twidth)
586 mwidth = twidth;
589 wCoreConfigure(menu->menu, 0, theight, mwidth, menu->entry_no*eheight -1);
591 wFrameWindowResize(menu->frame, mwidth, menu->entry_no*eheight-1
592 + menu->frame->top_width + menu->frame->bottom_width);
595 updateTexture(menu);
597 menu->flags.realized = 1;
599 if (menu->flags.mapped)
600 wMenuPaint(menu);
601 if (menu->brother->flags.mapped)
602 wMenuPaint(menu->brother);
606 void
607 wMenuDestroy(WMenu *menu, int recurse)
609 int i;
611 WMRemoveNotificationObserver(menu);
613 /* remove any pending timers */
614 if (menu->timer)
615 WMDeleteTimerHandler(menu->timer);
616 menu->timer = NULL;
618 /* call destroy handler */
619 if (menu->on_destroy)
620 (*menu->on_destroy)(menu);
622 /* Destroy items if this menu own them. If this is the "brother" menu,
623 * leave them alone as it is shared by them.
625 if (!menu->flags.brother) {
626 for (i=0; i<menu->entry_no; i++) {
628 wfree(menu->entries[i]->text);
630 if (menu->entries[i]->rtext)
631 wfree(menu->entries[i]->rtext);
632 #ifdef USER_MENU
634 if (menu->entries[i]->instances){
635 PLRelease(menu->entries[i]->instances);
637 #endif /* USER_MENU */
639 if (menu->entries[i]->free_cdata && menu->entries[i]->clientdata) {
640 (*menu->entries[i]->free_cdata)(menu->entries[i]->clientdata);
642 wfree(menu->entries[i]);
645 if (recurse) {
646 for (i=0; i<menu->cascade_no; i++) {
647 if (menu->cascades[i]) {
648 if (menu->cascades[i]->flags.brother)
649 wMenuDestroy(menu->cascades[i]->brother, recurse);
650 else
651 wMenuDestroy(menu->cascades[i], recurse);
656 if (menu->entries)
657 wfree(menu->entries);
661 FREE_PIXMAP(menu->menu_texture_data);
663 if (menu->cascades)
664 wfree(menu->cascades);
666 wCoreDestroy(menu->menu);
667 wFrameWindowDestroy(menu->frame);
669 /* destroy copy of this menu */
670 if (!menu->flags.brother && menu->brother)
671 wMenuDestroy(menu->brother, False);
673 wfree(menu);
677 #define F_NORMAL 0
678 #define F_TOP 1
679 #define F_BOTTOM 2
680 #define F_NONE 3
682 static void
683 drawFrame(WScreen *scr, Drawable win, int y, int w, int h, int type)
685 XSegment segs[2];
686 int i;
688 i = 0;
689 segs[i].x1 = segs[i].x2 = w-1;
690 segs[i].y1 = y;
691 segs[i].y2 = y + h - 1;
692 i++;
693 if (type != F_TOP && type != F_NONE) {
694 segs[i].x1 = 1;
695 segs[i].y1 = segs[i].y2 = y + h-2;
696 segs[i].x2 = w-1;
697 i++;
699 XDrawSegments(dpy, win, scr->menu_item_auxtexture->dim_gc, segs, i);
701 i = 0;
702 segs[i].x1 = 0;
703 segs[i].y1 = y;
704 segs[i].x2 = 0;
705 segs[i].y2 = y + h - 1;
706 i++;
707 if (type != F_BOTTOM && type != F_NONE) {
708 segs[i].x1 = 0;
709 segs[i].y1 = y;
710 segs[i].x2 = w-1;
711 segs[i].y2 = y;
712 i++;
714 XDrawSegments(dpy, win, scr->menu_item_auxtexture->light_gc, segs, i);
716 if (type != F_TOP && type != F_NONE)
717 XDrawLine(dpy, win, scr->menu_item_auxtexture->dark_gc, 0, y+h-1,
718 w-1, y+h-1);
722 static void
723 paintEntry(WMenu *menu, int index, int selected)
725 int x, y, w, h, tw;
726 int type;
727 GC light, dim, dark, textGC;
728 WScreen *scr=menu->frame->screen_ptr;
729 Window win = menu->menu->window;
730 WMenuEntry *entry=menu->entries[index];
732 if (!menu->flags.realized) return;
733 h = menu->entry_height;
734 w = menu->menu->width;
735 y = index * h;
737 light = scr->menu_item_auxtexture->light_gc;
738 dim = scr->menu_item_auxtexture->dim_gc;
739 dark = scr->menu_item_auxtexture->dark_gc;
741 if (wPreferences.menu_style == MS_FLAT && menu->entry_no > 1) {
742 if (index == 0)
743 type = F_TOP;
744 else if (index == menu->entry_no - 1)
745 type = F_BOTTOM;
746 else
747 type = F_NONE;
748 } else {
749 type = F_NORMAL;
752 /* paint background */
753 if (selected) {
754 XSetForeground(dpy, scr->select_menu_gc, scr->select_pixel);
755 XFillRectangle(dpy, win, scr->select_menu_gc, 1, y+1, w-2, h-3);
756 if (scr->menu_item_texture->any.type == WTEX_SOLID)
757 drawFrame(scr, win, y, w, h, type);
758 } else {
759 if (scr->menu_item_texture->any.type == WTEX_SOLID) {
760 XClearArea(dpy, win, 0, y + 1, w - 1, h - 3, False);
761 /* draw the frame */
762 drawFrame(scr, win, y, w, h, type);
763 } else {
764 XClearArea(dpy, win, 0, y, w, h, False);
768 if (selected) {
769 textGC = scr->select_menu_gc;
770 if (entry->flags.enabled)
771 XSetForeground(dpy, textGC, scr->select_text_pixel);
772 else
773 XSetForeground(dpy, textGC, scr->dtext_pixel);
774 } else if (!entry->flags.enabled) {
775 textGC = scr->disabled_menu_entry_gc;
776 } else {
777 textGC = scr->menu_entry_gc;
779 /* draw text */
780 x = 5;
781 if (entry->flags.indicator)
782 x += MENU_INDICATOR_SPACE + 2;
784 WMDrawString(scr->wmscreen, win, textGC, scr->menu_entry_font,
785 x, 3 + y + wPreferences.menu_text_clearance, entry->text, strlen(entry->text));
787 if (entry->cascade>=0) {
788 /* draw the cascade indicator */
789 XDrawLine(dpy,win,dim, w-11, y+6, w-6, y+h/2-1);
790 XDrawLine(dpy,win,light, w-11, y+h-8, w-6, y+h/2-1);
791 XDrawLine(dpy,win,dark, w-12, y+6, w-12, y+h-8);
794 /* draw indicator */
795 if (entry->flags.indicator && entry->flags.indicator_on) {
796 int iw, ih;
797 WPixmap *indicator;
800 switch (entry->flags.indicator_type) {
801 case MI_CHECK:
802 indicator = scr->menu_check_indicator;
803 break;
804 case MI_MINIWINDOW:
805 indicator = scr->menu_mini_indicator;
806 break;
807 case MI_HIDDEN:
808 indicator = scr->menu_hide_indicator;
809 break;
810 case MI_SHADED:
811 indicator = scr->menu_shade_indicator;
812 break;
813 case MI_DIAMOND:
814 default:
815 indicator = scr->menu_radio_indicator;
816 break;
819 iw = indicator->width;
820 ih = indicator->height;
821 XSetClipMask(dpy, scr->copy_gc, indicator->mask);
822 XSetClipOrigin(dpy, scr->copy_gc, 5, y+(h-ih)/2);
823 if (selected)
824 XSetForeground(dpy, scr->copy_gc, scr->black_pixel);
825 else
826 XSetForeground(dpy, scr->copy_gc, scr->mtext_pixel);
827 XFillRectangle(dpy, win, scr->copy_gc, 5, y+(h-ih)/2, iw, ih);
829 XCopyArea(dpy, indicator->image, win, scr->copy_gc, 0, 0,
830 iw, ih, 5, y+(h-ih)/2);
832 XSetClipOrigin(dpy, scr->copy_gc, 0, 0);
835 /* draw right text */
837 if (entry->rtext && entry->cascade<0) {
839 tw = WMWidthOfString(scr->menu_entry_font, entry->rtext,
840 strlen(entry->rtext));
841 WMDrawString(scr->wmscreen, win, textGC, scr->menu_entry_font, w-6-tw,
842 y + 3 + wPreferences.menu_text_clearance, entry->rtext, strlen(entry->rtext));
847 static void
848 move_menus(WMenu *menu, int x, int y)
850 while (menu->parent) {
851 menu = menu->parent;
852 x -= MENUW(menu);
853 if (!wPreferences.align_menus && menu->selected_entry>=0) {
854 y -= menu->selected_entry*menu->entry_height;
857 wMenuMove(menu, x, y, True);
860 static void
861 makeVisible(WMenu *menu)
863 WScreen *scr = menu->frame->screen_ptr;
864 int x1, y1, x2, y2, new_x, new_y, move;
866 if (menu->entry_no<0) return;
868 x1 = menu->frame_x;
869 y1 = menu->frame_y+menu->frame->top_width
870 + menu->selected_entry*menu->entry_height;
871 x2 = x1 + MENUW(menu);
872 y2 = y1 + menu->entry_height;
874 new_x = x1;
875 new_y = y1;
876 move = 0;
878 if (x1 < 0) {
879 new_x = 0;
880 move = 1;
881 } else if (x2 >= scr->scr_width) {
882 new_x = scr->scr_width - MENUW(menu) - 1;
883 move = 1;
886 if (y1 < 0) {
887 new_y = 0;
888 move = 1;
889 } else if (y2 >= scr->scr_height) {
890 new_y = scr->scr_height - menu->entry_height - 1;
891 move = 1;
894 new_y = new_y - menu->frame->top_width
895 - menu->selected_entry*menu->entry_height;
896 move_menus(menu, new_x, new_y);
900 static int
901 check_key(WMenu *menu, XKeyEvent *event)
903 int i, ch, s;
904 char buffer[32];
906 if (XLookupString(event, buffer, 32, NULL, NULL)<1)
907 return -1;
909 ch = toupper(buffer[0]);
911 s = (menu->selected_entry>=0 ? menu->selected_entry+1 : 0);
913 again:
914 for (i=s; i<menu->entry_no; i++) {
915 if (ch==toupper(menu->entries[i]->text[0])) {
916 return i;
919 /* no match. Retry from start, if previous started from a selected entry */
920 if (s!=0) {
921 s = 0;
922 goto again;
924 return -1;
928 static int
929 keyboardMenu(WMenu *menu)
931 XEvent event;
932 KeySym ksym=NoSymbol;
933 int done=0;
934 int index;
935 WMenuEntry *entry;
936 int old_pos_x = menu->frame_x;
937 int old_pos_y = menu->frame_y;
938 int new_x = old_pos_x, new_y = old_pos_y;
939 int scr_width = menu->frame->screen_ptr->scr_width;
940 int scr_height = menu->frame->screen_ptr->scr_height;
942 if (menu->flags.editing)
943 return False;
946 XGrabKeyboard(dpy, menu->frame->core->window, True, GrabModeAsync,
947 GrabModeAsync, CurrentTime);
949 if (menu->frame_y+menu->frame->top_width >= scr_height)
950 new_y = scr_height - menu->frame->top_width;
952 if (menu->frame_x+MENUW(menu) >= scr_width)
953 new_x = scr_width-MENUW(menu)-1;
955 move_menus(menu, new_x, new_y);
957 while (!done && menu->flags.mapped) {
958 XAllowEvents(dpy, AsyncKeyboard, CurrentTime);
959 WMMaskEvent(dpy, ExposureMask|ButtonMotionMask|ButtonPressMask
960 |ButtonReleaseMask|KeyPressMask|KeyReleaseMask
961 |SubstructureNotifyMask, &event);
963 switch (event.type) {
964 case KeyPress:
965 ksym = XLookupKeysym(&event.xkey, 0);
966 switch (ksym) {
967 case XK_Escape:
968 done = 1;
969 break;
971 case XK_Home:
972 #ifdef XK_KP_Home
973 case XK_KP_Home:
974 #endif
975 selectEntry(menu, 0);
976 makeVisible(menu);
977 break;
979 case XK_End:
980 #ifdef XK_KP_End
981 case XK_KP_End:
982 #endif
983 selectEntry(menu, menu->entry_no-1);
984 makeVisible(menu);
985 break;
987 case XK_Up:
988 #ifdef ARROWLESS_KBD
989 case XK_k:
990 #endif
991 #ifdef XK_KP_Up
992 case XK_KP_Up:
993 #endif
994 if (menu->selected_entry <= 0)
995 selectEntry(menu, menu->entry_no-1);
996 else
997 selectEntry(menu, menu->selected_entry-1);
998 makeVisible(menu);
999 break;
1001 case XK_Down:
1002 #ifdef ARROWLESS_KBD
1003 case XK_j:
1004 #endif
1005 #ifdef XK_KP_Down
1006 case XK_KP_Down:
1007 #endif
1008 if (menu->selected_entry<0)
1009 selectEntry(menu, 0);
1010 else if (menu->selected_entry == menu->entry_no-1)
1011 selectEntry(menu, 0);
1012 else if (menu->selected_entry < menu->entry_no-1)
1013 selectEntry(menu, menu->selected_entry+1);
1014 makeVisible(menu);
1015 break;
1017 case XK_Right:
1018 #ifdef ARROWLESS_KBD
1019 case XK_l:
1020 #endif
1021 #ifdef XK_KP_Right
1022 case XK_KP_Right:
1023 #endif
1024 if (menu->selected_entry>=0) {
1025 WMenuEntry *entry;
1026 entry = menu->entries[menu->selected_entry];
1028 if (entry->cascade >= 0 && menu->cascades
1029 && menu->cascades[entry->cascade]->entry_no > 0) {
1031 XUngrabKeyboard(dpy, CurrentTime);
1033 selectEntry(menu->cascades[entry->cascade], 0);
1034 if (!keyboardMenu(menu->cascades[entry->cascade]))
1035 done = 1;
1037 XGrabKeyboard(dpy, menu->frame->core->window, True,
1038 GrabModeAsync, GrabModeAsync,
1039 CurrentTime);
1042 break;
1044 case XK_Left:
1045 #ifdef ARROWLESS_KBD
1046 case XK_h:
1047 #endif
1048 #ifdef XK_KP_Left
1049 case XK_KP_Left:
1050 #endif
1051 if (menu->parent!=NULL && menu->parent->selected_entry>=0) {
1052 selectEntry(menu, -1);
1053 move_menus(menu, old_pos_x, old_pos_y);
1054 return True;
1056 break;
1058 case XK_Return:
1059 done = 2;
1060 break;
1062 default:
1063 index = check_key(menu, &event.xkey);
1064 if (index>=0) {
1065 selectEntry(menu, index);
1068 break;
1070 default:
1071 if (event.type==ButtonPress)
1072 done = 1;
1074 WMHandleEvent(&event);
1078 XUngrabKeyboard(dpy, CurrentTime);
1080 if (done==2 && menu->selected_entry>=0) {
1081 entry = menu->entries[menu->selected_entry];
1082 } else {
1083 entry = NULL;
1086 if (entry && entry->callback!=NULL && entry->flags.enabled
1087 && entry->cascade < 0) {
1088 #if (MENU_BLINK_COUNT > 0)
1089 int sel = menu->selected_entry;
1090 int i;
1092 for (i=0; i<MENU_BLINK_COUNT; i++) {
1093 paintEntry(menu, sel, False);
1094 XSync(dpy, 0);
1095 wusleep(MENU_BLINK_DELAY);
1096 paintEntry(menu, sel, True);
1097 XSync(dpy, 0);
1098 wusleep(MENU_BLINK_DELAY);
1100 #endif
1101 selectEntry(menu, -1);
1103 if (!menu->flags.buttoned) {
1104 wMenuUnmap(menu);
1105 move_menus(menu, old_pos_x, old_pos_y);
1107 closeCascade(menu);
1109 (*entry->callback)(menu, entry);
1110 } else {
1111 if (!menu->flags.buttoned) {
1112 wMenuUnmap(menu);
1113 move_menus(menu, old_pos_x, old_pos_y);
1115 selectEntry(menu, -1);
1119 /* returns True if returning from a submenu to a parent menu,
1120 * False if exiting from menu */
1121 return False;
1125 void
1126 wMenuMapAt(WMenu *menu, int x, int y, int keyboard)
1128 int scr_width = menu->frame->screen_ptr->scr_width;
1129 int scr_height = menu->frame->screen_ptr->scr_height;
1131 if (!menu->flags.realized) {
1132 menu->flags.realized=1;
1133 wMenuRealize(menu);
1135 if (!menu->flags.mapped) {
1136 if (wPreferences.wrap_menus) {
1137 if (x<0) x = 0;
1138 if (y<0) y = 0;
1139 if (x+MENUW(menu) > scr_width)
1140 x = scr_width - MENUW(menu);
1141 if (y+MENUH(menu) > scr_height)
1142 y = scr_height - MENUH(menu);
1145 XMoveWindow(dpy, menu->frame->core->window, x, y);
1146 menu->frame_x = x;
1147 menu->frame_y = y;
1148 XMapWindow(dpy, menu->frame->core->window);
1149 wRaiseFrame(menu->frame->core);
1150 menu->flags.mapped = 1;
1151 } else {
1152 selectEntry(menu, 0);
1155 if (keyboard)
1156 keyboardMenu(menu);
1160 void
1161 wMenuMap(WMenu *menu)
1163 if (!menu->flags.realized) {
1164 menu->flags.realized=1;
1165 wMenuRealize(menu);
1167 if (menu->flags.app_menu && menu->parent==NULL) {
1168 menu->frame_x = menu->frame->screen_ptr->app_menu_x;
1169 menu->frame_y = menu->frame->screen_ptr->app_menu_y;
1170 XMoveWindow(dpy, menu->frame->core->window, menu->frame_x, menu->frame_y);
1172 XMapWindow(dpy, menu->frame->core->window);
1173 wRaiseFrame(menu->frame->core);
1174 menu->flags.mapped = 1;
1178 void
1179 wMenuUnmap(WMenu *menu)
1181 int i;
1183 XUnmapWindow(dpy, menu->frame->core->window);
1184 if (menu->flags.titled && menu->flags.buttoned) {
1185 wFrameWindowHideButton(menu->frame, WFF_RIGHT_BUTTON);
1187 menu->flags.buttoned = 0;
1188 menu->flags.mapped = 0;
1189 menu->flags.open_to_left = 0;
1191 for (i=0; i<menu->cascade_no; i++) {
1192 if (menu->cascades[i]!=NULL
1193 && menu->cascades[i]->flags.mapped
1194 && !menu->cascades[i]->flags.buttoned) {
1196 wMenuUnmap(menu->cascades[i]);
1199 menu->selected_entry = -1;
1204 void
1205 wMenuPaint(WMenu *menu)
1207 int i;
1209 if (!menu->flags.mapped) {
1210 return;
1213 /* paint entries */
1214 for (i=0; i<menu->entry_no; i++) {
1215 paintEntry(menu, i, i==menu->selected_entry);
1220 void
1221 wMenuSetEnabled(WMenu *menu, int index, int enable)
1223 if (index>=menu->entry_no) return;
1224 menu->entries[index]->flags.enabled=enable;
1225 paintEntry(menu, index, index==menu->selected_entry);
1226 paintEntry(menu->brother, index, index==menu->selected_entry);
1230 /* ====================================================================== */
1233 static void
1234 editEntry(WMenu *menu, WMenuEntry *entry)
1236 WTextInput *text;
1237 XEvent event;
1238 WObjDescriptor *desc;
1239 char *t;
1240 int done = 0;
1241 Window old_focus;
1242 int old_revert;
1244 menu->flags.editing = 1;
1246 text = wTextCreate(menu->menu, 1, menu->entry_height * entry->order,
1247 menu->menu->width - 2, menu->entry_height - 1);
1249 wTextPutText(text, entry->text);
1250 XGetInputFocus(dpy, &old_focus, &old_revert);
1251 XSetInputFocus(dpy, text->core->window, RevertToNone, CurrentTime);
1253 if (XGrabKeyboard(dpy, text->core->window, True, GrabModeAsync,
1254 GrabModeAsync, CurrentTime)!=GrabSuccess) {
1255 wwarning(_("could not grab keyboard"));
1256 wTextDestroy(text);
1258 wSetFocusTo(menu->frame->screen_ptr,
1259 menu->frame->screen_ptr->focused_window);
1260 return;
1264 while (!done && !text->done) {
1265 XSync(dpy, 0);
1266 XAllowEvents(dpy, AsyncKeyboard|AsyncPointer, CurrentTime);
1267 XSync(dpy, 0);
1268 WMNextEvent(dpy, &event);
1270 if (XFindContext(dpy, event.xany.window, wWinContext,
1271 (XPointer *)&desc)==XCNOENT)
1272 desc = NULL;
1274 if ((desc != NULL) && (desc->handle_anything != NULL)) {
1276 (*desc->handle_anything)(desc, &event);
1278 } else {
1279 switch (event.type) {
1280 case ButtonPress:
1281 XAllowEvents(dpy, ReplayPointer, CurrentTime);
1282 done = 1;
1284 default:
1285 WMHandleEvent(&event);
1286 break;
1291 XSetInputFocus(dpy, old_focus, old_revert, CurrentTime);
1293 wSetFocusTo(menu->frame->screen_ptr,
1294 menu->frame->screen_ptr->focused_window);
1297 t = wTextGetText(text);
1298 /* if !t, the user has canceled editing */
1299 if (t) {
1300 if (entry->text)
1301 wfree(entry->text);
1302 entry->text = wstrdup(t);
1304 menu->flags.realized = 0;
1306 wTextDestroy(text);
1308 XUngrabKeyboard(dpy, CurrentTime);
1310 if (t && menu->on_edit)
1311 (*menu->on_edit)(menu, entry);
1313 menu->flags.editing = 0;
1315 if (!menu->flags.realized)
1316 wMenuRealize(menu);
1320 static void
1321 selectEntry(WMenu *menu, int entry_no)
1323 WMenuEntry *entry;
1324 WMenu *submenu;
1325 int old_entry;
1327 if (menu->entries==NULL)
1328 return;
1330 if (entry_no >= menu->entry_no)
1331 return;
1333 old_entry = menu->selected_entry;
1334 menu->selected_entry = entry_no;
1336 if (old_entry!=entry_no) {
1338 /* unselect previous entry */
1339 if (old_entry>=0) {
1340 paintEntry(menu, old_entry, False);
1341 entry = menu->entries[old_entry];
1343 /* unmap cascade */
1344 if (entry->cascade>=0 && menu->cascades) {
1345 if (!menu->cascades[entry->cascade]->flags.buttoned) {
1346 wMenuUnmap(menu->cascades[entry->cascade]);
1351 if (entry_no<0) {
1352 menu->selected_entry = -1;
1353 return;
1355 entry = menu->entries[entry_no];
1357 if (entry->cascade>=0 && menu->cascades && entry->flags.enabled) {
1358 /* Callback for when the submenu is opened.
1360 submenu = menu->cascades[entry->cascade];
1361 if (submenu && submenu->flags.brother)
1362 submenu = submenu->brother;
1364 if (entry->callback) {
1365 /* Only call the callback if the submenu is not yet mapped.
1367 if (menu->flags.brother) {
1368 if (!submenu || !submenu->flags.mapped)
1369 (*entry->callback)(menu->brother, entry);
1370 } else {
1371 if (!submenu || !submenu->flags.buttoned)
1372 (*entry->callback)(menu, entry);
1376 /* the submenu menu might have changed */
1377 submenu = menu->cascades[entry->cascade];
1379 /* map cascade */
1380 if (!submenu->flags.mapped) {
1381 int x, y;
1383 if (!submenu->flags.realized)
1384 wMenuRealize(submenu);
1385 if (wPreferences.wrap_menus) {
1386 if (menu->flags.open_to_left)
1387 submenu->flags.open_to_left = 1;
1389 if (submenu->flags.open_to_left) {
1390 x = menu->frame_x - MENUW(submenu);
1391 if (x<0) {
1392 x = 0;
1393 submenu->flags.open_to_left = 0;
1395 } else {
1396 x = menu->frame_x + MENUW(menu);
1398 if (x + MENUW(submenu)
1399 >= menu->frame->screen_ptr->scr_width) {
1401 x = menu->frame_x - MENUW(submenu);
1402 submenu->flags.open_to_left = 1;
1405 } else {
1406 x = menu->frame_x + MENUW(menu);
1409 if (wPreferences.align_menus) {
1410 y = menu->frame_y;
1411 } else {
1412 y = menu->frame_y + menu->entry_height*entry_no;
1413 if (menu->flags.titled)
1414 y += menu->frame->top_width;
1415 if (menu->cascades[entry->cascade]->flags.titled)
1416 y -= menu->cascades[entry->cascade]->frame->top_width;
1419 wMenuMapAt(menu->cascades[entry->cascade], x, y, False);
1420 menu->cascades[entry->cascade]->parent = menu;
1421 } else {
1422 return;
1425 paintEntry(menu, entry_no, True);
1430 static WMenu*
1431 findMenu(WScreen *scr, int *x_ret, int *y_ret)
1433 WMenu *menu;
1434 WObjDescriptor *desc;
1435 Window root_ret, win, junk_win;
1436 int x, y, wx, wy;
1437 unsigned int mask;
1439 XQueryPointer(dpy, scr->root_win, &root_ret, &win, &x, &y, &wx, &wy,
1440 &mask);
1442 if (win==None) return NULL;
1444 if (XFindContext(dpy, win, wWinContext, (XPointer *)&desc)==XCNOENT)
1445 return NULL;
1447 if (desc->parent_type == WCLASS_MENU) {
1448 menu = (WMenu*)desc->parent;
1449 XTranslateCoordinates(dpy, root_ret, menu->menu->window, wx, wy,
1450 x_ret, y_ret, &junk_win);
1451 return menu;
1453 return NULL;
1459 static void
1460 closeCascade(WMenu *menu)
1462 WMenu *parent=menu->parent;
1464 if (menu->flags.brother
1465 || (!menu->flags.buttoned
1466 && (!menu->flags.app_menu||menu->parent!=NULL))) {
1468 selectEntry(menu, -1);
1469 XSync(dpy, 0);
1470 #if (MENU_BLINK_DELAY > 2)
1471 wusleep(MENU_BLINK_DELAY/2);
1472 #endif
1473 wMenuUnmap(menu);
1474 while (parent!=NULL
1475 && (parent->parent!=NULL || !parent->flags.app_menu
1476 || parent->flags.brother)
1477 && !parent->flags.buttoned) {
1478 selectEntry(parent, -1);
1479 wMenuUnmap(parent);
1480 parent = parent->parent;
1482 if (parent)
1483 selectEntry(parent, -1);
1488 static void
1489 closeBrotherCascadesOf(WMenu *menu)
1491 WMenu *tmp;
1492 int i;
1494 for (i=0; i<menu->cascade_no; i++) {
1495 if (menu->cascades[i]->flags.brother) {
1496 tmp = menu->cascades[i];
1497 } else {
1498 tmp = menu->cascades[i]->brother;
1500 if (tmp->flags.mapped) {
1501 selectEntry(tmp->parent, -1);
1502 closeBrotherCascadesOf(tmp);
1503 break;
1509 #define getEntryAt(menu, x, y) ((y)<0 ? -1 : (y)/(menu->entry_height))
1512 static WMenu*
1513 parentMenu(WMenu *menu)
1515 WMenu *parent;
1516 WMenuEntry *entry;
1518 if (menu->flags.buttoned)
1519 return menu;
1521 while (menu->parent && menu->parent->flags.mapped) {
1522 parent = menu->parent;
1523 if (parent->selected_entry < 0)
1524 break;
1525 entry = parent->entries[parent->selected_entry];
1526 if (!entry->flags.enabled || entry->cascade<0 || !parent->cascades ||
1527 parent->cascades[entry->cascade] != menu)
1528 break;
1529 menu = parent;
1530 if (menu->flags.buttoned)
1531 break;
1534 return menu;
1540 * Will raise the passed menu, if submenu = 0
1541 * If submenu > 0 will also raise all mapped submenus
1542 * until the first buttoned one
1543 * If submenu < 0 will also raise all mapped parent menus
1544 * until the first buttoned one
1547 static void
1548 raiseMenus(WMenu *menu, int submenus)
1550 WMenu *submenu;
1551 int i;
1553 if(!menu) return;
1555 wRaiseFrame(menu->frame->core);
1557 if (submenus>0 && menu->selected_entry>=0) {
1558 i = menu->entries[menu->selected_entry]->cascade;
1559 if (i>=0 && menu->cascades) {
1560 submenu = menu->cascades[i];
1561 if (submenu->flags.mapped && !submenu->flags.buttoned)
1562 raiseMenus(submenu, submenus);
1565 if (submenus<0 && !menu->flags.buttoned &&
1566 menu->parent && menu->parent->flags.mapped)
1567 raiseMenus(menu->parent, submenus);
1571 WMenu*
1572 wMenuUnderPointer(WScreen *screen)
1574 WObjDescriptor *desc;
1575 Window root_ret, win;
1576 int dummy;
1577 unsigned int mask;
1579 XQueryPointer(dpy, screen->root_win, &root_ret, &win, &dummy, &dummy,
1580 &dummy, &dummy, &mask);
1582 if (win==None) return NULL;
1584 if (XFindContext(dpy, win, wWinContext, (XPointer *)&desc)==XCNOENT)
1585 return NULL;
1587 if (desc->parent_type == WCLASS_MENU)
1588 return (WMenu *)desc->parent;
1589 return NULL;
1595 static void
1596 getPointerPosition(WScreen *scr, int *x, int *y)
1598 Window root_ret, win;
1599 int wx, wy;
1600 unsigned int mask;
1602 XQueryPointer(dpy, scr->root_win, &root_ret, &win, x, y, &wx, &wy, &mask);
1606 static void
1607 getScrollAmount(WMenu *menu, int *hamount, int *vamount)
1609 WScreen *scr = menu->menu->screen_ptr;
1610 int menuX1 = menu->frame_x;
1611 int menuY1 = menu->frame_y;
1612 int menuX2 = menu->frame_x + MENUW(menu);
1613 int menuY2 = menu->frame_y + MENUH(menu);
1614 int screenW = scr->scr_width;
1615 int screenH = scr->scr_height;
1616 int xroot, yroot;
1618 *hamount = 0;
1619 *vamount = 0;
1621 getPointerPosition(scr, &xroot, &yroot);
1624 if (xroot <= 1 && menuX1 < 0) {
1625 /* scroll to the right */
1626 *hamount = WMIN(MENU_SCROLL_STEP, abs(menuX1));
1628 } else if (xroot >= screenW-2 && menuX2 > screenW-1) {
1629 /* scroll to the left */
1630 *hamount = WMIN(MENU_SCROLL_STEP, abs(menuX2-screenW-1));
1632 if (*hamount==0)
1633 *hamount = 1;
1635 *hamount = -*hamount;
1638 if (yroot <= 1 && menuY1 < 0) {
1639 /* scroll down */
1640 *vamount = WMIN(MENU_SCROLL_STEP, abs(menuY1));
1642 } else if (yroot >= screenH-2 && menuY2 > screenH-1) {
1643 /* scroll up */
1644 *vamount = WMIN(MENU_SCROLL_STEP, abs(menuY2-screenH-2));
1646 *vamount = -*vamount;
1651 static void
1652 dragScrollMenuCallback(void *data)
1654 WMenu *menu = (WMenu*)data;
1655 WScreen *scr = menu->menu->screen_ptr;
1656 WMenu *parent = parentMenu(menu);
1657 int hamount, vamount;
1658 int x, y;
1659 int newSelectedEntry;
1661 getScrollAmount(menu, &hamount, &vamount);
1664 if (hamount != 0 || vamount != 0) {
1665 wMenuMove(parent, parent->frame_x + hamount,
1666 parent->frame_y + vamount, True);
1667 if (findMenu(scr, &x, &y)) {
1668 newSelectedEntry = getEntryAt(menu, x, y);
1669 selectEntry(menu, newSelectedEntry);
1670 } else {
1671 /* Pointer fell outside of menu. If the selected entry is
1672 * not a submenu, unselect it */
1673 if (menu->selected_entry >= 0
1674 && menu->entries[menu->selected_entry]->cascade<0)
1675 selectEntry(menu, -1);
1676 newSelectedEntry = 0;
1679 /* paranoid check */
1680 if (newSelectedEntry >= 0) {
1681 /* keep scrolling */
1682 menu->timer = WMAddTimerHandler(MENU_SCROLL_DELAY,
1683 dragScrollMenuCallback, menu);
1684 } else {
1685 menu->timer = NULL;
1687 } else {
1688 /* don't need to scroll anymore */
1689 menu->timer = NULL;
1690 if (findMenu(scr, &x, &y)) {
1691 newSelectedEntry = getEntryAt(menu, x, y);
1692 selectEntry(menu, newSelectedEntry);
1698 static void
1699 scrollMenuCallback(void *data)
1701 WMenu *menu = (WMenu*)data;
1702 WMenu *parent = parentMenu(menu);
1703 int hamount = 0; /* amount to scroll */
1704 int vamount = 0;
1706 #ifdef VIRTUAL_DESKTOP
1707 /* don't scroll if it is in vdesk mode */
1708 if (!wPreferences.vedge_thickness)
1709 #endif
1710 getScrollAmount(menu, &hamount, &vamount);
1712 if (hamount != 0 || vamount != 0) {
1713 wMenuMove(parent, parent->frame_x + hamount,
1714 parent->frame_y + vamount, True);
1716 /* keep scrolling */
1717 menu->timer = WMAddTimerHandler(MENU_SCROLL_DELAY,
1718 scrollMenuCallback, menu);
1719 } else {
1720 /* don't need to scroll anymore */
1721 menu->timer = NULL;
1727 #define MENU_SCROLL_BORDER 5
1729 static int
1730 isPointNearBoder(WMenu *menu, int x, int y)
1732 int menuX1 = menu->frame_x;
1733 int menuY1 = menu->frame_y;
1734 int menuX2 = menu->frame_x + MENUW(menu);
1735 int menuY2 = menu->frame_y + MENUH(menu);
1736 int scrXe = menu->menu->screen_ptr->scr_width-1;
1737 int scrYe = menu->menu->screen_ptr->scr_height-1;
1738 int flag = 0;
1740 if (x >= menuX1 && x <= menuX2 && (y < MENU_SCROLL_BORDER
1741 || y > scrYe-MENU_SCROLL_BORDER))
1742 flag = 1;
1743 else if (y >= menuY1 && y <= menuY2 && (x < MENU_SCROLL_BORDER
1744 || x > scrXe-MENU_SCROLL_BORDER))
1745 flag = 1;
1747 return flag;
1751 typedef struct _delay {
1752 WWindow *wwin;
1753 WMenu *menu;
1754 int ox,oy;
1755 } _delay;
1758 static void
1759 _leaving(_delay *dl)
1761 wMenuMove(dl->menu, dl->ox, dl->oy, True);
1762 dl->menu->jump_back=NULL;
1763 dl->menu->menu->screen_ptr->flags.jump_back_pending = 0;
1764 wfree(dl);
1768 void
1769 wMenuScroll(WMenu *menu, XEvent *event)
1771 WMenu *smenu;
1772 WMenu *omenu = parentMenu(menu);
1773 WScreen *scr = menu->frame->screen_ptr;
1774 int done = 0;
1775 int jump_back = 0;
1776 int old_frame_x = omenu->frame_x;
1777 int old_frame_y = omenu->frame_y;
1778 XEvent ev;
1780 if (omenu->jump_back)
1781 WMDeleteTimerWithClientData(omenu->jump_back);
1784 if ((/*omenu->flags.buttoned &&*/ !wPreferences.wrap_menus)
1785 || omenu->flags.app_menu) {
1786 jump_back = 1;
1789 if (!wPreferences.wrap_menus)
1790 raiseMenus(omenu, True);
1791 else
1792 raiseMenus(menu, False);
1794 if (!menu->timer)
1795 scrollMenuCallback(menu);
1797 while(!done) {
1798 int x, y, on_border, on_x_edge, on_y_edge, on_title;
1800 WMNextEvent(dpy, &ev);
1801 switch (ev.type) {
1802 case EnterNotify:
1803 WMHandleEvent(&ev);
1804 case MotionNotify:
1805 x = (ev.type==MotionNotify) ? ev.xmotion.x_root : ev.xcrossing.x_root;
1806 y = (ev.type==MotionNotify) ? ev.xmotion.y_root : ev.xcrossing.y_root;
1808 /* on_border is != 0 if the pointer is between the menu
1809 * and the screen border and is close enough to the border */
1810 on_border = isPointNearBoder(menu, x, y);
1812 smenu = wMenuUnderPointer(scr);
1814 if ((smenu==NULL && !on_border) || (smenu && parentMenu(smenu)!=omenu)) {
1815 done = 1;
1816 break;
1819 on_x_edge = x <= 1 || x >= scr->scr_width - 2;
1820 on_y_edge = y <= 1 || y >= scr->scr_height - 2;
1821 on_border = on_x_edge || on_y_edge;
1823 if (!on_border && !jump_back) {
1824 done = 1;
1825 break;
1828 if (menu->timer && (smenu!=menu || (!on_y_edge && !on_x_edge))) {
1829 WMDeleteTimerHandler(menu->timer);
1830 menu->timer = NULL;
1833 if (smenu != NULL)
1834 menu = smenu;
1836 if (!menu->timer)
1837 scrollMenuCallback(menu);
1838 break;
1839 case ButtonPress:
1840 /* True if we push on title, or drag the omenu to other position */
1841 on_title = ev.xbutton.x_root >= omenu->frame_x &&
1842 ev.xbutton.x_root <= omenu->frame_x + MENUW(omenu) &&
1843 ev.xbutton.y_root >= omenu->frame_y &&
1844 ev.xbutton.y_root <= omenu->frame_y + omenu->frame->top_width;
1845 WMHandleEvent(&ev);
1846 smenu = wMenuUnderPointer(scr);
1847 if (smenu == NULL || (smenu && smenu->flags.buttoned && smenu != omenu))
1848 done = 1;
1849 else if (smenu==omenu && on_title) {
1850 jump_back = 0;
1851 done = 1;
1853 break;
1854 case KeyPress:
1855 done = 1;
1856 default:
1857 WMHandleEvent(&ev);
1858 break;
1862 if (menu->timer) {
1863 WMDeleteTimerHandler(menu->timer);
1864 menu->timer = NULL;
1867 if (jump_back) {
1868 _delay *delayer;
1869 if (!omenu->jump_back) {
1870 delayer=wmalloc(sizeof(_delay));
1871 delayer->menu=omenu;
1872 delayer->ox=old_frame_x;
1873 delayer->oy=old_frame_y;
1874 omenu->jump_back=delayer;
1875 scr->flags.jump_back_pending = 1;
1877 else delayer = omenu->jump_back;
1878 WMAddTimerHandler(MENU_JUMP_BACK_DELAY,(WMCallback*)_leaving, delayer);
1884 static void
1885 menuExpose(WObjDescriptor *desc, XEvent *event)
1887 wMenuPaint(desc->parent);
1890 typedef struct {
1891 int *delayed_select;
1892 WMenu *menu;
1893 WMHandlerID magic;
1894 } delay_data;
1897 static void
1898 delaySelection(void *data)
1900 delay_data *d = (delay_data*)data;
1901 int x, y, entry_no;
1902 WMenu *menu;
1904 d->magic = NULL;
1906 menu = findMenu(d->menu->menu->screen_ptr, &x, &y);
1907 if (menu && (d->menu == menu || d->delayed_select)) {
1908 entry_no = getEntryAt(menu, x, y);
1909 selectEntry(menu, entry_no);
1911 if (d->delayed_select)
1912 *(d->delayed_select) = 0;
1916 static void
1917 menuMouseDown(WObjDescriptor *desc, XEvent *event)
1919 XButtonEvent *bev = &event->xbutton;
1920 WMenu *menu = desc->parent;
1921 WMenu *smenu;
1922 WScreen *scr=menu->frame->screen_ptr;
1923 WMenuEntry *entry=NULL;
1924 XEvent ev;
1925 int close_on_exit=0;
1926 int done=0;
1927 int delayed_select = 0;
1928 int entry_no;
1929 int x, y;
1930 int prevx, prevy;
1931 int old_frame_x = 0;
1932 int old_frame_y = 0;
1933 delay_data d_data = {NULL, NULL, NULL};
1935 if (!wPreferences.wrap_menus) {
1936 smenu = parentMenu(menu);
1937 old_frame_x = smenu->frame_x;
1938 old_frame_y = smenu->frame_y;
1939 } else if (event->xbutton.window == menu->frame->core->window) {
1940 /* This is true if the menu was launched with right click on root window */
1941 delayed_select = 1;
1942 d_data.delayed_select = &delayed_select;
1943 d_data.menu = menu;
1944 d_data.magic = WMAddTimerHandler(wPreferences.dblclick_time,
1945 delaySelection, &d_data);
1948 if (menu->flags.inside_handler) {
1949 return;
1951 menu->flags.inside_handler = 1;
1953 wRaiseFrame(menu->frame->core);
1955 close_on_exit = (bev->send_event || menu->flags.brother);
1957 smenu = findMenu(scr, &x, &y);
1958 if (!smenu) {
1959 x = -1;
1960 y = -1;
1961 } else {
1962 menu = smenu;
1965 if (menu->flags.editing) {
1966 goto byebye;
1968 entry_no = getEntryAt(menu, x, y);
1969 if (entry_no>=0) {
1970 entry = menu->entries[entry_no];
1972 if (!close_on_exit && (bev->state & ControlMask) && smenu
1973 && entry->flags.editable) {
1974 editEntry(smenu, entry);
1975 goto byebye;
1976 } else if (bev->state & ControlMask) {
1977 goto byebye;
1980 if (entry->flags.enabled && entry->cascade>=0 && menu->cascades) {
1981 WMenu *submenu = menu->cascades[entry->cascade];
1982 /* map cascade */
1983 if (submenu->flags.mapped && !submenu->flags.buttoned &&
1984 menu->selected_entry!=entry_no) {
1985 wMenuUnmap(submenu);
1987 if (!submenu->flags.mapped && !delayed_select) {
1988 selectEntry(menu, entry_no);
1989 } else if (!submenu->flags.buttoned) {
1990 selectEntry(menu, -1);
1993 } else if (!delayed_select) {
1994 selectEntry(menu, entry_no);
1997 if (!wPreferences.wrap_menus && !wPreferences.scrollable_menus) {
1998 if (!menu->timer)
1999 dragScrollMenuCallback(menu);
2003 #ifdef VIRTUAL_DESKTOP
2004 if (wPreferences.vedge_thickness) {
2005 wWorkspaceLowerEdge(scr);
2007 #endif
2009 prevx = bev->x_root;
2010 prevy = bev->y_root;
2011 while (!done) {
2012 int x, y;
2014 XAllowEvents(dpy, AsyncPointer|SyncPointer, CurrentTime);
2016 WMMaskEvent(dpy, ExposureMask|ButtonMotionMask|ButtonReleaseMask
2017 |ButtonPressMask, &ev);
2018 switch (ev.type) {
2019 case MotionNotify:
2020 smenu = findMenu(scr, &x, &y);
2022 if (smenu == NULL) {
2023 /* moved mouse out of menu */
2025 if (!delayed_select && d_data.magic) {
2026 WMDeleteTimerHandler(d_data.magic);
2027 d_data.magic = NULL;
2029 if (menu==NULL
2030 || (menu->selected_entry>=0
2031 && menu->entries[menu->selected_entry]->cascade>=0)) {
2032 prevx = ev.xmotion.x_root;
2033 prevy = ev.xmotion.y_root;
2035 break;
2037 selectEntry(menu, -1);
2038 menu = smenu;
2039 prevx = ev.xmotion.x_root;
2040 prevy = ev.xmotion.y_root;
2041 break;
2042 } else if (menu && menu!=smenu
2043 && (menu->selected_entry<0
2044 || menu->entries[menu->selected_entry]->cascade<0)) {
2045 selectEntry(menu, -1);
2047 if (!delayed_select && d_data.magic) {
2048 WMDeleteTimerHandler(d_data.magic);
2049 d_data.magic = NULL;
2051 } else {
2053 /* hysteresis for item selection */
2055 /* check if the motion was to the side, indicating that
2056 * the user may want to cross to a submenu */
2057 if (!delayed_select && menu) {
2058 int dx;
2059 Bool moved_to_submenu;/* moved to direction of submenu */
2061 dx = abs(prevx - ev.xmotion.x_root);
2063 moved_to_submenu = False;
2064 if (dx > 0 /* if moved enough to the side */
2065 /* maybe a open submenu */
2066 && menu->selected_entry>=0
2067 /* moving to the right direction */
2068 && (wPreferences.align_menus
2069 || ev.xmotion.y_root >= prevy)) {
2070 int index;
2072 index = menu->entries[menu->selected_entry]->cascade;
2073 if (index>=0) {
2074 if (menu->cascades[index]->frame_x>menu->frame_x) {
2075 if (prevx < ev.xmotion.x_root)
2076 moved_to_submenu = True;
2077 } else {
2078 if (prevx > ev.xmotion.x_root)
2079 moved_to_submenu = True;
2085 if (menu != smenu) {
2086 if (d_data.magic) {
2087 WMDeleteTimerHandler(d_data.magic);
2089 d_data.magic = NULL;
2090 } else if (moved_to_submenu) {
2091 /* while we are moving, postpone the selection */
2092 if (d_data.magic) {
2093 WMDeleteTimerHandler(d_data.magic);
2095 d_data.delayed_select = NULL;
2096 d_data.menu = menu;
2097 d_data.magic = WMAddTimerHandler(MENU_SELECT_DELAY,
2098 delaySelection,
2099 &d_data);
2100 prevx = ev.xmotion.x_root;
2101 prevy = ev.xmotion.y_root;
2102 break;
2103 } else {
2104 if (d_data.magic)
2105 WMDeleteTimerHandler(d_data.magic);
2106 d_data.magic = NULL;
2110 prevx = ev.xmotion.x_root;
2111 prevy = ev.xmotion.y_root;
2112 if (menu!=smenu) {
2113 /* pointer crossed menus */
2114 if (menu && menu->timer) {
2115 WMDeleteTimerHandler(menu->timer);
2116 menu->timer = NULL;
2118 if (smenu)
2119 dragScrollMenuCallback(smenu);
2121 menu = smenu;
2122 if (!menu->timer)
2123 dragScrollMenuCallback(menu);
2125 if (!delayed_select) {
2126 entry_no = getEntryAt(menu, x, y);
2127 if (entry_no>=0) {
2128 entry = menu->entries[entry_no];
2129 if (entry->flags.enabled && entry->cascade>=0 &&
2130 menu->cascades) {
2131 WMenu *submenu = menu->cascades[entry->cascade];
2132 if (submenu->flags.mapped && !submenu->flags.buttoned
2133 && menu->selected_entry!=entry_no) {
2134 wMenuUnmap(submenu);
2138 selectEntry(menu, entry_no);
2140 break;
2142 case ButtonPress:
2143 break;
2145 case ButtonRelease:
2146 if (ev.xbutton.button == event->xbutton.button)
2147 done=1;
2148 break;
2150 case Expose:
2151 WMHandleEvent(&ev);
2152 #ifdef VIRTUAL_DESKTOP
2153 /* since expose will raise edge up.. I need another ugly hack here */
2154 if (wPreferences.vedge_thickness) {
2155 wWorkspaceLowerEdge(scr);
2157 #endif
2158 break;
2162 if (menu && menu->timer) {
2163 WMDeleteTimerHandler(menu->timer);
2164 menu->timer = NULL;
2166 if (d_data.magic!=NULL)
2167 WMDeleteTimerHandler(d_data.magic);
2169 if (menu && menu->selected_entry>=0) {
2170 entry = menu->entries[menu->selected_entry];
2171 if (entry->callback!=NULL && entry->flags.enabled
2172 && entry->cascade < 0) {
2173 /* blink and erase menu selection */
2174 #if (MENU_BLINK_DELAY > 0)
2175 int sel = menu->selected_entry;
2176 int i;
2178 for (i=0; i<MENU_BLINK_COUNT; i++) {
2179 paintEntry(menu, sel, False);
2180 XSync(dpy, 0);
2181 wusleep(MENU_BLINK_DELAY);
2182 paintEntry(menu, sel, True);
2183 XSync(dpy, 0);
2184 wusleep(MENU_BLINK_DELAY);
2186 #endif
2187 /* unmap the menu, it's parents and call the callback */
2188 if (!menu->flags.buttoned &&
2189 (!menu->flags.app_menu||menu->parent!=NULL)) {
2190 closeCascade(menu);
2191 } else {
2192 selectEntry(menu, -1);
2194 (*entry->callback)(menu, entry);
2196 /* If the user double clicks an entry, the entry will
2197 * be executed twice, which is not good for things like
2198 * the root menu. So, ignore any clicks that were generated
2199 * while the entry was being executed */
2200 while (XCheckTypedWindowEvent(dpy, menu->menu->window,
2201 ButtonPress, &ev));
2202 } else if (entry->callback!=NULL && entry->cascade<0) {
2203 selectEntry(menu, -1);
2204 } else {
2205 if (entry->cascade>=0 && menu->cascades
2206 && menu->cascades[entry->cascade]->flags.brother) {
2207 selectEntry(menu, -1);
2212 if (((WMenu*)desc->parent)->flags.brother || close_on_exit || !smenu)
2213 closeCascade(desc->parent);
2215 /* close the cascade windows that should not remain opened */
2216 closeBrotherCascadesOf(desc->parent);
2218 if (!wPreferences.wrap_menus)
2219 wMenuMove(parentMenu(desc->parent), old_frame_x, old_frame_y, True);
2221 byebye:
2222 ((WMenu*)desc->parent)->flags.inside_handler = 0;
2223 #ifdef VIRTUAL_DESKTOP
2224 if (wPreferences.vedge_thickness) {
2225 wWorkspaceRaiseEdge(scr);
2227 #endif
2231 void
2232 wMenuMove(WMenu *menu, int x, int y, int submenus)
2234 WMenu *submenu;
2235 int i;
2237 if (!menu) return;
2239 menu->frame_x = x;
2240 menu->frame_y = y;
2241 XMoveWindow(dpy, menu->frame->core->window, x, y);
2243 if (submenus>0 && menu->selected_entry>=0) {
2244 i = menu->entries[menu->selected_entry]->cascade;
2246 if (i>=0 && menu->cascades) {
2247 submenu = menu->cascades[i];
2248 if (submenu->flags.mapped && !submenu->flags.buttoned) {
2249 if (wPreferences.align_menus) {
2250 wMenuMove(submenu, x + MENUW(menu), y, submenus);
2251 } else {
2252 wMenuMove(submenu, x+ MENUW(menu),
2253 y + submenu->entry_height*menu->selected_entry,
2254 submenus);
2259 if (submenus<0 && menu->parent!=NULL && menu->parent->flags.mapped &&
2260 !menu->parent->flags.buttoned) {
2261 if (wPreferences.align_menus) {
2262 wMenuMove(menu->parent, x - MENUW(menu->parent), y, submenus);
2263 } else {
2264 wMenuMove(menu->parent, x - MENUW(menu->parent), menu->frame_y
2265 - menu->parent->entry_height*menu->parent->selected_entry,
2266 submenus);
2272 static void
2273 changeMenuLevels(WMenu *menu, int lower)
2275 int i;
2277 if (!lower) {
2278 ChangeStackingLevel(menu->frame->core, (!menu->parent ? WMMainMenuLevel
2279 : WMSubmenuLevel));
2280 wRaiseFrame(menu->frame->core);
2281 menu->flags.lowered = 0;
2282 } else {
2283 ChangeStackingLevel(menu->frame->core, WMNormalLevel);
2284 wLowerFrame(menu->frame->core);
2285 menu->flags.lowered = 1;
2287 for (i=0; i<menu->cascade_no; i++) {
2288 if (menu->cascades[i]
2289 && !menu->cascades[i]->flags.buttoned
2290 && menu->cascades[i]->flags.lowered!=lower) {
2291 changeMenuLevels(menu->cascades[i], lower);
2298 static void
2299 menuTitleDoubleClick(WCoreWindow *sender, void *data, XEvent *event)
2301 WMenu *menu = data;
2302 int lower;
2304 if (event->xbutton.state & MOD_MASK) {
2305 if (menu->flags.lowered) {
2306 lower = 0;
2307 } else {
2308 lower = 1;
2310 changeMenuLevels(menu, lower);
2315 static void
2316 menuTitleMouseDown(WCoreWindow *sender, void *data, XEvent *event)
2318 WMenu *menu = data;
2319 WMenu *tmp;
2320 XEvent ev;
2321 int x=menu->frame_x, y=menu->frame_y;
2322 int dx=event->xbutton.x_root, dy=event->xbutton.y_root;
2323 int i, lower;
2324 Bool started;
2326 /* can't touch the menu copy */
2327 if (menu->flags.brother)
2328 return;
2330 if (event->xbutton.button != Button1 && event->xbutton.button != Button2)
2331 return;
2333 if (event->xbutton.state & MOD_MASK) {
2334 wLowerFrame(menu->frame->core);
2335 lower = 1;
2336 } else {
2337 wRaiseFrame(menu->frame->core);
2338 lower = 0;
2340 tmp = menu;
2342 /* lower/raise all submenus */
2343 while (1) {
2344 if (tmp->selected_entry>=0 && tmp->cascades
2345 && tmp->entries[tmp->selected_entry]->cascade>=0) {
2346 tmp = tmp->cascades[tmp->entries[tmp->selected_entry]->cascade];
2347 if (!tmp || !tmp->flags.mapped)
2348 break;
2349 if (lower)
2350 wLowerFrame(tmp->frame->core);
2351 else
2352 wRaiseFrame(tmp->frame->core);
2353 } else {
2354 break;
2358 /* tear off the menu if it's a root menu or a cascade
2359 application menu */
2360 if (!menu->flags.buttoned && !menu->flags.brother
2361 && (!menu->flags.app_menu||menu->parent!=NULL)) {
2362 menu->flags.buttoned=1;
2363 wFrameWindowShowButton(menu->frame, WFF_RIGHT_BUTTON);
2364 if (menu->parent) {
2365 /* turn off selected menu entry in parent menu */
2366 selectEntry(menu->parent, -1);
2368 /* make parent map the copy in place of the original */
2369 for (i=0; i<menu->parent->cascade_no; i++) {
2370 if (menu->parent->cascades[i] == menu) {
2371 menu->parent->cascades[i] = menu->brother;
2372 break;
2378 started = False;
2379 while(1) {
2380 WMMaskEvent(dpy, ButtonMotionMask|ButtonReleaseMask|ButtonPressMask
2381 |ExposureMask, &ev);
2382 switch (ev.type) {
2383 case MotionNotify:
2384 if (started) {
2385 x += ev.xmotion.x_root - dx;
2386 y += ev.xmotion.y_root - dy;
2387 dx = ev.xmotion.x_root;
2388 dy = ev.xmotion.y_root;
2389 wMenuMove(menu, x, y, True);
2390 } else {
2391 if (abs(ev.xmotion.x_root - dx) > MOVE_THRESHOLD
2392 || abs(ev.xmotion.y_root - dy) > MOVE_THRESHOLD) {
2393 started = True;
2394 XGrabPointer(dpy, menu->frame->titlebar->window, False,
2395 ButtonMotionMask|ButtonReleaseMask
2396 |ButtonPressMask,
2397 GrabModeAsync, GrabModeAsync, None,
2398 wCursor[WCUR_MOVE], CurrentTime);
2401 break;
2403 case ButtonPress:
2404 break;
2406 case ButtonRelease:
2407 if (ev.xbutton.button != event->xbutton.button)
2408 break;
2409 XUngrabPointer(dpy, CurrentTime);
2410 return;
2412 default:
2413 WMHandleEvent(&ev);
2414 break;
2420 *----------------------------------------------------------------------
2421 * menuCloseClick--
2422 * Handles mouse click on the close button of menus. The menu is
2423 * closed when the button is clicked.
2425 * Side effects:
2426 * The closed menu is reinserted at it's parent menus
2427 * cascade list.
2428 *----------------------------------------------------------------------
2430 static void
2431 menuCloseClick(WCoreWindow *sender, void *data, XEvent *event)
2433 WMenu *menu = (WMenu*)data;
2434 WMenu *parent = menu->parent;
2435 int i;
2437 if (parent) {
2438 for (i=0; i<parent->cascade_no; i++) {
2439 /* find the entry that points to the copy */
2440 if (parent->cascades[i] == menu->brother) {
2441 /* make it point to the original */
2442 parent->cascades[i] = menu;
2443 menu->parent = parent;
2444 break;
2448 wMenuUnmap(menu);
2452 static void
2453 saveMenuInfo(proplist_t dict, WMenu *menu, proplist_t key)
2455 proplist_t value, list;
2456 char buffer[256];
2458 sprintf(buffer, "%i,%i", menu->frame_x, menu->frame_y);
2459 value = PLMakeString(buffer);
2460 list = PLMakeArrayFromElements(value, NULL);
2461 if (menu->flags.lowered)
2462 PLAppendArrayElement(list, PLMakeString("lowered"));
2463 PLInsertDictionaryEntry(dict, key, list);
2464 PLRelease(value);
2465 PLRelease(list);
2469 void
2470 wMenuSaveState(WScreen *scr)
2472 proplist_t menus, key;
2473 int save_menus = 0;
2475 menus = PLMakeDictionaryFromEntries(NULL, NULL, NULL);
2477 #ifndef LITE
2478 if (scr->switch_menu && scr->switch_menu->flags.buttoned) {
2479 key = PLMakeString("SwitchMenu");
2480 saveMenuInfo(menus, scr->switch_menu, key);
2481 PLRelease(key);
2482 save_menus = 1;
2485 if (saveMenuRecurs(menus, scr, scr->root_menu))
2486 save_menus = 1;
2488 #endif /* !LITE */
2489 if (scr->workspace_menu && scr->workspace_menu->flags.buttoned) {
2490 key = PLMakeString("WorkspaceMenu");
2491 saveMenuInfo(menus, scr->workspace_menu, key);
2492 PLRelease(key);
2493 save_menus = 1;
2496 if (save_menus) {
2497 key = PLMakeString("Menus");
2498 PLInsertDictionaryEntry(scr->session_state, key, menus);
2499 PLRelease(key);
2501 PLRelease(menus);
2505 #ifndef LITE
2507 static Bool
2508 getMenuPath(WMenu *menu, char *buffer, int bufSize)
2510 Bool ok = True;
2511 int len = 0;
2513 if (!menu->flags.titled || !menu->frame->title[0])
2514 return False;
2516 len = strlen(menu->frame->title);
2517 if (len >= bufSize)
2518 return False;
2520 if (menu->parent) {
2521 ok = getMenuPath(menu->parent, buffer, bufSize - len - 1);
2522 if (!ok)
2523 return False;
2526 strcat(buffer, "\\");
2527 strcat(buffer, menu->frame->title);
2529 return True;
2533 static Bool
2534 saveMenuRecurs(proplist_t menus, WScreen *scr, WMenu *menu)
2536 proplist_t key;
2537 int save_menus = 0, i;
2538 char buffer[512];
2539 Bool ok = True;
2542 if (menu->flags.brother)
2543 menu = menu->brother;
2545 if (menu->flags.buttoned && menu != scr->switch_menu) {
2547 buffer[0] = '\0';
2548 ok = getMenuPath(menu, buffer, 510);
2550 if (ok) {
2551 key = PLMakeString(buffer);
2552 saveMenuInfo(menus, menu, key);
2553 PLRelease(key);
2554 save_menus = 1;
2558 if (ok) {
2559 for (i = 0; i < menu->cascade_no; i++) {
2560 if (saveMenuRecurs(menus, scr, menu->cascades[i]))
2561 save_menus = 1;
2564 return save_menus;
2566 #endif /* !LITE */
2569 #define COMPLAIN(key) wwarning(_("bad value in menus state info:%s"), key)
2572 static Bool
2573 getMenuInfo(proplist_t info, int *x, int *y, Bool *lowered)
2575 proplist_t pos;
2577 *lowered = False;
2579 if (PLIsArray(info)) {
2580 proplist_t flags;
2581 pos = PLGetArrayElement(info, 0);
2582 flags = PLGetArrayElement(info, 1);
2583 if (flags != NULL && PLIsString(flags) && PLGetString(flags) != NULL
2584 && strcmp(PLGetString(flags), "lowered") == 0) {
2585 *lowered = True;
2587 } else {
2588 pos = info;
2591 if (pos != NULL && PLIsString(pos)) {
2592 if (sscanf(PLGetString(pos), "%i,%i", x, y)!=2)
2593 COMPLAIN("Position");
2594 } else {
2595 COMPLAIN("(position, flags...)");
2596 return False;
2599 return True;
2603 static int
2604 restoreMenu(WScreen *scr, proplist_t menu, int which)
2606 int x, y;
2607 Bool lowered = False;
2608 WMenu *pmenu = NULL;
2610 if (!menu)
2611 return False;
2613 if (!getMenuInfo(menu, &x, &y, &lowered))
2614 return False;
2617 #ifndef LITE
2618 if (which & WSS_SWITCHMENU) {
2619 OpenSwitchMenu(scr, x, y, False);
2620 pmenu = scr->switch_menu;
2622 #endif /* !LITE */
2624 if (pmenu) {
2625 int width = MENUW(pmenu);
2626 int height = MENUH(pmenu);
2628 if (lowered) {
2629 changeMenuLevels(pmenu, True);
2632 x = (x < -width) ? 0 : x;
2633 x = (x > scr->scr_width) ? scr->scr_width - width : x;
2634 y = (y < 0) ? 0 : y;
2635 y = (y > scr->scr_height) ? scr->scr_height - height : y;
2636 wMenuMove(pmenu, x, y, True);
2637 pmenu->flags.buttoned = 1;
2638 wFrameWindowShowButton(pmenu->frame, WFF_RIGHT_BUTTON);
2639 return True;
2641 return False;
2645 #ifndef LITE
2646 static int
2647 restoreMenuRecurs(WScreen *scr, proplist_t menus, WMenu *menu, char *path)
2649 proplist_t key, entry;
2650 char buffer[512];
2651 int i, x, y, res;
2652 Bool lowered;
2654 if (strlen(path) + strlen(menu->frame->title) > 510)
2655 return False;
2657 sprintf(buffer, "%s\\%s", path, menu->frame->title);
2658 key = PLMakeString(buffer);
2659 entry = PLGetDictionaryEntry(menus, key);
2660 res = False;
2662 if (entry && getMenuInfo(entry, &x, &y, &lowered)) {
2664 if (!menu->flags.mapped) {
2665 int width = MENUW(menu);
2666 int height = MENUH(menu);
2668 wMenuMapAt(menu, x, y, False);
2670 if (menu->parent) {
2671 /* make parent map the copy in place of the original */
2672 for (i=0; i<menu->parent->cascade_no; i++) {
2673 if (menu->parent->cascades[i] == menu) {
2674 menu->parent->cascades[i] = menu->brother;
2675 break;
2679 if (lowered) {
2680 changeMenuLevels(menu, True);
2682 x = (x < -width) ? 0 : x;
2683 x = (x > scr->scr_width) ? scr->scr_width - width : x;
2684 y = (y < 0) ? 0 : y;
2685 y = (y > scr->scr_height) ? scr->scr_height - height : y;
2686 wMenuMove(menu, x, y, True);
2687 menu->flags.buttoned = 1;
2688 wFrameWindowShowButton(menu->frame, WFF_RIGHT_BUTTON);
2689 res = True;
2693 PLRelease(key);
2695 for (i=0; i<menu->cascade_no; i++) {
2696 if (restoreMenuRecurs(scr, menus, menu->cascades[i], buffer) != False)
2697 res = True;
2700 return res;
2702 #endif /* !LITE */
2705 void
2706 wMenuRestoreState(WScreen *scr)
2708 proplist_t menus, menu, key, skey;
2710 key = PLMakeString("Menus");
2711 menus = PLGetDictionaryEntry(scr->session_state, key);
2712 PLRelease(key);
2714 if (!menus)
2715 return;
2717 /* restore menus */
2719 skey = PLMakeString("SwitchMenu");
2720 menu = PLGetDictionaryEntry(menus, skey);
2721 PLRelease(skey);
2722 restoreMenu(scr, menu, WSS_SWITCHMENU);
2724 #ifndef LITE
2725 if (!scr->root_menu) {
2726 OpenRootMenu(scr, scr->scr_width*2, 0, False);
2727 wMenuUnmap(scr->root_menu);
2729 restoreMenuRecurs(scr, menus, scr->root_menu, "");
2730 #endif /* !LITE */
2734 void
2735 OpenWorkspaceMenu(WScreen *scr, int x, int y)
2737 WMenu *menu, *parent;
2738 WMenuEntry *entry;
2740 #ifndef LITE
2741 if (!scr->root_menu) {
2742 OpenRootMenu(scr, scr->scr_width*2, 0, False);
2743 wMenuUnmap(scr->root_menu);
2745 #endif
2746 menu = scr->workspace_menu;
2747 if (menu) {
2748 if (menu->flags.mapped) {
2749 if (!menu->flags.buttoned) {
2750 wMenuUnmap(menu);
2751 parent = menu->parent;
2752 if (parent && parent->selected_entry >= 0) {
2753 entry = parent->entries[parent->selected_entry];
2754 if (parent->cascades[entry->cascade] == menu) {
2755 selectEntry(parent, -1);
2756 wMenuMapAt(menu, x, y, False);
2759 } else {
2760 wRaiseFrame(menu->frame->core);
2761 wMenuMapCopyAt(menu, x, y);
2763 } else {
2764 wMenuMapAt(menu, x, y, False);