cursor fix + style
93af72e1
1 file(s) · +19 −25
| 30 | 30 | static void drawmenu(void); |
|
| 31 | 31 | static void grabkeyboard(void); |
|
| 32 | 32 | static void insert(const char *s, ssize_t n); |
|
| 33 | - | static void keypress(XKeyEvent *e); |
|
| 33 | + | static void keypress(XKeyEvent *ev); |
|
| 34 | 34 | static void match(void); |
|
| 35 | 35 | static void paste(void); |
|
| 36 | 36 | static void readstdin(void); |
|
| 73 | 73 | } |
|
| 74 | 74 | ||
| 75 | 75 | void |
|
| 76 | - | calcoffsets(void) |
|
| 77 | - | { |
|
| 76 | + | calcoffsets(void) { |
|
| 78 | 77 | unsigned int h, i, n; |
|
| 79 | 78 | ||
| 80 | 79 | h = dc->font.height+2; |
|
| 104 | 103 | ||
| 105 | 104 | void |
|
| 106 | 105 | drawmenu(void) { |
|
| 106 | + | int curpos; |
|
| 107 | 107 | Item *item; |
|
| 108 | 108 | ||
| 109 | 109 | dc->x = 0; |
|
| 110 | 110 | dc->y = 0; |
|
| 111 | 111 | drawrect(dc, 0, 0, mw, mh, BG(dc, normcol)); |
|
| 112 | - | ||
| 113 | 112 | dc->h = dc->font.height + 2; |
|
| 114 | 113 | dc->y = topbar ? 0 : mh - dc->h; |
|
| 115 | - | /* print prompt? */ |
|
| 114 | + | ||
| 116 | 115 | if(prompt) { |
|
| 117 | 116 | dc->w = promptw; |
|
| 118 | 117 | drawtext(dc, prompt, selcol); |
|
| 119 | 118 | dc->x = dc->w; |
|
| 120 | 119 | } |
|
| 121 | - | dc->w = mw - dc->x; |
|
| 122 | - | /* print input field */ |
|
| 123 | - | if(matches && lines == 0 && textw(dc, text) <= inputw) |
|
| 124 | - | dc->w = inputw; |
|
| 120 | + | dc->w = (lines > 0 || !matches) ? mw - dc->x : inputw; |
|
| 125 | 121 | drawtext(dc, text, normcol); |
|
| 126 | - | drawrect(dc, textnw(dc, text, cursor) + dc->h/2 - 2, 2, 1, dc->h - 4, FG(dc, normcol)); |
|
| 122 | + | if((curpos = textnw(dc, text, cursor) + dc->h/2 - 2) < dc->w) |
|
| 123 | + | drawrect(dc, curpos, 2, 1, dc->h - 4, FG(dc, normcol)); |
|
| 127 | 124 | ||
| 128 | 125 | if(lines > 0) { |
|
| 129 | 126 | dc->y = topbar ? dc->h : 0; |
|
| 133 | 130 | dc->y += dc->h; |
|
| 134 | 131 | } |
|
| 135 | 132 | } |
|
| 136 | - | else if(curr && (dc->w == inputw || curr->next)) { |
|
| 133 | + | else if(matches) { |
|
| 137 | 134 | dc->x += inputw; |
|
| 138 | 135 | dc->w = textw(dc, "<"); |
|
| 139 | 136 | if(curr->left) |
|
| 173 | 170 | } |
|
| 174 | 171 | ||
| 175 | 172 | void |
|
| 176 | - | keypress(XKeyEvent *e) { |
|
| 177 | - | char buf[sizeof text]; |
|
| 173 | + | keypress(XKeyEvent *ev) { |
|
| 174 | + | char buf[32]; |
|
| 178 | 175 | int n; |
|
| 179 | 176 | size_t len; |
|
| 180 | 177 | KeySym ksym; |
|
| 181 | 178 | ||
| 182 | 179 | len = strlen(text); |
|
| 183 | - | XLookupString(e, buf, sizeof buf, &ksym, NULL); |
|
| 184 | - | if(e->state & ControlMask) { |
|
| 180 | + | XLookupString(ev, buf, sizeof buf, &ksym, NULL); |
|
| 181 | + | if(ev->state & ControlMask) { |
|
| 185 | 182 | switch(tolower(ksym)) { |
|
| 186 | 183 | default: |
|
| 187 | 184 | return; |
|
| 235 | 232 | break; |
|
| 236 | 233 | case XK_y: /* paste selection */ |
|
| 237 | 234 | XConvertSelection(dc->dpy, XA_PRIMARY, utf8, None, win, CurrentTime); |
|
| 238 | - | /* causes SelectionNotify event */ |
|
| 239 | 235 | return; |
|
| 240 | 236 | } |
|
| 241 | 237 | } |
|
| 289 | 285 | case XK_Up: |
|
| 290 | 286 | if(!sel || !sel->left) |
|
| 291 | 287 | return; |
|
| 292 | - | sel = sel->left; |
|
| 293 | - | if(sel->right == curr) { |
|
| 288 | + | if((sel = sel->left)->right == curr) { |
|
| 294 | 289 | curr = prev; |
|
| 295 | 290 | calcoffsets(); |
|
| 296 | 291 | } |
|
| 309 | 304 | break; |
|
| 310 | 305 | case XK_Return: |
|
| 311 | 306 | case XK_KP_Enter: |
|
| 312 | - | fputs((sel && !(e->state & ShiftMask)) ? sel->text : text, stdout); |
|
| 307 | + | fputs((sel && !(ev->state & ShiftMask)) ? sel->text : text, stdout); |
|
| 313 | 308 | fflush(stdout); |
|
| 314 | 309 | exit(EXIT_SUCCESS); |
|
| 315 | 310 | case XK_Right: |
|
| 322 | 317 | case XK_Down: |
|
| 323 | 318 | if(!sel || !sel->right) |
|
| 324 | 319 | return; |
|
| 325 | - | sel = sel->right; |
|
| 326 | - | if(sel == next) { |
|
| 320 | + | if((sel = sel->right) == next) { |
|
| 327 | 321 | curr = next; |
|
| 328 | 322 | calcoffsets(); |
|
| 329 | 323 | } |
|
| 404 | 398 | if(!(new = malloc(sizeof *new))) |
|
| 405 | 399 | eprintf("cannot malloc %u bytes\n", sizeof *new); |
|
| 406 | 400 | if(!(new->text = strdup(buf))) |
|
| 407 | - | eprintf("cannot strdup %u bytes\n", strlen(buf)); |
|
| 401 | + | eprintf("cannot strdup %u bytes\n", strlen(buf)+1); |
|
| 408 | 402 | inputw = MAX(inputw, textw(dc, new->text)); |
|
| 409 | 403 | new->next = new->left = new->right = NULL; |
|
| 410 | 404 | if(item) |
|
| 485 | 479 | wa.background_pixmap = ParentRelative; |
|
| 486 | 480 | wa.event_mask = ExposureMask | KeyPressMask | VisibilityChangeMask; |
|
| 487 | 481 | win = XCreateWindow(dc->dpy, root, x, y, mw, mh, 0, |
|
| 488 | - | DefaultDepth(dc->dpy, screen), CopyFromParent, |
|
| 489 | - | DefaultVisual(dc->dpy, screen), |
|
| 490 | - | CWOverrideRedirect | CWBackPixmap | CWEventMask, &wa); |
|
| 482 | + | DefaultDepth(dc->dpy, screen), CopyFromParent, |
|
| 483 | + | DefaultVisual(dc->dpy, screen), |
|
| 484 | + | CWOverrideRedirect | CWBackPixmap | CWEventMask, &wa); |
|
| 491 | 485 | ||
| 492 | 486 | grabkeyboard(); |
|
| 493 | 487 | setcanvas(dc, mw, mh); |
|