commit 45162e6515293436daf5773d216f9feb73bebce5
parent 64a8e14e70cd6a5cea2637ea39b9185ed02e929b
Author: rpa <rpa@laika>
Date: Sun, 4 Dec 2022 21:32:47 +0000
tabul: add selection
Diffstat:
M | src/tabul/tabul.c | | | 73 | +++++++++++++++++++++++++++++++++++++++++-------------------------------- |
1 file changed, 41 insertions(+), 32 deletions(-)
diff --git a/src/tabul/tabul.c b/src/tabul/tabul.c
@@ -41,6 +41,7 @@ void drawedit(void);
struct {
Rectangle r;
+ Rectangle sel;
Point scroll;
Point cur;
} cells;
@@ -120,17 +121,16 @@ drawcell(Point xy)
Image *cellbg = bg;
char *s = "";
int t = 1;
- if (eqpt(xy, cells.cur) != 0) {
- t = 2;
- cellbg = curbg;
- }
+ if (eqpt(xy, cells.cur) != 0) t = 2;
+ if (ptinrect(xy, cells.sel) != 0) cellbg = curbg;
Rectangle r = rectaddpt(cell, addpt(Pt(
(xy.x - cells.scroll.x) * cell.max.x,
(xy.y - cells.scroll.y) * cell.max.y), cells.r.min));
- if (ptinrect(r.min, cells.r) != 1) return;
- char **v = getview(xy);
- if ((v != nil) && (*v != nil)) s = *v;
- _cell(screen, r, bord, cellbg, text, t, font, s);
+ if (ptinrect(r.min, cells.r) == 1) {
+ char **v = getview(xy);
+ if ((v != nil) && (*v != nil)) s = *v;
+ _cell(screen, r, bord, cellbg, text, t, font, s);
+ }
}
void
@@ -213,25 +213,29 @@ mouse(Mouse m)
{
enum { MFREE, MSELECT, MSCROLL, };
static state = MFREE;
- static Point startxy, scroll;
+ static Point startxy, startsel, scroll;
if (m.buttons == 0) {
state = MFREE;
}
- if (m.buttons == 1) {
- state = MSELECT;
- if (ptinrect(m.xy, cells.r) != 0) {
- Point mr = subpt(m.xy, cells.r.min);
- Point newcur = addpt(Pt(mr.x / cell.max.x, mr.y / cell.max.y), cells.scroll);
- if ((cells.cur.x != newcur.x) || (cells.cur.y != newcur.y)) {
- Point oldcur = cells.cur;
- flushedit();
- cells.cur = newcur;
- drawcell(oldcur);
- drawcell(newcur);
- setedit();
- drawedit();
- flushimage(display, 1);
- }
+ if ((m.buttons == 1) && (ptinrect(m.xy, cells.r) != 0)) {
+ Point mr = subpt(m.xy, cells.r.min);
+ Point newcur = addpt(Pt(mr.x / cell.max.x, mr.y / cell.max.y), cells.scroll);
+ if (state != MSELECT) {
+ state = MSELECT;
+ startsel = newcur;
+ }
+ cells.sel = canonrect(Rpt(startsel, newcur));
+ cells.sel.max = addpt(cells.sel.max, Pt(1, 1));
+ if ((cells.cur.x != newcur.x) || (cells.cur.y != newcur.y)) {
+ //Point oldcur = cells.cur;
+ flushedit();
+ cells.cur = newcur;
+ //drawcell(oldcur);
+ //drawcell(newcur);
+ drawcells();
+ setedit();
+ drawedit();
+ flushimage(display, 1);
}
} else if (m.buttons == 2) {
if (state != MSCROLL) {
@@ -288,23 +292,28 @@ kbd(Rune r)
{
switch (r) {
case 0x7f: /* del */
- threadexitsall(nil);
+ // TODO: should reset cursor to original state?
+ break;
case 0x0a: /* newline */
flushedit();
cells.cur.y++;
+ cells.sel = Rpt(cells.cur, addpt(Pt(1, 1), cells.cur));
setedit();
drawedit();
- drawcell(cells.cur);
- drawcell(subpt(cells.cur, Pt(0,1)));
+ //drawcell(cells.cur);
+ //drawcell(subpt(cells.cur, Pt(0,1)));
+ drawcells();
flushimage(display, 1);
break;
case 0x09: /* tab */
flushedit();
cells.cur.x++;
+ cells.sel = Rpt(cells.cur, addpt(Pt(1, 1), cells.cur));
setedit();
drawedit();
- drawcell(cells.cur);
- drawcell(subpt(cells.cur, Pt(1,0)));
+ //drawcell(cells.cur);
+ //drawcell(subpt(cells.cur, Pt(1,0)));
+ drawcells();
flushimage(display, 1);
break;
case 0x08: /* backspace */
@@ -322,7 +331,6 @@ kbd(Rune r)
}
}
-
void
usage(void)
{
@@ -337,10 +345,11 @@ init(void)
colors();
mctl = initmouse(nil, screen);
kctl = initkeyboard(nil);
- cell = Rect(0, 0, stringwidth(font, "0") * 8 + 4, font->height + 4);
+ cell = Rect(0, 0, stringwidth(font, "0") * 16 + 4, font->height + 4);
edit.str = s_new();
cells.cur = ZP;
- cells.scroll = Pt(5, 7);
+ cells.scroll = ZP;
+ cells.sel = Rect(0, 0, 1, 1);
setedit();
resize();
redraw();