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 "ev-page-action.h"
25 #include "ev-window.h"
26 #include "ev-document-links.h"
28 #include <glib/gi18n.h>
29 #include <gtk/gtkentry.h>
30 #include <gtk/gtktoolitem.h>
31 #include <gtk/gtklabel.h>
32 #include <gtk/gtkhbox.h>
35 typedef struct _EvPageActionWidget EvPageActionWidget;
36 typedef struct _EvPageActionWidgetClass EvPageActionWidgetClass;
37 struct _EvPageActionWidget
42 EvPageCache *page_cache;
44 GtkTreeModel *filter_model;
48 struct _EvPageActionWidgetClass
50 GtkToolItemClass parent_class;
53 struct _EvPageActionPrivate
55 EvPageCache *page_cache;
60 /* Widget we pass back */
61 static GType ev_page_action_widget_get_type (void);
62 static void ev_page_action_widget_init (EvPageActionWidget *action_widget);
63 static void ev_page_action_widget_class_init (EvPageActionWidgetClass *action_widget);
65 G_DEFINE_TYPE (EvPageActionWidget, ev_page_action_widget, GTK_TYPE_TOOL_ITEM)
68 ev_page_action_widget_init (EvPageActionWidget *action_widget)
74 ev_page_action_widget_class_init (EvPageActionWidgetClass *action_widget)
80 static void ev_page_action_init (EvPageAction *action);
81 static void ev_page_action_class_init (EvPageActionClass *class);
83 G_DEFINE_TYPE (EvPageAction, ev_page_action, GTK_TYPE_ACTION)
85 #define EV_PAGE_ACTION_GET_PRIVATE(object)(G_TYPE_INSTANCE_GET_PRIVATE ((object), EV_TYPE_PAGE_ACTION, EvPageActionPrivate))
93 /* user data to set on the widget. */
94 #define EPA_FILTER_MODEL_DATA "epa-filter-model"
97 page_changed_cb (EvPageCache *page_cache,
99 EvPageActionWidget *proxy)
103 if (page_cache != NULL && page >= 0) {
104 gchar *page_label = ev_page_cache_get_page_label (page_cache, page);
105 gtk_entry_set_text (GTK_ENTRY (proxy->entry), page_label);
106 gtk_editable_set_position (GTK_EDITABLE (proxy->entry), -1);
109 gtk_entry_set_text (GTK_ENTRY (proxy->entry), "");
114 activate_cb (GtkWidget *entry, GtkAction *action)
116 EvPageAction *page = EV_PAGE_ACTION (action);
117 EvPageCache *page_cache;
120 text = gtk_entry_get_text (GTK_ENTRY (entry));
121 page_cache = page->priv->page_cache;
123 if (! ev_page_cache_set_page_label (page_cache, text)) {
124 /* rest the entry to the current page if we were unable to
127 ev_page_cache_get_page_label (page_cache,
128 ev_page_cache_get_current_page (page_cache));
129 gtk_entry_set_text (GTK_ENTRY (entry), page_label);
130 gtk_editable_set_position (GTK_EDITABLE (entry), -1);
136 create_tool_item (GtkAction *action)
138 EvPageActionWidget *proxy;
140 proxy = g_object_new (ev_page_action_widget_get_type (), NULL);
141 gtk_container_set_border_width (GTK_CONTAINER (proxy), 6);
142 gtk_widget_show (GTK_WIDGET (proxy));
144 proxy->entry = gtk_entry_new ();
145 gtk_entry_set_width_chars (GTK_ENTRY (proxy->entry), 5);
146 gtk_widget_show (proxy->entry);
148 g_signal_connect (proxy->entry, "activate",
149 G_CALLBACK (activate_cb),
152 gtk_container_add (GTK_CONTAINER (proxy), proxy->entry);
154 return GTK_WIDGET (proxy);
158 update_page_cache (EvPageAction *page, GParamSpec *pspec, EvPageActionWidget *proxy)
160 EvPageCache *page_cache;
163 page_cache = page->priv->page_cache;
165 /* clear the old signal */
166 if (proxy->signal_id > 0 && proxy->page_cache)
167 g_signal_handler_disconnect (proxy->page_cache, proxy->signal_id);
169 if (page_cache != NULL) {
170 signal_id = g_signal_connect (page_cache,
172 G_CALLBACK (page_changed_cb),
174 /* Set the initial value */
175 page_changed_cb (page_cache,
176 ev_page_cache_get_current_page (page_cache),
179 /* Or clear the entry */
181 page_changed_cb (NULL, 0, proxy);
183 proxy->page_cache = page_cache;
184 proxy->signal_id = signal_id;
188 build_new_tree_cb (GtkTreeModel *model,
193 GtkTreeModel *filter_model = GTK_TREE_MODEL (data);
196 gtk_tree_model_get (model, iter,
197 EV_DOCUMENT_LINKS_COLUMN_LINK, &link,
200 if (link && ev_link_get_link_type (link) == EV_LINK_TYPE_PAGE) {
201 GtkTreeIter filter_iter;
203 gtk_list_store_append (GTK_LIST_STORE (filter_model), &filter_iter);
204 gtk_list_store_set (GTK_LIST_STORE (filter_model), &filter_iter,
212 static GtkTreeModel *
213 get_filter_model_from_model (GtkTreeModel *model)
215 GtkTreeModel *filter_model;
218 (GtkTreeModel *) g_object_get_data (G_OBJECT (model), EPA_FILTER_MODEL_DATA);
219 if (filter_model == NULL) {
220 filter_model = (GtkTreeModel *) gtk_list_store_new (1, GTK_TYPE_TREE_ITER);
222 gtk_tree_model_foreach (model,
225 g_object_set_data_full (G_OBJECT (model), EPA_FILTER_MODEL_DATA, filter_model, g_object_unref);
232 match_selected_cb (GtkEntryCompletion *completion,
233 GtkTreeModel *filter_model,
234 GtkTreeIter *filter_iter,
235 EvPageActionWidget *proxy)
240 gtk_tree_model_get (filter_model, filter_iter,
243 gtk_tree_model_get (proxy->model, iter,
244 EV_DOCUMENT_LINKS_COLUMN_LINK, &link,
246 ev_page_cache_set_link (proxy->page_cache, link);
253 display_completion_text (GtkCellLayout *cell_layout,
254 GtkCellRenderer *renderer,
255 GtkTreeModel *filter_model,
256 GtkTreeIter *filter_iter,
257 EvPageActionWidget *proxy)
262 gtk_tree_model_get (filter_model, filter_iter,
265 gtk_tree_model_get (proxy->model, iter,
266 EV_DOCUMENT_LINKS_COLUMN_LINK, &link,
269 g_object_set (renderer, "text", ev_link_get_title (link), NULL);
273 match_completion (GtkEntryCompletion *completion,
275 GtkTreeIter *filter_iter,
276 EvPageActionWidget *proxy)
280 const gchar *text = NULL;
282 gtk_tree_model_get (gtk_entry_completion_get_model (completion),
286 gtk_tree_model_get (proxy->model, iter,
287 EV_DOCUMENT_LINKS_COLUMN_LINK, &link,
292 text = ev_link_get_title (link);
295 gchar *normalized_text;
296 gchar *normalized_key;
297 gchar *case_normalized_text;
298 gchar *case_normalized_key;
299 gboolean retval = FALSE;
301 normalized_text = g_utf8_normalize (text, -1, G_NORMALIZE_ALL);
302 normalized_key = g_utf8_normalize (key, -1, G_NORMALIZE_ALL);
303 case_normalized_text = g_utf8_casefold (normalized_text, -1);
304 case_normalized_key = g_utf8_casefold (normalized_key, -1);
306 if (strstr (case_normalized_text, case_normalized_key))
309 g_free (normalized_text);
310 g_free (normalized_key);
311 g_free (case_normalized_text);
312 g_free (case_normalized_key);
322 update_model (EvPageAction *page, GParamSpec *pspec, EvPageActionWidget *proxy)
325 GtkTreeModel *filter_model;
327 g_object_get (G_OBJECT (page),
332 GtkEntryCompletion *completion;
333 GtkCellRenderer *renderer;
335 proxy->model = model;
336 filter_model = get_filter_model_from_model (model);
338 completion = gtk_entry_completion_new ();
340 /* popup-set-width is 2.7.0 only */
341 g_object_set (G_OBJECT (completion),
342 "popup-set-width", FALSE,
343 "model", filter_model,
346 g_signal_connect (completion, "match-selected", G_CALLBACK (match_selected_cb), proxy);
347 gtk_entry_completion_set_match_func (completion,
348 (GtkEntryCompletionMatchFunc) match_completion,
351 /* Set up the layout */
352 renderer = (GtkCellRenderer *)
353 g_object_new (GTK_TYPE_CELL_RENDERER_TEXT,
354 "ellipsize", PANGO_ELLIPSIZE_END,
357 gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (completion), renderer, TRUE);
358 gtk_cell_layout_set_cell_data_func (GTK_CELL_LAYOUT (completion),
360 (GtkCellLayoutDataFunc) display_completion_text,
362 gtk_entry_set_completion (GTK_ENTRY (proxy->entry), completion);
367 connect_proxy (GtkAction *action, GtkWidget *proxy)
369 if (GTK_IS_TOOL_ITEM (proxy)) {
370 g_signal_connect_object (action, "notify::page-cache",
371 G_CALLBACK (update_page_cache),
373 /* We only go through this whole rigmarole if we can set
374 * GtkEntryCompletion::popup-set-width, which appeared in
376 if (gtk_check_version (2, 7, 0) == NULL) {
377 g_signal_connect_object (action, "notify::model",
378 G_CALLBACK (update_model),
383 GTK_ACTION_CLASS (ev_page_action_parent_class)->connect_proxy (action, proxy);
387 ev_page_action_dispose (GObject *object)
389 EvPageAction *page = EV_PAGE_ACTION (object);
391 if (page->priv->page_cache) {
392 g_object_unref (page->priv->page_cache);
393 page->priv->page_cache = NULL;
396 G_OBJECT_CLASS (ev_page_action_parent_class)->dispose (object);
400 ev_page_action_set_property (GObject *object,
406 EvPageCache *page_cache;
409 page = EV_PAGE_ACTION (object);
413 case PROP_PAGE_CACHE:
414 page_cache = page->priv->page_cache;
415 page->priv->page_cache = EV_PAGE_CACHE (g_value_dup_object (value));
417 g_object_unref (page_cache);
420 model = page->priv->model;
421 page->priv->model = GTK_TREE_MODEL (g_value_dup_object (value));
423 g_object_unref (model);
426 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
432 ev_page_action_get_property (GObject *object,
439 page = EV_PAGE_ACTION (object);
443 case PROP_PAGE_CACHE:
444 g_value_set_object (value, page->priv->page_cache);
447 g_value_set_object (value, page->priv->model);
450 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
456 ev_page_action_set_document (EvPageAction *page, EvDocument *document)
458 EvPageCache *page_cache = NULL;
461 page_cache = ev_document_get_page_cache (document);
464 "page-cache", page_cache,
470 ev_page_action_set_model (EvPageAction *page_action,
473 g_object_set (page_action,
479 ev_page_action_init (EvPageAction *page)
481 page->priv = EV_PAGE_ACTION_GET_PRIVATE (page);
485 ev_page_action_class_init (EvPageActionClass *class)
487 GObjectClass *object_class = G_OBJECT_CLASS (class);
488 GtkActionClass *action_class = GTK_ACTION_CLASS (class);
490 object_class->dispose = ev_page_action_dispose;
491 object_class->set_property = ev_page_action_set_property;
492 object_class->get_property = ev_page_action_get_property;
494 action_class->toolbar_item_type = GTK_TYPE_TOOL_ITEM;
495 action_class->create_tool_item = create_tool_item;
496 action_class->connect_proxy = connect_proxy;
498 g_object_class_install_property (object_class,
500 g_param_spec_object ("page-cache",
502 "Current page cache",
506 g_object_class_install_property (object_class,
508 g_param_spec_object ("model",
514 g_type_class_add_private (object_class, sizeof (EvPageActionPrivate));