Add history to some dialog boxes
[wmaker-crm.git] / src / appmenu.c
blob4f1072667c53661cb67a9358d00f15e3cf844251
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 "wcore.h"
36 #include "menu.h"
37 #include "actions.h"
38 #include "funcs.h"
40 #include "framewin.h"
42 /******** Global Variables **********/
43 extern Atom _XA_WINDOWMAKER_MENU;
44 extern Atom _XA_WINDOWMAKER_WM_PROTOCOLS;
45 extern Time LastTimestamp;
47 extern WPreferences wPreferences;
49 typedef struct {
50 short code;
51 short tag;
52 Window window;
53 } WAppMenuData;
55 enum {
56 wmBeginMenu = 1,
57 wmEndMenu = 2,
58 wmNormalItem = 10,
59 wmDoubleItem = 11,
60 wmSubmenuItem = 12
63 enum {
64 wmSelectItem = 1
67 static void sendMessage(Window window, int what, int tag)
69 XEvent event;
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);
84 static void notifyClient(WMenu * menu, WMenuEntry * entry)
86 WAppMenuData *data = entry->clientdata;
88 sendMessage(data->window, wmSelectItem, data->tag);
91 static WMenu *parseMenuCommand(WScreen * scr, Window win, char **slist, int count, int *index)
93 WMenu *menu;
94 int command;
95 int code, pos;
96 char title[300];
97 char rtext[300];
99 if (strlen(slist[*index]) > 300) {
100 wwarning("appmenu: menu command size exceeded in window %x", win);
101 return NULL;
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;
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;
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;
121 if (command == wmEndMenu) {
122 *index += 1;
123 break;
125 } else if (command == wmNormalItem || command == wmDoubleItem) {
126 WAppMenuData *data;
127 WMenuEntry *entry;
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;
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;
147 strcpy(title, &slist[*index][pos]);
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;
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;
164 if (rtext[0] != 0)
165 entry->rtext = wstrdup(rtext);
166 else
167 entry->rtext = NULL;
168 entry->free_cdata = free;
169 *index += 1;
171 } else if (command == wmSubmenuItem) {
172 int ncode;
173 WMenuEntry *entry;
174 WMenu *submenu;
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);
181 return NULL;
183 strcpy(title, &slist[*index][pos]);
184 *index += 1;
186 submenu = parseMenuCommand(scr, win, slist, count, index);
188 entry = wMenuAddCallback(menu, title, NULL, NULL);
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;
197 wMenuEntrySetCascade(menu, entry, submenu);
199 } else {
200 wMenuDestroy(menu, True);
201 wwarning("appmenu: bad menu entry \"%s\" in window %x", slist[*index], win);
202 return NULL;
206 return menu;
209 WMenu *wAppMenuGet(WScreen * scr, Window window)
211 XTextProperty text_prop;
212 int count, i;
213 char **slist;
214 WMenu *menu;
216 if (!XGetTextProperty(dpy, window, &text_prop, _XA_WINDOWMAKER_MENU)) {
217 return NULL;
219 if (!XTextPropertyToStringList(&text_prop, &slist, &count) || count < 1) {
220 XFree(text_prop.value);
221 return NULL;
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;
230 i = 1;
231 menu = parseMenuCommand(scr, window, slist, count, &i);
232 if (menu)
233 menu->parent = NULL;
235 XFreeStringList(slist);
237 return menu;
240 void wAppMenuDestroy(WMenu * menu)
242 if (menu)
243 wMenuDestroy(menu, True);
246 static void mapmenus(WMenu * menu)
248 int i;
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]);
260 void wAppMenuMap(WMenu * menu, WWindow * wwin)
263 if (!menu)
264 return;
266 if (!menu->flags.mapped) {
267 wMenuMap(menu);
269 if (wwin && (wPreferences.focus_mode != WKF_CLICK)) {
270 int x, min;
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;
278 wMenuMove(menu, x, wwin->frame_y, True);
280 mapmenus(menu);
284 static void unmapmenus(WMenu * menu)
286 int i;
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]);
298 void wAppMenuUnmap(WMenu * menu)
300 if (menu)
301 unmapmenus(menu);