Added option to 'configure' to control debug information for compilation
[wmaker-crm.git] / src / appmenu.c
blob0d6c7f4354023901d64037a9d9b0815048782462
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 "funcs.h"
37 #include "appmenu.h"
38 #include "framewin.h"
40 /******** Global Variables **********/
41 extern Atom _XA_WINDOWMAKER_MENU;
42 extern Time LastTimestamp;
43 extern WPreferences wPreferences;
45 typedef struct {
46 short code;
47 short tag;
48 Window window;
49 } WAppMenuData;
51 enum {
52 wmBeginMenu = 1,
53 wmEndMenu = 2,
54 wmNormalItem = 10,
55 wmDoubleItem = 11,
56 wmSubmenuItem = 12
59 enum {
60 wmSelectItem = 1
63 static void sendMessage(Window window, int what, int tag)
65 XEvent event;
67 event.xclient.type = ClientMessage;
68 event.xclient.message_type = _XA_WINDOWMAKER_MENU;
69 event.xclient.format = 32;
70 event.xclient.display = dpy;
71 event.xclient.window = window;
72 event.xclient.data.l[0] = LastTimestamp;
73 event.xclient.data.l[1] = what;
74 event.xclient.data.l[2] = tag;
75 event.xclient.data.l[3] = 0;
76 XSendEvent(dpy, window, False, NoEventMask, &event);
77 XFlush(dpy);
80 static void notifyClient(WMenu * menu, WMenuEntry * entry)
82 WAppMenuData *data = entry->clientdata;
84 sendMessage(data->window, wmSelectItem, data->tag);
87 static WMenu *parseMenuCommand(WScreen * scr, Window win, char **slist, int count, int *index)
89 WMenu *menu;
90 int command;
91 int code, pos;
92 char title[300];
93 char rtext[300];
95 if (strlen(slist[*index]) > sizeof(title) - 1) {
96 wwarning("appmenu: menu command size exceeded in window %lx", win);
97 return NULL;
99 if (sscanf(slist[*index], "%i %i %n", &command, &code, &pos) < 2 || command != wmBeginMenu) {
100 wwarning("appmenu: bad menu entry \"%s\" in window %lx", slist[*index], win);
101 return NULL;
103 strcpy(title, &slist[*index][pos]);
104 menu = wMenuCreateForApp(scr, title, *index == 1);
105 if (!menu)
106 return NULL;
107 *index += 1;
108 while (*index < count) {
109 int ecode, etag, enab;
111 if (sscanf(slist[*index], "%i", &command) != 1) {
112 wMenuDestroy(menu, True);
113 wwarning("appmenu: bad menu entry \"%s\" in window %lx", slist[*index], win);
114 return NULL;
117 if (command == wmEndMenu) {
118 *index += 1;
119 break;
121 } else if (command == wmNormalItem || command == wmDoubleItem) {
122 WAppMenuData *data;
123 WMenuEntry *entry;
125 if (command == wmNormalItem) {
126 if (sscanf(slist[*index], "%i %i %i %i %n",
127 &command, &ecode, &etag, &enab, &pos) != 4 || ecode != code) {
128 wMenuDestroy(menu, True);
129 wwarning("appmenu: bad menu entry \"%s\" in window %lx",
130 slist[*index], win);
131 return NULL;
133 strcpy(title, &slist[*index][pos]);
134 rtext[0] = 0;
135 } else {
136 if (sscanf(slist[*index], "%i %i %i %i %s %n",
137 &command, &ecode, &etag, &enab, rtext, &pos) != 5 || ecode != code) {
138 wMenuDestroy(menu, True);
139 wwarning("appmenu: bad menu entry \"%s\" in window %lx",
140 slist[*index], win);
141 return NULL;
143 strcpy(title, &slist[*index][pos]);
145 if (!(data = malloc(sizeof(WAppMenuData)))) {
146 wwarning("appmenu: out of memory making menu for window %lx", win);
147 wMenuDestroy(menu, True);
148 return NULL;
150 data->code = code;
151 data->tag = etag;
152 data->window = win;
153 entry = wMenuAddCallback(menu, title, notifyClient, data);
154 if (!entry) {
155 wMenuDestroy(menu, True);
156 wwarning("appmenu: out of memory creating menu for window %lx", win);
157 free(data);
158 return NULL;
160 if (rtext[0] != 0)
161 entry->rtext = wstrdup(rtext);
162 else
163 entry->rtext = NULL;
164 entry->free_cdata = free;
165 *index += 1;
167 } else if (command == wmSubmenuItem) {
168 int ncode;
169 WMenuEntry *entry;
170 WMenu *submenu;
172 if (sscanf(slist[*index], "%i %i %i %i %i %n",
173 &command, &ecode, &etag, &enab, &ncode, &pos) != 5 || ecode != code) {
174 wMenuDestroy(menu, True);
175 wwarning("appmenu: bad menu entry \"%s\" in window %lx", slist[*index], win);
177 return NULL;
179 strcpy(title, &slist[*index][pos]);
180 *index += 1;
182 submenu = parseMenuCommand(scr, win, slist, count, index);
184 entry = wMenuAddCallback(menu, title, NULL, NULL);
186 if (!entry) {
187 wMenuDestroy(menu, True);
188 wMenuDestroy(submenu, True);
189 wwarning("appmenu: out of memory creating menu for window %lx", win);
190 return NULL;
193 wMenuEntrySetCascade(menu, entry, submenu);
195 } else {
196 wMenuDestroy(menu, True);
197 wwarning("appmenu: bad menu entry \"%s\" in window %lx", slist[*index], win);
198 return NULL;
202 return menu;
205 WMenu *wAppMenuGet(WScreen * scr, Window window)
207 XTextProperty text_prop;
208 int count, i;
209 char **slist;
210 WMenu *menu;
212 if (!XGetTextProperty(dpy, window, &text_prop, _XA_WINDOWMAKER_MENU)) {
213 return NULL;
215 if (!XTextPropertyToStringList(&text_prop, &slist, &count) || count < 1) {
216 XFree(text_prop.value);
217 return NULL;
219 XFree(text_prop.value);
220 if (strcmp(slist[0], "WMMenu 0") != 0) {
221 wwarning("appmenu: unknown version of WMMenu in window %lx: %s", window, slist[0]);
222 XFreeStringList(slist);
223 return NULL;
226 i = 1;
227 menu = parseMenuCommand(scr, window, slist, count, &i);
228 if (menu)
229 menu->parent = NULL;
231 XFreeStringList(slist);
233 return menu;
236 void wAppMenuDestroy(WMenu * menu)
238 if (menu)
239 wMenuDestroy(menu, True);
242 static void mapmenus(WMenu * menu)
244 int i;
246 if (menu->flags.mapped)
247 XMapWindow(dpy, menu->frame->core->window);
248 if (menu->brother->flags.mapped)
249 XMapWindow(dpy, menu->brother->frame->core->window);
250 for (i = 0; i < menu->cascade_no; i++) {
251 if (menu->cascades[i])
252 mapmenus(menu->cascades[i]);
256 void wAppMenuMap(WMenu * menu, WWindow * wwin)
259 if (!menu)
260 return;
262 if (!menu->flags.mapped) {
263 wMenuMap(menu);
265 if (wwin && (wPreferences.focus_mode != WKF_CLICK)) {
266 int x, min;
268 min = 20; /* Keep at least 20 pixels visible */
269 if (wwin->frame_x > min) {
270 x = wwin->frame_x - menu->frame->core->width;
271 } else {
272 x = min - menu->frame->core->width;
274 wMenuMove(menu, x, wwin->frame_y, True);
276 mapmenus(menu);
280 static void unmapmenus(WMenu * menu)
282 int i;
284 if (menu->flags.mapped)
285 XUnmapWindow(dpy, menu->frame->core->window);
286 if (menu->brother->flags.mapped)
287 XUnmapWindow(dpy, menu->brother->frame->core->window);
288 for (i = 0; i < menu->cascade_no; i++) {
289 if (menu->cascades[i])
290 unmapmenus(menu->cascades[i]);
294 void wAppMenuUnmap(WMenu * menu)
296 if (menu)
297 unmapmenus(menu);