1 /* GTK - The GIMP Toolkit
2 * Copyright (C) 2011 Red Hat, Inc.
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library. If not, see <http://www.gnu.org/licenses/>.
20 #include "gtkcsssectionprivate.h"
22 #include "gtkcssparserprivate.h"
27 * Defines a part of a CSS document. Because sections are nested into
28 * one another, you can use gtk_css_section_get_parent() to get the
36 volatile gint ref_count;
37 GtkCssSectionType section_type;
38 GtkCssSection *parent;
42 GtkCssParser *parser; /* parser if section isn't finished parsing yet or %NULL */
43 guint end_line; /* end line if parser is %NULL */
44 guint end_position; /* end position if parser is %NULL */
47 G_DEFINE_BOXED_TYPE (GtkCssSection, gtk_css_section, gtk_css_section_ref, gtk_css_section_unref)
50 _gtk_css_section_new (GtkCssSection *parent,
51 GtkCssSectionType type,
55 GtkCssSection *section;
57 g_return_val_if_fail (parser != NULL, NULL);
58 g_return_val_if_fail (file == NULL || G_IS_FILE (file), NULL);
60 section = g_slice_new0 (GtkCssSection);
62 section->ref_count = 1;
63 section->section_type = type;
65 section->parent = gtk_css_section_ref (parent);
67 section->file = g_object_ref (file);
68 section->start_line = _gtk_css_parser_get_line (parser);
69 section->start_position = _gtk_css_parser_get_position (parser);
70 section->parser = parser;
76 _gtk_css_section_new_for_file (GtkCssSectionType type,
79 GtkCssSection *section;
81 g_return_val_if_fail (G_IS_FILE (file), NULL);
83 section = g_slice_new0 (GtkCssSection);
85 section->ref_count = 1;
86 section->section_type = type;
87 section->file = g_object_ref (file);
93 _gtk_css_section_end (GtkCssSection *section)
95 g_return_if_fail (section != NULL);
96 g_return_if_fail (section->parser != NULL);
98 section->end_line = _gtk_css_parser_get_line (section->parser);
99 section->end_position = _gtk_css_parser_get_position (section->parser);
100 section->parser = NULL;
104 * gtk_css_section_ref:
105 * @section: a #GtkCssSection
107 * Increments the reference count on @section.
109 * Returns: @section itself.
114 gtk_css_section_ref (GtkCssSection *section)
116 g_return_val_if_fail (section != NULL, NULL);
118 g_atomic_int_add (§ion->ref_count, 1);
124 * gtk_css_section_unref:
125 * @section: a #GtkCssSection
127 * Decrements the reference count on @section, freeing the
128 * structure if the reference count reaches 0.
133 gtk_css_section_unref (GtkCssSection *section)
135 g_return_if_fail (section != NULL);
137 if (!g_atomic_int_dec_and_test (§ion->ref_count))
141 gtk_css_section_unref (section->parent);
143 g_object_unref (section->file);
145 g_slice_free (GtkCssSection, section);
149 * gtk_css_section_get_section_type:
150 * @section: the section
152 * Gets the type of information that @section describes.
154 * Returns: the type of @section
159 gtk_css_section_get_section_type (const GtkCssSection *section)
161 g_return_val_if_fail (section != NULL, GTK_CSS_SECTION_DOCUMENT);
163 return section->section_type;
167 * gtk_css_section_get_parent:
168 * @section: the section
170 * Gets the parent section for the given @section. The parent section is
171 * the section that contains this @section. A special case are sections of
172 * type #GTK_CSS_SECTION_TYPE_DOCUMENT. Their parent will either be %NULL
173 * if they are the original CSS document that was loaded by
174 * gtk_css_provider_load_from_file() or a section of type
175 * #GTK_CSS_SECTION_TYPE_IMPORT if it was loaded with an import rule from
178 * Returns: the parent section or %NULL if none
183 gtk_css_section_get_parent (const GtkCssSection *section)
185 g_return_val_if_fail (section != NULL, NULL);
187 return section->parent;
191 * gtk_css_section_get_file:
192 * @section: the section
194 * Gets the file that @section was parsed from. If no such file exists,
195 * for example because the CSS was loaded via
196 * @gtk_css_provider_load_from_data(), then %NULL is returned.
198 * Returns: (transfer none): the #GFile that @section was parsed from
199 * or %NULL if @section was parsed from other data
204 gtk_css_section_get_file (const GtkCssSection *section)
206 g_return_val_if_fail (section != NULL, NULL);
208 return section->file;
212 * gtk_css_section_get_start_line:
213 * @section: the section
215 * Returns the line in the CSS document where this section starts.
216 * The line number is 0-indexed, so the first line of the document
219 * Returns: the line number
224 gtk_css_section_get_start_line (const GtkCssSection *section)
226 g_return_val_if_fail (section != NULL, 0);
228 return section->start_line;
232 * gtk_css_section_get_start_position:
233 * @section: the section
235 * Returns the offset in bytes from the start of the current line
236 * returned via gtk_css_section_get_start_line().
238 * Returns: the offset in bytes from the start of the line.
243 gtk_css_section_get_start_position (const GtkCssSection *section)
245 g_return_val_if_fail (section != NULL, 0);
247 return section->start_position;
251 * gtk_css_section_get_end_line:
252 * @section: the section
254 * Returns the line in the CSS document where this section end.
255 * The line number is 0-indexed, so the first line of the document
257 * This value may change in future invocations of this function if
258 * @section is not yet parsed completely. This will for example
259 * happen in the GtkCssProvider::parsing-error signal.
260 * The end position and line may be identical to the start
261 * position and line for sections which failed to parse anything
264 * Returns: the line number
269 gtk_css_section_get_end_line (const GtkCssSection *section)
271 g_return_val_if_fail (section != NULL, 0);
274 return _gtk_css_parser_get_line (section->parser);
276 return section->end_line;
280 * gtk_css_section_get_end_position:
281 * @section: the section
283 * Returns the offset in bytes from the start of the current line
284 * returned via gtk_css_section_get_end_line().
285 * This value may change in future invocations of this function if
286 * @section is not yet parsed completely. This will for example
287 * happen in the GtkCssProvider::parsing-error signal.
288 * The end position and line may be identical to the start
289 * position and line for sections which failed to parse anything
292 * Returns: the offset in bytes from the start of the line.
297 gtk_css_section_get_end_position (const GtkCssSection *section)
299 g_return_val_if_fail (section != NULL, 0);
302 return _gtk_css_parser_get_position (section->parser);
304 return section->end_position;