]> Pileus Git - ~andy/gtk/blob - tests/testframe.c
Use gtk_box_new() instead gtk_[v|h]box_new()
[~andy/gtk] / tests / testframe.c
1 /* testframe.c
2  * Copyright (C) 2007  Xan López <xan@gnome.org>
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Library General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Library General Public License for more details.
13  *
14  * You should have received a copy of the GNU Library General Public
15  * License along with this library; if not, write to the
16  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17  * Boston, MA 02111-1307, USA.
18  */
19
20 #include <gtk/gtk.h>
21 #include <math.h>
22
23 static void
24 spin_ythickness_cb (GtkSpinButton *spin, gpointer user_data)
25 {
26   GtkWidget *frame = user_data;
27   GtkRcStyle *rcstyle;
28
29   rcstyle = gtk_rc_style_new ();
30   rcstyle->xthickness = gtk_widget_get_style (frame)->xthickness;
31   rcstyle->ythickness = gtk_spin_button_get_value (spin);
32   gtk_widget_modify_style (frame, rcstyle);
33
34   g_object_unref (rcstyle);
35 }
36
37 static void
38 spin_xthickness_cb (GtkSpinButton *spin, gpointer user_data)
39 {
40   GtkWidget *frame = user_data;
41   GtkRcStyle *rcstyle;
42
43   rcstyle = gtk_rc_style_new ();
44   rcstyle->xthickness = gtk_spin_button_get_value (spin);
45   rcstyle->ythickness = gtk_widget_get_style (frame)->ythickness;
46   gtk_widget_modify_style (frame, rcstyle);
47
48   g_object_unref (rcstyle);
49 }
50
51 /* Function to normalize rounding errors in FP arithmetic to
52    our desired limits */
53
54 #define EPSILON 1e-10
55
56 static gdouble
57 double_normalize (gdouble n)
58 {
59   if (fabs (1.0 - n) < EPSILON)
60     n = 1.0;
61   else if (n < EPSILON)
62     n = 0.0;
63
64   return n;
65 }
66
67 static void
68 spin_xalign_cb (GtkSpinButton *spin, GtkFrame *frame)
69 {
70   gdouble xalign;
71   gfloat yalign;
72
73   xalign = double_normalize (gtk_spin_button_get_value (spin));
74   gtk_frame_get_label_align (frame, NULL, &yalign);
75   gtk_frame_set_label_align (frame, xalign, yalign);
76 }
77
78 static void
79 spin_yalign_cb (GtkSpinButton *spin, GtkFrame *frame)
80 {
81   gdouble yalign;
82   gfloat xalign;
83
84   yalign = double_normalize (gtk_spin_button_get_value (spin));
85   gtk_frame_get_label_align (frame, &xalign, NULL);
86   gtk_frame_set_label_align (frame, xalign, yalign);
87 }
88
89 int main (int argc, char **argv)
90 {
91   GtkStyle *style;
92   GtkWidget *window, *frame, *xthickness_spin, *ythickness_spin, *vbox;
93   GtkWidget *xalign_spin, *yalign_spin, *button, *table, *label;
94   gfloat xalign, yalign;
95
96   gtk_init (&argc, &argv);
97
98   window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
99   gtk_container_set_border_width (GTK_CONTAINER (window), 5);
100   gtk_window_set_default_size (GTK_WINDOW (window), 300, 300);
101
102   g_signal_connect (G_OBJECT (window), "delete-event", gtk_main_quit, NULL);
103
104   vbox = gtk_box_new (GTK_ORIENTATION_VERTICAL, FALSE, 5);
105   gtk_container_add (GTK_CONTAINER (window), vbox);
106
107   frame = gtk_frame_new ("Testing");
108   gtk_box_pack_start (GTK_BOX (vbox), frame, TRUE, TRUE, 0);
109
110   button = gtk_button_new_with_label ("Hello!");
111   gtk_container_add (GTK_CONTAINER (frame), button);
112
113   table = gtk_table_new (4, 2, FALSE);
114   gtk_box_pack_start (GTK_BOX (vbox), table, FALSE, FALSE, 0);
115
116   style = gtk_widget_get_style (frame);
117
118   /* Spin to control xthickness */
119   label = gtk_label_new ("xthickness: ");
120   gtk_table_attach_defaults (GTK_TABLE (table), label, 0, 1, 0, 1);
121
122   xthickness_spin = gtk_spin_button_new_with_range (0, 250, 1);
123   g_signal_connect (G_OBJECT (xthickness_spin), "value-changed", G_CALLBACK (spin_xthickness_cb), frame);
124   gtk_spin_button_set_value (GTK_SPIN_BUTTON (xthickness_spin), style->xthickness);
125   gtk_table_attach_defaults (GTK_TABLE (table), xthickness_spin, 1, 2, 0, 1);
126
127   /* Spin to control ythickness */
128   label = gtk_label_new ("ythickness: ");
129   gtk_table_attach_defaults (GTK_TABLE (table), label, 0, 1, 1, 2);
130
131   ythickness_spin = gtk_spin_button_new_with_range (0, 250, 1);
132   g_signal_connect (G_OBJECT (ythickness_spin), "value-changed", G_CALLBACK (spin_ythickness_cb), frame);
133   gtk_spin_button_set_value (GTK_SPIN_BUTTON (ythickness_spin), style->ythickness);
134   gtk_table_attach_defaults (GTK_TABLE (table), ythickness_spin, 1, 2, 1, 2);
135
136   gtk_frame_get_label_align (GTK_FRAME (frame), &xalign, &yalign);
137
138   /* Spin to control label xalign */
139   label = gtk_label_new ("xalign: ");
140   gtk_table_attach_defaults (GTK_TABLE (table), label, 0, 1, 2, 3);
141
142   xalign_spin = gtk_spin_button_new_with_range (0.0, 1.0, 0.1);
143   g_signal_connect (G_OBJECT (xalign_spin), "value-changed", G_CALLBACK (spin_xalign_cb), frame);
144   gtk_spin_button_set_value (GTK_SPIN_BUTTON (xalign_spin), xalign);
145   gtk_table_attach_defaults (GTK_TABLE (table), xalign_spin, 1, 2, 2, 3);
146
147   /* Spin to control label yalign */
148   label = gtk_label_new ("yalign: ");
149   gtk_table_attach_defaults (GTK_TABLE (table), label, 0, 1, 3, 4);
150
151   yalign_spin = gtk_spin_button_new_with_range (0.0, 1.0, 0.1);
152   g_signal_connect (G_OBJECT (yalign_spin), "value-changed", G_CALLBACK (spin_yalign_cb), frame);
153   gtk_spin_button_set_value (GTK_SPIN_BUTTON (yalign_spin), yalign);
154   gtk_table_attach_defaults (GTK_TABLE (table), yalign_spin, 1, 2, 3, 4);
155
156   gtk_widget_show_all (window);
157
158   gtk_main ();
159
160   return 0;
161 }