]> www.fi.muni.cz Git - evince.git/blob - shell/ev-navigation-action.c
Update FSF address everywhere.
[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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18  *
19  */
20
21 #include "config.h"
22
23 #include <glib/gi18n.h>
24 #include <gtk/gtk.h>
25
26 #include "ev-navigation-action.h"
27 #include "ev-navigation-action-widget.h"
28
29
30 enum
31 {
32         WIDGET_ACTIVATE_LINK,
33         WIDGET_N_SIGNALS
34 };
35
36 static guint widget_signals[WIDGET_N_SIGNALS] = {0, };
37
38 struct _EvNavigationActionPrivate
39 {
40         EvHistory *history;
41 };
42
43 static void ev_navigation_action_init       (EvNavigationAction *action);
44 static void ev_navigation_action_class_init (EvNavigationActionClass *class);
45
46 G_DEFINE_TYPE (EvNavigationAction, ev_navigation_action, GTK_TYPE_ACTION)
47
48 #define MAX_LABEL_LENGTH 48
49
50 #define EV_NAVIGATION_ACTION_GET_PRIVATE(object)(G_TYPE_INSTANCE_GET_PRIVATE ((object), EV_TYPE_NAVIGATION_ACTION, EvNavigationActionPrivate))
51
52 static void
53 ev_navigation_action_history_changed (EvHistory *history,
54                                       gpointer data)
55 {
56         EvNavigationAction *action = EV_NAVIGATION_ACTION (data);
57         
58         gtk_action_set_sensitive (GTK_ACTION (action),
59                                   ev_history_get_n_links (history) > 0);
60 }
61
62 void
63 ev_navigation_action_set_history (EvNavigationAction *action,
64                                   EvHistory          *history)
65 {
66         action->priv->history = history;
67
68         g_object_add_weak_pointer (G_OBJECT (action->priv->history),
69                                    (gpointer) &action->priv->history);
70         
71         g_signal_connect_object (history, "changed",
72                                  G_CALLBACK (ev_navigation_action_history_changed),
73                                  action, 0);
74 }
75
76 static void
77 activate_menu_item_cb (GtkWidget *widget, EvNavigationAction *action)
78 {
79         int index;
80
81         g_return_if_fail (EV_IS_HISTORY (action->priv->history));
82
83         index = GPOINTER_TO_INT (g_object_get_data (G_OBJECT (widget), "index"));
84         
85         if (action->priv->history) {
86                 EvLink *link;
87
88                 link = ev_history_get_link_nth (action->priv->history, index);
89                 
90                 g_signal_emit (action, widget_signals[WIDGET_ACTIVATE_LINK], 0, link);
91         }
92 }
93
94 static GtkWidget *
95 new_history_menu_item (EvNavigationAction *action,
96                        EvLink             *link,
97                        int                 index)
98 {
99         GtkLabel *label;
100         GtkWidget *item;
101         const char *title;
102
103         title = ev_link_get_title (link);
104         item = gtk_image_menu_item_new_with_label (title);
105         label = GTK_LABEL (gtk_bin_get_child (GTK_BIN (item)));
106         gtk_label_set_use_markup (label, TRUE);
107         g_object_set_data (G_OBJECT (item), "index",
108                            GINT_TO_POINTER (index));
109
110         gtk_label_set_ellipsize (label, PANGO_ELLIPSIZE_END);
111         gtk_label_set_max_width_chars (label, MAX_LABEL_LENGTH);
112
113         g_signal_connect (item, "activate",
114                           G_CALLBACK (activate_menu_item_cb),
115                           action);
116
117         gtk_widget_show (item);
118
119         return item;
120 }
121
122 static GtkWidget *
123 build_menu (EvNavigationAction *action)
124 {
125         GtkMenuShell *menu;
126         GtkWidget *item;
127         EvLink *link;
128         EvHistory *history = action->priv->history;
129         int start, end, i;
130
131         if (history == NULL || ev_history_get_n_links (history) <= 0) {
132                 return NULL;
133         }
134
135         menu = GTK_MENU_SHELL (gtk_menu_new ());
136
137         start = 0;
138         end = ev_history_get_n_links (history);
139
140         for (i = start; i < end; i++) {
141                 link = ev_history_get_link_nth (history, i);
142                 item = new_history_menu_item (action, link, i);
143                 gtk_menu_shell_prepend (menu, item);
144         }
145
146         return GTK_WIDGET (menu);
147 }
148
149 static void
150 menu_activated_cb (EvNavigationActionWidget *button,
151                    EvNavigationAction *action)
152 {
153         GtkWidget *menu;
154
155         menu = build_menu (action);
156         ev_navigation_action_widget_set_menu (button, menu);
157 }
158
159 static void
160 connect_proxy (GtkAction *action, GtkWidget *proxy)
161 {
162         GtkWidget *menu;
163
164         if (GTK_IS_TOOL_ITEM (proxy)) {
165                 /* set dummy menu so the arrow gets sensitive */
166                 menu = gtk_menu_new ();
167                 ev_navigation_action_widget_set_menu (EV_NAVIGATION_ACTION_WIDGET (proxy), menu);
168
169                 g_signal_connect (proxy, "show-menu",
170                                   G_CALLBACK (menu_activated_cb), action);
171         }
172
173         GTK_ACTION_CLASS (ev_navigation_action_parent_class)->connect_proxy (action, proxy);
174 }
175
176 static GtkWidget *
177 create_tool_item (GtkAction *action)
178 {
179         EvNavigationActionWidget *proxy;
180
181         proxy = g_object_new (EV_TYPE_NAVIGATION_ACTION_WIDGET, NULL);
182         gtk_widget_show (GTK_WIDGET (proxy));
183
184         return GTK_WIDGET (proxy);
185 }
186
187 static GtkWidget *
188 create_menu_item (GtkAction *action)
189 {
190         GtkWidget *menu;
191         GtkWidget *menu_item;
192
193         menu = build_menu (EV_NAVIGATION_ACTION (action));
194
195         menu_item = GTK_ACTION_CLASS (ev_navigation_action_parent_class)->create_menu_item (action);
196
197         gtk_menu_item_set_submenu (GTK_MENU_ITEM (menu_item), menu);
198
199         gtk_widget_show (menu_item);
200         
201         return menu_item;
202 }
203
204 static void
205 ev_navigation_action_init (EvNavigationAction *action)
206 {
207         action->priv = EV_NAVIGATION_ACTION_GET_PRIVATE (action);
208 }
209
210 static void
211 ev_navigation_action_finalize (GObject *object)
212 {
213         EvNavigationAction *action = EV_NAVIGATION_ACTION (object);
214
215         if (action->priv->history) {
216                 g_object_remove_weak_pointer (G_OBJECT (action->priv->history),
217                                              (gpointer) &action->priv->history);
218                 action->priv->history = NULL;
219         }
220
221         G_OBJECT_CLASS (ev_navigation_action_parent_class)->finalize (object);
222 }
223
224 static void
225 ev_navigation_action_class_init (EvNavigationActionClass *class)
226 {
227         GObjectClass *object_class = G_OBJECT_CLASS (class);
228         GtkActionClass *action_class = GTK_ACTION_CLASS (class);
229
230         object_class->finalize = ev_navigation_action_finalize;
231
232         action_class->toolbar_item_type = GTK_TYPE_TOOL_ITEM;
233         action_class->create_tool_item = create_tool_item;
234         action_class->connect_proxy = connect_proxy;
235         action_class->create_menu_item = create_menu_item;
236
237         widget_signals[WIDGET_ACTIVATE_LINK] = g_signal_new ("activate_link",
238                                                G_OBJECT_CLASS_TYPE (object_class),
239                                                G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
240                                                G_STRUCT_OFFSET (EvNavigationActionClass, activate_link),
241                                                NULL, NULL,
242                                                g_cclosure_marshal_VOID__OBJECT,
243                                                G_TYPE_NONE, 1,
244                                                G_TYPE_OBJECT);
245
246         g_type_class_add_private (object_class, sizeof (EvNavigationActionPrivate));
247 }