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
|
2017-03-06 23:03:37 +00:00
|
|
|
spin_hpadding_cb (GtkSpinButton *spin, gpointer user_data)
|
2007-06-22 10:16:39 +00:00
|
|
|
{
|
|
|
|
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
|
|
|
|
2016-10-27 12:04:44 +00:00
|
|
|
data = g_strdup_printf ("frame { 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
|
2017-03-06 23:03:37 +00:00
|
|
|
spin_vpadding_cb (GtkSpinButton *spin, gpointer user_data)
|
2007-06-22 10:16:39 +00:00
|
|
|
{
|
|
|
|
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-10-27 12:04:44 +00:00
|
|
|
data = g_strdup_printf ("frame { 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;
|
|
|
|
|
|
|
|
xalign = double_normalize (gtk_spin_button_get_value (spin));
|
2018-03-01 19:53:53 +00:00
|
|
|
gtk_frame_set_label_align (frame, xalign);
|
2007-06-22 10:16:39 +00:00
|
|
|
}
|
|
|
|
|
2017-03-06 23:12:20 +00:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2020-02-10 03:24:47 +00:00
|
|
|
static void
|
|
|
|
quit_cb (GtkWidget *widget,
|
|
|
|
gpointer data)
|
|
|
|
{
|
|
|
|
gboolean *done = data;
|
|
|
|
|
|
|
|
*done = TRUE;
|
|
|
|
|
|
|
|
g_main_context_wakeup (NULL);
|
|
|
|
}
|
|
|
|
|
2007-06-22 10:16:39 +00:00
|
|
|
int main (int argc, char **argv)
|
|
|
|
{
|
2017-03-06 22:55:49 +00:00
|
|
|
GtkWidget *window, *widget;
|
|
|
|
GtkBox *vbox;
|
|
|
|
GtkFrame *frame;
|
|
|
|
GtkGrid *grid;
|
2018-03-01 19:53:53 +00:00
|
|
|
gfloat xalign;
|
2017-03-06 23:12:20 +00:00
|
|
|
gboolean draw_border;
|
2020-02-10 03:24:47 +00:00
|
|
|
gboolean done = FALSE;
|
2007-06-22 10:16:39 +00:00
|
|
|
|
2016-12-28 13:53:22 +00:00
|
|
|
gtk_init ();
|
2007-06-22 10:16:39 +00:00
|
|
|
|
|
|
|
window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
|
|
|
|
gtk_window_set_default_size (GTK_WINDOW (window), 300, 300);
|
|
|
|
|
2020-02-10 03:24:47 +00:00
|
|
|
g_signal_connect (window, "destroy", G_CALLBACK (quit_cb), &done);
|
2007-06-22 10:16:39 +00:00
|
|
|
|
2017-03-06 22:55:49 +00:00
|
|
|
vbox = GTK_BOX (gtk_box_new (GTK_ORIENTATION_VERTICAL, 5));
|
2017-03-06 23:12:20 +00:00
|
|
|
g_object_set (vbox, "margin", 12, NULL);
|
2017-03-06 22:55:49 +00:00
|
|
|
gtk_container_add (GTK_CONTAINER (window), GTK_WIDGET (vbox));
|
2007-06-22 10:16:39 +00:00
|
|
|
|
2017-03-06 23:03:37 +00:00
|
|
|
frame = GTK_FRAME (gtk_frame_new ("Test GtkFrame"));
|
2017-04-21 20:34:36 +00:00
|
|
|
gtk_widget_set_vexpand (GTK_WIDGET (frame), TRUE);
|
2018-12-26 07:20:34 +00:00
|
|
|
gtk_container_add (GTK_CONTAINER (vbox), GTK_WIDGET (frame));
|
2007-06-22 10:16:39 +00:00
|
|
|
|
2017-03-06 22:39:01 +00:00
|
|
|
widget = gtk_button_new_with_label ("Hello!");
|
|
|
|
gtk_container_add (GTK_CONTAINER (frame), widget);
|
2007-06-22 10:16:39 +00:00
|
|
|
|
2017-03-06 22:55:49 +00:00
|
|
|
grid = GTK_GRID (gtk_grid_new ());
|
2017-03-06 23:03:37 +00:00
|
|
|
gtk_grid_set_row_spacing (grid, 12);
|
|
|
|
gtk_grid_set_column_spacing (grid, 6);
|
2018-12-26 07:20:34 +00:00
|
|
|
gtk_container_add (GTK_CONTAINER (vbox), GTK_WIDGET (grid));
|
2007-06-22 10:16:39 +00:00
|
|
|
|
2018-03-01 19:53:53 +00:00
|
|
|
xalign = gtk_frame_get_label_align (frame);
|
2017-03-06 23:03:37 +00:00
|
|
|
|
|
|
|
/* Spin to control :label-xalign */
|
|
|
|
widget = gtk_label_new ("label xalign:");
|
2017-03-06 22:55:49 +00:00
|
|
|
gtk_grid_attach (grid, widget, 0, 0, 1, 1);
|
2007-06-22 10:16:39 +00:00
|
|
|
|
2017-03-06 23:03:37 +00:00
|
|
|
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);
|
2017-03-06 22:55:49 +00:00
|
|
|
gtk_grid_attach (grid, widget, 1, 0, 1, 1);
|
2007-06-22 10:16:39 +00:00
|
|
|
|
2017-03-06 23:03:37 +00:00
|
|
|
/* Spin to control vertical padding */
|
|
|
|
widget = gtk_label_new ("vertical padding:");
|
2017-03-06 23:23:31 +00:00
|
|
|
gtk_grid_attach (grid, widget, 0, 1, 1, 1);
|
2007-06-22 10:16:39 +00:00
|
|
|
|
2017-03-06 23:03:37 +00:00
|
|
|
widget = gtk_spin_button_new_with_range (0, 250, 1);
|
|
|
|
g_signal_connect (widget, "value-changed", G_CALLBACK (spin_vpadding_cb), frame);
|
2017-03-06 23:59:17 +00:00
|
|
|
gtk_spin_button_set_value (GTK_SPIN_BUTTON (widget), 0);
|
2017-03-06 23:23:31 +00:00
|
|
|
gtk_grid_attach (grid, widget, 1, 1, 1, 1);
|
2007-06-22 10:16:39 +00:00
|
|
|
|
2017-03-06 23:03:37 +00:00
|
|
|
/* Spin to control horizontal padding */
|
|
|
|
widget = gtk_label_new ("horizontal padding:");
|
2017-03-06 23:23:31 +00:00
|
|
|
gtk_grid_attach (grid, widget, 0, 2, 1, 1);
|
2007-06-22 10:16:39 +00:00
|
|
|
|
2017-03-06 23:03:37 +00:00
|
|
|
widget = gtk_spin_button_new_with_range (0, 250, 1);
|
|
|
|
g_signal_connect (widget, "value-changed", G_CALLBACK (spin_hpadding_cb), frame);
|
2017-03-06 23:59:17 +00:00
|
|
|
gtk_spin_button_set_value (GTK_SPIN_BUTTON (widget), 0);
|
2017-03-06 23:23:31 +00:00
|
|
|
gtk_grid_attach (grid, widget, 1, 2, 1, 1);
|
2007-06-22 10:16:39 +00:00
|
|
|
|
2017-03-06 23:12:20 +00:00
|
|
|
/* 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);
|
2017-03-06 23:23:31 +00:00
|
|
|
gtk_grid_attach (grid, widget, 0, 3, 1, 1);
|
2017-03-06 23:12:20 +00:00
|
|
|
|
2017-01-19 09:02:04 +00:00
|
|
|
gtk_widget_show (window);
|
2007-06-22 10:16:39 +00:00
|
|
|
|
2020-02-10 03:24:47 +00:00
|
|
|
while (!done)
|
|
|
|
g_main_context_iteration (NULL, TRUE);
|
2007-06-22 10:16:39 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|