2007-06-22 10:16:39 +00:00
|
|
|
/* 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
|
2012-02-27 13:01:10 +00:00
|
|
|
* License along with this library. If not, see <http://www.gnu.org/licenses/>.
|
2007-06-22 10:16:39 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <gtk/gtk.h>
|
|
|
|
#include <math.h>
|
|
|
|
|
|
|
|
static void
|
|
|
|
spin_ythickness_cb (GtkSpinButton *spin, gpointer user_data)
|
|
|
|
{
|
|
|
|
GtkWidget *frame = user_data;
|
2010-11-25 17:49:16 +00:00
|
|
|
GtkCssProvider *provider;
|
|
|
|
GtkStyleContext *context;
|
|
|
|
gchar *data;
|
2011-05-29 14:45:40 +00:00
|
|
|
GtkBorder pad;
|
2010-11-25 17:49:16 +00:00
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2016-01-02 14:40:50 +00:00
|
|
|
gtk_style_context_save (context);
|
|
|
|
gtk_style_context_set_state (context, GTK_STATE_FLAG_NORMAL);
|
2016-10-08 03:07:23 +00:00
|
|
|
gtk_style_context_get_padding (context, &pad);
|
2016-01-02 14:40:50 +00:00
|
|
|
gtk_style_context_restore (context);
|
2010-11-25 17:49:16 +00:00
|
|
|
|
2016-01-02 14:40:50 +00:00
|
|
|
|
|
|
|
data = g_strdup_printf ("GtkFrame { padding: %dpx %dpx }",
|
2011-05-29 14:45:40 +00:00
|
|
|
pad.top,
|
2010-11-25 17:49:16 +00:00
|
|
|
(gint)gtk_spin_button_get_value (spin));
|
|
|
|
|
2016-10-17 14:09:12 +00:00
|
|
|
gtk_css_provider_load_from_data (provider, data, -1);
|
2010-11-25 17:49:16 +00:00
|
|
|
g_free (data);
|
|
|
|
|
|
|
|
gtk_widget_queue_resize (frame);
|
2007-06-22 10:16:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
spin_xthickness_cb (GtkSpinButton *spin, gpointer user_data)
|
|
|
|
{
|
|
|
|
GtkWidget *frame = user_data;
|
2010-11-25 17:49:16 +00:00
|
|
|
GtkCssProvider *provider;
|
|
|
|
GtkStyleContext *context;
|
|
|
|
gchar *data;
|
2011-05-29 14:45:40 +00:00
|
|
|
GtkBorder pad;
|
2010-11-25 17:49:16 +00:00
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2016-01-02 14:40:50 +00:00
|
|
|
gtk_style_context_save (context);
|
|
|
|
gtk_style_context_set_state (context, GTK_STATE_FLAG_NORMAL);
|
2016-10-08 03:07:23 +00:00
|
|
|
gtk_style_context_get_padding (context, &pad);
|
2016-01-02 14:40:50 +00:00
|
|
|
gtk_style_context_restore (context);
|
|
|
|
|
2010-11-25 17:49:16 +00:00
|
|
|
|
2016-01-02 14:40:50 +00:00
|
|
|
data = g_strdup_printf ("GtkFrame { padding: %dpx %dpx }",
|
2010-11-25 17:49:16 +00:00
|
|
|
(gint)gtk_spin_button_get_value (spin),
|
2011-05-29 14:45:40 +00:00
|
|
|
pad.left);
|
2010-11-25 17:49:16 +00:00
|
|
|
|
2016-10-17 14:09:12 +00:00
|
|
|
gtk_css_provider_load_from_data (provider, data, -1);
|
2010-11-25 17:49:16 +00:00
|
|
|
g_free (data);
|
|
|
|
|
|
|
|
gtk_widget_queue_resize (frame);
|
2007-06-22 10:16:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* 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)
|
|
|
|
{
|
2010-06-05 01:29:31 +00:00
|
|
|
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);
|
2007-06-22 10:16:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
spin_yalign_cb (GtkSpinButton *spin, GtkFrame *frame)
|
|
|
|
{
|
2010-06-05 01:29:31 +00:00
|
|
|
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);
|
2007-06-22 10:16:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int main (int argc, char **argv)
|
|
|
|
{
|
2010-11-25 17:49:16 +00:00
|
|
|
GtkStyleContext *context;
|
2011-05-29 14:45:40 +00:00
|
|
|
GtkBorder pad;
|
2007-06-22 10:16:39 +00:00
|
|
|
GtkWidget *window, *frame, *xthickness_spin, *ythickness_spin, *vbox;
|
2011-09-28 00:12:05 +00:00
|
|
|
GtkWidget *xalign_spin, *yalign_spin, *button, *grid, *label;
|
2010-06-05 01:29:31 +00:00
|
|
|
gfloat xalign, yalign;
|
2007-06-22 10:16:39 +00:00
|
|
|
|
|
|
|
gtk_init (&argc, &argv);
|
|
|
|
|
|
|
|
window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
|
|
|
|
gtk_window_set_default_size (GTK_WINDOW (window), 300, 300);
|
|
|
|
|
|
|
|
g_signal_connect (G_OBJECT (window), "delete-event", gtk_main_quit, NULL);
|
|
|
|
|
2010-10-31 17:07:20 +00:00
|
|
|
vbox = gtk_box_new (GTK_ORIENTATION_VERTICAL, 5);
|
2007-06-22 10:16:39 +00:00
|
|
|
gtk_container_add (GTK_CONTAINER (window), vbox);
|
|
|
|
|
|
|
|
frame = gtk_frame_new ("Testing");
|
2016-10-02 15:37:22 +00:00
|
|
|
gtk_box_pack_start (GTK_BOX (vbox), frame, TRUE, TRUE);
|
2007-06-22 10:16:39 +00:00
|
|
|
|
|
|
|
button = gtk_button_new_with_label ("Hello!");
|
|
|
|
gtk_container_add (GTK_CONTAINER (frame), button);
|
|
|
|
|
2011-09-28 00:12:05 +00:00
|
|
|
grid = gtk_grid_new ();
|
2016-10-02 15:37:22 +00:00
|
|
|
gtk_box_pack_start (GTK_BOX (vbox), grid, FALSE, FALSE);
|
2007-06-22 10:16:39 +00:00
|
|
|
|
2010-11-25 17:49:16 +00:00
|
|
|
context = gtk_widget_get_style_context (frame);
|
2016-01-02 14:40:50 +00:00
|
|
|
gtk_style_context_save (context);
|
|
|
|
gtk_style_context_set_state (context, GTK_STATE_FLAG_NORMAL);
|
2016-10-08 03:07:23 +00:00
|
|
|
gtk_style_context_get_padding (context, &pad);
|
2016-01-02 14:40:50 +00:00
|
|
|
gtk_style_context_restore (context);
|
2010-08-15 22:34:17 +00:00
|
|
|
|
2007-06-22 10:16:39 +00:00
|
|
|
/* Spin to control xthickness */
|
|
|
|
label = gtk_label_new ("xthickness: ");
|
2011-09-28 00:12:05 +00:00
|
|
|
gtk_grid_attach (GTK_GRID (grid), label, 0, 0, 1, 1);
|
2007-06-22 10:16:39 +00:00
|
|
|
|
|
|
|
xthickness_spin = gtk_spin_button_new_with_range (0, 250, 1);
|
|
|
|
g_signal_connect (G_OBJECT (xthickness_spin), "value-changed", G_CALLBACK (spin_xthickness_cb), frame);
|
2011-05-29 14:45:40 +00:00
|
|
|
gtk_spin_button_set_value (GTK_SPIN_BUTTON (xthickness_spin), pad.left);
|
2011-09-28 00:12:05 +00:00
|
|
|
gtk_grid_attach (GTK_GRID (grid), xthickness_spin, 1, 0, 1, 1);
|
2007-06-22 10:16:39 +00:00
|
|
|
|
|
|
|
/* Spin to control ythickness */
|
|
|
|
label = gtk_label_new ("ythickness: ");
|
2011-09-28 00:12:05 +00:00
|
|
|
gtk_grid_attach (GTK_GRID (grid), label, 0, 1, 1, 1);
|
2007-06-22 10:16:39 +00:00
|
|
|
|
|
|
|
ythickness_spin = gtk_spin_button_new_with_range (0, 250, 1);
|
|
|
|
g_signal_connect (G_OBJECT (ythickness_spin), "value-changed", G_CALLBACK (spin_ythickness_cb), frame);
|
2011-05-29 14:45:40 +00:00
|
|
|
gtk_spin_button_set_value (GTK_SPIN_BUTTON (ythickness_spin), pad.top);
|
2011-09-28 00:12:05 +00:00
|
|
|
gtk_grid_attach (GTK_GRID (grid), ythickness_spin, 1, 1, 1, 1);
|
2007-06-22 10:16:39 +00:00
|
|
|
|
2010-06-05 01:29:31 +00:00
|
|
|
gtk_frame_get_label_align (GTK_FRAME (frame), &xalign, &yalign);
|
|
|
|
|
2007-06-22 10:16:39 +00:00
|
|
|
/* Spin to control label xalign */
|
|
|
|
label = gtk_label_new ("xalign: ");
|
2011-09-28 00:12:05 +00:00
|
|
|
gtk_grid_attach (GTK_GRID (grid), label, 0, 2, 1, 1);
|
2007-06-22 10:16:39 +00:00
|
|
|
|
|
|
|
xalign_spin = gtk_spin_button_new_with_range (0.0, 1.0, 0.1);
|
|
|
|
g_signal_connect (G_OBJECT (xalign_spin), "value-changed", G_CALLBACK (spin_xalign_cb), frame);
|
2010-06-05 01:29:31 +00:00
|
|
|
gtk_spin_button_set_value (GTK_SPIN_BUTTON (xalign_spin), xalign);
|
2011-09-28 00:12:05 +00:00
|
|
|
gtk_grid_attach (GTK_GRID (grid), xalign_spin, 1, 2, 1, 1);
|
2007-06-22 10:16:39 +00:00
|
|
|
|
|
|
|
/* Spin to control label yalign */
|
|
|
|
label = gtk_label_new ("yalign: ");
|
2011-09-28 00:12:05 +00:00
|
|
|
gtk_grid_attach (GTK_GRID (grid), label, 0, 3, 1, 1);
|
2007-06-22 10:16:39 +00:00
|
|
|
|
|
|
|
yalign_spin = gtk_spin_button_new_with_range (0.0, 1.0, 0.1);
|
|
|
|
g_signal_connect (G_OBJECT (yalign_spin), "value-changed", G_CALLBACK (spin_yalign_cb), frame);
|
2010-06-05 01:29:31 +00:00
|
|
|
gtk_spin_button_set_value (GTK_SPIN_BUTTON (yalign_spin), yalign);
|
2011-09-28 00:12:05 +00:00
|
|
|
gtk_grid_attach (GTK_GRID (grid), yalign_spin, 1, 3, 1, 1);
|
2007-06-22 10:16:39 +00:00
|
|
|
|
|
|
|
gtk_widget_show_all (window);
|
|
|
|
|
|
|
|
gtk_main ();
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|