testbbox.c 4.79 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14
/*
 * Copyright (C) 2006 Nokia Corporation.
 * Author: Xan Lopez <xan.lopez@nokia.com>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public License
 * version 2.1 as published by the Free Software Foundation.
 *
 * 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/>.
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
 *
 */

#include <gtk/gtk.h>

#define N_BUTTONS 3

GtkWidget *bbox = NULL;
GtkWidget *hbbox = NULL, *vbbox = NULL;

static const char* styles[] = { "GTK_BUTTONBOX_DEFAULT_STYLE",
				"GTK_BUTTONBOX_SPREAD",
				"GTK_BUTTONBOX_EDGE",
				"GTK_BUTTONBOX_START",
				"GTK_BUTTONBOX_END",
				"GTK_BUTTONBOX_CENTER",
				NULL};

static const char* types[] = { "GtkHButtonBox",
			       "GtkVButtonBox",
			       NULL};

static void
39
populate_combo_with (GtkComboBoxText *combo, const char** elements)
40 41 42 43
{
  int i;
  
  for (i = 0; elements[i] != NULL; i++) {
44
    gtk_combo_box_text_append_text (combo, elements[i]);
45 46
  }
  
47
  gtk_combo_box_set_active (GTK_COMBO_BOX (combo), 0);
48 49 50
}

static void
51
combo_changed_cb (GtkComboBoxText *combo,
52 53 54 55 56
		  gpointer user_data)
{
  char *text;
  int i;
  
57
  text = gtk_combo_box_text_get_active_text (combo);
58 59 60 61 62 63 64 65 66 67 68 69 70
  
  for (i = 0; styles[i]; i++) {
    if (g_str_equal (text, styles[i])) {
      gtk_button_box_set_layout (GTK_BUTTON_BOX (bbox), (GtkButtonBoxStyle)i);
    }
  }
}

static void
reparent_widget (GtkWidget *widget,
		 GtkWidget *old_parent,
		 GtkWidget *new_parent)
{
71
  g_object_ref (widget);
72 73
  gtk_container_remove (GTK_CONTAINER (old_parent), widget);
  gtk_container_add (GTK_CONTAINER (new_parent), widget);
74
  g_object_unref (widget);
75 76 77
}

static void
78
combo_types_changed_cb (GtkComboBoxText *combo,
79 80 81 82 83 84 85
			GtkWidget **buttons)
{
  int i;
  char *text;
  GtkWidget *old_parent, *new_parent;
  GtkButtonBoxStyle style;
  
86
  text = gtk_combo_box_text_get_active_text (combo);
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132
  
  if (g_str_equal (text, "GtkHButtonBox")) {
    old_parent = vbbox;
    new_parent = hbbox;
  } else {
    old_parent = hbbox;
    new_parent = vbbox;
  }
  
  bbox = new_parent;
  
  for (i = 0; i < N_BUTTONS; i++) {
    reparent_widget (buttons[i], old_parent, new_parent);
  }
  
  gtk_widget_hide (old_parent);
  style = gtk_button_box_get_layout (GTK_BUTTON_BOX (old_parent));
  gtk_button_box_set_layout (GTK_BUTTON_BOX (new_parent), style);
  gtk_widget_show (new_parent);
}

static void
option_cb (GtkToggleButton *option,
	   GtkWidget *button)
{
  gboolean active = gtk_toggle_button_get_active (option);
  
  gtk_button_box_set_child_secondary (GTK_BUTTON_BOX (bbox),
				      button, active);
}

static const char* strings[] = { "Ok", "Cancel", "Help" };

int
main (int    argc,
      char **argv)
{
  GtkWidget *window, *buttons[N_BUTTONS];
  GtkWidget *vbox, *hbox, *combo_styles, *combo_types, *option;
  int i;
  
  gtk_init (&argc, &argv);
  
  window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
  g_signal_connect (G_OBJECT (window), "delete-event", G_CALLBACK (gtk_main_quit), NULL);
  
133
  vbox = gtk_box_new (GTK_ORIENTATION_VERTICAL, 0);
134 135 136 137
  gtk_container_add (GTK_CONTAINER (window), vbox);
  
  /* GtkHButtonBox */
  
138
  hbbox = gtk_button_box_new (GTK_ORIENTATION_HORIZONTAL);
139 140 141 142 143 144 145 146 147 148
  gtk_box_pack_start (GTK_BOX (vbox), hbbox, TRUE, TRUE, 5);
  
  for (i = 0; i < N_BUTTONS; i++) {
    buttons[i] = gtk_button_new_with_label (strings[i]);
    gtk_container_add (GTK_CONTAINER (hbbox), buttons[i]);
  }
  
  bbox = hbbox;
  
  /* GtkVButtonBox */
149
  vbbox = gtk_button_box_new (GTK_ORIENTATION_VERTICAL);
150 151 152 153
  gtk_box_pack_start (GTK_BOX (vbox), vbbox, TRUE, TRUE, 5);
  
  /* Options */
  
154
  hbox = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 0);
155 156
  gtk_box_pack_start (GTK_BOX (vbox), hbox, FALSE, FALSE, 0);
  
157 158
  combo_types = gtk_combo_box_text_new ();
  populate_combo_with (GTK_COMBO_BOX_TEXT (combo_types), types);
159 160 161
  g_signal_connect (G_OBJECT (combo_types), "changed", G_CALLBACK (combo_types_changed_cb), buttons);
  gtk_box_pack_start (GTK_BOX (hbox), combo_types, TRUE, TRUE, 0);
  
162 163
  combo_styles = gtk_combo_box_text_new ();
  populate_combo_with (GTK_COMBO_BOX_TEXT (combo_styles), styles);
164 165 166 167 168 169 170 171 172 173 174 175 176 177 178
  g_signal_connect (G_OBJECT (combo_styles), "changed", G_CALLBACK (combo_changed_cb), NULL);
  gtk_box_pack_start (GTK_BOX (hbox), combo_styles, TRUE, TRUE, 0);
  
  option = gtk_check_button_new_with_label ("Help is secondary");
  g_signal_connect (G_OBJECT (option), "toggled", G_CALLBACK (option_cb), buttons[N_BUTTONS - 1]);
  
  gtk_box_pack_start (GTK_BOX (hbox), option, FALSE, FALSE, 0);
  
  gtk_widget_show_all (window);
  gtk_widget_hide (vbbox);
  
  gtk_main ();
  
  return 0;
}