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