testgtk.c 302 KB
Newer Older
Cody Russell's avatar
Cody Russell committed
1
/* GTK - The GIMP Toolkit
Emmanuele Bassi's avatar
Emmanuele Bassi committed
2 3 4 5 6 7 8 9 10 11 12 13 14
 * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
Javier Jardón's avatar
Javier Jardón committed
15
 * License along with this library. If not, see <http://www.gnu.org/licenses/>.
Emmanuele Bassi's avatar
Emmanuele Bassi committed
16
 */
17

Emmanuele Bassi's avatar
Emmanuele Bassi committed
18 19 20 21 22 23 24 25
/*
 * Modified by the GTK+ Team and others 1997-2000.  See the AUTHORS
 * file for a list of people on the GTK+ Team.  See the ChangeLog
 * files for a list of changes.  These files are distributed with
 * GTK+ at ftp://ftp.gtk.org/pub/gtk/. 
 */


26
#include "config.h"
Emmanuele Bassi's avatar
Emmanuele Bassi committed
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

#undef	G_LOG_DOMAIN

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <math.h>
#include <time.h>
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif

#include "gtk/gtk.h"
#include "gdk/gdk.h"
#include "gdk/gdkkeysyms.h"

#ifdef G_OS_WIN32
#define sleep(n) _sleep(n)
#endif

#include "prop-editor.h"

#include "test.xpm"

gboolean
file_exists (const char *filename)
{
  struct stat statbuf;

  return stat (filename, &statbuf) == 0;
}

GtkWidget *
shape_create_icon (GdkScreen *screen,
		   char      *xpm_file,
		   gint       x,
		   gint       y,
		   gint       px,
		   gint       py,
		   gint       window_type);

static GtkWidget *
build_option_menu (gchar           *items[],
		   gint             num_items,
		   gint             history,
		   void           (*func) (GtkWidget *widget, gpointer data),
		   gpointer         data);

/* macro, structure and variables used by tree window demos */
#define DEFAULT_NUMBER_OF_ITEM  3
#define DEFAULT_RECURSION_LEVEL 3

struct {
  GSList* selection_mode_group;
  GtkWidget* single_button;
  GtkWidget* browse_button;
  GtkWidget* multiple_button;
  GtkWidget* draw_line_button;
  GtkWidget* view_line_button;
  GtkWidget* no_root_item_button;
  GtkWidget* nb_item_spinner;
  GtkWidget* recursion_spinner;
} sTreeSampleSelection;

typedef struct sTreeButtons {
  guint nb_item_add;
  GtkWidget* add_button;
  GtkWidget* remove_button;
  GtkWidget* subtree_button;
} sTreeButtons;
/* end of tree section */

static GtkWidget *
build_option_menu (gchar           *items[],
		   gint             num_items,
		   gint             history,
		   void           (*func)(GtkWidget *widget, gpointer data),
		   gpointer         data)
{
  GtkWidget *omenu;
  gint i;

110
  omenu = gtk_combo_box_text_new ();
Emmanuele Bassi's avatar
Emmanuele Bassi committed
111 112 113 114
  g_signal_connect (omenu, "changed",
		    G_CALLBACK (func), data);
      
  for (i = 0; i < num_items; i++)
115
      gtk_combo_box_text_append_text (GTK_COMBO_BOX_TEXT (omenu), items[i]);
Emmanuele Bassi's avatar
Emmanuele Bassi committed
116

117
  gtk_combo_box_set_active (GTK_COMBO_BOX (omenu), history);
Emmanuele Bassi's avatar
Emmanuele Bassi committed
118 119 120 121 122 123 124 125 126 127
  
  return omenu;
}

/*
 * Windows with an alpha channel
 */


static gboolean
128 129
on_alpha_window_draw (GtkWidget *widget,
                      cairo_t   *cr)
Emmanuele Bassi's avatar
Emmanuele Bassi committed
130 131
{
  cairo_pattern_t *pattern;
132
  int radius, width, height;
Emmanuele Bassi's avatar
Emmanuele Bassi committed
133

134 135 136 137 138
  width = gtk_widget_get_allocated_width (widget);
  height = gtk_widget_get_allocated_height (widget);
  radius = MIN (width, height) / 2;
  pattern = cairo_pattern_create_radial (width / 2,
                                         height / 2,
Emmanuele Bassi's avatar
Emmanuele Bassi committed
139
					 0.0,
140 141
                                         width / 2,
                                         height / 2,
Emmanuele Bassi's avatar
Emmanuele Bassi committed
142 143
					 radius * 1.33);

144
  if (gdk_screen_get_rgba_visual (gtk_widget_get_screen (widget)) &&
Emmanuele Bassi's avatar
Emmanuele Bassi committed
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
      gtk_widget_is_composited (widget))
    cairo_set_source_rgba (cr, 1.0, 1.0, 1.0, 0.0); /* transparent */
  else
    cairo_set_source_rgb (cr, 1.0, 1.0, 1.0); /* opaque white */
    
  cairo_set_operator (cr, CAIRO_OPERATOR_SOURCE);
  cairo_paint (cr);
  
  cairo_pattern_add_color_stop_rgba (pattern, 0.0,
				     1.0, 0.75, 0.0, 1.0); /* solid orange */
  cairo_pattern_add_color_stop_rgba (pattern, 1.0,
				     1.0, 0.75, 0.0, 0.0); /* transparent orange */

  cairo_set_source (cr, pattern);
  cairo_pattern_destroy (pattern);
  
  cairo_set_operator (cr, CAIRO_OPERATOR_OVER);
  cairo_paint (cr);

  return FALSE;
}

static GtkWidget *
build_alpha_widgets (void)
{
170
  GtkWidget *grid;
Emmanuele Bassi's avatar
Emmanuele Bassi committed
171
  GtkWidget *radio_button;
172
  GtkWidget *check_button;
Emmanuele Bassi's avatar
Emmanuele Bassi committed
173 174 175 176
  GtkWidget *hbox;
  GtkWidget *label;
  GtkWidget *entry;

177
  grid = gtk_grid_new ();
Emmanuele Bassi's avatar
Emmanuele Bassi committed
178 179

  radio_button = gtk_radio_button_new_with_label (NULL, "Red");
180 181
  gtk_widget_set_hexpand (radio_button, TRUE);
  gtk_grid_attach (GTK_GRID (grid), radio_button, 0, 0, 1, 1);
Emmanuele Bassi's avatar
Emmanuele Bassi committed
182 183

  radio_button = gtk_radio_button_new_with_label_from_widget (GTK_RADIO_BUTTON (radio_button), "Green");
184 185
  gtk_widget_set_hexpand (radio_button, TRUE);
  gtk_grid_attach (GTK_GRID (grid), radio_button, 0, 1, 1, 1);
Emmanuele Bassi's avatar
Emmanuele Bassi committed
186 187

  radio_button = gtk_radio_button_new_with_label_from_widget (GTK_RADIO_BUTTON (radio_button), "Blue"),
188 189
  gtk_widget_set_hexpand (radio_button, TRUE);
  gtk_grid_attach (GTK_GRID (grid), radio_button, 0, 2, 1, 1);
Emmanuele Bassi's avatar
Emmanuele Bassi committed
190

191 192 193 194 195 196 197 198 199 200 201
  check_button = gtk_check_button_new_with_label ("Sedentary"),
  gtk_widget_set_hexpand (check_button, TRUE);
  gtk_grid_attach (GTK_GRID (grid), check_button, 1, 0, 1, 1);

  check_button = gtk_check_button_new_with_label ("Nocturnal"),
  gtk_widget_set_hexpand (check_button, TRUE);
  gtk_grid_attach (GTK_GRID (grid), check_button, 1, 1, 1, 1);

  check_button = gtk_check_button_new_with_label ("Compulsive"),
  gtk_widget_set_hexpand (check_button, TRUE);
  gtk_grid_attach (GTK_GRID (grid), check_button, 1, 2, 1, 1);
Emmanuele Bassi's avatar
Emmanuele Bassi committed
202

203
  hbox = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 0);
Emmanuele Bassi's avatar
Emmanuele Bassi committed
204 205 206 207 208
  label = gtk_label_new (NULL);
  gtk_label_set_markup (GTK_LABEL (label), "<i>Entry: </i>");
  gtk_box_pack_start (GTK_BOX (hbox), label, FALSE, FALSE, 0);
  entry = gtk_entry_new ();
  gtk_box_pack_start (GTK_BOX (hbox), entry, TRUE, TRUE, 0);
209 210
  gtk_widget_set_hexpand (hbox, TRUE);
  gtk_grid_attach (GTK_GRID (grid), hbox, 0, 3, 2, 1);
Emmanuele Bassi's avatar
Emmanuele Bassi committed
211
  
212
  return grid;
Emmanuele Bassi's avatar
Emmanuele Bassi committed
213 214 215
}

static void
216
on_alpha_screen_changed (GtkWindow *window,
Emmanuele Bassi's avatar
Emmanuele Bassi committed
217 218 219
			 GdkScreen *old_screen,
			 GtkWidget *label)
{
220 221
  GdkScreen *screen = gtk_widget_get_screen (GTK_WIDGET (window));
  GdkVisual *visual = gdk_screen_get_rgba_visual (screen);
Emmanuele Bassi's avatar
Emmanuele Bassi committed
222

223
  if (!visual)
Emmanuele Bassi's avatar
Emmanuele Bassi committed
224
    {
225
      visual = gdk_screen_get_system_visual (screen);
Emmanuele Bassi's avatar
Emmanuele Bassi committed
226 227 228 229 230 231 232
      gtk_label_set_markup (GTK_LABEL (label), "<b>Screen doesn't support alpha</b>");
    }
  else
    {
      gtk_label_set_markup (GTK_LABEL (label), "<b>Screen supports alpha</b>");
    }

233
  gtk_widget_set_visual (GTK_WIDGET (window), visual);
Emmanuele Bassi's avatar
Emmanuele Bassi committed
234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254
}

static void
on_composited_changed (GtkWidget *window,
		      GtkLabel *label)
{
  gboolean is_composited = gtk_widget_is_composited (window);

  if (is_composited)
    gtk_label_set_text (label, "Composited");
  else
    gtk_label_set_text (label, "Not composited");
}

void
create_alpha_window (GtkWidget *widget)
{
  static GtkWidget *window;

  if (!window)
    {
255
      GtkWidget *content_area;
Emmanuele Bassi's avatar
Emmanuele Bassi committed
256 257 258 259 260 261 262 263 264
      GtkWidget *vbox;
      GtkWidget *label;
      
      window = gtk_dialog_new_with_buttons ("Alpha Window",
					    GTK_WINDOW (gtk_widget_get_toplevel (widget)), 0,
					    GTK_STOCK_CLOSE, 0,
					    NULL);

      gtk_widget_set_app_paintable (window, TRUE);
265 266
      g_signal_connect (window, "draw",
			G_CALLBACK (on_alpha_window_draw), NULL);
267 268

      content_area = gtk_dialog_get_content_area (GTK_DIALOG (window));
Emmanuele Bassi's avatar
Emmanuele Bassi committed
269
      
270
      vbox = gtk_box_new (GTK_ORIENTATION_VERTICAL, 8);
Emmanuele Bassi's avatar
Emmanuele Bassi committed
271
      gtk_container_set_border_width (GTK_CONTAINER (vbox), 12);
272
      gtk_box_pack_start (GTK_BOX (content_area), vbox,
Emmanuele Bassi's avatar
Emmanuele Bassi committed
273 274 275 276
			  TRUE, TRUE, 0);

      label = gtk_label_new (NULL);
      gtk_box_pack_start (GTK_BOX (vbox), label, TRUE, TRUE, 0);
277
      on_alpha_screen_changed (GTK_WINDOW (window), NULL, label);
Emmanuele Bassi's avatar
Emmanuele Bassi committed
278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296
      g_signal_connect (window, "screen-changed",
			G_CALLBACK (on_alpha_screen_changed), label);
      
      label = gtk_label_new (NULL);
      gtk_box_pack_start (GTK_BOX (vbox), label, TRUE, TRUE, 0);
      on_composited_changed (window, GTK_LABEL (label));
      g_signal_connect (window, "composited_changed", G_CALLBACK (on_composited_changed), label);
      
      gtk_box_pack_start (GTK_BOX (vbox), build_alpha_widgets (), TRUE, TRUE, 0);

      g_signal_connect (window, "destroy",
			G_CALLBACK (gtk_widget_destroyed),
			&window);
      
      g_signal_connect (window, "response",
                        G_CALLBACK (gtk_widget_destroy),
                        NULL); 
    }

297
  if (!gtk_widget_get_visible (window))
Emmanuele Bassi's avatar
Emmanuele Bassi committed
298 299 300 301 302 303 304 305 306
    gtk_widget_show_all (window);
  else
    gtk_widget_destroy (window);
}

/*
 * Composited non-toplevel window
 */

307
/* The draw event handler for the event box.
Emmanuele Bassi's avatar
Emmanuele Bassi committed
308 309 310 311 312 313 314 315 316 317 318 319
 *
 * This function simply draws a transparency onto a widget on the area
 * for which it receives expose events.  This is intended to give the
 * event box a "transparent" background.
 *
 * In order for this to work properly, the widget must have an RGBA
 * colourmap.  The widget should also be set as app-paintable since it
 * doesn't make sense for GTK to draw a background if we are drawing it
 * (and because GTK might actually replace our transparency with its
 * default background colour).
 */
static gboolean
320 321
transparent_draw (GtkWidget *widget,
                  cairo_t   *cr)
Emmanuele Bassi's avatar
Emmanuele Bassi committed
322 323
{
  cairo_set_operator (cr, CAIRO_OPERATOR_CLEAR);
324
  cairo_paint (cr);
Emmanuele Bassi's avatar
Emmanuele Bassi committed
325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340

  return FALSE;
}

/* The expose event handler for the window.
 *
 * This function performs the actual compositing of the event box onto
 * the already-existing background of the window at 50% normal opacity.
 *
 * In this case we do not want app-paintable to be set on the widget
 * since we want it to draw its own (red) background.  Because of this,
 * however, we must ensure that we use g_signal_register_after so that
 * this handler is called after the red has been drawn.  If it was
 * called before then GTK would just blindly paint over our work.
 */
static gboolean
341 342
window_draw (GtkWidget *widget,
             cairo_t   *cr)
Emmanuele Bassi's avatar
Emmanuele Bassi committed
343
{
344
  GtkAllocation allocation;
Emmanuele Bassi's avatar
Emmanuele Bassi committed
345 346 347 348 349
  GtkWidget *child;

  /* get our child (in this case, the event box) */ 
  child = gtk_bin_get_child (GTK_BIN (widget));

350
  gtk_widget_get_allocation (child, &allocation);
Emmanuele Bassi's avatar
Emmanuele Bassi committed
351 352

  /* the source data is the (composited) event box */
353
  gdk_cairo_set_source_window (cr, gtk_widget_get_window (child),
354 355
                               allocation.x,
                               allocation.y);
Emmanuele Bassi's avatar
Emmanuele Bassi committed
356 357 358 359 360 361 362 363 364 365 366 367 368 369 370

  /* composite, with a 50% opacity */
  cairo_paint_with_alpha (cr, 0.5);

  return FALSE;
}

void
create_composited_window (GtkWidget *widget)
{
  static GtkWidget *window;

  if (!window)
    {
      GtkWidget *event, *button;
371
      GdkRGBA red;
Emmanuele Bassi's avatar
Emmanuele Bassi committed
372 373 374 375 376 377 378 379 380 381 382

      /* make the widgets */
      button = gtk_button_new_with_label ("A Button");
      event = gtk_event_box_new ();
      window = gtk_window_new (GTK_WINDOW_TOPLEVEL);

      g_signal_connect (window, "destroy",
                        G_CALLBACK (gtk_widget_destroyed),
                        &window);

      /* put a red background on the window */
383
      gdk_rgba_parse (&red, "red");
384
      gtk_widget_override_background_color (window, 0, &red);
Emmanuele Bassi's avatar
Emmanuele Bassi committed
385 386 387 388 389 390

      /* set our event box to have a fully-transparent background
       * drawn on it.  currently there is no way to simply tell gtk
       * that "transparency" is the background colour for a widget.
       */
      gtk_widget_set_app_paintable (GTK_WIDGET (event), TRUE);
391 392
      g_signal_connect (event, "draw",
                        G_CALLBACK (transparent_draw), NULL);
Emmanuele Bassi's avatar
Emmanuele Bassi committed
393 394 395 396 397 398 399 400 401 402 403 404

      /* put them inside one another */
      gtk_container_set_border_width (GTK_CONTAINER (window), 10);
      gtk_container_add (GTK_CONTAINER (window), event);
      gtk_container_add (GTK_CONTAINER (event), button);

      /* realise and show everything */
      gtk_widget_realize (button);

      /* set the event box GdkWindow to be composited.
       * obviously must be performed after event box is realised.
       */
405 406
      gdk_window_set_composited (gtk_widget_get_window (event),
                                 TRUE);
Emmanuele Bassi's avatar
Emmanuele Bassi committed
407 408 409 410 411

      /* set up the compositing handler.
       * note that we do _after so that the normal (red) background is drawn
       * by gtk before our compositing occurs.
       */
412 413
      g_signal_connect_after (window, "draw",
                              G_CALLBACK (window_draw), NULL);
Emmanuele Bassi's avatar
Emmanuele Bassi committed
414 415
    }

416
  if (!gtk_widget_get_visible (window))
Emmanuele Bassi's avatar
Emmanuele Bassi committed
417 418 419 420 421 422 423 424 425 426 427 428 429 430
    gtk_widget_show_all (window);
  else
    gtk_widget_destroy (window);
}

/*
 * Big windows and guffaw scrolling
 */

static void
pattern_set_bg (GtkWidget   *widget,
		GdkWindow   *child,
		gint         level)
{
431
  static GdkRGBA colors[] = {
432 433 434
    { 0.27, 0.27, 1.0, 1.0 },
    { 0.53, 0.53, 1.0, 1.0},
    { 0.67, 0.67, 1.0, 1.0 }
Emmanuele Bassi's avatar
Emmanuele Bassi committed
435 436 437
  };
    
  gdk_window_set_user_data (child, widget);
438
  gdk_window_set_background_rgba (child, &colors[level]);
Emmanuele Bassi's avatar
Emmanuele Bassi committed
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 472 473 474 475 476
}

static void
create_pattern (GtkWidget   *widget,
		GdkWindow   *parent,
		gint         level,
		gint         width,
		gint         height)
{
  gint h = 1;
  gint i = 0;
    
  GdkWindow *child;

  while (2 * h <= height)
    {
      gint w = 1;
      gint j = 0;
      
      while (2 * w <= width)
	{
	  if ((i + j) % 2 == 0)
	    {
	      gint x = w  - 1;
	      gint y = h - 1;
	      
	      GdkWindowAttr attributes;

	      attributes.window_type = GDK_WINDOW_CHILD;
	      attributes.x = x;
	      attributes.y = y;
	      attributes.width = w;
	      attributes.height = h;
	      attributes.wclass = GDK_INPUT_OUTPUT;
	      attributes.event_mask = GDK_EXPOSURE_MASK;
	      attributes.visual = gtk_widget_get_visual (widget);
	      
	      child = gdk_window_new (parent, &attributes,
477
				      GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL);
Emmanuele Bassi's avatar
Emmanuele Bassi committed
478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496

	      pattern_set_bg (widget, child, level);

	      if (level < 2)
		create_pattern (widget, child, level + 1, w, h);

	      gdk_window_show (child);
	    }
	  j++;
	  w *= 2;
	}
      i++;
      h *= 2;
    }
}

#define PATTERN_SIZE (1 << 18)

static void
497
pattern_hadj_changed (GtkAdjustment *adjustment,
Emmanuele Bassi's avatar
Emmanuele Bassi committed
498 499
		      GtkWidget     *darea)
{
500
  gint *old_value = g_object_get_data (G_OBJECT (adjustment), "old-value");
501
  gint new_value = gtk_adjustment_get_value (adjustment);
Emmanuele Bassi's avatar
Emmanuele Bassi committed
502

503
  if (gtk_widget_get_realized (darea))
Emmanuele Bassi's avatar
Emmanuele Bassi committed
504
    {
505 506
      gdk_window_scroll (gtk_widget_get_window (darea),
                         *old_value - new_value, 0);
Emmanuele Bassi's avatar
Emmanuele Bassi committed
507 508 509 510 511
      *old_value = new_value;
    }
}

static void
512
pattern_vadj_changed (GtkAdjustment *adjustment,
Emmanuele Bassi's avatar
Emmanuele Bassi committed
513 514
		      GtkWidget *darea)
{
515
  gint *old_value = g_object_get_data (G_OBJECT (adjustment), "old-value");
516
  gint new_value = gtk_adjustment_get_value (adjustment);
Emmanuele Bassi's avatar
Emmanuele Bassi committed
517

518
  if (gtk_widget_get_realized (darea))
Emmanuele Bassi's avatar
Emmanuele Bassi committed
519
    {
520 521
      gdk_window_scroll (gtk_widget_get_window (darea),
                         0, *old_value - new_value);
Emmanuele Bassi's avatar
Emmanuele Bassi committed
522 523 524 525 526 527 528 529
      *old_value = new_value;
    }
}

static void
pattern_realize (GtkWidget *widget,
		 gpointer   data)
{
530 531 532 533 534
  GdkWindow *window;

  window = gtk_widget_get_window (widget);
  pattern_set_bg (widget, window, 0);
  create_pattern (widget, window, 1, PATTERN_SIZE, PATTERN_SIZE);
Emmanuele Bassi's avatar
Emmanuele Bassi committed
535 536 537 538 539 540
}

static void 
create_big_windows (GtkWidget *widget)
{
  static GtkWidget *window = NULL;
541
  GtkWidget *content_area;
542
  GtkWidget *darea, *grid, *scrollbar;
Emmanuele Bassi's avatar
Emmanuele Bassi committed
543
  GtkWidget *eventbox;
544 545
  GtkAdjustment *hadjustment;
  GtkAdjustment *vadjustment;
Emmanuele Bassi's avatar
Emmanuele Bassi committed
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
  static gint current_x;
  static gint current_y;
 
  if (!window)
    {
      current_x = 0;
      current_y = 0;
      
      window = gtk_dialog_new_with_buttons ("Big Windows",
                                            NULL, 0,
                                            GTK_STOCK_CLOSE,
                                            GTK_RESPONSE_NONE,
                                            NULL);
 
      gtk_window_set_screen (GTK_WINDOW (window),
			     gtk_widget_get_screen (widget));

      gtk_window_set_default_size (GTK_WINDOW (window), 200, 300);

      g_signal_connect (window, "destroy",
			G_CALLBACK (gtk_widget_destroyed),
			&window);

      g_signal_connect (window, "response",
                        G_CALLBACK (gtk_widget_destroy),
                        NULL);

573 574
      content_area = gtk_dialog_get_content_area (GTK_DIALOG (window));

575 576
      grid = gtk_grid_new ();
      gtk_box_pack_start (GTK_BOX (content_area), grid, TRUE, TRUE, 0);
Emmanuele Bassi's avatar
Emmanuele Bassi committed
577 578 579

      darea = gtk_drawing_area_new ();

580 581
      hadjustment = gtk_adjustment_new (0, 0, PATTERN_SIZE, 10, 100, 100);
      g_signal_connect (hadjustment, "value_changed",
Emmanuele Bassi's avatar
Emmanuele Bassi committed
582
			G_CALLBACK (pattern_hadj_changed), darea);
583
      g_object_set_data (G_OBJECT (hadjustment), "old-value", &current_x);
Javier Jardón's avatar
Javier Jardón committed
584

585 586
      vadjustment = gtk_adjustment_new (0, 0, PATTERN_SIZE, 10, 100, 100);
      g_signal_connect (vadjustment, "value_changed",
Emmanuele Bassi's avatar
Emmanuele Bassi committed
587
			G_CALLBACK (pattern_vadj_changed), darea);
588
      g_object_set_data (G_OBJECT (vadjustment), "old-value", &current_y);
Emmanuele Bassi's avatar
Emmanuele Bassi committed
589 590 591 592 593 594
      
      g_signal_connect (darea, "realize",
                        G_CALLBACK (pattern_realize),
                        NULL);

      eventbox = gtk_event_box_new ();
595 596 597
      gtk_widget_set_hexpand (eventbox, TRUE);
      gtk_widget_set_vexpand (eventbox, TRUE);
      gtk_grid_attach (GTK_GRID (grid), eventbox, 0, 0, 1, 1);
Emmanuele Bassi's avatar
Emmanuele Bassi committed
598 599 600

      gtk_container_add (GTK_CONTAINER (eventbox), darea);

601
      scrollbar = gtk_scrollbar_new (GTK_ORIENTATION_HORIZONTAL, hadjustment);
602 603
      gtk_widget_set_hexpand (scrollbar, TRUE);
      gtk_grid_attach (GTK_GRID (grid), scrollbar, 0, 1, 1, 1);
Emmanuele Bassi's avatar
Emmanuele Bassi committed
604

605
      scrollbar = gtk_scrollbar_new (GTK_ORIENTATION_VERTICAL, vadjustment);
606 607
      gtk_widget_set_vexpand (scrollbar, TRUE);
      gtk_grid_attach (GTK_GRID (grid), scrollbar, 1, 0, 1, 1);
Emmanuele Bassi's avatar
Emmanuele Bassi committed
608 609
    }

610
  if (!gtk_widget_get_visible (window))
Emmanuele Bassi's avatar
Emmanuele Bassi committed
611 612 613 614 615 616 617 618 619 620 621 622 623
    gtk_widget_show_all (window);
  else
    gtk_widget_hide (window);
}

/*
 * GtkButton
 */

static void
button_window (GtkWidget *widget,
	       GtkWidget *button)
{
624
  if (!gtk_widget_get_visible (button))
Emmanuele Bassi's avatar
Emmanuele Bassi committed
625 626 627 628 629 630 631 632 633 634 635
    gtk_widget_show (button);
  else
    gtk_widget_hide (button);
}

static void
create_buttons (GtkWidget *widget)
{
  static GtkWidget *window = NULL;
  GtkWidget *box1;
  GtkWidget *box2;
636
  GtkWidget *grid;
Emmanuele Bassi's avatar
Emmanuele Bassi committed
637
  GtkWidget *separator;
638 639 640 641
  GtkWidget *button[10];
  int button_x[9] = { 0, 1, 2, 0, 2, 1, 1, 2, 0 };
  int button_y[9] = { 0, 1, 2, 2, 0, 2, 0, 1, 1 };
  guint i;
Emmanuele Bassi's avatar
Emmanuele Bassi committed
642 643 644 645 646 647 648 649 650 651 652 653 654 655

  if (!window)
    {
      window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
      gtk_window_set_screen (GTK_WINDOW (window),
			     gtk_widget_get_screen (widget));

      g_signal_connect (window, "destroy",
			G_CALLBACK (gtk_widget_destroyed),
			&window);

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

656
      box1 = gtk_box_new (GTK_ORIENTATION_VERTICAL, 0);
Emmanuele Bassi's avatar
Emmanuele Bassi committed
657 658
      gtk_container_add (GTK_CONTAINER (window), box1);

659 660 661 662 663
      grid = gtk_grid_new ();
      gtk_grid_set_row_spacing (GTK_GRID (grid), 5);
      gtk_grid_set_column_spacing (GTK_GRID (grid), 5);
      gtk_container_set_border_width (GTK_CONTAINER (grid), 10);
      gtk_box_pack_start (GTK_BOX (box1), grid, TRUE, TRUE, 0);
Emmanuele Bassi's avatar
Emmanuele Bassi committed
664 665 666 667 668 669 670 671 672 673 674

      button[0] = gtk_button_new_with_label ("button1");
      button[1] = gtk_button_new_with_mnemonic ("_button2");
      button[2] = gtk_button_new_with_mnemonic ("_button3");
      button[3] = gtk_button_new_from_stock (GTK_STOCK_OK);
      button[4] = gtk_button_new_with_label ("button5");
      button[5] = gtk_button_new_with_label ("button6");
      button[6] = gtk_button_new_with_label ("button7");
      button[7] = gtk_button_new_from_stock (GTK_STOCK_CLOSE);
      button[8] = gtk_button_new_with_label ("button9");
      
675 676 677 678 679
      for (i = 0; i < 9; i++)
        {
          g_signal_connect (button[i], "clicked",
                            G_CALLBACK (button_window),
                            button[(i + 1) % 9]);
680 681
          gtk_widget_set_hexpand (button[i], TRUE);
          gtk_widget_set_vexpand (button[i], TRUE);
682

683 684
          gtk_grid_attach (GTK_GRID (grid), button[i],
                           button_x[i], button_y[i] + 1, 1, 1);
685
        }
Emmanuele Bassi's avatar
Emmanuele Bassi committed
686

687
      separator = gtk_separator_new (GTK_ORIENTATION_HORIZONTAL);
Emmanuele Bassi's avatar
Emmanuele Bassi committed
688 689
      gtk_box_pack_start (GTK_BOX (box1), separator, FALSE, TRUE, 0);

690
      box2 = gtk_box_new (GTK_ORIENTATION_VERTICAL, 10);
Emmanuele Bassi's avatar
Emmanuele Bassi committed
691 692 693 694 695 696 697 698
      gtk_container_set_border_width (GTK_CONTAINER (box2), 10);
      gtk_box_pack_start (GTK_BOX (box1), box2, FALSE, TRUE, 0);

      button[9] = gtk_button_new_with_label ("close");
      g_signal_connect_swapped (button[9], "clicked",
				G_CALLBACK (gtk_widget_destroy),
				window);
      gtk_box_pack_start (GTK_BOX (box2), button[9], TRUE, TRUE, 0);
699
      gtk_widget_set_can_default (button[9], TRUE);
Emmanuele Bassi's avatar
Emmanuele Bassi committed
700 701 702
      gtk_widget_grab_default (button[9]);
    }

703
  if (!gtk_widget_get_visible (window))
Emmanuele Bassi's avatar
Emmanuele Bassi committed
704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734
    gtk_widget_show_all (window);
  else
    gtk_widget_destroy (window);
}

/*
 * GtkToggleButton
 */

static void
create_toggle_buttons (GtkWidget *widget)
{
  static GtkWidget *window = NULL;
  GtkWidget *box1;
  GtkWidget *box2;
  GtkWidget *button;
  GtkWidget *separator;

  if (!window)
    {
      window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
      gtk_window_set_screen (GTK_WINDOW (window),
			     gtk_widget_get_screen (widget));

      g_signal_connect (window, "destroy",
			G_CALLBACK (gtk_widget_destroyed),
			&window);

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

735
      box1 = gtk_box_new (GTK_ORIENTATION_VERTICAL, 0);
Emmanuele Bassi's avatar
Emmanuele Bassi committed
736 737
      gtk_container_add (GTK_CONTAINER (window), box1);

738
      box2 = gtk_box_new (GTK_ORIENTATION_VERTICAL, 10);
Emmanuele Bassi's avatar
Emmanuele Bassi committed
739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754
      gtk_container_set_border_width (GTK_CONTAINER (box2), 10);
      gtk_box_pack_start (GTK_BOX (box1), box2, TRUE, TRUE, 0);

      button = gtk_toggle_button_new_with_label ("button1");
      gtk_box_pack_start (GTK_BOX (box2), button, TRUE, TRUE, 0);

      button = gtk_toggle_button_new_with_label ("button2");
      gtk_box_pack_start (GTK_BOX (box2), button, TRUE, TRUE, 0);

      button = gtk_toggle_button_new_with_label ("button3");
      gtk_box_pack_start (GTK_BOX (box2), button, TRUE, TRUE, 0);

      button = gtk_toggle_button_new_with_label ("inconsistent");
      gtk_toggle_button_set_inconsistent (GTK_TOGGLE_BUTTON (button), TRUE);
      gtk_box_pack_start (GTK_BOX (box2), button, TRUE, TRUE, 0);
      
755
      separator = gtk_separator_new (GTK_ORIENTATION_HORIZONTAL);
Emmanuele Bassi's avatar
Emmanuele Bassi committed
756 757
      gtk_box_pack_start (GTK_BOX (box1), separator, FALSE, TRUE, 0);

758
      box2 = gtk_box_new (GTK_ORIENTATION_VERTICAL, 10);
Emmanuele Bassi's avatar
Emmanuele Bassi committed
759 760 761 762 763 764 765 766
      gtk_container_set_border_width (GTK_CONTAINER (box2), 10);
      gtk_box_pack_start (GTK_BOX (box1), box2, FALSE, TRUE, 0);

      button = gtk_button_new_with_label ("close");
      g_signal_connect_swapped (button, "clicked",
			        G_CALLBACK (gtk_widget_destroy),
				window);
      gtk_box_pack_start (GTK_BOX (box2), button, TRUE, TRUE, 0);
767
      gtk_widget_set_can_default (button, TRUE);
Emmanuele Bassi's avatar
Emmanuele Bassi committed
768 769 770
      gtk_widget_grab_default (button);
    }

771
  if (!gtk_widget_get_visible (window))
Emmanuele Bassi's avatar
Emmanuele Bassi committed
772 773 774 775 776 777 778 779
    gtk_widget_show_all (window);
  else
    gtk_widget_destroy (window);
}

static GtkWidget *
create_widget_grid (GType widget_type)
{
780
  GtkWidget *grid;
Emmanuele Bassi's avatar
Emmanuele Bassi committed
781 782 783
  GtkWidget *group_widget = NULL;
  gint i, j;
  
784
  grid = gtk_grid_new ();
Emmanuele Bassi's avatar
Emmanuele Bassi committed
785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822
  
  for (i = 0; i < 5; i++)
    {
      for (j = 0; j < 5; j++)
	{
	  GtkWidget *widget;
	  char *tmp;
	  
	  if (i == 0 && j == 0)
	    {
	      widget = NULL;
	    }
	  else if (i == 0)
	    {
	      tmp = g_strdup_printf ("%d", j);
	      widget = gtk_label_new (tmp);
	      g_free (tmp);
	    }
	  else if (j == 0)
	    {
	      tmp = g_strdup_printf ("%c", 'A' + i - 1);
	      widget = gtk_label_new (tmp);
	      g_free (tmp);
	    }
	  else
	    {
	      widget = g_object_new (widget_type, NULL);
	      
	      if (g_type_is_a (widget_type, GTK_TYPE_RADIO_BUTTON))
		{
		  if (!group_widget)
		    group_widget = widget;
		  else
		    g_object_set (widget, "group", group_widget, NULL);
		}
	    }
	  
	  if (widget)
823
	    gtk_grid_attach (GTK_GRID (grid), widget, i, j, 1, 1);
Emmanuele Bassi's avatar
Emmanuele Bassi committed
824 825 826
	}
    }

827
  return grid;
Emmanuele Bassi's avatar
Emmanuele Bassi committed
828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861
}

/*
 * GtkCheckButton
 */

static void
create_check_buttons (GtkWidget *widget)
{
  static GtkWidget *window = NULL;
  GtkWidget *box1;
  GtkWidget *box2;
  GtkWidget *button;
  GtkWidget *separator;
  GtkWidget *table;
  
  if (!window)
    {
      window = gtk_dialog_new_with_buttons ("Check Buttons",
                                            NULL, 0,
                                            GTK_STOCK_CLOSE,
                                            GTK_RESPONSE_NONE,
                                            NULL);

      gtk_window_set_screen (GTK_WINDOW (window), 
			     gtk_widget_get_screen (widget));

      g_signal_connect (window, "destroy",
			G_CALLBACK (gtk_widget_destroyed),
			&window);
      g_signal_connect (window, "response",
                        G_CALLBACK (gtk_widget_destroy),
                        NULL);

862
      box1 = gtk_dialog_get_content_area (GTK_DIALOG (window));
Emmanuele Bassi's avatar
Emmanuele Bassi committed
863
      
864
      box2 = gtk_box_new (GTK_ORIENTATION_VERTICAL, 10);
Emmanuele Bassi's avatar
Emmanuele Bassi committed
865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880
      gtk_container_set_border_width (GTK_CONTAINER (box2), 10);
      gtk_box_pack_start (GTK_BOX (box1), box2, TRUE, TRUE, 0);

      button = gtk_check_button_new_with_mnemonic ("_button1");
      gtk_box_pack_start (GTK_BOX (box2), button, TRUE, TRUE, 0);

      button = gtk_check_button_new_with_label ("button2");
      gtk_box_pack_start (GTK_BOX (box2), button, TRUE, TRUE, 0);

      button = gtk_check_button_new_with_label ("button3");
      gtk_box_pack_start (GTK_BOX (box2), button, TRUE, TRUE, 0);

      button = gtk_check_button_new_with_label ("inconsistent");
      gtk_toggle_button_set_inconsistent (GTK_TOGGLE_BUTTON (button), TRUE);
      gtk_box_pack_start (GTK_BOX (box2), button, TRUE, TRUE, 0);

881
      separator = gtk_separator_new (GTK_ORIENTATION_HORIZONTAL);
Emmanuele Bassi's avatar
Emmanuele Bassi committed
882 883 884 885 886 887 888
      gtk_box_pack_start (GTK_BOX (box1), separator, FALSE, TRUE, 0);

      table = create_widget_grid (GTK_TYPE_CHECK_BUTTON);
      gtk_container_set_border_width (GTK_CONTAINER (table), 10);
      gtk_box_pack_start (GTK_BOX (box1), table, TRUE, TRUE, 0);
    }

889
  if (!gtk_widget_get_visible (window))
Emmanuele Bassi's avatar
Emmanuele Bassi committed
890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926
    gtk_widget_show_all (window);
  else
    gtk_widget_destroy (window);
}

/*
 * GtkRadioButton
 */

static void
create_radio_buttons (GtkWidget *widget)
{
  static GtkWidget *window = NULL;
  GtkWidget *box1;
  GtkWidget *box2;
  GtkWidget *button;
  GtkWidget *separator;
  GtkWidget *table;

  if (!window)
    {
      window = gtk_dialog_new_with_buttons ("Radio Buttons",
                                            NULL, 0,
                                            GTK_STOCK_CLOSE,
                                            GTK_RESPONSE_NONE,
                                            NULL);

      gtk_window_set_screen (GTK_WINDOW (window),
			     gtk_widget_get_screen (widget));

      g_signal_connect (window, "destroy",
			G_CALLBACK (gtk_widget_destroyed),
			&window);
      g_signal_connect (window, "response",
                        G_CALLBACK (gtk_widget_destroy),
                        NULL);

927
      box1 = gtk_dialog_get_content_area (GTK_DIALOG (window));
Emmanuele Bassi's avatar
Emmanuele Bassi committed
928

929
      box2 = gtk_box_new (GTK_ORIENTATION_VERTICAL, 10);
Emmanuele Bassi's avatar
Emmanuele Bassi committed
930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952
      gtk_container_set_border_width (GTK_CONTAINER (box2), 10);
      gtk_box_pack_start (GTK_BOX (box1), box2, TRUE, TRUE, 0);

      button = gtk_radio_button_new_with_label (NULL, "button1");
      gtk_box_pack_start (GTK_BOX (box2), button, TRUE, TRUE, 0);

      button = gtk_radio_button_new_with_label (
	         gtk_radio_button_get_group (GTK_RADIO_BUTTON (button)),
		 "button2");
      gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (button), TRUE);
      gtk_box_pack_start (GTK_BOX (box2), button, TRUE, TRUE, 0);

      button = gtk_radio_button_new_with_label (
                 gtk_radio_button_get_group (GTK_RADIO_BUTTON (button)),
		 "button3");
      gtk_box_pack_start (GTK_BOX (box2), button, TRUE, TRUE, 0);

      button = gtk_radio_button_new_with_label (
                 gtk_radio_button_get_group (GTK_RADIO_BUTTON (button)),
		 "inconsistent");
      gtk_toggle_button_set_inconsistent (GTK_TOGGLE_BUTTON (button), TRUE);
      gtk_box_pack_start (GTK_BOX (box2), button, TRUE, TRUE, 0);
      
953
      separator = gtk_separator_new (GTK_ORIENTATION_HORIZONTAL);
Emmanuele Bassi's avatar
Emmanuele Bassi committed
954 955
      gtk_box_pack_start (GTK_BOX (box1), separator, FALSE, TRUE, 0);

956
      box2 = gtk_box_new (GTK_ORIENTATION_VERTICAL, 10);
Emmanuele Bassi's avatar
Emmanuele Bassi committed
957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976
      gtk_container_set_border_width (GTK_CONTAINER (box2), 10);
      gtk_box_pack_start (GTK_BOX (box1), box2, TRUE, TRUE, 0);

      button = gtk_radio_button_new_with_label (NULL, "button4");
      gtk_toggle_button_set_mode (GTK_TOGGLE_BUTTON (button), FALSE);
      gtk_box_pack_start (GTK_BOX (box2), button, TRUE, TRUE, 0);

      button = gtk_radio_button_new_with_label (
	         gtk_radio_button_get_group (GTK_RADIO_BUTTON (button)),
		 "button5");
      gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (button), TRUE);
      gtk_toggle_button_set_mode (GTK_TOGGLE_BUTTON (button), FALSE);
      gtk_box_pack_start (GTK_BOX (box2), button, TRUE, TRUE, 0);

      button = gtk_radio_button_new_with_label (
                 gtk_radio_button_get_group (GTK_RADIO_BUTTON (button)),
		 "button6");
      gtk_toggle_button_set_mode (GTK_TOGGLE_BUTTON (button), FALSE);
      gtk_box_pack_start (GTK_BOX (box2), button, TRUE, TRUE, 0);

977
      separator = gtk_separator_new (GTK_ORIENTATION_HORIZONTAL);
Emmanuele Bassi's avatar
Emmanuele Bassi committed
978 979 980 981 982 983 984
      gtk_box_pack_start (GTK_BOX (box1), separator, FALSE, TRUE, 0);

      table = create_widget_grid (GTK_TYPE_RADIO_BUTTON);
      gtk_container_set_border_width (GTK_CONTAINER (table), 10);
      gtk_box_pack_start (GTK_BOX (box1), table, TRUE, TRUE, 0);
    }

985
  if (!gtk_widget_get_visible (window))
Emmanuele Bassi's avatar
Emmanuele Bassi committed
986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009
    gtk_widget_show_all (window);
  else
    gtk_widget_destroy (window);
}

/*
 * GtkButtonBox
 */

static GtkWidget *
create_bbox (gint  horizontal,
	     char* title, 
	     gint  spacing,
	     gint  child_w,
	     gint  child_h,
	     gint  layout)
{
  GtkWidget *frame;
  GtkWidget *bbox;
  GtkWidget *button;
	
  frame = gtk_frame_new (title);

  if (horizontal)
1010
    bbox = gtk_button_box_new (GTK_ORIENTATION_HORIZONTAL);
Emmanuele Bassi's avatar
Emmanuele Bassi committed
1011
  else
1012
    bbox = gtk_button_box_new (GTK_ORIENTATION_VERTICAL);
Emmanuele Bassi's avatar
Emmanuele Bassi committed
1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053

  gtk_container_set_border_width (GTK_CONTAINER (bbox), 5);
  gtk_container_add (GTK_CONTAINER (frame), bbox);

  gtk_button_box_set_layout (GTK_BUTTON_BOX (bbox), layout);
  gtk_box_set_spacing (GTK_BOX (bbox), spacing);
  
  button = gtk_button_new_with_label ("OK");
  gtk_container_add (GTK_CONTAINER (bbox), button);
  
  button = gtk_button_new_with_label ("Cancel");
  gtk_container_add (GTK_CONTAINER (bbox), button);
  
  button = gtk_button_new_with_label ("Help");
  gtk_container_add (GTK_CONTAINER (bbox), button);

  return frame;
}

static void
create_button_box (GtkWidget *widget)
{
  static GtkWidget* window = NULL;
  GtkWidget *main_vbox;
  GtkWidget *vbox;
  GtkWidget *hbox;
  GtkWidget *frame_horz;
  GtkWidget *frame_vert;

  if (!window)
  {
    window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
    gtk_window_set_screen (GTK_WINDOW (window), gtk_widget_get_screen (widget));
    gtk_window_set_title (GTK_WINDOW (window), "Button Boxes");
    
    g_signal_connect (window, "destroy",
		      G_CALLBACK (gtk_widget_destroyed),
		      &window);

    gtk_container_set_border_width (GTK_CONTAINER (window), 10);

1054
    main_vbox = gtk_box_new (GTK_ORIENTATION_VERTICAL, 0);
Emmanuele Bassi's avatar
Emmanuele Bassi committed
1055 1056 1057 1058 1059
    gtk_container_add (GTK_CONTAINER (window), main_vbox);
    
    frame_horz = gtk_frame_new ("Horizontal Button Boxes");
    gtk_box_pack_start (GTK_BOX (main_vbox), frame_horz, TRUE, TRUE, 10);
    
1060
    vbox = gtk_box_new (GTK_ORIENTATION_VERTICAL, 0);
Emmanuele Bassi's avatar
Emmanuele Bassi committed
1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086
    gtk_container_set_border_width (GTK_CONTAINER (vbox), 10);
    gtk_container_add (GTK_CONTAINER (frame_horz), vbox);
    
    gtk_box_pack_start (GTK_BOX (vbox), 
                        create_bbox (TRUE, "Spread", 40, 85, 20, GTK_BUTTONBOX_SPREAD),
			TRUE, TRUE, 0);
    
    gtk_box_pack_start (GTK_BOX (vbox), 
                        create_bbox (TRUE, "Edge", 40, 85, 20, GTK_BUTTONBOX_EDGE),
			TRUE, TRUE, 5);
    
    gtk_box_pack_start (GTK_BOX (vbox), 
                        create_bbox (TRUE, "Start", 40, 85, 20, GTK_BUTTONBOX_START),
			TRUE, TRUE, 5);
    
    gtk_box_pack_start (GTK_BOX (vbox), 
                        create_bbox (TRUE, "End", 40, 85, 20, GTK_BUTTONBOX_END),
			TRUE, TRUE, 5);
    
    gtk_box_pack_start (GTK_BOX (vbox),
                        create_bbox (TRUE, "Center", 40, 85, 20, GTK_BUTTONBOX_CENTER),
			TRUE, TRUE, 5);
    
    frame_vert = gtk_frame_new ("Vertical Button Boxes");
    gtk_box_pack_start (GTK_BOX (main_vbox), frame_vert, TRUE, TRUE, 10);
    
1087
    hbox = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 0);
Emmanuele Bassi's avatar
Emmanuele Bassi committed
1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111
    gtk_container_set_border_width (GTK_CONTAINER (hbox), 10);
    gtk_container_add (GTK_CONTAINER (frame_vert), hbox);

    gtk_box_pack_start (GTK_BOX (hbox), 
                        create_bbox (FALSE, "Spread", 30, 85, 20, GTK_BUTTONBOX_SPREAD),
			TRUE, TRUE, 0);
    
    gtk_box_pack_start (GTK_BOX (hbox), 
                        create_bbox (FALSE, "Edge", 30, 85, 20, GTK_BUTTONBOX_EDGE),
			TRUE, TRUE, 5);
    
    gtk_box_pack_start (GTK_BOX (hbox), 
                        create_bbox (FALSE, "Start", 30, 85, 20, GTK_BUTTONBOX_START),
			TRUE, TRUE, 5);
    
    gtk_box_pack_start (GTK_BOX (hbox), 
                        create_bbox (FALSE, "End", 30, 85, 20, GTK_BUTTONBOX_END),
			TRUE, TRUE, 5);
    
    gtk_box_pack_start (GTK_BOX (hbox),
                        create_bbox (FALSE, "Center", 30, 85, 20, GTK_BUTTONBOX_CENTER),
			TRUE, TRUE, 5);
  }

1112
  if (!gtk_widget_get_visible (window))
Emmanuele Bassi's avatar
Emmanuele Bassi committed
1113 1114 1115 1116 1117 1118 1119 1120 1121 1122
    gtk_widget_show_all (window);
  else
    gtk_widget_destroy (window);
}

/*
 * GtkToolBar
 */

static GtkWidget*
1123
new_pixbuf (char      *filename,
1124
	    GdkWindow *window)
Emmanuele Bassi's avatar
Emmanuele Bassi committed
1125
{
1126 1127
  GtkWidget *widget;
  GdkPixbuf *pixbuf;
Emmanuele Bassi's avatar
Emmanuele Bassi committed
1128

1129 1130
  if (strcmp (filename, "test.xpm") == 0)
    pixbuf = NULL;
Emmanuele Bassi's avatar
Emmanuele Bassi committed
1131
  else
1132 1133 1134 1135
    pixbuf = gdk_pixbuf_new_from_file (filename, NULL);

  if (pixbuf == NULL)
    pixbuf = gdk_pixbuf_new_from_xpm_data ((const char **) openfile);
Emmanuele Bassi's avatar
Emmanuele Bassi committed
1136
  
1137 1138 1139
  widget = gtk_image_new_from_pixbuf (pixbuf);

  g_object_unref (pixbuf);
Emmanuele Bassi's avatar
Emmanuele Bassi committed
1140

1141
  return widget;
Emmanuele Bassi's avatar
Emmanuele Bassi committed
1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162
}


static void
set_toolbar_small_stock (GtkWidget *widget,
			 gpointer   data)
{
  gtk_toolbar_set_icon_size (GTK_TOOLBAR (data), GTK_ICON_SIZE_SMALL_TOOLBAR);
}

static void
set_toolbar_large_stock (GtkWidget *widget,
			 gpointer   data)
{
  gtk_toolbar_set_icon_size (GTK_TOOLBAR (data), GTK_ICON_SIZE_LARGE_TOOLBAR);
}

static void
set_toolbar_horizontal (GtkWidget *widget,
			gpointer   data)
{
1163
  gtk_orientable_set_orientation (GTK_ORIENTABLE (data), GTK_ORIENTATION_HORIZONTAL);
Emmanuele Bassi's avatar
Emmanuele Bassi committed
1164 1165 1166 1167 1168 1169
}

static void
set_toolbar_vertical (GtkWidget *widget,
		      gpointer   data)
{
1170
  gtk_orientable_set_orientation (GTK_ORIENTABLE (data), GTK_ORIENTATION_VERTICAL);
Emmanuele Bassi's avatar
Emmanuele Bassi committed
1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204
}

static void
set_toolbar_icons (GtkWidget *widget,
		   gpointer   data)
{
  gtk_toolbar_set_style (GTK_TOOLBAR (data), GTK_TOOLBAR_ICONS);
}

static void
set_toolbar_text (GtkWidget *widget,
	          gpointer   data)
{
  gtk_toolbar_set_style (GTK_TOOLBAR (data), GTK_TOOLBAR_TEXT);
}

static void
set_toolbar_both (GtkWidget *widget,
		  gpointer   data)
{
  gtk_toolbar_set_style (GTK_TOOLBAR (data), GTK_TOOLBAR_BOTH);
}

static void
set_toolbar_both_horiz (GtkWidget *widget,
			gpointer   data)
{
  gtk_toolbar_set_style (GTK_TOOLBAR (data), GTK_TOOLBAR_BOTH_HORIZ);
}

static void
set_toolbar_enable (GtkWidget *widget,
		    gpointer   data)
{
1205 1206
  GtkSettings *settings = gtk_widget_get_settings (widget);
  g_object_set (settings, "gtk-enable-tooltips", TRUE, NULL);
Emmanuele Bassi's avatar
Emmanuele Bassi committed
1207 1208 1209 1210 1211 1212
}

static void
set_toolbar_disable (GtkWidget *widget,
		     gpointer   data)
{
1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254
  GtkSettings *settings = gtk_widget_get_settings (widget);
  g_object_set (settings, "gtk-enable-tooltips", FALSE, NULL);
}

static GtkActionEntry create_toolbar_items[] = {
    { NULL, GTK_STOCK_NEW, NULL, NULL, "Stock icon: New",
      G_CALLBACK (set_toolbar_small_stock) },
    { NULL, GTK_STOCK_OPEN, NULL, NULL, "Stock icon: Open",
      G_CALLBACK (set_toolbar_large_stock) },
    { NULL, NULL, "Horizontal", NULL, "Horizontal toolbar layout",
      G_CALLBACK (set_toolbar_horizontal) },
    { NULL, NULL, "Vertical", NULL, "Vertical toolbar layout",
      G_CALLBACK (set_toolbar_vertical) },
    { NULL },
    { NULL, NULL, "Icons", NULL, "Only show toolbar icons",
      G_CALLBACK (set_toolbar_icons) },
    { NULL, NULL, "Text", NULL, "Only show toolbar text",
      G_CALLBACK (set_toolbar_text) },
    { NULL, NULL, "Both", NULL, "Show toolbar icons and text",
      G_CALLBACK (set_toolbar_both) },
    { NULL, NULL, "Both (horizontal)", NULL, "Show toolbar icons and text in a horizontal fashion",
      G_CALLBACK (set_toolbar_both_horiz) },
    { NULL },
    { "entry", NULL, NULL, "This is an unusable GtkEntry ;)",
      NULL },
    { NULL },
    { NULL },
    { NULL, NULL, "Enable", NULL, "Enable tooltips",
      G_CALLBACK (set_toolbar_enable) },
    { NULL, NULL, "Disable", NULL, "Disable tooltips",
      G_CALLBACK (set_toolbar_disable) },
    { NULL },
    { NULL, NULL, "Frobate", NULL, "Frobate tooltip",
      NULL },
    { NULL, NULL, "Baz", NULL, "Baz tooltip",
      NULL },
    { NULL },
    { NULL, NULL, "Blah", NULL, "Blash tooltip",
      NULL },
    { NULL, NULL, "Bar", NULL, "Bar tooltip",
      NULL },
};
Emmanuele Bassi's avatar
Emmanuele Bassi committed
1255 1256 1257 1258 1259 1260 1261 1262 1263

static void
create_toolbar (GtkWidget *widget)
{
  static GtkWidget *window = NULL;
  GtkWidget *toolbar;

  if (!window)
    {
1264 1265
      guint i;

Emmanuele Bassi's avatar
Emmanuele Bassi committed
1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279
      window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
      gtk_window_set_screen (GTK_WINDOW (window),
			     gtk_widget_get_screen (widget));
      
      gtk_window_set_title (GTK_WINDOW (window), "Toolbar test");

      g_signal_connect (window, "destroy",
			G_CALLBACK (gtk_widget_destroyed),
			&window);

      gtk_container_set_border_width (GTK_CONTAINER (window), 0);
      gtk_widget_realize (window);

      toolbar = gtk_toolbar_new ();
1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299
      for (i = 0; i < G_N_ELEMENTS (create_toolbar_items); i++)
        {
          GtkToolItem *toolitem;

          if (create_toolbar_items[i].tooltip == NULL)
            toolitem = gtk_separator_tool_item_new ();
          else if (g_strcmp0 (create_toolbar_items[i].name, "entry") == 0)
            {
              GtkWidget *entry;

              toolitem = gtk_tool_item_new ();
              entry = gtk_entry_new ();
              gtk_container_add (GTK_CONTAINER (toolitem), entry);
            }
          else if (create_toolbar_items[i].stock_id)
            toolitem = gtk_tool_button_new_from_stock (create_toolbar_items[i].stock_id);
          else
            {
              GtkWidget *icon;

1300
              icon = new_pixbuf ("test.xpm", gtk_widget_get_window (window));
1301 1302 1303 1304 1305 1306 1307 1308
              toolitem = gtk_tool_button_new (icon, create_toolbar_items[i].label);
            }
          if (create_toolbar_items[i].callback)
            g_signal_connect (toolitem, "clicked",
                              create_toolbar_items[i].callback, toolbar);
          gtk_tool_item_set_tooltip_text (toolitem, create_toolbar_items[i].tooltip);
          gtk_toolbar_insert (GTK_TOOLBAR (toolbar), toolitem, -1);
        }
Emmanuele Bassi's avatar
Emmanuele Bassi committed
1309 1310 1311 1312 1313 1314

      gtk_container_add (GTK_CONTAINER (window), toolbar);

      gtk_widget_set_size_request (toolbar, 200, -1);
    }

1315
  if (!gtk_widget_get_visible (window))
Emmanuele Bassi's avatar
Emmanuele Bassi committed
1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367
    gtk_widget_show_all (window);
  else
    gtk_widget_destroy (window);
}

/*
 * GtkStatusBar
 */

static guint statusbar_counter = 1;

static void
statusbar_push (GtkWidget *button,
		GtkStatusbar *statusbar)
{
  gchar text[1024];

  sprintf (text, "something %d", statusbar_counter++);

  gtk_statusbar_push (statusbar, 1, text);
}

static void
statusbar_push_long (GtkWidget *button,
                     GtkStatusbar *statusbar)
{
  gchar text[1024];

  sprintf (text, "Just because a system has menu choices written with English words, phrases or sentences, that is no guarantee, that it is comprehensible. Individual words may not be familiar to some users (for example, \"repaginate\"), and two menu items may appear to satisfy the users's needs, whereas only one does (for example, \"put away\" or \"eject\").");

  gtk_statusbar_push (statusbar, 1, text);
}

static void
statusbar_pop (GtkWidget *button,
	       GtkStatusbar *statusbar)
{
  gtk_statusbar_pop (statusbar, 1);
}

static void
statusbar_steal (GtkWidget *button,
	         GtkStatusbar *statusbar)
{
  gtk_statusbar_remove (statusbar, 1, 4);
}

static void
statusbar_popped (GtkStatusbar  *statusbar,
		  guint          context_id,
		  const gchar	*text)
{
1368
  if (!text)
Emmanuele Bassi's avatar
Emmanuele Bassi committed
1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425
    statusbar_counter = 1;
}

static void
statusbar_contexts (GtkStatusbar *statusbar)
{
  gchar *string;

  string = "any context";
  g_print ("GtkStatusBar: context=\"%s\", context_id=%d\n",
	   string,
	   gtk_statusbar_get_context_id (statusbar, string));
  
  string = "idle messages";
  g_print ("GtkStatusBar: context=\"%s\", context_id=%d\n",
	   string,
	   gtk_statusbar_get_context_id (statusbar, string));
  
  string = "some text";
  g_print ("GtkStatusBar: context=\"%s\", context_id=%d\n",
	   string,
	   gtk_statusbar_get_context_id (statusbar, string));

  string = "hit the mouse";
  g_print ("GtkStatusBar: context=\"%s\", context_id=%d\n",
	   string,
	   gtk_statusbar_get_context_id (statusbar, string));

  string = "hit the mouse2";
  g_print ("GtkStatusBar: context=\"%s\", context_id=%d\n",
	   string,
	   gtk_statusbar_get_context_id (statusbar, string));
}

static void
create_statusbar (GtkWidget *widget)
{
  static GtkWidget *window = NULL;
  GtkWidget *box1;
  GtkWidget *box2;
  GtkWidget *button;
  GtkWidget *separator;
  GtkWidget *statusbar;

  if (!window)
    {
      window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
      gtk_window_set_screen (GTK_WINDOW (window),
			     gtk_widget_get_screen (widget));

      g_signal_connect (window, "destroy",
			G_CALLBACK (gtk_widget_destroyed),
			&window);

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

1426
      box1 = gtk_box_new (GTK_ORIENTATION_VERTICAL, 0);
Emmanuele Bassi's avatar
Emmanuele Bassi committed
1427 1428
      gtk_container_add (GTK_CONTAINER (window), box1);

1429
      box2 = gtk_box_new (GTK_ORIENTATION_VERTICAL, 10);
Emmanuele Bassi's avatar
Emmanuele Bassi committed
1430 1431 1432 1433 1434 1435 1436 1437 1438 1439
      gtk_container_set_border_width (GTK_CONTAINER (box2), 10);
      gtk_box_pack_start (GTK_BOX (box1), box2, TRUE, TRUE, 0);

      statusbar = gtk_statusbar_new ();
      gtk_box_pack_end (GTK_BOX (box1), statusbar, TRUE, TRUE, 0);
      g_signal_connect (statusbar,
			"text_popped",
			G_CALLBACK (statusbar_popped),
			NULL);

1440
      button = g_object_new (gtk_button_get_type (),
Emmanuele Bassi's avatar
Emmanuele Bassi committed
1441 1442 1443 1444 1445 1446 1447 1448
			       "label", "push something",
			       "visible", TRUE,
			       "parent", box2,
			       NULL);
      g_object_connect (button,
			"signal::clicked", statusbar_push, statusbar,
			NULL);

1449
      button = g_object_connect (g_object_new (gtk_button_get_type (),
Emmanuele Bassi's avatar
Emmanuele Bassi committed
1450 1451 1452 1453 1454 1455 1456
						 "label", "pop",
						 "visible", TRUE,
						 "parent", box2,
						 NULL),
				 "signal_after::clicked", statusbar_pop, statusbar,
				 NULL);

1457
      button = g_object_connect (g_object_new (gtk_button_get_type (),
Emmanuele Bassi's avatar
Emmanuele Bassi committed
1458 1459 1460 1461 1462 1463 1464
						 "label", "steal #4",
						 "visible", TRUE,
						 "parent", box2,
						 NULL),
				 "signal_after::clicked", statusbar_steal, statusbar,
				 NULL);

1465
      button = g_object_connect (g_object_new (gtk_button_get_type (),
Emmanuele Bassi's avatar
Emmanuele Bassi committed
1466 1467 1468 1469 1470 1471 1472
						 "label", "test contexts",
						 "visible", TRUE,
						 "parent", box2,
						 NULL),
				 "swapped_signal_after::clicked", statusbar_contexts, statusbar,
				 NULL);

1473
      button = g_object_connect (g_object_new (gtk_button_get_type (),
Emmanuele Bassi's avatar
Emmanuele Bassi committed
1474 1475 1476 1477 1478 1479 1480
						 "label", "push something long",
						 "visible", TRUE,
						 "parent", box2,
						 NULL),
				 "signal_after::clicked", statusbar_push_long, statusbar,
				 NULL);
      
1481
      separator = gtk_separator_new (GTK_ORIENTATION_HORIZONTAL);
Emmanuele Bassi's avatar
Emmanuele Bassi committed
1482 1483
      gtk_box_pack_start (GTK_BOX (box1), separator, FALSE, TRUE, 0);

1484
      box2 = gtk_box_new (GTK_ORIENTATION_VERTICAL, 10);
Emmanuele Bassi's avatar
Emmanuele Bassi committed
1485 1486 1487 1488 1489 1490 1491 1492
      gtk_container_set_border_width (GTK_CONTAINER (box2), 10);
      gtk_box_pack_start (GTK_BOX (box1), box2, FALSE, TRUE, 0);

      button = gtk_button_new_with_label ("close");
      g_signal_connect_swapped (button, "clicked",
			        G_CALLBACK (gtk_widget_destroy),
				window);
      gtk_box_pack_start (GTK_BOX (box2), button, TRUE, TRUE, 0);
1493
      gtk_widget_set_can_default (button, TRUE);
Emmanuele Bassi's avatar
Emmanuele Bassi committed
1494 1495 1496
      gtk_widget_grab_default (button);
    }

1497
  if (!gtk_widget_get_visible (window))
Emmanuele Bassi's avatar
Emmanuele Bassi committed
1498 1499 1500 1501 1502
    gtk_widget_show_all (window);
  else
    gtk_widget_destroy (window);
}

1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614
/* Alpha demo */

static void
alpha_changed (GtkRange *range, GtkWidget *widget)
{
  gdouble alpha;

  alpha = gtk_range_get_value (range);

  gtk_widget_set_opacity (widget, alpha / 100.0);
}


void
create_alpha_widgets (GtkWidget *widget)
{
  static GtkWidget *window = NULL;

  if (!window)
    {
      GtkWidget *vbox2, *vbox, *main_hbox;
      GtkWidget *button, *event_box, *label, *scale;
      GtkWidget *alpha1, *alpha2, *alpha3;

      window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
      gtk_window_set_screen (GTK_WINDOW (window),
                             gtk_widget_get_screen (widget));
      gtk_window_set_default_size (GTK_WINDOW (window),
                                   450, 450);

      g_signal_connect (window, "destroy",
                        G_CALLBACK (gtk_widget_destroyed), &window);

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

      main_hbox = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 0);
      gtk_container_add (GTK_CONTAINER (window), main_hbox);

      vbox = gtk_box_new (GTK_ORIENTATION_VERTICAL, 0);

      gtk_box_pack_start (GTK_BOX (main_hbox), vbox, FALSE, FALSE, 0);

      /* Plain button (no gdkwindows */

      label = gtk_label_new ("non-window widget");
      gtk_box_pack_start (GTK_BOX (vbox), label, FALSE, FALSE, 0);

      alpha1 = button = gtk_button_new_with_label ("A Button");
      gtk_box_pack_start (GTK_BOX (vbox), button, FALSE, FALSE, 0);

      /* windowed container with both windowed and normal button */
      label = gtk_label_new ("\nwindow widget");
      gtk_box_pack_start (GTK_BOX (vbox), label, FALSE, FALSE, 0);

      alpha2 = event_box = gtk_event_box_new ();
      gtk_box_pack_start (GTK_BOX (vbox), event_box, FALSE, FALSE, 0);

      vbox2 = gtk_box_new (GTK_ORIENTATION_VERTICAL, 0);
      gtk_container_add (GTK_CONTAINER (event_box), vbox2);

      button = gtk_button_new_with_label ("A Button");
      gtk_box_pack_start (GTK_BOX (vbox2), button, FALSE, FALSE, 0);

      event_box = gtk_event_box_new ();
      button = gtk_button_new_with_label ("A Button (in window)");
      gtk_container_add (GTK_CONTAINER (event_box), button);
      gtk_box_pack_start (GTK_BOX (vbox2), event_box, FALSE, FALSE, 0);

      /* non-windowed container with both windowed and normal button */
      label = gtk_label_new ("\nnon-window widget with widget child");
      gtk_box_pack_start (GTK_BOX (vbox), label, FALSE, FALSE, 0);

      alpha3 = vbox2 = gtk_box_new (GTK_ORIENTATION_VERTICAL, 0);
      gtk_box_pack_start (GTK_BOX (vbox), vbox2, FALSE, FALSE, 0);

      button = gtk_button_new_with_label ("A Button");
      gtk_box_pack_start (GTK_BOX (vbox2), button, FALSE, FALSE, 0);

      event_box = gtk_event_box_new ();
      button = gtk_button_new_with_label ("A Button (in window)");
      gtk_container_add (GTK_CONTAINER (event_box), button);
      gtk_box_pack_start (GTK_BOX (vbox2), event_box, FALSE, FALSE, 0);

      scale = gtk_scale_new_with_range (GTK_ORIENTATION_VERTICAL,
                                         0, 100, 1);
      gtk_box_pack_start (GTK_BOX (main_hbox), scale, FALSE, FALSE, 0);
      g_signal_connect (scale, "value_changed", G_CALLBACK (alpha_changed), alpha1);
      gtk_range_set_value (GTK_RANGE (scale), 50);

      scale = gtk_scale_new_with_range (GTK_ORIENTATION_VERTICAL,
                                         0, 100, 1);
      gtk_box_pack_start (GTK_BOX (main_hbox), scale, FALSE, FALSE, 0);
      g_signal_connect (scale, "value_changed", G_CALLBACK (alpha_changed), alpha2);
      gtk_range_set_value (GTK_RANGE (scale), 50);

      scale = gtk_scale_new_with_range (GTK_ORIENTATION_VERTICAL,
                                         0, 100, 1);
      gtk_box_pack_start (GTK_BOX (main_hbox), scale, FALSE, FALSE, 0);
      g_signal_connect (scale, "value_changed", G_CALLBACK (alpha_changed), alpha3);
      gtk_range_set_value (GTK_RANGE (scale), 50);

      gtk_widget_show_all (main_hbox);
    }

  if (!gtk_widget_get_visible (window))
    gtk_widget_show (window);
  else
    gtk_widget_destroy (window);
}


Emmanuele Bassi's avatar
Emmanuele Bassi committed
1615 1616 1617 1618 1619 1620 1621
/* 
 * Label Demo
 */
static void
sensitivity_toggled (GtkWidget *toggle,
                     GtkWidget *widget)
{
1622 1623
  gtk_widget_set_sensitive (widget,
                            gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (toggle)));
Emmanuele Bassi's avatar
Emmanuele Bassi committed
1624 1625 1626 1627 1628 1629 1630 1631 1632 1633
}

static GtkWidget*
create_sensitivity_control (GtkWidget *widget)
{
  GtkWidget *button;

  button = gtk_toggle_button_new_with_label ("Sensitive");  

  gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (button),
1634
                                gtk_widget_is_sensitive (widget));
Emmanuele Bassi's avatar
Emmanuele Bassi committed
1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675
  
  g_signal_connect (button,
                    "toggled",
                    G_CALLBACK (sensitivity_toggled),
                    widget);
  
  gtk_widget_show_all (button);

  return button;
}

static void
set_selectable_recursive (GtkWidget *widget,
                          gboolean   setting)
{
  if (GTK_IS_CONTAINER (widget))
    {
      GList *children;
      GList *tmp;
      
      children = gtk_container_get_children (GTK_CONTAINER (widget));
      tmp = children;
      while (tmp)
        {
          set_selectable_recursive (tmp->data, setting);

          tmp = tmp->next;
        }
      g_list_free (children);
    }
  else if (GTK_IS_LABEL (widget))
    {
      gtk_label_set_selectable (GTK_LABEL (widget), setting);
    }
}

static void
selectable_toggled (GtkWidget *toggle,
                    GtkWidget *widget)
{
  set_selectable_recursive (widget,
1676
                            gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (toggle)));
Emmanuele Bassi's avatar
Emmanuele Bassi committed
1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698
}

static GtkWidget*
create_selectable_control (GtkWidget *widget)
{
  GtkWidget *button;

  button = gtk_toggle_button_new_with_label ("Selectable");  

  gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (button),
                                FALSE);
  
  g_signal_connect (button,
                    "toggled",
                    G_CALLBACK (selectable_toggled),
                    widget);
  
  gtk_widget_show_all (button);

  return button;
}

1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713
static void
dialog_response (GtkWidget *dialog, gint response_id, GtkLabel *label)
{
  const gchar *text;

  gtk_widget_destroy (dialog);

  text = "Some <a href=\"http://en.wikipedia.org/wiki/Text\" title=\"plain text\">text</a> may be marked up\n"
         "as hyperlinks, which can be clicked\n"
         "or activated via <a href=\"keynav\">keynav</a>.\n"
         "The links remain the same.";
  gtk_label_set_markup (label, text);
}

static gboolean
1714
activate_link (GtkWidget *label, const gchar *uri, gpointer data)
1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737
{
  if (g_strcmp0 (uri, "keynav") == 0)
    {
      GtkWidget *dialog;

      dialog = gtk_message_dialog_new_with_markup (GTK_WINDOW (gtk_widget_get_toplevel (label)),
                                       GTK_DIALOG_DESTROY_WITH_PARENT,
                                       GTK_MESSAGE_INFO,
                                       GTK_BUTTONS_OK,
                                       "The term <i>keynav</i> is a shorthand for "
                                       "keyboard navigation and refers to the process of using a program "
                                       "(exclusively) via keyboard input.");

      gtk_window_present (GTK_WINDOW (dialog));

      g_signal_connect (dialog, "response", G_CALLBACK (dialog_response), label);

      return TRUE;
    }

  return FALSE;
}

Emmanuele Bassi's avatar
Emmanuele Bassi committed
1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759
void create_labels (GtkWidget *widget)
{
  static GtkWidget *window = NULL;
  GtkWidget *hbox;
  GtkWidget *vbox;
  GtkWidget *frame;
  GtkWidget *label;
  GtkWidget *button;

  if (!window)
    {
      window = gtk_window_new (GTK_WINDOW_TOPLEVEL);

      gtk_window_set_screen (GTK_WINDOW (window),
			     gtk_widget_get_screen (widget));

      g_signal_connect (window, "destroy",
			G_CALLBACK (gtk_widget_destroyed),
			&window);

      gtk_window_set_title (GTK_WINDOW (window), "Label");

1760
      vbox = gtk_box_new (GTK_ORIENTATION_VERTICAL, 5);
Emmanuele Bassi's avatar
Emmanuele Bassi committed
1761
      
1762
      hbox = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 5);
Emmanuele Bassi's avatar
Emmanuele Bassi committed
1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774
      gtk_container_add (GTK_CONTAINER (window), vbox);

      gtk_box_pack_end (GTK_BOX (vbox), hbox, FALSE, FALSE, 0);

      button = create_sensitivity_control (hbox);

      gtk_box_pack_start (GTK_BOX (vbox), button, FALSE, FALSE, 0);

      button = create_selectable_control (hbox);

      gtk_box_pack_start (GTK_BOX (vbox), button, FALSE, FALSE, 0);
      
1775
      vbox = gtk_box_new (GTK_ORIENTATION_VERTICAL, 5);
Emmanuele Bassi's avatar
Emmanuele Bassi committed
1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824
      
      gtk_box_pack_start (GTK_BOX (hbox), vbox, FALSE, FALSE, 0);
      gtk_container_set_border_width (GTK_CONTAINER (window), 5);

      frame = gtk_frame_new ("Normal Label");
      label = gtk_label_new ("This is a Normal label");
      gtk_label_set_ellipsize (GTK_LABEL (label), PANGO_ELLIPSIZE_START);
      gtk_container_add (GTK_CONTAINER (frame), label);
      gtk_box_pack_start (GTK_BOX (vbox), frame, FALSE, FALSE, 0);

      frame = gtk_frame_new ("Multi-line Label");
      label = gtk_label_new ("This is a Multi-line label.\nSecond line\nThird line");
      gtk_label_set_ellipsize (GTK_LABEL (label), PANGO_ELLIPSIZE_END);
      gtk_container_add (GTK_CONTAINER (frame), label);
      gtk_box_pack_start (GTK_BOX (vbox), frame, FALSE, FALSE, 0);

      frame = gtk_frame_new ("Left Justified Label");
      label = gtk_label_new ("This is a Left-Justified\nMulti-line label.\nThird      line");
      gtk_label_set_ellipsize (GTK_LABEL (label), PANGO_ELLIPSIZE_MIDDLE);
      gtk_label_set_justify (GTK_LABEL (label), GTK_JUSTIFY_LEFT);
      gtk_container_add (GTK_CONTAINER (frame), label);
      gtk_box_pack_start (GTK_BOX (vbox), frame, FALSE, FALSE, 0);

      frame = gtk_frame_new ("Right Justified Label");
      gtk_label_set_ellipsize (GTK_LABEL (label), PANGO_ELLIPSIZE_START);
      label = gtk_label_new ("This is a Right-Justified\nMulti-line label.\nFourth line, (j/k)");
      gtk_label_set_justify (GTK_LABEL (label), GTK_JUSTIFY_RIGHT);
      gtk_container_add (GTK_CONTAINER (frame), label);
      gtk_box_pack_start (GTK_BOX (vbox), frame, FALSE, FALSE, 0);

      frame = gtk_frame_new ("Internationalized Label");
      label = gtk_label_new (NULL);
      gtk_label_set_markup (GTK_LABEL (label),
			    "French (Fran\303\247ais) Bonjour, Salut\n"
			    "Korean (\355\225\234\352\270\200)   \354\225\210\353\205\225\355\225\230\354\204\270\354\232\224, \354\225\210\353\205\225\355\225\230\354\213\255\353\213\210\352\271\214\n"
			    "Russian (\320\240\321\203\321\201\321\201\320\272\320\270\320\271) \320\227\320\264\321\200\320\260\320\262\321\201\321\202\320\262\321\203\320\271\321\202\320\265!\n"
			    "Chinese (Simplified) <span lang=\"zh-cn\">\345\205\203\346\260\224	\345\274\200\345\217\221</span>\n"
			    "Chinese (Traditional) <span lang=\"zh-tw\">\345\205\203\346\260\243	\351\226\213\347\231\274</span>\n"
			    "Japanese <span lang=\"ja\">\345\205\203\346\260\227	\351\226\213\347\231\272</span>");
      gtk_label_set_justify (GTK_LABEL (label), GTK_JUSTIFY_LEFT);
      gtk_container_add (GTK_CONTAINER (frame), label);
      gtk_box_pack_start (GTK_BOX (vbox), frame, FALSE, FALSE, 0);

      frame = gtk_frame_new ("Bidirection Label");
      label = gtk_label_new ("\342\200\217Arabic	\330\247\331\204\330\263\331\204\330\247\331\205 \330\271\331\204\331\212\331\203\331\205\n"
			     "\342\200\217Hebrew	\327\251\327\234\327\225\327\235");
      gtk_container_add (GTK_CONTAINER (frame), label);
      gtk_box_pack_start (GTK_BOX (vbox), frame, FALSE, FALSE, 0);

1825 1826 1827 1828 1829 1830 1831 1832 1833
      frame = gtk_frame_new ("Links in a label");
      label = gtk_label_new ("Some <a href=\"http://en.wikipedia.org/wiki/Text\" title=\"plain text\">text</a> may be marked up\n"
                             "as hyperlinks, which can be clicked\n"
                             "or activated via <a href=\"keynav\">keynav</a>");
      gtk_label_set_use_markup (GTK_LABEL (label), TRUE);
      gtk_container_add (GTK_CONTAINER (frame), label);
      gtk_box_pack_start (GTK_BOX (vbox), frame, FALSE, FALSE, 0);
      g_signal_connect (label, "activate-link", G_CALLBACK (activate_link), NULL);

1834
      vbox = gtk_box_new (GTK_ORIENTATION_VERTICAL, 5);
Emmanuele Bassi's avatar
Emmanuele Bassi committed
1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891
      gtk_box_pack_start (GTK_BOX (hbox), vbox, FALSE, FALSE, 0);
      frame = gtk_frame_new ("Line wrapped label");
      label = gtk_label_new ("This is an example of a line-wrapped label.  It should not be taking "\
			     "up the entire             "/* big space to test spacing */\
			     "width allocated to it, but automatically wraps the words to fit.  "\
			     "The time has come, for all good men, to come to the aid of their party.  "\
			     "The sixth sheik's six sheep's sick.\n"\
			     "     It supports multiple paragraphs correctly, and  correctly   adds "\
			     "many          extra  spaces. ");

      gtk_label_set_line_wrap (GTK_LABEL (label), TRUE);
      gtk_container_add (GTK_CONTAINER (frame), label);
      gtk_box_pack_start (GTK_BOX (vbox), frame, FALSE, FALSE, 0);

      frame = gtk_frame_new ("Filled, wrapped label");
      label = gtk_label_new ("This is an example of a line-wrapped, filled label.  It should be taking "\
			     "up the entire              width allocated to it.  Here is a seneance to prove "\
			     "my point.  Here is another sentence. "\
			     "Here comes the sun, do de do de do.\n"\
			     "    This is a new paragraph.\n"\
			     "    This is another newer, longer, better paragraph.  It is coming to an end, "\
			     "unfortunately.");
      gtk_label_set_justify (GTK_LABEL (label), GTK_JUSTIFY_FILL);
      gtk_label_set_line_wrap (GTK_LABEL (label), TRUE);
      gtk_container_add (GTK_CONTAINER (frame), label);
      gtk_box_pack_start (GTK_BOX (vbox), frame, FALSE, FALSE, 0);

      frame = gtk_frame_new ("Underlined label");
      label = gtk_label_new ("This label is underlined!\n"
			     "This one is underlined (\343\201\223\343\202\223\343\201\253\343\201\241\343\201\257) in quite a funky fashion");
      gtk_label_set_justify (GTK_LABEL (label), GTK_JUSTIFY_LEFT);
      gtk_label_set_pattern (GTK_LABEL (label), "_________________________ _ _________ _ _____ _ __ __  ___ ____ _____");
      gtk_container_add (GTK_CONTAINER (frame), label);
      gtk_box_pack_start (GTK_BOX (vbox), frame, FALSE, FALSE, 0);

      frame = gtk_frame_new ("Markup label");
      label = gtk_label_new (NULL);

      /* There's also a gtk_label_set_markup() without accel if you
       * don't have an accelerator key
       */
      gtk_label_set_markup_with_mnemonic (GTK_LABEL (label),
					  "This <span foreground=\"blue\" background=\"orange\">label</span> has "
					  "<b>markup</b> _such as "
					  "<big><i>Big Italics</i></big>\n"
					  "<tt>Monospace font</tt>\n"
					  "<u>Underline!</u>\n"
					  "foo\n"
					  "<span foreground=\"green\" background=\"red\">Ugly colors</span>\n"
					  "and nothing on this line,\n"
					  "or this.\n"
					  "or this either\n"
					  "or even on this one\n"
					  "la <big>la <big>la <big>la <big>la</big></big></big></big>\n"
					  "but this _word is <span foreground=\"purple\"><big>purple</big></span>\n"
					  "<span underline=\"double\">We like <sup>superscript</sup> and <sub>subscript</sub> too</span>");

1892
      g_assert (gtk_label_get_mnemonic_keyval (GTK_LABEL (label)) == GDK_KEY_s);
Emmanuele Bassi's avatar
Emmanuele Bassi committed
1893 1894 1895 1896 1897
      
      gtk_container_add (GTK_CONTAINER (frame), label);
      gtk_box_pack_start (GTK_BOX (vbox), frame, FALSE, FALSE, 0);
    }
  
1898
  if (!gtk_widget_get_visible (window))
Emmanuele Bassi's avatar
Emmanuele Bassi committed
1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914
    gtk_widget_show_all (window);
  else
    gtk_widget_destroy (window);
}

static void
on_angle_scale_changed (GtkRange *range,
			GtkLabel *label)
{
  gtk_label_set_angle (GTK_LABEL (label), gtk_range_get_value (range));
}

static void
create_rotated_label (GtkWidget *widget)
{
  static GtkWidget *window = NULL;
1915
  GtkWidget *content_area;
Emmanuele Bassi's avatar
Emmanuele Bassi committed
1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934
  GtkWidget *vbox;
  GtkWidget *hscale;
  GtkWidget *label;  
  GtkWidget *scale_label;  
  GtkWidget *scale_hbox;  

  if (!window)
    {
      window = gtk_dialog_new_with_buttons ("Rotated Label",
					    GTK_WINDOW (gtk_widget_get_toplevel (widget)), 0,
					    GTK_STOCK_CLOSE, GTK_RESPONSE_CLOSE,
					    NULL);

      gtk_window_set_resizable (GTK_WINDOW (window), TRUE);

      gtk_window_set_screen (GTK_WINDOW (window),
			     gtk_widget_get_screen (widget));

      g_signal_connect (window, "response",
1935
			G_CALLBACK (gtk_widget_destroy), NULL);
Emmanuele Bassi's avatar
Emmanuele Bassi committed
1936 1937 1938
      g_signal_connect (window, "destroy",
			G_CALLBACK (gtk_widget_destroyed), &window);

1939 1940
      content_area = gtk_dialog_get_content_area (GTK_DIALOG (window));

1941
      vbox = gtk_box_new (GTK_ORIENTATION_VERTICAL, 5);
1942
      gtk_box_pack_start (GTK_BOX (content_area), vbox, TRUE, TRUE, 0);
Emmanuele Bassi's avatar
Emmanuele Bassi committed
1943 1944 1945 1946 1947 1948
      gtk_container_set_border_width (GTK_CONTAINER (vbox), 10);

      label = gtk_label_new (NULL);
      gtk_label_set_markup (GTK_LABEL (label), "Hello World\n<i>Rotate</i> <span underline='single' foreground='blue'>me</span>");
      gtk_box_pack_start (GTK_BOX (vbox), label, TRUE, TRUE, 0);

1949
      scale_hbox = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 0);
Emmanuele Bassi's avatar
Emmanuele Bassi committed
1950 1951 1952 1953 1954 1955
      gtk_box_pack_start (GTK_BOX (vbox), scale_hbox, FALSE, FALSE, 0);
      
      scale_label = gtk_label_new (NULL);
      gtk_label_set_markup (GTK_LABEL (scale_label), "<i>Angle: </i>");
      gtk_box_pack_start (GTK_BOX (scale_hbox), scale_label, FALSE, FALSE, 0);

1956 1957
      hscale = gtk_scale_new_with_range (GTK_ORIENTATION_HORIZONTAL,
                                         0, 360, 5);
Emmanuele Bassi's avatar
Emmanuele Bassi committed
1958 1959 1960 1961
      g_signal_connect (hscale, "value-changed",
			G_CALLBACK (on_angle_scale_changed), label);
      
      gtk_range_set_value (GTK_RANGE (hscale), 45);
1962
      gtk_widget_set_size_request (hscale, 200, -1);
Emmanuele Bassi's avatar
Emmanuele Bassi committed
1963 1964 1965
      gtk_box_pack_start (GTK_BOX (scale_hbox), hscale, TRUE, TRUE, 0);
    }
  
1966
  if (!gtk_widget_get_visible (window))
Emmanuele Bassi's avatar
Emmanuele Bassi committed
1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980
    gtk_widget_show_all (window);
  else
    gtk_widget_destroy (window);
}

#define DEFAULT_TEXT_RADIUS 200

static void
on_rotated_text_unrealize (GtkWidget *widget)
{
  g_object_set_data (G_OBJECT (widget), "text-gc", NULL);
}

static gboolean
1981 1982 1983
on_rotated_text_draw (GtkWidget *widget,
                      cairo_t   *cr,
	              GdkPixbuf *tile_pixbuf)
Emmanuele Bassi's avatar
Emmanuele Bassi committed
1984 1985 1986 1987 1988
{
  static const gchar *words[] = { "The", "grand", "old", "Duke", "of", "York",
                                  "had", "10,000", "men" };
  int n_words;
  int i;
1989
  int width, height;
Emmanuele Bassi's avatar
Emmanuele Bassi committed
1990 1991 1992 1993 1994
  double radius;
  PangoLayout *layout;
  PangoContext *context;
  PangoFontDescription *desc;

1995 1996 1997 1998
  if (tile_pixbuf)
    {
      gdk_cairo_set_source_pixbuf (cr, tile_pixbuf, 0, 0);
      cairo_pattern_set_extend (cairo_get_source (cr), CAIRO_EXTEND_REPEAT);
Emmanuele Bassi's avatar
Emmanuele Bassi committed
1999
    }
2000 2001
  else
    cairo_set_source_rgb (cr, 0, 0, 0);
Emmanuele Bassi's avatar
Emmanuele Bassi committed
2002

2003 2004 2005
  width = gtk_widget_get_allocated_width (widget);
  height = gtk_widget_get_allocated_height (widget);
  radius = MIN (width, height) / 2.;