]> www.fi.muni.cz Git - evince.git/blob - shell/ev-navigation-action.c
History finally works as expected.
[evince.git] / shell / ev-navigation-action.c
1 /*
2  *  Copyright (C) 2003, 2004 Marco Pesenti Gritti
3  *  Copyright (C) 2003, 2004 Christian Persch
4  *
5  *  This program is free software; you can redistribute it and/or modify
6  *  it under the terms of the GNU General Public License as published by
7  *  the Free Software Foundation; either version 2, or (at your option)
8  *  any later version.
9  *
10  *  This program is distributed in the hope that it will be useful,
11  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  *  GNU General Public License for more details.
14  *
15  *  You should have received a copy of the GNU General Public License
16  *  along with this program; if not, write to the Free Software
17  *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18  *
19  */
20
21 #include "config.h"
22
23 #include "ev-navigation-action.h"
24 #include "ev-navigation-action-widget.h"
25
26 #include <gtk/gtklabel.h>
27 #include <gtk/gtkimage.h>
28 #include <gtk/gtkmenuitem.h>
29 #include <gtk/gtkimagemenuitem.h>
30 #include <gtk/gtkmenushell.h>
31 #include <gtk/gtkmenu.h>
32 #include <gtk/gtkmenutoolbutton.h>
33 #include <glib/gi18n.h>
34
35 enum
36 {
37         WIDGET_ACTIVATE_LINK,
38         WIDGET_N_SIGNALS
39 };
40
41 static guint widget_signals[WIDGET_N_SIGNALS] = {0, };
42
43 struct _EvNavigationActionPrivate
44 {
45         EvHistory *history;
46 };
47
48 static void ev_navigation_action_init       (EvNavigationAction *action);
49 static void ev_navigation_action_class_init (EvNavigationActionClass *class);
50
51 G_DEFINE_TYPE (EvNavigationAction, ev_navigation_action, GTK_TYPE_ACTION)
52
53 #define MAX_LABEL_LENGTH 48
54
55 #define EV_NAVIGATION_ACTION_GET_PRIVATE(object)(G_TYPE_INSTANCE_GET_PRIVATE ((object), EV_TYPE_NAVIGATION_ACTION, EvNavigationActionPrivate))
56
57 void
58 ev_navigation_action_set_history (EvNavigationAction *action,
59                                   EvHistory          *history)
60 {
61         action->priv->history = history;
62
63         g_object_add_weak_pointer (G_OBJECT (action->priv->history),
64                                    (gpointer *) &action->priv->history);
65 }
66
67 static void
68 activate_menu_item_cb (GtkWidget *widget, EvNavigationAction *action)
69 {
70         int index;
71
72         g_return_if_fail (EV_IS_HISTORY (action->priv->history));
73
74         index = GPOINTER_TO_INT (g_object_get_data (G_OBJECT (widget), "index"));
75         ev_history_set_current_index (action->priv->history, index);
76         
77         if (action->priv->history) {
78                 EvLink *link;
79
80                 link = ev_history_get_link_nth (action->priv->history, index);
81                 
82                 g_signal_emit (action, widget_signals[WIDGET_ACTIVATE_LINK], 0, link);
83         }
84 }
85
86 static GtkWidget *
87 new_history_menu_item (EvNavigationAction *action,
88                        EvLink             *link,
89                        int                 index)
90 {
91         GtkLabel *label;
92         GtkWidget *item;
93         const char *title;
94
95         title = ev_link_get_title (link);
96         item = gtk_image_menu_item_new_with_label (title);
97         g_object_set_data (G_OBJECT (item), "index",
98                            GINT_TO_POINTER (index));
99
100         label = GTK_LABEL (GTK_BIN (item)->child);
101         gtk_label_set_ellipsize (label, PANGO_ELLIPSIZE_END);
102         gtk_label_set_max_width_chars (label, MAX_LABEL_LENGTH);
103
104         g_signal_connect (item, "activate",
105                           G_CALLBACK (activate_menu_item_cb),
106                           action);
107
108         gtk_widget_show (item);
109
110         return item;
111 }
112
113 static GtkWidget *
114 new_empty_history_menu_item (EvNavigationAction *action)
115 {
116         GtkWidget *item;
117         
118         item = gtk_image_menu_item_new_with_label (_("Empty"));
119         gtk_widget_set_sensitive (item, FALSE);
120         gtk_widget_show (item);
121         
122         return item;
123 }
124
125 static GtkWidget *
126 build_menu (EvNavigationAction *action)
127 {
128         GtkMenuShell *menu;
129         GtkWidget *item;
130         EvLink *link;
131         EvHistory *history = action->priv->history;
132         int start, end, i;
133
134         menu = GTK_MENU_SHELL (gtk_menu_new ());
135
136         if (history == NULL || ev_history_get_n_links (history) <= 0) {
137                 item = new_empty_history_menu_item (action);
138                 gtk_menu_shell_append (menu, item);             
139                 return GTK_WIDGET (menu);
140         }
141
142         start = MAX (ev_history_get_current_index (action->priv->history) - 5, 0);
143         end = MIN (ev_history_get_n_links (history), start + 7);
144
145         for (i = start; i < end; i++) {
146                 link = ev_history_get_link_nth (history, i);
147                 item = new_history_menu_item (action, link, i);
148                 gtk_menu_shell_prepend (menu, item);
149         }
150
151         return GTK_WIDGET (menu);
152 }
153
154 static void
155 menu_activated_cb (EvNavigationActionWidget *button,
156                    EvNavigationAction *action)
157 {
158         GtkWidget *menu;
159
160         menu = build_menu (action);
161         ev_navigation_action_widget_set_menu (button, menu);
162 }
163
164 static void
165 connect_proxy (GtkAction *action, GtkWidget *proxy)
166 {
167         GtkWidget *menu;
168
169         /* set dummy menu so the arrow gets sensitive */
170         menu = gtk_menu_new ();
171         ev_navigation_action_widget_set_menu (EV_NAVIGATION_ACTION_WIDGET (proxy), menu);
172
173         g_signal_connect (proxy, "show-menu",
174                           G_CALLBACK (menu_activated_cb), action);
175
176         GTK_ACTION_CLASS (ev_navigation_action_parent_class)->connect_proxy (action, proxy);
177 }
178
179 static GtkWidget *
180 create_tool_item (GtkAction *action)
181 {
182         EvNavigationActionWidget *proxy;
183
184         proxy = g_object_new (EV_TYPE_NAVIGATION_ACTION_WIDGET, NULL);
185         gtk_widget_show (GTK_WIDGET (proxy));
186
187         return GTK_WIDGET (proxy);
188 }
189
190 static void
191 ev_navigation_action_init (EvNavigationAction *action)
192 {
193         action->priv = EV_NAVIGATION_ACTION_GET_PRIVATE (action);
194 }
195
196 static void
197 ev_navigation_action_finalize (GObject *object)
198 {
199         EvNavigationAction *action = EV_NAVIGATION_ACTION (object);
200
201         if (action->priv->history) {
202                 g_object_add_weak_pointer (G_OBJECT (action->priv->history),
203                                            (gpointer *) &action->priv->history);
204         }
205
206         G_OBJECT_CLASS (ev_navigation_action_parent_class)->finalize (object);
207 }
208
209 static void
210 ev_navigation_action_class_init (EvNavigationActionClass *class)
211 {
212         GObjectClass *object_class = G_OBJECT_CLASS (class);
213         GtkActionClass *action_class = GTK_ACTION_CLASS (class);
214
215         object_class->finalize = ev_navigation_action_finalize;
216
217         action_class->toolbar_item_type = GTK_TYPE_TOOL_ITEM;
218         action_class->create_tool_item = create_tool_item;
219         action_class->connect_proxy = connect_proxy;
220
221         widget_signals[WIDGET_ACTIVATE_LINK] = g_signal_new ("activate_link",
222                                                G_OBJECT_CLASS_TYPE (object_class),
223                                                G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
224                                                G_STRUCT_OFFSET (EvNavigationActionClass, activate_link),
225                                                NULL, NULL,
226                                                g_cclosure_marshal_VOID__OBJECT,
227                                                G_TYPE_NONE, 1,
228                                                G_TYPE_OBJECT);
229
230         g_type_class_add_private (object_class, sizeof (EvNavigationActionPrivate));
231 }