1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
|
/*
* Copyright (C) 2003 Red Hat, Inc.
*
* This is free software; you can redistribute it and/or modify it under
* the terms of the GNU Library General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU Library General Public
* License along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
#include "../config.h"
#include <sys/types.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <gtk/gtk.h>
#include <atk/atk.h>
#ifdef USE_VTE
#include "vte.h"
#endif
static GArray *contents = NULL;
#ifdef USE_TEXT_VIEW
/*
* Implementation for a TextView widget.
*/
static void
terminal_init_text_view(GtkWidget **widget)
{
*widget = gtk_text_view_new();
gtk_text_view_set_editable(GTK_TEXT_VIEW(*widget), TRUE);
}
static void
terminal_shell_text_view(GtkWidget *widget)
{
/* no-op */
}
static GtkAdjustment *
terminal_adjustment_text_view(GtkWidget *terminal)
{
return (GTK_TEXT_VIEW(terminal))->vadjustment;
}
#endif
#ifdef USE_VTE
/*
* Implementation for a VteTerminal widget.
*/
static void
terminal_init_vte(GtkWidget **terminal)
{
*terminal = vte_terminal_new();
g_signal_connect(G_OBJECT(*terminal), "eof",
G_CALLBACK(gtk_main_quit), NULL);
g_signal_connect(G_OBJECT(*terminal), "child-exited",
G_CALLBACK(gtk_main_quit), NULL);
}
static void
terminal_shell_vte(GtkWidget *terminal)
{
vte_terminal_fork_command(VTE_TERMINAL(terminal),
getenv("SHELL") ? getenv("SHELL") : "/bin/sh",
NULL,
NULL,
g_get_home_dir() ? g_get_home_dir() : NULL,
FALSE,
FALSE,
FALSE);
}
static GtkAdjustment *
terminal_adjustment_vte(GtkWidget *terminal)
{
return (VTE_TERMINAL(terminal))->adjustment;
}
#endif
/*
* Update the contents of the widget with the data from our contents array.
*/
static void
update_contents(AtkObject *obj, GtkWidget *widget)
{
int caret, i;
GString *s;
caret = atk_text_get_caret_offset(ATK_TEXT(obj));
s = g_string_new(NULL);
for (i = 0; i < contents->len; i++) {
if (i == caret) {
s = g_string_append(s, "[CARET]");
}
s = g_string_append_unichar(s,
g_array_index(contents,
gunichar,
i));
}
if (i == caret) {
s = g_string_append(s, "[CARET]");
}
if (GTK_IS_LABEL(widget)) {
gtk_label_set_text(GTK_LABEL(widget), s->str);
gtk_label_set_selectable(GTK_LABEL(widget),
atk_text_get_n_selections(ATK_TEXT(obj)) > 0);
if (gtk_label_get_selectable(GTK_LABEL(widget))) {
int selection_start, selection_end;
atk_text_get_selection(ATK_TEXT(obj), 0,
&selection_start,
&selection_end);
gtk_label_select_region(GTK_LABEL(widget),
selection_start, selection_end);
}
}
g_string_free(s, TRUE);
}
/* Handle inserted text by inserting the text into our gunichar array. */
static void
text_changed_insert(AtkObject *obj, gint offset, gint length, gpointer data)
{
char *inserted, *p;
gunichar c;
int i;
inserted = atk_text_get_text(ATK_TEXT(obj), offset, offset + length);
if (!g_utf8_validate(inserted, -1, NULL)) {
g_free(inserted);
g_error("UTF-8 validation error");
return;
}
p = inserted;
i = 0;
while (i < length) {
c = g_utf8_get_char(p);
if (offset + i >= contents->len) {
g_array_append_val(contents, c);
} else {
g_array_insert_val(contents, offset + i, c);
}
i++;
p = g_utf8_next_char(p);
}
#ifdef VTE_DEBUG
if ((getenv("REFLECT_VERBOSE") != NULL) &&
(atol(getenv("REFLECT_VERBOSE")) != 0)) {
fprintf(stderr, "Inserted %d chars ('%.*s') at %d,",
length, (int)(p - inserted), inserted, offset);
fprintf(stderr, " buffer contains %d characters.\n",
contents->len);
}
#endif
g_free(inserted);
update_contents(obj, GTK_WIDGET(data));
}
/* Handle deleted text by removing the text from our gunichar array. */
static void
text_changed_delete(AtkObject *obj, gint offset, gint length, gpointer data)
{
int i;
for (i = offset + length - 1; i >= offset; i--) {
if (i > contents->len - 1) {
g_warning("Invalid character %d was deleted.\n", i);
}
g_array_remove_index(contents, i);
}
#ifdef VTE_DEBUG
if ((getenv("REFLECT_VERBOSE") != NULL) &&
(atol(getenv("REFLECT_VERBOSE")) != 0)) {
fprintf(stderr, "Deleted %d chars at %d.\n", length, offset);
}
#endif
update_contents(obj, GTK_WIDGET(data));
}
static void
text_caret_moved(AtkObject *obj, gint offset, gpointer data)
{
update_contents(obj, GTK_WIDGET(data));
}
static void
text_selection_changed(AtkObject *obj, gpointer data)
{
update_contents(obj, GTK_WIDGET(data));
}
/* Wrapper versions. */
static void
terminal_init(GtkWidget **terminal)
{
*terminal = NULL;
#ifdef USE_TEXT_VIEW
terminal_init_text_view(terminal);
return;
#endif
#ifdef USE_VTE
terminal_init_vte(terminal);
return;
#endif
g_assert_not_reached();
}
static void
terminal_shell(GtkWidget *terminal)
{
#ifdef USE_TEXT_VIEW
terminal_shell_text_view(terminal);
return;
#endif
#ifdef USE_VTE
terminal_shell_vte(terminal);
return;
#endif
g_assert_not_reached();
}
static GtkAdjustment *
terminal_adjustment(GtkWidget *terminal)
{
#ifdef USE_TEXT_VIEW
return terminal_adjustment_text_view(terminal);
#endif
#ifdef USE_VTE
return terminal_adjustment_vte(terminal);
#endif
g_assert_not_reached();
}
int
main(int argc, char **argv)
{
GtkWidget *label, *terminal, *tophalf, *pane, *window, *scrollbar, *sw;
AtkObject *obj;
char *text, *p;
gunichar c;
gint count;
gtk_init(&argc, &argv);
contents = g_array_new(TRUE, FALSE, sizeof(gunichar));
terminal_init(&terminal);
#ifdef USE_TEXT_VIEW
tophalf = gtk_scrolled_window_new(NULL, terminal_adjustment(terminal));
gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(tophalf),
GTK_POLICY_AUTOMATIC,
GTK_POLICY_AUTOMATIC);
scrollbar = NULL;
gtk_container_add(GTK_CONTAINER(tophalf), terminal);
#else
tophalf = gtk_hbox_new(FALSE, 0);
gtk_box_pack_start(GTK_BOX(tophalf), terminal, TRUE, TRUE, 0);
gtk_widget_show(terminal);
scrollbar = gtk_vscrollbar_new(terminal_adjustment(terminal));
gtk_box_pack_start(GTK_BOX(tophalf), scrollbar, FALSE, TRUE, 0);
gtk_widget_show(scrollbar);
#endif
gtk_widget_show(terminal);
label = gtk_label_new("");
gtk_label_set_justify(GTK_LABEL(label), GTK_JUSTIFY_LEFT);
gtk_misc_set_alignment(GTK_MISC(label), 0, 0);
sw = gtk_scrolled_window_new(NULL, NULL);
gtk_scrolled_window_add_with_viewport(GTK_SCROLLED_WINDOW(sw), label);
gtk_widget_show(label);
pane = gtk_vpaned_new();
gtk_paned_pack1(GTK_PANED(pane), tophalf, TRUE, FALSE);
gtk_paned_pack2(GTK_PANED(pane), sw, TRUE, FALSE);
gtk_widget_show(tophalf);
gtk_widget_show(sw);
window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
g_signal_connect(G_OBJECT(window), "delete_event",
G_CALLBACK(gtk_main_quit), NULL);
gtk_container_add(GTK_CONTAINER(window), pane);
gtk_widget_show(pane);
obj = gtk_widget_get_accessible(terminal);
g_assert(obj != NULL);
g_signal_connect(G_OBJECT(obj), "text-changed::insert",
G_CALLBACK(text_changed_insert), label);
g_signal_connect(G_OBJECT(obj), "text-changed::delete",
G_CALLBACK(text_changed_delete), label);
g_signal_connect(G_OBJECT(obj), "text-caret-moved",
G_CALLBACK(text_caret_moved), label);
g_signal_connect(G_OBJECT(obj), "text-selection-changed",
G_CALLBACK(text_selection_changed), label);
count = atk_text_get_character_count(ATK_TEXT(obj));
if (count > 0) {
text = atk_text_get_text(ATK_TEXT(obj), 0, count);
if (text != NULL) {
for (p = text;
contents->len < count;
p = g_utf8_next_char(p)) {
c = g_utf8_get_char(p);
g_array_append_val(contents, c);
}
g_free(text);
}
}
terminal_shell(terminal);
gtk_window_set_default_size(GTK_WINDOW(window), 600, 450);
gtk_widget_show(window);
update_contents(obj, terminal);
gtk_main();
g_array_free(contents, TRUE);
contents = NULL;
return 0;
}
|