[gtk/wip/otte/css: 1/2] stylecontext: Get rid of gtk_style_context_get()



commit 758a9082e87b89f415de5341c4e8a543f3878098
Author: Benjamin Otte <otte redhat com>
Date:   Tue Jan 28 00:27:36 2020 +0100

    stylecontext: Get rid of gtk_style_context_get()
    
    ... and replace all the calls to it with
    gtk_style_context_peek_property().
    
    All the calls are hacks that need to go away btw.

 docs/reference/gtk/gtk4-sections.txt |   3 -
 gtk/gtkcellrenderertext.c            |   5 +-
 gtk/gtkflowbox.c                     |   6 +-
 gtk/gtkimcontextime.c                |   7 +-
 gtk/gtkstylecontext.c                | 180 +++--------------------------------
 gtk/gtkstylecontext.h                |  13 ---
 gtk/gtktexthandle.c                  |  11 ++-
 gtk/gtktextlayout.c                  |  10 +-
 gtk/gtktextutil.c                    |  25 ++---
 gtk/gtktextview.c                    |  13 +--
 10 files changed, 45 insertions(+), 228 deletions(-)
---
diff --git a/docs/reference/gtk/gtk4-sections.txt b/docs/reference/gtk/gtk4-sections.txt
index 9e396b17e3..b4a737d2f0 100644
--- a/docs/reference/gtk/gtk4-sections.txt
+++ b/docs/reference/gtk/gtk4-sections.txt
@@ -4801,12 +4801,9 @@ GtkStyleContext
 gtk_style_context_new
 gtk_style_context_add_provider
 gtk_style_context_add_provider_for_display
-gtk_style_context_get
 gtk_style_context_get_parent
-gtk_style_context_get_property
 gtk_style_context_get_display
 gtk_style_context_get_state
-gtk_style_context_get_valist
 gtk_style_context_get_color
 gtk_style_context_get_border
 gtk_style_context_get_padding
diff --git a/gtk/gtkcellrenderertext.c b/gtk/gtkcellrenderertext.c
index 7ed41be696..3a57b66d5a 100644
--- a/gtk/gtkcellrenderertext.c
+++ b/gtk/gtkcellrenderertext.c
@@ -19,6 +19,7 @@
 
 #include "gtkcellrenderertext.h"
 
+#include "gtkcssnumbervalueprivate.h"
 #include "gtkeditable.h"
 #include "gtkentry.h"
 #include "gtkentryprivate.h"
@@ -27,7 +28,7 @@
 #include "gtkprivate.h"
 #include "gtksizerequest.h"
 #include "gtksnapshot.h"
-#include "gtkstylecontext.h"
+#include "gtkstylecontextprivate.h"
 #include "gtktreeprivate.h"
 
 #include "a11y/gtktextcellaccessible.h"
@@ -1631,7 +1632,7 @@ get_size (GtkCellRenderer    *cell,
 
       style_context = gtk_widget_get_style_context (widget);
 
-      gtk_style_context_get (style_context, "font", &font_desc, NULL);
+      font_desc = gtk_css_style_get_pango_font (gtk_style_context_lookup_style (style_context));
       pango_font_description_merge_static (font_desc, priv->font, TRUE);
 
       if (priv->scale_set)
diff --git a/gtk/gtkflowbox.c b/gtk/gtkflowbox.c
index 1844943f80..9a422593dd 100644
--- a/gtk/gtkflowbox.c
+++ b/gtk/gtkflowbox.c
@@ -80,6 +80,7 @@
 #include "gtkadjustment.h"
 #include "gtkbindings.h"
 #include "gtkcontainerprivate.h"
+#include "gtkcsscolorvalueprivate.h"
 #include "gtkcssnodeprivate.h"
 #include "gtkgesturedrag.h"
 #include "gtkgestureclick.h"
@@ -2385,7 +2386,7 @@ gtk_flow_box_snapshot (GtkWidget   *widget,
         {
           cairo_path_t *path;
           GtkBorder border;
-          GdkRGBA *border_color;
+          const GdkRGBA *border_color;
 
           if (vertical)
             path_from_vertical_line_rects (cr, (GdkRectangle *)lines->data, lines->len);
@@ -2405,13 +2406,12 @@ gtk_flow_box_snapshot (GtkWidget   *widget,
           cairo_append_path (cr, path);
           cairo_path_destroy (path);
 
-          gtk_style_context_get (context, "border-color", &border_color, NULL);
+          border_color = gtk_css_color_value_get_rgba (_gtk_style_context_peek_property (context, 
GTK_CSS_PROPERTY_BORDER_TOP_COLOR));
           gtk_style_context_get_border (context, &border);
 
           cairo_set_line_width (cr, border.left);
           gdk_cairo_set_source_rgba (cr, border_color);
           cairo_stroke (cr);
-          gdk_rgba_free (border_color);
         }
       g_array_free (lines, TRUE);
 
diff --git a/gtk/gtkimcontextime.c b/gtk/gtkimcontextime.c
index 241ac3d99f..aac0019853 100644
--- a/gtk/gtkimcontextime.c
+++ b/gtk/gtkimcontextime.c
@@ -869,7 +869,6 @@ gtk_im_context_ime_set_preedit_font (GtkIMContext *context)
   PangoContext *pango_context;
   PangoFont *font;
   LOGFONT *logfont;
-  GtkStyleContext *style;
   PangoFontDescription *font_desc;
 
   g_return_if_fail (GTK_IS_IM_CONTEXT_IME (context));
@@ -923,11 +922,7 @@ gtk_im_context_ime_set_preedit_font (GtkIMContext *context)
       lang = ""; break;
     }
 
-  style = gtk_widget_get_style_context (widget);
-  gtk_style_context_get (style,
-                         "font",
-                         &font_desc,
-                         NULL);
+  font_desc = gtk_css_style_get_pango_font (gtk_style_context_lookup_style (gtk_widget_get_style_context 
(widget)));
 
   if (lang[0])
     {
diff --git a/gtk/gtkstylecontext.c b/gtk/gtkstylecontext.c
index f99783285e..ad22895e90 100644
--- a/gtk/gtkstylecontext.c
+++ b/gtk/gtkstylecontext.c
@@ -617,142 +617,6 @@ gtk_style_context_remove_provider_for_display (GdkDisplay       *display,
   _gtk_style_cascade_remove_provider (cascade, provider);
 }
 
-static GtkCssValue *
-gtk_style_context_query_func (guint    id,
-                              gpointer values)
-{
-  return gtk_css_style_get_value (values, id);
-}
-
-/**
- * gtk_style_context_get_property:
- * @context: a #GtkStyleContext
- * @property: style property name
- * @value: (out) (transfer full):  return location for the style property value
- *
- * Gets a style property from @context for the current state.
- *
- * Note that not all CSS properties that are supported by GTK+ can be
- * retrieved in this way, since they may not be representable as #GValue.
- * GTK+ defines macros for a number of properties that can be used
- * with this function.
- *
- * When @value is no longer needed, g_value_unset() must be called
- * to free any allocated memory.
- **/
-void
-gtk_style_context_get_property (GtkStyleContext *context,
-                                const gchar     *property,
-                                GValue          *value)
-{
-  GtkStyleContextPrivate *priv = gtk_style_context_get_instance_private (context);
-  GtkStyleProperty *prop;
-
-  g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
-  g_return_if_fail (property != NULL);
-  g_return_if_fail (value != NULL);
-
-  prop = _gtk_style_property_lookup (property);
-  if (prop == NULL)
-    {
-      g_warning ("Style property \"%s\" is not registered", property);
-      return;
-    }
-  if (_gtk_style_property_get_value_type (prop) == G_TYPE_NONE)
-    {
-      g_warning ("Style property \"%s\" is not gettable", property);
-      return;
-    }
-
-  _gtk_style_property_query (prop,
-                             value,
-                             gtk_style_context_query_func,
-                             gtk_css_node_get_style (priv->cssnode));
-}
-
-/**
- * gtk_style_context_get_valist:
- * @context: a #GtkStyleContext
- * @first_property_name: Name of the first property
- * @args: va_list of property name/return location pairs, followed by %NULL
- *
- * Retrieves several style property values from @context for a given state.
- *
- * See gtk_style_context_get_property() for details.
- *
- * As with g_object_get(), a copy is made of the property contents for
- * pointer-valued properties, and the caller is responsible for freeing the
- * memory in the appropriate manner for the type. For example, by calling
- * g_free() or g_object_unref(). Non-pointer-valued properties, such as
- * integers, are returned by value and do not need to be freed.
- */
-void
-gtk_style_context_get_valist (GtkStyleContext *context,
-                              const char      *first_property_name,
-                              va_list          args)
-{
-  const gchar *property_name;
-
-  g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
-  g_return_if_fail (first_property_name != NULL);
-
-  property_name = first_property_name;
-
-  while (property_name)
-    {
-      gchar *error = NULL;
-      GValue value = G_VALUE_INIT;
-
-      gtk_style_context_get_property (context,
-                                      property_name,
-                                      &value);
-
-      G_VALUE_LCOPY (&value, args, 0, &error);
-      g_value_unset (&value);
-
-      if (error)
-        {
-          g_warning ("Could not get style property \"%s\": %s", property_name, error);
-          g_free (error);
-          break;
-        }
-
-      property_name = va_arg (args, const gchar *);
-    }
-}
-
-/**
- * gtk_style_context_get:
- * @context: a #GtkStyleContext
- * @first_property_name: Name of the first property
- * @...: property name /return value pairs, followed by %NULL
- *
- * Retrieves several style property values from @context for a
- * given state.
- *
- * See gtk_style_context_get_property() for details.
- *
- * As with g_object_get(), a copy is made of the property contents for
- * pointer-valued properties, and the caller is responsible for freeing the
- * memory in the appropriate manner for the type. For example, by calling
- * g_free() or g_object_unref(). Non-pointer-valued properties, such as
- * integers, are returned by value and do not need to be freed.
- */
-void
-gtk_style_context_get (GtkStyleContext *context,
-                       const char      *first_property_name,
-                       ...)
-{
-  va_list args;
-
-  g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
-  g_return_if_fail (first_property_name != NULL);
-
-  va_start (args, first_property_name);
-  gtk_style_context_get_valist (context, first_property_name, args);
-  va_end (args);
-}
-
 /*
  * gtk_style_context_set_id:
  * @context: a #GtkStyleContext
@@ -1317,17 +1181,10 @@ void
 gtk_style_context_get_color (GtkStyleContext *context,
                              GdkRGBA         *color)
 {
-  GdkRGBA *c;
-
   g_return_if_fail (color != NULL);
   g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
 
-  gtk_style_context_get (context,
-                         "color", &c,
-                         NULL);
-
-  *color = *c;
-  gdk_rgba_free (c);
+  *color = *gtk_css_color_value_get_rgba (_gtk_style_context_peek_property (context, 
GTK_CSS_PROPERTY_COLOR));
 }
 
 /**
@@ -1432,20 +1289,11 @@ _gtk_style_context_get_cursor_color (GtkStyleContext *context,
                                      GdkRGBA         *primary_color,
                                      GdkRGBA         *secondary_color)
 {
-  GdkRGBA *pc, *sc;
-
-  gtk_style_context_get (context,
-                         "caret-color", &pc,
-                         "-gtk-secondary-caret-color", &sc,
-                         NULL);
   if (primary_color)
-    *primary_color = *pc;
+    *primary_color = *gtk_css_color_value_get_rgba (_gtk_style_context_peek_property (context, 
GTK_CSS_PROPERTY_CARET_COLOR));
 
   if (secondary_color)
-    *secondary_color = *sc;
-
-  gdk_rgba_free (pc);
-  gdk_rgba_free (sc);
+    *secondary_color = *gtk_css_color_value_get_rgba (_gtk_style_context_peek_property (context, 
GTK_CSS_PROPERTY_SECONDARY_CARET_COLOR));
 }
 
 static void
@@ -1827,26 +1675,22 @@ AtkAttributeSet *
 _gtk_style_context_get_attributes (AtkAttributeSet *attributes,
                                    GtkStyleContext *context)
 {
-  GdkRGBA *bg; 
-  GdkRGBA color;
+  const GdkRGBA *color; 
   gchar *value;
 
-  gtk_style_context_get (context, "background-color", &bg, NULL);
+  color = gtk_css_color_value_get_rgba (_gtk_style_context_peek_property (context, 
GTK_CSS_PROPERTY_BACKGROUND_COLOR));
   value = g_strdup_printf ("%u,%u,%u",
-                           (guint) ceil (bg->red * 65536 - bg->red),
-                           (guint) ceil (bg->green * 65536 - bg->green),
-                           (guint) ceil (bg->blue * 65536 - bg->blue));
+                           (guint) ceil (color->red * 65536 - color->red),
+                           (guint) ceil (color->green * 65536 - color->green),
+                           (guint) ceil (color->blue * 65536 - color->blue));
   attributes = add_attribute (attributes, ATK_TEXT_ATTR_BG_COLOR, value);
-  g_free (value);
-  gdk_rgba_free (bg);
 
-  gtk_style_context_get_color (context, &color);
+  color = gtk_css_color_value_get_rgba (_gtk_style_context_peek_property (context, GTK_CSS_PROPERTY_COLOR));
   value = g_strdup_printf ("%u,%u,%u",
-                           (guint) ceil (color.red * 65536 - color.red),
-                           (guint) ceil (color.green * 65536 - color.green),
-                           (guint) ceil (color.blue * 65536 - color.blue));
+                           (guint) ceil (color->red * 65536 - color->red),
+                           (guint) ceil (color->green * 65536 - color->green),
+                           (guint) ceil (color->blue * 65536 - color->blue));
   attributes = add_attribute (attributes, ATK_TEXT_ATTR_FG_COLOR, value);
-  g_free (value);
 
   return attributes;
 }
diff --git a/gtk/gtkstylecontext.h b/gtk/gtkstylecontext.h
index 46a98bc7ec..331241d78a 100644
--- a/gtk/gtkstylecontext.h
+++ b/gtk/gtkstylecontext.h
@@ -874,19 +874,6 @@ void gtk_style_context_save    (GtkStyleContext *context);
 GDK_AVAILABLE_IN_ALL
 void gtk_style_context_restore (GtkStyleContext *context);
 
-GDK_AVAILABLE_IN_ALL
-void gtk_style_context_get_property (GtkStyleContext *context,
-                                     const gchar     *property,
-                                     GValue          *value);
-GDK_AVAILABLE_IN_ALL
-void gtk_style_context_get_valist   (GtkStyleContext *context,
-                                     const char      *first_property_name,
-                                     va_list          args);
-GDK_AVAILABLE_IN_ALL
-void gtk_style_context_get          (GtkStyleContext *context,
-                                     const char      *first_property_name,
-                                     ...) G_GNUC_NULL_TERMINATED;
-
 GDK_AVAILABLE_IN_ALL
 void          gtk_style_context_set_state    (GtkStyleContext *context,
                                               GtkStateFlags    flags);
diff --git a/gtk/gtktexthandle.c b/gtk/gtktexthandle.c
index 2d3d148ac7..3f993920ba 100644
--- a/gtk/gtktexthandle.c
+++ b/gtk/gtktexthandle.c
@@ -16,6 +16,8 @@
  */
 
 #include "config.h"
+
+#include "gtkcssnumbervalueprivate.h"
 #include "gtkprivatetypebuiltins.h"
 #include "gtktexthandleprivate.h"
 #include "gtkmarshalers.h"
@@ -25,6 +27,7 @@
 #include "gtkwidgetprivate.h"
 #include "gtkgizmoprivate.h"
 #include "gtkrendericonprivate.h"
+#include "gtkstylecontextprivate.h"
 #include "gtkintl.h"
 
 #include <gtk/gtk.h>
@@ -89,11 +92,9 @@ _gtk_text_handle_get_size (GtkTextHandle         *handle,
   GtkStyleContext *context;
 
   context = gtk_widget_get_style_context (widget);
-
-  gtk_style_context_get (context,
-                         "min-width", width,
-                         "min-height", height,
-                         NULL);
+  
+  *width = _gtk_css_number_value_get (_gtk_style_context_peek_property (context, 
GTK_CSS_PROPERTY_MIN_WIDTH), 100);
+  *height = _gtk_css_number_value_get (_gtk_style_context_peek_property (context, 
GTK_CSS_PROPERTY_MIN_HEIGHT), 100);
 }
 
 static gint
diff --git a/gtk/gtktextlayout.c b/gtk/gtktextlayout.c
index 6d4b41b65a..5a2b419730 100644
--- a/gtk/gtktextlayout.c
+++ b/gtk/gtktextlayout.c
@@ -76,9 +76,12 @@
  */
 
 #include "config.h"
+
+#include "gtktextlayoutprivate.h"
+
+#include "gtkcsscolorvalueprivate.h"
 #include "gtkmarshalers.h"
 #include "gtkstylecontextprivate.h"
-#include "gtktextlayoutprivate.h"
 #include "gtktextbtree.h"
 #include "gtktextbufferprivate.h"
 #include "gtktextiterprivate.h"
@@ -3866,7 +3869,7 @@ render_para (GskPangoRenderer   *crenderer,
   int byte_offset = 0;
   PangoLayoutIter *iter;
   int screen_width;
-  GdkRGBA *selection = NULL;
+  const GdkRGBA *selection = NULL;
   gboolean first = TRUE;
 
   g_return_if_fail (GTK_IS_TEXT_VIEW (crenderer->widget));
@@ -3880,7 +3883,7 @@ render_para (GskPangoRenderer   *crenderer,
       GtkCssNode *selection_node = gtk_text_view_get_selection_node ((GtkTextView*)crenderer->widget);
       gtk_style_context_save_to_node (context, selection_node);
 
-      gtk_style_context_get (context, "background-color", &selection, NULL);
+      selection = gtk_css_color_value_get_rgba (_gtk_style_context_peek_property (context, 
GTK_CSS_PROPERTY_BACKGROUND_COLOR));
 
       gtk_style_context_restore (context);
     }
@@ -4066,7 +4069,6 @@ render_para (GskPangoRenderer   *crenderer,
   if (offset_y)
     gtk_snapshot_restore (crenderer->snapshot);
 
-  gdk_rgba_free (selection);
   pango_layout_iter_free (iter);
 }
 
diff --git a/gtk/gtktextutil.c b/gtk/gtktextutil.c
index e12ddac84e..bc5be49127 100644
--- a/gtk/gtktextutil.c
+++ b/gtk/gtktextutil.c
@@ -27,6 +27,8 @@
 #include "gtktextview.h"
 #include "gtktextutil.h"
 
+#include "gtkcsscolorvalueprivate.h"
+#include "gtkstylecontextprivate.h"
 #include "gtktextbuffer.h"
 #include "gtktextlayoutprivate.h"
 #include "gtkintl.h"
@@ -139,39 +141,26 @@ gtk_text_util_create_drag_icon (GtkWidget *widget,
   return paintable;
 }
 
-static GtkCssValue *
-query_func (guint id, gpointer values)
-{
-  return gtk_css_style_get_value (values, id);
-}
-
 static void
 set_attributes_from_style (GtkWidget         *widget,
                            GtkTextAttributes *values)
 {
-  GtkCssStyle *style;
+  GtkStyleContext *context;
   const GdkRGBA black = { 0, };
-  const GdkRGBA *color;
-  GValue value = G_VALUE_INIT;
 
   if (!values->appearance.bg_rgba)
     values->appearance.bg_rgba = gdk_rgba_copy (&black);
   if (!values->appearance.fg_rgba)
     values->appearance.fg_rgba = gdk_rgba_copy (&black);
 
-  style = gtk_css_node_get_style (gtk_widget_get_css_node (widget));
-  color = gtk_css_color_value_get_rgba (gtk_css_style_get_value (style, GTK_CSS_PROPERTY_BACKGROUND_COLOR));
-  *values->appearance.bg_rgba = *color;
-  color = gtk_css_color_value_get_rgba (gtk_css_style_get_value (style, GTK_CSS_PROPERTY_BACKGROUND_COLOR));
-  *values->appearance.fg_rgba = *color;
+  context = gtk_widget_get_style_context (widget);
+  *values->appearance.bg_rgba = *gtk_css_color_value_get_rgba (_gtk_style_context_peek_property (context, 
GTK_CSS_PROPERTY_BACKGROUND_COLOR));
+  *values->appearance.fg_rgba = *gtk_css_color_value_get_rgba (_gtk_style_context_peek_property (context, 
GTK_CSS_PROPERTY_COLOR));
 
   if (values->font)
     pango_font_description_free (values->font);
 
-  _gtk_style_property_query (_gtk_style_property_lookup ("font"), &value, query_func, style);
-  
-  values->font = g_value_get_boxed (&value);
-  g_value_unset (&value);
+  values->font = gtk_css_style_get_pango_font (gtk_style_context_lookup_style (context));
 }
 
 static gint
diff --git a/gtk/gtktextview.c b/gtk/gtktextview.c
index b405f0e187..dea5a11f11 100644
--- a/gtk/gtktextview.c
+++ b/gtk/gtktextview.c
@@ -31,6 +31,7 @@
 
 #include "gtkadjustmentprivate.h"
 #include "gtkbindings.h"
+#include "gtkcsscolorvalueprivate.h"
 #include "gtkdebug.h"
 #include "gtkintl.h"
 #include "gtkmain.h"
@@ -7437,7 +7438,7 @@ gtk_text_view_set_attributes_from_style (GtkTextView        *text_view,
 {
   GtkStyleContext *context;
   const GdkRGBA black = { 0, };
-  GdkRGBA *bg;
+  const GdkRGBA *color;
 
   if (!values->appearance.bg_rgba)
     values->appearance.bg_rgba = gdk_rgba_copy (&black);
@@ -7446,15 +7447,15 @@ gtk_text_view_set_attributes_from_style (GtkTextView        *text_view,
 
   context = gtk_widget_get_style_context (GTK_WIDGET (text_view));
 
-  gtk_style_context_get (context, "background-color", &bg, NULL);
-  *values->appearance.bg_rgba = *bg;
-  gdk_rgba_free (bg);
-  gtk_style_context_get_color (context, values->appearance.fg_rgba);
+  color = gtk_css_color_value_get_rgba (_gtk_style_context_peek_property (context, 
GTK_CSS_PROPERTY_BACKGROUND_COLOR));
+  *values->appearance.bg_rgba = *color;
+  color = gtk_css_color_value_get_rgba (_gtk_style_context_peek_property (context, GTK_CSS_PROPERTY_COLOR));
+  *values->appearance.fg_rgba = *color;
 
   if (values->font)
     pango_font_description_free (values->font);
 
-  gtk_style_context_get (context, "font", &values->font, NULL);
+  values->font = gtk_css_style_get_pango_font (gtk_style_context_lookup_style (context));
 }
 
 static void


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]