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"
44 #include "workspace.h"
47 /****** Global Variables ******/
49 extern Cursor wCursor
[WCUR_LAST
];
51 extern XContext wWinContext
;
53 extern WPreferences wPreferences
;
55 #define MOD_MASK wPreferences.modifier_mask
57 #define MENU_SCROLL_STEP menuScrollParameters[(int)wPreferences.menu_scroll_speed].steps
58 #define MENU_SCROLL_DELAY menuScrollParameters[(int)wPreferences.menu_scroll_speed].delay
60 #define MENUW(m) ((m)->frame->core->width+2*FRAME_BORDER_WIDTH)
61 #define MENUH(m) ((m)->frame->core->height+2*FRAME_BORDER_WIDTH)
63 /***** Local Stuff ******/
65 #define WSS_ROOTMENU (1<<0)
66 #define WSS_SWITCHMENU (1<<1)
67 #define WSS_WSMENU (1<<2)
72 } menuScrollParameters
[5] = {
74 MENU_SCROLL_STEPS_UF
, MENU_SCROLL_DELAY_UF
}, {
75 MENU_SCROLL_STEPS_F
, MENU_SCROLL_DELAY_F
}, {
76 MENU_SCROLL_STEPS_M
, MENU_SCROLL_DELAY_M
}, {
77 MENU_SCROLL_STEPS_S
, MENU_SCROLL_DELAY_S
}, {
78 MENU_SCROLL_STEPS_US
, MENU_SCROLL_DELAY_US
}};
80 static void menuMouseDown(WObjDescriptor
* desc
, XEvent
* event
);
81 static void menuExpose(WObjDescriptor
* desc
, XEvent
* event
);
82 static void menuTitleDoubleClick(WCoreWindow
* sender
, void *data
, XEvent
* event
);
83 static void menuTitleMouseDown(WCoreWindow
* sender
, void *data
, XEvent
* event
);
84 static void menuCloseClick(WCoreWindow
* sender
, void *data
, XEvent
* event
);
85 static void updateTexture(WMenu
* menu
);
86 static int saveMenuRecurs(WMPropList
* menus
, WScreen
* scr
, WMenu
* menu
);
87 static int restoreMenuRecurs(WScreen
* scr
, WMPropList
* menus
, WMenu
* menu
, char *path
);
88 static void selectEntry(WMenu
* menu
, int entry_no
);
89 static void closeCascade(WMenu
* menu
);
91 /****** Notification Observers ******/
93 static void appearanceObserver(void *self
, WMNotification
* notif
)
95 WMenu
*menu
= (WMenu
*) self
;
96 uintptr_t flags
= (uintptr_t)WMGetNotificationClientData(notif
);
98 if (!menu
->flags
.realized
)
101 if (WMGetNotificationName(notif
) == WNMenuAppearanceSettingsChanged
) {
102 if (flags
& WFontSettings
) {
103 menu
->flags
.realized
= 0;
106 if (flags
& WTextureSettings
) {
107 if (!menu
->flags
.brother
)
110 if (flags
& (WTextureSettings
| WColorSettings
)) {
113 } else if (menu
->flags
.titled
) {
115 if (flags
& WFontSettings
) {
116 menu
->flags
.realized
= 0;
119 if (flags
& WTextureSettings
) {
120 menu
->frame
->flags
.need_texture_remake
= 1;
122 if (flags
& (WColorSettings
| WTextureSettings
)) {
123 wFrameWindowPaint(menu
->frame
);
128 /************************************/
131 *----------------------------------------------------------------------
133 * Creates a new empty menu with the specified title. If main_menu
134 * is True, the created menu will be a main menu, which has some special
135 * properties such as being placed over other normal menus.
136 * If title is NULL, the menu will have no titlebar.
140 *----------------------------------------------------------------------
142 WMenu
*wMenuCreate(WScreen
* screen
, char *title
, int main_menu
)
145 static int brother
= 0;
148 menu
= wmalloc(sizeof(WMenu
));
150 memset(menu
, 0, sizeof(WMenu
));
152 #ifdef SINGLE_MENULEVEL
153 tmp
= WMSubmenuLevel
;
155 tmp
= (main_menu
? WMMainMenuLevel
: WMSubmenuLevel
);
158 flags
= WFF_SINGLE_STATE
| WFF_BORDER
;
160 flags
|= WFF_TITLEBAR
| WFF_RIGHT_BUTTON
;
161 menu
->flags
.titled
= 1;
164 wFrameWindowCreate(screen
, tmp
, 8, 2, 1, 1, &wPreferences
.menu_title_clearance
, flags
,
165 screen
->menu_title_texture
, NULL
,
166 screen
->menu_title_color
, &screen
->menu_title_font
);
168 menu
->frame
->core
->descriptor
.parent
= menu
;
169 menu
->frame
->core
->descriptor
.parent_type
= WCLASS_MENU
;
170 menu
->frame
->core
->descriptor
.handle_mousedown
= menuMouseDown
;
172 wFrameWindowHideButton(menu
->frame
, WFF_RIGHT_BUTTON
);
175 menu
->frame
->title
= wstrdup(title
);
178 menu
->frame
->flags
.justification
= WTJ_LEFT
;
180 menu
->frame
->rbutton_image
= screen
->b_pixmaps
[WBUT_CLOSE
];
183 menu
->alloced_entries
= 0;
184 menu
->selected_entry
= -1;
185 menu
->entries
= NULL
;
187 menu
->frame_x
= screen
->app_menu_x
;
188 menu
->frame_y
= screen
->app_menu_y
;
190 menu
->frame
->child
= menu
;
192 menu
->flags
.lowered
= 0;
196 /* setup object descriptors */
197 menu
->frame
->on_mousedown_titlebar
= menuTitleMouseDown
;
198 menu
->frame
->on_dblclick_titlebar
= menuTitleDoubleClick
;
201 menu
->frame
->on_click_right
= menuCloseClick
;
203 menu
->menu
= wCoreCreate(menu
->frame
->core
, 0, menu
->frame
->top_width
, menu
->frame
->core
->width
, 10);
205 menu
->menu
->descriptor
.parent
= menu
;
206 menu
->menu
->descriptor
.parent_type
= WCLASS_MENU
;
207 menu
->menu
->descriptor
.handle_expose
= menuExpose
;
208 menu
->menu
->descriptor
.handle_mousedown
= menuMouseDown
;
210 menu
->menu_texture_data
= None
;
212 XMapWindow(dpy
, menu
->menu
->window
);
218 menu
->brother
= wMenuCreate(screen
, title
, main_menu
);
220 menu
->brother
->flags
.brother
= 1;
221 menu
->brother
->brother
= menu
;
223 WMAddNotificationObserver(appearanceObserver
, menu
, WNMenuAppearanceSettingsChanged
, menu
);
225 WMAddNotificationObserver(appearanceObserver
, menu
, WNMenuTitleAppearanceSettingsChanged
, menu
);
230 WMenu
*wMenuCreateForApp(WScreen
* screen
, char *title
, int main_menu
)
234 menu
= wMenuCreate(screen
, title
, main_menu
);
237 menu
->flags
.app_menu
= 1;
238 menu
->brother
->flags
.app_menu
= 1;
243 static void insertEntry(WMenu
* menu
, WMenuEntry
* entry
, int index
)
247 for (i
= menu
->entry_no
- 1; i
>= index
; i
--) {
248 menu
->entries
[i
]->order
++;
249 menu
->entries
[i
+ 1] = menu
->entries
[i
];
251 menu
->entries
[index
] = entry
;
254 WMenuEntry
*wMenuInsertCallback(WMenu
* menu
, int index
, char *text
,
255 void (*callback
) (WMenu
* menu
, WMenuEntry
* entry
), void *clientdata
)
259 menu
->flags
.realized
= 0;
260 menu
->brother
->flags
.realized
= 0;
262 /* reallocate array if it's too small */
263 if (menu
->entry_no
>= menu
->alloced_entries
) {
266 tmp
= wrealloc(menu
->entries
, sizeof(WMenuEntry
) * (menu
->alloced_entries
+ 5));
269 menu
->alloced_entries
+= 5;
271 menu
->brother
->entries
= tmp
;
272 menu
->brother
->alloced_entries
= menu
->alloced_entries
;
274 entry
= wmalloc(sizeof(WMenuEntry
));
275 memset(entry
, 0, sizeof(WMenuEntry
));
276 entry
->flags
.enabled
= 1;
277 entry
->text
= wstrdup(text
);
279 entry
->clientdata
= clientdata
;
280 entry
->callback
= callback
;
281 if (index
< 0 || index
>= menu
->entry_no
) {
282 entry
->order
= menu
->entry_no
;
283 menu
->entries
[menu
->entry_no
] = entry
;
285 entry
->order
= index
;
286 insertEntry(menu
, entry
, index
);
290 menu
->brother
->entry_no
= menu
->entry_no
;
295 void wMenuEntrySetCascade(WMenu
* menu
, WMenuEntry
* entry
, WMenu
* cascade
)
297 WMenu
*brother
= menu
->brother
;
300 assert(menu
->flags
.brother
== 0);
302 if (entry
->cascade
>= 0) {
303 menu
->flags
.realized
= 0;
304 brother
->flags
.realized
= 0;
307 cascade
->parent
= menu
;
309 cascade
->brother
->parent
= brother
;
312 for (i
= 0; i
< menu
->cascade_no
; i
++) {
313 if (menu
->cascades
[i
] == NULL
) {
314 menu
->cascades
[i
] = cascade
;
315 brother
->cascades
[i
] = cascade
->brother
;
322 entry
->cascade
= menu
->cascade_no
;
324 menu
->cascades
= wrealloc(menu
->cascades
, sizeof(WMenu
) * (menu
->cascade_no
+ 1));
325 menu
->cascades
[menu
->cascade_no
++] = cascade
;
327 brother
->cascades
= wrealloc(brother
->cascades
, sizeof(WMenu
) * (brother
->cascade_no
+ 1));
328 brother
->cascades
[brother
->cascade_no
++] = cascade
->brother
;
331 if (menu
->flags
.lowered
) {
333 cascade
->flags
.lowered
= 1;
334 ChangeStackingLevel(cascade
->frame
->core
, WMNormalLevel
);
336 cascade
->brother
->flags
.lowered
= 1;
337 ChangeStackingLevel(cascade
->brother
->frame
->core
, WMNormalLevel
);
340 if (!menu
->flags
.realized
)
344 void wMenuEntryRemoveCascade(WMenu
* menu
, WMenuEntry
* entry
)
346 assert(menu
->flags
.brother
== 0);
348 /* destroy cascade menu */
349 if (entry
->cascade
>= 0 && menu
->cascades
&& menu
->cascades
[entry
->cascade
] != NULL
) {
351 wMenuDestroy(menu
->cascades
[entry
->cascade
], True
);
353 menu
->cascades
[entry
->cascade
] = NULL
;
354 menu
->brother
->cascades
[entry
->cascade
] = NULL
;
360 void wMenuRemoveItem(WMenu
* menu
, int index
)
364 if (menu
->flags
.brother
) {
365 wMenuRemoveItem(menu
->brother
, index
);
369 if (index
>= menu
->entry_no
)
372 /* destroy cascade menu */
373 wMenuEntryRemoveCascade(menu
, menu
->entries
[index
]);
375 /* destroy unshared data */
377 if (menu
->entries
[index
]->text
)
378 wfree(menu
->entries
[index
]->text
);
380 if (menu
->entries
[index
]->rtext
)
381 wfree(menu
->entries
[index
]->rtext
);
383 if (menu
->entries
[index
]->free_cdata
&& menu
->entries
[index
]->clientdata
)
384 (*menu
->entries
[index
]->free_cdata
) (menu
->entries
[index
]->clientdata
);
386 wfree(menu
->entries
[index
]);
388 for (i
= index
; i
< menu
->entry_no
- 1; i
++) {
389 menu
->entries
[i
+ 1]->order
--;
390 menu
->entries
[i
] = menu
->entries
[i
+ 1];
393 menu
->brother
->entry_no
--;
396 static Pixmap
renderTexture(WMenu
* menu
)
404 WScreen
*scr
= menu
->menu
->screen_ptr
;
405 WTexture
*texture
= scr
->menu_item_texture
;
407 if (wPreferences
.menu_style
== MS_NORMAL
) {
408 img
= wTextureRenderImage(texture
, menu
->menu
->width
, menu
->entry_height
, WREL_MENUENTRY
);
410 img
= wTextureRenderImage(texture
, menu
->menu
->width
, menu
->menu
->height
+ 1, WREL_MENUENTRY
);
413 wwarning(_("could not render texture: %s"), RMessageForError(RErrorCode
));
418 if (wPreferences
.menu_style
== MS_SINGLE_TEXTURE
) {
420 light
.red
= light
.green
= light
.blue
= 80;
423 dark
.red
= dark
.green
= dark
.blue
= 0;
426 mid
.red
= mid
.green
= mid
.blue
= 40;
428 for (i
= 1; i
< menu
->entry_no
; i
++) {
429 ROperateLine(img
, RSubtractOperation
, 0, i
* menu
->entry_height
- 2,
430 menu
->menu
->width
- 1, i
* menu
->entry_height
- 2, &mid
);
432 RDrawLine(img
, 0, i
* menu
->entry_height
- 1,
433 menu
->menu
->width
- 1, i
* menu
->entry_height
- 1, &dark
);
435 ROperateLine(img
, RAddOperation
, 0, i
* menu
->entry_height
,
436 menu
->menu
->width
- 1, i
* menu
->entry_height
, &light
);
439 if (!RConvertImage(scr
->rcontext
, img
, &pix
)) {
440 wwarning(_("error rendering image:%s"), RMessageForError(RErrorCode
));
447 static void updateTexture(WMenu
* menu
)
449 WScreen
*scr
= menu
->menu
->screen_ptr
;
451 /* setup background texture */
452 if (scr
->menu_item_texture
->any
.type
!= WTEX_SOLID
) {
453 if (!menu
->flags
.brother
) {
454 FREE_PIXMAP(menu
->menu_texture_data
);
456 menu
->menu_texture_data
= renderTexture(menu
);
458 XSetWindowBackgroundPixmap(dpy
, menu
->menu
->window
, menu
->menu_texture_data
);
459 XClearWindow(dpy
, menu
->menu
->window
);
461 XSetWindowBackgroundPixmap(dpy
, menu
->brother
->menu
->window
, menu
->menu_texture_data
);
462 XClearWindow(dpy
, menu
->brother
->menu
->window
);
465 XSetWindowBackground(dpy
, menu
->menu
->window
, scr
->menu_item_texture
->any
.color
.pixel
);
466 XClearWindow(dpy
, menu
->menu
->window
);
470 void wMenuRealize(WMenu
* menu
)
473 int width
, rwidth
, mrwidth
, mwidth
;
474 int theight
, twidth
, eheight
;
475 WScreen
*scr
= menu
->frame
->screen_ptr
;
476 static int brother_done
= 0;
481 wMenuRealize(menu
->brother
);
485 flags
= WFF_SINGLE_STATE
| WFF_BORDER
;
486 if (menu
->flags
.titled
)
487 flags
|= WFF_TITLEBAR
| WFF_RIGHT_BUTTON
;
489 wFrameWindowUpdateBorders(menu
->frame
, flags
);
491 if (menu
->flags
.titled
) {
492 twidth
= WMWidthOfString(scr
->menu_title_font
, menu
->frame
->title
, strlen(menu
->frame
->title
));
493 theight
= menu
->frame
->top_width
;
494 twidth
+= theight
+ (wPreferences
.new_style
== TS_NEW
? 16 : 8);
499 eheight
= WMFontHeight(scr
->menu_entry_font
) + 6 + wPreferences
.menu_text_clearance
* 2;
500 menu
->entry_height
= eheight
;
503 for (i
= 0; i
< menu
->entry_no
; i
++) {
506 /* search widest text */
507 text
= menu
->entries
[i
]->text
;
508 width
= WMWidthOfString(scr
->menu_entry_font
, text
, strlen(text
)) + 10;
510 if (menu
->entries
[i
]->flags
.indicator
) {
511 width
+= MENU_INDICATOR_SPACE
;
517 /* search widest text on right */
518 text
= menu
->entries
[i
]->rtext
;
520 rwidth
= WMWidthOfString(scr
->menu_entry_font
, text
, strlen(text
))
522 else if (menu
->entries
[i
]->cascade
>= 0)
527 if (rwidth
> mrwidth
)
535 wCoreConfigure(menu
->menu
, 0, theight
, mwidth
, menu
->entry_no
* eheight
- 1);
537 wFrameWindowResize(menu
->frame
, mwidth
, menu
->entry_no
* eheight
- 1
538 + menu
->frame
->top_width
+ menu
->frame
->bottom_width
);
542 menu
->flags
.realized
= 1;
544 if (menu
->flags
.mapped
)
546 if (menu
->brother
->flags
.mapped
)
547 wMenuPaint(menu
->brother
);
550 void wMenuDestroy(WMenu
* menu
, int recurse
)
554 WMRemoveNotificationObserver(menu
);
556 /* remove any pending timers */
558 WMDeleteTimerHandler(menu
->timer
);
561 /* call destroy handler */
562 if (menu
->on_destroy
)
563 (*menu
->on_destroy
) (menu
);
565 /* Destroy items if this menu own them. If this is the "brother" menu,
566 * leave them alone as it is shared by them.
568 if (!menu
->flags
.brother
) {
569 for (i
= 0; i
< menu
->entry_no
; i
++) {
571 wfree(menu
->entries
[i
]->text
);
573 if (menu
->entries
[i
]->rtext
)
574 wfree(menu
->entries
[i
]->rtext
);
577 if (menu
->entries
[i
]->instances
) {
578 WMReleasePropList(menu
->entries
[i
]->instances
);
580 #endif /* USER_MENU */
582 if (menu
->entries
[i
]->free_cdata
&& menu
->entries
[i
]->clientdata
) {
583 (*menu
->entries
[i
]->free_cdata
) (menu
->entries
[i
]->clientdata
);
585 wfree(menu
->entries
[i
]);
589 for (i
= 0; i
< menu
->cascade_no
; i
++) {
590 if (menu
->cascades
[i
]) {
591 if (menu
->cascades
[i
]->flags
.brother
)
592 wMenuDestroy(menu
->cascades
[i
]->brother
, recurse
);
594 wMenuDestroy(menu
->cascades
[i
], recurse
);
600 wfree(menu
->entries
);
604 FREE_PIXMAP(menu
->menu_texture_data
);
607 wfree(menu
->cascades
);
609 wCoreDestroy(menu
->menu
);
610 wFrameWindowDestroy(menu
->frame
);
612 /* destroy copy of this menu */
613 if (!menu
->flags
.brother
&& menu
->brother
)
614 wMenuDestroy(menu
->brother
, False
);
624 static void drawFrame(WScreen
* scr
, Drawable win
, int y
, int w
, int h
, int type
)
630 segs
[i
].x1
= segs
[i
].x2
= w
- 1;
632 segs
[i
].y2
= y
+ h
- 1;
634 if (type
!= F_TOP
&& type
!= F_NONE
) {
636 segs
[i
].y1
= segs
[i
].y2
= y
+ h
- 2;
640 XDrawSegments(dpy
, win
, scr
->menu_item_auxtexture
->dim_gc
, segs
, i
);
646 segs
[i
].y2
= y
+ h
- 1;
648 if (type
!= F_BOTTOM
&& type
!= F_NONE
) {
655 XDrawSegments(dpy
, win
, scr
->menu_item_auxtexture
->light_gc
, segs
, i
);
657 if (type
!= F_TOP
&& type
!= F_NONE
)
658 XDrawLine(dpy
, win
, scr
->menu_item_auxtexture
->dark_gc
, 0, y
+ h
- 1, w
- 1, y
+ h
- 1);
661 static void paintEntry(WMenu
* menu
, int index
, int selected
)
663 WScreen
*scr
= menu
->frame
->screen_ptr
;
664 Window win
= menu
->menu
->window
;
665 WMenuEntry
*entry
= menu
->entries
[index
];
671 if (!menu
->flags
.realized
)
673 h
= menu
->entry_height
;
674 w
= menu
->menu
->width
;
677 light
= scr
->menu_item_auxtexture
->light_gc
;
678 dim
= scr
->menu_item_auxtexture
->dim_gc
;
679 dark
= scr
->menu_item_auxtexture
->dark_gc
;
681 if (wPreferences
.menu_style
== MS_FLAT
&& menu
->entry_no
> 1) {
684 else if (index
== menu
->entry_no
- 1)
692 /* paint background */
694 XFillRectangle(dpy
, win
, WMColorGC(scr
->select_color
), 1, y
+ 1, w
- 2, h
- 3);
695 if (scr
->menu_item_texture
->any
.type
== WTEX_SOLID
)
696 drawFrame(scr
, win
, y
, w
, h
, type
);
698 if (scr
->menu_item_texture
->any
.type
== WTEX_SOLID
) {
699 XClearArea(dpy
, win
, 0, y
+ 1, w
- 1, h
- 3, False
);
701 drawFrame(scr
, win
, y
, w
, h
, type
);
703 XClearArea(dpy
, win
, 0, y
, w
, h
, False
);
708 if (entry
->flags
.enabled
)
709 color
= scr
->select_text_color
;
711 color
= scr
->dtext_color
;
712 } else if (!entry
->flags
.enabled
) {
713 color
= scr
->dtext_color
;
715 color
= scr
->mtext_color
;
719 if (entry
->flags
.indicator
)
720 x
+= MENU_INDICATOR_SPACE
+ 2;
722 WMDrawString(scr
->wmscreen
, win
, color
, scr
->menu_entry_font
,
723 x
, 3 + y
+ wPreferences
.menu_text_clearance
, entry
->text
, strlen(entry
->text
));
725 if (entry
->cascade
>= 0) {
726 /* draw the cascade indicator */
727 XDrawLine(dpy
, win
, dim
, w
- 11, y
+ 6, w
- 6, y
+ h
/ 2 - 1);
728 XDrawLine(dpy
, win
, light
, w
- 11, y
+ h
- 8, w
- 6, y
+ h
/ 2 - 1);
729 XDrawLine(dpy
, win
, dark
, w
- 12, y
+ 6, w
- 12, y
+ h
- 8);
733 if (entry
->flags
.indicator
&& entry
->flags
.indicator_on
) {
737 switch (entry
->flags
.indicator_type
) {
739 indicator
= scr
->menu_check_indicator
;
742 indicator
= scr
->menu_mini_indicator
;
745 indicator
= scr
->menu_hide_indicator
;
748 indicator
= scr
->menu_shade_indicator
;
752 indicator
= scr
->menu_radio_indicator
;
756 iw
= indicator
->width
;
757 ih
= indicator
->height
;
758 XSetClipMask(dpy
, scr
->copy_gc
, indicator
->mask
);
759 XSetClipOrigin(dpy
, scr
->copy_gc
, 5, y
+ (h
- ih
) / 2);
761 if (entry
->flags
.enabled
) {
762 XSetForeground(dpy
, scr
->copy_gc
, WMColorPixel(scr
->select_text_color
));
764 XSetForeground(dpy
, scr
->copy_gc
, WMColorPixel(scr
->dtext_color
));
767 if (entry
->flags
.enabled
) {
768 XSetForeground(dpy
, scr
->copy_gc
, WMColorPixel(scr
->mtext_color
));
770 XSetForeground(dpy
, scr
->copy_gc
, WMColorPixel(scr
->dtext_color
));
773 XFillRectangle(dpy
, win
, scr
->copy_gc
, 5, y
+ (h
- ih
) / 2, iw
, ih
);
775 XCopyArea(dpy, indicator->image, win, scr->copy_gc, 0, 0,
776 iw, ih, 5, y+(h-ih)/2);
778 XSetClipOrigin(dpy
, scr
->copy_gc
, 0, 0);
781 /* draw right text */
783 if (entry
->rtext
&& entry
->cascade
< 0) {
784 tw
= WMWidthOfString(scr
->menu_entry_font
, entry
->rtext
, strlen(entry
->rtext
));
786 WMDrawString(scr
->wmscreen
, win
, color
, scr
->menu_entry_font
, w
- 6 - tw
,
787 y
+ 3 + wPreferences
.menu_text_clearance
, entry
->rtext
, strlen(entry
->rtext
));
791 static void move_menus(WMenu
* menu
, int x
, int y
)
793 while (menu
->parent
) {
796 if (!wPreferences
.align_menus
&& menu
->selected_entry
>= 0) {
797 y
-= menu
->selected_entry
* menu
->entry_height
;
800 wMenuMove(menu
, x
, y
, True
);
803 static void makeVisible(WMenu
* menu
)
805 WScreen
*scr
= menu
->frame
->screen_ptr
;
806 int x1
, y1
, x2
, y2
, new_x
, new_y
;
807 WMRect rect
= wGetRectForHead(scr
, wGetHeadForPointerLocation(scr
));
809 if (menu
->entry_no
< 0)
813 y1
= menu
->frame_y
+ menu
->frame
->top_width
+ menu
->selected_entry
* menu
->entry_height
;
814 x2
= x1
+ MENUW(menu
);
815 y2
= y1
+ menu
->entry_height
;
820 if (x1
< rect
.pos
.x
) {
822 } else if (x2
>= rect
.pos
.x
+ rect
.size
.width
) {
823 new_x
= rect
.pos
.x
+ rect
.size
.width
- MENUW(menu
) - 1;
826 if (y1
< rect
.pos
.y
) {
828 } else if (y2
>= rect
.pos
.y
+ rect
.size
.height
) {
829 new_y
= rect
.pos
.y
+ rect
.size
.height
- menu
->entry_height
- 1;
832 new_y
= new_y
- menu
->frame
->top_width
- menu
->selected_entry
* menu
->entry_height
;
833 move_menus(menu
, new_x
, new_y
);
836 static int check_key(WMenu
* menu
, XKeyEvent
* event
)
841 if (XLookupString(event
, buffer
, 32, NULL
, NULL
) < 1)
844 ch
= toupper(buffer
[0]);
846 s
= (menu
->selected_entry
>= 0 ? menu
->selected_entry
+ 1 : 0);
849 for (i
= s
; i
< menu
->entry_no
; i
++) {
850 if (ch
== toupper(menu
->entries
[i
]->text
[0])) {
854 /* no match. Retry from start, if previous started from a selected entry */
862 static int keyboardMenu(WMenu
* menu
)
865 KeySym ksym
= NoSymbol
;
869 int old_pos_x
= menu
->frame_x
;
870 int old_pos_y
= menu
->frame_y
;
871 int new_x
= old_pos_x
, new_y
= old_pos_y
;
872 WMRect rect
= wGetRectForHead(menu
->frame
->screen_ptr
,
873 wGetHeadForPointerLocation(menu
->frame
->screen_ptr
));
875 if (menu
->flags
.editing
)
878 XGrabKeyboard(dpy
, menu
->frame
->core
->window
, True
, GrabModeAsync
, GrabModeAsync
, CurrentTime
);
880 if (menu
->frame_y
+ menu
->frame
->top_width
>= rect
.pos
.y
+ rect
.size
.height
)
881 new_y
= rect
.pos
.y
+ rect
.size
.height
- menu
->frame
->top_width
;
883 if (menu
->frame_x
+ MENUW(menu
) >= rect
.pos
.x
+ rect
.size
.width
)
884 new_x
= rect
.pos
.x
+ rect
.size
.width
- MENUW(menu
) - 1;
886 move_menus(menu
, new_x
, new_y
);
888 while (!done
&& menu
->flags
.mapped
) {
889 XAllowEvents(dpy
, AsyncKeyboard
, CurrentTime
);
890 WMMaskEvent(dpy
, ExposureMask
| ButtonMotionMask
| ButtonPressMask
891 | ButtonReleaseMask
| KeyPressMask
| KeyReleaseMask
| SubstructureNotifyMask
, &event
);
893 switch (event
.type
) {
895 ksym
= XLookupKeysym(&event
.xkey
, 0);
905 selectEntry(menu
, 0);
913 selectEntry(menu
, menu
->entry_no
- 1);
924 if (menu
->selected_entry
<= 0)
925 selectEntry(menu
, menu
->entry_no
- 1);
927 selectEntry(menu
, menu
->selected_entry
- 1);
938 if (menu
->selected_entry
< 0)
939 selectEntry(menu
, 0);
940 else if (menu
->selected_entry
== menu
->entry_no
- 1)
941 selectEntry(menu
, 0);
942 else if (menu
->selected_entry
< menu
->entry_no
- 1)
943 selectEntry(menu
, menu
->selected_entry
+ 1);
954 if (menu
->selected_entry
>= 0) {
956 entry
= menu
->entries
[menu
->selected_entry
];
958 if (entry
->cascade
>= 0 && menu
->cascades
959 && menu
->cascades
[entry
->cascade
]->entry_no
> 0) {
961 XUngrabKeyboard(dpy
, CurrentTime
);
963 selectEntry(menu
->cascades
[entry
->cascade
], 0);
964 if (!keyboardMenu(menu
->cascades
[entry
->cascade
]))
967 XGrabKeyboard(dpy
, menu
->frame
->core
->window
, True
,
968 GrabModeAsync
, GrabModeAsync
, CurrentTime
);
980 if (menu
->parent
!= NULL
&& menu
->parent
->selected_entry
>= 0) {
981 selectEntry(menu
, -1);
982 move_menus(menu
, old_pos_x
, old_pos_y
);
995 index
= check_key(menu
, &event
.xkey
);
997 selectEntry(menu
, index
);
1003 if (event
.type
== ButtonPress
)
1006 WMHandleEvent(&event
);
1010 XUngrabKeyboard(dpy
, CurrentTime
);
1012 if (done
== 2 && menu
->selected_entry
>= 0) {
1013 entry
= menu
->entries
[menu
->selected_entry
];
1018 if (entry
&& entry
->callback
!= NULL
&& entry
->flags
.enabled
&& entry
->cascade
< 0) {
1019 #if (MENU_BLINK_COUNT > 0)
1020 int sel
= menu
->selected_entry
;
1023 for (i
= 0; i
< MENU_BLINK_COUNT
; i
++) {
1024 paintEntry(menu
, sel
, False
);
1026 wusleep(MENU_BLINK_DELAY
);
1027 paintEntry(menu
, sel
, True
);
1029 wusleep(MENU_BLINK_DELAY
);
1032 selectEntry(menu
, -1);
1034 if (!menu
->flags
.buttoned
) {
1036 move_menus(menu
, old_pos_x
, old_pos_y
);
1040 (*entry
->callback
) (menu
, entry
);
1042 if (!menu
->flags
.buttoned
) {
1044 move_menus(menu
, old_pos_x
, old_pos_y
);
1046 selectEntry(menu
, -1);
1049 /* returns True if returning from a submenu to a parent menu,
1050 * False if exiting from menu */
1054 void wMenuMapAt(WMenu
* menu
, int x
, int y
, int keyboard
)
1056 WMRect rect
= wGetRectForHead(menu
->frame
->screen_ptr
,
1057 wGetHeadForPointerLocation(menu
->frame
->screen_ptr
));
1059 if (!menu
->flags
.realized
) {
1060 menu
->flags
.realized
= 1;
1063 if (!menu
->flags
.mapped
) {
1064 if (wPreferences
.wrap_menus
) {
1069 if (x
+ MENUW(menu
) > rect
.pos
.x
+ rect
.size
.width
)
1070 x
= rect
.pos
.x
+ rect
.size
.width
- MENUW(menu
);
1071 if (y
+ MENUH(menu
) > rect
.pos
.y
+ rect
.size
.height
)
1072 y
= rect
.pos
.y
+ rect
.size
.height
- MENUH(menu
);
1075 XMoveWindow(dpy
, menu
->frame
->core
->window
, x
, y
);
1078 XMapWindow(dpy
, menu
->frame
->core
->window
);
1079 wRaiseFrame(menu
->frame
->core
);
1080 menu
->flags
.mapped
= 1;
1082 selectEntry(menu
, 0);
1089 void wMenuMap(WMenu
* menu
)
1091 if (!menu
->flags
.realized
) {
1092 menu
->flags
.realized
= 1;
1095 if (menu
->flags
.app_menu
&& menu
->parent
== NULL
) {
1096 menu
->frame_x
= menu
->frame
->screen_ptr
->app_menu_x
;
1097 menu
->frame_y
= menu
->frame
->screen_ptr
->app_menu_y
;
1098 XMoveWindow(dpy
, menu
->frame
->core
->window
, menu
->frame_x
, menu
->frame_y
);
1100 XMapWindow(dpy
, menu
->frame
->core
->window
);
1101 wRaiseFrame(menu
->frame
->core
);
1102 menu
->flags
.mapped
= 1;
1105 void wMenuUnmap(WMenu
* menu
)
1109 XUnmapWindow(dpy
, menu
->frame
->core
->window
);
1110 if (menu
->flags
.titled
&& menu
->flags
.buttoned
) {
1111 wFrameWindowHideButton(menu
->frame
, WFF_RIGHT_BUTTON
);
1113 menu
->flags
.buttoned
= 0;
1114 menu
->flags
.mapped
= 0;
1115 menu
->flags
.open_to_left
= 0;
1117 for (i
= 0; i
< menu
->cascade_no
; i
++) {
1118 if (menu
->cascades
[i
] != NULL
1119 && menu
->cascades
[i
]->flags
.mapped
&& !menu
->cascades
[i
]->flags
.buttoned
) {
1121 wMenuUnmap(menu
->cascades
[i
]);
1124 menu
->selected_entry
= -1;
1127 void wMenuPaint(WMenu
* menu
)
1131 if (!menu
->flags
.mapped
) {
1136 for (i
= 0; i
< menu
->entry_no
; i
++) {
1137 paintEntry(menu
, i
, i
== menu
->selected_entry
);
1141 void wMenuSetEnabled(WMenu
* menu
, int index
, int enable
)
1143 if (index
>= menu
->entry_no
)
1145 menu
->entries
[index
]->flags
.enabled
= enable
;
1146 paintEntry(menu
, index
, index
== menu
->selected_entry
);
1147 paintEntry(menu
->brother
, index
, index
== menu
->selected_entry
);
1151 static void selectEntry(WMenu
* menu
, int entry_no
)
1157 if (menu
->entries
== NULL
)
1160 if (entry_no
>= menu
->entry_no
)
1163 old_entry
= menu
->selected_entry
;
1164 menu
->selected_entry
= entry_no
;
1166 if (old_entry
!= entry_no
) {
1168 /* unselect previous entry */
1169 if (old_entry
>= 0) {
1170 paintEntry(menu
, old_entry
, False
);
1171 entry
= menu
->entries
[old_entry
];
1174 if (entry
->cascade
>= 0 && menu
->cascades
) {
1175 if (!menu
->cascades
[entry
->cascade
]->flags
.buttoned
) {
1176 wMenuUnmap(menu
->cascades
[entry
->cascade
]);
1182 menu
->selected_entry
= -1;
1185 entry
= menu
->entries
[entry_no
];
1187 if (entry
->cascade
>= 0 && menu
->cascades
&& entry
->flags
.enabled
) {
1188 /* Callback for when the submenu is opened.
1190 submenu
= menu
->cascades
[entry
->cascade
];
1191 if (submenu
&& submenu
->flags
.brother
)
1192 submenu
= submenu
->brother
;
1194 if (entry
->callback
) {
1195 /* Only call the callback if the submenu is not yet mapped.
1197 if (menu
->flags
.brother
) {
1198 if (!submenu
|| !submenu
->flags
.mapped
)
1199 (*entry
->callback
) (menu
->brother
, entry
);
1201 if (!submenu
|| !submenu
->flags
.buttoned
)
1202 (*entry
->callback
) (menu
, entry
);
1206 /* the submenu menu might have changed */
1207 submenu
= menu
->cascades
[entry
->cascade
];
1210 if (!submenu
->flags
.mapped
) {
1213 if (!submenu
->flags
.realized
)
1214 wMenuRealize(submenu
);
1215 if (wPreferences
.wrap_menus
) {
1216 if (menu
->flags
.open_to_left
)
1217 submenu
->flags
.open_to_left
= 1;
1219 if (submenu
->flags
.open_to_left
) {
1220 x
= menu
->frame_x
- MENUW(submenu
);
1223 submenu
->flags
.open_to_left
= 0;
1226 x
= menu
->frame_x
+ MENUW(menu
);
1228 if (x
+ MENUW(submenu
)
1229 >= menu
->frame
->screen_ptr
->scr_width
) {
1231 x
= menu
->frame_x
- MENUW(submenu
);
1232 submenu
->flags
.open_to_left
= 1;
1236 x
= menu
->frame_x
+ MENUW(menu
);
1239 if (wPreferences
.align_menus
) {
1242 y
= menu
->frame_y
+ menu
->entry_height
* entry_no
;
1243 if (menu
->flags
.titled
)
1244 y
+= menu
->frame
->top_width
;
1245 if (menu
->cascades
[entry
->cascade
]->flags
.titled
)
1246 y
-= menu
->cascades
[entry
->cascade
]->frame
->top_width
;
1249 wMenuMapAt(menu
->cascades
[entry
->cascade
], x
, y
, False
);
1250 menu
->cascades
[entry
->cascade
]->parent
= menu
;
1255 paintEntry(menu
, entry_no
, True
);
1259 static WMenu
*findMenu(WScreen
* scr
, int *x_ret
, int *y_ret
)
1262 WObjDescriptor
*desc
;
1263 Window root_ret
, win
, junk_win
;
1267 XQueryPointer(dpy
, scr
->root_win
, &root_ret
, &win
, &x
, &y
, &wx
, &wy
, &mask
);
1272 if (XFindContext(dpy
, win
, wWinContext
, (XPointer
*) & desc
) == XCNOENT
)
1275 if (desc
->parent_type
== WCLASS_MENU
) {
1276 menu
= (WMenu
*) desc
->parent
;
1277 XTranslateCoordinates(dpy
, root_ret
, menu
->menu
->window
, wx
, wy
, x_ret
, y_ret
, &junk_win
);
1283 static void closeCascade(WMenu
* menu
)
1285 WMenu
*parent
= menu
->parent
;
1287 if (menu
->flags
.brother
|| (!menu
->flags
.buttoned
&& (!menu
->flags
.app_menu
|| menu
->parent
!= NULL
))) {
1289 selectEntry(menu
, -1);
1291 #if (MENU_BLINK_DELAY > 2)
1292 wusleep(MENU_BLINK_DELAY
/ 2);
1295 while (parent
!= NULL
1296 && (parent
->parent
!= NULL
|| !parent
->flags
.app_menu
|| parent
->flags
.brother
)
1297 && !parent
->flags
.buttoned
) {
1298 selectEntry(parent
, -1);
1300 parent
= parent
->parent
;
1303 selectEntry(parent
, -1);
1307 static void closeBrotherCascadesOf(WMenu
* menu
)
1312 for (i
= 0; i
< menu
->cascade_no
; i
++) {
1313 if (menu
->cascades
[i
]->flags
.brother
) {
1314 tmp
= menu
->cascades
[i
];
1316 tmp
= menu
->cascades
[i
]->brother
;
1318 if (tmp
->flags
.mapped
) {
1319 selectEntry(tmp
->parent
, -1);
1320 closeBrotherCascadesOf(tmp
);
1326 #define getEntryAt(menu, x, y) ((y)<0 ? -1 : (y)/(menu->entry_height))
1328 static WMenu
*parentMenu(WMenu
* menu
)
1333 if (menu
->flags
.buttoned
)
1336 while (menu
->parent
&& menu
->parent
->flags
.mapped
) {
1337 parent
= menu
->parent
;
1338 if (parent
->selected_entry
< 0)
1340 entry
= parent
->entries
[parent
->selected_entry
];
1341 if (!entry
->flags
.enabled
|| entry
->cascade
< 0 || !parent
->cascades
||
1342 parent
->cascades
[entry
->cascade
] != menu
)
1345 if (menu
->flags
.buttoned
)
1353 * Will raise the passed menu, if submenu = 0
1354 * If submenu > 0 will also raise all mapped submenus
1355 * until the first buttoned one
1356 * If submenu < 0 will also raise all mapped parent menus
1357 * until the first buttoned one
1360 static void raiseMenus(WMenu
* menu
, int submenus
)
1368 wRaiseFrame(menu
->frame
->core
);
1370 if (submenus
> 0 && menu
->selected_entry
>= 0) {
1371 i
= menu
->entries
[menu
->selected_entry
]->cascade
;
1372 if (i
>= 0 && menu
->cascades
) {
1373 submenu
= menu
->cascades
[i
];
1374 if (submenu
->flags
.mapped
&& !submenu
->flags
.buttoned
)
1375 raiseMenus(submenu
, submenus
);
1378 if (submenus
< 0 && !menu
->flags
.buttoned
&& menu
->parent
&& menu
->parent
->flags
.mapped
)
1379 raiseMenus(menu
->parent
, submenus
);
1382 WMenu
*wMenuUnderPointer(WScreen
* screen
)
1384 WObjDescriptor
*desc
;
1385 Window root_ret
, win
;
1389 XQueryPointer(dpy
, screen
->root_win
, &root_ret
, &win
, &dummy
, &dummy
, &dummy
, &dummy
, &mask
);
1394 if (XFindContext(dpy
, win
, wWinContext
, (XPointer
*) & desc
) == XCNOENT
)
1397 if (desc
->parent_type
== WCLASS_MENU
)
1398 return (WMenu
*) desc
->parent
;
1402 static void getPointerPosition(WScreen
* scr
, int *x
, int *y
)
1404 Window root_ret
, win
;
1408 XQueryPointer(dpy
, scr
->root_win
, &root_ret
, &win
, x
, y
, &wx
, &wy
, &mask
);
1411 static void getScrollAmount(WMenu
* menu
, int *hamount
, int *vamount
)
1413 WScreen
*scr
= menu
->menu
->screen_ptr
;
1414 int menuX1
= menu
->frame_x
;
1415 int menuY1
= menu
->frame_y
;
1416 int menuX2
= menu
->frame_x
+ MENUW(menu
);
1417 int menuY2
= menu
->frame_y
+ MENUH(menu
);
1419 WMRect rect
= wGetRectForHead(scr
, wGetHeadForPointerLocation(scr
));
1424 getPointerPosition(scr
, &xroot
, &yroot
);
1426 if (xroot
<= (rect
.pos
.x
+ 1) && menuX1
< rect
.pos
.x
) {
1427 /* scroll to the right */
1428 *hamount
= WMIN(MENU_SCROLL_STEP
, abs(menuX1
));
1430 } else if (xroot
>= (rect
.pos
.x
+ rect
.size
.width
- 2) && menuX2
> (rect
.pos
.x
+ rect
.size
.width
- 1)) {
1431 /* scroll to the left */
1432 *hamount
= WMIN(MENU_SCROLL_STEP
, abs(menuX2
- rect
.pos
.x
- rect
.size
.width
- 1));
1437 *hamount
= -*hamount
;
1440 if (yroot
<= (rect
.pos
.y
+ 1) && menuY1
< rect
.pos
.y
) {
1442 *vamount
= WMIN(MENU_SCROLL_STEP
, abs(menuY1
));
1444 } else if (yroot
>= (rect
.pos
.y
+ rect
.size
.height
- 2) && menuY2
> (rect
.pos
.y
+ rect
.size
.height
- 1)) {
1446 *vamount
= WMIN(MENU_SCROLL_STEP
, abs(menuY2
- rect
.pos
.y
- rect
.size
.height
- 2));
1448 *vamount
= -*vamount
;
1452 static void dragScrollMenuCallback(void *data
)
1454 WMenu
*menu
= (WMenu
*) data
;
1455 WScreen
*scr
= menu
->menu
->screen_ptr
;
1456 WMenu
*parent
= parentMenu(menu
);
1457 int hamount
, vamount
;
1459 int newSelectedEntry
;
1461 getScrollAmount(menu
, &hamount
, &vamount
);
1463 if (hamount
!= 0 || vamount
!= 0) {
1464 wMenuMove(parent
, parent
->frame_x
+ hamount
, parent
->frame_y
+ vamount
, True
);
1465 if (findMenu(scr
, &x
, &y
)) {
1466 newSelectedEntry
= getEntryAt(menu
, x
, y
);
1467 selectEntry(menu
, newSelectedEntry
);
1469 /* Pointer fell outside of menu. If the selected entry is
1470 * not a submenu, unselect it */
1471 if (menu
->selected_entry
>= 0 && menu
->entries
[menu
->selected_entry
]->cascade
< 0)
1472 selectEntry(menu
, -1);
1473 newSelectedEntry
= 0;
1476 /* paranoid check */
1477 if (newSelectedEntry
>= 0) {
1478 /* keep scrolling */
1479 menu
->timer
= WMAddTimerHandler(MENU_SCROLL_DELAY
, dragScrollMenuCallback
, menu
);
1484 /* don't need to scroll anymore */
1486 if (findMenu(scr
, &x
, &y
)) {
1487 newSelectedEntry
= getEntryAt(menu
, x
, y
);
1488 selectEntry(menu
, newSelectedEntry
);
1493 static void scrollMenuCallback(void *data
)
1495 WMenu
*menu
= (WMenu
*) data
;
1496 WMenu
*parent
= parentMenu(menu
);
1497 int hamount
= 0; /* amount to scroll */
1500 getScrollAmount(menu
, &hamount
, &vamount
);
1502 if (hamount
!= 0 || vamount
!= 0) {
1503 wMenuMove(parent
, parent
->frame_x
+ hamount
, parent
->frame_y
+ vamount
, True
);
1505 /* keep scrolling */
1506 menu
->timer
= WMAddTimerHandler(MENU_SCROLL_DELAY
, scrollMenuCallback
, menu
);
1508 /* don't need to scroll anymore */
1513 #define MENU_SCROLL_BORDER 5
1515 static int isPointNearBoder(WMenu
* menu
, int x
, int y
)
1517 int menuX1
= menu
->frame_x
;
1518 int menuY1
= menu
->frame_y
;
1519 int menuX2
= menu
->frame_x
+ MENUW(menu
);
1520 int menuY2
= menu
->frame_y
+ MENUH(menu
);
1522 int head
= wGetHeadForPoint(menu
->frame
->screen_ptr
, wmkpoint(x
, y
));
1523 WMRect rect
= wGetRectForHead(menu
->frame
->screen_ptr
, head
);
1525 /* XXX: handle screen joins properly !! */
1527 if (x
>= menuX1
&& x
<= menuX2
&&
1528 (y
< rect
.pos
.y
+ MENU_SCROLL_BORDER
|| y
>= rect
.pos
.y
+ rect
.size
.height
- MENU_SCROLL_BORDER
))
1530 else if (y
>= menuY1
&& y
<= menuY2
&&
1531 (x
< rect
.pos
.x
+ MENU_SCROLL_BORDER
|| x
>= rect
.pos
.x
+ rect
.size
.width
- MENU_SCROLL_BORDER
))
1537 typedef struct _delay
{
1542 static void leaving(_delay
* dl
)
1544 wMenuMove(dl
->menu
, dl
->ox
, dl
->oy
, True
);
1545 dl
->menu
->jump_back
= NULL
;
1546 dl
->menu
->menu
->screen_ptr
->flags
.jump_back_pending
= 0;
1550 void wMenuScroll(WMenu
* menu
, XEvent
* event
)
1553 WMenu
*omenu
= parentMenu(menu
);
1554 WScreen
*scr
= menu
->frame
->screen_ptr
;
1557 int old_frame_x
= omenu
->frame_x
;
1558 int old_frame_y
= omenu
->frame_y
;
1561 if (omenu
->jump_back
)
1562 WMDeleteTimerWithClientData(omenu
->jump_back
);
1564 if (( /*omenu->flags.buttoned && */ !wPreferences
.wrap_menus
)
1565 || omenu
->flags
.app_menu
) {
1569 if (!wPreferences
.wrap_menus
)
1570 raiseMenus(omenu
, True
);
1572 raiseMenus(menu
, False
);
1575 scrollMenuCallback(menu
);
1578 int x
, y
, on_border
, on_x_edge
, on_y_edge
, on_title
;
1581 WMNextEvent(dpy
, &ev
);
1586 x
= (ev
.type
== MotionNotify
) ? ev
.xmotion
.x_root
: ev
.xcrossing
.x_root
;
1587 y
= (ev
.type
== MotionNotify
) ? ev
.xmotion
.y_root
: ev
.xcrossing
.y_root
;
1589 /* on_border is != 0 if the pointer is between the menu
1590 * and the screen border and is close enough to the border */
1591 on_border
= isPointNearBoder(menu
, x
, y
);
1593 smenu
= wMenuUnderPointer(scr
);
1595 if ((smenu
== NULL
&& !on_border
) || (smenu
&& parentMenu(smenu
) != omenu
)) {
1600 rect
= wGetRectForHead(scr
, wGetHeadForPoint(scr
, wmkpoint(x
, y
)));
1601 on_x_edge
= x
<= rect
.pos
.x
+ 1 || x
>= rect
.pos
.x
+ rect
.size
.width
- 2;
1602 on_y_edge
= y
<= rect
.pos
.y
+ 1 || y
>= rect
.pos
.y
+ rect
.size
.height
- 2;
1603 on_border
= on_x_edge
|| on_y_edge
;
1605 if (!on_border
&& !jump_back
) {
1610 if (menu
->timer
&& (smenu
!= menu
|| (!on_y_edge
&& !on_x_edge
))) {
1611 WMDeleteTimerHandler(menu
->timer
);
1619 scrollMenuCallback(menu
);
1622 /* True if we push on title, or drag the omenu to other position */
1623 on_title
= ev
.xbutton
.x_root
>= omenu
->frame_x
&&
1624 ev
.xbutton
.x_root
<= omenu
->frame_x
+ MENUW(omenu
) &&
1625 ev
.xbutton
.y_root
>= omenu
->frame_y
&&
1626 ev
.xbutton
.y_root
<= omenu
->frame_y
+ omenu
->frame
->top_width
;
1628 smenu
= wMenuUnderPointer(scr
);
1629 if (smenu
== NULL
|| (smenu
&& smenu
->flags
.buttoned
&& smenu
!= omenu
))
1631 else if (smenu
== omenu
&& on_title
) {
1645 WMDeleteTimerHandler(menu
->timer
);
1651 if (!omenu
->jump_back
) {
1652 delayer
= wmalloc(sizeof(_delay
));
1653 delayer
->menu
= omenu
;
1654 delayer
->ox
= old_frame_x
;
1655 delayer
->oy
= old_frame_y
;
1656 omenu
->jump_back
= delayer
;
1657 scr
->flags
.jump_back_pending
= 1;
1659 delayer
= omenu
->jump_back
;
1660 WMAddTimerHandler(MENU_JUMP_BACK_DELAY
, (WMCallback
*) leaving
, delayer
);
1664 static void menuExpose(WObjDescriptor
* desc
, XEvent
* event
)
1666 wMenuPaint(desc
->parent
);
1670 int *delayed_select
;
1675 static void delaySelection(void *data
)
1677 delay_data
*d
= (delay_data
*) data
;
1683 menu
= findMenu(d
->menu
->menu
->screen_ptr
, &x
, &y
);
1684 if (menu
&& (d
->menu
== menu
|| d
->delayed_select
)) {
1685 entry_no
= getEntryAt(menu
, x
, y
);
1686 selectEntry(menu
, entry_no
);
1688 if (d
->delayed_select
)
1689 *(d
->delayed_select
) = 0;
1692 static void menuMouseDown(WObjDescriptor
* desc
, XEvent
* event
)
1695 XButtonEvent
*bev
= &event
->xbutton
;
1696 WMenu
*menu
= desc
->parent
;
1698 WScreen
*scr
= menu
->frame
->screen_ptr
;
1699 WMenuEntry
*entry
= NULL
;
1701 int close_on_exit
= 0;
1703 int delayed_select
= 0;
1707 int old_frame_x
= 0;
1708 int old_frame_y
= 0;
1709 delay_data d_data
= { NULL
, NULL
, NULL
};
1711 /* Doesn't seem to be needed anymore (if delayed selection handler is
1712 * added only if not present). there seem to be no other side effects
1713 * from removing this and it is also possible that it was only added
1714 * to avoid problems with adding the delayed selection timer handler
1717 /*if (menu->flags.inside_handler) {
1720 menu
->flags
.inside_handler
= 1;
1722 if (!wPreferences
.wrap_menus
) {
1723 smenu
= parentMenu(menu
);
1724 old_frame_x
= smenu
->frame_x
;
1725 old_frame_y
= smenu
->frame_y
;
1726 } else if (event
->xbutton
.window
== menu
->frame
->core
->window
) {
1727 /* This is true if the menu was launched with right click on root window */
1728 if (!d_data
.magic
) {
1730 d_data
.delayed_select
= &delayed_select
;
1732 d_data
.magic
= WMAddTimerHandler(wPreferences
.dblclick_time
, delaySelection
, &d_data
);
1736 wRaiseFrame(menu
->frame
->core
);
1738 close_on_exit
= (bev
->send_event
|| menu
->flags
.brother
);
1740 smenu
= findMenu(scr
, &x
, &y
);
1748 if (menu
->flags
.editing
) {
1751 entry_no
= getEntryAt(menu
, x
, y
);
1752 if (entry_no
>= 0) {
1753 entry
= menu
->entries
[entry_no
];
1755 if (!close_on_exit
&& (bev
->state
& ControlMask
) && smenu
&& entry
->flags
.editable
) {
1758 int number
= entry_no
- 2; /* Entries "New" and "Destroy Last" appear before workspaces */
1760 name
= wstrdup(scr
->workspaces
[number
]->name
);
1761 snprintf(buffer
, sizeof(buffer
), _("Type the name for workspace %i:"), number
+ 1);
1763 wMenuUnmap(scr
->root_menu
);
1765 if (wInputDialog(scr
, _("Rename Workspace"), buffer
, &name
))
1766 wWorkspaceRename(scr
, number
, name
);
1772 } else if (bev
->state
& ControlMask
) {
1776 if (entry
->flags
.enabled
&& entry
->cascade
>= 0 && menu
->cascades
) {
1777 WMenu
*submenu
= menu
->cascades
[entry
->cascade
];
1779 if (submenu
->flags
.mapped
&& !submenu
->flags
.buttoned
&& menu
->selected_entry
!= entry_no
) {
1780 wMenuUnmap(submenu
);
1782 if (!submenu
->flags
.mapped
&& !delayed_select
) {
1783 selectEntry(menu
, entry_no
);
1784 } else if (!submenu
->flags
.buttoned
) {
1785 selectEntry(menu
, -1);
1788 } else if (!delayed_select
) {
1789 if (menu
== scr
->switch_menu
&& event
->xbutton
.button
== Button3
) {
1790 selectEntry(menu
, entry_no
);
1791 OpenWindowMenu2((WWindow
*)entry
->clientdata
,
1792 event
->xbutton
.x_root
,
1793 event
->xbutton
.y_root
, False
);
1794 wwin
= (WWindow
*)entry
->clientdata
;
1795 desc
= &wwin
->screen_ptr
->window_menu
->menu
->descriptor
;
1796 event
->xany
.send_event
= True
;
1797 (*desc
->handle_mousedown
)(desc
, event
);
1799 XUngrabPointer(dpy
, CurrentTime
);
1800 selectEntry(menu
, -1);
1803 selectEntry(menu
, entry_no
);
1807 if (!wPreferences
.wrap_menus
&& !wPreferences
.scrollable_menus
) {
1809 dragScrollMenuCallback(menu
);
1813 prevx
= bev
->x_root
;
1814 prevy
= bev
->y_root
;
1818 XAllowEvents(dpy
, AsyncPointer
| SyncPointer
, CurrentTime
);
1820 WMMaskEvent(dpy
, ExposureMask
| ButtonMotionMask
| ButtonReleaseMask
| ButtonPressMask
, &ev
);
1823 smenu
= findMenu(scr
, &x
, &y
);
1825 if (smenu
== NULL
) {
1826 /* moved mouse out of menu */
1828 if (!delayed_select
&& d_data
.magic
) {
1829 WMDeleteTimerHandler(d_data
.magic
);
1830 d_data
.magic
= NULL
;
1833 || (menu
->selected_entry
>= 0
1834 && menu
->entries
[menu
->selected_entry
]->cascade
>= 0)) {
1835 prevx
= ev
.xmotion
.x_root
;
1836 prevy
= ev
.xmotion
.y_root
;
1840 selectEntry(menu
, -1);
1842 prevx
= ev
.xmotion
.x_root
;
1843 prevy
= ev
.xmotion
.y_root
;
1845 } else if (menu
&& menu
!= smenu
1846 && (menu
->selected_entry
< 0
1847 || menu
->entries
[menu
->selected_entry
]->cascade
< 0)) {
1848 selectEntry(menu
, -1);
1850 if (!delayed_select
&& d_data
.magic
) {
1851 WMDeleteTimerHandler(d_data
.magic
);
1852 d_data
.magic
= NULL
;
1856 /* hysteresis for item selection */
1858 /* check if the motion was to the side, indicating that
1859 * the user may want to cross to a submenu */
1860 if (!delayed_select
&& menu
) {
1862 Bool moved_to_submenu
; /* moved to direction of submenu */
1864 dx
= abs(prevx
- ev
.xmotion
.x_root
);
1866 moved_to_submenu
= False
;
1867 if (dx
> 0 /* if moved enough to the side */
1868 /* maybe a open submenu */
1869 && menu
->selected_entry
>= 0
1870 /* moving to the right direction */
1871 && (wPreferences
.align_menus
|| ev
.xmotion
.y_root
>= prevy
)) {
1874 index
= menu
->entries
[menu
->selected_entry
]->cascade
;
1876 if (menu
->cascades
[index
]->frame_x
> menu
->frame_x
) {
1877 if (prevx
< ev
.xmotion
.x_root
)
1878 moved_to_submenu
= True
;
1880 if (prevx
> ev
.xmotion
.x_root
)
1881 moved_to_submenu
= True
;
1886 if (menu
!= smenu
) {
1888 WMDeleteTimerHandler(d_data
.magic
);
1889 d_data
.magic
= NULL
;
1891 } else if (moved_to_submenu
) {
1892 /* while we are moving, postpone the selection */
1894 WMDeleteTimerHandler(d_data
.magic
);
1896 d_data
.delayed_select
= NULL
;
1898 d_data
.magic
= WMAddTimerHandler(MENU_SELECT_DELAY
,
1899 delaySelection
, &d_data
);
1900 prevx
= ev
.xmotion
.x_root
;
1901 prevy
= ev
.xmotion
.y_root
;
1905 WMDeleteTimerHandler(d_data
.magic
);
1906 d_data
.magic
= NULL
;
1911 prevx
= ev
.xmotion
.x_root
;
1912 prevy
= ev
.xmotion
.y_root
;
1913 if (menu
!= smenu
) {
1914 /* pointer crossed menus */
1915 if (menu
&& menu
->timer
) {
1916 WMDeleteTimerHandler(menu
->timer
);
1920 dragScrollMenuCallback(smenu
);
1924 dragScrollMenuCallback(menu
);
1926 if (!delayed_select
) {
1927 entry_no
= getEntryAt(menu
, x
, y
);
1928 if (entry_no
>= 0) {
1929 entry
= menu
->entries
[entry_no
];
1930 if (entry
->flags
.enabled
&& entry
->cascade
>= 0 && menu
->cascades
) {
1931 WMenu
*submenu
= menu
->cascades
[entry
->cascade
];
1932 if (submenu
->flags
.mapped
&& !submenu
->flags
.buttoned
1933 && menu
->selected_entry
!= entry_no
) {
1934 wMenuUnmap(submenu
);
1938 selectEntry(menu
, entry_no
);
1946 if (ev
.xbutton
.button
== event
->xbutton
.button
)
1956 if (menu
&& menu
->timer
) {
1957 WMDeleteTimerHandler(menu
->timer
);
1960 if (d_data
.magic
!= NULL
) {
1961 WMDeleteTimerHandler(d_data
.magic
);
1962 d_data
.magic
= NULL
;
1965 if (menu
&& menu
->selected_entry
>= 0) {
1966 entry
= menu
->entries
[menu
->selected_entry
];
1967 if (entry
->callback
!= NULL
&& entry
->flags
.enabled
&& entry
->cascade
< 0) {
1968 /* blink and erase menu selection */
1969 #if (MENU_BLINK_DELAY > 0)
1970 int sel
= menu
->selected_entry
;
1973 for (i
= 0; i
< MENU_BLINK_COUNT
; i
++) {
1974 paintEntry(menu
, sel
, False
);
1976 wusleep(MENU_BLINK_DELAY
);
1977 paintEntry(menu
, sel
, True
);
1979 wusleep(MENU_BLINK_DELAY
);
1982 /* unmap the menu, it's parents and call the callback */
1983 if (!menu
->flags
.buttoned
&& (!menu
->flags
.app_menu
|| menu
->parent
!= NULL
)) {
1986 selectEntry(menu
, -1);
1988 (*entry
->callback
) (menu
, entry
);
1990 /* If the user double clicks an entry, the entry will
1991 * be executed twice, which is not good for things like
1992 * the root menu. So, ignore any clicks that were generated
1993 * while the entry was being executed */
1994 while (XCheckTypedWindowEvent(dpy
, menu
->menu
->window
, ButtonPress
, &ev
)) ;
1995 } else if (entry
->callback
!= NULL
&& entry
->cascade
< 0) {
1996 selectEntry(menu
, -1);
1998 if (entry
->cascade
>= 0 && menu
->cascades
&& menu
->cascades
[entry
->cascade
]->flags
.brother
) {
1999 selectEntry(menu
, -1);
2004 if (((WMenu
*) desc
->parent
)->flags
.brother
|| close_on_exit
|| !smenu
)
2005 closeCascade(desc
->parent
);
2007 /* close the cascade windows that should not remain opened */
2008 closeBrotherCascadesOf(desc
->parent
);
2010 if (!wPreferences
.wrap_menus
)
2011 wMenuMove(parentMenu(desc
->parent
), old_frame_x
, old_frame_y
, True
);
2014 /* Just to be sure in case we skip the 2 above because of a goto byebye */
2015 if (menu
&& menu
->timer
) {
2016 WMDeleteTimerHandler(menu
->timer
);
2019 if (d_data
.magic
!= NULL
) {
2020 WMDeleteTimerHandler(d_data
.magic
);
2021 d_data
.magic
= NULL
;
2024 ((WMenu
*) desc
->parent
)->flags
.inside_handler
= 0;
2027 void wMenuMove(WMenu
* menu
, int x
, int y
, int submenus
)
2037 XMoveWindow(dpy
, menu
->frame
->core
->window
, x
, y
);
2039 if (submenus
> 0 && menu
->selected_entry
>= 0) {
2040 i
= menu
->entries
[menu
->selected_entry
]->cascade
;
2042 if (i
>= 0 && menu
->cascades
) {
2043 submenu
= menu
->cascades
[i
];
2044 if (submenu
->flags
.mapped
&& !submenu
->flags
.buttoned
) {
2045 if (wPreferences
.align_menus
) {
2046 wMenuMove(submenu
, x
+ MENUW(menu
), y
, submenus
);
2048 wMenuMove(submenu
, x
+ MENUW(menu
),
2049 y
+ submenu
->entry_height
* menu
->selected_entry
, submenus
);
2054 if (submenus
< 0 && menu
->parent
!= NULL
&& menu
->parent
->flags
.mapped
&& !menu
->parent
->flags
.buttoned
) {
2055 if (wPreferences
.align_menus
) {
2056 wMenuMove(menu
->parent
, x
- MENUW(menu
->parent
), y
, submenus
);
2058 wMenuMove(menu
->parent
, x
- MENUW(menu
->parent
), menu
->frame_y
2059 - menu
->parent
->entry_height
* menu
->parent
->selected_entry
, submenus
);
2064 static void changeMenuLevels(WMenu
* menu
, int lower
)
2069 ChangeStackingLevel(menu
->frame
->core
, (!menu
->parent
? WMMainMenuLevel
: WMSubmenuLevel
));
2070 wRaiseFrame(menu
->frame
->core
);
2071 menu
->flags
.lowered
= 0;
2073 ChangeStackingLevel(menu
->frame
->core
, WMNormalLevel
);
2074 wLowerFrame(menu
->frame
->core
);
2075 menu
->flags
.lowered
= 1;
2077 for (i
= 0; i
< menu
->cascade_no
; i
++) {
2078 if (menu
->cascades
[i
]
2079 && !menu
->cascades
[i
]->flags
.buttoned
&& menu
->cascades
[i
]->flags
.lowered
!= lower
) {
2080 changeMenuLevels(menu
->cascades
[i
], lower
);
2085 static void menuTitleDoubleClick(WCoreWindow
* sender
, void *data
, XEvent
* event
)
2090 if (event
->xbutton
.state
& MOD_MASK
) {
2091 if (menu
->flags
.lowered
) {
2096 changeMenuLevels(menu
, lower
);
2100 static void menuTitleMouseDown(WCoreWindow
* sender
, void *data
, XEvent
* event
)
2105 int x
= menu
->frame_x
, y
= menu
->frame_y
;
2106 int dx
= event
->xbutton
.x_root
, dy
= event
->xbutton
.y_root
;
2110 /* can't touch the menu copy */
2111 if (menu
->flags
.brother
)
2114 if (event
->xbutton
.button
!= Button1
&& event
->xbutton
.button
!= Button2
)
2117 if (event
->xbutton
.state
& MOD_MASK
) {
2118 wLowerFrame(menu
->frame
->core
);
2121 wRaiseFrame(menu
->frame
->core
);
2126 /* lower/raise all submenus */
2128 if (tmp
->selected_entry
>= 0 && tmp
->cascades
&& tmp
->entries
[tmp
->selected_entry
]->cascade
>= 0) {
2129 tmp
= tmp
->cascades
[tmp
->entries
[tmp
->selected_entry
]->cascade
];
2130 if (!tmp
|| !tmp
->flags
.mapped
)
2133 wLowerFrame(tmp
->frame
->core
);
2135 wRaiseFrame(tmp
->frame
->core
);
2141 /* tear off the menu if it's a root menu or a cascade
2143 if (!menu
->flags
.buttoned
&& !menu
->flags
.brother
&& (!menu
->flags
.app_menu
|| menu
->parent
!= NULL
)) {
2144 menu
->flags
.buttoned
= 1;
2145 wFrameWindowShowButton(menu
->frame
, WFF_RIGHT_BUTTON
);
2147 /* turn off selected menu entry in parent menu */
2148 selectEntry(menu
->parent
, -1);
2150 /* make parent map the copy in place of the original */
2151 for (i
= 0; i
< menu
->parent
->cascade_no
; i
++) {
2152 if (menu
->parent
->cascades
[i
] == menu
) {
2153 menu
->parent
->cascades
[i
] = menu
->brother
;
2162 WMMaskEvent(dpy
, ButtonMotionMask
| ButtonReleaseMask
| ButtonPressMask
| ExposureMask
, &ev
);
2166 x
+= ev
.xmotion
.x_root
- dx
;
2167 y
+= ev
.xmotion
.y_root
- dy
;
2168 dx
= ev
.xmotion
.x_root
;
2169 dy
= ev
.xmotion
.y_root
;
2170 wMenuMove(menu
, x
, y
, True
);
2172 if (abs(ev
.xmotion
.x_root
- dx
) > MOVE_THRESHOLD
2173 || abs(ev
.xmotion
.y_root
- dy
) > MOVE_THRESHOLD
) {
2175 XGrabPointer(dpy
, menu
->frame
->titlebar
->window
, False
,
2176 ButtonMotionMask
| ButtonReleaseMask
2178 GrabModeAsync
, GrabModeAsync
, None
,
2179 wCursor
[WCUR_MOVE
], CurrentTime
);
2188 if (ev
.xbutton
.button
!= event
->xbutton
.button
)
2190 XUngrabPointer(dpy
, CurrentTime
);
2201 *----------------------------------------------------------------------
2203 * Handles mouse click on the close button of menus. The menu is
2204 * closed when the button is clicked.
2207 * The closed menu is reinserted at it's parent menus
2209 *----------------------------------------------------------------------
2211 static void menuCloseClick(WCoreWindow
* sender
, void *data
, XEvent
* event
)
2213 WMenu
*menu
= (WMenu
*) data
;
2214 WMenu
*parent
= menu
->parent
;
2218 for (i
= 0; i
< parent
->cascade_no
; i
++) {
2219 /* find the entry that points to the copy */
2220 if (parent
->cascades
[i
] == menu
->brother
) {
2221 /* make it point to the original */
2222 parent
->cascades
[i
] = menu
;
2223 menu
->parent
= parent
;
2231 static void saveMenuInfo(WMPropList
* dict
, WMenu
* menu
, WMPropList
* key
)
2233 WMPropList
*value
, *list
;
2236 snprintf(buffer
, sizeof(buffer
), "%i,%i", menu
->frame_x
, menu
->frame_y
);
2237 value
= WMCreatePLString(buffer
);
2238 list
= WMCreatePLArray(value
, NULL
);
2239 if (menu
->flags
.lowered
)
2240 WMAddToPLArray(list
, WMCreatePLString("lowered"));
2241 WMPutInPLDictionary(dict
, key
, list
);
2242 WMReleasePropList(value
);
2243 WMReleasePropList(list
);
2246 void wMenuSaveState(WScreen
* scr
)
2248 WMPropList
*menus
, *key
;
2251 menus
= WMCreatePLDictionary(NULL
, NULL
);
2253 if (scr
->switch_menu
&& scr
->switch_menu
->flags
.buttoned
) {
2254 key
= WMCreatePLString("SwitchMenu");
2255 saveMenuInfo(menus
, scr
->switch_menu
, key
);
2256 WMReleasePropList(key
);
2260 if (saveMenuRecurs(menus
, scr
, scr
->root_menu
))
2263 if (scr
->workspace_menu
&& scr
->workspace_menu
->flags
.buttoned
) {
2264 key
= WMCreatePLString("WorkspaceMenu");
2265 saveMenuInfo(menus
, scr
->workspace_menu
, key
);
2266 WMReleasePropList(key
);
2271 key
= WMCreatePLString("Menus");
2272 WMPutInPLDictionary(scr
->session_state
, key
, menus
);
2273 WMReleasePropList(key
);
2275 WMReleasePropList(menus
);
2278 static Bool
getMenuPath(WMenu
* menu
, char *buffer
, int bufSize
)
2283 if (!menu
->flags
.titled
|| !menu
->frame
->title
[0])
2286 len
= strlen(menu
->frame
->title
);
2291 ok
= getMenuPath(menu
->parent
, buffer
, bufSize
- len
- 1);
2296 strcat(buffer
, "\\");
2297 strcat(buffer
, menu
->frame
->title
);
2302 static Bool
saveMenuRecurs(WMPropList
* menus
, WScreen
* scr
, WMenu
* menu
)
2305 int save_menus
= 0, i
;
2309 if (menu
->flags
.brother
)
2310 menu
= menu
->brother
;
2312 if (menu
->flags
.buttoned
&& menu
!= scr
->switch_menu
) {
2315 ok
= getMenuPath(menu
, buffer
, 510);
2318 key
= WMCreatePLString(buffer
);
2319 saveMenuInfo(menus
, menu
, key
);
2320 WMReleasePropList(key
);
2326 for (i
= 0; i
< menu
->cascade_no
; i
++) {
2327 if (saveMenuRecurs(menus
, scr
, menu
->cascades
[i
]))
2334 #define COMPLAIN(key) wwarning(_("bad value in menus state info: %s"), key)
2336 static Bool
getMenuInfo(WMPropList
* info
, int *x
, int *y
, Bool
* lowered
)
2342 if (WMIsPLArray(info
)) {
2344 pos
= WMGetFromPLArray(info
, 0);
2345 flags
= WMGetFromPLArray(info
, 1);
2346 if (flags
!= NULL
&& WMIsPLString(flags
) && WMGetFromPLString(flags
) != NULL
2347 && strcmp(WMGetFromPLString(flags
), "lowered") == 0) {
2354 if (pos
!= NULL
&& WMIsPLString(pos
)) {
2355 if (sscanf(WMGetFromPLString(pos
), "%i,%i", x
, y
) != 2)
2356 COMPLAIN("Position");
2358 COMPLAIN("(position, flags...)");
2365 static int restoreMenu(WScreen
* scr
, WMPropList
* menu
, int which
)
2368 Bool lowered
= False
;
2369 WMenu
*pmenu
= NULL
;
2374 if (!getMenuInfo(menu
, &x
, &y
, &lowered
))
2377 if (which
& WSS_SWITCHMENU
) {
2378 OpenSwitchMenu(scr
, x
, y
, False
);
2379 pmenu
= scr
->switch_menu
;
2383 int width
= MENUW(pmenu
);
2384 int height
= MENUH(pmenu
);
2385 WMRect rect
= wGetRectForHead(scr
, wGetHeadForPointerLocation(scr
));
2388 changeMenuLevels(pmenu
, True
);
2391 if (x
< rect
.pos
.x
- width
)
2393 if (x
> rect
.pos
.x
+ rect
.size
.width
)
2394 x
= rect
.pos
.x
+ rect
.size
.width
- width
;
2397 if (y
> rect
.pos
.y
+ rect
.size
.height
)
2398 y
= rect
.pos
.y
+ rect
.size
.height
- height
;
2400 wMenuMove(pmenu
, x
, y
, True
);
2401 pmenu
->flags
.buttoned
= 1;
2402 wFrameWindowShowButton(pmenu
->frame
, WFF_RIGHT_BUTTON
);
2408 static int restoreMenuRecurs(WScreen
* scr
, WMPropList
* menus
, WMenu
* menu
, char *path
)
2410 WMPropList
*key
, *entry
;
2415 if (strlen(path
) + strlen(menu
->frame
->title
) > 510)
2418 snprintf(buffer
, sizeof(buffer
), "%s\\%s", path
, menu
->frame
->title
);
2419 key
= WMCreatePLString(buffer
);
2420 entry
= WMGetFromPLDictionary(menus
, key
);
2423 if (entry
&& getMenuInfo(entry
, &x
, &y
, &lowered
)) {
2425 if (!menu
->flags
.mapped
) {
2426 int width
= MENUW(menu
);
2427 int height
= MENUH(menu
);
2428 WMRect rect
= wGetRectForHead(scr
, wGetHeadForPointerLocation(scr
));
2430 wMenuMapAt(menu
, x
, y
, False
);
2433 /* make parent map the copy in place of the original */
2434 for (i
= 0; i
< menu
->parent
->cascade_no
; i
++) {
2435 if (menu
->parent
->cascades
[i
] == menu
) {
2436 menu
->parent
->cascades
[i
] = menu
->brother
;
2442 changeMenuLevels(menu
, True
);
2445 if (x
< rect
.pos
.x
- width
)
2447 if (x
> rect
.pos
.x
+ rect
.size
.width
)
2448 x
= rect
.pos
.x
+ rect
.size
.width
- width
;
2451 if (y
> rect
.pos
.y
+ rect
.size
.height
)
2452 y
= rect
.pos
.y
+ rect
.size
.height
- height
;
2454 wMenuMove(menu
, x
, y
, True
);
2455 menu
->flags
.buttoned
= 1;
2456 wFrameWindowShowButton(menu
->frame
, WFF_RIGHT_BUTTON
);
2461 WMReleasePropList(key
);
2463 for (i
= 0; i
< menu
->cascade_no
; i
++) {
2464 if (restoreMenuRecurs(scr
, menus
, menu
->cascades
[i
], buffer
) != False
)
2471 void wMenuRestoreState(WScreen
* scr
)
2473 WMPropList
*menus
, *menu
, *key
, *skey
;
2475 if (!scr
->session_state
) {
2479 key
= WMCreatePLString("Menus");
2480 menus
= WMGetFromPLDictionary(scr
->session_state
, key
);
2481 WMReleasePropList(key
);
2488 skey
= WMCreatePLString("SwitchMenu");
2489 menu
= WMGetFromPLDictionary(menus
, skey
);
2490 WMReleasePropList(skey
);
2491 restoreMenu(scr
, menu
, WSS_SWITCHMENU
);
2493 if (!scr
->root_menu
) {
2494 OpenRootMenu(scr
, scr
->scr_width
* 2, 0, False
);
2495 wMenuUnmap(scr
->root_menu
);
2497 restoreMenuRecurs(scr
, menus
, scr
->root_menu
, "");
2500 void OpenWorkspaceMenu(WScreen
* scr
, int x
, int y
)
2502 WMenu
*menu
, *parent
;
2505 if (!scr
->root_menu
) {
2506 OpenRootMenu(scr
, scr
->scr_width
* 2, 0, False
);
2507 wMenuUnmap(scr
->root_menu
);
2510 menu
= scr
->workspace_menu
;
2512 if (menu
->flags
.mapped
) {
2513 if (!menu
->flags
.buttoned
) {
2515 parent
= menu
->parent
;
2516 if (parent
&& parent
->selected_entry
>= 0) {
2517 entry
= parent
->entries
[parent
->selected_entry
];
2518 if (parent
->cascades
[entry
->cascade
] == menu
) {
2519 selectEntry(parent
, -1);
2520 wMenuMapAt(menu
, x
, y
, False
);
2524 wRaiseFrame(menu
->frame
->core
);
2525 wMenuMapCopyAt(menu
, x
, y
);
2528 wMenuMapAt(menu
, x
, y
, False
);