improved algorithm that draw menus

This commit is contained in:
phillbush 2020-05-16 02:14:29 -03:00
parent f70a900c8e
commit d888f2cae4
2 changed files with 17 additions and 4 deletions

View File

@ -8,4 +8,4 @@
#define ITEMW 130 #define ITEMW 130
#define ITEMB 5 #define ITEMB 5
#define BORDER 2 #define BORDER 2
#define SEPARATOR 2 #define SEPARATOR 1

17
xmenu.c
View File

@ -54,6 +54,7 @@ struct Item {
/* menu structure */ /* menu structure */
struct Menu { struct Menu {
struct Menu *parent; struct Menu *parent;
struct Item *caller;
struct Item *list; struct Item *list;
struct Item *selected; struct Item *selected;
int x, y, w, h; int x, y, w, h;
@ -235,6 +236,7 @@ allocmenu(struct Menu *parent, struct Item *list, unsigned level)
err(1, "malloc"); err(1, "malloc");
menu->parent = parent; menu->parent = parent;
menu->list = list; menu->list = list;
menu->caller = NULL;
menu->selected = NULL; menu->selected = NULL;
menu->w = geom.itemw; menu->w = geom.itemw;
menu->h = 0; /* calculated by calcmenu() */ menu->h = 0; /* calculated by calcmenu() */
@ -327,6 +329,7 @@ parsestdin(void)
; ;
item->submenu = menu; item->submenu = menu;
menu->caller = item;
prevmenu = menu; prevmenu = menu;
} }
@ -452,6 +455,7 @@ static void
setcurrmenu(struct Menu *currmenu_new) setcurrmenu(struct Menu *currmenu_new)
{ {
struct Menu *menu; struct Menu *menu;
struct Item *item;
if (currmenu_new == currmenu) if (currmenu_new == currmenu)
return; return;
@ -462,8 +466,13 @@ setcurrmenu(struct Menu *currmenu_new)
currmenu = currmenu_new; currmenu = currmenu_new;
for (menu = currmenu; menu != NULL; menu = menu->parent) item = NULL;
for (menu = currmenu; menu != NULL; menu = menu->parent) {
XMapWindow(dpy, menu->win); XMapWindow(dpy, menu->win);
if (item != NULL)
menu->selected = item;
item = menu->caller;
}
} }
/* draw items of the current menu and of its ancestors */ /* draw items of the current menu and of its ancestors */
@ -488,6 +497,8 @@ drawmenu(void)
/* draw item box */ /* draw item box */
XSetForeground(dpy, dc.gc, color[ColorBG]); XSetForeground(dpy, dc.gc, color[ColorBG]);
XDrawRectangle(dpy, menu->pixmap, dc.gc, 0, item->y,
menu->w, item->h);
XFillRectangle(dpy, menu->pixmap, dc.gc, 0, item->y, XFillRectangle(dpy, menu->pixmap, dc.gc, 0, item->y,
menu->w, item->h); menu->w, item->h);
@ -549,10 +560,12 @@ run(void)
else else
setcurrmenu(menu); setcurrmenu(menu);
previtem = item; previtem = item;
} else if (menu->selected != item) drawmenu();
} else if (menu->selected != item) {
menu->selected = item; menu->selected = item;
drawmenu(); drawmenu();
} }
}
break; break;
case ButtonRelease: case ButtonRelease:
getmenuitem(ev.xbutton.window, ev.xbutton.y, &menu, &item); getmenuitem(ev.xbutton.window, ev.xbutton.y, &menu, &item);