mirror of
https://gitlab.gnome.org/GNOME/gtk.git
synced 2025-01-12 05:20:17 +00:00
tests: avoid boxed structs copying where possible
Use the GtkStyleContext accessors for boxed properties where possible, to reduce allocations.
This commit is contained in:
parent
20433efbfa
commit
e5c7daa828
@ -20,13 +20,12 @@ draw_callback (GtkWidget *widget,
|
||||
gpointer data)
|
||||
{
|
||||
GtkStyleContext *context;
|
||||
GdkRGBA *bg;
|
||||
GdkRGBA rgba;
|
||||
|
||||
context = gtk_widget_get_style_context (widget);
|
||||
gtk_style_context_get (context, 0, "background-color", &bg, NULL);
|
||||
gdk_cairo_set_source_rgba (cr, bg);
|
||||
gtk_style_context_get_background_color (context, GTK_STATE_FLAG_NORMAL, &rgba);
|
||||
gdk_cairo_set_source_rgba (cr, &rgba);
|
||||
cairo_paint (cr);
|
||||
gdk_rgba_free (bg);
|
||||
|
||||
return TRUE;
|
||||
}
|
||||
|
@ -356,7 +356,7 @@ test_match (void)
|
||||
GtkCssProvider *provider;
|
||||
GError *error;
|
||||
const gchar *data;
|
||||
GdkRGBA *color;
|
||||
GdkRGBA color;
|
||||
GdkRGBA expected;
|
||||
|
||||
error = NULL;
|
||||
@ -383,18 +383,16 @@ test_match (void)
|
||||
gtk_css_provider_load_from_data (provider, data, -1, &error);
|
||||
g_assert_no_error (error);
|
||||
gtk_style_context_invalidate (context);
|
||||
gtk_style_context_get (context, 0, "color", &color, NULL);
|
||||
g_assert (gdk_rgba_equal (color, &expected));
|
||||
gdk_rgba_free (color);
|
||||
gtk_style_context_get_color (context, GTK_STATE_FLAG_NORMAL, &color);
|
||||
g_assert (gdk_rgba_equal (&color, &expected));
|
||||
|
||||
data = "* { color: #f00 }\n"
|
||||
"GtkButton { color: #fff }";
|
||||
gtk_css_provider_load_from_data (provider, data, -1, &error);
|
||||
g_assert_no_error (error);
|
||||
gtk_style_context_invalidate (context);
|
||||
gtk_style_context_get (context, 0, "color", &color, NULL);
|
||||
g_assert (gdk_rgba_equal (color, &expected));
|
||||
gdk_rgba_free (color);
|
||||
gtk_style_context_get_color (context, GTK_STATE_FLAG_NORMAL, &color);
|
||||
g_assert (gdk_rgba_equal (&color, &expected));
|
||||
|
||||
data = "* { color: #f00 }\n"
|
||||
"GtkButton { color: #fff }\n"
|
||||
@ -402,18 +400,16 @@ test_match (void)
|
||||
gtk_css_provider_load_from_data (provider, data, -1, &error);
|
||||
g_assert_no_error (error);
|
||||
gtk_style_context_invalidate (context);
|
||||
gtk_style_context_get (context, 0, "color", &color, NULL);
|
||||
g_assert (gdk_rgba_equal (color, &expected));
|
||||
gdk_rgba_free (color);
|
||||
gtk_style_context_get_color (context, GTK_STATE_FLAG_NORMAL, &color);
|
||||
g_assert (gdk_rgba_equal (&color, &expected));
|
||||
|
||||
data = "* { color: #f00 }\n"
|
||||
".button { color: #fff }";
|
||||
gtk_css_provider_load_from_data (provider, data, -1, &error);
|
||||
g_assert_no_error (error);
|
||||
gtk_style_context_invalidate (context);
|
||||
gtk_style_context_get (context, 0, "color", &color, NULL);
|
||||
g_assert (gdk_rgba_equal (color, &expected));
|
||||
gdk_rgba_free (color);
|
||||
gtk_style_context_get_color (context, GTK_STATE_FLAG_NORMAL, &color);
|
||||
g_assert (gdk_rgba_equal (&color, &expected));
|
||||
|
||||
data = "* { color: #f00 }\n"
|
||||
"GtkButton { color: #000 }\n"
|
||||
@ -421,9 +417,8 @@ test_match (void)
|
||||
gtk_css_provider_load_from_data (provider, data, -1, &error);
|
||||
g_assert_no_error (error);
|
||||
gtk_style_context_invalidate (context);
|
||||
gtk_style_context_get (context, 0, "color", &color, NULL);
|
||||
g_assert (gdk_rgba_equal (color, &expected));
|
||||
gdk_rgba_free (color);
|
||||
gtk_style_context_get_color (context, GTK_STATE_FLAG_NORMAL, &color);
|
||||
g_assert (gdk_rgba_equal (&color, &expected));
|
||||
|
||||
data = "* { color: #f00 }\n"
|
||||
"GtkButton { color: #000 }\n"
|
||||
@ -431,9 +426,8 @@ test_match (void)
|
||||
gtk_css_provider_load_from_data (provider, data, -1, &error);
|
||||
g_assert_no_error (error);
|
||||
gtk_style_context_invalidate (context);
|
||||
gtk_style_context_get (context, 0, "color", &color, NULL);
|
||||
g_assert (gdk_rgba_equal (color, &expected));
|
||||
gdk_rgba_free (color);
|
||||
gtk_style_context_get_color (context, GTK_STATE_FLAG_NORMAL, &color);
|
||||
g_assert (gdk_rgba_equal (&color, &expected));
|
||||
|
||||
data = "* { color: #f00 }\n"
|
||||
".button { color: #000 }\n"
|
||||
@ -441,9 +435,8 @@ test_match (void)
|
||||
gtk_css_provider_load_from_data (provider, data, -1, &error);
|
||||
g_assert_no_error (error);
|
||||
gtk_style_context_invalidate (context);
|
||||
gtk_style_context_get (context, 0, "color", &color, NULL);
|
||||
g_assert (gdk_rgba_equal (color, &expected));
|
||||
gdk_rgba_free (color);
|
||||
gtk_style_context_get_color (context, GTK_STATE_FLAG_NORMAL, &color);
|
||||
g_assert (gdk_rgba_equal (&color, &expected));
|
||||
|
||||
data = "* { color: #f00 }\n"
|
||||
"* .button { color: #000 }\n"
|
||||
@ -451,9 +444,8 @@ test_match (void)
|
||||
gtk_css_provider_load_from_data (provider, data, -1, &error);
|
||||
g_assert_no_error (error);
|
||||
gtk_style_context_invalidate (context);
|
||||
gtk_style_context_get (context, 0, "color", &color, NULL);
|
||||
g_assert (gdk_rgba_equal (color, &expected));
|
||||
gdk_rgba_free (color);
|
||||
gtk_style_context_get_color (context, GTK_STATE_FLAG_NORMAL, &color);
|
||||
g_assert (gdk_rgba_equal (&color, &expected));
|
||||
|
||||
data = "* { color: #f00 }\n"
|
||||
"GtkWindow .button { color: #000 }\n"
|
||||
@ -461,9 +453,8 @@ test_match (void)
|
||||
gtk_css_provider_load_from_data (provider, data, -1, &error);
|
||||
g_assert_no_error (error);
|
||||
gtk_style_context_invalidate (context);
|
||||
gtk_style_context_get (context, 0, "color", &color, NULL);
|
||||
g_assert (gdk_rgba_equal (color, &expected));
|
||||
gdk_rgba_free (color);
|
||||
gtk_style_context_get_color (context, GTK_STATE_FLAG_NORMAL, &color);
|
||||
g_assert (gdk_rgba_equal (&color, &expected));
|
||||
|
||||
data = "* { color: #f00 }\n"
|
||||
"GtkWindow .button { color: #fff }\n"
|
||||
@ -471,9 +462,8 @@ test_match (void)
|
||||
gtk_css_provider_load_from_data (provider, data, -1, &error);
|
||||
g_assert_no_error (error);
|
||||
gtk_style_context_invalidate (context);
|
||||
gtk_style_context_get (context, 0, "color", &color, NULL);
|
||||
g_assert (gdk_rgba_equal (color, &expected));
|
||||
gdk_rgba_free (color);
|
||||
gtk_style_context_get_color (context, GTK_STATE_FLAG_NORMAL, &color);
|
||||
g_assert (gdk_rgba_equal (&color, &expected));
|
||||
|
||||
g_object_unref (provider);
|
||||
g_object_unref (context);
|
||||
@ -488,7 +478,7 @@ test_style_property (void)
|
||||
GError *error;
|
||||
const gchar *data;
|
||||
gint x;
|
||||
GdkRGBA *color;
|
||||
GdkRGBA color;
|
||||
GdkRGBA expected;
|
||||
|
||||
error = NULL;
|
||||
@ -524,10 +514,9 @@ test_style_property (void)
|
||||
|
||||
gtk_style_context_invalidate (context);
|
||||
|
||||
gtk_style_context_get (context, GTK_STATE_FLAG_PRELIGHT, "color", &color, NULL);
|
||||
gtk_style_context_get_color (context, GTK_STATE_FLAG_PRELIGHT, &color);
|
||||
gdk_rgba_parse (&expected, "#003");
|
||||
g_assert (gdk_rgba_equal (color, &expected));
|
||||
gdk_rgba_free (color);
|
||||
g_assert (gdk_rgba_equal (&color, &expected));
|
||||
|
||||
gtk_style_context_get_style (context, "child-displacement-x", &x, NULL);
|
||||
|
||||
|
@ -477,10 +477,9 @@ create_calendar(void)
|
||||
size = gtk_size_group_new (GTK_SIZE_GROUP_HORIZONTAL);
|
||||
|
||||
context = gtk_widget_get_style_context (calendar);
|
||||
gtk_style_context_get (context, 0, "font", &font_desc, NULL);
|
||||
font_desc = gtk_style_context_get_font (context, GTK_STATE_FLAG_NORMAL);
|
||||
font = pango_font_description_to_string (font_desc);
|
||||
button = gtk_font_button_new_with_font (font);
|
||||
pango_font_description_free (font_desc);
|
||||
g_free (font);
|
||||
|
||||
g_signal_connect (button, "font-set",
|
||||
|
@ -27,7 +27,7 @@ spin_ythickness_cb (GtkSpinButton *spin, gpointer user_data)
|
||||
GtkCssProvider *provider;
|
||||
GtkStyleContext *context;
|
||||
gchar *data;
|
||||
GtkBorder *pad;
|
||||
GtkBorder pad;
|
||||
|
||||
context = gtk_widget_get_style_context (frame);
|
||||
provider = g_object_get_data (G_OBJECT (frame), "provider");
|
||||
@ -40,15 +40,14 @@ spin_ythickness_cb (GtkSpinButton *spin, gpointer user_data)
|
||||
GTK_STYLE_PROVIDER_PRIORITY_APPLICATION);
|
||||
}
|
||||
|
||||
gtk_style_context_get (context, 0, "padding", &pad, NULL);
|
||||
gtk_style_context_get_padding (context, GTK_STATE_FLAG_NORMAL, &pad);
|
||||
|
||||
data = g_strdup_printf ("GtkFrame { padding: %d %d }",
|
||||
pad->top,
|
||||
pad.top,
|
||||
(gint)gtk_spin_button_get_value (spin));
|
||||
|
||||
gtk_css_provider_load_from_data (provider, data, -1, NULL);
|
||||
g_free (data);
|
||||
gtk_border_free (pad);
|
||||
|
||||
gtk_style_context_invalidate (context);
|
||||
gtk_widget_queue_resize (frame);
|
||||
@ -61,7 +60,7 @@ spin_xthickness_cb (GtkSpinButton *spin, gpointer user_data)
|
||||
GtkCssProvider *provider;
|
||||
GtkStyleContext *context;
|
||||
gchar *data;
|
||||
GtkBorder *pad;
|
||||
GtkBorder pad;
|
||||
|
||||
context = gtk_widget_get_style_context (frame);
|
||||
provider = g_object_get_data (G_OBJECT (frame), "provider");
|
||||
@ -74,15 +73,14 @@ spin_xthickness_cb (GtkSpinButton *spin, gpointer user_data)
|
||||
GTK_STYLE_PROVIDER_PRIORITY_APPLICATION);
|
||||
}
|
||||
|
||||
gtk_style_context_get (context, 0, "padding", &pad, NULL);
|
||||
gtk_style_context_get_padding (context, GTK_STATE_FLAG_NORMAL, &pad);
|
||||
|
||||
data = g_strdup_printf ("GtkFrame { padding: %d %d }",
|
||||
(gint)gtk_spin_button_get_value (spin),
|
||||
pad->left);
|
||||
pad.left);
|
||||
|
||||
gtk_css_provider_load_from_data (provider, data, -1, NULL);
|
||||
g_free (data);
|
||||
gtk_border_free (pad);
|
||||
|
||||
gtk_style_context_invalidate (context);
|
||||
gtk_widget_queue_resize (frame);
|
||||
@ -129,7 +127,7 @@ spin_yalign_cb (GtkSpinButton *spin, GtkFrame *frame)
|
||||
int main (int argc, char **argv)
|
||||
{
|
||||
GtkStyleContext *context;
|
||||
GtkBorder *pad;
|
||||
GtkBorder pad;
|
||||
GtkWidget *window, *frame, *xthickness_spin, *ythickness_spin, *vbox;
|
||||
GtkWidget *xalign_spin, *yalign_spin, *button, *table, *label;
|
||||
gfloat xalign, yalign;
|
||||
@ -155,7 +153,7 @@ int main (int argc, char **argv)
|
||||
gtk_box_pack_start (GTK_BOX (vbox), table, FALSE, FALSE, 0);
|
||||
|
||||
context = gtk_widget_get_style_context (frame);
|
||||
gtk_style_context_get (context, 0, "padding", &pad, NULL);
|
||||
gtk_style_context_get_padding (context, GTK_STATE_FLAG_NORMAL, &pad);
|
||||
|
||||
/* Spin to control xthickness */
|
||||
label = gtk_label_new ("xthickness: ");
|
||||
@ -163,7 +161,7 @@ int main (int argc, char **argv)
|
||||
|
||||
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);
|
||||
gtk_spin_button_set_value (GTK_SPIN_BUTTON (xthickness_spin), pad->left);
|
||||
gtk_spin_button_set_value (GTK_SPIN_BUTTON (xthickness_spin), pad.left);
|
||||
gtk_table_attach_defaults (GTK_TABLE (table), xthickness_spin, 1, 2, 0, 1);
|
||||
|
||||
/* Spin to control ythickness */
|
||||
@ -172,11 +170,9 @@ int main (int argc, char **argv)
|
||||
|
||||
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);
|
||||
gtk_spin_button_set_value (GTK_SPIN_BUTTON (ythickness_spin), pad->top);
|
||||
gtk_spin_button_set_value (GTK_SPIN_BUTTON (ythickness_spin), pad.top);
|
||||
gtk_table_attach_defaults (GTK_TABLE (table), ythickness_spin, 1, 2, 1, 2);
|
||||
|
||||
gtk_border_free (pad);
|
||||
|
||||
gtk_frame_get_label_align (GTK_FRAME (frame), &xalign, &yalign);
|
||||
|
||||
/* Spin to control label xalign */
|
||||
|
@ -4832,7 +4832,7 @@ cursor_draw (GtkWidget *widget,
|
||||
{
|
||||
int width, height;
|
||||
GtkStyleContext *context;
|
||||
GdkRGBA *bg;
|
||||
GdkRGBA bg;
|
||||
|
||||
width = gtk_widget_get_allocated_width (widget);
|
||||
height = gtk_widget_get_allocated_height (widget);
|
||||
@ -4846,9 +4846,8 @@ cursor_draw (GtkWidget *widget,
|
||||
cairo_fill (cr);
|
||||
|
||||
context = gtk_widget_get_style_context (widget);
|
||||
gtk_style_context_get (context, 0, "background-color", &bg, NULL);
|
||||
gdk_cairo_set_source_rgba (cr, bg);
|
||||
gdk_rgba_free (bg);
|
||||
gtk_style_context_get_background_color (context, GTK_STATE_FLAG_NORMAL, &bg);
|
||||
gdk_cairo_set_source_rgba (cr, &bg);
|
||||
cairo_rectangle (cr, width / 3, height / 3, width / 3, height / 3);
|
||||
cairo_fill (cr);
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user