fix solid color for framewin with plugin
[wmaker-crm.git] / src / menu.c
bloba89cef0ef229fce649e3cf3a845bdf3f11a3401e
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];
735 if (!menu->flags.realized) return;
736 h = menu->entry_height;
737 w = menu->menu->width;
738 y = index * h;
740 light = scr->menu_item_auxtexture->light_gc;
741 dim = scr->menu_item_auxtexture->dim_gc;
742 dark = scr->menu_item_auxtexture->dark_gc;
744 if (wPreferences.menu_style == MS_FLAT && menu->entry_no > 1) {
745 if (index == 0)
746 type = F_TOP;
747 else if (index == menu->entry_no - 1)
748 type = F_BOTTOM;
749 else
750 type = F_NONE;
751 } else {
752 type = F_NORMAL;
755 /* paint background */
756 if (selected) {
757 XSetForeground(dpy, scr->select_menu_gc, scr->select_pixel);
758 XFillRectangle(dpy, win, scr->select_menu_gc, 1, y+1, w-2, h-3);
759 if (scr->menu_item_texture->any.type == WTEX_SOLID)
760 drawFrame(scr, win, y, w, h, type);
761 } else {
762 #ifdef DRAWSTRING_PLUGIN
763 if (scr->menu_item_texture->any.type == WTEX_SOLID) {
764 XClearArea(dpy, win, 0, y + 1, w - 1, h - 3, False);
765 drawFrame(scr, win, y, w, h, type);
766 } else {
768 * if the function is there, it responses for clearing area
769 * to reduce flickering
771 if (!scr->drawstring_func[W_STRING_MTEXT])
772 XClearArea(dpy, win, 0, y, w, h, False);
774 #else
775 if (scr->menu_item_texture->any.type == WTEX_SOLID) {
776 XClearArea(dpy, win, 0, y + 1, w - 1, h - 3, False);
777 /* draw the frame */
778 drawFrame(scr, win, y, w, h, type);
779 } else {
780 XClearArea(dpy, win, 0, y, w, h, False);
782 #endif
785 if (selected) {
786 textGC = scr->select_menu_gc;
787 if (entry->flags.enabled)
788 XSetForeground(dpy, textGC, scr->select_text_pixel);
789 else
790 XSetForeground(dpy, textGC, scr->dtext_pixel);
791 } else if (!entry->flags.enabled) {
792 textGC = scr->disabled_menu_entry_gc;
793 } else {
794 textGC = scr->menu_entry_gc;
796 /* draw text */
797 x = 5;
798 if (entry->flags.indicator)
799 x += MENU_INDICATOR_SPACE + 2;
801 #ifdef DRAWSTRING_PLUGIN
802 if (scr->drawstring_func[W_STRING_MTEXT]) {
803 Pixmap tmp_bg;
804 Pixmap *texture_data;
805 void **p;
806 int _y;
807 int tb = menu->entry_height; /* convert short into int */
809 _y = (wPreferences.menu_style == MS_NORMAL) ? 0 : y;
810 texture_data = menu->flags.brother ?
811 &menu->brother->menu_texture_data : &menu->menu_texture_data;
813 tmp_bg = XCreatePixmap(dpy, win, w, menu->entry_height,
814 DefaultDepth(dpy, DefaultScreen(dpy)));
816 if (scr->menu_item_texture->any.type == WTEX_SOLID) {
817 XFillRectangle(dpy, tmp_bg, scr->menu_item_texture->solid.normal_gc,
818 0, 0, w, h);
819 drawFrame(scr, tmp_bg, 0, w, h, type);
821 } else {
822 XCopyArea(dpy, *texture_data, tmp_bg, textGC,
823 0, _y, w, menu->entry_height, 0, 0);
826 if (selected) {
827 XSetForeground(dpy, scr->select_menu_gc, scr->select_pixel);
828 XFillRectangle(dpy, tmp_bg, scr->select_menu_gc, 1, 1, w-2, h-3);
832 p = wPluginPackData(6,
833 scr->drawstring_func[W_STRING_MTEXT]->data,
834 &tmp_bg,
835 &textGC,
836 scr->menu_entry_font,
837 &menu->frame->core->width,
838 &tb,
839 "extendable");
841 scr->drawstring_func[W_STRING_MTEXT]->proc.drawString(
842 scr->drawstring_func[W_STRING_MTEXT]->arg,
843 win,
844 x, y,
845 entry->text, strlen(entry->text), p);
847 XFreePixmap(dpy, tmp_bg);
849 free(p);
850 } else {
851 WMDrawString(scr->wmscreen, win, textGC, scr->menu_entry_font,
852 x, 3 + y + wPreferences.menu_text_clearance, entry->text, strlen(entry->text));
855 #else
856 WMDrawString(scr->wmscreen, win, textGC, scr->menu_entry_font,
857 x, 3 + y + wPreferences.menu_text_clearance, entry->text, strlen(entry->text));
858 #endif
860 if (entry->cascade>=0) {
861 /* draw the cascade indicator */
862 XDrawLine(dpy,win,dim, w-11, y+6, w-6, y+h/2-1);
863 XDrawLine(dpy,win,light, w-11, y+h-8, w-6, y+h/2-1);
864 XDrawLine(dpy,win,dark, w-12, y+6, w-12, y+h-8);
867 /* draw indicator */
868 if (entry->flags.indicator && entry->flags.indicator_on) {
869 int iw, ih;
870 WPixmap *indicator;
873 switch (entry->flags.indicator_type) {
874 case MI_CHECK:
875 indicator = scr->menu_check_indicator;
876 break;
877 case MI_MINIWINDOW:
878 indicator = scr->menu_mini_indicator;
879 break;
880 case MI_HIDDEN:
881 indicator = scr->menu_hide_indicator;
882 break;
883 case MI_SHADED:
884 indicator = scr->menu_shade_indicator;
885 break;
886 case MI_DIAMOND:
887 default:
888 indicator = scr->menu_radio_indicator;
889 break;
892 iw = indicator->width;
893 ih = indicator->height;
894 XSetClipMask(dpy, scr->copy_gc, indicator->mask);
895 XSetClipOrigin(dpy, scr->copy_gc, 5, y+(h-ih)/2);
896 if (selected)
897 XSetForeground(dpy, scr->copy_gc, scr->black_pixel);
898 else
899 XSetForeground(dpy, scr->copy_gc, scr->mtext_pixel);
900 XFillRectangle(dpy, win, scr->copy_gc, 5, y+(h-ih)/2, iw, ih);
902 XCopyArea(dpy, indicator->image, win, scr->copy_gc, 0, 0,
903 iw, ih, 5, y+(h-ih)/2);
905 XSetClipOrigin(dpy, scr->copy_gc, 0, 0);
908 /* draw right text */
910 if (entry->rtext && entry->cascade<0) {
911 tw = WMWidthOfString(scr->menu_entry_font, entry->rtext,
912 strlen(entry->rtext));
914 WMDrawString(scr->wmscreen, win, textGC, scr->menu_entry_font, w-6-tw,
915 y + 3 + wPreferences.menu_text_clearance, entry->rtext, strlen(entry->rtext));
920 static void
921 move_menus(WMenu *menu, int x, int y)
923 while (menu->parent) {
924 menu = menu->parent;
925 x -= MENUW(menu);
926 if (!wPreferences.align_menus && menu->selected_entry>=0) {
927 y -= menu->selected_entry*menu->entry_height;
930 wMenuMove(menu, x, y, True);
933 static void
934 makeVisible(WMenu *menu)
936 WScreen *scr = menu->frame->screen_ptr;
937 int x1, y1, x2, y2, new_x, new_y, move;
939 if (menu->entry_no<0) return;
941 x1 = menu->frame_x;
942 y1 = menu->frame_y+menu->frame->top_width
943 + menu->selected_entry*menu->entry_height;
944 x2 = x1 + MENUW(menu);
945 y2 = y1 + menu->entry_height;
947 new_x = x1;
948 new_y = y1;
949 move = 0;
951 if (x1 < 0) {
952 new_x = 0;
953 move = 1;
954 } else if (x2 >= scr->scr_width) {
955 new_x = scr->scr_width - MENUW(menu) - 1;
956 move = 1;
959 if (y1 < 0) {
960 new_y = 0;
961 move = 1;
962 } else if (y2 >= scr->scr_height) {
963 new_y = scr->scr_height - menu->entry_height - 1;
964 move = 1;
967 new_y = new_y - menu->frame->top_width
968 - menu->selected_entry*menu->entry_height;
969 move_menus(menu, new_x, new_y);
973 static int
974 check_key(WMenu *menu, XKeyEvent *event)
976 int i, ch, s;
977 char buffer[32];
979 if (XLookupString(event, buffer, 32, NULL, NULL)<1)
980 return -1;
982 ch = toupper(buffer[0]);
984 s = (menu->selected_entry>=0 ? menu->selected_entry+1 : 0);
986 again:
987 for (i=s; i<menu->entry_no; i++) {
988 if (ch==toupper(menu->entries[i]->text[0])) {
989 return i;
992 /* no match. Retry from start, if previous started from a selected entry */
993 if (s!=0) {
994 s = 0;
995 goto again;
997 return -1;
1001 static int
1002 keyboardMenu(WMenu *menu)
1004 XEvent event;
1005 KeySym ksym=NoSymbol;
1006 int done=0;
1007 int index;
1008 WMenuEntry *entry;
1009 int old_pos_x = menu->frame_x;
1010 int old_pos_y = menu->frame_y;
1011 int new_x = old_pos_x, new_y = old_pos_y;
1012 int scr_width = menu->frame->screen_ptr->scr_width;
1013 int scr_height = menu->frame->screen_ptr->scr_height;
1015 if (menu->flags.editing)
1016 return False;
1019 XGrabKeyboard(dpy, menu->frame->core->window, True, GrabModeAsync,
1020 GrabModeAsync, CurrentTime);
1022 if (menu->frame_y+menu->frame->top_width >= scr_height)
1023 new_y = scr_height - menu->frame->top_width;
1025 if (menu->frame_x+MENUW(menu) >= scr_width)
1026 new_x = scr_width-MENUW(menu)-1;
1028 move_menus(menu, new_x, new_y);
1030 while (!done && menu->flags.mapped) {
1031 XAllowEvents(dpy, AsyncKeyboard, CurrentTime);
1032 WMMaskEvent(dpy, ExposureMask|ButtonMotionMask|ButtonPressMask
1033 |ButtonReleaseMask|KeyPressMask|KeyReleaseMask
1034 |SubstructureNotifyMask, &event);
1036 switch (event.type) {
1037 case KeyPress:
1038 ksym = XLookupKeysym(&event.xkey, 0);
1039 switch (ksym) {
1040 case XK_Escape:
1041 done = 1;
1042 break;
1044 case XK_Home:
1045 #ifdef XK_KP_Home
1046 case XK_KP_Home:
1047 #endif
1048 selectEntry(menu, 0);
1049 makeVisible(menu);
1050 break;
1052 case XK_End:
1053 #ifdef XK_KP_End
1054 case XK_KP_End:
1055 #endif
1056 selectEntry(menu, menu->entry_no-1);
1057 makeVisible(menu);
1058 break;
1060 case XK_Up:
1061 #ifdef ARROWLESS_KBD
1062 case XK_k:
1063 #endif
1064 #ifdef XK_KP_Up
1065 case XK_KP_Up:
1066 #endif
1067 if (menu->selected_entry <= 0)
1068 selectEntry(menu, menu->entry_no-1);
1069 else
1070 selectEntry(menu, menu->selected_entry-1);
1071 makeVisible(menu);
1072 break;
1074 case XK_Down:
1075 #ifdef ARROWLESS_KBD
1076 case XK_j:
1077 #endif
1078 #ifdef XK_KP_Down
1079 case XK_KP_Down:
1080 #endif
1081 if (menu->selected_entry<0)
1082 selectEntry(menu, 0);
1083 else if (menu->selected_entry == menu->entry_no-1)
1084 selectEntry(menu, 0);
1085 else if (menu->selected_entry < menu->entry_no-1)
1086 selectEntry(menu, menu->selected_entry+1);
1087 makeVisible(menu);
1088 break;
1090 case XK_Right:
1091 #ifdef ARROWLESS_KBD
1092 case XK_l:
1093 #endif
1094 #ifdef XK_KP_Right
1095 case XK_KP_Right:
1096 #endif
1097 if (menu->selected_entry>=0) {
1098 WMenuEntry *entry;
1099 entry = menu->entries[menu->selected_entry];
1101 if (entry->cascade >= 0 && menu->cascades
1102 && menu->cascades[entry->cascade]->entry_no > 0) {
1104 XUngrabKeyboard(dpy, CurrentTime);
1106 selectEntry(menu->cascades[entry->cascade], 0);
1107 if (!keyboardMenu(menu->cascades[entry->cascade]))
1108 done = 1;
1110 XGrabKeyboard(dpy, menu->frame->core->window, True,
1111 GrabModeAsync, GrabModeAsync,
1112 CurrentTime);
1115 break;
1117 case XK_Left:
1118 #ifdef ARROWLESS_KBD
1119 case XK_h:
1120 #endif
1121 #ifdef XK_KP_Left
1122 case XK_KP_Left:
1123 #endif
1124 if (menu->parent!=NULL && menu->parent->selected_entry>=0) {
1125 selectEntry(menu, -1);
1126 move_menus(menu, old_pos_x, old_pos_y);
1127 return True;
1129 break;
1131 case XK_Return:
1132 done = 2;
1133 break;
1135 default:
1136 index = check_key(menu, &event.xkey);
1137 if (index>=0) {
1138 selectEntry(menu, index);
1141 break;
1143 default:
1144 if (event.type==ButtonPress)
1145 done = 1;
1147 WMHandleEvent(&event);
1151 XUngrabKeyboard(dpy, CurrentTime);
1153 if (done==2 && menu->selected_entry>=0) {
1154 entry = menu->entries[menu->selected_entry];
1155 } else {
1156 entry = NULL;
1159 if (entry && entry->callback!=NULL && entry->flags.enabled
1160 && entry->cascade < 0) {
1161 #if (MENU_BLINK_COUNT > 0)
1162 int sel = menu->selected_entry;
1163 int i;
1165 for (i=0; i<MENU_BLINK_COUNT; i++) {
1166 paintEntry(menu, sel, False);
1167 XSync(dpy, 0);
1168 wusleep(MENU_BLINK_DELAY);
1169 paintEntry(menu, sel, True);
1170 XSync(dpy, 0);
1171 wusleep(MENU_BLINK_DELAY);
1173 #endif
1174 selectEntry(menu, -1);
1176 if (!menu->flags.buttoned) {
1177 wMenuUnmap(menu);
1178 move_menus(menu, old_pos_x, old_pos_y);
1180 closeCascade(menu);
1182 (*entry->callback)(menu, entry);
1183 } else {
1184 if (!menu->flags.buttoned) {
1185 wMenuUnmap(menu);
1186 move_menus(menu, old_pos_x, old_pos_y);
1188 selectEntry(menu, -1);
1192 /* returns True if returning from a submenu to a parent menu,
1193 * False if exiting from menu */
1194 return False;
1198 void
1199 wMenuMapAt(WMenu *menu, int x, int y, int keyboard)
1201 int scr_width = menu->frame->screen_ptr->scr_width;
1202 int scr_height = menu->frame->screen_ptr->scr_height;
1204 if (!menu->flags.realized) {
1205 menu->flags.realized=1;
1206 wMenuRealize(menu);
1208 if (!menu->flags.mapped) {
1209 if (wPreferences.wrap_menus) {
1210 if (x<0) x = 0;
1211 if (y<0) y = 0;
1212 if (x+MENUW(menu) > scr_width)
1213 x = scr_width - MENUW(menu);
1214 if (y+MENUH(menu) > scr_height)
1215 y = scr_height - MENUH(menu);
1218 XMoveWindow(dpy, menu->frame->core->window, x, y);
1219 menu->frame_x = x;
1220 menu->frame_y = y;
1221 XMapWindow(dpy, menu->frame->core->window);
1222 wRaiseFrame(menu->frame->core);
1223 menu->flags.mapped = 1;
1224 } else {
1225 selectEntry(menu, 0);
1228 if (keyboard)
1229 keyboardMenu(menu);
1233 void
1234 wMenuMap(WMenu *menu)
1236 if (!menu->flags.realized) {
1237 menu->flags.realized=1;
1238 wMenuRealize(menu);
1240 if (menu->flags.app_menu && menu->parent==NULL) {
1241 menu->frame_x = menu->frame->screen_ptr->app_menu_x;
1242 menu->frame_y = menu->frame->screen_ptr->app_menu_y;
1243 XMoveWindow(dpy, menu->frame->core->window, menu->frame_x, menu->frame_y);
1245 XMapWindow(dpy, menu->frame->core->window);
1246 wRaiseFrame(menu->frame->core);
1247 menu->flags.mapped = 1;
1251 void
1252 wMenuUnmap(WMenu *menu)
1254 int i;
1256 XUnmapWindow(dpy, menu->frame->core->window);
1257 if (menu->flags.titled && menu->flags.buttoned) {
1258 wFrameWindowHideButton(menu->frame, WFF_RIGHT_BUTTON);
1260 menu->flags.buttoned = 0;
1261 menu->flags.mapped = 0;
1262 menu->flags.open_to_left = 0;
1264 for (i=0; i<menu->cascade_no; i++) {
1265 if (menu->cascades[i]!=NULL
1266 && menu->cascades[i]->flags.mapped
1267 && !menu->cascades[i]->flags.buttoned) {
1269 wMenuUnmap(menu->cascades[i]);
1272 menu->selected_entry = -1;
1277 void
1278 wMenuPaint(WMenu *menu)
1280 int i;
1282 if (!menu->flags.mapped) {
1283 return;
1286 /* paint entries */
1287 for (i=0; i<menu->entry_no; i++) {
1288 paintEntry(menu, i, i==menu->selected_entry);
1293 void
1294 wMenuSetEnabled(WMenu *menu, int index, int enable)
1296 if (index>=menu->entry_no) return;
1297 menu->entries[index]->flags.enabled=enable;
1298 paintEntry(menu, index, index==menu->selected_entry);
1299 paintEntry(menu->brother, index, index==menu->selected_entry);
1303 /* ====================================================================== */
1306 static void
1307 editEntry(WMenu *menu, WMenuEntry *entry)
1309 WTextInput *text;
1310 XEvent event;
1311 WObjDescriptor *desc;
1312 char *t;
1313 int done = 0;
1314 Window old_focus;
1315 int old_revert;
1317 menu->flags.editing = 1;
1319 text = wTextCreate(menu->menu, 1, menu->entry_height * entry->order,
1320 menu->menu->width - 2, menu->entry_height - 1);
1322 wTextPutText(text, entry->text);
1323 XGetInputFocus(dpy, &old_focus, &old_revert);
1324 XSetInputFocus(dpy, text->core->window, RevertToNone, CurrentTime);
1326 if (XGrabKeyboard(dpy, text->core->window, True, GrabModeAsync,
1327 GrabModeAsync, CurrentTime)!=GrabSuccess) {
1328 wwarning("could not grab keyboard");
1329 wTextDestroy(text);
1331 wSetFocusTo(menu->frame->screen_ptr,
1332 menu->frame->screen_ptr->focused_window);
1333 return;
1337 while (!done && !text->done) {
1338 XSync(dpy, 0);
1339 XAllowEvents(dpy, AsyncKeyboard|AsyncPointer, CurrentTime);
1340 XSync(dpy, 0);
1341 WMNextEvent(dpy, &event);
1343 if (XFindContext(dpy, event.xany.window, wWinContext,
1344 (XPointer *)&desc)==XCNOENT)
1345 desc = NULL;
1347 if ((desc != NULL) && (desc->handle_anything != NULL)) {
1349 (*desc->handle_anything)(desc, &event);
1351 } else {
1352 switch (event.type) {
1353 case ButtonPress:
1354 XAllowEvents(dpy, ReplayPointer, CurrentTime);
1355 done = 1;
1357 default:
1358 WMHandleEvent(&event);
1359 break;
1364 XSetInputFocus(dpy, old_focus, old_revert, CurrentTime);
1366 wSetFocusTo(menu->frame->screen_ptr,
1367 menu->frame->screen_ptr->focused_window);
1370 t = wTextGetText(text);
1371 /* if !t, the user has canceled editing */
1372 if (t) {
1373 if (entry->text)
1374 wfree(entry->text);
1375 entry->text = wstrdup(t);
1377 menu->flags.realized = 0;
1379 wTextDestroy(text);
1381 XUngrabKeyboard(dpy, CurrentTime);
1383 if (t && menu->on_edit)
1384 (*menu->on_edit)(menu, entry);
1386 menu->flags.editing = 0;
1388 if (!menu->flags.realized)
1389 wMenuRealize(menu);
1393 static void
1394 selectEntry(WMenu *menu, int entry_no)
1396 WMenuEntry *entry;
1397 WMenu *submenu;
1398 int old_entry;
1400 if (menu->entries==NULL)
1401 return;
1403 if (entry_no >= menu->entry_no)
1404 return;
1406 old_entry = menu->selected_entry;
1407 menu->selected_entry = entry_no;
1409 if (old_entry!=entry_no) {
1411 /* unselect previous entry */
1412 if (old_entry>=0) {
1413 paintEntry(menu, old_entry, False);
1414 entry = menu->entries[old_entry];
1416 /* unmap cascade */
1417 if (entry->cascade>=0 && menu->cascades) {
1418 if (!menu->cascades[entry->cascade]->flags.buttoned) {
1419 wMenuUnmap(menu->cascades[entry->cascade]);
1424 if (entry_no<0) {
1425 menu->selected_entry = -1;
1426 return;
1428 entry = menu->entries[entry_no];
1430 if (entry->cascade>=0 && menu->cascades && entry->flags.enabled) {
1431 /* Callback for when the submenu is opened.
1433 submenu = menu->cascades[entry->cascade];
1434 if (submenu && submenu->flags.brother)
1435 submenu = submenu->brother;
1437 if (entry->callback) {
1438 /* Only call the callback if the submenu is not yet mapped.
1440 if (menu->flags.brother) {
1441 if (!submenu || !submenu->flags.mapped)
1442 (*entry->callback)(menu->brother, entry);
1443 } else {
1444 if (!submenu || !submenu->flags.buttoned)
1445 (*entry->callback)(menu, entry);
1449 /* the submenu menu might have changed */
1450 submenu = menu->cascades[entry->cascade];
1452 /* map cascade */
1453 if (!submenu->flags.mapped) {
1454 int x, y;
1456 if (!submenu->flags.realized)
1457 wMenuRealize(submenu);
1458 if (wPreferences.wrap_menus) {
1459 if (menu->flags.open_to_left)
1460 submenu->flags.open_to_left = 1;
1462 if (submenu->flags.open_to_left) {
1463 x = menu->frame_x - MENUW(submenu);
1464 if (x<0) {
1465 x = 0;
1466 submenu->flags.open_to_left = 0;
1468 } else {
1469 x = menu->frame_x + MENUW(menu);
1471 if (x + MENUW(submenu)
1472 >= menu->frame->screen_ptr->scr_width) {
1474 x = menu->frame_x - MENUW(submenu);
1475 submenu->flags.open_to_left = 1;
1478 } else {
1479 x = menu->frame_x + MENUW(menu);
1482 if (wPreferences.align_menus) {
1483 y = menu->frame_y;
1484 } else {
1485 y = menu->frame_y + menu->entry_height*entry_no;
1486 if (menu->flags.titled)
1487 y += menu->frame->top_width;
1488 if (menu->cascades[entry->cascade]->flags.titled)
1489 y -= menu->cascades[entry->cascade]->frame->top_width;
1492 wMenuMapAt(menu->cascades[entry->cascade], x, y, False);
1493 menu->cascades[entry->cascade]->parent = menu;
1494 } else {
1495 return;
1498 paintEntry(menu, entry_no, True);
1503 static WMenu*
1504 findMenu(WScreen *scr, int *x_ret, int *y_ret)
1506 WMenu *menu;
1507 WObjDescriptor *desc;
1508 Window root_ret, win, junk_win;
1509 int x, y, wx, wy;
1510 unsigned int mask;
1512 XQueryPointer(dpy, scr->root_win, &root_ret, &win, &x, &y, &wx, &wy,
1513 &mask);
1515 if (win==None) return NULL;
1517 if (XFindContext(dpy, win, wWinContext, (XPointer *)&desc)==XCNOENT)
1518 return NULL;
1520 if (desc->parent_type == WCLASS_MENU) {
1521 menu = (WMenu*)desc->parent;
1522 XTranslateCoordinates(dpy, root_ret, menu->menu->window, wx, wy,
1523 x_ret, y_ret, &junk_win);
1524 return menu;
1526 return NULL;
1532 static void
1533 closeCascade(WMenu *menu)
1535 WMenu *parent=menu->parent;
1537 if (menu->flags.brother
1538 || (!menu->flags.buttoned
1539 && (!menu->flags.app_menu||menu->parent!=NULL))) {
1541 selectEntry(menu, -1);
1542 XSync(dpy, 0);
1543 #if (MENU_BLINK_DELAY > 2)
1544 wusleep(MENU_BLINK_DELAY/2);
1545 #endif
1546 wMenuUnmap(menu);
1547 while (parent!=NULL
1548 && (parent->parent!=NULL || !parent->flags.app_menu
1549 || parent->flags.brother)
1550 && !parent->flags.buttoned) {
1551 selectEntry(parent, -1);
1552 wMenuUnmap(parent);
1553 parent = parent->parent;
1555 if (parent)
1556 selectEntry(parent, -1);
1561 static void
1562 closeBrotherCascadesOf(WMenu *menu)
1564 WMenu *tmp;
1565 int i;
1567 for (i=0; i<menu->cascade_no; i++) {
1568 if (menu->cascades[i]->flags.brother) {
1569 tmp = menu->cascades[i];
1570 } else {
1571 tmp = menu->cascades[i]->brother;
1573 if (tmp->flags.mapped) {
1574 selectEntry(tmp->parent, -1);
1575 closeBrotherCascadesOf(tmp);
1576 break;
1582 #define getEntryAt(menu, x, y) ((y)<0 ? -1 : (y)/(menu->entry_height))
1585 static WMenu*
1586 parentMenu(WMenu *menu)
1588 WMenu *parent;
1589 WMenuEntry *entry;
1591 if (menu->flags.buttoned)
1592 return menu;
1594 while (menu->parent && menu->parent->flags.mapped) {
1595 parent = menu->parent;
1596 if (parent->selected_entry < 0)
1597 break;
1598 entry = parent->entries[parent->selected_entry];
1599 if (!entry->flags.enabled || entry->cascade<0 || !parent->cascades ||
1600 parent->cascades[entry->cascade] != menu)
1601 break;
1602 menu = parent;
1603 if (menu->flags.buttoned)
1604 break;
1607 return menu;
1613 * Will raise the passed menu, if submenu = 0
1614 * If submenu > 0 will also raise all mapped submenus
1615 * until the first buttoned one
1616 * If submenu < 0 will also raise all mapped parent menus
1617 * until the first buttoned one
1620 static void
1621 raiseMenus(WMenu *menu, int submenus)
1623 WMenu *submenu;
1624 int i;
1626 if(!menu) return;
1628 wRaiseFrame(menu->frame->core);
1630 if (submenus>0 && menu->selected_entry>=0) {
1631 i = menu->entries[menu->selected_entry]->cascade;
1632 if (i>=0 && menu->cascades) {
1633 submenu = menu->cascades[i];
1634 if (submenu->flags.mapped && !submenu->flags.buttoned)
1635 raiseMenus(submenu, submenus);
1638 if (submenus<0 && !menu->flags.buttoned &&
1639 menu->parent && menu->parent->flags.mapped)
1640 raiseMenus(menu->parent, submenus);
1644 WMenu*
1645 wMenuUnderPointer(WScreen *screen)
1647 WObjDescriptor *desc;
1648 Window root_ret, win;
1649 int dummy;
1650 unsigned int mask;
1652 XQueryPointer(dpy, screen->root_win, &root_ret, &win, &dummy, &dummy,
1653 &dummy, &dummy, &mask);
1655 if (win==None) return NULL;
1657 if (XFindContext(dpy, win, wWinContext, (XPointer *)&desc)==XCNOENT)
1658 return NULL;
1660 if (desc->parent_type == WCLASS_MENU)
1661 return (WMenu *)desc->parent;
1662 return NULL;
1668 static void
1669 getPointerPosition(WScreen *scr, int *x, int *y)
1671 Window root_ret, win;
1672 int wx, wy;
1673 unsigned int mask;
1675 XQueryPointer(dpy, scr->root_win, &root_ret, &win, x, y, &wx, &wy, &mask);
1679 static void
1680 getScrollAmount(WMenu *menu, int *hamount, int *vamount)
1682 WScreen *scr = menu->menu->screen_ptr;
1683 int menuX1 = menu->frame_x;
1684 int menuY1 = menu->frame_y;
1685 int menuX2 = menu->frame_x + MENUW(menu);
1686 int menuY2 = menu->frame_y + MENUH(menu);
1687 int screenW = scr->scr_width;
1688 int screenH = scr->scr_height;
1689 int xroot, yroot;
1691 *hamount = 0;
1692 *vamount = 0;
1694 getPointerPosition(scr, &xroot, &yroot);
1696 #ifdef VIRTUAL_DESKTOP
1697 if (wPreferences.vedge_thickness) {
1698 if (xroot <= wPreferences.vedge_thickness + 1 && menuX1 < wPreferences.vedge_thickness) {
1699 /* scroll to the right */
1700 *hamount = WMIN(MENU_SCROLL_STEP, abs(menuX1));
1702 } else if (xroot >= screenW-2-wPreferences.vedge_thickness && menuX2 > screenW-1-wPreferences.vedge_thickness) {
1703 /* scroll to the left */
1704 *hamount = WMIN(MENU_SCROLL_STEP, abs(menuX2-screenW-1));
1706 if (*hamount==0)
1707 *hamount = 1;
1709 *hamount = -*hamount;
1711 } else
1712 #endif
1714 if (xroot <= 1 && menuX1 < 0) {
1715 /* scroll to the right */
1716 *hamount = WMIN(MENU_SCROLL_STEP, abs(menuX1));
1718 } else if (xroot >= screenW-2 && menuX2 > screenW-1) {
1719 /* scroll to the left */
1720 *hamount = WMIN(MENU_SCROLL_STEP, abs(menuX2-screenW-1));
1722 if (*hamount==0)
1723 *hamount = 1;
1725 *hamount = -*hamount;
1728 if (yroot <= 1 && menuY1 < 0) {
1729 /* scroll down */
1730 *vamount = WMIN(MENU_SCROLL_STEP, abs(menuY1));
1732 } else if (yroot >= screenH-2 && menuY2 > screenH-1) {
1733 /* scroll up */
1734 *vamount = WMIN(MENU_SCROLL_STEP, abs(menuY2-screenH-2));
1736 *vamount = -*vamount;
1741 static void
1742 dragScrollMenuCallback(void *data)
1744 WMenu *menu = (WMenu*)data;
1745 WScreen *scr = menu->menu->screen_ptr;
1746 WMenu *parent = parentMenu(menu);
1747 int hamount, vamount;
1748 int x, y;
1749 int newSelectedEntry;
1751 getScrollAmount(menu, &hamount, &vamount);
1754 if (hamount != 0 || vamount != 0) {
1755 wMenuMove(parent, parent->frame_x + hamount,
1756 parent->frame_y + vamount, True);
1757 if (findMenu(scr, &x, &y)) {
1758 newSelectedEntry = getEntryAt(menu, x, y);
1759 selectEntry(menu, newSelectedEntry);
1760 } else {
1761 /* Pointer fell outside of menu. If the selected entry is
1762 * not a submenu, unselect it */
1763 if (menu->selected_entry >= 0
1764 && menu->entries[menu->selected_entry]->cascade<0)
1765 selectEntry(menu, -1);
1766 newSelectedEntry = 0;
1769 /* paranoid check */
1770 if (newSelectedEntry >= 0) {
1771 /* keep scrolling */
1772 menu->timer = WMAddTimerHandler(MENU_SCROLL_DELAY,
1773 dragScrollMenuCallback, menu);
1774 } else {
1775 menu->timer = NULL;
1777 } else {
1778 /* don't need to scroll anymore */
1779 menu->timer = NULL;
1780 if (findMenu(scr, &x, &y)) {
1781 newSelectedEntry = getEntryAt(menu, x, y);
1782 selectEntry(menu, newSelectedEntry);
1788 static void
1789 scrollMenuCallback(void *data)
1791 WMenu *menu = (WMenu*)data;
1792 WMenu *parent = parentMenu(menu);
1793 int hamount = 0; /* amount to scroll */
1794 int vamount = 0;
1796 getScrollAmount(menu, &hamount, &vamount);
1798 if (hamount != 0 || vamount != 0) {
1799 wMenuMove(parent, parent->frame_x + hamount,
1800 parent->frame_y + vamount, True);
1802 /* keep scrolling */
1803 menu->timer = WMAddTimerHandler(MENU_SCROLL_DELAY,
1804 scrollMenuCallback, menu);
1805 } else {
1806 /* don't need to scroll anymore */
1807 menu->timer = NULL;
1813 #define MENU_SCROLL_BORDER 5
1815 static int
1816 isPointNearBoder(WMenu *menu, int x, int y)
1818 int menuX1 = menu->frame_x;
1819 int menuY1 = menu->frame_y;
1820 int menuX2 = menu->frame_x + MENUW(menu);
1821 int menuY2 = menu->frame_y + MENUH(menu);
1822 int scrXe = menu->menu->screen_ptr->scr_width-1;
1823 int scrYe = menu->menu->screen_ptr->scr_height-1;
1824 int flag = 0;
1826 if (x >= menuX1 && x <= menuX2 && (y < MENU_SCROLL_BORDER
1827 || y > scrYe-MENU_SCROLL_BORDER))
1828 flag = 1;
1829 else if (y >= menuY1 && y <= menuY2 && (x < MENU_SCROLL_BORDER
1830 || x > scrXe-MENU_SCROLL_BORDER))
1831 flag = 1;
1833 return flag;
1837 typedef struct _delay {
1838 WWindow *wwin;
1839 WMenu *menu;
1840 int ox,oy;
1841 } _delay;
1844 static void
1845 _leaving(_delay *dl)
1847 wMenuMove(dl->menu, dl->ox, dl->oy, True);
1848 dl->menu->jump_back=NULL;
1849 dl->menu->menu->screen_ptr->flags.jump_back_pending = 0;
1850 wfree(dl);
1854 void
1855 wMenuScroll(WMenu *menu, XEvent *event)
1857 WMenu *smenu;
1858 WMenu *omenu = parentMenu(menu);
1859 WScreen *scr = menu->frame->screen_ptr;
1860 int done = 0;
1861 int jump_back = 0;
1862 int old_frame_x = omenu->frame_x;
1863 int old_frame_y = omenu->frame_y;
1864 XEvent ev;
1866 if (omenu->jump_back)
1867 WMDeleteTimerWithClientData(omenu->jump_back);
1870 if ((/*omenu->flags.buttoned &&*/ !wPreferences.wrap_menus)
1871 || omenu->flags.app_menu) {
1872 jump_back = 1;
1875 if (!wPreferences.wrap_menus)
1876 raiseMenus(omenu, True);
1877 else
1878 raiseMenus(menu, False);
1880 if (!menu->timer)
1881 scrollMenuCallback(menu);
1883 while(!done) {
1884 int x, y, on_border, on_x_edge, on_y_edge, on_title;
1886 WMNextEvent(dpy, &ev);
1887 switch (ev.type) {
1888 case EnterNotify:
1889 WMHandleEvent(&ev);
1890 case MotionNotify:
1891 x = (ev.type==MotionNotify) ? ev.xmotion.x_root : ev.xcrossing.x_root;
1892 y = (ev.type==MotionNotify) ? ev.xmotion.y_root : ev.xcrossing.y_root;
1894 /* on_border is != 0 if the pointer is between the menu
1895 * and the screen border and is close enough to the border */
1896 on_border = isPointNearBoder(menu, x, y);
1898 smenu = wMenuUnderPointer(scr);
1900 if ((smenu==NULL && !on_border) || (smenu && parentMenu(smenu)!=omenu)) {
1901 done = 1;
1902 break;
1905 on_x_edge = x <= 1 || x >= scr->scr_width - 2;
1906 on_y_edge = y <= 1 || y >= scr->scr_height - 2;
1907 on_border = on_x_edge || on_y_edge;
1909 if (!on_border && !jump_back) {
1910 done = 1;
1911 break;
1914 if (menu->timer && (smenu!=menu || (!on_y_edge && !on_x_edge))) {
1915 WMDeleteTimerHandler(menu->timer);
1916 menu->timer = NULL;
1919 if (smenu != NULL)
1920 menu = smenu;
1922 if (!menu->timer)
1923 scrollMenuCallback(menu);
1924 break;
1925 case ButtonPress:
1926 /* True if we push on title, or drag the omenu to other position */
1927 on_title = ev.xbutton.x_root >= omenu->frame_x &&
1928 ev.xbutton.x_root <= omenu->frame_x + MENUW(omenu) &&
1929 ev.xbutton.y_root >= omenu->frame_y &&
1930 ev.xbutton.y_root <= omenu->frame_y + omenu->frame->top_width;
1931 WMHandleEvent(&ev);
1932 smenu = wMenuUnderPointer(scr);
1933 if (smenu == NULL || (smenu && smenu->flags.buttoned && smenu != omenu))
1934 done = 1;
1935 else if (smenu==omenu && on_title) {
1936 jump_back = 0;
1937 done = 1;
1939 break;
1940 case KeyPress:
1941 done = 1;
1942 default:
1943 WMHandleEvent(&ev);
1944 break;
1948 if (menu->timer) {
1949 WMDeleteTimerHandler(menu->timer);
1950 menu->timer = NULL;
1953 if (jump_back) {
1954 _delay *delayer;
1955 if (!omenu->jump_back) {
1956 delayer=wmalloc(sizeof(_delay));
1957 delayer->menu=omenu;
1958 delayer->ox=old_frame_x;
1959 delayer->oy=old_frame_y;
1960 omenu->jump_back=delayer;
1961 scr->flags.jump_back_pending = 1;
1963 else delayer = omenu->jump_back;
1964 WMAddTimerHandler(MENU_JUMP_BACK_DELAY,(WMCallback*)_leaving, delayer);
1970 static void
1971 menuExpose(WObjDescriptor *desc, XEvent *event)
1973 wMenuPaint(desc->parent);
1976 typedef struct {
1977 int *delayed_select;
1978 WMenu *menu;
1979 WMHandlerID magic;
1980 } delay_data;
1983 static void
1984 delaySelection(void *data)
1986 delay_data *d = (delay_data*)data;
1987 int x, y, entry_no;
1988 WMenu *menu;
1990 d->magic = NULL;
1992 menu = findMenu(d->menu->menu->screen_ptr, &x, &y);
1993 if (menu && (d->menu == menu || d->delayed_select)) {
1994 entry_no = getEntryAt(menu, x, y);
1995 selectEntry(menu, entry_no);
1997 if (d->delayed_select)
1998 *(d->delayed_select) = 0;
2002 static void
2003 menuMouseDown(WObjDescriptor *desc, XEvent *event)
2005 XButtonEvent *bev = &event->xbutton;
2006 WMenu *menu = desc->parent;
2007 WMenu *smenu;
2008 WScreen *scr=menu->frame->screen_ptr;
2009 WMenuEntry *entry=NULL;
2010 XEvent ev;
2011 int close_on_exit=0;
2012 int done=0;
2013 int delayed_select = 0;
2014 int entry_no;
2015 int x, y;
2016 int prevx, prevy;
2017 int old_frame_x = 0;
2018 int old_frame_y = 0;
2019 delay_data d_data = {NULL, NULL, NULL};
2021 if (!wPreferences.wrap_menus) {
2022 smenu = parentMenu(menu);
2023 old_frame_x = smenu->frame_x;
2024 old_frame_y = smenu->frame_y;
2025 } else if (event->xbutton.window == menu->frame->core->window) {
2026 /* This is true if the menu was launched with right click on root window */
2027 delayed_select = 1;
2028 d_data.delayed_select = &delayed_select;
2029 d_data.menu = menu;
2030 d_data.magic = WMAddTimerHandler(wPreferences.dblclick_time,
2031 delaySelection, &d_data);
2034 if (menu->flags.inside_handler) {
2035 return;
2037 menu->flags.inside_handler = 1;
2039 wRaiseFrame(menu->frame->core);
2041 close_on_exit = (bev->send_event || menu->flags.brother);
2043 smenu = findMenu(scr, &x, &y);
2044 if (!smenu) {
2045 x = -1;
2046 y = -1;
2047 } else {
2048 menu = smenu;
2051 if (menu->flags.editing) {
2052 goto byebye;
2054 entry_no = getEntryAt(menu, x, y);
2055 if (entry_no>=0) {
2056 entry = menu->entries[entry_no];
2058 if (!close_on_exit && (bev->state & ControlMask) && smenu
2059 && entry->flags.editable) {
2060 editEntry(smenu, entry);
2061 goto byebye;
2062 } else if (bev->state & ControlMask) {
2063 goto byebye;
2066 if (entry->flags.enabled && entry->cascade>=0 && menu->cascades) {
2067 WMenu *submenu = menu->cascades[entry->cascade];
2068 /* map cascade */
2069 if (submenu->flags.mapped && !submenu->flags.buttoned &&
2070 menu->selected_entry!=entry_no) {
2071 wMenuUnmap(submenu);
2073 if (!submenu->flags.mapped && !delayed_select) {
2074 selectEntry(menu, entry_no);
2075 } else if (!submenu->flags.buttoned) {
2076 selectEntry(menu, -1);
2079 } else if (!delayed_select) {
2080 selectEntry(menu, entry_no);
2083 if (!wPreferences.wrap_menus && !wPreferences.scrollable_menus) {
2084 if (!menu->timer)
2085 dragScrollMenuCallback(menu);
2089 prevx = bev->x_root;
2090 prevy = bev->y_root;
2091 while (!done) {
2092 int x, y;
2094 XAllowEvents(dpy, AsyncPointer|SyncPointer, CurrentTime);
2096 WMMaskEvent(dpy, ExposureMask|ButtonMotionMask|ButtonReleaseMask
2097 |ButtonPressMask, &ev);
2098 switch (ev.type) {
2099 case MotionNotify:
2100 smenu = findMenu(scr, &x, &y);
2102 if (smenu == NULL) {
2103 /* moved mouse out of menu */
2105 if (!delayed_select && d_data.magic) {
2106 WMDeleteTimerHandler(d_data.magic);
2107 d_data.magic = NULL;
2109 if (menu==NULL
2110 || (menu->selected_entry>=0
2111 && menu->entries[menu->selected_entry]->cascade>=0)) {
2112 prevx = ev.xmotion.x_root;
2113 prevy = ev.xmotion.y_root;
2115 break;
2117 selectEntry(menu, -1);
2118 menu = smenu;
2119 prevx = ev.xmotion.x_root;
2120 prevy = ev.xmotion.y_root;
2121 break;
2122 } else if (menu && menu!=smenu
2123 && (menu->selected_entry<0
2124 || menu->entries[menu->selected_entry]->cascade<0)) {
2125 selectEntry(menu, -1);
2127 if (!delayed_select && d_data.magic) {
2128 WMDeleteTimerHandler(d_data.magic);
2129 d_data.magic = NULL;
2131 } else {
2133 /* hysteresis for item selection */
2135 /* check if the motion was to the side, indicating that
2136 * the user may want to cross to a submenu */
2137 if (!delayed_select && menu) {
2138 int dx;
2139 Bool moved_to_submenu;/* moved to direction of submenu */
2141 dx = abs(prevx - ev.xmotion.x_root);
2143 moved_to_submenu = False;
2144 if (dx > 0 /* if moved enough to the side */
2145 /* maybe a open submenu */
2146 && menu->selected_entry>=0
2147 /* moving to the right direction */
2148 && (wPreferences.align_menus
2149 || ev.xmotion.y_root >= prevy)) {
2150 int index;
2152 index = menu->entries[menu->selected_entry]->cascade;
2153 if (index>=0) {
2154 if (menu->cascades[index]->frame_x>menu->frame_x) {
2155 if (prevx < ev.xmotion.x_root)
2156 moved_to_submenu = True;
2157 } else {
2158 if (prevx > ev.xmotion.x_root)
2159 moved_to_submenu = True;
2165 if (menu != smenu) {
2166 if (d_data.magic) {
2167 WMDeleteTimerHandler(d_data.magic);
2169 d_data.magic = NULL;
2170 } else if (moved_to_submenu) {
2171 /* while we are moving, postpone the selection */
2172 if (d_data.magic) {
2173 WMDeleteTimerHandler(d_data.magic);
2175 d_data.delayed_select = NULL;
2176 d_data.menu = menu;
2177 d_data.magic = WMAddTimerHandler(MENU_SELECT_DELAY,
2178 delaySelection,
2179 &d_data);
2180 prevx = ev.xmotion.x_root;
2181 prevy = ev.xmotion.y_root;
2182 break;
2183 } else {
2184 if (d_data.magic)
2185 WMDeleteTimerHandler(d_data.magic);
2186 d_data.magic = NULL;
2190 prevx = ev.xmotion.x_root;
2191 prevy = ev.xmotion.y_root;
2192 if (menu!=smenu) {
2193 /* pointer crossed menus */
2194 if (menu && menu->timer) {
2195 WMDeleteTimerHandler(menu->timer);
2196 menu->timer = NULL;
2198 if (smenu)
2199 dragScrollMenuCallback(smenu);
2201 menu = smenu;
2202 if (!menu->timer)
2203 dragScrollMenuCallback(menu);
2205 if (!delayed_select) {
2206 entry_no = getEntryAt(menu, x, y);
2207 if (entry_no>=0) {
2208 entry = menu->entries[entry_no];
2209 if (entry->flags.enabled && entry->cascade>=0 &&
2210 menu->cascades) {
2211 WMenu *submenu = menu->cascades[entry->cascade];
2212 if (submenu->flags.mapped && !submenu->flags.buttoned
2213 && menu->selected_entry!=entry_no) {
2214 wMenuUnmap(submenu);
2218 selectEntry(menu, entry_no);
2220 break;
2222 case ButtonPress:
2223 break;
2225 case ButtonRelease:
2226 if (ev.xbutton.button == event->xbutton.button)
2227 done=1;
2228 break;
2230 case Expose:
2231 WMHandleEvent(&ev);
2232 break;
2236 if (menu && menu->timer) {
2237 WMDeleteTimerHandler(menu->timer);
2238 menu->timer = NULL;
2240 if (d_data.magic!=NULL)
2241 WMDeleteTimerHandler(d_data.magic);
2243 if (menu && menu->selected_entry>=0) {
2244 entry = menu->entries[menu->selected_entry];
2245 if (entry->callback!=NULL && entry->flags.enabled
2246 && entry->cascade < 0) {
2247 /* blink and erase menu selection */
2248 #if (MENU_BLINK_DELAY > 0)
2249 int sel = menu->selected_entry;
2250 int i;
2252 for (i=0; i<MENU_BLINK_COUNT; i++) {
2253 paintEntry(menu, sel, False);
2254 XSync(dpy, 0);
2255 wusleep(MENU_BLINK_DELAY);
2256 paintEntry(menu, sel, True);
2257 XSync(dpy, 0);
2258 wusleep(MENU_BLINK_DELAY);
2260 #endif
2261 /* unmap the menu, it's parents and call the callback */
2262 if (!menu->flags.buttoned &&
2263 (!menu->flags.app_menu||menu->parent!=NULL)) {
2264 closeCascade(menu);
2265 } else {
2266 selectEntry(menu, -1);
2268 (*entry->callback)(menu, entry);
2270 /* If the user double clicks an entry, the entry will
2271 * be executed twice, which is not good for things like
2272 * the root menu. So, ignore any clicks that were generated
2273 * while the entry was being executed */
2274 while (XCheckTypedWindowEvent(dpy, menu->menu->window,
2275 ButtonPress, &ev));
2276 } else if (entry->callback!=NULL && entry->cascade<0) {
2277 selectEntry(menu, -1);
2278 } else {
2279 if (entry->cascade>=0 && menu->cascades
2280 && menu->cascades[entry->cascade]->flags.brother) {
2281 selectEntry(menu, -1);
2286 if (((WMenu*)desc->parent)->flags.brother || close_on_exit || !smenu)
2287 closeCascade(desc->parent);
2289 /* close the cascade windows that should not remain opened */
2290 closeBrotherCascadesOf(desc->parent);
2292 if (!wPreferences.wrap_menus)
2293 wMenuMove(parentMenu(desc->parent), old_frame_x, old_frame_y, True);
2295 byebye:
2296 ((WMenu*)desc->parent)->flags.inside_handler = 0;
2300 void
2301 wMenuMove(WMenu *menu, int x, int y, int submenus)
2303 WMenu *submenu;
2304 int i;
2306 if (!menu) return;
2308 menu->frame_x = x;
2309 menu->frame_y = y;
2310 XMoveWindow(dpy, menu->frame->core->window, x, y);
2312 if (submenus>0 && menu->selected_entry>=0) {
2313 i = menu->entries[menu->selected_entry]->cascade;
2315 if (i>=0 && menu->cascades) {
2316 submenu = menu->cascades[i];
2317 if (submenu->flags.mapped && !submenu->flags.buttoned) {
2318 if (wPreferences.align_menus) {
2319 wMenuMove(submenu, x + MENUW(menu), y, submenus);
2320 } else {
2321 wMenuMove(submenu, x+ MENUW(menu),
2322 y + submenu->entry_height*menu->selected_entry,
2323 submenus);
2328 if (submenus<0 && menu->parent!=NULL && menu->parent->flags.mapped &&
2329 !menu->parent->flags.buttoned) {
2330 if (wPreferences.align_menus) {
2331 wMenuMove(menu->parent, x - MENUW(menu->parent), y, submenus);
2332 } else {
2333 wMenuMove(menu->parent, x - MENUW(menu->parent), menu->frame_y
2334 - menu->parent->entry_height*menu->parent->selected_entry,
2335 submenus);
2341 static void
2342 changeMenuLevels(WMenu *menu, int lower)
2344 int i;
2346 if (!lower) {
2347 ChangeStackingLevel(menu->frame->core, (!menu->parent ? WMMainMenuLevel
2348 : WMSubmenuLevel));
2349 wRaiseFrame(menu->frame->core);
2350 menu->flags.lowered = 0;
2351 } else {
2352 ChangeStackingLevel(menu->frame->core, WMNormalLevel);
2353 wLowerFrame(menu->frame->core);
2354 menu->flags.lowered = 1;
2356 for (i=0; i<menu->cascade_no; i++) {
2357 if (menu->cascades[i]
2358 && !menu->cascades[i]->flags.buttoned
2359 && menu->cascades[i]->flags.lowered!=lower) {
2360 changeMenuLevels(menu->cascades[i], lower);
2367 static void
2368 menuTitleDoubleClick(WCoreWindow *sender, void *data, XEvent *event)
2370 WMenu *menu = data;
2371 int lower;
2373 if (event->xbutton.state & MOD_MASK) {
2374 if (menu->flags.lowered) {
2375 lower = 0;
2376 } else {
2377 lower = 1;
2379 changeMenuLevels(menu, lower);
2384 static void
2385 menuTitleMouseDown(WCoreWindow *sender, void *data, XEvent *event)
2387 WMenu *menu = data;
2388 WMenu *tmp;
2389 XEvent ev;
2390 int x=menu->frame_x, y=menu->frame_y;
2391 int dx=event->xbutton.x_root, dy=event->xbutton.y_root;
2392 int i, lower;
2393 Bool started;
2395 /* can't touch the menu copy */
2396 if (menu->flags.brother)
2397 return;
2399 if (event->xbutton.button != Button1 && event->xbutton.button != Button2)
2400 return;
2402 if (event->xbutton.state & MOD_MASK) {
2403 wLowerFrame(menu->frame->core);
2404 lower = 1;
2405 } else {
2406 wRaiseFrame(menu->frame->core);
2407 lower = 0;
2409 tmp = menu;
2411 /* lower/raise all submenus */
2412 while (1) {
2413 if (tmp->selected_entry>=0 && tmp->cascades
2414 && tmp->entries[tmp->selected_entry]->cascade>=0) {
2415 tmp = tmp->cascades[tmp->entries[tmp->selected_entry]->cascade];
2416 if (!tmp || !tmp->flags.mapped)
2417 break;
2418 if (lower)
2419 wLowerFrame(tmp->frame->core);
2420 else
2421 wRaiseFrame(tmp->frame->core);
2422 } else {
2423 break;
2427 /* tear off the menu if it's a root menu or a cascade
2428 application menu */
2429 if (!menu->flags.buttoned && !menu->flags.brother
2430 && (!menu->flags.app_menu||menu->parent!=NULL)) {
2431 menu->flags.buttoned=1;
2432 wFrameWindowShowButton(menu->frame, WFF_RIGHT_BUTTON);
2433 if (menu->parent) {
2434 /* turn off selected menu entry in parent menu */
2435 selectEntry(menu->parent, -1);
2437 /* make parent map the copy in place of the original */
2438 for (i=0; i<menu->parent->cascade_no; i++) {
2439 if (menu->parent->cascades[i] == menu) {
2440 menu->parent->cascades[i] = menu->brother;
2441 break;
2447 started = False;
2448 while(1) {
2449 WMMaskEvent(dpy, ButtonMotionMask|ButtonReleaseMask|ButtonPressMask
2450 |ExposureMask, &ev);
2451 switch (ev.type) {
2452 case MotionNotify:
2453 if (started) {
2454 x += ev.xmotion.x_root - dx;
2455 y += ev.xmotion.y_root - dy;
2456 dx = ev.xmotion.x_root;
2457 dy = ev.xmotion.y_root;
2458 wMenuMove(menu, x, y, True);
2459 } else {
2460 if (abs(ev.xmotion.x_root - dx) > MOVE_THRESHOLD
2461 || abs(ev.xmotion.y_root - dy) > MOVE_THRESHOLD) {
2462 started = True;
2463 XGrabPointer(dpy, menu->frame->titlebar->window, False,
2464 ButtonMotionMask|ButtonReleaseMask
2465 |ButtonPressMask,
2466 GrabModeAsync, GrabModeAsync, None,
2467 wCursor[WCUR_MOVE], CurrentTime);
2470 break;
2472 case ButtonPress:
2473 break;
2475 case ButtonRelease:
2476 if (ev.xbutton.button != event->xbutton.button)
2477 break;
2478 XUngrabPointer(dpy, CurrentTime);
2479 return;
2481 default:
2482 WMHandleEvent(&ev);
2483 break;
2489 *----------------------------------------------------------------------
2490 * menuCloseClick--
2491 * Handles mouse click on the close button of menus. The menu is
2492 * closed when the button is clicked.
2494 * Side effects:
2495 * The closed menu is reinserted at it's parent menus
2496 * cascade list.
2497 *----------------------------------------------------------------------
2499 static void
2500 menuCloseClick(WCoreWindow *sender, void *data, XEvent *event)
2502 WMenu *menu = (WMenu*)data;
2503 WMenu *parent = menu->parent;
2504 int i;
2506 if (parent) {
2507 for (i=0; i<parent->cascade_no; i++) {
2508 /* find the entry that points to the copy */
2509 if (parent->cascades[i] == menu->brother) {
2510 /* make it point to the original */
2511 parent->cascades[i] = menu;
2512 menu->parent = parent;
2513 break;
2517 wMenuUnmap(menu);
2521 static void
2522 saveMenuInfo(proplist_t dict, WMenu *menu, proplist_t key)
2524 proplist_t value, list;
2525 char buffer[256];
2527 sprintf(buffer, "%i,%i", menu->frame_x, menu->frame_y);
2528 value = PLMakeString(buffer);
2529 list = PLMakeArrayFromElements(value, NULL);
2530 if (menu->flags.lowered)
2531 PLAppendArrayElement(list, PLMakeString("lowered"));
2532 PLInsertDictionaryEntry(dict, key, list);
2533 PLRelease(value);
2534 PLRelease(list);
2538 void
2539 wMenuSaveState(WScreen *scr)
2541 proplist_t menus, key;
2542 int save_menus = 0;
2544 menus = PLMakeDictionaryFromEntries(NULL, NULL, NULL);
2546 #ifndef LITE
2547 if (scr->switch_menu && scr->switch_menu->flags.buttoned) {
2548 key = PLMakeString("SwitchMenu");
2549 saveMenuInfo(menus, scr->switch_menu, key);
2550 PLRelease(key);
2551 save_menus = 1;
2554 if (saveMenuRecurs(menus, scr, scr->root_menu))
2555 save_menus = 1;
2557 #endif /* !LITE */
2558 if (scr->workspace_menu && scr->workspace_menu->flags.buttoned) {
2559 key = PLMakeString("WorkspaceMenu");
2560 saveMenuInfo(menus, scr->workspace_menu, key);
2561 PLRelease(key);
2562 save_menus = 1;
2565 if (save_menus) {
2566 key = PLMakeString("Menus");
2567 PLInsertDictionaryEntry(scr->session_state, key, menus);
2568 PLRelease(key);
2570 PLRelease(menus);
2574 #ifndef LITE
2576 static Bool
2577 getMenuPath(WMenu *menu, char *buffer, int bufSize)
2579 Bool ok = True;
2580 int len = 0;
2582 if (!menu->flags.titled || !menu->frame->title[0])
2583 return False;
2585 len = strlen(menu->frame->title);
2586 if (len >= bufSize)
2587 return False;
2589 if (menu->parent) {
2590 ok = getMenuPath(menu->parent, buffer, bufSize - len - 1);
2591 if (!ok)
2592 return False;
2595 strcat(buffer, "\\");
2596 strcat(buffer, menu->frame->title);
2598 return True;
2602 static Bool
2603 saveMenuRecurs(proplist_t menus, WScreen *scr, WMenu *menu)
2605 proplist_t key;
2606 int save_menus = 0, i;
2607 char buffer[512];
2608 Bool ok = True;
2611 if (menu->flags.brother)
2612 menu = menu->brother;
2614 if (menu->flags.buttoned && menu != scr->switch_menu) {
2616 buffer[0] = '\0';
2617 ok = getMenuPath(menu, buffer, 510);
2619 if (ok) {
2620 key = PLMakeString(buffer);
2621 saveMenuInfo(menus, menu, key);
2622 PLRelease(key);
2623 save_menus = 1;
2627 if (ok) {
2628 for (i = 0; i < menu->cascade_no; i++) {
2629 if (saveMenuRecurs(menus, scr, menu->cascades[i]))
2630 save_menus = 1;
2633 return save_menus;
2635 #endif /* !LITE */
2638 #define COMPLAIN(key) wwarning(_("bad value in menus state info:%s"), key)
2641 static Bool
2642 getMenuInfo(proplist_t info, int *x, int *y, Bool *lowered)
2644 proplist_t pos;
2646 *lowered = False;
2648 if (PLIsArray(info)) {
2649 proplist_t flags;
2650 pos = PLGetArrayElement(info, 0);
2651 flags = PLGetArrayElement(info, 1);
2652 if (flags != NULL && PLIsString(flags) && PLGetString(flags) != NULL
2653 && strcmp(PLGetString(flags), "lowered") == 0) {
2654 *lowered = True;
2656 } else {
2657 pos = info;
2660 if (pos != NULL && PLIsString(pos)) {
2661 if (sscanf(PLGetString(pos), "%i,%i", x, y)!=2)
2662 COMPLAIN("Position");
2663 } else {
2664 COMPLAIN("(position, flags...)");
2665 return False;
2668 return True;
2672 static int
2673 restoreMenu(WScreen *scr, proplist_t menu, int which)
2675 int x, y;
2676 Bool lowered = False;
2677 WMenu *pmenu = NULL;
2679 if (!menu)
2680 return False;
2682 if (!getMenuInfo(menu, &x, &y, &lowered))
2683 return False;
2686 #ifndef LITE
2687 if (which & WSS_SWITCHMENU) {
2688 OpenSwitchMenu(scr, x, y, False);
2689 pmenu = scr->switch_menu;
2691 #endif /* !LITE */
2693 if (pmenu) {
2694 int width = MENUW(pmenu);
2695 int height = MENUH(pmenu);
2697 if (lowered) {
2698 changeMenuLevels(pmenu, True);
2701 x = (x < -width) ? 0 : x;
2702 x = (x > scr->scr_width) ? scr->scr_width - width : x;
2703 y = (y < 0) ? 0 : y;
2704 y = (y > scr->scr_height) ? scr->scr_height - height : y;
2705 wMenuMove(pmenu, x, y, True);
2706 pmenu->flags.buttoned = 1;
2707 wFrameWindowShowButton(pmenu->frame, WFF_RIGHT_BUTTON);
2708 return True;
2710 return False;
2714 #ifndef LITE
2715 static int
2716 restoreMenuRecurs(WScreen *scr, proplist_t menus, WMenu *menu, char *path)
2718 proplist_t key, entry;
2719 char buffer[512];
2720 int i, x, y, res;
2721 Bool lowered;
2723 if (strlen(path) + strlen(menu->frame->title) > 510)
2724 return False;
2726 sprintf(buffer, "%s\\%s", path, menu->frame->title);
2727 key = PLMakeString(buffer);
2728 entry = PLGetDictionaryEntry(menus, key);
2729 res = False;
2731 if (entry && getMenuInfo(entry, &x, &y, &lowered)) {
2733 if (!menu->flags.mapped) {
2734 int width = MENUW(menu);
2735 int height = MENUH(menu);
2737 wMenuMapAt(menu, x, y, False);
2739 if (menu->parent) {
2740 /* make parent map the copy in place of the original */
2741 for (i=0; i<menu->parent->cascade_no; i++) {
2742 if (menu->parent->cascades[i] == menu) {
2743 menu->parent->cascades[i] = menu->brother;
2744 break;
2748 if (lowered) {
2749 changeMenuLevels(menu, True);
2751 x = (x < -width) ? 0 : x;
2752 x = (x > scr->scr_width) ? scr->scr_width - width : x;
2753 y = (y < 0) ? 0 : y;
2754 y = (y > scr->scr_height) ? scr->scr_height - height : y;
2755 wMenuMove(menu, x, y, True);
2756 menu->flags.buttoned = 1;
2757 wFrameWindowShowButton(menu->frame, WFF_RIGHT_BUTTON);
2758 res = True;
2762 PLRelease(key);
2764 for (i=0; i<menu->cascade_no; i++) {
2765 if (restoreMenuRecurs(scr, menus, menu->cascades[i], buffer) != False)
2766 res = True;
2769 return res;
2771 #endif /* !LITE */
2774 void
2775 wMenuRestoreState(WScreen *scr)
2777 proplist_t menus, menu, key, skey;
2779 key = PLMakeString("Menus");
2780 menus = PLGetDictionaryEntry(scr->session_state, key);
2781 PLRelease(key);
2783 if (!menus)
2784 return;
2786 /* restore menus */
2788 skey = PLMakeString("SwitchMenu");
2789 menu = PLGetDictionaryEntry(menus, skey);
2790 PLRelease(skey);
2791 restoreMenu(scr, menu, WSS_SWITCHMENU);
2793 #ifndef LITE
2794 if (!scr->root_menu) {
2795 OpenRootMenu(scr, scr->scr_width*2, 0, False);
2796 wMenuUnmap(scr->root_menu);
2798 restoreMenuRecurs(scr, menus, scr->root_menu, "");
2799 #endif /* !LITE */
2803 void
2804 OpenWorkspaceMenu(WScreen *scr, int x, int y)
2806 WMenu *menu, *parent;
2807 WMenuEntry *entry;
2809 #ifndef LITE
2810 if (!scr->root_menu) {
2811 OpenRootMenu(scr, scr->scr_width*2, 0, False);
2812 wMenuUnmap(scr->root_menu);
2814 #endif
2815 menu = scr->workspace_menu;
2816 if (menu) {
2817 if (menu->flags.mapped) {
2818 if (!menu->flags.buttoned) {
2819 wMenuUnmap(menu);
2820 parent = menu->parent;
2821 if (parent && parent->selected_entry >= 0) {
2822 entry = parent->entries[parent->selected_entry];
2823 if (parent->cascades[entry->cascade] == menu) {
2824 selectEntry(parent, -1);
2825 wMenuMapAt(menu, x, y, False);
2828 } else {
2829 wRaiseFrame(menu->frame->core);
2830 wMenuMapCopyAt(menu, x, y);
2832 } else {
2833 wMenuMapAt(menu, x, y, False);