1 /* this file is part of evince, a gnome document viewer
3 * Copyright (C) 2004 Red Hat, Inc.
6 * Jonathan Blandford <jrb@alum.mit.edu>
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.
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.
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.
30 #include <gdk/gdkkeysyms.h>
32 #include "ev-sidebar.h"
33 #include "ev-sidebar-page.h"
44 PAGE_COLUMN_MENU_ITEM,
45 PAGE_COLUMN_MAIN_WIDGET,
46 PAGE_COLUMN_NOTEBOOK_INDEX,
50 struct _EvSidebarPrivate {
52 GtkWidget *select_button;
57 EvDocumentModel *model;
58 GtkTreeModel *page_model;
61 G_DEFINE_TYPE (EvSidebar, ev_sidebar, GTK_TYPE_VBOX)
63 #define EV_SIDEBAR_GET_PRIVATE(object) \
64 (G_TYPE_INSTANCE_GET_PRIVATE ((object), EV_TYPE_SIDEBAR, EvSidebarPrivate))
67 ev_sidebar_dispose (GObject *object)
69 EvSidebar *ev_sidebar = EV_SIDEBAR (object);
71 if (ev_sidebar->priv->menu) {
72 gtk_menu_detach (GTK_MENU (ev_sidebar->priv->menu));
73 ev_sidebar->priv->menu = NULL;
76 if (ev_sidebar->priv->page_model) {
77 g_object_unref (ev_sidebar->priv->page_model);
78 ev_sidebar->priv->page_model = NULL;
82 G_OBJECT_CLASS (ev_sidebar_parent_class)->dispose (object);
86 ev_sidebar_select_page (EvSidebar *ev_sidebar, GtkTreeIter *iter)
91 gtk_tree_model_get (ev_sidebar->priv->page_model, iter,
92 PAGE_COLUMN_TITLE, &title,
93 PAGE_COLUMN_NOTEBOOK_INDEX, &index,
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);
103 ev_sidebar_set_page (EvSidebar *ev_sidebar,
104 GtkWidget *main_widget)
109 valid = gtk_tree_model_get_iter_first (ev_sidebar->priv->page_model, &iter);
114 gtk_tree_model_get (ev_sidebar->priv->page_model, &iter,
115 PAGE_COLUMN_MAIN_WIDGET, &widget,
118 if (widget == main_widget) {
119 ev_sidebar_select_page (ev_sidebar, &iter);
122 valid = gtk_tree_model_iter_next (ev_sidebar->priv->page_model, &iter);
124 g_object_unref (widget);
127 g_object_notify (G_OBJECT (ev_sidebar), "current-page");
131 ev_sidebar_set_property (GObject *object,
136 EvSidebar *sidebar = EV_SIDEBAR (object);
140 case PROP_CURRENT_PAGE:
141 ev_sidebar_set_page (sidebar, g_value_get_object (value));
144 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
149 ev_sidebar_get_current_page (EvSidebar *sidebar)
151 GtkNotebook *notebook = GTK_NOTEBOOK (sidebar->priv->notebook);
153 return gtk_notebook_get_nth_page
154 (notebook, gtk_notebook_get_current_page (notebook));
158 ev_sidebar_get_property (GObject *object,
163 EvSidebar *sidebar = EV_SIDEBAR (object);
167 case PROP_CURRENT_PAGE:
168 g_value_set_object (value, ev_sidebar_get_current_page (sidebar));
171 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
176 ev_sidebar_class_init (EvSidebarClass *ev_sidebar_class)
178 GObjectClass *g_object_class = G_OBJECT_CLASS (ev_sidebar_class);
180 g_type_class_add_private (g_object_class, sizeof (EvSidebarPrivate));
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;
186 g_object_class_install_property (g_object_class,
188 g_param_spec_object ("current-page",
190 "The currently visible page",
196 ev_sidebar_menu_position_under (GtkMenu *menu,
203 GtkAllocation allocation;
205 g_return_if_fail (GTK_IS_BUTTON (user_data));
206 g_return_if_fail (!gtk_widget_get_has_window (GTK_WIDGET (user_data)));
208 widget = GTK_WIDGET (user_data);
210 gdk_window_get_origin (gtk_widget_get_window (widget), x, y);
211 gtk_widget_get_allocation (widget, &allocation);
214 *y += allocation.y + allocation.height;
220 ev_sidebar_select_button_press_cb (GtkWidget *widget,
221 GdkEventButton *event,
224 EvSidebar *ev_sidebar = EV_SIDEBAR (user_data);
226 if (event->button == 1) {
227 GtkRequisition requisition;
228 GtkAllocation allocation;
231 gtk_widget_get_allocation (widget, &allocation);
232 width = allocation.width;
233 gtk_widget_set_size_request (ev_sidebar->priv->menu, -1, -1);
234 gtk_widget_get_preferred_size (ev_sidebar->priv->menu, &requisition, NULL);
235 gtk_widget_set_size_request (ev_sidebar->priv->menu,
236 MAX (width, requisition.width), -1);
238 gtk_widget_grab_focus (widget);
240 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (widget), TRUE);
241 gtk_menu_popup (GTK_MENU (ev_sidebar->priv->menu),
242 NULL, NULL, ev_sidebar_menu_position_under, widget,
243 event->button, event->time);
252 ev_sidebar_select_button_key_press_cb (GtkWidget *widget,
256 EvSidebar *ev_sidebar = EV_SIDEBAR (user_data);
258 if (event->keyval == GDK_KEY_space ||
259 event->keyval == GDK_KEY_KP_Space ||
260 event->keyval == GDK_KEY_Return ||
261 event->keyval == GDK_KEY_KP_Enter) {
262 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (widget), TRUE);
263 gtk_menu_popup (GTK_MENU (ev_sidebar->priv->menu),
264 NULL, NULL, ev_sidebar_menu_position_under, widget,
273 ev_sidebar_close_clicked_cb (GtkWidget *widget,
276 EvSidebar *ev_sidebar = EV_SIDEBAR (user_data);
278 gtk_widget_hide (GTK_WIDGET (ev_sidebar));
282 ev_sidebar_menu_deactivate_cb (GtkWidget *widget,
285 GtkWidget *menu_button;
287 menu_button = GTK_WIDGET (user_data);
289 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (menu_button), FALSE);
293 ev_sidebar_menu_detach_cb (GtkWidget *widget,
296 EvSidebar *ev_sidebar = EV_SIDEBAR (widget);
298 ev_sidebar->priv->menu = NULL;
302 ev_sidebar_menu_item_activate_cb (GtkWidget *widget,
305 EvSidebar *ev_sidebar = EV_SIDEBAR (user_data);
307 GtkWidget *menu_item, *item;
310 menu_item = gtk_menu_get_active (GTK_MENU (ev_sidebar->priv->menu));
311 valid = gtk_tree_model_get_iter_first (ev_sidebar->priv->page_model, &iter);
314 gtk_tree_model_get (ev_sidebar->priv->page_model, &iter,
315 PAGE_COLUMN_MENU_ITEM, &item,
318 if (item == menu_item) {
319 ev_sidebar_select_page (ev_sidebar, &iter);
322 valid = gtk_tree_model_iter_next (ev_sidebar->priv->page_model, &iter);
324 g_object_unref (item);
327 g_object_notify (G_OBJECT (ev_sidebar), "current-page");
331 ev_sidebar_init (EvSidebar *ev_sidebar)
334 GtkWidget *close_button;
335 GtkWidget *select_hbox;
339 ev_sidebar->priv = EV_SIDEBAR_GET_PRIVATE (ev_sidebar);
342 ev_sidebar->priv->page_model = (GtkTreeModel *)
343 gtk_list_store_new (PAGE_COLUMN_NUM_COLS,
349 /* top option menu */
350 hbox = gtk_hbox_new (FALSE, 0);
351 ev_sidebar->priv->hbox = hbox;
352 gtk_box_pack_start (GTK_BOX (ev_sidebar), hbox, FALSE, FALSE, 0);
353 gtk_widget_show (hbox);
355 ev_sidebar->priv->select_button = gtk_toggle_button_new ();
356 gtk_button_set_relief (GTK_BUTTON (ev_sidebar->priv->select_button), GTK_RELIEF_NONE);
357 g_signal_connect (ev_sidebar->priv->select_button, "button_press_event",
358 G_CALLBACK (ev_sidebar_select_button_press_cb),
360 g_signal_connect (ev_sidebar->priv->select_button, "key_press_event",
361 G_CALLBACK (ev_sidebar_select_button_key_press_cb),
364 select_hbox = gtk_hbox_new (FALSE, 0);
366 ev_sidebar->priv->label = gtk_label_new ("");
367 gtk_box_pack_start (GTK_BOX (select_hbox),
368 ev_sidebar->priv->label,
370 gtk_widget_show (ev_sidebar->priv->label);
372 arrow = gtk_arrow_new (GTK_ARROW_DOWN, GTK_SHADOW_NONE);
373 gtk_box_pack_end (GTK_BOX (select_hbox), arrow, FALSE, FALSE, 0);
374 gtk_widget_show (arrow);
376 gtk_container_add (GTK_CONTAINER (ev_sidebar->priv->select_button), select_hbox);
377 gtk_widget_show (select_hbox);
379 gtk_box_pack_start (GTK_BOX (hbox), ev_sidebar->priv->select_button, TRUE, TRUE, 0);
380 gtk_widget_show (ev_sidebar->priv->select_button);
382 close_button = gtk_button_new ();
383 gtk_button_set_relief (GTK_BUTTON (close_button), GTK_RELIEF_NONE);
384 g_signal_connect (close_button, "clicked",
385 G_CALLBACK (ev_sidebar_close_clicked_cb),
388 image = gtk_image_new_from_stock (GTK_STOCK_CLOSE,
390 gtk_container_add (GTK_CONTAINER (close_button), image);
391 gtk_widget_show (image);
393 gtk_box_pack_end (GTK_BOX (hbox), close_button, FALSE, FALSE, 0);
394 gtk_widget_show (close_button);
396 ev_sidebar->priv->menu = gtk_menu_new ();
397 g_signal_connect (ev_sidebar->priv->menu, "deactivate",
398 G_CALLBACK (ev_sidebar_menu_deactivate_cb),
399 ev_sidebar->priv->select_button);
400 gtk_menu_attach_to_widget (GTK_MENU (ev_sidebar->priv->menu),
401 GTK_WIDGET (ev_sidebar),
402 ev_sidebar_menu_detach_cb);
403 gtk_widget_show (ev_sidebar->priv->menu);
405 ev_sidebar->priv->notebook = gtk_notebook_new ();
406 gtk_notebook_set_show_border (GTK_NOTEBOOK (ev_sidebar->priv->notebook), FALSE);
407 gtk_notebook_set_show_tabs (GTK_NOTEBOOK (ev_sidebar->priv->notebook), FALSE);
408 gtk_box_pack_start (GTK_BOX (ev_sidebar), ev_sidebar->priv->notebook,
410 gtk_widget_show (ev_sidebar->priv->notebook);
412 gtk_widget_set_sensitive (GTK_WIDGET (ev_sidebar->priv->notebook), FALSE);
413 gtk_widget_set_sensitive (GTK_WIDGET (ev_sidebar->priv->select_button), FALSE);
416 /* Public functions */
419 ev_sidebar_new (void)
421 GtkWidget *ev_sidebar;
423 ev_sidebar = g_object_new (EV_TYPE_SIDEBAR, NULL);
429 ev_sidebar_add_page (EvSidebar *ev_sidebar,
430 GtkWidget *main_widget)
433 GtkWidget *menu_item;
438 g_return_if_fail (EV_IS_SIDEBAR (ev_sidebar));
439 g_return_if_fail (EV_IS_SIDEBAR_PAGE (main_widget));
440 g_return_if_fail (GTK_IS_WIDGET (main_widget));
442 ev_sidebar_page_set_model (EV_SIDEBAR_PAGE (main_widget),
443 ev_sidebar->priv->model);
444 title = ev_sidebar_page_get_label (EV_SIDEBAR_PAGE (main_widget));
446 index = gtk_notebook_append_page (GTK_NOTEBOOK (ev_sidebar->priv->notebook),
449 menu_item = gtk_image_menu_item_new_with_label (title);
450 g_signal_connect (menu_item, "activate",
451 G_CALLBACK (ev_sidebar_menu_item_activate_cb),
453 gtk_widget_show (menu_item);
454 gtk_menu_shell_append (GTK_MENU_SHELL (ev_sidebar->priv->menu),
457 /* Insert and move to end */
458 gtk_list_store_insert_with_values (GTK_LIST_STORE (ev_sidebar->priv->page_model),
460 PAGE_COLUMN_TITLE, title,
461 PAGE_COLUMN_MENU_ITEM, menu_item,
462 PAGE_COLUMN_MAIN_WIDGET, main_widget,
463 PAGE_COLUMN_NOTEBOOK_INDEX, index,
465 gtk_list_store_move_before(GTK_LIST_STORE(ev_sidebar->priv->page_model),
469 /* Set the first item added as active */
470 gtk_tree_model_get_iter_first (ev_sidebar->priv->page_model, &iter);
471 gtk_tree_model_get (ev_sidebar->priv->page_model,
473 PAGE_COLUMN_TITLE, &label_title,
474 PAGE_COLUMN_NOTEBOOK_INDEX, &index,
477 gtk_menu_set_active (GTK_MENU (ev_sidebar->priv->menu), index);
478 gtk_label_set_text (GTK_LABEL (ev_sidebar->priv->label), label_title);
479 gtk_notebook_set_current_page (GTK_NOTEBOOK (ev_sidebar->priv->notebook),
481 g_free (label_title);
485 ev_sidebar_document_changed_cb (EvDocumentModel *model,
489 EvSidebarPrivate *priv = sidebar->priv;
490 EvDocument *document = ev_document_model_get_document (model);
493 gboolean has_pages = FALSE;
495 for (valid = gtk_tree_model_get_iter_first (priv->page_model, &iter);
497 valid = gtk_tree_model_iter_next (priv->page_model, &iter)) {
499 GtkWidget *menu_widget;
501 gtk_tree_model_get (priv->page_model, &iter,
502 PAGE_COLUMN_MAIN_WIDGET, &widget,
503 PAGE_COLUMN_MENU_ITEM, &menu_widget,
506 if (ev_sidebar_page_support_document (EV_SIDEBAR_PAGE (widget), document)) {
509 gtk_widget_set_sensitive (menu_widget, FALSE);
511 g_object_unref (widget);
512 g_object_unref (menu_widget);
516 gtk_widget_hide (GTK_WIDGET (sidebar));
518 gtk_widget_set_sensitive (GTK_WIDGET (sidebar->priv->notebook), TRUE);
519 gtk_widget_set_sensitive (GTK_WIDGET (sidebar->priv->select_button), TRUE);
524 ev_sidebar_set_model (EvSidebar *sidebar,
525 EvDocumentModel *model)
527 g_return_if_fail (EV_IS_SIDEBAR (sidebar));
528 g_return_if_fail (EV_IS_DOCUMENT_MODEL (model));
530 if (model == sidebar->priv->model)
533 sidebar->priv->model = model;
534 g_signal_connect (model, "notify::document",
535 G_CALLBACK (ev_sidebar_document_changed_cb),