fixed many bugs, removed linked list
[wmaker-crm.git] / src / menu.c
blob20bb572de48d39fd99b16843ad1dfa0e29d7cefb
1 /* menu.c- generic menu, used for root menu, application menus etc.
3 * Window Maker window manager
4 *
5 * Copyright (c) 1997, 1998 Alfredo K. Kojima
6 * Copyright (c) 1997, 1998 Dan Pascu
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
21 * USA.
24 #include "wconfig.h"
26 #include <X11/Xlib.h>
27 #include <X11/Xutil.h>
28 #include <X11/keysym.h>
29 #include <stdlib.h>
30 #include <string.h>
31 #include <stdio.h>
32 #include <unistd.h>
33 #include <ctype.h>
35 #include "WindowMaker.h"
36 #include "wcore.h"
37 #include "framewin.h"
38 #include "menu.h"
39 #include "actions.h"
40 #include "funcs.h"
41 #include "stacking.h"
42 #include "text.h"
45 /****** Global Variables ******/
47 extern Cursor wCursor[WCUR_LAST];
49 extern XContext wWinContext;
51 extern WPreferences wPreferences;
53 #define MOD_MASK wPreferences.modifier_mask
55 #define MENU_SCROLL_STEP menuScrollParameters[(int)wPreferences.menu_scroll_speed].steps
56 #define MENU_SCROLL_DELAY menuScrollParameters[(int)wPreferences.menu_scroll_speed].delay
60 #define MENUW(m) ((m)->frame->core->width+2*FRAME_BORDER_WIDTH)
61 #define MENUH(m) ((m)->frame->core->height+2*FRAME_BORDER_WIDTH)
64 /***** Local Stuff ******/
66 static struct {
67 int steps;
68 int delay;
69 } menuScrollParameters[5] = {
70 {MENU_SCROLL_STEPS_UF, MENU_SCROLL_DELAY_UF},
71 {MENU_SCROLL_STEPS_F, MENU_SCROLL_DELAY_F},
72 {MENU_SCROLL_STEPS_M, MENU_SCROLL_DELAY_M},
73 {MENU_SCROLL_STEPS_S, MENU_SCROLL_DELAY_S},
74 {MENU_SCROLL_STEPS_U, MENU_SCROLL_DELAY_U}};
77 static void menuMouseDown(WObjDescriptor *desc, XEvent *event);
78 static void menuExpose(WObjDescriptor *desc, XEvent *event);
80 static void menuTitleDoubleClick(WCoreWindow *sender, void *data, XEvent *event);
81 static void menuTitleMouseDown(WCoreWindow *sender, void *data, XEvent *event);
83 static void menuCloseClick(WCoreWindow *sender, void *data, XEvent *event);
85 static void updateTexture(WMenu *menu);
87 #ifndef LITE
88 static int saveMenuRecurs(proplist_t menus, WScreen *scr, WMenu *menu);
89 static int restoreMenuRecurs(WScreen *scr, proplist_t menus, WMenu *menu, char *path);
90 #endif /* !LITE */
92 static void selectEntry(WMenu *menu, int entry_no);
93 static void closeCascade(WMenu *menu);
96 /****** Notification Observers ******/
98 static void
99 appearanceObserver(void *self, WMNotification *notif)
101 WMenu *menu = (WMenu*)self;
102 int flags = (int)WMGetNotificationClientData(notif);
104 if (!menu->flags.realized)
105 return;
107 if (WMGetNotificationName(notif) == WNMenuAppearanceSettingsChanged) {
108 if (flags & WFontSettings) {
109 menu->flags.realized = 0;
110 wMenuRealize(menu);
112 if (flags & WTextureSettings) {
113 if (!menu->flags.brother)
114 updateTexture(menu);
116 if (flags & (WTextureSettings|WColorSettings)) {
117 wMenuPaint(menu);
119 } else if (menu->flags.titled) {
121 if (flags & WFontSettings) {
122 menu->flags.realized = 0;
123 wMenuRealize(menu);
125 if (flags & WTextureSettings) {
126 menu->frame->flags.need_texture_remake = 1;
128 if (flags & (WColorSettings|WTextureSettings)) {
129 wFrameWindowPaint(menu->frame);
134 /************************************/
138 *----------------------------------------------------------------------
139 * wMenuCreate--
140 * Creates a new empty menu with the specified title. If main_menu
141 * is True, the created menu will be a main menu, which has some special
142 * properties such as being placed over other normal menus.
143 * If title is NULL, the menu will have no titlebar.
145 * Returns:
146 * The created menu.
147 *----------------------------------------------------------------------
149 WMenu*
150 wMenuCreate(WScreen *screen, char *title, int main_menu)
152 WMenu *menu;
153 static int brother=0;
154 int tmp, flags;
156 menu = wmalloc(sizeof(WMenu));
158 memset(menu, 0, sizeof(WMenu));
160 #ifdef SINGLE_MENULEVEL
161 tmp = WMSubmenuLevel;
162 #else
163 tmp = (main_menu ? WMMainMenuLevel : WMSubmenuLevel);
164 #endif
166 flags = WFF_SINGLE_STATE;
167 if (title) {
168 flags |= WFF_TITLEBAR|WFF_RIGHT_BUTTON;
169 menu->flags.titled = 1;
171 menu->frame =
172 wFrameWindowCreate(screen, tmp, 8, 2, 1, 1, flags,
173 screen->menu_title_texture, NULL,
174 screen->menu_title_pixel,
175 #ifdef DRAWSTRING_PLUGIN
176 W_STRING_MTITLE,
177 #endif
178 &screen->menu_title_gc,
179 &screen->menu_title_font);
181 menu->frame->core->descriptor.parent = menu;
182 menu->frame->core->descriptor.parent_type = WCLASS_MENU;
183 menu->frame->core->descriptor.handle_mousedown = menuMouseDown;
185 wFrameWindowHideButton(menu->frame, WFF_RIGHT_BUTTON);
187 if (title) {
188 menu->frame->title = wstrdup(title);
191 menu->frame->flags.justification = WTJ_LEFT;
193 menu->frame->rbutton_image = screen->b_pixmaps[WBUT_CLOSE];
195 menu->entry_no = 0;
196 menu->alloced_entries = 0;
197 menu->selected_entry = -1;
198 menu->entries = NULL;
200 menu->frame_x = screen->app_menu_x;
201 menu->frame_y = screen->app_menu_y;
203 menu->frame->child = menu;
205 menu->flags.lowered = 0;
207 /* create borders */
208 if (title) {
209 /* setup object descriptors */
210 menu->frame->on_mousedown_titlebar = menuTitleMouseDown;
211 menu->frame->on_dblclick_titlebar = menuTitleDoubleClick;
214 menu->frame->on_click_right = menuCloseClick;
217 menu->menu = wCoreCreate(menu->frame->core, 0, menu->frame->top_width,
218 menu->frame->core->width, 10);
220 menu->menu->descriptor.parent = menu;
221 menu->menu->descriptor.parent_type = WCLASS_MENU;
222 menu->menu->descriptor.handle_expose = menuExpose;
223 menu->menu->descriptor.handle_mousedown = menuMouseDown;
225 menu->menu_texture_data = None;
227 XMapWindow(dpy, menu->menu->window);
229 XFlush(dpy);
231 if (!brother) {
232 brother = 1;
233 menu->brother = wMenuCreate(screen, title, main_menu);
234 brother = 0;
235 menu->brother->flags.brother = 1;
236 menu->brother->brother = menu;
238 WMAddNotificationObserver(appearanceObserver, menu,
239 WNMenuAppearanceSettingsChanged, menu);
241 WMAddNotificationObserver(appearanceObserver, menu,
242 WNMenuTitleAppearanceSettingsChanged, menu);
245 return menu;
251 WMenu*
252 wMenuCreateForApp(WScreen *screen, char *title, int main_menu)
254 WMenu *menu;
256 menu = wMenuCreate(screen, title, main_menu);
257 if (!menu)
258 return NULL;
259 menu->flags.app_menu = 1;
260 menu->brother->flags.app_menu = 1;
262 return menu;
267 static void
268 insertEntry(WMenu *menu, WMenuEntry *entry, int index)
270 int i;
272 for (i = menu->entry_no-1; i >= index; i--) {
273 menu->entries[i]->order++;
274 menu->entries[i+1] = menu->entries[i];
276 menu->entries[index] = entry;
280 WMenuEntry*
281 wMenuInsertCallback(WMenu *menu, int index, char *text,
282 void (*callback)(WMenu *menu, WMenuEntry *entry),
283 void *clientdata)
285 WMenuEntry *entry;
287 #ifdef DEBUG
288 if (!menu) {
289 dprintf("Passed NULL as menu parameter to wMenuAddCallback() \n");
290 return NULL;
292 #endif
294 assert(menu->flags.brother==0);
295 menu->flags.realized = 0;
296 menu->brother->flags.realized = 0;
298 /* reallocate array if it's too small */
299 if (menu->entry_no >= menu->alloced_entries) {
300 void *tmp;
301 #ifdef DEBUG
302 dputs("doing wrealloc()");
303 #endif
304 tmp = wrealloc(menu->entries,
305 sizeof(WMenuEntry)*(menu->alloced_entries+5));
306 if (tmp==NULL) {
307 wwarning(_("wrealloc() failed while trying to add menu item"));
308 return NULL;
311 menu->entries = tmp;
312 menu->alloced_entries += 5;
314 menu->brother->entries = tmp;
315 menu->brother->alloced_entries = menu->alloced_entries;
317 entry = wmalloc(sizeof(WMenuEntry));
318 memset(entry, 0, sizeof(WMenuEntry));
319 entry->flags.enabled = 1;
320 entry->text = wstrdup(text);
321 entry->cascade = -1;
322 entry->clientdata = clientdata;
323 entry->callback = callback;
324 if (index<0 || index>=menu->entry_no) {
325 entry->order = menu->entry_no;
326 menu->entries[menu->entry_no] = entry;
327 } else {
328 entry->order = index;
329 insertEntry(menu, entry, index);
332 menu->entry_no++;
333 menu->brother->entry_no = menu->entry_no;
335 return entry;
340 void
341 wMenuEntrySetCascade(WMenu *menu, WMenuEntry *entry, WMenu *cascade)
343 WMenu *brother = menu->brother;
344 int i, done;
346 assert(menu->flags.brother==0);
348 if (entry->cascade>=0) {
349 menu->flags.realized = 0;
350 brother->flags.realized = 0;
353 cascade->parent = menu;
355 cascade->brother->parent = brother;
357 done = 0;
358 for (i=0; i<menu->cascade_no; i++) {
359 if (menu->cascades[i]==NULL) {
360 menu->cascades[i] = cascade;
361 brother->cascades[i] = cascade->brother;
362 done = 1;
363 entry->cascade = i;
364 break;
367 if (!done) {
368 entry->cascade = menu->cascade_no;
370 menu->cascades = wrealloc(menu->cascades,
371 sizeof(WMenu)*(menu->cascade_no+1));
372 menu->cascades[menu->cascade_no++] = cascade;
375 brother->cascades = wrealloc(brother->cascades,
376 sizeof(WMenu)*(brother->cascade_no+1));
377 brother->cascades[brother->cascade_no++] = cascade->brother;
381 if (menu->flags.lowered) {
383 cascade->flags.lowered = 1;
384 ChangeStackingLevel(cascade->frame->core, WMNormalLevel);
386 cascade->brother->flags.lowered = 1;
387 ChangeStackingLevel(cascade->brother->frame->core, WMNormalLevel);
390 if (!menu->flags.realized)
391 wMenuRealize(menu);
395 void
396 wMenuEntryRemoveCascade(WMenu *menu, WMenuEntry *entry)
398 assert(menu->flags.brother==0);
400 /* destroy cascade menu */
401 if (entry->cascade>=0 && menu->cascades
402 && menu->cascades[entry->cascade]!=NULL) {
404 wMenuDestroy(menu->cascades[entry->cascade], True);
406 menu->cascades[entry->cascade] = NULL;
407 menu->brother->cascades[entry->cascade] = NULL;
409 entry->cascade = -1;
414 void
415 wMenuRemoveItem(WMenu *menu, int index)
417 int i;
419 if (menu->flags.brother) {
420 wMenuRemoveItem(menu->brother, index);
421 return;
424 if (index>=menu->entry_no) return;
426 /* destroy cascade menu */
427 wMenuEntryRemoveCascade(menu, menu->entries[index]);
429 /* destroy unshared data */
431 if (menu->entries[index]->text)
432 free(menu->entries[index]->text);
434 if (menu->entries[index]->rtext)
435 free(menu->entries[index]->rtext);
437 if (menu->entries[index]->free_cdata && menu->entries[index]->clientdata)
438 (*menu->entries[index]->free_cdata)(menu->entries[index]->clientdata);
440 free(menu->entries[index]);
442 for (i=index; i<menu->entry_no-1; i++) {
443 menu->entries[i+1]->order--;
444 menu->entries[i]=menu->entries[i+1];
446 menu->entry_no--;
447 menu->brother->entry_no--;
451 static Pixmap
452 renderTexture(WMenu *menu)
454 RImage *img;
455 Pixmap pix;
456 int i;
457 RColor light;
458 RColor dark;
459 RColor mid;
460 WScreen *scr = menu->menu->screen_ptr;
461 WTexture *texture = scr->menu_item_texture;
463 if (wPreferences.menu_style == MS_NORMAL) {
464 img = wTextureRenderImage(texture, menu->menu->width,
465 menu->entry_height, WREL_MENUENTRY);
466 } else {
467 img = wTextureRenderImage(texture, menu->menu->width,
468 menu->menu->height+1, WREL_MENUENTRY);
470 if (!img) {
471 wwarning(_("could not render texture: %s"),
472 RMessageForError(RErrorCode));
474 return None;
477 if (wPreferences.menu_style == MS_SINGLE_TEXTURE) {
478 light.alpha = 0;
479 light.red = light.green = light.blue = 80;
481 dark.alpha = 255;
482 dark.red = dark.green = dark.blue = 0;
484 mid.alpha = 0;
485 mid.red = mid.green = mid.blue = 40;
487 for (i = 1; i < menu->entry_no; i++) {
488 ROperateLine(img, RSubtractOperation, 0, i*menu->entry_height-2,
489 menu->menu->width-1, i*menu->entry_height-2, &mid);
491 RDrawLine(img, 0, i*menu->entry_height-1,
492 menu->menu->width-1, i*menu->entry_height-1, &dark);
494 ROperateLine(img, RAddOperation, 0, i*menu->entry_height,
495 menu->menu->width-1, i*menu->entry_height,
496 &light);
499 if (!RConvertImage(scr->rcontext, img, &pix)) {
500 wwarning(_("error rendering image:%s"), RMessageForError(RErrorCode));
502 RDestroyImage(img);
504 return pix;
508 static void
509 updateTexture(WMenu *menu)
511 WScreen *scr = menu->menu->screen_ptr;
513 /* setup background texture */
514 if (scr->menu_item_texture->any.type != WTEX_SOLID) {
515 if (!menu->flags.brother) {
516 FREE_PIXMAP(menu->menu_texture_data);
518 menu->menu_texture_data = renderTexture(menu);
520 XSetWindowBackgroundPixmap(dpy, menu->menu->window,
521 menu->menu_texture_data);
522 XClearWindow(dpy, menu->menu->window);
524 XSetWindowBackgroundPixmap(dpy, menu->brother->menu->window,
525 menu->menu_texture_data);
526 XClearWindow(dpy, menu->brother->menu->window);
528 } else {
529 XSetWindowBackground(dpy, menu->menu->window,
530 scr->menu_item_texture->any.color.pixel);
531 XClearWindow(dpy, menu->menu->window);
536 void
537 wMenuRealize(WMenu *menu)
539 int i;
540 int width, rwidth, mrwidth, mwidth;
541 int theight, twidth, eheight;
542 WScreen *scr = menu->frame->screen_ptr;
543 static int brother_done=0;
544 int flags;
546 if (!brother_done) {
547 brother_done = 1;
548 wMenuRealize(menu->brother);
549 brother_done = 0;
552 flags = WFF_SINGLE_STATE;
553 if (menu->flags.titled)
554 flags |= WFF_TITLEBAR|WFF_RIGHT_BUTTON;
556 wFrameWindowUpdateBorders(menu->frame, flags);
558 if (menu->flags.titled) {
559 twidth = WMWidthOfString(scr->menu_title_font, menu->frame->title,
560 strlen(menu->frame->title));
561 theight = menu->frame->top_width;
562 twidth += theight + (wPreferences.new_style ? 16 : 8);
563 } else {
564 twidth = 0;
565 theight = 0;
567 eheight = WMFontHeight(scr->menu_entry_font) + 6;
568 menu->entry_height = eheight;
569 mrwidth = 0;
570 mwidth = 0;
571 for (i=0; i<menu->entry_no; i++) {
572 char *text;
574 /* search widest text */
575 text = menu->entries[i]->text;
576 width = WMWidthOfString(scr->menu_entry_font, text, strlen(text))+10;
578 if (menu->entries[i]->flags.indicator) {
579 width += MENU_INDICATOR_SPACE;
582 if (width > mwidth)
583 mwidth = width;
585 /* search widest text on right */
586 text = menu->entries[i]->rtext;
587 if (text)
588 rwidth = WMWidthOfString(scr->menu_entry_font, text, strlen(text))
589 + 5;
590 else if (menu->entries[i]->cascade>=0)
591 rwidth = 16;
592 else
593 rwidth = 4;
595 if (rwidth > mrwidth)
596 mrwidth = rwidth;
598 mwidth += mrwidth;
600 if (mwidth < twidth)
601 mwidth = twidth;
604 wCoreConfigure(menu->menu, 0, theight, mwidth, menu->entry_no*eheight -1);
606 wFrameWindowResize(menu->frame, mwidth, menu->entry_no*eheight-1
607 + menu->frame->top_width + menu->frame->bottom_width);
610 updateTexture(menu);
612 menu->flags.realized = 1;
614 if (menu->flags.mapped)
615 wMenuPaint(menu);
616 if (menu->brother->flags.mapped)
617 wMenuPaint(menu->brother);
621 void
622 wMenuDestroy(WMenu *menu, int recurse)
624 int i;
626 WMRemoveNotificationObserver(menu);
628 /* remove any pending timers */
629 if (menu->timer)
630 WMDeleteTimerHandler(menu->timer);
631 menu->timer = NULL;
633 /* call destroy handler */
634 if (menu->on_destroy)
635 (*menu->on_destroy)(menu);
637 /* Destroy items if this menu own them. If this is the "brother" menu,
638 * leave them alone as it is shared by them.
640 if (!menu->flags.brother) {
641 for (i=0; i<menu->entry_no; i++) {
643 free(menu->entries[i]->text);
645 if (menu->entries[i]->rtext)
646 free(menu->entries[i]->rtext);
647 #ifdef USER_MENU
649 if (menu->entries[i]->instances){
650 PLRelease(menu->entries[i]->instances);
652 #endif /* USER_MENU */
654 if (menu->entries[i]->free_cdata && menu->entries[i]->clientdata) {
655 (*menu->entries[i]->free_cdata)(menu->entries[i]->clientdata);
657 free(menu->entries[i]);
660 if (recurse) {
661 for (i=0; i<menu->cascade_no; i++) {
662 if (menu->cascades[i]) {
663 if (menu->cascades[i]->flags.brother)
664 wMenuDestroy(menu->cascades[i]->brother, recurse);
665 else
666 wMenuDestroy(menu->cascades[i], recurse);
671 if (menu->entries)
672 free(menu->entries);
676 FREE_PIXMAP(menu->menu_texture_data);
678 if (menu->cascades)
679 free(menu->cascades);
681 wCoreDestroy(menu->menu);
682 wFrameWindowDestroy(menu->frame);
684 /* destroy copy of this menu */
685 if (!menu->flags.brother && menu->brother)
686 wMenuDestroy(menu->brother, False);
688 free(menu);
692 #define F_NORMAL 0
693 #define F_TOP 1
694 #define F_BOTTOM 2
695 #define F_NONE 3
697 static void
698 drawFrame(WScreen *scr, Window win, int y, int w, int h, int type)
700 XSegment segs[2];
701 int i;
703 i = 0;
704 segs[i].x1 = segs[i].x2 = w-1;
705 segs[i].y1 = y;
706 segs[i].y2 = y + h - 1;
707 i++;
708 if (type != F_TOP && type != F_NONE) {
709 segs[i].x1 = 1;
710 segs[i].y1 = segs[i].y2 = y + h-2;
711 segs[i].x2 = w-1;
712 i++;
714 XDrawSegments(dpy, win, scr->menu_item_auxtexture->dim_gc, segs, i);
716 i = 0;
717 segs[i].x1 = 0;
718 segs[i].y1 = y;
719 segs[i].x2 = 0;
720 segs[i].y2 = y + h - 1;
721 i++;
722 if (type != F_BOTTOM && type != F_NONE) {
723 segs[i].x1 = 0;
724 segs[i].y1 = y;
725 segs[i].x2 = w-1;
726 segs[i].y2 = y;
727 i++;
729 XDrawSegments(dpy, win, scr->menu_item_auxtexture->light_gc, segs, i);
731 if (type != F_TOP && type != F_NONE)
732 XDrawLine(dpy, win, scr->menu_item_auxtexture->dark_gc, 0, y+h-1,
733 w-1, y+h-1);
737 static void
738 paintEntry(WMenu *menu, int index, int selected)
740 int x, y, w, h, tw;
741 int type;
742 GC light, dim, dark, textGC;
743 WScreen *scr=menu->frame->screen_ptr;
744 Window win = menu->menu->window;
745 WMenuEntry *entry=menu->entries[index];
747 if (!menu->flags.realized) return;
748 h = menu->entry_height;
749 w = menu->menu->width;
750 y = index * h;
752 light = scr->menu_item_auxtexture->light_gc;
753 dim = scr->menu_item_auxtexture->dim_gc;
754 dark = scr->menu_item_auxtexture->dark_gc;
756 if (wPreferences.menu_style == MS_FLAT && menu->entry_no > 1) {
757 if (index == 0)
758 type = F_TOP;
759 else if (index == menu->entry_no - 1)
760 type = F_BOTTOM;
761 else
762 type = F_NONE;
763 } else {
764 type = F_NORMAL;
767 /* paint background */
768 if (selected) {
769 XSetForeground(dpy, scr->select_menu_gc, scr->select_pixel);
770 XFillRectangle(dpy, win, scr->select_menu_gc, 1, y+1, w-2, h-3);
771 if (scr->menu_item_texture->any.type == WTEX_SOLID)
772 drawFrame(scr, win, y, w, h, type);
773 } else {
774 if (scr->menu_item_texture->any.type == WTEX_SOLID) {
775 XClearArea(dpy, win, 0, y + 1, w - 1, h - 3, False);
776 /* draw the frame */
777 drawFrame(scr, win, y, w, h, type);
778 } else {
779 XClearArea(dpy, win, 0, y, w, h, False);
783 if (selected) {
784 textGC = scr->select_menu_gc;
785 if (entry->flags.enabled)
786 XSetForeground(dpy, textGC, scr->select_text_pixel);
787 else
788 XSetForeground(dpy, textGC, scr->dtext_pixel);
789 } else if (!entry->flags.enabled) {
790 textGC = scr->disabled_menu_entry_gc;
791 } else {
792 textGC = scr->menu_entry_gc;
794 /* draw text */
795 x = 5;
796 if (entry->flags.indicator)
797 x += MENU_INDICATOR_SPACE + 2;
799 WMDrawString(scr->wmscreen, win, textGC, scr->menu_entry_font,
800 x, 3 + y, entry->text, strlen(entry->text));
802 if (entry->cascade>=0) {
803 /* draw the cascade indicator */
804 XDrawLine(dpy,win,dim, w-11, y+6, w-6, y+h/2-1);
805 XDrawLine(dpy,win,light, w-11, y+h-8, w-6, y+h/2-1);
806 XDrawLine(dpy,win,dark, w-12, y+6, w-12, y+h-8);
809 /* draw indicator */
810 if (entry->flags.indicator && entry->flags.indicator_on) {
811 int iw, ih;
812 WPixmap *indicator;
815 switch (entry->flags.indicator_type) {
816 case MI_CHECK:
817 indicator = scr->menu_check_indicator;
818 break;
819 case MI_MINIWINDOW:
820 indicator = scr->menu_mini_indicator;
821 break;
822 case MI_HIDDEN:
823 indicator = scr->menu_hide_indicator;
824 break;
825 case MI_SHADED:
826 indicator = scr->menu_shade_indicator;
827 break;
828 case MI_DIAMOND:
829 default:
830 indicator = scr->menu_radio_indicator;
831 break;
834 iw = indicator->width;
835 ih = indicator->height;
836 XSetClipMask(dpy, scr->copy_gc, indicator->mask);
837 XSetClipOrigin(dpy, scr->copy_gc, 5, y+(h-ih)/2);
838 if (selected)
839 XSetForeground(dpy, scr->copy_gc, scr->black_pixel);
840 else
841 XSetForeground(dpy, scr->copy_gc, scr->mtext_pixel);
842 XFillRectangle(dpy, win, scr->copy_gc, 5, y+(h-ih)/2, iw, ih);
844 XCopyArea(dpy, indicator->image, win, scr->copy_gc, 0, 0,
845 iw, ih, 5, y+(h-ih)/2);
847 XSetClipOrigin(dpy, scr->copy_gc, 0, 0);
850 /* draw right text */
852 if (entry->rtext && entry->cascade<0) {
853 tw = WMWidthOfString(scr->menu_entry_font, entry->rtext,
854 strlen(entry->rtext));
856 WMDrawString(scr->wmscreen, win, textGC, scr->menu_entry_font, w-6-tw,
857 y + 3, entry->rtext, strlen(entry->rtext));
862 static void
863 move_menus(WMenu *menu, int x, int y)
865 while (menu->parent) {
866 menu = menu->parent;
867 x -= MENUW(menu);
868 if (!wPreferences.align_menus && menu->selected_entry>=0) {
869 y -= menu->selected_entry*menu->entry_height;
872 wMenuMove(menu, x, y, True);
875 static void
876 makeVisible(WMenu *menu)
878 WScreen *scr = menu->frame->screen_ptr;
879 int x1, y1, x2, y2, new_x, new_y, move;
881 if (menu->entry_no<0) return;
883 x1 = menu->frame_x;
884 y1 = menu->frame_y+menu->frame->top_width
885 + menu->selected_entry*menu->entry_height;
886 x2 = x1 + MENUW(menu);
887 y2 = y1 + menu->entry_height;
889 new_x = x1;
890 new_y = y1;
891 move = 0;
893 if (x1 < 0) {
894 new_x = 0;
895 move = 1;
896 } else if (x2 >= scr->scr_width) {
897 new_x = scr->scr_width - MENUW(menu) - 1;
898 move = 1;
901 if (y1 < 0) {
902 new_y = 0;
903 move = 1;
904 } else if (y2 >= scr->scr_height) {
905 new_y = scr->scr_height - menu->entry_height - 1;
906 move = 1;
909 new_y = new_y - menu->frame->top_width
910 - menu->selected_entry*menu->entry_height;
911 move_menus(menu, new_x, new_y);
915 static int
916 check_key(WMenu *menu, XKeyEvent *event)
918 int i, ch, s;
919 char buffer[32];
921 if (XLookupString(event, buffer, 32, NULL, NULL)<1)
922 return -1;
924 ch = toupper(buffer[0]);
926 s = (menu->selected_entry>=0 ? menu->selected_entry+1 : 0);
928 again:
929 for (i=s; i<menu->entry_no; i++) {
930 if (ch==toupper(menu->entries[i]->text[0])) {
931 return i;
934 /* no match. Retry from start, if previous started from a selected entry */
935 if (s!=0) {
936 s = 0;
937 goto again;
939 return -1;
943 static int
944 keyboardMenu(WMenu *menu)
946 XEvent event;
947 KeySym ksym=NoSymbol;
948 int done=0;
949 int index;
950 WMenuEntry *entry;
951 int old_pos_x = menu->frame_x;
952 int old_pos_y = menu->frame_y;
953 int new_x = old_pos_x, new_y = old_pos_y;
954 int scr_width = menu->frame->screen_ptr->scr_width;
955 int scr_height = menu->frame->screen_ptr->scr_height;
957 if (menu->flags.editing)
958 return False;
961 XGrabKeyboard(dpy, menu->frame->core->window, True, GrabModeAsync,
962 GrabModeAsync, CurrentTime);
964 if (menu->frame_y+menu->frame->top_width >= scr_height)
965 new_y = scr_height - menu->frame->top_width;
967 if (menu->frame_x+MENUW(menu) >= scr_width)
968 new_x = scr_width-MENUW(menu)-1;
970 move_menus(menu, new_x, new_y);
972 while (!done && menu->flags.mapped) {
973 XAllowEvents(dpy, AsyncKeyboard, CurrentTime);
974 WMMaskEvent(dpy, ExposureMask|ButtonMotionMask|ButtonPressMask
975 |ButtonReleaseMask|KeyPressMask|KeyReleaseMask
976 |SubstructureNotifyMask, &event);
978 switch (event.type) {
979 case KeyPress:
980 ksym = XLookupKeysym(&event.xkey, 0);
981 switch (ksym) {
982 case XK_Escape:
983 done = 1;
984 break;
986 case XK_Home:
987 case XK_KP_Home:
988 selectEntry(menu, 0);
989 makeVisible(menu);
990 break;
992 case XK_End:
993 case XK_KP_End:
994 selectEntry(menu, menu->entry_no-1);
995 makeVisible(menu);
996 break;
998 case XK_Up:
999 #ifdef ARROWLESS_KBD
1000 case XK_k:
1001 #endif
1002 case XK_KP_Up:
1003 if (menu->selected_entry <= 0)
1004 selectEntry(menu, menu->entry_no-1);
1005 else
1006 selectEntry(menu, menu->selected_entry-1);
1007 makeVisible(menu);
1008 break;
1010 case XK_Down:
1011 #ifdef ARROWLESS_KBD
1012 case XK_j:
1013 #endif
1014 case XK_KP_Down:
1015 if (menu->selected_entry<0)
1016 selectEntry(menu, 0);
1017 else if (menu->selected_entry == menu->entry_no-1)
1018 selectEntry(menu, 0);
1019 else if (menu->selected_entry < menu->entry_no-1)
1020 selectEntry(menu, menu->selected_entry+1);
1021 makeVisible(menu);
1022 break;
1024 case XK_Right:
1025 #ifdef ARROWLESS_KBD
1026 case XK_l:
1027 #endif
1028 case XK_KP_Right:
1029 if (menu->selected_entry>=0) {
1030 WMenuEntry *entry;
1031 entry = menu->entries[menu->selected_entry];
1033 if (entry->cascade >= 0 && menu->cascades
1034 && menu->cascades[entry->cascade]->entry_no > 0) {
1036 XUngrabKeyboard(dpy, CurrentTime);
1038 selectEntry(menu->cascades[entry->cascade], 0);
1039 if (!keyboardMenu(menu->cascades[entry->cascade]))
1040 done = 1;
1042 XGrabKeyboard(dpy, menu->frame->core->window, True,
1043 GrabModeAsync, GrabModeAsync,
1044 CurrentTime);
1047 break;
1049 case XK_Left:
1050 #ifdef ARROWLESS_KBD
1051 case XK_h:
1052 #endif
1053 case XK_KP_Left:
1054 if (menu->parent!=NULL && menu->parent->selected_entry>=0) {
1055 selectEntry(menu, -1);
1056 move_menus(menu, old_pos_x, old_pos_y);
1057 return True;
1059 break;
1061 case XK_Return:
1062 done = 2;
1063 break;
1065 default:
1066 index = check_key(menu, &event.xkey);
1067 if (index>=0) {
1068 selectEntry(menu, index);
1071 break;
1073 default:
1074 if (event.type==ButtonPress)
1075 done = 1;
1077 WMHandleEvent(&event);
1081 XUngrabKeyboard(dpy, CurrentTime);
1083 if (done==2 && menu->selected_entry>=0) {
1084 entry = menu->entries[menu->selected_entry];
1085 } else {
1086 entry = NULL;
1089 if (entry && entry->callback!=NULL && entry->flags.enabled
1090 && entry->cascade < 0) {
1091 #if (MENU_BLINK_COUNT > 0)
1092 int sel = menu->selected_entry;
1093 int i;
1095 for (i=0; i<MENU_BLINK_COUNT; i++) {
1096 paintEntry(menu, sel, False);
1097 XSync(dpy, 0);
1098 wusleep(MENU_BLINK_DELAY);
1099 paintEntry(menu, sel, True);
1100 XSync(dpy, 0);
1101 wusleep(MENU_BLINK_DELAY);
1103 #endif
1104 selectEntry(menu, -1);
1106 if (!menu->flags.buttoned) {
1107 wMenuUnmap(menu);
1108 move_menus(menu, old_pos_x, old_pos_y);
1110 closeCascade(menu);
1112 (*entry->callback)(menu, entry);
1113 } else {
1114 if (!menu->flags.buttoned) {
1115 wMenuUnmap(menu);
1116 move_menus(menu, old_pos_x, old_pos_y);
1118 selectEntry(menu, -1);
1122 /* returns True if returning from a submenu to a parent menu,
1123 * False if exiting from menu */
1124 return False;
1128 void
1129 wMenuMapAt(WMenu *menu, int x, int y, int keyboard)
1131 int scr_width = menu->frame->screen_ptr->scr_width;
1132 int scr_height = menu->frame->screen_ptr->scr_height;
1134 if (!menu->flags.realized) {
1135 menu->flags.realized=1;
1136 wMenuRealize(menu);
1138 if (!menu->flags.mapped) {
1139 if (wPreferences.wrap_menus) {
1140 if (x<0) x = 0;
1141 if (y<0) y = 0;
1142 if (x+MENUW(menu) > scr_width)
1143 x = scr_width - MENUW(menu);
1144 if (y+MENUH(menu) > scr_height)
1145 y = scr_height - MENUH(menu);
1148 XMoveWindow(dpy, menu->frame->core->window, x, y);
1149 menu->frame_x = x;
1150 menu->frame_y = y;
1151 XMapWindow(dpy, menu->frame->core->window);
1152 wRaiseFrame(menu->frame->core);
1153 menu->flags.mapped = 1;
1154 } else {
1155 selectEntry(menu, 0);
1158 if (keyboard)
1159 keyboardMenu(menu);
1163 void
1164 wMenuMap(WMenu *menu)
1166 if (!menu->flags.realized) {
1167 menu->flags.realized=1;
1168 wMenuRealize(menu);
1170 if (menu->flags.app_menu && menu->parent==NULL) {
1171 menu->frame_x = menu->frame->screen_ptr->app_menu_x;
1172 menu->frame_y = menu->frame->screen_ptr->app_menu_y;
1173 XMoveWindow(dpy, menu->frame->core->window, menu->frame_x, menu->frame_y);
1175 XMapWindow(dpy, menu->frame->core->window);
1176 wRaiseFrame(menu->frame->core);
1177 menu->flags.mapped = 1;
1181 void
1182 wMenuUnmap(WMenu *menu)
1184 int i;
1186 XUnmapWindow(dpy, menu->frame->core->window);
1187 if (menu->flags.titled && menu->flags.buttoned) {
1188 wFrameWindowHideButton(menu->frame, WFF_RIGHT_BUTTON);
1190 menu->flags.buttoned = 0;
1191 menu->flags.mapped = 0;
1192 menu->flags.open_to_left = 0;
1194 for (i=0; i<menu->cascade_no; i++) {
1195 if (menu->cascades[i]!=NULL
1196 && menu->cascades[i]->flags.mapped
1197 && !menu->cascades[i]->flags.buttoned) {
1199 wMenuUnmap(menu->cascades[i]);
1202 menu->selected_entry = -1;
1207 void
1208 wMenuPaint(WMenu *menu)
1210 int i;
1212 if (!menu->flags.mapped) {
1213 return;
1216 /* paint entries */
1217 for (i=0; i<menu->entry_no; i++) {
1218 paintEntry(menu, i, i==menu->selected_entry);
1223 void
1224 wMenuSetEnabled(WMenu *menu, int index, int enable)
1226 if (index>=menu->entry_no) return;
1227 menu->entries[index]->flags.enabled=enable;
1228 paintEntry(menu, index, index==menu->selected_entry);
1229 paintEntry(menu->brother, index, index==menu->selected_entry);
1233 /* ====================================================================== */
1236 static void
1237 editEntry(WMenu *menu, WMenuEntry *entry)
1239 WTextInput *text;
1240 XEvent event;
1241 WObjDescriptor *desc;
1242 char *t;
1243 int done = 0;
1244 Window old_focus;
1245 int old_revert;
1247 menu->flags.editing = 1;
1249 text = wTextCreate(menu->menu, 1, menu->entry_height * entry->order,
1250 menu->menu->width - 2, menu->entry_height - 1);
1252 wTextPutText(text, entry->text);
1253 XGetInputFocus(dpy, &old_focus, &old_revert);
1254 XSetInputFocus(dpy, text->core->window, RevertToNone, CurrentTime);
1256 if (XGrabKeyboard(dpy, text->core->window, True, GrabModeAsync,
1257 GrabModeAsync, CurrentTime)!=GrabSuccess) {
1258 wwarning("could not grab keyboard");
1259 wTextDestroy(text);
1261 wSetFocusTo(menu->frame->screen_ptr,
1262 menu->frame->screen_ptr->focused_window);
1263 return;
1267 while (!done && !text->done) {
1268 XSync(dpy, 0);
1269 XAllowEvents(dpy, AsyncKeyboard|AsyncPointer, CurrentTime);
1270 XSync(dpy, 0);
1271 WMNextEvent(dpy, &event);
1273 if (XFindContext(dpy, event.xany.window, wWinContext,
1274 (XPointer *)&desc)==XCNOENT)
1275 desc = NULL;
1277 if ((desc != NULL) && (desc->handle_anything != NULL)) {
1279 (*desc->handle_anything)(desc, &event);
1281 } else {
1282 switch (event.type) {
1283 case ButtonPress:
1284 XAllowEvents(dpy, ReplayPointer, CurrentTime);
1285 done = 1;
1287 default:
1288 WMHandleEvent(&event);
1289 break;
1294 XSetInputFocus(dpy, old_focus, old_revert, CurrentTime);
1296 wSetFocusTo(menu->frame->screen_ptr,
1297 menu->frame->screen_ptr->focused_window);
1300 t = wTextGetText(text);
1301 /* if !t, the user has canceled editing */
1302 if (t) {
1303 if (entry->text)
1304 free(entry->text);
1305 entry->text = wstrdup(t);
1307 menu->flags.realized = 0;
1309 wTextDestroy(text);
1311 XUngrabKeyboard(dpy, CurrentTime);
1313 if (t && menu->on_edit)
1314 (*menu->on_edit)(menu, entry);
1316 menu->flags.editing = 0;
1318 if (!menu->flags.realized)
1319 wMenuRealize(menu);
1323 static void
1324 selectEntry(WMenu *menu, int entry_no)
1326 WMenuEntry *entry;
1327 WMenu *submenu;
1328 int old_entry;
1330 if (menu->entries==NULL)
1331 return;
1333 if (entry_no >= menu->entry_no)
1334 return;
1336 old_entry = menu->selected_entry;
1337 menu->selected_entry = entry_no;
1339 if (old_entry!=entry_no) {
1341 /* unselect previous entry */
1342 if (old_entry>=0) {
1343 paintEntry(menu, old_entry, False);
1344 entry = menu->entries[old_entry];
1346 /* unmap cascade */
1347 if (entry->cascade>=0 && menu->cascades) {
1348 if (!menu->cascades[entry->cascade]->flags.buttoned) {
1349 wMenuUnmap(menu->cascades[entry->cascade]);
1354 if (entry_no<0) {
1355 menu->selected_entry = -1;
1356 return;
1358 entry = menu->entries[entry_no];
1360 if (entry->cascade>=0 && menu->cascades && entry->flags.enabled) {
1361 /* Callback for when the submenu is opened.
1363 submenu = menu->cascades[entry->cascade];
1364 if (submenu && submenu->flags.brother)
1365 submenu = submenu->brother;
1367 if (entry->callback) {
1368 /* Only call the callback if the submenu is not yet mapped.
1370 if (menu->flags.brother) {
1371 if (!submenu || !submenu->flags.mapped)
1372 (*entry->callback)(menu->brother, entry);
1373 } else {
1374 if (!submenu || !submenu->flags.buttoned)
1375 (*entry->callback)(menu, entry);
1379 /* the submenu menu might have changed */
1380 submenu = menu->cascades[entry->cascade];
1382 /* map cascade */
1383 if (!submenu->flags.mapped) {
1384 int x, y;
1386 if (!submenu->flags.realized)
1387 wMenuRealize(submenu);
1388 if (wPreferences.wrap_menus) {
1389 if (menu->flags.open_to_left)
1390 submenu->flags.open_to_left = 1;
1392 if (submenu->flags.open_to_left) {
1393 x = menu->frame_x - MENUW(submenu);
1394 if (x<0) {
1395 x = 0;
1396 submenu->flags.open_to_left = 0;
1398 } else {
1399 x = menu->frame_x + MENUW(menu);
1401 if (x + MENUW(submenu)
1402 >= menu->frame->screen_ptr->scr_width) {
1404 x = menu->frame_x - MENUW(submenu);
1405 submenu->flags.open_to_left = 1;
1408 } else {
1409 x = menu->frame_x + MENUW(menu);
1412 if (wPreferences.align_menus) {
1413 y = menu->frame_y;
1414 } else {
1415 y = menu->frame_y + menu->entry_height*entry_no;
1416 if (menu->flags.titled)
1417 y += menu->frame->top_width;
1418 if (menu->cascades[entry->cascade]->flags.titled)
1419 y -= menu->cascades[entry->cascade]->frame->top_width;
1422 wMenuMapAt(menu->cascades[entry->cascade], x, y, False);
1423 menu->cascades[entry->cascade]->parent = menu;
1424 } else {
1425 return;
1428 paintEntry(menu, entry_no, True);
1433 static WMenu*
1434 findMenu(WScreen *scr, int *x_ret, int *y_ret)
1436 WMenu *menu;
1437 WObjDescriptor *desc;
1438 Window root_ret, win, junk_win;
1439 int x, y, wx, wy;
1440 unsigned int mask;
1442 XQueryPointer(dpy, scr->root_win, &root_ret, &win, &x, &y, &wx, &wy,
1443 &mask);
1445 if (win==None) return NULL;
1447 if (XFindContext(dpy, win, wWinContext, (XPointer *)&desc)==XCNOENT)
1448 return NULL;
1450 if (desc->parent_type == WCLASS_MENU) {
1451 menu = (WMenu*)desc->parent;
1452 XTranslateCoordinates(dpy, root_ret, menu->menu->window, wx, wy,
1453 x_ret, y_ret, &junk_win);
1454 return menu;
1456 return NULL;
1462 static void
1463 closeCascade(WMenu *menu)
1465 WMenu *parent=menu->parent;
1467 if (menu->flags.brother
1468 || (!menu->flags.buttoned
1469 && (!menu->flags.app_menu||menu->parent!=NULL))) {
1471 selectEntry(menu, -1);
1472 XSync(dpy, 0);
1473 #if (MENU_BLINK_DELAY > 2)
1474 wusleep(MENU_BLINK_DELAY/2);
1475 #endif
1476 wMenuUnmap(menu);
1477 while (parent!=NULL
1478 && (parent->parent!=NULL || !parent->flags.app_menu
1479 || parent->flags.brother)
1480 && !parent->flags.buttoned) {
1481 selectEntry(parent, -1);
1482 wMenuUnmap(parent);
1483 parent = parent->parent;
1485 if (parent)
1486 selectEntry(parent, -1);
1491 static void
1492 closeBrotherCascadesOf(WMenu *menu)
1494 WMenu *tmp;
1495 int i;
1497 for (i=0; i<menu->cascade_no; i++) {
1498 if (menu->cascades[i]->flags.brother) {
1499 tmp = menu->cascades[i];
1500 } else {
1501 tmp = menu->cascades[i]->brother;
1503 if (tmp->flags.mapped) {
1504 selectEntry(tmp->parent, -1);
1505 closeBrotherCascadesOf(tmp);
1506 break;
1512 #define getEntryAt(menu, x, y) ((y)<0 ? -1 : (y)/(menu->entry_height))
1515 static WMenu*
1516 parentMenu(WMenu *menu)
1518 WMenu *parent;
1519 WMenuEntry *entry;
1521 if (menu->flags.buttoned)
1522 return menu;
1524 while (menu->parent && menu->parent->flags.mapped) {
1525 parent = menu->parent;
1526 if (parent->selected_entry < 0)
1527 break;
1528 entry = parent->entries[parent->selected_entry];
1529 if (!entry->flags.enabled || entry->cascade<0 || !parent->cascades ||
1530 parent->cascades[entry->cascade] != menu)
1531 break;
1532 menu = parent;
1533 if (menu->flags.buttoned)
1534 break;
1537 return menu;
1543 * Will raise the passed menu, if submenu = 0
1544 * If submenu > 0 will also raise all mapped submenus
1545 * until the first buttoned one
1546 * If submenu < 0 will also raise all mapped parent menus
1547 * until the first buttoned one
1550 static void
1551 raiseMenus(WMenu *menu, int submenus)
1553 WMenu *submenu;
1554 int i;
1556 if(!menu) return;
1558 wRaiseFrame(menu->frame->core);
1560 if (submenus>0 && menu->selected_entry>=0) {
1561 i = menu->entries[menu->selected_entry]->cascade;
1562 if (i>=0 && menu->cascades) {
1563 submenu = menu->cascades[i];
1564 if (submenu->flags.mapped && !submenu->flags.buttoned)
1565 raiseMenus(submenu, submenus);
1568 if (submenus<0 && !menu->flags.buttoned &&
1569 menu->parent && menu->parent->flags.mapped)
1570 raiseMenus(menu->parent, submenus);
1574 WMenu*
1575 wMenuUnderPointer(WScreen *screen)
1577 WObjDescriptor *desc;
1578 Window root_ret, win;
1579 int dummy;
1580 unsigned int mask;
1582 XQueryPointer(dpy, screen->root_win, &root_ret, &win, &dummy, &dummy,
1583 &dummy, &dummy, &mask);
1585 if (win==None) return NULL;
1587 if (XFindContext(dpy, win, wWinContext, (XPointer *)&desc)==XCNOENT)
1588 return NULL;
1590 if (desc->parent_type == WCLASS_MENU)
1591 return (WMenu *)desc->parent;
1592 return NULL;
1597 #define MIN(a,b) (((a) > (b)) ? (b) : (a))
1600 static void
1601 getPointerPosition(WScreen *scr, int *x, int *y)
1603 Window root_ret, win;
1604 int wx, wy;
1605 unsigned int mask;
1607 XQueryPointer(dpy, scr->root_win, &root_ret, &win, x, y, &wx, &wy, &mask);
1611 static void
1612 getScrollAmount(WMenu *menu, int *hamount, int *vamount)
1614 WScreen *scr = menu->menu->screen_ptr;
1615 int menuX1 = menu->frame_x;
1616 int menuY1 = menu->frame_y;
1617 int menuX2 = menu->frame_x + MENUW(menu);
1618 int menuY2 = menu->frame_y + MENUH(menu);
1619 int screenW = scr->scr_width;
1620 int screenH = scr->scr_height;
1621 int xroot, yroot;
1623 *hamount = 0;
1624 *vamount = 0;
1626 getPointerPosition(scr, &xroot, &yroot);
1629 if (xroot <= 1 && menuX1 < 0) {
1630 /* scroll to the right */
1631 *hamount = MIN(MENU_SCROLL_STEP, abs(menuX1));
1633 } else if (xroot >= screenW-2 && menuX2 > screenW-1) {
1634 /* scroll to the left */
1635 *hamount = MIN(MENU_SCROLL_STEP, abs(menuX2-screenW-1));
1637 if (*hamount==0)
1638 *hamount = 1;
1640 *hamount = -*hamount;
1643 if (yroot <= 1 && menuY1 < 0) {
1644 /* scroll down */
1645 *vamount = MIN(MENU_SCROLL_STEP, abs(menuY1));
1647 } else if (yroot >= screenH-2 && menuY2 > screenH-1) {
1648 /* scroll up */
1649 *vamount = MIN(MENU_SCROLL_STEP, abs(menuY2-screenH-2));
1651 *vamount = -*vamount;
1656 static void
1657 dragScrollMenuCallback(void *data)
1659 WMenu *menu = (WMenu*)data;
1660 WScreen *scr = menu->menu->screen_ptr;
1661 WMenu *parent = parentMenu(menu);
1662 int hamount, vamount;
1663 int x, y;
1664 int newSelectedEntry;
1666 getScrollAmount(menu, &hamount, &vamount);
1669 if (hamount != 0 || vamount != 0) {
1670 wMenuMove(parent, parent->frame_x + hamount,
1671 parent->frame_y + vamount, True);
1672 if (findMenu(scr, &x, &y)) {
1673 newSelectedEntry = getEntryAt(menu, x, y);
1674 selectEntry(menu, newSelectedEntry);
1675 } else {
1676 /* Pointer fell outside of menu. If the selected entry is
1677 * not a submenu, unselect it */
1678 if (menu->selected_entry >= 0
1679 && menu->entries[menu->selected_entry]->cascade<0)
1680 selectEntry(menu, -1);
1681 newSelectedEntry = 0;
1684 /* paranoid check */
1685 if (newSelectedEntry >= 0) {
1686 /* keep scrolling */
1687 menu->timer = WMAddTimerHandler(MENU_SCROLL_DELAY,
1688 dragScrollMenuCallback, menu);
1689 } else {
1690 menu->timer = NULL;
1692 } else {
1693 /* don't need to scroll anymore */
1694 menu->timer = NULL;
1695 if (findMenu(scr, &x, &y)) {
1696 newSelectedEntry = getEntryAt(menu, x, y);
1697 selectEntry(menu, newSelectedEntry);
1703 static void
1704 scrollMenuCallback(void *data)
1706 WMenu *menu = (WMenu*)data;
1707 WMenu *parent = parentMenu(menu);
1708 int hamount = 0; /* amount to scroll */
1709 int vamount = 0;
1711 getScrollAmount(menu, &hamount, &vamount);
1713 if (hamount != 0 || vamount != 0) {
1714 wMenuMove(parent, parent->frame_x + hamount,
1715 parent->frame_y + vamount, True);
1717 /* keep scrolling */
1718 menu->timer = WMAddTimerHandler(MENU_SCROLL_DELAY,
1719 scrollMenuCallback, menu);
1720 } else {
1721 /* don't need to scroll anymore */
1722 menu->timer = NULL;
1728 #define MENU_SCROLL_BORDER 5
1730 static int
1731 isPointNearBoder(WMenu *menu, int x, int y)
1733 int menuX1 = menu->frame_x;
1734 int menuY1 = menu->frame_y;
1735 int menuX2 = menu->frame_x + MENUW(menu);
1736 int menuY2 = menu->frame_y + MENUH(menu);
1737 int scrXe = menu->menu->screen_ptr->scr_width-1;
1738 int scrYe = menu->menu->screen_ptr->scr_height-1;
1739 int flag = 0;
1741 if (x >= menuX1 && x <= menuX2 && (y < MENU_SCROLL_BORDER
1742 || y > scrYe-MENU_SCROLL_BORDER))
1743 flag = 1;
1744 else if (y >= menuY1 && y <= menuY2 && (x < MENU_SCROLL_BORDER
1745 || x > scrXe-MENU_SCROLL_BORDER))
1746 flag = 1;
1748 return flag;
1752 typedef struct _delay {
1753 WWindow *wwin;
1754 WMenu *menu;
1755 int ox,oy;
1756 } _delay;
1759 static void
1760 _leaving(_delay *dl)
1762 wMenuMove(dl->menu, dl->ox, dl->oy, True);
1763 dl->menu->jump_back=NULL;
1764 dl->menu->menu->screen_ptr->flags.jump_back_pending = 0;
1765 free(dl);
1769 void
1770 wMenuScroll(WMenu *menu, XEvent *event)
1772 WMenu *smenu;
1773 WMenu *omenu = parentMenu(menu);
1774 WScreen *scr = menu->frame->screen_ptr;
1775 int done = 0;
1776 int jump_back = 0;
1777 int old_frame_x = omenu->frame_x;
1778 int old_frame_y = omenu->frame_y;
1779 XEvent ev;
1781 #ifdef DEBUG
1782 dputs("Entering menu Scroll");
1783 #endif
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:
1809 this um causes a nasty crash ugly ugh i dont see *why* we do this,
1810 everything seems fine without it. ( swivel )
1811 WMHandleEvent(&ev);
1813 break;
1814 case MotionNotify:
1815 x = (ev.type==MotionNotify) ? ev.xmotion.x_root : ev.xcrossing.x_root;
1816 y = (ev.type==MotionNotify) ? ev.xmotion.y_root : ev.xcrossing.y_root;
1818 /* on_border is != 0 if the pointer is between the menu
1819 * and the screen border and is close enough to the border */
1820 on_border = isPointNearBoder(menu, x, y);
1822 smenu = wMenuUnderPointer(scr);
1824 if ((smenu==NULL && !on_border) || (smenu && parentMenu(smenu)!=omenu)) {
1825 done = 1;
1826 break;
1829 on_x_edge = x <= 1 || x >= scr->scr_width - 2;
1830 on_y_edge = y <= 1 || y >= scr->scr_height - 2;
1831 on_border = on_x_edge || on_y_edge;
1833 if (!on_border && !jump_back) {
1834 done = 1;
1835 break;
1838 if (menu->timer && (smenu!=menu || (!on_y_edge && !on_x_edge))) {
1839 WMDeleteTimerHandler(menu->timer);
1840 menu->timer = NULL;
1843 if (smenu != NULL)
1844 menu = smenu;
1846 if (!menu->timer)
1847 scrollMenuCallback(menu);
1848 break;
1849 case ButtonPress:
1850 /* True if we push on title, or drag the omenu to other position */
1851 on_title = ev.xbutton.x_root >= omenu->frame_x &&
1852 ev.xbutton.x_root <= omenu->frame_x + MENUW(omenu) &&
1853 ev.xbutton.y_root >= omenu->frame_y &&
1854 ev.xbutton.y_root <= omenu->frame_y + omenu->frame->top_width;
1855 WMHandleEvent(&ev);
1856 smenu = wMenuUnderPointer(scr);
1857 if (smenu == NULL || (smenu && smenu->flags.buttoned && smenu != omenu))
1858 done = 1;
1859 else if (smenu==omenu && on_title) {
1860 jump_back = 0;
1861 done = 1;
1863 break;
1864 case KeyPress:
1865 done = 1;
1866 default:
1867 WMHandleEvent(&ev);
1868 break;
1872 if (menu->timer) {
1873 WMDeleteTimerHandler(menu->timer);
1874 menu->timer = NULL;
1877 if (jump_back) {
1878 _delay *delayer;
1879 if (!omenu->jump_back) {
1880 delayer=wmalloc(sizeof(_delay));
1881 delayer->menu=omenu;
1882 delayer->ox=old_frame_x;
1883 delayer->oy=old_frame_y;
1884 omenu->jump_back=delayer;
1885 scr->flags.jump_back_pending = 1;
1887 else delayer = omenu->jump_back;
1888 WMAddTimerHandler(MENU_JUMP_BACK_DELAY,(WMCallback*)_leaving, delayer);
1892 #ifdef DEBUG
1893 dputs("Leaving menu Scroll");
1894 #endif
1899 static void
1900 menuExpose(WObjDescriptor *desc, XEvent *event)
1902 wMenuPaint(desc->parent);
1905 typedef struct {
1906 int *delayed_select;
1907 WMenu *menu;
1908 WMHandlerID magic;
1909 } delay_data;
1912 static void
1913 delaySelection(void *data)
1915 delay_data *d = (delay_data*)data;
1916 int x, y, entry_no;
1917 WMenu *menu;
1919 d->magic = NULL;
1921 menu = findMenu(d->menu->menu->screen_ptr, &x, &y);
1922 if (menu && (d->menu == menu || d->delayed_select)) {
1923 entry_no = getEntryAt(menu, x, y);
1924 selectEntry(menu, entry_no);
1926 if (d->delayed_select)
1927 *(d->delayed_select) = 0;
1931 static void
1932 menuMouseDown(WObjDescriptor *desc, XEvent *event)
1934 XButtonEvent *bev = &event->xbutton;
1935 WMenu *menu = desc->parent;
1936 WMenu *smenu;
1937 WScreen *scr=menu->frame->screen_ptr;
1938 WMenuEntry *entry=NULL;
1939 XEvent ev;
1940 int close_on_exit=0;
1941 int done=0;
1942 int delayed_select = 0;
1943 int entry_no;
1944 int x, y;
1945 int prevx, prevy;
1946 int old_frame_x = 0;
1947 int old_frame_y = 0;
1948 delay_data d_data = {NULL, NULL, NULL};
1950 if (!wPreferences.wrap_menus) {
1951 smenu = parentMenu(menu);
1952 old_frame_x = smenu->frame_x;
1953 old_frame_y = smenu->frame_y;
1954 } else if (event->xbutton.window == menu->frame->core->window) {
1955 /* This is true if the menu was launched with right click on root window */
1956 delayed_select = 1;
1957 d_data.delayed_select = &delayed_select;
1958 d_data.menu = menu;
1959 d_data.magic = WMAddTimerHandler(wPreferences.dblclick_time,
1960 delaySelection, &d_data);
1963 wRaiseFrame(menu->frame->core);
1965 close_on_exit = (bev->send_event || menu->flags.brother);
1967 smenu = findMenu(scr, &x, &y);
1968 if (!smenu) {
1969 x = -1;
1970 y = -1;
1971 } else {
1972 menu = smenu;
1975 if (menu->flags.editing) {
1976 return;
1978 entry_no = getEntryAt(menu, x, y);
1979 if (entry_no>=0) {
1980 entry = menu->entries[entry_no];
1982 if (!close_on_exit && (bev->state & ControlMask) && smenu
1983 && entry->flags.editable) {
1984 editEntry(smenu, entry);
1985 return;
1986 } else if (bev->state & ControlMask) {
1987 return;
1990 if (entry->flags.enabled && entry->cascade>=0 && menu->cascades) {
1991 WMenu *submenu = menu->cascades[entry->cascade];
1992 /* map cascade */
1993 if (submenu->flags.mapped && !submenu->flags.buttoned &&
1994 menu->selected_entry!=entry_no) {
1995 wMenuUnmap(submenu);
1997 if (!submenu->flags.mapped && !delayed_select) {
1998 selectEntry(menu, entry_no);
1999 } else if (!submenu->flags.buttoned) {
2000 selectEntry(menu, -1);
2003 } else if (!delayed_select) {
2004 selectEntry(menu, entry_no);
2007 if (!wPreferences.wrap_menus && !wPreferences.scrollable_menus) {
2008 if (!menu->timer)
2009 dragScrollMenuCallback(menu);
2013 prevx = bev->x_root;
2014 prevy = bev->y_root;
2015 while (!done) {
2016 int x, y;
2018 XAllowEvents(dpy, SyncPointer, CurrentTime);
2020 WMMaskEvent(dpy, ExposureMask|ButtonMotionMask|ButtonReleaseMask
2021 |ButtonPressMask, &ev);
2022 switch (ev.type) {
2023 case MotionNotify:
2024 smenu = findMenu(scr, &x, &y);
2026 if (smenu == NULL) {
2027 /* moved mouse out of menu */
2029 if (!delayed_select && d_data.magic) {
2030 WMDeleteTimerHandler(d_data.magic);
2031 d_data.magic = NULL;
2033 if (menu==NULL
2034 || (menu->selected_entry>=0
2035 && menu->entries[menu->selected_entry]->cascade>=0)) {
2036 prevx = ev.xmotion.x_root;
2037 prevy = ev.xmotion.y_root;
2039 break;
2041 selectEntry(menu, -1);
2042 menu = smenu;
2043 prevx = ev.xmotion.x_root;
2044 prevy = ev.xmotion.y_root;
2045 break;
2046 } else if (menu && menu!=smenu
2047 && (menu->selected_entry<0
2048 || menu->entries[menu->selected_entry]->cascade<0)) {
2049 selectEntry(menu, -1);
2051 if (!delayed_select && d_data.magic) {
2052 WMDeleteTimerHandler(d_data.magic);
2053 d_data.magic = NULL;
2055 } else {
2057 /* hysteresis for item selection */
2059 /* check if the motion was to the side, indicating that
2060 * the user may want to cross to a submenu */
2061 if (!delayed_select && menu) {
2062 int dx;
2063 Bool moved_to_submenu;/* moved to direction of submenu */
2065 dx = abs(prevx - ev.xmotion.x_root);
2067 moved_to_submenu = False;
2068 if (dx > 0 /* if moved enough to the side */
2069 /* maybe a open submenu */
2070 && menu->selected_entry>=0
2071 /* moving to the right direction */
2072 && (wPreferences.align_menus
2073 || ev.xmotion.y_root >= prevy)) {
2074 int index;
2076 index = menu->entries[menu->selected_entry]->cascade;
2077 if (index>=0) {
2078 if (menu->cascades[index]->frame_x>menu->frame_x) {
2079 if (prevx < ev.xmotion.x_root)
2080 moved_to_submenu = True;
2081 } else {
2082 if (prevx > ev.xmotion.x_root)
2083 moved_to_submenu = True;
2089 if (menu != smenu) {
2090 if (d_data.magic) {
2091 WMDeleteTimerHandler(d_data.magic);
2093 d_data.magic = NULL;
2094 } else if (moved_to_submenu) {
2095 /* while we are moving, postpone the selection */
2096 if (d_data.magic) {
2097 WMDeleteTimerHandler(d_data.magic);
2099 d_data.delayed_select = NULL;
2100 d_data.menu = menu;
2101 d_data.magic = WMAddTimerHandler(MENU_SELECT_DELAY,
2102 delaySelection,
2103 &d_data);
2104 prevx = ev.xmotion.x_root;
2105 prevy = ev.xmotion.y_root;
2106 break;
2107 } else {
2108 if (d_data.magic)
2109 WMDeleteTimerHandler(d_data.magic);
2110 d_data.magic = NULL;
2114 prevx = ev.xmotion.x_root;
2115 prevy = ev.xmotion.y_root;
2116 if (menu!=smenu) {
2117 /* pointer crossed menus */
2118 if (menu && menu->timer) {
2119 WMDeleteTimerHandler(menu->timer);
2120 menu->timer = NULL;
2122 if (smenu)
2123 dragScrollMenuCallback(smenu);
2125 menu = smenu;
2126 if (!menu->timer)
2127 dragScrollMenuCallback(menu);
2129 if (!delayed_select) {
2130 entry_no = getEntryAt(menu, x, y);
2131 if (entry_no>=0) {
2132 entry = menu->entries[entry_no];
2133 if (entry->flags.enabled && entry->cascade>=0 &&
2134 menu->cascades) {
2135 WMenu *submenu = menu->cascades[entry->cascade];
2136 if (submenu->flags.mapped && !submenu->flags.buttoned
2137 && menu->selected_entry!=entry_no) {
2138 wMenuUnmap(submenu);
2142 selectEntry(menu, entry_no);
2144 break;
2146 case ButtonPress:
2147 break;
2149 case ButtonRelease:
2150 if (ev.xbutton.button == event->xbutton.button)
2151 done=1;
2152 break;
2154 case Expose:
2155 WMHandleEvent(&ev);
2156 break;
2160 if (menu && menu->timer) {
2161 WMDeleteTimerHandler(menu->timer);
2162 menu->timer = NULL;
2164 if (d_data.magic!=NULL)
2165 WMDeleteTimerHandler(d_data.magic);
2167 if (menu && menu->selected_entry>=0) {
2168 entry = menu->entries[menu->selected_entry];
2169 if (entry->callback!=NULL && entry->flags.enabled
2170 && entry->cascade < 0) {
2171 /* blink and erase menu selection */
2172 #if (MENU_BLINK_DELAY > 0)
2173 int sel = menu->selected_entry;
2174 int i;
2176 for (i=0; i<MENU_BLINK_COUNT; i++) {
2177 paintEntry(menu, sel, False);
2178 XSync(dpy, 0);
2179 wusleep(MENU_BLINK_DELAY);
2180 paintEntry(menu, sel, True);
2181 XSync(dpy, 0);
2182 wusleep(MENU_BLINK_DELAY);
2184 #endif
2185 /* unmap the menu, it's parents and call the callback */
2186 if (!menu->flags.buttoned &&
2187 (!menu->flags.app_menu||menu->parent!=NULL)) {
2188 closeCascade(menu);
2189 } else {
2190 selectEntry(menu, -1);
2192 (*entry->callback)(menu, entry);
2194 /* If the user double clicks an entry, the entry will
2195 * be executed twice, which is not good for things like
2196 * the root menu. So, ignore any clicks that were generated
2197 * while the entry was being executed */
2198 while (XCheckTypedWindowEvent(dpy, menu->menu->window,
2199 ButtonPress, &ev));
2200 } else if (entry->callback!=NULL && entry->cascade<0) {
2201 selectEntry(menu, -1);
2202 } else {
2203 if (entry->cascade>=0 && menu->cascades
2204 && menu->cascades[entry->cascade]->flags.brother) {
2205 selectEntry(menu, -1);
2210 if (((WMenu*)desc->parent)->flags.brother || close_on_exit || !smenu)
2211 closeCascade(desc->parent);
2213 /* close the cascade windows that should not remain opened */
2214 closeBrotherCascadesOf(desc->parent);
2216 if (!wPreferences.wrap_menus)
2217 wMenuMove(parentMenu(desc->parent), old_frame_x, old_frame_y, True);
2221 void
2222 wMenuMove(WMenu *menu, int x, int y, int submenus)
2224 WMenu *submenu;
2225 int i;
2227 if (!menu) return;
2229 menu->frame_x = x;
2230 menu->frame_y = y;
2231 XMoveWindow(dpy, menu->frame->core->window, x, y);
2233 if (submenus>0 && menu->selected_entry>=0) {
2234 i = menu->entries[menu->selected_entry]->cascade;
2236 if (i>=0 && menu->cascades) {
2237 submenu = menu->cascades[i];
2238 if (submenu->flags.mapped && !submenu->flags.buttoned) {
2239 if (wPreferences.align_menus) {
2240 wMenuMove(submenu, x + MENUW(menu), y, submenus);
2241 } else {
2242 wMenuMove(submenu, x+ MENUW(menu),
2243 y + submenu->entry_height*menu->selected_entry,
2244 submenus);
2249 if (submenus<0 && menu->parent!=NULL && menu->parent->flags.mapped &&
2250 !menu->parent->flags.buttoned) {
2251 if (wPreferences.align_menus) {
2252 wMenuMove(menu->parent, x - MENUW(menu->parent), y, submenus);
2253 } else {
2254 wMenuMove(menu->parent, x - MENUW(menu->parent), menu->frame_y
2255 - menu->parent->entry_height*menu->parent->selected_entry,
2256 submenus);
2262 static void
2263 changeMenuLevels(WMenu *menu, int lower)
2265 int i;
2267 if (!lower) {
2268 ChangeStackingLevel(menu->frame->core, (!menu->parent ? WMMainMenuLevel
2269 : WMSubmenuLevel));
2270 wRaiseFrame(menu->frame->core);
2271 menu->flags.lowered = 0;
2272 } else {
2273 ChangeStackingLevel(menu->frame->core, WMNormalLevel);
2274 wLowerFrame(menu->frame->core);
2275 menu->flags.lowered = 1;
2277 for (i=0; i<menu->cascade_no; i++) {
2278 if (menu->cascades[i]
2279 && !menu->cascades[i]->flags.buttoned
2280 && menu->cascades[i]->flags.lowered!=lower) {
2281 changeMenuLevels(menu->cascades[i], lower);
2288 static void
2289 menuTitleDoubleClick(WCoreWindow *sender, void *data, XEvent *event)
2291 WMenu *menu = data;
2292 int lower;
2294 if (event->xbutton.state & MOD_MASK) {
2295 if (menu->flags.lowered) {
2296 lower = 0;
2297 } else {
2298 lower = 1;
2300 changeMenuLevels(menu, lower);
2305 static void
2306 menuTitleMouseDown(WCoreWindow *sender, void *data, XEvent *event)
2308 WMenu *menu = data;
2309 WMenu *tmp;
2310 XEvent ev;
2311 int x=menu->frame_x, y=menu->frame_y;
2312 int dx=event->xbutton.x_root, dy=event->xbutton.y_root;
2313 int i, lower;
2314 Bool started;
2316 #ifdef DEBUG
2317 dprintf("Moving menu\n");
2318 #endif
2320 /* can't touch the menu copy */
2321 if (menu->flags.brother)
2322 return;
2324 if (event->xbutton.button != Button1 && event->xbutton.button != Button2)
2325 return;
2327 if (event->xbutton.state & MOD_MASK) {
2328 wLowerFrame(menu->frame->core);
2329 lower = 1;
2330 } else {
2331 wRaiseFrame(menu->frame->core);
2332 lower = 0;
2334 tmp = menu;
2336 /* lower/raise all submenus */
2337 while (1) {
2338 if (tmp->selected_entry>=0 && tmp->cascades
2339 && tmp->entries[tmp->selected_entry]->cascade>=0) {
2340 tmp = tmp->cascades[tmp->entries[tmp->selected_entry]->cascade];
2341 if (!tmp || !tmp->flags.mapped)
2342 break;
2343 if (lower)
2344 wLowerFrame(tmp->frame->core);
2345 else
2346 wRaiseFrame(tmp->frame->core);
2347 } else {
2348 break;
2352 /* tear off the menu if it's a root menu or a cascade
2353 application menu */
2354 if (!menu->flags.buttoned && !menu->flags.brother
2355 && (!menu->flags.app_menu||menu->parent!=NULL)) {
2356 menu->flags.buttoned=1;
2357 wFrameWindowShowButton(menu->frame, WFF_RIGHT_BUTTON);
2358 if (menu->parent) {
2359 /* turn off selected menu entry in parent menu */
2360 selectEntry(menu->parent, -1);
2362 /* make parent map the copy in place of the original */
2363 for (i=0; i<menu->parent->cascade_no; i++) {
2364 if (menu->parent->cascades[i] == menu) {
2365 menu->parent->cascades[i] = menu->brother;
2366 break;
2372 started = False;
2373 while(1) {
2374 WMMaskEvent(dpy, ButtonMotionMask|ButtonReleaseMask|ButtonPressMask
2375 |ExposureMask, &ev);
2376 switch (ev.type) {
2377 case MotionNotify:
2378 if (started) {
2379 x += ev.xmotion.x_root - dx;
2380 y += ev.xmotion.y_root - dy;
2381 dx = ev.xmotion.x_root;
2382 dy = ev.xmotion.y_root;
2383 wMenuMove(menu, x, y, True);
2384 } else {
2385 if (abs(ev.xmotion.x_root - dx) > MOVE_THRESHOLD
2386 || abs(ev.xmotion.y_root - dy) > MOVE_THRESHOLD) {
2387 started = True;
2388 XGrabPointer(dpy, menu->frame->titlebar->window, False,
2389 ButtonMotionMask|ButtonReleaseMask
2390 |ButtonPressMask,
2391 GrabModeAsync, GrabModeAsync, None,
2392 wCursor[WCUR_MOVE], CurrentTime);
2395 break;
2397 case ButtonPress:
2398 break;
2400 case ButtonRelease:
2401 if (ev.xbutton.button != event->xbutton.button)
2402 break;
2403 #ifdef DEBUG
2404 dprintf("End menu move\n");
2405 #endif
2406 XUngrabPointer(dpy, CurrentTime);
2407 return;
2409 default:
2410 WMHandleEvent(&ev);
2411 break;
2417 *----------------------------------------------------------------------
2418 * menuCloseClick--
2419 * Handles mouse click on the close button of menus. The menu is
2420 * closed when the button is clicked.
2422 * Side effects:
2423 * The closed menu is reinserted at it's parent menus
2424 * cascade list.
2425 *----------------------------------------------------------------------
2427 static void
2428 menuCloseClick(WCoreWindow *sender, void *data, XEvent *event)
2430 WMenu *menu = (WMenu*)data;
2431 WMenu *parent = menu->parent;
2432 int i;
2434 if (parent) {
2435 for (i=0; i<parent->cascade_no; i++) {
2436 /* find the entry that points to the copy */
2437 if (parent->cascades[i] == menu->brother) {
2438 /* make it point to the original */
2439 parent->cascades[i] = menu;
2440 menu->parent = parent;
2441 break;
2445 wMenuUnmap(menu);
2449 void
2450 wMenuSaveState(WScreen *scr)
2452 proplist_t menus, key, value;
2453 int save_menus = 0;
2455 char buffer[256];
2457 menus = PLMakeDictionaryFromEntries(NULL, NULL, NULL);
2459 #ifndef LITE
2460 if (scr->switch_menu && scr->switch_menu->flags.buttoned) {
2461 sprintf(buffer, "%i,%i", scr->switch_menu->frame_x,
2462 scr->switch_menu->frame_y);
2463 key = PLMakeString("SwitchMenu");
2464 value = PLMakeString(buffer);
2465 PLInsertDictionaryEntry(menus, key, value);
2466 PLRelease(key);
2467 PLRelease(value);
2468 save_menus = 1;
2471 if (saveMenuRecurs(menus, scr, scr->root_menu))
2472 save_menus = 1;
2474 #endif /* !LITE */
2475 if (scr->workspace_menu && scr->workspace_menu->flags.buttoned) {
2476 sprintf(buffer, "%i,%i", scr->workspace_menu->frame_x,
2477 scr->workspace_menu->frame_y);
2478 key = PLMakeString("WorkspaceMenu");
2479 value = PLMakeString(buffer);
2480 PLInsertDictionaryEntry(menus, key, value);
2481 PLRelease(key);
2482 PLRelease(value);
2483 save_menus = 1;
2486 if (save_menus) {
2487 key = PLMakeString("Menus");
2488 PLInsertDictionaryEntry(scr->session_state, key, menus);
2489 PLRelease(key);
2491 PLRelease(menus);
2495 #ifndef LITE
2497 static Bool
2498 getMenuPath(WMenu *menu, char *buffer, int bufSize)
2500 Bool ok = True;
2501 int len = 0;
2503 if (!menu->flags.titled || !menu->frame->title[0])
2504 return False;
2506 len = strlen(menu->frame->title);
2507 if (len >= bufSize)
2508 return False;
2510 if (menu->parent) {
2511 ok = getMenuPath(menu->parent, buffer, bufSize - len - 1);
2512 if (!ok)
2513 return False;
2516 strcat(buffer, "\\");
2517 strcat(buffer, menu->frame->title);
2519 return True;
2522 static Bool
2523 saveMenuRecurs(proplist_t menus, WScreen *scr, WMenu *menu)
2525 proplist_t key, value;
2526 int save_menus = 0, i;
2527 char buffer[512];
2528 Bool ok = True;
2531 if (menu->flags.brother)
2532 menu = menu->brother;
2534 if (menu->flags.buttoned && menu != scr->switch_menu) {
2536 sprintf(buffer, "%i,%i", menu->frame_x, menu->frame_y);
2537 value = PLMakeString(buffer);
2539 buffer[0] = '\0';
2540 ok = getMenuPath(menu, buffer, 510);
2542 if (ok) {
2543 key = PLMakeString(buffer);
2544 PLInsertDictionaryEntry(menus, key, value);
2545 PLRelease(key);
2546 PLRelease(value);
2547 save_menus = 1;
2551 if (ok) {
2552 for (i = 0; i < menu->cascade_no; i++) {
2553 if (saveMenuRecurs(menus, scr, menu->cascades[i]))
2554 save_menus = 1;
2557 return save_menus;
2559 #endif /* !LITE */
2562 #define COMPLAIN(key) wwarning(_("bad value in menus state info:%s"), key)
2565 static int
2566 restoreMenu(WScreen *scr, proplist_t menu, int which)
2568 int x, y;
2569 WMenu *pmenu = NULL;
2571 if (!menu)
2572 return False;
2574 if (!PLIsString(menu)) {
2575 COMPLAIN("Position");
2576 return False;
2579 if (sscanf(PLGetString(menu), "%i,%i", &x, &y)!=2)
2580 COMPLAIN("Position");
2582 #ifndef LITE
2583 if (which & WSS_SWITCHMENU) {
2584 OpenSwitchMenu(scr, x, y, False);
2585 pmenu = scr->switch_menu;
2587 #endif /* !LITE */
2589 if (pmenu) {
2590 int width = MENUW(pmenu);
2591 int height = MENUH(pmenu);
2593 x = (x < -width) ? 0 : x;
2594 x = (x > scr->scr_width) ? scr->scr_width - width : x;
2595 y = (y < 0) ? 0 : y;
2596 y = (y > scr->scr_height) ? scr->scr_height - height : y;
2597 wMenuMove(pmenu, x, y, True);
2598 pmenu->flags.buttoned = 1;
2599 wFrameWindowShowButton(pmenu->frame, WFF_RIGHT_BUTTON);
2600 return True;
2602 return False;
2606 #ifndef LITE
2607 static int
2608 restoreMenuRecurs(WScreen *scr, proplist_t menus, WMenu *menu, char *path)
2610 proplist_t key, entry;
2611 char buffer[512];
2612 int i, x, y, res;
2614 if (strlen(path) + strlen(menu->frame->title) > 510)
2615 return False;
2617 sprintf(buffer, "%s\\%s", path, menu->frame->title);
2618 key = PLMakeString(buffer);
2619 entry = PLGetDictionaryEntry(menus, key);
2620 res = False;
2622 if (entry && PLIsString(entry)) {
2624 if (sscanf(PLGetString(entry), "%i,%i", &x, &y) != 2)
2625 COMPLAIN("Position");
2627 if (!menu->flags.mapped) {
2628 int width = MENUW(menu);
2629 int height = MENUH(menu);
2631 wMenuMapAt(menu, x, y, False);
2633 if (menu->parent) {
2634 /* make parent map the copy in place of the original */
2635 for (i=0; i<menu->parent->cascade_no; i++) {
2636 if (menu->parent->cascades[i] == menu) {
2637 menu->parent->cascades[i] = menu->brother;
2638 break;
2642 x = (x < -width) ? 0 : x;
2643 x = (x > scr->scr_width) ? scr->scr_width - width : x;
2644 y = (y < 0) ? 0 : y;
2645 y = (y > scr->scr_height) ? scr->scr_height - height : y;
2646 wMenuMove(menu, x, y, True);
2647 menu->flags.buttoned = 1;
2648 wFrameWindowShowButton(menu->frame, WFF_RIGHT_BUTTON);
2649 res = True;
2653 PLRelease(key);
2655 for (i=0; i<menu->cascade_no; i++) {
2656 if (restoreMenuRecurs(scr, menus, menu->cascades[i], buffer) != False)
2657 res = True;
2660 return res;
2662 #endif /* !LITE */
2665 void
2666 wMenuRestoreState(WScreen *scr)
2668 proplist_t menus, menu, key, skey;
2670 key = PLMakeString("Menus");
2671 menus = PLGetDictionaryEntry(scr->session_state, key);
2672 PLRelease(key);
2674 if (!menus)
2675 return;
2677 /* restore menus */
2679 skey = PLMakeString("SwitchMenu");
2680 menu = PLGetDictionaryEntry(menus, skey);
2681 restoreMenu(scr, menu, WSS_SWITCHMENU);
2683 #ifndef LITE
2684 if (!scr->root_menu) {
2685 OpenRootMenu(scr, scr->scr_width*2, 0, False);
2686 wMenuUnmap(scr->root_menu);
2688 restoreMenuRecurs(scr, menus, scr->root_menu, "");
2689 #endif /* !LITE */
2691 PLRelease(skey);
2695 void
2696 OpenWorkspaceMenu(WScreen *scr, int x, int y)
2698 WMenu *menu, *parent;
2699 WMenuEntry *entry;
2701 #ifndef LITE
2702 if (!scr->root_menu) {
2703 OpenRootMenu(scr, scr->scr_width*2, 0, False);
2704 wMenuUnmap(scr->root_menu);
2706 #endif
2707 menu = scr->workspace_menu;
2708 if (menu) {
2709 if (menu->flags.mapped) {
2710 if (!menu->flags.buttoned) {
2711 wMenuUnmap(menu);
2712 parent = menu->parent;
2713 if (parent && parent->selected_entry >= 0) {
2714 entry = parent->entries[parent->selected_entry];
2715 if (parent->cascades[entry->cascade] == menu) {
2716 selectEntry(parent, -1);
2717 wMenuMapAt(menu, x, y, False);
2720 } else {
2721 wRaiseFrame(menu->frame->core);
2722 wMenuMapCopyAt(menu, x, y);
2724 } else {
2725 wMenuMapAt(menu, x, y, False);