Work around the definable cursor patch.
[wmaker-crm.git] / src / menu.c
blobdba824a5c9bbd9d0c113b1719df5b7ff55b89e03
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 wRaiseFrame(menu->frame->core);
1955 close_on_exit = (bev->send_event || menu->flags.brother);
1957 smenu = findMenu(scr, &x, &y);
1958 if (!smenu) {
1959 x = -1;
1960 y = -1;
1961 } else {
1962 menu = smenu;
1965 if (menu->flags.editing) {
1966 return;
1968 entry_no = getEntryAt(menu, x, y);
1969 if (entry_no>=0) {
1970 entry = menu->entries[entry_no];
1972 if (!close_on_exit && (bev->state & ControlMask) && smenu
1973 && entry->flags.editable) {
1974 editEntry(smenu, entry);
1975 return;
1976 } else if (bev->state & ControlMask) {
1977 return;
1980 if (entry->flags.enabled && entry->cascade>=0 && menu->cascades) {
1981 WMenu *submenu = menu->cascades[entry->cascade];
1982 /* map cascade */
1983 if (submenu->flags.mapped && !submenu->flags.buttoned &&
1984 menu->selected_entry!=entry_no) {
1985 wMenuUnmap(submenu);
1987 if (!submenu->flags.mapped && !delayed_select) {
1988 selectEntry(menu, entry_no);
1989 } else if (!submenu->flags.buttoned) {
1990 selectEntry(menu, -1);
1993 } else if (!delayed_select) {
1994 selectEntry(menu, entry_no);
1997 if (!wPreferences.wrap_menus && !wPreferences.scrollable_menus) {
1998 if (!menu->timer)
1999 dragScrollMenuCallback(menu);
2003 prevx = bev->x_root;
2004 prevy = bev->y_root;
2005 while (!done) {
2006 int x, y;
2008 XAllowEvents(dpy, SyncPointer, CurrentTime);
2010 WMMaskEvent(dpy, ExposureMask|ButtonMotionMask|ButtonReleaseMask
2011 |ButtonPressMask, &ev);
2012 switch (ev.type) {
2013 case MotionNotify:
2014 smenu = findMenu(scr, &x, &y);
2016 if (smenu == NULL) {
2017 /* moved mouse out of menu */
2019 if (!delayed_select && d_data.magic) {
2020 WMDeleteTimerHandler(d_data.magic);
2021 d_data.magic = NULL;
2023 if (menu==NULL
2024 || (menu->selected_entry>=0
2025 && menu->entries[menu->selected_entry]->cascade>=0)) {
2026 prevx = ev.xmotion.x_root;
2027 prevy = ev.xmotion.y_root;
2029 break;
2031 selectEntry(menu, -1);
2032 menu = smenu;
2033 prevx = ev.xmotion.x_root;
2034 prevy = ev.xmotion.y_root;
2035 break;
2036 } else if (menu && menu!=smenu
2037 && (menu->selected_entry<0
2038 || menu->entries[menu->selected_entry]->cascade<0)) {
2039 selectEntry(menu, -1);
2041 if (!delayed_select && d_data.magic) {
2042 WMDeleteTimerHandler(d_data.magic);
2043 d_data.magic = NULL;
2045 } else {
2047 /* hysteresis for item selection */
2049 /* check if the motion was to the side, indicating that
2050 * the user may want to cross to a submenu */
2051 if (!delayed_select && menu) {
2052 int dx;
2053 Bool moved_to_submenu;/* moved to direction of submenu */
2055 dx = abs(prevx - ev.xmotion.x_root);
2057 moved_to_submenu = False;
2058 if (dx > 0 /* if moved enough to the side */
2059 /* maybe a open submenu */
2060 && menu->selected_entry>=0
2061 /* moving to the right direction */
2062 && (wPreferences.align_menus
2063 || ev.xmotion.y_root >= prevy)) {
2064 int index;
2066 index = menu->entries[menu->selected_entry]->cascade;
2067 if (index>=0) {
2068 if (menu->cascades[index]->frame_x>menu->frame_x) {
2069 if (prevx < ev.xmotion.x_root)
2070 moved_to_submenu = True;
2071 } else {
2072 if (prevx > ev.xmotion.x_root)
2073 moved_to_submenu = True;
2079 if (menu != smenu) {
2080 if (d_data.magic) {
2081 WMDeleteTimerHandler(d_data.magic);
2083 d_data.magic = NULL;
2084 } else if (moved_to_submenu) {
2085 /* while we are moving, postpone the selection */
2086 if (d_data.magic) {
2087 WMDeleteTimerHandler(d_data.magic);
2089 d_data.delayed_select = NULL;
2090 d_data.menu = menu;
2091 d_data.magic = WMAddTimerHandler(MENU_SELECT_DELAY,
2092 delaySelection,
2093 &d_data);
2094 prevx = ev.xmotion.x_root;
2095 prevy = ev.xmotion.y_root;
2096 break;
2097 } else {
2098 if (d_data.magic)
2099 WMDeleteTimerHandler(d_data.magic);
2100 d_data.magic = NULL;
2104 prevx = ev.xmotion.x_root;
2105 prevy = ev.xmotion.y_root;
2106 if (menu!=smenu) {
2107 /* pointer crossed menus */
2108 if (menu && menu->timer) {
2109 WMDeleteTimerHandler(menu->timer);
2110 menu->timer = NULL;
2112 if (smenu)
2113 dragScrollMenuCallback(smenu);
2115 menu = smenu;
2116 if (!menu->timer)
2117 dragScrollMenuCallback(menu);
2119 if (!delayed_select) {
2120 entry_no = getEntryAt(menu, x, y);
2121 if (entry_no>=0) {
2122 entry = menu->entries[entry_no];
2123 if (entry->flags.enabled && entry->cascade>=0 &&
2124 menu->cascades) {
2125 WMenu *submenu = menu->cascades[entry->cascade];
2126 if (submenu->flags.mapped && !submenu->flags.buttoned
2127 && menu->selected_entry!=entry_no) {
2128 wMenuUnmap(submenu);
2132 selectEntry(menu, entry_no);
2134 break;
2136 case ButtonPress:
2137 break;
2139 case ButtonRelease:
2140 if (ev.xbutton.button == event->xbutton.button)
2141 done=1;
2142 break;
2144 case Expose:
2145 WMHandleEvent(&ev);
2146 break;
2150 if (menu && menu->timer) {
2151 WMDeleteTimerHandler(menu->timer);
2152 menu->timer = NULL;
2154 if (d_data.magic!=NULL)
2155 WMDeleteTimerHandler(d_data.magic);
2157 if (menu && menu->selected_entry>=0) {
2158 entry = menu->entries[menu->selected_entry];
2159 if (entry->callback!=NULL && entry->flags.enabled
2160 && entry->cascade < 0) {
2161 /* blink and erase menu selection */
2162 #if (MENU_BLINK_DELAY > 0)
2163 int sel = menu->selected_entry;
2164 int i;
2166 for (i=0; i<MENU_BLINK_COUNT; i++) {
2167 paintEntry(menu, sel, False);
2168 XSync(dpy, 0);
2169 wusleep(MENU_BLINK_DELAY);
2170 paintEntry(menu, sel, True);
2171 XSync(dpy, 0);
2172 wusleep(MENU_BLINK_DELAY);
2174 #endif
2175 /* unmap the menu, it's parents and call the callback */
2176 if (!menu->flags.buttoned &&
2177 (!menu->flags.app_menu||menu->parent!=NULL)) {
2178 closeCascade(menu);
2179 } else {
2180 selectEntry(menu, -1);
2182 (*entry->callback)(menu, entry);
2184 /* If the user double clicks an entry, the entry will
2185 * be executed twice, which is not good for things like
2186 * the root menu. So, ignore any clicks that were generated
2187 * while the entry was being executed */
2188 while (XCheckTypedWindowEvent(dpy, menu->menu->window,
2189 ButtonPress, &ev));
2190 } else if (entry->callback!=NULL && entry->cascade<0) {
2191 selectEntry(menu, -1);
2192 } else {
2193 if (entry->cascade>=0 && menu->cascades
2194 && menu->cascades[entry->cascade]->flags.brother) {
2195 selectEntry(menu, -1);
2200 if (((WMenu*)desc->parent)->flags.brother || close_on_exit || !smenu)
2201 closeCascade(desc->parent);
2203 /* close the cascade windows that should not remain opened */
2204 closeBrotherCascadesOf(desc->parent);
2206 if (!wPreferences.wrap_menus)
2207 wMenuMove(parentMenu(desc->parent), old_frame_x, old_frame_y, True);
2211 void
2212 wMenuMove(WMenu *menu, int x, int y, int submenus)
2214 WMenu *submenu;
2215 int i;
2217 if (!menu) return;
2219 menu->frame_x = x;
2220 menu->frame_y = y;
2221 XMoveWindow(dpy, menu->frame->core->window, x, y);
2223 if (submenus>0 && menu->selected_entry>=0) {
2224 i = menu->entries[menu->selected_entry]->cascade;
2226 if (i>=0 && menu->cascades) {
2227 submenu = menu->cascades[i];
2228 if (submenu->flags.mapped && !submenu->flags.buttoned) {
2229 if (wPreferences.align_menus) {
2230 wMenuMove(submenu, x + MENUW(menu), y, submenus);
2231 } else {
2232 wMenuMove(submenu, x+ MENUW(menu),
2233 y + submenu->entry_height*menu->selected_entry,
2234 submenus);
2239 if (submenus<0 && menu->parent!=NULL && menu->parent->flags.mapped &&
2240 !menu->parent->flags.buttoned) {
2241 if (wPreferences.align_menus) {
2242 wMenuMove(menu->parent, x - MENUW(menu->parent), y, submenus);
2243 } else {
2244 wMenuMove(menu->parent, x - MENUW(menu->parent), menu->frame_y
2245 - menu->parent->entry_height*menu->parent->selected_entry,
2246 submenus);
2252 static void
2253 changeMenuLevels(WMenu *menu, int lower)
2255 int i;
2257 if (!lower) {
2258 ChangeStackingLevel(menu->frame->core, (!menu->parent ? WMMainMenuLevel
2259 : WMSubmenuLevel));
2260 wRaiseFrame(menu->frame->core);
2261 menu->flags.lowered = 0;
2262 } else {
2263 ChangeStackingLevel(menu->frame->core, WMNormalLevel);
2264 wLowerFrame(menu->frame->core);
2265 menu->flags.lowered = 1;
2267 for (i=0; i<menu->cascade_no; i++) {
2268 if (menu->cascades[i]
2269 && !menu->cascades[i]->flags.buttoned
2270 && menu->cascades[i]->flags.lowered!=lower) {
2271 changeMenuLevels(menu->cascades[i], lower);
2278 static void
2279 menuTitleDoubleClick(WCoreWindow *sender, void *data, XEvent *event)
2281 WMenu *menu = data;
2282 int lower;
2284 if (event->xbutton.state & MOD_MASK) {
2285 if (menu->flags.lowered) {
2286 lower = 0;
2287 } else {
2288 lower = 1;
2290 changeMenuLevels(menu, lower);
2295 static void
2296 menuTitleMouseDown(WCoreWindow *sender, void *data, XEvent *event)
2298 WMenu *menu = data;
2299 WMenu *tmp;
2300 XEvent ev;
2301 int x=menu->frame_x, y=menu->frame_y;
2302 int dx=event->xbutton.x_root, dy=event->xbutton.y_root;
2303 int i, lower;
2304 Bool started;
2306 /* can't touch the menu copy */
2307 if (menu->flags.brother)
2308 return;
2310 if (event->xbutton.button != Button1 && event->xbutton.button != Button2)
2311 return;
2313 if (event->xbutton.state & MOD_MASK) {
2314 wLowerFrame(menu->frame->core);
2315 lower = 1;
2316 } else {
2317 wRaiseFrame(menu->frame->core);
2318 lower = 0;
2320 tmp = menu;
2322 /* lower/raise all submenus */
2323 while (1) {
2324 if (tmp->selected_entry>=0 && tmp->cascades
2325 && tmp->entries[tmp->selected_entry]->cascade>=0) {
2326 tmp = tmp->cascades[tmp->entries[tmp->selected_entry]->cascade];
2327 if (!tmp || !tmp->flags.mapped)
2328 break;
2329 if (lower)
2330 wLowerFrame(tmp->frame->core);
2331 else
2332 wRaiseFrame(tmp->frame->core);
2333 } else {
2334 break;
2338 /* tear off the menu if it's a root menu or a cascade
2339 application menu */
2340 if (!menu->flags.buttoned && !menu->flags.brother
2341 && (!menu->flags.app_menu||menu->parent!=NULL)) {
2342 menu->flags.buttoned=1;
2343 wFrameWindowShowButton(menu->frame, WFF_RIGHT_BUTTON);
2344 if (menu->parent) {
2345 /* turn off selected menu entry in parent menu */
2346 selectEntry(menu->parent, -1);
2348 /* make parent map the copy in place of the original */
2349 for (i=0; i<menu->parent->cascade_no; i++) {
2350 if (menu->parent->cascades[i] == menu) {
2351 menu->parent->cascades[i] = menu->brother;
2352 break;
2358 started = False;
2359 while(1) {
2360 WMMaskEvent(dpy, ButtonMotionMask|ButtonReleaseMask|ButtonPressMask
2361 |ExposureMask, &ev);
2362 switch (ev.type) {
2363 case MotionNotify:
2364 if (started) {
2365 x += ev.xmotion.x_root - dx;
2366 y += ev.xmotion.y_root - dy;
2367 dx = ev.xmotion.x_root;
2368 dy = ev.xmotion.y_root;
2369 wMenuMove(menu, x, y, True);
2370 } else {
2371 if (abs(ev.xmotion.x_root - dx) > MOVE_THRESHOLD
2372 || abs(ev.xmotion.y_root - dy) > MOVE_THRESHOLD) {
2373 started = True;
2374 XGrabPointer(dpy, menu->frame->titlebar->window, False,
2375 ButtonMotionMask|ButtonReleaseMask
2376 |ButtonPressMask,
2377 GrabModeAsync, GrabModeAsync, None,
2378 wCursor[WCUR_MOVE], CurrentTime);
2381 break;
2383 case ButtonPress:
2384 break;
2386 case ButtonRelease:
2387 if (ev.xbutton.button != event->xbutton.button)
2388 break;
2389 XUngrabPointer(dpy, CurrentTime);
2390 return;
2392 default:
2393 WMHandleEvent(&ev);
2394 break;
2400 *----------------------------------------------------------------------
2401 * menuCloseClick--
2402 * Handles mouse click on the close button of menus. The menu is
2403 * closed when the button is clicked.
2405 * Side effects:
2406 * The closed menu is reinserted at it's parent menus
2407 * cascade list.
2408 *----------------------------------------------------------------------
2410 static void
2411 menuCloseClick(WCoreWindow *sender, void *data, XEvent *event)
2413 WMenu *menu = (WMenu*)data;
2414 WMenu *parent = menu->parent;
2415 int i;
2417 if (parent) {
2418 for (i=0; i<parent->cascade_no; i++) {
2419 /* find the entry that points to the copy */
2420 if (parent->cascades[i] == menu->brother) {
2421 /* make it point to the original */
2422 parent->cascades[i] = menu;
2423 menu->parent = parent;
2424 break;
2428 wMenuUnmap(menu);
2432 void
2433 wMenuSaveState(WScreen *scr)
2435 proplist_t menus, key, value;
2436 int save_menus = 0;
2438 char buffer[256];
2440 menus = PLMakeDictionaryFromEntries(NULL, NULL, NULL);
2442 #ifndef LITE
2443 if (scr->switch_menu && scr->switch_menu->flags.buttoned) {
2444 sprintf(buffer, "%i,%i", scr->switch_menu->frame_x,
2445 scr->switch_menu->frame_y);
2446 key = PLMakeString("SwitchMenu");
2447 value = PLMakeString(buffer);
2448 PLInsertDictionaryEntry(menus, key, value);
2449 PLRelease(key);
2450 PLRelease(value);
2451 save_menus = 1;
2454 if (saveMenuRecurs(menus, scr, scr->root_menu))
2455 save_menus = 1;
2457 #endif /* !LITE */
2458 if (scr->workspace_menu && scr->workspace_menu->flags.buttoned) {
2459 sprintf(buffer, "%i,%i", scr->workspace_menu->frame_x,
2460 scr->workspace_menu->frame_y);
2461 key = PLMakeString("WorkspaceMenu");
2462 value = PLMakeString(buffer);
2463 PLInsertDictionaryEntry(menus, key, value);
2464 PLRelease(key);
2465 PLRelease(value);
2466 save_menus = 1;
2469 if (save_menus) {
2470 key = PLMakeString("Menus");
2471 PLInsertDictionaryEntry(scr->session_state, key, menus);
2472 PLRelease(key);
2474 PLRelease(menus);
2478 #ifndef LITE
2480 static Bool
2481 getMenuPath(WMenu *menu, char *buffer, int bufSize)
2483 Bool ok = True;
2484 int len = 0;
2486 if (!menu->flags.titled || !menu->frame->title[0])
2487 return False;
2489 len = strlen(menu->frame->title);
2490 if (len >= bufSize)
2491 return False;
2493 if (menu->parent) {
2494 ok = getMenuPath(menu->parent, buffer, bufSize - len - 1);
2495 if (!ok)
2496 return False;
2499 strcat(buffer, "\\");
2500 strcat(buffer, menu->frame->title);
2502 return True;
2505 static Bool
2506 saveMenuRecurs(proplist_t menus, WScreen *scr, WMenu *menu)
2508 proplist_t key, value;
2509 int save_menus = 0, i;
2510 char buffer[512];
2511 Bool ok = True;
2514 if (menu->flags.brother)
2515 menu = menu->brother;
2517 if (menu->flags.buttoned && menu != scr->switch_menu) {
2519 sprintf(buffer, "%i,%i", menu->frame_x, menu->frame_y);
2520 value = PLMakeString(buffer);
2522 buffer[0] = '\0';
2523 ok = getMenuPath(menu, buffer, 510);
2525 if (ok) {
2526 key = PLMakeString(buffer);
2527 PLInsertDictionaryEntry(menus, key, value);
2528 PLRelease(key);
2529 PLRelease(value);
2530 save_menus = 1;
2534 if (ok) {
2535 for (i = 0; i < menu->cascade_no; i++) {
2536 if (saveMenuRecurs(menus, scr, menu->cascades[i]))
2537 save_menus = 1;
2540 return save_menus;
2542 #endif /* !LITE */
2545 #define COMPLAIN(key) wwarning(_("bad value in menus state info:%s"), key)
2548 static int
2549 restoreMenu(WScreen *scr, proplist_t menu, int which)
2551 int x, y;
2552 WMenu *pmenu = NULL;
2554 if (!menu)
2555 return False;
2557 if (!PLIsString(menu)) {
2558 COMPLAIN("Position");
2559 return False;
2562 if (sscanf(PLGetString(menu), "%i,%i", &x, &y)!=2)
2563 COMPLAIN("Position");
2565 #ifndef LITE
2566 if (which & WSS_SWITCHMENU) {
2567 OpenSwitchMenu(scr, x, y, False);
2568 pmenu = scr->switch_menu;
2570 #endif /* !LITE */
2572 if (pmenu) {
2573 int width = MENUW(pmenu);
2574 int height = MENUH(pmenu);
2576 x = (x < -width) ? 0 : x;
2577 x = (x > scr->scr_width) ? scr->scr_width - width : x;
2578 y = (y < 0) ? 0 : y;
2579 y = (y > scr->scr_height) ? scr->scr_height - height : y;
2580 wMenuMove(pmenu, x, y, True);
2581 pmenu->flags.buttoned = 1;
2582 wFrameWindowShowButton(pmenu->frame, WFF_RIGHT_BUTTON);
2583 return True;
2585 return False;
2589 #ifndef LITE
2590 static int
2591 restoreMenuRecurs(WScreen *scr, proplist_t menus, WMenu *menu, char *path)
2593 proplist_t key, entry;
2594 char buffer[512];
2595 int i, x, y, res;
2597 if (strlen(path) + strlen(menu->frame->title) > 510)
2598 return False;
2600 sprintf(buffer, "%s\\%s", path, menu->frame->title);
2601 key = PLMakeString(buffer);
2602 entry = PLGetDictionaryEntry(menus, key);
2603 res = False;
2605 if (entry && PLIsString(entry)) {
2607 if (sscanf(PLGetString(entry), "%i,%i", &x, &y) != 2)
2608 COMPLAIN("Position");
2610 if (!menu->flags.mapped) {
2611 int width = MENUW(menu);
2612 int height = MENUH(menu);
2614 wMenuMapAt(menu, x, y, False);
2616 if (menu->parent) {
2617 /* make parent map the copy in place of the original */
2618 for (i=0; i<menu->parent->cascade_no; i++) {
2619 if (menu->parent->cascades[i] == menu) {
2620 menu->parent->cascades[i] = menu->brother;
2621 break;
2625 x = (x < -width) ? 0 : x;
2626 x = (x > scr->scr_width) ? scr->scr_width - width : x;
2627 y = (y < 0) ? 0 : y;
2628 y = (y > scr->scr_height) ? scr->scr_height - height : y;
2629 wMenuMove(menu, x, y, True);
2630 menu->flags.buttoned = 1;
2631 wFrameWindowShowButton(menu->frame, WFF_RIGHT_BUTTON);
2632 res = True;
2636 PLRelease(key);
2638 for (i=0; i<menu->cascade_no; i++) {
2639 if (restoreMenuRecurs(scr, menus, menu->cascades[i], buffer) != False)
2640 res = True;
2643 return res;
2645 #endif /* !LITE */
2648 void
2649 wMenuRestoreState(WScreen *scr)
2651 proplist_t menus, menu, key, skey;
2653 key = PLMakeString("Menus");
2654 menus = PLGetDictionaryEntry(scr->session_state, key);
2655 PLRelease(key);
2657 if (!menus)
2658 return;
2660 /* restore menus */
2662 skey = PLMakeString("SwitchMenu");
2663 menu = PLGetDictionaryEntry(menus, skey);
2664 restoreMenu(scr, menu, WSS_SWITCHMENU);
2666 #ifndef LITE
2667 if (!scr->root_menu) {
2668 OpenRootMenu(scr, scr->scr_width*2, 0, False);
2669 wMenuUnmap(scr->root_menu);
2671 restoreMenuRecurs(scr, menus, scr->root_menu, "");
2672 #endif /* !LITE */
2674 PLRelease(skey);
2678 void
2679 OpenWorkspaceMenu(WScreen *scr, int x, int y)
2681 WMenu *menu, *parent;
2682 WMenuEntry *entry;
2684 #ifndef LITE
2685 if (!scr->root_menu) {
2686 OpenRootMenu(scr, scr->scr_width*2, 0, False);
2687 wMenuUnmap(scr->root_menu);
2689 #endif
2690 menu = scr->workspace_menu;
2691 if (menu) {
2692 if (menu->flags.mapped) {
2693 if (!menu->flags.buttoned) {
2694 wMenuUnmap(menu);
2695 parent = menu->parent;
2696 if (parent && parent->selected_entry >= 0) {
2697 entry = parent->entries[parent->selected_entry];
2698 if (parent->cascades[entry->cascade] == menu) {
2699 selectEntry(parent, -1);
2700 wMenuMapAt(menu, x, y, False);
2703 } else {
2704 wRaiseFrame(menu->frame->core);
2705 wMenuMapCopyAt(menu, x, y);
2707 } else {
2708 wMenuMapAt(menu, x, y, False);