Repository navigation
Expand file tree
/
Copy pathtext.c
More file actions
510 lines (475 loc) · 21.9 KB
/
Copy pathtext.c
File metadata and controls
510 lines (475 loc) · 21.9 KB
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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
#include "data/struct/bt_tree.h"
#include "doc.h"
#include "files/buffer.h"
#include "types.h"
#include "uno.h"
#include "math/math.h"
#include "input_keycodes.h"
#include "draw/textdraw.h"
extern int print(char* fmt,...);
#define SPACES_PER_TAB 4
void uno_text_field_shift_cursor_node(document_node *node, i32 x_shift, i32 y_shift);
extern document_node* uno_find_node(document_node *node, int tag);
bt_tree uno_text_make_piece_tree(){
return bt_tree_create(sizeof(uno_text_piece), bt_balancing_rb);
}
tern uno_text_find_closest(void *ctx, bt_tree * tree, bt_node *node){
range_t *new_range = ctx;
uno_text_piece *piece = (uno_text_piece*)node->data;
size_t old_size = piece->range.size;
if (node->key == (i64)new_range->start) return 0;
if (node->key < (i64)new_range->start && node->key + old_size > new_range->start) return 0;
if (node->key >= (i64)new_range->start + (i64)new_range->size) return -1;
return 1;
}
i32 count_chars(string_slice sl, char c){
i32 count = 0;
for (u32 i = 0; i < sl.length; i++)
if (sl.data[i] == c) count++;
return count;
}
void pos_to_lin_col_tree(u32 pos, bt_tree *tree, text_field_info *info, i32 *lin, i32 *col){
if (!lin && !col) return;
bt_tree_traversal traverse = {
.tree = tree
};
*lin = 0; *col = 0;
i32 count = 0;
while (bt_tree_next(&traverse)){
uno_text_piece *piece = (uno_text_piece*)traverse.current->data;
if (count + piece->range.size >= pos){
string_slice sl = {info->children[piece->buffer_index]->buffer + piece->range.start, pos - count};
i32 nl_count = count_chars(sl,'\n');
i32 tab_count = count_chars(sl, '\t');
*lin += nl_count;
if (nl_count){
for (int i = 0; i < nl_count; i++)
sl = slice_seek_to(sl, '\n');
tab_count = count_chars(sl, '\t');
*col = sl.length + (tab_count*SPACES_PER_TAB);
} else
*col += (pos - count) + (tab_count*SPACES_PER_TAB);
return;
}
count += piece->range.size;
*lin += piece->nl_count;
string_slice sl = {info->children[piece->buffer_index]->buffer + piece->range.start, piece->range.size};
if (piece->nl_count){
for (int n = 0; n < piece->nl_count; n++){
sl = slice_seek_to(sl, '\n');
}
i32 tab_count = count_chars(sl, '\t');
*col = (i32)sl.length + (tab_count * SPACES_PER_TAB);
} else *col += piece->range.size + (piece->tab_count*SPACES_PER_TAB);
}
}
u32 lin_col_to_pos_tree(i32 line, i32 col, text_field_info *info, bt_tree *tree){
bt_tree_traversal traverse = {
.tree = tree
};
i32 cline = 0, ccol = 0;
i32 count = 0;
while (bt_tree_next(&traverse)){
uno_text_piece *piece = (uno_text_piece*)traverse.current->data;
if (cline + piece->nl_count < line){
cline += piece->nl_count;
} else {
if (cline + piece->nl_count == line){
if (piece->range.size - piece->nl_count < col){
print("Foujnd in piece %i",traverse.index);
return count + col;
}
} else {
string_slice sl = {info->children[piece->buffer_index]->buffer + piece->range.start, piece->range.size};
return count + lin_col_to_pos(line - cline, col, sl);
}
}
count += piece->range.size;
}
return count;
}
void uno_text_piece_shift_by_until(bt_tree *tree, i64 offset, bt_node *until){
bt_tree_traversal traversal = {
.tree = tree,
.backwards = true,
};
bt_node *node = 0;
while ((node = bt_tree_next(&traversal))){
node->key += offset;
if (node == until) break;
}
}
void uno_text_remove_from_piece(text_field_info *info, range_t range){
bt_node *existing_node = bt_tree_find_node(info->piece_tree, 0, &range, uno_text_find_closest);
print("Do we have anything in the %r range? %x",range,existing_node);
if (!existing_node) return;
uno_text_piece *old_piece = (uno_text_piece*)existing_node->data;
range_t old_range = old_piece->range;
old_range.start = existing_node->key;
//TODO: we could even have multiples
if (old_range.start == range.start && old_range.size == range.size){
bt_tree_remove(info->piece_tree, existing_node);
} else {
i64 offset = range.start-existing_node->key;
uno_text_piece lh = { .buffer_index = old_piece->buffer_index, .range = {.start = old_piece->range.start, .size = offset } };
string_slice lhs = {info->children[old_piece->buffer_index]->buffer + old_piece->range.start, offset};
lh.nl_count = count_chars(lhs,'\n');
lh.tab_count = count_chars(lhs, '\t');
bt_tree_insert(info->piece_tree, &lh, existing_node->key);
i64 new_size = old_piece->range.size-offset;
new_size -= range.size;
if (new_size > 0){
uno_text_piece rh = { .buffer_index = old_piece->buffer_index, .range = {.start = old_piece->range.start + offset + range.size, .size = new_size } };
string_slice rhs = { info->children[old_piece->buffer_index]->buffer + old_piece->range.start + offset + range.size, new_size };
rh.nl_count = count_chars(rhs,'\n');
rh.tab_count = count_chars(rhs, '\t');
bt_node *new_bound = bt_tree_insert(info->piece_tree, &rh, existing_node->key + offset);
} else if (new_size < 0){
uno_text_remove_from_piece(info, (range_t){ existing_node->key + offset, -new_size });
}
}
}
void uno_text_add_piece(text_field_info *info, uno_text_piece piece, i64 cursor_index){
if (info->cursor_count < (u64)cursor_index) return;
i64 position = info->cursors[cursor_index];
range_t new_range = {position,piece.range.size};
bt_node *existing_node = bt_tree_find_node(info->piece_tree, 0, &new_range, uno_text_find_closest);
if (existing_node){
uno_text_piece *old_piece = (uno_text_piece*)existing_node->data;
range_t old_range = old_piece->range;
// print("Found existing node %i - %r",existing_node->key,old_range);
if (existing_node->key < (i64)new_range.start && existing_node->key + (u64)old_range.size > (u64)new_range.start){
// print("Need to split");
i64 offset = position-existing_node->key;
uno_text_piece lh = { .buffer_index = old_piece->buffer_index, .range = {.start = old_piece->range.start, .size = offset } };
string_slice lhs = {info->children[old_piece->buffer_index]->buffer + old_piece->range.start, offset};
lh.nl_count = count_chars(lhs,'\n');
lh.tab_count = count_chars(lhs, '\t');
uno_text_piece rh = { .buffer_index = old_piece->buffer_index, .range = {.start = old_piece->range.start + offset, .size = old_piece->range.size - offset } };
string_slice rhs = {info->children[old_piece->buffer_index]->buffer + old_piece->range.start + offset, old_piece->range.size - offset};
rh.nl_count = count_chars(rhs,'\n');
rh.tab_count = count_chars(rhs, '\t');
//delete the node off the tree, save its key and piece
bt_tree_insert(info->piece_tree, &lh, existing_node->key);
bt_node *new_bound = bt_tree_insert(info->piece_tree, &rh, existing_node->key + offset);
uno_text_piece_shift_by_until(info->piece_tree, new_range.size, new_bound);
bt_tree_remove(info->piece_tree, existing_node);
} else {
uno_text_piece_shift_by_until(info->piece_tree, new_range.size, existing_node);
}
}
bt_tree_insert(info->piece_tree, &piece, position);
info->cursors[cursor_index] += piece.range.size;
}
void uno_text_piece_identity(text_field_info *info){
bt_reset(info->piece_tree);
info->total_size = 0;
for (u32 i = 0; i < info->cursor_count; i++)
info->cursors[i] = 0;
uno_text_add_piece(info,(uno_text_piece){
.buffer_index = 0,
.range = (range_t){0,info->content->buffer_size}
}, 0);
info->cursors[0] = 0;
}
void uno_text_field_scroll_in_line(document_node *node, bool begin){
if (!node || !node->ctx) return;
text_field_info *info = node->ctx;
buffer *content = info->content;
bool found = false;
if (begin){
for (u64 i = content->cursor; i > 0; i--)
if (((char*)content->buffer)[i-1] == '\n'){ found = true; content->cursor = i; break; }
if (!found) content->cursor = 0;
} else {
if (((char*)content->buffer)[content->cursor] == '\n'){ return; }
for (u64 i = content->cursor; i < content->buffer_size; i++)
if (((char*)content->buffer)[i+1] == '\n'){ found = true; content->cursor = i+1; break; }
if (!found) content->cursor = content->buffer_size;
}
content->cursor = clamp(content->cursor, 0, content->buffer_size);
uno_refresh();
}
bool uno_text_field_input(document_node *node, kbd_event event, u8 modifier){
if (!node || !node->ctx) return false;
text_field_info *info = node->ctx;
buffer *content = info->content;
if (!content || !content->buffer) return false;
if (event.key == KEY_ENTER && !info->multiline) return false;
if (event.type != KEY_PRESS && event.type != KEY_CONTINUE) return false;
if (event.key == KEY_HOME)
uno_text_field_scroll_in_line(node, true);
else if (event.key == KEY_END)
uno_text_field_scroll_in_line(node, false);
else if (((event.key >= KEY_RIGHT && event.key <= KEY_UP) || event.key == KEY_PAGEUP || event.key == KEY_PAGEDOWN )){
i32 x_shift = 0;
i32 y_shift = 0;
switch (event.key) {
case KEY_RIGHT: x_shift = 1; break;
case KEY_LEFT: x_shift = -1; break;
case KEY_DOWN: y_shift = 1; break;
case KEY_UP: y_shift = -1; break;
case KEY_PAGEDOWN:
// y_shift = (ctx.height/line_height)-1;
// offset.y -= ctx.height;
break;
case KEY_PAGEUP:
// y_shift = -(ctx.height/line_height)-1;
// offset.y += ctx.height;
break;
}
if (x_shift || y_shift)
uno_text_field_shift_cursor_node(node, x_shift, y_shift);
uno_refresh();
}
if (event.key == KEY_BACKSPACE){
if (info->selection.start || info->selection.end){
if (info->piece_tree && info->gap){
uno_text_remove_from_piece(info, (range_t){info->selection.start,info->selection.end-info->selection.start});
} else
buffer_delete(content, info->selection.end, info->selection.end-info->selection.start);
info->selection.start = 0;
info->selection.end = 0;
} else {
if (info->cursors && info->cursors[0] && info->piece_tree && info->gap){
uno_text_remove_from_piece(info, (range_t){info->cursors[0]-1, 1});
info->cursors[0] -= 1;
} else
buffer_delete(content, content->cursor, 1);
}
uno_refresh();
return true;
}
if (event.key == KEY_DELETE){
if (info->selection.start || info->selection.end){
if (info->piece_tree && info->gap){
uno_text_remove_from_piece(info, (range_t){info->selection.start, info->selection.end-info->selection.start});
} else
buffer_delete(content, info->selection.end, info->selection.end-info->selection.start);
info->selection.start = 0;
info->selection.end = 0;
} else {
if (info->piece_tree && info->gap){
uno_text_remove_from_piece(info, (range_t){info->cursors[0], 1});
} else
buffer_delete(content, content->cursor+1, 1);
}
uno_refresh();
return true;
}
if (event.key == KEY_TAB){
char *indent = "\t";
if (info->piece_tree && info->gap){
if (buffer_write_lim(info->gap, indent, 1)){
uno_text_add_piece(info,(uno_text_piece){
.buffer_index = 1,
.range = {info->gap->cursor-1,1},
.tab_count = 1
}, 0);
}
} else
buffer_write_to(content, indent, 1, content->cursor);
uno_refresh();
return true;
}
char c = hid_to_char(event.key, modifier, 0);
if (c){
if (info->piece_tree && info->gap){
if (buffer_write_lim(info->gap, &c, 1)){
uno_text_add_piece(info,(uno_text_piece){
.buffer_index = 1,
.range = {info->gap->cursor-1,1},
.nl_count = c == '\n'
}, 0);
}
} else {
buffer_write_to(content, &c, 1, content->cursor);
}
uno_refresh();
return true;
}
return false;
}
void uno_text_field_scroll_node(document_node *node, i32 x_shift, i32 y_shift);
bool uno_text_field_mouse(document_node *node, mouse_data data, u8 modifier){
bool res = false;
if (data.position.x < node->info.rect.point.x ||
data.position.x > node->info.rect.point.x + (i32)node->info.rect.size.width ||
data.position.y < node->info.rect.point.y ||
data.position.y > node->info.rect.point.y + (i32)node->info.rect.size.height) return false;
if (data.raw.scroll){
if (modifier & KEY_MOD_LSHIFT){
uno_text_field_scroll_node(node, data.raw.scroll, 0);
} else {
uno_text_field_scroll_node(node, 0, data.raw.scroll);
}
uno_refresh();
res = true;
}
if (!mouse_button_down(&data, 0)) return res;
text_field_info *info = node->ctx;
if (!info) return false;
buffer *content = info->content;
if (!content || !content->buffer) return res;
i32 x = (data.position.x-(node->info.offset ? node->info.offset->x : 0))/fb_get_char_size(text_to_scale(node->info.type));
i32 y = (data.position.y-(node->info.offset ? node->info.offset->y : 0))/(fb_line_height(text_to_scale(node->info.type)));//TODO: line padding should be customizable
i32 selection = -1;
if (info->piece_tree){
selection = lin_col_to_pos_tree(y, x, info, info->piece_tree);
}
selection = lin_col_to_pos(y, x, (string_slice){content->buffer,content->buffer_size});
if (selection < 0) return true;
if (modifier & KEY_MOD_LSHIFT){
info->selection.start = min(content->cursor,selection);
info->selection.end = max(content->cursor,selection);
} else {
text_format fmt = get_current_format(selection, (text_format){}, node->info.text_formatting);
if (fmt.link.length && info->link_manager){
info->link_manager(info->ctx, slice_from_string(fmt.link));
}
info->selection.start = 0;
info->selection.end = 0;
}
content->cursor = selection;
uno_refresh();
return true;
}
void* uno_text_field_copy(document_node* node, size_t *out_size){
if (!node || !node->ctx || !out_size) return false;
text_field_info *info = node->ctx;
buffer *content = info->content;
if (!content || !content->buffer) return false;
*out_size = info->selection.end-info->selection.start;
return &((char*)info->content->buffer)[info->selection.start];
}
bool uno_text_field_paste(document_node* node, void* buf, size_t size){
if (!buf || !size) return false;
if (!node || !node->ctx) return false;
text_field_info *info = node->ctx;
buffer *content = info->content;
if (!content || !content->buffer) return false;
for (size_t i = 0; i < size-1; i++)
if (!is_printable(((char*)buf)[i]))
return false;
buffer_write_to(content, buf, size, content->cursor);
uno_refresh();
return true;
}
document_node* uno_text_field(int tag, node_info info, text_field_info *text_info){
info.general_type = doc_gen_text;
if (info.type == doc_text_none) info.type = doc_text_footnote;
if (!((info.fg_color >> 24) & 0xFF)) info.fg_color |= 0xFF << 24;
info.offset = text_info->offset;
node_info text_node_info = info;
info.general_type = doc_gen_text;
document_node *node = uno_create_view(text_node_info, text_info->content && text_info->content->buffer && text_info->content->buffer_size ? (string_slice){.data = text_info->content->buffer, .length = text_info->content->buffer_size } : text_info->placeholder);
node->input.keyboard_input = uno_text_field_input;
node->input.mouse_input = uno_text_field_mouse;
node->input.on_copy = uno_text_field_copy;
node->input.on_paste = uno_text_field_paste;
node->info.tag = tag;
node->ctx = text_info;
return node;
}
void uno_text_field_scroll_node(document_node *node, i32 x_shift, i32 y_shift){
text_field_info *info = node->ctx;
if (!info) return;
buffer *content = info->content;
if (!content || !content->buffer) return;
u8 cw = fb_line_height(text_to_scale(node->info.type));
u8 lh = fb_line_height(text_to_scale(node->info.type));
if (x_shift != 0){
if (info->offset){
if (info->offset->x + x_shift > 0) info->offset->x = 0;
else info->offset->x += x_shift * cw;
}
}
if (y_shift != 0){
if (info->offset){
if (info->offset->y + y_shift > 0) info->offset->y = 0;
else info->offset->y += y_shift * lh;
}
}
}
void uno_text_field_scroll(int tag, i32 x_shift, i32 y_shift){
document_node *node = uno_find_node(default_doc_data.root, tag);
if (!node) return;
if (node->info.general_type != doc_gen_text) return;
uno_text_field_scroll_node(node, x_shift, y_shift);
}
void uno_text_field_shift_cursor_node(document_node *node, i32 x_shift, i32 y_shift){
text_field_info *info = node->ctx;
if (!info) return;
buffer *content = info->content;
if (!content || !content->buffer) return;
u8 cw = fb_line_height(text_to_scale(node->info.type));
u8 lh = fb_line_height(text_to_scale(node->info.type));
if (info->piece_tree){
//TODO: The logic for this can be improved by only calculating through the delta, not the full file
for (int i = info->cursor_count-1; i >= 0; i--){
if (x_shift){
if ((i64)info->cursors[i] + x_shift < 0) info->cursors[i] = 0;
else info->cursors[i] += x_shift;
i32 lin, col = 0;
if (info->piece_tree){
pos_to_lin_col_tree(info->cursors[i], info->piece_tree, info, &lin, &col);
} else {
string_slice slice = (string_slice){content->buffer,content->buffer_size};
pos_to_lin_col(info->cursors[i], slice, &lin, &col);
}
i32 dis = col + ((info->offset ? info->offset->x : 0)/cw) - (node->info.rect.size.width/cw);
if (dis >= 0) uno_text_field_scroll_node(node, -dis, 0);
else if (-dis > (i32)node->info.rect.size.width/cw) uno_text_field_scroll_node(node, -((node->info.rect.size.width/cw)+dis), 0);
}
if (y_shift){
i32 lin, col = 0;
if (info->piece_tree){
pos_to_lin_col_tree(info->cursors[i], info->piece_tree, info, &lin, &col);
// print("Start at %i,%i",lin,col);
if (lin + y_shift < 0) lin = 0;
else lin += y_shift;
info->cursors[i] = lin_col_to_pos_tree(lin, col, info, info->piece_tree);
// print("Lin %i Col %i",lin,col);
} else {
string_slice slice = (string_slice){content->buffer,content->buffer_size};
pos_to_lin_col(info->cursors[i], slice, &lin, &col);
if (lin + y_shift < 0) lin = 0;
else lin += y_shift;
info->cursors[i] = lin_col_to_pos(lin, col, slice);
}
i32 dis = lin + ((info->offset ? info->offset->y : 0)/lh) - (node->info.rect.size.height/lh);
if (dis >= 0) uno_text_field_scroll_node(node, 0, -dis);
else if (-dis > (i32)node->info.rect.size.height/lh) uno_text_field_scroll_node(node, 0, -((node->info.rect.size.height/lh)+dis));
}
}
content->cursor = clamp(content->cursor, 0, content->buffer_size);
} else {
if (x_shift){
if ((i64)content->cursor + x_shift < 0) content->cursor = 0;
else content->cursor += x_shift;
i32 lin, col = 0;
string_slice slice = (string_slice){content->buffer,content->buffer_size};
pos_to_lin_col(content->cursor, slice, &lin, &col);
i32 dis = col + ((info->offset ? info->offset->x : 0)/cw) - (node->info.rect.size.width/cw);
if (dis >= 0) uno_text_field_scroll_node(node, -dis, 0);
else if (-dis > (i32)node->info.rect.size.width/cw) uno_text_field_scroll_node(node, -((node->info.rect.size.width/cw)+dis), 0);
}
if (y_shift){
i32 lin, col = 0;
string_slice slice = (string_slice){content->buffer,content->buffer_size};
pos_to_lin_col(content->cursor, slice, &lin, &col);
if (lin + y_shift < 0) lin = 0;
else lin += y_shift;
content->cursor = lin_col_to_pos(lin, col, slice);
i32 dis = lin + ((info->offset ? info->offset->y : 0)/lh) - (node->info.rect.size.height/lh);
if (dis >= 0) uno_text_field_scroll_node(node, 0, -dis);
else if (-dis > (i32)node->info.rect.size.height/lh) uno_text_field_scroll_node(node, 0, -((node->info.rect.size.height/lh)+dis));
}
}
}
void uno_text_field_shift_cursor(int tag, i32 x_shift, i32 y_shift){
document_node *node = uno_find_node(default_doc_data.root, tag);
if (!node) return;
if (node->info.general_type != doc_gen_text) return;
uno_text_field_shift_cursor_node(node, x_shift, y_shift);
}