Improve dockapp recognition
[wmaker-crm.git] / src / appmenu.c
blob1f821109e19d7619458071a9ebe41fc08bb86e26
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"
38 #include "appmenu.h"
39 #include "framewin.h"
41 /******** Global Variables **********/
42 extern Atom _XA_WINDOWMAKER_MENU;
43 extern Time LastTimestamp;
44 extern WPreferences wPreferences;
46 typedef struct {
47 short code;
48 short tag;
49 Window window;
50 } WAppMenuData;
52 enum {
53 wmBeginMenu = 1,
54 wmEndMenu = 2,
55 wmNormalItem = 10,
56 wmDoubleItem = 11,
57 wmSubmenuItem = 12
60 enum {
61 wmSelectItem = 1
64 static void sendMessage(Window window, int what, int tag)
66 XEvent event;
68 event.xclient.type = ClientMessage;
69 event.xclient.message_type = _XA_WINDOWMAKER_MENU;
70 event.xclient.format = 32;
71 event.xclient.display = dpy;
72 event.xclient.window = window;
73 event.xclient.data.l[0] = LastTimestamp;
74 event.xclient.data.l[1] = what;
75 event.xclient.data.l[2] = tag;
76 event.xclient.data.l[3] = 0;
77 XSendEvent(dpy, window, False, NoEventMask, &event);
78 XFlush(dpy);
81 static void notifyClient(WMenu * menu, WMenuEntry * entry)
83 WAppMenuData *data = entry->clientdata;
85 sendMessage(data->window, wmSelectItem, data->tag);
88 static WMenu *parseMenuCommand(WScreen * scr, Window win, char **slist, int count, int *index)
90 WMenu *menu;
91 int command;
92 int code, pos;
93 char title[300];
94 char rtext[300];
96 if (strlen(slist[*index]) > 300) {
97 wwarning("appmenu: menu command size exceeded in window %lx", win);
98 return NULL;
100 if (sscanf(slist[*index], "%i %i %n", &command, &code, &pos) < 2 || command != wmBeginMenu) {
101 wwarning("appmenu: bad menu entry \"%s\" in window %lx", slist[*index], win);
102 return NULL;
104 strcpy(title, &slist[*index][pos]);
105 menu = wMenuCreateForApp(scr, title, *index == 1);
106 if (!menu)
107 return NULL;
108 *index += 1;
109 while (*index < count) {
110 int ecode, etag, enab;
112 if (sscanf(slist[*index], "%i", &command) != 1) {
113 wMenuDestroy(menu, True);
114 wwarning("appmenu: bad menu entry \"%s\" in window %lx", slist[*index], win);
115 return NULL;
118 if (command == wmEndMenu) {
119 *index += 1;
120 break;
122 } else if (command == wmNormalItem || command == wmDoubleItem) {
123 WAppMenuData *data;
124 WMenuEntry *entry;
126 if (command == wmNormalItem) {
127 if (sscanf(slist[*index], "%i %i %i %i %n",
128 &command, &ecode, &etag, &enab, &pos) != 4 || ecode != code) {
129 wMenuDestroy(menu, True);
130 wwarning("appmenu: bad menu entry \"%s\" in window %lx",
131 slist[*index], win);
132 return NULL;
134 strcpy(title, &slist[*index][pos]);
135 rtext[0] = 0;
136 } else {
137 if (sscanf(slist[*index], "%i %i %i %i %s %n",
138 &command, &ecode, &etag, &enab, rtext, &pos) != 5 || ecode != code) {
139 wMenuDestroy(menu, True);
140 wwarning("appmenu: bad menu entry \"%s\" in window %lx",
141 slist[*index], win);
142 return NULL;
144 strcpy(title, &slist[*index][pos]);
146 if (!(data = malloc(sizeof(WAppMenuData)))) {
147 wwarning("appmenu: out of memory making menu for window %lx", win);
148 wMenuDestroy(menu, True);
149 return NULL;
151 data->code = code;
152 data->tag = etag;
153 data->window = win;
154 entry = wMenuAddCallback(menu, title, notifyClient, data);
155 if (!entry) {
156 wMenuDestroy(menu, True);
157 wwarning("appmenu: out of memory creating menu for window %lx", win);
158 wfree(data);
159 return NULL;
161 if (rtext[0] != 0)
162 entry->rtext = wstrdup(rtext);
163 else
164 entry->rtext = NULL;
165 entry->free_cdata = free;
166 *index += 1;
168 } else if (command == wmSubmenuItem) {
169 int ncode;
170 WMenuEntry *entry;
171 WMenu *submenu;
173 if (sscanf(slist[*index], "%i %i %i %i %i %n",
174 &command, &ecode, &etag, &enab, &ncode, &pos) != 5 || ecode != code) {
175 wMenuDestroy(menu, True);
176 wwarning("appmenu: bad menu entry \"%s\" in window %lx", slist[*index], win);
178 return NULL;
180 strcpy(title, &slist[*index][pos]);
181 *index += 1;
183 submenu = parseMenuCommand(scr, win, slist, count, index);
185 entry = wMenuAddCallback(menu, title, NULL, NULL);
187 if (!entry) {
188 wMenuDestroy(menu, True);
189 wMenuDestroy(submenu, True);
190 wwarning("appmenu: out of memory creating menu for window %lx", win);
191 return NULL;
194 wMenuEntrySetCascade(menu, entry, submenu);
196 } else {
197 wMenuDestroy(menu, True);
198 wwarning("appmenu: bad menu entry \"%s\" in window %lx", slist[*index], win);
199 return NULL;
203 return menu;
206 WMenu *wAppMenuGet(WScreen * scr, Window window)
208 XTextProperty text_prop;
209 int count, i;
210 char **slist;
211 WMenu *menu;
213 if (!XGetTextProperty(dpy, window, &text_prop, _XA_WINDOWMAKER_MENU)) {
214 return NULL;
216 if (!XTextPropertyToStringList(&text_prop, &slist, &count) || count < 1) {
217 XFree(text_prop.value);
218 return NULL;
220 XFree(text_prop.value);
221 if (strcmp(slist[0], "WMMenu 0") != 0) {
222 wwarning("appmenu: unknown version of WMMenu in window %lx: %s", window, slist[0]);
223 XFreeStringList(slist);
224 return NULL;
227 i = 1;
228 menu = parseMenuCommand(scr, window, slist, count, &i);
229 if (menu)
230 menu->parent = NULL;
232 XFreeStringList(slist);
234 return menu;
237 void wAppMenuDestroy(WMenu * menu)
239 if (menu)
240 wMenuDestroy(menu, True);
243 static void mapmenus(WMenu * menu)
245 int i;
247 if (menu->flags.mapped)
248 XMapWindow(dpy, menu->frame->core->window);
249 if (menu->brother->flags.mapped)
250 XMapWindow(dpy, menu->brother->frame->core->window);
251 for (i = 0; i < menu->cascade_no; i++) {
252 if (menu->cascades[i])
253 mapmenus(menu->cascades[i]);
257 void wAppMenuMap(WMenu * menu, WWindow * wwin)
260 if (!menu)
261 return;
263 if (!menu->flags.mapped) {
264 wMenuMap(menu);
266 if (wwin && (wPreferences.focus_mode != WKF_CLICK)) {
267 int x, min;
269 min = 20; /* Keep at least 20 pixels visible */
270 if (wwin->frame_x > min) {
271 x = wwin->frame_x - menu->frame->core->width;
272 } else {
273 x = min - menu->frame->core->width;
275 wMenuMove(menu, x, wwin->frame_y, True);
277 mapmenus(menu);
281 static void unmapmenus(WMenu * menu)
283 int i;
285 if (menu->flags.mapped)
286 XUnmapWindow(dpy, menu->frame->core->window);
287 if (menu->brother->flags.mapped)
288 XUnmapWindow(dpy, menu->brother->frame->core->window);
289 for (i = 0; i < menu->cascade_no; i++) {
290 if (menu->cascades[i])
291 unmapmenus(menu->cascades[i]);
295 void wAppMenuUnmap(WMenu * menu)
297 if (menu)
298 unmapmenus(menu);