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
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
26 #include <X11/Xutil.h>
27 #include <X11/Xproto.h>
28 #include <X11/Xatom.h>
34 #include "WindowMaker.h"
41 /******** Global Variables **********/
42 extern Atom _XA_WINDOWMAKER_MENU
;
43 extern Time LastTimestamp
;
44 extern WPreferences wPreferences
;
64 static void sendMessage(Window window
, int what
, int tag
)
68 event
.xclient
.type
= ClientMessage
;
69 event
.xclient
.message_type
= _XA_WINDOWMAKER_MENU
;
70 event
.xclient
.format
= 32;
71 event
.xclient
.display
= dpy
;
72 event
.xclient
.window
= window
;
73 event
.xclient
.data
.l
[0] = LastTimestamp
;
74 event
.xclient
.data
.l
[1] = what
;
75 event
.xclient
.data
.l
[2] = tag
;
76 event
.xclient
.data
.l
[3] = 0;
77 XSendEvent(dpy
, window
, False
, NoEventMask
, &event
);
81 static void notifyClient(WMenu
* menu
, WMenuEntry
* entry
)
83 WAppMenuData
*data
= entry
->clientdata
;
85 sendMessage(data
->window
, wmSelectItem
, data
->tag
);
88 static WMenu
*parseMenuCommand(WScreen
* scr
, Window win
, char **slist
, int count
, int *index
)
96 if (strlen(slist
[*index
]) > 300) {
97 wwarning("appmenu: menu command size exceeded in window %lx", win
);
100 if (sscanf(slist
[*index
], "%i %i %n", &command
, &code
, &pos
) < 2 || command
!= wmBeginMenu
) {
101 wwarning("appmenu: bad menu entry \"%s\" in window %lx", slist
[*index
], win
);
104 strcpy(title
, &slist
[*index
][pos
]);
105 menu
= wMenuCreateForApp(scr
, title
, *index
== 1);
109 while (*index
< count
) {
110 int ecode
, etag
, enab
;
112 if (sscanf(slist
[*index
], "%i", &command
) != 1) {
113 wMenuDestroy(menu
, True
);
114 wwarning("appmenu: bad menu entry \"%s\" in window %lx", slist
[*index
], win
);
118 if (command
== wmEndMenu
) {
122 } else if (command
== wmNormalItem
|| command
== wmDoubleItem
) {
126 if (command
== wmNormalItem
) {
127 if (sscanf(slist
[*index
], "%i %i %i %i %n",
128 &command
, &ecode
, &etag
, &enab
, &pos
) != 4 || ecode
!= code
) {
129 wMenuDestroy(menu
, True
);
130 wwarning("appmenu: bad menu entry \"%s\" in window %lx",
134 strcpy(title
, &slist
[*index
][pos
]);
137 if (sscanf(slist
[*index
], "%i %i %i %i %s %n",
138 &command
, &ecode
, &etag
, &enab
, rtext
, &pos
) != 5 || ecode
!= code
) {
139 wMenuDestroy(menu
, True
);
140 wwarning("appmenu: bad menu entry \"%s\" in window %lx",
144 strcpy(title
, &slist
[*index
][pos
]);
146 if (!(data
= malloc(sizeof(WAppMenuData
)))) {
147 wwarning("appmenu: out of memory making menu for window %lx", win
);
148 wMenuDestroy(menu
, True
);
154 entry
= wMenuAddCallback(menu
, title
, notifyClient
, data
);
156 wMenuDestroy(menu
, True
);
157 wwarning("appmenu: out of memory creating menu for window %lx", win
);
162 entry
->rtext
= wstrdup(rtext
);
165 entry
->free_cdata
= free
;
168 } else if (command
== wmSubmenuItem
) {
173 if (sscanf(slist
[*index
], "%i %i %i %i %i %n",
174 &command
, &ecode
, &etag
, &enab
, &ncode
, &pos
) != 5 || ecode
!= code
) {
175 wMenuDestroy(menu
, True
);
176 wwarning("appmenu: bad menu entry \"%s\" in window %lx", slist
[*index
], win
);
180 strcpy(title
, &slist
[*index
][pos
]);
183 submenu
= parseMenuCommand(scr
, win
, slist
, count
, index
);
185 entry
= wMenuAddCallback(menu
, title
, NULL
, NULL
);
188 wMenuDestroy(menu
, True
);
189 wMenuDestroy(submenu
, True
);
190 wwarning("appmenu: out of memory creating menu for window %lx", win
);
194 wMenuEntrySetCascade(menu
, entry
, submenu
);
197 wMenuDestroy(menu
, True
);
198 wwarning("appmenu: bad menu entry \"%s\" in window %lx", slist
[*index
], win
);
206 WMenu
*wAppMenuGet(WScreen
* scr
, Window window
)
208 XTextProperty text_prop
;
213 if (!XGetTextProperty(dpy
, window
, &text_prop
, _XA_WINDOWMAKER_MENU
)) {
216 if (!XTextPropertyToStringList(&text_prop
, &slist
, &count
) || count
< 1) {
217 XFree(text_prop
.value
);
220 XFree(text_prop
.value
);
221 if (strcmp(slist
[0], "WMMenu 0") != 0) {
222 wwarning("appmenu: unknown version of WMMenu in window %lx: %s", window
, slist
[0]);
223 XFreeStringList(slist
);
228 menu
= parseMenuCommand(scr
, window
, slist
, count
, &i
);
232 XFreeStringList(slist
);
237 void wAppMenuDestroy(WMenu
* menu
)
240 wMenuDestroy(menu
, True
);
243 static void mapmenus(WMenu
* menu
)
247 if (menu
->flags
.mapped
)
248 XMapWindow(dpy
, menu
->frame
->core
->window
);
249 if (menu
->brother
->flags
.mapped
)
250 XMapWindow(dpy
, menu
->brother
->frame
->core
->window
);
251 for (i
= 0; i
< menu
->cascade_no
; i
++) {
252 if (menu
->cascades
[i
])
253 mapmenus(menu
->cascades
[i
]);
257 void wAppMenuMap(WMenu
* menu
, WWindow
* wwin
)
263 if (!menu
->flags
.mapped
) {
266 if (wwin
&& (wPreferences
.focus_mode
!= WKF_CLICK
)) {
269 min
= 20; /* Keep at least 20 pixels visible */
270 if (wwin
->frame_x
> min
) {
271 x
= wwin
->frame_x
- menu
->frame
->core
->width
;
273 x
= min
- menu
->frame
->core
->width
;
275 wMenuMove(menu
, x
, wwin
->frame_y
, True
);
281 static void unmapmenus(WMenu
* menu
)
285 if (menu
->flags
.mapped
)
286 XUnmapWindow(dpy
, menu
->frame
->core
->window
);
287 if (menu
->brother
->flags
.mapped
)
288 XUnmapWindow(dpy
, menu
->brother
->frame
->core
->window
);
289 for (i
= 0; i
< menu
->cascade_no
; i
++) {
290 if (menu
->cascades
[i
])
291 unmapmenus(menu
->cascades
[i
]);
295 void wAppMenuUnmap(WMenu
* menu
)