1 /* appmenu.c- application defined menu
3 * Window Maker window manager
5 * Copyright (c) 1997-2003 Alfredo K. Kojima
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License along
18 * with this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
25 #include <X11/Xutil.h>
26 #include <X11/Xproto.h>
27 #include <X11/Xatom.h>
33 #include "WindowMaker.h"
39 /******** Global Variables **********/
40 extern Atom _XA_WINDOWMAKER_MENU
;
41 extern Time LastTimestamp
;
42 extern WPreferences wPreferences
;
62 static void sendMessage(Window window
, int what
, int tag
)
66 event
.xclient
.type
= ClientMessage
;
67 event
.xclient
.message_type
= _XA_WINDOWMAKER_MENU
;
68 event
.xclient
.format
= 32;
69 event
.xclient
.display
= dpy
;
70 event
.xclient
.window
= window
;
71 event
.xclient
.data
.l
[0] = LastTimestamp
;
72 event
.xclient
.data
.l
[1] = what
;
73 event
.xclient
.data
.l
[2] = tag
;
74 event
.xclient
.data
.l
[3] = 0;
75 XSendEvent(dpy
, window
, False
, NoEventMask
, &event
);
79 static void notifyClient(WMenu
* menu
, WMenuEntry
* entry
)
81 WAppMenuData
*data
= entry
->clientdata
;
83 sendMessage(data
->window
, wmSelectItem
, data
->tag
);
86 static WMenu
*parseMenuCommand(WScreen
* scr
, Window win
, char **slist
, int count
, int *index
)
94 if (strlen(slist
[*index
]) > sizeof(title
) - 1) {
95 wwarning("appmenu: menu command size exceeded in window %lx", win
);
98 if (sscanf(slist
[*index
], "%i %i %n", &command
, &code
, &pos
) < 2 || command
!= wmBeginMenu
) {
99 wwarning("appmenu: bad menu entry \"%s\" in window %lx", slist
[*index
], win
);
102 strcpy(title
, &slist
[*index
][pos
]);
103 menu
= wMenuCreateForApp(scr
, title
, *index
== 1);
107 while (*index
< count
) {
108 int ecode
, etag
, enab
;
110 if (sscanf(slist
[*index
], "%i", &command
) != 1) {
111 wMenuDestroy(menu
, True
);
112 wwarning("appmenu: bad menu entry \"%s\" in window %lx", slist
[*index
], win
);
116 if (command
== wmEndMenu
) {
120 } else if (command
== wmNormalItem
|| command
== wmDoubleItem
) {
124 if (command
== wmNormalItem
) {
125 if (sscanf(slist
[*index
], "%i %i %i %i %n",
126 &command
, &ecode
, &etag
, &enab
, &pos
) != 4 || ecode
!= code
) {
127 wMenuDestroy(menu
, True
);
128 wwarning("appmenu: bad menu entry \"%s\" in window %lx",
132 strcpy(title
, &slist
[*index
][pos
]);
135 if (sscanf(slist
[*index
], "%i %i %i %i %s %n",
136 &command
, &ecode
, &etag
, &enab
, rtext
, &pos
) != 5 || ecode
!= code
) {
137 wMenuDestroy(menu
, True
);
138 wwarning("appmenu: bad menu entry \"%s\" in window %lx",
142 strcpy(title
, &slist
[*index
][pos
]);
144 if (!(data
= malloc(sizeof(WAppMenuData
)))) {
145 wwarning("appmenu: out of memory making menu for window %lx", win
);
146 wMenuDestroy(menu
, True
);
152 entry
= wMenuAddCallback(menu
, title
, notifyClient
, data
);
154 wMenuDestroy(menu
, True
);
155 wwarning("appmenu: out of memory creating menu for window %lx", win
);
160 entry
->rtext
= wstrdup(rtext
);
163 entry
->free_cdata
= free
;
166 } else if (command
== wmSubmenuItem
) {
171 if (sscanf(slist
[*index
], "%i %i %i %i %i %n",
172 &command
, &ecode
, &etag
, &enab
, &ncode
, &pos
) != 5 || ecode
!= code
) {
173 wMenuDestroy(menu
, True
);
174 wwarning("appmenu: bad menu entry \"%s\" in window %lx", slist
[*index
], win
);
178 strcpy(title
, &slist
[*index
][pos
]);
181 submenu
= parseMenuCommand(scr
, win
, slist
, count
, index
);
183 entry
= wMenuAddCallback(menu
, title
, NULL
, NULL
);
186 wMenuDestroy(menu
, True
);
187 wMenuDestroy(submenu
, True
);
188 wwarning("appmenu: out of memory creating menu for window %lx", win
);
192 wMenuEntrySetCascade(menu
, entry
, submenu
);
195 wMenuDestroy(menu
, True
);
196 wwarning("appmenu: bad menu entry \"%s\" in window %lx", slist
[*index
], win
);
204 WMenu
*wAppMenuGet(WScreen
* scr
, Window window
)
206 XTextProperty text_prop
;
211 if (!XGetTextProperty(dpy
, window
, &text_prop
, _XA_WINDOWMAKER_MENU
)) {
214 if (!XTextPropertyToStringList(&text_prop
, &slist
, &count
) || count
< 1) {
215 XFree(text_prop
.value
);
218 XFree(text_prop
.value
);
219 if (strcmp(slist
[0], "WMMenu 0") != 0) {
220 wwarning("appmenu: unknown version of WMMenu in window %lx: %s", window
, slist
[0]);
221 XFreeStringList(slist
);
226 menu
= parseMenuCommand(scr
, window
, slist
, count
, &i
);
230 XFreeStringList(slist
);
235 void wAppMenuDestroy(WMenu
* menu
)
238 wMenuDestroy(menu
, True
);
241 static void mapmenus(WMenu
* menu
)
245 if (menu
->flags
.mapped
)
246 XMapWindow(dpy
, menu
->frame
->core
->window
);
247 if (menu
->brother
->flags
.mapped
)
248 XMapWindow(dpy
, menu
->brother
->frame
->core
->window
);
249 for (i
= 0; i
< menu
->cascade_no
; i
++) {
250 if (menu
->cascades
[i
])
251 mapmenus(menu
->cascades
[i
]);
255 void wAppMenuMap(WMenu
* menu
, WWindow
* wwin
)
261 if (!menu
->flags
.mapped
) {
264 if (wwin
&& (wPreferences
.focus_mode
!= WKF_CLICK
)) {
267 min
= 20; /* Keep at least 20 pixels visible */
268 if (wwin
->frame_x
> min
) {
269 x
= wwin
->frame_x
- menu
->frame
->core
->width
;
271 x
= min
- menu
->frame
->core
->width
;
273 wMenuMove(menu
, x
, wwin
->frame_y
, True
);
279 static void unmapmenus(WMenu
* menu
)
283 if (menu
->flags
.mapped
)
284 XUnmapWindow(dpy
, menu
->frame
->core
->window
);
285 if (menu
->brother
->flags
.mapped
)
286 XUnmapWindow(dpy
, menu
->brother
->frame
->core
->window
);
287 for (i
= 0; i
< menu
->cascade_no
; i
++) {
288 if (menu
->cascades
[i
])
289 unmapmenus(menu
->cascades
[i
]);
293 void wAppMenuUnmap(WMenu
* menu
)