]> www.fi.muni.cz Git - evince.git/blob - libview/ev-loading-window.c
434beac990552be164e3870c61af9e78965ffc0a
[evince.git] / libview / ev-loading-window.c
1 /* ev-loading-window.c
2  *  this file is part of evince, a gnome document viewer
3  *
4  * Copyright (C) 2010 Carlos Garcia Campos <carlosgc@gnome.org>
5  *
6  * Evince is free software; you can redistribute it and/or modify it
7  * under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * Evince is distributed in the hope that it will be useful, but
12  * WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19  */
20
21 #include "config.h"
22
23 #include <string.h>
24 #include <glib/gi18n.h>
25 #include "ev-loading-window.h"
26
27 enum {
28         PROP_0,
29         PROP_PARENT
30 };
31
32 struct _EvLoadingWindow {
33         GtkWindow  base_instance;
34
35         GtkWindow *parent;
36         GtkWidget *spinner;
37
38         gint       x;
39         gint       y;
40         gint       width;
41         gint       height;
42 };
43
44 struct _EvLoadingWindowClass {
45         GtkWindowClass base_class;
46 };
47
48 G_DEFINE_TYPE (EvLoadingWindow, ev_loading_window, GTK_TYPE_WINDOW)
49
50 static void
51 ev_loading_window_set_property (GObject      *object,
52                                 guint         prop_id,
53                                 const GValue *value,
54                                 GParamSpec   *pspec)
55 {
56         EvLoadingWindow *window = EV_LOADING_WINDOW (object);
57
58         switch (prop_id) {
59         case PROP_PARENT:
60                 window->parent = g_value_get_object (value);
61                 break;
62         default:
63                 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
64         }
65 }
66
67 static void
68 ev_loading_window_init (EvLoadingWindow *window)
69 {
70         GtkWindow   *gtk_window = GTK_WINDOW (window);
71         GtkWidget   *widget = GTK_WIDGET (window);
72         GtkWidget   *hbox;
73         GtkWidget   *label;
74         GtkStyle    *style;
75         GdkColor    fg, bg;
76         const gchar *loading_text = _("Loading…");
77         const gchar *fg_color_name = "info_fg_color";
78         const gchar *bg_color_name = "info_bg_color";
79
80         hbox = gtk_hbox_new (FALSE, 12);
81
82         window->spinner = gtk_spinner_new ();
83         gtk_box_pack_start (GTK_BOX (hbox), window->spinner, FALSE, FALSE, 0);
84         gtk_widget_show (window->spinner);
85
86         label = gtk_label_new (loading_text);
87         gtk_box_pack_start (GTK_BOX (hbox), label, FALSE, FALSE, 0);
88         gtk_widget_show (label);
89
90         gtk_container_add (GTK_CONTAINER (window), hbox);
91         gtk_widget_show (hbox);
92
93         gtk_widget_set_app_paintable (widget, TRUE);
94
95         gtk_container_set_border_width (GTK_CONTAINER (window), 10);
96
97         gtk_window_set_type_hint (gtk_window, GDK_WINDOW_TYPE_HINT_NOTIFICATION);
98         gtk_window_set_accept_focus (gtk_window, FALSE);
99         gtk_window_set_decorated (gtk_window, FALSE);
100         gtk_window_set_resizable (gtk_window, FALSE);
101
102         style = gtk_widget_get_style (widget);
103         if (!gtk_style_lookup_color (style, fg_color_name, &fg) ||
104             !gtk_style_lookup_color (style, bg_color_name, &bg)) {
105                 fg.pixel = 0;
106                 fg.red = 0xb800;
107                 fg.green = 0xad00;
108                 fg.blue = 0x9d00;
109
110                 bg.pixel = 0;
111                 bg.red = 0xff00;
112                 bg.green = 0xff00;
113                 bg.blue = 0xbf00;
114         }
115
116         if (!gdk_color_equal (&bg, &style->bg[GTK_STATE_NORMAL]))
117                 gtk_widget_modify_bg (widget, GTK_STATE_NORMAL, &bg);
118         if (!gdk_color_equal (&fg, &style->fg[GTK_STATE_NORMAL]))
119                 gtk_widget_modify_fg (widget, GTK_STATE_NORMAL, &fg);
120 }
121
122 static GObject *
123 ev_loading_window_constructor (GType                  type,
124                                guint                  n_construct_properties,
125                                GObjectConstructParam *construct_params)
126 {
127         GObject         *object;
128         EvLoadingWindow *window;
129         GtkWindow       *gtk_window;
130
131         object = G_OBJECT_CLASS (ev_loading_window_parent_class)->constructor (type,
132                                                                                n_construct_properties,
133                                                                                construct_params);
134         window = EV_LOADING_WINDOW (object);
135         gtk_window = GTK_WINDOW (window);
136
137         gtk_window_set_transient_for (gtk_window, window->parent);
138         gtk_window_set_destroy_with_parent (gtk_window, TRUE);
139
140         return object;
141 }
142
143 static void
144 _cairo_rounded_rectangle (cairo_t *cr,
145                           gint     width,
146                           gint     height,
147                           gdouble  radius)
148 {
149         cairo_move_to (cr, radius, 0);
150         cairo_line_to (cr, width - radius, 0);
151         cairo_curve_to (cr,
152                         width, 0,
153                         width, 0,
154                         width,
155                         radius);
156         cairo_line_to (cr, width, height - radius);
157         cairo_curve_to (cr,
158                         width,height,
159                         width, height,
160                         width - radius,
161                         height);
162         cairo_line_to (cr, radius, height);
163         cairo_curve_to (cr,
164                         0, height,
165                         0, height,
166                         0, height - radius);
167         cairo_line_to (cr, 0, radius);
168         cairo_curve_to (cr,
169                         0, 0,
170                         0, 0,
171                         radius, 0);
172 }
173
174 static void
175 ev_loading_window_size_allocate (GtkWidget      *widget,
176                                  GtkAllocation  *allocation)
177 {
178         EvLoadingWindow *window = EV_LOADING_WINDOW (widget);
179         cairo_surface_t *surface;
180         cairo_region_t  *shape;
181         cairo_t         *cr;
182         double           r;
183
184         GTK_WIDGET_CLASS (ev_loading_window_parent_class)->size_allocate (widget, allocation);
185
186         if (allocation->width == window->width && allocation->height == window->height)
187                 return;
188
189         window->width = allocation->width;
190         window->height = allocation->height;
191
192         surface = cairo_image_surface_create (CAIRO_FORMAT_A8,
193                                               window->width,
194                                               window->height);
195         cr = cairo_create (surface);
196
197         cairo_save (cr);
198         cairo_rectangle (cr, 0, 0, window->width, window->height);
199         cairo_set_operator (cr, CAIRO_OPERATOR_CLEAR);
200         cairo_fill (cr);
201         cairo_restore (cr);
202
203         cairo_set_source_rgb (cr, 1., 1., 1.);
204         r = MIN (window->width, window->height) / 2.;
205         _cairo_rounded_rectangle (cr, window->width, window->height, r);
206         cairo_fill (cr);
207
208         cairo_destroy (cr);
209
210         shape = gdk_cairo_region_create_from_surface (surface);
211         cairo_surface_destroy (surface);
212
213         gtk_widget_shape_combine_region (widget, shape);
214         cairo_region_destroy (shape);
215 }
216
217 static void
218 ev_loading_window_hide (GtkWidget *widget)
219 {
220         EvLoadingWindow *window = EV_LOADING_WINDOW (widget);
221
222         window->x = window->y = 0;
223
224         gtk_spinner_stop (GTK_SPINNER (window->spinner));
225
226         GTK_WIDGET_CLASS (ev_loading_window_parent_class)->hide (widget);
227 }
228
229 static void
230 ev_loading_window_show (GtkWidget *widget)
231 {
232         EvLoadingWindow *window = EV_LOADING_WINDOW (widget);
233
234         gtk_spinner_start (GTK_SPINNER (window->spinner));
235
236         GTK_WIDGET_CLASS (ev_loading_window_parent_class)->show (widget);
237 }
238
239 static void
240 ev_loading_window_class_init (EvLoadingWindowClass *klass)
241 {
242         GObjectClass   *g_object_class = G_OBJECT_CLASS (klass);
243         GtkWidgetClass *gtk_widget_class = GTK_WIDGET_CLASS (klass);
244
245         g_object_class->constructor = ev_loading_window_constructor;
246         g_object_class->set_property = ev_loading_window_set_property;
247
248         gtk_widget_class->size_allocate = ev_loading_window_size_allocate;
249         gtk_widget_class->show = ev_loading_window_show;
250         gtk_widget_class->hide = ev_loading_window_hide;
251
252         g_object_class_install_property (g_object_class,
253                                          PROP_PARENT,
254                                          g_param_spec_object ("parent",
255                                                               "Parent",
256                                                               "The parent window",
257                                                               GTK_TYPE_WINDOW,
258                                                               G_PARAM_WRITABLE |
259                                                               G_PARAM_CONSTRUCT_ONLY));
260 }
261
262 /* Public methods */
263 GtkWidget *
264 ev_loading_window_new (GtkWindow *parent)
265 {
266         GtkWidget *window;
267
268         g_return_val_if_fail (GTK_IS_WINDOW (parent), NULL);
269
270         window = g_object_new (EV_TYPE_LOADING_WINDOW,
271                                "parent", parent,
272                                NULL);
273         return window;
274 }
275
276 void
277 ev_loading_window_get_size (EvLoadingWindow *window,
278                             gint            *width,
279                             gint            *height)
280 {
281         if (width) *width = window->width;
282         if (height) *height = window->height;
283 }
284
285 void
286 ev_loading_window_move (EvLoadingWindow *window,
287                         gint             x,
288                         gint             y)
289 {
290         if (x == window->x && y == window->y)
291                 return;
292
293         window->x = x;
294         window->y = y;
295         gtk_window_move (GTK_WINDOW (window), x, y);
296 }