[gtk/matthiasc/css-matching-2: 1/2] css: Add getters for a matchers name and classes



commit 257d638e9798fa2006f351c822a18ffd6b43b174
Author: Matthias Clasen <mclasen redhat com>
Date:   Sun Jan 19 11:33:47 2020 -0500

    css: Add getters for a matchers name and classes
    
    These will be used in the selector tree in the future.
    
    The classes getter annoyingly has to allow returning
    allocated memory, due to widget paths. This can be
    removed when widget paths go away.

 gtk/gtkcssmatcher.c        | 93 ++++++++++++++++++++++++++++++++++++++++++++++
 gtk/gtkcssmatcherprivate.h | 19 ++++++++++
 2 files changed, 112 insertions(+)
---
diff --git a/gtk/gtkcssmatcher.c b/gtk/gtkcssmatcher.c
index ea082b1408..bfdaa500ea 100644
--- a/gtk/gtkcssmatcher.c
+++ b/gtk/gtkcssmatcher.c
@@ -72,6 +72,79 @@ gtk_css_matcher_widget_path_get_previous (GtkCssMatcher       *matcher,
   return TRUE;
 }
 
+static const char *
+gtk_css_matcher_widget_path_get_name (const GtkCssMatcher *matcher)
+{
+  const GtkWidgetPath *siblings;
+
+  siblings = gtk_widget_path_iter_get_siblings (matcher->path.path, matcher->path.index);
+  if (siblings && matcher->path.sibling_index != gtk_widget_path_iter_get_sibling_index (matcher->path.path, 
matcher->path.index))
+    {
+      const char *path_name = gtk_widget_path_iter_get_object_name (siblings, matcher->path.sibling_index);
+
+      if (path_name == NULL)
+        path_name = g_type_name (gtk_widget_path_iter_get_object_type (siblings, 
matcher->path.sibling_index));
+
+      return path_name;
+    }
+  else
+    {
+      const char *path_name = gtk_widget_path_iter_get_object_name (matcher->path.path, matcher->path.index);
+
+      if (path_name == NULL)
+        path_name = g_type_name (gtk_widget_path_iter_get_object_type (matcher->path.path, 
matcher->path.index));
+
+      return path_name;
+    }
+
+  return NULL;
+}
+
+static GQuark *
+gtk_css_matcher_widget_path_get_classes (const GtkCssMatcher *matcher,
+                                         guint               *n_classes,
+                                         gboolean            *allocated)
+{
+  int num;
+  const GtkWidgetPath *siblings;
+  GSList *list, *l;
+  GQuark *classes;
+  const GQuark *decl_classes = NULL;
+  guint n_decl_classes = 0;
+  int i;
+  
+  if (matcher->path.decl)
+    decl_classes = gtk_css_node_declaration_get_classes (matcher->path.decl, &n_decl_classes);
+
+  siblings = gtk_widget_path_iter_get_siblings (matcher->path.path, matcher->path.index);
+  if (siblings && matcher->path.sibling_index != gtk_widget_path_iter_get_sibling_index (matcher->path.path, 
matcher->path.index))
+    list = gtk_widget_path_iter_list_classes (siblings, matcher->path.sibling_index);
+  else
+    list = gtk_widget_path_iter_list_classes (matcher->path.path, matcher->path.index);
+
+  num = n_decl_classes + g_slist_length (list);
+
+  classes = g_new (GQuark, num);
+
+  if (matcher->path.decl)
+    {
+      for (i = 0; i < n_decl_classes; i++)
+        classes[i] = decl_classes[i];
+    }
+
+  for (l = list; l; l = l->next)
+    {
+      const char *name = l->data;
+
+      classes[i++] = g_quark_from_string (name);
+    }
+
+  *n_classes = num;
+  *allocated = TRUE;
+
+  return classes;
+}
+
 static gboolean
 gtk_css_matcher_widget_path_has_state (const GtkCssMatcher *matcher,
                                        GtkStateFlags        state)
@@ -193,6 +266,8 @@ static const GtkCssMatcherClass GTK_CSS_MATCHER_WIDGET_PATH = {
   GTK_CSS_MATCHER_TYPE_WIDGET_PATH,
   gtk_css_matcher_widget_path_get_parent,
   gtk_css_matcher_widget_path_get_previous,
+  gtk_css_matcher_widget_path_get_name,
+  gtk_css_matcher_widget_path_get_classes,
   gtk_css_matcher_widget_path_has_state,
   gtk_css_matcher_widget_path_has_name,
   gtk_css_matcher_widget_path_has_class,
@@ -266,6 +341,22 @@ gtk_css_matcher_node_get_previous (GtkCssMatcher       *matcher,
   return gtk_css_node_init_matcher (node, matcher);
 }
 
+static const char *
+gtk_css_matcher_node_get_name (const GtkCssMatcher *matcher)
+{
+  return matcher->node.node_name;
+}
+
+static GQuark *
+gtk_css_matcher_node_get_classes (const GtkCssMatcher *matcher,
+                                  guint               *n_classes,
+                                  gboolean            *allocated)
+{
+  *n_classes = matcher->node.n_classes;
+  *allocated = FALSE;
+  return (GQuark *)matcher->node.classes;
+}
+
 static gboolean
 gtk_css_matcher_node_has_state (const GtkCssMatcher *matcher,
                                 GtkStateFlags        state)
@@ -378,6 +469,8 @@ static const GtkCssMatcherClass GTK_CSS_MATCHER_NODE = {
   GTK_CSS_MATCHER_TYPE_NODE,
   gtk_css_matcher_node_get_parent,
   gtk_css_matcher_node_get_previous,
+  gtk_css_matcher_node_get_name,
+  gtk_css_matcher_node_get_classes,
   gtk_css_matcher_node_has_state,
   gtk_css_matcher_node_has_name,
   gtk_css_matcher_node_has_class,
diff --git a/gtk/gtkcssmatcherprivate.h b/gtk/gtkcssmatcherprivate.h
index 69b4f0e2e4..da95e2d690 100644
--- a/gtk/gtkcssmatcherprivate.h
+++ b/gtk/gtkcssmatcherprivate.h
@@ -41,6 +41,11 @@ struct _GtkCssMatcherClass {
   gboolean        (* get_previous)                (GtkCssMatcher          *matcher,
                                                    const GtkCssMatcher    *next);
 
+  const char *    (* get_name)                    (const GtkCssMatcher   *matcher);
+  GQuark *        (* get_classes)                 (const GtkCssMatcher   *matcher,
+                                                   guint                 *n_classes,
+                                                   gboolean              *allocated);
+
   gboolean        (* has_state)                   (const GtkCssMatcher   *matcher,
                                                    GtkStateFlags          state);
   gboolean        (* has_name)                    (const GtkCssMatcher   *matcher,
@@ -106,6 +111,20 @@ _gtk_css_matcher_get_previous (GtkCssMatcher       *matcher,
   return next->klass->get_previous (matcher, next);
 }
 
+static inline const char *
+_gtk_css_matcher_get_name (const GtkCssMatcher *matcher)
+{
+  return matcher->klass->get_name (matcher);
+}
+
+static inline GQuark *
+_gtk_css_matcher_get_classes (const GtkCssMatcher *matcher,
+                              guint               *n_classes,
+                              gboolean            *allocated)
+{
+  return matcher->klass->get_classes (matcher, n_classes, allocated);
+}
+
 static inline gboolean
 _gtk_css_matcher_has_state (const GtkCssMatcher *matcher,
                             GtkStateFlags        state)


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