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, see <http://www.gnu.org/licenses/>.
16 */
17
18#include <gtk/gtk.h>
19#include <math.h>
20
21/* Function to normalize rounding errors in FP arithmetic to
22 our desired limits */
23
24#define EPSILON 1e-10
25
26static double
27double_normalize (double n)
28{
29 if (fabs (x: 1.0 - n) < EPSILON)
30 n = 1.0;
31 else if (n < EPSILON)
32 n = 0.0;
33
34 return n;
35}
36
37static void
38spin_xalign_cb (GtkSpinButton *spin, GtkFrame *frame)
39{
40 double xalign;
41
42 xalign = double_normalize (n: gtk_spin_button_get_value (spin_button: spin));
43 gtk_frame_set_label_align (frame, xalign);
44}
45
46static void
47quit_cb (GtkWidget *widget,
48 gpointer data)
49{
50 gboolean *done = data;
51
52 *done = TRUE;
53
54 g_main_context_wakeup (NULL);
55}
56
57int main (int argc, char **argv)
58{
59 GtkWidget *window, *widget;
60 GtkBox *vbox;
61 GtkFrame *frame;
62 GtkGrid *grid;
63 float xalign;
64 gboolean done = FALSE;
65
66 gtk_init ();
67
68 window = gtk_window_new ();
69 gtk_window_set_default_size (GTK_WINDOW (window), width: 300, height: 300);
70
71 g_signal_connect (window, "destroy", G_CALLBACK (quit_cb), &done);
72
73 vbox = GTK_BOX (gtk_box_new (GTK_ORIENTATION_VERTICAL, 5));
74 gtk_widget_set_margin_start (GTK_WIDGET (vbox), margin: 12);
75 gtk_widget_set_margin_end (GTK_WIDGET (vbox), margin: 12);
76 gtk_widget_set_margin_top (GTK_WIDGET (vbox), margin: 12);
77 gtk_widget_set_margin_bottom (GTK_WIDGET (vbox), margin: 12);
78 gtk_window_set_child (GTK_WINDOW (window), GTK_WIDGET (vbox));
79
80 frame = GTK_FRAME (gtk_frame_new ("Test GtkFrame"));
81 gtk_widget_set_vexpand (GTK_WIDGET (frame), TRUE);
82 gtk_box_append (GTK_BOX (vbox), GTK_WIDGET (frame));
83
84 widget = gtk_button_new_with_label (label: "Hello!");
85 gtk_frame_set_child (GTK_FRAME (frame), child: widget);
86
87 grid = GTK_GRID (gtk_grid_new ());
88 gtk_grid_set_row_spacing (grid, spacing: 12);
89 gtk_grid_set_column_spacing (grid, spacing: 6);
90 gtk_box_append (GTK_BOX (vbox), GTK_WIDGET (grid));
91
92 xalign = gtk_frame_get_label_align (frame);
93
94 /* Spin to control :label-xalign */
95 widget = gtk_label_new (str: "label xalign:");
96 gtk_grid_attach (grid, child: widget, column: 0, row: 0, width: 1, height: 1);
97
98 widget = gtk_spin_button_new_with_range (min: 0.0, max: 1.0, step: 0.1);
99 gtk_spin_button_set_value (GTK_SPIN_BUTTON (widget), value: xalign);
100 g_signal_connect (widget, "value-changed", G_CALLBACK (spin_xalign_cb), frame);
101 gtk_grid_attach (grid, child: widget, column: 1, row: 0, width: 1, height: 1);
102
103 gtk_widget_show (widget: window);
104
105 while (!done)
106 g_main_context_iteration (NULL, TRUE);
107
108 return 0;
109}
110

source code of gtk/tests/testframe.c