- API change in WINGs for WMDraw*String().
[wmaker-crm.git] / src / menu.c
blob49b001f7992b615f0bd92bceee28b218bfd83f5d
1 /* menu.c- generic menu, used for root menu, application menus etc.
3 * Window Maker window manager
4 *
5 * Copyright (c) 1997-2002 Alfredo K. Kojima
6 * Copyright (c) 1998-2002 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"
46 #include <WINGs/WINGsP.h>
49 /****** Global Variables ******/
51 extern Cursor wCursor[WCUR_LAST];
53 extern XContext wWinContext;
55 extern WPreferences wPreferences;
57 #define MOD_MASK wPreferences.modifier_mask
59 #define MENU_SCROLL_STEP menuScrollParameters[(int)wPreferences.menu_scroll_speed].steps
60 #define MENU_SCROLL_DELAY menuScrollParameters[(int)wPreferences.menu_scroll_speed].delay
64 #define MENUW(m) ((m)->frame->core->width+2*FRAME_BORDER_WIDTH)
65 #define MENUH(m) ((m)->frame->core->height+2*FRAME_BORDER_WIDTH)
68 /***** Local Stuff ******/
70 static struct {
71 int steps;
72 int delay;
73 } menuScrollParameters[5] = {
74 {MENU_SCROLL_STEPS_UF, MENU_SCROLL_DELAY_UF},
75 {MENU_SCROLL_STEPS_F, MENU_SCROLL_DELAY_F},
76 {MENU_SCROLL_STEPS_M, MENU_SCROLL_DELAY_M},
77 {MENU_SCROLL_STEPS_S, MENU_SCROLL_DELAY_S},
78 {MENU_SCROLL_STEPS_US, MENU_SCROLL_DELAY_US}};
81 static void menuMouseDown(WObjDescriptor *desc, XEvent *event);
82 static void menuExpose(WObjDescriptor *desc, XEvent *event);
84 static void menuTitleDoubleClick(WCoreWindow *sender, void *data, XEvent *event);
85 static void menuTitleMouseDown(WCoreWindow *sender, void *data, XEvent *event);
87 static void menuCloseClick(WCoreWindow *sender, void *data, XEvent *event);
89 static void updateTexture(WMenu *menu);
91 #ifndef LITE
92 static int saveMenuRecurs(WMPropList *menus, WScreen *scr, WMenu *menu);
93 static int restoreMenuRecurs(WScreen *scr, WMPropList *menus, WMenu *menu, char *path);
94 #endif /* !LITE */
96 static void selectEntry(WMenu *menu, int entry_no);
97 static void closeCascade(WMenu *menu);
100 /****** Notification Observers ******/
102 static void
103 appearanceObserver(void *self, WMNotification *notif)
105 WMenu *menu = (WMenu*)self;
106 int flags = (int)WMGetNotificationClientData(notif);
108 if (!menu->flags.realized)
109 return;
111 if (WMGetNotificationName(notif) == WNMenuAppearanceSettingsChanged) {
112 if (flags & WFontSettings) {
113 menu->flags.realized = 0;
114 wMenuRealize(menu);
116 if (flags & WTextureSettings) {
117 if (!menu->flags.brother)
118 updateTexture(menu);
120 if (flags & (WTextureSettings|WColorSettings)) {
121 wMenuPaint(menu);
123 } else if (menu->flags.titled) {
125 if (flags & WFontSettings) {
126 menu->flags.realized = 0;
127 wMenuRealize(menu);
129 if (flags & WTextureSettings) {
130 menu->frame->flags.need_texture_remake = 1;
132 if (flags & (WColorSettings|WTextureSettings)) {
133 wFrameWindowPaint(menu->frame);
138 /************************************/
142 *----------------------------------------------------------------------
143 * wMenuCreate--
144 * Creates a new empty menu with the specified title. If main_menu
145 * is True, the created menu will be a main menu, which has some special
146 * properties such as being placed over other normal menus.
147 * If title is NULL, the menu will have no titlebar.
149 * Returns:
150 * The created menu.
151 *----------------------------------------------------------------------
153 WMenu*
154 wMenuCreate(WScreen *screen, char *title, int main_menu)
156 WMenu *menu;
157 static int brother=0;
158 int tmp, flags;
160 menu = wmalloc(sizeof(WMenu));
162 memset(menu, 0, sizeof(WMenu));
164 #ifdef SINGLE_MENULEVEL
165 tmp = WMSubmenuLevel;
166 #else
167 tmp = (main_menu ? WMMainMenuLevel : WMSubmenuLevel);
168 #endif
170 flags = WFF_SINGLE_STATE|WFF_BORDER;
171 if (title) {
172 flags |= WFF_TITLEBAR|WFF_RIGHT_BUTTON;
173 menu->flags.titled = 1;
175 menu->frame =
176 wFrameWindowCreate(screen, tmp, 8, 2, 1, 1, &wPreferences.menu_title_clearance, flags,
177 screen->menu_title_texture, NULL,
178 screen->menu_title_pixel,
179 &screen->menu_title_font);
181 menu->frame->core->descriptor.parent = menu;
182 menu->frame->core->descriptor.parent_type = WCLASS_MENU;
183 menu->frame->core->descriptor.handle_mousedown = menuMouseDown;
185 wFrameWindowHideButton(menu->frame, WFF_RIGHT_BUTTON);
187 if (title) {
188 menu->frame->title = wstrdup(title);
191 menu->frame->flags.justification = WTJ_LEFT;
193 menu->frame->rbutton_image = screen->b_pixmaps[WBUT_CLOSE];
195 menu->entry_no = 0;
196 menu->alloced_entries = 0;
197 menu->selected_entry = -1;
198 menu->entries = NULL;
200 menu->frame_x = screen->app_menu_x;
201 menu->frame_y = screen->app_menu_y;
203 menu->frame->child = menu;
205 menu->flags.lowered = 0;
207 /* create borders */
208 if (title) {
209 /* setup object descriptors */
210 menu->frame->on_mousedown_titlebar = menuTitleMouseDown;
211 menu->frame->on_dblclick_titlebar = menuTitleDoubleClick;
214 menu->frame->on_click_right = menuCloseClick;
217 menu->menu = wCoreCreate(menu->frame->core, 0, menu->frame->top_width,
218 menu->frame->core->width, 10);
220 menu->menu->descriptor.parent = menu;
221 menu->menu->descriptor.parent_type = WCLASS_MENU;
222 menu->menu->descriptor.handle_expose = menuExpose;
223 menu->menu->descriptor.handle_mousedown = menuMouseDown;
225 menu->menu_texture_data = None;
227 XMapWindow(dpy, menu->menu->window);
229 XFlush(dpy);
231 if (!brother) {
232 brother = 1;
233 menu->brother = wMenuCreate(screen, title, main_menu);
234 brother = 0;
235 menu->brother->flags.brother = 1;
236 menu->brother->brother = menu;
238 WMAddNotificationObserver(appearanceObserver, menu,
239 WNMenuAppearanceSettingsChanged, menu);
241 WMAddNotificationObserver(appearanceObserver, menu,
242 WNMenuTitleAppearanceSettingsChanged, menu);
245 return menu;
251 WMenu*
252 wMenuCreateForApp(WScreen *screen, char *title, int main_menu)
254 WMenu *menu;
256 menu = wMenuCreate(screen, title, main_menu);
257 if (!menu)
258 return NULL;
259 menu->flags.app_menu = 1;
260 menu->brother->flags.app_menu = 1;
262 return menu;
267 static void
268 insertEntry(WMenu *menu, WMenuEntry *entry, int index)
270 int i;
272 for (i = menu->entry_no-1; i >= index; i--) {
273 menu->entries[i]->order++;
274 menu->entries[i+1] = menu->entries[i];
276 menu->entries[index] = entry;
280 WMenuEntry*
281 wMenuInsertCallback(WMenu *menu, int index, char *text,
282 void (*callback)(WMenu *menu, WMenuEntry *entry),
283 void *clientdata)
285 WMenuEntry *entry;
287 menu->flags.realized = 0;
288 menu->brother->flags.realized = 0;
290 /* reallocate array if it's too small */
291 if (menu->entry_no >= menu->alloced_entries) {
292 void *tmp;
294 tmp = wrealloc(menu->entries,
295 sizeof(WMenuEntry)*(menu->alloced_entries+5));
297 menu->entries = tmp;
298 menu->alloced_entries += 5;
300 menu->brother->entries = tmp;
301 menu->brother->alloced_entries = menu->alloced_entries;
303 entry = wmalloc(sizeof(WMenuEntry));
304 memset(entry, 0, sizeof(WMenuEntry));
305 entry->flags.enabled = 1;
306 entry->text = wstrdup(text);
307 entry->cascade = -1;
308 entry->clientdata = clientdata;
309 entry->callback = callback;
310 if (index<0 || index>=menu->entry_no) {
311 entry->order = menu->entry_no;
312 menu->entries[menu->entry_no] = entry;
313 } else {
314 entry->order = index;
315 insertEntry(menu, entry, index);
318 menu->entry_no++;
319 menu->brother->entry_no = menu->entry_no;
321 return entry;
326 void
327 wMenuEntrySetCascade(WMenu *menu, WMenuEntry *entry, WMenu *cascade)
329 WMenu *brother = menu->brother;
330 int i, done;
332 assert(menu->flags.brother==0);
334 if (entry->cascade>=0) {
335 menu->flags.realized = 0;
336 brother->flags.realized = 0;
339 cascade->parent = menu;
341 cascade->brother->parent = brother;
343 done = 0;
344 for (i=0; i<menu->cascade_no; i++) {
345 if (menu->cascades[i]==NULL) {
346 menu->cascades[i] = cascade;
347 brother->cascades[i] = cascade->brother;
348 done = 1;
349 entry->cascade = i;
350 break;
353 if (!done) {
354 entry->cascade = menu->cascade_no;
356 menu->cascades = wrealloc(menu->cascades,
357 sizeof(WMenu)*(menu->cascade_no+1));
358 menu->cascades[menu->cascade_no++] = cascade;
361 brother->cascades = wrealloc(brother->cascades,
362 sizeof(WMenu)*(brother->cascade_no+1));
363 brother->cascades[brother->cascade_no++] = cascade->brother;
367 if (menu->flags.lowered) {
369 cascade->flags.lowered = 1;
370 ChangeStackingLevel(cascade->frame->core, WMNormalLevel);
372 cascade->brother->flags.lowered = 1;
373 ChangeStackingLevel(cascade->brother->frame->core, WMNormalLevel);
376 if (!menu->flags.realized)
377 wMenuRealize(menu);
381 void
382 wMenuEntryRemoveCascade(WMenu *menu, WMenuEntry *entry)
384 assert(menu->flags.brother==0);
386 /* destroy cascade menu */
387 if (entry->cascade>=0 && menu->cascades
388 && menu->cascades[entry->cascade]!=NULL) {
390 wMenuDestroy(menu->cascades[entry->cascade], True);
392 menu->cascades[entry->cascade] = NULL;
393 menu->brother->cascades[entry->cascade] = NULL;
395 entry->cascade = -1;
400 void
401 wMenuRemoveItem(WMenu *menu, int index)
403 int i;
405 if (menu->flags.brother) {
406 wMenuRemoveItem(menu->brother, index);
407 return;
410 if (index>=menu->entry_no) return;
412 /* destroy cascade menu */
413 wMenuEntryRemoveCascade(menu, menu->entries[index]);
415 /* destroy unshared data */
417 if (menu->entries[index]->text)
418 wfree(menu->entries[index]->text);
420 if (menu->entries[index]->rtext)
421 wfree(menu->entries[index]->rtext);
423 if (menu->entries[index]->free_cdata && menu->entries[index]->clientdata)
424 (*menu->entries[index]->free_cdata)(menu->entries[index]->clientdata);
426 wfree(menu->entries[index]);
428 for (i=index; i<menu->entry_no-1; i++) {
429 menu->entries[i+1]->order--;
430 menu->entries[i]=menu->entries[i+1];
432 menu->entry_no--;
433 menu->brother->entry_no--;
437 static Pixmap
438 renderTexture(WMenu *menu)
440 RImage *img;
441 Pixmap pix;
442 int i;
443 RColor light;
444 RColor dark;
445 RColor mid;
446 WScreen *scr = menu->menu->screen_ptr;
447 WTexture *texture = scr->menu_item_texture;
449 if (wPreferences.menu_style == MS_NORMAL) {
450 img = wTextureRenderImage(texture, menu->menu->width,
451 menu->entry_height, WREL_MENUENTRY);
452 } else {
453 img = wTextureRenderImage(texture, menu->menu->width,
454 menu->menu->height+1, WREL_MENUENTRY);
456 if (!img) {
457 wwarning(_("could not render texture: %s"),
458 RMessageForError(RErrorCode));
460 return None;
463 if (wPreferences.menu_style == MS_SINGLE_TEXTURE) {
464 light.alpha = 0;
465 light.red = light.green = light.blue = 80;
467 dark.alpha = 255;
468 dark.red = dark.green = dark.blue = 0;
470 mid.alpha = 0;
471 mid.red = mid.green = mid.blue = 40;
473 for (i = 1; i < menu->entry_no; i++) {
474 ROperateLine(img, RSubtractOperation, 0, i*menu->entry_height-2,
475 menu->menu->width-1, i*menu->entry_height-2, &mid);
477 RDrawLine(img, 0, i*menu->entry_height-1,
478 menu->menu->width-1, i*menu->entry_height-1, &dark);
480 ROperateLine(img, RAddOperation, 0, i*menu->entry_height,
481 menu->menu->width-1, i*menu->entry_height,
482 &light);
485 if (!RConvertImage(scr->rcontext, img, &pix)) {
486 wwarning(_("error rendering image:%s"), RMessageForError(RErrorCode));
488 RReleaseImage(img);
490 return pix;
494 static void
495 updateTexture(WMenu *menu)
497 WScreen *scr = menu->menu->screen_ptr;
499 /* setup background texture */
500 if (scr->menu_item_texture->any.type != WTEX_SOLID) {
501 if (!menu->flags.brother) {
502 FREE_PIXMAP(menu->menu_texture_data);
504 menu->menu_texture_data = renderTexture(menu);
506 XSetWindowBackgroundPixmap(dpy, menu->menu->window,
507 menu->menu_texture_data);
508 XClearWindow(dpy, menu->menu->window);
510 XSetWindowBackgroundPixmap(dpy, menu->brother->menu->window,
511 menu->menu_texture_data);
512 XClearWindow(dpy, menu->brother->menu->window);
514 } else {
515 XSetWindowBackground(dpy, menu->menu->window,
516 scr->menu_item_texture->any.color.pixel);
517 XClearWindow(dpy, menu->menu->window);
522 void
523 wMenuRealize(WMenu *menu)
525 int i;
526 int width, rwidth, mrwidth, mwidth;
527 int theight, twidth, eheight;
528 WScreen *scr = menu->frame->screen_ptr;
529 static int brother_done=0;
530 int flags;
532 if (!brother_done) {
533 brother_done = 1;
534 wMenuRealize(menu->brother);
535 brother_done = 0;
538 flags = WFF_SINGLE_STATE|WFF_BORDER;
539 if (menu->flags.titled)
540 flags |= WFF_TITLEBAR|WFF_RIGHT_BUTTON;
542 wFrameWindowUpdateBorders(menu->frame, flags);
544 if (menu->flags.titled) {
545 twidth = WMWidthOfString(scr->menu_title_font, menu->frame->title,
546 strlen(menu->frame->title));
547 theight = menu->frame->top_width;
548 twidth += theight + (wPreferences.new_style ? 16 : 8);
549 } else {
550 twidth = 0;
551 theight = 0;
553 eheight = WMFontHeight(scr->menu_entry_font) + 6 + wPreferences.menu_text_clearance * 2;
554 menu->entry_height = eheight;
555 mrwidth = 0;
556 mwidth = 0;
557 for (i=0; i<menu->entry_no; i++) {
558 char *text;
560 /* search widest text */
561 text = menu->entries[i]->text;
562 width = WMWidthOfString(scr->menu_entry_font, text, strlen(text))+10;
564 if (menu->entries[i]->flags.indicator) {
565 width += MENU_INDICATOR_SPACE;
568 if (width > mwidth)
569 mwidth = width;
571 /* search widest text on right */
572 text = menu->entries[i]->rtext;
573 if (text)
574 rwidth = WMWidthOfString(scr->menu_entry_font, text, strlen(text))
575 + 5;
576 else if (menu->entries[i]->cascade>=0)
577 rwidth = 16;
578 else
579 rwidth = 4;
581 if (rwidth > mrwidth)
582 mrwidth = rwidth;
584 mwidth += mrwidth;
586 if (mwidth < twidth)
587 mwidth = twidth;
590 wCoreConfigure(menu->menu, 0, theight, mwidth, menu->entry_no*eheight -1);
592 wFrameWindowResize(menu->frame, mwidth, menu->entry_no*eheight-1
593 + menu->frame->top_width + menu->frame->bottom_width);
596 updateTexture(menu);
598 menu->flags.realized = 1;
600 if (menu->flags.mapped)
601 wMenuPaint(menu);
602 if (menu->brother->flags.mapped)
603 wMenuPaint(menu->brother);
607 void
608 wMenuDestroy(WMenu *menu, int recurse)
610 int i;
612 WMRemoveNotificationObserver(menu);
614 /* remove any pending timers */
615 if (menu->timer)
616 WMDeleteTimerHandler(menu->timer);
617 menu->timer = NULL;
619 /* call destroy handler */
620 if (menu->on_destroy)
621 (*menu->on_destroy)(menu);
623 /* Destroy items if this menu own them. If this is the "brother" menu,
624 * leave them alone as it is shared by them.
626 if (!menu->flags.brother) {
627 for (i=0; i<menu->entry_no; i++) {
629 wfree(menu->entries[i]->text);
631 if (menu->entries[i]->rtext)
632 wfree(menu->entries[i]->rtext);
633 #ifdef USER_MENU
635 if (menu->entries[i]->instances){
636 WMReleasePropList(menu->entries[i]->instances);
638 #endif /* USER_MENU */
640 if (menu->entries[i]->free_cdata && menu->entries[i]->clientdata) {
641 (*menu->entries[i]->free_cdata)(menu->entries[i]->clientdata);
643 wfree(menu->entries[i]);
646 if (recurse) {
647 for (i=0; i<menu->cascade_no; i++) {
648 if (menu->cascades[i]) {
649 if (menu->cascades[i]->flags.brother)
650 wMenuDestroy(menu->cascades[i]->brother, recurse);
651 else
652 wMenuDestroy(menu->cascades[i], recurse);
657 if (menu->entries)
658 wfree(menu->entries);
662 FREE_PIXMAP(menu->menu_texture_data);
664 if (menu->cascades)
665 wfree(menu->cascades);
667 wCoreDestroy(menu->menu);
668 wFrameWindowDestroy(menu->frame);
670 /* destroy copy of this menu */
671 if (!menu->flags.brother && menu->brother)
672 wMenuDestroy(menu->brother, False);
674 wfree(menu);
678 #define F_NORMAL 0
679 #define F_TOP 1
680 #define F_BOTTOM 2
681 #define F_NONE 3
683 static void
684 drawFrame(WScreen *scr, Drawable win, int y, int w, int h, int type)
686 XSegment segs[2];
687 int i;
689 i = 0;
690 segs[i].x1 = segs[i].x2 = w-1;
691 segs[i].y1 = y;
692 segs[i].y2 = y + h - 1;
693 i++;
694 if (type != F_TOP && type != F_NONE) {
695 segs[i].x1 = 1;
696 segs[i].y1 = segs[i].y2 = y + h-2;
697 segs[i].x2 = w-1;
698 i++;
700 XDrawSegments(dpy, win, scr->menu_item_auxtexture->dim_gc, segs, i);
702 i = 0;
703 segs[i].x1 = 0;
704 segs[i].y1 = y;
705 segs[i].x2 = 0;
706 segs[i].y2 = y + h - 1;
707 i++;
708 if (type != F_BOTTOM && type != F_NONE) {
709 segs[i].x1 = 0;
710 segs[i].y1 = y;
711 segs[i].x2 = w-1;
712 segs[i].y2 = y;
713 i++;
715 XDrawSegments(dpy, win, scr->menu_item_auxtexture->light_gc, segs, i);
717 if (type != F_TOP && type != F_NONE)
718 XDrawLine(dpy, win, scr->menu_item_auxtexture->dark_gc, 0, y+h-1,
719 w-1, y+h-1);
723 static void
724 paintEntry(WMenu *menu, int index, int selected)
726 int x, y, w, h, tw;
727 int type;
728 GC light, dim, dark, textGC, saveGC;
729 WMColor *color;
730 WMPixel pixel, savePixel;
731 WScreen *scr=menu->frame->screen_ptr;
732 Window win = menu->menu->window;
733 WMenuEntry *entry=menu->entries[index];
735 if (!menu->flags.realized) return;
736 h = menu->entry_height;
737 w = menu->menu->width;
738 y = index * h;
740 light = scr->menu_item_auxtexture->light_gc;
741 dim = scr->menu_item_auxtexture->dim_gc;
742 dark = scr->menu_item_auxtexture->dark_gc;
744 if (wPreferences.menu_style == MS_FLAT && menu->entry_no > 1) {
745 if (index == 0)
746 type = F_TOP;
747 else if (index == menu->entry_no - 1)
748 type = F_BOTTOM;
749 else
750 type = F_NONE;
751 } else {
752 type = F_NORMAL;
755 /* paint background */
756 if (selected) {
757 XSetForeground(dpy, scr->select_menu_gc, scr->select_pixel);
758 XFillRectangle(dpy, win, scr->select_menu_gc, 1, y+1, w-2, h-3);
759 if (scr->menu_item_texture->any.type == WTEX_SOLID)
760 drawFrame(scr, win, y, w, h, type);
761 } else {
762 if (scr->menu_item_texture->any.type == WTEX_SOLID) {
763 XClearArea(dpy, win, 0, y + 1, w - 1, h - 3, False);
764 /* draw the frame */
765 drawFrame(scr, win, y, w, h, type);
766 } else {
767 XClearArea(dpy, win, 0, y, w, h, False);
771 /* very ugly hack */
772 saveGC = scr->wmscreen->drawStringGC;
773 color = WMBlackColor(scr->wmscreen);
774 savePixel = color->color.pixel;
776 if (selected) {
777 textGC = scr->select_menu_gc;
778 if (entry->flags.enabled)
779 pixel = scr->select_text_pixel;
780 else
781 pixel = scr->dtext_pixel;
782 } else if (!entry->flags.enabled) {
783 textGC = scr->disabled_menu_entry_gc;
784 pixel = scr->dtext_pixel;
785 } else {
786 textGC = scr->menu_entry_gc;
787 pixel = scr->mtext_pixel;
789 /* draw text */
790 x = 5;
791 if (entry->flags.indicator)
792 x += MENU_INDICATOR_SPACE + 2;
794 /* this is nasty */
795 scr->wmscreen->drawStringGC = textGC;
796 color->color.pixel = pixel;
797 WMDrawString(scr->wmscreen, win, color, scr->menu_entry_font,
798 x, 3 + y + wPreferences.menu_text_clearance, entry->text, strlen(entry->text));
799 color->color.pixel = savePixel;
800 scr->wmscreen->drawStringGC = saveGC;
802 if (entry->cascade>=0) {
803 /* draw the cascade indicator */
804 XDrawLine(dpy,win,dim, w-11, y+6, w-6, y+h/2-1);
805 XDrawLine(dpy,win,light, w-11, y+h-8, w-6, y+h/2-1);
806 XDrawLine(dpy,win,dark, w-12, y+6, w-12, y+h-8);
809 /* draw indicator */
810 if (entry->flags.indicator && entry->flags.indicator_on) {
811 int iw, ih;
812 WPixmap *indicator;
815 switch (entry->flags.indicator_type) {
816 case MI_CHECK:
817 indicator = scr->menu_check_indicator;
818 break;
819 case MI_MINIWINDOW:
820 indicator = scr->menu_mini_indicator;
821 break;
822 case MI_HIDDEN:
823 indicator = scr->menu_hide_indicator;
824 break;
825 case MI_SHADED:
826 indicator = scr->menu_shade_indicator;
827 break;
828 case MI_DIAMOND:
829 default:
830 indicator = scr->menu_radio_indicator;
831 break;
834 iw = indicator->width;
835 ih = indicator->height;
836 XSetClipMask(dpy, scr->copy_gc, indicator->mask);
837 XSetClipOrigin(dpy, scr->copy_gc, 5, y+(h-ih)/2);
838 if (selected) {
839 if (entry->flags.enabled) {
840 XSetForeground(dpy, scr->copy_gc, scr->black_pixel);
841 } else {
842 XSetForeground(dpy, scr->copy_gc, scr->dtext_pixel);
844 } else {
845 if (entry->flags.enabled) {
846 XSetForeground(dpy, scr->copy_gc, scr->mtext_pixel);
847 } else {
848 XSetForeground(dpy, scr->copy_gc, scr->dtext_pixel);
851 XFillRectangle(dpy, win, scr->copy_gc, 5, y+(h-ih)/2, iw, ih);
853 XCopyArea(dpy, indicator->image, win, scr->copy_gc, 0, 0,
854 iw, ih, 5, y+(h-ih)/2);
856 XSetClipOrigin(dpy, scr->copy_gc, 0, 0);
859 /* draw right text */
861 if (entry->rtext && entry->cascade<0) {
862 tw = WMWidthOfString(scr->menu_entry_font, entry->rtext,
863 strlen(entry->rtext));
865 /* this is nasty */
866 scr->wmscreen->drawStringGC = textGC;
867 color->color.pixel = pixel;
868 WMDrawString(scr->wmscreen, win, color, scr->menu_entry_font, w-6-tw,
869 y + 3 + wPreferences.menu_text_clearance, entry->rtext, strlen(entry->rtext));
870 color->color.pixel = savePixel;
871 scr->wmscreen->drawStringGC = saveGC;
873 WMReleaseColor(color);
877 static void
878 move_menus(WMenu *menu, int x, int y)
880 while (menu->parent) {
881 menu = menu->parent;
882 x -= MENUW(menu);
883 if (!wPreferences.align_menus && menu->selected_entry>=0) {
884 y -= menu->selected_entry*menu->entry_height;
887 wMenuMove(menu, x, y, True);
890 static void
891 makeVisible(WMenu *menu)
893 WScreen *scr = menu->frame->screen_ptr;
894 int x1, y1, x2, y2, new_x, new_y, move;
896 if (menu->entry_no<0) return;
898 x1 = menu->frame_x;
899 y1 = menu->frame_y+menu->frame->top_width
900 + menu->selected_entry*menu->entry_height;
901 x2 = x1 + MENUW(menu);
902 y2 = y1 + menu->entry_height;
904 new_x = x1;
905 new_y = y1;
906 move = 0;
908 if (x1 < 0) {
909 new_x = 0;
910 move = 1;
911 } else if (x2 >= scr->scr_width) {
912 new_x = scr->scr_width - MENUW(menu) - 1;
913 move = 1;
916 if (y1 < 0) {
917 new_y = 0;
918 move = 1;
919 } else if (y2 >= scr->scr_height) {
920 new_y = scr->scr_height - menu->entry_height - 1;
921 move = 1;
924 new_y = new_y - menu->frame->top_width
925 - menu->selected_entry*menu->entry_height;
926 move_menus(menu, new_x, new_y);
930 static int
931 check_key(WMenu *menu, XKeyEvent *event)
933 int i, ch, s;
934 char buffer[32];
936 if (XLookupString(event, buffer, 32, NULL, NULL)<1)
937 return -1;
939 ch = toupper(buffer[0]);
941 s = (menu->selected_entry>=0 ? menu->selected_entry+1 : 0);
943 again:
944 for (i=s; i<menu->entry_no; i++) {
945 if (ch==toupper(menu->entries[i]->text[0])) {
946 return i;
949 /* no match. Retry from start, if previous started from a selected entry */
950 if (s!=0) {
951 s = 0;
952 goto again;
954 return -1;
958 static int
959 keyboardMenu(WMenu *menu)
961 XEvent event;
962 KeySym ksym=NoSymbol;
963 int done=0;
964 int index;
965 WMenuEntry *entry;
966 int old_pos_x = menu->frame_x;
967 int old_pos_y = menu->frame_y;
968 int new_x = old_pos_x, new_y = old_pos_y;
969 int scr_width = menu->frame->screen_ptr->scr_width;
970 int scr_height = menu->frame->screen_ptr->scr_height;
972 if (menu->flags.editing)
973 return False;
976 XGrabKeyboard(dpy, menu->frame->core->window, True, GrabModeAsync,
977 GrabModeAsync, CurrentTime);
979 if (menu->frame_y+menu->frame->top_width >= scr_height)
980 new_y = scr_height - menu->frame->top_width;
982 if (menu->frame_x+MENUW(menu) >= scr_width)
983 new_x = scr_width-MENUW(menu)-1;
985 move_menus(menu, new_x, new_y);
987 while (!done && menu->flags.mapped) {
988 XAllowEvents(dpy, AsyncKeyboard, CurrentTime);
989 WMMaskEvent(dpy, ExposureMask|ButtonMotionMask|ButtonPressMask
990 |ButtonReleaseMask|KeyPressMask|KeyReleaseMask
991 |SubstructureNotifyMask, &event);
993 switch (event.type) {
994 case KeyPress:
995 ksym = XLookupKeysym(&event.xkey, 0);
996 switch (ksym) {
997 case XK_Escape:
998 done = 1;
999 break;
1001 case XK_Home:
1002 #ifdef XK_KP_Home
1003 case XK_KP_Home:
1004 #endif
1005 selectEntry(menu, 0);
1006 makeVisible(menu);
1007 break;
1009 case XK_End:
1010 #ifdef XK_KP_End
1011 case XK_KP_End:
1012 #endif
1013 selectEntry(menu, menu->entry_no-1);
1014 makeVisible(menu);
1015 break;
1017 case XK_Up:
1018 #ifdef ARROWLESS_KBD
1019 case XK_k:
1020 #endif
1021 #ifdef XK_KP_Up
1022 case XK_KP_Up:
1023 #endif
1024 if (menu->selected_entry <= 0)
1025 selectEntry(menu, menu->entry_no-1);
1026 else
1027 selectEntry(menu, menu->selected_entry-1);
1028 makeVisible(menu);
1029 break;
1031 case XK_Down:
1032 #ifdef ARROWLESS_KBD
1033 case XK_j:
1034 #endif
1035 #ifdef XK_KP_Down
1036 case XK_KP_Down:
1037 #endif
1038 if (menu->selected_entry<0)
1039 selectEntry(menu, 0);
1040 else if (menu->selected_entry == menu->entry_no-1)
1041 selectEntry(menu, 0);
1042 else if (menu->selected_entry < menu->entry_no-1)
1043 selectEntry(menu, menu->selected_entry+1);
1044 makeVisible(menu);
1045 break;
1047 case XK_Right:
1048 #ifdef ARROWLESS_KBD
1049 case XK_l:
1050 #endif
1051 #ifdef XK_KP_Right
1052 case XK_KP_Right:
1053 #endif
1054 if (menu->selected_entry>=0) {
1055 WMenuEntry *entry;
1056 entry = menu->entries[menu->selected_entry];
1058 if (entry->cascade >= 0 && menu->cascades
1059 && menu->cascades[entry->cascade]->entry_no > 0) {
1061 XUngrabKeyboard(dpy, CurrentTime);
1063 selectEntry(menu->cascades[entry->cascade], 0);
1064 if (!keyboardMenu(menu->cascades[entry->cascade]))
1065 done = 1;
1067 XGrabKeyboard(dpy, menu->frame->core->window, True,
1068 GrabModeAsync, GrabModeAsync,
1069 CurrentTime);
1072 break;
1074 case XK_Left:
1075 #ifdef ARROWLESS_KBD
1076 case XK_h:
1077 #endif
1078 #ifdef XK_KP_Left
1079 case XK_KP_Left:
1080 #endif
1081 if (menu->parent!=NULL && menu->parent->selected_entry>=0) {
1082 selectEntry(menu, -1);
1083 move_menus(menu, old_pos_x, old_pos_y);
1084 return True;
1086 break;
1088 case XK_Return:
1089 done = 2;
1090 break;
1092 default:
1093 index = check_key(menu, &event.xkey);
1094 if (index>=0) {
1095 selectEntry(menu, index);
1098 break;
1100 default:
1101 if (event.type==ButtonPress)
1102 done = 1;
1104 WMHandleEvent(&event);
1108 XUngrabKeyboard(dpy, CurrentTime);
1110 if (done==2 && menu->selected_entry>=0) {
1111 entry = menu->entries[menu->selected_entry];
1112 } else {
1113 entry = NULL;
1116 if (entry && entry->callback!=NULL && entry->flags.enabled
1117 && entry->cascade < 0) {
1118 #if (MENU_BLINK_COUNT > 0)
1119 int sel = menu->selected_entry;
1120 int i;
1122 for (i=0; i<MENU_BLINK_COUNT; i++) {
1123 paintEntry(menu, sel, False);
1124 XSync(dpy, 0);
1125 wusleep(MENU_BLINK_DELAY);
1126 paintEntry(menu, sel, True);
1127 XSync(dpy, 0);
1128 wusleep(MENU_BLINK_DELAY);
1130 #endif
1131 selectEntry(menu, -1);
1133 if (!menu->flags.buttoned) {
1134 wMenuUnmap(menu);
1135 move_menus(menu, old_pos_x, old_pos_y);
1137 closeCascade(menu);
1139 (*entry->callback)(menu, entry);
1140 } else {
1141 if (!menu->flags.buttoned) {
1142 wMenuUnmap(menu);
1143 move_menus(menu, old_pos_x, old_pos_y);
1145 selectEntry(menu, -1);
1149 /* returns True if returning from a submenu to a parent menu,
1150 * False if exiting from menu */
1151 return False;
1155 void
1156 wMenuMapAt(WMenu *menu, int x, int y, int keyboard)
1158 int scr_width = menu->frame->screen_ptr->scr_width;
1159 int scr_height = menu->frame->screen_ptr->scr_height;
1161 if (!menu->flags.realized) {
1162 menu->flags.realized=1;
1163 wMenuRealize(menu);
1165 if (!menu->flags.mapped) {
1166 if (wPreferences.wrap_menus) {
1167 if (x<0) x = 0;
1168 if (y<0) y = 0;
1169 if (x+MENUW(menu) > scr_width)
1170 x = scr_width - MENUW(menu);
1171 if (y+MENUH(menu) > scr_height)
1172 y = scr_height - MENUH(menu);
1175 XMoveWindow(dpy, menu->frame->core->window, x, y);
1176 menu->frame_x = x;
1177 menu->frame_y = y;
1178 XMapWindow(dpy, menu->frame->core->window);
1179 wRaiseFrame(menu->frame->core);
1180 menu->flags.mapped = 1;
1181 } else {
1182 selectEntry(menu, 0);
1185 if (keyboard)
1186 keyboardMenu(menu);
1190 void
1191 wMenuMap(WMenu *menu)
1193 if (!menu->flags.realized) {
1194 menu->flags.realized=1;
1195 wMenuRealize(menu);
1197 if (menu->flags.app_menu && menu->parent==NULL) {
1198 menu->frame_x = menu->frame->screen_ptr->app_menu_x;
1199 menu->frame_y = menu->frame->screen_ptr->app_menu_y;
1200 XMoveWindow(dpy, menu->frame->core->window, menu->frame_x, menu->frame_y);
1202 XMapWindow(dpy, menu->frame->core->window);
1203 wRaiseFrame(menu->frame->core);
1204 menu->flags.mapped = 1;
1208 void
1209 wMenuUnmap(WMenu *menu)
1211 int i;
1213 XUnmapWindow(dpy, menu->frame->core->window);
1214 if (menu->flags.titled && menu->flags.buttoned) {
1215 wFrameWindowHideButton(menu->frame, WFF_RIGHT_BUTTON);
1217 menu->flags.buttoned = 0;
1218 menu->flags.mapped = 0;
1219 menu->flags.open_to_left = 0;
1221 for (i=0; i<menu->cascade_no; i++) {
1222 if (menu->cascades[i]!=NULL
1223 && menu->cascades[i]->flags.mapped
1224 && !menu->cascades[i]->flags.buttoned) {
1226 wMenuUnmap(menu->cascades[i]);
1229 menu->selected_entry = -1;
1234 void
1235 wMenuPaint(WMenu *menu)
1237 int i;
1239 if (!menu->flags.mapped) {
1240 return;
1243 /* paint entries */
1244 for (i=0; i<menu->entry_no; i++) {
1245 paintEntry(menu, i, i==menu->selected_entry);
1250 void
1251 wMenuSetEnabled(WMenu *menu, int index, int enable)
1253 if (index>=menu->entry_no) return;
1254 menu->entries[index]->flags.enabled=enable;
1255 paintEntry(menu, index, index==menu->selected_entry);
1256 paintEntry(menu->brother, index, index==menu->selected_entry);
1260 /* ====================================================================== */
1263 static void
1264 editEntry(WMenu *menu, WMenuEntry *entry)
1266 WTextInput *text;
1267 XEvent event;
1268 WObjDescriptor *desc;
1269 char *t;
1270 int done = 0;
1271 Window old_focus;
1272 int old_revert;
1274 menu->flags.editing = 1;
1276 text = wTextCreate(menu->menu, 1, menu->entry_height * entry->order,
1277 menu->menu->width - 2, menu->entry_height - 1);
1279 wTextPutText(text, entry->text);
1280 XGetInputFocus(dpy, &old_focus, &old_revert);
1281 XSetInputFocus(dpy, text->core->window, RevertToNone, CurrentTime);
1283 if (XGrabKeyboard(dpy, text->core->window, True, GrabModeAsync,
1284 GrabModeAsync, CurrentTime)!=GrabSuccess) {
1285 wwarning(_("could not grab keyboard"));
1286 wTextDestroy(text);
1288 wSetFocusTo(menu->frame->screen_ptr,
1289 menu->frame->screen_ptr->focused_window);
1290 return;
1294 while (!done && !text->done) {
1295 XSync(dpy, 0);
1296 XAllowEvents(dpy, AsyncKeyboard|AsyncPointer, CurrentTime);
1297 XSync(dpy, 0);
1298 WMNextEvent(dpy, &event);
1300 if (XFindContext(dpy, event.xany.window, wWinContext,
1301 (XPointer *)&desc)==XCNOENT)
1302 desc = NULL;
1304 if ((desc != NULL) && (desc->handle_anything != NULL)) {
1306 (*desc->handle_anything)(desc, &event);
1308 } else {
1309 switch (event.type) {
1310 case ButtonPress:
1311 XAllowEvents(dpy, ReplayPointer, CurrentTime);
1312 done = 1;
1314 default:
1315 WMHandleEvent(&event);
1316 break;
1321 XSetInputFocus(dpy, old_focus, old_revert, CurrentTime);
1323 wSetFocusTo(menu->frame->screen_ptr,
1324 menu->frame->screen_ptr->focused_window);
1327 t = wTextGetText(text);
1328 /* if !t, the user has canceled editing */
1329 if (t) {
1330 if (entry->text)
1331 wfree(entry->text);
1332 entry->text = wstrdup(t);
1334 menu->flags.realized = 0;
1336 wTextDestroy(text);
1338 XUngrabKeyboard(dpy, CurrentTime);
1340 if (t && menu->on_edit)
1341 (*menu->on_edit)(menu, entry);
1343 menu->flags.editing = 0;
1345 if (!menu->flags.realized)
1346 wMenuRealize(menu);
1350 static void
1351 selectEntry(WMenu *menu, int entry_no)
1353 WMenuEntry *entry;
1354 WMenu *submenu;
1355 int old_entry;
1357 if (menu->entries==NULL)
1358 return;
1360 if (entry_no >= menu->entry_no)
1361 return;
1363 old_entry = menu->selected_entry;
1364 menu->selected_entry = entry_no;
1366 if (old_entry!=entry_no) {
1368 /* unselect previous entry */
1369 if (old_entry>=0) {
1370 paintEntry(menu, old_entry, False);
1371 entry = menu->entries[old_entry];
1373 /* unmap cascade */
1374 if (entry->cascade>=0 && menu->cascades) {
1375 if (!menu->cascades[entry->cascade]->flags.buttoned) {
1376 wMenuUnmap(menu->cascades[entry->cascade]);
1381 if (entry_no<0) {
1382 menu->selected_entry = -1;
1383 return;
1385 entry = menu->entries[entry_no];
1387 if (entry->cascade>=0 && menu->cascades && entry->flags.enabled) {
1388 /* Callback for when the submenu is opened.
1390 submenu = menu->cascades[entry->cascade];
1391 if (submenu && submenu->flags.brother)
1392 submenu = submenu->brother;
1394 if (entry->callback) {
1395 /* Only call the callback if the submenu is not yet mapped.
1397 if (menu->flags.brother) {
1398 if (!submenu || !submenu->flags.mapped)
1399 (*entry->callback)(menu->brother, entry);
1400 } else {
1401 if (!submenu || !submenu->flags.buttoned)
1402 (*entry->callback)(menu, entry);
1406 /* the submenu menu might have changed */
1407 submenu = menu->cascades[entry->cascade];
1409 /* map cascade */
1410 if (!submenu->flags.mapped) {
1411 int x, y;
1413 if (!submenu->flags.realized)
1414 wMenuRealize(submenu);
1415 if (wPreferences.wrap_menus) {
1416 if (menu->flags.open_to_left)
1417 submenu->flags.open_to_left = 1;
1419 if (submenu->flags.open_to_left) {
1420 x = menu->frame_x - MENUW(submenu);
1421 if (x<0) {
1422 x = 0;
1423 submenu->flags.open_to_left = 0;
1425 } else {
1426 x = menu->frame_x + MENUW(menu);
1428 if (x + MENUW(submenu)
1429 >= menu->frame->screen_ptr->scr_width) {
1431 x = menu->frame_x - MENUW(submenu);
1432 submenu->flags.open_to_left = 1;
1435 } else {
1436 x = menu->frame_x + MENUW(menu);
1439 if (wPreferences.align_menus) {
1440 y = menu->frame_y;
1441 } else {
1442 y = menu->frame_y + menu->entry_height*entry_no;
1443 if (menu->flags.titled)
1444 y += menu->frame->top_width;
1445 if (menu->cascades[entry->cascade]->flags.titled)
1446 y -= menu->cascades[entry->cascade]->frame->top_width;
1449 wMenuMapAt(menu->cascades[entry->cascade], x, y, False);
1450 menu->cascades[entry->cascade]->parent = menu;
1451 } else {
1452 return;
1455 paintEntry(menu, entry_no, True);
1460 static WMenu*
1461 findMenu(WScreen *scr, int *x_ret, int *y_ret)
1463 WMenu *menu;
1464 WObjDescriptor *desc;
1465 Window root_ret, win, junk_win;
1466 int x, y, wx, wy;
1467 unsigned int mask;
1469 XQueryPointer(dpy, scr->root_win, &root_ret, &win, &x, &y, &wx, &wy,
1470 &mask);
1472 if (win==None) return NULL;
1474 if (XFindContext(dpy, win, wWinContext, (XPointer *)&desc)==XCNOENT)
1475 return NULL;
1477 if (desc->parent_type == WCLASS_MENU) {
1478 menu = (WMenu*)desc->parent;
1479 XTranslateCoordinates(dpy, root_ret, menu->menu->window, wx, wy,
1480 x_ret, y_ret, &junk_win);
1481 return menu;
1483 return NULL;
1489 static void
1490 closeCascade(WMenu *menu)
1492 WMenu *parent=menu->parent;
1494 if (menu->flags.brother
1495 || (!menu->flags.buttoned
1496 && (!menu->flags.app_menu||menu->parent!=NULL))) {
1498 selectEntry(menu, -1);
1499 XSync(dpy, 0);
1500 #if (MENU_BLINK_DELAY > 2)
1501 wusleep(MENU_BLINK_DELAY/2);
1502 #endif
1503 wMenuUnmap(menu);
1504 while (parent!=NULL
1505 && (parent->parent!=NULL || !parent->flags.app_menu
1506 || parent->flags.brother)
1507 && !parent->flags.buttoned) {
1508 selectEntry(parent, -1);
1509 wMenuUnmap(parent);
1510 parent = parent->parent;
1512 if (parent)
1513 selectEntry(parent, -1);
1518 static void
1519 closeBrotherCascadesOf(WMenu *menu)
1521 WMenu *tmp;
1522 int i;
1524 for (i=0; i<menu->cascade_no; i++) {
1525 if (menu->cascades[i]->flags.brother) {
1526 tmp = menu->cascades[i];
1527 } else {
1528 tmp = menu->cascades[i]->brother;
1530 if (tmp->flags.mapped) {
1531 selectEntry(tmp->parent, -1);
1532 closeBrotherCascadesOf(tmp);
1533 break;
1539 #define getEntryAt(menu, x, y) ((y)<0 ? -1 : (y)/(menu->entry_height))
1542 static WMenu*
1543 parentMenu(WMenu *menu)
1545 WMenu *parent;
1546 WMenuEntry *entry;
1548 if (menu->flags.buttoned)
1549 return menu;
1551 while (menu->parent && menu->parent->flags.mapped) {
1552 parent = menu->parent;
1553 if (parent->selected_entry < 0)
1554 break;
1555 entry = parent->entries[parent->selected_entry];
1556 if (!entry->flags.enabled || entry->cascade<0 || !parent->cascades ||
1557 parent->cascades[entry->cascade] != menu)
1558 break;
1559 menu = parent;
1560 if (menu->flags.buttoned)
1561 break;
1564 return menu;
1570 * Will raise the passed menu, if submenu = 0
1571 * If submenu > 0 will also raise all mapped submenus
1572 * until the first buttoned one
1573 * If submenu < 0 will also raise all mapped parent menus
1574 * until the first buttoned one
1577 static void
1578 raiseMenus(WMenu *menu, int submenus)
1580 WMenu *submenu;
1581 int i;
1583 if(!menu) return;
1585 wRaiseFrame(menu->frame->core);
1587 if (submenus>0 && menu->selected_entry>=0) {
1588 i = menu->entries[menu->selected_entry]->cascade;
1589 if (i>=0 && menu->cascades) {
1590 submenu = menu->cascades[i];
1591 if (submenu->flags.mapped && !submenu->flags.buttoned)
1592 raiseMenus(submenu, submenus);
1595 if (submenus<0 && !menu->flags.buttoned &&
1596 menu->parent && menu->parent->flags.mapped)
1597 raiseMenus(menu->parent, submenus);
1601 WMenu*
1602 wMenuUnderPointer(WScreen *screen)
1604 WObjDescriptor *desc;
1605 Window root_ret, win;
1606 int dummy;
1607 unsigned int mask;
1609 XQueryPointer(dpy, screen->root_win, &root_ret, &win, &dummy, &dummy,
1610 &dummy, &dummy, &mask);
1612 if (win==None) return NULL;
1614 if (XFindContext(dpy, win, wWinContext, (XPointer *)&desc)==XCNOENT)
1615 return NULL;
1617 if (desc->parent_type == WCLASS_MENU)
1618 return (WMenu *)desc->parent;
1619 return NULL;
1625 static void
1626 getPointerPosition(WScreen *scr, int *x, int *y)
1628 Window root_ret, win;
1629 int wx, wy;
1630 unsigned int mask;
1632 XQueryPointer(dpy, scr->root_win, &root_ret, &win, x, y, &wx, &wy, &mask);
1636 static void
1637 getScrollAmount(WMenu *menu, int *hamount, int *vamount)
1639 WScreen *scr = menu->menu->screen_ptr;
1640 int menuX1 = menu->frame_x;
1641 int menuY1 = menu->frame_y;
1642 int menuX2 = menu->frame_x + MENUW(menu);
1643 int menuY2 = menu->frame_y + MENUH(menu);
1644 int screenW = scr->scr_width;
1645 int screenH = scr->scr_height;
1646 int xroot, yroot;
1648 *hamount = 0;
1649 *vamount = 0;
1651 getPointerPosition(scr, &xroot, &yroot);
1654 if (xroot <= 1 && menuX1 < 0) {
1655 /* scroll to the right */
1656 *hamount = WMIN(MENU_SCROLL_STEP, abs(menuX1));
1658 } else if (xroot >= screenW-2 && menuX2 > screenW-1) {
1659 /* scroll to the left */
1660 *hamount = WMIN(MENU_SCROLL_STEP, abs(menuX2-screenW-1));
1662 if (*hamount==0)
1663 *hamount = 1;
1665 *hamount = -*hamount;
1668 if (yroot <= 1 && menuY1 < 0) {
1669 /* scroll down */
1670 *vamount = WMIN(MENU_SCROLL_STEP, abs(menuY1));
1672 } else if (yroot >= screenH-2 && menuY2 > screenH-1) {
1673 /* scroll up */
1674 *vamount = WMIN(MENU_SCROLL_STEP, abs(menuY2-screenH-2));
1676 *vamount = -*vamount;
1681 static void
1682 dragScrollMenuCallback(void *data)
1684 WMenu *menu = (WMenu*)data;
1685 WScreen *scr = menu->menu->screen_ptr;
1686 WMenu *parent = parentMenu(menu);
1687 int hamount, vamount;
1688 int x, y;
1689 int newSelectedEntry;
1691 getScrollAmount(menu, &hamount, &vamount);
1694 if (hamount != 0 || vamount != 0) {
1695 wMenuMove(parent, parent->frame_x + hamount,
1696 parent->frame_y + vamount, True);
1697 if (findMenu(scr, &x, &y)) {
1698 newSelectedEntry = getEntryAt(menu, x, y);
1699 selectEntry(menu, newSelectedEntry);
1700 } else {
1701 /* Pointer fell outside of menu. If the selected entry is
1702 * not a submenu, unselect it */
1703 if (menu->selected_entry >= 0
1704 && menu->entries[menu->selected_entry]->cascade<0)
1705 selectEntry(menu, -1);
1706 newSelectedEntry = 0;
1709 /* paranoid check */
1710 if (newSelectedEntry >= 0) {
1711 /* keep scrolling */
1712 menu->timer = WMAddTimerHandler(MENU_SCROLL_DELAY,
1713 dragScrollMenuCallback, menu);
1714 } else {
1715 menu->timer = NULL;
1717 } else {
1718 /* don't need to scroll anymore */
1719 menu->timer = NULL;
1720 if (findMenu(scr, &x, &y)) {
1721 newSelectedEntry = getEntryAt(menu, x, y);
1722 selectEntry(menu, newSelectedEntry);
1728 static void
1729 scrollMenuCallback(void *data)
1731 WMenu *menu = (WMenu*)data;
1732 WMenu *parent = parentMenu(menu);
1733 int hamount = 0; /* amount to scroll */
1734 int vamount = 0;
1736 #ifdef VIRTUAL_DESKTOP
1737 /* don't scroll if it is in vdesk mode */
1738 if (!wPreferences.vedge_thickness)
1739 #endif
1740 getScrollAmount(menu, &hamount, &vamount);
1742 if (hamount != 0 || vamount != 0) {
1743 wMenuMove(parent, parent->frame_x + hamount,
1744 parent->frame_y + vamount, True);
1746 /* keep scrolling */
1747 menu->timer = WMAddTimerHandler(MENU_SCROLL_DELAY,
1748 scrollMenuCallback, menu);
1749 } else {
1750 /* don't need to scroll anymore */
1751 menu->timer = NULL;
1757 #define MENU_SCROLL_BORDER 5
1759 static int
1760 isPointNearBoder(WMenu *menu, int x, int y)
1762 int menuX1 = menu->frame_x;
1763 int menuY1 = menu->frame_y;
1764 int menuX2 = menu->frame_x + MENUW(menu);
1765 int menuY2 = menu->frame_y + MENUH(menu);
1766 int scrXe = menu->menu->screen_ptr->scr_width-1;
1767 int scrYe = menu->menu->screen_ptr->scr_height-1;
1768 int flag = 0;
1770 if (x >= menuX1 && x <= menuX2 && (y < MENU_SCROLL_BORDER
1771 || y > scrYe-MENU_SCROLL_BORDER))
1772 flag = 1;
1773 else if (y >= menuY1 && y <= menuY2 && (x < MENU_SCROLL_BORDER
1774 || x > scrXe-MENU_SCROLL_BORDER))
1775 flag = 1;
1777 return flag;
1781 typedef struct _delay {
1782 WWindow *wwin;
1783 WMenu *menu;
1784 int ox,oy;
1785 } _delay;
1788 static void
1789 _leaving(_delay *dl)
1791 wMenuMove(dl->menu, dl->ox, dl->oy, True);
1792 dl->menu->jump_back=NULL;
1793 dl->menu->menu->screen_ptr->flags.jump_back_pending = 0;
1794 wfree(dl);
1798 void
1799 wMenuScroll(WMenu *menu, XEvent *event)
1801 WMenu *smenu;
1802 WMenu *omenu = parentMenu(menu);
1803 WScreen *scr = menu->frame->screen_ptr;
1804 int done = 0;
1805 int jump_back = 0;
1806 int old_frame_x = omenu->frame_x;
1807 int old_frame_y = omenu->frame_y;
1808 XEvent ev;
1810 if (omenu->jump_back)
1811 WMDeleteTimerWithClientData(omenu->jump_back);
1814 if ((/*omenu->flags.buttoned &&*/ !wPreferences.wrap_menus)
1815 || omenu->flags.app_menu) {
1816 jump_back = 1;
1819 if (!wPreferences.wrap_menus)
1820 raiseMenus(omenu, True);
1821 else
1822 raiseMenus(menu, False);
1824 if (!menu->timer)
1825 scrollMenuCallback(menu);
1827 while(!done) {
1828 int x, y, on_border, on_x_edge, on_y_edge, on_title;
1830 WMNextEvent(dpy, &ev);
1831 switch (ev.type) {
1832 case EnterNotify:
1833 WMHandleEvent(&ev);
1834 case MotionNotify:
1835 x = (ev.type==MotionNotify) ? ev.xmotion.x_root : ev.xcrossing.x_root;
1836 y = (ev.type==MotionNotify) ? ev.xmotion.y_root : ev.xcrossing.y_root;
1838 /* on_border is != 0 if the pointer is between the menu
1839 * and the screen border and is close enough to the border */
1840 on_border = isPointNearBoder(menu, x, y);
1842 smenu = wMenuUnderPointer(scr);
1844 if ((smenu==NULL && !on_border) || (smenu && parentMenu(smenu)!=omenu)) {
1845 done = 1;
1846 break;
1849 on_x_edge = x <= 1 || x >= scr->scr_width - 2;
1850 on_y_edge = y <= 1 || y >= scr->scr_height - 2;
1851 on_border = on_x_edge || on_y_edge;
1853 if (!on_border && !jump_back) {
1854 done = 1;
1855 break;
1858 if (menu->timer && (smenu!=menu || (!on_y_edge && !on_x_edge))) {
1859 WMDeleteTimerHandler(menu->timer);
1860 menu->timer = NULL;
1863 if (smenu != NULL)
1864 menu = smenu;
1866 if (!menu->timer)
1867 scrollMenuCallback(menu);
1868 break;
1869 case ButtonPress:
1870 /* True if we push on title, or drag the omenu to other position */
1871 on_title = ev.xbutton.x_root >= omenu->frame_x &&
1872 ev.xbutton.x_root <= omenu->frame_x + MENUW(omenu) &&
1873 ev.xbutton.y_root >= omenu->frame_y &&
1874 ev.xbutton.y_root <= omenu->frame_y + omenu->frame->top_width;
1875 WMHandleEvent(&ev);
1876 smenu = wMenuUnderPointer(scr);
1877 if (smenu == NULL || (smenu && smenu->flags.buttoned && smenu != omenu))
1878 done = 1;
1879 else if (smenu==omenu && on_title) {
1880 jump_back = 0;
1881 done = 1;
1883 break;
1884 case KeyPress:
1885 done = 1;
1886 default:
1887 WMHandleEvent(&ev);
1888 break;
1892 if (menu->timer) {
1893 WMDeleteTimerHandler(menu->timer);
1894 menu->timer = NULL;
1897 if (jump_back) {
1898 _delay *delayer;
1899 if (!omenu->jump_back) {
1900 delayer=wmalloc(sizeof(_delay));
1901 delayer->menu=omenu;
1902 delayer->ox=old_frame_x;
1903 delayer->oy=old_frame_y;
1904 omenu->jump_back=delayer;
1905 scr->flags.jump_back_pending = 1;
1907 else delayer = omenu->jump_back;
1908 WMAddTimerHandler(MENU_JUMP_BACK_DELAY,(WMCallback*)_leaving, delayer);
1914 static void
1915 menuExpose(WObjDescriptor *desc, XEvent *event)
1917 wMenuPaint(desc->parent);
1920 typedef struct {
1921 int *delayed_select;
1922 WMenu *menu;
1923 WMHandlerID magic;
1924 } delay_data;
1927 static void
1928 delaySelection(void *data)
1930 delay_data *d = (delay_data*)data;
1931 int x, y, entry_no;
1932 WMenu *menu;
1934 d->magic = NULL;
1936 menu = findMenu(d->menu->menu->screen_ptr, &x, &y);
1937 if (menu && (d->menu == menu || d->delayed_select)) {
1938 entry_no = getEntryAt(menu, x, y);
1939 selectEntry(menu, entry_no);
1941 if (d->delayed_select)
1942 *(d->delayed_select) = 0;
1946 static void
1947 menuMouseDown(WObjDescriptor *desc, XEvent *event)
1949 XButtonEvent *bev = &event->xbutton;
1950 WMenu *menu = desc->parent;
1951 WMenu *smenu;
1952 WScreen *scr=menu->frame->screen_ptr;
1953 WMenuEntry *entry=NULL;
1954 XEvent ev;
1955 int close_on_exit=0;
1956 int done=0;
1957 int delayed_select = 0;
1958 int entry_no;
1959 int x, y;
1960 int prevx, prevy;
1961 int old_frame_x = 0;
1962 int old_frame_y = 0;
1963 delay_data d_data = {NULL, NULL, NULL};
1965 if (!wPreferences.wrap_menus) {
1966 smenu = parentMenu(menu);
1967 old_frame_x = smenu->frame_x;
1968 old_frame_y = smenu->frame_y;
1969 } else if (event->xbutton.window == menu->frame->core->window) {
1970 /* This is true if the menu was launched with right click on root window */
1971 delayed_select = 1;
1972 d_data.delayed_select = &delayed_select;
1973 d_data.menu = menu;
1974 d_data.magic = WMAddTimerHandler(wPreferences.dblclick_time,
1975 delaySelection, &d_data);
1978 if (menu->flags.inside_handler) {
1979 return;
1981 menu->flags.inside_handler = 1;
1983 wRaiseFrame(menu->frame->core);
1985 close_on_exit = (bev->send_event || menu->flags.brother);
1987 smenu = findMenu(scr, &x, &y);
1988 if (!smenu) {
1989 x = -1;
1990 y = -1;
1991 } else {
1992 menu = smenu;
1995 if (menu->flags.editing) {
1996 goto byebye;
1998 entry_no = getEntryAt(menu, x, y);
1999 if (entry_no>=0) {
2000 entry = menu->entries[entry_no];
2002 if (!close_on_exit && (bev->state & ControlMask) && smenu
2003 && entry->flags.editable) {
2004 editEntry(smenu, entry);
2005 goto byebye;
2006 } else if (bev->state & ControlMask) {
2007 goto byebye;
2010 if (entry->flags.enabled && entry->cascade>=0 && menu->cascades) {
2011 WMenu *submenu = menu->cascades[entry->cascade];
2012 /* map cascade */
2013 if (submenu->flags.mapped && !submenu->flags.buttoned &&
2014 menu->selected_entry!=entry_no) {
2015 wMenuUnmap(submenu);
2017 if (!submenu->flags.mapped && !delayed_select) {
2018 selectEntry(menu, entry_no);
2019 } else if (!submenu->flags.buttoned) {
2020 selectEntry(menu, -1);
2023 } else if (!delayed_select) {
2024 selectEntry(menu, entry_no);
2027 if (!wPreferences.wrap_menus && !wPreferences.scrollable_menus) {
2028 if (!menu->timer)
2029 dragScrollMenuCallback(menu);
2033 #ifdef VIRTUAL_DESKTOP
2034 if (wPreferences.vedge_thickness) {
2035 wWorkspaceLowerEdge(scr);
2037 #endif
2039 prevx = bev->x_root;
2040 prevy = bev->y_root;
2041 while (!done) {
2042 int x, y;
2044 XAllowEvents(dpy, AsyncPointer|SyncPointer, CurrentTime);
2046 WMMaskEvent(dpy, ExposureMask|ButtonMotionMask|ButtonReleaseMask
2047 |ButtonPressMask, &ev);
2048 switch (ev.type) {
2049 case MotionNotify:
2050 smenu = findMenu(scr, &x, &y);
2052 if (smenu == NULL) {
2053 /* moved mouse out of menu */
2055 if (!delayed_select && d_data.magic) {
2056 WMDeleteTimerHandler(d_data.magic);
2057 d_data.magic = NULL;
2059 if (menu==NULL
2060 || (menu->selected_entry>=0
2061 && menu->entries[menu->selected_entry]->cascade>=0)) {
2062 prevx = ev.xmotion.x_root;
2063 prevy = ev.xmotion.y_root;
2065 break;
2067 selectEntry(menu, -1);
2068 menu = smenu;
2069 prevx = ev.xmotion.x_root;
2070 prevy = ev.xmotion.y_root;
2071 break;
2072 } else if (menu && menu!=smenu
2073 && (menu->selected_entry<0
2074 || menu->entries[menu->selected_entry]->cascade<0)) {
2075 selectEntry(menu, -1);
2077 if (!delayed_select && d_data.magic) {
2078 WMDeleteTimerHandler(d_data.magic);
2079 d_data.magic = NULL;
2081 } else {
2083 /* hysteresis for item selection */
2085 /* check if the motion was to the side, indicating that
2086 * the user may want to cross to a submenu */
2087 if (!delayed_select && menu) {
2088 int dx;
2089 Bool moved_to_submenu;/* moved to direction of submenu */
2091 dx = abs(prevx - ev.xmotion.x_root);
2093 moved_to_submenu = False;
2094 if (dx > 0 /* if moved enough to the side */
2095 /* maybe a open submenu */
2096 && menu->selected_entry>=0
2097 /* moving to the right direction */
2098 && (wPreferences.align_menus
2099 || ev.xmotion.y_root >= prevy)) {
2100 int index;
2102 index = menu->entries[menu->selected_entry]->cascade;
2103 if (index>=0) {
2104 if (menu->cascades[index]->frame_x>menu->frame_x) {
2105 if (prevx < ev.xmotion.x_root)
2106 moved_to_submenu = True;
2107 } else {
2108 if (prevx > ev.xmotion.x_root)
2109 moved_to_submenu = True;
2115 if (menu != smenu) {
2116 if (d_data.magic) {
2117 WMDeleteTimerHandler(d_data.magic);
2119 d_data.magic = NULL;
2120 } else if (moved_to_submenu) {
2121 /* while we are moving, postpone the selection */
2122 if (d_data.magic) {
2123 WMDeleteTimerHandler(d_data.magic);
2125 d_data.delayed_select = NULL;
2126 d_data.menu = menu;
2127 d_data.magic = WMAddTimerHandler(MENU_SELECT_DELAY,
2128 delaySelection,
2129 &d_data);
2130 prevx = ev.xmotion.x_root;
2131 prevy = ev.xmotion.y_root;
2132 break;
2133 } else {
2134 if (d_data.magic)
2135 WMDeleteTimerHandler(d_data.magic);
2136 d_data.magic = NULL;
2140 prevx = ev.xmotion.x_root;
2141 prevy = ev.xmotion.y_root;
2142 if (menu!=smenu) {
2143 /* pointer crossed menus */
2144 if (menu && menu->timer) {
2145 WMDeleteTimerHandler(menu->timer);
2146 menu->timer = NULL;
2148 if (smenu)
2149 dragScrollMenuCallback(smenu);
2151 menu = smenu;
2152 if (!menu->timer)
2153 dragScrollMenuCallback(menu);
2155 if (!delayed_select) {
2156 entry_no = getEntryAt(menu, x, y);
2157 if (entry_no>=0) {
2158 entry = menu->entries[entry_no];
2159 if (entry->flags.enabled && entry->cascade>=0 &&
2160 menu->cascades) {
2161 WMenu *submenu = menu->cascades[entry->cascade];
2162 if (submenu->flags.mapped && !submenu->flags.buttoned
2163 && menu->selected_entry!=entry_no) {
2164 wMenuUnmap(submenu);
2168 selectEntry(menu, entry_no);
2170 break;
2172 case ButtonPress:
2173 break;
2175 case ButtonRelease:
2176 if (ev.xbutton.button == event->xbutton.button)
2177 done=1;
2178 break;
2180 case Expose:
2181 WMHandleEvent(&ev);
2182 #ifdef VIRTUAL_DESKTOP
2183 /* since expose will raise edge up.. I need another ugly hack here */
2184 if (wPreferences.vedge_thickness) {
2185 wWorkspaceLowerEdge(scr);
2187 #endif
2188 break;
2192 if (menu && menu->timer) {
2193 WMDeleteTimerHandler(menu->timer);
2194 menu->timer = NULL;
2196 if (d_data.magic!=NULL)
2197 WMDeleteTimerHandler(d_data.magic);
2199 if (menu && menu->selected_entry>=0) {
2200 entry = menu->entries[menu->selected_entry];
2201 if (entry->callback!=NULL && entry->flags.enabled
2202 && entry->cascade < 0) {
2203 /* blink and erase menu selection */
2204 #if (MENU_BLINK_DELAY > 0)
2205 int sel = menu->selected_entry;
2206 int i;
2208 for (i=0; i<MENU_BLINK_COUNT; i++) {
2209 paintEntry(menu, sel, False);
2210 XSync(dpy, 0);
2211 wusleep(MENU_BLINK_DELAY);
2212 paintEntry(menu, sel, True);
2213 XSync(dpy, 0);
2214 wusleep(MENU_BLINK_DELAY);
2216 #endif
2217 /* unmap the menu, it's parents and call the callback */
2218 if (!menu->flags.buttoned &&
2219 (!menu->flags.app_menu||menu->parent!=NULL)) {
2220 closeCascade(menu);
2221 } else {
2222 selectEntry(menu, -1);
2224 (*entry->callback)(menu, entry);
2226 /* If the user double clicks an entry, the entry will
2227 * be executed twice, which is not good for things like
2228 * the root menu. So, ignore any clicks that were generated
2229 * while the entry was being executed */
2230 while (XCheckTypedWindowEvent(dpy, menu->menu->window,
2231 ButtonPress, &ev));
2232 } else if (entry->callback!=NULL && entry->cascade<0) {
2233 selectEntry(menu, -1);
2234 } else {
2235 if (entry->cascade>=0 && menu->cascades
2236 && menu->cascades[entry->cascade]->flags.brother) {
2237 selectEntry(menu, -1);
2242 if (((WMenu*)desc->parent)->flags.brother || close_on_exit || !smenu)
2243 closeCascade(desc->parent);
2245 /* close the cascade windows that should not remain opened */
2246 closeBrotherCascadesOf(desc->parent);
2248 if (!wPreferences.wrap_menus)
2249 wMenuMove(parentMenu(desc->parent), old_frame_x, old_frame_y, True);
2251 byebye:
2252 ((WMenu*)desc->parent)->flags.inside_handler = 0;
2253 #ifdef VIRTUAL_DESKTOP
2254 if (wPreferences.vedge_thickness) {
2255 wWorkspaceRaiseEdge(scr);
2257 #endif
2261 void
2262 wMenuMove(WMenu *menu, int x, int y, int submenus)
2264 WMenu *submenu;
2265 int i;
2267 if (!menu) return;
2269 menu->frame_x = x;
2270 menu->frame_y = y;
2271 XMoveWindow(dpy, menu->frame->core->window, x, y);
2273 if (submenus>0 && menu->selected_entry>=0) {
2274 i = menu->entries[menu->selected_entry]->cascade;
2276 if (i>=0 && menu->cascades) {
2277 submenu = menu->cascades[i];
2278 if (submenu->flags.mapped && !submenu->flags.buttoned) {
2279 if (wPreferences.align_menus) {
2280 wMenuMove(submenu, x + MENUW(menu), y, submenus);
2281 } else {
2282 wMenuMove(submenu, x+ MENUW(menu),
2283 y + submenu->entry_height*menu->selected_entry,
2284 submenus);
2289 if (submenus<0 && menu->parent!=NULL && menu->parent->flags.mapped &&
2290 !menu->parent->flags.buttoned) {
2291 if (wPreferences.align_menus) {
2292 wMenuMove(menu->parent, x - MENUW(menu->parent), y, submenus);
2293 } else {
2294 wMenuMove(menu->parent, x - MENUW(menu->parent), menu->frame_y
2295 - menu->parent->entry_height*menu->parent->selected_entry,
2296 submenus);
2302 static void
2303 changeMenuLevels(WMenu *menu, int lower)
2305 int i;
2307 if (!lower) {
2308 ChangeStackingLevel(menu->frame->core, (!menu->parent ? WMMainMenuLevel
2309 : WMSubmenuLevel));
2310 wRaiseFrame(menu->frame->core);
2311 menu->flags.lowered = 0;
2312 } else {
2313 ChangeStackingLevel(menu->frame->core, WMNormalLevel);
2314 wLowerFrame(menu->frame->core);
2315 menu->flags.lowered = 1;
2317 for (i=0; i<menu->cascade_no; i++) {
2318 if (menu->cascades[i]
2319 && !menu->cascades[i]->flags.buttoned
2320 && menu->cascades[i]->flags.lowered!=lower) {
2321 changeMenuLevels(menu->cascades[i], lower);
2328 static void
2329 menuTitleDoubleClick(WCoreWindow *sender, void *data, XEvent *event)
2331 WMenu *menu = data;
2332 int lower;
2334 if (event->xbutton.state & MOD_MASK) {
2335 if (menu->flags.lowered) {
2336 lower = 0;
2337 } else {
2338 lower = 1;
2340 changeMenuLevels(menu, lower);
2345 static void
2346 menuTitleMouseDown(WCoreWindow *sender, void *data, XEvent *event)
2348 WMenu *menu = data;
2349 WMenu *tmp;
2350 XEvent ev;
2351 int x=menu->frame_x, y=menu->frame_y;
2352 int dx=event->xbutton.x_root, dy=event->xbutton.y_root;
2353 int i, lower;
2354 Bool started;
2356 /* can't touch the menu copy */
2357 if (menu->flags.brother)
2358 return;
2360 if (event->xbutton.button != Button1 && event->xbutton.button != Button2)
2361 return;
2363 if (event->xbutton.state & MOD_MASK) {
2364 wLowerFrame(menu->frame->core);
2365 lower = 1;
2366 } else {
2367 wRaiseFrame(menu->frame->core);
2368 lower = 0;
2370 tmp = menu;
2372 /* lower/raise all submenus */
2373 while (1) {
2374 if (tmp->selected_entry>=0 && tmp->cascades
2375 && tmp->entries[tmp->selected_entry]->cascade>=0) {
2376 tmp = tmp->cascades[tmp->entries[tmp->selected_entry]->cascade];
2377 if (!tmp || !tmp->flags.mapped)
2378 break;
2379 if (lower)
2380 wLowerFrame(tmp->frame->core);
2381 else
2382 wRaiseFrame(tmp->frame->core);
2383 } else {
2384 break;
2388 /* tear off the menu if it's a root menu or a cascade
2389 application menu */
2390 if (!menu->flags.buttoned && !menu->flags.brother
2391 && (!menu->flags.app_menu||menu->parent!=NULL)) {
2392 menu->flags.buttoned=1;
2393 wFrameWindowShowButton(menu->frame, WFF_RIGHT_BUTTON);
2394 if (menu->parent) {
2395 /* turn off selected menu entry in parent menu */
2396 selectEntry(menu->parent, -1);
2398 /* make parent map the copy in place of the original */
2399 for (i=0; i<menu->parent->cascade_no; i++) {
2400 if (menu->parent->cascades[i] == menu) {
2401 menu->parent->cascades[i] = menu->brother;
2402 break;
2408 started = False;
2409 while(1) {
2410 WMMaskEvent(dpy, ButtonMotionMask|ButtonReleaseMask|ButtonPressMask
2411 |ExposureMask, &ev);
2412 switch (ev.type) {
2413 case MotionNotify:
2414 if (started) {
2415 x += ev.xmotion.x_root - dx;
2416 y += ev.xmotion.y_root - dy;
2417 dx = ev.xmotion.x_root;
2418 dy = ev.xmotion.y_root;
2419 wMenuMove(menu, x, y, True);
2420 } else {
2421 if (abs(ev.xmotion.x_root - dx) > MOVE_THRESHOLD
2422 || abs(ev.xmotion.y_root - dy) > MOVE_THRESHOLD) {
2423 started = True;
2424 XGrabPointer(dpy, menu->frame->titlebar->window, False,
2425 ButtonMotionMask|ButtonReleaseMask
2426 |ButtonPressMask,
2427 GrabModeAsync, GrabModeAsync, None,
2428 wCursor[WCUR_MOVE], CurrentTime);
2431 break;
2433 case ButtonPress:
2434 break;
2436 case ButtonRelease:
2437 if (ev.xbutton.button != event->xbutton.button)
2438 break;
2439 XUngrabPointer(dpy, CurrentTime);
2440 return;
2442 default:
2443 WMHandleEvent(&ev);
2444 break;
2450 *----------------------------------------------------------------------
2451 * menuCloseClick--
2452 * Handles mouse click on the close button of menus. The menu is
2453 * closed when the button is clicked.
2455 * Side effects:
2456 * The closed menu is reinserted at it's parent menus
2457 * cascade list.
2458 *----------------------------------------------------------------------
2460 static void
2461 menuCloseClick(WCoreWindow *sender, void *data, XEvent *event)
2463 WMenu *menu = (WMenu*)data;
2464 WMenu *parent = menu->parent;
2465 int i;
2467 if (parent) {
2468 for (i=0; i<parent->cascade_no; i++) {
2469 /* find the entry that points to the copy */
2470 if (parent->cascades[i] == menu->brother) {
2471 /* make it point to the original */
2472 parent->cascades[i] = menu;
2473 menu->parent = parent;
2474 break;
2478 wMenuUnmap(menu);
2482 static void
2483 saveMenuInfo(WMPropList *dict, WMenu *menu, WMPropList *key)
2485 WMPropList *value, *list;
2486 char buffer[256];
2488 snprintf(buffer, sizeof(buffer), "%i,%i", menu->frame_x, menu->frame_y);
2489 value = WMCreatePLString(buffer);
2490 list = WMCreatePLArray(value, NULL);
2491 if (menu->flags.lowered)
2492 WMAddToPLArray(list, WMCreatePLString("lowered"));
2493 WMPutInPLDictionary(dict, key, list);
2494 WMReleasePropList(value);
2495 WMReleasePropList(list);
2499 void
2500 wMenuSaveState(WScreen *scr)
2502 WMPropList *menus, *key;
2503 int save_menus = 0;
2505 menus = WMCreatePLDictionary(NULL, NULL, NULL);
2507 #ifndef LITE
2508 if (scr->switch_menu && scr->switch_menu->flags.buttoned) {
2509 key = WMCreatePLString("SwitchMenu");
2510 saveMenuInfo(menus, scr->switch_menu, key);
2511 WMReleasePropList(key);
2512 save_menus = 1;
2515 if (saveMenuRecurs(menus, scr, scr->root_menu))
2516 save_menus = 1;
2518 #endif /* !LITE */
2519 if (scr->workspace_menu && scr->workspace_menu->flags.buttoned) {
2520 key = WMCreatePLString("WorkspaceMenu");
2521 saveMenuInfo(menus, scr->workspace_menu, key);
2522 WMReleasePropList(key);
2523 save_menus = 1;
2526 if (save_menus) {
2527 key = WMCreatePLString("Menus");
2528 WMPutInPLDictionary(scr->session_state, key, menus);
2529 WMReleasePropList(key);
2531 WMReleasePropList(menus);
2535 #ifndef LITE
2537 static Bool
2538 getMenuPath(WMenu *menu, char *buffer, int bufSize)
2540 Bool ok = True;
2541 int len = 0;
2543 if (!menu->flags.titled || !menu->frame->title[0])
2544 return False;
2546 len = strlen(menu->frame->title);
2547 if (len >= bufSize)
2548 return False;
2550 if (menu->parent) {
2551 ok = getMenuPath(menu->parent, buffer, bufSize - len - 1);
2552 if (!ok)
2553 return False;
2556 strcat(buffer, "\\");
2557 strcat(buffer, menu->frame->title);
2559 return True;
2563 static Bool
2564 saveMenuRecurs(WMPropList *menus, WScreen *scr, WMenu *menu)
2566 WMPropList *key;
2567 int save_menus = 0, i;
2568 char buffer[512];
2569 Bool ok = True;
2572 if (menu->flags.brother)
2573 menu = menu->brother;
2575 if (menu->flags.buttoned && menu != scr->switch_menu) {
2577 buffer[0] = '\0';
2578 ok = getMenuPath(menu, buffer, 510);
2580 if (ok) {
2581 key = WMCreatePLString(buffer);
2582 saveMenuInfo(menus, menu, key);
2583 WMReleasePropList(key);
2584 save_menus = 1;
2588 if (ok) {
2589 for (i = 0; i < menu->cascade_no; i++) {
2590 if (saveMenuRecurs(menus, scr, menu->cascades[i]))
2591 save_menus = 1;
2594 return save_menus;
2596 #endif /* !LITE */
2599 #define COMPLAIN(key) wwarning(_("bad value in menus state info:%s"), key)
2602 static Bool
2603 getMenuInfo(WMPropList *info, int *x, int *y, Bool *lowered)
2605 WMPropList *pos;
2607 *lowered = False;
2609 if (WMIsPLArray(info)) {
2610 WMPropList *flags;
2611 pos = WMGetFromPLArray(info, 0);
2612 flags = WMGetFromPLArray(info, 1);
2613 if (flags != NULL && WMIsPLString(flags) && WMGetFromPLString(flags) != NULL
2614 && strcmp(WMGetFromPLString(flags), "lowered") == 0) {
2615 *lowered = True;
2617 } else {
2618 pos = info;
2621 if (pos != NULL && WMIsPLString(pos)) {
2622 if (sscanf(WMGetFromPLString(pos), "%i,%i", x, y)!=2)
2623 COMPLAIN("Position");
2624 } else {
2625 COMPLAIN("(position, flags...)");
2626 return False;
2629 return True;
2633 static int
2634 restoreMenu(WScreen *scr, WMPropList *menu, int which)
2636 int x, y;
2637 Bool lowered = False;
2638 WMenu *pmenu = NULL;
2640 if (!menu)
2641 return False;
2643 if (!getMenuInfo(menu, &x, &y, &lowered))
2644 return False;
2647 #ifndef LITE
2648 if (which & WSS_SWITCHMENU) {
2649 OpenSwitchMenu(scr, x, y, False);
2650 pmenu = scr->switch_menu;
2652 #endif /* !LITE */
2654 if (pmenu) {
2655 int width = MENUW(pmenu);
2656 int height = MENUH(pmenu);
2658 if (lowered) {
2659 changeMenuLevels(pmenu, True);
2662 x = (x < -width) ? 0 : x;
2663 x = (x > scr->scr_width) ? scr->scr_width - width : x;
2664 y = (y < 0) ? 0 : y;
2665 y = (y > scr->scr_height) ? scr->scr_height - height : y;
2666 wMenuMove(pmenu, x, y, True);
2667 pmenu->flags.buttoned = 1;
2668 wFrameWindowShowButton(pmenu->frame, WFF_RIGHT_BUTTON);
2669 return True;
2671 return False;
2675 #ifndef LITE
2676 static int
2677 restoreMenuRecurs(WScreen *scr, WMPropList *menus, WMenu *menu, char *path)
2679 WMPropList *key, *entry;
2680 char buffer[512];
2681 int i, x, y, res;
2682 Bool lowered;
2684 if (strlen(path) + strlen(menu->frame->title) > 510)
2685 return False;
2687 snprintf(buffer, sizeof(buffer), "%s\\%s", path, menu->frame->title);
2688 key = WMCreatePLString(buffer);
2689 entry = WMGetFromPLDictionary(menus, key);
2690 res = False;
2692 if (entry && getMenuInfo(entry, &x, &y, &lowered)) {
2694 if (!menu->flags.mapped) {
2695 int width = MENUW(menu);
2696 int height = MENUH(menu);
2698 wMenuMapAt(menu, x, y, False);
2700 if (menu->parent) {
2701 /* make parent map the copy in place of the original */
2702 for (i=0; i<menu->parent->cascade_no; i++) {
2703 if (menu->parent->cascades[i] == menu) {
2704 menu->parent->cascades[i] = menu->brother;
2705 break;
2709 if (lowered) {
2710 changeMenuLevels(menu, True);
2712 x = (x < -width) ? 0 : x;
2713 x = (x > scr->scr_width) ? scr->scr_width - width : x;
2714 y = (y < 0) ? 0 : y;
2715 y = (y > scr->scr_height) ? scr->scr_height - height : y;
2716 wMenuMove(menu, x, y, True);
2717 menu->flags.buttoned = 1;
2718 wFrameWindowShowButton(menu->frame, WFF_RIGHT_BUTTON);
2719 res = True;
2723 WMReleasePropList(key);
2725 for (i=0; i<menu->cascade_no; i++) {
2726 if (restoreMenuRecurs(scr, menus, menu->cascades[i], buffer) != False)
2727 res = True;
2730 return res;
2732 #endif /* !LITE */
2735 void
2736 wMenuRestoreState(WScreen *scr)
2738 WMPropList *menus, *menu, *key, *skey;
2740 key = WMCreatePLString("Menus");
2741 menus = WMGetFromPLDictionary(scr->session_state, key);
2742 WMReleasePropList(key);
2744 if (!menus)
2745 return;
2747 /* restore menus */
2749 skey = WMCreatePLString("SwitchMenu");
2750 menu = WMGetFromPLDictionary(menus, skey);
2751 WMReleasePropList(skey);
2752 restoreMenu(scr, menu, WSS_SWITCHMENU);
2754 #ifndef LITE
2755 if (!scr->root_menu) {
2756 OpenRootMenu(scr, scr->scr_width*2, 0, False);
2757 wMenuUnmap(scr->root_menu);
2759 restoreMenuRecurs(scr, menus, scr->root_menu, "");
2760 #endif /* !LITE */
2764 void
2765 OpenWorkspaceMenu(WScreen *scr, int x, int y)
2767 WMenu *menu, *parent;
2768 WMenuEntry *entry;
2770 #ifndef LITE
2771 if (!scr->root_menu) {
2772 OpenRootMenu(scr, scr->scr_width*2, 0, False);
2773 wMenuUnmap(scr->root_menu);
2775 #endif
2776 menu = scr->workspace_menu;
2777 if (menu) {
2778 if (menu->flags.mapped) {
2779 if (!menu->flags.buttoned) {
2780 wMenuUnmap(menu);
2781 parent = menu->parent;
2782 if (parent && parent->selected_entry >= 0) {
2783 entry = parent->entries[parent->selected_entry];
2784 if (parent->cascades[entry->cascade] == menu) {
2785 selectEntry(parent, -1);
2786 wMenuMapAt(menu, x, y, False);
2789 } else {
2790 wRaiseFrame(menu->frame->core);
2791 wMenuMapCopyAt(menu, x, y);
2793 } else {
2794 wMenuMapAt(menu, x, y, False);