]> git.mdlowis.com Git - projs/tide.git/commitdiff
implemented bare minimum file picker
authorMichael D. Lowis <mike@mdlowis.com>
Mon, 24 Dec 2018 03:58:23 +0000 (22:58 -0500)
committerMichael D. Lowis <mike@mdlowis.com>
Mon, 24 Dec 2018 03:58:23 +0000 (22:58 -0500)
inc/x11.h
src/pick.c

index 33fbcee8a4f33c6a19004ea3f66034c10028e3f7..f416a6e5e92b1fcbd0a06604223e6f2514b43305 100644 (file)
--- a/inc/x11.h
+++ b/inc/x11.h
@@ -185,4 +185,11 @@ static KeySym x11_getkey(XConf* x, XEvent* e) {
     return key;
 }
 
+static void x11_draw_string(XConf* x, XftFont* font, int posx, int posy, int color, char* str) {
+    XftColor clr;
+    xftcolor(x, &clr, color);
+    XftDrawStringUtf8(x->xft, &clr, font, posx, posy, (const FcChar8*)str, strlen(str));
+    XftColorFree(x->display, x->visual, x->colormap, &clr);
+}
+
 #pragma GCC diagnostic pop
index 743c93abbf9471ab16c1dd40b87a4c0dfbc82df7..5f59fe493620fd02894f820ed669857a934908da 100644 (file)
@@ -16,7 +16,8 @@ typedef struct {
 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))
@@ -117,19 +118,25 @@ static void xkeypress(XConf* x, XEvent* e) {
         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) {
@@ -146,21 +153,35 @@ static void xresize(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);
 }
 
@@ -190,7 +211,7 @@ int main(void) {
     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;