widget: Add style class API
authorTimm Bäder <mail@baedert.org>
Wed, 29 Jan 2020 07:33:41 +0000 (08:33 +0100)
committerTimm Bäder <mail@baedert.org>
Wed, 29 Jan 2020 08:36:48 +0000 (09:36 +0100)
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
gtk/gtkwidget.c
gtk/gtkwidget.h

index b4a737d2f0b4488fab70e874982de2ddf723c7a0..21b08c4037a078809a8cf9ace4a01e9764d517fa 100644 (file)
@@ -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
index ba4f4b461c4bad9e1cfbc755c9005cc98362bc06..684ed5e141cb6af5f51393e37e77a38c3390313d 100644 (file)
@@ -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);
+}
index 1719f59487bd0019ebafc019d4a032e34db83637..bcde90db0093bc314495810b43fddec9b01c67fc 100644 (file)
@@ -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);
+
+
 
 
 /**