about summary refs log tree commit diff
path: root/menu.c
diff options
context:
space:
mode:
authorokan <okan>2012-12-19 15:21:34 +0000
committerokan <okan>2012-12-19 15:21:34 +0000
commitf77166194f4586cb19abcb7951ca05e356ac74e1 (patch)
tree304227f75a0b05af3f544be74a6ce47b3fae676e /menu.c
parent878eb6bf050c6b55104e12c17b9644c2625bace0 (diff)
parentdc1e3a88438dc732349154bca610dbe813d2fc04 (diff)
downloadcwm-f77166194f4586cb19abcb7951ca05e356ac74e1.tar.gz
cwm-f77166194f4586cb19abcb7951ca05e356ac74e1.tar.xz
cwm-f77166194f4586cb19abcb7951ca05e356ac74e1.zip
cvsimport
Diffstat (limited to 'menu.c')
-rw-r--r--menu.c93
1 files changed, 60 insertions, 33 deletions
diff --git a/menu.c b/menu.c
index dcf801d..87c04d6 100644
--- a/menu.c
+++ b/menu.c
@@ -65,11 +65,13 @@ struct menu_ctx {
 static struct menu	*menu_handle_key(XEvent *, struct menu_ctx *,
 			     struct menu_q *, struct menu_q *);
 static void		 menu_handle_move(XEvent *, struct menu_ctx *,
-			     struct screen_ctx *);
+			     struct screen_ctx *, struct menu_q *);
 static struct menu	*menu_handle_release(XEvent *, struct menu_ctx *,
 			     struct screen_ctx *, struct menu_q *);
 static void		 menu_draw(struct screen_ctx *, struct menu_ctx *,
 			     struct menu_q *, struct menu_q *);
+static void 		 menu_draw_entry(struct screen_ctx *, struct menu_ctx *,
+			     struct menu_q *, int, int);
 static int		 menu_calc_entry(struct screen_ctx *, struct menu_ctx *,
 			     int, int);
 static int		 menu_keycode(XKeyEvent *, enum ctltype *,
@@ -78,20 +80,10 @@ static int		 menu_keycode(XKeyEvent *, enum ctltype *,
 void
 menu_init(struct screen_ctx *sc)
 {
-	XGCValues	 gv;
-
 	sc->menuwin = XCreateSimpleWindow(X_Dpy, sc->rootwin, 0, 0, 1, 1,
 	    Conf.bwidth,
-	    sc->color[CWM_COLOR_FG_MENU].pixel,
-	    sc->color[CWM_COLOR_BG_MENU].pixel);
-
-	gv.foreground =
-	    sc->color[CWM_COLOR_FG_MENU].pixel^sc->color[CWM_COLOR_BG_MENU].pixel;
-	gv.background = sc->color[CWM_COLOR_BG_MENU].pixel;
-	gv.function = GXxor;
-
-	sc->gc = XCreateGC(X_Dpy, sc->menuwin,
-	    GCForeground|GCBackground|GCFunction, &gv);
+	    sc->xftcolor[CWM_COLOR_MENU_FG].pixel,
+	    sc->xftcolor[CWM_COLOR_MENU_BG].pixel);
 }
 
 struct menu *
@@ -174,7 +166,7 @@ menu_filter(struct screen_ctx *sc, struct menu_q *menuq, char *prompt,
 			menu_draw(sc, &mc, menuq, &resultq);
 			break;
 		case MotionNotify:
-			menu_handle_move(&e, &mc, sc);
+			menu_handle_move(&e, &mc, sc, &resultq);
 			break;
 		case ButtonRelease:
 			if ((mi = menu_handle_release(&e, &mc, sc, &resultq))
@@ -223,10 +215,7 @@ menu_complete_path(struct menu_ctx *mc)
 		strlcpy(path, mi->text, sizeof(mi->text));
 	}
 	
-	while ((mi = TAILQ_FIRST(&menuq)) != NULL) {
-		TAILQ_REMOVE(&menuq, mi, entry);
-		free(mi);
-	}
+	menuq_clear(&menuq);
 
 	if (path[0] != '\0') 
 		snprintf(mr->text, sizeof(mr->text), "%s \"%s\"",
@@ -442,8 +431,8 @@ menu_draw(struct screen_ctx *sc, struct menu_ctx *mc, struct menu_q *menuq,
 	    mc->width, mc->height);
 
 	if (mc->hasprompt) {
-		font_draw(sc, mc->dispstr, strlen(mc->dispstr), sc->menuwin,
-		    0, font_ascent(sc) + 1);
+		font_draw(sc, mc->dispstr, strlen(mc->dispstr), sc->menuwin, 0,
+		    0, font_ascent(sc));
 		n = 1;
 	} else
 		n = 0;
@@ -458,34 +447,61 @@ menu_draw(struct screen_ctx *sc, struct menu_ctx *mc, struct menu_q *menuq,
 			break;
 
 		font_draw(sc, text, MIN(strlen(text), MENU_MAXENTRY),
-		    sc->menuwin, 0, y);
+		    sc->menuwin, 0, 0, y);
 		n++;
 	}
+	if (mc->hasprompt && n > 1 && (mc->searchstr[0] != '\0')) {
+		mc->entry = 1;
+		menu_draw_entry(sc, mc, resultq, mc->entry, 1);
+	}
+}
 
-	if (mc->hasprompt && n > 1 && (mc->searchstr[0] != '\0'))
-		XFillRectangle(X_Dpy, sc->menuwin, sc->gc,
-		    0, font_height(sc), mc->width, font_height(sc));
+static void
+menu_draw_entry(struct screen_ctx *sc, struct menu_ctx *mc,
+    struct menu_q *resultq, int entry, int active)
+{
+	struct menu	*mi;
+	char 		*text;
+	int		 color, i = 0;
 
-	if (mc->noresult)
-		XFillRectangle(X_Dpy, sc->menuwin, sc->gc,
-		    0, 0, mc->width, font_height(sc));
+	if (mc->hasprompt)
+		i = 1;
+
+	TAILQ_FOREACH(mi, resultq, resultentry)
+		if (entry == i++)
+			break;
+	if (mi == NULL)
+		return;
+
+	color = active ? CWM_COLOR_MENU_FG : CWM_COLOR_MENU_BG;
+	text = mi->print[0] != '\0' ?  mi->print : mi->text;
+	XftDrawRect(sc->xftdraw, &sc->xftcolor[color], 0,
+	    font_height(sc) * entry, mc->width,
+	    font_height(sc) + font_descent(sc));
+	font_draw(sc, text, strlen(text), sc->menuwin, active,
+	    0, font_height(sc) * entry + font_ascent(sc) + 1);
 }
 
 static void
-menu_handle_move(XEvent *e, struct menu_ctx *mc, struct screen_ctx *sc)
+menu_handle_move(XEvent *e, struct menu_ctx *mc, struct screen_ctx *sc,
+    struct menu_q *resultq)
 {
 	mc->prev = mc->entry;
 	mc->entry = menu_calc_entry(sc, mc, e->xbutton.x, e->xbutton.y);
 
+	if (mc->prev == mc->entry)
+		return;
+
 	if (mc->prev != -1)
-		XFillRectangle(X_Dpy, sc->menuwin, sc->gc, 0,
-		    font_height(sc) * mc->prev, mc->width, font_height(sc));
+		menu_draw_entry(sc, mc, resultq, mc->prev, 0);
 	if (mc->entry != -1) {
 		(void)xu_ptr_regrab(MENUGRABMASK, Cursor_normal);
-		XFillRectangle(X_Dpy, sc->menuwin, sc->gc, 0,
-		    font_height(sc) * mc->entry, mc->width, font_height(sc));
+		menu_draw_entry(sc, mc, resultq, mc->entry, 1);
 	} else
 		(void)xu_ptr_regrab(MENUGRABMASK, Cursor_default);
+
+	if (mc->hasprompt)
+		menu_draw_entry(sc, mc, resultq, 1, 1);
 }
 
 static struct menu *
@@ -519,7 +535,7 @@ menu_calc_entry(struct screen_ctx *sc, struct menu_ctx *mc, int x, int y)
 	entry = y / font_height(sc);
 
 	/* in bounds? */
-	if (x <= 0 || x > mc->width || y <= 0 ||
+	if (x < 0 || x > mc->width || y < 0 ||
 	    y > font_height(sc) * mc->num || entry < 0 || entry >= mc->num)
 		entry = -1;
 
@@ -613,3 +629,14 @@ menu_keycode(XKeyEvent *ev, enum ctltype *ctl, char *chr)
 
 	return (0);
 }
+
+void
+menuq_clear(struct menu_q *mq)
+{
+	struct menu	*mi;
+
+	while ((mi = TAILQ_FIRST(mq)) != NULL) {
+		TAILQ_REMOVE(mq, mi, entry);
+		free(mi);
+	}
+}