Added the crash dialog panel
[wmaker-crm.git] / src / menu.c
blob896e1edc833c2f68f219c26812281296910a33cd
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_texture_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 void
1745 wMenuScroll(WMenu *menu, XEvent *event)
1747 WMenu *smenu;
1748 WMenu *omenu = parentMenu(menu);
1749 WScreen *scr = menu->frame->screen_ptr;
1750 int done = 0;
1751 int jump_back = 0;
1752 int old_frame_x = omenu->frame_x;
1753 int old_frame_y = omenu->frame_y;
1754 XEvent ev;
1756 #ifdef DEBUG
1757 puts("Entering menu Scroll");
1758 #endif
1760 if ((/*omenu->flags.buttoned &&*/ !wPreferences.wrap_menus)
1761 || omenu->flags.app_menu) {
1762 jump_back = 1;
1765 if (!wPreferences.wrap_menus)
1766 raiseMenus(omenu, True);
1767 else
1768 raiseMenus(menu, False);
1770 if (!menu->timer)
1771 scrollMenuCallback(menu);
1773 while(!done) {
1774 int x, y, on_border, on_x_edge, on_y_edge, on_title;
1776 WMNextEvent(dpy, &ev);
1777 switch (ev.type) {
1778 case EnterNotify:
1779 WMHandleEvent(&ev);
1780 case MotionNotify:
1781 x = (ev.type==MotionNotify) ? ev.xmotion.x_root : ev.xcrossing.x_root;
1782 y = (ev.type==MotionNotify) ? ev.xmotion.y_root : ev.xcrossing.y_root;
1784 /* on_border is != 0 if the pointer is between the menu
1785 * and the screen border and is close enough to the border */
1786 on_border = isPointNearBoder(menu, x, y);
1788 smenu = wMenuUnderPointer(scr);
1790 if ((smenu==NULL && !on_border) || (smenu && parentMenu(smenu)!=omenu)) {
1791 done = 1;
1792 break;
1795 on_x_edge = x <= 1 || x >= scr->scr_width - 2;
1796 on_y_edge = y <= 1 || y >= scr->scr_height - 2;
1797 on_border = on_x_edge || on_y_edge;
1799 if (!on_border && !jump_back) {
1800 done = 1;
1801 break;
1804 if (menu->timer && (smenu!=menu || (!on_y_edge && !on_x_edge))) {
1805 WMDeleteTimerHandler(menu->timer);
1806 menu->timer = NULL;
1809 if (smenu != NULL)
1810 menu = smenu;
1812 if (!menu->timer)
1813 scrollMenuCallback(menu);
1814 break;
1815 case ButtonPress:
1816 /* True if we push on title, or drag the omenu to other position */
1817 on_title = ev.xbutton.x_root >= omenu->frame_x &&
1818 ev.xbutton.x_root <= omenu->frame_x + MENUW(omenu) &&
1819 ev.xbutton.y_root >= omenu->frame_y &&
1820 ev.xbutton.y_root <= omenu->frame_y + omenu->frame->top_width;
1821 WMHandleEvent(&ev);
1822 smenu = wMenuUnderPointer(scr);
1823 if (smenu == NULL || (smenu && smenu->flags.buttoned && smenu != omenu))
1824 done = 1;
1825 else if (smenu==omenu && on_title) {
1826 jump_back = 0;
1827 done = 1;
1829 break;
1830 case KeyPress:
1831 done = 1;
1832 default:
1833 WMHandleEvent(&ev);
1834 break;
1838 if (menu->timer) {
1839 WMDeleteTimerHandler(menu->timer);
1840 menu->timer = NULL;
1843 if (jump_back)
1844 wMenuMove(omenu, old_frame_x, old_frame_y, True);
1846 #ifdef DEBUG
1847 puts("Leaving menu Scroll");
1848 #endif
1853 static void
1854 menuExpose(WObjDescriptor *desc, XEvent *event)
1856 wMenuPaint(desc->parent);
1859 typedef struct {
1860 int *delayed_select;
1861 WMenu *menu;
1862 WMHandlerID magic;
1863 } delay_data;
1866 static void
1867 delaySelection(void *data)
1869 delay_data *d = (delay_data*)data;
1870 int x, y, entry_no;
1871 WMenu *menu;
1873 d->magic = NULL;
1875 menu = findMenu(d->menu->menu->screen_ptr, &x, &y);
1876 if (menu && (d->menu == menu || d->delayed_select)) {
1877 entry_no = getEntryAt(menu, x, y);
1878 selectEntry(menu, entry_no);
1880 if (d->delayed_select)
1881 *(d->delayed_select) = 0;
1885 static void
1886 menuMouseDown(WObjDescriptor *desc, XEvent *event)
1888 XButtonEvent *bev = &event->xbutton;
1889 WMenu *menu = desc->parent;
1890 WMenu *smenu;
1891 WScreen *scr=menu->frame->screen_ptr;
1892 WMenuEntry *entry=NULL;
1893 XEvent ev;
1894 int close_on_exit=0;
1895 int done=0;
1896 int delayed_select = 0;
1897 int entry_no;
1898 int x, y;
1899 int prevx, prevy;
1900 int old_frame_x = 0;
1901 int old_frame_y = 0;
1902 delay_data d_data = {NULL, NULL, NULL};
1904 if (!wPreferences.wrap_menus) {
1905 smenu = parentMenu(menu);
1906 old_frame_x = smenu->frame_x;
1907 old_frame_y = smenu->frame_y;
1909 else if (event->xbutton.window == menu->frame->core->window) {
1910 /* This is true if the menu was launched with right click on root window */
1911 delayed_select = 1;
1912 d_data.delayed_select = &delayed_select;
1913 d_data.menu = menu;
1914 d_data.magic = WMAddTimerHandler(wPreferences.dblclick_time,
1915 delaySelection, &d_data);
1918 wRaiseFrame(menu->frame->core);
1920 close_on_exit = (bev->send_event || menu->flags.brother);
1922 smenu = findMenu(scr, &x, &y);
1923 if (!smenu) {
1924 x = -1;
1925 y = -1;
1926 } else {
1927 menu = smenu;
1930 if (menu->flags.editing) {
1931 return;
1933 entry_no = getEntryAt(menu, x, y);
1934 if (entry_no>=0) {
1935 entry = menu->entries[entry_no];
1937 if (!close_on_exit && (bev->state & ControlMask) && smenu
1938 && entry->flags.editable) {
1939 editEntry(smenu, entry);
1940 return;
1941 } else if (bev->state & ControlMask) {
1942 return;
1945 if (entry->flags.enabled && entry->cascade>=0 && menu->cascades) {
1946 WMenu *submenu = menu->cascades[entry->cascade];
1947 /* map cascade */
1948 if (submenu->flags.mapped && !submenu->flags.buttoned &&
1949 menu->selected_entry!=entry_no) {
1950 wMenuUnmap(submenu);
1952 if (!submenu->flags.mapped && !delayed_select) {
1953 selectEntry(menu, entry_no);
1954 } else if (!submenu->flags.buttoned) {
1955 selectEntry(menu, -1);
1958 } else if (!delayed_select) {
1959 selectEntry(menu, entry_no);
1962 if (!wPreferences.wrap_menus && !wPreferences.scrollable_menus) {
1963 if (!menu->timer)
1964 dragScrollMenuCallback(menu);
1968 prevx = bev->x_root;
1969 prevy = bev->y_root;
1970 while (!done) {
1971 int x, y;
1973 XAllowEvents(dpy, SyncPointer, CurrentTime);
1975 WMMaskEvent(dpy, ExposureMask|ButtonMotionMask|ButtonReleaseMask
1976 |ButtonPressMask, &ev);
1977 switch (ev.type) {
1978 case MotionNotify:
1979 smenu = findMenu(scr, &x, &y);
1981 if (smenu == NULL) {
1982 /* moved mouse out of menu */
1984 if (!delayed_select && d_data.magic) {
1985 WMDeleteTimerHandler(d_data.magic);
1986 d_data.magic = NULL;
1988 if (menu==NULL
1989 || (menu->selected_entry>=0
1990 && menu->entries[menu->selected_entry]->cascade>=0)) {
1991 prevx = ev.xmotion.x_root;
1992 prevy = ev.xmotion.y_root;
1994 break;
1996 selectEntry(menu, -1);
1997 menu = smenu;
1998 prevx = ev.xmotion.x_root;
1999 prevy = ev.xmotion.y_root;
2000 break;
2001 } else if (menu && menu!=smenu
2002 && (menu->selected_entry<0
2003 || menu->entries[menu->selected_entry]->cascade<0)) {
2004 selectEntry(menu, -1);
2006 if (!delayed_select && d_data.magic) {
2007 WMDeleteTimerHandler(d_data.magic);
2008 d_data.magic = NULL;
2010 } else {
2012 /* hysteresis for item selection */
2014 /* check if the motion was to the side, indicating that
2015 * the user may want to cross to a submenu */
2016 if (!delayed_select && menu) {
2017 int dx;
2018 Bool moved_to_submenu;/* moved to direction of submenu */
2020 dx = abs(prevx - ev.xmotion.x_root);
2022 moved_to_submenu = False;
2023 if (dx > 0 /* if moved enough to the side */
2024 /* maybe a open submenu */
2025 && menu->selected_entry>=0
2026 /* moving to the right direction */
2027 && (wPreferences.align_menus
2028 || ev.xmotion.y_root >= prevy)) {
2029 int index;
2031 index = menu->entries[menu->selected_entry]->cascade;
2032 if (index>=0) {
2033 if (menu->cascades[index]->frame_x>menu->frame_x) {
2034 if (prevx < ev.xmotion.x_root)
2035 moved_to_submenu = True;
2036 } else {
2037 if (prevx > ev.xmotion.x_root)
2038 moved_to_submenu = True;
2044 if (menu != smenu) {
2045 if (d_data.magic) {
2046 WMDeleteTimerHandler(d_data.magic);
2048 d_data.magic = NULL;
2049 } else if (moved_to_submenu) {
2050 /* while we are moving, postpone the selection */
2051 if (d_data.magic) {
2052 WMDeleteTimerHandler(d_data.magic);
2054 d_data.delayed_select = NULL;
2055 d_data.menu = menu;
2056 d_data.magic = WMAddTimerHandler(MENU_SELECT_DELAY,
2057 delaySelection,
2058 &d_data);
2059 prevx = ev.xmotion.x_root;
2060 prevy = ev.xmotion.y_root;
2061 break;
2062 } else {
2063 if (d_data.magic)
2064 WMDeleteTimerHandler(d_data.magic);
2065 d_data.magic = NULL;
2069 prevx = ev.xmotion.x_root;
2070 prevy = ev.xmotion.y_root;
2071 if (menu!=smenu) {
2072 /* pointer crossed menus */
2073 if (menu && menu->timer) {
2074 WMDeleteTimerHandler(menu->timer);
2075 menu->timer = NULL;
2077 if (smenu)
2078 dragScrollMenuCallback(smenu);
2080 menu = smenu;
2081 if (!menu->timer)
2082 dragScrollMenuCallback(menu);
2084 if (!delayed_select) {
2085 entry_no = getEntryAt(menu, x, y);
2086 if (entry_no>=0) {
2087 entry = menu->entries[entry_no];
2088 if (entry->flags.enabled && entry->cascade>=0 &&
2089 menu->cascades) {
2090 WMenu *submenu = menu->cascades[entry->cascade];
2091 if (submenu->flags.mapped && !submenu->flags.buttoned
2092 && menu->selected_entry!=entry_no) {
2093 wMenuUnmap(submenu);
2097 selectEntry(menu, entry_no);
2099 break;
2101 case ButtonPress:
2102 break;
2104 case ButtonRelease:
2105 if (ev.xbutton.button == event->xbutton.button)
2106 done=1;
2107 break;
2109 case Expose:
2110 WMHandleEvent(&ev);
2111 break;
2115 if (menu && menu->timer) {
2116 WMDeleteTimerHandler(menu->timer);
2117 menu->timer = NULL;
2119 if (d_data.magic!=NULL)
2120 WMDeleteTimerHandler(d_data.magic);
2122 if (menu && menu->selected_entry>=0) {
2123 entry = menu->entries[menu->selected_entry];
2124 if (entry->callback!=NULL && entry->flags.enabled
2125 && entry->cascade < 0) {
2126 /* blink and erase menu selection */
2127 #if (MENU_BLINK_DELAY > 0)
2128 int sel = menu->selected_entry;
2129 int i;
2131 for (i=0; i<MENU_BLINK_COUNT; i++) {
2132 paintEntry(menu, sel, False);
2133 XSync(dpy, 0);
2134 wusleep(MENU_BLINK_DELAY);
2135 paintEntry(menu, sel, True);
2136 XSync(dpy, 0);
2137 wusleep(MENU_BLINK_DELAY);
2139 #endif
2140 /* unmap the menu, it's parents and call the callback */
2141 if (!menu->flags.buttoned &&
2142 (!menu->flags.app_menu||menu->parent!=NULL)) {
2143 closeCascade(menu);
2144 } else {
2145 selectEntry(menu, -1);
2147 (*entry->callback)(menu, entry);
2149 /* If the user double clicks an entry, the entry will
2150 * be executed twice, which is not good for things like
2151 * the root menu. So, ignore any clicks that were generated
2152 * while the entry was being executed */
2153 while (XCheckTypedWindowEvent(dpy, menu->menu->window,
2154 ButtonPress, &ev));
2155 } else if (entry->callback!=NULL && entry->cascade<0) {
2156 selectEntry(menu, -1);
2157 } else {
2158 if (entry->cascade>=0 && menu->cascades
2159 && menu->cascades[entry->cascade]->flags.brother) {
2160 selectEntry(menu, -1);
2165 if (((WMenu*)desc->parent)->flags.brother || close_on_exit
2166 || !smenu)
2167 closeCascade(desc->parent);
2169 /* close the cascade windows that should not remain opened */
2170 closeBrotherCascadesOf(desc->parent);
2172 if (!wPreferences.wrap_menus)
2173 wMenuMove(parentMenu(desc->parent), old_frame_x, old_frame_y, True);
2177 void
2178 wMenuMove(WMenu *menu, int x, int y, int submenus)
2180 WMenu *submenu;
2181 int i;
2183 if (!menu) return;
2185 menu->frame_x = x;
2186 menu->frame_y = y;
2187 XMoveWindow(dpy, menu->frame->core->window, x, y);
2189 if (submenus>0 && menu->selected_entry>=0) {
2190 i = menu->entries[menu->selected_entry]->cascade;
2192 if (i>=0 && menu->cascades) {
2193 submenu = menu->cascades[i];
2194 if (submenu->flags.mapped && !submenu->flags.buttoned)
2195 if (wPreferences.align_menus) {
2196 wMenuMove(submenu, x + MENUW(menu), y, submenus);
2197 } else {
2198 wMenuMove(submenu, x+ MENUW(menu),
2199 y + submenu->entry_height*menu->selected_entry,
2200 submenus);
2204 if (submenus<0 && menu->parent!=NULL && menu->parent->flags.mapped &&
2205 !menu->parent->flags.buttoned) {
2206 if (wPreferences.align_menus) {
2207 wMenuMove(menu->parent, x - MENUW(menu->parent), y, submenus);
2208 } else {
2209 wMenuMove(menu->parent, x - MENUW(menu->parent), menu->frame_y
2210 - menu->parent->entry_height*menu->parent->selected_entry,
2211 submenus);
2217 static void
2218 changeMenuLevels(WMenu *menu, int lower)
2220 int i;
2222 if (!lower) {
2223 ChangeStackingLevel(menu->frame->core, (!menu->parent ? WMMainMenuLevel
2224 : WMSubmenuLevel));
2225 wRaiseFrame(menu->frame->core);
2226 menu->flags.lowered = 0;
2227 } else {
2228 ChangeStackingLevel(menu->frame->core, WMNormalLevel);
2229 wLowerFrame(menu->frame->core);
2230 menu->flags.lowered = 1;
2232 for (i=0; i<menu->cascade_no; i++) {
2233 if (menu->cascades[i]
2234 && !menu->cascades[i]->flags.buttoned
2235 && menu->cascades[i]->flags.lowered!=lower) {
2236 changeMenuLevels(menu->cascades[i], lower);
2243 static void
2244 menuTitleDoubleClick(WCoreWindow *sender, void *data, XEvent *event)
2246 WMenu *menu = data;
2247 int lower;
2249 if (event->xbutton.state & MOD_MASK) {
2250 if (menu->flags.lowered) {
2251 lower = 0;
2252 } else {
2253 lower = 1;
2255 changeMenuLevels(menu, lower);
2260 static void
2261 menuTitleMouseDown(WCoreWindow *sender, void *data, XEvent *event)
2263 WMenu *menu = data;
2264 WMenu *tmp;
2265 XEvent ev;
2266 int x=menu->frame_x, y=menu->frame_y;
2267 int dx=event->xbutton.x_root, dy=event->xbutton.y_root;
2268 int i, lower;
2269 Bool started;
2271 #ifdef DEBUG
2272 printf("Moving menu\n");
2273 #endif
2275 /* can't touch the menu copy */
2276 if (menu->flags.brother)
2277 return;
2279 if (event->xbutton.button != Button1 && event->xbutton.button != Button2)
2280 return;
2282 if (event->xbutton.state & MOD_MASK) {
2283 wLowerFrame(menu->frame->core);
2284 lower = 1;
2285 } else {
2286 wRaiseFrame(menu->frame->core);
2287 lower = 0;
2289 tmp = menu;
2291 /* lower/raise all submenus */
2292 while (1) {
2293 if (tmp->selected_entry>=0 && tmp->cascades
2294 && tmp->entries[tmp->selected_entry]->cascade>=0) {
2295 tmp = tmp->cascades[tmp->entries[tmp->selected_entry]->cascade];
2296 if (!tmp || !tmp->flags.mapped)
2297 break;
2298 if (lower)
2299 wLowerFrame(tmp->frame->core);
2300 else
2301 wRaiseFrame(tmp->frame->core);
2302 } else {
2303 break;
2307 /* tear off the menu if it's a root menu or a cascade
2308 application menu */
2309 if (!menu->flags.buttoned && !menu->flags.brother
2310 && (!menu->flags.app_menu||menu->parent!=NULL)) {
2311 menu->flags.buttoned=1;
2312 wFrameWindowShowButton(menu->frame, WFF_RIGHT_BUTTON);
2313 if (menu->parent) {
2314 /* turn off selected menu entry in parent menu */
2315 selectEntry(menu->parent, -1);
2317 /* make parent map the copy in place of the original */
2318 for (i=0; i<menu->parent->cascade_no; i++) {
2319 if (menu->parent->cascades[i] == menu) {
2320 menu->parent->cascades[i] = menu->brother;
2321 break;
2327 started = False;
2328 while(1) {
2329 WMMaskEvent(dpy, ButtonMotionMask|ButtonReleaseMask|ButtonPressMask
2330 |ExposureMask, &ev);
2331 switch (ev.type) {
2332 case MotionNotify:
2333 if (started) {
2334 x += ev.xmotion.x_root - dx;
2335 y += ev.xmotion.y_root - dy;
2336 dx = ev.xmotion.x_root;
2337 dy = ev.xmotion.y_root;
2338 wMenuMove(menu, x, y, True);
2339 } else {
2340 if (abs(ev.xmotion.x_root - dx) > MOVE_THRESHOLD
2341 || abs(ev.xmotion.y_root - dy) > MOVE_THRESHOLD) {
2342 started = True;
2343 XGrabPointer(dpy, menu->frame->titlebar->window, False,
2344 ButtonMotionMask|ButtonReleaseMask
2345 |ButtonPressMask,
2346 GrabModeAsync, GrabModeAsync, None,
2347 wCursor[WCUR_MOVE], CurrentTime);
2350 break;
2352 case ButtonPress:
2353 break;
2355 case ButtonRelease:
2356 if (ev.xbutton.button != event->xbutton.button)
2357 break;
2358 #ifdef DEBUG
2359 printf("End menu move\n");
2360 #endif
2361 XUngrabPointer(dpy, CurrentTime);
2362 return;
2364 default:
2365 WMHandleEvent(&ev);
2366 break;
2372 *----------------------------------------------------------------------
2373 * menuCloseClick--
2374 * Handles mouse click on the close button of menus. The menu is
2375 * closed when the button is clicked.
2377 * Side effects:
2378 * The closed menu is reinserted at it's parent menus
2379 * cascade list.
2380 *----------------------------------------------------------------------
2382 static void
2383 menuCloseClick(WCoreWindow *sender, void *data, XEvent *event)
2385 WMenu *menu = (WMenu*)data;
2386 WMenu *parent = menu->parent;
2387 int i;
2389 if (parent) {
2390 for (i=0; i<parent->cascade_no; i++) {
2391 /* find the entry that points to the copy */
2392 if (parent->cascades[i] == menu->brother) {
2393 /* make it point to the original */
2394 parent->cascades[i] = menu;
2395 menu->parent = parent;
2396 break;
2400 wMenuUnmap(menu);
2404 void
2405 wMenuSaveState(WScreen *scr)
2407 proplist_t menus, key, value;
2408 int save_menus = 0;
2409 char buffer[256];
2411 menus = PLMakeDictionaryFromEntries(NULL, NULL, NULL);
2413 #ifndef LITE
2414 if (scr->root_menu && scr->root_menu->flags.buttoned) {
2415 sprintf(buffer, "%i,%i", scr->root_menu->frame_x,
2416 scr->root_menu->frame_y);
2417 key = PLMakeString("RootMenu");
2418 value = PLMakeString(buffer);
2419 PLInsertDictionaryEntry(menus, key, value);
2420 PLRelease(key);
2421 PLRelease(value);
2422 save_menus = 1;
2425 if (scr->switch_menu && scr->switch_menu->flags.buttoned) {
2426 sprintf(buffer, "%i,%i", scr->switch_menu->frame_x,
2427 scr->switch_menu->frame_y);
2428 key = PLMakeString("SwitchMenu");
2429 value = PLMakeString(buffer);
2430 PLInsertDictionaryEntry(menus, key, value);
2431 PLRelease(key);
2432 PLRelease(value);
2433 save_menus = 1;
2435 #endif /* !LITE */
2436 if (scr->workspace_menu && scr->workspace_menu->flags.buttoned) {
2437 sprintf(buffer, "%i,%i", scr->workspace_menu->frame_x,
2438 scr->workspace_menu->frame_y);
2439 key = PLMakeString("WorkspaceMenu");
2440 value = PLMakeString(buffer);
2441 PLInsertDictionaryEntry(menus, key, value);
2442 PLRelease(key);
2443 PLRelease(value);
2444 save_menus = 1;
2447 if (save_menus) {
2448 key = PLMakeString("Menus");
2449 PLInsertDictionaryEntry(scr->session_state, key, menus);
2450 PLRelease(key);
2452 PLRelease(menus);
2456 #define COMPLAIN(key) wwarning(_("bad value in menus state info:%s"), key)
2459 static int
2460 restoreMenu(WScreen *scr, proplist_t menu, int which)
2462 int i, x, y;
2463 WMenu *pmenu = NULL;
2465 if (!menu)
2466 return False;
2468 if (!PLIsString(menu)) {
2469 COMPLAIN("Position");
2470 return False;
2473 if (sscanf(PLGetString(menu), "%i,%i", &x, &y)!=2)
2474 COMPLAIN("Position");
2476 #ifndef LITE
2477 if (which & WSS_ROOTMENU) {
2478 OpenRootMenu(scr, x, y, False);
2479 pmenu = scr->root_menu;
2480 } else if (which & WSS_SWITCHMENU) {
2481 OpenSwitchMenu(scr, x, y, False);
2482 pmenu = scr->switch_menu;
2483 } else
2484 #endif /* !LITE */
2485 if (which & WSS_WSMENU) {
2486 OpenWorkspaceMenu(scr, x, y);
2487 pmenu = scr->workspace_menu;
2488 if (pmenu->parent) {
2489 /* make parent map the copy in place of the original */
2490 for (i=0; i<pmenu->parent->cascade_no; i++) {
2491 if (pmenu->parent->cascades[i] == pmenu) {
2492 pmenu->parent->cascades[i] = pmenu->brother;
2493 break;
2499 if (pmenu) {
2500 int width = MENUW(pmenu);
2501 int height = MENUH(pmenu);
2503 x = (x < -width) ? 0 : x;
2504 x = (x > scr->scr_width) ? scr->scr_width - width : x;
2505 y = (y < 0) ? 0 : y;
2506 y = (y > scr->scr_height) ? scr->scr_height - height : y;
2507 wMenuMove(pmenu, x, y, True);
2508 pmenu->flags.buttoned = 1;
2509 wFrameWindowShowButton(pmenu->frame, WFF_RIGHT_BUTTON);
2510 return True;
2512 return False;
2516 void
2517 wMenuRestoreState(WScreen *scr)
2519 proplist_t menus, menu, key, rkey, skey, wkey;
2521 key = PLMakeString("Menus");
2522 menus = PLGetDictionaryEntry(scr->session_state, key);
2523 PLRelease(key);
2525 if (!menus)
2526 return;
2528 /* restore menus */
2530 rkey = PLMakeString("RootMenu");
2531 skey = PLMakeString("SwitchMenu");
2532 wkey = PLMakeString("WorkspaceMenu");
2533 menu = PLGetDictionaryEntry(menus, rkey);
2534 restoreMenu(scr, menu, WSS_ROOTMENU);
2535 menu = PLGetDictionaryEntry(menus, skey);
2536 restoreMenu(scr, menu, WSS_SWITCHMENU);
2537 menu = PLGetDictionaryEntry(menus, wkey);
2538 restoreMenu(scr, menu, WSS_WSMENU);
2540 PLRelease(rkey);
2541 PLRelease(skey);
2542 PLRelease(wkey);
2546 void
2547 OpenWorkspaceMenu(WScreen *scr, int x, int y)
2549 WMenu *menu, *parent;
2550 WMenuEntry *entry;
2552 #ifndef LITE
2553 if (!scr->root_menu) {
2554 OpenRootMenu(scr, scr->scr_width*2, 0, False);
2555 wMenuUnmap(scr->root_menu);
2557 #endif
2558 menu = scr->workspace_menu;
2559 if (menu) {
2560 if (menu->flags.mapped) {
2561 if (!menu->flags.buttoned) {
2562 wMenuUnmap(menu);
2563 parent = menu->parent;
2564 if (parent && parent->selected_entry >= 0) {
2565 entry = parent->entries[parent->selected_entry];
2566 if (parent->cascades[entry->cascade] == menu) {
2567 selectEntry(parent, -1);
2568 wMenuMapAt(menu, x, y, False);
2571 } else {
2572 wRaiseFrame(menu->frame->core);
2573 wMenuMapCopyAt(menu, x, y);
2576 else {
2577 wMenuMapAt(menu, x, y, False);