X-Git-Url: https://git.subgeniuskitty.com/xmenu/.git/blobdiff_plain/09c13122cc2b473246f0239700cf3569d1faf8c6..f70a900c8e9264aa5735ee03a605442b7d9b1035:/xmenu.c diff --git a/xmenu.c b/xmenu.c index d02620a..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}; @@ -45,6 +46,7 @@ struct Item { char *output; int y; int h; + size_t labellen; struct Item *next; struct Menu *submenu; }; @@ -56,6 +58,7 @@ struct Menu { struct Item *selected; int x, y, w, h; unsigned level; + Drawable pixmap; Window win; }; @@ -66,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); @@ -211,6 +214,10 @@ allocitem(const char *label, const char *output) } 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; @@ -239,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, @@ -347,14 +354,19 @@ 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 == 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 */ @@ -389,9 +401,10 @@ 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; @@ -399,6 +412,10 @@ calcmenu(struct Menu *menu) 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) { if (item->submenu != NULL) @@ -459,7 +476,6 @@ drawmenu(void) for (menu = currmenu; menu != NULL; menu = menu->parent) { for (item = menu->list; item != NULL; item = item->next) { unsigned long *color; - size_t labellen; int labelx, labely; /* determine item color */ @@ -472,35 +488,38 @@ drawmenu(void) /* draw item box */ XSetForeground(dpy, dc.gc, color[ColorBG]); - XFillRectangle(dpy, menu->win, dc.gc, 0, item->y, - geom.itemw, item->h); + 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 = 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 = item->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); } + + XCopyArea(dpy, menu->pixmap, menu->win, dc.gc, 0, item->y, + menu->w, item->h, 0, item->y); } } } @@ -532,8 +551,8 @@ 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); @@ -551,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); }