textview.c 23.3 KB
Newer Older
Havoc Pennington's avatar
Havoc Pennington committed
1 2 3 4 5 6 7 8 9 10
/* Text Widget
 *
 * The GtkTextView widget displays a GtkTextBuffer. One GtkTextBuffer
 * can be displayed by multiple GtkTextViews. This demo has two views
 * displaying a single buffer, and shows off the widget's text
 * formatting features.
 *
 */

#include <gtk/gtk.h>
11
#include <stdlib.h> /* for exit() */
Havoc Pennington's avatar
Havoc Pennington committed
12

13 14
#include "demo-common.h"

Havoc Pennington's avatar
Havoc Pennington committed
15 16
static void easter_egg_callback (GtkWidget *button, gpointer data);

Havoc Pennington's avatar
Havoc Pennington committed
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
#define gray50_width 2
#define gray50_height 2
static char gray50_bits[] = {
  0x02, 0x01
};

static void
create_tags (GtkTextBuffer *buffer)
{
  GdkBitmap *stipple;

  /* Create a bunch of tags. Note that it's also possible to
   * create tags with gtk_text_tag_new() then add them to the
   * tag table for the buffer, gtk_text_buffer_create_tag() is
   * just a convenience function. Also note that you don't have
   * to give tags a name; pass NULL for the name to create an
   * anonymous tag.
   *
   * In any real app, another useful optimization would be to create
   * a GtkTextTagTable in advance, and reuse the same tag table for
   * all the buffers with the same tag set, instead of creating
   * new copies of the same tags for every buffer.
   *
   * Tags are assigned default priorities in order of addition to the
41
   * tag table.	 That is, tags created later that affect the same text
42 43 44
   * property affected by an earlier tag will override the earlier
   * tag.  You can modify tag priorities with
   * gtk_text_tag_set_priority().
Havoc Pennington's avatar
Havoc Pennington committed
45 46
   */

47
  gtk_text_buffer_create_tag (buffer, "heading",
48
			      "weight", PANGO_WEIGHT_BOLD,
49
			      "size", 15 * PANGO_SCALE,
50
			      NULL);
51 52
  
  gtk_text_buffer_create_tag (buffer, "italic",
53
			      "style", PANGO_STYLE_ITALIC, NULL);
Havoc Pennington's avatar
Havoc Pennington committed
54

55
  gtk_text_buffer_create_tag (buffer, "bold",
56
			      "weight", PANGO_WEIGHT_BOLD, NULL);  
57 58
  
  gtk_text_buffer_create_tag (buffer, "big",
59
			      /* points times the PANGO_SCALE factor */
60
			      "size", 20 * PANGO_SCALE, NULL);
Havoc Pennington's avatar
Havoc Pennington committed
61

Havoc Pennington's avatar
Havoc Pennington committed
62
  gtk_text_buffer_create_tag (buffer, "xx-small",
63
			      "scale", PANGO_SCALE_XX_SMALL, NULL);
Havoc Pennington's avatar
Havoc Pennington committed
64 65

  gtk_text_buffer_create_tag (buffer, "x-large",
66
			      "scale", PANGO_SCALE_X_LARGE, NULL);
Havoc Pennington's avatar
Havoc Pennington committed
67
  
68
  gtk_text_buffer_create_tag (buffer, "monospace",
69
			      "family", "monospace", NULL);
Havoc Pennington's avatar
Havoc Pennington committed
70
  
71
  gtk_text_buffer_create_tag (buffer, "blue_foreground",
72
			      "foreground", "blue", NULL);  
Havoc Pennington's avatar
Havoc Pennington committed
73

74
  gtk_text_buffer_create_tag (buffer, "red_background",
75
			      "background", "red", NULL);
Havoc Pennington's avatar
Havoc Pennington committed
76 77

  stipple = gdk_bitmap_create_from_data (NULL,
78 79
					 gray50_bits, gray50_width,
					 gray50_height);
Havoc Pennington's avatar
Havoc Pennington committed
80
  
81
  gtk_text_buffer_create_tag (buffer, "background_stipple",
82
			      "background_stipple", stipple, NULL);
Havoc Pennington's avatar
Havoc Pennington committed
83

84
  gtk_text_buffer_create_tag (buffer, "foreground_stipple",
85
			      "foreground_stipple", stipple, NULL);
Havoc Pennington's avatar
Havoc Pennington committed
86

Manish Singh's avatar
Manish Singh committed
87
  g_object_unref (stipple);
Havoc Pennington's avatar
Havoc Pennington committed
88

89
  gtk_text_buffer_create_tag (buffer, "big_gap_before_line",
90
			      "pixels_above_lines", 30, NULL);
Havoc Pennington's avatar
Havoc Pennington committed
91

92
  gtk_text_buffer_create_tag (buffer, "big_gap_after_line",
93
			      "pixels_below_lines", 30, NULL);
Havoc Pennington's avatar
Havoc Pennington committed
94

95
  gtk_text_buffer_create_tag (buffer, "double_spaced_line",
96
			      "pixels_inside_wrap", 10, NULL);
Havoc Pennington's avatar
Havoc Pennington committed
97

98
  gtk_text_buffer_create_tag (buffer, "not_editable",
99
			      "editable", FALSE, NULL);
Havoc Pennington's avatar
Havoc Pennington committed
100
  
101
  gtk_text_buffer_create_tag (buffer, "word_wrap",
102
			      "wrap_mode", GTK_WRAP_WORD, NULL);
Havoc Pennington's avatar
Havoc Pennington committed
103

104
  gtk_text_buffer_create_tag (buffer, "char_wrap",
105
			      "wrap_mode", GTK_WRAP_CHAR, NULL);
Havoc Pennington's avatar
Havoc Pennington committed
106

107
  gtk_text_buffer_create_tag (buffer, "no_wrap",
108
			      "wrap_mode", GTK_WRAP_NONE, NULL);
Havoc Pennington's avatar
Havoc Pennington committed
109
  
110
  gtk_text_buffer_create_tag (buffer, "center",
111
			      "justification", GTK_JUSTIFY_CENTER, NULL);
Havoc Pennington's avatar
Havoc Pennington committed
112

113
  gtk_text_buffer_create_tag (buffer, "right_justify",
114
			      "justification", GTK_JUSTIFY_RIGHT, NULL);
Havoc Pennington's avatar
Havoc Pennington committed
115

116
  gtk_text_buffer_create_tag (buffer, "wide_margins",
117 118
			      "left_margin", 50, "right_margin", 50,
			      NULL);
Havoc Pennington's avatar
Havoc Pennington committed
119
  
120
  gtk_text_buffer_create_tag (buffer, "strikethrough",
121
			      "strikethrough", TRUE, NULL);
122 123
  
  gtk_text_buffer_create_tag (buffer, "underline",
124
			      "underline", PANGO_UNDERLINE_SINGLE, NULL);
Havoc Pennington's avatar
Havoc Pennington committed
125

126
  gtk_text_buffer_create_tag (buffer, "double_underline",
127
			      "underline", PANGO_UNDERLINE_DOUBLE, NULL);
Havoc Pennington's avatar
Havoc Pennington committed
128

129
  gtk_text_buffer_create_tag (buffer, "superscript",
130 131 132
			      "rise", 10 * PANGO_SCALE,	  /* 10 pixels */
			      "size", 8 * PANGO_SCALE,	  /* 8 points */
			      NULL);
Havoc Pennington's avatar
Havoc Pennington committed
133
  
134
  gtk_text_buffer_create_tag (buffer, "subscript",
135 136 137
			      "rise", -10 * PANGO_SCALE,   /* 10 pixels */
			      "size", 8 * PANGO_SCALE,	   /* 8 points */
			      NULL);
138 139

  gtk_text_buffer_create_tag (buffer, "rtl_quote",
140 141 142 143 144 145
			      "wrap_mode", GTK_WRAP_WORD,
			      "direction", GTK_TEXT_DIR_RTL,
			      "indent", 30,
			      "left_margin", 20,
			      "right_margin", 20,
			      NULL);
Havoc Pennington's avatar
Havoc Pennington committed
146 147 148 149 150 151 152
}

static void
insert_text (GtkTextBuffer *buffer)
{
  GtkTextIter iter;
  GtkTextIter start, end;
153
  GdkPixbuf *pixbuf;
154 155
  GdkPixbuf *scaled;
  GtkTextChildAnchor *anchor;
156
  char *filename;
157

158 159 160 161 162 163 164 165 166 167 168
  /* demo_find_file() looks in the the current directory first,
   * so you can run gtk-demo without installing GTK, then looks
   * in the location where the file is installed.
   */
  pixbuf = NULL;
  filename = demo_find_file ("gtk-logo-rgb.gif", NULL);
  if (filename)
    {
      pixbuf = gdk_pixbuf_new_from_file (filename, NULL);
      g_free (filename);
    }
169 170 171 172 173 174

  if (pixbuf == NULL)
    {
      g_printerr ("Failed to load image file gtk-logo-rgb.gif\n");
      exit (1);
    }
175

176
  scaled = gdk_pixbuf_scale_simple (pixbuf, 32, 32, GDK_INTERP_BILINEAR);
Manish Singh's avatar
Manish Singh committed
177
  g_object_unref (pixbuf);
178
  pixbuf = scaled;
Havoc Pennington's avatar
Havoc Pennington committed
179 180 181 182 183 184
  
  /* get start of buffer; each insertion will revalidate the
   * iterator to point to just after the inserted text.
   */
  gtk_text_buffer_get_iter_at_offset (buffer, &iter, 0);

185
  gtk_text_buffer_insert (buffer, &iter, "The text widget can display text with all kinds of nifty attributes. It also supports multiple views of the same buffer; this demo is showing the same buffer in two places.\n\n", -1);
Havoc Pennington's avatar
Havoc Pennington committed
186

187
  gtk_text_buffer_insert_with_tags_by_name (buffer, &iter, "Font styles. ", -1,
188
					    "heading", NULL);
189
  
Havoc Pennington's avatar
Havoc Pennington committed
190 191
  gtk_text_buffer_insert (buffer, &iter, "For example, you can have ", -1);
  gtk_text_buffer_insert_with_tags_by_name (buffer, &iter,
192 193
					    "italic", -1,
					    "italic", NULL);
Havoc Pennington's avatar
Havoc Pennington committed
194 195
  gtk_text_buffer_insert (buffer, &iter, ", ", -1);  
  gtk_text_buffer_insert_with_tags_by_name (buffer, &iter,
196 197
					    "bold", -1,
					    "bold", NULL);
198
  gtk_text_buffer_insert (buffer, &iter, ", or ", -1);
Havoc Pennington's avatar
Havoc Pennington committed
199
  gtk_text_buffer_insert_with_tags_by_name (buffer, &iter,
200 201
					    "monospace (typewriter)", -1,
					    "monospace", NULL);
202 203
  gtk_text_buffer_insert (buffer, &iter, ", or ", -1);
  gtk_text_buffer_insert_with_tags_by_name (buffer, &iter,
204 205
					    "big", -1,
					    "big", NULL);
Havoc Pennington's avatar
Havoc Pennington committed
206 207 208
  gtk_text_buffer_insert (buffer, &iter, " text. ", -1);
  gtk_text_buffer_insert (buffer, &iter, "It's best not to hardcode specific text sizes; you can use relative sizes as with CSS, such as ", -1);
  gtk_text_buffer_insert_with_tags_by_name (buffer, &iter,
209 210
					    "xx-small", -1,
					    "xx-small", NULL);
Havoc Pennington's avatar
Havoc Pennington committed
211 212
  gtk_text_buffer_insert (buffer, &iter, " or ", -1);
  gtk_text_buffer_insert_with_tags_by_name (buffer, &iter,
213 214
					    "x-large", -1,
					    "x-large", NULL);
Havoc Pennington's avatar
Havoc Pennington committed
215 216
  gtk_text_buffer_insert (buffer, &iter, " to ensure that your program properly adapts if the user changes the default font size.\n\n", -1);
  
217
  gtk_text_buffer_insert_with_tags_by_name (buffer, &iter, "Colors. ", -1,
218
					    "heading", NULL);
219 220
  
  gtk_text_buffer_insert (buffer, &iter, "Colors such as ", -1);  
Havoc Pennington's avatar
Havoc Pennington committed
221
  gtk_text_buffer_insert_with_tags_by_name (buffer, &iter,
222 223
					    "a blue foreground", -1,
					    "blue_foreground", NULL);
Havoc Pennington's avatar
Havoc Pennington committed
224 225
  gtk_text_buffer_insert (buffer, &iter, " or ", -1);  
  gtk_text_buffer_insert_with_tags_by_name (buffer, &iter,
226 227
					    "a red background", -1,
					    "red_background", NULL);
Havoc Pennington's avatar
Havoc Pennington committed
228 229
  gtk_text_buffer_insert (buffer, &iter, " or even ", -1);  
  gtk_text_buffer_insert_with_tags_by_name (buffer, &iter,
230 231 232 233
					    "a stippled red background", -1,
					    "red_background",
					    "background_stipple",
					    NULL);
Havoc Pennington's avatar
Havoc Pennington committed
234 235 236

  gtk_text_buffer_insert (buffer, &iter, " or ", -1);  
  gtk_text_buffer_insert_with_tags_by_name (buffer, &iter,
237 238 239 240 241
					    "a stippled blue foreground on solid red background", -1,
					    "blue_foreground",
					    "red_background",
					    "foreground_stipple",
					    NULL);
242
  gtk_text_buffer_insert (buffer, &iter, " (select that to read it) can be used.\n\n", -1);  
Havoc Pennington's avatar
Havoc Pennington committed
243

244
  gtk_text_buffer_insert_with_tags_by_name (buffer, &iter, "Underline, strikethrough, and rise. ", -1,
245
					    "heading", NULL);
246
  
247
  gtk_text_buffer_insert_with_tags_by_name (buffer, &iter,
248 249
					    "Strikethrough", -1,
					    "strikethrough", NULL);
250 251
  gtk_text_buffer_insert (buffer, &iter, ", ", -1);
  gtk_text_buffer_insert_with_tags_by_name (buffer, &iter,
252 253
					    "underline", -1,
					    "underline", NULL);
254 255
  gtk_text_buffer_insert (buffer, &iter, ", ", -1);
  gtk_text_buffer_insert_with_tags_by_name (buffer, &iter,
256 257
					    "double underline", -1, 
					    "double_underline", NULL);
258 259
  gtk_text_buffer_insert (buffer, &iter, ", ", -1);
  gtk_text_buffer_insert_with_tags_by_name (buffer, &iter,
260 261
					    "superscript", -1,
					    "superscript", NULL);
262 263
  gtk_text_buffer_insert (buffer, &iter, ", and ", -1);
  gtk_text_buffer_insert_with_tags_by_name (buffer, &iter,
264 265
					    "subscript", -1,
					    "subscript", NULL);
266
  gtk_text_buffer_insert (buffer, &iter, " are all supported.\n\n", -1);
267

268
  gtk_text_buffer_insert_with_tags_by_name (buffer, &iter, "Images. ", -1,
269
					    "heading", NULL);
270
  
271 272 273 274
  gtk_text_buffer_insert (buffer, &iter, "The buffer can have images in it: ", -1);
  gtk_text_buffer_insert_pixbuf (buffer, &iter, pixbuf);
  gtk_text_buffer_insert_pixbuf (buffer, &iter, pixbuf);
  gtk_text_buffer_insert_pixbuf (buffer, &iter, pixbuf);
275 276 277
  gtk_text_buffer_insert (buffer, &iter, " for example.\n\n", -1);

  gtk_text_buffer_insert_with_tags_by_name (buffer, &iter, "Spacing. ", -1,
278
					    "heading", NULL);
279 280

  gtk_text_buffer_insert (buffer, &iter, "You can adjust the amount of space before each line.\n", -1);
Havoc Pennington's avatar
Havoc Pennington committed
281
  
282
  gtk_text_buffer_insert_with_tags_by_name (buffer, &iter,
283 284
					    "This line has a whole lot of space before it.\n", -1,
					    "big_gap_before_line", "wide_margins", NULL);
285
  gtk_text_buffer_insert_with_tags_by_name (buffer, &iter,
286 287
					    "You can also adjust the amount of space after each line; this line has a whole lot of space after it.\n", -1,
					    "big_gap_after_line", "wide_margins", NULL);
Havoc Pennington's avatar
Havoc Pennington committed
288
  
289
  gtk_text_buffer_insert_with_tags_by_name (buffer, &iter,
290 291
					    "You can also adjust the amount of space between wrapped lines; this line has extra space between each wrapped line in the same paragraph. To show off wrapping, some filler text: the quick brown fox jumped over the lazy dog. Blah blah blah blah blah blah blah blah blah.\n", -1,
					    "double_spaced_line", "wide_margins", NULL);
292

293 294 295
  gtk_text_buffer_insert (buffer, &iter, "Also note that those lines have extra-wide margins.\n\n", -1);

  gtk_text_buffer_insert_with_tags_by_name (buffer, &iter, "Editability. ", -1,
296
					    "heading", NULL);
297 298
  
  gtk_text_buffer_insert_with_tags_by_name (buffer, &iter,
299 300
					    "This line is 'locked down' and can't be edited by the user - just try it! You can't delete this line.\n\n", -1,
					    "not_editable", NULL);
301

302
  gtk_text_buffer_insert_with_tags_by_name (buffer, &iter, "Wrapping. ", -1,
303
					    "heading", NULL);
304 305

  gtk_text_buffer_insert (buffer, &iter,
306
			  "This line (and most of the others in this buffer) is word-wrapped, using the proper Unicode algorithm. Word wrap should work in all scripts and languages that GTK+ supports. Let's make this a long paragraph to demonstrate: blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah\n\n", -1);  
307
  
308
  gtk_text_buffer_insert_with_tags_by_name (buffer, &iter,
309 310
					    "This line has character-based wrapping, and can wrap between any two character glyphs. Let's make this a long paragraph to demonstrate: blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah\n\n", -1,
					    "char_wrap", NULL);
311
  
312
  gtk_text_buffer_insert_with_tags_by_name (buffer, &iter,
313 314
					    "This line has all wrapping turned off, so it makes the horizontal scrollbar appear.\n\n\n", -1,
					    "no_wrap", NULL);
315 316

  gtk_text_buffer_insert_with_tags_by_name (buffer, &iter, "Justification. ", -1,
317
					    "heading", NULL);  
318 319
  
  gtk_text_buffer_insert_with_tags_by_name (buffer, &iter,
320 321
					    "\nThis line has center justification.\n", -1,
					    "center", NULL);
322 323

  gtk_text_buffer_insert_with_tags_by_name (buffer, &iter,
324 325
					    "This line has right justification.\n", -1,
					    "right_justify", NULL);
326 327

  gtk_text_buffer_insert_with_tags_by_name (buffer, &iter,
328 329
					    "\nThis line has big wide margins. Text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text.\n", -1,
					    "wide_margins", NULL);  
330 331

  gtk_text_buffer_insert_with_tags_by_name (buffer, &iter, "Internationalization. ", -1,
332 333
					    "heading", NULL);
	  
334
  gtk_text_buffer_insert (buffer, &iter,
335
			  "You can put all sorts of Unicode text in the buffer.\n\nGerman (Deutsch S\303\274d) Gr\303\274\303\237 Gott\nGreek (\316\225\316\273\316\273\316\267\316\275\316\271\316\272\316\254) \316\223\316\265\316\271\316\254 \317\203\316\261\317\202\nHebrew	\327\251\327\234\327\225\327\235\nJapanese (\346\227\245\346\234\254\350\252\236)\n\nThe widget properly handles bidirectional text, word wrapping, DOS/UNIX/Unicode paragraph separators, grapheme boundaries, and so on using the Pango internationalization framework.\n", -1);  
336

337
  gtk_text_buffer_insert (buffer, &iter, "Here's a word-wrapped quote in a right-to-left language:\n", -1);
338
  gtk_text_buffer_insert_with_tags_by_name (buffer, &iter, "\331\210\331\202\330\257 \330\250\330\257\330\243 \330\253\331\204\330\247\330\253 \331\205\331\206 \330\243\331\203\330\253\330\261 \330\247\331\204\331\205\330\244\330\263\330\263\330\247\330\252 \330\252\331\202\330\257\331\205\330\247 \331\201\331\212 \330\264\330\250\331\203\330\251 \330\247\331\203\330\263\331\212\331\210\331\206 \330\250\330\261\330\247\331\205\330\254\331\207\330\247 \331\203\331\205\331\206\330\270\331\205\330\247\330\252 \331\204\330\247 \330\252\330\263\330\271\331\211 \331\204\331\204\330\261\330\250\330\255\330\214 \330\253\331\205 \330\252\330\255\331\210\331\204\330\252 \331\201\331\212 \330\247\331\204\330\263\331\206\331\210\330\247\330\252 \330\247\331\204\330\256\331\205\330\263 \330\247\331\204\331\205\330\247\330\266\331\212\330\251 \330\245\331\204\331\211 \331\205\330\244\330\263\330\263\330\247\330\252 \331\205\330\247\331\204\331\212\330\251 \331\205\331\206\330\270\331\205\330\251\330\214 \331\210\330\250\330\247\330\252\330\252 \330\254\330\262\330\241\330\247 \331\205\331\206 \330\247\331\204\331\206\330\270\330\247\331\205 \330\247\331\204\331\205\330\247\331\204\331\212 \331\201\331\212 \330\250\331\204\330\257\330\247\331\206\331\207\330\247\330\214 \331\210\331\204\331\203\331\206\331\207\330\247 \330\252\330\252\330\256\330\265\330\265 \331\201\331\212 \330\256\330\257\331\205\330\251 \331\202\330\267\330\247\330\271 \330\247\331\204\331\205\330\264\330\261\331\210\330\271\330\247\330\252 \330\247\331\204\330\265\330\272\331\212\330\261\330\251. \331\210\330\243\330\255\330\257 \330\243\331\203\330\253\330\261 \331\207\330\260\331\207 \330\247\331\204\331\205\330\244\330\263\330\263\330\247\330\252 \331\206\330\254\330\247\330\255\330\247 \331\207\331\210 \302\273\330\250\330\247\331\206\331\203\331\210\330\263\331\210\331\204\302\253 \331\201\331\212 \330\250\331\210\331\204\331\212\331\201\331\212\330\247.\n\n", -1,
339
						"rtl_quote", NULL);
340 341 342 343 344 345 346 347 348 349 350 351
      
  gtk_text_buffer_insert (buffer, &iter, "You can put widgets in the buffer: Here's a button: ", -1);
  anchor = gtk_text_buffer_create_child_anchor (buffer, &iter);
  gtk_text_buffer_insert (buffer, &iter, " and a menu: ", -1);
  anchor = gtk_text_buffer_create_child_anchor (buffer, &iter);
  gtk_text_buffer_insert (buffer, &iter, " and a scale: ", -1);
  anchor = gtk_text_buffer_create_child_anchor (buffer, &iter);
  gtk_text_buffer_insert (buffer, &iter, " and an animation: ", -1);
  anchor = gtk_text_buffer_create_child_anchor (buffer, &iter);
  gtk_text_buffer_insert (buffer, &iter, " finally a text entry: ", -1);
  anchor = gtk_text_buffer_create_child_anchor (buffer, &iter);
  gtk_text_buffer_insert (buffer, &iter, ".\n", -1);
Havoc Pennington's avatar
Havoc Pennington committed
352
  
353
  gtk_text_buffer_insert (buffer, &iter, "\n\nThis demo doesn't demonstrate all the GtkTextBuffer features; it leaves out, for example: invisible/hidden text (doesn't work in GTK 2, but planned), tab stops, application-drawn areas on the sides of the widget for displaying breakpoints and such...", -1);
Havoc Pennington's avatar
Havoc Pennington committed
354 355 356 357

  /* Apply word_wrap tag to whole buffer */
  gtk_text_buffer_get_bounds (buffer, &start, &end);
  gtk_text_buffer_apply_tag_by_name (buffer, "word_wrap", &start, &end);
358

Manish Singh's avatar
Manish Singh committed
359
  g_object_unref (pixbuf);
Havoc Pennington's avatar
Havoc Pennington committed
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 394
static gboolean
find_anchor (GtkTextIter *iter)
{
  while (gtk_text_iter_forward_char (iter))
    {
      if (gtk_text_iter_get_child_anchor (iter))
        return TRUE;
    }
  return FALSE;
}

static void
attach_widgets (GtkTextView *text_view)
{
  GtkTextIter iter;
  GtkTextBuffer *buffer;
  int i;
  
  buffer = gtk_text_view_get_buffer (text_view);

  gtk_text_buffer_get_start_iter (buffer, &iter);

  i = 0;
  while (find_anchor (&iter))
    {
      GtkTextChildAnchor *anchor;
      GtkWidget *widget;
      
      anchor = gtk_text_iter_get_child_anchor (&iter);

      if (i == 0)
        {
          widget = gtk_button_new_with_label ("Click Me");
Havoc Pennington's avatar
Havoc Pennington committed
395

Manish Singh's avatar
Manish Singh committed
396
          g_signal_connect (widget, "clicked",
Havoc Pennington's avatar
Havoc Pennington committed
397 398
                            G_CALLBACK (easter_egg_callback),
                            NULL);
399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425
        }
      else if (i == 1)
        {
          GtkWidget *menu_item;
          GtkWidget *menu;

          menu = gtk_menu_new ();
          
          widget = gtk_option_menu_new ();

          menu_item = gtk_menu_item_new_with_label ("Option 1");
          gtk_menu_shell_append (GTK_MENU_SHELL (menu), menu_item);
          menu_item = gtk_menu_item_new_with_label ("Option 2");
          gtk_menu_shell_append (GTK_MENU_SHELL (menu), menu_item);
          menu_item = gtk_menu_item_new_with_label ("Option 3");
          gtk_menu_shell_append (GTK_MENU_SHELL (menu), menu_item);

          gtk_option_menu_set_menu (GTK_OPTION_MENU (widget), menu);
        }
      else if (i == 2)
        {
          widget = gtk_hscale_new (NULL);
          gtk_range_set_range (GTK_RANGE (widget), 0, 100);
          gtk_widget_set_size_request (widget, 70, -1);
        }
      else if (i == 3)
        {
426 427 428
	  gchar *filename = demo_find_file ("floppybuddy.gif", NULL);
	  widget = gtk_image_new_from_file (filename);
	  g_free (filename);
429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449
        }
      else if (i == 4)
        {
          widget = gtk_entry_new ();
        }
      else
        {
          widget = NULL; /* avoids a compiler warning */
          g_assert_not_reached ();
        }

      gtk_text_view_add_child_at_anchor (text_view,
                                         widget,
                                         anchor);

      gtk_widget_show_all (widget);

      ++i;
    }
}

450
GtkWidget *
Havoc Pennington's avatar
Havoc Pennington committed
451 452 453 454 455 456 457 458 459
do_textview (void)
{
  static GtkWidget *window = NULL;

  if (!window)
    {
      GtkWidget *vpaned;
      GtkWidget *view1;
      GtkWidget *view2;
460
      GtkWidget *sw;
Havoc Pennington's avatar
Havoc Pennington committed
461 462 463
      GtkTextBuffer *buffer;
      
      window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
464
      gtk_window_set_default_size (GTK_WINDOW (window),
465
				   450, 450);
466
      
467 468
      g_signal_connect (window, "destroy",
			G_CALLBACK (gtk_widget_destroyed), &window);
Havoc Pennington's avatar
Havoc Pennington committed
469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485

      gtk_window_set_title (GTK_WINDOW (window), "TextView");
      gtk_container_set_border_width (GTK_CONTAINER (window), 0);

      vpaned = gtk_vpaned_new ();
      gtk_container_set_border_width (GTK_CONTAINER(vpaned), 5);
      gtk_container_add (GTK_CONTAINER (window), vpaned);

      /* For convenience, we just use the autocreated buffer from
       * the first text view; you could also create the buffer
       * by itself with gtk_text_buffer_new(), then later create
       * a view widget.
       */
      view1 = gtk_text_view_new ();
      buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (view1));
      view2 = gtk_text_view_new_with_buffer (buffer);
      
486 487
      sw = gtk_scrolled_window_new (NULL, NULL);
      gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (sw),
488 489
				      GTK_POLICY_AUTOMATIC,
				      GTK_POLICY_AUTOMATIC);
490
      gtk_paned_add1 (GTK_PANED (vpaned), sw);
Havoc Pennington's avatar
Havoc Pennington committed
491

492 493 494 495
      gtk_container_add (GTK_CONTAINER (sw), view1);

      sw = gtk_scrolled_window_new (NULL, NULL);
      gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (sw),
496 497
				      GTK_POLICY_AUTOMATIC,
				      GTK_POLICY_AUTOMATIC);
498
      gtk_paned_add2 (GTK_PANED (vpaned), sw);
Havoc Pennington's avatar
Havoc Pennington committed
499

500
      gtk_container_add (GTK_CONTAINER (sw), view2);
Havoc Pennington's avatar
Havoc Pennington committed
501 502 503

      create_tags (buffer);
      insert_text (buffer);
504 505 506

      attach_widgets (GTK_TEXT_VIEW (view1));
      attach_widgets (GTK_TEXT_VIEW (view2));
Havoc Pennington's avatar
Havoc Pennington committed
507 508 509 510 511
      
      gtk_widget_show_all (vpaned);
    }

  if (!GTK_WIDGET_VISIBLE (window))
512 513 514
    {
      gtk_widget_show (window);
    }
Havoc Pennington's avatar
Havoc Pennington committed
515
  else
516 517 518 519 520 521
    {
      gtk_widget_destroy (window);
      window = NULL;
    }

  return window;
Havoc Pennington's avatar
Havoc Pennington committed
522 523
}

Havoc Pennington's avatar
Havoc Pennington committed
524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585
static void
recursive_attach_view (int                 depth,
                       GtkTextView        *view,
                       GtkTextChildAnchor *anchor)
{
  GtkWidget *child_view;
  GtkWidget *event_box;
  GdkColor color;
  GtkWidget *align;
  
  if (depth > 4)
    return;
  
  child_view = gtk_text_view_new_with_buffer (gtk_text_view_get_buffer (view));

  /* Event box is to add a black border around each child view */
  event_box = gtk_event_box_new ();
  gdk_color_parse ("black", &color);
  gtk_widget_modify_bg (event_box, GTK_STATE_NORMAL, &color);

  align = gtk_alignment_new (0.5, 0.5, 1.0, 1.0);
  gtk_container_set_border_width (GTK_CONTAINER (align), 1);
  
  gtk_container_add (GTK_CONTAINER (event_box), align);
  gtk_container_add (GTK_CONTAINER (align), child_view);
  
  gtk_text_view_add_child_at_anchor (view, event_box, anchor);

  recursive_attach_view (depth + 1, GTK_TEXT_VIEW (child_view), anchor);
}

static void
easter_egg_callback (GtkWidget *button,
                     gpointer   data)
{
  static GtkWidget *window = NULL;
  GtkTextBuffer *buffer;
  GtkWidget     *view;
  GtkTextIter    iter;
  GtkTextChildAnchor *anchor;
  GtkWidget *sw;

  if (window)
    {
      gtk_window_present (GTK_WINDOW (window));
      return;
    }
  
  buffer = gtk_text_buffer_new (NULL);

  gtk_text_buffer_get_start_iter (buffer, &iter);

  gtk_text_buffer_insert (buffer, &iter,
                          "This buffer is shared by a set of nested text views.\n Nested view:\n", -1);
  anchor = gtk_text_buffer_create_child_anchor (buffer, &iter);
  gtk_text_buffer_insert (buffer, &iter,
                          "\nDon't do this in real applications, please.\n", -1);

  view = gtk_text_view_new_with_buffer (buffer);
  
  recursive_attach_view (0, GTK_TEXT_VIEW (view), anchor);
  
Manish Singh's avatar
Manish Singh committed
586
  g_object_unref (buffer);
Havoc Pennington's avatar
Havoc Pennington committed
587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604

  window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
  sw = gtk_scrolled_window_new (NULL, NULL);
  gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (sw),
                                  GTK_POLICY_AUTOMATIC,
                                  GTK_POLICY_AUTOMATIC);

  gtk_container_add (GTK_CONTAINER (window), sw);
  gtk_container_add (GTK_CONTAINER (sw), view);

  g_object_add_weak_pointer (G_OBJECT (window),
                             (gpointer *) &window);

  gtk_window_set_default_size (GTK_WINDOW (window), 300, 400);
  
  gtk_widget_show_all (window);
}