]> www.fi.muni.cz Git - evince.git/blob - shell/ev-page-action-widget.c
7a6ad3b4d241dce5eb51c0a3b9e175f4474fe42d
[evince.git] / shell / ev-page-action-widget.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 <string.h>
24 #include <glib/gi18n.h>
25 #include <gtk/gtk.h>
26
27 #include "ev-document-links.h"
28 #include "ev-marshal.h"
29 #include "ev-page-action.h"
30 #include "ev-page-action-widget.h"
31 #include "ev-page-cache.h"
32 #include "ev-window.h"
33
34 /* Widget we pass back */
35 static void  ev_page_action_widget_init       (EvPageActionWidget      *action_widget);
36 static void  ev_page_action_widget_class_init (EvPageActionWidgetClass *action_widget);
37
38 enum
39 {
40         WIDGET_ACTIVATE_LINK,
41         WIDGET_N_SIGNALS
42 };
43
44 static guint widget_signals[WIDGET_N_SIGNALS] = {0, };
45
46 G_DEFINE_TYPE (EvPageActionWidget, ev_page_action_widget, GTK_TYPE_TOOL_ITEM)
47
48 static void
49 ev_page_action_widget_init (EvPageActionWidget *action_widget)
50 {
51         return;
52 }
53
54 void
55 ev_page_action_widget_set_page_cache (EvPageActionWidget *action_widget,
56                                       EvPageCache        *page_cache)
57 {
58         if (action_widget->page_cache != NULL) {
59                 g_object_remove_weak_pointer (G_OBJECT (action_widget->page_cache),
60                                               (gpointer)&action_widget->page_cache);
61                 action_widget->page_cache = NULL;
62         }
63
64         if (page_cache != NULL) {
65                 action_widget->page_cache = page_cache;
66                 g_object_add_weak_pointer (G_OBJECT (page_cache),
67                                            (gpointer)&action_widget->page_cache);
68         }
69 }
70
71 static void
72 ev_page_action_widget_finalize (GObject *object)
73 {
74         EvPageActionWidget *action_widget = EV_PAGE_ACTION_WIDGET (object);
75
76         ev_page_action_widget_set_page_cache (action_widget, NULL);
77
78         G_OBJECT_CLASS (ev_page_action_widget_parent_class)->finalize (object);
79 }
80
81 static void
82 ev_page_action_widget_class_init (EvPageActionWidgetClass *class)
83 {
84         GObjectClass *object_class = G_OBJECT_CLASS (class);
85
86         object_class->finalize = ev_page_action_widget_finalize;
87
88         widget_signals[WIDGET_ACTIVATE_LINK] = g_signal_new ("activate_link",
89                                                G_OBJECT_CLASS_TYPE (object_class),
90                                                G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
91                                                G_STRUCT_OFFSET (EvPageActionClass, activate_link),
92                                                NULL, NULL,
93                                                g_cclosure_marshal_VOID__OBJECT,
94                                                G_TYPE_NONE, 1,
95                                                G_TYPE_OBJECT);
96
97 }
98
99 static gboolean
100 match_selected_cb (GtkEntryCompletion *completion,
101                    GtkTreeModel       *filter_model,
102                    GtkTreeIter        *filter_iter,
103                    EvPageActionWidget *proxy)
104 {
105         EvLink *link;
106         GtkTreeIter *iter;
107
108         gtk_tree_model_get (filter_model, filter_iter,
109                             0, &iter,
110                             -1);
111         gtk_tree_model_get (proxy->model, iter,
112                             EV_DOCUMENT_LINKS_COLUMN_LINK, &link,
113                             -1);
114
115         g_signal_emit (proxy, widget_signals[WIDGET_ACTIVATE_LINK], 0, link);
116
117         if (link)
118                 g_object_unref (link);
119
120         gtk_tree_iter_free (iter);
121         
122         return TRUE;
123 }
124                    
125
126 static void
127 display_completion_text (GtkCellLayout      *cell_layout,
128                          GtkCellRenderer    *renderer,
129                          GtkTreeModel       *filter_model,
130                          GtkTreeIter        *filter_iter,
131                          EvPageActionWidget *proxy)
132 {
133         EvLink *link;
134         GtkTreeIter *iter;
135
136         gtk_tree_model_get (filter_model, filter_iter,
137                             0, &iter,
138                             -1);
139         gtk_tree_model_get (proxy->model, iter,
140                             EV_DOCUMENT_LINKS_COLUMN_LINK, &link,
141                             -1);
142
143         g_object_set (renderer, "text", ev_link_get_title (link), NULL);
144
145         if (link)
146                 g_object_unref (link);
147         
148         gtk_tree_iter_free (iter);
149 }
150
151 static gboolean
152 match_completion (GtkEntryCompletion *completion,
153                   const gchar        *key,
154                   GtkTreeIter        *filter_iter,
155                   EvPageActionWidget *proxy)
156 {
157         EvLink *link;
158         GtkTreeIter *iter;
159         const gchar *text = NULL;
160
161         gtk_tree_model_get (gtk_entry_completion_get_model (completion),
162                             filter_iter,
163                             0, &iter,
164                             -1);
165         gtk_tree_model_get (proxy->model, iter,
166                             EV_DOCUMENT_LINKS_COLUMN_LINK, &link,
167                             -1);
168
169
170         if (link) {
171                 text = ev_link_get_title (link);
172                 g_object_unref (link);
173         }
174
175         gtk_tree_iter_free (iter);
176
177         if (text && key) {
178                 gchar *normalized_text;
179                 gchar *normalized_key;
180                 gchar *case_normalized_text;
181                 gchar *case_normalized_key;
182                 gboolean retval = FALSE;
183
184                 normalized_text = g_utf8_normalize (text, -1, G_NORMALIZE_ALL);
185                 normalized_key = g_utf8_normalize (key, -1, G_NORMALIZE_ALL);
186                 case_normalized_text = g_utf8_casefold (normalized_text, -1);
187                 case_normalized_key = g_utf8_casefold (normalized_key, -1);
188
189                 if (strstr (case_normalized_text, case_normalized_key))
190                         retval = TRUE;
191
192                 g_free (normalized_text);
193                 g_free (normalized_key);
194                 g_free (case_normalized_text);
195                 g_free (case_normalized_key);
196
197                 return retval;
198         }
199
200         return FALSE;
201 }
202
203 /* user data to set on the widget. */
204 #define EPA_FILTER_MODEL_DATA "epa-filter-model"
205
206 static gboolean
207 build_new_tree_cb (GtkTreeModel *model,
208                    GtkTreePath  *path,
209                    GtkTreeIter  *iter,
210                    gpointer      data)
211 {
212         GtkTreeModel *filter_model = GTK_TREE_MODEL (data);
213         EvLink *link;
214         EvLinkAction *action;
215         EvLinkActionType type;
216
217         gtk_tree_model_get (model, iter,
218                             EV_DOCUMENT_LINKS_COLUMN_LINK, &link,
219                             -1);
220
221         if (!link)
222                 return FALSE;
223
224         action = ev_link_get_action (link);
225         if (!action) {
226                 g_object_unref (link);
227                 return FALSE;
228         }
229         
230         type = ev_link_action_get_action_type (action);
231
232         if (type == EV_LINK_ACTION_TYPE_GOTO_DEST) {
233                 GtkTreeIter filter_iter;
234
235                 gtk_list_store_append (GTK_LIST_STORE (filter_model), &filter_iter);
236                 gtk_list_store_set (GTK_LIST_STORE (filter_model), &filter_iter,
237                                     0, iter,
238                                     -1);
239         }
240         
241         g_object_unref (link);
242         
243         return FALSE;
244 }
245
246 static GtkTreeModel *
247 get_filter_model_from_model (GtkTreeModel *model)
248 {
249         GtkTreeModel *filter_model;
250
251         filter_model =
252                 (GtkTreeModel *) g_object_get_data (G_OBJECT (model), EPA_FILTER_MODEL_DATA);
253         if (filter_model == NULL) {
254                 filter_model = (GtkTreeModel *) gtk_list_store_new (1, GTK_TYPE_TREE_ITER);
255
256                 gtk_tree_model_foreach (model,
257                                         build_new_tree_cb,
258                                         filter_model);
259                 g_object_set_data_full (G_OBJECT (model), EPA_FILTER_MODEL_DATA, filter_model, g_object_unref);
260         }
261
262         return filter_model;
263 }
264
265
266 void
267 ev_page_action_widget_update_model (EvPageActionWidget *proxy, GtkTreeModel *model)
268 {
269         GtkTreeModel *filter_model;
270
271         if (model != NULL) {
272                 /* Magik */
273                 GtkEntryCompletion *completion;
274                 GtkCellRenderer *renderer;
275
276                 proxy->model = model;
277                 filter_model = get_filter_model_from_model (model);
278
279                 completion = gtk_entry_completion_new ();
280
281                 g_object_set (G_OBJECT (completion),
282                               "popup-set-width", FALSE,
283                               "model", filter_model,
284                               NULL);
285
286                 g_signal_connect (completion, "match-selected", G_CALLBACK (match_selected_cb), proxy);
287                 gtk_entry_completion_set_match_func (completion,
288                                                      (GtkEntryCompletionMatchFunc) match_completion,
289                                                      proxy, NULL);
290
291                 /* Set up the layout */
292                 renderer = (GtkCellRenderer *)
293                         g_object_new (GTK_TYPE_CELL_RENDERER_TEXT,
294                                       "ellipsize", PANGO_ELLIPSIZE_END,
295                                       "width_chars", 30,
296                                       NULL);
297                 gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (completion), renderer, TRUE);
298                 gtk_cell_layout_set_cell_data_func (GTK_CELL_LAYOUT (completion),
299                                                     renderer,
300                                                     (GtkCellLayoutDataFunc) display_completion_text,
301                                                     proxy, NULL);
302                 gtk_entry_set_completion (GTK_ENTRY (proxy->entry), completion);
303                 
304                 g_object_unref (completion);
305                 g_object_unref (model);
306         }
307 }