fixed brothr menu texture update bug
[wmaker-crm.git] / src / menu.c
blob6cd96fdd651fbe3b21ccd89bb7ab0650007fed35
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>
35 #include "WindowMaker.h"
36 #include "wcore.h"
37 #include "framewin.h"
38 #include "menu.h"
39 #include "actions.h"
40 #include "funcs.h"
41 #include "stacking.h"
42 #include "text.h"
45 /****** Global Variables ******/
47 extern Cursor wCursor[WCUR_LAST];
49 extern XContext wWinContext;
51 extern WPreferences wPreferences;
53 #define MOD_MASK wPreferences.modifier_mask
55 #define MENU_SCROLL_STEP menuScrollParameters[(int)wPreferences.menu_scroll_speed].steps
56 #define MENU_SCROLL_DELAY menuScrollParameters[(int)wPreferences.menu_scroll_speed].delay
60 #define MENUW(m) ((m)->frame->core->width+2*FRAME_BORDER_WIDTH)
61 #define MENUH(m) ((m)->frame->core->height+2*FRAME_BORDER_WIDTH)
64 /***** Local Stuff ******/
66 static struct {
67 int steps;
68 int delay;
69 } menuScrollParameters[5] = {
70 {MENU_SCROLL_STEPS_UF, MENU_SCROLL_DELAY_UF},
71 {MENU_SCROLL_STEPS_F, MENU_SCROLL_DELAY_F},
72 {MENU_SCROLL_STEPS_M, MENU_SCROLL_DELAY_M},
73 {MENU_SCROLL_STEPS_S, MENU_SCROLL_DELAY_S},
74 {MENU_SCROLL_STEPS_U, MENU_SCROLL_DELAY_U}};
77 static void menuMouseDown(WObjDescriptor *desc, XEvent *event);
78 static void menuExpose(WObjDescriptor *desc, XEvent *event);
80 static void menuTitleDoubleClick(WCoreWindow *sender, void *data, XEvent *event);
81 static void menuTitleMouseDown(WCoreWindow *sender, void *data, XEvent *event);
83 static void menuCloseClick(WCoreWindow *sender, void *data, XEvent *event);
85 static void updateTexture(WMenu *menu);
87 #ifndef LITE
88 static int saveMenuRecurs(proplist_t menus, WScreen *scr, WMenu *menu);
89 static int restoreMenuRecurs(WScreen *scr, proplist_t menus, WMenu *menu, char *path);
90 #endif /* !LITE */
92 static void selectEntry(WMenu *menu, int entry_no);
93 static void closeCascade(WMenu *menu);
96 /****** Notification Observers ******/
98 static void
99 appearanceObserver(void *self, WMNotification *notif)
101 WMenu *menu = (WMenu*)self;
102 int flags = (int)WMGetNotificationClientData(notif);
104 if (!menu->flags.realized)
105 return;
107 if (WMGetNotificationName(notif) == WNMenuAppearanceSettingsChanged) {
108 if (flags & WFontSettings) {
109 menu->flags.realized = 0;
110 wMenuRealize(menu);
112 if (flags & WTextureSettings) {
113 if (!menu->flags.brother)
114 updateTexture(menu);
116 if (flags & (WTextureSettings|WColorSettings)) {
117 wMenuPaint(menu);
119 } else if (menu->flags.titled) {
121 if (flags & WFontSettings) {
122 menu->flags.realized = 0;
123 wMenuRealize(menu);
125 if (flags & WTextureSettings) {
126 menu->frame->flags.need_texture_remake = 1;
128 if (flags & (WColorSettings|WTextureSettings)) {
129 wFrameWindowPaint(menu->frame);
134 /************************************/
138 *----------------------------------------------------------------------
139 * wMenuCreate--
140 * Creates a new empty menu with the specified title. If main_menu
141 * is True, the created menu will be a main menu, which has some special
142 * properties such as being placed over other normal menus.
143 * If title is NULL, the menu will have no titlebar.
145 * Returns:
146 * The created menu.
147 *----------------------------------------------------------------------
149 WMenu*
150 wMenuCreate(WScreen *screen, char *title, int main_menu)
152 WMenu *menu;
153 static int brother=0;
154 int tmp, flags;
156 menu = wmalloc(sizeof(WMenu));
158 memset(menu, 0, sizeof(WMenu));
160 #ifdef SINGLE_MENULEVEL
161 tmp = WMSubmenuLevel;
162 #else
163 tmp = (main_menu ? WMMainMenuLevel : WMSubmenuLevel);
164 #endif
166 flags = WFF_SINGLE_STATE;
167 if (title) {
168 flags |= WFF_TITLEBAR|WFF_RIGHT_BUTTON;
169 menu->flags.titled = 1;
171 menu->frame =
172 wFrameWindowCreate(screen, tmp, 8, 2, 1, 1, flags,
173 screen->menu_title_texture, NULL,
174 screen->menu_title_pixel, &screen->menu_title_gc,
175 &screen->menu_title_font);
177 menu->frame->core->descriptor.parent = menu;
178 menu->frame->core->descriptor.parent_type = WCLASS_MENU;
179 menu->frame->core->descriptor.handle_mousedown = menuMouseDown;
181 wFrameWindowHideButton(menu->frame, WFF_RIGHT_BUTTON);
183 if (title) {
184 menu->frame->title = wstrdup(title);
187 menu->frame->flags.justification = WTJ_LEFT;
189 menu->frame->rbutton_image = screen->b_pixmaps[WBUT_CLOSE];
191 menu->entry_no = 0;
192 menu->alloced_entries = 0;
193 menu->selected_entry = -1;
194 menu->entries = NULL;
196 menu->frame_x = screen->app_menu_x;
197 menu->frame_y = screen->app_menu_y;
199 menu->frame->child = menu;
201 menu->flags.lowered = 0;
203 /* create borders */
204 if (title) {
205 /* setup object descriptors */
206 menu->frame->on_mousedown_titlebar = menuTitleMouseDown;
207 menu->frame->on_dblclick_titlebar = menuTitleDoubleClick;
210 menu->frame->on_click_right = menuCloseClick;
213 menu->menu = wCoreCreate(menu->frame->core, 0, menu->frame->top_width,
214 menu->frame->core->width, 10);
216 menu->menu->descriptor.parent = menu;
217 menu->menu->descriptor.parent_type = WCLASS_MENU;
218 menu->menu->descriptor.handle_expose = menuExpose;
219 menu->menu->descriptor.handle_mousedown = menuMouseDown;
221 menu->menu_texture_data = None;
223 XMapWindow(dpy, menu->menu->window);
225 XFlush(dpy);
227 if (!brother) {
228 brother = 1;
229 menu->brother = wMenuCreate(screen, title, main_menu);
230 brother = 0;
231 menu->brother->flags.brother = 1;
232 menu->brother->brother = menu;
234 WMAddNotificationObserver(appearanceObserver, menu,
235 WNMenuAppearanceSettingsChanged, menu);
237 WMAddNotificationObserver(appearanceObserver, menu,
238 WNMenuTitleAppearanceSettingsChanged, menu);
241 return menu;
247 WMenu*
248 wMenuCreateForApp(WScreen *screen, char *title, int main_menu)
250 WMenu *menu;
252 menu = wMenuCreate(screen, title, main_menu);
253 if (!menu)
254 return NULL;
255 menu->flags.app_menu = 1;
256 menu->brother->flags.app_menu = 1;
258 return menu;
263 static void
264 insertEntry(WMenu *menu, WMenuEntry *entry, int index)
266 int i;
268 for (i = menu->entry_no-1; i >= index; i--) {
269 menu->entries[i]->order++;
270 menu->entries[i+1] = menu->entries[i];
272 menu->entries[index] = entry;
276 WMenuEntry*
277 wMenuInsertCallback(WMenu *menu, int index, char *text,
278 void (*callback)(WMenu *menu, WMenuEntry *entry),
279 void *clientdata)
281 WMenuEntry *entry;
283 #ifdef DEBUG
284 if (!menu) {
285 printf("Passed NULL as menu parameter to wMenuAddCallback() \n");
286 return NULL;
288 #endif
290 assert(menu->flags.brother==0);
291 menu->flags.realized = 0;
292 menu->brother->flags.realized = 0;
294 /* reallocate array if it's too small */
295 if (menu->entry_no >= menu->alloced_entries) {
296 void *tmp;
297 #ifdef DEBUG
298 puts("doing wrealloc()");
299 #endif
300 tmp = wrealloc(menu->entries,
301 sizeof(WMenuEntry)*(menu->alloced_entries+5));
302 if (tmp==NULL) {
303 wwarning(_("wrealloc() failed while trying to add menu item"));
304 return NULL;
307 menu->entries = tmp;
308 menu->alloced_entries += 5;
310 menu->brother->entries = tmp;
311 menu->brother->alloced_entries = menu->alloced_entries;
313 entry = wmalloc(sizeof(WMenuEntry));
314 memset(entry, 0, sizeof(WMenuEntry));
315 entry->flags.enabled = 1;
316 entry->text = wstrdup(text);
317 entry->cascade = -1;
318 entry->clientdata = clientdata;
319 entry->callback = callback;
320 if (index<0 || index>=menu->entry_no) {
321 entry->order = menu->entry_no;
322 menu->entries[menu->entry_no] = entry;
323 } else {
324 entry->order = index;
325 insertEntry(menu, entry, index);
328 menu->entry_no++;
329 menu->brother->entry_no = menu->entry_no;
331 return entry;
336 void
337 wMenuEntrySetCascade(WMenu *menu, WMenuEntry *entry, WMenu *cascade)
339 WMenu *brother = menu->brother;
340 int i, done;
342 assert(menu->flags.brother==0);
344 if (entry->cascade>=0) {
345 menu->flags.realized = 0;
346 brother->flags.realized = 0;
349 cascade->parent = menu;
351 cascade->brother->parent = brother;
353 done = 0;
354 for (i=0; i<menu->cascade_no; i++) {
355 if (menu->cascades[i]==NULL) {
356 menu->cascades[i] = cascade;
357 brother->cascades[i] = cascade->brother;
358 done = 1;
359 entry->cascade = i;
360 break;
363 if (!done) {
364 entry->cascade = menu->cascade_no;
366 menu->cascades = wrealloc(menu->cascades,
367 sizeof(WMenu)*(menu->cascade_no+1));
368 menu->cascades[menu->cascade_no++] = cascade;
371 brother->cascades = wrealloc(brother->cascades,
372 sizeof(WMenu)*(brother->cascade_no+1));
373 brother->cascades[brother->cascade_no++] = cascade->brother;
377 if (menu->flags.lowered) {
379 cascade->flags.lowered = 1;
380 ChangeStackingLevel(cascade->frame->core, WMNormalLevel);
382 cascade->brother->flags.lowered = 1;
383 ChangeStackingLevel(cascade->brother->frame->core, WMNormalLevel);
386 if (!menu->flags.realized)
387 wMenuRealize(menu);
391 void
392 wMenuEntryRemoveCascade(WMenu *menu, WMenuEntry *entry)
394 assert(menu->flags.brother==0);
396 /* destroy cascade menu */
397 if (entry->cascade>=0 && menu->cascades
398 && menu->cascades[entry->cascade]!=NULL) {
400 wMenuDestroy(menu->cascades[entry->cascade], True);
402 menu->cascades[entry->cascade] = NULL;
403 menu->brother->cascades[entry->cascade] = NULL;
405 entry->cascade = -1;
410 void
411 wMenuRemoveItem(WMenu *menu, int index)
413 int i;
415 if (menu->flags.brother) {
416 wMenuRemoveItem(menu->brother, index);
417 return;
420 if (index>=menu->entry_no) return;
422 /* destroy cascade menu */
423 wMenuEntryRemoveCascade(menu, menu->entries[index]);
425 /* destroy unshared data */
427 if (menu->entries[index]->text)
428 free(menu->entries[index]->text);
430 if (menu->entries[index]->rtext)
431 free(menu->entries[index]->rtext);
433 if (menu->entries[index]->free_cdata && menu->entries[index]->clientdata)
434 (*menu->entries[index]->free_cdata)(menu->entries[index]->clientdata);
436 free(menu->entries[index]);
438 for (i=index; i<menu->entry_no-1; i++) {
439 menu->entries[i+1]->order--;
440 menu->entries[i]=menu->entries[i+1];
442 menu->entry_no--;
443 menu->brother->entry_no--;
447 static Pixmap
448 renderTexture(WMenu *menu)
450 RImage *img;
451 Pixmap pix;
452 int i;
453 RColor light;
454 RColor dark;
455 RColor mid;
456 WScreen *scr = menu->menu->screen_ptr;
457 WTexture *texture = scr->menu_item_texture;
459 if (wPreferences.menu_style == MS_NORMAL) {
460 img = wTextureRenderImage(texture, menu->menu->width,
461 menu->entry_height, WREL_MENUENTRY);
462 } else {
463 img = wTextureRenderImage(texture, menu->menu->width,
464 menu->menu->height+1, WREL_MENUENTRY);
466 if (!img) {
467 wwarning(_("could not render texture: %s"),
468 RMessageForError(RErrorCode));
470 return None;
473 if (wPreferences.menu_style == MS_SINGLE_TEXTURE) {
474 light.alpha = 0;
475 light.red = light.green = light.blue = 80;
477 dark.alpha = 255;
478 dark.red = dark.green = dark.blue = 0;
480 mid.alpha = 0;
481 mid.red = mid.green = mid.blue = 40;
483 for (i = 1; i < menu->entry_no; i++) {
484 ROperateLine(img, RSubtractOperation, 0, i*menu->entry_height-2,
485 menu->menu->width-1, i*menu->entry_height-2, &mid);
487 RDrawLine(img, 0, i*menu->entry_height-1,
488 menu->menu->width-1, i*menu->entry_height-1, &dark);
490 ROperateLine(img, RAddOperation, 0, i*menu->entry_height,
491 menu->menu->width-1, i*menu->entry_height,
492 &light);
495 if (!RConvertImage(scr->rcontext, img, &pix)) {
496 wwarning(_("error rendering image:%s"), RMessageForError(RErrorCode));
498 RDestroyImage(img);
500 return pix;
504 static void
505 updateTexture(WMenu *menu)
507 WScreen *scr = menu->menu->screen_ptr;
509 /* setup background texture */
510 if (scr->menu_item_texture->any.type != WTEX_SOLID) {
511 if (!menu->flags.brother) {
512 FREE_PIXMAP(menu->menu_texture_data);
514 menu->menu_texture_data = renderTexture(menu);
516 XSetWindowBackgroundPixmap(dpy, menu->menu->window,
517 menu->menu_texture_data);
518 XClearWindow(dpy, menu->menu->window);
520 XSetWindowBackgroundPixmap(dpy, menu->brother->menu->window,
521 menu->menu_texture_data);
522 XClearWindow(dpy, menu->brother->menu->window);
524 } else {
525 XSetWindowBackground(dpy, menu->menu->window,
526 scr->menu_item_texture->any.color.pixel);
527 XClearWindow(dpy, menu->menu->window);
532 void
533 wMenuRealize(WMenu *menu)
535 int i;
536 int width, rwidth, mrwidth, mwidth;
537 int theight, twidth, eheight;
538 WScreen *scr = menu->frame->screen_ptr;
539 static int brother_done=0;
540 int flags;
542 if (!brother_done) {
543 brother_done = 1;
544 wMenuRealize(menu->brother);
545 brother_done = 0;
548 flags = WFF_SINGLE_STATE;
549 if (menu->flags.titled)
550 flags |= WFF_TITLEBAR|WFF_RIGHT_BUTTON;
552 wFrameWindowUpdateBorders(menu->frame, flags);
554 if (menu->flags.titled) {
555 twidth = wTextWidth(scr->menu_title_font->font, menu->frame->title,
556 strlen(menu->frame->title));
557 theight = menu->frame->top_width;
558 twidth += theight + (wPreferences.new_style ? 16 : 8);
559 } else {
560 twidth = 0;
561 theight = 0;
563 eheight = scr->menu_entry_font->height + 6;
564 menu->entry_height = eheight;
565 mrwidth = 0;
566 mwidth = 0;
567 for (i=0; i<menu->entry_no; i++) {
568 char *text;
570 /* search widest text */
571 text = menu->entries[i]->text;
572 width = wTextWidth(scr->menu_entry_font->font, text, strlen(text))+10;
574 if (menu->entries[i]->flags.indicator) {
575 width += MENU_INDICATOR_SPACE;
578 if (width > mwidth)
579 mwidth = width;
581 /* search widest text on right */
582 text = menu->entries[i]->rtext;
583 if (text)
584 rwidth = wTextWidth(scr->menu_entry_font->font, text,
585 strlen(text)) + 5;
586 else if (menu->entries[i]->cascade>=0)
587 rwidth = 16;
588 else
589 rwidth = 4;
591 if (rwidth > mrwidth)
592 mrwidth = rwidth;
594 mwidth += mrwidth;
596 if (mwidth < twidth)
597 mwidth = twidth;
600 wCoreConfigure(menu->menu, 0, theight, mwidth, menu->entry_no*eheight -1);
602 wFrameWindowResize(menu->frame, mwidth, menu->entry_no*eheight-1
603 + menu->frame->top_width + menu->frame->bottom_width);
606 updateTexture(menu);
608 menu->flags.realized = 1;
610 if (menu->flags.mapped)
611 wMenuPaint(menu);
612 if (menu->brother->flags.mapped)
613 wMenuPaint(menu->brother);
617 void
618 wMenuDestroy(WMenu *menu, int recurse)
620 int i;
622 WMRemoveNotificationObserver(menu);
624 /* remove any pending timers */
625 if (menu->timer)
626 WMDeleteTimerHandler(menu->timer);
627 menu->timer = NULL;
629 /* call destroy handler */
630 if (menu->on_destroy)
631 (*menu->on_destroy)(menu);
633 /* Destroy items if this menu own them. If this is the "brother" menu,
634 * leave them alone as it is shared by them.
636 if (!menu->flags.brother) {
637 for (i=0; i<menu->entry_no; i++) {
639 free(menu->entries[i]->text);
641 if (menu->entries[i]->rtext)
642 free(menu->entries[i]->rtext);
643 #ifdef USER_MENU
645 if (menu->entries[i]->instances){
646 PLRelease(menu->entries[i]->instances);
648 #endif /* USER_MENU */
650 if (menu->entries[i]->free_cdata && menu->entries[i]->clientdata) {
651 (*menu->entries[i]->free_cdata)(menu->entries[i]->clientdata);
653 free(menu->entries[i]);
656 if (recurse) {
657 for (i=0; i<menu->cascade_no; i++) {
658 if (menu->cascades[i]) {
659 if (menu->cascades[i]->flags.brother)
660 wMenuDestroy(menu->cascades[i]->brother, recurse);
661 else
662 wMenuDestroy(menu->cascades[i], recurse);
667 if (menu->entries)
668 free(menu->entries);
672 FREE_PIXMAP(menu->menu_texture_data);
674 if (menu->cascades)
675 free(menu->cascades);
677 wCoreDestroy(menu->menu);
678 wFrameWindowDestroy(menu->frame);
680 /* destroy copy of this menu */
681 if (!menu->flags.brother && menu->brother)
682 wMenuDestroy(menu->brother, False);
684 free(menu);
688 #define F_NORMAL 0
689 #define F_TOP 1
690 #define F_BOTTOM 2
691 #define F_NONE 3
693 static void
694 drawFrame(WScreen *scr, Window win, int y, int w, int h, int type)
696 XSegment segs[2];
697 int i;
699 i = 0;
700 segs[i].x1 = segs[i].x2 = w-1;
701 segs[i].y1 = y;
702 segs[i].y2 = y + h - 1;
703 i++;
704 if (type != F_TOP && type != F_NONE) {
705 segs[i].x1 = 1;
706 segs[i].y1 = segs[i].y2 = y + h-2;
707 segs[i].x2 = w-1;
708 i++;
710 XDrawSegments(dpy, win, scr->menu_item_auxtexture->dim_gc, segs, i);
712 i = 0;
713 segs[i].x1 = 0;
714 segs[i].y1 = y;
715 segs[i].x2 = 0;
716 segs[i].y2 = y + h - 1;
717 i++;
718 if (type != F_BOTTOM && type != F_NONE) {
719 segs[i].x1 = 0;
720 segs[i].y1 = y;
721 segs[i].x2 = w-1;
722 segs[i].y2 = y;
723 i++;
725 XDrawSegments(dpy, win, scr->menu_item_auxtexture->light_gc, segs, i);
727 if (type != F_TOP && type != F_NONE)
728 XDrawLine(dpy, win, scr->menu_item_auxtexture->dark_gc, 0, y+h-1,
729 w-1, y+h-1);
733 static void
734 paintEntry(WMenu *menu, int index, int selected)
736 int x, y, w, h, tw;
737 int type;
738 GC light, dim, dark, textGC;
739 WScreen *scr=menu->frame->screen_ptr;
740 Window win = menu->menu->window;
741 WMenuEntry *entry=menu->entries[index];
743 if (!menu->flags.realized) return;
744 h = menu->entry_height;
745 w = menu->menu->width;
746 y = index * h;
748 light = scr->menu_item_auxtexture->light_gc;
749 dim = scr->menu_item_auxtexture->dim_gc;
750 dark = scr->menu_item_auxtexture->dark_gc;
752 if (wPreferences.menu_style == MS_FLAT && menu->entry_no > 1) {
753 if (index == 0)
754 type = F_TOP;
755 else if (index == menu->entry_no - 1)
756 type = F_BOTTOM;
757 else
758 type = F_NONE;
759 } else {
760 type = F_NORMAL;
763 /* paint background */
764 if (selected) {
765 XSetForeground(dpy, scr->select_menu_gc, scr->select_pixel);
766 XFillRectangle(dpy, win, scr->select_menu_gc, 1, y+1, w-2, h-3);
767 if (scr->menu_item_texture->any.type == WTEX_SOLID)
768 drawFrame(scr, win, y, w, h, type);
769 } else {
770 if (scr->menu_item_texture->any.type == WTEX_SOLID) {
771 XClearArea(dpy, win, 0, y + 1, w - 1, h - 3, False);
772 /* draw the frame */
773 drawFrame(scr, win, y, w, h, type);
774 } else {
775 XClearArea(dpy, win, 0, y, w, h, False);
779 if (selected) {
780 textGC = scr->select_menu_gc;
781 if (entry->flags.enabled)
782 XSetForeground(dpy, textGC, scr->select_text_pixel);
783 else
784 XSetForeground(dpy, textGC, scr->dtext_pixel);
785 } else if (!entry->flags.enabled) {
786 textGC = scr->disabled_menu_entry_gc;
787 } else {
788 textGC = scr->menu_entry_gc;
790 /* draw text */
791 x = 5;
792 if (entry->flags.indicator)
793 x += MENU_INDICATOR_SPACE + 2;
795 wDrawString(win, scr->menu_entry_font,
796 textGC, x, 3+y+scr->menu_entry_font->y, entry->text,
797 strlen(entry->text));
799 if (entry->cascade>=0) {
800 /* draw the cascade indicator */
801 XDrawLine(dpy,win,dim, w-11, y+6, w-6, y+h/2-1);
802 XDrawLine(dpy,win,light, w-11, y+h-8, w-6, y+h/2-1);
803 XDrawLine(dpy,win,dark, w-12, y+6, w-12, y+h-8);
806 /* draw indicator */
807 if (entry->flags.indicator && entry->flags.indicator_on) {
808 int iw, ih;
809 WPixmap *indicator;
812 switch (entry->flags.indicator_type) {
813 case MI_CHECK:
814 indicator = scr->menu_check_indicator;
815 break;
816 case MI_MINIWINDOW:
817 indicator = scr->menu_mini_indicator;
818 break;
819 case MI_HIDDEN:
820 indicator = scr->menu_hide_indicator;
821 break;
822 case MI_SHADED:
823 indicator = scr->menu_shade_indicator;
824 break;
825 case MI_DIAMOND:
826 default:
827 indicator = scr->menu_radio_indicator;
828 break;
831 iw = indicator->width;
832 ih = indicator->height;
833 XSetClipMask(dpy, scr->copy_gc, indicator->mask);
834 XSetClipOrigin(dpy, scr->copy_gc, 5, y+(h-ih)/2);
835 if (selected)
836 XSetForeground(dpy, scr->copy_gc, scr->black_pixel);
837 else
838 XSetForeground(dpy, scr->copy_gc, scr->mtext_pixel);
839 XFillRectangle(dpy, win, scr->copy_gc, 5, y+(h-ih)/2, iw, ih);
841 XCopyArea(dpy, indicator->image, win, scr->copy_gc, 0, 0,
842 iw, ih, 5, y+(h-ih)/2);
844 XSetClipOrigin(dpy, scr->copy_gc, 0, 0);
847 /* draw right text */
849 if (entry->rtext && entry->cascade<0) {
850 tw = wTextWidth(scr->menu_entry_font->font, entry->rtext,
851 strlen(entry->rtext));
853 wDrawString(win, scr->menu_entry_font, textGC, w-6-tw,
854 3+y+scr->menu_entry_font->y, entry->rtext,
855 strlen(entry->rtext));
860 static void
861 move_menus(WMenu *menu, int x, int y)
863 while (menu->parent) {
864 menu = menu->parent;
865 x -= MENUW(menu);
866 if (!wPreferences.align_menus && menu->selected_entry>=0) {
867 y -= menu->selected_entry*menu->entry_height;
870 wMenuMove(menu, x, y, True);
873 static void
874 makeVisible(WMenu *menu)
876 WScreen *scr = menu->frame->screen_ptr;
877 int x1, y1, x2, y2, new_x, new_y, move;
879 if (menu->entry_no<0) return;
881 x1 = menu->frame_x;
882 y1 = menu->frame_y+menu->frame->top_width
883 + menu->selected_entry*menu->entry_height;
884 x2 = x1 + MENUW(menu);
885 y2 = y1 + menu->entry_height;
887 new_x = x1;
888 new_y = y1;
889 move = 0;
891 if (x1 < 0) {
892 new_x = 0;
893 move = 1;
894 } else if (x2 >= scr->scr_width) {
895 new_x = scr->scr_width - MENUW(menu) - 1;
896 move = 1;
899 if (y1 < 0) {
900 new_y = 0;
901 move = 1;
902 } else if (y2 >= scr->scr_height) {
903 new_y = scr->scr_height - menu->entry_height - 1;
904 move = 1;
907 new_y = new_y - menu->frame->top_width
908 - menu->selected_entry*menu->entry_height;
909 move_menus(menu, new_x, new_y);
913 static int
914 check_key(WMenu *menu, XKeyEvent *event)
916 int i, ch, s;
917 char buffer[32];
919 if (XLookupString(event, buffer, 32, NULL, NULL)<1)
920 return -1;
922 ch = toupper(buffer[0]);
924 s = (menu->selected_entry>=0 ? menu->selected_entry+1 : 0);
926 again:
927 for (i=s; i<menu->entry_no; i++) {
928 if (ch==toupper(menu->entries[i]->text[0])) {
929 return i;
932 /* no match. Retry from start, if previous started from a selected entry */
933 if (s!=0) {
934 s = 0;
935 goto again;
937 return -1;
941 static int
942 keyboardMenu(WMenu *menu)
944 XEvent event;
945 KeySym ksym=NoSymbol;
946 int done=0;
947 int index;
948 WMenuEntry *entry;
949 int old_pos_x = menu->frame_x;
950 int old_pos_y = menu->frame_y;
951 int new_x = old_pos_x, new_y = old_pos_y;
952 int scr_width = menu->frame->screen_ptr->scr_width;
953 int scr_height = menu->frame->screen_ptr->scr_height;
955 if (menu->flags.editing)
956 return False;
959 XGrabKeyboard(dpy, menu->frame->core->window, True, GrabModeAsync,
960 GrabModeAsync, CurrentTime);
962 if (menu->frame_y+menu->frame->top_width >= scr_height)
963 new_y = scr_height - menu->frame->top_width;
965 if (menu->frame_x+MENUW(menu) >= scr_width)
966 new_x = scr_width-MENUW(menu)-1;
968 move_menus(menu, new_x, new_y);
970 while (!done && menu->flags.mapped) {
971 XAllowEvents(dpy, AsyncKeyboard, CurrentTime);
972 WMMaskEvent(dpy, ExposureMask|ButtonMotionMask|ButtonPressMask
973 |ButtonReleaseMask|KeyPressMask|KeyReleaseMask
974 |SubstructureNotifyMask, &event);
976 switch (event.type) {
977 case KeyPress:
978 ksym = XLookupKeysym(&event.xkey, 0);
979 switch (ksym) {
980 case XK_Escape:
981 done = 1;
982 break;
984 case XK_Home:
985 case XK_KP_Home:
986 selectEntry(menu, 0);
987 makeVisible(menu);
988 break;
990 case XK_End:
991 case XK_KP_End:
992 selectEntry(menu, menu->entry_no-1);
993 makeVisible(menu);
994 break;
996 case XK_Up:
997 #ifdef ARROWLESS_KBD
998 case XK_k:
999 #endif
1000 case XK_KP_Up:
1001 if (menu->selected_entry <= 0)
1002 selectEntry(menu, menu->entry_no-1);
1003 else
1004 selectEntry(menu, menu->selected_entry-1);
1005 makeVisible(menu);
1006 break;
1008 case XK_Down:
1009 #ifdef ARROWLESS_KBD
1010 case XK_j:
1011 #endif
1012 case XK_KP_Down:
1013 if (menu->selected_entry<0)
1014 selectEntry(menu, 0);
1015 else if (menu->selected_entry == menu->entry_no-1)
1016 selectEntry(menu, 0);
1017 else if (menu->selected_entry < menu->entry_no-1)
1018 selectEntry(menu, menu->selected_entry+1);
1019 makeVisible(menu);
1020 break;
1022 case XK_Right:
1023 #ifdef ARROWLESS_KBD
1024 case XK_l:
1025 #endif
1026 case XK_KP_Right:
1027 if (menu->selected_entry>=0) {
1028 WMenuEntry *entry;
1029 entry = menu->entries[menu->selected_entry];
1031 if (entry->cascade >= 0 && menu->cascades
1032 && menu->cascades[entry->cascade]->entry_no > 0) {
1034 XUngrabKeyboard(dpy, CurrentTime);
1036 selectEntry(menu->cascades[entry->cascade], 0);
1037 if (!keyboardMenu(menu->cascades[entry->cascade]))
1038 done = 1;
1040 XGrabKeyboard(dpy, menu->frame->core->window, True,
1041 GrabModeAsync, GrabModeAsync,
1042 CurrentTime);
1045 break;
1047 case XK_Left:
1048 #ifdef ARROWLESS_KBD
1049 case XK_h:
1050 #endif
1051 case XK_KP_Left:
1052 if (menu->parent!=NULL && menu->parent->selected_entry>=0) {
1053 selectEntry(menu, -1);
1054 move_menus(menu, old_pos_x, old_pos_y);
1055 return True;
1057 break;
1059 case XK_Return:
1060 done = 2;
1061 break;
1063 default:
1064 index = check_key(menu, &event.xkey);
1065 if (index>=0) {
1066 selectEntry(menu, index);
1069 break;
1071 default:
1072 if (event.type==ButtonPress)
1073 done = 1;
1075 WMHandleEvent(&event);
1079 XUngrabKeyboard(dpy, CurrentTime);
1081 if (done==2 && menu->selected_entry>=0) {
1082 entry = menu->entries[menu->selected_entry];
1083 } else {
1084 entry = NULL;
1087 if (entry && entry->callback!=NULL && entry->flags.enabled
1088 && entry->cascade < 0) {
1089 #if (MENU_BLINK_COUNT > 0)
1090 int sel = menu->selected_entry;
1091 int i;
1093 for (i=0; i<MENU_BLINK_COUNT; i++) {
1094 paintEntry(menu, sel, False);
1095 XSync(dpy, 0);
1096 wusleep(MENU_BLINK_DELAY);
1097 paintEntry(menu, sel, True);
1098 XSync(dpy, 0);
1099 wusleep(MENU_BLINK_DELAY);
1101 #endif
1102 selectEntry(menu, -1);
1104 if (!menu->flags.buttoned) {
1105 wMenuUnmap(menu);
1106 move_menus(menu, old_pos_x, old_pos_y);
1108 closeCascade(menu);
1110 (*entry->callback)(menu, entry);
1111 } else {
1112 if (!menu->flags.buttoned) {
1113 wMenuUnmap(menu);
1114 move_menus(menu, old_pos_x, old_pos_y);
1116 selectEntry(menu, -1);
1120 /* returns True if returning from a submenu to a parent menu,
1121 * False if exiting from menu */
1122 return False;
1126 void
1127 wMenuMapAt(WMenu *menu, int x, int y, int keyboard)
1129 int scr_width = menu->frame->screen_ptr->scr_width;
1130 int scr_height = menu->frame->screen_ptr->scr_height;
1132 if (!menu->flags.realized) {
1133 menu->flags.realized=1;
1134 wMenuRealize(menu);
1136 if (!menu->flags.mapped) {
1137 if (wPreferences.wrap_menus) {
1138 if (x<0) x = 0;
1139 if (y<0) y = 0;
1140 if (x+MENUW(menu) > scr_width)
1141 x = scr_width - MENUW(menu);
1142 if (y+MENUH(menu) > scr_height)
1143 y = scr_height - MENUH(menu);
1146 XMoveWindow(dpy, menu->frame->core->window, x, y);
1147 menu->frame_x = x;
1148 menu->frame_y = y;
1149 XMapWindow(dpy, menu->frame->core->window);
1150 wRaiseFrame(menu->frame->core);
1151 menu->flags.mapped = 1;
1152 } else {
1153 selectEntry(menu, 0);
1156 if (keyboard)
1157 keyboardMenu(menu);
1161 void
1162 wMenuMap(WMenu *menu)
1164 if (!menu->flags.realized) {
1165 menu->flags.realized=1;
1166 wMenuRealize(menu);
1168 if (menu->flags.app_menu && menu->parent==NULL) {
1169 menu->frame_x = menu->frame->screen_ptr->app_menu_x;
1170 menu->frame_y = menu->frame->screen_ptr->app_menu_y;
1171 XMoveWindow(dpy, menu->frame->core->window, menu->frame_x, menu->frame_y);
1173 XMapWindow(dpy, menu->frame->core->window);
1174 wRaiseFrame(menu->frame->core);
1175 menu->flags.mapped = 1;
1179 void
1180 wMenuUnmap(WMenu *menu)
1182 int i;
1184 XUnmapWindow(dpy, menu->frame->core->window);
1185 if (menu->flags.titled && menu->flags.buttoned) {
1186 wFrameWindowHideButton(menu->frame, WFF_RIGHT_BUTTON);
1188 menu->flags.buttoned = 0;
1189 menu->flags.mapped = 0;
1190 menu->flags.open_to_left = 0;
1192 for (i=0; i<menu->cascade_no; i++) {
1193 if (menu->cascades[i]!=NULL
1194 && menu->cascades[i]->flags.mapped
1195 && !menu->cascades[i]->flags.buttoned) {
1197 wMenuUnmap(menu->cascades[i]);
1200 menu->selected_entry = -1;
1205 void
1206 wMenuPaint(WMenu *menu)
1208 int i;
1210 if (!menu->flags.mapped) {
1211 return;
1214 /* paint entries */
1215 for (i=0; i<menu->entry_no; i++) {
1216 paintEntry(menu, i, i==menu->selected_entry);
1221 void
1222 wMenuSetEnabled(WMenu *menu, int index, int enable)
1224 if (index>=menu->entry_no) return;
1225 menu->entries[index]->flags.enabled=enable;
1226 paintEntry(menu, index, index==menu->selected_entry);
1227 paintEntry(menu->brother, index, index==menu->selected_entry);
1231 /* ====================================================================== */
1234 static void
1235 editEntry(WMenu *menu, WMenuEntry *entry)
1237 WTextInput *text;
1238 XEvent event;
1239 WObjDescriptor *desc;
1240 char *t;
1241 int done = 0;
1242 Window old_focus;
1243 int old_revert;
1245 menu->flags.editing = 1;
1247 text = wTextCreate(menu->menu, 1, menu->entry_height * entry->order,
1248 menu->menu->width - 2, menu->entry_height - 1);
1250 wTextPutText(text, entry->text);
1251 XGetInputFocus(dpy, &old_focus, &old_revert);
1252 XSetInputFocus(dpy, text->core->window, RevertToNone, CurrentTime);
1254 if (XGrabKeyboard(dpy, text->core->window, True, GrabModeAsync,
1255 GrabModeAsync, CurrentTime)!=GrabSuccess) {
1256 wwarning("could not grab keyboard");
1257 wTextDestroy(text);
1259 wSetFocusTo(menu->frame->screen_ptr,
1260 menu->frame->screen_ptr->focused_window);
1261 return;
1265 while (!done && !text->done) {
1266 XSync(dpy, 0);
1267 XAllowEvents(dpy, AsyncKeyboard|AsyncPointer, CurrentTime);
1268 XSync(dpy, 0);
1269 WMNextEvent(dpy, &event);
1271 if (XFindContext(dpy, event.xany.window, wWinContext,
1272 (XPointer *)&desc)==XCNOENT)
1273 desc = NULL;
1275 if ((desc != NULL) && (desc->handle_anything != NULL)) {
1277 (*desc->handle_anything)(desc, &event);
1279 } else {
1280 switch (event.type) {
1281 case ButtonPress:
1282 XAllowEvents(dpy, ReplayPointer, CurrentTime);
1283 done = 1;
1285 default:
1286 WMHandleEvent(&event);
1287 break;
1292 XSetInputFocus(dpy, old_focus, old_revert, CurrentTime);
1294 wSetFocusTo(menu->frame->screen_ptr,
1295 menu->frame->screen_ptr->focused_window);
1298 t = wTextGetText(text);
1299 /* if !t, the user has canceled editing */
1300 if (t) {
1301 if (entry->text)
1302 free(entry->text);
1303 entry->text = wstrdup(t);
1305 menu->flags.realized = 0;
1307 wTextDestroy(text);
1309 XUngrabKeyboard(dpy, CurrentTime);
1311 if (t && menu->on_edit)
1312 (*menu->on_edit)(menu, entry);
1314 menu->flags.editing = 0;
1316 if (!menu->flags.realized)
1317 wMenuRealize(menu);
1321 static void
1322 selectEntry(WMenu *menu, int entry_no)
1324 WMenuEntry *entry;
1325 WMenu *submenu;
1326 int old_entry;
1328 if (menu->entries==NULL)
1329 return;
1331 if (entry_no >= menu->entry_no)
1332 return;
1334 old_entry = menu->selected_entry;
1335 menu->selected_entry = entry_no;
1337 if (old_entry!=entry_no) {
1339 /* unselect previous entry */
1340 if (old_entry>=0) {
1341 paintEntry(menu, old_entry, False);
1342 entry = menu->entries[old_entry];
1344 /* unmap cascade */
1345 if (entry->cascade>=0 && menu->cascades) {
1346 if (!menu->cascades[entry->cascade]->flags.buttoned) {
1347 wMenuUnmap(menu->cascades[entry->cascade]);
1352 if (entry_no<0) {
1353 menu->selected_entry = -1;
1354 return;
1356 entry = menu->entries[entry_no];
1358 if (entry->cascade>=0 && menu->cascades && entry->flags.enabled) {
1359 /* Callback for when the submenu is opened.
1361 submenu = menu->cascades[entry->cascade];
1362 if (submenu && submenu->flags.brother)
1363 submenu = submenu->brother;
1365 if (entry->callback) {
1366 /* Only call the callback if the submenu is not yet mapped.
1368 if (menu->flags.brother) {
1369 if (!submenu || !submenu->flags.mapped)
1370 (*entry->callback)(menu->brother, entry);
1371 } else {
1372 if (!submenu || !submenu->flags.buttoned)
1373 (*entry->callback)(menu, entry);
1377 /* the submenu menu might have changed */
1378 submenu = menu->cascades[entry->cascade];
1380 /* map cascade */
1381 if (!submenu->flags.mapped) {
1382 int x, y;
1384 if (!submenu->flags.realized)
1385 wMenuRealize(submenu);
1386 if (wPreferences.wrap_menus) {
1387 if (menu->flags.open_to_left)
1388 submenu->flags.open_to_left = 1;
1390 if (submenu->flags.open_to_left) {
1391 x = menu->frame_x - MENUW(submenu);
1392 if (x<0) {
1393 x = 0;
1394 submenu->flags.open_to_left = 0;
1396 } else {
1397 x = menu->frame_x + MENUW(menu);
1399 if (x + MENUW(submenu)
1400 >= menu->frame->screen_ptr->scr_width) {
1402 x = menu->frame_x - MENUW(submenu);
1403 submenu->flags.open_to_left = 1;
1406 } else {
1407 x = menu->frame_x + MENUW(menu);
1410 if (wPreferences.align_menus) {
1411 y = menu->frame_y;
1412 } else {
1413 y = menu->frame_y + menu->entry_height*entry_no;
1414 if (menu->flags.titled)
1415 y += menu->frame->top_width;
1416 if (menu->cascades[entry->cascade]->flags.titled)
1417 y -= menu->cascades[entry->cascade]->frame->top_width;
1420 wMenuMapAt(menu->cascades[entry->cascade], x, y, False);
1421 menu->cascades[entry->cascade]->parent = menu;
1422 } else {
1423 return;
1426 paintEntry(menu, entry_no, True);
1431 static WMenu*
1432 findMenu(WScreen *scr, int *x_ret, int *y_ret)
1434 WMenu *menu;
1435 WObjDescriptor *desc;
1436 Window root_ret, win, junk_win;
1437 int x, y, wx, wy;
1438 unsigned int mask;
1440 XQueryPointer(dpy, scr->root_win, &root_ret, &win, &x, &y, &wx, &wy,
1441 &mask);
1443 if (win==None) return NULL;
1445 if (XFindContext(dpy, win, wWinContext, (XPointer *)&desc)==XCNOENT)
1446 return NULL;
1448 if (desc->parent_type == WCLASS_MENU) {
1449 menu = (WMenu*)desc->parent;
1450 XTranslateCoordinates(dpy, root_ret, menu->menu->window, wx, wy,
1451 x_ret, y_ret, &junk_win);
1452 return menu;
1454 return NULL;
1460 static void
1461 closeCascade(WMenu *menu)
1463 WMenu *parent=menu->parent;
1465 if (menu->flags.brother
1466 || (!menu->flags.buttoned
1467 && (!menu->flags.app_menu||menu->parent!=NULL))) {
1469 selectEntry(menu, -1);
1470 XSync(dpy, 0);
1471 #if (MENU_BLINK_DELAY > 2)
1472 wusleep(MENU_BLINK_DELAY/2);
1473 #endif
1474 wMenuUnmap(menu);
1475 while (parent!=NULL
1476 && (parent->parent!=NULL || !parent->flags.app_menu
1477 || parent->flags.brother)
1478 && !parent->flags.buttoned) {
1479 selectEntry(parent, -1);
1480 wMenuUnmap(parent);
1481 parent = parent->parent;
1483 if (parent)
1484 selectEntry(parent, -1);
1489 static void
1490 closeBrotherCascadesOf(WMenu *menu)
1492 WMenu *tmp;
1493 int i;
1495 for (i=0; i<menu->cascade_no; i++) {
1496 if (menu->cascades[i]->flags.brother) {
1497 tmp = menu->cascades[i];
1498 } else {
1499 tmp = menu->cascades[i]->brother;
1501 if (tmp->flags.mapped) {
1502 selectEntry(tmp->parent, -1);
1503 closeBrotherCascadesOf(tmp);
1504 break;
1510 #define getEntryAt(menu, x, y) ((y)<0 ? -1 : (y)/(menu->entry_height))
1513 static WMenu*
1514 parentMenu(WMenu *menu)
1516 WMenu *parent;
1517 WMenuEntry *entry;
1519 if (menu->flags.buttoned)
1520 return menu;
1522 while (menu->parent && menu->parent->flags.mapped) {
1523 parent = menu->parent;
1524 if (parent->selected_entry < 0)
1525 break;
1526 entry = parent->entries[parent->selected_entry];
1527 if (!entry->flags.enabled || entry->cascade<0 || !parent->cascades ||
1528 parent->cascades[entry->cascade] != menu)
1529 break;
1530 menu = parent;
1531 if (menu->flags.buttoned)
1532 break;
1535 return menu;
1541 * Will raise the passed menu, if submenu = 0
1542 * If submenu > 0 will also raise all mapped submenus
1543 * until the first buttoned one
1544 * If submenu < 0 will also raise all mapped parent menus
1545 * until the first buttoned one
1548 static void
1549 raiseMenus(WMenu *menu, int submenus)
1551 WMenu *submenu;
1552 int i;
1554 if(!menu) return;
1556 wRaiseFrame(menu->frame->core);
1558 if (submenus>0 && menu->selected_entry>=0) {
1559 i = menu->entries[menu->selected_entry]->cascade;
1560 if (i>=0 && menu->cascades) {
1561 submenu = menu->cascades[i];
1562 if (submenu->flags.mapped && !submenu->flags.buttoned)
1563 raiseMenus(submenu, submenus);
1566 if (submenus<0 && !menu->flags.buttoned &&
1567 menu->parent && menu->parent->flags.mapped)
1568 raiseMenus(menu->parent, submenus);
1572 WMenu*
1573 wMenuUnderPointer(WScreen *screen)
1575 WObjDescriptor *desc;
1576 Window root_ret, win;
1577 int dummy;
1578 unsigned int mask;
1580 XQueryPointer(dpy, screen->root_win, &root_ret, &win, &dummy, &dummy,
1581 &dummy, &dummy, &mask);
1583 if (win==None) return NULL;
1585 if (XFindContext(dpy, win, wWinContext, (XPointer *)&desc)==XCNOENT)
1586 return NULL;
1588 if (desc->parent_type == WCLASS_MENU)
1589 return (WMenu *)desc->parent;
1590 return NULL;
1595 #define MIN(a,b) (((a) > (b)) ? (b) : (a))
1598 static void
1599 getPointerPosition(WScreen *scr, int *x, int *y)
1601 Window root_ret, win;
1602 int wx, wy;
1603 unsigned int mask;
1605 XQueryPointer(dpy, scr->root_win, &root_ret, &win, x, y, &wx, &wy, &mask);
1609 static void
1610 getScrollAmount(WMenu *menu, int *hamount, int *vamount)
1612 WScreen *scr = menu->menu->screen_ptr;
1613 int menuX1 = menu->frame_x;
1614 int menuY1 = menu->frame_y;
1615 int menuX2 = menu->frame_x + MENUW(menu);
1616 int menuY2 = menu->frame_y + MENUH(menu);
1617 int screenW = scr->scr_width;
1618 int screenH = scr->scr_height;
1619 int xroot, yroot;
1621 *hamount = 0;
1622 *vamount = 0;
1624 getPointerPosition(scr, &xroot, &yroot);
1627 if (xroot <= 1 && menuX1 < 0) {
1628 /* scroll to the right */
1629 *hamount = MIN(MENU_SCROLL_STEP, abs(menuX1));
1631 } else if (xroot >= screenW-2 && menuX2 > screenW-1) {
1632 /* scroll to the left */
1633 *hamount = MIN(MENU_SCROLL_STEP, abs(menuX2-screenW-1));
1635 if (*hamount==0)
1636 *hamount = 1;
1638 *hamount = -*hamount;
1641 if (yroot <= 1 && menuY1 < 0) {
1642 /* scroll down */
1643 *vamount = MIN(MENU_SCROLL_STEP, abs(menuY1));
1645 } else if (yroot >= screenH-2 && menuY2 > screenH-1) {
1646 /* scroll up */
1647 *vamount = MIN(MENU_SCROLL_STEP, abs(menuY2-screenH-2));
1649 *vamount = -*vamount;
1654 static void
1655 dragScrollMenuCallback(void *data)
1657 WMenu *menu = (WMenu*)data;
1658 WScreen *scr = menu->menu->screen_ptr;
1659 WMenu *parent = parentMenu(menu);
1660 int hamount, vamount;
1661 int x, y;
1662 int newSelectedEntry;
1664 getScrollAmount(menu, &hamount, &vamount);
1667 if (hamount != 0 || vamount != 0) {
1668 wMenuMove(parent, parent->frame_x + hamount,
1669 parent->frame_y + vamount, True);
1670 if (findMenu(scr, &x, &y)) {
1671 newSelectedEntry = getEntryAt(menu, x, y);
1672 selectEntry(menu, newSelectedEntry);
1673 } else {
1674 /* Pointer fell outside of menu. If the selected entry is
1675 * not a submenu, unselect it */
1676 if (menu->selected_entry >= 0
1677 && menu->entries[menu->selected_entry]->cascade<0)
1678 selectEntry(menu, -1);
1679 newSelectedEntry = 0;
1682 /* paranoid check */
1683 if (newSelectedEntry >= 0) {
1684 /* keep scrolling */
1685 menu->timer = WMAddTimerHandler(MENU_SCROLL_DELAY,
1686 dragScrollMenuCallback, menu);
1687 } else {
1688 menu->timer = NULL;
1690 } else {
1691 /* don't need to scroll anymore */
1692 menu->timer = NULL;
1693 if (findMenu(scr, &x, &y)) {
1694 newSelectedEntry = getEntryAt(menu, x, y);
1695 selectEntry(menu, newSelectedEntry);
1701 static void
1702 scrollMenuCallback(void *data)
1704 WMenu *menu = (WMenu*)data;
1705 WMenu *parent = parentMenu(menu);
1706 int hamount = 0; /* amount to scroll */
1707 int vamount = 0;
1709 getScrollAmount(menu, &hamount, &vamount);
1711 if (hamount != 0 || vamount != 0) {
1712 wMenuMove(parent, parent->frame_x + hamount,
1713 parent->frame_y + vamount, True);
1715 /* keep scrolling */
1716 menu->timer = WMAddTimerHandler(MENU_SCROLL_DELAY,
1717 scrollMenuCallback, menu);
1718 } else {
1719 /* don't need to scroll anymore */
1720 menu->timer = NULL;
1726 #define MENU_SCROLL_BORDER 5
1728 static int
1729 isPointNearBoder(WMenu *menu, int x, int y)
1731 int menuX1 = menu->frame_x;
1732 int menuY1 = menu->frame_y;
1733 int menuX2 = menu->frame_x + MENUW(menu);
1734 int menuY2 = menu->frame_y + MENUH(menu);
1735 int scrXe = menu->menu->screen_ptr->scr_width-1;
1736 int scrYe = menu->menu->screen_ptr->scr_height-1;
1737 int flag = 0;
1739 if (x >= menuX1 && x <= menuX2 && (y < MENU_SCROLL_BORDER
1740 || y > scrYe-MENU_SCROLL_BORDER))
1741 flag = 1;
1742 else if (y >= menuY1 && y <= menuY2 && (x < MENU_SCROLL_BORDER
1743 || x > scrXe-MENU_SCROLL_BORDER))
1744 flag = 1;
1746 return flag;
1750 typedef struct _delay {
1751 WWindow *wwin;
1752 WMenu *menu;
1753 int ox,oy;
1754 } _delay;
1757 static void
1758 _leaving(_delay *dl)
1760 wMenuMove(dl->menu, dl->ox, dl->oy, True);
1761 dl->menu->jump_back=NULL;
1762 dl->menu->menu->screen_ptr->flags.jump_back_pending = 0;
1763 free(dl);
1767 void
1768 wMenuScroll(WMenu *menu, XEvent *event)
1770 WMenu *smenu;
1771 WMenu *omenu = parentMenu(menu);
1772 WScreen *scr = menu->frame->screen_ptr;
1773 int done = 0;
1774 int jump_back = 0;
1775 int old_frame_x = omenu->frame_x;
1776 int old_frame_y = omenu->frame_y;
1777 XEvent ev;
1779 #ifdef DEBUG
1780 puts("Entering menu Scroll");
1781 #endif
1783 if (omenu->jump_back)
1784 WMDeleteTimerWithClientData(omenu->jump_back);
1787 if ((/*omenu->flags.buttoned &&*/ !wPreferences.wrap_menus)
1788 || omenu->flags.app_menu) {
1789 jump_back = 1;
1792 if (!wPreferences.wrap_menus)
1793 raiseMenus(omenu, True);
1794 else
1795 raiseMenus(menu, False);
1797 if (!menu->timer)
1798 scrollMenuCallback(menu);
1800 while(!done) {
1801 int x, y, on_border, on_x_edge, on_y_edge, on_title;
1803 WMNextEvent(dpy, &ev);
1804 switch (ev.type) {
1805 case EnterNotify:
1806 WMHandleEvent(&ev);
1807 case MotionNotify:
1808 x = (ev.type==MotionNotify) ? ev.xmotion.x_root : ev.xcrossing.x_root;
1809 y = (ev.type==MotionNotify) ? ev.xmotion.y_root : ev.xcrossing.y_root;
1811 /* on_border is != 0 if the pointer is between the menu
1812 * and the screen border and is close enough to the border */
1813 on_border = isPointNearBoder(menu, x, y);
1815 smenu = wMenuUnderPointer(scr);
1817 if ((smenu==NULL && !on_border) || (smenu && parentMenu(smenu)!=omenu)) {
1818 done = 1;
1819 break;
1822 on_x_edge = x <= 1 || x >= scr->scr_width - 2;
1823 on_y_edge = y <= 1 || y >= scr->scr_height - 2;
1824 on_border = on_x_edge || on_y_edge;
1826 if (!on_border && !jump_back) {
1827 done = 1;
1828 break;
1831 if (menu->timer && (smenu!=menu || (!on_y_edge && !on_x_edge))) {
1832 WMDeleteTimerHandler(menu->timer);
1833 menu->timer = NULL;
1836 if (smenu != NULL)
1837 menu = smenu;
1839 if (!menu->timer)
1840 scrollMenuCallback(menu);
1841 break;
1842 case ButtonPress:
1843 /* True if we push on title, or drag the omenu to other position */
1844 on_title = ev.xbutton.x_root >= omenu->frame_x &&
1845 ev.xbutton.x_root <= omenu->frame_x + MENUW(omenu) &&
1846 ev.xbutton.y_root >= omenu->frame_y &&
1847 ev.xbutton.y_root <= omenu->frame_y + omenu->frame->top_width;
1848 WMHandleEvent(&ev);
1849 smenu = wMenuUnderPointer(scr);
1850 if (smenu == NULL || (smenu && smenu->flags.buttoned && smenu != omenu))
1851 done = 1;
1852 else if (smenu==omenu && on_title) {
1853 jump_back = 0;
1854 done = 1;
1856 break;
1857 case KeyPress:
1858 done = 1;
1859 default:
1860 WMHandleEvent(&ev);
1861 break;
1865 if (menu->timer) {
1866 WMDeleteTimerHandler(menu->timer);
1867 menu->timer = NULL;
1870 if (jump_back) {
1871 _delay *delayer;
1872 if (!omenu->jump_back) {
1873 delayer=wmalloc(sizeof(_delay));
1874 delayer->menu=omenu;
1875 delayer->ox=old_frame_x;
1876 delayer->oy=old_frame_y;
1877 omenu->jump_back=delayer;
1878 scr->flags.jump_back_pending = 1;
1880 else delayer = omenu->jump_back;
1881 WMAddTimerHandler(MENU_JUMP_BACK_DELAY,(WMCallback*)_leaving, delayer);
1885 #ifdef DEBUG
1886 puts("Leaving menu Scroll");
1887 #endif
1892 static void
1893 menuExpose(WObjDescriptor *desc, XEvent *event)
1895 wMenuPaint(desc->parent);
1898 typedef struct {
1899 int *delayed_select;
1900 WMenu *menu;
1901 WMHandlerID magic;
1902 } delay_data;
1905 static void
1906 delaySelection(void *data)
1908 delay_data *d = (delay_data*)data;
1909 int x, y, entry_no;
1910 WMenu *menu;
1912 d->magic = NULL;
1914 menu = findMenu(d->menu->menu->screen_ptr, &x, &y);
1915 if (menu && (d->menu == menu || d->delayed_select)) {
1916 entry_no = getEntryAt(menu, x, y);
1917 selectEntry(menu, entry_no);
1919 if (d->delayed_select)
1920 *(d->delayed_select) = 0;
1924 static void
1925 menuMouseDown(WObjDescriptor *desc, XEvent *event)
1927 XButtonEvent *bev = &event->xbutton;
1928 WMenu *menu = desc->parent;
1929 WMenu *smenu;
1930 WScreen *scr=menu->frame->screen_ptr;
1931 WMenuEntry *entry=NULL;
1932 XEvent ev;
1933 int close_on_exit=0;
1934 int done=0;
1935 int delayed_select = 0;
1936 int entry_no;
1937 int x, y;
1938 int prevx, prevy;
1939 int old_frame_x = 0;
1940 int old_frame_y = 0;
1941 delay_data d_data = {NULL, NULL, NULL};
1943 if (!wPreferences.wrap_menus) {
1944 smenu = parentMenu(menu);
1945 old_frame_x = smenu->frame_x;
1946 old_frame_y = smenu->frame_y;
1947 } else if (event->xbutton.window == menu->frame->core->window) {
1948 /* This is true if the menu was launched with right click on root window */
1949 delayed_select = 1;
1950 d_data.delayed_select = &delayed_select;
1951 d_data.menu = menu;
1952 d_data.magic = WMAddTimerHandler(wPreferences.dblclick_time,
1953 delaySelection, &d_data);
1956 wRaiseFrame(menu->frame->core);
1958 close_on_exit = (bev->send_event || menu->flags.brother);
1960 smenu = findMenu(scr, &x, &y);
1961 if (!smenu) {
1962 x = -1;
1963 y = -1;
1964 } else {
1965 menu = smenu;
1968 if (menu->flags.editing) {
1969 return;
1971 entry_no = getEntryAt(menu, x, y);
1972 if (entry_no>=0) {
1973 entry = menu->entries[entry_no];
1975 if (!close_on_exit && (bev->state & ControlMask) && smenu
1976 && entry->flags.editable) {
1977 editEntry(smenu, entry);
1978 return;
1979 } else if (bev->state & ControlMask) {
1980 return;
1983 if (entry->flags.enabled && entry->cascade>=0 && menu->cascades) {
1984 WMenu *submenu = menu->cascades[entry->cascade];
1985 /* map cascade */
1986 if (submenu->flags.mapped && !submenu->flags.buttoned &&
1987 menu->selected_entry!=entry_no) {
1988 wMenuUnmap(submenu);
1990 if (!submenu->flags.mapped && !delayed_select) {
1991 selectEntry(menu, entry_no);
1992 } else if (!submenu->flags.buttoned) {
1993 selectEntry(menu, -1);
1996 } else if (!delayed_select) {
1997 selectEntry(menu, entry_no);
2000 if (!wPreferences.wrap_menus && !wPreferences.scrollable_menus) {
2001 if (!menu->timer)
2002 dragScrollMenuCallback(menu);
2006 prevx = bev->x_root;
2007 prevy = bev->y_root;
2008 while (!done) {
2009 int x, y;
2011 XAllowEvents(dpy, SyncPointer, CurrentTime);
2013 WMMaskEvent(dpy, ExposureMask|ButtonMotionMask|ButtonReleaseMask
2014 |ButtonPressMask, &ev);
2015 switch (ev.type) {
2016 case MotionNotify:
2017 smenu = findMenu(scr, &x, &y);
2019 if (smenu == NULL) {
2020 /* moved mouse out of menu */
2022 if (!delayed_select && d_data.magic) {
2023 WMDeleteTimerHandler(d_data.magic);
2024 d_data.magic = NULL;
2026 if (menu==NULL
2027 || (menu->selected_entry>=0
2028 && menu->entries[menu->selected_entry]->cascade>=0)) {
2029 prevx = ev.xmotion.x_root;
2030 prevy = ev.xmotion.y_root;
2032 break;
2034 selectEntry(menu, -1);
2035 menu = smenu;
2036 prevx = ev.xmotion.x_root;
2037 prevy = ev.xmotion.y_root;
2038 break;
2039 } else if (menu && menu!=smenu
2040 && (menu->selected_entry<0
2041 || menu->entries[menu->selected_entry]->cascade<0)) {
2042 selectEntry(menu, -1);
2044 if (!delayed_select && d_data.magic) {
2045 WMDeleteTimerHandler(d_data.magic);
2046 d_data.magic = NULL;
2048 } else {
2050 /* hysteresis for item selection */
2052 /* check if the motion was to the side, indicating that
2053 * the user may want to cross to a submenu */
2054 if (!delayed_select && menu) {
2055 int dx;
2056 Bool moved_to_submenu;/* moved to direction of submenu */
2058 dx = abs(prevx - ev.xmotion.x_root);
2060 moved_to_submenu = False;
2061 if (dx > 0 /* if moved enough to the side */
2062 /* maybe a open submenu */
2063 && menu->selected_entry>=0
2064 /* moving to the right direction */
2065 && (wPreferences.align_menus
2066 || ev.xmotion.y_root >= prevy)) {
2067 int index;
2069 index = menu->entries[menu->selected_entry]->cascade;
2070 if (index>=0) {
2071 if (menu->cascades[index]->frame_x>menu->frame_x) {
2072 if (prevx < ev.xmotion.x_root)
2073 moved_to_submenu = True;
2074 } else {
2075 if (prevx > ev.xmotion.x_root)
2076 moved_to_submenu = True;
2082 if (menu != smenu) {
2083 if (d_data.magic) {
2084 WMDeleteTimerHandler(d_data.magic);
2086 d_data.magic = NULL;
2087 } else if (moved_to_submenu) {
2088 /* while we are moving, postpone the selection */
2089 if (d_data.magic) {
2090 WMDeleteTimerHandler(d_data.magic);
2092 d_data.delayed_select = NULL;
2093 d_data.menu = menu;
2094 d_data.magic = WMAddTimerHandler(MENU_SELECT_DELAY,
2095 delaySelection,
2096 &d_data);
2097 prevx = ev.xmotion.x_root;
2098 prevy = ev.xmotion.y_root;
2099 break;
2100 } else {
2101 if (d_data.magic)
2102 WMDeleteTimerHandler(d_data.magic);
2103 d_data.magic = NULL;
2107 prevx = ev.xmotion.x_root;
2108 prevy = ev.xmotion.y_root;
2109 if (menu!=smenu) {
2110 /* pointer crossed menus */
2111 if (menu && menu->timer) {
2112 WMDeleteTimerHandler(menu->timer);
2113 menu->timer = NULL;
2115 if (smenu)
2116 dragScrollMenuCallback(smenu);
2118 menu = smenu;
2119 if (!menu->timer)
2120 dragScrollMenuCallback(menu);
2122 if (!delayed_select) {
2123 entry_no = getEntryAt(menu, x, y);
2124 if (entry_no>=0) {
2125 entry = menu->entries[entry_no];
2126 if (entry->flags.enabled && entry->cascade>=0 &&
2127 menu->cascades) {
2128 WMenu *submenu = menu->cascades[entry->cascade];
2129 if (submenu->flags.mapped && !submenu->flags.buttoned
2130 && menu->selected_entry!=entry_no) {
2131 wMenuUnmap(submenu);
2135 selectEntry(menu, entry_no);
2137 break;
2139 case ButtonPress:
2140 break;
2142 case ButtonRelease:
2143 if (ev.xbutton.button == event->xbutton.button)
2144 done=1;
2145 break;
2147 case Expose:
2148 WMHandleEvent(&ev);
2149 break;
2153 if (menu && menu->timer) {
2154 WMDeleteTimerHandler(menu->timer);
2155 menu->timer = NULL;
2157 if (d_data.magic!=NULL)
2158 WMDeleteTimerHandler(d_data.magic);
2160 if (menu && menu->selected_entry>=0) {
2161 entry = menu->entries[menu->selected_entry];
2162 if (entry->callback!=NULL && entry->flags.enabled
2163 && entry->cascade < 0) {
2164 /* blink and erase menu selection */
2165 #if (MENU_BLINK_DELAY > 0)
2166 int sel = menu->selected_entry;
2167 int i;
2169 for (i=0; i<MENU_BLINK_COUNT; i++) {
2170 paintEntry(menu, sel, False);
2171 XSync(dpy, 0);
2172 wusleep(MENU_BLINK_DELAY);
2173 paintEntry(menu, sel, True);
2174 XSync(dpy, 0);
2175 wusleep(MENU_BLINK_DELAY);
2177 #endif
2178 /* unmap the menu, it's parents and call the callback */
2179 if (!menu->flags.buttoned &&
2180 (!menu->flags.app_menu||menu->parent!=NULL)) {
2181 closeCascade(menu);
2182 } else {
2183 selectEntry(menu, -1);
2185 (*entry->callback)(menu, entry);
2187 /* If the user double clicks an entry, the entry will
2188 * be executed twice, which is not good for things like
2189 * the root menu. So, ignore any clicks that were generated
2190 * while the entry was being executed */
2191 while (XCheckTypedWindowEvent(dpy, menu->menu->window,
2192 ButtonPress, &ev));
2193 } else if (entry->callback!=NULL && entry->cascade<0) {
2194 selectEntry(menu, -1);
2195 } else {
2196 if (entry->cascade>=0 && menu->cascades
2197 && menu->cascades[entry->cascade]->flags.brother) {
2198 selectEntry(menu, -1);
2203 if (((WMenu*)desc->parent)->flags.brother || close_on_exit || !smenu)
2204 closeCascade(desc->parent);
2206 /* close the cascade windows that should not remain opened */
2207 closeBrotherCascadesOf(desc->parent);
2209 if (!wPreferences.wrap_menus)
2210 wMenuMove(parentMenu(desc->parent), old_frame_x, old_frame_y, True);
2214 void
2215 wMenuMove(WMenu *menu, int x, int y, int submenus)
2217 WMenu *submenu;
2218 int i;
2220 if (!menu) return;
2222 menu->frame_x = x;
2223 menu->frame_y = y;
2224 XMoveWindow(dpy, menu->frame->core->window, x, y);
2226 if (submenus>0 && menu->selected_entry>=0) {
2227 i = menu->entries[menu->selected_entry]->cascade;
2229 if (i>=0 && menu->cascades) {
2230 submenu = menu->cascades[i];
2231 if (submenu->flags.mapped && !submenu->flags.buttoned)
2232 if (wPreferences.align_menus) {
2233 wMenuMove(submenu, x + MENUW(menu), y, submenus);
2234 } else {
2235 wMenuMove(submenu, x+ MENUW(menu),
2236 y + submenu->entry_height*menu->selected_entry,
2237 submenus);
2241 if (submenus<0 && menu->parent!=NULL && menu->parent->flags.mapped &&
2242 !menu->parent->flags.buttoned) {
2243 if (wPreferences.align_menus) {
2244 wMenuMove(menu->parent, x - MENUW(menu->parent), y, submenus);
2245 } else {
2246 wMenuMove(menu->parent, x - MENUW(menu->parent), menu->frame_y
2247 - menu->parent->entry_height*menu->parent->selected_entry,
2248 submenus);
2254 static void
2255 changeMenuLevels(WMenu *menu, int lower)
2257 int i;
2259 if (!lower) {
2260 ChangeStackingLevel(menu->frame->core, (!menu->parent ? WMMainMenuLevel
2261 : WMSubmenuLevel));
2262 wRaiseFrame(menu->frame->core);
2263 menu->flags.lowered = 0;
2264 } else {
2265 ChangeStackingLevel(menu->frame->core, WMNormalLevel);
2266 wLowerFrame(menu->frame->core);
2267 menu->flags.lowered = 1;
2269 for (i=0; i<menu->cascade_no; i++) {
2270 if (menu->cascades[i]
2271 && !menu->cascades[i]->flags.buttoned
2272 && menu->cascades[i]->flags.lowered!=lower) {
2273 changeMenuLevels(menu->cascades[i], lower);
2280 static void
2281 menuTitleDoubleClick(WCoreWindow *sender, void *data, XEvent *event)
2283 WMenu *menu = data;
2284 int lower;
2286 if (event->xbutton.state & MOD_MASK) {
2287 if (menu->flags.lowered) {
2288 lower = 0;
2289 } else {
2290 lower = 1;
2292 changeMenuLevels(menu, lower);
2297 static void
2298 menuTitleMouseDown(WCoreWindow *sender, void *data, XEvent *event)
2300 WMenu *menu = data;
2301 WMenu *tmp;
2302 XEvent ev;
2303 int x=menu->frame_x, y=menu->frame_y;
2304 int dx=event->xbutton.x_root, dy=event->xbutton.y_root;
2305 int i, lower;
2306 Bool started;
2308 #ifdef DEBUG
2309 printf("Moving menu\n");
2310 #endif
2312 /* can't touch the menu copy */
2313 if (menu->flags.brother)
2314 return;
2316 if (event->xbutton.button != Button1 && event->xbutton.button != Button2)
2317 return;
2319 if (event->xbutton.state & MOD_MASK) {
2320 wLowerFrame(menu->frame->core);
2321 lower = 1;
2322 } else {
2323 wRaiseFrame(menu->frame->core);
2324 lower = 0;
2326 tmp = menu;
2328 /* lower/raise all submenus */
2329 while (1) {
2330 if (tmp->selected_entry>=0 && tmp->cascades
2331 && tmp->entries[tmp->selected_entry]->cascade>=0) {
2332 tmp = tmp->cascades[tmp->entries[tmp->selected_entry]->cascade];
2333 if (!tmp || !tmp->flags.mapped)
2334 break;
2335 if (lower)
2336 wLowerFrame(tmp->frame->core);
2337 else
2338 wRaiseFrame(tmp->frame->core);
2339 } else {
2340 break;
2344 /* tear off the menu if it's a root menu or a cascade
2345 application menu */
2346 if (!menu->flags.buttoned && !menu->flags.brother
2347 && (!menu->flags.app_menu||menu->parent!=NULL)) {
2348 menu->flags.buttoned=1;
2349 wFrameWindowShowButton(menu->frame, WFF_RIGHT_BUTTON);
2350 if (menu->parent) {
2351 /* turn off selected menu entry in parent menu */
2352 selectEntry(menu->parent, -1);
2354 /* make parent map the copy in place of the original */
2355 for (i=0; i<menu->parent->cascade_no; i++) {
2356 if (menu->parent->cascades[i] == menu) {
2357 menu->parent->cascades[i] = menu->brother;
2358 break;
2364 started = False;
2365 while(1) {
2366 WMMaskEvent(dpy, ButtonMotionMask|ButtonReleaseMask|ButtonPressMask
2367 |ExposureMask, &ev);
2368 switch (ev.type) {
2369 case MotionNotify:
2370 if (started) {
2371 x += ev.xmotion.x_root - dx;
2372 y += ev.xmotion.y_root - dy;
2373 dx = ev.xmotion.x_root;
2374 dy = ev.xmotion.y_root;
2375 wMenuMove(menu, x, y, True);
2376 } else {
2377 if (abs(ev.xmotion.x_root - dx) > MOVE_THRESHOLD
2378 || abs(ev.xmotion.y_root - dy) > MOVE_THRESHOLD) {
2379 started = True;
2380 XGrabPointer(dpy, menu->frame->titlebar->window, False,
2381 ButtonMotionMask|ButtonReleaseMask
2382 |ButtonPressMask,
2383 GrabModeAsync, GrabModeAsync, None,
2384 wCursor[WCUR_MOVE], CurrentTime);
2387 break;
2389 case ButtonPress:
2390 break;
2392 case ButtonRelease:
2393 if (ev.xbutton.button != event->xbutton.button)
2394 break;
2395 #ifdef DEBUG
2396 printf("End menu move\n");
2397 #endif
2398 XUngrabPointer(dpy, CurrentTime);
2399 return;
2401 default:
2402 WMHandleEvent(&ev);
2403 break;
2409 *----------------------------------------------------------------------
2410 * menuCloseClick--
2411 * Handles mouse click on the close button of menus. The menu is
2412 * closed when the button is clicked.
2414 * Side effects:
2415 * The closed menu is reinserted at it's parent menus
2416 * cascade list.
2417 *----------------------------------------------------------------------
2419 static void
2420 menuCloseClick(WCoreWindow *sender, void *data, XEvent *event)
2422 WMenu *menu = (WMenu*)data;
2423 WMenu *parent = menu->parent;
2424 int i;
2426 if (parent) {
2427 for (i=0; i<parent->cascade_no; i++) {
2428 /* find the entry that points to the copy */
2429 if (parent->cascades[i] == menu->brother) {
2430 /* make it point to the original */
2431 parent->cascades[i] = menu;
2432 menu->parent = parent;
2433 break;
2437 wMenuUnmap(menu);
2441 void
2442 wMenuSaveState(WScreen *scr)
2444 proplist_t menus, key, value;
2445 int save_menus = 0;
2447 char buffer[256];
2449 menus = PLMakeDictionaryFromEntries(NULL, NULL, NULL);
2451 #ifndef LITE
2452 if (scr->switch_menu && scr->switch_menu->flags.buttoned) {
2453 sprintf(buffer, "%i,%i", scr->switch_menu->frame_x,
2454 scr->switch_menu->frame_y);
2455 key = PLMakeString("SwitchMenu");
2456 value = PLMakeString(buffer);
2457 PLInsertDictionaryEntry(menus, key, value);
2458 PLRelease(key);
2459 PLRelease(value);
2460 save_menus = 1;
2463 if (saveMenuRecurs(menus, scr, scr->root_menu))
2464 save_menus = 1;
2466 #endif /* !LITE */
2467 if (scr->workspace_menu && scr->workspace_menu->flags.buttoned) {
2468 sprintf(buffer, "%i,%i", scr->workspace_menu->frame_x,
2469 scr->workspace_menu->frame_y);
2470 key = PLMakeString("WorkspaceMenu");
2471 value = PLMakeString(buffer);
2472 PLInsertDictionaryEntry(menus, key, value);
2473 PLRelease(key);
2474 PLRelease(value);
2475 save_menus = 1;
2478 if (save_menus) {
2479 key = PLMakeString("Menus");
2480 PLInsertDictionaryEntry(scr->session_state, key, menus);
2481 PLRelease(key);
2483 PLRelease(menus);
2487 #ifndef LITE
2489 static Bool
2490 getMenuPath(WMenu *menu, char *buffer, int bufSize)
2492 Bool ok = True;
2493 int len = 0;
2495 if (!menu->flags.titled || !menu->frame->title[0])
2496 return False;
2498 len = strlen(menu->frame->title);
2499 if (len >= bufSize)
2500 return False;
2502 if (menu->parent) {
2503 ok = getMenuPath(menu->parent, buffer, bufSize - len - 1);
2504 if (!ok)
2505 return False;
2508 strcat(buffer, "\\");
2509 strcat(buffer, menu->frame->title);
2511 return True;
2514 static Bool
2515 saveMenuRecurs(proplist_t menus, WScreen *scr, WMenu *menu)
2517 proplist_t key, value;
2518 int save_menus = 0, i;
2519 char buffer[512];
2520 Bool ok = True;
2523 if (menu->flags.brother)
2524 menu = menu->brother;
2526 if (menu->flags.buttoned && menu != scr->switch_menu) {
2528 sprintf(buffer, "%i,%i", menu->frame_x, menu->frame_y);
2529 value = PLMakeString(buffer);
2531 buffer[0] = '\0';
2532 ok = getMenuPath(menu, buffer, 510);
2534 if (ok) {
2535 key = PLMakeString(buffer);
2536 PLInsertDictionaryEntry(menus, key, value);
2537 PLRelease(key);
2538 PLRelease(value);
2539 save_menus = 1;
2543 if (ok) {
2544 for (i = 0; i < menu->cascade_no; i++) {
2545 if (saveMenuRecurs(menus, scr, menu->cascades[i]))
2546 save_menus = 1;
2549 return save_menus;
2551 #endif /* !LITE */
2554 #define COMPLAIN(key) wwarning(_("bad value in menus state info:%s"), key)
2557 static int
2558 restoreMenu(WScreen *scr, proplist_t menu, int which)
2560 int x, y;
2561 WMenu *pmenu = NULL;
2563 if (!menu)
2564 return False;
2566 if (!PLIsString(menu)) {
2567 COMPLAIN("Position");
2568 return False;
2571 if (sscanf(PLGetString(menu), "%i,%i", &x, &y)!=2)
2572 COMPLAIN("Position");
2574 #ifndef LITE
2575 if (which & WSS_SWITCHMENU) {
2576 OpenSwitchMenu(scr, x, y, False);
2577 pmenu = scr->switch_menu;
2579 #endif /* !LITE */
2581 if (pmenu) {
2582 int width = MENUW(pmenu);
2583 int height = MENUH(pmenu);
2585 x = (x < -width) ? 0 : x;
2586 x = (x > scr->scr_width) ? scr->scr_width - width : x;
2587 y = (y < 0) ? 0 : y;
2588 y = (y > scr->scr_height) ? scr->scr_height - height : y;
2589 wMenuMove(pmenu, x, y, True);
2590 pmenu->flags.buttoned = 1;
2591 wFrameWindowShowButton(pmenu->frame, WFF_RIGHT_BUTTON);
2592 return True;
2594 return False;
2598 #ifndef LITE
2599 static int
2600 restoreMenuRecurs(WScreen *scr, proplist_t menus, WMenu *menu, char *path)
2602 proplist_t key, entry;
2603 char buffer[512];
2604 int i, x, y, res;
2606 if (strlen(path) + strlen(menu->frame->title) > 510)
2607 return False;
2609 sprintf(buffer, "%s\\%s", path, menu->frame->title);
2610 key = PLMakeString(buffer);
2611 entry = PLGetDictionaryEntry(menus, key);
2612 res = False;
2614 if (entry && PLIsString(entry)) {
2616 if (sscanf(PLGetString(entry), "%i,%i", &x, &y) != 2)
2617 COMPLAIN("Position");
2619 if (!menu->flags.mapped) {
2620 int width = MENUW(menu);
2621 int height = MENUH(menu);
2623 wMenuMapAt(menu, x, y, False);
2625 if (menu->parent) {
2626 /* make parent map the copy in place of the original */
2627 for (i=0; i<menu->parent->cascade_no; i++) {
2628 if (menu->parent->cascades[i] == menu) {
2629 menu->parent->cascades[i] = menu->brother;
2630 break;
2634 x = (x < -width) ? 0 : x;
2635 x = (x > scr->scr_width) ? scr->scr_width - width : x;
2636 y = (y < 0) ? 0 : y;
2637 y = (y > scr->scr_height) ? scr->scr_height - height : y;
2638 wMenuMove(menu, x, y, True);
2639 menu->flags.buttoned = 1;
2640 wFrameWindowShowButton(menu->frame, WFF_RIGHT_BUTTON);
2641 res = True;
2645 PLRelease(key);
2647 for (i=0; i<menu->cascade_no; i++) {
2648 if (restoreMenuRecurs(scr, menus, menu->cascades[i], buffer) != False)
2649 res = True;
2652 return res;
2654 #endif /* !LITE */
2657 void
2658 wMenuRestoreState(WScreen *scr)
2660 proplist_t menus, menu, key, skey;
2662 key = PLMakeString("Menus");
2663 menus = PLGetDictionaryEntry(scr->session_state, key);
2664 PLRelease(key);
2666 if (!menus)
2667 return;
2669 /* restore menus */
2671 skey = PLMakeString("SwitchMenu");
2672 menu = PLGetDictionaryEntry(menus, skey);
2673 restoreMenu(scr, menu, WSS_SWITCHMENU);
2675 #ifndef LITE
2676 if (!scr->root_menu) {
2677 OpenRootMenu(scr, scr->scr_width*2, 0, False);
2678 wMenuUnmap(scr->root_menu);
2680 restoreMenuRecurs(scr, menus, scr->root_menu, "");
2681 #endif /* !LITE */
2683 PLRelease(skey);
2687 void
2688 OpenWorkspaceMenu(WScreen *scr, int x, int y)
2690 WMenu *menu, *parent;
2691 WMenuEntry *entry;
2693 #ifndef LITE
2694 if (!scr->root_menu) {
2695 OpenRootMenu(scr, scr->scr_width*2, 0, False);
2696 wMenuUnmap(scr->root_menu);
2698 #endif
2699 menu = scr->workspace_menu;
2700 if (menu) {
2701 if (menu->flags.mapped) {
2702 if (!menu->flags.buttoned) {
2703 wMenuUnmap(menu);
2704 parent = menu->parent;
2705 if (parent && parent->selected_entry >= 0) {
2706 entry = parent->entries[parent->selected_entry];
2707 if (parent->cascades[entry->cascade] == menu) {
2708 selectEntry(parent, -1);
2709 wMenuMapAt(menu, x, y, False);
2712 } else {
2713 wRaiseFrame(menu->frame->core);
2714 wMenuMapCopyAt(menu, x, y);
2716 } else {
2717 wMenuMapAt(menu, x, y, False);