ui/gtk/text.c

Tue, 25 Mar 2014 20:25:09 +0100

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Tue, 25 Mar 2014 20:25:09 +0100
changeset 7
431dde3c5fbe
parent 6
05a18c56d9ca
child 8
84a541c6e093
permissions
-rw-r--r--

added Cocoa implementation

5
19d37cb9c96c added textarea
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1 /*
19d37cb9c96c added textarea
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
19d37cb9c96c added textarea
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
3 *
19d37cb9c96c added textarea
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
4 * Copyright 2014 Olaf Wintermann. All rights reserved.
19d37cb9c96c added textarea
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
5 *
19d37cb9c96c added textarea
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
6 * Redistribution and use in source and binary forms, with or without
19d37cb9c96c added textarea
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
7 * modification, are permitted provided that the following conditions are met:
19d37cb9c96c added textarea
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
8 *
19d37cb9c96c added textarea
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
9 * 1. Redistributions of source code must retain the above copyright
19d37cb9c96c added textarea
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
10 * notice, this list of conditions and the following disclaimer.
19d37cb9c96c added textarea
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
11 *
19d37cb9c96c added textarea
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
12 * 2. Redistributions in binary form must reproduce the above copyright
19d37cb9c96c added textarea
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
13 * notice, this list of conditions and the following disclaimer in the
19d37cb9c96c added textarea
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
14 * documentation and/or other materials provided with the distribution.
19d37cb9c96c added textarea
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
15 *
19d37cb9c96c added textarea
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19d37cb9c96c added textarea
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19d37cb9c96c added textarea
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19d37cb9c96c added textarea
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
19d37cb9c96c added textarea
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
19d37cb9c96c added textarea
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
19d37cb9c96c added textarea
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
19d37cb9c96c added textarea
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
19d37cb9c96c added textarea
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
19d37cb9c96c added textarea
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
19d37cb9c96c added textarea
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
26 * POSSIBILITY OF SUCH DAMAGE.
19d37cb9c96c added textarea
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
27 */
19d37cb9c96c added textarea
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
28
19d37cb9c96c added textarea
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
29 #include <stdio.h>
19d37cb9c96c added textarea
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
30 #include <stdlib.h>
6
05a18c56d9ca added undo for text
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
31 #include <string.h>
5
19d37cb9c96c added textarea
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
32
19d37cb9c96c added textarea
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
33 #include "text.h"
19d37cb9c96c added textarea
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
34 #include "container.h"
19d37cb9c96c added textarea
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
35
19d37cb9c96c added textarea
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
36 UIWIDGET ui_textarea(UiObject *obj, UiText *value) {
19d37cb9c96c added textarea
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
37 GtkWidget *text_area = gtk_text_view_new();
19d37cb9c96c added textarea
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
38 gtk_text_view_set_wrap_mode(GTK_TEXT_VIEW(text_area), GTK_WRAP_WORD_CHAR);
19d37cb9c96c added textarea
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
39 g_signal_connect(
19d37cb9c96c added textarea
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
40 text_area,
19d37cb9c96c added textarea
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
41 "realize",
19d37cb9c96c added textarea
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
42 G_CALLBACK(ui_textarea_realize_event),
19d37cb9c96c added textarea
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
43 NULL);
19d37cb9c96c added textarea
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
44
19d37cb9c96c added textarea
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
45 GtkWidget *scroll_area = gtk_scrolled_window_new (NULL, NULL);
19d37cb9c96c added textarea
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
46 gtk_scrolled_window_set_policy(
19d37cb9c96c added textarea
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
47 GTK_SCROLLED_WINDOW(scroll_area),
19d37cb9c96c added textarea
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
48 GTK_POLICY_AUTOMATIC,
19d37cb9c96c added textarea
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
49 GTK_POLICY_AUTOMATIC); // GTK_POLICY_ALWAYS
19d37cb9c96c added textarea
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
50 gtk_container_add(GTK_CONTAINER(scroll_area), text_area);
19d37cb9c96c added textarea
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
51
19d37cb9c96c added textarea
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
52 // font and padding
19d37cb9c96c added textarea
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
53 PangoFontDescription *font;
19d37cb9c96c added textarea
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
54 font = pango_font_description_from_string("Monospace");
19d37cb9c96c added textarea
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
55 gtk_widget_modify_font(text_area, font);
19d37cb9c96c added textarea
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
56 pango_font_description_free(font);
19d37cb9c96c added textarea
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
57
19d37cb9c96c added textarea
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
58 gtk_text_view_set_left_margin(GTK_TEXT_VIEW(text_area), 2);
19d37cb9c96c added textarea
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
59 gtk_text_view_set_right_margin(GTK_TEXT_VIEW(text_area), 2);
19d37cb9c96c added textarea
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
60
19d37cb9c96c added textarea
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
61 // add
19d37cb9c96c added textarea
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
62 UiContainer *ct = uic_get_current_container(obj);
19d37cb9c96c added textarea
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
63 ct->add(ct, scroll_area);
19d37cb9c96c added textarea
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
64
19d37cb9c96c added textarea
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
65 // bind value
19d37cb9c96c added textarea
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
66 if(value) {
19d37cb9c96c added textarea
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
67 value->get = ui_textarea_get;
19d37cb9c96c added textarea
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
68 value->set = ui_textarea_set;
19d37cb9c96c added textarea
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
69 value->value = NULL;
19d37cb9c96c added textarea
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
70 GtkTextBuffer *buf = gtk_text_view_get_buffer(GTK_TEXT_VIEW(text_area));
19d37cb9c96c added textarea
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
71 value->obj = buf;
6
05a18c56d9ca added undo for text
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
72 if(!value->undomgr) {
05a18c56d9ca added undo for text
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
73 value->undomgr = ui_create_undomgr();
05a18c56d9ca added undo for text
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
74 }
5
19d37cb9c96c added textarea
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
75
19d37cb9c96c added textarea
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
76 // register undo manager
19d37cb9c96c added textarea
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
77 g_signal_connect(
19d37cb9c96c added textarea
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
78 buf,
19d37cb9c96c added textarea
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
79 "insert-text",
19d37cb9c96c added textarea
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
80 G_CALLBACK(ui_textbuf_insert),
6
05a18c56d9ca added undo for text
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
81 value);
5
19d37cb9c96c added textarea
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
82 g_signal_connect(
19d37cb9c96c added textarea
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
83 buf,
19d37cb9c96c added textarea
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
84 "delete-range",
19d37cb9c96c added textarea
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
85 G_CALLBACK(ui_textbuf_delete),
6
05a18c56d9ca added undo for text
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
86 value);
5
19d37cb9c96c added textarea
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
87 }
19d37cb9c96c added textarea
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
88
19d37cb9c96c added textarea
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
89 return scroll_area;
19d37cb9c96c added textarea
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
90 }
19d37cb9c96c added textarea
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
91
19d37cb9c96c added textarea
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
92 char* ui_textarea_get(UiText *text) {
19d37cb9c96c added textarea
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
93 if(text->value) {
19d37cb9c96c added textarea
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
94 g_free(text->value);
19d37cb9c96c added textarea
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
95 }
19d37cb9c96c added textarea
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
96 GtkTextBuffer *buf = text->obj;
19d37cb9c96c added textarea
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
97 GtkTextIter start;
19d37cb9c96c added textarea
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
98 GtkTextIter end;
19d37cb9c96c added textarea
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
99 gtk_text_buffer_get_bounds(buf, &start, &end);
19d37cb9c96c added textarea
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
100 char *str = gtk_text_buffer_get_text(buf, &start, &end, FALSE);
19d37cb9c96c added textarea
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
101 text->value = str;
19d37cb9c96c added textarea
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
102 return str;
19d37cb9c96c added textarea
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
103 }
19d37cb9c96c added textarea
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
104
19d37cb9c96c added textarea
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
105 void ui_textarea_set(UiText *text, char *str) {
19d37cb9c96c added textarea
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
106 if(text->value) {
19d37cb9c96c added textarea
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
107 g_free(text->value);
19d37cb9c96c added textarea
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
108 text->value = NULL;
19d37cb9c96c added textarea
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
109 }
19d37cb9c96c added textarea
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
110 gtk_text_buffer_set_text((GtkTextBuffer*)text->obj, str, -1);
19d37cb9c96c added textarea
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
111 }
19d37cb9c96c added textarea
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
112
19d37cb9c96c added textarea
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
113 void ui_textarea_realize_event(GtkWidget *widget, gpointer data) {
19d37cb9c96c added textarea
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
114 gtk_widget_grab_focus(widget);
19d37cb9c96c added textarea
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
115 }
19d37cb9c96c added textarea
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
116
6
05a18c56d9ca added undo for text
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
117
05a18c56d9ca added undo for text
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
118 // undo manager functions
05a18c56d9ca added undo for text
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
119
5
19d37cb9c96c added textarea
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
120 void ui_textbuf_insert(
19d37cb9c96c added textarea
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
121 GtkTextBuffer *textbuffer,
19d37cb9c96c added textarea
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
122 GtkTextIter *location,
19d37cb9c96c added textarea
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
123 char *text,
6
05a18c56d9ca added undo for text
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
124 int length,
5
19d37cb9c96c added textarea
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
125 void *data)
19d37cb9c96c added textarea
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
126 {
6
05a18c56d9ca added undo for text
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
127 UiText *value = data;
05a18c56d9ca added undo for text
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
128 UiUndoMgr *mgr = value->undomgr;
05a18c56d9ca added undo for text
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
129 if(!mgr->event) {
05a18c56d9ca added undo for text
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
130 return;
05a18c56d9ca added undo for text
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
131 }
05a18c56d9ca added undo for text
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
132
05a18c56d9ca added undo for text
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
133 printf("insert\n");
05a18c56d9ca added undo for text
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
134 if(mgr->cur) {
05a18c56d9ca added undo for text
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
135 UcxList *elm = mgr->cur->next;
05a18c56d9ca added undo for text
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
136 while(elm) {
05a18c56d9ca added undo for text
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
137 ui_free_textbuf_op(elm->data);
05a18c56d9ca added undo for text
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
138 UcxList *next = elm->next;
05a18c56d9ca added undo for text
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
139 ucx_list_remove(mgr->begin, elm);
05a18c56d9ca added undo for text
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
140 elm = next;
05a18c56d9ca added undo for text
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
141 }
05a18c56d9ca added undo for text
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
142
05a18c56d9ca added undo for text
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
143 UiTextBufOp *last_op = mgr->cur->data;
05a18c56d9ca added undo for text
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
144 if(ui_check_insertstr(last_op->text, last_op->len, text, length) == 0) {
05a18c56d9ca added undo for text
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
145 // append text to last op
05a18c56d9ca added undo for text
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
146 int ln = last_op->len;
05a18c56d9ca added undo for text
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
147 char *newtext = malloc(ln + length + 1);
05a18c56d9ca added undo for text
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
148 memcpy(newtext, last_op->text, ln);
05a18c56d9ca added undo for text
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
149 memcpy(newtext+ln, text, length);
05a18c56d9ca added undo for text
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
150 newtext[ln+length] = '\0';
05a18c56d9ca added undo for text
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
151
05a18c56d9ca added undo for text
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
152 last_op->text = newtext;
05a18c56d9ca added undo for text
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
153 last_op->len = ln + length;
05a18c56d9ca added undo for text
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
154 last_op->end += length;
05a18c56d9ca added undo for text
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
155
05a18c56d9ca added undo for text
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
156 return;
05a18c56d9ca added undo for text
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
157 }
05a18c56d9ca added undo for text
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
158 }
05a18c56d9ca added undo for text
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
159
05a18c56d9ca added undo for text
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
160 char *dpstr = malloc(length + 1);
05a18c56d9ca added undo for text
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
161 memcpy(dpstr, text, length);
05a18c56d9ca added undo for text
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
162 dpstr[length] = 0;
05a18c56d9ca added undo for text
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
163
05a18c56d9ca added undo for text
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
164 UiTextBufOp *op = malloc(sizeof(UiTextBufOp));
05a18c56d9ca added undo for text
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
165 op->type = UI_TEXTBUF_INSERT;
05a18c56d9ca added undo for text
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
166 op->start = gtk_text_iter_get_offset(location);
05a18c56d9ca added undo for text
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
167 op->end = op->start+length;
05a18c56d9ca added undo for text
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
168 op->len = length;
05a18c56d9ca added undo for text
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
169 op->text = dpstr;
05a18c56d9ca added undo for text
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
170
05a18c56d9ca added undo for text
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
171 UcxList *elm = ucx_list_append(NULL, op);
05a18c56d9ca added undo for text
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
172 mgr->cur = elm;
05a18c56d9ca added undo for text
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
173 mgr->begin = ucx_list_concat(mgr->begin, elm);
5
19d37cb9c96c added textarea
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
174 }
19d37cb9c96c added textarea
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
175
19d37cb9c96c added textarea
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
176 void ui_textbuf_delete(
19d37cb9c96c added textarea
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
177 GtkTextBuffer *textbuffer,
19d37cb9c96c added textarea
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
178 GtkTextIter *start,
19d37cb9c96c added textarea
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
179 GtkTextIter *end,
19d37cb9c96c added textarea
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
180 void *data)
19d37cb9c96c added textarea
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
181 {
6
05a18c56d9ca added undo for text
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
182 UiText *value = data;
05a18c56d9ca added undo for text
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
183 UiUndoMgr *mgr = value->undomgr;
05a18c56d9ca added undo for text
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
184 if(!mgr->event) {
05a18c56d9ca added undo for text
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
185 return;
05a18c56d9ca added undo for text
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
186 }
05a18c56d9ca added undo for text
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
187
05a18c56d9ca added undo for text
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
188 printf("delete\n");
05a18c56d9ca added undo for text
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
189 if(mgr->cur) {
05a18c56d9ca added undo for text
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
190 UcxList *elm = mgr->cur->next;
05a18c56d9ca added undo for text
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
191 while(elm) {
05a18c56d9ca added undo for text
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
192 ui_free_textbuf_op(elm->data);
05a18c56d9ca added undo for text
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
193 UcxList *next = elm->next;
05a18c56d9ca added undo for text
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
194 ucx_list_remove(mgr->begin, elm);
05a18c56d9ca added undo for text
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
195 elm = next;
05a18c56d9ca added undo for text
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
196 }
05a18c56d9ca added undo for text
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
197 }
05a18c56d9ca added undo for text
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
198
05a18c56d9ca added undo for text
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
199 char *text = gtk_text_buffer_get_text(value->obj, start, end, FALSE);
05a18c56d9ca added undo for text
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
200
05a18c56d9ca added undo for text
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
201 UiTextBufOp *op = malloc(sizeof(UiTextBufOp));
05a18c56d9ca added undo for text
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
202 op->type = UI_TEXTBUF_DELETE;
05a18c56d9ca added undo for text
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
203 op->start = gtk_text_iter_get_offset(start);
05a18c56d9ca added undo for text
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
204 op->end = gtk_text_iter_get_offset(end);
05a18c56d9ca added undo for text
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
205 op->len = op->end - op->start;
05a18c56d9ca added undo for text
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
206
05a18c56d9ca added undo for text
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
207 char *dpstr = malloc(op->len + 1);
05a18c56d9ca added undo for text
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
208 memcpy(dpstr, text, op->len);
05a18c56d9ca added undo for text
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
209 dpstr[op->len] = 0;
05a18c56d9ca added undo for text
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
210 op->text = dpstr;
05a18c56d9ca added undo for text
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
211
05a18c56d9ca added undo for text
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
212 UcxList *elm = ucx_list_append(NULL, op);
05a18c56d9ca added undo for text
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
213 mgr->cur = elm;
05a18c56d9ca added undo for text
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
214 mgr->begin = ucx_list_concat(mgr->begin, elm);
05a18c56d9ca added undo for text
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
215 }
05a18c56d9ca added undo for text
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
216
05a18c56d9ca added undo for text
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
217 UiUndoMgr* ui_create_undomgr() {
05a18c56d9ca added undo for text
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
218 UiUndoMgr *mgr = malloc(sizeof(UiUndoMgr));
05a18c56d9ca added undo for text
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
219 mgr->begin = NULL;
05a18c56d9ca added undo for text
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
220 mgr->cur = NULL;
05a18c56d9ca added undo for text
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
221 mgr->length = 0;
05a18c56d9ca added undo for text
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
222 mgr->event = 1;
05a18c56d9ca added undo for text
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
223 return mgr;
05a18c56d9ca added undo for text
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
224 }
05a18c56d9ca added undo for text
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
225
05a18c56d9ca added undo for text
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
226 void ui_free_textbuf_op(UiTextBufOp *op) {
05a18c56d9ca added undo for text
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
227 if(op->text) {
05a18c56d9ca added undo for text
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
228 g_free(op->text);
05a18c56d9ca added undo for text
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
229 }
05a18c56d9ca added undo for text
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
230 free(op);
5
19d37cb9c96c added textarea
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
231 }
6
05a18c56d9ca added undo for text
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
232
05a18c56d9ca added undo for text
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
233 int ui_check_insertstr(char *oldstr, int oldlen, char *newstr, int newlen) {
05a18c56d9ca added undo for text
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
234 // return 1 if oldstr + newstr are one word
05a18c56d9ca added undo for text
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
235
05a18c56d9ca added undo for text
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
236 int has_space = 0;
05a18c56d9ca added undo for text
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
237 for(int i=0;i<oldlen;i++) {
05a18c56d9ca added undo for text
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
238 if(oldstr[i] < 33) {
05a18c56d9ca added undo for text
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
239 has_space = 1;
05a18c56d9ca added undo for text
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
240 break;
05a18c56d9ca added undo for text
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
241 }
05a18c56d9ca added undo for text
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
242 }
05a18c56d9ca added undo for text
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
243
05a18c56d9ca added undo for text
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
244 for(int i=0;i<newlen;i++) {
05a18c56d9ca added undo for text
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
245 if(has_space && newstr[i] > 32) {
05a18c56d9ca added undo for text
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
246 return 1;
05a18c56d9ca added undo for text
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
247 }
05a18c56d9ca added undo for text
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
248 }
05a18c56d9ca added undo for text
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
249
05a18c56d9ca added undo for text
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
250 return 0;
05a18c56d9ca added undo for text
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
251 }
05a18c56d9ca added undo for text
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
252
05a18c56d9ca added undo for text
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
253 void ui_text_undo(UiText *value) {
05a18c56d9ca added undo for text
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
254 UiUndoMgr *mgr = value->undomgr;
05a18c56d9ca added undo for text
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
255
05a18c56d9ca added undo for text
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
256 if(mgr->cur) {
05a18c56d9ca added undo for text
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
257 UiTextBufOp *op = mgr->cur->data;
05a18c56d9ca added undo for text
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
258 mgr->event = 0;
05a18c56d9ca added undo for text
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
259 switch(op->type) {
05a18c56d9ca added undo for text
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
260 case UI_TEXTBUF_INSERT: {
05a18c56d9ca added undo for text
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
261 GtkTextIter begin;
05a18c56d9ca added undo for text
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
262 GtkTextIter end;
05a18c56d9ca added undo for text
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
263 gtk_text_buffer_get_iter_at_offset(value->obj, &begin, op->start);
05a18c56d9ca added undo for text
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
264 gtk_text_buffer_get_iter_at_offset(value->obj, &end, op->end);
05a18c56d9ca added undo for text
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
265 gtk_text_buffer_delete(value->obj, &begin, &end);
05a18c56d9ca added undo for text
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
266 break;
05a18c56d9ca added undo for text
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
267 }
05a18c56d9ca added undo for text
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
268 case UI_TEXTBUF_DELETE: {
05a18c56d9ca added undo for text
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
269 GtkTextIter begin;
05a18c56d9ca added undo for text
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
270 GtkTextIter end;
05a18c56d9ca added undo for text
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
271 gtk_text_buffer_get_iter_at_offset(value->obj, &begin, op->start);
05a18c56d9ca added undo for text
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
272 gtk_text_buffer_get_iter_at_offset(value->obj, &end, op->end);
05a18c56d9ca added undo for text
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
273 gtk_text_buffer_insert(value->obj, &begin, op->text, op->len);
05a18c56d9ca added undo for text
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
274 break;
05a18c56d9ca added undo for text
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
275 }
05a18c56d9ca added undo for text
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
276 }
05a18c56d9ca added undo for text
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
277 mgr->event = 1;
05a18c56d9ca added undo for text
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
278 mgr->cur = mgr->cur->prev;
05a18c56d9ca added undo for text
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
279 }
05a18c56d9ca added undo for text
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
280 }
05a18c56d9ca added undo for text
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
281
05a18c56d9ca added undo for text
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
282 void ui_text_redo(UiText *value) {
05a18c56d9ca added undo for text
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
283
05a18c56d9ca added undo for text
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
284 }

mercurial