commit cbdc57971fd13cb6c42ca5a7b1d0f4f7321c382a
parent 995efe1edb4d78897bb2c0f6e19883c41d6280b5
Author: Pavel Renev <an2qzavok@gmail.com>
Date: Wed, 7 Oct 2020 10:01:58 +0000
remove Addr in favor of Point, make selection rectangular
Diffstat:
M | cells.c | | | 28 | ++++++---------------------- |
M | cells.h | | | 16 | +++------------- |
M | sss.c | | | 130 | ++++++++++++++++++++++++++++++++++++++++++++++++------------------------------- |
3 files changed, 88 insertions(+), 86 deletions(-)
diff --git a/cells.c b/cells.c
@@ -1,26 +1,10 @@
#include <u.h>
#include <libc.h>
+#include <draw.h> /* for points, rectangles and associated funcs */
#include "stack.h"
#include "cells.h"
-/*** ADDRESS ***/
-
-Addr xy2addr(long x, long y)
-{
- Addr a;
- a.x = x;
- a.y = y;
- return a;
-}
-
-int
-addrcmp(Addr addr1, Addr addr2)
-{
- if ((addr1.x != addr2.x) || (addr1.y != addr2.y)) return -1;
- return 0;
-}
-
/*** VALUES ***/
Value
@@ -30,7 +14,7 @@ copy_value(Value v)
}
Value
-mk_value(Addr addr, char *data)
+mk_value(Point addr, char *data)
{
Value v;
v.addr = addr;
@@ -76,7 +60,7 @@ table_read(char *path)
switch (b){
case '\t':
v = malloc(sizeof(Value));
- *v = mk_value(xy2addr(x, y), buf);
+ *v = mk_value(Pt(x, y), buf);
push(T, v);
bufsize = 0;
buf = realloc(buf, 1);
@@ -85,7 +69,7 @@ table_read(char *path)
break;
case '\n':
v = malloc(sizeof(Value));
- *v = mk_value(xy2addr(x, y), buf);
+ *v = mk_value(Pt(x, y), buf);
push(T, v);
bufsize = 0;
buf = realloc(buf, 1);
@@ -102,7 +86,7 @@ table_read(char *path)
}
if (buf != nil) {
v = malloc(sizeof(Value));
- *v = mk_value(xy2addr(x, y), buf);
+ *v = mk_value(Pt(x, y), buf);
push(T, v);
}
realloc(buf, 0);
@@ -120,6 +104,6 @@ print_table(Stack *T)
print("----\n");
for (i = 0; i < T->size; i++) {
v = T->data[i];
- print("%ld:%ld %s\n", v->addr.x, v->addr.y, v->data);
+ print("%d:%d %s\n", v->addr.x, v->addr.y, v->data);
}
}
diff --git a/cells.h b/cells.h
@@ -1,24 +1,14 @@
-/*** ADDRESS ***/
-typedef struct Addr Addr;
-
-struct Addr {
- long x;
- long y;
-};
-
-Addr xy2addr(long, long);
-int addrcmp(Addr, Addr);
-
/*** VALUES ***/
+
typedef struct Value Value;
struct Value {
- Addr addr;
+ Point addr;
char *data;
};
Value copy_value(Value v);
-Value mk_value(Addr, char *data);
+Value mk_value(Point, char *data);
void free_value(Value);
/*** TABLE ***/
diff --git a/sss.c b/sss.c
@@ -24,7 +24,7 @@ struct Selection {
Stack rows;
Stack cols;
Stack cells;
- Addr cursor;
+ Point cursor;
} selection;
int defcwidth, defcheight;
@@ -48,17 +48,17 @@ void cell_primitive(Image *dst, Rectangle r, char *s, int flag);
void clear(void);
void draw_columns(void);
void draw_cells(void);
-void draw_cell(Addr);
+void draw_cell(Point);
void draw_rows(void);
void colors(void);
void mousecells(Mouse);
void mousecols(Mouse);
void mouserows(Mouse);
void run_cmd(Mousectl *mc, Keyboardctl *kc);
-void edit_cell(Addr addr);
-Value* hist_get_value(Stack*, Addr);
-Addr mouse2addr(Point);
-int is_selected(Addr);
+void edit_cell(Point addr);
+Value* hist_get_value(Stack*, Point);
+Point mouse2addr(Point);
+int is_selected(Point);
void unselect(void);
void
@@ -92,8 +92,7 @@ threadmain(int argc, char **argv)
if (argc == 1) T = table_read(argv[0]);
else T = newstack();
hp = newstack();
- //selection.cells = newstack();
- selection.cursor = xy2addr(0, 0);
+ selection.cursor = Pt(0, 0);
push(hp, T);
calc_rects();
@@ -190,14 +189,13 @@ void
colors(void)
{
Inorm[BG] = allocimage(display, Rect(0,0,1,1), RGBA32, 1, 0xFFFFFFFF);
- Inorm[HG] = allocimage(display, Rect(0,0,1,1), RGBA32, 1, 0xEEEEEEFF);
+ Inorm[HG] = allocimage(display, Rect(0,0,1,1), RGBA32, 1, 0xBBBBBBFF);
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, 0x444444FF);
Isel[FG] = allocimage(display, Rect(0,0,1,1), RGBA32, 1, 0xFFFFFFFF);
Isel[BR] = allocimage(display, Rect(0,0,1,1), RGBA32, 1, 0x888888FF);
-
}
void
@@ -212,13 +210,15 @@ calc_rects(void)
{
Rectangle r;
r = screen->r;
- rrows = Rect(r.min.x, r.min.y + defcheight, r.min.x + defcwidth, r.max.y);
+ 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);
- rcells = Rect(r.min.x + defcwidth, r.min.y + defcheight, r.max.x, r.max.y);
+ 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);
}
void
@@ -288,7 +288,7 @@ draw_rows(void)
}
void
-draw_cell(Addr addr)
+draw_cell(Point addr)
{
Rectangle r;
Point p;
@@ -302,7 +302,7 @@ draw_cell(Addr addr)
v = hist_get_value(hp, addr);
text = (v == nil) ? "" : v->data;
flag = is_selected(addr);
- if (addrcmp(addr, selection.cursor) == 0) flag |= CURSOR;
+ if (eqpt(addr, selection.cursor) != 0) flag |= CURSOR;
cell_primitive(screen, r, text, flag);
}
@@ -319,7 +319,7 @@ draw_cells(void)
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));
+ draw_cell(Pt(x, y));
}
@@ -333,13 +333,13 @@ run_cmd(Mousectl *mc, Keyboardctl *kc)
}
void
-edit_cell(Addr addr)
+edit_cell(Point addr)
{
Value *v;
Stack *N;
N = newstack();
char prompt[256], buf[1024];
- snprint(prompt, 256, "%ld,%ld:", addr.x, addr.y);
+ snprint(prompt, 256, "%d,%d:", addr.x, addr.y);
v = hist_get_value(hp, addr);
*buf = 0;
if ((v != nil)&&(v->data != nil)) strncat(buf, v->data, 1024);
@@ -353,7 +353,7 @@ edit_cell(Addr addr)
}
Value*
-hist_get_value(Stack *H, Addr addr)
+hist_get_value(Stack *H, Point addr)
{
Stack *T;
Value *v;
@@ -362,16 +362,16 @@ hist_get_value(Stack *H, Addr addr)
T = H->data[x];
for (y = 0; y < T->size; y++) {
v = T->data[y];
- if (addrcmp(addr, v->addr) == 0) return v;
+ if (eqpt(addr, v->addr) != 0) return v;
}
}
return nil;
}
-Addr
+Point
mouse2addr(Point xy)
{
- Addr addr;
+ Point addr;
addr.x = (xy.x - view.x - screen->r.min.x)/defcwidth - 1;
addr.y = (xy.y - view.y - screen->r.min.y)/defcheight - 1;
return addr;
@@ -379,14 +379,14 @@ mouse2addr(Point xy)
int
-is_selected(Addr addr)
+is_selected(Point addr)
{
long i;
- Addr *a;
+ Point *a;
long *n;
for (i = 0; i < selection.cells.size; i++) {
a = selection.cells.data[i];
- if (addrcmp(addr, *a) == 0) return SELECTED;
+ if (eqpt(addr, *a) != 0) return SELECTED;
}
for (i = 0; i < selection.cols.size; i++) {
n = selection.cols.data[i];
@@ -403,26 +403,39 @@ is_selected(Addr addr)
void
mousecells(Mouse mv)
{
- Addr m;
+ Point m, n;
+ static Rectangle s;
+ Rectangle r;
m = mouse2addr(mv.xy);
if (mv.buttons == 1) {
if (mstate == 0) {
+ s.min = m;
mstate = 2;
- unselect();
}
- Addr *a;
- a = malloc(sizeof(Addr));
- *a = m;
+ unselect();
+ s.max = m;
+ r = canonrect(s);
+ for (n = r.min;; n.x++) {
+ if (n.x > r.max.x) {
+ n.x = r.min.x;
+ n.y++;
+ }
+ if (n.y > r.max.y) break;
+ Point *a;
+ a = malloc(sizeof(Point));
+ *a = n;
+ push(&selection.cells, a);
+ }
selection.cursor = m;
- push(&selection.cells, a);
+ draw_cells();
draw_columns();
draw_rows();
- draw_cells();
+ cell_primitive(screen, Rpt(screen->r.min, addpt(screen->r.min,
+ Pt(defcwidth, defcheight))), "", 0);
flushimage(display, 1);
}
if (mv.buttons == 4) {
edit_cell(m);
- //draw_cells();
draw_cell(m);
draw_columns();
draw_rows();
@@ -436,17 +449,25 @@ mousecells(Mouse mv)
void
mousecols(Mouse mv)
{
- Addr m;
+ Point m;
+ static Rectangle s;
+ Rectangle r;
+ int i;
m = mouse2addr(mv.xy);
if (mv.buttons == 1) {
if (mstate == 0) {
+ s.min = m;
mstate = 2;
- unselect();
}
- long *col;
- col = malloc(sizeof(long));
- *col = m.x;
- push(&selection.cols, col);
+ unselect();
+ s.max = m;
+ r = canonrect(s);
+ for (i = r.min.x; i <= r.max.x; i++) {
+ long *col;
+ col = malloc(sizeof(long));
+ *col = i;
+ push(&selection.cols, col);
+ }
draw_columns();
draw_rows();
draw_cells();
@@ -457,29 +478,36 @@ mousecols(Mouse mv)
void
mouserows(Mouse mv)
{
- Addr m;
+ Point m;
+ static Rectangle s;
+ Rectangle r;
+ int i;
m = mouse2addr(mv.xy);
if (mv.buttons == 1) {
if (mstate == 0) {
+ s.min = m;
mstate = 2;
- unselect();
}
- long *row;
- row = malloc(sizeof(long));
- *row = m.y;
- push(&selection.rows, row);
+ unselect();
+ s.max = m;
+ r = canonrect(s);
+ for (i = r.min.y; i <= r.max.y; i++) {
+ long *row;
+ row = malloc(sizeof(long));
+ *row = i;
+ 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));
+ while (selection.rows.size > 0) free(pop(&selection.rows));
+ while (selection.cols.size > 0) free(pop(&selection.cols));
}