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"
46 /****** Global Variables ******/
48 extern Cursor wCursor
[WCUR_LAST
];
50 extern XContext wWinContext
;
52 extern WPreferences wPreferences
;
54 #define MOD_MASK wPreferences.modifier_mask
56 #define MENU_SCROLL_STEP menuScrollParameters[(int)wPreferences.menu_scroll_speed].steps
57 #define MENU_SCROLL_DELAY menuScrollParameters[(int)wPreferences.menu_scroll_speed].delay
59 #define MENUW(m) ((m)->frame->core->width+2*FRAME_BORDER_WIDTH)
60 #define MENUH(m) ((m)->frame->core->height+2*FRAME_BORDER_WIDTH)
62 /***** Local Stuff ******/
64 #define WSS_ROOTMENU (1<<0)
65 #define WSS_SWITCHMENU (1<<1)
66 #define WSS_WSMENU (1<<2)
71 } menuScrollParameters
[5] = {
73 MENU_SCROLL_STEPS_UF
, MENU_SCROLL_DELAY_UF
}, {
74 MENU_SCROLL_STEPS_F
, MENU_SCROLL_DELAY_F
}, {
75 MENU_SCROLL_STEPS_M
, MENU_SCROLL_DELAY_M
}, {
76 MENU_SCROLL_STEPS_S
, MENU_SCROLL_DELAY_S
}, {
77 MENU_SCROLL_STEPS_US
, MENU_SCROLL_DELAY_US
}};
79 static void menuMouseDown(WObjDescriptor
* desc
, XEvent
* event
);
80 static void menuExpose(WObjDescriptor
* desc
, XEvent
* event
);
81 static void menuTitleDoubleClick(WCoreWindow
* sender
, void *data
, XEvent
* event
);
82 static void menuTitleMouseDown(WCoreWindow
* sender
, void *data
, XEvent
* event
);
83 static void menuCloseClick(WCoreWindow
* sender
, void *data
, XEvent
* event
);
84 static void updateTexture(WMenu
* menu
);
85 static int saveMenuRecurs(WMPropList
* menus
, WScreen
* scr
, WMenu
* menu
);
86 static int restoreMenuRecurs(WScreen
* scr
, WMPropList
* menus
, WMenu
* menu
, char *path
);
87 static void selectEntry(WMenu
* menu
, int entry_no
);
88 static void closeCascade(WMenu
* menu
);
90 /****** Notification Observers ******/
92 static void appearanceObserver(void *self
, WMNotification
* notif
)
94 WMenu
*menu
= (WMenu
*) self
;
95 uintptr_t flags
= (uintptr_t)WMGetNotificationClientData(notif
);
97 if (!menu
->flags
.realized
)
100 if (WMGetNotificationName(notif
) == WNMenuAppearanceSettingsChanged
) {
101 if (flags
& WFontSettings
) {
102 menu
->flags
.realized
= 0;
105 if (flags
& WTextureSettings
) {
106 if (!menu
->flags
.brother
)
109 if (flags
& (WTextureSettings
| WColorSettings
)) {
112 } else if (menu
->flags
.titled
) {
114 if (flags
& WFontSettings
) {
115 menu
->flags
.realized
= 0;
118 if (flags
& WTextureSettings
) {
119 menu
->frame
->flags
.need_texture_remake
= 1;
121 if (flags
& (WColorSettings
| WTextureSettings
)) {
122 wFrameWindowPaint(menu
->frame
);
127 /************************************/
130 *----------------------------------------------------------------------
132 * Creates a new empty menu with the specified title. If main_menu
133 * is True, the created menu will be a main menu, which has some special
134 * properties such as being placed over other normal menus.
135 * If title is NULL, the menu will have no titlebar.
139 *----------------------------------------------------------------------
141 WMenu
*wMenuCreate(WScreen
* screen
, char *title
, int main_menu
)
144 static int brother
= 0;
147 menu
= wmalloc(sizeof(WMenu
));
149 memset(menu
, 0, sizeof(WMenu
));
151 #ifdef SINGLE_MENULEVEL
152 tmp
= WMSubmenuLevel
;
154 tmp
= (main_menu
? WMMainMenuLevel
: WMSubmenuLevel
);
157 flags
= WFF_SINGLE_STATE
| WFF_BORDER
;
159 flags
|= WFF_TITLEBAR
| WFF_RIGHT_BUTTON
;
160 menu
->flags
.titled
= 1;
163 wFrameWindowCreate(screen
, tmp
, 8, 2, 1, 1, &wPreferences
.menu_title_clearance
, flags
,
164 screen
->menu_title_texture
, NULL
,
165 screen
->menu_title_color
, &screen
->menu_title_font
);
167 menu
->frame
->core
->descriptor
.parent
= menu
;
168 menu
->frame
->core
->descriptor
.parent_type
= WCLASS_MENU
;
169 menu
->frame
->core
->descriptor
.handle_mousedown
= menuMouseDown
;
171 wFrameWindowHideButton(menu
->frame
, WFF_RIGHT_BUTTON
);
174 menu
->frame
->title
= wstrdup(title
);
177 menu
->frame
->flags
.justification
= WTJ_LEFT
;
179 menu
->frame
->rbutton_image
= screen
->b_pixmaps
[WBUT_CLOSE
];
182 menu
->alloced_entries
= 0;
183 menu
->selected_entry
= -1;
184 menu
->entries
= NULL
;
186 menu
->frame_x
= screen
->app_menu_x
;
187 menu
->frame_y
= screen
->app_menu_y
;
189 menu
->frame
->child
= menu
;
191 menu
->flags
.lowered
= 0;
195 /* setup object descriptors */
196 menu
->frame
->on_mousedown_titlebar
= menuTitleMouseDown
;
197 menu
->frame
->on_dblclick_titlebar
= menuTitleDoubleClick
;
200 menu
->frame
->on_click_right
= menuCloseClick
;
202 menu
->menu
= wCoreCreate(menu
->frame
->core
, 0, menu
->frame
->top_width
, menu
->frame
->core
->width
, 10);
204 menu
->menu
->descriptor
.parent
= menu
;
205 menu
->menu
->descriptor
.parent_type
= WCLASS_MENU
;
206 menu
->menu
->descriptor
.handle_expose
= menuExpose
;
207 menu
->menu
->descriptor
.handle_mousedown
= menuMouseDown
;
209 menu
->menu_texture_data
= None
;
211 XMapWindow(dpy
, menu
->menu
->window
);
217 menu
->brother
= wMenuCreate(screen
, title
, main_menu
);
219 menu
->brother
->flags
.brother
= 1;
220 menu
->brother
->brother
= menu
;
222 WMAddNotificationObserver(appearanceObserver
, menu
, WNMenuAppearanceSettingsChanged
, menu
);
224 WMAddNotificationObserver(appearanceObserver
, menu
, WNMenuTitleAppearanceSettingsChanged
, menu
);
229 WMenu
*wMenuCreateForApp(WScreen
* screen
, char *title
, int main_menu
)
233 menu
= wMenuCreate(screen
, title
, main_menu
);
236 menu
->flags
.app_menu
= 1;
237 menu
->brother
->flags
.app_menu
= 1;
242 static void insertEntry(WMenu
* menu
, WMenuEntry
* entry
, int index
)
246 for (i
= menu
->entry_no
- 1; i
>= index
; i
--) {
247 menu
->entries
[i
]->order
++;
248 menu
->entries
[i
+ 1] = menu
->entries
[i
];
250 menu
->entries
[index
] = entry
;
253 WMenuEntry
*wMenuInsertCallback(WMenu
* menu
, int index
, char *text
,
254 void (*callback
) (WMenu
* menu
, WMenuEntry
* entry
), void *clientdata
)
258 menu
->flags
.realized
= 0;
259 menu
->brother
->flags
.realized
= 0;
261 /* reallocate array if it's too small */
262 if (menu
->entry_no
>= menu
->alloced_entries
) {
265 tmp
= wrealloc(menu
->entries
, sizeof(WMenuEntry
) * (menu
->alloced_entries
+ 5));
268 menu
->alloced_entries
+= 5;
270 menu
->brother
->entries
= tmp
;
271 menu
->brother
->alloced_entries
= menu
->alloced_entries
;
273 entry
= wmalloc(sizeof(WMenuEntry
));
274 memset(entry
, 0, sizeof(WMenuEntry
));
275 entry
->flags
.enabled
= 1;
276 entry
->text
= wstrdup(text
);
278 entry
->clientdata
= clientdata
;
279 entry
->callback
= callback
;
280 if (index
< 0 || index
>= menu
->entry_no
) {
281 entry
->order
= menu
->entry_no
;
282 menu
->entries
[menu
->entry_no
] = entry
;
284 entry
->order
= index
;
285 insertEntry(menu
, entry
, index
);
289 menu
->brother
->entry_no
= menu
->entry_no
;
294 void wMenuEntrySetCascade(WMenu
* menu
, WMenuEntry
* entry
, WMenu
* cascade
)
296 WMenu
*brother
= menu
->brother
;
299 assert(menu
->flags
.brother
== 0);
301 if (entry
->cascade
>= 0) {
302 menu
->flags
.realized
= 0;
303 brother
->flags
.realized
= 0;
306 cascade
->parent
= menu
;
308 cascade
->brother
->parent
= brother
;
311 for (i
= 0; i
< menu
->cascade_no
; i
++) {
312 if (menu
->cascades
[i
] == NULL
) {
313 menu
->cascades
[i
] = cascade
;
314 brother
->cascades
[i
] = cascade
->brother
;
321 entry
->cascade
= menu
->cascade_no
;
323 menu
->cascades
= wrealloc(menu
->cascades
, sizeof(WMenu
) * (menu
->cascade_no
+ 1));
324 menu
->cascades
[menu
->cascade_no
++] = cascade
;
326 brother
->cascades
= wrealloc(brother
->cascades
, sizeof(WMenu
) * (brother
->cascade_no
+ 1));
327 brother
->cascades
[brother
->cascade_no
++] = cascade
->brother
;
330 if (menu
->flags
.lowered
) {
332 cascade
->flags
.lowered
= 1;
333 ChangeStackingLevel(cascade
->frame
->core
, WMNormalLevel
);
335 cascade
->brother
->flags
.lowered
= 1;
336 ChangeStackingLevel(cascade
->brother
->frame
->core
, WMNormalLevel
);
339 if (!menu
->flags
.realized
)
343 void wMenuEntryRemoveCascade(WMenu
* menu
, WMenuEntry
* entry
)
345 assert(menu
->flags
.brother
== 0);
347 /* destroy cascade menu */
348 if (entry
->cascade
>= 0 && menu
->cascades
&& menu
->cascades
[entry
->cascade
] != NULL
) {
350 wMenuDestroy(menu
->cascades
[entry
->cascade
], True
);
352 menu
->cascades
[entry
->cascade
] = NULL
;
353 menu
->brother
->cascades
[entry
->cascade
] = NULL
;
359 void wMenuRemoveItem(WMenu
* menu
, int index
)
363 if (menu
->flags
.brother
) {
364 wMenuRemoveItem(menu
->brother
, index
);
368 if (index
>= menu
->entry_no
)
371 /* destroy cascade menu */
372 wMenuEntryRemoveCascade(menu
, menu
->entries
[index
]);
374 /* destroy unshared data */
376 if (menu
->entries
[index
]->text
)
377 wfree(menu
->entries
[index
]->text
);
379 if (menu
->entries
[index
]->rtext
)
380 wfree(menu
->entries
[index
]->rtext
);
382 if (menu
->entries
[index
]->free_cdata
&& menu
->entries
[index
]->clientdata
)
383 (*menu
->entries
[index
]->free_cdata
) (menu
->entries
[index
]->clientdata
);
385 wfree(menu
->entries
[index
]);
387 for (i
= index
; i
< menu
->entry_no
- 1; i
++) {
388 menu
->entries
[i
+ 1]->order
--;
389 menu
->entries
[i
] = menu
->entries
[i
+ 1];
392 menu
->brother
->entry_no
--;
395 static Pixmap
renderTexture(WMenu
* menu
)
403 WScreen
*scr
= menu
->menu
->screen_ptr
;
404 WTexture
*texture
= scr
->menu_item_texture
;
406 if (wPreferences
.menu_style
== MS_NORMAL
) {
407 img
= wTextureRenderImage(texture
, menu
->menu
->width
, menu
->entry_height
, WREL_MENUENTRY
);
409 img
= wTextureRenderImage(texture
, menu
->menu
->width
, menu
->menu
->height
+ 1, WREL_MENUENTRY
);
412 wwarning(_("could not render texture: %s"), RMessageForError(RErrorCode
));
417 if (wPreferences
.menu_style
== MS_SINGLE_TEXTURE
) {
419 light
.red
= light
.green
= light
.blue
= 80;
422 dark
.red
= dark
.green
= dark
.blue
= 0;
425 mid
.red
= mid
.green
= mid
.blue
= 40;
427 for (i
= 1; i
< menu
->entry_no
; i
++) {
428 ROperateLine(img
, RSubtractOperation
, 0, i
* menu
->entry_height
- 2,
429 menu
->menu
->width
- 1, i
* menu
->entry_height
- 2, &mid
);
431 RDrawLine(img
, 0, i
* menu
->entry_height
- 1,
432 menu
->menu
->width
- 1, i
* menu
->entry_height
- 1, &dark
);
434 ROperateLine(img
, RAddOperation
, 0, i
* menu
->entry_height
,
435 menu
->menu
->width
- 1, i
* menu
->entry_height
, &light
);
438 if (!RConvertImage(scr
->rcontext
, img
, &pix
)) {
439 wwarning(_("error rendering image:%s"), RMessageForError(RErrorCode
));
446 static void updateTexture(WMenu
* menu
)
448 WScreen
*scr
= menu
->menu
->screen_ptr
;
450 /* setup background texture */
451 if (scr
->menu_item_texture
->any
.type
!= WTEX_SOLID
) {
452 if (!menu
->flags
.brother
) {
453 FREE_PIXMAP(menu
->menu_texture_data
);
455 menu
->menu_texture_data
= renderTexture(menu
);
457 XSetWindowBackgroundPixmap(dpy
, menu
->menu
->window
, menu
->menu_texture_data
);
458 XClearWindow(dpy
, menu
->menu
->window
);
460 XSetWindowBackgroundPixmap(dpy
, menu
->brother
->menu
->window
, menu
->menu_texture_data
);
461 XClearWindow(dpy
, menu
->brother
->menu
->window
);
464 XSetWindowBackground(dpy
, menu
->menu
->window
, scr
->menu_item_texture
->any
.color
.pixel
);
465 XClearWindow(dpy
, menu
->menu
->window
);
469 void wMenuRealize(WMenu
* menu
)
472 int width
, rwidth
, mrwidth
, mwidth
;
473 int theight
, twidth
, eheight
;
474 WScreen
*scr
= menu
->frame
->screen_ptr
;
475 static int brother_done
= 0;
480 wMenuRealize(menu
->brother
);
484 flags
= WFF_SINGLE_STATE
| WFF_BORDER
;
485 if (menu
->flags
.titled
)
486 flags
|= WFF_TITLEBAR
| WFF_RIGHT_BUTTON
;
488 wFrameWindowUpdateBorders(menu
->frame
, flags
);
490 if (menu
->flags
.titled
) {
491 twidth
= WMWidthOfString(scr
->menu_title_font
, menu
->frame
->title
, strlen(menu
->frame
->title
));
492 theight
= menu
->frame
->top_width
;
493 twidth
+= theight
+ (wPreferences
.new_style
== TS_NEW
? 16 : 8);
498 eheight
= WMFontHeight(scr
->menu_entry_font
) + 6 + wPreferences
.menu_text_clearance
* 2;
499 menu
->entry_height
= eheight
;
502 for (i
= 0; i
< menu
->entry_no
; i
++) {
505 /* search widest text */
506 text
= menu
->entries
[i
]->text
;
507 width
= WMWidthOfString(scr
->menu_entry_font
, text
, strlen(text
)) + 10;
509 if (menu
->entries
[i
]->flags
.indicator
) {
510 width
+= MENU_INDICATOR_SPACE
;
516 /* search widest text on right */
517 text
= menu
->entries
[i
]->rtext
;
519 rwidth
= WMWidthOfString(scr
->menu_entry_font
, text
, strlen(text
))
521 else if (menu
->entries
[i
]->cascade
>= 0)
526 if (rwidth
> mrwidth
)
534 wCoreConfigure(menu
->menu
, 0, theight
, mwidth
, menu
->entry_no
* eheight
- 1);
536 wFrameWindowResize(menu
->frame
, mwidth
, menu
->entry_no
* eheight
- 1
537 + menu
->frame
->top_width
+ menu
->frame
->bottom_width
);
541 menu
->flags
.realized
= 1;
543 if (menu
->flags
.mapped
)
545 if (menu
->brother
->flags
.mapped
)
546 wMenuPaint(menu
->brother
);
549 void wMenuDestroy(WMenu
* menu
, int recurse
)
553 WMRemoveNotificationObserver(menu
);
555 /* remove any pending timers */
557 WMDeleteTimerHandler(menu
->timer
);
560 /* call destroy handler */
561 if (menu
->on_destroy
)
562 (*menu
->on_destroy
) (menu
);
564 /* Destroy items if this menu own them. If this is the "brother" menu,
565 * leave them alone as it is shared by them.
567 if (!menu
->flags
.brother
) {
568 for (i
= 0; i
< menu
->entry_no
; i
++) {
570 wfree(menu
->entries
[i
]->text
);
572 if (menu
->entries
[i
]->rtext
)
573 wfree(menu
->entries
[i
]->rtext
);
576 if (menu
->entries
[i
]->instances
) {
577 WMReleasePropList(menu
->entries
[i
]->instances
);
579 #endif /* USER_MENU */
581 if (menu
->entries
[i
]->free_cdata
&& menu
->entries
[i
]->clientdata
) {
582 (*menu
->entries
[i
]->free_cdata
) (menu
->entries
[i
]->clientdata
);
584 wfree(menu
->entries
[i
]);
588 for (i
= 0; i
< menu
->cascade_no
; i
++) {
589 if (menu
->cascades
[i
]) {
590 if (menu
->cascades
[i
]->flags
.brother
)
591 wMenuDestroy(menu
->cascades
[i
]->brother
, recurse
);
593 wMenuDestroy(menu
->cascades
[i
], recurse
);
599 wfree(menu
->entries
);
603 FREE_PIXMAP(menu
->menu_texture_data
);
606 wfree(menu
->cascades
);
608 wCoreDestroy(menu
->menu
);
609 wFrameWindowDestroy(menu
->frame
);
611 /* destroy copy of this menu */
612 if (!menu
->flags
.brother
&& menu
->brother
)
613 wMenuDestroy(menu
->brother
, False
);
623 static void drawFrame(WScreen
* scr
, Drawable win
, int y
, int w
, int h
, int type
)
629 segs
[i
].x1
= segs
[i
].x2
= w
- 1;
631 segs
[i
].y2
= y
+ h
- 1;
633 if (type
!= F_TOP
&& type
!= F_NONE
) {
635 segs
[i
].y1
= segs
[i
].y2
= y
+ h
- 2;
639 XDrawSegments(dpy
, win
, scr
->menu_item_auxtexture
->dim_gc
, segs
, i
);
645 segs
[i
].y2
= y
+ h
- 1;
647 if (type
!= F_BOTTOM
&& type
!= F_NONE
) {
654 XDrawSegments(dpy
, win
, scr
->menu_item_auxtexture
->light_gc
, segs
, i
);
656 if (type
!= F_TOP
&& type
!= F_NONE
)
657 XDrawLine(dpy
, win
, scr
->menu_item_auxtexture
->dark_gc
, 0, y
+ h
- 1, w
- 1, y
+ h
- 1);
660 static void paintEntry(WMenu
* menu
, int index
, int selected
)
662 WScreen
*scr
= menu
->frame
->screen_ptr
;
663 Window win
= menu
->menu
->window
;
664 WMenuEntry
*entry
= menu
->entries
[index
];
670 if (!menu
->flags
.realized
)
672 h
= menu
->entry_height
;
673 w
= menu
->menu
->width
;
676 light
= scr
->menu_item_auxtexture
->light_gc
;
677 dim
= scr
->menu_item_auxtexture
->dim_gc
;
678 dark
= scr
->menu_item_auxtexture
->dark_gc
;
680 if (wPreferences
.menu_style
== MS_FLAT
&& menu
->entry_no
> 1) {
683 else if (index
== menu
->entry_no
- 1)
691 /* paint background */
693 XFillRectangle(dpy
, win
, WMColorGC(scr
->select_color
), 1, y
+ 1, w
- 2, h
- 3);
694 if (scr
->menu_item_texture
->any
.type
== WTEX_SOLID
)
695 drawFrame(scr
, win
, y
, w
, h
, type
);
697 if (scr
->menu_item_texture
->any
.type
== WTEX_SOLID
) {
698 XClearArea(dpy
, win
, 0, y
+ 1, w
- 1, h
- 3, False
);
700 drawFrame(scr
, win
, y
, w
, h
, type
);
702 XClearArea(dpy
, win
, 0, y
, w
, h
, False
);
707 if (entry
->flags
.enabled
)
708 color
= scr
->select_text_color
;
710 color
= scr
->dtext_color
;
711 } else if (!entry
->flags
.enabled
) {
712 color
= scr
->dtext_color
;
714 color
= scr
->mtext_color
;
718 if (entry
->flags
.indicator
)
719 x
+= MENU_INDICATOR_SPACE
+ 2;
721 WMDrawString(scr
->wmscreen
, win
, color
, scr
->menu_entry_font
,
722 x
, 3 + y
+ wPreferences
.menu_text_clearance
, entry
->text
, strlen(entry
->text
));
724 if (entry
->cascade
>= 0) {
725 /* draw the cascade indicator */
726 XDrawLine(dpy
, win
, dim
, w
- 11, y
+ 6, w
- 6, y
+ h
/ 2 - 1);
727 XDrawLine(dpy
, win
, light
, w
- 11, y
+ h
- 8, w
- 6, y
+ h
/ 2 - 1);
728 XDrawLine(dpy
, win
, dark
, w
- 12, y
+ 6, w
- 12, y
+ h
- 8);
732 if (entry
->flags
.indicator
&& entry
->flags
.indicator_on
) {
736 switch (entry
->flags
.indicator_type
) {
738 indicator
= scr
->menu_check_indicator
;
741 indicator
= scr
->menu_mini_indicator
;
744 indicator
= scr
->menu_hide_indicator
;
747 indicator
= scr
->menu_shade_indicator
;
751 indicator
= scr
->menu_radio_indicator
;
755 iw
= indicator
->width
;
756 ih
= indicator
->height
;
757 XSetClipMask(dpy
, scr
->copy_gc
, indicator
->mask
);
758 XSetClipOrigin(dpy
, scr
->copy_gc
, 5, y
+ (h
- ih
) / 2);
760 if (entry
->flags
.enabled
) {
761 XSetForeground(dpy
, scr
->copy_gc
, WMColorPixel(scr
->select_text_color
));
763 XSetForeground(dpy
, scr
->copy_gc
, WMColorPixel(scr
->dtext_color
));
766 if (entry
->flags
.enabled
) {
767 XSetForeground(dpy
, scr
->copy_gc
, WMColorPixel(scr
->mtext_color
));
769 XSetForeground(dpy
, scr
->copy_gc
, WMColorPixel(scr
->dtext_color
));
772 XFillRectangle(dpy
, win
, scr
->copy_gc
, 5, y
+ (h
- ih
) / 2, iw
, ih
);
774 XCopyArea(dpy, indicator->image, win, scr->copy_gc, 0, 0,
775 iw, ih, 5, y+(h-ih)/2);
777 XSetClipOrigin(dpy
, scr
->copy_gc
, 0, 0);
780 /* draw right text */
782 if (entry
->rtext
&& entry
->cascade
< 0) {
783 tw
= WMWidthOfString(scr
->menu_entry_font
, entry
->rtext
, strlen(entry
->rtext
));
785 WMDrawString(scr
->wmscreen
, win
, color
, scr
->menu_entry_font
, w
- 6 - tw
,
786 y
+ 3 + wPreferences
.menu_text_clearance
, entry
->rtext
, strlen(entry
->rtext
));
790 static void move_menus(WMenu
* menu
, int x
, int y
)
792 while (menu
->parent
) {
795 if (!wPreferences
.align_menus
&& menu
->selected_entry
>= 0) {
796 y
-= menu
->selected_entry
* menu
->entry_height
;
799 wMenuMove(menu
, x
, y
, True
);
802 static void makeVisible(WMenu
* menu
)
804 WScreen
*scr
= menu
->frame
->screen_ptr
;
805 int x1
, y1
, x2
, y2
, new_x
, new_y
;
806 WMRect rect
= wGetRectForHead(scr
, wGetHeadForPointerLocation(scr
));
808 if (menu
->entry_no
< 0)
812 y1
= menu
->frame_y
+ menu
->frame
->top_width
+ menu
->selected_entry
* menu
->entry_height
;
813 x2
= x1
+ MENUW(menu
);
814 y2
= y1
+ menu
->entry_height
;
819 if (x1
< rect
.pos
.x
) {
821 } else if (x2
>= rect
.pos
.x
+ rect
.size
.width
) {
822 new_x
= rect
.pos
.x
+ rect
.size
.width
- MENUW(menu
) - 1;
825 if (y1
< rect
.pos
.y
) {
827 } else if (y2
>= rect
.pos
.y
+ rect
.size
.height
) {
828 new_y
= rect
.pos
.y
+ rect
.size
.height
- menu
->entry_height
- 1;
831 new_y
= new_y
- menu
->frame
->top_width
- menu
->selected_entry
* menu
->entry_height
;
832 move_menus(menu
, new_x
, new_y
);
835 static int check_key(WMenu
* menu
, XKeyEvent
* event
)
840 if (XLookupString(event
, buffer
, 32, NULL
, NULL
) < 1)
843 ch
= toupper(buffer
[0]);
845 s
= (menu
->selected_entry
>= 0 ? menu
->selected_entry
+ 1 : 0);
848 for (i
= s
; i
< menu
->entry_no
; i
++) {
849 if (ch
== toupper(menu
->entries
[i
]->text
[0])) {
853 /* no match. Retry from start, if previous started from a selected entry */
861 static int keyboardMenu(WMenu
* menu
)
864 KeySym ksym
= NoSymbol
;
868 int old_pos_x
= menu
->frame_x
;
869 int old_pos_y
= menu
->frame_y
;
870 int new_x
= old_pos_x
, new_y
= old_pos_y
;
871 WMRect rect
= wGetRectForHead(menu
->frame
->screen_ptr
,
872 wGetHeadForPointerLocation(menu
->frame
->screen_ptr
));
874 if (menu
->flags
.editing
)
877 XGrabKeyboard(dpy
, menu
->frame
->core
->window
, True
, GrabModeAsync
, GrabModeAsync
, CurrentTime
);
879 if (menu
->frame_y
+ menu
->frame
->top_width
>= rect
.pos
.y
+ rect
.size
.height
)
880 new_y
= rect
.pos
.y
+ rect
.size
.height
- menu
->frame
->top_width
;
882 if (menu
->frame_x
+ MENUW(menu
) >= rect
.pos
.x
+ rect
.size
.width
)
883 new_x
= rect
.pos
.x
+ rect
.size
.width
- MENUW(menu
) - 1;
885 move_menus(menu
, new_x
, new_y
);
887 while (!done
&& menu
->flags
.mapped
) {
888 XAllowEvents(dpy
, AsyncKeyboard
, CurrentTime
);
889 WMMaskEvent(dpy
, ExposureMask
| ButtonMotionMask
| ButtonPressMask
890 | ButtonReleaseMask
| KeyPressMask
| KeyReleaseMask
| SubstructureNotifyMask
, &event
);
892 switch (event
.type
) {
894 ksym
= XLookupKeysym(&event
.xkey
, 0);
904 selectEntry(menu
, 0);
912 selectEntry(menu
, menu
->entry_no
- 1);
923 if (menu
->selected_entry
<= 0)
924 selectEntry(menu
, menu
->entry_no
- 1);
926 selectEntry(menu
, menu
->selected_entry
- 1);
937 if (menu
->selected_entry
< 0)
938 selectEntry(menu
, 0);
939 else if (menu
->selected_entry
== menu
->entry_no
- 1)
940 selectEntry(menu
, 0);
941 else if (menu
->selected_entry
< menu
->entry_no
- 1)
942 selectEntry(menu
, menu
->selected_entry
+ 1);
953 if (menu
->selected_entry
>= 0) {
955 entry
= menu
->entries
[menu
->selected_entry
];
957 if (entry
->cascade
>= 0 && menu
->cascades
958 && menu
->cascades
[entry
->cascade
]->entry_no
> 0) {
960 XUngrabKeyboard(dpy
, CurrentTime
);
962 selectEntry(menu
->cascades
[entry
->cascade
], 0);
963 if (!keyboardMenu(menu
->cascades
[entry
->cascade
]))
966 XGrabKeyboard(dpy
, menu
->frame
->core
->window
, True
,
967 GrabModeAsync
, GrabModeAsync
, CurrentTime
);
979 if (menu
->parent
!= NULL
&& menu
->parent
->selected_entry
>= 0) {
980 selectEntry(menu
, -1);
981 move_menus(menu
, old_pos_x
, old_pos_y
);
994 index
= check_key(menu
, &event
.xkey
);
996 selectEntry(menu
, index
);
1002 if (event
.type
== ButtonPress
)
1005 WMHandleEvent(&event
);
1009 XUngrabKeyboard(dpy
, CurrentTime
);
1011 if (done
== 2 && menu
->selected_entry
>= 0) {
1012 entry
= menu
->entries
[menu
->selected_entry
];
1017 if (entry
&& entry
->callback
!= NULL
&& entry
->flags
.enabled
&& entry
->cascade
< 0) {
1018 #if (MENU_BLINK_COUNT > 0)
1019 int sel
= menu
->selected_entry
;
1022 for (i
= 0; i
< MENU_BLINK_COUNT
; i
++) {
1023 paintEntry(menu
, sel
, False
);
1025 wusleep(MENU_BLINK_DELAY
);
1026 paintEntry(menu
, sel
, True
);
1028 wusleep(MENU_BLINK_DELAY
);
1031 selectEntry(menu
, -1);
1033 if (!menu
->flags
.buttoned
) {
1035 move_menus(menu
, old_pos_x
, old_pos_y
);
1039 (*entry
->callback
) (menu
, entry
);
1041 if (!menu
->flags
.buttoned
) {
1043 move_menus(menu
, old_pos_x
, old_pos_y
);
1045 selectEntry(menu
, -1);
1048 /* returns True if returning from a submenu to a parent menu,
1049 * False if exiting from menu */
1053 void wMenuMapAt(WMenu
* menu
, int x
, int y
, int keyboard
)
1055 WMRect rect
= wGetRectForHead(menu
->frame
->screen_ptr
,
1056 wGetHeadForPointerLocation(menu
->frame
->screen_ptr
));
1058 if (!menu
->flags
.realized
) {
1059 menu
->flags
.realized
= 1;
1062 if (!menu
->flags
.mapped
) {
1063 if (wPreferences
.wrap_menus
) {
1068 if (x
+ MENUW(menu
) > rect
.pos
.x
+ rect
.size
.width
)
1069 x
= rect
.pos
.x
+ rect
.size
.width
- MENUW(menu
);
1070 if (y
+ MENUH(menu
) > rect
.pos
.y
+ rect
.size
.height
)
1071 y
= rect
.pos
.y
+ rect
.size
.height
- MENUH(menu
);
1074 XMoveWindow(dpy
, menu
->frame
->core
->window
, x
, y
);
1077 XMapWindow(dpy
, menu
->frame
->core
->window
);
1078 wRaiseFrame(menu
->frame
->core
);
1079 menu
->flags
.mapped
= 1;
1081 selectEntry(menu
, 0);
1088 void wMenuMap(WMenu
* menu
)
1090 if (!menu
->flags
.realized
) {
1091 menu
->flags
.realized
= 1;
1094 if (menu
->flags
.app_menu
&& menu
->parent
== NULL
) {
1095 menu
->frame_x
= menu
->frame
->screen_ptr
->app_menu_x
;
1096 menu
->frame_y
= menu
->frame
->screen_ptr
->app_menu_y
;
1097 XMoveWindow(dpy
, menu
->frame
->core
->window
, menu
->frame_x
, menu
->frame_y
);
1099 XMapWindow(dpy
, menu
->frame
->core
->window
);
1100 wRaiseFrame(menu
->frame
->core
);
1101 menu
->flags
.mapped
= 1;
1104 void wMenuUnmap(WMenu
* menu
)
1108 XUnmapWindow(dpy
, menu
->frame
->core
->window
);
1109 if (menu
->flags
.titled
&& menu
->flags
.buttoned
) {
1110 wFrameWindowHideButton(menu
->frame
, WFF_RIGHT_BUTTON
);
1112 menu
->flags
.buttoned
= 0;
1113 menu
->flags
.mapped
= 0;
1114 menu
->flags
.open_to_left
= 0;
1116 for (i
= 0; i
< menu
->cascade_no
; i
++) {
1117 if (menu
->cascades
[i
] != NULL
1118 && menu
->cascades
[i
]->flags
.mapped
&& !menu
->cascades
[i
]->flags
.buttoned
) {
1120 wMenuUnmap(menu
->cascades
[i
]);
1123 menu
->selected_entry
= -1;
1126 void wMenuPaint(WMenu
* menu
)
1130 if (!menu
->flags
.mapped
) {
1135 for (i
= 0; i
< menu
->entry_no
; i
++) {
1136 paintEntry(menu
, i
, i
== menu
->selected_entry
);
1140 void wMenuSetEnabled(WMenu
* menu
, int index
, int enable
)
1142 if (index
>= menu
->entry_no
)
1144 menu
->entries
[index
]->flags
.enabled
= enable
;
1145 paintEntry(menu
, index
, index
== menu
->selected_entry
);
1146 paintEntry(menu
->brother
, index
, index
== menu
->selected_entry
);
1149 /* ====================================================================== */
1151 static void editEntry(WMenu
* menu
, WMenuEntry
* entry
)
1155 WObjDescriptor
*desc
;
1161 menu
->flags
.editing
= 1;
1163 text
= wTextCreate(menu
->menu
, 1, menu
->entry_height
* entry
->order
,
1164 menu
->menu
->width
- 2, menu
->entry_height
- 1);
1166 wTextPutText(text
, entry
->text
);
1167 XGetInputFocus(dpy
, &old_focus
, &old_revert
);
1168 XSetInputFocus(dpy
, text
->core
->window
, RevertToNone
, CurrentTime
);
1170 if (XGrabKeyboard(dpy
, text
->core
->window
, True
, GrabModeAsync
, GrabModeAsync
, CurrentTime
) != GrabSuccess
) {
1171 wwarning(_("could not grab keyboard"));
1174 wSetFocusTo(menu
->frame
->screen_ptr
, menu
->frame
->screen_ptr
->focused_window
);
1178 while (!done
&& !text
->done
) {
1180 XAllowEvents(dpy
, AsyncKeyboard
| AsyncPointer
, CurrentTime
);
1182 WMNextEvent(dpy
, &event
);
1184 if (XFindContext(dpy
, event
.xany
.window
, wWinContext
, (XPointer
*) & desc
) == XCNOENT
)
1187 if ((desc
!= NULL
) && (desc
->handle_anything
!= NULL
)) {
1189 (*desc
->handle_anything
) (desc
, &event
);
1192 switch (event
.type
) {
1194 XAllowEvents(dpy
, ReplayPointer
, CurrentTime
);
1198 WMHandleEvent(&event
);
1204 XSetInputFocus(dpy
, old_focus
, old_revert
, CurrentTime
);
1206 wSetFocusTo(menu
->frame
->screen_ptr
, menu
->frame
->screen_ptr
->focused_window
);
1208 t
= wTextGetText(text
);
1209 /* if !t, the user has canceled editing */
1213 entry
->text
= wstrdup(t
);
1215 menu
->flags
.realized
= 0;
1219 XUngrabKeyboard(dpy
, CurrentTime
);
1221 if (t
&& menu
->on_edit
)
1222 (*menu
->on_edit
) (menu
, entry
);
1224 menu
->flags
.editing
= 0;
1226 if (!menu
->flags
.realized
)
1230 static void selectEntry(WMenu
* menu
, int entry_no
)
1236 if (menu
->entries
== NULL
)
1239 if (entry_no
>= menu
->entry_no
)
1242 old_entry
= menu
->selected_entry
;
1243 menu
->selected_entry
= entry_no
;
1245 if (old_entry
!= entry_no
) {
1247 /* unselect previous entry */
1248 if (old_entry
>= 0) {
1249 paintEntry(menu
, old_entry
, False
);
1250 entry
= menu
->entries
[old_entry
];
1253 if (entry
->cascade
>= 0 && menu
->cascades
) {
1254 if (!menu
->cascades
[entry
->cascade
]->flags
.buttoned
) {
1255 wMenuUnmap(menu
->cascades
[entry
->cascade
]);
1261 menu
->selected_entry
= -1;
1264 entry
= menu
->entries
[entry_no
];
1266 if (entry
->cascade
>= 0 && menu
->cascades
&& entry
->flags
.enabled
) {
1267 /* Callback for when the submenu is opened.
1269 submenu
= menu
->cascades
[entry
->cascade
];
1270 if (submenu
&& submenu
->flags
.brother
)
1271 submenu
= submenu
->brother
;
1273 if (entry
->callback
) {
1274 /* Only call the callback if the submenu is not yet mapped.
1276 if (menu
->flags
.brother
) {
1277 if (!submenu
|| !submenu
->flags
.mapped
)
1278 (*entry
->callback
) (menu
->brother
, entry
);
1280 if (!submenu
|| !submenu
->flags
.buttoned
)
1281 (*entry
->callback
) (menu
, entry
);
1285 /* the submenu menu might have changed */
1286 submenu
= menu
->cascades
[entry
->cascade
];
1289 if (!submenu
->flags
.mapped
) {
1292 if (!submenu
->flags
.realized
)
1293 wMenuRealize(submenu
);
1294 if (wPreferences
.wrap_menus
) {
1295 if (menu
->flags
.open_to_left
)
1296 submenu
->flags
.open_to_left
= 1;
1298 if (submenu
->flags
.open_to_left
) {
1299 x
= menu
->frame_x
- MENUW(submenu
);
1302 submenu
->flags
.open_to_left
= 0;
1305 x
= menu
->frame_x
+ MENUW(menu
);
1307 if (x
+ MENUW(submenu
)
1308 >= menu
->frame
->screen_ptr
->scr_width
) {
1310 x
= menu
->frame_x
- MENUW(submenu
);
1311 submenu
->flags
.open_to_left
= 1;
1315 x
= menu
->frame_x
+ MENUW(menu
);
1318 if (wPreferences
.align_menus
) {
1321 y
= menu
->frame_y
+ menu
->entry_height
* entry_no
;
1322 if (menu
->flags
.titled
)
1323 y
+= menu
->frame
->top_width
;
1324 if (menu
->cascades
[entry
->cascade
]->flags
.titled
)
1325 y
-= menu
->cascades
[entry
->cascade
]->frame
->top_width
;
1328 wMenuMapAt(menu
->cascades
[entry
->cascade
], x
, y
, False
);
1329 menu
->cascades
[entry
->cascade
]->parent
= menu
;
1334 paintEntry(menu
, entry_no
, True
);
1338 static WMenu
*findMenu(WScreen
* scr
, int *x_ret
, int *y_ret
)
1341 WObjDescriptor
*desc
;
1342 Window root_ret
, win
, junk_win
;
1346 XQueryPointer(dpy
, scr
->root_win
, &root_ret
, &win
, &x
, &y
, &wx
, &wy
, &mask
);
1351 if (XFindContext(dpy
, win
, wWinContext
, (XPointer
*) & desc
) == XCNOENT
)
1354 if (desc
->parent_type
== WCLASS_MENU
) {
1355 menu
= (WMenu
*) desc
->parent
;
1356 XTranslateCoordinates(dpy
, root_ret
, menu
->menu
->window
, wx
, wy
, x_ret
, y_ret
, &junk_win
);
1362 static void closeCascade(WMenu
* menu
)
1364 WMenu
*parent
= menu
->parent
;
1366 if (menu
->flags
.brother
|| (!menu
->flags
.buttoned
&& (!menu
->flags
.app_menu
|| menu
->parent
!= NULL
))) {
1368 selectEntry(menu
, -1);
1370 #if (MENU_BLINK_DELAY > 2)
1371 wusleep(MENU_BLINK_DELAY
/ 2);
1374 while (parent
!= NULL
1375 && (parent
->parent
!= NULL
|| !parent
->flags
.app_menu
|| parent
->flags
.brother
)
1376 && !parent
->flags
.buttoned
) {
1377 selectEntry(parent
, -1);
1379 parent
= parent
->parent
;
1382 selectEntry(parent
, -1);
1386 static void closeBrotherCascadesOf(WMenu
* menu
)
1391 for (i
= 0; i
< menu
->cascade_no
; i
++) {
1392 if (menu
->cascades
[i
]->flags
.brother
) {
1393 tmp
= menu
->cascades
[i
];
1395 tmp
= menu
->cascades
[i
]->brother
;
1397 if (tmp
->flags
.mapped
) {
1398 selectEntry(tmp
->parent
, -1);
1399 closeBrotherCascadesOf(tmp
);
1405 #define getEntryAt(menu, x, y) ((y)<0 ? -1 : (y)/(menu->entry_height))
1407 static WMenu
*parentMenu(WMenu
* menu
)
1412 if (menu
->flags
.buttoned
)
1415 while (menu
->parent
&& menu
->parent
->flags
.mapped
) {
1416 parent
= menu
->parent
;
1417 if (parent
->selected_entry
< 0)
1419 entry
= parent
->entries
[parent
->selected_entry
];
1420 if (!entry
->flags
.enabled
|| entry
->cascade
< 0 || !parent
->cascades
||
1421 parent
->cascades
[entry
->cascade
] != menu
)
1424 if (menu
->flags
.buttoned
)
1432 * Will raise the passed menu, if submenu = 0
1433 * If submenu > 0 will also raise all mapped submenus
1434 * until the first buttoned one
1435 * If submenu < 0 will also raise all mapped parent menus
1436 * until the first buttoned one
1439 static void raiseMenus(WMenu
* menu
, int submenus
)
1447 wRaiseFrame(menu
->frame
->core
);
1449 if (submenus
> 0 && menu
->selected_entry
>= 0) {
1450 i
= menu
->entries
[menu
->selected_entry
]->cascade
;
1451 if (i
>= 0 && menu
->cascades
) {
1452 submenu
= menu
->cascades
[i
];
1453 if (submenu
->flags
.mapped
&& !submenu
->flags
.buttoned
)
1454 raiseMenus(submenu
, submenus
);
1457 if (submenus
< 0 && !menu
->flags
.buttoned
&& menu
->parent
&& menu
->parent
->flags
.mapped
)
1458 raiseMenus(menu
->parent
, submenus
);
1461 WMenu
*wMenuUnderPointer(WScreen
* screen
)
1463 WObjDescriptor
*desc
;
1464 Window root_ret
, win
;
1468 XQueryPointer(dpy
, screen
->root_win
, &root_ret
, &win
, &dummy
, &dummy
, &dummy
, &dummy
, &mask
);
1473 if (XFindContext(dpy
, win
, wWinContext
, (XPointer
*) & desc
) == XCNOENT
)
1476 if (desc
->parent_type
== WCLASS_MENU
)
1477 return (WMenu
*) desc
->parent
;
1481 static void getPointerPosition(WScreen
* scr
, int *x
, int *y
)
1483 Window root_ret
, win
;
1487 XQueryPointer(dpy
, scr
->root_win
, &root_ret
, &win
, x
, y
, &wx
, &wy
, &mask
);
1490 static void getScrollAmount(WMenu
* menu
, int *hamount
, int *vamount
)
1492 WScreen
*scr
= menu
->menu
->screen_ptr
;
1493 int menuX1
= menu
->frame_x
;
1494 int menuY1
= menu
->frame_y
;
1495 int menuX2
= menu
->frame_x
+ MENUW(menu
);
1496 int menuY2
= menu
->frame_y
+ MENUH(menu
);
1498 WMRect rect
= wGetRectForHead(scr
, wGetHeadForPointerLocation(scr
));
1503 getPointerPosition(scr
, &xroot
, &yroot
);
1505 if (xroot
<= (rect
.pos
.x
+ 1) && menuX1
< rect
.pos
.x
) {
1506 /* scroll to the right */
1507 *hamount
= WMIN(MENU_SCROLL_STEP
, abs(menuX1
));
1509 } else if (xroot
>= (rect
.pos
.x
+ rect
.size
.width
- 2) && menuX2
> (rect
.pos
.x
+ rect
.size
.width
- 1)) {
1510 /* scroll to the left */
1511 *hamount
= WMIN(MENU_SCROLL_STEP
, abs(menuX2
- rect
.pos
.x
- rect
.size
.width
- 1));
1516 *hamount
= -*hamount
;
1519 if (yroot
<= (rect
.pos
.y
+ 1) && menuY1
< rect
.pos
.y
) {
1521 *vamount
= WMIN(MENU_SCROLL_STEP
, abs(menuY1
));
1523 } else if (yroot
>= (rect
.pos
.y
+ rect
.size
.height
- 2) && menuY2
> (rect
.pos
.y
+ rect
.size
.height
- 1)) {
1525 *vamount
= WMIN(MENU_SCROLL_STEP
, abs(menuY2
- rect
.pos
.y
- rect
.size
.height
- 2));
1527 *vamount
= -*vamount
;
1531 static void dragScrollMenuCallback(void *data
)
1533 WMenu
*menu
= (WMenu
*) data
;
1534 WScreen
*scr
= menu
->menu
->screen_ptr
;
1535 WMenu
*parent
= parentMenu(menu
);
1536 int hamount
, vamount
;
1538 int newSelectedEntry
;
1540 getScrollAmount(menu
, &hamount
, &vamount
);
1542 if (hamount
!= 0 || vamount
!= 0) {
1543 wMenuMove(parent
, parent
->frame_x
+ hamount
, parent
->frame_y
+ vamount
, True
);
1544 if (findMenu(scr
, &x
, &y
)) {
1545 newSelectedEntry
= getEntryAt(menu
, x
, y
);
1546 selectEntry(menu
, newSelectedEntry
);
1548 /* Pointer fell outside of menu. If the selected entry is
1549 * not a submenu, unselect it */
1550 if (menu
->selected_entry
>= 0 && menu
->entries
[menu
->selected_entry
]->cascade
< 0)
1551 selectEntry(menu
, -1);
1552 newSelectedEntry
= 0;
1555 /* paranoid check */
1556 if (newSelectedEntry
>= 0) {
1557 /* keep scrolling */
1558 menu
->timer
= WMAddTimerHandler(MENU_SCROLL_DELAY
, dragScrollMenuCallback
, menu
);
1563 /* don't need to scroll anymore */
1565 if (findMenu(scr
, &x
, &y
)) {
1566 newSelectedEntry
= getEntryAt(menu
, x
, y
);
1567 selectEntry(menu
, newSelectedEntry
);
1572 static void scrollMenuCallback(void *data
)
1574 WMenu
*menu
= (WMenu
*) data
;
1575 WMenu
*parent
= parentMenu(menu
);
1576 int hamount
= 0; /* amount to scroll */
1579 getScrollAmount(menu
, &hamount
, &vamount
);
1581 if (hamount
!= 0 || vamount
!= 0) {
1582 wMenuMove(parent
, parent
->frame_x
+ hamount
, parent
->frame_y
+ vamount
, True
);
1584 /* keep scrolling */
1585 menu
->timer
= WMAddTimerHandler(MENU_SCROLL_DELAY
, scrollMenuCallback
, menu
);
1587 /* don't need to scroll anymore */
1592 #define MENU_SCROLL_BORDER 5
1594 static int isPointNearBoder(WMenu
* menu
, int x
, int y
)
1596 int menuX1
= menu
->frame_x
;
1597 int menuY1
= menu
->frame_y
;
1598 int menuX2
= menu
->frame_x
+ MENUW(menu
);
1599 int menuY2
= menu
->frame_y
+ MENUH(menu
);
1601 int head
= wGetHeadForPoint(menu
->frame
->screen_ptr
, wmkpoint(x
, y
));
1602 WMRect rect
= wGetRectForHead(menu
->frame
->screen_ptr
, head
);
1604 /* XXX: handle screen joins properly !! */
1606 if (x
>= menuX1
&& x
<= menuX2
&&
1607 (y
< rect
.pos
.y
+ MENU_SCROLL_BORDER
|| y
>= rect
.pos
.y
+ rect
.size
.height
- MENU_SCROLL_BORDER
))
1609 else if (y
>= menuY1
&& y
<= menuY2
&&
1610 (x
< rect
.pos
.x
+ MENU_SCROLL_BORDER
|| x
>= rect
.pos
.x
+ rect
.size
.width
- MENU_SCROLL_BORDER
))
1616 typedef struct _delay
{
1621 static void leaving(_delay
* dl
)
1623 wMenuMove(dl
->menu
, dl
->ox
, dl
->oy
, True
);
1624 dl
->menu
->jump_back
= NULL
;
1625 dl
->menu
->menu
->screen_ptr
->flags
.jump_back_pending
= 0;
1629 void wMenuScroll(WMenu
* menu
, XEvent
* event
)
1632 WMenu
*omenu
= parentMenu(menu
);
1633 WScreen
*scr
= menu
->frame
->screen_ptr
;
1636 int old_frame_x
= omenu
->frame_x
;
1637 int old_frame_y
= omenu
->frame_y
;
1640 if (omenu
->jump_back
)
1641 WMDeleteTimerWithClientData(omenu
->jump_back
);
1643 if (( /*omenu->flags.buttoned && */ !wPreferences
.wrap_menus
)
1644 || omenu
->flags
.app_menu
) {
1648 if (!wPreferences
.wrap_menus
)
1649 raiseMenus(omenu
, True
);
1651 raiseMenus(menu
, False
);
1654 scrollMenuCallback(menu
);
1657 int x
, y
, on_border
, on_x_edge
, on_y_edge
, on_title
;
1660 WMNextEvent(dpy
, &ev
);
1665 x
= (ev
.type
== MotionNotify
) ? ev
.xmotion
.x_root
: ev
.xcrossing
.x_root
;
1666 y
= (ev
.type
== MotionNotify
) ? ev
.xmotion
.y_root
: ev
.xcrossing
.y_root
;
1668 /* on_border is != 0 if the pointer is between the menu
1669 * and the screen border and is close enough to the border */
1670 on_border
= isPointNearBoder(menu
, x
, y
);
1672 smenu
= wMenuUnderPointer(scr
);
1674 if ((smenu
== NULL
&& !on_border
) || (smenu
&& parentMenu(smenu
) != omenu
)) {
1679 rect
= wGetRectForHead(scr
, wGetHeadForPoint(scr
, wmkpoint(x
, y
)));
1680 on_x_edge
= x
<= rect
.pos
.x
+ 1 || x
>= rect
.pos
.x
+ rect
.size
.width
- 2;
1681 on_y_edge
= y
<= rect
.pos
.y
+ 1 || y
>= rect
.pos
.y
+ rect
.size
.height
- 2;
1682 on_border
= on_x_edge
|| on_y_edge
;
1684 if (!on_border
&& !jump_back
) {
1689 if (menu
->timer
&& (smenu
!= menu
|| (!on_y_edge
&& !on_x_edge
))) {
1690 WMDeleteTimerHandler(menu
->timer
);
1698 scrollMenuCallback(menu
);
1701 /* True if we push on title, or drag the omenu to other position */
1702 on_title
= ev
.xbutton
.x_root
>= omenu
->frame_x
&&
1703 ev
.xbutton
.x_root
<= omenu
->frame_x
+ MENUW(omenu
) &&
1704 ev
.xbutton
.y_root
>= omenu
->frame_y
&&
1705 ev
.xbutton
.y_root
<= omenu
->frame_y
+ omenu
->frame
->top_width
;
1707 smenu
= wMenuUnderPointer(scr
);
1708 if (smenu
== NULL
|| (smenu
&& smenu
->flags
.buttoned
&& smenu
!= omenu
))
1710 else if (smenu
== omenu
&& on_title
) {
1724 WMDeleteTimerHandler(menu
->timer
);
1730 if (!omenu
->jump_back
) {
1731 delayer
= wmalloc(sizeof(_delay
));
1732 delayer
->menu
= omenu
;
1733 delayer
->ox
= old_frame_x
;
1734 delayer
->oy
= old_frame_y
;
1735 omenu
->jump_back
= delayer
;
1736 scr
->flags
.jump_back_pending
= 1;
1738 delayer
= omenu
->jump_back
;
1739 WMAddTimerHandler(MENU_JUMP_BACK_DELAY
, (WMCallback
*) leaving
, delayer
);
1743 static void menuExpose(WObjDescriptor
* desc
, XEvent
* event
)
1745 wMenuPaint(desc
->parent
);
1749 int *delayed_select
;
1754 static void delaySelection(void *data
)
1756 delay_data
*d
= (delay_data
*) data
;
1762 menu
= findMenu(d
->menu
->menu
->screen_ptr
, &x
, &y
);
1763 if (menu
&& (d
->menu
== menu
|| d
->delayed_select
)) {
1764 entry_no
= getEntryAt(menu
, x
, y
);
1765 selectEntry(menu
, entry_no
);
1767 if (d
->delayed_select
)
1768 *(d
->delayed_select
) = 0;
1771 static void menuMouseDown(WObjDescriptor
* desc
, XEvent
* event
)
1774 XButtonEvent
*bev
= &event
->xbutton
;
1775 WMenu
*menu
= desc
->parent
;
1777 WScreen
*scr
= menu
->frame
->screen_ptr
;
1778 WMenuEntry
*entry
= NULL
;
1780 int close_on_exit
= 0;
1782 int delayed_select
= 0;
1786 int old_frame_x
= 0;
1787 int old_frame_y
= 0;
1788 delay_data d_data
= { NULL
, NULL
, NULL
};
1790 /* Doesn't seem to be needed anymore (if delayed selection handler is
1791 * added only if not present). there seem to be no other side effects
1792 * from removing this and it is also possible that it was only added
1793 * to avoid problems with adding the delayed selection timer handler
1796 /*if (menu->flags.inside_handler) {
1799 menu
->flags
.inside_handler
= 1;
1801 if (!wPreferences
.wrap_menus
) {
1802 smenu
= parentMenu(menu
);
1803 old_frame_x
= smenu
->frame_x
;
1804 old_frame_y
= smenu
->frame_y
;
1805 } else if (event
->xbutton
.window
== menu
->frame
->core
->window
) {
1806 /* This is true if the menu was launched with right click on root window */
1807 if (!d_data
.magic
) {
1809 d_data
.delayed_select
= &delayed_select
;
1811 d_data
.magic
= WMAddTimerHandler(wPreferences
.dblclick_time
, delaySelection
, &d_data
);
1815 wRaiseFrame(menu
->frame
->core
);
1817 close_on_exit
= (bev
->send_event
|| menu
->flags
.brother
);
1819 smenu
= findMenu(scr
, &x
, &y
);
1827 if (menu
->flags
.editing
) {
1830 entry_no
= getEntryAt(menu
, x
, y
);
1831 if (entry_no
>= 0) {
1832 entry
= menu
->entries
[entry_no
];
1834 if (!close_on_exit
&& (bev
->state
& ControlMask
) && smenu
&& entry
->flags
.editable
) {
1835 editEntry(smenu
, entry
);
1837 } else if (bev
->state
& ControlMask
) {
1841 if (entry
->flags
.enabled
&& entry
->cascade
>= 0 && menu
->cascades
) {
1842 WMenu
*submenu
= menu
->cascades
[entry
->cascade
];
1844 if (submenu
->flags
.mapped
&& !submenu
->flags
.buttoned
&& menu
->selected_entry
!= entry_no
) {
1845 wMenuUnmap(submenu
);
1847 if (!submenu
->flags
.mapped
&& !delayed_select
) {
1848 selectEntry(menu
, entry_no
);
1849 } else if (!submenu
->flags
.buttoned
) {
1850 selectEntry(menu
, -1);
1853 } else if (!delayed_select
) {
1854 if (menu
== scr
->switch_menu
&& event
->xbutton
.button
== Button3
) {
1855 selectEntry(menu
, entry_no
);
1856 OpenWindowMenu2((WWindow
*)entry
->clientdata
,
1857 event
->xbutton
.x_root
,
1858 event
->xbutton
.y_root
, False
);
1859 wwin
= (WWindow
*)entry
->clientdata
;
1860 desc
= &wwin
->screen_ptr
->window_menu
->menu
->descriptor
;
1861 event
->xany
.send_event
= True
;
1862 (*desc
->handle_mousedown
)(desc
, event
);
1864 XUngrabPointer(dpy
, CurrentTime
);
1865 selectEntry(menu
, -1);
1868 selectEntry(menu
, entry_no
);
1872 if (!wPreferences
.wrap_menus
&& !wPreferences
.scrollable_menus
) {
1874 dragScrollMenuCallback(menu
);
1878 prevx
= bev
->x_root
;
1879 prevy
= bev
->y_root
;
1883 XAllowEvents(dpy
, AsyncPointer
| SyncPointer
, CurrentTime
);
1885 WMMaskEvent(dpy
, ExposureMask
| ButtonMotionMask
| ButtonReleaseMask
| ButtonPressMask
, &ev
);
1888 smenu
= findMenu(scr
, &x
, &y
);
1890 if (smenu
== NULL
) {
1891 /* moved mouse out of menu */
1893 if (!delayed_select
&& d_data
.magic
) {
1894 WMDeleteTimerHandler(d_data
.magic
);
1895 d_data
.magic
= NULL
;
1898 || (menu
->selected_entry
>= 0
1899 && menu
->entries
[menu
->selected_entry
]->cascade
>= 0)) {
1900 prevx
= ev
.xmotion
.x_root
;
1901 prevy
= ev
.xmotion
.y_root
;
1905 selectEntry(menu
, -1);
1907 prevx
= ev
.xmotion
.x_root
;
1908 prevy
= ev
.xmotion
.y_root
;
1910 } else if (menu
&& menu
!= smenu
1911 && (menu
->selected_entry
< 0
1912 || menu
->entries
[menu
->selected_entry
]->cascade
< 0)) {
1913 selectEntry(menu
, -1);
1915 if (!delayed_select
&& d_data
.magic
) {
1916 WMDeleteTimerHandler(d_data
.magic
);
1917 d_data
.magic
= NULL
;
1921 /* hysteresis for item selection */
1923 /* check if the motion was to the side, indicating that
1924 * the user may want to cross to a submenu */
1925 if (!delayed_select
&& menu
) {
1927 Bool moved_to_submenu
; /* moved to direction of submenu */
1929 dx
= abs(prevx
- ev
.xmotion
.x_root
);
1931 moved_to_submenu
= False
;
1932 if (dx
> 0 /* if moved enough to the side */
1933 /* maybe a open submenu */
1934 && menu
->selected_entry
>= 0
1935 /* moving to the right direction */
1936 && (wPreferences
.align_menus
|| ev
.xmotion
.y_root
>= prevy
)) {
1939 index
= menu
->entries
[menu
->selected_entry
]->cascade
;
1941 if (menu
->cascades
[index
]->frame_x
> menu
->frame_x
) {
1942 if (prevx
< ev
.xmotion
.x_root
)
1943 moved_to_submenu
= True
;
1945 if (prevx
> ev
.xmotion
.x_root
)
1946 moved_to_submenu
= True
;
1951 if (menu
!= smenu
) {
1953 WMDeleteTimerHandler(d_data
.magic
);
1954 d_data
.magic
= NULL
;
1956 } else if (moved_to_submenu
) {
1957 /* while we are moving, postpone the selection */
1959 WMDeleteTimerHandler(d_data
.magic
);
1961 d_data
.delayed_select
= NULL
;
1963 d_data
.magic
= WMAddTimerHandler(MENU_SELECT_DELAY
,
1964 delaySelection
, &d_data
);
1965 prevx
= ev
.xmotion
.x_root
;
1966 prevy
= ev
.xmotion
.y_root
;
1970 WMDeleteTimerHandler(d_data
.magic
);
1971 d_data
.magic
= NULL
;
1976 prevx
= ev
.xmotion
.x_root
;
1977 prevy
= ev
.xmotion
.y_root
;
1978 if (menu
!= smenu
) {
1979 /* pointer crossed menus */
1980 if (menu
&& menu
->timer
) {
1981 WMDeleteTimerHandler(menu
->timer
);
1985 dragScrollMenuCallback(smenu
);
1989 dragScrollMenuCallback(menu
);
1991 if (!delayed_select
) {
1992 entry_no
= getEntryAt(menu
, x
, y
);
1993 if (entry_no
>= 0) {
1994 entry
= menu
->entries
[entry_no
];
1995 if (entry
->flags
.enabled
&& entry
->cascade
>= 0 && menu
->cascades
) {
1996 WMenu
*submenu
= menu
->cascades
[entry
->cascade
];
1997 if (submenu
->flags
.mapped
&& !submenu
->flags
.buttoned
1998 && menu
->selected_entry
!= entry_no
) {
1999 wMenuUnmap(submenu
);
2003 selectEntry(menu
, entry_no
);
2011 if (ev
.xbutton
.button
== event
->xbutton
.button
)
2021 if (menu
&& menu
->timer
) {
2022 WMDeleteTimerHandler(menu
->timer
);
2025 if (d_data
.magic
!= NULL
) {
2026 WMDeleteTimerHandler(d_data
.magic
);
2027 d_data
.magic
= NULL
;
2030 if (menu
&& menu
->selected_entry
>= 0) {
2031 entry
= menu
->entries
[menu
->selected_entry
];
2032 if (entry
->callback
!= NULL
&& entry
->flags
.enabled
&& entry
->cascade
< 0) {
2033 /* blink and erase menu selection */
2034 #if (MENU_BLINK_DELAY > 0)
2035 int sel
= menu
->selected_entry
;
2038 for (i
= 0; i
< MENU_BLINK_COUNT
; i
++) {
2039 paintEntry(menu
, sel
, False
);
2041 wusleep(MENU_BLINK_DELAY
);
2042 paintEntry(menu
, sel
, True
);
2044 wusleep(MENU_BLINK_DELAY
);
2047 /* unmap the menu, it's parents and call the callback */
2048 if (!menu
->flags
.buttoned
&& (!menu
->flags
.app_menu
|| menu
->parent
!= NULL
)) {
2051 selectEntry(menu
, -1);
2053 (*entry
->callback
) (menu
, entry
);
2055 /* If the user double clicks an entry, the entry will
2056 * be executed twice, which is not good for things like
2057 * the root menu. So, ignore any clicks that were generated
2058 * while the entry was being executed */
2059 while (XCheckTypedWindowEvent(dpy
, menu
->menu
->window
, ButtonPress
, &ev
)) ;
2060 } else if (entry
->callback
!= NULL
&& entry
->cascade
< 0) {
2061 selectEntry(menu
, -1);
2063 if (entry
->cascade
>= 0 && menu
->cascades
&& menu
->cascades
[entry
->cascade
]->flags
.brother
) {
2064 selectEntry(menu
, -1);
2069 if (((WMenu
*) desc
->parent
)->flags
.brother
|| close_on_exit
|| !smenu
)
2070 closeCascade(desc
->parent
);
2072 /* close the cascade windows that should not remain opened */
2073 closeBrotherCascadesOf(desc
->parent
);
2075 if (!wPreferences
.wrap_menus
)
2076 wMenuMove(parentMenu(desc
->parent
), old_frame_x
, old_frame_y
, True
);
2079 /* Just to be sure in case we skip the 2 above because of a goto byebye */
2080 if (menu
&& menu
->timer
) {
2081 WMDeleteTimerHandler(menu
->timer
);
2084 if (d_data
.magic
!= NULL
) {
2085 WMDeleteTimerHandler(d_data
.magic
);
2086 d_data
.magic
= NULL
;
2089 ((WMenu
*) desc
->parent
)->flags
.inside_handler
= 0;
2092 void wMenuMove(WMenu
* menu
, int x
, int y
, int submenus
)
2102 XMoveWindow(dpy
, menu
->frame
->core
->window
, x
, y
);
2104 if (submenus
> 0 && menu
->selected_entry
>= 0) {
2105 i
= menu
->entries
[menu
->selected_entry
]->cascade
;
2107 if (i
>= 0 && menu
->cascades
) {
2108 submenu
= menu
->cascades
[i
];
2109 if (submenu
->flags
.mapped
&& !submenu
->flags
.buttoned
) {
2110 if (wPreferences
.align_menus
) {
2111 wMenuMove(submenu
, x
+ MENUW(menu
), y
, submenus
);
2113 wMenuMove(submenu
, x
+ MENUW(menu
),
2114 y
+ submenu
->entry_height
* menu
->selected_entry
, submenus
);
2119 if (submenus
< 0 && menu
->parent
!= NULL
&& menu
->parent
->flags
.mapped
&& !menu
->parent
->flags
.buttoned
) {
2120 if (wPreferences
.align_menus
) {
2121 wMenuMove(menu
->parent
, x
- MENUW(menu
->parent
), y
, submenus
);
2123 wMenuMove(menu
->parent
, x
- MENUW(menu
->parent
), menu
->frame_y
2124 - menu
->parent
->entry_height
* menu
->parent
->selected_entry
, submenus
);
2129 static void changeMenuLevels(WMenu
* menu
, int lower
)
2134 ChangeStackingLevel(menu
->frame
->core
, (!menu
->parent
? WMMainMenuLevel
: WMSubmenuLevel
));
2135 wRaiseFrame(menu
->frame
->core
);
2136 menu
->flags
.lowered
= 0;
2138 ChangeStackingLevel(menu
->frame
->core
, WMNormalLevel
);
2139 wLowerFrame(menu
->frame
->core
);
2140 menu
->flags
.lowered
= 1;
2142 for (i
= 0; i
< menu
->cascade_no
; i
++) {
2143 if (menu
->cascades
[i
]
2144 && !menu
->cascades
[i
]->flags
.buttoned
&& menu
->cascades
[i
]->flags
.lowered
!= lower
) {
2145 changeMenuLevels(menu
->cascades
[i
], lower
);
2150 static void menuTitleDoubleClick(WCoreWindow
* sender
, void *data
, XEvent
* event
)
2155 if (event
->xbutton
.state
& MOD_MASK
) {
2156 if (menu
->flags
.lowered
) {
2161 changeMenuLevels(menu
, lower
);
2165 static void menuTitleMouseDown(WCoreWindow
* sender
, void *data
, XEvent
* event
)
2170 int x
= menu
->frame_x
, y
= menu
->frame_y
;
2171 int dx
= event
->xbutton
.x_root
, dy
= event
->xbutton
.y_root
;
2175 /* can't touch the menu copy */
2176 if (menu
->flags
.brother
)
2179 if (event
->xbutton
.button
!= Button1
&& event
->xbutton
.button
!= Button2
)
2182 if (event
->xbutton
.state
& MOD_MASK
) {
2183 wLowerFrame(menu
->frame
->core
);
2186 wRaiseFrame(menu
->frame
->core
);
2191 /* lower/raise all submenus */
2193 if (tmp
->selected_entry
>= 0 && tmp
->cascades
&& tmp
->entries
[tmp
->selected_entry
]->cascade
>= 0) {
2194 tmp
= tmp
->cascades
[tmp
->entries
[tmp
->selected_entry
]->cascade
];
2195 if (!tmp
|| !tmp
->flags
.mapped
)
2198 wLowerFrame(tmp
->frame
->core
);
2200 wRaiseFrame(tmp
->frame
->core
);
2206 /* tear off the menu if it's a root menu or a cascade
2208 if (!menu
->flags
.buttoned
&& !menu
->flags
.brother
&& (!menu
->flags
.app_menu
|| menu
->parent
!= NULL
)) {
2209 menu
->flags
.buttoned
= 1;
2210 wFrameWindowShowButton(menu
->frame
, WFF_RIGHT_BUTTON
);
2212 /* turn off selected menu entry in parent menu */
2213 selectEntry(menu
->parent
, -1);
2215 /* make parent map the copy in place of the original */
2216 for (i
= 0; i
< menu
->parent
->cascade_no
; i
++) {
2217 if (menu
->parent
->cascades
[i
] == menu
) {
2218 menu
->parent
->cascades
[i
] = menu
->brother
;
2227 WMMaskEvent(dpy
, ButtonMotionMask
| ButtonReleaseMask
| ButtonPressMask
| ExposureMask
, &ev
);
2231 x
+= ev
.xmotion
.x_root
- dx
;
2232 y
+= ev
.xmotion
.y_root
- dy
;
2233 dx
= ev
.xmotion
.x_root
;
2234 dy
= ev
.xmotion
.y_root
;
2235 wMenuMove(menu
, x
, y
, True
);
2237 if (abs(ev
.xmotion
.x_root
- dx
) > MOVE_THRESHOLD
2238 || abs(ev
.xmotion
.y_root
- dy
) > MOVE_THRESHOLD
) {
2240 XGrabPointer(dpy
, menu
->frame
->titlebar
->window
, False
,
2241 ButtonMotionMask
| ButtonReleaseMask
2243 GrabModeAsync
, GrabModeAsync
, None
,
2244 wCursor
[WCUR_MOVE
], CurrentTime
);
2253 if (ev
.xbutton
.button
!= event
->xbutton
.button
)
2255 XUngrabPointer(dpy
, CurrentTime
);
2266 *----------------------------------------------------------------------
2268 * Handles mouse click on the close button of menus. The menu is
2269 * closed when the button is clicked.
2272 * The closed menu is reinserted at it's parent menus
2274 *----------------------------------------------------------------------
2276 static void menuCloseClick(WCoreWindow
* sender
, void *data
, XEvent
* event
)
2278 WMenu
*menu
= (WMenu
*) data
;
2279 WMenu
*parent
= menu
->parent
;
2283 for (i
= 0; i
< parent
->cascade_no
; i
++) {
2284 /* find the entry that points to the copy */
2285 if (parent
->cascades
[i
] == menu
->brother
) {
2286 /* make it point to the original */
2287 parent
->cascades
[i
] = menu
;
2288 menu
->parent
= parent
;
2296 static void saveMenuInfo(WMPropList
* dict
, WMenu
* menu
, WMPropList
* key
)
2298 WMPropList
*value
, *list
;
2301 snprintf(buffer
, sizeof(buffer
), "%i,%i", menu
->frame_x
, menu
->frame_y
);
2302 value
= WMCreatePLString(buffer
);
2303 list
= WMCreatePLArray(value
, NULL
);
2304 if (menu
->flags
.lowered
)
2305 WMAddToPLArray(list
, WMCreatePLString("lowered"));
2306 WMPutInPLDictionary(dict
, key
, list
);
2307 WMReleasePropList(value
);
2308 WMReleasePropList(list
);
2311 void wMenuSaveState(WScreen
* scr
)
2313 WMPropList
*menus
, *key
;
2316 menus
= WMCreatePLDictionary(NULL
, NULL
);
2318 if (scr
->switch_menu
&& scr
->switch_menu
->flags
.buttoned
) {
2319 key
= WMCreatePLString("SwitchMenu");
2320 saveMenuInfo(menus
, scr
->switch_menu
, key
);
2321 WMReleasePropList(key
);
2325 if (saveMenuRecurs(menus
, scr
, scr
->root_menu
))
2328 if (scr
->workspace_menu
&& scr
->workspace_menu
->flags
.buttoned
) {
2329 key
= WMCreatePLString("WorkspaceMenu");
2330 saveMenuInfo(menus
, scr
->workspace_menu
, key
);
2331 WMReleasePropList(key
);
2336 key
= WMCreatePLString("Menus");
2337 WMPutInPLDictionary(scr
->session_state
, key
, menus
);
2338 WMReleasePropList(key
);
2340 WMReleasePropList(menus
);
2343 static Bool
getMenuPath(WMenu
* menu
, char *buffer
, int bufSize
)
2348 if (!menu
->flags
.titled
|| !menu
->frame
->title
[0])
2351 len
= strlen(menu
->frame
->title
);
2356 ok
= getMenuPath(menu
->parent
, buffer
, bufSize
- len
- 1);
2361 strcat(buffer
, "\\");
2362 strcat(buffer
, menu
->frame
->title
);
2367 static Bool
saveMenuRecurs(WMPropList
* menus
, WScreen
* scr
, WMenu
* menu
)
2370 int save_menus
= 0, i
;
2374 if (menu
->flags
.brother
)
2375 menu
= menu
->brother
;
2377 if (menu
->flags
.buttoned
&& menu
!= scr
->switch_menu
) {
2380 ok
= getMenuPath(menu
, buffer
, 510);
2383 key
= WMCreatePLString(buffer
);
2384 saveMenuInfo(menus
, menu
, key
);
2385 WMReleasePropList(key
);
2391 for (i
= 0; i
< menu
->cascade_no
; i
++) {
2392 if (saveMenuRecurs(menus
, scr
, menu
->cascades
[i
]))
2399 #define COMPLAIN(key) wwarning(_("bad value in menus state info: %s"), key)
2401 static Bool
getMenuInfo(WMPropList
* info
, int *x
, int *y
, Bool
* lowered
)
2407 if (WMIsPLArray(info
)) {
2409 pos
= WMGetFromPLArray(info
, 0);
2410 flags
= WMGetFromPLArray(info
, 1);
2411 if (flags
!= NULL
&& WMIsPLString(flags
) && WMGetFromPLString(flags
) != NULL
2412 && strcmp(WMGetFromPLString(flags
), "lowered") == 0) {
2419 if (pos
!= NULL
&& WMIsPLString(pos
)) {
2420 if (sscanf(WMGetFromPLString(pos
), "%i,%i", x
, y
) != 2)
2421 COMPLAIN("Position");
2423 COMPLAIN("(position, flags...)");
2430 static int restoreMenu(WScreen
* scr
, WMPropList
* menu
, int which
)
2433 Bool lowered
= False
;
2434 WMenu
*pmenu
= NULL
;
2439 if (!getMenuInfo(menu
, &x
, &y
, &lowered
))
2442 if (which
& WSS_SWITCHMENU
) {
2443 OpenSwitchMenu(scr
, x
, y
, False
);
2444 pmenu
= scr
->switch_menu
;
2448 int width
= MENUW(pmenu
);
2449 int height
= MENUH(pmenu
);
2450 WMRect rect
= wGetRectForHead(scr
, wGetHeadForPointerLocation(scr
));
2453 changeMenuLevels(pmenu
, True
);
2456 if (x
< rect
.pos
.x
- width
)
2458 if (x
> rect
.pos
.x
+ rect
.size
.width
)
2459 x
= rect
.pos
.x
+ rect
.size
.width
- width
;
2462 if (y
> rect
.pos
.y
+ rect
.size
.height
)
2463 y
= rect
.pos
.y
+ rect
.size
.height
- height
;
2465 wMenuMove(pmenu
, x
, y
, True
);
2466 pmenu
->flags
.buttoned
= 1;
2467 wFrameWindowShowButton(pmenu
->frame
, WFF_RIGHT_BUTTON
);
2473 static int restoreMenuRecurs(WScreen
* scr
, WMPropList
* menus
, WMenu
* menu
, char *path
)
2475 WMPropList
*key
, *entry
;
2480 if (strlen(path
) + strlen(menu
->frame
->title
) > 510)
2483 snprintf(buffer
, sizeof(buffer
), "%s\\%s", path
, menu
->frame
->title
);
2484 key
= WMCreatePLString(buffer
);
2485 entry
= WMGetFromPLDictionary(menus
, key
);
2488 if (entry
&& getMenuInfo(entry
, &x
, &y
, &lowered
)) {
2490 if (!menu
->flags
.mapped
) {
2491 int width
= MENUW(menu
);
2492 int height
= MENUH(menu
);
2493 WMRect rect
= wGetRectForHead(scr
, wGetHeadForPointerLocation(scr
));
2495 wMenuMapAt(menu
, x
, y
, False
);
2498 /* make parent map the copy in place of the original */
2499 for (i
= 0; i
< menu
->parent
->cascade_no
; i
++) {
2500 if (menu
->parent
->cascades
[i
] == menu
) {
2501 menu
->parent
->cascades
[i
] = menu
->brother
;
2507 changeMenuLevels(menu
, True
);
2510 if (x
< rect
.pos
.x
- width
)
2512 if (x
> rect
.pos
.x
+ rect
.size
.width
)
2513 x
= rect
.pos
.x
+ rect
.size
.width
- width
;
2516 if (y
> rect
.pos
.y
+ rect
.size
.height
)
2517 y
= rect
.pos
.y
+ rect
.size
.height
- height
;
2519 wMenuMove(menu
, x
, y
, True
);
2520 menu
->flags
.buttoned
= 1;
2521 wFrameWindowShowButton(menu
->frame
, WFF_RIGHT_BUTTON
);
2526 WMReleasePropList(key
);
2528 for (i
= 0; i
< menu
->cascade_no
; i
++) {
2529 if (restoreMenuRecurs(scr
, menus
, menu
->cascades
[i
], buffer
) != False
)
2536 void wMenuRestoreState(WScreen
* scr
)
2538 WMPropList
*menus
, *menu
, *key
, *skey
;
2540 if (!scr
->session_state
) {
2544 key
= WMCreatePLString("Menus");
2545 menus
= WMGetFromPLDictionary(scr
->session_state
, key
);
2546 WMReleasePropList(key
);
2553 skey
= WMCreatePLString("SwitchMenu");
2554 menu
= WMGetFromPLDictionary(menus
, skey
);
2555 WMReleasePropList(skey
);
2556 restoreMenu(scr
, menu
, WSS_SWITCHMENU
);
2558 if (!scr
->root_menu
) {
2559 OpenRootMenu(scr
, scr
->scr_width
* 2, 0, False
);
2560 wMenuUnmap(scr
->root_menu
);
2562 restoreMenuRecurs(scr
, menus
, scr
->root_menu
, "");
2565 void OpenWorkspaceMenu(WScreen
* scr
, int x
, int y
)
2567 WMenu
*menu
, *parent
;
2570 if (!scr
->root_menu
) {
2571 OpenRootMenu(scr
, scr
->scr_width
* 2, 0, False
);
2572 wMenuUnmap(scr
->root_menu
);
2575 menu
= scr
->workspace_menu
;
2577 if (menu
->flags
.mapped
) {
2578 if (!menu
->flags
.buttoned
) {
2580 parent
= menu
->parent
;
2581 if (parent
&& parent
->selected_entry
>= 0) {
2582 entry
= parent
->entries
[parent
->selected_entry
];
2583 if (parent
->cascades
[entry
->cascade
] == menu
) {
2584 selectEntry(parent
, -1);
2585 wMenuMapAt(menu
, x
, y
, False
);
2589 wRaiseFrame(menu
->frame
->core
);
2590 wMenuMapCopyAt(menu
, x
, y
);
2593 wMenuMapAt(menu
, x
, y
, False
);