/* macros */
#define LEN(x) (sizeof (x) / sizeof (x[0]))
+#define MAX(x,y) ((x)>(y)?(x):(y))
/* color enum */
enum {ColorFG, ColorBG, ColorLast};
struct Item {
char *label;
char *output;
- int y; /* only y is necessary, item's x is always 0 relative to the menu*/
+ int y;
+ int h;
+ size_t labellen;
struct Item *next;
struct Menu *submenu;
};
struct Item *selected;
int x, y, w, h;
unsigned level;
+ Drawable pixmap;
Window win;
};
static void setupgrab(void);
static struct Item *allocitem(const char *label, const char *output);
static struct Menu *allocmenu(struct Menu *parent, struct Item *list, unsigned level);
-static void getmenuitem(Window win, int y,
- struct Menu **menu_ret, struct Item **item_ret);
+static void getmenuitem(Window win, int y, struct Menu **menu_ret, struct Item **item_ret);
static void drawmenu(void);
static void calcscreengeom(void);
static void calcmenu(struct Menu *menu);
static void setcurrmenu(struct Menu *currmenu_new);
static void parsestdin(void);
static void run(void);
+static void freewindow(struct Menu *menu);
static void cleanupexit(void);
static void usage(void);
if ((item = malloc(sizeof *item)) == NULL)
err(1, "malloc");
- if ((item->label = strdup(label)) == NULL)
- err(1, "strdup");
- if ((item->output = strdup(output)) == NULL)
- err(1, "strdup");
+ if (*label == '\0') {
+ item->label = NULL;
+ item->output = NULL;
+ } else {
+ if ((item->label = strdup(label)) == NULL)
+ err(1, "strdup");
+ if ((item->output = strdup(output)) == NULL)
+ err(1, "strdup");
+ }
item->y = 0;
+ item->h = item->label ? geom.itemh : geom.separator;
+ if (item->label == NULL)
+ item->labellen = 0;
+ else
+ item->labellen = strlen(item->label);
item->next = NULL;
item->submenu = NULL;
swa.background_pixel = dc.decoration[ColorBG];
swa.border_pixel = dc.decoration[ColorFG];
swa.event_mask = ExposureMask | KeyPressMask | ButtonPressMask | ButtonReleaseMask
- | PointerMotionMask;
+ | PointerMotionMask | LeaveWindowMask;
menu->win = XCreateWindow(dpy, rootwin, 0, 0, geom.itemw, geom.itemh, geom.border,
CopyFromParent, CopyFromParent, CopyFromParent,
CWOverrideRedirect | CWBackPixel | CWBorderPixel | CWEventMask,
calcmenu(struct Menu *menu)
{
XWindowChanges changes;
+ XSizeHints sizeh;
struct Item *item;
+ int labelwidth;
- /* calculate items positions and menu height */
+ /* calculate items positions and menu width and height */
+ menu->w = geom.itemw;
for (item = menu->list; item != NULL; item = item->next) {
item->y = menu->h;
- if (*item->label == '\0') /* height for separator item */
+ if (item->label == NULL) /* height for separator item */
menu->h += geom.separator;
else
menu->h += geom.itemh;
+
+ labelwidth = XTextWidth(dc.font, item->label, item->labellen) + dc.fonth * 2;
+ menu->w = MAX(menu->w, labelwidth);
}
/* calculate menu's x and y positions */
/* update menu geometry */
changes.height = menu->h;
+ changes.width = menu->w;
changes.x = menu->x;
changes.y = menu->y;
- XConfigureWindow(dpy, menu->win, CWHeight | CWX | CWY, &changes);
+ XConfigureWindow(dpy, menu->win, CWWidth | CWHeight | CWX | CWY, &changes);
+
+ /* set window manager size hints */
+ sizeh.flags = PMaxSize | PMinSize;
+ sizeh.min_width = sizeh.max_width = menu->w;
+ sizeh.min_height = sizeh.max_height = menu->h;
+ XSetWMNormalHints(dpy, menu->win, &sizeh);
+
+ /* create pixmap */
+ menu->pixmap = XCreatePixmap(dpy, menu->win, menu->w, menu->h,
+ DefaultDepth(dpy, screen));
/* calculate positions of submenus */
for (item = menu->list; item != NULL; item = item->next) {
for (menu = currmenu; menu != NULL; menu = menu->parent) {
if (menu->win == win) {
for (item = menu->list; item != NULL; item = item->next) {
- if (y >= item->y && y <= item->y + geom.itemh) {
+ if (y >= item->y && y <= item->y + item->h) {
goto done;
}
}
struct Item *item;
for (menu = currmenu; menu != NULL; menu = menu->parent) {
- size_t nitems; /* number of items before current item */
-
- nitems = 0;
for (item = menu->list; item != NULL; item = item->next) {
unsigned long *color;
- size_t labellen;
int labelx, labely;
- int y;
/* determine item color */
- if (item == menu->selected)
+ if (item->label == NULL)
+ color = dc.decoration;
+ else if (item == menu->selected)
color = dc.pressed;
else
color = dc.unpressed;
- /* calculate item's y position */
- y = nitems * geom.itemh;
-
/* draw item box */
XSetForeground(dpy, dc.gc, color[ColorBG]);
- XFillRectangle(dpy, menu->win, dc.gc, 0, y,
- geom.itemw, geom.itemh);
+ XFillRectangle(dpy, menu->pixmap, dc.gc, 0, item->y,
+ menu->w, item->h);
+
+ /* continue if item is a separator */
+ if (item->label == NULL)
+ continue;
/* draw item label */
- labellen = strlen(item->label);
labelx = 0 + dc.fonth;
- labely = y + dc.fonth + geom.itemb;
+ labely = item->y + dc.fonth + geom.itemb;
XSetForeground(dpy, dc.gc, color[ColorFG]);
- XDrawString(dpy, menu->win, dc.gc, labelx, labely, item->label, labellen);
+ XDrawString(dpy, menu->pixmap, dc.gc, labelx, labely,
+ item->label, item->labellen);
/* draw triangle, if item contains a submenu */
if (item->submenu != NULL) {
- int trianglex = geom.itemw - (geom.itemb + dc.fonth);
- int triangley = y + geom.itemb;
+ int trianglex = menu->w - dc.fonth + geom.itemb - 1;
+ int triangley = item->y + (3 * item->h)/8 -1;
XPoint triangle[] = {
{trianglex, triangley},
- {trianglex + dc.fonth, triangley + dc.fonth/2},
- {trianglex, triangley + dc.fonth},
+ {trianglex + item->h/8 + 1, item->y + item->h/2},
+ {trianglex, triangley + item->h/4 + 2},
{trianglex, triangley}
};
- XFillPolygon(dpy, menu->win, dc.gc, triangle, LEN(triangle),
+ XFillPolygon(dpy, menu->pixmap, dc.gc, triangle, LEN(triangle),
Convex, CoordModeOrigin);
}
- nitems++;
+ XCopyArea(dpy, menu->pixmap, menu->win, dc.gc, 0, item->y,
+ menu->w, item->h, 0, item->y);
}
}
}
previtem = item;
} else if (menu->selected != item)
menu->selected = item;
+ drawmenu();
}
- drawmenu();
break;
case ButtonRelease:
getmenuitem(ev.xbutton.window, ev.xbutton.y, &menu, &item);
if (menu != NULL && item != NULL) {
+ if (item->label == NULL)
+ break; /* ignore separators */
if (item->submenu != NULL) {
setcurrmenu(item->submenu);
} else {
cleanupexit();
}
break;
+ case LeaveNotify:
+ currmenu->selected = NULL;
+ drawmenu();
+ break;
}
}
}
+/* recursivelly free a pixmap */
+static void
+freewindow(struct Menu *menu)
+{
+ struct Item *item;
+
+ for (item = menu->list; item != NULL; item = item->next)
+ if (item->submenu != NULL)
+ freewindow(item->submenu);
+
+ XFreePixmap(dpy, menu->pixmap);
+ XDestroyWindow(dpy, menu->win);
+}
+
/* cleanup and exit */
static void
cleanupexit(void)
{
+ freewindow(rootmenu);
+ XFreeFont(dpy, dc.font);
+ XFreeGC(dpy, dc.gc);
XCloseDisplay(dpy);
exit(0);
}