2 * Copyright (C) 2003, 2004 Marco Pesenti Gritti
3 * Copyright (C) 2003, 2004 Christian Persch
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)
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.
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.
24 #include <glib/gi18n.h>
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"
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);
44 static guint widget_signals[WIDGET_N_SIGNALS] = {0, };
46 G_DEFINE_TYPE (EvPageActionWidget, ev_page_action_widget, GTK_TYPE_TOOL_ITEM)
49 ev_page_action_widget_init (EvPageActionWidget *action_widget)
55 ev_page_action_widget_set_page_cache (EvPageActionWidget *action_widget,
56 EvPageCache *page_cache)
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;
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);
72 ev_page_action_widget_finalize (GObject *object)
74 EvPageActionWidget *action_widget = EV_PAGE_ACTION_WIDGET (object);
76 ev_page_action_widget_set_page_cache (action_widget, NULL);
78 G_OBJECT_CLASS (ev_page_action_widget_parent_class)->finalize (object);
82 ev_page_action_widget_class_init (EvPageActionWidgetClass *class)
84 GObjectClass *object_class = G_OBJECT_CLASS (class);
86 object_class->finalize = ev_page_action_widget_finalize;
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),
93 g_cclosure_marshal_VOID__OBJECT,
100 match_selected_cb (GtkEntryCompletion *completion,
101 GtkTreeModel *filter_model,
102 GtkTreeIter *filter_iter,
103 EvPageActionWidget *proxy)
108 gtk_tree_model_get (filter_model, filter_iter,
111 gtk_tree_model_get (proxy->model, iter,
112 EV_DOCUMENT_LINKS_COLUMN_LINK, &link,
115 g_signal_emit (proxy, widget_signals[WIDGET_ACTIVATE_LINK], 0, link);
118 g_object_unref (link);
120 gtk_tree_iter_free (iter);
127 display_completion_text (GtkCellLayout *cell_layout,
128 GtkCellRenderer *renderer,
129 GtkTreeModel *filter_model,
130 GtkTreeIter *filter_iter,
131 EvPageActionWidget *proxy)
136 gtk_tree_model_get (filter_model, filter_iter,
139 gtk_tree_model_get (proxy->model, iter,
140 EV_DOCUMENT_LINKS_COLUMN_LINK, &link,
143 g_object_set (renderer, "text", ev_link_get_title (link), NULL);
146 g_object_unref (link);
148 gtk_tree_iter_free (iter);
152 match_completion (GtkEntryCompletion *completion,
154 GtkTreeIter *filter_iter,
155 EvPageActionWidget *proxy)
159 const gchar *text = NULL;
161 gtk_tree_model_get (gtk_entry_completion_get_model (completion),
165 gtk_tree_model_get (proxy->model, iter,
166 EV_DOCUMENT_LINKS_COLUMN_LINK, &link,
171 text = ev_link_get_title (link);
172 g_object_unref (link);
175 gtk_tree_iter_free (iter);
178 gchar *normalized_text;
179 gchar *normalized_key;
180 gchar *case_normalized_text;
181 gchar *case_normalized_key;
182 gboolean retval = FALSE;
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);
189 if (strstr (case_normalized_text, case_normalized_key))
192 g_free (normalized_text);
193 g_free (normalized_key);
194 g_free (case_normalized_text);
195 g_free (case_normalized_key);
203 /* user data to set on the widget. */
204 #define EPA_FILTER_MODEL_DATA "epa-filter-model"
207 build_new_tree_cb (GtkTreeModel *model,
212 GtkTreeModel *filter_model = GTK_TREE_MODEL (data);
214 EvLinkAction *action;
215 EvLinkActionType type;
217 gtk_tree_model_get (model, iter,
218 EV_DOCUMENT_LINKS_COLUMN_LINK, &link,
224 action = ev_link_get_action (link);
226 g_object_unref (link);
230 type = ev_link_action_get_action_type (action);
232 if (type == EV_LINK_ACTION_TYPE_GOTO_DEST) {
233 GtkTreeIter filter_iter;
235 gtk_list_store_append (GTK_LIST_STORE (filter_model), &filter_iter);
236 gtk_list_store_set (GTK_LIST_STORE (filter_model), &filter_iter,
241 g_object_unref (link);
246 static GtkTreeModel *
247 get_filter_model_from_model (GtkTreeModel *model)
249 GtkTreeModel *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);
256 gtk_tree_model_foreach (model,
259 g_object_set_data_full (G_OBJECT (model), EPA_FILTER_MODEL_DATA, filter_model, g_object_unref);
267 ev_page_action_widget_update_model (EvPageActionWidget *proxy, GtkTreeModel *model)
269 GtkTreeModel *filter_model;
273 GtkEntryCompletion *completion;
274 GtkCellRenderer *renderer;
276 proxy->model = model;
277 filter_model = get_filter_model_from_model (model);
279 completion = gtk_entry_completion_new ();
281 g_object_set (G_OBJECT (completion),
282 "popup-set-width", FALSE,
283 "model", filter_model,
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,
291 /* Set up the layout */
292 renderer = (GtkCellRenderer *)
293 g_object_new (GTK_TYPE_CELL_RENDERER_TEXT,
294 "ellipsize", PANGO_ELLIPSIZE_END,
297 gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (completion), renderer, TRUE);
298 gtk_cell_layout_set_cell_data_func (GTK_CELL_LAYOUT (completion),
300 (GtkCellLayoutDataFunc) display_completion_text,
302 gtk_entry_set_completion (GTK_ENTRY (proxy->entry), completion);
304 g_object_unref (completion);
305 g_object_unref (model);