aboutsummaryrefslogtreecommitdiff
path: root/fastiv-view.c
blob: 9d00e637e718f9a7138a863d85f1f9fc66c7293a (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
//
// fastiv-view.c: fast image viewer - view widget
//
// Copyright (c) 2021, Přemysl Eric Janouch <p@janouch.name>
//
// Permission to use, copy, modify, and/or distribute this software for any
// purpose with or without fee is hereby granted.
//
// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
// WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
// MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
// SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
// WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
// OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
// CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
//

#include <math.h>

#include "fastiv-view.h"

struct _FastivView {
	GtkWidget parent_instance;
	cairo_surface_t *surface;
	// TODO(p): Zoom-to-fit indication.
	double scale;
};

G_DEFINE_TYPE(FastivView, fastiv_view, GTK_TYPE_WIDGET)

static int
get_display_width(FastivView *self)
{
	if (!self->surface)
		return 0;

	return ceil(cairo_image_surface_get_width(self->surface) * self->scale);
}

static int
get_display_height(FastivView *self)
{
	if (!self->surface)
		return 0;

	return ceil(cairo_image_surface_get_height(self->surface) * self->scale);
}

static void
fastiv_view_finalize(GObject *gobject)
{
	FastivView *self = FASTIV_VIEW(gobject);
	cairo_surface_destroy(self->surface);

	G_OBJECT_CLASS(fastiv_view_parent_class)->finalize(gobject);
}

static void
fastiv_view_get_preferred_height(GtkWidget *widget,
	gint *minimum, gint *natural)
{
	*minimum = 0;
	*natural = 0;

	FastivView *self = FASTIV_VIEW(widget);
	*natural = get_display_height(self);
}

static void
fastiv_view_get_preferred_width(GtkWidget *widget,
	gint *minimum, gint *natural)
{
	*minimum = 0;
	*natural = 0;

	FastivView *self = FASTIV_VIEW(widget);
	*natural = get_display_width(self);
}

static gboolean
fastiv_view_draw(GtkWidget *widget, cairo_t *cr)
{
	FastivView *self = FASTIV_VIEW(widget);
	if (!self->surface)
		return TRUE;

	// TODO(p): Make this adjustable later.
	cairo_set_source_rgb(cr, 0, 0, 0);
	cairo_paint(cr);

	GtkAllocation allocation;
	gtk_widget_get_allocation(widget, &allocation);

	int w = get_display_width(self);
	int h = get_display_height(self);

	double x = 0;
	double y = 0;
	if (w < allocation.width)
		x = (allocation.width - w) / 2;
	if (h < allocation.height)
		y = (allocation.height - h) / 2;

	cairo_scale(cr, self->scale, self->scale);
	cairo_set_source_surface(cr, self->surface,
		x / self->scale, y / self->scale);

	cairo_paint(cr);
	return TRUE;
}

static void
fastiv_view_class_init(FastivViewClass *klass)
{
	GObjectClass *object_class = G_OBJECT_CLASS(klass);
	object_class->finalize = fastiv_view_finalize;

	GtkWidgetClass *widget_class = GTK_WIDGET_CLASS(klass);
	widget_class->get_preferred_height = fastiv_view_get_preferred_height;
	widget_class->get_preferred_width = fastiv_view_get_preferred_width;
	widget_class->draw = fastiv_view_draw;
}

static void
fastiv_view_init(FastivView *self)
{
	self->scale = 1.0;

	gtk_widget_set_has_window(GTK_WIDGET(self), FALSE);
}

// --- Picture loading ---------------------------------------------------------

// TODO(p): Progressive picture loading, or at least async/cancellable.
gboolean
fastiv_view_open(FastivView *self, const gchar *path, GError **error)
{
	cairo_surface_t *surface = fastiv_io_open(path, error);
	if (!surface)
		return FALSE;
	if (self->surface)
		cairo_surface_destroy(self->surface);

	self->surface = surface;
	gtk_widget_queue_resize(GTK_WIDGET(self));
	return TRUE;
}