cursor fix + style

This commit is contained in:
Connor Lane Smith 2010-08-09 11:54:46 +01:00
parent 0b8072a5a9
commit 93af72e116

38
dmenu.c
View File

@ -30,7 +30,7 @@ static char *cistrstr(const char *s, const char *sub);
static void drawmenu(void); static void drawmenu(void);
static void grabkeyboard(void); static void grabkeyboard(void);
static void insert(const char *s, ssize_t n); static void insert(const char *s, ssize_t n);
static void keypress(XKeyEvent *e); static void keypress(XKeyEvent *ev);
static void match(void); static void match(void);
static void paste(void); static void paste(void);
static void readstdin(void); static void readstdin(void);
@ -73,8 +73,7 @@ appenditem(Item *item, Item **list, Item **last) {
} }
void void
calcoffsets(void) calcoffsets(void) {
{
unsigned int h, i, n; unsigned int h, i, n;
h = dc->font.height+2; h = dc->font.height+2;
@ -104,26 +103,24 @@ cistrstr(const char *s, const char *sub) {
void void
drawmenu(void) { drawmenu(void) {
int curpos;
Item *item; Item *item;
dc->x = 0; dc->x = 0;
dc->y = 0; dc->y = 0;
drawrect(dc, 0, 0, mw, mh, BG(dc, normcol)); drawrect(dc, 0, 0, mw, mh, BG(dc, normcol));
dc->h = dc->font.height + 2; dc->h = dc->font.height + 2;
dc->y = topbar ? 0 : mh - dc->h; dc->y = topbar ? 0 : mh - dc->h;
/* print prompt? */
if(prompt) { if(prompt) {
dc->w = promptw; dc->w = promptw;
drawtext(dc, prompt, selcol); drawtext(dc, prompt, selcol);
dc->x = dc->w; dc->x = dc->w;
} }
dc->w = mw - dc->x; dc->w = (lines > 0 || !matches) ? mw - dc->x : inputw;
/* print input field */
if(matches && lines == 0 && textw(dc, text) <= inputw)
dc->w = inputw;
drawtext(dc, text, normcol); drawtext(dc, text, normcol);
drawrect(dc, textnw(dc, text, cursor) + dc->h/2 - 2, 2, 1, dc->h - 4, FG(dc, normcol)); if((curpos = textnw(dc, text, cursor) + dc->h/2 - 2) < dc->w)
drawrect(dc, curpos, 2, 1, dc->h - 4, FG(dc, normcol));
if(lines > 0) { if(lines > 0) {
dc->y = topbar ? dc->h : 0; dc->y = topbar ? dc->h : 0;
@ -133,7 +130,7 @@ drawmenu(void) {
dc->y += dc->h; dc->y += dc->h;
} }
} }
else if(curr && (dc->w == inputw || curr->next)) { else if(matches) {
dc->x += inputw; dc->x += inputw;
dc->w = textw(dc, "<"); dc->w = textw(dc, "<");
if(curr->left) if(curr->left)
@ -173,15 +170,15 @@ insert(const char *s, ssize_t n) {
} }
void void
keypress(XKeyEvent *e) { keypress(XKeyEvent *ev) {
char buf[sizeof text]; char buf[32];
int n; int n;
size_t len; size_t len;
KeySym ksym; KeySym ksym;
len = strlen(text); len = strlen(text);
XLookupString(e, buf, sizeof buf, &ksym, NULL); XLookupString(ev, buf, sizeof buf, &ksym, NULL);
if(e->state & ControlMask) { if(ev->state & ControlMask) {
switch(tolower(ksym)) { switch(tolower(ksym)) {
default: default:
return; return;
@ -235,7 +232,6 @@ keypress(XKeyEvent *e) {
break; break;
case XK_y: /* paste selection */ case XK_y: /* paste selection */
XConvertSelection(dc->dpy, XA_PRIMARY, utf8, None, win, CurrentTime); XConvertSelection(dc->dpy, XA_PRIMARY, utf8, None, win, CurrentTime);
/* causes SelectionNotify event */
return; return;
} }
} }
@ -289,8 +285,7 @@ keypress(XKeyEvent *e) {
case XK_Up: case XK_Up:
if(!sel || !sel->left) if(!sel || !sel->left)
return; return;
sel = sel->left; if((sel = sel->left)->right == curr) {
if(sel->right == curr) {
curr = prev; curr = prev;
calcoffsets(); calcoffsets();
} }
@ -309,7 +304,7 @@ keypress(XKeyEvent *e) {
break; break;
case XK_Return: case XK_Return:
case XK_KP_Enter: case XK_KP_Enter:
fputs((sel && !(e->state & ShiftMask)) ? sel->text : text, stdout); fputs((sel && !(ev->state & ShiftMask)) ? sel->text : text, stdout);
fflush(stdout); fflush(stdout);
exit(EXIT_SUCCESS); exit(EXIT_SUCCESS);
case XK_Right: case XK_Right:
@ -322,8 +317,7 @@ keypress(XKeyEvent *e) {
case XK_Down: case XK_Down:
if(!sel || !sel->right) if(!sel || !sel->right)
return; return;
sel = sel->right; if((sel = sel->right) == next) {
if(sel == next) {
curr = next; curr = next;
calcoffsets(); calcoffsets();
} }
@ -404,7 +398,7 @@ readstdin(void) {
if(!(new = malloc(sizeof *new))) if(!(new = malloc(sizeof *new)))
eprintf("cannot malloc %u bytes\n", sizeof *new); eprintf("cannot malloc %u bytes\n", sizeof *new);
if(!(new->text = strdup(buf))) if(!(new->text = strdup(buf)))
eprintf("cannot strdup %u bytes\n", strlen(buf)); eprintf("cannot strdup %u bytes\n", strlen(buf)+1);
inputw = MAX(inputw, textw(dc, new->text)); inputw = MAX(inputw, textw(dc, new->text));
new->next = new->left = new->right = NULL; new->next = new->left = new->right = NULL;
if(item) if(item)