ui/gtk/text.c

changeset 0
2483f517c562
child 29
3fc287f06305
equal deleted inserted replaced
-1:000000000000 0:2483f517c562
1 /*
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3 *
4 * Copyright 2017 Olaf Wintermann. All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are met:
8 *
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 *
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 * POSSIBILITY OF SUCH DAMAGE.
27 */
28
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <string.h>
32
33 #include "text.h"
34 #include "container.h"
35
36
37 #include "../common/types.h"
38
39 static void selection_handler(
40 GtkTextBuffer *buf,
41 GtkTextIter *location,
42 GtkTextMark *mark,
43 UiTextArea *textview)
44 {
45 const char *mname = gtk_text_mark_get_name(mark);
46 if(mname) {
47 GtkTextIter begin;
48 GtkTextIter end;
49 int sel = gtk_text_buffer_get_selection_bounds (buf, &begin, &end);
50 if(sel != textview->last_selection_state) {
51 if(sel) {
52 ui_set_group(textview->ctx, UI_GROUP_SELECTION);
53 } else {
54 ui_unset_group(textview->ctx, UI_GROUP_SELECTION);
55 }
56 }
57 textview->last_selection_state = sel;
58 }
59 }
60
61 UIWIDGET ui_textarea_var(UiObject *obj, UiVar *var) {
62 GtkWidget *text_area = gtk_text_view_new();
63 gtk_text_view_set_wrap_mode(GTK_TEXT_VIEW(text_area), GTK_WRAP_WORD_CHAR);
64 g_signal_connect(
65 text_area,
66 "realize",
67 G_CALLBACK(ui_textarea_realize_event),
68 NULL);
69
70 UiTextArea *uitext = malloc(sizeof(UiTextArea));
71 uitext->ctx = obj->ctx;
72 uitext->var = var;
73 uitext->last_selection_state = 0;
74
75 g_signal_connect(
76 text_area,
77 "destroy",
78 G_CALLBACK(ui_textarea_destroy),
79 uitext);
80
81 GtkWidget *scroll_area = gtk_scrolled_window_new (NULL, NULL);
82 gtk_scrolled_window_set_policy(
83 GTK_SCROLLED_WINDOW(scroll_area),
84 GTK_POLICY_AUTOMATIC,
85 GTK_POLICY_AUTOMATIC); // GTK_POLICY_ALWAYS
86 gtk_container_add(GTK_CONTAINER(scroll_area), text_area);
87
88 // font and padding
89 PangoFontDescription *font;
90 font = pango_font_description_from_string("Monospace");
91 gtk_widget_modify_font(text_area, font);
92 pango_font_description_free(font);
93
94 gtk_text_view_set_left_margin(GTK_TEXT_VIEW(text_area), 2);
95 gtk_text_view_set_right_margin(GTK_TEXT_VIEW(text_area), 2);
96
97 // add
98 UiContainer *ct = uic_get_current_container(obj);
99 ct->add(ct, scroll_area, TRUE);
100
101 // bind value
102 UiText *value = var->value;
103 if(value) {
104 GtkTextBuffer *buf = gtk_text_view_get_buffer(GTK_TEXT_VIEW(text_area));
105
106 if(value->value.ptr) {
107 gtk_text_buffer_set_text(buf, value->value.ptr, -1);
108 value->value.free(value->value.ptr);
109 }
110
111 value->get = ui_textarea_get;
112 value->set = ui_textarea_set;
113 value->getsubstr = ui_textarea_getsubstr;
114 value->insert = ui_textarea_insert;
115 value->setposition = ui_textarea_setposition;
116 value->position = ui_textarea_position;
117 value->selection = ui_textarea_selection;
118 value->length = ui_textarea_length;
119 value->remove = ui_textarea_remove;
120 value->value.ptr = NULL;
121 value->value.free = NULL;
122 value->obj = buf;
123 if(!value->undomgr) {
124 value->undomgr = ui_create_undomgr();
125 }
126
127 g_signal_connect(
128 buf,
129 "changed",
130 G_CALLBACK(ui_textbuf_changed),
131 uitext);
132
133 // register undo manager
134 g_signal_connect(
135 buf,
136 "insert-text",
137 G_CALLBACK(ui_textbuf_insert),
138 var);
139 g_signal_connect(
140 buf,
141 "delete-range",
142 G_CALLBACK(ui_textbuf_delete),
143 var);
144 g_signal_connect(
145 buf,
146 "mark-set",
147 G_CALLBACK(selection_handler),
148 uitext);
149 }
150
151 return scroll_area;
152 }
153
154 void ui_textarea_destroy(GtkWidget *object, UiTextArea *textarea) {
155 ui_destroy_boundvar(textarea->ctx, textarea->var);
156 free(textarea);
157 }
158
159 UIWIDGET ui_textarea(UiObject *obj, UiText *value) {
160 UiVar *var = malloc(sizeof(UiVar));
161 var->value = value;
162 var->type = UI_VAR_SPECIAL;
163 var->from = NULL;
164 var->from_ctx = NULL;
165 return ui_textarea_var(obj, var);
166 }
167
168 UIWIDGET ui_textarea_nv(UiObject *obj, char *varname) {
169 UiVar *var = uic_create_var(obj->ctx, varname, UI_VAR_TEXT);
170 if(var) {
171 return ui_textarea_var(obj, var);
172 } else {
173 // TODO: error
174 }
175 return NULL;
176 }
177
178 UIWIDGET ui_textarea_gettextwidget(UIWIDGET textarea) {
179 return gtk_bin_get_child(GTK_BIN(textarea));
180 }
181
182 char* ui_textarea_get(UiText *text) {
183 if(text->value.ptr) {
184 text->value.free(text->value.ptr);
185 }
186 GtkTextBuffer *buf = text->obj;
187 GtkTextIter start;
188 GtkTextIter end;
189 gtk_text_buffer_get_bounds(buf, &start, &end);
190 char *str = gtk_text_buffer_get_text(buf, &start, &end, FALSE);
191 text->value.ptr = g_strdup(str);
192 text->value.free = (ui_freefunc)g_free;
193 return str;
194 }
195
196 void ui_textarea_set(UiText *text, char *str) {
197 gtk_text_buffer_set_text((GtkTextBuffer*)text->obj, str, -1);
198 if(text->value.ptr) {
199 text->value.free(text->value.ptr);
200 }
201 text->value.ptr = NULL;
202 text->value.free = NULL;
203 }
204
205 char* ui_textarea_getsubstr(UiText *text, int begin, int end) {
206 if(text->value.ptr) {
207 text->value.free(text->value.ptr);
208 }
209 GtkTextBuffer *buf = text->obj;
210 GtkTextIter ib;
211 GtkTextIter ie;
212 gtk_text_buffer_get_iter_at_offset(text->obj, &ib, begin);
213 gtk_text_buffer_get_iter_at_offset(text->obj, &ie, end);
214 char *str = gtk_text_buffer_get_text(buf, &ib, &ie, FALSE);
215 text->value.ptr = g_strdup(str);
216 text->value.free = (ui_freefunc)g_free;
217 return str;
218 }
219
220 void ui_textarea_insert(UiText *text, int pos, char *str) {
221 GtkTextIter offset;
222 gtk_text_buffer_get_iter_at_offset(text->obj, &offset, pos);
223 gtk_text_buffer_insert(text->obj, &offset, str, -1);
224 if(text->value.ptr) {
225 text->value.free(text->value.ptr);
226 }
227 text->value.ptr = NULL;
228 text->value.free = NULL;
229 }
230
231 void ui_textarea_setposition(UiText *text, int pos) {
232 GtkTextIter iter;
233 gtk_text_buffer_get_iter_at_offset(text->obj, &iter, pos);
234 gtk_text_buffer_place_cursor(text->obj, &iter);
235 }
236
237 int ui_textarea_position(UiText *text) {
238 GtkTextIter begin;
239 GtkTextIter end;
240 gtk_text_buffer_get_selection_bounds(text->obj, &begin, &end);
241 text->pos = gtk_text_iter_get_offset(&begin);
242 return text->pos;
243 }
244
245 void ui_textarea_selection(UiText *text, int *begin, int *end) {
246 GtkTextIter b;
247 GtkTextIter e;
248 gtk_text_buffer_get_selection_bounds(text->obj, &b, &e);
249 *begin = gtk_text_iter_get_offset(&b);
250 *end = gtk_text_iter_get_offset(&e);
251 }
252
253 int ui_textarea_length(UiText *text) {
254 GtkTextBuffer *buf = text->obj;
255 GtkTextIter start;
256 GtkTextIter end;
257 gtk_text_buffer_get_bounds(buf, &start, &end);
258 return gtk_text_iter_get_offset(&end);
259 }
260
261 void ui_textarea_remove(UiText *text, int begin, int end) {
262 GtkTextBuffer *buf = text->obj;
263 GtkTextIter ib;
264 GtkTextIter ie;
265 gtk_text_buffer_get_iter_at_offset(buf, &ib, begin);
266 gtk_text_buffer_get_iter_at_offset(buf, &ie, end);
267 gtk_text_buffer_delete(buf, &ib, &ie);
268 }
269
270 void ui_textarea_realize_event(GtkWidget *widget, gpointer data) {
271 gtk_widget_grab_focus(widget);
272 }
273
274
275
276 void ui_textbuf_changed(GtkTextBuffer *textbuffer, UiTextArea *textarea) {
277 UiText *value = textarea->var->value;
278 if(value->observers) {
279 UiEvent e;
280 e.obj = textarea->ctx->obj;
281 e.window = e.obj->window;
282 e.document = textarea->ctx->document;
283 e.eventdata = value;
284 e.intval = 0;
285 ui_notify_evt(value->observers, &e);
286 }
287 }
288
289 // undo manager functions
290
291 void ui_textbuf_insert(
292 GtkTextBuffer *textbuffer,
293 GtkTextIter *location,
294 char *text,
295 int length,
296 void *data)
297 {
298 UiVar *var = data;
299 UiText *value = var->value;
300 if(!value->undomgr) {
301 value->undomgr = ui_create_undomgr();
302 }
303 UiUndoMgr *mgr = value->undomgr;
304 if(!mgr->event) {
305 return;
306 }
307
308 if(mgr->cur) {
309 UiTextBufOp *elm = mgr->cur->next;
310 if(elm) {
311 mgr->cur->next = NULL;
312 mgr->end = mgr->cur;
313 while(elm) {
314 elm->prev = NULL;
315 UiTextBufOp *next = elm->next;
316 ui_free_textbuf_op(elm);
317 elm = next;
318 }
319 }
320
321 UiTextBufOp *last_op = mgr->cur;
322 if(
323 last_op->type == UI_TEXTBUF_INSERT &&
324 ui_check_insertstr(last_op->text, last_op->len, text, length) == 0)
325 {
326 // append text to last op
327 int ln = last_op->len;
328 char *newtext = malloc(ln + length + 1);
329 memcpy(newtext, last_op->text, ln);
330 memcpy(newtext+ln, text, length);
331 newtext[ln+length] = '\0';
332
333 last_op->text = newtext;
334 last_op->len = ln + length;
335 last_op->end += length;
336
337 return;
338 }
339 }
340
341 char *dpstr = malloc(length + 1);
342 memcpy(dpstr, text, length);
343 dpstr[length] = 0;
344
345 UiTextBufOp *op = malloc(sizeof(UiTextBufOp));
346 op->prev = NULL;
347 op->next = NULL;
348 op->type = UI_TEXTBUF_INSERT;
349 op->start = gtk_text_iter_get_offset(location);
350 op->end = op->start+length;
351 op->len = length;
352 op->text = dpstr;
353
354 cx_linked_list_add(
355 (void**)&mgr->begin,
356 (void**)&mgr->end,
357 offsetof(UiTextBufOp, prev),
358 offsetof(UiTextBufOp, next),
359 op);
360
361 mgr->cur = op;
362 }
363
364 void ui_textbuf_delete(
365 GtkTextBuffer *textbuffer,
366 GtkTextIter *start,
367 GtkTextIter *end,
368 void *data)
369 {
370 UiVar *var = data;
371 UiText *value = var->value;
372 if(!value->undomgr) {
373 value->undomgr = ui_create_undomgr();
374 }
375 UiUndoMgr *mgr = value->undomgr;
376 if(!mgr->event) {
377 return;
378 }
379
380 if(mgr->cur) {
381 UiTextBufOp *elm = mgr->cur->next;
382 if(elm) {
383 mgr->cur->next = NULL;
384 mgr->end = mgr->cur;
385 while(elm) {
386 elm->prev = NULL;
387 UiTextBufOp *next = elm->next;
388 ui_free_textbuf_op(elm);
389 elm = next;
390 }
391 }
392 }
393
394 char *text = gtk_text_buffer_get_text(value->obj, start, end, FALSE);
395
396 UiTextBufOp *op = malloc(sizeof(UiTextBufOp));
397 op->prev = NULL;
398 op->next = NULL;
399 op->type = UI_TEXTBUF_DELETE;
400 op->start = gtk_text_iter_get_offset(start);
401 op->end = gtk_text_iter_get_offset(end);
402 op->len = op->end - op->start;
403
404 char *dpstr = malloc(op->len + 1);
405 memcpy(dpstr, text, op->len);
406 dpstr[op->len] = 0;
407 op->text = dpstr;
408
409 cx_linked_list_add(
410 (void**)&mgr->begin,
411 (void**)&mgr->end,
412 offsetof(UiTextBufOp, prev),
413 offsetof(UiTextBufOp, next),
414 op);
415
416 mgr->cur = op;
417 }
418
419 UiUndoMgr* ui_create_undomgr() {
420 UiUndoMgr *mgr = malloc(sizeof(UiUndoMgr));
421 mgr->begin = NULL;
422 mgr->end = NULL;
423 mgr->cur = NULL;
424 mgr->length = 0;
425 mgr->event = 1;
426 return mgr;
427 }
428
429 void ui_destroy_undomgr(UiUndoMgr *mgr) {
430 UiTextBufOp *op = mgr->begin;
431 while(op) {
432 UiTextBufOp *nextOp = op->next;
433 if(op->text) {
434 free(op->text);
435 }
436 free(op);
437 op = nextOp;
438 }
439 free(mgr);
440 }
441
442 void ui_free_textbuf_op(UiTextBufOp *op) {
443 if(op->text) {
444 free(op->text);
445 }
446 free(op);
447 }
448
449 int ui_check_insertstr(char *oldstr, int oldlen, char *newstr, int newlen) {
450 // return 1 if oldstr + newstr are one word
451
452 int has_space = 0;
453 for(int i=0;i<oldlen;i++) {
454 if(oldstr[i] < 33) {
455 has_space = 1;
456 break;
457 }
458 }
459
460 for(int i=0;i<newlen;i++) {
461 if(has_space && newstr[i] > 32) {
462 return 1;
463 }
464 }
465
466 return 0;
467 }
468
469 void ui_text_undo(UiText *value) {
470 UiUndoMgr *mgr = value->undomgr;
471
472 if(mgr->cur) {
473 UiTextBufOp *op = mgr->cur;
474 mgr->event = 0;
475 switch(op->type) {
476 case UI_TEXTBUF_INSERT: {
477 GtkTextIter begin;
478 GtkTextIter end;
479 gtk_text_buffer_get_iter_at_offset(value->obj, &begin, op->start);
480 gtk_text_buffer_get_iter_at_offset(value->obj, &end, op->end);
481 gtk_text_buffer_delete(value->obj, &begin, &end);
482 break;
483 }
484 case UI_TEXTBUF_DELETE: {
485 GtkTextIter begin;
486 GtkTextIter end;
487 gtk_text_buffer_get_iter_at_offset(value->obj, &begin, op->start);
488 gtk_text_buffer_get_iter_at_offset(value->obj, &end, op->end);
489 gtk_text_buffer_insert(value->obj, &begin, op->text, op->len);
490 break;
491 }
492 }
493 mgr->event = 1;
494 mgr->cur = mgr->cur->prev;
495 }
496 }
497
498 void ui_text_redo(UiText *value) {
499 UiUndoMgr *mgr = value->undomgr;
500
501 UiTextBufOp *elm = NULL;
502 if(mgr->cur) {
503 if(mgr->cur->next) {
504 elm = mgr->cur->next;
505 }
506 } else if(mgr->begin) {
507 elm = mgr->begin;
508 }
509
510 if(elm) {
511 UiTextBufOp *op = elm;
512 mgr->event = 0;
513 switch(op->type) {
514 case UI_TEXTBUF_INSERT: {
515 GtkTextIter begin;
516 GtkTextIter end;
517 gtk_text_buffer_get_iter_at_offset(value->obj, &begin, op->start);
518 gtk_text_buffer_get_iter_at_offset(value->obj, &end, op->end);
519 gtk_text_buffer_insert(value->obj, &begin, op->text, op->len);
520 break;
521 }
522 case UI_TEXTBUF_DELETE: {
523 GtkTextIter begin;
524 GtkTextIter end;
525 gtk_text_buffer_get_iter_at_offset(value->obj, &begin, op->start);
526 gtk_text_buffer_get_iter_at_offset(value->obj, &end, op->end);
527 gtk_text_buffer_delete(value->obj, &begin, &end);
528 break;
529 }
530 }
531 mgr->event = 1;
532 mgr->cur = elm;
533 }
534 }
535
536
537 static UIWIDGET create_textfield_var(UiObject *obj, int width, UiBool frameless, UiBool password, UiVar *var) {
538 GtkWidget *textfield = gtk_entry_new();
539
540 UiTextField *uitext = malloc(sizeof(UiTextField));
541 uitext->ctx = obj->ctx;
542 uitext->var = var;
543
544 g_signal_connect(
545 textfield,
546 "destroy",
547 G_CALLBACK(ui_textfield_destroy),
548 uitext);
549
550 if(width > 0) {
551 gtk_entry_set_width_chars(GTK_ENTRY(textfield), width);
552 }
553 if(frameless) {
554 // TODO: gtk2legacy workaroud
555 gtk_entry_set_has_frame(GTK_ENTRY(textfield), FALSE);
556 }
557 if(password) {
558 gtk_entry_set_visibility(GTK_ENTRY(textfield), FALSE);
559 }
560
561 UiContainer *ct = uic_get_current_container(obj);
562 ct->add(ct, textfield, FALSE);
563
564 if(var) {
565 UiString *value = var->value;
566 if(value->value.ptr) {
567 gtk_entry_set_text(GTK_ENTRY(textfield), value->value.ptr);
568 value->value.free(value->value.ptr);
569 value->value.ptr = NULL;
570 value->value.free = NULL;
571 }
572
573 value->get = ui_textfield_get;
574 value->set = ui_textfield_set;
575 value->value.ptr = NULL;
576 value->value.free = NULL;
577 value->obj = GTK_ENTRY(textfield);
578
579 g_signal_connect(
580 textfield,
581 "changed",
582 G_CALLBACK(ui_textfield_changed),
583 uitext);
584 }
585
586 return textfield;
587 }
588
589 static UIWIDGET create_textfield_nv(UiObject *obj, int width, UiBool frameless, UiBool password, char *varname) {
590 UiVar *var = uic_create_var(obj->ctx, varname, UI_VAR_STRING);
591 if(var) {
592 return create_textfield_var(obj, width, frameless, password, var);
593 } else {
594 // TODO: error
595 }
596 return NULL;
597 }
598
599 static UIWIDGET create_textfield(UiObject *obj, int width, UiBool frameless, UiBool password, UiString *value) {
600 UiVar *var = NULL;
601 if(value) {
602 var = malloc(sizeof(UiVar));
603 var->value = value;
604 var->type = UI_VAR_SPECIAL;
605 var->from = NULL;
606 var->from_ctx = NULL;
607 }
608 return create_textfield_var(obj, width, frameless, password, var);
609 }
610
611 void ui_textfield_destroy(GtkWidget *object, UiTextField *textfield) {
612 if(textfield->var) {
613 UiText *text = textfield->var->value;
614 if(text->undomgr) {
615 ui_destroy_undomgr(text->undomgr);
616 }
617 ui_destroy_boundvar(textfield->ctx, textfield->var);
618 }
619 free(textfield);
620 }
621
622 void ui_textfield_changed(GtkEditable *editable, UiTextField *textfield) {
623 UiString *value = textfield->var->value;
624 if(value->observers) {
625 UiEvent e;
626 e.obj = textfield->ctx->obj;
627 e.window = e.obj->window;
628 e.document = textfield->ctx->document;
629 e.eventdata = value;
630 e.intval = 0;
631 ui_notify_evt(value->observers, &e);
632 }
633 }
634
635 UIWIDGET ui_textfield(UiObject *obj, UiString *value) {
636 return create_textfield(obj, 0, FALSE, FALSE, value);
637 }
638
639 UIWIDGET ui_textfield_nv(UiObject *obj, char *varname) {
640 return create_textfield_nv(obj, 0, FALSE, FALSE, varname);
641 }
642
643 UIWIDGET ui_textfield_w(UiObject *obj, int width, UiString *value) {
644 return create_textfield(obj, width, FALSE, FALSE, value);
645 }
646
647 UIWIDGET ui_textfield_wnv(UiObject *obj, int width, char *varname) {
648 return create_textfield_nv(obj, width, FALSE, FALSE, varname);
649 }
650
651 UIWIDGET ui_frameless_textfield(UiObject *obj, UiString *value) {
652 return create_textfield(obj, 0, TRUE, FALSE, value);
653 }
654
655 UIWIDGET ui_frameless_textfield_nv(UiObject *obj, char *varname) {
656 return create_textfield_nv(obj, 0, TRUE, FALSE, varname);
657 }
658
659 UIWIDGET ui_passwordfield(UiObject *obj, UiString *value) {
660 return create_textfield(obj, 0, FALSE, TRUE, value);
661 }
662
663 UIWIDGET ui_passwordfield_nv(UiObject *obj, char *varname) {
664 return create_textfield_nv(obj, 0, FALSE, TRUE, varname);
665 }
666
667 UIWIDGET ui_passwordfield_w(UiObject *obj, int width, UiString *value) {
668 return create_textfield(obj, width, FALSE, TRUE, value);
669 }
670
671 UIWIDGET ui_passwordfield_wnv(UiObject *obj, int width, char *varname) {
672 return create_textfield_nv(obj, width, FALSE, TRUE, varname);
673 }
674
675 char* ui_textfield_get(UiString *str) {
676 if(str->value.ptr) {
677 str->value.free(str->value.ptr);
678 }
679 str->value.ptr = g_strdup(gtk_entry_get_text(str->obj));
680 str->value.free = (ui_freefunc)g_free;
681 return str->value.ptr;
682 }
683
684 void ui_textfield_set(UiString *str, char *value) {
685 gtk_entry_set_text(str->obj, value);
686 if(str->value.ptr) {
687 str->value.free(str->value.ptr);
688 str->value.ptr = NULL;
689 str->value.free = NULL;
690 }
691 }

mercurial