[gtk/wip/baedert/widget-add-style-class] widget: Add style class API



commit ad511d2ed0c5ab0933fc703fe4291d0cbdac29ad
Author: Timm Bäder <mail baedert org>
Date:   Wed Jan 29 08:33:41 2020 +0100

    widget: Add style class API
    
    Add GtkWidget API for adding and removing style classes, as well as
    checking whether a widget has a style class applied.
    
    Everyone has to go through GtkStyleContext for this these days but with
    GtkStyleContext eventually going away, it makse sense for GtkWidget to
    have API for this.

 docs/reference/gtk/gtk4-sections.txt |  3 ++
 gtk/gtkwidget.c                      | 81 ++++++++++++++++++++++++++++++++++++
 gtk/gtkwidget.h                      | 11 +++++
 3 files changed, 95 insertions(+)
---
diff --git a/docs/reference/gtk/gtk4-sections.txt b/docs/reference/gtk/gtk4-sections.txt
index b4a737d2f0..21b08c4037 100644
--- a/docs/reference/gtk/gtk4-sections.txt
+++ b/docs/reference/gtk/gtk4-sections.txt
@@ -4409,6 +4409,9 @@ gtk_widget_insert_after
 gtk_widget_set_layout_manager
 gtk_widget_get_layout_manager
 gtk_widget_should_layout
+gtk_widget_add_style_class
+gtk_widget_remove_style_class
+gtk_widget_has_style_class
 
 <SUBSECTION>
 gtk_widget_get_style_context
diff --git a/gtk/gtkwidget.c b/gtk/gtkwidget.c
index ba4f4b461c..574d0ca205 100644
--- a/gtk/gtkwidget.c
+++ b/gtk/gtkwidget.c
@@ -13175,3 +13175,84 @@ gtk_widget_class_query_action (GtkWidgetClass      *widget_class,
 
   return FALSE;
 }
+
+/**
+ * gtk_widget_add_style_class:
+ * @widget: a #GtkWidget
+ * @style_class: The style class to add to @widget, without
+ *   the leading '.' used for notation of style classes
+ *
+ * Adds @style_class to @widget. After calling this function, @widget's
+ * style will match for @style_class, after the CSS matching rules.
+ */
+void
+gtk_widget_add_style_class (GtkWidget   *widget,
+                            const char  *style_class)
+{
+  GtkWidgetPrivate *priv = gtk_widget_get_instance_private (widget);
+
+  g_return_if_fail (GTK_IS_WIDGET (widget));
+  g_return_if_fail (style_class != NULL);
+  g_return_if_fail (style_class[0] != '\0');
+  g_return_if_fail (style_class[0] != '.');
+
+  gtk_css_node_add_class (priv->cssnode, g_quark_from_string (style_class));
+}
+
+/**
+ * gtk_widget_remove_style_class:
+ * @widget: a #GtkWidget
+ * @style_class: The style class to remove from @widget, without
+ *   the leading '.' used for notation of style classes
+ *
+ * Removes @style_class from @widget. After this, the style of @widget
+ * will stop matching for @style_class.
+ */
+void
+gtk_widget_remove_style_class (GtkWidget   *widget,
+                               const char  *style_class)
+{
+  GtkWidgetPrivate *priv = gtk_widget_get_instance_private (widget);
+  GQuark class_quark;
+
+  g_return_if_fail (GTK_IS_WIDGET (widget));
+  g_return_if_fail (style_class != NULL);
+  g_return_if_fail (style_class[0] != '\0');
+  g_return_if_fail (style_class[0] != '.');
+
+  class_quark = g_quark_from_string (style_class);
+  if (!class_quark)
+    return;
+
+  gtk_css_node_remove_class (priv->cssnode, class_quark);
+}
+
+/**
+ * gtk_widget_has_style_class:
+ * @widget: a #GtkWidget
+ * @style_class: A CSS style class, without the leading '.'
+ *   used for notation of style classes
+ *
+ * Returns whether @style_class is currently applied to @widget.
+ *
+ * Returns: %TRUE if @style_class is currently applied to @widget,
+ *   %FALSE otherwise.
+ */
+gboolean
+gtk_widget_has_style_class (GtkWidget   *widget,
+                            const char  *style_class)
+{
+  GtkWidgetPrivate *priv = gtk_widget_get_instance_private (widget);
+  GQuark class_quark;
+
+  g_return_val_if_fail (GTK_IS_WIDGET (widget), FALSE);
+  g_return_val_if_fail (style_class != NULL, FALSE);
+  g_return_val_if_fail (style_class[0] != '\0', FALSE);
+  g_return_val_if_fail (style_class[0] != '.', FALSE);
+
+  class_quark = g_quark_from_string (style_class);
+  if (!class_quark)
+    return FALSE;
+
+  return gtk_css_node_has_class (priv->cssnode, class_quark);
+}
diff --git a/gtk/gtkwidget.h b/gtk/gtkwidget.h
index 1719f59487..bcde90db00 100644
--- a/gtk/gtkwidget.h
+++ b/gtk/gtkwidget.h
@@ -967,6 +967,17 @@ void                    gtk_widget_snapshot_child       (GtkWidget   *widget,
                                                          GtkSnapshot *snapshot);
 GDK_AVAILABLE_IN_ALL
 gboolean                gtk_widget_should_layout        (GtkWidget   *widget);
+GDK_AVAILABLE_IN_ALL
+void                    gtk_widget_add_style_class      (GtkWidget   *widget,
+                                                         const char  *style_class);
+GDK_AVAILABLE_IN_ALL
+void                    gtk_widget_remove_style_class   (GtkWidget   *widget,
+                                                         const char  *style_class);
+GDK_AVAILABLE_IN_ALL
+gboolean                gtk_widget_has_style_class      (GtkWidget   *widget,
+                                                         const char  *style_class);
+
+
 
 
 /**


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