summaryrefslogtreecommitdiff
path: root/capplets/about-me/e-image-chooser.c
blob: cf5d1c68e823dd15cfad2d0ceae44b2b58342846 (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
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
/* e-image-chooser.c
 * Copyright (C) 2004  Novell, Inc.
 * Author: Chris Toshok <toshok@ximian.com>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of version 2 of the GNU General Public
 * License as published by the Free Software Foundation.
 *
 * 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 library; if not, write to the
 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
 * Boston, MA 02110-1301, USA.
 */

#include <config.h>

#include <string.h>

#include <glib-object.h>
#include <glib/gi18n.h>
#include <gio/gio.h>
#include <gtk/gtk.h>

#include "e-image-chooser.h"

struct _EImageChooserPrivate {

	GtkWidget *image;
	GtkWidget *browse_button;

	char *image_buf;
	int   image_buf_size;
	int   image_width;
	int   image_height;

	gboolean editable;
};

enum {
	CHANGED,
	LAST_SIGNAL
};


static guint image_chooser_signals [LAST_SIGNAL] = { 0 };

static void e_image_chooser_init	 (EImageChooser		 *chooser);
static void e_image_chooser_class_init	 (EImageChooserClass	 *klass);
static void e_image_chooser_dispose      (GObject *object);

static gboolean image_drag_motion_cb (GtkWidget *widget,
				      GdkDragContext *context,
				      gint x, gint y, guint time, EImageChooser *chooser);
static gboolean image_drag_drop_cb (GtkWidget *widget,
				    GdkDragContext *context,
				    gint x, gint y, guint time, EImageChooser *chooser);
static void image_drag_data_received_cb (GtkWidget *widget,
					 GdkDragContext *context,
					 gint x, gint y,
					 GtkSelectionData *selection_data,
					 guint info, guint time, EImageChooser *chooser);

static GObjectClass *parent_class = NULL;
#define PARENT_TYPE GTK_TYPE_BOX

enum DndTargetType {
	DND_TARGET_TYPE_URI_LIST
};
#define URI_LIST_TYPE "text/uri-list"

static GtkTargetEntry image_drag_types[] = {
	{ URI_LIST_TYPE, 0, DND_TARGET_TYPE_URI_LIST },
};
static const int num_image_drag_types = sizeof (image_drag_types) / sizeof (image_drag_types[0]);

GtkWidget *
e_image_chooser_new (void)
{
	return g_object_new (E_TYPE_IMAGE_CHOOSER, "orientation", GTK_ORIENTATION_VERTICAL, NULL);
}

GType
e_image_chooser_get_type (void)
{
	static GType eic_type = 0;

	if (!eic_type) {
		static const GTypeInfo eic_info =  {
			sizeof (EImageChooserClass),
			NULL,           /* base_init */
			NULL,           /* base_finalize */
			(GClassInitFunc) e_image_chooser_class_init,
			NULL,           /* class_finalize */
			NULL,           /* class_data */
			sizeof (EImageChooser),
			0,             /* n_preallocs */
			(GInstanceInitFunc) e_image_chooser_init,
		};

		eic_type = g_type_register_static (PARENT_TYPE, "EImageChooser", &eic_info, 0);
	}

	return eic_type;
}


static void
e_image_chooser_class_init (EImageChooserClass *klass)
{
	GObjectClass *object_class = G_OBJECT_CLASS (klass);

	parent_class = g_type_class_ref (PARENT_TYPE);

	image_chooser_signals [CHANGED] =
		g_signal_new ("changed",
			      G_OBJECT_CLASS_TYPE (object_class),
			      G_SIGNAL_RUN_FIRST,
			      G_STRUCT_OFFSET (EImageChooserClass, changed),
			      NULL, NULL,
			      g_cclosure_marshal_VOID__VOID,
#if GTK_CHECK_VERSION (3, 0, 0)
			      G_TYPE_NONE, 0);
#else
			      GTK_TYPE_NONE, 0);
#endif

	object_class->dispose = e_image_chooser_dispose;
}

static void
e_image_chooser_init (EImageChooser *chooser)
{
	EImageChooserPrivate *priv;

	priv = chooser->priv = g_new0 (EImageChooserPrivate, 1);

	priv->image = gtk_image_new ();

	gtk_box_set_homogeneous (GTK_BOX (chooser), FALSE);
	gtk_box_pack_start (GTK_BOX (chooser), priv->image, TRUE, TRUE, 0);

	gtk_drag_dest_set (priv->image, 0, image_drag_types, num_image_drag_types, GDK_ACTION_COPY);
	g_signal_connect (priv->image,
			  "drag_motion", G_CALLBACK (image_drag_motion_cb), chooser);
	g_signal_connect (priv->image,
			  "drag_drop", G_CALLBACK (image_drag_drop_cb), chooser);
	g_signal_connect (priv->image,
			  "drag_data_received", G_CALLBACK (image_drag_data_received_cb), chooser);

	gtk_widget_show_all (priv->image);

	/* we default to being editable */
	priv->editable = TRUE;
}

static void
e_image_chooser_dispose (GObject *object)
{
	EImageChooser *eic = E_IMAGE_CHOOSER (object);

	if (eic->priv) {
		EImageChooserPrivate *priv = eic->priv;

		if (priv->image_buf) {
			g_free (priv->image_buf);
			priv->image_buf = NULL;
		}

		g_free (eic->priv);
		eic->priv = NULL;
	}

	if (G_OBJECT_CLASS (parent_class)->dispose)
		(* G_OBJECT_CLASS (parent_class)->dispose) (object);
}


static gboolean
set_image_from_data (EImageChooser *chooser,
		     char *data, int length)
{
	gboolean rv = FALSE;
	GdkPixbufLoader *loader = gdk_pixbuf_loader_new ();
	GdkPixbuf *pixbuf;

	gdk_pixbuf_loader_write (loader, (guchar *) data, length, NULL);
	gdk_pixbuf_loader_close (loader, NULL);

	pixbuf = gdk_pixbuf_loader_get_pixbuf (loader);
	if (pixbuf)
		g_object_ref (pixbuf);
	g_object_unref (loader);

	if (pixbuf) {
		GdkPixbuf *scaled;
		GtkRequisition chooser_size;

		float scale;
		int new_height, new_width;

		gtk_widget_size_request (gtk_widget_get_parent (GTK_WIDGET (chooser)),
		                         &chooser_size);
		chooser_size.width -= 5;
		chooser_size.height -= 5;

		new_height = gdk_pixbuf_get_height (pixbuf);
		new_width = gdk_pixbuf_get_width (pixbuf);

		if (chooser->priv->image_height == 0
		    && chooser->priv->image_width == 0) {
			scale = 1.0;
		}
		else if (chooser->priv->image_height < new_height
			 || chooser->priv->image_width < new_width) {
			/* we need to scale down */
			if (new_height > new_width)
				scale = (float)chooser_size.height / new_height;
			else
				scale = (float)chooser_size.width / new_width;
		}
		else {
			/* we need to scale up */
			if (new_height > new_width)
				scale = (float)new_height / chooser_size.height;
			else
				scale = (float)new_width / chooser_size.width;
		}

		if (scale == 1.0) {
			gtk_image_set_from_pixbuf (GTK_IMAGE (chooser->priv->image), pixbuf);

			chooser->priv->image_width = new_width;
			chooser->priv->image_height = new_height;
		}
		else {
			new_width *= scale;
			new_height *= scale;
			new_width = MIN (new_width, chooser_size.width);
			new_height = MIN (new_height, chooser_size.height);

			scaled = gdk_pixbuf_scale_simple (pixbuf,
							  new_width, new_height,
							  GDK_INTERP_BILINEAR);

			gtk_image_set_from_pixbuf (GTK_IMAGE (chooser->priv->image), scaled);
			g_object_unref (scaled);
		}

		g_object_unref (pixbuf);

		g_free (chooser->priv->image_buf);
		chooser->priv->image_buf = data;
		chooser->priv->image_buf_size = length;

		g_signal_emit (chooser,
			       image_chooser_signals [CHANGED], 0);

		rv = TRUE;
	}

	return rv;
}

static gboolean
image_drag_motion_cb (GtkWidget *widget,
		      GdkDragContext *context,
		      gint x, gint y, guint time, EImageChooser *chooser)
{
	GList *p;

	if (!chooser->priv->editable)
		return FALSE;

	for (p = gdk_drag_context_list_targets (context); p; p = p->next) {
		char *possible_type;

		possible_type = gdk_atom_name (GDK_POINTER_TO_ATOM (p->data));
		if (!strcmp (possible_type, URI_LIST_TYPE)) {
			g_free (possible_type);
			gdk_drag_status (context, GDK_ACTION_COPY, time);
			return TRUE;
		}

		g_free (possible_type);
	}

	return FALSE;
}

static gboolean
image_drag_drop_cb (GtkWidget *widget,
		    GdkDragContext *context,
		    gint x, gint y, guint time, EImageChooser *chooser)
{
	GList *p;

	if (!chooser->priv->editable)
		return FALSE;

	if (gdk_drag_context_list_targets (context) == NULL) {
		return FALSE;
	}

	for (p = gdk_drag_context_list_targets (context); p; p = p->next) {
		char *possible_type;

		possible_type = gdk_atom_name (GDK_POINTER_TO_ATOM (p->data));
		if (!strcmp (possible_type, URI_LIST_TYPE)) {
			g_free (possible_type);
			gtk_drag_get_data (widget, context,
					   GDK_POINTER_TO_ATOM (p->data),
					   time);
			return TRUE;
		}

		g_free (possible_type);
	}

	return FALSE;
}

static void
image_drag_data_received_cb (GtkWidget *widget,
			     GdkDragContext *context,
			     gint x, gint y,
			     GtkSelectionData *selection_data,
			     guint info, guint time, EImageChooser *chooser)
{
	char *target_type;
	gboolean handled = FALSE;

	target_type = gdk_atom_name (gtk_selection_data_get_target (selection_data));

	if (!strcmp (target_type, URI_LIST_TYPE)) {
		const char *data = (const char *) gtk_selection_data_get_data (selection_data);
		char *uri;
		GFile *file;
		GInputStream *istream;
		char *nl = strstr (data, "\r\n");

		if (nl)
			uri = g_strndup (data, nl - (char *) data);
		else
			uri = g_strdup (data);

		file = g_file_new_for_uri (uri);
		istream = G_INPUT_STREAM (g_file_read (file, NULL, NULL));

		if (istream != NULL) {
			GFileInfo *info;

			info = g_file_query_info (file,
						  G_FILE_ATTRIBUTE_STANDARD_SIZE,
						  G_FILE_QUERY_INFO_NONE,
						  NULL, NULL);

			if (info != NULL) {
				gsize size;
				gboolean success;
				gchar *buf;

				size = g_file_info_get_size (info);
				g_object_unref (info);

				buf = g_malloc (size);

				success = g_input_stream_read_all (istream,
								   buf,
								   size,
								   &size,
								   NULL,
								   NULL);
				g_input_stream_close (istream, NULL, NULL);

				if (success &&
						set_image_from_data (chooser, buf, size))
					handled = TRUE;
				else
					g_free (buf);
			}

			g_object_unref (istream);
		}

		g_object_unref (file);
		g_free (uri);
	}

	gtk_drag_finish (context, handled, FALSE, time);
}

gboolean
e_image_chooser_set_from_file (EImageChooser *chooser, const char *filename)
{
	gchar *data;
	gsize data_length;

	g_return_val_if_fail (E_IS_IMAGE_CHOOSER (chooser), FALSE);
	g_return_val_if_fail (filename, FALSE);

	if (!g_file_get_contents (filename, &data, &data_length, NULL)) {
		return FALSE;
	}

	if (!set_image_from_data (chooser, data, data_length))
		g_free (data);

	return TRUE;
}

void
e_image_chooser_set_editable (EImageChooser *chooser, gboolean editable)
{
	g_return_if_fail (E_IS_IMAGE_CHOOSER (chooser));

	chooser->priv->editable = editable;

	gtk_widget_set_sensitive (chooser->priv->browse_button, editable);
}

gboolean
e_image_chooser_get_image_data (EImageChooser *chooser, char **data, gsize *data_length)
{
	g_return_val_if_fail (E_IS_IMAGE_CHOOSER (chooser), FALSE);
	g_return_val_if_fail (data != NULL, FALSE);
	g_return_val_if_fail (data_length != NULL, FALSE);

	*data_length = chooser->priv->image_buf_size;
	*data = g_malloc (*data_length);
	memcpy (*data, chooser->priv->image_buf, *data_length);

	return TRUE;
}

gboolean
e_image_chooser_set_image_data (EImageChooser *chooser, char *data, gsize data_length)
{
	char *buf;

	g_return_val_if_fail (E_IS_IMAGE_CHOOSER (chooser), FALSE);
	g_return_val_if_fail (data != NULL, FALSE);

	/* yuck, a copy... */
	buf = g_malloc (data_length);
	memcpy (buf, data, data_length);

	if (!set_image_from_data (chooser, buf, data_length)) {
		g_free (buf);
		return FALSE;
	}

	return TRUE;
}