Change to the linux kernel coding style
[wmaker-crm.git] / src / menu.c
1 /* menu.c- generic menu, used for root menu, application menus etc.
2 *
3 * Window Maker window manager
4 *
5 * Copyright (c) 1997-2003 Alfredo K. Kojima
6 * Copyright (c) 1998-2003 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.
12 *
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.
17 *
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.
22 */
23
24 #include "wconfig.h"
25
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 <stdint.h>
33 #include <unistd.h>
34 #include <ctype.h>
35 #if 0
36 #include <nana.h>
37 #endif
38 #include "WindowMaker.h"
39 #include "wcore.h"
40 #include "framewin.h"
41 #include "menu.h"
42 #include "actions.h"
43 #include "funcs.h"
44 #include "stacking.h"
45 #include "text.h"
46 #include "xinerama.h"
47 #include "workspace.h"
48
49 /****** Global Variables ******/
50
51 extern Cursor wCursor[WCUR_LAST];
52
53 extern XContext wWinContext;
54
55 extern WPreferences wPreferences;
56
57 #define MOD_MASK wPreferences.modifier_mask
58
59 #define MENU_SCROLL_STEP menuScrollParameters[(int)wPreferences.menu_scroll_speed].steps
60 #define MENU_SCROLL_DELAY menuScrollParameters[(int)wPreferences.menu_scroll_speed].delay
61
62 #define MENUW(m) ((m)->frame->core->width+2*FRAME_BORDER_WIDTH)
63 #define MENUH(m) ((m)->frame->core->height+2*FRAME_BORDER_WIDTH)
64
65 /***** Local Stuff ******/
66
67 #define WSS_ROOTMENU (1<<0)
68 #define WSS_SWITCHMENU (1<<1)
69 #define WSS_WSMENU (1<<2)
70
71 static struct {
72 int steps;
73 int delay;
74 } menuScrollParameters[5] = {
75 {
76 MENU_SCROLL_STEPS_UF, MENU_SCROLL_DELAY_UF}, {
77 MENU_SCROLL_STEPS_F, MENU_SCROLL_DELAY_F}, {
78 MENU_SCROLL_STEPS_M, MENU_SCROLL_DELAY_M}, {
79 MENU_SCROLL_STEPS_S, MENU_SCROLL_DELAY_S}, {
80 MENU_SCROLL_STEPS_US, MENU_SCROLL_DELAY_US}};
81
82 static void menuMouseDown(WObjDescriptor * desc, XEvent * event);
83 static void menuExpose(WObjDescriptor * desc, XEvent * event);
84
85 static void menuTitleDoubleClick(WCoreWindow * sender, void *data, XEvent * event);
86 static void menuTitleMouseDown(WCoreWindow * sender, void *data, XEvent * event);
87
88 static void menuCloseClick(WCoreWindow * sender, void *data, XEvent * event);
89
90 static void updateTexture(WMenu * menu);
91
92 #ifndef LITE
93 static int saveMenuRecurs(WMPropList * menus, WScreen * scr, WMenu * menu);
94 static int restoreMenuRecurs(WScreen * scr, WMPropList * menus, WMenu * menu, char *path);
95 #endif /* !LITE */
96
97 static void selectEntry(WMenu * menu, int entry_no);
98 static void closeCascade(WMenu * menu);
99
100 /****** Notification Observers ******/
101
102 static void appearanceObserver(void *self, WMNotification * notif)
103 {
104 WMenu *menu = (WMenu *) self;
105 int flags = (int)(uintptr_t) WMGetNotificationClientData(notif);
106
107 if (!menu->flags.realized)
108 return;
109
110 if (WMGetNotificationName(notif) == WNMenuAppearanceSettingsChanged) {
111 if (flags & WFontSettings) {
112 menu->flags.realized = 0;
113 wMenuRealize(menu);
114 }
115 if (flags & WTextureSettings) {
116 if (!menu->flags.brother)
117 updateTexture(menu);
118 }
119 if (flags & (WTextureSettings | WColorSettings)) {
120 wMenuPaint(menu);
121 }
122 } else if (menu->flags.titled) {
123
124 if (flags & WFontSettings) {
125 menu->flags.realized = 0;
126 wMenuRealize(menu);
127 }
128 if (flags & WTextureSettings) {
129 menu->frame->flags.need_texture_remake = 1;
130 }
131 if (flags & (WColorSettings | WTextureSettings)) {
132 wFrameWindowPaint(menu->frame);
133 }
134 }
135 }
136
137 /************************************/
138
139 /*
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.
146 *
147 * Returns:
148 * The created menu.
149 *----------------------------------------------------------------------
150 */
151 WMenu *wMenuCreate(WScreen * screen, char *title, int main_menu)
152 {
153 WMenu *menu;
154 static int brother = 0;
155 int tmp, flags;
156
157 menu = wmalloc(sizeof(WMenu));
158
159 memset(menu, 0, sizeof(WMenu));
160
161 #ifdef SINGLE_MENULEVEL
162 tmp = WMSubmenuLevel;
163 #else
164 tmp = (main_menu ? WMMainMenuLevel : WMSubmenuLevel);
165 #endif
166
167 flags = WFF_SINGLE_STATE | WFF_BORDER;
168 if (title) {
169 flags |= WFF_TITLEBAR | WFF_RIGHT_BUTTON;
170 menu->flags.titled = 1;
171 }
172 menu->frame =
173 wFrameWindowCreate(screen, tmp, 8, 2, 1, 1, &wPreferences.menu_title_clearance, flags,
174 screen->menu_title_texture, NULL,
175 screen->menu_title_color, &screen->menu_title_font);
176
177 menu->frame->core->descriptor.parent = menu;
178 menu->frame->core->descriptor.parent_type = WCLASS_MENU;
179 menu->frame->core->descriptor.handle_mousedown = menuMouseDown;
180
181 wFrameWindowHideButton(menu->frame, WFF_RIGHT_BUTTON);
182
183 if (title) {
184 menu->frame->title = wstrdup(title);
185 }
186
187 menu->frame->flags.justification = WTJ_LEFT;
188
189 menu->frame->rbutton_image = screen->b_pixmaps[WBUT_CLOSE];
190
191 menu->entry_no = 0;
192 menu->alloced_entries = 0;
193 menu->selected_entry = -1;
194 menu->entries = NULL;
195
196 menu->frame_x = screen->app_menu_x;
197 menu->frame_y = screen->app_menu_y;
198
199 menu->frame->child = menu;
200
201 menu->flags.lowered = 0;
202
203 /* create borders */
204 if (title) {
205 /* setup object descriptors */
206 menu->frame->on_mousedown_titlebar = menuTitleMouseDown;
207 menu->frame->on_dblclick_titlebar = menuTitleDoubleClick;
208 }
209
210 menu->frame->on_click_right = menuCloseClick;
211
212 menu->menu = wCoreCreate(menu->frame->core, 0, menu->frame->top_width, menu->frame->core->width, 10);
213
214 menu->menu->descriptor.parent = menu;
215 menu->menu->descriptor.parent_type = WCLASS_MENU;
216 menu->menu->descriptor.handle_expose = menuExpose;
217 menu->menu->descriptor.handle_mousedown = menuMouseDown;
218
219 menu->menu_texture_data = None;
220
221 XMapWindow(dpy, menu->menu->window);
222
223 XFlush(dpy);
224
225 if (!brother) {
226 brother = 1;
227 menu->brother = wMenuCreate(screen, title, main_menu);
228 brother = 0;
229 menu->brother->flags.brother = 1;
230 menu->brother->brother = menu;
231 }
232 WMAddNotificationObserver(appearanceObserver, menu, WNMenuAppearanceSettingsChanged, menu);
233
234 WMAddNotificationObserver(appearanceObserver, menu, WNMenuTitleAppearanceSettingsChanged, menu);
235
236 return menu;
237 }
238
239 WMenu *wMenuCreateForApp(WScreen * screen, char *title, int main_menu)
240 {
241 WMenu *menu;
242
243 menu = wMenuCreate(screen, title, main_menu);
244 if (!menu)
245 return NULL;
246 menu->flags.app_menu = 1;
247 menu->brother->flags.app_menu = 1;
248
249 return menu;
250 }
251
252 static void insertEntry(WMenu * menu, WMenuEntry * entry, int index)
253 {
254 int i;
255
256 for (i = menu->entry_no - 1; i >= index; i--) {
257 menu->entries[i]->order++;
258 menu->entries[i + 1] = menu->entries[i];
259 }
260 menu->entries[index] = entry;
261 }
262
263 WMenuEntry *wMenuInsertCallback(WMenu * menu, int index, char *text,
264 void (*callback) (WMenu * menu, WMenuEntry * entry), void *clientdata)
265 {
266 WMenuEntry *entry;
267
268 menu->flags.realized = 0;
269 menu->brother->flags.realized = 0;
270
271 /* reallocate array if it's too small */
272 if (menu->entry_no >= menu->alloced_entries) {
273 void *tmp;
274
275 tmp = wrealloc(menu->entries, sizeof(WMenuEntry) * (menu->alloced_entries + 5));
276
277 menu->entries = tmp;
278 menu->alloced_entries += 5;
279
280 menu->brother->entries = tmp;
281 menu->brother->alloced_entries = menu->alloced_entries;
282 }
283 entry = wmalloc(sizeof(WMenuEntry));
284 memset(entry, 0, sizeof(WMenuEntry));
285 entry->flags.enabled = 1;
286 entry->text = wstrdup(text);
287 entry->cascade = -1;
288 entry->clientdata = clientdata;
289 entry->callback = callback;
290 if (index < 0 || index >= menu->entry_no) {
291 entry->order = menu->entry_no;
292 menu->entries[menu->entry_no] = entry;
293 } else {
294 entry->order = index;
295 insertEntry(menu, entry, index);
296 }
297
298 menu->entry_no++;
299 menu->brother->entry_no = menu->entry_no;
300
301 return entry;
302 }
303
304 void wMenuEntrySetCascade(WMenu * menu, WMenuEntry * entry, WMenu * cascade)
305 {
306 WMenu *brother = menu->brother;
307 int i, done;
308
309 assert(menu->flags.brother == 0);
310
311 if (entry->cascade >= 0) {
312 menu->flags.realized = 0;
313 brother->flags.realized = 0;
314 }
315
316 cascade->parent = menu;
317
318 cascade->brother->parent = brother;
319
320 done = 0;
321 for (i = 0; i < menu->cascade_no; i++) {
322 if (menu->cascades[i] == NULL) {
323 menu->cascades[i] = cascade;
324 brother->cascades[i] = cascade->brother;
325 done = 1;
326 entry->cascade = i;
327 break;
328 }
329 }
330 if (!done) {
331 entry->cascade = menu->cascade_no;
332
333 menu->cascades = wrealloc(menu->cascades, sizeof(WMenu) * (menu->cascade_no + 1));
334 menu->cascades[menu->cascade_no++] = cascade;
335
336 brother->cascades = wrealloc(brother->cascades, sizeof(WMenu) * (brother->cascade_no + 1));
337 brother->cascades[brother->cascade_no++] = cascade->brother;
338 }
339
340 if (menu->flags.lowered) {
341
342 cascade->flags.lowered = 1;
343 ChangeStackingLevel(cascade->frame->core, WMNormalLevel);
344
345 cascade->brother->flags.lowered = 1;
346 ChangeStackingLevel(cascade->brother->frame->core, WMNormalLevel);
347 }
348
349 if (!menu->flags.realized)
350 wMenuRealize(menu);
351 }
352
353 void wMenuEntryRemoveCascade(WMenu * menu, WMenuEntry * entry)
354 {
355 assert(menu->flags.brother == 0);
356
357 /* destroy cascade menu */
358 if (entry->cascade >= 0 && menu->cascades && menu->cascades[entry->cascade] != NULL) {
359
360 wMenuDestroy(menu->cascades[entry->cascade], True);
361
362 menu->cascades[entry->cascade] = NULL;
363 menu->brother->cascades[entry->cascade] = NULL;
364
365 entry->cascade = -1;
366 }
367 }
368
369 void wMenuRemoveItem(WMenu * menu, int index)
370 {
371 int i;
372
373 if (menu->flags.brother) {
374 wMenuRemoveItem(menu->brother, index);
375 return;
376 }
377
378 if (index >= menu->entry_no)
379 return;
380
381 /* destroy cascade menu */
382 wMenuEntryRemoveCascade(menu, menu->entries[index]);
383
384 /* destroy unshared data */
385
386 if (menu->entries[index]->text)
387 wfree(menu->entries[index]->text);
388
389 if (menu->entries[index]->rtext)
390 wfree(menu->entries[index]->rtext);
391
392 if (menu->entries[index]->free_cdata && menu->entries[index]->clientdata)
393 (*menu->entries[index]->free_cdata) (menu->entries[index]->clientdata);
394
395 wfree(menu->entries[index]);
396
397 for (i = index; i < menu->entry_no - 1; i++) {
398 menu->entries[i + 1]->order--;
399 menu->entries[i] = menu->entries[i + 1];
400 }
401 menu->entry_no--;
402 menu->brother->entry_no--;
403 }
404
405 static Pixmap renderTexture(WMenu * menu)
406 {
407 RImage *img;
408 Pixmap pix;
409 int i;
410 RColor light;
411 RColor dark;
412 RColor mid;
413 WScreen *scr = menu->menu->screen_ptr;
414 WTexture *texture = scr->menu_item_texture;
415
416 if (wPreferences.menu_style == MS_NORMAL) {
417 img = wTextureRenderImage(texture, menu->menu->width, menu->entry_height, WREL_MENUENTRY);
418 } else {
419 img = wTextureRenderImage(texture, menu->menu->width, menu->menu->height + 1, WREL_MENUENTRY);
420 }
421 if (!img) {
422 wwarning(_("could not render texture: %s"), RMessageForError(RErrorCode));
423
424 return None;
425 }
426
427 if (wPreferences.menu_style == MS_SINGLE_TEXTURE) {
428 light.alpha = 0;
429 light.red = light.green = light.blue = 80;
430
431 dark.alpha = 255;
432 dark.red = dark.green = dark.blue = 0;
433
434 mid.alpha = 0;
435 mid.red = mid.green = mid.blue = 40;
436
437 for (i = 1; i < menu->entry_no; i++) {
438 ROperateLine(img, RSubtractOperation, 0, i * menu->entry_height - 2,
439 menu->menu->width - 1, i * menu->entry_height - 2, &mid);
440
441 RDrawLine(img, 0, i * menu->entry_height - 1,
442 menu->menu->width - 1, i * menu->entry_height - 1, &dark);
443
444 ROperateLine(img, RAddOperation, 0, i * menu->entry_height,
445 menu->menu->width - 1, i * menu->entry_height, &light);
446 }
447 }
448 if (!RConvertImage(scr->rcontext, img, &pix)) {
449 wwarning(_("error rendering image:%s"), RMessageForError(RErrorCode));
450 }
451 RReleaseImage(img);
452
453 return pix;
454 }
455
456 static void updateTexture(WMenu * menu)
457 {
458 WScreen *scr = menu->menu->screen_ptr;
459
460 /* setup background texture */
461 if (scr->menu_item_texture->any.type != WTEX_SOLID) {
462 if (!menu->flags.brother) {
463 FREE_PIXMAP(menu->menu_texture_data);
464
465 menu->menu_texture_data = renderTexture(menu);
466
467 XSetWindowBackgroundPixmap(dpy, menu->menu->window, menu->menu_texture_data);
468 XClearWindow(dpy, menu->menu->window);
469
470 XSetWindowBackgroundPixmap(dpy, menu->brother->menu->window, menu->menu_texture_data);
471 XClearWindow(dpy, menu->brother->menu->window);
472 }
473 } else {
474 XSetWindowBackground(dpy, menu->menu->window, scr->menu_item_texture->any.color.pixel);
475 XClearWindow(dpy, menu->menu->window);
476 }
477 }
478
479 void wMenuRealize(WMenu * menu)
480 {
481 int i;
482 int width, rwidth, mrwidth, mwidth;
483 int theight, twidth, eheight;
484 WScreen *scr = menu->frame->screen_ptr;
485 static int brother_done = 0;
486 int flags;
487
488 if (!brother_done) {
489 brother_done = 1;
490 wMenuRealize(menu->brother);
491 brother_done = 0;
492 }
493
494 flags = WFF_SINGLE_STATE | WFF_BORDER;
495 if (menu->flags.titled)
496 flags |= WFF_TITLEBAR | WFF_RIGHT_BUTTON;
497
498 wFrameWindowUpdateBorders(menu->frame, flags);
499
500 if (menu->flags.titled) {
501 twidth = WMWidthOfString(scr->menu_title_font, menu->frame->title, strlen(menu->frame->title));
502 theight = menu->frame->top_width;
503 twidth += theight + (wPreferences.new_style ? 16 : 8);
504 } else {
505 twidth = 0;
506 theight = 0;
507 }
508 eheight = WMFontHeight(scr->menu_entry_font) + 6 + wPreferences.menu_text_clearance * 2;
509 menu->entry_height = eheight;
510 mrwidth = 0;
511 mwidth = 0;
512 for (i = 0; i < menu->entry_no; i++) {
513 char *text;
514
515 /* search widest text */
516 text = menu->entries[i]->text;
517 width = WMWidthOfString(scr->menu_entry_font, text, strlen(text)) + 10;
518
519 if (menu->entries[i]->flags.indicator) {
520 width += MENU_INDICATOR_SPACE;
521 }
522
523 if (width > mwidth)
524 mwidth = width;
525
526 /* search widest text on right */
527 text = menu->entries[i]->rtext;
528 if (text)
529 rwidth = WMWidthOfString(scr->menu_entry_font, text, strlen(text))
530 + 10;
531 else if (menu->entries[i]->cascade >= 0)
532 rwidth = 16;
533 else
534 rwidth = 4;
535
536 if (rwidth > mrwidth)
537 mrwidth = rwidth;
538 }
539 mwidth += mrwidth;
540
541 if (mwidth < twidth)
542 mwidth = twidth;
543
544 wCoreConfigure(menu->menu, 0, theight, mwidth, menu->entry_no * eheight - 1);
545
546 wFrameWindowResize(menu->frame, mwidth, menu->entry_no * eheight - 1
547 + menu->frame->top_width + menu->frame->bottom_width);
548
549 updateTexture(menu);
550
551 menu->flags.realized = 1;
552
553 if (menu->flags.mapped)
554 wMenuPaint(menu);
555 if (menu->brother->flags.mapped)
556 wMenuPaint(menu->brother);
557 }
558
559 void wMenuDestroy(WMenu * menu, int recurse)
560 {
561 int i;
562
563 WMRemoveNotificationObserver(menu);
564
565 /* remove any pending timers */
566 if (menu->timer)
567 WMDeleteTimerHandler(menu->timer);
568 menu->timer = NULL;
569
570 /* call destroy handler */
571 if (menu->on_destroy)
572 (*menu->on_destroy) (menu);
573
574 /* Destroy items if this menu own them. If this is the "brother" menu,
575 * leave them alone as it is shared by them.
576 */
577 if (!menu->flags.brother) {
578 for (i = 0; i < menu->entry_no; i++) {
579
580 wfree(menu->entries[i]->text);
581
582 if (menu->entries[i]->rtext)
583 wfree(menu->entries[i]->rtext);
584 #ifdef USER_MENU
585
586 if (menu->entries[i]->instances) {
587 WMReleasePropList(menu->entries[i]->instances);
588 }
589 #endif /* USER_MENU */
590
591 if (menu->entries[i]->free_cdata && menu->entries[i]->clientdata) {
592 (*menu->entries[i]->free_cdata) (menu->entries[i]->clientdata);
593 }
594 wfree(menu->entries[i]);
595 }
596
597 if (recurse) {
598 for (i = 0; i < menu->cascade_no; i++) {
599 if (menu->cascades[i]) {
600 if (menu->cascades[i]->flags.brother)
601 wMenuDestroy(menu->cascades[i]->brother, recurse);
602 else
603 wMenuDestroy(menu->cascades[i], recurse);
604 }
605 }
606 }
607
608 if (menu->entries)
609 wfree(menu->entries);
610
611 }
612
613 FREE_PIXMAP(menu->menu_texture_data);
614
615 if (menu->cascades)
616 wfree(menu->cascades);
617
618 wCoreDestroy(menu->menu);
619 wFrameWindowDestroy(menu->frame);
620
621 /* destroy copy of this menu */
622 if (!menu->flags.brother && menu->brother)
623 wMenuDestroy(menu->brother, False);
624
625 wfree(menu);
626 }
627
628 #define F_NORMAL 0
629 #define F_TOP 1
630 #define F_BOTTOM 2
631 #define F_NONE 3
632
633 static void drawFrame(WScreen * scr, Drawable win, int y, int w, int h, int type)
634 {
635 XSegment segs[2];
636 int i;
637
638 i = 0;
639 segs[i].x1 = segs[i].x2 = w - 1;
640 segs[i].y1 = y;
641 segs[i].y2 = y + h - 1;
642 i++;
643 if (type != F_TOP && type != F_NONE) {
644 segs[i].x1 = 1;
645 segs[i].y1 = segs[i].y2 = y + h - 2;
646 segs[i].x2 = w - 1;
647 i++;
648 }
649 XDrawSegments(dpy, win, scr->menu_item_auxtexture->dim_gc, segs, i);
650
651 i = 0;
652 segs[i].x1 = 0;
653 segs[i].y1 = y;
654 segs[i].x2 = 0;
655 segs[i].y2 = y + h - 1;
656 i++;
657 if (type != F_BOTTOM && type != F_NONE) {
658 segs[i].x1 = 0;
659 segs[i].y1 = y;
660 segs[i].x2 = w - 1;
661 segs[i].y2 = y;
662 i++;
663 }
664 XDrawSegments(dpy, win, scr->menu_item_auxtexture->light_gc, segs, i);
665
666 if (type != F_TOP && type != F_NONE)
667 XDrawLine(dpy, win, scr->menu_item_auxtexture->dark_gc, 0, y + h - 1, w - 1, y + h - 1);
668 }
669
670 static void paintEntry(WMenu * menu, int index, int selected)
671 {
672 WScreen *scr = menu->frame->screen_ptr;
673 Window win = menu->menu->window;
674 WMenuEntry *entry = menu->entries[index];
675 GC light, dim, dark;
676 WMColor *color;
677 int x, y, w, h, tw;
678 int type;
679
680 if (!menu->flags.realized)
681 return;
682 h = menu->entry_height;
683 w = menu->menu->width;
684 y = index * h;
685
686 light = scr->menu_item_auxtexture->light_gc;
687 dim = scr->menu_item_auxtexture->dim_gc;
688 dark = scr->menu_item_auxtexture->dark_gc;
689
690 if (wPreferences.menu_style == MS_FLAT && menu->entry_no > 1) {
691 if (index == 0)
692 type = F_TOP;
693 else if (index == menu->entry_no - 1)
694 type = F_BOTTOM;
695 else
696 type = F_NONE;
697 } else {
698 type = F_NORMAL;
699 }
700
701 /* paint background */
702 if (selected) {
703 XFillRectangle(dpy, win, WMColorGC(scr->select_color), 1, y + 1, w - 2, h - 3);
704 if (scr->menu_item_texture->any.type == WTEX_SOLID)
705 drawFrame(scr, win, y, w, h, type);
706 } else {
707 if (scr->menu_item_texture->any.type == WTEX_SOLID) {
708 XClearArea(dpy, win, 0, y + 1, w - 1, h - 3, False);
709 /* draw the frame */
710 drawFrame(scr, win, y, w, h, type);
711 } else {
712 XClearArea(dpy, win, 0, y, w, h, False);
713 }
714 }
715
716 if (selected) {
717 if (entry->flags.enabled)
718 color = scr->select_text_color;
719 else
720 color = scr->dtext_color;
721 } else if (!entry->flags.enabled) {
722 color = scr->dtext_color;
723 } else {
724 color = scr->mtext_color;
725 }
726 /* draw text */
727 x = 5;
728 if (entry->flags.indicator)
729 x += MENU_INDICATOR_SPACE + 2;
730
731 WMDrawString(scr->wmscreen, win, color, scr->menu_entry_font,
732 x, 3 + y + wPreferences.menu_text_clearance, entry->text, strlen(entry->text));
733
734 if (entry->cascade >= 0) {
735 /* draw the cascade indicator */
736 XDrawLine(dpy, win, dim, w - 11, y + 6, w - 6, y + h / 2 - 1);
737 XDrawLine(dpy, win, light, w - 11, y + h - 8, w - 6, y + h / 2 - 1);
738 XDrawLine(dpy, win, dark, w - 12, y + 6, w - 12, y + h - 8);
739 }
740
741 /* draw indicator */
742 if (entry->flags.indicator && entry->flags.indicator_on) {
743 int iw, ih;
744 WPixmap *indicator;
745
746 switch (entry->flags.indicator_type) {
747 case MI_CHECK:
748 indicator = scr->menu_check_indicator;
749 break;
750 case MI_MINIWINDOW:
751 indicator = scr->menu_mini_indicator;
752 break;
753 case MI_HIDDEN:
754 indicator = scr->menu_hide_indicator;
755 break;
756 case MI_SHADED:
757 indicator = scr->menu_shade_indicator;
758 break;
759 case MI_DIAMOND:
760 default:
761 indicator = scr->menu_radio_indicator;
762 break;
763 }
764
765 iw = indicator->width;
766 ih = indicator->height;
767 XSetClipMask(dpy, scr->copy_gc, indicator->mask);
768 XSetClipOrigin(dpy, scr->copy_gc, 5, y + (h - ih) / 2);
769 if (selected) {
770 if (entry->flags.enabled) {
771 XSetForeground(dpy, scr->copy_gc, WMColorPixel(scr->select_text_color));
772 } else {
773 XSetForeground(dpy, scr->copy_gc, WMColorPixel(scr->dtext_color));
774 }
775 } else {
776 if (entry->flags.enabled) {
777 XSetForeground(dpy, scr->copy_gc, WMColorPixel(scr->mtext_color));
778 } else {
779 XSetForeground(dpy, scr->copy_gc, WMColorPixel(scr->dtext_color));
780 }
781 }
782 XFillRectangle(dpy, win, scr->copy_gc, 5, y + (h - ih) / 2, iw, ih);
783 /*
784 XCopyArea(dpy, indicator->image, win, scr->copy_gc, 0, 0,
785 iw, ih, 5, y+(h-ih)/2);
786 */
787 XSetClipOrigin(dpy, scr->copy_gc, 0, 0);
788 }
789
790 /* draw right text */
791
792 if (entry->rtext && entry->cascade < 0) {
793 tw = WMWidthOfString(scr->menu_entry_font, entry->rtext, strlen(entry->rtext));
794
795 WMDrawString(scr->wmscreen, win, color, scr->menu_entry_font, w - 6 - tw,
796 y + 3 + wPreferences.menu_text_clearance, entry->rtext, strlen(entry->rtext));
797 }
798 }
799
800 static void move_menus(WMenu * menu, int x, int y)
801 {
802 while (menu->parent) {
803 menu = menu->parent;
804 x -= MENUW(menu);
805 if (!wPreferences.align_menus && menu->selected_entry >= 0) {
806 y -= menu->selected_entry * menu->entry_height;
807 }
808 }
809 wMenuMove(menu, x, y, True);
810 }
811
812 static void makeVisible(WMenu * menu)
813 {
814 WScreen *scr = menu->frame->screen_ptr;
815 int x1, y1, x2, y2, new_x, new_y, move;
816 WMRect rect = wGetRectForHead(scr, wGetHeadForPointerLocation(scr));
817
818 if (menu->entry_no < 0)
819 return;
820
821 x1 = menu->frame_x;
822 y1 = menu->frame_y + menu->frame->top_width + menu->selected_entry * menu->entry_height;
823 x2 = x1 + MENUW(menu);
824 y2 = y1 + menu->entry_height;
825
826 new_x = x1;
827 new_y = y1;
828 move = 0;
829
830 if (x1 < rect.pos.x) {
831 new_x = rect.pos.x;
832 move = 1;
833 } else if (x2 >= rect.pos.x + rect.size.width) {
834 new_x = rect.pos.x + rect.size.width - MENUW(menu) - 1;
835 move = 1;
836 }
837
838 if (y1 < rect.pos.y) {
839 new_y = rect.pos.y;
840 move = 1;
841 } else if (y2 >= rect.pos.y + rect.size.height) {
842 new_y = rect.pos.y + rect.size.height - menu->entry_height - 1;
843 move = 1;
844 }
845
846 new_y = new_y - menu->frame->top_width - menu->selected_entry * menu->entry_height;
847 move_menus(menu, new_x, new_y);
848 }
849
850 static int check_key(WMenu * menu, XKeyEvent * event)
851 {
852 int i, ch, s;
853 char buffer[32];
854
855 if (XLookupString(event, buffer, 32, NULL, NULL) < 1)
856 return -1;
857
858 ch = toupper(buffer[0]);
859
860 s = (menu->selected_entry >= 0 ? menu->selected_entry + 1 : 0);
861
862 again:
863 for (i = s; i < menu->entry_no; i++) {
864 if (ch == toupper(menu->entries[i]->text[0])) {
865 return i;
866 }
867 }
868 /* no match. Retry from start, if previous started from a selected entry */
869 if (s != 0) {
870 s = 0;
871 goto again;
872 }
873 return -1;
874 }
875
876 static int keyboardMenu(WMenu * menu)
877 {
878 XEvent event;
879 KeySym ksym = NoSymbol;
880 int done = 0;
881 int index;
882 WMenuEntry *entry;
883 int old_pos_x = menu->frame_x;
884 int old_pos_y = menu->frame_y;
885 int new_x = old_pos_x, new_y = old_pos_y;
886 WMRect rect = wGetRectForHead(menu->frame->screen_ptr,
887 wGetHeadForPointerLocation(menu->frame->screen_ptr));
888
889 if (menu->flags.editing)
890 return False;
891
892 XGrabKeyboard(dpy, menu->frame->core->window, True, GrabModeAsync, GrabModeAsync, CurrentTime);
893
894 if (menu->frame_y + menu->frame->top_width >= rect.pos.y + rect.size.height)
895 new_y = rect.pos.y + rect.size.height - menu->frame->top_width;
896
897 if (menu->frame_x + MENUW(menu) >= rect.pos.x + rect.size.width)
898 new_x = rect.pos.x + rect.size.width - MENUW(menu) - 1;
899
900 move_menus(menu, new_x, new_y);
901
902 while (!done && menu->flags.mapped) {
903 XAllowEvents(dpy, AsyncKeyboard, CurrentTime);
904 WMMaskEvent(dpy, ExposureMask | ButtonMotionMask | ButtonPressMask
905 | ButtonReleaseMask | KeyPressMask | KeyReleaseMask | SubstructureNotifyMask, &event);
906
907 switch (event.type) {
908 case KeyPress:
909 ksym = XLookupKeysym(&event.xkey, 0);
910 switch (ksym) {
911 case XK_Escape:
912 done = 1;
913 break;
914
915 case XK_Home:
916 #ifdef XK_KP_Home
917 case XK_KP_Home:
918 #endif
919 selectEntry(menu, 0);
920 makeVisible(menu);
921 break;
922
923 case XK_End:
924 #ifdef XK_KP_End
925 case XK_KP_End:
926 #endif
927 selectEntry(menu, menu->entry_no - 1);
928 makeVisible(menu);
929 break;
930
931 case XK_Up:
932 #ifdef ARROWLESS_KBD
933 case XK_k:
934 #endif
935 #ifdef XK_KP_Up
936 case XK_KP_Up:
937 #endif
938 if (menu->selected_entry <= 0)
939 selectEntry(menu, menu->entry_no - 1);
940 else
941 selectEntry(menu, menu->selected_entry - 1);
942 makeVisible(menu);
943 break;
944
945 case XK_Down:
946 #ifdef ARROWLESS_KBD
947 case XK_j:
948 #endif
949 #ifdef XK_KP_Down
950 case XK_KP_Down:
951 #endif
952 if (menu->selected_entry < 0)
953 selectEntry(menu, 0);
954 else if (menu->selected_entry == menu->entry_no - 1)
955 selectEntry(menu, 0);
956 else if (menu->selected_entry < menu->entry_no - 1)
957 selectEntry(menu, menu->selected_entry + 1);
958 makeVisible(menu);
959 break;
960
961 case XK_Right:
962 #ifdef ARROWLESS_KBD
963 case XK_l:
964 #endif
965 #ifdef XK_KP_Right
966 case XK_KP_Right:
967 #endif
968 if (menu->selected_entry >= 0) {
969 WMenuEntry *entry;
970 entry = menu->entries[menu->selected_entry];
971
972 if (entry->cascade >= 0 && menu->cascades
973 && menu->cascades[entry->cascade]->entry_no > 0) {
974
975 XUngrabKeyboard(dpy, CurrentTime);
976
977 selectEntry(menu->cascades[entry->cascade], 0);
978 if (!keyboardMenu(menu->cascades[entry->cascade]))
979 done = 1;
980
981 XGrabKeyboard(dpy, menu->frame->core->window, True,
982 GrabModeAsync, GrabModeAsync, CurrentTime);
983 }
984 }
985 break;
986
987 case XK_Left:
988 #ifdef ARROWLESS_KBD
989 case XK_h:
990 #endif
991 #ifdef XK_KP_Left
992 case XK_KP_Left:
993 #endif
994 if (menu->parent != NULL && menu->parent->selected_entry >= 0) {
995 selectEntry(menu, -1);
996 move_menus(menu, old_pos_x, old_pos_y);
997 return True;
998 }
999 break;
1000
1001 case XK_Return:
1002 #ifdef XK_KP_Enter
1003 case XK_KP_Enter:
1004 #endif
1005 done = 2;
1006 break;
1007
1008 default:
1009 index = check_key(menu, &event.xkey);
1010 if (index >= 0) {
1011 selectEntry(menu, index);
1012 }
1013 }
1014 break;
1015
1016 default:
1017 if (event.type == ButtonPress)
1018 done = 1;
1019
1020 WMHandleEvent(&event);
1021 }
1022 }
1023
1024 XUngrabKeyboard(dpy, CurrentTime);
1025
1026 if (done == 2 && menu->selected_entry >= 0) {
1027 entry = menu->entries[menu->selected_entry];
1028 } else {
1029 entry = NULL;
1030 }
1031
1032 if (entry && entry->callback != NULL && entry->flags.enabled && entry->cascade < 0) {
1033 #if (MENU_BLINK_COUNT > 0)
1034 int sel = menu->selected_entry;
1035 int i;
1036
1037 for (i = 0; i < MENU_BLINK_COUNT; i++) {
1038 paintEntry(menu, sel, False);
1039 XSync(dpy, 0);
1040 wusleep(MENU_BLINK_DELAY);
1041 paintEntry(menu, sel, True);
1042 XSync(dpy, 0);
1043 wusleep(MENU_BLINK_DELAY);
1044 }
1045 #endif
1046 selectEntry(menu, -1);
1047
1048 if (!menu->flags.buttoned) {
1049 wMenuUnmap(menu);
1050 move_menus(menu, old_pos_x, old_pos_y);
1051 }
1052 closeCascade(menu);
1053
1054 (*entry->callback) (menu, entry);
1055 } else {
1056 if (!menu->flags.buttoned) {
1057 wMenuUnmap(menu);
1058 move_menus(menu, old_pos_x, old_pos_y);
1059 }
1060 selectEntry(menu, -1);
1061 }
1062
1063 /* returns True if returning from a submenu to a parent menu,
1064 * False if exiting from menu */
1065 return False;
1066 }
1067
1068 void wMenuMapAt(WMenu * menu, int x, int y, int keyboard)
1069 {
1070 WMRect rect = wGetRectForHead(menu->frame->screen_ptr,
1071 wGetHeadForPointerLocation(menu->frame->screen_ptr));
1072
1073 if (!menu->flags.realized) {
1074 menu->flags.realized = 1;
1075 wMenuRealize(menu);
1076 }
1077 if (!menu->flags.mapped) {
1078 if (wPreferences.wrap_menus) {
1079 if (x < rect.pos.x)
1080 x = rect.pos.x;
1081 if (y < rect.pos.y)
1082 y = rect.pos.y;
1083 if (x + MENUW(menu) > rect.pos.x + rect.size.width)
1084 x = rect.pos.x + rect.size.width - MENUW(menu);
1085 if (y + MENUH(menu) > rect.pos.y + rect.size.height)
1086 y = rect.pos.y + rect.size.height - MENUH(menu);
1087 }
1088
1089 XMoveWindow(dpy, menu->frame->core->window, x, y);
1090 menu->frame_x = (x < 0) ? 0 : x;
1091 menu->frame_y = (y < 0) ? 0 : y;
1092 XMapWindow(dpy, menu->frame->core->window);
1093 wRaiseFrame(menu->frame->core);
1094 menu->flags.mapped = 1;
1095 } else {
1096 selectEntry(menu, 0);
1097 }
1098
1099 if (keyboard)
1100 keyboardMenu(menu);
1101 }
1102
1103 void wMenuMap(WMenu * menu)
1104 {
1105 if (!menu->flags.realized) {
1106 menu->flags.realized = 1;
1107 wMenuRealize(menu);
1108 }
1109 if (menu->flags.app_menu && menu->parent == NULL) {
1110 menu->frame_x = menu->frame->screen_ptr->app_menu_x;
1111 menu->frame_y = menu->frame->screen_ptr->app_menu_y;
1112 XMoveWindow(dpy, menu->frame->core->window, menu->frame_x, menu->frame_y);
1113 }
1114 XMapWindow(dpy, menu->frame->core->window);
1115 wRaiseFrame(menu->frame->core);
1116 menu->flags.mapped = 1;
1117 }
1118
1119 void wMenuUnmap(WMenu * menu)
1120 {
1121 int i;
1122
1123 XUnmapWindow(dpy, menu->frame->core->window);
1124 if (menu->flags.titled && menu->flags.buttoned) {
1125 wFrameWindowHideButton(menu->frame, WFF_RIGHT_BUTTON);
1126 }
1127 menu->flags.buttoned = 0;
1128 menu->flags.mapped = 0;
1129 menu->flags.open_to_left = 0;
1130
1131 for (i = 0; i < menu->cascade_no; i++) {
1132 if (menu->cascades[i] != NULL
1133 && menu->cascades[i]->flags.mapped && !menu->cascades[i]->flags.buttoned) {
1134
1135 wMenuUnmap(menu->cascades[i]);
1136 }
1137 }
1138 menu->selected_entry = -1;
1139 }
1140
1141 void wMenuPaint(WMenu * menu)
1142 {
1143 int i;
1144
1145 if (!menu->flags.mapped) {
1146 return;
1147 }
1148
1149 /* paint entries */
1150 for (i = 0; i < menu->entry_no; i++) {
1151 paintEntry(menu, i, i == menu->selected_entry);
1152 }
1153 }
1154
1155 void wMenuSetEnabled(WMenu * menu, int index, int enable)
1156 {
1157 if (index >= menu->entry_no)
1158 return;
1159 menu->entries[index]->flags.enabled = enable;
1160 paintEntry(menu, index, index == menu->selected_entry);
1161 paintEntry(menu->brother, index, index == menu->selected_entry);
1162 }
1163
1164 /* ====================================================================== */
1165
1166 static void editEntry(WMenu * menu, WMenuEntry * entry)
1167 {
1168 WTextInput *text;
1169 XEvent event;
1170 WObjDescriptor *desc;
1171 char *t;
1172 int done = 0;
1173 Window old_focus;
1174 int old_revert;
1175
1176 menu->flags.editing = 1;
1177
1178 text = wTextCreate(menu->menu, 1, menu->entry_height * entry->order,
1179 menu->menu->width - 2, menu->entry_height - 1);
1180
1181 wTextPutText(text, entry->text);
1182 XGetInputFocus(dpy, &old_focus, &old_revert);
1183 XSetInputFocus(dpy, text->core->window, RevertToNone, CurrentTime);
1184
1185 if (XGrabKeyboard(dpy, text->core->window, True, GrabModeAsync, GrabModeAsync, CurrentTime) != GrabSuccess) {
1186 wwarning(_("could not grab keyboard"));
1187 wTextDestroy(text);
1188
1189 wSetFocusTo(menu->frame->screen_ptr, menu->frame->screen_ptr->focused_window);
1190 return;
1191 }
1192
1193 while (!done && !text->done) {
1194 XSync(dpy, 0);
1195 XAllowEvents(dpy, AsyncKeyboard | AsyncPointer, CurrentTime);
1196 XSync(dpy, 0);
1197 WMNextEvent(dpy, &event);
1198
1199 if (XFindContext(dpy, event.xany.window, wWinContext, (XPointer *) & desc) == XCNOENT)
1200 desc = NULL;
1201
1202 if ((desc != NULL) && (desc->handle_anything != NULL)) {
1203
1204 (*desc->handle_anything) (desc, &event);
1205
1206 } else {
1207 switch (event.type) {
1208 case ButtonPress:
1209 XAllowEvents(dpy, ReplayPointer, CurrentTime);
1210 done = 1;
1211
1212 default:
1213 WMHandleEvent(&event);
1214 break;
1215 }
1216 }
1217 }
1218
1219 XSetInputFocus(dpy, old_focus, old_revert, CurrentTime);
1220
1221 wSetFocusTo(menu->frame->screen_ptr, menu->frame->screen_ptr->focused_window);
1222
1223 t = wTextGetText(text);
1224 /* if !t, the user has canceled editing */
1225 if (t) {
1226 if (entry->text)
1227 wfree(entry->text);
1228 entry->text = wstrdup(t);
1229
1230 menu->flags.realized = 0;
1231 }
1232 wTextDestroy(text);
1233
1234 XUngrabKeyboard(dpy, CurrentTime);
1235
1236 if (t && menu->on_edit)
1237 (*menu->on_edit) (menu, entry);
1238
1239 menu->flags.editing = 0;
1240
1241 if (!menu->flags.realized)
1242 wMenuRealize(menu);
1243 }
1244
1245 static void selectEntry(WMenu * menu, int entry_no)
1246 {
1247 WMenuEntry *entry;
1248 WMenu *submenu;
1249 int old_entry;
1250
1251 if (menu->entries == NULL)
1252 return;
1253
1254 if (entry_no >= menu->entry_no)
1255 return;
1256
1257 old_entry = menu->selected_entry;
1258 menu->selected_entry = entry_no;
1259
1260 if (old_entry != entry_no) {
1261
1262 /* unselect previous entry */
1263 if (old_entry >= 0) {
1264 paintEntry(menu, old_entry, False);
1265 entry = menu->entries[old_entry];
1266
1267 /* unmap cascade */
1268 if (entry->cascade >= 0 && menu->cascades) {
1269 if (!menu->cascades[entry->cascade]->flags.buttoned) {
1270 wMenuUnmap(menu->cascades[entry->cascade]);
1271 }
1272 }
1273 }
1274
1275 if (entry_no < 0) {
1276 menu->selected_entry = -1;
1277 return;
1278 }
1279 entry = menu->entries[entry_no];
1280
1281 if (entry->cascade >= 0 && menu->cascades && entry->flags.enabled) {
1282 /* Callback for when the submenu is opened.
1283 */
1284 submenu = menu->cascades[entry->cascade];
1285 if (submenu && submenu->flags.brother)
1286 submenu = submenu->brother;
1287
1288 if (entry->callback) {
1289 /* Only call the callback if the submenu is not yet mapped.
1290 */
1291 if (menu->flags.brother) {
1292 if (!submenu || !submenu->flags.mapped)
1293 (*entry->callback) (menu->brother, entry);
1294 } else {
1295 if (!submenu || !submenu->flags.buttoned)
1296 (*entry->callback) (menu, entry);
1297 }
1298 }
1299
1300 /* the submenu menu might have changed */
1301 submenu = menu->cascades[entry->cascade];
1302
1303 /* map cascade */
1304 if (!submenu->flags.mapped) {
1305 int x, y;
1306
1307 if (!submenu->flags.realized)
1308 wMenuRealize(submenu);
1309 if (wPreferences.wrap_menus) {
1310 if (menu->flags.open_to_left)
1311 submenu->flags.open_to_left = 1;
1312
1313 if (submenu->flags.open_to_left) {
1314 x = menu->frame_x - MENUW(submenu);
1315 if (x < 0) {
1316 x = 0;
1317 submenu->flags.open_to_left = 0;
1318 }
1319 } else {
1320 x = menu->frame_x + MENUW(menu);
1321
1322 if (x + MENUW(submenu)
1323 >= menu->frame->screen_ptr->scr_width) {
1324
1325 x = menu->frame_x - MENUW(submenu);
1326 submenu->flags.open_to_left = 1;
1327 }
1328 }
1329 } else {
1330 x = menu->frame_x + MENUW(menu);
1331 }
1332
1333 if (wPreferences.align_menus) {
1334 y = menu->frame_y;
1335 } else {
1336 y = menu->frame_y + menu->entry_height * entry_no;
1337 if (menu->flags.titled)
1338 y += menu->frame->top_width;
1339 if (menu->cascades[entry->cascade]->flags.titled)
1340 y -= menu->cascades[entry->cascade]->frame->top_width;
1341 }
1342
1343 wMenuMapAt(menu->cascades[entry->cascade], x, y, False);
1344 menu->cascades[entry->cascade]->parent = menu;
1345 } else {
1346 return;
1347 }
1348 }
1349 paintEntry(menu, entry_no, True);
1350 }
1351 }
1352
1353 static WMenu *findMenu(WScreen * scr, int *x_ret, int *y_ret)
1354 {
1355 WMenu *menu;
1356 WObjDescriptor *desc;
1357 Window root_ret, win, junk_win;
1358 int x, y, wx, wy;
1359 unsigned int mask;
1360
1361 XQueryPointer(dpy, scr->root_win, &root_ret, &win, &x, &y, &wx, &wy, &mask);
1362
1363 if (win == None)
1364 return NULL;
1365
1366 if (XFindContext(dpy, win, wWinContext, (XPointer *) & desc) == XCNOENT)
1367 return NULL;
1368
1369 if (desc->parent_type == WCLASS_MENU) {
1370 menu = (WMenu *) desc->parent;
1371 XTranslateCoordinates(dpy, root_ret, menu->menu->window, wx, wy, x_ret, y_ret, &junk_win);
1372 return menu;
1373 }
1374 return NULL;
1375 }
1376
1377 static void closeCascade(WMenu * menu)
1378 {
1379 WMenu *parent = menu->parent;
1380
1381 if (menu->flags.brother || (!menu->flags.buttoned && (!menu->flags.app_menu || menu->parent != NULL))) {
1382
1383 selectEntry(menu, -1);
1384 XSync(dpy, 0);
1385 #if (MENU_BLINK_DELAY > 2)
1386 wusleep(MENU_BLINK_DELAY / 2);
1387 #endif
1388 wMenuUnmap(menu);
1389 while (parent != NULL
1390 && (parent->parent != NULL || !parent->flags.app_menu || parent->flags.brother)
1391 && !parent->flags.buttoned) {
1392 selectEntry(parent, -1);
1393 wMenuUnmap(parent);
1394 parent = parent->parent;
1395 }
1396 if (parent)
1397 selectEntry(parent, -1);
1398 }
1399 }
1400
1401 static void closeBrotherCascadesOf(WMenu * menu)
1402 {
1403 WMenu *tmp;
1404 int i;
1405
1406 for (i = 0; i < menu->cascade_no; i++) {
1407 if (menu->cascades[i]->flags.brother) {
1408 tmp = menu->cascades[i];
1409 } else {
1410 tmp = menu->cascades[i]->brother;
1411 }
1412 if (tmp->flags.mapped) {
1413 selectEntry(tmp->parent, -1);
1414 closeBrotherCascadesOf(tmp);
1415 break;
1416 }
1417 }
1418 }
1419
1420 #define getEntryAt(menu, x, y) ((y)<0 ? -1 : (y)/(menu->entry_height))
1421
1422 static WMenu *parentMenu(WMenu * menu)
1423 {
1424 WMenu *parent;
1425 WMenuEntry *entry;
1426
1427 if (menu->flags.buttoned)
1428 return menu;
1429
1430 while (menu->parent && menu->parent->flags.mapped) {
1431 parent = menu->parent;
1432 if (parent->selected_entry < 0)
1433 break;
1434 entry = parent->entries[parent->selected_entry];
1435 if (!entry->flags.enabled || entry->cascade < 0 || !parent->cascades ||
1436 parent->cascades[entry->cascade] != menu)
1437 break;
1438 menu = parent;
1439 if (menu->flags.buttoned)
1440 break;
1441 }
1442
1443 return menu;
1444 }
1445
1446 /*
1447 * Will raise the passed menu, if submenu = 0
1448 * If submenu > 0 will also raise all mapped submenus
1449 * until the first buttoned one
1450 * If submenu < 0 will also raise all mapped parent menus
1451 * until the first buttoned one
1452 */
1453
1454 static void raiseMenus(WMenu * menu, int submenus)
1455 {
1456 WMenu *submenu;
1457 int i;
1458
1459 if (!menu)
1460 return;
1461
1462 wRaiseFrame(menu->frame->core);
1463
1464 if (submenus > 0 && menu->selected_entry >= 0) {
1465 i = menu->entries[menu->selected_entry]->cascade;
1466 if (i >= 0 && menu->cascades) {
1467 submenu = menu->cascades[i];
1468 if (submenu->flags.mapped && !submenu->flags.buttoned)
1469 raiseMenus(submenu, submenus);
1470 }
1471 }
1472 if (submenus < 0 && !menu->flags.buttoned && menu->parent && menu->parent->flags.mapped)
1473 raiseMenus(menu->parent, submenus);
1474 }
1475
1476 WMenu *wMenuUnderPointer(WScreen * screen)
1477 {
1478 WObjDescriptor *desc;
1479 Window root_ret, win;
1480 int dummy;
1481 unsigned int mask;
1482
1483 XQueryPointer(dpy, screen->root_win, &root_ret, &win, &dummy, &dummy, &dummy, &dummy, &mask);
1484
1485 if (win == None)
1486 return NULL;
1487
1488 if (XFindContext(dpy, win, wWinContext, (XPointer *) & desc) == XCNOENT)
1489 return NULL;
1490
1491 if (desc->parent_type == WCLASS_MENU)
1492 return (WMenu *) desc->parent;
1493 return NULL;
1494 }
1495
1496 static void getPointerPosition(WScreen * scr, int *x, int *y)
1497 {
1498 Window root_ret, win;
1499 int wx, wy;
1500 unsigned int mask;
1501
1502 XQueryPointer(dpy, scr->root_win, &root_ret, &win, x, y, &wx, &wy, &mask);
1503 }
1504
1505 static void getScrollAmount(WMenu * menu, int *hamount, int *vamount)
1506 {
1507 WScreen *scr = menu->menu->screen_ptr;
1508 int menuX1 = menu->frame_x;
1509 int menuY1 = menu->frame_y;
1510 int menuX2 = menu->frame_x + MENUW(menu);
1511 int menuY2 = menu->frame_y + MENUH(menu);
1512 int xroot, yroot;
1513 WMRect rect = wGetRectForHead(scr, wGetHeadForPointerLocation(scr));
1514
1515 *hamount = 0;
1516 *vamount = 0;
1517
1518 getPointerPosition(scr, &xroot, &yroot);
1519
1520 if (xroot <= (rect.pos.x + 1) && menuX1 < rect.pos.x) {
1521 /* scroll to the right */
1522 *hamount = WMIN(MENU_SCROLL_STEP, abs(menuX1));
1523
1524 } else if (xroot >= (rect.pos.x + rect.size.width - 2) && menuX2 > (rect.pos.x + rect.size.width - 1)) {
1525 /* scroll to the left */
1526 *hamount = WMIN(MENU_SCROLL_STEP, abs(menuX2 - rect.pos.x - rect.size.width - 1));
1527
1528 if (*hamount == 0)
1529 *hamount = 1;
1530
1531 *hamount = -*hamount;
1532 }
1533
1534 if (yroot <= (rect.pos.y + 1) && menuY1 < rect.pos.y) {
1535 /* scroll down */
1536 *vamount = WMIN(MENU_SCROLL_STEP, abs(menuY1));
1537
1538 } else if (yroot >= (rect.pos.y + rect.size.height - 2) && menuY2 > (rect.pos.y + rect.size.height - 1)) {
1539 /* scroll up */
1540 *vamount = WMIN(MENU_SCROLL_STEP, abs(menuY2 - rect.pos.y - rect.size.height - 2));
1541
1542 *vamount = -*vamount;
1543 }
1544 }
1545
1546 static void dragScrollMenuCallback(void *data)
1547 {
1548 WMenu *menu = (WMenu *) data;
1549 WScreen *scr = menu->menu->screen_ptr;
1550 WMenu *parent = parentMenu(menu);
1551 int hamount, vamount;
1552 int x, y;
1553 int newSelectedEntry;
1554
1555 getScrollAmount(menu, &hamount, &vamount);
1556
1557 if (hamount != 0 || vamount != 0) {
1558 wMenuMove(parent, parent->frame_x + hamount, parent->frame_y + vamount, True);
1559 if (findMenu(scr, &x, &y)) {
1560 newSelectedEntry = getEntryAt(menu, x, y);
1561 selectEntry(menu, newSelectedEntry);
1562 } else {
1563 /* Pointer fell outside of menu. If the selected entry is
1564 * not a submenu, unselect it */
1565 if (menu->selected_entry >= 0 && menu->entries[menu->selected_entry]->cascade < 0)
1566 selectEntry(menu, -1);
1567 newSelectedEntry = 0;
1568 }
1569
1570 /* paranoid check */
1571 if (newSelectedEntry >= 0) {
1572 /* keep scrolling */
1573 menu->timer = WMAddTimerHandler(MENU_SCROLL_DELAY, dragScrollMenuCallback, menu);
1574 } else {
1575 menu->timer = NULL;
1576 }
1577 } else {
1578 /* don't need to scroll anymore */
1579 menu->timer = NULL;
1580 if (findMenu(scr, &x, &y)) {
1581 newSelectedEntry = getEntryAt(menu, x, y);
1582 selectEntry(menu, newSelectedEntry);
1583 }
1584 }
1585 }
1586
1587 static void scrollMenuCallback(void *data)
1588 {
1589 WMenu *menu = (WMenu *) data;
1590 WMenu *parent = parentMenu(menu);
1591 int hamount = 0; /* amount to scroll */
1592 int vamount = 0;
1593
1594 #ifdef VIRTUAL_DESKTOP
1595 /* don't scroll if it is in vdesk mode */
1596 if (!wPreferences.vdesk_enable)
1597 #endif
1598 getScrollAmount(menu, &hamount, &vamount);
1599
1600 if (hamount != 0 || vamount != 0) {
1601 wMenuMove(parent, parent->frame_x + hamount, parent->frame_y + vamount, True);
1602
1603 /* keep scrolling */
1604 menu->timer = WMAddTimerHandler(MENU_SCROLL_DELAY, scrollMenuCallback, menu);
1605 } else {
1606 /* don't need to scroll anymore */
1607 menu->timer = NULL;
1608 }
1609 }
1610
1611 #define MENU_SCROLL_BORDER 5
1612
1613 static int isPointNearBoder(WMenu * menu, int x, int y)
1614 {
1615 int menuX1 = menu->frame_x;
1616 int menuY1 = menu->frame_y;
1617 int menuX2 = menu->frame_x + MENUW(menu);
1618 int menuY2 = menu->frame_y + MENUH(menu);
1619 int flag = 0;
1620 int head = wGetHeadForPoint(menu->frame->screen_ptr, wmkpoint(x, y));
1621 WMRect rect = wGetRectForHead(menu->frame->screen_ptr, head);
1622
1623 /* XXX: handle screen joins properly !! */
1624
1625 if (x >= menuX1 && x <= menuX2 &&
1626 (y < rect.pos.y + MENU_SCROLL_BORDER || y >= rect.pos.y + rect.size.height - MENU_SCROLL_BORDER))
1627 flag = 1;
1628 else if (y >= menuY1 && y <= menuY2 &&
1629 (x < rect.pos.x + MENU_SCROLL_BORDER || x >= rect.pos.x + rect.size.width - MENU_SCROLL_BORDER))
1630 flag = 1;
1631
1632 return flag;
1633 }
1634
1635 typedef struct _delay {
1636 WMenu *menu;
1637 int ox, oy;
1638 } _delay;
1639
1640 static void leaving(_delay * dl)
1641 {
1642 wMenuMove(dl->menu, dl->ox, dl->oy, True);
1643 dl->menu->jump_back = NULL;
1644 dl->menu->menu->screen_ptr->flags.jump_back_pending = 0;
1645 wfree(dl);
1646 }
1647
1648 void wMenuScroll(WMenu * menu, XEvent * event)
1649 {
1650 WMenu *smenu;
1651 WMenu *omenu = parentMenu(menu);
1652 WScreen *scr = menu->frame->screen_ptr;
1653 int done = 0;
1654 int jump_back = 0;
1655 int old_frame_x = omenu->frame_x;
1656 int old_frame_y = omenu->frame_y;
1657 XEvent ev;
1658
1659 if (omenu->jump_back)
1660 WMDeleteTimerWithClientData(omenu->jump_back);
1661
1662 if (( /*omenu->flags.buttoned && */ !wPreferences.wrap_menus)
1663 || omenu->flags.app_menu) {
1664 jump_back = 1;
1665 }
1666
1667 if (!wPreferences.wrap_menus)
1668 raiseMenus(omenu, True);
1669 else
1670 raiseMenus(menu, False);
1671
1672 if (!menu->timer)
1673 scrollMenuCallback(menu);
1674
1675 while (!done) {
1676 int x, y, on_border, on_x_edge, on_y_edge, on_title;
1677 WMRect rect;
1678
1679 WMNextEvent(dpy, &ev);
1680 switch (ev.type) {
1681 case EnterNotify:
1682 WMHandleEvent(&ev);
1683 case MotionNotify:
1684 x = (ev.type == MotionNotify) ? ev.xmotion.x_root : ev.xcrossing.x_root;
1685 y = (ev.type == MotionNotify) ? ev.xmotion.y_root : ev.xcrossing.y_root;
1686
1687 /* on_border is != 0 if the pointer is between the menu
1688 * and the screen border and is close enough to the border */
1689 on_border = isPointNearBoder(menu, x, y);
1690
1691 smenu = wMenuUnderPointer(scr);
1692
1693 if ((smenu == NULL && !on_border) || (smenu && parentMenu(smenu) != omenu)) {
1694 done = 1;
1695 break;
1696 }
1697
1698 rect = wGetRectForHead(scr, wGetHeadForPoint(scr, wmkpoint(x, y)));
1699 on_x_edge = x <= rect.pos.x + 1 || x >= rect.pos.x + rect.size.width - 2;
1700 on_y_edge = y <= rect.pos.y + 1 || y >= rect.pos.y + rect.size.height - 2;
1701 on_border = on_x_edge || on_y_edge;
1702
1703 if (!on_border && !jump_back) {
1704 done = 1;
1705 break;
1706 }
1707
1708 if (menu->timer && (smenu != menu || (!on_y_edge && !on_x_edge))) {
1709 WMDeleteTimerHandler(menu->timer);
1710 menu->timer = NULL;
1711 }
1712
1713 if (smenu != NULL)
1714 menu = smenu;
1715
1716 if (!menu->timer)
1717 scrollMenuCallback(menu);
1718 break;
1719 case ButtonPress:
1720 /* True if we push on title, or drag the omenu to other position */
1721 on_title = ev.xbutton.x_root >= omenu->frame_x &&
1722 ev.xbutton.x_root <= omenu->frame_x + MENUW(omenu) &&
1723 ev.xbutton.y_root >= omenu->frame_y &&
1724 ev.xbutton.y_root <= omenu->frame_y + omenu->frame->top_width;
1725 WMHandleEvent(&ev);
1726 smenu = wMenuUnderPointer(scr);
1727 if (smenu == NULL || (smenu && smenu->flags.buttoned && smenu != omenu))
1728 done = 1;
1729 else if (smenu == omenu && on_title) {
1730 jump_back = 0;
1731 done = 1;
1732 }
1733 break;
1734 case KeyPress:
1735 done = 1;
1736 default:
1737 WMHandleEvent(&ev);
1738 break;
1739 }
1740 }
1741
1742 if (menu->timer) {
1743 WMDeleteTimerHandler(menu->timer);
1744 menu->timer = NULL;
1745 }
1746
1747 if (jump_back) {
1748 _delay *delayer;
1749 if (!omenu->jump_back) {
1750 delayer = wmalloc(sizeof(_delay));
1751 delayer->menu = omenu;
1752 delayer->ox = old_frame_x;
1753 delayer->oy = old_frame_y;
1754 omenu->jump_back = delayer;
1755 scr->flags.jump_back_pending = 1;
1756 } else
1757 delayer = omenu->jump_back;
1758 WMAddTimerHandler(MENU_JUMP_BACK_DELAY, (WMCallback *) leaving, delayer);
1759 }
1760 }
1761
1762 static void menuExpose(WObjDescriptor * desc, XEvent * event)
1763 {
1764 wMenuPaint(desc->parent);
1765 }
1766
1767 typedef struct {
1768 int *delayed_select;
1769 WMenu *menu;
1770 WMHandlerID magic;
1771 } delay_data;
1772
1773 static void delaySelection(void *data)
1774 {
1775 delay_data *d = (delay_data *) data;
1776 int x, y, entry_no;
1777 WMenu *menu;
1778
1779 d->magic = NULL;
1780
1781 menu = findMenu(d->menu->menu->screen_ptr, &x, &y);
1782 if (menu && (d->menu == menu || d->delayed_select)) {
1783 entry_no = getEntryAt(menu, x, y);
1784 selectEntry(menu, entry_no);
1785 }
1786 if (d->delayed_select)
1787 *(d->delayed_select) = 0;
1788 }
1789
1790 static void menuMouseDown(WObjDescriptor * desc, XEvent * event)
1791 {
1792 XButtonEvent *bev = &event->xbutton;
1793 WMenu *menu = desc->parent;
1794 WMenu *smenu;
1795 WScreen *scr = menu->frame->screen_ptr;
1796 WMenuEntry *entry = NULL;
1797 XEvent ev;
1798 int close_on_exit = 0;
1799 int done = 0;
1800 int delayed_select = 0;
1801 int entry_no;
1802 int x, y;
1803 int prevx, prevy;
1804 int old_frame_x = 0;
1805 int old_frame_y = 0;
1806 delay_data d_data = { NULL, NULL, NULL };
1807
1808 /* Doesn't seem to be needed anymore (if delayed selection handler is
1809 * added only if not present). there seem to be no other side effects
1810 * from removing this and it is also possible that it was only added
1811 * to avoid problems with adding the delayed selection timer handler
1812 * multiple times
1813 */
1814 /*if (menu->flags.inside_handler) {
1815 return;
1816 } */
1817 menu->flags.inside_handler = 1;
1818
1819 if (!wPreferences.wrap_menus) {
1820 smenu = parentMenu(menu);
1821 old_frame_x = smenu->frame_x;
1822 old_frame_y = smenu->frame_y;
1823 } else if (event->xbutton.window == menu->frame->core->window) {
1824 /* This is true if the menu was launched with right click on root window */
1825 if (!d_data.magic) {
1826 delayed_select = 1;
1827 d_data.delayed_select = &delayed_select;
1828 d_data.menu = menu;
1829 d_data.magic = WMAddTimerHandler(wPreferences.dblclick_time, delaySelection, &d_data);
1830 }
1831 }
1832
1833 wRaiseFrame(menu->frame->core);
1834
1835 close_on_exit = (bev->send_event || menu->flags.brother);
1836
1837 smenu = findMenu(scr, &x, &y);
1838 if (!smenu) {
1839 x = -1;
1840 y = -1;
1841 } else {
1842 menu = smenu;
1843 }
1844
1845 if (menu->flags.editing) {
1846 goto byebye;
1847 }
1848 entry_no = getEntryAt(menu, x, y);
1849 if (entry_no >= 0) {
1850 entry = menu->entries[entry_no];
1851
1852 if (!close_on_exit && (bev->state & ControlMask) && smenu && entry->flags.editable) {
1853 editEntry(smenu, entry);
1854 goto byebye;
1855 } else if (bev->state & ControlMask) {
1856 goto byebye;
1857 }
1858
1859 if (entry->flags.enabled && entry->cascade >= 0 && menu->cascades) {
1860 WMenu *submenu = menu->cascades[entry->cascade];
1861 /* map cascade */
1862 if (submenu->flags.mapped && !submenu->flags.buttoned && menu->selected_entry != entry_no) {
1863 wMenuUnmap(submenu);
1864 }
1865 if (!submenu->flags.mapped && !delayed_select) {
1866 selectEntry(menu, entry_no);
1867 } else if (!submenu->flags.buttoned) {
1868 selectEntry(menu, -1);
1869 }
1870
1871 } else if (!delayed_select) {
1872 selectEntry(menu, entry_no);
1873 }
1874
1875 if (!wPreferences.wrap_menus && !wPreferences.scrollable_menus) {
1876 if (!menu->timer)
1877 dragScrollMenuCallback(menu);
1878 }
1879 }
1880 #ifdef VIRTUAL_DESKTOP
1881 if (wPreferences.vdesk_enable) {
1882 wWorkspaceLowerEdge(scr);
1883 }
1884 #endif
1885
1886 prevx = bev->x_root;
1887 prevy = bev->y_root;
1888 while (!done) {
1889 int x, y;
1890
1891 XAllowEvents(dpy, AsyncPointer | SyncPointer, CurrentTime);
1892
1893 WMMaskEvent(dpy, ExposureMask | ButtonMotionMask | ButtonReleaseMask | ButtonPressMask, &ev);
1894 switch (ev.type) {
1895 case MotionNotify:
1896 smenu = findMenu(scr, &x, &y);
1897
1898 if (smenu == NULL) {
1899 /* moved mouse out of menu */
1900
1901 if (!delayed_select && d_data.magic) {
1902 WMDeleteTimerHandler(d_data.magic);
1903 d_data.magic = NULL;
1904 }
1905 if (menu == NULL
1906 || (menu->selected_entry >= 0
1907 && menu->entries[menu->selected_entry]->cascade >= 0)) {
1908 prevx = ev.xmotion.x_root;
1909 prevy = ev.xmotion.y_root;
1910
1911 break;
1912 }
1913 selectEntry(menu, -1);
1914 menu = smenu;
1915 prevx = ev.xmotion.x_root;
1916 prevy = ev.xmotion.y_root;
1917 break;
1918 } else if (menu && menu != smenu
1919 && (menu->selected_entry < 0
1920 || menu->entries[menu->selected_entry]->cascade < 0)) {
1921 selectEntry(menu, -1);
1922
1923 if (!delayed_select && d_data.magic) {
1924 WMDeleteTimerHandler(d_data.magic);
1925 d_data.magic = NULL;
1926 }
1927 } else {
1928
1929 /* hysteresis for item selection */
1930
1931 /* check if the motion was to the side, indicating that
1932 * the user may want to cross to a submenu */
1933 if (!delayed_select && menu) {
1934 int dx;
1935 Bool moved_to_submenu; /* moved to direction of submenu */
1936
1937 dx = abs(prevx - ev.xmotion.x_root);
1938
1939 moved_to_submenu = False;
1940 if (dx > 0 /* if moved enough to the side */
1941 /* maybe a open submenu */
1942 && menu->selected_entry >= 0
1943 /* moving to the right direction */
1944 && (wPreferences.align_menus || ev.xmotion.y_root >= prevy)) {
1945 int index;
1946
1947 index = menu->entries[menu->selected_entry]->cascade;
1948 if (index >= 0) {
1949 if (menu->cascades[index]->frame_x > menu->frame_x) {
1950 if (prevx < ev.xmotion.x_root)
1951 moved_to_submenu = True;
1952 } else {
1953 if (prevx > ev.xmotion.x_root)
1954 moved_to_submenu = True;
1955 }
1956 }
1957 }
1958
1959 if (menu != smenu) {
1960 if (d_data.magic) {
1961 WMDeleteTimerHandler(d_data.magic);
1962 d_data.magic = NULL;
1963 }
1964 } else if (moved_to_submenu) {
1965 /* while we are moving, postpone the selection */
1966 if (d_data.magic) {
1967 WMDeleteTimerHandler(d_data.magic);
1968 }
1969 d_data.delayed_select = NULL;
1970 d_data.menu = menu;
1971 d_data.magic = WMAddTimerHandler(MENU_SELECT_DELAY,
1972 delaySelection, &d_data);
1973 prevx = ev.xmotion.x_root;
1974 prevy = ev.xmotion.y_root;
1975 break;
1976 } else {
1977 if (d_data.magic) {
1978 WMDeleteTimerHandler(d_data.magic);
1979 d_data.magic = NULL;
1980 }
1981 }
1982 }
1983 }
1984 prevx = ev.xmotion.x_root;
1985 prevy = ev.xmotion.y_root;
1986 if (menu != smenu) {
1987 /* pointer crossed menus */
1988 if (menu && menu->timer) {
1989 WMDeleteTimerHandler(menu->timer);
1990 menu->timer = NULL;
1991 }
1992 if (smenu)
1993 dragScrollMenuCallback(smenu);
1994 }
1995 menu = smenu;
1996 if (!menu->timer)
1997 dragScrollMenuCallback(menu);
1998
1999 if (!delayed_select) {
2000 entry_no = getEntryAt(menu, x, y);
2001 if (entry_no >= 0) {
2002 entry = menu->entries[entry_no];
2003 if (entry->flags.enabled && entry->cascade >= 0 && menu->cascades) {
2004 WMenu *submenu = menu->cascades[entry->cascade];
2005 if (submenu->flags.mapped && !submenu->flags.buttoned
2006 && menu->selected_entry != entry_no) {
2007 wMenuUnmap(submenu);
2008 }
2009 }
2010 }
2011 selectEntry(menu, entry_no);
2012 }
2013 break;
2014
2015 case ButtonPress:
2016 break;
2017
2018 case ButtonRelease:
2019 if (ev.xbutton.button == event->xbutton.button)
2020 done = 1;
2021 break;
2022
2023 case Expose:
2024 WMHandleEvent(&ev);
2025 #ifdef VIRTUAL_DESKTOP
2026 /* since expose will raise edge up.. I need another ugly hack here */
2027 if (wPreferences.vdesk_enable) {
2028 wWorkspaceLowerEdge(scr);
2029 }
2030 #endif
2031 break;
2032 }
2033 }
2034
2035 if (menu && menu->timer) {
2036 WMDeleteTimerHandler(menu->timer);
2037 menu->timer = NULL;
2038 }
2039 if (d_data.magic != NULL) {
2040 WMDeleteTimerHandler(d_data.magic);
2041 d_data.magic = NULL;
2042 }
2043
2044 if (menu && menu->selected_entry >= 0) {
2045 entry = menu->entries[menu->selected_entry];
2046 if (entry->callback != NULL && entry->flags.enabled && entry->cascade < 0) {
2047 /* blink and erase menu selection */
2048 #if (MENU_BLINK_DELAY > 0)
2049 int sel = menu->selected_entry;
2050 int i;
2051
2052 for (i = 0; i < MENU_BLINK_COUNT; i++) {
2053 paintEntry(menu, sel, False);
2054 XSync(dpy, 0);
2055 wusleep(MENU_BLINK_DELAY);
2056 paintEntry(menu, sel, True);
2057 XSync(dpy, 0);
2058 wusleep(MENU_BLINK_DELAY);
2059 }
2060 #endif
2061 /* unmap the menu, it's parents and call the callback */
2062 if (!menu->flags.buttoned && (!menu->flags.app_menu || menu->parent != NULL)) {
2063 closeCascade(menu);
2064 } else {
2065 selectEntry(menu, -1);
2066 }
2067 (*entry->callback) (menu, entry);
2068
2069 /* If the user double clicks an entry, the entry will
2070 * be executed twice, which is not good for things like
2071 * the root menu. So, ignore any clicks that were generated
2072 * while the entry was being executed */
2073 while (XCheckTypedWindowEvent(dpy, menu->menu->window, ButtonPress, &ev)) ;
2074 } else if (entry->callback != NULL && entry->cascade < 0) {
2075 selectEntry(menu, -1);
2076 } else {
2077 if (entry->cascade >= 0 && menu->cascades && menu->cascades[entry->cascade]->flags.brother) {
2078 selectEntry(menu, -1);
2079 }
2080 }
2081 }
2082
2083 if (((WMenu *) desc->parent)->flags.brother || close_on_exit || !smenu)
2084 closeCascade(desc->parent);
2085
2086 /* close the cascade windows that should not remain opened */
2087 closeBrotherCascadesOf(desc->parent);
2088
2089 if (!wPreferences.wrap_menus)
2090 wMenuMove(parentMenu(desc->parent), old_frame_x, old_frame_y, True);
2091
2092 byebye:
2093 /* Just to be sure in case we skip the 2 above because of a goto byebye */
2094 if (menu && menu->timer) {
2095 WMDeleteTimerHandler(menu->timer);
2096 menu->timer = NULL;
2097 }
2098 if (d_data.magic != NULL) {
2099 WMDeleteTimerHandler(d_data.magic);
2100 d_data.magic = NULL;
2101 }
2102
2103 ((WMenu *) desc->parent)->flags.inside_handler = 0;
2104 #ifdef VIRTUAL_DESKTOP
2105 wWorkspaceRaiseEdge(scr);
2106 #endif
2107 }
2108
2109 void wMenuMove(WMenu * menu, int x, int y, int submenus)
2110 {
2111 WMenu *submenu;
2112 int i;
2113
2114 if (!menu)
2115 return;
2116
2117 menu->frame_x = x;
2118 menu->frame_y = y;
2119 XMoveWindow(dpy, menu->frame->core->window, x, y);
2120
2121 if (submenus > 0 && menu->selected_entry >= 0) {
2122 i = menu->entries[menu->selected_entry]->cascade;
2123
2124 if (i >= 0 && menu->cascades) {
2125 submenu = menu->cascades[i];
2126 if (submenu->flags.mapped && !submenu->flags.buttoned) {
2127 if (wPreferences.align_menus) {
2128 wMenuMove(submenu, x + MENUW(menu), y, submenus);
2129 } else {
2130 wMenuMove(submenu, x + MENUW(menu),
2131 y + submenu->entry_height * menu->selected_entry, submenus);
2132 }
2133 }
2134 }
2135 }
2136 if (submenus < 0 && menu->parent != NULL && menu->parent->flags.mapped && !menu->parent->flags.buttoned) {
2137 if (wPreferences.align_menus) {
2138 wMenuMove(menu->parent, x - MENUW(menu->parent), y, submenus);
2139 } else {
2140 wMenuMove(menu->parent, x - MENUW(menu->parent), menu->frame_y
2141 - menu->parent->entry_height * menu->parent->selected_entry, submenus);
2142 }
2143 }
2144 }
2145
2146 static void changeMenuLevels(WMenu * menu, int lower)
2147 {
2148 int i;
2149
2150 if (!lower) {
2151 ChangeStackingLevel(menu->frame->core, (!menu->parent ? WMMainMenuLevel : WMSubmenuLevel));
2152 wRaiseFrame(menu->frame->core);
2153 menu->flags.lowered = 0;
2154 } else {
2155 ChangeStackingLevel(menu->frame->core, WMNormalLevel);
2156 wLowerFrame(menu->frame->core);
2157 menu->flags.lowered = 1;
2158 }
2159 for (i = 0; i < menu->cascade_no; i++) {
2160 if (menu->cascades[i]
2161 && !menu->cascades[i]->flags.buttoned && menu->cascades[i]->flags.lowered != lower) {
2162 changeMenuLevels(menu->cascades[i], lower);
2163 }
2164 }
2165 }
2166
2167 static void menuTitleDoubleClick(WCoreWindow * sender, void *data, XEvent * event)
2168 {
2169 WMenu *menu = data;
2170 int lower;
2171
2172 if (event->xbutton.state & MOD_MASK) {
2173 if (menu->flags.lowered) {
2174 lower = 0;
2175 } else {
2176 lower = 1;
2177 }
2178 changeMenuLevels(menu, lower);
2179 }
2180 }
2181
2182 static void menuTitleMouseDown(WCoreWindow * sender, void *data, XEvent * event)
2183 {
2184 WMenu *menu = data;
2185 WMenu *tmp;
2186 XEvent ev;
2187 int x = menu->frame_x, y = menu->frame_y;
2188 int dx = event->xbutton.x_root, dy = event->xbutton.y_root;
2189 int i, lower;
2190 Bool started;
2191
2192 /* can't touch the menu copy */
2193 if (menu->flags.brother)
2194 return;
2195
2196 if (event->xbutton.button != Button1 && event->xbutton.button != Button2)
2197 return;
2198
2199 if (event->xbutton.state & MOD_MASK) {
2200 wLowerFrame(menu->frame->core);
2201 lower = 1;
2202 } else {
2203 wRaiseFrame(menu->frame->core);
2204 lower = 0;
2205 }
2206 tmp = menu;
2207
2208 /* lower/raise all submenus */
2209 while (1) {
2210 if (tmp->selected_entry >= 0 && tmp->cascades && tmp->entries[tmp->selected_entry]->cascade >= 0) {
2211 tmp = tmp->cascades[tmp->entries[tmp->selected_entry]->cascade];
2212 if (!tmp || !tmp->flags.mapped)
2213 break;
2214 if (lower)
2215 wLowerFrame(tmp->frame->core);
2216 else
2217 wRaiseFrame(tmp->frame->core);
2218 } else {
2219 break;
2220 }
2221 }
2222
2223 /* tear off the menu if it's a root menu or a cascade
2224 application menu */
2225 if (!menu->flags.buttoned && !menu->flags.brother && (!menu->flags.app_menu || menu->parent != NULL)) {
2226 menu->flags.buttoned = 1;
2227 wFrameWindowShowButton(menu->frame, WFF_RIGHT_BUTTON);
2228 if (menu->parent) {
2229 /* turn off selected menu entry in parent menu */
2230 selectEntry(menu->parent, -1);
2231
2232 /* make parent map the copy in place of the original */
2233 for (i = 0; i < menu->parent->cascade_no; i++) {
2234 if (menu->parent->cascades[i] == menu) {
2235 menu->parent->cascades[i] = menu->brother;
2236 break;
2237 }
2238 }
2239 }
2240 }
2241
2242 started = False;
2243 while (1) {
2244 WMMaskEvent(dpy, ButtonMotionMask | ButtonReleaseMask | ButtonPressMask | ExposureMask, &ev);
2245 switch (ev.type) {
2246 case MotionNotify:
2247 if (started) {
2248 x += ev.xmotion.x_root - dx;
2249 y += ev.xmotion.y_root - dy;
2250 dx = ev.xmotion.x_root;
2251 dy = ev.xmotion.y_root;
2252 wMenuMove(menu, x, y, True);
2253 } else {
2254 if (abs(ev.xmotion.x_root - dx) > MOVE_THRESHOLD
2255 || abs(ev.xmotion.y_root - dy) > MOVE_THRESHOLD) {
2256 started = True;
2257 XGrabPointer(dpy, menu->frame->titlebar->window, False,
2258 ButtonMotionMask | ButtonReleaseMask
2259 | ButtonPressMask,
2260 GrabModeAsync, GrabModeAsync, None,
2261 wCursor[WCUR_MOVE], CurrentTime);
2262 }
2263 }
2264 break;
2265
2266 case ButtonPress:
2267 break;
2268
2269 case ButtonRelease:
2270 if (ev.xbutton.button != event->xbutton.button)
2271 break;
2272 XUngrabPointer(dpy, CurrentTime);
2273 return;
2274
2275 default:
2276 WMHandleEvent(&ev);
2277 break;
2278 }
2279 }
2280 }
2281
2282 /*
2283 *----------------------------------------------------------------------
2284 * menuCloseClick--
2285 * Handles mouse click on the close button of menus. The menu is
2286 * closed when the button is clicked.
2287 *
2288 * Side effects:
2289 * The closed menu is reinserted at it's parent menus
2290 * cascade list.
2291 *----------------------------------------------------------------------
2292 */
2293 static void menuCloseClick(WCoreWindow * sender, void *data, XEvent * event)
2294 {
2295 WMenu *menu = (WMenu *) data;
2296 WMenu *parent = menu->parent;
2297 int i;
2298
2299 if (parent) {
2300 for (i = 0; i < parent->cascade_no; i++) {
2301 /* find the entry that points to the copy */
2302 if (parent->cascades[i] == menu->brother) {
2303 /* make it point to the original */
2304 parent->cascades[i] = menu;
2305 menu->parent = parent;
2306 break;
2307 }
2308 }
2309 }
2310 wMenuUnmap(menu);
2311 }
2312
2313 static void saveMenuInfo(WMPropList * dict, WMenu * menu, WMPropList * key)
2314 {
2315 WMPropList *value, *list;
2316 char buffer[256];
2317
2318 snprintf(buffer, sizeof(buffer), "%i,%i", menu->frame_x, menu->frame_y);
2319 value = WMCreatePLString(buffer);
2320 list = WMCreatePLArray(value, NULL);
2321 if (menu->flags.lowered)
2322 WMAddToPLArray(list, WMCreatePLString("lowered"));
2323 WMPutInPLDictionary(dict, key, list);
2324 WMReleasePropList(value);
2325 WMReleasePropList(list);
2326 }
2327
2328 void wMenuSaveState(WScreen * scr)
2329 {
2330 WMPropList *menus, *key;
2331 int save_menus = 0;
2332
2333 menus = WMCreatePLDictionary(NULL, NULL);
2334
2335 #ifndef LITE
2336 if (scr->switch_menu && scr->switch_menu->flags.buttoned) {
2337 key = WMCreatePLString("SwitchMenu");
2338 saveMenuInfo(menus, scr->switch_menu, key);
2339 WMReleasePropList(key);
2340 save_menus = 1;
2341 }
2342
2343 if (saveMenuRecurs(menus, scr, scr->root_menu))
2344 save_menus = 1;
2345
2346 #endif /* !LITE */
2347 if (scr->workspace_menu && scr->workspace_menu->flags.buttoned) {
2348 key = WMCreatePLString("WorkspaceMenu");
2349 saveMenuInfo(menus, scr->workspace_menu, key);
2350 WMReleasePropList(key);
2351 save_menus = 1;
2352 }
2353
2354 if (save_menus) {
2355 key = WMCreatePLString("Menus");
2356 WMPutInPLDictionary(scr->session_state, key, menus);
2357 WMReleasePropList(key);
2358 }
2359 WMReleasePropList(menus);
2360 }
2361
2362 #ifndef LITE
2363
2364 static Bool getMenuPath(WMenu * menu, char *buffer, int bufSize)
2365 {
2366 Bool ok = True;
2367 int len = 0;
2368
2369 if (!menu->flags.titled || !menu->frame->title[0])
2370 return False;
2371
2372 len = strlen(menu->frame->title);
2373 if (len >= bufSize)
2374 return False;
2375
2376 if (menu->parent) {
2377 ok = getMenuPath(menu->parent, buffer, bufSize - len - 1);
2378 if (!ok)
2379 return False;
2380 }
2381
2382 strcat(buffer, "\\");
2383 strcat(buffer, menu->frame->title);
2384
2385 return True;
2386 }
2387
2388 static Bool saveMenuRecurs(WMPropList * menus, WScreen * scr, WMenu * menu)
2389 {
2390 WMPropList *key;
2391 int save_menus = 0, i;
2392 char buffer[512];
2393 Bool ok = True;
2394
2395 if (menu->flags.brother)
2396 menu = menu->brother;
2397
2398 if (menu->flags.buttoned && menu != scr->switch_menu) {
2399
2400 buffer[0] = '\0';
2401 ok = getMenuPath(menu, buffer, 510);
2402
2403 if (ok) {
2404 key = WMCreatePLString(buffer);
2405 saveMenuInfo(menus, menu, key);
2406 WMReleasePropList(key);
2407 save_menus = 1;
2408 }
2409 }
2410
2411 if (ok) {
2412 for (i = 0; i < menu->cascade_no; i++) {
2413 if (saveMenuRecurs(menus, scr, menu->cascades[i]))
2414 save_menus = 1;
2415 }
2416 }
2417 return save_menus;
2418 }
2419 #endif /* !LITE */
2420
2421 #define COMPLAIN(key) wwarning(_("bad value in menus state info: %s"), key)
2422
2423 static Bool getMenuInfo(WMPropList * info, int *x, int *y, Bool * lowered)
2424 {
2425 WMPropList *pos;
2426
2427 *lowered = False;
2428
2429 if (WMIsPLArray(info)) {
2430 WMPropList *flags;
2431 pos = WMGetFromPLArray(info, 0);
2432 flags = WMGetFromPLArray(info, 1);
2433 if (flags != NULL && WMIsPLString(flags) && WMGetFromPLString(flags) != NULL
2434 && strcmp(WMGetFromPLString(flags), "lowered") == 0) {
2435 *lowered = True;
2436 }
2437 } else {
2438 pos = info;
2439 }
2440
2441 if (pos != NULL && WMIsPLString(pos)) {
2442 if (sscanf(WMGetFromPLString(pos), "%i,%i", x, y) != 2)
2443 COMPLAIN("Position");
2444 } else {
2445 COMPLAIN("(position, flags...)");
2446 return False;
2447 }
2448
2449 return True;
2450 }
2451
2452 static int restoreMenu(WScreen * scr, WMPropList * menu, int which)
2453 {
2454 int x, y;
2455 Bool lowered = False;
2456 WMenu *pmenu = NULL;
2457
2458 if (!menu)
2459 return False;
2460
2461 if (!getMenuInfo(menu, &x, &y, &lowered))
2462 return False;
2463
2464 #ifndef LITE
2465 if (which & WSS_SWITCHMENU) {
2466 OpenSwitchMenu(scr, x, y, False);
2467 pmenu = scr->switch_menu;
2468 }
2469 #endif /* !LITE */
2470
2471 if (pmenu) {
2472 int width = MENUW(pmenu);
2473 int height = MENUH(pmenu);
2474 WMRect rect = wGetRectForHead(scr, wGetHeadForPointerLocation(scr));
2475
2476 if (lowered) {
2477 changeMenuLevels(pmenu, True);
2478 }
2479
2480 if (x < rect.pos.x - width)
2481 x = rect.pos.x;
2482 if (x > rect.pos.x + rect.size.width)
2483 x = rect.pos.x + rect.size.width - width;
2484 if (y < rect.pos.y)
2485 y = rect.pos.y;
2486 if (y > rect.pos.y + rect.size.height)
2487 y = rect.pos.y + rect.size.height - height;
2488
2489 wMenuMove(pmenu, x, y, True);
2490 pmenu->flags.buttoned = 1;
2491 wFrameWindowShowButton(pmenu->frame, WFF_RIGHT_BUTTON);
2492 return True;
2493 }
2494 return False;
2495 }
2496
2497 #ifndef LITE
2498 static int restoreMenuRecurs(WScreen * scr, WMPropList * menus, WMenu * menu, char *path)
2499 {
2500 WMPropList *key, *entry;
2501 char buffer[512];
2502 int i, x, y, res;
2503 Bool lowered;
2504
2505 if (strlen(path) + strlen(menu->frame->title) > 510)
2506 return False;
2507
2508 snprintf(buffer, sizeof(buffer), "%s\\%s", path, menu->frame->title);
2509 key = WMCreatePLString(buffer);
2510 entry = WMGetFromPLDictionary(menus, key);
2511 res = False;
2512
2513 if (entry && getMenuInfo(entry, &x, &y, &lowered)) {
2514
2515 if (!menu->flags.mapped) {
2516 int width = MENUW(menu);
2517 int height = MENUH(menu);
2518 WMRect rect = wGetRectForHead(scr, wGetHeadForPointerLocation(scr));
2519
2520 wMenuMapAt(menu, x, y, False);
2521
2522 if (menu->parent) {
2523 /* make parent map the copy in place of the original */
2524 for (i = 0; i < menu->parent->cascade_no; i++) {
2525 if (menu->parent->cascades[i] == menu) {
2526 menu->parent->cascades[i] = menu->brother;
2527 break;
2528 }
2529 }
2530 }
2531 if (lowered) {
2532 changeMenuLevels(menu, True);
2533 }
2534
2535 if (x < rect.pos.x - width)
2536 x = rect.pos.x;
2537 if (x > rect.pos.x + rect.size.width)
2538 x = rect.pos.x + rect.size.width - width;
2539 if (y < rect.pos.y)
2540 y = rect.pos.y;
2541 if (y > rect.pos.y + rect.size.height)
2542 y = rect.pos.y + rect.size.height - height;
2543
2544 wMenuMove(menu, x, y, True);
2545 menu->flags.buttoned = 1;
2546 wFrameWindowShowButton(menu->frame, WFF_RIGHT_BUTTON);
2547 res = True;
2548 }
2549 }
2550
2551 WMReleasePropList(key);
2552
2553 for (i = 0; i < menu->cascade_no; i++) {
2554 if (restoreMenuRecurs(scr, menus, menu->cascades[i], buffer) != False)
2555 res = True;
2556 }
2557
2558 return res;
2559 }
2560 #endif /* !LITE */
2561
2562 void wMenuRestoreState(WScreen * scr)
2563 {
2564 WMPropList *menus, *menu, *key, *skey;
2565
2566 if (!scr->session_state) {
2567 return;
2568 }
2569
2570 key = WMCreatePLString("Menus");
2571 menus = WMGetFromPLDictionary(scr->session_state, key);
2572 WMReleasePropList(key);
2573
2574 if (!menus)
2575 return;
2576
2577 /* restore menus */
2578
2579 skey = WMCreatePLString("SwitchMenu");
2580 menu = WMGetFromPLDictionary(menus, skey);
2581 WMReleasePropList(skey);
2582 restoreMenu(scr, menu, WSS_SWITCHMENU);
2583
2584 #ifndef LITE
2585 if (!scr->root_menu) {
2586 OpenRootMenu(scr, scr->scr_width * 2, 0, False);
2587 wMenuUnmap(scr->root_menu);
2588 }
2589 restoreMenuRecurs(scr, menus, scr->root_menu, "");
2590 #endif /* !LITE */
2591 }
2592
2593 void OpenWorkspaceMenu(WScreen * scr, int x, int y)
2594 {
2595 WMenu *menu, *parent;
2596 WMenuEntry *entry;
2597
2598 #ifndef LITE
2599 if (!scr->root_menu) {
2600 OpenRootMenu(scr, scr->scr_width * 2, 0, False);
2601 wMenuUnmap(scr->root_menu);
2602 }
2603 #endif
2604 menu = scr->workspace_menu;
2605 if (menu) {
2606 if (menu->flags.mapped) {
2607 if (!menu->flags.buttoned) {
2608 wMenuUnmap(menu);
2609 parent = menu->parent;
2610 if (parent && parent->selected_entry >= 0) {
2611 entry = parent->entries[parent->selected_entry];
2612 if (parent->cascades[entry->cascade] == menu) {
2613 selectEntry(parent, -1);
2614 wMenuMapAt(menu, x, y, False);
2615 }
2616 }
2617 } else {
2618 wRaiseFrame(menu->frame->core);
2619 wMenuMapCopyAt(menu, x, y);
2620 }
2621 } else {
2622 wMenuMapAt(menu, x, y, False);
2623 }
2624 }
2625 }