Evince
Evince is a document viewer capable of displaying multiple and single page document formats like PDF and Postscript.
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
ev-sidebar-layers.c
Go to the documentation of this file.
1 /* ev-sidebar-layers.c
2  * this file is part of evince, a gnome document viewer
3  *
4  * Copyright (C) 2008 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 <glib/gi18n.h>
24 #include "gimpcellrenderertoggle.h"
25 
26 #include "ev-document-layers.h"
27 #include "ev-sidebar-page.h"
28 #include "ev-jobs.h"
29 #include "ev-job-scheduler.h"
30 #include "ev-stock-icons.h"
31 #include "ev-sidebar-layers.h"
32 
34  GtkTreeView *tree_view;
35 
38 };
39 
40 enum {
43 };
44 
45 enum {
48 };
49 
51 static void job_finished_callback (EvJobLayers *job,
52  EvSidebarLayers *sidebar_layers);
53 
54 static guint signals[N_SIGNALS];
55 
57  ev_sidebar_layers,
58  GTK_TYPE_BOX,
59  0,
60  G_IMPLEMENT_INTERFACE (EV_TYPE_SIDEBAR_PAGE,
62 
63 #define EV_SIDEBAR_LAYERS_GET_PRIVATE(object) \
64  (G_TYPE_INSTANCE_GET_PRIVATE ((object), EV_TYPE_SIDEBAR_LAYERS, EvSidebarLayersPrivate))
65 
66 static void
67 ev_sidebar_layers_dispose (GObject *object)
68 {
69  EvSidebarLayers *sidebar = EV_SIDEBAR_LAYERS (object);
70 
71  if (sidebar->priv->job) {
72  g_signal_handlers_disconnect_by_func (sidebar->priv->job,
74  sidebar);
75  ev_job_cancel (sidebar->priv->job);
76  g_object_unref (sidebar->priv->job);
77  sidebar->priv->job = NULL;
78  }
79 
80  if (sidebar->priv->document) {
81  g_object_unref (sidebar->priv->document);
82  sidebar->priv->document = NULL;
83  }
84 
85  G_OBJECT_CLASS (ev_sidebar_layers_parent_class)->dispose (object);
86 }
87 
88 static void
90  guint prop_id,
91  GValue *value,
92  GParamSpec *pspec)
93 {
94  EvSidebarLayers *ev_sidebar_layers;
95 
96  ev_sidebar_layers = EV_SIDEBAR_LAYERS (object);
97 
98  switch (prop_id) {
99  case PROP_WIDGET:
100  g_value_set_object (value, ev_sidebar_layers->priv->tree_view);
101  break;
102  default:
103  G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
104  break;
105  }
106 }
107 
108 static GtkTreeModel *
110 {
111  GtkTreeModel *retval;
112  GtkTreeIter iter;
113  gchar *markup;
114 
115  /* Creates a fake model to indicate that we're loading */
116  retval = (GtkTreeModel *)gtk_list_store_new (EV_DOCUMENT_LAYERS_N_COLUMNS,
117  G_TYPE_STRING,
118  G_TYPE_OBJECT,
119  G_TYPE_BOOLEAN,
120  G_TYPE_BOOLEAN,
121  G_TYPE_BOOLEAN,
122  G_TYPE_INT);
123 
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,
133  -1);
134  g_free (markup);
135 
136  return retval;
137 }
138 
139 static gboolean
140 update_kids (GtkTreeModel *model,
141  GtkTreePath *path,
142  GtkTreeIter *iter,
143  GtkTreeIter *parent)
144 {
145  if (gtk_tree_store_is_ancestor (GTK_TREE_STORE (model), parent, iter)) {
146  gboolean visible;
147 
148  gtk_tree_model_get (model, parent,
150  -1);
151  gtk_tree_store_set (GTK_TREE_STORE (model), iter,
153  -1);
154  }
155 
156  return FALSE;
157 }
158 
159 static gboolean
160 clear_rb_group (GtkTreeModel *model,
161  GtkTreePath *path,
162  GtkTreeIter *iter,
163  gint *rb_group)
164 {
165  gint group;
166 
167  gtk_tree_model_get (model, iter,
169  -1);
170 
171  if (group == *rb_group) {
172  gtk_tree_store_set (GTK_TREE_STORE (model), iter,
174  -1);
175  }
176 
177  return FALSE;
178 }
179 
180 static void
181 ev_sidebar_layers_visibility_changed (GtkCellRendererToggle *cell,
182  gchar *path_str,
183  EvSidebarLayers *ev_layers)
184 {
185  GtkTreeModel *model;
186  GtkTreePath *path;
187  GtkTreeIter iter;
188  gboolean visible;
189  EvLayer *layer;
190 
191  model = gtk_tree_view_get_model (ev_layers->priv->tree_view);
192 
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,
198  -1);
199 
200  visible = !visible;
201  if (visible) {
202  gint rb_group;
203 
204  ev_document_layers_show_layer (EV_DOCUMENT_LAYERS (ev_layers->priv->document),
205  layer);
206 
207  rb_group = ev_layer_get_rb_group (layer);
208  if (rb_group) {
209  gtk_tree_model_foreach (model,
210  (GtkTreeModelForeachFunc)clear_rb_group,
211  &rb_group);
212  }
213  } else {
214  ev_document_layers_hide_layer (EV_DOCUMENT_LAYERS (ev_layers->priv->document),
215  layer);
216  }
217 
218  gtk_tree_store_set (GTK_TREE_STORE (model), &iter,
220  -1);
221 
222  if (ev_layer_is_parent (layer)) {
223  gtk_tree_model_foreach (model,
224  (GtkTreeModelForeachFunc)update_kids,
225  &iter);
226  }
227 
228  gtk_tree_path_free (path);
229 
230  g_signal_emit (ev_layers, signals[LAYERS_VISIBILITY_CHANGED], 0);
231 }
232 
233 static GtkTreeView *
235 {
236  GtkTreeView *tree_view;
237  GtkTreeViewColumn *column;
238  GtkCellRenderer *renderer;
239 
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),
243  GTK_SELECTION_NONE);
244 
245 
246  column = gtk_tree_view_column_new ();
247 
249  gtk_tree_view_column_pack_start (column, renderer, FALSE);
250  gtk_tree_view_column_set_attributes (column, renderer,
252  "activatable", EV_DOCUMENT_LAYERS_COLUMN_ENABLED,
255  NULL);
256  g_object_set (G_OBJECT (renderer),
257  "xpad", 0,
258  "ypad", 0,
259  NULL);
260  g_signal_connect (renderer, "toggled",
262  (gpointer)ev_layers);
263 
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,
269  NULL);
270  g_object_set (G_OBJECT (renderer), "ellipsize", PANGO_ELLIPSIZE_END, NULL);
271 
272  gtk_tree_view_append_column (tree_view, column);
273 
274  return tree_view;
275 }
276 
277 static void
279 {
280  GtkWidget *swindow;
281  GtkTreeModel *model;
282 
283  ev_layers->priv = EV_SIDEBAR_LAYERS_GET_PRIVATE (ev_layers);
284 
285  swindow = gtk_scrolled_window_new (NULL, NULL);
286  gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (swindow),
287  GTK_POLICY_NEVER,
288  GTK_POLICY_AUTOMATIC);
289  /* Data Model */
291 
292  /* Layers list */
293  ev_layers->priv->tree_view = ev_sidebar_layers_create_tree_view (ev_layers);
294  gtk_tree_view_set_model (ev_layers->priv->tree_view, model);
295  g_object_unref (model);
296 
297  gtk_container_add (GTK_CONTAINER (swindow),
298  GTK_WIDGET (ev_layers->priv->tree_view));
299 
300  gtk_box_pack_start (GTK_BOX (ev_layers), swindow, TRUE, TRUE, 0);
301  gtk_widget_show_all (GTK_WIDGET (ev_layers));
302 }
303 
304 static void
306 {
307  GObjectClass *g_object_class = G_OBJECT_CLASS (ev_layers_class);
308 
309  g_object_class->get_property = ev_sidebar_layers_get_property;
310  g_object_class->dispose = ev_sidebar_layers_dispose;
311 
312  g_type_class_add_private (g_object_class, sizeof (EvSidebarLayersPrivate));
313 
314  g_object_class_override_property (g_object_class, PROP_WIDGET, "main-widget");
315 
317  g_signal_new ("layers_visibility_changed",
318  G_TYPE_FROM_CLASS (g_object_class),
319  G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
320  G_STRUCT_OFFSET (EvSidebarLayersClass, layers_visibility_changed),
321  NULL, NULL,
322  g_cclosure_marshal_VOID__VOID,
323  G_TYPE_NONE, 0, G_TYPE_NONE);
324 }
325 
326 GtkWidget *
328 {
329  return GTK_WIDGET (g_object_new (EV_TYPE_SIDEBAR_LAYERS,
330  "orientation", GTK_ORIENTATION_VERTICAL,
331  NULL));
332 }
333 
334 static void
335 update_layers_state (GtkTreeModel *model,
336  GtkTreeIter *iter,
337  EvDocumentLayers *document_layers)
338 {
339  EvLayer *layer;
340  gboolean visible;
341  GtkTreeIter child_iter;
342 
343  do {
344  gtk_tree_model_get (model, iter,
347  -1);
348  if (layer) {
349  gboolean layer_visible;
350 
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,
355  -1);
356  }
357  }
358 
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));
362 }
363 
364 void
366 {
367  GtkTreeModel *model;
368  GtkTreeIter iter;
369  EvDocumentLayers *document_layers;
370 
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);
375 }
376 
377 static void
379  EvSidebarLayers *sidebar_layers)
380 {
382 
383  priv = sidebar_layers->priv;
384 
385  gtk_tree_view_set_model (GTK_TREE_VIEW (priv->tree_view), job->model);
386 
387  g_object_unref (job);
388  priv->job = NULL;
389 }
390 
391 static void
393  GParamSpec *pspec,
394  EvSidebarLayers *sidebar_layers)
395 {
396  EvDocument *document = ev_document_model_get_document (model);
397  EvSidebarLayersPrivate *priv = sidebar_layers->priv;
398 
399  if (!EV_IS_DOCUMENT_LAYERS (document))
400  return;
401 
402  if (priv->document) {
403  gtk_tree_view_set_model (GTK_TREE_VIEW (priv->tree_view), NULL);
404  g_object_unref (priv->document);
405  }
406 
407  priv->document = g_object_ref (document);
408 
409  if (priv->job) {
410  g_signal_handlers_disconnect_by_func (priv->job,
412  sidebar_layers);
413  g_object_unref (priv->job);
414  }
415 
416  priv->job = ev_job_layers_new (document);
417  g_signal_connect (priv->job, "finished",
418  G_CALLBACK (job_finished_callback),
419  sidebar_layers);
420  /* The priority doesn't matter for this job */
422 }
423 
424 static void
426  EvDocumentModel *model)
427 {
428  g_signal_connect (model, "notify::document",
430  sidebar_page);
431 }
432 
433 static gboolean
435  EvDocument *document)
436 {
437  return (EV_IS_DOCUMENT_LAYERS (document) &&
439 }
440 
441 static const gchar*
443 {
444  return _("Layers");
445 }
446 
447 static void
449 {
453 }
454