summaryrefslogtreecommitdiffstats
path: root/src/libr-gtk.c
blob: f746aa83c8c5da80cc2d6648e3b4c5ebe152e595 (plain)
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
/*
 *
 *  Copyright (c) 2008-2009 Erich Hoover
 *
 *  libr GTK support - Convenience functions for using resources in GTK applications
 *
 * This program 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.
 *
 * This program 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 *
 * To provide feedback, report bugs, or otherwise contact me:
 * ehoover at mines dot edu
 *
 */

/* Include compile-time parameters */
#include "config.h"

#include "libr.h"
#include "libr-gtk.h"
#include "libr-icons.h"
#include "tempfiles.h"

/* For loading GTK/GDK images */
#include <gdk-pixbuf/gdk-pixbuf.h>
#include <glib.h>

/* For loading GLADE files */
#include <glade/glade.h>

/* For loading GTK+ Builder files */
#include <gtk/gtk.h>

/* For malloc/free */
#include <stdlib.h>

/* For string handling */
#include <string.h>

typedef gchar * (*GladeFileCallback)(GladeXML *, const gchar *, guint *);
GladeFileCallback glade_set_file_callback(GladeFileCallback callback, gpointer user_data);

/* Use weak binding for all glade and GTK+ requirements */
#pragma weak glade_set_file_callback

#pragma weak gtk_window_set_default_icon_list
#pragma weak gdk_pixbuf_loader_get_pixbuf
#pragma weak gtk_builder_add_from_string
#pragma weak gdk_pixbuf_loader_set_size
#pragma weak g_type_check_instance_cast
#pragma weak gtk_builder_add_from_file
#pragma weak glade_xml_new_from_buffer
#pragma weak gdk_pixbuf_loader_write
#pragma weak gdk_pixbuf_loader_close
#pragma weak gdk_pixbuf_loader_new
#pragma weak g_signal_connect_data
#pragma weak g_signal_connect
#pragma weak gtk_builder_new
#pragma weak g_object_unref
#pragma weak glade_xml_new
#pragma weak g_list_append
#pragma weak glade_init
#pragma weak gtk_init
#pragma weak g_free

#define GLADE_SECTION   ".glade"
#define BUILDER_SECTION ".ui"

/*
 * Handle the resource request from libglade
 */
gchar *libr_glade_read_resource(GladeXML *gladefile, const gchar *filename, guint *size, gpointer user_data)
{
	return libr_malloc((libr_file *) user_data, (char *) filename, (size_t *) size); 
}

/*
 * Handle the resource request from GtkBuilder
 */
gboolean libr_gtk_read_resource(GtkBuilder *builder, const gchar *filename, gchar **data, gsize *size, GError **error, gpointer user_data)
{
	if(data == NULL)
		return FALSE;
	*data = libr_malloc((libr_file *) user_data, (char *) filename, (size_t *) size);
	if(*data == NULL)
		return FALSE;
	return TRUE;
}

/*
 * Load the libglade resource appropriately for the currently installed version
 * (AKA, hurray hacks!)
 */
GladeXML *libr_new_glade(libr_file *handle, char *gladefile, size_t gladefile_size)
{
	if(glade_set_file_callback) /* The not-yet (ever?) existing way */
	{
		/* Register a callback for libglade to load our resources */
		if(glade_set_file_callback((GladeFileCallback) libr_glade_read_resource, handle) != NULL)
			printf("warning: over-wrote an application callback!\n");
		/* Initialize libglade almost as usual, just use a buffer instead of a file */
		return glade_xml_new_from_buffer(gladefile, gladefile_size, NULL, NULL);
	}
	else /* The hacky way */
	{
		char *glade_file[PATH_MAX];
		GladeXML *ret = NULL;
		char *temp_folder;
		
		temp_folder = libr_extract_resources(handle);
		if(temp_folder == NULL)
			return NULL;
		strcpy((char*)glade_file, temp_folder);
		strcat((char*)glade_file, "/");
		strcat((char*)glade_file, GLADE_SECTION);
		ret = glade_xml_new((char*)glade_file, NULL, NULL);
		if(ret == NULL)
			cleanup_folder(temp_folder);
		else
			register_folder_cleanup(temp_folder);
		return ret;
	}
}

/*
 * Load the GtkBuilder resource appropriately for the currently installed version
 * (AKA, hurray hacks!)
 */
int libr_new_builder(libr_file *handle, char *builderfile, size_t builderfile_size, GtkBuilder *builder)
{
	/* Register a callback for GtkBuilder to load our resources */
	if(g_signal_connect(builder, "load-resource", (GCallback) libr_gtk_read_resource, handle))
	{
		/* Initialize GtkBuilder almost as usual, just use a buffer instead of a file */
		if(gtk_builder_add_from_string(builder, builderfile, builderfile_size, NULL) == 0)
			return false;
		return true;
	}
	else /* The hacky way */
	{
		char *builder_file[PATH_MAX];
		char *temp_folder;
		int ret = false;
		
		temp_folder = libr_extract_resources(handle);
		if(temp_folder == NULL)
			return false;
		strcpy((char*)builder_file, temp_folder);
		strcat((char*)builder_file, "/");
		strcat((char*)builder_file, BUILDER_SECTION);
		ret = gtk_builder_add_from_file(builder, (char*)builder_file, NULL);
		if(ret == 0)
			cleanup_folder(temp_folder);
		else
			register_folder_cleanup(temp_folder);
		g_free(temp_folder);
		return (ret != 0);
	}
}

/*
 * Return a GTK icon list
 */
EXPORT_FN IconList *libr_gtk_iconlist(libr_file *handle)
{
	int sizes[] = {16, 32, 48, 96, 128};
	IconList *icons = NULL;
	GdkPixbuf *icon = NULL;
	int sizes_len = 5, i;
	
	if(handle == NULL)
	{
		/* Must pass a file handle to obtain the icons from */
		return NULL;
	}
	if(gtk_init == NULL)
	{
		/* GTK+ was not linked with the application */
		return false;
	}
	/* Go through the list of GTK "required" image sizes and build the icons */
	for(i=0;i<sizes_len;i++)
	{ 
		libr_icon *icon_handle = libr_icon_geticon_bysize(handle, sizes[i]);
		GdkPixbufLoader *stream = gdk_pixbuf_loader_new();
		char *iconfile = NULL;
		size_t iconfile_size;
		
		if(icon_handle == NULL)
		{
			/* Failed to find an icon */
			printf("Failed to find an icon\n");
			continue;
		}
		iconfile = libr_icon_malloc(icon_handle, &iconfile_size);
		if(iconfile == NULL)
		{
			/* Failed to obtain embedded icon */
			continue;
		}
		libr_icon_close(icon_handle);
		/* TODO: Use the "size-prepared" signal to properly scale the width and height
void user_function (GdkPixbufLoader *loader, gint width, gint height, gpointer user_data)
		 */ 
		gdk_pixbuf_loader_set_size(stream, sizes[i], sizes[i]);
		if(!gdk_pixbuf_loader_write(stream, (unsigned char *)iconfile, iconfile_size, NULL))
		{
			/* Failed to process image */
			continue;
		}
		if(!gdk_pixbuf_loader_close(stream, NULL))
		{
			/* Failed to create image */
			continue;
		}
		icon = gdk_pixbuf_loader_get_pixbuf(stream);
		if(icon == NULL)
		{
			/* Failed to convert image to pixbuf */
			continue;
		}
		icons = g_list_append(icons, icon);
	}
	return icons;
}

/*
 * Shared GtkBuilder resource loading
 */
GtkBuilder *libr_gtk_load_internal(libr_file *handle, char *resource_name)
{
	GtkBuilder *builder = NULL;
	size_t builder_length;
	char *builder_data;
	
	/* Obtain the GtkBuilder XML definition */
	builder_data = libr_malloc(handle, resource_name, &builder_length);
	if(builder_data == NULL)
	{
		/* Failed to obtain embedded GtkBuilder definition file */
		goto failed;
	}
	/* Setup the GtkBuilder environment */
	builder = gtk_builder_new();
	if(builder == NULL)
		goto failed;
	if(!libr_new_builder(handle, builder_data, builder_length, builder))
	{
		/* Failed to build interface from resource file */
		g_object_unref(G_OBJECT(builder));
		goto failed;
	}
failed:
	free(builder_data);
	return builder;
}

/*
 * Load the requested GtkBuilder resource and any applicable icons
 */
EXPORT_FN int libr_gtk_load(BuilderHandle **gtk_ret, char *resource_name)
{
	libr_file *handle;
	int ret = false;
	
	if(gtk_ret == NULL)
	{
		/* Why on earth would you call this without obtaining the handle to the resource? */
		return false;
	}
	if(gtk_builder_new == NULL)
	{
		/* GtkBuilder was not linked with the application */
		return false;
	}
	/* Obtain the handle to the executable */
	if((handle = libr_open(NULL, LIBR_READ)) == NULL)
	{
		/* "Failed to open this executable (%s) for resources", progname() */
		return false;
	}
	register_internal_handle(handle);
	*gtk_ret = libr_gtk_load_internal(handle, resource_name);
	if(*gtk_ret == NULL)
		goto failed;
	ret = true;
failed:
	if(!ret)
		libr_close(handle);
	return ret;
}

/*
 * Automatically load the ".ui" GtkBuilder resource and any applicable icons
 */
EXPORT_FN int libr_gtk_autoload(BuilderHandle **gtk_ret, IconList **icons_ret, int set_default_icon)
{
	GList *icons = NULL;
	libr_file *handle;
	int ret = false;
	
	if(gtk_ret == NULL)
	{
		/* Why on earth would you call this without obtaining the handle to the resource? */
		return false;
	}
	if(gtk_builder_new == NULL)
	{
		/* GtkBuilder was not linked with the application */
		return false;
	}
	/* Obtain the handle to the executable */
	if((handle = libr_open(NULL, LIBR_READ)) == NULL)
	{
		/* "Failed to open this executable (%s) for resources", progname() */
		return false;
	}
	register_internal_handle(handle);
	/* Obtain the icons from the ELF binary */
	icons = libr_gtk_iconlist(handle);
	/* Set the embedded icons as the default icon list (if requested) */
	if(icons != NULL && set_default_icon)
		gtk_window_set_default_icon_list(icons);
	*gtk_ret = libr_gtk_load_internal(handle, BUILDER_SECTION);
	if(*gtk_ret == NULL)
		goto failed;
	if(icons_ret)
		*icons_ret = icons;
	ret = true;
failed:
	if(!ret)
		libr_close(handle);
	return ret;
}

/*
 * Shared libglade resource loading
 */
GladeXML *libr_glade_load_internal(libr_file *handle, char *resource_name)
{
	GladeXML *glade = NULL;
	size_t glade_length;
	char *glade_data;
	
	/* Obtain the GLADE XML definition */
	glade_data = libr_malloc(handle, resource_name, &glade_length);
	if(glade_data == NULL)
	{
		/* Failed to obtain embedded glade file */
		goto failed;
	}
	/* Initialize libglade appropriate for the available version */
	glade = libr_new_glade(handle, glade_data, glade_length);
	if(glade == NULL)
	{
		/* Failed to initialize embedded glade file */
		goto failed;
	}
failed:
	free(glade_data);
	return glade;
}

/*
 * Load the requested libglade resource and any applicable icons
 */
EXPORT_FN int libr_glade_load(GladeHandle **glade_ret, char *resource_name)
{
	libr_file *handle;
	int ret = false;
	
	if(glade_ret == NULL)
	{
		/* Why on earth would you call this without obtaining the handle to the resource? */
		return false;
	}
	if(glade_init == NULL)
	{
		/* libglade was not linked with the application */
		return false;
	}
	/* Obtain the handle to the executable */
	if((handle = libr_open(NULL, LIBR_READ)) == NULL)
	{
		/* "Failed to open this executable (%s) for resources", progname() */
		return false;
	}
	register_internal_handle(handle);
	*glade_ret = libr_glade_load_internal(handle, resource_name);
	if(*glade_ret == NULL)
		goto failed;
	ret = true;
failed:
	if(!ret)
		libr_close(handle);
	return ret;
}

/*
 * Automatically load the ".glade" resource and any applicable icons
 */
EXPORT_FN int libr_glade_autoload(GladeHandle **glade_ret, IconList **icons_ret, int set_default_icon)
{
	libr_file *handle = NULL;
	GList *icons = NULL;
	
	if(glade_ret == NULL)
	{
		/* Why on earth would you call this without obtaining the handle to the resource? */
		return false;
	}
	if(glade_init == NULL)
	{
		/* libglade was not linked with the application */
		return false;
	}
	/* Obtain the handle to the executable */
	if((handle = libr_open(NULL, LIBR_READ)) == NULL)
	{
		/* "Failed to open this executable (%s) for resources", progname() */
		return false;
	}
	register_internal_handle(handle);
	icons = libr_gtk_iconlist(handle);
	/* Set the embedded icons as the default icon list (if requested) */
	if(icons != NULL && set_default_icon)
		gtk_window_set_default_icon_list(icons);
	/* Return the libglade and icon handles for the application */
	*glade_ret = libr_glade_load_internal(handle, GLADE_SECTION);
	if(icons_ret)
		*icons_ret = icons;
	return true;
}