File: gtksourcepixbufhelper.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 (293 lines) | stat: -rw-r--r-- 6,274 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
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8; coding: utf-8 -*-
 *
 * This file is part of GtkSourceView
 *
 * Copyright (C) 2010 - Jesse van den Kieboom
 *
 * 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/>.
 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include "gtksourcepixbufhelper.h"

typedef enum _IconType
{
	ICON_TYPE_PIXBUF,
	ICON_TYPE_GICON,
	ICON_TYPE_NAME
} IconType;

struct _GtkSourcePixbufHelper
{
	GdkPixbuf *cached_pixbuf;
	IconType type;

	GdkPixbuf *pixbuf;
	gchar *icon_name;
	GIcon *gicon;
};

GtkSourcePixbufHelper *
gtk_source_pixbuf_helper_new (void)
{
	return g_new0 (GtkSourcePixbufHelper, 1);
}

void
gtk_source_pixbuf_helper_free (GtkSourcePixbufHelper *helper)
{
	if (helper->pixbuf)
	{
		g_object_unref (helper->pixbuf);
	}

	if (helper->cached_pixbuf)
	{
		g_object_unref (helper->cached_pixbuf);
	}

	if (helper->gicon)
	{
		g_object_unref (helper->gicon);
	}

	g_free (helper->icon_name);

	g_free (helper);
}

static void
set_cache (GtkSourcePixbufHelper *helper,
           GdkPixbuf             *pixbuf)
{
	if (helper->cached_pixbuf)
	{
		g_object_unref (helper->cached_pixbuf);
		helper->cached_pixbuf = NULL;
	}

	if (pixbuf)
	{
		helper->cached_pixbuf = pixbuf;
	}
}

static void
clear_cache (GtkSourcePixbufHelper *helper)
{
	set_cache (helper, NULL);
}

void
gtk_source_pixbuf_helper_set_pixbuf (GtkSourcePixbufHelper *helper,
                                     const GdkPixbuf       *pixbuf)
{
	helper->type = ICON_TYPE_PIXBUF;

	if (helper->pixbuf)
	{
		g_object_unref (helper->pixbuf);
		helper->pixbuf = NULL;
	}

	if (pixbuf)
	{
		helper->pixbuf = gdk_pixbuf_copy (pixbuf);
	}

	clear_cache (helper);
}

GdkPixbuf *
gtk_source_pixbuf_helper_get_pixbuf (GtkSourcePixbufHelper *helper)
{
	return helper->pixbuf;
}

void
gtk_source_pixbuf_helper_set_icon_name (GtkSourcePixbufHelper *helper,
                                        const gchar           *icon_name)
{
	helper->type = ICON_TYPE_NAME;

	if (helper->icon_name)
	{
		g_free (helper->icon_name);
	}

	helper->icon_name = g_strdup (icon_name);

	clear_cache (helper);
}

const gchar *
gtk_source_pixbuf_helper_get_icon_name (GtkSourcePixbufHelper *helper)
{
	return helper->icon_name;
}

void
gtk_source_pixbuf_helper_set_gicon (GtkSourcePixbufHelper *helper,
                                    GIcon                 *gicon)
{
	helper->type = ICON_TYPE_GICON;

	if (helper->gicon)
	{
		g_object_unref (helper->gicon);
		helper->gicon = NULL;
	}

	if (gicon)
	{
		helper->gicon = g_object_ref (gicon);
	}

	clear_cache (helper);
}

GIcon *
gtk_source_pixbuf_helper_get_gicon (GtkSourcePixbufHelper *helper)
{
	return helper->gicon;
}

static void
from_pixbuf (GtkSourcePixbufHelper *helper,
             GtkWidget             *widget,
             gint                   size)
{
	if (helper->pixbuf == NULL)
	{
		return;
	}

	if (gdk_pixbuf_get_width (helper->pixbuf) <= size)
	{
		if (!helper->cached_pixbuf)
		{
			set_cache (helper, gdk_pixbuf_copy (helper->pixbuf));
		}

		return;
	}

	/* Make smaller */
	set_cache (helper, gdk_pixbuf_scale_simple (helper->pixbuf,
	                                            size,
	                                            size,
	                                            GDK_INTERP_BILINEAR));
}

static void
from_gicon (GtkSourcePixbufHelper *helper,
            GtkWidget             *widget,
            gint                   size)
{
	GdkScreen *screen;
	GtkIconTheme *icon_theme;
	GtkIconInfo *info;
	GtkIconLookupFlags flags;

	screen = gtk_widget_get_screen (widget);
	icon_theme = gtk_icon_theme_get_for_screen (screen);

	flags = GTK_ICON_LOOKUP_USE_BUILTIN;

	info = gtk_icon_theme_lookup_by_gicon (icon_theme,
	                                       helper->gicon,
	                                       size,
	                                       flags);

	if (info)
	{
		set_cache (helper, gtk_icon_info_load_icon (info, NULL));
	}
}

static void
from_name (GtkSourcePixbufHelper *helper,
           GtkWidget             *widget,
           gint                   size)
{
	GdkScreen *screen;
	GtkIconTheme *icon_theme;
	GtkIconInfo *info;
	GtkIconLookupFlags flags;
	gint scale;

	screen = gtk_widget_get_screen (widget);
	icon_theme = gtk_icon_theme_get_for_screen (screen);

	flags = GTK_ICON_LOOKUP_USE_BUILTIN;
        scale = gtk_widget_get_scale_factor (widget);

	info = gtk_icon_theme_lookup_icon_for_scale (icon_theme,
	                                             helper->icon_name,
	                                             size,
	                                             scale,
	                                             flags);

	if (info)
	{
		GdkPixbuf *pixbuf;

		if (gtk_icon_info_is_symbolic (info))
		{
			GtkStyleContext *context;

			context = gtk_widget_get_style_context (widget);
			pixbuf = gtk_icon_info_load_symbolic_for_context (info, context, NULL, NULL);
		}
		else
		{
			pixbuf = gtk_icon_info_load_icon (info, NULL);
		}

		set_cache (helper, pixbuf);
	}
}

GdkPixbuf *
gtk_source_pixbuf_helper_render (GtkSourcePixbufHelper *helper,
                                 GtkWidget             *widget,
                                 gint                   size)
{
	if (helper->cached_pixbuf &&
	    gdk_pixbuf_get_width (helper->cached_pixbuf) == size)
	{
		return helper->cached_pixbuf;
	}

	switch (helper->type)
	{
		case ICON_TYPE_PIXBUF:
			from_pixbuf (helper, widget, size);
			break;
		case ICON_TYPE_GICON:
			from_gicon (helper, widget, size);
			break;
		case ICON_TYPE_NAME:
			from_name (helper, widget, size);
			break;
		default:
			g_assert_not_reached ();
	}

	return helper->cached_pixbuf;
}