commit 250d761b1b676c0daf8b9af6805416d4b99ca9c1
parent 978bd51f2027bff5fb3d242fed25b156512a9e93
Author: Pavel Renev <an2qzavok@gmail.com>
Date: Fri, 2 Oct 2020 19:12:28 +0000
barebones of selection
Diffstat:
M | sss.c | | | 110 | +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-------------------- |
1 file changed, 83 insertions(+), 27 deletions(-)
diff --git a/sss.c b/sss.c
@@ -9,12 +9,21 @@
#include "stack.h"
#include "cells.h"
+enum {
+ CENTER = 1,
+ HIGHLIGHT = 2,
+ SELECTED = 4,
+};
+
Stack *hp, *hr;
Stack *T;
+Stack *selection;
int defcwidth, defcheight;
-Image *Iborder, *Ibg, *Ifg, *Ibg2;
+enum { BG, HG, FG, BR };
+
+Image *Inorm[4], *Isel[4];
Point view;
@@ -22,12 +31,14 @@ void usage(void);
void clear(void);
void draw_columns(void);
void draw_cells(void);
-void draw_cell(Image*, Rectangle, Image*, Image*, Image*, char*, int);
+void draw_cell(Image*, Rectangle, char*, int);
void draw_rows(void);
void colors(void);
void run_cmd(Mousectl *mc, Keyboardctl *kc);
void edit_cell(Addr addr, Mousectl *mc, Keyboardctl *kc);
Value* hist_get_value(Stack*, Addr);
+Addr mouse2addr(Point);
+int is_selected(Addr);
void
threadmain(int argc, char **argv)
@@ -69,6 +80,7 @@ threadmain(int argc, char **argv)
if (argc == 1) T = table_read(argv[0]);
else T = newstack();
hp = newstack();
+ selection = newstack();
push(hp, T);
clear();
draw_cells();
@@ -87,11 +99,21 @@ threadmain(int argc, char **argv)
if (mv.buttons == 0){
mstate = 0;
}
+ if (mv.buttons == 1) {
+ while (selection->size > 0) {
+ free(pop(selection));
+ }
+ Addr *a;
+ a = malloc(sizeof(Addr));
+ *a = mouse2addr(mv.xy);
+ push(selection, a);
+ draw_cells();
+ flushimage(display, 1);
+ }
if (mv.buttons == 2) mstate = 1;
- if (mv.buttons == 4){
+ if (mv.buttons == 4) {
Addr a;
- a.x = (mv.xy.x - view.x - screen->r.min.x)/defcwidth - 1;
- a.y = (mv.xy.y - view.y - screen->r.min.y)/defcheight - 1;
+ a = mouse2addr(mv.xy);
edit_cell(a, mc, kc);
draw_cells();
draw_columns();
@@ -101,7 +123,7 @@ threadmain(int argc, char **argv)
flushimage(display, 1);
flushimage(display, 1);
}
- if (mstate == 1){
+ if (mstate == 1) {
view = addpt(view, subpt(mv.xy, mv_old.xy));
if (view.x > 0) view.x = 0;
if (view.y > 0) view.y = 0;
@@ -132,10 +154,15 @@ threadmain(int argc, char **argv)
void
colors(void)
{
- Ibg2 = allocimage(display, Rect(0,0,1,1), RGBA32, 1, 0xEEEEEEFF);
- Ibg = display->white;
- Iborder = display->black;
- Ifg = display->black;
+ 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[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[FG] = allocimage(display, Rect(0,0,1,1), RGBA32, 1, 0xFFFFFFFF);
+ Isel[BR] = allocimage(display, Rect(0,0,1,1), RGBA32, 1, 0x808080FF);
+
}
void
@@ -164,7 +191,7 @@ draw_columns(void)
if (pt.x >= screen->r.min.x) {
r = rectaddpt(Rect(0, 0, defcwidth, defcheight), pt);
colnum = smprint("%ld", col);
- draw_cell(screen, r, Ibg2, Iborder, Ifg, colnum, 0);
+ draw_cell(screen, r, colnum, CENTER|HIGHLIGHT);
free(colnum);
}
pt.x += defcwidth;
@@ -178,15 +205,15 @@ draw_rows(void)
Point pt;
Rectangle r;
long row;
- char *colnum;
+ 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) {
r = rectaddpt(Rect(0, 0, defcwidth, defcheight), pt);
- colnum = smprint("%ld", row);
- draw_cell(screen, r, Ibg2, Iborder, Ifg, colnum, 0);
- free(colnum);
+ rownum = smprint("%ld", row);
+ draw_cell(screen, r, rownum, CENTER|HIGHLIGHT);
+ free(rownum);
}
pt.y += defcheight;
row++;
@@ -194,10 +221,17 @@ draw_rows(void)
}
void
-draw_cell(Image *dst, Rectangle r, Image *bg, Image *brd, Image*fg, char *s, int align)
+draw_cell(Image *dst, Rectangle r, char *s, int flag)
{
Rectangle rl, rr, rt, rb;
int dx;
+ Image **Ipal, *Ibg;
+ int center, selected, high;
+ center = flag & CENTER;
+ selected = flag & SELECTED;
+ high = flag & HIGHLIGHT;
+ Ipal = selected ? Isel : Inorm;
+ Ibg = high ? Ipal[HG] : Ipal[BG];
rl = r;
rr = r;
rt = r;
@@ -206,21 +240,21 @@ draw_cell(Image *dst, Rectangle r, Image *bg, Image *brd, Image*fg, char *s, int
rr.min.x = rr.max.x - 1;
rt.max.y = rt.min.y + 1;
rb.min.y = rb.max.y - 1;
- draw(dst, r, bg, 0, ZP);
- draw(dst, rl, brd, 0, ZP);
- draw(dst, rr, brd, 0, ZP);
- draw(dst, rt, brd, 0, ZP);
- draw(dst, rb, brd, 0, ZP);
- dx = (r.max.x - r.min.x - stringwidth(font,s));
- if (align == 0) dx = dx / 2;
- if (align < 0) dx = 0;
- string(dst, addpt(Pt(2 + dx, 2), r.min), fg, ZP, font, s);
+ draw(dst, r, Ibg, 0, ZP);
+ draw(dst, rl, Ipal[BR], 0, ZP);
+ draw(dst, rr, Ipal[BR], 0, ZP);
+ draw(dst, rt, Ipal[BR], 0, ZP);
+ draw(dst, rb, Ipal[BR], 0, ZP);
+ dx = 0;
+ if (center > 0) dx = (r.max.x - r.min.x - stringwidth(font,s)) / 2;
+ string(dst, addpt(Pt(2 + dx, 2), r.min), Ipal[FG], ZP, font, s);
}
void
draw_cells(void)
{
+ Addr addr;
Point pt;
Rectangle r;
long x, y;
@@ -232,11 +266,12 @@ draw_cells(void)
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, xy2addr(x, y));
+ v = hist_get_value(hp, addr);
if (v == nil) text = "";
else text = v->data;
- draw_cell(screen, r, Ibg, Iborder, Ifg, text, -1);
+ draw_cell(screen, r, text, is_selected(addr));
};
pt.x += defcwidth;
x++;
@@ -295,3 +330,23 @@ hist_get_value(Stack *H, Addr addr)
return nil;
}
+Addr
+mouse2addr(Point xy)
+{
+ Addr 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;
+}
+
+int
+is_selected(Addr addr)
+{
+ long i;
+ Addr *a;
+ for (i = 0; i < selection->size; i++) {
+ a = selection->data[i];
+ if (addrcmp(addr, *a) == 0) return SELECTED;
+ }
+ return 0;
+}
+\ No newline at end of file