makefile.am for icons
[wmaker-crm.git] / src / menu.c
blob582aa2299febf3cafd230f194bf035cb3e133c9d
1 /* menu.c- generic menu, used for root menu, application menus etc.
3 * Window Maker window manager
4 *
5 * Copyright (c) 1997, 1998 Alfredo K. Kojima
6 * Copyright (c) 1997, 1998 Dan Pascu
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
21 * USA.
24 #include "wconfig.h"
26 #include <X11/Xlib.h>
27 #include <X11/Xutil.h>
28 #include <X11/keysym.h>
29 #include <stdlib.h>
30 #include <string.h>
31 #include <stdio.h>
32 #include <unistd.h>
33 #include <ctype.h>
34 #include <nana.h>
36 #include "WindowMaker.h"
37 #include "wcore.h"
38 #include "framewin.h"
39 #include "menu.h"
40 #include "actions.h"
41 #include "funcs.h"
42 #include "stacking.h"
43 #include "text.h"
46 /****** Global Variables ******/
48 extern Cursor wCursor[WCUR_LAST];
50 extern XContext wWinContext;
52 extern WPreferences wPreferences;
54 #define MOD_MASK wPreferences.modifier_mask
56 #define MENU_SCROLL_STEP menuScrollParameters[(int)wPreferences.menu_scroll_speed].steps
57 #define MENU_SCROLL_DELAY menuScrollParameters[(int)wPreferences.menu_scroll_speed].delay
61 #define MENUW(m) ((m)->frame->core->width+2*FRAME_BORDER_WIDTH)
62 #define MENUH(m) ((m)->frame->core->height+2*FRAME_BORDER_WIDTH)
65 /***** Local Stuff ******/
67 static struct {
68 int steps;
69 int delay;
70 } menuScrollParameters[5] = {
71 {MENU_SCROLL_STEPS_UF, MENU_SCROLL_DELAY_UF},
72 {MENU_SCROLL_STEPS_F, MENU_SCROLL_DELAY_F},
73 {MENU_SCROLL_STEPS_M, MENU_SCROLL_DELAY_M},
74 {MENU_SCROLL_STEPS_S, MENU_SCROLL_DELAY_S},
75 {MENU_SCROLL_STEPS_U, MENU_SCROLL_DELAY_U}};
78 static void menuMouseDown(WObjDescriptor *desc, XEvent *event);
79 static void menuExpose(WObjDescriptor *desc, XEvent *event);
81 static void menuTitleDoubleClick(WCoreWindow *sender, void *data, XEvent *event);
82 static void menuTitleMouseDown(WCoreWindow *sender, void *data, XEvent *event);
84 static void menuCloseClick(WCoreWindow *sender, void *data, XEvent *event);
86 static void updateTexture(WMenu *menu);
88 #ifndef LITE
89 static int saveMenuRecurs(proplist_t menus, WScreen *scr, WMenu *menu);
90 static int restoreMenuRecurs(WScreen *scr, proplist_t menus, WMenu *menu, char *path);
91 #endif /* !LITE */
93 static void selectEntry(WMenu *menu, int entry_no);
94 static void closeCascade(WMenu *menu);
97 /****** Notification Observers ******/
99 static void
100 appearanceObserver(void *self, WMNotification *notif)
102 WMenu *menu = (WMenu*)self;
103 int flags = (int)WMGetNotificationClientData(notif);
105 if (!menu->flags.realized)
106 return;
108 if (WMGetNotificationName(notif) == WNMenuAppearanceSettingsChanged) {
109 if (flags & WFontSettings) {
110 menu->flags.realized = 0;
111 wMenuRealize(menu);
113 if (flags & WTextureSettings) {
114 if (!menu->flags.brother)
115 updateTexture(menu);
117 if (flags & (WTextureSettings|WColorSettings)) {
118 wMenuPaint(menu);
120 } else if (menu->flags.titled) {
122 if (flags & WFontSettings) {
123 menu->flags.realized = 0;
124 wMenuRealize(menu);
126 if (flags & WTextureSettings) {
127 menu->frame->flags.need_texture_remake = 1;
129 if (flags & (WColorSettings|WTextureSettings)) {
130 wFrameWindowPaint(menu->frame);
135 /************************************/
139 *----------------------------------------------------------------------
140 * wMenuCreate--
141 * Creates a new empty menu with the specified title. If main_menu
142 * is True, the created menu will be a main menu, which has some special
143 * properties such as being placed over other normal menus.
144 * If title is NULL, the menu will have no titlebar.
146 * Returns:
147 * The created menu.
148 *----------------------------------------------------------------------
150 WMenu*
151 wMenuCreate(WScreen *screen, char *title, int main_menu)
153 WMenu *menu;
154 static int brother=0;
155 int tmp, flags;
157 menu = wmalloc(sizeof(WMenu));
159 memset(menu, 0, sizeof(WMenu));
161 #ifdef SINGLE_MENULEVEL
162 tmp = WMSubmenuLevel;
163 #else
164 tmp = (main_menu ? WMMainMenuLevel : WMSubmenuLevel);
165 #endif
167 flags = WFF_SINGLE_STATE|WFF_BORDER;
168 if (title) {
169 flags |= WFF_TITLEBAR|WFF_RIGHT_BUTTON;
170 menu->flags.titled = 1;
172 menu->frame =
173 wFrameWindowCreate(screen, tmp, 8, 2, 1, 1, &wPreferences.menu_title_clearance, flags,
174 screen->menu_title_texture, NULL,
175 screen->menu_title_pixel,
176 #ifdef DRAWSTRING_PLUGIN
177 W_STRING_MTITLE,
178 #endif
179 &screen->menu_title_gc,
180 &screen->menu_title_font);
182 menu->frame->core->descriptor.parent = menu;
183 menu->frame->core->descriptor.parent_type = WCLASS_MENU;
184 menu->frame->core->descriptor.handle_mousedown = menuMouseDown;
186 wFrameWindowHideButton(menu->frame, WFF_RIGHT_BUTTON);
188 if (title) {
189 menu->frame->title = wstrdup(title);
192 menu->frame->flags.justification = WTJ_LEFT;
194 menu->frame->rbutton_image = screen->b_pixmaps[WBUT_CLOSE];
196 menu->entry_no = 0;
197 menu->alloced_entries = 0;
198 menu->selected_entry = -1;
199 menu->entries = NULL;
201 menu->frame_x = screen->app_menu_x;
202 menu->frame_y = screen->app_menu_y;
204 menu->frame->child = menu;
206 menu->flags.lowered = 0;
208 /* create borders */
209 if (title) {
210 /* setup object descriptors */
211 menu->frame->on_mousedown_titlebar = menuTitleMouseDown;
212 menu->frame->on_dblclick_titlebar = menuTitleDoubleClick;
215 menu->frame->on_click_right = menuCloseClick;
218 menu->menu = wCoreCreate(menu->frame->core, 0, menu->frame->top_width,
219 menu->frame->core->width, 10);
221 menu->menu->descriptor.parent = menu;
222 menu->menu->descriptor.parent_type = WCLASS_MENU;
223 menu->menu->descriptor.handle_expose = menuExpose;
224 menu->menu->descriptor.handle_mousedown = menuMouseDown;
226 menu->menu_texture_data = None;
228 XMapWindow(dpy, menu->menu->window);
230 XFlush(dpy);
232 if (!brother) {
233 brother = 1;
234 menu->brother = wMenuCreate(screen, title, main_menu);
235 brother = 0;
236 menu->brother->flags.brother = 1;
237 menu->brother->brother = menu;
239 WMAddNotificationObserver(appearanceObserver, menu,
240 WNMenuAppearanceSettingsChanged, menu);
242 WMAddNotificationObserver(appearanceObserver, menu,
243 WNMenuTitleAppearanceSettingsChanged, menu);
246 return menu;
252 WMenu*
253 wMenuCreateForApp(WScreen *screen, char *title, int main_menu)
255 WMenu *menu;
257 menu = wMenuCreate(screen, title, main_menu);
258 if (!menu)
259 return NULL;
260 menu->flags.app_menu = 1;
261 menu->brother->flags.app_menu = 1;
263 return menu;
268 static void
269 insertEntry(WMenu *menu, WMenuEntry *entry, int index)
271 int i;
273 for (i = menu->entry_no-1; i >= index; i--) {
274 menu->entries[i]->order++;
275 menu->entries[i+1] = menu->entries[i];
277 menu->entries[index] = entry;
281 WMenuEntry*
282 wMenuInsertCallback(WMenu *menu, int index, char *text,
283 void (*callback)(WMenu *menu, WMenuEntry *entry),
284 void *clientdata)
286 WMenuEntry *entry;
288 menu->flags.realized = 0;
289 menu->brother->flags.realized = 0;
291 /* reallocate array if it's too small */
292 if (menu->entry_no >= menu->alloced_entries) {
293 void *tmp;
295 tmp = wrealloc(menu->entries,
296 sizeof(WMenuEntry)*(menu->alloced_entries+5));
298 menu->entries = tmp;
299 menu->alloced_entries += 5;
301 menu->brother->entries = tmp;
302 menu->brother->alloced_entries = menu->alloced_entries;
304 entry = wmalloc(sizeof(WMenuEntry));
305 memset(entry, 0, sizeof(WMenuEntry));
306 entry->flags.enabled = 1;
307 entry->text = wstrdup(text);
308 entry->cascade = -1;
309 entry->clientdata = clientdata;
310 entry->callback = callback;
311 if (index<0 || index>=menu->entry_no) {
312 entry->order = menu->entry_no;
313 menu->entries[menu->entry_no] = entry;
314 } else {
315 entry->order = index;
316 insertEntry(menu, entry, index);
319 menu->entry_no++;
320 menu->brother->entry_no = menu->entry_no;
322 return entry;
327 void
328 wMenuEntrySetCascade(WMenu *menu, WMenuEntry *entry, WMenu *cascade)
330 WMenu *brother = menu->brother;
331 int i, done;
333 assert(menu->flags.brother==0);
335 if (entry->cascade>=0) {
336 menu->flags.realized = 0;
337 brother->flags.realized = 0;
340 cascade->parent = menu;
342 cascade->brother->parent = brother;
344 done = 0;
345 for (i=0; i<menu->cascade_no; i++) {
346 if (menu->cascades[i]==NULL) {
347 menu->cascades[i] = cascade;
348 brother->cascades[i] = cascade->brother;
349 done = 1;
350 entry->cascade = i;
351 break;
354 if (!done) {
355 entry->cascade = menu->cascade_no;
357 menu->cascades = wrealloc(menu->cascades,
358 sizeof(WMenu)*(menu->cascade_no+1));
359 menu->cascades[menu->cascade_no++] = cascade;
362 brother->cascades = wrealloc(brother->cascades,
363 sizeof(WMenu)*(brother->cascade_no+1));
364 brother->cascades[brother->cascade_no++] = cascade->brother;
368 if (menu->flags.lowered) {
370 cascade->flags.lowered = 1;
371 ChangeStackingLevel(cascade->frame->core, WMNormalLevel);
373 cascade->brother->flags.lowered = 1;
374 ChangeStackingLevel(cascade->brother->frame->core, WMNormalLevel);
377 if (!menu->flags.realized)
378 wMenuRealize(menu);
382 void
383 wMenuEntryRemoveCascade(WMenu *menu, WMenuEntry *entry)
385 assert(menu->flags.brother==0);
387 /* destroy cascade menu */
388 if (entry->cascade>=0 && menu->cascades
389 && menu->cascades[entry->cascade]!=NULL) {
391 wMenuDestroy(menu->cascades[entry->cascade], True);
393 menu->cascades[entry->cascade] = NULL;
394 menu->brother->cascades[entry->cascade] = NULL;
396 entry->cascade = -1;
401 void
402 wMenuRemoveItem(WMenu *menu, int index)
404 int i;
406 if (menu->flags.brother) {
407 wMenuRemoveItem(menu->brother, index);
408 return;
411 if (index>=menu->entry_no) return;
413 /* destroy cascade menu */
414 wMenuEntryRemoveCascade(menu, menu->entries[index]);
416 /* destroy unshared data */
418 if (menu->entries[index]->text)
419 free(menu->entries[index]->text);
421 if (menu->entries[index]->rtext)
422 free(menu->entries[index]->rtext);
424 if (menu->entries[index]->free_cdata && menu->entries[index]->clientdata)
425 (*menu->entries[index]->free_cdata)(menu->entries[index]->clientdata);
427 free(menu->entries[index]);
429 for (i=index; i<menu->entry_no-1; i++) {
430 menu->entries[i+1]->order--;
431 menu->entries[i]=menu->entries[i+1];
433 menu->entry_no--;
434 menu->brother->entry_no--;
438 static Pixmap
439 renderTexture(WMenu *menu)
441 RImage *img;
442 Pixmap pix;
443 int i;
444 RColor light;
445 RColor dark;
446 RColor mid;
447 WScreen *scr = menu->menu->screen_ptr;
448 WTexture *texture = scr->menu_item_texture;
450 if (wPreferences.menu_style == MS_NORMAL) {
451 img = wTextureRenderImage(texture, menu->menu->width,
452 menu->entry_height, WREL_MENUENTRY);
453 } else {
454 img = wTextureRenderImage(texture, menu->menu->width,
455 menu->menu->height+1, WREL_MENUENTRY);
457 if (!img) {
458 wwarning(_("could not render texture: %s"),
459 RMessageForError(RErrorCode));
461 return None;
464 if (wPreferences.menu_style == MS_SINGLE_TEXTURE) {
465 light.alpha = 0;
466 light.red = light.green = light.blue = 80;
468 dark.alpha = 255;
469 dark.red = dark.green = dark.blue = 0;
471 mid.alpha = 0;
472 mid.red = mid.green = mid.blue = 40;
474 for (i = 1; i < menu->entry_no; i++) {
475 ROperateLine(img, RSubtractOperation, 0, i*menu->entry_height-2,
476 menu->menu->width-1, i*menu->entry_height-2, &mid);
478 RDrawLine(img, 0, i*menu->entry_height-1,
479 menu->menu->width-1, i*menu->entry_height-1, &dark);
481 ROperateLine(img, RAddOperation, 0, i*menu->entry_height,
482 menu->menu->width-1, i*menu->entry_height,
483 &light);
486 if (!RConvertImage(scr->rcontext, img, &pix)) {
487 wwarning(_("error rendering image:%s"), RMessageForError(RErrorCode));
489 RDestroyImage(img);
491 return pix;
495 static void
496 updateTexture(WMenu *menu)
498 WScreen *scr = menu->menu->screen_ptr;
500 /* setup background texture */
501 if (scr->menu_item_texture->any.type != WTEX_SOLID) {
502 if (!menu->flags.brother) {
503 FREE_PIXMAP(menu->menu_texture_data);
505 menu->menu_texture_data = renderTexture(menu);
507 XSetWindowBackgroundPixmap(dpy, menu->menu->window,
508 menu->menu_texture_data);
509 XClearWindow(dpy, menu->menu->window);
511 XSetWindowBackgroundPixmap(dpy, menu->brother->menu->window,
512 menu->menu_texture_data);
513 XClearWindow(dpy, menu->brother->menu->window);
515 } else {
516 XSetWindowBackground(dpy, menu->menu->window,
517 scr->menu_item_texture->any.color.pixel);
518 XClearWindow(dpy, menu->menu->window);
523 void
524 wMenuRealize(WMenu *menu)
526 int i;
527 int width, rwidth, mrwidth, mwidth;
528 int theight, twidth, eheight;
529 WScreen *scr = menu->frame->screen_ptr;
530 static int brother_done=0;
531 int flags;
533 if (!brother_done) {
534 brother_done = 1;
535 wMenuRealize(menu->brother);
536 brother_done = 0;
539 flags = WFF_SINGLE_STATE|WFF_BORDER;
540 if (menu->flags.titled)
541 flags |= WFF_TITLEBAR|WFF_RIGHT_BUTTON;
543 wFrameWindowUpdateBorders(menu->frame, flags);
545 if (menu->flags.titled) {
546 twidth = WMWidthOfString(scr->menu_title_font, menu->frame->title,
547 strlen(menu->frame->title));
548 theight = menu->frame->top_width;
549 twidth += theight + (wPreferences.new_style ? 16 : 8);
550 } else {
551 twidth = 0;
552 theight = 0;
554 eheight = WMFontHeight(scr->menu_entry_font) + 6 + wPreferences.menu_text_clearance * 2;
555 menu->entry_height = eheight;
556 mrwidth = 0;
557 mwidth = 0;
558 for (i=0; i<menu->entry_no; i++) {
559 char *text;
561 /* search widest text */
562 text = menu->entries[i]->text;
563 width = WMWidthOfString(scr->menu_entry_font, text, strlen(text))+10;
565 if (menu->entries[i]->flags.indicator) {
566 width += MENU_INDICATOR_SPACE;
569 if (width > mwidth)
570 mwidth = width;
572 /* search widest text on right */
573 text = menu->entries[i]->rtext;
574 if (text)
575 rwidth = WMWidthOfString(scr->menu_entry_font, text, strlen(text))
576 + 5;
577 else if (menu->entries[i]->cascade>=0)
578 rwidth = 16;
579 else
580 rwidth = 4;
582 if (rwidth > mrwidth)
583 mrwidth = rwidth;
585 mwidth += mrwidth;
587 if (mwidth < twidth)
588 mwidth = twidth;
591 wCoreConfigure(menu->menu, 0, theight, mwidth, menu->entry_no*eheight -1);
593 wFrameWindowResize(menu->frame, mwidth, menu->entry_no*eheight-1
594 + menu->frame->top_width + menu->frame->bottom_width);
597 updateTexture(menu);
599 menu->flags.realized = 1;
601 if (menu->flags.mapped)
602 wMenuPaint(menu);
603 if (menu->brother->flags.mapped)
604 wMenuPaint(menu->brother);
608 void
609 wMenuDestroy(WMenu *menu, int recurse)
611 int i;
613 WMRemoveNotificationObserver(menu);
615 /* remove any pending timers */
616 if (menu->timer)
617 WMDeleteTimerHandler(menu->timer);
618 menu->timer = NULL;
620 /* call destroy handler */
621 if (menu->on_destroy)
622 (*menu->on_destroy)(menu);
624 /* Destroy items if this menu own them. If this is the "brother" menu,
625 * leave them alone as it is shared by them.
627 if (!menu->flags.brother) {
628 for (i=0; i<menu->entry_no; i++) {
630 free(menu->entries[i]->text);
632 if (menu->entries[i]->rtext)
633 free(menu->entries[i]->rtext);
634 #ifdef USER_MENU
636 if (menu->entries[i]->instances){
637 PLRelease(menu->entries[i]->instances);
639 #endif /* USER_MENU */
641 if (menu->entries[i]->free_cdata && menu->entries[i]->clientdata) {
642 (*menu->entries[i]->free_cdata)(menu->entries[i]->clientdata);
644 free(menu->entries[i]);
647 if (recurse) {
648 for (i=0; i<menu->cascade_no; i++) {
649 if (menu->cascades[i]) {
650 if (menu->cascades[i]->flags.brother)
651 wMenuDestroy(menu->cascades[i]->brother, recurse);
652 else
653 wMenuDestroy(menu->cascades[i], recurse);
658 if (menu->entries)
659 free(menu->entries);
663 FREE_PIXMAP(menu->menu_texture_data);
665 if (menu->cascades)
666 free(menu->cascades);
668 wCoreDestroy(menu->menu);
669 wFrameWindowDestroy(menu->frame);
671 /* destroy copy of this menu */
672 if (!menu->flags.brother && menu->brother)
673 wMenuDestroy(menu->brother, False);
675 free(menu);
679 #define F_NORMAL 0
680 #define F_TOP 1
681 #define F_BOTTOM 2
682 #define F_NONE 3
684 static void
685 drawFrame(WScreen *scr, Window win, int y, int w, int h, int type)
687 XSegment segs[2];
688 int i;
690 i = 0;
691 segs[i].x1 = segs[i].x2 = w-1;
692 segs[i].y1 = y;
693 segs[i].y2 = y + h - 1;
694 i++;
695 if (type != F_TOP && type != F_NONE) {
696 segs[i].x1 = 1;
697 segs[i].y1 = segs[i].y2 = y + h-2;
698 segs[i].x2 = w-1;
699 i++;
701 XDrawSegments(dpy, win, scr->menu_item_auxtexture->dim_gc, segs, i);
703 i = 0;
704 segs[i].x1 = 0;
705 segs[i].y1 = y;
706 segs[i].x2 = 0;
707 segs[i].y2 = y + h - 1;
708 i++;
709 if (type != F_BOTTOM && type != F_NONE) {
710 segs[i].x1 = 0;
711 segs[i].y1 = y;
712 segs[i].x2 = w-1;
713 segs[i].y2 = y;
714 i++;
716 XDrawSegments(dpy, win, scr->menu_item_auxtexture->light_gc, segs, i);
718 if (type != F_TOP && type != F_NONE)
719 XDrawLine(dpy, win, scr->menu_item_auxtexture->dark_gc, 0, y+h-1,
720 w-1, y+h-1);
724 static void
725 paintEntry(WMenu *menu, int index, int selected)
727 int x, y, w, h, tw;
728 int type;
729 GC light, dim, dark, textGC;
730 WScreen *scr=menu->frame->screen_ptr;
731 Window win = menu->menu->window;
732 WMenuEntry *entry=menu->entries[index];
734 if (!menu->flags.realized) return;
735 h = menu->entry_height;
736 w = menu->menu->width;
737 y = index * h;
739 light = scr->menu_item_auxtexture->light_gc;
740 dim = scr->menu_item_auxtexture->dim_gc;
741 dark = scr->menu_item_auxtexture->dark_gc;
743 if (wPreferences.menu_style == MS_FLAT && menu->entry_no > 1) {
744 if (index == 0)
745 type = F_TOP;
746 else if (index == menu->entry_no - 1)
747 type = F_BOTTOM;
748 else
749 type = F_NONE;
750 } else {
751 type = F_NORMAL;
754 /* paint background */
755 if (selected) {
756 XSetForeground(dpy, scr->select_menu_gc, scr->select_pixel);
757 XFillRectangle(dpy, win, scr->select_menu_gc, 1, y+1, w-2, h-3);
758 if (scr->menu_item_texture->any.type == WTEX_SOLID)
759 drawFrame(scr, win, y, w, h, type);
760 } else {
761 if (scr->menu_item_texture->any.type == WTEX_SOLID) {
762 XClearArea(dpy, win, 0, y + 1, w - 1, h - 3, False);
763 /* draw the frame */
764 drawFrame(scr, win, y, w, h, type);
765 } else {
766 XClearArea(dpy, win, 0, y, w, h, False);
770 if (selected) {
771 textGC = scr->select_menu_gc;
772 if (entry->flags.enabled)
773 XSetForeground(dpy, textGC, scr->select_text_pixel);
774 else
775 XSetForeground(dpy, textGC, scr->dtext_pixel);
776 } else if (!entry->flags.enabled) {
777 textGC = scr->disabled_menu_entry_gc;
778 } else {
779 textGC = scr->menu_entry_gc;
781 /* draw text */
782 x = 5;
783 if (entry->flags.indicator)
784 x += MENU_INDICATOR_SPACE + 2;
786 WMDrawString(scr->wmscreen, win, textGC, scr->menu_entry_font,
787 x, 3 + y + wPreferences.menu_text_clearance, entry->text, strlen(entry->text));
789 if (entry->cascade>=0) {
790 /* draw the cascade indicator */
791 XDrawLine(dpy,win,dim, w-11, y+6, w-6, y+h/2-1);
792 XDrawLine(dpy,win,light, w-11, y+h-8, w-6, y+h/2-1);
793 XDrawLine(dpy,win,dark, w-12, y+6, w-12, y+h-8);
796 /* draw indicator */
797 if (entry->flags.indicator && entry->flags.indicator_on) {
798 int iw, ih;
799 WPixmap *indicator;
802 switch (entry->flags.indicator_type) {
803 case MI_CHECK:
804 indicator = scr->menu_check_indicator;
805 break;
806 case MI_MINIWINDOW:
807 indicator = scr->menu_mini_indicator;
808 break;
809 case MI_HIDDEN:
810 indicator = scr->menu_hide_indicator;
811 break;
812 case MI_SHADED:
813 indicator = scr->menu_shade_indicator;
814 break;
815 case MI_DIAMOND:
816 default:
817 indicator = scr->menu_radio_indicator;
818 break;
821 iw = indicator->width;
822 ih = indicator->height;
823 XSetClipMask(dpy, scr->copy_gc, indicator->mask);
824 XSetClipOrigin(dpy, scr->copy_gc, 5, y+(h-ih)/2);
825 if (selected)
826 XSetForeground(dpy, scr->copy_gc, scr->black_pixel);
827 else
828 XSetForeground(dpy, scr->copy_gc, scr->mtext_pixel);
829 XFillRectangle(dpy, win, scr->copy_gc, 5, y+(h-ih)/2, iw, ih);
831 XCopyArea(dpy, indicator->image, win, scr->copy_gc, 0, 0,
832 iw, ih, 5, y+(h-ih)/2);
834 XSetClipOrigin(dpy, scr->copy_gc, 0, 0);
837 /* draw right text */
839 if (entry->rtext && entry->cascade<0) {
840 tw = WMWidthOfString(scr->menu_entry_font, entry->rtext,
841 strlen(entry->rtext));
843 WMDrawString(scr->wmscreen, win, textGC, scr->menu_entry_font, w-6-tw,
844 y + 3 + wPreferences.menu_text_clearance, entry->rtext, strlen(entry->rtext));
849 static void
850 move_menus(WMenu *menu, int x, int y)
852 while (menu->parent) {
853 menu = menu->parent;
854 x -= MENUW(menu);
855 if (!wPreferences.align_menus && menu->selected_entry>=0) {
856 y -= menu->selected_entry*menu->entry_height;
859 wMenuMove(menu, x, y, True);
862 static void
863 makeVisible(WMenu *menu)
865 WScreen *scr = menu->frame->screen_ptr;
866 int x1, y1, x2, y2, new_x, new_y, move;
868 if (menu->entry_no<0) return;
870 x1 = menu->frame_x;
871 y1 = menu->frame_y+menu->frame->top_width
872 + menu->selected_entry*menu->entry_height;
873 x2 = x1 + MENUW(menu);
874 y2 = y1 + menu->entry_height;
876 new_x = x1;
877 new_y = y1;
878 move = 0;
880 if (x1 < 0) {
881 new_x = 0;
882 move = 1;
883 } else if (x2 >= scr->scr_width) {
884 new_x = scr->scr_width - MENUW(menu) - 1;
885 move = 1;
888 if (y1 < 0) {
889 new_y = 0;
890 move = 1;
891 } else if (y2 >= scr->scr_height) {
892 new_y = scr->scr_height - menu->entry_height - 1;
893 move = 1;
896 new_y = new_y - menu->frame->top_width
897 - menu->selected_entry*menu->entry_height;
898 move_menus(menu, new_x, new_y);
902 static int
903 check_key(WMenu *menu, XKeyEvent *event)
905 int i, ch, s;
906 char buffer[32];
908 if (XLookupString(event, buffer, 32, NULL, NULL)<1)
909 return -1;
911 ch = toupper(buffer[0]);
913 s = (menu->selected_entry>=0 ? menu->selected_entry+1 : 0);
915 again:
916 for (i=s; i<menu->entry_no; i++) {
917 if (ch==toupper(menu->entries[i]->text[0])) {
918 return i;
921 /* no match. Retry from start, if previous started from a selected entry */
922 if (s!=0) {
923 s = 0;
924 goto again;
926 return -1;
930 static int
931 keyboardMenu(WMenu *menu)
933 XEvent event;
934 KeySym ksym=NoSymbol;
935 int done=0;
936 int index;
937 WMenuEntry *entry;
938 int old_pos_x = menu->frame_x;
939 int old_pos_y = menu->frame_y;
940 int new_x = old_pos_x, new_y = old_pos_y;
941 int scr_width = menu->frame->screen_ptr->scr_width;
942 int scr_height = menu->frame->screen_ptr->scr_height;
944 if (menu->flags.editing)
945 return False;
948 XGrabKeyboard(dpy, menu->frame->core->window, True, GrabModeAsync,
949 GrabModeAsync, CurrentTime);
951 if (menu->frame_y+menu->frame->top_width >= scr_height)
952 new_y = scr_height - menu->frame->top_width;
954 if (menu->frame_x+MENUW(menu) >= scr_width)
955 new_x = scr_width-MENUW(menu)-1;
957 move_menus(menu, new_x, new_y);
959 while (!done && menu->flags.mapped) {
960 XAllowEvents(dpy, AsyncKeyboard, CurrentTime);
961 WMMaskEvent(dpy, ExposureMask|ButtonMotionMask|ButtonPressMask
962 |ButtonReleaseMask|KeyPressMask|KeyReleaseMask
963 |SubstructureNotifyMask, &event);
965 switch (event.type) {
966 case KeyPress:
967 ksym = XLookupKeysym(&event.xkey, 0);
968 switch (ksym) {
969 case XK_Escape:
970 done = 1;
971 break;
973 case XK_Home:
974 case XK_KP_Home:
975 selectEntry(menu, 0);
976 makeVisible(menu);
977 break;
979 case XK_End:
980 case XK_KP_End:
981 selectEntry(menu, menu->entry_no-1);
982 makeVisible(menu);
983 break;
985 case XK_Up:
986 #ifdef ARROWLESS_KBD
987 case XK_k:
988 #endif
989 case XK_KP_Up:
990 if (menu->selected_entry <= 0)
991 selectEntry(menu, menu->entry_no-1);
992 else
993 selectEntry(menu, menu->selected_entry-1);
994 makeVisible(menu);
995 break;
997 case XK_Down:
998 #ifdef ARROWLESS_KBD
999 case XK_j:
1000 #endif
1001 case XK_KP_Down:
1002 if (menu->selected_entry<0)
1003 selectEntry(menu, 0);
1004 else if (menu->selected_entry == menu->entry_no-1)
1005 selectEntry(menu, 0);
1006 else if (menu->selected_entry < menu->entry_no-1)
1007 selectEntry(menu, menu->selected_entry+1);
1008 makeVisible(menu);
1009 break;
1011 case XK_Right:
1012 #ifdef ARROWLESS_KBD
1013 case XK_l:
1014 #endif
1015 case XK_KP_Right:
1016 if (menu->selected_entry>=0) {
1017 WMenuEntry *entry;
1018 entry = menu->entries[menu->selected_entry];
1020 if (entry->cascade >= 0 && menu->cascades
1021 && menu->cascades[entry->cascade]->entry_no > 0) {
1023 XUngrabKeyboard(dpy, CurrentTime);
1025 selectEntry(menu->cascades[entry->cascade], 0);
1026 if (!keyboardMenu(menu->cascades[entry->cascade]))
1027 done = 1;
1029 XGrabKeyboard(dpy, menu->frame->core->window, True,
1030 GrabModeAsync, GrabModeAsync,
1031 CurrentTime);
1034 break;
1036 case XK_Left:
1037 #ifdef ARROWLESS_KBD
1038 case XK_h:
1039 #endif
1040 case XK_KP_Left:
1041 if (menu->parent!=NULL && menu->parent->selected_entry>=0) {
1042 selectEntry(menu, -1);
1043 move_menus(menu, old_pos_x, old_pos_y);
1044 return True;
1046 break;
1048 case XK_Return:
1049 done = 2;
1050 break;
1052 default:
1053 index = check_key(menu, &event.xkey);
1054 if (index>=0) {
1055 selectEntry(menu, index);
1058 break;
1060 default:
1061 if (event.type==ButtonPress)
1062 done = 1;
1064 WMHandleEvent(&event);
1068 XUngrabKeyboard(dpy, CurrentTime);
1070 if (done==2 && menu->selected_entry>=0) {
1071 entry = menu->entries[menu->selected_entry];
1072 } else {
1073 entry = NULL;
1076 if (entry && entry->callback!=NULL && entry->flags.enabled
1077 && entry->cascade < 0) {
1078 #if (MENU_BLINK_COUNT > 0)
1079 int sel = menu->selected_entry;
1080 int i;
1082 for (i=0; i<MENU_BLINK_COUNT; i++) {
1083 paintEntry(menu, sel, False);
1084 XSync(dpy, 0);
1085 wusleep(MENU_BLINK_DELAY);
1086 paintEntry(menu, sel, True);
1087 XSync(dpy, 0);
1088 wusleep(MENU_BLINK_DELAY);
1090 #endif
1091 selectEntry(menu, -1);
1093 if (!menu->flags.buttoned) {
1094 wMenuUnmap(menu);
1095 move_menus(menu, old_pos_x, old_pos_y);
1097 closeCascade(menu);
1099 (*entry->callback)(menu, entry);
1100 } else {
1101 if (!menu->flags.buttoned) {
1102 wMenuUnmap(menu);
1103 move_menus(menu, old_pos_x, old_pos_y);
1105 selectEntry(menu, -1);
1109 /* returns True if returning from a submenu to a parent menu,
1110 * False if exiting from menu */
1111 return False;
1115 void
1116 wMenuMapAt(WMenu *menu, int x, int y, int keyboard)
1118 int scr_width = menu->frame->screen_ptr->scr_width;
1119 int scr_height = menu->frame->screen_ptr->scr_height;
1121 if (!menu->flags.realized) {
1122 menu->flags.realized=1;
1123 wMenuRealize(menu);
1125 if (!menu->flags.mapped) {
1126 if (wPreferences.wrap_menus) {
1127 if (x<0) x = 0;
1128 if (y<0) y = 0;
1129 if (x+MENUW(menu) > scr_width)
1130 x = scr_width - MENUW(menu);
1131 if (y+MENUH(menu) > scr_height)
1132 y = scr_height - MENUH(menu);
1135 XMoveWindow(dpy, menu->frame->core->window, x, y);
1136 menu->frame_x = x;
1137 menu->frame_y = y;
1138 XMapWindow(dpy, menu->frame->core->window);
1139 wRaiseFrame(menu->frame->core);
1140 menu->flags.mapped = 1;
1141 } else {
1142 selectEntry(menu, 0);
1145 if (keyboard)
1146 keyboardMenu(menu);
1150 void
1151 wMenuMap(WMenu *menu)
1153 if (!menu->flags.realized) {
1154 menu->flags.realized=1;
1155 wMenuRealize(menu);
1157 if (menu->flags.app_menu && menu->parent==NULL) {
1158 menu->frame_x = menu->frame->screen_ptr->app_menu_x;
1159 menu->frame_y = menu->frame->screen_ptr->app_menu_y;
1160 XMoveWindow(dpy, menu->frame->core->window, menu->frame_x, menu->frame_y);
1162 XMapWindow(dpy, menu->frame->core->window);
1163 wRaiseFrame(menu->frame->core);
1164 menu->flags.mapped = 1;
1168 void
1169 wMenuUnmap(WMenu *menu)
1171 int i;
1173 XUnmapWindow(dpy, menu->frame->core->window);
1174 if (menu->flags.titled && menu->flags.buttoned) {
1175 wFrameWindowHideButton(menu->frame, WFF_RIGHT_BUTTON);
1177 menu->flags.buttoned = 0;
1178 menu->flags.mapped = 0;
1179 menu->flags.open_to_left = 0;
1181 for (i=0; i<menu->cascade_no; i++) {
1182 if (menu->cascades[i]!=NULL
1183 && menu->cascades[i]->flags.mapped
1184 && !menu->cascades[i]->flags.buttoned) {
1186 wMenuUnmap(menu->cascades[i]);
1189 menu->selected_entry = -1;
1194 void
1195 wMenuPaint(WMenu *menu)
1197 int i;
1199 if (!menu->flags.mapped) {
1200 return;
1203 /* paint entries */
1204 for (i=0; i<menu->entry_no; i++) {
1205 paintEntry(menu, i, i==menu->selected_entry);
1210 void
1211 wMenuSetEnabled(WMenu *menu, int index, int enable)
1213 if (index>=menu->entry_no) return;
1214 menu->entries[index]->flags.enabled=enable;
1215 paintEntry(menu, index, index==menu->selected_entry);
1216 paintEntry(menu->brother, index, index==menu->selected_entry);
1220 /* ====================================================================== */
1223 static void
1224 editEntry(WMenu *menu, WMenuEntry *entry)
1226 WTextInput *text;
1227 XEvent event;
1228 WObjDescriptor *desc;
1229 char *t;
1230 int done = 0;
1231 Window old_focus;
1232 int old_revert;
1234 menu->flags.editing = 1;
1236 text = wTextCreate(menu->menu, 1, menu->entry_height * entry->order,
1237 menu->menu->width - 2, menu->entry_height - 1);
1239 wTextPutText(text, entry->text);
1240 XGetInputFocus(dpy, &old_focus, &old_revert);
1241 XSetInputFocus(dpy, text->core->window, RevertToNone, CurrentTime);
1243 if (XGrabKeyboard(dpy, text->core->window, True, GrabModeAsync,
1244 GrabModeAsync, CurrentTime)!=GrabSuccess) {
1245 wwarning("could not grab keyboard");
1246 wTextDestroy(text);
1248 wSetFocusTo(menu->frame->screen_ptr,
1249 menu->frame->screen_ptr->focused_window);
1250 return;
1254 while (!done && !text->done) {
1255 XSync(dpy, 0);
1256 XAllowEvents(dpy, AsyncKeyboard|AsyncPointer, CurrentTime);
1257 XSync(dpy, 0);
1258 WMNextEvent(dpy, &event);
1260 if (XFindContext(dpy, event.xany.window, wWinContext,
1261 (XPointer *)&desc)==XCNOENT)
1262 desc = NULL;
1264 if ((desc != NULL) && (desc->handle_anything != NULL)) {
1266 (*desc->handle_anything)(desc, &event);
1268 } else {
1269 switch (event.type) {
1270 case ButtonPress:
1271 XAllowEvents(dpy, ReplayPointer, CurrentTime);
1272 done = 1;
1274 default:
1275 WMHandleEvent(&event);
1276 break;
1281 XSetInputFocus(dpy, old_focus, old_revert, CurrentTime);
1283 wSetFocusTo(menu->frame->screen_ptr,
1284 menu->frame->screen_ptr->focused_window);
1287 t = wTextGetText(text);
1288 /* if !t, the user has canceled editing */
1289 if (t) {
1290 if (entry->text)
1291 free(entry->text);
1292 entry->text = wstrdup(t);
1294 menu->flags.realized = 0;
1296 wTextDestroy(text);
1298 XUngrabKeyboard(dpy, CurrentTime);
1300 if (t && menu->on_edit)
1301 (*menu->on_edit)(menu, entry);
1303 menu->flags.editing = 0;
1305 if (!menu->flags.realized)
1306 wMenuRealize(menu);
1310 static void
1311 selectEntry(WMenu *menu, int entry_no)
1313 WMenuEntry *entry;
1314 WMenu *submenu;
1315 int old_entry;
1317 if (menu->entries==NULL)
1318 return;
1320 if (entry_no >= menu->entry_no)
1321 return;
1323 old_entry = menu->selected_entry;
1324 menu->selected_entry = entry_no;
1326 if (old_entry!=entry_no) {
1328 /* unselect previous entry */
1329 if (old_entry>=0) {
1330 paintEntry(menu, old_entry, False);
1331 entry = menu->entries[old_entry];
1333 /* unmap cascade */
1334 if (entry->cascade>=0 && menu->cascades) {
1335 if (!menu->cascades[entry->cascade]->flags.buttoned) {
1336 wMenuUnmap(menu->cascades[entry->cascade]);
1341 if (entry_no<0) {
1342 menu->selected_entry = -1;
1343 return;
1345 entry = menu->entries[entry_no];
1347 if (entry->cascade>=0 && menu->cascades && entry->flags.enabled) {
1348 /* Callback for when the submenu is opened.
1350 submenu = menu->cascades[entry->cascade];
1351 if (submenu && submenu->flags.brother)
1352 submenu = submenu->brother;
1354 if (entry->callback) {
1355 /* Only call the callback if the submenu is not yet mapped.
1357 if (menu->flags.brother) {
1358 if (!submenu || !submenu->flags.mapped)
1359 (*entry->callback)(menu->brother, entry);
1360 } else {
1361 if (!submenu || !submenu->flags.buttoned)
1362 (*entry->callback)(menu, entry);
1366 /* the submenu menu might have changed */
1367 submenu = menu->cascades[entry->cascade];
1369 /* map cascade */
1370 if (!submenu->flags.mapped) {
1371 int x, y;
1373 if (!submenu->flags.realized)
1374 wMenuRealize(submenu);
1375 if (wPreferences.wrap_menus) {
1376 if (menu->flags.open_to_left)
1377 submenu->flags.open_to_left = 1;
1379 if (submenu->flags.open_to_left) {
1380 x = menu->frame_x - MENUW(submenu);
1381 if (x<0) {
1382 x = 0;
1383 submenu->flags.open_to_left = 0;
1385 } else {
1386 x = menu->frame_x + MENUW(menu);
1388 if (x + MENUW(submenu)
1389 >= menu->frame->screen_ptr->scr_width) {
1391 x = menu->frame_x - MENUW(submenu);
1392 submenu->flags.open_to_left = 1;
1395 } else {
1396 x = menu->frame_x + MENUW(menu);
1399 if (wPreferences.align_menus) {
1400 y = menu->frame_y;
1401 } else {
1402 y = menu->frame_y + menu->entry_height*entry_no;
1403 if (menu->flags.titled)
1404 y += menu->frame->top_width;
1405 if (menu->cascades[entry->cascade]->flags.titled)
1406 y -= menu->cascades[entry->cascade]->frame->top_width;
1409 wMenuMapAt(menu->cascades[entry->cascade], x, y, False);
1410 menu->cascades[entry->cascade]->parent = menu;
1411 } else {
1412 return;
1415 paintEntry(menu, entry_no, True);
1420 static WMenu*
1421 findMenu(WScreen *scr, int *x_ret, int *y_ret)
1423 WMenu *menu;
1424 WObjDescriptor *desc;
1425 Window root_ret, win, junk_win;
1426 int x, y, wx, wy;
1427 unsigned int mask;
1429 XQueryPointer(dpy, scr->root_win, &root_ret, &win, &x, &y, &wx, &wy,
1430 &mask);
1432 if (win==None) return NULL;
1434 if (XFindContext(dpy, win, wWinContext, (XPointer *)&desc)==XCNOENT)
1435 return NULL;
1437 if (desc->parent_type == WCLASS_MENU) {
1438 menu = (WMenu*)desc->parent;
1439 XTranslateCoordinates(dpy, root_ret, menu->menu->window, wx, wy,
1440 x_ret, y_ret, &junk_win);
1441 return menu;
1443 return NULL;
1449 static void
1450 closeCascade(WMenu *menu)
1452 WMenu *parent=menu->parent;
1454 if (menu->flags.brother
1455 || (!menu->flags.buttoned
1456 && (!menu->flags.app_menu||menu->parent!=NULL))) {
1458 selectEntry(menu, -1);
1459 XSync(dpy, 0);
1460 #if (MENU_BLINK_DELAY > 2)
1461 wusleep(MENU_BLINK_DELAY/2);
1462 #endif
1463 wMenuUnmap(menu);
1464 while (parent!=NULL
1465 && (parent->parent!=NULL || !parent->flags.app_menu
1466 || parent->flags.brother)
1467 && !parent->flags.buttoned) {
1468 selectEntry(parent, -1);
1469 wMenuUnmap(parent);
1470 parent = parent->parent;
1472 if (parent)
1473 selectEntry(parent, -1);
1478 static void
1479 closeBrotherCascadesOf(WMenu *menu)
1481 WMenu *tmp;
1482 int i;
1484 for (i=0; i<menu->cascade_no; i++) {
1485 if (menu->cascades[i]->flags.brother) {
1486 tmp = menu->cascades[i];
1487 } else {
1488 tmp = menu->cascades[i]->brother;
1490 if (tmp->flags.mapped) {
1491 selectEntry(tmp->parent, -1);
1492 closeBrotherCascadesOf(tmp);
1493 break;
1499 #define getEntryAt(menu, x, y) ((y)<0 ? -1 : (y)/(menu->entry_height))
1502 static WMenu*
1503 parentMenu(WMenu *menu)
1505 WMenu *parent;
1506 WMenuEntry *entry;
1508 if (menu->flags.buttoned)
1509 return menu;
1511 while (menu->parent && menu->parent->flags.mapped) {
1512 parent = menu->parent;
1513 if (parent->selected_entry < 0)
1514 break;
1515 entry = parent->entries[parent->selected_entry];
1516 if (!entry->flags.enabled || entry->cascade<0 || !parent->cascades ||
1517 parent->cascades[entry->cascade] != menu)
1518 break;
1519 menu = parent;
1520 if (menu->flags.buttoned)
1521 break;
1524 return menu;
1530 * Will raise the passed menu, if submenu = 0
1531 * If submenu > 0 will also raise all mapped submenus
1532 * until the first buttoned one
1533 * If submenu < 0 will also raise all mapped parent menus
1534 * until the first buttoned one
1537 static void
1538 raiseMenus(WMenu *menu, int submenus)
1540 WMenu *submenu;
1541 int i;
1543 if(!menu) return;
1545 wRaiseFrame(menu->frame->core);
1547 if (submenus>0 && menu->selected_entry>=0) {
1548 i = menu->entries[menu->selected_entry]->cascade;
1549 if (i>=0 && menu->cascades) {
1550 submenu = menu->cascades[i];
1551 if (submenu->flags.mapped && !submenu->flags.buttoned)
1552 raiseMenus(submenu, submenus);
1555 if (submenus<0 && !menu->flags.buttoned &&
1556 menu->parent && menu->parent->flags.mapped)
1557 raiseMenus(menu->parent, submenus);
1561 WMenu*
1562 wMenuUnderPointer(WScreen *screen)
1564 WObjDescriptor *desc;
1565 Window root_ret, win;
1566 int dummy;
1567 unsigned int mask;
1569 XQueryPointer(dpy, screen->root_win, &root_ret, &win, &dummy, &dummy,
1570 &dummy, &dummy, &mask);
1572 if (win==None) return NULL;
1574 if (XFindContext(dpy, win, wWinContext, (XPointer *)&desc)==XCNOENT)
1575 return NULL;
1577 if (desc->parent_type == WCLASS_MENU)
1578 return (WMenu *)desc->parent;
1579 return NULL;
1584 #define MIN(a,b) (((a) > (b)) ? (b) : (a))
1587 static void
1588 getPointerPosition(WScreen *scr, int *x, int *y)
1590 Window root_ret, win;
1591 int wx, wy;
1592 unsigned int mask;
1594 XQueryPointer(dpy, scr->root_win, &root_ret, &win, x, y, &wx, &wy, &mask);
1598 static void
1599 getScrollAmount(WMenu *menu, int *hamount, int *vamount)
1601 WScreen *scr = menu->menu->screen_ptr;
1602 int menuX1 = menu->frame_x;
1603 int menuY1 = menu->frame_y;
1604 int menuX2 = menu->frame_x + MENUW(menu);
1605 int menuY2 = menu->frame_y + MENUH(menu);
1606 int screenW = scr->scr_width;
1607 int screenH = scr->scr_height;
1608 int xroot, yroot;
1610 *hamount = 0;
1611 *vamount = 0;
1613 getPointerPosition(scr, &xroot, &yroot);
1615 #ifdef VIRTUAL_DESKTOP
1616 if (wPreferences.vedge_thickness) {
1617 if (xroot <= wPreferences.vedge_thickness + 1 && menuX1 < wPreferences.vedge_thickness) {
1618 /* scroll to the right */
1619 *hamount = MIN(MENU_SCROLL_STEP, abs(menuX1));
1621 } else if (xroot >= screenW-2-wPreferences.vedge_thickness && menuX2 > screenW-1-wPreferences.vedge_thickness) {
1622 /* scroll to the left */
1623 *hamount = MIN(MENU_SCROLL_STEP, abs(menuX2-screenW-1));
1625 if (*hamount==0)
1626 *hamount = 1;
1628 *hamount = -*hamount;
1630 } else
1631 #endif
1633 if (xroot <= 1 && menuX1 < 0) {
1634 /* scroll to the right */
1635 *hamount = MIN(MENU_SCROLL_STEP, abs(menuX1));
1637 } else if (xroot >= screenW-2 && menuX2 > screenW-1) {
1638 /* scroll to the left */
1639 *hamount = MIN(MENU_SCROLL_STEP, abs(menuX2-screenW-1));
1641 if (*hamount==0)
1642 *hamount = 1;
1644 *hamount = -*hamount;
1647 if (yroot <= 1 && menuY1 < 0) {
1648 /* scroll down */
1649 *vamount = MIN(MENU_SCROLL_STEP, abs(menuY1));
1651 } else if (yroot >= screenH-2 && menuY2 > screenH-1) {
1652 /* scroll up */
1653 *vamount = MIN(MENU_SCROLL_STEP, abs(menuY2-screenH-2));
1655 *vamount = -*vamount;
1660 static void
1661 dragScrollMenuCallback(void *data)
1663 WMenu *menu = (WMenu*)data;
1664 WScreen *scr = menu->menu->screen_ptr;
1665 WMenu *parent = parentMenu(menu);
1666 int hamount, vamount;
1667 int x, y;
1668 int newSelectedEntry;
1670 getScrollAmount(menu, &hamount, &vamount);
1673 if (hamount != 0 || vamount != 0) {
1674 wMenuMove(parent, parent->frame_x + hamount,
1675 parent->frame_y + vamount, True);
1676 if (findMenu(scr, &x, &y)) {
1677 newSelectedEntry = getEntryAt(menu, x, y);
1678 selectEntry(menu, newSelectedEntry);
1679 } else {
1680 /* Pointer fell outside of menu. If the selected entry is
1681 * not a submenu, unselect it */
1682 if (menu->selected_entry >= 0
1683 && menu->entries[menu->selected_entry]->cascade<0)
1684 selectEntry(menu, -1);
1685 newSelectedEntry = 0;
1688 /* paranoid check */
1689 if (newSelectedEntry >= 0) {
1690 /* keep scrolling */
1691 menu->timer = WMAddTimerHandler(MENU_SCROLL_DELAY,
1692 dragScrollMenuCallback, menu);
1693 } else {
1694 menu->timer = NULL;
1696 } else {
1697 /* don't need to scroll anymore */
1698 menu->timer = NULL;
1699 if (findMenu(scr, &x, &y)) {
1700 newSelectedEntry = getEntryAt(menu, x, y);
1701 selectEntry(menu, newSelectedEntry);
1707 static void
1708 scrollMenuCallback(void *data)
1710 WMenu *menu = (WMenu*)data;
1711 WMenu *parent = parentMenu(menu);
1712 int hamount = 0; /* amount to scroll */
1713 int vamount = 0;
1715 getScrollAmount(menu, &hamount, &vamount);
1717 if (hamount != 0 || vamount != 0) {
1718 wMenuMove(parent, parent->frame_x + hamount,
1719 parent->frame_y + vamount, True);
1721 /* keep scrolling */
1722 menu->timer = WMAddTimerHandler(MENU_SCROLL_DELAY,
1723 scrollMenuCallback, menu);
1724 } else {
1725 /* don't need to scroll anymore */
1726 menu->timer = NULL;
1732 #define MENU_SCROLL_BORDER 5
1734 static int
1735 isPointNearBoder(WMenu *menu, int x, int y)
1737 int menuX1 = menu->frame_x;
1738 int menuY1 = menu->frame_y;
1739 int menuX2 = menu->frame_x + MENUW(menu);
1740 int menuY2 = menu->frame_y + MENUH(menu);
1741 int scrXe = menu->menu->screen_ptr->scr_width-1;
1742 int scrYe = menu->menu->screen_ptr->scr_height-1;
1743 int flag = 0;
1745 if (x >= menuX1 && x <= menuX2 && (y < MENU_SCROLL_BORDER
1746 || y > scrYe-MENU_SCROLL_BORDER))
1747 flag = 1;
1748 else if (y >= menuY1 && y <= menuY2 && (x < MENU_SCROLL_BORDER
1749 || x > scrXe-MENU_SCROLL_BORDER))
1750 flag = 1;
1752 return flag;
1756 typedef struct _delay {
1757 WWindow *wwin;
1758 WMenu *menu;
1759 int ox,oy;
1760 } _delay;
1763 static void
1764 _leaving(_delay *dl)
1766 wMenuMove(dl->menu, dl->ox, dl->oy, True);
1767 dl->menu->jump_back=NULL;
1768 dl->menu->menu->screen_ptr->flags.jump_back_pending = 0;
1769 free(dl);
1773 void
1774 wMenuScroll(WMenu *menu, XEvent *event)
1776 WMenu *smenu;
1777 WMenu *omenu = parentMenu(menu);
1778 WScreen *scr = menu->frame->screen_ptr;
1779 int done = 0;
1780 int jump_back = 0;
1781 int old_frame_x = omenu->frame_x;
1782 int old_frame_y = omenu->frame_y;
1783 XEvent ev;
1785 if (omenu->jump_back)
1786 WMDeleteTimerWithClientData(omenu->jump_back);
1789 if ((/*omenu->flags.buttoned &&*/ !wPreferences.wrap_menus)
1790 || omenu->flags.app_menu) {
1791 jump_back = 1;
1794 if (!wPreferences.wrap_menus)
1795 raiseMenus(omenu, True);
1796 else
1797 raiseMenus(menu, False);
1799 if (!menu->timer)
1800 scrollMenuCallback(menu);
1802 while(!done) {
1803 int x, y, on_border, on_x_edge, on_y_edge, on_title;
1805 WMNextEvent(dpy, &ev);
1806 switch (ev.type) {
1807 case EnterNotify:
1808 WMHandleEvent(&ev);
1809 case MotionNotify:
1810 x = (ev.type==MotionNotify) ? ev.xmotion.x_root : ev.xcrossing.x_root;
1811 y = (ev.type==MotionNotify) ? ev.xmotion.y_root : ev.xcrossing.y_root;
1813 /* on_border is != 0 if the pointer is between the menu
1814 * and the screen border and is close enough to the border */
1815 on_border = isPointNearBoder(menu, x, y);
1817 smenu = wMenuUnderPointer(scr);
1819 if ((smenu==NULL && !on_border) || (smenu && parentMenu(smenu)!=omenu)) {
1820 done = 1;
1821 break;
1824 on_x_edge = x <= 1 || x >= scr->scr_width - 2;
1825 on_y_edge = y <= 1 || y >= scr->scr_height - 2;
1826 on_border = on_x_edge || on_y_edge;
1828 if (!on_border && !jump_back) {
1829 done = 1;
1830 break;
1833 if (menu->timer && (smenu!=menu || (!on_y_edge && !on_x_edge))) {
1834 WMDeleteTimerHandler(menu->timer);
1835 menu->timer = NULL;
1838 if (smenu != NULL)
1839 menu = smenu;
1841 if (!menu->timer)
1842 scrollMenuCallback(menu);
1843 break;
1844 case ButtonPress:
1845 /* True if we push on title, or drag the omenu to other position */
1846 on_title = ev.xbutton.x_root >= omenu->frame_x &&
1847 ev.xbutton.x_root <= omenu->frame_x + MENUW(omenu) &&
1848 ev.xbutton.y_root >= omenu->frame_y &&
1849 ev.xbutton.y_root <= omenu->frame_y + omenu->frame->top_width;
1850 WMHandleEvent(&ev);
1851 smenu = wMenuUnderPointer(scr);
1852 if (smenu == NULL || (smenu && smenu->flags.buttoned && smenu != omenu))
1853 done = 1;
1854 else if (smenu==omenu && on_title) {
1855 jump_back = 0;
1856 done = 1;
1858 break;
1859 case KeyPress:
1860 done = 1;
1861 default:
1862 WMHandleEvent(&ev);
1863 break;
1867 if (menu->timer) {
1868 WMDeleteTimerHandler(menu->timer);
1869 menu->timer = NULL;
1872 if (jump_back) {
1873 _delay *delayer;
1874 if (!omenu->jump_back) {
1875 delayer=wmalloc(sizeof(_delay));
1876 delayer->menu=omenu;
1877 delayer->ox=old_frame_x;
1878 delayer->oy=old_frame_y;
1879 omenu->jump_back=delayer;
1880 scr->flags.jump_back_pending = 1;
1882 else delayer = omenu->jump_back;
1883 WMAddTimerHandler(MENU_JUMP_BACK_DELAY,(WMCallback*)_leaving, delayer);
1889 static void
1890 menuExpose(WObjDescriptor *desc, XEvent *event)
1892 wMenuPaint(desc->parent);
1895 typedef struct {
1896 int *delayed_select;
1897 WMenu *menu;
1898 WMHandlerID magic;
1899 } delay_data;
1902 static void
1903 delaySelection(void *data)
1905 delay_data *d = (delay_data*)data;
1906 int x, y, entry_no;
1907 WMenu *menu;
1909 d->magic = NULL;
1911 menu = findMenu(d->menu->menu->screen_ptr, &x, &y);
1912 if (menu && (d->menu == menu || d->delayed_select)) {
1913 entry_no = getEntryAt(menu, x, y);
1914 selectEntry(menu, entry_no);
1916 if (d->delayed_select)
1917 *(d->delayed_select) = 0;
1921 static void
1922 menuMouseDown(WObjDescriptor *desc, XEvent *event)
1924 XButtonEvent *bev = &event->xbutton;
1925 WMenu *menu = desc->parent;
1926 WMenu *smenu;
1927 WScreen *scr=menu->frame->screen_ptr;
1928 WMenuEntry *entry=NULL;
1929 XEvent ev;
1930 int close_on_exit=0;
1931 int done=0;
1932 int delayed_select = 0;
1933 int entry_no;
1934 int x, y;
1935 int prevx, prevy;
1936 int old_frame_x = 0;
1937 int old_frame_y = 0;
1938 delay_data d_data = {NULL, NULL, NULL};
1940 if (!wPreferences.wrap_menus) {
1941 smenu = parentMenu(menu);
1942 old_frame_x = smenu->frame_x;
1943 old_frame_y = smenu->frame_y;
1944 } else if (event->xbutton.window == menu->frame->core->window) {
1945 /* This is true if the menu was launched with right click on root window */
1946 delayed_select = 1;
1947 d_data.delayed_select = &delayed_select;
1948 d_data.menu = menu;
1949 d_data.magic = WMAddTimerHandler(wPreferences.dblclick_time,
1950 delaySelection, &d_data);
1953 if (menu->flags.inside_handler) {
1954 return;
1956 menu->flags.inside_handler = 1;
1958 wRaiseFrame(menu->frame->core);
1960 close_on_exit = (bev->send_event || menu->flags.brother);
1962 smenu = findMenu(scr, &x, &y);
1963 if (!smenu) {
1964 x = -1;
1965 y = -1;
1966 } else {
1967 menu = smenu;
1970 if (menu->flags.editing) {
1971 goto byebye;
1973 entry_no = getEntryAt(menu, x, y);
1974 if (entry_no>=0) {
1975 entry = menu->entries[entry_no];
1977 if (!close_on_exit && (bev->state & ControlMask) && smenu
1978 && entry->flags.editable) {
1979 editEntry(smenu, entry);
1980 goto byebye;
1981 } else if (bev->state & ControlMask) {
1982 goto byebye;
1985 if (entry->flags.enabled && entry->cascade>=0 && menu->cascades) {
1986 WMenu *submenu = menu->cascades[entry->cascade];
1987 /* map cascade */
1988 if (submenu->flags.mapped && !submenu->flags.buttoned &&
1989 menu->selected_entry!=entry_no) {
1990 wMenuUnmap(submenu);
1992 if (!submenu->flags.mapped && !delayed_select) {
1993 selectEntry(menu, entry_no);
1994 } else if (!submenu->flags.buttoned) {
1995 selectEntry(menu, -1);
1998 } else if (!delayed_select) {
1999 selectEntry(menu, entry_no);
2002 if (!wPreferences.wrap_menus && !wPreferences.scrollable_menus) {
2003 if (!menu->timer)
2004 dragScrollMenuCallback(menu);
2008 prevx = bev->x_root;
2009 prevy = bev->y_root;
2010 while (!done) {
2011 int x, y;
2013 XAllowEvents(dpy, AsyncPointer|SyncPointer, CurrentTime);
2015 WMMaskEvent(dpy, ExposureMask|ButtonMotionMask|ButtonReleaseMask
2016 |ButtonPressMask, &ev);
2017 switch (ev.type) {
2018 case MotionNotify:
2019 smenu = findMenu(scr, &x, &y);
2021 if (smenu == NULL) {
2022 /* moved mouse out of menu */
2024 if (!delayed_select && d_data.magic) {
2025 WMDeleteTimerHandler(d_data.magic);
2026 d_data.magic = NULL;
2028 if (menu==NULL
2029 || (menu->selected_entry>=0
2030 && menu->entries[menu->selected_entry]->cascade>=0)) {
2031 prevx = ev.xmotion.x_root;
2032 prevy = ev.xmotion.y_root;
2034 break;
2036 selectEntry(menu, -1);
2037 menu = smenu;
2038 prevx = ev.xmotion.x_root;
2039 prevy = ev.xmotion.y_root;
2040 break;
2041 } else if (menu && menu!=smenu
2042 && (menu->selected_entry<0
2043 || menu->entries[menu->selected_entry]->cascade<0)) {
2044 selectEntry(menu, -1);
2046 if (!delayed_select && d_data.magic) {
2047 WMDeleteTimerHandler(d_data.magic);
2048 d_data.magic = NULL;
2050 } else {
2052 /* hysteresis for item selection */
2054 /* check if the motion was to the side, indicating that
2055 * the user may want to cross to a submenu */
2056 if (!delayed_select && menu) {
2057 int dx;
2058 Bool moved_to_submenu;/* moved to direction of submenu */
2060 dx = abs(prevx - ev.xmotion.x_root);
2062 moved_to_submenu = False;
2063 if (dx > 0 /* if moved enough to the side */
2064 /* maybe a open submenu */
2065 && menu->selected_entry>=0
2066 /* moving to the right direction */
2067 && (wPreferences.align_menus
2068 || ev.xmotion.y_root >= prevy)) {
2069 int index;
2071 index = menu->entries[menu->selected_entry]->cascade;
2072 if (index>=0) {
2073 if (menu->cascades[index]->frame_x>menu->frame_x) {
2074 if (prevx < ev.xmotion.x_root)
2075 moved_to_submenu = True;
2076 } else {
2077 if (prevx > ev.xmotion.x_root)
2078 moved_to_submenu = True;
2084 if (menu != smenu) {
2085 if (d_data.magic) {
2086 WMDeleteTimerHandler(d_data.magic);
2088 d_data.magic = NULL;
2089 } else if (moved_to_submenu) {
2090 /* while we are moving, postpone the selection */
2091 if (d_data.magic) {
2092 WMDeleteTimerHandler(d_data.magic);
2094 d_data.delayed_select = NULL;
2095 d_data.menu = menu;
2096 d_data.magic = WMAddTimerHandler(MENU_SELECT_DELAY,
2097 delaySelection,
2098 &d_data);
2099 prevx = ev.xmotion.x_root;
2100 prevy = ev.xmotion.y_root;
2101 break;
2102 } else {
2103 if (d_data.magic)
2104 WMDeleteTimerHandler(d_data.magic);
2105 d_data.magic = NULL;
2109 prevx = ev.xmotion.x_root;
2110 prevy = ev.xmotion.y_root;
2111 if (menu!=smenu) {
2112 /* pointer crossed menus */
2113 if (menu && menu->timer) {
2114 WMDeleteTimerHandler(menu->timer);
2115 menu->timer = NULL;
2117 if (smenu)
2118 dragScrollMenuCallback(smenu);
2120 menu = smenu;
2121 if (!menu->timer)
2122 dragScrollMenuCallback(menu);
2124 if (!delayed_select) {
2125 entry_no = getEntryAt(menu, x, y);
2126 if (entry_no>=0) {
2127 entry = menu->entries[entry_no];
2128 if (entry->flags.enabled && entry->cascade>=0 &&
2129 menu->cascades) {
2130 WMenu *submenu = menu->cascades[entry->cascade];
2131 if (submenu->flags.mapped && !submenu->flags.buttoned
2132 && menu->selected_entry!=entry_no) {
2133 wMenuUnmap(submenu);
2137 selectEntry(menu, entry_no);
2139 break;
2141 case ButtonPress:
2142 break;
2144 case ButtonRelease:
2145 if (ev.xbutton.button == event->xbutton.button)
2146 done=1;
2147 break;
2149 case Expose:
2150 WMHandleEvent(&ev);
2151 break;
2155 if (menu && menu->timer) {
2156 WMDeleteTimerHandler(menu->timer);
2157 menu->timer = NULL;
2159 if (d_data.magic!=NULL)
2160 WMDeleteTimerHandler(d_data.magic);
2162 if (menu && menu->selected_entry>=0) {
2163 entry = menu->entries[menu->selected_entry];
2164 if (entry->callback!=NULL && entry->flags.enabled
2165 && entry->cascade < 0) {
2166 /* blink and erase menu selection */
2167 #if (MENU_BLINK_DELAY > 0)
2168 int sel = menu->selected_entry;
2169 int i;
2171 for (i=0; i<MENU_BLINK_COUNT; i++) {
2172 paintEntry(menu, sel, False);
2173 XSync(dpy, 0);
2174 wusleep(MENU_BLINK_DELAY);
2175 paintEntry(menu, sel, True);
2176 XSync(dpy, 0);
2177 wusleep(MENU_BLINK_DELAY);
2179 #endif
2180 /* unmap the menu, it's parents and call the callback */
2181 if (!menu->flags.buttoned &&
2182 (!menu->flags.app_menu||menu->parent!=NULL)) {
2183 closeCascade(menu);
2184 } else {
2185 selectEntry(menu, -1);
2187 (*entry->callback)(menu, entry);
2189 /* If the user double clicks an entry, the entry will
2190 * be executed twice, which is not good for things like
2191 * the root menu. So, ignore any clicks that were generated
2192 * while the entry was being executed */
2193 while (XCheckTypedWindowEvent(dpy, menu->menu->window,
2194 ButtonPress, &ev));
2195 } else if (entry->callback!=NULL && entry->cascade<0) {
2196 selectEntry(menu, -1);
2197 } else {
2198 if (entry->cascade>=0 && menu->cascades
2199 && menu->cascades[entry->cascade]->flags.brother) {
2200 selectEntry(menu, -1);
2205 if (((WMenu*)desc->parent)->flags.brother || close_on_exit || !smenu)
2206 closeCascade(desc->parent);
2208 /* close the cascade windows that should not remain opened */
2209 closeBrotherCascadesOf(desc->parent);
2211 if (!wPreferences.wrap_menus)
2212 wMenuMove(parentMenu(desc->parent), old_frame_x, old_frame_y, True);
2214 byebye:
2215 ((WMenu*)desc->parent)->flags.inside_handler = 0;
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);
2247 if (submenus<0 && menu->parent!=NULL && menu->parent->flags.mapped &&
2248 !menu->parent->flags.buttoned) {
2249 if (wPreferences.align_menus) {
2250 wMenuMove(menu->parent, x - MENUW(menu->parent), y, submenus);
2251 } else {
2252 wMenuMove(menu->parent, x - MENUW(menu->parent), menu->frame_y
2253 - menu->parent->entry_height*menu->parent->selected_entry,
2254 submenus);
2260 static void
2261 changeMenuLevels(WMenu *menu, int lower)
2263 int i;
2265 if (!lower) {
2266 ChangeStackingLevel(menu->frame->core, (!menu->parent ? WMMainMenuLevel
2267 : WMSubmenuLevel));
2268 wRaiseFrame(menu->frame->core);
2269 menu->flags.lowered = 0;
2270 } else {
2271 ChangeStackingLevel(menu->frame->core, WMNormalLevel);
2272 wLowerFrame(menu->frame->core);
2273 menu->flags.lowered = 1;
2275 for (i=0; i<menu->cascade_no; i++) {
2276 if (menu->cascades[i]
2277 && !menu->cascades[i]->flags.buttoned
2278 && menu->cascades[i]->flags.lowered!=lower) {
2279 changeMenuLevels(menu->cascades[i], lower);
2286 static void
2287 menuTitleDoubleClick(WCoreWindow *sender, void *data, XEvent *event)
2289 WMenu *menu = data;
2290 int lower;
2292 if (event->xbutton.state & MOD_MASK) {
2293 if (menu->flags.lowered) {
2294 lower = 0;
2295 } else {
2296 lower = 1;
2298 changeMenuLevels(menu, lower);
2303 static void
2304 menuTitleMouseDown(WCoreWindow *sender, void *data, XEvent *event)
2306 WMenu *menu = data;
2307 WMenu *tmp;
2308 XEvent ev;
2309 int x=menu->frame_x, y=menu->frame_y;
2310 int dx=event->xbutton.x_root, dy=event->xbutton.y_root;
2311 int i, lower;
2312 Bool started;
2314 /* can't touch the menu copy */
2315 if (menu->flags.brother)
2316 return;
2318 if (event->xbutton.button != Button1 && event->xbutton.button != Button2)
2319 return;
2321 if (event->xbutton.state & MOD_MASK) {
2322 wLowerFrame(menu->frame->core);
2323 lower = 1;
2324 } else {
2325 wRaiseFrame(menu->frame->core);
2326 lower = 0;
2328 tmp = menu;
2330 /* lower/raise all submenus */
2331 while (1) {
2332 if (tmp->selected_entry>=0 && tmp->cascades
2333 && tmp->entries[tmp->selected_entry]->cascade>=0) {
2334 tmp = tmp->cascades[tmp->entries[tmp->selected_entry]->cascade];
2335 if (!tmp || !tmp->flags.mapped)
2336 break;
2337 if (lower)
2338 wLowerFrame(tmp->frame->core);
2339 else
2340 wRaiseFrame(tmp->frame->core);
2341 } else {
2342 break;
2346 /* tear off the menu if it's a root menu or a cascade
2347 application menu */
2348 if (!menu->flags.buttoned && !menu->flags.brother
2349 && (!menu->flags.app_menu||menu->parent!=NULL)) {
2350 menu->flags.buttoned=1;
2351 wFrameWindowShowButton(menu->frame, WFF_RIGHT_BUTTON);
2352 if (menu->parent) {
2353 /* turn off selected menu entry in parent menu */
2354 selectEntry(menu->parent, -1);
2356 /* make parent map the copy in place of the original */
2357 for (i=0; i<menu->parent->cascade_no; i++) {
2358 if (menu->parent->cascades[i] == menu) {
2359 menu->parent->cascades[i] = menu->brother;
2360 break;
2366 started = False;
2367 while(1) {
2368 WMMaskEvent(dpy, ButtonMotionMask|ButtonReleaseMask|ButtonPressMask
2369 |ExposureMask, &ev);
2370 switch (ev.type) {
2371 case MotionNotify:
2372 if (started) {
2373 x += ev.xmotion.x_root - dx;
2374 y += ev.xmotion.y_root - dy;
2375 dx = ev.xmotion.x_root;
2376 dy = ev.xmotion.y_root;
2377 wMenuMove(menu, x, y, True);
2378 } else {
2379 if (abs(ev.xmotion.x_root - dx) > MOVE_THRESHOLD
2380 || abs(ev.xmotion.y_root - dy) > MOVE_THRESHOLD) {
2381 started = True;
2382 XGrabPointer(dpy, menu->frame->titlebar->window, False,
2383 ButtonMotionMask|ButtonReleaseMask
2384 |ButtonPressMask,
2385 GrabModeAsync, GrabModeAsync, None,
2386 wCursor[WCUR_MOVE], CurrentTime);
2389 break;
2391 case ButtonPress:
2392 break;
2394 case ButtonRelease:
2395 if (ev.xbutton.button != event->xbutton.button)
2396 break;
2397 XUngrabPointer(dpy, CurrentTime);
2398 return;
2400 default:
2401 WMHandleEvent(&ev);
2402 break;
2408 *----------------------------------------------------------------------
2409 * menuCloseClick--
2410 * Handles mouse click on the close button of menus. The menu is
2411 * closed when the button is clicked.
2413 * Side effects:
2414 * The closed menu is reinserted at it's parent menus
2415 * cascade list.
2416 *----------------------------------------------------------------------
2418 static void
2419 menuCloseClick(WCoreWindow *sender, void *data, XEvent *event)
2421 WMenu *menu = (WMenu*)data;
2422 WMenu *parent = menu->parent;
2423 int i;
2425 if (parent) {
2426 for (i=0; i<parent->cascade_no; i++) {
2427 /* find the entry that points to the copy */
2428 if (parent->cascades[i] == menu->brother) {
2429 /* make it point to the original */
2430 parent->cascades[i] = menu;
2431 menu->parent = parent;
2432 break;
2436 wMenuUnmap(menu);
2440 void
2441 wMenuSaveState(WScreen *scr)
2443 proplist_t menus, key, value;
2444 int save_menus = 0;
2446 char buffer[256];
2448 menus = PLMakeDictionaryFromEntries(NULL, NULL, NULL);
2450 #ifndef LITE
2451 if (scr->switch_menu && scr->switch_menu->flags.buttoned) {
2452 sprintf(buffer, "%i,%i", scr->switch_menu->frame_x,
2453 scr->switch_menu->frame_y);
2454 key = PLMakeString("SwitchMenu");
2455 value = PLMakeString(buffer);
2456 PLInsertDictionaryEntry(menus, key, value);
2457 PLRelease(key);
2458 PLRelease(value);
2459 save_menus = 1;
2462 if (saveMenuRecurs(menus, scr, scr->root_menu))
2463 save_menus = 1;
2465 #endif /* !LITE */
2466 if (scr->workspace_menu && scr->workspace_menu->flags.buttoned) {
2467 sprintf(buffer, "%i,%i", scr->workspace_menu->frame_x,
2468 scr->workspace_menu->frame_y);
2469 key = PLMakeString("WorkspaceMenu");
2470 value = PLMakeString(buffer);
2471 PLInsertDictionaryEntry(menus, key, value);
2472 PLRelease(key);
2473 PLRelease(value);
2474 save_menus = 1;
2477 if (save_menus) {
2478 key = PLMakeString("Menus");
2479 PLInsertDictionaryEntry(scr->session_state, key, menus);
2480 PLRelease(key);
2482 PLRelease(menus);
2486 #ifndef LITE
2488 static Bool
2489 getMenuPath(WMenu *menu, char *buffer, int bufSize)
2491 Bool ok = True;
2492 int len = 0;
2494 if (!menu->flags.titled || !menu->frame->title[0])
2495 return False;
2497 len = strlen(menu->frame->title);
2498 if (len >= bufSize)
2499 return False;
2501 if (menu->parent) {
2502 ok = getMenuPath(menu->parent, buffer, bufSize - len - 1);
2503 if (!ok)
2504 return False;
2507 strcat(buffer, "\\");
2508 strcat(buffer, menu->frame->title);
2510 return True;
2513 static Bool
2514 saveMenuRecurs(proplist_t menus, WScreen *scr, WMenu *menu)
2516 proplist_t key, value;
2517 int save_menus = 0, i;
2518 char buffer[512];
2519 Bool ok = True;
2522 if (menu->flags.brother)
2523 menu = menu->brother;
2525 if (menu->flags.buttoned && menu != scr->switch_menu) {
2527 sprintf(buffer, "%i,%i", menu->frame_x, menu->frame_y);
2528 value = PLMakeString(buffer);
2530 buffer[0] = '\0';
2531 ok = getMenuPath(menu, buffer, 510);
2533 if (ok) {
2534 key = PLMakeString(buffer);
2535 PLInsertDictionaryEntry(menus, key, value);
2536 PLRelease(key);
2537 PLRelease(value);
2538 save_menus = 1;
2542 if (ok) {
2543 for (i = 0; i < menu->cascade_no; i++) {
2544 if (saveMenuRecurs(menus, scr, menu->cascades[i]))
2545 save_menus = 1;
2548 return save_menus;
2550 #endif /* !LITE */
2553 #define COMPLAIN(key) wwarning(_("bad value in menus state info:%s"), key)
2556 static int
2557 restoreMenu(WScreen *scr, proplist_t menu, int which)
2559 int x, y;
2560 WMenu *pmenu = NULL;
2562 if (!menu)
2563 return False;
2565 if (!PLIsString(menu)) {
2566 COMPLAIN("Position");
2567 return False;
2570 if (sscanf(PLGetString(menu), "%i,%i", &x, &y)!=2)
2571 COMPLAIN("Position");
2573 #ifndef LITE
2574 if (which & WSS_SWITCHMENU) {
2575 OpenSwitchMenu(scr, x, y, False);
2576 pmenu = scr->switch_menu;
2578 #endif /* !LITE */
2580 if (pmenu) {
2581 int width = MENUW(pmenu);
2582 int height = MENUH(pmenu);
2584 x = (x < -width) ? 0 : x;
2585 x = (x > scr->scr_width) ? scr->scr_width - width : x;
2586 y = (y < 0) ? 0 : y;
2587 y = (y > scr->scr_height) ? scr->scr_height - height : y;
2588 wMenuMove(pmenu, x, y, True);
2589 pmenu->flags.buttoned = 1;
2590 wFrameWindowShowButton(pmenu->frame, WFF_RIGHT_BUTTON);
2591 return True;
2593 return False;
2597 #ifndef LITE
2598 static int
2599 restoreMenuRecurs(WScreen *scr, proplist_t menus, WMenu *menu, char *path)
2601 proplist_t key, entry;
2602 char buffer[512];
2603 int i, x, y, res;
2605 if (strlen(path) + strlen(menu->frame->title) > 510)
2606 return False;
2608 sprintf(buffer, "%s\\%s", path, menu->frame->title);
2609 key = PLMakeString(buffer);
2610 entry = PLGetDictionaryEntry(menus, key);
2611 res = False;
2613 if (entry && PLIsString(entry)) {
2615 if (sscanf(PLGetString(entry), "%i,%i", &x, &y) != 2)
2616 COMPLAIN("Position");
2618 if (!menu->flags.mapped) {
2619 int width = MENUW(menu);
2620 int height = MENUH(menu);
2622 wMenuMapAt(menu, x, y, False);
2624 if (menu->parent) {
2625 /* make parent map the copy in place of the original */
2626 for (i=0; i<menu->parent->cascade_no; i++) {
2627 if (menu->parent->cascades[i] == menu) {
2628 menu->parent->cascades[i] = menu->brother;
2629 break;
2633 x = (x < -width) ? 0 : x;
2634 x = (x > scr->scr_width) ? scr->scr_width - width : x;
2635 y = (y < 0) ? 0 : y;
2636 y = (y > scr->scr_height) ? scr->scr_height - height : y;
2637 wMenuMove(menu, x, y, True);
2638 menu->flags.buttoned = 1;
2639 wFrameWindowShowButton(menu->frame, WFF_RIGHT_BUTTON);
2640 res = True;
2644 PLRelease(key);
2646 for (i=0; i<menu->cascade_no; i++) {
2647 if (restoreMenuRecurs(scr, menus, menu->cascades[i], buffer) != False)
2648 res = True;
2651 return res;
2653 #endif /* !LITE */
2656 void
2657 wMenuRestoreState(WScreen *scr)
2659 proplist_t menus, menu, key, skey;
2661 key = PLMakeString("Menus");
2662 menus = PLGetDictionaryEntry(scr->session_state, key);
2663 PLRelease(key);
2665 if (!menus)
2666 return;
2668 /* restore menus */
2670 skey = PLMakeString("SwitchMenu");
2671 menu = PLGetDictionaryEntry(menus, skey);
2672 restoreMenu(scr, menu, WSS_SWITCHMENU);
2674 #ifndef LITE
2675 if (!scr->root_menu) {
2676 OpenRootMenu(scr, scr->scr_width*2, 0, False);
2677 wMenuUnmap(scr->root_menu);
2679 restoreMenuRecurs(scr, menus, scr->root_menu, "");
2680 #endif /* !LITE */
2682 PLRelease(skey);
2686 void
2687 OpenWorkspaceMenu(WScreen *scr, int x, int y)
2689 WMenu *menu, *parent;
2690 WMenuEntry *entry;
2692 #ifndef LITE
2693 if (!scr->root_menu) {
2694 OpenRootMenu(scr, scr->scr_width*2, 0, False);
2695 wMenuUnmap(scr->root_menu);
2697 #endif
2698 menu = scr->workspace_menu;
2699 if (menu) {
2700 if (menu->flags.mapped) {
2701 if (!menu->flags.buttoned) {
2702 wMenuUnmap(menu);
2703 parent = menu->parent;
2704 if (parent && parent->selected_entry >= 0) {
2705 entry = parent->entries[parent->selected_entry];
2706 if (parent->cascades[entry->cascade] == menu) {
2707 selectEntry(parent, -1);
2708 wMenuMapAt(menu, x, y, False);
2711 } else {
2712 wRaiseFrame(menu->frame->core);
2713 wMenuMapCopyAt(menu, x, y);
2715 } else {
2716 wMenuMapAt(menu, x, y, False);