pass texture to plugin so it doesn't have to grab from title bar.
[wmaker-crm.git] / src / menu.c
blobc96545127d6b5b656825a3ee0710a48471b3d4eb
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 if (scr->menu_item_texture->any.type == WTEX_SOLID) {
763 XClearArea(dpy, win, 0, y + 1, w - 1, h - 3, False);
764 /* draw the frame */
765 drawFrame(scr, win, y, w, h, type);
766 } else {
767 XClearArea(dpy, win, 0, y, w, h, False);
771 if (selected) {
772 textGC = scr->select_menu_gc;
773 if (entry->flags.enabled)
774 XSetForeground(dpy, textGC, scr->select_text_pixel);
775 else
776 XSetForeground(dpy, textGC, scr->dtext_pixel);
777 } else if (!entry->flags.enabled) {
778 textGC = scr->disabled_menu_entry_gc;
779 } else {
780 textGC = scr->menu_entry_gc;
782 /* draw text */
783 x = 5;
784 if (entry->flags.indicator)
785 x += MENU_INDICATOR_SPACE + 2;
787 #ifdef DRAWSTRING_PLUGIN
788 if (scr->drawstring_func[W_STRING_MTEXT]) {
789 Pixmap tmp_bg;
790 void **p;
791 tmp_bg = XCreatePixmap(dpy, win, w, menu->entry_height, DefaultDepth(dpy, DefaultScreen(dpy)));
792 XCopyArea(dpy, win, tmp_bg, textGC, 0, y, w, menu->entry_height, 0, 0);
793 p = wPluginPackData(4,
794 textGC,
795 scr->menu_entry_font,
796 scr->drawstring_func[W_STRING_MTEXT]->data,
797 /*menu->menu_texture_data,*/
798 tmp_bg,
799 "extendable");
800 scr->drawstring_func[W_STRING_MTEXT]->proc.drawString(
801 scr->drawstring_func[W_STRING_MTEXT]->arg,
802 win,
803 x, y,
804 menu->frame->titlebar->width, menu->entry_height,
805 entry->text, p);
806 XFreePixmap(dpy, tmp_bg);
807 free(p);
808 #undef DRAWSTRING_CURRENT_STATE
809 } else {
810 WMDrawString(scr->wmscreen, win, textGC, scr->menu_entry_font,
811 x, 3 + y + wPreferences.menu_text_clearance, entry->text, strlen(entry->text));
814 #else
815 WMDrawString(scr->wmscreen, win, textGC, scr->menu_entry_font,
816 x, 3 + y + wPreferences.menu_text_clearance, entry->text, strlen(entry->text));
817 #endif
819 if (entry->cascade>=0) {
820 /* draw the cascade indicator */
821 XDrawLine(dpy,win,dim, w-11, y+6, w-6, y+h/2-1);
822 XDrawLine(dpy,win,light, w-11, y+h-8, w-6, y+h/2-1);
823 XDrawLine(dpy,win,dark, w-12, y+6, w-12, y+h-8);
826 /* draw indicator */
827 if (entry->flags.indicator && entry->flags.indicator_on) {
828 int iw, ih;
829 WPixmap *indicator;
832 switch (entry->flags.indicator_type) {
833 case MI_CHECK:
834 indicator = scr->menu_check_indicator;
835 break;
836 case MI_MINIWINDOW:
837 indicator = scr->menu_mini_indicator;
838 break;
839 case MI_HIDDEN:
840 indicator = scr->menu_hide_indicator;
841 break;
842 case MI_SHADED:
843 indicator = scr->menu_shade_indicator;
844 break;
845 case MI_DIAMOND:
846 default:
847 indicator = scr->menu_radio_indicator;
848 break;
851 iw = indicator->width;
852 ih = indicator->height;
853 XSetClipMask(dpy, scr->copy_gc, indicator->mask);
854 XSetClipOrigin(dpy, scr->copy_gc, 5, y+(h-ih)/2);
855 if (selected)
856 XSetForeground(dpy, scr->copy_gc, scr->black_pixel);
857 else
858 XSetForeground(dpy, scr->copy_gc, scr->mtext_pixel);
859 XFillRectangle(dpy, win, scr->copy_gc, 5, y+(h-ih)/2, iw, ih);
861 XCopyArea(dpy, indicator->image, win, scr->copy_gc, 0, 0,
862 iw, ih, 5, y+(h-ih)/2);
864 XSetClipOrigin(dpy, scr->copy_gc, 0, 0);
867 /* draw right text */
869 if (entry->rtext && entry->cascade<0) {
870 tw = WMWidthOfString(scr->menu_entry_font, entry->rtext,
871 strlen(entry->rtext));
873 WMDrawString(scr->wmscreen, win, textGC, scr->menu_entry_font, w-6-tw,
874 y + 3 + wPreferences.menu_text_clearance, entry->rtext, strlen(entry->rtext));
879 static void
880 move_menus(WMenu *menu, int x, int y)
882 while (menu->parent) {
883 menu = menu->parent;
884 x -= MENUW(menu);
885 if (!wPreferences.align_menus && menu->selected_entry>=0) {
886 y -= menu->selected_entry*menu->entry_height;
889 wMenuMove(menu, x, y, True);
892 static void
893 makeVisible(WMenu *menu)
895 WScreen *scr = menu->frame->screen_ptr;
896 int x1, y1, x2, y2, new_x, new_y, move;
898 if (menu->entry_no<0) return;
900 x1 = menu->frame_x;
901 y1 = menu->frame_y+menu->frame->top_width
902 + menu->selected_entry*menu->entry_height;
903 x2 = x1 + MENUW(menu);
904 y2 = y1 + menu->entry_height;
906 new_x = x1;
907 new_y = y1;
908 move = 0;
910 if (x1 < 0) {
911 new_x = 0;
912 move = 1;
913 } else if (x2 >= scr->scr_width) {
914 new_x = scr->scr_width - MENUW(menu) - 1;
915 move = 1;
918 if (y1 < 0) {
919 new_y = 0;
920 move = 1;
921 } else if (y2 >= scr->scr_height) {
922 new_y = scr->scr_height - menu->entry_height - 1;
923 move = 1;
926 new_y = new_y - menu->frame->top_width
927 - menu->selected_entry*menu->entry_height;
928 move_menus(menu, new_x, new_y);
932 static int
933 check_key(WMenu *menu, XKeyEvent *event)
935 int i, ch, s;
936 char buffer[32];
938 if (XLookupString(event, buffer, 32, NULL, NULL)<1)
939 return -1;
941 ch = toupper(buffer[0]);
943 s = (menu->selected_entry>=0 ? menu->selected_entry+1 : 0);
945 again:
946 for (i=s; i<menu->entry_no; i++) {
947 if (ch==toupper(menu->entries[i]->text[0])) {
948 return i;
951 /* no match. Retry from start, if previous started from a selected entry */
952 if (s!=0) {
953 s = 0;
954 goto again;
956 return -1;
960 static int
961 keyboardMenu(WMenu *menu)
963 XEvent event;
964 KeySym ksym=NoSymbol;
965 int done=0;
966 int index;
967 WMenuEntry *entry;
968 int old_pos_x = menu->frame_x;
969 int old_pos_y = menu->frame_y;
970 int new_x = old_pos_x, new_y = old_pos_y;
971 int scr_width = menu->frame->screen_ptr->scr_width;
972 int scr_height = menu->frame->screen_ptr->scr_height;
974 if (menu->flags.editing)
975 return False;
978 XGrabKeyboard(dpy, menu->frame->core->window, True, GrabModeAsync,
979 GrabModeAsync, CurrentTime);
981 if (menu->frame_y+menu->frame->top_width >= scr_height)
982 new_y = scr_height - menu->frame->top_width;
984 if (menu->frame_x+MENUW(menu) >= scr_width)
985 new_x = scr_width-MENUW(menu)-1;
987 move_menus(menu, new_x, new_y);
989 while (!done && menu->flags.mapped) {
990 XAllowEvents(dpy, AsyncKeyboard, CurrentTime);
991 WMMaskEvent(dpy, ExposureMask|ButtonMotionMask|ButtonPressMask
992 |ButtonReleaseMask|KeyPressMask|KeyReleaseMask
993 |SubstructureNotifyMask, &event);
995 switch (event.type) {
996 case KeyPress:
997 ksym = XLookupKeysym(&event.xkey, 0);
998 switch (ksym) {
999 case XK_Escape:
1000 done = 1;
1001 break;
1003 case XK_Home:
1004 #ifdef XK_KP_Home
1005 case XK_KP_Home:
1006 #endif
1007 selectEntry(menu, 0);
1008 makeVisible(menu);
1009 break;
1011 case XK_End:
1012 #ifdef XK_KP_End
1013 case XK_KP_End:
1014 #endif
1015 selectEntry(menu, menu->entry_no-1);
1016 makeVisible(menu);
1017 break;
1019 case XK_Up:
1020 #ifdef ARROWLESS_KBD
1021 case XK_k:
1022 #endif
1023 #ifdef XK_KP_Up
1024 case XK_KP_Up:
1025 #endif
1026 if (menu->selected_entry <= 0)
1027 selectEntry(menu, menu->entry_no-1);
1028 else
1029 selectEntry(menu, menu->selected_entry-1);
1030 makeVisible(menu);
1031 break;
1033 case XK_Down:
1034 #ifdef ARROWLESS_KBD
1035 case XK_j:
1036 #endif
1037 #ifdef XK_KP_Down
1038 case XK_KP_Down:
1039 #endif
1040 if (menu->selected_entry<0)
1041 selectEntry(menu, 0);
1042 else if (menu->selected_entry == menu->entry_no-1)
1043 selectEntry(menu, 0);
1044 else if (menu->selected_entry < menu->entry_no-1)
1045 selectEntry(menu, menu->selected_entry+1);
1046 makeVisible(menu);
1047 break;
1049 case XK_Right:
1050 #ifdef ARROWLESS_KBD
1051 case XK_l:
1052 #endif
1053 #ifdef XK_KP_Right
1054 case XK_KP_Right:
1055 #endif
1056 if (menu->selected_entry>=0) {
1057 WMenuEntry *entry;
1058 entry = menu->entries[menu->selected_entry];
1060 if (entry->cascade >= 0 && menu->cascades
1061 && menu->cascades[entry->cascade]->entry_no > 0) {
1063 XUngrabKeyboard(dpy, CurrentTime);
1065 selectEntry(menu->cascades[entry->cascade], 0);
1066 if (!keyboardMenu(menu->cascades[entry->cascade]))
1067 done = 1;
1069 XGrabKeyboard(dpy, menu->frame->core->window, True,
1070 GrabModeAsync, GrabModeAsync,
1071 CurrentTime);
1074 break;
1076 case XK_Left:
1077 #ifdef ARROWLESS_KBD
1078 case XK_h:
1079 #endif
1080 #ifdef XK_KP_Left
1081 case XK_KP_Left:
1082 #endif
1083 if (menu->parent!=NULL && menu->parent->selected_entry>=0) {
1084 selectEntry(menu, -1);
1085 move_menus(menu, old_pos_x, old_pos_y);
1086 return True;
1088 break;
1090 case XK_Return:
1091 done = 2;
1092 break;
1094 default:
1095 index = check_key(menu, &event.xkey);
1096 if (index>=0) {
1097 selectEntry(menu, index);
1100 break;
1102 default:
1103 if (event.type==ButtonPress)
1104 done = 1;
1106 WMHandleEvent(&event);
1110 XUngrabKeyboard(dpy, CurrentTime);
1112 if (done==2 && menu->selected_entry>=0) {
1113 entry = menu->entries[menu->selected_entry];
1114 } else {
1115 entry = NULL;
1118 if (entry && entry->callback!=NULL && entry->flags.enabled
1119 && entry->cascade < 0) {
1120 #if (MENU_BLINK_COUNT > 0)
1121 int sel = menu->selected_entry;
1122 int i;
1124 for (i=0; i<MENU_BLINK_COUNT; i++) {
1125 paintEntry(menu, sel, False);
1126 XSync(dpy, 0);
1127 wusleep(MENU_BLINK_DELAY);
1128 paintEntry(menu, sel, True);
1129 XSync(dpy, 0);
1130 wusleep(MENU_BLINK_DELAY);
1132 #endif
1133 selectEntry(menu, -1);
1135 if (!menu->flags.buttoned) {
1136 wMenuUnmap(menu);
1137 move_menus(menu, old_pos_x, old_pos_y);
1139 closeCascade(menu);
1141 (*entry->callback)(menu, entry);
1142 } else {
1143 if (!menu->flags.buttoned) {
1144 wMenuUnmap(menu);
1145 move_menus(menu, old_pos_x, old_pos_y);
1147 selectEntry(menu, -1);
1151 /* returns True if returning from a submenu to a parent menu,
1152 * False if exiting from menu */
1153 return False;
1157 void
1158 wMenuMapAt(WMenu *menu, int x, int y, int keyboard)
1160 int scr_width = menu->frame->screen_ptr->scr_width;
1161 int scr_height = menu->frame->screen_ptr->scr_height;
1163 if (!menu->flags.realized) {
1164 menu->flags.realized=1;
1165 wMenuRealize(menu);
1167 if (!menu->flags.mapped) {
1168 if (wPreferences.wrap_menus) {
1169 if (x<0) x = 0;
1170 if (y<0) y = 0;
1171 if (x+MENUW(menu) > scr_width)
1172 x = scr_width - MENUW(menu);
1173 if (y+MENUH(menu) > scr_height)
1174 y = scr_height - MENUH(menu);
1177 XMoveWindow(dpy, menu->frame->core->window, x, y);
1178 menu->frame_x = x;
1179 menu->frame_y = y;
1180 XMapWindow(dpy, menu->frame->core->window);
1181 wRaiseFrame(menu->frame->core);
1182 menu->flags.mapped = 1;
1183 } else {
1184 selectEntry(menu, 0);
1187 if (keyboard)
1188 keyboardMenu(menu);
1192 void
1193 wMenuMap(WMenu *menu)
1195 if (!menu->flags.realized) {
1196 menu->flags.realized=1;
1197 wMenuRealize(menu);
1199 if (menu->flags.app_menu && menu->parent==NULL) {
1200 menu->frame_x = menu->frame->screen_ptr->app_menu_x;
1201 menu->frame_y = menu->frame->screen_ptr->app_menu_y;
1202 XMoveWindow(dpy, menu->frame->core->window, menu->frame_x, menu->frame_y);
1204 XMapWindow(dpy, menu->frame->core->window);
1205 wRaiseFrame(menu->frame->core);
1206 menu->flags.mapped = 1;
1210 void
1211 wMenuUnmap(WMenu *menu)
1213 int i;
1215 XUnmapWindow(dpy, menu->frame->core->window);
1216 if (menu->flags.titled && menu->flags.buttoned) {
1217 wFrameWindowHideButton(menu->frame, WFF_RIGHT_BUTTON);
1219 menu->flags.buttoned = 0;
1220 menu->flags.mapped = 0;
1221 menu->flags.open_to_left = 0;
1223 for (i=0; i<menu->cascade_no; i++) {
1224 if (menu->cascades[i]!=NULL
1225 && menu->cascades[i]->flags.mapped
1226 && !menu->cascades[i]->flags.buttoned) {
1228 wMenuUnmap(menu->cascades[i]);
1231 menu->selected_entry = -1;
1236 void
1237 wMenuPaint(WMenu *menu)
1239 int i;
1241 if (!menu->flags.mapped) {
1242 return;
1245 /* paint entries */
1246 for (i=0; i<menu->entry_no; i++) {
1247 paintEntry(menu, i, i==menu->selected_entry);
1252 void
1253 wMenuSetEnabled(WMenu *menu, int index, int enable)
1255 if (index>=menu->entry_no) return;
1256 menu->entries[index]->flags.enabled=enable;
1257 paintEntry(menu, index, index==menu->selected_entry);
1258 paintEntry(menu->brother, index, index==menu->selected_entry);
1262 /* ====================================================================== */
1265 static void
1266 editEntry(WMenu *menu, WMenuEntry *entry)
1268 WTextInput *text;
1269 XEvent event;
1270 WObjDescriptor *desc;
1271 char *t;
1272 int done = 0;
1273 Window old_focus;
1274 int old_revert;
1276 menu->flags.editing = 1;
1278 text = wTextCreate(menu->menu, 1, menu->entry_height * entry->order,
1279 menu->menu->width - 2, menu->entry_height - 1);
1281 wTextPutText(text, entry->text);
1282 XGetInputFocus(dpy, &old_focus, &old_revert);
1283 XSetInputFocus(dpy, text->core->window, RevertToNone, CurrentTime);
1285 if (XGrabKeyboard(dpy, text->core->window, True, GrabModeAsync,
1286 GrabModeAsync, CurrentTime)!=GrabSuccess) {
1287 wwarning("could not grab keyboard");
1288 wTextDestroy(text);
1290 wSetFocusTo(menu->frame->screen_ptr,
1291 menu->frame->screen_ptr->focused_window);
1292 return;
1296 while (!done && !text->done) {
1297 XSync(dpy, 0);
1298 XAllowEvents(dpy, AsyncKeyboard|AsyncPointer, CurrentTime);
1299 XSync(dpy, 0);
1300 WMNextEvent(dpy, &event);
1302 if (XFindContext(dpy, event.xany.window, wWinContext,
1303 (XPointer *)&desc)==XCNOENT)
1304 desc = NULL;
1306 if ((desc != NULL) && (desc->handle_anything != NULL)) {
1308 (*desc->handle_anything)(desc, &event);
1310 } else {
1311 switch (event.type) {
1312 case ButtonPress:
1313 XAllowEvents(dpy, ReplayPointer, CurrentTime);
1314 done = 1;
1316 default:
1317 WMHandleEvent(&event);
1318 break;
1323 XSetInputFocus(dpy, old_focus, old_revert, CurrentTime);
1325 wSetFocusTo(menu->frame->screen_ptr,
1326 menu->frame->screen_ptr->focused_window);
1329 t = wTextGetText(text);
1330 /* if !t, the user has canceled editing */
1331 if (t) {
1332 if (entry->text)
1333 wfree(entry->text);
1334 entry->text = wstrdup(t);
1336 menu->flags.realized = 0;
1338 wTextDestroy(text);
1340 XUngrabKeyboard(dpy, CurrentTime);
1342 if (t && menu->on_edit)
1343 (*menu->on_edit)(menu, entry);
1345 menu->flags.editing = 0;
1347 if (!menu->flags.realized)
1348 wMenuRealize(menu);
1352 static void
1353 selectEntry(WMenu *menu, int entry_no)
1355 WMenuEntry *entry;
1356 WMenu *submenu;
1357 int old_entry;
1359 if (menu->entries==NULL)
1360 return;
1362 if (entry_no >= menu->entry_no)
1363 return;
1365 old_entry = menu->selected_entry;
1366 menu->selected_entry = entry_no;
1368 if (old_entry!=entry_no) {
1370 /* unselect previous entry */
1371 if (old_entry>=0) {
1372 paintEntry(menu, old_entry, False);
1373 entry = menu->entries[old_entry];
1375 /* unmap cascade */
1376 if (entry->cascade>=0 && menu->cascades) {
1377 if (!menu->cascades[entry->cascade]->flags.buttoned) {
1378 wMenuUnmap(menu->cascades[entry->cascade]);
1383 if (entry_no<0) {
1384 menu->selected_entry = -1;
1385 return;
1387 entry = menu->entries[entry_no];
1389 if (entry->cascade>=0 && menu->cascades && entry->flags.enabled) {
1390 /* Callback for when the submenu is opened.
1392 submenu = menu->cascades[entry->cascade];
1393 if (submenu && submenu->flags.brother)
1394 submenu = submenu->brother;
1396 if (entry->callback) {
1397 /* Only call the callback if the submenu is not yet mapped.
1399 if (menu->flags.brother) {
1400 if (!submenu || !submenu->flags.mapped)
1401 (*entry->callback)(menu->brother, entry);
1402 } else {
1403 if (!submenu || !submenu->flags.buttoned)
1404 (*entry->callback)(menu, entry);
1408 /* the submenu menu might have changed */
1409 submenu = menu->cascades[entry->cascade];
1411 /* map cascade */
1412 if (!submenu->flags.mapped) {
1413 int x, y;
1415 if (!submenu->flags.realized)
1416 wMenuRealize(submenu);
1417 if (wPreferences.wrap_menus) {
1418 if (menu->flags.open_to_left)
1419 submenu->flags.open_to_left = 1;
1421 if (submenu->flags.open_to_left) {
1422 x = menu->frame_x - MENUW(submenu);
1423 if (x<0) {
1424 x = 0;
1425 submenu->flags.open_to_left = 0;
1427 } else {
1428 x = menu->frame_x + MENUW(menu);
1430 if (x + MENUW(submenu)
1431 >= menu->frame->screen_ptr->scr_width) {
1433 x = menu->frame_x - MENUW(submenu);
1434 submenu->flags.open_to_left = 1;
1437 } else {
1438 x = menu->frame_x + MENUW(menu);
1441 if (wPreferences.align_menus) {
1442 y = menu->frame_y;
1443 } else {
1444 y = menu->frame_y + menu->entry_height*entry_no;
1445 if (menu->flags.titled)
1446 y += menu->frame->top_width;
1447 if (menu->cascades[entry->cascade]->flags.titled)
1448 y -= menu->cascades[entry->cascade]->frame->top_width;
1451 wMenuMapAt(menu->cascades[entry->cascade], x, y, False);
1452 menu->cascades[entry->cascade]->parent = menu;
1453 } else {
1454 return;
1457 paintEntry(menu, entry_no, True);
1462 static WMenu*
1463 findMenu(WScreen *scr, int *x_ret, int *y_ret)
1465 WMenu *menu;
1466 WObjDescriptor *desc;
1467 Window root_ret, win, junk_win;
1468 int x, y, wx, wy;
1469 unsigned int mask;
1471 XQueryPointer(dpy, scr->root_win, &root_ret, &win, &x, &y, &wx, &wy,
1472 &mask);
1474 if (win==None) return NULL;
1476 if (XFindContext(dpy, win, wWinContext, (XPointer *)&desc)==XCNOENT)
1477 return NULL;
1479 if (desc->parent_type == WCLASS_MENU) {
1480 menu = (WMenu*)desc->parent;
1481 XTranslateCoordinates(dpy, root_ret, menu->menu->window, wx, wy,
1482 x_ret, y_ret, &junk_win);
1483 return menu;
1485 return NULL;
1491 static void
1492 closeCascade(WMenu *menu)
1494 WMenu *parent=menu->parent;
1496 if (menu->flags.brother
1497 || (!menu->flags.buttoned
1498 && (!menu->flags.app_menu||menu->parent!=NULL))) {
1500 selectEntry(menu, -1);
1501 XSync(dpy, 0);
1502 #if (MENU_BLINK_DELAY > 2)
1503 wusleep(MENU_BLINK_DELAY/2);
1504 #endif
1505 wMenuUnmap(menu);
1506 while (parent!=NULL
1507 && (parent->parent!=NULL || !parent->flags.app_menu
1508 || parent->flags.brother)
1509 && !parent->flags.buttoned) {
1510 selectEntry(parent, -1);
1511 wMenuUnmap(parent);
1512 parent = parent->parent;
1514 if (parent)
1515 selectEntry(parent, -1);
1520 static void
1521 closeBrotherCascadesOf(WMenu *menu)
1523 WMenu *tmp;
1524 int i;
1526 for (i=0; i<menu->cascade_no; i++) {
1527 if (menu->cascades[i]->flags.brother) {
1528 tmp = menu->cascades[i];
1529 } else {
1530 tmp = menu->cascades[i]->brother;
1532 if (tmp->flags.mapped) {
1533 selectEntry(tmp->parent, -1);
1534 closeBrotherCascadesOf(tmp);
1535 break;
1541 #define getEntryAt(menu, x, y) ((y)<0 ? -1 : (y)/(menu->entry_height))
1544 static WMenu*
1545 parentMenu(WMenu *menu)
1547 WMenu *parent;
1548 WMenuEntry *entry;
1550 if (menu->flags.buttoned)
1551 return menu;
1553 while (menu->parent && menu->parent->flags.mapped) {
1554 parent = menu->parent;
1555 if (parent->selected_entry < 0)
1556 break;
1557 entry = parent->entries[parent->selected_entry];
1558 if (!entry->flags.enabled || entry->cascade<0 || !parent->cascades ||
1559 parent->cascades[entry->cascade] != menu)
1560 break;
1561 menu = parent;
1562 if (menu->flags.buttoned)
1563 break;
1566 return menu;
1572 * Will raise the passed menu, if submenu = 0
1573 * If submenu > 0 will also raise all mapped submenus
1574 * until the first buttoned one
1575 * If submenu < 0 will also raise all mapped parent menus
1576 * until the first buttoned one
1579 static void
1580 raiseMenus(WMenu *menu, int submenus)
1582 WMenu *submenu;
1583 int i;
1585 if(!menu) return;
1587 wRaiseFrame(menu->frame->core);
1589 if (submenus>0 && menu->selected_entry>=0) {
1590 i = menu->entries[menu->selected_entry]->cascade;
1591 if (i>=0 && menu->cascades) {
1592 submenu = menu->cascades[i];
1593 if (submenu->flags.mapped && !submenu->flags.buttoned)
1594 raiseMenus(submenu, submenus);
1597 if (submenus<0 && !menu->flags.buttoned &&
1598 menu->parent && menu->parent->flags.mapped)
1599 raiseMenus(menu->parent, submenus);
1603 WMenu*
1604 wMenuUnderPointer(WScreen *screen)
1606 WObjDescriptor *desc;
1607 Window root_ret, win;
1608 int dummy;
1609 unsigned int mask;
1611 XQueryPointer(dpy, screen->root_win, &root_ret, &win, &dummy, &dummy,
1612 &dummy, &dummy, &mask);
1614 if (win==None) return NULL;
1616 if (XFindContext(dpy, win, wWinContext, (XPointer *)&desc)==XCNOENT)
1617 return NULL;
1619 if (desc->parent_type == WCLASS_MENU)
1620 return (WMenu *)desc->parent;
1621 return NULL;
1627 static void
1628 getPointerPosition(WScreen *scr, int *x, int *y)
1630 Window root_ret, win;
1631 int wx, wy;
1632 unsigned int mask;
1634 XQueryPointer(dpy, scr->root_win, &root_ret, &win, x, y, &wx, &wy, &mask);
1638 static void
1639 getScrollAmount(WMenu *menu, int *hamount, int *vamount)
1641 WScreen *scr = menu->menu->screen_ptr;
1642 int menuX1 = menu->frame_x;
1643 int menuY1 = menu->frame_y;
1644 int menuX2 = menu->frame_x + MENUW(menu);
1645 int menuY2 = menu->frame_y + MENUH(menu);
1646 int screenW = scr->scr_width;
1647 int screenH = scr->scr_height;
1648 int xroot, yroot;
1650 *hamount = 0;
1651 *vamount = 0;
1653 getPointerPosition(scr, &xroot, &yroot);
1655 #ifdef VIRTUAL_DESKTOP
1656 if (wPreferences.vedge_thickness) {
1657 if (xroot <= wPreferences.vedge_thickness + 1 && menuX1 < wPreferences.vedge_thickness) {
1658 /* scroll to the right */
1659 *hamount = WMIN(MENU_SCROLL_STEP, abs(menuX1));
1661 } else if (xroot >= screenW-2-wPreferences.vedge_thickness && menuX2 > screenW-1-wPreferences.vedge_thickness) {
1662 /* scroll to the left */
1663 *hamount = WMIN(MENU_SCROLL_STEP, abs(menuX2-screenW-1));
1665 if (*hamount==0)
1666 *hamount = 1;
1668 *hamount = -*hamount;
1670 } else
1671 #endif
1673 if (xroot <= 1 && menuX1 < 0) {
1674 /* scroll to the right */
1675 *hamount = WMIN(MENU_SCROLL_STEP, abs(menuX1));
1677 } else if (xroot >= screenW-2 && menuX2 > screenW-1) {
1678 /* scroll to the left */
1679 *hamount = WMIN(MENU_SCROLL_STEP, abs(menuX2-screenW-1));
1681 if (*hamount==0)
1682 *hamount = 1;
1684 *hamount = -*hamount;
1687 if (yroot <= 1 && menuY1 < 0) {
1688 /* scroll down */
1689 *vamount = WMIN(MENU_SCROLL_STEP, abs(menuY1));
1691 } else if (yroot >= screenH-2 && menuY2 > screenH-1) {
1692 /* scroll up */
1693 *vamount = WMIN(MENU_SCROLL_STEP, abs(menuY2-screenH-2));
1695 *vamount = -*vamount;
1700 static void
1701 dragScrollMenuCallback(void *data)
1703 WMenu *menu = (WMenu*)data;
1704 WScreen *scr = menu->menu->screen_ptr;
1705 WMenu *parent = parentMenu(menu);
1706 int hamount, vamount;
1707 int x, y;
1708 int newSelectedEntry;
1710 getScrollAmount(menu, &hamount, &vamount);
1713 if (hamount != 0 || vamount != 0) {
1714 wMenuMove(parent, parent->frame_x + hamount,
1715 parent->frame_y + vamount, True);
1716 if (findMenu(scr, &x, &y)) {
1717 newSelectedEntry = getEntryAt(menu, x, y);
1718 selectEntry(menu, newSelectedEntry);
1719 } else {
1720 /* Pointer fell outside of menu. If the selected entry is
1721 * not a submenu, unselect it */
1722 if (menu->selected_entry >= 0
1723 && menu->entries[menu->selected_entry]->cascade<0)
1724 selectEntry(menu, -1);
1725 newSelectedEntry = 0;
1728 /* paranoid check */
1729 if (newSelectedEntry >= 0) {
1730 /* keep scrolling */
1731 menu->timer = WMAddTimerHandler(MENU_SCROLL_DELAY,
1732 dragScrollMenuCallback, menu);
1733 } else {
1734 menu->timer = NULL;
1736 } else {
1737 /* don't need to scroll anymore */
1738 menu->timer = NULL;
1739 if (findMenu(scr, &x, &y)) {
1740 newSelectedEntry = getEntryAt(menu, x, y);
1741 selectEntry(menu, newSelectedEntry);
1747 static void
1748 scrollMenuCallback(void *data)
1750 WMenu *menu = (WMenu*)data;
1751 WMenu *parent = parentMenu(menu);
1752 int hamount = 0; /* amount to scroll */
1753 int vamount = 0;
1755 getScrollAmount(menu, &hamount, &vamount);
1757 if (hamount != 0 || vamount != 0) {
1758 wMenuMove(parent, parent->frame_x + hamount,
1759 parent->frame_y + vamount, True);
1761 /* keep scrolling */
1762 menu->timer = WMAddTimerHandler(MENU_SCROLL_DELAY,
1763 scrollMenuCallback, menu);
1764 } else {
1765 /* don't need to scroll anymore */
1766 menu->timer = NULL;
1772 #define MENU_SCROLL_BORDER 5
1774 static int
1775 isPointNearBoder(WMenu *menu, int x, int y)
1777 int menuX1 = menu->frame_x;
1778 int menuY1 = menu->frame_y;
1779 int menuX2 = menu->frame_x + MENUW(menu);
1780 int menuY2 = menu->frame_y + MENUH(menu);
1781 int scrXe = menu->menu->screen_ptr->scr_width-1;
1782 int scrYe = menu->menu->screen_ptr->scr_height-1;
1783 int flag = 0;
1785 if (x >= menuX1 && x <= menuX2 && (y < MENU_SCROLL_BORDER
1786 || y > scrYe-MENU_SCROLL_BORDER))
1787 flag = 1;
1788 else if (y >= menuY1 && y <= menuY2 && (x < MENU_SCROLL_BORDER
1789 || x > scrXe-MENU_SCROLL_BORDER))
1790 flag = 1;
1792 return flag;
1796 typedef struct _delay {
1797 WWindow *wwin;
1798 WMenu *menu;
1799 int ox,oy;
1800 } _delay;
1803 static void
1804 _leaving(_delay *dl)
1806 wMenuMove(dl->menu, dl->ox, dl->oy, True);
1807 dl->menu->jump_back=NULL;
1808 dl->menu->menu->screen_ptr->flags.jump_back_pending = 0;
1809 wfree(dl);
1813 void
1814 wMenuScroll(WMenu *menu, XEvent *event)
1816 WMenu *smenu;
1817 WMenu *omenu = parentMenu(menu);
1818 WScreen *scr = menu->frame->screen_ptr;
1819 int done = 0;
1820 int jump_back = 0;
1821 int old_frame_x = omenu->frame_x;
1822 int old_frame_y = omenu->frame_y;
1823 XEvent ev;
1825 if (omenu->jump_back)
1826 WMDeleteTimerWithClientData(omenu->jump_back);
1829 if ((/*omenu->flags.buttoned &&*/ !wPreferences.wrap_menus)
1830 || omenu->flags.app_menu) {
1831 jump_back = 1;
1834 if (!wPreferences.wrap_menus)
1835 raiseMenus(omenu, True);
1836 else
1837 raiseMenus(menu, False);
1839 if (!menu->timer)
1840 scrollMenuCallback(menu);
1842 while(!done) {
1843 int x, y, on_border, on_x_edge, on_y_edge, on_title;
1845 WMNextEvent(dpy, &ev);
1846 switch (ev.type) {
1847 case EnterNotify:
1848 WMHandleEvent(&ev);
1849 case MotionNotify:
1850 x = (ev.type==MotionNotify) ? ev.xmotion.x_root : ev.xcrossing.x_root;
1851 y = (ev.type==MotionNotify) ? ev.xmotion.y_root : ev.xcrossing.y_root;
1853 /* on_border is != 0 if the pointer is between the menu
1854 * and the screen border and is close enough to the border */
1855 on_border = isPointNearBoder(menu, x, y);
1857 smenu = wMenuUnderPointer(scr);
1859 if ((smenu==NULL && !on_border) || (smenu && parentMenu(smenu)!=omenu)) {
1860 done = 1;
1861 break;
1864 on_x_edge = x <= 1 || x >= scr->scr_width - 2;
1865 on_y_edge = y <= 1 || y >= scr->scr_height - 2;
1866 on_border = on_x_edge || on_y_edge;
1868 if (!on_border && !jump_back) {
1869 done = 1;
1870 break;
1873 if (menu->timer && (smenu!=menu || (!on_y_edge && !on_x_edge))) {
1874 WMDeleteTimerHandler(menu->timer);
1875 menu->timer = NULL;
1878 if (smenu != NULL)
1879 menu = smenu;
1881 if (!menu->timer)
1882 scrollMenuCallback(menu);
1883 break;
1884 case ButtonPress:
1885 /* True if we push on title, or drag the omenu to other position */
1886 on_title = ev.xbutton.x_root >= omenu->frame_x &&
1887 ev.xbutton.x_root <= omenu->frame_x + MENUW(omenu) &&
1888 ev.xbutton.y_root >= omenu->frame_y &&
1889 ev.xbutton.y_root <= omenu->frame_y + omenu->frame->top_width;
1890 WMHandleEvent(&ev);
1891 smenu = wMenuUnderPointer(scr);
1892 if (smenu == NULL || (smenu && smenu->flags.buttoned && smenu != omenu))
1893 done = 1;
1894 else if (smenu==omenu && on_title) {
1895 jump_back = 0;
1896 done = 1;
1898 break;
1899 case KeyPress:
1900 done = 1;
1901 default:
1902 WMHandleEvent(&ev);
1903 break;
1907 if (menu->timer) {
1908 WMDeleteTimerHandler(menu->timer);
1909 menu->timer = NULL;
1912 if (jump_back) {
1913 _delay *delayer;
1914 if (!omenu->jump_back) {
1915 delayer=wmalloc(sizeof(_delay));
1916 delayer->menu=omenu;
1917 delayer->ox=old_frame_x;
1918 delayer->oy=old_frame_y;
1919 omenu->jump_back=delayer;
1920 scr->flags.jump_back_pending = 1;
1922 else delayer = omenu->jump_back;
1923 WMAddTimerHandler(MENU_JUMP_BACK_DELAY,(WMCallback*)_leaving, delayer);
1929 static void
1930 menuExpose(WObjDescriptor *desc, XEvent *event)
1932 wMenuPaint(desc->parent);
1935 typedef struct {
1936 int *delayed_select;
1937 WMenu *menu;
1938 WMHandlerID magic;
1939 } delay_data;
1942 static void
1943 delaySelection(void *data)
1945 delay_data *d = (delay_data*)data;
1946 int x, y, entry_no;
1947 WMenu *menu;
1949 d->magic = NULL;
1951 menu = findMenu(d->menu->menu->screen_ptr, &x, &y);
1952 if (menu && (d->menu == menu || d->delayed_select)) {
1953 entry_no = getEntryAt(menu, x, y);
1954 selectEntry(menu, entry_no);
1956 if (d->delayed_select)
1957 *(d->delayed_select) = 0;
1961 static void
1962 menuMouseDown(WObjDescriptor *desc, XEvent *event)
1964 XButtonEvent *bev = &event->xbutton;
1965 WMenu *menu = desc->parent;
1966 WMenu *smenu;
1967 WScreen *scr=menu->frame->screen_ptr;
1968 WMenuEntry *entry=NULL;
1969 XEvent ev;
1970 int close_on_exit=0;
1971 int done=0;
1972 int delayed_select = 0;
1973 int entry_no;
1974 int x, y;
1975 int prevx, prevy;
1976 int old_frame_x = 0;
1977 int old_frame_y = 0;
1978 delay_data d_data = {NULL, NULL, NULL};
1980 if (!wPreferences.wrap_menus) {
1981 smenu = parentMenu(menu);
1982 old_frame_x = smenu->frame_x;
1983 old_frame_y = smenu->frame_y;
1984 } else if (event->xbutton.window == menu->frame->core->window) {
1985 /* This is true if the menu was launched with right click on root window */
1986 delayed_select = 1;
1987 d_data.delayed_select = &delayed_select;
1988 d_data.menu = menu;
1989 d_data.magic = WMAddTimerHandler(wPreferences.dblclick_time,
1990 delaySelection, &d_data);
1993 if (menu->flags.inside_handler) {
1994 return;
1996 menu->flags.inside_handler = 1;
1998 wRaiseFrame(menu->frame->core);
2000 close_on_exit = (bev->send_event || menu->flags.brother);
2002 smenu = findMenu(scr, &x, &y);
2003 if (!smenu) {
2004 x = -1;
2005 y = -1;
2006 } else {
2007 menu = smenu;
2010 if (menu->flags.editing) {
2011 goto byebye;
2013 entry_no = getEntryAt(menu, x, y);
2014 if (entry_no>=0) {
2015 entry = menu->entries[entry_no];
2017 if (!close_on_exit && (bev->state & ControlMask) && smenu
2018 && entry->flags.editable) {
2019 editEntry(smenu, entry);
2020 goto byebye;
2021 } else if (bev->state & ControlMask) {
2022 goto byebye;
2025 if (entry->flags.enabled && entry->cascade>=0 && menu->cascades) {
2026 WMenu *submenu = menu->cascades[entry->cascade];
2027 /* map cascade */
2028 if (submenu->flags.mapped && !submenu->flags.buttoned &&
2029 menu->selected_entry!=entry_no) {
2030 wMenuUnmap(submenu);
2032 if (!submenu->flags.mapped && !delayed_select) {
2033 selectEntry(menu, entry_no);
2034 } else if (!submenu->flags.buttoned) {
2035 selectEntry(menu, -1);
2038 } else if (!delayed_select) {
2039 selectEntry(menu, entry_no);
2042 if (!wPreferences.wrap_menus && !wPreferences.scrollable_menus) {
2043 if (!menu->timer)
2044 dragScrollMenuCallback(menu);
2048 prevx = bev->x_root;
2049 prevy = bev->y_root;
2050 while (!done) {
2051 int x, y;
2053 XAllowEvents(dpy, AsyncPointer|SyncPointer, CurrentTime);
2055 WMMaskEvent(dpy, ExposureMask|ButtonMotionMask|ButtonReleaseMask
2056 |ButtonPressMask, &ev);
2057 switch (ev.type) {
2058 case MotionNotify:
2059 smenu = findMenu(scr, &x, &y);
2061 if (smenu == NULL) {
2062 /* moved mouse out of menu */
2064 if (!delayed_select && d_data.magic) {
2065 WMDeleteTimerHandler(d_data.magic);
2066 d_data.magic = NULL;
2068 if (menu==NULL
2069 || (menu->selected_entry>=0
2070 && menu->entries[menu->selected_entry]->cascade>=0)) {
2071 prevx = ev.xmotion.x_root;
2072 prevy = ev.xmotion.y_root;
2074 break;
2076 selectEntry(menu, -1);
2077 menu = smenu;
2078 prevx = ev.xmotion.x_root;
2079 prevy = ev.xmotion.y_root;
2080 break;
2081 } else if (menu && menu!=smenu
2082 && (menu->selected_entry<0
2083 || menu->entries[menu->selected_entry]->cascade<0)) {
2084 selectEntry(menu, -1);
2086 if (!delayed_select && d_data.magic) {
2087 WMDeleteTimerHandler(d_data.magic);
2088 d_data.magic = NULL;
2090 } else {
2092 /* hysteresis for item selection */
2094 /* check if the motion was to the side, indicating that
2095 * the user may want to cross to a submenu */
2096 if (!delayed_select && menu) {
2097 int dx;
2098 Bool moved_to_submenu;/* moved to direction of submenu */
2100 dx = abs(prevx - ev.xmotion.x_root);
2102 moved_to_submenu = False;
2103 if (dx > 0 /* if moved enough to the side */
2104 /* maybe a open submenu */
2105 && menu->selected_entry>=0
2106 /* moving to the right direction */
2107 && (wPreferences.align_menus
2108 || ev.xmotion.y_root >= prevy)) {
2109 int index;
2111 index = menu->entries[menu->selected_entry]->cascade;
2112 if (index>=0) {
2113 if (menu->cascades[index]->frame_x>menu->frame_x) {
2114 if (prevx < ev.xmotion.x_root)
2115 moved_to_submenu = True;
2116 } else {
2117 if (prevx > ev.xmotion.x_root)
2118 moved_to_submenu = True;
2124 if (menu != smenu) {
2125 if (d_data.magic) {
2126 WMDeleteTimerHandler(d_data.magic);
2128 d_data.magic = NULL;
2129 } else if (moved_to_submenu) {
2130 /* while we are moving, postpone the selection */
2131 if (d_data.magic) {
2132 WMDeleteTimerHandler(d_data.magic);
2134 d_data.delayed_select = NULL;
2135 d_data.menu = menu;
2136 d_data.magic = WMAddTimerHandler(MENU_SELECT_DELAY,
2137 delaySelection,
2138 &d_data);
2139 prevx = ev.xmotion.x_root;
2140 prevy = ev.xmotion.y_root;
2141 break;
2142 } else {
2143 if (d_data.magic)
2144 WMDeleteTimerHandler(d_data.magic);
2145 d_data.magic = NULL;
2149 prevx = ev.xmotion.x_root;
2150 prevy = ev.xmotion.y_root;
2151 if (menu!=smenu) {
2152 /* pointer crossed menus */
2153 if (menu && menu->timer) {
2154 WMDeleteTimerHandler(menu->timer);
2155 menu->timer = NULL;
2157 if (smenu)
2158 dragScrollMenuCallback(smenu);
2160 menu = smenu;
2161 if (!menu->timer)
2162 dragScrollMenuCallback(menu);
2164 if (!delayed_select) {
2165 entry_no = getEntryAt(menu, x, y);
2166 if (entry_no>=0) {
2167 entry = menu->entries[entry_no];
2168 if (entry->flags.enabled && entry->cascade>=0 &&
2169 menu->cascades) {
2170 WMenu *submenu = menu->cascades[entry->cascade];
2171 if (submenu->flags.mapped && !submenu->flags.buttoned
2172 && menu->selected_entry!=entry_no) {
2173 wMenuUnmap(submenu);
2177 selectEntry(menu, entry_no);
2179 break;
2181 case ButtonPress:
2182 break;
2184 case ButtonRelease:
2185 if (ev.xbutton.button == event->xbutton.button)
2186 done=1;
2187 break;
2189 case Expose:
2190 WMHandleEvent(&ev);
2191 break;
2195 if (menu && menu->timer) {
2196 WMDeleteTimerHandler(menu->timer);
2197 menu->timer = NULL;
2199 if (d_data.magic!=NULL)
2200 WMDeleteTimerHandler(d_data.magic);
2202 if (menu && menu->selected_entry>=0) {
2203 entry = menu->entries[menu->selected_entry];
2204 if (entry->callback!=NULL && entry->flags.enabled
2205 && entry->cascade < 0) {
2206 /* blink and erase menu selection */
2207 #if (MENU_BLINK_DELAY > 0)
2208 int sel = menu->selected_entry;
2209 int i;
2211 for (i=0; i<MENU_BLINK_COUNT; i++) {
2212 paintEntry(menu, sel, False);
2213 XSync(dpy, 0);
2214 wusleep(MENU_BLINK_DELAY);
2215 paintEntry(menu, sel, True);
2216 XSync(dpy, 0);
2217 wusleep(MENU_BLINK_DELAY);
2219 #endif
2220 /* unmap the menu, it's parents and call the callback */
2221 if (!menu->flags.buttoned &&
2222 (!menu->flags.app_menu||menu->parent!=NULL)) {
2223 closeCascade(menu);
2224 } else {
2225 selectEntry(menu, -1);
2227 (*entry->callback)(menu, entry);
2229 /* If the user double clicks an entry, the entry will
2230 * be executed twice, which is not good for things like
2231 * the root menu. So, ignore any clicks that were generated
2232 * while the entry was being executed */
2233 while (XCheckTypedWindowEvent(dpy, menu->menu->window,
2234 ButtonPress, &ev));
2235 } else if (entry->callback!=NULL && entry->cascade<0) {
2236 selectEntry(menu, -1);
2237 } else {
2238 if (entry->cascade>=0 && menu->cascades
2239 && menu->cascades[entry->cascade]->flags.brother) {
2240 selectEntry(menu, -1);
2245 if (((WMenu*)desc->parent)->flags.brother || close_on_exit || !smenu)
2246 closeCascade(desc->parent);
2248 /* close the cascade windows that should not remain opened */
2249 closeBrotherCascadesOf(desc->parent);
2251 if (!wPreferences.wrap_menus)
2252 wMenuMove(parentMenu(desc->parent), old_frame_x, old_frame_y, True);
2254 byebye:
2255 ((WMenu*)desc->parent)->flags.inside_handler = 0;
2259 void
2260 wMenuMove(WMenu *menu, int x, int y, int submenus)
2262 WMenu *submenu;
2263 int i;
2265 if (!menu) return;
2267 menu->frame_x = x;
2268 menu->frame_y = y;
2269 XMoveWindow(dpy, menu->frame->core->window, x, y);
2271 if (submenus>0 && menu->selected_entry>=0) {
2272 i = menu->entries[menu->selected_entry]->cascade;
2274 if (i>=0 && menu->cascades) {
2275 submenu = menu->cascades[i];
2276 if (submenu->flags.mapped && !submenu->flags.buttoned) {
2277 if (wPreferences.align_menus) {
2278 wMenuMove(submenu, x + MENUW(menu), y, submenus);
2279 } else {
2280 wMenuMove(submenu, x+ MENUW(menu),
2281 y + submenu->entry_height*menu->selected_entry,
2282 submenus);
2287 if (submenus<0 && menu->parent!=NULL && menu->parent->flags.mapped &&
2288 !menu->parent->flags.buttoned) {
2289 if (wPreferences.align_menus) {
2290 wMenuMove(menu->parent, x - MENUW(menu->parent), y, submenus);
2291 } else {
2292 wMenuMove(menu->parent, x - MENUW(menu->parent), menu->frame_y
2293 - menu->parent->entry_height*menu->parent->selected_entry,
2294 submenus);
2300 static void
2301 changeMenuLevels(WMenu *menu, int lower)
2303 int i;
2305 if (!lower) {
2306 ChangeStackingLevel(menu->frame->core, (!menu->parent ? WMMainMenuLevel
2307 : WMSubmenuLevel));
2308 wRaiseFrame(menu->frame->core);
2309 menu->flags.lowered = 0;
2310 } else {
2311 ChangeStackingLevel(menu->frame->core, WMNormalLevel);
2312 wLowerFrame(menu->frame->core);
2313 menu->flags.lowered = 1;
2315 for (i=0; i<menu->cascade_no; i++) {
2316 if (menu->cascades[i]
2317 && !menu->cascades[i]->flags.buttoned
2318 && menu->cascades[i]->flags.lowered!=lower) {
2319 changeMenuLevels(menu->cascades[i], lower);
2326 static void
2327 menuTitleDoubleClick(WCoreWindow *sender, void *data, XEvent *event)
2329 WMenu *menu = data;
2330 int lower;
2332 if (event->xbutton.state & MOD_MASK) {
2333 if (menu->flags.lowered) {
2334 lower = 0;
2335 } else {
2336 lower = 1;
2338 changeMenuLevels(menu, lower);
2343 static void
2344 menuTitleMouseDown(WCoreWindow *sender, void *data, XEvent *event)
2346 WMenu *menu = data;
2347 WMenu *tmp;
2348 XEvent ev;
2349 int x=menu->frame_x, y=menu->frame_y;
2350 int dx=event->xbutton.x_root, dy=event->xbutton.y_root;
2351 int i, lower;
2352 Bool started;
2354 /* can't touch the menu copy */
2355 if (menu->flags.brother)
2356 return;
2358 if (event->xbutton.button != Button1 && event->xbutton.button != Button2)
2359 return;
2361 if (event->xbutton.state & MOD_MASK) {
2362 wLowerFrame(menu->frame->core);
2363 lower = 1;
2364 } else {
2365 wRaiseFrame(menu->frame->core);
2366 lower = 0;
2368 tmp = menu;
2370 /* lower/raise all submenus */
2371 while (1) {
2372 if (tmp->selected_entry>=0 && tmp->cascades
2373 && tmp->entries[tmp->selected_entry]->cascade>=0) {
2374 tmp = tmp->cascades[tmp->entries[tmp->selected_entry]->cascade];
2375 if (!tmp || !tmp->flags.mapped)
2376 break;
2377 if (lower)
2378 wLowerFrame(tmp->frame->core);
2379 else
2380 wRaiseFrame(tmp->frame->core);
2381 } else {
2382 break;
2386 /* tear off the menu if it's a root menu or a cascade
2387 application menu */
2388 if (!menu->flags.buttoned && !menu->flags.brother
2389 && (!menu->flags.app_menu||menu->parent!=NULL)) {
2390 menu->flags.buttoned=1;
2391 wFrameWindowShowButton(menu->frame, WFF_RIGHT_BUTTON);
2392 if (menu->parent) {
2393 /* turn off selected menu entry in parent menu */
2394 selectEntry(menu->parent, -1);
2396 /* make parent map the copy in place of the original */
2397 for (i=0; i<menu->parent->cascade_no; i++) {
2398 if (menu->parent->cascades[i] == menu) {
2399 menu->parent->cascades[i] = menu->brother;
2400 break;
2406 started = False;
2407 while(1) {
2408 WMMaskEvent(dpy, ButtonMotionMask|ButtonReleaseMask|ButtonPressMask
2409 |ExposureMask, &ev);
2410 switch (ev.type) {
2411 case MotionNotify:
2412 if (started) {
2413 x += ev.xmotion.x_root - dx;
2414 y += ev.xmotion.y_root - dy;
2415 dx = ev.xmotion.x_root;
2416 dy = ev.xmotion.y_root;
2417 wMenuMove(menu, x, y, True);
2418 } else {
2419 if (abs(ev.xmotion.x_root - dx) > MOVE_THRESHOLD
2420 || abs(ev.xmotion.y_root - dy) > MOVE_THRESHOLD) {
2421 started = True;
2422 XGrabPointer(dpy, menu->frame->titlebar->window, False,
2423 ButtonMotionMask|ButtonReleaseMask
2424 |ButtonPressMask,
2425 GrabModeAsync, GrabModeAsync, None,
2426 wCursor[WCUR_MOVE], CurrentTime);
2429 break;
2431 case ButtonPress:
2432 break;
2434 case ButtonRelease:
2435 if (ev.xbutton.button != event->xbutton.button)
2436 break;
2437 XUngrabPointer(dpy, CurrentTime);
2438 return;
2440 default:
2441 WMHandleEvent(&ev);
2442 break;
2448 *----------------------------------------------------------------------
2449 * menuCloseClick--
2450 * Handles mouse click on the close button of menus. The menu is
2451 * closed when the button is clicked.
2453 * Side effects:
2454 * The closed menu is reinserted at it's parent menus
2455 * cascade list.
2456 *----------------------------------------------------------------------
2458 static void
2459 menuCloseClick(WCoreWindow *sender, void *data, XEvent *event)
2461 WMenu *menu = (WMenu*)data;
2462 WMenu *parent = menu->parent;
2463 int i;
2465 if (parent) {
2466 for (i=0; i<parent->cascade_no; i++) {
2467 /* find the entry that points to the copy */
2468 if (parent->cascades[i] == menu->brother) {
2469 /* make it point to the original */
2470 parent->cascades[i] = menu;
2471 menu->parent = parent;
2472 break;
2476 wMenuUnmap(menu);
2480 static void
2481 saveMenuInfo(proplist_t dict, WMenu *menu, proplist_t key)
2483 proplist_t value, list;
2484 char buffer[256];
2486 sprintf(buffer, "%i,%i", menu->frame_x, menu->frame_y);
2487 value = PLMakeString(buffer);
2488 list = PLMakeArrayFromElements(value, NULL);
2489 if (menu->flags.lowered)
2490 PLAppendArrayElement(list, PLMakeString("lowered"));
2491 PLInsertDictionaryEntry(dict, key, list);
2492 PLRelease(value);
2493 PLRelease(list);
2497 void
2498 wMenuSaveState(WScreen *scr)
2500 proplist_t menus, key;
2501 int save_menus = 0;
2503 menus = PLMakeDictionaryFromEntries(NULL, NULL, NULL);
2505 #ifndef LITE
2506 if (scr->switch_menu && scr->switch_menu->flags.buttoned) {
2507 key = PLMakeString("SwitchMenu");
2508 saveMenuInfo(menus, scr->switch_menu, key);
2509 PLRelease(key);
2510 save_menus = 1;
2513 if (saveMenuRecurs(menus, scr, scr->root_menu))
2514 save_menus = 1;
2516 #endif /* !LITE */
2517 if (scr->workspace_menu && scr->workspace_menu->flags.buttoned) {
2518 key = PLMakeString("WorkspaceMenu");
2519 saveMenuInfo(menus, scr->workspace_menu, key);
2520 PLRelease(key);
2521 save_menus = 1;
2524 if (save_menus) {
2525 key = PLMakeString("Menus");
2526 PLInsertDictionaryEntry(scr->session_state, key, menus);
2527 PLRelease(key);
2529 PLRelease(menus);
2533 #ifndef LITE
2535 static Bool
2536 getMenuPath(WMenu *menu, char *buffer, int bufSize)
2538 Bool ok = True;
2539 int len = 0;
2541 if (!menu->flags.titled || !menu->frame->title[0])
2542 return False;
2544 len = strlen(menu->frame->title);
2545 if (len >= bufSize)
2546 return False;
2548 if (menu->parent) {
2549 ok = getMenuPath(menu->parent, buffer, bufSize - len - 1);
2550 if (!ok)
2551 return False;
2554 strcat(buffer, "\\");
2555 strcat(buffer, menu->frame->title);
2557 return True;
2561 static Bool
2562 saveMenuRecurs(proplist_t menus, WScreen *scr, WMenu *menu)
2564 proplist_t key;
2565 int save_menus = 0, i;
2566 char buffer[512];
2567 Bool ok = True;
2570 if (menu->flags.brother)
2571 menu = menu->brother;
2573 if (menu->flags.buttoned && menu != scr->switch_menu) {
2575 buffer[0] = '\0';
2576 ok = getMenuPath(menu, buffer, 510);
2578 if (ok) {
2579 key = PLMakeString(buffer);
2580 saveMenuInfo(menus, menu, key);
2581 PLRelease(key);
2582 save_menus = 1;
2586 if (ok) {
2587 for (i = 0; i < menu->cascade_no; i++) {
2588 if (saveMenuRecurs(menus, scr, menu->cascades[i]))
2589 save_menus = 1;
2592 return save_menus;
2594 #endif /* !LITE */
2597 #define COMPLAIN(key) wwarning(_("bad value in menus state info:%s"), key)
2600 static Bool
2601 getMenuInfo(proplist_t info, int *x, int *y, Bool *lowered)
2603 proplist_t pos;
2605 *lowered = False;
2607 if (PLIsArray(info)) {
2608 proplist_t flags;
2609 pos = PLGetArrayElement(info, 0);
2610 flags = PLGetArrayElement(info, 1);
2611 if (flags != NULL && PLIsString(flags) && PLGetString(flags) != NULL
2612 && strcmp(PLGetString(flags), "lowered") == 0) {
2613 *lowered = True;
2615 } else {
2616 pos = info;
2619 if (pos != NULL && PLIsString(pos)) {
2620 if (sscanf(PLGetString(pos), "%i,%i", x, y)!=2)
2621 COMPLAIN("Position");
2622 } else {
2623 COMPLAIN("(position, flags...)");
2624 return False;
2627 return True;
2631 static int
2632 restoreMenu(WScreen *scr, proplist_t menu, int which)
2634 int x, y;
2635 Bool lowered = False;
2636 WMenu *pmenu = NULL;
2638 if (!menu)
2639 return False;
2641 if (!getMenuInfo(menu, &x, &y, &lowered))
2642 return False;
2645 #ifndef LITE
2646 if (which & WSS_SWITCHMENU) {
2647 OpenSwitchMenu(scr, x, y, False);
2648 pmenu = scr->switch_menu;
2650 #endif /* !LITE */
2652 if (pmenu) {
2653 int width = MENUW(pmenu);
2654 int height = MENUH(pmenu);
2656 if (lowered) {
2657 changeMenuLevels(pmenu, True);
2660 x = (x < -width) ? 0 : x;
2661 x = (x > scr->scr_width) ? scr->scr_width - width : x;
2662 y = (y < 0) ? 0 : y;
2663 y = (y > scr->scr_height) ? scr->scr_height - height : y;
2664 wMenuMove(pmenu, x, y, True);
2665 pmenu->flags.buttoned = 1;
2666 wFrameWindowShowButton(pmenu->frame, WFF_RIGHT_BUTTON);
2667 return True;
2669 return False;
2673 #ifndef LITE
2674 static int
2675 restoreMenuRecurs(WScreen *scr, proplist_t menus, WMenu *menu, char *path)
2677 proplist_t key, entry;
2678 char buffer[512];
2679 int i, x, y, res;
2680 Bool lowered;
2682 if (strlen(path) + strlen(menu->frame->title) > 510)
2683 return False;
2685 sprintf(buffer, "%s\\%s", path, menu->frame->title);
2686 key = PLMakeString(buffer);
2687 entry = PLGetDictionaryEntry(menus, key);
2688 res = False;
2690 if (entry && getMenuInfo(entry, &x, &y, &lowered)) {
2692 if (!menu->flags.mapped) {
2693 int width = MENUW(menu);
2694 int height = MENUH(menu);
2696 wMenuMapAt(menu, x, y, False);
2698 if (menu->parent) {
2699 /* make parent map the copy in place of the original */
2700 for (i=0; i<menu->parent->cascade_no; i++) {
2701 if (menu->parent->cascades[i] == menu) {
2702 menu->parent->cascades[i] = menu->brother;
2703 break;
2707 if (lowered) {
2708 changeMenuLevels(menu, True);
2710 x = (x < -width) ? 0 : x;
2711 x = (x > scr->scr_width) ? scr->scr_width - width : x;
2712 y = (y < 0) ? 0 : y;
2713 y = (y > scr->scr_height) ? scr->scr_height - height : y;
2714 wMenuMove(menu, x, y, True);
2715 menu->flags.buttoned = 1;
2716 wFrameWindowShowButton(menu->frame, WFF_RIGHT_BUTTON);
2717 res = True;
2721 PLRelease(key);
2723 for (i=0; i<menu->cascade_no; i++) {
2724 if (restoreMenuRecurs(scr, menus, menu->cascades[i], buffer) != False)
2725 res = True;
2728 return res;
2730 #endif /* !LITE */
2733 void
2734 wMenuRestoreState(WScreen *scr)
2736 proplist_t menus, menu, key, skey;
2738 key = PLMakeString("Menus");
2739 menus = PLGetDictionaryEntry(scr->session_state, key);
2740 PLRelease(key);
2742 if (!menus)
2743 return;
2745 /* restore menus */
2747 skey = PLMakeString("SwitchMenu");
2748 menu = PLGetDictionaryEntry(menus, skey);
2749 PLRelease(skey);
2750 restoreMenu(scr, menu, WSS_SWITCHMENU);
2752 #ifndef LITE
2753 if (!scr->root_menu) {
2754 OpenRootMenu(scr, scr->scr_width*2, 0, False);
2755 wMenuUnmap(scr->root_menu);
2757 restoreMenuRecurs(scr, menus, scr->root_menu, "");
2758 #endif /* !LITE */
2762 void
2763 OpenWorkspaceMenu(WScreen *scr, int x, int y)
2765 WMenu *menu, *parent;
2766 WMenuEntry *entry;
2768 #ifndef LITE
2769 if (!scr->root_menu) {
2770 OpenRootMenu(scr, scr->scr_width*2, 0, False);
2771 wMenuUnmap(scr->root_menu);
2773 #endif
2774 menu = scr->workspace_menu;
2775 if (menu) {
2776 if (menu->flags.mapped) {
2777 if (!menu->flags.buttoned) {
2778 wMenuUnmap(menu);
2779 parent = menu->parent;
2780 if (parent && parent->selected_entry >= 0) {
2781 entry = parent->entries[parent->selected_entry];
2782 if (parent->cascades[entry->cascade] == menu) {
2783 selectEntry(parent, -1);
2784 wMenuMapAt(menu, x, y, False);
2787 } else {
2788 wRaiseFrame(menu->frame->core);
2789 wMenuMapCopyAt(menu, x, y);
2791 } else {
2792 wMenuMapAt(menu, x, y, False);