summaryrefslogtreecommitdiff
path: root/gtk/testtextbuffer.c
blob: dfd95bf0a4f06711ee3d4c9ce496af2aa018a761 (plain)
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
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
/* Simplistic test suite */

#include <stdio.h>

#include <gtk/gtk.h>
#include "gtktextbtree.h"

static void fill_buffer(GtkTextBuffer *buffer);

static void run_tests(GtkTextBuffer *buffer);

int
main(int argc, char** argv)
{
  GtkTextBuffer *buffer;
  int n;
  GtkTextUniChar ch;
  GtkTextIter start, end;
  
  gtk_init(&argc, &argv);

  /* Check UTF8 unknown char thing */
  g_assert(gtk_text_view_num_utf_chars(gtk_text_unknown_char_utf8, 3) == 1);
  gtk_text_utf_to_unichar(gtk_text_unknown_char_utf8, &ch);
  g_assert(ch == gtk_text_unknown_char);
  
  /* First, we turn on btree debugging. */
  gtk_debug_flags |= GTK_DEBUG_TEXT;

  /* Create a buffer */
  buffer = gtk_text_buffer_new(NULL);

  /* Check that buffer starts with one empty line and zero chars */
  
  n = gtk_text_buffer_get_line_count(buffer);
  if (n != 1)
    g_error("%d lines, expected 1", n);
  
  n = gtk_text_buffer_get_char_count(buffer);
  if (n != 1)
    g_error("%d chars, expected 1", n);
  
  /* Run gruesome alien test suite on buffer */
  run_tests(buffer);
  
  /* Put stuff in the buffer */

  fill_buffer(buffer);

  /* Subject stuff-bloated buffer to further torment */
  run_tests(buffer);

  /* Delete all stuff from the buffer */
  gtk_text_buffer_get_bounds(buffer, &start, &end);
  gtk_text_buffer_delete(buffer, &start, &end);

  /* Check buffer for emptiness (note that a single
     empty line always remains in the buffer) */
  n = gtk_text_buffer_get_line_count(buffer);
  if (n != 1)
    g_error("%d lines, expected 1", n);
  
  n = gtk_text_buffer_get_char_count(buffer);
  if (n != 1)
    g_error("%d chars, expected 1", n);

  run_tests(buffer);
  
  return 0;
}

static void
run_tests(GtkTextBuffer *buffer)
{
  GtkTextIter iter;
  GtkTextIter start;
  GtkTextIter end;
  GtkTextIter mark;
  gint i, j;
  gint num_chars;
  GtkTextMark *bar_mark;
  
  gtk_text_buffer_get_bounds(buffer, &start, &end);
  
  /* Check that walking the tree via chars and via indexes produces
   * the same number of indexable locations.
   */
  num_chars = gtk_text_buffer_get_char_count(buffer);
  iter = start;
  bar_mark = gtk_text_buffer_create_mark(buffer, "bar", &iter, FALSE);
  i = 0;
  while (i < num_chars)
    {
      GtkTextIter current;
      GtkTextMark *foo_mark;
      
      gtk_text_buffer_get_iter_at_char(buffer, &current, i);

      if (!gtk_text_iter_equal(&iter, &current))
        {
          g_error("get_char_index didn't return current iter");
        }

      j = gtk_text_iter_get_char_index(&iter);

      if (i != j)
        {
          g_error("iter converted to %d not %d", j, i);
        }

      /* get/set mark */
      gtk_text_buffer_get_iter_at_mark(buffer, &mark, bar_mark);

      if (!gtk_text_iter_equal(&iter, &mark))
        {
          gtk_text_iter_spew(&iter, "iter");
          gtk_text_iter_spew(&mark, "mark");
          g_error("Mark not moved to the right place.");
        }
      
      foo_mark = gtk_text_buffer_create_mark(buffer, "foo", &iter, FALSE);
      gtk_text_buffer_get_iter_at_mark(buffer, &mark, foo_mark);
      gtk_text_buffer_delete_mark(buffer, foo_mark);
      
      if (!gtk_text_iter_equal(&iter, &mark))
        {
          gtk_text_iter_spew(&iter, "iter");
          gtk_text_iter_spew(&mark, "mark");
          g_error("Mark not created in the right place.");
        }
      
      if (!gtk_text_iter_forward_char(&iter))
        g_error("iterators ran out before chars");      

      gtk_text_buffer_move_mark(buffer, bar_mark, &iter);
      
      ++i;
    }

  if (!gtk_text_iter_equal(&iter, &end))
    g_error("Iterating over all chars didn't end with the end iter");

  /* Do the tree-walk backward 
   */
  num_chars = gtk_text_buffer_get_char_count(buffer);
  gtk_text_buffer_get_iter_at_char(buffer, &iter, -1);

  gtk_text_buffer_move_mark(buffer, bar_mark, &iter);
  
  i = num_chars;

  if (!gtk_text_iter_equal(&iter, &end))
    g_error("iter at char -1 is not equal to the end iterator");
  
  while (i >= 0)
    {
      GtkTextIter current;
      GtkTextMark *foo_mark;
      
      gtk_text_buffer_get_iter_at_char(buffer, &current, i);

      if (!gtk_text_iter_equal(&iter, &current))
        {
          g_error("get_char_index didn't return current iter while going backward");
        }
      j = gtk_text_iter_get_char_index(&iter);

      if (i != j)
        {
          g_error("going backward, iter converted to %d not %d", j, i);
        }

      /* get/set mark */
      gtk_text_buffer_get_iter_at_mark(buffer, &mark, bar_mark);

      if (!gtk_text_iter_equal(&iter, &mark))
        {
          gtk_text_iter_spew(&iter, "iter");
          gtk_text_iter_spew(&mark, "mark");
          g_error("Mark not moved to the right place.");
        }
      
      foo_mark = gtk_text_buffer_create_mark(buffer, "foo", &iter, FALSE);
      gtk_text_buffer_get_iter_at_mark(buffer, &mark, foo_mark);
      gtk_text_buffer_delete_mark(buffer, foo_mark);
      
      if (!gtk_text_iter_equal(&iter, &mark))
        {
          gtk_text_iter_spew(&iter, "iter");
          gtk_text_iter_spew(&mark, "mark");
          g_error("Mark not created in the right place.");
        }
      
      if (i > 0)
        {
          if (!gtk_text_iter_backward_char(&iter))
            g_error("iterators ran out before char indexes");

          gtk_text_buffer_move_mark(buffer, bar_mark, &iter);
        }
      else
        {
          if (gtk_text_iter_backward_char(&iter))
            g_error("went backward from 0?");
        }
      
      --i;
    }
  
  if (!gtk_text_iter_equal(&iter, &start))
    g_error("Iterating backward over all chars didn't end with the start iter");

  /*
   * Check that get_line_count returns the same number of lines
   * as walking the tree by line
   */
  i = 1; /* include current (first) line */
  gtk_text_buffer_get_iter_at_line(buffer, &iter, 0);
  while (gtk_text_iter_forward_line(&iter))
    ++i;

  /* Add 1 to the line count, because 'i' counts the end-iterator line */
  if (i != gtk_text_buffer_get_line_count(buffer) + 1)
    g_error("Counted %d lines, buffer has %d", i,
            gtk_text_buffer_get_line_count(buffer) + 1);
}


static char  *book_closed_xpm[] = {
"16 16 6 1",
"       c None s None",
".      c black",
"X      c red",
"o      c yellow",
"O      c #808080",
"#      c white",
"                ",
"       ..       ",
"     ..XX.      ",
"   ..XXXXX.     ",
" ..XXXXXXXX.    ",
".ooXXXXXXXXX.   ",
"..ooXXXXXXXXX.  ",
".X.ooXXXXXXXXX. ",
".XX.ooXXXXXX..  ",
" .XX.ooXXX..#O  ",
"  .XX.oo..##OO. ",
"   .XX..##OO..  ",
"    .X.#OO..    ",
"     ..O..      ",
"      ..        ",
"                "};

static void
fill_buffer(GtkTextBuffer *buffer)
{
  GtkTextTag *tag;
  GdkColor color, color2;
  GtkTextIter iter;
  GtkTextIter iter2;
  GdkPixmap *pixmap;
  GdkBitmap *mask;
  int i;
  
  tag = gtk_text_buffer_create_tag(buffer, "fg_blue");

  color.red = color.green = 0;
  color.blue = 0xffff;
  color2.red = 0xfff;
  color2.blue = 0x0;
  color2.green = 0;
  gtk_object_set(GTK_OBJECT(tag),
                 "foreground_gdk", &color,
                 "background_gdk", &color2,
                 "font", "-*-courier-bold-r-*-*-30-*-*-*-*-*-*-*",
                 NULL);

  tag = gtk_text_buffer_create_tag(buffer, "fg_red");

  color.blue = color.green = 0;
  color.red = 0xffff;
  gtk_object_set(GTK_OBJECT(tag),
                 "offset", -4,
                 "foreground_gdk", &color,
                 NULL);

  tag = gtk_text_buffer_create_tag(buffer, "bg_green");

  color.blue = color.red = 0;
  color.green = 0xffff;
  gtk_object_set(GTK_OBJECT(tag),
                 "background_gdk", &color,
                 "font", "-*-courier-bold-r-*-*-10-*-*-*-*-*-*-*",
                 NULL);

  pixmap = gdk_pixmap_colormap_create_from_xpm_d (NULL,
                                                  gtk_widget_get_default_colormap(),
                                                  &mask,
                                                  NULL, book_closed_xpm);
  
  g_assert(pixmap != NULL);
  
  i = 0;
  while (i < 10)
    {
      gchar *str;

      gtk_text_buffer_get_iter_at_char(buffer, &iter, 0);
      
      gtk_text_buffer_insert_pixmap (buffer, &iter, pixmap, mask);

      gtk_text_buffer_get_iter_at_char(buffer, &iter, 1);
      
      gtk_text_buffer_insert_pixmap (buffer, &iter, pixmap, mask);
      
      str = g_strdup_printf("%d Hello World!\nwoo woo woo woo woo woo woo woo\n",
                            i);
      
      gtk_text_buffer_insert(buffer, &iter, str, -1);

      g_free(str);
      
      gtk_text_buffer_insert(buffer, &iter,
                              "(Hello World!)\nfoo foo Hello this is some text we are using to text word wrap. It has punctuation! gee; blah - hmm, great.\nnew line\n\n"
                              /* This is UTF8 stuff, Emacs doesn't
                                 really know how to display it */
                              "Spanish (Español) ¡Hola! / French (Français) Bonjour, Salut / German (Deutsch Süd) Grüß Gott (testing Latin-1 chars encoded in UTF8)\nThai (we can't display this, just making sure we don't crash)  (ภาษาไทย)  สวัสดีครับ, สวัสดีค่ะ\n",
                              -1);  
      
      gtk_text_buffer_insert_pixmap (buffer, &iter, pixmap, mask);
      gtk_text_buffer_insert_pixmap (buffer, &iter, pixmap, mask);
      
      gtk_text_buffer_get_iter_at_char(buffer, &iter, 4);
      
      gtk_text_buffer_insert_pixmap (buffer, &iter, pixmap, mask);

      gtk_text_buffer_get_iter_at_char(buffer, &iter, 7);
      
      gtk_text_buffer_insert_pixmap (buffer, &iter, pixmap, mask);

      gtk_text_buffer_get_iter_at_char(buffer, &iter, 8);
      
      gtk_text_buffer_insert_pixmap (buffer, &iter, pixmap, mask);

      gtk_text_buffer_get_iter_at_line_char(buffer, &iter, 0, 8);
      iter2 = iter;
      gtk_text_iter_forward_chars(&iter2, 10);

      gtk_text_buffer_apply_tag(buffer, "fg_blue", &iter, &iter2);

      gtk_text_iter_forward_chars(&iter, 7);
      gtk_text_iter_forward_chars(&iter2, 10);
      
      gtk_text_buffer_apply_tag(buffer, "bg_green", &iter, &iter2);

      gtk_text_iter_forward_chars(&iter, 12);
      gtk_text_iter_forward_chars(&iter2, 10);
      
      gtk_text_buffer_apply_tag(buffer, "bg_green", &iter, &iter2);

      gtk_text_iter_forward_chars(&iter, 10);
      gtk_text_iter_forward_chars(&iter2, 15);
      
      gtk_text_buffer_apply_tag(buffer, "fg_red", &iter, &iter2);
      gtk_text_buffer_apply_tag(buffer, "fg_blue", &iter, &iter2);      

      gtk_text_iter_forward_chars(&iter, 20);
      gtk_text_iter_forward_chars(&iter2, 20);
      
      gtk_text_buffer_apply_tag(buffer, "fg_red", &iter, &iter2);
      gtk_text_buffer_apply_tag(buffer, "fg_blue", &iter, &iter2);      

      gtk_text_iter_backward_chars(&iter, 25);
      gtk_text_iter_forward_chars(&iter2, 5);
      
      gtk_text_buffer_apply_tag(buffer, "fg_red", &iter, &iter2);
      gtk_text_buffer_apply_tag(buffer, "fg_blue", &iter, &iter2);      

      gtk_text_iter_forward_chars(&iter, 15);
      gtk_text_iter_backward_chars(&iter2, 10);

      gtk_text_buffer_remove_tag(buffer, "fg_red", &iter, &iter2);
      gtk_text_buffer_remove_tag(buffer, "fg_blue", &iter, &iter2);      
      
      ++i;
    }

  gdk_pixmap_unref(pixmap);
  if (mask)
    gdk_bitmap_unref(mask);
}