-
Notifications
You must be signed in to change notification settings - Fork 5
/
xmenu.c
250 lines (227 loc) · 5.73 KB
/
xmenu.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
#include "shod.h"
#define BORDER 1 /* pixel size of decoration around menus */
static struct Menu *
menunew(Window win, int x, int y, int w, int h)
{
struct Menu *menu;
int framex, framey, framew, frameh, titlew;
/* adjust geometry for border on all sides and titlebar on top */
framex = x - BORDER;
framey = y - config.titlewidth;
framew = w + 2 * BORDER;
frameh = h + BORDER + config.titlewidth;
titlew = framew - config.titlewidth;
if (titlew < 0)
titlew = framew;
menu = emalloc(sizeof(*menu));
*menu = (struct Menu){
.titlebar = None,
.button = None,
.obj.win = win,
.obj.class = menu_class,
.pixtitlebar = None,
.x = framex,
.y = framey,
.w = framew,
.h = frameh,
};
menu->frame = createframe((XRectangle){0, 0, framew, frameh});
menu->titlebar = createdecoration(
menu->frame,
(XRectangle){0, 0, titlew, config.titlewidth},
None, NorthWestGravity
);
menu->button = createdecoration(
menu->frame,
(XRectangle){titlew, 0, config.titlewidth, config.titlewidth},
wm.cursors[CURSOR_PIRATE], NorthEastGravity
);
XReparentWindow(
dpy,
menu->obj.win, menu->frame,
BORDER, config.titlewidth
);
XMapWindow(dpy, menu->obj.win);
XMapWindow(dpy, menu->button);
XMapWindow(dpy, menu->titlebar);
return menu;
}
/* remove menu from the menu list */
static void
menudelraise(struct Menu *menu)
{
if (TAILQ_EMPTY(&wm.menuq))
return;
TAILQ_REMOVE(&wm.menuq, (struct Object *)menu, entry);
}
/* configure menu window */
void
menuconfigure(struct Menu *menu, unsigned int valuemask, XWindowChanges *wc)
{
if (menu == NULL)
return;
if (valuemask & CWX)
menu->x = wc->x - BORDER;
if (valuemask & CWY)
menu->y = wc->y - config.titlewidth;
if (valuemask & CWWidth)
menu->w = wc->width + 2 * BORDER;
if (valuemask & CWHeight)
menu->h = wc->height + BORDER + config.titlewidth;
menumoveresize(menu);
menudecorate(menu);
}
static void
menunotify(struct Menu *menu)
{
winnotify(
menu->obj.win,
menu->x + BORDER,
menu->y + config.titlewidth,
menu->w - 2 * BORDER,
menu->h - BORDER - config.titlewidth
);
}
void
menuincrmove(struct Menu *menu, int x, int y)
{
menu->x += x;
menu->y += y;
XMoveWindow(dpy, menu->frame, menu->x, menu->y);
menunotify(menu);
}
/* commit menu geometry */
void
menumoveresize(struct Menu *menu)
{
XMoveResizeWindow(dpy, menu->frame, menu->x, menu->y, menu->w, menu->h);
XMoveWindow(dpy, menu->button, menu->w - config.titlewidth, 0);
XResizeWindow(
dpy, menu->titlebar,
max(1, menu->w - config.titlewidth),
config.titlewidth
);
XResizeWindow(
dpy, menu->obj.win,
max(1, menu->w - 2 * BORDER),
max(1, menu->h - BORDER - config.titlewidth)
);
menu->mon = getmon(menu->x, menu->y);
menudecorate(menu);
menunotify(menu);
}
/* decorate menu */
void
menudecorate(struct Menu *menu)
{
int tw;
tw = max(1, menu->w - config.titlewidth);
updatepixmap(&menu->pixtitlebar, &menu->tw, NULL, tw, config.titlewidth);
drawshadow(
menu->pixtitlebar,
0, 0, tw, config.titlewidth,
FOCUSED, False, config.shadowthickness
);
drawtitle(menu->pixtitlebar, menu->name, tw, 0, FOCUSED, 0, 1);
drawcommit(wm.decorations[FOCUSED].btn_right, menu->button);
drawcommit(menu->pixtitlebar, menu->titlebar);
}
void
menufocus(struct Menu *menu)
{
XSetInputFocus(dpy, menu->obj.win, RevertToParent, CurrentTime);
}
/* put menu on beginning of menu list */
void
menufocusraise(struct Menu *menu)
{
menudelraise(menu);
TAILQ_INSERT_HEAD(&wm.menuq, (struct Object *)menu, entry);
menufocus(menu);
}
/* place menu next to its container */
void
menuplace(struct Monitor *mon, struct Menu *menu)
{
fitmonitor(mon, &menu->x, &menu->y, &menu->w, &menu->h, 1.0);
menumoveresize(menu);
}
/* raise desktop menu */
void
menuraise(struct Menu *menu)
{
Window wins[2];
wins[1] = menu->frame;
wins[0] = wm.layers[LAYER_MENU].frame;
XRestackWindows(dpy, wins, 2);
}
static void
manage(struct Tab *tab, struct Monitor *mon, int desk, Window win, Window leader, XRectangle rect, enum State state)
{
struct Menu *menu;
(void)tab;
(void)mon;
(void)desk;
(void)state;
menu = menunew(win, rect.x, rect.y, rect.width, rect.height);
menu->leader = leader;
winupdatetitle(menu->obj.win, &menu->name);
TAILQ_INSERT_HEAD(&wm.menuq, (struct Object *)menu, entry);
icccmwmstate(menu->obj.win, NormalState);
menuplace(mon, menu); /* this will set menu->mon for us */
menudecorate(menu);
menuraise(menu);
if (menu->leader == None ||
(wm.focused != NULL &&
istabformenu(wm.focused->selcol->selrow->seltab, menu))) {
XMapWindow(dpy, menu->frame);
menufocus(menu);
}
}
static void
unmanage(struct Object *obj)
{
struct Menu *menu;
menu = (struct Menu *)obj;
menudelraise(menu);
if (menu->pixtitlebar != None)
XFreePixmap(dpy, menu->pixtitlebar);
icccmdeletestate(menu->obj.win);
XReparentWindow(dpy, menu->obj.win, root, 0, 0);
XDestroyWindow(dpy, menu->frame);
XDestroyWindow(dpy, menu->titlebar);
XDestroyWindow(dpy, menu->button);
free(menu->name);
free(menu);
}
/* check if given tab accepts given menu */
int
istabformenu(struct Tab *tab, struct Menu *menu)
{
return (menu->leader == tab->obj.win || menu->leader == tab->leader);
}
/* map menus for current focused tab */
void
menuupdate(void)
{
struct Object *obj;
struct Menu *menu;
TAILQ_FOREACH(obj, &wm.menuq, entry) {
menu = ((struct Menu *)obj);
if (menu->leader == None)
continue;
if (!wm.showingdesk && wm.focused != NULL && istabformenu(wm.focused->selcol->selrow->seltab, menu)) {
XMapWindow(dpy, menu->frame);
icccmwmstate(obj->win, NormalState);
} else {
XUnmapWindow(dpy, ((struct Menu *)obj)->frame);
icccmwmstate(obj->win, IconicState);
}
}
}
struct Class *menu_class = &(struct Class){
.type = TYPE_MENU,
.setstate = NULL,
.manage = manage,
.unmanage = unmanage,
};