width is now calculated from the font

This commit is contained in:
phillbush 2020-05-16 01:24:43 -03:00
parent 2b6968f911
commit f158328539

25
xmenu.c
View File

@ -8,6 +8,7 @@
/* macros */ /* macros */
#define LEN(x) (sizeof (x) / sizeof (x[0])) #define LEN(x) (sizeof (x) / sizeof (x[0]))
#define MAX(x,y) ((x)>(y)?(x):(y))
/* color enum */ /* color enum */
enum {ColorFG, ColorBG, ColorLast}; enum {ColorFG, ColorBG, ColorLast};
@ -45,6 +46,7 @@ struct Item {
char *output; char *output;
int y; int y;
int h; int h;
size_t labellen;
struct Item *next; struct Item *next;
struct Menu *submenu; struct Menu *submenu;
}; };
@ -212,6 +214,10 @@ allocitem(const char *label, const char *output)
} }
item->y = 0; item->y = 0;
item->h = item->label ? geom.itemh : geom.separator; 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->next = NULL;
item->submenu = NULL; item->submenu = NULL;
@ -348,14 +354,19 @@ calcmenu(struct Menu *menu)
XWindowChanges changes; XWindowChanges changes;
XSizeHints sizeh; XSizeHints sizeh;
struct Item *item; 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) { for (item = menu->list; item != NULL; item = item->next) {
item->y = menu->h; item->y = menu->h;
if (item->label == NULL) /* height for separator item */ if (item->label == NULL) /* height for separator item */
menu->h += geom.separator; menu->h += geom.separator;
else else
menu->h += geom.itemh; 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 */ /* calculate menu's x and y positions */
@ -390,9 +401,10 @@ calcmenu(struct Menu *menu)
/* update menu geometry */ /* update menu geometry */
changes.height = menu->h; changes.height = menu->h;
changes.width = menu->w;
changes.x = menu->x; changes.x = menu->x;
changes.y = menu->y; 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 */ /* set window manager size hints */
sizeh.flags = PMaxSize | PMinSize; sizeh.flags = PMaxSize | PMinSize;
@ -464,7 +476,6 @@ drawmenu(void)
for (menu = currmenu; menu != NULL; menu = menu->parent) { for (menu = currmenu; menu != NULL; menu = menu->parent) {
for (item = menu->list; item != NULL; item = item->next) { for (item = menu->list; item != NULL; item = item->next) {
unsigned long *color; unsigned long *color;
size_t labellen;
int labelx, labely; int labelx, labely;
/* determine item color */ /* determine item color */
@ -478,22 +489,22 @@ drawmenu(void)
/* draw item box */ /* draw item box */
XSetForeground(dpy, dc.gc, color[ColorBG]); XSetForeground(dpy, dc.gc, color[ColorBG]);
XFillRectangle(dpy, menu->pixmap, dc.gc, 0, item->y, XFillRectangle(dpy, menu->pixmap, dc.gc, 0, item->y,
geom.itemw, item->h); menu->w, item->h);
/* continue if item is a separator */ /* continue if item is a separator */
if (item->label == NULL) if (item->label == NULL)
continue; continue;
/* draw item label */ /* draw item label */
labellen = strlen(item->label);
labelx = 0 + dc.fonth; labelx = 0 + dc.fonth;
labely = item->y + dc.fonth + geom.itemb; labely = item->y + dc.fonth + geom.itemb;
XSetForeground(dpy, dc.gc, color[ColorFG]); XSetForeground(dpy, dc.gc, color[ColorFG]);
XDrawString(dpy, menu->pixmap, 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 */ /* draw triangle, if item contains a submenu */
if (item->submenu != NULL) { if (item->submenu != NULL) {
int trianglex = geom.itemw - dc.fonth + geom.itemb - 1; int trianglex = menu->w - dc.fonth + geom.itemb - 1;
int triangley = item->y + (3 * item->h)/8 -1; int triangley = item->y + (3 * item->h)/8 -1;
XPoint triangle[] = { XPoint triangle[] = {