wmaker: removed unused constant SCROLL_STEPS in the switchpanel code
[wmaker-crm.git] / src / appmenu.c
blob342cef3c95db93f6b0a4b940e679f559b27cb167
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 if (!(data = malloc(sizeof(WAppMenuData)))) {
143 wwarning("appmenu: out of memory making menu for window %lx", win);
144 wMenuDestroy(menu, True);
145 return NULL;
147 data->code = code;
148 data->tag = etag;
149 data->window = win;
150 entry = wMenuAddCallback(menu, title, notifyClient, data);
151 if (!entry) {
152 wMenuDestroy(menu, True);
153 wwarning("appmenu: out of memory creating menu for window %lx", win);
154 free(data);
155 return NULL;
157 if (rtext[0] != 0)
158 entry->rtext = wstrdup(rtext);
159 else
160 entry->rtext = NULL;
161 entry->free_cdata = free;
162 *index += 1;
164 } else if (command == wmSubmenuItem) {
165 int ncode;
166 WMenuEntry *entry;
167 WMenu *submenu;
169 if (sscanf(slist[*index], "%i %i %i %i %i %n",
170 &command, &ecode, &etag, &enab, &ncode, &pos) != 5 || ecode != code) {
171 wMenuDestroy(menu, True);
172 wwarning("appmenu: bad menu entry \"%s\" in window %lx", slist[*index], win);
174 return NULL;
176 wstrlcpy(title, &slist[*index][pos], sizeof(title));
177 *index += 1;
179 submenu = parseMenuCommand(scr, win, slist, count, index);
181 entry = wMenuAddCallback(menu, title, NULL, NULL);
183 if (!entry) {
184 wMenuDestroy(menu, True);
185 wMenuDestroy(submenu, True);
186 wwarning("appmenu: out of memory creating menu for window %lx", win);
187 return NULL;
190 wMenuEntrySetCascade(menu, entry, submenu);
192 } else {
193 wMenuDestroy(menu, True);
194 wwarning("appmenu: bad menu entry \"%s\" in window %lx", slist[*index], win);
195 return NULL;
199 return menu;
202 WMenu *wAppMenuGet(WScreen * scr, Window window)
204 XTextProperty text_prop;
205 int count, i;
206 char **slist;
207 WMenu *menu;
209 if (!XGetTextProperty(dpy, window, &text_prop, w_global.atom.wmaker.menu)) {
210 return NULL;
212 if (!XTextPropertyToStringList(&text_prop, &slist, &count) || count < 1) {
213 XFree(text_prop.value);
214 return NULL;
216 XFree(text_prop.value);
217 if (strcmp(slist[0], "WMMenu 0") != 0) {
218 wwarning("appmenu: unknown version of WMMenu in window %lx: %s", window, slist[0]);
219 XFreeStringList(slist);
220 return NULL;
223 i = 1;
224 menu = parseMenuCommand(scr, window, slist, count, &i);
225 if (menu)
226 menu->parent = NULL;
228 XFreeStringList(slist);
230 return menu;
233 void wAppMenuDestroy(WMenu * menu)
235 if (menu)
236 wMenuDestroy(menu, True);
239 static void mapmenus(WMenu * menu)
241 int i;
243 if (menu->flags.mapped)
244 XMapWindow(dpy, menu->frame->core->window);
245 if (menu->brother->flags.mapped)
246 XMapWindow(dpy, menu->brother->frame->core->window);
247 for (i = 0; i < menu->cascade_no; i++) {
248 if (menu->cascades[i])
249 mapmenus(menu->cascades[i]);
253 void wAppMenuMap(WMenu * menu, WWindow * wwin)
256 if (!menu)
257 return;
259 if (!menu->flags.mapped) {
260 wMenuMap(menu);
262 if (wwin && (wPreferences.focus_mode != WKF_CLICK)) {
263 int x, min;
265 min = 20; /* Keep at least 20 pixels visible */
266 if (wwin->frame_x > min) {
267 x = wwin->frame_x - menu->frame->core->width;
268 } else {
269 x = min - menu->frame->core->width;
271 wMenuMove(menu, x, wwin->frame_y, True);
273 mapmenus(menu);
277 static void unmapmenus(WMenu * menu)
279 int i;
281 if (menu->flags.mapped)
282 XUnmapWindow(dpy, menu->frame->core->window);
283 if (menu->brother->flags.mapped)
284 XUnmapWindow(dpy, menu->brother->frame->core->window);
285 for (i = 0; i < menu->cascade_no; i++) {
286 if (menu->cascades[i])
287 unmapmenus(menu->cascades[i]);
291 void wAppMenuUnmap(WMenu * menu)
293 if (menu)
294 unmapmenus(menu);