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