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, write to the
16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
22 #include "gtkcsssectionprivate.h"
24 #include "gtkcssparserprivate.h"
29 * Defines a part of a CSS document. Because sections are nested into
30 * one another, you can use gtk_css_section_get_parent() to get the
38 volatile gint ref_count;
39 GtkCssSectionType section_type;
40 GtkCssSection *parent;
44 GtkCssParser *parser; /* parser if section isn't finished parsing yet or %NULL */
45 guint end_line; /* end line if parser is %NULL */
46 guint end_position; /* end position if parser is %NULL */
49 G_DEFINE_BOXED_TYPE (GtkCssSection, gtk_css_section, gtk_css_section_ref, gtk_css_section_unref)
52 _gtk_css_section_new (GtkCssSection *parent,
53 GtkCssSectionType type,
57 GtkCssSection *section;
59 g_return_val_if_fail (parser != NULL, NULL);
60 g_return_val_if_fail (file == NULL || G_IS_FILE (file), NULL);
62 section = g_slice_new0 (GtkCssSection);
64 section->ref_count = 1;
65 section->section_type = type;
67 section->parent = gtk_css_section_ref (parent);
69 section->file = g_object_ref (file);
70 section->start_line = _gtk_css_parser_get_line (parser);
71 section->start_position = _gtk_css_parser_get_position (parser);
72 section->parser = parser;
78 _gtk_css_section_new_for_file (GtkCssSectionType type,
81 GtkCssSection *section;
83 g_return_val_if_fail (G_IS_FILE (file), NULL);
85 section = g_slice_new0 (GtkCssSection);
87 section->ref_count = 1;
88 section->section_type = type;
89 section->file = g_object_ref (file);
95 _gtk_css_section_end (GtkCssSection *section)
97 g_return_if_fail (section != NULL);
98 g_return_if_fail (section->parser != NULL);
100 section->end_line = _gtk_css_parser_get_line (section->parser);
101 section->end_position = _gtk_css_parser_get_position (section->parser);
102 section->parser = NULL;
106 * gtk_css_section_ref:
107 * @section: a #GtkCssSection
109 * Increments the reference count on @section.
111 * Returns: @section itself.
116 gtk_css_section_ref (GtkCssSection *section)
118 g_return_val_if_fail (section != NULL, NULL);
120 g_atomic_int_add (§ion->ref_count, 1);
126 * gtk_css_section_unref:
127 * @section: a #GtkCssSection
129 * Decrements the reference count on @section, freeing the
130 * structure if the reference count reaches 0.
135 gtk_css_section_unref (GtkCssSection *section)
137 g_return_if_fail (section != NULL);
139 if (!g_atomic_int_dec_and_test (§ion->ref_count))
143 gtk_css_section_unref (section->parent);
145 g_object_unref (section->file);
147 g_slice_free (GtkCssSection, section);
151 * gtk_css_section_get_section_type:
152 * @section: the section
154 * Gets the type of information that @section describes.
156 * Returns: the type of @section
161 gtk_css_section_get_section_type (const GtkCssSection *section)
163 g_return_val_if_fail (section != NULL, GTK_CSS_SECTION_DOCUMENT);
165 return section->section_type;
169 * gtk_css_section_get_parent:
170 * @section: the section
172 * Gets the parent section for the given @section. The parent section is
173 * the section that contains this @section. A special case are sections of
174 * type #GTK_CSS_SECTION_TYPE_DOCUMENT. Their parent will either be %NULL
175 * if they are the original CSS document that was loaded by
176 * gtk_css_provider_load_from_file() or a section of type
177 * #GTK_CSS_SECTION_TYPE_IMPORT if it was loaded with an import rule from
180 * Returns: the parent section or %NULL if none
185 gtk_css_section_get_parent (const GtkCssSection *section)
187 g_return_val_if_fail (section != NULL, NULL);
189 return section->parent;
193 * gtk_css_section_get_file:
194 * @section: the section
196 * Gets the file that @section was parsed from. If no such file exists,
197 * for example because the CSS was loaded via
198 * @gtk_css_provider_load_from_data(), then %NULL is returned.
200 * Returns: the #GFile that @section was parsed from or %NULL if
201 * @section was parsed from other data.
206 gtk_css_section_get_file (const GtkCssSection *section)
208 g_return_val_if_fail (section != NULL, NULL);
210 return section->file;
214 * gtk_css_section_get_start_line:
215 * @section: the section
217 * Returns the line in the CSS document where this section starts.
218 * The line number is 0-indexed, so the first line of the document
221 * Returns: the line number
226 gtk_css_section_get_start_line (const GtkCssSection *section)
228 g_return_val_if_fail (section != NULL, 0);
230 return section->start_line;
234 * gtk_css_section_get_start_position:
235 * @section: the section
237 * Returns the offset in bytes from the start of the current line
238 * returned via gtk_css_section_get_start_line().
240 * Returns: the offset in bytes from the start of the line.
245 gtk_css_section_get_start_position (const GtkCssSection *section)
247 g_return_val_if_fail (section != NULL, 0);
249 return section->start_position;
253 * gtk_css_section_get_end_line:
254 * @section: the section
256 * Returns the line in the CSS document where this section end.
257 * The line number is 0-indexed, so the first line of the document
259 * This value may change in future invocations of this function if
260 * @section is not yet parsed completely. This will for example
261 * happen in the GtkCssProvider::parsing-error signal.
262 * The end position and line may be identical to the start
263 * position and line for sections which failed to parse anything
266 * Returns: the line number
271 gtk_css_section_get_end_line (const GtkCssSection *section)
273 g_return_val_if_fail (section != NULL, 0);
276 return _gtk_css_parser_get_line (section->parser);
278 return section->end_line;
282 * gtk_css_section_get_end_position:
283 * @section: the section
285 * Returns the offset in bytes from the start of the current line
286 * returned via gtk_css_section_get_end_line().
287 * This value may change in future invocations of this function if
288 * @section is not yet parsed completely. This will for example
289 * happen in the GtkCssProvider::parsing-error signal.
290 * The end position and line may be identical to the start
291 * position and line for sections which failed to parse anything
294 * Returns: the offset in bytes from the start of the line.
299 gtk_css_section_get_end_position (const GtkCssSection *section)
301 g_return_val_if_fail (section != NULL, 0);
304 return _gtk_css_parser_get_position (section->parser);
306 return section->end_position;