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-page-cache.h"
26 #include "ev-window.h"
27 #include "ev-document-links.h"
28 #include "ev-marshal.h"
30 #include <glib/gi18n.h>
31 #include <gtk/gtkentry.h>
32 #include <gtk/gtktoolitem.h>
33 #include <gtk/gtklabel.h>
34 #include <gtk/gtkhbox.h>
37 typedef struct _EvPageActionWidget EvPageActionWidget;
38 typedef struct _EvPageActionWidgetClass EvPageActionWidgetClass;
39 struct _EvPageActionWidget
45 EvPageCache *page_cache;
47 GtkTreeModel *filter_model;
51 struct _EvPageActionWidgetClass
53 GtkToolItemClass parent_class;
55 void (* activate_link) (EvPageActionWidget *page_action,
59 struct _EvPageActionPrivate
61 EvPageCache *page_cache;
66 /* Widget we pass back */
67 static GType ev_page_action_widget_get_type (void);
68 static void ev_page_action_widget_init (EvPageActionWidget *action_widget);
69 static void ev_page_action_widget_class_init (EvPageActionWidgetClass *action_widget);
71 #define EV_TYPE_PAGE_ACTION_WIDGET (ev_page_action_widget_get_type ())
72 #define EV_PAGE_ACTION_WIDGET(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), EV_TYPE_PAGE_ACTION_WIDGET, EvPageActionWidget))
80 static guint widget_signals[WIDGET_N_SIGNALS] = {0, };
82 G_DEFINE_TYPE (EvPageActionWidget, ev_page_action_widget, GTK_TYPE_TOOL_ITEM)
85 ev_page_action_widget_init (EvPageActionWidget *action_widget)
91 ev_page_action_widget_set_page_cache (EvPageActionWidget *action_widget,
92 EvPageCache *page_cache)
94 if (action_widget->page_cache != NULL) {
95 g_object_remove_weak_pointer (G_OBJECT (action_widget->page_cache),
96 (gpointer *)&action_widget->page_cache);
97 action_widget->page_cache = NULL;
100 if (page_cache != NULL) {
101 action_widget->page_cache = page_cache;
102 g_object_add_weak_pointer (G_OBJECT (page_cache),
103 (gpointer *)&action_widget->page_cache);
108 ev_page_action_widget_finalize (GObject *object)
110 EvPageActionWidget *action_widget = EV_PAGE_ACTION_WIDGET (object);
112 ev_page_action_widget_set_page_cache (action_widget, NULL);
116 ev_page_action_widget_class_init (EvPageActionWidgetClass *class)
118 GObjectClass *object_class = G_OBJECT_CLASS (class);
120 object_class->finalize = ev_page_action_widget_finalize;
122 widget_signals[WIDGET_ACTIVATE_LINK] = g_signal_new ("activate_link",
123 G_OBJECT_CLASS_TYPE (object_class),
124 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
125 G_STRUCT_OFFSET (EvPageActionClass, activate_link),
127 g_cclosure_marshal_VOID__OBJECT,
133 static void ev_page_action_init (EvPageAction *action);
134 static void ev_page_action_class_init (EvPageActionClass *class);
143 static guint signals[N_SIGNALS] = {0, };
145 G_DEFINE_TYPE (EvPageAction, ev_page_action, GTK_TYPE_ACTION)
147 #define EV_PAGE_ACTION_GET_PRIVATE(object)(G_TYPE_INSTANCE_GET_PRIVATE ((object), EV_TYPE_PAGE_ACTION, EvPageActionPrivate))
155 /* user data to set on the widget. */
156 #define EPA_FILTER_MODEL_DATA "epa-filter-model"
159 update_pages_label (EvPageActionWidget *proxy,
161 EvPageCache *page_cache)
166 n_pages = page_cache ? ev_page_cache_get_n_pages (page_cache) : 0;
167 if (page_cache && ev_page_cache_has_nonnumeric_page_labels (page_cache)) {
168 label_text = g_strdup_printf (_("(%d of %d)"), page + 1, n_pages);
170 label_text = g_strdup_printf (_("of %d"), n_pages);
172 gtk_label_set_text (GTK_LABEL (proxy->label), label_text);
177 page_changed_cb (EvPageCache *page_cache,
179 EvPageActionWidget *proxy)
183 if (page_cache != NULL && page >= 0) {
186 gtk_entry_set_width_chars (GTK_ENTRY (proxy->entry),
187 CLAMP (ev_page_cache_get_max_label_chars (page_cache),
190 page_label = ev_page_cache_get_page_label (page_cache, page);
191 gtk_entry_set_text (GTK_ENTRY (proxy->entry), page_label);
192 gtk_editable_set_position (GTK_EDITABLE (proxy->entry), -1);
196 gtk_entry_set_text (GTK_ENTRY (proxy->entry), "");
199 update_pages_label (proxy, page, page_cache);
203 activate_cb (GtkWidget *entry, GtkAction *action)
205 EvPageAction *page = EV_PAGE_ACTION (action);
206 EvPageCache *page_cache;
210 text = gtk_entry_get_text (GTK_ENTRY (entry));
211 page_cache = page->priv->page_cache;
213 g_signal_emit (action, signals[ACTIVATE_LABEL], 0, text, &changed);
216 /* rest the entry to the current page if we were unable to
219 ev_page_cache_get_page_label (page_cache,
220 ev_page_cache_get_current_page (page_cache));
221 gtk_entry_set_text (GTK_ENTRY (entry), page_label);
222 gtk_editable_set_position (GTK_EDITABLE (entry), -1);
228 create_tool_item (GtkAction *action)
230 EvPageActionWidget *proxy;
233 proxy = g_object_new (ev_page_action_widget_get_type (), NULL);
234 gtk_container_set_border_width (GTK_CONTAINER (proxy), 6);
235 gtk_widget_show (GTK_WIDGET (proxy));
237 hbox = gtk_hbox_new (FALSE, 0);
238 gtk_box_set_spacing (GTK_BOX (hbox), 6);
240 proxy->entry = gtk_entry_new ();
241 gtk_entry_set_width_chars (GTK_ENTRY (proxy->entry), 5);
242 gtk_box_pack_start (GTK_BOX (hbox), proxy->entry, FALSE, FALSE, 0);
243 gtk_widget_show (proxy->entry);
244 g_signal_connect (proxy->entry, "activate",
245 G_CALLBACK (activate_cb),
248 proxy->label = gtk_label_new (NULL);
249 gtk_box_pack_start (GTK_BOX (hbox), proxy->label, FALSE, FALSE, 0);
250 gtk_widget_show (proxy->label);
252 gtk_container_add (GTK_CONTAINER (proxy), hbox);
253 gtk_widget_show (hbox);
255 return GTK_WIDGET (proxy);
259 update_page_cache (EvPageAction *page, GParamSpec *pspec, EvPageActionWidget *proxy)
261 EvPageCache *page_cache;
264 page_cache = page->priv->page_cache;
266 /* clear the old signal */
267 if (proxy->signal_id > 0 && proxy->page_cache)
268 g_signal_handler_disconnect (proxy->page_cache, proxy->signal_id);
270 if (page_cache != NULL) {
271 signal_id = g_signal_connect_object (page_cache,
273 G_CALLBACK (page_changed_cb),
275 /* Set the initial value */
276 page_changed_cb (page_cache,
277 ev_page_cache_get_current_page (page_cache),
280 /* Or clear the entry */
282 page_changed_cb (NULL, 0, proxy);
284 ev_page_action_widget_set_page_cache (proxy, page_cache);
285 proxy->signal_id = signal_id;
289 build_new_tree_cb (GtkTreeModel *model,
294 GtkTreeModel *filter_model = GTK_TREE_MODEL (data);
297 gtk_tree_model_get (model, iter,
298 EV_DOCUMENT_LINKS_COLUMN_LINK, &link,
301 if (link && ev_link_get_link_type (link) == EV_LINK_TYPE_PAGE) {
302 GtkTreeIter filter_iter;
304 gtk_list_store_append (GTK_LIST_STORE (filter_model), &filter_iter);
305 gtk_list_store_set (GTK_LIST_STORE (filter_model), &filter_iter,
311 g_object_unref (link);
316 static GtkTreeModel *
317 get_filter_model_from_model (GtkTreeModel *model)
319 GtkTreeModel *filter_model;
322 (GtkTreeModel *) g_object_get_data (G_OBJECT (model), EPA_FILTER_MODEL_DATA);
323 if (filter_model == NULL) {
324 filter_model = (GtkTreeModel *) gtk_list_store_new (1, GTK_TYPE_TREE_ITER);
326 gtk_tree_model_foreach (model,
329 g_object_set_data_full (G_OBJECT (model), EPA_FILTER_MODEL_DATA, filter_model, g_object_unref);
336 match_selected_cb (GtkEntryCompletion *completion,
337 GtkTreeModel *filter_model,
338 GtkTreeIter *filter_iter,
339 EvPageActionWidget *proxy)
344 gtk_tree_model_get (filter_model, filter_iter,
347 gtk_tree_model_get (proxy->model, iter,
348 EV_DOCUMENT_LINKS_COLUMN_LINK, &link,
351 g_signal_emit (proxy, widget_signals[WIDGET_ACTIVATE_LINK], 0, link);
354 g_object_unref (link);
356 gtk_tree_iter_free (iter);
363 display_completion_text (GtkCellLayout *cell_layout,
364 GtkCellRenderer *renderer,
365 GtkTreeModel *filter_model,
366 GtkTreeIter *filter_iter,
367 EvPageActionWidget *proxy)
372 gtk_tree_model_get (filter_model, filter_iter,
375 gtk_tree_model_get (proxy->model, iter,
376 EV_DOCUMENT_LINKS_COLUMN_LINK, &link,
379 g_object_set (renderer, "text", ev_link_get_title (link), NULL);
382 g_object_unref (link);
384 gtk_tree_iter_free (iter);
388 match_completion (GtkEntryCompletion *completion,
390 GtkTreeIter *filter_iter,
391 EvPageActionWidget *proxy)
395 const gchar *text = NULL;
397 gtk_tree_model_get (gtk_entry_completion_get_model (completion),
401 gtk_tree_model_get (proxy->model, iter,
402 EV_DOCUMENT_LINKS_COLUMN_LINK, &link,
407 text = ev_link_get_title (link);
408 g_object_unref (link);
411 gtk_tree_iter_free (iter);
414 gchar *normalized_text;
415 gchar *normalized_key;
416 gchar *case_normalized_text;
417 gchar *case_normalized_key;
418 gboolean retval = FALSE;
420 normalized_text = g_utf8_normalize (text, -1, G_NORMALIZE_ALL);
421 normalized_key = g_utf8_normalize (key, -1, G_NORMALIZE_ALL);
422 case_normalized_text = g_utf8_casefold (normalized_text, -1);
423 case_normalized_key = g_utf8_casefold (normalized_key, -1);
425 if (strstr (case_normalized_text, case_normalized_key))
428 g_free (normalized_text);
429 g_free (normalized_key);
430 g_free (case_normalized_text);
431 g_free (case_normalized_key);
441 update_model (EvPageAction *page, GParamSpec *pspec, EvPageActionWidget *proxy)
444 GtkTreeModel *filter_model;
446 g_object_get (G_OBJECT (page),
451 GtkEntryCompletion *completion;
452 GtkCellRenderer *renderer;
454 proxy->model = model;
455 filter_model = get_filter_model_from_model (model);
457 completion = gtk_entry_completion_new ();
459 /* popup-set-width is 2.7.0 only */
460 g_object_set (G_OBJECT (completion),
461 "popup-set-width", FALSE,
462 "model", filter_model,
465 g_signal_connect (completion, "match-selected", G_CALLBACK (match_selected_cb), proxy);
466 gtk_entry_completion_set_match_func (completion,
467 (GtkEntryCompletionMatchFunc) match_completion,
470 /* Set up the layout */
471 renderer = (GtkCellRenderer *)
472 g_object_new (GTK_TYPE_CELL_RENDERER_TEXT,
473 "ellipsize", PANGO_ELLIPSIZE_END,
476 gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (completion), renderer, TRUE);
477 gtk_cell_layout_set_cell_data_func (GTK_CELL_LAYOUT (completion),
479 (GtkCellLayoutDataFunc) display_completion_text,
481 gtk_entry_set_completion (GTK_ENTRY (proxy->entry), completion);
483 g_object_unref (completion);
484 g_object_unref (model);
489 activate_link_cb (EvPageActionWidget *proxy, EvLink *link, EvPageAction *action)
491 g_signal_emit (action, signals[ACTIVATE_LINK], 0, link);
495 connect_proxy (GtkAction *action, GtkWidget *proxy)
497 if (GTK_IS_TOOL_ITEM (proxy)) {
498 g_signal_connect_object (action, "notify::page-cache",
499 G_CALLBACK (update_page_cache),
501 g_signal_connect (proxy, "activate_link",
502 G_CALLBACK (activate_link_cb),
504 update_page_cache (EV_PAGE_ACTION (action), NULL,
505 EV_PAGE_ACTION_WIDGET (proxy));
506 /* We only go through this whole rigmarole if we can set
507 * GtkEntryCompletion::popup-set-width, which appeared in
509 if (gtk_check_version (2, 7, 0) == NULL) {
510 g_signal_connect_object (action, "notify::model",
511 G_CALLBACK (update_model),
516 GTK_ACTION_CLASS (ev_page_action_parent_class)->connect_proxy (action, proxy);
520 ev_page_action_dispose (GObject *object)
522 EvPageAction *page = EV_PAGE_ACTION (object);
524 if (page->priv->page_cache) {
525 g_object_unref (page->priv->page_cache);
526 page->priv->page_cache = NULL;
529 G_OBJECT_CLASS (ev_page_action_parent_class)->dispose (object);
533 ev_page_action_set_property (GObject *object,
539 EvPageCache *page_cache;
542 page = EV_PAGE_ACTION (object);
546 case PROP_PAGE_CACHE:
547 page_cache = page->priv->page_cache;
548 page->priv->page_cache = EV_PAGE_CACHE (g_value_dup_object (value));
550 g_object_unref (page_cache);
553 model = page->priv->model;
554 page->priv->model = GTK_TREE_MODEL (g_value_dup_object (value));
556 g_object_unref (model);
559 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
565 ev_page_action_get_property (GObject *object,
572 page = EV_PAGE_ACTION (object);
576 case PROP_PAGE_CACHE:
577 g_value_set_object (value, page->priv->page_cache);
580 g_value_set_object (value, page->priv->model);
583 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
589 ev_page_action_set_document (EvPageAction *page, EvDocument *document)
591 EvPageCache *page_cache = NULL;
594 page_cache = ev_page_cache_get (document);
597 "page-cache", page_cache,
603 ev_page_action_set_model (EvPageAction *page_action,
606 g_object_set (page_action,
612 ev_page_action_grab_focus (EvPageAction *page_action)
616 proxies = gtk_action_get_proxies (GTK_ACTION (page_action));
617 for (; proxies != NULL; proxies = proxies->next) {
618 EvPageActionWidget *proxy;
620 proxy = EV_PAGE_ACTION_WIDGET (proxies->data);
621 gtk_widget_grab_focus (proxy->entry);
626 ev_page_action_init (EvPageAction *page)
628 page->priv = EV_PAGE_ACTION_GET_PRIVATE (page);
632 ev_page_action_class_init (EvPageActionClass *class)
634 GObjectClass *object_class = G_OBJECT_CLASS (class);
635 GtkActionClass *action_class = GTK_ACTION_CLASS (class);
637 object_class->dispose = ev_page_action_dispose;
638 object_class->set_property = ev_page_action_set_property;
639 object_class->get_property = ev_page_action_get_property;
641 action_class->toolbar_item_type = GTK_TYPE_TOOL_ITEM;
642 action_class->create_tool_item = create_tool_item;
643 action_class->connect_proxy = connect_proxy;
645 signals[ACTIVATE_LINK] = g_signal_new ("activate_link",
646 G_OBJECT_CLASS_TYPE (object_class),
647 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
648 G_STRUCT_OFFSET (EvPageActionClass, activate_link),
650 g_cclosure_marshal_VOID__OBJECT,
653 signals[ACTIVATE_LABEL] = g_signal_new ("activate_label",
654 G_OBJECT_CLASS_TYPE (object_class),
655 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
656 G_STRUCT_OFFSET (EvPageActionClass, activate_label),
658 ev_marshal_BOOLEAN__STRING,
662 g_object_class_install_property (object_class,
664 g_param_spec_object ("page-cache",
666 "Current page cache",
670 g_object_class_install_property (object_class,
672 g_param_spec_object ("model",
678 g_type_class_add_private (object_class, sizeof (EvPageActionPrivate));