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
|
/* testrichtext.c
* Copyright (C) 2006 Imendio AB
* Authors: Michael Natterer, Tim Janik
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library 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
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the
* Free Software Foundation, Inc., 59 Temple Place - Suite 330,
* Boston, MA 02111-1307, USA.
*/
#include <string.h>
#include <gtk/gtk.h>
static guint32 quick_rand32_accu = 2147483563;
static inline guint32
quick_rand32 (void)
{
quick_rand32_accu = 1664525 * quick_rand32_accu + 1013904223;
return quick_rand32_accu;
}
static gboolean
delete_event (GtkWidget *widget,
GdkEventAny *event,
gpointer user_data)
{
gtk_main_quit ();
return TRUE;
}
static void
text_tag_enqueue (GtkTextTag *tag,
gpointer data)
{
GSList **slist_p = data;
*slist_p = g_slist_prepend (*slist_p, tag);
}
static const gchar *example_text =
"vkndsk vfds vkfds vkdsv fdlksnvkfdvnkfdvnkdsnvs\n"
"kmvofdmvfdsvkv fdskvnkfdv nnd.mckfdvnknsknvdnvs"
"fdlvmfdsvlkfdsmvnskdnvfdsnvf sbskjnvlknfd cvdvnd"
"mvlfdsv vfdkjv m, ds vkfdks v df,v j kfds v d\n"
"vnfdskv kjvnfv cfdkvndfnvcm fd,vk kdsf vj d\n"
"KLJHkjh kjh klhjKLJH Kjh kjl h34kj h34kj3h klj 23 "
"kjlkjlhsdjk 34kljh klj hklj 23k4jkjkjh234kjh 52kj "
"2h34 sdaf ukklj kjl32l jkkjl 23j jkl ljk23 jkl\n"
"hjhjhj2hj23jh jh jk jk2h3 hj kjj jk jh21 jhhj32.";
static GdkAtom
setup_buffer (GtkTextBuffer *buffer)
{
const guint tlen = strlen (example_text);
const guint tcount = 17;
GtkTextTag *tags[tcount];
GtkTextTagTable *ttable = gtk_text_buffer_get_tag_table (buffer);
GSList *node, *slist = NULL;
GdkAtom atom;
guint i;
/* cleanup */
gtk_text_buffer_set_text (buffer, "", 0);
gtk_text_tag_table_foreach (ttable, text_tag_enqueue, &slist);
for (node = slist; node; node = node->next)
gtk_text_tag_table_remove (ttable, node->data);
g_slist_free (slist);
/* create new tags */
for (i = 0; i < tcount; i++)
{
char *s = g_strdup_printf ("tag%u", i);
tags[i] = gtk_text_buffer_create_tag (buffer, s,
"weight", quick_rand32() >> 31 ? PANGO_WEIGHT_BOLD : PANGO_WEIGHT_NORMAL,
"style", quick_rand32() >> 31 ? PANGO_STYLE_OBLIQUE : PANGO_STYLE_NORMAL,
"underline", quick_rand32() >> 31,
NULL);
g_free (s);
}
/* assign text and tags */
gtk_text_buffer_set_text (buffer, example_text, -1);
for (i = 0; i < tcount * 5; i++)
{
gint a = quick_rand32() % tlen, b = quick_rand32() % tlen;
GtkTextIter start, end;
gtk_text_buffer_get_iter_at_offset (buffer, &start, MIN (a, b));
gtk_text_buffer_get_iter_at_offset (buffer, &end, MAX (a, b));
gtk_text_buffer_apply_tag (buffer, tags[i % tcount], &start, &end);
}
/* return serialization format */
atom = gtk_text_buffer_register_deserialize_tagset (buffer, NULL);
gtk_text_buffer_deserialize_set_can_create_tags (buffer, atom, TRUE);
return atom;
}
static gboolean
test_serialize_deserialize (GtkTextBuffer *buffer,
GdkAtom atom,
GError **error)
{
GtkTextIter start, end;
guint8 *spew;
gsize spew_length;
gboolean success;
gtk_text_buffer_get_bounds (buffer, &start, &end);
spew = gtk_text_buffer_serialize (buffer, buffer, atom,
&start, &end, &spew_length);
success = gtk_text_buffer_deserialize (buffer, buffer, atom, &end,
spew, spew_length, error);
g_free (spew);
return success;
}
gint
main (gint argc,
gchar *argv[])
{
GtkWidget *window;
GtkWidget *sw;
GtkWidget *view;
GtkTextBuffer *buffer;
GdkAtom atom;
guint i, broken = 0;
gtk_init (&argc, &argv);
window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
gtk_widget_set_size_request (window, 400, 300);
sw = gtk_scrolled_window_new (NULL, NULL);
gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (sw),
GTK_SHADOW_IN);
gtk_container_set_border_width (GTK_CONTAINER (sw), 12);
gtk_container_add (GTK_CONTAINER (window), sw);
g_signal_connect (window, "delete-event",
G_CALLBACK (delete_event),
NULL);
buffer = gtk_text_buffer_new (NULL);
view = gtk_text_view_new_with_buffer (buffer);
g_object_unref (buffer);
gtk_container_add (GTK_CONTAINER (sw), view);
gtk_widget_show_all (window);
if (0)
gtk_main ();
for (i = 0; i < 250; i++)
{
GError *error = NULL;
g_printerr ("creating randomly tagged text buffer with accu=0x%x...\n", quick_rand32_accu);
atom = setup_buffer (buffer);
if (test_serialize_deserialize (buffer, atom, &error))
g_printerr ("ok.\n");
else
{
g_printerr ("FAIL: serialization/deserialization failed:\n %s\n", error->message);
broken += 1;
}
g_clear_error (&error);
}
return broken > 0;
}
|