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 if (ev_page_cache_has_nonnumeric_page_labels (page_cache)) {
107 label_text = g_strdup_printf (_("(%d of %d)"), page + 1, n_pages);
109 label_text = g_strdup_printf (_("of %d"), n_pages);
111 gtk_label_set_text (GTK_LABEL (proxy->label), label_text);
116 page_changed_cb (EvPageCache *page_cache,
118 EvPageActionWidget *proxy)
122 if (page_cache != NULL && page >= 0) {
123 gchar *page_label = ev_page_cache_get_page_label (page_cache, page);
124 gtk_entry_set_text (GTK_ENTRY (proxy->entry), page_label);
125 gtk_editable_set_position (GTK_EDITABLE (proxy->entry), -1);
128 gtk_entry_set_text (GTK_ENTRY (proxy->entry), "");
131 update_pages_label (proxy, page, page_cache);
135 activate_cb (GtkWidget *entry, GtkAction *action)
137 EvPageAction *page = EV_PAGE_ACTION (action);
138 EvPageCache *page_cache;
141 text = gtk_entry_get_text (GTK_ENTRY (entry));
142 page_cache = page->priv->page_cache;
144 if (! ev_page_cache_set_page_label (page_cache, text)) {
145 /* rest the entry to the current page if we were unable to
148 ev_page_cache_get_page_label (page_cache,
149 ev_page_cache_get_current_page (page_cache));
150 gtk_entry_set_text (GTK_ENTRY (entry), page_label);
151 gtk_editable_set_position (GTK_EDITABLE (entry), -1);
157 create_tool_item (GtkAction *action)
159 EvPageActionWidget *proxy;
162 proxy = g_object_new (ev_page_action_widget_get_type (), NULL);
163 gtk_container_set_border_width (GTK_CONTAINER (proxy), 6);
164 gtk_widget_show (GTK_WIDGET (proxy));
166 hbox = gtk_hbox_new (FALSE, 0);
167 gtk_box_set_spacing (GTK_BOX (hbox), 6);
169 proxy->entry = gtk_entry_new ();
170 gtk_entry_set_width_chars (GTK_ENTRY (proxy->entry), 5);
171 gtk_box_pack_start (GTK_BOX (hbox), proxy->entry, FALSE, FALSE, 0);
172 gtk_widget_show (proxy->entry);
173 g_signal_connect (proxy->entry, "activate",
174 G_CALLBACK (activate_cb),
177 proxy->label = gtk_label_new (NULL);
178 gtk_box_pack_start (GTK_BOX (hbox), proxy->label, FALSE, FALSE, 0);
179 gtk_widget_show (proxy->label);
181 gtk_container_add (GTK_CONTAINER (proxy), hbox);
182 gtk_widget_show (hbox);
184 return GTK_WIDGET (proxy);
188 update_page_cache (EvPageAction *page, GParamSpec *pspec, EvPageActionWidget *proxy)
190 EvPageCache *page_cache;
193 page_cache = page->priv->page_cache;
195 /* clear the old signal */
196 if (proxy->signal_id > 0 && proxy->page_cache)
197 g_signal_handler_disconnect (proxy->page_cache, proxy->signal_id);
199 if (page_cache != NULL) {
200 signal_id = g_signal_connect_object (page_cache,
202 G_CALLBACK (page_changed_cb),
204 /* Set the initial value */
205 page_changed_cb (page_cache,
206 ev_page_cache_get_current_page (page_cache),
209 /* Or clear the entry */
211 page_changed_cb (NULL, 0, proxy);
213 proxy->page_cache = page_cache;
214 proxy->signal_id = signal_id;
218 build_new_tree_cb (GtkTreeModel *model,
223 GtkTreeModel *filter_model = GTK_TREE_MODEL (data);
226 gtk_tree_model_get (model, iter,
227 EV_DOCUMENT_LINKS_COLUMN_LINK, &link,
230 if (link && ev_link_get_link_type (link) == EV_LINK_TYPE_PAGE) {
231 GtkTreeIter filter_iter;
233 gtk_list_store_append (GTK_LIST_STORE (filter_model), &filter_iter);
234 gtk_list_store_set (GTK_LIST_STORE (filter_model), &filter_iter,
242 static GtkTreeModel *
243 get_filter_model_from_model (GtkTreeModel *model)
245 GtkTreeModel *filter_model;
248 (GtkTreeModel *) g_object_get_data (G_OBJECT (model), EPA_FILTER_MODEL_DATA);
249 if (filter_model == NULL) {
250 filter_model = (GtkTreeModel *) gtk_list_store_new (1, GTK_TYPE_TREE_ITER);
252 gtk_tree_model_foreach (model,
255 g_object_set_data_full (G_OBJECT (model), EPA_FILTER_MODEL_DATA, filter_model, g_object_unref);
262 match_selected_cb (GtkEntryCompletion *completion,
263 GtkTreeModel *filter_model,
264 GtkTreeIter *filter_iter,
265 EvPageActionWidget *proxy)
270 gtk_tree_model_get (filter_model, filter_iter,
273 gtk_tree_model_get (proxy->model, iter,
274 EV_DOCUMENT_LINKS_COLUMN_LINK, &link,
276 ev_page_cache_set_link (proxy->page_cache, link);
283 display_completion_text (GtkCellLayout *cell_layout,
284 GtkCellRenderer *renderer,
285 GtkTreeModel *filter_model,
286 GtkTreeIter *filter_iter,
287 EvPageActionWidget *proxy)
292 gtk_tree_model_get (filter_model, filter_iter,
295 gtk_tree_model_get (proxy->model, iter,
296 EV_DOCUMENT_LINKS_COLUMN_LINK, &link,
299 g_object_set (renderer, "text", ev_link_get_title (link), NULL);
303 match_completion (GtkEntryCompletion *completion,
305 GtkTreeIter *filter_iter,
306 EvPageActionWidget *proxy)
310 const gchar *text = NULL;
312 gtk_tree_model_get (gtk_entry_completion_get_model (completion),
316 gtk_tree_model_get (proxy->model, iter,
317 EV_DOCUMENT_LINKS_COLUMN_LINK, &link,
322 text = ev_link_get_title (link);
325 gchar *normalized_text;
326 gchar *normalized_key;
327 gchar *case_normalized_text;
328 gchar *case_normalized_key;
329 gboolean retval = FALSE;
331 normalized_text = g_utf8_normalize (text, -1, G_NORMALIZE_ALL);
332 normalized_key = g_utf8_normalize (key, -1, G_NORMALIZE_ALL);
333 case_normalized_text = g_utf8_casefold (normalized_text, -1);
334 case_normalized_key = g_utf8_casefold (normalized_key, -1);
336 if (strstr (case_normalized_text, case_normalized_key))
339 g_free (normalized_text);
340 g_free (normalized_key);
341 g_free (case_normalized_text);
342 g_free (case_normalized_key);
352 update_model (EvPageAction *page, GParamSpec *pspec, EvPageActionWidget *proxy)
355 GtkTreeModel *filter_model;
357 g_object_get (G_OBJECT (page),
362 GtkEntryCompletion *completion;
363 GtkCellRenderer *renderer;
365 proxy->model = model;
366 filter_model = get_filter_model_from_model (model);
368 completion = gtk_entry_completion_new ();
370 /* popup-set-width is 2.7.0 only */
371 g_object_set (G_OBJECT (completion),
372 "popup-set-width", FALSE,
373 "model", filter_model,
376 g_signal_connect (completion, "match-selected", G_CALLBACK (match_selected_cb), proxy);
377 gtk_entry_completion_set_match_func (completion,
378 (GtkEntryCompletionMatchFunc) match_completion,
381 /* Set up the layout */
382 renderer = (GtkCellRenderer *)
383 g_object_new (GTK_TYPE_CELL_RENDERER_TEXT,
384 "ellipsize", PANGO_ELLIPSIZE_END,
387 gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (completion), renderer, TRUE);
388 gtk_cell_layout_set_cell_data_func (GTK_CELL_LAYOUT (completion),
390 (GtkCellLayoutDataFunc) display_completion_text,
392 gtk_entry_set_completion (GTK_ENTRY (proxy->entry), completion);
397 connect_proxy (GtkAction *action, GtkWidget *proxy)
399 if (GTK_IS_TOOL_ITEM (proxy)) {
400 g_signal_connect_object (action, "notify::page-cache",
401 G_CALLBACK (update_page_cache),
403 /* We only go through this whole rigmarole if we can set
404 * GtkEntryCompletion::popup-set-width, which appeared in
406 if (gtk_check_version (2, 7, 0) == NULL) {
407 g_signal_connect_object (action, "notify::model",
408 G_CALLBACK (update_model),
413 GTK_ACTION_CLASS (ev_page_action_parent_class)->connect_proxy (action, proxy);
417 ev_page_action_dispose (GObject *object)
419 EvPageAction *page = EV_PAGE_ACTION (object);
421 if (page->priv->page_cache) {
422 g_object_unref (page->priv->page_cache);
423 page->priv->page_cache = NULL;
426 G_OBJECT_CLASS (ev_page_action_parent_class)->dispose (object);
430 ev_page_action_set_property (GObject *object,
436 EvPageCache *page_cache;
439 page = EV_PAGE_ACTION (object);
443 case PROP_PAGE_CACHE:
444 page_cache = page->priv->page_cache;
445 page->priv->page_cache = EV_PAGE_CACHE (g_value_dup_object (value));
447 g_object_unref (page_cache);
450 model = page->priv->model;
451 page->priv->model = GTK_TREE_MODEL (g_value_dup_object (value));
453 g_object_unref (model);
456 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
462 ev_page_action_get_property (GObject *object,
469 page = EV_PAGE_ACTION (object);
473 case PROP_PAGE_CACHE:
474 g_value_set_object (value, page->priv->page_cache);
477 g_value_set_object (value, page->priv->model);
480 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
486 ev_page_action_set_document (EvPageAction *page, EvDocument *document)
488 EvPageCache *page_cache = NULL;
491 page_cache = ev_document_get_page_cache (document);
494 "page-cache", page_cache,
500 ev_page_action_set_model (EvPageAction *page_action,
503 g_object_set (page_action,
509 ev_page_action_init (EvPageAction *page)
511 page->priv = EV_PAGE_ACTION_GET_PRIVATE (page);
515 ev_page_action_class_init (EvPageActionClass *class)
517 GObjectClass *object_class = G_OBJECT_CLASS (class);
518 GtkActionClass *action_class = GTK_ACTION_CLASS (class);
520 object_class->dispose = ev_page_action_dispose;
521 object_class->set_property = ev_page_action_set_property;
522 object_class->get_property = ev_page_action_get_property;
524 action_class->toolbar_item_type = GTK_TYPE_TOOL_ITEM;
525 action_class->create_tool_item = create_tool_item;
526 action_class->connect_proxy = connect_proxy;
528 g_object_class_install_property (object_class,
530 g_param_spec_object ("page-cache",
532 "Current page cache",
536 g_object_class_install_property (object_class,
538 g_param_spec_object ("model",
544 g_type_class_add_private (object_class, sizeof (EvPageActionPrivate));