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