Fix "smart" placement bug when there are shaded windows in other workspaces
[wmaker-crm.git] / src / menu.c
bloba86568161ff91136b2f72825ed859af379c7cc72
1 /* menu.c- generic menu, used for root menu, application menus etc.
3 * Window Maker window manager
5 * Copyright (c) 1997-2003 Alfredo K. Kojima
6 * Copyright (c) 1998-2003 Dan Pascu
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 <stdint.h>
33 #include <unistd.h>
34 #include <ctype.h>
35 #if 0
36 #include <nana.h>
37 #endif
38 #include "WindowMaker.h"
39 #include "wcore.h"
40 #include "framewin.h"
41 #include "menu.h"
42 #include "actions.h"
43 #include "funcs.h"
44 #include "stacking.h"
45 #include "text.h"
46 #include "xinerama.h"
47 #include "workspace.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
62 #define MENUW(m) ((m)->frame->core->width+2*FRAME_BORDER_WIDTH)
63 #define MENUH(m) ((m)->frame->core->height+2*FRAME_BORDER_WIDTH)
65 /***** Local Stuff ******/
67 #define WSS_ROOTMENU (1<<0)
68 #define WSS_SWITCHMENU (1<<1)
69 #define WSS_WSMENU (1<<2)
71 static struct {
72 int steps;
73 int delay;
74 } menuScrollParameters[5] = {
76 MENU_SCROLL_STEPS_UF, MENU_SCROLL_DELAY_UF}, {
77 MENU_SCROLL_STEPS_F, MENU_SCROLL_DELAY_F}, {
78 MENU_SCROLL_STEPS_M, MENU_SCROLL_DELAY_M}, {
79 MENU_SCROLL_STEPS_S, MENU_SCROLL_DELAY_S}, {
80 MENU_SCROLL_STEPS_US, MENU_SCROLL_DELAY_US}};
82 static void menuMouseDown(WObjDescriptor * desc, XEvent * event);
83 static void menuExpose(WObjDescriptor * desc, XEvent * event);
85 static void menuTitleDoubleClick(WCoreWindow * sender, void *data, XEvent * event);
86 static void menuTitleMouseDown(WCoreWindow * sender, void *data, XEvent * event);
88 static void menuCloseClick(WCoreWindow * sender, void *data, XEvent * event);
90 static void updateTexture(WMenu * menu);
92 #ifndef LITE
93 static int saveMenuRecurs(WMPropList * menus, WScreen * scr, WMenu * menu);
94 static int restoreMenuRecurs(WScreen * scr, WMPropList * menus, WMenu * menu, char *path);
95 #endif /* !LITE */
97 static void selectEntry(WMenu * menu, int entry_no);
98 static void closeCascade(WMenu * menu);
100 /****** Notification Observers ******/
102 static void appearanceObserver(void *self, WMNotification * notif)
104 WMenu *menu = (WMenu *) self;
105 int flags = (int)(uintptr_t) WMGetNotificationClientData(notif);
107 if (!menu->flags.realized)
108 return;
110 if (WMGetNotificationName(notif) == WNMenuAppearanceSettingsChanged) {
111 if (flags & WFontSettings) {
112 menu->flags.realized = 0;
113 wMenuRealize(menu);
115 if (flags & WTextureSettings) {
116 if (!menu->flags.brother)
117 updateTexture(menu);
119 if (flags & (WTextureSettings | WColorSettings)) {
120 wMenuPaint(menu);
122 } else if (menu->flags.titled) {
124 if (flags & WFontSettings) {
125 menu->flags.realized = 0;
126 wMenuRealize(menu);
128 if (flags & WTextureSettings) {
129 menu->frame->flags.need_texture_remake = 1;
131 if (flags & (WColorSettings | WTextureSettings)) {
132 wFrameWindowPaint(menu->frame);
137 /************************************/
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 *wMenuCreate(WScreen * screen, char *title, int main_menu)
153 WMenu *menu;
154 static int brother = 0;
155 int tmp, flags;
157 menu = wmalloc(sizeof(WMenu));
159 memset(menu, 0, sizeof(WMenu));
161 #ifdef SINGLE_MENULEVEL
162 tmp = WMSubmenuLevel;
163 #else
164 tmp = (main_menu ? WMMainMenuLevel : WMSubmenuLevel);
165 #endif
167 flags = WFF_SINGLE_STATE | WFF_BORDER;
168 if (title) {
169 flags |= WFF_TITLEBAR | WFF_RIGHT_BUTTON;
170 menu->flags.titled = 1;
172 menu->frame =
173 wFrameWindowCreate(screen, tmp, 8, 2, 1, 1, &wPreferences.menu_title_clearance, flags,
174 screen->menu_title_texture, NULL,
175 screen->menu_title_color, &screen->menu_title_font);
177 menu->frame->core->descriptor.parent = menu;
178 menu->frame->core->descriptor.parent_type = WCLASS_MENU;
179 menu->frame->core->descriptor.handle_mousedown = menuMouseDown;
181 wFrameWindowHideButton(menu->frame, WFF_RIGHT_BUTTON);
183 if (title) {
184 menu->frame->title = wstrdup(title);
187 menu->frame->flags.justification = WTJ_LEFT;
189 menu->frame->rbutton_image = screen->b_pixmaps[WBUT_CLOSE];
191 menu->entry_no = 0;
192 menu->alloced_entries = 0;
193 menu->selected_entry = -1;
194 menu->entries = NULL;
196 menu->frame_x = screen->app_menu_x;
197 menu->frame_y = screen->app_menu_y;
199 menu->frame->child = menu;
201 menu->flags.lowered = 0;
203 /* create borders */
204 if (title) {
205 /* setup object descriptors */
206 menu->frame->on_mousedown_titlebar = menuTitleMouseDown;
207 menu->frame->on_dblclick_titlebar = menuTitleDoubleClick;
210 menu->frame->on_click_right = menuCloseClick;
212 menu->menu = wCoreCreate(menu->frame->core, 0, menu->frame->top_width, menu->frame->core->width, 10);
214 menu->menu->descriptor.parent = menu;
215 menu->menu->descriptor.parent_type = WCLASS_MENU;
216 menu->menu->descriptor.handle_expose = menuExpose;
217 menu->menu->descriptor.handle_mousedown = menuMouseDown;
219 menu->menu_texture_data = None;
221 XMapWindow(dpy, menu->menu->window);
223 XFlush(dpy);
225 if (!brother) {
226 brother = 1;
227 menu->brother = wMenuCreate(screen, title, main_menu);
228 brother = 0;
229 menu->brother->flags.brother = 1;
230 menu->brother->brother = menu;
232 WMAddNotificationObserver(appearanceObserver, menu, WNMenuAppearanceSettingsChanged, menu);
234 WMAddNotificationObserver(appearanceObserver, menu, WNMenuTitleAppearanceSettingsChanged, menu);
236 return menu;
239 WMenu *wMenuCreateForApp(WScreen * screen, char *title, int main_menu)
241 WMenu *menu;
243 menu = wMenuCreate(screen, title, main_menu);
244 if (!menu)
245 return NULL;
246 menu->flags.app_menu = 1;
247 menu->brother->flags.app_menu = 1;
249 return menu;
252 static void insertEntry(WMenu * menu, WMenuEntry * entry, int index)
254 int i;
256 for (i = menu->entry_no - 1; i >= index; i--) {
257 menu->entries[i]->order++;
258 menu->entries[i + 1] = menu->entries[i];
260 menu->entries[index] = entry;
263 WMenuEntry *wMenuInsertCallback(WMenu * menu, int index, char *text,
264 void (*callback) (WMenu * menu, WMenuEntry * entry), void *clientdata)
266 WMenuEntry *entry;
268 menu->flags.realized = 0;
269 menu->brother->flags.realized = 0;
271 /* reallocate array if it's too small */
272 if (menu->entry_no >= menu->alloced_entries) {
273 void *tmp;
275 tmp = wrealloc(menu->entries, sizeof(WMenuEntry) * (menu->alloced_entries + 5));
277 menu->entries = tmp;
278 menu->alloced_entries += 5;
280 menu->brother->entries = tmp;
281 menu->brother->alloced_entries = menu->alloced_entries;
283 entry = wmalloc(sizeof(WMenuEntry));
284 memset(entry, 0, sizeof(WMenuEntry));
285 entry->flags.enabled = 1;
286 entry->text = wstrdup(text);
287 entry->cascade = -1;
288 entry->clientdata = clientdata;
289 entry->callback = callback;
290 if (index < 0 || index >= menu->entry_no) {
291 entry->order = menu->entry_no;
292 menu->entries[menu->entry_no] = entry;
293 } else {
294 entry->order = index;
295 insertEntry(menu, entry, index);
298 menu->entry_no++;
299 menu->brother->entry_no = menu->entry_no;
301 return entry;
304 void wMenuEntrySetCascade(WMenu * menu, WMenuEntry * entry, WMenu * cascade)
306 WMenu *brother = menu->brother;
307 int i, done;
309 assert(menu->flags.brother == 0);
311 if (entry->cascade >= 0) {
312 menu->flags.realized = 0;
313 brother->flags.realized = 0;
316 cascade->parent = menu;
318 cascade->brother->parent = brother;
320 done = 0;
321 for (i = 0; i < menu->cascade_no; i++) {
322 if (menu->cascades[i] == NULL) {
323 menu->cascades[i] = cascade;
324 brother->cascades[i] = cascade->brother;
325 done = 1;
326 entry->cascade = i;
327 break;
330 if (!done) {
331 entry->cascade = menu->cascade_no;
333 menu->cascades = wrealloc(menu->cascades, sizeof(WMenu) * (menu->cascade_no + 1));
334 menu->cascades[menu->cascade_no++] = cascade;
336 brother->cascades = wrealloc(brother->cascades, sizeof(WMenu) * (brother->cascade_no + 1));
337 brother->cascades[brother->cascade_no++] = cascade->brother;
340 if (menu->flags.lowered) {
342 cascade->flags.lowered = 1;
343 ChangeStackingLevel(cascade->frame->core, WMNormalLevel);
345 cascade->brother->flags.lowered = 1;
346 ChangeStackingLevel(cascade->brother->frame->core, WMNormalLevel);
349 if (!menu->flags.realized)
350 wMenuRealize(menu);
353 void wMenuEntryRemoveCascade(WMenu * menu, WMenuEntry * entry)
355 assert(menu->flags.brother == 0);
357 /* destroy cascade menu */
358 if (entry->cascade >= 0 && menu->cascades && menu->cascades[entry->cascade] != NULL) {
360 wMenuDestroy(menu->cascades[entry->cascade], True);
362 menu->cascades[entry->cascade] = NULL;
363 menu->brother->cascades[entry->cascade] = NULL;
365 entry->cascade = -1;
369 void wMenuRemoveItem(WMenu * menu, int index)
371 int i;
373 if (menu->flags.brother) {
374 wMenuRemoveItem(menu->brother, index);
375 return;
378 if (index >= menu->entry_no)
379 return;
381 /* destroy cascade menu */
382 wMenuEntryRemoveCascade(menu, menu->entries[index]);
384 /* destroy unshared data */
386 if (menu->entries[index]->text)
387 wfree(menu->entries[index]->text);
389 if (menu->entries[index]->rtext)
390 wfree(menu->entries[index]->rtext);
392 if (menu->entries[index]->free_cdata && menu->entries[index]->clientdata)
393 (*menu->entries[index]->free_cdata) (menu->entries[index]->clientdata);
395 wfree(menu->entries[index]);
397 for (i = index; i < menu->entry_no - 1; i++) {
398 menu->entries[i + 1]->order--;
399 menu->entries[i] = menu->entries[i + 1];
401 menu->entry_no--;
402 menu->brother->entry_no--;
405 static Pixmap renderTexture(WMenu * menu)
407 RImage *img;
408 Pixmap pix;
409 int i;
410 RColor light;
411 RColor dark;
412 RColor mid;
413 WScreen *scr = menu->menu->screen_ptr;
414 WTexture *texture = scr->menu_item_texture;
416 if (wPreferences.menu_style == MS_NORMAL) {
417 img = wTextureRenderImage(texture, menu->menu->width, menu->entry_height, WREL_MENUENTRY);
418 } else {
419 img = wTextureRenderImage(texture, menu->menu->width, menu->menu->height + 1, WREL_MENUENTRY);
421 if (!img) {
422 wwarning(_("could not render texture: %s"), RMessageForError(RErrorCode));
424 return None;
427 if (wPreferences.menu_style == MS_SINGLE_TEXTURE) {
428 light.alpha = 0;
429 light.red = light.green = light.blue = 80;
431 dark.alpha = 255;
432 dark.red = dark.green = dark.blue = 0;
434 mid.alpha = 0;
435 mid.red = mid.green = mid.blue = 40;
437 for (i = 1; i < menu->entry_no; i++) {
438 ROperateLine(img, RSubtractOperation, 0, i * menu->entry_height - 2,
439 menu->menu->width - 1, i * menu->entry_height - 2, &mid);
441 RDrawLine(img, 0, i * menu->entry_height - 1,
442 menu->menu->width - 1, i * menu->entry_height - 1, &dark);
444 ROperateLine(img, RAddOperation, 0, i * menu->entry_height,
445 menu->menu->width - 1, i * menu->entry_height, &light);
448 if (!RConvertImage(scr->rcontext, img, &pix)) {
449 wwarning(_("error rendering image:%s"), RMessageForError(RErrorCode));
451 RReleaseImage(img);
453 return pix;
456 static void updateTexture(WMenu * menu)
458 WScreen *scr = menu->menu->screen_ptr;
460 /* setup background texture */
461 if (scr->menu_item_texture->any.type != WTEX_SOLID) {
462 if (!menu->flags.brother) {
463 FREE_PIXMAP(menu->menu_texture_data);
465 menu->menu_texture_data = renderTexture(menu);
467 XSetWindowBackgroundPixmap(dpy, menu->menu->window, menu->menu_texture_data);
468 XClearWindow(dpy, menu->menu->window);
470 XSetWindowBackgroundPixmap(dpy, menu->brother->menu->window, menu->menu_texture_data);
471 XClearWindow(dpy, menu->brother->menu->window);
473 } else {
474 XSetWindowBackground(dpy, menu->menu->window, scr->menu_item_texture->any.color.pixel);
475 XClearWindow(dpy, menu->menu->window);
479 void wMenuRealize(WMenu * menu)
481 int i;
482 int width, rwidth, mrwidth, mwidth;
483 int theight, twidth, eheight;
484 WScreen *scr = menu->frame->screen_ptr;
485 static int brother_done = 0;
486 int flags;
488 if (!brother_done) {
489 brother_done = 1;
490 wMenuRealize(menu->brother);
491 brother_done = 0;
494 flags = WFF_SINGLE_STATE | WFF_BORDER;
495 if (menu->flags.titled)
496 flags |= WFF_TITLEBAR | WFF_RIGHT_BUTTON;
498 wFrameWindowUpdateBorders(menu->frame, flags);
500 if (menu->flags.titled) {
501 twidth = WMWidthOfString(scr->menu_title_font, menu->frame->title, strlen(menu->frame->title));
502 theight = menu->frame->top_width;
503 twidth += theight + (wPreferences.new_style ? 16 : 8);
504 } else {
505 twidth = 0;
506 theight = 0;
508 eheight = WMFontHeight(scr->menu_entry_font) + 6 + wPreferences.menu_text_clearance * 2;
509 menu->entry_height = eheight;
510 mrwidth = 0;
511 mwidth = 0;
512 for (i = 0; i < menu->entry_no; i++) {
513 char *text;
515 /* search widest text */
516 text = menu->entries[i]->text;
517 width = WMWidthOfString(scr->menu_entry_font, text, strlen(text)) + 10;
519 if (menu->entries[i]->flags.indicator) {
520 width += MENU_INDICATOR_SPACE;
523 if (width > mwidth)
524 mwidth = width;
526 /* search widest text on right */
527 text = menu->entries[i]->rtext;
528 if (text)
529 rwidth = WMWidthOfString(scr->menu_entry_font, text, strlen(text))
530 + 10;
531 else if (menu->entries[i]->cascade >= 0)
532 rwidth = 16;
533 else
534 rwidth = 4;
536 if (rwidth > mrwidth)
537 mrwidth = rwidth;
539 mwidth += mrwidth;
541 if (mwidth < twidth)
542 mwidth = twidth;
544 wCoreConfigure(menu->menu, 0, theight, mwidth, menu->entry_no * eheight - 1);
546 wFrameWindowResize(menu->frame, mwidth, menu->entry_no * eheight - 1
547 + menu->frame->top_width + menu->frame->bottom_width);
549 updateTexture(menu);
551 menu->flags.realized = 1;
553 if (menu->flags.mapped)
554 wMenuPaint(menu);
555 if (menu->brother->flags.mapped)
556 wMenuPaint(menu->brother);
559 void wMenuDestroy(WMenu * menu, int recurse)
561 int i;
563 WMRemoveNotificationObserver(menu);
565 /* remove any pending timers */
566 if (menu->timer)
567 WMDeleteTimerHandler(menu->timer);
568 menu->timer = NULL;
570 /* call destroy handler */
571 if (menu->on_destroy)
572 (*menu->on_destroy) (menu);
574 /* Destroy items if this menu own them. If this is the "brother" menu,
575 * leave them alone as it is shared by them.
577 if (!menu->flags.brother) {
578 for (i = 0; i < menu->entry_no; i++) {
580 wfree(menu->entries[i]->text);
582 if (menu->entries[i]->rtext)
583 wfree(menu->entries[i]->rtext);
584 #ifdef USER_MENU
586 if (menu->entries[i]->instances) {
587 WMReleasePropList(menu->entries[i]->instances);
589 #endif /* USER_MENU */
591 if (menu->entries[i]->free_cdata && menu->entries[i]->clientdata) {
592 (*menu->entries[i]->free_cdata) (menu->entries[i]->clientdata);
594 wfree(menu->entries[i]);
597 if (recurse) {
598 for (i = 0; i < menu->cascade_no; i++) {
599 if (menu->cascades[i]) {
600 if (menu->cascades[i]->flags.brother)
601 wMenuDestroy(menu->cascades[i]->brother, recurse);
602 else
603 wMenuDestroy(menu->cascades[i], recurse);
608 if (menu->entries)
609 wfree(menu->entries);
613 FREE_PIXMAP(menu->menu_texture_data);
615 if (menu->cascades)
616 wfree(menu->cascades);
618 wCoreDestroy(menu->menu);
619 wFrameWindowDestroy(menu->frame);
621 /* destroy copy of this menu */
622 if (!menu->flags.brother && menu->brother)
623 wMenuDestroy(menu->brother, False);
625 wfree(menu);
628 #define F_NORMAL 0
629 #define F_TOP 1
630 #define F_BOTTOM 2
631 #define F_NONE 3
633 static void drawFrame(WScreen * scr, Drawable win, int y, int w, int h, int type)
635 XSegment segs[2];
636 int i;
638 i = 0;
639 segs[i].x1 = segs[i].x2 = w - 1;
640 segs[i].y1 = y;
641 segs[i].y2 = y + h - 1;
642 i++;
643 if (type != F_TOP && type != F_NONE) {
644 segs[i].x1 = 1;
645 segs[i].y1 = segs[i].y2 = y + h - 2;
646 segs[i].x2 = w - 1;
647 i++;
649 XDrawSegments(dpy, win, scr->menu_item_auxtexture->dim_gc, segs, i);
651 i = 0;
652 segs[i].x1 = 0;
653 segs[i].y1 = y;
654 segs[i].x2 = 0;
655 segs[i].y2 = y + h - 1;
656 i++;
657 if (type != F_BOTTOM && type != F_NONE) {
658 segs[i].x1 = 0;
659 segs[i].y1 = y;
660 segs[i].x2 = w - 1;
661 segs[i].y2 = y;
662 i++;
664 XDrawSegments(dpy, win, scr->menu_item_auxtexture->light_gc, segs, i);
666 if (type != F_TOP && type != F_NONE)
667 XDrawLine(dpy, win, scr->menu_item_auxtexture->dark_gc, 0, y + h - 1, w - 1, y + h - 1);
670 static void paintEntry(WMenu * menu, int index, int selected)
672 WScreen *scr = menu->frame->screen_ptr;
673 Window win = menu->menu->window;
674 WMenuEntry *entry = menu->entries[index];
675 GC light, dim, dark;
676 WMColor *color;
677 int x, y, w, h, tw;
678 int type;
680 if (!menu->flags.realized)
681 return;
682 h = menu->entry_height;
683 w = menu->menu->width;
684 y = index * h;
686 light = scr->menu_item_auxtexture->light_gc;
687 dim = scr->menu_item_auxtexture->dim_gc;
688 dark = scr->menu_item_auxtexture->dark_gc;
690 if (wPreferences.menu_style == MS_FLAT && menu->entry_no > 1) {
691 if (index == 0)
692 type = F_TOP;
693 else if (index == menu->entry_no - 1)
694 type = F_BOTTOM;
695 else
696 type = F_NONE;
697 } else {
698 type = F_NORMAL;
701 /* paint background */
702 if (selected) {
703 XFillRectangle(dpy, win, WMColorGC(scr->select_color), 1, y + 1, w - 2, h - 3);
704 if (scr->menu_item_texture->any.type == WTEX_SOLID)
705 drawFrame(scr, win, y, w, h, type);
706 } else {
707 if (scr->menu_item_texture->any.type == WTEX_SOLID) {
708 XClearArea(dpy, win, 0, y + 1, w - 1, h - 3, False);
709 /* draw the frame */
710 drawFrame(scr, win, y, w, h, type);
711 } else {
712 XClearArea(dpy, win, 0, y, w, h, False);
716 if (selected) {
717 if (entry->flags.enabled)
718 color = scr->select_text_color;
719 else
720 color = scr->dtext_color;
721 } else if (!entry->flags.enabled) {
722 color = scr->dtext_color;
723 } else {
724 color = scr->mtext_color;
726 /* draw text */
727 x = 5;
728 if (entry->flags.indicator)
729 x += MENU_INDICATOR_SPACE + 2;
731 WMDrawString(scr->wmscreen, win, color, scr->menu_entry_font,
732 x, 3 + y + wPreferences.menu_text_clearance, entry->text, strlen(entry->text));
734 if (entry->cascade >= 0) {
735 /* draw the cascade indicator */
736 XDrawLine(dpy, win, dim, w - 11, y + 6, w - 6, y + h / 2 - 1);
737 XDrawLine(dpy, win, light, w - 11, y + h - 8, w - 6, y + h / 2 - 1);
738 XDrawLine(dpy, win, dark, w - 12, y + 6, w - 12, y + h - 8);
741 /* draw indicator */
742 if (entry->flags.indicator && entry->flags.indicator_on) {
743 int iw, ih;
744 WPixmap *indicator;
746 switch (entry->flags.indicator_type) {
747 case MI_CHECK:
748 indicator = scr->menu_check_indicator;
749 break;
750 case MI_MINIWINDOW:
751 indicator = scr->menu_mini_indicator;
752 break;
753 case MI_HIDDEN:
754 indicator = scr->menu_hide_indicator;
755 break;
756 case MI_SHADED:
757 indicator = scr->menu_shade_indicator;
758 break;
759 case MI_DIAMOND:
760 default:
761 indicator = scr->menu_radio_indicator;
762 break;
765 iw = indicator->width;
766 ih = indicator->height;
767 XSetClipMask(dpy, scr->copy_gc, indicator->mask);
768 XSetClipOrigin(dpy, scr->copy_gc, 5, y + (h - ih) / 2);
769 if (selected) {
770 if (entry->flags.enabled) {
771 XSetForeground(dpy, scr->copy_gc, WMColorPixel(scr->select_text_color));
772 } else {
773 XSetForeground(dpy, scr->copy_gc, WMColorPixel(scr->dtext_color));
775 } else {
776 if (entry->flags.enabled) {
777 XSetForeground(dpy, scr->copy_gc, WMColorPixel(scr->mtext_color));
778 } else {
779 XSetForeground(dpy, scr->copy_gc, WMColorPixel(scr->dtext_color));
782 XFillRectangle(dpy, win, scr->copy_gc, 5, y + (h - ih) / 2, iw, ih);
784 XCopyArea(dpy, indicator->image, win, scr->copy_gc, 0, 0,
785 iw, ih, 5, y+(h-ih)/2);
787 XSetClipOrigin(dpy, scr->copy_gc, 0, 0);
790 /* draw right text */
792 if (entry->rtext && entry->cascade < 0) {
793 tw = WMWidthOfString(scr->menu_entry_font, entry->rtext, strlen(entry->rtext));
795 WMDrawString(scr->wmscreen, win, color, scr->menu_entry_font, w - 6 - tw,
796 y + 3 + wPreferences.menu_text_clearance, entry->rtext, strlen(entry->rtext));
800 static void move_menus(WMenu * menu, int x, int y)
802 while (menu->parent) {
803 menu = menu->parent;
804 x -= MENUW(menu);
805 if (!wPreferences.align_menus && menu->selected_entry >= 0) {
806 y -= menu->selected_entry * menu->entry_height;
809 wMenuMove(menu, x, y, True);
812 static void makeVisible(WMenu * menu)
814 WScreen *scr = menu->frame->screen_ptr;
815 int x1, y1, x2, y2, new_x, new_y, move;
816 WMRect rect = wGetRectForHead(scr, wGetHeadForPointerLocation(scr));
818 if (menu->entry_no < 0)
819 return;
821 x1 = menu->frame_x;
822 y1 = menu->frame_y + menu->frame->top_width + menu->selected_entry * menu->entry_height;
823 x2 = x1 + MENUW(menu);
824 y2 = y1 + menu->entry_height;
826 new_x = x1;
827 new_y = y1;
828 move = 0;
830 if (x1 < rect.pos.x) {
831 new_x = rect.pos.x;
832 move = 1;
833 } else if (x2 >= rect.pos.x + rect.size.width) {
834 new_x = rect.pos.x + rect.size.width - MENUW(menu) - 1;
835 move = 1;
838 if (y1 < rect.pos.y) {
839 new_y = rect.pos.y;
840 move = 1;
841 } else if (y2 >= rect.pos.y + rect.size.height) {
842 new_y = rect.pos.y + rect.size.height - menu->entry_height - 1;
843 move = 1;
846 new_y = new_y - menu->frame->top_width - menu->selected_entry * menu->entry_height;
847 move_menus(menu, new_x, new_y);
850 static int check_key(WMenu * menu, XKeyEvent * event)
852 int i, ch, s;
853 char buffer[32];
855 if (XLookupString(event, buffer, 32, NULL, NULL) < 1)
856 return -1;
858 ch = toupper(buffer[0]);
860 s = (menu->selected_entry >= 0 ? menu->selected_entry + 1 : 0);
862 again:
863 for (i = s; i < menu->entry_no; i++) {
864 if (ch == toupper(menu->entries[i]->text[0])) {
865 return i;
868 /* no match. Retry from start, if previous started from a selected entry */
869 if (s != 0) {
870 s = 0;
871 goto again;
873 return -1;
876 static int keyboardMenu(WMenu * menu)
878 XEvent event;
879 KeySym ksym = NoSymbol;
880 int done = 0;
881 int index;
882 WMenuEntry *entry;
883 int old_pos_x = menu->frame_x;
884 int old_pos_y = menu->frame_y;
885 int new_x = old_pos_x, new_y = old_pos_y;
886 WMRect rect = wGetRectForHead(menu->frame->screen_ptr,
887 wGetHeadForPointerLocation(menu->frame->screen_ptr));
889 if (menu->flags.editing)
890 return False;
892 XGrabKeyboard(dpy, menu->frame->core->window, True, GrabModeAsync, GrabModeAsync, CurrentTime);
894 if (menu->frame_y + menu->frame->top_width >= rect.pos.y + rect.size.height)
895 new_y = rect.pos.y + rect.size.height - menu->frame->top_width;
897 if (menu->frame_x + MENUW(menu) >= rect.pos.x + rect.size.width)
898 new_x = rect.pos.x + rect.size.width - MENUW(menu) - 1;
900 move_menus(menu, new_x, new_y);
902 while (!done && menu->flags.mapped) {
903 XAllowEvents(dpy, AsyncKeyboard, CurrentTime);
904 WMMaskEvent(dpy, ExposureMask | ButtonMotionMask | ButtonPressMask
905 | ButtonReleaseMask | KeyPressMask | KeyReleaseMask | SubstructureNotifyMask, &event);
907 switch (event.type) {
908 case KeyPress:
909 ksym = XLookupKeysym(&event.xkey, 0);
910 switch (ksym) {
911 case XK_Escape:
912 done = 1;
913 break;
915 case XK_Home:
916 #ifdef XK_KP_Home
917 case XK_KP_Home:
918 #endif
919 selectEntry(menu, 0);
920 makeVisible(menu);
921 break;
923 case XK_End:
924 #ifdef XK_KP_End
925 case XK_KP_End:
926 #endif
927 selectEntry(menu, menu->entry_no - 1);
928 makeVisible(menu);
929 break;
931 case XK_Up:
932 #ifdef ARROWLESS_KBD
933 case XK_k:
934 #endif
935 #ifdef XK_KP_Up
936 case XK_KP_Up:
937 #endif
938 if (menu->selected_entry <= 0)
939 selectEntry(menu, menu->entry_no - 1);
940 else
941 selectEntry(menu, menu->selected_entry - 1);
942 makeVisible(menu);
943 break;
945 case XK_Down:
946 #ifdef ARROWLESS_KBD
947 case XK_j:
948 #endif
949 #ifdef XK_KP_Down
950 case XK_KP_Down:
951 #endif
952 if (menu->selected_entry < 0)
953 selectEntry(menu, 0);
954 else if (menu->selected_entry == menu->entry_no - 1)
955 selectEntry(menu, 0);
956 else if (menu->selected_entry < menu->entry_no - 1)
957 selectEntry(menu, menu->selected_entry + 1);
958 makeVisible(menu);
959 break;
961 case XK_Right:
962 #ifdef ARROWLESS_KBD
963 case XK_l:
964 #endif
965 #ifdef XK_KP_Right
966 case XK_KP_Right:
967 #endif
968 if (menu->selected_entry >= 0) {
969 WMenuEntry *entry;
970 entry = menu->entries[menu->selected_entry];
972 if (entry->cascade >= 0 && menu->cascades
973 && menu->cascades[entry->cascade]->entry_no > 0) {
975 XUngrabKeyboard(dpy, CurrentTime);
977 selectEntry(menu->cascades[entry->cascade], 0);
978 if (!keyboardMenu(menu->cascades[entry->cascade]))
979 done = 1;
981 XGrabKeyboard(dpy, menu->frame->core->window, True,
982 GrabModeAsync, GrabModeAsync, CurrentTime);
985 break;
987 case XK_Left:
988 #ifdef ARROWLESS_KBD
989 case XK_h:
990 #endif
991 #ifdef XK_KP_Left
992 case XK_KP_Left:
993 #endif
994 if (menu->parent != NULL && menu->parent->selected_entry >= 0) {
995 selectEntry(menu, -1);
996 move_menus(menu, old_pos_x, old_pos_y);
997 return True;
999 break;
1001 case XK_Return:
1002 #ifdef XK_KP_Enter
1003 case XK_KP_Enter:
1004 #endif
1005 done = 2;
1006 break;
1008 default:
1009 index = check_key(menu, &event.xkey);
1010 if (index >= 0) {
1011 selectEntry(menu, index);
1014 break;
1016 default:
1017 if (event.type == ButtonPress)
1018 done = 1;
1020 WMHandleEvent(&event);
1024 XUngrabKeyboard(dpy, CurrentTime);
1026 if (done == 2 && menu->selected_entry >= 0) {
1027 entry = menu->entries[menu->selected_entry];
1028 } else {
1029 entry = NULL;
1032 if (entry && entry->callback != NULL && entry->flags.enabled && entry->cascade < 0) {
1033 #if (MENU_BLINK_COUNT > 0)
1034 int sel = menu->selected_entry;
1035 int i;
1037 for (i = 0; i < MENU_BLINK_COUNT; i++) {
1038 paintEntry(menu, sel, False);
1039 XSync(dpy, 0);
1040 wusleep(MENU_BLINK_DELAY);
1041 paintEntry(menu, sel, True);
1042 XSync(dpy, 0);
1043 wusleep(MENU_BLINK_DELAY);
1045 #endif
1046 selectEntry(menu, -1);
1048 if (!menu->flags.buttoned) {
1049 wMenuUnmap(menu);
1050 move_menus(menu, old_pos_x, old_pos_y);
1052 closeCascade(menu);
1054 (*entry->callback) (menu, entry);
1055 } else {
1056 if (!menu->flags.buttoned) {
1057 wMenuUnmap(menu);
1058 move_menus(menu, old_pos_x, old_pos_y);
1060 selectEntry(menu, -1);
1063 /* returns True if returning from a submenu to a parent menu,
1064 * False if exiting from menu */
1065 return False;
1068 void wMenuMapAt(WMenu * menu, int x, int y, int keyboard)
1070 WMRect rect = wGetRectForHead(menu->frame->screen_ptr,
1071 wGetHeadForPointerLocation(menu->frame->screen_ptr));
1073 if (!menu->flags.realized) {
1074 menu->flags.realized = 1;
1075 wMenuRealize(menu);
1077 if (!menu->flags.mapped) {
1078 if (wPreferences.wrap_menus) {
1079 if (x < rect.pos.x)
1080 x = rect.pos.x;
1081 if (y < rect.pos.y)
1082 y = rect.pos.y;
1083 if (x + MENUW(menu) > rect.pos.x + rect.size.width)
1084 x = rect.pos.x + rect.size.width - MENUW(menu);
1085 if (y + MENUH(menu) > rect.pos.y + rect.size.height)
1086 y = rect.pos.y + rect.size.height - MENUH(menu);
1089 XMoveWindow(dpy, menu->frame->core->window, x, y);
1090 menu->frame_x = (x < 0) ? 0 : x;
1091 menu->frame_y = (y < 0) ? 0 : y;
1092 XMapWindow(dpy, menu->frame->core->window);
1093 wRaiseFrame(menu->frame->core);
1094 menu->flags.mapped = 1;
1095 } else {
1096 selectEntry(menu, 0);
1099 if (keyboard)
1100 keyboardMenu(menu);
1103 void wMenuMap(WMenu * menu)
1105 if (!menu->flags.realized) {
1106 menu->flags.realized = 1;
1107 wMenuRealize(menu);
1109 if (menu->flags.app_menu && menu->parent == NULL) {
1110 menu->frame_x = menu->frame->screen_ptr->app_menu_x;
1111 menu->frame_y = menu->frame->screen_ptr->app_menu_y;
1112 XMoveWindow(dpy, menu->frame->core->window, menu->frame_x, menu->frame_y);
1114 XMapWindow(dpy, menu->frame->core->window);
1115 wRaiseFrame(menu->frame->core);
1116 menu->flags.mapped = 1;
1119 void wMenuUnmap(WMenu * menu)
1121 int i;
1123 XUnmapWindow(dpy, menu->frame->core->window);
1124 if (menu->flags.titled && menu->flags.buttoned) {
1125 wFrameWindowHideButton(menu->frame, WFF_RIGHT_BUTTON);
1127 menu->flags.buttoned = 0;
1128 menu->flags.mapped = 0;
1129 menu->flags.open_to_left = 0;
1131 for (i = 0; i < menu->cascade_no; i++) {
1132 if (menu->cascades[i] != NULL
1133 && menu->cascades[i]->flags.mapped && !menu->cascades[i]->flags.buttoned) {
1135 wMenuUnmap(menu->cascades[i]);
1138 menu->selected_entry = -1;
1141 void wMenuPaint(WMenu * menu)
1143 int i;
1145 if (!menu->flags.mapped) {
1146 return;
1149 /* paint entries */
1150 for (i = 0; i < menu->entry_no; i++) {
1151 paintEntry(menu, i, i == menu->selected_entry);
1155 void wMenuSetEnabled(WMenu * menu, int index, int enable)
1157 if (index >= menu->entry_no)
1158 return;
1159 menu->entries[index]->flags.enabled = enable;
1160 paintEntry(menu, index, index == menu->selected_entry);
1161 paintEntry(menu->brother, index, index == menu->selected_entry);
1164 /* ====================================================================== */
1166 static void editEntry(WMenu * menu, WMenuEntry * entry)
1168 WTextInput *text;
1169 XEvent event;
1170 WObjDescriptor *desc;
1171 char *t;
1172 int done = 0;
1173 Window old_focus;
1174 int old_revert;
1176 menu->flags.editing = 1;
1178 text = wTextCreate(menu->menu, 1, menu->entry_height * entry->order,
1179 menu->menu->width - 2, menu->entry_height - 1);
1181 wTextPutText(text, entry->text);
1182 XGetInputFocus(dpy, &old_focus, &old_revert);
1183 XSetInputFocus(dpy, text->core->window, RevertToNone, CurrentTime);
1185 if (XGrabKeyboard(dpy, text->core->window, True, GrabModeAsync, GrabModeAsync, CurrentTime) != GrabSuccess) {
1186 wwarning(_("could not grab keyboard"));
1187 wTextDestroy(text);
1189 wSetFocusTo(menu->frame->screen_ptr, menu->frame->screen_ptr->focused_window);
1190 return;
1193 while (!done && !text->done) {
1194 XSync(dpy, 0);
1195 XAllowEvents(dpy, AsyncKeyboard | AsyncPointer, CurrentTime);
1196 XSync(dpy, 0);
1197 WMNextEvent(dpy, &event);
1199 if (XFindContext(dpy, event.xany.window, wWinContext, (XPointer *) & desc) == XCNOENT)
1200 desc = NULL;
1202 if ((desc != NULL) && (desc->handle_anything != NULL)) {
1204 (*desc->handle_anything) (desc, &event);
1206 } else {
1207 switch (event.type) {
1208 case ButtonPress:
1209 XAllowEvents(dpy, ReplayPointer, CurrentTime);
1210 done = 1;
1212 default:
1213 WMHandleEvent(&event);
1214 break;
1219 XSetInputFocus(dpy, old_focus, old_revert, CurrentTime);
1221 wSetFocusTo(menu->frame->screen_ptr, menu->frame->screen_ptr->focused_window);
1223 t = wTextGetText(text);
1224 /* if !t, the user has canceled editing */
1225 if (t) {
1226 if (entry->text)
1227 wfree(entry->text);
1228 entry->text = wstrdup(t);
1230 menu->flags.realized = 0;
1232 wTextDestroy(text);
1234 XUngrabKeyboard(dpy, CurrentTime);
1236 if (t && menu->on_edit)
1237 (*menu->on_edit) (menu, entry);
1239 menu->flags.editing = 0;
1241 if (!menu->flags.realized)
1242 wMenuRealize(menu);
1245 static void selectEntry(WMenu * menu, int entry_no)
1247 WMenuEntry *entry;
1248 WMenu *submenu;
1249 int old_entry;
1251 if (menu->entries == NULL)
1252 return;
1254 if (entry_no >= menu->entry_no)
1255 return;
1257 old_entry = menu->selected_entry;
1258 menu->selected_entry = entry_no;
1260 if (old_entry != entry_no) {
1262 /* unselect previous entry */
1263 if (old_entry >= 0) {
1264 paintEntry(menu, old_entry, False);
1265 entry = menu->entries[old_entry];
1267 /* unmap cascade */
1268 if (entry->cascade >= 0 && menu->cascades) {
1269 if (!menu->cascades[entry->cascade]->flags.buttoned) {
1270 wMenuUnmap(menu->cascades[entry->cascade]);
1275 if (entry_no < 0) {
1276 menu->selected_entry = -1;
1277 return;
1279 entry = menu->entries[entry_no];
1281 if (entry->cascade >= 0 && menu->cascades && entry->flags.enabled) {
1282 /* Callback for when the submenu is opened.
1284 submenu = menu->cascades[entry->cascade];
1285 if (submenu && submenu->flags.brother)
1286 submenu = submenu->brother;
1288 if (entry->callback) {
1289 /* Only call the callback if the submenu is not yet mapped.
1291 if (menu->flags.brother) {
1292 if (!submenu || !submenu->flags.mapped)
1293 (*entry->callback) (menu->brother, entry);
1294 } else {
1295 if (!submenu || !submenu->flags.buttoned)
1296 (*entry->callback) (menu, entry);
1300 /* the submenu menu might have changed */
1301 submenu = menu->cascades[entry->cascade];
1303 /* map cascade */
1304 if (!submenu->flags.mapped) {
1305 int x, y;
1307 if (!submenu->flags.realized)
1308 wMenuRealize(submenu);
1309 if (wPreferences.wrap_menus) {
1310 if (menu->flags.open_to_left)
1311 submenu->flags.open_to_left = 1;
1313 if (submenu->flags.open_to_left) {
1314 x = menu->frame_x - MENUW(submenu);
1315 if (x < 0) {
1316 x = 0;
1317 submenu->flags.open_to_left = 0;
1319 } else {
1320 x = menu->frame_x + MENUW(menu);
1322 if (x + MENUW(submenu)
1323 >= menu->frame->screen_ptr->scr_width) {
1325 x = menu->frame_x - MENUW(submenu);
1326 submenu->flags.open_to_left = 1;
1329 } else {
1330 x = menu->frame_x + MENUW(menu);
1333 if (wPreferences.align_menus) {
1334 y = menu->frame_y;
1335 } else {
1336 y = menu->frame_y + menu->entry_height * entry_no;
1337 if (menu->flags.titled)
1338 y += menu->frame->top_width;
1339 if (menu->cascades[entry->cascade]->flags.titled)
1340 y -= menu->cascades[entry->cascade]->frame->top_width;
1343 wMenuMapAt(menu->cascades[entry->cascade], x, y, False);
1344 menu->cascades[entry->cascade]->parent = menu;
1345 } else {
1346 return;
1349 paintEntry(menu, entry_no, True);
1353 static WMenu *findMenu(WScreen * scr, int *x_ret, int *y_ret)
1355 WMenu *menu;
1356 WObjDescriptor *desc;
1357 Window root_ret, win, junk_win;
1358 int x, y, wx, wy;
1359 unsigned int mask;
1361 XQueryPointer(dpy, scr->root_win, &root_ret, &win, &x, &y, &wx, &wy, &mask);
1363 if (win == None)
1364 return NULL;
1366 if (XFindContext(dpy, win, wWinContext, (XPointer *) & desc) == XCNOENT)
1367 return NULL;
1369 if (desc->parent_type == WCLASS_MENU) {
1370 menu = (WMenu *) desc->parent;
1371 XTranslateCoordinates(dpy, root_ret, menu->menu->window, wx, wy, x_ret, y_ret, &junk_win);
1372 return menu;
1374 return NULL;
1377 static void closeCascade(WMenu * menu)
1379 WMenu *parent = menu->parent;
1381 if (menu->flags.brother || (!menu->flags.buttoned && (!menu->flags.app_menu || menu->parent != NULL))) {
1383 selectEntry(menu, -1);
1384 XSync(dpy, 0);
1385 #if (MENU_BLINK_DELAY > 2)
1386 wusleep(MENU_BLINK_DELAY / 2);
1387 #endif
1388 wMenuUnmap(menu);
1389 while (parent != NULL
1390 && (parent->parent != NULL || !parent->flags.app_menu || parent->flags.brother)
1391 && !parent->flags.buttoned) {
1392 selectEntry(parent, -1);
1393 wMenuUnmap(parent);
1394 parent = parent->parent;
1396 if (parent)
1397 selectEntry(parent, -1);
1401 static void closeBrotherCascadesOf(WMenu * menu)
1403 WMenu *tmp;
1404 int i;
1406 for (i = 0; i < menu->cascade_no; i++) {
1407 if (menu->cascades[i]->flags.brother) {
1408 tmp = menu->cascades[i];
1409 } else {
1410 tmp = menu->cascades[i]->brother;
1412 if (tmp->flags.mapped) {
1413 selectEntry(tmp->parent, -1);
1414 closeBrotherCascadesOf(tmp);
1415 break;
1420 #define getEntryAt(menu, x, y) ((y)<0 ? -1 : (y)/(menu->entry_height))
1422 static WMenu *parentMenu(WMenu * menu)
1424 WMenu *parent;
1425 WMenuEntry *entry;
1427 if (menu->flags.buttoned)
1428 return menu;
1430 while (menu->parent && menu->parent->flags.mapped) {
1431 parent = menu->parent;
1432 if (parent->selected_entry < 0)
1433 break;
1434 entry = parent->entries[parent->selected_entry];
1435 if (!entry->flags.enabled || entry->cascade < 0 || !parent->cascades ||
1436 parent->cascades[entry->cascade] != menu)
1437 break;
1438 menu = parent;
1439 if (menu->flags.buttoned)
1440 break;
1443 return menu;
1447 * Will raise the passed menu, if submenu = 0
1448 * If submenu > 0 will also raise all mapped submenus
1449 * until the first buttoned one
1450 * If submenu < 0 will also raise all mapped parent menus
1451 * until the first buttoned one
1454 static void raiseMenus(WMenu * menu, int submenus)
1456 WMenu *submenu;
1457 int i;
1459 if (!menu)
1460 return;
1462 wRaiseFrame(menu->frame->core);
1464 if (submenus > 0 && menu->selected_entry >= 0) {
1465 i = menu->entries[menu->selected_entry]->cascade;
1466 if (i >= 0 && menu->cascades) {
1467 submenu = menu->cascades[i];
1468 if (submenu->flags.mapped && !submenu->flags.buttoned)
1469 raiseMenus(submenu, submenus);
1472 if (submenus < 0 && !menu->flags.buttoned && menu->parent && menu->parent->flags.mapped)
1473 raiseMenus(menu->parent, submenus);
1476 WMenu *wMenuUnderPointer(WScreen * screen)
1478 WObjDescriptor *desc;
1479 Window root_ret, win;
1480 int dummy;
1481 unsigned int mask;
1483 XQueryPointer(dpy, screen->root_win, &root_ret, &win, &dummy, &dummy, &dummy, &dummy, &mask);
1485 if (win == None)
1486 return NULL;
1488 if (XFindContext(dpy, win, wWinContext, (XPointer *) & desc) == XCNOENT)
1489 return NULL;
1491 if (desc->parent_type == WCLASS_MENU)
1492 return (WMenu *) desc->parent;
1493 return NULL;
1496 static void getPointerPosition(WScreen * scr, int *x, int *y)
1498 Window root_ret, win;
1499 int wx, wy;
1500 unsigned int mask;
1502 XQueryPointer(dpy, scr->root_win, &root_ret, &win, x, y, &wx, &wy, &mask);
1505 static void getScrollAmount(WMenu * menu, int *hamount, int *vamount)
1507 WScreen *scr = menu->menu->screen_ptr;
1508 int menuX1 = menu->frame_x;
1509 int menuY1 = menu->frame_y;
1510 int menuX2 = menu->frame_x + MENUW(menu);
1511 int menuY2 = menu->frame_y + MENUH(menu);
1512 int xroot, yroot;
1513 WMRect rect = wGetRectForHead(scr, wGetHeadForPointerLocation(scr));
1515 *hamount = 0;
1516 *vamount = 0;
1518 getPointerPosition(scr, &xroot, &yroot);
1520 if (xroot <= (rect.pos.x + 1) && menuX1 < rect.pos.x) {
1521 /* scroll to the right */
1522 *hamount = WMIN(MENU_SCROLL_STEP, abs(menuX1));
1524 } else if (xroot >= (rect.pos.x + rect.size.width - 2) && menuX2 > (rect.pos.x + rect.size.width - 1)) {
1525 /* scroll to the left */
1526 *hamount = WMIN(MENU_SCROLL_STEP, abs(menuX2 - rect.pos.x - rect.size.width - 1));
1528 if (*hamount == 0)
1529 *hamount = 1;
1531 *hamount = -*hamount;
1534 if (yroot <= (rect.pos.y + 1) && menuY1 < rect.pos.y) {
1535 /* scroll down */
1536 *vamount = WMIN(MENU_SCROLL_STEP, abs(menuY1));
1538 } else if (yroot >= (rect.pos.y + rect.size.height - 2) && menuY2 > (rect.pos.y + rect.size.height - 1)) {
1539 /* scroll up */
1540 *vamount = WMIN(MENU_SCROLL_STEP, abs(menuY2 - rect.pos.y - rect.size.height - 2));
1542 *vamount = -*vamount;
1546 static void dragScrollMenuCallback(void *data)
1548 WMenu *menu = (WMenu *) data;
1549 WScreen *scr = menu->menu->screen_ptr;
1550 WMenu *parent = parentMenu(menu);
1551 int hamount, vamount;
1552 int x, y;
1553 int newSelectedEntry;
1555 getScrollAmount(menu, &hamount, &vamount);
1557 if (hamount != 0 || vamount != 0) {
1558 wMenuMove(parent, parent->frame_x + hamount, parent->frame_y + vamount, True);
1559 if (findMenu(scr, &x, &y)) {
1560 newSelectedEntry = getEntryAt(menu, x, y);
1561 selectEntry(menu, newSelectedEntry);
1562 } else {
1563 /* Pointer fell outside of menu. If the selected entry is
1564 * not a submenu, unselect it */
1565 if (menu->selected_entry >= 0 && menu->entries[menu->selected_entry]->cascade < 0)
1566 selectEntry(menu, -1);
1567 newSelectedEntry = 0;
1570 /* paranoid check */
1571 if (newSelectedEntry >= 0) {
1572 /* keep scrolling */
1573 menu->timer = WMAddTimerHandler(MENU_SCROLL_DELAY, dragScrollMenuCallback, menu);
1574 } else {
1575 menu->timer = NULL;
1577 } else {
1578 /* don't need to scroll anymore */
1579 menu->timer = NULL;
1580 if (findMenu(scr, &x, &y)) {
1581 newSelectedEntry = getEntryAt(menu, x, y);
1582 selectEntry(menu, newSelectedEntry);
1587 static void scrollMenuCallback(void *data)
1589 WMenu *menu = (WMenu *) data;
1590 WMenu *parent = parentMenu(menu);
1591 int hamount = 0; /* amount to scroll */
1592 int vamount = 0;
1594 #ifdef VIRTUAL_DESKTOP
1595 /* don't scroll if it is in vdesk mode */
1596 if (!wPreferences.vdesk_enable)
1597 #endif
1598 getScrollAmount(menu, &hamount, &vamount);
1600 if (hamount != 0 || vamount != 0) {
1601 wMenuMove(parent, parent->frame_x + hamount, parent->frame_y + vamount, True);
1603 /* keep scrolling */
1604 menu->timer = WMAddTimerHandler(MENU_SCROLL_DELAY, scrollMenuCallback, menu);
1605 } else {
1606 /* don't need to scroll anymore */
1607 menu->timer = NULL;
1611 #define MENU_SCROLL_BORDER 5
1613 static int isPointNearBoder(WMenu * menu, int x, int y)
1615 int menuX1 = menu->frame_x;
1616 int menuY1 = menu->frame_y;
1617 int menuX2 = menu->frame_x + MENUW(menu);
1618 int menuY2 = menu->frame_y + MENUH(menu);
1619 int flag = 0;
1620 int head = wGetHeadForPoint(menu->frame->screen_ptr, wmkpoint(x, y));
1621 WMRect rect = wGetRectForHead(menu->frame->screen_ptr, head);
1623 /* XXX: handle screen joins properly !! */
1625 if (x >= menuX1 && x <= menuX2 &&
1626 (y < rect.pos.y + MENU_SCROLL_BORDER || y >= rect.pos.y + rect.size.height - MENU_SCROLL_BORDER))
1627 flag = 1;
1628 else if (y >= menuY1 && y <= menuY2 &&
1629 (x < rect.pos.x + MENU_SCROLL_BORDER || x >= rect.pos.x + rect.size.width - MENU_SCROLL_BORDER))
1630 flag = 1;
1632 return flag;
1635 typedef struct _delay {
1636 WMenu *menu;
1637 int ox, oy;
1638 } _delay;
1640 static void leaving(_delay * dl)
1642 wMenuMove(dl->menu, dl->ox, dl->oy, True);
1643 dl->menu->jump_back = NULL;
1644 dl->menu->menu->screen_ptr->flags.jump_back_pending = 0;
1645 wfree(dl);
1648 void wMenuScroll(WMenu * menu, XEvent * event)
1650 WMenu *smenu;
1651 WMenu *omenu = parentMenu(menu);
1652 WScreen *scr = menu->frame->screen_ptr;
1653 int done = 0;
1654 int jump_back = 0;
1655 int old_frame_x = omenu->frame_x;
1656 int old_frame_y = omenu->frame_y;
1657 XEvent ev;
1659 if (omenu->jump_back)
1660 WMDeleteTimerWithClientData(omenu->jump_back);
1662 if (( /*omenu->flags.buttoned && */ !wPreferences.wrap_menus)
1663 || omenu->flags.app_menu) {
1664 jump_back = 1;
1667 if (!wPreferences.wrap_menus)
1668 raiseMenus(omenu, True);
1669 else
1670 raiseMenus(menu, False);
1672 if (!menu->timer)
1673 scrollMenuCallback(menu);
1675 while (!done) {
1676 int x, y, on_border, on_x_edge, on_y_edge, on_title;
1677 WMRect rect;
1679 WMNextEvent(dpy, &ev);
1680 switch (ev.type) {
1681 case EnterNotify:
1682 WMHandleEvent(&ev);
1683 case MotionNotify:
1684 x = (ev.type == MotionNotify) ? ev.xmotion.x_root : ev.xcrossing.x_root;
1685 y = (ev.type == MotionNotify) ? ev.xmotion.y_root : ev.xcrossing.y_root;
1687 /* on_border is != 0 if the pointer is between the menu
1688 * and the screen border and is close enough to the border */
1689 on_border = isPointNearBoder(menu, x, y);
1691 smenu = wMenuUnderPointer(scr);
1693 if ((smenu == NULL && !on_border) || (smenu && parentMenu(smenu) != omenu)) {
1694 done = 1;
1695 break;
1698 rect = wGetRectForHead(scr, wGetHeadForPoint(scr, wmkpoint(x, y)));
1699 on_x_edge = x <= rect.pos.x + 1 || x >= rect.pos.x + rect.size.width - 2;
1700 on_y_edge = y <= rect.pos.y + 1 || y >= rect.pos.y + rect.size.height - 2;
1701 on_border = on_x_edge || on_y_edge;
1703 if (!on_border && !jump_back) {
1704 done = 1;
1705 break;
1708 if (menu->timer && (smenu != menu || (!on_y_edge && !on_x_edge))) {
1709 WMDeleteTimerHandler(menu->timer);
1710 menu->timer = NULL;
1713 if (smenu != NULL)
1714 menu = smenu;
1716 if (!menu->timer)
1717 scrollMenuCallback(menu);
1718 break;
1719 case ButtonPress:
1720 /* True if we push on title, or drag the omenu to other position */
1721 on_title = ev.xbutton.x_root >= omenu->frame_x &&
1722 ev.xbutton.x_root <= omenu->frame_x + MENUW(omenu) &&
1723 ev.xbutton.y_root >= omenu->frame_y &&
1724 ev.xbutton.y_root <= omenu->frame_y + omenu->frame->top_width;
1725 WMHandleEvent(&ev);
1726 smenu = wMenuUnderPointer(scr);
1727 if (smenu == NULL || (smenu && smenu->flags.buttoned && smenu != omenu))
1728 done = 1;
1729 else if (smenu == omenu && on_title) {
1730 jump_back = 0;
1731 done = 1;
1733 break;
1734 case KeyPress:
1735 done = 1;
1736 default:
1737 WMHandleEvent(&ev);
1738 break;
1742 if (menu->timer) {
1743 WMDeleteTimerHandler(menu->timer);
1744 menu->timer = NULL;
1747 if (jump_back) {
1748 _delay *delayer;
1749 if (!omenu->jump_back) {
1750 delayer = wmalloc(sizeof(_delay));
1751 delayer->menu = omenu;
1752 delayer->ox = old_frame_x;
1753 delayer->oy = old_frame_y;
1754 omenu->jump_back = delayer;
1755 scr->flags.jump_back_pending = 1;
1756 } else
1757 delayer = omenu->jump_back;
1758 WMAddTimerHandler(MENU_JUMP_BACK_DELAY, (WMCallback *) leaving, delayer);
1762 static void menuExpose(WObjDescriptor * desc, XEvent * event)
1764 wMenuPaint(desc->parent);
1767 typedef struct {
1768 int *delayed_select;
1769 WMenu *menu;
1770 WMHandlerID magic;
1771 } delay_data;
1773 static void delaySelection(void *data)
1775 delay_data *d = (delay_data *) data;
1776 int x, y, entry_no;
1777 WMenu *menu;
1779 d->magic = NULL;
1781 menu = findMenu(d->menu->menu->screen_ptr, &x, &y);
1782 if (menu && (d->menu == menu || d->delayed_select)) {
1783 entry_no = getEntryAt(menu, x, y);
1784 selectEntry(menu, entry_no);
1786 if (d->delayed_select)
1787 *(d->delayed_select) = 0;
1790 static void menuMouseDown(WObjDescriptor * desc, XEvent * event)
1792 XButtonEvent *bev = &event->xbutton;
1793 WMenu *menu = desc->parent;
1794 WMenu *smenu;
1795 WScreen *scr = menu->frame->screen_ptr;
1796 WMenuEntry *entry = NULL;
1797 XEvent ev;
1798 int close_on_exit = 0;
1799 int done = 0;
1800 int delayed_select = 0;
1801 int entry_no;
1802 int x, y;
1803 int prevx, prevy;
1804 int old_frame_x = 0;
1805 int old_frame_y = 0;
1806 delay_data d_data = { NULL, NULL, NULL };
1808 /* Doesn't seem to be needed anymore (if delayed selection handler is
1809 * added only if not present). there seem to be no other side effects
1810 * from removing this and it is also possible that it was only added
1811 * to avoid problems with adding the delayed selection timer handler
1812 * multiple times
1814 /*if (menu->flags.inside_handler) {
1815 return;
1816 } */
1817 menu->flags.inside_handler = 1;
1819 if (!wPreferences.wrap_menus) {
1820 smenu = parentMenu(menu);
1821 old_frame_x = smenu->frame_x;
1822 old_frame_y = smenu->frame_y;
1823 } else if (event->xbutton.window == menu->frame->core->window) {
1824 /* This is true if the menu was launched with right click on root window */
1825 if (!d_data.magic) {
1826 delayed_select = 1;
1827 d_data.delayed_select = &delayed_select;
1828 d_data.menu = menu;
1829 d_data.magic = WMAddTimerHandler(wPreferences.dblclick_time, delaySelection, &d_data);
1833 wRaiseFrame(menu->frame->core);
1835 close_on_exit = (bev->send_event || menu->flags.brother);
1837 smenu = findMenu(scr, &x, &y);
1838 if (!smenu) {
1839 x = -1;
1840 y = -1;
1841 } else {
1842 menu = smenu;
1845 if (menu->flags.editing) {
1846 goto byebye;
1848 entry_no = getEntryAt(menu, x, y);
1849 if (entry_no >= 0) {
1850 entry = menu->entries[entry_no];
1852 if (!close_on_exit && (bev->state & ControlMask) && smenu && entry->flags.editable) {
1853 editEntry(smenu, entry);
1854 goto byebye;
1855 } else if (bev->state & ControlMask) {
1856 goto byebye;
1859 if (entry->flags.enabled && entry->cascade >= 0 && menu->cascades) {
1860 WMenu *submenu = menu->cascades[entry->cascade];
1861 /* map cascade */
1862 if (submenu->flags.mapped && !submenu->flags.buttoned && menu->selected_entry != entry_no) {
1863 wMenuUnmap(submenu);
1865 if (!submenu->flags.mapped && !delayed_select) {
1866 selectEntry(menu, entry_no);
1867 } else if (!submenu->flags.buttoned) {
1868 selectEntry(menu, -1);
1871 } else if (!delayed_select) {
1872 selectEntry(menu, entry_no);
1875 if (!wPreferences.wrap_menus && !wPreferences.scrollable_menus) {
1876 if (!menu->timer)
1877 dragScrollMenuCallback(menu);
1880 #ifdef VIRTUAL_DESKTOP
1881 if (wPreferences.vdesk_enable) {
1882 wWorkspaceLowerEdge(scr);
1884 #endif
1886 prevx = bev->x_root;
1887 prevy = bev->y_root;
1888 while (!done) {
1889 int x, y;
1891 XAllowEvents(dpy, AsyncPointer | SyncPointer, CurrentTime);
1893 WMMaskEvent(dpy, ExposureMask | ButtonMotionMask | ButtonReleaseMask | ButtonPressMask, &ev);
1894 switch (ev.type) {
1895 case MotionNotify:
1896 smenu = findMenu(scr, &x, &y);
1898 if (smenu == NULL) {
1899 /* moved mouse out of menu */
1901 if (!delayed_select && d_data.magic) {
1902 WMDeleteTimerHandler(d_data.magic);
1903 d_data.magic = NULL;
1905 if (menu == NULL
1906 || (menu->selected_entry >= 0
1907 && menu->entries[menu->selected_entry]->cascade >= 0)) {
1908 prevx = ev.xmotion.x_root;
1909 prevy = ev.xmotion.y_root;
1911 break;
1913 selectEntry(menu, -1);
1914 menu = smenu;
1915 prevx = ev.xmotion.x_root;
1916 prevy = ev.xmotion.y_root;
1917 break;
1918 } else if (menu && menu != smenu
1919 && (menu->selected_entry < 0
1920 || menu->entries[menu->selected_entry]->cascade < 0)) {
1921 selectEntry(menu, -1);
1923 if (!delayed_select && d_data.magic) {
1924 WMDeleteTimerHandler(d_data.magic);
1925 d_data.magic = NULL;
1927 } else {
1929 /* hysteresis for item selection */
1931 /* check if the motion was to the side, indicating that
1932 * the user may want to cross to a submenu */
1933 if (!delayed_select && menu) {
1934 int dx;
1935 Bool moved_to_submenu; /* moved to direction of submenu */
1937 dx = abs(prevx - ev.xmotion.x_root);
1939 moved_to_submenu = False;
1940 if (dx > 0 /* if moved enough to the side */
1941 /* maybe a open submenu */
1942 && menu->selected_entry >= 0
1943 /* moving to the right direction */
1944 && (wPreferences.align_menus || ev.xmotion.y_root >= prevy)) {
1945 int index;
1947 index = menu->entries[menu->selected_entry]->cascade;
1948 if (index >= 0) {
1949 if (menu->cascades[index]->frame_x > menu->frame_x) {
1950 if (prevx < ev.xmotion.x_root)
1951 moved_to_submenu = True;
1952 } else {
1953 if (prevx > ev.xmotion.x_root)
1954 moved_to_submenu = True;
1959 if (menu != smenu) {
1960 if (d_data.magic) {
1961 WMDeleteTimerHandler(d_data.magic);
1962 d_data.magic = NULL;
1964 } else if (moved_to_submenu) {
1965 /* while we are moving, postpone the selection */
1966 if (d_data.magic) {
1967 WMDeleteTimerHandler(d_data.magic);
1969 d_data.delayed_select = NULL;
1970 d_data.menu = menu;
1971 d_data.magic = WMAddTimerHandler(MENU_SELECT_DELAY,
1972 delaySelection, &d_data);
1973 prevx = ev.xmotion.x_root;
1974 prevy = ev.xmotion.y_root;
1975 break;
1976 } else {
1977 if (d_data.magic) {
1978 WMDeleteTimerHandler(d_data.magic);
1979 d_data.magic = NULL;
1984 prevx = ev.xmotion.x_root;
1985 prevy = ev.xmotion.y_root;
1986 if (menu != smenu) {
1987 /* pointer crossed menus */
1988 if (menu && menu->timer) {
1989 WMDeleteTimerHandler(menu->timer);
1990 menu->timer = NULL;
1992 if (smenu)
1993 dragScrollMenuCallback(smenu);
1995 menu = smenu;
1996 if (!menu->timer)
1997 dragScrollMenuCallback(menu);
1999 if (!delayed_select) {
2000 entry_no = getEntryAt(menu, x, y);
2001 if (entry_no >= 0) {
2002 entry = menu->entries[entry_no];
2003 if (entry->flags.enabled && entry->cascade >= 0 && menu->cascades) {
2004 WMenu *submenu = menu->cascades[entry->cascade];
2005 if (submenu->flags.mapped && !submenu->flags.buttoned
2006 && menu->selected_entry != entry_no) {
2007 wMenuUnmap(submenu);
2011 selectEntry(menu, entry_no);
2013 break;
2015 case ButtonPress:
2016 break;
2018 case ButtonRelease:
2019 if (ev.xbutton.button == event->xbutton.button)
2020 done = 1;
2021 break;
2023 case Expose:
2024 WMHandleEvent(&ev);
2025 #ifdef VIRTUAL_DESKTOP
2026 /* since expose will raise edge up.. I need another ugly hack here */
2027 if (wPreferences.vdesk_enable) {
2028 wWorkspaceLowerEdge(scr);
2030 #endif
2031 break;
2035 if (menu && menu->timer) {
2036 WMDeleteTimerHandler(menu->timer);
2037 menu->timer = NULL;
2039 if (d_data.magic != NULL) {
2040 WMDeleteTimerHandler(d_data.magic);
2041 d_data.magic = NULL;
2044 if (menu && menu->selected_entry >= 0) {
2045 entry = menu->entries[menu->selected_entry];
2046 if (entry->callback != NULL && entry->flags.enabled && entry->cascade < 0) {
2047 /* blink and erase menu selection */
2048 #if (MENU_BLINK_DELAY > 0)
2049 int sel = menu->selected_entry;
2050 int i;
2052 for (i = 0; i < MENU_BLINK_COUNT; i++) {
2053 paintEntry(menu, sel, False);
2054 XSync(dpy, 0);
2055 wusleep(MENU_BLINK_DELAY);
2056 paintEntry(menu, sel, True);
2057 XSync(dpy, 0);
2058 wusleep(MENU_BLINK_DELAY);
2060 #endif
2061 /* unmap the menu, it's parents and call the callback */
2062 if (!menu->flags.buttoned && (!menu->flags.app_menu || menu->parent != NULL)) {
2063 closeCascade(menu);
2064 } else {
2065 selectEntry(menu, -1);
2067 (*entry->callback) (menu, entry);
2069 /* If the user double clicks an entry, the entry will
2070 * be executed twice, which is not good for things like
2071 * the root menu. So, ignore any clicks that were generated
2072 * while the entry was being executed */
2073 while (XCheckTypedWindowEvent(dpy, menu->menu->window, ButtonPress, &ev)) ;
2074 } else if (entry->callback != NULL && entry->cascade < 0) {
2075 selectEntry(menu, -1);
2076 } else {
2077 if (entry->cascade >= 0 && menu->cascades && menu->cascades[entry->cascade]->flags.brother) {
2078 selectEntry(menu, -1);
2083 if (((WMenu *) desc->parent)->flags.brother || close_on_exit || !smenu)
2084 closeCascade(desc->parent);
2086 /* close the cascade windows that should not remain opened */
2087 closeBrotherCascadesOf(desc->parent);
2089 if (!wPreferences.wrap_menus)
2090 wMenuMove(parentMenu(desc->parent), old_frame_x, old_frame_y, True);
2092 byebye:
2093 /* Just to be sure in case we skip the 2 above because of a goto byebye */
2094 if (menu && menu->timer) {
2095 WMDeleteTimerHandler(menu->timer);
2096 menu->timer = NULL;
2098 if (d_data.magic != NULL) {
2099 WMDeleteTimerHandler(d_data.magic);
2100 d_data.magic = NULL;
2103 ((WMenu *) desc->parent)->flags.inside_handler = 0;
2104 #ifdef VIRTUAL_DESKTOP
2105 wWorkspaceRaiseEdge(scr);
2106 #endif
2109 void wMenuMove(WMenu * menu, int x, int y, int submenus)
2111 WMenu *submenu;
2112 int i;
2114 if (!menu)
2115 return;
2117 menu->frame_x = x;
2118 menu->frame_y = y;
2119 XMoveWindow(dpy, menu->frame->core->window, x, y);
2121 if (submenus > 0 && menu->selected_entry >= 0) {
2122 i = menu->entries[menu->selected_entry]->cascade;
2124 if (i >= 0 && menu->cascades) {
2125 submenu = menu->cascades[i];
2126 if (submenu->flags.mapped && !submenu->flags.buttoned) {
2127 if (wPreferences.align_menus) {
2128 wMenuMove(submenu, x + MENUW(menu), y, submenus);
2129 } else {
2130 wMenuMove(submenu, x + MENUW(menu),
2131 y + submenu->entry_height * menu->selected_entry, submenus);
2136 if (submenus < 0 && menu->parent != NULL && menu->parent->flags.mapped && !menu->parent->flags.buttoned) {
2137 if (wPreferences.align_menus) {
2138 wMenuMove(menu->parent, x - MENUW(menu->parent), y, submenus);
2139 } else {
2140 wMenuMove(menu->parent, x - MENUW(menu->parent), menu->frame_y
2141 - menu->parent->entry_height * menu->parent->selected_entry, submenus);
2146 static void changeMenuLevels(WMenu * menu, int lower)
2148 int i;
2150 if (!lower) {
2151 ChangeStackingLevel(menu->frame->core, (!menu->parent ? WMMainMenuLevel : WMSubmenuLevel));
2152 wRaiseFrame(menu->frame->core);
2153 menu->flags.lowered = 0;
2154 } else {
2155 ChangeStackingLevel(menu->frame->core, WMNormalLevel);
2156 wLowerFrame(menu->frame->core);
2157 menu->flags.lowered = 1;
2159 for (i = 0; i < menu->cascade_no; i++) {
2160 if (menu->cascades[i]
2161 && !menu->cascades[i]->flags.buttoned && menu->cascades[i]->flags.lowered != lower) {
2162 changeMenuLevels(menu->cascades[i], lower);
2167 static void menuTitleDoubleClick(WCoreWindow * sender, void *data, XEvent * event)
2169 WMenu *menu = data;
2170 int lower;
2172 if (event->xbutton.state & MOD_MASK) {
2173 if (menu->flags.lowered) {
2174 lower = 0;
2175 } else {
2176 lower = 1;
2178 changeMenuLevels(menu, lower);
2182 static void menuTitleMouseDown(WCoreWindow * sender, void *data, XEvent * event)
2184 WMenu *menu = data;
2185 WMenu *tmp;
2186 XEvent ev;
2187 int x = menu->frame_x, y = menu->frame_y;
2188 int dx = event->xbutton.x_root, dy = event->xbutton.y_root;
2189 int i, lower;
2190 Bool started;
2192 /* can't touch the menu copy */
2193 if (menu->flags.brother)
2194 return;
2196 if (event->xbutton.button != Button1 && event->xbutton.button != Button2)
2197 return;
2199 if (event->xbutton.state & MOD_MASK) {
2200 wLowerFrame(menu->frame->core);
2201 lower = 1;
2202 } else {
2203 wRaiseFrame(menu->frame->core);
2204 lower = 0;
2206 tmp = menu;
2208 /* lower/raise all submenus */
2209 while (1) {
2210 if (tmp->selected_entry >= 0 && tmp->cascades && tmp->entries[tmp->selected_entry]->cascade >= 0) {
2211 tmp = tmp->cascades[tmp->entries[tmp->selected_entry]->cascade];
2212 if (!tmp || !tmp->flags.mapped)
2213 break;
2214 if (lower)
2215 wLowerFrame(tmp->frame->core);
2216 else
2217 wRaiseFrame(tmp->frame->core);
2218 } else {
2219 break;
2223 /* tear off the menu if it's a root menu or a cascade
2224 application menu */
2225 if (!menu->flags.buttoned && !menu->flags.brother && (!menu->flags.app_menu || menu->parent != NULL)) {
2226 menu->flags.buttoned = 1;
2227 wFrameWindowShowButton(menu->frame, WFF_RIGHT_BUTTON);
2228 if (menu->parent) {
2229 /* turn off selected menu entry in parent menu */
2230 selectEntry(menu->parent, -1);
2232 /* make parent map the copy in place of the original */
2233 for (i = 0; i < menu->parent->cascade_no; i++) {
2234 if (menu->parent->cascades[i] == menu) {
2235 menu->parent->cascades[i] = menu->brother;
2236 break;
2242 started = False;
2243 while (1) {
2244 WMMaskEvent(dpy, ButtonMotionMask | ButtonReleaseMask | ButtonPressMask | ExposureMask, &ev);
2245 switch (ev.type) {
2246 case MotionNotify:
2247 if (started) {
2248 x += ev.xmotion.x_root - dx;
2249 y += ev.xmotion.y_root - dy;
2250 dx = ev.xmotion.x_root;
2251 dy = ev.xmotion.y_root;
2252 wMenuMove(menu, x, y, True);
2253 } else {
2254 if (abs(ev.xmotion.x_root - dx) > MOVE_THRESHOLD
2255 || abs(ev.xmotion.y_root - dy) > MOVE_THRESHOLD) {
2256 started = True;
2257 XGrabPointer(dpy, menu->frame->titlebar->window, False,
2258 ButtonMotionMask | ButtonReleaseMask
2259 | ButtonPressMask,
2260 GrabModeAsync, GrabModeAsync, None,
2261 wCursor[WCUR_MOVE], CurrentTime);
2264 break;
2266 case ButtonPress:
2267 break;
2269 case ButtonRelease:
2270 if (ev.xbutton.button != event->xbutton.button)
2271 break;
2272 XUngrabPointer(dpy, CurrentTime);
2273 return;
2275 default:
2276 WMHandleEvent(&ev);
2277 break;
2283 *----------------------------------------------------------------------
2284 * menuCloseClick--
2285 * Handles mouse click on the close button of menus. The menu is
2286 * closed when the button is clicked.
2288 * Side effects:
2289 * The closed menu is reinserted at it's parent menus
2290 * cascade list.
2291 *----------------------------------------------------------------------
2293 static void menuCloseClick(WCoreWindow * sender, void *data, XEvent * event)
2295 WMenu *menu = (WMenu *) data;
2296 WMenu *parent = menu->parent;
2297 int i;
2299 if (parent) {
2300 for (i = 0; i < parent->cascade_no; i++) {
2301 /* find the entry that points to the copy */
2302 if (parent->cascades[i] == menu->brother) {
2303 /* make it point to the original */
2304 parent->cascades[i] = menu;
2305 menu->parent = parent;
2306 break;
2310 wMenuUnmap(menu);
2313 static void saveMenuInfo(WMPropList * dict, WMenu * menu, WMPropList * key)
2315 WMPropList *value, *list;
2316 char buffer[256];
2318 snprintf(buffer, sizeof(buffer), "%i,%i", menu->frame_x, menu->frame_y);
2319 value = WMCreatePLString(buffer);
2320 list = WMCreatePLArray(value, NULL);
2321 if (menu->flags.lowered)
2322 WMAddToPLArray(list, WMCreatePLString("lowered"));
2323 WMPutInPLDictionary(dict, key, list);
2324 WMReleasePropList(value);
2325 WMReleasePropList(list);
2328 void wMenuSaveState(WScreen * scr)
2330 WMPropList *menus, *key;
2331 int save_menus = 0;
2333 menus = WMCreatePLDictionary(NULL, NULL);
2335 #ifndef LITE
2336 if (scr->switch_menu && scr->switch_menu->flags.buttoned) {
2337 key = WMCreatePLString("SwitchMenu");
2338 saveMenuInfo(menus, scr->switch_menu, key);
2339 WMReleasePropList(key);
2340 save_menus = 1;
2343 if (saveMenuRecurs(menus, scr, scr->root_menu))
2344 save_menus = 1;
2346 #endif /* !LITE */
2347 if (scr->workspace_menu && scr->workspace_menu->flags.buttoned) {
2348 key = WMCreatePLString("WorkspaceMenu");
2349 saveMenuInfo(menus, scr->workspace_menu, key);
2350 WMReleasePropList(key);
2351 save_menus = 1;
2354 if (save_menus) {
2355 key = WMCreatePLString("Menus");
2356 WMPutInPLDictionary(scr->session_state, key, menus);
2357 WMReleasePropList(key);
2359 WMReleasePropList(menus);
2362 #ifndef LITE
2364 static Bool getMenuPath(WMenu * menu, char *buffer, int bufSize)
2366 Bool ok = True;
2367 int len = 0;
2369 if (!menu->flags.titled || !menu->frame->title[0])
2370 return False;
2372 len = strlen(menu->frame->title);
2373 if (len >= bufSize)
2374 return False;
2376 if (menu->parent) {
2377 ok = getMenuPath(menu->parent, buffer, bufSize - len - 1);
2378 if (!ok)
2379 return False;
2382 strcat(buffer, "\\");
2383 strcat(buffer, menu->frame->title);
2385 return True;
2388 static Bool saveMenuRecurs(WMPropList * menus, WScreen * scr, WMenu * menu)
2390 WMPropList *key;
2391 int save_menus = 0, i;
2392 char buffer[512];
2393 Bool ok = True;
2395 if (menu->flags.brother)
2396 menu = menu->brother;
2398 if (menu->flags.buttoned && menu != scr->switch_menu) {
2400 buffer[0] = '\0';
2401 ok = getMenuPath(menu, buffer, 510);
2403 if (ok) {
2404 key = WMCreatePLString(buffer);
2405 saveMenuInfo(menus, menu, key);
2406 WMReleasePropList(key);
2407 save_menus = 1;
2411 if (ok) {
2412 for (i = 0; i < menu->cascade_no; i++) {
2413 if (saveMenuRecurs(menus, scr, menu->cascades[i]))
2414 save_menus = 1;
2417 return save_menus;
2419 #endif /* !LITE */
2421 #define COMPLAIN(key) wwarning(_("bad value in menus state info: %s"), key)
2423 static Bool getMenuInfo(WMPropList * info, int *x, int *y, Bool * lowered)
2425 WMPropList *pos;
2427 *lowered = False;
2429 if (WMIsPLArray(info)) {
2430 WMPropList *flags;
2431 pos = WMGetFromPLArray(info, 0);
2432 flags = WMGetFromPLArray(info, 1);
2433 if (flags != NULL && WMIsPLString(flags) && WMGetFromPLString(flags) != NULL
2434 && strcmp(WMGetFromPLString(flags), "lowered") == 0) {
2435 *lowered = True;
2437 } else {
2438 pos = info;
2441 if (pos != NULL && WMIsPLString(pos)) {
2442 if (sscanf(WMGetFromPLString(pos), "%i,%i", x, y) != 2)
2443 COMPLAIN("Position");
2444 } else {
2445 COMPLAIN("(position, flags...)");
2446 return False;
2449 return True;
2452 static int restoreMenu(WScreen * scr, WMPropList * menu, int which)
2454 int x, y;
2455 Bool lowered = False;
2456 WMenu *pmenu = NULL;
2458 if (!menu)
2459 return False;
2461 if (!getMenuInfo(menu, &x, &y, &lowered))
2462 return False;
2464 #ifndef LITE
2465 if (which & WSS_SWITCHMENU) {
2466 OpenSwitchMenu(scr, x, y, False);
2467 pmenu = scr->switch_menu;
2469 #endif /* !LITE */
2471 if (pmenu) {
2472 int width = MENUW(pmenu);
2473 int height = MENUH(pmenu);
2474 WMRect rect = wGetRectForHead(scr, wGetHeadForPointerLocation(scr));
2476 if (lowered) {
2477 changeMenuLevels(pmenu, True);
2480 if (x < rect.pos.x - width)
2481 x = rect.pos.x;
2482 if (x > rect.pos.x + rect.size.width)
2483 x = rect.pos.x + rect.size.width - width;
2484 if (y < rect.pos.y)
2485 y = rect.pos.y;
2486 if (y > rect.pos.y + rect.size.height)
2487 y = rect.pos.y + rect.size.height - height;
2489 wMenuMove(pmenu, x, y, True);
2490 pmenu->flags.buttoned = 1;
2491 wFrameWindowShowButton(pmenu->frame, WFF_RIGHT_BUTTON);
2492 return True;
2494 return False;
2497 #ifndef LITE
2498 static int restoreMenuRecurs(WScreen * scr, WMPropList * menus, WMenu * menu, char *path)
2500 WMPropList *key, *entry;
2501 char buffer[512];
2502 int i, x, y, res;
2503 Bool lowered;
2505 if (strlen(path) + strlen(menu->frame->title) > 510)
2506 return False;
2508 snprintf(buffer, sizeof(buffer), "%s\\%s", path, menu->frame->title);
2509 key = WMCreatePLString(buffer);
2510 entry = WMGetFromPLDictionary(menus, key);
2511 res = False;
2513 if (entry && getMenuInfo(entry, &x, &y, &lowered)) {
2515 if (!menu->flags.mapped) {
2516 int width = MENUW(menu);
2517 int height = MENUH(menu);
2518 WMRect rect = wGetRectForHead(scr, wGetHeadForPointerLocation(scr));
2520 wMenuMapAt(menu, x, y, False);
2522 if (menu->parent) {
2523 /* make parent map the copy in place of the original */
2524 for (i = 0; i < menu->parent->cascade_no; i++) {
2525 if (menu->parent->cascades[i] == menu) {
2526 menu->parent->cascades[i] = menu->brother;
2527 break;
2531 if (lowered) {
2532 changeMenuLevels(menu, True);
2535 if (x < rect.pos.x - width)
2536 x = rect.pos.x;
2537 if (x > rect.pos.x + rect.size.width)
2538 x = rect.pos.x + rect.size.width - width;
2539 if (y < rect.pos.y)
2540 y = rect.pos.y;
2541 if (y > rect.pos.y + rect.size.height)
2542 y = rect.pos.y + rect.size.height - height;
2544 wMenuMove(menu, x, y, True);
2545 menu->flags.buttoned = 1;
2546 wFrameWindowShowButton(menu->frame, WFF_RIGHT_BUTTON);
2547 res = True;
2551 WMReleasePropList(key);
2553 for (i = 0; i < menu->cascade_no; i++) {
2554 if (restoreMenuRecurs(scr, menus, menu->cascades[i], buffer) != False)
2555 res = True;
2558 return res;
2560 #endif /* !LITE */
2562 void wMenuRestoreState(WScreen * scr)
2564 WMPropList *menus, *menu, *key, *skey;
2566 if (!scr->session_state) {
2567 return;
2570 key = WMCreatePLString("Menus");
2571 menus = WMGetFromPLDictionary(scr->session_state, key);
2572 WMReleasePropList(key);
2574 if (!menus)
2575 return;
2577 /* restore menus */
2579 skey = WMCreatePLString("SwitchMenu");
2580 menu = WMGetFromPLDictionary(menus, skey);
2581 WMReleasePropList(skey);
2582 restoreMenu(scr, menu, WSS_SWITCHMENU);
2584 #ifndef LITE
2585 if (!scr->root_menu) {
2586 OpenRootMenu(scr, scr->scr_width * 2, 0, False);
2587 wMenuUnmap(scr->root_menu);
2589 restoreMenuRecurs(scr, menus, scr->root_menu, "");
2590 #endif /* !LITE */
2593 void OpenWorkspaceMenu(WScreen * scr, int x, int y)
2595 WMenu *menu, *parent;
2596 WMenuEntry *entry;
2598 #ifndef LITE
2599 if (!scr->root_menu) {
2600 OpenRootMenu(scr, scr->scr_width * 2, 0, False);
2601 wMenuUnmap(scr->root_menu);
2603 #endif
2604 menu = scr->workspace_menu;
2605 if (menu) {
2606 if (menu->flags.mapped) {
2607 if (!menu->flags.buttoned) {
2608 wMenuUnmap(menu);
2609 parent = menu->parent;
2610 if (parent && parent->selected_entry >= 0) {
2611 entry = parent->entries[parent->selected_entry];
2612 if (parent->cascades[entry->cascade] == menu) {
2613 selectEntry(parent, -1);
2614 wMenuMapAt(menu, x, y, False);
2617 } else {
2618 wRaiseFrame(menu->frame->core);
2619 wMenuMapCopyAt(menu, x, y);
2621 } else {
2622 wMenuMapAt(menu, x, y, False);