NOW IN PUBLIC DOMAIN!
[xmenu] / xmenu.c
diff --git a/xmenu.c b/xmenu.c
index e3588fd..a56091b 100644 (file)
--- a/xmenu.c
+++ b/xmenu.c
@@ -8,6 +8,7 @@
 
 /* macros */
 #define LEN(x) (sizeof (x) / sizeof (x[0]))
 
 /* macros */
 #define LEN(x) (sizeof (x) / sizeof (x[0]))
+#define MAX(x,y) ((x)>(y)?(x):(y))
 
 /* color enum */
 enum {ColorFG, ColorBG, ColorLast};
 
 /* color enum */
 enum {ColorFG, ColorBG, ColorLast};
@@ -45,6 +46,7 @@ struct Item {
        char *output;
        int y;
        int h;
        char *output;
        int y;
        int h;
+       size_t labellen;
        struct Item *next;
        struct Menu *submenu;
 };
        struct Item *next;
        struct Menu *submenu;
 };
@@ -56,6 +58,7 @@ struct Menu {
        struct Item *selected;
        int x, y, w, h;
        unsigned level;
        struct Item *selected;
        int x, y, w, h;
        unsigned level;
+       Drawable pixmap;
        Window win;
 };
 
        Window win;
 };
 
@@ -66,14 +69,14 @@ static void setupgeom(void);
 static void setupgrab(void);
 static struct Item *allocitem(const char *label, const char *output);
 static struct Menu *allocmenu(struct Menu *parent, struct Item *list, unsigned level);
 static void setupgrab(void);
 static struct Item *allocitem(const char *label, const char *output);
 static struct Menu *allocmenu(struct Menu *parent, struct Item *list, unsigned level);
-static void getmenuitem(Window win, int y,
-                        struct Menu **menu_ret, struct Item **item_ret);
+static void getmenuitem(Window win, int y, struct Menu **menu_ret, struct Item **item_ret);
 static void drawmenu(void);
 static void calcscreengeom(void);
 static void calcmenu(struct Menu *menu);
 static void setcurrmenu(struct Menu *currmenu_new);
 static void parsestdin(void);
 static void run(void);
 static void drawmenu(void);
 static void calcscreengeom(void);
 static void calcmenu(struct Menu *menu);
 static void setcurrmenu(struct Menu *currmenu_new);
 static void parsestdin(void);
 static void run(void);
+static void freewindow(struct Menu *menu);
 static void cleanupexit(void);
 static void usage(void);
 
 static void cleanupexit(void);
 static void usage(void);
 
@@ -211,6 +214,10 @@ allocitem(const char *label, const char *output)
        }
        item->y = 0;
        item->h = item->label ? geom.itemh : geom.separator;
        }
        item->y = 0;
        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->submenu = NULL;
 
        item->next = NULL;
        item->submenu = NULL;
 
@@ -239,7 +246,7 @@ allocmenu(struct Menu *parent, struct Item *list, unsigned level)
        swa.background_pixel = dc.decoration[ColorBG];
        swa.border_pixel = dc.decoration[ColorFG];
        swa.event_mask = ExposureMask | KeyPressMask | ButtonPressMask | ButtonReleaseMask
        swa.background_pixel = dc.decoration[ColorBG];
        swa.border_pixel = dc.decoration[ColorFG];
        swa.event_mask = ExposureMask | KeyPressMask | ButtonPressMask | ButtonReleaseMask
-                      | PointerMotionMask;
+                      | PointerMotionMask | LeaveWindowMask;
        menu->win = XCreateWindow(dpy, rootwin, 0, 0, geom.itemw, geom.itemh, geom.border,
                                  CopyFromParent, CopyFromParent, CopyFromParent,
                                  CWOverrideRedirect | CWBackPixel | CWBorderPixel | CWEventMask,
        menu->win = XCreateWindow(dpy, rootwin, 0, 0, geom.itemw, geom.itemh, geom.border,
                                  CopyFromParent, CopyFromParent, CopyFromParent,
                                  CWOverrideRedirect | CWBackPixel | CWBorderPixel | CWEventMask,
@@ -345,15 +352,21 @@ static void
 calcmenu(struct Menu *menu)
 {
        XWindowChanges changes;
 calcmenu(struct Menu *menu)
 {
        XWindowChanges changes;
+       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) {
                item->y = menu->h;
                if (item->label == NULL)   /* height for separator item */
                        menu->h += geom.separator;
                else
                        menu->h += geom.itemh;
        for (item = menu->list; item != NULL; item = item->next) {
                item->y = menu->h;
                if (item->label == NULL)   /* height for separator item */
                        menu->h += geom.separator;
                else
                        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 */
@@ -388,9 +401,20 @@ calcmenu(struct Menu *menu)
 
        /* update menu geometry */
        changes.height = menu->h;
 
        /* update menu geometry */
        changes.height = menu->h;
+       changes.width = menu->w;
        changes.x = menu->x;
        changes.y = menu->y;
        changes.x = menu->x;
        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 */
+       sizeh.flags = PMaxSize | PMinSize;
+       sizeh.min_width = sizeh.max_width = menu->w;
+       sizeh.min_height = sizeh.max_height = menu->h;
+       XSetWMNormalHints(dpy, menu->win, &sizeh);
+
+       /* create pixmap */
+       menu->pixmap = XCreatePixmap(dpy, menu->win, menu->w, menu->h,
+                                    DefaultDepth(dpy, screen));
 
        /* calculate positions of submenus */
        for (item = menu->list; item != NULL; item = item->next) {
 
        /* calculate positions of submenus */
        for (item = menu->list; item != NULL; item = item->next) {
@@ -452,7 +476,6 @@ drawmenu(void)
        for (menu = currmenu; menu != NULL; menu = menu->parent) {
                for (item = menu->list; item != NULL; item = item->next) {
                        unsigned long *color;
        for (menu = currmenu; menu != NULL; menu = menu->parent) {
                for (item = menu->list; item != NULL; item = item->next) {
                        unsigned long *color;
-                       size_t labellen;
                        int labelx, labely;
 
                        /* determine item color */
                        int labelx, labely;
 
                        /* determine item color */
@@ -465,35 +488,38 @@ drawmenu(void)
 
                        /* draw item box */
                        XSetForeground(dpy, dc.gc, color[ColorBG]);
 
                        /* draw item box */
                        XSetForeground(dpy, dc.gc, color[ColorBG]);
-                       XFillRectangle(dpy, menu->win, dc.gc, 0, item->y,
-                                      geom.itemw, item->h);
+                       XFillRectangle(dpy, menu->pixmap, dc.gc, 0, item->y,
+                                      menu->w, item->h);
 
                        /* continue if item is a separator */
                        if (item->label == NULL)
                                continue;
 
                        /* draw item label */
 
                        /* continue if item is a separator */
                        if (item->label == NULL)
                                continue;
 
                        /* draw item label */
-                       labellen = strlen(item->label);
                        labelx = 0 + dc.fonth;
                        labely = item->y + dc.fonth + geom.itemb;
                        XSetForeground(dpy, dc.gc, color[ColorFG]);
                        labelx = 0 + dc.fonth;
                        labely = item->y + dc.fonth + geom.itemb;
                        XSetForeground(dpy, dc.gc, color[ColorFG]);
-                       XDrawString(dpy, menu->win, 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 */
                        if (item->submenu != NULL) {
 
                        /* draw triangle, if item contains a submenu */
                        if (item->submenu != NULL) {
-                               int trianglex = geom.itemw - (geom.itemb + dc.fonth);
-                               int triangley = item->y + geom.itemb;
+                               int trianglex = menu->w - dc.fonth + geom.itemb - 1;
+                               int triangley = item->y + (3 * item->h)/8 -1;
 
                                XPoint triangle[] = {
                                        {trianglex, triangley},
 
                                XPoint triangle[] = {
                                        {trianglex, triangley},
-                                       {trianglex + dc.fonth, triangley + dc.fonth/2},
-                                       {trianglex, triangley + dc.fonth},
+                                       {trianglex + item->h/8 + 1, item->y + item->h/2},
+                                       {trianglex, triangley + item->h/4 + 2},
                                        {trianglex, triangley}
                                };
 
                                        {trianglex, triangley}
                                };
 
-                               XFillPolygon(dpy, menu->win, dc.gc, triangle, LEN(triangle),
+                               XFillPolygon(dpy, menu->pixmap, dc.gc, triangle, LEN(triangle),
                                             Convex, CoordModeOrigin);
                        }
                                             Convex, CoordModeOrigin);
                        }
+
+                       XCopyArea(dpy, menu->pixmap, menu->win, dc.gc, 0, item->y,
+                                 menu->w, item->h, 0, item->y);
                }
        }
 }
                }
        }
 }
@@ -525,8 +551,8 @@ run(void)
                                        previtem = item;
                                } else if (menu->selected != item)
                                        menu->selected = item;
                                        previtem = item;
                                } else if (menu->selected != item)
                                        menu->selected = item;
+                               drawmenu();
                        }
                        }
-                       drawmenu();
                        break;
                case ButtonRelease:
                        getmenuitem(ev.xbutton.window, ev.xbutton.y, &menu, &item);
                        break;
                case ButtonRelease:
                        getmenuitem(ev.xbutton.window, ev.xbutton.y, &menu, &item);
@@ -544,14 +570,35 @@ run(void)
                                cleanupexit();
                        }
                        break;
                                cleanupexit();
                        }
                        break;
+               case LeaveNotify:
+                       currmenu->selected = NULL;
+                       drawmenu();
+                       break;
                }
        }
 }
 
                }
        }
 }
 
+/* recursivelly free a pixmap */
+static void
+freewindow(struct Menu *menu)
+{
+       struct Item *item;
+
+       for (item = menu->list; item != NULL; item = item->next)
+               if (item->submenu != NULL)
+                       freewindow(item->submenu);
+
+       XFreePixmap(dpy, menu->pixmap);
+       XDestroyWindow(dpy, menu->win);
+}
+
 /* cleanup and exit */
 static void
 cleanupexit(void)
 {
 /* cleanup and exit */
 static void
 cleanupexit(void)
 {
+       freewindow(rootmenu);
+       XFreeFont(dpy, dc.font);
+       XFreeGC(dpy, dc.gc);
        XCloseDisplay(dpy);
        exit(0);
 }
        XCloseDisplay(dpy);
        exit(0);
 }