main.c 20.3 KB
Newer Older
1
#include "config.h"
2 3
#include <errno.h>
#include <stdio.h>
4
#include <stdlib.h>
5 6 7
#include <string.h>

#include <gtk/gtk.h>
Tor Lillqvist's avatar
Tor Lillqvist committed
8
#include <glib/gstdio.h>
9 10 11 12 13 14

#include <demos.h>

static GtkTextBuffer *info_buffer;
static GtkTextBuffer *source_buffer;

15 16
static gchar *current_file = NULL;

17

18 19 20 21
enum {
  TITLE_COLUMN,
  FILENAME_COLUMN,
  FUNC_COLUMN,
22
  STYLE_COLUMN,
23 24
  NUM_COLUMNS
};
25

26 27 28 29 30 31 32
typedef struct _CallbackData CallbackData;
struct _CallbackData
{
  GtkTreeModel *model;
  GtkTreePath *path;
};

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
#ifdef G_OS_WIN32

#undef DEMOCODEDIR

static char *
get_democodedir (void)
{
  static char *result = NULL;

  if (result == NULL)
    {
      result = g_win32_get_package_installation_directory (NULL, NULL);
      if (result == NULL)
	result = "unknown-location";

      result = g_strconcat (result, "\\share\\gtk-2.0\\demo", NULL);
    }

  return result;
}

#define DEMOCODEDIR get_democodedir ()

#endif

58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
/**
 * demo_find_file:
 * @base: base filename
 * @err:  location to store error, or %NULL.
 * 
 * Looks for @base first in the current directory, then in the
 * location GTK+ where it will be installed on make install,
 * returns the first file found.
 * 
 * Return value: the filename, if found or %NULL
 **/
gchar *
demo_find_file (const char *base,
		GError    **err)
{
73
  g_return_val_if_fail (err == NULL || *err == NULL, NULL);
74
  
75 76
  if (g_file_test ("gtk-logo-rgb.gif", G_FILE_TEST_EXISTS) &&
      g_file_test (base, G_FILE_TEST_EXISTS))
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91
    return g_strdup (base);
  else
    {
      char *filename = g_build_filename (DEMOCODEDIR, base, NULL);
      if (!g_file_test (filename, G_FILE_TEST_EXISTS))
	{
	  g_set_error (err, G_FILE_ERROR, G_FILE_ERROR_NOENT,
		       "Cannot find demo data file \"%s\"", base);
	  g_free (filename);
	  return NULL;
	}
      return filename;
    }
}

92 93 94 95 96
static void
window_closed_cb (GtkWidget *window, gpointer data)
{
  CallbackData *cbdata = data;
  GtkTreeIter iter;
97
  PangoStyle style;
98 99

  gtk_tree_model_get_iter (cbdata->model, &iter, cbdata->path);
100
  gtk_tree_model_get (GTK_TREE_MODEL (cbdata->model), &iter,
101
		      STYLE_COLUMN, &style,
102
		      -1);
103
  if (style == PANGO_STYLE_ITALIC)
104
    gtk_tree_store_set (GTK_TREE_STORE (cbdata->model), &iter,
105
			STYLE_COLUMN, PANGO_STYLE_NORMAL,
106 107 108 109 110 111
			-1);

  gtk_tree_path_free (cbdata->path);
  g_free (cbdata);
}

112 113 114 115 116
gboolean
read_line (FILE *stream, GString *str)
{
  int n_read = 0;
  
Owen Taylor's avatar
Owen Taylor committed
117
#ifdef HAVE_FLOCKFILE
118
  flockfile (stream);
119
#endif
120 121 122 123 124 125 126

  g_string_truncate (str, 0);
  
  while (1)
    {
      int c;
      
127
#ifdef HAVE_FLOCKFILE
128
      c = getc_unlocked (stream);
129 130 131
#else
      c = getc (stream);
#endif
132 133 134 135 136 137 138 139 140 141 142

      if (c == EOF)
	goto done;
      else
	n_read++;

      switch (c)
	{
	case '\r':
	case '\n':
	  {
Owen Taylor's avatar
Owen Taylor committed
143
#ifdef HAVE_FLOCKFILE
144
	    int next_c = getc_unlocked (stream);
145 146 147
#else
	    int next_c = getc (stream);
#endif
148 149 150 151 152 153 154 155 156 157 158 159 160 161 162
	    
	    if (!(next_c == EOF ||
		  (c == '\r' && next_c == '\n') ||
		  (c == '\n' && next_c == '\r')))
	      ungetc (next_c, stream);
	    
	    goto done;
	  }
	default:
	  g_string_append_c (str, c);
	}
    }

 done:

Owen Taylor's avatar
Owen Taylor committed
163
#ifdef HAVE_FLOCKFILE
164
  funlockfile (stream);
165
#endif
166 167 168 169

  return n_read > 0;
}

170 171 172 173 174 175 176 177 178 179 180 181

/* Stupid syntax highlighting.
 *
 * No regex was used in the making of this highlighting.
 * It should only work for simple cases.  This is good, as
 * that's all we should have in the demos.
 */
/* This code should not be used elsewhere, except perhaps as an example of how
 * to iterate through a text buffer.
 */
enum {
  STATE_NORMAL,
182
  STATE_IN_COMMENT
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
};

static gchar *tokens[] =
{
  "/*",
  "\"",
  NULL
};

static gchar *types[] =
{
  "static",
  "const ",
  "void",
  "gint",
  "int ",
  "char ",
  "gchar ",
  "gfloat",
  "float",
  "gint8",
  "gint16",
  "gint32",
  "guint",
  "guint8",
  "guint16",
  "guint32",
  "guchar",
  "glong",
  "gboolean" ,
  "gshort",
  "gushort",
  "gulong",
  "gdouble",
  "gldouble",
  "gpointer",
  "NULL",
  "GList",
  "GSList",
  "FALSE",
  "TRUE",
  "FILE ",
  "GtkObject ",
  "GtkColorSelection ",
  "GtkWidget ",
  "GtkButton ",
  "GdkColor ",
  "GdkRectangle ",
  "GdkEventExpose ",
  "GdkGC ",
  "GdkPixbufLoader ",
  "GdkPixbuf ",
  "GError",
  "size_t",
  NULL
};

static gchar *control[] =
{
  " if ",
  " while ",
  " else",
  " do ",
  " for ",
  "?",
  ":",
  "return ",
  "goto ",
  NULL
};
void
parse_chars (gchar     *text,
	     gchar    **end_ptr,
	     gint      *state,
	     gchar    **tag,
	     gboolean   start)
{
  gint i;
  gchar *next_token;

  /* Handle comments first */
  if (*state == STATE_IN_COMMENT)
    {
      *end_ptr = strstr (text, "*/");
      if (*end_ptr)
	{
	  *end_ptr += 2;
	  *state = STATE_NORMAL;
	  *tag = "comment";
	}
      return;
    }

  *tag = NULL;
  *end_ptr = NULL;

  /* check for comment */
  if (!strncmp (text, "/*", 2))
    {
      *end_ptr = strstr (text, "*/");
      if (*end_ptr)
	*end_ptr += 2;
      else
	*state = STATE_IN_COMMENT;
      *tag = "comment";
      return;
    }

  /* check for preprocessor defines */
  if (*text == '#' && start)
    {
      *end_ptr = NULL;
      *tag = "preprocessor";
      return;
    }

  /* functions */
  if (start && * text != '\t' && *text != ' ' && *text != '{' && *text != '}')
    {
      if (strstr (text, "("))
	{
	  *end_ptr = strstr (text, "(");
	  *tag = "function";
	  return;
	}
    }
  /* check for types */
  for (i = 0; types[i] != NULL; i++)
    if (!strncmp (text, types[i], strlen (types[i])))
      {
	*end_ptr = text + strlen (types[i]);
	*tag = "type";
	return;
      }

  /* check for control */
  for (i = 0; control[i] != NULL; i++)
    if (!strncmp (text, control[i], strlen (control[i])))
      {
	*end_ptr = text + strlen (control[i]);
	*tag = "control";
	return;
      }

  /* check for string */
  if (text[0] == '"')
    {
      gint maybe_escape = FALSE;

      *end_ptr = text + 1;
      *tag = "string";
      while (**end_ptr != '\000')
	{
	  if (**end_ptr == '\"' && !maybe_escape)
	    {
	      *end_ptr += 1;
	      return;
	    }
	  if (**end_ptr == '\\')
	    maybe_escape = TRUE;
	  else
	    maybe_escape = FALSE;
	  *end_ptr += 1;
	}
      return;
    }

  /* not at the start of a tag.  Find the next one. */
  for (i = 0; tokens[i] != NULL; i++)
    {
      next_token = strstr (text, tokens[i]);
      if (next_token)
	{
	  if (*end_ptr)
	    *end_ptr = (*end_ptr<next_token)?*end_ptr:next_token;
	  else
	    *end_ptr = next_token;
	}
    }

  for (i = 0; types[i] != NULL; i++)
    {
      next_token = strstr (text, types[i]);
      if (next_token)
	{
	  if (*end_ptr)
	    *end_ptr = (*end_ptr<next_token)?*end_ptr:next_token;
	  else
	    *end_ptr = next_token;
	}
    }

  for (i = 0; control[i] != NULL; i++)
    {
      next_token = strstr (text, control[i]);
      if (next_token)
	{
	  if (*end_ptr)
	    *end_ptr = (*end_ptr<next_token)?*end_ptr:next_token;
	  else
	    *end_ptr = next_token;
	}
    }
}

/* While not as cool as c-mode, this will do as a quick attempt at highlighting */
static void
Matthias Clasen's avatar
Matthias Clasen committed
390
fontify (void)
391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422
{
  GtkTextIter start_iter, next_iter, tmp_iter;
  gint state;
  gchar *text;
  gchar *start_ptr, *end_ptr;
  gchar *tag;

  state = STATE_NORMAL;

  gtk_text_buffer_get_iter_at_offset (source_buffer, &start_iter, 0);

  next_iter = start_iter;
  while (gtk_text_iter_forward_line (&next_iter))
    {
      gboolean start = TRUE;
      start_ptr = text = gtk_text_iter_get_text (&start_iter, &next_iter);

      do
	{
	  parse_chars (start_ptr, &end_ptr, &state, &tag, start);

	  start = FALSE;
	  if (end_ptr)
	    {
	      tmp_iter = start_iter;
	      gtk_text_iter_forward_chars (&tmp_iter, end_ptr - start_ptr);
	    }
	  else
	    {
	      tmp_iter = next_iter;
	    }
	  if (tag)
Havoc Pennington's avatar
Havoc Pennington committed
423
	    gtk_text_buffer_apply_tag_by_name (source_buffer, tag, &start_iter, &tmp_iter);
424 425 426 427 428 429 430 431 432 433 434

	  start_iter = tmp_iter;
	  start_ptr = end_ptr;
	}
      while (end_ptr);

      g_free (text);
      start_iter = next_iter;
    }
}

435
void
436
load_file (const gchar *filename)
437 438 439
{
  FILE *file;
  GtkTextIter start, end;
440 441
  char *full_filename;
  GError *err = NULL;
442 443 444 445
  GString *buffer = g_string_new (NULL);
  int state = 0;
  gboolean in_para = 0;

446
  if (current_file && !strcmp (current_file, filename))
447 448 449 450
    {
      g_string_free (buffer, TRUE);
      return;
    }
451

452 453
  g_free (current_file);
  current_file = g_strdup (filename);
454 455 456 457 458 459 460
  
  gtk_text_buffer_get_bounds (info_buffer, &start, &end);
  gtk_text_buffer_delete (info_buffer, &start, &end);

  gtk_text_buffer_get_bounds (source_buffer, &start, &end);
  gtk_text_buffer_delete (source_buffer, &start, &end);

461 462 463 464 465 466 467 468
  full_filename = demo_find_file (filename, &err);
  if (!full_filename)
    {
      g_warning ("%s", err->message);
      g_error_free (err);
      return;
    }

Tor Lillqvist's avatar
Tor Lillqvist committed
469
  file = g_fopen (full_filename, "r");
470 471

  if (!file)
472
    g_warning ("Cannot open %s: %s\n", full_filename, g_strerror (errno));
473

474
  g_free (full_filename);
475

476
  if (!file)
477
    return;
478 479 480 481 482 483

  gtk_text_buffer_get_iter_at_offset (info_buffer, &start, 0);
  while (read_line (file, buffer))
    {
      gchar *p = buffer->str;
      gchar *q;
484
      gchar *r;
485 486 487 488 489
      
      switch (state)
	{
	case 0:
	  /* Reading title */
490
	  while (*p == '/' || *p == '*' || g_ascii_isspace (*p))
491
	    p++;
492 493 494 495 496
	  r = p;
	  while (*r != '/' && strlen (r))
	    r++;
	  if (strlen (r) > 0)
	    p = r + 1;
497
	  q = p + strlen (p);
498
	  while (q > p && g_ascii_isspace (*(q - 1)))
499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521
	    q--;

	  if (q > p)
	    {
	      int len_chars = g_utf8_pointer_to_offset (p, q);

	      end = start;

	      g_assert (strlen (p) >= q - p);
	      gtk_text_buffer_insert (info_buffer, &end, p, q - p);
	      start = end;

	      gtk_text_iter_backward_chars (&start, len_chars);
	      gtk_text_buffer_apply_tag_by_name (info_buffer, "title", &start, &end);

	      start = end;
	      
	      state++;
	    }
	  break;
	    
	case 1:
	  /* Reading body of info section */
522
	  while (g_ascii_isspace (*p))
523 524 525 526 527 528 529 530 531 532
	    p++;
	  if (*p == '*' && *(p + 1) == '/')
	    {
	      gtk_text_buffer_get_iter_at_offset (source_buffer, &start, 0);
	      state++;
	    }
	  else
	    {
	      int len;
	      
533
	      while (*p == '*' || g_ascii_isspace (*p))
534 535 536
		p++;

	      len = strlen (p);
537
	      while (g_ascii_isspace (*(p + len - 1)))
538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558
		len--;
	      
	      if (len > 0)
		{
		  if (in_para)
		    gtk_text_buffer_insert (info_buffer, &start, " ", 1);

		  g_assert (strlen (p) >= len);
		  gtk_text_buffer_insert (info_buffer, &start, p, len);
		  in_para = 1;
		}
	      else
		{
		  gtk_text_buffer_insert (info_buffer, &start, "\n", 1);
		  in_para = 0;
		}
	    }
	  break;

	case 2:
	  /* Skipping blank lines */
559
	  while (g_ascii_isspace (*p))
560 561 562 563 564 565 566 567 568 569 570 571 572
	    p++;
	  if (*p)
	    {
	      p = buffer->str;
	      state++;
	      /* Fall through */
	    }
	  else
	    break;
	  
	case 3:
	  /* Reading program body */
	  gtk_text_buffer_insert (source_buffer, &start, p, -1);
573
	  gtk_text_buffer_insert (source_buffer, &start, "\n", 1);
574 575 576 577
	  break;
	}
    }

578 579
  fclose (file);
  
580
  fontify ();
581 582

  g_string_free (buffer, TRUE);
583 584
}

585
void
586
row_activated_cb (GtkTreeView       *tree_view,
587 588
                  GtkTreePath       *path,
		  GtkTreeViewColumn *column)
589 590
{
  GtkTreeIter iter;
591
  PangoStyle style;
592 593
  GDoDemoFunc func;
  GtkWidget *window;
594 595 596
  GtkTreeModel *model;

  model = gtk_tree_view_get_model (tree_view);
597 598 599 600 601
  
  gtk_tree_model_get_iter (model, &iter, path);
  gtk_tree_model_get (GTK_TREE_MODEL (model),
		      &iter,
		      FUNC_COLUMN, &func,
602
		      STYLE_COLUMN, &style,
603
		      -1);
604

605
  if (func)
606
    {
607 608
      gtk_tree_store_set (GTK_TREE_STORE (model),
			  &iter,
609
			  STYLE_COLUMN, (style == PANGO_STYLE_ITALIC ? PANGO_STYLE_NORMAL : PANGO_STYLE_ITALIC),
610
			  -1);
611
      window = (func) (gtk_widget_get_toplevel (GTK_WIDGET (tree_view)));
612
      
613 614 615 616 617 618 619 620
      if (window != NULL)
	{
	  CallbackData *cbdata;
	  
	  cbdata = g_new (CallbackData, 1);
	  cbdata->model = model;
	  cbdata->path = gtk_tree_path_copy (path);
	  
621 622
	  g_signal_connect (window, "destroy",
			    G_CALLBACK (window_closed_cb), cbdata);
623
	}
624 625 626
    }
}

627 628 629
static void
selection_cb (GtkTreeSelection *selection,
	      GtkTreeModel     *model)
630
{
631 632 633
  GtkTreeIter iter;
  GValue value = {0, };

634
  if (! gtk_tree_selection_get_selected (selection, NULL, &iter))
635 636
    return;

637 638 639
  gtk_tree_model_get_value (model, &iter,
			    FILENAME_COLUMN,
			    &value);
640 641
  if (g_value_get_string (&value))
    load_file (g_value_get_string (&value));
642
  g_value_unset (&value);
643 644
}

645
static GtkWidget *
646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666
create_text (GtkTextBuffer **buffer,
	     gboolean        is_source)
{
  GtkWidget *scrolled_window;
  GtkWidget *text_view;
  PangoFontDescription *font_desc;

  scrolled_window = gtk_scrolled_window_new (NULL, NULL);
  gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scrolled_window),
				  GTK_POLICY_AUTOMATIC,
				  GTK_POLICY_AUTOMATIC);
  gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (scrolled_window),
				       GTK_SHADOW_IN);
  
  text_view = gtk_text_view_new ();
  
  *buffer = gtk_text_buffer_new (NULL);
  gtk_text_view_set_buffer (GTK_TEXT_VIEW (text_view), *buffer);
  gtk_text_view_set_editable (GTK_TEXT_VIEW (text_view), FALSE);
  gtk_text_view_set_cursor_visible (GTK_TEXT_VIEW (text_view), FALSE);

Havoc Pennington's avatar
Havoc Pennington committed
667 668
  gtk_container_add (GTK_CONTAINER (scrolled_window), text_view);
  
669 670
  if (is_source)
    {
671
      font_desc = pango_font_description_from_string ("monospace");
672 673
      gtk_widget_modify_font (text_view, font_desc);
      pango_font_description_free (font_desc);
674 675 676 677 678 679 680 681 682 683 684 685 686

      gtk_text_view_set_wrap_mode (GTK_TEXT_VIEW (text_view),
                                   GTK_WRAP_NONE);
    }
  else
    {
      /* Make it a bit nicer for text. */
      gtk_text_view_set_wrap_mode (GTK_TEXT_VIEW (text_view),
                                   GTK_WRAP_WORD);
      gtk_text_view_set_pixels_above_lines (GTK_TEXT_VIEW (text_view),
                                            2);
      gtk_text_view_set_pixels_below_lines (GTK_TEXT_VIEW (text_view),
                                            2);
687 688 689 690 691
    }
  
  return scrolled_window;
}

692 693 694 695 696 697
static GtkWidget *
create_tree (void)
{
  GtkTreeSelection *selection;
  GtkCellRenderer *cell;
  GtkWidget *tree_view;
698 699
  GtkTreeViewColumn *column;
  GtkTreeStore *model;
700
  GtkTreeIter iter;
701
  GtkWidget *box, *label, *scrolled_window;
702 703

  Demo *d = testgtk_demos;
704

705
  model = gtk_tree_store_new (NUM_COLUMNS, G_TYPE_STRING, G_TYPE_STRING, G_TYPE_POINTER, G_TYPE_INT);
Jonathan Blandford's avatar
Jonathan Blandford committed
706 707
  tree_view = gtk_tree_view_new ();
  gtk_tree_view_set_model (GTK_TREE_VIEW (tree_view), GTK_TREE_MODEL (model));
708 709
  selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (tree_view));

710
  gtk_tree_selection_set_mode (GTK_TREE_SELECTION (selection),
711
			       GTK_SELECTION_BROWSE);
Havoc Pennington's avatar
Havoc Pennington committed
712
  gtk_widget_set_size_request (tree_view, 200, -1);
713

714 715 716 717
  /* this code only supports 1 level of children. If we
   * want more we probably have to use a recursing function.
   */
  while (d->title)
718
    {
719 720
      Demo *children = d->children;

721 722 723 724
      gtk_tree_store_append (GTK_TREE_STORE (model), &iter, NULL);

      gtk_tree_store_set (GTK_TREE_STORE (model),
			  &iter,
725 726 727
			  TITLE_COLUMN, d->title,
			  FILENAME_COLUMN, d->filename,
			  FUNC_COLUMN, d->func,
728
			  STYLE_COLUMN, PANGO_STYLE_NORMAL,
729
			  -1);
730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746

      d++;

      if (!children)
	continue;
      
      while (children->title)
	{
	  GtkTreeIter child_iter;

	  gtk_tree_store_append (GTK_TREE_STORE (model), &child_iter, &iter);
	  
	  gtk_tree_store_set (GTK_TREE_STORE (model),
			      &child_iter,
			      TITLE_COLUMN, children->title,
			      FILENAME_COLUMN, children->filename,
			      FUNC_COLUMN, children->func,
747
			      STYLE_COLUMN, PANGO_STYLE_NORMAL,
748 749 750 751
			      -1);
	  
	  children++;
	}
752 753 754
    }

  cell = gtk_cell_renderer_text_new ();
Havoc Pennington's avatar
Havoc Pennington committed
755

756
  column = gtk_tree_view_column_new_with_attributes ("Widget (double click for demo)",
757 758
						     cell,
						     "text", TITLE_COLUMN,
759
						     "style", STYLE_COLUMN,
760
						     NULL);
Havoc Pennington's avatar
Havoc Pennington committed
761
  
762 763 764
  gtk_tree_view_append_column (GTK_TREE_VIEW (tree_view),
			       GTK_TREE_VIEW_COLUMN (column));

765 766 767
  gtk_tree_model_get_iter_first (GTK_TREE_MODEL (model), &iter);
  gtk_tree_selection_select_iter (GTK_TREE_SELECTION (selection), &iter);

768 769
  g_signal_connect (selection, "changed", G_CALLBACK (selection_cb), model);
  g_signal_connect (tree_view, "row_activated", G_CALLBACK (row_activated_cb), model);
770

771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786
  gtk_tree_view_collapse_all (GTK_TREE_VIEW (tree_view));
  gtk_tree_view_set_headers_visible (GTK_TREE_VIEW (tree_view), FALSE);
  				    
  scrolled_window = gtk_scrolled_window_new (NULL, NULL);
  gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scrolled_window),
				  GTK_POLICY_NEVER,
				  GTK_POLICY_AUTOMATIC);
  gtk_container_add (GTK_CONTAINER (scrolled_window), tree_view);

  label = gtk_label_new ("Widget (double click for demo)");

  box = gtk_notebook_new ();
  gtk_notebook_append_page (GTK_NOTEBOOK (box), scrolled_window, label);

  gtk_widget_grab_focus (tree_view);

787 788
   g_object_unref (model);

789
  return box;
790 791
}

Havoc Pennington's avatar
Havoc Pennington committed
792 793 794 795
static void
setup_default_icon (void)
{
  GdkPixbuf *pixbuf;
796 797 798 799
  char *filename;
  GError *err;

  err = NULL;
Havoc Pennington's avatar
Havoc Pennington committed
800

801 802 803
  pixbuf = NULL;
  filename = demo_find_file ("gtk-logo-rgb.gif", &err);
  if (filename)
Havoc Pennington's avatar
Havoc Pennington committed
804
    {
805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825
      pixbuf = gdk_pixbuf_new_from_file (filename, &err);
      g_free (filename);
    }

  /* Ignoring this error (passing NULL instead of &err above)
   * would probably be reasonable for most apps.  We're just
   * showing off.
   */
  if (err)
    {
      GtkWidget *dialog;
      
      dialog = gtk_message_dialog_new (NULL, 0,
				       GTK_MESSAGE_ERROR,
				       GTK_BUTTONS_CLOSE,
				       "Failed to read icon file: %s",
				       err->message);
      g_error_free (err);

      g_signal_connect (dialog, "response",
			G_CALLBACK (gtk_widget_destroy), NULL);
Havoc Pennington's avatar
Havoc Pennington committed
826 827 828 829 830
    }

  if (pixbuf)
    {
      GList *list;      
831 832 833 834
      GdkPixbuf *transparent;

      /* The gtk-logo-rgb icon has a white background, make it transparent */
      transparent = gdk_pixbuf_add_alpha (pixbuf, TRUE, 0xff, 0xff, 0xff);
Havoc Pennington's avatar
Havoc Pennington committed
835 836

      list = NULL;
837
      list = g_list_append (list, transparent);
Havoc Pennington's avatar
Havoc Pennington committed
838 839
      gtk_window_set_default_icon_list (list);
      g_list_free (list);
Manish Singh's avatar
Manish Singh committed
840 841
      g_object_unref (pixbuf);
      g_object_unref (transparent);
Havoc Pennington's avatar
Havoc Pennington committed
842 843 844
    }
}

845 846 847 848 849 850
int
main (int argc, char **argv)
{
  GtkWidget *window;
  GtkWidget *notebook;
  GtkWidget *hbox;
851
  GtkWidget *tree;
852 853
  GtkTextTag *tag;

Havoc Pennington's avatar
Havoc Pennington committed
854 855 856 857
  /* Most code in gtk-demo is intended to be exemplary, but not
   * these few lines, which are just a hack so gtk-demo will work
   * in the GTK tree without installing it.
   */
858
  if (g_file_test ("../../gdk-pixbuf/libpixbufloader-pnm.la",
Havoc Pennington's avatar
Havoc Pennington committed
859 860
                   G_FILE_TEST_EXISTS))
    {
861 862
      g_setenv ("GDK_PIXBUF_MODULE_FILE", "../../gdk-pixbuf/gdk-pixbuf.loaders", TRUE);
      g_setenv ("GTK_IM_MODULE_FILE", "../../modules/input/gtk.immodules", TRUE);
Havoc Pennington's avatar
Havoc Pennington committed
863 864 865
    }
  /* -- End of hack -- */
  
866
  gtk_init (&argc, &argv);
Havoc Pennington's avatar
Havoc Pennington committed
867 868

  setup_default_icon ();
869 870
  
  window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
871
  gtk_window_set_title (GTK_WINDOW (window), "GTK+ Code Demos");
872
  g_signal_connect_after (window, "destroy",
873
		    G_CALLBACK (gtk_main_quit), NULL);
874 875 876 877

  hbox = gtk_hbox_new (FALSE, 0);
  gtk_container_add (GTK_CONTAINER (window), hbox);

878 879
  tree = create_tree ();
  gtk_box_pack_start (GTK_BOX (hbox), tree, FALSE, FALSE, 0);
880 881 882 883 884 885

  notebook = gtk_notebook_new ();
  gtk_box_pack_start (GTK_BOX (hbox), notebook, TRUE, TRUE, 0);

  gtk_notebook_append_page (GTK_NOTEBOOK (notebook),
			    create_text (&info_buffer, FALSE),
886
			    gtk_label_new_with_mnemonic ("_Info"));
887

888 889 890 891 892
  tag = gtk_text_buffer_create_tag (info_buffer, "title",
                                    "font", "Sans 18",
                                    NULL);
   g_object_unref (info_buffer);

893 894
  gtk_notebook_append_page (GTK_NOTEBOOK (notebook),
			    create_text (&source_buffer, TRUE),
895
			    gtk_label_new_with_mnemonic ("_Source"));
896

897

Havoc Pennington's avatar
Havoc Pennington committed
898
  tag = gtk_text_buffer_create_tag (source_buffer, "comment",
899
				    "foreground", "DodgerBlue",
900
                                    NULL);
Havoc Pennington's avatar
Havoc Pennington committed
901 902 903 904 905
  tag = gtk_text_buffer_create_tag (source_buffer, "type",
				    "foreground", "ForestGreen",
                                    NULL);
  tag = gtk_text_buffer_create_tag (source_buffer, "string",
				    "foreground", "RosyBrown",
906 907
				    "weight", PANGO_WEIGHT_BOLD,
                                    NULL);
Havoc Pennington's avatar
Havoc Pennington committed
908
  tag = gtk_text_buffer_create_tag (source_buffer, "control",
909 910
				    "foreground", "purple",
                                    NULL);
Havoc Pennington's avatar
Havoc Pennington committed
911
  tag = gtk_text_buffer_create_tag (source_buffer, "preprocessor",
912 913 914
				    "style", PANGO_STYLE_OBLIQUE,
 				    "foreground", "burlywood4",
                                    NULL);
Havoc Pennington's avatar
Havoc Pennington committed
915
  tag = gtk_text_buffer_create_tag (source_buffer, "function",
916 917
				    "weight", PANGO_WEIGHT_BOLD,
 				    "foreground", "DarkGoldenrod4",
918
                                    NULL);
919
   g_object_unref (source_buffer);
Havoc Pennington's avatar
Havoc Pennington committed
920
  
921 922 923 924
  gtk_window_set_default_size (GTK_WINDOW (window), 600, 400);
  gtk_widget_show_all (window);
  

925
  load_file (testgtk_demos[0].filename);
926 927 928 929 930
  
  gtk_main ();

  return 0;
}