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) {
183 gchar *page_label = ev_page_cache_get_page_label (page_cache, page);
184 gtk_entry_set_text (GTK_ENTRY (proxy->entry), page_label);
185 gtk_editable_set_position (GTK_EDITABLE (proxy->entry), -1);
188 gtk_entry_set_text (GTK_ENTRY (proxy->entry), "");
191 update_pages_label (proxy, page, page_cache);
195 activate_cb (GtkWidget *entry, GtkAction *action)
197 EvPageAction *page = EV_PAGE_ACTION (action);
198 EvPageCache *page_cache;
202 text = gtk_entry_get_text (GTK_ENTRY (entry));
203 page_cache = page->priv->page_cache;
205 g_signal_emit (action, signals[ACTIVATE_LABEL], 0, text, &changed);
208 /* rest the entry to the current page if we were unable to
211 ev_page_cache_get_page_label (page_cache,
212 ev_page_cache_get_current_page (page_cache));
213 gtk_entry_set_text (GTK_ENTRY (entry), page_label);
214 gtk_editable_set_position (GTK_EDITABLE (entry), -1);
220 create_tool_item (GtkAction *action)
222 EvPageActionWidget *proxy;
225 proxy = g_object_new (ev_page_action_widget_get_type (), NULL);
226 gtk_container_set_border_width (GTK_CONTAINER (proxy), 6);
227 gtk_widget_show (GTK_WIDGET (proxy));
229 hbox = gtk_hbox_new (FALSE, 0);
230 gtk_box_set_spacing (GTK_BOX (hbox), 6);
232 proxy->entry = gtk_entry_new ();
233 gtk_entry_set_width_chars (GTK_ENTRY (proxy->entry), 5);
234 gtk_box_pack_start (GTK_BOX (hbox), proxy->entry, FALSE, FALSE, 0);
235 gtk_widget_show (proxy->entry);
236 g_signal_connect (proxy->entry, "activate",
237 G_CALLBACK (activate_cb),
240 proxy->label = gtk_label_new (NULL);
241 gtk_box_pack_start (GTK_BOX (hbox), proxy->label, FALSE, FALSE, 0);
242 gtk_widget_show (proxy->label);
244 gtk_container_add (GTK_CONTAINER (proxy), hbox);
245 gtk_widget_show (hbox);
247 return GTK_WIDGET (proxy);
251 update_page_cache (EvPageAction *page, GParamSpec *pspec, EvPageActionWidget *proxy)
253 EvPageCache *page_cache;
256 page_cache = page->priv->page_cache;
258 /* clear the old signal */
259 if (proxy->signal_id > 0 && proxy->page_cache)
260 g_signal_handler_disconnect (proxy->page_cache, proxy->signal_id);
262 if (page_cache != NULL) {
263 signal_id = g_signal_connect_object (page_cache,
265 G_CALLBACK (page_changed_cb),
267 /* Set the initial value */
268 page_changed_cb (page_cache,
269 ev_page_cache_get_current_page (page_cache),
272 /* Or clear the entry */
274 page_changed_cb (NULL, 0, proxy);
276 ev_page_action_widget_set_page_cache (proxy, page_cache);
277 proxy->signal_id = signal_id;
281 build_new_tree_cb (GtkTreeModel *model,
286 GtkTreeModel *filter_model = GTK_TREE_MODEL (data);
289 gtk_tree_model_get (model, iter,
290 EV_DOCUMENT_LINKS_COLUMN_LINK, &link,
293 if (link && ev_link_get_link_type (link) == EV_LINK_TYPE_PAGE) {
294 GtkTreeIter filter_iter;
296 gtk_list_store_append (GTK_LIST_STORE (filter_model), &filter_iter);
297 gtk_list_store_set (GTK_LIST_STORE (filter_model), &filter_iter,
305 static GtkTreeModel *
306 get_filter_model_from_model (GtkTreeModel *model)
308 GtkTreeModel *filter_model;
311 (GtkTreeModel *) g_object_get_data (G_OBJECT (model), EPA_FILTER_MODEL_DATA);
312 if (filter_model == NULL) {
313 filter_model = (GtkTreeModel *) gtk_list_store_new (1, GTK_TYPE_TREE_ITER);
315 gtk_tree_model_foreach (model,
318 g_object_set_data_full (G_OBJECT (model), EPA_FILTER_MODEL_DATA, filter_model, g_object_unref);
325 match_selected_cb (GtkEntryCompletion *completion,
326 GtkTreeModel *filter_model,
327 GtkTreeIter *filter_iter,
328 EvPageActionWidget *proxy)
333 gtk_tree_model_get (filter_model, filter_iter,
336 gtk_tree_model_get (proxy->model, iter,
337 EV_DOCUMENT_LINKS_COLUMN_LINK, &link,
340 g_signal_emit (proxy, signals[ACTIVATE_LINK], 0, link);
347 display_completion_text (GtkCellLayout *cell_layout,
348 GtkCellRenderer *renderer,
349 GtkTreeModel *filter_model,
350 GtkTreeIter *filter_iter,
351 EvPageActionWidget *proxy)
356 gtk_tree_model_get (filter_model, filter_iter,
359 gtk_tree_model_get (proxy->model, iter,
360 EV_DOCUMENT_LINKS_COLUMN_LINK, &link,
363 g_object_set (renderer, "text", ev_link_get_title (link), NULL);
367 match_completion (GtkEntryCompletion *completion,
369 GtkTreeIter *filter_iter,
370 EvPageActionWidget *proxy)
374 const gchar *text = NULL;
376 gtk_tree_model_get (gtk_entry_completion_get_model (completion),
380 gtk_tree_model_get (proxy->model, iter,
381 EV_DOCUMENT_LINKS_COLUMN_LINK, &link,
386 text = ev_link_get_title (link);
389 gchar *normalized_text;
390 gchar *normalized_key;
391 gchar *case_normalized_text;
392 gchar *case_normalized_key;
393 gboolean retval = FALSE;
395 normalized_text = g_utf8_normalize (text, -1, G_NORMALIZE_ALL);
396 normalized_key = g_utf8_normalize (key, -1, G_NORMALIZE_ALL);
397 case_normalized_text = g_utf8_casefold (normalized_text, -1);
398 case_normalized_key = g_utf8_casefold (normalized_key, -1);
400 if (strstr (case_normalized_text, case_normalized_key))
403 g_free (normalized_text);
404 g_free (normalized_key);
405 g_free (case_normalized_text);
406 g_free (case_normalized_key);
416 update_model (EvPageAction *page, GParamSpec *pspec, EvPageActionWidget *proxy)
419 GtkTreeModel *filter_model;
421 g_object_get (G_OBJECT (page),
426 GtkEntryCompletion *completion;
427 GtkCellRenderer *renderer;
429 proxy->model = model;
430 filter_model = get_filter_model_from_model (model);
432 completion = gtk_entry_completion_new ();
434 /* popup-set-width is 2.7.0 only */
435 g_object_set (G_OBJECT (completion),
436 "popup-set-width", FALSE,
437 "model", filter_model,
440 g_signal_connect (completion, "match-selected", G_CALLBACK (match_selected_cb), proxy);
441 gtk_entry_completion_set_match_func (completion,
442 (GtkEntryCompletionMatchFunc) match_completion,
445 /* Set up the layout */
446 renderer = (GtkCellRenderer *)
447 g_object_new (GTK_TYPE_CELL_RENDERER_TEXT,
448 "ellipsize", PANGO_ELLIPSIZE_END,
451 gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (completion), renderer, TRUE);
452 gtk_cell_layout_set_cell_data_func (GTK_CELL_LAYOUT (completion),
454 (GtkCellLayoutDataFunc) display_completion_text,
456 gtk_entry_set_completion (GTK_ENTRY (proxy->entry), completion);
461 activate_link_cb (EvPageActionWidget *proxy, EvLink *link, EvPageAction *action)
463 g_signal_emit (action, signals[ACTIVATE_LINK], 0, link);
467 connect_proxy (GtkAction *action, GtkWidget *proxy)
469 if (GTK_IS_TOOL_ITEM (proxy)) {
470 g_signal_connect_object (action, "notify::page-cache",
471 G_CALLBACK (update_page_cache),
473 g_signal_connect (proxy, "activate_link",
474 G_CALLBACK (activate_link_cb),
476 update_page_cache (EV_PAGE_ACTION (action), NULL,
477 EV_PAGE_ACTION_WIDGET (proxy));
478 /* We only go through this whole rigmarole if we can set
479 * GtkEntryCompletion::popup-set-width, which appeared in
481 if (gtk_check_version (2, 7, 0) == NULL) {
482 g_signal_connect_object (action, "notify::model",
483 G_CALLBACK (update_model),
488 GTK_ACTION_CLASS (ev_page_action_parent_class)->connect_proxy (action, proxy);
492 ev_page_action_dispose (GObject *object)
494 EvPageAction *page = EV_PAGE_ACTION (object);
496 if (page->priv->page_cache) {
497 g_object_unref (page->priv->page_cache);
498 page->priv->page_cache = NULL;
501 G_OBJECT_CLASS (ev_page_action_parent_class)->dispose (object);
505 ev_page_action_set_property (GObject *object,
511 EvPageCache *page_cache;
514 page = EV_PAGE_ACTION (object);
518 case PROP_PAGE_CACHE:
519 page_cache = page->priv->page_cache;
520 page->priv->page_cache = EV_PAGE_CACHE (g_value_dup_object (value));
522 g_object_unref (page_cache);
525 model = page->priv->model;
526 page->priv->model = GTK_TREE_MODEL (g_value_dup_object (value));
528 g_object_unref (model);
531 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
537 ev_page_action_get_property (GObject *object,
544 page = EV_PAGE_ACTION (object);
548 case PROP_PAGE_CACHE:
549 g_value_set_object (value, page->priv->page_cache);
552 g_value_set_object (value, page->priv->model);
555 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
561 ev_page_action_set_document (EvPageAction *page, EvDocument *document)
563 EvPageCache *page_cache = NULL;
566 page_cache = ev_document_get_page_cache (document);
569 "page-cache", page_cache,
575 ev_page_action_set_model (EvPageAction *page_action,
578 g_object_set (page_action,
584 ev_page_action_grab_focus (EvPageAction *page_action)
588 proxies = gtk_action_get_proxies (GTK_ACTION (page_action));
589 for (; proxies != NULL; proxies = proxies->next) {
590 EvPageActionWidget *proxy;
592 proxy = EV_PAGE_ACTION_WIDGET (proxies->data);
593 gtk_widget_grab_focus (proxy->entry);
598 ev_page_action_init (EvPageAction *page)
600 page->priv = EV_PAGE_ACTION_GET_PRIVATE (page);
604 ev_page_action_class_init (EvPageActionClass *class)
606 GObjectClass *object_class = G_OBJECT_CLASS (class);
607 GtkActionClass *action_class = GTK_ACTION_CLASS (class);
609 object_class->dispose = ev_page_action_dispose;
610 object_class->set_property = ev_page_action_set_property;
611 object_class->get_property = ev_page_action_get_property;
613 action_class->toolbar_item_type = GTK_TYPE_TOOL_ITEM;
614 action_class->create_tool_item = create_tool_item;
615 action_class->connect_proxy = connect_proxy;
617 signals[ACTIVATE_LINK] = g_signal_new ("activate_link",
618 G_OBJECT_CLASS_TYPE (object_class),
619 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
620 G_STRUCT_OFFSET (EvPageActionClass, activate_link),
622 g_cclosure_marshal_VOID__OBJECT,
625 signals[ACTIVATE_LABEL] = g_signal_new ("activate_label",
626 G_OBJECT_CLASS_TYPE (object_class),
627 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
628 G_STRUCT_OFFSET (EvPageActionClass, activate_link),
630 ev_marshal_BOOLEAN__STRING,
634 g_object_class_install_property (object_class,
636 g_param_spec_object ("page-cache",
638 "Current page cache",
642 g_object_class_install_property (object_class,
644 g_param_spec_object ("model",
650 g_type_class_add_private (object_class, sizeof (EvPageActionPrivate));