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.c
Go to the documentation of this file.
1 /* this file is part of evince, a gnome document viewer
2  *
3  * Copyright (C) 2004 Red Hat, Inc.
4  *
5  * Author:
6  * Jonathan Blandford <jrb@alum.mit.edu>
7  *
8  * Evince is free software; you can redistribute it and/or modify it
9  * under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * Evince is distributed in the hope that it will be useful, but
14  * WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16  * General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
21  */
22 
23 #ifdef HAVE_CONFIG_H
24 #include "config.h"
25 #endif
26 
27 #include <string.h>
28 
29 #include <gtk/gtk.h>
30 #include <gdk/gdkkeysyms.h>
31 
32 #include "ev-sidebar.h"
33 #include "ev-sidebar-page.h"
34 
35 enum
36 {
39 };
40 
41 enum
42 {
48 };
49 
51  GtkWidget *notebook;
52  GtkWidget *select_button;
53  GtkWidget *menu;
54  GtkWidget *hbox;
55  GtkWidget *label;
56 
58  GtkTreeModel *page_model;
59 };
60 
61 G_DEFINE_TYPE (EvSidebar, ev_sidebar, GTK_TYPE_BOX)
62 
63 #define EV_SIDEBAR_GET_PRIVATE(object) \
64  (G_TYPE_INSTANCE_GET_PRIVATE ((object), EV_TYPE_SIDEBAR, EvSidebarPrivate))
65 
66 static void
67 ev_sidebar_dispose (GObject *object)
68 {
69  EvSidebar *ev_sidebar = EV_SIDEBAR (object);
70 
71  if (ev_sidebar->priv->menu) {
72  gtk_menu_detach (GTK_MENU (ev_sidebar->priv->menu));
73  ev_sidebar->priv->menu = NULL;
74  }
75 
76  if (ev_sidebar->priv->page_model) {
77  g_object_unref (ev_sidebar->priv->page_model);
78  ev_sidebar->priv->page_model = NULL;
79  }
80 
81 
82  G_OBJECT_CLASS (ev_sidebar_parent_class)->dispose (object);
83 }
84 
85 static void
86 ev_sidebar_select_page (EvSidebar *ev_sidebar, GtkTreeIter *iter)
87 {
88  char *title;
89  int index;
90 
91  gtk_tree_model_get (ev_sidebar->priv->page_model, iter,
92  PAGE_COLUMN_TITLE, &title,
94  -1);
95 
96  gtk_notebook_set_current_page (GTK_NOTEBOOK (ev_sidebar->priv->notebook), index);
97  gtk_label_set_text (GTK_LABEL (ev_sidebar->priv->label), title);
98 
99  g_free (title);
100 }
101 
102 void
104  GtkWidget *main_widget)
105 {
106  GtkTreeIter iter;
107  gboolean valid;
108 
109  valid = gtk_tree_model_get_iter_first (ev_sidebar->priv->page_model, &iter);
110 
111  while (valid) {
112  GtkWidget *widget;
113 
114  gtk_tree_model_get (ev_sidebar->priv->page_model, &iter,
115  PAGE_COLUMN_MAIN_WIDGET, &widget,
116  -1);
117 
118  if (widget == main_widget) {
119  ev_sidebar_select_page (ev_sidebar, &iter);
120  valid = FALSE;
121  } else {
122  valid = gtk_tree_model_iter_next (ev_sidebar->priv->page_model, &iter);
123  }
124  g_object_unref (widget);
125  }
126 
127  g_object_notify (G_OBJECT (ev_sidebar), "current-page");
128 }
129 
130 static void
131 ev_sidebar_set_property (GObject *object,
132  guint prop_id,
133  const GValue *value,
134  GParamSpec *pspec)
135 {
136  EvSidebar *sidebar = EV_SIDEBAR (object);
137 
138  switch (prop_id)
139  {
140  case PROP_CURRENT_PAGE:
141  ev_sidebar_set_page (sidebar, g_value_get_object (value));
142  break;
143  default:
144  G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
145  }
146 }
147 
148 static GtkWidget *
150 {
151  GtkNotebook *notebook = GTK_NOTEBOOK (sidebar->priv->notebook);
152 
153  return gtk_notebook_get_nth_page
154  (notebook, gtk_notebook_get_current_page (notebook));
155 }
156 
157 static void
158 ev_sidebar_get_property (GObject *object,
159  guint prop_id,
160  GValue *value,
161  GParamSpec *pspec)
162 {
163  EvSidebar *sidebar = EV_SIDEBAR (object);
164 
165  switch (prop_id)
166  {
167  case PROP_CURRENT_PAGE:
168  g_value_set_object (value, ev_sidebar_get_current_page (sidebar));
169  break;
170  default:
171  G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
172  }
173 }
174 
175 static void
177 {
178  GObjectClass *g_object_class = G_OBJECT_CLASS (ev_sidebar_class);
179 
180  g_type_class_add_private (g_object_class, sizeof (EvSidebarPrivate));
181 
182  g_object_class->dispose = ev_sidebar_dispose;
183  g_object_class->get_property = ev_sidebar_get_property;
184  g_object_class->set_property = ev_sidebar_set_property;
185 
186  g_object_class_install_property (g_object_class,
188  g_param_spec_object ("current-page",
189  "Current page",
190  "The currently visible page",
191  GTK_TYPE_WIDGET,
192  G_PARAM_READWRITE |
193  G_PARAM_STATIC_STRINGS));
194 }
195 
196 static void
198  int *x,
199  int *y,
200  gboolean *push_in,
201  gpointer user_data)
202 {
203  GtkWidget *widget;
204  GtkAllocation allocation;
205 
206  g_return_if_fail (GTK_IS_BUTTON (user_data));
207  g_return_if_fail (!gtk_widget_get_has_window (GTK_WIDGET (user_data)));
208 
209  widget = GTK_WIDGET (user_data);
210 
211  gdk_window_get_origin (gtk_widget_get_window (widget), x, y);
212  gtk_widget_get_allocation (widget, &allocation);
213 
214  *x += allocation.x;
215  *y += allocation.y + allocation.height;
216 
217  *push_in = FALSE;
218 }
219 
220 static gboolean
222  GdkEventButton *event,
223  gpointer user_data)
224 {
225  EvSidebar *ev_sidebar = EV_SIDEBAR (user_data);
226 
227  if (event->button == 1) {
228  GtkRequisition requisition;
229  GtkAllocation allocation;
230  gint width;
231 
232  gtk_widget_get_allocation (widget, &allocation);
233  width = allocation.width;
234  gtk_widget_set_size_request (ev_sidebar->priv->menu, -1, -1);
235  gtk_widget_get_preferred_size (ev_sidebar->priv->menu, &requisition, NULL);
236  gtk_widget_set_size_request (ev_sidebar->priv->menu,
237  MAX (width, requisition.width), -1);
238 
239  gtk_widget_grab_focus (widget);
240 
241  gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (widget), TRUE);
242  gtk_menu_popup (GTK_MENU (ev_sidebar->priv->menu),
243  NULL, NULL, ev_sidebar_menu_position_under, widget,
244  event->button, event->time);
245 
246  return TRUE;
247  }
248 
249  return FALSE;
250 }
251 
252 static gboolean
254  GdkEventKey *event,
255  gpointer user_data)
256 {
257  EvSidebar *ev_sidebar = EV_SIDEBAR (user_data);
258 
259  if (event->keyval == GDK_KEY_space ||
260  event->keyval == GDK_KEY_KP_Space ||
261  event->keyval == GDK_KEY_Return ||
262  event->keyval == GDK_KEY_KP_Enter) {
263  gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (widget), TRUE);
264  gtk_menu_popup (GTK_MENU (ev_sidebar->priv->menu),
265  NULL, NULL, ev_sidebar_menu_position_under, widget,
266  1, event->time);
267  return TRUE;
268  }
269 
270  return FALSE;
271 }
272 
273 static void
274 ev_sidebar_close_clicked_cb (GtkWidget *widget,
275  gpointer user_data)
276 {
277  EvSidebar *ev_sidebar = EV_SIDEBAR (user_data);
278 
279  gtk_widget_hide (GTK_WIDGET (ev_sidebar));
280 }
281 
282 static void
283 ev_sidebar_menu_deactivate_cb (GtkWidget *widget,
284  gpointer user_data)
285 {
286  GtkWidget *menu_button;
287 
288  menu_button = GTK_WIDGET (user_data);
289 
290  gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (menu_button), FALSE);
291 }
292 
293 static void
294 ev_sidebar_menu_detach_cb (GtkWidget *widget,
295  GtkMenu *menu)
296 {
297  EvSidebar *ev_sidebar = EV_SIDEBAR (widget);
298 
299  ev_sidebar->priv->menu = NULL;
300 }
301 
302 static void
304  gpointer user_data)
305 {
306  EvSidebar *ev_sidebar = EV_SIDEBAR (user_data);
307  GtkTreeIter iter;
308  GtkWidget *menu_item, *item;
309  gboolean valid;
310 
311  menu_item = gtk_menu_get_active (GTK_MENU (ev_sidebar->priv->menu));
312  valid = gtk_tree_model_get_iter_first (ev_sidebar->priv->page_model, &iter);
313 
314  while (valid) {
315  gtk_tree_model_get (ev_sidebar->priv->page_model, &iter,
316  PAGE_COLUMN_MENU_ITEM, &item,
317  -1);
318 
319  if (item == menu_item) {
320  ev_sidebar_select_page (ev_sidebar, &iter);
321  valid = FALSE;
322  } else {
323  valid = gtk_tree_model_iter_next (ev_sidebar->priv->page_model, &iter);
324  }
325  g_object_unref (item);
326  }
327 
328  g_object_notify (G_OBJECT (ev_sidebar), "current-page");
329 }
330 
331 static void
333 {
334  GtkWidget *hbox;
335  GtkWidget *close_button;
336  GtkWidget *select_hbox;
337  GtkWidget *separator;
338  GtkWidget *arrow;
339 
340  ev_sidebar->priv = EV_SIDEBAR_GET_PRIVATE (ev_sidebar);
341 
342  /* data model */
343  ev_sidebar->priv->page_model = (GtkTreeModel *)
344  gtk_list_store_new (PAGE_COLUMN_NUM_COLS,
345  G_TYPE_STRING,
346  GTK_TYPE_WIDGET,
347  GTK_TYPE_WIDGET,
348  G_TYPE_INT);
349 
350  /* top option menu */
351  hbox = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 6);
352  gtk_container_set_border_width (GTK_CONTAINER (hbox), 6);
353  ev_sidebar->priv->hbox = hbox;
354  gtk_box_pack_start (GTK_BOX (ev_sidebar), hbox, FALSE, FALSE, 0);
355  gtk_widget_show (hbox);
356 
357  /* separator */
358  separator = gtk_separator_new (GTK_ORIENTATION_HORIZONTAL);
359  gtk_box_pack_start (GTK_BOX (ev_sidebar), separator, FALSE, FALSE, 0);
360  gtk_widget_show (separator);
361 
362  ev_sidebar->priv->select_button = gtk_toggle_button_new ();
363  gtk_button_set_relief (GTK_BUTTON (ev_sidebar->priv->select_button), GTK_RELIEF_NONE);
364  g_signal_connect (ev_sidebar->priv->select_button, "button_press_event",
366  ev_sidebar);
367  g_signal_connect (ev_sidebar->priv->select_button, "key_press_event",
369  ev_sidebar);
370 
371  select_hbox = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 12);
372 
373  ev_sidebar->priv->label = gtk_label_new ("");
374  gtk_box_pack_start (GTK_BOX (select_hbox),
375  ev_sidebar->priv->label,
376  FALSE, FALSE, 0);
377  gtk_widget_show (ev_sidebar->priv->label);
378 
379  arrow = gtk_arrow_new (GTK_ARROW_DOWN, GTK_SHADOW_NONE);
380  gtk_box_pack_end (GTK_BOX (select_hbox), arrow, FALSE, FALSE, 0);
381  gtk_widget_show (arrow);
382 
383  gtk_container_add (GTK_CONTAINER (ev_sidebar->priv->select_button), select_hbox);
384  gtk_widget_show (select_hbox);
385 
386  gtk_box_set_center_widget (GTK_BOX (hbox), ev_sidebar->priv->select_button);
387  gtk_widget_show (ev_sidebar->priv->select_button);
388 
389  close_button = gtk_button_new_from_icon_name ("window-close-symbolic", GTK_ICON_SIZE_BUTTON);
390  gtk_button_set_relief (GTK_BUTTON (close_button), GTK_RELIEF_NONE);
391  g_signal_connect (close_button, "clicked",
392  G_CALLBACK (ev_sidebar_close_clicked_cb),
393  ev_sidebar);
394  gtk_box_pack_end (GTK_BOX (hbox), close_button, FALSE, FALSE, 0);
395  gtk_widget_show (close_button);
396 
397  ev_sidebar->priv->menu = gtk_menu_new ();
398  g_signal_connect (ev_sidebar->priv->menu, "deactivate",
399  G_CALLBACK (ev_sidebar_menu_deactivate_cb),
400  ev_sidebar->priv->select_button);
401  gtk_menu_attach_to_widget (GTK_MENU (ev_sidebar->priv->menu),
402  GTK_WIDGET (ev_sidebar),
404  gtk_widget_show (ev_sidebar->priv->menu);
405 
406  ev_sidebar->priv->notebook = gtk_notebook_new ();
407  gtk_notebook_set_show_border (GTK_NOTEBOOK (ev_sidebar->priv->notebook), FALSE);
408  gtk_notebook_set_show_tabs (GTK_NOTEBOOK (ev_sidebar->priv->notebook), FALSE);
409  gtk_box_pack_start (GTK_BOX (ev_sidebar), ev_sidebar->priv->notebook,
410  TRUE, TRUE, 0);
411  gtk_widget_show (ev_sidebar->priv->notebook);
412 
413  gtk_widget_set_sensitive (GTK_WIDGET (ev_sidebar->priv->notebook), FALSE);
414  gtk_widget_set_sensitive (GTK_WIDGET (ev_sidebar->priv->select_button), FALSE);
415 }
416 
417 /* Public functions */
418 
419 GtkWidget *
421 {
422  GtkWidget *ev_sidebar;
423 
424  ev_sidebar = g_object_new (EV_TYPE_SIDEBAR,
425  "orientation", GTK_ORIENTATION_VERTICAL,
426  NULL);
427 
428  return ev_sidebar;
429 }
430 
431 void
433  GtkWidget *main_widget)
434 {
435  GtkTreeIter iter;
436  GtkWidget *menu_item;
437  gchar *label_title;
438  const gchar *title;
439  int index;
440 
441  g_return_if_fail (EV_IS_SIDEBAR (ev_sidebar));
442  g_return_if_fail (EV_IS_SIDEBAR_PAGE (main_widget));
443  g_return_if_fail (GTK_IS_WIDGET (main_widget));
444 
446  ev_sidebar->priv->model);
447  title = ev_sidebar_page_get_label (EV_SIDEBAR_PAGE (main_widget));
448 
449  index = gtk_notebook_append_page (GTK_NOTEBOOK (ev_sidebar->priv->notebook),
450  main_widget, NULL);
451 
452  menu_item = gtk_image_menu_item_new_with_label (title);
453  g_signal_connect (menu_item, "activate",
455  ev_sidebar);
456  gtk_widget_show (menu_item);
457  gtk_menu_shell_append (GTK_MENU_SHELL (ev_sidebar->priv->menu),
458  menu_item);
459 
460  /* Insert and move to end */
461  gtk_list_store_insert_with_values (GTK_LIST_STORE (ev_sidebar->priv->page_model),
462  &iter, 0,
463  PAGE_COLUMN_TITLE, title,
464  PAGE_COLUMN_MENU_ITEM, menu_item,
465  PAGE_COLUMN_MAIN_WIDGET, main_widget,
467  -1);
468  gtk_list_store_move_before(GTK_LIST_STORE(ev_sidebar->priv->page_model),
469  &iter, NULL);
470 
471 
472  /* Set the first item added as active */
473  gtk_tree_model_get_iter_first (ev_sidebar->priv->page_model, &iter);
474  gtk_tree_model_get (ev_sidebar->priv->page_model,
475  &iter,
476  PAGE_COLUMN_TITLE, &label_title,
478  -1);
479 
480  gtk_menu_set_active (GTK_MENU (ev_sidebar->priv->menu), index);
481  gtk_label_set_text (GTK_LABEL (ev_sidebar->priv->label), label_title);
482  gtk_notebook_set_current_page (GTK_NOTEBOOK (ev_sidebar->priv->notebook),
483  index);
484  g_free (label_title);
485 }
486 
487 static gboolean
489  EvDocument *document)
490 {
491  GtkWidget *current_page = ev_sidebar_get_current_page (sidebar);
492 
493  return ev_sidebar_page_support_document (EV_SIDEBAR_PAGE (current_page), document);
494 }
495 
496 
497 static void
499  GParamSpec *pspec,
500  EvSidebar *sidebar)
501 {
502  EvSidebarPrivate *priv = sidebar->priv;
503  EvDocument *document = ev_document_model_get_document (model);
504  GtkTreeIter iter;
505  gboolean valid;
506  GtkWidget *first_supported_page = NULL;
507 
508  for (valid = gtk_tree_model_get_iter_first (priv->page_model, &iter);
509  valid;
510  valid = gtk_tree_model_iter_next (priv->page_model, &iter)) {
511  GtkWidget *widget;
512  GtkWidget *menu_widget;
513 
514  gtk_tree_model_get (priv->page_model, &iter,
515  PAGE_COLUMN_MAIN_WIDGET, &widget,
516  PAGE_COLUMN_MENU_ITEM, &menu_widget,
517  -1);
518 
519  if (ev_sidebar_page_support_document (EV_SIDEBAR_PAGE (widget), document)) {
520  gtk_widget_set_sensitive (menu_widget, TRUE);
521  if (!first_supported_page)
522  first_supported_page = widget;
523  } else {
524  gtk_widget_set_sensitive (menu_widget, FALSE);
525  }
526  g_object_unref (widget);
527  g_object_unref (menu_widget);
528  }
529 
530  if (first_supported_page != NULL) {
531  if (!ev_sidebar_current_page_support_document (sidebar, document)) {
532  ev_sidebar_set_page (sidebar, first_supported_page);
533  }
534  gtk_widget_set_sensitive (GTK_WIDGET (sidebar->priv->notebook), TRUE);
535  gtk_widget_set_sensitive (GTK_WIDGET (sidebar->priv->select_button), TRUE);
536  } else {
537  gtk_widget_hide (GTK_WIDGET (sidebar));
538  }
539 }
540 
541 void
543  EvDocumentModel *model)
544 {
545  g_return_if_fail (EV_IS_SIDEBAR (sidebar));
546  g_return_if_fail (EV_IS_DOCUMENT_MODEL (model));
547 
548  if (model == sidebar->priv->model)
549  return;
550 
551  sidebar->priv->model = model;
552  g_signal_connect (model, "notify::document",
553  G_CALLBACK (ev_sidebar_document_changed_cb),
554  sidebar);
555 }