1 /* ide-source-style-scheme.c
2  *
3  * Copyright 2016-2019 Christian Hergert <chergert@redhat.com>
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
17  *
18  * SPDX-License-Identifier: GPL-3.0-or-later
19  */
20 
21 #define G_LOG_DOMAIN "ide-source-style-scheme"
22 
23 #include "config.h"
24 
25 #include <string.h>
26 
27 #include "ide-source-style-scheme.h"
28 
29 gboolean
ide_source_style_scheme_apply_style(GtkSourceStyleScheme * style_scheme,const gchar * style_name,GtkTextTag * tag)30 ide_source_style_scheme_apply_style (GtkSourceStyleScheme *style_scheme,
31                                      const gchar          *style_name,
32                                      GtkTextTag           *tag)
33 {
34   g_autofree gchar *foreground = NULL;
35   g_autofree gchar *background = NULL;
36   g_autofree gchar *underline_color = NULL;
37   GdkRGBA underline_rgba;
38   GtkSourceStyle *style;
39   const gchar *colon;
40   PangoUnderline pango_underline;
41   gboolean foreground_set = FALSE;
42   gboolean background_set = FALSE;
43   gboolean bold = FALSE;
44   gboolean bold_set = FALSE;
45   gboolean underline_set = FALSE;
46   gboolean underline_color_set = FALSE;
47   gboolean italic = FALSE;
48   gboolean italic_set = FALSE;
49 
50   g_return_val_if_fail (!style_scheme || GTK_SOURCE_IS_STYLE_SCHEME (style_scheme), FALSE);
51   g_return_val_if_fail (style_name != NULL, FALSE);
52 
53   g_object_set (tag,
54                 "foreground-set", FALSE,
55                 "background-set", FALSE,
56                 "weight-set", FALSE,
57                 "underline-set", FALSE,
58                 "underline-rgba-set", FALSE,
59                 "style-set", FALSE,
60                 NULL);
61 
62   if (style_scheme == NULL)
63     return FALSE;
64 
65   style = gtk_source_style_scheme_get_style (style_scheme, style_name);
66 
67   if (style == NULL && (colon = strchr (style_name, ':')))
68     {
69       gchar defname[64];
70 
71       g_snprintf (defname, sizeof defname, "def%s", colon);
72 
73       style = gtk_source_style_scheme_get_style (style_scheme, defname);
74     }
75 
76   if (style == NULL)
77     return FALSE;
78 
79   g_object_get (style,
80                 "background", &background,
81                 "background-set", &background_set,
82                 "foreground", &foreground,
83                 "foreground-set", &foreground_set,
84                 "bold", &bold,
85                 "bold-set", &bold_set,
86                 "pango-underline", &pango_underline,
87                 "underline-set", &underline_set,
88                 "underline-color", &underline_color,
89                 "underline-color-set", &underline_color_set,
90                 "italic", &italic,
91                 "italic-set", &italic_set,
92                 NULL);
93 
94   if (background_set)
95     g_object_set (tag, "background", background, NULL);
96 
97   if (foreground_set)
98     g_object_set (tag, "foreground", foreground, NULL);
99 
100   if (bold_set && bold)
101     g_object_set (tag, "weight", PANGO_WEIGHT_BOLD, NULL);
102 
103   if (italic_set && italic)
104     g_object_set (tag, "style", PANGO_STYLE_ITALIC, NULL);
105 
106   if (underline_set)
107     g_object_set (tag, "underline", pango_underline, NULL);
108 
109   if (underline_color_set && underline_color != NULL)
110     {
111       gdk_rgba_parse (&underline_rgba, underline_color);
112       g_object_set (tag,
113                     "underline-rgba", &underline_rgba,
114                     NULL);
115     }
116   return TRUE;
117 }
118