wmaker: Added 'const' attribute to local variables
[wmaker-crm.git] / src / appmenu.c
blob343797b42d3b6709d33b7b0a3919b53455227493
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"
39 /******** Global Variables **********/
40 extern Atom _XA_WINDOWMAKER_MENU;
41 extern Time LastTimestamp;
42 extern WPreferences wPreferences;
44 typedef struct {
45 short code;
46 short tag;
47 Window window;
48 } WAppMenuData;
50 enum {
51 wmBeginMenu = 1,
52 wmEndMenu = 2,
53 wmNormalItem = 10,
54 wmDoubleItem = 11,
55 wmSubmenuItem = 12
58 enum {
59 wmSelectItem = 1
62 static void sendMessage(Window window, int what, int tag)
64 XEvent event;
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);
76 XFlush(dpy);
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)
88 WMenu *menu;
89 int command;
90 int code, pos;
91 char title[300];
92 char rtext[300];
94 if (strlen(slist[*index]) > sizeof(title) - 1) {
95 wwarning("appmenu: menu command size exceeded in window %lx", win);
96 return NULL;
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);
100 return NULL;
102 strcpy(title, &slist[*index][pos]);
103 menu = wMenuCreateForApp(scr, title, *index == 1);
104 if (!menu)
105 return NULL;
106 *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);
113 return NULL;
116 if (command == wmEndMenu) {
117 *index += 1;
118 break;
120 } else if (command == wmNormalItem || command == wmDoubleItem) {
121 WAppMenuData *data;
122 WMenuEntry *entry;
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",
129 slist[*index], win);
130 return NULL;
132 strcpy(title, &slist[*index][pos]);
133 rtext[0] = 0;
134 } else {
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",
139 slist[*index], win);
140 return NULL;
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);
147 return NULL;
149 data->code = code;
150 data->tag = etag;
151 data->window = win;
152 entry = wMenuAddCallback(menu, title, notifyClient, data);
153 if (!entry) {
154 wMenuDestroy(menu, True);
155 wwarning("appmenu: out of memory creating menu for window %lx", win);
156 free(data);
157 return NULL;
159 if (rtext[0] != 0)
160 entry->rtext = wstrdup(rtext);
161 else
162 entry->rtext = NULL;
163 entry->free_cdata = free;
164 *index += 1;
166 } else if (command == wmSubmenuItem) {
167 int ncode;
168 WMenuEntry *entry;
169 WMenu *submenu;
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);
176 return NULL;
178 strcpy(title, &slist[*index][pos]);
179 *index += 1;
181 submenu = parseMenuCommand(scr, win, slist, count, index);
183 entry = wMenuAddCallback(menu, title, NULL, NULL);
185 if (!entry) {
186 wMenuDestroy(menu, True);
187 wMenuDestroy(submenu, True);
188 wwarning("appmenu: out of memory creating menu for window %lx", win);
189 return NULL;
192 wMenuEntrySetCascade(menu, entry, submenu);
194 } else {
195 wMenuDestroy(menu, True);
196 wwarning("appmenu: bad menu entry \"%s\" in window %lx", slist[*index], win);
197 return NULL;
201 return menu;
204 WMenu *wAppMenuGet(WScreen * scr, Window window)
206 XTextProperty text_prop;
207 int count, i;
208 char **slist;
209 WMenu *menu;
211 if (!XGetTextProperty(dpy, window, &text_prop, _XA_WINDOWMAKER_MENU)) {
212 return NULL;
214 if (!XTextPropertyToStringList(&text_prop, &slist, &count) || count < 1) {
215 XFree(text_prop.value);
216 return NULL;
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);
222 return NULL;
225 i = 1;
226 menu = parseMenuCommand(scr, window, slist, count, &i);
227 if (menu)
228 menu->parent = NULL;
230 XFreeStringList(slist);
232 return menu;
235 void wAppMenuDestroy(WMenu * menu)
237 if (menu)
238 wMenuDestroy(menu, True);
241 static void mapmenus(WMenu * menu)
243 int i;
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)
258 if (!menu)
259 return;
261 if (!menu->flags.mapped) {
262 wMenuMap(menu);
264 if (wwin && (wPreferences.focus_mode != WKF_CLICK)) {
265 int x, min;
267 min = 20; /* Keep at least 20 pixels visible */
268 if (wwin->frame_x > min) {
269 x = wwin->frame_x - menu->frame->core->width;
270 } else {
271 x = min - menu->frame->core->width;
273 wMenuMove(menu, x, wwin->frame_y, True);
275 mapmenus(menu);
279 static void unmapmenus(WMenu * menu)
281 int i;
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)
295 if (menu)
296 unmapmenus(menu);