fixed cosmetic bug in geom. dpy window for 8bpp
[wmaker-crm.git] / src / menu.c
bloba35e0e9a6f7328cfef695e3d0e08d54a4ffdf079
1 /* menu.c- generic menu, used for root menu, application menus etc.
3 * Window Maker window manager
4 *
5 * Copyright (c) 1997, 1998 Alfredo K. Kojima
6 * Copyright (c) 1997, 1998 Dan Pascu
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
21 * USA.
24 #include "wconfig.h"
26 #include <X11/Xlib.h>
27 #include <X11/Xutil.h>
28 #include <X11/keysym.h>
29 #include <stdlib.h>
30 #include <string.h>
31 #include <stdio.h>
32 #include <unistd.h>
33 #include <ctype.h>
35 #include "WindowMaker.h"
36 #include "wcore.h"
37 #include "framewin.h"
38 #include "menu.h"
39 #include "actions.h"
40 #include "funcs.h"
41 #include "stacking.h"
42 #include "text.h"
45 /****** Global Variables ******/
47 extern Cursor wCursor[WCUR_LAST];
49 extern XContext wWinContext;
51 extern WPreferences wPreferences;
53 #define MOD_MASK wPreferences.modifier_mask
55 #define MENU_SCROLL_STEP menuScrollParameters[(int)wPreferences.menu_scroll_speed].steps
56 #define MENU_SCROLL_DELAY menuScrollParameters[(int)wPreferences.menu_scroll_speed].delay
60 #define MENUW(m) ((m)->frame->core->width+2*FRAME_BORDER_WIDTH)
61 #define MENUH(m) ((m)->frame->core->height+2*FRAME_BORDER_WIDTH)
64 /***** Local Stuff ******/
66 static struct {
67 int steps;
68 int delay;
69 } menuScrollParameters[5] = {
70 {MENU_SCROLL_STEPS_UF, MENU_SCROLL_DELAY_UF},
71 {MENU_SCROLL_STEPS_F, MENU_SCROLL_DELAY_F},
72 {MENU_SCROLL_STEPS_M, MENU_SCROLL_DELAY_M},
73 {MENU_SCROLL_STEPS_S, MENU_SCROLL_DELAY_S},
74 {MENU_SCROLL_STEPS_U, MENU_SCROLL_DELAY_U}};
77 static void menuMouseDown(WObjDescriptor *desc, XEvent *event);
78 static void menuExpose(WObjDescriptor *desc, XEvent *event);
80 static void menuTitleDoubleClick(WCoreWindow *sender, void *data, XEvent *event);
81 static void menuTitleMouseDown(WCoreWindow *sender, void *data, XEvent *event);
83 static void menuCloseClick(WCoreWindow *sender, void *data, XEvent *event);
85 static void updateTexture(WMenu *menu);
87 #ifndef LITE
88 static int saveMenuRecurs(proplist_t menus, WScreen *scr, WMenu *menu);
89 static int restoreMenuRecurs(WScreen *scr, proplist_t menus, WMenu *menu, char *path);
90 #endif /* !LITE */
92 static void selectEntry(WMenu *menu, int entry_no);
93 static void closeCascade(WMenu *menu);
96 /****** Notification Observers ******/
98 static void
99 appearanceObserver(void *self, WMNotification *notif)
101 WMenu *menu = (WMenu*)self;
102 int flags = (int)WMGetNotificationClientData(notif);
104 if (!menu->flags.realized)
105 return;
107 if (WMGetNotificationName(notif) == WNMenuAppearanceSettingsChanged) {
108 if (flags & WFontSettings) {
109 menu->flags.realized = 0;
110 wMenuRealize(menu);
112 if (flags & WTextureSettings) {
113 if (!menu->flags.brother)
114 updateTexture(menu);
116 if (flags & (WTextureSettings|WColorSettings)) {
117 wMenuPaint(menu);
119 } else if (menu->flags.titled) {
121 if (flags & WFontSettings) {
122 menu->flags.realized = 0;
123 wMenuRealize(menu);
125 if (flags & WTextureSettings) {
126 menu->frame->flags.need_texture_remake = 1;
128 if (flags & (WColorSettings|WTextureSettings)) {
129 wFrameWindowPaint(menu->frame);
134 /************************************/
138 *----------------------------------------------------------------------
139 * wMenuCreate--
140 * Creates a new empty menu with the specified title. If main_menu
141 * is True, the created menu will be a main menu, which has some special
142 * properties such as being placed over other normal menus.
143 * If title is NULL, the menu will have no titlebar.
145 * Returns:
146 * The created menu.
147 *----------------------------------------------------------------------
149 WMenu*
150 wMenuCreate(WScreen *screen, char *title, int main_menu)
152 WMenu *menu;
153 static int brother=0;
154 int tmp, flags;
156 menu = wmalloc(sizeof(WMenu));
158 memset(menu, 0, sizeof(WMenu));
160 #ifdef SINGLE_MENULEVEL
161 tmp = WMSubmenuLevel;
162 #else
163 tmp = (main_menu ? WMMainMenuLevel : WMSubmenuLevel);
164 #endif
166 flags = WFF_SINGLE_STATE;
167 if (title) {
168 flags |= WFF_TITLEBAR|WFF_RIGHT_BUTTON;
169 menu->flags.titled = 1;
171 menu->frame =
172 wFrameWindowCreate(screen, tmp, 8, 2, 1, 1, flags,
173 screen->menu_title_texture, NULL,
174 screen->menu_title_pixel, &screen->menu_title_gc,
175 &screen->menu_title_font);
177 menu->frame->core->descriptor.parent = menu;
178 menu->frame->core->descriptor.parent_type = WCLASS_MENU;
179 menu->frame->core->descriptor.handle_mousedown = menuMouseDown;
181 wFrameWindowHideButton(menu->frame, WFF_RIGHT_BUTTON);
183 if (title) {
184 menu->frame->title = wstrdup(title);
187 menu->frame->flags.justification = WTJ_LEFT;
189 menu->frame->rbutton_image = screen->b_pixmaps[WBUT_CLOSE];
191 menu->entry_no = 0;
192 menu->alloced_entries = 0;
193 menu->selected_entry = -1;
194 menu->entries = NULL;
196 menu->frame_x = screen->app_menu_x;
197 menu->frame_y = screen->app_menu_y;
199 menu->frame->child = menu;
201 menu->flags.lowered = 0;
203 /* create borders */
204 if (title) {
205 /* setup object descriptors */
206 menu->frame->on_mousedown_titlebar = menuTitleMouseDown;
207 menu->frame->on_dblclick_titlebar = menuTitleDoubleClick;
210 menu->frame->on_click_right = menuCloseClick;
213 menu->menu = wCoreCreate(menu->frame->core, 0, menu->frame->top_width,
214 menu->frame->core->width, 10);
216 menu->menu->descriptor.parent = menu;
217 menu->menu->descriptor.parent_type = WCLASS_MENU;
218 menu->menu->descriptor.handle_expose = menuExpose;
219 menu->menu->descriptor.handle_mousedown = menuMouseDown;
221 menu->menu_texture_data = None;
223 XMapWindow(dpy, menu->menu->window);
225 XFlush(dpy);
227 if (!brother) {
228 brother = 1;
229 menu->brother = wMenuCreate(screen, title, main_menu);
230 brother = 0;
231 menu->brother->flags.brother = 1;
232 menu->brother->brother = menu;
234 WMAddNotificationObserver(appearanceObserver, menu,
235 WNMenuAppearanceSettingsChanged, menu);
237 WMAddNotificationObserver(appearanceObserver, menu,
238 WNMenuTitleAppearanceSettingsChanged, menu);
241 return menu;
247 WMenu*
248 wMenuCreateForApp(WScreen *screen, char *title, int main_menu)
250 WMenu *menu;
252 menu = wMenuCreate(screen, title, main_menu);
253 if (!menu)
254 return NULL;
255 menu->flags.app_menu = 1;
256 menu->brother->flags.app_menu = 1;
258 return menu;
263 static void
264 insertEntry(WMenu *menu, WMenuEntry *entry, int index)
266 int i;
268 for (i = menu->entry_no-1; i >= index; i--) {
269 menu->entries[i]->order++;
270 menu->entries[i+1] = menu->entries[i];
272 menu->entries[index] = entry;
276 WMenuEntry*
277 wMenuInsertCallback(WMenu *menu, int index, char *text,
278 void (*callback)(WMenu *menu, WMenuEntry *entry),
279 void *clientdata)
281 WMenuEntry *entry;
283 #ifdef DEBUG
284 if (!menu) {
285 printf("Passed NULL as menu parameter to wMenuAddCallback() \n");
286 return NULL;
288 #endif
290 assert(menu->flags.brother==0);
291 menu->flags.realized = 0;
292 menu->brother->flags.realized = 0;
294 /* reallocate array if it's too small */
295 if (menu->entry_no >= menu->alloced_entries) {
296 void *tmp;
297 #ifdef DEBUG
298 puts("doing wrealloc()");
299 #endif
300 tmp = wrealloc(menu->entries,
301 sizeof(WMenuEntry)*(menu->alloced_entries+5));
302 if (tmp==NULL) {
303 wwarning(_("wrealloc() failed while trying to add menu item"));
304 return NULL;
307 menu->entries = tmp;
308 menu->alloced_entries += 5;
310 menu->brother->entries = tmp;
311 menu->brother->alloced_entries = menu->alloced_entries;
313 entry = wmalloc(sizeof(WMenuEntry));
314 memset(entry, 0, sizeof(WMenuEntry));
315 entry->flags.enabled = 1;
316 entry->text = wstrdup(text);
317 entry->cascade = -1;
318 entry->clientdata = clientdata;
319 entry->callback = callback;
320 if (index<0 || index>=menu->entry_no) {
321 entry->order = menu->entry_no;
322 menu->entries[menu->entry_no] = entry;
323 } else {
324 entry->order = index;
325 insertEntry(menu, entry, index);
328 menu->entry_no++;
329 menu->brother->entry_no = menu->entry_no;
331 return entry;
336 void
337 wMenuEntrySetCascade(WMenu *menu, WMenuEntry *entry, WMenu *cascade)
339 WMenu *brother = menu->brother;
340 int i, done;
342 assert(menu->flags.brother==0);
344 if (entry->cascade>=0) {
345 menu->flags.realized = 0;
346 brother->flags.realized = 0;
349 cascade->parent = menu;
351 cascade->brother->parent = brother;
353 done = 0;
354 for (i=0; i<menu->cascade_no; i++) {
355 if (menu->cascades[i]==NULL) {
356 menu->cascades[i] = cascade;
357 brother->cascades[i] = cascade->brother;
358 done = 1;
359 entry->cascade = i;
360 break;
363 if (!done) {
364 entry->cascade = menu->cascade_no;
366 menu->cascades = wrealloc(menu->cascades,
367 sizeof(WMenu)*(menu->cascade_no+1));
368 menu->cascades[menu->cascade_no++] = cascade;
371 brother->cascades = wrealloc(brother->cascades,
372 sizeof(WMenu)*(brother->cascade_no+1));
373 brother->cascades[brother->cascade_no++] = cascade->brother;
377 if (menu->flags.lowered) {
379 cascade->flags.lowered = 1;
380 ChangeStackingLevel(cascade->frame->core, WMNormalLevel);
382 cascade->brother->flags.lowered = 1;
383 ChangeStackingLevel(cascade->brother->frame->core, WMNormalLevel);
386 if (!menu->flags.realized)
387 wMenuRealize(menu);
391 void
392 wMenuEntryRemoveCascade(WMenu *menu, WMenuEntry *entry)
394 assert(menu->flags.brother==0);
396 /* destroy cascade menu */
397 if (entry->cascade>=0 && menu->cascades
398 && menu->cascades[entry->cascade]!=NULL) {
400 wMenuDestroy(menu->cascades[entry->cascade], True);
402 menu->cascades[entry->cascade] = NULL;
403 menu->brother->cascades[entry->cascade] = NULL;
405 entry->cascade = -1;
410 void
411 wMenuRemoveItem(WMenu *menu, int index)
413 int i;
415 if (menu->flags.brother) {
416 wMenuRemoveItem(menu->brother, index);
417 return;
420 if (index>=menu->entry_no) return;
422 /* destroy cascade menu */
423 wMenuEntryRemoveCascade(menu, menu->entries[index]);
425 /* destroy unshared data */
427 if (menu->entries[index]->text)
428 free(menu->entries[index]->text);
430 if (menu->entries[index]->rtext)
431 free(menu->entries[index]->rtext);
433 if (menu->entries[index]->free_cdata && menu->entries[index]->clientdata)
434 (*menu->entries[index]->free_cdata)(menu->entries[index]->clientdata);
436 free(menu->entries[index]);
438 for (i=index; i<menu->entry_no-1; i++) {
439 menu->entries[i+1]->order--;
440 menu->entries[i]=menu->entries[i+1];
442 menu->entry_no--;
443 menu->brother->entry_no--;
447 static Pixmap
448 renderTexture(WMenu *menu)
450 RImage *img;
451 Pixmap pix;
452 int i;
453 RColor light;
454 RColor dark;
455 RColor mid;
456 WScreen *scr = menu->menu->screen_ptr;
457 WTexture *texture = scr->menu_item_texture;
459 if (wPreferences.menu_style == MS_NORMAL) {
460 img = wTextureRenderImage(texture, menu->menu->width,
461 menu->entry_height, WREL_MENUENTRY);
462 } else {
463 img = wTextureRenderImage(texture, menu->menu->width,
464 menu->menu->height+1, WREL_MENUENTRY);
466 if (!img) {
467 wwarning(_("could not render texture: %s"),
468 RMessageForError(RErrorCode));
470 return None;
473 if (wPreferences.menu_style == MS_SINGLE_TEXTURE) {
474 light.alpha = 0;
475 light.red = light.green = light.blue = 80;
477 dark.alpha = 255;
478 dark.red = dark.green = dark.blue = 0;
480 mid.alpha = 0;
481 mid.red = mid.green = mid.blue = 40;
483 for (i = 1; i < menu->entry_no; i++) {
484 ROperateLine(img, RSubtractOperation, 0, i*menu->entry_height-2,
485 menu->menu->width-1, i*menu->entry_height-2, &mid);
487 RDrawLine(img, 0, i*menu->entry_height-1,
488 menu->menu->width-1, i*menu->entry_height-1, &dark);
490 ROperateLine(img, RAddOperation, 0, i*menu->entry_height,
491 menu->menu->width-1, i*menu->entry_height,
492 &light);
495 if (!RConvertImage(scr->rcontext, img, &pix)) {
496 wwarning(_("error rendering image:%s"), RMessageForError(RErrorCode));
498 RDestroyImage(img);
500 return pix;
504 static void
505 updateTexture(WMenu *menu)
507 WScreen *scr = menu->menu->screen_ptr;
509 /* setup background texture */
510 if (scr->menu_item_texture->any.type != WTEX_SOLID) {
511 if (!menu->flags.brother) {
512 FREE_PIXMAP(menu->menu_texture_data);
514 menu->menu_texture_data = renderTexture(menu);
516 XSetWindowBackgroundPixmap(dpy, menu->menu->window,
517 menu->menu_texture_data);
518 XClearWindow(dpy, menu->menu->window);
520 XSetWindowBackgroundPixmap(dpy, menu->brother->menu->window,
521 menu->menu_texture_data);
522 XClearWindow(dpy, menu->brother->menu->window);
524 } else {
525 XSetWindowBackground(dpy, menu->menu->window,
526 scr->menu_item_texture->any.color.pixel);
527 XClearWindow(dpy, menu->menu->window);
532 void
533 wMenuRealize(WMenu *menu)
535 int i;
536 int width, rwidth, mrwidth, mwidth;
537 int theight, twidth, eheight;
538 WScreen *scr = menu->frame->screen_ptr;
539 static int brother_done=0;
540 int flags;
542 if (!brother_done) {
543 brother_done = 1;
544 wMenuRealize(menu->brother);
545 brother_done = 0;
548 flags = WFF_SINGLE_STATE;
549 if (menu->flags.titled)
550 flags |= WFF_TITLEBAR|WFF_RIGHT_BUTTON;
552 wFrameWindowUpdateBorders(menu->frame, flags);
554 if (menu->flags.titled) {
555 twidth = WMWidthOfString(scr->menu_title_font, menu->frame->title,
556 strlen(menu->frame->title));
557 theight = menu->frame->top_width;
558 twidth += theight + (wPreferences.new_style ? 16 : 8);
559 } else {
560 twidth = 0;
561 theight = 0;
563 eheight = WMFontHeight(scr->menu_entry_font) + 6;
564 menu->entry_height = eheight;
565 mrwidth = 0;
566 mwidth = 0;
567 for (i=0; i<menu->entry_no; i++) {
568 char *text;
570 /* search widest text */
571 text = menu->entries[i]->text;
572 width = WMWidthOfString(scr->menu_entry_font, text, strlen(text))+10;
574 if (menu->entries[i]->flags.indicator) {
575 width += MENU_INDICATOR_SPACE;
578 if (width > mwidth)
579 mwidth = width;
581 /* search widest text on right */
582 text = menu->entries[i]->rtext;
583 if (text)
584 rwidth = WMWidthOfString(scr->menu_entry_font, text, strlen(text))
585 + 5;
586 else if (menu->entries[i]->cascade>=0)
587 rwidth = 16;
588 else
589 rwidth = 4;
591 if (rwidth > mrwidth)
592 mrwidth = rwidth;
594 mwidth += mrwidth;
596 if (mwidth < twidth)
597 mwidth = twidth;
600 wCoreConfigure(menu->menu, 0, theight, mwidth, menu->entry_no*eheight -1);
602 wFrameWindowResize(menu->frame, mwidth, menu->entry_no*eheight-1
603 + menu->frame->top_width + menu->frame->bottom_width);
606 updateTexture(menu);
608 menu->flags.realized = 1;
610 if (menu->flags.mapped)
611 wMenuPaint(menu);
612 if (menu->brother->flags.mapped)
613 wMenuPaint(menu->brother);
617 void
618 wMenuDestroy(WMenu *menu, int recurse)
620 int i;
622 WMRemoveNotificationObserver(menu);
624 /* remove any pending timers */
625 if (menu->timer)
626 WMDeleteTimerHandler(menu->timer);
627 menu->timer = NULL;
629 /* call destroy handler */
630 if (menu->on_destroy)
631 (*menu->on_destroy)(menu);
633 /* Destroy items if this menu own them. If this is the "brother" menu,
634 * leave them alone as it is shared by them.
636 if (!menu->flags.brother) {
637 for (i=0; i<menu->entry_no; i++) {
639 free(menu->entries[i]->text);
641 if (menu->entries[i]->rtext)
642 free(menu->entries[i]->rtext);
643 #ifdef USER_MENU
645 if (menu->entries[i]->instances){
646 PLRelease(menu->entries[i]->instances);
648 #endif /* USER_MENU */
650 if (menu->entries[i]->free_cdata && menu->entries[i]->clientdata) {
651 (*menu->entries[i]->free_cdata)(menu->entries[i]->clientdata);
653 free(menu->entries[i]);
656 if (recurse) {
657 for (i=0; i<menu->cascade_no; i++) {
658 if (menu->cascades[i]) {
659 if (menu->cascades[i]->flags.brother)
660 wMenuDestroy(menu->cascades[i]->brother, recurse);
661 else
662 wMenuDestroy(menu->cascades[i], recurse);
667 if (menu->entries)
668 free(menu->entries);
672 FREE_PIXMAP(menu->menu_texture_data);
674 if (menu->cascades)
675 free(menu->cascades);
677 wCoreDestroy(menu->menu);
678 wFrameWindowDestroy(menu->frame);
680 /* destroy copy of this menu */
681 if (!menu->flags.brother && menu->brother)
682 wMenuDestroy(menu->brother, False);
684 free(menu);
688 #define F_NORMAL 0
689 #define F_TOP 1
690 #define F_BOTTOM 2
691 #define F_NONE 3
693 static void
694 drawFrame(WScreen *scr, Window win, int y, int w, int h, int type)
696 XSegment segs[2];
697 int i;
699 i = 0;
700 segs[i].x1 = segs[i].x2 = w-1;
701 segs[i].y1 = y;
702 segs[i].y2 = y + h - 1;
703 i++;
704 if (type != F_TOP && type != F_NONE) {
705 segs[i].x1 = 1;
706 segs[i].y1 = segs[i].y2 = y + h-2;
707 segs[i].x2 = w-1;
708 i++;
710 XDrawSegments(dpy, win, scr->menu_item_auxtexture->dim_gc, segs, i);
712 i = 0;
713 segs[i].x1 = 0;
714 segs[i].y1 = y;
715 segs[i].x2 = 0;
716 segs[i].y2 = y + h - 1;
717 i++;
718 if (type != F_BOTTOM && type != F_NONE) {
719 segs[i].x1 = 0;
720 segs[i].y1 = y;
721 segs[i].x2 = w-1;
722 segs[i].y2 = y;
723 i++;
725 XDrawSegments(dpy, win, scr->menu_item_auxtexture->light_gc, segs, i);
727 if (type != F_TOP && type != F_NONE)
728 XDrawLine(dpy, win, scr->menu_item_auxtexture->dark_gc, 0, y+h-1,
729 w-1, y+h-1);
733 static void
734 paintEntry(WMenu *menu, int index, int selected)
736 int x, y, w, h, tw;
737 int type;
738 GC light, dim, dark, textGC;
739 WScreen *scr=menu->frame->screen_ptr;
740 Window win = menu->menu->window;
741 WMenuEntry *entry=menu->entries[index];
743 if (!menu->flags.realized) return;
744 h = menu->entry_height;
745 w = menu->menu->width;
746 y = index * h;
748 light = scr->menu_item_auxtexture->light_gc;
749 dim = scr->menu_item_auxtexture->dim_gc;
750 dark = scr->menu_item_auxtexture->dark_gc;
752 if (wPreferences.menu_style == MS_FLAT && menu->entry_no > 1) {
753 if (index == 0)
754 type = F_TOP;
755 else if (index == menu->entry_no - 1)
756 type = F_BOTTOM;
757 else
758 type = F_NONE;
759 } else {
760 type = F_NORMAL;
763 /* paint background */
764 if (selected) {
765 XSetForeground(dpy, scr->select_menu_gc, scr->select_pixel);
766 XFillRectangle(dpy, win, scr->select_menu_gc, 1, y+1, w-2, h-3);
767 if (scr->menu_item_texture->any.type == WTEX_SOLID)
768 drawFrame(scr, win, y, w, h, type);
769 } else {
770 if (scr->menu_item_texture->any.type == WTEX_SOLID) {
771 XClearArea(dpy, win, 0, y + 1, w - 1, h - 3, False);
772 /* draw the frame */
773 drawFrame(scr, win, y, w, h, type);
774 } else {
775 XClearArea(dpy, win, 0, y, w, h, False);
779 if (selected) {
780 textGC = scr->select_menu_gc;
781 if (entry->flags.enabled)
782 XSetForeground(dpy, textGC, scr->select_text_pixel);
783 else
784 XSetForeground(dpy, textGC, scr->dtext_pixel);
785 } else if (!entry->flags.enabled) {
786 textGC = scr->disabled_menu_entry_gc;
787 } else {
788 textGC = scr->menu_entry_gc;
790 /* draw text */
791 x = 5;
792 if (entry->flags.indicator)
793 x += MENU_INDICATOR_SPACE + 2;
795 WMDrawString(scr->wmscreen, win, textGC, scr->menu_entry_font,
796 x, 3 + y, entry->text, strlen(entry->text));
798 if (entry->cascade>=0) {
799 /* draw the cascade indicator */
800 XDrawLine(dpy,win,dim, w-11, y+6, w-6, y+h/2-1);
801 XDrawLine(dpy,win,light, w-11, y+h-8, w-6, y+h/2-1);
802 XDrawLine(dpy,win,dark, w-12, y+6, w-12, y+h-8);
805 /* draw indicator */
806 if (entry->flags.indicator && entry->flags.indicator_on) {
807 int iw, ih;
808 WPixmap *indicator;
811 switch (entry->flags.indicator_type) {
812 case MI_CHECK:
813 indicator = scr->menu_check_indicator;
814 break;
815 case MI_MINIWINDOW:
816 indicator = scr->menu_mini_indicator;
817 break;
818 case MI_HIDDEN:
819 indicator = scr->menu_hide_indicator;
820 break;
821 case MI_SHADED:
822 indicator = scr->menu_shade_indicator;
823 break;
824 case MI_DIAMOND:
825 default:
826 indicator = scr->menu_radio_indicator;
827 break;
830 iw = indicator->width;
831 ih = indicator->height;
832 XSetClipMask(dpy, scr->copy_gc, indicator->mask);
833 XSetClipOrigin(dpy, scr->copy_gc, 5, y+(h-ih)/2);
834 if (selected)
835 XSetForeground(dpy, scr->copy_gc, scr->black_pixel);
836 else
837 XSetForeground(dpy, scr->copy_gc, scr->mtext_pixel);
838 XFillRectangle(dpy, win, scr->copy_gc, 5, y+(h-ih)/2, iw, ih);
840 XCopyArea(dpy, indicator->image, win, scr->copy_gc, 0, 0,
841 iw, ih, 5, y+(h-ih)/2);
843 XSetClipOrigin(dpy, scr->copy_gc, 0, 0);
846 /* draw right text */
848 if (entry->rtext && entry->cascade<0) {
849 tw = WMWidthOfString(scr->menu_entry_font, entry->rtext,
850 strlen(entry->rtext));
852 WMDrawString(scr->wmscreen, win, textGC, scr->menu_entry_font, w-6-tw,
853 y + 3, entry->rtext, strlen(entry->rtext));
858 static void
859 move_menus(WMenu *menu, int x, int y)
861 while (menu->parent) {
862 menu = menu->parent;
863 x -= MENUW(menu);
864 if (!wPreferences.align_menus && menu->selected_entry>=0) {
865 y -= menu->selected_entry*menu->entry_height;
868 wMenuMove(menu, x, y, True);
871 static void
872 makeVisible(WMenu *menu)
874 WScreen *scr = menu->frame->screen_ptr;
875 int x1, y1, x2, y2, new_x, new_y, move;
877 if (menu->entry_no<0) return;
879 x1 = menu->frame_x;
880 y1 = menu->frame_y+menu->frame->top_width
881 + menu->selected_entry*menu->entry_height;
882 x2 = x1 + MENUW(menu);
883 y2 = y1 + menu->entry_height;
885 new_x = x1;
886 new_y = y1;
887 move = 0;
889 if (x1 < 0) {
890 new_x = 0;
891 move = 1;
892 } else if (x2 >= scr->scr_width) {
893 new_x = scr->scr_width - MENUW(menu) - 1;
894 move = 1;
897 if (y1 < 0) {
898 new_y = 0;
899 move = 1;
900 } else if (y2 >= scr->scr_height) {
901 new_y = scr->scr_height - menu->entry_height - 1;
902 move = 1;
905 new_y = new_y - menu->frame->top_width
906 - menu->selected_entry*menu->entry_height;
907 move_menus(menu, new_x, new_y);
911 static int
912 check_key(WMenu *menu, XKeyEvent *event)
914 int i, ch, s;
915 char buffer[32];
917 if (XLookupString(event, buffer, 32, NULL, NULL)<1)
918 return -1;
920 ch = toupper(buffer[0]);
922 s = (menu->selected_entry>=0 ? menu->selected_entry+1 : 0);
924 again:
925 for (i=s; i<menu->entry_no; i++) {
926 if (ch==toupper(menu->entries[i]->text[0])) {
927 return i;
930 /* no match. Retry from start, if previous started from a selected entry */
931 if (s!=0) {
932 s = 0;
933 goto again;
935 return -1;
939 static int
940 keyboardMenu(WMenu *menu)
942 XEvent event;
943 KeySym ksym=NoSymbol;
944 int done=0;
945 int index;
946 WMenuEntry *entry;
947 int old_pos_x = menu->frame_x;
948 int old_pos_y = menu->frame_y;
949 int new_x = old_pos_x, new_y = old_pos_y;
950 int scr_width = menu->frame->screen_ptr->scr_width;
951 int scr_height = menu->frame->screen_ptr->scr_height;
953 if (menu->flags.editing)
954 return False;
957 XGrabKeyboard(dpy, menu->frame->core->window, True, GrabModeAsync,
958 GrabModeAsync, CurrentTime);
960 if (menu->frame_y+menu->frame->top_width >= scr_height)
961 new_y = scr_height - menu->frame->top_width;
963 if (menu->frame_x+MENUW(menu) >= scr_width)
964 new_x = scr_width-MENUW(menu)-1;
966 move_menus(menu, new_x, new_y);
968 while (!done && menu->flags.mapped) {
969 XAllowEvents(dpy, AsyncKeyboard, CurrentTime);
970 WMMaskEvent(dpy, ExposureMask|ButtonMotionMask|ButtonPressMask
971 |ButtonReleaseMask|KeyPressMask|KeyReleaseMask
972 |SubstructureNotifyMask, &event);
974 switch (event.type) {
975 case KeyPress:
976 ksym = XLookupKeysym(&event.xkey, 0);
977 switch (ksym) {
978 case XK_Escape:
979 done = 1;
980 break;
982 case XK_Home:
983 case XK_KP_Home:
984 selectEntry(menu, 0);
985 makeVisible(menu);
986 break;
988 case XK_End:
989 case XK_KP_End:
990 selectEntry(menu, menu->entry_no-1);
991 makeVisible(menu);
992 break;
994 case XK_Up:
995 #ifdef ARROWLESS_KBD
996 case XK_k:
997 #endif
998 case XK_KP_Up:
999 if (menu->selected_entry <= 0)
1000 selectEntry(menu, menu->entry_no-1);
1001 else
1002 selectEntry(menu, menu->selected_entry-1);
1003 makeVisible(menu);
1004 break;
1006 case XK_Down:
1007 #ifdef ARROWLESS_KBD
1008 case XK_j:
1009 #endif
1010 case XK_KP_Down:
1011 if (menu->selected_entry<0)
1012 selectEntry(menu, 0);
1013 else if (menu->selected_entry == menu->entry_no-1)
1014 selectEntry(menu, 0);
1015 else if (menu->selected_entry < menu->entry_no-1)
1016 selectEntry(menu, menu->selected_entry+1);
1017 makeVisible(menu);
1018 break;
1020 case XK_Right:
1021 #ifdef ARROWLESS_KBD
1022 case XK_l:
1023 #endif
1024 case XK_KP_Right:
1025 if (menu->selected_entry>=0) {
1026 WMenuEntry *entry;
1027 entry = menu->entries[menu->selected_entry];
1029 if (entry->cascade >= 0 && menu->cascades
1030 && menu->cascades[entry->cascade]->entry_no > 0) {
1032 XUngrabKeyboard(dpy, CurrentTime);
1034 selectEntry(menu->cascades[entry->cascade], 0);
1035 if (!keyboardMenu(menu->cascades[entry->cascade]))
1036 done = 1;
1038 XGrabKeyboard(dpy, menu->frame->core->window, True,
1039 GrabModeAsync, GrabModeAsync,
1040 CurrentTime);
1043 break;
1045 case XK_Left:
1046 #ifdef ARROWLESS_KBD
1047 case XK_h:
1048 #endif
1049 case XK_KP_Left:
1050 if (menu->parent!=NULL && menu->parent->selected_entry>=0) {
1051 selectEntry(menu, -1);
1052 move_menus(menu, old_pos_x, old_pos_y);
1053 return True;
1055 break;
1057 case XK_Return:
1058 done = 2;
1059 break;
1061 default:
1062 index = check_key(menu, &event.xkey);
1063 if (index>=0) {
1064 selectEntry(menu, index);
1067 break;
1069 default:
1070 if (event.type==ButtonPress)
1071 done = 1;
1073 WMHandleEvent(&event);
1077 XUngrabKeyboard(dpy, CurrentTime);
1079 if (done==2 && menu->selected_entry>=0) {
1080 entry = menu->entries[menu->selected_entry];
1081 } else {
1082 entry = NULL;
1085 if (entry && entry->callback!=NULL && entry->flags.enabled
1086 && entry->cascade < 0) {
1087 #if (MENU_BLINK_COUNT > 0)
1088 int sel = menu->selected_entry;
1089 int i;
1091 for (i=0; i<MENU_BLINK_COUNT; i++) {
1092 paintEntry(menu, sel, False);
1093 XSync(dpy, 0);
1094 wusleep(MENU_BLINK_DELAY);
1095 paintEntry(menu, sel, True);
1096 XSync(dpy, 0);
1097 wusleep(MENU_BLINK_DELAY);
1099 #endif
1100 selectEntry(menu, -1);
1102 if (!menu->flags.buttoned) {
1103 wMenuUnmap(menu);
1104 move_menus(menu, old_pos_x, old_pos_y);
1106 closeCascade(menu);
1108 (*entry->callback)(menu, entry);
1109 } else {
1110 if (!menu->flags.buttoned) {
1111 wMenuUnmap(menu);
1112 move_menus(menu, old_pos_x, old_pos_y);
1114 selectEntry(menu, -1);
1118 /* returns True if returning from a submenu to a parent menu,
1119 * False if exiting from menu */
1120 return False;
1124 void
1125 wMenuMapAt(WMenu *menu, int x, int y, int keyboard)
1127 int scr_width = menu->frame->screen_ptr->scr_width;
1128 int scr_height = menu->frame->screen_ptr->scr_height;
1130 if (!menu->flags.realized) {
1131 menu->flags.realized=1;
1132 wMenuRealize(menu);
1134 if (!menu->flags.mapped) {
1135 if (wPreferences.wrap_menus) {
1136 if (x<0) x = 0;
1137 if (y<0) y = 0;
1138 if (x+MENUW(menu) > scr_width)
1139 x = scr_width - MENUW(menu);
1140 if (y+MENUH(menu) > scr_height)
1141 y = scr_height - MENUH(menu);
1144 XMoveWindow(dpy, menu->frame->core->window, x, y);
1145 menu->frame_x = x;
1146 menu->frame_y = y;
1147 XMapWindow(dpy, menu->frame->core->window);
1148 wRaiseFrame(menu->frame->core);
1149 menu->flags.mapped = 1;
1150 } else {
1151 selectEntry(menu, 0);
1154 if (keyboard)
1155 keyboardMenu(menu);
1159 void
1160 wMenuMap(WMenu *menu)
1162 if (!menu->flags.realized) {
1163 menu->flags.realized=1;
1164 wMenuRealize(menu);
1166 if (menu->flags.app_menu && menu->parent==NULL) {
1167 menu->frame_x = menu->frame->screen_ptr->app_menu_x;
1168 menu->frame_y = menu->frame->screen_ptr->app_menu_y;
1169 XMoveWindow(dpy, menu->frame->core->window, menu->frame_x, menu->frame_y);
1171 XMapWindow(dpy, menu->frame->core->window);
1172 wRaiseFrame(menu->frame->core);
1173 menu->flags.mapped = 1;
1177 void
1178 wMenuUnmap(WMenu *menu)
1180 int i;
1182 XUnmapWindow(dpy, menu->frame->core->window);
1183 if (menu->flags.titled && menu->flags.buttoned) {
1184 wFrameWindowHideButton(menu->frame, WFF_RIGHT_BUTTON);
1186 menu->flags.buttoned = 0;
1187 menu->flags.mapped = 0;
1188 menu->flags.open_to_left = 0;
1190 for (i=0; i<menu->cascade_no; i++) {
1191 if (menu->cascades[i]!=NULL
1192 && menu->cascades[i]->flags.mapped
1193 && !menu->cascades[i]->flags.buttoned) {
1195 wMenuUnmap(menu->cascades[i]);
1198 menu->selected_entry = -1;
1203 void
1204 wMenuPaint(WMenu *menu)
1206 int i;
1208 if (!menu->flags.mapped) {
1209 return;
1212 /* paint entries */
1213 for (i=0; i<menu->entry_no; i++) {
1214 paintEntry(menu, i, i==menu->selected_entry);
1219 void
1220 wMenuSetEnabled(WMenu *menu, int index, int enable)
1222 if (index>=menu->entry_no) return;
1223 menu->entries[index]->flags.enabled=enable;
1224 paintEntry(menu, index, index==menu->selected_entry);
1225 paintEntry(menu->brother, index, index==menu->selected_entry);
1229 /* ====================================================================== */
1232 static void
1233 editEntry(WMenu *menu, WMenuEntry *entry)
1235 WTextInput *text;
1236 XEvent event;
1237 WObjDescriptor *desc;
1238 char *t;
1239 int done = 0;
1240 Window old_focus;
1241 int old_revert;
1243 menu->flags.editing = 1;
1245 text = wTextCreate(menu->menu, 1, menu->entry_height * entry->order,
1246 menu->menu->width - 2, menu->entry_height - 1);
1248 wTextPutText(text, entry->text);
1249 XGetInputFocus(dpy, &old_focus, &old_revert);
1250 XSetInputFocus(dpy, text->core->window, RevertToNone, CurrentTime);
1252 if (XGrabKeyboard(dpy, text->core->window, True, GrabModeAsync,
1253 GrabModeAsync, CurrentTime)!=GrabSuccess) {
1254 wwarning("could not grab keyboard");
1255 wTextDestroy(text);
1257 wSetFocusTo(menu->frame->screen_ptr,
1258 menu->frame->screen_ptr->focused_window);
1259 return;
1263 while (!done && !text->done) {
1264 XSync(dpy, 0);
1265 XAllowEvents(dpy, AsyncKeyboard|AsyncPointer, CurrentTime);
1266 XSync(dpy, 0);
1267 WMNextEvent(dpy, &event);
1269 if (XFindContext(dpy, event.xany.window, wWinContext,
1270 (XPointer *)&desc)==XCNOENT)
1271 desc = NULL;
1273 if ((desc != NULL) && (desc->handle_anything != NULL)) {
1275 (*desc->handle_anything)(desc, &event);
1277 } else {
1278 switch (event.type) {
1279 case ButtonPress:
1280 XAllowEvents(dpy, ReplayPointer, CurrentTime);
1281 done = 1;
1283 default:
1284 WMHandleEvent(&event);
1285 break;
1290 XSetInputFocus(dpy, old_focus, old_revert, CurrentTime);
1292 wSetFocusTo(menu->frame->screen_ptr,
1293 menu->frame->screen_ptr->focused_window);
1296 t = wTextGetText(text);
1297 /* if !t, the user has canceled editing */
1298 if (t) {
1299 if (entry->text)
1300 free(entry->text);
1301 entry->text = wstrdup(t);
1303 menu->flags.realized = 0;
1305 wTextDestroy(text);
1307 XUngrabKeyboard(dpy, CurrentTime);
1309 if (t && menu->on_edit)
1310 (*menu->on_edit)(menu, entry);
1312 menu->flags.editing = 0;
1314 if (!menu->flags.realized)
1315 wMenuRealize(menu);
1319 static void
1320 selectEntry(WMenu *menu, int entry_no)
1322 WMenuEntry *entry;
1323 WMenu *submenu;
1324 int old_entry;
1326 if (menu->entries==NULL)
1327 return;
1329 if (entry_no >= menu->entry_no)
1330 return;
1332 old_entry = menu->selected_entry;
1333 menu->selected_entry = entry_no;
1335 if (old_entry!=entry_no) {
1337 /* unselect previous entry */
1338 if (old_entry>=0) {
1339 paintEntry(menu, old_entry, False);
1340 entry = menu->entries[old_entry];
1342 /* unmap cascade */
1343 if (entry->cascade>=0 && menu->cascades) {
1344 if (!menu->cascades[entry->cascade]->flags.buttoned) {
1345 wMenuUnmap(menu->cascades[entry->cascade]);
1350 if (entry_no<0) {
1351 menu->selected_entry = -1;
1352 return;
1354 entry = menu->entries[entry_no];
1356 if (entry->cascade>=0 && menu->cascades && entry->flags.enabled) {
1357 /* Callback for when the submenu is opened.
1359 submenu = menu->cascades[entry->cascade];
1360 if (submenu && submenu->flags.brother)
1361 submenu = submenu->brother;
1363 if (entry->callback) {
1364 /* Only call the callback if the submenu is not yet mapped.
1366 if (menu->flags.brother) {
1367 if (!submenu || !submenu->flags.mapped)
1368 (*entry->callback)(menu->brother, entry);
1369 } else {
1370 if (!submenu || !submenu->flags.buttoned)
1371 (*entry->callback)(menu, entry);
1375 /* the submenu menu might have changed */
1376 submenu = menu->cascades[entry->cascade];
1378 /* map cascade */
1379 if (!submenu->flags.mapped) {
1380 int x, y;
1382 if (!submenu->flags.realized)
1383 wMenuRealize(submenu);
1384 if (wPreferences.wrap_menus) {
1385 if (menu->flags.open_to_left)
1386 submenu->flags.open_to_left = 1;
1388 if (submenu->flags.open_to_left) {
1389 x = menu->frame_x - MENUW(submenu);
1390 if (x<0) {
1391 x = 0;
1392 submenu->flags.open_to_left = 0;
1394 } else {
1395 x = menu->frame_x + MENUW(menu);
1397 if (x + MENUW(submenu)
1398 >= menu->frame->screen_ptr->scr_width) {
1400 x = menu->frame_x - MENUW(submenu);
1401 submenu->flags.open_to_left = 1;
1404 } else {
1405 x = menu->frame_x + MENUW(menu);
1408 if (wPreferences.align_menus) {
1409 y = menu->frame_y;
1410 } else {
1411 y = menu->frame_y + menu->entry_height*entry_no;
1412 if (menu->flags.titled)
1413 y += menu->frame->top_width;
1414 if (menu->cascades[entry->cascade]->flags.titled)
1415 y -= menu->cascades[entry->cascade]->frame->top_width;
1418 wMenuMapAt(menu->cascades[entry->cascade], x, y, False);
1419 menu->cascades[entry->cascade]->parent = menu;
1420 } else {
1421 return;
1424 paintEntry(menu, entry_no, True);
1429 static WMenu*
1430 findMenu(WScreen *scr, int *x_ret, int *y_ret)
1432 WMenu *menu;
1433 WObjDescriptor *desc;
1434 Window root_ret, win, junk_win;
1435 int x, y, wx, wy;
1436 unsigned int mask;
1438 XQueryPointer(dpy, scr->root_win, &root_ret, &win, &x, &y, &wx, &wy,
1439 &mask);
1441 if (win==None) return NULL;
1443 if (XFindContext(dpy, win, wWinContext, (XPointer *)&desc)==XCNOENT)
1444 return NULL;
1446 if (desc->parent_type == WCLASS_MENU) {
1447 menu = (WMenu*)desc->parent;
1448 XTranslateCoordinates(dpy, root_ret, menu->menu->window, wx, wy,
1449 x_ret, y_ret, &junk_win);
1450 return menu;
1452 return NULL;
1458 static void
1459 closeCascade(WMenu *menu)
1461 WMenu *parent=menu->parent;
1463 if (menu->flags.brother
1464 || (!menu->flags.buttoned
1465 && (!menu->flags.app_menu||menu->parent!=NULL))) {
1467 selectEntry(menu, -1);
1468 XSync(dpy, 0);
1469 #if (MENU_BLINK_DELAY > 2)
1470 wusleep(MENU_BLINK_DELAY/2);
1471 #endif
1472 wMenuUnmap(menu);
1473 while (parent!=NULL
1474 && (parent->parent!=NULL || !parent->flags.app_menu
1475 || parent->flags.brother)
1476 && !parent->flags.buttoned) {
1477 selectEntry(parent, -1);
1478 wMenuUnmap(parent);
1479 parent = parent->parent;
1481 if (parent)
1482 selectEntry(parent, -1);
1487 static void
1488 closeBrotherCascadesOf(WMenu *menu)
1490 WMenu *tmp;
1491 int i;
1493 for (i=0; i<menu->cascade_no; i++) {
1494 if (menu->cascades[i]->flags.brother) {
1495 tmp = menu->cascades[i];
1496 } else {
1497 tmp = menu->cascades[i]->brother;
1499 if (tmp->flags.mapped) {
1500 selectEntry(tmp->parent, -1);
1501 closeBrotherCascadesOf(tmp);
1502 break;
1508 #define getEntryAt(menu, x, y) ((y)<0 ? -1 : (y)/(menu->entry_height))
1511 static WMenu*
1512 parentMenu(WMenu *menu)
1514 WMenu *parent;
1515 WMenuEntry *entry;
1517 if (menu->flags.buttoned)
1518 return menu;
1520 while (menu->parent && menu->parent->flags.mapped) {
1521 parent = menu->parent;
1522 if (parent->selected_entry < 0)
1523 break;
1524 entry = parent->entries[parent->selected_entry];
1525 if (!entry->flags.enabled || entry->cascade<0 || !parent->cascades ||
1526 parent->cascades[entry->cascade] != menu)
1527 break;
1528 menu = parent;
1529 if (menu->flags.buttoned)
1530 break;
1533 return menu;
1539 * Will raise the passed menu, if submenu = 0
1540 * If submenu > 0 will also raise all mapped submenus
1541 * until the first buttoned one
1542 * If submenu < 0 will also raise all mapped parent menus
1543 * until the first buttoned one
1546 static void
1547 raiseMenus(WMenu *menu, int submenus)
1549 WMenu *submenu;
1550 int i;
1552 if(!menu) return;
1554 wRaiseFrame(menu->frame->core);
1556 if (submenus>0 && menu->selected_entry>=0) {
1557 i = menu->entries[menu->selected_entry]->cascade;
1558 if (i>=0 && menu->cascades) {
1559 submenu = menu->cascades[i];
1560 if (submenu->flags.mapped && !submenu->flags.buttoned)
1561 raiseMenus(submenu, submenus);
1564 if (submenus<0 && !menu->flags.buttoned &&
1565 menu->parent && menu->parent->flags.mapped)
1566 raiseMenus(menu->parent, submenus);
1570 WMenu*
1571 wMenuUnderPointer(WScreen *screen)
1573 WObjDescriptor *desc;
1574 Window root_ret, win;
1575 int dummy;
1576 unsigned int mask;
1578 XQueryPointer(dpy, screen->root_win, &root_ret, &win, &dummy, &dummy,
1579 &dummy, &dummy, &mask);
1581 if (win==None) return NULL;
1583 if (XFindContext(dpy, win, wWinContext, (XPointer *)&desc)==XCNOENT)
1584 return NULL;
1586 if (desc->parent_type == WCLASS_MENU)
1587 return (WMenu *)desc->parent;
1588 return NULL;
1593 #define MIN(a,b) (((a) > (b)) ? (b) : (a))
1596 static void
1597 getPointerPosition(WScreen *scr, int *x, int *y)
1599 Window root_ret, win;
1600 int wx, wy;
1601 unsigned int mask;
1603 XQueryPointer(dpy, scr->root_win, &root_ret, &win, x, y, &wx, &wy, &mask);
1607 static void
1608 getScrollAmount(WMenu *menu, int *hamount, int *vamount)
1610 WScreen *scr = menu->menu->screen_ptr;
1611 int menuX1 = menu->frame_x;
1612 int menuY1 = menu->frame_y;
1613 int menuX2 = menu->frame_x + MENUW(menu);
1614 int menuY2 = menu->frame_y + MENUH(menu);
1615 int screenW = scr->scr_width;
1616 int screenH = scr->scr_height;
1617 int xroot, yroot;
1619 *hamount = 0;
1620 *vamount = 0;
1622 getPointerPosition(scr, &xroot, &yroot);
1625 if (xroot <= 1 && menuX1 < 0) {
1626 /* scroll to the right */
1627 *hamount = MIN(MENU_SCROLL_STEP, abs(menuX1));
1629 } else if (xroot >= screenW-2 && menuX2 > screenW-1) {
1630 /* scroll to the left */
1631 *hamount = MIN(MENU_SCROLL_STEP, abs(menuX2-screenW-1));
1633 if (*hamount==0)
1634 *hamount = 1;
1636 *hamount = -*hamount;
1639 if (yroot <= 1 && menuY1 < 0) {
1640 /* scroll down */
1641 *vamount = MIN(MENU_SCROLL_STEP, abs(menuY1));
1643 } else if (yroot >= screenH-2 && menuY2 > screenH-1) {
1644 /* scroll up */
1645 *vamount = MIN(MENU_SCROLL_STEP, abs(menuY2-screenH-2));
1647 *vamount = -*vamount;
1652 static void
1653 dragScrollMenuCallback(void *data)
1655 WMenu *menu = (WMenu*)data;
1656 WScreen *scr = menu->menu->screen_ptr;
1657 WMenu *parent = parentMenu(menu);
1658 int hamount, vamount;
1659 int x, y;
1660 int newSelectedEntry;
1662 getScrollAmount(menu, &hamount, &vamount);
1665 if (hamount != 0 || vamount != 0) {
1666 wMenuMove(parent, parent->frame_x + hamount,
1667 parent->frame_y + vamount, True);
1668 if (findMenu(scr, &x, &y)) {
1669 newSelectedEntry = getEntryAt(menu, x, y);
1670 selectEntry(menu, newSelectedEntry);
1671 } else {
1672 /* Pointer fell outside of menu. If the selected entry is
1673 * not a submenu, unselect it */
1674 if (menu->selected_entry >= 0
1675 && menu->entries[menu->selected_entry]->cascade<0)
1676 selectEntry(menu, -1);
1677 newSelectedEntry = 0;
1680 /* paranoid check */
1681 if (newSelectedEntry >= 0) {
1682 /* keep scrolling */
1683 menu->timer = WMAddTimerHandler(MENU_SCROLL_DELAY,
1684 dragScrollMenuCallback, menu);
1685 } else {
1686 menu->timer = NULL;
1688 } else {
1689 /* don't need to scroll anymore */
1690 menu->timer = NULL;
1691 if (findMenu(scr, &x, &y)) {
1692 newSelectedEntry = getEntryAt(menu, x, y);
1693 selectEntry(menu, newSelectedEntry);
1699 static void
1700 scrollMenuCallback(void *data)
1702 WMenu *menu = (WMenu*)data;
1703 WMenu *parent = parentMenu(menu);
1704 int hamount = 0; /* amount to scroll */
1705 int vamount = 0;
1707 getScrollAmount(menu, &hamount, &vamount);
1709 if (hamount != 0 || vamount != 0) {
1710 wMenuMove(parent, parent->frame_x + hamount,
1711 parent->frame_y + vamount, True);
1713 /* keep scrolling */
1714 menu->timer = WMAddTimerHandler(MENU_SCROLL_DELAY,
1715 scrollMenuCallback, menu);
1716 } else {
1717 /* don't need to scroll anymore */
1718 menu->timer = NULL;
1724 #define MENU_SCROLL_BORDER 5
1726 static int
1727 isPointNearBoder(WMenu *menu, int x, int y)
1729 int menuX1 = menu->frame_x;
1730 int menuY1 = menu->frame_y;
1731 int menuX2 = menu->frame_x + MENUW(menu);
1732 int menuY2 = menu->frame_y + MENUH(menu);
1733 int scrXe = menu->menu->screen_ptr->scr_width-1;
1734 int scrYe = menu->menu->screen_ptr->scr_height-1;
1735 int flag = 0;
1737 if (x >= menuX1 && x <= menuX2 && (y < MENU_SCROLL_BORDER
1738 || y > scrYe-MENU_SCROLL_BORDER))
1739 flag = 1;
1740 else if (y >= menuY1 && y <= menuY2 && (x < MENU_SCROLL_BORDER
1741 || x > scrXe-MENU_SCROLL_BORDER))
1742 flag = 1;
1744 return flag;
1748 typedef struct _delay {
1749 WWindow *wwin;
1750 WMenu *menu;
1751 int ox,oy;
1752 } _delay;
1755 static void
1756 _leaving(_delay *dl)
1758 wMenuMove(dl->menu, dl->ox, dl->oy, True);
1759 dl->menu->jump_back=NULL;
1760 dl->menu->menu->screen_ptr->flags.jump_back_pending = 0;
1761 free(dl);
1765 void
1766 wMenuScroll(WMenu *menu, XEvent *event)
1768 WMenu *smenu;
1769 WMenu *omenu = parentMenu(menu);
1770 WScreen *scr = menu->frame->screen_ptr;
1771 int done = 0;
1772 int jump_back = 0;
1773 int old_frame_x = omenu->frame_x;
1774 int old_frame_y = omenu->frame_y;
1775 XEvent ev;
1777 #ifdef DEBUG
1778 puts("Entering menu Scroll");
1779 #endif
1781 if (omenu->jump_back)
1782 WMDeleteTimerWithClientData(omenu->jump_back);
1785 if ((/*omenu->flags.buttoned &&*/ !wPreferences.wrap_menus)
1786 || omenu->flags.app_menu) {
1787 jump_back = 1;
1790 if (!wPreferences.wrap_menus)
1791 raiseMenus(omenu, True);
1792 else
1793 raiseMenus(menu, False);
1795 if (!menu->timer)
1796 scrollMenuCallback(menu);
1798 while(!done) {
1799 int x, y, on_border, on_x_edge, on_y_edge, on_title;
1801 WMNextEvent(dpy, &ev);
1802 switch (ev.type) {
1803 case EnterNotify:
1804 WMHandleEvent(&ev);
1805 case MotionNotify:
1806 x = (ev.type==MotionNotify) ? ev.xmotion.x_root : ev.xcrossing.x_root;
1807 y = (ev.type==MotionNotify) ? ev.xmotion.y_root : ev.xcrossing.y_root;
1809 /* on_border is != 0 if the pointer is between the menu
1810 * and the screen border and is close enough to the border */
1811 on_border = isPointNearBoder(menu, x, y);
1813 smenu = wMenuUnderPointer(scr);
1815 if ((smenu==NULL && !on_border) || (smenu && parentMenu(smenu)!=omenu)) {
1816 done = 1;
1817 break;
1820 on_x_edge = x <= 1 || x >= scr->scr_width - 2;
1821 on_y_edge = y <= 1 || y >= scr->scr_height - 2;
1822 on_border = on_x_edge || on_y_edge;
1824 if (!on_border && !jump_back) {
1825 done = 1;
1826 break;
1829 if (menu->timer && (smenu!=menu || (!on_y_edge && !on_x_edge))) {
1830 WMDeleteTimerHandler(menu->timer);
1831 menu->timer = NULL;
1834 if (smenu != NULL)
1835 menu = smenu;
1837 if (!menu->timer)
1838 scrollMenuCallback(menu);
1839 break;
1840 case ButtonPress:
1841 /* True if we push on title, or drag the omenu to other position */
1842 on_title = ev.xbutton.x_root >= omenu->frame_x &&
1843 ev.xbutton.x_root <= omenu->frame_x + MENUW(omenu) &&
1844 ev.xbutton.y_root >= omenu->frame_y &&
1845 ev.xbutton.y_root <= omenu->frame_y + omenu->frame->top_width;
1846 WMHandleEvent(&ev);
1847 smenu = wMenuUnderPointer(scr);
1848 if (smenu == NULL || (smenu && smenu->flags.buttoned && smenu != omenu))
1849 done = 1;
1850 else if (smenu==omenu && on_title) {
1851 jump_back = 0;
1852 done = 1;
1854 break;
1855 case KeyPress:
1856 done = 1;
1857 default:
1858 WMHandleEvent(&ev);
1859 break;
1863 if (menu->timer) {
1864 WMDeleteTimerHandler(menu->timer);
1865 menu->timer = NULL;
1868 if (jump_back) {
1869 _delay *delayer;
1870 if (!omenu->jump_back) {
1871 delayer=wmalloc(sizeof(_delay));
1872 delayer->menu=omenu;
1873 delayer->ox=old_frame_x;
1874 delayer->oy=old_frame_y;
1875 omenu->jump_back=delayer;
1876 scr->flags.jump_back_pending = 1;
1878 else delayer = omenu->jump_back;
1879 WMAddTimerHandler(MENU_JUMP_BACK_DELAY,(WMCallback*)_leaving, delayer);
1883 #ifdef DEBUG
1884 puts("Leaving menu Scroll");
1885 #endif
1890 static void
1891 menuExpose(WObjDescriptor *desc, XEvent *event)
1893 wMenuPaint(desc->parent);
1896 typedef struct {
1897 int *delayed_select;
1898 WMenu *menu;
1899 WMHandlerID magic;
1900 } delay_data;
1903 static void
1904 delaySelection(void *data)
1906 delay_data *d = (delay_data*)data;
1907 int x, y, entry_no;
1908 WMenu *menu;
1910 d->magic = NULL;
1912 menu = findMenu(d->menu->menu->screen_ptr, &x, &y);
1913 if (menu && (d->menu == menu || d->delayed_select)) {
1914 entry_no = getEntryAt(menu, x, y);
1915 selectEntry(menu, entry_no);
1917 if (d->delayed_select)
1918 *(d->delayed_select) = 0;
1922 static void
1923 menuMouseDown(WObjDescriptor *desc, XEvent *event)
1925 XButtonEvent *bev = &event->xbutton;
1926 WMenu *menu = desc->parent;
1927 WMenu *smenu;
1928 WScreen *scr=menu->frame->screen_ptr;
1929 WMenuEntry *entry=NULL;
1930 XEvent ev;
1931 int close_on_exit=0;
1932 int done=0;
1933 int delayed_select = 0;
1934 int entry_no;
1935 int x, y;
1936 int prevx, prevy;
1937 int old_frame_x = 0;
1938 int old_frame_y = 0;
1939 delay_data d_data = {NULL, NULL, NULL};
1941 if (!wPreferences.wrap_menus) {
1942 smenu = parentMenu(menu);
1943 old_frame_x = smenu->frame_x;
1944 old_frame_y = smenu->frame_y;
1945 } else if (event->xbutton.window == menu->frame->core->window) {
1946 /* This is true if the menu was launched with right click on root window */
1947 delayed_select = 1;
1948 d_data.delayed_select = &delayed_select;
1949 d_data.menu = menu;
1950 d_data.magic = WMAddTimerHandler(wPreferences.dblclick_time,
1951 delaySelection, &d_data);
1954 wRaiseFrame(menu->frame->core);
1956 close_on_exit = (bev->send_event || menu->flags.brother);
1958 smenu = findMenu(scr, &x, &y);
1959 if (!smenu) {
1960 x = -1;
1961 y = -1;
1962 } else {
1963 menu = smenu;
1966 if (menu->flags.editing) {
1967 return;
1969 entry_no = getEntryAt(menu, x, y);
1970 if (entry_no>=0) {
1971 entry = menu->entries[entry_no];
1973 if (!close_on_exit && (bev->state & ControlMask) && smenu
1974 && entry->flags.editable) {
1975 editEntry(smenu, entry);
1976 return;
1977 } else if (bev->state & ControlMask) {
1978 return;
1981 if (entry->flags.enabled && entry->cascade>=0 && menu->cascades) {
1982 WMenu *submenu = menu->cascades[entry->cascade];
1983 /* map cascade */
1984 if (submenu->flags.mapped && !submenu->flags.buttoned &&
1985 menu->selected_entry!=entry_no) {
1986 wMenuUnmap(submenu);
1988 if (!submenu->flags.mapped && !delayed_select) {
1989 selectEntry(menu, entry_no);
1990 } else if (!submenu->flags.buttoned) {
1991 selectEntry(menu, -1);
1994 } else if (!delayed_select) {
1995 selectEntry(menu, entry_no);
1998 if (!wPreferences.wrap_menus && !wPreferences.scrollable_menus) {
1999 if (!menu->timer)
2000 dragScrollMenuCallback(menu);
2004 prevx = bev->x_root;
2005 prevy = bev->y_root;
2006 while (!done) {
2007 int x, y;
2009 XAllowEvents(dpy, SyncPointer, CurrentTime);
2011 WMMaskEvent(dpy, ExposureMask|ButtonMotionMask|ButtonReleaseMask
2012 |ButtonPressMask, &ev);
2013 switch (ev.type) {
2014 case MotionNotify:
2015 smenu = findMenu(scr, &x, &y);
2017 if (smenu == NULL) {
2018 /* moved mouse out of menu */
2020 if (!delayed_select && d_data.magic) {
2021 WMDeleteTimerHandler(d_data.magic);
2022 d_data.magic = NULL;
2024 if (menu==NULL
2025 || (menu->selected_entry>=0
2026 && menu->entries[menu->selected_entry]->cascade>=0)) {
2027 prevx = ev.xmotion.x_root;
2028 prevy = ev.xmotion.y_root;
2030 break;
2032 selectEntry(menu, -1);
2033 menu = smenu;
2034 prevx = ev.xmotion.x_root;
2035 prevy = ev.xmotion.y_root;
2036 break;
2037 } else if (menu && menu!=smenu
2038 && (menu->selected_entry<0
2039 || menu->entries[menu->selected_entry]->cascade<0)) {
2040 selectEntry(menu, -1);
2042 if (!delayed_select && d_data.magic) {
2043 WMDeleteTimerHandler(d_data.magic);
2044 d_data.magic = NULL;
2046 } else {
2048 /* hysteresis for item selection */
2050 /* check if the motion was to the side, indicating that
2051 * the user may want to cross to a submenu */
2052 if (!delayed_select && menu) {
2053 int dx;
2054 Bool moved_to_submenu;/* moved to direction of submenu */
2056 dx = abs(prevx - ev.xmotion.x_root);
2058 moved_to_submenu = False;
2059 if (dx > 0 /* if moved enough to the side */
2060 /* maybe a open submenu */
2061 && menu->selected_entry>=0
2062 /* moving to the right direction */
2063 && (wPreferences.align_menus
2064 || ev.xmotion.y_root >= prevy)) {
2065 int index;
2067 index = menu->entries[menu->selected_entry]->cascade;
2068 if (index>=0) {
2069 if (menu->cascades[index]->frame_x>menu->frame_x) {
2070 if (prevx < ev.xmotion.x_root)
2071 moved_to_submenu = True;
2072 } else {
2073 if (prevx > ev.xmotion.x_root)
2074 moved_to_submenu = True;
2080 if (menu != smenu) {
2081 if (d_data.magic) {
2082 WMDeleteTimerHandler(d_data.magic);
2084 d_data.magic = NULL;
2085 } else if (moved_to_submenu) {
2086 /* while we are moving, postpone the selection */
2087 if (d_data.magic) {
2088 WMDeleteTimerHandler(d_data.magic);
2090 d_data.delayed_select = NULL;
2091 d_data.menu = menu;
2092 d_data.magic = WMAddTimerHandler(MENU_SELECT_DELAY,
2093 delaySelection,
2094 &d_data);
2095 prevx = ev.xmotion.x_root;
2096 prevy = ev.xmotion.y_root;
2097 break;
2098 } else {
2099 if (d_data.magic)
2100 WMDeleteTimerHandler(d_data.magic);
2101 d_data.magic = NULL;
2105 prevx = ev.xmotion.x_root;
2106 prevy = ev.xmotion.y_root;
2107 if (menu!=smenu) {
2108 /* pointer crossed menus */
2109 if (menu && menu->timer) {
2110 WMDeleteTimerHandler(menu->timer);
2111 menu->timer = NULL;
2113 if (smenu)
2114 dragScrollMenuCallback(smenu);
2116 menu = smenu;
2117 if (!menu->timer)
2118 dragScrollMenuCallback(menu);
2120 if (!delayed_select) {
2121 entry_no = getEntryAt(menu, x, y);
2122 if (entry_no>=0) {
2123 entry = menu->entries[entry_no];
2124 if (entry->flags.enabled && entry->cascade>=0 &&
2125 menu->cascades) {
2126 WMenu *submenu = menu->cascades[entry->cascade];
2127 if (submenu->flags.mapped && !submenu->flags.buttoned
2128 && menu->selected_entry!=entry_no) {
2129 wMenuUnmap(submenu);
2133 selectEntry(menu, entry_no);
2135 break;
2137 case ButtonPress:
2138 break;
2140 case ButtonRelease:
2141 if (ev.xbutton.button == event->xbutton.button)
2142 done=1;
2143 break;
2145 case Expose:
2146 WMHandleEvent(&ev);
2147 break;
2151 if (menu && menu->timer) {
2152 WMDeleteTimerHandler(menu->timer);
2153 menu->timer = NULL;
2155 if (d_data.magic!=NULL)
2156 WMDeleteTimerHandler(d_data.magic);
2158 if (menu && menu->selected_entry>=0) {
2159 entry = menu->entries[menu->selected_entry];
2160 if (entry->callback!=NULL && entry->flags.enabled
2161 && entry->cascade < 0) {
2162 /* blink and erase menu selection */
2163 #if (MENU_BLINK_DELAY > 0)
2164 int sel = menu->selected_entry;
2165 int i;
2167 for (i=0; i<MENU_BLINK_COUNT; i++) {
2168 paintEntry(menu, sel, False);
2169 XSync(dpy, 0);
2170 wusleep(MENU_BLINK_DELAY);
2171 paintEntry(menu, sel, True);
2172 XSync(dpy, 0);
2173 wusleep(MENU_BLINK_DELAY);
2175 #endif
2176 /* unmap the menu, it's parents and call the callback */
2177 if (!menu->flags.buttoned &&
2178 (!menu->flags.app_menu||menu->parent!=NULL)) {
2179 closeCascade(menu);
2180 } else {
2181 selectEntry(menu, -1);
2183 (*entry->callback)(menu, entry);
2185 /* If the user double clicks an entry, the entry will
2186 * be executed twice, which is not good for things like
2187 * the root menu. So, ignore any clicks that were generated
2188 * while the entry was being executed */
2189 while (XCheckTypedWindowEvent(dpy, menu->menu->window,
2190 ButtonPress, &ev));
2191 } else if (entry->callback!=NULL && entry->cascade<0) {
2192 selectEntry(menu, -1);
2193 } else {
2194 if (entry->cascade>=0 && menu->cascades
2195 && menu->cascades[entry->cascade]->flags.brother) {
2196 selectEntry(menu, -1);
2201 if (((WMenu*)desc->parent)->flags.brother || close_on_exit || !smenu)
2202 closeCascade(desc->parent);
2204 /* close the cascade windows that should not remain opened */
2205 closeBrotherCascadesOf(desc->parent);
2207 if (!wPreferences.wrap_menus)
2208 wMenuMove(parentMenu(desc->parent), old_frame_x, old_frame_y, True);
2212 void
2213 wMenuMove(WMenu *menu, int x, int y, int submenus)
2215 WMenu *submenu;
2216 int i;
2218 if (!menu) return;
2220 menu->frame_x = x;
2221 menu->frame_y = y;
2222 XMoveWindow(dpy, menu->frame->core->window, x, y);
2224 if (submenus>0 && menu->selected_entry>=0) {
2225 i = menu->entries[menu->selected_entry]->cascade;
2227 if (i>=0 && menu->cascades) {
2228 submenu = menu->cascades[i];
2229 if (submenu->flags.mapped && !submenu->flags.buttoned)
2230 if (wPreferences.align_menus) {
2231 wMenuMove(submenu, x + MENUW(menu), y, submenus);
2232 } else {
2233 wMenuMove(submenu, x+ MENUW(menu),
2234 y + submenu->entry_height*menu->selected_entry,
2235 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 #ifdef DEBUG
2307 printf("Moving menu\n");
2308 #endif
2310 /* can't touch the menu copy */
2311 if (menu->flags.brother)
2312 return;
2314 if (event->xbutton.button != Button1 && event->xbutton.button != Button2)
2315 return;
2317 if (event->xbutton.state & MOD_MASK) {
2318 wLowerFrame(menu->frame->core);
2319 lower = 1;
2320 } else {
2321 wRaiseFrame(menu->frame->core);
2322 lower = 0;
2324 tmp = menu;
2326 /* lower/raise all submenus */
2327 while (1) {
2328 if (tmp->selected_entry>=0 && tmp->cascades
2329 && tmp->entries[tmp->selected_entry]->cascade>=0) {
2330 tmp = tmp->cascades[tmp->entries[tmp->selected_entry]->cascade];
2331 if (!tmp || !tmp->flags.mapped)
2332 break;
2333 if (lower)
2334 wLowerFrame(tmp->frame->core);
2335 else
2336 wRaiseFrame(tmp->frame->core);
2337 } else {
2338 break;
2342 /* tear off the menu if it's a root menu or a cascade
2343 application menu */
2344 if (!menu->flags.buttoned && !menu->flags.brother
2345 && (!menu->flags.app_menu||menu->parent!=NULL)) {
2346 menu->flags.buttoned=1;
2347 wFrameWindowShowButton(menu->frame, WFF_RIGHT_BUTTON);
2348 if (menu->parent) {
2349 /* turn off selected menu entry in parent menu */
2350 selectEntry(menu->parent, -1);
2352 /* make parent map the copy in place of the original */
2353 for (i=0; i<menu->parent->cascade_no; i++) {
2354 if (menu->parent->cascades[i] == menu) {
2355 menu->parent->cascades[i] = menu->brother;
2356 break;
2362 started = False;
2363 while(1) {
2364 WMMaskEvent(dpy, ButtonMotionMask|ButtonReleaseMask|ButtonPressMask
2365 |ExposureMask, &ev);
2366 switch (ev.type) {
2367 case MotionNotify:
2368 if (started) {
2369 x += ev.xmotion.x_root - dx;
2370 y += ev.xmotion.y_root - dy;
2371 dx = ev.xmotion.x_root;
2372 dy = ev.xmotion.y_root;
2373 wMenuMove(menu, x, y, True);
2374 } else {
2375 if (abs(ev.xmotion.x_root - dx) > MOVE_THRESHOLD
2376 || abs(ev.xmotion.y_root - dy) > MOVE_THRESHOLD) {
2377 started = True;
2378 XGrabPointer(dpy, menu->frame->titlebar->window, False,
2379 ButtonMotionMask|ButtonReleaseMask
2380 |ButtonPressMask,
2381 GrabModeAsync, GrabModeAsync, None,
2382 wCursor[WCUR_MOVE], CurrentTime);
2385 break;
2387 case ButtonPress:
2388 break;
2390 case ButtonRelease:
2391 if (ev.xbutton.button != event->xbutton.button)
2392 break;
2393 #ifdef DEBUG
2394 printf("End menu move\n");
2395 #endif
2396 XUngrabPointer(dpy, CurrentTime);
2397 return;
2399 default:
2400 WMHandleEvent(&ev);
2401 break;
2407 *----------------------------------------------------------------------
2408 * menuCloseClick--
2409 * Handles mouse click on the close button of menus. The menu is
2410 * closed when the button is clicked.
2412 * Side effects:
2413 * The closed menu is reinserted at it's parent menus
2414 * cascade list.
2415 *----------------------------------------------------------------------
2417 static void
2418 menuCloseClick(WCoreWindow *sender, void *data, XEvent *event)
2420 WMenu *menu = (WMenu*)data;
2421 WMenu *parent = menu->parent;
2422 int i;
2424 if (parent) {
2425 for (i=0; i<parent->cascade_no; i++) {
2426 /* find the entry that points to the copy */
2427 if (parent->cascades[i] == menu->brother) {
2428 /* make it point to the original */
2429 parent->cascades[i] = menu;
2430 menu->parent = parent;
2431 break;
2435 wMenuUnmap(menu);
2439 void
2440 wMenuSaveState(WScreen *scr)
2442 proplist_t menus, key, value;
2443 int save_menus = 0;
2445 char buffer[256];
2447 menus = PLMakeDictionaryFromEntries(NULL, NULL, NULL);
2449 #ifndef LITE
2450 if (scr->switch_menu && scr->switch_menu->flags.buttoned) {
2451 sprintf(buffer, "%i,%i", scr->switch_menu->frame_x,
2452 scr->switch_menu->frame_y);
2453 key = PLMakeString("SwitchMenu");
2454 value = PLMakeString(buffer);
2455 PLInsertDictionaryEntry(menus, key, value);
2456 PLRelease(key);
2457 PLRelease(value);
2458 save_menus = 1;
2461 if (saveMenuRecurs(menus, scr, scr->root_menu))
2462 save_menus = 1;
2464 #endif /* !LITE */
2465 if (scr->workspace_menu && scr->workspace_menu->flags.buttoned) {
2466 sprintf(buffer, "%i,%i", scr->workspace_menu->frame_x,
2467 scr->workspace_menu->frame_y);
2468 key = PLMakeString("WorkspaceMenu");
2469 value = PLMakeString(buffer);
2470 PLInsertDictionaryEntry(menus, key, value);
2471 PLRelease(key);
2472 PLRelease(value);
2473 save_menus = 1;
2476 if (save_menus) {
2477 key = PLMakeString("Menus");
2478 PLInsertDictionaryEntry(scr->session_state, key, menus);
2479 PLRelease(key);
2481 PLRelease(menus);
2485 #ifndef LITE
2487 static Bool
2488 getMenuPath(WMenu *menu, char *buffer, int bufSize)
2490 Bool ok = True;
2491 int len = 0;
2493 if (!menu->flags.titled || !menu->frame->title[0])
2494 return False;
2496 len = strlen(menu->frame->title);
2497 if (len >= bufSize)
2498 return False;
2500 if (menu->parent) {
2501 ok = getMenuPath(menu->parent, buffer, bufSize - len - 1);
2502 if (!ok)
2503 return False;
2506 strcat(buffer, "\\");
2507 strcat(buffer, menu->frame->title);
2509 return True;
2512 static Bool
2513 saveMenuRecurs(proplist_t menus, WScreen *scr, WMenu *menu)
2515 proplist_t key, value;
2516 int save_menus = 0, i;
2517 char buffer[512];
2518 Bool ok = True;
2521 if (menu->flags.brother)
2522 menu = menu->brother;
2524 if (menu->flags.buttoned && menu != scr->switch_menu) {
2526 sprintf(buffer, "%i,%i", menu->frame_x, menu->frame_y);
2527 value = PLMakeString(buffer);
2529 buffer[0] = '\0';
2530 ok = getMenuPath(menu, buffer, 510);
2532 if (ok) {
2533 key = PLMakeString(buffer);
2534 PLInsertDictionaryEntry(menus, key, value);
2535 PLRelease(key);
2536 PLRelease(value);
2537 save_menus = 1;
2541 if (ok) {
2542 for (i = 0; i < menu->cascade_no; i++) {
2543 if (saveMenuRecurs(menus, scr, menu->cascades[i]))
2544 save_menus = 1;
2547 return save_menus;
2549 #endif /* !LITE */
2552 #define COMPLAIN(key) wwarning(_("bad value in menus state info:%s"), key)
2555 static int
2556 restoreMenu(WScreen *scr, proplist_t menu, int which)
2558 int x, y;
2559 WMenu *pmenu = NULL;
2561 if (!menu)
2562 return False;
2564 if (!PLIsString(menu)) {
2565 COMPLAIN("Position");
2566 return False;
2569 if (sscanf(PLGetString(menu), "%i,%i", &x, &y)!=2)
2570 COMPLAIN("Position");
2572 #ifndef LITE
2573 if (which & WSS_SWITCHMENU) {
2574 OpenSwitchMenu(scr, x, y, False);
2575 pmenu = scr->switch_menu;
2577 #endif /* !LITE */
2579 if (pmenu) {
2580 int width = MENUW(pmenu);
2581 int height = MENUH(pmenu);
2583 x = (x < -width) ? 0 : x;
2584 x = (x > scr->scr_width) ? scr->scr_width - width : x;
2585 y = (y < 0) ? 0 : y;
2586 y = (y > scr->scr_height) ? scr->scr_height - height : y;
2587 wMenuMove(pmenu, x, y, True);
2588 pmenu->flags.buttoned = 1;
2589 wFrameWindowShowButton(pmenu->frame, WFF_RIGHT_BUTTON);
2590 return True;
2592 return False;
2596 #ifndef LITE
2597 static int
2598 restoreMenuRecurs(WScreen *scr, proplist_t menus, WMenu *menu, char *path)
2600 proplist_t key, entry;
2601 char buffer[512];
2602 int i, x, y, res;
2604 if (strlen(path) + strlen(menu->frame->title) > 510)
2605 return False;
2607 sprintf(buffer, "%s\\%s", path, menu->frame->title);
2608 key = PLMakeString(buffer);
2609 entry = PLGetDictionaryEntry(menus, key);
2610 res = False;
2612 if (entry && PLIsString(entry)) {
2614 if (sscanf(PLGetString(entry), "%i,%i", &x, &y) != 2)
2615 COMPLAIN("Position");
2617 if (!menu->flags.mapped) {
2618 int width = MENUW(menu);
2619 int height = MENUH(menu);
2621 wMenuMapAt(menu, x, y, False);
2623 if (menu->parent) {
2624 /* make parent map the copy in place of the original */
2625 for (i=0; i<menu->parent->cascade_no; i++) {
2626 if (menu->parent->cascades[i] == menu) {
2627 menu->parent->cascades[i] = menu->brother;
2628 break;
2632 x = (x < -width) ? 0 : x;
2633 x = (x > scr->scr_width) ? scr->scr_width - width : x;
2634 y = (y < 0) ? 0 : y;
2635 y = (y > scr->scr_height) ? scr->scr_height - height : y;
2636 wMenuMove(menu, x, y, True);
2637 menu->flags.buttoned = 1;
2638 wFrameWindowShowButton(menu->frame, WFF_RIGHT_BUTTON);
2639 res = True;
2643 PLRelease(key);
2645 for (i=0; i<menu->cascade_no; i++) {
2646 if (restoreMenuRecurs(scr, menus, menu->cascades[i], buffer) != False)
2647 res = True;
2650 return res;
2652 #endif /* !LITE */
2655 void
2656 wMenuRestoreState(WScreen *scr)
2658 proplist_t menus, menu, key, skey;
2660 key = PLMakeString("Menus");
2661 menus = PLGetDictionaryEntry(scr->session_state, key);
2662 PLRelease(key);
2664 if (!menus)
2665 return;
2667 /* restore menus */
2669 skey = PLMakeString("SwitchMenu");
2670 menu = PLGetDictionaryEntry(menus, skey);
2671 restoreMenu(scr, menu, WSS_SWITCHMENU);
2673 #ifndef LITE
2674 if (!scr->root_menu) {
2675 OpenRootMenu(scr, scr->scr_width*2, 0, False);
2676 wMenuUnmap(scr->root_menu);
2678 restoreMenuRecurs(scr, menus, scr->root_menu, "");
2679 #endif /* !LITE */
2681 PLRelease(skey);
2685 void
2686 OpenWorkspaceMenu(WScreen *scr, int x, int y)
2688 WMenu *menu, *parent;
2689 WMenuEntry *entry;
2691 #ifndef LITE
2692 if (!scr->root_menu) {
2693 OpenRootMenu(scr, scr->scr_width*2, 0, False);
2694 wMenuUnmap(scr->root_menu);
2696 #endif
2697 menu = scr->workspace_menu;
2698 if (menu) {
2699 if (menu->flags.mapped) {
2700 if (!menu->flags.buttoned) {
2701 wMenuUnmap(menu);
2702 parent = menu->parent;
2703 if (parent && parent->selected_entry >= 0) {
2704 entry = parent->entries[parent->selected_entry];
2705 if (parent->cascades[entry->cascade] == menu) {
2706 selectEntry(parent, -1);
2707 wMenuMapAt(menu, x, y, False);
2710 } else {
2711 wRaiseFrame(menu->frame->core);
2712 wMenuMapCopyAt(menu, x, y);
2714 } else {
2715 wMenuMapAt(menu, x, y, False);