debian: Import version 0.95.6-1.
[wmaker-crm.git] / src / appmenu.c
blobbc70896416af24a8ab2e0b2c0912ce2480599a95
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.
22 #include "wconfig.h"
24 #include <X11/Xlib.h>
25 #include <X11/Xutil.h>
26 #include <X11/Xproto.h>
27 #include <X11/Xatom.h>
28 #include <stdlib.h>
29 #include <string.h>
30 #include <stdio.h>
31 #include <unistd.h>
33 #include "WindowMaker.h"
34 #include "menu.h"
35 #include "actions.h"
36 #include "appmenu.h"
37 #include "framewin.h"
40 typedef struct {
41 short code;
42 short tag;
43 Window window;
44 } WAppMenuData;
46 enum {
47 wmBeginMenu = 1,
48 wmEndMenu = 2,
49 wmNormalItem = 10,
50 wmDoubleItem = 11,
51 wmSubmenuItem = 12
54 enum {
55 wmSelectItem = 1
58 static void sendMessage(Window window, int what, int tag)
60 XEvent event;
62 event.xclient.type = ClientMessage;
63 event.xclient.message_type = w_global.atom.wmaker.menu;
64 event.xclient.format = 32;
65 event.xclient.display = dpy;
66 event.xclient.window = window;
67 event.xclient.data.l[0] = w_global.timestamp.last_event;
68 event.xclient.data.l[1] = what;
69 event.xclient.data.l[2] = tag;
70 event.xclient.data.l[3] = 0;
71 XSendEvent(dpy, window, False, NoEventMask, &event);
72 XFlush(dpy);
75 static void notifyClient(WMenu * menu, WMenuEntry * entry)
77 WAppMenuData *data = entry->clientdata;
79 /* Parameter not used, but tell the compiler that it is ok */
80 (void) menu;
82 sendMessage(data->window, wmSelectItem, data->tag);
85 static WMenu *parseMenuCommand(WScreen * scr, Window win, char **slist, int count, int *index)
87 WMenu *menu;
88 int command;
89 int code, pos;
90 char title[300];
91 char rtext[300];
93 if (sscanf(slist[*index], "%i %i %n", &command, &code, &pos) < 2 || command != wmBeginMenu) {
94 wwarning(_("appmenu: bad menu entry \"%s\" in window %lx"), slist[*index], win);
95 return NULL;
97 if (wstrlcpy(title, &slist[*index][pos], sizeof(title)) >= sizeof(title)) {
98 wwarning(_("appmenu: menu command size exceeded in window %lx"), win);
99 return NULL;
101 menu = wMenuCreateForApp(scr, title, *index == 1);
102 if (!menu)
103 return NULL;
104 *index += 1;
105 while (*index < count) {
106 int ecode, etag, enab;
108 if (sscanf(slist[*index], "%i", &command) != 1) {
109 wMenuDestroy(menu, True);
110 wwarning(_("appmenu: bad menu entry \"%s\" in window %lx"), slist[*index], win);
111 return NULL;
114 if (command == wmEndMenu) {
115 *index += 1;
116 break;
118 } else if (command == wmNormalItem || command == wmDoubleItem) {
119 WAppMenuData *data;
120 WMenuEntry *entry;
122 if (command == wmNormalItem) {
123 if (sscanf(slist[*index], "%i %i %i %i %n",
124 &command, &ecode, &etag, &enab, &pos) != 4 || ecode != code) {
125 wMenuDestroy(menu, True);
126 wwarning(_("appmenu: bad menu entry \"%s\" in window %lx"),
127 slist[*index], win);
128 return NULL;
130 wstrlcpy(title, &slist[*index][pos], sizeof(title));
131 rtext[0] = 0;
132 } else {
133 if (sscanf(slist[*index], "%i %i %i %i %s %n",
134 &command, &ecode, &etag, &enab, rtext, &pos) != 5 || ecode != code) {
135 wMenuDestroy(menu, True);
136 wwarning(_("appmenu: bad menu entry \"%s\" in window %lx"),
137 slist[*index], win);
138 return NULL;
140 wstrlcpy(title, &slist[*index][pos], sizeof(title));
142 data = malloc(sizeof(WAppMenuData));
143 if (data == NULL) {
144 wwarning(_("appmenu: out of memory creating menu for window %lx"), win);
145 wMenuDestroy(menu, True);
146 return NULL;
148 data->code = code;
149 data->tag = etag;
150 data->window = win;
151 entry = wMenuAddCallback(menu, title, notifyClient, data);
152 if (!entry) {
153 wMenuDestroy(menu, True);
154 wwarning(_("appmenu: out of memory creating menu for window %lx"), win);
155 free(data);
156 return NULL;
158 if (rtext[0] != 0)
159 entry->rtext = wstrdup(rtext);
160 else
161 entry->rtext = NULL;
162 entry->free_cdata = free;
163 *index += 1;
165 } else if (command == wmSubmenuItem) {
166 int ncode;
167 WMenuEntry *entry;
168 WMenu *submenu;
170 if (sscanf(slist[*index], "%i %i %i %i %i %n",
171 &command, &ecode, &etag, &enab, &ncode, &pos) != 5 || ecode != code) {
172 wMenuDestroy(menu, True);
173 wwarning(_("appmenu: bad menu entry \"%s\" in window %lx"), slist[*index], win);
175 return NULL;
177 wstrlcpy(title, &slist[*index][pos], sizeof(title));
178 *index += 1;
180 submenu = parseMenuCommand(scr, win, slist, count, index);
182 entry = wMenuAddCallback(menu, title, NULL, NULL);
184 if (!entry) {
185 wMenuDestroy(menu, True);
186 wMenuDestroy(submenu, True);
187 wwarning(_("appmenu: out of memory creating menu for window %lx"), win);
188 return NULL;
191 wMenuEntrySetCascade(menu, entry, submenu);
193 } else {
194 wMenuDestroy(menu, True);
195 wwarning(_("appmenu: bad menu entry \"%s\" in window %lx"), slist[*index], win);
196 return NULL;
200 return menu;
203 WMenu *wAppMenuGet(WScreen * scr, Window window)
205 XTextProperty text_prop;
206 int count, i;
207 char **slist;
208 WMenu *menu;
210 if (!XGetTextProperty(dpy, window, &text_prop, w_global.atom.wmaker.menu)) {
211 return NULL;
213 if (!XTextPropertyToStringList(&text_prop, &slist, &count) || count < 1) {
214 XFree(text_prop.value);
215 return NULL;
217 XFree(text_prop.value);
218 if (strcmp(slist[0], "WMMenu 0") != 0) {
219 wwarning(_("appmenu: unknown version of WMMenu in window %lx: %s"), window, slist[0]);
220 XFreeStringList(slist);
221 return NULL;
224 i = 1;
225 menu = parseMenuCommand(scr, window, slist, count, &i);
226 if (menu)
227 menu->parent = NULL;
229 XFreeStringList(slist);
231 return menu;
234 void wAppMenuDestroy(WMenu * menu)
236 if (menu)
237 wMenuDestroy(menu, True);
240 static void mapmenus(WMenu * menu)
242 int i;
244 if (menu->flags.mapped)
245 XMapWindow(dpy, menu->frame->core->window);
246 if (menu->brother->flags.mapped)
247 XMapWindow(dpy, menu->brother->frame->core->window);
248 for (i = 0; i < menu->cascade_no; i++) {
249 if (menu->cascades[i])
250 mapmenus(menu->cascades[i]);
254 void wAppMenuMap(WMenu * menu, WWindow * wwin)
257 if (!menu)
258 return;
260 if (!menu->flags.mapped) {
261 wMenuMap(menu);
263 if (wwin && (wPreferences.focus_mode != WKF_CLICK)) {
264 int x, min;
266 min = 20; /* Keep at least 20 pixels visible */
267 if (wwin->frame_x > min) {
268 x = wwin->frame_x - menu->frame->core->width;
269 } else {
270 x = min - menu->frame->core->width;
272 wMenuMove(menu, x, wwin->frame_y, True);
274 mapmenus(menu);
278 static void unmapmenus(WMenu * menu)
280 int i;
282 if (menu->flags.mapped)
283 XUnmapWindow(dpy, menu->frame->core->window);
284 if (menu->brother->flags.mapped)
285 XUnmapWindow(dpy, menu->brother->frame->core->window);
286 for (i = 0; i < menu->cascade_no; i++) {
287 if (menu->cascades[i])
288 unmapmenus(menu->cascades[i]);
292 void wAppMenuUnmap(WMenu * menu)
294 if (menu)
295 unmapmenus(menu);