X-Git-Url: https://git.subgeniuskitty.com/xmenu/.git/blobdiff_plain/a80fee227ad7cc2a74b7141870c67b7b1fca7076..f70a900c8e9264aa5735ee03a605442b7d9b1035:/xmenu.c diff --git a/xmenu.c b/xmenu.c index 71c1149..a56091b 100644 --- a/xmenu.c +++ b/xmenu.c @@ -8,6 +8,7 @@ /* macros */ #define LEN(x) (sizeof (x) / sizeof (x[0])) +#define MAX(x,y) ((x)>(y)?(x):(y)) /* color enum */ enum {ColorFG, ColorBG, ColorLast}; @@ -43,7 +44,9 @@ struct ScreenGeometry { 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; }; @@ -55,6 +58,7 @@ struct Menu { struct Item *selected; int x, y, w, h; unsigned level; + Drawable pixmap; Window win; }; @@ -65,14 +69,14 @@ static void setupgeom(void); 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); @@ -199,11 +203,21 @@ allocitem(const char *label, const char *output) 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; @@ -232,7 +246,7 @@ allocmenu(struct Menu *parent, struct Item *list, unsigned level) 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, @@ -338,15 +352,21 @@ static void 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 */ @@ -381,9 +401,20 @@ calcmenu(struct Menu *menu) /* 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) { @@ -403,7 +434,7 @@ getmenuitem(Window win, int y, 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; } } @@ -443,53 +474,52 @@ drawmenu(void) 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); } } } @@ -521,12 +551,14 @@ run(void) 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 { @@ -538,14 +570,35 @@ run(void) 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); }