ui/gtk/text.c

changeset 0
804d8803eade
child 2
ea89bbb0c4c8
equal deleted inserted replaced
-1:000000000000 0:804d8803eade
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 static void selection_handler(
38 GtkTextBuffer *buf,
39 GtkTextIter *location,
40 GtkTextMark *mark,
41 UiTextArea *textview)
42 {
43 const char *mname = gtk_text_mark_get_name(mark);
44 if(mname) {
45 GtkTextIter begin;
46 GtkTextIter end;
47 int sel = gtk_text_buffer_get_selection_bounds (buf, &begin, &end);
48 if(sel != textview->last_selection_state) {
49 if(sel) {
50 ui_set_group(textview->ctx, UI_GROUP_SELECTION);
51 } else {
52 ui_unset_group(textview->ctx, UI_GROUP_SELECTION);
53 }
54 }
55 textview->last_selection_state = sel;
56 }
57 }
58
59 UIWIDGET ui_textarea_var(UiObject *obj, UiVar *var) {
60 GtkWidget *text_area = gtk_text_view_new();
61 gtk_text_view_set_wrap_mode(GTK_TEXT_VIEW(text_area), GTK_WRAP_WORD_CHAR);
62 g_signal_connect(
63 text_area,
64 "realize",
65 G_CALLBACK(ui_textarea_realize_event),
66 NULL);
67
68 UiTextArea *uitext = malloc(sizeof(UiTextArea));
69 uitext->ctx = obj->ctx;
70 uitext->var = var;
71 uitext->last_selection_state = 0;
72
73 g_signal_connect(
74 text_area,
75 "destroy",
76 G_CALLBACK(ui_textarea_destroy),
77 uitext);
78
79 GtkWidget *scroll_area = gtk_scrolled_window_new (NULL, NULL);
80 gtk_scrolled_window_set_policy(
81 GTK_SCROLLED_WINDOW(scroll_area),
82 GTK_POLICY_AUTOMATIC,
83 GTK_POLICY_AUTOMATIC); // GTK_POLICY_ALWAYS
84 gtk_container_add(GTK_CONTAINER(scroll_area), text_area);
85
86 // font and padding
87 PangoFontDescription *font;
88 font = pango_font_description_from_string("Monospace");
89 gtk_widget_modify_font(text_area, font);
90 pango_font_description_free(font);
91
92 gtk_text_view_set_left_margin(GTK_TEXT_VIEW(text_area), 2);
93 gtk_text_view_set_right_margin(GTK_TEXT_VIEW(text_area), 2);
94
95 // add
96 UiContainer *ct = uic_get_current_container(obj);
97 ct->add(ct, scroll_area, TRUE);
98
99 // bind value
100 UiText *value = var->value;
101 if(value) {
102 GtkTextBuffer *buf = gtk_text_view_get_buffer(GTK_TEXT_VIEW(text_area));
103
104 if(value->value.ptr) {
105 gtk_text_buffer_set_text(buf, value->value.ptr, -1);
106 value->value.free(value->value.ptr);
107 }
108
109 value->get = ui_textarea_get;
110 value->set = ui_textarea_set;
111 value->getsubstr = ui_textarea_getsubstr;
112 value->insert = ui_textarea_insert;
113 value->setposition = ui_textarea_setposition;
114 value->position = ui_textarea_position;
115 value->selection = ui_textarea_selection;
116 value->length = ui_textarea_length;
117 value->remove = ui_textarea_remove;
118 value->value.ptr = NULL;
119 value->value.free = NULL;
120 value->obj = buf;
121 if(!value->undomgr) {
122 value->undomgr = ui_create_undomgr();
123 }
124
125 g_signal_connect(
126 buf,
127 "changed",
128 G_CALLBACK(ui_textbuf_changed),
129 uitext);
130
131 // register undo manager
132 g_signal_connect(
133 buf,
134 "insert-text",
135 G_CALLBACK(ui_textbuf_insert),
136 var);
137 g_signal_connect(
138 buf,
139 "delete-range",
140 G_CALLBACK(ui_textbuf_delete),
141 var);
142 g_signal_connect(
143 buf,
144 "mark-set",
145 G_CALLBACK(selection_handler),
146 uitext);
147 }
148
149 return scroll_area;
150 }
151
152 void ui_textarea_destroy(GtkWidget *object, UiTextArea *textarea) {
153 ui_destroy_boundvar(textarea->ctx, textarea->var);
154 free(textarea);
155 }
156
157 UIWIDGET ui_textarea(UiObject *obj, UiText *value) {
158 UiVar *var = malloc(sizeof(UiVar));
159 var->value = value;
160 var->type = UI_VAR_SPECIAL;
161 var->from = NULL;
162 var->from_ctx = NULL;
163 return ui_textarea_var(obj, var);
164 }
165
166 UIWIDGET ui_textarea_nv(UiObject *obj, char *varname) {
167 UiVar *var = uic_create_var(obj->ctx, varname, UI_VAR_TEXT);
168 if(var) {
169 return ui_textarea_var(obj, var);
170 } else {
171 // TODO: error
172 }
173 return NULL;
174 }
175
176 char* ui_textarea_get(UiText *text) {
177 if(text->value.ptr) {
178 text->value.free(text->value.ptr);
179 }
180 GtkTextBuffer *buf = text->obj;
181 GtkTextIter start;
182 GtkTextIter end;
183 gtk_text_buffer_get_bounds(buf, &start, &end);
184 char *str = gtk_text_buffer_get_text(buf, &start, &end, FALSE);
185 text->value.ptr = g_strdup(str);
186 text->value.free = (ui_freefunc)g_free;
187 return str;
188 }
189
190 void ui_textarea_set(UiText *text, char *str) {
191 gtk_text_buffer_set_text((GtkTextBuffer*)text->obj, str, -1);
192 if(text->value.ptr) {
193 text->value.free(text->value.ptr);
194 }
195 text->value.ptr = NULL;
196 text->value.free = NULL;
197 }
198
199 char* ui_textarea_getsubstr(UiText *text, int begin, int end) {
200 if(text->value.ptr) {
201 text->value.free(text->value.ptr);
202 }
203 GtkTextBuffer *buf = text->obj;
204 GtkTextIter ib;
205 GtkTextIter ie;
206 gtk_text_buffer_get_iter_at_offset(text->obj, &ib, begin);
207 gtk_text_buffer_get_iter_at_offset(text->obj, &ie, end);
208 char *str = gtk_text_buffer_get_text(buf, &ib, &ie, FALSE);
209 text->value.ptr = g_strdup(str);
210 text->value.free = (ui_freefunc)g_free;
211 return str;
212 }
213
214 void ui_textarea_insert(UiText *text, int pos, char *str) {
215 GtkTextIter offset;
216 gtk_text_buffer_get_iter_at_offset(text->obj, &offset, pos);
217 gtk_text_buffer_insert(text->obj, &offset, str, -1);
218 if(text->value.ptr) {
219 text->value.free(text->value.ptr);
220 }
221 text->value.ptr = NULL;
222 text->value.free = NULL;
223 }
224
225 void ui_textarea_setposition(UiText *text, int pos) {
226 GtkTextIter iter;
227 gtk_text_buffer_get_iter_at_offset(text->obj, &iter, pos);
228 gtk_text_buffer_place_cursor(text->obj, &iter);
229 }
230
231 int ui_textarea_position(UiText *text) {
232 GtkTextIter begin;
233 GtkTextIter end;
234 gtk_text_buffer_get_selection_bounds(text->obj, &begin, &end);
235 text->pos = gtk_text_iter_get_offset(&begin);
236 return text->pos;
237 }
238
239 void ui_textarea_selection(UiText *text, int *begin, int *end) {
240 GtkTextIter b;
241 GtkTextIter e;
242 gtk_text_buffer_get_selection_bounds(text->obj, &b, &e);
243 *begin = gtk_text_iter_get_offset(&b);
244 *end = gtk_text_iter_get_offset(&e);
245 }
246
247 int ui_textarea_length(UiText *text) {
248 GtkTextBuffer *buf = text->obj;
249 GtkTextIter start;
250 GtkTextIter end;
251 gtk_text_buffer_get_bounds(buf, &start, &end);
252 return gtk_text_iter_get_offset(&end);
253 }
254
255 void ui_textarea_remove(UiText *text, int begin, int end) {
256 GtkTextBuffer *buf = text->obj;
257 GtkTextIter ib;
258 GtkTextIter ie;
259 gtk_text_buffer_get_iter_at_offset(buf, &ib, begin);
260 gtk_text_buffer_get_iter_at_offset(buf, &ie, end);
261 gtk_text_buffer_delete(buf, &ib, &ie);
262 }
263
264 void ui_textarea_realize_event(GtkWidget *widget, gpointer data) {
265 gtk_widget_grab_focus(widget);
266 }
267
268
269
270 void ui_textbuf_changed(GtkTextBuffer *textbuffer, UiTextArea *textarea) {
271 UiText *value = textarea->var->value;
272 if(value->observers) {
273 UiEvent e;
274 e.obj = textarea->ctx->obj;
275 e.window = e.obj->window;
276 e.document = textarea->ctx->document;
277 e.eventdata = value;
278 e.intval = 0;
279 ui_notify_evt(value->observers, &e);
280 }
281 }
282
283 // undo manager functions
284
285 void ui_textbuf_insert(
286 GtkTextBuffer *textbuffer,
287 GtkTextIter *location,
288 char *text,
289 int length,
290 void *data)
291 {
292 UiVar *var = data;
293 UiText *value = var->value;
294 if(!value->undomgr) {
295 value->undomgr = ui_create_undomgr();
296 }
297 UiUndoMgr *mgr = value->undomgr;
298 if(!mgr->event) {
299 return;
300 }
301
302 if(mgr->cur) {
303 UcxList *elm = mgr->cur->next;
304 if(elm) {
305 mgr->cur->next = NULL;
306 while(elm) {
307 elm->prev = NULL;
308 UcxList *next = elm->next;
309 ui_free_textbuf_op(elm->data);
310 free(elm);
311 elm = next;
312 }
313 }
314
315 UiTextBufOp *last_op = mgr->cur->data;
316 if(
317 last_op->type == UI_TEXTBUF_INSERT &&
318 ui_check_insertstr(last_op->text, last_op->len, text, length) == 0)
319 {
320 // append text to last op
321 int ln = last_op->len;
322 char *newtext = malloc(ln + length + 1);
323 memcpy(newtext, last_op->text, ln);
324 memcpy(newtext+ln, text, length);
325 newtext[ln+length] = '\0';
326
327 last_op->text = newtext;
328 last_op->len = ln + length;
329 last_op->end += length;
330
331 return;
332 }
333 }
334
335 char *dpstr = malloc(length + 1);
336 memcpy(dpstr, text, length);
337 dpstr[length] = 0;
338
339 UiTextBufOp *op = malloc(sizeof(UiTextBufOp));
340 op->type = UI_TEXTBUF_INSERT;
341 op->start = gtk_text_iter_get_offset(location);
342 op->end = op->start+length;
343 op->len = length;
344 op->text = dpstr;
345
346 UcxList *elm = ucx_list_append(NULL, op);
347 mgr->cur = elm;
348 mgr->begin = ucx_list_concat(mgr->begin, elm);
349 }
350
351 void ui_textbuf_delete(
352 GtkTextBuffer *textbuffer,
353 GtkTextIter *start,
354 GtkTextIter *end,
355 void *data)
356 {
357 UiVar *var = data;
358 UiText *value = var->value;
359 if(!value->undomgr) {
360 value->undomgr = ui_create_undomgr();
361 }
362 UiUndoMgr *mgr = value->undomgr;
363 if(!mgr->event) {
364 return;
365 }
366
367 if(mgr->cur) {
368 UcxList *elm = mgr->cur->next;
369 if(elm) {
370 mgr->cur->next = NULL;
371 while(elm) {
372 elm->prev = NULL;
373 UcxList *next = elm->next;
374 ui_free_textbuf_op(elm->data);
375 free(elm);
376 elm = next;
377 }
378 }
379 }
380
381 char *text = gtk_text_buffer_get_text(value->obj, start, end, FALSE);
382
383 UiTextBufOp *op = malloc(sizeof(UiTextBufOp));
384 op->type = UI_TEXTBUF_DELETE;
385 op->start = gtk_text_iter_get_offset(start);
386 op->end = gtk_text_iter_get_offset(end);
387 op->len = op->end - op->start;
388
389 char *dpstr = malloc(op->len + 1);
390 memcpy(dpstr, text, op->len);
391 dpstr[op->len] = 0;
392 op->text = dpstr;
393
394 UcxList *elm = ucx_list_append(NULL, op);
395 mgr->cur = elm;
396 mgr->begin = ucx_list_concat(mgr->begin, elm);
397 }
398
399 UiUndoMgr* ui_create_undomgr() {
400 UiUndoMgr *mgr = malloc(sizeof(UiUndoMgr));
401 mgr->begin = NULL;
402 mgr->cur = NULL;
403 mgr->length = 0;
404 mgr->event = 1;
405 return mgr;
406 }
407
408 void ui_free_textbuf_op(UiTextBufOp *op) {
409 if(op->text) {
410 free(op->text);
411 }
412 free(op);
413 }
414
415 int ui_check_insertstr(char *oldstr, int oldlen, char *newstr, int newlen) {
416 // return 1 if oldstr + newstr are one word
417
418 int has_space = 0;
419 for(int i=0;i<oldlen;i++) {
420 if(oldstr[i] < 33) {
421 has_space = 1;
422 break;
423 }
424 }
425
426 for(int i=0;i<newlen;i++) {
427 if(has_space && newstr[i] > 32) {
428 return 1;
429 }
430 }
431
432 return 0;
433 }
434
435 void ui_text_undo(UiText *value) {
436 UiUndoMgr *mgr = value->undomgr;
437
438 if(mgr->cur) {
439 UiTextBufOp *op = mgr->cur->data;
440 mgr->event = 0;
441 switch(op->type) {
442 case UI_TEXTBUF_INSERT: {
443 GtkTextIter begin;
444 GtkTextIter end;
445 gtk_text_buffer_get_iter_at_offset(value->obj, &begin, op->start);
446 gtk_text_buffer_get_iter_at_offset(value->obj, &end, op->end);
447 gtk_text_buffer_delete(value->obj, &begin, &end);
448 break;
449 }
450 case UI_TEXTBUF_DELETE: {
451 GtkTextIter begin;
452 GtkTextIter end;
453 gtk_text_buffer_get_iter_at_offset(value->obj, &begin, op->start);
454 gtk_text_buffer_get_iter_at_offset(value->obj, &end, op->end);
455 gtk_text_buffer_insert(value->obj, &begin, op->text, op->len);
456 break;
457 }
458 }
459 mgr->event = 1;
460 mgr->cur = mgr->cur->prev;
461 }
462 }
463
464 void ui_text_redo(UiText *value) {
465 UiUndoMgr *mgr = value->undomgr;
466
467 UcxList *elm = NULL;
468 if(mgr->cur) {
469 if(mgr->cur->next) {
470 elm = mgr->cur->next;
471 }
472 } else if(mgr->begin) {
473 elm = mgr->begin;
474 }
475
476 if(elm) {
477 UiTextBufOp *op = elm->data;
478 mgr->event = 0;
479 switch(op->type) {
480 case UI_TEXTBUF_INSERT: {
481 GtkTextIter begin;
482 GtkTextIter end;
483 gtk_text_buffer_get_iter_at_offset(value->obj, &begin, op->start);
484 gtk_text_buffer_get_iter_at_offset(value->obj, &end, op->end);
485 gtk_text_buffer_insert(value->obj, &begin, op->text, op->len);
486 break;
487 }
488 case UI_TEXTBUF_DELETE: {
489 GtkTextIter begin;
490 GtkTextIter end;
491 gtk_text_buffer_get_iter_at_offset(value->obj, &begin, op->start);
492 gtk_text_buffer_get_iter_at_offset(value->obj, &end, op->end);
493 gtk_text_buffer_delete(value->obj, &begin, &end);
494 break;
495 }
496 }
497 mgr->event = 1;
498 mgr->cur = elm;
499 }
500 }
501
502
503 static UIWIDGET create_textfield_var(UiObject *obj, int width, UiBool frameless, UiBool password, UiVar *var) {
504 GtkWidget *textfield = gtk_entry_new();
505
506 UiTextField *uitext = malloc(sizeof(UiTextField));
507 uitext->ctx = obj->ctx;
508 uitext->var = var;
509
510 g_signal_connect(
511 textfield,
512 "destroy",
513 G_CALLBACK(ui_textfield_destroy),
514 uitext);
515
516 if(width > 0) {
517 gtk_entry_set_width_chars(GTK_ENTRY(textfield), width);
518 }
519 if(frameless) {
520 // TODO: gtk2legacy workaroud
521 gtk_entry_set_has_frame(GTK_ENTRY(textfield), FALSE);
522 }
523 if(password) {
524 gtk_entry_set_visibility(GTK_ENTRY(textfield), FALSE);
525 }
526
527 UiContainer *ct = uic_get_current_container(obj);
528 ct->add(ct, textfield, FALSE);
529
530 if(var) {
531 UiString *value = var->value;
532 if(value->value.ptr) {
533 gtk_entry_set_text(GTK_ENTRY(textfield), value->value.ptr);
534 value->value.free(value->value.ptr);
535 value->value.ptr = NULL;
536 value->value.free = NULL;
537 }
538
539 value->get = ui_textfield_get;
540 value->set = ui_textfield_set;
541 value->value.ptr = NULL;
542 value->value.free = NULL;
543 value->obj = GTK_ENTRY(textfield);
544
545 g_signal_connect(
546 textfield,
547 "changed",
548 G_CALLBACK(ui_textfield_changed),
549 uitext);
550 }
551
552 return textfield;
553 }
554
555 static UIWIDGET create_textfield_nv(UiObject *obj, int width, UiBool frameless, UiBool password, char *varname) {
556 UiVar *var = uic_create_var(obj->ctx, varname, UI_VAR_STRING);
557 if(var) {
558 return create_textfield_var(obj, width, frameless, password, var);
559 } else {
560 // TODO: error
561 }
562 return NULL;
563 }
564
565 static UIWIDGET create_textfield(UiObject *obj, int width, UiBool frameless, UiBool password, UiString *value) {
566 UiVar *var = NULL;
567 if(value) {
568 var = malloc(sizeof(UiVar));
569 var->value = value;
570 var->type = UI_VAR_SPECIAL;
571 }
572 return create_textfield_var(obj, width, frameless, password, var);
573 }
574
575 void ui_textfield_destroy(GtkWidget *object, UiTextField *textfield) {
576 if(textfield->var) {
577 ui_destroy_boundvar(textfield->ctx, textfield->var);
578 }
579 free(textfield);
580 }
581
582 void ui_textfield_changed(GtkEditable *editable, UiTextField *textfield) {
583 UiString *value = textfield->var->value;
584 if(value->observers) {
585 UiEvent e;
586 e.obj = textfield->ctx->obj;
587 e.window = e.obj->window;
588 e.document = textfield->ctx->document;
589 e.eventdata = value;
590 e.intval = 0;
591 ui_notify_evt(value->observers, &e);
592 }
593 }
594
595 UIWIDGET ui_textfield(UiObject *obj, UiString *value) {
596 return create_textfield(obj, 0, FALSE, FALSE, value);
597 }
598
599 UIWIDGET ui_textfield_nv(UiObject *obj, char *varname) {
600 return create_textfield_nv(obj, 0, FALSE, FALSE, varname);
601 }
602
603 UIWIDGET ui_textfield_w(UiObject *obj, int width, UiString *value) {
604 return create_textfield(obj, width, FALSE, FALSE, value);
605 }
606
607 UIWIDGET ui_textfield_wnv(UiObject *obj, int width, char *varname) {
608 return create_textfield_nv(obj, width, FALSE, FALSE, varname);
609 }
610
611 UIWIDGET ui_frameless_textfield(UiObject *obj, UiString *value) {
612 return create_textfield(obj, 0, TRUE, FALSE, value);
613 }
614
615 UIWIDGET ui_frameless_textfield_nv(UiObject *obj, char *varname) {
616 return create_textfield_nv(obj, 0, TRUE, FALSE, varname);
617 }
618
619 UIWIDGET ui_passwordfield(UiObject *obj, UiString *value) {
620 return create_textfield(obj, 0, FALSE, TRUE, value);
621 }
622
623 UIWIDGET ui_passwordfield_nv(UiObject *obj, char *varname) {
624 return create_textfield_nv(obj, 0, FALSE, TRUE, varname);
625 }
626
627 UIWIDGET ui_passwordfield_w(UiObject *obj, int width, UiString *value) {
628 return create_textfield(obj, width, FALSE, TRUE, value);
629 }
630
631 UIWIDGET ui_passwordfield_wnv(UiObject *obj, int width, char *varname) {
632 return create_textfield_nv(obj, width, FALSE, TRUE, varname);
633 }
634
635 char* ui_textfield_get(UiString *str) {
636 if(str->value.ptr) {
637 str->value.free(str->value.ptr);
638 }
639 str->value.ptr = g_strdup(gtk_entry_get_text(str->obj));
640 str->value.free = (ui_freefunc)g_free;
641 return str->value.ptr;
642 }
643
644 void ui_textfield_set(UiString *str, char *value) {
645 gtk_entry_set_text(str->obj, value);
646 if(str->value.ptr) {
647 str->value.free(str->value.ptr);
648 str->value.ptr = NULL;
649 str->value.free = NULL;
650 }
651 }

mercurial