Debian Codegraph @

gtksourceview/gtksourcestylescheme.c

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