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
43 EvPageCache *page_cache;
45 GtkTreeModel *filter_model;
49 struct _EvPageActionWidgetClass
51 GtkToolItemClass parent_class;
54 struct _EvPageActionPrivate
56 EvPageCache *page_cache;
61 /* Widget we pass back */
62 static GType ev_page_action_widget_get_type (void);
63 static void ev_page_action_widget_init (EvPageActionWidget *action_widget);
64 static void ev_page_action_widget_class_init (EvPageActionWidgetClass *action_widget);
66 #define EV_TYPE_PAGE_ACTION_WIDGET (ev_page_action_widget_get_type ())
67 #define EV_PAGE_ACTION_WIDGET(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), EV_TYPE_PAGE_ACTION_WIDGET, EvPageActionWidget))
69 G_DEFINE_TYPE (EvPageActionWidget, ev_page_action_widget, GTK_TYPE_TOOL_ITEM)
72 ev_page_action_widget_init (EvPageActionWidget *action_widget)
78 ev_page_action_widget_set_page_cache (EvPageActionWidget *action_widget,
79 EvPageCache *page_cache)
81 if (action_widget->page_cache != NULL) {
82 g_object_remove_weak_pointer (G_OBJECT (action_widget->page_cache),
83 (gpointer *)&action_widget->page_cache);
84 action_widget->page_cache = NULL;
87 if (page_cache != NULL) {
88 action_widget->page_cache = page_cache;
89 g_object_add_weak_pointer (G_OBJECT (page_cache),
90 (gpointer *)&action_widget->page_cache);
95 ev_page_action_widget_finalize (GObject *object)
97 EvPageActionWidget *action_widget = EV_PAGE_ACTION_WIDGET (object);
99 ev_page_action_widget_set_page_cache (action_widget, NULL);
103 ev_page_action_widget_class_init (EvPageActionWidgetClass *class)
105 GObjectClass *object_class = G_OBJECT_CLASS (class);
107 object_class->finalize = ev_page_action_widget_finalize;
110 static void ev_page_action_init (EvPageAction *action);
111 static void ev_page_action_class_init (EvPageActionClass *class);
113 G_DEFINE_TYPE (EvPageAction, ev_page_action, GTK_TYPE_ACTION)
115 #define EV_PAGE_ACTION_GET_PRIVATE(object)(G_TYPE_INSTANCE_GET_PRIVATE ((object), EV_TYPE_PAGE_ACTION, EvPageActionPrivate))
123 /* user data to set on the widget. */
124 #define EPA_FILTER_MODEL_DATA "epa-filter-model"
127 update_pages_label (EvPageActionWidget *proxy,
129 EvPageCache *page_cache)
134 n_pages = page_cache ? ev_page_cache_get_n_pages (page_cache) : 0;
135 if (page_cache && ev_page_cache_has_nonnumeric_page_labels (page_cache)) {
136 label_text = g_strdup_printf (_("(%d of %d)"), page + 1, n_pages);
138 label_text = g_strdup_printf (_("of %d"), n_pages);
140 gtk_label_set_text (GTK_LABEL (proxy->label), label_text);
145 page_changed_cb (EvPageCache *page_cache,
147 EvPageActionWidget *proxy)
151 if (page_cache != NULL && page >= 0) {
152 gchar *page_label = ev_page_cache_get_page_label (page_cache, page);
153 gtk_entry_set_text (GTK_ENTRY (proxy->entry), page_label);
154 gtk_editable_set_position (GTK_EDITABLE (proxy->entry), -1);
157 gtk_entry_set_text (GTK_ENTRY (proxy->entry), "");
160 update_pages_label (proxy, page, page_cache);
164 activate_cb (GtkWidget *entry, GtkAction *action)
166 EvPageAction *page = EV_PAGE_ACTION (action);
167 EvPageCache *page_cache;
170 text = gtk_entry_get_text (GTK_ENTRY (entry));
171 page_cache = page->priv->page_cache;
173 if (! ev_page_cache_set_page_label (page_cache, text)) {
174 /* rest the entry to the current page if we were unable to
177 ev_page_cache_get_page_label (page_cache,
178 ev_page_cache_get_current_page (page_cache));
179 gtk_entry_set_text (GTK_ENTRY (entry), page_label);
180 gtk_editable_set_position (GTK_EDITABLE (entry), -1);
186 create_tool_item (GtkAction *action)
188 EvPageActionWidget *proxy;
191 proxy = g_object_new (ev_page_action_widget_get_type (), NULL);
192 gtk_container_set_border_width (GTK_CONTAINER (proxy), 6);
193 gtk_widget_show (GTK_WIDGET (proxy));
195 hbox = gtk_hbox_new (FALSE, 0);
196 gtk_box_set_spacing (GTK_BOX (hbox), 6);
198 proxy->entry = gtk_entry_new ();
199 gtk_entry_set_width_chars (GTK_ENTRY (proxy->entry), 5);
200 gtk_box_pack_start (GTK_BOX (hbox), proxy->entry, FALSE, FALSE, 0);
201 gtk_widget_show (proxy->entry);
202 g_signal_connect (proxy->entry, "activate",
203 G_CALLBACK (activate_cb),
206 proxy->label = gtk_label_new (NULL);
207 gtk_box_pack_start (GTK_BOX (hbox), proxy->label, FALSE, FALSE, 0);
208 gtk_widget_show (proxy->label);
210 gtk_container_add (GTK_CONTAINER (proxy), hbox);
211 gtk_widget_show (hbox);
213 return GTK_WIDGET (proxy);
217 update_page_cache (EvPageAction *page, GParamSpec *pspec, EvPageActionWidget *proxy)
219 EvPageCache *page_cache;
222 page_cache = page->priv->page_cache;
224 /* clear the old signal */
225 if (proxy->signal_id > 0 && proxy->page_cache)
226 g_signal_handler_disconnect (proxy->page_cache, proxy->signal_id);
228 if (page_cache != NULL) {
229 signal_id = g_signal_connect_object (page_cache,
231 G_CALLBACK (page_changed_cb),
233 /* Set the initial value */
234 page_changed_cb (page_cache,
235 ev_page_cache_get_current_page (page_cache),
238 /* Or clear the entry */
240 page_changed_cb (NULL, 0, proxy);
242 ev_page_action_widget_set_page_cache (proxy, page_cache);
243 proxy->signal_id = signal_id;
247 build_new_tree_cb (GtkTreeModel *model,
252 GtkTreeModel *filter_model = GTK_TREE_MODEL (data);
255 gtk_tree_model_get (model, iter,
256 EV_DOCUMENT_LINKS_COLUMN_LINK, &link,
259 if (link && ev_link_get_link_type (link) == EV_LINK_TYPE_PAGE) {
260 GtkTreeIter filter_iter;
262 gtk_list_store_append (GTK_LIST_STORE (filter_model), &filter_iter);
263 gtk_list_store_set (GTK_LIST_STORE (filter_model), &filter_iter,
271 static GtkTreeModel *
272 get_filter_model_from_model (GtkTreeModel *model)
274 GtkTreeModel *filter_model;
277 (GtkTreeModel *) g_object_get_data (G_OBJECT (model), EPA_FILTER_MODEL_DATA);
278 if (filter_model == NULL) {
279 filter_model = (GtkTreeModel *) gtk_list_store_new (1, GTK_TYPE_TREE_ITER);
281 gtk_tree_model_foreach (model,
284 g_object_set_data_full (G_OBJECT (model), EPA_FILTER_MODEL_DATA, filter_model, g_object_unref);
291 match_selected_cb (GtkEntryCompletion *completion,
292 GtkTreeModel *filter_model,
293 GtkTreeIter *filter_iter,
294 EvPageActionWidget *proxy)
299 gtk_tree_model_get (filter_model, filter_iter,
302 gtk_tree_model_get (proxy->model, iter,
303 EV_DOCUMENT_LINKS_COLUMN_LINK, &link,
305 ev_page_cache_set_link (proxy->page_cache, link);
312 display_completion_text (GtkCellLayout *cell_layout,
313 GtkCellRenderer *renderer,
314 GtkTreeModel *filter_model,
315 GtkTreeIter *filter_iter,
316 EvPageActionWidget *proxy)
321 gtk_tree_model_get (filter_model, filter_iter,
324 gtk_tree_model_get (proxy->model, iter,
325 EV_DOCUMENT_LINKS_COLUMN_LINK, &link,
328 g_object_set (renderer, "text", ev_link_get_title (link), NULL);
332 match_completion (GtkEntryCompletion *completion,
334 GtkTreeIter *filter_iter,
335 EvPageActionWidget *proxy)
339 const gchar *text = NULL;
341 gtk_tree_model_get (gtk_entry_completion_get_model (completion),
345 gtk_tree_model_get (proxy->model, iter,
346 EV_DOCUMENT_LINKS_COLUMN_LINK, &link,
351 text = ev_link_get_title (link);
354 gchar *normalized_text;
355 gchar *normalized_key;
356 gchar *case_normalized_text;
357 gchar *case_normalized_key;
358 gboolean retval = FALSE;
360 normalized_text = g_utf8_normalize (text, -1, G_NORMALIZE_ALL);
361 normalized_key = g_utf8_normalize (key, -1, G_NORMALIZE_ALL);
362 case_normalized_text = g_utf8_casefold (normalized_text, -1);
363 case_normalized_key = g_utf8_casefold (normalized_key, -1);
365 if (strstr (case_normalized_text, case_normalized_key))
368 g_free (normalized_text);
369 g_free (normalized_key);
370 g_free (case_normalized_text);
371 g_free (case_normalized_key);
381 update_model (EvPageAction *page, GParamSpec *pspec, EvPageActionWidget *proxy)
384 GtkTreeModel *filter_model;
386 g_object_get (G_OBJECT (page),
391 GtkEntryCompletion *completion;
392 GtkCellRenderer *renderer;
394 proxy->model = model;
395 filter_model = get_filter_model_from_model (model);
397 completion = gtk_entry_completion_new ();
399 /* popup-set-width is 2.7.0 only */
400 g_object_set (G_OBJECT (completion),
401 "popup-set-width", FALSE,
402 "model", filter_model,
405 g_signal_connect (completion, "match-selected", G_CALLBACK (match_selected_cb), proxy);
406 gtk_entry_completion_set_match_func (completion,
407 (GtkEntryCompletionMatchFunc) match_completion,
410 /* Set up the layout */
411 renderer = (GtkCellRenderer *)
412 g_object_new (GTK_TYPE_CELL_RENDERER_TEXT,
413 "ellipsize", PANGO_ELLIPSIZE_END,
416 gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (completion), renderer, TRUE);
417 gtk_cell_layout_set_cell_data_func (GTK_CELL_LAYOUT (completion),
419 (GtkCellLayoutDataFunc) display_completion_text,
421 gtk_entry_set_completion (GTK_ENTRY (proxy->entry), completion);
426 connect_proxy (GtkAction *action, GtkWidget *proxy)
428 if (GTK_IS_TOOL_ITEM (proxy)) {
429 g_signal_connect_object (action, "notify::page-cache",
430 G_CALLBACK (update_page_cache),
432 update_page_cache (EV_PAGE_ACTION (action), NULL,
433 EV_PAGE_ACTION_WIDGET (proxy));
434 /* We only go through this whole rigmarole if we can set
435 * GtkEntryCompletion::popup-set-width, which appeared in
437 if (gtk_check_version (2, 7, 0) == NULL) {
438 g_signal_connect_object (action, "notify::model",
439 G_CALLBACK (update_model),
444 GTK_ACTION_CLASS (ev_page_action_parent_class)->connect_proxy (action, proxy);
448 ev_page_action_dispose (GObject *object)
450 EvPageAction *page = EV_PAGE_ACTION (object);
452 if (page->priv->page_cache) {
453 g_object_unref (page->priv->page_cache);
454 page->priv->page_cache = NULL;
457 G_OBJECT_CLASS (ev_page_action_parent_class)->dispose (object);
461 ev_page_action_set_property (GObject *object,
467 EvPageCache *page_cache;
470 page = EV_PAGE_ACTION (object);
474 case PROP_PAGE_CACHE:
475 page_cache = page->priv->page_cache;
476 page->priv->page_cache = EV_PAGE_CACHE (g_value_dup_object (value));
478 g_object_unref (page_cache);
481 model = page->priv->model;
482 page->priv->model = GTK_TREE_MODEL (g_value_dup_object (value));
484 g_object_unref (model);
487 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
493 ev_page_action_get_property (GObject *object,
500 page = EV_PAGE_ACTION (object);
504 case PROP_PAGE_CACHE:
505 g_value_set_object (value, page->priv->page_cache);
508 g_value_set_object (value, page->priv->model);
511 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
517 ev_page_action_set_document (EvPageAction *page, EvDocument *document)
519 EvPageCache *page_cache = NULL;
522 page_cache = ev_document_get_page_cache (document);
525 "page-cache", page_cache,
531 ev_page_action_set_model (EvPageAction *page_action,
534 g_object_set (page_action,
540 ev_page_action_grab_focus (EvPageAction *page_action)
544 proxies = gtk_action_get_proxies (GTK_ACTION (page_action));
545 for (; proxies != NULL; proxies = proxies->next) {
546 EvPageActionWidget *proxy;
548 proxy = EV_PAGE_ACTION_WIDGET (proxies->data);
549 gtk_widget_grab_focus (proxy->entry);
554 ev_page_action_init (EvPageAction *page)
556 page->priv = EV_PAGE_ACTION_GET_PRIVATE (page);
560 ev_page_action_class_init (EvPageActionClass *class)
562 GObjectClass *object_class = G_OBJECT_CLASS (class);
563 GtkActionClass *action_class = GTK_ACTION_CLASS (class);
565 object_class->dispose = ev_page_action_dispose;
566 object_class->set_property = ev_page_action_set_property;
567 object_class->get_property = ev_page_action_get_property;
569 action_class->toolbar_item_type = GTK_TYPE_TOOL_ITEM;
570 action_class->create_tool_item = create_tool_item;
571 action_class->connect_proxy = connect_proxy;
573 g_object_class_install_property (object_class,
575 g_param_spec_object ("page-cache",
577 "Current page cache",
581 g_object_class_install_property (object_class,
583 g_param_spec_object ("model",
589 g_type_class_add_private (object_class, sizeof (EvPageActionPrivate));