1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8; coding: utf-8 -*- */
2 /* gtksourcestylescheme.c
3  * This file is part of GtkSourceView
4  *
5  * Copyright (C) 2003 - Paolo Maggi <paolo.maggi@polito.it>
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with this library; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
20  */
21 
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
25 
26 #include "gtksourcestylescheme.h"
27 #include "gtksourcestyleschememanager.h"
28 #include "gtksourcestyle.h"
29 #include "gtksourcestyle-private.h"
30 #include "gtksourceview.h"
31 #include "gtksourcelanguage-private.h"
32 #include "gtksourceview-i18n.h"
33 #include <libxml/parser.h>
34 #include <string.h>
35 
36 /**
37  * SECTION:stylescheme
38  * @Short_description: Controls the appearance of GtkSourceView
39  * @Title: GtkSourceStyleScheme
40  * @See_also: #GtkSourceStyle, #GtkSourceStyleSchemeManager
41  *
42  * #GtkSourceStyleScheme contains all the text styles to be used in
43  * #GtkSourceView and #GtkSourceBuffer. For instance, it contains text styles
44  * for syntax highlighting, it may contain foreground and background color for
45  * non-highlighted text, color for the line numbers, current line highlighting,
46  * bracket matching, etc.
47  *
48  * Style schemes are stored in XML files. The format of a scheme file is
49  * documented in the [style scheme reference][style-reference].
50  *
51  * The two style schemes with IDs "classic" and "tango" follow more closely the
52  * GTK+ theme (for example for the background color).
53  */
54 
55 #define STYLE_TEXT			"text"
56 #define STYLE_SELECTED			"selection"
57 #define STYLE_SELECTED_UNFOCUSED	"selection-unfocused"
58 #define STYLE_BRACKET_MATCH		"bracket-match"
59 #define STYLE_BRACKET_MISMATCH		"bracket-mismatch"
60 #define STYLE_CURSOR			"cursor"
61 #define STYLE_SECONDARY_CURSOR		"secondary-cursor"
62 #define STYLE_CURRENT_LINE		"current-line"
63 #define STYLE_LINE_NUMBERS		"line-numbers"
64 #define STYLE_CURRENT_LINE_NUMBER	"current-line-number"
65 #define STYLE_RIGHT_MARGIN		"right-margin"
66 #define STYLE_DRAW_SPACES		"draw-spaces"
67 #define STYLE_BACKGROUND_PATTERN	"background-pattern"
68 
69 #define STYLE_SCHEME_VERSION		"1.0"
70 
71 #define DEFAULT_STYLE_SCHEME		"classic"
72 
73 enum
74 {
75 	PROP_0,
76 	PROP_ID,
77 	PROP_NAME,
78 	PROP_DESCRIPTION,
79 	PROP_FILENAME
80 };
81 
82 struct _GtkSourceStyleSchemePrivate
83 {
84 	gchar *id;
85 	gchar *name;
86 	GPtrArray *authors;
87 	gchar *description;
88 	gchar *filename;
89 	GtkSourceStyleScheme *parent;
90 	gchar *parent_id;
91 	GHashTable *defined_styles;
92 	GHashTable *style_cache;
93 	GHashTable *named_colors;
94 
95 	GtkCssProvider *css_provider;
96 	GtkCssProvider *css_provider_cursors;
97 };
98 
G_DEFINE_TYPE_WITH_PRIVATE(GtkSourceStyleScheme,gtk_source_style_scheme,G_TYPE_OBJECT)99 G_DEFINE_TYPE_WITH_PRIVATE (GtkSourceStyleScheme, gtk_source_style_scheme, G_TYPE_OBJECT)
100 
101 static void
102 gtk_source_style_scheme_dispose (GObject *object)
103 {
104 	GtkSourceStyleScheme *scheme = GTK_SOURCE_STYLE_SCHEME (object);
105 
106 	if (scheme->priv->named_colors != NULL)
107 	{
108 		g_hash_table_unref (scheme->priv->named_colors);
109 		scheme->priv->named_colors = NULL;
110 	}
111 
112 	if (scheme->priv->style_cache != NULL)
113 	{
114 		g_hash_table_unref (scheme->priv->style_cache);
115 		scheme->priv->style_cache = NULL;
116 	}
117 
118 	if (scheme->priv->defined_styles != NULL)
119 	{
120 		g_hash_table_unref (scheme->priv->defined_styles);
121 		scheme->priv->defined_styles = NULL;
122 	}
123 
124 	g_clear_object (&scheme->priv->parent);
125 	g_clear_object (&scheme->priv->css_provider);
126 	g_clear_object (&scheme->priv->css_provider_cursors);
127 
128 	G_OBJECT_CLASS (gtk_source_style_scheme_parent_class)->dispose (object);
129 }
130 
131 static void
gtk_source_style_scheme_finalize(GObject * object)132 gtk_source_style_scheme_finalize (GObject *object)
133 {
134 	GtkSourceStyleScheme *scheme = GTK_SOURCE_STYLE_SCHEME (object);
135 
136 	if (scheme->priv->authors != NULL)
137 	{
138 		g_ptr_array_foreach (scheme->priv->authors, (GFunc)g_free, NULL);
139 		g_ptr_array_free (scheme->priv->authors, TRUE);
140 	}
141 
142 	g_free (scheme->priv->filename);
143 	g_free (scheme->priv->description);
144 	g_free (scheme->priv->id);
145 	g_free (scheme->priv->name);
146 	g_free (scheme->priv->parent_id);
147 
148 	G_OBJECT_CLASS (gtk_source_style_scheme_parent_class)->finalize (object);
149 }
150 
151 static void
gtk_source_style_scheme_set_property(GObject * object,guint prop_id,const GValue * value,GParamSpec * pspec)152 gtk_source_style_scheme_set_property (GObject 	   *object,
153 				      guint         prop_id,
154 				      const GValue *value,
155 				      GParamSpec   *pspec)
156 {
157 	GtkSourceStyleScheme *scheme = GTK_SOURCE_STYLE_SCHEME (object);
158 
159 	switch (prop_id)
160 	{
161 		case PROP_ID:
162 			g_free (scheme->priv->id);
163 			scheme->priv->id = g_value_dup_string (value);
164 			break;
165 
166 		default:
167 			G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
168 			break;
169 	}
170 }
171 
172 static void
gtk_source_style_scheme_get_property(GObject * object,guint prop_id,GValue * value,GParamSpec * pspec)173 gtk_source_style_scheme_get_property (GObject 	 *object,
174 				      guint 	  prop_id,
175 				      GValue 	 *value,
176 				      GParamSpec *pspec)
177 {
178 	GtkSourceStyleScheme *scheme = GTK_SOURCE_STYLE_SCHEME (object);
179 
180 	switch (prop_id)
181 	{
182 		case PROP_ID:
183 			g_value_set_string (value, scheme->priv->id);
184 			break;
185 
186 		case PROP_NAME:
187 			g_value_set_string (value, scheme->priv->name);
188 			break;
189 
190 		case PROP_DESCRIPTION:
191 			g_value_set_string (value, scheme->priv->description);
192 			break;
193 
194 		case PROP_FILENAME:
195 			g_value_set_string (value, scheme->priv->filename);
196 			break;
197 
198 		default:
199 			G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
200 			break;
201 	}
202 }
203 
204 static void
gtk_source_style_scheme_class_init(GtkSourceStyleSchemeClass * klass)205 gtk_source_style_scheme_class_init (GtkSourceStyleSchemeClass *klass)
206 {
207 	GObjectClass *object_class = G_OBJECT_CLASS (klass);
208 
209 	object_class->dispose = gtk_source_style_scheme_dispose;
210 	object_class->finalize = gtk_source_style_scheme_finalize;
211 	object_class->set_property = gtk_source_style_scheme_set_property;
212 	object_class->get_property = gtk_source_style_scheme_get_property;
213 
214 	/**
215 	 * GtkSourceStyleScheme:id:
216 	 *
217 	 * Style scheme id, a unique string used to identify the style scheme
218 	 * in #GtkSourceStyleSchemeManager.
219 	 */
220 	g_object_class_install_property (object_class,
221 					 PROP_ID,
222 					 g_param_spec_string ("id",
223 						 	      "Style scheme id",
224 							      "Style scheme id",
225 							      NULL,
226 							      G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
227 
228 	/**
229 	 * GtkSourceStyleScheme:name:
230 	 *
231 	 * Style scheme name, a translatable string to present to the user.
232 	 */
233 	g_object_class_install_property (object_class,
234 					 PROP_NAME,
235 					 g_param_spec_string ("name",
236 						 	      "Style scheme name",
237 							      "Style scheme name",
238 							      NULL,
239 							      G_PARAM_READABLE));
240 
241 	/**
242 	 * GtkSourceStyleScheme:description:
243 	 *
244 	 * Style scheme description, a translatable string to present to the user.
245 	 */
246 	g_object_class_install_property (object_class,
247 					 PROP_DESCRIPTION,
248 					 g_param_spec_string ("description",
249 						 	      "Style scheme description",
250 							      "Style scheme description",
251 							      NULL,
252 							      G_PARAM_READABLE));
253 
254 	/**
255 	 * GtkSourceStyleScheme:filename:
256 	 *
257 	 * Style scheme filename or %NULL.
258 	 */
259 	g_object_class_install_property (object_class,
260 					 PROP_FILENAME,
261 					 g_param_spec_string ("filename",
262 						 	      "Style scheme filename",
263 							      "Style scheme filename",
264 							      NULL,
265 							      G_PARAM_READABLE));
266 }
267 
268 static void
unref_if_not_null(gpointer object)269 unref_if_not_null (gpointer object)
270 {
271 	if (object != NULL)
272 	{
273 		g_object_unref (object);
274 	}
275 }
276 
277 static void
gtk_source_style_scheme_init(GtkSourceStyleScheme * scheme)278 gtk_source_style_scheme_init (GtkSourceStyleScheme *scheme)
279 {
280 	scheme->priv = gtk_source_style_scheme_get_instance_private (scheme);
281 
282 	scheme->priv->defined_styles = g_hash_table_new_full (g_str_hash, g_str_equal,
283 							      g_free, g_object_unref);
284 
285 	scheme->priv->style_cache = g_hash_table_new_full (g_str_hash, g_str_equal,
286 							   g_free, unref_if_not_null);
287 
288 	scheme->priv->named_colors = g_hash_table_new_full (g_str_hash, g_str_equal,
289 							    g_free, g_free);
290 
291 	scheme->priv->css_provider = gtk_css_provider_new ();
292 }
293 
294 /**
295  * gtk_source_style_scheme_get_id:
296  * @scheme: a #GtkSourceStyleScheme.
297  *
298  * Returns: @scheme id.
299  *
300  * Since: 2.0
301  */
302 const gchar *
gtk_source_style_scheme_get_id(GtkSourceStyleScheme * scheme)303 gtk_source_style_scheme_get_id (GtkSourceStyleScheme *scheme)
304 {
305 	g_return_val_if_fail (GTK_SOURCE_IS_STYLE_SCHEME (scheme), NULL);
306 	g_return_val_if_fail (scheme->priv->id != NULL, "");
307 
308 	return scheme->priv->id;
309 }
310 
311 /**
312  * gtk_source_style_scheme_get_name:
313  * @scheme: a #GtkSourceStyleScheme.
314  *
315  * Returns: @scheme name.
316  *
317  * Since: 2.0
318  */
319 const gchar *
gtk_source_style_scheme_get_name(GtkSourceStyleScheme * scheme)320 gtk_source_style_scheme_get_name (GtkSourceStyleScheme *scheme)
321 {
322 	g_return_val_if_fail (GTK_SOURCE_IS_STYLE_SCHEME (scheme), NULL);
323 	g_return_val_if_fail (scheme->priv->name != NULL, "");
324 
325 	return scheme->priv->name;
326 }
327 
328 /**
329  * gtk_source_style_scheme_get_description:
330  * @scheme: a #GtkSourceStyleScheme.
331  *
332  * Returns: (nullable): @scheme description (if defined), or %NULL.
333  *
334  * Since: 2.0
335  */
336 const gchar *
gtk_source_style_scheme_get_description(GtkSourceStyleScheme * scheme)337 gtk_source_style_scheme_get_description (GtkSourceStyleScheme *scheme)
338 {
339 	g_return_val_if_fail (GTK_SOURCE_IS_STYLE_SCHEME (scheme), NULL);
340 
341 	return scheme->priv->description;
342 }
343 
344 /**
345  * gtk_source_style_scheme_get_authors:
346  * @scheme: a #GtkSourceStyleScheme.
347  *
348  * Returns: (nullable) (array zero-terminated=1) (transfer none): a
349  * %NULL-terminated array containing the @scheme authors or %NULL if
350  * no author is specified by the style scheme.
351  *
352  * Since: 2.0
353  */
354 const gchar * const *
gtk_source_style_scheme_get_authors(GtkSourceStyleScheme * scheme)355 gtk_source_style_scheme_get_authors (GtkSourceStyleScheme *scheme)
356 {
357 	g_return_val_if_fail (GTK_SOURCE_IS_STYLE_SCHEME (scheme), NULL);
358 
359 	if (scheme->priv->authors == NULL)
360 	{
361 		return NULL;
362 	}
363 
364 	return (const gchar * const *)scheme->priv->authors->pdata;
365 }
366 
367 /**
368  * gtk_source_style_scheme_get_filename:
369  * @scheme: a #GtkSourceStyleScheme.
370  *
371  * Returns: (nullable): @scheme file name if the scheme was created
372  * parsing a style scheme file or %NULL in the other cases.
373  *
374  * Since: 2.0
375  */
376 const gchar *
gtk_source_style_scheme_get_filename(GtkSourceStyleScheme * scheme)377 gtk_source_style_scheme_get_filename (GtkSourceStyleScheme *scheme)
378 {
379 	g_return_val_if_fail (GTK_SOURCE_IS_STYLE_SCHEME (scheme), NULL);
380 
381 	return scheme->priv->filename;
382 }
383 
384 /*
385  * Try to parse a color string.
386  * If the color can be parsed, return the offset in the string
387  * with the real start of the color (either the string itself, or after
388  * the initial '#' character).
389  */
390 static const gchar *
color_parse(const gchar * color,GdkRGBA * rgba)391 color_parse (const gchar *color,
392              GdkRGBA     *rgba)
393 {
394 	if ((*color == '#') && gdk_rgba_parse (rgba, color + 1))
395 	{
396 		return color + 1;
397 	}
398 
399 	if (gdk_rgba_parse (rgba, color))
400 	{
401 		return color;
402 	}
403 
404 	return NULL;
405 }
406 
407 /*
408  * get_color_by_name:
409  * @scheme: a #GtkSourceStyleScheme.
410  * @name: color name to find.
411  *
412  * Returns: color which corresponds to @name in the @scheme.
413  * Returned value is actual color string suitable for gdk_rgba_parse().
414  * It may be @name or part of @name so copy it or something, if you need
415  * it to stay around.
416  *
417  * Since: 2.0
418  */
419 static const gchar *
get_color_by_name(GtkSourceStyleScheme * scheme,const gchar * name)420 get_color_by_name (GtkSourceStyleScheme *scheme,
421 		   const gchar          *name)
422 {
423 	const char *color = NULL;
424 
425 	g_return_val_if_fail (name != NULL, NULL);
426 
427 	if (name[0] == '#')
428 	{
429 		GdkRGBA dummy;
430 
431 		color = color_parse (name, &dummy);
432 
433 		if (color == NULL)
434 		{
435 			g_warning ("could not parse color '%s'", name);
436 		}
437 	}
438 	else
439 	{
440 		color = g_hash_table_lookup (scheme->priv->named_colors, name);
441 
442 		if (color == NULL && scheme->priv->parent != NULL)
443 		{
444 			color = get_color_by_name (scheme->priv->parent, name);
445 		}
446 
447 		if (color == NULL)
448 		{
449 			g_warning ("no color named '%s'", name);
450 		}
451 	}
452 
453 	return color;
454 }
455 
456 static GtkSourceStyle *
fix_style_colors(GtkSourceStyleScheme * scheme,GtkSourceStyle * real_style)457 fix_style_colors (GtkSourceStyleScheme *scheme,
458 		  GtkSourceStyle       *real_style)
459 {
460 	GtkSourceStyle *style;
461 	guint i;
462 
463 	struct {
464 		guint mask;
465 		guint offset;
466 	} attributes[] = {
467 		{ GTK_SOURCE_STYLE_USE_BACKGROUND, G_STRUCT_OFFSET (GtkSourceStyle, background) },
468 		{ GTK_SOURCE_STYLE_USE_FOREGROUND, G_STRUCT_OFFSET (GtkSourceStyle, foreground) },
469 		{ GTK_SOURCE_STYLE_USE_LINE_BACKGROUND, G_STRUCT_OFFSET (GtkSourceStyle, line_background) },
470 		{ GTK_SOURCE_STYLE_USE_UNDERLINE_COLOR, G_STRUCT_OFFSET (GtkSourceStyle, underline_color) }
471 	};
472 
473 	style = gtk_source_style_copy (real_style);
474 
475 	for (i = 0; i < G_N_ELEMENTS (attributes); i++)
476 	{
477 		if (style->mask & attributes[i].mask)
478 		{
479 			const gchar **attr = G_STRUCT_MEMBER_P (style, attributes[i].offset);
480 			const gchar *color = get_color_by_name (scheme, *attr);
481 
482 			if (color == NULL)
483 			{
484 				/* warning is spit out in get_color_by_name,
485 				 * here we make sure style doesn't have NULL color */
486 				style->mask &= ~attributes[i].mask;
487 			}
488 			else
489 			{
490 				*attr = g_intern_string (color);
491 			}
492 		}
493 	}
494 
495 	return style;
496 }
497 
498 /**
499  * gtk_source_style_scheme_get_style:
500  * @scheme: a #GtkSourceStyleScheme.
501  * @style_id: id of the style to retrieve.
502  *
503  * Returns: (nullable) (transfer none): style which corresponds to @style_id in
504  * the @scheme, or %NULL when no style with this name found.  It is owned by
505  * @scheme and may not be unref'ed.
506  *
507  * Since: 2.0
508  */
509 /*
510  * It's a little weird because we have named colors: styles loaded from
511  * scheme file can have "#red" or "blue", and we want to give out styles
512  * which have nice colors suitable for gdk_color_parse(), so that GtkSourceStyle
513  * foreground and background properties are the same as GtkTextTag's.
514  * Yet we do need to preserve what we got from file in style schemes,
515  * since there may be child schemes which may redefine colors or something,
516  * so we can't translate colors when loading scheme.
517  * So, defined_styles hash has named colors; styles returned with get_style()
518  * have real colors.
519  */
520 GtkSourceStyle *
gtk_source_style_scheme_get_style(GtkSourceStyleScheme * scheme,const gchar * style_id)521 gtk_source_style_scheme_get_style (GtkSourceStyleScheme *scheme,
522 				   const gchar          *style_id)
523 {
524 	GtkSourceStyle *style = NULL;
525 	GtkSourceStyle *real_style;
526 
527 	g_return_val_if_fail (GTK_SOURCE_IS_STYLE_SCHEME (scheme), NULL);
528 	g_return_val_if_fail (style_id != NULL, NULL);
529 
530 	if (g_hash_table_lookup_extended (scheme->priv->style_cache,
531 					  style_id,
532 					  NULL,
533 					  (gpointer)&style))
534 	{
535 		return style;
536 	}
537 
538 	real_style = g_hash_table_lookup (scheme->priv->defined_styles, style_id);
539 
540 	if (real_style == NULL)
541 	{
542 		if (scheme->priv->parent != NULL)
543 		{
544 			style = gtk_source_style_scheme_get_style (scheme->priv->parent,
545 								   style_id);
546 		}
547 		if (style != NULL)
548 		{
549 			g_object_ref (style);
550 		}
551 	}
552 	else
553 	{
554 		style = fix_style_colors (scheme, real_style);
555 	}
556 
557 	g_hash_table_insert (scheme->priv->style_cache,
558 			     g_strdup (style_id),
559 			     style);
560 
561 	return style;
562 }
563 
564 GtkSourceStyle *
_gtk_source_style_scheme_get_matching_brackets_style(GtkSourceStyleScheme * scheme)565 _gtk_source_style_scheme_get_matching_brackets_style (GtkSourceStyleScheme *scheme)
566 {
567 	g_return_val_if_fail (GTK_SOURCE_IS_STYLE_SCHEME (scheme), NULL);
568 
569 	return gtk_source_style_scheme_get_style (scheme, STYLE_BRACKET_MATCH);
570 }
571 
572 GtkSourceStyle *
_gtk_source_style_scheme_get_right_margin_style(GtkSourceStyleScheme * scheme)573 _gtk_source_style_scheme_get_right_margin_style (GtkSourceStyleScheme *scheme)
574 {
575 	g_return_val_if_fail (GTK_SOURCE_IS_STYLE_SCHEME (scheme), NULL);
576 
577 	return gtk_source_style_scheme_get_style (scheme, STYLE_RIGHT_MARGIN);
578 }
579 
580 GtkSourceStyle *
_gtk_source_style_scheme_get_draw_spaces_style(GtkSourceStyleScheme * scheme)581 _gtk_source_style_scheme_get_draw_spaces_style (GtkSourceStyleScheme *scheme)
582 {
583 	g_return_val_if_fail (GTK_SOURCE_IS_STYLE_SCHEME (scheme), NULL);
584 
585 	return gtk_source_style_scheme_get_style (scheme, STYLE_DRAW_SPACES);
586 }
587 
588 static gboolean
get_color(GtkSourceStyle * style,gboolean foreground,GdkRGBA * dest)589 get_color (GtkSourceStyle *style,
590 	   gboolean        foreground,
591 	   GdkRGBA        *dest)
592 {
593 	const gchar *color;
594 	guint mask;
595 
596 	if (style != NULL)
597 	{
598 		if (foreground)
599 		{
600 			color = style->foreground;
601 			mask = GTK_SOURCE_STYLE_USE_FOREGROUND;
602 		}
603 		else
604 		{
605 			color = style->background;
606 			mask = GTK_SOURCE_STYLE_USE_BACKGROUND;
607 		}
608 
609 		if (style->mask & mask)
610 		{
611 			if (color == NULL || !color_parse (color, dest))
612 			{
613 				g_warning ("%s: invalid color '%s'", G_STRLOC,
614 					   color != NULL ? color : "(null)");
615 				return FALSE;
616 			}
617 
618 			return TRUE;
619 		}
620 	}
621 
622 	return FALSE;
623 }
624 
625 /*
626  * Returns TRUE if the style for current-line is set in the scheme
627  */
628 gboolean
_gtk_source_style_scheme_get_current_line_color(GtkSourceStyleScheme * scheme,GdkRGBA * color)629 _gtk_source_style_scheme_get_current_line_color (GtkSourceStyleScheme *scheme,
630 						 GdkRGBA              *color)
631 {
632 	GtkSourceStyle *style;
633 
634 	g_return_val_if_fail (GTK_SOURCE_IS_STYLE_SCHEME (scheme), FALSE);
635 	g_return_val_if_fail (color != NULL, FALSE);
636 
637 	style = gtk_source_style_scheme_get_style (scheme, STYLE_CURRENT_LINE);
638 
639 	return get_color (style, FALSE, color);
640 }
641 
642 /*
643  * Returns TRUE if the style for background-pattern-color is set in the scheme
644  */
645 gboolean
_gtk_source_style_scheme_get_background_pattern_color(GtkSourceStyleScheme * scheme,GdkRGBA * color)646 _gtk_source_style_scheme_get_background_pattern_color (GtkSourceStyleScheme *scheme,
647                                                        GdkRGBA              *color)
648 {
649 	GtkSourceStyle *style;
650 
651 	g_return_val_if_fail (GTK_SOURCE_IS_STYLE_SCHEME (scheme), FALSE);
652 	g_return_val_if_fail (color != NULL, FALSE);
653 
654 	style = gtk_source_style_scheme_get_style (scheme, STYLE_BACKGROUND_PATTERN);
655 
656 	return get_color (style, FALSE, color);
657 }
658 
659 static gchar *
get_cursors_css_style(GtkSourceStyleScheme * scheme,GtkWidget * widget)660 get_cursors_css_style (GtkSourceStyleScheme *scheme,
661 		       GtkWidget            *widget)
662 {
663 	GtkSourceStyle *primary_style;
664 	GtkSourceStyle *secondary_style;
665 	GdkRGBA primary_color = { 0 };
666 	GdkRGBA secondary_color = { 0 };
667 	gboolean primary_color_set;
668 	gboolean secondary_color_set;
669 	gchar *secondary_color_str;
670 	GString *css;
671 
672 	primary_style = gtk_source_style_scheme_get_style (scheme, STYLE_CURSOR);
673 	secondary_style = gtk_source_style_scheme_get_style (scheme, STYLE_SECONDARY_CURSOR);
674 
675 	primary_color_set = get_color (primary_style, TRUE, &primary_color);
676 	secondary_color_set = get_color (secondary_style, TRUE, &secondary_color);
677 
678 	if (!primary_color_set && !secondary_color_set)
679 	{
680 		return NULL;
681 	}
682 
683 	css = g_string_new ("textview text {\n");
684 
685 	if (primary_color_set)
686 	{
687 		gchar *primary_color_str;
688 
689 		primary_color_str = gdk_rgba_to_string (&primary_color);
690 		g_string_append_printf (css,
691 					"\tcaret-color: %s;\n",
692 					primary_color_str);
693 		g_free (primary_color_str);
694 	}
695 
696 	if (!secondary_color_set)
697 	{
698 		GtkStyleContext *context;
699 		GdkRGBA *background_color;
700 
701 		g_assert (primary_color_set);
702 
703 		context = gtk_widget_get_style_context (widget);
704 
705 		gtk_style_context_save (context);
706 		gtk_style_context_set_state (context, GTK_STATE_FLAG_NORMAL);
707 
708 		gtk_style_context_get (context,
709 				       gtk_style_context_get_state (context),
710 				       "background-color", &background_color,
711 				       NULL);
712 
713 		gtk_style_context_restore (context);
714 
715 		/* Blend primary cursor color with background color. */
716 		secondary_color.red = (primary_color.red + background_color->red) * 0.5;
717 		secondary_color.green = (primary_color.green + background_color->green) * 0.5;
718 		secondary_color.blue = (primary_color.blue + background_color->blue) * 0.5;
719 		secondary_color.alpha = (primary_color.alpha + background_color->alpha) * 0.5;
720 
721 		gdk_rgba_free (background_color);
722 	}
723 
724 	secondary_color_str = gdk_rgba_to_string (&secondary_color);
725 	g_string_append_printf (css,
726 				"\t-gtk-secondary-caret-color: %s;\n",
727 				secondary_color_str);
728 	g_free (secondary_color_str);
729 
730 	g_string_append_printf (css, "}\n");
731 
732 	return g_string_free (css, FALSE);
733 }
734 
735 /* The CssProvider for the cursors depends only on @scheme, but it needs a
736  * @widget to shade the background color in case the secondary cursor color
737  * isn't defined. The background color is normally defined by @scheme, or if
738  * it's not defined it is taken from the GTK+ theme. So ideally, if the GTK+
739  * theme changes at runtime, we should regenerate the CssProvider for the
740  * cursors, but it isn't done.
741  */
742 static GtkCssProvider *
get_css_provider_cursors(GtkSourceStyleScheme * scheme,GtkWidget * widget)743 get_css_provider_cursors (GtkSourceStyleScheme *scheme,
744 			  GtkWidget            *widget)
745 {
746 	gchar *css;
747 	GtkCssProvider *provider;
748 	GError *error = NULL;
749 
750 	css = get_cursors_css_style (scheme, widget);
751 
752 	if (css == NULL)
753 	{
754 		return NULL;
755 	}
756 
757 	provider = gtk_css_provider_new ();
758 
759 	gtk_css_provider_load_from_data (provider, css, -1, &error);
760 	g_free (css);
761 
762 	if (error != NULL)
763 	{
764 		g_warning ("Error when loading CSS for cursors: %s", error->message);
765 		g_clear_error (&error);
766 		g_clear_object (&provider);
767 	}
768 
769 	return provider;
770 }
771 
772 /**
773  * _gtk_source_style_scheme_apply:
774  * @scheme:: a #GtkSourceStyleScheme.
775  * @view: a #GtkSourceView to apply styles to.
776  *
777  * Sets style colors from @scheme to the @view.
778  *
779  * Since: 2.0
780  */
781 void
_gtk_source_style_scheme_apply(GtkSourceStyleScheme * scheme,GtkSourceView * view)782 _gtk_source_style_scheme_apply (GtkSourceStyleScheme *scheme,
783 				GtkSourceView        *view)
784 {
785 	GtkStyleContext *context;
786 
787 	g_return_if_fail (GTK_SOURCE_IS_STYLE_SCHEME (scheme));
788 	g_return_if_fail (GTK_SOURCE_IS_VIEW (view));
789 
790 	context = gtk_widget_get_style_context (GTK_WIDGET (view));
791 	gtk_style_context_add_provider (context,
792 	                                GTK_STYLE_PROVIDER (scheme->priv->css_provider),
793 	                                GTK_SOURCE_STYLE_PROVIDER_PRIORITY);
794 
795 	G_GNUC_BEGIN_IGNORE_DEPRECATIONS;
796 	/* See https://bugzilla.gnome.org/show_bug.cgi?id=708583 */
797 	gtk_style_context_invalidate (context);
798 	G_GNUC_END_IGNORE_DEPRECATIONS;
799 
800 	/* The CssProvider for the cursors needs that the first provider is
801 	 * applied, to get the background color.
802 	 */
803 	if (scheme->priv->css_provider_cursors == NULL)
804 	{
805 		scheme->priv->css_provider_cursors = get_css_provider_cursors (scheme,
806 									       GTK_WIDGET (view));
807 	}
808 
809 	if (scheme->priv->css_provider_cursors != NULL)
810 	{
811 		gtk_style_context_add_provider (context,
812 						GTK_STYLE_PROVIDER (scheme->priv->css_provider_cursors),
813 						GTK_SOURCE_STYLE_PROVIDER_PRIORITY);
814 
815 		G_GNUC_BEGIN_IGNORE_DEPRECATIONS;
816 		gtk_style_context_invalidate (context);
817 		G_GNUC_END_IGNORE_DEPRECATIONS;
818 	}
819 }
820 
821 /**
822  * _gtk_source_style_scheme_unapply:
823  * @scheme: (nullable): a #GtkSourceStyleScheme or %NULL.
824  * @view: a #GtkSourceView to unapply styles to.
825  *
826  * Removes the styles from @scheme in the @view.
827  *
828  * Since: 3.0
829  */
830 void
_gtk_source_style_scheme_unapply(GtkSourceStyleScheme * scheme,GtkSourceView * view)831 _gtk_source_style_scheme_unapply (GtkSourceStyleScheme *scheme,
832 				  GtkSourceView        *view)
833 {
834 	GtkStyleContext *context;
835 
836 	g_return_if_fail (GTK_SOURCE_IS_STYLE_SCHEME (scheme));
837 	g_return_if_fail (GTK_SOURCE_IS_VIEW (view));
838 
839 	context = gtk_widget_get_style_context (GTK_WIDGET (view));
840 	gtk_style_context_remove_provider (context,
841 	                                   GTK_STYLE_PROVIDER (scheme->priv->css_provider));
842 
843 	if (scheme->priv->css_provider_cursors != NULL)
844 	{
845 		gtk_style_context_remove_provider (context,
846 						   GTK_STYLE_PROVIDER (scheme->priv->css_provider_cursors));
847 	}
848 
849 	G_GNUC_BEGIN_IGNORE_DEPRECATIONS;
850 	/* See https://bugzilla.gnome.org/show_bug.cgi?id=708583 */
851 	gtk_style_context_invalidate (context);
852 	G_GNUC_END_IGNORE_DEPRECATIONS;
853 }
854 
855 /* --- PARSER ---------------------------------------------------------------- */
856 
857 #define ERROR_QUARK (g_quark_from_static_string ("gtk-source-style-scheme-parser-error"))
858 
859 static void
get_css_color_style(GtkSourceStyle * style,gchar ** bg,gchar ** text)860 get_css_color_style (GtkSourceStyle *style,
861                      gchar         **bg,
862                      gchar         **text)
863 {
864 	GdkRGBA color;
865 
866 	if (get_color (style, FALSE, &color))
867 	{
868 		gchar *bg_color;
869 		bg_color = gdk_rgba_to_string (&color);
870 		*bg = g_strdup_printf ("background-color: %s;\n", bg_color);
871 		g_free (bg_color);
872 	}
873 	else
874 	{
875 		*bg = NULL;
876 	}
877 
878 	if (get_color (style, TRUE, &color))
879 	{
880 		gchar *text_color;
881 		text_color = gdk_rgba_to_string (&color);
882 		*text = g_strdup_printf ("color: %s;\n", text_color);
883 		g_free (text_color);
884 	}
885 	else
886 	{
887 		*text = NULL;
888 	}
889 }
890 
891 static void
append_css_style(GString * string,GtkSourceStyle * style,const gchar * selector)892 append_css_style (GString        *string,
893                   GtkSourceStyle *style,
894                   const gchar    *selector)
895 {
896 	gchar *bg, *text;
897 	const gchar css_style[] =
898 		"%s {\n"
899 		"	%s"
900 		"	%s"
901 		"}\n";
902 
903 	get_css_color_style (style, &bg, &text);
904 	if (bg || text)
905 	{
906 		g_string_append_printf (string, css_style, selector,
907 		                        bg != NULL ? bg : "",
908 		                        text != NULL ? text : "");
909 
910 		g_free (bg);
911 		g_free (text);
912 	}
913 }
914 
915 static void
generate_css_style(GtkSourceStyleScheme * scheme)916 generate_css_style (GtkSourceStyleScheme *scheme)
917 {
918 	GString *final_style;
919 	GtkSourceStyle *style, *style2;
920 
921 	final_style = g_string_new ("");
922 
923 	style = gtk_source_style_scheme_get_style (scheme, STYLE_TEXT);
924 	append_css_style (final_style, style, "textview text");
925 
926 	style = gtk_source_style_scheme_get_style (scheme, STYLE_SELECTED);
927 	append_css_style (final_style, style, "textview:focus text selection");
928 
929 	style2 = gtk_source_style_scheme_get_style (scheme, STYLE_SELECTED_UNFOCUSED);
930 	append_css_style (final_style,
931 			  style2 != NULL ? style2 : style,
932 			  "textview text selection");
933 
934 	/* For now we use "line numbers" colors for all the gutters */
935 	style = gtk_source_style_scheme_get_style (scheme, STYLE_LINE_NUMBERS);
936 	if (style != NULL)
937 	{
938 		append_css_style (final_style, style, "textview border");
939 
940 		/* Needed for GtkSourceGutter. In the ::draw callback,
941 		 * gtk_style_context_add_class() is called to add e.g. the
942 		 * "left" class. Because as of GTK+ 3.20 we cannot do the same
943 		 * to add the "border" subnode.
944 		 */
945 		append_css_style (final_style, style, "textview .left");
946 		append_css_style (final_style, style, "textview .right");
947 		append_css_style (final_style, style, "textview .top");
948 		append_css_style (final_style, style, "textview .bottom");
949 
950 		/* For the corners if the top or bottom gutter is also
951 		 * displayed.
952 		 * FIXME: this shouldn't be necessary, GTK+ should apply the
953 		 * border style to the corners too, see:
954 		 * https://bugzilla.gnome.org/show_bug.cgi?id=764239
955 		 */
956 		append_css_style (final_style, style, "textview");
957 	}
958 
959 	style = gtk_source_style_scheme_get_style (scheme, STYLE_CURRENT_LINE_NUMBER);
960 	if (style != NULL)
961 	{
962 		append_css_style (final_style, style, "textview .current-line-number");
963 	}
964 
965 	if (*final_style->str != '\0')
966 	{
967 		GError *error = NULL;
968 
969 		gtk_css_provider_load_from_data (scheme->priv->css_provider,
970 						 final_style->str,
971 						 final_style->len,
972 						 &error);
973 
974 		if (error != NULL)
975 		{
976 			g_warning ("%s", error->message);
977 			g_clear_error (&error);
978 		}
979 	}
980 
981 	g_string_free (final_style, TRUE);
982 }
983 
984 static gboolean
parse_bool(char * value)985 parse_bool (char *value)
986 {
987 	return (g_ascii_strcasecmp (value, "true") == 0 ||
988 	        g_ascii_strcasecmp (value, "yes") == 0 ||
989 	        g_ascii_strcasecmp (value, "1") == 0);
990 }
991 
992 static void
get_bool(xmlNode * node,const char * propname,guint * mask,guint mask_value,gboolean * value)993 get_bool (xmlNode    *node,
994 	  const char *propname,
995 	  guint      *mask,
996 	  guint       mask_value,
997 	  gboolean   *value)
998 {
999 	xmlChar *tmp = xmlGetProp (node, BAD_CAST propname);
1000 
1001 	if (tmp != NULL)
1002 	{
1003 		*mask |= mask_value;
1004 		*value = parse_bool ((char*) tmp);
1005 	}
1006 
1007 	xmlFree (tmp);
1008 }
1009 
1010 static gboolean
parse_style(GtkSourceStyleScheme * scheme,xmlNode * node,gchar ** style_name_p,GtkSourceStyle ** style_p,GError ** error)1011 parse_style (GtkSourceStyleScheme *scheme,
1012 	     xmlNode              *node,
1013 	     gchar               **style_name_p,
1014 	     GtkSourceStyle      **style_p,
1015 	     GError              **error)
1016 {
1017 	GtkSourceStyle *use_style = NULL;
1018 	GtkSourceStyle *result = NULL;
1019 	xmlChar *fg = NULL;
1020 	xmlChar *bg = NULL;
1021 	xmlChar *line_bg = NULL;
1022 	gchar *style_name = NULL;
1023 	guint mask = 0;
1024 	gboolean bold = FALSE;
1025 	gboolean italic = FALSE;
1026 	gboolean strikethrough = FALSE;
1027 	xmlChar *underline = NULL;
1028 	xmlChar *underline_color = NULL;
1029 	xmlChar *scale = NULL;
1030 	xmlChar *tmp;
1031 
1032 	tmp = xmlGetProp (node, BAD_CAST "name");
1033 	if (tmp == NULL)
1034 	{
1035 		g_set_error (error, ERROR_QUARK, 0, "name attribute missing");
1036 		return FALSE;
1037 	}
1038 	style_name = g_strdup ((char*) tmp);
1039 	xmlFree (tmp);
1040 
1041 	tmp = xmlGetProp (node, BAD_CAST "use-style");
1042 	if (tmp != NULL)
1043 	{
1044 		use_style = gtk_source_style_scheme_get_style (scheme, (char*) tmp);
1045 
1046 		if (use_style == NULL)
1047 		{
1048 			g_set_error (error, ERROR_QUARK, 0,
1049 				     "in style '%s': unknown style '%s'",
1050 				     style_name, tmp);
1051 			g_free (style_name);
1052 			return FALSE;
1053 		}
1054 
1055 		g_object_ref (use_style);
1056 	}
1057 	xmlFree (tmp);
1058 
1059 	fg = xmlGetProp (node, BAD_CAST "foreground");
1060 	bg = xmlGetProp (node, BAD_CAST "background");
1061 	line_bg = xmlGetProp (node, BAD_CAST "line-background");
1062 	get_bool (node, "italic", &mask, GTK_SOURCE_STYLE_USE_ITALIC, &italic);
1063 	get_bool (node, "bold", &mask, GTK_SOURCE_STYLE_USE_BOLD, &bold);
1064 	get_bool (node, "strikethrough", &mask, GTK_SOURCE_STYLE_USE_STRIKETHROUGH, &strikethrough);
1065 	underline = xmlGetProp (node, BAD_CAST "underline");
1066 	underline_color = xmlGetProp (node, BAD_CAST "underline-color");
1067 	scale = xmlGetProp (node, BAD_CAST "scale");
1068 
1069 	if (use_style)
1070 	{
1071 		if (fg != NULL ||
1072 		    bg != NULL ||
1073 		    line_bg != NULL ||
1074 		    mask != 0 ||
1075 		    underline != NULL ||
1076 		    underline_color != NULL ||
1077 		    scale != NULL)
1078 		{
1079 			g_set_error (error, ERROR_QUARK, 0,
1080 				     "in style '%s': style attributes used along with use-style",
1081 				     style_name);
1082 			g_object_unref (use_style);
1083 			g_free (style_name);
1084 			xmlFree (fg);
1085 			xmlFree (bg);
1086 			xmlFree (line_bg);
1087 			xmlFree (underline);
1088 			xmlFree (underline_color);
1089 			xmlFree (scale);
1090 			return FALSE;
1091 		}
1092 
1093 		result = use_style;
1094 		use_style = NULL;
1095 	}
1096 	else
1097 	{
1098 		result = g_object_new (GTK_SOURCE_TYPE_STYLE, NULL);
1099 
1100 		result->mask = mask;
1101 		result->bold = bold;
1102 		result->italic = italic;
1103 		result->strikethrough = strikethrough;
1104 
1105 		if (fg != NULL)
1106 		{
1107 			result->foreground = g_intern_string ((char*) fg);
1108 			result->mask |= GTK_SOURCE_STYLE_USE_FOREGROUND;
1109 		}
1110 
1111 		if (bg != NULL)
1112 		{
1113 			result->background = g_intern_string ((char*) bg);
1114 			result->mask |= GTK_SOURCE_STYLE_USE_BACKGROUND;
1115 		}
1116 
1117 		if (line_bg != NULL)
1118 		{
1119 			result->line_background = g_intern_string ((char*) line_bg);
1120 			result->mask |= GTK_SOURCE_STYLE_USE_LINE_BACKGROUND;
1121 		}
1122 
1123 		if (underline != NULL)
1124 		{
1125 			/* Up until 3.16 underline was a "bool", so for backward
1126 			 * compat we accept underline="true" and map it to "single"
1127 			 */
1128 			if (parse_bool ((char *) underline))
1129 			{
1130 				result->underline = PANGO_UNDERLINE_SINGLE;
1131 				result->mask |= GTK_SOURCE_STYLE_USE_UNDERLINE;
1132 			}
1133 			else
1134 			{
1135 				GEnumClass *enum_class;
1136 				GEnumValue *enum_value;
1137 				gchar *underline_lowercase;
1138 
1139 				enum_class = G_ENUM_CLASS (g_type_class_ref (PANGO_TYPE_UNDERLINE));
1140 
1141 				underline_lowercase = g_ascii_strdown ((char*) underline, -1);
1142 				enum_value = g_enum_get_value_by_nick (enum_class, underline_lowercase);
1143 				g_free (underline_lowercase);
1144 
1145 				if (enum_value != NULL)
1146 				{
1147 					result->underline = enum_value->value;
1148 					result->mask |= GTK_SOURCE_STYLE_USE_UNDERLINE;
1149 				}
1150 
1151 				g_type_class_unref (enum_class);
1152 			}
1153 		}
1154 
1155 		if (underline_color != NULL)
1156 		{
1157 			result->underline_color = g_intern_string ((char*) underline_color);
1158 			result->mask |= GTK_SOURCE_STYLE_USE_UNDERLINE_COLOR;
1159 		}
1160 
1161 		if (scale != NULL)
1162 		{
1163 			result->scale = g_intern_string ((char*) scale);
1164 			result->mask |= GTK_SOURCE_STYLE_USE_SCALE;
1165 		}
1166 	}
1167 
1168 	*style_p = result;
1169 	*style_name_p = style_name;
1170 
1171 	xmlFree (fg);
1172 	xmlFree (bg);
1173 	xmlFree (line_bg);
1174 	xmlFree (underline);
1175 	xmlFree (underline_color);
1176 	xmlFree (scale);
1177 
1178 	return TRUE;
1179 }
1180 
1181 static gboolean
parse_color(GtkSourceStyleScheme * scheme,xmlNode * node,GError ** error)1182 parse_color (GtkSourceStyleScheme *scheme,
1183 	     xmlNode              *node,
1184 	     GError              **error)
1185 {
1186 	xmlChar *name, *value;
1187 	gboolean result = FALSE;
1188 
1189 	name = xmlGetProp (node, BAD_CAST "name");
1190 	value = xmlGetProp (node, BAD_CAST "value");
1191 
1192 	if (name == NULL || name[0] == 0)
1193 		g_set_error (error, ERROR_QUARK, 0, "name attribute missing in 'color' tag");
1194 	else if (value == NULL)
1195 		g_set_error (error, ERROR_QUARK, 0, "value attribute missing in 'color' tag");
1196 	else if (value[0] != '#' || value[1] == 0)
1197 		g_set_error (error, ERROR_QUARK, 0, "value in 'color' tag is not of the form '#RGB' or '#name'");
1198 	else if (g_hash_table_lookup (scheme->priv->named_colors, name) != NULL)
1199 		g_set_error (error, ERROR_QUARK, 0, "duplicated color '%s'", name);
1200 	else
1201 		result = TRUE;
1202 
1203 	if (result)
1204 		g_hash_table_insert (scheme->priv->named_colors,
1205 				     g_strdup ((char *) name),
1206 				     g_strdup ((char *) value));
1207 
1208 	xmlFree (value);
1209 	xmlFree (name);
1210 
1211 	return result;
1212 }
1213 
1214 static gboolean
parse_style_scheme_child(GtkSourceStyleScheme * scheme,xmlNode * node,GError ** error)1215 parse_style_scheme_child (GtkSourceStyleScheme *scheme,
1216 			  xmlNode              *node,
1217 			  GError              **error)
1218 {
1219 	if (strcmp ((char*) node->name, "style") == 0)
1220 	{
1221 		GtkSourceStyle *style;
1222 		gchar *style_name;
1223 
1224 		if (!parse_style (scheme, node, &style_name, &style, error))
1225 			return FALSE;
1226 
1227 		g_hash_table_insert (scheme->priv->defined_styles, style_name, style);
1228 	}
1229 	else if (strcmp ((char*) node->name, "color") == 0)
1230 	{
1231 		if (!parse_color (scheme, node, error))
1232 			return FALSE;
1233 	}
1234 	else if (strcmp ((char*) node->name, "author") == 0)
1235 	{
1236 		xmlChar *tmp = xmlNodeGetContent (node);
1237 		if (scheme->priv->authors == NULL)
1238 			scheme->priv->authors = g_ptr_array_new ();
1239 
1240 		g_ptr_array_add (scheme->priv->authors, g_strdup ((char*) tmp));
1241 
1242 		xmlFree (tmp);
1243 	}
1244 	else if (strcmp ((char*) node->name, "description") == 0)
1245 	{
1246 		xmlChar *tmp = xmlNodeGetContent (node);
1247 		scheme->priv->description = g_strdup ((char*) tmp);
1248 		xmlFree (tmp);
1249 	}
1250 	else if (strcmp ((char*) node->name, "_description") == 0)
1251 	{
1252 		xmlChar *tmp = xmlNodeGetContent (node);
1253 		scheme->priv->description = g_strdup (_((char*) tmp));
1254 		xmlFree (tmp);
1255 	}
1256 	else
1257 	{
1258 		g_set_error (error, ERROR_QUARK, 0, "unknown node '%s'", node->name);
1259 		return FALSE;
1260 	}
1261 
1262 	return TRUE;
1263 }
1264 
1265 static void
parse_style_scheme_element(GtkSourceStyleScheme * scheme,xmlNode * scheme_node,GError ** error)1266 parse_style_scheme_element (GtkSourceStyleScheme *scheme,
1267 			    xmlNode              *scheme_node,
1268 			    GError              **error)
1269 {
1270 	xmlChar *tmp;
1271 	xmlNode *node;
1272 
1273 	if (strcmp ((char*) scheme_node->name, "style-scheme") != 0)
1274 	{
1275 		g_set_error (error, ERROR_QUARK, 0,
1276 			     "unexpected element '%s'",
1277 			     (char*) scheme_node->name);
1278 		return;
1279 	}
1280 
1281 	tmp = xmlGetProp (scheme_node, BAD_CAST "version");
1282 	if (tmp == NULL)
1283 	{
1284 		g_set_error (error, ERROR_QUARK, 0, "missing 'version' attribute");
1285 		return;
1286 	}
1287 	if (strcmp ((char*) tmp, STYLE_SCHEME_VERSION) != 0)
1288 	{
1289 		g_set_error (error, ERROR_QUARK, 0, "unsupported version '%s'", (char*) tmp);
1290 		xmlFree (tmp);
1291 		return;
1292 	}
1293 	xmlFree (tmp);
1294 
1295 	tmp = xmlGetProp (scheme_node, BAD_CAST "id");
1296 	if (tmp == NULL)
1297 	{
1298 		g_set_error (error, ERROR_QUARK, 0, "missing 'id' attribute");
1299 		return;
1300 	}
1301 	scheme->priv->id = g_strdup ((char*) tmp);
1302 	xmlFree (tmp);
1303 
1304 	tmp = xmlGetProp (scheme_node, BAD_CAST "_name");
1305 	if (tmp != NULL)
1306 		scheme->priv->name = g_strdup (_((char*) tmp));
1307 	else if ((tmp = xmlGetProp (scheme_node, BAD_CAST "name")) != NULL)
1308 		scheme->priv->name = g_strdup ((char*) tmp);
1309 	else
1310 	{
1311 		g_set_error (error, ERROR_QUARK, 0, "missing 'name' attribute");
1312 		return;
1313 	}
1314 	xmlFree (tmp);
1315 
1316 	tmp = xmlGetProp (scheme_node, BAD_CAST "parent-scheme");
1317 	if (tmp != NULL)
1318 		scheme->priv->parent_id = g_strdup ((char*) tmp);
1319 	xmlFree (tmp);
1320 
1321 	for (node = scheme_node->children; node != NULL; node = node->next)
1322 		if (node->type == XML_ELEMENT_NODE)
1323 			if (!parse_style_scheme_child (scheme, node, error))
1324 				return;
1325 
1326 	/* NULL-terminate the array of authors */
1327 	if (scheme->priv->authors != NULL)
1328 		g_ptr_array_add (scheme->priv->authors, NULL);
1329 }
1330 
1331 /**
1332  * _gtk_source_style_scheme_new_from_file:
1333  * @filename: file to parse.
1334  *
1335  * Returns: (nullable): new #GtkSourceStyleScheme created from file,
1336  * or %NULL on error.
1337  *
1338  * Since: 2.0
1339  */
1340 GtkSourceStyleScheme *
_gtk_source_style_scheme_new_from_file(const gchar * filename)1341 _gtk_source_style_scheme_new_from_file (const gchar *filename)
1342 {
1343 	GtkSourceStyleScheme *scheme;
1344 	gchar *text;
1345 	gsize text_len;
1346 	xmlDoc *doc;
1347 	xmlNode *node;
1348 	GError *error = NULL;
1349 
1350 	g_return_val_if_fail (filename != NULL, NULL);
1351 
1352 	if (!g_file_get_contents (filename, &text, &text_len, &error))
1353 	{
1354 		gchar *filename_utf8 = g_filename_display_name (filename);
1355 		g_warning ("could not load style scheme file '%s': %s",
1356 			   filename_utf8, error->message);
1357 		g_free (filename_utf8);
1358 		g_clear_error (&error);
1359 		return NULL;
1360 	}
1361 
1362 	doc = xmlParseMemory (text, text_len);
1363 
1364 	if (!doc)
1365 	{
1366 		gchar *filename_utf8 = g_filename_display_name (filename);
1367 		g_warning ("could not parse scheme file '%s'", filename_utf8);
1368 		g_free (filename_utf8);
1369 		g_free (text);
1370 		return NULL;
1371 	}
1372 
1373 	node = xmlDocGetRootElement (doc);
1374 
1375 	if (node == NULL)
1376 	{
1377 		gchar *filename_utf8 = g_filename_display_name (filename);
1378 		g_warning ("could not load scheme file '%s': empty document", filename_utf8);
1379 		g_free (filename_utf8);
1380 		xmlFreeDoc (doc);
1381 		g_free (text);
1382 		return NULL;
1383 	}
1384 
1385 	scheme = g_object_new (GTK_SOURCE_TYPE_STYLE_SCHEME, NULL);
1386 	scheme->priv->filename = g_strdup (filename);
1387 
1388 	parse_style_scheme_element (scheme, node, &error);
1389 
1390 	if (error != NULL)
1391 	{
1392 		gchar *filename_utf8 = g_filename_display_name (filename);
1393 		g_warning ("could not load style scheme file '%s': %s",
1394 			   filename_utf8, error->message);
1395 		g_free (filename_utf8);
1396 		g_clear_error (&error);
1397 		g_clear_object (&scheme);
1398 	}
1399 	else
1400 	{
1401 		/* css style part */
1402 		generate_css_style (scheme);
1403 	}
1404 
1405 	xmlFreeDoc (doc);
1406 	g_free (text);
1407 
1408 	return scheme;
1409 }
1410 
1411 /**
1412  * _gtk_source_style_scheme_get_parent_id:
1413  * @scheme: a #GtkSourceStyleScheme.
1414  *
1415  * Returns: (nullable): parent style scheme id or %NULL.
1416  *
1417  * Since: 2.0
1418  */
1419 const gchar *
_gtk_source_style_scheme_get_parent_id(GtkSourceStyleScheme * scheme)1420 _gtk_source_style_scheme_get_parent_id (GtkSourceStyleScheme *scheme)
1421 {
1422 	g_return_val_if_fail (GTK_SOURCE_IS_STYLE_SCHEME (scheme), NULL);
1423 
1424 	return scheme->priv->parent_id;
1425 }
1426 
1427 /**
1428  * _gtk_source_style_scheme_set_parent:
1429  * @scheme: a #GtkSourceStyleScheme.
1430  * @parent_scheme: parent #GtkSourceStyleScheme for @scheme.
1431  *
1432  * Sets @parent_scheme as parent scheme for @scheme, @scheme will
1433  * look for styles in @parent_scheme if it doesn't have style set
1434  * for given name.
1435  *
1436  * Since: 2.0
1437  */
1438 void
_gtk_source_style_scheme_set_parent(GtkSourceStyleScheme * scheme,GtkSourceStyleScheme * parent_scheme)1439 _gtk_source_style_scheme_set_parent (GtkSourceStyleScheme *scheme,
1440 				     GtkSourceStyleScheme *parent_scheme)
1441 {
1442 	g_return_if_fail (GTK_SOURCE_IS_STYLE_SCHEME (scheme));
1443 	g_return_if_fail (parent_scheme == NULL || GTK_SOURCE_IS_STYLE_SCHEME (parent_scheme));
1444 
1445 	if (scheme->priv->parent == parent_scheme)
1446 	{
1447 		return;
1448 	}
1449 
1450 	g_clear_object (&scheme->priv->parent);
1451 
1452 	if (parent_scheme != NULL)
1453 	{
1454 		g_object_ref (parent_scheme);
1455 	}
1456 
1457 	scheme->priv->parent = parent_scheme;
1458 
1459 	/* Update CSS based on parent styles */
1460 	g_hash_table_remove_all (scheme->priv->style_cache);
1461 	generate_css_style (scheme);
1462 }
1463 
1464 /**
1465  * _gtk_source_style_scheme_get_default:
1466  *
1467  * Returns: default style scheme to be used when user didn't set
1468  * style scheme explicitly.
1469  *
1470  * Since: 2.0
1471  */
1472 GtkSourceStyleScheme *
_gtk_source_style_scheme_get_default(void)1473 _gtk_source_style_scheme_get_default (void)
1474 {
1475 	GtkSourceStyleSchemeManager *manager;
1476 
1477 	manager = gtk_source_style_scheme_manager_get_default ();
1478 
1479 	return gtk_source_style_scheme_manager_get_scheme (manager,
1480 							   DEFAULT_STYLE_SCHEME);
1481 }
1482