summaryrefslogtreecommitdiff
path: root/libview/ev-loading-window.c
blob: feade15599cd885d675b8e17b82fec226dc80726 (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
/* ev-loading-window.c
 *  this file is part of atril, a mate document viewer
 *
 * Copyright (C) 2010 Carlos Garcia Campos <carlosgc@gnome.org>
 *
 * Atril 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.
 *
 * Atril 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 */

#include "config.h"

#include <string.h>
#include <glib/gi18n.h>
#include "ev-loading-window.h"

enum {
	PROP_0,
	PROP_PARENT
};

struct _EvLoadingWindow {
	GtkWindow  base_instance;

	GtkWindow *parent;
	GtkWidget *spinner;

	gint       x;
	gint       y;
	gint       width;
	gint       height;
};

struct _EvLoadingWindowClass {
	GtkWindowClass base_class;
};

G_DEFINE_TYPE (EvLoadingWindow, ev_loading_window, GTK_TYPE_WINDOW)

static void
ev_loading_window_set_property (GObject      *object,
				guint         prop_id,
				const GValue *value,
				GParamSpec   *pspec)
{
	EvLoadingWindow *window = EV_LOADING_WINDOW (object);

	switch (prop_id) {
	case PROP_PARENT:
		window->parent = g_value_get_object (value);
		break;
	default:
		G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
	}
}

static void
ev_loading_window_init (EvLoadingWindow *window)
{
	GtkWindow   *gtk_window = GTK_WINDOW (window);
	GtkWidget   *widget = GTK_WIDGET (window);
	GtkWidget   *hbox;
	GtkWidget   *label;
#if GTK_CHECK_VERSION (3, 0, 0)
	GtkStyleContext *context;
	GdkRGBA    fg, bg;
#else
	GtkStyle    *style;
	GdkColor    fg, bg;
#endif
	const gchar *loading_text = _("Loading…");
	const gchar *fg_color_name = "info_fg_color";
	const gchar *bg_color_name = "info_bg_color";

	hbox = gtk_hbox_new (FALSE, 12);

	window->spinner = gtk_spinner_new ();
	gtk_box_pack_start (GTK_BOX (hbox), window->spinner, FALSE, FALSE, 0);
	gtk_widget_show (window->spinner);

	label = gtk_label_new (loading_text);
	gtk_box_pack_start (GTK_BOX (hbox), label, FALSE, FALSE, 0);
	gtk_widget_show (label);

	gtk_container_add (GTK_CONTAINER (window), hbox);
	gtk_widget_show (hbox);

	gtk_widget_set_app_paintable (widget, TRUE);

	gtk_container_set_border_width (GTK_CONTAINER (window), 10);

	gtk_window_set_type_hint (gtk_window, GDK_WINDOW_TYPE_HINT_NOTIFICATION);
	gtk_window_set_accept_focus (gtk_window, FALSE);
	gtk_window_set_decorated (gtk_window, FALSE);
	gtk_window_set_resizable (gtk_window, FALSE);

#if GTK_CHECK_VERSION (3, 0, 0)
	context = gtk_widget_get_style_context (widget);
	if (!gtk_style_context_lookup_color (context, fg_color_name, &fg) ||
	    !gtk_style_context_lookup_color (context, bg_color_name, &bg)) {
		fg.red = 0.7;
		fg.green = 0.67;
		fg.blue = 0.63;
		fg.alpha = 1.0;

		bg.red = 0.99;
		bg.green = 0.99;
		bg.blue = 0.71;
		bg.alpha = 1.0;
	}

        gtk_widget_override_background_color (widget, GTK_STATE_NORMAL, &bg);
        gtk_widget_override_color (widget, GTK_STATE_NORMAL, &fg);
}
#else
	style = gtk_widget_get_style (widget);
	if (!gtk_style_lookup_color (style, fg_color_name, &fg) ||
	    !gtk_style_lookup_color (style, bg_color_name, &bg)) {
		fg.pixel = 0;
		fg.red = 0xb800;
		fg.green = 0xad00;
		fg.blue = 0x9d00;

		bg.pixel = 0;
		bg.red = 0xff00;
		bg.green = 0xff00;
		bg.blue = 0xbf00;
	}

	if (!gdk_color_equal (&bg, &style->bg[GTK_STATE_NORMAL]))
		gtk_widget_modify_bg (widget, GTK_STATE_NORMAL, &bg);
	if (!gdk_color_equal (&fg, &style->fg[GTK_STATE_NORMAL]))
		gtk_widget_modify_fg (widget, GTK_STATE_NORMAL, &fg);
}
#endif

static GObject *
ev_loading_window_constructor (GType                  type,
			       guint                  n_construct_properties,
			       GObjectConstructParam *construct_params)
{
	GObject         *object;
	EvLoadingWindow *window;
	GtkWindow       *gtk_window;

	object = G_OBJECT_CLASS (ev_loading_window_parent_class)->constructor (type,
									       n_construct_properties,
									       construct_params);
	window = EV_LOADING_WINDOW (object);
	gtk_window = GTK_WINDOW (window);

	gtk_window_set_transient_for (gtk_window, window->parent);
	gtk_window_set_destroy_with_parent (gtk_window, TRUE);

	return object;
}

static void
_cairo_rounded_rectangle (cairo_t *cr,
			  gint     width,
			  gint     height,
			  gdouble  radius)
{
	cairo_move_to (cr, radius, 0);
	cairo_line_to (cr, width - radius, 0);
	cairo_curve_to (cr,
			width, 0,
			width, 0,
			width,
			radius);
	cairo_line_to (cr, width, height - radius);
	cairo_curve_to (cr,
			width,height,
			width, height,
			width - radius,
			height);
	cairo_line_to (cr, radius, height);
	cairo_curve_to (cr,
			0, height,
			0, height,
			0, height - radius);
	cairo_line_to (cr, 0, radius);
	cairo_curve_to (cr,
			0, 0,
			0, 0,
			radius, 0);
}

static void
ev_loading_window_size_allocate (GtkWidget      *widget,
				 GtkAllocation  *allocation)
{
	EvLoadingWindow *window = EV_LOADING_WINDOW (widget);
#if GTK_CHECK_VERSION (3, 0, 0)
	cairo_surface_t *surface;
	cairo_region_t *shape;
#else
	GdkPixmap       *mask;
#endif
	cairo_t         *cr;
	double           r;

	GTK_WIDGET_CLASS (ev_loading_window_parent_class)->size_allocate (widget, allocation);

	if (allocation->width == window->width && allocation->height == window->height)
		return;

	window->width = allocation->width;
	window->height = allocation->height;

#if GTK_CHECK_VERSION (3, 0, 0)
	surface = cairo_image_surface_create (CAIRO_FORMAT_A8, window->width, window->height);
	cr = cairo_create (surface);
#else
	mask = gdk_pixmap_new (NULL, window->width, window->height, 1);
	cr = gdk_cairo_create (GDK_DRAWABLE (mask));
#endif

	cairo_save (cr);
	cairo_rectangle (cr, 0, 0, window->width, window->height);
	cairo_set_operator (cr, CAIRO_OPERATOR_CLEAR);
	cairo_fill (cr);
	cairo_restore (cr);

	cairo_set_source_rgb (cr, 1., 1., 1.);
	r = MIN (window->width, window->height) / 2.;
	_cairo_rounded_rectangle (cr, window->width, window->height, r);
	cairo_fill (cr);

	cairo_destroy (cr);

#if GTK_CHECK_VERSION (3, 0, 0)
	shape = gdk_cairo_region_create_from_surface (surface);
	cairo_surface_destroy (surface);

	gtk_widget_shape_combine_region (widget, shape);
	cairo_region_destroy (shape);
#else
	gtk_widget_shape_combine_mask (widget, mask, 0, 0);
	g_object_unref (mask);
#endif
}

static void
ev_loading_window_hide (GtkWidget *widget)
{
	EvLoadingWindow *window = EV_LOADING_WINDOW (widget);

	window->x = window->y = 0;

	gtk_spinner_stop (GTK_SPINNER (window->spinner));

	GTK_WIDGET_CLASS (ev_loading_window_parent_class)->hide (widget);
}

static void
ev_loading_window_show (GtkWidget *widget)
{
	EvLoadingWindow *window = EV_LOADING_WINDOW (widget);

	gtk_spinner_start (GTK_SPINNER (window->spinner));

	GTK_WIDGET_CLASS (ev_loading_window_parent_class)->show (widget);
}

static void
ev_loading_window_class_init (EvLoadingWindowClass *klass)
{
	GObjectClass   *g_object_class = G_OBJECT_CLASS (klass);
	GtkWidgetClass *gtk_widget_class = GTK_WIDGET_CLASS (klass);

	g_object_class->constructor = ev_loading_window_constructor;
	g_object_class->set_property = ev_loading_window_set_property;

	gtk_widget_class->size_allocate = ev_loading_window_size_allocate;
	gtk_widget_class->show = ev_loading_window_show;
	gtk_widget_class->hide = ev_loading_window_hide;

	g_object_class_install_property (g_object_class,
					 PROP_PARENT,
					 g_param_spec_object ("parent",
							      "Parent",
							      "The parent window",
							      GTK_TYPE_WINDOW,
							      G_PARAM_WRITABLE |
							      G_PARAM_CONSTRUCT_ONLY));
}

/* Public methods */
GtkWidget *
ev_loading_window_new (GtkWindow *parent)
{
	GtkWidget *window;

	g_return_val_if_fail (GTK_IS_WINDOW (parent), NULL);

	window = g_object_new (EV_TYPE_LOADING_WINDOW,
                               "type", GTK_WINDOW_POPUP,
			       "parent", parent,
			       NULL);
	return window;
}

void
ev_loading_window_get_size (EvLoadingWindow *window,
			    gint            *width,
			    gint            *height)
{
	if (width) *width = window->width;
	if (height) *height = window->height;
}

void
ev_loading_window_move (EvLoadingWindow *window,
			gint             x,
			gint             y)
{
	if (x == window->x && y == window->y)
		return;

	window->x = x;
	window->y = y;
	gtk_window_move (GTK_WINDOW (window), x, y);
}