]> www.fi.muni.cz Git - evince.git/blob - shell/ev-navigation-action.c
Preliminary history implementation
[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 #include "ev-window.h"
26
27 #include <gtk/gtklabel.h>
28 #include <gtk/gtkimage.h>
29 #include <gtk/gtkmenuitem.h>
30 #include <gtk/gtkimagemenuitem.h>
31 #include <gtk/gtkmenushell.h>
32 #include <gtk/gtkmenu.h>
33 #include <gtk/gtkmenutoolbutton.h>
34 #include <glib/gi18n.h>
35
36 struct _EvNavigationActionPrivate
37 {
38         EvWindow *window;
39         EvHistory *history;
40 };
41
42 static void ev_navigation_action_init       (EvNavigationAction *action);
43 static void ev_navigation_action_class_init (EvNavigationActionClass *class);
44
45 static GObjectClass *parent_class = NULL;
46
47 G_DEFINE_TYPE (EvNavigationAction, ev_navigation_action, GTK_TYPE_ACTION)
48
49 #define MAX_LABEL_LENGTH 48
50
51 #define EV_NAVIGATION_ACTION_GET_PRIVATE(object)(G_TYPE_INSTANCE_GET_PRIVATE ((object), EV_TYPE_NAVIGATION_ACTION, EvNavigationActionPrivate))
52
53 void
54 ev_navigation_action_set_history (EvNavigationAction *action,
55                                   EvHistory          *history)
56 {
57         action->priv->history = history;
58
59         g_object_add_weak_pointer (G_OBJECT (action->priv->history),
60                                    (gpointer *) &action->priv->history);
61 }
62
63 void
64 ev_navigation_action_set_window (EvNavigationAction *action,
65                                  EvWindow           *window)
66 {
67         action->priv->window = window;
68 }
69
70 static void
71 activate_menu_item_cb (GtkWidget *widget, EvNavigationAction *action)
72 {
73         int index;
74
75         g_return_if_fail (EV_IS_HISTORY (action->priv->history));
76
77         index = GPOINTER_TO_INT (g_object_get_data (G_OBJECT (widget), "index"));
78         ev_history_set_current_index (action->priv->history, index);
79         
80         if (action->priv->window) {
81                 EvLink *link;
82                 EvLinkAction *link_action;
83                 EvLinkDest *dest;
84
85                 link = ev_history_get_link_nth (action->priv->history, index);
86                 link_action = ev_link_get_action (link);
87                 dest = ev_link_action_get_dest (link_action);
88                 
89                 ev_window_goto_dest (action->priv->window, dest);
90         }
91 }
92
93 static GtkWidget *
94 new_history_menu_item (EvNavigationAction *action,
95                        EvLink             *link,
96                        int                 index)
97 {
98         GtkLabel *label;
99         GtkWidget *item;
100         const char *title;
101
102         title = ev_link_get_title (link);
103         item = gtk_image_menu_item_new_with_label (title);
104         g_object_set_data (G_OBJECT (item), "index",
105                            GINT_TO_POINTER (index));
106
107         label = GTK_LABEL (GTK_BIN (item)->child);
108         gtk_label_set_ellipsize (label, PANGO_ELLIPSIZE_END);
109         gtk_label_set_max_width_chars (label, MAX_LABEL_LENGTH);
110
111         g_signal_connect (item, "activate",
112                           G_CALLBACK (activate_menu_item_cb),
113                           action);
114
115         gtk_widget_show (item);
116
117         return item;
118 }
119
120 static GtkWidget *
121 new_empty_history_menu_item (EvNavigationAction *action)
122 {
123         GtkWidget *item;
124         
125         item = gtk_image_menu_item_new_with_label (_("Empty"));
126         gtk_widget_set_sensitive (item, FALSE);
127         gtk_widget_show (item);
128         
129         return item;
130 }
131
132 static GtkWidget *
133 build_menu (EvNavigationAction *action)
134 {
135         GtkMenuShell *menu;
136         GtkWidget *item;
137         EvLink *link;
138         EvHistory *history = action->priv->history;
139         int start, end, i;
140
141         menu = GTK_MENU_SHELL (gtk_menu_new ());
142
143         if (history == NULL || ev_history_get_n_links (history) <= 0) {
144                 item = new_empty_history_menu_item (action);
145                 gtk_menu_shell_append (menu, item);             
146                 return GTK_WIDGET (menu);
147         }
148
149         start = 0;
150         end = ev_history_get_n_links (history);
151
152         for (i = start; i < end; i++) {
153                 link = ev_history_get_link_nth (history, i);
154                 item = new_history_menu_item (action, link, i);
155                 gtk_menu_shell_append (menu, item);
156         }
157
158         return GTK_WIDGET (menu);
159 }
160
161 static void
162 menu_activated_cb (EvNavigationActionWidget *button,
163                    EvNavigationAction *action)
164 {
165         GtkWidget *menu;
166
167         menu = build_menu (action);
168         ev_navigation_action_widget_set_menu (button, menu);
169 }
170
171 static void
172 connect_proxy (GtkAction *action, GtkWidget *proxy)
173 {
174         GtkWidget *menu;
175
176         /* set dummy menu so the arrow gets sensitive */
177         menu = gtk_menu_new ();
178         ev_navigation_action_widget_set_menu (EV_NAVIGATION_ACTION_WIDGET (proxy), menu);
179
180         g_signal_connect (proxy, "show-menu",
181                           G_CALLBACK (menu_activated_cb), action);
182
183         GTK_ACTION_CLASS (parent_class)->connect_proxy (action, proxy);
184 }
185
186 static GtkWidget *
187 create_tool_item (GtkAction *action)
188 {
189         EvNavigationActionWidget *proxy;
190
191         proxy = g_object_new (EV_TYPE_NAVIGATION_ACTION_WIDGET, NULL);
192         gtk_widget_show (GTK_WIDGET (proxy));
193
194         return GTK_WIDGET (proxy);
195 }
196
197 static void
198 ev_navigation_action_init (EvNavigationAction *action)
199 {
200         action->priv = EV_NAVIGATION_ACTION_GET_PRIVATE (action);
201 }
202
203 static void
204 ev_navigation_action_finalize (GObject *object)
205 {
206         EvNavigationAction *action = EV_NAVIGATION_ACTION (object);
207
208         if (action->priv->history) {
209                 g_object_add_weak_pointer (G_OBJECT (action->priv->history),
210                                            (gpointer *) &action->priv->history);
211         }
212
213         parent_class->finalize (object);
214 }
215
216 static void
217 ev_navigation_action_class_init (EvNavigationActionClass *class)
218 {
219         GObjectClass *object_class = G_OBJECT_CLASS (class);
220         GtkActionClass *action_class = GTK_ACTION_CLASS (class);
221
222         object_class->finalize = ev_navigation_action_finalize;
223         parent_class = g_type_class_peek_parent (class);
224
225         action_class->toolbar_item_type = GTK_TYPE_TOOL_ITEM;
226         action_class->create_tool_item = create_tool_item;
227         action_class->connect_proxy = connect_proxy;
228
229         g_type_class_add_private (object_class, sizeof (EvNavigationActionPrivate));
230 }