commit 0edd75f5c35b5ebc24818e5558ee67c0c6058586
parent d398300d47a0564641f8c0c628f8682d27ebcf84
Author: Pavel Renev <an2qzavok@gmail.com>
Date: Mon, 12 Apr 2021 15:05:24 +0000
some drawing logic
Diffstat:
M | devfs.c | | | 2 | ++ |
M | richterm.c | | | 85 | +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-------------- |
M | richterm.h | | | 17 | +++++++---------- |
A | ui.h | | | 34 | ++++++++++++++++++++++++++++++++++ |
4 files changed, 113 insertions(+), 25 deletions(-)
diff --git a/devfs.c b/devfs.c
@@ -4,6 +4,8 @@
#include <thread.h>
#include <9p.h>
+#include "richterm.h"
+
void
devfs_read(Req *r)
{
diff --git a/richterm.c b/richterm.c
@@ -9,31 +9,63 @@
#include <9p.h>
#include "richterm.h"
+#include "ui.h"
+
+typedef struct Rich Rich;
+struct Rich {
+ Object *obj;
+ long count;
+ Page page;
+};
+
+Page generatepage(Rectangle, Rich *);
+
+void
+usage(void)
+{
+ fprint(2, "usage: %s [-D] [cmd]\n", argv0);
+ threadexitsall("usage");
+}
void
threadmain(int argc, char **argv)
{
+ Rich rich;
Mousectl *mctl;
int rv[2];
Mouse mv;
- View view;
ARGBEGIN{
case 'D':
chatty9p++;
break;
default:
usage();
- }ARGEND
+ } ARGEND
if (initdraw(0, 0, "richterm") < 0)
sysfatal("initdraw failed: %r");
- if (initview(&view) < 0) sysfatal("initview failed: %r);
- draw(screen, screen->r, display->white, nil, ZP);
+
+ //if (initview(&view) < 0) sysfatal("initview failed: %r);
+ rich.obj = malloc(sizeof(Object) * 4);
+ rich.count = 4;
+ rich.obj[0] = (Object){"text", nil, "hello ", strlen("hello ")};
+ rich.obj[1] = (Object){"text", nil, "world", strlen("world")};
+ rich.obj[2] = (Object){"text", nil, "!", strlen("!")};
+ rich.obj[3] = (Object){"text", nil, "\n", strlen("\n")};
+
+ rich.page = generatepage(screen->r, &rich);
+
+ draw(screen, screen->r, display->black, nil, ZP);
+
+ int i;
+ for (i = 0; i < 4; i++){
+ drawview(screen, &rich.page.view[i]);
+ }
flushimage(display, 1);
if ((mctl = initmouse(nil, screen)) == nil)
sysfatal("initmouse failed: %r");
- if (initdevfs() < 0) sysfatal("initdevfs failed: %r");
+ // if (initdevfs() < 0) sysfatal("initdevfs failed: %r");
// init /mnt fs for exposing internals
// launch a subprocess from cmd passed on args
@@ -52,7 +84,7 @@ threadmain(int argc, char **argv)
case RESIZE:
if (getwindow(display, Refnone) < 0)
sysfatal("resize failed: %r");
- draw(screen, screen->r, display->white, nil, ZP);
+ draw(screen, screen->r, display->black, nil, ZP);
flushimage(display, 1);
break;
case NONE:
@@ -62,17 +94,40 @@ threadmain(int argc, char **argv)
}
void
-usage(void)
+drawview(Image *dst, View *v)
{
- fprint(2, "usage: %s [-D] [cmd]\n", argv0);
- threadexitsall("usage");
+ int w, n;
+ char buf[4096];
+ w = Dx(v->r);
+ draw(dst, v->r, display->white, nil, ZP);
+ for (n = 0; stringnwidth(font, v->dp, n) < w; n++)
+ if (n >= 4096) break;
+ memcpy(buf, v->dp, n);
+ buf[n] = '\0';
+ string(dst, v->r.min, display->black, ZP, font, buf);
}
-int
-initview(View *view)
+Page
+generatepage(Rectangle r, Rich *rich)
{
- char *m = "Welcome to RichTerm!";
- view->count = 1;
- view->obj = malloc(sizeof(Obj));
- view->obj[0] = (Obj){ "text", nil, m, strlen(m) };
+ int i;
+ Point pt;
+ Page page;
+ page.view = nil;
+ page.count = 0;
+ pt = r.min;
+ for (i = 0; i < rich->count; i++) {
+ Object *obj;
+ View view;
+ obj = &rich->obj[i];
+ page.count++;
+ page.view = realloc(page.view, sizeof(View) * (page.count));
+ view.obj = obj;
+ view.page = &page;
+ view.dp = obj->data;
+ view.r = Rpt(pt, addpt(pt, Pt(100,20)));
+ page.view[page.count -1] = view;
+ pt.y = view.r.max.y;
+ }
+ return page;
}
diff --git a/richterm.h b/richterm.h
@@ -1,17 +1,14 @@
-typedef struct Obj Obj;
-struct Obj {
+/*
+ * Object is an atom of internal data structure.
+ * There should be a list of Objects, manipulatable
+ * either through GUI or file system.
+ */
+typedef struct Object Object;
+struct Object {
char *type;
char *opts;
char *data;
long count;
};
-typedef struct View View;
-struct View {
- Obj *obj;
- long count;
-};
-
-void usage(void);
-int initview(View *);
int initdevfs(void);
diff --git a/ui.h b/ui.h
@@ -0,0 +1,33 @@
+/*
+ * View is an atom of our main GUI screen.
+ * It represents a small "window" through which
+ * a part of object can be seen.
+ * dp points to obj->data+x, so we can display
+ * only a part of the object (used for wrapped
+ * lines and such).
+ * r holds data on where view fits on screen.
+ * page holds pointer to page view belongs to.
+ */
+typedef struct View View;
+
+/*
+ * Page is a collection of views.
+ */
+typedef struct Page Page;
+
+struct View {
+ Object *obj;
+ char *dp;
+ Rectangle r;
+ Page *page;
+};
+
+struct Page {
+ View *view;
+ long count;
+ Point scroll;
+ Point max;
+};
+
+void drawview(Image *, View *);
+Point viewsize(View *);
+\ No newline at end of file