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"
43 /******** Global Variables **********/
44 extern Atom _XA_WINDOWMAKER_MENU
;
45 extern Atom _XA_WINDOWMAKER_WM_PROTOCOLS
;
46 extern Time LastTimestamp
;
48 extern WPreferences wPreferences
;
72 sendMessage(Window window
, int what
, int tag
)
76 event
.xclient
.type
= ClientMessage
;
77 event
.xclient
.message_type
= _XA_WINDOWMAKER_MENU
;
78 event
.xclient
.format
= 32;
79 event
.xclient
.display
= dpy
;
80 event
.xclient
.window
= window
;
81 event
.xclient
.data
.l
[0] = LastTimestamp
;
82 event
.xclient
.data
.l
[1] = what
;
83 event
.xclient
.data
.l
[2] = tag
;
84 event
.xclient
.data
.l
[3] = 0;
85 XSendEvent(dpy
, window
, False
, NoEventMask
, &event
);
91 notifyClient(WMenu
*menu
, WMenuEntry
*entry
)
93 WAppMenuData
*data
= entry
->clientdata
;
95 sendMessage(data
->window
, wmSelectItem
, data
->tag
);
101 parseMenuCommand(WScreen
*scr
, Window win
, char **slist
, int count
, int *index
)
109 if (strlen(slist
[*index
])>300) {
110 wwarning("appmenu: menu command size exceeded in window %x", win
);
113 if (sscanf(slist
[*index
], "%i %i %n", &command
, &code
, &pos
)<2
114 || command
!=wmBeginMenu
) {
115 wwarning("appmenu: bad menu entry \"%s\" in window %x",
119 strcpy(title
, &slist
[*index
][pos
]);
120 menu
= wMenuCreateForApp(scr
, title
, *index
==1);
124 while (*index
<count
) {
125 int ecode
, etag
, enab
;
127 if (sscanf(slist
[*index
], "%i", &command
)!=1) {
128 wMenuDestroy(menu
, True
);
129 wwarning("appmenu: bad menu entry \"%s\" in window %x",
134 if (command
==wmEndMenu
) {
138 } else if (command
==wmNormalItem
139 || command
==wmDoubleItem
) {
143 if (command
== wmNormalItem
) {
144 if (sscanf(slist
[*index
], "%i %i %i %i %n",
145 &command
, &ecode
, &etag
, &enab
, &pos
)!=4
147 wMenuDestroy(menu
, True
);
148 wwarning("appmenu: bad menu entry \"%s\" in window %x",
152 strcpy(title
, &slist
[*index
][pos
]);
155 if (sscanf(slist
[*index
], "%i %i %i %i %s %n",
156 &command
, &ecode
, &etag
, &enab
, rtext
, &pos
)!=5
158 wMenuDestroy(menu
, True
);
159 wwarning("appmenu: bad menu entry \"%s\" in window %x",
163 strcpy(title
, &slist
[*index
][pos
]);
165 if (!(data
= malloc(sizeof(WAppMenuData
)))) {
166 wwarning("appmenu: out of memory making menu for window %x",
168 wMenuDestroy(menu
, True
);
174 entry
= wMenuAddCallback(menu
, title
, notifyClient
, data
);
176 wMenuDestroy(menu
, True
);
177 wwarning("appmenu: out of memory creating menu for window %x",
183 entry
->rtext
= wstrdup(rtext
);
186 entry
->free_cdata
= free
;
189 } else if (command
==wmSubmenuItem
) {
194 if (sscanf(slist
[*index
], "%i %i %i %i %i %n",
195 &command
, &ecode
, &etag
, &enab
, &ncode
, &pos
)!=5
197 wMenuDestroy(menu
, True
);
198 wwarning("appmenu: bad menu entry \"%s\" in window %x",
203 strcpy(title
, &slist
[*index
][pos
]);
206 submenu
= parseMenuCommand(scr
, win
, slist
, count
, index
);
208 entry
= wMenuAddCallback(menu
, title
, NULL
, NULL
);
211 wMenuDestroy(menu
, True
);
212 wMenuDestroy(submenu
, True
);
213 wwarning("appmenu: out of memory creating menu for window %x",
218 wMenuEntrySetCascade(menu
, entry
, submenu
);
221 wMenuDestroy(menu
, True
);
222 wwarning("appmenu: bad menu entry \"%s\" in window %x",
233 wAppMenuGet(WScreen
*scr
, Window window
)
235 XTextProperty text_prop
;
240 if (!XGetTextProperty(dpy
, window
, &text_prop
, _XA_WINDOWMAKER_MENU
)) {
243 if (!XTextPropertyToStringList(&text_prop
, &slist
, &count
) || count
<1) {
244 XFree(text_prop
.value
);
247 XFree(text_prop
.value
);
248 if (strcmp(slist
[0], "WMMenu 0")!=0) {
249 wwarning("appmenu: unknown version of WMMenu in window %x: %s",
251 XFreeStringList(slist
);
256 menu
= parseMenuCommand(scr
, window
, slist
, count
, &i
);
260 XFreeStringList(slist
);
266 wAppMenuDestroy(WMenu
*menu
)
269 wMenuDestroy(menu
, True
);
274 mapmenus(WMenu
*menu
)
278 if (menu
->flags
.mapped
)
279 XMapWindow(dpy
, menu
->frame
->core
->window
);
280 if (menu
->brother
->flags
.mapped
)
281 XMapWindow(dpy
, menu
->brother
->frame
->core
->window
);
282 for (i
=0; i
<menu
->cascade_no
; i
++) {
283 if (menu
->cascades
[i
])
284 mapmenus(menu
->cascades
[i
]);
290 wAppMenuMap(WMenu
*menu
, WWindow
*wwin
)
296 if (!menu
->flags
.mapped
) {
299 if(wwin
&& (wPreferences
.focus_mode
!=WKF_CLICK
)) {
302 min
= 20; /* Keep at least 20 pixels visible */
303 if (wwin
->frame_x
> min
) {
304 x
= wwin
->frame_x
- menu
->frame
->core
->width
;
307 x
= min
- menu
->frame
->core
->width
;
309 wMenuMove(menu
, x
, wwin
->frame_y
, True
);
317 unmapmenus(WMenu
*menu
)
321 if (menu
->flags
.mapped
)
322 XUnmapWindow(dpy
, menu
->frame
->core
->window
);
323 if (menu
->brother
->flags
.mapped
)
324 XUnmapWindow(dpy
, menu
->brother
->frame
->core
->window
);
325 for (i
=0; i
<menu
->cascade_no
; i
++) {
326 if (menu
->cascades
[i
])
327 unmapmenus(menu
->cascades
[i
]);
332 wAppMenuUnmap(WMenu
*menu
)