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 G_DEFINE_TYPE (EvPageActionWidget, ev_page_action_widget, GTK_TYPE_TOOL_ITEM)
69 ev_page_action_widget_init (EvPageActionWidget *action_widget)
75 ev_page_action_widget_class_init (EvPageActionWidgetClass *action_widget)
81 static void ev_page_action_init (EvPageAction *action);
82 static void ev_page_action_class_init (EvPageActionClass *class);
84 G_DEFINE_TYPE (EvPageAction, ev_page_action, GTK_TYPE_ACTION)
86 #define EV_PAGE_ACTION_GET_PRIVATE(object)(G_TYPE_INSTANCE_GET_PRIVATE ((object), EV_TYPE_PAGE_ACTION, EvPageActionPrivate))
94 /* user data to set on the widget. */
95 #define EPA_FILTER_MODEL_DATA "epa-filter-model"
98 update_pages_label (EvPageActionWidget *proxy,
100 EvPageCache *page_cache)
105 n_pages = page_cache ? ev_page_cache_get_n_pages (page_cache) : 0;
106 label_text = g_strdup_printf (_("(%d of %d)"), page + 1, n_pages);
107 gtk_label_set_text (GTK_LABEL (proxy->label), label_text);
112 page_changed_cb (EvPageCache *page_cache,
114 EvPageActionWidget *proxy)
118 if (page_cache != NULL && page >= 0) {
119 gchar *page_label = ev_page_cache_get_page_label (page_cache, page);
120 gtk_entry_set_text (GTK_ENTRY (proxy->entry), page_label);
121 gtk_editable_set_position (GTK_EDITABLE (proxy->entry), -1);
124 gtk_entry_set_text (GTK_ENTRY (proxy->entry), "");
127 update_pages_label (proxy, page, page_cache);
131 activate_cb (GtkWidget *entry, GtkAction *action)
133 EvPageAction *page = EV_PAGE_ACTION (action);
134 EvPageCache *page_cache;
137 text = gtk_entry_get_text (GTK_ENTRY (entry));
138 page_cache = page->priv->page_cache;
140 if (! ev_page_cache_set_page_label (page_cache, text)) {
141 /* rest the entry to the current page if we were unable to
144 ev_page_cache_get_page_label (page_cache,
145 ev_page_cache_get_current_page (page_cache));
146 gtk_entry_set_text (GTK_ENTRY (entry), page_label);
147 gtk_editable_set_position (GTK_EDITABLE (entry), -1);
153 create_tool_item (GtkAction *action)
155 EvPageActionWidget *proxy;
158 proxy = g_object_new (ev_page_action_widget_get_type (), NULL);
159 gtk_container_set_border_width (GTK_CONTAINER (proxy), 6);
160 gtk_widget_show (GTK_WIDGET (proxy));
162 hbox = gtk_hbox_new (FALSE, 0);
163 gtk_box_set_spacing (GTK_BOX (hbox), 6);
165 proxy->entry = gtk_entry_new ();
166 gtk_entry_set_width_chars (GTK_ENTRY (proxy->entry), 5);
167 gtk_box_pack_start (GTK_BOX (hbox), proxy->entry, FALSE, FALSE, 0);
168 gtk_widget_show (proxy->entry);
169 g_signal_connect (proxy->entry, "activate",
170 G_CALLBACK (activate_cb),
173 proxy->label = gtk_label_new (NULL);
174 gtk_box_pack_start (GTK_BOX (hbox), proxy->label, FALSE, FALSE, 0);
175 gtk_widget_show (proxy->label);
177 gtk_container_add (GTK_CONTAINER (proxy), hbox);
178 gtk_widget_show (hbox);
180 return GTK_WIDGET (proxy);
184 update_page_cache (EvPageAction *page, GParamSpec *pspec, EvPageActionWidget *proxy)
186 EvPageCache *page_cache;
189 page_cache = page->priv->page_cache;
191 /* clear the old signal */
192 if (proxy->signal_id > 0 && proxy->page_cache)
193 g_signal_handler_disconnect (proxy->page_cache, proxy->signal_id);
195 if (page_cache != NULL) {
196 signal_id = g_signal_connect (page_cache,
198 G_CALLBACK (page_changed_cb),
200 /* Set the initial value */
201 page_changed_cb (page_cache,
202 ev_page_cache_get_current_page (page_cache),
205 /* Or clear the entry */
207 page_changed_cb (NULL, 0, proxy);
209 proxy->page_cache = page_cache;
210 proxy->signal_id = signal_id;
214 build_new_tree_cb (GtkTreeModel *model,
219 GtkTreeModel *filter_model = GTK_TREE_MODEL (data);
222 gtk_tree_model_get (model, iter,
223 EV_DOCUMENT_LINKS_COLUMN_LINK, &link,
226 if (link && ev_link_get_link_type (link) == EV_LINK_TYPE_PAGE) {
227 GtkTreeIter filter_iter;
229 gtk_list_store_append (GTK_LIST_STORE (filter_model), &filter_iter);
230 gtk_list_store_set (GTK_LIST_STORE (filter_model), &filter_iter,
238 static GtkTreeModel *
239 get_filter_model_from_model (GtkTreeModel *model)
241 GtkTreeModel *filter_model;
244 (GtkTreeModel *) g_object_get_data (G_OBJECT (model), EPA_FILTER_MODEL_DATA);
245 if (filter_model == NULL) {
246 filter_model = (GtkTreeModel *) gtk_list_store_new (1, GTK_TYPE_TREE_ITER);
248 gtk_tree_model_foreach (model,
251 g_object_set_data_full (G_OBJECT (model), EPA_FILTER_MODEL_DATA, filter_model, g_object_unref);
258 match_selected_cb (GtkEntryCompletion *completion,
259 GtkTreeModel *filter_model,
260 GtkTreeIter *filter_iter,
261 EvPageActionWidget *proxy)
266 gtk_tree_model_get (filter_model, filter_iter,
269 gtk_tree_model_get (proxy->model, iter,
270 EV_DOCUMENT_LINKS_COLUMN_LINK, &link,
272 ev_page_cache_set_link (proxy->page_cache, link);
279 display_completion_text (GtkCellLayout *cell_layout,
280 GtkCellRenderer *renderer,
281 GtkTreeModel *filter_model,
282 GtkTreeIter *filter_iter,
283 EvPageActionWidget *proxy)
288 gtk_tree_model_get (filter_model, filter_iter,
291 gtk_tree_model_get (proxy->model, iter,
292 EV_DOCUMENT_LINKS_COLUMN_LINK, &link,
295 g_object_set (renderer, "text", ev_link_get_title (link), NULL);
299 match_completion (GtkEntryCompletion *completion,
301 GtkTreeIter *filter_iter,
302 EvPageActionWidget *proxy)
306 const gchar *text = NULL;
308 gtk_tree_model_get (gtk_entry_completion_get_model (completion),
312 gtk_tree_model_get (proxy->model, iter,
313 EV_DOCUMENT_LINKS_COLUMN_LINK, &link,
318 text = ev_link_get_title (link);
321 gchar *normalized_text;
322 gchar *normalized_key;
323 gchar *case_normalized_text;
324 gchar *case_normalized_key;
325 gboolean retval = FALSE;
327 normalized_text = g_utf8_normalize (text, -1, G_NORMALIZE_ALL);
328 normalized_key = g_utf8_normalize (key, -1, G_NORMALIZE_ALL);
329 case_normalized_text = g_utf8_casefold (normalized_text, -1);
330 case_normalized_key = g_utf8_casefold (normalized_key, -1);
332 if (strstr (case_normalized_text, case_normalized_key))
335 g_free (normalized_text);
336 g_free (normalized_key);
337 g_free (case_normalized_text);
338 g_free (case_normalized_key);
348 update_model (EvPageAction *page, GParamSpec *pspec, EvPageActionWidget *proxy)
351 GtkTreeModel *filter_model;
353 g_object_get (G_OBJECT (page),
358 GtkEntryCompletion *completion;
359 GtkCellRenderer *renderer;
361 proxy->model = model;
362 filter_model = get_filter_model_from_model (model);
364 completion = gtk_entry_completion_new ();
366 /* popup-set-width is 2.7.0 only */
367 g_object_set (G_OBJECT (completion),
368 "popup-set-width", FALSE,
369 "model", filter_model,
372 g_signal_connect (completion, "match-selected", G_CALLBACK (match_selected_cb), proxy);
373 gtk_entry_completion_set_match_func (completion,
374 (GtkEntryCompletionMatchFunc) match_completion,
377 /* Set up the layout */
378 renderer = (GtkCellRenderer *)
379 g_object_new (GTK_TYPE_CELL_RENDERER_TEXT,
380 "ellipsize", PANGO_ELLIPSIZE_END,
383 gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (completion), renderer, TRUE);
384 gtk_cell_layout_set_cell_data_func (GTK_CELL_LAYOUT (completion),
386 (GtkCellLayoutDataFunc) display_completion_text,
388 gtk_entry_set_completion (GTK_ENTRY (proxy->entry), completion);
393 connect_proxy (GtkAction *action, GtkWidget *proxy)
395 if (GTK_IS_TOOL_ITEM (proxy)) {
396 g_signal_connect_object (action, "notify::page-cache",
397 G_CALLBACK (update_page_cache),
399 /* We only go through this whole rigmarole if we can set
400 * GtkEntryCompletion::popup-set-width, which appeared in
402 if (gtk_check_version (2, 7, 0) == NULL) {
403 g_signal_connect_object (action, "notify::model",
404 G_CALLBACK (update_model),
409 GTK_ACTION_CLASS (ev_page_action_parent_class)->connect_proxy (action, proxy);
413 ev_page_action_dispose (GObject *object)
415 EvPageAction *page = EV_PAGE_ACTION (object);
417 if (page->priv->page_cache) {
418 g_object_unref (page->priv->page_cache);
419 page->priv->page_cache = NULL;
422 G_OBJECT_CLASS (ev_page_action_parent_class)->dispose (object);
426 ev_page_action_set_property (GObject *object,
432 EvPageCache *page_cache;
435 page = EV_PAGE_ACTION (object);
439 case PROP_PAGE_CACHE:
440 page_cache = page->priv->page_cache;
441 page->priv->page_cache = EV_PAGE_CACHE (g_value_dup_object (value));
443 g_object_unref (page_cache);
446 model = page->priv->model;
447 page->priv->model = GTK_TREE_MODEL (g_value_dup_object (value));
449 g_object_unref (model);
452 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
458 ev_page_action_get_property (GObject *object,
465 page = EV_PAGE_ACTION (object);
469 case PROP_PAGE_CACHE:
470 g_value_set_object (value, page->priv->page_cache);
473 g_value_set_object (value, page->priv->model);
476 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
482 ev_page_action_set_document (EvPageAction *page, EvDocument *document)
484 EvPageCache *page_cache = NULL;
487 page_cache = ev_document_get_page_cache (document);
490 "page-cache", page_cache,
496 ev_page_action_set_model (EvPageAction *page_action,
499 g_object_set (page_action,
505 ev_page_action_init (EvPageAction *page)
507 page->priv = EV_PAGE_ACTION_GET_PRIVATE (page);
511 ev_page_action_class_init (EvPageActionClass *class)
513 GObjectClass *object_class = G_OBJECT_CLASS (class);
514 GtkActionClass *action_class = GTK_ACTION_CLASS (class);
516 object_class->dispose = ev_page_action_dispose;
517 object_class->set_property = ev_page_action_set_property;
518 object_class->get_property = ev_page_action_get_property;
520 action_class->toolbar_item_type = GTK_TYPE_TOOL_ITEM;
521 action_class->create_tool_item = create_tool_item;
522 action_class->connect_proxy = connect_proxy;
524 g_object_class_install_property (object_class,
526 g_param_spec_object ("page-cache",
528 "Current page cache",
532 g_object_class_install_property (object_class,
534 g_param_spec_object ("model",
540 g_type_class_add_private (object_class, sizeof (EvPageActionPrivate));