commit 995efe1edb4d78897bb2c0f6e19883c41d6280b5
parent 5ca0d55f8ec8ac5833afdef4eea3f8eb31add7b4
Author: Pavel Renev <an2qzavok@gmail.com>
Date: Mon, 5 Oct 2020 12:21:03 +0000
row and column selection
Diffstat:
M | sss.c | | | 186 | +++++++++++++++++++++++++++++++++++++++++++++++++++++-------------------------- |
1 file changed, 126 insertions(+), 60 deletions(-)
diff --git a/sss.c b/sss.c
@@ -21,9 +21,9 @@ Stack *hp, *hr;
Stack *T;
struct Selection {
- Stack *rows;
- Stack *cols;
- Stack *cells;
+ Stack rows;
+ Stack cols;
+ Stack cells;
Addr cursor;
} selection;
@@ -59,6 +59,7 @@ void edit_cell(Addr addr);
Value* hist_get_value(Stack*, Addr);
Addr mouse2addr(Point);
int is_selected(Addr);
+void unselect(void);
void
threadmain(int argc, char **argv)
@@ -91,7 +92,7 @@ threadmain(int argc, char **argv)
if (argc == 1) T = table_read(argv[0]);
else T = newstack();
hp = newstack();
- selection.cells = newstack();
+ //selection.cells = newstack();
selection.cursor = xy2addr(0, 0);
push(hp, T);
@@ -100,8 +101,8 @@ threadmain(int argc, char **argv)
draw_cells();
draw_columns();
draw_rows();
- draw(screen, Rpt(screen->r.min, addpt(screen->r.min,
- Pt(defcwidth, defcheight))), display->white, 0, ZP);
+ cell_primitive(screen, Rpt(screen->r.min, addpt(screen->r.min,
+ Pt(defcwidth, defcheight))), "", 0);
flushimage(display, 1);
enum { KBD, MOUSE, RESIZE, CHEND };
@@ -136,8 +137,9 @@ threadmain(int argc, char **argv)
draw_cells();
draw_columns();
draw_rows();
- draw(screen, Rpt(screen->r.min, addpt(screen->r.min,
- Pt(defcwidth, defcheight))), display->white, 0, ZP);
+ cell_primitive(screen,
+ Rpt(screen->r.min, addpt(screen->r.min,
+ Pt(defcwidth, defcheight))), "", 0);
flushimage(display, 1);
}
mv_old = mv;
@@ -150,8 +152,8 @@ threadmain(int argc, char **argv)
draw_cells();
draw_columns();
draw_rows();
- draw(screen, Rpt(screen->r.min, addpt(screen->r.min,
- Pt(defcwidth, defcheight))), display->white, 0, ZP);
+ cell_primitive(screen, Rpt(screen->r.min, addpt(screen->r.min,
+ Pt(defcwidth, defcheight))), "", 0);
flushimage(display, 1);
break;
}
@@ -172,10 +174,10 @@ cell_primitive(Image *dst, Rectangle r, char *s, int flag)
Ipal = selected ? Isel : Inorm;
Ibg = high ? Ipal[HG] : Ipal[BG];
Ibr = Ipal[BR];
- rr = Rpt(addpt(r.min, Pt(1,1)), subpt(r.max, Pt(1,1)));
+ rr = insetrect(r, 1);
if (cursor != 0) {
Ibr = Ipal[FG];
- rr = Rpt(addpt(r.min, Pt(2,2)), subpt(r.max, Pt(2,2)));
+ rr = insetrect(r, 2);
}
dx = 0;
if (center != 0) dx = (rr.max.x - rr.min.x - stringwidth(font, s)) / 2;
@@ -192,9 +194,9 @@ colors(void)
Inorm[FG] = allocimage(display, Rect(0,0,1,1), RGBA32, 1, 0x000000FF);
Inorm[BR] = allocimage(display, Rect(0,0,1,1), RGBA32, 1, 0x000000FF);
Isel[BG] = allocimage(display, Rect(0,0,1,1), RGBA32, 1, 0x000000FF);
- Isel[HG] = allocimage(display, Rect(0,0,1,1), RGBA32, 1, 0x111111FF);
+ Isel[HG] = allocimage(display, Rect(0,0,1,1), RGBA32, 1, 0x444444FF);
Isel[FG] = allocimage(display, Rect(0,0,1,1), RGBA32, 1, 0xFFFFFFFF);
- Isel[BR] = allocimage(display, Rect(0,0,1,1), RGBA32, 1, 0x808080FF);
+ Isel[BR] = allocimage(display, Rect(0,0,1,1), RGBA32, 1, 0x888888FF);
}
@@ -211,7 +213,11 @@ calc_rects(void)
Rectangle r;
r = screen->r;
rrows = Rect(r.min.x, r.min.y + defcheight, r.min.x + defcwidth, r.max.y);
- rcols = Rect(r.min.x + defcwidth, r.min.y, r.max.x, r.min.y + defcheight);
+ rcols = Rect(
+ r.min.x + defcwidth,
+ r.min.y,
+ r.max.x,
+ r.min.y + defcheight);
rcells = Rect(r.min.x + defcwidth, r.min.y + defcheight, r.max.x, r.max.y);
}
@@ -226,15 +232,24 @@ draw_columns(void)
{
Point pt;
Rectangle r;
- long col;
+ long col, *scol, i;
+ int flag;
char *colnum;
col = 0;
pt = addpt(Pt(defcwidth + view.x, 0), screen->r.min);
while (pt.x < screen->r.max.x) {
if (pt.x >= screen->r.min.x) {
+ flag = CENTER|HIGHLIGHT;
+ for (i = 0; i < selection.cols.size; i++) {
+ scol = selection.cols.data[i];
+ if (col == *scol) {
+ flag |= SELECTED;
+ break;
+ }
+ }
r = rectaddpt(Rect(0, 0, defcwidth, defcheight), pt);
colnum = smprint("%ld", col);
- cell_primitive(screen, r, colnum, CENTER|HIGHLIGHT);
+ cell_primitive(screen, r, colnum, flag);
free(colnum);
}
pt.x += defcwidth;
@@ -247,15 +262,24 @@ draw_rows(void)
{
Point pt;
Rectangle r;
- long row;
+ long row, *srow, i;
+ int flag;
char *rownum;
row = 0;
pt = addpt(Pt(0, defcheight + view.y), screen->r.min);
while (pt.y < screen->r.max.y) {
if (pt.y >= screen->r.min.y) {
+ flag = CENTER|HIGHLIGHT;
+ for (i = 0; i < selection.rows.size; i++) {
+ srow = selection.rows.data[i];
+ if (row == *srow) {
+ flag |= SELECTED;
+ break;
+ }
+ }
r = rectaddpt(Rect(0, 0, defcwidth, defcheight), pt);
rownum = smprint("%ld", row);
- cell_primitive(screen, r, rownum, CENTER|HIGHLIGHT);
+ cell_primitive(screen, r, rownum, flag);
free(rownum);
}
pt.y += defcheight;
@@ -266,45 +290,36 @@ draw_rows(void)
void
draw_cell(Addr addr)
{
-
+ Rectangle r;
+ Point p;
+ Value *v;
+ char *text;
+ int flag;
+ p = addpt(Pt((addr.x + 1) * defcwidth, (addr.y + 1) * defcheight),
+ view);
+ r.min = addpt(screen->r.min, p);
+ r.max = addpt(r.min, Pt(defcwidth, defcheight));
+ v = hist_get_value(hp, addr);
+ text = (v == nil) ? "" : v->data;
+ flag = is_selected(addr);
+ if (addrcmp(addr, selection.cursor) == 0) flag |= CURSOR;
+ cell_primitive(screen, r, text, flag);
}
void
draw_cells(void)
{
- // TODO: draw cursor
- Addr addr;
- Point pt;
Rectangle r;
long x, y;
- int flag;
- char *text;
- Value *v;
- x = 0;
- y = 0;
- pt = addpt(Pt(defcwidth, defcheight), screen->r.min);
- pt = addpt(pt, view);
- while (pt.y < screen->r.max.y) {
- if ((pt.x >= screen->r.min.x) && (pt.y >= screen->r.min.y)) {
- addr = xy2addr(x, y);
- r = rectaddpt(Rect(0,0,defcwidth, defcheight), pt);
- v = hist_get_value(hp, addr);
- if (v == nil) text = "";
- else text = v->data;
- flag = is_selected(addr);
- if (addrcmp(addr, selection.cursor) == 0) flag |= CURSOR;
- cell_primitive(screen, r, text, flag);
- };
- pt.x += defcwidth;
- x++;
- if (pt.x > screen->r.max.x) {
- pt.x = screen->r.min.x + defcwidth + view.x;
- pt.y += defcheight;
- y++;
- x = 0;
- }
- }
+ r = rectsubpt(screen->r, addpt(view, screen->r.min));
+ r.min.x = r.min.x / defcwidth;
+ r.max.x = r.max.x / defcwidth;
+ r.min.y = r.min.y / defcheight;
+ r.max.y = r.max.y / defcheight;
+ for (x = r.min.x; x < r.max.x; x++)
+ for (y = r.min.y; y < r.max.y; y++)
+ draw_cell(xy2addr(x, y));
}
@@ -362,15 +377,25 @@ mouse2addr(Point xy)
return addr;
}
+
int
is_selected(Addr addr)
{
long i;
Addr *a;
- for (i = 0; i < selection.cells->size; i++) {
- a = selection.cells->data[i];
+ long *n;
+ for (i = 0; i < selection.cells.size; i++) {
+ a = selection.cells.data[i];
if (addrcmp(addr, *a) == 0) return SELECTED;
}
+ for (i = 0; i < selection.cols.size; i++) {
+ n = selection.cols.data[i];
+ if (*n == addr.x) return SELECTED;
+ }
+ for (i = 0; i < selection.rows.size; i++) {
+ n = selection.rows.data[i];
+ if (*n == addr.y) return SELECTED;
+ }
return 0;
}
@@ -383,26 +408,26 @@ mousecells(Mouse mv)
if (mv.buttons == 1) {
if (mstate == 0) {
mstate = 2;
-
- while (selection.cells->size > 0) {
- free(pop(selection.cells));
- }
+ unselect();
}
Addr *a;
a = malloc(sizeof(Addr));
*a = m;
selection.cursor = m;
- push(selection.cells, a);
+ push(&selection.cells, a);
+ draw_columns();
+ draw_rows();
draw_cells();
flushimage(display, 1);
}
if (mv.buttons == 4) {
edit_cell(m);
- draw_cells();
+ //draw_cells();
+ draw_cell(m);
draw_columns();
draw_rows();
- draw(screen, Rpt(screen->r.min, addpt(screen->r.min,
- Pt(defcwidth, defcheight))), display->white, 0, ZP);
+ cell_primitive(screen, Rpt(screen->r.min, addpt(screen->r.min,
+ Pt(defcwidth, defcheight))), "", 0);
flushimage(display, 1);
flushimage(display, 1);
}
@@ -411,9 +436,50 @@ mousecells(Mouse mv)
void
mousecols(Mouse mv)
{
+ Addr m;
+ m = mouse2addr(mv.xy);
+ if (mv.buttons == 1) {
+ if (mstate == 0) {
+ mstate = 2;
+ unselect();
+ }
+ long *col;
+ col = malloc(sizeof(long));
+ *col = m.x;
+ push(&selection.cols, col);
+ draw_columns();
+ draw_rows();
+ draw_cells();
+ flushimage(display, 1);
+ }
}
void
mouserows(Mouse mv)
{
+ Addr m;
+ m = mouse2addr(mv.xy);
+ if (mv.buttons == 1) {
+ if (mstate == 0) {
+ mstate = 2;
+ unselect();
+ }
+ long *row;
+ row = malloc(sizeof(long));
+ *row = m.y;
+ push(&selection.rows, row);
+ draw_columns();
+ draw_rows();
+ draw_cells();
+ flushimage(display, 1);
+ }
+
+}
+
+void
+unselect(void)
+{
+ while (selection.cells.size > 0) free(pop(&selection.cells));
+ while (selection.rows.size > 0) free(pop(&selection.rows));
+ while (selection.cols.size > 0) free(pop(&selection.cols));
}