#include "gtkcssproviderprivate.h"
#include "gtkcssparserprivate.h"
-#include "gtkcssstringfuncsprivate.h"
+#include "gtkcssselectorprivate.h"
#include "gtksymboliccolor.h"
#include "gtkstyleprovider.h"
#include "gtkstylecontextprivate.h"
+#include "gtkstylepropertiesprivate.h"
+#include "gtkstylepropertyprivate.h"
#include "gtkbindings.h"
#include "gtkmarshalers.h"
#include "gtkprivate.h"
*
* /* Theme any widget within a GtkBin */
* GtkBin * {
- * font-name: Sans 20
+ * font: Sans 20
* }
*
* /* Theme a label named title-label */
* GtkLabel#title-label {
- * font-name: Sans 15
+ * font: Sans 15
* }
*
* /* Theme any widget named main-entry */
* color-stop (1, @green))</literallayout></para>
* </example>
* </refsect2>
- * <refsect2 id="gtkcssprovider-slices">
+ * <refsect2 id="gtkcssprovider-shadows">
+ * <title>Text shadow</title>
+ * <para>
+ * A shadow list can be applied to text or symbolic icons, using the CSS3
+ * text-shadow syntax, as defined in
+ * <ulink url="http://www.w3.org/TR/css3-text/#text-shadow">the CSS3 specification</ulink>.
+ * </para>
+ * <para>
+ * A text shadow is specified using the syntax
+ * <literallayout>text-shadow: @horizontal_offset @vertical_offset [ @blur_radius ] @color</literallayout>
+ * The offset of the shadow is specified with the @horizontal_offset and @vertical_offset
+ * parameters. The optional blur radius is parsed, but it is currently not rendered by
+ * the GTK+ theming engine.
+ * </para>
+ * <para>
+ * To set multiple shadows on an element, you can specify a comma-separated list
+ * of shadow elements in the text-shadow property. Shadows are always rendered
+ * front-back, i.e. the first shadow specified is on top of the others. Shadows
+ * can thus overlay each other, but they can never overlay the text itself,
+ * which is always rendered on top of the shadow layer.
+ * </para>
+ * </refsect2>
+ * <refsect2>
+ * <title>Box shadow</title>
+ * <para>
+ * Themes can apply shadows on framed elements using the CSS3 box-shadow syntax,
+ * as defined in
+ * <ulink url="http://www.w3.org/TR/css3-background/#the-box-shadow">the CSS3 specification</ulink>.
+ * </para>
+ * <para>
+ * A box shadow is specified using the syntax
+ * <literallayout>box-shadow: [ @inset ] @horizontal_offset @vertical_offset [ @blur_radius ] [ @spread ] @color</literallayout>
+ * A positive offset will draw a shadow that is offset to the right (down) of the box,
+ * a negative offset to the left (top). The optional spread parameter defines an additional
+ * distance to expand the shadow shape in all directions, by the specified radius.
+ * The optional blur radius parameter is parsed, but it is currently not rendered by
+ * the GTK+ theming engine.
+ * The inset parameter defines whether the drop shadow should be rendered inside or outside
+ * the box canvas. Only inset box-shadows are currently supported by the GTK+ theming engine,
+ * non-inset elements are currently ignored.
+ * </para>
+ * <para>
+ * To set multiple box-shadows on an element, you can specify a comma-separated list
+ * of shadow elements in the box-shadow property. Shadows are always rendered
+ * front-back, i.e. the first shadow specified is on top of the others, so they may
+ * overlap other boxes or other shadows.
+ * </para>
+ * </refsect2>
+ * <refsect2 id="gtkcssprovider-border-image">
* <title>Border images</title>
* <para>
- * Images can be used in 'slices' for the purpose of creating scalable
- * borders.
+ * Images and gradients can also be used in slices for the purpose of creating
+ * scalable borders.
+ * For more information, see the CSS3 documentation for the border-image property,
+ * which can be found <ulink url="http://www.w3.org/TR/css3-background/#border-images">here</ulink>.
* </para>
* <inlinegraphic fileref="slices.png" format="PNG"/>
* <para>
- * The syntax for specifying border images of this kind is:
- * <literallayout>url(@path) @top @right @bottom @left [repeat|stretch]? [repeat|stretch]?</literallayout>
- * The sizes of the 'cut off' portions are specified
- * with the @top, @right, @bottom and @left parameters.
- * The 'middle' sections can be repeated or stretched to create
- * the desired effect, by adding the 'repeat' or 'stretch' options after
- * the dimensions. If two options are specified, the first one affects
+ * The parameters of the slicing process are controlled by
+ * four separate properties. Note that you can use the
+ * <literallayout>border-image</literallayout> shorthand property
+ * to set values for the three properties at the same time.
+ * </para>
+ * <para>
+ * <literallayout>border-image-source: url(@path)
+ * (or border-image-source: -gtk-gradient(...))</literallayout>:
+ * Specifies the source of the border image, and it can either
+ * be an URL or a gradient (see above).
+ * </para>
+ * <para>
+ * <literallayout>border-image-slice: @top @right @bottom @left</literallayout>
+ * The sizes specified by the @top, @right, @bottom and @left parameters
+ * are the offsets, in pixels, from the relevant edge where the image
+ * should be "cut off" to build the slices used for the rendering
+ * of the border.
+ * </para>
+ * <para>
+ * <literallayout>border-image-width: @top @right @bottom @left</literallayout>
+ * The sizes specified by the @top, @right, @bottom and @left parameters
+ * are inward distances from the border box edge, used to specify the
+ * rendered size of each slice determined by border-image-slice.
+ * If this property is not specified, the values of border-width will
+ * be used as a fallback.
+ * </para>
+ * <para>
+ * <literallayout>border-image-repeat: [stretch|repeat|round|space] ?
+ * [stretch|repeat|round|space]</literallayout>
+ * Specifies how the image slices should be rendered in the area
+ * outlined by border-width.
+ * The default (stretch) is to resize the slice to fill in the whole
+ * allocated area.
+ * If the value of this property is 'repeat', the image slice
+ * will be tiled to fill the area.
+ * If the value of this property is 'round', the image slice will
+ * be tiled to fill the area, and scaled to fit it exactly
+ * a whole number of times.
+ * If the value of this property is 'space', the image slice will
+ * be tiled to fill the area, and if it doesn't fit it exactly a whole
+ * number of times, the extra space is distributed as padding around
+ * the slices.
+ * If two options are specified, the first one affects
* the horizontal behaviour and the second one the vertical behaviour.
* If only one option is specified, it affects both.
* </para>
* <entry>font: Sans 15;</entry>
* </row>
* <row>
+ * <entry>margin-top</entry>
+ * <entry>integer</entry>
+ * <entry>#gint</entry>
+ * <entry>margin-top: 0;</entry>
+ * </row>
+ * <row>
+ * <entry>margin-left</entry>
+ * <entry>integer</entry>
+ * <entry>#gint</entry>
+ * <entry>margin-left: 1;</entry>
+ * </row>
+ * <row>
+ * <entry>margin-bottom</entry>
+ * <entry>integer</entry>
+ * <entry>#gint</entry>
+ * <entry>margin-bottom: 2;</entry>
+ * </row>
+ * <row>
+ * <entry>margin-right</entry>
+ * <entry>integer</entry>
+ * <entry>#gint</entry>
+ * <entry>margin-right: 4;</entry>
+ * </row>
+ * <row>
* <entry>margin</entry>
* <entry morerows="1"><literallayout>@width
* @vertical_width @horizontal_width
* </entry>
* </row>
* <row>
+ * <entry>padding-top</entry>
+ * <entry>integer</entry>
+ * <entry>#gint</entry>
+ * <entry>padding-top: 5;</entry>
+ * </row>
+ * <row>
+ * <entry>padding-left</entry>
+ * <entry>integer</entry>
+ * <entry>#gint</entry>
+ * <entry>padding-left: 5;</entry>
+ * </row>
+ * <row>
+ * <entry>padding-bottom</entry>
+ * <entry>integer</entry>
+ * <entry>#gint</entry>
+ * <entry>padding-bottom: 5;</entry>
+ * </row>
+ * <row>
+ * <entry>padding-right</entry>
+ * <entry>integer</entry>
+ * <entry>#gint</entry>
+ * <entry>padding-right: 5;</entry>
+ * </row>
+ * <row>
* <entry>padding</entry>
* </row>
* <row>
* </entry>
* </row>
* <row>
- * <entry>border-width</entry>
+ * <entry>border-top-width</entry>
+ * <entry>integer</entry>
+ * <entry>#gint</entry>
+ * <entry>border-top-width: 5;</entry>
+ * </row>
+ * <row>
+ * <entry>border-left-width</entry>
+ * <entry>integer</entry>
+ * <entry>#gint</entry>
+ * <entry>border-left-width: 5;</entry>
+ * </row>
+ * <row>
+ * <entry>border-bottom-width</entry>
+ * <entry>integer</entry>
+ * <entry>#gint</entry>
+ * <entry>border-bottom-width: 5;</entry>
+ * </row>
+ * <row>
+ * <entry>border-right-width</entry>
* <entry>integer</entry>
* <entry>#gint</entry>
- * <entry>border-width: 5;</entry>
+ * <entry>border-right-width: 5;</entry>
+ * </row>
+ * <row>
+ * <entry>border-width</entry>
+ * <entry morerows="1">#GtkBorder</entry>
+ * <entry morerows="1"><literallayout>border-width: 1;
+ * border-width: 1 2;
+ * border-width: 1 2 3;
+ * border-width: 1 2 3 5;</literallayout>
+ * </entry>
* </row>
* <row>
* <entry>border-radius</entry>
* </entry>
* </row>
* <row>
+ * <entry>text-shadow</entry>
+ * <entry>shadow list (see above)</entry>
+ * <entry>#GtkTextShadow</entry>
+ * <entry><literallayout>text-shadow: 1 1 0 blue, -4 -4 red;</literallayout></entry>
+ * </row>
+ * <row>
* <entry>transition</entry>
* <entry>transition (see above)</entry>
* <entry>internal use only</entry>
* </refsect2>
*/
-typedef struct SelectorElement SelectorElement;
-typedef struct SelectorPath SelectorPath;
-typedef struct SelectorStyleInfo SelectorStyleInfo;
+typedef struct GtkCssRuleset GtkCssRuleset;
typedef struct _GtkCssScanner GtkCssScanner;
-typedef enum SelectorElementType SelectorElementType;
-typedef enum CombinatorType CombinatorType;
typedef enum ParserScope ParserScope;
typedef enum ParserSymbol ParserSymbol;
-enum SelectorElementType {
- SELECTOR_TYPE_NAME,
- SELECTOR_NAME,
- SELECTOR_GTYPE,
- SELECTOR_REGION,
- SELECTOR_CLASS,
- SELECTOR_GLOB
-};
-
-enum CombinatorType {
- COMBINATOR_DESCENDANT, /* No direct relation needed */
- COMBINATOR_CHILD /* Direct child */
-};
-
-struct SelectorElement
+struct GtkCssRuleset
{
- SelectorElementType elem_type;
- CombinatorType combinator;
-
- union
- {
- GQuark name;
- GType type;
-
- struct
- {
- GQuark name;
- GtkRegionFlags flags;
- } region;
- };
-};
-
-struct SelectorPath
-{
- GSList *elements;
- GtkStateFlags state;
- guint ref_count;
-};
-
-struct SelectorStyleInfo
-{
- SelectorPath *path;
+ GtkCssSelector *selector;
+ GHashTable *widget_style;
GHashTable *style;
+
+ guint has_inherit :1;
};
struct _GtkCssScanner
GHashTable *symbolic_colors;
- GPtrArray *selectors_info;
+ GArray *rulesets;
};
enum ParserScope {
g_error_free (error);
}
-static SelectorPath *
-selector_path_new (void)
-{
- SelectorPath *path;
-
- path = g_slice_new0 (SelectorPath);
- path->ref_count = 1;
-
- return path;
-}
-
-static SelectorPath *
-selector_path_ref (SelectorPath *path)
-{
- path->ref_count++;
- return path;
-}
-
static void
-selector_path_unref (SelectorPath *path)
+gtk_css_ruleset_init_copy (GtkCssRuleset *new,
+ const GtkCssRuleset *ruleset,
+ GtkCssSelector *selector)
{
- path->ref_count--;
+ memcpy (new, ruleset, sizeof (GtkCssRuleset));
- if (path->ref_count > 0)
- return;
-
- while (path->elements)
- {
- g_slice_free (SelectorElement, path->elements->data);
- path->elements = g_slist_delete_link (path->elements, path->elements);
- }
-
- g_slice_free (SelectorPath, path);
-}
-
-static void
-selector_path_prepend_type (SelectorPath *path,
- const gchar *type_name)
-{
- SelectorElement *elem;
- GType type;
-
- elem = g_slice_new (SelectorElement);
- elem->combinator = COMBINATOR_DESCENDANT;
- type = g_type_from_name (type_name);
-
- if (type == G_TYPE_INVALID)
- {
- elem->elem_type = SELECTOR_TYPE_NAME;
- elem->name = g_quark_from_string (type_name);
- }
- else
- {
- elem->elem_type = SELECTOR_GTYPE;
- elem->type = type;
- }
-
- path->elements = g_slist_prepend (path->elements, elem);
-}
-
-static void
-selector_path_prepend_glob (SelectorPath *path)
-{
- SelectorElement *elem;
-
- elem = g_slice_new (SelectorElement);
- elem->elem_type = SELECTOR_GLOB;
- elem->combinator = COMBINATOR_DESCENDANT;
-
- path->elements = g_slist_prepend (path->elements, elem);
+ new->selector = selector;
+ if (new->widget_style)
+ g_hash_table_ref (new->widget_style);
+ if (new->style)
+ g_hash_table_ref (new->style);
}
static void
-selector_path_prepend_region (SelectorPath *path,
- const gchar *name,
- GtkRegionFlags flags)
+gtk_css_ruleset_clear (GtkCssRuleset *ruleset)
{
- SelectorElement *elem;
-
- elem = g_slice_new (SelectorElement);
- elem->combinator = COMBINATOR_DESCENDANT;
- elem->elem_type = SELECTOR_REGION;
+ if (ruleset->style)
+ g_hash_table_unref (ruleset->style);
+ if (ruleset->widget_style)
+ g_hash_table_unref (ruleset->widget_style);
+ if (ruleset->selector)
+ _gtk_css_selector_free (ruleset->selector);
- elem->region.name = g_quark_from_string (name);
- elem->region.flags = flags;
-
- path->elements = g_slist_prepend (path->elements, elem);
+ memset (ruleset, 0, sizeof (GtkCssRuleset));
}
static void
-selector_path_prepend_name (SelectorPath *path,
- const gchar *name)
+property_value_free (GValue *value)
{
- SelectorElement *elem;
-
- elem = g_slice_new (SelectorElement);
- elem->combinator = COMBINATOR_DESCENDANT;
- elem->elem_type = SELECTOR_NAME;
-
- elem->name = g_quark_from_string (name);
+ if (G_IS_VALUE (value))
+ g_value_unset (value);
- path->elements = g_slist_prepend (path->elements, elem);
+ g_slice_free (GValue, value);
}
static void
-selector_path_prepend_class (SelectorPath *path,
- const gchar *name)
+gtk_css_ruleset_add_style (GtkCssRuleset *ruleset,
+ char *name,
+ GValue *value)
{
- SelectorElement *elem;
-
- elem = g_slice_new (SelectorElement);
- elem->combinator = COMBINATOR_DESCENDANT;
- elem->elem_type = SELECTOR_CLASS;
-
- elem->name = g_quark_from_string (name);
+ if (ruleset->widget_style == NULL)
+ ruleset->widget_style = g_hash_table_new_full (g_str_hash,
+ g_str_equal,
+ (GDestroyNotify) g_free,
+ (GDestroyNotify) property_value_free);
- path->elements = g_slist_prepend (path->elements, elem);
+ g_hash_table_insert (ruleset->widget_style, name, value);
}
static void
-selector_path_prepend_combinator (SelectorPath *path,
- CombinatorType combinator)
-{
- SelectorElement *elem;
-
- g_assert (path->elements != NULL);
-
- /* It is actually stored in the last element */
- elem = path->elements->data;
- elem->combinator = combinator;
-}
-
-static SelectorStyleInfo *
-selector_style_info_new (SelectorPath *path)
+gtk_css_ruleset_add (GtkCssRuleset *ruleset,
+ const GtkStyleProperty *prop,
+ GValue *value)
{
- SelectorStyleInfo *info;
-
- info = g_slice_new0 (SelectorStyleInfo);
- info->path = selector_path_ref (path);
-
- return info;
-}
+ if (ruleset->style == NULL)
+ ruleset->style = g_hash_table_new_full (g_direct_hash,
+ g_direct_equal,
+ NULL,
+ (GDestroyNotify) property_value_free);
-static void
-selector_style_info_free (SelectorStyleInfo *info)
-{
- if (info->style)
- g_hash_table_unref (info->style);
+ if (_gtk_style_property_is_shorthand (prop))
+ {
+ GParameter *parameters;
+ guint i, n_parameters;
- if (info->path)
- selector_path_unref (info->path);
+ parameters = _gtk_style_property_unpack (prop, value, &n_parameters);
- g_slice_free (SelectorStyleInfo, info);
-}
+ for (i = 0; i < n_parameters; i++)
+ {
+ const GtkStyleProperty *child;
+ GValue *value;
-static void
-selector_style_info_set_style (SelectorStyleInfo *info,
- GHashTable *style)
-{
- if (info->style)
- g_hash_table_unref (info->style);
+ child = _gtk_style_property_lookup (parameters[i].name);
+ value = g_slice_dup (GValue, ¶meters[i].value);
+ gtk_css_ruleset_add (ruleset, child, value);
+ }
+ g_free (parameters);
+ property_value_free (value);
+ return;
+ }
- if (style)
- info->style = g_hash_table_ref (style);
- else
- info->style = NULL;
+ ruleset->has_inherit |= _gtk_style_property_is_inherit (prop);
+ g_hash_table_insert (ruleset->style, (gpointer) prop, value);
}
-static void
-property_value_free (GValue *value)
+static gboolean
+gtk_css_ruleset_matches (GtkCssRuleset *ruleset,
+ GtkWidgetPath *path,
+ guint length)
{
- if (G_IS_VALUE (value))
- g_value_unset (value);
-
- g_slice_free (GValue, value);
+ return _gtk_css_selector_matches (ruleset->selector, path, length);
}
static void
g_slist_free (scanner->state);
scanner->state = NULL;
- g_slist_foreach (scanner->cur_selectors, (GFunc) selector_path_unref, NULL);
- g_slist_free (scanner->cur_selectors);
+ g_slist_free_full (scanner->cur_selectors, (GDestroyNotify) _gtk_css_selector_free);
scanner->cur_selectors = NULL;
if (scanner->cur_properties)
GTK_TYPE_CSS_PROVIDER,
GtkCssProviderPrivate);
- priv->selectors_info = g_ptr_array_new_with_free_func ((GDestroyNotify) selector_style_info_free);
+ priv->rulesets = g_array_new (FALSE, FALSE, sizeof (GtkCssRuleset));
priv->symbolic_colors = g_hash_table_new_full (g_str_hash, g_str_equal,
(GDestroyNotify) g_free,
- (GDestroyNotify) gtk_symbolic_color_unref);
-}
-
-typedef struct ComparePathData ComparePathData;
-
-struct ComparePathData
-{
- guint64 score;
- SelectorPath *path;
- GSList *iter;
-};
-
-static gboolean
-compare_selector_element (GtkWidgetPath *path,
- guint index,
- SelectorElement *elem,
- guint8 *score)
-{
- *score = 0;
-
- if (elem->elem_type == SELECTOR_TYPE_NAME)
- {
- const gchar *type_name;
- GType resolved_type;
-
- /* Resolve the type name */
- type_name = g_quark_to_string (elem->name);
- resolved_type = g_type_from_name (type_name);
-
- if (resolved_type == G_TYPE_INVALID)
- {
- /* Type couldn't be resolved, so the selector
- * clearly doesn't affect the given widget path
- */
- return FALSE;
- }
-
- elem->elem_type = SELECTOR_GTYPE;
- elem->type = resolved_type;
- }
-
- if (elem->elem_type == SELECTOR_GTYPE)
- {
- GType type;
-
- type = gtk_widget_path_iter_get_object_type (path, index);
-
- if (!g_type_is_a (type, elem->type))
- return FALSE;
-
- if (type == elem->type)
- *score |= 0xF;
- else
- {
- guint diff = g_type_depth (type) - g_type_depth (elem->type);
-
- if (G_UNLIKELY (diff > 0xE))
- {
- g_warning ("Hierarchy is higher than expected.");
- diff = 0xE;
- }
-
- *score = 0XF - diff;
- }
-
- return TRUE;
- }
- else if (elem->elem_type == SELECTOR_REGION)
- {
- GtkRegionFlags flags;
-
- if (!gtk_widget_path_iter_has_qregion (path, index,
- elem->region.name,
- &flags))
- return FALSE;
-
- if (elem->region.flags != 0 &&
- (flags & elem->region.flags) == 0)
- return FALSE;
-
- *score = 0xF;
- return TRUE;
- }
- else if (elem->elem_type == SELECTOR_GLOB)
- {
- /* Treat as lowest matching type */
- *score = 1;
- return TRUE;
- }
- else if (elem->elem_type == SELECTOR_NAME)
- {
- if (!gtk_widget_path_iter_has_qname (path, index, elem->name))
- return FALSE;
-
- *score = 0xF;
- return TRUE;
- }
- else if (elem->elem_type == SELECTOR_CLASS)
- {
- if (!gtk_widget_path_iter_has_qclass (path, index, elem->name))
- return FALSE;
-
- *score = 0xF;
- return TRUE;
- }
-
- return FALSE;
-}
-
-static guint64
-compare_selector (GtkWidgetPath *path,
- SelectorPath *selector)
-{
- GSList *elements = selector->elements;
- gboolean match = TRUE, first = TRUE, first_match = FALSE;
- guint64 score = 0;
- gint i;
-
- i = gtk_widget_path_length (path) - 1;
-
- while (elements && match && i >= 0)
- {
- SelectorElement *elem;
- guint8 elem_score;
-
- elem = elements->data;
-
- match = compare_selector_element (path, i, elem, &elem_score);
-
- if (match && first)
- first_match = TRUE;
-
- /* Only move on to the next index if there is no match
- * with the current element (whether to continue or not
- * handled right after in the combinator check), or a
- * GType or glob has just been matched.
- *
- * Region and widget names do not trigger this because
- * the next element in the selector path could also be
- * related to the same index.
- */
- if (!match ||
- (elem->elem_type == SELECTOR_GTYPE ||
- elem->elem_type == SELECTOR_GLOB))
- i--;
-
- if (!match &&
- elem->elem_type != SELECTOR_TYPE_NAME &&
- elem->combinator == COMBINATOR_DESCENDANT)
- {
- /* With descendant combinators there may
- * be intermediate chidren in the hierarchy
- */
- match = TRUE;
- }
- else if (match)
- elements = elements->next;
-
- if (match)
- {
- /* Only 4 bits are actually used */
- score <<= 4;
- score |= elem_score;
- }
-
- first = FALSE;
- }
-
- /* If there are pending selector
- * elements to compare, it's not
- * a match.
- */
- if (elements)
- match = FALSE;
-
- if (!match)
- score = 0;
- else if (first_match)
- {
- /* Assign more weight to these selectors
- * that matched right from the first element.
- */
- score <<= 4;
- }
-
- return score;
-}
-
-typedef struct StylePriorityInfo StylePriorityInfo;
-
-struct StylePriorityInfo
-{
- guint64 score;
- GHashTable *style;
- GtkStateFlags state;
-};
-
-static GArray *
-css_provider_get_selectors (GtkCssProvider *css_provider,
- GtkWidgetPath *path)
-{
- GtkCssProviderPrivate *priv;
- GArray *priority_info;
- guint i, j;
-
- priv = css_provider->priv;
- priority_info = g_array_new (FALSE, FALSE, sizeof (StylePriorityInfo));
-
- for (i = 0; i < priv->selectors_info->len; i++)
- {
- SelectorStyleInfo *info;
- StylePriorityInfo new;
- gboolean added = FALSE;
- guint64 score;
-
- info = g_ptr_array_index (priv->selectors_info, i);
- score = compare_selector (path, info->path);
-
- if (score <= 0)
- continue;
-
- new.score = score;
- new.style = info->style;
- new.state = info->path->state;
-
- for (j = 0; j < priority_info->len; j++)
- {
- StylePriorityInfo *cur;
-
- cur = &g_array_index (priority_info, StylePriorityInfo, j);
-
- if (cur->score > new.score)
- {
- g_array_insert_val (priority_info, j, new);
- added = TRUE;
- break;
- }
- }
-
- if (!added)
- g_array_append_val (priority_info, new);
- }
-
- return priority_info;
+ (GDestroyNotify) gtk_symbolic_color_unref);
}
static void
GtkWidgetPath *path)
{
GtkCssProvider *css_provider;
+ GtkCssProviderPrivate *priv;
GtkStyleProperties *props;
- GArray *priority_info;
- guint i;
+ guint i, l, length;
css_provider = GTK_CSS_PROVIDER (provider);
+ priv = css_provider->priv;
+ length = gtk_widget_path_length (path);
props = gtk_style_properties_new ();
css_provider_dump_symbolic_colors (css_provider, props);
- priority_info = css_provider_get_selectors (css_provider, path);
- for (i = 0; i < priority_info->len; i++)
+ for (l = 1; l <= length; l++)
{
- StylePriorityInfo *info;
- GHashTableIter iter;
- gpointer key, value;
+ for (i = 0; i < priv->rulesets->len; i++)
+ {
+ GtkCssRuleset *ruleset;
+ GHashTableIter iter;
+ gpointer key, value;
- info = &g_array_index (priority_info, StylePriorityInfo, i);
- g_hash_table_iter_init (&iter, info->style);
+ ruleset = &g_array_index (priv->rulesets, GtkCssRuleset, i);
- while (g_hash_table_iter_next (&iter, &key, &value))
- {
- gchar *prop = key;
-
- /* Properties starting with '-' may be both widget style properties
- * or custom properties from the theming engine, so check whether
- * the type is registered or not.
- */
- if (prop[0] == '-' &&
- !gtk_style_properties_lookup_property (prop, NULL, NULL))
+ if (ruleset->style == NULL)
+ continue;
+
+ if (l < length && (!ruleset->has_inherit || _gtk_css_selector_get_state_flags (ruleset->selector)))
continue;
- gtk_style_properties_set_property (props, key, info->state, value);
+ if (!gtk_css_ruleset_matches (ruleset, path, l))
+ continue;
+
+ g_hash_table_iter_init (&iter, ruleset->style);
+
+ while (g_hash_table_iter_next (&iter, &key, &value))
+ {
+ GtkStyleProperty *prop = key;
+
+ if (l != length && !_gtk_style_property_is_inherit (prop))
+ continue;
+
+ _gtk_style_properties_set_property_by_property (props,
+ prop,
+ _gtk_css_selector_get_state_flags (ruleset->selector),
+ value);
+ }
}
}
- g_array_free (priority_info, TRUE);
-
return props;
}
+static void
+gtk_css_provider_parser_error (GtkCssParser *parser,
+ const GError *error,
+ gpointer user_data)
+{
+ GtkCssProvider *provider = user_data;
+
+ gtk_css_provider_take_error_full (provider,
+ NULL,
+ _gtk_css_parser_get_line (parser),
+ _gtk_css_parser_get_position (parser),
+ g_error_copy (error));
+}
+
static gboolean
gtk_css_provider_get_style_property (GtkStyleProvider *provider,
GtkWidgetPath *path,
GParamSpec *pspec,
GValue *value)
{
- GArray *priority_info;
+ GtkCssProvider *css_provider = GTK_CSS_PROVIDER (provider);
+ GtkCssProviderPrivate *priv = css_provider->priv;
+ const GValue *val;
gboolean found = FALSE;
gchar *prop_name;
gint i;
g_type_name (pspec->owner_type),
pspec->name);
- priority_info = css_provider_get_selectors (GTK_CSS_PROVIDER (provider), path);
-
- for (i = priority_info->len - 1; i >= 0; i--)
+ for (i = priv->rulesets->len - 1; i >= 0; i--)
{
- StylePriorityInfo *info;
- GValue *val;
+ GtkCssRuleset *ruleset;
+ GtkStateFlags selector_state;
+
+ ruleset = &g_array_index (priv->rulesets, GtkCssRuleset, i);
+
+ if (ruleset->widget_style == NULL)
+ continue;
- info = &g_array_index (priority_info, StylePriorityInfo, i);
- val = g_hash_table_lookup (info->style, prop_name);
+ if (!gtk_css_ruleset_matches (ruleset, path, gtk_widget_path_length (path)))
+ continue;
+
+ selector_state = _gtk_css_selector_get_state_flags (ruleset->selector);
+ val = g_hash_table_lookup (ruleset->widget_style, prop_name);
if (val &&
- (info->state == 0 ||
- info->state == state ||
- ((info->state & state) != 0 &&
- (info->state & ~(state)) == 0)))
+ (selector_state == 0 ||
+ selector_state == state ||
+ ((selector_state & state) != 0 &&
+ (selector_state & ~(state)) == 0)))
{
- GError *error = NULL;
+ GtkCssParser *parser;
+
+ parser = _gtk_css_parser_new (g_value_get_string (val),
+ gtk_css_provider_parser_error,
+ provider);
+
+ found = _gtk_style_property_parse_value (NULL,
+ value,
+ parser,
+ NULL);
- found = _gtk_css_value_from_string (value,
- NULL,
- g_value_get_string (val),
- &error);
+ _gtk_css_parser_free (parser);
if (found)
break;
-
- /* error location should be _way_ better */
- gtk_css_provider_take_error_full (GTK_CSS_PROVIDER (provider),
- NULL,
- 0, 0,
- error);
}
}
- g_array_free (priority_info, TRUE);
g_free (prop_name);
return found;
{
GtkCssProvider *css_provider;
GtkCssProviderPrivate *priv;
+ guint i;
css_provider = GTK_CSS_PROVIDER (object);
priv = css_provider->priv;
- g_ptr_array_free (priv->selectors_info, TRUE);
+ for (i = 0; i < priv->rulesets->len; i++)
+ gtk_css_ruleset_clear (&g_array_index (priv->rulesets, GtkCssRuleset, i));
+
+ g_array_free (priv->rulesets, TRUE);
if (priv->symbolic_colors)
g_hash_table_destroy (priv->symbolic_colors);
static void
css_provider_commit (GtkCssProvider *css_provider,
GSList *selectors,
- GHashTable *properties)
+ GtkCssRuleset *ruleset)
{
GtkCssProviderPrivate *priv;
GSList *l;
priv = css_provider->priv;
- if (g_hash_table_size (properties) == 0)
- return;
-
- for (l = selectors; l; l = l->next)
+ if (ruleset->style == NULL && ruleset->widget_style == NULL)
{
- SelectorPath *path = l->data;
- SelectorStyleInfo *info;
-
- info = selector_style_info_new (path);
- selector_style_info_set_style (info, properties);
-
- g_ptr_array_add (priv->selectors_info, info);
+ g_slist_free_full (selectors, (GDestroyNotify) _gtk_css_selector_free);
+ return;
}
-}
-
-static void
-resolve_binding_sets (const gchar *value_str,
- GValue *value)
-{
- GPtrArray *array;
- gchar **bindings, **str;
-
- bindings = g_strsplit (value_str, ",", -1);
- array = g_ptr_array_new ();
- for (str = bindings; *str; str++)
+ for (l = selectors; l; l = l->next)
{
- GtkBindingSet *binding_set;
-
- binding_set = gtk_binding_set_find (g_strstrip (*str));
+ GtkCssRuleset new;
- if (!binding_set)
- continue;
+ gtk_css_ruleset_init_copy (&new, ruleset, l->data);
- g_ptr_array_add (array, binding_set);
+ g_array_append_val (priv->rulesets, new);
}
- g_value_take_boxed (value, array);
- g_strfreev (bindings);
+ g_slist_free (selectors);
}
static void
gtk_css_provider_reset (GtkCssProvider *css_provider)
{
GtkCssProviderPrivate *priv;
+ guint i;
priv = css_provider->priv;
- if (priv->selectors_info->len > 0)
- g_ptr_array_remove_range (priv->selectors_info, 0, priv->selectors_info->len);
+ g_hash_table_remove_all (priv->symbolic_colors);
+
+ for (i = 0; i < priv->rulesets->len; i++)
+ gtk_css_ruleset_clear (&g_array_index (priv->rulesets, GtkCssRuleset, i));
+ g_array_set_size (priv->rulesets, 0);
}
static void
const GError *error,
GError **propagate_to)
{
+ /* don't fail for deprecations */
+ if (g_error_matches (error, GTK_CSS_PROVIDER_ERROR, GTK_CSS_PROVIDER_ERROR_DEPRECATED))
+ {
+ g_warning ("Theme parsing error: %s:%u:%u: %s", path ? path : "<unknown>", line, position, error->message);
+ return;
+ }
+
/* we already set an error. And we'd like to keep the first one */
if (*propagate_to)
return;
}
static gboolean
-parse_selector_pseudo_class (GtkCssScanner *scanner, SelectorPath *path)
-{
- struct {
- const char *name;
- GtkStateFlags flag;
- } classes[] = {
- { "active", GTK_STATE_FLAG_ACTIVE },
- { "prelight", GTK_STATE_FLAG_PRELIGHT },
- { "hover", GTK_STATE_FLAG_PRELIGHT },
- { "selected", GTK_STATE_FLAG_SELECTED },
- { "insensitive", GTK_STATE_FLAG_INSENSITIVE },
- { "inconsistent", GTK_STATE_FLAG_INCONSISTENT },
- { "focused", GTK_STATE_FLAG_FOCUSED },
- { "focus", GTK_STATE_FLAG_FOCUSED }
- };
- guint i;
-
- for (i = 0; i < G_N_ELEMENTS (classes); i++)
- {
- if (_gtk_css_parser_try (scanner->parser, classes[i].name, FALSE))
- {
- path->state |= classes[i].flag;
- return TRUE;
- }
- }
-
- gtk_css_provider_error_literal (scanner->provider,
- scanner,
- GTK_CSS_PROVIDER_ERROR,
- GTK_CSS_PROVIDER_ERROR_SYNTAX,
- "Expected a valid state name");
- return FALSE;
-}
-
-static gboolean
-parse_selector_class (GtkCssScanner *scanner, SelectorPath *path)
+parse_selector_class (GtkCssScanner *scanner, GArray *classes)
{
- char *name = _gtk_css_parser_try_name (scanner->parser, FALSE);
+ GQuark qname;
+ char *name;
+
+ name = _gtk_css_parser_try_name (scanner->parser, FALSE);
if (name == NULL)
{
scanner,
GTK_CSS_PROVIDER_ERROR,
GTK_CSS_PROVIDER_ERROR_SYNTAX,
- "Expected a valid name");
+ "Expected a valid name for class");
return FALSE;
}
- selector_path_prepend_combinator (path, COMBINATOR_CHILD);
- selector_path_prepend_class (path, name);
+ qname = g_quark_from_string (name);
+ g_array_append_val (classes, qname);
+ g_free (name);
return TRUE;
}
static gboolean
-parse_selector_name (GtkCssScanner *scanner, SelectorPath *path)
+parse_selector_name (GtkCssScanner *scanner, GArray *names)
{
- char *name = _gtk_css_parser_try_name (scanner->parser, FALSE);
+ GQuark qname;
+ char *name;
+
+ name = _gtk_css_parser_try_name (scanner->parser, FALSE);
if (name == NULL)
{
scanner,
GTK_CSS_PROVIDER_ERROR,
GTK_CSS_PROVIDER_ERROR_SYNTAX,
- "Expected a valid name");
+ "Expected a valid name for id");
return FALSE;
}
- selector_path_prepend_combinator (path, COMBINATOR_CHILD);
- selector_path_prepend_name (path, name);
+ qname = g_quark_from_string (name);
+ g_array_append_val (names, qname);
+ g_free (name);
return TRUE;
}
static gboolean
-parse_selector_pseudo_class_for_region (GtkCssScanner *scanner,
- SelectorPath *path,
- GtkRegionFlags *flags_to_modify)
+parse_selector_pseudo_class (GtkCssScanner *scanner,
+ GtkRegionFlags *region_to_modify,
+ GtkStateFlags *state_to_modify)
{
struct {
const char *name;
- GtkRegionFlags flag;
- } classes[] = {
- { "first", GTK_REGION_FIRST },
- { "last", GTK_REGION_LAST },
- { "sorted", GTK_REGION_SORTED }
- }, nth_child[] = {
- { "first", GTK_REGION_FIRST },
- { "last", GTK_REGION_LAST },
- { "even", GTK_REGION_EVEN },
- { "odd", GTK_REGION_ODD }
- };
+ GtkRegionFlags region_flag;
+ GtkStateFlags state_flag;
+ } pseudo_classes[] = {
+ { "first", GTK_REGION_FIRST, 0 },
+ { "last", GTK_REGION_LAST, 0 },
+ { "sorted", GTK_REGION_SORTED, 0 },
+ { "active", 0, GTK_STATE_FLAG_ACTIVE },
+ { "prelight", 0, GTK_STATE_FLAG_PRELIGHT },
+ { "hover", 0, GTK_STATE_FLAG_PRELIGHT },
+ { "selected", 0, GTK_STATE_FLAG_SELECTED },
+ { "insensitive", 0, GTK_STATE_FLAG_INSENSITIVE },
+ { "inconsistent", 0, GTK_STATE_FLAG_INCONSISTENT },
+ { "focused", 0, GTK_STATE_FLAG_FOCUSED },
+ { "focus", 0, GTK_STATE_FLAG_FOCUSED },
+ { NULL, }
+ }, nth_child_classes[] = {
+ { "first", GTK_REGION_FIRST, 0 },
+ { "last", GTK_REGION_LAST, 0 },
+ { "even", GTK_REGION_EVEN, 0 },
+ { "odd", GTK_REGION_ODD, 0 },
+ { NULL, }
+ }, *classes;
guint i;
+ char *name;
- for (i = 0; i < G_N_ELEMENTS (classes); i++)
- {
- if (_gtk_css_parser_try (scanner->parser, classes[i].name, FALSE))
- {
- *flags_to_modify |=classes[i].flag;
- return TRUE;
- }
- }
-
- if (!_gtk_css_parser_try (scanner->parser, "nth-child(", TRUE))
- return parse_selector_pseudo_class (scanner, path);
-
- for (i = 0; i < G_N_ELEMENTS (nth_child); i++)
- {
- if (_gtk_css_parser_try (scanner->parser, nth_child[i].name, TRUE))
- {
- *flags_to_modify |= nth_child[i].flag;
- break;
- }
- }
-
- if (i == G_N_ELEMENTS (nth_child))
+ name = _gtk_css_parser_try_ident (scanner->parser, FALSE);
+ if (name == NULL)
{
gtk_css_provider_error_literal (scanner->provider,
scanner,
GTK_CSS_PROVIDER_ERROR,
GTK_CSS_PROVIDER_ERROR_SYNTAX,
- "Not a valid value for nth-child");
+ "Missing name of pseudo-class");
return FALSE;
}
- if (!_gtk_css_parser_try (scanner->parser, ")", FALSE))
+ if (_gtk_css_parser_try (scanner->parser, "(", TRUE))
{
- gtk_css_provider_error_literal (scanner->provider,
- scanner,
- GTK_CSS_PROVIDER_ERROR,
- GTK_CSS_PROVIDER_ERROR_SYNTAX,
- "Missing closing bracket");
- return FALSE;
- }
+ char *function = name;
- return TRUE;
-}
+ name = _gtk_css_parser_try_ident (scanner->parser, TRUE);
+ if (!_gtk_css_parser_try (scanner->parser, ")", FALSE))
+ {
+ gtk_css_provider_error_literal (scanner->provider,
+ scanner,
+ GTK_CSS_PROVIDER_ERROR,
+ GTK_CSS_PROVIDER_ERROR_SYNTAX,
+ "Missing closing bracket for pseudo-class");
+ return FALSE;
+ }
-static gboolean
-parse_simple_selector (GtkCssScanner *scanner, SelectorPath *path)
-{
- char *name;
- gboolean parsed_something;
-
- name = _gtk_css_parser_try_ident (scanner->parser, FALSE);
- if (name)
- {
- if (_gtk_style_context_check_region_name (name))
+ if (g_ascii_strcasecmp (function, "nth-child") != 0)
+ {
+ gtk_css_provider_error (scanner->provider,
+ scanner,
+ GTK_CSS_PROVIDER_ERROR,
+ GTK_CSS_PROVIDER_ERROR_UNKNOWN_VALUE,
+ "Unknown pseudo-class '%s(%s)'", function, name ? name : "");
+ g_free (function);
+ g_free (name);
+ return FALSE;
+ }
+
+ g_free (function);
+
+ if (name == NULL)
{
- GtkRegionFlags flags;
-
- flags = 0;
+ gtk_css_provider_error (scanner->provider,
+ scanner,
+ GTK_CSS_PROVIDER_ERROR,
+ GTK_CSS_PROVIDER_ERROR_UNKNOWN_VALUE,
+ "nth-child() requires an argument");
+ return FALSE;
+ }
+
+ classes = nth_child_classes;
+ }
+ else
+ classes = pseudo_classes;
- while (_gtk_css_parser_try (scanner->parser, ":", FALSE))
+ for (i = 0; classes[i].name != NULL; i++)
+ {
+ if (g_ascii_strcasecmp (name, classes[i].name) == 0)
+ {
+ if ((*region_to_modify & classes[i].region_flag) ||
+ (*state_to_modify & classes[i].state_flag))
{
- if (!parse_selector_pseudo_class_for_region (scanner, path, &flags))
- {
- g_free (name);
- return FALSE;
- }
+ if (classes == nth_child_classes)
+ gtk_css_provider_error (scanner->provider,
+ scanner,
+ GTK_CSS_PROVIDER_ERROR,
+ GTK_CSS_PROVIDER_ERROR_SYNTAX,
+ "Duplicate pseudo-class 'nth-child(%s)'", name);
+ else
+ gtk_css_provider_error (scanner->provider,
+ scanner,
+ GTK_CSS_PROVIDER_ERROR,
+ GTK_CSS_PROVIDER_ERROR_SYNTAX,
+ "Duplicate pseudo-class '%s'", name);
}
+ *region_to_modify |= classes[i].region_flag;
+ *state_to_modify |= classes[i].state_flag;
- selector_path_prepend_region (path, name, flags);
g_free (name);
- _gtk_css_parser_skip_whitespace (scanner->parser);
return TRUE;
}
- else
- {
- selector_path_prepend_type (path, name);
- parsed_something = TRUE;
- }
}
+
+ if (classes == nth_child_classes)
+ gtk_css_provider_error (scanner->provider,
+ scanner,
+ GTK_CSS_PROVIDER_ERROR,
+ GTK_CSS_PROVIDER_ERROR_UNKNOWN_VALUE,
+ "Unknown pseudo-class 'nth-child(%s)'", name);
else
- {
- parsed_something = _gtk_css_parser_try (scanner->parser, "*", FALSE);
- selector_path_prepend_glob (path);
- }
+ gtk_css_provider_error (scanner->provider,
+ scanner,
+ GTK_CSS_PROVIDER_ERROR,
+ GTK_CSS_PROVIDER_ERROR_UNKNOWN_VALUE,
+ "Unknown pseudo-class '%s'", name);
+ g_free (name);
+ return FALSE;
+}
+
+static gboolean
+parse_simple_selector (GtkCssScanner *scanner,
+ char **name,
+ GArray *ids,
+ GArray *classes,
+ GtkRegionFlags *pseudo_classes,
+ GtkStateFlags *state)
+{
+ gboolean parsed_something;
+
+ *name = _gtk_css_parser_try_ident (scanner->parser, FALSE);
+ if (*name)
+ parsed_something = TRUE;
+ else
+ parsed_something = _gtk_css_parser_try (scanner->parser, "*", FALSE);
do {
if (_gtk_css_parser_try (scanner->parser, "#", FALSE))
{
- if (!parse_selector_name (scanner, path))
+ if (!parse_selector_name (scanner, ids))
return FALSE;
}
else if (_gtk_css_parser_try (scanner->parser, ".", FALSE))
{
- if (!parse_selector_class (scanner, path))
+ if (!parse_selector_class (scanner, classes))
return FALSE;
}
else if (_gtk_css_parser_try (scanner->parser, ":", FALSE))
{
- if (!parse_selector_pseudo_class (scanner, path))
+ if (!parse_selector_pseudo_class (scanner, pseudo_classes, state))
return FALSE;
}
else if (!parsed_something)
return TRUE;
}
-static SelectorPath *
+static GtkCssSelector *
parse_selector (GtkCssScanner *scanner)
{
- SelectorPath *path;
-
- path = selector_path_new ();
+ GtkCssSelector *selector = NULL;
do {
- if (!parse_simple_selector (scanner, path))
+ char *name = NULL;
+ GArray *ids = g_array_new (TRUE, FALSE, sizeof (GQuark));
+ GArray *classes = g_array_new (TRUE, FALSE, sizeof (GQuark));
+ GtkRegionFlags pseudo_classes = 0;
+ GtkStateFlags state = 0;
+ GtkCssCombinator combine = GTK_CSS_COMBINE_DESCANDANT;
+
+ if (selector)
+ {
+ if (_gtk_css_parser_try (scanner->parser, ">", TRUE))
+ combine = GTK_CSS_COMBINE_CHILD;
+ }
+
+ if (!parse_simple_selector (scanner, &name, ids, classes, &pseudo_classes, &state))
{
- selector_path_unref (path);
+ g_array_free (ids, TRUE);
+ g_array_free (classes, TRUE);
+ if (selector)
+ _gtk_css_selector_free (selector);
return NULL;
}
- if (_gtk_css_parser_try (scanner->parser, ">", TRUE))
- selector_path_prepend_combinator (path, COMBINATOR_CHILD);
+ selector = _gtk_css_selector_new (selector,
+ combine,
+ name,
+ (GQuark *) g_array_free (ids, ids->len == 0),
+ (GQuark *) g_array_free (classes, classes->len == 0),
+ pseudo_classes,
+ state);
+ g_free (name);
}
- while (path->state == 0 &&
- !_gtk_css_parser_is_eof (scanner->parser) &&
+ while (!_gtk_css_parser_is_eof (scanner->parser) &&
!_gtk_css_parser_begins_with (scanner->parser, ',') &&
!_gtk_css_parser_begins_with (scanner->parser, '{'));
- return path;
+ return selector;
}
static GSList *
GSList *selectors = NULL;
do {
- SelectorPath *path = parse_selector (scanner);
+ GtkCssSelector *select = parse_selector (scanner);
- if (path == NULL)
+ if (select == NULL)
{
- g_slist_free_full (selectors, (GDestroyNotify) selector_path_unref);
+ g_slist_free_full (selectors, (GDestroyNotify) _gtk_css_selector_free);
_gtk_css_parser_resync (scanner->parser, FALSE, 0);
return NULL;
}
- selectors = g_slist_prepend (selectors, path);
+ selectors = g_slist_prepend (selectors, select);
}
while (_gtk_css_parser_try (scanner->parser, ",", TRUE));
}
static void
-parse_declaration (GtkCssScanner *scanner, GHashTable *properties)
+parse_declaration (GtkCssScanner *scanner,
+ GtkCssRuleset *ruleset)
{
- GtkStylePropertyParser parse_func = NULL;
- GParamSpec *pspec = NULL;
- char *name, *value_str;
+ const GtkStyleProperty *property;
+ char *name;
name = _gtk_css_parser_try_ident (scanner->parser, TRUE);
if (name == NULL)
goto check_for_semicolon;
- if (!gtk_style_properties_lookup_property (name, &parse_func, &pspec) &&
- name[0] != '-')
+ property = _gtk_style_property_lookup (name);
+ if (property == NULL && name[0] != '-')
{
gtk_css_provider_error (scanner->provider,
scanner,
return;
}
- value_str = _gtk_css_parser_read_value (scanner->parser);
- if (value_str == NULL)
- {
- _gtk_css_parser_resync (scanner->parser, TRUE, '}');
- g_free (name);
- return;
- }
-
- if (pspec)
+ if (property)
{
GValue *val;
+ g_free (name);
+
val = g_slice_new0 (GValue);
- g_value_init (val, pspec->value_type);
+ g_value_init (val, property->pspec->value_type);
- if (strcmp (value_str, "none") == 0)
- {
- /* Insert the default value, so it has an opportunity
- * to override other style providers when merged
- */
- g_param_value_set_default (pspec, val);
- g_hash_table_insert (properties, name, val);
- }
- else if (strcmp (name, "gtk-key-bindings") == 0)
- {
- /* Private property holding the binding sets */
- resolve_binding_sets (value_str, val);
- g_hash_table_insert (properties, name, val);
- }
- else if (parse_func)
- {
- GError *error = NULL;
-
- if ((*parse_func) (value_str, val, &error))
- g_hash_table_insert (properties, name, val);
- else
- gtk_css_provider_take_error (scanner->provider, scanner, error);
- }
- else
+ if (_gtk_style_property_parse_value (property,
+ val,
+ scanner->parser,
+ gtk_css_scanner_get_base_url (scanner)))
{
- GError *error = NULL;
-
- if (_gtk_css_value_from_string (val,
- gtk_css_scanner_get_base_url (scanner),
- value_str,
- &error))
+ if (_gtk_css_parser_begins_with (scanner->parser, ';') ||
+ _gtk_css_parser_begins_with (scanner->parser, '}') ||
+ _gtk_css_parser_is_eof (scanner->parser))
{
- g_hash_table_insert (properties, name, val);
+ gtk_css_ruleset_add (ruleset, property, val);
}
else
{
+ gtk_css_provider_error_literal (scanner->provider,
+ scanner,
+ GTK_CSS_PROVIDER_ERROR,
+ GTK_CSS_PROVIDER_ERROR_SYNTAX,
+ "Junk at end of value");
+ _gtk_css_parser_resync (scanner->parser, TRUE, '}');
g_value_unset (val);
g_slice_free (GValue, val);
- g_free (name);
-
- gtk_css_provider_take_error (scanner->provider, scanner, error);
+ return;
}
}
+ else
+ {
+ g_value_unset (val);
+ g_slice_free (GValue, val);
+ _gtk_css_parser_resync (scanner->parser, TRUE, '}');
+ return;
+ }
}
else if (name[0] == '-')
{
- GValue *val;
+ char *value_str;
- val = g_slice_new0 (GValue);
- g_value_init (val, G_TYPE_STRING);
- g_value_set_string (val, value_str);
+ value_str = _gtk_css_parser_read_value (scanner->parser);
+ if (value_str)
+ {
+ GValue *val;
- g_hash_table_insert (properties, name, val);
+ val = g_slice_new0 (GValue);
+ g_value_init (val, G_TYPE_STRING);
+ g_value_take_string (val, value_str);
+
+ gtk_css_ruleset_add_style (ruleset, name, val);
+ }
+ else
+ {
+ _gtk_css_parser_resync (scanner->parser, TRUE, '}');
+ return;
+ }
}
else
g_free (name);
- g_free (value_str);
-
check_for_semicolon:
if (!_gtk_css_parser_try (scanner->parser, ";", TRUE))
{
}
}
-static GHashTable *
-parse_declarations (GtkCssScanner *scanner)
+static void
+parse_declarations (GtkCssScanner *scanner,
+ GtkCssRuleset *ruleset)
{
- GHashTable *properties;
-
- properties = g_hash_table_new_full (g_str_hash,
- g_str_equal,
- (GDestroyNotify) g_free,
- (GDestroyNotify) property_value_free);
-
while (!_gtk_css_parser_is_eof (scanner->parser) &&
!_gtk_css_parser_begins_with (scanner->parser, '}'))
{
- parse_declaration (scanner, properties);
+ parse_declaration (scanner, ruleset);
}
-
- return properties;
}
static void
parse_ruleset (GtkCssScanner *scanner)
{
GSList *selectors;
- GHashTable *properties;
+ GtkCssRuleset ruleset = { 0, };
selectors = parse_selector_list (scanner);
if (selectors == NULL)
GTK_CSS_PROVIDER_ERROR_SYNTAX,
"expected '{' after selectors");
_gtk_css_parser_resync (scanner->parser, FALSE, 0);
- g_slist_free_full (selectors, (GDestroyNotify) selector_path_unref);
+ g_slist_free_full (selectors, (GDestroyNotify) _gtk_css_selector_free);
return;
}
- properties = parse_declarations (scanner);
+ parse_declarations (scanner, &ruleset);
if (!_gtk_css_parser_try (scanner->parser, "}", TRUE))
{
if (!_gtk_css_parser_is_eof (scanner->parser))
{
_gtk_css_parser_resync (scanner->parser, FALSE, 0);
- g_slist_free_full (selectors, (GDestroyNotify) selector_path_unref);
- if (properties)
- g_hash_table_unref (properties);
+ g_slist_free_full (selectors, (GDestroyNotify) _gtk_css_selector_free);
+ gtk_css_ruleset_clear (&ruleset);
return;
}
}
- if (properties)
- {
- css_provider_commit (scanner->provider, selectors, properties);
- g_hash_table_unref (properties);
- }
- g_slist_free_full (selectors, (GDestroyNotify) selector_path_unref);
+ css_provider_commit (scanner->provider, selectors, &ruleset);
+ gtk_css_ruleset_clear (&ruleset);
}
static void
}
static void
-parse_stylesheet (GtkCssScanner *scanner)
+parse_stylesheet (GtkCssScanner *scanner)
{
_gtk_css_parser_skip_whitespace (scanner->parser);
}
}
+static int
+gtk_css_provider_compare_rule (gconstpointer a_,
+ gconstpointer b_)
+{
+ const GtkCssRuleset *a = (const GtkCssRuleset *) a_;
+ const GtkCssRuleset *b = (const GtkCssRuleset *) b_;
+ int compare;
+
+ compare = _gtk_css_selector_compare (a->selector, b->selector);
+ if (compare != 0)
+ return compare;
+
+ /* compare pointers in array to ensure a stable sort */
+ if (a_ < b_)
+ return -1;
+
+ if (a_ > b_)
+ return 1;
+
+ return 0;
+}
+
+static void
+gtk_css_provider_postprocess (GtkCssProvider *css_provider)
+{
+ GtkCssProviderPrivate *priv = css_provider->priv;
+
+ g_array_sort (priv->rulesets, gtk_css_provider_compare_rule);
+}
+
static gboolean
gtk_css_provider_load_internal (GtkCssProvider *css_provider,
GtkCssScanner *parent,
{
GtkCssScanner *scanner;
gulong error_handler;
- char *free_data;
+ char *free_data = NULL;
if (error)
error_handler = g_signal_connect (css_provider,
}
}
}
- else
- free_data = NULL;
if (data)
{
parse_stylesheet (scanner);
gtk_css_scanner_destroy (scanner);
+
+ if (parent == NULL)
+ gtk_css_provider_postprocess (css_provider);
}
+ g_free (free_data);
+
if (error)
{
g_signal_handler_disconnect (css_provider, error_handler);
-
+
if (*error)
{
/* We clear all contents from the provider for backwards compat reasons */
/**
* gtk_css_provider_load_from_data:
* @css_provider: a #GtkCssProvider
- * @data: CSS data loaded in memory
+ * @data: (array length=length) (element-type guint8): CSS data loaded in memory
* @length: the length of @data in bytes, or -1 for NUL terminated strings
* @error: (out) (allow-none): return location for a #GError, or %NULL
*
if (path)
{
- GError *error;
-
provider = gtk_css_provider_new ();
- error = NULL;
- if (!gtk_css_provider_load_from_path (provider, path, &error))
- {
- g_warning ("Could not load named theme \"%s\": %s", name, error->message);
- g_error_free (error);
+ if (!gtk_css_provider_load_from_path (provider, path, NULL))
+ {
g_object_unref (provider);
provider = NULL;
}
return provider;
}
+static int
+compare_properties (gconstpointer a, gconstpointer b)
+{
+ return strcmp (((const GtkStyleProperty *) a)->pspec->name,
+ ((const GtkStyleProperty *) b)->pspec->name);
+}
+
static void
-selector_path_print (const SelectorPath *path,
- GString * str)
+gtk_css_ruleset_print (const GtkCssRuleset *ruleset,
+ GString *str)
{
- GSList *walk, *reverse;
+ GList *keys, *walk;
- reverse = g_slist_copy (path->elements);
- reverse = g_slist_reverse (reverse);
+ _gtk_css_selector_print (ruleset->selector, str);
+
+ g_string_append (str, " {\n");
- for (walk = reverse; walk; walk = walk->next)
+ if (ruleset->style)
{
- SelectorElement *elem = walk->data;
+ keys = g_hash_table_get_keys (ruleset->style);
+ /* so the output is identical for identical selector styles */
+ keys = g_list_sort (keys, compare_properties);
- switch (elem->elem_type)
+ for (walk = keys; walk; walk = walk->next)
{
- case SELECTOR_TYPE_NAME:
- case SELECTOR_NAME:
- g_string_append (str, g_quark_to_string (elem->name));
- break;
- case SELECTOR_GTYPE:
- g_string_append (str, g_type_name (elem->type));
- break;
- case SELECTOR_REGION:
- g_string_append (str, g_quark_to_string (elem->region.name));
- if (elem->region.flags)
- {
- char * flag_names[] = {
- "nth-child(even)",
- "nth-child(odd)",
- "first-child",
- "last-child",
- "sorted"
- };
- guint i;
-
- for (i = 0; i < G_N_ELEMENTS (flag_names); i++)
- {
- if (elem->region.flags & (1 << i))
- {
- g_string_append_c (str, ':');
- g_string_append (str, flag_names[i]);
- }
- }
- }
- break;
- case SELECTOR_CLASS:
- g_string_append_c (str, '.');
- g_string_append (str, g_quark_to_string (elem->name));
- break;
- case SELECTOR_GLOB:
- if (walk->next == NULL ||
- elem->combinator != COMBINATOR_CHILD ||
- ((SelectorElement *) walk->next->data)->elem_type != SELECTOR_CLASS)
- g_string_append (str, "*");
- break;
- default:
- g_assert_not_reached ();
+ GtkStyleProperty *prop = walk->data;
+ const GValue *value = g_hash_table_lookup (ruleset->style, prop);
+
+ g_string_append (str, " ");
+ g_string_append (str, prop->pspec->name);
+ g_string_append (str, ": ");
+ _gtk_style_property_print_value (prop, value, str);
+ g_string_append (str, ";\n");
}
- if (walk->next)
- {
- switch (elem->combinator)
- {
- case COMBINATOR_DESCENDANT:
- if (elem->elem_type != SELECTOR_CLASS ||
- ((SelectorElement *) walk->next->data)->elem_type != SELECTOR_CLASS)
- g_string_append_c (str, ' ');
- break;
- case COMBINATOR_CHILD:
- if (((SelectorElement *) walk->next->data)->elem_type != SELECTOR_CLASS)
- g_string_append (str, " > ");
- break;
- default:
- g_assert_not_reached ();
- }
- }
-
+ g_list_free (keys);
}
- if (path->state)
+ if (ruleset->widget_style)
{
- char * state_names[] = {
- "active",
- "hover",
- "selected",
- "insensitive",
- "inconsistent",
- "focus"
- };
- guint i;
-
- for (i = 0; i < G_N_ELEMENTS (state_names); i++)
+ keys = g_hash_table_get_keys (ruleset->widget_style);
+ /* so the output is identical for identical selector styles */
+ keys = g_list_sort (keys, (GCompareFunc) strcmp);
+
+ for (walk = keys; walk; walk = walk->next)
{
- if (path->state & (1 << i))
- {
- g_string_append_c (str, ':');
- g_string_append (str, state_names[i]);
- }
+ const char *name = walk->data;
+ const GValue *value = g_hash_table_lookup (ruleset->widget_style, (gpointer) name);
+
+ g_string_append (str, " ");
+ g_string_append (str, name);
+ g_string_append (str, ": ");
+ g_string_append (str, g_value_get_string (value));
+ g_string_append (str, ";\n");
}
- }
-
- g_slist_free (reverse);
-}
-
-static void
-selector_style_info_print (const SelectorStyleInfo *info,
- GString *str)
-{
- GList *keys, *walk;
- char *s;
-
- selector_path_print (info->path, str);
-
- g_string_append (str, " {\n");
-
- keys = g_hash_table_get_keys (info->style);
- /* so the output is identical for identical selector styles */
- keys = g_list_sort (keys, (GCompareFunc) strcmp);
-
- for (walk = keys; walk; walk = walk->next)
- {
- const char *name = walk->data;
- const GValue *value = g_hash_table_lookup (info->style, (gpointer) name);
- g_string_append (str, " ");
- g_string_append (str, name);
- g_string_append (str, ": ");
- s = _gtk_css_value_to_string (value);
- g_string_append (str, s);
- g_free (s);
- g_string_append (str, ";\n");
+ g_list_free (keys);
}
- g_list_free (keys);
-
g_string_append (str, "}\n");
}
gtk_css_provider_print_colors (priv->symbolic_colors, str);
- for (i = 0; i < priv->selectors_info->len; i++)
+ for (i = 0; i < priv->rulesets->len; i++)
{
if (i > 0)
g_string_append (str, "\n");
- selector_style_info_print (g_ptr_array_index (priv->selectors_info, i),
- str);
+ gtk_css_ruleset_print (&g_array_index (priv->rulesets, GtkCssRuleset, i), str);
}
return g_string_free (str, FALSE);