]> www.fi.muni.cz Git - evince.git/blob - libview/ev-loading-window.c
[libview] Add loading window to show loading progress information
[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
37         gint       x;
38         gint       y;
39         gint       width;
40         gint       height;
41 };
42
43 struct _EvLoadingWindowClass {
44         GtkWindowClass base_class;
45 };
46
47 G_DEFINE_TYPE (EvLoadingWindow, ev_loading_window, GTK_TYPE_WINDOW)
48
49 static void
50 ev_loading_window_set_property (GObject      *object,
51                                 guint         prop_id,
52                                 const GValue *value,
53                                 GParamSpec   *pspec)
54 {
55         EvLoadingWindow *window = EV_LOADING_WINDOW (object);
56
57         switch (prop_id) {
58         case PROP_PARENT:
59                 window->parent = g_value_get_object (value);
60                 break;
61         default:
62                 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
63         }
64 }
65
66 static void
67 ev_loading_window_init (EvLoadingWindow *window)
68 {
69         GtkWindow   *gtk_window = GTK_WINDOW (window);
70         GtkWidget   *widget = GTK_WIDGET (window);
71         GtkWidget   *hbox;
72         GtkWidget   *spinner;
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         spinner = gtk_spinner_new ();
83         gtk_spinner_start (GTK_SPINNER (spinner));
84         gtk_box_pack_start (GTK_BOX (hbox), spinner, FALSE, FALSE, 0);
85         gtk_widget_show (spinner);
86
87         label = gtk_label_new (loading_text);
88         gtk_box_pack_start (GTK_BOX (hbox), label, FALSE, FALSE, 0);
89         gtk_widget_show (label);
90
91         gtk_container_add (GTK_CONTAINER (window), hbox);
92         gtk_widget_show (hbox);
93
94         gtk_widget_set_app_paintable (widget, TRUE);
95
96         gtk_container_set_border_width (GTK_CONTAINER (window), 10);
97
98         gtk_window_set_accept_focus (gtk_window, FALSE);
99         gtk_window_set_focus_on_map (gtk_window, FALSE);
100         gtk_window_set_decorated (gtk_window, FALSE);
101         gtk_window_set_skip_taskbar_hint (gtk_window, TRUE);
102         gtk_window_set_skip_pager_hint (gtk_window, TRUE);
103         gtk_window_set_resizable (gtk_window, FALSE);
104
105         style = gtk_widget_get_style (widget);
106         if (!gtk_style_lookup_color (style, fg_color_name, &fg) ||
107             !gtk_style_lookup_color (style, bg_color_name, &bg)) {
108                 fg.pixel = 0;
109                 fg.red = 0xb800;
110                 fg.green = 0xad00;
111                 fg.blue = 0x9d00;
112
113                 bg.pixel = 0;
114                 bg.red = 0xff00;
115                 bg.green = 0xff00;
116                 bg.blue = 0xbf00;
117         }
118
119         if (!gdk_color_equal (&bg, &style->bg[GTK_STATE_NORMAL]))
120                 gtk_widget_modify_bg (widget, GTK_STATE_NORMAL, &bg);
121         if (!gdk_color_equal (&fg, &style->fg[GTK_STATE_NORMAL]))
122                 gtk_widget_modify_fg (widget, GTK_STATE_NORMAL, &fg);
123 }
124
125 static GObject *
126 ev_loading_window_constructor (GType                  type,
127                                guint                  n_construct_properties,
128                                GObjectConstructParam *construct_params)
129 {
130         GObject         *object;
131         EvLoadingWindow *window;
132         GtkWindow       *gtk_window;
133
134         object = G_OBJECT_CLASS (ev_loading_window_parent_class)->constructor (type,
135                                                                                n_construct_properties,
136                                                                                construct_params);
137         window = EV_LOADING_WINDOW (object);
138         gtk_window = GTK_WINDOW (window);
139
140         gtk_window_set_transient_for (gtk_window, window->parent);
141         gtk_window_set_destroy_with_parent (gtk_window, TRUE);
142
143         return object;
144 }
145
146 static void
147 _cairo_rounded_rectangle (cairo_t *cr,
148                           gint     width,
149                           gint     height,
150                           gdouble  radius)
151 {
152         cairo_move_to (cr, radius, 0);
153         cairo_line_to (cr, width - radius, 0);
154         cairo_curve_to (cr,
155                         width, 0,
156                         width, 0,
157                         width,
158                         radius);
159         cairo_line_to (cr, width, height - radius);
160         cairo_curve_to (cr,
161                         width,height,
162                         width, height,
163                         width - radius,
164                         height);
165         cairo_line_to (cr, radius, height);
166         cairo_curve_to (cr,
167                         0, height,
168                         0, height,
169                         0, height - radius);
170         cairo_line_to (cr, 0, radius);
171         cairo_curve_to (cr,
172                         0, 0,
173                         0, 0,
174                         radius, 0);
175 }
176
177 static void
178 ev_loading_window_size_allocate (GtkWidget      *widget,
179                                  GtkAllocation  *allocation)
180 {
181         EvLoadingWindow *window = EV_LOADING_WINDOW (widget);
182         GdkPixmap       *mask;
183         cairo_t         *cr;
184         double           r;
185
186         GTK_WIDGET_CLASS (ev_loading_window_parent_class)->size_allocate (widget, allocation);
187
188         if (allocation->width == window->width && allocation->height == window->height)
189                 return;
190
191         window->width = allocation->width;
192         window->height = allocation->height;
193
194         mask = gdk_pixmap_new (NULL, window->width, window->height, 1);
195         cr = gdk_cairo_create (GDK_DRAWABLE (mask));
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         gtk_widget_shape_combine_mask (widget, mask, 0, 0);
211         g_object_unref (mask);
212 }
213
214 static void
215 ev_loading_window_hide (GtkWidget *widget)
216 {
217         EvLoadingWindow *window = EV_LOADING_WINDOW (widget);
218
219         window->x = window->y = 0;
220
221         GTK_WIDGET_CLASS (ev_loading_window_parent_class)->hide (widget);
222 }
223
224 static void
225 ev_loading_window_class_init (EvLoadingWindowClass *klass)
226 {
227         GObjectClass   *g_object_class = G_OBJECT_CLASS (klass);
228         GtkWidgetClass *gtk_widget_class = GTK_WIDGET_CLASS (klass);
229
230         g_object_class->constructor = ev_loading_window_constructor;
231         g_object_class->set_property = ev_loading_window_set_property;
232
233         gtk_widget_class->size_allocate = ev_loading_window_size_allocate;
234         gtk_widget_class->hide = ev_loading_window_hide;
235
236         g_object_class_install_property (g_object_class,
237                                          PROP_PARENT,
238                                          g_param_spec_object ("parent",
239                                                               "Parent",
240                                                               "The parent window",
241                                                               GTK_TYPE_WINDOW,
242                                                               G_PARAM_WRITABLE |
243                                                               G_PARAM_CONSTRUCT_ONLY));
244 }
245
246 /* Public methods */
247 GtkWidget *
248 ev_loading_window_new (GtkWindow *parent)
249 {
250         GtkWidget *window;
251
252         g_return_val_if_fail (GTK_IS_WINDOW (parent), NULL);
253
254         window = g_object_new (EV_TYPE_LOADING_WINDOW,
255                                "parent", parent,
256                                NULL);
257         return window;
258 }
259
260 void
261 ev_loading_window_get_size (EvLoadingWindow *window,
262                             gint            *width,
263                             gint            *height)
264 {
265         if (width) *width = window->width;
266         if (height) *height = window->height;
267 }
268
269 void
270 ev_loading_window_move (EvLoadingWindow *window,
271                         gint             x,
272                         gint             y)
273 {
274         if (x == window->x && y == window->y)
275                 return;
276
277         window->x = x;
278         window->y = y;
279         gtk_window_move (GTK_WINDOW (window), x, y);
280 }