bug fixes, performance enhancement for image rendering
[wmaker-crm.git] / src / menu.c
blob3b45c8b13ac4de260aed5de571b53ec127a14edb
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);
88 static void selectEntry(WMenu *menu, int entry_no);
89 static void closeCascade(WMenu *menu);
92 /****** Notification Observers ******/
94 static void
95 appearanceObserver(void *self, WMNotification *notif)
97 WMenu *menu = (WMenu*)self;
98 int flags = (int)WMGetNotificationClientData(notif);
100 if (!menu->flags.realized)
101 return;
103 if (WMGetNotificationName(notif) == WNMenuAppearanceSettingsChanged) {
104 if (flags & WFontSettings) {
105 menu->flags.realized = 0;
106 wMenuRealize(menu);
108 if (flags & WTextureSettings) {
109 updateTexture(menu);
111 if (flags & (WTextureSettings|WColorSettings)) {
112 wMenuPaint(menu);
114 } else if (menu->flags.titled) {
116 if (flags & WFontSettings) {
117 menu->flags.realized = 0;
118 wMenuRealize(menu);
120 if (flags & WTextureSettings) {
121 menu->frame->flags.need_texture1_remake = 1;
123 if (flags & (WColorSettings|WTextureSettings))
124 wFrameWindowPaint(menu->frame);
128 /************************************/
132 *----------------------------------------------------------------------
133 * wMenuCreate--
134 * Creates a new empty menu with the specified title. If main_menu
135 * is True, the created menu will be a main menu, which has some special
136 * properties such as being placed over other normal menus.
137 * If title is NULL, the menu will have no titlebar.
139 * Returns:
140 * The created menu.
141 *----------------------------------------------------------------------
143 WMenu*
144 wMenuCreate(WScreen *screen, char *title, int main_menu)
146 WMenu *menu;
147 static int brother=0;
148 int tmp, flags;
150 menu = wmalloc(sizeof(WMenu));
152 memset(menu, 0, sizeof(WMenu));
154 #ifdef SINGLE_MENULEVEL
155 tmp = WMSubmenuLevel;
156 #else
157 tmp = (main_menu ? WMMainMenuLevel : WMSubmenuLevel);
158 #endif
160 flags = WFF_SINGLE_STATE;
161 if (title) {
162 flags |= WFF_TITLEBAR|WFF_RIGHT_BUTTON;
163 menu->flags.titled = 1;
165 menu->frame =
166 wFrameWindowCreate(screen, tmp, 8, 2, 1, 1, flags,
167 screen->menu_title_texture, NULL,
168 screen->menu_title_pixel, &screen->menu_title_gc,
169 &screen->menu_title_font);
171 menu->frame->core->descriptor.parent = menu;
172 menu->frame->core->descriptor.parent_type = WCLASS_MENU;
173 menu->frame->core->descriptor.handle_mousedown = menuMouseDown;
175 wFrameWindowHideButton(menu->frame, WFF_RIGHT_BUTTON);
177 if (title) {
178 menu->frame->title = wstrdup(title);
181 menu->frame->flags.justification = WTJ_LEFT;
183 menu->frame->rbutton_image = screen->b_pixmaps[WBUT_CLOSE];
185 menu->entry_no = 0;
186 menu->alloced_entries = 0;
187 menu->selected_entry = -1;
188 menu->entries = NULL;
190 menu->frame_x = screen->app_menu_x;
191 menu->frame_y = screen->app_menu_y;
193 menu->frame->child = menu;
195 menu->flags.lowered = 0;
197 /* create borders */
198 if (title) {
199 /* setup object descriptors */
200 menu->frame->on_mousedown_titlebar = menuTitleMouseDown;
201 menu->frame->on_dblclick_titlebar = menuTitleDoubleClick;
204 menu->frame->on_click_right = menuCloseClick;
207 menu->menu = wCoreCreate(menu->frame->core, 0, menu->frame->top_width,
208 menu->frame->core->width, 10);
210 menu->menu->descriptor.parent = menu;
211 menu->menu->descriptor.parent_type = WCLASS_MENU;
212 menu->menu->descriptor.handle_expose = menuExpose;
213 menu->menu->descriptor.handle_mousedown = menuMouseDown;
215 menu->menu_texture_data = None;
217 XMapWindow(dpy, menu->menu->window);
219 XFlush(dpy);
221 if (!brother) {
222 brother = 1;
223 menu->brother = wMenuCreate(screen, title, main_menu);
224 brother = 0;
225 menu->brother->flags.brother = 1;
226 menu->brother->brother = menu;
228 WMAddNotificationObserver(appearanceObserver, menu,
229 WNMenuTitleAppearanceSettingsChanged, menu);
231 WMAddNotificationObserver(appearanceObserver, menu,
232 WNMenuAppearanceSettingsChanged, menu);
235 return menu;
241 WMenu*
242 wMenuCreateForApp(WScreen *screen, char *title, int main_menu)
244 WMenu *menu;
246 menu = wMenuCreate(screen, title, main_menu);
247 if (!menu)
248 return NULL;
249 menu->flags.app_menu = 1;
250 menu->brother->flags.app_menu = 1;
252 return menu;
257 static void
258 insertEntry(WMenu *menu, WMenuEntry *entry, int index)
260 int i;
262 for (i = menu->entry_no-1; i >= index; i--) {
263 menu->entries[i]->order++;
264 menu->entries[i+1] = menu->entries[i];
266 menu->entries[index] = entry;
270 WMenuEntry*
271 wMenuInsertCallback(WMenu *menu, int index, char *text,
272 void (*callback)(WMenu *menu, WMenuEntry *entry),
273 void *clientdata)
275 WMenuEntry *entry;
277 #ifdef DEBUG
278 if (!menu) {
279 printf("Passed NULL as menu parameter to wMenuAddCallback() \n");
280 return NULL;
282 #endif
284 assert(menu->flags.brother==0);
285 menu->flags.realized = 0;
286 menu->brother->flags.realized = 0;
288 /* reallocate array if it's too small */
289 if (menu->entry_no >= menu->alloced_entries) {
290 void *tmp;
291 #ifdef DEBUG
292 puts("doing wrealloc()");
293 #endif
294 tmp = wrealloc(menu->entries,
295 sizeof(WMenuEntry)*(menu->alloced_entries+5));
296 if (tmp==NULL) {
297 wwarning(_("wrealloc() failed while trying to add menu item"));
298 return NULL;
301 menu->entries = tmp;
302 menu->alloced_entries += 5;
304 menu->brother->entries = tmp;
305 menu->brother->alloced_entries = menu->alloced_entries;
307 entry = wmalloc(sizeof(WMenuEntry));
308 memset(entry, 0, sizeof(WMenuEntry));
309 entry->flags.enabled = 1;
310 entry->text = wstrdup(text);
311 entry->cascade = -1;
312 entry->clientdata = clientdata;
313 entry->callback = callback;
314 if (index<0 || index>=menu->entry_no) {
315 entry->order = menu->entry_no;
316 menu->entries[menu->entry_no] = entry;
317 } else {
318 entry->order = index;
319 insertEntry(menu, entry, index);
322 menu->entry_no++;
323 menu->brother->entry_no = menu->entry_no;
325 return entry;
330 void
331 wMenuEntrySetCascade(WMenu *menu, WMenuEntry *entry, WMenu *cascade)
333 WMenu *brother = menu->brother;
334 int i, done;
336 assert(menu->flags.brother==0);
338 if (entry->cascade>=0) {
339 menu->flags.realized = 0;
340 brother->flags.realized = 0;
343 cascade->parent = menu;
345 cascade->brother->parent = brother;
347 done = 0;
348 for (i=0; i<menu->cascade_no; i++) {
349 if (menu->cascades[i]==NULL) {
350 menu->cascades[i] = cascade;
351 brother->cascades[i] = cascade->brother;
352 done = 1;
353 entry->cascade = i;
354 break;
357 if (!done) {
358 entry->cascade = menu->cascade_no;
360 menu->cascades = wrealloc(menu->cascades,
361 sizeof(WMenu)*(menu->cascade_no+1));
362 menu->cascades[menu->cascade_no++] = cascade;
365 brother->cascades = wrealloc(brother->cascades,
366 sizeof(WMenu)*(brother->cascade_no+1));
367 brother->cascades[brother->cascade_no++] = cascade->brother;
371 if (menu->flags.lowered) {
373 cascade->flags.lowered = 1;
374 ChangeStackingLevel(cascade->frame->core, WMNormalLevel);
376 cascade->brother->flags.lowered = 1;
377 ChangeStackingLevel(cascade->brother->frame->core, WMNormalLevel);
380 if (!menu->flags.realized)
381 wMenuRealize(menu);
385 void
386 wMenuEntryRemoveCascade(WMenu *menu, WMenuEntry *entry)
388 assert(menu->flags.brother==0);
390 /* destroy cascade menu */
391 if (entry->cascade>=0 && menu->cascades
392 && menu->cascades[entry->cascade]!=NULL) {
394 wMenuDestroy(menu->cascades[entry->cascade], True);
396 menu->cascades[entry->cascade] = NULL;
397 menu->brother->cascades[entry->cascade] = NULL;
399 entry->cascade = -1;
404 void
405 wMenuRemoveItem(WMenu *menu, int index)
407 int i;
409 if (menu->flags.brother) {
410 wMenuRemoveItem(menu->brother, index);
411 return;
414 if (index>=menu->entry_no) return;
416 /* destroy cascade menu */
417 wMenuEntryRemoveCascade(menu, menu->entries[index]);
419 /* destroy unshared data */
421 if (menu->entries[index]->text)
422 free(menu->entries[index]->text);
424 if (menu->entries[index]->rtext)
425 free(menu->entries[index]->rtext);
427 if (menu->entries[index]->free_cdata && menu->entries[index]->clientdata)
428 (*menu->entries[index]->free_cdata)(menu->entries[index]->clientdata);
430 free(menu->entries[index]);
432 for (i=index; i<menu->entry_no-1; i++) {
433 menu->entries[i+1]->order--;
434 menu->entries[i]=menu->entries[i+1];
436 menu->entry_no--;
437 menu->brother->entry_no--;
441 static Pixmap
442 renderTexture(WMenu *menu)
444 RImage *img;
445 Pixmap pix;
446 int i;
447 RColor light;
448 RColor dark;
449 RColor mid;
450 WScreen *scr = menu->menu->screen_ptr;
451 WTexture *texture = scr->menu_item_texture;
453 if (wPreferences.menu_style == MS_NORMAL) {
454 img = wTextureRenderImage(texture, menu->menu->width,
455 menu->entry_height, WREL_MENUENTRY);
456 } else {
457 img = wTextureRenderImage(texture, menu->menu->width,
458 menu->menu->height+1, WREL_MENUENTRY);
460 if (!img) {
461 wwarning(_("could not render texture: %s"),
462 RMessageForError(RErrorCode));
464 return None;
467 if (wPreferences.menu_style == MS_SINGLE_TEXTURE) {
468 light.alpha = 0;
469 light.red = light.green = light.blue = 80;
471 dark.alpha = 255;
472 dark.red = dark.green = dark.blue = 0;
474 mid.alpha = 0;
475 mid.red = mid.green = mid.blue = 40;
477 for (i = 1; i < menu->entry_no; i++) {
478 ROperateLine(img, RSubtractOperation, 0, i*menu->entry_height-2,
479 menu->menu->width-1, i*menu->entry_height-2, &mid);
481 RDrawLine(img, 0, i*menu->entry_height-1,
482 menu->menu->width-1, i*menu->entry_height-1, &dark);
484 ROperateLine(img, RAddOperation, 0, i*menu->entry_height,
485 menu->menu->width-1, i*menu->entry_height,
486 &light);
489 if (!RConvertImage(scr->rcontext, img, &pix)) {
490 wwarning(_("error rendering image:%s"), RMessageForError(RErrorCode));
492 RDestroyImage(img);
494 return pix;
498 static void
499 updateTexture(WMenu *menu)
501 WScreen *scr = menu->menu->screen_ptr;
503 /* setup background texture */
504 if (scr->menu_item_texture->any.type != WTEX_SOLID) {
505 if (!menu->flags.brother) {
506 FREE_PIXMAP(menu->menu_texture_data);
508 menu->menu_texture_data = renderTexture(menu);
510 XSetWindowBackgroundPixmap(dpy, menu->menu->window,
511 menu->menu_texture_data);
512 XClearWindow(dpy, menu->menu->window);
514 XSetWindowBackgroundPixmap(dpy, menu->brother->menu->window,
515 menu->menu_texture_data);
516 XClearWindow(dpy, menu->brother->menu->window);
518 } else {
519 XSetWindowBackground(dpy, menu->menu->window,
520 scr->menu_item_texture->any.color.pixel);
521 XClearWindow(dpy, menu->menu->window);
526 void
527 wMenuRealize(WMenu *menu)
529 int i;
530 int width, rwidth, mrwidth, mwidth;
531 int theight, twidth, eheight;
532 WScreen *scr = menu->frame->screen_ptr;
533 static int brother_done=0;
534 int flags;
536 if (!brother_done) {
537 brother_done = 1;
538 wMenuRealize(menu->brother);
539 brother_done = 0;
542 flags = WFF_SINGLE_STATE;
543 if (menu->flags.titled)
544 flags |= WFF_TITLEBAR|WFF_RIGHT_BUTTON;
546 wFrameWindowUpdateBorders(menu->frame, flags);
548 if (menu->flags.titled) {
549 twidth = wTextWidth(scr->menu_title_font->font, menu->frame->title,
550 strlen(menu->frame->title));
551 theight = menu->frame->top_width;
552 twidth += theight + (wPreferences.new_style ? 16 : 8);
553 } else {
554 twidth = 0;
555 theight = 0;
557 eheight = scr->menu_entry_font->height + 6;
558 menu->entry_height = eheight;
559 mrwidth = 0;
560 mwidth = 0;
561 for (i=0; i<menu->entry_no; i++) {
562 char *text;
564 /* search widest text */
565 text = menu->entries[i]->text;
566 width = wTextWidth(scr->menu_entry_font->font, text, strlen(text))+10;
568 if (menu->entries[i]->flags.indicator) {
569 width += MENU_INDICATOR_SPACE;
572 if (width > mwidth)
573 mwidth = width;
575 /* search widest text on right */
576 text = menu->entries[i]->rtext;
577 if (text)
578 rwidth = wTextWidth(scr->menu_entry_font->font, text,
579 strlen(text)) + 5;
580 else if (menu->entries[i]->cascade>=0)
581 rwidth = 16;
582 else
583 rwidth = 4;
585 if (rwidth > mrwidth)
586 mrwidth = rwidth;
588 mwidth += mrwidth;
590 if (mwidth < twidth)
591 mwidth = twidth;
594 wCoreConfigure(menu->menu, 0, theight, mwidth, menu->entry_no*eheight -1);
596 wFrameWindowResize(menu->frame, mwidth, menu->entry_no*eheight-1
597 + menu->frame->top_width + menu->frame->bottom_width);
600 updateTexture(menu);
602 menu->flags.realized = 1;
604 if (menu->flags.mapped)
605 wMenuPaint(menu);
606 if (menu->brother->flags.mapped)
607 wMenuPaint(menu->brother);
611 void
612 wMenuDestroy(WMenu *menu, int recurse)
614 int i;
616 WMRemoveNotificationObserver(menu);
618 /* remove any pending timers */
619 if (menu->timer)
620 WMDeleteTimerHandler(menu->timer);
621 menu->timer = NULL;
623 /* call destroy handler */
624 if (menu->on_destroy)
625 (*menu->on_destroy)(menu);
627 /* Destroy items if this menu own them. If this is the "brother" menu,
628 * leave them alone as it is shared by them.
630 if (!menu->flags.brother) {
631 for (i=0; i<menu->entry_no; i++) {
633 free(menu->entries[i]->text);
635 if (menu->entries[i]->rtext)
636 free(menu->entries[i]->rtext);
637 #ifdef USER_MENU
639 if (menu->entries[i]->instances){
640 PLRelease(menu->entries[i]->instances);
642 #endif /* USER_MENU */
644 if (menu->entries[i]->free_cdata && menu->entries[i]->clientdata) {
645 (*menu->entries[i]->free_cdata)(menu->entries[i]->clientdata);
647 free(menu->entries[i]);
650 if (recurse) {
651 for (i=0; i<menu->cascade_no; i++) {
652 if (menu->cascades[i]) {
653 if (menu->cascades[i]->flags.brother)
654 wMenuDestroy(menu->cascades[i]->brother, recurse);
655 else
656 wMenuDestroy(menu->cascades[i], recurse);
661 if (menu->entries)
662 free(menu->entries);
666 FREE_PIXMAP(menu->menu_texture_data);
668 if (menu->cascades)
669 free(menu->cascades);
671 wCoreDestroy(menu->menu);
672 wFrameWindowDestroy(menu->frame);
674 /* destroy copy of this menu */
675 if (!menu->flags.brother && menu->brother)
676 wMenuDestroy(menu->brother, False);
678 free(menu);
682 #define F_NORMAL 0
683 #define F_TOP 1
684 #define F_BOTTOM 2
685 #define F_NONE 3
687 static void
688 drawFrame(WScreen *scr, Window win, int y, int w, int h, int type)
690 XSegment segs[2];
691 int i;
693 i = 0;
694 segs[i].x1 = segs[i].x2 = w-1;
695 segs[i].y1 = y;
696 segs[i].y2 = y + h - 1;
697 i++;
698 if (type != F_TOP && type != F_NONE) {
699 segs[i].x1 = 1;
700 segs[i].y1 = segs[i].y2 = y + h-2;
701 segs[i].x2 = w-1;
702 i++;
704 XDrawSegments(dpy, win, scr->menu_item_auxtexture->dim_gc, segs, i);
706 i = 0;
707 segs[i].x1 = 0;
708 segs[i].y1 = y;
709 segs[i].x2 = 0;
710 segs[i].y2 = y + h - 1;
711 i++;
712 if (type != F_BOTTOM && type != F_NONE) {
713 segs[i].x1 = 0;
714 segs[i].y1 = y;
715 segs[i].x2 = w-1;
716 segs[i].y2 = y;
717 i++;
719 XDrawSegments(dpy, win, scr->menu_item_auxtexture->light_gc, segs, i);
721 if (type != F_TOP && type != F_NONE)
722 XDrawLine(dpy, win, scr->menu_item_auxtexture->dark_gc, 0, y+h-1,
723 w-1, y+h-1);
727 static void
728 paintEntry(WMenu *menu, int index, int selected)
730 int x, y, w, h, tw;
731 int type;
732 GC light, dim, dark, textGC;
733 WScreen *scr=menu->frame->screen_ptr;
734 Window win = menu->menu->window;
735 WMenuEntry *entry=menu->entries[index];
737 if (!menu->flags.realized) return;
738 h = menu->entry_height;
739 w = menu->menu->width;
740 y = index * h;
742 light = scr->menu_item_auxtexture->light_gc;
743 dim = scr->menu_item_auxtexture->dim_gc;
744 dark = scr->menu_item_auxtexture->dark_gc;
746 if (wPreferences.menu_style == MS_FLAT && menu->entry_no > 1) {
747 if (index == 0)
748 type = F_TOP;
749 else if (index == menu->entry_no - 1)
750 type = F_BOTTOM;
751 else
752 type = F_NONE;
753 } else {
754 type = F_NORMAL;
757 /* paint background */
758 if (selected) {
759 XSetForeground(dpy, scr->select_menu_gc, scr->select_pixel);
760 XFillRectangle(dpy, win, scr->select_menu_gc, 1, y+1, w-2, h-3);
761 if (scr->menu_item_texture->any.type == WTEX_SOLID)
762 drawFrame(scr, win, y, w, h, type);
763 } else {
764 if (scr->menu_item_texture->any.type == WTEX_SOLID) {
765 XClearArea(dpy, win, 0, y + 1, w - 1, h - 3, False);
766 /* draw the frame */
767 drawFrame(scr, win, y, w, h, type);
768 } else {
769 XClearArea(dpy, win, 0, y, w, h, False);
773 if (selected) {
774 textGC = scr->select_menu_gc;
775 if (entry->flags.enabled)
776 XSetForeground(dpy, textGC, scr->select_text_pixel);
777 else
778 XSetForeground(dpy, textGC, scr->dtext_pixel);
779 } else if (!entry->flags.enabled) {
780 textGC = scr->disabled_menu_entry_gc;
781 } else {
782 textGC = scr->menu_entry_gc;
784 /* draw text */
785 x = 5;
786 if (entry->flags.indicator)
787 x += MENU_INDICATOR_SPACE + 2;
789 wDrawString(win, scr->menu_entry_font,
790 textGC, x, 3+y+scr->menu_entry_font->y, entry->text,
791 strlen(entry->text));
793 if (entry->cascade>=0) {
794 /* draw the cascade indicator */
795 XDrawLine(dpy,win,dim, w-11, y+6, w-6, y+h/2-1);
796 XDrawLine(dpy,win,light, w-11, y+h-8, w-6, y+h/2-1);
797 XDrawLine(dpy,win,dark, w-12, y+6, w-12, y+h-8);
800 /* draw indicator */
801 if (entry->flags.indicator && entry->flags.indicator_on) {
802 int iw, ih;
803 WPixmap *indicator;
806 switch (entry->flags.indicator_type) {
807 case MI_CHECK:
808 indicator = scr->menu_check_indicator;
809 break;
810 case MI_MINIWINDOW:
811 indicator = scr->menu_mini_indicator;
812 break;
813 case MI_HIDDEN:
814 indicator = scr->menu_hide_indicator;
815 break;
816 case MI_SHADED:
817 indicator = scr->menu_shade_indicator;
818 break;
819 case MI_DIAMOND:
820 default:
821 indicator = scr->menu_radio_indicator;
822 break;
825 iw = indicator->width;
826 ih = indicator->height;
827 XSetClipMask(dpy, scr->copy_gc, indicator->mask);
828 XSetClipOrigin(dpy, scr->copy_gc, 5, y+(h-ih)/2);
829 if (selected)
830 XSetForeground(dpy, scr->copy_gc, scr->black_pixel);
831 else
832 XSetForeground(dpy, scr->copy_gc, scr->mtext_pixel);
833 XFillRectangle(dpy, win, scr->copy_gc, 5, y+(h-ih)/2, iw, ih);
835 XCopyArea(dpy, indicator->image, win, scr->copy_gc, 0, 0,
836 iw, ih, 5, y+(h-ih)/2);
838 XSetClipOrigin(dpy, scr->copy_gc, 0, 0);
841 /* draw right text */
843 if (entry->rtext && entry->cascade<0) {
844 tw = wTextWidth(scr->menu_entry_font->font, entry->rtext,
845 strlen(entry->rtext));
847 wDrawString(win, scr->menu_entry_font, textGC, w-6-tw,
848 3+y+scr->menu_entry_font->y, entry->rtext,
849 strlen(entry->rtext));
854 static void
855 move_menus(WMenu *menu, int x, int y)
857 while (menu->parent) {
858 menu = menu->parent;
859 x -= MENUW(menu);
860 if (!wPreferences.align_menus && menu->selected_entry>=0) {
861 y -= menu->selected_entry*menu->entry_height;
864 wMenuMove(menu, x, y, True);
867 static void
868 makeVisible(WMenu *menu)
870 WScreen *scr = menu->frame->screen_ptr;
871 int x1, y1, x2, y2, new_x, new_y, move;
873 if (menu->entry_no<0) return;
875 x1 = menu->frame_x;
876 y1 = menu->frame_y+menu->frame->top_width
877 + menu->selected_entry*menu->entry_height;
878 x2 = x1 + MENUW(menu);
879 y2 = y1 + menu->entry_height;
881 new_x = x1;
882 new_y = y1;
883 move = 0;
885 if (x1 < 0) {
886 new_x = 0;
887 move = 1;
888 } else if (x2 >= scr->scr_width) {
889 new_x = scr->scr_width - MENUW(menu) - 1;
890 move = 1;
893 if (y1 < 0) {
894 new_y = 0;
895 move = 1;
896 } else if (y2 >= scr->scr_height) {
897 new_y = scr->scr_height - menu->entry_height - 1;
898 move = 1;
901 new_y = new_y - menu->frame->top_width
902 - menu->selected_entry*menu->entry_height;
903 move_menus(menu, new_x, new_y);
907 static int
908 check_key(WMenu *menu, XKeyEvent *event)
910 int i, ch, s;
911 char buffer[32];
913 if (XLookupString(event, buffer, 32, NULL, NULL)<1)
914 return -1;
916 ch = toupper(buffer[0]);
918 s = (menu->selected_entry>=0 ? menu->selected_entry+1 : 0);
920 again:
921 for (i=s; i<menu->entry_no; i++) {
922 if (ch==toupper(menu->entries[i]->text[0])) {
923 return i;
926 /* no match. Retry from start, if previous started from a selected entry */
927 if (s!=0) {
928 s = 0;
929 goto again;
931 return -1;
935 static int
936 keyboardMenu(WMenu *menu)
938 XEvent event;
939 KeySym ksym=NoSymbol;
940 int done=0;
941 int index;
942 WMenuEntry *entry;
943 int old_pos_x = menu->frame_x;
944 int old_pos_y = menu->frame_y;
945 int new_x = old_pos_x, new_y = old_pos_y;
946 int scr_width = menu->frame->screen_ptr->scr_width;
947 int scr_height = menu->frame->screen_ptr->scr_height;
949 if (menu->flags.editing)
950 return False;
953 XGrabKeyboard(dpy, menu->frame->core->window, True, GrabModeAsync,
954 GrabModeAsync, CurrentTime);
956 if (menu->frame_y+menu->frame->top_width >= scr_height)
957 new_y = scr_height - menu->frame->top_width;
959 if (menu->frame_x+MENUW(menu) >= scr_width)
960 new_x = scr_width-MENUW(menu)-1;
962 move_menus(menu, new_x, new_y);
964 while (!done && menu->flags.mapped) {
965 XAllowEvents(dpy, AsyncKeyboard, CurrentTime);
966 WMMaskEvent(dpy, ExposureMask|ButtonMotionMask|ButtonPressMask
967 |ButtonReleaseMask|KeyPressMask|KeyReleaseMask
968 |SubstructureNotifyMask, &event);
970 switch (event.type) {
971 case KeyPress:
972 ksym = XLookupKeysym(&event.xkey, 0);
973 switch (ksym) {
974 case XK_Escape:
975 done = 1;
976 break;
978 case XK_Home:
979 case XK_KP_Home:
980 selectEntry(menu, 0);
981 makeVisible(menu);
982 break;
984 case XK_End:
985 case XK_KP_End:
986 selectEntry(menu, menu->entry_no-1);
987 makeVisible(menu);
988 break;
990 case XK_Up:
991 #ifdef ARROWLESS_KBD
992 case XK_k:
993 #endif
994 case XK_KP_Up:
995 if (menu->selected_entry <= 0)
996 selectEntry(menu, menu->entry_no-1);
997 else
998 selectEntry(menu, menu->selected_entry-1);
999 makeVisible(menu);
1000 break;
1002 case XK_Down:
1003 #ifdef ARROWLESS_KBD
1004 case XK_j:
1005 #endif
1006 case XK_KP_Down:
1007 if (menu->selected_entry<0)
1008 selectEntry(menu, 0);
1009 else if (menu->selected_entry == menu->entry_no-1)
1010 selectEntry(menu, 0);
1011 else if (menu->selected_entry < menu->entry_no-1)
1012 selectEntry(menu, menu->selected_entry+1);
1013 makeVisible(menu);
1014 break;
1016 case XK_Right:
1017 #ifdef ARROWLESS_KBD
1018 case XK_l:
1019 #endif
1020 case XK_KP_Right:
1021 if (menu->selected_entry>=0) {
1022 WMenuEntry *entry;
1023 entry = menu->entries[menu->selected_entry];
1025 if (entry->cascade >= 0 && menu->cascades
1026 && menu->cascades[entry->cascade]->entry_no > 0) {
1028 XUngrabKeyboard(dpy, CurrentTime);
1030 selectEntry(menu->cascades[entry->cascade], 0);
1031 if (!keyboardMenu(menu->cascades[entry->cascade]))
1032 done = 1;
1034 XGrabKeyboard(dpy, menu->frame->core->window, True,
1035 GrabModeAsync, GrabModeAsync,
1036 CurrentTime);
1039 break;
1041 case XK_Left:
1042 #ifdef ARROWLESS_KBD
1043 case XK_h:
1044 #endif
1045 case XK_KP_Left:
1046 if (menu->parent!=NULL && menu->parent->selected_entry>=0) {
1047 selectEntry(menu, -1);
1048 move_menus(menu, old_pos_x, old_pos_y);
1049 return True;
1051 break;
1053 case XK_Return:
1054 done = 2;
1055 break;
1057 default:
1058 index = check_key(menu, &event.xkey);
1059 if (index>=0) {
1060 selectEntry(menu, index);
1063 break;
1065 default:
1066 if (event.type==ButtonPress)
1067 done = 1;
1069 WMHandleEvent(&event);
1073 XUngrabKeyboard(dpy, CurrentTime);
1075 if (done==2 && menu->selected_entry>=0) {
1076 entry = menu->entries[menu->selected_entry];
1077 } else {
1078 entry = NULL;
1081 if (entry && entry->callback!=NULL && entry->flags.enabled
1082 && entry->cascade < 0) {
1083 #if (MENU_BLINK_COUNT > 0)
1084 int sel = menu->selected_entry;
1085 int i;
1087 for (i=0; i<MENU_BLINK_COUNT; i++) {
1088 paintEntry(menu, sel, False);
1089 XSync(dpy, 0);
1090 wusleep(MENU_BLINK_DELAY);
1091 paintEntry(menu, sel, True);
1092 XSync(dpy, 0);
1093 wusleep(MENU_BLINK_DELAY);
1095 #endif
1096 selectEntry(menu, -1);
1098 if (!menu->flags.buttoned) {
1099 wMenuUnmap(menu);
1100 move_menus(menu, old_pos_x, old_pos_y);
1102 closeCascade(menu);
1104 (*entry->callback)(menu, entry);
1105 } else {
1106 if (!menu->flags.buttoned) {
1107 wMenuUnmap(menu);
1108 move_menus(menu, old_pos_x, old_pos_y);
1110 selectEntry(menu, -1);
1114 /* returns True if returning from a submenu to a parent menu,
1115 * False if exiting from menu */
1116 return False;
1120 void
1121 wMenuMapAt(WMenu *menu, int x, int y, int keyboard)
1123 int scr_width = menu->frame->screen_ptr->scr_width;
1124 int scr_height = menu->frame->screen_ptr->scr_height;
1126 if (!menu->flags.realized) {
1127 menu->flags.realized=1;
1128 wMenuRealize(menu);
1130 if (!menu->flags.mapped) {
1131 if (wPreferences.wrap_menus) {
1132 if (x<0) x = 0;
1133 if (y<0) y = 0;
1134 if (x+MENUW(menu) > scr_width)
1135 x = scr_width - MENUW(menu);
1136 if (y+MENUH(menu) > scr_height)
1137 y = scr_height - MENUH(menu);
1140 XMoveWindow(dpy, menu->frame->core->window, x, y);
1141 menu->frame_x = x;
1142 menu->frame_y = y;
1143 XMapWindow(dpy, menu->frame->core->window);
1144 wRaiseFrame(menu->frame->core);
1145 menu->flags.mapped = 1;
1146 } else {
1147 selectEntry(menu, 0);
1150 if (keyboard)
1151 keyboardMenu(menu);
1155 void
1156 wMenuMap(WMenu *menu)
1158 if (!menu->flags.realized) {
1159 menu->flags.realized=1;
1160 wMenuRealize(menu);
1162 if (menu->flags.app_menu && menu->parent==NULL) {
1163 menu->frame_x = menu->frame->screen_ptr->app_menu_x;
1164 menu->frame_y = menu->frame->screen_ptr->app_menu_y;
1165 XMoveWindow(dpy, menu->frame->core->window, menu->frame_x, menu->frame_y);
1167 XMapWindow(dpy, menu->frame->core->window);
1168 wRaiseFrame(menu->frame->core);
1169 menu->flags.mapped = 1;
1173 void
1174 wMenuUnmap(WMenu *menu)
1176 int i;
1178 XUnmapWindow(dpy, menu->frame->core->window);
1179 if (menu->flags.titled && menu->flags.buttoned) {
1180 wFrameWindowHideButton(menu->frame, WFF_RIGHT_BUTTON);
1182 menu->flags.buttoned = 0;
1183 menu->flags.mapped = 0;
1184 menu->flags.open_to_left = 0;
1186 for (i=0; i<menu->cascade_no; i++) {
1187 if (menu->cascades[i]!=NULL
1188 && menu->cascades[i]->flags.mapped
1189 && !menu->cascades[i]->flags.buttoned) {
1191 wMenuUnmap(menu->cascades[i]);
1194 menu->selected_entry = -1;
1199 void
1200 wMenuPaint(WMenu *menu)
1202 int i;
1204 if (!menu->flags.mapped) {
1205 return;
1208 /* paint entries */
1209 for (i=0; i<menu->entry_no; i++) {
1210 paintEntry(menu, i, i==menu->selected_entry);
1215 void
1216 wMenuSetEnabled(WMenu *menu, int index, int enable)
1218 if (index>=menu->entry_no) return;
1219 menu->entries[index]->flags.enabled=enable;
1220 paintEntry(menu, index, index==menu->selected_entry);
1221 paintEntry(menu->brother, index, index==menu->selected_entry);
1225 /* ====================================================================== */
1228 static void
1229 editEntry(WMenu *menu, WMenuEntry *entry)
1231 WTextInput *text;
1232 XEvent event;
1233 WObjDescriptor *desc;
1234 char *t;
1235 int done = 0;
1236 Window old_focus;
1237 int old_revert;
1239 menu->flags.editing = 1;
1241 text = wTextCreate(menu->menu, 1, menu->entry_height * entry->order,
1242 menu->menu->width - 2, menu->entry_height - 1);
1244 wTextPutText(text, entry->text);
1245 XGetInputFocus(dpy, &old_focus, &old_revert);
1246 XSetInputFocus(dpy, text->core->window, RevertToNone, CurrentTime);
1248 if (XGrabKeyboard(dpy, text->core->window, True, GrabModeAsync,
1249 GrabModeAsync, CurrentTime)!=GrabSuccess) {
1250 wwarning("could not grab keyboard");
1251 wTextDestroy(text);
1253 wSetFocusTo(menu->frame->screen_ptr,
1254 menu->frame->screen_ptr->focused_window);
1255 return;
1259 while (!done && !text->done) {
1260 XSync(dpy, 0);
1261 XAllowEvents(dpy, AsyncKeyboard|AsyncPointer, CurrentTime);
1262 XSync(dpy, 0);
1263 WMNextEvent(dpy, &event);
1265 if (XFindContext(dpy, event.xany.window, wWinContext,
1266 (XPointer *)&desc)==XCNOENT)
1267 desc = NULL;
1269 if ((desc != NULL) && (desc->handle_anything != NULL)) {
1271 (*desc->handle_anything)(desc, &event);
1273 } else {
1274 switch (event.type) {
1275 case ButtonPress:
1276 XAllowEvents(dpy, ReplayPointer, CurrentTime);
1277 done = 1;
1279 default:
1280 WMHandleEvent(&event);
1281 break;
1286 XSetInputFocus(dpy, old_focus, old_revert, CurrentTime);
1288 wSetFocusTo(menu->frame->screen_ptr,
1289 menu->frame->screen_ptr->focused_window);
1292 t = wTextGetText(text);
1293 /* if !t, the user has canceled editing */
1294 if (t) {
1295 if (entry->text)
1296 free(entry->text);
1297 entry->text = wstrdup(t);
1299 menu->flags.realized = 0;
1301 wTextDestroy(text);
1303 XUngrabKeyboard(dpy, CurrentTime);
1305 if (t && menu->on_edit)
1306 (*menu->on_edit)(menu, entry);
1308 menu->flags.editing = 0;
1310 if (!menu->flags.realized)
1311 wMenuRealize(menu);
1315 static void
1316 selectEntry(WMenu *menu, int entry_no)
1318 WMenuEntry *entry;
1319 WMenu *submenu;
1320 int old_entry;
1322 if (menu->entries==NULL)
1323 return;
1325 if (entry_no >= menu->entry_no)
1326 return;
1328 old_entry = menu->selected_entry;
1329 menu->selected_entry = entry_no;
1331 if (old_entry!=entry_no) {
1333 /* unselect previous entry */
1334 if (old_entry>=0) {
1335 paintEntry(menu, old_entry, False);
1336 entry = menu->entries[old_entry];
1338 /* unmap cascade */
1339 if (entry->cascade>=0 && menu->cascades) {
1340 if (!menu->cascades[entry->cascade]->flags.buttoned) {
1341 wMenuUnmap(menu->cascades[entry->cascade]);
1346 if (entry_no<0) {
1347 menu->selected_entry = -1;
1348 return;
1350 entry = menu->entries[entry_no];
1352 if (entry->cascade>=0 && menu->cascades && entry->flags.enabled) {
1353 /* Callback for when the submenu is opened.
1355 submenu = menu->cascades[entry->cascade];
1356 if (submenu && submenu->flags.brother)
1357 submenu = submenu->brother;
1359 if (entry->callback) {
1360 /* Only call the callback if the submenu is not yet mapped.
1362 if (menu->flags.brother) {
1363 if (!submenu || !submenu->flags.mapped)
1364 (*entry->callback)(menu->brother, entry);
1365 } else {
1366 if (!submenu || !submenu->flags.buttoned)
1367 (*entry->callback)(menu, entry);
1371 /* the submenu menu might have changed */
1372 submenu = menu->cascades[entry->cascade];
1374 /* map cascade */
1375 if (!submenu->flags.mapped) {
1376 int x, y;
1378 if (!submenu->flags.realized)
1379 wMenuRealize(submenu);
1380 if (wPreferences.wrap_menus) {
1381 if (menu->flags.open_to_left)
1382 submenu->flags.open_to_left = 1;
1384 if (submenu->flags.open_to_left) {
1385 x = menu->frame_x - MENUW(submenu);
1386 if (x<0) {
1387 x = 0;
1388 submenu->flags.open_to_left = 0;
1390 } else {
1391 x = menu->frame_x + MENUW(menu);
1393 if (x + MENUW(submenu)
1394 >= menu->frame->screen_ptr->scr_width) {
1396 x = menu->frame_x - MENUW(submenu);
1397 submenu->flags.open_to_left = 1;
1400 } else {
1401 x = menu->frame_x + MENUW(menu);
1404 if (wPreferences.align_menus) {
1405 y = menu->frame_y;
1406 } else {
1407 y = menu->frame_y + menu->entry_height*entry_no;
1408 if (menu->flags.titled)
1409 y += menu->frame->top_width;
1410 if (menu->cascades[entry->cascade]->flags.titled)
1411 y -= menu->cascades[entry->cascade]->frame->top_width;
1414 wMenuMapAt(menu->cascades[entry->cascade], x, y, False);
1415 menu->cascades[entry->cascade]->parent = menu;
1416 } else {
1417 return;
1420 paintEntry(menu, entry_no, True);
1425 static WMenu*
1426 findMenu(WScreen *scr, int *x_ret, int *y_ret)
1428 WMenu *menu;
1429 WObjDescriptor *desc;
1430 Window root_ret, win, junk_win;
1431 int x, y, wx, wy;
1432 unsigned int mask;
1434 XQueryPointer(dpy, scr->root_win, &root_ret, &win, &x, &y, &wx, &wy,
1435 &mask);
1437 if (win==None) return NULL;
1439 if (XFindContext(dpy, win, wWinContext, (XPointer *)&desc)==XCNOENT)
1440 return NULL;
1442 if (desc->parent_type == WCLASS_MENU) {
1443 menu = (WMenu*)desc->parent;
1444 XTranslateCoordinates(dpy, root_ret, menu->menu->window, wx, wy,
1445 x_ret, y_ret, &junk_win);
1446 return menu;
1448 return NULL;
1454 static void
1455 closeCascade(WMenu *menu)
1457 WMenu *parent=menu->parent;
1459 if (menu->flags.brother
1460 || (!menu->flags.buttoned
1461 && (!menu->flags.app_menu||menu->parent!=NULL))) {
1463 selectEntry(menu, -1);
1464 XSync(dpy, 0);
1465 #if (MENU_BLINK_DELAY > 2)
1466 wusleep(MENU_BLINK_DELAY/2);
1467 #endif
1468 wMenuUnmap(menu);
1469 while (parent!=NULL
1470 && (parent->parent!=NULL || !parent->flags.app_menu
1471 || parent->flags.brother)
1472 && !parent->flags.buttoned) {
1473 selectEntry(parent, -1);
1474 wMenuUnmap(parent);
1475 parent = parent->parent;
1477 if (parent)
1478 selectEntry(parent, -1);
1483 static void
1484 closeBrotherCascadesOf(WMenu *menu)
1486 WMenu *tmp;
1487 int i;
1489 for (i=0; i<menu->cascade_no; i++) {
1490 if (menu->cascades[i]->flags.brother) {
1491 tmp = menu->cascades[i];
1492 } else {
1493 tmp = menu->cascades[i]->brother;
1495 if (tmp->flags.mapped) {
1496 selectEntry(tmp->parent, -1);
1497 closeBrotherCascadesOf(tmp);
1498 break;
1504 #define getEntryAt(menu, x, y) ((y)<0 ? -1 : (y)/(menu->entry_height))
1507 static WMenu*
1508 parentMenu(WMenu *menu)
1510 WMenu *parent;
1511 WMenuEntry *entry;
1513 if (menu->flags.buttoned)
1514 return menu;
1516 while (menu->parent && menu->parent->flags.mapped) {
1517 parent = menu->parent;
1518 if (parent->selected_entry < 0)
1519 break;
1520 entry = parent->entries[parent->selected_entry];
1521 if (!entry->flags.enabled || entry->cascade<0 || !parent->cascades ||
1522 parent->cascades[entry->cascade] != menu)
1523 break;
1524 menu = parent;
1525 if (menu->flags.buttoned)
1526 break;
1529 return menu;
1535 * Will raise the passed menu, if submenu = 0
1536 * If submenu > 0 will also raise all mapped submenus
1537 * until the first buttoned one
1538 * If submenu < 0 will also raise all mapped parent menus
1539 * until the first buttoned one
1542 static void
1543 raiseMenus(WMenu *menu, int submenus)
1545 WMenu *submenu;
1546 int i;
1548 if(!menu) return;
1550 wRaiseFrame(menu->frame->core);
1552 if (submenus>0 && menu->selected_entry>=0) {
1553 i = menu->entries[menu->selected_entry]->cascade;
1554 if (i>=0 && menu->cascades) {
1555 submenu = menu->cascades[i];
1556 if (submenu->flags.mapped && !submenu->flags.buttoned)
1557 raiseMenus(submenu, submenus);
1560 if (submenus<0 && !menu->flags.buttoned &&
1561 menu->parent && menu->parent->flags.mapped)
1562 raiseMenus(menu->parent, submenus);
1566 WMenu*
1567 wMenuUnderPointer(WScreen *screen)
1569 WObjDescriptor *desc;
1570 Window root_ret, win;
1571 int dummy;
1572 unsigned int mask;
1574 XQueryPointer(dpy, screen->root_win, &root_ret, &win, &dummy, &dummy,
1575 &dummy, &dummy, &mask);
1577 if (win==None) return NULL;
1579 if (XFindContext(dpy, win, wWinContext, (XPointer *)&desc)==XCNOENT)
1580 return NULL;
1582 if (desc->parent_type == WCLASS_MENU)
1583 return (WMenu *)desc->parent;
1584 return NULL;
1589 #define MIN(a,b) (((a) > (b)) ? (b) : (a))
1592 static void
1593 getPointerPosition(WScreen *scr, int *x, int *y)
1595 Window root_ret, win;
1596 int wx, wy;
1597 unsigned int mask;
1599 XQueryPointer(dpy, scr->root_win, &root_ret, &win, x, y, &wx, &wy, &mask);
1603 static void
1604 getScrollAmount(WMenu *menu, int *hamount, int *vamount)
1606 WScreen *scr = menu->menu->screen_ptr;
1607 int menuX1 = menu->frame_x;
1608 int menuY1 = menu->frame_y;
1609 int menuX2 = menu->frame_x + MENUW(menu);
1610 int menuY2 = menu->frame_y + MENUH(menu);
1611 int screenW = scr->scr_width;
1612 int screenH = scr->scr_height;
1613 int xroot, yroot;
1615 *hamount = 0;
1616 *vamount = 0;
1618 getPointerPosition(scr, &xroot, &yroot);
1621 if (xroot <= 1 && menuX1 < 0) {
1622 /* scroll to the right */
1623 *hamount = MIN(MENU_SCROLL_STEP, abs(menuX1));
1625 } else if (xroot >= screenW-2 && menuX2 > screenW-1) {
1626 /* scroll to the left */
1627 *hamount = MIN(MENU_SCROLL_STEP, abs(menuX2-screenW-1));
1629 if (*hamount==0)
1630 *hamount = 1;
1632 *hamount = -*hamount;
1635 if (yroot <= 1 && menuY1 < 0) {
1636 /* scroll down */
1637 *vamount = MIN(MENU_SCROLL_STEP, abs(menuY1));
1639 } else if (yroot >= screenH-2 && menuY2 > screenH-1) {
1640 /* scroll up */
1641 *vamount = MIN(MENU_SCROLL_STEP, abs(menuY2-screenH-2));
1643 *vamount = -*vamount;
1648 static void
1649 dragScrollMenuCallback(void *data)
1651 WMenu *menu = (WMenu*)data;
1652 WScreen *scr = menu->menu->screen_ptr;
1653 WMenu *parent = parentMenu(menu);
1654 int hamount, vamount;
1655 int x, y;
1656 int newSelectedEntry;
1658 getScrollAmount(menu, &hamount, &vamount);
1661 if (hamount != 0 || vamount != 0) {
1662 wMenuMove(parent, parent->frame_x + hamount,
1663 parent->frame_y + vamount, True);
1664 if (findMenu(scr, &x, &y)) {
1665 newSelectedEntry = getEntryAt(menu, x, y);
1666 selectEntry(menu, newSelectedEntry);
1667 } else {
1668 /* Pointer fell outside of menu. If the selected entry is
1669 * not a submenu, unselect it */
1670 if (menu->selected_entry >= 0
1671 && menu->entries[menu->selected_entry]->cascade<0)
1672 selectEntry(menu, -1);
1673 newSelectedEntry = 0;
1676 /* paranoid check */
1677 if (newSelectedEntry >= 0) {
1678 /* keep scrolling */
1679 menu->timer = WMAddTimerHandler(MENU_SCROLL_DELAY,
1680 dragScrollMenuCallback, menu);
1681 } else {
1682 menu->timer = NULL;
1684 } else {
1685 /* don't need to scroll anymore */
1686 menu->timer = NULL;
1687 if (findMenu(scr, &x, &y)) {
1688 newSelectedEntry = getEntryAt(menu, x, y);
1689 selectEntry(menu, newSelectedEntry);
1695 static void
1696 scrollMenuCallback(void *data)
1698 WMenu *menu = (WMenu*)data;
1699 WMenu *parent = parentMenu(menu);
1700 int hamount = 0; /* amount to scroll */
1701 int vamount = 0;
1703 getScrollAmount(menu, &hamount, &vamount);
1705 if (hamount != 0 || vamount != 0) {
1706 wMenuMove(parent, parent->frame_x + hamount,
1707 parent->frame_y + vamount, True);
1709 /* keep scrolling */
1710 menu->timer = WMAddTimerHandler(MENU_SCROLL_DELAY,
1711 scrollMenuCallback, menu);
1712 } else {
1713 /* don't need to scroll anymore */
1714 menu->timer = NULL;
1720 #define MENU_SCROLL_BORDER 5
1722 static int
1723 isPointNearBoder(WMenu *menu, int x, int y)
1725 int menuX1 = menu->frame_x;
1726 int menuY1 = menu->frame_y;
1727 int menuX2 = menu->frame_x + MENUW(menu);
1728 int menuY2 = menu->frame_y + MENUH(menu);
1729 int scrXe = menu->menu->screen_ptr->scr_width-1;
1730 int scrYe = menu->menu->screen_ptr->scr_height-1;
1731 int flag = 0;
1733 if (x >= menuX1 && x <= menuX2 && (y < MENU_SCROLL_BORDER
1734 || y > scrYe-MENU_SCROLL_BORDER))
1735 flag = 1;
1736 else if (y >= menuY1 && y <= menuY2 && (x < MENU_SCROLL_BORDER
1737 || x > scrXe-MENU_SCROLL_BORDER))
1738 flag = 1;
1740 return flag;
1744 typedef struct _delay {
1745 WWindow *wwin;
1746 WMenu *menu;
1747 int ox,oy;
1748 } _delay;
1751 static void
1752 _leaving(_delay *dl)
1754 wMenuMove(dl->menu, dl->ox, dl->oy, True);
1755 dl->menu->jump_back=NULL;
1756 dl->menu->menu->screen_ptr->flags.jump_back_pending = 0;
1757 free(dl);
1761 void
1762 wMenuScroll(WMenu *menu, XEvent *event)
1764 WMenu *smenu;
1765 WMenu *omenu = parentMenu(menu);
1766 WScreen *scr = menu->frame->screen_ptr;
1767 int done = 0;
1768 int jump_back = 0;
1769 int old_frame_x = omenu->frame_x;
1770 int old_frame_y = omenu->frame_y;
1771 XEvent ev;
1773 #ifdef DEBUG
1774 puts("Entering menu Scroll");
1775 #endif
1777 if (omenu->jump_back)
1778 WMDeleteTimerWithClientData(omenu->jump_back);
1781 if ((/*omenu->flags.buttoned &&*/ !wPreferences.wrap_menus)
1782 || omenu->flags.app_menu) {
1783 jump_back = 1;
1786 if (!wPreferences.wrap_menus)
1787 raiseMenus(omenu, True);
1788 else
1789 raiseMenus(menu, False);
1791 if (!menu->timer)
1792 scrollMenuCallback(menu);
1794 while(!done) {
1795 int x, y, on_border, on_x_edge, on_y_edge, on_title;
1797 WMNextEvent(dpy, &ev);
1798 switch (ev.type) {
1799 case EnterNotify:
1800 WMHandleEvent(&ev);
1801 case MotionNotify:
1802 x = (ev.type==MotionNotify) ? ev.xmotion.x_root : ev.xcrossing.x_root;
1803 y = (ev.type==MotionNotify) ? ev.xmotion.y_root : ev.xcrossing.y_root;
1805 /* on_border is != 0 if the pointer is between the menu
1806 * and the screen border and is close enough to the border */
1807 on_border = isPointNearBoder(menu, x, y);
1809 smenu = wMenuUnderPointer(scr);
1811 if ((smenu==NULL && !on_border) || (smenu && parentMenu(smenu)!=omenu)) {
1812 done = 1;
1813 break;
1816 on_x_edge = x <= 1 || x >= scr->scr_width - 2;
1817 on_y_edge = y <= 1 || y >= scr->scr_height - 2;
1818 on_border = on_x_edge || on_y_edge;
1820 if (!on_border && !jump_back) {
1821 done = 1;
1822 break;
1825 if (menu->timer && (smenu!=menu || (!on_y_edge && !on_x_edge))) {
1826 WMDeleteTimerHandler(menu->timer);
1827 menu->timer = NULL;
1830 if (smenu != NULL)
1831 menu = smenu;
1833 if (!menu->timer)
1834 scrollMenuCallback(menu);
1835 break;
1836 case ButtonPress:
1837 /* True if we push on title, or drag the omenu to other position */
1838 on_title = ev.xbutton.x_root >= omenu->frame_x &&
1839 ev.xbutton.x_root <= omenu->frame_x + MENUW(omenu) &&
1840 ev.xbutton.y_root >= omenu->frame_y &&
1841 ev.xbutton.y_root <= omenu->frame_y + omenu->frame->top_width;
1842 WMHandleEvent(&ev);
1843 smenu = wMenuUnderPointer(scr);
1844 if (smenu == NULL || (smenu && smenu->flags.buttoned && smenu != omenu))
1845 done = 1;
1846 else if (smenu==omenu && on_title) {
1847 jump_back = 0;
1848 done = 1;
1850 break;
1851 case KeyPress:
1852 done = 1;
1853 default:
1854 WMHandleEvent(&ev);
1855 break;
1859 if (menu->timer) {
1860 WMDeleteTimerHandler(menu->timer);
1861 menu->timer = NULL;
1864 if (jump_back) {
1865 _delay *delayer;
1866 if (!omenu->jump_back) {
1867 delayer=wmalloc(sizeof(_delay));
1868 delayer->menu=omenu;
1869 delayer->ox=old_frame_x;
1870 delayer->oy=old_frame_y;
1871 omenu->jump_back=delayer;
1872 scr->flags.jump_back_pending = 1;
1874 else delayer = omenu->jump_back;
1875 WMAddTimerHandler(MENU_JUMP_BACK_DELAY,(WMCallback*)_leaving, delayer);
1879 #ifdef DEBUG
1880 puts("Leaving menu Scroll");
1881 #endif
1886 static void
1887 menuExpose(WObjDescriptor *desc, XEvent *event)
1889 wMenuPaint(desc->parent);
1892 typedef struct {
1893 int *delayed_select;
1894 WMenu *menu;
1895 WMHandlerID magic;
1896 } delay_data;
1899 static void
1900 delaySelection(void *data)
1902 delay_data *d = (delay_data*)data;
1903 int x, y, entry_no;
1904 WMenu *menu;
1906 d->magic = NULL;
1908 menu = findMenu(d->menu->menu->screen_ptr, &x, &y);
1909 if (menu && (d->menu == menu || d->delayed_select)) {
1910 entry_no = getEntryAt(menu, x, y);
1911 selectEntry(menu, entry_no);
1913 if (d->delayed_select)
1914 *(d->delayed_select) = 0;
1918 static void
1919 menuMouseDown(WObjDescriptor *desc, XEvent *event)
1921 XButtonEvent *bev = &event->xbutton;
1922 WMenu *menu = desc->parent;
1923 WMenu *smenu;
1924 WScreen *scr=menu->frame->screen_ptr;
1925 WMenuEntry *entry=NULL;
1926 XEvent ev;
1927 int close_on_exit=0;
1928 int done=0;
1929 int delayed_select = 0;
1930 int entry_no;
1931 int x, y;
1932 int prevx, prevy;
1933 int old_frame_x = 0;
1934 int old_frame_y = 0;
1935 delay_data d_data = {NULL, NULL, NULL};
1937 if (!wPreferences.wrap_menus) {
1938 smenu = parentMenu(menu);
1939 old_frame_x = smenu->frame_x;
1940 old_frame_y = smenu->frame_y;
1941 } else if (event->xbutton.window == menu->frame->core->window) {
1942 /* This is true if the menu was launched with right click on root window */
1943 delayed_select = 1;
1944 d_data.delayed_select = &delayed_select;
1945 d_data.menu = menu;
1946 d_data.magic = WMAddTimerHandler(wPreferences.dblclick_time,
1947 delaySelection, &d_data);
1950 wRaiseFrame(menu->frame->core);
1952 close_on_exit = (bev->send_event || menu->flags.brother);
1954 smenu = findMenu(scr, &x, &y);
1955 if (!smenu) {
1956 x = -1;
1957 y = -1;
1958 } else {
1959 menu = smenu;
1962 if (menu->flags.editing) {
1963 return;
1965 entry_no = getEntryAt(menu, x, y);
1966 if (entry_no>=0) {
1967 entry = menu->entries[entry_no];
1969 if (!close_on_exit && (bev->state & ControlMask) && smenu
1970 && entry->flags.editable) {
1971 editEntry(smenu, entry);
1972 return;
1973 } else if (bev->state & ControlMask) {
1974 return;
1977 if (entry->flags.enabled && entry->cascade>=0 && menu->cascades) {
1978 WMenu *submenu = menu->cascades[entry->cascade];
1979 /* map cascade */
1980 if (submenu->flags.mapped && !submenu->flags.buttoned &&
1981 menu->selected_entry!=entry_no) {
1982 wMenuUnmap(submenu);
1984 if (!submenu->flags.mapped && !delayed_select) {
1985 selectEntry(menu, entry_no);
1986 } else if (!submenu->flags.buttoned) {
1987 selectEntry(menu, -1);
1990 } else if (!delayed_select) {
1991 selectEntry(menu, entry_no);
1994 if (!wPreferences.wrap_menus && !wPreferences.scrollable_menus) {
1995 if (!menu->timer)
1996 dragScrollMenuCallback(menu);
2000 prevx = bev->x_root;
2001 prevy = bev->y_root;
2002 while (!done) {
2003 int x, y;
2005 XAllowEvents(dpy, SyncPointer, CurrentTime);
2007 WMMaskEvent(dpy, ExposureMask|ButtonMotionMask|ButtonReleaseMask
2008 |ButtonPressMask, &ev);
2009 switch (ev.type) {
2010 case MotionNotify:
2011 smenu = findMenu(scr, &x, &y);
2013 if (smenu == NULL) {
2014 /* moved mouse out of menu */
2016 if (!delayed_select && d_data.magic) {
2017 WMDeleteTimerHandler(d_data.magic);
2018 d_data.magic = NULL;
2020 if (menu==NULL
2021 || (menu->selected_entry>=0
2022 && menu->entries[menu->selected_entry]->cascade>=0)) {
2023 prevx = ev.xmotion.x_root;
2024 prevy = ev.xmotion.y_root;
2026 break;
2028 selectEntry(menu, -1);
2029 menu = smenu;
2030 prevx = ev.xmotion.x_root;
2031 prevy = ev.xmotion.y_root;
2032 break;
2033 } else if (menu && menu!=smenu
2034 && (menu->selected_entry<0
2035 || menu->entries[menu->selected_entry]->cascade<0)) {
2036 selectEntry(menu, -1);
2038 if (!delayed_select && d_data.magic) {
2039 WMDeleteTimerHandler(d_data.magic);
2040 d_data.magic = NULL;
2042 } else {
2044 /* hysteresis for item selection */
2046 /* check if the motion was to the side, indicating that
2047 * the user may want to cross to a submenu */
2048 if (!delayed_select && menu) {
2049 int dx;
2050 Bool moved_to_submenu;/* moved to direction of submenu */
2052 dx = abs(prevx - ev.xmotion.x_root);
2054 moved_to_submenu = False;
2055 if (dx > 0 /* if moved enough to the side */
2056 /* maybe a open submenu */
2057 && menu->selected_entry>=0
2058 /* moving to the right direction */
2059 && (wPreferences.align_menus
2060 || ev.xmotion.y_root >= prevy)) {
2061 int index;
2063 index = menu->entries[menu->selected_entry]->cascade;
2064 if (index>=0) {
2065 if (menu->cascades[index]->frame_x>menu->frame_x) {
2066 if (prevx < ev.xmotion.x_root)
2067 moved_to_submenu = True;
2068 } else {
2069 if (prevx > ev.xmotion.x_root)
2070 moved_to_submenu = True;
2076 if (menu != smenu) {
2077 if (d_data.magic) {
2078 WMDeleteTimerHandler(d_data.magic);
2080 d_data.magic = NULL;
2081 } else if (moved_to_submenu) {
2082 /* while we are moving, postpone the selection */
2083 if (d_data.magic) {
2084 WMDeleteTimerHandler(d_data.magic);
2086 d_data.delayed_select = NULL;
2087 d_data.menu = menu;
2088 d_data.magic = WMAddTimerHandler(MENU_SELECT_DELAY,
2089 delaySelection,
2090 &d_data);
2091 prevx = ev.xmotion.x_root;
2092 prevy = ev.xmotion.y_root;
2093 break;
2094 } else {
2095 if (d_data.magic)
2096 WMDeleteTimerHandler(d_data.magic);
2097 d_data.magic = NULL;
2101 prevx = ev.xmotion.x_root;
2102 prevy = ev.xmotion.y_root;
2103 if (menu!=smenu) {
2104 /* pointer crossed menus */
2105 if (menu && menu->timer) {
2106 WMDeleteTimerHandler(menu->timer);
2107 menu->timer = NULL;
2109 if (smenu)
2110 dragScrollMenuCallback(smenu);
2112 menu = smenu;
2113 if (!menu->timer)
2114 dragScrollMenuCallback(menu);
2116 if (!delayed_select) {
2117 entry_no = getEntryAt(menu, x, y);
2118 if (entry_no>=0) {
2119 entry = menu->entries[entry_no];
2120 if (entry->flags.enabled && entry->cascade>=0 &&
2121 menu->cascades) {
2122 WMenu *submenu = menu->cascades[entry->cascade];
2123 if (submenu->flags.mapped && !submenu->flags.buttoned
2124 && menu->selected_entry!=entry_no) {
2125 wMenuUnmap(submenu);
2129 selectEntry(menu, entry_no);
2131 break;
2133 case ButtonPress:
2134 break;
2136 case ButtonRelease:
2137 if (ev.xbutton.button == event->xbutton.button)
2138 done=1;
2139 break;
2141 case Expose:
2142 WMHandleEvent(&ev);
2143 break;
2147 if (menu && menu->timer) {
2148 WMDeleteTimerHandler(menu->timer);
2149 menu->timer = NULL;
2151 if (d_data.magic!=NULL)
2152 WMDeleteTimerHandler(d_data.magic);
2154 if (menu && menu->selected_entry>=0) {
2155 entry = menu->entries[menu->selected_entry];
2156 if (entry->callback!=NULL && entry->flags.enabled
2157 && entry->cascade < 0) {
2158 /* blink and erase menu selection */
2159 #if (MENU_BLINK_DELAY > 0)
2160 int sel = menu->selected_entry;
2161 int i;
2163 for (i=0; i<MENU_BLINK_COUNT; i++) {
2164 paintEntry(menu, sel, False);
2165 XSync(dpy, 0);
2166 wusleep(MENU_BLINK_DELAY);
2167 paintEntry(menu, sel, True);
2168 XSync(dpy, 0);
2169 wusleep(MENU_BLINK_DELAY);
2171 #endif
2172 /* unmap the menu, it's parents and call the callback */
2173 if (!menu->flags.buttoned &&
2174 (!menu->flags.app_menu||menu->parent!=NULL)) {
2175 closeCascade(menu);
2176 } else {
2177 selectEntry(menu, -1);
2179 (*entry->callback)(menu, entry);
2181 /* If the user double clicks an entry, the entry will
2182 * be executed twice, which is not good for things like
2183 * the root menu. So, ignore any clicks that were generated
2184 * while the entry was being executed */
2185 while (XCheckTypedWindowEvent(dpy, menu->menu->window,
2186 ButtonPress, &ev));
2187 } else if (entry->callback!=NULL && entry->cascade<0) {
2188 selectEntry(menu, -1);
2189 } else {
2190 if (entry->cascade>=0 && menu->cascades
2191 && menu->cascades[entry->cascade]->flags.brother) {
2192 selectEntry(menu, -1);
2197 if (((WMenu*)desc->parent)->flags.brother || close_on_exit || !smenu)
2198 closeCascade(desc->parent);
2200 /* close the cascade windows that should not remain opened */
2201 closeBrotherCascadesOf(desc->parent);
2203 if (!wPreferences.wrap_menus)
2204 wMenuMove(parentMenu(desc->parent), old_frame_x, old_frame_y, True);
2208 void
2209 wMenuMove(WMenu *menu, int x, int y, int submenus)
2211 WMenu *submenu;
2212 int i;
2214 if (!menu) return;
2216 menu->frame_x = x;
2217 menu->frame_y = y;
2218 XMoveWindow(dpy, menu->frame->core->window, x, y);
2220 if (submenus>0 && menu->selected_entry>=0) {
2221 i = menu->entries[menu->selected_entry]->cascade;
2223 if (i>=0 && menu->cascades) {
2224 submenu = menu->cascades[i];
2225 if (submenu->flags.mapped && !submenu->flags.buttoned)
2226 if (wPreferences.align_menus) {
2227 wMenuMove(submenu, x + MENUW(menu), y, submenus);
2228 } else {
2229 wMenuMove(submenu, x+ MENUW(menu),
2230 y + submenu->entry_height*menu->selected_entry,
2231 submenus);
2235 if (submenus<0 && menu->parent!=NULL && menu->parent->flags.mapped &&
2236 !menu->parent->flags.buttoned) {
2237 if (wPreferences.align_menus) {
2238 wMenuMove(menu->parent, x - MENUW(menu->parent), y, submenus);
2239 } else {
2240 wMenuMove(menu->parent, x - MENUW(menu->parent), menu->frame_y
2241 - menu->parent->entry_height*menu->parent->selected_entry,
2242 submenus);
2248 static void
2249 changeMenuLevels(WMenu *menu, int lower)
2251 int i;
2253 if (!lower) {
2254 ChangeStackingLevel(menu->frame->core, (!menu->parent ? WMMainMenuLevel
2255 : WMSubmenuLevel));
2256 wRaiseFrame(menu->frame->core);
2257 menu->flags.lowered = 0;
2258 } else {
2259 ChangeStackingLevel(menu->frame->core, WMNormalLevel);
2260 wLowerFrame(menu->frame->core);
2261 menu->flags.lowered = 1;
2263 for (i=0; i<menu->cascade_no; i++) {
2264 if (menu->cascades[i]
2265 && !menu->cascades[i]->flags.buttoned
2266 && menu->cascades[i]->flags.lowered!=lower) {
2267 changeMenuLevels(menu->cascades[i], lower);
2274 static void
2275 menuTitleDoubleClick(WCoreWindow *sender, void *data, XEvent *event)
2277 WMenu *menu = data;
2278 int lower;
2280 if (event->xbutton.state & MOD_MASK) {
2281 if (menu->flags.lowered) {
2282 lower = 0;
2283 } else {
2284 lower = 1;
2286 changeMenuLevels(menu, lower);
2291 static void
2292 menuTitleMouseDown(WCoreWindow *sender, void *data, XEvent *event)
2294 WMenu *menu = data;
2295 WMenu *tmp;
2296 XEvent ev;
2297 int x=menu->frame_x, y=menu->frame_y;
2298 int dx=event->xbutton.x_root, dy=event->xbutton.y_root;
2299 int i, lower;
2300 Bool started;
2302 #ifdef DEBUG
2303 printf("Moving menu\n");
2304 #endif
2306 /* can't touch the menu copy */
2307 if (menu->flags.brother)
2308 return;
2310 if (event->xbutton.button != Button1 && event->xbutton.button != Button2)
2311 return;
2313 if (event->xbutton.state & MOD_MASK) {
2314 wLowerFrame(menu->frame->core);
2315 lower = 1;
2316 } else {
2317 wRaiseFrame(menu->frame->core);
2318 lower = 0;
2320 tmp = menu;
2322 /* lower/raise all submenus */
2323 while (1) {
2324 if (tmp->selected_entry>=0 && tmp->cascades
2325 && tmp->entries[tmp->selected_entry]->cascade>=0) {
2326 tmp = tmp->cascades[tmp->entries[tmp->selected_entry]->cascade];
2327 if (!tmp || !tmp->flags.mapped)
2328 break;
2329 if (lower)
2330 wLowerFrame(tmp->frame->core);
2331 else
2332 wRaiseFrame(tmp->frame->core);
2333 } else {
2334 break;
2338 /* tear off the menu if it's a root menu or a cascade
2339 application menu */
2340 if (!menu->flags.buttoned && !menu->flags.brother
2341 && (!menu->flags.app_menu||menu->parent!=NULL)) {
2342 menu->flags.buttoned=1;
2343 wFrameWindowShowButton(menu->frame, WFF_RIGHT_BUTTON);
2344 if (menu->parent) {
2345 /* turn off selected menu entry in parent menu */
2346 selectEntry(menu->parent, -1);
2348 /* make parent map the copy in place of the original */
2349 for (i=0; i<menu->parent->cascade_no; i++) {
2350 if (menu->parent->cascades[i] == menu) {
2351 menu->parent->cascades[i] = menu->brother;
2352 break;
2358 started = False;
2359 while(1) {
2360 WMMaskEvent(dpy, ButtonMotionMask|ButtonReleaseMask|ButtonPressMask
2361 |ExposureMask, &ev);
2362 switch (ev.type) {
2363 case MotionNotify:
2364 if (started) {
2365 x += ev.xmotion.x_root - dx;
2366 y += ev.xmotion.y_root - dy;
2367 dx = ev.xmotion.x_root;
2368 dy = ev.xmotion.y_root;
2369 wMenuMove(menu, x, y, True);
2370 } else {
2371 if (abs(ev.xmotion.x_root - dx) > MOVE_THRESHOLD
2372 || abs(ev.xmotion.y_root - dy) > MOVE_THRESHOLD) {
2373 started = True;
2374 XGrabPointer(dpy, menu->frame->titlebar->window, False,
2375 ButtonMotionMask|ButtonReleaseMask
2376 |ButtonPressMask,
2377 GrabModeAsync, GrabModeAsync, None,
2378 wCursor[WCUR_MOVE], CurrentTime);
2381 break;
2383 case ButtonPress:
2384 break;
2386 case ButtonRelease:
2387 if (ev.xbutton.button != event->xbutton.button)
2388 break;
2389 #ifdef DEBUG
2390 printf("End menu move\n");
2391 #endif
2392 XUngrabPointer(dpy, CurrentTime);
2393 return;
2395 default:
2396 WMHandleEvent(&ev);
2397 break;
2403 *----------------------------------------------------------------------
2404 * menuCloseClick--
2405 * Handles mouse click on the close button of menus. The menu is
2406 * closed when the button is clicked.
2408 * Side effects:
2409 * The closed menu is reinserted at it's parent menus
2410 * cascade list.
2411 *----------------------------------------------------------------------
2413 static void
2414 menuCloseClick(WCoreWindow *sender, void *data, XEvent *event)
2416 WMenu *menu = (WMenu*)data;
2417 WMenu *parent = menu->parent;
2418 int i;
2420 if (parent) {
2421 for (i=0; i<parent->cascade_no; i++) {
2422 /* find the entry that points to the copy */
2423 if (parent->cascades[i] == menu->brother) {
2424 /* make it point to the original */
2425 parent->cascades[i] = menu;
2426 menu->parent = parent;
2427 break;
2431 wMenuUnmap(menu);
2435 void
2436 wMenuSaveState(WScreen *scr)
2438 proplist_t menus, key, value;
2439 int save_menus = 0;
2440 char buffer[256];
2442 menus = PLMakeDictionaryFromEntries(NULL, NULL, NULL);
2444 #ifndef LITE
2445 if (scr->root_menu && scr->root_menu->flags.buttoned) {
2446 sprintf(buffer, "%i,%i", scr->root_menu->frame_x,
2447 scr->root_menu->frame_y);
2448 key = PLMakeString("RootMenu");
2449 value = PLMakeString(buffer);
2450 PLInsertDictionaryEntry(menus, key, value);
2451 PLRelease(key);
2452 PLRelease(value);
2453 save_menus = 1;
2456 if (scr->switch_menu && scr->switch_menu->flags.buttoned) {
2457 sprintf(buffer, "%i,%i", scr->switch_menu->frame_x,
2458 scr->switch_menu->frame_y);
2459 key = PLMakeString("SwitchMenu");
2460 value = PLMakeString(buffer);
2461 PLInsertDictionaryEntry(menus, key, value);
2462 PLRelease(key);
2463 PLRelease(value);
2464 save_menus = 1;
2466 #endif /* !LITE */
2467 if (scr->workspace_menu && scr->workspace_menu->flags.buttoned) {
2468 sprintf(buffer, "%i,%i", scr->workspace_menu->frame_x,
2469 scr->workspace_menu->frame_y);
2470 key = PLMakeString("WorkspaceMenu");
2471 value = PLMakeString(buffer);
2472 PLInsertDictionaryEntry(menus, key, value);
2473 PLRelease(key);
2474 PLRelease(value);
2475 save_menus = 1;
2478 if (save_menus) {
2479 key = PLMakeString("Menus");
2480 PLInsertDictionaryEntry(scr->session_state, key, menus);
2481 PLRelease(key);
2483 PLRelease(menus);
2487 #define COMPLAIN(key) wwarning(_("bad value in menus state info:%s"), key)
2490 static int
2491 restoreMenu(WScreen *scr, proplist_t menu, int which)
2493 int i, x, y;
2494 WMenu *pmenu = NULL;
2496 if (!menu)
2497 return False;
2499 if (!PLIsString(menu)) {
2500 COMPLAIN("Position");
2501 return False;
2504 if (sscanf(PLGetString(menu), "%i,%i", &x, &y)!=2)
2505 COMPLAIN("Position");
2507 #ifndef LITE
2508 if (which & WSS_ROOTMENU) {
2509 OpenRootMenu(scr, x, y, False);
2510 pmenu = scr->root_menu;
2511 } else if (which & WSS_SWITCHMENU) {
2512 OpenSwitchMenu(scr, x, y, False);
2513 pmenu = scr->switch_menu;
2514 } else
2515 #endif /* !LITE */
2516 if (which & WSS_WSMENU) {
2517 OpenWorkspaceMenu(scr, x, y);
2518 pmenu = scr->workspace_menu;
2519 if (pmenu->parent) {
2520 /* make parent map the copy in place of the original */
2521 for (i=0; i<pmenu->parent->cascade_no; i++) {
2522 if (pmenu->parent->cascades[i] == pmenu) {
2523 pmenu->parent->cascades[i] = pmenu->brother;
2524 break;
2530 if (pmenu) {
2531 int width = MENUW(pmenu);
2532 int height = MENUH(pmenu);
2534 x = (x < -width) ? 0 : x;
2535 x = (x > scr->scr_width) ? scr->scr_width - width : x;
2536 y = (y < 0) ? 0 : y;
2537 y = (y > scr->scr_height) ? scr->scr_height - height : y;
2538 wMenuMove(pmenu, x, y, True);
2539 pmenu->flags.buttoned = 1;
2540 wFrameWindowShowButton(pmenu->frame, WFF_RIGHT_BUTTON);
2541 return True;
2543 return False;
2547 void
2548 wMenuRestoreState(WScreen *scr)
2550 proplist_t menus, menu, key, rkey, skey, wkey;
2552 key = PLMakeString("Menus");
2553 menus = PLGetDictionaryEntry(scr->session_state, key);
2554 PLRelease(key);
2556 if (!menus)
2557 return;
2559 /* restore menus */
2561 rkey = PLMakeString("RootMenu");
2562 skey = PLMakeString("SwitchMenu");
2563 wkey = PLMakeString("WorkspaceMenu");
2564 menu = PLGetDictionaryEntry(menus, rkey);
2565 restoreMenu(scr, menu, WSS_ROOTMENU);
2566 menu = PLGetDictionaryEntry(menus, skey);
2567 restoreMenu(scr, menu, WSS_SWITCHMENU);
2568 menu = PLGetDictionaryEntry(menus, wkey);
2569 restoreMenu(scr, menu, WSS_WSMENU);
2571 PLRelease(rkey);
2572 PLRelease(skey);
2573 PLRelease(wkey);
2577 void
2578 OpenWorkspaceMenu(WScreen *scr, int x, int y)
2580 WMenu *menu, *parent;
2581 WMenuEntry *entry;
2583 #ifndef LITE
2584 if (!scr->root_menu) {
2585 OpenRootMenu(scr, scr->scr_width*2, 0, False);
2586 wMenuUnmap(scr->root_menu);
2588 #endif
2589 menu = scr->workspace_menu;
2590 if (menu) {
2591 if (menu->flags.mapped) {
2592 if (!menu->flags.buttoned) {
2593 wMenuUnmap(menu);
2594 parent = menu->parent;
2595 if (parent && parent->selected_entry >= 0) {
2596 entry = parent->entries[parent->selected_entry];
2597 if (parent->cascades[entry->cascade] == menu) {
2598 selectEntry(parent, -1);
2599 wMenuMapAt(menu, x, y, False);
2602 } else {
2603 wRaiseFrame(menu->frame->core);
2604 wMenuMapCopyAt(menu, x, y);
2607 else {
2608 wMenuMapAt(menu, x, y, False);