Change to the linux kernel coding style
[wmaker-crm.git] / src / appmenu.c
1 /* appmenu.c- application defined menu
2 *
3 * Window Maker window manager
4 *
5 * Copyright (c) 1997-2003 Alfredo K. Kojima
6 *
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.
11 *
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.
16 *
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,
20 * USA.
21 */
22
23 #include "wconfig.h"
24
25 #include <X11/Xlib.h>
26 #include <X11/Xutil.h>
27 #include <X11/Xproto.h>
28 #include <X11/Xatom.h>
29 #include <stdlib.h>
30 #include <string.h>
31 #include <stdio.h>
32 #include <unistd.h>
33
34 #include "WindowMaker.h"
35 #include "wcore.h"
36 #include "menu.h"
37 #include "actions.h"
38 #include "funcs.h"
39
40 #include "framewin.h"
41
42 /******** Global Variables **********/
43 extern Atom _XA_WINDOWMAKER_MENU;
44 extern Atom _XA_WINDOWMAKER_WM_PROTOCOLS;
45 extern Time LastTimestamp;
46
47 extern WPreferences wPreferences;
48
49 typedef struct {
50 short code;
51 short tag;
52 Window window;
53 } WAppMenuData;
54
55 enum {
56 wmBeginMenu = 1,
57 wmEndMenu = 2,
58 wmNormalItem = 10,
59 wmDoubleItem = 11,
60 wmSubmenuItem = 12
61 };
62
63 enum {
64 wmSelectItem = 1
65 };
66
67 static void sendMessage(Window window, int what, int tag)
68 {
69 XEvent event;
70
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);
81 XFlush(dpy);
82 }
83
84 static void notifyClient(WMenu * menu, WMenuEntry * entry)
85 {
86 WAppMenuData *data = entry->clientdata;
87
88 sendMessage(data->window, wmSelectItem, data->tag);
89 }
90
91 static WMenu *parseMenuCommand(WScreen * scr, Window win, char **slist, int count, int *index)
92 {
93 WMenu *menu;
94 int command;
95 int code, pos;
96 char title[300];
97 char rtext[300];
98
99 if (strlen(slist[*index]) > 300) {
100 wwarning("appmenu: menu command size exceeded in window %x", win);
101 return NULL;
102 }
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);
105 return NULL;
106 }
107 strcpy(title, &slist[*index][pos]);
108 menu = wMenuCreateForApp(scr, title, *index == 1);
109 if (!menu)
110 return NULL;
111 *index += 1;
112 while (*index < count) {
113 int ecode, etag, enab;
114
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);
118 return NULL;
119 }
120
121 if (command == wmEndMenu) {
122 *index += 1;
123 break;
124
125 } else if (command == wmNormalItem || command == wmDoubleItem) {
126 WAppMenuData *data;
127 WMenuEntry *entry;
128
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",
134 slist[*index], win);
135 return NULL;
136 }
137 strcpy(title, &slist[*index][pos]);
138 rtext[0] = 0;
139 } else {
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",
144 slist[*index], win);
145 return NULL;
146 }
147 strcpy(title, &slist[*index][pos]);
148 }
149 if (!(data = malloc(sizeof(WAppMenuData)))) {
150 wwarning("appmenu: out of memory making menu for window %x", win);
151 wMenuDestroy(menu, True);
152 return NULL;
153 }
154 data->code = code;
155 data->tag = etag;
156 data->window = win;
157 entry = wMenuAddCallback(menu, title, notifyClient, data);
158 if (!entry) {
159 wMenuDestroy(menu, True);
160 wwarning("appmenu: out of memory creating menu for window %x", slist[*index], win);
161 wfree(data);
162 return NULL;
163 }
164 if (rtext[0] != 0)
165 entry->rtext = wstrdup(rtext);
166 else
167 entry->rtext = NULL;
168 entry->free_cdata = free;
169 *index += 1;
170
171 } else if (command == wmSubmenuItem) {
172 int ncode;
173 WMenuEntry *entry;
174 WMenu *submenu;
175
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);
180
181 return NULL;
182 }
183 strcpy(title, &slist[*index][pos]);
184 *index += 1;
185
186 submenu = parseMenuCommand(scr, win, slist, count, index);
187
188 entry = wMenuAddCallback(menu, title, NULL, NULL);
189
190 if (!entry) {
191 wMenuDestroy(menu, True);
192 wMenuDestroy(submenu, True);
193 wwarning("appmenu: out of memory creating menu for window %x", slist[*index], win);
194 return NULL;
195 }
196
197 wMenuEntrySetCascade(menu, entry, submenu);
198
199 } else {
200 wMenuDestroy(menu, True);
201 wwarning("appmenu: bad menu entry \"%s\" in window %x", slist[*index], win);
202 return NULL;
203 }
204 }
205
206 return menu;
207 }
208
209 WMenu *wAppMenuGet(WScreen * scr, Window window)
210 {
211 XTextProperty text_prop;
212 int count, i;
213 char **slist;
214 WMenu *menu;
215
216 if (!XGetTextProperty(dpy, window, &text_prop, _XA_WINDOWMAKER_MENU)) {
217 return NULL;
218 }
219 if (!XTextPropertyToStringList(&text_prop, &slist, &count) || count < 1) {
220 XFree(text_prop.value);
221 return NULL;
222 }
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);
227 return NULL;
228 }
229
230 i = 1;
231 menu = parseMenuCommand(scr, window, slist, count, &i);
232 if (menu)
233 menu->parent = NULL;
234
235 XFreeStringList(slist);
236
237 return menu;
238 }
239
240 void wAppMenuDestroy(WMenu * menu)
241 {
242 if (menu)
243 wMenuDestroy(menu, True);
244 }
245
246 static void mapmenus(WMenu * menu)
247 {
248 int i;
249
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]);
257 }
258 }
259
260 void wAppMenuMap(WMenu * menu, WWindow * wwin)
261 {
262
263 if (!menu)
264 return;
265
266 if (!menu->flags.mapped) {
267 wMenuMap(menu);
268 }
269 if (wwin && (wPreferences.focus_mode != WKF_CLICK)) {
270 int x, min;
271
272 min = 20; /* Keep at least 20 pixels visible */
273 if (wwin->frame_x > min) {
274 x = wwin->frame_x - menu->frame->core->width;
275 } else {
276 x = min - menu->frame->core->width;
277 }
278 wMenuMove(menu, x, wwin->frame_y, True);
279 }
280 mapmenus(menu);
281
282 }
283
284 static void unmapmenus(WMenu * menu)
285 {
286 int i;
287
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]);
295 }
296 }
297
298 void wAppMenuUnmap(WMenu * menu)
299 {
300 if (menu)
301 unmapmenus(menu);
302 }