]> git.mdlowis.com Git - projs/tide.git/commitdiff
implemented scrolling with mouse scroll wheel
authorMichael D. Lowis <mike@mdlowis.com>
Fri, 18 Nov 2016 01:40:12 +0000 (20:40 -0500)
committerMichael D. Lowis <mike@mdlowis.com>
Fri, 18 Nov 2016 01:40:12 +0000 (20:40 -0500)
inc/edit.h
libedit/view.c
xedit.c

index 55f0b3f272c6f78b1351d7e881adbdcf94cdec1d..a7d3b9c1a38d8bcfd20a1a22959ea4112004102d 100644 (file)
@@ -161,6 +161,7 @@ void view_undo(View* view);
 void view_redo(View* view);
 void view_putstr(View* view, char* str);
 char* view_getstr(View* view, Sel* sel);
+void view_scroll(View* view, int move);
 
 /* Command Executions
  *****************************************************************************/
index d2cf1a8f8cd1c6f928a6a6bfff5a44667c6d67e1..8cdfd9c2aa8041c7cb1dc900fc2d0a97c73fa485 100644 (file)
@@ -88,48 +88,47 @@ static unsigned prev_screen_line(View* view, unsigned bol, unsigned off) {
     return pos;
 }
 
-static void scroll_up(View* view, unsigned csr, unsigned first) {
-    while (csr < first) {
-        unsigned bol    = buf_bol(&(view->buffer), first);
-        unsigned prevln = (first == bol ? buf_byline(&(view->buffer), bol, -1) : bol);
-        prevln = prev_screen_line(view, prevln, first);
-        /* delete the last row and shift the others */
-        free(view->rows[view->nrows - 1]);
-        memmove(&view->rows[1], &view->rows[0], sizeof(Row*) * (view->nrows-1));
-        view->rows[0] = calloc(1, sizeof(Row) + (view->ncols * sizeof(UGlyph)));
-        view->rows[0]->off = prevln;
-        /* fill in row content */
-        fill_row(view, 0, view->rows[0]->off);
-        first = view->rows[0]->off;
-    }
+static unsigned scroll_up(View* view) {
+    unsigned first = view->rows[0]->off;
+    unsigned bol    = buf_bol(&(view->buffer), first);
+    unsigned prevln = (first == bol ? buf_byline(&(view->buffer), bol, -1) : bol);
+    prevln = prev_screen_line(view, prevln, first);
+    /* delete the last row and shift the others */
+    free(view->rows[view->nrows - 1]);
+    memmove(&view->rows[1], &view->rows[0], sizeof(Row*) * (view->nrows-1));
+    view->rows[0] = calloc(1, sizeof(Row) + (view->ncols * sizeof(UGlyph)));
+    view->rows[0]->off = prevln;
+    /* fill in row content */
+    fill_row(view, 0, view->rows[0]->off);
+    return view->rows[0]->off;
 }
 
-static void scroll_dn(View* view, unsigned csr, unsigned last) {
-    while (csr > last) {
-        /* delete the first row and shift the others */
-        if (view->nrows > 1) {
-            free(view->rows[0]);
-            memmove(&view->rows[0], &view->rows[1], sizeof(Row*) * (view->nrows-1));
-            view->rows[view->nrows-1] = calloc(1, sizeof(Row) + (view->ncols * sizeof(UGlyph)));
-            view->rows[view->nrows-1]->off = (view->rows[view->nrows-2]->off + view->rows[view->nrows-2]->rlen);
-            /* fill in row content */
-            fill_row(view, view->nrows-1, view->rows[view->nrows-1]->off);
-        } else {
-            view->rows[0]->off += view->rows[0]->rlen;
-            fill_row(view, 0, view->rows[0]->off);
-        }
-        last = view->rows[view->nrows-1]->off + view->rows[view->nrows-1]->rlen - 1;
+static unsigned scroll_dn(View* view) {
+    unsigned last  = view->rows[view->nrows-1]->off + view->rows[view->nrows-1]->rlen - 1;
+    if (last >= buf_end(&(view->buffer)))
+        return last;
+    /* delete the first row and shift the others */
+    if (view->nrows > 1) {
+        free(view->rows[0]);
+        memmove(&view->rows[0], &view->rows[1], sizeof(Row*) * (view->nrows-1));
+        view->rows[view->nrows-1] = calloc(1, sizeof(Row) + (view->ncols * sizeof(UGlyph)));
+        view->rows[view->nrows-1]->off = (view->rows[view->nrows-2]->off + view->rows[view->nrows-2]->rlen);
+        /* fill in row content */
+        fill_row(view, view->nrows-1, view->rows[view->nrows-1]->off);
+    } else {
+        view->rows[0]->off += view->rows[0]->rlen;
+        fill_row(view, 0, view->rows[0]->off);
     }
+    return view->rows[view->nrows-1]->off + view->rows[view->nrows-1]->rlen - 1;
 }
 
 static void sync_view(View* view, size_t csr) {
     unsigned first = view->rows[0]->off;
     unsigned last  = view->rows[view->nrows-1]->off + view->rows[view->nrows-1]->rlen - 1;
-    if (csr < first) {
-        scroll_up(view, csr, first);
-    } else if (csr > last) {
-        scroll_dn(view, csr, last);
-    }
+    while (csr < first)
+        first = scroll_up(view);
+    while (csr > last)
+        last = scroll_dn(view);
     view->sync_needed = false;
 }
 
@@ -314,7 +313,6 @@ char* view_getstr(View* view, Sel* range) {
     char utf[UTF_MAX] = {0};
     size_t len = 0;
     char*  str = NULL;
-
     for (; sel.beg <= sel.end; sel.beg++) {
         Rune rune = buf_get(buf, sel.beg);
         if (rune == RUNE_CRLF) {
@@ -329,8 +327,14 @@ char* view_getstr(View* view, Sel* range) {
             len += n;
         }
     }
-
     str = realloc(str, len+1);
     if (str) str[len] = '\0';
     return str;
 }
+
+void view_scroll(View* view, int move) {
+    if (move < 0)
+        scroll_up(view);
+    else
+        scroll_dn(view);
+}
diff --git a/xedit.c b/xedit.c
index de36454f90c42a44f3ec7fb0c1966a5a0fee85d4..44ded474c9226afcc11d83af6d4e96c3e3319d35 100644 (file)
--- a/xedit.c
+++ b/xedit.c
@@ -182,11 +182,11 @@ static void mouse_right(enum RegionId id, size_t count, size_t row, size_t col)
 }
 
 static void mouse_wheelup(enum RegionId id, size_t count, size_t row, size_t col) {
-    //view_scroll(getview(id), -1);
+    view_scroll(getview(id), -1);
 }
 
 static void mouse_wheeldn(enum RegionId id, size_t count, size_t row, size_t col) {
-    //view_scroll(getview(id), 1);
+    view_scroll(getview(id), 1);
 }
 
 void (*MouseActs[MOUSE_BTN_COUNT])(enum RegionId id, size_t count, size_t row, size_t col) = {
@@ -338,7 +338,7 @@ static void draw_region(enum RegionId id) {
     size_t fwidth  = x11_font_width(Font);
     /* update the screen buffer and retrieve cursor coordinates */
     View* view = getview(id);
-    size_t csrx, csry;
+    size_t csrx = SIZE_MAX, csry = SIZE_MAX;
     view_update(view, &csrx, &csry);
     /* draw the region to the frame buffer */
     if (id == TAGS)
@@ -349,7 +349,7 @@ static void draw_region(enum RegionId id) {
         draw_glyphs(2, Regions[id].y + ((y+1) * fheight), row->cols, row->rlen, row->len);
     }
     /* Place cursor on screen */
-    if (id == Focused)
+    if (id == Focused && csrx != SIZE_MAX && csry != SIZE_MAX)
         x11_draw_rect(CLR_BASE3, 2 + csrx * fwidth, Regions[id].y + (csry * fheight), 1, fheight);
 }