testing new loginfo script
[wmaker-crm.git] / src / menu.c
blobe3d896af2ee9362ee47a0a3723c10d3f855d5bd4
1 /* menu.c- generic menu, used for root menu, application menus etc.
3 * Window Maker window manager
4 *
5 * Copyright (c) 1997, 1998 Alfredo K. Kojima
6 * Copyright (c) 1997, 1998 Dan Pascu
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
21 * USA.
24 #include "wconfig.h"
26 #include <X11/Xlib.h>
27 #include <X11/Xutil.h>
28 #include <X11/keysym.h>
29 #include <stdlib.h>
30 #include <string.h>
31 #include <stdio.h>
32 #include <unistd.h>
33 #include <ctype.h>
34 #if 0
35 #include <nana.h>
36 #endif
37 #include "WindowMaker.h"
38 #include "wcore.h"
39 #include "framewin.h"
40 #include "menu.h"
41 #include "actions.h"
42 #include "funcs.h"
43 #include "stacking.h"
44 #include "text.h"
47 /****** Global Variables ******/
49 extern Cursor wCursor[WCUR_LAST];
51 extern XContext wWinContext;
53 extern WPreferences wPreferences;
55 #define MOD_MASK wPreferences.modifier_mask
57 #define MENU_SCROLL_STEP menuScrollParameters[(int)wPreferences.menu_scroll_speed].steps
58 #define MENU_SCROLL_DELAY menuScrollParameters[(int)wPreferences.menu_scroll_speed].delay
62 #define MENUW(m) ((m)->frame->core->width+2*FRAME_BORDER_WIDTH)
63 #define MENUH(m) ((m)->frame->core->height+2*FRAME_BORDER_WIDTH)
66 /***** Local Stuff ******/
68 static struct {
69 int steps;
70 int delay;
71 } menuScrollParameters[5] = {
72 {MENU_SCROLL_STEPS_UF, MENU_SCROLL_DELAY_UF},
73 {MENU_SCROLL_STEPS_F, MENU_SCROLL_DELAY_F},
74 {MENU_SCROLL_STEPS_M, MENU_SCROLL_DELAY_M},
75 {MENU_SCROLL_STEPS_S, MENU_SCROLL_DELAY_S},
76 {MENU_SCROLL_STEPS_U, MENU_SCROLL_DELAY_U}};
79 static void menuMouseDown(WObjDescriptor *desc, XEvent *event);
80 static void menuExpose(WObjDescriptor *desc, XEvent *event);
82 static void menuTitleDoubleClick(WCoreWindow *sender, void *data, XEvent *event);
83 static void menuTitleMouseDown(WCoreWindow *sender, void *data, XEvent *event);
85 static void menuCloseClick(WCoreWindow *sender, void *data, XEvent *event);
87 static void updateTexture(WMenu *menu);
89 #ifndef LITE
90 static int saveMenuRecurs(proplist_t menus, WScreen *scr, WMenu *menu);
91 static int restoreMenuRecurs(WScreen *scr, proplist_t menus, WMenu *menu, char *path);
92 #endif /* !LITE */
94 static void selectEntry(WMenu *menu, int entry_no);
95 static void closeCascade(WMenu *menu);
98 /****** Notification Observers ******/
100 static void
101 appearanceObserver(void *self, WMNotification *notif)
103 WMenu *menu = (WMenu*)self;
104 int flags = (int)WMGetNotificationClientData(notif);
106 if (!menu->flags.realized)
107 return;
109 if (WMGetNotificationName(notif) == WNMenuAppearanceSettingsChanged) {
110 if (flags & WFontSettings) {
111 menu->flags.realized = 0;
112 wMenuRealize(menu);
114 if (flags & WTextureSettings) {
115 if (!menu->flags.brother)
116 updateTexture(menu);
118 if (flags & (WTextureSettings|WColorSettings)) {
119 wMenuPaint(menu);
121 } else if (menu->flags.titled) {
123 if (flags & WFontSettings) {
124 menu->flags.realized = 0;
125 wMenuRealize(menu);
127 if (flags & WTextureSettings) {
128 menu->frame->flags.need_texture_remake = 1;
130 if (flags & (WColorSettings|WTextureSettings)) {
131 wFrameWindowPaint(menu->frame);
136 /************************************/
140 *----------------------------------------------------------------------
141 * wMenuCreate--
142 * Creates a new empty menu with the specified title. If main_menu
143 * is True, the created menu will be a main menu, which has some special
144 * properties such as being placed over other normal menus.
145 * If title is NULL, the menu will have no titlebar.
147 * Returns:
148 * The created menu.
149 *----------------------------------------------------------------------
151 WMenu*
152 wMenuCreate(WScreen *screen, char *title, int main_menu)
154 WMenu *menu;
155 static int brother=0;
156 int tmp, flags;
158 menu = wmalloc(sizeof(WMenu));
160 memset(menu, 0, sizeof(WMenu));
162 #ifdef SINGLE_MENULEVEL
163 tmp = WMSubmenuLevel;
164 #else
165 tmp = (main_menu ? WMMainMenuLevel : WMSubmenuLevel);
166 #endif
168 flags = WFF_SINGLE_STATE|WFF_BORDER;
169 if (title) {
170 flags |= WFF_TITLEBAR|WFF_RIGHT_BUTTON;
171 menu->flags.titled = 1;
173 menu->frame =
174 wFrameWindowCreate(screen, tmp, 8, 2, 1, 1, &wPreferences.menu_title_clearance, flags,
175 screen->menu_title_texture, NULL,
176 screen->menu_title_pixel,
177 &screen->menu_title_gc,
178 &screen->menu_title_font);
180 menu->frame->core->descriptor.parent = menu;
181 menu->frame->core->descriptor.parent_type = WCLASS_MENU;
182 menu->frame->core->descriptor.handle_mousedown = menuMouseDown;
184 wFrameWindowHideButton(menu->frame, WFF_RIGHT_BUTTON);
186 if (title) {
187 menu->frame->title = wstrdup(title);
190 menu->frame->flags.justification = WTJ_LEFT;
192 menu->frame->rbutton_image = screen->b_pixmaps[WBUT_CLOSE];
194 menu->entry_no = 0;
195 menu->alloced_entries = 0;
196 menu->selected_entry = -1;
197 menu->entries = NULL;
199 menu->frame_x = screen->app_menu_x;
200 menu->frame_y = screen->app_menu_y;
202 menu->frame->child = menu;
204 menu->flags.lowered = 0;
206 /* create borders */
207 if (title) {
208 /* setup object descriptors */
209 menu->frame->on_mousedown_titlebar = menuTitleMouseDown;
210 menu->frame->on_dblclick_titlebar = menuTitleDoubleClick;
213 menu->frame->on_click_right = menuCloseClick;
216 menu->menu = wCoreCreate(menu->frame->core, 0, menu->frame->top_width,
217 menu->frame->core->width, 10);
219 menu->menu->descriptor.parent = menu;
220 menu->menu->descriptor.parent_type = WCLASS_MENU;
221 menu->menu->descriptor.handle_expose = menuExpose;
222 menu->menu->descriptor.handle_mousedown = menuMouseDown;
224 menu->menu_texture_data = None;
226 XMapWindow(dpy, menu->menu->window);
228 XFlush(dpy);
230 if (!brother) {
231 brother = 1;
232 menu->brother = wMenuCreate(screen, title, main_menu);
233 brother = 0;
234 menu->brother->flags.brother = 1;
235 menu->brother->brother = menu;
237 WMAddNotificationObserver(appearanceObserver, menu,
238 WNMenuAppearanceSettingsChanged, menu);
240 WMAddNotificationObserver(appearanceObserver, menu,
241 WNMenuTitleAppearanceSettingsChanged, menu);
244 return menu;
250 WMenu*
251 wMenuCreateForApp(WScreen *screen, char *title, int main_menu)
253 WMenu *menu;
255 menu = wMenuCreate(screen, title, main_menu);
256 if (!menu)
257 return NULL;
258 menu->flags.app_menu = 1;
259 menu->brother->flags.app_menu = 1;
261 return menu;
266 static void
267 insertEntry(WMenu *menu, WMenuEntry *entry, int index)
269 int i;
271 for (i = menu->entry_no-1; i >= index; i--) {
272 menu->entries[i]->order++;
273 menu->entries[i+1] = menu->entries[i];
275 menu->entries[index] = entry;
279 WMenuEntry*
280 wMenuInsertCallback(WMenu *menu, int index, char *text,
281 void (*callback)(WMenu *menu, WMenuEntry *entry),
282 void *clientdata)
284 WMenuEntry *entry;
286 menu->flags.realized = 0;
287 menu->brother->flags.realized = 0;
289 /* reallocate array if it's too small */
290 if (menu->entry_no >= menu->alloced_entries) {
291 void *tmp;
293 tmp = wrealloc(menu->entries,
294 sizeof(WMenuEntry)*(menu->alloced_entries+5));
296 menu->entries = tmp;
297 menu->alloced_entries += 5;
299 menu->brother->entries = tmp;
300 menu->brother->alloced_entries = menu->alloced_entries;
302 entry = wmalloc(sizeof(WMenuEntry));
303 memset(entry, 0, sizeof(WMenuEntry));
304 entry->flags.enabled = 1;
305 entry->text = wstrdup(text);
306 entry->cascade = -1;
307 entry->clientdata = clientdata;
308 entry->callback = callback;
309 if (index<0 || index>=menu->entry_no) {
310 entry->order = menu->entry_no;
311 menu->entries[menu->entry_no] = entry;
312 } else {
313 entry->order = index;
314 insertEntry(menu, entry, index);
317 menu->entry_no++;
318 menu->brother->entry_no = menu->entry_no;
320 return entry;
325 void
326 wMenuEntrySetCascade(WMenu *menu, WMenuEntry *entry, WMenu *cascade)
328 WMenu *brother = menu->brother;
329 int i, done;
331 assert(menu->flags.brother==0);
333 if (entry->cascade>=0) {
334 menu->flags.realized = 0;
335 brother->flags.realized = 0;
338 cascade->parent = menu;
340 cascade->brother->parent = brother;
342 done = 0;
343 for (i=0; i<menu->cascade_no; i++) {
344 if (menu->cascades[i]==NULL) {
345 menu->cascades[i] = cascade;
346 brother->cascades[i] = cascade->brother;
347 done = 1;
348 entry->cascade = i;
349 break;
352 if (!done) {
353 entry->cascade = menu->cascade_no;
355 menu->cascades = wrealloc(menu->cascades,
356 sizeof(WMenu)*(menu->cascade_no+1));
357 menu->cascades[menu->cascade_no++] = cascade;
360 brother->cascades = wrealloc(brother->cascades,
361 sizeof(WMenu)*(brother->cascade_no+1));
362 brother->cascades[brother->cascade_no++] = cascade->brother;
366 if (menu->flags.lowered) {
368 cascade->flags.lowered = 1;
369 ChangeStackingLevel(cascade->frame->core, WMNormalLevel);
371 cascade->brother->flags.lowered = 1;
372 ChangeStackingLevel(cascade->brother->frame->core, WMNormalLevel);
375 if (!menu->flags.realized)
376 wMenuRealize(menu);
380 void
381 wMenuEntryRemoveCascade(WMenu *menu, WMenuEntry *entry)
383 assert(menu->flags.brother==0);
385 /* destroy cascade menu */
386 if (entry->cascade>=0 && menu->cascades
387 && menu->cascades[entry->cascade]!=NULL) {
389 wMenuDestroy(menu->cascades[entry->cascade], True);
391 menu->cascades[entry->cascade] = NULL;
392 menu->brother->cascades[entry->cascade] = NULL;
394 entry->cascade = -1;
399 void
400 wMenuRemoveItem(WMenu *menu, int index)
402 int i;
404 if (menu->flags.brother) {
405 wMenuRemoveItem(menu->brother, index);
406 return;
409 if (index>=menu->entry_no) return;
411 /* destroy cascade menu */
412 wMenuEntryRemoveCascade(menu, menu->entries[index]);
414 /* destroy unshared data */
416 if (menu->entries[index]->text)
417 wfree(menu->entries[index]->text);
419 if (menu->entries[index]->rtext)
420 wfree(menu->entries[index]->rtext);
422 if (menu->entries[index]->free_cdata && menu->entries[index]->clientdata)
423 (*menu->entries[index]->free_cdata)(menu->entries[index]->clientdata);
425 wfree(menu->entries[index]);
427 for (i=index; i<menu->entry_no-1; i++) {
428 menu->entries[i+1]->order--;
429 menu->entries[i]=menu->entries[i+1];
431 menu->entry_no--;
432 menu->brother->entry_no--;
436 static Pixmap
437 renderTexture(WMenu *menu)
439 RImage *img;
440 Pixmap pix;
441 int i;
442 RColor light;
443 RColor dark;
444 RColor mid;
445 WScreen *scr = menu->menu->screen_ptr;
446 WTexture *texture = scr->menu_item_texture;
448 if (wPreferences.menu_style == MS_NORMAL) {
449 img = wTextureRenderImage(texture, menu->menu->width,
450 menu->entry_height, WREL_MENUENTRY);
451 } else {
452 img = wTextureRenderImage(texture, menu->menu->width,
453 menu->menu->height+1, WREL_MENUENTRY);
455 if (!img) {
456 wwarning(_("could not render texture: %s"),
457 RMessageForError(RErrorCode));
459 return None;
462 if (wPreferences.menu_style == MS_SINGLE_TEXTURE) {
463 light.alpha = 0;
464 light.red = light.green = light.blue = 80;
466 dark.alpha = 255;
467 dark.red = dark.green = dark.blue = 0;
469 mid.alpha = 0;
470 mid.red = mid.green = mid.blue = 40;
472 for (i = 1; i < menu->entry_no; i++) {
473 ROperateLine(img, RSubtractOperation, 0, i*menu->entry_height-2,
474 menu->menu->width-1, i*menu->entry_height-2, &mid);
476 RDrawLine(img, 0, i*menu->entry_height-1,
477 menu->menu->width-1, i*menu->entry_height-1, &dark);
479 ROperateLine(img, RAddOperation, 0, i*menu->entry_height,
480 menu->menu->width-1, i*menu->entry_height,
481 &light);
484 if (!RConvertImage(scr->rcontext, img, &pix)) {
485 wwarning(_("error rendering image:%s"), RMessageForError(RErrorCode));
487 RDestroyImage(img);
489 return pix;
493 static void
494 updateTexture(WMenu *menu)
496 WScreen *scr = menu->menu->screen_ptr;
498 /* setup background texture */
499 if (scr->menu_item_texture->any.type != WTEX_SOLID) {
500 if (!menu->flags.brother) {
501 FREE_PIXMAP(menu->menu_texture_data);
503 menu->menu_texture_data = renderTexture(menu);
505 XSetWindowBackgroundPixmap(dpy, menu->menu->window,
506 menu->menu_texture_data);
507 XClearWindow(dpy, menu->menu->window);
509 XSetWindowBackgroundPixmap(dpy, menu->brother->menu->window,
510 menu->menu_texture_data);
511 XClearWindow(dpy, menu->brother->menu->window);
513 } else {
514 XSetWindowBackground(dpy, menu->menu->window,
515 scr->menu_item_texture->any.color.pixel);
516 XClearWindow(dpy, menu->menu->window);
521 void
522 wMenuRealize(WMenu *menu)
524 int i;
525 int width, rwidth, mrwidth, mwidth;
526 int theight, twidth, eheight;
527 WScreen *scr = menu->frame->screen_ptr;
528 static int brother_done=0;
529 int flags;
531 if (!brother_done) {
532 brother_done = 1;
533 wMenuRealize(menu->brother);
534 brother_done = 0;
537 flags = WFF_SINGLE_STATE|WFF_BORDER;
538 if (menu->flags.titled)
539 flags |= WFF_TITLEBAR|WFF_RIGHT_BUTTON;
541 wFrameWindowUpdateBorders(menu->frame, flags);
543 if (menu->flags.titled) {
544 twidth = WMWidthOfString(scr->menu_title_font, menu->frame->title,
545 strlen(menu->frame->title));
546 theight = menu->frame->top_width;
547 twidth += theight + (wPreferences.new_style ? 16 : 8);
548 } else {
549 twidth = 0;
550 theight = 0;
552 eheight = WMFontHeight(scr->menu_entry_font) + 6 + wPreferences.menu_text_clearance * 2;
553 menu->entry_height = eheight;
554 mrwidth = 0;
555 mwidth = 0;
556 for (i=0; i<menu->entry_no; i++) {
557 char *text;
559 /* search widest text */
560 text = menu->entries[i]->text;
561 width = WMWidthOfString(scr->menu_entry_font, text, strlen(text))+10;
563 if (menu->entries[i]->flags.indicator) {
564 width += MENU_INDICATOR_SPACE;
567 if (width > mwidth)
568 mwidth = width;
570 /* search widest text on right */
571 text = menu->entries[i]->rtext;
572 if (text)
573 rwidth = WMWidthOfString(scr->menu_entry_font, text, strlen(text))
574 + 5;
575 else if (menu->entries[i]->cascade>=0)
576 rwidth = 16;
577 else
578 rwidth = 4;
580 if (rwidth > mrwidth)
581 mrwidth = rwidth;
583 mwidth += mrwidth;
585 if (mwidth < twidth)
586 mwidth = twidth;
589 wCoreConfigure(menu->menu, 0, theight, mwidth, menu->entry_no*eheight -1);
591 wFrameWindowResize(menu->frame, mwidth, menu->entry_no*eheight-1
592 + menu->frame->top_width + menu->frame->bottom_width);
595 updateTexture(menu);
597 menu->flags.realized = 1;
599 if (menu->flags.mapped)
600 wMenuPaint(menu);
601 if (menu->brother->flags.mapped)
602 wMenuPaint(menu->brother);
606 void
607 wMenuDestroy(WMenu *menu, int recurse)
609 int i;
611 WMRemoveNotificationObserver(menu);
613 /* remove any pending timers */
614 if (menu->timer)
615 WMDeleteTimerHandler(menu->timer);
616 menu->timer = NULL;
618 /* call destroy handler */
619 if (menu->on_destroy)
620 (*menu->on_destroy)(menu);
622 /* Destroy items if this menu own them. If this is the "brother" menu,
623 * leave them alone as it is shared by them.
625 if (!menu->flags.brother) {
626 for (i=0; i<menu->entry_no; i++) {
628 wfree(menu->entries[i]->text);
630 if (menu->entries[i]->rtext)
631 wfree(menu->entries[i]->rtext);
632 #ifdef USER_MENU
634 if (menu->entries[i]->instances){
635 PLRelease(menu->entries[i]->instances);
637 #endif /* USER_MENU */
639 if (menu->entries[i]->free_cdata && menu->entries[i]->clientdata) {
640 (*menu->entries[i]->free_cdata)(menu->entries[i]->clientdata);
642 wfree(menu->entries[i]);
645 if (recurse) {
646 for (i=0; i<menu->cascade_no; i++) {
647 if (menu->cascades[i]) {
648 if (menu->cascades[i]->flags.brother)
649 wMenuDestroy(menu->cascades[i]->brother, recurse);
650 else
651 wMenuDestroy(menu->cascades[i], recurse);
656 if (menu->entries)
657 wfree(menu->entries);
661 FREE_PIXMAP(menu->menu_texture_data);
663 if (menu->cascades)
664 wfree(menu->cascades);
666 wCoreDestroy(menu->menu);
667 wFrameWindowDestroy(menu->frame);
669 /* destroy copy of this menu */
670 if (!menu->flags.brother && menu->brother)
671 wMenuDestroy(menu->brother, False);
673 wfree(menu);
677 #define F_NORMAL 0
678 #define F_TOP 1
679 #define F_BOTTOM 2
680 #define F_NONE 3
682 static void
683 drawFrame(WScreen *scr, Drawable win, int y, int w, int h, int type)
685 XSegment segs[2];
686 int i;
688 i = 0;
689 segs[i].x1 = segs[i].x2 = w-1;
690 segs[i].y1 = y;
691 segs[i].y2 = y + h - 1;
692 i++;
693 if (type != F_TOP && type != F_NONE) {
694 segs[i].x1 = 1;
695 segs[i].y1 = segs[i].y2 = y + h-2;
696 segs[i].x2 = w-1;
697 i++;
699 XDrawSegments(dpy, win, scr->menu_item_auxtexture->dim_gc, segs, i);
701 i = 0;
702 segs[i].x1 = 0;
703 segs[i].y1 = y;
704 segs[i].x2 = 0;
705 segs[i].y2 = y + h - 1;
706 i++;
707 if (type != F_BOTTOM && type != F_NONE) {
708 segs[i].x1 = 0;
709 segs[i].y1 = y;
710 segs[i].x2 = w-1;
711 segs[i].y2 = y;
712 i++;
714 XDrawSegments(dpy, win, scr->menu_item_auxtexture->light_gc, segs, i);
716 if (type != F_TOP && type != F_NONE)
717 XDrawLine(dpy, win, scr->menu_item_auxtexture->dark_gc, 0, y+h-1,
718 w-1, y+h-1);
722 static void
723 paintEntry(WMenu *menu, int index, int selected)
725 int x, y, w, h, tw;
726 int type;
727 GC light, dim, dark, textGC;
728 WScreen *scr=menu->frame->screen_ptr;
729 Window win = menu->menu->window;
730 WMenuEntry *entry=menu->entries[index];
732 if (!menu->flags.realized) return;
733 h = menu->entry_height;
734 w = menu->menu->width;
735 y = index * h;
737 light = scr->menu_item_auxtexture->light_gc;
738 dim = scr->menu_item_auxtexture->dim_gc;
739 dark = scr->menu_item_auxtexture->dark_gc;
741 if (wPreferences.menu_style == MS_FLAT && menu->entry_no > 1) {
742 if (index == 0)
743 type = F_TOP;
744 else if (index == menu->entry_no - 1)
745 type = F_BOTTOM;
746 else
747 type = F_NONE;
748 } else {
749 type = F_NORMAL;
752 /* paint background */
753 if (selected) {
754 XSetForeground(dpy, scr->select_menu_gc, scr->select_pixel);
755 XFillRectangle(dpy, win, scr->select_menu_gc, 1, y+1, w-2, h-3);
756 if (scr->menu_item_texture->any.type == WTEX_SOLID)
757 drawFrame(scr, win, y, w, h, type);
758 } else {
759 if (scr->menu_item_texture->any.type == WTEX_SOLID) {
760 XClearArea(dpy, win, 0, y + 1, w - 1, h - 3, False);
761 /* draw the frame */
762 drawFrame(scr, win, y, w, h, type);
763 } else {
764 XClearArea(dpy, win, 0, y, w, h, False);
768 if (selected) {
769 textGC = scr->select_menu_gc;
770 if (entry->flags.enabled)
771 XSetForeground(dpy, textGC, scr->select_text_pixel);
772 else
773 XSetForeground(dpy, textGC, scr->dtext_pixel);
774 } else if (!entry->flags.enabled) {
775 textGC = scr->disabled_menu_entry_gc;
776 } else {
777 textGC = scr->menu_entry_gc;
779 /* draw text */
780 x = 5;
781 if (entry->flags.indicator)
782 x += MENU_INDICATOR_SPACE + 2;
784 WMDrawString(scr->wmscreen, win, textGC, scr->menu_entry_font,
785 x, 3 + y + wPreferences.menu_text_clearance, entry->text, strlen(entry->text));
787 if (entry->cascade>=0) {
788 /* draw the cascade indicator */
789 XDrawLine(dpy,win,dim, w-11, y+6, w-6, y+h/2-1);
790 XDrawLine(dpy,win,light, w-11, y+h-8, w-6, y+h/2-1);
791 XDrawLine(dpy,win,dark, w-12, y+6, w-12, y+h-8);
794 /* draw indicator */
795 if (entry->flags.indicator && entry->flags.indicator_on) {
796 int iw, ih;
797 WPixmap *indicator;
800 switch (entry->flags.indicator_type) {
801 case MI_CHECK:
802 indicator = scr->menu_check_indicator;
803 break;
804 case MI_MINIWINDOW:
805 indicator = scr->menu_mini_indicator;
806 break;
807 case MI_HIDDEN:
808 indicator = scr->menu_hide_indicator;
809 break;
810 case MI_SHADED:
811 indicator = scr->menu_shade_indicator;
812 break;
813 case MI_DIAMOND:
814 default:
815 indicator = scr->menu_radio_indicator;
816 break;
819 iw = indicator->width;
820 ih = indicator->height;
821 XSetClipMask(dpy, scr->copy_gc, indicator->mask);
822 XSetClipOrigin(dpy, scr->copy_gc, 5, y+(h-ih)/2);
823 if (selected)
824 XSetForeground(dpy, scr->copy_gc, scr->black_pixel);
825 else
826 XSetForeground(dpy, scr->copy_gc, scr->mtext_pixel);
827 XFillRectangle(dpy, win, scr->copy_gc, 5, y+(h-ih)/2, iw, ih);
829 XCopyArea(dpy, indicator->image, win, scr->copy_gc, 0, 0,
830 iw, ih, 5, y+(h-ih)/2);
832 XSetClipOrigin(dpy, scr->copy_gc, 0, 0);
835 /* draw right text */
837 if (entry->rtext && entry->cascade<0) {
839 tw = WMWidthOfString(scr->menu_entry_font, entry->rtext,
840 strlen(entry->rtext));
841 WMDrawString(scr->wmscreen, win, textGC, scr->menu_entry_font, w-6-tw,
842 y + 3 + wPreferences.menu_text_clearance, entry->rtext, strlen(entry->rtext));
847 static void
848 move_menus(WMenu *menu, int x, int y)
850 while (menu->parent) {
851 menu = menu->parent;
852 x -= MENUW(menu);
853 if (!wPreferences.align_menus && menu->selected_entry>=0) {
854 y -= menu->selected_entry*menu->entry_height;
857 wMenuMove(menu, x, y, True);
860 static void
861 makeVisible(WMenu *menu)
863 WScreen *scr = menu->frame->screen_ptr;
864 int x1, y1, x2, y2, new_x, new_y, move;
866 if (menu->entry_no<0) return;
868 x1 = menu->frame_x;
869 y1 = menu->frame_y+menu->frame->top_width
870 + menu->selected_entry*menu->entry_height;
871 x2 = x1 + MENUW(menu);
872 y2 = y1 + menu->entry_height;
874 new_x = x1;
875 new_y = y1;
876 move = 0;
878 if (x1 < 0) {
879 new_x = 0;
880 move = 1;
881 } else if (x2 >= scr->scr_width) {
882 new_x = scr->scr_width - MENUW(menu) - 1;
883 move = 1;
886 if (y1 < 0) {
887 new_y = 0;
888 move = 1;
889 } else if (y2 >= scr->scr_height) {
890 new_y = scr->scr_height - menu->entry_height - 1;
891 move = 1;
894 new_y = new_y - menu->frame->top_width
895 - menu->selected_entry*menu->entry_height;
896 move_menus(menu, new_x, new_y);
900 static int
901 check_key(WMenu *menu, XKeyEvent *event)
903 int i, ch, s;
904 char buffer[32];
906 if (XLookupString(event, buffer, 32, NULL, NULL)<1)
907 return -1;
909 ch = toupper(buffer[0]);
911 s = (menu->selected_entry>=0 ? menu->selected_entry+1 : 0);
913 again:
914 for (i=s; i<menu->entry_no; i++) {
915 if (ch==toupper(menu->entries[i]->text[0])) {
916 return i;
919 /* no match. Retry from start, if previous started from a selected entry */
920 if (s!=0) {
921 s = 0;
922 goto again;
924 return -1;
928 static int
929 keyboardMenu(WMenu *menu)
931 XEvent event;
932 KeySym ksym=NoSymbol;
933 int done=0;
934 int index;
935 WMenuEntry *entry;
936 int old_pos_x = menu->frame_x;
937 int old_pos_y = menu->frame_y;
938 int new_x = old_pos_x, new_y = old_pos_y;
939 int scr_width = menu->frame->screen_ptr->scr_width;
940 int scr_height = menu->frame->screen_ptr->scr_height;
942 if (menu->flags.editing)
943 return False;
946 XGrabKeyboard(dpy, menu->frame->core->window, True, GrabModeAsync,
947 GrabModeAsync, CurrentTime);
949 if (menu->frame_y+menu->frame->top_width >= scr_height)
950 new_y = scr_height - menu->frame->top_width;
952 if (menu->frame_x+MENUW(menu) >= scr_width)
953 new_x = scr_width-MENUW(menu)-1;
955 move_menus(menu, new_x, new_y);
957 while (!done && menu->flags.mapped) {
958 XAllowEvents(dpy, AsyncKeyboard, CurrentTime);
959 WMMaskEvent(dpy, ExposureMask|ButtonMotionMask|ButtonPressMask
960 |ButtonReleaseMask|KeyPressMask|KeyReleaseMask
961 |SubstructureNotifyMask, &event);
963 switch (event.type) {
964 case KeyPress:
965 ksym = XLookupKeysym(&event.xkey, 0);
966 switch (ksym) {
967 case XK_Escape:
968 done = 1;
969 break;
971 case XK_Home:
972 #ifdef XK_KP_Home
973 case XK_KP_Home:
974 #endif
975 selectEntry(menu, 0);
976 makeVisible(menu);
977 break;
979 case XK_End:
980 #ifdef XK_KP_End
981 case XK_KP_End:
982 #endif
983 selectEntry(menu, menu->entry_no-1);
984 makeVisible(menu);
985 break;
987 case XK_Up:
988 #ifdef ARROWLESS_KBD
989 case XK_k:
990 #endif
991 #ifdef XK_KP_Up
992 case XK_KP_Up:
993 #endif
994 if (menu->selected_entry <= 0)
995 selectEntry(menu, menu->entry_no-1);
996 else
997 selectEntry(menu, menu->selected_entry-1);
998 makeVisible(menu);
999 break;
1001 case XK_Down:
1002 #ifdef ARROWLESS_KBD
1003 case XK_j:
1004 #endif
1005 #ifdef XK_KP_Down
1006 case XK_KP_Down:
1007 #endif
1008 if (menu->selected_entry<0)
1009 selectEntry(menu, 0);
1010 else if (menu->selected_entry == menu->entry_no-1)
1011 selectEntry(menu, 0);
1012 else if (menu->selected_entry < menu->entry_no-1)
1013 selectEntry(menu, menu->selected_entry+1);
1014 makeVisible(menu);
1015 break;
1017 case XK_Right:
1018 #ifdef ARROWLESS_KBD
1019 case XK_l:
1020 #endif
1021 #ifdef XK_KP_Right
1022 case XK_KP_Right:
1023 #endif
1024 if (menu->selected_entry>=0) {
1025 WMenuEntry *entry;
1026 entry = menu->entries[menu->selected_entry];
1028 if (entry->cascade >= 0 && menu->cascades
1029 && menu->cascades[entry->cascade]->entry_no > 0) {
1031 XUngrabKeyboard(dpy, CurrentTime);
1033 selectEntry(menu->cascades[entry->cascade], 0);
1034 if (!keyboardMenu(menu->cascades[entry->cascade]))
1035 done = 1;
1037 XGrabKeyboard(dpy, menu->frame->core->window, True,
1038 GrabModeAsync, GrabModeAsync,
1039 CurrentTime);
1042 break;
1044 case XK_Left:
1045 #ifdef ARROWLESS_KBD
1046 case XK_h:
1047 #endif
1048 #ifdef XK_KP_Left
1049 case XK_KP_Left:
1050 #endif
1051 if (menu->parent!=NULL && menu->parent->selected_entry>=0) {
1052 selectEntry(menu, -1);
1053 move_menus(menu, old_pos_x, old_pos_y);
1054 return True;
1056 break;
1058 case XK_Return:
1059 done = 2;
1060 break;
1062 default:
1063 index = check_key(menu, &event.xkey);
1064 if (index>=0) {
1065 selectEntry(menu, index);
1068 break;
1070 default:
1071 if (event.type==ButtonPress)
1072 done = 1;
1074 WMHandleEvent(&event);
1078 XUngrabKeyboard(dpy, CurrentTime);
1080 if (done==2 && menu->selected_entry>=0) {
1081 entry = menu->entries[menu->selected_entry];
1082 } else {
1083 entry = NULL;
1086 if (entry && entry->callback!=NULL && entry->flags.enabled
1087 && entry->cascade < 0) {
1088 #if (MENU_BLINK_COUNT > 0)
1089 int sel = menu->selected_entry;
1090 int i;
1092 for (i=0; i<MENU_BLINK_COUNT; i++) {
1093 paintEntry(menu, sel, False);
1094 XSync(dpy, 0);
1095 wusleep(MENU_BLINK_DELAY);
1096 paintEntry(menu, sel, True);
1097 XSync(dpy, 0);
1098 wusleep(MENU_BLINK_DELAY);
1100 #endif
1101 selectEntry(menu, -1);
1103 if (!menu->flags.buttoned) {
1104 wMenuUnmap(menu);
1105 move_menus(menu, old_pos_x, old_pos_y);
1107 closeCascade(menu);
1109 (*entry->callback)(menu, entry);
1110 } else {
1111 if (!menu->flags.buttoned) {
1112 wMenuUnmap(menu);
1113 move_menus(menu, old_pos_x, old_pos_y);
1115 selectEntry(menu, -1);
1119 /* returns True if returning from a submenu to a parent menu,
1120 * False if exiting from menu */
1121 return False;
1125 void
1126 wMenuMapAt(WMenu *menu, int x, int y, int keyboard)
1128 int scr_width = menu->frame->screen_ptr->scr_width;
1129 int scr_height = menu->frame->screen_ptr->scr_height;
1131 if (!menu->flags.realized) {
1132 menu->flags.realized=1;
1133 wMenuRealize(menu);
1135 if (!menu->flags.mapped) {
1136 if (wPreferences.wrap_menus) {
1137 if (x<0) x = 0;
1138 if (y<0) y = 0;
1139 if (x+MENUW(menu) > scr_width)
1140 x = scr_width - MENUW(menu);
1141 if (y+MENUH(menu) > scr_height)
1142 y = scr_height - MENUH(menu);
1145 XMoveWindow(dpy, menu->frame->core->window, x, y);
1146 menu->frame_x = x;
1147 menu->frame_y = y;
1148 XMapWindow(dpy, menu->frame->core->window);
1149 wRaiseFrame(menu->frame->core);
1150 menu->flags.mapped = 1;
1151 } else {
1152 selectEntry(menu, 0);
1155 if (keyboard)
1156 keyboardMenu(menu);
1160 void
1161 wMenuMap(WMenu *menu)
1163 if (!menu->flags.realized) {
1164 menu->flags.realized=1;
1165 wMenuRealize(menu);
1167 if (menu->flags.app_menu && menu->parent==NULL) {
1168 menu->frame_x = menu->frame->screen_ptr->app_menu_x;
1169 menu->frame_y = menu->frame->screen_ptr->app_menu_y;
1170 XMoveWindow(dpy, menu->frame->core->window, menu->frame_x, menu->frame_y);
1172 XMapWindow(dpy, menu->frame->core->window);
1173 wRaiseFrame(menu->frame->core);
1174 menu->flags.mapped = 1;
1178 void
1179 wMenuUnmap(WMenu *menu)
1181 int i;
1183 XUnmapWindow(dpy, menu->frame->core->window);
1184 if (menu->flags.titled && menu->flags.buttoned) {
1185 wFrameWindowHideButton(menu->frame, WFF_RIGHT_BUTTON);
1187 menu->flags.buttoned = 0;
1188 menu->flags.mapped = 0;
1189 menu->flags.open_to_left = 0;
1191 for (i=0; i<menu->cascade_no; i++) {
1192 if (menu->cascades[i]!=NULL
1193 && menu->cascades[i]->flags.mapped
1194 && !menu->cascades[i]->flags.buttoned) {
1196 wMenuUnmap(menu->cascades[i]);
1199 menu->selected_entry = -1;
1204 void
1205 wMenuPaint(WMenu *menu)
1207 int i;
1209 if (!menu->flags.mapped) {
1210 return;
1213 /* paint entries */
1214 for (i=0; i<menu->entry_no; i++) {
1215 paintEntry(menu, i, i==menu->selected_entry);
1220 void
1221 wMenuSetEnabled(WMenu *menu, int index, int enable)
1223 if (index>=menu->entry_no) return;
1224 menu->entries[index]->flags.enabled=enable;
1225 paintEntry(menu, index, index==menu->selected_entry);
1226 paintEntry(menu->brother, index, index==menu->selected_entry);
1230 /* ====================================================================== */
1233 static void
1234 editEntry(WMenu *menu, WMenuEntry *entry)
1236 WTextInput *text;
1237 XEvent event;
1238 WObjDescriptor *desc;
1239 char *t;
1240 int done = 0;
1241 Window old_focus;
1242 int old_revert;
1244 menu->flags.editing = 1;
1246 text = wTextCreate(menu->menu, 1, menu->entry_height * entry->order,
1247 menu->menu->width - 2, menu->entry_height - 1);
1249 wTextPutText(text, entry->text);
1250 XGetInputFocus(dpy, &old_focus, &old_revert);
1251 XSetInputFocus(dpy, text->core->window, RevertToNone, CurrentTime);
1253 if (XGrabKeyboard(dpy, text->core->window, True, GrabModeAsync,
1254 GrabModeAsync, CurrentTime)!=GrabSuccess) {
1255 wwarning(_("could not grab keyboard"));
1256 wTextDestroy(text);
1258 wSetFocusTo(menu->frame->screen_ptr,
1259 menu->frame->screen_ptr->focused_window);
1260 return;
1264 while (!done && !text->done) {
1265 XSync(dpy, 0);
1266 XAllowEvents(dpy, AsyncKeyboard|AsyncPointer, CurrentTime);
1267 XSync(dpy, 0);
1268 WMNextEvent(dpy, &event);
1270 if (XFindContext(dpy, event.xany.window, wWinContext,
1271 (XPointer *)&desc)==XCNOENT)
1272 desc = NULL;
1274 if ((desc != NULL) && (desc->handle_anything != NULL)) {
1276 (*desc->handle_anything)(desc, &event);
1278 } else {
1279 switch (event.type) {
1280 case ButtonPress:
1281 XAllowEvents(dpy, ReplayPointer, CurrentTime);
1282 done = 1;
1284 default:
1285 WMHandleEvent(&event);
1286 break;
1291 XSetInputFocus(dpy, old_focus, old_revert, CurrentTime);
1293 wSetFocusTo(menu->frame->screen_ptr,
1294 menu->frame->screen_ptr->focused_window);
1297 t = wTextGetText(text);
1298 /* if !t, the user has canceled editing */
1299 if (t) {
1300 if (entry->text)
1301 wfree(entry->text);
1302 entry->text = wstrdup(t);
1304 menu->flags.realized = 0;
1306 wTextDestroy(text);
1308 XUngrabKeyboard(dpy, CurrentTime);
1310 if (t && menu->on_edit)
1311 (*menu->on_edit)(menu, entry);
1313 menu->flags.editing = 0;
1315 if (!menu->flags.realized)
1316 wMenuRealize(menu);
1320 static void
1321 selectEntry(WMenu *menu, int entry_no)
1323 WMenuEntry *entry;
1324 WMenu *submenu;
1325 int old_entry;
1327 if (menu->entries==NULL)
1328 return;
1330 if (entry_no >= menu->entry_no)
1331 return;
1333 old_entry = menu->selected_entry;
1334 menu->selected_entry = entry_no;
1336 if (old_entry!=entry_no) {
1338 /* unselect previous entry */
1339 if (old_entry>=0) {
1340 paintEntry(menu, old_entry, False);
1341 entry = menu->entries[old_entry];
1343 /* unmap cascade */
1344 if (entry->cascade>=0 && menu->cascades) {
1345 if (!menu->cascades[entry->cascade]->flags.buttoned) {
1346 wMenuUnmap(menu->cascades[entry->cascade]);
1351 if (entry_no<0) {
1352 menu->selected_entry = -1;
1353 return;
1355 entry = menu->entries[entry_no];
1357 if (entry->cascade>=0 && menu->cascades && entry->flags.enabled) {
1358 /* Callback for when the submenu is opened.
1360 submenu = menu->cascades[entry->cascade];
1361 if (submenu && submenu->flags.brother)
1362 submenu = submenu->brother;
1364 if (entry->callback) {
1365 /* Only call the callback if the submenu is not yet mapped.
1367 if (menu->flags.brother) {
1368 if (!submenu || !submenu->flags.mapped)
1369 (*entry->callback)(menu->brother, entry);
1370 } else {
1371 if (!submenu || !submenu->flags.buttoned)
1372 (*entry->callback)(menu, entry);
1376 /* the submenu menu might have changed */
1377 submenu = menu->cascades[entry->cascade];
1379 /* map cascade */
1380 if (!submenu->flags.mapped) {
1381 int x, y;
1383 if (!submenu->flags.realized)
1384 wMenuRealize(submenu);
1385 if (wPreferences.wrap_menus) {
1386 if (menu->flags.open_to_left)
1387 submenu->flags.open_to_left = 1;
1389 if (submenu->flags.open_to_left) {
1390 x = menu->frame_x - MENUW(submenu);
1391 if (x<0) {
1392 x = 0;
1393 submenu->flags.open_to_left = 0;
1395 } else {
1396 x = menu->frame_x + MENUW(menu);
1398 if (x + MENUW(submenu)
1399 >= menu->frame->screen_ptr->scr_width) {
1401 x = menu->frame_x - MENUW(submenu);
1402 submenu->flags.open_to_left = 1;
1405 } else {
1406 x = menu->frame_x + MENUW(menu);
1409 if (wPreferences.align_menus) {
1410 y = menu->frame_y;
1411 } else {
1412 y = menu->frame_y + menu->entry_height*entry_no;
1413 if (menu->flags.titled)
1414 y += menu->frame->top_width;
1415 if (menu->cascades[entry->cascade]->flags.titled)
1416 y -= menu->cascades[entry->cascade]->frame->top_width;
1419 wMenuMapAt(menu->cascades[entry->cascade], x, y, False);
1420 menu->cascades[entry->cascade]->parent = menu;
1421 } else {
1422 return;
1425 paintEntry(menu, entry_no, True);
1430 static WMenu*
1431 findMenu(WScreen *scr, int *x_ret, int *y_ret)
1433 WMenu *menu;
1434 WObjDescriptor *desc;
1435 Window root_ret, win, junk_win;
1436 int x, y, wx, wy;
1437 unsigned int mask;
1439 XQueryPointer(dpy, scr->root_win, &root_ret, &win, &x, &y, &wx, &wy,
1440 &mask);
1442 if (win==None) return NULL;
1444 if (XFindContext(dpy, win, wWinContext, (XPointer *)&desc)==XCNOENT)
1445 return NULL;
1447 if (desc->parent_type == WCLASS_MENU) {
1448 menu = (WMenu*)desc->parent;
1449 XTranslateCoordinates(dpy, root_ret, menu->menu->window, wx, wy,
1450 x_ret, y_ret, &junk_win);
1451 return menu;
1453 return NULL;
1459 static void
1460 closeCascade(WMenu *menu)
1462 WMenu *parent=menu->parent;
1464 if (menu->flags.brother
1465 || (!menu->flags.buttoned
1466 && (!menu->flags.app_menu||menu->parent!=NULL))) {
1468 selectEntry(menu, -1);
1469 XSync(dpy, 0);
1470 #if (MENU_BLINK_DELAY > 2)
1471 wusleep(MENU_BLINK_DELAY/2);
1472 #endif
1473 wMenuUnmap(menu);
1474 while (parent!=NULL
1475 && (parent->parent!=NULL || !parent->flags.app_menu
1476 || parent->flags.brother)
1477 && !parent->flags.buttoned) {
1478 selectEntry(parent, -1);
1479 wMenuUnmap(parent);
1480 parent = parent->parent;
1482 if (parent)
1483 selectEntry(parent, -1);
1488 static void
1489 closeBrotherCascadesOf(WMenu *menu)
1491 WMenu *tmp;
1492 int i;
1494 for (i=0; i<menu->cascade_no; i++) {
1495 if (menu->cascades[i]->flags.brother) {
1496 tmp = menu->cascades[i];
1497 } else {
1498 tmp = menu->cascades[i]->brother;
1500 if (tmp->flags.mapped) {
1501 selectEntry(tmp->parent, -1);
1502 closeBrotherCascadesOf(tmp);
1503 break;
1509 #define getEntryAt(menu, x, y) ((y)<0 ? -1 : (y)/(menu->entry_height))
1512 static WMenu*
1513 parentMenu(WMenu *menu)
1515 WMenu *parent;
1516 WMenuEntry *entry;
1518 if (menu->flags.buttoned)
1519 return menu;
1521 while (menu->parent && menu->parent->flags.mapped) {
1522 parent = menu->parent;
1523 if (parent->selected_entry < 0)
1524 break;
1525 entry = parent->entries[parent->selected_entry];
1526 if (!entry->flags.enabled || entry->cascade<0 || !parent->cascades ||
1527 parent->cascades[entry->cascade] != menu)
1528 break;
1529 menu = parent;
1530 if (menu->flags.buttoned)
1531 break;
1534 return menu;
1540 * Will raise the passed menu, if submenu = 0
1541 * If submenu > 0 will also raise all mapped submenus
1542 * until the first buttoned one
1543 * If submenu < 0 will also raise all mapped parent menus
1544 * until the first buttoned one
1547 static void
1548 raiseMenus(WMenu *menu, int submenus)
1550 WMenu *submenu;
1551 int i;
1553 if(!menu) return;
1555 wRaiseFrame(menu->frame->core);
1557 if (submenus>0 && menu->selected_entry>=0) {
1558 i = menu->entries[menu->selected_entry]->cascade;
1559 if (i>=0 && menu->cascades) {
1560 submenu = menu->cascades[i];
1561 if (submenu->flags.mapped && !submenu->flags.buttoned)
1562 raiseMenus(submenu, submenus);
1565 if (submenus<0 && !menu->flags.buttoned &&
1566 menu->parent && menu->parent->flags.mapped)
1567 raiseMenus(menu->parent, submenus);
1571 WMenu*
1572 wMenuUnderPointer(WScreen *screen)
1574 WObjDescriptor *desc;
1575 Window root_ret, win;
1576 int dummy;
1577 unsigned int mask;
1579 XQueryPointer(dpy, screen->root_win, &root_ret, &win, &dummy, &dummy,
1580 &dummy, &dummy, &mask);
1582 if (win==None) return NULL;
1584 if (XFindContext(dpy, win, wWinContext, (XPointer *)&desc)==XCNOENT)
1585 return NULL;
1587 if (desc->parent_type == WCLASS_MENU)
1588 return (WMenu *)desc->parent;
1589 return NULL;
1595 static void
1596 getPointerPosition(WScreen *scr, int *x, int *y)
1598 Window root_ret, win;
1599 int wx, wy;
1600 unsigned int mask;
1602 XQueryPointer(dpy, scr->root_win, &root_ret, &win, x, y, &wx, &wy, &mask);
1606 static void
1607 getScrollAmount(WMenu *menu, int *hamount, int *vamount)
1609 WScreen *scr = menu->menu->screen_ptr;
1610 int menuX1 = menu->frame_x;
1611 int menuY1 = menu->frame_y;
1612 int menuX2 = menu->frame_x + MENUW(menu);
1613 int menuY2 = menu->frame_y + MENUH(menu);
1614 int screenW = scr->scr_width;
1615 int screenH = scr->scr_height;
1616 int xroot, yroot;
1618 *hamount = 0;
1619 *vamount = 0;
1621 getPointerPosition(scr, &xroot, &yroot);
1623 #ifdef VIRTUAL_DESKTOP
1624 if (wPreferences.vedge_thickness) {
1625 if (xroot <= wPreferences.vedge_thickness + 1 && menuX1 < wPreferences.vedge_thickness) {
1626 /* scroll to the right */
1627 *hamount = WMIN(MENU_SCROLL_STEP, abs(menuX1));
1629 } else if (xroot >= screenW-2-wPreferences.vedge_thickness && menuX2 > screenW-1-wPreferences.vedge_thickness) {
1630 /* scroll to the left */
1631 *hamount = WMIN(MENU_SCROLL_STEP, abs(menuX2-screenW-1));
1633 if (*hamount==0)
1634 *hamount = 1;
1636 *hamount = -*hamount;
1638 } else
1639 #endif
1641 if (xroot <= 1 && menuX1 < 0) {
1642 /* scroll to the right */
1643 *hamount = WMIN(MENU_SCROLL_STEP, abs(menuX1));
1645 } else if (xroot >= screenW-2 && menuX2 > screenW-1) {
1646 /* scroll to the left */
1647 *hamount = WMIN(MENU_SCROLL_STEP, abs(menuX2-screenW-1));
1649 if (*hamount==0)
1650 *hamount = 1;
1652 *hamount = -*hamount;
1655 if (yroot <= 1 && menuY1 < 0) {
1656 /* scroll down */
1657 *vamount = WMIN(MENU_SCROLL_STEP, abs(menuY1));
1659 } else if (yroot >= screenH-2 && menuY2 > screenH-1) {
1660 /* scroll up */
1661 *vamount = WMIN(MENU_SCROLL_STEP, abs(menuY2-screenH-2));
1663 *vamount = -*vamount;
1668 static void
1669 dragScrollMenuCallback(void *data)
1671 WMenu *menu = (WMenu*)data;
1672 WScreen *scr = menu->menu->screen_ptr;
1673 WMenu *parent = parentMenu(menu);
1674 int hamount, vamount;
1675 int x, y;
1676 int newSelectedEntry;
1678 getScrollAmount(menu, &hamount, &vamount);
1681 if (hamount != 0 || vamount != 0) {
1682 wMenuMove(parent, parent->frame_x + hamount,
1683 parent->frame_y + vamount, True);
1684 if (findMenu(scr, &x, &y)) {
1685 newSelectedEntry = getEntryAt(menu, x, y);
1686 selectEntry(menu, newSelectedEntry);
1687 } else {
1688 /* Pointer fell outside of menu. If the selected entry is
1689 * not a submenu, unselect it */
1690 if (menu->selected_entry >= 0
1691 && menu->entries[menu->selected_entry]->cascade<0)
1692 selectEntry(menu, -1);
1693 newSelectedEntry = 0;
1696 /* paranoid check */
1697 if (newSelectedEntry >= 0) {
1698 /* keep scrolling */
1699 menu->timer = WMAddTimerHandler(MENU_SCROLL_DELAY,
1700 dragScrollMenuCallback, menu);
1701 } else {
1702 menu->timer = NULL;
1704 } else {
1705 /* don't need to scroll anymore */
1706 menu->timer = NULL;
1707 if (findMenu(scr, &x, &y)) {
1708 newSelectedEntry = getEntryAt(menu, x, y);
1709 selectEntry(menu, newSelectedEntry);
1715 static void
1716 scrollMenuCallback(void *data)
1718 WMenu *menu = (WMenu*)data;
1719 WMenu *parent = parentMenu(menu);
1720 int hamount = 0; /* amount to scroll */
1721 int vamount = 0;
1723 getScrollAmount(menu, &hamount, &vamount);
1725 if (hamount != 0 || vamount != 0) {
1726 wMenuMove(parent, parent->frame_x + hamount,
1727 parent->frame_y + vamount, True);
1729 /* keep scrolling */
1730 menu->timer = WMAddTimerHandler(MENU_SCROLL_DELAY,
1731 scrollMenuCallback, menu);
1732 } else {
1733 /* don't need to scroll anymore */
1734 menu->timer = NULL;
1740 #define MENU_SCROLL_BORDER 5
1742 static int
1743 isPointNearBoder(WMenu *menu, int x, int y)
1745 int menuX1 = menu->frame_x;
1746 int menuY1 = menu->frame_y;
1747 int menuX2 = menu->frame_x + MENUW(menu);
1748 int menuY2 = menu->frame_y + MENUH(menu);
1749 int scrXe = menu->menu->screen_ptr->scr_width-1;
1750 int scrYe = menu->menu->screen_ptr->scr_height-1;
1751 int flag = 0;
1753 if (x >= menuX1 && x <= menuX2 && (y < MENU_SCROLL_BORDER
1754 || y > scrYe-MENU_SCROLL_BORDER))
1755 flag = 1;
1756 else if (y >= menuY1 && y <= menuY2 && (x < MENU_SCROLL_BORDER
1757 || x > scrXe-MENU_SCROLL_BORDER))
1758 flag = 1;
1760 return flag;
1764 typedef struct _delay {
1765 WWindow *wwin;
1766 WMenu *menu;
1767 int ox,oy;
1768 } _delay;
1771 static void
1772 _leaving(_delay *dl)
1774 wMenuMove(dl->menu, dl->ox, dl->oy, True);
1775 dl->menu->jump_back=NULL;
1776 dl->menu->menu->screen_ptr->flags.jump_back_pending = 0;
1777 wfree(dl);
1781 void
1782 wMenuScroll(WMenu *menu, XEvent *event)
1784 WMenu *smenu;
1785 WMenu *omenu = parentMenu(menu);
1786 WScreen *scr = menu->frame->screen_ptr;
1787 int done = 0;
1788 int jump_back = 0;
1789 int old_frame_x = omenu->frame_x;
1790 int old_frame_y = omenu->frame_y;
1791 XEvent ev;
1793 if (omenu->jump_back)
1794 WMDeleteTimerWithClientData(omenu->jump_back);
1797 if ((/*omenu->flags.buttoned &&*/ !wPreferences.wrap_menus)
1798 || omenu->flags.app_menu) {
1799 jump_back = 1;
1802 if (!wPreferences.wrap_menus)
1803 raiseMenus(omenu, True);
1804 else
1805 raiseMenus(menu, False);
1807 if (!menu->timer)
1808 scrollMenuCallback(menu);
1810 while(!done) {
1811 int x, y, on_border, on_x_edge, on_y_edge, on_title;
1813 WMNextEvent(dpy, &ev);
1814 switch (ev.type) {
1815 case EnterNotify:
1816 WMHandleEvent(&ev);
1817 case MotionNotify:
1818 x = (ev.type==MotionNotify) ? ev.xmotion.x_root : ev.xcrossing.x_root;
1819 y = (ev.type==MotionNotify) ? ev.xmotion.y_root : ev.xcrossing.y_root;
1821 /* on_border is != 0 if the pointer is between the menu
1822 * and the screen border and is close enough to the border */
1823 on_border = isPointNearBoder(menu, x, y);
1825 smenu = wMenuUnderPointer(scr);
1827 if ((smenu==NULL && !on_border) || (smenu && parentMenu(smenu)!=omenu)) {
1828 done = 1;
1829 break;
1832 on_x_edge = x <= 1 || x >= scr->scr_width - 2;
1833 on_y_edge = y <= 1 || y >= scr->scr_height - 2;
1834 on_border = on_x_edge || on_y_edge;
1836 if (!on_border && !jump_back) {
1837 done = 1;
1838 break;
1841 if (menu->timer && (smenu!=menu || (!on_y_edge && !on_x_edge))) {
1842 WMDeleteTimerHandler(menu->timer);
1843 menu->timer = NULL;
1846 if (smenu != NULL)
1847 menu = smenu;
1849 if (!menu->timer)
1850 scrollMenuCallback(menu);
1851 break;
1852 case ButtonPress:
1853 /* True if we push on title, or drag the omenu to other position */
1854 on_title = ev.xbutton.x_root >= omenu->frame_x &&
1855 ev.xbutton.x_root <= omenu->frame_x + MENUW(omenu) &&
1856 ev.xbutton.y_root >= omenu->frame_y &&
1857 ev.xbutton.y_root <= omenu->frame_y + omenu->frame->top_width;
1858 WMHandleEvent(&ev);
1859 smenu = wMenuUnderPointer(scr);
1860 if (smenu == NULL || (smenu && smenu->flags.buttoned && smenu != omenu))
1861 done = 1;
1862 else if (smenu==omenu && on_title) {
1863 jump_back = 0;
1864 done = 1;
1866 break;
1867 case KeyPress:
1868 done = 1;
1869 default:
1870 WMHandleEvent(&ev);
1871 break;
1875 if (menu->timer) {
1876 WMDeleteTimerHandler(menu->timer);
1877 menu->timer = NULL;
1880 if (jump_back) {
1881 _delay *delayer;
1882 if (!omenu->jump_back) {
1883 delayer=wmalloc(sizeof(_delay));
1884 delayer->menu=omenu;
1885 delayer->ox=old_frame_x;
1886 delayer->oy=old_frame_y;
1887 omenu->jump_back=delayer;
1888 scr->flags.jump_back_pending = 1;
1890 else delayer = omenu->jump_back;
1891 WMAddTimerHandler(MENU_JUMP_BACK_DELAY,(WMCallback*)_leaving, delayer);
1897 static void
1898 menuExpose(WObjDescriptor *desc, XEvent *event)
1900 wMenuPaint(desc->parent);
1903 typedef struct {
1904 int *delayed_select;
1905 WMenu *menu;
1906 WMHandlerID magic;
1907 } delay_data;
1910 static void
1911 delaySelection(void *data)
1913 delay_data *d = (delay_data*)data;
1914 int x, y, entry_no;
1915 WMenu *menu;
1917 d->magic = NULL;
1919 menu = findMenu(d->menu->menu->screen_ptr, &x, &y);
1920 if (menu && (d->menu == menu || d->delayed_select)) {
1921 entry_no = getEntryAt(menu, x, y);
1922 selectEntry(menu, entry_no);
1924 if (d->delayed_select)
1925 *(d->delayed_select) = 0;
1929 static void
1930 menuMouseDown(WObjDescriptor *desc, XEvent *event)
1932 XButtonEvent *bev = &event->xbutton;
1933 WMenu *menu = desc->parent;
1934 WMenu *smenu;
1935 WScreen *scr=menu->frame->screen_ptr;
1936 WMenuEntry *entry=NULL;
1937 XEvent ev;
1938 int close_on_exit=0;
1939 int done=0;
1940 int delayed_select = 0;
1941 int entry_no;
1942 int x, y;
1943 int prevx, prevy;
1944 int old_frame_x = 0;
1945 int old_frame_y = 0;
1946 delay_data d_data = {NULL, NULL, NULL};
1948 if (!wPreferences.wrap_menus) {
1949 smenu = parentMenu(menu);
1950 old_frame_x = smenu->frame_x;
1951 old_frame_y = smenu->frame_y;
1952 } else if (event->xbutton.window == menu->frame->core->window) {
1953 /* This is true if the menu was launched with right click on root window */
1954 delayed_select = 1;
1955 d_data.delayed_select = &delayed_select;
1956 d_data.menu = menu;
1957 d_data.magic = WMAddTimerHandler(wPreferences.dblclick_time,
1958 delaySelection, &d_data);
1961 if (menu->flags.inside_handler) {
1962 return;
1964 menu->flags.inside_handler = 1;
1966 wRaiseFrame(menu->frame->core);
1968 close_on_exit = (bev->send_event || menu->flags.brother);
1970 smenu = findMenu(scr, &x, &y);
1971 if (!smenu) {
1972 x = -1;
1973 y = -1;
1974 } else {
1975 menu = smenu;
1978 if (menu->flags.editing) {
1979 goto byebye;
1981 entry_no = getEntryAt(menu, x, y);
1982 if (entry_no>=0) {
1983 entry = menu->entries[entry_no];
1985 if (!close_on_exit && (bev->state & ControlMask) && smenu
1986 && entry->flags.editable) {
1987 editEntry(smenu, entry);
1988 goto byebye;
1989 } else if (bev->state & ControlMask) {
1990 goto byebye;
1993 if (entry->flags.enabled && entry->cascade>=0 && menu->cascades) {
1994 WMenu *submenu = menu->cascades[entry->cascade];
1995 /* map cascade */
1996 if (submenu->flags.mapped && !submenu->flags.buttoned &&
1997 menu->selected_entry!=entry_no) {
1998 wMenuUnmap(submenu);
2000 if (!submenu->flags.mapped && !delayed_select) {
2001 selectEntry(menu, entry_no);
2002 } else if (!submenu->flags.buttoned) {
2003 selectEntry(menu, -1);
2006 } else if (!delayed_select) {
2007 selectEntry(menu, entry_no);
2010 if (!wPreferences.wrap_menus && !wPreferences.scrollable_menus) {
2011 if (!menu->timer)
2012 dragScrollMenuCallback(menu);
2016 prevx = bev->x_root;
2017 prevy = bev->y_root;
2018 while (!done) {
2019 int x, y;
2021 XAllowEvents(dpy, AsyncPointer|SyncPointer, CurrentTime);
2023 WMMaskEvent(dpy, ExposureMask|ButtonMotionMask|ButtonReleaseMask
2024 |ButtonPressMask, &ev);
2025 switch (ev.type) {
2026 case MotionNotify:
2027 smenu = findMenu(scr, &x, &y);
2029 if (smenu == NULL) {
2030 /* moved mouse out of menu */
2032 if (!delayed_select && d_data.magic) {
2033 WMDeleteTimerHandler(d_data.magic);
2034 d_data.magic = NULL;
2036 if (menu==NULL
2037 || (menu->selected_entry>=0
2038 && menu->entries[menu->selected_entry]->cascade>=0)) {
2039 prevx = ev.xmotion.x_root;
2040 prevy = ev.xmotion.y_root;
2042 break;
2044 selectEntry(menu, -1);
2045 menu = smenu;
2046 prevx = ev.xmotion.x_root;
2047 prevy = ev.xmotion.y_root;
2048 break;
2049 } else if (menu && menu!=smenu
2050 && (menu->selected_entry<0
2051 || menu->entries[menu->selected_entry]->cascade<0)) {
2052 selectEntry(menu, -1);
2054 if (!delayed_select && d_data.magic) {
2055 WMDeleteTimerHandler(d_data.magic);
2056 d_data.magic = NULL;
2058 } else {
2060 /* hysteresis for item selection */
2062 /* check if the motion was to the side, indicating that
2063 * the user may want to cross to a submenu */
2064 if (!delayed_select && menu) {
2065 int dx;
2066 Bool moved_to_submenu;/* moved to direction of submenu */
2068 dx = abs(prevx - ev.xmotion.x_root);
2070 moved_to_submenu = False;
2071 if (dx > 0 /* if moved enough to the side */
2072 /* maybe a open submenu */
2073 && menu->selected_entry>=0
2074 /* moving to the right direction */
2075 && (wPreferences.align_menus
2076 || ev.xmotion.y_root >= prevy)) {
2077 int index;
2079 index = menu->entries[menu->selected_entry]->cascade;
2080 if (index>=0) {
2081 if (menu->cascades[index]->frame_x>menu->frame_x) {
2082 if (prevx < ev.xmotion.x_root)
2083 moved_to_submenu = True;
2084 } else {
2085 if (prevx > ev.xmotion.x_root)
2086 moved_to_submenu = True;
2092 if (menu != smenu) {
2093 if (d_data.magic) {
2094 WMDeleteTimerHandler(d_data.magic);
2096 d_data.magic = NULL;
2097 } else if (moved_to_submenu) {
2098 /* while we are moving, postpone the selection */
2099 if (d_data.magic) {
2100 WMDeleteTimerHandler(d_data.magic);
2102 d_data.delayed_select = NULL;
2103 d_data.menu = menu;
2104 d_data.magic = WMAddTimerHandler(MENU_SELECT_DELAY,
2105 delaySelection,
2106 &d_data);
2107 prevx = ev.xmotion.x_root;
2108 prevy = ev.xmotion.y_root;
2109 break;
2110 } else {
2111 if (d_data.magic)
2112 WMDeleteTimerHandler(d_data.magic);
2113 d_data.magic = NULL;
2117 prevx = ev.xmotion.x_root;
2118 prevy = ev.xmotion.y_root;
2119 if (menu!=smenu) {
2120 /* pointer crossed menus */
2121 if (menu && menu->timer) {
2122 WMDeleteTimerHandler(menu->timer);
2123 menu->timer = NULL;
2125 if (smenu)
2126 dragScrollMenuCallback(smenu);
2128 menu = smenu;
2129 if (!menu->timer)
2130 dragScrollMenuCallback(menu);
2132 if (!delayed_select) {
2133 entry_no = getEntryAt(menu, x, y);
2134 if (entry_no>=0) {
2135 entry = menu->entries[entry_no];
2136 if (entry->flags.enabled && entry->cascade>=0 &&
2137 menu->cascades) {
2138 WMenu *submenu = menu->cascades[entry->cascade];
2139 if (submenu->flags.mapped && !submenu->flags.buttoned
2140 && menu->selected_entry!=entry_no) {
2141 wMenuUnmap(submenu);
2145 selectEntry(menu, entry_no);
2147 break;
2149 case ButtonPress:
2150 break;
2152 case ButtonRelease:
2153 if (ev.xbutton.button == event->xbutton.button)
2154 done=1;
2155 break;
2157 case Expose:
2158 WMHandleEvent(&ev);
2159 break;
2163 if (menu && menu->timer) {
2164 WMDeleteTimerHandler(menu->timer);
2165 menu->timer = NULL;
2167 if (d_data.magic!=NULL)
2168 WMDeleteTimerHandler(d_data.magic);
2170 if (menu && menu->selected_entry>=0) {
2171 entry = menu->entries[menu->selected_entry];
2172 if (entry->callback!=NULL && entry->flags.enabled
2173 && entry->cascade < 0) {
2174 /* blink and erase menu selection */
2175 #if (MENU_BLINK_DELAY > 0)
2176 int sel = menu->selected_entry;
2177 int i;
2179 for (i=0; i<MENU_BLINK_COUNT; i++) {
2180 paintEntry(menu, sel, False);
2181 XSync(dpy, 0);
2182 wusleep(MENU_BLINK_DELAY);
2183 paintEntry(menu, sel, True);
2184 XSync(dpy, 0);
2185 wusleep(MENU_BLINK_DELAY);
2187 #endif
2188 /* unmap the menu, it's parents and call the callback */
2189 if (!menu->flags.buttoned &&
2190 (!menu->flags.app_menu||menu->parent!=NULL)) {
2191 closeCascade(menu);
2192 } else {
2193 selectEntry(menu, -1);
2195 (*entry->callback)(menu, entry);
2197 /* If the user double clicks an entry, the entry will
2198 * be executed twice, which is not good for things like
2199 * the root menu. So, ignore any clicks that were generated
2200 * while the entry was being executed */
2201 while (XCheckTypedWindowEvent(dpy, menu->menu->window,
2202 ButtonPress, &ev));
2203 } else if (entry->callback!=NULL && entry->cascade<0) {
2204 selectEntry(menu, -1);
2205 } else {
2206 if (entry->cascade>=0 && menu->cascades
2207 && menu->cascades[entry->cascade]->flags.brother) {
2208 selectEntry(menu, -1);
2213 if (((WMenu*)desc->parent)->flags.brother || close_on_exit || !smenu)
2214 closeCascade(desc->parent);
2216 /* close the cascade windows that should not remain opened */
2217 closeBrotherCascadesOf(desc->parent);
2219 if (!wPreferences.wrap_menus)
2220 wMenuMove(parentMenu(desc->parent), old_frame_x, old_frame_y, True);
2222 byebye:
2223 ((WMenu*)desc->parent)->flags.inside_handler = 0;
2227 void
2228 wMenuMove(WMenu *menu, int x, int y, int submenus)
2230 WMenu *submenu;
2231 int i;
2233 if (!menu) return;
2235 menu->frame_x = x;
2236 menu->frame_y = y;
2237 XMoveWindow(dpy, menu->frame->core->window, x, y);
2239 if (submenus>0 && menu->selected_entry>=0) {
2240 i = menu->entries[menu->selected_entry]->cascade;
2242 if (i>=0 && menu->cascades) {
2243 submenu = menu->cascades[i];
2244 if (submenu->flags.mapped && !submenu->flags.buttoned) {
2245 if (wPreferences.align_menus) {
2246 wMenuMove(submenu, x + MENUW(menu), y, submenus);
2247 } else {
2248 wMenuMove(submenu, x+ MENUW(menu),
2249 y + submenu->entry_height*menu->selected_entry,
2250 submenus);
2255 if (submenus<0 && menu->parent!=NULL && menu->parent->flags.mapped &&
2256 !menu->parent->flags.buttoned) {
2257 if (wPreferences.align_menus) {
2258 wMenuMove(menu->parent, x - MENUW(menu->parent), y, submenus);
2259 } else {
2260 wMenuMove(menu->parent, x - MENUW(menu->parent), menu->frame_y
2261 - menu->parent->entry_height*menu->parent->selected_entry,
2262 submenus);
2268 static void
2269 changeMenuLevels(WMenu *menu, int lower)
2271 int i;
2273 if (!lower) {
2274 ChangeStackingLevel(menu->frame->core, (!menu->parent ? WMMainMenuLevel
2275 : WMSubmenuLevel));
2276 wRaiseFrame(menu->frame->core);
2277 menu->flags.lowered = 0;
2278 } else {
2279 ChangeStackingLevel(menu->frame->core, WMNormalLevel);
2280 wLowerFrame(menu->frame->core);
2281 menu->flags.lowered = 1;
2283 for (i=0; i<menu->cascade_no; i++) {
2284 if (menu->cascades[i]
2285 && !menu->cascades[i]->flags.buttoned
2286 && menu->cascades[i]->flags.lowered!=lower) {
2287 changeMenuLevels(menu->cascades[i], lower);
2294 static void
2295 menuTitleDoubleClick(WCoreWindow *sender, void *data, XEvent *event)
2297 WMenu *menu = data;
2298 int lower;
2300 if (event->xbutton.state & MOD_MASK) {
2301 if (menu->flags.lowered) {
2302 lower = 0;
2303 } else {
2304 lower = 1;
2306 changeMenuLevels(menu, lower);
2311 static void
2312 menuTitleMouseDown(WCoreWindow *sender, void *data, XEvent *event)
2314 WMenu *menu = data;
2315 WMenu *tmp;
2316 XEvent ev;
2317 int x=menu->frame_x, y=menu->frame_y;
2318 int dx=event->xbutton.x_root, dy=event->xbutton.y_root;
2319 int i, lower;
2320 Bool started;
2322 /* can't touch the menu copy */
2323 if (menu->flags.brother)
2324 return;
2326 if (event->xbutton.button != Button1 && event->xbutton.button != Button2)
2327 return;
2329 if (event->xbutton.state & MOD_MASK) {
2330 wLowerFrame(menu->frame->core);
2331 lower = 1;
2332 } else {
2333 wRaiseFrame(menu->frame->core);
2334 lower = 0;
2336 tmp = menu;
2338 /* lower/raise all submenus */
2339 while (1) {
2340 if (tmp->selected_entry>=0 && tmp->cascades
2341 && tmp->entries[tmp->selected_entry]->cascade>=0) {
2342 tmp = tmp->cascades[tmp->entries[tmp->selected_entry]->cascade];
2343 if (!tmp || !tmp->flags.mapped)
2344 break;
2345 if (lower)
2346 wLowerFrame(tmp->frame->core);
2347 else
2348 wRaiseFrame(tmp->frame->core);
2349 } else {
2350 break;
2354 /* tear off the menu if it's a root menu or a cascade
2355 application menu */
2356 if (!menu->flags.buttoned && !menu->flags.brother
2357 && (!menu->flags.app_menu||menu->parent!=NULL)) {
2358 menu->flags.buttoned=1;
2359 wFrameWindowShowButton(menu->frame, WFF_RIGHT_BUTTON);
2360 if (menu->parent) {
2361 /* turn off selected menu entry in parent menu */
2362 selectEntry(menu->parent, -1);
2364 /* make parent map the copy in place of the original */
2365 for (i=0; i<menu->parent->cascade_no; i++) {
2366 if (menu->parent->cascades[i] == menu) {
2367 menu->parent->cascades[i] = menu->brother;
2368 break;
2374 started = False;
2375 while(1) {
2376 WMMaskEvent(dpy, ButtonMotionMask|ButtonReleaseMask|ButtonPressMask
2377 |ExposureMask, &ev);
2378 switch (ev.type) {
2379 case MotionNotify:
2380 if (started) {
2381 x += ev.xmotion.x_root - dx;
2382 y += ev.xmotion.y_root - dy;
2383 dx = ev.xmotion.x_root;
2384 dy = ev.xmotion.y_root;
2385 wMenuMove(menu, x, y, True);
2386 } else {
2387 if (abs(ev.xmotion.x_root - dx) > MOVE_THRESHOLD
2388 || abs(ev.xmotion.y_root - dy) > MOVE_THRESHOLD) {
2389 started = True;
2390 XGrabPointer(dpy, menu->frame->titlebar->window, False,
2391 ButtonMotionMask|ButtonReleaseMask
2392 |ButtonPressMask,
2393 GrabModeAsync, GrabModeAsync, None,
2394 wCursor[WCUR_MOVE], CurrentTime);
2397 break;
2399 case ButtonPress:
2400 break;
2402 case ButtonRelease:
2403 if (ev.xbutton.button != event->xbutton.button)
2404 break;
2405 XUngrabPointer(dpy, CurrentTime);
2406 return;
2408 default:
2409 WMHandleEvent(&ev);
2410 break;
2416 *----------------------------------------------------------------------
2417 * menuCloseClick--
2418 * Handles mouse click on the close button of menus. The menu is
2419 * closed when the button is clicked.
2421 * Side effects:
2422 * The closed menu is reinserted at it's parent menus
2423 * cascade list.
2424 *----------------------------------------------------------------------
2426 static void
2427 menuCloseClick(WCoreWindow *sender, void *data, XEvent *event)
2429 WMenu *menu = (WMenu*)data;
2430 WMenu *parent = menu->parent;
2431 int i;
2433 if (parent) {
2434 for (i=0; i<parent->cascade_no; i++) {
2435 /* find the entry that points to the copy */
2436 if (parent->cascades[i] == menu->brother) {
2437 /* make it point to the original */
2438 parent->cascades[i] = menu;
2439 menu->parent = parent;
2440 break;
2444 wMenuUnmap(menu);
2448 static void
2449 saveMenuInfo(proplist_t dict, WMenu *menu, proplist_t key)
2451 proplist_t value, list;
2452 char buffer[256];
2454 sprintf(buffer, "%i,%i", menu->frame_x, menu->frame_y);
2455 value = PLMakeString(buffer);
2456 list = PLMakeArrayFromElements(value, NULL);
2457 if (menu->flags.lowered)
2458 PLAppendArrayElement(list, PLMakeString("lowered"));
2459 PLInsertDictionaryEntry(dict, key, list);
2460 PLRelease(value);
2461 PLRelease(list);
2465 void
2466 wMenuSaveState(WScreen *scr)
2468 proplist_t menus, key;
2469 int save_menus = 0;
2471 menus = PLMakeDictionaryFromEntries(NULL, NULL, NULL);
2473 #ifndef LITE
2474 if (scr->switch_menu && scr->switch_menu->flags.buttoned) {
2475 key = PLMakeString("SwitchMenu");
2476 saveMenuInfo(menus, scr->switch_menu, key);
2477 PLRelease(key);
2478 save_menus = 1;
2481 if (saveMenuRecurs(menus, scr, scr->root_menu))
2482 save_menus = 1;
2484 #endif /* !LITE */
2485 if (scr->workspace_menu && scr->workspace_menu->flags.buttoned) {
2486 key = PLMakeString("WorkspaceMenu");
2487 saveMenuInfo(menus, scr->workspace_menu, key);
2488 PLRelease(key);
2489 save_menus = 1;
2492 if (save_menus) {
2493 key = PLMakeString("Menus");
2494 PLInsertDictionaryEntry(scr->session_state, key, menus);
2495 PLRelease(key);
2497 PLRelease(menus);
2501 #ifndef LITE
2503 static Bool
2504 getMenuPath(WMenu *menu, char *buffer, int bufSize)
2506 Bool ok = True;
2507 int len = 0;
2509 if (!menu->flags.titled || !menu->frame->title[0])
2510 return False;
2512 len = strlen(menu->frame->title);
2513 if (len >= bufSize)
2514 return False;
2516 if (menu->parent) {
2517 ok = getMenuPath(menu->parent, buffer, bufSize - len - 1);
2518 if (!ok)
2519 return False;
2522 strcat(buffer, "\\");
2523 strcat(buffer, menu->frame->title);
2525 return True;
2529 static Bool
2530 saveMenuRecurs(proplist_t menus, WScreen *scr, WMenu *menu)
2532 proplist_t key;
2533 int save_menus = 0, i;
2534 char buffer[512];
2535 Bool ok = True;
2538 if (menu->flags.brother)
2539 menu = menu->brother;
2541 if (menu->flags.buttoned && menu != scr->switch_menu) {
2543 buffer[0] = '\0';
2544 ok = getMenuPath(menu, buffer, 510);
2546 if (ok) {
2547 key = PLMakeString(buffer);
2548 saveMenuInfo(menus, menu, key);
2549 PLRelease(key);
2550 save_menus = 1;
2554 if (ok) {
2555 for (i = 0; i < menu->cascade_no; i++) {
2556 if (saveMenuRecurs(menus, scr, menu->cascades[i]))
2557 save_menus = 1;
2560 return save_menus;
2562 #endif /* !LITE */
2565 #define COMPLAIN(key) wwarning(_("bad value in menus state info:%s"), key)
2568 static Bool
2569 getMenuInfo(proplist_t info, int *x, int *y, Bool *lowered)
2571 proplist_t pos;
2573 *lowered = False;
2575 if (PLIsArray(info)) {
2576 proplist_t flags;
2577 pos = PLGetArrayElement(info, 0);
2578 flags = PLGetArrayElement(info, 1);
2579 if (flags != NULL && PLIsString(flags) && PLGetString(flags) != NULL
2580 && strcmp(PLGetString(flags), "lowered") == 0) {
2581 *lowered = True;
2583 } else {
2584 pos = info;
2587 if (pos != NULL && PLIsString(pos)) {
2588 if (sscanf(PLGetString(pos), "%i,%i", x, y)!=2)
2589 COMPLAIN("Position");
2590 } else {
2591 COMPLAIN("(position, flags...)");
2592 return False;
2595 return True;
2599 static int
2600 restoreMenu(WScreen *scr, proplist_t menu, int which)
2602 int x, y;
2603 Bool lowered = False;
2604 WMenu *pmenu = NULL;
2606 if (!menu)
2607 return False;
2609 if (!getMenuInfo(menu, &x, &y, &lowered))
2610 return False;
2613 #ifndef LITE
2614 if (which & WSS_SWITCHMENU) {
2615 OpenSwitchMenu(scr, x, y, False);
2616 pmenu = scr->switch_menu;
2618 #endif /* !LITE */
2620 if (pmenu) {
2621 int width = MENUW(pmenu);
2622 int height = MENUH(pmenu);
2624 if (lowered) {
2625 changeMenuLevels(pmenu, True);
2628 x = (x < -width) ? 0 : x;
2629 x = (x > scr->scr_width) ? scr->scr_width - width : x;
2630 y = (y < 0) ? 0 : y;
2631 y = (y > scr->scr_height) ? scr->scr_height - height : y;
2632 wMenuMove(pmenu, x, y, True);
2633 pmenu->flags.buttoned = 1;
2634 wFrameWindowShowButton(pmenu->frame, WFF_RIGHT_BUTTON);
2635 return True;
2637 return False;
2641 #ifndef LITE
2642 static int
2643 restoreMenuRecurs(WScreen *scr, proplist_t menus, WMenu *menu, char *path)
2645 proplist_t key, entry;
2646 char buffer[512];
2647 int i, x, y, res;
2648 Bool lowered;
2650 if (strlen(path) + strlen(menu->frame->title) > 510)
2651 return False;
2653 sprintf(buffer, "%s\\%s", path, menu->frame->title);
2654 key = PLMakeString(buffer);
2655 entry = PLGetDictionaryEntry(menus, key);
2656 res = False;
2658 if (entry && getMenuInfo(entry, &x, &y, &lowered)) {
2660 if (!menu->flags.mapped) {
2661 int width = MENUW(menu);
2662 int height = MENUH(menu);
2664 wMenuMapAt(menu, x, y, False);
2666 if (menu->parent) {
2667 /* make parent map the copy in place of the original */
2668 for (i=0; i<menu->parent->cascade_no; i++) {
2669 if (menu->parent->cascades[i] == menu) {
2670 menu->parent->cascades[i] = menu->brother;
2671 break;
2675 if (lowered) {
2676 changeMenuLevels(menu, True);
2678 x = (x < -width) ? 0 : x;
2679 x = (x > scr->scr_width) ? scr->scr_width - width : x;
2680 y = (y < 0) ? 0 : y;
2681 y = (y > scr->scr_height) ? scr->scr_height - height : y;
2682 wMenuMove(menu, x, y, True);
2683 menu->flags.buttoned = 1;
2684 wFrameWindowShowButton(menu->frame, WFF_RIGHT_BUTTON);
2685 res = True;
2689 PLRelease(key);
2691 for (i=0; i<menu->cascade_no; i++) {
2692 if (restoreMenuRecurs(scr, menus, menu->cascades[i], buffer) != False)
2693 res = True;
2696 return res;
2698 #endif /* !LITE */
2701 void
2702 wMenuRestoreState(WScreen *scr)
2704 proplist_t menus, menu, key, skey;
2706 key = PLMakeString("Menus");
2707 menus = PLGetDictionaryEntry(scr->session_state, key);
2708 PLRelease(key);
2710 if (!menus)
2711 return;
2713 /* restore menus */
2715 skey = PLMakeString("SwitchMenu");
2716 menu = PLGetDictionaryEntry(menus, skey);
2717 PLRelease(skey);
2718 restoreMenu(scr, menu, WSS_SWITCHMENU);
2720 #ifndef LITE
2721 if (!scr->root_menu) {
2722 OpenRootMenu(scr, scr->scr_width*2, 0, False);
2723 wMenuUnmap(scr->root_menu);
2725 restoreMenuRecurs(scr, menus, scr->root_menu, "");
2726 #endif /* !LITE */
2730 void
2731 OpenWorkspaceMenu(WScreen *scr, int x, int y)
2733 WMenu *menu, *parent;
2734 WMenuEntry *entry;
2736 #ifndef LITE
2737 if (!scr->root_menu) {
2738 OpenRootMenu(scr, scr->scr_width*2, 0, False);
2739 wMenuUnmap(scr->root_menu);
2741 #endif
2742 menu = scr->workspace_menu;
2743 if (menu) {
2744 if (menu->flags.mapped) {
2745 if (!menu->flags.buttoned) {
2746 wMenuUnmap(menu);
2747 parent = menu->parent;
2748 if (parent && parent->selected_entry >= 0) {
2749 entry = parent->entries[parent->selected_entry];
2750 if (parent->cascades[entry->cascade] == menu) {
2751 selectEntry(parent, -1);
2752 wMenuMapAt(menu, x, y, False);
2755 } else {
2756 wRaiseFrame(menu->frame->core);
2757 wMenuMapCopyAt(menu, x, y);
2759 } else {
2760 wMenuMapAt(menu, x, y, False);