From 8e7bcf88c6cd0da750fc584202c21c3bcad096f4 Mon Sep 17 00:00:00 2001 From: "Michael D. Lowis" Date: Wed, 28 Mar 2018 22:03:58 -0400 Subject: [PATCH] more refactoring --- inc/win.h | 3 --- lib/x11.c | 42 ++++++++++++++---------------------------- tide.c | 33 --------------------------------- 3 files changed, 14 insertions(+), 64 deletions(-) diff --git a/inc/win.h b/inc/win.h index a9cca05..89e2213 100644 --- a/inc/win.h +++ b/inc/win.h @@ -49,10 +49,7 @@ void win_setscroll(double offset, double visible); /* These functions must be implemented by any appliation that wishes to use this module */ void onshutdown(void); -void onfocus(bool focused); void onupdate(void); -void onlayout(void); -void onscroll(double percent); void onmouseleft(WinRegion id, bool pressed, size_t row, size_t col); void onmousemiddle(WinRegion id, bool pressed, size_t row, size_t col); void onmouseright(WinRegion id, bool pressed, size_t row, size_t col); diff --git a/lib/x11.c b/lib/x11.c index c6081b5..85966ca 100644 --- a/lib/x11.c +++ b/lib/x11.c @@ -483,12 +483,21 @@ static void onredraw(int width, int height) { int clreditnor = (Regions[EDIT].clrnor.bg << 8 | Regions[EDIT].clrnor.fg); int clreditsel = (Regions[EDIT].clrsel.bg << 8 | Regions[EDIT].clrsel.fg); view_update(win_view(EDIT), clreditnor, clreditsel, &(Regions[EDIT].csrx), &(Regions[EDIT].csry)); - onlayout(); // Let the user program update the scroll bar + + /* calculate and update scroll region */ + View* view = win_view(EDIT); + size_t bend = buf_end(win_buf(EDIT)); + if (bend == 0) bend = 1; + if (!view->rows) return; + size_t vbeg = view->rows[0]->off; + size_t vend = view->rows[view->nrows-1]->off + view->rows[view->nrows-1]->rlen; + double scroll_vis = (double)(vend - vbeg) / (double)bend; + double scroll_off = ((double)vbeg / (double)bend); + win_setscroll(scroll_off, scroll_vis); int clr_hbor = Colors[ClrBorders].bg; int clr_vbor = Colors[ClrBorders].bg; CPair clr_scroll = Colors[ClrScrollNor]; - for (int i = 0; i < SCROLL; i++) { View* view = win_view(i); x11_draw_rect(Regions[i].clrnor.bg, 0, Regions[i].y - 3, width, Regions[i].height + 8); @@ -520,31 +529,6 @@ static void onredraw(int width, int height) { } -static void scroll_actions(int btn, bool pressed, int x, int y) { - size_t row = (y-Regions[SCROLL].y) / x11_font_height(CurrFont); - switch (btn) { - case MouseLeft: - if (pressed) - view_scroll(win_view(EDIT), -row); - break; - case MouseMiddle: - if (pressed) - onscroll((double)(y - Regions[SCROLL].y) / - (double)(Regions[SCROLL].height - Regions[SCROLL].y)); - break; - case MouseRight: - if (pressed) - view_scroll(win_view(EDIT), +row); - break; - case MouseWheelUp: - view_scroll(win_view(EDIT), -ScrollBy); - break; - case MouseWheelDn: - view_scroll(win_view(EDIT), +ScrollBy); - break; - } -} - static void draw_glyphs(size_t x, size_t y, UGlyph* glyphs, size_t rlen, size_t ncols) { XGlyphSpec specs[rlen]; size_t i = 0; @@ -623,7 +607,9 @@ static void xupdate(Job* job) { static void xfocus(XEvent* e) { if (X.xic) (e->type == FocusIn ? XSetICFocus : XUnsetICFocus)(X.xic); - onfocus(e->type == FocusIn); + Buf* buf = win_buf(EDIT); + if (buf->path && buf->modtime != modtime(buf->path)) + buf->errfn("File modified externally: {SaveAs } Overwrite Reload"); } static void xkeypress(XEvent* e) { diff --git a/tide.c b/tide.c index b53faba..096c305 100644 --- a/tide.c +++ b/tide.c @@ -325,14 +325,6 @@ static void quit(char* arg) { before = now; } -static bool changed_externally(Buf* buf) { - if (!buf->path) return false; - bool modified = (buf->modtime != modtime(buf->path)); - if (modified) - ondiagmsg("File modified externally: {SaveAs } Overwrite Reload"); - return modified; -} - static void put(char* arg) { trim_whitespace(arg); win_save(arg); @@ -582,31 +574,6 @@ static KeyBinding Bindings[] = { { 0, 0, 0 } }; -void onscroll(double percent) { - size_t bend = buf_end(win_buf(EDIT)); - size_t off = (size_t)((double)bend * percent); - view_scrollto(win_view(EDIT), (off >= bend ? bend : off)); -} - -void onfocus(bool focused) { - /* notify the user if the file has changed externally */ - if (focused) - (void)changed_externally(win_buf(EDIT)); -} - -void onlayout(void) { - /* calculate and update scroll region */ - View* view = win_view(EDIT); - size_t bend = buf_end(win_buf(EDIT)); - if (bend == 0) bend = 1; - if (!view->rows) return; - size_t vbeg = view->rows[0]->off; - size_t vend = view->rows[view->nrows-1]->off + view->rows[view->nrows-1]->rlen; - double scroll_vis = (double)(vend - vbeg) / (double)bend; - double scroll_off = ((double)vbeg / (double)bend); - win_setscroll(scroll_off, scroll_vis); -} - void onshutdown(void) { quit(0); } -- 2.49.0