[hackers] [dmenu] merged *{h,v} functions || Connor Lane Smith

From: <hg_AT_suckless.org>
Date: Tue, 3 Aug 2010 16:10:34 +0000 (UTC)

changeset: 347:60f1c0d04405
tag: tip
user: Connor Lane Smith <cls_AT_lubutu.com>
date: Tue Aug 03 17:10:29 2010 +0100
files: dmenu.c
description:
merged *{h,v} functions

diff -r 4bef776d2be7 -r 60f1c0d04405 dmenu.c
--- a/dmenu.c Mon Aug 02 15:13:33 2010 +0100
+++ b/dmenu.c Tue Aug 03 17:10:29 2010 +0100
@@ -25,12 +25,9 @@
 };
 
 static void appenditem(Item *item, Item **list, Item **last);
-static void calcoffsetsh(void);
-static void calcoffsetsv(void);
+static void calcoffsets(void);
 static char *cistrstr(const char *s, const char *sub);
 static void drawmenu(void);
-static void drawmenuh(void);
-static void drawmenuv(void);
 static void grabkeyboard(void);
 static void insert(const char *s, ssize_t n);
 static void keypress(XKeyEvent *e);
@@ -63,7 +60,6 @@
 
 static int (*fstrncmp)(const char *, const char *, size_t) = strncmp;
 static char *(*fstrstr)(const char *, const char *) = strstr;
-static void (*calcoffsets)(void) = calcoffsetsh;
 
 void
 appenditem(Item *item, Item **list, Item **last) {
@@ -77,29 +73,25 @@
 }
 
 void
-calcoffsetsh(void) {
- unsigned int w, x;
+calcoffsets(void)
+{
+ unsigned int h, i, n;
 
- w = promptw + inputw + textw(dc, "<") + textw(dc, ">");
- for(x = w, next = curr; next; next = next->right)
- if((x += MIN(textw(dc, next->text), mw / 3)) > mw)
+ h = dc->font.height+2;
+ if(lines > 0)
+ n = lines * h;
+ else
+ n = mw - (promptw + inputw + textw(dc, "<") + textw(dc, ">"));
+
+ prev = next = curr;
+ for(i = 0; next; next = next->right)
+ if((i += (lines > 0) ? h : MIN(textw(dc, next->text), mw/3)) > n)
                         break;
- for(x = w, prev = curr; prev && prev->left; prev = prev->left)
- if((x += MIN(textw(dc, prev->left->text), mw / 3)) > mw)
+ for(i = 0; prev && prev->left; prev = prev->left)
+ if((i += (lines > 0) ? h : MIN(textw(dc, prev->left->text), mw/3)) > n)
                         break;
 }
 
-void
-calcoffsetsv(void) {
- unsigned int i;
-
- next = prev = curr;
- for(i = 0; i < lines && next; i++)
- next = next->right;
- for(i = 0; i < lines && prev && prev->left; i++)
- prev = prev->left;
-}
-
 char *
 cistrstr(const char *s, const char *sub) {
         size_t len;
@@ -112,6 +104,8 @@
 
 void
 drawmenu(void) {
+ Item *item;
+
         dc->x = 0;
         dc->y = 0;
         drawrect(dc, 0, 0, mw, mh, BG(dc, normcol));
@@ -129,45 +123,34 @@
                 dc->w = inputw;
         drawtext(dc, text, normcol);
         drawrect(dc, textnw(dc, text, cursor) + dc->h/2 - 2, 2, 1, dc->h - 4, FG(dc, normcol));
- if(lines > 0)
- drawmenuv();
- else if(curr && (dc->w == inputw || curr->next))
- drawmenuh();
+
+ if(lines > 0) {
+ dc->y = topbar ? dc->h : 0;
+ dc->w = mw - dc->x;
+ for(item = curr; item != next; item = item->right) {
+ drawtext(dc, item->text, (item == sel) ? selcol : normcol);
+ dc->y += dc->h;
+ }
+ }
+ else if(curr && (dc->w == inputw || curr->next)) {
+ dc->x += inputw;
+ dc->w = textw(dc, "<");
+ if(prev)
+ drawtext(dc, "<", normcol);
+ for(item = curr; item != next; item = item->right) {
+ dc->x += dc->w;
+ dc->w = MIN(textw(dc, item->text), mw/3);
+ drawtext(dc, item->text, (item == sel) ? selcol : normcol);
+ }
+ dc->w = textw(dc, ">");
+ dc->x = mw - dc->w;
+ if(next)
+ drawtext(dc, ">", normcol);
+ }
         commitdraw(dc, win);
 }
 
 void
-drawmenuh(void) {
- Item *item;
-
- dc->x += inputw;
- dc->w = textw(dc, "<");
- if(curr->left)
- drawtext(dc, "<", normcol);
- for(item = curr; item != next; item = item->right) {
- dc->x += dc->w;
- dc->w = MIN(textw(dc, item->text), mw / 3);
- drawtext(dc, item->text, (item == sel) ? selcol : normcol);
- }
- dc->w = textw(dc, ">");
- dc->x = mw - dc->w;
- if(next)
- drawtext(dc, ">", normcol);
-}
-
-void
-drawmenuv(void) {
- Item *item;
-
- dc->y = topbar ? dc->h : 0;
- dc->w = mw - dc->x;
- for(item = curr; item != next; item = item->right) {
- drawtext(dc, item->text, (item == sel) ? selcol : normcol);
- dc->y += dc->h;
- }
-}
-
-void
 grabkeyboard(void) {
         int i;
 
@@ -494,7 +477,6 @@
                 y = topbar ? 0 : DisplayHeight(dc->dpy, screen) - mh;
                 mw = DisplayWidth(dc->dpy, screen);
         }
-
         /* input window */
         wa.override_redirect = True;
         wa.background_pixmap = ParentRelative;
@@ -543,8 +525,7 @@
                         usage();
                 /* double flags */
                 else if(!strcmp(argv[i], "-l")) {
- if((lines = atoi(argv[++i])) > 0)
- calcoffsets = calcoffsetsv;
+ lines = atoi(argv[++i]);
                 }
                 else if(!strcmp(argv[i], "-p"))
                         prompt = argv[++i];
Received on Tue Aug 03 2010 - 18:10:34 CEST

This archive was generated by hypermail 2.2.0 : Tue Aug 03 2010 - 18:12:04 CEST