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"
42 /******** Global Variables **********/
43 extern Atom _XA_WINDOWMAKER_MENU
;
44 extern Atom _XA_WINDOWMAKER_WM_PROTOCOLS
;
45 extern Time LastTimestamp
;
47 extern WPreferences wPreferences
;
67 static void sendMessage(Window window
, int what
, int tag
)
71 event
.xclient
.type
= ClientMessage
;
72 event
.xclient
.message_type
= _XA_WINDOWMAKER_MENU
;
73 event
.xclient
.format
= 32;
74 event
.xclient
.display
= dpy
;
75 event
.xclient
.window
= window
;
76 event
.xclient
.data
.l
[0] = LastTimestamp
;
77 event
.xclient
.data
.l
[1] = what
;
78 event
.xclient
.data
.l
[2] = tag
;
79 event
.xclient
.data
.l
[3] = 0;
80 XSendEvent(dpy
, window
, False
, NoEventMask
, &event
);
84 static void notifyClient(WMenu
* menu
, WMenuEntry
* entry
)
86 WAppMenuData
*data
= entry
->clientdata
;
88 sendMessage(data
->window
, wmSelectItem
, data
->tag
);
91 static WMenu
*parseMenuCommand(WScreen
* scr
, Window win
, char **slist
, int count
, int *index
)
99 if (strlen(slist
[*index
]) > 300) {
100 wwarning("appmenu: menu command size exceeded in window %x", win
);
103 if (sscanf(slist
[*index
], "%i %i %n", &command
, &code
, &pos
) < 2 || command
!= wmBeginMenu
) {
104 wwarning("appmenu: bad menu entry \"%s\" in window %x", slist
[*index
], win
);
107 strcpy(title
, &slist
[*index
][pos
]);
108 menu
= wMenuCreateForApp(scr
, title
, *index
== 1);
112 while (*index
< count
) {
113 int ecode
, etag
, enab
;
115 if (sscanf(slist
[*index
], "%i", &command
) != 1) {
116 wMenuDestroy(menu
, True
);
117 wwarning("appmenu: bad menu entry \"%s\" in window %x", slist
[*index
], win
);
121 if (command
== wmEndMenu
) {
125 } else if (command
== wmNormalItem
|| command
== wmDoubleItem
) {
129 if (command
== wmNormalItem
) {
130 if (sscanf(slist
[*index
], "%i %i %i %i %n",
131 &command
, &ecode
, &etag
, &enab
, &pos
) != 4 || ecode
!= code
) {
132 wMenuDestroy(menu
, True
);
133 wwarning("appmenu: bad menu entry \"%s\" in window %x",
137 strcpy(title
, &slist
[*index
][pos
]);
140 if (sscanf(slist
[*index
], "%i %i %i %i %s %n",
141 &command
, &ecode
, &etag
, &enab
, rtext
, &pos
) != 5 || ecode
!= code
) {
142 wMenuDestroy(menu
, True
);
143 wwarning("appmenu: bad menu entry \"%s\" in window %x",
147 strcpy(title
, &slist
[*index
][pos
]);
149 if (!(data
= malloc(sizeof(WAppMenuData
)))) {
150 wwarning("appmenu: out of memory making menu for window %x", win
);
151 wMenuDestroy(menu
, True
);
157 entry
= wMenuAddCallback(menu
, title
, notifyClient
, data
);
159 wMenuDestroy(menu
, True
);
160 wwarning("appmenu: out of memory creating menu for window %x", slist
[*index
], win
);
165 entry
->rtext
= wstrdup(rtext
);
168 entry
->free_cdata
= free
;
171 } else if (command
== wmSubmenuItem
) {
176 if (sscanf(slist
[*index
], "%i %i %i %i %i %n",
177 &command
, &ecode
, &etag
, &enab
, &ncode
, &pos
) != 5 || ecode
!= code
) {
178 wMenuDestroy(menu
, True
);
179 wwarning("appmenu: bad menu entry \"%s\" in window %x", slist
[*index
], win
);
183 strcpy(title
, &slist
[*index
][pos
]);
186 submenu
= parseMenuCommand(scr
, win
, slist
, count
, index
);
188 entry
= wMenuAddCallback(menu
, title
, NULL
, NULL
);
191 wMenuDestroy(menu
, True
);
192 wMenuDestroy(submenu
, True
);
193 wwarning("appmenu: out of memory creating menu for window %x", slist
[*index
], win
);
197 wMenuEntrySetCascade(menu
, entry
, submenu
);
200 wMenuDestroy(menu
, True
);
201 wwarning("appmenu: bad menu entry \"%s\" in window %x", slist
[*index
], win
);
209 WMenu
*wAppMenuGet(WScreen
* scr
, Window window
)
211 XTextProperty text_prop
;
216 if (!XGetTextProperty(dpy
, window
, &text_prop
, _XA_WINDOWMAKER_MENU
)) {
219 if (!XTextPropertyToStringList(&text_prop
, &slist
, &count
) || count
< 1) {
220 XFree(text_prop
.value
);
223 XFree(text_prop
.value
);
224 if (strcmp(slist
[0], "WMMenu 0") != 0) {
225 wwarning("appmenu: unknown version of WMMenu in window %x: %s", window
, slist
[0]);
226 XFreeStringList(slist
);
231 menu
= parseMenuCommand(scr
, window
, slist
, count
, &i
);
235 XFreeStringList(slist
);
240 void wAppMenuDestroy(WMenu
* menu
)
243 wMenuDestroy(menu
, True
);
246 static void mapmenus(WMenu
* menu
)
250 if (menu
->flags
.mapped
)
251 XMapWindow(dpy
, menu
->frame
->core
->window
);
252 if (menu
->brother
->flags
.mapped
)
253 XMapWindow(dpy
, menu
->brother
->frame
->core
->window
);
254 for (i
= 0; i
< menu
->cascade_no
; i
++) {
255 if (menu
->cascades
[i
])
256 mapmenus(menu
->cascades
[i
]);
260 void wAppMenuMap(WMenu
* menu
, WWindow
* wwin
)
266 if (!menu
->flags
.mapped
) {
269 if (wwin
&& (wPreferences
.focus_mode
!= WKF_CLICK
)) {
272 min
= 20; /* Keep at least 20 pixels visible */
273 if (wwin
->frame_x
> min
) {
274 x
= wwin
->frame_x
- menu
->frame
->core
->width
;
276 x
= min
- menu
->frame
->core
->width
;
278 wMenuMove(menu
, x
, wwin
->frame_y
, True
);
284 static void unmapmenus(WMenu
* menu
)
288 if (menu
->flags
.mapped
)
289 XUnmapWindow(dpy
, menu
->frame
->core
->window
);
290 if (menu
->brother
->flags
.mapped
)
291 XUnmapWindow(dpy
, menu
->brother
->frame
->core
->window
);
292 for (i
= 0; i
< menu
->cascade_no
; i
++) {
293 if (menu
->cascades
[i
])
294 unmapmenus(menu
->cascades
[i
]);
298 void wAppMenuUnmap(WMenu
* menu
)