]> www.fi.muni.cz Git - evince.git/blob - shell/ev-history.c
Implement history dropdowns
[evince.git] / shell / ev-history.c
1 /*
2  *  Copyright (C) 2005 Marco Pesenti Gritti
3  *
4  *  This program is free software; you can redistribute it and/or modify
5  *  it under the terms of the GNU General Public License as published by
6  *  the Free Software Foundation; either version 2, or (at your option)
7  *  any later version.
8  *
9  *  This program is distributed in the hope that it will be useful,
10  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  *  GNU General Public License for more details.
13  *
14  *  You should have received a copy of the GNU General Public License
15  *  along with this program; if not, write to the Free Software
16  *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17  *
18  *  $Id$
19  */
20
21 #include "config.h"
22
23 #include <glib/gi18n.h>
24
25 #include "ev-history.h"
26
27 struct _EvHistoryPrivate
28 {
29         GList *links;
30         int current_index;
31 };
32
33 enum {
34         PROP_0,
35         PROP_INDEX
36 };
37
38 static void ev_history_init       (EvHistory *history);
39 static void ev_history_class_init (EvHistoryClass *class);
40
41 static GObjectClass *parent_class = NULL;
42
43 G_DEFINE_TYPE (EvHistory, ev_history, G_TYPE_OBJECT)
44
45 #define EV_HISTORY_GET_PRIVATE(object)(G_TYPE_INSTANCE_GET_PRIVATE ((object), EV_TYPE_HISTORY, EvHistoryPrivate))
46
47 static void
48 ev_history_init (EvHistory *history)
49 {
50         history->priv = EV_HISTORY_GET_PRIVATE (history);
51
52         history->priv->links = NULL;
53         history->priv->current_index = -1;
54 }
55
56 static void
57 free_links_list (GList *l)
58 {
59         g_list_foreach (l, (GFunc)g_object_unref, NULL);
60         g_list_free (l);
61 }
62
63 static void
64 ev_history_finalize (GObject *object)
65 {
66         EvHistory *history = EV_HISTORY (object);
67
68         free_links_list (history->priv->links);
69
70         parent_class->finalize (object);
71 }
72
73 static void
74 ev_history_get_property (GObject *object, guint prop_id, GValue *value,
75                          GParamSpec *param_spec)
76 {
77         EvHistory *self;
78
79         self = EV_HISTORY (object);
80
81         switch (prop_id) {
82         case PROP_INDEX:
83                 g_value_set_int (value, self->priv->current_index);
84                 break;
85         default:
86                 G_OBJECT_WARN_INVALID_PROPERTY_ID (object,
87                                                    prop_id,
88                                                    param_spec);
89                 break;
90         }
91 }
92
93 static void
94 ev_history_set_property (GObject *object, guint prop_id, const GValue *value,
95                          GParamSpec *param_spec)
96 {
97         EvHistory *self;
98         
99         self = EV_HISTORY (object);
100         
101         switch (prop_id) {
102         case PROP_INDEX:
103                 ev_history_set_current_index (self, g_value_get_int (value));
104                 break;
105         default:
106                 G_OBJECT_WARN_INVALID_PROPERTY_ID (object,
107                                                    prop_id,
108                                                    param_spec);
109                 break;
110         }
111 }
112
113 static void
114 ev_history_class_init (EvHistoryClass *class)
115 {
116         GObjectClass *object_class = G_OBJECT_CLASS (class);
117
118         object_class->finalize = ev_history_finalize;
119         object_class->set_property = ev_history_set_property;
120         object_class->get_property = ev_history_get_property;
121
122         parent_class = g_type_class_peek_parent (class);
123
124         g_object_class_install_property (object_class,
125                                          PROP_INDEX,
126                                          g_param_spec_int ("index",
127                                                            "Current Index",
128                                                            "The current index",
129                                                             -1,
130                                                             G_MAXINT,
131                                                             0,
132                                                             G_PARAM_READWRITE));
133
134         g_type_class_add_private (object_class, sizeof (EvHistoryPrivate));
135 }
136
137 void
138 ev_history_add_link (EvHistory *history, EvLink *link)
139 {
140         int length;
141
142         g_return_if_fail (EV_IS_HISTORY (history));
143         g_return_if_fail (EV_IS_LINK (link));
144
145         length = g_list_length (history->priv->links);
146         if (history->priv->current_index < length - 1) {
147                 GList *l = g_list_nth (history->priv->links,
148                                        history->priv->current_index + 1);
149                 
150                 if (l->prev) {
151                         l->prev->next = NULL;
152                         free_links_list (l);
153                 } else {
154                         free_links_list (history->priv->links);
155                         history->priv->links = NULL;
156                 }
157         }
158
159         g_object_ref (link);
160         history->priv->links = g_list_append (history->priv->links,
161                                               link);
162
163         length = g_list_length (history->priv->links);
164         history->priv->current_index = length - 1;
165 }
166
167 void
168 ev_history_add_page (EvHistory *history, int page)
169 {
170         EvLink *link;
171         char *title;
172
173         g_return_if_fail (EV_IS_HISTORY (history));
174
175         title = g_strdup_printf (_("Page %d"), page);
176         link = ev_link_new_page (title, page);
177         g_free (title);
178
179         ev_history_add_link (history, link);
180 }
181
182 EvLink *
183 ev_history_get_link_nth (EvHistory *history, int index)
184 {
185         GList *l;
186
187         g_return_val_if_fail (EV_IS_HISTORY (history), NULL);
188
189         l = g_list_nth (history->priv->links, index);
190
191         return EV_LINK (l->data);
192 }
193
194 int
195 ev_history_get_n_links (EvHistory *history)
196 {
197         g_return_val_if_fail (EV_IS_HISTORY (history), -1);
198
199         return g_list_length (history->priv->links);
200 }
201
202 int
203 ev_history_get_current_index (EvHistory *history)
204 {
205         g_return_val_if_fail (EV_IS_HISTORY (history), -1);
206
207         return history->priv->current_index;
208 }
209
210 void
211 ev_history_set_current_index (EvHistory *history, int index)
212 {
213         g_return_if_fail (EV_IS_HISTORY (history));
214
215         history->priv->current_index = index;
216
217         g_object_notify (G_OBJECT (history), "index");
218 }
219
220 EvHistory *
221 ev_history_new (void)
222 {
223         return EV_HISTORY (g_object_new (EV_TYPE_HISTORY, NULL));
224 }