]> www.fi.muni.cz Git - evince.git/blob - shell/ev-navigation-action.c
Implement history dropdowns
[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  *  $Id$
20  */
21
22 #include "config.h"
23
24 #include "ev-navigation-action.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
35 struct _EvNavigationActionPrivate
36 {
37         EvWindow *window;
38         EvNavigationDirection direction;
39         char *arrow_tooltip;
40         EvHistory *history;
41 };
42
43 enum
44 {
45         PROP_0,
46         PROP_ARROW_TOOLTIP,
47         PROP_DIRECTION
48 };
49
50 static void ev_navigation_action_init       (EvNavigationAction *action);
51 static void ev_navigation_action_class_init (EvNavigationActionClass *class);
52
53 static GObjectClass *parent_class = NULL;
54
55 G_DEFINE_TYPE (EvNavigationAction, ev_navigation_action, GTK_TYPE_ACTION)
56
57 #define MAX_LABEL_LENGTH 48
58
59 #define EV_NAVIGATION_ACTION_GET_PRIVATE(object)(G_TYPE_INSTANCE_GET_PRIVATE ((object), EV_TYPE_NAVIGATION_ACTION, EvNavigationActionPrivate))
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
71 static void
72 activate_menu_item_cb (GtkWidget *widget, EvNavigationAction *action)
73 {
74         int index;
75
76         g_return_if_fail (EV_IS_HISTORY (action->priv->history));
77
78         index = GPOINTER_TO_INT (g_object_get_data (G_OBJECT (widget), "index"));
79         ev_history_set_current_index (action->priv->history, index);
80 }
81
82 static GtkWidget *
83 new_history_menu_item (EvNavigationAction *action,
84                        EvLink             *link,
85                        int                 index)
86 {
87         GtkLabel *label;
88         GtkWidget *item;
89         const char *title;
90
91         title = ev_link_get_title (link);
92         item = gtk_image_menu_item_new_with_label (title);
93         g_object_set_data (G_OBJECT (item), "index",
94                            GINT_TO_POINTER (index));
95
96         label = GTK_LABEL (GTK_BIN (item)->child);
97         gtk_label_set_ellipsize (label, PANGO_ELLIPSIZE_END);
98         gtk_label_set_max_width_chars (label, MAX_LABEL_LENGTH);
99
100         g_signal_connect (item, "activate",
101                           G_CALLBACK (activate_menu_item_cb),
102                           action);
103
104         gtk_widget_show (item);
105
106         return item;
107 }
108
109 static GtkWidget *
110 build_menu (EvNavigationAction *action)
111 {
112         GtkMenuShell *menu;
113         EvHistory *history = action->priv->history;
114         int start, end, i;
115
116         menu = GTK_MENU_SHELL (gtk_menu_new ());
117
118         if (history == NULL) {
119                 return GTK_WIDGET (menu);
120         }
121
122         if (action->priv->direction == EV_NAVIGATION_DIRECTION_BACK) {
123                 start = 0;
124                 end = ev_history_get_current_index (history);
125         } else {
126                 start = ev_history_get_current_index (history) + 1;
127                 end = ev_history_get_n_links (history);
128         }
129
130         for (i = start; i < end; i++) {
131                 EvLink *link = ev_history_get_link_nth (history, i);
132                 GtkWidget *item;
133
134                 item = new_history_menu_item (action, link, i);
135                 gtk_menu_shell_append (menu, item);
136         }
137
138         return GTK_WIDGET (menu);
139 }
140
141 static void
142 menu_activated_cb (GtkMenuToolButton *button,
143                    EvNavigationAction *action)
144 {
145         GtkWidget *menu;
146
147         menu = build_menu (action);
148         gtk_menu_tool_button_set_menu (button, menu);
149 }
150
151 static gboolean
152 set_tooltip_cb (GtkMenuToolButton *proxy,
153                 GtkTooltips *tooltips,
154                 const char *tip,
155                 const char *tip_private,
156                 EvNavigationAction *action)
157 {
158         gtk_menu_tool_button_set_arrow_tooltip (proxy, tooltips,
159                                                 action->priv->arrow_tooltip,
160                                                 NULL);
161
162         /* don't stop emission */
163         return FALSE;
164 }
165
166 static void
167 connect_proxy (GtkAction *action, GtkWidget *proxy)
168 {
169         if (GTK_IS_MENU_TOOL_BUTTON (proxy))
170         {
171                 GtkWidget *menu;
172
173                 /* set dummy menu so the arrow gets sensitive */
174                 menu = gtk_menu_new ();
175                 gtk_menu_tool_button_set_menu (GTK_MENU_TOOL_BUTTON (proxy), menu);
176
177                 g_signal_connect (proxy, "show-menu",
178                                   G_CALLBACK (menu_activated_cb), action);
179
180                 g_signal_connect (proxy, "set-tooltip",
181                                   G_CALLBACK (set_tooltip_cb), action);
182         }
183
184         GTK_ACTION_CLASS (parent_class)->connect_proxy (action, proxy);
185 }
186
187 static void
188 ev_navigation_action_init (EvNavigationAction *action)
189 {
190         action->priv = EV_NAVIGATION_ACTION_GET_PRIVATE (action);
191 }
192
193 static void
194 ev_navigation_action_finalize (GObject *object)
195 {
196         EvNavigationAction *action = EV_NAVIGATION_ACTION (object);
197
198         if (action->priv->history) {
199                 g_object_add_weak_pointer (G_OBJECT (action->priv->history),
200                                            (gpointer *) &action->priv->history);
201         }
202
203         g_free (action->priv->arrow_tooltip);
204
205         parent_class->finalize (object);
206 }
207
208 static void
209 ev_navigation_action_set_property (GObject *object,
210                                      guint prop_id,
211                                      const GValue *value,
212                                      GParamSpec *pspec)
213 {
214         EvNavigationAction *nav = EV_NAVIGATION_ACTION (object);
215
216         switch (prop_id)
217         {
218                 case PROP_ARROW_TOOLTIP:
219                         nav->priv->arrow_tooltip = g_value_dup_string (value);
220                         g_object_notify (object, "tooltip");
221                         break;
222                 case PROP_DIRECTION:
223                         nav->priv->direction = g_value_get_int (value);
224                         break;
225         }
226 }
227
228 static void
229 ev_navigation_action_get_property (GObject *object,
230                                      guint prop_id,
231                                      GValue *value,
232                                      GParamSpec *pspec)
233 {
234         EvNavigationAction *nav = EV_NAVIGATION_ACTION (object);
235
236         switch (prop_id)
237         {
238                 case PROP_ARROW_TOOLTIP:
239                         g_value_set_string (value, nav->priv->arrow_tooltip);
240                         break;
241                 case PROP_DIRECTION:
242                         g_value_set_int (value, nav->priv->direction);
243                         break;
244         }
245 }
246
247 static void
248 ev_navigation_action_class_init (EvNavigationActionClass *class)
249 {
250         GObjectClass *object_class = G_OBJECT_CLASS (class);
251         GtkActionClass *action_class = GTK_ACTION_CLASS (class);
252
253         object_class->finalize = ev_navigation_action_finalize;
254         object_class->set_property = ev_navigation_action_set_property;
255         object_class->get_property = ev_navigation_action_get_property;
256
257         parent_class = g_type_class_peek_parent (class);
258
259         action_class->toolbar_item_type = GTK_TYPE_MENU_TOOL_BUTTON;
260         action_class->connect_proxy = connect_proxy;
261
262         g_object_class_install_property (object_class,
263                                          PROP_ARROW_TOOLTIP,
264                                          g_param_spec_string ("arrow-tooltip",
265                                                               "Arrow Tooltip",
266                                                               "Arrow Tooltip",
267                                                               NULL,
268                                                               G_PARAM_READWRITE));
269
270         g_object_class_install_property (object_class,
271                                          PROP_DIRECTION,
272                                          g_param_spec_int ("direction",
273                                                            "Direction",
274                                                            "Direction",
275                                                            0,
276                                                            G_MAXINT,
277                                                            0,
278                                                            G_PARAM_READWRITE));
279
280         g_type_class_add_private (object_class, sizeof (EvNavigationActionPrivate));
281 }