stylecontext: Make save/restore create a child CSS node

This is a change for how CSS is applied.

Previously, subelements (I'll take GtkEntry icons as an example) were
treated as having the same parent as the regular elements. So a selector
such as
  .entry
would match an entry inside a window. But it'd also match the icon image
inside the entry. So CSS like
  .entry { padding: 10px; }
would add 10px of padding to both the entry itself and to the icon image
inside the entry, so the icon would effective have 20px padding. To get
around that, one would have to unset it again like so:
  .entry { padding: 10px; }
  .entry.image { padding: unset; }
This is getting more and more of a problem as we make subelements
respect more properties that aren't inherited by default anyway, like
backgrounds and padding/margin/border.

This patch has one caveat though: It makes calling
gtk_style_context_save() the first time have an important side effect.
It's important to audit code to make sure it is only used for
subelements.

And last but not least, this patch is only useful if code unsets
parent's style classes that it doesn't want to apply any longer. Because
style classes are inherited by default (and I don't think we want to
change that), the example will still apply until the subelements no
longer contain the .entry style class.
This commit is contained in:
Benjamin Otte 2014-10-12 23:43:48 +02:00
parent 9843837593
commit 3a337156d1

View File

@ -762,8 +762,21 @@ create_query_path (GtkStyleContext *context,
priv = context->priv;
path = priv->widget ? _gtk_widget_create_path (priv->widget) : gtk_widget_path_copy (priv->widget_path);
length = gtk_widget_path_length (path);
if (length > 0)
style_info_add_to_widget_path (info, path, length - 1);
if (gtk_style_context_is_saved (context))
{
GtkStyleInfo *root = g_slist_last (context->priv->saved_nodes)->data;
if (length > 0)
style_info_add_to_widget_path (root, path, length - 1);
gtk_widget_path_append_type (path, length > 0 ?gtk_widget_path_iter_get_object_type (path, length - 1) : G_TYPE_NONE);
style_info_add_to_widget_path (info, path, length);
}
else
{
if (length > 0)
style_info_add_to_widget_path (info, path, length - 1);
}
return path;
}