wmaker: Removed non necessary macro for buffer size
[wmaker-crm.git] / src / appmenu.c
blob276194ae9ab763ca6e2e43bcb5ec4b91d78161e5
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 sendMessage(data->window, wmSelectItem, data->tag);
82 static WMenu *parseMenuCommand(WScreen * scr, Window win, char **slist, int count, int *index)
84 WMenu *menu;
85 int command;
86 int code, pos;
87 char title[300];
88 char rtext[300];
90 if (strlen(slist[*index]) > sizeof(title) - 1) {
91 wwarning("appmenu: menu command size exceeded in window %lx", win);
92 return NULL;
94 if (sscanf(slist[*index], "%i %i %n", &command, &code, &pos) < 2 || command != wmBeginMenu) {
95 wwarning("appmenu: bad menu entry \"%s\" in window %lx", slist[*index], win);
96 return NULL;
98 strcpy(title, &slist[*index][pos]);
99 menu = wMenuCreateForApp(scr, title, *index == 1);
100 if (!menu)
101 return NULL;
102 *index += 1;
103 while (*index < count) {
104 int ecode, etag, enab;
106 if (sscanf(slist[*index], "%i", &command) != 1) {
107 wMenuDestroy(menu, True);
108 wwarning("appmenu: bad menu entry \"%s\" in window %lx", slist[*index], win);
109 return NULL;
112 if (command == wmEndMenu) {
113 *index += 1;
114 break;
116 } else if (command == wmNormalItem || command == wmDoubleItem) {
117 WAppMenuData *data;
118 WMenuEntry *entry;
120 if (command == wmNormalItem) {
121 if (sscanf(slist[*index], "%i %i %i %i %n",
122 &command, &ecode, &etag, &enab, &pos) != 4 || ecode != code) {
123 wMenuDestroy(menu, True);
124 wwarning("appmenu: bad menu entry \"%s\" in window %lx",
125 slist[*index], win);
126 return NULL;
128 strcpy(title, &slist[*index][pos]);
129 rtext[0] = 0;
130 } else {
131 if (sscanf(slist[*index], "%i %i %i %i %s %n",
132 &command, &ecode, &etag, &enab, rtext, &pos) != 5 || ecode != code) {
133 wMenuDestroy(menu, True);
134 wwarning("appmenu: bad menu entry \"%s\" in window %lx",
135 slist[*index], win);
136 return NULL;
138 strcpy(title, &slist[*index][pos]);
140 if (!(data = malloc(sizeof(WAppMenuData)))) {
141 wwarning("appmenu: out of memory making menu for window %lx", win);
142 wMenuDestroy(menu, True);
143 return NULL;
145 data->code = code;
146 data->tag = etag;
147 data->window = win;
148 entry = wMenuAddCallback(menu, title, notifyClient, data);
149 if (!entry) {
150 wMenuDestroy(menu, True);
151 wwarning("appmenu: out of memory creating menu for window %lx", win);
152 free(data);
153 return NULL;
155 if (rtext[0] != 0)
156 entry->rtext = wstrdup(rtext);
157 else
158 entry->rtext = NULL;
159 entry->free_cdata = free;
160 *index += 1;
162 } else if (command == wmSubmenuItem) {
163 int ncode;
164 WMenuEntry *entry;
165 WMenu *submenu;
167 if (sscanf(slist[*index], "%i %i %i %i %i %n",
168 &command, &ecode, &etag, &enab, &ncode, &pos) != 5 || ecode != code) {
169 wMenuDestroy(menu, True);
170 wwarning("appmenu: bad menu entry \"%s\" in window %lx", slist[*index], win);
172 return NULL;
174 strcpy(title, &slist[*index][pos]);
175 *index += 1;
177 submenu = parseMenuCommand(scr, win, slist, count, index);
179 entry = wMenuAddCallback(menu, title, NULL, NULL);
181 if (!entry) {
182 wMenuDestroy(menu, True);
183 wMenuDestroy(submenu, True);
184 wwarning("appmenu: out of memory creating menu for window %lx", win);
185 return NULL;
188 wMenuEntrySetCascade(menu, entry, submenu);
190 } else {
191 wMenuDestroy(menu, True);
192 wwarning("appmenu: bad menu entry \"%s\" in window %lx", slist[*index], win);
193 return NULL;
197 return menu;
200 WMenu *wAppMenuGet(WScreen * scr, Window window)
202 XTextProperty text_prop;
203 int count, i;
204 char **slist;
205 WMenu *menu;
207 if (!XGetTextProperty(dpy, window, &text_prop, w_global.atom.wmaker.menu)) {
208 return NULL;
210 if (!XTextPropertyToStringList(&text_prop, &slist, &count) || count < 1) {
211 XFree(text_prop.value);
212 return NULL;
214 XFree(text_prop.value);
215 if (strcmp(slist[0], "WMMenu 0") != 0) {
216 wwarning("appmenu: unknown version of WMMenu in window %lx: %s", window, slist[0]);
217 XFreeStringList(slist);
218 return NULL;
221 i = 1;
222 menu = parseMenuCommand(scr, window, slist, count, &i);
223 if (menu)
224 menu->parent = NULL;
226 XFreeStringList(slist);
228 return menu;
231 void wAppMenuDestroy(WMenu * menu)
233 if (menu)
234 wMenuDestroy(menu, True);
237 static void mapmenus(WMenu * menu)
239 int i;
241 if (menu->flags.mapped)
242 XMapWindow(dpy, menu->frame->core->window);
243 if (menu->brother->flags.mapped)
244 XMapWindow(dpy, menu->brother->frame->core->window);
245 for (i = 0; i < menu->cascade_no; i++) {
246 if (menu->cascades[i])
247 mapmenus(menu->cascades[i]);
251 void wAppMenuMap(WMenu * menu, WWindow * wwin)
254 if (!menu)
255 return;
257 if (!menu->flags.mapped) {
258 wMenuMap(menu);
260 if (wwin && (wPreferences.focus_mode != WKF_CLICK)) {
261 int x, min;
263 min = 20; /* Keep at least 20 pixels visible */
264 if (wwin->frame_x > min) {
265 x = wwin->frame_x - menu->frame->core->width;
266 } else {
267 x = min - menu->frame->core->width;
269 wMenuMove(menu, x, wwin->frame_y, True);
271 mapmenus(menu);
275 static void unmapmenus(WMenu * menu)
277 int i;
279 if (menu->flags.mapped)
280 XUnmapWindow(dpy, menu->frame->core->window);
281 if (menu->brother->flags.mapped)
282 XUnmapWindow(dpy, menu->brother->frame->core->window);
283 for (i = 0; i < menu->cascade_no; i++) {
284 if (menu->cascades[i])
285 unmapmenus(menu->cascades[i]);
289 void wAppMenuUnmap(WMenu * menu)
291 if (menu)
292 unmapmenus(menu);