I moved all the geometry related code to calcmenu().
The pro is that geom is now a local variable rather than global;
and that all geometry-related code is now in one place.
The con is that it weakens performance a little bit since some
variables, such as the border, have to be reset after the window
creation.
static void getresources(void);
static void getcolor(const char *s, XftColor *color);
static void setupdc(void);
static void getresources(void);
static void getcolor(const char *s, XftColor *color);
static void setupdc(void);
-static void calcgeom(void);
+static void calcgeom(struct Geometry *geom);
static struct Item *allocitem(const char *label, const char *output);
static struct Menu *allocmenu(struct Menu *parent, struct Item *list, unsigned level);
static struct Menu * buildmenutree(unsigned level, const char *label, const char *output);
static struct Menu *parsestdin(void);
static struct Item *allocitem(const char *label, const char *output);
static struct Menu *allocmenu(struct Menu *parent, struct Item *list, unsigned level);
static struct Menu * buildmenutree(unsigned level, const char *label, const char *output);
static struct Menu *parsestdin(void);
-static void calcmenu(struct Menu *menu);
+static void calcmenu(struct Geometry *geom, struct Menu *menu);
static void grabpointer(void);
static void grabkeyboard(void);
static struct Menu *getmenu(struct Menu *currmenu, Window win);
static void grabpointer(void);
static void grabkeyboard(void);
static struct Menu *getmenu(struct Menu *currmenu, Window win);
static Window rootwin;
static Colormap colormap;
static struct DC dc;
static Window rootwin;
static Colormap colormap;
static struct DC dc;
-static struct Geometry geom;
+/* xmenu: generate menu from stdin and print selected entry to stdout */
int
main(int argc, char *argv[])
{
struct Menu *rootmenu;
int
main(int argc, char *argv[])
{
struct Menu *rootmenu;
int ch;
while ((ch = getopt(argc, argv, "")) != -1) {
int ch;
while ((ch = getopt(argc, argv, "")) != -1) {
/* setup */
getresources();
setupdc();
/* setup */
getresources();
setupdc();
/* generate menus and recalculate them */
rootmenu = parsestdin();
if (rootmenu == NULL)
errx(1, "no menu generated");
/* generate menus and recalculate them */
rootmenu = parsestdin();
if (rootmenu == NULL)
errx(1, "no menu generated");
+ calcmenu(&geom, rootmenu);
/* grab mouse and keyboard */
grabpointer();
/* grab mouse and keyboard */
grabpointer();
/* calculate menu and screen geometry */
static void
/* calculate menu and screen geometry */
static void
+calcgeom(struct Geometry *geom)
{
Window w1, w2; /* unused variables */
int a, b; /* unused variables */
unsigned mask; /* unused variable */
{
Window w1, w2; /* unused variables */
int a, b; /* unused variables */
unsigned mask; /* unused variable */
- XQueryPointer(dpy, rootwin, &w1, &w2, &geom.cursx, &geom.cursy, &a, &b, &mask);
- geom.screenw = DisplayWidth(dpy, screen);
- geom.screenh = DisplayHeight(dpy, screen);
- geom.itemh = dc.font->height + padding_pixels * 2;
- geom.itemw = width_pixels;
- geom.border = border_pixels;
- geom.separator = separator_pixels;
+ XQueryPointer(dpy, rootwin, &w1, &w2, &geom->cursx, &geom->cursy, &a, &b, &mask);
+ geom->screenw = DisplayWidth(dpy, screen);
+ geom->screenh = DisplayHeight(dpy, screen);
+ geom->itemh = dc.font->height + padding_pixels * 2;
+ geom->itemw = width_pixels;
+ geom->border = border_pixels;
+ geom->separator = separator_pixels;
- item->h = item->label ? geom.itemh : geom.separator;
if (item->label == NULL)
item->labellen = 0;
else
if (item->label == NULL)
item->labellen = 0;
else
menu->list = list;
menu->caller = NULL;
menu->selected = NULL;
menu->list = list;
menu->caller = NULL;
menu->selected = NULL;
+ menu->w = 0; /* calculated by calcmenu() */
menu->h = 0; /* calculated by calcmenu() */
menu->x = 0; /* calculated by calcmenu() */
menu->y = 0; /* calculated by calcmenu() */
menu->h = 0; /* calculated by calcmenu() */
menu->x = 0; /* calculated by calcmenu() */
menu->y = 0; /* calculated by calcmenu() */
swa.save_under = True; /* pop-up windows should save_under*/
swa.event_mask = ExposureMask | KeyPressMask | ButtonPressMask | ButtonReleaseMask
| PointerMotionMask | LeaveWindowMask;
swa.save_under = True; /* pop-up windows should save_under*/
swa.event_mask = ExposureMask | KeyPressMask | ButtonPressMask | ButtonReleaseMask
| PointerMotionMask | LeaveWindowMask;
- menu->win = XCreateWindow(dpy, rootwin, 0, 0, geom.itemw, geom.itemh, geom.border,
+ menu->win = XCreateWindow(dpy, rootwin, 0, 0, 1, 1, 0,
CopyFromParent, CopyFromParent, CopyFromParent,
CWOverrideRedirect | CWBackPixel |
CWBorderPixel | CWEventMask | CWSaveUnder,
CopyFromParent, CopyFromParent, CopyFromParent,
CWOverrideRedirect | CWBackPixel |
CWBorderPixel | CWEventMask | CWSaveUnder,
/* recursivelly calculate menu geometry and set window hints */
static void
/* recursivelly calculate menu geometry and set window hints */
static void
-calcmenu(struct Menu *menu)
+calcmenu(struct Geometry *geom, struct Menu *menu)
{
static XClassHint classh = {PROGNAME, PROGNAME};
XWindowChanges changes;
{
static XClassHint classh = {PROGNAME, PROGNAME};
XWindowChanges changes;
int width, height;
/* calculate items positions and menu width and height */
int width, height;
/* calculate items positions and menu width and height */
for (item = menu->list; item != NULL; item = item->next) {
item->y = menu->h;
for (item = menu->list; item != NULL; item = item->next) {
item->y = menu->h;
if (item->label == NULL) /* height for separator item */
if (item->label == NULL) /* height for separator item */
- menu->h += geom.separator;
+ item->h = geom->separator;
+ item->h = geom->itemh;
+ menu->h += item->h;
XftTextExtentsUtf8(dpy, dc.font, (XftChar8 *)item->label,
item->labellen, &ext);
XftTextExtentsUtf8(dpy, dc.font, (XftChar8 *)item->label,
item->labellen, &ext);
}
/* calculate menu's x and y positions */
}
/* calculate menu's x and y positions */
- width = menu->w + geom.border * 2;
- height = menu->h + geom.border * 2;
+ width = menu->w + geom->border * 2;
+ height = menu->h + geom->border * 2;
if (menu->parent == NULL) { /* if root menu, calculate in respect to cursor */
if (menu->parent == NULL) { /* if root menu, calculate in respect to cursor */
- if (geom.screenw - geom.cursx >= menu->w)
- menu->x = geom.cursx;
- else if (geom.cursx > width)
- menu->x = geom.cursx - width;
-
- if (geom.screenh - geom.cursy >= height)
- menu->y = geom.cursy;
- else if (geom.screenh > height)
- menu->y = geom.screenh - height;
+ if (geom->screenw - geom->cursx >= menu->w)
+ menu->x = geom->cursx;
+ else if (geom->cursx > width)
+ menu->x = geom->cursx - width;
+
+ if (geom->screenh - geom->cursy >= height)
+ menu->y = geom->cursy;
+ else if (geom->screenh > height)
+ menu->y = geom->screenh - height;
} else { /* else, calculate in respect to parent menu */
} else { /* else, calculate in respect to parent menu */
- if (geom.screenw - (menu->parent->x + menu->parent->w + geom.border) >= width)
- menu->x = menu->parent->x + menu->parent->w + geom.border;
- else if (menu->parent->x > menu->w + geom.border)
- menu->x = menu->parent->x - menu->w - geom.border;
+ if (geom->screenw - (menu->parent->x + menu->parent->w + geom->border) >= width)
+ menu->x = menu->parent->x + menu->parent->w + geom->border;
+ else if (menu->parent->x > menu->w + geom->border)
+ menu->x = menu->parent->x - menu->w - geom->border;
- if (geom.screenh - (menu->caller->y + menu->parent->y) > height)
+ if (geom->screenh - (menu->caller->y + menu->parent->y) > height)
menu->y = menu->caller->y + menu->parent->y;
menu->y = menu->caller->y + menu->parent->y;
- else if (geom.screenh - menu->parent->y > height)
+ else if (geom->screenh - menu->parent->y > height)
menu->y = menu->parent->y;
menu->y = menu->parent->y;
- else if (geom.screenh > height)
- menu->y = geom.screenh - height;
+ else if (geom->screenh > height)
+ menu->y = geom->screenh - height;
}
/* update menu geometry */
}
/* update menu geometry */
+ changes.border_width = geom->border;
changes.height = menu->h;
changes.width = menu->w;
changes.x = menu->x;
changes.y = menu->y;
changes.height = menu->h;
changes.width = menu->w;
changes.x = menu->x;
changes.y = menu->y;
- XConfigureWindow(dpy, menu->win, CWWidth | CWHeight | CWX | CWY, &changes);
+ XConfigureWindow(dpy, menu->win, CWBorderWidth | CWWidth | CWHeight | CWX | CWY, &changes);
/* set window manager hints */
sizeh.flags = PMaxSize | PMinSize;
/* set window manager hints */
sizeh.flags = PMaxSize | PMinSize;
/* calculate positions of submenus */
for (item = menu->list; item != NULL; item = item->next) {
if (item->submenu != NULL)
/* calculate positions of submenus */
for (item = menu->list; item != NULL; item = item->next) {
if (item->submenu != NULL)
- calcmenu(item->submenu);
+ calcmenu(geom, item->submenu);