File: gth-preview-tool.c

package info (click to toggle)
gthumb 3%3A3.11.2-0.1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 29,364 kB
  • sloc: ansic: 145,227; cpp: 1,266; xml: 783; python: 369; yacc: 354; lex: 210; makefile: 76; sh: 46
file content (352 lines) | stat: -rw-r--r-- 9,253 bytes parent folder | download | duplicates (4)
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
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */

/*
 *  GThumb
 *
 *  Copyright (C) 2012 Free Software Foundation, Inc.
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 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, see <http://www.gnu.org/licenses/>.
 */

#include <config.h>
#include <stdlib.h>
#include <math.h>
#include <gthumb.h>
#include "gth-preview-tool.h"


static void gth_preview_tool_gth_image_tool_interface_init (GthImageViewerToolInterface *iface);


struct _GthPreviewToolPrivate {
	GthImageViewer        *viewer;
	GthFit                 original_fit_mode;
	gboolean               original_zoom_enabled;
	cairo_surface_t       *preview_image;
	cairo_rectangle_int_t  preview_image_area;
	GdkRGBA                background_color;
};


G_DEFINE_TYPE_WITH_CODE (GthPreviewTool,
			 gth_preview_tool,
			 G_TYPE_OBJECT,
			 G_ADD_PRIVATE (GthPreviewTool)
			 G_IMPLEMENT_INTERFACE (GTH_TYPE_IMAGE_VIEWER_TOOL,
						gth_preview_tool_gth_image_tool_interface_init))


static void
gth_preview_tool_set_viewer (GthImageViewerTool *base,
			     GthImageViewer     *viewer)
{
	GthPreviewTool *self = GTH_PREVIEW_TOOL (base);
	GdkCursor      *cursor;

	self->priv->viewer = viewer;
	self->priv->original_fit_mode = gth_image_viewer_get_fit_mode (GTH_IMAGE_VIEWER (viewer));
	self->priv->original_zoom_enabled = gth_image_viewer_get_zoom_enabled (GTH_IMAGE_VIEWER (viewer));
	gth_image_viewer_set_fit_mode (GTH_IMAGE_VIEWER (viewer), GTH_FIT_SIZE_IF_LARGER);
	gth_image_viewer_set_zoom_enabled (GTH_IMAGE_VIEWER (viewer), FALSE);

	cursor = _gdk_cursor_new_for_widget (GTK_WIDGET (self->priv->viewer), GDK_LEFT_PTR);
	gth_image_viewer_set_cursor (self->priv->viewer, cursor);

	g_object_unref (cursor);
}


static void
gth_preview_tool_unset_viewer (GthImageViewerTool *base,
			       GthImageViewer     *viewer)
{
	GthPreviewTool *self = GTH_PREVIEW_TOOL (base);

	gth_image_viewer_set_fit_mode (GTH_IMAGE_VIEWER (viewer), self->priv->original_fit_mode);
	gth_image_viewer_set_zoom_enabled (GTH_IMAGE_VIEWER (viewer), self->priv->original_zoom_enabled);
	self->priv->viewer = NULL;
}


static void
gth_preview_tool_realize (GthImageViewerTool *base)
{
	/* void */
}


static void
gth_preview_tool_unrealize (GthImageViewerTool *base)
{
	/* void */
}


#if 0


static void
update_preview_image (GthPreviewTool *self)
{
	cairo_surface_t *image;
	int              width;
	int              height;
	GtkAllocation    allocation;
	int              max_size;

	if (self->priv->preview_image != NULL) {
		cairo_surface_destroy (self->priv->preview_image);
		self->priv->preview_image = NULL;
	}

	image = gth_image_viewer_get_current_image (GTH_IMAGE_VIEWER (self->priv->viewer));
	if (image == NULL)
		return;

	width = cairo_image_surface_get_width (image);
	height = cairo_image_surface_get_height (image);
	gtk_widget_get_allocation (GTK_WIDGET (self->priv->viewer), &allocation);
	max_size = MAX (allocation.width, allocation.height) / G_SQRT2 + 2;
	if (scale_keeping_ratio (&width, &height, max_size, max_size, FALSE))
		self->priv->preview_image = _cairo_image_surface_scale_fast (image, width, height);
	else
		self->priv->preview_image = cairo_surface_reference (image);

	self->priv->preview_image_area.width = width;
	self->priv->preview_image_area.height = height;
	self->priv->preview_image_area.x = MAX ((allocation.width - self->priv->preview_image_area.width) / 2 - 0.5, 0);
	self->priv->preview_image_area.y = MAX ((allocation.height - self->priv->preview_image_area.height) / 2 - 0.5, 0);
}


#endif


static void
update_preview_image_area (GthPreviewTool *self)
{
	int           width;
	int           height;
	GtkAllocation allocation;

	if ((self->priv->preview_image == NULL) || (self->priv->viewer == NULL))
		return;

	if (! gtk_widget_get_realized (GTK_WIDGET (self->priv->viewer)))
		return;

	width = cairo_image_surface_get_width (self->priv->preview_image);
	height = cairo_image_surface_get_height (self->priv->preview_image);
	gtk_widget_get_allocation (GTK_WIDGET (self->priv->viewer), &allocation);

	self->priv->preview_image_area.width = width;
	self->priv->preview_image_area.height = height;
	self->priv->preview_image_area.x = MAX ((allocation.width - self->priv->preview_image_area.width) / 2 - 0.5, 0);
	self->priv->preview_image_area.y = MAX ((allocation.height - self->priv->preview_image_area.height) / 2 - 0.5, 0);
}


static void
gth_preview_tool_size_allocate (GthImageViewerTool *base,
				GtkAllocation      *allocation)
{
	update_preview_image_area (GTH_PREVIEW_TOOL (base));
}


static void
gth_preview_tool_map (GthImageViewerTool *base)
{
	/* void */
}


static void
gth_preview_tool_unmap (GthImageViewerTool *base)
{
	/* void */
}


static void
gth_preview_tool_draw (GthImageViewerTool *base,
		       cairo_t            *cr)
{
	GthPreviewTool *self = GTH_PREVIEW_TOOL (base);
	GtkAllocation   allocation;

  	/* background */

	cairo_save (cr);
	gtk_widget_get_allocation (GTK_WIDGET (self->priv->viewer), &allocation);
	cairo_rectangle (cr, 0, 0, allocation.width, allocation.height);

  	cairo_set_source_rgba (cr,
  			       self->priv->background_color.red,
  			       self->priv->background_color.green,
  			       self->priv->background_color.blue,
  			       self->priv->background_color.alpha);
	cairo_fill (cr);
	cairo_restore (cr);

	/* image */

	cairo_save (cr);

	if (self->priv->preview_image != NULL) {
		cairo_set_antialias (cr, CAIRO_ANTIALIAS_NONE);
		cairo_set_source_surface (cr, self->priv->preview_image,
					  self->priv->preview_image_area.x,
					  self->priv->preview_image_area.y);
		cairo_pattern_set_filter (cairo_get_source (cr), CAIRO_FILTER_FAST);
		cairo_rectangle (cr,
				 self->priv->preview_image_area.x,
				 self->priv->preview_image_area.y,
				 self->priv->preview_image_area.width,
				 self->priv->preview_image_area.height);
		cairo_fill (cr);
	}

  	cairo_restore (cr);
}


static gboolean
gth_preview_tool_button_release (GthImageViewerTool *base,
				 GdkEventButton     *event)
{
	/* void */

	return FALSE;
}


static gboolean
gth_preview_tool_button_press (GthImageViewerTool *base,
			       GdkEventButton     *event)
{
	/* void */

	return FALSE;
}


static gboolean
gth_preview_tool_motion_notify (GthImageViewerTool *base,
				GdkEventMotion     *event)
{
	/* void */

	return FALSE;
}


static void
gth_preview_tool_image_changed (GthImageViewerTool *base)
{
	/* void */
}


static void
gth_preview_tool_zoom_changed (GthImageViewerTool *base)
{
	/* void */
}


static void
gth_preview_tool_finalize (GObject *object)
{
	GthPreviewTool *self;

	g_return_if_fail (object != NULL);
	g_return_if_fail (GTH_IS_PREVIEW_TOOL (object));

	self = (GthPreviewTool *) object;
	cairo_surface_destroy (self->priv->preview_image);

	/* Chain up */
	G_OBJECT_CLASS (gth_preview_tool_parent_class)->finalize (object);
}


static void
gth_preview_tool_class_init (GthPreviewToolClass *class)
{
	GObjectClass *gobject_class;

	gobject_class = (GObjectClass*) class;
	gobject_class->finalize = gth_preview_tool_finalize;
}


static void
gth_preview_tool_gth_image_tool_interface_init (GthImageViewerToolInterface *iface)
{
	iface->set_viewer = gth_preview_tool_set_viewer;
	iface->unset_viewer = gth_preview_tool_unset_viewer;
	iface->realize = gth_preview_tool_realize;
	iface->unrealize = gth_preview_tool_unrealize;
	iface->size_allocate = gth_preview_tool_size_allocate;
	iface->map = gth_preview_tool_map;
	iface->unmap = gth_preview_tool_unmap;
	iface->draw = gth_preview_tool_draw;
	iface->button_press = gth_preview_tool_button_press;
	iface->button_release = gth_preview_tool_button_release;
	iface->motion_notify = gth_preview_tool_motion_notify;
	iface->image_changed = gth_preview_tool_image_changed;
	iface->zoom_changed = gth_preview_tool_zoom_changed;
}


static void
gth_preview_tool_init (GthPreviewTool *self)
{
	self->priv = gth_preview_tool_get_instance_private (self);
	self->priv->preview_image = NULL;
	self->priv->background_color.red = 0.2;
	self->priv->background_color.green = 0.2;
	self->priv->background_color.blue = 0.2;
	self->priv->background_color.alpha = 1.0;
}


GthImageViewerTool *
gth_preview_tool_new (void)
{
	return GTH_IMAGE_VIEWER_TOOL ( g_object_new (GTH_TYPE_PREVIEW_TOOL, NULL));
}


void
gth_preview_tool_set_image (GthPreviewTool  *self,
			    cairo_surface_t *modified)
{
	_cairo_clear_surface (&self->priv->preview_image);
	if (modified != NULL) {
		self->priv->preview_image = cairo_surface_reference (modified);
		update_preview_image_area (self);
	}

	if (self->priv->viewer)
		gtk_widget_queue_draw (GTK_WIDGET (self->priv->viewer));
}


cairo_surface_t *
gth_preview_tool_get_image (GthPreviewTool *self)
{
	return self->priv->preview_image;
}