merged *{h,v} functions
b7695fa9
1 file(s) · +40 −59
| 25 | 25 | }; |
|
| 26 | 26 | ||
| 27 | 27 | static void appenditem(Item *item, Item **list, Item **last); |
|
| 28 | - | static void calcoffsetsh(void); |
|
| 29 | - | static void calcoffsetsv(void); |
|
| 28 | + | static void calcoffsets(void); |
|
| 30 | 29 | static char *cistrstr(const char *s, const char *sub); |
|
| 31 | 30 | static void drawmenu(void); |
|
| 32 | - | static void drawmenuh(void); |
|
| 33 | - | static void drawmenuv(void); |
|
| 34 | 31 | static void grabkeyboard(void); |
|
| 35 | 32 | static void insert(const char *s, ssize_t n); |
|
| 36 | 33 | static void keypress(XKeyEvent *e); |
|
| 63 | 60 | ||
| 64 | 61 | static int (*fstrncmp)(const char *, const char *, size_t) = strncmp; |
|
| 65 | 62 | static char *(*fstrstr)(const char *, const char *) = strstr; |
|
| 66 | - | static void (*calcoffsets)(void) = calcoffsetsh; |
|
| 67 | 63 | ||
| 68 | 64 | void |
|
| 69 | 65 | appenditem(Item *item, Item **list, Item **last) { |
|
| 77 | 73 | } |
|
| 78 | 74 | ||
| 79 | 75 | void |
|
| 80 | - | calcoffsetsh(void) { |
|
| 81 | - | unsigned int w, x; |
|
| 76 | + | calcoffsets(void) |
|
| 77 | + | { |
|
| 78 | + | unsigned int h, i, n; |
|
| 79 | + | ||
| 80 | + | h = dc->font.height+2; |
|
| 81 | + | if(lines > 0) |
|
| 82 | + | n = lines * h; |
|
| 83 | + | else |
|
| 84 | + | n = mw - (promptw + inputw + textw(dc, "<") + textw(dc, ">")); |
|
| 82 | 85 | ||
| 83 | - | w = promptw + inputw + textw(dc, "<") + textw(dc, ">"); |
|
| 84 | - | for(x = w, next = curr; next; next = next->right) |
|
| 85 | - | if((x += MIN(textw(dc, next->text), mw / 3)) > mw) |
|
| 86 | + | prev = next = curr; |
|
| 87 | + | for(i = 0; next; next = next->right) |
|
| 88 | + | if((i += (lines > 0) ? h : MIN(textw(dc, next->text), mw/3)) > n) |
|
| 86 | 89 | break; |
|
| 87 | - | for(x = w, prev = curr; prev && prev->left; prev = prev->left) |
|
| 88 | - | if((x += MIN(textw(dc, prev->left->text), mw / 3)) > mw) |
|
| 90 | + | for(i = 0; prev && prev->left; prev = prev->left) |
|
| 91 | + | if((i += (lines > 0) ? h : MIN(textw(dc, prev->left->text), mw/3)) > n) |
|
| 89 | 92 | break; |
|
| 90 | 93 | } |
|
| 91 | 94 | ||
| 92 | - | void |
|
| 93 | - | calcoffsetsv(void) { |
|
| 94 | - | unsigned int i; |
|
| 95 | - | ||
| 96 | - | next = prev = curr; |
|
| 97 | - | for(i = 0; i < lines && next; i++) |
|
| 98 | - | next = next->right; |
|
| 99 | - | for(i = 0; i < lines && prev && prev->left; i++) |
|
| 100 | - | prev = prev->left; |
|
| 101 | - | } |
|
| 102 | - | ||
| 103 | 95 | char * |
|
| 104 | 96 | cistrstr(const char *s, const char *sub) { |
|
| 105 | 97 | size_t len; |
|
| 112 | 104 | ||
| 113 | 105 | void |
|
| 114 | 106 | drawmenu(void) { |
|
| 107 | + | Item *item; |
|
| 108 | + | ||
| 115 | 109 | dc->x = 0; |
|
| 116 | 110 | dc->y = 0; |
|
| 117 | 111 | drawrect(dc, 0, 0, mw, mh, BG(dc, normcol)); |
|
| 129 | 123 | dc->w = inputw; |
|
| 130 | 124 | drawtext(dc, text, normcol); |
|
| 131 | 125 | drawrect(dc, textnw(dc, text, cursor) + dc->h/2 - 2, 2, 1, dc->h - 4, FG(dc, normcol)); |
|
| 132 | - | if(lines > 0) |
|
| 133 | - | drawmenuv(); |
|
| 134 | - | else if(curr && (dc->w == inputw || curr->next)) |
|
| 135 | - | drawmenuh(); |
|
| 136 | - | commitdraw(dc, win); |
|
| 137 | - | } |
|
| 138 | 126 | ||
| 139 | - | void |
|
| 140 | - | drawmenuh(void) { |
|
| 141 | - | Item *item; |
|
| 142 | - | ||
| 143 | - | dc->x += inputw; |
|
| 144 | - | dc->w = textw(dc, "<"); |
|
| 145 | - | if(curr->left) |
|
| 146 | - | drawtext(dc, "<", normcol); |
|
| 147 | - | for(item = curr; item != next; item = item->right) { |
|
| 148 | - | dc->x += dc->w; |
|
| 149 | - | dc->w = MIN(textw(dc, item->text), mw / 3); |
|
| 150 | - | drawtext(dc, item->text, (item == sel) ? selcol : normcol); |
|
| 127 | + | if(lines > 0) { |
|
| 128 | + | dc->y = topbar ? dc->h : 0; |
|
| 129 | + | dc->w = mw - dc->x; |
|
| 130 | + | for(item = curr; item != next; item = item->right) { |
|
| 131 | + | drawtext(dc, item->text, (item == sel) ? selcol : normcol); |
|
| 132 | + | dc->y += dc->h; |
|
| 133 | + | } |
|
| 151 | 134 | } |
|
| 152 | - | dc->w = textw(dc, ">"); |
|
| 153 | - | dc->x = mw - dc->w; |
|
| 154 | - | if(next) |
|
| 155 | - | drawtext(dc, ">", normcol); |
|
| 156 | - | } |
|
| 157 | - | ||
| 158 | - | void |
|
| 159 | - | drawmenuv(void) { |
|
| 160 | - | Item *item; |
|
| 161 | - | ||
| 162 | - | dc->y = topbar ? dc->h : 0; |
|
| 163 | - | dc->w = mw - dc->x; |
|
| 164 | - | for(item = curr; item != next; item = item->right) { |
|
| 165 | - | drawtext(dc, item->text, (item == sel) ? selcol : normcol); |
|
| 166 | - | dc->y += dc->h; |
|
| 135 | + | else if(curr && (dc->w == inputw || curr->next)) { |
|
| 136 | + | dc->x += inputw; |
|
| 137 | + | dc->w = textw(dc, "<"); |
|
| 138 | + | if(prev) |
|
| 139 | + | drawtext(dc, "<", normcol); |
|
| 140 | + | for(item = curr; item != next; item = item->right) { |
|
| 141 | + | dc->x += dc->w; |
|
| 142 | + | dc->w = MIN(textw(dc, item->text), mw/3); |
|
| 143 | + | drawtext(dc, item->text, (item == sel) ? selcol : normcol); |
|
| 144 | + | } |
|
| 145 | + | dc->w = textw(dc, ">"); |
|
| 146 | + | dc->x = mw - dc->w; |
|
| 147 | + | if(next) |
|
| 148 | + | drawtext(dc, ">", normcol); |
|
| 167 | 149 | } |
|
| 150 | + | commitdraw(dc, win); |
|
| 168 | 151 | } |
|
| 169 | 152 | ||
| 170 | 153 | void |
|
| 494 | 477 | y = topbar ? 0 : DisplayHeight(dc->dpy, screen) - mh; |
|
| 495 | 478 | mw = DisplayWidth(dc->dpy, screen); |
|
| 496 | 479 | } |
|
| 497 | - | ||
| 498 | 480 | /* input window */ |
|
| 499 | 481 | wa.override_redirect = True; |
|
| 500 | 482 | wa.background_pixmap = ParentRelative; |
|
| 543 | 525 | usage(); |
|
| 544 | 526 | /* double flags */ |
|
| 545 | 527 | else if(!strcmp(argv[i], "-l")) { |
|
| 546 | - | if((lines = atoi(argv[++i])) > 0) |
|
| 547 | - | calcoffsets = calcoffsetsv; |
|
| 528 | + | lines = atoi(argv[++i]); |
|
| 548 | 529 | } |
|
| 549 | 530 | else if(!strcmp(argv[i], "-p")) |
|
| 550 | 531 | prompt = argv[++i]; |
|