fix drawstring plugin support
[wmaker-crm.git] / src / menu.c
blob3c6d89e5b49c77f2a12485c0770cd3f020f8b672
1 /* menu.c- generic menu, used for root menu, application menus etc.
3 * Window Maker window manager
4 *
5 * Copyright (c) 1997, 1998 Alfredo K. Kojima
6 * Copyright (c) 1997, 1998 Dan Pascu
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
21 * USA.
24 #include "wconfig.h"
26 #include <X11/Xlib.h>
27 #include <X11/Xutil.h>
28 #include <X11/keysym.h>
29 #include <stdlib.h>
30 #include <string.h>
31 #include <stdio.h>
32 #include <unistd.h>
33 #include <ctype.h>
34 #if 0
35 #include <nana.h>
36 #endif
37 #include "WindowMaker.h"
38 #include "wcore.h"
39 #include "framewin.h"
40 #include "menu.h"
41 #include "actions.h"
42 #include "funcs.h"
43 #include "stacking.h"
44 #include "text.h"
47 /****** Global Variables ******/
49 extern Cursor wCursor[WCUR_LAST];
51 extern XContext wWinContext;
53 extern WPreferences wPreferences;
55 #define MOD_MASK wPreferences.modifier_mask
57 #define MENU_SCROLL_STEP menuScrollParameters[(int)wPreferences.menu_scroll_speed].steps
58 #define MENU_SCROLL_DELAY menuScrollParameters[(int)wPreferences.menu_scroll_speed].delay
62 #define MENUW(m) ((m)->frame->core->width+2*FRAME_BORDER_WIDTH)
63 #define MENUH(m) ((m)->frame->core->height+2*FRAME_BORDER_WIDTH)
66 /***** Local Stuff ******/
68 static struct {
69 int steps;
70 int delay;
71 } menuScrollParameters[5] = {
72 {MENU_SCROLL_STEPS_UF, MENU_SCROLL_DELAY_UF},
73 {MENU_SCROLL_STEPS_F, MENU_SCROLL_DELAY_F},
74 {MENU_SCROLL_STEPS_M, MENU_SCROLL_DELAY_M},
75 {MENU_SCROLL_STEPS_S, MENU_SCROLL_DELAY_S},
76 {MENU_SCROLL_STEPS_U, MENU_SCROLL_DELAY_U}};
79 static void menuMouseDown(WObjDescriptor *desc, XEvent *event);
80 static void menuExpose(WObjDescriptor *desc, XEvent *event);
82 static void menuTitleDoubleClick(WCoreWindow *sender, void *data, XEvent *event);
83 static void menuTitleMouseDown(WCoreWindow *sender, void *data, XEvent *event);
85 static void menuCloseClick(WCoreWindow *sender, void *data, XEvent *event);
87 static void updateTexture(WMenu *menu);
89 #ifndef LITE
90 static int saveMenuRecurs(proplist_t menus, WScreen *scr, WMenu *menu);
91 static int restoreMenuRecurs(WScreen *scr, proplist_t menus, WMenu *menu, char *path);
92 #endif /* !LITE */
94 static void selectEntry(WMenu *menu, int entry_no);
95 static void closeCascade(WMenu *menu);
98 /****** Notification Observers ******/
100 static void
101 appearanceObserver(void *self, WMNotification *notif)
103 WMenu *menu = (WMenu*)self;
104 int flags = (int)WMGetNotificationClientData(notif);
106 if (!menu->flags.realized)
107 return;
109 if (WMGetNotificationName(notif) == WNMenuAppearanceSettingsChanged) {
110 if (flags & WFontSettings) {
111 menu->flags.realized = 0;
112 wMenuRealize(menu);
114 if (flags & WTextureSettings) {
115 if (!menu->flags.brother)
116 updateTexture(menu);
118 if (flags & (WTextureSettings|WColorSettings)) {
119 wMenuPaint(menu);
121 } else if (menu->flags.titled) {
123 if (flags & WFontSettings) {
124 menu->flags.realized = 0;
125 wMenuRealize(menu);
127 if (flags & WTextureSettings) {
128 menu->frame->flags.need_texture_remake = 1;
130 if (flags & (WColorSettings|WTextureSettings)) {
131 wFrameWindowPaint(menu->frame);
136 /************************************/
140 *----------------------------------------------------------------------
141 * wMenuCreate--
142 * Creates a new empty menu with the specified title. If main_menu
143 * is True, the created menu will be a main menu, which has some special
144 * properties such as being placed over other normal menus.
145 * If title is NULL, the menu will have no titlebar.
147 * Returns:
148 * The created menu.
149 *----------------------------------------------------------------------
151 WMenu*
152 wMenuCreate(WScreen *screen, char *title, int main_menu)
154 WMenu *menu;
155 static int brother=0;
156 int tmp, flags;
158 menu = wmalloc(sizeof(WMenu));
160 memset(menu, 0, sizeof(WMenu));
162 #ifdef SINGLE_MENULEVEL
163 tmp = WMSubmenuLevel;
164 #else
165 tmp = (main_menu ? WMMainMenuLevel : WMSubmenuLevel);
166 #endif
168 flags = WFF_SINGLE_STATE|WFF_BORDER;
169 if (title) {
170 flags |= WFF_TITLEBAR|WFF_RIGHT_BUTTON;
171 menu->flags.titled = 1;
173 menu->frame =
174 wFrameWindowCreate(screen, tmp, 8, 2, 1, 1, &wPreferences.menu_title_clearance, flags,
175 screen->menu_title_texture, NULL,
176 screen->menu_title_pixel,
177 #ifdef DRAWSTRING_PLUGIN
178 W_STRING_MTITLE,
179 #endif
180 &screen->menu_title_gc,
181 &screen->menu_title_font);
183 menu->frame->core->descriptor.parent = menu;
184 menu->frame->core->descriptor.parent_type = WCLASS_MENU;
185 menu->frame->core->descriptor.handle_mousedown = menuMouseDown;
187 wFrameWindowHideButton(menu->frame, WFF_RIGHT_BUTTON);
189 if (title) {
190 menu->frame->title = wstrdup(title);
193 menu->frame->flags.justification = WTJ_LEFT;
195 menu->frame->rbutton_image = screen->b_pixmaps[WBUT_CLOSE];
197 menu->entry_no = 0;
198 menu->alloced_entries = 0;
199 menu->selected_entry = -1;
200 menu->entries = NULL;
202 menu->frame_x = screen->app_menu_x;
203 menu->frame_y = screen->app_menu_y;
205 menu->frame->child = menu;
207 menu->flags.lowered = 0;
209 /* create borders */
210 if (title) {
211 /* setup object descriptors */
212 menu->frame->on_mousedown_titlebar = menuTitleMouseDown;
213 menu->frame->on_dblclick_titlebar = menuTitleDoubleClick;
216 menu->frame->on_click_right = menuCloseClick;
219 menu->menu = wCoreCreate(menu->frame->core, 0, menu->frame->top_width,
220 menu->frame->core->width, 10);
222 menu->menu->descriptor.parent = menu;
223 menu->menu->descriptor.parent_type = WCLASS_MENU;
224 menu->menu->descriptor.handle_expose = menuExpose;
225 menu->menu->descriptor.handle_mousedown = menuMouseDown;
227 menu->menu_texture_data = None;
229 XMapWindow(dpy, menu->menu->window);
231 XFlush(dpy);
233 if (!brother) {
234 brother = 1;
235 menu->brother = wMenuCreate(screen, title, main_menu);
236 brother = 0;
237 menu->brother->flags.brother = 1;
238 menu->brother->brother = menu;
240 WMAddNotificationObserver(appearanceObserver, menu,
241 WNMenuAppearanceSettingsChanged, menu);
243 WMAddNotificationObserver(appearanceObserver, menu,
244 WNMenuTitleAppearanceSettingsChanged, menu);
247 return menu;
253 WMenu*
254 wMenuCreateForApp(WScreen *screen, char *title, int main_menu)
256 WMenu *menu;
258 menu = wMenuCreate(screen, title, main_menu);
259 if (!menu)
260 return NULL;
261 menu->flags.app_menu = 1;
262 menu->brother->flags.app_menu = 1;
264 return menu;
269 static void
270 insertEntry(WMenu *menu, WMenuEntry *entry, int index)
272 int i;
274 for (i = menu->entry_no-1; i >= index; i--) {
275 menu->entries[i]->order++;
276 menu->entries[i+1] = menu->entries[i];
278 menu->entries[index] = entry;
282 WMenuEntry*
283 wMenuInsertCallback(WMenu *menu, int index, char *text,
284 void (*callback)(WMenu *menu, WMenuEntry *entry),
285 void *clientdata)
287 WMenuEntry *entry;
289 menu->flags.realized = 0;
290 menu->brother->flags.realized = 0;
292 /* reallocate array if it's too small */
293 if (menu->entry_no >= menu->alloced_entries) {
294 void *tmp;
296 tmp = wrealloc(menu->entries,
297 sizeof(WMenuEntry)*(menu->alloced_entries+5));
299 menu->entries = tmp;
300 menu->alloced_entries += 5;
302 menu->brother->entries = tmp;
303 menu->brother->alloced_entries = menu->alloced_entries;
305 entry = wmalloc(sizeof(WMenuEntry));
306 memset(entry, 0, sizeof(WMenuEntry));
307 entry->flags.enabled = 1;
308 entry->text = wstrdup(text);
309 entry->cascade = -1;
310 entry->clientdata = clientdata;
311 entry->callback = callback;
312 if (index<0 || index>=menu->entry_no) {
313 entry->order = menu->entry_no;
314 menu->entries[menu->entry_no] = entry;
315 } else {
316 entry->order = index;
317 insertEntry(menu, entry, index);
320 menu->entry_no++;
321 menu->brother->entry_no = menu->entry_no;
323 return entry;
328 void
329 wMenuEntrySetCascade(WMenu *menu, WMenuEntry *entry, WMenu *cascade)
331 WMenu *brother = menu->brother;
332 int i, done;
334 assert(menu->flags.brother==0);
336 if (entry->cascade>=0) {
337 menu->flags.realized = 0;
338 brother->flags.realized = 0;
341 cascade->parent = menu;
343 cascade->brother->parent = brother;
345 done = 0;
346 for (i=0; i<menu->cascade_no; i++) {
347 if (menu->cascades[i]==NULL) {
348 menu->cascades[i] = cascade;
349 brother->cascades[i] = cascade->brother;
350 done = 1;
351 entry->cascade = i;
352 break;
355 if (!done) {
356 entry->cascade = menu->cascade_no;
358 menu->cascades = wrealloc(menu->cascades,
359 sizeof(WMenu)*(menu->cascade_no+1));
360 menu->cascades[menu->cascade_no++] = cascade;
363 brother->cascades = wrealloc(brother->cascades,
364 sizeof(WMenu)*(brother->cascade_no+1));
365 brother->cascades[brother->cascade_no++] = cascade->brother;
369 if (menu->flags.lowered) {
371 cascade->flags.lowered = 1;
372 ChangeStackingLevel(cascade->frame->core, WMNormalLevel);
374 cascade->brother->flags.lowered = 1;
375 ChangeStackingLevel(cascade->brother->frame->core, WMNormalLevel);
378 if (!menu->flags.realized)
379 wMenuRealize(menu);
383 void
384 wMenuEntryRemoveCascade(WMenu *menu, WMenuEntry *entry)
386 assert(menu->flags.brother==0);
388 /* destroy cascade menu */
389 if (entry->cascade>=0 && menu->cascades
390 && menu->cascades[entry->cascade]!=NULL) {
392 wMenuDestroy(menu->cascades[entry->cascade], True);
394 menu->cascades[entry->cascade] = NULL;
395 menu->brother->cascades[entry->cascade] = NULL;
397 entry->cascade = -1;
402 void
403 wMenuRemoveItem(WMenu *menu, int index)
405 int i;
407 if (menu->flags.brother) {
408 wMenuRemoveItem(menu->brother, index);
409 return;
412 if (index>=menu->entry_no) return;
414 /* destroy cascade menu */
415 wMenuEntryRemoveCascade(menu, menu->entries[index]);
417 /* destroy unshared data */
419 if (menu->entries[index]->text)
420 wfree(menu->entries[index]->text);
422 if (menu->entries[index]->rtext)
423 wfree(menu->entries[index]->rtext);
425 if (menu->entries[index]->free_cdata && menu->entries[index]->clientdata)
426 (*menu->entries[index]->free_cdata)(menu->entries[index]->clientdata);
428 wfree(menu->entries[index]);
430 for (i=index; i<menu->entry_no-1; i++) {
431 menu->entries[i+1]->order--;
432 menu->entries[i]=menu->entries[i+1];
434 menu->entry_no--;
435 menu->brother->entry_no--;
439 static Pixmap
440 renderTexture(WMenu *menu)
442 RImage *img;
443 Pixmap pix;
444 int i;
445 RColor light;
446 RColor dark;
447 RColor mid;
448 WScreen *scr = menu->menu->screen_ptr;
449 WTexture *texture = scr->menu_item_texture;
451 if (wPreferences.menu_style == MS_NORMAL) {
452 img = wTextureRenderImage(texture, menu->menu->width,
453 menu->entry_height, WREL_MENUENTRY);
454 } else {
455 img = wTextureRenderImage(texture, menu->menu->width,
456 menu->menu->height+1, WREL_MENUENTRY);
458 if (!img) {
459 wwarning(_("could not render texture: %s"),
460 RMessageForError(RErrorCode));
462 return None;
465 if (wPreferences.menu_style == MS_SINGLE_TEXTURE) {
466 light.alpha = 0;
467 light.red = light.green = light.blue = 80;
469 dark.alpha = 255;
470 dark.red = dark.green = dark.blue = 0;
472 mid.alpha = 0;
473 mid.red = mid.green = mid.blue = 40;
475 for (i = 1; i < menu->entry_no; i++) {
476 ROperateLine(img, RSubtractOperation, 0, i*menu->entry_height-2,
477 menu->menu->width-1, i*menu->entry_height-2, &mid);
479 RDrawLine(img, 0, i*menu->entry_height-1,
480 menu->menu->width-1, i*menu->entry_height-1, &dark);
482 ROperateLine(img, RAddOperation, 0, i*menu->entry_height,
483 menu->menu->width-1, i*menu->entry_height,
484 &light);
487 if (!RConvertImage(scr->rcontext, img, &pix)) {
488 wwarning(_("error rendering image:%s"), RMessageForError(RErrorCode));
490 RDestroyImage(img);
492 return pix;
496 static void
497 updateTexture(WMenu *menu)
499 WScreen *scr = menu->menu->screen_ptr;
501 /* setup background texture */
502 if (scr->menu_item_texture->any.type != WTEX_SOLID) {
503 if (!menu->flags.brother) {
504 FREE_PIXMAP(menu->menu_texture_data);
506 menu->menu_texture_data = renderTexture(menu);
508 XSetWindowBackgroundPixmap(dpy, menu->menu->window,
509 menu->menu_texture_data);
510 XClearWindow(dpy, menu->menu->window);
512 XSetWindowBackgroundPixmap(dpy, menu->brother->menu->window,
513 menu->menu_texture_data);
514 XClearWindow(dpy, menu->brother->menu->window);
516 } else {
517 XSetWindowBackground(dpy, menu->menu->window,
518 scr->menu_item_texture->any.color.pixel);
519 XClearWindow(dpy, menu->menu->window);
524 void
525 wMenuRealize(WMenu *menu)
527 int i;
528 int width, rwidth, mrwidth, mwidth;
529 int theight, twidth, eheight;
530 WScreen *scr = menu->frame->screen_ptr;
531 static int brother_done=0;
532 int flags;
534 if (!brother_done) {
535 brother_done = 1;
536 wMenuRealize(menu->brother);
537 brother_done = 0;
540 flags = WFF_SINGLE_STATE|WFF_BORDER;
541 if (menu->flags.titled)
542 flags |= WFF_TITLEBAR|WFF_RIGHT_BUTTON;
544 wFrameWindowUpdateBorders(menu->frame, flags);
546 if (menu->flags.titled) {
547 twidth = WMWidthOfString(scr->menu_title_font, menu->frame->title,
548 strlen(menu->frame->title));
549 theight = menu->frame->top_width;
550 twidth += theight + (wPreferences.new_style ? 16 : 8);
551 } else {
552 twidth = 0;
553 theight = 0;
555 eheight = WMFontHeight(scr->menu_entry_font) + 6 + wPreferences.menu_text_clearance * 2;
556 menu->entry_height = eheight;
557 mrwidth = 0;
558 mwidth = 0;
559 for (i=0; i<menu->entry_no; i++) {
560 char *text;
562 /* search widest text */
563 text = menu->entries[i]->text;
564 width = WMWidthOfString(scr->menu_entry_font, text, strlen(text))+10;
566 if (menu->entries[i]->flags.indicator) {
567 width += MENU_INDICATOR_SPACE;
570 if (width > mwidth)
571 mwidth = width;
573 /* search widest text on right */
574 text = menu->entries[i]->rtext;
575 if (text)
576 rwidth = WMWidthOfString(scr->menu_entry_font, text, strlen(text))
577 + 5;
578 else if (menu->entries[i]->cascade>=0)
579 rwidth = 16;
580 else
581 rwidth = 4;
583 if (rwidth > mrwidth)
584 mrwidth = rwidth;
586 mwidth += mrwidth;
588 if (mwidth < twidth)
589 mwidth = twidth;
592 wCoreConfigure(menu->menu, 0, theight, mwidth, menu->entry_no*eheight -1);
594 wFrameWindowResize(menu->frame, mwidth, menu->entry_no*eheight-1
595 + menu->frame->top_width + menu->frame->bottom_width);
598 updateTexture(menu);
600 menu->flags.realized = 1;
602 if (menu->flags.mapped)
603 wMenuPaint(menu);
604 if (menu->brother->flags.mapped)
605 wMenuPaint(menu->brother);
609 void
610 wMenuDestroy(WMenu *menu, int recurse)
612 int i;
614 WMRemoveNotificationObserver(menu);
616 /* remove any pending timers */
617 if (menu->timer)
618 WMDeleteTimerHandler(menu->timer);
619 menu->timer = NULL;
621 /* call destroy handler */
622 if (menu->on_destroy)
623 (*menu->on_destroy)(menu);
625 /* Destroy items if this menu own them. If this is the "brother" menu,
626 * leave them alone as it is shared by them.
628 if (!menu->flags.brother) {
629 for (i=0; i<menu->entry_no; i++) {
631 wfree(menu->entries[i]->text);
633 if (menu->entries[i]->rtext)
634 wfree(menu->entries[i]->rtext);
635 #ifdef USER_MENU
637 if (menu->entries[i]->instances){
638 PLRelease(menu->entries[i]->instances);
640 #endif /* USER_MENU */
642 if (menu->entries[i]->free_cdata && menu->entries[i]->clientdata) {
643 (*menu->entries[i]->free_cdata)(menu->entries[i]->clientdata);
645 wfree(menu->entries[i]);
648 if (recurse) {
649 for (i=0; i<menu->cascade_no; i++) {
650 if (menu->cascades[i]) {
651 if (menu->cascades[i]->flags.brother)
652 wMenuDestroy(menu->cascades[i]->brother, recurse);
653 else
654 wMenuDestroy(menu->cascades[i], recurse);
659 if (menu->entries)
660 wfree(menu->entries);
664 FREE_PIXMAP(menu->menu_texture_data);
666 if (menu->cascades)
667 wfree(menu->cascades);
669 wCoreDestroy(menu->menu);
670 wFrameWindowDestroy(menu->frame);
672 /* destroy copy of this menu */
673 if (!menu->flags.brother && menu->brother)
674 wMenuDestroy(menu->brother, False);
676 wfree(menu);
680 #define F_NORMAL 0
681 #define F_TOP 1
682 #define F_BOTTOM 2
683 #define F_NONE 3
685 static void
686 drawFrame(WScreen *scr, Window win, int y, int w, int h, int type)
688 XSegment segs[2];
689 int i;
691 i = 0;
692 segs[i].x1 = segs[i].x2 = w-1;
693 segs[i].y1 = y;
694 segs[i].y2 = y + h - 1;
695 i++;
696 if (type != F_TOP && type != F_NONE) {
697 segs[i].x1 = 1;
698 segs[i].y1 = segs[i].y2 = y + h-2;
699 segs[i].x2 = w-1;
700 i++;
702 XDrawSegments(dpy, win, scr->menu_item_auxtexture->dim_gc, segs, i);
704 i = 0;
705 segs[i].x1 = 0;
706 segs[i].y1 = y;
707 segs[i].x2 = 0;
708 segs[i].y2 = y + h - 1;
709 i++;
710 if (type != F_BOTTOM && type != F_NONE) {
711 segs[i].x1 = 0;
712 segs[i].y1 = y;
713 segs[i].x2 = w-1;
714 segs[i].y2 = y;
715 i++;
717 XDrawSegments(dpy, win, scr->menu_item_auxtexture->light_gc, segs, i);
719 if (type != F_TOP && type != F_NONE)
720 XDrawLine(dpy, win, scr->menu_item_auxtexture->dark_gc, 0, y+h-1,
721 w-1, y+h-1);
725 static void
726 paintEntry(WMenu *menu, int index, int selected)
728 int x, y, w, h, tw;
729 int type;
730 GC light, dim, dark, textGC;
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 #ifdef DRAWSTRING_PLUGIN
763 if (scr->menu_item_texture->any.type == WTEX_SOLID) {
764 XClearArea(dpy, win, 0, y + 1, w - 1, h - 3, False);
765 /* draw the frame */
766 drawFrame(scr, win, y, w, h, type);
767 } else {
768 /* if the function is there, it responses for clearing area */
769 if (!scr->drawstring_func[W_STRING_MTEXT])
770 XClearArea(dpy, win, 0, y, w, h, False);
772 #else
773 if (scr->menu_item_texture->any.type == WTEX_SOLID) {
774 XClearArea(dpy, win, 0, y + 1, w - 1, h - 3, False);
775 /* draw the frame */
776 drawFrame(scr, win, y, w, h, type);
777 } else {
778 XClearArea(dpy, win, 0, y, w, h, False);
780 #endif
783 if (selected) {
784 textGC = scr->select_menu_gc;
785 if (entry->flags.enabled)
786 XSetForeground(dpy, textGC, scr->select_text_pixel);
787 else
788 XSetForeground(dpy, textGC, scr->dtext_pixel);
789 } else if (!entry->flags.enabled) {
790 textGC = scr->disabled_menu_entry_gc;
791 } else {
792 textGC = scr->menu_entry_gc;
794 /* draw text */
795 x = 5;
796 if (entry->flags.indicator)
797 x += MENU_INDICATOR_SPACE + 2;
799 #ifdef DRAWSTRING_PLUGIN
800 if (scr->drawstring_func[W_STRING_MTEXT]) {
801 Pixmap tmp_bg;
802 void **p;
803 tmp_bg = XCreatePixmap(dpy, win, w, menu->entry_height, DefaultDepth(dpy, DefaultScreen(dpy)));
804 if (selected) {
805 if (menu->flags.brother) {
806 XCopyArea(dpy, menu->brother->menu_texture_data, tmp_bg, textGC,
807 0, y, w, menu->entry_height, 0, 0);
808 } else
809 XCopyArea(dpy, menu->menu_texture_data, tmp_bg, textGC,
810 0, y, w, menu->entry_height, 0, 0);
812 XSetForeground(dpy, scr->select_menu_gc, scr->select_pixel);
813 XFillRectangle(dpy, tmp_bg, scr->select_menu_gc, 1, 1, w-2, h-3);
814 } else {
815 if (menu->flags.brother) {
816 XCopyArea(dpy, menu->brother->menu_texture_data, tmp_bg, textGC,
817 0, y, w, menu->entry_height, 0, 0);
819 else
820 XCopyArea(dpy, menu->menu_texture_data, tmp_bg, textGC,
821 0, y, w, menu->entry_height, 0, 0);
824 p = wPluginPackData(4,
825 textGC,
826 scr->menu_entry_font,
827 scr->drawstring_func[W_STRING_MTEXT]->data,
828 /*menu->menu_texture_data,*/
829 tmp_bg,
830 "extendable");
831 scr->drawstring_func[W_STRING_MTEXT]->proc.drawString(
832 scr->drawstring_func[W_STRING_MTEXT]->arg,
833 win,
834 x, y,
835 menu->frame->core->width, menu->entry_height,
836 entry->text, p);
837 XFreePixmap(dpy, tmp_bg);
838 free(p);
839 } else {
840 WMDrawString(scr->wmscreen, win, textGC, scr->menu_entry_font,
841 x, 3 + y + wPreferences.menu_text_clearance, entry->text, strlen(entry->text));
844 #else
845 WMDrawString(scr->wmscreen, win, textGC, scr->menu_entry_font,
846 x, 3 + y + wPreferences.menu_text_clearance, entry->text, strlen(entry->text));
847 #endif
849 if (entry->cascade>=0) {
850 /* draw the cascade indicator */
851 XDrawLine(dpy,win,dim, w-11, y+6, w-6, y+h/2-1);
852 XDrawLine(dpy,win,light, w-11, y+h-8, w-6, y+h/2-1);
853 XDrawLine(dpy,win,dark, w-12, y+6, w-12, y+h-8);
856 /* draw indicator */
857 if (entry->flags.indicator && entry->flags.indicator_on) {
858 int iw, ih;
859 WPixmap *indicator;
862 switch (entry->flags.indicator_type) {
863 case MI_CHECK:
864 indicator = scr->menu_check_indicator;
865 break;
866 case MI_MINIWINDOW:
867 indicator = scr->menu_mini_indicator;
868 break;
869 case MI_HIDDEN:
870 indicator = scr->menu_hide_indicator;
871 break;
872 case MI_SHADED:
873 indicator = scr->menu_shade_indicator;
874 break;
875 case MI_DIAMOND:
876 default:
877 indicator = scr->menu_radio_indicator;
878 break;
881 iw = indicator->width;
882 ih = indicator->height;
883 XSetClipMask(dpy, scr->copy_gc, indicator->mask);
884 XSetClipOrigin(dpy, scr->copy_gc, 5, y+(h-ih)/2);
885 if (selected)
886 XSetForeground(dpy, scr->copy_gc, scr->black_pixel);
887 else
888 XSetForeground(dpy, scr->copy_gc, scr->mtext_pixel);
889 XFillRectangle(dpy, win, scr->copy_gc, 5, y+(h-ih)/2, iw, ih);
891 XCopyArea(dpy, indicator->image, win, scr->copy_gc, 0, 0,
892 iw, ih, 5, y+(h-ih)/2);
894 XSetClipOrigin(dpy, scr->copy_gc, 0, 0);
897 /* draw right text */
899 if (entry->rtext && entry->cascade<0) {
900 tw = WMWidthOfString(scr->menu_entry_font, entry->rtext,
901 strlen(entry->rtext));
903 WMDrawString(scr->wmscreen, win, textGC, scr->menu_entry_font, w-6-tw,
904 y + 3 + wPreferences.menu_text_clearance, entry->rtext, strlen(entry->rtext));
909 static void
910 move_menus(WMenu *menu, int x, int y)
912 while (menu->parent) {
913 menu = menu->parent;
914 x -= MENUW(menu);
915 if (!wPreferences.align_menus && menu->selected_entry>=0) {
916 y -= menu->selected_entry*menu->entry_height;
919 wMenuMove(menu, x, y, True);
922 static void
923 makeVisible(WMenu *menu)
925 WScreen *scr = menu->frame->screen_ptr;
926 int x1, y1, x2, y2, new_x, new_y, move;
928 if (menu->entry_no<0) return;
930 x1 = menu->frame_x;
931 y1 = menu->frame_y+menu->frame->top_width
932 + menu->selected_entry*menu->entry_height;
933 x2 = x1 + MENUW(menu);
934 y2 = y1 + menu->entry_height;
936 new_x = x1;
937 new_y = y1;
938 move = 0;
940 if (x1 < 0) {
941 new_x = 0;
942 move = 1;
943 } else if (x2 >= scr->scr_width) {
944 new_x = scr->scr_width - MENUW(menu) - 1;
945 move = 1;
948 if (y1 < 0) {
949 new_y = 0;
950 move = 1;
951 } else if (y2 >= scr->scr_height) {
952 new_y = scr->scr_height - menu->entry_height - 1;
953 move = 1;
956 new_y = new_y - menu->frame->top_width
957 - menu->selected_entry*menu->entry_height;
958 move_menus(menu, new_x, new_y);
962 static int
963 check_key(WMenu *menu, XKeyEvent *event)
965 int i, ch, s;
966 char buffer[32];
968 if (XLookupString(event, buffer, 32, NULL, NULL)<1)
969 return -1;
971 ch = toupper(buffer[0]);
973 s = (menu->selected_entry>=0 ? menu->selected_entry+1 : 0);
975 again:
976 for (i=s; i<menu->entry_no; i++) {
977 if (ch==toupper(menu->entries[i]->text[0])) {
978 return i;
981 /* no match. Retry from start, if previous started from a selected entry */
982 if (s!=0) {
983 s = 0;
984 goto again;
986 return -1;
990 static int
991 keyboardMenu(WMenu *menu)
993 XEvent event;
994 KeySym ksym=NoSymbol;
995 int done=0;
996 int index;
997 WMenuEntry *entry;
998 int old_pos_x = menu->frame_x;
999 int old_pos_y = menu->frame_y;
1000 int new_x = old_pos_x, new_y = old_pos_y;
1001 int scr_width = menu->frame->screen_ptr->scr_width;
1002 int scr_height = menu->frame->screen_ptr->scr_height;
1004 if (menu->flags.editing)
1005 return False;
1008 XGrabKeyboard(dpy, menu->frame->core->window, True, GrabModeAsync,
1009 GrabModeAsync, CurrentTime);
1011 if (menu->frame_y+menu->frame->top_width >= scr_height)
1012 new_y = scr_height - menu->frame->top_width;
1014 if (menu->frame_x+MENUW(menu) >= scr_width)
1015 new_x = scr_width-MENUW(menu)-1;
1017 move_menus(menu, new_x, new_y);
1019 while (!done && menu->flags.mapped) {
1020 XAllowEvents(dpy, AsyncKeyboard, CurrentTime);
1021 WMMaskEvent(dpy, ExposureMask|ButtonMotionMask|ButtonPressMask
1022 |ButtonReleaseMask|KeyPressMask|KeyReleaseMask
1023 |SubstructureNotifyMask, &event);
1025 switch (event.type) {
1026 case KeyPress:
1027 ksym = XLookupKeysym(&event.xkey, 0);
1028 switch (ksym) {
1029 case XK_Escape:
1030 done = 1;
1031 break;
1033 case XK_Home:
1034 #ifdef XK_KP_Home
1035 case XK_KP_Home:
1036 #endif
1037 selectEntry(menu, 0);
1038 makeVisible(menu);
1039 break;
1041 case XK_End:
1042 #ifdef XK_KP_End
1043 case XK_KP_End:
1044 #endif
1045 selectEntry(menu, menu->entry_no-1);
1046 makeVisible(menu);
1047 break;
1049 case XK_Up:
1050 #ifdef ARROWLESS_KBD
1051 case XK_k:
1052 #endif
1053 #ifdef XK_KP_Up
1054 case XK_KP_Up:
1055 #endif
1056 if (menu->selected_entry <= 0)
1057 selectEntry(menu, menu->entry_no-1);
1058 else
1059 selectEntry(menu, menu->selected_entry-1);
1060 makeVisible(menu);
1061 break;
1063 case XK_Down:
1064 #ifdef ARROWLESS_KBD
1065 case XK_j:
1066 #endif
1067 #ifdef XK_KP_Down
1068 case XK_KP_Down:
1069 #endif
1070 if (menu->selected_entry<0)
1071 selectEntry(menu, 0);
1072 else if (menu->selected_entry == menu->entry_no-1)
1073 selectEntry(menu, 0);
1074 else if (menu->selected_entry < menu->entry_no-1)
1075 selectEntry(menu, menu->selected_entry+1);
1076 makeVisible(menu);
1077 break;
1079 case XK_Right:
1080 #ifdef ARROWLESS_KBD
1081 case XK_l:
1082 #endif
1083 #ifdef XK_KP_Right
1084 case XK_KP_Right:
1085 #endif
1086 if (menu->selected_entry>=0) {
1087 WMenuEntry *entry;
1088 entry = menu->entries[menu->selected_entry];
1090 if (entry->cascade >= 0 && menu->cascades
1091 && menu->cascades[entry->cascade]->entry_no > 0) {
1093 XUngrabKeyboard(dpy, CurrentTime);
1095 selectEntry(menu->cascades[entry->cascade], 0);
1096 if (!keyboardMenu(menu->cascades[entry->cascade]))
1097 done = 1;
1099 XGrabKeyboard(dpy, menu->frame->core->window, True,
1100 GrabModeAsync, GrabModeAsync,
1101 CurrentTime);
1104 break;
1106 case XK_Left:
1107 #ifdef ARROWLESS_KBD
1108 case XK_h:
1109 #endif
1110 #ifdef XK_KP_Left
1111 case XK_KP_Left:
1112 #endif
1113 if (menu->parent!=NULL && menu->parent->selected_entry>=0) {
1114 selectEntry(menu, -1);
1115 move_menus(menu, old_pos_x, old_pos_y);
1116 return True;
1118 break;
1120 case XK_Return:
1121 done = 2;
1122 break;
1124 default:
1125 index = check_key(menu, &event.xkey);
1126 if (index>=0) {
1127 selectEntry(menu, index);
1130 break;
1132 default:
1133 if (event.type==ButtonPress)
1134 done = 1;
1136 WMHandleEvent(&event);
1140 XUngrabKeyboard(dpy, CurrentTime);
1142 if (done==2 && menu->selected_entry>=0) {
1143 entry = menu->entries[menu->selected_entry];
1144 } else {
1145 entry = NULL;
1148 if (entry && entry->callback!=NULL && entry->flags.enabled
1149 && entry->cascade < 0) {
1150 #if (MENU_BLINK_COUNT > 0)
1151 int sel = menu->selected_entry;
1152 int i;
1154 for (i=0; i<MENU_BLINK_COUNT; i++) {
1155 paintEntry(menu, sel, False);
1156 XSync(dpy, 0);
1157 wusleep(MENU_BLINK_DELAY);
1158 paintEntry(menu, sel, True);
1159 XSync(dpy, 0);
1160 wusleep(MENU_BLINK_DELAY);
1162 #endif
1163 selectEntry(menu, -1);
1165 if (!menu->flags.buttoned) {
1166 wMenuUnmap(menu);
1167 move_menus(menu, old_pos_x, old_pos_y);
1169 closeCascade(menu);
1171 (*entry->callback)(menu, entry);
1172 } else {
1173 if (!menu->flags.buttoned) {
1174 wMenuUnmap(menu);
1175 move_menus(menu, old_pos_x, old_pos_y);
1177 selectEntry(menu, -1);
1181 /* returns True if returning from a submenu to a parent menu,
1182 * False if exiting from menu */
1183 return False;
1187 void
1188 wMenuMapAt(WMenu *menu, int x, int y, int keyboard)
1190 int scr_width = menu->frame->screen_ptr->scr_width;
1191 int scr_height = menu->frame->screen_ptr->scr_height;
1193 if (!menu->flags.realized) {
1194 menu->flags.realized=1;
1195 wMenuRealize(menu);
1197 if (!menu->flags.mapped) {
1198 if (wPreferences.wrap_menus) {
1199 if (x<0) x = 0;
1200 if (y<0) y = 0;
1201 if (x+MENUW(menu) > scr_width)
1202 x = scr_width - MENUW(menu);
1203 if (y+MENUH(menu) > scr_height)
1204 y = scr_height - MENUH(menu);
1207 XMoveWindow(dpy, menu->frame->core->window, x, y);
1208 menu->frame_x = x;
1209 menu->frame_y = y;
1210 XMapWindow(dpy, menu->frame->core->window);
1211 wRaiseFrame(menu->frame->core);
1212 menu->flags.mapped = 1;
1213 } else {
1214 selectEntry(menu, 0);
1217 if (keyboard)
1218 keyboardMenu(menu);
1222 void
1223 wMenuMap(WMenu *menu)
1225 if (!menu->flags.realized) {
1226 menu->flags.realized=1;
1227 wMenuRealize(menu);
1229 if (menu->flags.app_menu && menu->parent==NULL) {
1230 menu->frame_x = menu->frame->screen_ptr->app_menu_x;
1231 menu->frame_y = menu->frame->screen_ptr->app_menu_y;
1232 XMoveWindow(dpy, menu->frame->core->window, menu->frame_x, menu->frame_y);
1234 XMapWindow(dpy, menu->frame->core->window);
1235 wRaiseFrame(menu->frame->core);
1236 menu->flags.mapped = 1;
1240 void
1241 wMenuUnmap(WMenu *menu)
1243 int i;
1245 XUnmapWindow(dpy, menu->frame->core->window);
1246 if (menu->flags.titled && menu->flags.buttoned) {
1247 wFrameWindowHideButton(menu->frame, WFF_RIGHT_BUTTON);
1249 menu->flags.buttoned = 0;
1250 menu->flags.mapped = 0;
1251 menu->flags.open_to_left = 0;
1253 for (i=0; i<menu->cascade_no; i++) {
1254 if (menu->cascades[i]!=NULL
1255 && menu->cascades[i]->flags.mapped
1256 && !menu->cascades[i]->flags.buttoned) {
1258 wMenuUnmap(menu->cascades[i]);
1261 menu->selected_entry = -1;
1266 void
1267 wMenuPaint(WMenu *menu)
1269 int i;
1271 if (!menu->flags.mapped) {
1272 return;
1275 /* paint entries */
1276 for (i=0; i<menu->entry_no; i++) {
1277 paintEntry(menu, i, i==menu->selected_entry);
1282 void
1283 wMenuSetEnabled(WMenu *menu, int index, int enable)
1285 if (index>=menu->entry_no) return;
1286 menu->entries[index]->flags.enabled=enable;
1287 paintEntry(menu, index, index==menu->selected_entry);
1288 paintEntry(menu->brother, index, index==menu->selected_entry);
1292 /* ====================================================================== */
1295 static void
1296 editEntry(WMenu *menu, WMenuEntry *entry)
1298 WTextInput *text;
1299 XEvent event;
1300 WObjDescriptor *desc;
1301 char *t;
1302 int done = 0;
1303 Window old_focus;
1304 int old_revert;
1306 menu->flags.editing = 1;
1308 text = wTextCreate(menu->menu, 1, menu->entry_height * entry->order,
1309 menu->menu->width - 2, menu->entry_height - 1);
1311 wTextPutText(text, entry->text);
1312 XGetInputFocus(dpy, &old_focus, &old_revert);
1313 XSetInputFocus(dpy, text->core->window, RevertToNone, CurrentTime);
1315 if (XGrabKeyboard(dpy, text->core->window, True, GrabModeAsync,
1316 GrabModeAsync, CurrentTime)!=GrabSuccess) {
1317 wwarning("could not grab keyboard");
1318 wTextDestroy(text);
1320 wSetFocusTo(menu->frame->screen_ptr,
1321 menu->frame->screen_ptr->focused_window);
1322 return;
1326 while (!done && !text->done) {
1327 XSync(dpy, 0);
1328 XAllowEvents(dpy, AsyncKeyboard|AsyncPointer, CurrentTime);
1329 XSync(dpy, 0);
1330 WMNextEvent(dpy, &event);
1332 if (XFindContext(dpy, event.xany.window, wWinContext,
1333 (XPointer *)&desc)==XCNOENT)
1334 desc = NULL;
1336 if ((desc != NULL) && (desc->handle_anything != NULL)) {
1338 (*desc->handle_anything)(desc, &event);
1340 } else {
1341 switch (event.type) {
1342 case ButtonPress:
1343 XAllowEvents(dpy, ReplayPointer, CurrentTime);
1344 done = 1;
1346 default:
1347 WMHandleEvent(&event);
1348 break;
1353 XSetInputFocus(dpy, old_focus, old_revert, CurrentTime);
1355 wSetFocusTo(menu->frame->screen_ptr,
1356 menu->frame->screen_ptr->focused_window);
1359 t = wTextGetText(text);
1360 /* if !t, the user has canceled editing */
1361 if (t) {
1362 if (entry->text)
1363 wfree(entry->text);
1364 entry->text = wstrdup(t);
1366 menu->flags.realized = 0;
1368 wTextDestroy(text);
1370 XUngrabKeyboard(dpy, CurrentTime);
1372 if (t && menu->on_edit)
1373 (*menu->on_edit)(menu, entry);
1375 menu->flags.editing = 0;
1377 if (!menu->flags.realized)
1378 wMenuRealize(menu);
1382 static void
1383 selectEntry(WMenu *menu, int entry_no)
1385 WMenuEntry *entry;
1386 WMenu *submenu;
1387 int old_entry;
1389 if (menu->entries==NULL)
1390 return;
1392 if (entry_no >= menu->entry_no)
1393 return;
1395 old_entry = menu->selected_entry;
1396 menu->selected_entry = entry_no;
1398 if (old_entry!=entry_no) {
1400 /* unselect previous entry */
1401 if (old_entry>=0) {
1402 paintEntry(menu, old_entry, False);
1403 entry = menu->entries[old_entry];
1405 /* unmap cascade */
1406 if (entry->cascade>=0 && menu->cascades) {
1407 if (!menu->cascades[entry->cascade]->flags.buttoned) {
1408 wMenuUnmap(menu->cascades[entry->cascade]);
1413 if (entry_no<0) {
1414 menu->selected_entry = -1;
1415 return;
1417 entry = menu->entries[entry_no];
1419 if (entry->cascade>=0 && menu->cascades && entry->flags.enabled) {
1420 /* Callback for when the submenu is opened.
1422 submenu = menu->cascades[entry->cascade];
1423 if (submenu && submenu->flags.brother)
1424 submenu = submenu->brother;
1426 if (entry->callback) {
1427 /* Only call the callback if the submenu is not yet mapped.
1429 if (menu->flags.brother) {
1430 if (!submenu || !submenu->flags.mapped)
1431 (*entry->callback)(menu->brother, entry);
1432 } else {
1433 if (!submenu || !submenu->flags.buttoned)
1434 (*entry->callback)(menu, entry);
1438 /* the submenu menu might have changed */
1439 submenu = menu->cascades[entry->cascade];
1441 /* map cascade */
1442 if (!submenu->flags.mapped) {
1443 int x, y;
1445 if (!submenu->flags.realized)
1446 wMenuRealize(submenu);
1447 if (wPreferences.wrap_menus) {
1448 if (menu->flags.open_to_left)
1449 submenu->flags.open_to_left = 1;
1451 if (submenu->flags.open_to_left) {
1452 x = menu->frame_x - MENUW(submenu);
1453 if (x<0) {
1454 x = 0;
1455 submenu->flags.open_to_left = 0;
1457 } else {
1458 x = menu->frame_x + MENUW(menu);
1460 if (x + MENUW(submenu)
1461 >= menu->frame->screen_ptr->scr_width) {
1463 x = menu->frame_x - MENUW(submenu);
1464 submenu->flags.open_to_left = 1;
1467 } else {
1468 x = menu->frame_x + MENUW(menu);
1471 if (wPreferences.align_menus) {
1472 y = menu->frame_y;
1473 } else {
1474 y = menu->frame_y + menu->entry_height*entry_no;
1475 if (menu->flags.titled)
1476 y += menu->frame->top_width;
1477 if (menu->cascades[entry->cascade]->flags.titled)
1478 y -= menu->cascades[entry->cascade]->frame->top_width;
1481 wMenuMapAt(menu->cascades[entry->cascade], x, y, False);
1482 menu->cascades[entry->cascade]->parent = menu;
1483 } else {
1484 return;
1487 paintEntry(menu, entry_no, True);
1492 static WMenu*
1493 findMenu(WScreen *scr, int *x_ret, int *y_ret)
1495 WMenu *menu;
1496 WObjDescriptor *desc;
1497 Window root_ret, win, junk_win;
1498 int x, y, wx, wy;
1499 unsigned int mask;
1501 XQueryPointer(dpy, scr->root_win, &root_ret, &win, &x, &y, &wx, &wy,
1502 &mask);
1504 if (win==None) return NULL;
1506 if (XFindContext(dpy, win, wWinContext, (XPointer *)&desc)==XCNOENT)
1507 return NULL;
1509 if (desc->parent_type == WCLASS_MENU) {
1510 menu = (WMenu*)desc->parent;
1511 XTranslateCoordinates(dpy, root_ret, menu->menu->window, wx, wy,
1512 x_ret, y_ret, &junk_win);
1513 return menu;
1515 return NULL;
1521 static void
1522 closeCascade(WMenu *menu)
1524 WMenu *parent=menu->parent;
1526 if (menu->flags.brother
1527 || (!menu->flags.buttoned
1528 && (!menu->flags.app_menu||menu->parent!=NULL))) {
1530 selectEntry(menu, -1);
1531 XSync(dpy, 0);
1532 #if (MENU_BLINK_DELAY > 2)
1533 wusleep(MENU_BLINK_DELAY/2);
1534 #endif
1535 wMenuUnmap(menu);
1536 while (parent!=NULL
1537 && (parent->parent!=NULL || !parent->flags.app_menu
1538 || parent->flags.brother)
1539 && !parent->flags.buttoned) {
1540 selectEntry(parent, -1);
1541 wMenuUnmap(parent);
1542 parent = parent->parent;
1544 if (parent)
1545 selectEntry(parent, -1);
1550 static void
1551 closeBrotherCascadesOf(WMenu *menu)
1553 WMenu *tmp;
1554 int i;
1556 for (i=0; i<menu->cascade_no; i++) {
1557 if (menu->cascades[i]->flags.brother) {
1558 tmp = menu->cascades[i];
1559 } else {
1560 tmp = menu->cascades[i]->brother;
1562 if (tmp->flags.mapped) {
1563 selectEntry(tmp->parent, -1);
1564 closeBrotherCascadesOf(tmp);
1565 break;
1571 #define getEntryAt(menu, x, y) ((y)<0 ? -1 : (y)/(menu->entry_height))
1574 static WMenu*
1575 parentMenu(WMenu *menu)
1577 WMenu *parent;
1578 WMenuEntry *entry;
1580 if (menu->flags.buttoned)
1581 return menu;
1583 while (menu->parent && menu->parent->flags.mapped) {
1584 parent = menu->parent;
1585 if (parent->selected_entry < 0)
1586 break;
1587 entry = parent->entries[parent->selected_entry];
1588 if (!entry->flags.enabled || entry->cascade<0 || !parent->cascades ||
1589 parent->cascades[entry->cascade] != menu)
1590 break;
1591 menu = parent;
1592 if (menu->flags.buttoned)
1593 break;
1596 return menu;
1602 * Will raise the passed menu, if submenu = 0
1603 * If submenu > 0 will also raise all mapped submenus
1604 * until the first buttoned one
1605 * If submenu < 0 will also raise all mapped parent menus
1606 * until the first buttoned one
1609 static void
1610 raiseMenus(WMenu *menu, int submenus)
1612 WMenu *submenu;
1613 int i;
1615 if(!menu) return;
1617 wRaiseFrame(menu->frame->core);
1619 if (submenus>0 && menu->selected_entry>=0) {
1620 i = menu->entries[menu->selected_entry]->cascade;
1621 if (i>=0 && menu->cascades) {
1622 submenu = menu->cascades[i];
1623 if (submenu->flags.mapped && !submenu->flags.buttoned)
1624 raiseMenus(submenu, submenus);
1627 if (submenus<0 && !menu->flags.buttoned &&
1628 menu->parent && menu->parent->flags.mapped)
1629 raiseMenus(menu->parent, submenus);
1633 WMenu*
1634 wMenuUnderPointer(WScreen *screen)
1636 WObjDescriptor *desc;
1637 Window root_ret, win;
1638 int dummy;
1639 unsigned int mask;
1641 XQueryPointer(dpy, screen->root_win, &root_ret, &win, &dummy, &dummy,
1642 &dummy, &dummy, &mask);
1644 if (win==None) return NULL;
1646 if (XFindContext(dpy, win, wWinContext, (XPointer *)&desc)==XCNOENT)
1647 return NULL;
1649 if (desc->parent_type == WCLASS_MENU)
1650 return (WMenu *)desc->parent;
1651 return NULL;
1657 static void
1658 getPointerPosition(WScreen *scr, int *x, int *y)
1660 Window root_ret, win;
1661 int wx, wy;
1662 unsigned int mask;
1664 XQueryPointer(dpy, scr->root_win, &root_ret, &win, x, y, &wx, &wy, &mask);
1668 static void
1669 getScrollAmount(WMenu *menu, int *hamount, int *vamount)
1671 WScreen *scr = menu->menu->screen_ptr;
1672 int menuX1 = menu->frame_x;
1673 int menuY1 = menu->frame_y;
1674 int menuX2 = menu->frame_x + MENUW(menu);
1675 int menuY2 = menu->frame_y + MENUH(menu);
1676 int screenW = scr->scr_width;
1677 int screenH = scr->scr_height;
1678 int xroot, yroot;
1680 *hamount = 0;
1681 *vamount = 0;
1683 getPointerPosition(scr, &xroot, &yroot);
1685 #ifdef VIRTUAL_DESKTOP
1686 if (wPreferences.vedge_thickness) {
1687 if (xroot <= wPreferences.vedge_thickness + 1 && menuX1 < wPreferences.vedge_thickness) {
1688 /* scroll to the right */
1689 *hamount = WMIN(MENU_SCROLL_STEP, abs(menuX1));
1691 } else if (xroot >= screenW-2-wPreferences.vedge_thickness && menuX2 > screenW-1-wPreferences.vedge_thickness) {
1692 /* scroll to the left */
1693 *hamount = WMIN(MENU_SCROLL_STEP, abs(menuX2-screenW-1));
1695 if (*hamount==0)
1696 *hamount = 1;
1698 *hamount = -*hamount;
1700 } else
1701 #endif
1703 if (xroot <= 1 && menuX1 < 0) {
1704 /* scroll to the right */
1705 *hamount = WMIN(MENU_SCROLL_STEP, abs(menuX1));
1707 } else if (xroot >= screenW-2 && menuX2 > screenW-1) {
1708 /* scroll to the left */
1709 *hamount = WMIN(MENU_SCROLL_STEP, abs(menuX2-screenW-1));
1711 if (*hamount==0)
1712 *hamount = 1;
1714 *hamount = -*hamount;
1717 if (yroot <= 1 && menuY1 < 0) {
1718 /* scroll down */
1719 *vamount = WMIN(MENU_SCROLL_STEP, abs(menuY1));
1721 } else if (yroot >= screenH-2 && menuY2 > screenH-1) {
1722 /* scroll up */
1723 *vamount = WMIN(MENU_SCROLL_STEP, abs(menuY2-screenH-2));
1725 *vamount = -*vamount;
1730 static void
1731 dragScrollMenuCallback(void *data)
1733 WMenu *menu = (WMenu*)data;
1734 WScreen *scr = menu->menu->screen_ptr;
1735 WMenu *parent = parentMenu(menu);
1736 int hamount, vamount;
1737 int x, y;
1738 int newSelectedEntry;
1740 getScrollAmount(menu, &hamount, &vamount);
1743 if (hamount != 0 || vamount != 0) {
1744 wMenuMove(parent, parent->frame_x + hamount,
1745 parent->frame_y + vamount, True);
1746 if (findMenu(scr, &x, &y)) {
1747 newSelectedEntry = getEntryAt(menu, x, y);
1748 selectEntry(menu, newSelectedEntry);
1749 } else {
1750 /* Pointer fell outside of menu. If the selected entry is
1751 * not a submenu, unselect it */
1752 if (menu->selected_entry >= 0
1753 && menu->entries[menu->selected_entry]->cascade<0)
1754 selectEntry(menu, -1);
1755 newSelectedEntry = 0;
1758 /* paranoid check */
1759 if (newSelectedEntry >= 0) {
1760 /* keep scrolling */
1761 menu->timer = WMAddTimerHandler(MENU_SCROLL_DELAY,
1762 dragScrollMenuCallback, menu);
1763 } else {
1764 menu->timer = NULL;
1766 } else {
1767 /* don't need to scroll anymore */
1768 menu->timer = NULL;
1769 if (findMenu(scr, &x, &y)) {
1770 newSelectedEntry = getEntryAt(menu, x, y);
1771 selectEntry(menu, newSelectedEntry);
1777 static void
1778 scrollMenuCallback(void *data)
1780 WMenu *menu = (WMenu*)data;
1781 WMenu *parent = parentMenu(menu);
1782 int hamount = 0; /* amount to scroll */
1783 int vamount = 0;
1785 getScrollAmount(menu, &hamount, &vamount);
1787 if (hamount != 0 || vamount != 0) {
1788 wMenuMove(parent, parent->frame_x + hamount,
1789 parent->frame_y + vamount, True);
1791 /* keep scrolling */
1792 menu->timer = WMAddTimerHandler(MENU_SCROLL_DELAY,
1793 scrollMenuCallback, menu);
1794 } else {
1795 /* don't need to scroll anymore */
1796 menu->timer = NULL;
1802 #define MENU_SCROLL_BORDER 5
1804 static int
1805 isPointNearBoder(WMenu *menu, int x, int y)
1807 int menuX1 = menu->frame_x;
1808 int menuY1 = menu->frame_y;
1809 int menuX2 = menu->frame_x + MENUW(menu);
1810 int menuY2 = menu->frame_y + MENUH(menu);
1811 int scrXe = menu->menu->screen_ptr->scr_width-1;
1812 int scrYe = menu->menu->screen_ptr->scr_height-1;
1813 int flag = 0;
1815 if (x >= menuX1 && x <= menuX2 && (y < MENU_SCROLL_BORDER
1816 || y > scrYe-MENU_SCROLL_BORDER))
1817 flag = 1;
1818 else if (y >= menuY1 && y <= menuY2 && (x < MENU_SCROLL_BORDER
1819 || x > scrXe-MENU_SCROLL_BORDER))
1820 flag = 1;
1822 return flag;
1826 typedef struct _delay {
1827 WWindow *wwin;
1828 WMenu *menu;
1829 int ox,oy;
1830 } _delay;
1833 static void
1834 _leaving(_delay *dl)
1836 wMenuMove(dl->menu, dl->ox, dl->oy, True);
1837 dl->menu->jump_back=NULL;
1838 dl->menu->menu->screen_ptr->flags.jump_back_pending = 0;
1839 wfree(dl);
1843 void
1844 wMenuScroll(WMenu *menu, XEvent *event)
1846 WMenu *smenu;
1847 WMenu *omenu = parentMenu(menu);
1848 WScreen *scr = menu->frame->screen_ptr;
1849 int done = 0;
1850 int jump_back = 0;
1851 int old_frame_x = omenu->frame_x;
1852 int old_frame_y = omenu->frame_y;
1853 XEvent ev;
1855 if (omenu->jump_back)
1856 WMDeleteTimerWithClientData(omenu->jump_back);
1859 if ((/*omenu->flags.buttoned &&*/ !wPreferences.wrap_menus)
1860 || omenu->flags.app_menu) {
1861 jump_back = 1;
1864 if (!wPreferences.wrap_menus)
1865 raiseMenus(omenu, True);
1866 else
1867 raiseMenus(menu, False);
1869 if (!menu->timer)
1870 scrollMenuCallback(menu);
1872 while(!done) {
1873 int x, y, on_border, on_x_edge, on_y_edge, on_title;
1875 WMNextEvent(dpy, &ev);
1876 switch (ev.type) {
1877 case EnterNotify:
1878 WMHandleEvent(&ev);
1879 case MotionNotify:
1880 x = (ev.type==MotionNotify) ? ev.xmotion.x_root : ev.xcrossing.x_root;
1881 y = (ev.type==MotionNotify) ? ev.xmotion.y_root : ev.xcrossing.y_root;
1883 /* on_border is != 0 if the pointer is between the menu
1884 * and the screen border and is close enough to the border */
1885 on_border = isPointNearBoder(menu, x, y);
1887 smenu = wMenuUnderPointer(scr);
1889 if ((smenu==NULL && !on_border) || (smenu && parentMenu(smenu)!=omenu)) {
1890 done = 1;
1891 break;
1894 on_x_edge = x <= 1 || x >= scr->scr_width - 2;
1895 on_y_edge = y <= 1 || y >= scr->scr_height - 2;
1896 on_border = on_x_edge || on_y_edge;
1898 if (!on_border && !jump_back) {
1899 done = 1;
1900 break;
1903 if (menu->timer && (smenu!=menu || (!on_y_edge && !on_x_edge))) {
1904 WMDeleteTimerHandler(menu->timer);
1905 menu->timer = NULL;
1908 if (smenu != NULL)
1909 menu = smenu;
1911 if (!menu->timer)
1912 scrollMenuCallback(menu);
1913 break;
1914 case ButtonPress:
1915 /* True if we push on title, or drag the omenu to other position */
1916 on_title = ev.xbutton.x_root >= omenu->frame_x &&
1917 ev.xbutton.x_root <= omenu->frame_x + MENUW(omenu) &&
1918 ev.xbutton.y_root >= omenu->frame_y &&
1919 ev.xbutton.y_root <= omenu->frame_y + omenu->frame->top_width;
1920 WMHandleEvent(&ev);
1921 smenu = wMenuUnderPointer(scr);
1922 if (smenu == NULL || (smenu && smenu->flags.buttoned && smenu != omenu))
1923 done = 1;
1924 else if (smenu==omenu && on_title) {
1925 jump_back = 0;
1926 done = 1;
1928 break;
1929 case KeyPress:
1930 done = 1;
1931 default:
1932 WMHandleEvent(&ev);
1933 break;
1937 if (menu->timer) {
1938 WMDeleteTimerHandler(menu->timer);
1939 menu->timer = NULL;
1942 if (jump_back) {
1943 _delay *delayer;
1944 if (!omenu->jump_back) {
1945 delayer=wmalloc(sizeof(_delay));
1946 delayer->menu=omenu;
1947 delayer->ox=old_frame_x;
1948 delayer->oy=old_frame_y;
1949 omenu->jump_back=delayer;
1950 scr->flags.jump_back_pending = 1;
1952 else delayer = omenu->jump_back;
1953 WMAddTimerHandler(MENU_JUMP_BACK_DELAY,(WMCallback*)_leaving, delayer);
1959 static void
1960 menuExpose(WObjDescriptor *desc, XEvent *event)
1962 wMenuPaint(desc->parent);
1965 typedef struct {
1966 int *delayed_select;
1967 WMenu *menu;
1968 WMHandlerID magic;
1969 } delay_data;
1972 static void
1973 delaySelection(void *data)
1975 delay_data *d = (delay_data*)data;
1976 int x, y, entry_no;
1977 WMenu *menu;
1979 d->magic = NULL;
1981 menu = findMenu(d->menu->menu->screen_ptr, &x, &y);
1982 if (menu && (d->menu == menu || d->delayed_select)) {
1983 entry_no = getEntryAt(menu, x, y);
1984 selectEntry(menu, entry_no);
1986 if (d->delayed_select)
1987 *(d->delayed_select) = 0;
1991 static void
1992 menuMouseDown(WObjDescriptor *desc, XEvent *event)
1994 XButtonEvent *bev = &event->xbutton;
1995 WMenu *menu = desc->parent;
1996 WMenu *smenu;
1997 WScreen *scr=menu->frame->screen_ptr;
1998 WMenuEntry *entry=NULL;
1999 XEvent ev;
2000 int close_on_exit=0;
2001 int done=0;
2002 int delayed_select = 0;
2003 int entry_no;
2004 int x, y;
2005 int prevx, prevy;
2006 int old_frame_x = 0;
2007 int old_frame_y = 0;
2008 delay_data d_data = {NULL, NULL, NULL};
2010 if (!wPreferences.wrap_menus) {
2011 smenu = parentMenu(menu);
2012 old_frame_x = smenu->frame_x;
2013 old_frame_y = smenu->frame_y;
2014 } else if (event->xbutton.window == menu->frame->core->window) {
2015 /* This is true if the menu was launched with right click on root window */
2016 delayed_select = 1;
2017 d_data.delayed_select = &delayed_select;
2018 d_data.menu = menu;
2019 d_data.magic = WMAddTimerHandler(wPreferences.dblclick_time,
2020 delaySelection, &d_data);
2023 if (menu->flags.inside_handler) {
2024 return;
2026 menu->flags.inside_handler = 1;
2028 wRaiseFrame(menu->frame->core);
2030 close_on_exit = (bev->send_event || menu->flags.brother);
2032 smenu = findMenu(scr, &x, &y);
2033 if (!smenu) {
2034 x = -1;
2035 y = -1;
2036 } else {
2037 menu = smenu;
2040 if (menu->flags.editing) {
2041 goto byebye;
2043 entry_no = getEntryAt(menu, x, y);
2044 if (entry_no>=0) {
2045 entry = menu->entries[entry_no];
2047 if (!close_on_exit && (bev->state & ControlMask) && smenu
2048 && entry->flags.editable) {
2049 editEntry(smenu, entry);
2050 goto byebye;
2051 } else if (bev->state & ControlMask) {
2052 goto byebye;
2055 if (entry->flags.enabled && entry->cascade>=0 && menu->cascades) {
2056 WMenu *submenu = menu->cascades[entry->cascade];
2057 /* map cascade */
2058 if (submenu->flags.mapped && !submenu->flags.buttoned &&
2059 menu->selected_entry!=entry_no) {
2060 wMenuUnmap(submenu);
2062 if (!submenu->flags.mapped && !delayed_select) {
2063 selectEntry(menu, entry_no);
2064 } else if (!submenu->flags.buttoned) {
2065 selectEntry(menu, -1);
2068 } else if (!delayed_select) {
2069 selectEntry(menu, entry_no);
2072 if (!wPreferences.wrap_menus && !wPreferences.scrollable_menus) {
2073 if (!menu->timer)
2074 dragScrollMenuCallback(menu);
2078 prevx = bev->x_root;
2079 prevy = bev->y_root;
2080 while (!done) {
2081 int x, y;
2083 XAllowEvents(dpy, AsyncPointer|SyncPointer, CurrentTime);
2085 WMMaskEvent(dpy, ExposureMask|ButtonMotionMask|ButtonReleaseMask
2086 |ButtonPressMask, &ev);
2087 switch (ev.type) {
2088 case MotionNotify:
2089 smenu = findMenu(scr, &x, &y);
2091 if (smenu == NULL) {
2092 /* moved mouse out of menu */
2094 if (!delayed_select && d_data.magic) {
2095 WMDeleteTimerHandler(d_data.magic);
2096 d_data.magic = NULL;
2098 if (menu==NULL
2099 || (menu->selected_entry>=0
2100 && menu->entries[menu->selected_entry]->cascade>=0)) {
2101 prevx = ev.xmotion.x_root;
2102 prevy = ev.xmotion.y_root;
2104 break;
2106 selectEntry(menu, -1);
2107 menu = smenu;
2108 prevx = ev.xmotion.x_root;
2109 prevy = ev.xmotion.y_root;
2110 break;
2111 } else if (menu && menu!=smenu
2112 && (menu->selected_entry<0
2113 || menu->entries[menu->selected_entry]->cascade<0)) {
2114 selectEntry(menu, -1);
2116 if (!delayed_select && d_data.magic) {
2117 WMDeleteTimerHandler(d_data.magic);
2118 d_data.magic = NULL;
2120 } else {
2122 /* hysteresis for item selection */
2124 /* check if the motion was to the side, indicating that
2125 * the user may want to cross to a submenu */
2126 if (!delayed_select && menu) {
2127 int dx;
2128 Bool moved_to_submenu;/* moved to direction of submenu */
2130 dx = abs(prevx - ev.xmotion.x_root);
2132 moved_to_submenu = False;
2133 if (dx > 0 /* if moved enough to the side */
2134 /* maybe a open submenu */
2135 && menu->selected_entry>=0
2136 /* moving to the right direction */
2137 && (wPreferences.align_menus
2138 || ev.xmotion.y_root >= prevy)) {
2139 int index;
2141 index = menu->entries[menu->selected_entry]->cascade;
2142 if (index>=0) {
2143 if (menu->cascades[index]->frame_x>menu->frame_x) {
2144 if (prevx < ev.xmotion.x_root)
2145 moved_to_submenu = True;
2146 } else {
2147 if (prevx > ev.xmotion.x_root)
2148 moved_to_submenu = True;
2154 if (menu != smenu) {
2155 if (d_data.magic) {
2156 WMDeleteTimerHandler(d_data.magic);
2158 d_data.magic = NULL;
2159 } else if (moved_to_submenu) {
2160 /* while we are moving, postpone the selection */
2161 if (d_data.magic) {
2162 WMDeleteTimerHandler(d_data.magic);
2164 d_data.delayed_select = NULL;
2165 d_data.menu = menu;
2166 d_data.magic = WMAddTimerHandler(MENU_SELECT_DELAY,
2167 delaySelection,
2168 &d_data);
2169 prevx = ev.xmotion.x_root;
2170 prevy = ev.xmotion.y_root;
2171 break;
2172 } else {
2173 if (d_data.magic)
2174 WMDeleteTimerHandler(d_data.magic);
2175 d_data.magic = NULL;
2179 prevx = ev.xmotion.x_root;
2180 prevy = ev.xmotion.y_root;
2181 if (menu!=smenu) {
2182 /* pointer crossed menus */
2183 if (menu && menu->timer) {
2184 WMDeleteTimerHandler(menu->timer);
2185 menu->timer = NULL;
2187 if (smenu)
2188 dragScrollMenuCallback(smenu);
2190 menu = smenu;
2191 if (!menu->timer)
2192 dragScrollMenuCallback(menu);
2194 if (!delayed_select) {
2195 entry_no = getEntryAt(menu, x, y);
2196 if (entry_no>=0) {
2197 entry = menu->entries[entry_no];
2198 if (entry->flags.enabled && entry->cascade>=0 &&
2199 menu->cascades) {
2200 WMenu *submenu = menu->cascades[entry->cascade];
2201 if (submenu->flags.mapped && !submenu->flags.buttoned
2202 && menu->selected_entry!=entry_no) {
2203 wMenuUnmap(submenu);
2207 selectEntry(menu, entry_no);
2209 break;
2211 case ButtonPress:
2212 break;
2214 case ButtonRelease:
2215 if (ev.xbutton.button == event->xbutton.button)
2216 done=1;
2217 break;
2219 case Expose:
2220 WMHandleEvent(&ev);
2221 break;
2225 if (menu && menu->timer) {
2226 WMDeleteTimerHandler(menu->timer);
2227 menu->timer = NULL;
2229 if (d_data.magic!=NULL)
2230 WMDeleteTimerHandler(d_data.magic);
2232 if (menu && menu->selected_entry>=0) {
2233 entry = menu->entries[menu->selected_entry];
2234 if (entry->callback!=NULL && entry->flags.enabled
2235 && entry->cascade < 0) {
2236 /* blink and erase menu selection */
2237 #if (MENU_BLINK_DELAY > 0)
2238 int sel = menu->selected_entry;
2239 int i;
2241 for (i=0; i<MENU_BLINK_COUNT; i++) {
2242 paintEntry(menu, sel, False);
2243 XSync(dpy, 0);
2244 wusleep(MENU_BLINK_DELAY);
2245 paintEntry(menu, sel, True);
2246 XSync(dpy, 0);
2247 wusleep(MENU_BLINK_DELAY);
2249 #endif
2250 /* unmap the menu, it's parents and call the callback */
2251 if (!menu->flags.buttoned &&
2252 (!menu->flags.app_menu||menu->parent!=NULL)) {
2253 closeCascade(menu);
2254 } else {
2255 selectEntry(menu, -1);
2257 (*entry->callback)(menu, entry);
2259 /* If the user double clicks an entry, the entry will
2260 * be executed twice, which is not good for things like
2261 * the root menu. So, ignore any clicks that were generated
2262 * while the entry was being executed */
2263 while (XCheckTypedWindowEvent(dpy, menu->menu->window,
2264 ButtonPress, &ev));
2265 } else if (entry->callback!=NULL && entry->cascade<0) {
2266 selectEntry(menu, -1);
2267 } else {
2268 if (entry->cascade>=0 && menu->cascades
2269 && menu->cascades[entry->cascade]->flags.brother) {
2270 selectEntry(menu, -1);
2275 if (((WMenu*)desc->parent)->flags.brother || close_on_exit || !smenu)
2276 closeCascade(desc->parent);
2278 /* close the cascade windows that should not remain opened */
2279 closeBrotherCascadesOf(desc->parent);
2281 if (!wPreferences.wrap_menus)
2282 wMenuMove(parentMenu(desc->parent), old_frame_x, old_frame_y, True);
2284 byebye:
2285 ((WMenu*)desc->parent)->flags.inside_handler = 0;
2289 void
2290 wMenuMove(WMenu *menu, int x, int y, int submenus)
2292 WMenu *submenu;
2293 int i;
2295 if (!menu) return;
2297 menu->frame_x = x;
2298 menu->frame_y = y;
2299 XMoveWindow(dpy, menu->frame->core->window, x, y);
2301 if (submenus>0 && menu->selected_entry>=0) {
2302 i = menu->entries[menu->selected_entry]->cascade;
2304 if (i>=0 && menu->cascades) {
2305 submenu = menu->cascades[i];
2306 if (submenu->flags.mapped && !submenu->flags.buttoned) {
2307 if (wPreferences.align_menus) {
2308 wMenuMove(submenu, x + MENUW(menu), y, submenus);
2309 } else {
2310 wMenuMove(submenu, x+ MENUW(menu),
2311 y + submenu->entry_height*menu->selected_entry,
2312 submenus);
2317 if (submenus<0 && menu->parent!=NULL && menu->parent->flags.mapped &&
2318 !menu->parent->flags.buttoned) {
2319 if (wPreferences.align_menus) {
2320 wMenuMove(menu->parent, x - MENUW(menu->parent), y, submenus);
2321 } else {
2322 wMenuMove(menu->parent, x - MENUW(menu->parent), menu->frame_y
2323 - menu->parent->entry_height*menu->parent->selected_entry,
2324 submenus);
2330 static void
2331 changeMenuLevels(WMenu *menu, int lower)
2333 int i;
2335 if (!lower) {
2336 ChangeStackingLevel(menu->frame->core, (!menu->parent ? WMMainMenuLevel
2337 : WMSubmenuLevel));
2338 wRaiseFrame(menu->frame->core);
2339 menu->flags.lowered = 0;
2340 } else {
2341 ChangeStackingLevel(menu->frame->core, WMNormalLevel);
2342 wLowerFrame(menu->frame->core);
2343 menu->flags.lowered = 1;
2345 for (i=0; i<menu->cascade_no; i++) {
2346 if (menu->cascades[i]
2347 && !menu->cascades[i]->flags.buttoned
2348 && menu->cascades[i]->flags.lowered!=lower) {
2349 changeMenuLevels(menu->cascades[i], lower);
2356 static void
2357 menuTitleDoubleClick(WCoreWindow *sender, void *data, XEvent *event)
2359 WMenu *menu = data;
2360 int lower;
2362 if (event->xbutton.state & MOD_MASK) {
2363 if (menu->flags.lowered) {
2364 lower = 0;
2365 } else {
2366 lower = 1;
2368 changeMenuLevels(menu, lower);
2373 static void
2374 menuTitleMouseDown(WCoreWindow *sender, void *data, XEvent *event)
2376 WMenu *menu = data;
2377 WMenu *tmp;
2378 XEvent ev;
2379 int x=menu->frame_x, y=menu->frame_y;
2380 int dx=event->xbutton.x_root, dy=event->xbutton.y_root;
2381 int i, lower;
2382 Bool started;
2384 /* can't touch the menu copy */
2385 if (menu->flags.brother)
2386 return;
2388 if (event->xbutton.button != Button1 && event->xbutton.button != Button2)
2389 return;
2391 if (event->xbutton.state & MOD_MASK) {
2392 wLowerFrame(menu->frame->core);
2393 lower = 1;
2394 } else {
2395 wRaiseFrame(menu->frame->core);
2396 lower = 0;
2398 tmp = menu;
2400 /* lower/raise all submenus */
2401 while (1) {
2402 if (tmp->selected_entry>=0 && tmp->cascades
2403 && tmp->entries[tmp->selected_entry]->cascade>=0) {
2404 tmp = tmp->cascades[tmp->entries[tmp->selected_entry]->cascade];
2405 if (!tmp || !tmp->flags.mapped)
2406 break;
2407 if (lower)
2408 wLowerFrame(tmp->frame->core);
2409 else
2410 wRaiseFrame(tmp->frame->core);
2411 } else {
2412 break;
2416 /* tear off the menu if it's a root menu or a cascade
2417 application menu */
2418 if (!menu->flags.buttoned && !menu->flags.brother
2419 && (!menu->flags.app_menu||menu->parent!=NULL)) {
2420 menu->flags.buttoned=1;
2421 wFrameWindowShowButton(menu->frame, WFF_RIGHT_BUTTON);
2422 if (menu->parent) {
2423 /* turn off selected menu entry in parent menu */
2424 selectEntry(menu->parent, -1);
2426 /* make parent map the copy in place of the original */
2427 for (i=0; i<menu->parent->cascade_no; i++) {
2428 if (menu->parent->cascades[i] == menu) {
2429 menu->parent->cascades[i] = menu->brother;
2430 break;
2436 started = False;
2437 while(1) {
2438 WMMaskEvent(dpy, ButtonMotionMask|ButtonReleaseMask|ButtonPressMask
2439 |ExposureMask, &ev);
2440 switch (ev.type) {
2441 case MotionNotify:
2442 if (started) {
2443 x += ev.xmotion.x_root - dx;
2444 y += ev.xmotion.y_root - dy;
2445 dx = ev.xmotion.x_root;
2446 dy = ev.xmotion.y_root;
2447 wMenuMove(menu, x, y, True);
2448 } else {
2449 if (abs(ev.xmotion.x_root - dx) > MOVE_THRESHOLD
2450 || abs(ev.xmotion.y_root - dy) > MOVE_THRESHOLD) {
2451 started = True;
2452 XGrabPointer(dpy, menu->frame->titlebar->window, False,
2453 ButtonMotionMask|ButtonReleaseMask
2454 |ButtonPressMask,
2455 GrabModeAsync, GrabModeAsync, None,
2456 wCursor[WCUR_MOVE], CurrentTime);
2459 break;
2461 case ButtonPress:
2462 break;
2464 case ButtonRelease:
2465 if (ev.xbutton.button != event->xbutton.button)
2466 break;
2467 XUngrabPointer(dpy, CurrentTime);
2468 return;
2470 default:
2471 WMHandleEvent(&ev);
2472 break;
2478 *----------------------------------------------------------------------
2479 * menuCloseClick--
2480 * Handles mouse click on the close button of menus. The menu is
2481 * closed when the button is clicked.
2483 * Side effects:
2484 * The closed menu is reinserted at it's parent menus
2485 * cascade list.
2486 *----------------------------------------------------------------------
2488 static void
2489 menuCloseClick(WCoreWindow *sender, void *data, XEvent *event)
2491 WMenu *menu = (WMenu*)data;
2492 WMenu *parent = menu->parent;
2493 int i;
2495 if (parent) {
2496 for (i=0; i<parent->cascade_no; i++) {
2497 /* find the entry that points to the copy */
2498 if (parent->cascades[i] == menu->brother) {
2499 /* make it point to the original */
2500 parent->cascades[i] = menu;
2501 menu->parent = parent;
2502 break;
2506 wMenuUnmap(menu);
2510 static void
2511 saveMenuInfo(proplist_t dict, WMenu *menu, proplist_t key)
2513 proplist_t value, list;
2514 char buffer[256];
2516 sprintf(buffer, "%i,%i", menu->frame_x, menu->frame_y);
2517 value = PLMakeString(buffer);
2518 list = PLMakeArrayFromElements(value, NULL);
2519 if (menu->flags.lowered)
2520 PLAppendArrayElement(list, PLMakeString("lowered"));
2521 PLInsertDictionaryEntry(dict, key, list);
2522 PLRelease(value);
2523 PLRelease(list);
2527 void
2528 wMenuSaveState(WScreen *scr)
2530 proplist_t menus, key;
2531 int save_menus = 0;
2533 menus = PLMakeDictionaryFromEntries(NULL, NULL, NULL);
2535 #ifndef LITE
2536 if (scr->switch_menu && scr->switch_menu->flags.buttoned) {
2537 key = PLMakeString("SwitchMenu");
2538 saveMenuInfo(menus, scr->switch_menu, key);
2539 PLRelease(key);
2540 save_menus = 1;
2543 if (saveMenuRecurs(menus, scr, scr->root_menu))
2544 save_menus = 1;
2546 #endif /* !LITE */
2547 if (scr->workspace_menu && scr->workspace_menu->flags.buttoned) {
2548 key = PLMakeString("WorkspaceMenu");
2549 saveMenuInfo(menus, scr->workspace_menu, key);
2550 PLRelease(key);
2551 save_menus = 1;
2554 if (save_menus) {
2555 key = PLMakeString("Menus");
2556 PLInsertDictionaryEntry(scr->session_state, key, menus);
2557 PLRelease(key);
2559 PLRelease(menus);
2563 #ifndef LITE
2565 static Bool
2566 getMenuPath(WMenu *menu, char *buffer, int bufSize)
2568 Bool ok = True;
2569 int len = 0;
2571 if (!menu->flags.titled || !menu->frame->title[0])
2572 return False;
2574 len = strlen(menu->frame->title);
2575 if (len >= bufSize)
2576 return False;
2578 if (menu->parent) {
2579 ok = getMenuPath(menu->parent, buffer, bufSize - len - 1);
2580 if (!ok)
2581 return False;
2584 strcat(buffer, "\\");
2585 strcat(buffer, menu->frame->title);
2587 return True;
2591 static Bool
2592 saveMenuRecurs(proplist_t menus, WScreen *scr, WMenu *menu)
2594 proplist_t key;
2595 int save_menus = 0, i;
2596 char buffer[512];
2597 Bool ok = True;
2600 if (menu->flags.brother)
2601 menu = menu->brother;
2603 if (menu->flags.buttoned && menu != scr->switch_menu) {
2605 buffer[0] = '\0';
2606 ok = getMenuPath(menu, buffer, 510);
2608 if (ok) {
2609 key = PLMakeString(buffer);
2610 saveMenuInfo(menus, menu, key);
2611 PLRelease(key);
2612 save_menus = 1;
2616 if (ok) {
2617 for (i = 0; i < menu->cascade_no; i++) {
2618 if (saveMenuRecurs(menus, scr, menu->cascades[i]))
2619 save_menus = 1;
2622 return save_menus;
2624 #endif /* !LITE */
2627 #define COMPLAIN(key) wwarning(_("bad value in menus state info:%s"), key)
2630 static Bool
2631 getMenuInfo(proplist_t info, int *x, int *y, Bool *lowered)
2633 proplist_t pos;
2635 *lowered = False;
2637 if (PLIsArray(info)) {
2638 proplist_t flags;
2639 pos = PLGetArrayElement(info, 0);
2640 flags = PLGetArrayElement(info, 1);
2641 if (flags != NULL && PLIsString(flags) && PLGetString(flags) != NULL
2642 && strcmp(PLGetString(flags), "lowered") == 0) {
2643 *lowered = True;
2645 } else {
2646 pos = info;
2649 if (pos != NULL && PLIsString(pos)) {
2650 if (sscanf(PLGetString(pos), "%i,%i", x, y)!=2)
2651 COMPLAIN("Position");
2652 } else {
2653 COMPLAIN("(position, flags...)");
2654 return False;
2657 return True;
2661 static int
2662 restoreMenu(WScreen *scr, proplist_t menu, int which)
2664 int x, y;
2665 Bool lowered = False;
2666 WMenu *pmenu = NULL;
2668 if (!menu)
2669 return False;
2671 if (!getMenuInfo(menu, &x, &y, &lowered))
2672 return False;
2675 #ifndef LITE
2676 if (which & WSS_SWITCHMENU) {
2677 OpenSwitchMenu(scr, x, y, False);
2678 pmenu = scr->switch_menu;
2680 #endif /* !LITE */
2682 if (pmenu) {
2683 int width = MENUW(pmenu);
2684 int height = MENUH(pmenu);
2686 if (lowered) {
2687 changeMenuLevels(pmenu, True);
2690 x = (x < -width) ? 0 : x;
2691 x = (x > scr->scr_width) ? scr->scr_width - width : x;
2692 y = (y < 0) ? 0 : y;
2693 y = (y > scr->scr_height) ? scr->scr_height - height : y;
2694 wMenuMove(pmenu, x, y, True);
2695 pmenu->flags.buttoned = 1;
2696 wFrameWindowShowButton(pmenu->frame, WFF_RIGHT_BUTTON);
2697 return True;
2699 return False;
2703 #ifndef LITE
2704 static int
2705 restoreMenuRecurs(WScreen *scr, proplist_t menus, WMenu *menu, char *path)
2707 proplist_t key, entry;
2708 char buffer[512];
2709 int i, x, y, res;
2710 Bool lowered;
2712 if (strlen(path) + strlen(menu->frame->title) > 510)
2713 return False;
2715 sprintf(buffer, "%s\\%s", path, menu->frame->title);
2716 key = PLMakeString(buffer);
2717 entry = PLGetDictionaryEntry(menus, key);
2718 res = False;
2720 if (entry && getMenuInfo(entry, &x, &y, &lowered)) {
2722 if (!menu->flags.mapped) {
2723 int width = MENUW(menu);
2724 int height = MENUH(menu);
2726 wMenuMapAt(menu, x, y, False);
2728 if (menu->parent) {
2729 /* make parent map the copy in place of the original */
2730 for (i=0; i<menu->parent->cascade_no; i++) {
2731 if (menu->parent->cascades[i] == menu) {
2732 menu->parent->cascades[i] = menu->brother;
2733 break;
2737 if (lowered) {
2738 changeMenuLevels(menu, True);
2740 x = (x < -width) ? 0 : x;
2741 x = (x > scr->scr_width) ? scr->scr_width - width : x;
2742 y = (y < 0) ? 0 : y;
2743 y = (y > scr->scr_height) ? scr->scr_height - height : y;
2744 wMenuMove(menu, x, y, True);
2745 menu->flags.buttoned = 1;
2746 wFrameWindowShowButton(menu->frame, WFF_RIGHT_BUTTON);
2747 res = True;
2751 PLRelease(key);
2753 for (i=0; i<menu->cascade_no; i++) {
2754 if (restoreMenuRecurs(scr, menus, menu->cascades[i], buffer) != False)
2755 res = True;
2758 return res;
2760 #endif /* !LITE */
2763 void
2764 wMenuRestoreState(WScreen *scr)
2766 proplist_t menus, menu, key, skey;
2768 key = PLMakeString("Menus");
2769 menus = PLGetDictionaryEntry(scr->session_state, key);
2770 PLRelease(key);
2772 if (!menus)
2773 return;
2775 /* restore menus */
2777 skey = PLMakeString("SwitchMenu");
2778 menu = PLGetDictionaryEntry(menus, skey);
2779 PLRelease(skey);
2780 restoreMenu(scr, menu, WSS_SWITCHMENU);
2782 #ifndef LITE
2783 if (!scr->root_menu) {
2784 OpenRootMenu(scr, scr->scr_width*2, 0, False);
2785 wMenuUnmap(scr->root_menu);
2787 restoreMenuRecurs(scr, menus, scr->root_menu, "");
2788 #endif /* !LITE */
2792 void
2793 OpenWorkspaceMenu(WScreen *scr, int x, int y)
2795 WMenu *menu, *parent;
2796 WMenuEntry *entry;
2798 #ifndef LITE
2799 if (!scr->root_menu) {
2800 OpenRootMenu(scr, scr->scr_width*2, 0, False);
2801 wMenuUnmap(scr->root_menu);
2803 #endif
2804 menu = scr->workspace_menu;
2805 if (menu) {
2806 if (menu->flags.mapped) {
2807 if (!menu->flags.buttoned) {
2808 wMenuUnmap(menu);
2809 parent = menu->parent;
2810 if (parent && parent->selected_entry >= 0) {
2811 entry = parent->entries[parent->selected_entry];
2812 if (parent->cascades[entry->cascade] == menu) {
2813 selectEntry(parent, -1);
2814 wMenuMapAt(menu, x, y, False);
2817 } else {
2818 wRaiseFrame(menu->frame->core);
2819 wMenuMapCopyAt(menu, x, y);
2821 } else {
2822 wMenuMapAt(menu, x, y, False);