1 /* menu.c - menu interface functions
3 * WMlib - WindowMaker application programming interface
5 * Copyright (C) 1997 Alfredo K. Kojima
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Library General Public
9 * License as published by the Free Software Foundation; either
10 * version 2 of the License, or (at your option) any later version.
12 * This library 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 GNU
15 * Library General Public License for more details.
17 * You should have received a copy of the GNU Library General Public
18 * License along with this library; if not, write to the Free
19 * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
26 #include <X11/Xutil.h>
34 WMMenuCreate(WMAppContext
*app
, char *title
)
38 if (strlen(title
)>255)
41 menu
= malloc(sizeof(wmMenu
));
45 menu
->appcontext
= app
;
51 menu
->realized
= False
;
52 menu
->code
= app
->last_menu_tag
++;
54 menu
->entryline
= malloc(strlen(title
)+32);
55 menu
->entryline2
= malloc(32);
56 if (!menu
->entryline
|| !menu
->entryline2
) {
58 free(menu
->entryline
);
62 sprintf(menu
->entryline
, "%i %i %s", wmBeginMenu
, menu
->code
, title
);
63 sprintf(menu
->entryline2
, "%i %i", wmEndMenu
, menu
->code
);
69 WMMenuAddItem(WMMenu
*menu
, char *text
, WMMenuAction action
,
70 void *clientData
, WMFreeFunction freedata
, char *rtext
)
74 /* max size of right side text */
75 if (rtext
&& strlen(rtext
)>4)
78 /* max size of menu text */
82 entry
= malloc(sizeof(wmMenuEntry
));
86 entry
->entryline
= malloc(strlen(text
)+100);
87 if (!entry
->entryline
) {
93 entry
->order
= menu
->entries
->order
+ 1;
99 entry
->prev
= menu
->entries
;
101 menu
->entries
->next
= entry
;
102 menu
->entries
= entry
;
106 entry
->shortcut
= rtext
;
107 entry
->callback
= action
;
108 entry
->clientData
= clientData
;
109 entry
->free
= freedata
;
110 entry
->tag
= menu
->appcontext
->last_menu_tag
++;
111 entry
->cascade
= NULL
;
112 entry
->enabled
= True
;
116 sprintf(entry
->entryline
, "%i %i %i %i %s", wmNormalItem
,
117 menu
->code
, entry
->tag
, True
, text
);
119 sprintf(entry
->entryline
, "%i %i %i %i %s %s", wmDoubleItem
,
120 menu
->code
, entry
->tag
, True
, rtext
, text
);
127 WMMenuAddSubmenu(WMMenu
*menu
, char *text
, WMMenu
*submenu
)
131 /* max size of menu text */
132 if (strlen(text
)>255)
135 entry
= malloc(sizeof(wmMenuEntry
));
139 entry
->entryline
= malloc(strlen(text
)+100);
140 if (!entry
->entryline
) {
146 entry
->order
= menu
->entries
->order
+ 1;
152 entry
->prev
= menu
->entries
;
154 menu
->entries
->next
= entry
;
155 menu
->entries
= entry
;
158 entry
->shortcut
= NULL
;
159 entry
->callback
= NULL
;
160 entry
->clientData
= NULL
;
161 entry
->tag
= menu
->appcontext
->last_menu_tag
++;
162 entry
->cascade
= submenu
;
163 entry
->enabled
= True
;
165 sprintf(entry
->entryline
, "%i %i %i %i %i %s", wmSubmenuItem
,
166 menu
->code
, entry
->tag
, True
, submenu
->code
, text
);
172 countItems(WMMenu
*menu
)
174 wmMenuEntry
*entry
= menu
->first
;
180 if (entry
->cascade
) {
181 c
+= countItems(entry
->cascade
);
191 addItems(char **slist
, int *index
, WMMenu
*menu
)
193 wmMenuEntry
*entry
= menu
->first
;
195 slist
[(*index
)++] = menu
->entryline
;
197 slist
[(*index
)++] = entry
->entryline
;
198 if (entry
->cascade
) {
199 addItems(slist
, index
, entry
->cascade
);
203 slist
[(*index
)++] = menu
->entryline2
;
208 getatom(Display
*dpy
)
213 atom
= XInternAtom(dpy
, WMMENU_PROPNAME
, False
);
219 WMRealizeMenus(WMAppContext
*app
)
223 XTextProperty text_prop
;
228 /* first count how many menu items there are */
229 count
= countItems(app
->main_menu
);
234 slist
= malloc(count
*sizeof(char*));
239 slist
[0] = "WMMenu 0";
241 addItems(slist
, &i
, app
->main_menu
);
243 if (!XStringListToTextProperty(slist
, i
, &text_prop
)) {
248 XSetTextProperty(app
->dpy
, app
->main_window
, &text_prop
,
251 XFree(text_prop
.value
);