]> www.fi.muni.cz Git - evince.git/blob - shell/ev-sidebar.c
Construct an actual sidebar.
[evince.git] / shell / ev-sidebar.c
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
21  */
22
23 #ifdef HAVE_CONFIG_H
24 #include "config.h"
25 #endif
26
27 #include <string.h>
28 #include <gtk/gtk.h>
29
30 #include "ev-sidebar.h"
31
32 typedef struct
33 {
34         char *id;
35         char *title;
36         GtkWidget *main_widget;
37 } EvSidebarPage;
38
39 enum
40 {
41         PAGE_COLUMN_ID,
42         PAGE_COLUMN_TITLE,
43         PAGE_COLUMN_MAIN_WIDGET,
44         PAGE_COLUMN_NOTEBOOK_INDEX,
45         PAGE_COLUMN_NUM_COLS
46 };
47
48 struct _EvSidebarPrivate {
49         GtkWidget *option_menu;
50         GtkWidget *notebook;
51
52         GtkTreeModel *page_model;
53 };
54
55 static void ev_sidebar_omenu_changed_cb (GtkComboBox *combo_box,
56                                          gpointer     user_data);
57
58 G_DEFINE_TYPE (EvSidebar, ev_sidebar, GTK_TYPE_VBOX)
59
60 #define EV_SIDEBAR_GET_PRIVATE(object) \
61         (G_TYPE_INSTANCE_GET_PRIVATE ((object), EV_TYPE_SIDEBAR, EvSidebarPrivate))
62
63 static void
64 ev_sidebar_class_init (EvSidebarClass *ev_sidebar_class)
65 {
66         GObjectClass *g_object_class;
67
68         g_object_class = G_OBJECT_CLASS (ev_sidebar_class);
69
70         g_type_class_add_private (g_object_class, sizeof (EvSidebarPrivate));
71
72 }
73
74 static void
75 ev_sidebar_init (EvSidebar *ev_sidebar)
76 {
77         GtkWidget *hbox;
78         GtkCellRenderer *renderer;
79
80         ev_sidebar->priv = EV_SIDEBAR_GET_PRIVATE (ev_sidebar);
81
82         gtk_box_set_spacing (GTK_BOX (ev_sidebar), 6);
83         /* data model */
84         ev_sidebar->priv->page_model = (GtkTreeModel *)
85                 gtk_list_store_new (PAGE_COLUMN_NUM_COLS,
86                                     G_TYPE_STRING,
87                                     G_TYPE_STRING,
88                                     GTK_TYPE_WIDGET,
89                                     G_TYPE_INT);
90         /* top option menu */
91         hbox = gtk_hbox_new (FALSE, 6);
92         gtk_box_pack_start (GTK_BOX (ev_sidebar), hbox,
93                             FALSE, FALSE, 0);
94         ev_sidebar->priv->option_menu =
95                 gtk_combo_box_new_with_model (ev_sidebar->priv->page_model);
96         g_signal_connect (ev_sidebar->priv->option_menu, "changed",
97                           G_CALLBACK (ev_sidebar_omenu_changed_cb), ev_sidebar);
98         gtk_box_pack_start (GTK_BOX (hbox),
99                             ev_sidebar->priv->option_menu,
100                             FALSE, FALSE, 0);
101         renderer = gtk_cell_renderer_text_new ();
102         gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (ev_sidebar->priv->option_menu),
103                                     renderer, TRUE);
104         gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (ev_sidebar->priv->option_menu),
105                                         renderer,
106                                         "text", PAGE_COLUMN_TITLE,
107                                         NULL);
108
109         ev_sidebar->priv->notebook = gtk_notebook_new ();
110         gtk_notebook_set_show_border (GTK_NOTEBOOK (ev_sidebar->priv->notebook), FALSE);
111         gtk_notebook_set_show_tabs (GTK_NOTEBOOK (ev_sidebar->priv->notebook), FALSE);
112         gtk_box_pack_start (GTK_BOX (ev_sidebar), ev_sidebar->priv->notebook,
113                             TRUE, TRUE, 0);
114         gtk_widget_show_all (GTK_WIDGET (ev_sidebar));
115 }
116
117 static void
118 ev_sidebar_omenu_changed_cb (GtkComboBox *combo_box,
119                              gpointer     user_data)
120 {
121         GtkTreeIter iter;
122         EvSidebar *ev_sidebar = EV_SIDEBAR (user_data);
123
124         if (gtk_combo_box_get_active_iter (combo_box, &iter)) {
125                 gint index;
126
127                 gtk_tree_model_get (ev_sidebar->priv->page_model,
128                                     &iter,
129                                     PAGE_COLUMN_NOTEBOOK_INDEX, &index,
130                                     -1);
131                 gtk_notebook_set_current_page (GTK_NOTEBOOK (ev_sidebar->priv->notebook),
132                                                index);
133                                                
134         }
135 }
136
137 /* Public functions */
138
139 GtkWidget *
140 ev_sidebar_new (void)
141 {
142         GtkWidget *ev_sidebar;
143
144         ev_sidebar = g_object_new (EV_TYPE_SIDEBAR, NULL);
145
146         return ev_sidebar;
147 }
148
149 void
150 ev_sidebar_add_page (EvSidebar   *ev_sidebar,
151                      const gchar *page_id,
152                      const gchar *title,
153                      GtkWidget   *main_widget)
154 {
155         GtkTreeIter iter;
156         int index;
157
158         g_return_if_fail (EV_IS_SIDEBAR (ev_sidebar));
159         g_return_if_fail (page_id != NULL);
160         g_return_if_fail (title != NULL);
161         g_return_if_fail (GTK_IS_WIDGET (main_widget));
162
163         index = gtk_notebook_append_page (GTK_NOTEBOOK (ev_sidebar->priv->notebook),
164                                           main_widget, NULL);
165                                           
166         gtk_list_store_insert_with_values (GTK_LIST_STORE (ev_sidebar->priv->page_model),
167                                            &iter, 0,
168                                            PAGE_COLUMN_ID, page_id,
169                                            PAGE_COLUMN_TITLE, title,
170                                            PAGE_COLUMN_MAIN_WIDGET, main_widget,
171                                            PAGE_COLUMN_NOTEBOOK_INDEX, index,
172                                            -1);
173 }
174
175 void
176 ev_sidebar_clear (EvSidebar *ev_sidebar)
177 {
178         g_return_if_fail (EV_IS_SIDEBAR (ev_sidebar));
179
180         gtk_list_store_clear (GTK_LIST_STORE (ev_sidebar->priv->page_model));
181 }