sss

spreadsheets for plan9
git clone git://nsmpr.xyz/sss.git
Log | Files | Refs | README

commit e4db4c9ff2c1c63710f7af454f34707490682e79
parent cbdc57971fd13cb6c42ca5a7b1d0f4f7321c382a
Author: Pavel Renev <an2qzavok@gmail.com>
Date:   Thu,  8 Oct 2020 19:24:13 +0000

rewrite drawing, add space for future cell editing widget

Diffstat:
Msss.c | 84+++++++++++++++++++++++++++++++++++++++++--------------------------------------
1 file changed, 44 insertions(+), 40 deletions(-)

diff --git a/sss.c b/sss.c @@ -35,7 +35,7 @@ Image *Inorm[4], *Isel[4]; Point view; -Rectangle rcells, rrows, rcols, rlbar, rbbar; +Rectangle rcells, rrows, rcols, rlbar, rbbar, rtext, rnope; void (*mousefunc)(Mouse); @@ -88,7 +88,7 @@ threadmain(int argc, char **argv) view = ZP; defcheight = font->height + 4; - defcwidth = stringwidth(font, "01234567") + 4; + defcwidth = stringwidth(font, "0") * 8 + 4; if (argc == 1) T = table_read(argv[0]); else T = newstack(); hp = newstack(); @@ -100,8 +100,7 @@ threadmain(int argc, char **argv) draw_cells(); draw_columns(); draw_rows(); - cell_primitive(screen, Rpt(screen->r.min, addpt(screen->r.min, - Pt(defcwidth, defcheight))), "", 0); + cell_primitive(screen, rnope, "", 0); flushimage(display, 1); enum { KBD, MOUSE, RESIZE, CHEND }; @@ -121,6 +120,7 @@ threadmain(int argc, char **argv) case MOUSE: if (mv.buttons == 0){ mstate = 0; + mousefunc = nil; if (ptinrect(mv.xy, rrows)) mousefunc = mouserows; if (ptinrect(mv.xy, rcols)) mousefunc = mousecols; if (ptinrect(mv.xy, rcells)) mousefunc = mousecells; @@ -136,9 +136,7 @@ threadmain(int argc, char **argv) draw_cells(); draw_columns(); draw_rows(); - cell_primitive(screen, - Rpt(screen->r.min, addpt(screen->r.min, - Pt(defcwidth, defcheight))), "", 0); + cell_primitive(screen, rnope, "", 0); flushimage(display, 1); } mv_old = mv; @@ -151,8 +149,7 @@ threadmain(int argc, char **argv) draw_cells(); draw_columns(); draw_rows(); - cell_primitive(screen, Rpt(screen->r.min, addpt(screen->r.min, - Pt(defcwidth, defcheight))), "", 0); + cell_primitive(screen, rnope, "", 0); flushimage(display, 1); break; } @@ -210,15 +207,25 @@ calc_rects(void) { Rectangle r; r = screen->r; + rtext = Rect( + r.min.x, r.min.y, + r.max.x, r.min.y + font->height + 4); + + rnope = Rect( + r.min.x, rtext.max.y, + r.min.x + defcwidth, rtext.max.y + defcheight); + rrows = Rect( - r.min.x, r.min.y + defcheight, - r.min.x + defcwidth, r.max.y); + r.min.x, rtext.max.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); + r.min.x + defcwidth, rtext.max.y, + r.max.x, rtext.max.y + defcheight); + rcells = Rect( - r.min.x + defcwidth, r.min.y + defcheight, - r.max.x, r.max.y); + rrows.max.x, rcols.max.y, + r.max.x, r.max.y); } void @@ -236,9 +243,9 @@ draw_columns(void) 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) { + pt = addpt(Pt(view.x, 0), rcols.min); + while (pt.x < rcols.max.x) { + if (pt.x + defcwidth >= rcols.min.x) { flag = CENTER|HIGHLIGHT; for (i = 0; i < selection.cols.size; i++) { scol = selection.cols.data[i]; @@ -266,9 +273,9 @@ draw_rows(void) 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) { + pt = addpt(Pt(0, view.y), rrows.min); + while (pt.y < rrows.max.y) { + if (pt.y + defcheight >= rrows.min.y) { flag = CENTER|HIGHLIGHT; for (i = 0; i < selection.rows.size; i++) { srow = selection.rows.data[i]; @@ -295,9 +302,8 @@ draw_cell(Point addr) 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); + p = addpt(Pt(addr.x * defcwidth, addr.y * defcheight), view); + r.min = addpt(rcells.min, p); r.max = addpt(r.min, Pt(defcwidth, defcheight)); v = hist_get_value(hp, addr); text = (v == nil) ? "" : v->data; @@ -312,13 +318,13 @@ draw_cells(void) { Rectangle r; long x, y; - r = rectsubpt(screen->r, addpt(view, screen->r.min)); + r = rectsubpt(rcells, addpt(view, rcells.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++) + for (x = r.min.x; x <= r.max.x; x++) + for (y = r.min.y; y <= r.max.y; y++) draw_cell(Pt(x, y)); } @@ -372,8 +378,8 @@ Point mouse2addr(Point xy) { 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; + addr.x = (xy.x - view.x - rcells.min.x)/defcwidth; + addr.y = (xy.y - view.y - rcells.min.y)/defcheight; return addr; } @@ -408,11 +414,13 @@ mousecells(Mouse mv) Rectangle r; m = mouse2addr(mv.xy); if (mv.buttons == 1) { + unselect(); if (mstate == 0) { + draw_columns(); + draw_rows(); s.min = m; mstate = 2; } - unselect(); s.max = m; r = canonrect(s); for (n = r.min;; n.x++) { @@ -428,10 +436,7 @@ mousecells(Mouse mv) } selection.cursor = m; draw_cells(); - draw_columns(); - draw_rows(); - cell_primitive(screen, Rpt(screen->r.min, addpt(screen->r.min, - Pt(defcwidth, defcheight))), "", 0); + cell_primitive(screen, rnope, "", 0); flushimage(display, 1); } if (mv.buttons == 4) { @@ -439,8 +444,7 @@ mousecells(Mouse mv) draw_cell(m); draw_columns(); draw_rows(); - cell_primitive(screen, Rpt(screen->r.min, addpt(screen->r.min, - Pt(defcwidth, defcheight))), "", 0); + cell_primitive(screen, rnope, "", 0); flushimage(display, 1); flushimage(display, 1); } @@ -455,11 +459,12 @@ mousecols(Mouse mv) int i; m = mouse2addr(mv.xy); if (mv.buttons == 1) { + unselect(); if (mstate == 0) { + draw_rows(); s.min = m; mstate = 2; } - unselect(); s.max = m; r = canonrect(s); for (i = r.min.x; i <= r.max.x; i++) { @@ -469,7 +474,6 @@ mousecols(Mouse mv) push(&selection.cols, col); } draw_columns(); - draw_rows(); draw_cells(); flushimage(display, 1); } @@ -484,11 +488,12 @@ mouserows(Mouse mv) int i; m = mouse2addr(mv.xy); if (mv.buttons == 1) { + unselect(); if (mstate == 0) { + draw_columns(); s.min = m; mstate = 2; } - unselect(); s.max = m; r = canonrect(s); for (i = r.min.y; i <= r.max.y; i++) { @@ -497,7 +502,6 @@ mouserows(Mouse mv) *row = i; push(&selection.rows, row); } - draw_columns(); draw_rows(); draw_cells(); flushimage(display, 1);