Repository navigation
Expand file tree
/
Copy pathtext.c
More file actions
327 lines (299 loc) · 13.1 KB
/
Copy pathtext.c
File metadata and controls
327 lines (299 loc) · 13.1 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
#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"
#include "syscalls/syscalls.h"
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;
}
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_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 } };
uno_text_piece rh = { .buffer_index = old_piece->buffer_index, .range = {.start = old_piece->range.start + offset, .size = old_piece->range.size - offset } };
//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){
buffer_delete(content, info->selection.end, info->selection.end-info->selection.start);
info->selection.start = 0;
info->selection.end = 0;
} else
buffer_delete(content, content->cursor, 1);
uno_refresh();
return true;
}
if (event.key == KEY_DELETE){
if (info->selection.start || info->selection.end){
buffer_delete(content, info->selection.end, info->selection.end-info->selection.start);
info->selection.start = 0;
info->selection.end = 0;
} else {
buffer_delete(content, content->cursor+1, 1);
}
uno_refresh();
return true;
}
if (event.key == KEY_TAB){
char *indent = "\t";
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}
}, 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
u32 selection = lin_col_to_pos(y, x, (string_slice){content->buffer,content->buffer_size});
if (modifier & KEY_MOD_LSHIFT){
info->selection.start = min(content->cursor,selection);
info->selection.end = max(content->cursor,selection);
} else {
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 (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));
}
content->cursor = clamp(content->cursor, 0, content->buffer_size);
}
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);
}