summaryrefslogtreecommitdiff
path: root/view.c
blob: 0f2c3f63106300e0553288a45e8b69f197ddabcc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
#include <string.h>

#include "editor.h"
#include "redr.h"
#include "mem.h"

#include "vector.h"		/* children */

#define MIN(a, b) ((a) < (b))? (a) : (b)

struct view;			/* for defining vector first */

DEFINE_VECTOR(Vec_sviewp, struct view *);

struct view_ops {
	void (*draw)(struct view *);
	void (*destroy)(struct view *);
};

enum view_type {
	VIEW_NONE,
	VIEW_ROOT,
	VIEW_EB
};

struct view {
	enum view_type type;
	struct redr_ctx *window;	/* context of window */

	/* tree */
	struct view *parent;
	Vec_sviewp *children;

	/* geometry */
	float posx;
	float posy;
	float w;
	float h;

	/* style */
	float font_size;
	float font_spacing;

	/* action */
	const struct view_ops *ops;
	int dirty:1;		/* re-draw only when dirty */
};

static inline void draw_textn(struct redr_ctx *window, int x, int y, int size,
			      struct redr_color c,
			      const char *text, size_t len) {
	char line[4096];
	size_t rlen;
	if ((rlen = len) > sizeof line - 1)
		rlen = sizeof line - 1;
	memcpy(line, text, len);
	line[rlen] = '\0';
	redr_draw_text(window, line, x, y, size, c);
}

static void draw_root(struct view *v) {
	redr_clear_background(v->window, WHITE);
}

const struct view_ops root_op = {
	.draw = draw_root,
	.destroy = NULL,
};

void view_init_root(struct view **root, struct redr_ctx *window) {
	struct view *nroot = mem_malloc(sizeof(struct view));

	nroot->type = VIEW_ROOT;
	nroot->window = window;

	nroot->parent = NULL;
	Vec_sviewp_init(&nroot->children);

	nroot->posx = 0;
	nroot->posy = 0;
	nroot->w = redr_width(window);
	nroot->h = redr_height(window);

	nroot->font_size = 0;
	nroot->font_spacing = 0;

	nroot->ops = &root_op;
	nroot->dirty = 0;

	*root = nroot;
}

size_t view_child_num(struct view *vw) {
	return Vec_sviewp_len(vw->children);
}

void view_free_under(struct view *root) {
	Vec_sviewp *s1, *s2;
	Vec_sviewp_init(&s1);
	Vec_sviewp_init(&s2);

	/* DFS search */
	Vec_sviewp_push(s1, root);
	size_t len;
	while ((len = Vec_sviewp_len(s1)) > 0) {
		struct view *vw;

		vw = Vec_sviewp_get(s1, len - 1);
		Vec_sviewp_pop(s1);
		Vec_sviewp_push(s2, vw);

		size_t child_num = view_child_num(vw);
		for (size_t i = 0; i < child_num; i++)
			Vec_sviewp_push(s1, Vec_sviewp_get(vw->children, i));
	}
	Vec_sviewp_free(s1);

	/* free with reverse order of DFS */
	while ((len = Vec_sviewp_len(s2)) > 0) {
		struct view *vw;

		vw = Vec_sviewp_get(s2, len - 1);
		Vec_sviewp_pop(s2);

		if (vw->ops && vw->ops->destroy)
			vw->ops->destroy(vw);

		Vec_sviewp_free(vw->children);
		mem_free(vw);
	}
	Vec_sviewp_free(s2);
}

void view_draw_under(struct view *root) {
	Vec_sviewp *stack;
	Vec_sviewp_init(&stack);
	Vec_sviewp_push(stack, root);

	size_t len;
	while ((len = Vec_sviewp_len(stack)) > 0) {
		struct view *vw;

		vw = Vec_sviewp_get(stack, len - 1);
		Vec_sviewp_pop(stack);

		if (vw->ops && vw->ops->draw)
			vw->ops->draw(vw);

		size_t child_num = view_child_num(vw);
		for (size_t i = 0; i < child_num; i++)
			Vec_sviewp_push(stack, Vec_sviewp_get(vw->children, i));
	}
	Vec_sviewp_free(stack);
}

struct view_eb {
	struct view base;
	struct ed_buf *eb;
	float scroll;
};

static void view_eb_draw(struct view *veb) {
	struct ed_buf *eb = ((struct view_eb *)veb)->eb;
	float scroll = ((struct view_eb *)veb)->scroll;

	float font_size = veb->font_size;
	float spacing = veb->font_spacing;
	float font_height = redr_measure_text(veb->window, "a", font_size, spacing).y;

	struct redr_ctx *window =  veb->window;
	const size_t linenum_to_draw = MIN(eb_get_line_num(eb), veb->h / font_height);
	const float padding = 10.f;

#ifdef CONFIG_DEBUG
	log_printf(RED_LOG_DEBUG,
		   "[draw]: %lu lines ------\n", linenum_to_draw);
	log_printf(RED_LOG_DEBUG,
		   "[draw]: cursor: (%lu, %lu) ------\n",
		   eb_get_cur_col(eb), eb_get_cur_row(eb));
#endif

	for (size_t i = scroll; i <= linenum_to_draw + scroll; i++) {
		struct slice sl = {};
		eb_get_line_slice(eb, i, &sl);

#ifdef CONFIG_DEBUG
		log_printf(RED_LOG_DEBUG,
			   "[draw]: line %lu, ptr=%p, len=%lu\n",
			   i, sl.ptr, sl.len);
#endif
		float text_posx = veb->posx + padding;
		float text_posy = veb->posy + padding + font_size * i;
		size_t len = MIN((size_t)veb->w, sl.len);

		if (i != eb_get_cur_row(eb)) {
			draw_textn(veb->window,
				   text_posx, text_posy,
				   font_size, BLACK, sl.ptr, len);
		} else { /* draw cursor */

#define BUFSIZE 4096
			char buf[BUFSIZE];
			size_t cur_col = eb_get_cur_col(eb);
			strncpy(buf, sl.ptr == NULL? "" : sl.ptr, MIN(sl.len, BUFSIZE));
			if (sl.len < 4096)
				buf[sl.len] = '\0';
			if (buf[cur_col] == '\0')
				buf[cur_col + 1] = '\0';
			buf[cur_col] = '_';
			redr_draw_text(window, buf, text_posx, text_posy,
				       font_size, BLACK);
		}
	}
}

const struct view_ops view_eb_ops = {
	.draw = view_eb_draw,
	.destroy = NULL
};

struct view_eb *view_eb_create_under(struct view *upper,
				     struct ed_buf *eb,
				     float posx,
				     float posy,
				     float w,
				     float h,
				     float font_size,
				     float font_spacing,
				     float scroll) {
	struct view_eb *veb = mem_malloc(sizeof *veb);


	((struct view *)veb)->type = VIEW_EB;
	((struct view *)veb)->window = upper->window;

	((struct view *)veb)->parent = upper;
	Vec_sviewp_init(&((struct view *)veb)->children);

	((struct view *)veb)->posx = posx;
	((struct view *)veb)->posy = posy;
	((struct view *)veb)->w = w;
	((struct view *)veb)->h = h;

	((struct view *)veb)->font_size = font_size;
	((struct view *)veb)->font_spacing = font_spacing;

	((struct view *)veb)->ops = &view_eb_ops;
	((struct view *)veb)->dirty = 0;

	veb->eb = eb;
	veb->scroll = scroll;

	Vec_sviewp_push(upper->children, (struct view *)veb);

	return veb;
}