a7732690 |
1 | #include <err.h> |
2 | #include <stdio.h> |
3 | #include <stdlib.h> |
4 | #include <string.h> |
5 | #include <unistd.h> |
6 | #include <X11/Xlib.h> |
7 | #include <X11/Xutil.h> |
f644b8bc |
8 | #include <X11/Xresource.h> |
a7732690 |
9 | |
10 | /* macros */ |
11 | #define LEN(x) (sizeof (x) / sizeof (x[0])) |
f1583285 |
12 | #define MAX(x,y) ((x)>(y)?(x):(y)) |
d8a7caf2 |
13 | #define MIN(x,y) ((x)<(y)?(x):(y)) |
a7732690 |
14 | |
15 | /* color enum */ |
16 | enum {ColorFG, ColorBG, ColorLast}; |
17 | |
18 | /* draw context structure */ |
19 | struct DC { |
f644b8bc |
20 | unsigned long normal[ColorLast]; |
21 | unsigned long selected[ColorLast]; |
a7732690 |
22 | unsigned long decoration[ColorLast]; |
23 | |
24 | Drawable d; |
25 | GC gc; |
26 | XFontStruct *font; |
27 | int fonth; |
28 | }; |
29 | |
30 | /* menu geometry structure */ |
31 | struct Geometry { |
32 | int itemb; /* item border */ |
33 | int itemw; /* item width */ |
34 | int itemh; /* item height */ |
a80fee22 |
35 | int border; /* window border width */ |
36 | int separator; /* menu separator width */ |
a7732690 |
37 | }; |
38 | |
39 | /* screen geometry structure */ |
40 | struct ScreenGeometry { |
41 | int cursx, cursy; /* cursor position */ |
42 | int screenw, screenh; /* screen width and height */ |
43 | }; |
44 | |
45 | /* menu item structure */ |
46 | struct Item { |
d8a7caf2 |
47 | char *label; /* string to be drawed on menu */ |
48 | char *output; /* string to be outputed when item is clicked */ |
49 | int y; /* item y position relative to menu */ |
50 | int h; /* item height */ |
51 | size_t labellen; /* strlen(label) */ |
52 | struct Item *next; /* next item */ |
53 | struct Menu *submenu; /* submenu spawned by clicking on item */ |
a7732690 |
54 | }; |
55 | |
56 | /* menu structure */ |
57 | struct Menu { |
d8a7caf2 |
58 | struct Menu *parent; /* parent menu */ |
59 | struct Item *caller; /* item that spawned the menu */ |
60 | struct Item *list; /* list of items contained by the menu */ |
61 | struct Item *selected; /* item currently selected in the menu */ |
62 | int x, y, w, h; /* menu geometry */ |
63 | unsigned level; /* menu level relative to root */ |
64 | Drawable pixmap; /* pixmap to draw the menu on */ |
65 | Window win; /* menu window to map on the screen */ |
a7732690 |
66 | }; |
67 | |
68 | /* function declarations */ |
69 | static unsigned long getcolor(const char *s); |
f644b8bc |
70 | static void getresources(void); |
a7732690 |
71 | static void setupdc(void); |
72 | static void setupgeom(void); |
73 | static void setupgrab(void); |
a80fee22 |
74 | static struct Item *allocitem(const char *label, const char *output); |
a7732690 |
75 | static struct Menu *allocmenu(struct Menu *parent, struct Item *list, unsigned level); |
f15fc339 |
76 | static void getmenuitem(Window win, int y, struct Menu **menu_ret, struct Item **item_ret); |
a7732690 |
77 | static void drawmenu(void); |
78 | static void calcscreengeom(void); |
79 | static void calcmenu(struct Menu *menu); |
80 | static void setcurrmenu(struct Menu *currmenu_new); |
81 | static void parsestdin(void); |
82 | static void run(void); |
f15fc339 |
83 | static void freewindow(struct Menu *menu); |
08f16589 |
84 | static void cleanup(void); |
a7732690 |
85 | static void usage(void); |
86 | |
87 | /* X variables */ |
88 | static Colormap colormap; |
89 | static Display *dpy; |
90 | static Window rootwin; |
91 | static int screen; |
92 | static struct DC dc; |
93 | |
94 | /* menu variables */ |
95 | static struct Menu *rootmenu = NULL; |
96 | static struct Menu *currmenu = NULL; |
97 | |
98 | /* geometry variables */ |
99 | static struct Geometry geom; |
a80fee22 |
100 | static struct ScreenGeometry screengeom; |
a7732690 |
101 | |
102 | /* flag variables */ |
103 | static Bool override_redirect = True; |
104 | |
105 | #include "config.h" |
106 | |
107 | int |
108 | main(int argc, char *argv[]) |
109 | { |
110 | int ch; |
111 | |
112 | while ((ch = getopt(argc, argv, "w")) != -1) { |
113 | switch (ch) { |
114 | case 'w': |
115 | override_redirect = False; |
116 | break; |
117 | default: |
118 | usage(); |
119 | break; |
120 | } |
121 | } |
122 | argc -= optind; |
123 | argv += optind; |
124 | |
125 | /* open connection to server and set X variables */ |
126 | if ((dpy = XOpenDisplay(NULL)) == NULL) |
127 | errx(1, "cannot open display"); |
128 | screen = DefaultScreen(dpy); |
129 | rootwin = RootWindow(dpy, screen); |
130 | colormap = DefaultColormap(dpy, screen); |
131 | |
132 | /* setup */ |
f644b8bc |
133 | getresources(); |
a7732690 |
134 | setupdc(); |
135 | setupgeom(); |
136 | setupgrab(); |
137 | |
138 | /* generate menus and recalculate them */ |
139 | parsestdin(); |
140 | if (rootmenu == NULL) |
141 | errx(1, "no menu generated"); |
142 | calcscreengeom(); |
143 | calcmenu(rootmenu); |
144 | |
d8a7caf2 |
145 | /* map root menu */ |
146 | currmenu = rootmenu; |
147 | XMapWindow(dpy, rootmenu->win); |
148 | |
a7732690 |
149 | /* run event loop */ |
150 | run(); |
151 | |
08f16589 |
152 | cleanup(); |
153 | |
154 | return 0; |
a7732690 |
155 | } |
156 | |
f644b8bc |
157 | /* read xrdb for configuration options */ |
158 | static void |
159 | getresources(void) |
160 | { |
161 | char *xrm; |
162 | long n; |
163 | |
164 | XrmInitialize(); |
165 | if ((xrm = XResourceManagerString(dpy))) { |
166 | char *type; |
167 | XrmDatabase xdb; |
168 | XrmValue xval; |
169 | |
170 | xdb = XrmGetStringDatabase(xrm); |
171 | |
172 | if (XrmGetResource(xdb, "xmenu.menuborder", "*", &type, &xval) == True) |
173 | if ((n = strtol(xval.addr, NULL, 10)) > 0) |
174 | menuborder = n; |
175 | if (XrmGetResource(xdb, "xmenu.separatorsize", "*", &type, &xval) == True) |
176 | if ((n = strtol(xval.addr, NULL, 10)) > 0) |
177 | separatorsize = n; |
178 | if (XrmGetResource(xdb, "xmenu.itemborder", "*", &type, &xval) == True) |
179 | if ((n = strtol(xval.addr, NULL, 10)) > 0) |
180 | itemborder = n; |
181 | if (XrmGetResource(xdb, "xmenu.width", "*", &type, &xval) == True) |
182 | if ((n = strtol(xval.addr, NULL, 10)) > 0) |
183 | width = n; |
184 | if (XrmGetResource(xdb, "xmenu.background", "*", &type, &xval) == True) |
185 | background = strdup(xval.addr); |
186 | if (XrmGetResource(xdb, "xmenu.foreground", "*", &type, &xval) == True) |
187 | foreground = strdup(xval.addr); |
188 | if (XrmGetResource(xdb, "xmenu.selbackground", "*", &type, &xval) == True) |
189 | selbackground = strdup(xval.addr); |
190 | if (XrmGetResource(xdb, "xmenu.selforeground", "*", &type, &xval) == True) |
191 | selforeground = strdup(xval.addr); |
192 | if (XrmGetResource(xdb, "xmenu.separator", "*", &type, &xval) == True) |
193 | separator = strdup(xval.addr); |
194 | if (XrmGetResource(xdb, "xmenu.border", "*", &type, &xval) == True) |
195 | border = strdup(xval.addr); |
196 | if (XrmGetResource(xdb, "xmenu.font", "*", &type, &xval) == True) |
197 | font = strdup(xval.addr); |
198 | |
199 | XrmDestroyDatabase(xdb); |
200 | } |
201 | } |
202 | |
a7732690 |
203 | /* get color from color string */ |
204 | static unsigned long |
205 | getcolor(const char *s) |
206 | { |
207 | XColor color; |
208 | |
209 | if(!XAllocNamedColor(dpy, colormap, s, &color, &color)) |
210 | errx(1, "cannot allocate color: %s", s); |
211 | return color.pixel; |
212 | } |
213 | |
214 | /* init draw context */ |
215 | static void |
216 | setupdc(void) |
217 | { |
218 | /* get color pixels */ |
f644b8bc |
219 | dc.normal[ColorBG] = getcolor(background); |
220 | dc.normal[ColorFG] = getcolor(foreground); |
221 | dc.selected[ColorBG] = getcolor(selbackground); |
222 | dc.selected[ColorFG] = getcolor(selforeground); |
223 | dc.decoration[ColorBG] = getcolor(separator); |
224 | dc.decoration[ColorFG] = getcolor(border); |
a7732690 |
225 | |
226 | /* try to get font */ |
f644b8bc |
227 | if ((dc.font = XLoadQueryFont(dpy, font)) == NULL) |
a7732690 |
228 | errx(1, "cannot load font"); |
229 | dc.fonth = dc.font->ascent + dc.font->descent; |
230 | |
231 | /* create GC and set its font */ |
232 | dc.gc = XCreateGC(dpy, rootwin, 0, NULL); |
233 | XSetFont(dpy, dc.gc, dc.font->fid); |
234 | } |
235 | |
236 | /* init menu geometry values */ |
237 | static void |
238 | setupgeom(void) |
239 | { |
f644b8bc |
240 | geom.itemb = itemborder; |
241 | geom.itemh = dc.fonth + itemborder * 2; |
242 | geom.itemw = width; |
243 | geom.border = menuborder; |
244 | geom.separator = separatorsize; |
a7732690 |
245 | } |
246 | |
247 | /* grab pointer */ |
248 | static void |
249 | setupgrab(void) |
250 | { |
251 | XGrabPointer(dpy, rootwin, True, ButtonPressMask | ButtonReleaseMask, |
252 | GrabModeAsync, GrabModeAsync, None, None, CurrentTime); |
253 | } |
254 | |
255 | /* allocate an item */ |
256 | static struct Item * |
a80fee22 |
257 | allocitem(const char *label, const char *output) |
a7732690 |
258 | { |
259 | struct Item *item; |
260 | |
261 | if ((item = malloc(sizeof *item)) == NULL) |
262 | err(1, "malloc"); |
7fbd1c5e |
263 | if (*label == '\0') { |
264 | item->label = NULL; |
265 | item->output = NULL; |
266 | } else { |
267 | if ((item->label = strdup(label)) == NULL) |
268 | err(1, "strdup"); |
269 | if ((item->output = strdup(output)) == NULL) |
270 | err(1, "strdup"); |
271 | } |
a80fee22 |
272 | item->y = 0; |
7fbd1c5e |
273 | item->h = item->label ? geom.itemh : geom.separator; |
f1583285 |
274 | if (item->label == NULL) |
275 | item->labellen = 0; |
276 | else |
277 | item->labellen = strlen(item->label); |
a7732690 |
278 | item->next = NULL; |
279 | item->submenu = NULL; |
280 | |
281 | return item; |
282 | } |
283 | |
284 | /* allocate a menu */ |
285 | static struct Menu * |
286 | allocmenu(struct Menu *parent, struct Item *list, unsigned level) |
287 | { |
288 | XSetWindowAttributes swa; |
289 | struct Menu *menu; |
290 | |
291 | if ((menu = malloc(sizeof *menu)) == NULL) |
292 | err(1, "malloc"); |
293 | menu->parent = parent; |
294 | menu->list = list; |
d888f2ca |
295 | menu->caller = NULL; |
a7732690 |
296 | menu->selected = NULL; |
a7732690 |
297 | menu->w = geom.itemw; |
a80fee22 |
298 | menu->h = 0; /* calculated by calcmenu() */ |
299 | menu->x = 0; /* calculated by calcmenu() */ |
300 | menu->y = 0; /* calculated by calcmenu() */ |
a7732690 |
301 | menu->level = level; |
a7732690 |
302 | |
303 | swa.override_redirect = override_redirect; |
304 | swa.background_pixel = dc.decoration[ColorBG]; |
305 | swa.border_pixel = dc.decoration[ColorFG]; |
306 | swa.event_mask = ExposureMask | KeyPressMask | ButtonPressMask | ButtonReleaseMask |
f15fc339 |
307 | | PointerMotionMask | LeaveWindowMask; |
a7732690 |
308 | menu->win = XCreateWindow(dpy, rootwin, 0, 0, geom.itemw, geom.itemh, geom.border, |
309 | CopyFromParent, CopyFromParent, CopyFromParent, |
310 | CWOverrideRedirect | CWBackPixel | CWBorderPixel | CWEventMask, |
311 | &swa); |
312 | |
313 | return menu; |
314 | } |
315 | |
316 | /* create menus and items from the stdin */ |
317 | static void |
318 | parsestdin(void) |
319 | { |
320 | char *s, buf[BUFSIZ]; |
321 | char *label, *output; |
322 | unsigned level = 0; |
323 | unsigned i; |
a80fee22 |
324 | struct Item *curritem = NULL; /* item currently being read */ |
325 | struct Menu *prevmenu = NULL; /* menu the previous item was added to */ |
326 | struct Item *item; /* dummy item for for loops */ |
327 | struct Menu *menu; /* dummy menu for for loops */ |
a7732690 |
328 | size_t count = 0; /* number of items in the current menu */ |
329 | |
330 | while (fgets(buf, BUFSIZ, stdin) != NULL) { |
331 | level = 0; |
332 | s = buf; |
333 | |
334 | while (*s == '\t') { |
335 | level++; |
336 | s++; |
337 | } |
338 | |
339 | label = output = s; |
340 | |
341 | while (*s != '\0' && *s != '\t' && *s != '\n') |
342 | s++; |
343 | |
344 | while (*s == '\t') |
345 | *s++ = '\0'; |
346 | |
347 | if (*s != '\0' && *s != '\n') |
348 | output = s; |
349 | |
350 | while (*s != '\0' && *s != '\n') |
351 | s++; |
352 | |
353 | if (*s == '\n') |
354 | *s = '\0'; |
355 | |
a80fee22 |
356 | curritem = allocitem(label, output); |
a7732690 |
357 | |
358 | if (prevmenu == NULL) { /* there is no menu yet */ |
a80fee22 |
359 | menu = allocmenu(NULL, curritem, level); |
a7732690 |
360 | rootmenu = menu; |
361 | prevmenu = menu; |
362 | count = 1; |
363 | } else if (level < prevmenu->level) { /* item is continuation of a parent menu*/ |
364 | for (menu = prevmenu, i = level; |
365 | menu != NULL && i < prevmenu->level; |
366 | menu = menu->parent, i++) |
367 | ; |
368 | |
369 | if (menu == NULL) |
370 | errx(1, "reached NULL menu"); |
371 | |
a80fee22 |
372 | for (item = menu->list; item->next != NULL; item = item->next) |
a7732690 |
373 | ; |
374 | |
a80fee22 |
375 | item->next = curritem; |
a7732690 |
376 | prevmenu = menu; |
377 | } else if (level == prevmenu->level) { /* item is a continuation of current menu */ |
a80fee22 |
378 | for (item = prevmenu->list; item->next != NULL; item = item->next) |
a7732690 |
379 | ; |
a80fee22 |
380 | item->next = curritem; |
a7732690 |
381 | } else if (level > prevmenu->level) { /* item begins a new menu */ |
a80fee22 |
382 | menu = allocmenu(prevmenu, curritem, level); |
a7732690 |
383 | |
a80fee22 |
384 | for (item = prevmenu->list; item->next != NULL; item = item->next) |
a7732690 |
385 | ; |
386 | |
a80fee22 |
387 | item->submenu = menu; |
d888f2ca |
388 | menu->caller = item; |
a7732690 |
389 | |
390 | prevmenu = menu; |
391 | } |
a80fee22 |
392 | count++; |
a7732690 |
393 | } |
394 | } |
395 | |
396 | /* calculate screen geometry */ |
397 | static void |
398 | calcscreengeom(void) |
399 | { |
400 | Window w1, w2; /* unused variables */ |
401 | int a, b; /* unused variables */ |
402 | unsigned mask; /* unused variable */ |
403 | |
a80fee22 |
404 | XQueryPointer(dpy, rootwin, &w1, &w2, &screengeom.cursx, &screengeom.cursy, &a, &b, &mask); |
405 | screengeom.screenw = DisplayWidth(dpy, screen); |
406 | screengeom.screenh = DisplayHeight(dpy, screen); |
a7732690 |
407 | } |
408 | |
409 | /* recursivelly calculate height and position of the menus */ |
410 | static void |
411 | calcmenu(struct Menu *menu) |
412 | { |
413 | XWindowChanges changes; |
09c13122 |
414 | XSizeHints sizeh; |
a80fee22 |
415 | struct Item *item; |
f1583285 |
416 | int labelwidth; |
a7732690 |
417 | |
f1583285 |
418 | /* calculate items positions and menu width and height */ |
419 | menu->w = geom.itemw; |
a80fee22 |
420 | for (item = menu->list; item != NULL; item = item->next) { |
421 | item->y = menu->h; |
7fbd1c5e |
422 | if (item->label == NULL) /* height for separator item */ |
a80fee22 |
423 | menu->h += geom.separator; |
424 | else |
425 | menu->h += geom.itemh; |
f1583285 |
426 | |
427 | labelwidth = XTextWidth(dc.font, item->label, item->labellen) + dc.fonth * 2; |
428 | menu->w = MAX(menu->w, labelwidth); |
a80fee22 |
429 | } |
a7732690 |
430 | |
431 | /* calculate menu's x and y positions */ |
432 | if (menu->parent == NULL) { /* if root menu, calculate in respect to cursor */ |
a80fee22 |
433 | if (screengeom.screenw - screengeom.cursx >= menu->w) |
434 | menu->x = screengeom.cursx; |
435 | else if (screengeom.cursx > menu->w) |
436 | menu->x = screengeom.cursx - menu->w; |
437 | |
438 | if (screengeom.screenh - screengeom.cursy >= menu->h) |
439 | menu->y = screengeom.cursy; |
440 | else if (screengeom.screenh > menu->h) |
441 | menu->y = screengeom.screenh - menu->h; |
a7732690 |
442 | } else { /* else, calculate in respect to parent menu */ |
443 | |
444 | /* search for the item in parent menu that generates this menu */ |
a80fee22 |
445 | for (item = menu->parent->list; item->submenu != menu; item = item->next) |
a7732690 |
446 | ; |
447 | |
f644b8bc |
448 | if (screengeom.screenw - (menu->parent->x + menu->parent->w + geom.border) >= menu->w) |
449 | menu->x = menu->parent->x + menu->parent->w + geom.border; |
450 | else if (menu->parent->x > menu->w + geom.border) |
451 | menu->x = menu->parent->x - menu->w - geom.border; |
a7732690 |
452 | |
a80fee22 |
453 | if (screengeom.screenh - (item->y + menu->parent->y) > menu->h) |
454 | menu->y = item->y + menu->parent->y; |
455 | else if (screengeom.screenh - menu->parent->y > menu->h) |
a7732690 |
456 | menu->y = menu->parent->y; |
a80fee22 |
457 | else if (screengeom.screenh > menu->h) |
458 | menu->y = screengeom.screenh - menu->h; |
a7732690 |
459 | } |
460 | |
461 | /* update menu geometry */ |
462 | changes.height = menu->h; |
f1583285 |
463 | changes.width = menu->w; |
a7732690 |
464 | changes.x = menu->x; |
465 | changes.y = menu->y; |
f1583285 |
466 | XConfigureWindow(dpy, menu->win, CWWidth | CWHeight | CWX | CWY, &changes); |
a7732690 |
467 | |
09c13122 |
468 | /* set window manager size hints */ |
469 | sizeh.flags = PMaxSize | PMinSize; |
470 | sizeh.min_width = sizeh.max_width = menu->w; |
471 | sizeh.min_height = sizeh.max_height = menu->h; |
472 | XSetWMNormalHints(dpy, menu->win, &sizeh); |
473 | |
f15fc339 |
474 | /* create pixmap */ |
475 | menu->pixmap = XCreatePixmap(dpy, menu->win, menu->w, menu->h, |
476 | DefaultDepth(dpy, screen)); |
477 | |
a80fee22 |
478 | /* calculate positions of submenus */ |
a7732690 |
479 | for (item = menu->list; item != NULL; item = item->next) { |
480 | if (item->submenu != NULL) |
481 | calcmenu(item->submenu); |
482 | } |
483 | } |
484 | |
485 | /* get menu and item of given window and position */ |
486 | static void |
a80fee22 |
487 | getmenuitem(Window win, int y, |
a7732690 |
488 | struct Menu **menu_ret, struct Item **item_ret) |
489 | { |
490 | struct Menu *menu = NULL; |
491 | struct Item *item = NULL; |
492 | |
493 | for (menu = currmenu; menu != NULL; menu = menu->parent) { |
494 | if (menu->win == win) { |
495 | for (item = menu->list; item != NULL; item = item->next) { |
7fbd1c5e |
496 | if (y >= item->y && y <= item->y + item->h) { |
a7732690 |
497 | goto done; |
498 | } |
499 | } |
500 | } |
501 | } |
502 | |
503 | |
504 | done: |
505 | *menu_ret = menu; |
506 | *item_ret = item; |
507 | } |
508 | |
509 | /* set currentmenu to menu, umap previous menus and map current menu and its parents */ |
510 | static void |
511 | setcurrmenu(struct Menu *currmenu_new) |
512 | { |
d8a7caf2 |
513 | struct Menu *menu, *menu_; |
d888f2ca |
514 | struct Item *item; |
d8a7caf2 |
515 | struct Menu *lcamenu; /* lowest common ancestor menu */ |
516 | unsigned minlevel; /* level of the closest to root menu */ |
517 | unsigned maxlevel; /* level of the closest to root menu */ |
a7732690 |
518 | |
519 | if (currmenu_new == currmenu) |
520 | return; |
521 | |
d8a7caf2 |
522 | /* find lowest common ancestor menu */ |
523 | lcamenu = rootmenu; |
524 | if (currmenu != NULL) { |
525 | minlevel = MIN(currmenu_new->level, currmenu->level); |
526 | maxlevel = MAX(currmenu_new->level, currmenu->level); |
527 | if (currmenu_new->level == maxlevel) { |
528 | menu = currmenu_new; |
529 | menu_ = currmenu; |
530 | } else { |
531 | menu = currmenu; |
532 | menu_ = currmenu_new; |
533 | } |
534 | while (menu->level > minlevel) |
535 | menu = menu->parent; |
536 | |
537 | while (menu != menu_) { |
538 | menu = menu->parent; |
539 | menu_ = menu_->parent; |
540 | } |
541 | lcamenu = menu; |
542 | } |
543 | |
873c080c |
544 | /* unmap menus from currmenu (inclusive) until lcamenu (exclusive) */ |
d8a7caf2 |
545 | for (menu = currmenu; menu != lcamenu; menu = menu->parent) { |
a7732690 |
546 | XUnmapWindow(dpy, menu->win); |
547 | } |
548 | |
549 | currmenu = currmenu_new; |
550 | |
873c080c |
551 | /* map menus from currmenu (inclusive) until lcamenu (exclusive) */ |
d888f2ca |
552 | item = NULL; |
d8a7caf2 |
553 | for (menu = currmenu; menu != lcamenu; menu = menu->parent) { |
a7732690 |
554 | XMapWindow(dpy, menu->win); |
d888f2ca |
555 | if (item != NULL) |
556 | menu->selected = item; |
557 | item = menu->caller; |
558 | } |
a7732690 |
559 | } |
560 | |
561 | /* draw items of the current menu and of its ancestors */ |
562 | static void |
563 | drawmenu(void) |
564 | { |
565 | struct Menu *menu; |
566 | struct Item *item; |
567 | |
568 | for (menu = currmenu; menu != NULL; menu = menu->parent) { |
a7732690 |
569 | for (item = menu->list; item != NULL; item = item->next) { |
570 | unsigned long *color; |
a7732690 |
571 | int labelx, labely; |
a7732690 |
572 | |
573 | /* determine item color */ |
f644b8bc |
574 | if (item == menu->selected) |
575 | color = dc.selected; |
a7732690 |
576 | else |
f644b8bc |
577 | color = dc.normal; |
578 | |
579 | /* continue if item is a separator */ |
580 | if (item->label == NULL) |
581 | continue; |
a7732690 |
582 | |
a7732690 |
583 | /* draw item box */ |
584 | XSetForeground(dpy, dc.gc, color[ColorBG]); |
d888f2ca |
585 | XDrawRectangle(dpy, menu->pixmap, dc.gc, 0, item->y, |
586 | menu->w, item->h); |
f15fc339 |
587 | XFillRectangle(dpy, menu->pixmap, dc.gc, 0, item->y, |
f1583285 |
588 | menu->w, item->h); |
7fbd1c5e |
589 | |
a7732690 |
590 | /* draw item label */ |
a7732690 |
591 | labelx = 0 + dc.fonth; |
7fbd1c5e |
592 | labely = item->y + dc.fonth + geom.itemb; |
a7732690 |
593 | XSetForeground(dpy, dc.gc, color[ColorFG]); |
f1583285 |
594 | XDrawString(dpy, menu->pixmap, dc.gc, labelx, labely, |
595 | item->label, item->labellen); |
a7732690 |
596 | |
597 | /* draw triangle, if item contains a submenu */ |
598 | if (item->submenu != NULL) { |
f1583285 |
599 | int trianglex = menu->w - dc.fonth + geom.itemb - 1; |
2b6968f9 |
600 | int triangley = item->y + (3 * item->h)/8 -1; |
a7732690 |
601 | |
602 | XPoint triangle[] = { |
603 | {trianglex, triangley}, |
2b6968f9 |
604 | {trianglex + item->h/8 + 1, item->y + item->h/2}, |
605 | {trianglex, triangley + item->h/4 + 2}, |
a7732690 |
606 | {trianglex, triangley} |
607 | }; |
608 | |
f15fc339 |
609 | XFillPolygon(dpy, menu->pixmap, dc.gc, triangle, LEN(triangle), |
a7732690 |
610 | Convex, CoordModeOrigin); |
611 | } |
f15fc339 |
612 | |
613 | XCopyArea(dpy, menu->pixmap, menu->win, dc.gc, 0, item->y, |
614 | menu->w, item->h, 0, item->y); |
a7732690 |
615 | } |
616 | } |
617 | } |
618 | |
619 | /* run event loop */ |
620 | static void |
621 | run(void) |
622 | { |
623 | struct Menu *menu; |
624 | struct Item *item; |
625 | struct Item *previtem = NULL; |
626 | XEvent ev; |
627 | |
a7732690 |
628 | while (!XNextEvent(dpy, &ev)) { |
629 | switch(ev.type) { |
630 | case Expose: |
631 | drawmenu(); |
632 | break; |
633 | case MotionNotify: |
a80fee22 |
634 | getmenuitem(ev.xbutton.window, ev.xbutton.y, &menu, &item); |
a7732690 |
635 | if (menu != NULL && item != NULL) { |
636 | if (previtem != item) { |
637 | if (item->submenu != NULL) |
638 | setcurrmenu(item->submenu); |
639 | else |
640 | setcurrmenu(menu); |
641 | previtem = item; |
d888f2ca |
642 | drawmenu(); |
643 | } else if (menu->selected != item) { |
a7732690 |
644 | menu->selected = item; |
d888f2ca |
645 | drawmenu(); |
646 | } |
a7732690 |
647 | } |
a7732690 |
648 | break; |
649 | case ButtonRelease: |
a80fee22 |
650 | getmenuitem(ev.xbutton.window, ev.xbutton.y, &menu, &item); |
a7732690 |
651 | if (menu != NULL && item != NULL) { |
7fbd1c5e |
652 | if (item->label == NULL) |
653 | break; /* ignore separators */ |
a7732690 |
654 | if (item->submenu != NULL) { |
655 | setcurrmenu(item->submenu); |
656 | } else { |
657 | printf("%s\n", item->output); |
08f16589 |
658 | return; |
a7732690 |
659 | } |
660 | drawmenu(); |
661 | } else { |
08f16589 |
662 | return; |
a7732690 |
663 | } |
664 | break; |
f15fc339 |
665 | case LeaveNotify: |
666 | currmenu->selected = NULL; |
667 | drawmenu(); |
668 | break; |
a7732690 |
669 | } |
670 | } |
671 | } |
672 | |
f15fc339 |
673 | /* recursivelly free a pixmap */ |
674 | static void |
675 | freewindow(struct Menu *menu) |
676 | { |
677 | struct Item *item; |
678 | |
679 | for (item = menu->list; item != NULL; item = item->next) |
680 | if (item->submenu != NULL) |
681 | freewindow(item->submenu); |
682 | |
683 | XFreePixmap(dpy, menu->pixmap); |
684 | XDestroyWindow(dpy, menu->win); |
685 | } |
686 | |
a7732690 |
687 | /* cleanup and exit */ |
688 | static void |
08f16589 |
689 | cleanup(void) |
a7732690 |
690 | { |
f15fc339 |
691 | freewindow(rootmenu); |
692 | XFreeFont(dpy, dc.font); |
693 | XFreeGC(dpy, dc.gc); |
a7732690 |
694 | XCloseDisplay(dpy); |
a7732690 |
695 | } |
696 | |
697 | /* show usage */ |
698 | static void |
699 | usage(void) |
700 | { |
08f16589 |
701 | (void)fprintf(stderr, "usage: xmenu [-w]\n"); |
a7732690 |
702 | exit(1); |
703 | } |