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.
This commit is contained in:
Timm Bäder 2020-01-29 08:33:41 +01:00
parent fd021e794c
commit a7b34df6af
3 changed files with 95 additions and 0 deletions

View 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

View 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);
}

View 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);
/**