Renaming routine and spliting it into subroutines
Renaming calcmenu() to setupmenu() and shrinking it by calling two new functions: setupmenusize() and setupmenupos().
This commit is contained in:
parent
0dfd6fa00e
commit
f8ffe0b2e4
50
xmenu.c
50
xmenu.c
|
@ -75,7 +75,9 @@ 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 *allocmenu(struct Menu *parent, struct Item *list, unsigned level);
|
||||||
static struct Menu *buildmenutree(unsigned level, const char *label, const char *output);
|
static struct Menu *buildmenutree(unsigned level, const char *label, const char *output);
|
||||||
static struct Menu *parsestdin(void);
|
static struct Menu *parsestdin(void);
|
||||||
static void calcmenu(struct Geometry *geom, struct Menu *menu);
|
static void setupmenusize(struct Geometry *geom, struct Menu *menu);
|
||||||
|
static void setupmenupos(struct Geometry *geom, struct Menu *menu);
|
||||||
|
static void setupmenu(struct Geometry *geom, struct Menu *menu);
|
||||||
static void grabpointer(void);
|
static void grabpointer(void);
|
||||||
static void grabkeyboard(void);
|
static void grabkeyboard(void);
|
||||||
static struct Menu *getmenu(struct Menu *currmenu, Window win);
|
static struct Menu *getmenu(struct Menu *currmenu, Window win);
|
||||||
|
@ -134,11 +136,11 @@ main(int argc, char *argv[])
|
||||||
setupdc();
|
setupdc();
|
||||||
calcgeom(&geom);
|
calcgeom(&geom);
|
||||||
|
|
||||||
/* generate menus and recalculate them */
|
/* generate menus and set them up */
|
||||||
rootmenu = parsestdin();
|
rootmenu = parsestdin();
|
||||||
if (rootmenu == NULL)
|
if (rootmenu == NULL)
|
||||||
errx(1, "no menu generated");
|
errx(1, "no menu generated");
|
||||||
calcmenu(&geom, rootmenu);
|
setupmenu(&geom, rootmenu);
|
||||||
|
|
||||||
/* grab mouse and keyboard */
|
/* grab mouse and keyboard */
|
||||||
grabpointer();
|
grabpointer();
|
||||||
|
@ -291,10 +293,10 @@ allocmenu(struct Menu *parent, struct Item *list, unsigned level)
|
||||||
menu->list = list;
|
menu->list = list;
|
||||||
menu->caller = NULL;
|
menu->caller = NULL;
|
||||||
menu->selected = NULL;
|
menu->selected = NULL;
|
||||||
menu->w = 0; /* calculated by calcmenu() */
|
menu->w = 0; /* calculated by setupmenu() */
|
||||||
menu->h = 0; /* calculated by calcmenu() */
|
menu->h = 0; /* calculated by setupmenu() */
|
||||||
menu->x = 0; /* calculated by calcmenu() */
|
menu->x = 0; /* calculated by setupmenu() */
|
||||||
menu->y = 0; /* calculated by calcmenu() */
|
menu->y = 0; /* calculated by setupmenu() */
|
||||||
menu->level = level;
|
menu->level = level;
|
||||||
|
|
||||||
swa.override_redirect = True;
|
swa.override_redirect = True;
|
||||||
|
@ -405,19 +407,14 @@ parsestdin(void)
|
||||||
return rootmenu;
|
return rootmenu;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* recursivelly calculate menu geometry and set window hints */
|
/* setup the size of a menu and the position of its items */
|
||||||
static void
|
static void
|
||||||
calcmenu(struct Geometry *geom, struct Menu *menu)
|
setupmenusize(struct Geometry *geom, struct Menu *menu)
|
||||||
{
|
{
|
||||||
static XClassHint classh = {PROGNAME, PROGNAME};
|
|
||||||
XWindowChanges changes;
|
|
||||||
XSizeHints sizeh;
|
|
||||||
XGlyphInfo ext;
|
XGlyphInfo ext;
|
||||||
struct Item *item;
|
struct Item *item;
|
||||||
int labelwidth;
|
int labelwidth;
|
||||||
int width, height;
|
|
||||||
|
|
||||||
/* calculate items positions and menu width and height */
|
|
||||||
menu->w = geom->itemw;
|
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;
|
||||||
|
@ -428,13 +425,20 @@ calcmenu(struct Geometry *geom, struct Menu *menu)
|
||||||
item->h = geom->itemh;
|
item->h = geom->itemh;
|
||||||
menu->h += item->h;
|
menu->h += item->h;
|
||||||
|
|
||||||
|
/* get length of item->label rendered in the font */
|
||||||
XftTextExtentsUtf8(dpy, dc.font, (XftChar8 *)item->label,
|
XftTextExtentsUtf8(dpy, dc.font, (XftChar8 *)item->label,
|
||||||
item->labellen, &ext);
|
item->labellen, &ext);
|
||||||
labelwidth = ext.xOff + dc.font->height * 2;
|
labelwidth = ext.xOff + dc.font->height * 2;
|
||||||
menu->w = MAX(menu->w, labelwidth);
|
menu->w = MAX(menu->w, labelwidth);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/* setup the position of a menu */
|
||||||
|
static void
|
||||||
|
setupmenupos(struct Geometry *geom, struct Menu *menu)
|
||||||
|
{
|
||||||
|
int width, height;
|
||||||
|
|
||||||
/* calculate menu's x and y positions */
|
|
||||||
width = menu->w + geom->border * 2;
|
width = menu->w + geom->border * 2;
|
||||||
height = menu->h + 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 */
|
||||||
|
@ -460,6 +464,20 @@ calcmenu(struct Geometry *geom, struct Menu *menu)
|
||||||
else if (geom->screenh > height)
|
else if (geom->screenh > height)
|
||||||
menu->y = geom->screenh - height;
|
menu->y = geom->screenh - height;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/* recursivelly setup menu configuration and its pixmap */
|
||||||
|
static void
|
||||||
|
setupmenu(struct Geometry *geom, struct Menu *menu)
|
||||||
|
{
|
||||||
|
struct Item *item;
|
||||||
|
static XClassHint classh = {PROGNAME, PROGNAME};
|
||||||
|
XWindowChanges changes;
|
||||||
|
XSizeHints sizeh;
|
||||||
|
|
||||||
|
/* setup size and position of menus */
|
||||||
|
setupmenusize(geom, menu);
|
||||||
|
setupmenupos(geom, menu);
|
||||||
|
|
||||||
/* update menu geometry */
|
/* update menu geometry */
|
||||||
changes.border_width = geom->border;
|
changes.border_width = geom->border;
|
||||||
|
@ -484,7 +502,7 @@ calcmenu(struct Geometry *geom, struct Menu *menu)
|
||||||
/* calculate positions of submenus */
|
/* calculate positions of submenus */
|
||||||
for (item = menu->list; item != NULL; item = item->next) {
|
for (item = menu->list; item != NULL; item = item->next) {
|
||||||
if (item->submenu != NULL)
|
if (item->submenu != NULL)
|
||||||
calcmenu(geom, item->submenu);
|
setupmenu(geom, item->submenu);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user