Removed titlebar drop shadow code and add plugin to draw text on titlebars.
[wmaker-crm.git] / src / menu.c
blobbd0b1051e4ca57bca0d4aff11a204724e46860c0
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 #ifdef DRAWSTRING_PLUGIN
130 XClearWindow(dpy, menu->frame->titlebar->window);
131 #endif
132 wFrameWindowPaint(menu->frame);
137 /************************************/
141 *----------------------------------------------------------------------
142 * wMenuCreate--
143 * Creates a new empty menu with the specified title. If main_menu
144 * is True, the created menu will be a main menu, which has some special
145 * properties such as being placed over other normal menus.
146 * If title is NULL, the menu will have no titlebar.
148 * Returns:
149 * The created menu.
150 *----------------------------------------------------------------------
152 WMenu*
153 wMenuCreate(WScreen *screen, char *title, int main_menu)
155 WMenu *menu;
156 static int brother=0;
157 int tmp, flags;
159 menu = wmalloc(sizeof(WMenu));
161 memset(menu, 0, sizeof(WMenu));
163 #ifdef SINGLE_MENULEVEL
164 tmp = WMSubmenuLevel;
165 #else
166 tmp = (main_menu ? WMMainMenuLevel : WMSubmenuLevel);
167 #endif
169 flags = WFF_SINGLE_STATE;
170 if (title) {
171 flags |= WFF_TITLEBAR|WFF_RIGHT_BUTTON;
172 menu->flags.titled = 1;
174 menu->frame =
175 wFrameWindowCreate(screen, tmp, 8, 2, 1, 1, flags,
176 screen->menu_title_texture, NULL,
177 screen->menu_title_pixel,
178 #ifdef DRAWSTRING_PLUGIN
179 W_STRING_MTITLE,
180 #endif
181 &screen->menu_title_gc,
182 &screen->menu_title_font);
184 menu->frame->core->descriptor.parent = menu;
185 menu->frame->core->descriptor.parent_type = WCLASS_MENU;
186 menu->frame->core->descriptor.handle_mousedown = menuMouseDown;
188 wFrameWindowHideButton(menu->frame, WFF_RIGHT_BUTTON);
190 if (title) {
191 menu->frame->title = wstrdup(title);
194 menu->frame->flags.justification = WTJ_LEFT;
196 menu->frame->rbutton_image = screen->b_pixmaps[WBUT_CLOSE];
198 menu->entry_no = 0;
199 menu->alloced_entries = 0;
200 menu->selected_entry = -1;
201 menu->entries = NULL;
203 menu->frame_x = screen->app_menu_x;
204 menu->frame_y = screen->app_menu_y;
206 menu->frame->child = menu;
208 menu->flags.lowered = 0;
210 /* create borders */
211 if (title) {
212 /* setup object descriptors */
213 menu->frame->on_mousedown_titlebar = menuTitleMouseDown;
214 menu->frame->on_dblclick_titlebar = menuTitleDoubleClick;
217 menu->frame->on_click_right = menuCloseClick;
220 menu->menu = wCoreCreate(menu->frame->core, 0, menu->frame->top_width,
221 menu->frame->core->width, 10);
223 menu->menu->descriptor.parent = menu;
224 menu->menu->descriptor.parent_type = WCLASS_MENU;
225 menu->menu->descriptor.handle_expose = menuExpose;
226 menu->menu->descriptor.handle_mousedown = menuMouseDown;
228 menu->menu_texture_data = None;
230 XMapWindow(dpy, menu->menu->window);
232 XFlush(dpy);
234 if (!brother) {
235 brother = 1;
236 menu->brother = wMenuCreate(screen, title, main_menu);
237 brother = 0;
238 menu->brother->flags.brother = 1;
239 menu->brother->brother = menu;
241 WMAddNotificationObserver(appearanceObserver, menu,
242 WNMenuAppearanceSettingsChanged, menu);
244 WMAddNotificationObserver(appearanceObserver, menu,
245 WNMenuTitleAppearanceSettingsChanged, menu);
248 return menu;
254 WMenu*
255 wMenuCreateForApp(WScreen *screen, char *title, int main_menu)
257 WMenu *menu;
259 menu = wMenuCreate(screen, title, main_menu);
260 if (!menu)
261 return NULL;
262 menu->flags.app_menu = 1;
263 menu->brother->flags.app_menu = 1;
265 return menu;
270 static void
271 insertEntry(WMenu *menu, WMenuEntry *entry, int index)
273 int i;
275 for (i = menu->entry_no-1; i >= index; i--) {
276 menu->entries[i]->order++;
277 menu->entries[i+1] = menu->entries[i];
279 menu->entries[index] = entry;
283 WMenuEntry*
284 wMenuInsertCallback(WMenu *menu, int index, char *text,
285 void (*callback)(WMenu *menu, WMenuEntry *entry),
286 void *clientdata)
288 WMenuEntry *entry;
290 #ifdef DEBUG
291 if (!menu) {
292 printf("Passed NULL as menu parameter to wMenuAddCallback() \n");
293 return NULL;
295 #endif
297 assert(menu->flags.brother==0);
298 menu->flags.realized = 0;
299 menu->brother->flags.realized = 0;
301 /* reallocate array if it's too small */
302 if (menu->entry_no >= menu->alloced_entries) {
303 void *tmp;
304 #ifdef DEBUG
305 puts("doing wrealloc()");
306 #endif
307 tmp = wrealloc(menu->entries,
308 sizeof(WMenuEntry)*(menu->alloced_entries+5));
309 if (tmp==NULL) {
310 wwarning(_("wrealloc() failed while trying to add menu item"));
311 return NULL;
314 menu->entries = tmp;
315 menu->alloced_entries += 5;
317 menu->brother->entries = tmp;
318 menu->brother->alloced_entries = menu->alloced_entries;
320 entry = wmalloc(sizeof(WMenuEntry));
321 memset(entry, 0, sizeof(WMenuEntry));
322 entry->flags.enabled = 1;
323 entry->text = wstrdup(text);
324 entry->cascade = -1;
325 entry->clientdata = clientdata;
326 entry->callback = callback;
327 if (index<0 || index>=menu->entry_no) {
328 entry->order = menu->entry_no;
329 menu->entries[menu->entry_no] = entry;
330 } else {
331 entry->order = index;
332 insertEntry(menu, entry, index);
335 menu->entry_no++;
336 menu->brother->entry_no = menu->entry_no;
338 return entry;
343 void
344 wMenuEntrySetCascade(WMenu *menu, WMenuEntry *entry, WMenu *cascade)
346 WMenu *brother = menu->brother;
347 int i, done;
349 assert(menu->flags.brother==0);
351 if (entry->cascade>=0) {
352 menu->flags.realized = 0;
353 brother->flags.realized = 0;
356 cascade->parent = menu;
358 cascade->brother->parent = brother;
360 done = 0;
361 for (i=0; i<menu->cascade_no; i++) {
362 if (menu->cascades[i]==NULL) {
363 menu->cascades[i] = cascade;
364 brother->cascades[i] = cascade->brother;
365 done = 1;
366 entry->cascade = i;
367 break;
370 if (!done) {
371 entry->cascade = menu->cascade_no;
373 menu->cascades = wrealloc(menu->cascades,
374 sizeof(WMenu)*(menu->cascade_no+1));
375 menu->cascades[menu->cascade_no++] = cascade;
378 brother->cascades = wrealloc(brother->cascades,
379 sizeof(WMenu)*(brother->cascade_no+1));
380 brother->cascades[brother->cascade_no++] = cascade->brother;
384 if (menu->flags.lowered) {
386 cascade->flags.lowered = 1;
387 ChangeStackingLevel(cascade->frame->core, WMNormalLevel);
389 cascade->brother->flags.lowered = 1;
390 ChangeStackingLevel(cascade->brother->frame->core, WMNormalLevel);
393 if (!menu->flags.realized)
394 wMenuRealize(menu);
398 void
399 wMenuEntryRemoveCascade(WMenu *menu, WMenuEntry *entry)
401 assert(menu->flags.brother==0);
403 /* destroy cascade menu */
404 if (entry->cascade>=0 && menu->cascades
405 && menu->cascades[entry->cascade]!=NULL) {
407 wMenuDestroy(menu->cascades[entry->cascade], True);
409 menu->cascades[entry->cascade] = NULL;
410 menu->brother->cascades[entry->cascade] = NULL;
412 entry->cascade = -1;
417 void
418 wMenuRemoveItem(WMenu *menu, int index)
420 int i;
422 if (menu->flags.brother) {
423 wMenuRemoveItem(menu->brother, index);
424 return;
427 if (index>=menu->entry_no) return;
429 /* destroy cascade menu */
430 wMenuEntryRemoveCascade(menu, menu->entries[index]);
432 /* destroy unshared data */
434 if (menu->entries[index]->text)
435 free(menu->entries[index]->text);
437 if (menu->entries[index]->rtext)
438 free(menu->entries[index]->rtext);
440 if (menu->entries[index]->free_cdata && menu->entries[index]->clientdata)
441 (*menu->entries[index]->free_cdata)(menu->entries[index]->clientdata);
443 free(menu->entries[index]);
445 for (i=index; i<menu->entry_no-1; i++) {
446 menu->entries[i+1]->order--;
447 menu->entries[i]=menu->entries[i+1];
449 menu->entry_no--;
450 menu->brother->entry_no--;
454 static Pixmap
455 renderTexture(WMenu *menu)
457 RImage *img;
458 Pixmap pix;
459 int i;
460 RColor light;
461 RColor dark;
462 RColor mid;
463 WScreen *scr = menu->menu->screen_ptr;
464 WTexture *texture = scr->menu_item_texture;
466 if (wPreferences.menu_style == MS_NORMAL) {
467 img = wTextureRenderImage(texture, menu->menu->width,
468 menu->entry_height, WREL_MENUENTRY);
469 } else {
470 img = wTextureRenderImage(texture, menu->menu->width,
471 menu->menu->height+1, WREL_MENUENTRY);
473 if (!img) {
474 wwarning(_("could not render texture: %s"),
475 RMessageForError(RErrorCode));
477 return None;
480 if (wPreferences.menu_style == MS_SINGLE_TEXTURE) {
481 light.alpha = 0;
482 light.red = light.green = light.blue = 80;
484 dark.alpha = 255;
485 dark.red = dark.green = dark.blue = 0;
487 mid.alpha = 0;
488 mid.red = mid.green = mid.blue = 40;
490 for (i = 1; i < menu->entry_no; i++) {
491 ROperateLine(img, RSubtractOperation, 0, i*menu->entry_height-2,
492 menu->menu->width-1, i*menu->entry_height-2, &mid);
494 RDrawLine(img, 0, i*menu->entry_height-1,
495 menu->menu->width-1, i*menu->entry_height-1, &dark);
497 ROperateLine(img, RAddOperation, 0, i*menu->entry_height,
498 menu->menu->width-1, i*menu->entry_height,
499 &light);
502 if (!RConvertImage(scr->rcontext, img, &pix)) {
503 wwarning(_("error rendering image:%s"), RMessageForError(RErrorCode));
505 RDestroyImage(img);
507 return pix;
511 static void
512 updateTexture(WMenu *menu)
514 WScreen *scr = menu->menu->screen_ptr;
516 /* setup background texture */
517 if (scr->menu_item_texture->any.type != WTEX_SOLID) {
518 if (!menu->flags.brother) {
519 FREE_PIXMAP(menu->menu_texture_data);
521 menu->menu_texture_data = renderTexture(menu);
523 XSetWindowBackgroundPixmap(dpy, menu->menu->window,
524 menu->menu_texture_data);
525 XClearWindow(dpy, menu->menu->window);
527 XSetWindowBackgroundPixmap(dpy, menu->brother->menu->window,
528 menu->menu_texture_data);
529 XClearWindow(dpy, menu->brother->menu->window);
531 } else {
532 XSetWindowBackground(dpy, menu->menu->window,
533 scr->menu_item_texture->any.color.pixel);
534 XClearWindow(dpy, menu->menu->window);
539 void
540 wMenuRealize(WMenu *menu)
542 int i;
543 int width, rwidth, mrwidth, mwidth;
544 int theight, twidth, eheight;
545 WScreen *scr = menu->frame->screen_ptr;
546 static int brother_done=0;
547 int flags;
549 if (!brother_done) {
550 brother_done = 1;
551 wMenuRealize(menu->brother);
552 brother_done = 0;
555 flags = WFF_SINGLE_STATE;
556 if (menu->flags.titled)
557 flags |= WFF_TITLEBAR|WFF_RIGHT_BUTTON;
559 wFrameWindowUpdateBorders(menu->frame, flags);
561 if (menu->flags.titled) {
562 twidth = WMWidthOfString(scr->menu_title_font, menu->frame->title,
563 strlen(menu->frame->title));
564 theight = menu->frame->top_width;
565 twidth += theight + (wPreferences.new_style ? 16 : 8);
566 } else {
567 twidth = 0;
568 theight = 0;
570 eheight = WMFontHeight(scr->menu_entry_font) + 6;
571 menu->entry_height = eheight;
572 mrwidth = 0;
573 mwidth = 0;
574 for (i=0; i<menu->entry_no; i++) {
575 char *text;
577 /* search widest text */
578 text = menu->entries[i]->text;
579 width = WMWidthOfString(scr->menu_entry_font, text, strlen(text))+10;
581 if (menu->entries[i]->flags.indicator) {
582 width += MENU_INDICATOR_SPACE;
585 if (width > mwidth)
586 mwidth = width;
588 /* search widest text on right */
589 text = menu->entries[i]->rtext;
590 if (text)
591 rwidth = WMWidthOfString(scr->menu_entry_font, text, strlen(text))
592 + 5;
593 else if (menu->entries[i]->cascade>=0)
594 rwidth = 16;
595 else
596 rwidth = 4;
598 if (rwidth > mrwidth)
599 mrwidth = rwidth;
601 mwidth += mrwidth;
603 if (mwidth < twidth)
604 mwidth = twidth;
607 wCoreConfigure(menu->menu, 0, theight, mwidth, menu->entry_no*eheight -1);
609 wFrameWindowResize(menu->frame, mwidth, menu->entry_no*eheight-1
610 + menu->frame->top_width + menu->frame->bottom_width);
613 updateTexture(menu);
615 menu->flags.realized = 1;
617 if (menu->flags.mapped)
618 wMenuPaint(menu);
619 if (menu->brother->flags.mapped)
620 wMenuPaint(menu->brother);
624 void
625 wMenuDestroy(WMenu *menu, int recurse)
627 int i;
629 WMRemoveNotificationObserver(menu);
631 /* remove any pending timers */
632 if (menu->timer)
633 WMDeleteTimerHandler(menu->timer);
634 menu->timer = NULL;
636 /* call destroy handler */
637 if (menu->on_destroy)
638 (*menu->on_destroy)(menu);
640 /* Destroy items if this menu own them. If this is the "brother" menu,
641 * leave them alone as it is shared by them.
643 if (!menu->flags.brother) {
644 for (i=0; i<menu->entry_no; i++) {
646 free(menu->entries[i]->text);
648 if (menu->entries[i]->rtext)
649 free(menu->entries[i]->rtext);
650 #ifdef USER_MENU
652 if (menu->entries[i]->instances){
653 PLRelease(menu->entries[i]->instances);
655 #endif /* USER_MENU */
657 if (menu->entries[i]->free_cdata && menu->entries[i]->clientdata) {
658 (*menu->entries[i]->free_cdata)(menu->entries[i]->clientdata);
660 free(menu->entries[i]);
663 if (recurse) {
664 for (i=0; i<menu->cascade_no; i++) {
665 if (menu->cascades[i]) {
666 if (menu->cascades[i]->flags.brother)
667 wMenuDestroy(menu->cascades[i]->brother, recurse);
668 else
669 wMenuDestroy(menu->cascades[i], recurse);
674 if (menu->entries)
675 free(menu->entries);
679 FREE_PIXMAP(menu->menu_texture_data);
681 if (menu->cascades)
682 free(menu->cascades);
684 wCoreDestroy(menu->menu);
685 wFrameWindowDestroy(menu->frame);
687 /* destroy copy of this menu */
688 if (!menu->flags.brother && menu->brother)
689 wMenuDestroy(menu->brother, False);
691 free(menu);
695 #define F_NORMAL 0
696 #define F_TOP 1
697 #define F_BOTTOM 2
698 #define F_NONE 3
700 static void
701 drawFrame(WScreen *scr, Window win, int y, int w, int h, int type)
703 XSegment segs[2];
704 int i;
706 i = 0;
707 segs[i].x1 = segs[i].x2 = w-1;
708 segs[i].y1 = y;
709 segs[i].y2 = y + h - 1;
710 i++;
711 if (type != F_TOP && type != F_NONE) {
712 segs[i].x1 = 1;
713 segs[i].y1 = segs[i].y2 = y + h-2;
714 segs[i].x2 = w-1;
715 i++;
717 XDrawSegments(dpy, win, scr->menu_item_auxtexture->dim_gc, segs, i);
719 i = 0;
720 segs[i].x1 = 0;
721 segs[i].y1 = y;
722 segs[i].x2 = 0;
723 segs[i].y2 = y + h - 1;
724 i++;
725 if (type != F_BOTTOM && type != F_NONE) {
726 segs[i].x1 = 0;
727 segs[i].y1 = y;
728 segs[i].x2 = w-1;
729 segs[i].y2 = y;
730 i++;
732 XDrawSegments(dpy, win, scr->menu_item_auxtexture->light_gc, segs, i);
734 if (type != F_TOP && type != F_NONE)
735 XDrawLine(dpy, win, scr->menu_item_auxtexture->dark_gc, 0, y+h-1,
736 w-1, y+h-1);
740 static void
741 paintEntry(WMenu *menu, int index, int selected)
743 int x, y, w, h, tw;
744 int type;
745 GC light, dim, dark, textGC;
746 WScreen *scr=menu->frame->screen_ptr;
747 Window win = menu->menu->window;
748 WMenuEntry *entry=menu->entries[index];
750 if (!menu->flags.realized) return;
751 h = menu->entry_height;
752 w = menu->menu->width;
753 y = index * h;
755 light = scr->menu_item_auxtexture->light_gc;
756 dim = scr->menu_item_auxtexture->dim_gc;
757 dark = scr->menu_item_auxtexture->dark_gc;
759 if (wPreferences.menu_style == MS_FLAT && menu->entry_no > 1) {
760 if (index == 0)
761 type = F_TOP;
762 else if (index == menu->entry_no - 1)
763 type = F_BOTTOM;
764 else
765 type = F_NONE;
766 } else {
767 type = F_NORMAL;
770 /* paint background */
771 if (selected) {
772 XSetForeground(dpy, scr->select_menu_gc, scr->select_pixel);
773 XFillRectangle(dpy, win, scr->select_menu_gc, 1, y+1, w-2, h-3);
774 if (scr->menu_item_texture->any.type == WTEX_SOLID)
775 drawFrame(scr, win, y, w, h, type);
776 } else {
777 if (scr->menu_item_texture->any.type == WTEX_SOLID) {
778 XClearArea(dpy, win, 0, y + 1, w - 1, h - 3, False);
779 /* draw the frame */
780 drawFrame(scr, win, y, w, h, type);
781 } else {
782 XClearArea(dpy, win, 0, y, w, h, False);
786 if (selected) {
787 textGC = scr->select_menu_gc;
788 if (entry->flags.enabled)
789 XSetForeground(dpy, textGC, scr->select_text_pixel);
790 else
791 XSetForeground(dpy, textGC, scr->dtext_pixel);
792 } else if (!entry->flags.enabled) {
793 textGC = scr->disabled_menu_entry_gc;
794 } else {
795 textGC = scr->menu_entry_gc;
797 /* draw text */
798 x = 5;
799 if (entry->flags.indicator)
800 x += MENU_INDICATOR_SPACE + 2;
802 WMDrawString(scr->wmscreen, win, textGC, scr->menu_entry_font,
803 x, 3 + y, entry->text, strlen(entry->text));
805 if (entry->cascade>=0) {
806 /* draw the cascade indicator */
807 XDrawLine(dpy,win,dim, w-11, y+6, w-6, y+h/2-1);
808 XDrawLine(dpy,win,light, w-11, y+h-8, w-6, y+h/2-1);
809 XDrawLine(dpy,win,dark, w-12, y+6, w-12, y+h-8);
812 /* draw indicator */
813 if (entry->flags.indicator && entry->flags.indicator_on) {
814 int iw, ih;
815 WPixmap *indicator;
818 switch (entry->flags.indicator_type) {
819 case MI_CHECK:
820 indicator = scr->menu_check_indicator;
821 break;
822 case MI_MINIWINDOW:
823 indicator = scr->menu_mini_indicator;
824 break;
825 case MI_HIDDEN:
826 indicator = scr->menu_hide_indicator;
827 break;
828 case MI_SHADED:
829 indicator = scr->menu_shade_indicator;
830 break;
831 case MI_DIAMOND:
832 default:
833 indicator = scr->menu_radio_indicator;
834 break;
837 iw = indicator->width;
838 ih = indicator->height;
839 XSetClipMask(dpy, scr->copy_gc, indicator->mask);
840 XSetClipOrigin(dpy, scr->copy_gc, 5, y+(h-ih)/2);
841 if (selected)
842 XSetForeground(dpy, scr->copy_gc, scr->black_pixel);
843 else
844 XSetForeground(dpy, scr->copy_gc, scr->mtext_pixel);
845 XFillRectangle(dpy, win, scr->copy_gc, 5, y+(h-ih)/2, iw, ih);
847 XCopyArea(dpy, indicator->image, win, scr->copy_gc, 0, 0,
848 iw, ih, 5, y+(h-ih)/2);
850 XSetClipOrigin(dpy, scr->copy_gc, 0, 0);
853 /* draw right text */
855 if (entry->rtext && entry->cascade<0) {
856 tw = WMWidthOfString(scr->menu_entry_font, entry->rtext,
857 strlen(entry->rtext));
859 WMDrawString(scr->wmscreen, win, textGC, scr->menu_entry_font, w-6-tw,
860 y + 3, entry->rtext, strlen(entry->rtext));
865 static void
866 move_menus(WMenu *menu, int x, int y)
868 while (menu->parent) {
869 menu = menu->parent;
870 x -= MENUW(menu);
871 if (!wPreferences.align_menus && menu->selected_entry>=0) {
872 y -= menu->selected_entry*menu->entry_height;
875 wMenuMove(menu, x, y, True);
878 static void
879 makeVisible(WMenu *menu)
881 WScreen *scr = menu->frame->screen_ptr;
882 int x1, y1, x2, y2, new_x, new_y, move;
884 if (menu->entry_no<0) return;
886 x1 = menu->frame_x;
887 y1 = menu->frame_y+menu->frame->top_width
888 + menu->selected_entry*menu->entry_height;
889 x2 = x1 + MENUW(menu);
890 y2 = y1 + menu->entry_height;
892 new_x = x1;
893 new_y = y1;
894 move = 0;
896 if (x1 < 0) {
897 new_x = 0;
898 move = 1;
899 } else if (x2 >= scr->scr_width) {
900 new_x = scr->scr_width - MENUW(menu) - 1;
901 move = 1;
904 if (y1 < 0) {
905 new_y = 0;
906 move = 1;
907 } else if (y2 >= scr->scr_height) {
908 new_y = scr->scr_height - menu->entry_height - 1;
909 move = 1;
912 new_y = new_y - menu->frame->top_width
913 - menu->selected_entry*menu->entry_height;
914 move_menus(menu, new_x, new_y);
918 static int
919 check_key(WMenu *menu, XKeyEvent *event)
921 int i, ch, s;
922 char buffer[32];
924 if (XLookupString(event, buffer, 32, NULL, NULL)<1)
925 return -1;
927 ch = toupper(buffer[0]);
929 s = (menu->selected_entry>=0 ? menu->selected_entry+1 : 0);
931 again:
932 for (i=s; i<menu->entry_no; i++) {
933 if (ch==toupper(menu->entries[i]->text[0])) {
934 return i;
937 /* no match. Retry from start, if previous started from a selected entry */
938 if (s!=0) {
939 s = 0;
940 goto again;
942 return -1;
946 static int
947 keyboardMenu(WMenu *menu)
949 XEvent event;
950 KeySym ksym=NoSymbol;
951 int done=0;
952 int index;
953 WMenuEntry *entry;
954 int old_pos_x = menu->frame_x;
955 int old_pos_y = menu->frame_y;
956 int new_x = old_pos_x, new_y = old_pos_y;
957 int scr_width = menu->frame->screen_ptr->scr_width;
958 int scr_height = menu->frame->screen_ptr->scr_height;
960 if (menu->flags.editing)
961 return False;
964 XGrabKeyboard(dpy, menu->frame->core->window, True, GrabModeAsync,
965 GrabModeAsync, CurrentTime);
967 if (menu->frame_y+menu->frame->top_width >= scr_height)
968 new_y = scr_height - menu->frame->top_width;
970 if (menu->frame_x+MENUW(menu) >= scr_width)
971 new_x = scr_width-MENUW(menu)-1;
973 move_menus(menu, new_x, new_y);
975 while (!done && menu->flags.mapped) {
976 XAllowEvents(dpy, AsyncKeyboard, CurrentTime);
977 WMMaskEvent(dpy, ExposureMask|ButtonMotionMask|ButtonPressMask
978 |ButtonReleaseMask|KeyPressMask|KeyReleaseMask
979 |SubstructureNotifyMask, &event);
981 switch (event.type) {
982 case KeyPress:
983 ksym = XLookupKeysym(&event.xkey, 0);
984 switch (ksym) {
985 case XK_Escape:
986 done = 1;
987 break;
989 case XK_Home:
990 case XK_KP_Home:
991 selectEntry(menu, 0);
992 makeVisible(menu);
993 break;
995 case XK_End:
996 case XK_KP_End:
997 selectEntry(menu, menu->entry_no-1);
998 makeVisible(menu);
999 break;
1001 case XK_Up:
1002 #ifdef ARROWLESS_KBD
1003 case XK_k:
1004 #endif
1005 case XK_KP_Up:
1006 if (menu->selected_entry <= 0)
1007 selectEntry(menu, menu->entry_no-1);
1008 else
1009 selectEntry(menu, menu->selected_entry-1);
1010 makeVisible(menu);
1011 break;
1013 case XK_Down:
1014 #ifdef ARROWLESS_KBD
1015 case XK_j:
1016 #endif
1017 case XK_KP_Down:
1018 if (menu->selected_entry<0)
1019 selectEntry(menu, 0);
1020 else if (menu->selected_entry == menu->entry_no-1)
1021 selectEntry(menu, 0);
1022 else if (menu->selected_entry < menu->entry_no-1)
1023 selectEntry(menu, menu->selected_entry+1);
1024 makeVisible(menu);
1025 break;
1027 case XK_Right:
1028 #ifdef ARROWLESS_KBD
1029 case XK_l:
1030 #endif
1031 case XK_KP_Right:
1032 if (menu->selected_entry>=0) {
1033 WMenuEntry *entry;
1034 entry = menu->entries[menu->selected_entry];
1036 if (entry->cascade >= 0 && menu->cascades
1037 && menu->cascades[entry->cascade]->entry_no > 0) {
1039 XUngrabKeyboard(dpy, CurrentTime);
1041 selectEntry(menu->cascades[entry->cascade], 0);
1042 if (!keyboardMenu(menu->cascades[entry->cascade]))
1043 done = 1;
1045 XGrabKeyboard(dpy, menu->frame->core->window, True,
1046 GrabModeAsync, GrabModeAsync,
1047 CurrentTime);
1050 break;
1052 case XK_Left:
1053 #ifdef ARROWLESS_KBD
1054 case XK_h:
1055 #endif
1056 case XK_KP_Left:
1057 if (menu->parent!=NULL && menu->parent->selected_entry>=0) {
1058 selectEntry(menu, -1);
1059 move_menus(menu, old_pos_x, old_pos_y);
1060 return True;
1062 break;
1064 case XK_Return:
1065 done = 2;
1066 break;
1068 default:
1069 index = check_key(menu, &event.xkey);
1070 if (index>=0) {
1071 selectEntry(menu, index);
1074 break;
1076 default:
1077 if (event.type==ButtonPress)
1078 done = 1;
1080 WMHandleEvent(&event);
1084 XUngrabKeyboard(dpy, CurrentTime);
1086 if (done==2 && menu->selected_entry>=0) {
1087 entry = menu->entries[menu->selected_entry];
1088 } else {
1089 entry = NULL;
1092 if (entry && entry->callback!=NULL && entry->flags.enabled
1093 && entry->cascade < 0) {
1094 #if (MENU_BLINK_COUNT > 0)
1095 int sel = menu->selected_entry;
1096 int i;
1098 for (i=0; i<MENU_BLINK_COUNT; i++) {
1099 paintEntry(menu, sel, False);
1100 XSync(dpy, 0);
1101 wusleep(MENU_BLINK_DELAY);
1102 paintEntry(menu, sel, True);
1103 XSync(dpy, 0);
1104 wusleep(MENU_BLINK_DELAY);
1106 #endif
1107 selectEntry(menu, -1);
1109 if (!menu->flags.buttoned) {
1110 wMenuUnmap(menu);
1111 move_menus(menu, old_pos_x, old_pos_y);
1113 closeCascade(menu);
1115 (*entry->callback)(menu, entry);
1116 } else {
1117 if (!menu->flags.buttoned) {
1118 wMenuUnmap(menu);
1119 move_menus(menu, old_pos_x, old_pos_y);
1121 selectEntry(menu, -1);
1125 /* returns True if returning from a submenu to a parent menu,
1126 * False if exiting from menu */
1127 return False;
1131 void
1132 wMenuMapAt(WMenu *menu, int x, int y, int keyboard)
1134 int scr_width = menu->frame->screen_ptr->scr_width;
1135 int scr_height = menu->frame->screen_ptr->scr_height;
1137 if (!menu->flags.realized) {
1138 menu->flags.realized=1;
1139 wMenuRealize(menu);
1141 if (!menu->flags.mapped) {
1142 if (wPreferences.wrap_menus) {
1143 if (x<0) x = 0;
1144 if (y<0) y = 0;
1145 if (x+MENUW(menu) > scr_width)
1146 x = scr_width - MENUW(menu);
1147 if (y+MENUH(menu) > scr_height)
1148 y = scr_height - MENUH(menu);
1151 XMoveWindow(dpy, menu->frame->core->window, x, y);
1152 menu->frame_x = x;
1153 menu->frame_y = y;
1154 XMapWindow(dpy, menu->frame->core->window);
1155 wRaiseFrame(menu->frame->core);
1156 menu->flags.mapped = 1;
1157 } else {
1158 selectEntry(menu, 0);
1161 if (keyboard)
1162 keyboardMenu(menu);
1166 void
1167 wMenuMap(WMenu *menu)
1169 if (!menu->flags.realized) {
1170 menu->flags.realized=1;
1171 wMenuRealize(menu);
1173 if (menu->flags.app_menu && menu->parent==NULL) {
1174 menu->frame_x = menu->frame->screen_ptr->app_menu_x;
1175 menu->frame_y = menu->frame->screen_ptr->app_menu_y;
1176 XMoveWindow(dpy, menu->frame->core->window, menu->frame_x, menu->frame_y);
1178 XMapWindow(dpy, menu->frame->core->window);
1179 wRaiseFrame(menu->frame->core);
1180 menu->flags.mapped = 1;
1184 void
1185 wMenuUnmap(WMenu *menu)
1187 int i;
1189 XUnmapWindow(dpy, menu->frame->core->window);
1190 if (menu->flags.titled && menu->flags.buttoned) {
1191 wFrameWindowHideButton(menu->frame, WFF_RIGHT_BUTTON);
1193 menu->flags.buttoned = 0;
1194 menu->flags.mapped = 0;
1195 menu->flags.open_to_left = 0;
1197 for (i=0; i<menu->cascade_no; i++) {
1198 if (menu->cascades[i]!=NULL
1199 && menu->cascades[i]->flags.mapped
1200 && !menu->cascades[i]->flags.buttoned) {
1202 wMenuUnmap(menu->cascades[i]);
1205 menu->selected_entry = -1;
1210 void
1211 wMenuPaint(WMenu *menu)
1213 int i;
1215 if (!menu->flags.mapped) {
1216 return;
1219 /* paint entries */
1220 for (i=0; i<menu->entry_no; i++) {
1221 paintEntry(menu, i, i==menu->selected_entry);
1226 void
1227 wMenuSetEnabled(WMenu *menu, int index, int enable)
1229 if (index>=menu->entry_no) return;
1230 menu->entries[index]->flags.enabled=enable;
1231 paintEntry(menu, index, index==menu->selected_entry);
1232 paintEntry(menu->brother, index, index==menu->selected_entry);
1236 /* ====================================================================== */
1239 static void
1240 editEntry(WMenu *menu, WMenuEntry *entry)
1242 WTextInput *text;
1243 XEvent event;
1244 WObjDescriptor *desc;
1245 char *t;
1246 int done = 0;
1247 Window old_focus;
1248 int old_revert;
1250 menu->flags.editing = 1;
1252 text = wTextCreate(menu->menu, 1, menu->entry_height * entry->order,
1253 menu->menu->width - 2, menu->entry_height - 1);
1255 wTextPutText(text, entry->text);
1256 XGetInputFocus(dpy, &old_focus, &old_revert);
1257 XSetInputFocus(dpy, text->core->window, RevertToNone, CurrentTime);
1259 if (XGrabKeyboard(dpy, text->core->window, True, GrabModeAsync,
1260 GrabModeAsync, CurrentTime)!=GrabSuccess) {
1261 wwarning("could not grab keyboard");
1262 wTextDestroy(text);
1264 wSetFocusTo(menu->frame->screen_ptr,
1265 menu->frame->screen_ptr->focused_window);
1266 return;
1270 while (!done && !text->done) {
1271 XSync(dpy, 0);
1272 XAllowEvents(dpy, AsyncKeyboard|AsyncPointer, CurrentTime);
1273 XSync(dpy, 0);
1274 WMNextEvent(dpy, &event);
1276 if (XFindContext(dpy, event.xany.window, wWinContext,
1277 (XPointer *)&desc)==XCNOENT)
1278 desc = NULL;
1280 if ((desc != NULL) && (desc->handle_anything != NULL)) {
1282 (*desc->handle_anything)(desc, &event);
1284 } else {
1285 switch (event.type) {
1286 case ButtonPress:
1287 XAllowEvents(dpy, ReplayPointer, CurrentTime);
1288 done = 1;
1290 default:
1291 WMHandleEvent(&event);
1292 break;
1297 XSetInputFocus(dpy, old_focus, old_revert, CurrentTime);
1299 wSetFocusTo(menu->frame->screen_ptr,
1300 menu->frame->screen_ptr->focused_window);
1303 t = wTextGetText(text);
1304 /* if !t, the user has canceled editing */
1305 if (t) {
1306 if (entry->text)
1307 free(entry->text);
1308 entry->text = wstrdup(t);
1310 menu->flags.realized = 0;
1312 wTextDestroy(text);
1314 XUngrabKeyboard(dpy, CurrentTime);
1316 if (t && menu->on_edit)
1317 (*menu->on_edit)(menu, entry);
1319 menu->flags.editing = 0;
1321 if (!menu->flags.realized)
1322 wMenuRealize(menu);
1326 static void
1327 selectEntry(WMenu *menu, int entry_no)
1329 WMenuEntry *entry;
1330 WMenu *submenu;
1331 int old_entry;
1333 if (menu->entries==NULL)
1334 return;
1336 if (entry_no >= menu->entry_no)
1337 return;
1339 old_entry = menu->selected_entry;
1340 menu->selected_entry = entry_no;
1342 if (old_entry!=entry_no) {
1344 /* unselect previous entry */
1345 if (old_entry>=0) {
1346 paintEntry(menu, old_entry, False);
1347 entry = menu->entries[old_entry];
1349 /* unmap cascade */
1350 if (entry->cascade>=0 && menu->cascades) {
1351 if (!menu->cascades[entry->cascade]->flags.buttoned) {
1352 wMenuUnmap(menu->cascades[entry->cascade]);
1357 if (entry_no<0) {
1358 menu->selected_entry = -1;
1359 return;
1361 entry = menu->entries[entry_no];
1363 if (entry->cascade>=0 && menu->cascades && entry->flags.enabled) {
1364 /* Callback for when the submenu is opened.
1366 submenu = menu->cascades[entry->cascade];
1367 if (submenu && submenu->flags.brother)
1368 submenu = submenu->brother;
1370 if (entry->callback) {
1371 /* Only call the callback if the submenu is not yet mapped.
1373 if (menu->flags.brother) {
1374 if (!submenu || !submenu->flags.mapped)
1375 (*entry->callback)(menu->brother, entry);
1376 } else {
1377 if (!submenu || !submenu->flags.buttoned)
1378 (*entry->callback)(menu, entry);
1382 /* the submenu menu might have changed */
1383 submenu = menu->cascades[entry->cascade];
1385 /* map cascade */
1386 if (!submenu->flags.mapped) {
1387 int x, y;
1389 if (!submenu->flags.realized)
1390 wMenuRealize(submenu);
1391 if (wPreferences.wrap_menus) {
1392 if (menu->flags.open_to_left)
1393 submenu->flags.open_to_left = 1;
1395 if (submenu->flags.open_to_left) {
1396 x = menu->frame_x - MENUW(submenu);
1397 if (x<0) {
1398 x = 0;
1399 submenu->flags.open_to_left = 0;
1401 } else {
1402 x = menu->frame_x + MENUW(menu);
1404 if (x + MENUW(submenu)
1405 >= menu->frame->screen_ptr->scr_width) {
1407 x = menu->frame_x - MENUW(submenu);
1408 submenu->flags.open_to_left = 1;
1411 } else {
1412 x = menu->frame_x + MENUW(menu);
1415 if (wPreferences.align_menus) {
1416 y = menu->frame_y;
1417 } else {
1418 y = menu->frame_y + menu->entry_height*entry_no;
1419 if (menu->flags.titled)
1420 y += menu->frame->top_width;
1421 if (menu->cascades[entry->cascade]->flags.titled)
1422 y -= menu->cascades[entry->cascade]->frame->top_width;
1425 wMenuMapAt(menu->cascades[entry->cascade], x, y, False);
1426 menu->cascades[entry->cascade]->parent = menu;
1427 } else {
1428 return;
1431 paintEntry(menu, entry_no, True);
1436 static WMenu*
1437 findMenu(WScreen *scr, int *x_ret, int *y_ret)
1439 WMenu *menu;
1440 WObjDescriptor *desc;
1441 Window root_ret, win, junk_win;
1442 int x, y, wx, wy;
1443 unsigned int mask;
1445 XQueryPointer(dpy, scr->root_win, &root_ret, &win, &x, &y, &wx, &wy,
1446 &mask);
1448 if (win==None) return NULL;
1450 if (XFindContext(dpy, win, wWinContext, (XPointer *)&desc)==XCNOENT)
1451 return NULL;
1453 if (desc->parent_type == WCLASS_MENU) {
1454 menu = (WMenu*)desc->parent;
1455 XTranslateCoordinates(dpy, root_ret, menu->menu->window, wx, wy,
1456 x_ret, y_ret, &junk_win);
1457 return menu;
1459 return NULL;
1465 static void
1466 closeCascade(WMenu *menu)
1468 WMenu *parent=menu->parent;
1470 if (menu->flags.brother
1471 || (!menu->flags.buttoned
1472 && (!menu->flags.app_menu||menu->parent!=NULL))) {
1474 selectEntry(menu, -1);
1475 XSync(dpy, 0);
1476 #if (MENU_BLINK_DELAY > 2)
1477 wusleep(MENU_BLINK_DELAY/2);
1478 #endif
1479 wMenuUnmap(menu);
1480 while (parent!=NULL
1481 && (parent->parent!=NULL || !parent->flags.app_menu
1482 || parent->flags.brother)
1483 && !parent->flags.buttoned) {
1484 selectEntry(parent, -1);
1485 wMenuUnmap(parent);
1486 parent = parent->parent;
1488 if (parent)
1489 selectEntry(parent, -1);
1494 static void
1495 closeBrotherCascadesOf(WMenu *menu)
1497 WMenu *tmp;
1498 int i;
1500 for (i=0; i<menu->cascade_no; i++) {
1501 if (menu->cascades[i]->flags.brother) {
1502 tmp = menu->cascades[i];
1503 } else {
1504 tmp = menu->cascades[i]->brother;
1506 if (tmp->flags.mapped) {
1507 selectEntry(tmp->parent, -1);
1508 closeBrotherCascadesOf(tmp);
1509 break;
1515 #define getEntryAt(menu, x, y) ((y)<0 ? -1 : (y)/(menu->entry_height))
1518 static WMenu*
1519 parentMenu(WMenu *menu)
1521 WMenu *parent;
1522 WMenuEntry *entry;
1524 if (menu->flags.buttoned)
1525 return menu;
1527 while (menu->parent && menu->parent->flags.mapped) {
1528 parent = menu->parent;
1529 if (parent->selected_entry < 0)
1530 break;
1531 entry = parent->entries[parent->selected_entry];
1532 if (!entry->flags.enabled || entry->cascade<0 || !parent->cascades ||
1533 parent->cascades[entry->cascade] != menu)
1534 break;
1535 menu = parent;
1536 if (menu->flags.buttoned)
1537 break;
1540 return menu;
1546 * Will raise the passed menu, if submenu = 0
1547 * If submenu > 0 will also raise all mapped submenus
1548 * until the first buttoned one
1549 * If submenu < 0 will also raise all mapped parent menus
1550 * until the first buttoned one
1553 static void
1554 raiseMenus(WMenu *menu, int submenus)
1556 WMenu *submenu;
1557 int i;
1559 if(!menu) return;
1561 wRaiseFrame(menu->frame->core);
1563 if (submenus>0 && menu->selected_entry>=0) {
1564 i = menu->entries[menu->selected_entry]->cascade;
1565 if (i>=0 && menu->cascades) {
1566 submenu = menu->cascades[i];
1567 if (submenu->flags.mapped && !submenu->flags.buttoned)
1568 raiseMenus(submenu, submenus);
1571 if (submenus<0 && !menu->flags.buttoned &&
1572 menu->parent && menu->parent->flags.mapped)
1573 raiseMenus(menu->parent, submenus);
1577 WMenu*
1578 wMenuUnderPointer(WScreen *screen)
1580 WObjDescriptor *desc;
1581 Window root_ret, win;
1582 int dummy;
1583 unsigned int mask;
1585 XQueryPointer(dpy, screen->root_win, &root_ret, &win, &dummy, &dummy,
1586 &dummy, &dummy, &mask);
1588 if (win==None) return NULL;
1590 if (XFindContext(dpy, win, wWinContext, (XPointer *)&desc)==XCNOENT)
1591 return NULL;
1593 if (desc->parent_type == WCLASS_MENU)
1594 return (WMenu *)desc->parent;
1595 return NULL;
1600 #define MIN(a,b) (((a) > (b)) ? (b) : (a))
1603 static void
1604 getPointerPosition(WScreen *scr, int *x, int *y)
1606 Window root_ret, win;
1607 int wx, wy;
1608 unsigned int mask;
1610 XQueryPointer(dpy, scr->root_win, &root_ret, &win, x, y, &wx, &wy, &mask);
1614 static void
1615 getScrollAmount(WMenu *menu, int *hamount, int *vamount)
1617 WScreen *scr = menu->menu->screen_ptr;
1618 int menuX1 = menu->frame_x;
1619 int menuY1 = menu->frame_y;
1620 int menuX2 = menu->frame_x + MENUW(menu);
1621 int menuY2 = menu->frame_y + MENUH(menu);
1622 int screenW = scr->scr_width;
1623 int screenH = scr->scr_height;
1624 int xroot, yroot;
1626 *hamount = 0;
1627 *vamount = 0;
1629 getPointerPosition(scr, &xroot, &yroot);
1632 if (xroot <= 1 && menuX1 < 0) {
1633 /* scroll to the right */
1634 *hamount = MIN(MENU_SCROLL_STEP, abs(menuX1));
1636 } else if (xroot >= screenW-2 && menuX2 > screenW-1) {
1637 /* scroll to the left */
1638 *hamount = MIN(MENU_SCROLL_STEP, abs(menuX2-screenW-1));
1640 if (*hamount==0)
1641 *hamount = 1;
1643 *hamount = -*hamount;
1646 if (yroot <= 1 && menuY1 < 0) {
1647 /* scroll down */
1648 *vamount = MIN(MENU_SCROLL_STEP, abs(menuY1));
1650 } else if (yroot >= screenH-2 && menuY2 > screenH-1) {
1651 /* scroll up */
1652 *vamount = MIN(MENU_SCROLL_STEP, abs(menuY2-screenH-2));
1654 *vamount = -*vamount;
1659 static void
1660 dragScrollMenuCallback(void *data)
1662 WMenu *menu = (WMenu*)data;
1663 WScreen *scr = menu->menu->screen_ptr;
1664 WMenu *parent = parentMenu(menu);
1665 int hamount, vamount;
1666 int x, y;
1667 int newSelectedEntry;
1669 getScrollAmount(menu, &hamount, &vamount);
1672 if (hamount != 0 || vamount != 0) {
1673 wMenuMove(parent, parent->frame_x + hamount,
1674 parent->frame_y + vamount, True);
1675 if (findMenu(scr, &x, &y)) {
1676 newSelectedEntry = getEntryAt(menu, x, y);
1677 selectEntry(menu, newSelectedEntry);
1678 } else {
1679 /* Pointer fell outside of menu. If the selected entry is
1680 * not a submenu, unselect it */
1681 if (menu->selected_entry >= 0
1682 && menu->entries[menu->selected_entry]->cascade<0)
1683 selectEntry(menu, -1);
1684 newSelectedEntry = 0;
1687 /* paranoid check */
1688 if (newSelectedEntry >= 0) {
1689 /* keep scrolling */
1690 menu->timer = WMAddTimerHandler(MENU_SCROLL_DELAY,
1691 dragScrollMenuCallback, menu);
1692 } else {
1693 menu->timer = NULL;
1695 } else {
1696 /* don't need to scroll anymore */
1697 menu->timer = NULL;
1698 if (findMenu(scr, &x, &y)) {
1699 newSelectedEntry = getEntryAt(menu, x, y);
1700 selectEntry(menu, newSelectedEntry);
1706 static void
1707 scrollMenuCallback(void *data)
1709 WMenu *menu = (WMenu*)data;
1710 WMenu *parent = parentMenu(menu);
1711 int hamount = 0; /* amount to scroll */
1712 int vamount = 0;
1714 getScrollAmount(menu, &hamount, &vamount);
1716 if (hamount != 0 || vamount != 0) {
1717 wMenuMove(parent, parent->frame_x + hamount,
1718 parent->frame_y + vamount, True);
1720 /* keep scrolling */
1721 menu->timer = WMAddTimerHandler(MENU_SCROLL_DELAY,
1722 scrollMenuCallback, menu);
1723 } else {
1724 /* don't need to scroll anymore */
1725 menu->timer = NULL;
1731 #define MENU_SCROLL_BORDER 5
1733 static int
1734 isPointNearBoder(WMenu *menu, int x, int y)
1736 int menuX1 = menu->frame_x;
1737 int menuY1 = menu->frame_y;
1738 int menuX2 = menu->frame_x + MENUW(menu);
1739 int menuY2 = menu->frame_y + MENUH(menu);
1740 int scrXe = menu->menu->screen_ptr->scr_width-1;
1741 int scrYe = menu->menu->screen_ptr->scr_height-1;
1742 int flag = 0;
1744 if (x >= menuX1 && x <= menuX2 && (y < MENU_SCROLL_BORDER
1745 || y > scrYe-MENU_SCROLL_BORDER))
1746 flag = 1;
1747 else if (y >= menuY1 && y <= menuY2 && (x < MENU_SCROLL_BORDER
1748 || x > scrXe-MENU_SCROLL_BORDER))
1749 flag = 1;
1751 return flag;
1755 typedef struct _delay {
1756 WWindow *wwin;
1757 WMenu *menu;
1758 int ox,oy;
1759 } _delay;
1762 static void
1763 _leaving(_delay *dl)
1765 wMenuMove(dl->menu, dl->ox, dl->oy, True);
1766 dl->menu->jump_back=NULL;
1767 dl->menu->menu->screen_ptr->flags.jump_back_pending = 0;
1768 free(dl);
1772 void
1773 wMenuScroll(WMenu *menu, XEvent *event)
1775 WMenu *smenu;
1776 WMenu *omenu = parentMenu(menu);
1777 WScreen *scr = menu->frame->screen_ptr;
1778 int done = 0;
1779 int jump_back = 0;
1780 int old_frame_x = omenu->frame_x;
1781 int old_frame_y = omenu->frame_y;
1782 XEvent ev;
1784 #ifdef DEBUG
1785 puts("Entering menu Scroll");
1786 #endif
1788 if (omenu->jump_back)
1789 WMDeleteTimerWithClientData(omenu->jump_back);
1792 if ((/*omenu->flags.buttoned &&*/ !wPreferences.wrap_menus)
1793 || omenu->flags.app_menu) {
1794 jump_back = 1;
1797 if (!wPreferences.wrap_menus)
1798 raiseMenus(omenu, True);
1799 else
1800 raiseMenus(menu, False);
1802 if (!menu->timer)
1803 scrollMenuCallback(menu);
1805 while(!done) {
1806 int x, y, on_border, on_x_edge, on_y_edge, on_title;
1808 WMNextEvent(dpy, &ev);
1809 switch (ev.type) {
1810 case EnterNotify:
1811 WMHandleEvent(&ev);
1812 case MotionNotify:
1813 x = (ev.type==MotionNotify) ? ev.xmotion.x_root : ev.xcrossing.x_root;
1814 y = (ev.type==MotionNotify) ? ev.xmotion.y_root : ev.xcrossing.y_root;
1816 /* on_border is != 0 if the pointer is between the menu
1817 * and the screen border and is close enough to the border */
1818 on_border = isPointNearBoder(menu, x, y);
1820 smenu = wMenuUnderPointer(scr);
1822 if ((smenu==NULL && !on_border) || (smenu && parentMenu(smenu)!=omenu)) {
1823 done = 1;
1824 break;
1827 on_x_edge = x <= 1 || x >= scr->scr_width - 2;
1828 on_y_edge = y <= 1 || y >= scr->scr_height - 2;
1829 on_border = on_x_edge || on_y_edge;
1831 if (!on_border && !jump_back) {
1832 done = 1;
1833 break;
1836 if (menu->timer && (smenu!=menu || (!on_y_edge && !on_x_edge))) {
1837 WMDeleteTimerHandler(menu->timer);
1838 menu->timer = NULL;
1841 if (smenu != NULL)
1842 menu = smenu;
1844 if (!menu->timer)
1845 scrollMenuCallback(menu);
1846 break;
1847 case ButtonPress:
1848 /* True if we push on title, or drag the omenu to other position */
1849 on_title = ev.xbutton.x_root >= omenu->frame_x &&
1850 ev.xbutton.x_root <= omenu->frame_x + MENUW(omenu) &&
1851 ev.xbutton.y_root >= omenu->frame_y &&
1852 ev.xbutton.y_root <= omenu->frame_y + omenu->frame->top_width;
1853 WMHandleEvent(&ev);
1854 smenu = wMenuUnderPointer(scr);
1855 if (smenu == NULL || (smenu && smenu->flags.buttoned && smenu != omenu))
1856 done = 1;
1857 else if (smenu==omenu && on_title) {
1858 jump_back = 0;
1859 done = 1;
1861 break;
1862 case KeyPress:
1863 done = 1;
1864 default:
1865 WMHandleEvent(&ev);
1866 break;
1870 if (menu->timer) {
1871 WMDeleteTimerHandler(menu->timer);
1872 menu->timer = NULL;
1875 if (jump_back) {
1876 _delay *delayer;
1877 if (!omenu->jump_back) {
1878 delayer=wmalloc(sizeof(_delay));
1879 delayer->menu=omenu;
1880 delayer->ox=old_frame_x;
1881 delayer->oy=old_frame_y;
1882 omenu->jump_back=delayer;
1883 scr->flags.jump_back_pending = 1;
1885 else delayer = omenu->jump_back;
1886 WMAddTimerHandler(MENU_JUMP_BACK_DELAY,(WMCallback*)_leaving, delayer);
1890 #ifdef DEBUG
1891 puts("Leaving menu Scroll");
1892 #endif
1897 static void
1898 menuExpose(WObjDescriptor *desc, XEvent *event)
1900 wMenuPaint(desc->parent);
1903 typedef struct {
1904 int *delayed_select;
1905 WMenu *menu;
1906 WMHandlerID magic;
1907 } delay_data;
1910 static void
1911 delaySelection(void *data)
1913 delay_data *d = (delay_data*)data;
1914 int x, y, entry_no;
1915 WMenu *menu;
1917 d->magic = NULL;
1919 menu = findMenu(d->menu->menu->screen_ptr, &x, &y);
1920 if (menu && (d->menu == menu || d->delayed_select)) {
1921 entry_no = getEntryAt(menu, x, y);
1922 selectEntry(menu, entry_no);
1924 if (d->delayed_select)
1925 *(d->delayed_select) = 0;
1929 static void
1930 menuMouseDown(WObjDescriptor *desc, XEvent *event)
1932 XButtonEvent *bev = &event->xbutton;
1933 WMenu *menu = desc->parent;
1934 WMenu *smenu;
1935 WScreen *scr=menu->frame->screen_ptr;
1936 WMenuEntry *entry=NULL;
1937 XEvent ev;
1938 int close_on_exit=0;
1939 int done=0;
1940 int delayed_select = 0;
1941 int entry_no;
1942 int x, y;
1943 int prevx, prevy;
1944 int old_frame_x = 0;
1945 int old_frame_y = 0;
1946 delay_data d_data = {NULL, NULL, NULL};
1948 if (!wPreferences.wrap_menus) {
1949 smenu = parentMenu(menu);
1950 old_frame_x = smenu->frame_x;
1951 old_frame_y = smenu->frame_y;
1952 } else if (event->xbutton.window == menu->frame->core->window) {
1953 /* This is true if the menu was launched with right click on root window */
1954 delayed_select = 1;
1955 d_data.delayed_select = &delayed_select;
1956 d_data.menu = menu;
1957 d_data.magic = WMAddTimerHandler(wPreferences.dblclick_time,
1958 delaySelection, &d_data);
1961 wRaiseFrame(menu->frame->core);
1963 close_on_exit = (bev->send_event || menu->flags.brother);
1965 smenu = findMenu(scr, &x, &y);
1966 if (!smenu) {
1967 x = -1;
1968 y = -1;
1969 } else {
1970 menu = smenu;
1973 if (menu->flags.editing) {
1974 return;
1976 entry_no = getEntryAt(menu, x, y);
1977 if (entry_no>=0) {
1978 entry = menu->entries[entry_no];
1980 if (!close_on_exit && (bev->state & ControlMask) && smenu
1981 && entry->flags.editable) {
1982 editEntry(smenu, entry);
1983 return;
1984 } else if (bev->state & ControlMask) {
1985 return;
1988 if (entry->flags.enabled && entry->cascade>=0 && menu->cascades) {
1989 WMenu *submenu = menu->cascades[entry->cascade];
1990 /* map cascade */
1991 if (submenu->flags.mapped && !submenu->flags.buttoned &&
1992 menu->selected_entry!=entry_no) {
1993 wMenuUnmap(submenu);
1995 if (!submenu->flags.mapped && !delayed_select) {
1996 selectEntry(menu, entry_no);
1997 } else if (!submenu->flags.buttoned) {
1998 selectEntry(menu, -1);
2001 } else if (!delayed_select) {
2002 selectEntry(menu, entry_no);
2005 if (!wPreferences.wrap_menus && !wPreferences.scrollable_menus) {
2006 if (!menu->timer)
2007 dragScrollMenuCallback(menu);
2011 prevx = bev->x_root;
2012 prevy = bev->y_root;
2013 while (!done) {
2014 int x, y;
2016 XAllowEvents(dpy, SyncPointer, CurrentTime);
2018 WMMaskEvent(dpy, ExposureMask|ButtonMotionMask|ButtonReleaseMask
2019 |ButtonPressMask, &ev);
2020 switch (ev.type) {
2021 case MotionNotify:
2022 smenu = findMenu(scr, &x, &y);
2024 if (smenu == NULL) {
2025 /* moved mouse out of menu */
2027 if (!delayed_select && d_data.magic) {
2028 WMDeleteTimerHandler(d_data.magic);
2029 d_data.magic = NULL;
2031 if (menu==NULL
2032 || (menu->selected_entry>=0
2033 && menu->entries[menu->selected_entry]->cascade>=0)) {
2034 prevx = ev.xmotion.x_root;
2035 prevy = ev.xmotion.y_root;
2037 break;
2039 selectEntry(menu, -1);
2040 menu = smenu;
2041 prevx = ev.xmotion.x_root;
2042 prevy = ev.xmotion.y_root;
2043 break;
2044 } else if (menu && menu!=smenu
2045 && (menu->selected_entry<0
2046 || menu->entries[menu->selected_entry]->cascade<0)) {
2047 selectEntry(menu, -1);
2049 if (!delayed_select && d_data.magic) {
2050 WMDeleteTimerHandler(d_data.magic);
2051 d_data.magic = NULL;
2053 } else {
2055 /* hysteresis for item selection */
2057 /* check if the motion was to the side, indicating that
2058 * the user may want to cross to a submenu */
2059 if (!delayed_select && menu) {
2060 int dx;
2061 Bool moved_to_submenu;/* moved to direction of submenu */
2063 dx = abs(prevx - ev.xmotion.x_root);
2065 moved_to_submenu = False;
2066 if (dx > 0 /* if moved enough to the side */
2067 /* maybe a open submenu */
2068 && menu->selected_entry>=0
2069 /* moving to the right direction */
2070 && (wPreferences.align_menus
2071 || ev.xmotion.y_root >= prevy)) {
2072 int index;
2074 index = menu->entries[menu->selected_entry]->cascade;
2075 if (index>=0) {
2076 if (menu->cascades[index]->frame_x>menu->frame_x) {
2077 if (prevx < ev.xmotion.x_root)
2078 moved_to_submenu = True;
2079 } else {
2080 if (prevx > ev.xmotion.x_root)
2081 moved_to_submenu = True;
2087 if (menu != smenu) {
2088 if (d_data.magic) {
2089 WMDeleteTimerHandler(d_data.magic);
2091 d_data.magic = NULL;
2092 } else if (moved_to_submenu) {
2093 /* while we are moving, postpone the selection */
2094 if (d_data.magic) {
2095 WMDeleteTimerHandler(d_data.magic);
2097 d_data.delayed_select = NULL;
2098 d_data.menu = menu;
2099 d_data.magic = WMAddTimerHandler(MENU_SELECT_DELAY,
2100 delaySelection,
2101 &d_data);
2102 prevx = ev.xmotion.x_root;
2103 prevy = ev.xmotion.y_root;
2104 break;
2105 } else {
2106 if (d_data.magic)
2107 WMDeleteTimerHandler(d_data.magic);
2108 d_data.magic = NULL;
2112 prevx = ev.xmotion.x_root;
2113 prevy = ev.xmotion.y_root;
2114 if (menu!=smenu) {
2115 /* pointer crossed menus */
2116 if (menu && menu->timer) {
2117 WMDeleteTimerHandler(menu->timer);
2118 menu->timer = NULL;
2120 if (smenu)
2121 dragScrollMenuCallback(smenu);
2123 menu = smenu;
2124 if (!menu->timer)
2125 dragScrollMenuCallback(menu);
2127 if (!delayed_select) {
2128 entry_no = getEntryAt(menu, x, y);
2129 if (entry_no>=0) {
2130 entry = menu->entries[entry_no];
2131 if (entry->flags.enabled && entry->cascade>=0 &&
2132 menu->cascades) {
2133 WMenu *submenu = menu->cascades[entry->cascade];
2134 if (submenu->flags.mapped && !submenu->flags.buttoned
2135 && menu->selected_entry!=entry_no) {
2136 wMenuUnmap(submenu);
2140 selectEntry(menu, entry_no);
2142 break;
2144 case ButtonPress:
2145 break;
2147 case ButtonRelease:
2148 if (ev.xbutton.button == event->xbutton.button)
2149 done=1;
2150 break;
2152 case Expose:
2153 WMHandleEvent(&ev);
2154 break;
2158 if (menu && menu->timer) {
2159 WMDeleteTimerHandler(menu->timer);
2160 menu->timer = NULL;
2162 if (d_data.magic!=NULL)
2163 WMDeleteTimerHandler(d_data.magic);
2165 if (menu && menu->selected_entry>=0) {
2166 entry = menu->entries[menu->selected_entry];
2167 if (entry->callback!=NULL && entry->flags.enabled
2168 && entry->cascade < 0) {
2169 /* blink and erase menu selection */
2170 #if (MENU_BLINK_DELAY > 0)
2171 int sel = menu->selected_entry;
2172 int i;
2174 for (i=0; i<MENU_BLINK_COUNT; i++) {
2175 paintEntry(menu, sel, False);
2176 XSync(dpy, 0);
2177 wusleep(MENU_BLINK_DELAY);
2178 paintEntry(menu, sel, True);
2179 XSync(dpy, 0);
2180 wusleep(MENU_BLINK_DELAY);
2182 #endif
2183 /* unmap the menu, it's parents and call the callback */
2184 if (!menu->flags.buttoned &&
2185 (!menu->flags.app_menu||menu->parent!=NULL)) {
2186 closeCascade(menu);
2187 } else {
2188 selectEntry(menu, -1);
2190 (*entry->callback)(menu, entry);
2192 /* If the user double clicks an entry, the entry will
2193 * be executed twice, which is not good for things like
2194 * the root menu. So, ignore any clicks that were generated
2195 * while the entry was being executed */
2196 while (XCheckTypedWindowEvent(dpy, menu->menu->window,
2197 ButtonPress, &ev));
2198 } else if (entry->callback!=NULL && entry->cascade<0) {
2199 selectEntry(menu, -1);
2200 } else {
2201 if (entry->cascade>=0 && menu->cascades
2202 && menu->cascades[entry->cascade]->flags.brother) {
2203 selectEntry(menu, -1);
2208 if (((WMenu*)desc->parent)->flags.brother || close_on_exit || !smenu)
2209 closeCascade(desc->parent);
2211 /* close the cascade windows that should not remain opened */
2212 closeBrotherCascadesOf(desc->parent);
2214 if (!wPreferences.wrap_menus)
2215 wMenuMove(parentMenu(desc->parent), old_frame_x, old_frame_y, True);
2219 void
2220 wMenuMove(WMenu *menu, int x, int y, int submenus)
2222 WMenu *submenu;
2223 int i;
2225 if (!menu) return;
2227 menu->frame_x = x;
2228 menu->frame_y = y;
2229 XMoveWindow(dpy, menu->frame->core->window, x, y);
2231 if (submenus>0 && menu->selected_entry>=0) {
2232 i = menu->entries[menu->selected_entry]->cascade;
2234 if (i>=0 && menu->cascades) {
2235 submenu = menu->cascades[i];
2236 if (submenu->flags.mapped && !submenu->flags.buttoned)
2237 if (wPreferences.align_menus) {
2238 wMenuMove(submenu, x + MENUW(menu), y, submenus);
2239 } else {
2240 wMenuMove(submenu, x+ MENUW(menu),
2241 y + submenu->entry_height*menu->selected_entry,
2242 submenus);
2246 if (submenus<0 && menu->parent!=NULL && menu->parent->flags.mapped &&
2247 !menu->parent->flags.buttoned) {
2248 if (wPreferences.align_menus) {
2249 wMenuMove(menu->parent, x - MENUW(menu->parent), y, submenus);
2250 } else {
2251 wMenuMove(menu->parent, x - MENUW(menu->parent), menu->frame_y
2252 - menu->parent->entry_height*menu->parent->selected_entry,
2253 submenus);
2259 static void
2260 changeMenuLevels(WMenu *menu, int lower)
2262 int i;
2264 if (!lower) {
2265 ChangeStackingLevel(menu->frame->core, (!menu->parent ? WMMainMenuLevel
2266 : WMSubmenuLevel));
2267 wRaiseFrame(menu->frame->core);
2268 menu->flags.lowered = 0;
2269 } else {
2270 ChangeStackingLevel(menu->frame->core, WMNormalLevel);
2271 wLowerFrame(menu->frame->core);
2272 menu->flags.lowered = 1;
2274 for (i=0; i<menu->cascade_no; i++) {
2275 if (menu->cascades[i]
2276 && !menu->cascades[i]->flags.buttoned
2277 && menu->cascades[i]->flags.lowered!=lower) {
2278 changeMenuLevels(menu->cascades[i], lower);
2285 static void
2286 menuTitleDoubleClick(WCoreWindow *sender, void *data, XEvent *event)
2288 WMenu *menu = data;
2289 int lower;
2291 if (event->xbutton.state & MOD_MASK) {
2292 if (menu->flags.lowered) {
2293 lower = 0;
2294 } else {
2295 lower = 1;
2297 changeMenuLevels(menu, lower);
2302 static void
2303 menuTitleMouseDown(WCoreWindow *sender, void *data, XEvent *event)
2305 WMenu *menu = data;
2306 WMenu *tmp;
2307 XEvent ev;
2308 int x=menu->frame_x, y=menu->frame_y;
2309 int dx=event->xbutton.x_root, dy=event->xbutton.y_root;
2310 int i, lower;
2311 Bool started;
2313 #ifdef DEBUG
2314 printf("Moving menu\n");
2315 #endif
2317 /* can't touch the menu copy */
2318 if (menu->flags.brother)
2319 return;
2321 if (event->xbutton.button != Button1 && event->xbutton.button != Button2)
2322 return;
2324 if (event->xbutton.state & MOD_MASK) {
2325 wLowerFrame(menu->frame->core);
2326 lower = 1;
2327 } else {
2328 wRaiseFrame(menu->frame->core);
2329 lower = 0;
2331 tmp = menu;
2333 /* lower/raise all submenus */
2334 while (1) {
2335 if (tmp->selected_entry>=0 && tmp->cascades
2336 && tmp->entries[tmp->selected_entry]->cascade>=0) {
2337 tmp = tmp->cascades[tmp->entries[tmp->selected_entry]->cascade];
2338 if (!tmp || !tmp->flags.mapped)
2339 break;
2340 if (lower)
2341 wLowerFrame(tmp->frame->core);
2342 else
2343 wRaiseFrame(tmp->frame->core);
2344 } else {
2345 break;
2349 /* tear off the menu if it's a root menu or a cascade
2350 application menu */
2351 if (!menu->flags.buttoned && !menu->flags.brother
2352 && (!menu->flags.app_menu||menu->parent!=NULL)) {
2353 menu->flags.buttoned=1;
2354 wFrameWindowShowButton(menu->frame, WFF_RIGHT_BUTTON);
2355 if (menu->parent) {
2356 /* turn off selected menu entry in parent menu */
2357 selectEntry(menu->parent, -1);
2359 /* make parent map the copy in place of the original */
2360 for (i=0; i<menu->parent->cascade_no; i++) {
2361 if (menu->parent->cascades[i] == menu) {
2362 menu->parent->cascades[i] = menu->brother;
2363 break;
2369 started = False;
2370 while(1) {
2371 WMMaskEvent(dpy, ButtonMotionMask|ButtonReleaseMask|ButtonPressMask
2372 |ExposureMask, &ev);
2373 switch (ev.type) {
2374 case MotionNotify:
2375 if (started) {
2376 x += ev.xmotion.x_root - dx;
2377 y += ev.xmotion.y_root - dy;
2378 dx = ev.xmotion.x_root;
2379 dy = ev.xmotion.y_root;
2380 wMenuMove(menu, x, y, True);
2381 } else {
2382 if (abs(ev.xmotion.x_root - dx) > MOVE_THRESHOLD
2383 || abs(ev.xmotion.y_root - dy) > MOVE_THRESHOLD) {
2384 started = True;
2385 XGrabPointer(dpy, menu->frame->titlebar->window, False,
2386 ButtonMotionMask|ButtonReleaseMask
2387 |ButtonPressMask,
2388 GrabModeAsync, GrabModeAsync, None,
2389 wCursor[WCUR_MOVE], CurrentTime);
2392 break;
2394 case ButtonPress:
2395 break;
2397 case ButtonRelease:
2398 if (ev.xbutton.button != event->xbutton.button)
2399 break;
2400 #ifdef DEBUG
2401 printf("End menu move\n");
2402 #endif
2403 XUngrabPointer(dpy, CurrentTime);
2404 return;
2406 default:
2407 WMHandleEvent(&ev);
2408 break;
2414 *----------------------------------------------------------------------
2415 * menuCloseClick--
2416 * Handles mouse click on the close button of menus. The menu is
2417 * closed when the button is clicked.
2419 * Side effects:
2420 * The closed menu is reinserted at it's parent menus
2421 * cascade list.
2422 *----------------------------------------------------------------------
2424 static void
2425 menuCloseClick(WCoreWindow *sender, void *data, XEvent *event)
2427 WMenu *menu = (WMenu*)data;
2428 WMenu *parent = menu->parent;
2429 int i;
2431 if (parent) {
2432 for (i=0; i<parent->cascade_no; i++) {
2433 /* find the entry that points to the copy */
2434 if (parent->cascades[i] == menu->brother) {
2435 /* make it point to the original */
2436 parent->cascades[i] = menu;
2437 menu->parent = parent;
2438 break;
2442 wMenuUnmap(menu);
2446 void
2447 wMenuSaveState(WScreen *scr)
2449 proplist_t menus, key, value;
2450 int save_menus = 0;
2452 char buffer[256];
2454 menus = PLMakeDictionaryFromEntries(NULL, NULL, NULL);
2456 #ifndef LITE
2457 if (scr->switch_menu && scr->switch_menu->flags.buttoned) {
2458 sprintf(buffer, "%i,%i", scr->switch_menu->frame_x,
2459 scr->switch_menu->frame_y);
2460 key = PLMakeString("SwitchMenu");
2461 value = PLMakeString(buffer);
2462 PLInsertDictionaryEntry(menus, key, value);
2463 PLRelease(key);
2464 PLRelease(value);
2465 save_menus = 1;
2468 if (saveMenuRecurs(menus, scr, scr->root_menu))
2469 save_menus = 1;
2471 #endif /* !LITE */
2472 if (scr->workspace_menu && scr->workspace_menu->flags.buttoned) {
2473 sprintf(buffer, "%i,%i", scr->workspace_menu->frame_x,
2474 scr->workspace_menu->frame_y);
2475 key = PLMakeString("WorkspaceMenu");
2476 value = PLMakeString(buffer);
2477 PLInsertDictionaryEntry(menus, key, value);
2478 PLRelease(key);
2479 PLRelease(value);
2480 save_menus = 1;
2483 if (save_menus) {
2484 key = PLMakeString("Menus");
2485 PLInsertDictionaryEntry(scr->session_state, key, menus);
2486 PLRelease(key);
2488 PLRelease(menus);
2492 #ifndef LITE
2494 static Bool
2495 getMenuPath(WMenu *menu, char *buffer, int bufSize)
2497 Bool ok = True;
2498 int len = 0;
2500 if (!menu->flags.titled || !menu->frame->title[0])
2501 return False;
2503 len = strlen(menu->frame->title);
2504 if (len >= bufSize)
2505 return False;
2507 if (menu->parent) {
2508 ok = getMenuPath(menu->parent, buffer, bufSize - len - 1);
2509 if (!ok)
2510 return False;
2513 strcat(buffer, "\\");
2514 strcat(buffer, menu->frame->title);
2516 return True;
2519 static Bool
2520 saveMenuRecurs(proplist_t menus, WScreen *scr, WMenu *menu)
2522 proplist_t key, value;
2523 int save_menus = 0, i;
2524 char buffer[512];
2525 Bool ok = True;
2528 if (menu->flags.brother)
2529 menu = menu->brother;
2531 if (menu->flags.buttoned && menu != scr->switch_menu) {
2533 sprintf(buffer, "%i,%i", menu->frame_x, menu->frame_y);
2534 value = PLMakeString(buffer);
2536 buffer[0] = '\0';
2537 ok = getMenuPath(menu, buffer, 510);
2539 if (ok) {
2540 key = PLMakeString(buffer);
2541 PLInsertDictionaryEntry(menus, key, value);
2542 PLRelease(key);
2543 PLRelease(value);
2544 save_menus = 1;
2548 if (ok) {
2549 for (i = 0; i < menu->cascade_no; i++) {
2550 if (saveMenuRecurs(menus, scr, menu->cascades[i]))
2551 save_menus = 1;
2554 return save_menus;
2556 #endif /* !LITE */
2559 #define COMPLAIN(key) wwarning(_("bad value in menus state info:%s"), key)
2562 static int
2563 restoreMenu(WScreen *scr, proplist_t menu, int which)
2565 int x, y;
2566 WMenu *pmenu = NULL;
2568 if (!menu)
2569 return False;
2571 if (!PLIsString(menu)) {
2572 COMPLAIN("Position");
2573 return False;
2576 if (sscanf(PLGetString(menu), "%i,%i", &x, &y)!=2)
2577 COMPLAIN("Position");
2579 #ifndef LITE
2580 if (which & WSS_SWITCHMENU) {
2581 OpenSwitchMenu(scr, x, y, False);
2582 pmenu = scr->switch_menu;
2584 #endif /* !LITE */
2586 if (pmenu) {
2587 int width = MENUW(pmenu);
2588 int height = MENUH(pmenu);
2590 x = (x < -width) ? 0 : x;
2591 x = (x > scr->scr_width) ? scr->scr_width - width : x;
2592 y = (y < 0) ? 0 : y;
2593 y = (y > scr->scr_height) ? scr->scr_height - height : y;
2594 wMenuMove(pmenu, x, y, True);
2595 pmenu->flags.buttoned = 1;
2596 wFrameWindowShowButton(pmenu->frame, WFF_RIGHT_BUTTON);
2597 return True;
2599 return False;
2603 #ifndef LITE
2604 static int
2605 restoreMenuRecurs(WScreen *scr, proplist_t menus, WMenu *menu, char *path)
2607 proplist_t key, entry;
2608 char buffer[512];
2609 int i, x, y, res;
2611 if (strlen(path) + strlen(menu->frame->title) > 510)
2612 return False;
2614 sprintf(buffer, "%s\\%s", path, menu->frame->title);
2615 key = PLMakeString(buffer);
2616 entry = PLGetDictionaryEntry(menus, key);
2617 res = False;
2619 if (entry && PLIsString(entry)) {
2621 if (sscanf(PLGetString(entry), "%i,%i", &x, &y) != 2)
2622 COMPLAIN("Position");
2624 if (!menu->flags.mapped) {
2625 int width = MENUW(menu);
2626 int height = MENUH(menu);
2628 wMenuMapAt(menu, x, y, False);
2630 if (menu->parent) {
2631 /* make parent map the copy in place of the original */
2632 for (i=0; i<menu->parent->cascade_no; i++) {
2633 if (menu->parent->cascades[i] == menu) {
2634 menu->parent->cascades[i] = menu->brother;
2635 break;
2639 x = (x < -width) ? 0 : x;
2640 x = (x > scr->scr_width) ? scr->scr_width - width : x;
2641 y = (y < 0) ? 0 : y;
2642 y = (y > scr->scr_height) ? scr->scr_height - height : y;
2643 wMenuMove(menu, x, y, True);
2644 menu->flags.buttoned = 1;
2645 wFrameWindowShowButton(menu->frame, WFF_RIGHT_BUTTON);
2646 res = True;
2650 PLRelease(key);
2652 for (i=0; i<menu->cascade_no; i++) {
2653 if (restoreMenuRecurs(scr, menus, menu->cascades[i], buffer) != False)
2654 res = True;
2657 return res;
2659 #endif /* !LITE */
2662 void
2663 wMenuRestoreState(WScreen *scr)
2665 proplist_t menus, menu, key, skey;
2667 key = PLMakeString("Menus");
2668 menus = PLGetDictionaryEntry(scr->session_state, key);
2669 PLRelease(key);
2671 if (!menus)
2672 return;
2674 /* restore menus */
2676 skey = PLMakeString("SwitchMenu");
2677 menu = PLGetDictionaryEntry(menus, skey);
2678 restoreMenu(scr, menu, WSS_SWITCHMENU);
2680 #ifndef LITE
2681 if (!scr->root_menu) {
2682 OpenRootMenu(scr, scr->scr_width*2, 0, False);
2683 wMenuUnmap(scr->root_menu);
2685 restoreMenuRecurs(scr, menus, scr->root_menu, "");
2686 #endif /* !LITE */
2688 PLRelease(skey);
2692 void
2693 OpenWorkspaceMenu(WScreen *scr, int x, int y)
2695 WMenu *menu, *parent;
2696 WMenuEntry *entry;
2698 #ifndef LITE
2699 if (!scr->root_menu) {
2700 OpenRootMenu(scr, scr->scr_width*2, 0, False);
2701 wMenuUnmap(scr->root_menu);
2703 #endif
2704 menu = scr->workspace_menu;
2705 if (menu) {
2706 if (menu->flags.mapped) {
2707 if (!menu->flags.buttoned) {
2708 wMenuUnmap(menu);
2709 parent = menu->parent;
2710 if (parent && parent->selected_entry >= 0) {
2711 entry = parent->entries[parent->selected_entry];
2712 if (parent->cascades[entry->cascade] == menu) {
2713 selectEntry(parent, -1);
2714 wMenuMapAt(menu, x, y, False);
2717 } else {
2718 wRaiseFrame(menu->frame->core);
2719 wMenuMapCopyAt(menu, x, y);
2721 } else {
2722 wMenuMapAt(menu, x, y, False);