Initial revision
[wmaker-crm.git] / src / menu.c
blob13087afa978f8bff2612876be53dd16a0675a2a5
1 /* menu.c- generic menu, used for root menu, application menus etc.
3 * WindowMaker window manager
4 *
5 * Copyright (c) 1997, 1998 Alfredo K. Kojima
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
20 * USA.
23 #include "wconfig.h"
25 #include <X11/Xlib.h>
26 #include <X11/Xutil.h>
27 #include <X11/keysym.h>
28 #include <stdlib.h>
29 #include <string.h>
30 #include <stdio.h>
31 #include <unistd.h>
32 #include <ctype.h>
34 #include "WindowMaker.h"
35 #include "wcore.h"
36 #include "framewin.h"
37 #include "menu.h"
38 #include "actions.h"
39 #include "funcs.h"
40 #include "stacking.h"
41 #include "text.h"
44 /****** Global Variables ******/
46 extern Cursor wCursor[WCUR_LAST];
48 extern XContext wWinContext;
50 extern WPreferences wPreferences;
52 #define MOD_MASK wPreferences.modifier_mask
54 #define MENU_SCROLL_STEP menuScrollParameters[wPreferences.menu_scroll_speed].steps
55 #define MENU_SCROLL_DELAY menuScrollParameters[wPreferences.menu_scroll_speed].delay
59 #define MENUW(m) ((m)->frame->core->width+2*FRAME_BORDER_WIDTH)
60 #define MENUH(m) ((m)->frame->core->height+2*FRAME_BORDER_WIDTH)
63 /***** Local Stuff ******/
65 static struct {
66 int steps;
67 int delay;
68 } menuScrollParameters[5] = {
69 {MENU_SCROLL_STEPS_UF, MENU_SCROLL_DELAY_UF},
70 {MENU_SCROLL_STEPS_F, MENU_SCROLL_DELAY_F},
71 {MENU_SCROLL_STEPS_M, MENU_SCROLL_DELAY_M},
72 {MENU_SCROLL_STEPS_S, MENU_SCROLL_DELAY_S},
73 {MENU_SCROLL_STEPS_U, MENU_SCROLL_DELAY_U}};
76 static void menuMouseDown(WObjDescriptor *desc, XEvent *event);
77 static void menuExpose(WObjDescriptor *desc, XEvent *event);
79 static void menuTitleDoubleClick(WCoreWindow *sender, void *data, XEvent *event);
80 static void menuTitleMouseDown(WCoreWindow *sender, void *data, XEvent *event);
82 static void menuCloseClick(WCoreWindow *sender, void *data, XEvent *event);
86 static void selectEntry(WMenu *menu, int entry_no);
87 static void closeCascade(WMenu *menu);
92 *----------------------------------------------------------------------
93 * wMenuCreate--
94 * Creates a new empty menu with the specified title. If main_menu
95 * is True, the created menu will be a main menu, which has some special
96 * properties such as being placed over other normal menus.
97 * If title is NULL, the menu will have no titlebar.
99 * Returns:
100 * The created menu.
101 *----------------------------------------------------------------------
103 WMenu*
104 wMenuCreate(WScreen *screen, char *title, int main_menu)
106 WMenu *menu;
107 static int brother=0;
108 int tmp, flags;
110 menu = wmalloc(sizeof(WMenu));
112 memset(menu, 0, sizeof(WMenu));
114 #ifdef SINGLE_MENULEVEL
115 tmp = WMSubmenuWindowLevel;
116 #else
117 tmp = (main_menu ? WMMainMenuWindowLevel : WMSubmenuWindowLevel);
118 #endif
120 flags = WFF_SINGLE_STATE;
121 if (title) {
122 flags |= WFF_TITLEBAR|WFF_RIGHT_BUTTON;
123 menu->flags.titled = 1;
125 menu->frame =
126 wFrameWindowCreate(screen, tmp, 8, 2, 1, 1, flags,
127 screen->menu_title_texture, NULL,
128 screen->menu_title_pixel, &screen->menu_title_gc,
129 &screen->menu_title_font);
131 menu->frame->core->descriptor.parent = menu;
132 menu->frame->core->descriptor.parent_type = WCLASS_MENU;
133 menu->frame->core->descriptor.handle_mousedown = menuMouseDown;
135 wFrameWindowHideButton(menu->frame, WFF_RIGHT_BUTTON);
137 if (title) {
138 menu->frame->title = wstrdup(title);
141 menu->frame->flags.justification = WTJ_LEFT;
143 menu->frame->rbutton_image = screen->b_pixmaps[WBUT_CLOSE];
145 menu->entry_no = 0;
146 menu->alloced_entries = 0;
147 menu->selected_entry = -1;
148 menu->entries = NULL;
150 menu->frame_x = screen->app_menu_x;
151 menu->frame_y = screen->app_menu_y;
153 menu->frame->child = menu;
155 menu->flags.lowered = 0;
157 /* create borders */
158 if (title) {
159 /* setup object descriptors */
160 menu->frame->on_mousedown_titlebar = menuTitleMouseDown;
161 menu->frame->on_dblclick_titlebar = menuTitleDoubleClick;
164 menu->frame->on_click_right = menuCloseClick;
167 menu->menu = wCoreCreate(menu->frame->core, 0, menu->frame->top_width,
168 menu->frame->core->width, 10);
170 menu->menu->descriptor.parent = menu;
171 menu->menu->descriptor.parent_type = WCLASS_MENU;
172 menu->menu->descriptor.handle_expose = menuExpose;
173 menu->menu->descriptor.handle_mousedown = menuMouseDown;
175 menu->menu_texture_data = None;
177 XMapWindow(dpy, menu->menu->window);
179 XFlush(dpy);
181 if (!brother) {
182 brother = 1;
183 menu->brother = wMenuCreate(screen, title, main_menu);
184 brother = 0;
185 menu->brother->flags.brother = 1;
186 menu->brother->brother = menu;
190 return menu;
196 WMenu*
197 wMenuCreateForApp(WScreen *screen, char *title, int main_menu)
199 WMenu *menu;
201 menu = wMenuCreate(screen, title, main_menu);
202 if (!menu)
203 return NULL;
204 menu->flags.app_menu = 1;
205 menu->brother->flags.app_menu = 1;
207 return menu;
212 static void
213 insertEntry(WMenu *menu, WMenuEntry *entry, int index)
215 int i;
217 for (i=menu->entry_no; i>index; i--) {
218 menu->entries[i]->order++;
219 menu->entries[i+1]=menu->entries[i];
221 menu->entries[index] = entry;
225 void
226 wMenuRefresh(WMenu *menu, int flags)
228 int i;
230 if (flags & MR_TEXT_BACK) {
231 menu->frame->flags.need_texture_remake = 1;
234 if (flags & (MR_RESIZED|MR_TITLE_TEXT)) {
235 menu->flags.realized = 0;
238 wMenuRealize(menu);
240 if (menu->flags.titled)
241 wFrameWindowPaint(menu->frame);
243 if (!menu->flags.brother) {
244 if (menu->brother)
245 wMenuRefresh(menu->brother, flags);
247 for (i=0; i < menu->cascade_no; i++) {
248 if (!menu->cascades[i]->flags.brother)
249 wMenuRefresh(menu->cascades[i], flags);
250 else
251 wMenuRefresh(menu->cascades[i]->brother, flags);
258 WMenuEntry*
259 wMenuInsertCallback(WMenu *menu, int index, char *text,
260 void (*callback)(WMenu *menu, WMenuEntry *entry),
261 void *clientdata)
263 WMenuEntry *entry;
265 #ifdef DEBUG
266 if (!menu) {
267 printf("Passed NULL as menu parameter to wMenuAddCallback() \n");
268 return NULL;
270 #endif
272 assert(menu->flags.brother==0);
273 menu->flags.realized = 0;
274 menu->brother->flags.realized = 0;
276 /* reallocate array if it's too small */
277 if (menu->entry_no >= menu->alloced_entries) {
278 void *tmp;
279 #ifdef DEBUG
280 puts("doing wrealloc()");
281 #endif
282 tmp = wrealloc(menu->entries,
283 sizeof(WMenuEntry)*(menu->alloced_entries+5));
284 if (tmp==NULL) {
285 wwarning(_("wrealloc() failed while trying to add menu item"));
286 return NULL;
289 menu->entries = tmp;
290 menu->alloced_entries += 5;
292 menu->brother->entries = tmp;
293 menu->brother->alloced_entries = menu->alloced_entries;
295 entry = wmalloc(sizeof(WMenuEntry));
296 memset(entry, 0, sizeof(WMenuEntry));
297 entry->flags.enabled = 1;
298 entry->text = wstrdup(text);
299 entry->cascade = -1;
300 entry->clientdata = clientdata;
301 entry->callback = callback;
302 if (index<0 || index>=menu->entry_no) {
303 entry->order = menu->entry_no;
304 menu->entries[menu->entry_no] = entry;
305 } else {
306 entry->order = index;
307 insertEntry(menu, entry, index);
310 menu->entry_no++;
311 menu->brother->entry_no = menu->entry_no;
313 return entry;
318 void
319 wMenuEntrySetCascade(WMenu *menu, WMenuEntry *entry, WMenu *cascade)
321 WMenu *brother = menu->brother;
322 int i, done;
324 assert(menu->flags.brother==0);
326 if (entry->cascade>=0) {
327 menu->flags.realized = 0;
328 brother->flags.realized = 0;
331 cascade->parent = menu;
333 cascade->brother->parent = brother;
335 done = 0;
336 for (i=0; i<menu->cascade_no; i++) {
337 if (menu->cascades[i]==NULL) {
338 menu->cascades[i] = cascade;
339 brother->cascades[i] = cascade->brother;
340 done = 1;
341 entry->cascade = i;
342 break;
345 if (!done) {
346 entry->cascade = menu->cascade_no;
348 menu->cascades = wrealloc(menu->cascades,
349 sizeof(WMenu)*(menu->cascade_no+1));
350 menu->cascades[menu->cascade_no++] = cascade;
353 brother->cascades = wrealloc(brother->cascades,
354 sizeof(WMenu)*(brother->cascade_no+1));
355 brother->cascades[brother->cascade_no++] = cascade->brother;
359 if (menu->flags.lowered) {
361 cascade->flags.lowered = 1;
362 ChangeStackingLevel(cascade->frame->core, WMNormalWindowLevel);
364 cascade->brother->flags.lowered = 1;
365 ChangeStackingLevel(cascade->brother->frame->core, WMNormalWindowLevel);
368 if (!menu->flags.realized)
369 wMenuRealize(menu);
373 void
374 wMenuEntryRemoveCascade(WMenu *menu, WMenuEntry *entry)
376 assert(menu->flags.brother==0);
378 /* destroy cascade menu */
379 if (entry->cascade>=0 && menu->cascades
380 && menu->cascades[entry->cascade]!=NULL) {
382 wMenuDestroy(menu->cascades[entry->cascade], True);
384 menu->cascades[entry->cascade] = NULL;
385 menu->brother->cascades[entry->cascade] = NULL;
387 entry->cascade = -1;
392 void
393 wMenuRemoveItem(WMenu *menu, int index)
395 int i;
397 if (menu->flags.brother) {
398 wMenuRemoveItem(menu->brother, index);
399 return;
402 if (index>=menu->entry_no) return;
404 /* destroy cascade menu */
405 wMenuEntryRemoveCascade(menu, menu->entries[index]);
407 /* destroy unshared data */
409 if (menu->entries[index]->text)
410 free(menu->entries[index]->text);
412 if (menu->entries[index]->rtext)
413 free(menu->entries[index]->rtext);
415 if (menu->entries[index]->free_cdata && menu->entries[index]->clientdata)
416 (*menu->entries[index]->free_cdata)(menu->entries[index]->clientdata);
418 free(menu->entries[index]);
420 for (i=index; i<menu->entry_no-1; i++) {
421 menu->entries[i+1]->order--;
422 menu->entries[i]=menu->entries[i+1];
424 menu->entry_no--;
425 menu->brother->entry_no--;
430 void
431 wMenuRealize(WMenu *menu)
433 int i;
434 int width, rwidth, mrwidth, mwidth;
435 int theight, twidth, eheight;
436 WScreen *scr = menu->frame->screen_ptr;
437 static int brother_done=0;
438 int flags;
440 if (!brother_done) {
441 brother_done = 1;
442 wMenuRealize(menu->brother);
443 brother_done = 0;
446 flags = WFF_SINGLE_STATE;
447 if (menu->flags.titled)
448 flags |= WFF_TITLEBAR|WFF_RIGHT_BUTTON;
450 wFrameWindowUpdateBorders(menu->frame, flags);
452 if (menu->flags.titled) {
453 twidth = wTextWidth(scr->menu_title_font->font, menu->frame->title,
454 strlen(menu->frame->title));
455 theight = menu->frame->top_width;
456 twidth += theight + theight/2 + (wPreferences.new_style ? 8 : 0);
457 } else {
458 twidth = 0;
459 theight = 0;
461 eheight = scr->menu_entry_font->height + 6;
462 menu->entry_height = eheight;
463 mrwidth = 0;
464 mwidth = 0;
465 for (i=0; i<menu->entry_no; i++) {
466 char *text;
468 /* search widest text */
469 text = menu->entries[i]->text;
470 width = wTextWidth(scr->menu_entry_font->font, text, strlen(text))+12;
472 if (menu->entries[i]->flags.indicator) {
473 width += MENU_INDICATOR_SPACE;
476 if (width > mwidth)
477 mwidth = width;
479 /* search widest text on right */
480 text = menu->entries[i]->rtext;
481 if (text)
482 rwidth = wTextWidth(scr->menu_entry_font->font, text,
483 strlen(text)) + 5;
484 else if (menu->entries[i]->cascade>=0)
485 rwidth = eheight;
486 else
487 rwidth = 4;
489 if (rwidth > mrwidth)
490 mrwidth = rwidth;
492 mwidth += mrwidth;
494 if (mwidth < twidth)
495 mwidth = twidth;
497 wCoreConfigure(menu->menu, 0, theight, mwidth, menu->entry_no*eheight -1);
499 wFrameWindowResizeInternal(menu->frame, mwidth, menu->entry_no*eheight -1);
502 /* setup background texture */
503 switch (scr->menu_item_texture->any.type) {
504 case WTEX_DGRADIENT:
505 case WTEX_VGRADIENT:
506 case WTEX_HGRADIENT:
507 case WTEX_MHGRADIENT:
508 case WTEX_MVGRADIENT:
509 case WTEX_MDGRADIENT:
510 case WTEX_PIXMAP:
511 if (!menu->flags.brother) {
512 FREE_PIXMAP(menu->menu_texture_data);
514 wTextureRender(scr, scr->menu_item_texture,
515 &menu->menu_texture_data, menu->menu->width,
516 menu->entry_height, WREL_MENUENTRY);
517 XSetWindowBackgroundPixmap(dpy, menu->menu->window,
518 menu->menu_texture_data);
519 XClearWindow(dpy, menu->menu->window);
521 XSetWindowBackgroundPixmap(dpy, menu->brother->menu->window,
522 menu->menu_texture_data);
523 XClearWindow(dpy, menu->brother->menu->window);
525 break;
527 default:
528 XSetWindowBackground(dpy, menu->menu->window,
529 scr->menu_item_texture->any.color.pixel);
530 XClearWindow(dpy, menu->menu->window);
533 menu->flags.realized = 1;
535 if (menu->flags.mapped)
536 wMenuPaint(menu);
537 if (menu->brother->flags.mapped)
538 wMenuPaint(menu->brother);
542 void
543 wMenuDestroy(WMenu *menu, int recurse)
545 int i;
547 /* remove any pending timers */
548 if (menu->timer)
549 WMDeleteTimerHandler(menu->timer);
550 menu->timer = NULL;
552 /* call destroy handler */
553 if (menu->on_destroy)
554 (*menu->on_destroy)(menu);
556 /* Destroy items if this menu own them. If this is the "brother" menu,
557 * leave them alone as it is shared by them.
559 if (!menu->flags.brother) {
560 for (i=0; i<menu->entry_no; i++) {
562 free(menu->entries[i]->text);
564 if (menu->entries[i]->rtext)
565 free(menu->entries[i]->rtext);
567 if (menu->entries[i]->free_cdata && menu->entries[i]->clientdata) {
568 (*menu->entries[i]->free_cdata)(menu->entries[i]->clientdata);
570 free(menu->entries[i]);
573 if (recurse) {
574 for (i=0; i<menu->cascade_no; i++) {
575 if (menu->cascades[i]) {
576 if (menu->cascades[i]->flags.brother)
577 wMenuDestroy(menu->cascades[i]->brother, recurse);
578 else
579 wMenuDestroy(menu->cascades[i], recurse);
584 if (menu->entries)
585 free(menu->entries);
589 FREE_PIXMAP(menu->menu_texture_data);
591 if (menu->cascades)
592 free(menu->cascades);
594 wCoreDestroy(menu->menu);
595 wFrameWindowDestroy(menu->frame);
597 /* destroy copy of this menu */
598 if (!menu->flags.brother && menu->brother)
599 wMenuDestroy(menu->brother, False);
601 free(menu);
605 static void
606 drawFrame(WScreen *scr, Window win, int y, int w, int h)
608 XSegment segs[2];
610 segs[0].x1 = 0;
611 segs[0].y1 = y;
612 segs[0].x2 = w-1;
613 segs[0].y2 = y;
614 segs[1].x1 = 0;
615 segs[1].y1 = y;
616 segs[1].x2 = 0;
617 segs[1].y2 = y + h - 2;
618 XDrawSegments(dpy, win, scr->menu_item_auxtexture->light_gc, segs, 2);
620 XDrawLine(dpy, win, scr->menu_item_auxtexture->dark_gc, 0, y+h-1,
621 w-1, y+h-1);
623 segs[0].x1 = 1;
624 segs[0].y1 = segs[0].y2 = y + h-2;
625 segs[0].x2 = w-1;
626 segs[1].x1 = segs[1].x2 = w-1;
627 segs[1].y1 = y + 1;
628 segs[1].y2 = y + h-2;
629 XDrawSegments(dpy, win, scr->menu_item_auxtexture->dim_gc, segs, 2);
633 static void
634 paintEntry(WMenu *menu, int index, int selected)
636 int x, y, w, h, tw;
637 GC light, dim, dark, textGC;
638 WScreen *scr=menu->frame->screen_ptr;
639 Window win = menu->menu->window;
640 WMenuEntry *entry=menu->entries[index];
642 if (!menu->flags.realized) return;
643 h = menu->entry_height;
644 w = menu->menu->width;
645 y = index * h;
647 light = scr->menu_item_auxtexture->light_gc;
648 dim = scr->menu_item_auxtexture->dim_gc;
649 dark = scr->menu_item_auxtexture->dark_gc;
651 /* paint background */
652 if (selected) {
653 XSetForeground(dpy, scr->select_menu_gc, scr->select_pixel);
654 XFillRectangle(dpy, win, scr->select_menu_gc, 1, y+1, w-2, h-3);
655 if (scr->menu_item_texture->any.type == WTEX_SOLID)
656 drawFrame(scr, win, y, w, h);
657 } else {
658 if (scr->menu_item_texture->any.type == WTEX_SOLID) {
659 XClearArea(dpy, win, 0, y + 1, w - 1, h - 3, False);
660 /* draw the frame */
661 drawFrame(scr, win, y, w, h);
662 } else {
663 XClearArea(dpy, win, 0, y, w, h, False);
667 if (selected) {
668 textGC = scr->select_menu_gc;
669 if (entry->flags.enabled)
670 XSetForeground(dpy, textGC, scr->select_text_pixel);
671 else
672 XSetForeground(dpy, textGC, scr->dtext_pixel);
673 } else if (!entry->flags.enabled) {
674 textGC = scr->disabled_menu_entry_gc;
675 } else {
676 textGC = scr->menu_entry_gc;
678 /* draw text */
679 x = 5;
680 if (entry->flags.indicator)
681 x += MENU_INDICATOR_SPACE + 2;
683 wDrawString(win, scr->menu_entry_font,
684 textGC, x, 3+y+scr->menu_entry_font->y, entry->text,
685 strlen(entry->text));
687 if (entry->cascade>=0) {
688 /* draw the cascade indicator */
689 XDrawLine(dpy,win,dim, w-11, y+6, w-6, y+h/2-1);
690 XDrawLine(dpy,win,light, w-11, y+h-8, w-6, y+h/2-1);
691 XDrawLine(dpy,win,dark, w-12, y+6, w-12, y+h-8);
694 /* draw indicator */
695 if (entry->flags.indicator && entry->flags.indicator_on) {
696 int iw, ih;
697 WPixmap *indicator;
700 switch (entry->flags.indicator_type) {
701 case MI_CHECK:
702 indicator = scr->menu_check_indicator;
703 break;
704 case MI_MINIWINDOW:
705 indicator = scr->menu_mini_indicator;
706 break;
707 case MI_HIDDEN:
708 indicator = scr->menu_hide_indicator;
709 break;
710 case MI_SHADED:
711 indicator = scr->menu_shade_indicator;
712 break;
713 case MI_DIAMOND:
714 default:
715 indicator = scr->menu_radio_indicator;
716 break;
719 iw = indicator->width;
720 ih = indicator->height;
721 XSetClipMask(dpy, scr->copy_gc, indicator->mask);
722 XSetClipOrigin(dpy, scr->copy_gc, 5, y+(h-ih)/2);
723 if (selected)
724 XSetForeground(dpy, scr->copy_gc, scr->black_pixel);
725 else
726 XSetForeground(dpy, scr->copy_gc, scr->mtext_pixel);
727 XFillRectangle(dpy, win, scr->copy_gc, 5, y+(h-ih)/2, iw, ih);
729 XCopyArea(dpy, indicator->image, win, scr->copy_gc, 0, 0,
730 iw, ih, 5, y+(h-ih)/2);
732 XSetClipOrigin(dpy, scr->copy_gc, 0, 0);
735 /* draw right text */
737 if (entry->rtext && entry->cascade<0) {
738 tw = wTextWidth(scr->menu_entry_font->font, entry->rtext,
739 strlen(entry->rtext));
741 wDrawString(win, scr->menu_entry_font, textGC, w-6-tw,
742 3+y+scr->menu_entry_font->y, entry->rtext,
743 strlen(entry->rtext));
747 static void
748 move_menus(WMenu *menu, int x, int y)
750 while (menu->parent) {
751 menu = menu->parent;
752 x -= MENUW(menu);
753 if (!wPreferences.align_menus && menu->selected_entry>=0) {
754 y -= menu->selected_entry*menu->entry_height;
757 wMenuMove(menu, x, y, True);
760 static void
761 makeVisible(WMenu *menu)
763 WScreen *scr = menu->frame->screen_ptr;
764 int x1, y1, x2, y2, new_x, new_y, move;
766 if (menu->entry_no<0) return;
768 x1 = menu->frame_x;
769 y1 = menu->frame_y+menu->frame->top_width
770 + menu->selected_entry*menu->entry_height;
771 x2 = x1 + MENUW(menu);
772 y2 = y1 + menu->entry_height;
774 new_x = x1;
775 new_y = y1;
776 move = 0;
778 if (x1 < 0) {
779 new_x = 0;
780 move = 1;
781 } else if (x2 >= scr->scr_width) {
782 new_x = scr->scr_width - MENUW(menu) - 1;
783 move = 1;
786 if (y1 < 0) {
787 new_y = 0;
788 move = 1;
789 } else if (y2 >= scr->scr_height) {
790 new_y = scr->scr_height - menu->entry_height - 1;
791 move = 1;
794 new_y = new_y - menu->frame->top_width
795 - menu->selected_entry*menu->entry_height;
796 move_menus(menu, new_x, new_y);
801 static int
802 check_key(WMenu *menu, XKeyEvent *event)
804 int i, ch, s;
805 char buffer[32];
807 if (XLookupString(event, buffer, 32, NULL, NULL)<1)
808 return -1;
810 ch = toupper(buffer[0]);
812 s = (menu->selected_entry>=0 ? menu->selected_entry+1 : 0);
814 again:
815 for (i=s; i<menu->entry_no; i++) {
816 if (ch==toupper(menu->entries[i]->text[0])) {
817 return i;
820 /* no match. Retry from start, if previous started from a selected entry */
821 if (s!=0) {
822 s = 0;
823 goto again;
825 return -1;
829 static int
830 keyboardMenu(WMenu *menu)
832 XEvent event;
833 KeySym ksym=NoSymbol;
834 int done=0;
835 int index;
836 WMenuEntry *entry;
837 int old_pos_x = menu->frame_x;
838 int old_pos_y = menu->frame_y;
839 int new_x = old_pos_x, new_y = old_pos_y;
840 int scr_width = menu->frame->screen_ptr->scr_width;
841 int scr_height = menu->frame->screen_ptr->scr_height;
843 if (menu->flags.editing)
844 return False;
847 XGrabKeyboard(dpy, menu->frame->core->window, True, GrabModeAsync,
848 GrabModeAsync, CurrentTime);
850 if (menu->frame_y+menu->frame->top_width >= scr_height)
851 new_y = scr_height - menu->frame->top_width;
853 if (menu->frame_x+MENUW(menu) >= scr_width)
854 new_x = scr_width-MENUW(menu)-1;
856 move_menus(menu, new_x, new_y);
858 while (!done) {
859 XAllowEvents(dpy, AsyncKeyboard, CurrentTime);
860 WMMaskEvent(dpy, ExposureMask|ButtonMotionMask|ButtonPressMask
861 |ButtonReleaseMask|KeyPressMask|KeyReleaseMask, &event);
863 switch (event.type) {
864 case KeyPress:
865 ksym = XLookupKeysym(&event.xkey, 0);
866 switch (ksym) {
867 case XK_Escape:
868 done = 1;
869 break;
871 case XK_Home:
872 case XK_KP_Home:
873 selectEntry(menu, 0);
874 makeVisible(menu);
875 break;
877 case XK_End:
878 case XK_KP_End:
879 selectEntry(menu, menu->entry_no-1);
880 makeVisible(menu);
881 break;
883 case XK_Up:
884 #ifdef ARROWLESS_KBD
885 case XK_k:
886 #endif
887 case XK_KP_Up:
888 if (menu->selected_entry <= 0)
889 selectEntry(menu, menu->entry_no-1);
890 else
891 selectEntry(menu, menu->selected_entry-1);
892 makeVisible(menu);
893 break;
895 case XK_Down:
896 #ifdef ARROWLESS_KBD
897 case XK_j:
898 #endif
899 case XK_KP_Down:
900 if (menu->selected_entry<0)
901 selectEntry(menu, 0);
902 else if (menu->selected_entry == menu->entry_no-1)
903 selectEntry(menu, 0);
904 else if (menu->selected_entry < menu->entry_no-1)
905 selectEntry(menu, menu->selected_entry+1);
906 makeVisible(menu);
907 break;
909 case XK_Right:
910 #ifdef ARROWLESS_KBD
911 case XK_l:
912 #endif
913 case XK_KP_Right:
914 if (menu->selected_entry>=0) {
915 WMenuEntry *entry;
916 entry = menu->entries[menu->selected_entry];
918 if (entry->cascade >= 0 && menu->cascades
919 && menu->cascades[entry->cascade]->entry_no > 0) {
921 XUngrabKeyboard(dpy, CurrentTime);
923 selectEntry(menu->cascades[entry->cascade], 0);
924 if (!keyboardMenu(menu->cascades[entry->cascade]))
925 done = 1;
927 XGrabKeyboard(dpy, menu->frame->core->window, True,
928 GrabModeAsync, GrabModeAsync,
929 CurrentTime);
932 break;
934 case XK_Left:
935 #ifdef ARROWLESS_KBD
936 case XK_h:
937 #endif
938 case XK_KP_Left:
939 if (menu->parent!=NULL && menu->parent->selected_entry>=0) {
940 selectEntry(menu, -1);
941 move_menus(menu, old_pos_x, old_pos_y);
942 return True;
944 break;
946 case XK_Return:
947 done = 2;
948 break;
950 default:
951 index = check_key(menu, &event.xkey);
952 if (index>=0) {
953 selectEntry(menu, index);
956 break;
958 default:
959 if (event.type==ButtonPress)
960 done = 1;
961 WMHandleEvent(&event);
965 XUngrabKeyboard(dpy, CurrentTime);
967 if (done==2 && menu->selected_entry>=0) {
968 entry = menu->entries[menu->selected_entry];
969 } else {
970 entry = NULL;
973 if (entry && entry->callback!=NULL && entry->flags.enabled
974 && entry->cascade < 0) {
975 #if (MENU_BLINK_COUNT > 0)
976 int sel = menu->selected_entry;
977 int i;
979 for (i=0; i<MENU_BLINK_COUNT; i++) {
980 paintEntry(menu, sel, False);
981 XSync(dpy, 0);
982 wusleep(MENU_BLINK_DELAY);
983 paintEntry(menu, sel, True);
984 XSync(dpy, 0);
985 wusleep(MENU_BLINK_DELAY);
987 #endif
988 selectEntry(menu, -1);
990 if (!menu->flags.buttoned) {
991 wMenuUnmap(menu);
992 move_menus(menu, old_pos_x, old_pos_y);
994 closeCascade(menu);
996 (*entry->callback)(menu, entry);
997 } else {
998 if (!menu->flags.buttoned) {
999 wMenuUnmap(menu);
1000 move_menus(menu, old_pos_x, old_pos_y);
1002 selectEntry(menu, -1);
1006 /* returns True if returning from a submenu to a parent menu,
1007 * False if exiting from menu */
1008 return False;
1012 void
1013 wMenuMapAt(WMenu *menu, int x, int y, int keyboard)
1015 int scr_width = menu->frame->screen_ptr->scr_width;
1016 int scr_height = menu->frame->screen_ptr->scr_height;
1018 if (!menu->flags.realized) {
1019 menu->flags.realized=1;
1020 wMenuRealize(menu);
1022 if (!menu->flags.mapped) {
1023 if (wPreferences.wrap_menus) {
1024 if (x<0) x = 0;
1025 if (y<0) y = 0;
1026 if (x+MENUW(menu) > scr_width)
1027 x = scr_width - MENUW(menu);
1028 if (y+MENUH(menu) > scr_height)
1029 y = scr_height - MENUH(menu);
1032 XMoveWindow(dpy, menu->frame->core->window, x, y);
1033 menu->frame_x = x;
1034 menu->frame_y = y;
1035 XMapWindow(dpy, menu->frame->core->window);
1036 wRaiseFrame(menu->frame->core);
1037 menu->flags.mapped = 1;
1038 } else {
1039 selectEntry(menu, 0);
1042 if (keyboard)
1043 keyboardMenu(menu);
1047 void
1048 wMenuMap(WMenu *menu)
1050 if (!menu->flags.realized) {
1051 menu->flags.realized=1;
1052 wMenuRealize(menu);
1054 if (menu->flags.app_menu && menu->parent==NULL) {
1055 menu->frame_x = menu->frame->screen_ptr->app_menu_x;
1056 menu->frame_y = menu->frame->screen_ptr->app_menu_y;
1057 XMoveWindow(dpy, menu->frame->core->window, menu->frame_x, menu->frame_y);
1059 XMapWindow(dpy, menu->frame->core->window);
1060 wRaiseFrame(menu->frame->core);
1061 menu->flags.mapped = 1;
1065 void
1066 wMenuUnmap(WMenu *menu)
1068 int i;
1070 XUnmapWindow(dpy, menu->frame->core->window);
1071 if (menu->flags.titled && menu->flags.buttoned) {
1072 wFrameWindowHideButton(menu->frame, WFF_RIGHT_BUTTON);
1074 menu->flags.buttoned = 0;
1075 menu->flags.mapped = 0;
1076 menu->flags.open_to_left = 0;
1078 for (i=0; i<menu->cascade_no; i++) {
1079 if (menu->cascades[i]!=NULL
1080 && menu->cascades[i]->flags.mapped
1081 && !menu->cascades[i]->flags.buttoned) {
1083 wMenuUnmap(menu->cascades[i]);
1086 menu->selected_entry = -1;
1091 void
1092 wMenuPaint(WMenu *menu)
1094 int i;
1096 if (!menu->flags.mapped) {
1097 return;
1100 /* paint entries */
1101 for (i=0; i<menu->entry_no; i++) {
1102 paintEntry(menu, i, i==menu->selected_entry);
1107 void
1108 wMenuSetEnabled(WMenu *menu, int index, int enable)
1110 if (index>=menu->entry_no) return;
1111 menu->entries[index]->flags.enabled=enable;
1112 paintEntry(menu, index, index==menu->selected_entry);
1113 paintEntry(menu->brother, index, index==menu->selected_entry);
1117 /* ====================================================================== */
1120 static void
1121 editEntry(WMenu *menu, WMenuEntry *entry)
1123 WTextInput *text;
1124 XEvent event;
1125 WObjDescriptor *desc;
1126 char *t;
1127 int done = 0;
1128 Window old_focus;
1129 int old_revert;
1131 menu->flags.editing = 1;
1133 text = wTextCreate(menu->menu, 1, menu->entry_height * entry->order,
1134 menu->menu->width - 2, menu->entry_height - 1);
1136 wTextPutText(text, entry->text);
1137 XGetInputFocus(dpy, &old_focus, &old_revert);
1138 XSetInputFocus(dpy, text->core->window, RevertToNone, CurrentTime);
1140 if (XGrabKeyboard(dpy, text->core->window, True, GrabModeAsync,
1141 GrabModeAsync, CurrentTime)!=GrabSuccess) {
1142 wwarning("could not grab keyboard");
1143 wTextDestroy(text);
1145 wSetFocusTo(menu->frame->screen_ptr,
1146 menu->frame->screen_ptr->focused_window);
1147 return;
1151 while (!done && !text->done) {
1152 XSync(dpy, 0);
1153 XAllowEvents(dpy, AsyncKeyboard|AsyncPointer, CurrentTime);
1154 XSync(dpy, 0);
1155 WMNextEvent(dpy, &event);
1157 if (XFindContext(dpy, event.xany.window, wWinContext,
1158 (XPointer *)&desc)==XCNOENT)
1159 desc = NULL;
1161 if ((desc != NULL) && (desc->handle_anything != NULL)) {
1163 (*desc->handle_anything)(desc, &event);
1165 } else {
1166 switch (event.type) {
1167 case ButtonPress:
1168 XAllowEvents(dpy, ReplayPointer, CurrentTime);
1169 done = 1;
1171 default:
1172 WMHandleEvent(&event);
1177 XSetInputFocus(dpy, old_focus, old_revert, CurrentTime);
1179 wSetFocusTo(menu->frame->screen_ptr,
1180 menu->frame->screen_ptr->focused_window);
1183 t = wTextGetText(text);
1184 /* if !t, the user has canceled editing */
1185 if (t) {
1186 if (entry->text)
1187 free(entry->text);
1188 entry->text = wstrdup(t);
1190 menu->flags.realized = 0;
1192 wTextDestroy(text);
1194 XUngrabKeyboard(dpy, CurrentTime);
1196 if (t && menu->on_edit)
1197 (*menu->on_edit)(menu, entry);
1199 menu->flags.editing = 0;
1201 if (!menu->flags.realized)
1202 wMenuRealize(menu);
1206 static void
1207 selectEntry(WMenu *menu, int entry_no)
1209 WMenuEntry *entry;
1210 WMenu *submenu;
1211 int old_entry;
1213 if (menu->entries==NULL)
1214 return;
1216 if (entry_no >= menu->entry_no)
1217 return;
1219 old_entry = menu->selected_entry;
1220 menu->selected_entry = entry_no;
1222 if (old_entry!=entry_no) {
1224 /* unselect previous entry */
1225 if (old_entry>=0) {
1226 paintEntry(menu, old_entry, False);
1227 entry = menu->entries[old_entry];
1229 /* unmap cascade */
1230 if (entry->cascade>=0 && menu->cascades) {
1231 if (!menu->cascades[entry->cascade]->flags.buttoned) {
1232 wMenuUnmap(menu->cascades[entry->cascade]);
1237 if (entry_no<0) {
1238 menu->selected_entry = -1;
1239 return;
1241 entry = menu->entries[entry_no];
1243 if (entry->cascade>=0 && menu->cascades && entry->flags.enabled) {
1244 /* Callback for when the submenu is opened.
1246 submenu = menu->cascades[entry->cascade];
1247 if (submenu && submenu->flags.brother)
1248 submenu = submenu->brother;
1250 if (entry->callback) {
1251 /* Only call the callback if the submenu is not yet mapped.
1253 if (menu->flags.brother) {
1254 if (!submenu || !submenu->flags.mapped)
1255 (*entry->callback)(menu->brother, entry);
1256 } else {
1257 if (!submenu || !submenu->flags.buttoned)
1258 (*entry->callback)(menu, entry);
1262 /* the submenu menu might have changed */
1263 submenu = menu->cascades[entry->cascade];
1265 /* map cascade */
1266 if (!submenu->flags.mapped) {
1267 int x, y;
1269 if (!submenu->flags.realized)
1270 wMenuRealize(submenu);
1271 if (wPreferences.wrap_menus) {
1272 if (menu->flags.open_to_left)
1273 submenu->flags.open_to_left = 1;
1275 if (submenu->flags.open_to_left) {
1276 x = menu->frame_x - MENUW(submenu);
1277 if (x<0) {
1278 x = 0;
1279 submenu->flags.open_to_left = 0;
1281 } else {
1282 x = menu->frame_x + MENUW(menu);
1284 if (x + MENUW(submenu)
1285 >= menu->frame->screen_ptr->scr_width) {
1287 x = menu->frame_x - MENUW(submenu);
1288 submenu->flags.open_to_left = 1;
1291 } else {
1292 x = menu->frame_x + MENUW(menu);
1295 if (wPreferences.align_menus) {
1296 y = menu->frame_y;
1297 } else {
1298 y = menu->frame_y + menu->entry_height*entry_no;
1299 if (menu->flags.titled)
1300 y += menu->frame->top_width;
1301 if (menu->cascades[entry->cascade]->flags.titled)
1302 y -= menu->cascades[entry->cascade]->frame->top_width;
1305 wMenuMapAt(menu->cascades[entry->cascade], x, y, False);
1306 menu->cascades[entry->cascade]->parent = menu;
1307 } else {
1308 return;
1311 paintEntry(menu, entry_no, True);
1316 static WMenu*
1317 findMenu(WScreen *scr, int *x_ret, int *y_ret)
1319 WMenu *menu;
1320 WObjDescriptor *desc;
1321 Window root_ret, win, junk_win;
1322 int x, y, wx, wy;
1323 unsigned int mask;
1325 XQueryPointer(dpy, scr->root_win, &root_ret, &win, &x, &y, &wx, &wy,
1326 &mask);
1328 if (win==None) return NULL;
1330 if (XFindContext(dpy, win, wWinContext, (XPointer *)&desc)==XCNOENT)
1331 return NULL;
1333 if (desc->parent_type == WCLASS_MENU) {
1334 menu = (WMenu*)desc->parent;
1335 XTranslateCoordinates(dpy, root_ret, menu->menu->window, wx, wy,
1336 x_ret, y_ret, &junk_win);
1337 return menu;
1339 return NULL;
1345 static void
1346 closeCascade(WMenu *menu)
1348 WMenu *parent=menu->parent;
1350 if (menu->flags.brother
1351 || (!menu->flags.buttoned
1352 && (!menu->flags.app_menu||menu->parent!=NULL))) {
1354 selectEntry(menu, -1);
1355 XSync(dpy, 0);
1356 #if (MENU_BLINK_DELAY > 2)
1357 wusleep(MENU_BLINK_DELAY/2);
1358 #endif
1359 wMenuUnmap(menu);
1360 while (parent!=NULL
1361 && (parent->parent!=NULL || !parent->flags.app_menu
1362 || parent->flags.brother)
1363 && !parent->flags.buttoned) {
1364 selectEntry(parent, -1);
1365 wMenuUnmap(parent);
1366 parent = parent->parent;
1368 if (parent)
1369 selectEntry(parent, -1);
1374 static void
1375 closeBrotherCascadesOf(WMenu *menu)
1377 WMenu *tmp;
1378 int i;
1380 for (i=0; i<menu->cascade_no; i++) {
1381 if (menu->cascades[i]->flags.brother) {
1382 tmp = menu->cascades[i];
1383 } else {
1384 tmp = menu->cascades[i]->brother;
1386 if (tmp->flags.mapped) {
1387 selectEntry(tmp->parent, -1);
1388 closeBrotherCascadesOf(tmp);
1389 break;
1395 #define getEntryAt(menu, x, y) ((y)<0 ? -1 : (y)/(menu->entry_height))
1398 static WMenu*
1399 parentMenu(WMenu *menu)
1401 WMenu *parent;
1402 WMenuEntry *entry;
1404 if (menu->flags.buttoned)
1405 return menu;
1407 while (menu->parent && menu->parent->flags.mapped) {
1408 parent = menu->parent;
1409 if (parent->selected_entry < 0)
1410 break;
1411 entry = parent->entries[parent->selected_entry];
1412 if (!entry->flags.enabled || entry->cascade<0 || !parent->cascades ||
1413 parent->cascades[entry->cascade] != menu)
1414 break;
1415 menu = parent;
1416 if (menu->flags.buttoned)
1417 break;
1420 return menu;
1426 * Will raise the passed menu, if submenu = 0
1427 * If submenu > 0 will also raise all mapped submenus
1428 * until the first buttoned one
1429 * If submenu < 0 will also raise all mapped parent menus
1430 * until the first buttoned one
1433 static void
1434 raiseMenus(WMenu *menu, int submenus)
1436 WMenu *submenu;
1437 int i;
1439 if(!menu) return;
1441 wRaiseFrame(menu->frame->core);
1443 if (submenus>0 && menu->selected_entry>=0) {
1444 i = menu->entries[menu->selected_entry]->cascade;
1445 if (i>=0 && menu->cascades) {
1446 submenu = menu->cascades[i];
1447 if (submenu->flags.mapped && !submenu->flags.buttoned)
1448 raiseMenus(submenu, submenus);
1451 if (submenus<0 && !menu->flags.buttoned &&
1452 menu->parent && menu->parent->flags.mapped)
1453 raiseMenus(menu->parent, submenus);
1457 WMenu*
1458 wMenuUnderPointer(WScreen *screen)
1460 WObjDescriptor *desc;
1461 Window root_ret, win;
1462 int dummy;
1463 unsigned int mask;
1465 XQueryPointer(dpy, screen->root_win, &root_ret, &win, &dummy, &dummy,
1466 &dummy, &dummy, &mask);
1468 if (win==None) return NULL;
1470 if (XFindContext(dpy, win, wWinContext, (XPointer *)&desc)==XCNOENT)
1471 return NULL;
1473 if (desc->parent_type == WCLASS_MENU)
1474 return (WMenu *)desc->parent;
1475 return NULL;
1480 #define MIN(a,b) (((a) > (b)) ? (b) : (a))
1483 static void
1484 getPointerPosition(WScreen *scr, int *x, int *y)
1486 Window root_ret, win;
1487 int wx, wy;
1488 unsigned int mask;
1490 XQueryPointer(dpy, scr->root_win, &root_ret, &win, x, y, &wx, &wy, &mask);
1494 static void
1495 getScrollAmount(WMenu *menu, int *hamount, int *vamount)
1497 WScreen *scr = menu->menu->screen_ptr;
1498 int menuX1 = menu->frame_x;
1499 int menuY1 = menu->frame_y;
1500 int menuX2 = menu->frame_x + MENUW(menu);
1501 int menuY2 = menu->frame_y + MENUH(menu);
1502 int screenW = scr->scr_width;
1503 int screenH = scr->scr_height;
1504 int xroot, yroot;
1506 *hamount = 0;
1507 *vamount = 0;
1509 getPointerPosition(scr, &xroot, &yroot);
1512 if (xroot <= 1 && menuX1 < 0) {
1513 /* scroll to the right */
1514 *hamount = MIN(MENU_SCROLL_STEP, abs(menuX1));
1516 } else if (xroot >= screenW-2 && menuX2 > screenW-1) {
1517 /* scroll to the left */
1518 *hamount = MIN(MENU_SCROLL_STEP, abs(menuX2-screenW-1));
1520 if (*hamount==0)
1521 *hamount = 1;
1523 *hamount = -*hamount;
1526 if (yroot <= 1 && menuY1 < 0) {
1527 /* scroll down */
1528 *vamount = MIN(MENU_SCROLL_STEP, abs(menuY1));
1530 } else if (yroot >= screenH-2 && menuY2 > screenH-1) {
1531 /* scroll up */
1532 *vamount = MIN(MENU_SCROLL_STEP, abs(menuY2-screenH-1));
1534 *vamount = -*vamount;
1539 static void
1540 dragScrollMenuCallback(void *data)
1542 WMenu *menu = (WMenu*)data;
1543 WScreen *scr = menu->menu->screen_ptr;
1544 WMenu *parent = parentMenu(menu);
1545 int hamount, vamount;
1546 int x, y;
1547 int newSelectedEntry;
1549 getScrollAmount(menu, &hamount, &vamount);
1552 if (hamount != 0 || vamount != 0) {
1553 wMenuMove(parent, parent->frame_x + hamount,
1554 parent->frame_y + vamount, True);
1555 if (findMenu(scr, &x, &y)) {
1556 newSelectedEntry = getEntryAt(menu, x, y);
1557 selectEntry(menu, newSelectedEntry);
1558 } else {
1559 /* Pointer fell outside of menu. If the selected entry is
1560 * not a submenu, unselect it */
1561 if (menu->selected_entry >= 0
1562 && menu->entries[menu->selected_entry]->cascade<0)
1563 selectEntry(menu, -1);
1564 newSelectedEntry = 0;
1567 /* paranoid check */
1568 if (newSelectedEntry >= 0) {
1569 /* keep scrolling */
1570 menu->timer = WMAddTimerHandler(MENU_SCROLL_DELAY,
1571 dragScrollMenuCallback, menu);
1572 } else {
1573 menu->timer = NULL;
1575 } else {
1576 /* don't need to scroll anymore */
1577 menu->timer = NULL;
1578 if (findMenu(scr, &x, &y)) {
1579 newSelectedEntry = getEntryAt(menu, x, y);
1580 selectEntry(menu, newSelectedEntry);
1586 static void
1587 scrollMenuCallback(void *data)
1589 WMenu *menu = (WMenu*)data;
1590 WMenu *parent = parentMenu(menu);
1591 int hamount = 0; /* amount to scroll */
1592 int vamount = 0;
1594 getScrollAmount(menu, &hamount, &vamount);
1596 if (hamount != 0 || vamount != 0) {
1597 wMenuMove(parent, parent->frame_x + hamount,
1598 parent->frame_y + vamount, True);
1600 /* keep scrolling */
1601 menu->timer = WMAddTimerHandler(MENU_SCROLL_DELAY,
1602 scrollMenuCallback, menu);
1603 } else {
1604 /* don't need to scroll anymore */
1605 menu->timer = NULL;
1611 #define MENU_SCROLL_BORDER 5
1613 static int
1614 isPointNearBoder(WMenu *menu, int x, int y)
1616 int menuX1 = menu->frame_x;
1617 int menuY1 = menu->frame_y;
1618 int menuX2 = menu->frame_x + MENUW(menu);
1619 int menuY2 = menu->frame_y + MENUH(menu);
1620 int scrXe = menu->menu->screen_ptr->scr_width-1;
1621 int scrYe = menu->menu->screen_ptr->scr_height-1;
1622 int flag = 0;
1624 if (x >= menuX1 && x <= menuX2 && (y < MENU_SCROLL_BORDER
1625 || y > scrYe-MENU_SCROLL_BORDER))
1626 flag = 1;
1627 else if (y >= menuY1 && y <= menuY2 && (x < MENU_SCROLL_BORDER
1628 || x > scrXe-MENU_SCROLL_BORDER))
1629 flag = 1;
1631 return flag;
1635 void
1636 wMenuScroll(WMenu *menu, XEvent *event)
1638 WMenu *smenu;
1639 WMenu *omenu = parentMenu(menu);
1640 WScreen *scr = menu->frame->screen_ptr;
1641 int done = 0;
1642 int jump_back = 0;
1643 int old_frame_x = omenu->frame_x;
1644 int old_frame_y = omenu->frame_y;
1645 XEvent ev;
1647 #ifdef DEBUG
1648 puts("Entering menu Scroll");
1649 #endif
1651 if ((/*omenu->flags.buttoned &&*/ !wPreferences.wrap_menus)
1652 || omenu->flags.app_menu) {
1653 jump_back = 1;
1656 if (!wPreferences.wrap_menus)
1657 raiseMenus(omenu, True);
1658 else
1659 raiseMenus(menu, False);
1661 if (!menu->timer)
1662 scrollMenuCallback(menu);
1664 while(!done) {
1665 int x, y, on_border, on_x_edge, on_y_edge, on_title;
1667 WMNextEvent(dpy, &ev);
1668 switch (ev.type) {
1669 case EnterNotify:
1670 WMHandleEvent(&ev);
1671 case MotionNotify:
1672 x = (ev.type==MotionNotify) ? ev.xmotion.x_root : ev.xcrossing.x_root;
1673 y = (ev.type==MotionNotify) ? ev.xmotion.y_root : ev.xcrossing.y_root;
1675 /* on_border is != 0 if the pointer is between the menu
1676 * and the screen border and is close enough to the border */
1677 on_border = isPointNearBoder(menu, x, y);
1679 smenu = wMenuUnderPointer(scr);
1681 if ((smenu==NULL && !on_border) || (smenu && parentMenu(smenu)!=omenu)) {
1682 done = 1;
1683 break;
1686 on_x_edge = x <= 1 || x >= scr->scr_width - 2;
1687 on_y_edge = y <= 1 || y >= scr->scr_height - 2;
1688 on_border = on_x_edge || on_y_edge;
1690 if (!on_border && !jump_back) {
1691 done = 1;
1692 break;
1695 if (menu->timer && (smenu!=menu || (!on_y_edge && !on_x_edge))) {
1696 WMDeleteTimerHandler(menu->timer);
1697 menu->timer = NULL;
1700 if (smenu != NULL)
1701 menu = smenu;
1703 if (!menu->timer)
1704 scrollMenuCallback(menu);
1705 break;
1706 case ButtonPress:
1707 /* True if we push on title, or drag the omenu to other position */
1708 on_title = ev.xbutton.x_root >= omenu->frame_x &&
1709 ev.xbutton.x_root <= omenu->frame_x + MENUW(omenu) &&
1710 ev.xbutton.y_root >= omenu->frame_y &&
1711 ev.xbutton.y_root <= omenu->frame_y + omenu->frame->top_width;
1712 WMHandleEvent(&ev);
1713 smenu = wMenuUnderPointer(scr);
1714 if (smenu == NULL || (smenu && smenu->flags.buttoned && smenu != omenu))
1715 done = 1;
1716 else if (smenu==omenu && on_title) {
1717 jump_back = 0;
1718 done = 1;
1720 break;
1721 case KeyPress:
1722 done = 1;
1723 default:
1724 WMHandleEvent(&ev);
1725 break;
1729 if (menu->timer) {
1730 WMDeleteTimerHandler(menu->timer);
1731 menu->timer = NULL;
1734 if (jump_back)
1735 wMenuMove(omenu, old_frame_x, old_frame_y, True);
1737 #ifdef DEBUG
1738 puts("Leaving menu Scroll");
1739 #endif
1744 static void
1745 menuExpose(WObjDescriptor *desc, XEvent *event)
1747 wMenuPaint(desc->parent);
1750 typedef struct {
1751 int *delayed_select;
1752 WMenu *menu;
1753 WMagicNumber magic;
1754 } delay_data;
1757 static void
1758 delaySelection(void *data)
1760 delay_data *d = (delay_data*)data;
1761 int x, y, entry_no;
1763 d->magic = NULL;
1764 *(d->delayed_select) = 0;
1766 if (findMenu(d->menu->menu->screen_ptr, &x, &y)) {
1767 entry_no = getEntryAt(d->menu, x, y);
1768 selectEntry(d->menu, entry_no);
1773 static void
1774 menuMouseDown(WObjDescriptor *desc, XEvent *event)
1776 XButtonEvent *bev = &event->xbutton;
1777 WMenu *menu = desc->parent;
1778 WMenu *smenu;
1779 WScreen *scr=menu->frame->screen_ptr;
1780 WMenuEntry *entry=NULL;
1781 XEvent ev;
1782 int close_on_exit=0;
1783 int done=0;
1784 int delayed_select = 0;
1785 int entry_no;
1786 int x, y;
1787 int old_frame_x = 0;
1788 int old_frame_y = 0;
1789 delay_data d_data = {NULL, NULL, NULL};
1791 if (!wPreferences.wrap_menus) {
1792 smenu = parentMenu(menu);
1793 old_frame_x = smenu->frame_x;
1794 old_frame_y = smenu->frame_y;
1796 else if (event->xbutton.window == menu->frame->core->window) {
1797 /* This is true if the menu was launched with right click on root window */
1798 delayed_select = 1;
1799 d_data.delayed_select = &delayed_select;
1800 d_data.menu = menu;
1801 d_data.magic = WMAddTimerHandler(wPreferences.dblclick_time,
1802 delaySelection, &d_data);
1805 wRaiseFrame(menu->frame->core);
1807 close_on_exit = (bev->send_event || menu->flags.brother);
1809 smenu = findMenu(scr, &x, &y);
1810 if (!smenu) {
1811 x = -1;
1812 y = -1;
1813 } else {
1814 menu = smenu;
1817 if (menu->flags.editing) {
1818 return;
1820 entry_no = getEntryAt(menu, x, y);
1821 if (entry_no>=0) {
1822 entry = menu->entries[entry_no];
1824 if (!close_on_exit && (bev->state & ControlMask) && smenu
1825 && entry->flags.editable) {
1826 editEntry(smenu, entry);
1827 return;
1828 } else if (bev->state & ControlMask) {
1829 return;
1832 if (entry->flags.enabled && entry->cascade>=0 && menu->cascades) {
1833 WMenu *submenu = menu->cascades[entry->cascade];
1834 /* map cascade */
1835 if (submenu->flags.mapped && !submenu->flags.buttoned &&
1836 menu->selected_entry!=entry_no) {
1837 wMenuUnmap(submenu);
1839 if (!submenu->flags.mapped && !delayed_select) {
1840 selectEntry(menu, entry_no);
1841 } else if (!submenu->flags.buttoned) {
1842 selectEntry(menu, -1);
1845 } else if (!delayed_select) {
1846 selectEntry(menu, entry_no);
1849 if (!wPreferences.wrap_menus && !wPreferences.scrollable_menus) {
1850 if (!menu->timer)
1851 dragScrollMenuCallback(menu);
1855 while (!done) {
1856 int x, y;
1857 WMMaskEvent(dpy, ExposureMask|ButtonMotionMask|ButtonReleaseMask
1858 |ButtonPressMask, &ev);
1859 switch (ev.type) {
1860 case MotionNotify:
1861 smenu = findMenu(scr, &x, &y);
1862 if (smenu == NULL) {
1863 if (menu==NULL
1864 || (menu->selected_entry>=0
1865 && menu->entries[menu->selected_entry]->cascade>=0))
1866 break;
1867 selectEntry(menu, -1);
1868 menu = smenu;
1869 break;
1870 } else if (menu && menu!=smenu
1871 && (menu->selected_entry<0
1872 || menu->entries[menu->selected_entry]->cascade<0)) {
1873 selectEntry(menu, -1);
1875 if (menu!=smenu) {
1876 /* pointer crossed menus */
1877 if (menu && menu->timer) {
1878 WMDeleteTimerHandler(menu->timer);
1879 menu->timer = NULL;
1881 if (smenu)
1882 dragScrollMenuCallback(smenu);
1884 menu = smenu;
1885 if (!menu->timer)
1886 dragScrollMenuCallback(menu);
1888 if (!delayed_select) {
1889 entry_no = getEntryAt(menu, x, y);
1890 if (entry_no>=0) {
1891 entry = menu->entries[entry_no];
1892 if (entry->flags.enabled && entry->cascade>=0 &&
1893 menu->cascades) {
1894 WMenu *submenu = menu->cascades[entry->cascade];
1895 if (submenu->flags.mapped && !submenu->flags.buttoned
1896 && menu->selected_entry!=entry_no) {
1897 wMenuUnmap(submenu);
1901 selectEntry(menu, entry_no);
1903 break;
1905 case ButtonPress:
1906 break;
1908 case ButtonRelease:
1909 if (ev.xbutton.button == event->xbutton.button)
1910 done=1;
1911 break;
1913 case Expose:
1914 WMHandleEvent(&ev);
1915 break;
1919 if (menu && menu->timer) {
1920 WMDeleteTimerHandler(menu->timer);
1921 menu->timer = NULL;
1923 if (d_data.magic!=NULL)
1924 WMDeleteTimerHandler(d_data.magic);
1926 if (menu && menu->selected_entry>=0) {
1927 entry = menu->entries[menu->selected_entry];
1928 if (entry->callback!=NULL && entry->flags.enabled
1929 && entry->cascade < 0) {
1930 /* blink and erase menu selection */
1931 #if (MENU_BLINK_DELAY > 0)
1932 int sel = menu->selected_entry;
1933 int i;
1935 for (i=0; i<MENU_BLINK_COUNT; i++) {
1936 paintEntry(menu, sel, False);
1937 XSync(dpy, 0);
1938 wusleep(MENU_BLINK_DELAY);
1939 paintEntry(menu, sel, True);
1940 XSync(dpy, 0);
1941 wusleep(MENU_BLINK_DELAY);
1943 #endif
1944 /* unmap the menu, it's parents and call the callback */
1945 if (!menu->flags.buttoned &&
1946 (!menu->flags.app_menu||menu->parent!=NULL)) {
1947 closeCascade(menu);
1948 } else {
1949 selectEntry(menu, -1);
1951 (*entry->callback)(menu, entry);
1953 /* If the user double clicks an entry, the entry will
1954 * be executed twice, which is not good for things like
1955 * the root menu. So, ignore any clicks that were generated
1956 * while the entry was being executed */
1957 while (XCheckTypedWindowEvent(dpy, menu->menu->window,
1958 ButtonPress, &ev));
1959 } else if (entry->callback!=NULL && entry->cascade<0) {
1960 selectEntry(menu, -1);
1961 } else {
1962 if (entry->cascade>=0 && menu->cascades
1963 && menu->cascades[entry->cascade]->flags.brother) {
1964 selectEntry(menu, -1);
1969 if (((WMenu*)desc->parent)->flags.brother || close_on_exit
1970 || !smenu)
1971 closeCascade(desc->parent);
1973 /* close the cascade windows that should not remain opened */
1974 closeBrotherCascadesOf(desc->parent);
1976 if (!wPreferences.wrap_menus)
1977 wMenuMove(parentMenu(desc->parent), old_frame_x, old_frame_y, True);
1981 void
1982 wMenuMove(WMenu *menu, int x, int y, int submenus)
1984 WMenu *submenu;
1985 int i;
1987 if (!menu) return;
1989 menu->frame_x = x;
1990 menu->frame_y = y;
1991 XMoveWindow(dpy, menu->frame->core->window, x, y);
1993 if (submenus>0 && menu->selected_entry>=0) {
1994 i = menu->entries[menu->selected_entry]->cascade;
1996 if (i>=0 && menu->cascades) {
1997 submenu = menu->cascades[i];
1998 if (submenu->flags.mapped && !submenu->flags.buttoned)
1999 if (wPreferences.align_menus) {
2000 wMenuMove(submenu, x + MENUW(menu), y, submenus);
2001 } else {
2002 wMenuMove(submenu, x+ MENUW(menu),
2003 y + submenu->entry_height*menu->selected_entry,
2004 submenus);
2008 if (submenus<0 && menu->parent!=NULL && menu->parent->flags.mapped &&
2009 !menu->parent->flags.buttoned) {
2010 if (wPreferences.align_menus) {
2011 wMenuMove(menu->parent, x - MENUW(menu->parent), y, submenus);
2012 } else {
2013 wMenuMove(menu->parent, x - MENUW(menu->parent), menu->frame_y
2014 - menu->parent->entry_height*menu->parent->selected_entry,
2015 submenus);
2021 static void
2022 changeMenuLevels(WMenu *menu, int lower)
2024 int i;
2026 if (!lower) {
2027 ChangeStackingLevel(menu->frame->core, (!menu->parent ? WMMainMenuWindowLevel
2028 : WMSubmenuWindowLevel));
2029 wRaiseFrame(menu->frame->core);
2030 menu->flags.lowered = 0;
2031 } else {
2032 ChangeStackingLevel(menu->frame->core, WMNormalWindowLevel);
2033 wLowerFrame(menu->frame->core);
2034 menu->flags.lowered = 1;
2036 for (i=0; i<menu->cascade_no; i++) {
2037 if (menu->cascades[i]
2038 && !menu->cascades[i]->flags.buttoned
2039 && menu->cascades[i]->flags.lowered!=lower) {
2040 changeMenuLevels(menu->cascades[i], lower);
2047 static void
2048 menuTitleDoubleClick(WCoreWindow *sender, void *data, XEvent *event)
2050 WMenu *menu = data;
2051 int lower;
2053 if (event->xbutton.state & MOD_MASK) {
2054 if (menu->flags.lowered) {
2055 lower = 0;
2056 } else {
2057 lower = 1;
2059 changeMenuLevels(menu, lower);
2064 static void
2065 menuTitleMouseDown(WCoreWindow *sender, void *data, XEvent *event)
2067 WMenu *menu = data;
2068 WMenu *tmp;
2069 XEvent ev;
2070 int x=menu->frame_x, y=menu->frame_y;
2071 int dx=event->xbutton.x_root, dy=event->xbutton.y_root;
2072 int i, lower;
2074 #ifdef DEBUG
2075 printf("Moving menu\n");
2076 #endif
2078 /* can't touch the menu copy */
2079 if (menu->flags.brother)
2080 return;
2082 if (event->xbutton.button != Button1 && event->xbutton.button != Button2)
2083 return;
2084 if (XGrabPointer(dpy, menu->frame->titlebar->window, False,
2085 ButtonMotionMask|ButtonReleaseMask|ButtonPressMask,
2086 GrabModeAsync, GrabModeAsync, None,
2087 wCursor[WCUR_MOVE], CurrentTime)!=GrabSuccess) {
2088 #ifdef DEBUG0
2089 wwarning("pointer grab failed for menu move\n");
2090 #endif
2091 return;
2093 if (event->xbutton.state & MOD_MASK) {
2094 wLowerFrame(menu->frame->core);
2095 lower = 1;
2096 } else {
2097 wRaiseFrame(menu->frame->core);
2098 lower = 0;
2100 tmp = menu;
2102 /* lower/raise all submenus */
2103 while (1) {
2104 if (tmp->selected_entry>=0 && tmp->cascades) {
2105 tmp = tmp->cascades[tmp->entries[tmp->selected_entry]->cascade];
2106 if (!tmp || !tmp->flags.mapped)
2107 break;
2108 if (lower)
2109 wLowerFrame(tmp->frame->core);
2110 else
2111 wRaiseFrame(tmp->frame->core);
2112 } else {
2113 break;
2116 /* tear off the menu if it's a root menu or a cascade
2117 application menu */
2118 if (!menu->flags.buttoned && !menu->flags.brother
2119 && (!menu->flags.app_menu||menu->parent!=NULL)) {
2120 menu->flags.buttoned=1;
2121 wFrameWindowShowButton(menu->frame, WFF_RIGHT_BUTTON);
2122 if (menu->parent) {
2123 /* turn off selected menu entry in parent menu */
2124 selectEntry(menu->parent, -1);
2126 /* make parent map the copy in place of the original */
2127 for (i=0; i<menu->parent->cascade_no; i++) {
2128 if (menu->parent->cascades[i] == menu) {
2129 menu->parent->cascades[i] = menu->brother;
2130 break;
2136 while(1) {
2137 WMMaskEvent(dpy, ButtonMotionMask|ButtonReleaseMask|ButtonPressMask
2138 |ExposureMask, &ev);
2139 switch (ev.type) {
2140 case MotionNotify:
2141 x += ev.xmotion.x_root - dx;
2142 y += ev.xmotion.y_root - dy;
2143 dx = ev.xmotion.x_root;
2144 dy = ev.xmotion.y_root;
2145 wMenuMove(menu, x, y, True);
2146 break;
2148 case ButtonPress:
2149 break;
2151 case ButtonRelease:
2152 if (ev.xbutton.button != event->xbutton.button)
2153 break;
2154 #ifdef DEBUG
2155 printf("End menu move\n");
2156 #endif
2157 XUngrabPointer(dpy, CurrentTime);
2158 return;
2160 default:
2161 WMHandleEvent(&ev);
2167 *----------------------------------------------------------------------
2168 * menuCloseClick--
2169 * Handles mouse click on the close button of menus. The menu is
2170 * closed when the button is clicked.
2172 * Side effects:
2173 * The closed menu is reinserted at it's parent menus
2174 * cascade list.
2175 *----------------------------------------------------------------------
2177 static void
2178 menuCloseClick(WCoreWindow *sender, void *data, XEvent *event)
2180 WMenu *menu = (WMenu*)data;
2181 WMenu *parent = menu->parent;
2182 int i;
2184 if (parent) {
2185 for (i=0; i<parent->cascade_no; i++) {
2186 /* find the entry that points to the copy */
2187 if (parent->cascades[i] == menu->brother) {
2188 /* make it point to the original */
2189 parent->cascades[i] = menu;
2190 menu->parent = parent;
2191 break;
2195 wMenuUnmap(menu);
2199 void
2200 wMenuSaveState(WScreen *scr)
2202 proplist_t menus, key, value;
2203 int save_menus = 0;
2204 char buffer[256];
2206 menus = PLMakeDictionaryFromEntries(NULL, NULL, NULL);
2208 if (scr->root_menu && scr->root_menu->flags.buttoned) {
2209 sprintf(buffer, "%i,%i", scr->root_menu->frame_x,
2210 scr->root_menu->frame_y);
2211 key = PLMakeString("RootMenu");
2212 value = PLMakeString(buffer);
2213 PLInsertDictionaryEntry(menus, key, value);
2214 PLRelease(key);
2215 PLRelease(value);
2216 save_menus = 1;
2219 if (scr->switch_menu && scr->switch_menu->flags.buttoned) {
2220 sprintf(buffer, "%i,%i", scr->switch_menu->frame_x,
2221 scr->switch_menu->frame_y);
2222 key = PLMakeString("SwitchMenu");
2223 value = PLMakeString(buffer);
2224 PLInsertDictionaryEntry(menus, key, value);
2225 PLRelease(key);
2226 PLRelease(value);
2227 save_menus = 1;
2230 if (scr->workspace_menu && scr->workspace_menu->flags.buttoned) {
2231 sprintf(buffer, "%i,%i", scr->workspace_menu->frame_x,
2232 scr->workspace_menu->frame_y);
2233 key = PLMakeString("WorkspaceMenu");
2234 value = PLMakeString(buffer);
2235 PLInsertDictionaryEntry(menus, key, value);
2236 PLRelease(key);
2237 PLRelease(value);
2238 save_menus = 1;
2241 if (save_menus) {
2242 key = PLMakeString("Menus");
2243 PLInsertDictionaryEntry(scr->session_state, key, menus);
2244 PLRelease(key);
2246 PLRelease(menus);
2250 #define COMPLAIN(key) wwarning(_("bad value in menus state info:%s"), key)
2253 static int
2254 restoreMenu(WScreen *scr, proplist_t menu, int which)
2256 int i, x, y;
2257 WMenu *pmenu = NULL;
2259 if (!menu)
2260 return False;
2262 if (!PLIsString(menu)) {
2263 COMPLAIN("Position");
2264 return False;
2267 if (sscanf(PLGetString(menu), "%i,%i", &x, &y)!=2)
2268 COMPLAIN("Position");
2270 if (which & WSS_ROOTMENU) {
2271 OpenRootMenu(scr, x, y, False);
2272 pmenu = scr->root_menu;
2274 else if (which & WSS_SWITCHMENU) {
2275 OpenSwitchMenu(scr, x, y, False);
2276 pmenu = scr->switch_menu;
2278 else if (which & WSS_WSMENU) {
2279 OpenWorkspaceMenu(scr, x, y);
2280 pmenu = scr->workspace_menu;
2281 if (pmenu->parent) {
2282 /* make parent map the copy in place of the original */
2283 for (i=0; i<pmenu->parent->cascade_no; i++) {
2284 if (pmenu->parent->cascades[i] == pmenu) {
2285 pmenu->parent->cascades[i] = pmenu->brother;
2286 break;
2292 if (pmenu) {
2293 int width = MENUW(pmenu);
2294 int height = MENUH(pmenu);
2296 x = (x < -width) ? 0 : x;
2297 x = (x > scr->scr_width) ? scr->scr_width - width : x;
2298 y = (y < 0) ? 0 : y;
2299 y = (y > scr->scr_height) ? scr->scr_height - height : y;
2300 wMenuMove(pmenu, x, y, True);
2301 pmenu->flags.buttoned = 1;
2302 wFrameWindowShowButton(pmenu->frame, WFF_RIGHT_BUTTON);
2303 return True;
2305 return False;
2309 void
2310 wMenuRestoreState(WScreen *scr)
2312 proplist_t menus, menu, key, rkey, skey, wkey;
2314 key = PLMakeString("Menus");
2315 menus = PLGetDictionaryEntry(scr->session_state, key);
2316 PLRelease(key);
2318 if (!menus)
2319 return;
2321 /* restore menus */
2323 rkey = PLMakeString("RootMenu");
2324 skey = PLMakeString("SwitchMenu");
2325 wkey = PLMakeString("WorkspaceMenu");
2326 menu = PLGetDictionaryEntry(menus, rkey);
2327 restoreMenu(scr, menu, WSS_ROOTMENU);
2328 menu = PLGetDictionaryEntry(menus, skey);
2329 restoreMenu(scr, menu, WSS_SWITCHMENU);
2330 menu = PLGetDictionaryEntry(menus, wkey);
2331 restoreMenu(scr, menu, WSS_WSMENU);
2333 PLRelease(rkey);
2334 PLRelease(skey);
2335 PLRelease(wkey);
2339 void
2340 OpenWorkspaceMenu(WScreen *scr, int x, int y)
2342 WMenu *menu, *parent;
2343 WMenuEntry *entry;
2345 if (!scr->root_menu) {
2346 OpenRootMenu(scr, scr->scr_width*2, 0, False);
2347 wMenuUnmap(scr->root_menu);
2350 menu = scr->workspace_menu;
2351 if (menu) {
2352 if (menu->flags.mapped) {
2353 if (!menu->flags.buttoned) {
2354 wMenuUnmap(menu);
2355 parent = menu->parent;
2356 if (parent && parent->selected_entry >= 0) {
2357 entry = parent->entries[parent->selected_entry];
2358 if (parent->cascades[entry->cascade] == menu) {
2359 selectEntry(parent, -1);
2360 wMenuMapAt(menu, x, y, False);
2363 } else {
2364 wRaiseFrame(menu->frame->core);
2365 wMenuMapCopyAt(menu, x, y);
2368 else {
2369 wMenuMapAt(menu, x, y, False);