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,
54 GtkCssSection *section;
56 g_return_val_if_fail (parser != NULL, NULL);
58 section = g_slice_new0 (GtkCssSection);
60 section->ref_count = 1;
61 section->section_type = type;
63 section->parent = gtk_css_section_ref (parent);
64 section->file = _gtk_css_parser_get_file (parser);
66 g_object_ref (section->file);
67 section->start_line = _gtk_css_parser_get_line (parser);
68 section->start_position = _gtk_css_parser_get_position (parser);
69 section->parser = parser;
75 _gtk_css_section_new_for_file (GtkCssSectionType type,
78 GtkCssSection *section;
80 g_return_val_if_fail (G_IS_FILE (file), NULL);
82 section = g_slice_new0 (GtkCssSection);
84 section->ref_count = 1;
85 section->section_type = type;
86 section->file = g_object_ref (file);
92 _gtk_css_section_end (GtkCssSection *section)
94 g_return_if_fail (section != NULL);
95 g_return_if_fail (section->parser != NULL);
97 section->end_line = _gtk_css_parser_get_line (section->parser);
98 section->end_position = _gtk_css_parser_get_position (section->parser);
99 section->parser = NULL;
103 * gtk_css_section_ref:
104 * @section: a #GtkCssSection
106 * Increments the reference count on @section.
108 * Returns: @section itself.
113 gtk_css_section_ref (GtkCssSection *section)
115 g_return_val_if_fail (section != NULL, NULL);
117 g_atomic_int_add (§ion->ref_count, 1);
123 * gtk_css_section_unref:
124 * @section: a #GtkCssSection
126 * Decrements the reference count on @section, freeing the
127 * structure if the reference count reaches 0.
132 gtk_css_section_unref (GtkCssSection *section)
134 g_return_if_fail (section != NULL);
136 if (!g_atomic_int_dec_and_test (§ion->ref_count))
140 gtk_css_section_unref (section->parent);
142 g_object_unref (section->file);
144 g_slice_free (GtkCssSection, section);
148 * gtk_css_section_get_section_type:
149 * @section: the section
151 * Gets the type of information that @section describes.
153 * Returns: the type of @section
158 gtk_css_section_get_section_type (const GtkCssSection *section)
160 g_return_val_if_fail (section != NULL, GTK_CSS_SECTION_DOCUMENT);
162 return section->section_type;
166 * gtk_css_section_get_parent:
167 * @section: the section
169 * Gets the parent section for the given @section. The parent section is
170 * the section that contains this @section. A special case are sections of
171 * type #GTK_CSS_SECTION_DOCUMENT. Their parent will either be %NULL
172 * if they are the original CSS document that was loaded by
173 * gtk_css_provider_load_from_file() or a section of type
174 * #GTK_CSS_SECTION_IMPORT if it was loaded with an import rule from
177 * Returns: the parent section or %NULL if none
182 gtk_css_section_get_parent (const GtkCssSection *section)
184 g_return_val_if_fail (section != NULL, NULL);
186 return section->parent;
190 * gtk_css_section_get_file:
191 * @section: the section
193 * Gets the file that @section was parsed from. If no such file exists,
194 * for example because the CSS was loaded via
195 * @gtk_css_provider_load_from_data(), then %NULL is returned.
197 * Returns: (transfer none): the #GFile that @section was parsed from
198 * or %NULL if @section was parsed from other data
203 gtk_css_section_get_file (const GtkCssSection *section)
205 g_return_val_if_fail (section != NULL, NULL);
207 return section->file;
211 * gtk_css_section_get_start_line:
212 * @section: the section
214 * Returns the line in the CSS document where this section starts.
215 * The line number is 0-indexed, so the first line of the document
218 * Returns: the line number
223 gtk_css_section_get_start_line (const GtkCssSection *section)
225 g_return_val_if_fail (section != NULL, 0);
227 return section->start_line;
231 * gtk_css_section_get_start_position:
232 * @section: the section
234 * Returns the offset in bytes from the start of the current line
235 * returned via gtk_css_section_get_start_line().
237 * Returns: the offset in bytes from the start of the line.
242 gtk_css_section_get_start_position (const GtkCssSection *section)
244 g_return_val_if_fail (section != NULL, 0);
246 return section->start_position;
250 * gtk_css_section_get_end_line:
251 * @section: the section
253 * Returns the line in the CSS document where this section end.
254 * The line number is 0-indexed, so the first line of the document
256 * This value may change in future invocations of this function if
257 * @section is not yet parsed completely. This will for example
258 * happen in the GtkCssProvider::parsing-error signal.
259 * The end position and line may be identical to the start
260 * position and line for sections which failed to parse anything
263 * Returns: the line number
268 gtk_css_section_get_end_line (const GtkCssSection *section)
270 g_return_val_if_fail (section != NULL, 0);
273 return _gtk_css_parser_get_line (section->parser);
275 return section->end_line;
279 * gtk_css_section_get_end_position:
280 * @section: the section
282 * Returns the offset in bytes from the start of the current line
283 * returned via gtk_css_section_get_end_line().
284 * This value may change in future invocations of this function if
285 * @section is not yet parsed completely. This will for example
286 * happen in the GtkCssProvider::parsing-error signal.
287 * The end position and line may be identical to the start
288 * position and line for sections which failed to parse anything
291 * Returns: the offset in bytes from the start of the line.
296 gtk_css_section_get_end_position (const GtkCssSection *section)
298 g_return_val_if_fail (section != NULL, 0);
301 return _gtk_css_parser_get_position (section->parser);
303 return section->end_position;
307 _gtk_css_section_print (const GtkCssSection *section,
314 info = g_file_query_info (section->file, G_FILE_ATTRIBUTE_STANDARD_DISPLAY_NAME, 0, NULL, NULL);
318 g_string_append (string, g_file_info_get_display_name (info));
319 g_object_unref (info);
323 g_string_append (string, "<broken file>");
328 g_string_append (string, "<data>");
331 g_string_append_printf (string, ":%u:%u",
332 gtk_css_section_get_end_line (section) + 1,
333 gtk_css_section_get_end_position (section));
337 _gtk_css_section_to_string (const GtkCssSection *section)
341 g_return_val_if_fail (section != NULL, NULL);
343 string = g_string_new (NULL);
344 _gtk_css_section_print (section, string);
346 return g_string_free (string, FALSE);