1 /* menu.c- generic menu, used for root menu, application menus etc.
3 * Window Maker window manager
5 * Copyright (c) 1997-2003 Alfredo K. Kojima
6 * Copyright (c) 1998-2003 Dan Pascu
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
26 #include <X11/Xutil.h>
27 #include <X11/keysym.h>
35 #include "WindowMaker.h"
43 #include "workspace.h"
46 #include "switchmenu.h"
49 #define MOD_MASK wPreferences.modifier_mask
51 #define MENU_SCROLL_STEP menuScrollParameters[(int)wPreferences.menu_scroll_speed].steps
52 #define MENU_SCROLL_DELAY menuScrollParameters[(int)wPreferences.menu_scroll_speed].delay
54 #define MENUW(m) ((m)->frame->core->width+2*(m)->frame->screen_ptr->frame_border_width)
55 #define MENUH(m) ((m)->frame->core->height+2*(m)->frame->screen_ptr->frame_border_width)
57 /***** Local Stuff ******/
62 } menuScrollParameters
[5] = {
64 MENU_SCROLL_STEPS_UF
, MENU_SCROLL_DELAY_UF
}, {
65 MENU_SCROLL_STEPS_F
, MENU_SCROLL_DELAY_F
}, {
66 MENU_SCROLL_STEPS_M
, MENU_SCROLL_DELAY_M
}, {
67 MENU_SCROLL_STEPS_S
, MENU_SCROLL_DELAY_S
}, {
68 MENU_SCROLL_STEPS_US
, MENU_SCROLL_DELAY_US
}};
70 static void menuMouseDown(WObjDescriptor
* desc
, XEvent
* event
);
71 static void menuExpose(WObjDescriptor
* desc
, XEvent
* event
);
72 static void menuTitleDoubleClick(WCoreWindow
* sender
, void *data
, XEvent
* event
);
73 static void menuTitleMouseDown(WCoreWindow
* sender
, void *data
, XEvent
* event
);
74 static void menuCloseClick(WCoreWindow
* sender
, void *data
, XEvent
* event
);
75 static void updateTexture(WMenu
* menu
);
76 static int saveMenuRecurs(WMPropList
* menus
, WScreen
* scr
, WMenu
* menu
);
77 static int restoreMenuRecurs(WScreen
*scr
, WMPropList
*menus
, WMenu
*menu
, const char *path
);
78 static void selectEntry(WMenu
* menu
, int entry_no
);
79 static void closeCascade(WMenu
* menu
);
81 /****** Notification Observers ******/
83 static void appearanceObserver(void *self
, WMNotification
* notif
)
85 WMenu
*menu
= (WMenu
*) self
;
86 uintptr_t flags
= (uintptr_t)WMGetNotificationClientData(notif
);
88 if (!menu
->flags
.realized
)
91 if (WMGetNotificationName(notif
) == WNMenuAppearanceSettingsChanged
) {
92 if (flags
& WFontSettings
) {
93 menu
->flags
.realized
= 0;
96 if (flags
& WTextureSettings
) {
97 if (!menu
->flags
.brother
)
100 if (flags
& (WTextureSettings
| WColorSettings
)) {
103 } else if (menu
->flags
.titled
) {
105 if (flags
& WFontSettings
) {
106 menu
->flags
.realized
= 0;
109 if (flags
& WTextureSettings
) {
110 menu
->frame
->flags
.need_texture_remake
= 1;
112 if (flags
& (WColorSettings
| WTextureSettings
)) {
113 wFrameWindowPaint(menu
->frame
);
118 /************************************/
121 *----------------------------------------------------------------------
123 * Creates a new empty menu with the specified title. If main_menu
124 * is True, the created menu will be a main menu, which has some special
125 * properties such as being placed over other normal menus.
126 * If title is NULL, the menu will have no titlebar.
130 *----------------------------------------------------------------------
132 WMenu
*wMenuCreate(WScreen
*screen
, const char *title
, int main_menu
)
135 static int brother
= 0;
138 menu
= wmalloc(sizeof(WMenu
));
140 #ifdef SINGLE_MENULEVEL
141 tmp
= WMSubmenuLevel
;
143 tmp
= (main_menu
? WMMainMenuLevel
: WMSubmenuLevel
);
146 flags
= WFF_SINGLE_STATE
| WFF_BORDER
;
148 flags
|= WFF_TITLEBAR
| WFF_RIGHT_BUTTON
;
149 menu
->flags
.titled
= 1;
152 wFrameWindowCreate(screen
, tmp
, 8, 2, 1, 1, &wPreferences
.menu_title_clearance
,
153 &wPreferences
.menu_title_min_height
,
154 &wPreferences
.menu_title_max_height
,
156 screen
->menu_title_texture
, NULL
,
157 screen
->menu_title_color
, &screen
->menu_title_font
,
158 screen
->w_depth
, screen
->w_visual
, screen
->w_colormap
);
160 menu
->frame
->core
->descriptor
.parent
= menu
;
161 menu
->frame
->core
->descriptor
.parent_type
= WCLASS_MENU
;
162 menu
->frame
->core
->descriptor
.handle_mousedown
= menuMouseDown
;
164 wFrameWindowHideButton(menu
->frame
, WFF_RIGHT_BUTTON
);
167 menu
->frame
->title
= wstrdup(title
);
170 menu
->frame
->flags
.justification
= WTJ_LEFT
;
172 menu
->frame
->rbutton_image
= screen
->b_pixmaps
[WBUT_CLOSE
];
175 menu
->alloced_entries
= 0;
176 menu
->selected_entry
= -1;
177 menu
->entries
= NULL
;
179 menu
->frame_x
= screen
->app_menu_x
;
180 menu
->frame_y
= screen
->app_menu_y
;
182 menu
->frame
->child
= menu
;
184 menu
->flags
.lowered
= 0;
185 menu
->flags
.shaded
= 0;
189 /* setup object descriptors */
190 menu
->frame
->on_mousedown_titlebar
= menuTitleMouseDown
;
191 menu
->frame
->on_dblclick_titlebar
= menuTitleDoubleClick
;
194 menu
->frame
->on_click_right
= menuCloseClick
;
196 menu
->menu
= wCoreCreate(menu
->frame
->core
, 0, menu
->frame
->top_width
, menu
->frame
->core
->width
, 10);
198 menu
->menu
->descriptor
.parent
= menu
;
199 menu
->menu
->descriptor
.parent_type
= WCLASS_MENU
;
200 menu
->menu
->descriptor
.handle_expose
= menuExpose
;
201 menu
->menu
->descriptor
.handle_mousedown
= menuMouseDown
;
203 menu
->menu_texture_data
= None
;
205 XMapWindow(dpy
, menu
->menu
->window
);
211 menu
->brother
= wMenuCreate(screen
, title
, main_menu
);
213 menu
->brother
->flags
.brother
= 1;
214 menu
->brother
->brother
= menu
;
216 WMAddNotificationObserver(appearanceObserver
, menu
, WNMenuAppearanceSettingsChanged
, menu
);
218 WMAddNotificationObserver(appearanceObserver
, menu
, WNMenuTitleAppearanceSettingsChanged
, menu
);
223 WMenu
*wMenuCreateForApp(WScreen
*screen
, const char *title
, int main_menu
)
227 menu
= wMenuCreate(screen
, title
, main_menu
);
230 menu
->flags
.app_menu
= 1;
231 menu
->brother
->flags
.app_menu
= 1;
236 static void insertEntry(WMenu
* menu
, WMenuEntry
* entry
, int index
)
240 for (i
= menu
->entry_no
- 1; i
>= index
; i
--) {
241 menu
->entries
[i
]->order
++;
242 menu
->entries
[i
+ 1] = menu
->entries
[i
];
244 menu
->entries
[index
] = entry
;
247 WMenuEntry
*wMenuInsertCallback(WMenu
*menu
, int index
, const char *text
,
248 void (*callback
) (WMenu
* menu
, WMenuEntry
* entry
), void *clientdata
)
252 menu
->flags
.realized
= 0;
253 menu
->brother
->flags
.realized
= 0;
255 /* reallocate array if it's too small */
256 if (menu
->entry_no
>= menu
->alloced_entries
) {
259 tmp
= wrealloc(menu
->entries
, sizeof(WMenuEntry
) * (menu
->alloced_entries
+ 5));
262 menu
->alloced_entries
+= 5;
264 menu
->brother
->entries
= tmp
;
265 menu
->brother
->alloced_entries
= menu
->alloced_entries
;
267 entry
= wmalloc(sizeof(WMenuEntry
));
268 entry
->flags
.enabled
= 1;
269 entry
->text
= wstrdup(text
);
271 entry
->clientdata
= clientdata
;
272 entry
->callback
= callback
;
273 if (index
< 0 || index
>= menu
->entry_no
) {
274 entry
->order
= menu
->entry_no
;
275 menu
->entries
[menu
->entry_no
] = entry
;
277 entry
->order
= index
;
278 insertEntry(menu
, entry
, index
);
282 menu
->brother
->entry_no
= menu
->entry_no
;
287 void wMenuEntrySetCascade(WMenu
* menu
, WMenuEntry
* entry
, WMenu
* cascade
)
289 WMenu
*brother
= menu
->brother
;
292 assert(menu
->flags
.brother
== 0);
294 if (entry
->cascade
>= 0) {
295 menu
->flags
.realized
= 0;
296 brother
->flags
.realized
= 0;
299 cascade
->parent
= menu
;
301 cascade
->brother
->parent
= brother
;
304 for (i
= 0; i
< menu
->cascade_no
; i
++) {
305 if (menu
->cascades
[i
] == NULL
) {
306 menu
->cascades
[i
] = cascade
;
307 brother
->cascades
[i
] = cascade
->brother
;
314 entry
->cascade
= menu
->cascade_no
;
316 menu
->cascades
= wrealloc(menu
->cascades
, sizeof(menu
->cascades
[0]) * (menu
->cascade_no
+ 1));
317 menu
->cascades
[menu
->cascade_no
++] = cascade
;
319 brother
->cascades
= wrealloc(brother
->cascades
, sizeof(brother
->cascades
[0]) * (brother
->cascade_no
+ 1));
320 brother
->cascades
[brother
->cascade_no
++] = cascade
->brother
;
323 if (menu
->flags
.lowered
) {
325 cascade
->flags
.lowered
= 1;
326 ChangeStackingLevel(cascade
->frame
->core
, WMNormalLevel
);
328 cascade
->brother
->flags
.lowered
= 1;
329 ChangeStackingLevel(cascade
->brother
->frame
->core
, WMNormalLevel
);
332 if (!menu
->flags
.realized
)
336 void wMenuEntryRemoveCascade(WMenu
* menu
, WMenuEntry
* entry
)
338 assert(menu
->flags
.brother
== 0);
340 /* destroy cascade menu */
341 if (entry
->cascade
>= 0 && menu
->cascades
&& menu
->cascades
[entry
->cascade
] != NULL
) {
343 wMenuDestroy(menu
->cascades
[entry
->cascade
], True
);
345 menu
->cascades
[entry
->cascade
] = NULL
;
346 menu
->brother
->cascades
[entry
->cascade
] = NULL
;
352 void wMenuRemoveItem(WMenu
* menu
, int index
)
356 if (menu
->flags
.brother
) {
357 wMenuRemoveItem(menu
->brother
, index
);
361 if (index
>= menu
->entry_no
)
364 /* destroy cascade menu */
365 wMenuEntryRemoveCascade(menu
, menu
->entries
[index
]);
367 /* destroy unshared data */
369 if (menu
->entries
[index
]->text
)
370 wfree(menu
->entries
[index
]->text
);
372 if (menu
->entries
[index
]->rtext
)
373 wfree(menu
->entries
[index
]->rtext
);
375 if (menu
->entries
[index
]->free_cdata
&& menu
->entries
[index
]->clientdata
)
376 (*menu
->entries
[index
]->free_cdata
) (menu
->entries
[index
]->clientdata
);
378 wfree(menu
->entries
[index
]);
380 for (i
= index
; i
< menu
->entry_no
- 1; i
++) {
381 menu
->entries
[i
+ 1]->order
--;
382 menu
->entries
[i
] = menu
->entries
[i
+ 1];
385 menu
->brother
->entry_no
--;
388 static Pixmap
renderTexture(WMenu
* menu
)
396 WScreen
*scr
= menu
->menu
->screen_ptr
;
397 WTexture
*texture
= scr
->menu_item_texture
;
399 if (wPreferences
.menu_style
== MS_NORMAL
) {
400 img
= wTextureRenderImage(texture
, menu
->menu
->width
, menu
->entry_height
, WREL_MENUENTRY
);
402 img
= wTextureRenderImage(texture
, menu
->menu
->width
, menu
->menu
->height
+ 1, WREL_MENUENTRY
);
405 wwarning(_("could not render texture: %s"), RMessageForError(RErrorCode
));
410 if (wPreferences
.menu_style
== MS_SINGLE_TEXTURE
) {
412 light
.red
= light
.green
= light
.blue
= 80;
415 dark
.red
= dark
.green
= dark
.blue
= 0;
418 mid
.red
= mid
.green
= mid
.blue
= 40;
420 for (i
= 1; i
< menu
->entry_no
; i
++) {
421 ROperateLine(img
, RSubtractOperation
, 0, i
* menu
->entry_height
- 2,
422 menu
->menu
->width
- 1, i
* menu
->entry_height
- 2, &mid
);
424 RDrawLine(img
, 0, i
* menu
->entry_height
- 1,
425 menu
->menu
->width
- 1, i
* menu
->entry_height
- 1, &dark
);
427 ROperateLine(img
, RAddOperation
, 0, i
* menu
->entry_height
,
428 menu
->menu
->width
- 1, i
* menu
->entry_height
, &light
);
431 if (!RConvertImage(scr
->rcontext
, img
, &pix
)) {
432 wwarning(_("error rendering image:%s"), RMessageForError(RErrorCode
));
439 static void updateTexture(WMenu
* menu
)
441 WScreen
*scr
= menu
->menu
->screen_ptr
;
443 /* setup background texture */
444 if (scr
->menu_item_texture
->any
.type
!= WTEX_SOLID
) {
445 if (!menu
->flags
.brother
) {
446 FREE_PIXMAP(menu
->menu_texture_data
);
448 menu
->menu_texture_data
= renderTexture(menu
);
450 XSetWindowBackgroundPixmap(dpy
, menu
->menu
->window
, menu
->menu_texture_data
);
451 XClearWindow(dpy
, menu
->menu
->window
);
453 XSetWindowBackgroundPixmap(dpy
, menu
->brother
->menu
->window
, menu
->menu_texture_data
);
454 XClearWindow(dpy
, menu
->brother
->menu
->window
);
457 XSetWindowBackground(dpy
, menu
->menu
->window
, scr
->menu_item_texture
->any
.color
.pixel
);
458 XClearWindow(dpy
, menu
->menu
->window
);
462 void wMenuRealize(WMenu
* menu
)
465 int width
, rwidth
, mrwidth
, mwidth
;
466 int theight
, twidth
, eheight
;
467 WScreen
*scr
= menu
->frame
->screen_ptr
;
468 static int brother_done
= 0;
473 wMenuRealize(menu
->brother
);
477 flags
= WFF_SINGLE_STATE
| WFF_BORDER
;
478 if (menu
->flags
.titled
)
479 flags
|= WFF_TITLEBAR
| WFF_RIGHT_BUTTON
;
481 wFrameWindowUpdateBorders(menu
->frame
, flags
);
483 if (menu
->flags
.titled
) {
484 twidth
= WMWidthOfString(scr
->menu_title_font
, menu
->frame
->title
, strlen(menu
->frame
->title
));
485 theight
= menu
->frame
->top_width
;
486 twidth
+= theight
+ (wPreferences
.new_style
== TS_NEW
? 16 : 8);
491 eheight
= WMFontHeight(scr
->menu_entry_font
) + 6 + wPreferences
.menu_text_clearance
* 2;
492 menu
->entry_height
= eheight
;
495 for (i
= 0; i
< menu
->entry_no
; i
++) {
498 /* search widest text */
499 text
= menu
->entries
[i
]->text
;
500 width
= WMWidthOfString(scr
->menu_entry_font
, text
, strlen(text
)) + 10;
502 if (menu
->entries
[i
]->flags
.indicator
) {
503 width
+= MENU_INDICATOR_SPACE
;
509 /* search widest text on right */
510 text
= menu
->entries
[i
]->rtext
;
512 rwidth
= WMWidthOfString(scr
->menu_entry_font
, text
, strlen(text
))
514 else if (menu
->entries
[i
]->cascade
>= 0)
519 if (rwidth
> mrwidth
)
527 wCoreConfigure(menu
->menu
, 0, theight
, mwidth
, menu
->entry_no
* eheight
- 1);
529 wFrameWindowResize(menu
->frame
, mwidth
, menu
->entry_no
* eheight
- 1
530 + menu
->frame
->top_width
+ menu
->frame
->bottom_width
);
534 menu
->flags
.realized
= 1;
536 if (menu
->flags
.mapped
)
538 if (menu
->brother
->flags
.mapped
)
539 wMenuPaint(menu
->brother
);
542 void wMenuDestroy(WMenu
* menu
, int recurse
)
546 WMRemoveNotificationObserver(menu
);
548 /* remove any pending timers */
550 WMDeleteTimerHandler(menu
->timer
);
553 /* call destroy handler */
554 if (menu
->on_destroy
)
555 (*menu
->on_destroy
) (menu
);
557 /* Destroy items if this menu own them. If this is the "brother" menu,
558 * leave them alone as it is shared by them.
560 if (!menu
->flags
.brother
) {
561 for (i
= 0; i
< menu
->entry_no
; i
++) {
563 wfree(menu
->entries
[i
]->text
);
565 if (menu
->entries
[i
]->rtext
)
566 wfree(menu
->entries
[i
]->rtext
);
569 if (menu
->entries
[i
]->instances
) {
570 WMReleasePropList(menu
->entries
[i
]->instances
);
572 #endif /* USER_MENU */
574 if (menu
->entries
[i
]->free_cdata
&& menu
->entries
[i
]->clientdata
) {
575 (*menu
->entries
[i
]->free_cdata
) (menu
->entries
[i
]->clientdata
);
577 wfree(menu
->entries
[i
]);
581 for (i
= 0; i
< menu
->cascade_no
; i
++) {
582 if (menu
->cascades
[i
]) {
583 if (menu
->cascades
[i
]->flags
.brother
)
584 wMenuDestroy(menu
->cascades
[i
]->brother
, recurse
);
586 wMenuDestroy(menu
->cascades
[i
], recurse
);
592 wfree(menu
->entries
);
596 FREE_PIXMAP(menu
->menu_texture_data
);
599 wfree(menu
->cascades
);
601 wCoreDestroy(menu
->menu
);
602 wFrameWindowDestroy(menu
->frame
);
604 /* destroy copy of this menu */
605 if (!menu
->flags
.brother
&& menu
->brother
)
606 wMenuDestroy(menu
->brother
, False
);
616 static void drawFrame(WScreen
* scr
, Drawable win
, int y
, int w
, int h
, int type
)
622 segs
[i
].x1
= segs
[i
].x2
= w
- 1;
624 segs
[i
].y2
= y
+ h
- 1;
626 if (type
!= F_TOP
&& type
!= F_NONE
) {
628 segs
[i
].y1
= segs
[i
].y2
= y
+ h
- 2;
632 XDrawSegments(dpy
, win
, scr
->menu_item_auxtexture
->dim_gc
, segs
, i
);
638 segs
[i
].y2
= y
+ h
- 1;
640 if (type
!= F_BOTTOM
&& type
!= F_NONE
) {
647 XDrawSegments(dpy
, win
, scr
->menu_item_auxtexture
->light_gc
, segs
, i
);
649 if (type
!= F_TOP
&& type
!= F_NONE
)
650 XDrawLine(dpy
, win
, scr
->menu_item_auxtexture
->dark_gc
, 0, y
+ h
- 1, w
- 1, y
+ h
- 1);
653 static void paintEntry(WMenu
* menu
, int index
, int selected
)
655 WScreen
*scr
= menu
->frame
->screen_ptr
;
656 Window win
= menu
->menu
->window
;
657 WMenuEntry
*entry
= menu
->entries
[index
];
663 if (!menu
->flags
.realized
)
665 h
= menu
->entry_height
;
666 w
= menu
->menu
->width
;
669 light
= scr
->menu_item_auxtexture
->light_gc
;
670 dim
= scr
->menu_item_auxtexture
->dim_gc
;
671 dark
= scr
->menu_item_auxtexture
->dark_gc
;
673 if (wPreferences
.menu_style
== MS_FLAT
&& menu
->entry_no
> 1) {
676 else if (index
== menu
->entry_no
- 1)
684 /* paint background */
686 XFillRectangle(dpy
, win
, WMColorGC(scr
->select_color
), 1, y
+ 1, w
- 2, h
- 3);
687 if (scr
->menu_item_texture
->any
.type
== WTEX_SOLID
)
688 drawFrame(scr
, win
, y
, w
, h
, type
);
690 if (scr
->menu_item_texture
->any
.type
== WTEX_SOLID
) {
691 XClearArea(dpy
, win
, 0, y
+ 1, w
- 1, h
- 3, False
);
693 drawFrame(scr
, win
, y
, w
, h
, type
);
695 XClearArea(dpy
, win
, 0, y
, w
, h
, False
);
700 if (entry
->flags
.enabled
)
701 color
= scr
->select_text_color
;
703 color
= scr
->dtext_color
;
704 } else if (!entry
->flags
.enabled
) {
705 color
= scr
->dtext_color
;
707 color
= scr
->mtext_color
;
711 if (entry
->flags
.indicator
)
712 x
+= MENU_INDICATOR_SPACE
+ 2;
714 WMDrawString(scr
->wmscreen
, win
, color
, scr
->menu_entry_font
,
715 x
, 3 + y
+ wPreferences
.menu_text_clearance
, entry
->text
, strlen(entry
->text
));
717 if (entry
->cascade
>= 0) {
718 /* draw the cascade indicator */
719 XDrawLine(dpy
, win
, dim
, w
- 11, y
+ 6, w
- 6, y
+ h
/ 2 - 1);
720 XDrawLine(dpy
, win
, light
, w
- 11, y
+ h
- 8, w
- 6, y
+ h
/ 2 - 1);
721 XDrawLine(dpy
, win
, dark
, w
- 12, y
+ 6, w
- 12, y
+ h
- 8);
725 if (entry
->flags
.indicator
&& entry
->flags
.indicator_on
) {
727 WPixmap
*indicator
= NULL
;
729 switch (entry
->flags
.indicator_type
) {
731 indicator
= scr
->menu_radio_indicator
;
734 indicator
= scr
->menu_check_indicator
;
737 indicator
= scr
->menu_mini_indicator
;
740 indicator
= scr
->menu_hide_indicator
;
743 indicator
= scr
->menu_shade_indicator
;
746 indicator
= scr
->menu_snap_vertical_indicator
;
749 indicator
= scr
->menu_snap_horizontal_indicator
;
752 indicator
= scr
->menu_central_indicator
;
755 indicator
= scr
->menu_snap_rh_indicator
;
758 indicator
= scr
->menu_snap_lh_indicator
;
761 indicator
= scr
->menu_snap_th_indicator
;
764 indicator
= scr
->menu_snap_bh_indicator
;
767 indicator
= scr
->menu_snap_tl_indicator
;
770 indicator
= scr
->menu_snap_tr_indicator
;
773 indicator
= scr
->menu_snap_bl_indicator
;
776 indicator
= scr
->menu_snap_br_indicator
;
779 indicator
= scr
->menu_snap_tiled_indicator
;
787 iw
= indicator
->width
;
788 ih
= indicator
->height
;
789 XSetClipMask(dpy
, scr
->copy_gc
, indicator
->mask
);
790 XSetClipOrigin(dpy
, scr
->copy_gc
, 5, y
+ (h
- ih
) / 2);
792 if (entry
->flags
.enabled
) {
793 XSetForeground(dpy
, scr
->copy_gc
, WMColorPixel(scr
->select_text_color
));
795 XSetForeground(dpy
, scr
->copy_gc
, WMColorPixel(scr
->dtext_color
));
798 if (entry
->flags
.enabled
) {
799 XSetForeground(dpy
, scr
->copy_gc
, WMColorPixel(scr
->mtext_color
));
801 XSetForeground(dpy
, scr
->copy_gc
, WMColorPixel(scr
->dtext_color
));
804 XFillRectangle(dpy
, win
, scr
->copy_gc
, 5, y
+ (h
- ih
) / 2, iw
, ih
);
806 XCopyArea(dpy, indicator->image, win, scr->copy_gc, 0, 0,
807 iw, ih, 5, y+(h-ih)/2);
809 XSetClipOrigin(dpy
, scr
->copy_gc
, 0, 0);
813 /* draw right text */
815 if (entry
->rtext
&& entry
->cascade
< 0) {
816 tw
= WMWidthOfString(scr
->menu_entry_font
, entry
->rtext
, strlen(entry
->rtext
));
818 WMDrawString(scr
->wmscreen
, win
, color
, scr
->menu_entry_font
, w
- 6 - tw
,
819 y
+ 3 + wPreferences
.menu_text_clearance
, entry
->rtext
, strlen(entry
->rtext
));
823 static void move_menus(WMenu
* menu
, int x
, int y
)
825 while (menu
->parent
) {
828 if (!wPreferences
.align_menus
&& menu
->selected_entry
>= 0) {
829 y
-= menu
->selected_entry
* menu
->entry_height
;
832 wMenuMove(menu
, x
, y
, True
);
835 static void makeVisible(WMenu
* menu
)
837 WScreen
*scr
= menu
->frame
->screen_ptr
;
838 int x1
, y1
, x2
, y2
, new_x
, new_y
;
839 WMRect rect
= wGetRectForHead(scr
, wGetHeadForPointerLocation(scr
));
841 if (menu
->entry_no
< 0)
845 y1
= menu
->frame_y
+ menu
->frame
->top_width
+ menu
->selected_entry
* menu
->entry_height
;
846 x2
= x1
+ MENUW(menu
);
847 y2
= y1
+ menu
->entry_height
;
852 if (x1
< rect
.pos
.x
) {
854 } else if (x2
>= rect
.pos
.x
+ rect
.size
.width
) {
855 new_x
= rect
.pos
.x
+ rect
.size
.width
- MENUW(menu
) - 1;
858 if (y1
< rect
.pos
.y
) {
860 } else if (y2
>= rect
.pos
.y
+ rect
.size
.height
) {
861 new_y
= rect
.pos
.y
+ rect
.size
.height
- menu
->entry_height
- 1;
864 new_y
= new_y
- menu
->frame
->top_width
- menu
->selected_entry
* menu
->entry_height
;
865 move_menus(menu
, new_x
, new_y
);
868 static int check_key(WMenu
* menu
, XKeyEvent
* event
)
873 if (XLookupString(event
, buffer
, 32, NULL
, NULL
) < 1)
876 ch
= toupper(buffer
[0]);
878 s
= (menu
->selected_entry
>= 0 ? menu
->selected_entry
+ 1 : 0);
881 for (i
= s
; i
< menu
->entry_no
; i
++) {
882 if (ch
== toupper(menu
->entries
[i
]->text
[0])) {
886 /* no match. Retry from start, if previous started from a selected entry */
894 static int keyboardMenu(WMenu
* menu
)
897 KeySym ksym
= NoSymbol
;
901 int old_pos_x
= menu
->frame_x
;
902 int old_pos_y
= menu
->frame_y
;
903 int new_x
= old_pos_x
, new_y
= old_pos_y
;
904 WMRect rect
= wGetRectForHead(menu
->frame
->screen_ptr
,
905 wGetHeadForPointerLocation(menu
->frame
->screen_ptr
));
907 if (menu
->flags
.editing
)
910 XGrabKeyboard(dpy
, menu
->frame
->core
->window
, True
, GrabModeAsync
, GrabModeAsync
, CurrentTime
);
912 if (menu
->frame_y
+ menu
->frame
->top_width
>= rect
.pos
.y
+ rect
.size
.height
)
913 new_y
= rect
.pos
.y
+ rect
.size
.height
- menu
->frame
->top_width
;
915 if (menu
->frame_x
+ MENUW(menu
) >= rect
.pos
.x
+ rect
.size
.width
)
916 new_x
= rect
.pos
.x
+ rect
.size
.width
- MENUW(menu
) - 1;
918 move_menus(menu
, new_x
, new_y
);
920 while (!done
&& menu
->flags
.mapped
) {
921 XAllowEvents(dpy
, AsyncKeyboard
, CurrentTime
);
922 WMMaskEvent(dpy
, ExposureMask
| ButtonMotionMask
| ButtonPressMask
923 | ButtonReleaseMask
| KeyPressMask
| KeyReleaseMask
| SubstructureNotifyMask
, &event
);
925 switch (event
.type
) {
927 ksym
= XLookupKeysym(&event
.xkey
, 0);
928 if (wPreferences
.vi_key_menus
) {
957 selectEntry(menu
, 0);
965 selectEntry(menu
, menu
->entry_no
- 1);
973 if (menu
->selected_entry
<= 0)
974 selectEntry(menu
, menu
->entry_no
- 1);
976 selectEntry(menu
, menu
->selected_entry
- 1);
984 if (menu
->selected_entry
< 0)
985 selectEntry(menu
, 0);
986 else if (menu
->selected_entry
== menu
->entry_no
- 1)
987 selectEntry(menu
, 0);
988 else if (menu
->selected_entry
< menu
->entry_no
- 1)
989 selectEntry(menu
, menu
->selected_entry
+ 1);
997 if (menu
->selected_entry
>= 0) {
999 entry
= menu
->entries
[menu
->selected_entry
];
1001 if (entry
->cascade
>= 0 && menu
->cascades
1002 && menu
->cascades
[entry
->cascade
]->entry_no
> 0) {
1004 XUngrabKeyboard(dpy
, CurrentTime
);
1006 selectEntry(menu
->cascades
[entry
->cascade
], 0);
1007 if (!keyboardMenu(menu
->cascades
[entry
->cascade
]))
1010 XGrabKeyboard(dpy
, menu
->frame
->core
->window
, True
,
1011 GrabModeAsync
, GrabModeAsync
, CurrentTime
);
1020 if (menu
->parent
!= NULL
&& menu
->parent
->selected_entry
>= 0) {
1021 selectEntry(menu
, -1);
1022 move_menus(menu
, old_pos_x
, old_pos_y
);
1035 index
= check_key(menu
, &event
.xkey
);
1037 selectEntry(menu
, index
);
1043 if (event
.type
== ButtonPress
)
1046 WMHandleEvent(&event
);
1050 XUngrabKeyboard(dpy
, CurrentTime
);
1052 if (done
== 2 && menu
->selected_entry
>= 0) {
1053 entry
= menu
->entries
[menu
->selected_entry
];
1058 if (entry
&& entry
->callback
!= NULL
&& entry
->flags
.enabled
&& entry
->cascade
< 0) {
1059 #if (MENU_BLINK_COUNT > 0)
1060 int sel
= menu
->selected_entry
;
1063 for (i
= 0; i
< MENU_BLINK_COUNT
; i
++) {
1064 paintEntry(menu
, sel
, False
);
1066 wusleep(MENU_BLINK_DELAY
);
1067 paintEntry(menu
, sel
, True
);
1069 wusleep(MENU_BLINK_DELAY
);
1072 selectEntry(menu
, -1);
1074 if (!menu
->flags
.buttoned
) {
1076 move_menus(menu
, old_pos_x
, old_pos_y
);
1080 (*entry
->callback
) (menu
, entry
);
1082 if (!menu
->flags
.buttoned
) {
1084 move_menus(menu
, old_pos_x
, old_pos_y
);
1086 selectEntry(menu
, -1);
1089 /* returns True if returning from a submenu to a parent menu,
1090 * False if exiting from menu */
1094 void wMenuMapAt(WMenu
* menu
, int x
, int y
, int keyboard
)
1096 if (!menu
->flags
.realized
) {
1097 menu
->flags
.realized
= 1;
1101 if (!menu
->flags
.mapped
) {
1102 if (wPreferences
.wrap_menus
) {
1103 WScreen
*scr
= menu
->frame
->screen_ptr
;
1104 WMRect rect
= wGetRectForHead(scr
, wGetHeadForPointerLocation(scr
));
1110 if (x
+ MENUW(menu
) > rect
.pos
.x
+ rect
.size
.width
)
1111 x
= rect
.pos
.x
+ rect
.size
.width
- MENUW(menu
);
1112 if (y
+ MENUH(menu
) > rect
.pos
.y
+ rect
.size
.height
)
1113 y
= rect
.pos
.y
+ rect
.size
.height
- MENUH(menu
);
1116 XMoveWindow(dpy
, menu
->frame
->core
->window
, x
, y
);
1119 XMapWindow(dpy
, menu
->frame
->core
->window
);
1120 wRaiseFrame(menu
->frame
->core
);
1121 menu
->flags
.mapped
= 1;
1123 selectEntry(menu
, 0);
1130 void wMenuMap(WMenu
* menu
)
1132 if (!menu
->flags
.realized
) {
1133 menu
->flags
.realized
= 1;
1136 if (menu
->flags
.app_menu
&& menu
->parent
== NULL
) {
1137 menu
->frame_x
= menu
->frame
->screen_ptr
->app_menu_x
;
1138 menu
->frame_y
= menu
->frame
->screen_ptr
->app_menu_y
;
1139 XMoveWindow(dpy
, menu
->frame
->core
->window
, menu
->frame_x
, menu
->frame_y
);
1141 XMapWindow(dpy
, menu
->frame
->core
->window
);
1142 wRaiseFrame(menu
->frame
->core
);
1143 menu
->flags
.mapped
= 1;
1146 void wMenuUnmap(WMenu
* menu
)
1150 XUnmapWindow(dpy
, menu
->frame
->core
->window
);
1151 if (menu
->flags
.titled
&& menu
->flags
.buttoned
) {
1152 wFrameWindowHideButton(menu
->frame
, WFF_RIGHT_BUTTON
);
1154 menu
->flags
.buttoned
= 0;
1155 menu
->flags
.mapped
= 0;
1156 menu
->flags
.open_to_left
= 0;
1158 if (menu
->flags
.shaded
) {
1159 wFrameWindowResize(menu
->frame
, menu
->frame
->core
->width
, menu
->frame
->top_width
+
1160 menu
->entry_height
*menu
->entry_no
+ menu
->frame
->bottom_width
- 1);
1161 menu
->flags
.shaded
= 0;
1164 for (i
= 0; i
< menu
->cascade_no
; i
++) {
1165 if (menu
->cascades
[i
] != NULL
1166 && menu
->cascades
[i
]->flags
.mapped
&& !menu
->cascades
[i
]->flags
.buttoned
) {
1168 wMenuUnmap(menu
->cascades
[i
]);
1171 menu
->selected_entry
= -1;
1174 void wMenuPaint(WMenu
* menu
)
1178 if (!menu
->flags
.mapped
) {
1183 for (i
= 0; i
< menu
->entry_no
; i
++) {
1184 paintEntry(menu
, i
, i
== menu
->selected_entry
);
1188 void wMenuSetEnabled(WMenu
* menu
, int index
, int enable
)
1190 if (index
>= menu
->entry_no
)
1192 menu
->entries
[index
]->flags
.enabled
= enable
;
1193 paintEntry(menu
, index
, index
== menu
->selected_entry
);
1194 paintEntry(menu
->brother
, index
, index
== menu
->selected_entry
);
1198 static void selectEntry(WMenu
* menu
, int entry_no
)
1204 if (menu
->entries
== NULL
)
1207 if (entry_no
>= menu
->entry_no
)
1210 old_entry
= menu
->selected_entry
;
1211 menu
->selected_entry
= entry_no
;
1213 if (old_entry
!= entry_no
) {
1215 /* unselect previous entry */
1216 if (old_entry
>= 0) {
1217 paintEntry(menu
, old_entry
, False
);
1218 entry
= menu
->entries
[old_entry
];
1221 if (entry
->cascade
>= 0 && menu
->cascades
) {
1222 if (!menu
->cascades
[entry
->cascade
]->flags
.buttoned
) {
1223 wMenuUnmap(menu
->cascades
[entry
->cascade
]);
1229 menu
->selected_entry
= -1;
1232 entry
= menu
->entries
[entry_no
];
1234 if (entry
->cascade
>= 0 && menu
->cascades
&& entry
->flags
.enabled
) {
1235 /* Callback for when the submenu is opened.
1237 submenu
= menu
->cascades
[entry
->cascade
];
1238 if (submenu
&& submenu
->flags
.brother
)
1239 submenu
= submenu
->brother
;
1241 if (entry
->callback
) {
1242 /* Only call the callback if the submenu is not yet mapped.
1244 if (menu
->flags
.brother
) {
1245 if (!submenu
|| !submenu
->flags
.mapped
)
1246 (*entry
->callback
) (menu
->brother
, entry
);
1248 if (!submenu
|| !submenu
->flags
.buttoned
)
1249 (*entry
->callback
) (menu
, entry
);
1253 /* the submenu menu might have changed */
1254 submenu
= menu
->cascades
[entry
->cascade
];
1257 if (!submenu
->flags
.mapped
) {
1260 if (!submenu
->flags
.realized
)
1261 wMenuRealize(submenu
);
1262 if (wPreferences
.wrap_menus
) {
1263 if (menu
->flags
.open_to_left
)
1264 submenu
->flags
.open_to_left
= 1;
1266 if (submenu
->flags
.open_to_left
) {
1267 x
= menu
->frame_x
- MENUW(submenu
);
1270 submenu
->flags
.open_to_left
= 0;
1273 x
= menu
->frame_x
+ MENUW(menu
);
1275 if (x
+ MENUW(submenu
)
1276 >= menu
->frame
->screen_ptr
->scr_width
) {
1278 x
= menu
->frame_x
- MENUW(submenu
);
1279 submenu
->flags
.open_to_left
= 1;
1283 x
= menu
->frame_x
+ MENUW(menu
);
1286 if (wPreferences
.align_menus
) {
1289 y
= menu
->frame_y
+ menu
->entry_height
* entry_no
;
1290 if (menu
->flags
.titled
)
1291 y
+= menu
->frame
->top_width
;
1292 if (menu
->cascades
[entry
->cascade
]->flags
.titled
)
1293 y
-= menu
->cascades
[entry
->cascade
]->frame
->top_width
;
1296 wMenuMapAt(menu
->cascades
[entry
->cascade
], x
, y
, False
);
1297 menu
->cascades
[entry
->cascade
]->parent
= menu
;
1302 paintEntry(menu
, entry_no
, True
);
1306 static WMenu
*findMenu(WScreen
* scr
, int *x_ret
, int *y_ret
)
1309 WObjDescriptor
*desc
;
1310 Window root_ret
, win
, junk_win
;
1314 XQueryPointer(dpy
, scr
->root_win
, &root_ret
, &win
, &x
, &y
, &wx
, &wy
, &mask
);
1319 if (XFindContext(dpy
, win
, w_global
.context
.client_win
, (XPointer
*) & desc
) == XCNOENT
)
1322 if (desc
->parent_type
== WCLASS_MENU
) {
1323 menu
= (WMenu
*) desc
->parent
;
1324 XTranslateCoordinates(dpy
, root_ret
, menu
->menu
->window
, wx
, wy
, x_ret
, y_ret
, &junk_win
);
1330 static void closeCascade(WMenu
* menu
)
1332 WMenu
*parent
= menu
->parent
;
1334 if (menu
->flags
.brother
|| (!menu
->flags
.buttoned
&& (!menu
->flags
.app_menu
|| menu
->parent
!= NULL
))) {
1336 selectEntry(menu
, -1);
1338 #if (MENU_BLINK_DELAY > 2)
1339 wusleep(MENU_BLINK_DELAY
/ 2);
1342 while (parent
!= NULL
1343 && (parent
->parent
!= NULL
|| !parent
->flags
.app_menu
|| parent
->flags
.brother
)
1344 && !parent
->flags
.buttoned
) {
1345 selectEntry(parent
, -1);
1347 parent
= parent
->parent
;
1350 selectEntry(parent
, -1);
1354 static void closeBrotherCascadesOf(WMenu
* menu
)
1359 for (i
= 0; i
< menu
->cascade_no
; i
++) {
1360 if (menu
->cascades
[i
]->flags
.brother
) {
1361 tmp
= menu
->cascades
[i
];
1363 tmp
= menu
->cascades
[i
]->brother
;
1365 if (tmp
->flags
.mapped
) {
1366 selectEntry(tmp
->parent
, -1);
1367 closeBrotherCascadesOf(tmp
);
1373 #define getEntryAt(menu, x, y) ((y)<0 ? -1 : (y)/(menu->entry_height))
1375 static WMenu
*parentMenu(WMenu
* menu
)
1380 if (menu
->flags
.buttoned
)
1383 while (menu
->parent
&& menu
->parent
->flags
.mapped
) {
1384 parent
= menu
->parent
;
1385 if (parent
->selected_entry
< 0)
1387 entry
= parent
->entries
[parent
->selected_entry
];
1388 if (!entry
->flags
.enabled
|| entry
->cascade
< 0 || !parent
->cascades
||
1389 parent
->cascades
[entry
->cascade
] != menu
)
1392 if (menu
->flags
.buttoned
)
1400 * Will raise the passed menu, if submenu = 0
1401 * If submenu > 0 will also raise all mapped submenus
1402 * until the first buttoned one
1403 * If submenu < 0 will also raise all mapped parent menus
1404 * until the first buttoned one
1407 static void raiseMenus(WMenu
* menu
, int submenus
)
1415 wRaiseFrame(menu
->frame
->core
);
1417 if (submenus
> 0 && menu
->selected_entry
>= 0) {
1418 i
= menu
->entries
[menu
->selected_entry
]->cascade
;
1419 if (i
>= 0 && menu
->cascades
) {
1420 submenu
= menu
->cascades
[i
];
1421 if (submenu
->flags
.mapped
&& !submenu
->flags
.buttoned
)
1422 raiseMenus(submenu
, submenus
);
1425 if (submenus
< 0 && !menu
->flags
.buttoned
&& menu
->parent
&& menu
->parent
->flags
.mapped
)
1426 raiseMenus(menu
->parent
, submenus
);
1429 WMenu
*wMenuUnderPointer(WScreen
* screen
)
1431 WObjDescriptor
*desc
;
1432 Window root_ret
, win
;
1436 XQueryPointer(dpy
, screen
->root_win
, &root_ret
, &win
, &dummy
, &dummy
, &dummy
, &dummy
, &mask
);
1441 if (XFindContext(dpy
, win
, w_global
.context
.client_win
, (XPointer
*) & desc
) == XCNOENT
)
1444 if (desc
->parent_type
== WCLASS_MENU
)
1445 return (WMenu
*) desc
->parent
;
1449 static void getPointerPosition(WScreen
* scr
, int *x
, int *y
)
1451 Window root_ret
, win
;
1455 XQueryPointer(dpy
, scr
->root_win
, &root_ret
, &win
, x
, y
, &wx
, &wy
, &mask
);
1458 static void getScrollAmount(WMenu
* menu
, int *hamount
, int *vamount
)
1460 WScreen
*scr
= menu
->menu
->screen_ptr
;
1461 int menuX1
= menu
->frame_x
;
1462 int menuY1
= menu
->frame_y
;
1463 int menuX2
= menu
->frame_x
+ MENUW(menu
);
1464 int menuY2
= menu
->frame_y
+ MENUH(menu
);
1466 WMRect rect
= wGetRectForHead(scr
, wGetHeadForPointerLocation(scr
));
1471 getPointerPosition(scr
, &xroot
, &yroot
);
1473 if (xroot
<= (rect
.pos
.x
+ 1) && menuX1
< rect
.pos
.x
) {
1474 /* scroll to the right */
1475 *hamount
= WMIN(MENU_SCROLL_STEP
, abs(menuX1
));
1477 } else if (xroot
>= (rect
.pos
.x
+ rect
.size
.width
- 2) && menuX2
> (rect
.pos
.x
+ rect
.size
.width
- 1)) {
1478 /* scroll to the left */
1479 *hamount
= WMIN(MENU_SCROLL_STEP
, abs(menuX2
- rect
.pos
.x
- (int)rect
.size
.width
- 1));
1484 *hamount
= -*hamount
;
1487 if (yroot
<= (rect
.pos
.y
+ 1) && menuY1
< rect
.pos
.y
) {
1489 *vamount
= WMIN(MENU_SCROLL_STEP
, abs(menuY1
));
1491 } else if (yroot
>= (rect
.pos
.y
+ rect
.size
.height
- 2) && menuY2
> (rect
.pos
.y
+ rect
.size
.height
- 1)) {
1493 *vamount
= WMIN(MENU_SCROLL_STEP
, abs(menuY2
- rect
.pos
.y
- (int)rect
.size
.height
- 2));
1495 *vamount
= -*vamount
;
1499 static void dragScrollMenuCallback(void *data
)
1501 WMenu
*menu
= (WMenu
*) data
;
1502 WScreen
*scr
= menu
->menu
->screen_ptr
;
1503 WMenu
*parent
= parentMenu(menu
);
1504 int hamount
, vamount
;
1506 int newSelectedEntry
;
1508 getScrollAmount(menu
, &hamount
, &vamount
);
1510 if (hamount
!= 0 || vamount
!= 0) {
1511 wMenuMove(parent
, parent
->frame_x
+ hamount
, parent
->frame_y
+ vamount
, True
);
1512 if (findMenu(scr
, &x
, &y
)) {
1513 newSelectedEntry
= getEntryAt(menu
, x
, y
);
1514 selectEntry(menu
, newSelectedEntry
);
1516 /* Pointer fell outside of menu. If the selected entry is
1517 * not a submenu, unselect it */
1518 if (menu
->selected_entry
>= 0 && menu
->entries
[menu
->selected_entry
]->cascade
< 0)
1519 selectEntry(menu
, -1);
1520 newSelectedEntry
= 0;
1523 /* paranoid check */
1524 if (newSelectedEntry
>= 0) {
1525 /* keep scrolling */
1526 menu
->timer
= WMAddTimerHandler(MENU_SCROLL_DELAY
, dragScrollMenuCallback
, menu
);
1531 /* don't need to scroll anymore */
1533 if (findMenu(scr
, &x
, &y
)) {
1534 newSelectedEntry
= getEntryAt(menu
, x
, y
);
1535 selectEntry(menu
, newSelectedEntry
);
1540 static void scrollMenuCallback(void *data
)
1542 WMenu
*menu
= (WMenu
*) data
;
1543 WMenu
*parent
= parentMenu(menu
);
1544 int hamount
= 0; /* amount to scroll */
1547 getScrollAmount(menu
, &hamount
, &vamount
);
1549 if (hamount
!= 0 || vamount
!= 0) {
1550 wMenuMove(parent
, parent
->frame_x
+ hamount
, parent
->frame_y
+ vamount
, True
);
1552 /* keep scrolling */
1553 menu
->timer
= WMAddTimerHandler(MENU_SCROLL_DELAY
, scrollMenuCallback
, menu
);
1555 /* don't need to scroll anymore */
1560 #define MENU_SCROLL_BORDER 5
1562 static int isPointNearBorder(WMenu
* menu
, int x
, int y
)
1564 int menuX1
= menu
->frame_x
;
1565 int menuY1
= menu
->frame_y
;
1566 int menuX2
= menu
->frame_x
+ MENUW(menu
);
1567 int menuY2
= menu
->frame_y
+ MENUH(menu
);
1569 int head
= wGetHeadForPoint(menu
->frame
->screen_ptr
, wmkpoint(x
, y
));
1570 WMRect rect
= wGetRectForHead(menu
->frame
->screen_ptr
, head
);
1572 /* XXX: handle screen joins properly !! */
1574 if (x
>= menuX1
&& x
<= menuX2
&&
1575 (y
< rect
.pos
.y
+ MENU_SCROLL_BORDER
|| y
>= rect
.pos
.y
+ rect
.size
.height
- MENU_SCROLL_BORDER
))
1577 else if (y
>= menuY1
&& y
<= menuY2
&&
1578 (x
< rect
.pos
.x
+ MENU_SCROLL_BORDER
|| x
>= rect
.pos
.x
+ rect
.size
.width
- MENU_SCROLL_BORDER
))
1584 typedef struct _delay
{
1589 static void callback_leaving(void *user_param
)
1591 _delay
*dl
= (_delay
*) user_param
;
1593 wMenuMove(dl
->menu
, dl
->ox
, dl
->oy
, True
);
1594 dl
->menu
->jump_back
= NULL
;
1595 dl
->menu
->menu
->screen_ptr
->flags
.jump_back_pending
= 0;
1599 void wMenuScroll(WMenu
*menu
)
1602 WMenu
*omenu
= parentMenu(menu
);
1603 WScreen
*scr
= menu
->frame
->screen_ptr
;
1606 int old_frame_x
= omenu
->frame_x
;
1607 int old_frame_y
= omenu
->frame_y
;
1610 if (omenu
->jump_back
)
1611 WMDeleteTimerWithClientData(omenu
->jump_back
);
1613 if (( /*omenu->flags.buttoned && */ !wPreferences
.wrap_menus
)
1614 || omenu
->flags
.app_menu
) {
1618 if (!wPreferences
.wrap_menus
)
1619 raiseMenus(omenu
, True
);
1621 raiseMenus(menu
, False
);
1624 scrollMenuCallback(menu
);
1627 int x
, y
, on_border
, on_x_edge
, on_y_edge
, on_title
;
1630 WMNextEvent(dpy
, &ev
);
1636 x
= (ev
.type
== MotionNotify
) ? ev
.xmotion
.x_root
: ev
.xcrossing
.x_root
;
1637 y
= (ev
.type
== MotionNotify
) ? ev
.xmotion
.y_root
: ev
.xcrossing
.y_root
;
1639 /* on_border is != 0 if the pointer is between the menu
1640 * and the screen border and is close enough to the border */
1641 on_border
= isPointNearBorder(menu
, x
, y
);
1643 smenu
= wMenuUnderPointer(scr
);
1645 if ((smenu
== NULL
&& !on_border
) || (smenu
&& parentMenu(smenu
) != omenu
)) {
1650 rect
= wGetRectForHead(scr
, wGetHeadForPoint(scr
, wmkpoint(x
, y
)));
1651 on_x_edge
= x
<= rect
.pos
.x
+ 1 || x
>= rect
.pos
.x
+ rect
.size
.width
- 2;
1652 on_y_edge
= y
<= rect
.pos
.y
+ 1 || y
>= rect
.pos
.y
+ rect
.size
.height
- 2;
1653 on_border
= on_x_edge
|| on_y_edge
;
1655 if (!on_border
&& !jump_back
) {
1660 if (menu
->timer
&& (smenu
!= menu
|| (!on_y_edge
&& !on_x_edge
))) {
1661 WMDeleteTimerHandler(menu
->timer
);
1669 scrollMenuCallback(menu
);
1672 /* True if we push on title, or drag the omenu to other position */
1673 on_title
= ev
.xbutton
.x_root
>= omenu
->frame_x
&&
1674 ev
.xbutton
.x_root
<= omenu
->frame_x
+ MENUW(omenu
) &&
1675 ev
.xbutton
.y_root
>= omenu
->frame_y
&&
1676 ev
.xbutton
.y_root
<= omenu
->frame_y
+ omenu
->frame
->top_width
;
1678 smenu
= wMenuUnderPointer(scr
);
1679 if (smenu
== NULL
|| (smenu
&& smenu
->flags
.buttoned
&& smenu
!= omenu
))
1681 else if (smenu
== omenu
&& on_title
) {
1696 WMDeleteTimerHandler(menu
->timer
);
1703 if (!omenu
->jump_back
) {
1704 delayer
= wmalloc(sizeof(_delay
));
1705 delayer
->menu
= omenu
;
1706 delayer
->ox
= old_frame_x
;
1707 delayer
->oy
= old_frame_y
;
1708 omenu
->jump_back
= delayer
;
1709 scr
->flags
.jump_back_pending
= 1;
1711 delayer
= omenu
->jump_back
;
1712 WMAddTimerHandler(MENU_JUMP_BACK_DELAY
, callback_leaving
, delayer
);
1716 static void menuExpose(WObjDescriptor
* desc
, XEvent
* event
)
1718 /* Parameter not used, but tell the compiler that it is ok */
1721 wMenuPaint(desc
->parent
);
1725 int *delayed_select
;
1730 static void delaySelection(void *data
)
1732 delay_data
*d
= (delay_data
*) data
;
1738 menu
= findMenu(d
->menu
->menu
->screen_ptr
, &x
, &y
);
1739 if (menu
&& (d
->menu
== menu
|| d
->delayed_select
)) {
1740 entry_no
= getEntryAt(menu
, x
, y
);
1741 selectEntry(menu
, entry_no
);
1743 if (d
->delayed_select
)
1744 *(d
->delayed_select
) = 0;
1747 static void menuMouseDown(WObjDescriptor
* desc
, XEvent
* event
)
1750 XButtonEvent
*bev
= &event
->xbutton
;
1751 WMenu
*menu
= desc
->parent
;
1753 WScreen
*scr
= menu
->frame
->screen_ptr
;
1754 WMenuEntry
*entry
= NULL
;
1756 int close_on_exit
= 0;
1758 int delayed_select
= 0;
1762 int old_frame_x
= 0;
1763 int old_frame_y
= 0;
1764 delay_data d_data
= { NULL
, NULL
, NULL
};
1766 /* Doesn't seem to be needed anymore (if delayed selection handler is
1767 * added only if not present). there seem to be no other side effects
1768 * from removing this and it is also possible that it was only added
1769 * to avoid problems with adding the delayed selection timer handler
1772 /*if (menu->flags.inside_handler) {
1775 menu
->flags
.inside_handler
= 1;
1777 if (!wPreferences
.wrap_menus
) {
1778 smenu
= parentMenu(menu
);
1779 old_frame_x
= smenu
->frame_x
;
1780 old_frame_y
= smenu
->frame_y
;
1781 } else if (event
->xbutton
.window
== menu
->frame
->core
->window
) {
1782 /* This is true if the menu was launched with right click on root window */
1783 if (!d_data
.magic
) {
1785 d_data
.delayed_select
= &delayed_select
;
1787 d_data
.magic
= WMAddTimerHandler(wPreferences
.dblclick_time
, delaySelection
, &d_data
);
1791 wRaiseFrame(menu
->frame
->core
);
1793 close_on_exit
= (bev
->send_event
|| menu
->flags
.brother
);
1795 smenu
= findMenu(scr
, &x
, &y
);
1803 if (menu
->flags
.editing
) {
1806 entry_no
= getEntryAt(menu
, x
, y
);
1807 if ((entry_no
>= 0) && (entry_no
< menu
->entry_no
)) {
1808 entry
= menu
->entries
[entry_no
];
1810 if (!close_on_exit
&& (bev
->state
& ControlMask
) && smenu
&& entry
->flags
.editable
) {
1813 int number
= entry_no
- 3; /* Entries "New", "Destroy Last" and "Last Used" appear before workspaces */
1815 name
= wstrdup(scr
->workspaces
[number
]->name
);
1816 snprintf(buffer
, sizeof(buffer
), _("Type the name for workspace %i:"), number
+ 1);
1818 wMenuUnmap(scr
->root_menu
);
1820 if (wInputDialog(scr
, _("Rename Workspace"), buffer
, &name
))
1821 wWorkspaceRename(scr
, number
, name
);
1827 } else if (bev
->state
& ControlMask
) {
1831 if (entry
->flags
.enabled
&& entry
->cascade
>= 0 && menu
->cascades
) {
1832 WMenu
*submenu
= menu
->cascades
[entry
->cascade
];
1834 if (submenu
->flags
.mapped
&& !submenu
->flags
.buttoned
&& menu
->selected_entry
!= entry_no
) {
1835 wMenuUnmap(submenu
);
1837 if (!submenu
->flags
.mapped
&& !delayed_select
) {
1838 selectEntry(menu
, entry_no
);
1839 } else if (!submenu
->flags
.buttoned
) {
1840 selectEntry(menu
, -1);
1843 } else if (!delayed_select
) {
1844 if (menu
== scr
->switch_menu
&& event
->xbutton
.button
== Button3
) {
1845 selectEntry(menu
, entry_no
);
1846 OpenWindowMenu2((WWindow
*)entry
->clientdata
,
1847 event
->xbutton
.x_root
,
1848 event
->xbutton
.y_root
, False
);
1849 wwin
= (WWindow
*)entry
->clientdata
;
1850 desc
= &wwin
->screen_ptr
->window_menu
->menu
->descriptor
;
1851 event
->xany
.send_event
= True
;
1852 (*desc
->handle_mousedown
)(desc
, event
);
1854 XUngrabPointer(dpy
, CurrentTime
);
1855 selectEntry(menu
, -1);
1858 selectEntry(menu
, entry_no
);
1862 if (!wPreferences
.wrap_menus
&& !wPreferences
.scrollable_menus
) {
1864 dragScrollMenuCallback(menu
);
1868 prevx
= bev
->x_root
;
1869 prevy
= bev
->y_root
;
1873 XAllowEvents(dpy
, AsyncPointer
| SyncPointer
, CurrentTime
);
1875 WMMaskEvent(dpy
, ExposureMask
| ButtonMotionMask
| ButtonReleaseMask
| ButtonPressMask
, &ev
);
1878 smenu
= findMenu(scr
, &x
, &y
);
1880 if (smenu
== NULL
) {
1881 /* moved mouse out of menu */
1883 if (!delayed_select
&& d_data
.magic
) {
1884 WMDeleteTimerHandler(d_data
.magic
);
1885 d_data
.magic
= NULL
;
1888 || (menu
->selected_entry
>= 0
1889 && menu
->entries
[menu
->selected_entry
]->cascade
>= 0)) {
1890 prevx
= ev
.xmotion
.x_root
;
1891 prevy
= ev
.xmotion
.y_root
;
1895 selectEntry(menu
, -1);
1897 prevx
= ev
.xmotion
.x_root
;
1898 prevy
= ev
.xmotion
.y_root
;
1900 } else if (menu
&& menu
!= smenu
1901 && (menu
->selected_entry
< 0
1902 || menu
->entries
[menu
->selected_entry
]->cascade
< 0)) {
1903 selectEntry(menu
, -1);
1905 if (!delayed_select
&& d_data
.magic
) {
1906 WMDeleteTimerHandler(d_data
.magic
);
1907 d_data
.magic
= NULL
;
1911 /* hysteresis for item selection */
1913 /* check if the motion was to the side, indicating that
1914 * the user may want to cross to a submenu */
1915 if (!delayed_select
&& menu
) {
1917 Bool moved_to_submenu
; /* moved to direction of submenu */
1919 dx
= abs(prevx
- ev
.xmotion
.x_root
);
1921 moved_to_submenu
= False
;
1922 if (dx
> 0 /* if moved enough to the side */
1923 /* maybe a open submenu */
1924 && menu
->selected_entry
>= 0
1925 /* moving to the right direction */
1926 && (wPreferences
.align_menus
|| ev
.xmotion
.y_root
>= prevy
)) {
1929 index
= menu
->entries
[menu
->selected_entry
]->cascade
;
1931 if (menu
->cascades
[index
]->frame_x
> menu
->frame_x
) {
1932 if (prevx
< ev
.xmotion
.x_root
)
1933 moved_to_submenu
= True
;
1935 if (prevx
> ev
.xmotion
.x_root
)
1936 moved_to_submenu
= True
;
1941 if (menu
!= smenu
) {
1943 WMDeleteTimerHandler(d_data
.magic
);
1944 d_data
.magic
= NULL
;
1946 } else if (moved_to_submenu
) {
1947 /* while we are moving, postpone the selection */
1949 WMDeleteTimerHandler(d_data
.magic
);
1951 d_data
.delayed_select
= NULL
;
1953 d_data
.magic
= WMAddTimerHandler(MENU_SELECT_DELAY
,
1954 delaySelection
, &d_data
);
1955 prevx
= ev
.xmotion
.x_root
;
1956 prevy
= ev
.xmotion
.y_root
;
1960 WMDeleteTimerHandler(d_data
.magic
);
1961 d_data
.magic
= NULL
;
1966 prevx
= ev
.xmotion
.x_root
;
1967 prevy
= ev
.xmotion
.y_root
;
1968 if (menu
!= smenu
) {
1969 /* pointer crossed menus */
1970 if (menu
&& menu
->timer
) {
1971 WMDeleteTimerHandler(menu
->timer
);
1975 dragScrollMenuCallback(smenu
);
1979 dragScrollMenuCallback(menu
);
1981 if (!delayed_select
) {
1982 entry_no
= getEntryAt(menu
, x
, y
);
1983 if ((entry_no
>= 0) && (entry_no
< menu
->entry_no
)) {
1984 entry
= menu
->entries
[entry_no
];
1985 if (entry
->flags
.enabled
&& entry
->cascade
>= 0 && menu
->cascades
) {
1986 WMenu
*submenu
= menu
->cascades
[entry
->cascade
];
1987 if (submenu
->flags
.mapped
&& !submenu
->flags
.buttoned
1988 && menu
->selected_entry
!= entry_no
) {
1989 wMenuUnmap(submenu
);
1993 selectEntry(menu
, entry_no
);
2001 if (ev
.xbutton
.button
== event
->xbutton
.button
)
2011 if (menu
&& menu
->timer
) {
2012 WMDeleteTimerHandler(menu
->timer
);
2015 if (d_data
.magic
!= NULL
) {
2016 WMDeleteTimerHandler(d_data
.magic
);
2017 d_data
.magic
= NULL
;
2020 if (menu
&& menu
->selected_entry
>= 0) {
2021 entry
= menu
->entries
[menu
->selected_entry
];
2022 if (entry
->callback
!= NULL
&& entry
->flags
.enabled
&& entry
->cascade
< 0) {
2023 /* blink and erase menu selection */
2024 #if (MENU_BLINK_DELAY > 0)
2025 int sel
= menu
->selected_entry
;
2028 for (i
= 0; i
< MENU_BLINK_COUNT
; i
++) {
2029 paintEntry(menu
, sel
, False
);
2031 wusleep(MENU_BLINK_DELAY
);
2032 paintEntry(menu
, sel
, True
);
2034 wusleep(MENU_BLINK_DELAY
);
2037 /* unmap the menu, its parents and call the callback */
2038 if (!menu
->flags
.buttoned
&& (!menu
->flags
.app_menu
|| menu
->parent
!= NULL
)) {
2041 selectEntry(menu
, -1);
2043 (*entry
->callback
) (menu
, entry
);
2045 /* If the user double clicks an entry, the entry will
2046 * be executed twice, which is not good for things like
2047 * the root menu. So, ignore any clicks that were generated
2048 * while the entry was being executed */
2049 while (XCheckTypedWindowEvent(dpy
, menu
->menu
->window
, ButtonPress
, &ev
)) ;
2050 } else if (entry
->callback
!= NULL
&& entry
->cascade
< 0) {
2051 selectEntry(menu
, -1);
2053 if (entry
->cascade
>= 0 && menu
->cascades
&& menu
->cascades
[entry
->cascade
]->flags
.brother
) {
2054 selectEntry(menu
, -1);
2059 if (((WMenu
*) desc
->parent
)->flags
.brother
|| close_on_exit
|| !smenu
)
2060 closeCascade(desc
->parent
);
2062 /* close the cascade windows that should not remain opened */
2063 closeBrotherCascadesOf(desc
->parent
);
2065 if (!wPreferences
.wrap_menus
)
2066 wMenuMove(parentMenu(desc
->parent
), old_frame_x
, old_frame_y
, True
);
2069 /* Just to be sure in case we skip the 2 above because of a goto byebye */
2070 if (menu
&& menu
->timer
) {
2071 WMDeleteTimerHandler(menu
->timer
);
2074 if (d_data
.magic
!= NULL
) {
2075 WMDeleteTimerHandler(d_data
.magic
);
2076 d_data
.magic
= NULL
;
2079 ((WMenu
*) desc
->parent
)->flags
.inside_handler
= 0;
2082 void wMenuMove(WMenu
* menu
, int x
, int y
, int submenus
)
2092 XMoveWindow(dpy
, menu
->frame
->core
->window
, x
, y
);
2094 if (submenus
> 0 && menu
->selected_entry
>= 0) {
2095 i
= menu
->entries
[menu
->selected_entry
]->cascade
;
2097 if (i
>= 0 && menu
->cascades
) {
2098 submenu
= menu
->cascades
[i
];
2099 if (submenu
->flags
.mapped
&& !submenu
->flags
.buttoned
) {
2100 if (wPreferences
.align_menus
) {
2101 wMenuMove(submenu
, x
+ MENUW(menu
), y
, submenus
);
2103 wMenuMove(submenu
, x
+ MENUW(menu
),
2104 y
+ submenu
->entry_height
* menu
->selected_entry
, submenus
);
2109 if (submenus
< 0 && menu
->parent
!= NULL
&& menu
->parent
->flags
.mapped
&& !menu
->parent
->flags
.buttoned
) {
2110 if (wPreferences
.align_menus
) {
2111 wMenuMove(menu
->parent
, x
- MENUW(menu
->parent
), y
, submenus
);
2113 wMenuMove(menu
->parent
, x
- MENUW(menu
->parent
), menu
->frame_y
2114 - menu
->parent
->entry_height
* menu
->parent
->selected_entry
, submenus
);
2119 static void changeMenuLevels(WMenu
* menu
, int lower
)
2124 ChangeStackingLevel(menu
->frame
->core
, (!menu
->parent
? WMMainMenuLevel
: WMSubmenuLevel
));
2125 wRaiseFrame(menu
->frame
->core
);
2126 menu
->flags
.lowered
= 0;
2128 ChangeStackingLevel(menu
->frame
->core
, WMNormalLevel
);
2129 wLowerFrame(menu
->frame
->core
);
2130 menu
->flags
.lowered
= 1;
2132 for (i
= 0; i
< menu
->cascade_no
; i
++) {
2133 if (menu
->cascades
[i
]
2134 && !menu
->cascades
[i
]->flags
.buttoned
&& menu
->cascades
[i
]->flags
.lowered
!= lower
) {
2135 changeMenuLevels(menu
->cascades
[i
], lower
);
2140 static void menuTitleDoubleClick(WCoreWindow
* sender
, void *data
, XEvent
* event
)
2145 /* Parameter not used, but tell the compiler that it is ok */
2148 if (event
->xbutton
.state
& MOD_MASK
) {
2149 if (menu
->flags
.lowered
) {
2154 changeMenuLevels(menu
, lower
);
2156 if (menu
->flags
.shaded
) {
2157 wFrameWindowResize(menu
->frame
, menu
->frame
->core
->width
, menu
->frame
->top_width
+
2158 menu
->entry_height
*menu
->entry_no
+ menu
->frame
->bottom_width
- 1);
2159 menu
->flags
.shaded
= 0;
2161 wFrameWindowResize(menu
->frame
, menu
->frame
->core
->width
, menu
->frame
->top_width
- 1);
2162 menu
->flags
.shaded
= 1;
2167 static void menuTitleMouseDown(WCoreWindow
* sender
, void *data
, XEvent
* event
)
2172 int x
= menu
->frame_x
, y
= menu
->frame_y
;
2173 int dx
= event
->xbutton
.x_root
, dy
= event
->xbutton
.y_root
;
2177 /* Parameter not used, but tell the compiler that it is ok */
2180 /* can't touch the menu copy */
2181 if (menu
->flags
.brother
)
2184 if (event
->xbutton
.button
!= Button1
&& event
->xbutton
.button
!= Button2
)
2187 if (event
->xbutton
.state
& MOD_MASK
) {
2188 wLowerFrame(menu
->frame
->core
);
2191 wRaiseFrame(menu
->frame
->core
);
2196 /* lower/raise all submenus */
2198 if (tmp
->selected_entry
>= 0 && tmp
->cascades
&& tmp
->entries
[tmp
->selected_entry
]->cascade
>= 0) {
2199 tmp
= tmp
->cascades
[tmp
->entries
[tmp
->selected_entry
]->cascade
];
2200 if (!tmp
|| !tmp
->flags
.mapped
)
2203 wLowerFrame(tmp
->frame
->core
);
2205 wRaiseFrame(tmp
->frame
->core
);
2211 /* tear off the menu if it's a root menu or a cascade
2213 if (!menu
->flags
.buttoned
&& !menu
->flags
.brother
&& (!menu
->flags
.app_menu
|| menu
->parent
!= NULL
)) {
2214 menu
->flags
.buttoned
= 1;
2215 wFrameWindowShowButton(menu
->frame
, WFF_RIGHT_BUTTON
);
2217 /* turn off selected menu entry in parent menu */
2218 selectEntry(menu
->parent
, -1);
2220 /* make parent map the copy in place of the original */
2221 for (i
= 0; i
< menu
->parent
->cascade_no
; i
++) {
2222 if (menu
->parent
->cascades
[i
] == menu
) {
2223 menu
->parent
->cascades
[i
] = menu
->brother
;
2232 WMMaskEvent(dpy
, ButtonMotionMask
| ButtonReleaseMask
| ButtonPressMask
| ExposureMask
, &ev
);
2236 x
+= ev
.xmotion
.x_root
- dx
;
2237 y
+= ev
.xmotion
.y_root
- dy
;
2238 dx
= ev
.xmotion
.x_root
;
2239 dy
= ev
.xmotion
.y_root
;
2240 wMenuMove(menu
, x
, y
, True
);
2242 if (abs(ev
.xmotion
.x_root
- dx
) > MOVE_THRESHOLD
2243 || abs(ev
.xmotion
.y_root
- dy
) > MOVE_THRESHOLD
) {
2245 XGrabPointer(dpy
, menu
->frame
->titlebar
->window
, False
,
2246 ButtonMotionMask
| ButtonReleaseMask
2248 GrabModeAsync
, GrabModeAsync
, None
,
2249 wPreferences
.cursor
[WCUR_MOVE
], CurrentTime
);
2258 if (ev
.xbutton
.button
!= event
->xbutton
.button
)
2260 XUngrabPointer(dpy
, CurrentTime
);
2271 *----------------------------------------------------------------------
2273 * Handles mouse click on the close button of menus. The menu is
2274 * closed when the button is clicked.
2277 * The closed menu is reinserted at its parent menus
2279 *----------------------------------------------------------------------
2281 static void menuCloseClick(WCoreWindow
* sender
, void *data
, XEvent
* event
)
2283 WMenu
*menu
= (WMenu
*) data
;
2284 WMenu
*parent
= menu
->parent
;
2287 /* Parameter not used, but tell the compiler that it is ok */
2292 for (i
= 0; i
< parent
->cascade_no
; i
++) {
2293 /* find the entry that points to the copy */
2294 if (parent
->cascades
[i
] == menu
->brother
) {
2295 /* make it point to the original */
2296 parent
->cascades
[i
] = menu
;
2297 menu
->parent
= parent
;
2305 static void saveMenuInfo(WMPropList
* dict
, WMenu
* menu
, WMPropList
* key
)
2307 WMPropList
*value
, *list
;
2310 snprintf(buffer
, sizeof(buffer
), "%i,%i", menu
->frame_x
, menu
->frame_y
);
2311 value
= WMCreatePLString(buffer
);
2312 list
= WMCreatePLArray(value
, NULL
);
2313 if (menu
->flags
.lowered
)
2314 WMAddToPLArray(list
, WMCreatePLString("lowered"));
2315 WMPutInPLDictionary(dict
, key
, list
);
2316 WMReleasePropList(value
);
2317 WMReleasePropList(list
);
2320 void wMenuSaveState(WScreen
* scr
)
2322 WMPropList
*menus
, *key
;
2325 menus
= WMCreatePLDictionary(NULL
, NULL
);
2327 if (scr
->switch_menu
&& scr
->switch_menu
->flags
.buttoned
) {
2328 key
= WMCreatePLString("SwitchMenu");
2329 saveMenuInfo(menus
, scr
->switch_menu
, key
);
2330 WMReleasePropList(key
);
2334 if (saveMenuRecurs(menus
, scr
, scr
->root_menu
))
2337 if (scr
->workspace_menu
&& scr
->workspace_menu
->flags
.buttoned
) {
2338 key
= WMCreatePLString("WorkspaceMenu");
2339 saveMenuInfo(menus
, scr
->workspace_menu
, key
);
2340 WMReleasePropList(key
);
2345 key
= WMCreatePLString("Menus");
2346 WMPutInPLDictionary(scr
->session_state
, key
, menus
);
2347 WMReleasePropList(key
);
2349 WMReleasePropList(menus
);
2352 static Bool
getMenuPath(WMenu
* menu
, char *buffer
, int bufSize
)
2357 if (!menu
->flags
.titled
|| !menu
->frame
->title
[0])
2360 len
= strlen(menu
->frame
->title
);
2365 ok
= getMenuPath(menu
->parent
, buffer
, bufSize
- len
- 1);
2370 strcat(buffer
, "\\");
2371 strcat(buffer
, menu
->frame
->title
);
2376 static Bool
saveMenuRecurs(WMPropList
* menus
, WScreen
* scr
, WMenu
* menu
)
2379 int save_menus
= 0, i
;
2383 if (menu
->flags
.brother
)
2384 menu
= menu
->brother
;
2386 if (menu
->flags
.buttoned
&& menu
!= scr
->switch_menu
) {
2389 ok
= getMenuPath(menu
, buffer
, 510);
2392 key
= WMCreatePLString(buffer
);
2393 saveMenuInfo(menus
, menu
, key
);
2394 WMReleasePropList(key
);
2400 for (i
= 0; i
< menu
->cascade_no
; i
++) {
2401 if (saveMenuRecurs(menus
, scr
, menu
->cascades
[i
]))
2408 #define COMPLAIN(key) wwarning(_("bad value in menus state info: %s"), key)
2410 static Bool
getMenuInfo(WMPropList
* info
, int *x
, int *y
, Bool
* lowered
)
2416 if (WMIsPLArray(info
)) {
2418 pos
= WMGetFromPLArray(info
, 0);
2419 flags
= WMGetFromPLArray(info
, 1);
2420 if (flags
!= NULL
&& WMIsPLString(flags
) && WMGetFromPLString(flags
) != NULL
2421 && strcmp(WMGetFromPLString(flags
), "lowered") == 0) {
2428 if (pos
!= NULL
&& WMIsPLString(pos
)) {
2429 if (sscanf(WMGetFromPLString(pos
), "%i,%i", x
, y
) != 2)
2430 COMPLAIN("Position");
2432 COMPLAIN("(position, flags...)");
2439 static int restoreMenu(WScreen
*scr
, WMPropList
*menu
)
2442 Bool lowered
= False
;
2443 WMenu
*pmenu
= NULL
;
2448 if (!getMenuInfo(menu
, &x
, &y
, &lowered
))
2451 OpenSwitchMenu(scr
, x
, y
, False
);
2452 pmenu
= scr
->switch_menu
;
2455 int width
= MENUW(pmenu
);
2456 int height
= MENUH(pmenu
);
2457 WMRect rect
= wGetRectForHead(scr
, wGetHeadForPointerLocation(scr
));
2460 changeMenuLevels(pmenu
, True
);
2463 if (x
< rect
.pos
.x
- width
)
2465 if (x
> rect
.pos
.x
+ rect
.size
.width
)
2466 x
= rect
.pos
.x
+ rect
.size
.width
- width
;
2469 if (y
> rect
.pos
.y
+ rect
.size
.height
)
2470 y
= rect
.pos
.y
+ rect
.size
.height
- height
;
2472 wMenuMove(pmenu
, x
, y
, True
);
2473 pmenu
->flags
.buttoned
= 1;
2474 wFrameWindowShowButton(pmenu
->frame
, WFF_RIGHT_BUTTON
);
2480 static int restoreMenuRecurs(WScreen
*scr
, WMPropList
*menus
, WMenu
*menu
, const char *path
)
2482 WMPropList
*key
, *entry
;
2487 if (strlen(path
) + strlen(menu
->frame
->title
) > 510)
2490 snprintf(buffer
, sizeof(buffer
), "%s\\%s", path
, menu
->frame
->title
);
2491 key
= WMCreatePLString(buffer
);
2492 entry
= WMGetFromPLDictionary(menus
, key
);
2495 if (entry
&& getMenuInfo(entry
, &x
, &y
, &lowered
)) {
2497 if (!menu
->flags
.mapped
) {
2498 int width
= MENUW(menu
);
2499 int height
= MENUH(menu
);
2500 WMRect rect
= wGetRectForHead(scr
, wGetHeadForPointerLocation(scr
));
2502 wMenuMapAt(menu
, x
, y
, False
);
2505 /* make parent map the copy in place of the original */
2506 for (i
= 0; i
< menu
->parent
->cascade_no
; i
++) {
2507 if (menu
->parent
->cascades
[i
] == menu
) {
2508 menu
->parent
->cascades
[i
] = menu
->brother
;
2514 changeMenuLevels(menu
, True
);
2517 if (x
< rect
.pos
.x
- width
)
2519 if (x
> rect
.pos
.x
+ rect
.size
.width
)
2520 x
= rect
.pos
.x
+ rect
.size
.width
- width
;
2523 if (y
> rect
.pos
.y
+ rect
.size
.height
)
2524 y
= rect
.pos
.y
+ rect
.size
.height
- height
;
2526 wMenuMove(menu
, x
, y
, True
);
2527 menu
->flags
.buttoned
= 1;
2528 wFrameWindowShowButton(menu
->frame
, WFF_RIGHT_BUTTON
);
2533 WMReleasePropList(key
);
2535 for (i
= 0; i
< menu
->cascade_no
; i
++) {
2536 if (restoreMenuRecurs(scr
, menus
, menu
->cascades
[i
], buffer
) != False
)
2543 void wMenuRestoreState(WScreen
* scr
)
2545 WMPropList
*menus
, *menu
, *key
, *skey
;
2547 if (!scr
->session_state
) {
2551 key
= WMCreatePLString("Menus");
2552 menus
= WMGetFromPLDictionary(scr
->session_state
, key
);
2553 WMReleasePropList(key
);
2560 skey
= WMCreatePLString("SwitchMenu");
2561 menu
= WMGetFromPLDictionary(menus
, skey
);
2562 WMReleasePropList(skey
);
2563 restoreMenu(scr
, menu
);
2565 if (!scr
->root_menu
) {
2566 OpenRootMenu(scr
, scr
->scr_width
* 2, 0, False
);
2567 wMenuUnmap(scr
->root_menu
);
2569 restoreMenuRecurs(scr
, menus
, scr
->root_menu
, "");