2 * this file is part of evince, a gnome document viewer
4 * Copyright (C) 2008 Carlos Garcia Campos <carlosgc@gnome.org>
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.
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.
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.
23 #include <glib/gi18n.h>
24 #include "gimpcellrenderertoggle.h"
26 #include "ev-document-layers.h"
27 #include "ev-sidebar-page.h"
29 #include "ev-job-scheduler.h"
30 #include "ev-stock-icons.h"
31 #include "ev-sidebar-layers.h"
33 struct _EvSidebarLayersPrivate {
34 GtkTreeView *tree_view;
46 LAYERS_VISIBILITY_CHANGED,
50 static void ev_sidebar_layers_page_iface_init (EvSidebarPageInterface *iface);
51 static void job_finished_callback (EvJobLayers *job,
52 EvSidebarLayers *sidebar_layers);
54 static guint signals[N_SIGNALS];
56 G_DEFINE_TYPE_EXTENDED (EvSidebarLayers,
60 G_IMPLEMENT_INTERFACE (EV_TYPE_SIDEBAR_PAGE,
61 ev_sidebar_layers_page_iface_init))
63 #define EV_SIDEBAR_LAYERS_GET_PRIVATE(object) \
64 (G_TYPE_INSTANCE_GET_PRIVATE ((object), EV_TYPE_SIDEBAR_LAYERS, EvSidebarLayersPrivate))
67 ev_sidebar_layers_dispose (GObject *object)
69 EvSidebarLayers *sidebar = EV_SIDEBAR_LAYERS (object);
71 if (sidebar->priv->job) {
72 g_signal_handlers_disconnect_by_func (sidebar->priv->job,
73 job_finished_callback,
75 ev_job_cancel (sidebar->priv->job);
76 g_object_unref (sidebar->priv->job);
77 sidebar->priv->job = NULL;
80 if (sidebar->priv->document) {
81 g_object_unref (sidebar->priv->document);
82 sidebar->priv->document = NULL;
85 G_OBJECT_CLASS (ev_sidebar_layers_parent_class)->dispose (object);
89 ev_sidebar_layers_get_property (GObject *object,
94 EvSidebarLayers *ev_sidebar_layers;
96 ev_sidebar_layers = EV_SIDEBAR_LAYERS (object);
100 g_value_set_object (value, ev_sidebar_layers->priv->tree_view);
103 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
108 static GtkTreeModel *
109 ev_sidebar_layers_create_loading_model (void)
111 GtkTreeModel *retval;
115 /* Creates a fake model to indicate that we're loading */
116 retval = (GtkTreeModel *)gtk_list_store_new (EV_DOCUMENT_LAYERS_N_COLUMNS,
124 gtk_list_store_append (GTK_LIST_STORE (retval), &iter);
125 markup = g_strdup_printf ("<span size=\"larger\" style=\"italic\">%s</span>", _("Loading…"));
126 gtk_list_store_set (GTK_LIST_STORE (retval), &iter,
127 EV_DOCUMENT_LAYERS_COLUMN_TITLE, markup,
128 EV_DOCUMENT_LAYERS_COLUMN_VISIBLE, FALSE,
129 EV_DOCUMENT_LAYERS_COLUMN_ENABLED, TRUE,
130 EV_DOCUMENT_LAYERS_COLUMN_SHOWTOGGLE, FALSE,
131 EV_DOCUMENT_LAYERS_COLUMN_RBGROUP, -1,
132 EV_DOCUMENT_LAYERS_COLUMN_LAYER, NULL,
140 update_kids (GtkTreeModel *model,
145 if (gtk_tree_store_is_ancestor (GTK_TREE_STORE (model), parent, iter)) {
148 gtk_tree_model_get (model, parent,
149 EV_DOCUMENT_LAYERS_COLUMN_VISIBLE, &visible,
151 gtk_tree_store_set (GTK_TREE_STORE (model), iter,
152 EV_DOCUMENT_LAYERS_COLUMN_ENABLED, visible,
160 clear_rb_group (GtkTreeModel *model,
167 gtk_tree_model_get (model, iter,
168 EV_DOCUMENT_LAYERS_COLUMN_RBGROUP, &group,
171 if (group == *rb_group) {
172 gtk_tree_store_set (GTK_TREE_STORE (model), iter,
173 EV_DOCUMENT_LAYERS_COLUMN_VISIBLE, FALSE,
181 ev_sidebar_layers_visibility_changed (GtkCellRendererToggle *cell,
183 EvSidebarLayers *ev_layers)
191 model = gtk_tree_view_get_model (ev_layers->priv->tree_view);
193 path = gtk_tree_path_new_from_string (path_str);
194 gtk_tree_model_get_iter (model, &iter, path);
195 gtk_tree_model_get (model, &iter,
196 EV_DOCUMENT_LAYERS_COLUMN_VISIBLE, &visible,
197 EV_DOCUMENT_LAYERS_COLUMN_LAYER, &layer,
204 ev_document_layers_show_layer (EV_DOCUMENT_LAYERS (ev_layers->priv->document),
207 rb_group = ev_layer_get_rb_group (layer);
209 gtk_tree_model_foreach (model,
210 (GtkTreeModelForeachFunc)clear_rb_group,
214 ev_document_layers_hide_layer (EV_DOCUMENT_LAYERS (ev_layers->priv->document),
218 gtk_tree_store_set (GTK_TREE_STORE (model), &iter,
219 EV_DOCUMENT_LAYERS_COLUMN_VISIBLE, visible,
222 if (ev_layer_is_parent (layer)) {
223 gtk_tree_model_foreach (model,
224 (GtkTreeModelForeachFunc)update_kids,
228 gtk_tree_path_free (path);
230 g_signal_emit (ev_layers, signals[LAYERS_VISIBILITY_CHANGED], 0);
234 ev_sidebar_layers_create_tree_view (EvSidebarLayers *ev_layers)
236 GtkTreeView *tree_view;
237 GtkTreeViewColumn *column;
238 GtkCellRenderer *renderer;
240 tree_view = GTK_TREE_VIEW (gtk_tree_view_new ());
241 gtk_tree_view_set_headers_visible (tree_view, FALSE);
242 gtk_tree_selection_set_mode (gtk_tree_view_get_selection (tree_view),
246 column = gtk_tree_view_column_new ();
248 renderer = gimp_cell_renderer_toggle_new (EV_STOCK_VISIBLE);
249 gtk_tree_view_column_pack_start (column, renderer, FALSE);
250 gtk_tree_view_column_set_attributes (column, renderer,
251 "active", EV_DOCUMENT_LAYERS_COLUMN_VISIBLE,
252 "activatable", EV_DOCUMENT_LAYERS_COLUMN_ENABLED,
253 "visible", EV_DOCUMENT_LAYERS_COLUMN_SHOWTOGGLE,
254 "sensitive", EV_DOCUMENT_LAYERS_COLUMN_ENABLED,
256 g_object_set (G_OBJECT (renderer),
260 g_signal_connect (renderer, "toggled",
261 G_CALLBACK (ev_sidebar_layers_visibility_changed),
262 (gpointer)ev_layers);
264 renderer = gtk_cell_renderer_text_new ();
265 gtk_tree_view_column_pack_start (column, renderer, TRUE);
266 gtk_tree_view_column_set_attributes (column, renderer,
267 "markup", EV_DOCUMENT_LAYERS_COLUMN_TITLE,
268 "sensitive", EV_DOCUMENT_LAYERS_COLUMN_ENABLED,
270 g_object_set (G_OBJECT (renderer), "ellipsize", PANGO_ELLIPSIZE_END, NULL);
272 gtk_tree_view_append_column (tree_view, column);
278 ev_sidebar_layers_init (EvSidebarLayers *ev_layers)
283 ev_layers->priv = EV_SIDEBAR_LAYERS_GET_PRIVATE (ev_layers);
285 swindow = gtk_scrolled_window_new (NULL, NULL);
286 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (swindow),
288 GTK_POLICY_AUTOMATIC);
289 gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (swindow),
292 model = ev_sidebar_layers_create_loading_model ();
295 ev_layers->priv->tree_view = ev_sidebar_layers_create_tree_view (ev_layers);
296 gtk_tree_view_set_model (ev_layers->priv->tree_view, model);
297 g_object_unref (model);
299 gtk_container_add (GTK_CONTAINER (swindow),
300 GTK_WIDGET (ev_layers->priv->tree_view));
302 gtk_container_add (GTK_CONTAINER (ev_layers), swindow);
303 gtk_widget_show_all (GTK_WIDGET (ev_layers));
307 ev_sidebar_layers_class_init (EvSidebarLayersClass *ev_layers_class)
309 GObjectClass *g_object_class = G_OBJECT_CLASS (ev_layers_class);
311 g_object_class->get_property = ev_sidebar_layers_get_property;
312 g_object_class->dispose = ev_sidebar_layers_dispose;
314 g_type_class_add_private (g_object_class, sizeof (EvSidebarLayersPrivate));
316 g_object_class_override_property (g_object_class, PROP_WIDGET, "main-widget");
318 signals[LAYERS_VISIBILITY_CHANGED] =
319 g_signal_new ("layers_visibility_changed",
320 G_TYPE_FROM_CLASS (g_object_class),
321 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
322 G_STRUCT_OFFSET (EvSidebarLayersClass, layers_visibility_changed),
324 g_cclosure_marshal_VOID__VOID,
325 G_TYPE_NONE, 0, G_TYPE_NONE);
329 ev_sidebar_layers_new (void)
331 return GTK_WIDGET (g_object_new (EV_TYPE_SIDEBAR_LAYERS, NULL));
335 update_layers_state (GtkTreeModel *model,
337 EvDocumentLayers *document_layers)
341 GtkTreeIter child_iter;
344 gtk_tree_model_get (model, iter,
345 EV_DOCUMENT_LAYERS_COLUMN_VISIBLE, &visible,
346 EV_DOCUMENT_LAYERS_COLUMN_LAYER, &layer,
349 gboolean layer_visible;
351 layer_visible = ev_document_layers_layer_is_visible (document_layers, layer);
352 if (layer_visible != visible) {
353 gtk_tree_store_set (GTK_TREE_STORE (model), iter,
354 EV_DOCUMENT_LAYERS_COLUMN_VISIBLE, layer_visible,
359 if (gtk_tree_model_iter_children (model, &child_iter, iter))
360 update_layers_state (model, &child_iter, document_layers);
361 } while (gtk_tree_model_iter_next (model, iter));
365 ev_sidebar_layers_update_layers_state (EvSidebarLayers *sidebar_layers)
369 EvDocumentLayers *document_layers;
371 document_layers = EV_DOCUMENT_LAYERS (sidebar_layers->priv->document);
372 model = gtk_tree_view_get_model (GTK_TREE_VIEW (sidebar_layers->priv->tree_view));
373 if (gtk_tree_model_get_iter_first (model, &iter))
374 update_layers_state (model, &iter, document_layers);
378 job_finished_callback (EvJobLayers *job,
379 EvSidebarLayers *sidebar_layers)
381 EvSidebarLayersPrivate *priv;
383 priv = sidebar_layers->priv;
385 gtk_tree_view_set_model (GTK_TREE_VIEW (priv->tree_view), job->model);
387 g_object_unref (job);
392 ev_sidebar_layers_document_changed_cb (EvDocumentModel *model,
394 EvSidebarLayers *sidebar_layers)
396 EvDocument *document = ev_document_model_get_document (model);
397 EvSidebarLayersPrivate *priv = sidebar_layers->priv;
399 if (!EV_IS_DOCUMENT_LAYERS (document))
402 if (priv->document) {
403 gtk_tree_view_set_model (GTK_TREE_VIEW (priv->tree_view), NULL);
404 g_object_unref (priv->document);
407 priv->document = g_object_ref (document);
410 g_signal_handlers_disconnect_by_func (priv->job,
411 job_finished_callback,
413 g_object_unref (priv->job);
416 priv->job = ev_job_layers_new (document);
417 g_signal_connect (priv->job, "finished",
418 G_CALLBACK (job_finished_callback),
420 /* The priority doesn't matter for this job */
421 ev_job_scheduler_push_job (priv->job, EV_JOB_PRIORITY_NONE);
425 ev_sidebar_layers_set_model (EvSidebarPage *sidebar_page,
426 EvDocumentModel *model)
428 g_signal_connect (model, "notify::document",
429 G_CALLBACK (ev_sidebar_layers_document_changed_cb),
434 ev_sidebar_layers_support_document (EvSidebarPage *sidebar_page,
435 EvDocument *document)
437 return (EV_IS_DOCUMENT_LAYERS (document) &&
438 ev_document_layers_has_layers (EV_DOCUMENT_LAYERS (document)));
442 ev_sidebar_layers_get_label (EvSidebarPage *sidebar_page)
448 ev_sidebar_layers_page_iface_init (EvSidebarPageInterface *iface)
450 iface->support_document = ev_sidebar_layers_support_document;
451 iface->set_model = ev_sidebar_layers_set_model;
452 iface->get_label = ev_sidebar_layers_get_label;