config.h
[xmenu] / xmenu.c
diff --git a/xmenu.c b/xmenu.c
index a56091b..41303cc 100644 (file)
--- a/xmenu.c
+++ b/xmenu.c
@@ -9,6 +9,7 @@
 /* macros */
 #define LEN(x) (sizeof (x) / sizeof (x[0]))
 #define MAX(x,y) ((x)>(y)?(x):(y))
 /* macros */
 #define LEN(x) (sizeof (x) / sizeof (x[0]))
 #define MAX(x,y) ((x)>(y)?(x):(y))
+#define MIN(x,y) ((x)<(y)?(x):(y))
 
 /* color enum */
 enum {ColorFG, ColorBG, ColorLast};
 
 /* color enum */
 enum {ColorFG, ColorBG, ColorLast};
@@ -42,24 +43,25 @@ struct ScreenGeometry {
 
 /* menu item structure */
 struct Item {
 
 /* menu item structure */
 struct Item {
-       char *label;
-       char *output;
-       int y;
-       int h;
-       size_t labellen;
-       struct Item *next;
-       struct Menu *submenu;
+       char *label;            /* string to be drawed on menu */
+       char *output;           /* string to be outputed when item is clicked */
+       int y;                  /* item y position relative to menu */
+       int h;                  /* item height */
+       size_t labellen;        /* strlen(label) */
+       struct Item *next;      /* next item */
+       struct Menu *submenu;   /* submenu spawned by clicking on item */
 };
 
 /* menu structure */
 struct Menu {
 };
 
 /* menu structure */
 struct Menu {
-       struct Menu *parent;
-       struct Item *list;
-       struct Item *selected;
-       int x, y, w, h;
-       unsigned level;
-       Drawable pixmap;
-       Window win;
+       struct Menu *parent;    /* parent menu */
+       struct Item *caller;    /* item that spawned the menu */
+       struct Item *list;      /* list of items contained by the menu */
+       struct Item *selected;  /* item currently selected in the menu */
+       int x, y, w, h;         /* menu geometry */
+       unsigned level;         /* menu level relative to root */
+       Drawable pixmap;        /* pixmap to draw the menu on */
+       Window win;             /* menu window to map on the screen */
 };
 
 /* function declarations */
 };
 
 /* function declarations */
@@ -77,7 +79,7 @@ static void setcurrmenu(struct Menu *currmenu_new);
 static void parsestdin(void);
 static void run(void);
 static void freewindow(struct Menu *menu);
 static void parsestdin(void);
 static void run(void);
 static void freewindow(struct Menu *menu);
-static void cleanupexit(void);
+static void cleanup(void);
 static void usage(void);
 
 /* X variables */
 static void usage(void);
 
 /* X variables */
@@ -137,10 +139,16 @@ main(int argc, char *argv[])
        calcscreengeom();
        calcmenu(rootmenu);
 
        calcscreengeom();
        calcmenu(rootmenu);
 
+       /* map root menu */
+       currmenu = rootmenu;
+       XMapWindow(dpy, rootmenu->win);
+
        /* run event loop */
        run();
 
        /* run event loop */
        run();
 
-       return 1;   /* UNREACHABLE */
+       cleanup();
+
+       return 0;
 }
 
 /* get color from color string */
 }
 
 /* get color from color string */
@@ -235,6 +243,7 @@ allocmenu(struct Menu *parent, struct Item *list, unsigned level)
                err(1, "malloc");
        menu->parent = parent;
        menu->list = list;
                err(1, "malloc");
        menu->parent = parent;
        menu->list = list;
+       menu->caller = NULL;
        menu->selected = NULL;
        menu->w = geom.itemw;
        menu->h = 0;    /* calculated by calcmenu() */
        menu->selected = NULL;
        menu->w = geom.itemw;
        menu->h = 0;    /* calculated by calcmenu() */
@@ -327,6 +336,7 @@ parsestdin(void)
                                ;
 
                        item->submenu = menu;
                                ;
 
                        item->submenu = menu;
+                       menu->caller = item;
 
                        prevmenu = menu;
                }
 
                        prevmenu = menu;
                }
@@ -451,19 +461,52 @@ done:
 static void
 setcurrmenu(struct Menu *currmenu_new)
 {
 static void
 setcurrmenu(struct Menu *currmenu_new)
 {
-       struct Menu *menu;
+       struct Menu *menu, *menu_;
+       struct Item *item;
+       struct Menu *lcamenu;   /* lowest common ancestor menu */
+       unsigned minlevel;      /* level of the closest to root menu */
+       unsigned maxlevel;      /* level of the closest to root menu */
 
        if (currmenu_new == currmenu)
                return;
 
 
        if (currmenu_new == currmenu)
                return;
 
-       for (menu = currmenu; menu != NULL; menu = menu->parent) {
+       /* find lowest common ancestor menu */
+       lcamenu = rootmenu;
+       if (currmenu != NULL) {
+               minlevel = MIN(currmenu_new->level, currmenu->level);
+               maxlevel = MAX(currmenu_new->level, currmenu->level);
+               if (currmenu_new->level == maxlevel) {
+                       menu = currmenu_new;
+                       menu_ = currmenu;
+               } else {
+                       menu = currmenu;
+                       menu_ = currmenu_new;
+               }
+               while (menu->level > minlevel)
+                       menu = menu->parent;
+
+               while (menu != menu_) {
+                       menu = menu->parent;
+                       menu_ = menu_->parent;
+               }
+               lcamenu = menu;
+       }
+
+       /* unmap menus from currmenu (inclusive) until lcamenu (exclusive) */
+       for (menu = currmenu; menu != lcamenu; menu = menu->parent) {
                XUnmapWindow(dpy, menu->win);
        }
 
        currmenu = currmenu_new;
 
                XUnmapWindow(dpy, menu->win);
        }
 
        currmenu = currmenu_new;
 
-       for (menu = currmenu; menu != NULL; menu = menu->parent)
+       /* map menus from currmenu (inclusive) until lcamenu (exclusive) */
+       item = NULL;
+       for (menu = currmenu; menu != lcamenu; 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 +531,8 @@ drawmenu(void)
 
                        /* draw item box */
                        XSetForeground(dpy, dc.gc, color[ColorBG]);
 
                        /* draw item box */
                        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,
                                       menu->w, item->h);
 
                        XFillRectangle(dpy, menu->pixmap, dc.gc, 0, item->y,
                                       menu->w, item->h);
 
@@ -533,8 +578,6 @@ run(void)
        struct Item *previtem = NULL;
        XEvent ev;
 
        struct Item *previtem = NULL;
        XEvent ev;
 
-       setcurrmenu(rootmenu);
-
        while (!XNextEvent(dpy, &ev)) {
                switch(ev.type) {
                case Expose:
        while (!XNextEvent(dpy, &ev)) {
                switch(ev.type) {
                case Expose:
@@ -549,9 +592,11 @@ run(void)
                                        else
                                                setcurrmenu(menu);
                                        previtem = item;
                                        else
                                                setcurrmenu(menu);
                                        previtem = item;
-                               } else if (menu->selected != item)
+                                       drawmenu();
+                               } else if (menu->selected != item) {
                                        menu->selected = item;
                                        menu->selected = item;
-                               drawmenu();
+                                       drawmenu();
+                               }
                        }
                        break;
                case ButtonRelease:
                        }
                        break;
                case ButtonRelease:
@@ -563,11 +608,11 @@ run(void)
                                        setcurrmenu(item->submenu);
                                } else {
                                        printf("%s\n", item->output);
                                        setcurrmenu(item->submenu);
                                } else {
                                        printf("%s\n", item->output);
-                                       cleanupexit();
+                                       return;
                                }
                                drawmenu();
                        } else {
                                }
                                drawmenu();
                        } else {
-                               cleanupexit();
+                               return;
                        }
                        break;
                case LeaveNotify:
                        }
                        break;
                case LeaveNotify:
@@ -594,19 +639,18 @@ freewindow(struct Menu *menu)
 
 /* cleanup and exit */
 static void
 
 /* cleanup and exit */
 static void
-cleanupexit(void)
+cleanup(void)
 {
        freewindow(rootmenu);
        XFreeFont(dpy, dc.font);
        XFreeGC(dpy, dc.gc);
        XCloseDisplay(dpy);
 {
        freewindow(rootmenu);
        XFreeFont(dpy, dc.font);
        XFreeGC(dpy, dc.gc);
        XCloseDisplay(dpy);
-       exit(0);
 }
 
 /* show usage */
 static void
 usage(void)
 {
 }
 
 /* show usage */
 static void
 usage(void)
 {
-       (void)fprintf(stderr, "usage: xmenu [-w] menuname\n");
+       (void)fprintf(stderr, "usage: xmenu [-w]\n");
        exit(1);
 }
        exit(1);
 }