Cleanup includes of wcore.h, defaults.h and pixmap.h
[wmaker-crm.git] / src / appmenu.c
blob0e182c62e9280af71e703e7e59e4ce107f2fb44c
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
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.
23 #include "wconfig.h"
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>
34 #include "WindowMaker.h"
35 #include "menu.h"
36 #include "actions.h"
37 #include "funcs.h"
39 #include "framewin.h"
41 /******** Global Variables **********/
42 extern Atom _XA_WINDOWMAKER_MENU;
43 extern Atom _XA_WINDOWMAKER_WM_PROTOCOLS;
44 extern Time LastTimestamp;
46 extern WPreferences wPreferences;
48 typedef struct {
49 short code;
50 short tag;
51 Window window;
52 } WAppMenuData;
54 enum {
55 wmBeginMenu = 1,
56 wmEndMenu = 2,
57 wmNormalItem = 10,
58 wmDoubleItem = 11,
59 wmSubmenuItem = 12
62 enum {
63 wmSelectItem = 1
66 static void sendMessage(Window window, int what, int tag)
68 XEvent event;
70 event.xclient.type = ClientMessage;
71 event.xclient.message_type = _XA_WINDOWMAKER_MENU;
72 event.xclient.format = 32;
73 event.xclient.display = dpy;
74 event.xclient.window = window;
75 event.xclient.data.l[0] = LastTimestamp;
76 event.xclient.data.l[1] = what;
77 event.xclient.data.l[2] = tag;
78 event.xclient.data.l[3] = 0;
79 XSendEvent(dpy, window, False, NoEventMask, &event);
80 XFlush(dpy);
83 static void notifyClient(WMenu * menu, WMenuEntry * entry)
85 WAppMenuData *data = entry->clientdata;
87 sendMessage(data->window, wmSelectItem, data->tag);
90 static WMenu *parseMenuCommand(WScreen * scr, Window win, char **slist, int count, int *index)
92 WMenu *menu;
93 int command;
94 int code, pos;
95 char title[300];
96 char rtext[300];
98 if (strlen(slist[*index]) > 300) {
99 wwarning("appmenu: menu command size exceeded in window %x", win);
100 return NULL;
102 if (sscanf(slist[*index], "%i %i %n", &command, &code, &pos) < 2 || command != wmBeginMenu) {
103 wwarning("appmenu: bad menu entry \"%s\" in window %x", slist[*index], win);
104 return NULL;
106 strcpy(title, &slist[*index][pos]);
107 menu = wMenuCreateForApp(scr, title, *index == 1);
108 if (!menu)
109 return NULL;
110 *index += 1;
111 while (*index < count) {
112 int ecode, etag, enab;
114 if (sscanf(slist[*index], "%i", &command) != 1) {
115 wMenuDestroy(menu, True);
116 wwarning("appmenu: bad menu entry \"%s\" in window %x", slist[*index], win);
117 return NULL;
120 if (command == wmEndMenu) {
121 *index += 1;
122 break;
124 } else if (command == wmNormalItem || command == wmDoubleItem) {
125 WAppMenuData *data;
126 WMenuEntry *entry;
128 if (command == wmNormalItem) {
129 if (sscanf(slist[*index], "%i %i %i %i %n",
130 &command, &ecode, &etag, &enab, &pos) != 4 || ecode != code) {
131 wMenuDestroy(menu, True);
132 wwarning("appmenu: bad menu entry \"%s\" in window %x",
133 slist[*index], win);
134 return NULL;
136 strcpy(title, &slist[*index][pos]);
137 rtext[0] = 0;
138 } else {
139 if (sscanf(slist[*index], "%i %i %i %i %s %n",
140 &command, &ecode, &etag, &enab, rtext, &pos) != 5 || ecode != code) {
141 wMenuDestroy(menu, True);
142 wwarning("appmenu: bad menu entry \"%s\" in window %x",
143 slist[*index], win);
144 return NULL;
146 strcpy(title, &slist[*index][pos]);
148 if (!(data = malloc(sizeof(WAppMenuData)))) {
149 wwarning("appmenu: out of memory making menu for window %x", win);
150 wMenuDestroy(menu, True);
151 return NULL;
153 data->code = code;
154 data->tag = etag;
155 data->window = win;
156 entry = wMenuAddCallback(menu, title, notifyClient, data);
157 if (!entry) {
158 wMenuDestroy(menu, True);
159 wwarning("appmenu: out of memory creating menu for window %x", slist[*index], win);
160 wfree(data);
161 return NULL;
163 if (rtext[0] != 0)
164 entry->rtext = wstrdup(rtext);
165 else
166 entry->rtext = NULL;
167 entry->free_cdata = free;
168 *index += 1;
170 } else if (command == wmSubmenuItem) {
171 int ncode;
172 WMenuEntry *entry;
173 WMenu *submenu;
175 if (sscanf(slist[*index], "%i %i %i %i %i %n",
176 &command, &ecode, &etag, &enab, &ncode, &pos) != 5 || ecode != code) {
177 wMenuDestroy(menu, True);
178 wwarning("appmenu: bad menu entry \"%s\" in window %x", slist[*index], win);
180 return NULL;
182 strcpy(title, &slist[*index][pos]);
183 *index += 1;
185 submenu = parseMenuCommand(scr, win, slist, count, index);
187 entry = wMenuAddCallback(menu, title, NULL, NULL);
189 if (!entry) {
190 wMenuDestroy(menu, True);
191 wMenuDestroy(submenu, True);
192 wwarning("appmenu: out of memory creating menu for window %x", slist[*index], win);
193 return NULL;
196 wMenuEntrySetCascade(menu, entry, submenu);
198 } else {
199 wMenuDestroy(menu, True);
200 wwarning("appmenu: bad menu entry \"%s\" in window %x", slist[*index], win);
201 return NULL;
205 return menu;
208 WMenu *wAppMenuGet(WScreen * scr, Window window)
210 XTextProperty text_prop;
211 int count, i;
212 char **slist;
213 WMenu *menu;
215 if (!XGetTextProperty(dpy, window, &text_prop, _XA_WINDOWMAKER_MENU)) {
216 return NULL;
218 if (!XTextPropertyToStringList(&text_prop, &slist, &count) || count < 1) {
219 XFree(text_prop.value);
220 return NULL;
222 XFree(text_prop.value);
223 if (strcmp(slist[0], "WMMenu 0") != 0) {
224 wwarning("appmenu: unknown version of WMMenu in window %x: %s", window, slist[0]);
225 XFreeStringList(slist);
226 return NULL;
229 i = 1;
230 menu = parseMenuCommand(scr, window, slist, count, &i);
231 if (menu)
232 menu->parent = NULL;
234 XFreeStringList(slist);
236 return menu;
239 void wAppMenuDestroy(WMenu * menu)
241 if (menu)
242 wMenuDestroy(menu, True);
245 static void mapmenus(WMenu * menu)
247 int i;
249 if (menu->flags.mapped)
250 XMapWindow(dpy, menu->frame->core->window);
251 if (menu->brother->flags.mapped)
252 XMapWindow(dpy, menu->brother->frame->core->window);
253 for (i = 0; i < menu->cascade_no; i++) {
254 if (menu->cascades[i])
255 mapmenus(menu->cascades[i]);
259 void wAppMenuMap(WMenu * menu, WWindow * wwin)
262 if (!menu)
263 return;
265 if (!menu->flags.mapped) {
266 wMenuMap(menu);
268 if (wwin && (wPreferences.focus_mode != WKF_CLICK)) {
269 int x, min;
271 min = 20; /* Keep at least 20 pixels visible */
272 if (wwin->frame_x > min) {
273 x = wwin->frame_x - menu->frame->core->width;
274 } else {
275 x = min - menu->frame->core->width;
277 wMenuMove(menu, x, wwin->frame_y, True);
279 mapmenus(menu);
283 static void unmapmenus(WMenu * menu)
285 int i;
287 if (menu->flags.mapped)
288 XUnmapWindow(dpy, menu->frame->core->window);
289 if (menu->brother->flags.mapped)
290 XUnmapWindow(dpy, menu->brother->frame->core->window);
291 for (i = 0; i < menu->cascade_no; i++) {
292 if (menu->cascades[i])
293 unmapmenus(menu->cascades[i]);
297 void wAppMenuUnmap(WMenu * menu)
299 if (menu)
300 unmapmenus(menu);