]> Pileus Git - ~andy/gtk/blob - gtk/gtkcsssection.c
Fix malformed doc comments
[~andy/gtk] / gtk / gtkcsssection.c
1 /* GTK - The GIMP Toolkit
2  * Copyright (C) 2011 Red Hat, Inc.
3  *
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.
8  *
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.
13  *
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/>.
16  */
17
18 #include "config.h"
19
20 #include "gtkcsssectionprivate.h"
21
22 #include "gtkcssparserprivate.h"
23
24 /**
25  * GtkCssSection:
26  *
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
29  * containing region.
30  *
31  * Since: 3.2
32  */
33
34 struct _GtkCssSection
35 {
36   volatile gint       ref_count;
37   GtkCssSectionType   section_type;
38   GtkCssSection      *parent;
39   GFile              *file;
40   guint               start_line;
41   guint               start_position;
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 */
45 };
46
47 G_DEFINE_BOXED_TYPE (GtkCssSection, gtk_css_section, gtk_css_section_ref, gtk_css_section_unref)
48
49 GtkCssSection *
50 _gtk_css_section_new (GtkCssSection     *parent,
51                       GtkCssSectionType  type,
52                       GtkCssParser      *parser,
53                       GFile             *file)
54 {
55   GtkCssSection *section;
56
57   g_return_val_if_fail (parser != NULL, NULL);
58   g_return_val_if_fail (file == NULL || G_IS_FILE (file), NULL);
59
60   section = g_slice_new0 (GtkCssSection);
61
62   section->ref_count = 1;
63   section->section_type = type;
64   if (parent)
65     section->parent = gtk_css_section_ref (parent);
66   if (file)
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;
71
72   return section;
73 }
74
75 GtkCssSection *
76 _gtk_css_section_new_for_file (GtkCssSectionType  type,
77                                GFile             *file)
78 {
79   GtkCssSection *section;
80
81   g_return_val_if_fail (G_IS_FILE (file), NULL);
82
83   section = g_slice_new0 (GtkCssSection);
84
85   section->ref_count = 1;
86   section->section_type = type;
87   section->file = g_object_ref (file);
88
89   return section;
90 }
91
92 void
93 _gtk_css_section_end (GtkCssSection *section)
94 {
95   g_return_if_fail (section != NULL);
96   g_return_if_fail (section->parser != NULL);
97
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;
101 }
102
103 /**
104  * gtk_css_section_ref:
105  * @section: a #GtkCssSection
106  *
107  * Increments the reference count on @section.
108  *
109  * Returns: @section itself.
110  *
111  * Since: 3.2
112  **/
113 GtkCssSection *
114 gtk_css_section_ref (GtkCssSection *section)
115 {
116   g_return_val_if_fail (section != NULL, NULL);
117
118   g_atomic_int_add (&section->ref_count, 1);
119
120   return section;
121 }
122
123 /**
124  * gtk_css_section_unref:
125  * @section: a #GtkCssSection
126  *
127  * Decrements the reference count on @section, freeing the
128  * structure if the reference count reaches 0.
129  *
130  * Since: 3.2
131  **/
132 void
133 gtk_css_section_unref (GtkCssSection *section)
134 {
135   g_return_if_fail (section != NULL);
136
137   if (!g_atomic_int_dec_and_test (&section->ref_count))
138     return;
139
140   if (section->parent)
141     gtk_css_section_unref (section->parent);
142   if (section->file)
143     g_object_unref (section->file);
144
145   g_slice_free (GtkCssSection, section);
146 }
147
148 /**
149  * gtk_css_section_get_section_type:
150  * @section: the section
151  *
152  * Gets the type of information that @section describes.
153  *
154  * Returns: the type of @section
155  *
156  * Since: 3.2
157  **/
158 GtkCssSectionType
159 gtk_css_section_get_section_type (const GtkCssSection *section)
160 {
161   g_return_val_if_fail (section != NULL, GTK_CSS_SECTION_DOCUMENT);
162
163   return section->section_type;
164 }
165
166 /**
167  * gtk_css_section_get_parent:
168  * @section: the section
169  *
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
176  * a different file.
177  *
178  * Returns: the parent section or %NULL if none
179  *
180  * Since: 3.2
181  **/
182 GtkCssSection *
183 gtk_css_section_get_parent (const GtkCssSection *section)
184 {
185   g_return_val_if_fail (section != NULL, NULL);
186
187   return section->parent;
188 }
189
190 /**
191  * gtk_css_section_get_file:
192  * @section: the section
193  *
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.
197  *
198  * Returns: (transfer none): the #GFile that @section was parsed from
199  *     or %NULL if @section was parsed from other data
200  *
201  * Since: 3.2
202  **/
203 GFile *
204 gtk_css_section_get_file (const GtkCssSection *section)
205 {
206   g_return_val_if_fail (section != NULL, NULL);
207
208   return section->file;
209 }
210
211 /**
212  * gtk_css_section_get_start_line:
213  * @section: the section
214  *
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
217  * will return 0.
218  *
219  * Returns: the line number
220  *
221  * Since: 3.2
222  **/
223 guint
224 gtk_css_section_get_start_line (const GtkCssSection *section)
225 {
226   g_return_val_if_fail (section != NULL, 0);
227
228   return section->start_line;
229 }
230
231 /**
232  * gtk_css_section_get_start_position:
233  * @section: the section
234  *
235  * Returns the offset in bytes from the start of the current line
236  * returned via gtk_css_section_get_start_line().
237  *
238  * Returns: the offset in bytes from the start of the line.
239  *
240  * Since: 3.2
241  **/
242 guint
243 gtk_css_section_get_start_position (const GtkCssSection *section)
244 {
245   g_return_val_if_fail (section != NULL, 0);
246
247   return section->start_position;
248 }
249
250 /**
251  * gtk_css_section_get_end_line:
252  * @section: the section
253  *
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
256  * will return 0.
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
262  * successfully.
263  *
264  * Returns: the line number
265  *
266  * Since: 3.2
267  **/
268 guint
269 gtk_css_section_get_end_line (const GtkCssSection *section)
270 {
271   g_return_val_if_fail (section != NULL, 0);
272
273   if (section->parser)
274     return _gtk_css_parser_get_line (section->parser);
275   else
276     return section->end_line;
277 }
278
279 /**
280  * gtk_css_section_get_end_position:
281  * @section: the section
282  *
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
290  * successfully.
291  *
292  * Returns: the offset in bytes from the start of the line.
293  *
294  * Since: 3.2
295  **/
296 guint
297 gtk_css_section_get_end_position (const GtkCssSection *section)
298 {
299   g_return_val_if_fail (section != NULL, 0);
300
301   if (section->parser)
302     return _gtk_css_parser_get_position (section->parser);
303   else
304     return section->end_position;
305 }
306