]> git.mdlowis.com Git - projs/tide.git/commitdiff
Added basic mouse functionality to move the cursor to the clicked location
authorMike Lowis <mike.lowis@gentex.com>
Wed, 5 Oct 2016 18:43:35 +0000 (14:43 -0400)
committerMike Lowis <mike.lowis@gentex.com>
Wed, 5 Oct 2016 18:43:35 +0000 (14:43 -0400)
edit.h
screen.c
xedit.c

diff --git a/edit.h b/edit.h
index 19380be4ada5ef899955d7004ed8b9f3ae73bed5..cab2c6743c13c4d8bed028e5818f87768808221c 100644 (file)
--- 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);
index 23569880c1c2638e350d97c0b8506f5aee7187d6..742818f1d569fc5ea4cbafd73392c3d32c39ac07 100644 (file)
--- 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 b56b117f7bebdd52f869e2ce6cacb1c4a3e36607..ebb8efd1d1a89cbaae34a391ff4fc06edee45e3c 100644 (file)
--- 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;