From e5c7daa828977999ac8b8c2bd64ef3a6077ed23d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Javier=20Jard=C3=B3n?= Date: Sun, 29 May 2011 16:45:40 +0200 Subject: [PATCH] tests: avoid boxed structs copying where possible Use the GtkStyleContext accessors for boxed properties where possible, to reduce allocations. --- demos/gtk-demo/colorsel.c | 7 ++--- gtk/tests/stylecontext.c | 59 ++++++++++++++++----------------------- tests/testcalendar.c | 3 +- tests/testframe.c | 24 +++++++--------- tests/testgtk.c | 7 ++--- 5 files changed, 41 insertions(+), 59 deletions(-) diff --git a/demos/gtk-demo/colorsel.c b/demos/gtk-demo/colorsel.c index 14ea9107c4..f9c119fbab 100644 --- a/demos/gtk-demo/colorsel.c +++ b/demos/gtk-demo/colorsel.c @@ -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; } diff --git a/gtk/tests/stylecontext.c b/gtk/tests/stylecontext.c index a6f50276ea..f68128ebe8 100644 --- a/gtk/tests/stylecontext.c +++ b/gtk/tests/stylecontext.c @@ -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); diff --git a/tests/testcalendar.c b/tests/testcalendar.c index a0292760a2..a71b5cc886 100644 --- a/tests/testcalendar.c +++ b/tests/testcalendar.c @@ -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", diff --git a/tests/testframe.c b/tests/testframe.c index 0070f5a3a9..189b92e288 100644 --- a/tests/testframe.c +++ b/tests/testframe.c @@ -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 */ diff --git a/tests/testgtk.c b/tests/testgtk.c index e34b77b2b9..d5c7427f7a 100644 --- a/tests/testgtk.c +++ b/tests/testgtk.c @@ -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);