File: gtksourcestyleschemecss.c

package info (click to toggle)
libgedit-gtksourceview 299.5.0-1
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 7,536 kB
  • sloc: ansic: 48,501; xml: 2,620; perl: 206; sh: 51; yacc: 45; makefile: 35; cobol: 20; objc: 19; javascript: 16; fortran: 14; python: 13; cpp: 8; ml: 3
file content (445 lines) | stat: -rw-r--r-- 11,987 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
/*
 * This file is part of GtkSourceView
 *
 * Copyright (C) 2023 - Sébastien Wilmet <swilmet@gnome.org>
 *
 * GtkSourceView is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * GtkSourceView is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this library; if not, see <http://www.gnu.org/licenses/>.
 */

#include "gtksourcestyleschemecss.h"

struct _GtkSourceStyleSchemeCssPrivate
{
	GtkSourceStyleScheme *scheme; /* weak ref */

	GtkCssProvider *main_provider;
	GtkCssProvider *cursors_provider;
};

/* We need to be lower than the application priority to allow application
 * overrides.
 */
#define GTK_SOURCE_STYLE_PROVIDER_PRIORITY (GTK_STYLE_PROVIDER_PRIORITY_APPLICATION - 1)

G_DEFINE_TYPE_WITH_PRIVATE (GtkSourceStyleSchemeCss, _gtk_source_style_scheme_css, G_TYPE_OBJECT)

static void
_gtk_source_style_scheme_css_dispose (GObject *object)
{
	GtkSourceStyleSchemeCss *scheme_css = GTK_SOURCE_STYLE_SCHEME_CSS (object);

	g_clear_weak_pointer (&scheme_css->priv->scheme);

	g_clear_object (&scheme_css->priv->main_provider);
	g_clear_object (&scheme_css->priv->cursors_provider);

	G_OBJECT_CLASS (_gtk_source_style_scheme_css_parent_class)->dispose (object);
}

static void
_gtk_source_style_scheme_css_class_init (GtkSourceStyleSchemeCssClass *klass)
{
	GObjectClass *object_class = G_OBJECT_CLASS (klass);

	object_class->dispose = _gtk_source_style_scheme_css_dispose;
}

static void
_gtk_source_style_scheme_css_init (GtkSourceStyleSchemeCss *scheme_css)
{
	scheme_css->priv = _gtk_source_style_scheme_css_get_instance_private (scheme_css);
}

static GtkCssProvider *
create_provider (const gchar *css)
{
	GtkCssProvider *provider;
	GError *error = NULL;

	if (css == NULL)
	{
		return NULL;
	}

	provider = gtk_css_provider_new ();
	gtk_css_provider_load_from_data (provider, css, -1, &error);

	if (error != NULL)
	{
		g_warning ("Failed to load CSS: %s", error->message);
		g_clear_error (&error);
		g_clear_object (&provider);
	}

	return provider;
}

/* --- For the main CSS provider ------------------------------------ */

static gchar *
get_foreground_color_css_declaration (GtkSourceStyle *style)
{
	gchar *ret = NULL;

	if (style->use_foreground_color)
	{
		gchar *fg_color_str = gdk_rgba_to_string (&style->foreground_color);
		ret = g_strdup_printf ("\tcolor: %s;\n", fg_color_str);
		g_free (fg_color_str);
	}

	return ret;
}

static gchar *
get_background_color_css_declaration (GtkSourceStyle *style)
{
	gchar *ret = NULL;

	if (style->use_background_color)
	{
		gchar *bg_color_str = gdk_rgba_to_string (&style->background_color);
		ret = g_strdup_printf ("\tbackground-color: %s;\n", bg_color_str);
		g_free (bg_color_str);
	}

	return ret;
}

static void
append_css_style (GString        *string,
                  GtkSourceStyle *style,
                  const gchar    *selector)
{
	gchar *fg_decl;
	gchar *bg_decl;

	if (style == NULL)
	{
		return;
	}

	fg_decl = get_foreground_color_css_declaration (style);
	bg_decl = get_background_color_css_declaration (style);

	if (fg_decl == NULL && bg_decl == NULL)
	{
		return;
	}

	g_string_append_printf (string,
				"%s {\n"
				"%s"
				"%s"
				"}\n",
				selector,
				fg_decl != NULL ? fg_decl : "",
				bg_decl != NULL ? bg_decl : "");

	g_free (fg_decl);
	g_free (bg_decl);
}

static gchar *
get_main_css (GtkSourceStyleScheme *scheme)
{
	GString *string;
	GtkSourceStyle *style;
	GtkSourceStyle *style2;

	string = g_string_new (NULL);

	style = gtk_source_style_scheme_get_style (scheme, "text");
	append_css_style (string, style, "textview text");

	style = gtk_source_style_scheme_get_style (scheme, "selection");
	append_css_style (string, style, "textview:focus text selection");

	style2 = gtk_source_style_scheme_get_style (scheme, "selection-unfocused");
	append_css_style (string,
			  style2 != NULL ? style2 : style,
			  "textview text selection");

	/* For now we use "line-numbers" colors for all the gutters. */
	style = gtk_source_style_scheme_get_style (scheme, "line-numbers");
	if (style != NULL)
	{
		append_css_style (string, style, "textview border");

		/* Needed for GtkSourceGutter. In the ::draw callback,
		 * gtk_style_context_add_class() is called to add e.g. the
		 * "left" class. Because as of GTK 3.20 we cannot do the same to
		 * add the "border" subnode.
		 */
		append_css_style (string, style, "textview .left");
		append_css_style (string, style, "textview .right");
		append_css_style (string, style, "textview .top");
		append_css_style (string, style, "textview .bottom");

		/* For the corners if the top or bottom gutter is also
		 * displayed.
		 * FIXME: this shouldn't be necessary, GTK should apply the
		 * border style to the corners too, see:
		 * https://bugzilla.gnome.org/show_bug.cgi?id=764239
		 */
		append_css_style (string, style, "textview");
	}

	style = gtk_source_style_scheme_get_style (scheme, "current-line-number");
	append_css_style (string, style, ".current-line-number");

	return g_string_free (string, FALSE);
}

static GtkCssProvider *
create_main_provider (GtkSourceStyleScheme *scheme)
{
	gchar *css;
	GtkCssProvider *provider;

	css = get_main_css (scheme);
	provider = create_provider (css);
	g_free (css);

	return provider;
}

/* --- For cursors -------------------------------------------------- */

static gboolean
get_style_foreground_color (GtkSourceStyleScheme *scheme,
			    const gchar          *style_id,
			    GdkRGBA              *foreground_color)
{
	GtkSourceStyle *style;

	g_return_val_if_fail (GTK_SOURCE_IS_STYLE_SCHEME (scheme), FALSE);
	g_return_val_if_fail (style_id != NULL, FALSE);
	g_return_val_if_fail (foreground_color != NULL, FALSE);

	style = gtk_source_style_scheme_get_style (scheme, style_id);

	if (style != NULL && style->use_foreground_color)
	{
		*foreground_color = style->foreground_color;
		return TRUE;
	}

	return FALSE;
}

static gchar *
get_cursors_css (GtkSourceStyleScheme *scheme,
		 GtkWidget            *widget)
{
	gboolean primary_color_set;
	gboolean secondary_color_set;
	GdkRGBA primary_color;
	GdkRGBA secondary_color;
	GString *css;

	primary_color_set = get_style_foreground_color (scheme, "cursor", &primary_color);
	secondary_color_set = get_style_foreground_color (scheme, "secondary-cursor", &secondary_color);

	if (!primary_color_set && !secondary_color_set)
	{
		return NULL;
	}

	if (!secondary_color_set)
	{
		GtkStyleContext *context;
		GdkRGBA *background_color = NULL;

		g_assert (primary_color_set);

		context = gtk_widget_get_style_context (widget);

		gtk_style_context_save (context);
		gtk_style_context_set_state (context, GTK_STATE_FLAG_NORMAL);

		gtk_style_context_get (context,
				       gtk_style_context_get_state (context),
				       "background-color", &background_color,
				       NULL);

		gtk_style_context_restore (context);

		if (background_color != NULL)
		{
			/* Blend primary cursor color with background color. */
			secondary_color.red = (primary_color.red + background_color->red) * 0.5;
			secondary_color.green = (primary_color.green + background_color->green) * 0.5;
			secondary_color.blue = (primary_color.blue + background_color->blue) * 0.5;
			secondary_color.alpha = (primary_color.alpha + background_color->alpha) * 0.5;

			secondary_color_set = TRUE;

			gdk_rgba_free (background_color);
		}
	}

	css = g_string_new ("textview text {\n");

	if (primary_color_set)
	{
		gchar *primary_color_str;

		primary_color_str = gdk_rgba_to_string (&primary_color);
		g_string_append_printf (css,
					"\tcaret-color: %s;\n",
					primary_color_str);
		g_free (primary_color_str);
	}

	if (secondary_color_set)
	{
		gchar *secondary_color_str;

		secondary_color_str = gdk_rgba_to_string (&secondary_color);
		g_string_append_printf (css,
					"\t-gtk-secondary-caret-color: %s;\n",
					secondary_color_str);
		g_free (secondary_color_str);
	}

	g_string_append (css, "}\n");

	return g_string_free (css, FALSE);
}

/* The GtkCssProvider for the cursors needs a GtkWidget to blend with the
 * background color in case the secondary cursor color isn't defined.
 *
 * The background color is either defined by the GtkSourceStyleScheme, or if
 * it's not defined it is taken from the GTK theme.
 *
 * So ideally, if the GTK theme changes at runtime, we should regenerate the
 * GtkCssProvider for the cursors, but it isn't done.
 */
static GtkCssProvider *
create_cursors_provider (GtkSourceStyleScheme *scheme,
			 GtkWidget            *widget)
{
	gchar *css;
	GtkCssProvider *provider;

	css = get_cursors_css (scheme, widget);
	provider = create_provider (css);
	g_free (css);

	return provider;
}

/* --- Public functions --------------------------------------------- */

GtkSourceStyleSchemeCss *
_gtk_source_style_scheme_css_new (GtkSourceStyleScheme *scheme)
{
	GtkSourceStyleSchemeCss *scheme_css;

	g_return_val_if_fail (GTK_SOURCE_IS_STYLE_SCHEME (scheme), NULL);

	scheme_css = g_object_new (GTK_SOURCE_TYPE_STYLE_SCHEME_CSS, NULL);

	g_set_weak_pointer (&scheme_css->priv->scheme, scheme);

	return scheme_css;
}

/* Adds the #GtkCssProvider's (that are part of @scheme_css) to @widget.
 * @widget is typically a #GtkSourceView.
 */
void
_gtk_source_style_scheme_css_apply (GtkSourceStyleSchemeCss *scheme_css,
				    GtkWidget               *widget)
{
	GtkStyleContext *context;

	g_return_if_fail (GTK_SOURCE_IS_STYLE_SCHEME_CSS (scheme_css));
	g_return_if_fail (GTK_IS_WIDGET (widget));

	if (scheme_css->priv->scheme == NULL)
	{
		return;
	}

	context = gtk_widget_get_style_context (widget);

	if (scheme_css->priv->main_provider == NULL)
	{
		scheme_css->priv->main_provider = create_main_provider (scheme_css->priv->scheme);
	}

	if (scheme_css->priv->main_provider != NULL)
	{
		gtk_style_context_add_provider (context,
						GTK_STYLE_PROVIDER (scheme_css->priv->main_provider),
						GTK_SOURCE_STYLE_PROVIDER_PRIORITY);

		G_GNUC_BEGIN_IGNORE_DEPRECATIONS;
		/* See https://bugzilla.gnome.org/show_bug.cgi?id=708583 */
		gtk_style_context_invalidate (context);
		G_GNUC_END_IGNORE_DEPRECATIONS;
	}

	/* The GtkCssProvider for the cursors needs that the first provider is
	 * applied, to get the background color.
	 */
	if (scheme_css->priv->cursors_provider == NULL)
	{
		scheme_css->priv->cursors_provider = create_cursors_provider (scheme_css->priv->scheme, widget);
	}

	if (scheme_css->priv->cursors_provider != NULL)
	{
		gtk_style_context_add_provider (context,
						GTK_STYLE_PROVIDER (scheme_css->priv->cursors_provider),
						GTK_SOURCE_STYLE_PROVIDER_PRIORITY);

		G_GNUC_BEGIN_IGNORE_DEPRECATIONS;
		gtk_style_context_invalidate (context);
		G_GNUC_END_IGNORE_DEPRECATIONS;
	}
}

/* Removes the #GtkCssProvider's (that are part of @scheme_css) from @widget.
 * @widget is typically a #GtkSourceView.
 */
void
_gtk_source_style_scheme_css_unapply (GtkSourceStyleSchemeCss *scheme_css,
				      GtkWidget               *widget)
{
	GtkStyleContext *context;

	g_return_if_fail (GTK_SOURCE_IS_STYLE_SCHEME_CSS (scheme_css));
	g_return_if_fail (GTK_IS_WIDGET (widget));

	context = gtk_widget_get_style_context (widget);

	if (scheme_css->priv->main_provider != NULL)
	{
		gtk_style_context_remove_provider (context, GTK_STYLE_PROVIDER (scheme_css->priv->main_provider));
	}

	if (scheme_css->priv->cursors_provider != NULL)
	{
		gtk_style_context_remove_provider (context, GTK_STYLE_PROVIDER (scheme_css->priv->cursors_provider));
	}

	G_GNUC_BEGIN_IGNORE_DEPRECATIONS;
	/* See https://bugzilla.gnome.org/show_bug.cgi?id=708583 */
	gtk_style_context_invalidate (context);
	G_GNUC_END_IGNORE_DEPRECATIONS;
}