changes related to plugin system & drawstring
[wmaker-crm.git] / src / menu.c
bloba10de6aa9cdf618d6406745a43550994a9c2b692
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, Drawable 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];
734 #ifdef DRAWSTRING_PLUGIN
735 Pixmap tmp_bg;
736 Pixmap *texture_data;
737 WPluginData *p;
738 int _y;
739 int tb = menu->entry_height; /* convert short into int */
740 #endif
743 if (!menu->flags.realized) return;
744 h = menu->entry_height;
745 w = menu->menu->width;
746 y = index * h;
748 light = scr->menu_item_auxtexture->light_gc;
749 dim = scr->menu_item_auxtexture->dim_gc;
750 dark = scr->menu_item_auxtexture->dark_gc;
752 if (wPreferences.menu_style == MS_FLAT && menu->entry_no > 1) {
753 if (index == 0)
754 type = F_TOP;
755 else if (index == menu->entry_no - 1)
756 type = F_BOTTOM;
757 else
758 type = F_NONE;
759 } else {
760 type = F_NORMAL;
763 /* paint background */
764 if (selected) {
765 XSetForeground(dpy, scr->select_menu_gc, scr->select_pixel);
766 XFillRectangle(dpy, win, scr->select_menu_gc, 1, y+1, w-2, h-3);
767 if (scr->menu_item_texture->any.type == WTEX_SOLID)
768 drawFrame(scr, win, y, w, h, type);
769 } else {
770 #ifdef DRAWSTRING_PLUGIN
771 if (scr->menu_item_texture->any.type == WTEX_SOLID) {
772 XClearArea(dpy, win, 0, y + 1, w - 1, h - 3, False);
773 drawFrame(scr, win, y, w, h, type);
774 } else {
776 * if the function is there, it responses for clearing area
777 * to reduce flickering
779 if (!scr->drawstring_func[W_STRING_MTEXT])
780 XClearArea(dpy, win, 0, y, w, h, False);
782 #else
783 if (scr->menu_item_texture->any.type == WTEX_SOLID) {
784 XClearArea(dpy, win, 0, y + 1, w - 1, h - 3, False);
785 /* draw the frame */
786 drawFrame(scr, win, y, w, h, type);
787 } else {
788 XClearArea(dpy, win, 0, y, w, h, False);
790 #endif
793 if (selected) {
794 textGC = scr->select_menu_gc;
795 if (entry->flags.enabled)
796 XSetForeground(dpy, textGC, scr->select_text_pixel);
797 else
798 XSetForeground(dpy, textGC, scr->dtext_pixel);
799 } else if (!entry->flags.enabled) {
800 textGC = scr->disabled_menu_entry_gc;
801 } else {
802 textGC = scr->menu_entry_gc;
804 /* draw text */
805 x = 5;
806 if (entry->flags.indicator)
807 x += MENU_INDICATOR_SPACE + 2;
809 #ifdef DRAWSTRING_PLUGIN
810 if (scr->drawstring_func[W_STRING_MTEXT]) {
811 _y = (wPreferences.menu_style == MS_NORMAL) ? 0 : y;
812 texture_data = menu->flags.brother ?
813 &menu->brother->menu_texture_data : &menu->menu_texture_data;
815 tmp_bg = XCreatePixmap(dpy, win, w, menu->entry_height,
816 DefaultDepth(dpy, DefaultScreen(dpy)));
818 if (scr->menu_item_texture->any.type == WTEX_SOLID) {
819 XFillRectangle(dpy, tmp_bg, scr->menu_item_texture->solid.normal_gc,
820 0, 0, w, h);
821 drawFrame(scr, tmp_bg, 0, w, h, type);
823 } else {
824 XCopyArea(dpy, *texture_data, tmp_bg, textGC,
825 0, _y, w, menu->entry_height, 0, 0);
828 if (selected) {
829 XSetForeground(dpy, scr->select_menu_gc, scr->select_pixel);
830 XFillRectangle(dpy, tmp_bg, scr->select_menu_gc, 1, 1, w-2, h-3);
834 p = wPluginPackData(6,
835 scr->drawstring_func[W_STRING_MTEXT]->data,
836 &tmp_bg,
837 &textGC,
838 scr->menu_entry_font,
839 &menu->frame->core->width,
840 &tb, "extendable");
842 scr->drawstring_func[W_STRING_MTEXT]->proc.drawString[W_DSPROC_DRAWSTRING](
843 scr->drawstring_func[W_STRING_MTEXT]->arg,
844 win,
845 x, y,
846 entry->text, strlen(entry->text), p);
848 XFreePixmap(dpy, tmp_bg);
850 free(p);
851 } else {
852 WMDrawString(scr->wmscreen, win, textGC, scr->menu_entry_font,
853 x, 3 + y + wPreferences.menu_text_clearance, entry->text, strlen(entry->text));
856 #else
857 WMDrawString(scr->wmscreen, win, textGC, scr->menu_entry_font,
858 x, 3 + y + wPreferences.menu_text_clearance, entry->text, strlen(entry->text));
859 #endif
861 if (entry->cascade>=0) {
862 /* draw the cascade indicator */
863 XDrawLine(dpy,win,dim, w-11, y+6, w-6, y+h/2-1);
864 XDrawLine(dpy,win,light, w-11, y+h-8, w-6, y+h/2-1);
865 XDrawLine(dpy,win,dark, w-12, y+6, w-12, y+h-8);
868 /* draw indicator */
869 if (entry->flags.indicator && entry->flags.indicator_on) {
870 int iw, ih;
871 WPixmap *indicator;
874 switch (entry->flags.indicator_type) {
875 case MI_CHECK:
876 indicator = scr->menu_check_indicator;
877 break;
878 case MI_MINIWINDOW:
879 indicator = scr->menu_mini_indicator;
880 break;
881 case MI_HIDDEN:
882 indicator = scr->menu_hide_indicator;
883 break;
884 case MI_SHADED:
885 indicator = scr->menu_shade_indicator;
886 break;
887 case MI_DIAMOND:
888 default:
889 indicator = scr->menu_radio_indicator;
890 break;
893 iw = indicator->width;
894 ih = indicator->height;
895 XSetClipMask(dpy, scr->copy_gc, indicator->mask);
896 XSetClipOrigin(dpy, scr->copy_gc, 5, y+(h-ih)/2);
897 if (selected)
898 XSetForeground(dpy, scr->copy_gc, scr->black_pixel);
899 else
900 XSetForeground(dpy, scr->copy_gc, scr->mtext_pixel);
901 XFillRectangle(dpy, win, scr->copy_gc, 5, y+(h-ih)/2, iw, ih);
903 XCopyArea(dpy, indicator->image, win, scr->copy_gc, 0, 0,
904 iw, ih, 5, y+(h-ih)/2);
906 XSetClipOrigin(dpy, scr->copy_gc, 0, 0);
909 /* draw right text */
911 if (entry->rtext && entry->cascade<0) {
913 #ifdef DRAWSTRING_PLUGIN
914 if (scr->drawstring_func[W_STRING_MTEXT]) {
916 p = wPluginPackData(1, scr->drawstring_func[W_STRING_MTEXT]->data);
917 scr->drawstring_func[W_STRING_MTEXT]->proc.widthOfString[W_DSPROC_WIDTHOFSTRING](
918 entry->rtext, strlen(entry->rtext), p, &tw, NULL, NULL);
919 wfree(p);
921 texture_data = menu->flags.brother ?
922 &menu->brother->menu_texture_data : &menu->menu_texture_data;
924 tmp_bg = XCreatePixmap(dpy, win, w, menu->entry_height,
925 DefaultDepth(dpy, DefaultScreen(dpy)));
927 XCopyArea(dpy, win, tmp_bg, textGC,
928 0, y, w, menu->entry_height, 0, 0);
930 p = wPluginPackData(6,
931 scr->drawstring_func[W_STRING_MTEXT]->data,
932 &tmp_bg,
933 &textGC,
934 scr->menu_entry_font,
935 &menu->frame->core->width,
936 &tb, "extendable");
938 scr->drawstring_func[W_STRING_MTEXT]->proc.drawString[W_DSPROC_DRAWSTRING](
939 scr->drawstring_func[W_STRING_MTEXT]->arg,
940 win,
941 w-0-tw, y,
942 entry->rtext, strlen(entry->rtext), p);
944 XFreePixmap(dpy, tmp_bg);
946 free(p);
948 } else {
949 tw = WMWidthOfString(scr->menu_entry_font, entry->rtext,
950 strlen(entry->rtext));
951 WMDrawString(scr->wmscreen, win, textGC, scr->menu_entry_font, w-6-tw,
952 y + 3 + wPreferences.menu_text_clearance, entry->rtext, strlen(entry->rtext));
954 #else
955 tw = WMWidthOfString(scr->menu_entry_font, entry->rtext,
956 strlen(entry->rtext));
957 WMDrawString(scr->wmscreen, win, textGC, scr->menu_entry_font, w-6-tw,
958 y + 3 + wPreferences.menu_text_clearance, entry->rtext, strlen(entry->rtext));
959 #endif
964 static void
965 move_menus(WMenu *menu, int x, int y)
967 while (menu->parent) {
968 menu = menu->parent;
969 x -= MENUW(menu);
970 if (!wPreferences.align_menus && menu->selected_entry>=0) {
971 y -= menu->selected_entry*menu->entry_height;
974 wMenuMove(menu, x, y, True);
977 static void
978 makeVisible(WMenu *menu)
980 WScreen *scr = menu->frame->screen_ptr;
981 int x1, y1, x2, y2, new_x, new_y, move;
983 if (menu->entry_no<0) return;
985 x1 = menu->frame_x;
986 y1 = menu->frame_y+menu->frame->top_width
987 + menu->selected_entry*menu->entry_height;
988 x2 = x1 + MENUW(menu);
989 y2 = y1 + menu->entry_height;
991 new_x = x1;
992 new_y = y1;
993 move = 0;
995 if (x1 < 0) {
996 new_x = 0;
997 move = 1;
998 } else if (x2 >= scr->scr_width) {
999 new_x = scr->scr_width - MENUW(menu) - 1;
1000 move = 1;
1003 if (y1 < 0) {
1004 new_y = 0;
1005 move = 1;
1006 } else if (y2 >= scr->scr_height) {
1007 new_y = scr->scr_height - menu->entry_height - 1;
1008 move = 1;
1011 new_y = new_y - menu->frame->top_width
1012 - menu->selected_entry*menu->entry_height;
1013 move_menus(menu, new_x, new_y);
1017 static int
1018 check_key(WMenu *menu, XKeyEvent *event)
1020 int i, ch, s;
1021 char buffer[32];
1023 if (XLookupString(event, buffer, 32, NULL, NULL)<1)
1024 return -1;
1026 ch = toupper(buffer[0]);
1028 s = (menu->selected_entry>=0 ? menu->selected_entry+1 : 0);
1030 again:
1031 for (i=s; i<menu->entry_no; i++) {
1032 if (ch==toupper(menu->entries[i]->text[0])) {
1033 return i;
1036 /* no match. Retry from start, if previous started from a selected entry */
1037 if (s!=0) {
1038 s = 0;
1039 goto again;
1041 return -1;
1045 static int
1046 keyboardMenu(WMenu *menu)
1048 XEvent event;
1049 KeySym ksym=NoSymbol;
1050 int done=0;
1051 int index;
1052 WMenuEntry *entry;
1053 int old_pos_x = menu->frame_x;
1054 int old_pos_y = menu->frame_y;
1055 int new_x = old_pos_x, new_y = old_pos_y;
1056 int scr_width = menu->frame->screen_ptr->scr_width;
1057 int scr_height = menu->frame->screen_ptr->scr_height;
1059 if (menu->flags.editing)
1060 return False;
1063 XGrabKeyboard(dpy, menu->frame->core->window, True, GrabModeAsync,
1064 GrabModeAsync, CurrentTime);
1066 if (menu->frame_y+menu->frame->top_width >= scr_height)
1067 new_y = scr_height - menu->frame->top_width;
1069 if (menu->frame_x+MENUW(menu) >= scr_width)
1070 new_x = scr_width-MENUW(menu)-1;
1072 move_menus(menu, new_x, new_y);
1074 while (!done && menu->flags.mapped) {
1075 XAllowEvents(dpy, AsyncKeyboard, CurrentTime);
1076 WMMaskEvent(dpy, ExposureMask|ButtonMotionMask|ButtonPressMask
1077 |ButtonReleaseMask|KeyPressMask|KeyReleaseMask
1078 |SubstructureNotifyMask, &event);
1080 switch (event.type) {
1081 case KeyPress:
1082 ksym = XLookupKeysym(&event.xkey, 0);
1083 switch (ksym) {
1084 case XK_Escape:
1085 done = 1;
1086 break;
1088 case XK_Home:
1089 #ifdef XK_KP_Home
1090 case XK_KP_Home:
1091 #endif
1092 selectEntry(menu, 0);
1093 makeVisible(menu);
1094 break;
1096 case XK_End:
1097 #ifdef XK_KP_End
1098 case XK_KP_End:
1099 #endif
1100 selectEntry(menu, menu->entry_no-1);
1101 makeVisible(menu);
1102 break;
1104 case XK_Up:
1105 #ifdef ARROWLESS_KBD
1106 case XK_k:
1107 #endif
1108 #ifdef XK_KP_Up
1109 case XK_KP_Up:
1110 #endif
1111 if (menu->selected_entry <= 0)
1112 selectEntry(menu, menu->entry_no-1);
1113 else
1114 selectEntry(menu, menu->selected_entry-1);
1115 makeVisible(menu);
1116 break;
1118 case XK_Down:
1119 #ifdef ARROWLESS_KBD
1120 case XK_j:
1121 #endif
1122 #ifdef XK_KP_Down
1123 case XK_KP_Down:
1124 #endif
1125 if (menu->selected_entry<0)
1126 selectEntry(menu, 0);
1127 else if (menu->selected_entry == menu->entry_no-1)
1128 selectEntry(menu, 0);
1129 else if (menu->selected_entry < menu->entry_no-1)
1130 selectEntry(menu, menu->selected_entry+1);
1131 makeVisible(menu);
1132 break;
1134 case XK_Right:
1135 #ifdef ARROWLESS_KBD
1136 case XK_l:
1137 #endif
1138 #ifdef XK_KP_Right
1139 case XK_KP_Right:
1140 #endif
1141 if (menu->selected_entry>=0) {
1142 WMenuEntry *entry;
1143 entry = menu->entries[menu->selected_entry];
1145 if (entry->cascade >= 0 && menu->cascades
1146 && menu->cascades[entry->cascade]->entry_no > 0) {
1148 XUngrabKeyboard(dpy, CurrentTime);
1150 selectEntry(menu->cascades[entry->cascade], 0);
1151 if (!keyboardMenu(menu->cascades[entry->cascade]))
1152 done = 1;
1154 XGrabKeyboard(dpy, menu->frame->core->window, True,
1155 GrabModeAsync, GrabModeAsync,
1156 CurrentTime);
1159 break;
1161 case XK_Left:
1162 #ifdef ARROWLESS_KBD
1163 case XK_h:
1164 #endif
1165 #ifdef XK_KP_Left
1166 case XK_KP_Left:
1167 #endif
1168 if (menu->parent!=NULL && menu->parent->selected_entry>=0) {
1169 selectEntry(menu, -1);
1170 move_menus(menu, old_pos_x, old_pos_y);
1171 return True;
1173 break;
1175 case XK_Return:
1176 done = 2;
1177 break;
1179 default:
1180 index = check_key(menu, &event.xkey);
1181 if (index>=0) {
1182 selectEntry(menu, index);
1185 break;
1187 default:
1188 if (event.type==ButtonPress)
1189 done = 1;
1191 WMHandleEvent(&event);
1195 XUngrabKeyboard(dpy, CurrentTime);
1197 if (done==2 && menu->selected_entry>=0) {
1198 entry = menu->entries[menu->selected_entry];
1199 } else {
1200 entry = NULL;
1203 if (entry && entry->callback!=NULL && entry->flags.enabled
1204 && entry->cascade < 0) {
1205 #if (MENU_BLINK_COUNT > 0)
1206 int sel = menu->selected_entry;
1207 int i;
1209 for (i=0; i<MENU_BLINK_COUNT; i++) {
1210 paintEntry(menu, sel, False);
1211 XSync(dpy, 0);
1212 wusleep(MENU_BLINK_DELAY);
1213 paintEntry(menu, sel, True);
1214 XSync(dpy, 0);
1215 wusleep(MENU_BLINK_DELAY);
1217 #endif
1218 selectEntry(menu, -1);
1220 if (!menu->flags.buttoned) {
1221 wMenuUnmap(menu);
1222 move_menus(menu, old_pos_x, old_pos_y);
1224 closeCascade(menu);
1226 (*entry->callback)(menu, entry);
1227 } else {
1228 if (!menu->flags.buttoned) {
1229 wMenuUnmap(menu);
1230 move_menus(menu, old_pos_x, old_pos_y);
1232 selectEntry(menu, -1);
1236 /* returns True if returning from a submenu to a parent menu,
1237 * False if exiting from menu */
1238 return False;
1242 void
1243 wMenuMapAt(WMenu *menu, int x, int y, int keyboard)
1245 int scr_width = menu->frame->screen_ptr->scr_width;
1246 int scr_height = menu->frame->screen_ptr->scr_height;
1248 if (!menu->flags.realized) {
1249 menu->flags.realized=1;
1250 wMenuRealize(menu);
1252 if (!menu->flags.mapped) {
1253 if (wPreferences.wrap_menus) {
1254 if (x<0) x = 0;
1255 if (y<0) y = 0;
1256 if (x+MENUW(menu) > scr_width)
1257 x = scr_width - MENUW(menu);
1258 if (y+MENUH(menu) > scr_height)
1259 y = scr_height - MENUH(menu);
1262 XMoveWindow(dpy, menu->frame->core->window, x, y);
1263 menu->frame_x = x;
1264 menu->frame_y = y;
1265 XMapWindow(dpy, menu->frame->core->window);
1266 wRaiseFrame(menu->frame->core);
1267 menu->flags.mapped = 1;
1268 } else {
1269 selectEntry(menu, 0);
1272 if (keyboard)
1273 keyboardMenu(menu);
1277 void
1278 wMenuMap(WMenu *menu)
1280 if (!menu->flags.realized) {
1281 menu->flags.realized=1;
1282 wMenuRealize(menu);
1284 if (menu->flags.app_menu && menu->parent==NULL) {
1285 menu->frame_x = menu->frame->screen_ptr->app_menu_x;
1286 menu->frame_y = menu->frame->screen_ptr->app_menu_y;
1287 XMoveWindow(dpy, menu->frame->core->window, menu->frame_x, menu->frame_y);
1289 XMapWindow(dpy, menu->frame->core->window);
1290 wRaiseFrame(menu->frame->core);
1291 menu->flags.mapped = 1;
1295 void
1296 wMenuUnmap(WMenu *menu)
1298 int i;
1300 XUnmapWindow(dpy, menu->frame->core->window);
1301 if (menu->flags.titled && menu->flags.buttoned) {
1302 wFrameWindowHideButton(menu->frame, WFF_RIGHT_BUTTON);
1304 menu->flags.buttoned = 0;
1305 menu->flags.mapped = 0;
1306 menu->flags.open_to_left = 0;
1308 for (i=0; i<menu->cascade_no; i++) {
1309 if (menu->cascades[i]!=NULL
1310 && menu->cascades[i]->flags.mapped
1311 && !menu->cascades[i]->flags.buttoned) {
1313 wMenuUnmap(menu->cascades[i]);
1316 menu->selected_entry = -1;
1321 void
1322 wMenuPaint(WMenu *menu)
1324 int i;
1326 if (!menu->flags.mapped) {
1327 return;
1330 /* paint entries */
1331 for (i=0; i<menu->entry_no; i++) {
1332 paintEntry(menu, i, i==menu->selected_entry);
1337 void
1338 wMenuSetEnabled(WMenu *menu, int index, int enable)
1340 if (index>=menu->entry_no) return;
1341 menu->entries[index]->flags.enabled=enable;
1342 paintEntry(menu, index, index==menu->selected_entry);
1343 paintEntry(menu->brother, index, index==menu->selected_entry);
1347 /* ====================================================================== */
1350 static void
1351 editEntry(WMenu *menu, WMenuEntry *entry)
1353 WTextInput *text;
1354 XEvent event;
1355 WObjDescriptor *desc;
1356 char *t;
1357 int done = 0;
1358 Window old_focus;
1359 int old_revert;
1361 menu->flags.editing = 1;
1363 text = wTextCreate(menu->menu, 1, menu->entry_height * entry->order,
1364 menu->menu->width - 2, menu->entry_height - 1);
1366 wTextPutText(text, entry->text);
1367 XGetInputFocus(dpy, &old_focus, &old_revert);
1368 XSetInputFocus(dpy, text->core->window, RevertToNone, CurrentTime);
1370 if (XGrabKeyboard(dpy, text->core->window, True, GrabModeAsync,
1371 GrabModeAsync, CurrentTime)!=GrabSuccess) {
1372 wwarning("could not grab keyboard");
1373 wTextDestroy(text);
1375 wSetFocusTo(menu->frame->screen_ptr,
1376 menu->frame->screen_ptr->focused_window);
1377 return;
1381 while (!done && !text->done) {
1382 XSync(dpy, 0);
1383 XAllowEvents(dpy, AsyncKeyboard|AsyncPointer, CurrentTime);
1384 XSync(dpy, 0);
1385 WMNextEvent(dpy, &event);
1387 if (XFindContext(dpy, event.xany.window, wWinContext,
1388 (XPointer *)&desc)==XCNOENT)
1389 desc = NULL;
1391 if ((desc != NULL) && (desc->handle_anything != NULL)) {
1393 (*desc->handle_anything)(desc, &event);
1395 } else {
1396 switch (event.type) {
1397 case ButtonPress:
1398 XAllowEvents(dpy, ReplayPointer, CurrentTime);
1399 done = 1;
1401 default:
1402 WMHandleEvent(&event);
1403 break;
1408 XSetInputFocus(dpy, old_focus, old_revert, CurrentTime);
1410 wSetFocusTo(menu->frame->screen_ptr,
1411 menu->frame->screen_ptr->focused_window);
1414 t = wTextGetText(text);
1415 /* if !t, the user has canceled editing */
1416 if (t) {
1417 if (entry->text)
1418 wfree(entry->text);
1419 entry->text = wstrdup(t);
1421 menu->flags.realized = 0;
1423 wTextDestroy(text);
1425 XUngrabKeyboard(dpy, CurrentTime);
1427 if (t && menu->on_edit)
1428 (*menu->on_edit)(menu, entry);
1430 menu->flags.editing = 0;
1432 if (!menu->flags.realized)
1433 wMenuRealize(menu);
1437 static void
1438 selectEntry(WMenu *menu, int entry_no)
1440 WMenuEntry *entry;
1441 WMenu *submenu;
1442 int old_entry;
1444 if (menu->entries==NULL)
1445 return;
1447 if (entry_no >= menu->entry_no)
1448 return;
1450 old_entry = menu->selected_entry;
1451 menu->selected_entry = entry_no;
1453 if (old_entry!=entry_no) {
1455 /* unselect previous entry */
1456 if (old_entry>=0) {
1457 paintEntry(menu, old_entry, False);
1458 entry = menu->entries[old_entry];
1460 /* unmap cascade */
1461 if (entry->cascade>=0 && menu->cascades) {
1462 if (!menu->cascades[entry->cascade]->flags.buttoned) {
1463 wMenuUnmap(menu->cascades[entry->cascade]);
1468 if (entry_no<0) {
1469 menu->selected_entry = -1;
1470 return;
1472 entry = menu->entries[entry_no];
1474 if (entry->cascade>=0 && menu->cascades && entry->flags.enabled) {
1475 /* Callback for when the submenu is opened.
1477 submenu = menu->cascades[entry->cascade];
1478 if (submenu && submenu->flags.brother)
1479 submenu = submenu->brother;
1481 if (entry->callback) {
1482 /* Only call the callback if the submenu is not yet mapped.
1484 if (menu->flags.brother) {
1485 if (!submenu || !submenu->flags.mapped)
1486 (*entry->callback)(menu->brother, entry);
1487 } else {
1488 if (!submenu || !submenu->flags.buttoned)
1489 (*entry->callback)(menu, entry);
1493 /* the submenu menu might have changed */
1494 submenu = menu->cascades[entry->cascade];
1496 /* map cascade */
1497 if (!submenu->flags.mapped) {
1498 int x, y;
1500 if (!submenu->flags.realized)
1501 wMenuRealize(submenu);
1502 if (wPreferences.wrap_menus) {
1503 if (menu->flags.open_to_left)
1504 submenu->flags.open_to_left = 1;
1506 if (submenu->flags.open_to_left) {
1507 x = menu->frame_x - MENUW(submenu);
1508 if (x<0) {
1509 x = 0;
1510 submenu->flags.open_to_left = 0;
1512 } else {
1513 x = menu->frame_x + MENUW(menu);
1515 if (x + MENUW(submenu)
1516 >= menu->frame->screen_ptr->scr_width) {
1518 x = menu->frame_x - MENUW(submenu);
1519 submenu->flags.open_to_left = 1;
1522 } else {
1523 x = menu->frame_x + MENUW(menu);
1526 if (wPreferences.align_menus) {
1527 y = menu->frame_y;
1528 } else {
1529 y = menu->frame_y + menu->entry_height*entry_no;
1530 if (menu->flags.titled)
1531 y += menu->frame->top_width;
1532 if (menu->cascades[entry->cascade]->flags.titled)
1533 y -= menu->cascades[entry->cascade]->frame->top_width;
1536 wMenuMapAt(menu->cascades[entry->cascade], x, y, False);
1537 menu->cascades[entry->cascade]->parent = menu;
1538 } else {
1539 return;
1542 paintEntry(menu, entry_no, True);
1547 static WMenu*
1548 findMenu(WScreen *scr, int *x_ret, int *y_ret)
1550 WMenu *menu;
1551 WObjDescriptor *desc;
1552 Window root_ret, win, junk_win;
1553 int x, y, wx, wy;
1554 unsigned int mask;
1556 XQueryPointer(dpy, scr->root_win, &root_ret, &win, &x, &y, &wx, &wy,
1557 &mask);
1559 if (win==None) return NULL;
1561 if (XFindContext(dpy, win, wWinContext, (XPointer *)&desc)==XCNOENT)
1562 return NULL;
1564 if (desc->parent_type == WCLASS_MENU) {
1565 menu = (WMenu*)desc->parent;
1566 XTranslateCoordinates(dpy, root_ret, menu->menu->window, wx, wy,
1567 x_ret, y_ret, &junk_win);
1568 return menu;
1570 return NULL;
1576 static void
1577 closeCascade(WMenu *menu)
1579 WMenu *parent=menu->parent;
1581 if (menu->flags.brother
1582 || (!menu->flags.buttoned
1583 && (!menu->flags.app_menu||menu->parent!=NULL))) {
1585 selectEntry(menu, -1);
1586 XSync(dpy, 0);
1587 #if (MENU_BLINK_DELAY > 2)
1588 wusleep(MENU_BLINK_DELAY/2);
1589 #endif
1590 wMenuUnmap(menu);
1591 while (parent!=NULL
1592 && (parent->parent!=NULL || !parent->flags.app_menu
1593 || parent->flags.brother)
1594 && !parent->flags.buttoned) {
1595 selectEntry(parent, -1);
1596 wMenuUnmap(parent);
1597 parent = parent->parent;
1599 if (parent)
1600 selectEntry(parent, -1);
1605 static void
1606 closeBrotherCascadesOf(WMenu *menu)
1608 WMenu *tmp;
1609 int i;
1611 for (i=0; i<menu->cascade_no; i++) {
1612 if (menu->cascades[i]->flags.brother) {
1613 tmp = menu->cascades[i];
1614 } else {
1615 tmp = menu->cascades[i]->brother;
1617 if (tmp->flags.mapped) {
1618 selectEntry(tmp->parent, -1);
1619 closeBrotherCascadesOf(tmp);
1620 break;
1626 #define getEntryAt(menu, x, y) ((y)<0 ? -1 : (y)/(menu->entry_height))
1629 static WMenu*
1630 parentMenu(WMenu *menu)
1632 WMenu *parent;
1633 WMenuEntry *entry;
1635 if (menu->flags.buttoned)
1636 return menu;
1638 while (menu->parent && menu->parent->flags.mapped) {
1639 parent = menu->parent;
1640 if (parent->selected_entry < 0)
1641 break;
1642 entry = parent->entries[parent->selected_entry];
1643 if (!entry->flags.enabled || entry->cascade<0 || !parent->cascades ||
1644 parent->cascades[entry->cascade] != menu)
1645 break;
1646 menu = parent;
1647 if (menu->flags.buttoned)
1648 break;
1651 return menu;
1657 * Will raise the passed menu, if submenu = 0
1658 * If submenu > 0 will also raise all mapped submenus
1659 * until the first buttoned one
1660 * If submenu < 0 will also raise all mapped parent menus
1661 * until the first buttoned one
1664 static void
1665 raiseMenus(WMenu *menu, int submenus)
1667 WMenu *submenu;
1668 int i;
1670 if(!menu) return;
1672 wRaiseFrame(menu->frame->core);
1674 if (submenus>0 && menu->selected_entry>=0) {
1675 i = menu->entries[menu->selected_entry]->cascade;
1676 if (i>=0 && menu->cascades) {
1677 submenu = menu->cascades[i];
1678 if (submenu->flags.mapped && !submenu->flags.buttoned)
1679 raiseMenus(submenu, submenus);
1682 if (submenus<0 && !menu->flags.buttoned &&
1683 menu->parent && menu->parent->flags.mapped)
1684 raiseMenus(menu->parent, submenus);
1688 WMenu*
1689 wMenuUnderPointer(WScreen *screen)
1691 WObjDescriptor *desc;
1692 Window root_ret, win;
1693 int dummy;
1694 unsigned int mask;
1696 XQueryPointer(dpy, screen->root_win, &root_ret, &win, &dummy, &dummy,
1697 &dummy, &dummy, &mask);
1699 if (win==None) return NULL;
1701 if (XFindContext(dpy, win, wWinContext, (XPointer *)&desc)==XCNOENT)
1702 return NULL;
1704 if (desc->parent_type == WCLASS_MENU)
1705 return (WMenu *)desc->parent;
1706 return NULL;
1712 static void
1713 getPointerPosition(WScreen *scr, int *x, int *y)
1715 Window root_ret, win;
1716 int wx, wy;
1717 unsigned int mask;
1719 XQueryPointer(dpy, scr->root_win, &root_ret, &win, x, y, &wx, &wy, &mask);
1723 static void
1724 getScrollAmount(WMenu *menu, int *hamount, int *vamount)
1726 WScreen *scr = menu->menu->screen_ptr;
1727 int menuX1 = menu->frame_x;
1728 int menuY1 = menu->frame_y;
1729 int menuX2 = menu->frame_x + MENUW(menu);
1730 int menuY2 = menu->frame_y + MENUH(menu);
1731 int screenW = scr->scr_width;
1732 int screenH = scr->scr_height;
1733 int xroot, yroot;
1735 *hamount = 0;
1736 *vamount = 0;
1738 getPointerPosition(scr, &xroot, &yroot);
1740 #ifdef VIRTUAL_DESKTOP
1741 if (wPreferences.vedge_thickness) {
1742 if (xroot <= wPreferences.vedge_thickness + 1 && menuX1 < wPreferences.vedge_thickness) {
1743 /* scroll to the right */
1744 *hamount = WMIN(MENU_SCROLL_STEP, abs(menuX1));
1746 } else if (xroot >= screenW-2-wPreferences.vedge_thickness && menuX2 > screenW-1-wPreferences.vedge_thickness) {
1747 /* scroll to the left */
1748 *hamount = WMIN(MENU_SCROLL_STEP, abs(menuX2-screenW-1));
1750 if (*hamount==0)
1751 *hamount = 1;
1753 *hamount = -*hamount;
1755 } else
1756 #endif
1758 if (xroot <= 1 && menuX1 < 0) {
1759 /* scroll to the right */
1760 *hamount = WMIN(MENU_SCROLL_STEP, abs(menuX1));
1762 } else if (xroot >= screenW-2 && menuX2 > screenW-1) {
1763 /* scroll to the left */
1764 *hamount = WMIN(MENU_SCROLL_STEP, abs(menuX2-screenW-1));
1766 if (*hamount==0)
1767 *hamount = 1;
1769 *hamount = -*hamount;
1772 if (yroot <= 1 && menuY1 < 0) {
1773 /* scroll down */
1774 *vamount = WMIN(MENU_SCROLL_STEP, abs(menuY1));
1776 } else if (yroot >= screenH-2 && menuY2 > screenH-1) {
1777 /* scroll up */
1778 *vamount = WMIN(MENU_SCROLL_STEP, abs(menuY2-screenH-2));
1780 *vamount = -*vamount;
1785 static void
1786 dragScrollMenuCallback(void *data)
1788 WMenu *menu = (WMenu*)data;
1789 WScreen *scr = menu->menu->screen_ptr;
1790 WMenu *parent = parentMenu(menu);
1791 int hamount, vamount;
1792 int x, y;
1793 int newSelectedEntry;
1795 getScrollAmount(menu, &hamount, &vamount);
1798 if (hamount != 0 || vamount != 0) {
1799 wMenuMove(parent, parent->frame_x + hamount,
1800 parent->frame_y + vamount, True);
1801 if (findMenu(scr, &x, &y)) {
1802 newSelectedEntry = getEntryAt(menu, x, y);
1803 selectEntry(menu, newSelectedEntry);
1804 } else {
1805 /* Pointer fell outside of menu. If the selected entry is
1806 * not a submenu, unselect it */
1807 if (menu->selected_entry >= 0
1808 && menu->entries[menu->selected_entry]->cascade<0)
1809 selectEntry(menu, -1);
1810 newSelectedEntry = 0;
1813 /* paranoid check */
1814 if (newSelectedEntry >= 0) {
1815 /* keep scrolling */
1816 menu->timer = WMAddTimerHandler(MENU_SCROLL_DELAY,
1817 dragScrollMenuCallback, menu);
1818 } else {
1819 menu->timer = NULL;
1821 } else {
1822 /* don't need to scroll anymore */
1823 menu->timer = NULL;
1824 if (findMenu(scr, &x, &y)) {
1825 newSelectedEntry = getEntryAt(menu, x, y);
1826 selectEntry(menu, newSelectedEntry);
1832 static void
1833 scrollMenuCallback(void *data)
1835 WMenu *menu = (WMenu*)data;
1836 WMenu *parent = parentMenu(menu);
1837 int hamount = 0; /* amount to scroll */
1838 int vamount = 0;
1840 getScrollAmount(menu, &hamount, &vamount);
1842 if (hamount != 0 || vamount != 0) {
1843 wMenuMove(parent, parent->frame_x + hamount,
1844 parent->frame_y + vamount, True);
1846 /* keep scrolling */
1847 menu->timer = WMAddTimerHandler(MENU_SCROLL_DELAY,
1848 scrollMenuCallback, menu);
1849 } else {
1850 /* don't need to scroll anymore */
1851 menu->timer = NULL;
1857 #define MENU_SCROLL_BORDER 5
1859 static int
1860 isPointNearBoder(WMenu *menu, int x, int y)
1862 int menuX1 = menu->frame_x;
1863 int menuY1 = menu->frame_y;
1864 int menuX2 = menu->frame_x + MENUW(menu);
1865 int menuY2 = menu->frame_y + MENUH(menu);
1866 int scrXe = menu->menu->screen_ptr->scr_width-1;
1867 int scrYe = menu->menu->screen_ptr->scr_height-1;
1868 int flag = 0;
1870 if (x >= menuX1 && x <= menuX2 && (y < MENU_SCROLL_BORDER
1871 || y > scrYe-MENU_SCROLL_BORDER))
1872 flag = 1;
1873 else if (y >= menuY1 && y <= menuY2 && (x < MENU_SCROLL_BORDER
1874 || x > scrXe-MENU_SCROLL_BORDER))
1875 flag = 1;
1877 return flag;
1881 typedef struct _delay {
1882 WWindow *wwin;
1883 WMenu *menu;
1884 int ox,oy;
1885 } _delay;
1888 static void
1889 _leaving(_delay *dl)
1891 wMenuMove(dl->menu, dl->ox, dl->oy, True);
1892 dl->menu->jump_back=NULL;
1893 dl->menu->menu->screen_ptr->flags.jump_back_pending = 0;
1894 wfree(dl);
1898 void
1899 wMenuScroll(WMenu *menu, XEvent *event)
1901 WMenu *smenu;
1902 WMenu *omenu = parentMenu(menu);
1903 WScreen *scr = menu->frame->screen_ptr;
1904 int done = 0;
1905 int jump_back = 0;
1906 int old_frame_x = omenu->frame_x;
1907 int old_frame_y = omenu->frame_y;
1908 XEvent ev;
1910 if (omenu->jump_back)
1911 WMDeleteTimerWithClientData(omenu->jump_back);
1914 if ((/*omenu->flags.buttoned &&*/ !wPreferences.wrap_menus)
1915 || omenu->flags.app_menu) {
1916 jump_back = 1;
1919 if (!wPreferences.wrap_menus)
1920 raiseMenus(omenu, True);
1921 else
1922 raiseMenus(menu, False);
1924 if (!menu->timer)
1925 scrollMenuCallback(menu);
1927 while(!done) {
1928 int x, y, on_border, on_x_edge, on_y_edge, on_title;
1930 WMNextEvent(dpy, &ev);
1931 switch (ev.type) {
1932 case EnterNotify:
1933 WMHandleEvent(&ev);
1934 case MotionNotify:
1935 x = (ev.type==MotionNotify) ? ev.xmotion.x_root : ev.xcrossing.x_root;
1936 y = (ev.type==MotionNotify) ? ev.xmotion.y_root : ev.xcrossing.y_root;
1938 /* on_border is != 0 if the pointer is between the menu
1939 * and the screen border and is close enough to the border */
1940 on_border = isPointNearBoder(menu, x, y);
1942 smenu = wMenuUnderPointer(scr);
1944 if ((smenu==NULL && !on_border) || (smenu && parentMenu(smenu)!=omenu)) {
1945 done = 1;
1946 break;
1949 on_x_edge = x <= 1 || x >= scr->scr_width - 2;
1950 on_y_edge = y <= 1 || y >= scr->scr_height - 2;
1951 on_border = on_x_edge || on_y_edge;
1953 if (!on_border && !jump_back) {
1954 done = 1;
1955 break;
1958 if (menu->timer && (smenu!=menu || (!on_y_edge && !on_x_edge))) {
1959 WMDeleteTimerHandler(menu->timer);
1960 menu->timer = NULL;
1963 if (smenu != NULL)
1964 menu = smenu;
1966 if (!menu->timer)
1967 scrollMenuCallback(menu);
1968 break;
1969 case ButtonPress:
1970 /* True if we push on title, or drag the omenu to other position */
1971 on_title = ev.xbutton.x_root >= omenu->frame_x &&
1972 ev.xbutton.x_root <= omenu->frame_x + MENUW(omenu) &&
1973 ev.xbutton.y_root >= omenu->frame_y &&
1974 ev.xbutton.y_root <= omenu->frame_y + omenu->frame->top_width;
1975 WMHandleEvent(&ev);
1976 smenu = wMenuUnderPointer(scr);
1977 if (smenu == NULL || (smenu && smenu->flags.buttoned && smenu != omenu))
1978 done = 1;
1979 else if (smenu==omenu && on_title) {
1980 jump_back = 0;
1981 done = 1;
1983 break;
1984 case KeyPress:
1985 done = 1;
1986 default:
1987 WMHandleEvent(&ev);
1988 break;
1992 if (menu->timer) {
1993 WMDeleteTimerHandler(menu->timer);
1994 menu->timer = NULL;
1997 if (jump_back) {
1998 _delay *delayer;
1999 if (!omenu->jump_back) {
2000 delayer=wmalloc(sizeof(_delay));
2001 delayer->menu=omenu;
2002 delayer->ox=old_frame_x;
2003 delayer->oy=old_frame_y;
2004 omenu->jump_back=delayer;
2005 scr->flags.jump_back_pending = 1;
2007 else delayer = omenu->jump_back;
2008 WMAddTimerHandler(MENU_JUMP_BACK_DELAY,(WMCallback*)_leaving, delayer);
2014 static void
2015 menuExpose(WObjDescriptor *desc, XEvent *event)
2017 wMenuPaint(desc->parent);
2020 typedef struct {
2021 int *delayed_select;
2022 WMenu *menu;
2023 WMHandlerID magic;
2024 } delay_data;
2027 static void
2028 delaySelection(void *data)
2030 delay_data *d = (delay_data*)data;
2031 int x, y, entry_no;
2032 WMenu *menu;
2034 d->magic = NULL;
2036 menu = findMenu(d->menu->menu->screen_ptr, &x, &y);
2037 if (menu && (d->menu == menu || d->delayed_select)) {
2038 entry_no = getEntryAt(menu, x, y);
2039 selectEntry(menu, entry_no);
2041 if (d->delayed_select)
2042 *(d->delayed_select) = 0;
2046 static void
2047 menuMouseDown(WObjDescriptor *desc, XEvent *event)
2049 XButtonEvent *bev = &event->xbutton;
2050 WMenu *menu = desc->parent;
2051 WMenu *smenu;
2052 WScreen *scr=menu->frame->screen_ptr;
2053 WMenuEntry *entry=NULL;
2054 XEvent ev;
2055 int close_on_exit=0;
2056 int done=0;
2057 int delayed_select = 0;
2058 int entry_no;
2059 int x, y;
2060 int prevx, prevy;
2061 int old_frame_x = 0;
2062 int old_frame_y = 0;
2063 delay_data d_data = {NULL, NULL, NULL};
2065 if (!wPreferences.wrap_menus) {
2066 smenu = parentMenu(menu);
2067 old_frame_x = smenu->frame_x;
2068 old_frame_y = smenu->frame_y;
2069 } else if (event->xbutton.window == menu->frame->core->window) {
2070 /* This is true if the menu was launched with right click on root window */
2071 delayed_select = 1;
2072 d_data.delayed_select = &delayed_select;
2073 d_data.menu = menu;
2074 d_data.magic = WMAddTimerHandler(wPreferences.dblclick_time,
2075 delaySelection, &d_data);
2078 if (menu->flags.inside_handler) {
2079 return;
2081 menu->flags.inside_handler = 1;
2083 wRaiseFrame(menu->frame->core);
2085 close_on_exit = (bev->send_event || menu->flags.brother);
2087 smenu = findMenu(scr, &x, &y);
2088 if (!smenu) {
2089 x = -1;
2090 y = -1;
2091 } else {
2092 menu = smenu;
2095 if (menu->flags.editing) {
2096 goto byebye;
2098 entry_no = getEntryAt(menu, x, y);
2099 if (entry_no>=0) {
2100 entry = menu->entries[entry_no];
2102 if (!close_on_exit && (bev->state & ControlMask) && smenu
2103 && entry->flags.editable) {
2104 editEntry(smenu, entry);
2105 goto byebye;
2106 } else if (bev->state & ControlMask) {
2107 goto byebye;
2110 if (entry->flags.enabled && entry->cascade>=0 && menu->cascades) {
2111 WMenu *submenu = menu->cascades[entry->cascade];
2112 /* map cascade */
2113 if (submenu->flags.mapped && !submenu->flags.buttoned &&
2114 menu->selected_entry!=entry_no) {
2115 wMenuUnmap(submenu);
2117 if (!submenu->flags.mapped && !delayed_select) {
2118 selectEntry(menu, entry_no);
2119 } else if (!submenu->flags.buttoned) {
2120 selectEntry(menu, -1);
2123 } else if (!delayed_select) {
2124 selectEntry(menu, entry_no);
2127 if (!wPreferences.wrap_menus && !wPreferences.scrollable_menus) {
2128 if (!menu->timer)
2129 dragScrollMenuCallback(menu);
2133 prevx = bev->x_root;
2134 prevy = bev->y_root;
2135 while (!done) {
2136 int x, y;
2138 XAllowEvents(dpy, AsyncPointer|SyncPointer, CurrentTime);
2140 WMMaskEvent(dpy, ExposureMask|ButtonMotionMask|ButtonReleaseMask
2141 |ButtonPressMask, &ev);
2142 switch (ev.type) {
2143 case MotionNotify:
2144 smenu = findMenu(scr, &x, &y);
2146 if (smenu == NULL) {
2147 /* moved mouse out of menu */
2149 if (!delayed_select && d_data.magic) {
2150 WMDeleteTimerHandler(d_data.magic);
2151 d_data.magic = NULL;
2153 if (menu==NULL
2154 || (menu->selected_entry>=0
2155 && menu->entries[menu->selected_entry]->cascade>=0)) {
2156 prevx = ev.xmotion.x_root;
2157 prevy = ev.xmotion.y_root;
2159 break;
2161 selectEntry(menu, -1);
2162 menu = smenu;
2163 prevx = ev.xmotion.x_root;
2164 prevy = ev.xmotion.y_root;
2165 break;
2166 } else if (menu && menu!=smenu
2167 && (menu->selected_entry<0
2168 || menu->entries[menu->selected_entry]->cascade<0)) {
2169 selectEntry(menu, -1);
2171 if (!delayed_select && d_data.magic) {
2172 WMDeleteTimerHandler(d_data.magic);
2173 d_data.magic = NULL;
2175 } else {
2177 /* hysteresis for item selection */
2179 /* check if the motion was to the side, indicating that
2180 * the user may want to cross to a submenu */
2181 if (!delayed_select && menu) {
2182 int dx;
2183 Bool moved_to_submenu;/* moved to direction of submenu */
2185 dx = abs(prevx - ev.xmotion.x_root);
2187 moved_to_submenu = False;
2188 if (dx > 0 /* if moved enough to the side */
2189 /* maybe a open submenu */
2190 && menu->selected_entry>=0
2191 /* moving to the right direction */
2192 && (wPreferences.align_menus
2193 || ev.xmotion.y_root >= prevy)) {
2194 int index;
2196 index = menu->entries[menu->selected_entry]->cascade;
2197 if (index>=0) {
2198 if (menu->cascades[index]->frame_x>menu->frame_x) {
2199 if (prevx < ev.xmotion.x_root)
2200 moved_to_submenu = True;
2201 } else {
2202 if (prevx > ev.xmotion.x_root)
2203 moved_to_submenu = True;
2209 if (menu != smenu) {
2210 if (d_data.magic) {
2211 WMDeleteTimerHandler(d_data.magic);
2213 d_data.magic = NULL;
2214 } else if (moved_to_submenu) {
2215 /* while we are moving, postpone the selection */
2216 if (d_data.magic) {
2217 WMDeleteTimerHandler(d_data.magic);
2219 d_data.delayed_select = NULL;
2220 d_data.menu = menu;
2221 d_data.magic = WMAddTimerHandler(MENU_SELECT_DELAY,
2222 delaySelection,
2223 &d_data);
2224 prevx = ev.xmotion.x_root;
2225 prevy = ev.xmotion.y_root;
2226 break;
2227 } else {
2228 if (d_data.magic)
2229 WMDeleteTimerHandler(d_data.magic);
2230 d_data.magic = NULL;
2234 prevx = ev.xmotion.x_root;
2235 prevy = ev.xmotion.y_root;
2236 if (menu!=smenu) {
2237 /* pointer crossed menus */
2238 if (menu && menu->timer) {
2239 WMDeleteTimerHandler(menu->timer);
2240 menu->timer = NULL;
2242 if (smenu)
2243 dragScrollMenuCallback(smenu);
2245 menu = smenu;
2246 if (!menu->timer)
2247 dragScrollMenuCallback(menu);
2249 if (!delayed_select) {
2250 entry_no = getEntryAt(menu, x, y);
2251 if (entry_no>=0) {
2252 entry = menu->entries[entry_no];
2253 if (entry->flags.enabled && entry->cascade>=0 &&
2254 menu->cascades) {
2255 WMenu *submenu = menu->cascades[entry->cascade];
2256 if (submenu->flags.mapped && !submenu->flags.buttoned
2257 && menu->selected_entry!=entry_no) {
2258 wMenuUnmap(submenu);
2262 selectEntry(menu, entry_no);
2264 break;
2266 case ButtonPress:
2267 break;
2269 case ButtonRelease:
2270 if (ev.xbutton.button == event->xbutton.button)
2271 done=1;
2272 break;
2274 case Expose:
2275 WMHandleEvent(&ev);
2276 break;
2280 if (menu && menu->timer) {
2281 WMDeleteTimerHandler(menu->timer);
2282 menu->timer = NULL;
2284 if (d_data.magic!=NULL)
2285 WMDeleteTimerHandler(d_data.magic);
2287 if (menu && menu->selected_entry>=0) {
2288 entry = menu->entries[menu->selected_entry];
2289 if (entry->callback!=NULL && entry->flags.enabled
2290 && entry->cascade < 0) {
2291 /* blink and erase menu selection */
2292 #if (MENU_BLINK_DELAY > 0)
2293 int sel = menu->selected_entry;
2294 int i;
2296 for (i=0; i<MENU_BLINK_COUNT; i++) {
2297 paintEntry(menu, sel, False);
2298 XSync(dpy, 0);
2299 wusleep(MENU_BLINK_DELAY);
2300 paintEntry(menu, sel, True);
2301 XSync(dpy, 0);
2302 wusleep(MENU_BLINK_DELAY);
2304 #endif
2305 /* unmap the menu, it's parents and call the callback */
2306 if (!menu->flags.buttoned &&
2307 (!menu->flags.app_menu||menu->parent!=NULL)) {
2308 closeCascade(menu);
2309 } else {
2310 selectEntry(menu, -1);
2312 (*entry->callback)(menu, entry);
2314 /* If the user double clicks an entry, the entry will
2315 * be executed twice, which is not good for things like
2316 * the root menu. So, ignore any clicks that were generated
2317 * while the entry was being executed */
2318 while (XCheckTypedWindowEvent(dpy, menu->menu->window,
2319 ButtonPress, &ev));
2320 } else if (entry->callback!=NULL && entry->cascade<0) {
2321 selectEntry(menu, -1);
2322 } else {
2323 if (entry->cascade>=0 && menu->cascades
2324 && menu->cascades[entry->cascade]->flags.brother) {
2325 selectEntry(menu, -1);
2330 if (((WMenu*)desc->parent)->flags.brother || close_on_exit || !smenu)
2331 closeCascade(desc->parent);
2333 /* close the cascade windows that should not remain opened */
2334 closeBrotherCascadesOf(desc->parent);
2336 if (!wPreferences.wrap_menus)
2337 wMenuMove(parentMenu(desc->parent), old_frame_x, old_frame_y, True);
2339 byebye:
2340 ((WMenu*)desc->parent)->flags.inside_handler = 0;
2344 void
2345 wMenuMove(WMenu *menu, int x, int y, int submenus)
2347 WMenu *submenu;
2348 int i;
2350 if (!menu) return;
2352 menu->frame_x = x;
2353 menu->frame_y = y;
2354 XMoveWindow(dpy, menu->frame->core->window, x, y);
2356 if (submenus>0 && menu->selected_entry>=0) {
2357 i = menu->entries[menu->selected_entry]->cascade;
2359 if (i>=0 && menu->cascades) {
2360 submenu = menu->cascades[i];
2361 if (submenu->flags.mapped && !submenu->flags.buttoned) {
2362 if (wPreferences.align_menus) {
2363 wMenuMove(submenu, x + MENUW(menu), y, submenus);
2364 } else {
2365 wMenuMove(submenu, x+ MENUW(menu),
2366 y + submenu->entry_height*menu->selected_entry,
2367 submenus);
2372 if (submenus<0 && menu->parent!=NULL && menu->parent->flags.mapped &&
2373 !menu->parent->flags.buttoned) {
2374 if (wPreferences.align_menus) {
2375 wMenuMove(menu->parent, x - MENUW(menu->parent), y, submenus);
2376 } else {
2377 wMenuMove(menu->parent, x - MENUW(menu->parent), menu->frame_y
2378 - menu->parent->entry_height*menu->parent->selected_entry,
2379 submenus);
2385 static void
2386 changeMenuLevels(WMenu *menu, int lower)
2388 int i;
2390 if (!lower) {
2391 ChangeStackingLevel(menu->frame->core, (!menu->parent ? WMMainMenuLevel
2392 : WMSubmenuLevel));
2393 wRaiseFrame(menu->frame->core);
2394 menu->flags.lowered = 0;
2395 } else {
2396 ChangeStackingLevel(menu->frame->core, WMNormalLevel);
2397 wLowerFrame(menu->frame->core);
2398 menu->flags.lowered = 1;
2400 for (i=0; i<menu->cascade_no; i++) {
2401 if (menu->cascades[i]
2402 && !menu->cascades[i]->flags.buttoned
2403 && menu->cascades[i]->flags.lowered!=lower) {
2404 changeMenuLevels(menu->cascades[i], lower);
2411 static void
2412 menuTitleDoubleClick(WCoreWindow *sender, void *data, XEvent *event)
2414 WMenu *menu = data;
2415 int lower;
2417 if (event->xbutton.state & MOD_MASK) {
2418 if (menu->flags.lowered) {
2419 lower = 0;
2420 } else {
2421 lower = 1;
2423 changeMenuLevels(menu, lower);
2428 static void
2429 menuTitleMouseDown(WCoreWindow *sender, void *data, XEvent *event)
2431 WMenu *menu = data;
2432 WMenu *tmp;
2433 XEvent ev;
2434 int x=menu->frame_x, y=menu->frame_y;
2435 int dx=event->xbutton.x_root, dy=event->xbutton.y_root;
2436 int i, lower;
2437 Bool started;
2439 /* can't touch the menu copy */
2440 if (menu->flags.brother)
2441 return;
2443 if (event->xbutton.button != Button1 && event->xbutton.button != Button2)
2444 return;
2446 if (event->xbutton.state & MOD_MASK) {
2447 wLowerFrame(menu->frame->core);
2448 lower = 1;
2449 } else {
2450 wRaiseFrame(menu->frame->core);
2451 lower = 0;
2453 tmp = menu;
2455 /* lower/raise all submenus */
2456 while (1) {
2457 if (tmp->selected_entry>=0 && tmp->cascades
2458 && tmp->entries[tmp->selected_entry]->cascade>=0) {
2459 tmp = tmp->cascades[tmp->entries[tmp->selected_entry]->cascade];
2460 if (!tmp || !tmp->flags.mapped)
2461 break;
2462 if (lower)
2463 wLowerFrame(tmp->frame->core);
2464 else
2465 wRaiseFrame(tmp->frame->core);
2466 } else {
2467 break;
2471 /* tear off the menu if it's a root menu or a cascade
2472 application menu */
2473 if (!menu->flags.buttoned && !menu->flags.brother
2474 && (!menu->flags.app_menu||menu->parent!=NULL)) {
2475 menu->flags.buttoned=1;
2476 wFrameWindowShowButton(menu->frame, WFF_RIGHT_BUTTON);
2477 if (menu->parent) {
2478 /* turn off selected menu entry in parent menu */
2479 selectEntry(menu->parent, -1);
2481 /* make parent map the copy in place of the original */
2482 for (i=0; i<menu->parent->cascade_no; i++) {
2483 if (menu->parent->cascades[i] == menu) {
2484 menu->parent->cascades[i] = menu->brother;
2485 break;
2491 started = False;
2492 while(1) {
2493 WMMaskEvent(dpy, ButtonMotionMask|ButtonReleaseMask|ButtonPressMask
2494 |ExposureMask, &ev);
2495 switch (ev.type) {
2496 case MotionNotify:
2497 if (started) {
2498 x += ev.xmotion.x_root - dx;
2499 y += ev.xmotion.y_root - dy;
2500 dx = ev.xmotion.x_root;
2501 dy = ev.xmotion.y_root;
2502 wMenuMove(menu, x, y, True);
2503 } else {
2504 if (abs(ev.xmotion.x_root - dx) > MOVE_THRESHOLD
2505 || abs(ev.xmotion.y_root - dy) > MOVE_THRESHOLD) {
2506 started = True;
2507 XGrabPointer(dpy, menu->frame->titlebar->window, False,
2508 ButtonMotionMask|ButtonReleaseMask
2509 |ButtonPressMask,
2510 GrabModeAsync, GrabModeAsync, None,
2511 wCursor[WCUR_MOVE], CurrentTime);
2514 break;
2516 case ButtonPress:
2517 break;
2519 case ButtonRelease:
2520 if (ev.xbutton.button != event->xbutton.button)
2521 break;
2522 XUngrabPointer(dpy, CurrentTime);
2523 return;
2525 default:
2526 WMHandleEvent(&ev);
2527 break;
2533 *----------------------------------------------------------------------
2534 * menuCloseClick--
2535 * Handles mouse click on the close button of menus. The menu is
2536 * closed when the button is clicked.
2538 * Side effects:
2539 * The closed menu is reinserted at it's parent menus
2540 * cascade list.
2541 *----------------------------------------------------------------------
2543 static void
2544 menuCloseClick(WCoreWindow *sender, void *data, XEvent *event)
2546 WMenu *menu = (WMenu*)data;
2547 WMenu *parent = menu->parent;
2548 int i;
2550 if (parent) {
2551 for (i=0; i<parent->cascade_no; i++) {
2552 /* find the entry that points to the copy */
2553 if (parent->cascades[i] == menu->brother) {
2554 /* make it point to the original */
2555 parent->cascades[i] = menu;
2556 menu->parent = parent;
2557 break;
2561 wMenuUnmap(menu);
2565 static void
2566 saveMenuInfo(proplist_t dict, WMenu *menu, proplist_t key)
2568 proplist_t value, list;
2569 char buffer[256];
2571 sprintf(buffer, "%i,%i", menu->frame_x, menu->frame_y);
2572 value = PLMakeString(buffer);
2573 list = PLMakeArrayFromElements(value, NULL);
2574 if (menu->flags.lowered)
2575 PLAppendArrayElement(list, PLMakeString("lowered"));
2576 PLInsertDictionaryEntry(dict, key, list);
2577 PLRelease(value);
2578 PLRelease(list);
2582 void
2583 wMenuSaveState(WScreen *scr)
2585 proplist_t menus, key;
2586 int save_menus = 0;
2588 menus = PLMakeDictionaryFromEntries(NULL, NULL, NULL);
2590 #ifndef LITE
2591 if (scr->switch_menu && scr->switch_menu->flags.buttoned) {
2592 key = PLMakeString("SwitchMenu");
2593 saveMenuInfo(menus, scr->switch_menu, key);
2594 PLRelease(key);
2595 save_menus = 1;
2598 if (saveMenuRecurs(menus, scr, scr->root_menu))
2599 save_menus = 1;
2601 #endif /* !LITE */
2602 if (scr->workspace_menu && scr->workspace_menu->flags.buttoned) {
2603 key = PLMakeString("WorkspaceMenu");
2604 saveMenuInfo(menus, scr->workspace_menu, key);
2605 PLRelease(key);
2606 save_menus = 1;
2609 if (save_menus) {
2610 key = PLMakeString("Menus");
2611 PLInsertDictionaryEntry(scr->session_state, key, menus);
2612 PLRelease(key);
2614 PLRelease(menus);
2618 #ifndef LITE
2620 static Bool
2621 getMenuPath(WMenu *menu, char *buffer, int bufSize)
2623 Bool ok = True;
2624 int len = 0;
2626 if (!menu->flags.titled || !menu->frame->title[0])
2627 return False;
2629 len = strlen(menu->frame->title);
2630 if (len >= bufSize)
2631 return False;
2633 if (menu->parent) {
2634 ok = getMenuPath(menu->parent, buffer, bufSize - len - 1);
2635 if (!ok)
2636 return False;
2639 strcat(buffer, "\\");
2640 strcat(buffer, menu->frame->title);
2642 return True;
2646 static Bool
2647 saveMenuRecurs(proplist_t menus, WScreen *scr, WMenu *menu)
2649 proplist_t key;
2650 int save_menus = 0, i;
2651 char buffer[512];
2652 Bool ok = True;
2655 if (menu->flags.brother)
2656 menu = menu->brother;
2658 if (menu->flags.buttoned && menu != scr->switch_menu) {
2660 buffer[0] = '\0';
2661 ok = getMenuPath(menu, buffer, 510);
2663 if (ok) {
2664 key = PLMakeString(buffer);
2665 saveMenuInfo(menus, menu, key);
2666 PLRelease(key);
2667 save_menus = 1;
2671 if (ok) {
2672 for (i = 0; i < menu->cascade_no; i++) {
2673 if (saveMenuRecurs(menus, scr, menu->cascades[i]))
2674 save_menus = 1;
2677 return save_menus;
2679 #endif /* !LITE */
2682 #define COMPLAIN(key) wwarning(_("bad value in menus state info:%s"), key)
2685 static Bool
2686 getMenuInfo(proplist_t info, int *x, int *y, Bool *lowered)
2688 proplist_t pos;
2690 *lowered = False;
2692 if (PLIsArray(info)) {
2693 proplist_t flags;
2694 pos = PLGetArrayElement(info, 0);
2695 flags = PLGetArrayElement(info, 1);
2696 if (flags != NULL && PLIsString(flags) && PLGetString(flags) != NULL
2697 && strcmp(PLGetString(flags), "lowered") == 0) {
2698 *lowered = True;
2700 } else {
2701 pos = info;
2704 if (pos != NULL && PLIsString(pos)) {
2705 if (sscanf(PLGetString(pos), "%i,%i", x, y)!=2)
2706 COMPLAIN("Position");
2707 } else {
2708 COMPLAIN("(position, flags...)");
2709 return False;
2712 return True;
2716 static int
2717 restoreMenu(WScreen *scr, proplist_t menu, int which)
2719 int x, y;
2720 Bool lowered = False;
2721 WMenu *pmenu = NULL;
2723 if (!menu)
2724 return False;
2726 if (!getMenuInfo(menu, &x, &y, &lowered))
2727 return False;
2730 #ifndef LITE
2731 if (which & WSS_SWITCHMENU) {
2732 OpenSwitchMenu(scr, x, y, False);
2733 pmenu = scr->switch_menu;
2735 #endif /* !LITE */
2737 if (pmenu) {
2738 int width = MENUW(pmenu);
2739 int height = MENUH(pmenu);
2741 if (lowered) {
2742 changeMenuLevels(pmenu, True);
2745 x = (x < -width) ? 0 : x;
2746 x = (x > scr->scr_width) ? scr->scr_width - width : x;
2747 y = (y < 0) ? 0 : y;
2748 y = (y > scr->scr_height) ? scr->scr_height - height : y;
2749 wMenuMove(pmenu, x, y, True);
2750 pmenu->flags.buttoned = 1;
2751 wFrameWindowShowButton(pmenu->frame, WFF_RIGHT_BUTTON);
2752 return True;
2754 return False;
2758 #ifndef LITE
2759 static int
2760 restoreMenuRecurs(WScreen *scr, proplist_t menus, WMenu *menu, char *path)
2762 proplist_t key, entry;
2763 char buffer[512];
2764 int i, x, y, res;
2765 Bool lowered;
2767 if (strlen(path) + strlen(menu->frame->title) > 510)
2768 return False;
2770 sprintf(buffer, "%s\\%s", path, menu->frame->title);
2771 key = PLMakeString(buffer);
2772 entry = PLGetDictionaryEntry(menus, key);
2773 res = False;
2775 if (entry && getMenuInfo(entry, &x, &y, &lowered)) {
2777 if (!menu->flags.mapped) {
2778 int width = MENUW(menu);
2779 int height = MENUH(menu);
2781 wMenuMapAt(menu, x, y, False);
2783 if (menu->parent) {
2784 /* make parent map the copy in place of the original */
2785 for (i=0; i<menu->parent->cascade_no; i++) {
2786 if (menu->parent->cascades[i] == menu) {
2787 menu->parent->cascades[i] = menu->brother;
2788 break;
2792 if (lowered) {
2793 changeMenuLevels(menu, True);
2795 x = (x < -width) ? 0 : x;
2796 x = (x > scr->scr_width) ? scr->scr_width - width : x;
2797 y = (y < 0) ? 0 : y;
2798 y = (y > scr->scr_height) ? scr->scr_height - height : y;
2799 wMenuMove(menu, x, y, True);
2800 menu->flags.buttoned = 1;
2801 wFrameWindowShowButton(menu->frame, WFF_RIGHT_BUTTON);
2802 res = True;
2806 PLRelease(key);
2808 for (i=0; i<menu->cascade_no; i++) {
2809 if (restoreMenuRecurs(scr, menus, menu->cascades[i], buffer) != False)
2810 res = True;
2813 return res;
2815 #endif /* !LITE */
2818 void
2819 wMenuRestoreState(WScreen *scr)
2821 proplist_t menus, menu, key, skey;
2823 key = PLMakeString("Menus");
2824 menus = PLGetDictionaryEntry(scr->session_state, key);
2825 PLRelease(key);
2827 if (!menus)
2828 return;
2830 /* restore menus */
2832 skey = PLMakeString("SwitchMenu");
2833 menu = PLGetDictionaryEntry(menus, skey);
2834 PLRelease(skey);
2835 restoreMenu(scr, menu, WSS_SWITCHMENU);
2837 #ifndef LITE
2838 if (!scr->root_menu) {
2839 OpenRootMenu(scr, scr->scr_width*2, 0, False);
2840 wMenuUnmap(scr->root_menu);
2842 restoreMenuRecurs(scr, menus, scr->root_menu, "");
2843 #endif /* !LITE */
2847 void
2848 OpenWorkspaceMenu(WScreen *scr, int x, int y)
2850 WMenu *menu, *parent;
2851 WMenuEntry *entry;
2853 #ifndef LITE
2854 if (!scr->root_menu) {
2855 OpenRootMenu(scr, scr->scr_width*2, 0, False);
2856 wMenuUnmap(scr->root_menu);
2858 #endif
2859 menu = scr->workspace_menu;
2860 if (menu) {
2861 if (menu->flags.mapped) {
2862 if (!menu->flags.buttoned) {
2863 wMenuUnmap(menu);
2864 parent = menu->parent;
2865 if (parent && parent->selected_entry >= 0) {
2866 entry = parent->entries[parent->selected_entry];
2867 if (parent->cascades[entry->cascade] == menu) {
2868 selectEntry(parent, -1);
2869 wMenuMapAt(menu, x, y, False);
2872 } else {
2873 wRaiseFrame(menu->frame->core);
2874 wMenuMapCopyAt(menu, x, y);
2876 } else {
2877 wMenuMapAt(menu, x, y, False);