From: Michael D. Lowis Date: Fri, 18 Nov 2016 01:40:12 +0000 (-0500) Subject: implemented scrolling with mouse scroll wheel X-Git-Url: https://git.mdlowis.com/?a=commitdiff_plain;h=0fce14fa3c038243f44529c41cccb1044395a658;p=projs%2Ftide.git implemented scrolling with mouse scroll wheel --- diff --git a/inc/edit.h b/inc/edit.h index 55f0b3f..a7d3b9c 100644 --- a/inc/edit.h +++ b/inc/edit.h @@ -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 *****************************************************************************/ diff --git a/libedit/view.c b/libedit/view.c index d2cf1a8..8cdfd9c 100644 --- a/libedit/view.c +++ b/libedit/view.c @@ -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 de36454..44ded47 100644 --- 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); }