X-Git-Url: https://git.subgeniuskitty.com/xmenu/.git/blobdiff_plain/a80fee227ad7cc2a74b7141870c67b7b1fca7076..f15fc33987e48f42dc42bbdc41992107a7e3faa8:/xmenu.c?ds=inline diff --git a/xmenu.c b/xmenu.c index 71c1149..4bd78cb 100644 --- a/xmenu.c +++ b/xmenu.c @@ -43,7 +43,8 @@ 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; struct Item *next; struct Menu *submenu; }; @@ -55,6 +56,7 @@ struct Menu { struct Item *selected; int x, y, w, h; unsigned level; + Drawable pixmap; Window win; }; @@ -65,14 +67,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 +201,17 @@ 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; item->next = NULL; item->submenu = NULL; @@ -232,7 +240,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,12 +346,13 @@ static void calcmenu(struct Menu *menu) { XWindowChanges changes; + XSizeHints sizeh; struct Item *item; /* calculate items positions and menu height */ 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; @@ -385,6 +394,16 @@ calcmenu(struct Menu *menu) changes.y = menu->y; XConfigureWindow(dpy, menu->win, 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) { if (item->submenu != NULL) @@ -403,7 +422,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,40 +462,39 @@ 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, + geom.itemw, 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, 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 triangley = item->y + geom.itemb; XPoint triangle[] = { {trianglex, triangley}, @@ -485,11 +503,12 @@ drawmenu(void) {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 +540,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 +559,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); }