Change to the linux kernel coding style
[wmaker-crm.git] / src / menu.c
Commit [+]AuthorDateLineData
9d2e6ef9 scottc1998-09-29 22:36:29 +00001/* menu.c- generic menu, used for root menu, application menus etc.
2 *
9af1c6c4 dan1998-10-21 14:43:47 +00003 * Window Maker window manager
3bed15d6 dan2002-10-17 20:26:30 +00004 *
4153e2fd dan2003-01-16 23:30:45 +00005 * Copyright (c) 1997-2003 Alfredo K. Kojima
6 * Copyright (c) 1998-2003 Dan Pascu
3bed15d6 dan2002-10-17 20:26:30 +00007 *
9d2e6ef9 scottc1998-09-29 22:36:29 +00008 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
3bed15d6 dan2002-10-17 20:26:30 +000020 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
9d2e6ef9 scottc1998-09-29 22:36:29 +000021 * USA.
22 */
23
24#include "wconfig.h"
25
26#include <X11/Xlib.h>
27#include <X11/Xutil.h>
28#include <X11/keysym.h>
29#include <stdlib.h>
30#include <string.h>
31#include <stdio.h>
06f59b99 Carlos R. Mafra2008-11-06 01:37:44 +010032#include <stdint.h>
9d2e6ef9 scottc1998-09-29 22:36:29 +000033#include <unistd.h>
34#include <ctype.h>
b188d55b dan2000-04-12 10:43:43 +000035#if 0
eb87c409 kojima1999-10-20 03:25:06 +000036#include <nana.h>
b188d55b dan2000-04-12 10:43:43 +000037#endif
9d2e6ef9 scottc1998-09-29 22:36:29 +000038#include "WindowMaker.h"
39#include "wcore.h"
40#include "framewin.h"
41#include "menu.h"
42#include "actions.h"
43#include "funcs.h"
44#include "stacking.h"
45#include "text.h"
a10214a5 kojima2002-11-28 22:04:07 +000046#include "xinerama.h"
9aca0d5f dan2004-10-12 01:34:32 +000047#include "workspace.h"
9d2e6ef9 scottc1998-09-29 22:36:29 +000048
9d2e6ef9 scottc1998-09-29 22:36:29 +000049/****** Global Variables ******/
50
51extern Cursor wCursor[WCUR_LAST];
52
53extern XContext wWinContext;
54
55extern WPreferences wPreferences;
56
57#define MOD_MASK wPreferences.modifier_mask
58
9af1c6c4 dan1998-10-21 14:43:47 +000059#define MENU_SCROLL_STEP menuScrollParameters[(int)wPreferences.menu_scroll_speed].steps
60#define MENU_SCROLL_DELAY menuScrollParameters[(int)wPreferences.menu_scroll_speed].delay
9d2e6ef9 scottc1998-09-29 22:36:29 +000061
9d2e6ef9 scottc1998-09-29 22:36:29 +000062#define MENUW(m) ((m)->frame->core->width+2*FRAME_BORDER_WIDTH)
63#define MENUH(m) ((m)->frame->core->height+2*FRAME_BORDER_WIDTH)
64
9d2e6ef9 scottc1998-09-29 22:36:29 +000065/***** Local Stuff ******/
66
879b00a5 kojima2004-10-13 05:09:08 +000067#define WSS_ROOTMENU (1<<0)
68#define WSS_SWITCHMENU (1<<1)
69#define WSS_WSMENU (1<<2)
70
9d2e6ef9 scottc1998-09-29 22:36:29 +000071static struct {
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +020072 int steps;
73 int delay;
9d2e6ef9 scottc1998-09-29 22:36:29 +000074} menuScrollParameters[5] = {
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +020075 {
76 MENU_SCROLL_STEPS_UF, MENU_SCROLL_DELAY_UF}, {
77 MENU_SCROLL_STEPS_F, MENU_SCROLL_DELAY_F}, {
78 MENU_SCROLL_STEPS_M, MENU_SCROLL_DELAY_M}, {
79 MENU_SCROLL_STEPS_S, MENU_SCROLL_DELAY_S}, {
80MENU_SCROLL_STEPS_US, MENU_SCROLL_DELAY_US}};
9d2e6ef9 scottc1998-09-29 22:36:29 +000081
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +020082static void menuMouseDown(WObjDescriptor * desc, XEvent * event);
83static void menuExpose(WObjDescriptor * desc, XEvent * event);
9d2e6ef9 scottc1998-09-29 22:36:29 +000084
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +020085static void menuTitleDoubleClick(WCoreWindow * sender, void *data, XEvent * event);
86static void menuTitleMouseDown(WCoreWindow * sender, void *data, XEvent * event);
9d2e6ef9 scottc1998-09-29 22:36:29 +000087
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +020088static void menuCloseClick(WCoreWindow * sender, void *data, XEvent * event);
9d2e6ef9 scottc1998-09-29 22:36:29 +000089
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +020090static void updateTexture(WMenu * menu);
9d2e6ef9 scottc1998-09-29 22:36:29 +000091
c2434e89 kojima1999-05-01 17:44:43 +000092#ifndef LITE
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +020093static int saveMenuRecurs(WMPropList * menus, WScreen * scr, WMenu * menu);
94static int restoreMenuRecurs(WScreen * scr, WMPropList * menus, WMenu * menu, char *path);
95#endif /* !LITE */
9d2e6ef9 scottc1998-09-29 22:36:29 +000096
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +020097static void selectEntry(WMenu * menu, int entry_no);
98static void closeCascade(WMenu * menu);
9d2e6ef9 scottc1998-09-29 22:36:29 +000099
9df0e167 kojima1999-04-03 03:51:17 +0000100/****** Notification Observers ******/
101
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200102static void appearanceObserver(void *self, WMNotification * notif)
9df0e167 kojima1999-04-03 03:51:17 +0000103{
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200104 WMenu *menu = (WMenu *) self;
105 int flags = (int)(uintptr_t) WMGetNotificationClientData(notif);
106
107 if (!menu->flags.realized)
108 return;
109
110 if (WMGetNotificationName(notif) == WNMenuAppearanceSettingsChanged) {
111 if (flags & WFontSettings) {
112 menu->flags.realized = 0;
113 wMenuRealize(menu);
114 }
115 if (flags & WTextureSettings) {
116 if (!menu->flags.brother)
117 updateTexture(menu);
118 }
119 if (flags & (WTextureSettings | WColorSettings)) {
120 wMenuPaint(menu);
121 }
122 } else if (menu->flags.titled) {
123
124 if (flags & WFontSettings) {
125 menu->flags.realized = 0;
126 wMenuRealize(menu);
127 }
128 if (flags & WTextureSettings) {
129 menu->frame->flags.need_texture_remake = 1;
130 }
131 if (flags & (WColorSettings | WTextureSettings)) {
132 wFrameWindowPaint(menu->frame);
133 }
134 }
9df0e167 kojima1999-04-03 03:51:17 +0000135}
136
137/************************************/
138
9d2e6ef9 scottc1998-09-29 22:36:29 +0000139/*
140 *----------------------------------------------------------------------
141 * wMenuCreate--
142 * Creates a new empty menu with the specified title. If main_menu
143 * is True, the created menu will be a main menu, which has some special
144 * properties such as being placed over other normal menus.
145 * If title is NULL, the menu will have no titlebar.
3bed15d6 dan2002-10-17 20:26:30 +0000146 *
9d2e6ef9 scottc1998-09-29 22:36:29 +0000147 * Returns:
148 * The created menu.
3bed15d6 dan2002-10-17 20:26:30 +0000149 *----------------------------------------------------------------------
9d2e6ef9 scottc1998-09-29 22:36:29 +0000150 */
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200151WMenu *wMenuCreate(WScreen * screen, char *title, int main_menu)
9d2e6ef9 scottc1998-09-29 22:36:29 +0000152{
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200153 WMenu *menu;
154 static int brother = 0;
155 int tmp, flags;
3bed15d6 dan2002-10-17 20:26:30 +0000156
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200157 menu = wmalloc(sizeof(WMenu));
9d2e6ef9 scottc1998-09-29 22:36:29 +0000158
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200159 memset(menu, 0, sizeof(WMenu));
9d2e6ef9 scottc1998-09-29 22:36:29 +0000160
161#ifdef SINGLE_MENULEVEL
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200162 tmp = WMSubmenuLevel;
9d2e6ef9 scottc1998-09-29 22:36:29 +0000163#else
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200164 tmp = (main_menu ? WMMainMenuLevel : WMSubmenuLevel);
9d2e6ef9 scottc1998-09-29 22:36:29 +0000165#endif
166
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200167 flags = WFF_SINGLE_STATE | WFF_BORDER;
168 if (title) {
169 flags |= WFF_TITLEBAR | WFF_RIGHT_BUTTON;
170 menu->flags.titled = 1;
171 }
172 menu->frame =
173 wFrameWindowCreate(screen, tmp, 8, 2, 1, 1, &wPreferences.menu_title_clearance, flags,
174 screen->menu_title_texture, NULL,
175 screen->menu_title_color, &screen->menu_title_font);
3bed15d6 dan2002-10-17 20:26:30 +0000176
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200177 menu->frame->core->descriptor.parent = menu;
178 menu->frame->core->descriptor.parent_type = WCLASS_MENU;
179 menu->frame->core->descriptor.handle_mousedown = menuMouseDown;
9d2e6ef9 scottc1998-09-29 22:36:29 +0000180
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200181 wFrameWindowHideButton(menu->frame, WFF_RIGHT_BUTTON);
9d2e6ef9 scottc1998-09-29 22:36:29 +0000182
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200183 if (title) {
184 menu->frame->title = wstrdup(title);
185 }
e7532d18 kojima1999-03-20 02:41:23 +0000186
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200187 menu->frame->flags.justification = WTJ_LEFT;
9d2e6ef9 scottc1998-09-29 22:36:29 +0000188
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200189 menu->frame->rbutton_image = screen->b_pixmaps[WBUT_CLOSE];
3bed15d6 dan2002-10-17 20:26:30 +0000190
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200191 menu->entry_no = 0;
192 menu->alloced_entries = 0;
193 menu->selected_entry = -1;
194 menu->entries = NULL;
3bed15d6 dan2002-10-17 20:26:30 +0000195
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200196 menu->frame_x = screen->app_menu_x;
197 menu->frame_y = screen->app_menu_y;
9d2e6ef9 scottc1998-09-29 22:36:29 +0000198
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200199 menu->frame->child = menu;
9d2e6ef9 scottc1998-09-29 22:36:29 +0000200
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200201 menu->flags.lowered = 0;
3bed15d6 dan2002-10-17 20:26:30 +0000202
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200203 /* create borders */
204 if (title) {
205 /* setup object descriptors */
206 menu->frame->on_mousedown_titlebar = menuTitleMouseDown;
207 menu->frame->on_dblclick_titlebar = menuTitleDoubleClick;
208 }
3bed15d6 dan2002-10-17 20:26:30 +0000209
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200210 menu->frame->on_click_right = menuCloseClick;
3bed15d6 dan2002-10-17 20:26:30 +0000211
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200212 menu->menu = wCoreCreate(menu->frame->core, 0, menu->frame->top_width, menu->frame->core->width, 10);
9d2e6ef9 scottc1998-09-29 22:36:29 +0000213
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200214 menu->menu->descriptor.parent = menu;
215 menu->menu->descriptor.parent_type = WCLASS_MENU;
216 menu->menu->descriptor.handle_expose = menuExpose;
217 menu->menu->descriptor.handle_mousedown = menuMouseDown;
3bed15d6 dan2002-10-17 20:26:30 +0000218
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200219 menu->menu_texture_data = None;
9d2e6ef9 scottc1998-09-29 22:36:29 +0000220
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200221 XMapWindow(dpy, menu->menu->window);
9d2e6ef9 scottc1998-09-29 22:36:29 +0000222
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200223 XFlush(dpy);
3bed15d6 dan2002-10-17 20:26:30 +0000224
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200225 if (!brother) {
226 brother = 1;
227 menu->brother = wMenuCreate(screen, title, main_menu);
228 brother = 0;
229 menu->brother->flags.brother = 1;
230 menu->brother->brother = menu;
231 }
232 WMAddNotificationObserver(appearanceObserver, menu, WNMenuAppearanceSettingsChanged, menu);
9df0e167 kojima1999-04-03 03:51:17 +0000233
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200234 WMAddNotificationObserver(appearanceObserver, menu, WNMenuTitleAppearanceSettingsChanged, menu);
9df0e167 kojima1999-04-03 03:51:17 +0000235
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200236 return menu;
9d2e6ef9 scottc1998-09-29 22:36:29 +0000237}
238
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200239WMenu *wMenuCreateForApp(WScreen * screen, char *title, int main_menu)
9d2e6ef9 scottc1998-09-29 22:36:29 +0000240{
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200241 WMenu *menu;
3bed15d6 dan2002-10-17 20:26:30 +0000242
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200243 menu = wMenuCreate(screen, title, main_menu);
244 if (!menu)
245 return NULL;
246 menu->flags.app_menu = 1;
247 menu->brother->flags.app_menu = 1;
9d2e6ef9 scottc1998-09-29 22:36:29 +0000248
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200249 return menu;
9d2e6ef9 scottc1998-09-29 22:36:29 +0000250}
251
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200252static void insertEntry(WMenu * menu, WMenuEntry * entry, int index)
9d2e6ef9 scottc1998-09-29 22:36:29 +0000253{
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200254 int i;
3bed15d6 dan2002-10-17 20:26:30 +0000255
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200256 for (i = menu->entry_no - 1; i >= index; i--) {
257 menu->entries[i]->order++;
258 menu->entries[i + 1] = menu->entries[i];
259 }
260 menu->entries[index] = entry;
9d2e6ef9 scottc1998-09-29 22:36:29 +0000261}
262
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200263WMenuEntry *wMenuInsertCallback(WMenu * menu, int index, char *text,
264 void (*callback) (WMenu * menu, WMenuEntry * entry), void *clientdata)
9d2e6ef9 scottc1998-09-29 22:36:29 +0000265{
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200266 WMenuEntry *entry;
267
268 menu->flags.realized = 0;
269 menu->brother->flags.realized = 0;
270
271 /* reallocate array if it's too small */
272 if (menu->entry_no >= menu->alloced_entries) {
273 void *tmp;
274
275 tmp = wrealloc(menu->entries, sizeof(WMenuEntry) * (menu->alloced_entries + 5));
276
277 menu->entries = tmp;
278 menu->alloced_entries += 5;
279
280 menu->brother->entries = tmp;
281 menu->brother->alloced_entries = menu->alloced_entries;
282 }
283 entry = wmalloc(sizeof(WMenuEntry));
284 memset(entry, 0, sizeof(WMenuEntry));
285 entry->flags.enabled = 1;
286 entry->text = wstrdup(text);
287 entry->cascade = -1;
288 entry->clientdata = clientdata;
289 entry->callback = callback;
290 if (index < 0 || index >= menu->entry_no) {
291 entry->order = menu->entry_no;
292 menu->entries[menu->entry_no] = entry;
293 } else {
294 entry->order = index;
295 insertEntry(menu, entry, index);
296 }
297
298 menu->entry_no++;
299 menu->brother->entry_no = menu->entry_no;
300
301 return entry;
9d2e6ef9 scottc1998-09-29 22:36:29 +0000302}
303
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200304void wMenuEntrySetCascade(WMenu * menu, WMenuEntry * entry, WMenu * cascade)
9d2e6ef9 scottc1998-09-29 22:36:29 +0000305{
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200306 WMenu *brother = menu->brother;
307 int i, done;
3bed15d6 dan2002-10-17 20:26:30 +0000308
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200309 assert(menu->flags.brother == 0);
3bed15d6 dan2002-10-17 20:26:30 +0000310
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200311 if (entry->cascade >= 0) {
312 menu->flags.realized = 0;
313 brother->flags.realized = 0;
314 }
9d2e6ef9 scottc1998-09-29 22:36:29 +0000315
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200316 cascade->parent = menu;
9d2e6ef9 scottc1998-09-29 22:36:29 +0000317
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200318 cascade->brother->parent = brother;
3bed15d6 dan2002-10-17 20:26:30 +0000319
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200320 done = 0;
321 for (i = 0; i < menu->cascade_no; i++) {
322 if (menu->cascades[i] == NULL) {
323 menu->cascades[i] = cascade;
324 brother->cascades[i] = cascade->brother;
325 done = 1;
326 entry->cascade = i;
327 break;
328 }
329 }
330 if (!done) {
331 entry->cascade = menu->cascade_no;
9d2e6ef9 scottc1998-09-29 22:36:29 +0000332
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200333 menu->cascades = wrealloc(menu->cascades, sizeof(WMenu) * (menu->cascade_no + 1));
334 menu->cascades[menu->cascade_no++] = cascade;
9d2e6ef9 scottc1998-09-29 22:36:29 +0000335
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200336 brother->cascades = wrealloc(brother->cascades, sizeof(WMenu) * (brother->cascade_no + 1));
337 brother->cascades[brother->cascade_no++] = cascade->brother;
338 }
9d2e6ef9 scottc1998-09-29 22:36:29 +0000339
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200340 if (menu->flags.lowered) {
9d2e6ef9 scottc1998-09-29 22:36:29 +0000341
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200342 cascade->flags.lowered = 1;
343 ChangeStackingLevel(cascade->frame->core, WMNormalLevel);
3bed15d6 dan2002-10-17 20:26:30 +0000344
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200345 cascade->brother->flags.lowered = 1;
346 ChangeStackingLevel(cascade->brother->frame->core, WMNormalLevel);
347 }
3bed15d6 dan2002-10-17 20:26:30 +0000348
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200349 if (!menu->flags.realized)
350 wMenuRealize(menu);
9d2e6ef9 scottc1998-09-29 22:36:29 +0000351}
352
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200353void wMenuEntryRemoveCascade(WMenu * menu, WMenuEntry * entry)
3bed15d6 dan2002-10-17 20:26:30 +0000354{
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200355 assert(menu->flags.brother == 0);
3bed15d6 dan2002-10-17 20:26:30 +0000356
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200357 /* destroy cascade menu */
358 if (entry->cascade >= 0 && menu->cascades && menu->cascades[entry->cascade] != NULL) {
9d2e6ef9 scottc1998-09-29 22:36:29 +0000359
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200360 wMenuDestroy(menu->cascades[entry->cascade], True);
9d2e6ef9 scottc1998-09-29 22:36:29 +0000361
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200362 menu->cascades[entry->cascade] = NULL;
363 menu->brother->cascades[entry->cascade] = NULL;
9d2e6ef9 scottc1998-09-29 22:36:29 +0000364
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200365 entry->cascade = -1;
366 }
9d2e6ef9 scottc1998-09-29 22:36:29 +0000367}
368
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200369void wMenuRemoveItem(WMenu * menu, int index)
9d2e6ef9 scottc1998-09-29 22:36:29 +0000370{
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200371 int i;
3bed15d6 dan2002-10-17 20:26:30 +0000372
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200373 if (menu->flags.brother) {
374 wMenuRemoveItem(menu->brother, index);
375 return;
376 }
3bed15d6 dan2002-10-17 20:26:30 +0000377
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200378 if (index >= menu->entry_no)
379 return;
9d2e6ef9 scottc1998-09-29 22:36:29 +0000380
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200381 /* destroy cascade menu */
382 wMenuEntryRemoveCascade(menu, menu->entries[index]);
3bed15d6 dan2002-10-17 20:26:30 +0000383
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200384 /* destroy unshared data */
9d2e6ef9 scottc1998-09-29 22:36:29 +0000385
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200386 if (menu->entries[index]->text)
387 wfree(menu->entries[index]->text);
3bed15d6 dan2002-10-17 20:26:30 +0000388
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200389 if (menu->entries[index]->rtext)
390 wfree(menu->entries[index]->rtext);
3bed15d6 dan2002-10-17 20:26:30 +0000391
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200392 if (menu->entries[index]->free_cdata && menu->entries[index]->clientdata)
393 (*menu->entries[index]->free_cdata) (menu->entries[index]->clientdata);
9d2e6ef9 scottc1998-09-29 22:36:29 +0000394
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200395 wfree(menu->entries[index]);
3bed15d6 dan2002-10-17 20:26:30 +0000396
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200397 for (i = index; i < menu->entry_no - 1; i++) {
398 menu->entries[i + 1]->order--;
399 menu->entries[i] = menu->entries[i + 1];
400 }
401 menu->entry_no--;
402 menu->brother->entry_no--;
9d2e6ef9 scottc1998-09-29 22:36:29 +0000403}
404
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200405static Pixmap renderTexture(WMenu * menu)
3bf0fa92 kojima1999-04-11 03:01:22 +0000406{
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200407 RImage *img;
408 Pixmap pix;
409 int i;
410 RColor light;
411 RColor dark;
412 RColor mid;
413 WScreen *scr = menu->menu->screen_ptr;
414 WTexture *texture = scr->menu_item_texture;
415
416 if (wPreferences.menu_style == MS_NORMAL) {
417 img = wTextureRenderImage(texture, menu->menu->width, menu->entry_height, WREL_MENUENTRY);
418 } else {
419 img = wTextureRenderImage(texture, menu->menu->width, menu->menu->height + 1, WREL_MENUENTRY);
420 }
421 if (!img) {
422 wwarning(_("could not render texture: %s"), RMessageForError(RErrorCode));
423
424 return None;
425 }
426
427 if (wPreferences.menu_style == MS_SINGLE_TEXTURE) {
428 light.alpha = 0;
429 light.red = light.green = light.blue = 80;
430
431 dark.alpha = 255;
432 dark.red = dark.green = dark.blue = 0;
433
434 mid.alpha = 0;
435 mid.red = mid.green = mid.blue = 40;
436
437 for (i = 1; i < menu->entry_no; i++) {
438 ROperateLine(img, RSubtractOperation, 0, i * menu->entry_height - 2,
439 menu->menu->width - 1, i * menu->entry_height - 2, &mid);
440
441 RDrawLine(img, 0, i * menu->entry_height - 1,
442 menu->menu->width - 1, i * menu->entry_height - 1, &dark);
443
444 ROperateLine(img, RAddOperation, 0, i * menu->entry_height,
445 menu->menu->width - 1, i * menu->entry_height, &light);
446 }
447 }
448 if (!RConvertImage(scr->rcontext, img, &pix)) {
449 wwarning(_("error rendering image:%s"), RMessageForError(RErrorCode));
450 }
451 RReleaseImage(img);
452
453 return pix;
3bf0fa92 kojima1999-04-11 03:01:22 +0000454}
455
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200456static void updateTexture(WMenu * menu)
9df0e167 kojima1999-04-03 03:51:17 +0000457{
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200458 WScreen *scr = menu->menu->screen_ptr;
9df0e167 kojima1999-04-03 03:51:17 +0000459
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200460 /* setup background texture */
461 if (scr->menu_item_texture->any.type != WTEX_SOLID) {
462 if (!menu->flags.brother) {
463 FREE_PIXMAP(menu->menu_texture_data);
9d2e6ef9 scottc1998-09-29 22:36:29 +0000464
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200465 menu->menu_texture_data = renderTexture(menu);
466
467 XSetWindowBackgroundPixmap(dpy, menu->menu->window, menu->menu_texture_data);
468 XClearWindow(dpy, menu->menu->window);
469
470 XSetWindowBackgroundPixmap(dpy, menu->brother->menu->window, menu->menu_texture_data);
471 XClearWindow(dpy, menu->brother->menu->window);
472 }
473 } else {
474 XSetWindowBackground(dpy, menu->menu->window, scr->menu_item_texture->any.color.pixel);
475 XClearWindow(dpy, menu->menu->window);
476 }
9d2e6ef9 scottc1998-09-29 22:36:29 +0000477}
478
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200479void wMenuRealize(WMenu * menu)
480{
481 int i;
482 int width, rwidth, mrwidth, mwidth;
483 int theight, twidth, eheight;
484 WScreen *scr = menu->frame->screen_ptr;
485 static int brother_done = 0;
486 int flags;
487
488 if (!brother_done) {
489 brother_done = 1;
490 wMenuRealize(menu->brother);
491 brother_done = 0;
492 }
493
494 flags = WFF_SINGLE_STATE | WFF_BORDER;
495 if (menu->flags.titled)
496 flags |= WFF_TITLEBAR | WFF_RIGHT_BUTTON;
497
498 wFrameWindowUpdateBorders(menu->frame, flags);
499
500 if (menu->flags.titled) {
501 twidth = WMWidthOfString(scr->menu_title_font, menu->frame->title, strlen(menu->frame->title));
502 theight = menu->frame->top_width;
503 twidth += theight + (wPreferences.new_style ? 16 : 8);
504 } else {
505 twidth = 0;
506 theight = 0;
507 }
508 eheight = WMFontHeight(scr->menu_entry_font) + 6 + wPreferences.menu_text_clearance * 2;
509 menu->entry_height = eheight;
510 mrwidth = 0;
511 mwidth = 0;
512 for (i = 0; i < menu->entry_no; i++) {
513 char *text;
514
515 /* search widest text */
516 text = menu->entries[i]->text;
517 width = WMWidthOfString(scr->menu_entry_font, text, strlen(text)) + 10;
518
519 if (menu->entries[i]->flags.indicator) {
520 width += MENU_INDICATOR_SPACE;
521 }
522
523 if (width > mwidth)
524 mwidth = width;
525
526 /* search widest text on right */
527 text = menu->entries[i]->rtext;
528 if (text)
529 rwidth = WMWidthOfString(scr->menu_entry_font, text, strlen(text))
530 + 10;
531 else if (menu->entries[i]->cascade >= 0)
532 rwidth = 16;
533 else
534 rwidth = 4;
535
536 if (rwidth > mrwidth)
537 mrwidth = rwidth;
538 }
539 mwidth += mrwidth;
540
541 if (mwidth < twidth)
542 mwidth = twidth;
543
544 wCoreConfigure(menu->menu, 0, theight, mwidth, menu->entry_no * eheight - 1);
545
546 wFrameWindowResize(menu->frame, mwidth, menu->entry_no * eheight - 1
547 + menu->frame->top_width + menu->frame->bottom_width);
548
549 updateTexture(menu);
550
551 menu->flags.realized = 1;
552
553 if (menu->flags.mapped)
554 wMenuPaint(menu);
555 if (menu->brother->flags.mapped)
556 wMenuPaint(menu->brother);
557}
9d2e6ef9 scottc1998-09-29 22:36:29 +0000558
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200559void wMenuDestroy(WMenu * menu, int recurse)
9d2e6ef9 scottc1998-09-29 22:36:29 +0000560{
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200561 int i;
9d2e6ef9 scottc1998-09-29 22:36:29 +0000562
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200563 WMRemoveNotificationObserver(menu);
3bed15d6 dan2002-10-17 20:26:30 +0000564
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200565 /* remove any pending timers */
566 if (menu->timer)
567 WMDeleteTimerHandler(menu->timer);
568 menu->timer = NULL;
3bed15d6 dan2002-10-17 20:26:30 +0000569
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200570 /* call destroy handler */
571 if (menu->on_destroy)
572 (*menu->on_destroy) (menu);
3bed15d6 dan2002-10-17 20:26:30 +0000573
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200574 /* Destroy items if this menu own them. If this is the "brother" menu,
575 * leave them alone as it is shared by them.
576 */
577 if (!menu->flags.brother) {
578 for (i = 0; i < menu->entry_no; i++) {
9d2e6ef9 scottc1998-09-29 22:36:29 +0000579
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200580 wfree(menu->entries[i]->text);
9d2e6ef9 scottc1998-09-29 22:36:29 +0000581
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200582 if (menu->entries[i]->rtext)
583 wfree(menu->entries[i]->rtext);
7e07b7b5 kojima1999-04-10 19:32:34 +0000584#ifdef USER_MENU
585
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200586 if (menu->entries[i]->instances) {
587 WMReleasePropList(menu->entries[i]->instances);
588 }
589#endif /* USER_MENU */
9d2e6ef9 scottc1998-09-29 22:36:29 +0000590
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200591 if (menu->entries[i]->free_cdata && menu->entries[i]->clientdata) {
592 (*menu->entries[i]->free_cdata) (menu->entries[i]->clientdata);
593 }
594 wfree(menu->entries[i]);
595 }
3bed15d6 dan2002-10-17 20:26:30 +0000596
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200597 if (recurse) {
598 for (i = 0; i < menu->cascade_no; i++) {
599 if (menu->cascades[i]) {
600 if (menu->cascades[i]->flags.brother)
601 wMenuDestroy(menu->cascades[i]->brother, recurse);
602 else
603 wMenuDestroy(menu->cascades[i], recurse);
604 }
605 }
606 }
3bed15d6 dan2002-10-17 20:26:30 +0000607
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200608 if (menu->entries)
609 wfree(menu->entries);
3bed15d6 dan2002-10-17 20:26:30 +0000610
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200611 }
9d2e6ef9 scottc1998-09-29 22:36:29 +0000612
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200613 FREE_PIXMAP(menu->menu_texture_data);
3bed15d6 dan2002-10-17 20:26:30 +0000614
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200615 if (menu->cascades)
616 wfree(menu->cascades);
9d2e6ef9 scottc1998-09-29 22:36:29 +0000617
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200618 wCoreDestroy(menu->menu);
619 wFrameWindowDestroy(menu->frame);
3bed15d6 dan2002-10-17 20:26:30 +0000620
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200621 /* destroy copy of this menu */
622 if (!menu->flags.brother && menu->brother)
623 wMenuDestroy(menu->brother, False);
3bed15d6 dan2002-10-17 20:26:30 +0000624
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200625 wfree(menu);
9d2e6ef9 scottc1998-09-29 22:36:29 +0000626}
627
3bf0fa92 kojima1999-04-11 03:01:22 +0000628#define F_NORMAL 0
629#define F_TOP 1
630#define F_BOTTOM 2
631#define F_NONE 3
632
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200633static void drawFrame(WScreen * scr, Drawable win, int y, int w, int h, int type)
9d2e6ef9 scottc1998-09-29 22:36:29 +0000634{
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200635 XSegment segs[2];
636 int i;
637
638 i = 0;
639 segs[i].x1 = segs[i].x2 = w - 1;
640 segs[i].y1 = y;
641 segs[i].y2 = y + h - 1;
642 i++;
643 if (type != F_TOP && type != F_NONE) {
644 segs[i].x1 = 1;
645 segs[i].y1 = segs[i].y2 = y + h - 2;
646 segs[i].x2 = w - 1;
647 i++;
648 }
649 XDrawSegments(dpy, win, scr->menu_item_auxtexture->dim_gc, segs, i);
650
651 i = 0;
652 segs[i].x1 = 0;
653 segs[i].y1 = y;
654 segs[i].x2 = 0;
655 segs[i].y2 = y + h - 1;
656 i++;
657 if (type != F_BOTTOM && type != F_NONE) {
658 segs[i].x1 = 0;
659 segs[i].y1 = y;
660 segs[i].x2 = w - 1;
661 segs[i].y2 = y;
662 i++;
663 }
664 XDrawSegments(dpy, win, scr->menu_item_auxtexture->light_gc, segs, i);
665
666 if (type != F_TOP && type != F_NONE)
667 XDrawLine(dpy, win, scr->menu_item_auxtexture->dark_gc, 0, y + h - 1, w - 1, y + h - 1);
9d2e6ef9 scottc1998-09-29 22:36:29 +0000668}
669
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200670static void paintEntry(WMenu * menu, int index, int selected)
9d2e6ef9 scottc1998-09-29 22:36:29 +0000671{
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200672 WScreen *scr = menu->frame->screen_ptr;
673 Window win = menu->menu->window;
674 WMenuEntry *entry = menu->entries[index];
675 GC light, dim, dark;
676 WMColor *color;
677 int x, y, w, h, tw;
678 int type;
679
680 if (!menu->flags.realized)
681 return;
682 h = menu->entry_height;
683 w = menu->menu->width;
684 y = index * h;
685
686 light = scr->menu_item_auxtexture->light_gc;
687 dim = scr->menu_item_auxtexture->dim_gc;
688 dark = scr->menu_item_auxtexture->dark_gc;
689
690 if (wPreferences.menu_style == MS_FLAT && menu->entry_no > 1) {
691 if (index == 0)
692 type = F_TOP;
693 else if (index == menu->entry_no - 1)
694 type = F_BOTTOM;
695 else
696 type = F_NONE;
697 } else {
698 type = F_NORMAL;
699 }
700
701 /* paint background */
702 if (selected) {
703 XFillRectangle(dpy, win, WMColorGC(scr->select_color), 1, y + 1, w - 2, h - 3);
704 if (scr->menu_item_texture->any.type == WTEX_SOLID)
705 drawFrame(scr, win, y, w, h, type);
706 } else {
707 if (scr->menu_item_texture->any.type == WTEX_SOLID) {
708 XClearArea(dpy, win, 0, y + 1, w - 1, h - 3, False);
709 /* draw the frame */
710 drawFrame(scr, win, y, w, h, type);
711 } else {
712 XClearArea(dpy, win, 0, y, w, h, False);
713 }
714 }
715
716 if (selected) {
717 if (entry->flags.enabled)
718 color = scr->select_text_color;
719 else
720 color = scr->dtext_color;
721 } else if (!entry->flags.enabled) {
722 color = scr->dtext_color;
723 } else {
724 color = scr->mtext_color;
725 }
726 /* draw text */
727 x = 5;
728 if (entry->flags.indicator)
729 x += MENU_INDICATOR_SPACE + 2;
730
731 WMDrawString(scr->wmscreen, win, color, scr->menu_entry_font,
732 x, 3 + y + wPreferences.menu_text_clearance, entry->text, strlen(entry->text));
733
734 if (entry->cascade >= 0) {
735 /* draw the cascade indicator */
736 XDrawLine(dpy, win, dim, w - 11, y + 6, w - 6, y + h / 2 - 1);
737 XDrawLine(dpy, win, light, w - 11, y + h - 8, w - 6, y + h / 2 - 1);
738 XDrawLine(dpy, win, dark, w - 12, y + 6, w - 12, y + h - 8);
739 }
740
741 /* draw indicator */
742 if (entry->flags.indicator && entry->flags.indicator_on) {
743 int iw, ih;
744 WPixmap *indicator;
745
746 switch (entry->flags.indicator_type) {
747 case MI_CHECK:
748 indicator = scr->menu_check_indicator;
749 break;
750 case MI_MINIWINDOW:
751 indicator = scr->menu_mini_indicator;
752 break;
753 case MI_HIDDEN:
754 indicator = scr->menu_hide_indicator;
755 break;
756 case MI_SHADED:
757 indicator = scr->menu_shade_indicator;
758 break;
759 case MI_DIAMOND:
760 default:
761 indicator = scr->menu_radio_indicator;
762 break;
763 }
764
765 iw = indicator->width;
766 ih = indicator->height;
767 XSetClipMask(dpy, scr->copy_gc, indicator->mask);
768 XSetClipOrigin(dpy, scr->copy_gc, 5, y + (h - ih) / 2);
769 if (selected) {
770 if (entry->flags.enabled) {
771 XSetForeground(dpy, scr->copy_gc, WMColorPixel(scr->select_text_color));
772 } else {
773 XSetForeground(dpy, scr->copy_gc, WMColorPixel(scr->dtext_color));
774 }
775 } else {
776 if (entry->flags.enabled) {
777 XSetForeground(dpy, scr->copy_gc, WMColorPixel(scr->mtext_color));
778 } else {
779 XSetForeground(dpy, scr->copy_gc, WMColorPixel(scr->dtext_color));
780 }
781 }
782 XFillRectangle(dpy, win, scr->copy_gc, 5, y + (h - ih) / 2, iw, ih);
783 /*
784 XCopyArea(dpy, indicator->image, win, scr->copy_gc, 0, 0,
785 iw, ih, 5, y+(h-ih)/2);
786 */
787 XSetClipOrigin(dpy, scr->copy_gc, 0, 0);
788 }
789
790 /* draw right text */
791
792 if (entry->rtext && entry->cascade < 0) {
793 tw = WMWidthOfString(scr->menu_entry_font, entry->rtext, strlen(entry->rtext));
794
795 WMDrawString(scr->wmscreen, win, color, scr->menu_entry_font, w - 6 - tw,
796 y + 3 + wPreferences.menu_text_clearance, entry->rtext, strlen(entry->rtext));
797 }
9d2e6ef9 scottc1998-09-29 22:36:29 +0000798}
799
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200800static void move_menus(WMenu * menu, int x, int y)
9d2e6ef9 scottc1998-09-29 22:36:29 +0000801{
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200802 while (menu->parent) {
803 menu = menu->parent;
804 x -= MENUW(menu);
805 if (!wPreferences.align_menus && menu->selected_entry >= 0) {
806 y -= menu->selected_entry * menu->entry_height;
807 }
808 }
809 wMenuMove(menu, x, y, True);
9d2e6ef9 scottc1998-09-29 22:36:29 +0000810}
811
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200812static void makeVisible(WMenu * menu)
9d2e6ef9 scottc1998-09-29 22:36:29 +0000813{
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200814 WScreen *scr = menu->frame->screen_ptr;
815 int x1, y1, x2, y2, new_x, new_y, move;
816 WMRect rect = wGetRectForHead(scr, wGetHeadForPointerLocation(scr));
817
818 if (menu->entry_no < 0)
819 return;
820
821 x1 = menu->frame_x;
822 y1 = menu->frame_y + menu->frame->top_width + menu->selected_entry * menu->entry_height;
823 x2 = x1 + MENUW(menu);
824 y2 = y1 + menu->entry_height;
825
826 new_x = x1;
827 new_y = y1;
828 move = 0;
829
830 if (x1 < rect.pos.x) {
831 new_x = rect.pos.x;
832 move = 1;
833 } else if (x2 >= rect.pos.x + rect.size.width) {
834 new_x = rect.pos.x + rect.size.width - MENUW(menu) - 1;
835 move = 1;
836 }
837
838 if (y1 < rect.pos.y) {
839 new_y = rect.pos.y;
840 move = 1;
841 } else if (y2 >= rect.pos.y + rect.size.height) {
842 new_y = rect.pos.y + rect.size.height - menu->entry_height - 1;
843 move = 1;
844 }
845
846 new_y = new_y - menu->frame->top_width - menu->selected_entry * menu->entry_height;
847 move_menus(menu, new_x, new_y);
9d2e6ef9 scottc1998-09-29 22:36:29 +0000848}
849
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200850static int check_key(WMenu * menu, XKeyEvent * event)
9d2e6ef9 scottc1998-09-29 22:36:29 +0000851{
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200852 int i, ch, s;
853 char buffer[32];
854
855 if (XLookupString(event, buffer, 32, NULL, NULL) < 1)
856 return -1;
857
858 ch = toupper(buffer[0]);
859
860 s = (menu->selected_entry >= 0 ? menu->selected_entry + 1 : 0);
861
862 again:
863 for (i = s; i < menu->entry_no; i++) {
864 if (ch == toupper(menu->entries[i]->text[0])) {
865 return i;
866 }
867 }
868 /* no match. Retry from start, if previous started from a selected entry */
869 if (s != 0) {
870 s = 0;
871 goto again;
872 }
873 return -1;
9d2e6ef9 scottc1998-09-29 22:36:29 +0000874}
875
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200876static int keyboardMenu(WMenu * menu)
9d2e6ef9 scottc1998-09-29 22:36:29 +0000877{
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200878 XEvent event;
879 KeySym ksym = NoSymbol;
880 int done = 0;
881 int index;
882 WMenuEntry *entry;
883 int old_pos_x = menu->frame_x;
884 int old_pos_y = menu->frame_y;
885 int new_x = old_pos_x, new_y = old_pos_y;
886 WMRect rect = wGetRectForHead(menu->frame->screen_ptr,
887 wGetHeadForPointerLocation(menu->frame->screen_ptr));
888
889 if (menu->flags.editing)
890 return False;
891
892 XGrabKeyboard(dpy, menu->frame->core->window, True, GrabModeAsync, GrabModeAsync, CurrentTime);
893
894 if (menu->frame_y + menu->frame->top_width >= rect.pos.y + rect.size.height)
895 new_y = rect.pos.y + rect.size.height - menu->frame->top_width;
896
897 if (menu->frame_x + MENUW(menu) >= rect.pos.x + rect.size.width)
898 new_x = rect.pos.x + rect.size.width - MENUW(menu) - 1;
899
900 move_menus(menu, new_x, new_y);
901
902 while (!done && menu->flags.mapped) {
903 XAllowEvents(dpy, AsyncKeyboard, CurrentTime);
904 WMMaskEvent(dpy, ExposureMask | ButtonMotionMask | ButtonPressMask
905 | ButtonReleaseMask | KeyPressMask | KeyReleaseMask | SubstructureNotifyMask, &event);
906
907 switch (event.type) {
908 case KeyPress:
909 ksym = XLookupKeysym(&event.xkey, 0);
910 switch (ksym) {
911 case XK_Escape:
912 done = 1;
913 break;
914
915 case XK_Home:
1e351cb5 kojima2000-05-21 16:34:23 +0000916#ifdef XK_KP_Home
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200917 case XK_KP_Home:
1e351cb5 kojima2000-05-21 16:34:23 +0000918#endif
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200919 selectEntry(menu, 0);
920 makeVisible(menu);
921 break;
3bed15d6 dan2002-10-17 20:26:30 +0000922
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200923 case XK_End:
1e351cb5 kojima2000-05-21 16:34:23 +0000924#ifdef XK_KP_End
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200925 case XK_KP_End:
1e351cb5 kojima2000-05-21 16:34:23 +0000926#endif
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200927 selectEntry(menu, menu->entry_no - 1);
928 makeVisible(menu);
929 break;
3bed15d6 dan2002-10-17 20:26:30 +0000930
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200931 case XK_Up:
9d2e6ef9 scottc1998-09-29 22:36:29 +0000932#ifdef ARROWLESS_KBD
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200933 case XK_k:
9d2e6ef9 scottc1998-09-29 22:36:29 +0000934#endif
1e351cb5 kojima2000-05-21 16:34:23 +0000935#ifdef XK_KP_Up
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200936 case XK_KP_Up:
1e351cb5 kojima2000-05-21 16:34:23 +0000937#endif
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200938 if (menu->selected_entry <= 0)
939 selectEntry(menu, menu->entry_no - 1);
940 else
941 selectEntry(menu, menu->selected_entry - 1);
942 makeVisible(menu);
943 break;
944
945 case XK_Down:
9d2e6ef9 scottc1998-09-29 22:36:29 +0000946#ifdef ARROWLESS_KBD
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200947 case XK_j:
9d2e6ef9 scottc1998-09-29 22:36:29 +0000948#endif
1e351cb5 kojima2000-05-21 16:34:23 +0000949#ifdef XK_KP_Down
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200950 case XK_KP_Down:
1e351cb5 kojima2000-05-21 16:34:23 +0000951#endif
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200952 if (menu->selected_entry < 0)
953 selectEntry(menu, 0);
954 else if (menu->selected_entry == menu->entry_no - 1)
955 selectEntry(menu, 0);
956 else if (menu->selected_entry < menu->entry_no - 1)
957 selectEntry(menu, menu->selected_entry + 1);
958 makeVisible(menu);
959 break;
960
961 case XK_Right:
9d2e6ef9 scottc1998-09-29 22:36:29 +0000962#ifdef ARROWLESS_KBD
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200963 case XK_l:
9d2e6ef9 scottc1998-09-29 22:36:29 +0000964#endif
1e351cb5 kojima2000-05-21 16:34:23 +0000965#ifdef XK_KP_Right
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200966 case XK_KP_Right:
1e351cb5 kojima2000-05-21 16:34:23 +0000967#endif
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200968 if (menu->selected_entry >= 0) {
969 WMenuEntry *entry;
970 entry = menu->entries[menu->selected_entry];
9d2e6ef9 scottc1998-09-29 22:36:29 +0000971
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200972 if (entry->cascade >= 0 && menu->cascades
973 && menu->cascades[entry->cascade]->entry_no > 0) {
9d2e6ef9 scottc1998-09-29 22:36:29 +0000974
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200975 XUngrabKeyboard(dpy, CurrentTime);
9d2e6ef9 scottc1998-09-29 22:36:29 +0000976
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200977 selectEntry(menu->cascades[entry->cascade], 0);
978 if (!keyboardMenu(menu->cascades[entry->cascade]))
979 done = 1;
9d2e6ef9 scottc1998-09-29 22:36:29 +0000980
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200981 XGrabKeyboard(dpy, menu->frame->core->window, True,
982 GrabModeAsync, GrabModeAsync, CurrentTime);
983 }
984 }
985 break;
9d2e6ef9 scottc1998-09-29 22:36:29 +0000986
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200987 case XK_Left:
9d2e6ef9 scottc1998-09-29 22:36:29 +0000988#ifdef ARROWLESS_KBD
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200989 case XK_h:
9d2e6ef9 scottc1998-09-29 22:36:29 +0000990#endif
1e351cb5 kojima2000-05-21 16:34:23 +0000991#ifdef XK_KP_Left
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200992 case XK_KP_Left:
1e351cb5 kojima2000-05-21 16:34:23 +0000993#endif
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200994 if (menu->parent != NULL && menu->parent->selected_entry >= 0) {
995 selectEntry(menu, -1);
996 move_menus(menu, old_pos_x, old_pos_y);
997 return True;
998 }
999 break;
1000
1001 case XK_Return:
3b46cf38 dan2004-10-31 00:57:25 +00001002#ifdef XK_KP_Enter
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +02001003 case XK_KP_Enter:
3b46cf38 dan2004-10-31 00:57:25 +00001004#endif
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02001005 done = 2;
1006 break;
1007
1008 default:
1009 index = check_key(menu, &event.xkey);
1010 if (index >= 0) {
1011 selectEntry(menu, index);
1012 }
1013 }
1014 break;
1015
1016 default:
1017 if (event.type == ButtonPress)
1018 done = 1;
1019
1020 WMHandleEvent(&event);
1021 }
1022 }
1023
1024 XUngrabKeyboard(dpy, CurrentTime);
1025
1026 if (done == 2 && menu->selected_entry >= 0) {
1027 entry = menu->entries[menu->selected_entry];
1028 } else {
1029 entry = NULL;
1030 }
1031
1032 if (entry && entry->callback != NULL && entry->flags.enabled && entry->cascade < 0) {
9d2e6ef9 scottc1998-09-29 22:36:29 +00001033#if (MENU_BLINK_COUNT > 0)
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02001034 int sel = menu->selected_entry;
1035 int i;
1036
1037 for (i = 0; i < MENU_BLINK_COUNT; i++) {
1038 paintEntry(menu, sel, False);
1039 XSync(dpy, 0);
1040 wusleep(MENU_BLINK_DELAY);
1041 paintEntry(menu, sel, True);
1042 XSync(dpy, 0);
1043 wusleep(MENU_BLINK_DELAY);
1044 }
9d2e6ef9 scottc1998-09-29 22:36:29 +00001045#endif
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02001046 selectEntry(menu, -1);
1047
1048 if (!menu->flags.buttoned) {
1049 wMenuUnmap(menu);
1050 move_menus(menu, old_pos_x, old_pos_y);
1051 }
1052 closeCascade(menu);
1053
1054 (*entry->callback) (menu, entry);
1055 } else {
1056 if (!menu->flags.buttoned) {
1057 wMenuUnmap(menu);
1058 move_menus(menu, old_pos_x, old_pos_y);
1059 }
1060 selectEntry(menu, -1);
1061 }
1062
1063 /* returns True if returning from a submenu to a parent menu,
1064 * False if exiting from menu */
1065 return False;
9d2e6ef9 scottc1998-09-29 22:36:29 +00001066}
1067
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +02001068void wMenuMapAt(WMenu * menu, int x, int y, int keyboard)
9d2e6ef9 scottc1998-09-29 22:36:29 +00001069{
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02001070 WMRect rect = wGetRectForHead(menu->frame->screen_ptr,
1071 wGetHeadForPointerLocation(menu->frame->screen_ptr));
1072
1073 if (!menu->flags.realized) {
1074 menu->flags.realized = 1;
1075 wMenuRealize(menu);
1076 }
1077 if (!menu->flags.mapped) {
1078 if (wPreferences.wrap_menus) {
1079 if (x < rect.pos.x)
1080 x = rect.pos.x;
1081 if (y < rect.pos.y)
1082 y = rect.pos.y;
1083 if (x + MENUW(menu) > rect.pos.x + rect.size.width)
1084 x = rect.pos.x + rect.size.width - MENUW(menu);
1085 if (y + MENUH(menu) > rect.pos.y + rect.size.height)
1086 y = rect.pos.y + rect.size.height - MENUH(menu);
1087 }
1088
1089 XMoveWindow(dpy, menu->frame->core->window, x, y);
1090 menu->frame_x = (x < 0) ? 0 : x;
1091 menu->frame_y = (y < 0) ? 0 : y;
1092 XMapWindow(dpy, menu->frame->core->window);
1093 wRaiseFrame(menu->frame->core);
1094 menu->flags.mapped = 1;
1095 } else {
1096 selectEntry(menu, 0);
1097 }
1098
1099 if (keyboard)
1100 keyboardMenu(menu);
9d2e6ef9 scottc1998-09-29 22:36:29 +00001101}
1102
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +02001103void wMenuMap(WMenu * menu)
3bed15d6 dan2002-10-17 20:26:30 +00001104{
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02001105 if (!menu->flags.realized) {
1106 menu->flags.realized = 1;
1107 wMenuRealize(menu);
1108 }
1109 if (menu->flags.app_menu && menu->parent == NULL) {
1110 menu->frame_x = menu->frame->screen_ptr->app_menu_x;
1111 menu->frame_y = menu->frame->screen_ptr->app_menu_y;
1112 XMoveWindow(dpy, menu->frame->core->window, menu->frame_x, menu->frame_y);
1113 }
1114 XMapWindow(dpy, menu->frame->core->window);
1115 wRaiseFrame(menu->frame->core);
1116 menu->flags.mapped = 1;
9d2e6ef9 scottc1998-09-29 22:36:29 +00001117}
1118
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +02001119void wMenuUnmap(WMenu * menu)
9d2e6ef9 scottc1998-09-29 22:36:29 +00001120{
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02001121 int i;
1122
1123 XUnmapWindow(dpy, menu->frame->core->window);
1124 if (menu->flags.titled && menu->flags.buttoned) {
1125 wFrameWindowHideButton(menu->frame, WFF_RIGHT_BUTTON);
1126 }
1127 menu->flags.buttoned = 0;
1128 menu->flags.mapped = 0;
1129 menu->flags.open_to_left = 0;
1130
1131 for (i = 0; i < menu->cascade_no; i++) {
1132 if (menu->cascades[i] != NULL
1133 && menu->cascades[i]->flags.mapped && !menu->cascades[i]->flags.buttoned) {
1134
1135 wMenuUnmap(menu->cascades[i]);
1136 }
1137 }
1138 menu->selected_entry = -1;
3bed15d6 dan2002-10-17 20:26:30 +00001139}
9d2e6ef9 scottc1998-09-29 22:36:29 +00001140
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +02001141void wMenuPaint(WMenu * menu)
3bed15d6 dan2002-10-17 20:26:30 +00001142{
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +02001143 int i;
3bed15d6 dan2002-10-17 20:26:30 +00001144
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02001145 if (!menu->flags.mapped) {
1146 return;
1147 }
9d2e6ef9 scottc1998-09-29 22:36:29 +00001148
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02001149 /* paint entries */
1150 for (i = 0; i < menu->entry_no; i++) {
1151 paintEntry(menu, i, i == menu->selected_entry);
1152 }
9d2e6ef9 scottc1998-09-29 22:36:29 +00001153}
1154
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +02001155void wMenuSetEnabled(WMenu * menu, int index, int enable)
9d2e6ef9 scottc1998-09-29 22:36:29 +00001156{
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02001157 if (index >= menu->entry_no)
1158 return;
1159 menu->entries[index]->flags.enabled = enable;
1160 paintEntry(menu, index, index == menu->selected_entry);
1161 paintEntry(menu->brother, index, index == menu->selected_entry);
9d2e6ef9 scottc1998-09-29 22:36:29 +00001162}
1163
9d2e6ef9 scottc1998-09-29 22:36:29 +00001164/* ====================================================================== */
1165
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +02001166static void editEntry(WMenu * menu, WMenuEntry * entry)
9d2e6ef9 scottc1998-09-29 22:36:29 +00001167{
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02001168 WTextInput *text;
1169 XEvent event;
1170 WObjDescriptor *desc;
1171 char *t;
1172 int done = 0;
1173 Window old_focus;
1174 int old_revert;
9d2e6ef9 scottc1998-09-29 22:36:29 +00001175
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +02001176 menu->flags.editing = 1;
9d2e6ef9 scottc1998-09-29 22:36:29 +00001177
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02001178 text = wTextCreate(menu->menu, 1, menu->entry_height * entry->order,
1179 menu->menu->width - 2, menu->entry_height - 1);
3bed15d6 dan2002-10-17 20:26:30 +00001180
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02001181 wTextPutText(text, entry->text);
1182 XGetInputFocus(dpy, &old_focus, &old_revert);
1183 XSetInputFocus(dpy, text->core->window, RevertToNone, CurrentTime);
3bed15d6 dan2002-10-17 20:26:30 +00001184
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02001185 if (XGrabKeyboard(dpy, text->core->window, True, GrabModeAsync, GrabModeAsync, CurrentTime) != GrabSuccess) {
1186 wwarning(_("could not grab keyboard"));
1187 wTextDestroy(text);
9d2e6ef9 scottc1998-09-29 22:36:29 +00001188
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02001189 wSetFocusTo(menu->frame->screen_ptr, menu->frame->screen_ptr->focused_window);
1190 return;
1191 }
9d2e6ef9 scottc1998-09-29 22:36:29 +00001192
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02001193 while (!done && !text->done) {
1194 XSync(dpy, 0);
1195 XAllowEvents(dpy, AsyncKeyboard | AsyncPointer, CurrentTime);
1196 XSync(dpy, 0);
1197 WMNextEvent(dpy, &event);
9d2e6ef9 scottc1998-09-29 22:36:29 +00001198
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02001199 if (XFindContext(dpy, event.xany.window, wWinContext, (XPointer *) & desc) == XCNOENT)
1200 desc = NULL;
9d2e6ef9 scottc1998-09-29 22:36:29 +00001201
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +02001202 if ((desc != NULL) && (desc->handle_anything != NULL)) {
9d2e6ef9 scottc1998-09-29 22:36:29 +00001203
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +02001204 (*desc->handle_anything) (desc, &event);
9d2e6ef9 scottc1998-09-29 22:36:29 +00001205
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02001206 } else {
1207 switch (event.type) {
1208 case ButtonPress:
1209 XAllowEvents(dpy, ReplayPointer, CurrentTime);
1210 done = 1;
3bed15d6 dan2002-10-17 20:26:30 +00001211
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02001212 default:
1213 WMHandleEvent(&event);
1214 break;
1215 }
1216 }
1217 }
9d2e6ef9 scottc1998-09-29 22:36:29 +00001218
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +02001219 XSetInputFocus(dpy, old_focus, old_revert, CurrentTime);
9d2e6ef9 scottc1998-09-29 22:36:29 +00001220
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +02001221 wSetFocusTo(menu->frame->screen_ptr, menu->frame->screen_ptr->focused_window);
9d2e6ef9 scottc1998-09-29 22:36:29 +00001222
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02001223 t = wTextGetText(text);
1224 /* if !t, the user has canceled editing */
1225 if (t) {
1226 if (entry->text)
1227 wfree(entry->text);
1228 entry->text = wstrdup(t);
9d2e6ef9 scottc1998-09-29 22:36:29 +00001229
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02001230 menu->flags.realized = 0;
1231 }
1232 wTextDestroy(text);
3bed15d6 dan2002-10-17 20:26:30 +00001233
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +02001234 XUngrabKeyboard(dpy, CurrentTime);
3bed15d6 dan2002-10-17 20:26:30 +00001235
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02001236 if (t && menu->on_edit)
1237 (*menu->on_edit) (menu, entry);
3bed15d6 dan2002-10-17 20:26:30 +00001238
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +02001239 menu->flags.editing = 0;
3bed15d6 dan2002-10-17 20:26:30 +00001240
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02001241 if (!menu->flags.realized)
1242 wMenuRealize(menu);
9d2e6ef9 scottc1998-09-29 22:36:29 +00001243}
1244
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +02001245static void selectEntry(WMenu * menu, int entry_no)
9d2e6ef9 scottc1998-09-29 22:36:29 +00001246{
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02001247 WMenuEntry *entry;
1248 WMenu *submenu;
1249 int old_entry;
1250
1251 if (menu->entries == NULL)
1252 return;
1253
1254 if (entry_no >= menu->entry_no)
1255 return;
1256
1257 old_entry = menu->selected_entry;
1258 menu->selected_entry = entry_no;
1259
1260 if (old_entry != entry_no) {
1261
1262 /* unselect previous entry */
1263 if (old_entry >= 0) {
1264 paintEntry(menu, old_entry, False);
1265 entry = menu->entries[old_entry];
1266
1267 /* unmap cascade */
1268 if (entry->cascade >= 0 && menu->cascades) {
1269 if (!menu->cascades[entry->cascade]->flags.buttoned) {
1270 wMenuUnmap(menu->cascades[entry->cascade]);
1271 }
1272 }
1273 }
1274
1275 if (entry_no < 0) {
1276 menu->selected_entry = -1;
1277 return;
1278 }
1279 entry = menu->entries[entry_no];
1280
1281 if (entry->cascade >= 0 && menu->cascades && entry->flags.enabled) {
1282 /* Callback for when the submenu is opened.
1283 */
1284 submenu = menu->cascades[entry->cascade];
1285 if (submenu && submenu->flags.brother)
1286 submenu = submenu->brother;
1287
1288 if (entry->callback) {
1289 /* Only call the callback if the submenu is not yet mapped.
1290 */
1291 if (menu->flags.brother) {
1292 if (!submenu || !submenu->flags.mapped)
1293 (*entry->callback) (menu->brother, entry);
1294 } else {
1295 if (!submenu || !submenu->flags.buttoned)
1296 (*entry->callback) (menu, entry);
1297 }
1298 }
1299
1300 /* the submenu menu might have changed */
1301 submenu = menu->cascades[entry->cascade];
1302
1303 /* map cascade */
1304 if (!submenu->flags.mapped) {
1305 int x, y;
1306
1307 if (!submenu->flags.realized)
1308 wMenuRealize(submenu);
1309 if (wPreferences.wrap_menus) {
1310 if (menu->flags.open_to_left)
1311 submenu->flags.open_to_left = 1;
1312
1313 if (submenu->flags.open_to_left) {
1314 x = menu->frame_x - MENUW(submenu);
1315 if (x < 0) {
1316 x = 0;
1317 submenu->flags.open_to_left = 0;
1318 }
1319 } else {
1320 x = menu->frame_x + MENUW(menu);
1321
1322 if (x + MENUW(submenu)
1323 >= menu->frame->screen_ptr->scr_width) {
1324
1325 x = menu->frame_x - MENUW(submenu);
1326 submenu->flags.open_to_left = 1;
1327 }
1328 }
1329 } else {
1330 x = menu->frame_x + MENUW(menu);
1331 }
1332
1333 if (wPreferences.align_menus) {
1334 y = menu->frame_y;
1335 } else {
1336 y = menu->frame_y + menu->entry_height * entry_no;
1337 if (menu->flags.titled)
1338 y += menu->frame->top_width;
1339 if (menu->cascades[entry->cascade]->flags.titled)
1340 y -= menu->cascades[entry->cascade]->frame->top_width;
1341 }
1342
1343 wMenuMapAt(menu->cascades[entry->cascade], x, y, False);
1344 menu->cascades[entry->cascade]->parent = menu;
1345 } else {
1346 return;
1347 }
1348 }
1349 paintEntry(menu, entry_no, True);
1350 }
9d2e6ef9 scottc1998-09-29 22:36:29 +00001351}
1352
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +02001353static WMenu *findMenu(WScreen * scr, int *x_ret, int *y_ret)
9d2e6ef9 scottc1998-09-29 22:36:29 +00001354{
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02001355 WMenu *menu;
1356 WObjDescriptor *desc;
1357 Window root_ret, win, junk_win;
1358 int x, y, wx, wy;
1359 unsigned int mask;
1360
1361 XQueryPointer(dpy, scr->root_win, &root_ret, &win, &x, &y, &wx, &wy, &mask);
1362
1363 if (win == None)
1364 return NULL;
1365
1366 if (XFindContext(dpy, win, wWinContext, (XPointer *) & desc) == XCNOENT)
1367 return NULL;
1368
1369 if (desc->parent_type == WCLASS_MENU) {
1370 menu = (WMenu *) desc->parent;
1371 XTranslateCoordinates(dpy, root_ret, menu->menu->window, wx, wy, x_ret, y_ret, &junk_win);
1372 return menu;
1373 }
1374 return NULL;
9d2e6ef9 scottc1998-09-29 22:36:29 +00001375}
1376
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +02001377static void closeCascade(WMenu * menu)
9d2e6ef9 scottc1998-09-29 22:36:29 +00001378{
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +02001379 WMenu *parent = menu->parent;
9d2e6ef9 scottc1998-09-29 22:36:29 +00001380
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +02001381 if (menu->flags.brother || (!menu->flags.buttoned && (!menu->flags.app_menu || menu->parent != NULL))) {
9d2e6ef9 scottc1998-09-29 22:36:29 +00001382
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02001383 selectEntry(menu, -1);
1384 XSync(dpy, 0);
9d2e6ef9 scottc1998-09-29 22:36:29 +00001385#if (MENU_BLINK_DELAY > 2)
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +02001386 wusleep(MENU_BLINK_DELAY / 2);
9d2e6ef9 scottc1998-09-29 22:36:29 +00001387#endif
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02001388 wMenuUnmap(menu);
1389 while (parent != NULL
1390 && (parent->parent != NULL || !parent->flags.app_menu || parent->flags.brother)
1391 && !parent->flags.buttoned) {
1392 selectEntry(parent, -1);
1393 wMenuUnmap(parent);
1394 parent = parent->parent;
1395 }
1396 if (parent)
1397 selectEntry(parent, -1);
1398 }
9d2e6ef9 scottc1998-09-29 22:36:29 +00001399}
1400
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +02001401static void closeBrotherCascadesOf(WMenu * menu)
9d2e6ef9 scottc1998-09-29 22:36:29 +00001402{
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02001403 WMenu *tmp;
1404 int i;
1405
1406 for (i = 0; i < menu->cascade_no; i++) {
1407 if (menu->cascades[i]->flags.brother) {
1408 tmp = menu->cascades[i];
1409 } else {
1410 tmp = menu->cascades[i]->brother;
1411 }
1412 if (tmp->flags.mapped) {
1413 selectEntry(tmp->parent, -1);
1414 closeBrotherCascadesOf(tmp);
1415 break;
1416 }
1417 }
9d2e6ef9 scottc1998-09-29 22:36:29 +00001418}
1419
9d2e6ef9 scottc1998-09-29 22:36:29 +00001420#define getEntryAt(menu, x, y) ((y)<0 ? -1 : (y)/(menu->entry_height))
1421
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +02001422static WMenu *parentMenu(WMenu * menu)
9d2e6ef9 scottc1998-09-29 22:36:29 +00001423{
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02001424 WMenu *parent;
1425 WMenuEntry *entry;
1426
1427 if (menu->flags.buttoned)
1428 return menu;
1429
1430 while (menu->parent && menu->parent->flags.mapped) {
1431 parent = menu->parent;
1432 if (parent->selected_entry < 0)
1433 break;
1434 entry = parent->entries[parent->selected_entry];
1435 if (!entry->flags.enabled || entry->cascade < 0 || !parent->cascades ||
1436 parent->cascades[entry->cascade] != menu)
1437 break;
1438 menu = parent;
1439 if (menu->flags.buttoned)
1440 break;
1441 }
1442
1443 return menu;
9d2e6ef9 scottc1998-09-29 22:36:29 +00001444}
1445
9d2e6ef9 scottc1998-09-29 22:36:29 +00001446/*
1447 * Will raise the passed menu, if submenu = 0
1448 * If submenu > 0 will also raise all mapped submenus
1449 * until the first buttoned one
1450 * If submenu < 0 will also raise all mapped parent menus
1451 * until the first buttoned one
1452 */
1453
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +02001454static void raiseMenus(WMenu * menu, int submenus)
9d2e6ef9 scottc1998-09-29 22:36:29 +00001455{
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02001456 WMenu *submenu;
1457 int i;
1458
1459 if (!menu)
1460 return;
1461
1462 wRaiseFrame(menu->frame->core);
1463
1464 if (submenus > 0 && menu->selected_entry >= 0) {
1465 i = menu->entries[menu->selected_entry]->cascade;
1466 if (i >= 0 && menu->cascades) {
1467 submenu = menu->cascades[i];
1468 if (submenu->flags.mapped && !submenu->flags.buttoned)
1469 raiseMenus(submenu, submenus);
1470 }
1471 }
1472 if (submenus < 0 && !menu->flags.buttoned && menu->parent && menu->parent->flags.mapped)
1473 raiseMenus(menu->parent, submenus);
9d2e6ef9 scottc1998-09-29 22:36:29 +00001474}
1475
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +02001476WMenu *wMenuUnderPointer(WScreen * screen)
9d2e6ef9 scottc1998-09-29 22:36:29 +00001477{
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02001478 WObjDescriptor *desc;
1479 Window root_ret, win;
1480 int dummy;
1481 unsigned int mask;
9d2e6ef9 scottc1998-09-29 22:36:29 +00001482
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +02001483 XQueryPointer(dpy, screen->root_win, &root_ret, &win, &dummy, &dummy, &dummy, &dummy, &mask);
9d2e6ef9 scottc1998-09-29 22:36:29 +00001484
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02001485 if (win == None)
1486 return NULL;
9d2e6ef9 scottc1998-09-29 22:36:29 +00001487
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02001488 if (XFindContext(dpy, win, wWinContext, (XPointer *) & desc) == XCNOENT)
1489 return NULL;
9d2e6ef9 scottc1998-09-29 22:36:29 +00001490
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02001491 if (desc->parent_type == WCLASS_MENU)
1492 return (WMenu *) desc->parent;
1493 return NULL;
9d2e6ef9 scottc1998-09-29 22:36:29 +00001494}
1495
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +02001496static void getPointerPosition(WScreen * scr, int *x, int *y)
9d2e6ef9 scottc1998-09-29 22:36:29 +00001497{
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02001498 Window root_ret, win;
1499 int wx, wy;
1500 unsigned int mask;
9d2e6ef9 scottc1998-09-29 22:36:29 +00001501
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +02001502 XQueryPointer(dpy, scr->root_win, &root_ret, &win, x, y, &wx, &wy, &mask);
9d2e6ef9 scottc1998-09-29 22:36:29 +00001503}
1504
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +02001505static void getScrollAmount(WMenu * menu, int *hamount, int *vamount)
9d2e6ef9 scottc1998-09-29 22:36:29 +00001506{
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02001507 WScreen *scr = menu->menu->screen_ptr;
1508 int menuX1 = menu->frame_x;
1509 int menuY1 = menu->frame_y;
1510 int menuX2 = menu->frame_x + MENUW(menu);
1511 int menuY2 = menu->frame_y + MENUH(menu);
1512 int xroot, yroot;
1513 WMRect rect = wGetRectForHead(scr, wGetHeadForPointerLocation(scr));
277d0055 dan2003-03-04 13:14:55 +00001514
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02001515 *hamount = 0;
1516 *vamount = 0;
3bed15d6 dan2002-10-17 20:26:30 +00001517
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +02001518 getPointerPosition(scr, &xroot, &yroot);
3bed15d6 dan2002-10-17 20:26:30 +00001519
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02001520 if (xroot <= (rect.pos.x + 1) && menuX1 < rect.pos.x) {
1521 /* scroll to the right */
1522 *hamount = WMIN(MENU_SCROLL_STEP, abs(menuX1));
9d2e6ef9 scottc1998-09-29 22:36:29 +00001523
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02001524 } else if (xroot >= (rect.pos.x + rect.size.width - 2) && menuX2 > (rect.pos.x + rect.size.width - 1)) {
1525 /* scroll to the left */
1526 *hamount = WMIN(MENU_SCROLL_STEP, abs(menuX2 - rect.pos.x - rect.size.width - 1));
277d0055 dan2003-03-04 13:14:55 +00001527
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02001528 if (*hamount == 0)
1529 *hamount = 1;
277d0055 dan2003-03-04 13:14:55 +00001530
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02001531 *hamount = -*hamount;
1532 }
277d0055 dan2003-03-04 13:14:55 +00001533
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02001534 if (yroot <= (rect.pos.y + 1) && menuY1 < rect.pos.y) {
1535 /* scroll down */
1536 *vamount = WMIN(MENU_SCROLL_STEP, abs(menuY1));
277d0055 dan2003-03-04 13:14:55 +00001537
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02001538 } else if (yroot >= (rect.pos.y + rect.size.height - 2) && menuY2 > (rect.pos.y + rect.size.height - 1)) {
1539 /* scroll up */
1540 *vamount = WMIN(MENU_SCROLL_STEP, abs(menuY2 - rect.pos.y - rect.size.height - 2));
277d0055 dan2003-03-04 13:14:55 +00001541
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02001542 *vamount = -*vamount;
1543 }
9d2e6ef9 scottc1998-09-29 22:36:29 +00001544}
1545
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +02001546static void dragScrollMenuCallback(void *data)
9d2e6ef9 scottc1998-09-29 22:36:29 +00001547{
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02001548 WMenu *menu = (WMenu *) data;
1549 WScreen *scr = menu->menu->screen_ptr;
1550 WMenu *parent = parentMenu(menu);
1551 int hamount, vamount;
1552 int x, y;
1553 int newSelectedEntry;
1554
1555 getScrollAmount(menu, &hamount, &vamount);
1556
1557 if (hamount != 0 || vamount != 0) {
1558 wMenuMove(parent, parent->frame_x + hamount, parent->frame_y + vamount, True);
1559 if (findMenu(scr, &x, &y)) {
1560 newSelectedEntry = getEntryAt(menu, x, y);
1561 selectEntry(menu, newSelectedEntry);
1562 } else {
1563 /* Pointer fell outside of menu. If the selected entry is
1564 * not a submenu, unselect it */
1565 if (menu->selected_entry >= 0 && menu->entries[menu->selected_entry]->cascade < 0)
1566 selectEntry(menu, -1);
1567 newSelectedEntry = 0;
1568 }
1569
1570 /* paranoid check */
1571 if (newSelectedEntry >= 0) {
1572 /* keep scrolling */
1573 menu->timer = WMAddTimerHandler(MENU_SCROLL_DELAY, dragScrollMenuCallback, menu);
1574 } else {
1575 menu->timer = NULL;
1576 }
1577 } else {
1578 /* don't need to scroll anymore */
1579 menu->timer = NULL;
1580 if (findMenu(scr, &x, &y)) {
1581 newSelectedEntry = getEntryAt(menu, x, y);
1582 selectEntry(menu, newSelectedEntry);
1583 }
1584 }
9d2e6ef9 scottc1998-09-29 22:36:29 +00001585}
1586
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +02001587static void scrollMenuCallback(void *data)
9d2e6ef9 scottc1998-09-29 22:36:29 +00001588{
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02001589 WMenu *menu = (WMenu *) data;
1590 WMenu *parent = parentMenu(menu);
1591 int hamount = 0; /* amount to scroll */
1592 int vamount = 0;
9d2e6ef9 scottc1998-09-29 22:36:29 +00001593
9d1306bf id2001-04-16 13:33:50 +00001594#ifdef VIRTUAL_DESKTOP
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02001595 /* don't scroll if it is in vdesk mode */
1596 if (!wPreferences.vdesk_enable)
9d1306bf id2001-04-16 13:33:50 +00001597#endif
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +02001598 getScrollAmount(menu, &hamount, &vamount);
9d2e6ef9 scottc1998-09-29 22:36:29 +00001599
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02001600 if (hamount != 0 || vamount != 0) {
1601 wMenuMove(parent, parent->frame_x + hamount, parent->frame_y + vamount, True);
9d2e6ef9 scottc1998-09-29 22:36:29 +00001602
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02001603 /* keep scrolling */
1604 menu->timer = WMAddTimerHandler(MENU_SCROLL_DELAY, scrollMenuCallback, menu);
1605 } else {
1606 /* don't need to scroll anymore */
1607 menu->timer = NULL;
1608 }
1609}
9d2e6ef9 scottc1998-09-29 22:36:29 +00001610
1611#define MENU_SCROLL_BORDER 5
1612
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +02001613static int isPointNearBoder(WMenu * menu, int x, int y)
9d2e6ef9 scottc1998-09-29 22:36:29 +00001614{
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02001615 int menuX1 = menu->frame_x;
1616 int menuY1 = menu->frame_y;
1617 int menuX2 = menu->frame_x + MENUW(menu);
1618 int menuY2 = menu->frame_y + MENUH(menu);
1619 int flag = 0;
1620 int head = wGetHeadForPoint(menu->frame->screen_ptr, wmkpoint(x, y));
1621 WMRect rect = wGetRectForHead(menu->frame->screen_ptr, head);
1622
1623 /* XXX: handle screen joins properly !! */
1624
1625 if (x >= menuX1 && x <= menuX2 &&
1626 (y < rect.pos.y + MENU_SCROLL_BORDER || y >= rect.pos.y + rect.size.height - MENU_SCROLL_BORDER))
1627 flag = 1;
1628 else if (y >= menuY1 && y <= menuY2 &&
1629 (x < rect.pos.x + MENU_SCROLL_BORDER || x >= rect.pos.x + rect.size.width - MENU_SCROLL_BORDER))
1630 flag = 1;
1631
1632 return flag;
9d2e6ef9 scottc1998-09-29 22:36:29 +00001633}
1634
0bebc5a7 id1999-04-22 15:21:29 +00001635typedef struct _delay {
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02001636 WMenu *menu;
1637 int ox, oy;
0bebc5a7 id1999-04-22 15:21:29 +00001638} _delay;
1639
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +02001640static void leaving(_delay * dl)
0bebc5a7 id1999-04-22 15:21:29 +00001641{
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02001642 wMenuMove(dl->menu, dl->ox, dl->oy, True);
1643 dl->menu->jump_back = NULL;
1644 dl->menu->menu->screen_ptr->flags.jump_back_pending = 0;
1645 wfree(dl);
0bebc5a7 id1999-04-22 15:21:29 +00001646}
1647
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +02001648void wMenuScroll(WMenu * menu, XEvent * event)
9d2e6ef9 scottc1998-09-29 22:36:29 +00001649{
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02001650 WMenu *smenu;
1651 WMenu *omenu = parentMenu(menu);
1652 WScreen *scr = menu->frame->screen_ptr;
1653 int done = 0;
1654 int jump_back = 0;
1655 int old_frame_x = omenu->frame_x;
1656 int old_frame_y = omenu->frame_y;
1657 XEvent ev;
1658
1659 if (omenu->jump_back)
1660 WMDeleteTimerWithClientData(omenu->jump_back);
1661
1662 if (( /*omenu->flags.buttoned && */ !wPreferences.wrap_menus)
1663 || omenu->flags.app_menu) {
1664 jump_back = 1;
1665 }
1666
1667 if (!wPreferences.wrap_menus)
1668 raiseMenus(omenu, True);
1669 else
1670 raiseMenus(menu, False);
1671
1672 if (!menu->timer)
1673 scrollMenuCallback(menu);
1674
1675 while (!done) {
1676 int x, y, on_border, on_x_edge, on_y_edge, on_title;
1677 WMRect rect;
1678
1679 WMNextEvent(dpy, &ev);
1680 switch (ev.type) {
1681 case EnterNotify:
1682 WMHandleEvent(&ev);
1683 case MotionNotify:
1684 x = (ev.type == MotionNotify) ? ev.xmotion.x_root : ev.xcrossing.x_root;
1685 y = (ev.type == MotionNotify) ? ev.xmotion.y_root : ev.xcrossing.y_root;
1686
1687 /* on_border is != 0 if the pointer is between the menu
1688 * and the screen border and is close enough to the border */
1689 on_border = isPointNearBoder(menu, x, y);
1690
1691 smenu = wMenuUnderPointer(scr);
1692
1693 if ((smenu == NULL && !on_border) || (smenu && parentMenu(smenu) != omenu)) {
1694 done = 1;
1695 break;
1696 }
1697
1698 rect = wGetRectForHead(scr, wGetHeadForPoint(scr, wmkpoint(x, y)));
1699 on_x_edge = x <= rect.pos.x + 1 || x >= rect.pos.x + rect.size.width - 2;
1700 on_y_edge = y <= rect.pos.y + 1 || y >= rect.pos.y + rect.size.height - 2;
1701 on_border = on_x_edge || on_y_edge;
1702
1703 if (!on_border && !jump_back) {
1704 done = 1;
1705 break;
1706 }
1707
1708 if (menu->timer && (smenu != menu || (!on_y_edge && !on_x_edge))) {
1709 WMDeleteTimerHandler(menu->timer);
1710 menu->timer = NULL;
1711 }
1712
1713 if (smenu != NULL)
1714 menu = smenu;
1715
1716 if (!menu->timer)
1717 scrollMenuCallback(menu);
1718 break;
1719 case ButtonPress:
1720 /* True if we push on title, or drag the omenu to other position */
1721 on_title = ev.xbutton.x_root >= omenu->frame_x &&
1722 ev.xbutton.x_root <= omenu->frame_x + MENUW(omenu) &&
1723 ev.xbutton.y_root >= omenu->frame_y &&
1724 ev.xbutton.y_root <= omenu->frame_y + omenu->frame->top_width;
1725 WMHandleEvent(&ev);
1726 smenu = wMenuUnderPointer(scr);
1727 if (smenu == NULL || (smenu && smenu->flags.buttoned && smenu != omenu))
1728 done = 1;
1729 else if (smenu == omenu && on_title) {
1730 jump_back = 0;
1731 done = 1;
1732 }
1733 break;
1734 case KeyPress:
1735 done = 1;
1736 default:
1737 WMHandleEvent(&ev);
1738 break;
1739 }
1740 }
1741
1742 if (menu->timer) {
1743 WMDeleteTimerHandler(menu->timer);
1744 menu->timer = NULL;
1745 }
1746
1747 if (jump_back) {
1748 _delay *delayer;
1749 if (!omenu->jump_back) {
1750 delayer = wmalloc(sizeof(_delay));
1751 delayer->menu = omenu;
1752 delayer->ox = old_frame_x;
1753 delayer->oy = old_frame_y;
1754 omenu->jump_back = delayer;
1755 scr->flags.jump_back_pending = 1;
1756 } else
1757 delayer = omenu->jump_back;
1758 WMAddTimerHandler(MENU_JUMP_BACK_DELAY, (WMCallback *) leaving, delayer);
1759 }
9d2e6ef9 scottc1998-09-29 22:36:29 +00001760}
1761
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +02001762static void menuExpose(WObjDescriptor * desc, XEvent * event)
9d2e6ef9 scottc1998-09-29 22:36:29 +00001763{
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +02001764 wMenuPaint(desc->parent);
9d2e6ef9 scottc1998-09-29 22:36:29 +00001765}
1766
1767typedef struct {
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02001768 int *delayed_select;
1769 WMenu *menu;
1770 WMHandlerID magic;
9d2e6ef9 scottc1998-09-29 22:36:29 +00001771} delay_data;
1772
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +02001773static void delaySelection(void *data)
9d2e6ef9 scottc1998-09-29 22:36:29 +00001774{
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02001775 delay_data *d = (delay_data *) data;
1776 int x, y, entry_no;
1777 WMenu *menu;
1778
1779 d->magic = NULL;
1780
1781 menu = findMenu(d->menu->menu->screen_ptr, &x, &y);
1782 if (menu && (d->menu == menu || d->delayed_select)) {
1783 entry_no = getEntryAt(menu, x, y);
1784 selectEntry(menu, entry_no);
1785 }
1786 if (d->delayed_select)
1787 *(d->delayed_select) = 0;
9d2e6ef9 scottc1998-09-29 22:36:29 +00001788}
1789
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +02001790static void menuMouseDown(WObjDescriptor * desc, XEvent * event)
9d2e6ef9 scottc1998-09-29 22:36:29 +00001791{
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02001792 XButtonEvent *bev = &event->xbutton;
1793 WMenu *menu = desc->parent;
1794 WMenu *smenu;
1795 WScreen *scr = menu->frame->screen_ptr;
1796 WMenuEntry *entry = NULL;
1797 XEvent ev;
1798 int close_on_exit = 0;
1799 int done = 0;
1800 int delayed_select = 0;
1801 int entry_no;
1802 int x, y;
1803 int prevx, prevy;
1804 int old_frame_x = 0;
1805 int old_frame_y = 0;
1806 delay_data d_data = { NULL, NULL, NULL };
1807
1808 /* Doesn't seem to be needed anymore (if delayed selection handler is
1809 * added only if not present). there seem to be no other side effects
1810 * from removing this and it is also possible that it was only added
1811 * to avoid problems with adding the delayed selection timer handler
1812 * multiple times
1813 */
1814 /*if (menu->flags.inside_handler) {
1815 return;
1816 } */
1817 menu->flags.inside_handler = 1;
1818
1819 if (!wPreferences.wrap_menus) {
1820 smenu = parentMenu(menu);
1821 old_frame_x = smenu->frame_x;
1822 old_frame_y = smenu->frame_y;
1823 } else if (event->xbutton.window == menu->frame->core->window) {
1824 /* This is true if the menu was launched with right click on root window */
1825 if (!d_data.magic) {
1826 delayed_select = 1;
1827 d_data.delayed_select = &delayed_select;
1828 d_data.menu = menu;
1829 d_data.magic = WMAddTimerHandler(wPreferences.dblclick_time, delaySelection, &d_data);
1830 }
1831 }
1832
1833 wRaiseFrame(menu->frame->core);
1834
1835 close_on_exit = (bev->send_event || menu->flags.brother);
1836
1837 smenu = findMenu(scr, &x, &y);
1838 if (!smenu) {
1839 x = -1;
1840 y = -1;
1841 } else {
1842 menu = smenu;
1843 }
1844
1845 if (menu->flags.editing) {
1846 goto byebye;
1847 }
1848 entry_no = getEntryAt(menu, x, y);
1849 if (entry_no >= 0) {
1850 entry = menu->entries[entry_no];
1851
1852 if (!close_on_exit && (bev->state & ControlMask) && smenu && entry->flags.editable) {
1853 editEntry(smenu, entry);
1854 goto byebye;
1855 } else if (bev->state & ControlMask) {
1856 goto byebye;
1857 }
1858
1859 if (entry->flags.enabled && entry->cascade >= 0 && menu->cascades) {
1860 WMenu *submenu = menu->cascades[entry->cascade];
1861 /* map cascade */
1862 if (submenu->flags.mapped && !submenu->flags.buttoned && menu->selected_entry != entry_no) {
1863 wMenuUnmap(submenu);
1864 }
1865 if (!submenu->flags.mapped && !delayed_select) {
1866 selectEntry(menu, entry_no);
1867 } else if (!submenu->flags.buttoned) {
1868 selectEntry(menu, -1);
1869 }
1870
1871 } else if (!delayed_select) {
1872 selectEntry(menu, entry_no);
1873 }
1874
1875 if (!wPreferences.wrap_menus && !wPreferences.scrollable_menus) {
1876 if (!menu->timer)
1877 dragScrollMenuCallback(menu);
1878 }
1879 }
9d1306bf id2001-04-16 13:33:50 +00001880#ifdef VIRTUAL_DESKTOP
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02001881 if (wPreferences.vdesk_enable) {
1882 wWorkspaceLowerEdge(scr);
1883 }
9d1306bf id2001-04-16 13:33:50 +00001884#endif
3bed15d6 dan2002-10-17 20:26:30 +00001885
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02001886 prevx = bev->x_root;
1887 prevy = bev->y_root;
1888 while (!done) {
1889 int x, y;
1890
1891 XAllowEvents(dpy, AsyncPointer | SyncPointer, CurrentTime);
1892
1893 WMMaskEvent(dpy, ExposureMask | ButtonMotionMask | ButtonReleaseMask | ButtonPressMask, &ev);
1894 switch (ev.type) {
1895 case MotionNotify:
1896 smenu = findMenu(scr, &x, &y);
1897
1898 if (smenu == NULL) {
1899 /* moved mouse out of menu */
1900
1901 if (!delayed_select && d_data.magic) {
1902 WMDeleteTimerHandler(d_data.magic);
1903 d_data.magic = NULL;
1904 }
1905 if (menu == NULL
1906 || (menu->selected_entry >= 0
1907 && menu->entries[menu->selected_entry]->cascade >= 0)) {
1908 prevx = ev.xmotion.x_root;
1909 prevy = ev.xmotion.y_root;
1910
1911 break;
1912 }
1913 selectEntry(menu, -1);
1914 menu = smenu;
1915 prevx = ev.xmotion.x_root;
1916 prevy = ev.xmotion.y_root;
1917 break;
1918 } else if (menu && menu != smenu
1919 && (menu->selected_entry < 0
1920 || menu->entries[menu->selected_entry]->cascade < 0)) {
1921 selectEntry(menu, -1);
1922
1923 if (!delayed_select && d_data.magic) {
1924 WMDeleteTimerHandler(d_data.magic);
1925 d_data.magic = NULL;
1926 }
1927 } else {
1928
1929 /* hysteresis for item selection */
1930
1931 /* check if the motion was to the side, indicating that
1932 * the user may want to cross to a submenu */
1933 if (!delayed_select && menu) {
1934 int dx;
1935 Bool moved_to_submenu; /* moved to direction of submenu */
1936
1937 dx = abs(prevx - ev.xmotion.x_root);
1938
1939 moved_to_submenu = False;
1940 if (dx > 0 /* if moved enough to the side */
1941 /* maybe a open submenu */
1942 && menu->selected_entry >= 0
1943 /* moving to the right direction */
1944 && (wPreferences.align_menus || ev.xmotion.y_root >= prevy)) {
1945 int index;
1946
1947 index = menu->entries[menu->selected_entry]->cascade;
1948 if (index >= 0) {
1949 if (menu->cascades[index]->frame_x > menu->frame_x) {
1950 if (prevx < ev.xmotion.x_root)
1951 moved_to_submenu = True;
1952 } else {
1953 if (prevx > ev.xmotion.x_root)
1954 moved_to_submenu = True;
1955 }
1956 }
1957 }
1958
1959 if (menu != smenu) {
1960 if (d_data.magic) {
1961 WMDeleteTimerHandler(d_data.magic);
1962 d_data.magic = NULL;
1963 }
1964 } else if (moved_to_submenu) {
1965 /* while we are moving, postpone the selection */
1966 if (d_data.magic) {
1967 WMDeleteTimerHandler(d_data.magic);
1968 }
1969 d_data.delayed_select = NULL;
1970 d_data.menu = menu;
1971 d_data.magic = WMAddTimerHandler(MENU_SELECT_DELAY,
1972 delaySelection, &d_data);
1973 prevx = ev.xmotion.x_root;
1974 prevy = ev.xmotion.y_root;
1975 break;
1976 } else {
1977 if (d_data.magic) {
1978 WMDeleteTimerHandler(d_data.magic);
1979 d_data.magic = NULL;
1980 }
1981 }
1982 }
1983 }
1984 prevx = ev.xmotion.x_root;
1985 prevy = ev.xmotion.y_root;
1986 if (menu != smenu) {
1987 /* pointer crossed menus */
1988 if (menu && menu->timer) {
1989 WMDeleteTimerHandler(menu->timer);
1990 menu->timer = NULL;
1991 }
1992 if (smenu)
1993 dragScrollMenuCallback(smenu);
1994 }
1995 menu = smenu;
1996 if (!menu->timer)
1997 dragScrollMenuCallback(menu);
1998
1999 if (!delayed_select) {
2000 entry_no = getEntryAt(menu, x, y);
2001 if (entry_no >= 0) {
2002 entry = menu->entries[entry_no];
2003 if (entry->flags.enabled && entry->cascade >= 0 && menu->cascades) {
2004 WMenu *submenu = menu->cascades[entry->cascade];
2005 if (submenu->flags.mapped && !submenu->flags.buttoned
2006 && menu->selected_entry != entry_no) {
2007 wMenuUnmap(submenu);
2008 }
2009 }
2010 }
2011 selectEntry(menu, entry_no);
2012 }
2013 break;
2014
2015 case ButtonPress:
2016 break;
2017
2018 case ButtonRelease:
2019 if (ev.xbutton.button == event->xbutton.button)
2020 done = 1;
2021 break;
2022
2023 case Expose:
2024 WMHandleEvent(&ev);
9d1306bf id2001-04-16 13:33:50 +00002025#ifdef VIRTUAL_DESKTOP
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02002026 /* since expose will raise edge up.. I need another ugly hack here */
2027 if (wPreferences.vdesk_enable) {
2028 wWorkspaceLowerEdge(scr);
2029 }
9d1306bf id2001-04-16 13:33:50 +00002030#endif
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02002031 break;
2032 }
2033 }
2034
2035 if (menu && menu->timer) {
2036 WMDeleteTimerHandler(menu->timer);
2037 menu->timer = NULL;
2038 }
2039 if (d_data.magic != NULL) {
2040 WMDeleteTimerHandler(d_data.magic);
2041 d_data.magic = NULL;
2042 }
2043
2044 if (menu && menu->selected_entry >= 0) {
2045 entry = menu->entries[menu->selected_entry];
2046 if (entry->callback != NULL && entry->flags.enabled && entry->cascade < 0) {
2047 /* blink and erase menu selection */
9d2e6ef9 scottc1998-09-29 22:36:29 +00002048#if (MENU_BLINK_DELAY > 0)
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02002049 int sel = menu->selected_entry;
2050 int i;
2051
2052 for (i = 0; i < MENU_BLINK_COUNT; i++) {
2053 paintEntry(menu, sel, False);
2054 XSync(dpy, 0);
2055 wusleep(MENU_BLINK_DELAY);
2056 paintEntry(menu, sel, True);
2057 XSync(dpy, 0);
2058 wusleep(MENU_BLINK_DELAY);
2059 }
9d2e6ef9 scottc1998-09-29 22:36:29 +00002060#endif
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02002061 /* unmap the menu, it's parents and call the callback */
2062 if (!menu->flags.buttoned && (!menu->flags.app_menu || menu->parent != NULL)) {
2063 closeCascade(menu);
2064 } else {
2065 selectEntry(menu, -1);
2066 }
2067 (*entry->callback) (menu, entry);
2068
2069 /* If the user double clicks an entry, the entry will
2070 * be executed twice, which is not good for things like
2071 * the root menu. So, ignore any clicks that were generated
2072 * while the entry was being executed */
2073 while (XCheckTypedWindowEvent(dpy, menu->menu->window, ButtonPress, &ev)) ;
2074 } else if (entry->callback != NULL && entry->cascade < 0) {
2075 selectEntry(menu, -1);
2076 } else {
2077 if (entry->cascade >= 0 && menu->cascades && menu->cascades[entry->cascade]->flags.brother) {
2078 selectEntry(menu, -1);
2079 }
2080 }
2081 }
2082
2083 if (((WMenu *) desc->parent)->flags.brother || close_on_exit || !smenu)
2084 closeCascade(desc->parent);
2085
2086 /* close the cascade windows that should not remain opened */
2087 closeBrotherCascadesOf(desc->parent);
2088
2089 if (!wPreferences.wrap_menus)
2090 wMenuMove(parentMenu(desc->parent), old_frame_x, old_frame_y, True);
2091
2092 byebye:
2093 /* Just to be sure in case we skip the 2 above because of a goto byebye */
2094 if (menu && menu->timer) {
2095 WMDeleteTimerHandler(menu->timer);
2096 menu->timer = NULL;
2097 }
2098 if (d_data.magic != NULL) {
2099 WMDeleteTimerHandler(d_data.magic);
2100 d_data.magic = NULL;
2101 }
2102
2103 ((WMenu *) desc->parent)->flags.inside_handler = 0;
9d1306bf id2001-04-16 13:33:50 +00002104#ifdef VIRTUAL_DESKTOP
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +02002105 wWorkspaceRaiseEdge(scr);
9d1306bf id2001-04-16 13:33:50 +00002106#endif
9d2e6ef9 scottc1998-09-29 22:36:29 +00002107}
2108
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +02002109void wMenuMove(WMenu * menu, int x, int y, int submenus)
9d2e6ef9 scottc1998-09-29 22:36:29 +00002110{
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02002111 WMenu *submenu;
2112 int i;
2113
2114 if (!menu)
2115 return;
2116
2117 menu->frame_x = x;
2118 menu->frame_y = y;
2119 XMoveWindow(dpy, menu->frame->core->window, x, y);
2120
2121 if (submenus > 0 && menu->selected_entry >= 0) {
2122 i = menu->entries[menu->selected_entry]->cascade;
2123
2124 if (i >= 0 && menu->cascades) {
2125 submenu = menu->cascades[i];
2126 if (submenu->flags.mapped && !submenu->flags.buttoned) {
2127 if (wPreferences.align_menus) {
2128 wMenuMove(submenu, x + MENUW(menu), y, submenus);
2129 } else {
2130 wMenuMove(submenu, x + MENUW(menu),
2131 y + submenu->entry_height * menu->selected_entry, submenus);
2132 }
2133 }
2134 }
2135 }
2136 if (submenus < 0 && menu->parent != NULL && menu->parent->flags.mapped && !menu->parent->flags.buttoned) {
2137 if (wPreferences.align_menus) {
2138 wMenuMove(menu->parent, x - MENUW(menu->parent), y, submenus);
2139 } else {
2140 wMenuMove(menu->parent, x - MENUW(menu->parent), menu->frame_y
2141 - menu->parent->entry_height * menu->parent->selected_entry, submenus);
2142 }
2143 }
9d2e6ef9 scottc1998-09-29 22:36:29 +00002144}
2145
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +02002146static void changeMenuLevels(WMenu * menu, int lower)
9d2e6ef9 scottc1998-09-29 22:36:29 +00002147{
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02002148 int i;
2149
2150 if (!lower) {
2151 ChangeStackingLevel(menu->frame->core, (!menu->parent ? WMMainMenuLevel : WMSubmenuLevel));
2152 wRaiseFrame(menu->frame->core);
2153 menu->flags.lowered = 0;
2154 } else {
2155 ChangeStackingLevel(menu->frame->core, WMNormalLevel);
2156 wLowerFrame(menu->frame->core);
2157 menu->flags.lowered = 1;
2158 }
2159 for (i = 0; i < menu->cascade_no; i++) {
2160 if (menu->cascades[i]
2161 && !menu->cascades[i]->flags.buttoned && menu->cascades[i]->flags.lowered != lower) {
2162 changeMenuLevels(menu->cascades[i], lower);
2163 }
2164 }
9d2e6ef9 scottc1998-09-29 22:36:29 +00002165}
2166
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +02002167static void menuTitleDoubleClick(WCoreWindow * sender, void *data, XEvent * event)
9d2e6ef9 scottc1998-09-29 22:36:29 +00002168{
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02002169 WMenu *menu = data;
2170 int lower;
2171
2172 if (event->xbutton.state & MOD_MASK) {
2173 if (menu->flags.lowered) {
2174 lower = 0;
2175 } else {
2176 lower = 1;
2177 }
2178 changeMenuLevels(menu, lower);
2179 }
9d2e6ef9 scottc1998-09-29 22:36:29 +00002180}
2181
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +02002182static void menuTitleMouseDown(WCoreWindow * sender, void *data, XEvent * event)
9d2e6ef9 scottc1998-09-29 22:36:29 +00002183{
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02002184 WMenu *menu = data;
2185 WMenu *tmp;
2186 XEvent ev;
2187 int x = menu->frame_x, y = menu->frame_y;
2188 int dx = event->xbutton.x_root, dy = event->xbutton.y_root;
2189 int i, lower;
2190 Bool started;
2191
2192 /* can't touch the menu copy */
2193 if (menu->flags.brother)
2194 return;
2195
2196 if (event->xbutton.button != Button1 && event->xbutton.button != Button2)
2197 return;
2198
2199 if (event->xbutton.state & MOD_MASK) {
2200 wLowerFrame(menu->frame->core);
2201 lower = 1;
2202 } else {
2203 wRaiseFrame(menu->frame->core);
2204 lower = 0;
2205 }
2206 tmp = menu;
2207
2208 /* lower/raise all submenus */
2209 while (1) {
2210 if (tmp->selected_entry >= 0 && tmp->cascades && tmp->entries[tmp->selected_entry]->cascade >= 0) {
2211 tmp = tmp->cascades[tmp->entries[tmp->selected_entry]->cascade];
2212 if (!tmp || !tmp->flags.mapped)
2213 break;
2214 if (lower)
2215 wLowerFrame(tmp->frame->core);
2216 else
2217 wRaiseFrame(tmp->frame->core);
2218 } else {
2219 break;
2220 }
2221 }
2222
2223 /* tear off the menu if it's a root menu or a cascade
2224 application menu */
2225 if (!menu->flags.buttoned && !menu->flags.brother && (!menu->flags.app_menu || menu->parent != NULL)) {
2226 menu->flags.buttoned = 1;
2227 wFrameWindowShowButton(menu->frame, WFF_RIGHT_BUTTON);
2228 if (menu->parent) {
2229 /* turn off selected menu entry in parent menu */
2230 selectEntry(menu->parent, -1);
2231
2232 /* make parent map the copy in place of the original */
2233 for (i = 0; i < menu->parent->cascade_no; i++) {
2234 if (menu->parent->cascades[i] == menu) {
2235 menu->parent->cascades[i] = menu->brother;
2236 break;
2237 }
2238 }
2239 }
2240 }
2241
2242 started = False;
2243 while (1) {
2244 WMMaskEvent(dpy, ButtonMotionMask | ButtonReleaseMask | ButtonPressMask | ExposureMask, &ev);
2245 switch (ev.type) {
2246 case MotionNotify:
2247 if (started) {
2248 x += ev.xmotion.x_root - dx;
2249 y += ev.xmotion.y_root - dy;
2250 dx = ev.xmotion.x_root;
2251 dy = ev.xmotion.y_root;
2252 wMenuMove(menu, x, y, True);
2253 } else {
2254 if (abs(ev.xmotion.x_root - dx) > MOVE_THRESHOLD
2255 || abs(ev.xmotion.y_root - dy) > MOVE_THRESHOLD) {
2256 started = True;
2257 XGrabPointer(dpy, menu->frame->titlebar->window, False,
2258 ButtonMotionMask | ButtonReleaseMask
2259 | ButtonPressMask,
2260 GrabModeAsync, GrabModeAsync, None,
2261 wCursor[WCUR_MOVE], CurrentTime);
2262 }
2263 }
2264 break;
2265
2266 case ButtonPress:
2267 break;
2268
2269 case ButtonRelease:
2270 if (ev.xbutton.button != event->xbutton.button)
2271 break;
2272 XUngrabPointer(dpy, CurrentTime);
2273 return;
2274
2275 default:
2276 WMHandleEvent(&ev);
2277 break;
2278 }
2279 }
9d2e6ef9 scottc1998-09-29 22:36:29 +00002280}
2281
2282/*
2283 *----------------------------------------------------------------------
2284 * menuCloseClick--
2285 * Handles mouse click on the close button of menus. The menu is
3bed15d6 dan2002-10-17 20:26:30 +00002286 * closed when the button is clicked.
2287 *
9d2e6ef9 scottc1998-09-29 22:36:29 +00002288 * Side effects:
2289 * The closed menu is reinserted at it's parent menus
2290 * cascade list.
3bed15d6 dan2002-10-17 20:26:30 +00002291 *----------------------------------------------------------------------
9d2e6ef9 scottc1998-09-29 22:36:29 +00002292 */
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +02002293static void menuCloseClick(WCoreWindow * sender, void *data, XEvent * event)
9d2e6ef9 scottc1998-09-29 22:36:29 +00002294{
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02002295 WMenu *menu = (WMenu *) data;
2296 WMenu *parent = menu->parent;
2297 int i;
2298
2299 if (parent) {
2300 for (i = 0; i < parent->cascade_no; i++) {
2301 /* find the entry that points to the copy */
2302 if (parent->cascades[i] == menu->brother) {
2303 /* make it point to the original */
2304 parent->cascades[i] = menu;
2305 menu->parent = parent;
2306 break;
2307 }
2308 }
2309 }
2310 wMenuUnmap(menu);
9d2e6ef9 scottc1998-09-29 22:36:29 +00002311}
2312
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +02002313static void saveMenuInfo(WMPropList * dict, WMenu * menu, WMPropList * key)
32160ff6 kojima2000-04-01 02:16:14 +00002314{
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02002315 WMPropList *value, *list;
2316 char buffer[256];
2317
2318 snprintf(buffer, sizeof(buffer), "%i,%i", menu->frame_x, menu->frame_y);
2319 value = WMCreatePLString(buffer);
2320 list = WMCreatePLArray(value, NULL);
2321 if (menu->flags.lowered)
2322 WMAddToPLArray(list, WMCreatePLString("lowered"));
2323 WMPutInPLDictionary(dict, key, list);
2324 WMReleasePropList(value);
2325 WMReleasePropList(list);
32160ff6 kojima2000-04-01 02:16:14 +00002326}
2327
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +02002328void wMenuSaveState(WScreen * scr)
9d2e6ef9 scottc1998-09-29 22:36:29 +00002329{
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02002330 WMPropList *menus, *key;
2331 int save_menus = 0;
c2434e89 kojima1999-05-01 17:44:43 +00002332
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +02002333 menus = WMCreatePLDictionary(NULL, NULL);
9d2e6ef9 scottc1998-09-29 22:36:29 +00002334
0261c326 dan1999-01-06 15:22:33 +00002335#ifndef LITE
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02002336 if (scr->switch_menu && scr->switch_menu->flags.buttoned) {
2337 key = WMCreatePLString("SwitchMenu");
2338 saveMenuInfo(menus, scr->switch_menu, key);
2339 WMReleasePropList(key);
2340 save_menus = 1;
2341 }
2342
2343 if (saveMenuRecurs(menus, scr, scr->root_menu))
2344 save_menus = 1;
2345
2346#endif /* !LITE */
2347 if (scr->workspace_menu && scr->workspace_menu->flags.buttoned) {
2348 key = WMCreatePLString("WorkspaceMenu");
2349 saveMenuInfo(menus, scr->workspace_menu, key);
2350 WMReleasePropList(key);
2351 save_menus = 1;
2352 }
2353
2354 if (save_menus) {
2355 key = WMCreatePLString("Menus");
2356 WMPutInPLDictionary(scr->session_state, key, menus);
2357 WMReleasePropList(key);
2358 }
2359 WMReleasePropList(menus);
9d2e6ef9 scottc1998-09-29 22:36:29 +00002360}
2361
c2434e89 kojima1999-05-01 17:44:43 +00002362#ifndef LITE
2363
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +02002364static Bool getMenuPath(WMenu * menu, char *buffer, int bufSize)
c2434e89 kojima1999-05-01 17:44:43 +00002365{
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02002366 Bool ok = True;
2367 int len = 0;
c2434e89 kojima1999-05-01 17:44:43 +00002368
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02002369 if (!menu->flags.titled || !menu->frame->title[0])
2370 return False;
c2434e89 kojima1999-05-01 17:44:43 +00002371
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02002372 len = strlen(menu->frame->title);
2373 if (len >= bufSize)
2374 return False;
c2434e89 kojima1999-05-01 17:44:43 +00002375
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02002376 if (menu->parent) {
2377 ok = getMenuPath(menu->parent, buffer, bufSize - len - 1);
2378 if (!ok)
2379 return False;
2380 }
c2434e89 kojima1999-05-01 17:44:43 +00002381
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02002382 strcat(buffer, "\\");
2383 strcat(buffer, menu->frame->title);
c2434e89 kojima1999-05-01 17:44:43 +00002384
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +02002385 return True;
c2434e89 kojima1999-05-01 17:44:43 +00002386}
2387
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +02002388static Bool saveMenuRecurs(WMPropList * menus, WScreen * scr, WMenu * menu)
c2434e89 kojima1999-05-01 17:44:43 +00002389{
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02002390 WMPropList *key;
2391 int save_menus = 0, i;
2392 char buffer[512];
2393 Bool ok = True;
2394
2395 if (menu->flags.brother)
2396 menu = menu->brother;
2397
2398 if (menu->flags.buttoned && menu != scr->switch_menu) {
2399
2400 buffer[0] = '\0';
2401 ok = getMenuPath(menu, buffer, 510);
2402
2403 if (ok) {
2404 key = WMCreatePLString(buffer);
2405 saveMenuInfo(menus, menu, key);
2406 WMReleasePropList(key);
2407 save_menus = 1;
2408 }
2409 }
2410
2411 if (ok) {
2412 for (i = 0; i < menu->cascade_no; i++) {
2413 if (saveMenuRecurs(menus, scr, menu->cascades[i]))
2414 save_menus = 1;
2415 }
2416 }
2417 return save_menus;
c2434e89 kojima1999-05-01 17:44:43 +00002418}
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +02002419#endif /* !LITE */
c2434e89 kojima1999-05-01 17:44:43 +00002420
fe381eca dan2005-03-04 23:47:09 +00002421#define COMPLAIN(key) wwarning(_("bad value in menus state info: %s"), key)
9d2e6ef9 scottc1998-09-29 22:36:29 +00002422
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +02002423static Bool getMenuInfo(WMPropList * info, int *x, int *y, Bool * lowered)
32160ff6 kojima2000-04-01 02:16:14 +00002424{
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02002425 WMPropList *pos;
2426
2427 *lowered = False;
2428
2429 if (WMIsPLArray(info)) {
2430 WMPropList *flags;
2431 pos = WMGetFromPLArray(info, 0);
2432 flags = WMGetFromPLArray(info, 1);
2433 if (flags != NULL && WMIsPLString(flags) && WMGetFromPLString(flags) != NULL
2434 && strcmp(WMGetFromPLString(flags), "lowered") == 0) {
2435 *lowered = True;
2436 }
2437 } else {
2438 pos = info;
2439 }
2440
2441 if (pos != NULL && WMIsPLString(pos)) {
2442 if (sscanf(WMGetFromPLString(pos), "%i,%i", x, y) != 2)
2443 COMPLAIN("Position");
2444 } else {
2445 COMPLAIN("(position, flags...)");
2446 return False;
2447 }
2448
2449 return True;
32160ff6 kojima2000-04-01 02:16:14 +00002450}
2451
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +02002452static int restoreMenu(WScreen * scr, WMPropList * menu, int which)
9d2e6ef9 scottc1998-09-29 22:36:29 +00002453{
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02002454 int x, y;
2455 Bool lowered = False;
2456 WMenu *pmenu = NULL;
32160ff6 kojima2000-04-01 02:16:14 +00002457
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02002458 if (!menu)
2459 return False;
9d2e6ef9 scottc1998-09-29 22:36:29 +00002460
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02002461 if (!getMenuInfo(menu, &x, &y, &lowered))
2462 return False;
9d2e6ef9 scottc1998-09-29 22:36:29 +00002463
0261c326 dan1999-01-06 15:22:33 +00002464#ifndef LITE
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02002465 if (which & WSS_SWITCHMENU) {
2466 OpenSwitchMenu(scr, x, y, False);
2467 pmenu = scr->switch_menu;
2468 }
2469#endif /* !LITE */
2470
2471 if (pmenu) {
2472 int width = MENUW(pmenu);
2473 int height = MENUH(pmenu);
2474 WMRect rect = wGetRectForHead(scr, wGetHeadForPointerLocation(scr));
2475
2476 if (lowered) {
2477 changeMenuLevels(pmenu, True);
2478 }
2479
2480 if (x < rect.pos.x - width)
2481 x = rect.pos.x;
2482 if (x > rect.pos.x + rect.size.width)
2483 x = rect.pos.x + rect.size.width - width;
2484 if (y < rect.pos.y)
2485 y = rect.pos.y;
2486 if (y > rect.pos.y + rect.size.height)
2487 y = rect.pos.y + rect.size.height - height;
2488
2489 wMenuMove(pmenu, x, y, True);
2490 pmenu->flags.buttoned = 1;
2491 wFrameWindowShowButton(pmenu->frame, WFF_RIGHT_BUTTON);
2492 return True;
2493 }
2494 return False;
9d2e6ef9 scottc1998-09-29 22:36:29 +00002495}
2496
c2434e89 kojima1999-05-01 17:44:43 +00002497#ifndef LITE
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +02002498static int restoreMenuRecurs(WScreen * scr, WMPropList * menus, WMenu * menu, char *path)
c2434e89 kojima1999-05-01 17:44:43 +00002499{
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02002500 WMPropList *key, *entry;
2501 char buffer[512];
2502 int i, x, y, res;
2503 Bool lowered;
2504
2505 if (strlen(path) + strlen(menu->frame->title) > 510)
2506 return False;
2507
2508 snprintf(buffer, sizeof(buffer), "%s\\%s", path, menu->frame->title);
2509 key = WMCreatePLString(buffer);
2510 entry = WMGetFromPLDictionary(menus, key);
2511 res = False;
2512
2513 if (entry && getMenuInfo(entry, &x, &y, &lowered)) {
2514
2515 if (!menu->flags.mapped) {
2516 int width = MENUW(menu);
2517 int height = MENUH(menu);
2518 WMRect rect = wGetRectForHead(scr, wGetHeadForPointerLocation(scr));
2519
2520 wMenuMapAt(menu, x, y, False);
2521
2522 if (menu->parent) {
2523 /* make parent map the copy in place of the original */
2524 for (i = 0; i < menu->parent->cascade_no; i++) {
2525 if (menu->parent->cascades[i] == menu) {
2526 menu->parent->cascades[i] = menu->brother;
2527 break;
2528 }
2529 }
2530 }
2531 if (lowered) {
2532 changeMenuLevels(menu, True);
2533 }
2534
2535 if (x < rect.pos.x - width)
2536 x = rect.pos.x;
2537 if (x > rect.pos.x + rect.size.width)
2538 x = rect.pos.x + rect.size.width - width;
2539 if (y < rect.pos.y)
2540 y = rect.pos.y;
2541 if (y > rect.pos.y + rect.size.height)
2542 y = rect.pos.y + rect.size.height - height;
2543
2544 wMenuMove(menu, x, y, True);
2545 menu->flags.buttoned = 1;
2546 wFrameWindowShowButton(menu->frame, WFF_RIGHT_BUTTON);
2547 res = True;
2548 }
2549 }
2550
2551 WMReleasePropList(key);
2552
2553 for (i = 0; i < menu->cascade_no; i++) {
2554 if (restoreMenuRecurs(scr, menus, menu->cascades[i], buffer) != False)
2555 res = True;
2556 }
2557
2558 return res;
c2434e89 kojima1999-05-01 17:44:43 +00002559}
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +02002560#endif /* !LITE */
c2434e89 kojima1999-05-01 17:44:43 +00002561
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +02002562void wMenuRestoreState(WScreen * scr)
9d2e6ef9 scottc1998-09-29 22:36:29 +00002563{
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +02002564 WMPropList *menus, *menu, *key, *skey;
9d2e6ef9 scottc1998-09-29 22:36:29 +00002565
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02002566 if (!scr->session_state) {
2567 return;
2568 }
df75c86e dan2003-11-13 06:10:33 +00002569
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02002570 key = WMCreatePLString("Menus");
2571 menus = WMGetFromPLDictionary(scr->session_state, key);
2572 WMReleasePropList(key);
9d2e6ef9 scottc1998-09-29 22:36:29 +00002573
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02002574 if (!menus)
2575 return;
9d2e6ef9 scottc1998-09-29 22:36:29 +00002576
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +02002577 /* restore menus */
9d2e6ef9 scottc1998-09-29 22:36:29 +00002578
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02002579 skey = WMCreatePLString("SwitchMenu");
2580 menu = WMGetFromPLDictionary(menus, skey);
2581 WMReleasePropList(skey);
2582 restoreMenu(scr, menu, WSS_SWITCHMENU);
9d2e6ef9 scottc1998-09-29 22:36:29 +00002583
c2434e89 kojima1999-05-01 17:44:43 +00002584#ifndef LITE
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02002585 if (!scr->root_menu) {
2586 OpenRootMenu(scr, scr->scr_width * 2, 0, False);
2587 wMenuUnmap(scr->root_menu);
2588 }
2589 restoreMenuRecurs(scr, menus, scr->root_menu, "");
2590#endif /* !LITE */
9d2e6ef9 scottc1998-09-29 22:36:29 +00002591}
2592
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +02002593void OpenWorkspaceMenu(WScreen * scr, int x, int y)
9d2e6ef9 scottc1998-09-29 22:36:29 +00002594{
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02002595 WMenu *menu, *parent;
2596 WMenuEntry *entry;
9d2e6ef9 scottc1998-09-29 22:36:29 +00002597
0261c326 dan1999-01-06 15:22:33 +00002598#ifndef LITE
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02002599 if (!scr->root_menu) {
2600 OpenRootMenu(scr, scr->scr_width * 2, 0, False);
2601 wMenuUnmap(scr->root_menu);
2602 }
0261c326 dan1999-01-06 15:22:33 +00002603#endif
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02002604 menu = scr->workspace_menu;
2605 if (menu) {
2606 if (menu->flags.mapped) {
2607 if (!menu->flags.buttoned) {
2608 wMenuUnmap(menu);
2609 parent = menu->parent;
2610 if (parent && parent->selected_entry >= 0) {
2611 entry = parent->entries[parent->selected_entry];
2612 if (parent->cascades[entry->cascade] == menu) {
2613 selectEntry(parent, -1);
2614 wMenuMapAt(menu, x, y, False);
2615 }
2616 }
2617 } else {
2618 wRaiseFrame(menu->frame->core);
2619 wMenuMapCopyAt(menu, x, y);
2620 }
2621 } else {
2622 wMenuMapAt(menu, x, y, False);
2623 }
2624 }
9d2e6ef9 scottc1998-09-29 22:36:29 +00002625}