ui/gtk/text.c

Sat, 17 Sep 2016 19:57:55 +0200

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Sat, 17 Sep 2016 19:57:55 +0200
changeset 129
5babf09f5f19
parent 122
e82b01d17a78
child 131
774b741984a2
permissions
-rw-r--r--

fixes stultus commit

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"
33
458831c574f4 added listview, sidebar and toolbar image button (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 29
diff changeset
35 #include "../common/context.h"
458831c574f4 added listview, sidebar and toolbar image button (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 29
diff changeset
36 #include "../common/document.h"
5
19d37cb9c96c added textarea
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
37
22
bcf880b29bc3 textarea automatically sets selection group (GTK, Motif)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 12
diff changeset
38 static void selection_handler(
bcf880b29bc3 textarea automatically sets selection group (GTK, Motif)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 12
diff changeset
39 GtkTextBuffer *buf,
bcf880b29bc3 textarea automatically sets selection group (GTK, Motif)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 12
diff changeset
40 GtkTextIter *location,
bcf880b29bc3 textarea automatically sets selection group (GTK, Motif)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 12
diff changeset
41 GtkTextMark *mark,
bcf880b29bc3 textarea automatically sets selection group (GTK, Motif)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 12
diff changeset
42 UiTextArea *textview)
bcf880b29bc3 textarea automatically sets selection group (GTK, Motif)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 12
diff changeset
43 {
bcf880b29bc3 textarea automatically sets selection group (GTK, Motif)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 12
diff changeset
44 const char *mname = gtk_text_mark_get_name(mark);
bcf880b29bc3 textarea automatically sets selection group (GTK, Motif)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 12
diff changeset
45 if(mname) {
bcf880b29bc3 textarea automatically sets selection group (GTK, Motif)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 12
diff changeset
46 GtkTextIter begin;
bcf880b29bc3 textarea automatically sets selection group (GTK, Motif)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 12
diff changeset
47 GtkTextIter end;
bcf880b29bc3 textarea automatically sets selection group (GTK, Motif)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 12
diff changeset
48 int sel = gtk_text_buffer_get_selection_bounds (buf, &begin, &end);
bcf880b29bc3 textarea automatically sets selection group (GTK, Motif)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 12
diff changeset
49 if(sel != textview->last_selection_state) {
bcf880b29bc3 textarea automatically sets selection group (GTK, Motif)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 12
diff changeset
50 if(sel) {
bcf880b29bc3 textarea automatically sets selection group (GTK, Motif)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 12
diff changeset
51 ui_set_group(textview->ctx, UI_GROUP_SELECTION);
bcf880b29bc3 textarea automatically sets selection group (GTK, Motif)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 12
diff changeset
52 } else {
bcf880b29bc3 textarea automatically sets selection group (GTK, Motif)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 12
diff changeset
53 ui_unset_group(textview->ctx, UI_GROUP_SELECTION);
bcf880b29bc3 textarea automatically sets selection group (GTK, Motif)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 12
diff changeset
54 }
bcf880b29bc3 textarea automatically sets selection group (GTK, Motif)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 12
diff changeset
55 }
bcf880b29bc3 textarea automatically sets selection group (GTK, Motif)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 12
diff changeset
56 textview->last_selection_state = sel;
bcf880b29bc3 textarea automatically sets selection group (GTK, Motif)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 12
diff changeset
57 }
bcf880b29bc3 textarea automatically sets selection group (GTK, Motif)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 12
diff changeset
58 }
bcf880b29bc3 textarea automatically sets selection group (GTK, Motif)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 12
diff changeset
59
5
19d37cb9c96c added textarea
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
60 UIWIDGET ui_textarea(UiObject *obj, UiText *value) {
19d37cb9c96c added textarea
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
61 GtkWidget *text_area = gtk_text_view_new();
19d37cb9c96c added textarea
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
62 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
63 g_signal_connect(
22
bcf880b29bc3 textarea automatically sets selection group (GTK, Motif)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 12
diff changeset
64 text_area,
bcf880b29bc3 textarea automatically sets selection group (GTK, Motif)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 12
diff changeset
65 "realize",
bcf880b29bc3 textarea automatically sets selection group (GTK, Motif)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 12
diff changeset
66 G_CALLBACK(ui_textarea_realize_event),
bcf880b29bc3 textarea automatically sets selection group (GTK, Motif)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 12
diff changeset
67 NULL);
bcf880b29bc3 textarea automatically sets selection group (GTK, Motif)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 12
diff changeset
68 g_signal_connect(
bcf880b29bc3 textarea automatically sets selection group (GTK, Motif)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 12
diff changeset
69 text_area,
bcf880b29bc3 textarea automatically sets selection group (GTK, Motif)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 12
diff changeset
70 "selection-clear-event",
bcf880b29bc3 textarea automatically sets selection group (GTK, Motif)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 12
diff changeset
71 G_CALLBACK(selection_handler),
bcf880b29bc3 textarea automatically sets selection group (GTK, Motif)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 12
diff changeset
72 NULL);
bcf880b29bc3 textarea automatically sets selection group (GTK, Motif)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 12
diff changeset
73
bcf880b29bc3 textarea automatically sets selection group (GTK, Motif)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 12
diff changeset
74 UiTextArea *uitext = ucx_mempool_malloc(
bcf880b29bc3 textarea automatically sets selection group (GTK, Motif)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 12
diff changeset
75 obj->ctx->mempool,
bcf880b29bc3 textarea automatically sets selection group (GTK, Motif)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 12
diff changeset
76 sizeof(UiTextArea));
bcf880b29bc3 textarea automatically sets selection group (GTK, Motif)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 12
diff changeset
77 uitext->ctx = obj->ctx;
bcf880b29bc3 textarea automatically sets selection group (GTK, Motif)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 12
diff changeset
78 uitext->last_selection_state = 0;
5
19d37cb9c96c added textarea
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
79
19d37cb9c96c added textarea
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
80 GtkWidget *scroll_area = gtk_scrolled_window_new (NULL, NULL);
19d37cb9c96c added textarea
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
81 gtk_scrolled_window_set_policy(
19d37cb9c96c added textarea
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
82 GTK_SCROLLED_WINDOW(scroll_area),
19d37cb9c96c added textarea
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
83 GTK_POLICY_AUTOMATIC,
19d37cb9c96c added textarea
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
84 GTK_POLICY_AUTOMATIC); // GTK_POLICY_ALWAYS
19d37cb9c96c added textarea
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
85 gtk_container_add(GTK_CONTAINER(scroll_area), text_area);
19d37cb9c96c added textarea
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
86
19d37cb9c96c added textarea
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
87 // font and padding
19d37cb9c96c added textarea
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
88 PangoFontDescription *font;
19d37cb9c96c added textarea
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
89 font = pango_font_description_from_string("Monospace");
19d37cb9c96c added textarea
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
90 gtk_widget_modify_font(text_area, font);
19d37cb9c96c added textarea
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
91 pango_font_description_free(font);
19d37cb9c96c added textarea
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
92
19d37cb9c96c added textarea
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
93 gtk_text_view_set_left_margin(GTK_TEXT_VIEW(text_area), 2);
19d37cb9c96c added textarea
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
94 gtk_text_view_set_right_margin(GTK_TEXT_VIEW(text_area), 2);
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 // add
19d37cb9c96c added textarea
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
97 UiContainer *ct = uic_get_current_container(obj);
59
eb6611be50c7 added box container (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 56
diff changeset
98 ct->add(ct, scroll_area, TRUE);
5
19d37cb9c96c added textarea
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
99
19d37cb9c96c added textarea
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
100 // bind value
19d37cb9c96c added textarea
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
101 if(value) {
33
458831c574f4 added listview, sidebar and toolbar image button (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 29
diff changeset
102 GtkTextBuffer *buf = gtk_text_view_get_buffer(GTK_TEXT_VIEW(text_area));
458831c574f4 added listview, sidebar and toolbar image button (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 29
diff changeset
103
458831c574f4 added listview, sidebar and toolbar image button (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 29
diff changeset
104 if(value->value) {
458831c574f4 added listview, sidebar and toolbar image button (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 29
diff changeset
105 gtk_text_buffer_set_text(buf, value->value, -1);
56
87e3a5dc66dd added text widget (Qt)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 33
diff changeset
106 // TODO: free value
33
458831c574f4 added listview, sidebar and toolbar image button (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 29
diff changeset
107 }
458831c574f4 added listview, sidebar and toolbar image button (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 29
diff changeset
108
5
19d37cb9c96c added textarea
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
109 value->get = ui_textarea_get;
19d37cb9c96c added textarea
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
110 value->set = ui_textarea_set;
12
fe94e0fb9ef3 added some text functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 9
diff changeset
111 value->getsubstr = ui_textarea_getsubstr;
fe94e0fb9ef3 added some text functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 9
diff changeset
112 value->insert = ui_textarea_insert;
90
2019fdbaadfd persistent textarea cursor position
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 65
diff changeset
113 value->setposition = ui_textarea_setposition;
27
77b09bb52ca0 added groups for toolbar items and copy & paste (GTK, Motif)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 22
diff changeset
114 value->position = ui_textarea_position;
77b09bb52ca0 added groups for toolbar items and copy & paste (GTK, Motif)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 22
diff changeset
115 value->selection = ui_textarea_selection;
29
c96169444d88 added locale support (Cocoa) and ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 27
diff changeset
116 value->length = ui_textarea_length;
33
458831c574f4 added listview, sidebar and toolbar image button (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 29
diff changeset
117 value->remove = ui_textarea_remove;
5
19d37cb9c96c added textarea
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
118 value->value = NULL;
19d37cb9c96c added textarea
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
119 value->obj = buf;
6
05a18c56d9ca added undo for text
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
120 if(!value->undomgr) {
05a18c56d9ca added undo for text
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
121 value->undomgr = ui_create_undomgr();
05a18c56d9ca added undo for text
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
122 }
5
19d37cb9c96c added textarea
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
123
19d37cb9c96c added textarea
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
124 // register undo manager
19d37cb9c96c added textarea
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
125 g_signal_connect(
19d37cb9c96c added textarea
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
126 buf,
19d37cb9c96c added textarea
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
127 "insert-text",
19d37cb9c96c added textarea
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
128 G_CALLBACK(ui_textbuf_insert),
6
05a18c56d9ca added undo for text
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
129 value);
5
19d37cb9c96c added textarea
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
130 g_signal_connect(
19d37cb9c96c added textarea
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
131 buf,
19d37cb9c96c added textarea
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
132 "delete-range",
19d37cb9c96c added textarea
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
133 G_CALLBACK(ui_textbuf_delete),
6
05a18c56d9ca added undo for text
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
134 value);
22
bcf880b29bc3 textarea automatically sets selection group (GTK, Motif)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 12
diff changeset
135
bcf880b29bc3 textarea automatically sets selection group (GTK, Motif)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 12
diff changeset
136 g_signal_connect(
bcf880b29bc3 textarea automatically sets selection group (GTK, Motif)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 12
diff changeset
137 buf,
bcf880b29bc3 textarea automatically sets selection group (GTK, Motif)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 12
diff changeset
138 "mark-set",
bcf880b29bc3 textarea automatically sets selection group (GTK, Motif)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 12
diff changeset
139 G_CALLBACK(selection_handler),
bcf880b29bc3 textarea automatically sets selection group (GTK, Motif)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 12
diff changeset
140 uitext);
5
19d37cb9c96c added textarea
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
141 }
19d37cb9c96c added textarea
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
142
19d37cb9c96c added textarea
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
143 return scroll_area;
19d37cb9c96c added textarea
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
144 }
19d37cb9c96c added textarea
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
145
33
458831c574f4 added listview, sidebar and toolbar image button (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 29
diff changeset
146 UIWIDGET ui_textarea_nv(UiObject *obj, char *varname) {
458831c574f4 added listview, sidebar and toolbar image button (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 29
diff changeset
147 UiVar *var = uic_connect_var(obj->ctx, varname, UI_VAR_TEXT);
458831c574f4 added listview, sidebar and toolbar image button (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 29
diff changeset
148 if(var) {
458831c574f4 added listview, sidebar and toolbar image button (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 29
diff changeset
149 UiText *value = var->value;
458831c574f4 added listview, sidebar and toolbar image button (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 29
diff changeset
150 return ui_textarea(obj, value);
458831c574f4 added listview, sidebar and toolbar image button (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 29
diff changeset
151 } else {
458831c574f4 added listview, sidebar and toolbar image button (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 29
diff changeset
152 // TODO: error
458831c574f4 added listview, sidebar and toolbar image button (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 29
diff changeset
153 }
458831c574f4 added listview, sidebar and toolbar image button (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 29
diff changeset
154 return NULL;
458831c574f4 added listview, sidebar and toolbar image button (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 29
diff changeset
155 }
458831c574f4 added listview, sidebar and toolbar image button (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 29
diff changeset
156
5
19d37cb9c96c added textarea
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
157 char* ui_textarea_get(UiText *text) {
19d37cb9c96c added textarea
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
158 if(text->value) {
19d37cb9c96c added textarea
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
159 g_free(text->value);
19d37cb9c96c added textarea
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
160 }
19d37cb9c96c added textarea
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
161 GtkTextBuffer *buf = text->obj;
19d37cb9c96c added textarea
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
162 GtkTextIter start;
19d37cb9c96c added textarea
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
163 GtkTextIter end;
19d37cb9c96c added textarea
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
164 gtk_text_buffer_get_bounds(buf, &start, &end);
19d37cb9c96c added textarea
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
165 char *str = gtk_text_buffer_get_text(buf, &start, &end, FALSE);
19d37cb9c96c added textarea
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
166 text->value = str;
19d37cb9c96c added textarea
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
167 return str;
19d37cb9c96c added textarea
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
168 }
19d37cb9c96c added textarea
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
169
19d37cb9c96c added textarea
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
170 void ui_textarea_set(UiText *text, char *str) {
19d37cb9c96c added textarea
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
171 if(text->value) {
19d37cb9c96c added textarea
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
172 g_free(text->value);
12
fe94e0fb9ef3 added some text functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 9
diff changeset
173 }
fe94e0fb9ef3 added some text functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 9
diff changeset
174 text->value = NULL;
fe94e0fb9ef3 added some text functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 9
diff changeset
175 gtk_text_buffer_set_text((GtkTextBuffer*)text->obj, str, -1);
fe94e0fb9ef3 added some text functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 9
diff changeset
176 }
fe94e0fb9ef3 added some text functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 9
diff changeset
177
fe94e0fb9ef3 added some text functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 9
diff changeset
178 char* ui_textarea_getsubstr(UiText *text, int begin, int end) {
fe94e0fb9ef3 added some text functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 9
diff changeset
179 if(text->value) {
fe94e0fb9ef3 added some text functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 9
diff changeset
180 g_free(text->value);
5
19d37cb9c96c added textarea
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
181 }
12
fe94e0fb9ef3 added some text functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 9
diff changeset
182 GtkTextBuffer *buf = text->obj;
fe94e0fb9ef3 added some text functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 9
diff changeset
183 GtkTextIter ib;
fe94e0fb9ef3 added some text functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 9
diff changeset
184 GtkTextIter ie;
fe94e0fb9ef3 added some text functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 9
diff changeset
185 gtk_text_buffer_get_iter_at_offset(text->obj, &ib, begin);
fe94e0fb9ef3 added some text functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 9
diff changeset
186 gtk_text_buffer_get_iter_at_offset(text->obj, &ie, end);
fe94e0fb9ef3 added some text functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 9
diff changeset
187 char *str = gtk_text_buffer_get_text(buf, &ib, &ie, FALSE);
fe94e0fb9ef3 added some text functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 9
diff changeset
188 text->value = str;
fe94e0fb9ef3 added some text functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 9
diff changeset
189 return str;
fe94e0fb9ef3 added some text functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 9
diff changeset
190 }
fe94e0fb9ef3 added some text functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 9
diff changeset
191
fe94e0fb9ef3 added some text functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 9
diff changeset
192 void ui_textarea_insert(UiText *text, int pos, char *str) {
fe94e0fb9ef3 added some text functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 9
diff changeset
193 if(text->value) {
fe94e0fb9ef3 added some text functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 9
diff changeset
194 g_free(text->value);
fe94e0fb9ef3 added some text functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 9
diff changeset
195 }
fe94e0fb9ef3 added some text functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 9
diff changeset
196 text->value = NULL;
fe94e0fb9ef3 added some text functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 9
diff changeset
197 GtkTextIter offset;
fe94e0fb9ef3 added some text functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 9
diff changeset
198 gtk_text_buffer_get_iter_at_offset(text->obj, &offset, pos);
fe94e0fb9ef3 added some text functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 9
diff changeset
199 gtk_text_buffer_insert(text->obj, &offset, str, -1);
5
19d37cb9c96c added textarea
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
200 }
19d37cb9c96c added textarea
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
201
90
2019fdbaadfd persistent textarea cursor position
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 65
diff changeset
202 void ui_textarea_setposition(UiText *text, int pos) {
2019fdbaadfd persistent textarea cursor position
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 65
diff changeset
203 GtkTextIter iter;
2019fdbaadfd persistent textarea cursor position
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 65
diff changeset
204 gtk_text_buffer_get_iter_at_offset(text->obj, &iter, pos);
2019fdbaadfd persistent textarea cursor position
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 65
diff changeset
205 gtk_text_buffer_place_cursor(text->obj, &iter);
2019fdbaadfd persistent textarea cursor position
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 65
diff changeset
206 }
2019fdbaadfd persistent textarea cursor position
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 65
diff changeset
207
27
77b09bb52ca0 added groups for toolbar items and copy & paste (GTK, Motif)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 22
diff changeset
208 int ui_textarea_position(UiText *text) {
77b09bb52ca0 added groups for toolbar items and copy & paste (GTK, Motif)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 22
diff changeset
209 GtkTextIter begin;
77b09bb52ca0 added groups for toolbar items and copy & paste (GTK, Motif)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 22
diff changeset
210 GtkTextIter end;
77b09bb52ca0 added groups for toolbar items and copy & paste (GTK, Motif)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 22
diff changeset
211 gtk_text_buffer_get_selection_bounds(text->obj, &begin, &end);
90
2019fdbaadfd persistent textarea cursor position
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 65
diff changeset
212 text->pos = gtk_text_iter_get_offset(&begin);
2019fdbaadfd persistent textarea cursor position
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 65
diff changeset
213 return text->pos;
27
77b09bb52ca0 added groups for toolbar items and copy & paste (GTK, Motif)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 22
diff changeset
214 }
77b09bb52ca0 added groups for toolbar items and copy & paste (GTK, Motif)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 22
diff changeset
215
77b09bb52ca0 added groups for toolbar items and copy & paste (GTK, Motif)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 22
diff changeset
216 void ui_textarea_selection(UiText *text, int *begin, int *end) {
77b09bb52ca0 added groups for toolbar items and copy & paste (GTK, Motif)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 22
diff changeset
217 GtkTextIter b;
77b09bb52ca0 added groups for toolbar items and copy & paste (GTK, Motif)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 22
diff changeset
218 GtkTextIter e;
77b09bb52ca0 added groups for toolbar items and copy & paste (GTK, Motif)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 22
diff changeset
219 gtk_text_buffer_get_selection_bounds(text->obj, &b, &e);
77b09bb52ca0 added groups for toolbar items and copy & paste (GTK, Motif)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 22
diff changeset
220 *begin = gtk_text_iter_get_offset(&b);
77b09bb52ca0 added groups for toolbar items and copy & paste (GTK, Motif)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 22
diff changeset
221 *end = gtk_text_iter_get_offset(&e);
77b09bb52ca0 added groups for toolbar items and copy & paste (GTK, Motif)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 22
diff changeset
222 }
77b09bb52ca0 added groups for toolbar items and copy & paste (GTK, Motif)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 22
diff changeset
223
29
c96169444d88 added locale support (Cocoa) and ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 27
diff changeset
224 int ui_textarea_length(UiText *text) {
c96169444d88 added locale support (Cocoa) and ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 27
diff changeset
225 GtkTextBuffer *buf = text->obj;
c96169444d88 added locale support (Cocoa) and ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 27
diff changeset
226 GtkTextIter start;
c96169444d88 added locale support (Cocoa) and ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 27
diff changeset
227 GtkTextIter end;
c96169444d88 added locale support (Cocoa) and ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 27
diff changeset
228 gtk_text_buffer_get_bounds(buf, &start, &end);
c96169444d88 added locale support (Cocoa) and ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 27
diff changeset
229 return gtk_text_iter_get_offset(&end);
c96169444d88 added locale support (Cocoa) and ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 27
diff changeset
230 }
c96169444d88 added locale support (Cocoa) and ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 27
diff changeset
231
33
458831c574f4 added listview, sidebar and toolbar image button (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 29
diff changeset
232 void ui_textarea_remove(UiText *text, int begin, int end) {
458831c574f4 added listview, sidebar and toolbar image button (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 29
diff changeset
233 GtkTextBuffer *buf = text->obj;
458831c574f4 added listview, sidebar and toolbar image button (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 29
diff changeset
234 GtkTextIter ib;
458831c574f4 added listview, sidebar and toolbar image button (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 29
diff changeset
235 GtkTextIter ie;
458831c574f4 added listview, sidebar and toolbar image button (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 29
diff changeset
236 gtk_text_buffer_get_iter_at_offset(buf, &ib, begin);
458831c574f4 added listview, sidebar and toolbar image button (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 29
diff changeset
237 gtk_text_buffer_get_iter_at_offset(buf, &ie, end);
458831c574f4 added listview, sidebar and toolbar image button (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 29
diff changeset
238 gtk_text_buffer_delete(buf, &ib, &ie);
458831c574f4 added listview, sidebar and toolbar image button (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 29
diff changeset
239 }
458831c574f4 added listview, sidebar and toolbar image button (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 29
diff changeset
240
5
19d37cb9c96c added textarea
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
241 void ui_textarea_realize_event(GtkWidget *widget, gpointer data) {
19d37cb9c96c added textarea
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
242 gtk_widget_grab_focus(widget);
19d37cb9c96c added textarea
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
243 }
19d37cb9c96c added textarea
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
244
6
05a18c56d9ca added undo for text
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
245
05a18c56d9ca added undo for text
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
246 // undo manager functions
05a18c56d9ca added undo for text
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
247
5
19d37cb9c96c added textarea
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
248 void ui_textbuf_insert(
19d37cb9c96c added textarea
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
249 GtkTextBuffer *textbuffer,
19d37cb9c96c added textarea
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
250 GtkTextIter *location,
19d37cb9c96c added textarea
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
251 char *text,
6
05a18c56d9ca added undo for text
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
252 int length,
5
19d37cb9c96c added textarea
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
253 void *data)
19d37cb9c96c added textarea
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
254 {
6
05a18c56d9ca added undo for text
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
255 UiText *value = data;
05a18c56d9ca added undo for text
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
256 UiUndoMgr *mgr = value->undomgr;
05a18c56d9ca added undo for text
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
257 if(!mgr->event) {
05a18c56d9ca added undo for text
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
258 return;
05a18c56d9ca added undo for text
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
259 }
05a18c56d9ca added undo for text
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
260
05a18c56d9ca added undo for text
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
261 if(mgr->cur) {
05a18c56d9ca added undo for text
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
262 UcxList *elm = mgr->cur->next;
9
e70e855cea89 added undo and redo for motif
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 8
diff changeset
263 if(elm) {
e70e855cea89 added undo and redo for motif
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 8
diff changeset
264 mgr->cur->next = NULL;
e70e855cea89 added undo and redo for motif
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 8
diff changeset
265 while(elm) {
e70e855cea89 added undo and redo for motif
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 8
diff changeset
266 elm->prev = NULL;
e70e855cea89 added undo and redo for motif
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 8
diff changeset
267 UcxList *next = elm->next;
e70e855cea89 added undo and redo for motif
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 8
diff changeset
268 ui_free_textbuf_op(elm->data);
e70e855cea89 added undo and redo for motif
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 8
diff changeset
269 free(elm);
e70e855cea89 added undo and redo for motif
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 8
diff changeset
270 elm = next;
e70e855cea89 added undo and redo for motif
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 8
diff changeset
271 }
6
05a18c56d9ca added undo for text
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
272 }
05a18c56d9ca added undo for text
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
273
05a18c56d9ca added undo for text
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
274 UiTextBufOp *last_op = mgr->cur->data;
9
e70e855cea89 added undo and redo for motif
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 8
diff changeset
275 if(
e70e855cea89 added undo and redo for motif
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 8
diff changeset
276 last_op->type == UI_TEXTBUF_INSERT &&
e70e855cea89 added undo and redo for motif
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 8
diff changeset
277 ui_check_insertstr(last_op->text, last_op->len, text, length) == 0)
e70e855cea89 added undo and redo for motif
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 8
diff changeset
278 {
6
05a18c56d9ca added undo for text
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
279 // append text to last op
05a18c56d9ca added undo for text
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
280 int ln = last_op->len;
05a18c56d9ca added undo for text
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
281 char *newtext = malloc(ln + length + 1);
05a18c56d9ca added undo for text
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
282 memcpy(newtext, last_op->text, ln);
05a18c56d9ca added undo for text
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
283 memcpy(newtext+ln, text, length);
05a18c56d9ca added undo for text
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
284 newtext[ln+length] = '\0';
05a18c56d9ca added undo for text
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
285
05a18c56d9ca added undo for text
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
286 last_op->text = newtext;
05a18c56d9ca added undo for text
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
287 last_op->len = ln + length;
05a18c56d9ca added undo for text
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
288 last_op->end += length;
05a18c56d9ca added undo for text
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
289
05a18c56d9ca added undo for text
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
290 return;
05a18c56d9ca added undo for text
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
291 }
05a18c56d9ca added undo for text
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
292 }
05a18c56d9ca added undo for text
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
293
05a18c56d9ca added undo for text
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
294 char *dpstr = malloc(length + 1);
05a18c56d9ca added undo for text
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
295 memcpy(dpstr, text, length);
05a18c56d9ca added undo for text
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
296 dpstr[length] = 0;
05a18c56d9ca added undo for text
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
297
05a18c56d9ca added undo for text
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
298 UiTextBufOp *op = malloc(sizeof(UiTextBufOp));
05a18c56d9ca added undo for text
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
299 op->type = UI_TEXTBUF_INSERT;
05a18c56d9ca added undo for text
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
300 op->start = gtk_text_iter_get_offset(location);
05a18c56d9ca added undo for text
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
301 op->end = op->start+length;
05a18c56d9ca added undo for text
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
302 op->len = length;
05a18c56d9ca added undo for text
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
303 op->text = dpstr;
05a18c56d9ca added undo for text
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
304
05a18c56d9ca added undo for text
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
305 UcxList *elm = ucx_list_append(NULL, op);
05a18c56d9ca added undo for text
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
306 mgr->cur = elm;
05a18c56d9ca added undo for text
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
307 mgr->begin = ucx_list_concat(mgr->begin, elm);
5
19d37cb9c96c added textarea
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
308 }
19d37cb9c96c added textarea
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
309
19d37cb9c96c added textarea
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
310 void ui_textbuf_delete(
19d37cb9c96c added textarea
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
311 GtkTextBuffer *textbuffer,
19d37cb9c96c added textarea
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
312 GtkTextIter *start,
19d37cb9c96c added textarea
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
313 GtkTextIter *end,
19d37cb9c96c added textarea
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
314 void *data)
19d37cb9c96c added textarea
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
315 {
6
05a18c56d9ca added undo for text
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
316 UiText *value = data;
05a18c56d9ca added undo for text
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
317 UiUndoMgr *mgr = value->undomgr;
05a18c56d9ca added undo for text
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
318 if(!mgr->event) {
05a18c56d9ca added undo for text
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
319 return;
05a18c56d9ca added undo for text
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
320 }
05a18c56d9ca added undo for text
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
321
05a18c56d9ca added undo for text
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
322 if(mgr->cur) {
05a18c56d9ca added undo for text
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
323 UcxList *elm = mgr->cur->next;
9
e70e855cea89 added undo and redo for motif
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 8
diff changeset
324 if(elm) {
e70e855cea89 added undo and redo for motif
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 8
diff changeset
325 mgr->cur->next = NULL;
e70e855cea89 added undo and redo for motif
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 8
diff changeset
326 while(elm) {
e70e855cea89 added undo and redo for motif
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 8
diff changeset
327 elm->prev = NULL;
e70e855cea89 added undo and redo for motif
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 8
diff changeset
328 UcxList *next = elm->next;
e70e855cea89 added undo and redo for motif
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 8
diff changeset
329 ui_free_textbuf_op(elm->data);
e70e855cea89 added undo and redo for motif
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 8
diff changeset
330 free(elm);
e70e855cea89 added undo and redo for motif
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 8
diff changeset
331 elm = next;
e70e855cea89 added undo and redo for motif
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 8
diff changeset
332 }
6
05a18c56d9ca added undo for text
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
333 }
05a18c56d9ca added undo for text
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
334 }
05a18c56d9ca added undo for text
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
335
05a18c56d9ca added undo for text
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
336 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
337
05a18c56d9ca added undo for text
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
338 UiTextBufOp *op = malloc(sizeof(UiTextBufOp));
05a18c56d9ca added undo for text
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
339 op->type = UI_TEXTBUF_DELETE;
05a18c56d9ca added undo for text
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
340 op->start = gtk_text_iter_get_offset(start);
05a18c56d9ca added undo for text
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
341 op->end = gtk_text_iter_get_offset(end);
05a18c56d9ca added undo for text
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
342 op->len = op->end - op->start;
05a18c56d9ca added undo for text
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
343
05a18c56d9ca added undo for text
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
344 char *dpstr = malloc(op->len + 1);
05a18c56d9ca added undo for text
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
345 memcpy(dpstr, text, op->len);
05a18c56d9ca added undo for text
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
346 dpstr[op->len] = 0;
05a18c56d9ca added undo for text
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
347 op->text = dpstr;
05a18c56d9ca added undo for text
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
348
05a18c56d9ca added undo for text
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
349 UcxList *elm = ucx_list_append(NULL, op);
05a18c56d9ca added undo for text
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
350 mgr->cur = elm;
05a18c56d9ca added undo for text
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
351 mgr->begin = ucx_list_concat(mgr->begin, elm);
05a18c56d9ca added undo for text
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
352 }
05a18c56d9ca added undo for text
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
353
05a18c56d9ca added undo for text
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
354 UiUndoMgr* ui_create_undomgr() {
05a18c56d9ca added undo for text
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
355 UiUndoMgr *mgr = malloc(sizeof(UiUndoMgr));
05a18c56d9ca added undo for text
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
356 mgr->begin = NULL;
05a18c56d9ca added undo for text
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
357 mgr->cur = NULL;
05a18c56d9ca added undo for text
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
358 mgr->length = 0;
05a18c56d9ca added undo for text
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
359 mgr->event = 1;
05a18c56d9ca added undo for text
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
360 return mgr;
05a18c56d9ca added undo for text
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
361 }
05a18c56d9ca added undo for text
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
362
05a18c56d9ca added undo for text
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
363 void ui_free_textbuf_op(UiTextBufOp *op) {
05a18c56d9ca added undo for text
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
364 if(op->text) {
9
e70e855cea89 added undo and redo for motif
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 8
diff changeset
365 free(op->text);
6
05a18c56d9ca added undo for text
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
366 }
05a18c56d9ca added undo for text
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
367 free(op);
5
19d37cb9c96c added textarea
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
368 }
6
05a18c56d9ca added undo for text
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
369
05a18c56d9ca added undo for text
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
370 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
371 // return 1 if oldstr + newstr are one word
05a18c56d9ca added undo for text
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
372
05a18c56d9ca added undo for text
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
373 int has_space = 0;
05a18c56d9ca added undo for text
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
374 for(int i=0;i<oldlen;i++) {
05a18c56d9ca added undo for text
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
375 if(oldstr[i] < 33) {
05a18c56d9ca added undo for text
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
376 has_space = 1;
05a18c56d9ca added undo for text
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
377 break;
05a18c56d9ca added undo for text
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
378 }
05a18c56d9ca added undo for text
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
379 }
05a18c56d9ca added undo for text
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
380
05a18c56d9ca added undo for text
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
381 for(int i=0;i<newlen;i++) {
05a18c56d9ca added undo for text
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
382 if(has_space && newstr[i] > 32) {
05a18c56d9ca added undo for text
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
383 return 1;
05a18c56d9ca added undo for text
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
384 }
05a18c56d9ca added undo for text
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
385 }
05a18c56d9ca added undo for text
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
386
05a18c56d9ca added undo for text
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
387 return 0;
05a18c56d9ca added undo for text
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
388 }
05a18c56d9ca added undo for text
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
389
05a18c56d9ca added undo for text
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
390 void ui_text_undo(UiText *value) {
05a18c56d9ca added undo for text
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
391 UiUndoMgr *mgr = value->undomgr;
05a18c56d9ca added undo for text
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
392
05a18c56d9ca added undo for text
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
393 if(mgr->cur) {
05a18c56d9ca added undo for text
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
394 UiTextBufOp *op = mgr->cur->data;
05a18c56d9ca added undo for text
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
395 mgr->event = 0;
05a18c56d9ca added undo for text
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
396 switch(op->type) {
05a18c56d9ca added undo for text
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
397 case UI_TEXTBUF_INSERT: {
05a18c56d9ca added undo for text
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
398 GtkTextIter begin;
05a18c56d9ca added undo for text
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
399 GtkTextIter end;
05a18c56d9ca added undo for text
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
400 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
401 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
402 gtk_text_buffer_delete(value->obj, &begin, &end);
05a18c56d9ca added undo for text
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
403 break;
05a18c56d9ca added undo for text
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
404 }
05a18c56d9ca added undo for text
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
405 case UI_TEXTBUF_DELETE: {
05a18c56d9ca added undo for text
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
406 GtkTextIter begin;
05a18c56d9ca added undo for text
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
407 GtkTextIter end;
05a18c56d9ca added undo for text
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
408 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
409 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
410 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
411 break;
05a18c56d9ca added undo for text
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
412 }
05a18c56d9ca added undo for text
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
413 }
05a18c56d9ca added undo for text
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
414 mgr->event = 1;
05a18c56d9ca added undo for text
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
415 mgr->cur = mgr->cur->prev;
05a18c56d9ca added undo for text
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
416 }
05a18c56d9ca added undo for text
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
417 }
05a18c56d9ca added undo for text
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
418
05a18c56d9ca added undo for text
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
419 void ui_text_redo(UiText *value) {
8
84a541c6e093 added redo
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 6
diff changeset
420 UiUndoMgr *mgr = value->undomgr;
6
05a18c56d9ca added undo for text
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
421
8
84a541c6e093 added redo
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 6
diff changeset
422 UcxList *elm = NULL;
84a541c6e093 added redo
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 6
diff changeset
423 if(mgr->cur) {
84a541c6e093 added redo
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 6
diff changeset
424 if(mgr->cur->next) {
84a541c6e093 added redo
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 6
diff changeset
425 elm = mgr->cur->next;
84a541c6e093 added redo
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 6
diff changeset
426 }
84a541c6e093 added redo
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 6
diff changeset
427 } else if(mgr->begin) {
84a541c6e093 added redo
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 6
diff changeset
428 elm = mgr->begin;
84a541c6e093 added redo
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 6
diff changeset
429 }
84a541c6e093 added redo
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 6
diff changeset
430
84a541c6e093 added redo
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 6
diff changeset
431 if(elm) {
84a541c6e093 added redo
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 6
diff changeset
432 UiTextBufOp *op = elm->data;
84a541c6e093 added redo
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 6
diff changeset
433 mgr->event = 0;
84a541c6e093 added redo
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 6
diff changeset
434 switch(op->type) {
84a541c6e093 added redo
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 6
diff changeset
435 case UI_TEXTBUF_INSERT: {
84a541c6e093 added redo
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 6
diff changeset
436 GtkTextIter begin;
84a541c6e093 added redo
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 6
diff changeset
437 GtkTextIter end;
84a541c6e093 added redo
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 6
diff changeset
438 gtk_text_buffer_get_iter_at_offset(value->obj, &begin, op->start);
84a541c6e093 added redo
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 6
diff changeset
439 gtk_text_buffer_get_iter_at_offset(value->obj, &end, op->end);
84a541c6e093 added redo
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 6
diff changeset
440 gtk_text_buffer_insert(value->obj, &begin, op->text, op->len);
84a541c6e093 added redo
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 6
diff changeset
441 break;
84a541c6e093 added redo
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 6
diff changeset
442 }
84a541c6e093 added redo
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 6
diff changeset
443 case UI_TEXTBUF_DELETE: {
84a541c6e093 added redo
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 6
diff changeset
444 GtkTextIter begin;
84a541c6e093 added redo
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 6
diff changeset
445 GtkTextIter end;
84a541c6e093 added redo
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 6
diff changeset
446 gtk_text_buffer_get_iter_at_offset(value->obj, &begin, op->start);
84a541c6e093 added redo
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 6
diff changeset
447 gtk_text_buffer_get_iter_at_offset(value->obj, &end, op->end);
84a541c6e093 added redo
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 6
diff changeset
448 gtk_text_buffer_delete(value->obj, &begin, &end);
84a541c6e093 added redo
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 6
diff changeset
449 break;
84a541c6e093 added redo
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 6
diff changeset
450 }
84a541c6e093 added redo
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 6
diff changeset
451 }
84a541c6e093 added redo
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 6
diff changeset
452 mgr->event = 1;
84a541c6e093 added redo
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 6
diff changeset
453 mgr->cur = elm;
84a541c6e093 added redo
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 6
diff changeset
454 }
6
05a18c56d9ca added undo for text
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
455 }
65
4697592e24ba added label, grid container, simple tabview and textfield (Gtk)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 59
diff changeset
456
4697592e24ba added label, grid container, simple tabview and textfield (Gtk)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 59
diff changeset
457
121
1cc365c34125 added some special textfields (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 94
diff changeset
458 static UIWIDGET create_textfield(UiObject *obj, int width, UiBool frameless, UiBool password, UiString *value) {
65
4697592e24ba added label, grid container, simple tabview and textfield (Gtk)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 59
diff changeset
459 GtkWidget *textfield = gtk_entry_new();
94
d51e334c1439 added checkbox and labels with alignment (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 90
diff changeset
460 if(width > 0) {
d51e334c1439 added checkbox and labels with alignment (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 90
diff changeset
461 gtk_entry_set_width_chars(GTK_ENTRY(textfield), width);
d51e334c1439 added checkbox and labels with alignment (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 90
diff changeset
462 }
121
1cc365c34125 added some special textfields (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 94
diff changeset
463 if(frameless) {
1cc365c34125 added some special textfields (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 94
diff changeset
464 // TODO: gtk2legacy workaroud
1cc365c34125 added some special textfields (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 94
diff changeset
465 gtk_entry_set_has_frame(GTK_ENTRY(textfield), FALSE);
1cc365c34125 added some special textfields (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 94
diff changeset
466 }
1cc365c34125 added some special textfields (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 94
diff changeset
467 if(password) {
1cc365c34125 added some special textfields (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 94
diff changeset
468 gtk_entry_set_visibility(GTK_ENTRY(textfield), FALSE);
1cc365c34125 added some special textfields (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 94
diff changeset
469 }
94
d51e334c1439 added checkbox and labels with alignment (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 90
diff changeset
470
65
4697592e24ba added label, grid container, simple tabview and textfield (Gtk)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 59
diff changeset
471 UiContainer *ct = uic_get_current_container(obj);
4697592e24ba added label, grid container, simple tabview and textfield (Gtk)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 59
diff changeset
472 ct->add(ct, textfield, FALSE);
4697592e24ba added label, grid container, simple tabview and textfield (Gtk)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 59
diff changeset
473
4697592e24ba added label, grid container, simple tabview and textfield (Gtk)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 59
diff changeset
474 if(value) {
4697592e24ba added label, grid container, simple tabview and textfield (Gtk)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 59
diff changeset
475 if(value->value) {
4697592e24ba added label, grid container, simple tabview and textfield (Gtk)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 59
diff changeset
476 gtk_entry_set_text(GTK_ENTRY(textfield), value->value);
4697592e24ba added label, grid container, simple tabview and textfield (Gtk)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 59
diff changeset
477 g_free(value->value);
4697592e24ba added label, grid container, simple tabview and textfield (Gtk)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 59
diff changeset
478 // TODO: free value
4697592e24ba added label, grid container, simple tabview and textfield (Gtk)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 59
diff changeset
479 }
4697592e24ba added label, grid container, simple tabview and textfield (Gtk)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 59
diff changeset
480
4697592e24ba added label, grid container, simple tabview and textfield (Gtk)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 59
diff changeset
481 value->get = ui_textfield_get;
4697592e24ba added label, grid container, simple tabview and textfield (Gtk)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 59
diff changeset
482 value->set = ui_textfield_set;
4697592e24ba added label, grid container, simple tabview and textfield (Gtk)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 59
diff changeset
483 value->value = NULL;
4697592e24ba added label, grid container, simple tabview and textfield (Gtk)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 59
diff changeset
484 value->obj = GTK_ENTRY(textfield);
4697592e24ba added label, grid container, simple tabview and textfield (Gtk)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 59
diff changeset
485 }
4697592e24ba added label, grid container, simple tabview and textfield (Gtk)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 59
diff changeset
486
4697592e24ba added label, grid container, simple tabview and textfield (Gtk)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 59
diff changeset
487 return textfield;
4697592e24ba added label, grid container, simple tabview and textfield (Gtk)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 59
diff changeset
488 }
4697592e24ba added label, grid container, simple tabview and textfield (Gtk)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 59
diff changeset
489
121
1cc365c34125 added some special textfields (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 94
diff changeset
490 static UIWIDGET create_textfield_nv(UiObject *obj, int width, UiBool frameless, UiBool password, char *varname) {
65
4697592e24ba added label, grid container, simple tabview and textfield (Gtk)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 59
diff changeset
491 UiVar *var = uic_connect_var(obj->ctx, varname, UI_VAR_STRING);
4697592e24ba added label, grid container, simple tabview and textfield (Gtk)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 59
diff changeset
492 if(var) {
4697592e24ba added label, grid container, simple tabview and textfield (Gtk)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 59
diff changeset
493 UiString *value = var->value;
121
1cc365c34125 added some special textfields (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 94
diff changeset
494 return create_textfield(obj, width, frameless, password, value);
65
4697592e24ba added label, grid container, simple tabview and textfield (Gtk)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 59
diff changeset
495 } else {
4697592e24ba added label, grid container, simple tabview and textfield (Gtk)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 59
diff changeset
496 // TODO: error
4697592e24ba added label, grid container, simple tabview and textfield (Gtk)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 59
diff changeset
497 }
4697592e24ba added label, grid container, simple tabview and textfield (Gtk)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 59
diff changeset
498 return NULL;
4697592e24ba added label, grid container, simple tabview and textfield (Gtk)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 59
diff changeset
499 }
4697592e24ba added label, grid container, simple tabview and textfield (Gtk)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 59
diff changeset
500
121
1cc365c34125 added some special textfields (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 94
diff changeset
501 UIWIDGET ui_textfield(UiObject *obj, UiString *value) {
1cc365c34125 added some special textfields (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 94
diff changeset
502 return create_textfield(obj, 0, FALSE, FALSE, value);
1cc365c34125 added some special textfields (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 94
diff changeset
503 }
1cc365c34125 added some special textfields (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 94
diff changeset
504
122
e82b01d17a78 added some special textfields (Motif)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 121
diff changeset
505 UIWIDGET ui_textfield_nv(UiObject *obj, char *varname) {
e82b01d17a78 added some special textfields (Motif)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 121
diff changeset
506 return create_textfield_nv(obj, 0, FALSE, FALSE, varname);
e82b01d17a78 added some special textfields (Motif)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 121
diff changeset
507 }
e82b01d17a78 added some special textfields (Motif)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 121
diff changeset
508
121
1cc365c34125 added some special textfields (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 94
diff changeset
509 UIWIDGET ui_textfield_w(UiObject *obj, int width, UiString *value) {
1cc365c34125 added some special textfields (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 94
diff changeset
510 return create_textfield(obj, width, FALSE, FALSE, value);
1cc365c34125 added some special textfields (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 94
diff changeset
511 }
1cc365c34125 added some special textfields (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 94
diff changeset
512
122
e82b01d17a78 added some special textfields (Motif)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 121
diff changeset
513 UIWIDGET ui_textfield_wnv(UiObject *obj, int width, char *varname) {
e82b01d17a78 added some special textfields (Motif)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 121
diff changeset
514 return create_textfield_nv(obj, width, FALSE, FALSE, varname);
e82b01d17a78 added some special textfields (Motif)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 121
diff changeset
515 }
e82b01d17a78 added some special textfields (Motif)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 121
diff changeset
516
121
1cc365c34125 added some special textfields (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 94
diff changeset
517 UIWIDGET ui_frameless_textfield(UiObject *obj, UiString *value) {
1cc365c34125 added some special textfields (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 94
diff changeset
518 return create_textfield(obj, 0, TRUE, FALSE, value);
1cc365c34125 added some special textfields (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 94
diff changeset
519 }
1cc365c34125 added some special textfields (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 94
diff changeset
520
122
e82b01d17a78 added some special textfields (Motif)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 121
diff changeset
521 UIWIDGET ui_frameless_textfield_nv(UiObject *obj, char *varname) {
e82b01d17a78 added some special textfields (Motif)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 121
diff changeset
522 return create_textfield_nv(obj, 0, TRUE, FALSE, varname);
e82b01d17a78 added some special textfields (Motif)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 121
diff changeset
523 }
e82b01d17a78 added some special textfields (Motif)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 121
diff changeset
524
121
1cc365c34125 added some special textfields (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 94
diff changeset
525 UIWIDGET ui_passwordfield(UiObject *obj, UiString *value) {
1cc365c34125 added some special textfields (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 94
diff changeset
526 return create_textfield(obj, 0, FALSE, TRUE, value);
1cc365c34125 added some special textfields (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 94
diff changeset
527 }
1cc365c34125 added some special textfields (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 94
diff changeset
528
122
e82b01d17a78 added some special textfields (Motif)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 121
diff changeset
529 UIWIDGET ui_passwordfield_nv(UiObject *obj, char *varname) {
e82b01d17a78 added some special textfields (Motif)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 121
diff changeset
530 return create_textfield_nv(obj, 0, FALSE, TRUE, varname);
121
1cc365c34125 added some special textfields (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 94
diff changeset
531 }
1cc365c34125 added some special textfields (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 94
diff changeset
532
122
e82b01d17a78 added some special textfields (Motif)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 121
diff changeset
533 UIWIDGET ui_passwordfield_w(UiObject *obj, int width, UiString *value) {
e82b01d17a78 added some special textfields (Motif)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 121
diff changeset
534 return create_textfield(obj, width, FALSE, TRUE, value);
121
1cc365c34125 added some special textfields (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 94
diff changeset
535 }
1cc365c34125 added some special textfields (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 94
diff changeset
536
1cc365c34125 added some special textfields (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 94
diff changeset
537 UIWIDGET ui_passwordfield_wnv(UiObject *obj, int width, char *varname) {
1cc365c34125 added some special textfields (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 94
diff changeset
538 return create_textfield_nv(obj, width, FALSE, TRUE, varname);
1cc365c34125 added some special textfields (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 94
diff changeset
539 }
1cc365c34125 added some special textfields (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 94
diff changeset
540
65
4697592e24ba added label, grid container, simple tabview and textfield (Gtk)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 59
diff changeset
541 char* ui_textfield_get(UiString *str) {
4697592e24ba added label, grid container, simple tabview and textfield (Gtk)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 59
diff changeset
542 if(str->value) {
4697592e24ba added label, grid container, simple tabview and textfield (Gtk)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 59
diff changeset
543 g_free(str->value);
4697592e24ba added label, grid container, simple tabview and textfield (Gtk)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 59
diff changeset
544 }
4697592e24ba added label, grid container, simple tabview and textfield (Gtk)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 59
diff changeset
545 str->value = g_strdup(gtk_entry_get_text(str->obj));
4697592e24ba added label, grid container, simple tabview and textfield (Gtk)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 59
diff changeset
546 return str->value;
4697592e24ba added label, grid container, simple tabview and textfield (Gtk)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 59
diff changeset
547 }
4697592e24ba added label, grid container, simple tabview and textfield (Gtk)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 59
diff changeset
548
4697592e24ba added label, grid container, simple tabview and textfield (Gtk)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 59
diff changeset
549 void ui_textfield_set(UiString *str, char *value) {
4697592e24ba added label, grid container, simple tabview and textfield (Gtk)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 59
diff changeset
550 if(str->value) {
4697592e24ba added label, grid container, simple tabview and textfield (Gtk)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 59
diff changeset
551 g_free(str->value);
4697592e24ba added label, grid container, simple tabview and textfield (Gtk)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 59
diff changeset
552 }
4697592e24ba added label, grid container, simple tabview and textfield (Gtk)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 59
diff changeset
553 str->value = NULL;
4697592e24ba added label, grid container, simple tabview and textfield (Gtk)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 59
diff changeset
554 gtk_entry_set_text(str->obj, value);
4697592e24ba added label, grid container, simple tabview and textfield (Gtk)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 59
diff changeset
555 }

mercurial