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,
26 #include <X11/Xutil.h>
27 #include <X11/Xproto.h>
28 #include <X11/Xatom.h>
34 #include "WindowMaker.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
;
66 static void sendMessage(Window window
, int what
, int tag
)
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
);
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
)
98 if (strlen(slist
[*index
]) > 300) {
99 wwarning("appmenu: menu command size exceeded in window %x", win
);
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
);
106 strcpy(title
, &slist
[*index
][pos
]);
107 menu
= wMenuCreateForApp(scr
, title
, *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
);
120 if (command
== wmEndMenu
) {
124 } else if (command
== wmNormalItem
|| command
== wmDoubleItem
) {
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",
136 strcpy(title
, &slist
[*index
][pos
]);
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",
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
);
156 entry
= wMenuAddCallback(menu
, title
, notifyClient
, data
);
158 wMenuDestroy(menu
, True
);
159 wwarning("appmenu: out of memory creating menu for window %x", slist
[*index
], win
);
164 entry
->rtext
= wstrdup(rtext
);
167 entry
->free_cdata
= free
;
170 } else if (command
== wmSubmenuItem
) {
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
);
182 strcpy(title
, &slist
[*index
][pos
]);
185 submenu
= parseMenuCommand(scr
, win
, slist
, count
, index
);
187 entry
= wMenuAddCallback(menu
, title
, NULL
, NULL
);
190 wMenuDestroy(menu
, True
);
191 wMenuDestroy(submenu
, True
);
192 wwarning("appmenu: out of memory creating menu for window %x", slist
[*index
], win
);
196 wMenuEntrySetCascade(menu
, entry
, submenu
);
199 wMenuDestroy(menu
, True
);
200 wwarning("appmenu: bad menu entry \"%s\" in window %x", slist
[*index
], win
);
208 WMenu
*wAppMenuGet(WScreen
* scr
, Window window
)
210 XTextProperty text_prop
;
215 if (!XGetTextProperty(dpy
, window
, &text_prop
, _XA_WINDOWMAKER_MENU
)) {
218 if (!XTextPropertyToStringList(&text_prop
, &slist
, &count
) || count
< 1) {
219 XFree(text_prop
.value
);
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
);
230 menu
= parseMenuCommand(scr
, window
, slist
, count
, &i
);
234 XFreeStringList(slist
);
239 void wAppMenuDestroy(WMenu
* menu
)
242 wMenuDestroy(menu
, True
);
245 static void mapmenus(WMenu
* menu
)
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
)
265 if (!menu
->flags
.mapped
) {
268 if (wwin
&& (wPreferences
.focus_mode
!= WKF_CLICK
)) {
271 min
= 20; /* Keep at least 20 pixels visible */
272 if (wwin
->frame_x
> min
) {
273 x
= wwin
->frame_x
- menu
->frame
->core
->width
;
275 x
= min
- menu
->frame
->core
->width
;
277 wMenuMove(menu
, x
, wwin
->frame_y
, True
);
283 static void unmapmenus(WMenu
* menu
)
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
)