char Query[8192] = {0};
size_t QueryIdx = 0;
vec_t Choices = {0};
-size_t ChoiceIdx = 2;
+size_t ChoiceIdx = 0;
+size_t Offset = 0;
static char* rdline(FILE* fin) {
if (feof(fin) || ferror(fin))
x->running = false;
ChoiceIdx = SIZE_MAX;
} else if (key == XK_Up) {
- if (ChoiceIdx <= vec_size(&Choices))
- ChoiceIdx++;
- } else if (key == XK_Down) {
if (ChoiceIdx > 0)
ChoiceIdx--;
+ if (ChoiceIdx < Offset)
+ Offset--;
+ } else if (key == XK_Down) {
+ size_t nlines = ((x->height - x->font->height - 4) / x->font->height) - 1;
+ size_t maxidx = Offset + nlines;
+ if (ChoiceIdx+1 < vec_size(&Choices))
+ ChoiceIdx++;
+ if (ChoiceIdx > maxidx)
+ Offset++;
} else if (key == XK_BackSpace) {
if (QueryIdx > 0)
Query[--QueryIdx] = '\0';
} else if (key >= 0x20 && key <= 0x7F) {
if (QueryIdx < sizeof(Query)-1)
Query[QueryIdx++] = key;
- score();
}
+ score();
}
static void xbtnpress(XConf* x, XEvent* e) {
}
static void redraw(XConf* x) {
+ /* draw the background colors and border */
size_t fheight = x->font->height;
x11_draw_rect(x, Palette[EditBg], 0, 0, x->width, x->height);
x11_draw_rect(x, Palette[TagsBg], 0, 0, x->width, fheight + 4);
x11_draw_rect(x, Palette[HorBdr], 0, fheight + 4, x->width, 1);
+ /* get size of the query when printed */
XGlyphInfo glyphinfo;
- XftTextExtentsUtf8 (x->display, x->font, (const FcChar8*)Query, strlen(Query), &glyphinfo);
+ XftTextExtentsUtf8(x->display, x->font, (const FcChar8*)Query, strlen(Query), &glyphinfo);
int offset = 0;
if (glyphinfo.width >= (x->width - 4))
offset = ((x->width - 4) - glyphinfo.width);
- XftColor clr;
- xftcolor(x, &clr, Palette[TagsFg]);
- XftDrawStringUtf8(x->xft, &clr, x->font, offset + 2, fheight, (const FcChar8*)Query, strlen(Query));
- XftColorFree(x->display, x->visual, x->colormap, &clr);
+ /* draw the query and the cursor to the query region */
+ int posx = 2 + glyphinfo.width + offset;
+ x11_draw_rect(x, Palette[TagsCsr], posx-1, 2, 3, 3);
+ x11_draw_rect(x, Palette[TagsCsr], posx, 2, 1, fheight);
+ x11_draw_rect(x, Palette[TagsCsr], posx-1, 2+fheight-3, 3, 3);
+ x11_draw_string(x, x->font, offset + 2, fheight, Palette[TagsFg], Query);
+
+ /* draw the scored and sorted results */
+ size_t nlines = ((x->height - x->font->height - 4) / x->font->height) - 1;
+ for (int i = Offset, y = 2 * fheight + 4; ((size_t)i < vec_size(&Choices)) && ((size_t)i <= Offset+nlines); i++, y += fheight) {
+ bool selected = ((size_t)i == ChoiceIdx);
+ Choice* choice = (Choice*)vec_at(&Choices, i);
+ if (selected)
+ x11_draw_rect(x, Palette[EditSel], 0, y - x->font->ascent, x->width, fheight);
+ x11_draw_string(x, x->font, 2, y, Palette[TagsFg], choice->string);
+ }
x11_flip(x);
}
load_choices();
if (vec_size(&Choices) > 1)
filter();
- Choice* choice = (Choice*)vec_at(&Choices, ChoiceIdx-2);
+ Choice* choice = (Choice*)vec_at(&Choices, ChoiceIdx);
if (vec_size(&Choices) && ChoiceIdx != SIZE_MAX)
printf("%s\n", choice->string);
return 0;