Nuklear
This is a minimal-state, immediate-mode graphical user interface toolkit written in ANSI C and licensed under public domain. It was designed as a simple embeddable user interface for application and does not have any dependencies, a default render backend or OS window/input handling but instead provides a highly modular, library-based approach, with simple input state for input and draw commands describing primitive shapes as output. So instead of providing a layered library that tries to abstract over a number of platform and render backends, it focuses only on the actual UI.
 
Loading...
Searching...
No Matches
nuklear_text_editor.c
1#include "nuklear.h"
2#include "nuklear_internal.h"
3
4/* ===============================================================
5 *
6 * TEXT EDITOR
7 *
8 * ===============================================================*/
9/* stb_textedit.h - v1.8 - public domain - Sean Barrett */
11 float x,y; /* position of n'th character */
12 float height; /* height of line */
13 int first_char, length; /* first char of row, and length */
14 int prev_first; /*_ first char of previous row */
15};
16
18 float x0,x1;
19 /* starting x location, end x location (allows for align=right, etc) */
20 float baseline_y_delta;
21 /* position of baseline relative to previous row's baseline*/
22 float ymin,ymax;
23 /* height of row above and below baseline */
24 int num_chars;
25};
26
27/* forward declarations */
28NK_INTERN void nk_textedit_makeundo_delete(struct nk_text_edit*, int, int);
29NK_INTERN void nk_textedit_makeundo_insert(struct nk_text_edit*, int, int);
30NK_INTERN void nk_textedit_makeundo_replace(struct nk_text_edit*, int, int, int);
31#define NK_TEXT_HAS_SELECTION(s) ((s)->select_start != (s)->select_end)
32
33NK_INTERN float
34nk_textedit_get_width(const struct nk_text_edit *edit, int line_start, int char_id,
35 const struct nk_user_font *font)
36{
37 int len = 0;
38 nk_rune unicode = 0;
39 const char *str = nk_str_at_const(&edit->string, line_start + char_id, &unicode, &len);
40 return font->width(font->userdata, font->height, str, len);
41}
42NK_INTERN void
43nk_textedit_layout_row(struct nk_text_edit_row *r, struct nk_text_edit *edit,
44 int line_start_id, float row_height, const struct nk_user_font *font)
45{
46 int l;
47 int glyphs = 0;
48 nk_rune unicode;
49 const char *remaining;
50 int len = nk_str_len_char(&edit->string);
51 const char *end = nk_str_get_const(&edit->string) + len;
52 const char *text = nk_str_at_const(&edit->string, line_start_id, &unicode, &l);
53 const struct nk_vec2 size = nk_text_calculate_text_bounds(font,
54 text, (int)(end - text), row_height, &remaining, 0, &glyphs, NK_STOP_ON_NEW_LINE);
55
56 r->x0 = 0.0f;
57 r->x1 = size.x;
58 r->baseline_y_delta = size.y;
59 r->ymin = 0.0f;
60 r->ymax = size.y;
61 r->num_chars = glyphs;
62}
63NK_INTERN int
64nk_textedit_locate_coord(struct nk_text_edit *edit, float x, float y,
65 const struct nk_user_font *font, float row_height)
66{
67 struct nk_text_edit_row r;
68 int n = edit->string.len;
69 float base_y = 0, prev_x;
70 int i=0, k;
71
72 r.x0 = r.x1 = 0;
73 r.ymin = r.ymax = 0;
74 r.num_chars = 0;
75
76 /* search rows to find one that straddles 'y' */
77 while (i < n) {
78 nk_textedit_layout_row(&r, edit, i, row_height, font);
79 if (r.num_chars <= 0)
80 return n;
81
82 if (i==0 && y < base_y + r.ymin)
83 return 0;
84
85 if (y < base_y + r.ymax)
86 break;
87
88 i += r.num_chars;
89 base_y += r.baseline_y_delta;
90 }
91
92 /* below all text, return 'after' last character */
93 if (i >= n)
94 return n;
95
96 /* check if it's before the beginning of the line */
97 if (x < r.x0)
98 return i;
99
100 /* check if it's before the end of the line */
101 if (x < r.x1) {
102 /* search characters in row for one that straddles 'x' */
103 k = i;
104 prev_x = r.x0;
105 for (i=0; i < r.num_chars; ++i) {
106 float w = nk_textedit_get_width(edit, k, i, font);
107 if (x < prev_x+w) {
108 if (x < prev_x+w/2)
109 return k+i;
110 else return k+i+1;
111 }
112 prev_x += w;
113 }
114 /* shouldn't happen, but if it does, fall through to end-of-line case */
115 }
116
117 /* if the last character is a newline, return that.
118 * otherwise return 'after' the last character */
119 if (nk_str_rune_at(&edit->string, i+r.num_chars-1) == '\n')
120 return i+r.num_chars-1;
121 else return i+r.num_chars;
122}
123NK_LIB void
124nk_textedit_click(struct nk_text_edit *state, float x, float y,
125 const struct nk_user_font *font, float row_height)
126{
127 /* API click: on mouse down, move the cursor to the clicked location,
128 * and reset the selection */
129 state->cursor = nk_textedit_locate_coord(state, x, y, font, row_height);
130 state->select_start = state->cursor;
131 state->select_end = state->cursor;
132 state->has_preferred_x = 0;
133}
134NK_LIB void
135nk_textedit_drag(struct nk_text_edit *state, float x, float y,
136 const struct nk_user_font *font, float row_height)
137{
138 /* API drag: on mouse drag, move the cursor and selection endpoint
139 * to the clicked location */
140 int p = nk_textedit_locate_coord(state, x, y, font, row_height);
141 if (state->select_start == state->select_end)
142 state->select_start = state->cursor;
143 state->cursor = state->select_end = p;
144}
145NK_INTERN void
146nk_textedit_find_charpos(struct nk_text_find *find, struct nk_text_edit *state,
147 int n, int single_line, const struct nk_user_font *font, float row_height)
148{
149 /* find the x/y location of a character, and remember info about the previous
150 * row in case we get a move-up event (for page up, we'll have to rescan) */
151 struct nk_text_edit_row r;
152 int prev_start = 0;
153 int z = state->string.len;
154 int i=0, first;
155
156 nk_zero_struct(r);
157 if (n == z) {
158 /* if it's at the end, then find the last line -- simpler than trying to
159 explicitly handle this case in the regular code */
160 nk_textedit_layout_row(&r, state, 0, row_height, font);
161 if (single_line) {
162 find->first_char = 0;
163 find->length = z;
164 } else {
165 while (i < z) {
166 prev_start = i;
167 i += r.num_chars;
168 nk_textedit_layout_row(&r, state, i, row_height, font);
169 }
170
171 find->first_char = i;
172 find->length = r.num_chars;
173 }
174 find->x = r.x1;
175 find->y = r.ymin;
176 find->height = r.ymax - r.ymin;
177 find->prev_first = prev_start;
178 return;
179 }
180
181 /* search rows to find the one that straddles character n */
182 find->y = 0;
183
184 for(;;) {
185 nk_textedit_layout_row(&r, state, i, row_height, font);
186 if (n < i + r.num_chars) break;
187 prev_start = i;
188 i += r.num_chars;
189 find->y += r.baseline_y_delta;
190 }
191
192 find->first_char = first = i;
193 find->length = r.num_chars;
194 find->height = r.ymax - r.ymin;
195 find->prev_first = prev_start;
196
197 /* now scan to find xpos */
198 find->x = r.x0;
199 for (i=0; first+i < n; ++i)
200 find->x += nk_textedit_get_width(state, first, i, font);
201}
202NK_INTERN void
203nk_textedit_clamp(struct nk_text_edit *state)
204{
205 /* make the selection/cursor state valid if client altered the string */
206 int n = state->string.len;
207 if (NK_TEXT_HAS_SELECTION(state)) {
208 if (state->select_start > n) state->select_start = n;
209 if (state->select_end > n) state->select_end = n;
210 /* if clamping forced them to be equal, move the cursor to match */
211 if (state->select_start == state->select_end)
212 state->cursor = state->select_start;
213 }
214 if (state->cursor > n) state->cursor = n;
215}
216NK_API void
217nk_textedit_delete(struct nk_text_edit *state, int where, int len)
218{
219 /* delete characters while updating undo */
220 nk_textedit_makeundo_delete(state, where, len);
221 nk_str_delete_runes(&state->string, where, len);
222 state->has_preferred_x = 0;
223}
224NK_API void
225nk_textedit_delete_selection(struct nk_text_edit *state)
226{
227 /* delete the section */
228 nk_textedit_clamp(state);
229 if (NK_TEXT_HAS_SELECTION(state)) {
230 if (state->select_start < state->select_end) {
231 nk_textedit_delete(state, state->select_start,
232 state->select_end - state->select_start);
233 state->select_end = state->cursor = state->select_start;
234 } else {
235 nk_textedit_delete(state, state->select_end,
236 state->select_start - state->select_end);
237 state->select_start = state->cursor = state->select_end;
238 }
239 state->has_preferred_x = 0;
240 }
241}
242NK_INTERN void
243nk_textedit_sortselection(struct nk_text_edit *state)
244{
245 /* canonicalize the selection so start <= end */
246 if (state->select_end < state->select_start) {
247 int temp = state->select_end;
248 state->select_end = state->select_start;
249 state->select_start = temp;
250 }
251}
252NK_INTERN void
253nk_textedit_move_to_first(struct nk_text_edit *state)
254{
255 /* move cursor to first character of selection */
256 if (NK_TEXT_HAS_SELECTION(state)) {
257 nk_textedit_sortselection(state);
258 state->cursor = state->select_start;
259 state->select_end = state->select_start;
260 state->has_preferred_x = 0;
261 }
262}
263NK_INTERN void
264nk_textedit_move_to_last(struct nk_text_edit *state)
265{
266 /* move cursor to last character of selection */
267 if (NK_TEXT_HAS_SELECTION(state)) {
268 nk_textedit_sortselection(state);
269 nk_textedit_clamp(state);
270 state->cursor = state->select_end;
271 state->select_start = state->select_end;
272 state->has_preferred_x = 0;
273 }
274}
275NK_INTERN int
276nk_is_word_boundary( struct nk_text_edit *state, int idx)
277{
278 int len;
279 nk_rune c;
280 if (idx < 0) return 1;
281 if (!nk_str_at_rune(&state->string, idx, &c, &len)) return 1;
282#ifndef NK_IS_WORD_BOUNDARY
283 return (c == ' ' || c == '\t' || c == '\n' || c == '\r' || c == '\f' ||
284 c == '\v' || c == 0x3000);
285#else
286 return NK_IS_WORD_BOUNDARY(c);
287#endif
288}
289NK_INTERN int
290nk_textedit_move_to_word_previous(struct nk_text_edit *state)
291{
292 int c = state->cursor - 1;
293 if (c > 0) {
294 if (nk_is_word_boundary(state, c)) {
295 while (c > 0 && nk_is_word_boundary(state, --c));
296 }
297 while (!nk_is_word_boundary(state, --c));
298 c++;
299 } else {
300 return 0;
301 }
302
303 return c;
304}
305NK_INTERN int
306nk_textedit_move_to_word_next(struct nk_text_edit *state)
307{
308 const int len = state->string.len;
309 int c = state->cursor;
310 if (c < len) {
311 if (!nk_is_word_boundary(state, c)) {
312 while (c < len && !nk_is_word_boundary(state, ++c));
313 }
314 while (c < len && nk_is_word_boundary(state, ++c));
315 } else {
316 return len;
317 }
318
319 return c;
320}
321NK_INTERN void
322nk_textedit_prep_selection_at_cursor(struct nk_text_edit *state)
323{
324 /* update selection and cursor to match each other */
325 if (!NK_TEXT_HAS_SELECTION(state))
326 state->select_start = state->select_end = state->cursor;
327 else state->cursor = state->select_end;
328}
329NK_API nk_bool
330nk_textedit_cut(struct nk_text_edit *state)
331{
332 /* API cut: delete selection */
333 if (state->mode == NK_TEXT_EDIT_MODE_VIEW)
334 return 0;
335 if (NK_TEXT_HAS_SELECTION(state)) {
336 nk_textedit_delete_selection(state); /* implicitly clamps */
337 state->has_preferred_x = 0;
338 return 1;
339 }
340 return 0;
341}
342NK_API nk_bool
343nk_textedit_paste(struct nk_text_edit *state, char const *ctext, int len)
344{
345 /* API paste: replace existing selection with passed-in text */
346 int glyphs;
347 const char *text = (const char *) ctext;
348 if (state->mode == NK_TEXT_EDIT_MODE_VIEW) return 0;
349
350 /* if there's a selection, the paste should delete it */
351 nk_textedit_clamp(state);
352 nk_textedit_delete_selection(state);
353
354 /* try to insert the characters */
355 glyphs = nk_utf_len(ctext, len);
356 if (nk_str_insert_text_char(&state->string, state->cursor, text, len)) {
357 nk_textedit_makeundo_insert(state, state->cursor, glyphs);
358 state->cursor += len;
359 state->has_preferred_x = 0;
360 return 1;
361 }
362 /* remove the undo since we didn't actually insert the characters */
363 if (state->undo.undo_point)
364 --state->undo.undo_point;
365 return 0;
366}
367NK_API void
368nk_textedit_text(struct nk_text_edit *state, const char *text, int total_len)
369{
370 nk_rune unicode;
371 int glyph_len;
372 int text_len = 0;
373
374 NK_ASSERT(state);
375 NK_ASSERT(text);
376 if (!text || !total_len || state->mode == NK_TEXT_EDIT_MODE_VIEW) return;
377
378 glyph_len = nk_utf_decode(text, &unicode, total_len);
379 while ((text_len < total_len) && glyph_len)
380 {
381 /* don't insert a backward delete, just process the event */
382 if (unicode == 127) goto next;
383 /* can't add newline in single-line mode */
384 if (unicode == '\n' && state->single_line) goto next;
385 /* filter incoming text */
386 if (state->filter && !state->filter(state, unicode)) goto next;
387
388 if (!NK_TEXT_HAS_SELECTION(state) &&
389 state->cursor < state->string.len)
390 {
391 if (state->mode == NK_TEXT_EDIT_MODE_REPLACE) {
392 nk_textedit_makeundo_replace(state, state->cursor, 1, 1);
393 nk_str_delete_runes(&state->string, state->cursor, 1);
394 }
395 if (nk_str_insert_text_utf8(&state->string, state->cursor,
396 text+text_len, 1))
397 {
398 ++state->cursor;
399 state->has_preferred_x = 0;
400 }
401 } else {
402 nk_textedit_delete_selection(state); /* implicitly clamps */
403 if (nk_str_insert_text_utf8(&state->string, state->cursor,
404 text+text_len, 1))
405 {
406 nk_textedit_makeundo_insert(state, state->cursor, 1);
407 state->cursor = NK_MIN(state->cursor + 1, state->string.len);
408 state->has_preferred_x = 0;
409 }
410 }
411 next:
412 text_len += glyph_len;
413 glyph_len = nk_utf_decode(text + text_len, &unicode, total_len-text_len);
414 }
415}
416NK_LIB void
417nk_textedit_key(struct nk_text_edit *state, enum nk_keys key, int shift_mod,
418 const struct nk_user_font *font, float row_height)
419{
420retry:
421 switch (key)
422 {
423 case NK_KEY_NONE:
424 case NK_KEY_CTRL:
425 case NK_KEY_ENTER:
426 case NK_KEY_SHIFT:
427 case NK_KEY_TAB:
428 case NK_KEY_COPY:
429 case NK_KEY_CUT:
430 case NK_KEY_PASTE:
431 case NK_KEY_MAX:
432 default: break;
433 case NK_KEY_TEXT_UNDO:
434 nk_textedit_undo(state);
435 state->has_preferred_x = 0;
436 break;
437
438 case NK_KEY_TEXT_REDO:
439 nk_textedit_redo(state);
440 state->has_preferred_x = 0;
441 break;
442
443 case NK_KEY_TEXT_SELECT_ALL:
444 nk_textedit_select_all(state);
445 state->has_preferred_x = 0;
446 break;
447
448 case NK_KEY_TEXT_INSERT_MODE:
449 state->mode = NK_TEXT_EDIT_MODE_INSERT;
450 break;
451 case NK_KEY_TEXT_REPLACE_MODE:
452 state->mode = NK_TEXT_EDIT_MODE_REPLACE;
453 break;
454 case NK_KEY_TEXT_RESET_MODE:
455 state->mode = NK_TEXT_EDIT_MODE_VIEW;
456 break;
457
458 case NK_KEY_LEFT:
459 if (shift_mod) {
460 nk_textedit_clamp(state);
461 nk_textedit_prep_selection_at_cursor(state);
462 /* move selection left */
463 if (state->select_end > 0)
464 --state->select_end;
465 state->cursor = state->select_end;
466 state->has_preferred_x = 0;
467 } else {
468 /* if currently there's a selection,
469 * move cursor to start of selection */
470 if (NK_TEXT_HAS_SELECTION(state))
471 nk_textedit_move_to_first(state);
472 else if (state->cursor > 0)
473 --state->cursor;
474 state->has_preferred_x = 0;
475 } break;
476
477 case NK_KEY_RIGHT:
478 if (shift_mod) {
479 nk_textedit_prep_selection_at_cursor(state);
480 /* move selection right */
481 ++state->select_end;
482 nk_textedit_clamp(state);
483 state->cursor = state->select_end;
484 state->has_preferred_x = 0;
485 } else {
486 /* if currently there's a selection,
487 * move cursor to end of selection */
488 if (NK_TEXT_HAS_SELECTION(state))
489 nk_textedit_move_to_last(state);
490 else ++state->cursor;
491 nk_textedit_clamp(state);
492 state->has_preferred_x = 0;
493 } break;
494
495 case NK_KEY_TEXT_WORD_LEFT:
496 if (shift_mod) {
497 if( !NK_TEXT_HAS_SELECTION( state ) )
498 nk_textedit_prep_selection_at_cursor(state);
499 state->cursor = nk_textedit_move_to_word_previous(state);
500 state->select_end = state->cursor;
501 nk_textedit_clamp(state );
502 } else {
503 if (NK_TEXT_HAS_SELECTION(state))
504 nk_textedit_move_to_first(state);
505 else {
506 state->cursor = nk_textedit_move_to_word_previous(state);
507 nk_textedit_clamp(state );
508 }
509 } break;
510
511 case NK_KEY_TEXT_WORD_RIGHT:
512 if (shift_mod) {
513 if( !NK_TEXT_HAS_SELECTION( state ) )
514 nk_textedit_prep_selection_at_cursor(state);
515 state->cursor = nk_textedit_move_to_word_next(state);
516 state->select_end = state->cursor;
517 nk_textedit_clamp(state);
518 } else {
519 if (NK_TEXT_HAS_SELECTION(state))
520 nk_textedit_move_to_last(state);
521 else {
522 state->cursor = nk_textedit_move_to_word_next(state);
523 nk_textedit_clamp(state );
524 }
525 } break;
526
527 case NK_KEY_DOWN: {
528 struct nk_text_find find;
529 struct nk_text_edit_row row;
530 int i, sel = shift_mod;
531
532 if (state->single_line) {
533 /* on windows, up&down in single-line behave like left&right */
534 key = NK_KEY_RIGHT;
535 goto retry;
536 }
537
538 if (sel)
539 nk_textedit_prep_selection_at_cursor(state);
540 else if (NK_TEXT_HAS_SELECTION(state))
541 nk_textedit_move_to_last(state);
542
543 /* compute current position of cursor point */
544 nk_textedit_clamp(state);
545 nk_textedit_find_charpos(&find, state, state->cursor, state->single_line,
546 font, row_height);
547
548 /* now find character position down a row */
549 if (find.length)
550 {
551 float x;
552 float goal_x = state->has_preferred_x ? state->preferred_x : find.x;
553 int start = find.first_char + find.length;
554
555 state->cursor = start;
556 nk_textedit_layout_row(&row, state, state->cursor, row_height, font);
557 x = row.x0;
558
559 for (i=0; i < row.num_chars && x < row.x1; ++i) {
560 float dx = nk_textedit_get_width(state, start, i, font);
561 x += dx;
562 if (x > goal_x)
563 break;
564 ++state->cursor;
565 }
566 nk_textedit_clamp(state);
567
568 state->has_preferred_x = 1;
569 state->preferred_x = goal_x;
570 if (sel)
571 state->select_end = state->cursor;
572 }
573 } break;
574
575 case NK_KEY_UP: {
576 struct nk_text_find find;
577 struct nk_text_edit_row row;
578 int i, sel = shift_mod;
579
580 if (state->single_line) {
581 /* on windows, up&down become left&right */
582 key = NK_KEY_LEFT;
583 goto retry;
584 }
585
586 if (sel)
587 nk_textedit_prep_selection_at_cursor(state);
588 else if (NK_TEXT_HAS_SELECTION(state))
589 nk_textedit_move_to_first(state);
590
591 /* compute current position of cursor point */
592 nk_textedit_clamp(state);
593 nk_textedit_find_charpos(&find, state, state->cursor, state->single_line,
594 font, row_height);
595
596 /* can only go up if there's a previous row */
597 if (find.prev_first != find.first_char) {
598 /* now find character position up a row */
599 float x;
600 float goal_x = state->has_preferred_x ? state->preferred_x : find.x;
601
602 state->cursor = find.prev_first;
603 nk_textedit_layout_row(&row, state, state->cursor, row_height, font);
604 x = row.x0;
605
606 for (i=0; i < row.num_chars && x < row.x1; ++i) {
607 float dx = nk_textedit_get_width(state, find.prev_first, i, font);
608 x += dx;
609 if (x > goal_x)
610 break;
611 ++state->cursor;
612 }
613 nk_textedit_clamp(state);
614
615 state->has_preferred_x = 1;
616 state->preferred_x = goal_x;
617 if (sel) state->select_end = state->cursor;
618 }
619 } break;
620
621 case NK_KEY_DEL:
622 if (state->mode == NK_TEXT_EDIT_MODE_VIEW)
623 break;
624 if (NK_TEXT_HAS_SELECTION(state))
625 nk_textedit_delete_selection(state);
626 else {
627 int n = state->string.len;
628 if (state->cursor < n)
629 nk_textedit_delete(state, state->cursor, 1);
630 }
631 state->has_preferred_x = 0;
632 break;
633
634 case NK_KEY_BACKSPACE:
635 if (state->mode == NK_TEXT_EDIT_MODE_VIEW)
636 break;
637 if (NK_TEXT_HAS_SELECTION(state))
638 nk_textedit_delete_selection(state);
639 else {
640 nk_textedit_clamp(state);
641 if (state->cursor > 0) {
642 nk_textedit_delete(state, state->cursor-1, 1);
643 --state->cursor;
644 }
645 }
646 state->has_preferred_x = 0;
647 break;
648
649 case NK_KEY_TEXT_START:
650 if (shift_mod) {
651 nk_textedit_prep_selection_at_cursor(state);
652 state->cursor = state->select_end = 0;
653 state->has_preferred_x = 0;
654 } else {
655 state->cursor = state->select_start = state->select_end = 0;
656 state->has_preferred_x = 0;
657 }
658 break;
659
660 case NK_KEY_TEXT_END:
661 if (shift_mod) {
662 nk_textedit_prep_selection_at_cursor(state);
663 state->cursor = state->select_end = state->string.len;
664 state->has_preferred_x = 0;
665 } else {
666 state->cursor = state->string.len;
667 state->select_start = state->select_end = 0;
668 state->has_preferred_x = 0;
669 }
670 break;
671
672 case NK_KEY_TEXT_LINE_START: {
673 if (shift_mod) {
674 struct nk_text_find find;
675 nk_textedit_clamp(state);
676 nk_textedit_prep_selection_at_cursor(state);
677 if (state->string.len && state->cursor == state->string.len)
678 --state->cursor;
679 nk_textedit_find_charpos(&find, state,state->cursor, state->single_line,
680 font, row_height);
681 state->cursor = state->select_end = find.first_char;
682 state->has_preferred_x = 0;
683 } else {
684 struct nk_text_find find;
685 if (state->string.len && state->cursor == state->string.len)
686 --state->cursor;
687 nk_textedit_clamp(state);
688 nk_textedit_move_to_first(state);
689 nk_textedit_find_charpos(&find, state, state->cursor, state->single_line,
690 font, row_height);
691 state->cursor = find.first_char;
692 state->has_preferred_x = 0;
693 }
694 } break;
695
696 case NK_KEY_TEXT_LINE_END: {
697 if (shift_mod) {
698 struct nk_text_find find;
699 nk_textedit_clamp(state);
700 nk_textedit_prep_selection_at_cursor(state);
701 nk_textedit_find_charpos(&find, state, state->cursor, state->single_line,
702 font, row_height);
703 state->has_preferred_x = 0;
704 state->cursor = find.first_char + find.length;
705 if (find.length > 0 && nk_str_rune_at(&state->string, state->cursor-1) == '\n')
706 --state->cursor;
707 state->select_end = state->cursor;
708 } else {
709 struct nk_text_find find;
710 nk_textedit_clamp(state);
711 nk_textedit_move_to_first(state);
712 nk_textedit_find_charpos(&find, state, state->cursor, state->single_line,
713 font, row_height);
714
715 state->has_preferred_x = 0;
716 state->cursor = find.first_char + find.length;
717 if (find.length > 0 && nk_str_rune_at(&state->string, state->cursor-1) == '\n')
718 --state->cursor;
719 }} break;
720 }
721}
722NK_INTERN void
723nk_textedit_flush_redo(struct nk_text_undo_state *state)
724{
725 state->redo_point = NK_TEXTEDIT_UNDOSTATECOUNT;
726 state->redo_char_point = NK_TEXTEDIT_UNDOCHARCOUNT;
727}
728NK_INTERN void
729nk_textedit_discard_undo(struct nk_text_undo_state *state)
730{
731 /* discard the oldest entry in the undo list */
732 if (state->undo_point > 0) {
733 /* if the 0th undo state has characters, clean those up */
734 if (state->undo_rec[0].char_storage >= 0) {
735 int n = state->undo_rec[0].insert_length, i;
736 /* delete n characters from all other records */
737 state->undo_char_point = (short)(state->undo_char_point - n);
738 NK_MEMCPY(state->undo_char, state->undo_char + n,
739 (nk_size)state->undo_char_point*sizeof(nk_rune));
740 for (i=0; i < state->undo_point; ++i) {
741 if (state->undo_rec[i].char_storage >= 0)
742 state->undo_rec[i].char_storage = (short)
743 (state->undo_rec[i].char_storage - n);
744 }
745 }
746 --state->undo_point;
747 NK_MEMCPY(state->undo_rec, state->undo_rec+1,
748 (nk_size)((nk_size)state->undo_point * sizeof(state->undo_rec[0])));
749 }
750}
751NK_INTERN void
752nk_textedit_discard_redo(struct nk_text_undo_state *state)
753{
754/* discard the oldest entry in the redo list--it's bad if this
755 ever happens, but because undo & redo have to store the actual
756 characters in different cases, the redo character buffer can
757 fill up even though the undo buffer didn't */
758 nk_size num;
759 int k = NK_TEXTEDIT_UNDOSTATECOUNT-1;
760 if (state->redo_point <= k) {
761 /* if the k'th undo state has characters, clean those up */
762 if (state->undo_rec[k].char_storage >= 0) {
763 int n = state->undo_rec[k].insert_length, i;
764 /* delete n characters from all other records */
765 state->redo_char_point = (short)(state->redo_char_point + n);
766 num = (nk_size)(NK_TEXTEDIT_UNDOCHARCOUNT - state->redo_char_point);
767 NK_MEMCPY(state->undo_char + state->redo_char_point,
768 state->undo_char + state->redo_char_point-n, num * sizeof(char));
769 for (i = state->redo_point; i < k; ++i) {
770 if (state->undo_rec[i].char_storage >= 0) {
771 state->undo_rec[i].char_storage = (short)
772 (state->undo_rec[i].char_storage + n);
773 }
774 }
775 }
776 ++state->redo_point;
777 num = (nk_size)(NK_TEXTEDIT_UNDOSTATECOUNT - state->redo_point);
778 if (num) NK_MEMCPY(state->undo_rec + state->redo_point-1,
779 state->undo_rec + state->redo_point, num * sizeof(state->undo_rec[0]));
780 }
781}
782NK_INTERN struct nk_text_undo_record*
783nk_textedit_create_undo_record(struct nk_text_undo_state *state, int numchars)
784{
785 /* any time we create a new undo record, we discard redo*/
786 nk_textedit_flush_redo(state);
787
788 /* if we have no free records, we have to make room,
789 * by sliding the existing records down */
790 if (state->undo_point == NK_TEXTEDIT_UNDOSTATECOUNT)
791 nk_textedit_discard_undo(state);
792
793 /* if the characters to store won't possibly fit in the buffer,
794 * we can't undo */
795 if (numchars > NK_TEXTEDIT_UNDOCHARCOUNT) {
796 state->undo_point = 0;
797 state->undo_char_point = 0;
798 return 0;
799 }
800
801 /* if we don't have enough free characters in the buffer,
802 * we have to make room */
803 while (state->undo_char_point + numchars > NK_TEXTEDIT_UNDOCHARCOUNT)
804 nk_textedit_discard_undo(state);
805 return &state->undo_rec[state->undo_point++];
806}
807NK_INTERN nk_rune*
808nk_textedit_createundo(struct nk_text_undo_state *state, int pos,
809 int insert_len, int delete_len)
810{
811 struct nk_text_undo_record *r = nk_textedit_create_undo_record(state, insert_len);
812 if (r == 0)
813 return 0;
814
815 r->where = pos;
816 r->insert_length = (short) insert_len;
817 r->delete_length = (short) delete_len;
818
819 if (insert_len == 0) {
820 r->char_storage = -1;
821 return 0;
822 } else {
823 r->char_storage = state->undo_char_point;
824 state->undo_char_point = (short)(state->undo_char_point + insert_len);
825 return &state->undo_char[r->char_storage];
826 }
827}
828NK_API void
829nk_textedit_undo(struct nk_text_edit *state)
830{
831 struct nk_text_undo_state *s = &state->undo;
832 struct nk_text_undo_record u, *r;
833 if (s->undo_point == 0)
834 return;
835
836 /* we need to do two things: apply the undo record, and create a redo record */
837 u = s->undo_rec[s->undo_point-1];
838 r = &s->undo_rec[s->redo_point-1];
839 r->char_storage = -1;
840
841 r->insert_length = u.delete_length;
842 r->delete_length = u.insert_length;
843 r->where = u.where;
844
845 if (u.delete_length)
846 {
847 /* if the undo record says to delete characters, then the redo record will
848 need to re-insert the characters that get deleted, so we need to store
849 them.
850 there are three cases:
851 - there's enough room to store the characters
852 - characters stored for *redoing* don't leave room for redo
853 - characters stored for *undoing* don't leave room for redo
854 if the last is true, we have to bail */
855 if (s->undo_char_point + u.delete_length >= NK_TEXTEDIT_UNDOCHARCOUNT) {
856 /* the undo records take up too much character space; there's no space
857 * to store the redo characters */
858 r->insert_length = 0;
859 } else {
860 int i;
861 /* there's definitely room to store the characters eventually */
862 while (s->undo_char_point + u.delete_length > s->redo_char_point) {
863 /* there's currently not enough room, so discard a redo record */
864 nk_textedit_discard_redo(s);
865 /* should never happen: */
866 if (s->redo_point == NK_TEXTEDIT_UNDOSTATECOUNT)
867 return;
868 }
869
870 r = &s->undo_rec[s->redo_point-1];
871 r->char_storage = (short)(s->redo_char_point - u.delete_length);
872 s->redo_char_point = (short)(s->redo_char_point - u.delete_length);
873
874 /* now save the characters */
875 for (i=0; i < u.delete_length; ++i)
876 s->undo_char[r->char_storage + i] =
877 nk_str_rune_at(&state->string, u.where + i);
878 }
879 /* now we can carry out the deletion */
880 nk_str_delete_runes(&state->string, u.where, u.delete_length);
881 }
882
883 /* check type of recorded action: */
884 if (u.insert_length) {
885 /* easy case: was a deletion, so we need to insert n characters */
886 nk_str_insert_text_runes(&state->string, u.where,
887 &s->undo_char[u.char_storage], u.insert_length);
888 s->undo_char_point = (short)(s->undo_char_point - u.insert_length);
889 }
890 state->cursor = (short)(u.where + u.insert_length);
891
892 s->undo_point--;
893 s->redo_point--;
894}
895NK_API void
896nk_textedit_redo(struct nk_text_edit *state)
897{
898 struct nk_text_undo_state *s = &state->undo;
899 struct nk_text_undo_record *u, r;
900 if (s->redo_point == NK_TEXTEDIT_UNDOSTATECOUNT)
901 return;
902
903 /* we need to do two things: apply the redo record, and create an undo record */
904 u = &s->undo_rec[s->undo_point];
905 r = s->undo_rec[s->redo_point];
906
907 /* we KNOW there must be room for the undo record, because the redo record
908 was derived from an undo record */
909 u->delete_length = r.insert_length;
910 u->insert_length = r.delete_length;
911 u->where = r.where;
912 u->char_storage = -1;
913
914 if (r.delete_length) {
915 /* the redo record requires us to delete characters, so the undo record
916 needs to store the characters */
917 if (s->undo_char_point + u->insert_length > s->redo_char_point) {
918 u->insert_length = 0;
919 u->delete_length = 0;
920 } else {
921 int i;
922 u->char_storage = s->undo_char_point;
923 s->undo_char_point = (short)(s->undo_char_point + u->insert_length);
924
925 /* now save the characters */
926 for (i=0; i < u->insert_length; ++i) {
927 s->undo_char[u->char_storage + i] =
928 nk_str_rune_at(&state->string, u->where + i);
929 }
930 }
931 nk_str_delete_runes(&state->string, r.where, r.delete_length);
932 }
933
934 if (r.insert_length) {
935 /* easy case: need to insert n characters */
936 nk_str_insert_text_runes(&state->string, r.where,
937 &s->undo_char[r.char_storage], r.insert_length);
938 }
939 state->cursor = r.where + r.insert_length;
940
941 s->undo_point++;
942 s->redo_point++;
943}
944NK_INTERN void
945nk_textedit_makeundo_insert(struct nk_text_edit *state, int where, int length)
946{
947 nk_textedit_createundo(&state->undo, where, 0, length);
948}
949NK_INTERN void
950nk_textedit_makeundo_delete(struct nk_text_edit *state, int where, int length)
951{
952 int i;
953 nk_rune *p = nk_textedit_createundo(&state->undo, where, length, 0);
954 if (p) {
955 for (i=0; i < length; ++i)
956 p[i] = nk_str_rune_at(&state->string, where+i);
957 }
958}
959NK_INTERN void
960nk_textedit_makeundo_replace(struct nk_text_edit *state, int where,
961 int old_length, int new_length)
962{
963 int i;
964 nk_rune *p = nk_textedit_createundo(&state->undo, where, old_length, new_length);
965 if (p) {
966 for (i=0; i < old_length; ++i)
967 p[i] = nk_str_rune_at(&state->string, where+i);
968 }
969}
970NK_LIB void
971nk_textedit_clear_state(struct nk_text_edit *state, enum nk_text_edit_type type,
972 nk_plugin_filter filter)
973{
974 /* reset the state to default */
975 state->undo.undo_point = 0;
976 state->undo.undo_char_point = 0;
977 state->undo.redo_point = NK_TEXTEDIT_UNDOSTATECOUNT;
978 state->undo.redo_char_point = NK_TEXTEDIT_UNDOCHARCOUNT;
979 state->select_end = state->select_start = 0;
980 state->cursor = 0;
981 state->has_preferred_x = 0;
982 state->preferred_x = 0;
983 state->cursor_at_end_of_line = 0;
984 state->initialized = 1;
985 state->single_line = (unsigned char)(type == NK_TEXT_EDIT_SINGLE_LINE);
986 state->mode = NK_TEXT_EDIT_MODE_VIEW;
987 state->filter = filter;
988 state->scrollbar = nk_vec2(0,0);
989}
990NK_API void
991nk_textedit_init_fixed(struct nk_text_edit *state, void *memory, nk_size size)
992{
993 NK_ASSERT(state);
994 NK_ASSERT(memory);
995 if (!state || !memory || !size) return;
996 NK_MEMSET(state, 0, sizeof(struct nk_text_edit));
997 nk_textedit_clear_state(state, NK_TEXT_EDIT_SINGLE_LINE, 0);
998 nk_str_init_fixed(&state->string, memory, size);
999}
1000NK_API void
1001nk_textedit_init(struct nk_text_edit *state, const struct nk_allocator *alloc, nk_size size)
1002{
1003 NK_ASSERT(state);
1004 NK_ASSERT(alloc);
1005 if (!state || !alloc) return;
1006 NK_MEMSET(state, 0, sizeof(struct nk_text_edit));
1007 nk_textedit_clear_state(state, NK_TEXT_EDIT_SINGLE_LINE, 0);
1008 nk_str_init(&state->string, alloc, size);
1009}
1010#ifdef NK_INCLUDE_DEFAULT_ALLOCATOR
1011NK_API void
1012nk_textedit_init_default(struct nk_text_edit *state)
1013{
1014 NK_ASSERT(state);
1015 if (!state) return;
1016 NK_MEMSET(state, 0, sizeof(struct nk_text_edit));
1017 nk_textedit_clear_state(state, NK_TEXT_EDIT_SINGLE_LINE, 0);
1018 nk_str_init_default(&state->string);
1019}
1020#endif
1021NK_API void
1022nk_textedit_select_all(struct nk_text_edit *state)
1023{
1024 NK_ASSERT(state);
1025 state->select_start = 0;
1026 state->select_end = state->string.len;
1027}
1028NK_API void
1029nk_textedit_free(struct nk_text_edit *state)
1030{
1031 NK_ASSERT(state);
1032 if (!state) return;
1033 nk_str_free(&state->string);
1034}
1035
main API and documentation file
NK_API void nk_textedit_init(struct nk_text_edit *, const struct nk_allocator *, nk_size size)
text editor
nk_text_width_f width
!< max height of the font
Definition nuklear.h:4009
float height
!< user provided font handle
Definition nuklear.h:4008