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
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
27 #include <X11/Xutil.h>
28 #include <X11/keysym.h>
38 #include "WindowMaker.h"
47 #include "workspace.h"
49 /****** Global Variables ******/
51 extern Cursor wCursor
[WCUR_LAST
];
53 extern XContext wWinContext
;
55 extern WPreferences wPreferences
;
57 #define MOD_MASK wPreferences.modifier_mask
59 #define MENU_SCROLL_STEP menuScrollParameters[(int)wPreferences.menu_scroll_speed].steps
60 #define MENU_SCROLL_DELAY menuScrollParameters[(int)wPreferences.menu_scroll_speed].delay
62 #define MENUW(m) ((m)->frame->core->width+2*FRAME_BORDER_WIDTH)
63 #define MENUH(m) ((m)->frame->core->height+2*FRAME_BORDER_WIDTH)
65 /***** Local Stuff ******/
67 #define WSS_ROOTMENU (1<<0)
68 #define WSS_SWITCHMENU (1<<1)
69 #define WSS_WSMENU (1<<2)
74 } menuScrollParameters
[5] = {
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
}};
82 static void menuMouseDown(WObjDescriptor
* desc
, XEvent
* event
);
83 static void menuExpose(WObjDescriptor
* desc
, XEvent
* event
);
85 static void menuTitleDoubleClick(WCoreWindow
* sender
, void *data
, XEvent
* event
);
86 static void menuTitleMouseDown(WCoreWindow
* sender
, void *data
, XEvent
* event
);
88 static void menuCloseClick(WCoreWindow
* sender
, void *data
, XEvent
* event
);
90 static void updateTexture(WMenu
* menu
);
93 static int saveMenuRecurs(WMPropList
* menus
, WScreen
* scr
, WMenu
* menu
);
94 static int restoreMenuRecurs(WScreen
* scr
, WMPropList
* menus
, WMenu
* menu
, char *path
);
97 static void selectEntry(WMenu
* menu
, int entry_no
);
98 static void closeCascade(WMenu
* menu
);
100 /****** Notification Observers ******/
102 static void appearanceObserver(void *self
, WMNotification
* notif
)
104 WMenu
*menu
= (WMenu
*) self
;
105 int flags
= (int)(uintptr_t) WMGetNotificationClientData(notif
);
107 if (!menu
->flags
.realized
)
110 if (WMGetNotificationName(notif
) == WNMenuAppearanceSettingsChanged
) {
111 if (flags
& WFontSettings
) {
112 menu
->flags
.realized
= 0;
115 if (flags
& WTextureSettings
) {
116 if (!menu
->flags
.brother
)
119 if (flags
& (WTextureSettings
| WColorSettings
)) {
122 } else if (menu
->flags
.titled
) {
124 if (flags
& WFontSettings
) {
125 menu
->flags
.realized
= 0;
128 if (flags
& WTextureSettings
) {
129 menu
->frame
->flags
.need_texture_remake
= 1;
131 if (flags
& (WColorSettings
| WTextureSettings
)) {
132 wFrameWindowPaint(menu
->frame
);
137 /************************************/
140 *----------------------------------------------------------------------
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.
149 *----------------------------------------------------------------------
151 WMenu
*wMenuCreate(WScreen
* screen
, char *title
, int main_menu
)
154 static int brother
= 0;
157 menu
= wmalloc(sizeof(WMenu
));
159 memset(menu
, 0, sizeof(WMenu
));
161 #ifdef SINGLE_MENULEVEL
162 tmp
= WMSubmenuLevel
;
164 tmp
= (main_menu
? WMMainMenuLevel
: WMSubmenuLevel
);
167 flags
= WFF_SINGLE_STATE
| WFF_BORDER
;
169 flags
|= WFF_TITLEBAR
| WFF_RIGHT_BUTTON
;
170 menu
->flags
.titled
= 1;
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
);
177 menu
->frame
->core
->descriptor
.parent
= menu
;
178 menu
->frame
->core
->descriptor
.parent_type
= WCLASS_MENU
;
179 menu
->frame
->core
->descriptor
.handle_mousedown
= menuMouseDown
;
181 wFrameWindowHideButton(menu
->frame
, WFF_RIGHT_BUTTON
);
184 menu
->frame
->title
= wstrdup(title
);
187 menu
->frame
->flags
.justification
= WTJ_LEFT
;
189 menu
->frame
->rbutton_image
= screen
->b_pixmaps
[WBUT_CLOSE
];
192 menu
->alloced_entries
= 0;
193 menu
->selected_entry
= -1;
194 menu
->entries
= NULL
;
196 menu
->frame_x
= screen
->app_menu_x
;
197 menu
->frame_y
= screen
->app_menu_y
;
199 menu
->frame
->child
= menu
;
201 menu
->flags
.lowered
= 0;
205 /* setup object descriptors */
206 menu
->frame
->on_mousedown_titlebar
= menuTitleMouseDown
;
207 menu
->frame
->on_dblclick_titlebar
= menuTitleDoubleClick
;
210 menu
->frame
->on_click_right
= menuCloseClick
;
212 menu
->menu
= wCoreCreate(menu
->frame
->core
, 0, menu
->frame
->top_width
, menu
->frame
->core
->width
, 10);
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
;
219 menu
->menu_texture_data
= None
;
221 XMapWindow(dpy
, menu
->menu
->window
);
227 menu
->brother
= wMenuCreate(screen
, title
, main_menu
);
229 menu
->brother
->flags
.brother
= 1;
230 menu
->brother
->brother
= menu
;
232 WMAddNotificationObserver(appearanceObserver
, menu
, WNMenuAppearanceSettingsChanged
, menu
);
234 WMAddNotificationObserver(appearanceObserver
, menu
, WNMenuTitleAppearanceSettingsChanged
, menu
);
239 WMenu
*wMenuCreateForApp(WScreen
* screen
, char *title
, int main_menu
)
243 menu
= wMenuCreate(screen
, title
, main_menu
);
246 menu
->flags
.app_menu
= 1;
247 menu
->brother
->flags
.app_menu
= 1;
252 static void insertEntry(WMenu
* menu
, WMenuEntry
* entry
, int index
)
256 for (i
= menu
->entry_no
- 1; i
>= index
; i
--) {
257 menu
->entries
[i
]->order
++;
258 menu
->entries
[i
+ 1] = menu
->entries
[i
];
260 menu
->entries
[index
] = entry
;
263 WMenuEntry
*wMenuInsertCallback(WMenu
* menu
, int index
, char *text
,
264 void (*callback
) (WMenu
* menu
, WMenuEntry
* entry
), void *clientdata
)
268 menu
->flags
.realized
= 0;
269 menu
->brother
->flags
.realized
= 0;
271 /* reallocate array if it's too small */
272 if (menu
->entry_no
>= menu
->alloced_entries
) {
275 tmp
= wrealloc(menu
->entries
, sizeof(WMenuEntry
) * (menu
->alloced_entries
+ 5));
278 menu
->alloced_entries
+= 5;
280 menu
->brother
->entries
= tmp
;
281 menu
->brother
->alloced_entries
= menu
->alloced_entries
;
283 entry
= wmalloc(sizeof(WMenuEntry
));
284 memset(entry
, 0, sizeof(WMenuEntry
));
285 entry
->flags
.enabled
= 1;
286 entry
->text
= wstrdup(text
);
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
;
294 entry
->order
= index
;
295 insertEntry(menu
, entry
, index
);
299 menu
->brother
->entry_no
= menu
->entry_no
;
304 void wMenuEntrySetCascade(WMenu
* menu
, WMenuEntry
* entry
, WMenu
* cascade
)
306 WMenu
*brother
= menu
->brother
;
309 assert(menu
->flags
.brother
== 0);
311 if (entry
->cascade
>= 0) {
312 menu
->flags
.realized
= 0;
313 brother
->flags
.realized
= 0;
316 cascade
->parent
= menu
;
318 cascade
->brother
->parent
= brother
;
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
;
331 entry
->cascade
= menu
->cascade_no
;
333 menu
->cascades
= wrealloc(menu
->cascades
, sizeof(WMenu
) * (menu
->cascade_no
+ 1));
334 menu
->cascades
[menu
->cascade_no
++] = cascade
;
336 brother
->cascades
= wrealloc(brother
->cascades
, sizeof(WMenu
) * (brother
->cascade_no
+ 1));
337 brother
->cascades
[brother
->cascade_no
++] = cascade
->brother
;
340 if (menu
->flags
.lowered
) {
342 cascade
->flags
.lowered
= 1;
343 ChangeStackingLevel(cascade
->frame
->core
, WMNormalLevel
);
345 cascade
->brother
->flags
.lowered
= 1;
346 ChangeStackingLevel(cascade
->brother
->frame
->core
, WMNormalLevel
);
349 if (!menu
->flags
.realized
)
353 void wMenuEntryRemoveCascade(WMenu
* menu
, WMenuEntry
* entry
)
355 assert(menu
->flags
.brother
== 0);
357 /* destroy cascade menu */
358 if (entry
->cascade
>= 0 && menu
->cascades
&& menu
->cascades
[entry
->cascade
] != NULL
) {
360 wMenuDestroy(menu
->cascades
[entry
->cascade
], True
);
362 menu
->cascades
[entry
->cascade
] = NULL
;
363 menu
->brother
->cascades
[entry
->cascade
] = NULL
;
369 void wMenuRemoveItem(WMenu
* menu
, int index
)
373 if (menu
->flags
.brother
) {
374 wMenuRemoveItem(menu
->brother
, index
);
378 if (index
>= menu
->entry_no
)
381 /* destroy cascade menu */
382 wMenuEntryRemoveCascade(menu
, menu
->entries
[index
]);
384 /* destroy unshared data */
386 if (menu
->entries
[index
]->text
)
387 wfree(menu
->entries
[index
]->text
);
389 if (menu
->entries
[index
]->rtext
)
390 wfree(menu
->entries
[index
]->rtext
);
392 if (menu
->entries
[index
]->free_cdata
&& menu
->entries
[index
]->clientdata
)
393 (*menu
->entries
[index
]->free_cdata
) (menu
->entries
[index
]->clientdata
);
395 wfree(menu
->entries
[index
]);
397 for (i
= index
; i
< menu
->entry_no
- 1; i
++) {
398 menu
->entries
[i
+ 1]->order
--;
399 menu
->entries
[i
] = menu
->entries
[i
+ 1];
402 menu
->brother
->entry_no
--;
405 static Pixmap
renderTexture(WMenu
* menu
)
413 WScreen
*scr
= menu
->menu
->screen_ptr
;
414 WTexture
*texture
= scr
->menu_item_texture
;
416 if (wPreferences
.menu_style
== MS_NORMAL
) {
417 img
= wTextureRenderImage(texture
, menu
->menu
->width
, menu
->entry_height
, WREL_MENUENTRY
);
419 img
= wTextureRenderImage(texture
, menu
->menu
->width
, menu
->menu
->height
+ 1, WREL_MENUENTRY
);
422 wwarning(_("could not render texture: %s"), RMessageForError(RErrorCode
));
427 if (wPreferences
.menu_style
== MS_SINGLE_TEXTURE
) {
429 light
.red
= light
.green
= light
.blue
= 80;
432 dark
.red
= dark
.green
= dark
.blue
= 0;
435 mid
.red
= mid
.green
= mid
.blue
= 40;
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
);
441 RDrawLine(img
, 0, i
* menu
->entry_height
- 1,
442 menu
->menu
->width
- 1, i
* menu
->entry_height
- 1, &dark
);
444 ROperateLine(img
, RAddOperation
, 0, i
* menu
->entry_height
,
445 menu
->menu
->width
- 1, i
* menu
->entry_height
, &light
);
448 if (!RConvertImage(scr
->rcontext
, img
, &pix
)) {
449 wwarning(_("error rendering image:%s"), RMessageForError(RErrorCode
));
456 static void updateTexture(WMenu
* menu
)
458 WScreen
*scr
= menu
->menu
->screen_ptr
;
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
);
465 menu
->menu_texture_data
= renderTexture(menu
);
467 XSetWindowBackgroundPixmap(dpy
, menu
->menu
->window
, menu
->menu_texture_data
);
468 XClearWindow(dpy
, menu
->menu
->window
);
470 XSetWindowBackgroundPixmap(dpy
, menu
->brother
->menu
->window
, menu
->menu_texture_data
);
471 XClearWindow(dpy
, menu
->brother
->menu
->window
);
474 XSetWindowBackground(dpy
, menu
->menu
->window
, scr
->menu_item_texture
->any
.color
.pixel
);
475 XClearWindow(dpy
, menu
->menu
->window
);
479 void wMenuRealize(WMenu
* menu
)
482 int width
, rwidth
, mrwidth
, mwidth
;
483 int theight
, twidth
, eheight
;
484 WScreen
*scr
= menu
->frame
->screen_ptr
;
485 static int brother_done
= 0;
490 wMenuRealize(menu
->brother
);
494 flags
= WFF_SINGLE_STATE
| WFF_BORDER
;
495 if (menu
->flags
.titled
)
496 flags
|= WFF_TITLEBAR
| WFF_RIGHT_BUTTON
;
498 wFrameWindowUpdateBorders(menu
->frame
, flags
);
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);
508 eheight
= WMFontHeight(scr
->menu_entry_font
) + 6 + wPreferences
.menu_text_clearance
* 2;
509 menu
->entry_height
= eheight
;
512 for (i
= 0; i
< menu
->entry_no
; i
++) {
515 /* search widest text */
516 text
= menu
->entries
[i
]->text
;
517 width
= WMWidthOfString(scr
->menu_entry_font
, text
, strlen(text
)) + 10;
519 if (menu
->entries
[i
]->flags
.indicator
) {
520 width
+= MENU_INDICATOR_SPACE
;
526 /* search widest text on right */
527 text
= menu
->entries
[i
]->rtext
;
529 rwidth
= WMWidthOfString(scr
->menu_entry_font
, text
, strlen(text
))
531 else if (menu
->entries
[i
]->cascade
>= 0)
536 if (rwidth
> mrwidth
)
544 wCoreConfigure(menu
->menu
, 0, theight
, mwidth
, menu
->entry_no
* eheight
- 1);
546 wFrameWindowResize(menu
->frame
, mwidth
, menu
->entry_no
* eheight
- 1
547 + menu
->frame
->top_width
+ menu
->frame
->bottom_width
);
551 menu
->flags
.realized
= 1;
553 if (menu
->flags
.mapped
)
555 if (menu
->brother
->flags
.mapped
)
556 wMenuPaint(menu
->brother
);
559 void wMenuDestroy(WMenu
* menu
, int recurse
)
563 WMRemoveNotificationObserver(menu
);
565 /* remove any pending timers */
567 WMDeleteTimerHandler(menu
->timer
);
570 /* call destroy handler */
571 if (menu
->on_destroy
)
572 (*menu
->on_destroy
) (menu
);
574 /* Destroy items if this menu own them. If this is the "brother" menu,
575 * leave them alone as it is shared by them.
577 if (!menu
->flags
.brother
) {
578 for (i
= 0; i
< menu
->entry_no
; i
++) {
580 wfree(menu
->entries
[i
]->text
);
582 if (menu
->entries
[i
]->rtext
)
583 wfree(menu
->entries
[i
]->rtext
);
586 if (menu
->entries
[i
]->instances
) {
587 WMReleasePropList(menu
->entries
[i
]->instances
);
589 #endif /* USER_MENU */
591 if (menu
->entries
[i
]->free_cdata
&& menu
->entries
[i
]->clientdata
) {
592 (*menu
->entries
[i
]->free_cdata
) (menu
->entries
[i
]->clientdata
);
594 wfree(menu
->entries
[i
]);
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
);
603 wMenuDestroy(menu
->cascades
[i
], recurse
);
609 wfree(menu
->entries
);
613 FREE_PIXMAP(menu
->menu_texture_data
);
616 wfree(menu
->cascades
);
618 wCoreDestroy(menu
->menu
);
619 wFrameWindowDestroy(menu
->frame
);
621 /* destroy copy of this menu */
622 if (!menu
->flags
.brother
&& menu
->brother
)
623 wMenuDestroy(menu
->brother
, False
);
633 static void drawFrame(WScreen
* scr
, Drawable win
, int y
, int w
, int h
, int type
)
639 segs
[i
].x1
= segs
[i
].x2
= w
- 1;
641 segs
[i
].y2
= y
+ h
- 1;
643 if (type
!= F_TOP
&& type
!= F_NONE
) {
645 segs
[i
].y1
= segs
[i
].y2
= y
+ h
- 2;
649 XDrawSegments(dpy
, win
, scr
->menu_item_auxtexture
->dim_gc
, segs
, i
);
655 segs
[i
].y2
= y
+ h
- 1;
657 if (type
!= F_BOTTOM
&& type
!= F_NONE
) {
664 XDrawSegments(dpy
, win
, scr
->menu_item_auxtexture
->light_gc
, segs
, i
);
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);
670 static void paintEntry(WMenu
* menu
, int index
, int selected
)
672 WScreen
*scr
= menu
->frame
->screen_ptr
;
673 Window win
= menu
->menu
->window
;
674 WMenuEntry
*entry
= menu
->entries
[index
];
680 if (!menu
->flags
.realized
)
682 h
= menu
->entry_height
;
683 w
= menu
->menu
->width
;
686 light
= scr
->menu_item_auxtexture
->light_gc
;
687 dim
= scr
->menu_item_auxtexture
->dim_gc
;
688 dark
= scr
->menu_item_auxtexture
->dark_gc
;
690 if (wPreferences
.menu_style
== MS_FLAT
&& menu
->entry_no
> 1) {
693 else if (index
== menu
->entry_no
- 1)
701 /* paint background */
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
);
707 if (scr
->menu_item_texture
->any
.type
== WTEX_SOLID
) {
708 XClearArea(dpy
, win
, 0, y
+ 1, w
- 1, h
- 3, False
);
710 drawFrame(scr
, win
, y
, w
, h
, type
);
712 XClearArea(dpy
, win
, 0, y
, w
, h
, False
);
717 if (entry
->flags
.enabled
)
718 color
= scr
->select_text_color
;
720 color
= scr
->dtext_color
;
721 } else if (!entry
->flags
.enabled
) {
722 color
= scr
->dtext_color
;
724 color
= scr
->mtext_color
;
728 if (entry
->flags
.indicator
)
729 x
+= MENU_INDICATOR_SPACE
+ 2;
731 WMDrawString(scr
->wmscreen
, win
, color
, scr
->menu_entry_font
,
732 x
, 3 + y
+ wPreferences
.menu_text_clearance
, entry
->text
, strlen(entry
->text
));
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);
742 if (entry
->flags
.indicator
&& entry
->flags
.indicator_on
) {
746 switch (entry
->flags
.indicator_type
) {
748 indicator
= scr
->menu_check_indicator
;
751 indicator
= scr
->menu_mini_indicator
;
754 indicator
= scr
->menu_hide_indicator
;
757 indicator
= scr
->menu_shade_indicator
;
761 indicator
= scr
->menu_radio_indicator
;
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);
770 if (entry
->flags
.enabled
) {
771 XSetForeground(dpy
, scr
->copy_gc
, WMColorPixel(scr
->select_text_color
));
773 XSetForeground(dpy
, scr
->copy_gc
, WMColorPixel(scr
->dtext_color
));
776 if (entry
->flags
.enabled
) {
777 XSetForeground(dpy
, scr
->copy_gc
, WMColorPixel(scr
->mtext_color
));
779 XSetForeground(dpy
, scr
->copy_gc
, WMColorPixel(scr
->dtext_color
));
782 XFillRectangle(dpy
, win
, scr
->copy_gc
, 5, y
+ (h
- ih
) / 2, iw
, ih
);
784 XCopyArea(dpy, indicator->image, win, scr->copy_gc, 0, 0,
785 iw, ih, 5, y+(h-ih)/2);
787 XSetClipOrigin(dpy
, scr
->copy_gc
, 0, 0);
790 /* draw right text */
792 if (entry
->rtext
&& entry
->cascade
< 0) {
793 tw
= WMWidthOfString(scr
->menu_entry_font
, entry
->rtext
, strlen(entry
->rtext
));
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
));
800 static void move_menus(WMenu
* menu
, int x
, int y
)
802 while (menu
->parent
) {
805 if (!wPreferences
.align_menus
&& menu
->selected_entry
>= 0) {
806 y
-= menu
->selected_entry
* menu
->entry_height
;
809 wMenuMove(menu
, x
, y
, True
);
812 static void makeVisible(WMenu
* menu
)
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
));
818 if (menu
->entry_no
< 0)
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
;
830 if (x1
< rect
.pos
.x
) {
833 } else if (x2
>= rect
.pos
.x
+ rect
.size
.width
) {
834 new_x
= rect
.pos
.x
+ rect
.size
.width
- MENUW(menu
) - 1;
838 if (y1
< rect
.pos
.y
) {
841 } else if (y2
>= rect
.pos
.y
+ rect
.size
.height
) {
842 new_y
= rect
.pos
.y
+ rect
.size
.height
- menu
->entry_height
- 1;
846 new_y
= new_y
- menu
->frame
->top_width
- menu
->selected_entry
* menu
->entry_height
;
847 move_menus(menu
, new_x
, new_y
);
850 static int check_key(WMenu
* menu
, XKeyEvent
* event
)
855 if (XLookupString(event
, buffer
, 32, NULL
, NULL
) < 1)
858 ch
= toupper(buffer
[0]);
860 s
= (menu
->selected_entry
>= 0 ? menu
->selected_entry
+ 1 : 0);
863 for (i
= s
; i
< menu
->entry_no
; i
++) {
864 if (ch
== toupper(menu
->entries
[i
]->text
[0])) {
868 /* no match. Retry from start, if previous started from a selected entry */
876 static int keyboardMenu(WMenu
* menu
)
879 KeySym ksym
= NoSymbol
;
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
));
889 if (menu
->flags
.editing
)
892 XGrabKeyboard(dpy
, menu
->frame
->core
->window
, True
, GrabModeAsync
, GrabModeAsync
, CurrentTime
);
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
;
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;
900 move_menus(menu
, new_x
, new_y
);
902 while (!done
&& menu
->flags
.mapped
) {
903 XAllowEvents(dpy
, AsyncKeyboard
, CurrentTime
);
904 WMMaskEvent(dpy
, ExposureMask
| ButtonMotionMask
| ButtonPressMask
905 | ButtonReleaseMask
| KeyPressMask
| KeyReleaseMask
| SubstructureNotifyMask
, &event
);
907 switch (event
.type
) {
909 ksym
= XLookupKeysym(&event
.xkey
, 0);
919 selectEntry(menu
, 0);
927 selectEntry(menu
, menu
->entry_no
- 1);
938 if (menu
->selected_entry
<= 0)
939 selectEntry(menu
, menu
->entry_no
- 1);
941 selectEntry(menu
, menu
->selected_entry
- 1);
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);
968 if (menu
->selected_entry
>= 0) {
970 entry
= menu
->entries
[menu
->selected_entry
];
972 if (entry
->cascade
>= 0 && menu
->cascades
973 && menu
->cascades
[entry
->cascade
]->entry_no
> 0) {
975 XUngrabKeyboard(dpy
, CurrentTime
);
977 selectEntry(menu
->cascades
[entry
->cascade
], 0);
978 if (!keyboardMenu(menu
->cascades
[entry
->cascade
]))
981 XGrabKeyboard(dpy
, menu
->frame
->core
->window
, True
,
982 GrabModeAsync
, GrabModeAsync
, CurrentTime
);
994 if (menu
->parent
!= NULL
&& menu
->parent
->selected_entry
>= 0) {
995 selectEntry(menu
, -1);
996 move_menus(menu
, old_pos_x
, old_pos_y
);
1009 index
= check_key(menu
, &event
.xkey
);
1011 selectEntry(menu
, index
);
1017 if (event
.type
== ButtonPress
)
1020 WMHandleEvent(&event
);
1024 XUngrabKeyboard(dpy
, CurrentTime
);
1026 if (done
== 2 && menu
->selected_entry
>= 0) {
1027 entry
= menu
->entries
[menu
->selected_entry
];
1032 if (entry
&& entry
->callback
!= NULL
&& entry
->flags
.enabled
&& entry
->cascade
< 0) {
1033 #if (MENU_BLINK_COUNT > 0)
1034 int sel
= menu
->selected_entry
;
1037 for (i
= 0; i
< MENU_BLINK_COUNT
; i
++) {
1038 paintEntry(menu
, sel
, False
);
1040 wusleep(MENU_BLINK_DELAY
);
1041 paintEntry(menu
, sel
, True
);
1043 wusleep(MENU_BLINK_DELAY
);
1046 selectEntry(menu
, -1);
1048 if (!menu
->flags
.buttoned
) {
1050 move_menus(menu
, old_pos_x
, old_pos_y
);
1054 (*entry
->callback
) (menu
, entry
);
1056 if (!menu
->flags
.buttoned
) {
1058 move_menus(menu
, old_pos_x
, old_pos_y
);
1060 selectEntry(menu
, -1);
1063 /* returns True if returning from a submenu to a parent menu,
1064 * False if exiting from menu */
1068 void wMenuMapAt(WMenu
* menu
, int x
, int y
, int keyboard
)
1070 WMRect rect
= wGetRectForHead(menu
->frame
->screen_ptr
,
1071 wGetHeadForPointerLocation(menu
->frame
->screen_ptr
));
1073 if (!menu
->flags
.realized
) {
1074 menu
->flags
.realized
= 1;
1077 if (!menu
->flags
.mapped
) {
1078 if (wPreferences
.wrap_menus
) {
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
);
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;
1096 selectEntry(menu
, 0);
1103 void wMenuMap(WMenu
* menu
)
1105 if (!menu
->flags
.realized
) {
1106 menu
->flags
.realized
= 1;
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
);
1114 XMapWindow(dpy
, menu
->frame
->core
->window
);
1115 wRaiseFrame(menu
->frame
->core
);
1116 menu
->flags
.mapped
= 1;
1119 void wMenuUnmap(WMenu
* menu
)
1123 XUnmapWindow(dpy
, menu
->frame
->core
->window
);
1124 if (menu
->flags
.titled
&& menu
->flags
.buttoned
) {
1125 wFrameWindowHideButton(menu
->frame
, WFF_RIGHT_BUTTON
);
1127 menu
->flags
.buttoned
= 0;
1128 menu
->flags
.mapped
= 0;
1129 menu
->flags
.open_to_left
= 0;
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
) {
1135 wMenuUnmap(menu
->cascades
[i
]);
1138 menu
->selected_entry
= -1;
1141 void wMenuPaint(WMenu
* menu
)
1145 if (!menu
->flags
.mapped
) {
1150 for (i
= 0; i
< menu
->entry_no
; i
++) {
1151 paintEntry(menu
, i
, i
== menu
->selected_entry
);
1155 void wMenuSetEnabled(WMenu
* menu
, int index
, int enable
)
1157 if (index
>= menu
->entry_no
)
1159 menu
->entries
[index
]->flags
.enabled
= enable
;
1160 paintEntry(menu
, index
, index
== menu
->selected_entry
);
1161 paintEntry(menu
->brother
, index
, index
== menu
->selected_entry
);
1164 /* ====================================================================== */
1166 static void editEntry(WMenu
* menu
, WMenuEntry
* entry
)
1170 WObjDescriptor
*desc
;
1176 menu
->flags
.editing
= 1;
1178 text
= wTextCreate(menu
->menu
, 1, menu
->entry_height
* entry
->order
,
1179 menu
->menu
->width
- 2, menu
->entry_height
- 1);
1181 wTextPutText(text
, entry
->text
);
1182 XGetInputFocus(dpy
, &old_focus
, &old_revert
);
1183 XSetInputFocus(dpy
, text
->core
->window
, RevertToNone
, CurrentTime
);
1185 if (XGrabKeyboard(dpy
, text
->core
->window
, True
, GrabModeAsync
, GrabModeAsync
, CurrentTime
) != GrabSuccess
) {
1186 wwarning(_("could not grab keyboard"));
1189 wSetFocusTo(menu
->frame
->screen_ptr
, menu
->frame
->screen_ptr
->focused_window
);
1193 while (!done
&& !text
->done
) {
1195 XAllowEvents(dpy
, AsyncKeyboard
| AsyncPointer
, CurrentTime
);
1197 WMNextEvent(dpy
, &event
);
1199 if (XFindContext(dpy
, event
.xany
.window
, wWinContext
, (XPointer
*) & desc
) == XCNOENT
)
1202 if ((desc
!= NULL
) && (desc
->handle_anything
!= NULL
)) {
1204 (*desc
->handle_anything
) (desc
, &event
);
1207 switch (event
.type
) {
1209 XAllowEvents(dpy
, ReplayPointer
, CurrentTime
);
1213 WMHandleEvent(&event
);
1219 XSetInputFocus(dpy
, old_focus
, old_revert
, CurrentTime
);
1221 wSetFocusTo(menu
->frame
->screen_ptr
, menu
->frame
->screen_ptr
->focused_window
);
1223 t
= wTextGetText(text
);
1224 /* if !t, the user has canceled editing */
1228 entry
->text
= wstrdup(t
);
1230 menu
->flags
.realized
= 0;
1234 XUngrabKeyboard(dpy
, CurrentTime
);
1236 if (t
&& menu
->on_edit
)
1237 (*menu
->on_edit
) (menu
, entry
);
1239 menu
->flags
.editing
= 0;
1241 if (!menu
->flags
.realized
)
1245 static void selectEntry(WMenu
* menu
, int entry_no
)
1251 if (menu
->entries
== NULL
)
1254 if (entry_no
>= menu
->entry_no
)
1257 old_entry
= menu
->selected_entry
;
1258 menu
->selected_entry
= entry_no
;
1260 if (old_entry
!= entry_no
) {
1262 /* unselect previous entry */
1263 if (old_entry
>= 0) {
1264 paintEntry(menu
, old_entry
, False
);
1265 entry
= menu
->entries
[old_entry
];
1268 if (entry
->cascade
>= 0 && menu
->cascades
) {
1269 if (!menu
->cascades
[entry
->cascade
]->flags
.buttoned
) {
1270 wMenuUnmap(menu
->cascades
[entry
->cascade
]);
1276 menu
->selected_entry
= -1;
1279 entry
= menu
->entries
[entry_no
];
1281 if (entry
->cascade
>= 0 && menu
->cascades
&& entry
->flags
.enabled
) {
1282 /* Callback for when the submenu is opened.
1284 submenu
= menu
->cascades
[entry
->cascade
];
1285 if (submenu
&& submenu
->flags
.brother
)
1286 submenu
= submenu
->brother
;
1288 if (entry
->callback
) {
1289 /* Only call the callback if the submenu is not yet mapped.
1291 if (menu
->flags
.brother
) {
1292 if (!submenu
|| !submenu
->flags
.mapped
)
1293 (*entry
->callback
) (menu
->brother
, entry
);
1295 if (!submenu
|| !submenu
->flags
.buttoned
)
1296 (*entry
->callback
) (menu
, entry
);
1300 /* the submenu menu might have changed */
1301 submenu
= menu
->cascades
[entry
->cascade
];
1304 if (!submenu
->flags
.mapped
) {
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;
1313 if (submenu
->flags
.open_to_left
) {
1314 x
= menu
->frame_x
- MENUW(submenu
);
1317 submenu
->flags
.open_to_left
= 0;
1320 x
= menu
->frame_x
+ MENUW(menu
);
1322 if (x
+ MENUW(submenu
)
1323 >= menu
->frame
->screen_ptr
->scr_width
) {
1325 x
= menu
->frame_x
- MENUW(submenu
);
1326 submenu
->flags
.open_to_left
= 1;
1330 x
= menu
->frame_x
+ MENUW(menu
);
1333 if (wPreferences
.align_menus
) {
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
;
1343 wMenuMapAt(menu
->cascades
[entry
->cascade
], x
, y
, False
);
1344 menu
->cascades
[entry
->cascade
]->parent
= menu
;
1349 paintEntry(menu
, entry_no
, True
);
1353 static WMenu
*findMenu(WScreen
* scr
, int *x_ret
, int *y_ret
)
1356 WObjDescriptor
*desc
;
1357 Window root_ret
, win
, junk_win
;
1361 XQueryPointer(dpy
, scr
->root_win
, &root_ret
, &win
, &x
, &y
, &wx
, &wy
, &mask
);
1366 if (XFindContext(dpy
, win
, wWinContext
, (XPointer
*) & desc
) == XCNOENT
)
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
);
1377 static void closeCascade(WMenu
* menu
)
1379 WMenu
*parent
= menu
->parent
;
1381 if (menu
->flags
.brother
|| (!menu
->flags
.buttoned
&& (!menu
->flags
.app_menu
|| menu
->parent
!= NULL
))) {
1383 selectEntry(menu
, -1);
1385 #if (MENU_BLINK_DELAY > 2)
1386 wusleep(MENU_BLINK_DELAY
/ 2);
1389 while (parent
!= NULL
1390 && (parent
->parent
!= NULL
|| !parent
->flags
.app_menu
|| parent
->flags
.brother
)
1391 && !parent
->flags
.buttoned
) {
1392 selectEntry(parent
, -1);
1394 parent
= parent
->parent
;
1397 selectEntry(parent
, -1);
1401 static void closeBrotherCascadesOf(WMenu
* menu
)
1406 for (i
= 0; i
< menu
->cascade_no
; i
++) {
1407 if (menu
->cascades
[i
]->flags
.brother
) {
1408 tmp
= menu
->cascades
[i
];
1410 tmp
= menu
->cascades
[i
]->brother
;
1412 if (tmp
->flags
.mapped
) {
1413 selectEntry(tmp
->parent
, -1);
1414 closeBrotherCascadesOf(tmp
);
1420 #define getEntryAt(menu, x, y) ((y)<0 ? -1 : (y)/(menu->entry_height))
1422 static WMenu
*parentMenu(WMenu
* menu
)
1427 if (menu
->flags
.buttoned
)
1430 while (menu
->parent
&& menu
->parent
->flags
.mapped
) {
1431 parent
= menu
->parent
;
1432 if (parent
->selected_entry
< 0)
1434 entry
= parent
->entries
[parent
->selected_entry
];
1435 if (!entry
->flags
.enabled
|| entry
->cascade
< 0 || !parent
->cascades
||
1436 parent
->cascades
[entry
->cascade
] != menu
)
1439 if (menu
->flags
.buttoned
)
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
1454 static void raiseMenus(WMenu
* menu
, int submenus
)
1462 wRaiseFrame(menu
->frame
->core
);
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
);
1472 if (submenus
< 0 && !menu
->flags
.buttoned
&& menu
->parent
&& menu
->parent
->flags
.mapped
)
1473 raiseMenus(menu
->parent
, submenus
);
1476 WMenu
*wMenuUnderPointer(WScreen
* screen
)
1478 WObjDescriptor
*desc
;
1479 Window root_ret
, win
;
1483 XQueryPointer(dpy
, screen
->root_win
, &root_ret
, &win
, &dummy
, &dummy
, &dummy
, &dummy
, &mask
);
1488 if (XFindContext(dpy
, win
, wWinContext
, (XPointer
*) & desc
) == XCNOENT
)
1491 if (desc
->parent_type
== WCLASS_MENU
)
1492 return (WMenu
*) desc
->parent
;
1496 static void getPointerPosition(WScreen
* scr
, int *x
, int *y
)
1498 Window root_ret
, win
;
1502 XQueryPointer(dpy
, scr
->root_win
, &root_ret
, &win
, x
, y
, &wx
, &wy
, &mask
);
1505 static void getScrollAmount(WMenu
* menu
, int *hamount
, int *vamount
)
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
);
1513 WMRect rect
= wGetRectForHead(scr
, wGetHeadForPointerLocation(scr
));
1518 getPointerPosition(scr
, &xroot
, &yroot
);
1520 if (xroot
<= (rect
.pos
.x
+ 1) && menuX1
< rect
.pos
.x
) {
1521 /* scroll to the right */
1522 *hamount
= WMIN(MENU_SCROLL_STEP
, abs(menuX1
));
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));
1531 *hamount
= -*hamount
;
1534 if (yroot
<= (rect
.pos
.y
+ 1) && menuY1
< rect
.pos
.y
) {
1536 *vamount
= WMIN(MENU_SCROLL_STEP
, abs(menuY1
));
1538 } else if (yroot
>= (rect
.pos
.y
+ rect
.size
.height
- 2) && menuY2
> (rect
.pos
.y
+ rect
.size
.height
- 1)) {
1540 *vamount
= WMIN(MENU_SCROLL_STEP
, abs(menuY2
- rect
.pos
.y
- rect
.size
.height
- 2));
1542 *vamount
= -*vamount
;
1546 static void dragScrollMenuCallback(void *data
)
1548 WMenu
*menu
= (WMenu
*) data
;
1549 WScreen
*scr
= menu
->menu
->screen_ptr
;
1550 WMenu
*parent
= parentMenu(menu
);
1551 int hamount
, vamount
;
1553 int newSelectedEntry
;
1555 getScrollAmount(menu
, &hamount
, &vamount
);
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
);
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;
1570 /* paranoid check */
1571 if (newSelectedEntry
>= 0) {
1572 /* keep scrolling */
1573 menu
->timer
= WMAddTimerHandler(MENU_SCROLL_DELAY
, dragScrollMenuCallback
, menu
);
1578 /* don't need to scroll anymore */
1580 if (findMenu(scr
, &x
, &y
)) {
1581 newSelectedEntry
= getEntryAt(menu
, x
, y
);
1582 selectEntry(menu
, newSelectedEntry
);
1587 static void scrollMenuCallback(void *data
)
1589 WMenu
*menu
= (WMenu
*) data
;
1590 WMenu
*parent
= parentMenu(menu
);
1591 int hamount
= 0; /* amount to scroll */
1594 #ifdef VIRTUAL_DESKTOP
1595 /* don't scroll if it is in vdesk mode */
1596 if (!wPreferences
.vdesk_enable
)
1598 getScrollAmount(menu
, &hamount
, &vamount
);
1600 if (hamount
!= 0 || vamount
!= 0) {
1601 wMenuMove(parent
, parent
->frame_x
+ hamount
, parent
->frame_y
+ vamount
, True
);
1603 /* keep scrolling */
1604 menu
->timer
= WMAddTimerHandler(MENU_SCROLL_DELAY
, scrollMenuCallback
, menu
);
1606 /* don't need to scroll anymore */
1611 #define MENU_SCROLL_BORDER 5
1613 static int isPointNearBoder(WMenu
* menu
, int x
, int y
)
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
);
1620 int head
= wGetHeadForPoint(menu
->frame
->screen_ptr
, wmkpoint(x
, y
));
1621 WMRect rect
= wGetRectForHead(menu
->frame
->screen_ptr
, head
);
1623 /* XXX: handle screen joins properly !! */
1625 if (x
>= menuX1
&& x
<= menuX2
&&
1626 (y
< rect
.pos
.y
+ MENU_SCROLL_BORDER
|| y
>= rect
.pos
.y
+ rect
.size
.height
- MENU_SCROLL_BORDER
))
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
))
1635 typedef struct _delay
{
1640 static void leaving(_delay
* dl
)
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;
1648 void wMenuScroll(WMenu
* menu
, XEvent
* event
)
1651 WMenu
*omenu
= parentMenu(menu
);
1652 WScreen
*scr
= menu
->frame
->screen_ptr
;
1655 int old_frame_x
= omenu
->frame_x
;
1656 int old_frame_y
= omenu
->frame_y
;
1659 if (omenu
->jump_back
)
1660 WMDeleteTimerWithClientData(omenu
->jump_back
);
1662 if (( /*omenu->flags.buttoned && */ !wPreferences
.wrap_menus
)
1663 || omenu
->flags
.app_menu
) {
1667 if (!wPreferences
.wrap_menus
)
1668 raiseMenus(omenu
, True
);
1670 raiseMenus(menu
, False
);
1673 scrollMenuCallback(menu
);
1676 int x
, y
, on_border
, on_x_edge
, on_y_edge
, on_title
;
1679 WMNextEvent(dpy
, &ev
);
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
;
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
);
1691 smenu
= wMenuUnderPointer(scr
);
1693 if ((smenu
== NULL
&& !on_border
) || (smenu
&& parentMenu(smenu
) != omenu
)) {
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
;
1703 if (!on_border
&& !jump_back
) {
1708 if (menu
->timer
&& (smenu
!= menu
|| (!on_y_edge
&& !on_x_edge
))) {
1709 WMDeleteTimerHandler(menu
->timer
);
1717 scrollMenuCallback(menu
);
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
;
1726 smenu
= wMenuUnderPointer(scr
);
1727 if (smenu
== NULL
|| (smenu
&& smenu
->flags
.buttoned
&& smenu
!= omenu
))
1729 else if (smenu
== omenu
&& on_title
) {
1743 WMDeleteTimerHandler(menu
->timer
);
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;
1757 delayer
= omenu
->jump_back
;
1758 WMAddTimerHandler(MENU_JUMP_BACK_DELAY
, (WMCallback
*) leaving
, delayer
);
1762 static void menuExpose(WObjDescriptor
* desc
, XEvent
* event
)
1764 wMenuPaint(desc
->parent
);
1768 int *delayed_select
;
1773 static void delaySelection(void *data
)
1775 delay_data
*d
= (delay_data
*) data
;
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
);
1786 if (d
->delayed_select
)
1787 *(d
->delayed_select
) = 0;
1790 static void menuMouseDown(WObjDescriptor
* desc
, XEvent
* event
)
1792 XButtonEvent
*bev
= &event
->xbutton
;
1793 WMenu
*menu
= desc
->parent
;
1795 WScreen
*scr
= menu
->frame
->screen_ptr
;
1796 WMenuEntry
*entry
= NULL
;
1798 int close_on_exit
= 0;
1800 int delayed_select
= 0;
1804 int old_frame_x
= 0;
1805 int old_frame_y
= 0;
1806 delay_data d_data
= { NULL
, NULL
, NULL
};
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
1814 /*if (menu->flags.inside_handler) {
1817 menu
->flags
.inside_handler
= 1;
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
) {
1827 d_data
.delayed_select
= &delayed_select
;
1829 d_data
.magic
= WMAddTimerHandler(wPreferences
.dblclick_time
, delaySelection
, &d_data
);
1833 wRaiseFrame(menu
->frame
->core
);
1835 close_on_exit
= (bev
->send_event
|| menu
->flags
.brother
);
1837 smenu
= findMenu(scr
, &x
, &y
);
1845 if (menu
->flags
.editing
) {
1848 entry_no
= getEntryAt(menu
, x
, y
);
1849 if (entry_no
>= 0) {
1850 entry
= menu
->entries
[entry_no
];
1852 if (!close_on_exit
&& (bev
->state
& ControlMask
) && smenu
&& entry
->flags
.editable
) {
1853 editEntry(smenu
, entry
);
1855 } else if (bev
->state
& ControlMask
) {
1859 if (entry
->flags
.enabled
&& entry
->cascade
>= 0 && menu
->cascades
) {
1860 WMenu
*submenu
= menu
->cascades
[entry
->cascade
];
1862 if (submenu
->flags
.mapped
&& !submenu
->flags
.buttoned
&& menu
->selected_entry
!= entry_no
) {
1863 wMenuUnmap(submenu
);
1865 if (!submenu
->flags
.mapped
&& !delayed_select
) {
1866 selectEntry(menu
, entry_no
);
1867 } else if (!submenu
->flags
.buttoned
) {
1868 selectEntry(menu
, -1);
1871 } else if (!delayed_select
) {
1872 selectEntry(menu
, entry_no
);
1875 if (!wPreferences
.wrap_menus
&& !wPreferences
.scrollable_menus
) {
1877 dragScrollMenuCallback(menu
);
1880 #ifdef VIRTUAL_DESKTOP
1881 if (wPreferences
.vdesk_enable
) {
1882 wWorkspaceLowerEdge(scr
);
1886 prevx
= bev
->x_root
;
1887 prevy
= bev
->y_root
;
1891 XAllowEvents(dpy
, AsyncPointer
| SyncPointer
, CurrentTime
);
1893 WMMaskEvent(dpy
, ExposureMask
| ButtonMotionMask
| ButtonReleaseMask
| ButtonPressMask
, &ev
);
1896 smenu
= findMenu(scr
, &x
, &y
);
1898 if (smenu
== NULL
) {
1899 /* moved mouse out of menu */
1901 if (!delayed_select
&& d_data
.magic
) {
1902 WMDeleteTimerHandler(d_data
.magic
);
1903 d_data
.magic
= 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
;
1913 selectEntry(menu
, -1);
1915 prevx
= ev
.xmotion
.x_root
;
1916 prevy
= ev
.xmotion
.y_root
;
1918 } else if (menu
&& menu
!= smenu
1919 && (menu
->selected_entry
< 0
1920 || menu
->entries
[menu
->selected_entry
]->cascade
< 0)) {
1921 selectEntry(menu
, -1);
1923 if (!delayed_select
&& d_data
.magic
) {
1924 WMDeleteTimerHandler(d_data
.magic
);
1925 d_data
.magic
= NULL
;
1929 /* hysteresis for item selection */
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
) {
1935 Bool moved_to_submenu
; /* moved to direction of submenu */
1937 dx
= abs(prevx
- ev
.xmotion
.x_root
);
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
)) {
1947 index
= menu
->entries
[menu
->selected_entry
]->cascade
;
1949 if (menu
->cascades
[index
]->frame_x
> menu
->frame_x
) {
1950 if (prevx
< ev
.xmotion
.x_root
)
1951 moved_to_submenu
= True
;
1953 if (prevx
> ev
.xmotion
.x_root
)
1954 moved_to_submenu
= True
;
1959 if (menu
!= smenu
) {
1961 WMDeleteTimerHandler(d_data
.magic
);
1962 d_data
.magic
= NULL
;
1964 } else if (moved_to_submenu
) {
1965 /* while we are moving, postpone the selection */
1967 WMDeleteTimerHandler(d_data
.magic
);
1969 d_data
.delayed_select
= NULL
;
1971 d_data
.magic
= WMAddTimerHandler(MENU_SELECT_DELAY
,
1972 delaySelection
, &d_data
);
1973 prevx
= ev
.xmotion
.x_root
;
1974 prevy
= ev
.xmotion
.y_root
;
1978 WMDeleteTimerHandler(d_data
.magic
);
1979 d_data
.magic
= NULL
;
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
);
1993 dragScrollMenuCallback(smenu
);
1997 dragScrollMenuCallback(menu
);
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
);
2011 selectEntry(menu
, entry_no
);
2019 if (ev
.xbutton
.button
== event
->xbutton
.button
)
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
);
2035 if (menu
&& menu
->timer
) {
2036 WMDeleteTimerHandler(menu
->timer
);
2039 if (d_data
.magic
!= NULL
) {
2040 WMDeleteTimerHandler(d_data
.magic
);
2041 d_data
.magic
= NULL
;
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
;
2052 for (i
= 0; i
< MENU_BLINK_COUNT
; i
++) {
2053 paintEntry(menu
, sel
, False
);
2055 wusleep(MENU_BLINK_DELAY
);
2056 paintEntry(menu
, sel
, True
);
2058 wusleep(MENU_BLINK_DELAY
);
2061 /* unmap the menu, it's parents and call the callback */
2062 if (!menu
->flags
.buttoned
&& (!menu
->flags
.app_menu
|| menu
->parent
!= NULL
)) {
2065 selectEntry(menu
, -1);
2067 (*entry
->callback
) (menu
, entry
);
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);
2077 if (entry
->cascade
>= 0 && menu
->cascades
&& menu
->cascades
[entry
->cascade
]->flags
.brother
) {
2078 selectEntry(menu
, -1);
2083 if (((WMenu
*) desc
->parent
)->flags
.brother
|| close_on_exit
|| !smenu
)
2084 closeCascade(desc
->parent
);
2086 /* close the cascade windows that should not remain opened */
2087 closeBrotherCascadesOf(desc
->parent
);
2089 if (!wPreferences
.wrap_menus
)
2090 wMenuMove(parentMenu(desc
->parent
), old_frame_x
, old_frame_y
, True
);
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
);
2098 if (d_data
.magic
!= NULL
) {
2099 WMDeleteTimerHandler(d_data
.magic
);
2100 d_data
.magic
= NULL
;
2103 ((WMenu
*) desc
->parent
)->flags
.inside_handler
= 0;
2104 #ifdef VIRTUAL_DESKTOP
2105 wWorkspaceRaiseEdge(scr
);
2109 void wMenuMove(WMenu
* menu
, int x
, int y
, int submenus
)
2119 XMoveWindow(dpy
, menu
->frame
->core
->window
, x
, y
);
2121 if (submenus
> 0 && menu
->selected_entry
>= 0) {
2122 i
= menu
->entries
[menu
->selected_entry
]->cascade
;
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
);
2130 wMenuMove(submenu
, x
+ MENUW(menu
),
2131 y
+ submenu
->entry_height
* menu
->selected_entry
, submenus
);
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
);
2140 wMenuMove(menu
->parent
, x
- MENUW(menu
->parent
), menu
->frame_y
2141 - menu
->parent
->entry_height
* menu
->parent
->selected_entry
, submenus
);
2146 static void changeMenuLevels(WMenu
* menu
, int lower
)
2151 ChangeStackingLevel(menu
->frame
->core
, (!menu
->parent
? WMMainMenuLevel
: WMSubmenuLevel
));
2152 wRaiseFrame(menu
->frame
->core
);
2153 menu
->flags
.lowered
= 0;
2155 ChangeStackingLevel(menu
->frame
->core
, WMNormalLevel
);
2156 wLowerFrame(menu
->frame
->core
);
2157 menu
->flags
.lowered
= 1;
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
);
2167 static void menuTitleDoubleClick(WCoreWindow
* sender
, void *data
, XEvent
* event
)
2172 if (event
->xbutton
.state
& MOD_MASK
) {
2173 if (menu
->flags
.lowered
) {
2178 changeMenuLevels(menu
, lower
);
2182 static void menuTitleMouseDown(WCoreWindow
* sender
, void *data
, XEvent
* event
)
2187 int x
= menu
->frame_x
, y
= menu
->frame_y
;
2188 int dx
= event
->xbutton
.x_root
, dy
= event
->xbutton
.y_root
;
2192 /* can't touch the menu copy */
2193 if (menu
->flags
.brother
)
2196 if (event
->xbutton
.button
!= Button1
&& event
->xbutton
.button
!= Button2
)
2199 if (event
->xbutton
.state
& MOD_MASK
) {
2200 wLowerFrame(menu
->frame
->core
);
2203 wRaiseFrame(menu
->frame
->core
);
2208 /* lower/raise all submenus */
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
)
2215 wLowerFrame(tmp
->frame
->core
);
2217 wRaiseFrame(tmp
->frame
->core
);
2223 /* tear off the menu if it's a root menu or a cascade
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
);
2229 /* turn off selected menu entry in parent menu */
2230 selectEntry(menu
->parent
, -1);
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
;
2244 WMMaskEvent(dpy
, ButtonMotionMask
| ButtonReleaseMask
| ButtonPressMask
| ExposureMask
, &ev
);
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
);
2254 if (abs(ev
.xmotion
.x_root
- dx
) > MOVE_THRESHOLD
2255 || abs(ev
.xmotion
.y_root
- dy
) > MOVE_THRESHOLD
) {
2257 XGrabPointer(dpy
, menu
->frame
->titlebar
->window
, False
,
2258 ButtonMotionMask
| ButtonReleaseMask
2260 GrabModeAsync
, GrabModeAsync
, None
,
2261 wCursor
[WCUR_MOVE
], CurrentTime
);
2270 if (ev
.xbutton
.button
!= event
->xbutton
.button
)
2272 XUngrabPointer(dpy
, CurrentTime
);
2283 *----------------------------------------------------------------------
2285 * Handles mouse click on the close button of menus. The menu is
2286 * closed when the button is clicked.
2289 * The closed menu is reinserted at it's parent menus
2291 *----------------------------------------------------------------------
2293 static void menuCloseClick(WCoreWindow
* sender
, void *data
, XEvent
* event
)
2295 WMenu
*menu
= (WMenu
*) data
;
2296 WMenu
*parent
= menu
->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
;
2313 static void saveMenuInfo(WMPropList
* dict
, WMenu
* menu
, WMPropList
* key
)
2315 WMPropList
*value
, *list
;
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
);
2328 void wMenuSaveState(WScreen
* scr
)
2330 WMPropList
*menus
, *key
;
2333 menus
= WMCreatePLDictionary(NULL
, NULL
);
2336 if (scr
->switch_menu
&& scr
->switch_menu
->flags
.buttoned
) {
2337 key
= WMCreatePLString("SwitchMenu");
2338 saveMenuInfo(menus
, scr
->switch_menu
, key
);
2339 WMReleasePropList(key
);
2343 if (saveMenuRecurs(menus
, scr
, scr
->root_menu
))
2347 if (scr
->workspace_menu
&& scr
->workspace_menu
->flags
.buttoned
) {
2348 key
= WMCreatePLString("WorkspaceMenu");
2349 saveMenuInfo(menus
, scr
->workspace_menu
, key
);
2350 WMReleasePropList(key
);
2355 key
= WMCreatePLString("Menus");
2356 WMPutInPLDictionary(scr
->session_state
, key
, menus
);
2357 WMReleasePropList(key
);
2359 WMReleasePropList(menus
);
2364 static Bool
getMenuPath(WMenu
* menu
, char *buffer
, int bufSize
)
2369 if (!menu
->flags
.titled
|| !menu
->frame
->title
[0])
2372 len
= strlen(menu
->frame
->title
);
2377 ok
= getMenuPath(menu
->parent
, buffer
, bufSize
- len
- 1);
2382 strcat(buffer
, "\\");
2383 strcat(buffer
, menu
->frame
->title
);
2388 static Bool
saveMenuRecurs(WMPropList
* menus
, WScreen
* scr
, WMenu
* menu
)
2391 int save_menus
= 0, i
;
2395 if (menu
->flags
.brother
)
2396 menu
= menu
->brother
;
2398 if (menu
->flags
.buttoned
&& menu
!= scr
->switch_menu
) {
2401 ok
= getMenuPath(menu
, buffer
, 510);
2404 key
= WMCreatePLString(buffer
);
2405 saveMenuInfo(menus
, menu
, key
);
2406 WMReleasePropList(key
);
2412 for (i
= 0; i
< menu
->cascade_no
; i
++) {
2413 if (saveMenuRecurs(menus
, scr
, menu
->cascades
[i
]))
2421 #define COMPLAIN(key) wwarning(_("bad value in menus state info: %s"), key)
2423 static Bool
getMenuInfo(WMPropList
* info
, int *x
, int *y
, Bool
* lowered
)
2429 if (WMIsPLArray(info
)) {
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) {
2441 if (pos
!= NULL
&& WMIsPLString(pos
)) {
2442 if (sscanf(WMGetFromPLString(pos
), "%i,%i", x
, y
) != 2)
2443 COMPLAIN("Position");
2445 COMPLAIN("(position, flags...)");
2452 static int restoreMenu(WScreen
* scr
, WMPropList
* menu
, int which
)
2455 Bool lowered
= False
;
2456 WMenu
*pmenu
= NULL
;
2461 if (!getMenuInfo(menu
, &x
, &y
, &lowered
))
2465 if (which
& WSS_SWITCHMENU
) {
2466 OpenSwitchMenu(scr
, x
, y
, False
);
2467 pmenu
= scr
->switch_menu
;
2472 int width
= MENUW(pmenu
);
2473 int height
= MENUH(pmenu
);
2474 WMRect rect
= wGetRectForHead(scr
, wGetHeadForPointerLocation(scr
));
2477 changeMenuLevels(pmenu
, True
);
2480 if (x
< rect
.pos
.x
- width
)
2482 if (x
> rect
.pos
.x
+ rect
.size
.width
)
2483 x
= rect
.pos
.x
+ rect
.size
.width
- width
;
2486 if (y
> rect
.pos
.y
+ rect
.size
.height
)
2487 y
= rect
.pos
.y
+ rect
.size
.height
- height
;
2489 wMenuMove(pmenu
, x
, y
, True
);
2490 pmenu
->flags
.buttoned
= 1;
2491 wFrameWindowShowButton(pmenu
->frame
, WFF_RIGHT_BUTTON
);
2498 static int restoreMenuRecurs(WScreen
* scr
, WMPropList
* menus
, WMenu
* menu
, char *path
)
2500 WMPropList
*key
, *entry
;
2505 if (strlen(path
) + strlen(menu
->frame
->title
) > 510)
2508 snprintf(buffer
, sizeof(buffer
), "%s\\%s", path
, menu
->frame
->title
);
2509 key
= WMCreatePLString(buffer
);
2510 entry
= WMGetFromPLDictionary(menus
, key
);
2513 if (entry
&& getMenuInfo(entry
, &x
, &y
, &lowered
)) {
2515 if (!menu
->flags
.mapped
) {
2516 int width
= MENUW(menu
);
2517 int height
= MENUH(menu
);
2518 WMRect rect
= wGetRectForHead(scr
, wGetHeadForPointerLocation(scr
));
2520 wMenuMapAt(menu
, x
, y
, False
);
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
;
2532 changeMenuLevels(menu
, True
);
2535 if (x
< rect
.pos
.x
- width
)
2537 if (x
> rect
.pos
.x
+ rect
.size
.width
)
2538 x
= rect
.pos
.x
+ rect
.size
.width
- width
;
2541 if (y
> rect
.pos
.y
+ rect
.size
.height
)
2542 y
= rect
.pos
.y
+ rect
.size
.height
- height
;
2544 wMenuMove(menu
, x
, y
, True
);
2545 menu
->flags
.buttoned
= 1;
2546 wFrameWindowShowButton(menu
->frame
, WFF_RIGHT_BUTTON
);
2551 WMReleasePropList(key
);
2553 for (i
= 0; i
< menu
->cascade_no
; i
++) {
2554 if (restoreMenuRecurs(scr
, menus
, menu
->cascades
[i
], buffer
) != False
)
2562 void wMenuRestoreState(WScreen
* scr
)
2564 WMPropList
*menus
, *menu
, *key
, *skey
;
2566 if (!scr
->session_state
) {
2570 key
= WMCreatePLString("Menus");
2571 menus
= WMGetFromPLDictionary(scr
->session_state
, key
);
2572 WMReleasePropList(key
);
2579 skey
= WMCreatePLString("SwitchMenu");
2580 menu
= WMGetFromPLDictionary(menus
, skey
);
2581 WMReleasePropList(skey
);
2582 restoreMenu(scr
, menu
, WSS_SWITCHMENU
);
2585 if (!scr
->root_menu
) {
2586 OpenRootMenu(scr
, scr
->scr_width
* 2, 0, False
);
2587 wMenuUnmap(scr
->root_menu
);
2589 restoreMenuRecurs(scr
, menus
, scr
->root_menu
, "");
2593 void OpenWorkspaceMenu(WScreen
* scr
, int x
, int y
)
2595 WMenu
*menu
, *parent
;
2599 if (!scr
->root_menu
) {
2600 OpenRootMenu(scr
, scr
->scr_width
* 2, 0, False
);
2601 wMenuUnmap(scr
->root_menu
);
2604 menu
= scr
->workspace_menu
;
2606 if (menu
->flags
.mapped
) {
2607 if (!menu
->flags
.buttoned
) {
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
);
2618 wRaiseFrame(menu
->frame
->core
);
2619 wMenuMapCopyAt(menu
, x
, y
);
2622 wMenuMapAt(menu
, x
, y
, False
);