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"
27 #include "ev-marshal.h"
29 #include <glib/gi18n.h>
30 #include <gtk/gtkentry.h>
31 #include <gtk/gtktoolitem.h>
32 #include <gtk/gtklabel.h>
33 #include <gtk/gtkhbox.h>
36 typedef struct _EvPageActionWidget EvPageActionWidget;
37 typedef struct _EvPageActionWidgetClass EvPageActionWidgetClass;
38 struct _EvPageActionWidget
44 EvPageCache *page_cache;
46 GtkTreeModel *filter_model;
50 struct _EvPageActionWidgetClass
52 GtkToolItemClass parent_class;
54 void (* activate_link) (EvPageActionWidget *page_action,
58 struct _EvPageActionPrivate
60 EvPageCache *page_cache;
65 /* Widget we pass back */
66 static GType ev_page_action_widget_get_type (void);
67 static void ev_page_action_widget_init (EvPageActionWidget *action_widget);
68 static void ev_page_action_widget_class_init (EvPageActionWidgetClass *action_widget);
70 #define EV_TYPE_PAGE_ACTION_WIDGET (ev_page_action_widget_get_type ())
71 #define EV_PAGE_ACTION_WIDGET(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), EV_TYPE_PAGE_ACTION_WIDGET, EvPageActionWidget))
79 static guint widget_signals[WIDGET_N_SIGNALS] = {0, };
81 G_DEFINE_TYPE (EvPageActionWidget, ev_page_action_widget, GTK_TYPE_TOOL_ITEM)
84 ev_page_action_widget_init (EvPageActionWidget *action_widget)
90 ev_page_action_widget_set_page_cache (EvPageActionWidget *action_widget,
91 EvPageCache *page_cache)
93 if (action_widget->page_cache != NULL) {
94 g_object_remove_weak_pointer (G_OBJECT (action_widget->page_cache),
95 (gpointer *)&action_widget->page_cache);
96 action_widget->page_cache = NULL;
99 if (page_cache != NULL) {
100 action_widget->page_cache = page_cache;
101 g_object_add_weak_pointer (G_OBJECT (page_cache),
102 (gpointer *)&action_widget->page_cache);
107 ev_page_action_widget_finalize (GObject *object)
109 EvPageActionWidget *action_widget = EV_PAGE_ACTION_WIDGET (object);
111 ev_page_action_widget_set_page_cache (action_widget, NULL);
115 ev_page_action_widget_class_init (EvPageActionWidgetClass *class)
117 GObjectClass *object_class = G_OBJECT_CLASS (class);
119 object_class->finalize = ev_page_action_widget_finalize;
121 widget_signals[WIDGET_ACTIVATE_LINK] = g_signal_new ("activate_link",
122 G_OBJECT_CLASS_TYPE (object_class),
123 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
124 G_STRUCT_OFFSET (EvPageActionClass, activate_link),
126 g_cclosure_marshal_VOID__OBJECT,
132 static void ev_page_action_init (EvPageAction *action);
133 static void ev_page_action_class_init (EvPageActionClass *class);
142 static guint signals[N_SIGNALS] = {0, };
144 G_DEFINE_TYPE (EvPageAction, ev_page_action, GTK_TYPE_ACTION)
146 #define EV_PAGE_ACTION_GET_PRIVATE(object)(G_TYPE_INSTANCE_GET_PRIVATE ((object), EV_TYPE_PAGE_ACTION, EvPageActionPrivate))
154 /* user data to set on the widget. */
155 #define EPA_FILTER_MODEL_DATA "epa-filter-model"
158 update_pages_label (EvPageActionWidget *proxy,
160 EvPageCache *page_cache)
165 n_pages = page_cache ? ev_page_cache_get_n_pages (page_cache) : 0;
166 if (page_cache && ev_page_cache_has_nonnumeric_page_labels (page_cache)) {
167 label_text = g_strdup_printf (_("(%d of %d)"), page + 1, n_pages);
169 label_text = g_strdup_printf (_("of %d"), n_pages);
171 gtk_label_set_text (GTK_LABEL (proxy->label), label_text);
176 page_changed_cb (EvPageCache *page_cache,
178 EvPageActionWidget *proxy)
182 if (page_cache != NULL && page >= 0) {
184 gtk_entry_set_width_chars (GTK_ENTRY (proxy->entry),
185 CLAMP (ev_page_cache_get_max_label_chars (page_cache),
188 gchar *page_label = ev_page_cache_get_page_label (page_cache, page);
189 gtk_entry_set_text (GTK_ENTRY (proxy->entry), page_label);
190 gtk_editable_set_position (GTK_EDITABLE (proxy->entry), -1);
194 gtk_entry_set_text (GTK_ENTRY (proxy->entry), "");
197 update_pages_label (proxy, page, page_cache);
201 activate_cb (GtkWidget *entry, GtkAction *action)
203 EvPageAction *page = EV_PAGE_ACTION (action);
204 EvPageCache *page_cache;
208 text = gtk_entry_get_text (GTK_ENTRY (entry));
209 page_cache = page->priv->page_cache;
211 g_signal_emit (action, signals[ACTIVATE_LABEL], 0, text, &changed);
214 /* rest the entry to the current page if we were unable to
217 ev_page_cache_get_page_label (page_cache,
218 ev_page_cache_get_current_page (page_cache));
219 gtk_entry_set_text (GTK_ENTRY (entry), page_label);
220 gtk_editable_set_position (GTK_EDITABLE (entry), -1);
226 create_tool_item (GtkAction *action)
228 EvPageActionWidget *proxy;
231 proxy = g_object_new (ev_page_action_widget_get_type (), NULL);
232 gtk_container_set_border_width (GTK_CONTAINER (proxy), 6);
233 gtk_widget_show (GTK_WIDGET (proxy));
235 hbox = gtk_hbox_new (FALSE, 0);
236 gtk_box_set_spacing (GTK_BOX (hbox), 6);
238 proxy->entry = gtk_entry_new ();
239 gtk_entry_set_width_chars (GTK_ENTRY (proxy->entry), 5);
240 gtk_box_pack_start (GTK_BOX (hbox), proxy->entry, FALSE, FALSE, 0);
241 gtk_widget_show (proxy->entry);
242 g_signal_connect (proxy->entry, "activate",
243 G_CALLBACK (activate_cb),
246 proxy->label = gtk_label_new (NULL);
247 gtk_box_pack_start (GTK_BOX (hbox), proxy->label, FALSE, FALSE, 0);
248 gtk_widget_show (proxy->label);
250 gtk_container_add (GTK_CONTAINER (proxy), hbox);
251 gtk_widget_show (hbox);
253 return GTK_WIDGET (proxy);
257 update_page_cache (EvPageAction *page, GParamSpec *pspec, EvPageActionWidget *proxy)
259 EvPageCache *page_cache;
262 page_cache = page->priv->page_cache;
264 /* clear the old signal */
265 if (proxy->signal_id > 0 && proxy->page_cache)
266 g_signal_handler_disconnect (proxy->page_cache, proxy->signal_id);
268 if (page_cache != NULL) {
269 signal_id = g_signal_connect_object (page_cache,
271 G_CALLBACK (page_changed_cb),
273 /* Set the initial value */
274 page_changed_cb (page_cache,
275 ev_page_cache_get_current_page (page_cache),
278 /* Or clear the entry */
280 page_changed_cb (NULL, 0, proxy);
282 ev_page_action_widget_set_page_cache (proxy, page_cache);
283 proxy->signal_id = signal_id;
287 build_new_tree_cb (GtkTreeModel *model,
292 GtkTreeModel *filter_model = GTK_TREE_MODEL (data);
295 gtk_tree_model_get (model, iter,
296 EV_DOCUMENT_LINKS_COLUMN_LINK, &link,
299 if (link && ev_link_get_link_type (link) == EV_LINK_TYPE_PAGE) {
300 GtkTreeIter filter_iter;
302 gtk_list_store_append (GTK_LIST_STORE (filter_model), &filter_iter);
303 gtk_list_store_set (GTK_LIST_STORE (filter_model), &filter_iter,
311 static GtkTreeModel *
312 get_filter_model_from_model (GtkTreeModel *model)
314 GtkTreeModel *filter_model;
317 (GtkTreeModel *) g_object_get_data (G_OBJECT (model), EPA_FILTER_MODEL_DATA);
318 if (filter_model == NULL) {
319 filter_model = (GtkTreeModel *) gtk_list_store_new (1, GTK_TYPE_TREE_ITER);
321 gtk_tree_model_foreach (model,
324 g_object_set_data_full (G_OBJECT (model), EPA_FILTER_MODEL_DATA, filter_model, g_object_unref);
331 match_selected_cb (GtkEntryCompletion *completion,
332 GtkTreeModel *filter_model,
333 GtkTreeIter *filter_iter,
334 EvPageActionWidget *proxy)
339 gtk_tree_model_get (filter_model, filter_iter,
342 gtk_tree_model_get (proxy->model, iter,
343 EV_DOCUMENT_LINKS_COLUMN_LINK, &link,
346 g_signal_emit (proxy, signals[ACTIVATE_LINK], 0, link);
353 display_completion_text (GtkCellLayout *cell_layout,
354 GtkCellRenderer *renderer,
355 GtkTreeModel *filter_model,
356 GtkTreeIter *filter_iter,
357 EvPageActionWidget *proxy)
362 gtk_tree_model_get (filter_model, filter_iter,
365 gtk_tree_model_get (proxy->model, iter,
366 EV_DOCUMENT_LINKS_COLUMN_LINK, &link,
369 g_object_set (renderer, "text", ev_link_get_title (link), NULL);
373 match_completion (GtkEntryCompletion *completion,
375 GtkTreeIter *filter_iter,
376 EvPageActionWidget *proxy)
380 const gchar *text = NULL;
382 gtk_tree_model_get (gtk_entry_completion_get_model (completion),
386 gtk_tree_model_get (proxy->model, iter,
387 EV_DOCUMENT_LINKS_COLUMN_LINK, &link,
392 text = ev_link_get_title (link);
395 gchar *normalized_text;
396 gchar *normalized_key;
397 gchar *case_normalized_text;
398 gchar *case_normalized_key;
399 gboolean retval = FALSE;
401 normalized_text = g_utf8_normalize (text, -1, G_NORMALIZE_ALL);
402 normalized_key = g_utf8_normalize (key, -1, G_NORMALIZE_ALL);
403 case_normalized_text = g_utf8_casefold (normalized_text, -1);
404 case_normalized_key = g_utf8_casefold (normalized_key, -1);
406 if (strstr (case_normalized_text, case_normalized_key))
409 g_free (normalized_text);
410 g_free (normalized_key);
411 g_free (case_normalized_text);
412 g_free (case_normalized_key);
422 update_model (EvPageAction *page, GParamSpec *pspec, EvPageActionWidget *proxy)
425 GtkTreeModel *filter_model;
427 g_object_get (G_OBJECT (page),
432 GtkEntryCompletion *completion;
433 GtkCellRenderer *renderer;
435 proxy->model = model;
436 filter_model = get_filter_model_from_model (model);
438 completion = gtk_entry_completion_new ();
440 /* popup-set-width is 2.7.0 only */
441 g_object_set (G_OBJECT (completion),
442 "popup-set-width", FALSE,
443 "model", filter_model,
446 g_signal_connect (completion, "match-selected", G_CALLBACK (match_selected_cb), proxy);
447 gtk_entry_completion_set_match_func (completion,
448 (GtkEntryCompletionMatchFunc) match_completion,
451 /* Set up the layout */
452 renderer = (GtkCellRenderer *)
453 g_object_new (GTK_TYPE_CELL_RENDERER_TEXT,
454 "ellipsize", PANGO_ELLIPSIZE_END,
457 gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (completion), renderer, TRUE);
458 gtk_cell_layout_set_cell_data_func (GTK_CELL_LAYOUT (completion),
460 (GtkCellLayoutDataFunc) display_completion_text,
462 gtk_entry_set_completion (GTK_ENTRY (proxy->entry), completion);
467 activate_link_cb (EvPageActionWidget *proxy, EvLink *link, EvPageAction *action)
469 g_signal_emit (action, signals[ACTIVATE_LINK], 0, link);
473 connect_proxy (GtkAction *action, GtkWidget *proxy)
475 if (GTK_IS_TOOL_ITEM (proxy)) {
476 g_signal_connect_object (action, "notify::page-cache",
477 G_CALLBACK (update_page_cache),
479 g_signal_connect (proxy, "activate_link",
480 G_CALLBACK (activate_link_cb),
482 update_page_cache (EV_PAGE_ACTION (action), NULL,
483 EV_PAGE_ACTION_WIDGET (proxy));
484 /* We only go through this whole rigmarole if we can set
485 * GtkEntryCompletion::popup-set-width, which appeared in
487 if (gtk_check_version (2, 7, 0) == NULL) {
488 g_signal_connect_object (action, "notify::model",
489 G_CALLBACK (update_model),
494 GTK_ACTION_CLASS (ev_page_action_parent_class)->connect_proxy (action, proxy);
498 ev_page_action_dispose (GObject *object)
500 EvPageAction *page = EV_PAGE_ACTION (object);
502 if (page->priv->page_cache) {
503 g_object_unref (page->priv->page_cache);
504 page->priv->page_cache = NULL;
507 G_OBJECT_CLASS (ev_page_action_parent_class)->dispose (object);
511 ev_page_action_set_property (GObject *object,
517 EvPageCache *page_cache;
520 page = EV_PAGE_ACTION (object);
524 case PROP_PAGE_CACHE:
525 page_cache = page->priv->page_cache;
526 page->priv->page_cache = EV_PAGE_CACHE (g_value_dup_object (value));
528 g_object_unref (page_cache);
531 model = page->priv->model;
532 page->priv->model = GTK_TREE_MODEL (g_value_dup_object (value));
534 g_object_unref (model);
537 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
543 ev_page_action_get_property (GObject *object,
550 page = EV_PAGE_ACTION (object);
554 case PROP_PAGE_CACHE:
555 g_value_set_object (value, page->priv->page_cache);
558 g_value_set_object (value, page->priv->model);
561 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
567 ev_page_action_set_document (EvPageAction *page, EvDocument *document)
569 EvPageCache *page_cache = NULL;
572 page_cache = ev_document_get_page_cache (document);
575 "page-cache", page_cache,
581 ev_page_action_set_model (EvPageAction *page_action,
584 g_object_set (page_action,
590 ev_page_action_grab_focus (EvPageAction *page_action)
594 proxies = gtk_action_get_proxies (GTK_ACTION (page_action));
595 for (; proxies != NULL; proxies = proxies->next) {
596 EvPageActionWidget *proxy;
598 proxy = EV_PAGE_ACTION_WIDGET (proxies->data);
599 gtk_widget_grab_focus (proxy->entry);
604 ev_page_action_init (EvPageAction *page)
606 page->priv = EV_PAGE_ACTION_GET_PRIVATE (page);
610 ev_page_action_class_init (EvPageActionClass *class)
612 GObjectClass *object_class = G_OBJECT_CLASS (class);
613 GtkActionClass *action_class = GTK_ACTION_CLASS (class);
615 object_class->dispose = ev_page_action_dispose;
616 object_class->set_property = ev_page_action_set_property;
617 object_class->get_property = ev_page_action_get_property;
619 action_class->toolbar_item_type = GTK_TYPE_TOOL_ITEM;
620 action_class->create_tool_item = create_tool_item;
621 action_class->connect_proxy = connect_proxy;
623 signals[ACTIVATE_LINK] = g_signal_new ("activate_link",
624 G_OBJECT_CLASS_TYPE (object_class),
625 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
626 G_STRUCT_OFFSET (EvPageActionClass, activate_link),
628 g_cclosure_marshal_VOID__OBJECT,
631 signals[ACTIVATE_LABEL] = g_signal_new ("activate_label",
632 G_OBJECT_CLASS_TYPE (object_class),
633 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
634 G_STRUCT_OFFSET (EvPageActionClass, activate_link),
636 ev_marshal_BOOLEAN__STRING,
640 g_object_class_install_property (object_class,
642 g_param_spec_object ("page-cache",
644 "Current page cache",
648 g_object_class_install_property (object_class,
650 g_param_spec_object ("model",
656 g_type_class_add_private (object_class, sizeof (EvPageActionPrivate));