main.c 22 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
#include "demos.h"
11 12 13 14

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

#undef DEMOCODEDIR

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

  if (result == NULL)
    {
44
      result = g_win32_get_package_installation_directory_of_module (NULL);
45 46 47 48 49 50 51 52 53 54 55 56 57
      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
};

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

static gchar *types[] =
{
  "static",
  "const ",
  "void",
  "gint",
Matthias Clasen's avatar
Matthias Clasen committed
198 199
  " int ",
  " char ",
200 201 202
  "gchar ",
  "gfloat",
  "float",
Matthias Clasen's avatar
Matthias Clasen committed
203
  "double",
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
  "gint8",
  "gint16",
  "gint32",
  "guint",
  "guint8",
  "guint16",
  "guint32",
  "guchar",
  "glong",
  "gboolean" ,
  "gshort",
  "gushort",
  "gulong",
  "gdouble",
  "gldouble",
  "gpointer",
  "NULL",
  "GList",
  "GSList",
  "FALSE",
  "TRUE",
  "FILE ",
  "GtkColorSelection ",
  "GtkWidget ",
  "GtkButton ",
  "GdkColor ",
  "GdkRectangle ",
  "GdkEventExpose ",
  "GdkGC ",
  "GdkPixbufLoader ",
  "GdkPixbuf ",
  "GError",
  "size_t",
Matthias Clasen's avatar
Matthias Clasen committed
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
  "GtkAboutDialog ",
  "GtkAction ",
  "GtkActionEntry ",
  "GtkRadioActionEntry ",
  "GtkIconFactory ",
  "GtkStockItem ",
  "GtkIconSet ",
  "GtkTextBuffer ",
  "GtkStatusbar ",
  "GtkTextIter ",
  "GtkTextMark ",
  "GdkEventWindowState ",
  "GtkActionGroup ",
  "GtkUIManager ",
  "GtkRadioAction ",
  "GtkActionClass ",
  "GtkToggleActionEntry ",
  "GtkAssistant ",
  "GtkBuilder ",
  "GtkSizeGroup ",
  "GtkTreeModel ",
  "GtkTreeSelection ",
  "GdkDisplay ",
  "GdkScreen ",
  "GdkWindow ",
  "GdkEventButton ",
  "GdkCursor ",
  "GtkTreeIter ",
  "GtkTreeViewColumn ",
  "GdkDisplayManager ",
  "GtkClipboard ",
  "GtkIconSize ",
  "GtkImage ",
  "GdkDragContext ",
  "GtkSelectionData ",
  "GtkDialog ",
  "GtkMenuItem ",
  "GtkListStore ",
  "GtkCellLayout ",
  "GtkCellRenderer ",
  "GtkTreePath ",
  "GtkTreeStore ",
  "GtkEntry ",
  "GtkEditable ",
281
  "GtkEditableInterface ",
Matthias Clasen's avatar
Matthias Clasen committed
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
  "GdkPixmap ",
  "GdkEventConfigure ",
  "GdkEventMotion ",
  "GdkModifierType ",
  "GtkEntryCompletion ",
  "GtkToolItem ",
  "GDir ",
  "GtkIconView ",
  "GtkCellRendererText ",
  "GtkContainer ",
  "GtkAccelGroup ",
  "GtkPaned ",
  "GtkPrintOperation ",
  "GtkPrintContext ",
  "cairo_t ",
  "PangoLayout "
  "PangoFontDescription ",
  "PangoRenderer ",
  "PangoMatrix ",
  "PangoContext ",
  "PangoLayout ",
  "GtkTable ",
  "GtkToggleButton ",
  "GString ",
  "GtkIconSize ",
  "GtkTreeView ",
  "GtkTextTag ",
  "GdkEvent ",
  "GdkEventKey ",
  "GtkTextView ",
  "GdkEventVisibility ",
  "GdkBitmap ",
  "GtkTextChildAnchor ",
  "GArray ",
  "GtkCellEditable ",
  "GtkCellRendererToggle ",
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
  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++)
Matthias Clasen's avatar
Matthias Clasen committed
392 393
    if (!strncmp (text, types[i], strlen (types[i])) ||
        (start && types[i][0] == ' ' && !strncmp (text, types[i] + 1, strlen (types[i]) - 1)))
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 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471
      {
	*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
472
fontify (void)
473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504
{
  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
505
	    gtk_text_buffer_apply_tag_by_name (source_buffer, tag, &start_iter, &tmp_iter);
506 507 508 509 510 511 512 513 514 515 516

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

      g_free (text);
      start_iter = next_iter;
    }
}

517
void
518
load_file (const gchar *filename)
519 520 521
{
  FILE *file;
  GtkTextIter start, end;
522 523
  char *full_filename;
  GError *err = NULL;
524 525 526 527
  GString *buffer = g_string_new (NULL);
  int state = 0;
  gboolean in_para = 0;

528
  if (current_file && !strcmp (current_file, filename))
529 530 531 532
    {
      g_string_free (buffer, TRUE);
      return;
    }
533

534 535
  g_free (current_file);
  current_file = g_strdup (filename);
536 537 538 539 540 541 542
  
  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);

543 544 545 546 547 548 549 550
  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
551
  file = g_fopen (full_filename, "r");
552 553

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

556
  g_free (full_filename);
557

558
  if (!file)
559
    return;
560 561 562 563 564 565

  gtk_text_buffer_get_iter_at_offset (info_buffer, &start, 0);
  while (read_line (file, buffer))
    {
      gchar *p = buffer->str;
      gchar *q;
566
      gchar *r;
567 568 569 570 571
      
      switch (state)
	{
	case 0:
	  /* Reading title */
572
	  while (*p == '/' || *p == '*' || g_ascii_isspace (*p))
573
	    p++;
574 575 576 577 578
	  r = p;
	  while (*r != '/' && strlen (r))
	    r++;
	  if (strlen (r) > 0)
	    p = r + 1;
579
	  q = p + strlen (p);
580
	  while (q > p && g_ascii_isspace (*(q - 1)))
581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603
	    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 */
604
	  while (g_ascii_isspace (*p))
605 606 607 608 609 610 611 612 613 614
	    p++;
	  if (*p == '*' && *(p + 1) == '/')
	    {
	      gtk_text_buffer_get_iter_at_offset (source_buffer, &start, 0);
	      state++;
	    }
	  else
	    {
	      int len;
	      
615
	      while (*p == '*' || g_ascii_isspace (*p))
616 617 618
		p++;

	      len = strlen (p);
619
	      while (g_ascii_isspace (*(p + len - 1)))
620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640
		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 */
641
	  while (g_ascii_isspace (*p))
642 643 644 645 646 647 648 649 650 651 652 653 654
	    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);
655
	  gtk_text_buffer_insert (source_buffer, &start, "\n", 1);
656 657 658 659
	  break;
	}
    }

660 661
  fclose (file);
  
662
  fontify ();
663 664

  g_string_free (buffer, TRUE);
665 666
}

667
void
668
row_activated_cb (GtkTreeView       *tree_view,
669 670
                  GtkTreePath       *path,
		  GtkTreeViewColumn *column)
671 672
{
  GtkTreeIter iter;
673
  PangoStyle style;
674 675
  GDoDemoFunc func;
  GtkWidget *window;
676 677 678
  GtkTreeModel *model;

  model = gtk_tree_view_get_model (tree_view);
679 680 681 682 683
  
  gtk_tree_model_get_iter (model, &iter, path);
  gtk_tree_model_get (GTK_TREE_MODEL (model),
		      &iter,
		      FUNC_COLUMN, &func,
684
		      STYLE_COLUMN, &style,
685
		      -1);
686

687
  if (func)
688
    {
689 690
      gtk_tree_store_set (GTK_TREE_STORE (model),
			  &iter,
691
			  STYLE_COLUMN, (style == PANGO_STYLE_ITALIC ? PANGO_STYLE_NORMAL : PANGO_STYLE_ITALIC),
692
			  -1);
693
      window = (func) (gtk_widget_get_toplevel (GTK_WIDGET (tree_view)));
694
      
695 696 697 698 699 700 701 702
      if (window != NULL)
	{
	  CallbackData *cbdata;
	  
	  cbdata = g_new (CallbackData, 1);
	  cbdata->model = model;
	  cbdata->path = gtk_tree_path_copy (path);
	  
703 704
	  g_signal_connect (window, "destroy",
			    G_CALLBACK (window_closed_cb), cbdata);
705
	}
706 707 708
    }
}

709 710 711
static void
selection_cb (GtkTreeSelection *selection,
	      GtkTreeModel     *model)
712
{
713 714 715
  GtkTreeIter iter;
  GValue value = {0, };

716
  if (! gtk_tree_selection_get_selected (selection, NULL, &iter))
717 718
    return;

719 720 721
  gtk_tree_model_get_value (model, &iter,
			    FILENAME_COLUMN,
			    &value);
722 723
  if (g_value_get_string (&value))
    load_file (g_value_get_string (&value));
724
  g_value_unset (&value);
725 726
}

727
static GtkWidget *
728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748
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
749 750
  gtk_container_add (GTK_CONTAINER (scrolled_window), text_view);
  
751 752
  if (is_source)
    {
753
      font_desc = pango_font_description_from_string ("monospace");
754 755
      gtk_widget_modify_font (text_view, font_desc);
      pango_font_description_free (font_desc);
756 757 758 759 760 761 762 763 764 765 766 767 768

      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);
769 770 771 772 773
    }
  
  return scrolled_window;
}

774 775 776 777 778 779
static GtkWidget *
create_tree (void)
{
  GtkTreeSelection *selection;
  GtkCellRenderer *cell;
  GtkWidget *tree_view;
780 781
  GtkTreeViewColumn *column;
  GtkTreeStore *model;
782
  GtkTreeIter iter;
783
  GtkWidget *box, *label, *scrolled_window;
784 785

  Demo *d = testgtk_demos;
786

787
  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
788 789
  tree_view = gtk_tree_view_new ();
  gtk_tree_view_set_model (GTK_TREE_VIEW (tree_view), GTK_TREE_MODEL (model));
790 791
  selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (tree_view));

792
  gtk_tree_selection_set_mode (GTK_TREE_SELECTION (selection),
793
			       GTK_SELECTION_BROWSE);
Havoc Pennington's avatar
Havoc Pennington committed
794
  gtk_widget_set_size_request (tree_view, 200, -1);
795

796 797 798 799
  /* this code only supports 1 level of children. If we
   * want more we probably have to use a recursing function.
   */
  while (d->title)
800
    {
801 802
      Demo *children = d->children;

803 804 805 806
      gtk_tree_store_append (GTK_TREE_STORE (model), &iter, NULL);

      gtk_tree_store_set (GTK_TREE_STORE (model),
			  &iter,
807 808 809
			  TITLE_COLUMN, d->title,
			  FILENAME_COLUMN, d->filename,
			  FUNC_COLUMN, d->func,
810
			  STYLE_COLUMN, PANGO_STYLE_NORMAL,
811
			  -1);
812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828

      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,
829
			      STYLE_COLUMN, PANGO_STYLE_NORMAL,
830 831 832 833
			      -1);
	  
	  children++;
	}
834 835 836
    }

  cell = gtk_cell_renderer_text_new ();
837

838
  column = gtk_tree_view_column_new_with_attributes ("Widget (double click for demo)",
839 840
						     cell,
						     "text", TITLE_COLUMN,
841
						     "style", STYLE_COLUMN,
842
						     NULL);
843
  
844 845 846
  gtk_tree_view_append_column (GTK_TREE_VIEW (tree_view),
			       GTK_TREE_VIEW_COLUMN (column));

847 848 849
  gtk_tree_model_get_iter_first (GTK_TREE_MODEL (model), &iter);
  gtk_tree_selection_select_iter (GTK_TREE_SELECTION (selection), &iter);

850 851
  g_signal_connect (selection, "changed", G_CALLBACK (selection_cb), model);
  g_signal_connect (tree_view, "row_activated", G_CALLBACK (row_activated_cb), model);
852

853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868
  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);

869 870
   g_object_unref (model);

871
  return box;
872 873
}

Havoc Pennington's avatar
Havoc Pennington committed
874 875 876 877
static void
setup_default_icon (void)
{
  GdkPixbuf *pixbuf;
878 879 880 881
  char *filename;
  GError *err;

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

883 884 885
  pixbuf = NULL;
  filename = demo_find_file ("gtk-logo-rgb.gif", &err);
  if (filename)
Havoc Pennington's avatar
Havoc Pennington committed
886
    {
887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907
      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
908 909 910 911 912
    }

  if (pixbuf)
    {
      GList *list;      
913 914 915 916
      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
917 918

      list = NULL;
919
      list = g_list_append (list, transparent);
Havoc Pennington's avatar
Havoc Pennington committed
920 921
      gtk_window_set_default_icon_list (list);
      g_list_free (list);
922 923
      g_object_unref (pixbuf);
      g_object_unref (transparent);
Havoc Pennington's avatar
Havoc Pennington committed
924 925 926
    }
}

927 928 929 930 931 932
int
main (int argc, char **argv)
{
  GtkWidget *window;
  GtkWidget *notebook;
  GtkWidget *hbox;
933
  GtkWidget *tree;
934 935
  GtkTextTag *tag;

Havoc Pennington's avatar
Havoc Pennington committed
936 937 938 939
  /* 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.
   */
940
  if (g_file_test ("../../gdk-pixbuf/libpixbufloader-pnm.la",
Havoc Pennington's avatar
Havoc Pennington committed
941 942
                   G_FILE_TEST_EXISTS))
    {
943 944
      g_setenv ("GDK_PIXBUF_MODULE_FILE", "../../gdk-pixbuf/loaders.cache", TRUE);
      g_setenv ("GTK_IM_MODULE_FILE", "../../modules/input/immodules.cache", TRUE);
Havoc Pennington's avatar
Havoc Pennington committed
945 946 947
    }
  /* -- End of hack -- */
  
948
  gtk_init (&argc, &argv);
Havoc Pennington's avatar
Havoc Pennington committed
949 950

  setup_default_icon ();
951 952
  
  window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
953
  gtk_window_set_title (GTK_WINDOW (window), "GTK+ Code Demos");
954
  g_signal_connect_after (window, "destroy",
955
		    G_CALLBACK (gtk_main_quit), NULL);
956

957
  hbox = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 0);
958 959
  gtk_container_add (GTK_CONTAINER (window), hbox);

960 961
  tree = create_tree ();
  gtk_box_pack_start (GTK_BOX (hbox), tree, FALSE, FALSE, 0);
962 963 964 965 966 967

  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),
968
			    gtk_label_new_with_mnemonic ("_Info"));
969

970 971 972 973 974
  tag = gtk_text_buffer_create_tag (info_buffer, "title",
                                    "font", "Sans 18",
                                    NULL);
   g_object_unref (info_buffer);

975 976
  gtk_notebook_append_page (GTK_NOTEBOOK (notebook),
			    create_text (&source_buffer, TRUE),
977
			    gtk_label_new_with_mnemonic ("_Source"));
978

979

Havoc Pennington's avatar
Havoc Pennington committed
980
  tag = gtk_text_buffer_create_tag (source_buffer, "comment",
981
				    "foreground", "DodgerBlue",
982
                                    NULL);
Havoc Pennington's avatar
Havoc Pennington committed
983 984 985 986 987
  tag = gtk_text_buffer_create_tag (source_buffer, "type",
				    "foreground", "ForestGreen",
                                    NULL);
  tag = gtk_text_buffer_create_tag (source_buffer, "string",
				    "foreground", "RosyBrown",
988 989
				    "weight", PANGO_WEIGHT_BOLD,
                                    NULL);
Havoc Pennington's avatar
Havoc Pennington committed
990
  tag = gtk_text_buffer_create_tag (source_buffer, "control",
991 992
				    "foreground", "purple",
                                    NULL);
Havoc Pennington's avatar
Havoc Pennington committed
993
  tag = gtk_text_buffer_create_tag (source_buffer, "preprocessor",
994 995 996
				    "style", PANGO_STYLE_OBLIQUE,
 				    "foreground", "burlywood4",
                                    NULL);
Havoc Pennington's avatar
Havoc Pennington committed
997
  tag = gtk_text_buffer_create_tag (source_buffer, "function",
998 999
				    "weight", PANGO_WEIGHT_BOLD,
 				    "foreground", "DarkGoldenrod4",
1000
                                    NULL);
1001
   g_object_unref (source_buffer);
Havoc Pennington's avatar
Havoc Pennington committed
1002
  
1003 1004 1005 1006
  gtk_window_set_default_size (GTK_WINDOW (window), 600, 400);
  gtk_widget_show_all (window);
  

1007
  load_file (testgtk_demos[0].filename);
1008 1009 1010 1011 1012
  
  gtk_main ();

  return 0;
}