From 1daad4db8bc3fdfc356903e50cdaf87651c12c2c Mon Sep 17 00:00:00 2001 From: Mike Lowis Date: Wed, 5 Oct 2016 14:43:35 -0400 Subject: [PATCH] Added basic mouse functionality to move the cursor to the clicked location --- edit.h | 1 + screen.c | 22 ++++++++++++++++++++++ xedit.c | 6 ++++-- 3 files changed, 27 insertions(+), 2 deletions(-) diff --git a/edit.h b/edit.h index 19380be..cab2c67 100644 --- a/edit.h +++ b/edit.h @@ -64,6 +64,7 @@ typedef struct { void screen_reflow(Buf* buf); void screen_update(Buf* buf, unsigned crsr, unsigned* csrx, unsigned* csry); +unsigned screen_getoff(Buf* buf, unsigned pos, unsigned row, unsigned col); void screen_setsize(Buf* buf, unsigned nrows, unsigned ncols); void screen_getsize(unsigned* nrows, unsigned* ncols); void screen_clear(void); diff --git a/screen.c b/screen.c index 2356988..742818f 100644 --- a/screen.c +++ b/screen.c @@ -38,6 +38,28 @@ void screen_setsize(Buf* buf, unsigned nrows, unsigned ncols) { screen_reflow(buf); } +unsigned screen_getoff(Buf* buf, unsigned pos, unsigned row, unsigned col) { + Row* scrrow = screen_getrow(row); + if (!scrrow) return pos; + pos = scrrow->off; + if (col > scrrow->len) { + pos = (scrrow->off + scrrow->rlen - 1); + } else { + for (unsigned x = 0; x < col;) { + Rune r = buf_get(buf,pos++); + if (r == '\n') + break; + else if (r == '\t') + x += (TabWidth - (x % TabWidth)); + else + x += 1; + } + } + if (pos >= buf_end(buf)-1) + return buf_end(buf)-2; + return pos; +} + void screen_getsize(unsigned* nrows, unsigned* ncols) { *nrows = NumRows, *ncols = NumCols; } diff --git a/xedit.c b/xedit.c index b56b117..ebb8efd 100644 --- a/xedit.c +++ b/xedit.c @@ -156,8 +156,7 @@ static void handle_key(XEvent* e) { if (len > 0) { Rune r; size_t len = 0; - if (buf[0] == '\r') - buf[0] = '\n'; + if (buf[0] == '\r') buf[0] = '\n'; for(int i = 0; i < 8 && !utf8decode(&r, &len, buf[i]); i++); if (Buffer.insert_mode) buf_ins(&Buffer, CursorPos++, r); @@ -169,6 +168,9 @@ static void handle_key(XEvent* e) { static void handle_mousebtn(XEvent* e) { switch (e->xbutton.button) { case Button1: /* Left Button */ + CursorPos = screen_getoff(&Buffer, CursorPos, + e->xbutton.y / (X.font->ascent + X.font->descent), + e->xbutton.x / X.font->max_advance_width); break; case Button2: /* Middle Button */ break; -- 2.49.0