testframe.c 6.63 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14
/* testframe.c
 * Copyright (C) 2007  Xan López <xan@gnome.org>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library 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
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
Javier Jardon's avatar
Javier Jardon committed
15
 * License along with this library. If not, see <http://www.gnu.org/licenses/>.
16 17 18 19 20
 */

#include <gtk/gtk.h>
#include <math.h>

21 22
static gint hpadding = 0, vpadding = 0;

23
static void
24
spin_hpadding_cb (GtkSpinButton *spin, gpointer user_data)
25 26
{
  GtkWidget *frame = user_data;
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
  GtkCssProvider *provider;
  GtkStyleContext *context;
  gchar *data;

  context = gtk_widget_get_style_context (frame);
  provider = g_object_get_data (G_OBJECT (frame), "provider");
  if (provider == NULL)
    {
      provider = gtk_css_provider_new ();
      g_object_set_data (G_OBJECT (frame), "provider", provider);
      gtk_style_context_add_provider (context,
                                      GTK_STYLE_PROVIDER (provider),
                                      GTK_STYLE_PROVIDER_PRIORITY_APPLICATION);
    }

42
  hpadding = (gint)gtk_spin_button_get_value (spin);
43 44
  data = g_strdup_printf ("frame > border { padding: %dpx %dpx }",
                          vpadding, hpadding);
45 46 47 48 49

  gtk_css_provider_load_from_data (provider, data, -1, NULL);
  g_free (data);

  gtk_widget_queue_resize (frame);
50 51 52
}

static void
53
spin_vpadding_cb (GtkSpinButton *spin, gpointer user_data)
54 55
{
  GtkWidget *frame = user_data;
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
  GtkCssProvider *provider;
  GtkStyleContext *context;
  gchar *data;

  context = gtk_widget_get_style_context (frame);
  provider = g_object_get_data (G_OBJECT (frame), "provider");
  if (provider == NULL)
    {
      provider = gtk_css_provider_new ();
      g_object_set_data (G_OBJECT (frame), "provider", provider);
      gtk_style_context_add_provider (context,
                                      GTK_STYLE_PROVIDER (provider),
                                      GTK_STYLE_PROVIDER_PRIORITY_APPLICATION);
    }

71
  vpadding = (gint)gtk_spin_button_get_value (spin);
72 73
  data = g_strdup_printf ("frame > border { padding: %dpx %dpx }",
                          vpadding, hpadding);
74 75 76 77 78

  gtk_css_provider_load_from_data (provider, data, -1, NULL);
  g_free (data);

  gtk_widget_queue_resize (frame);
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
}

/* Function to normalize rounding errors in FP arithmetic to
   our desired limits */

#define EPSILON 1e-10

static gdouble
double_normalize (gdouble n)
{
  if (fabs (1.0 - n) < EPSILON)
    n = 1.0;
  else if (n < EPSILON)
    n = 0.0;

  return n;
}

static void
spin_xalign_cb (GtkSpinButton *spin, GtkFrame *frame)
{
100 101 102 103 104 105
  gdouble xalign;
  gfloat yalign;

  xalign = double_normalize (gtk_spin_button_get_value (spin));
  gtk_frame_get_label_align (frame, NULL, &yalign);
  gtk_frame_set_label_align (frame, xalign, yalign);
106 107 108 109 110
}

static void
spin_yalign_cb (GtkSpinButton *spin, GtkFrame *frame)
{
111 112 113 114 115 116
  gdouble yalign;
  gfloat xalign;

  yalign = double_normalize (gtk_spin_button_get_value (spin));
  gtk_frame_get_label_align (frame, &xalign, NULL);
  gtk_frame_set_label_align (frame, xalign, yalign);
117 118
}

119 120 121 122 123 124 125 126 127
static void
draw_border_cb (GtkToggleButton *toggle_button, GtkFrame *frame)
{
  GtkShadowType shadow_type = gtk_toggle_button_get_active (toggle_button)
                              ? GTK_SHADOW_IN : GTK_SHADOW_NONE;

  gtk_frame_set_shadow_type (frame, shadow_type);
}

128 129
int main (int argc, char **argv)
{
130 131 132 133
  GtkWidget *window, *widget;
  GtkBox *vbox;
  GtkFrame *frame;
  GtkGrid *grid;
134
  gfloat xalign, yalign;
135
  gboolean draw_border;
136 137 138 139 140 141 142

  gtk_init (&argc, &argv);

  window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
  gtk_container_set_border_width (GTK_CONTAINER (window), 5);
  gtk_window_set_default_size (GTK_WINDOW (window), 300, 300);

143
  g_signal_connect (window, "delete-event", gtk_main_quit, NULL);
144

145
  vbox = GTK_BOX (gtk_box_new (GTK_ORIENTATION_VERTICAL, 5));
146
  g_object_set (vbox, "margin", 12, NULL);
147
  gtk_container_add (GTK_CONTAINER (window), GTK_WIDGET (vbox));
148

149
  frame = GTK_FRAME (gtk_frame_new ("Test GtkFrame"));
150
  gtk_box_pack_start (vbox, GTK_WIDGET (frame), TRUE, TRUE, 0);
151

152 153
  widget = gtk_button_new_with_label ("Hello!");
  gtk_container_add (GTK_CONTAINER (frame), widget);
154

155
  grid = GTK_GRID (gtk_grid_new ());
156 157
  gtk_grid_set_row_spacing (grid, 12);
  gtk_grid_set_column_spacing (grid, 6);
158
  gtk_box_pack_start (vbox, GTK_WIDGET (grid), FALSE, FALSE, 0);
159

160 161 162 163
  gtk_frame_get_label_align (frame, &xalign, &yalign);

  /* Spin to control :label-xalign */
  widget = gtk_label_new ("label xalign:");
164
  gtk_grid_attach (grid, widget, 0, 0, 1, 1);
165

166 167 168
  widget = gtk_spin_button_new_with_range (0.0, 1.0, 0.1);
  gtk_spin_button_set_value (GTK_SPIN_BUTTON (widget), xalign);
  g_signal_connect (widget, "value-changed", G_CALLBACK (spin_xalign_cb), frame);
169
  gtk_grid_attach (grid, widget, 1, 0, 1, 1);
170

171 172
  /* Spin to control :label-yalign */
  widget = gtk_label_new ("label yalign:");
173
  gtk_grid_attach (grid, widget, 0, 1, 1, 1);
174

175 176 177
  widget = gtk_spin_button_new_with_range (0.0, 1.0, 0.1);
  gtk_spin_button_set_value (GTK_SPIN_BUTTON (widget), yalign);
  g_signal_connect (widget, "value-changed", G_CALLBACK (spin_yalign_cb), frame);
178
  gtk_grid_attach (grid, widget, 1, 1, 1, 1);
179

180 181
  /* Spin to control vertical padding */
  widget = gtk_label_new ("vertical padding:");
182
  gtk_grid_attach (grid, widget, 0, 2, 1, 1);
183

184 185
  widget = gtk_spin_button_new_with_range (0, 250, 1);
  g_signal_connect (widget, "value-changed", G_CALLBACK (spin_vpadding_cb), frame);
186
  gtk_spin_button_set_value (GTK_SPIN_BUTTON (widget), vpadding);
187
  gtk_grid_attach (grid, widget, 1, 2, 1, 1);
188

189 190
  /* Spin to control horizontal padding */
  widget = gtk_label_new ("horizontal padding:");
191
  gtk_grid_attach (grid, widget, 0, 3, 1, 1);
192

193 194
  widget = gtk_spin_button_new_with_range (0, 250, 1);
  g_signal_connect (widget, "value-changed", G_CALLBACK (spin_hpadding_cb), frame);
195
  gtk_spin_button_set_value (GTK_SPIN_BUTTON (widget), hpadding);
196
  gtk_grid_attach (grid, widget, 1, 3, 1, 1);
197

198 199 200 201 202 203 204
  /* CheckButton to control whether to draw border */
  draw_border = gtk_frame_get_shadow_type (frame) != GTK_SHADOW_NONE;
  widget = gtk_check_button_new_with_label ("draw border");
  gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (widget), draw_border);
  g_signal_connect (widget, "toggled", G_CALLBACK (draw_border_cb), frame);
  gtk_grid_attach (grid, widget, 0, 4, 2, 1);

205 206 207 208 209 210
  gtk_widget_show_all (window);

  gtk_main ();

  return 0;
}