X-Git-Url: https://git.subgeniuskitty.com/xmenu/.git/blobdiff_plain/a77326901f6923610c36bab6abda3ffca4ed8d3e..f70a900c8e9264aa5735ee03a605442b7d9b1035:/xmenu.c diff --git a/xmenu.c b/xmenu.c index aa6bab8..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}; @@ -29,7 +30,8 @@ struct Geometry { int itemb; /* item border */ int itemw; /* item width */ int itemh; /* item height */ - int border; /* window border */ + int border; /* window border width */ + int separator; /* menu separator width */ }; /* screen geometry structure */ @@ -42,7 +44,9 @@ struct ScreenGeometry { struct Item { char *label; char *output; - int x, y; + int y; + int h; + size_t labellen; struct Item *next; struct Menu *submenu; }; @@ -54,7 +58,7 @@ struct Menu { struct Item *selected; int x, y, w, h; unsigned level; - unsigned nitems; + Drawable pixmap; Window win; }; @@ -63,16 +67,16 @@ static unsigned long getcolor(const char *s); static void setupdc(void); static void setupgeom(void); static void setupgrab(void); -static struct Item *allocitem(size_t count, const char *label, const char *output); +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 x, 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); @@ -89,7 +93,7 @@ static struct Menu *currmenu = NULL; /* geometry variables */ static struct Geometry geom; -static struct ScreenGeometry sgeom; +static struct ScreenGeometry screengeom; /* flag variables */ static Bool override_redirect = True; @@ -180,6 +184,7 @@ setupgeom(void) geom.itemh = dc.fonth + ITEMB * 2; geom.itemw = ITEMW; geom.border = BORDER; + geom.separator = SEPARATOR; } /* grab pointer */ @@ -192,18 +197,27 @@ setupgrab(void) /* allocate an item */ static struct Item * -allocitem(size_t count, const char *label, const char *output) +allocitem(const char *label, const char *output) { struct Item *item; 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"); - item->x = 0; - item->y = count * geom.itemh; + 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; @@ -222,18 +236,17 @@ allocmenu(struct Menu *parent, struct Item *list, unsigned level) menu->parent = parent; menu->list = list; menu->selected = NULL; - menu->x = 0; - menu->y = 0; menu->w = geom.itemw; - menu->h = geom.itemh; + menu->h = 0; /* calculated by calcmenu() */ + menu->x = 0; /* calculated by calcmenu() */ + menu->y = 0; /* calculated by calcmenu() */ menu->level = level; - menu->nitems = 0; swa.override_redirect = override_redirect; 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, @@ -250,9 +263,10 @@ parsestdin(void) char *label, *output; unsigned level = 0; unsigned i; - struct Item *item, *p; - struct Menu *menu; - struct Menu *prevmenu = NULL; + struct Item *curritem = NULL; /* item currently being read */ + struct Menu *prevmenu = NULL; /* menu the previous item was added to */ + struct Item *item; /* dummy item for for loops */ + struct Menu *menu; /* dummy menu for for loops */ size_t count = 0; /* number of items in the current menu */ while (fgets(buf, BUFSIZ, stdin) != NULL) { @@ -281,10 +295,10 @@ parsestdin(void) if (*s == '\n') *s = '\0'; - item = allocitem(count, label, output); + curritem = allocitem(label, output); if (prevmenu == NULL) { /* there is no menu yet */ - menu = allocmenu(NULL, item, level); + menu = allocmenu(NULL, curritem, level); rootmenu = menu; prevmenu = menu; count = 1; @@ -297,25 +311,26 @@ parsestdin(void) if (menu == NULL) errx(1, "reached NULL menu"); - for (p = menu->list; p->next != NULL; p = p->next) + for (item = menu->list; item->next != NULL; item = item->next) ; - p->next = item; + item->next = curritem; prevmenu = menu; } else if (level == prevmenu->level) { /* item is a continuation of current menu */ - for (p = prevmenu->list; p->next != NULL; p = p->next) + for (item = prevmenu->list; item->next != NULL; item = item->next) ; - p->next = item; + item->next = curritem; } else if (level > prevmenu->level) { /* item begins a new menu */ - menu = allocmenu(prevmenu, item, level); + menu = allocmenu(prevmenu, curritem, level); - for (p = prevmenu->list; p->next != NULL; p = p->next) + for (item = prevmenu->list; item->next != NULL; item = item->next) ; - p->submenu = menu; + item->submenu = menu; prevmenu = menu; } + count++; } } @@ -327,9 +342,9 @@ calcscreengeom(void) int a, b; /* unused variables */ unsigned mask; /* unused variable */ - XQueryPointer(dpy, rootwin, &w1, &w2, &sgeom.cursx, &sgeom.cursy, &a, &b, &mask); - sgeom.screenw = DisplayWidth(dpy, screen); - sgeom.screenh = DisplayHeight(dpy, screen); + XQueryPointer(dpy, rootwin, &w1, &w2, &screengeom.cursx, &screengeom.cursy, &a, &b, &mask); + screengeom.screenw = DisplayWidth(dpy, screen); + screengeom.screenh = DisplayHeight(dpy, screen); } /* recursivelly calculate height and position of the menus */ @@ -337,58 +352,71 @@ static void calcmenu(struct Menu *menu) { XWindowChanges changes; - struct Item *item, *p; - size_t i; + XSizeHints sizeh; + struct Item *item; + int labelwidth; - /* calculate number of items */ - i = 0; - for (item = menu->list; item != NULL; item = item->next) - i++; - menu->nitems = i; - menu->h = geom.itemh * i; + /* 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 */ if (menu->parent == NULL) { /* if root menu, calculate in respect to cursor */ - if (sgeom.screenw - sgeom.cursx >= menu->w) - menu->x = sgeom.cursx; - else if (sgeom.cursx > menu->w) - menu->x = sgeom.cursx - menu->w; - - if (sgeom.screenh - sgeom.cursy >= menu->h) - menu->y = sgeom.cursy; - else if (sgeom.screenh > menu->h) - menu->y = sgeom.screenh - menu->h; + if (screengeom.screenw - screengeom.cursx >= menu->w) + menu->x = screengeom.cursx; + else if (screengeom.cursx > menu->w) + menu->x = screengeom.cursx - menu->w; + + if (screengeom.screenh - screengeom.cursy >= menu->h) + menu->y = screengeom.cursy; + else if (screengeom.screenh > menu->h) + menu->y = screengeom.screenh - menu->h; } else { /* else, calculate in respect to parent menu */ /* search for the item in parent menu that generates this menu */ - for (p = menu->parent->list; p->submenu != menu; p = p->next) + for (item = menu->parent->list; item->submenu != menu; item = item->next) ; - if (sgeom.screenw - (menu->parent->x + menu->parent->w) >= menu->w) + if (screengeom.screenw - (menu->parent->x + menu->parent->w) >= menu->w) menu->x = menu->parent->x + menu->parent->w; else if (menu->parent->x > menu->w) menu->x = menu->parent->x - menu->w; - if (sgeom.screenh - p->y > menu->h) - menu->y = p->y; - else if (sgeom.screenh - menu->parent->y > menu->h) + if (screengeom.screenh - (item->y + menu->parent->y) > menu->h) + menu->y = item->y + menu->parent->y; + else if (screengeom.screenh - menu->parent->y > menu->h) menu->y = menu->parent->y; - else if (sgeom.screenh > menu->h) - menu->y = sgeom.screenh - menu->h; - } - - /* calculate position of each item in the menu */ - for (i = 0, item = menu->list; item != NULL; item = item->next, i++) { - item->x = menu->x; - item->y = menu->y + i * geom.itemh; + else if (screengeom.screenh > menu->h) + menu->y = screengeom.screenh - menu->h; } /* 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) { if (item->submenu != NULL) calcmenu(item->submenu); @@ -397,7 +425,7 @@ calcmenu(struct Menu *menu) /* get menu and item of given window and position */ static void -getmenuitem(Window win, int x, int y, +getmenuitem(Window win, int y, struct Menu **menu_ret, struct Item **item_ret) { struct Menu *menu = NULL; @@ -406,8 +434,7 @@ getmenuitem(Window win, int x, int y, for (menu = currmenu; menu != NULL; menu = menu->parent) { if (menu->win == win) { for (item = menu->list; item != NULL; item = item->next) { - if (x >= item->x && x <= item->x + geom.itemw && - y >= item->y && y <= item->y + geom.itemh) { + if (y >= item->y && y <= item->y + item->h) { goto done; } } @@ -447,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); } } } @@ -515,8 +541,7 @@ run(void) drawmenu(); break; case MotionNotify: - getmenuitem(ev.xbutton.window, ev.xbutton.x_root, ev.xbutton.y_root, - &menu, &item); + getmenuitem(ev.xbutton.window, ev.xbutton.y, &menu, &item); if (menu != NULL && item != NULL) { if (previtem != item) { if (item->submenu != NULL) @@ -526,13 +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.x_root, ev.xbutton.y_root, - &menu, &item); + 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 { @@ -544,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); }