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 <glib/gi18n.h>
27 #include <evince-document.h>
28 #include "ev-page-action.h"
29 #include "ev-page-action-widget.h"
31 /* Widget we pass back */
32 static void ev_page_action_widget_init (EvPageActionWidget *action_widget);
33 static void ev_page_action_widget_class_init (EvPageActionWidgetClass *action_widget);
41 struct _EvPageActionWidget
46 EvDocumentModel *doc_model;
51 GtkTreeModel *filter_model;
55 static guint widget_signals[WIDGET_N_SIGNALS] = {0, };
57 G_DEFINE_TYPE (EvPageActionWidget, ev_page_action_widget, GTK_TYPE_TOOL_ITEM)
60 update_pages_label (EvPageActionWidget *action_widget,
66 n_pages = ev_document_get_n_pages (action_widget->document);
67 if (ev_document_has_text_page_labels (action_widget->document)) {
68 label_text = g_strdup_printf (_("(%d of %d)"), page + 1, n_pages);
70 label_text = g_strdup_printf (_("of %d"), n_pages);
72 gtk_label_set_text (GTK_LABEL (action_widget->label), label_text);
77 ev_page_action_widget_set_current_page (EvPageActionWidget *action_widget,
83 gtk_entry_set_width_chars (GTK_ENTRY (action_widget->entry),
84 CLAMP (ev_document_get_max_label_len (action_widget->document),
87 page_label = ev_document_get_page_label (action_widget->document, page);
88 gtk_entry_set_text (GTK_ENTRY (action_widget->entry), page_label);
89 gtk_editable_set_position (GTK_EDITABLE (action_widget->entry), -1);
93 gtk_entry_set_text (GTK_ENTRY (action_widget->entry), "");
96 update_pages_label (action_widget, page);
100 page_changed_cb (EvDocumentModel *model,
103 EvPageActionWidget *action_widget)
105 ev_page_action_widget_set_current_page (action_widget, new_page);
109 page_scroll_cb (EvPageActionWidget *action_widget, GdkEventScroll *event)
111 EvDocumentModel *model = action_widget->doc_model;
114 pageno = ev_document_model_get_page (model);
115 if ((event->direction == GDK_SCROLL_DOWN) &&
116 (pageno < ev_document_get_n_pages (action_widget->document) - 1))
118 if ((event->direction == GDK_SCROLL_UP) && (pageno > 0))
120 ev_document_model_set_page (model, pageno);
126 activate_cb (EvPageActionWidget *action_widget)
128 EvDocumentModel *model;
131 EvLinkDest *link_dest;
132 EvLinkAction *link_action;
136 text = gtk_entry_get_text (GTK_ENTRY (action_widget->entry));
138 link_dest = ev_link_dest_new_page_label (text);
139 link_action = ev_link_action_new_dest (link_dest);
140 link_text = g_strdup_printf ("Page: %s", text);
141 link = ev_link_new (link_text, link_action);
143 g_signal_emit (action_widget, widget_signals[WIDGET_ACTIVATE_LINK], 0, link);
145 g_object_unref (link);
148 /* rest the entry to the current page if we were unable to
150 model = action_widget->doc_model;
151 page_label = ev_document_get_page_label (action_widget->document,
152 ev_document_model_get_page (model));
153 gtk_entry_set_text (GTK_ENTRY (action_widget->entry), page_label);
154 gtk_editable_set_position (GTK_EDITABLE (action_widget->entry), -1);
159 ev_page_action_widget_init (EvPageActionWidget *action_widget)
164 hbox = gtk_hbox_new (FALSE, 0);
165 gtk_box_set_spacing (GTK_BOX (hbox), 6);
167 action_widget->entry = gtk_entry_new ();
168 gtk_widget_add_events (action_widget->entry,
169 GDK_BUTTON_MOTION_MASK);
170 gtk_entry_set_width_chars (GTK_ENTRY (action_widget->entry), 5);
171 gtk_entry_set_text (GTK_ENTRY (action_widget->entry), "");
172 g_signal_connect_swapped (action_widget->entry, "scroll-event",
173 G_CALLBACK (page_scroll_cb),
175 g_signal_connect_swapped (action_widget->entry, "activate",
176 G_CALLBACK (activate_cb),
179 obj = gtk_widget_get_accessible (action_widget->entry);
180 atk_object_set_name (obj, "page-label-entry");
182 gtk_box_pack_start (GTK_BOX (hbox), action_widget->entry,
184 gtk_widget_show (action_widget->entry);
186 action_widget->label = gtk_label_new (NULL);
187 gtk_box_pack_start (GTK_BOX (hbox), action_widget->label,
189 gtk_widget_show (action_widget->label);
191 gtk_container_set_border_width (GTK_CONTAINER (action_widget), 6);
192 gtk_container_add (GTK_CONTAINER (action_widget), hbox);
193 gtk_widget_show (hbox);
195 gtk_widget_show (GTK_WIDGET (action_widget));
199 ev_page_action_widget_document_changed_cb (EvDocumentModel *model,
201 EvPageActionWidget *action_widget)
203 EvDocument *document = ev_document_model_get_document (model);
205 g_object_ref (document);
206 if (action_widget->document)
207 g_object_unref (action_widget->document);
208 action_widget->document = document;
210 if (action_widget->signal_id > 0) {
211 g_signal_handler_disconnect (action_widget->doc_model,
212 action_widget->signal_id);
213 action_widget->signal_id = 0;
215 action_widget->signal_id =
216 g_signal_connect_object (action_widget->doc_model,
218 G_CALLBACK (page_changed_cb),
221 ev_page_action_widget_set_current_page (action_widget,
222 ev_document_model_get_page (model));
226 ev_page_action_widget_set_model (EvPageActionWidget *action_widget,
227 EvDocumentModel *model)
229 if (action_widget->doc_model) {
230 g_object_remove_weak_pointer (G_OBJECT (action_widget->doc_model),
231 (gpointer)&action_widget->doc_model);
233 action_widget->doc_model = model;
234 g_object_add_weak_pointer (G_OBJECT (model),
235 (gpointer)&action_widget->doc_model);
237 g_signal_connect (model, "notify::document",
238 G_CALLBACK (ev_page_action_widget_document_changed_cb),
243 ev_page_action_widget_finalize (GObject *object)
245 EvPageActionWidget *action_widget = EV_PAGE_ACTION_WIDGET (object);
247 if (action_widget->doc_model != NULL) {
248 if (action_widget->signal_id > 0) {
249 g_signal_handler_disconnect (action_widget->doc_model,
250 action_widget->signal_id);
251 action_widget->signal_id = 0;
253 g_object_remove_weak_pointer (G_OBJECT (action_widget->doc_model),
254 (gpointer)&action_widget->doc_model);
255 action_widget->doc_model = NULL;
258 if (action_widget->document) {
259 g_object_unref (action_widget->document);
260 action_widget->document = NULL;
263 G_OBJECT_CLASS (ev_page_action_widget_parent_class)->finalize (object);
267 ev_page_action_widget_class_init (EvPageActionWidgetClass *class)
269 GObjectClass *object_class = G_OBJECT_CLASS (class);
271 object_class->finalize = ev_page_action_widget_finalize;
273 widget_signals[WIDGET_ACTIVATE_LINK] =
274 g_signal_new ("activate_link",
275 G_OBJECT_CLASS_TYPE (object_class),
276 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
277 G_STRUCT_OFFSET (EvPageActionClass, activate_link),
279 g_cclosure_marshal_VOID__OBJECT,
286 match_selected_cb (GtkEntryCompletion *completion,
287 GtkTreeModel *filter_model,
288 GtkTreeIter *filter_iter,
289 EvPageActionWidget *proxy)
294 gtk_tree_model_get (filter_model, filter_iter,
297 gtk_tree_model_get (proxy->model, iter,
298 EV_DOCUMENT_LINKS_COLUMN_LINK, &link,
301 g_signal_emit (proxy, widget_signals[WIDGET_ACTIVATE_LINK], 0, link);
304 g_object_unref (link);
306 gtk_tree_iter_free (iter);
313 display_completion_text (GtkCellLayout *cell_layout,
314 GtkCellRenderer *renderer,
315 GtkTreeModel *filter_model,
316 GtkTreeIter *filter_iter,
317 EvPageActionWidget *proxy)
322 gtk_tree_model_get (filter_model, filter_iter,
325 gtk_tree_model_get (proxy->model, iter,
326 EV_DOCUMENT_LINKS_COLUMN_LINK, &link,
329 g_object_set (renderer, "text", ev_link_get_title (link), NULL);
332 g_object_unref (link);
334 gtk_tree_iter_free (iter);
338 match_completion (GtkEntryCompletion *completion,
340 GtkTreeIter *filter_iter,
341 EvPageActionWidget *proxy)
345 const gchar *text = NULL;
347 gtk_tree_model_get (gtk_entry_completion_get_model (completion),
351 gtk_tree_model_get (proxy->model, iter,
352 EV_DOCUMENT_LINKS_COLUMN_LINK, &link,
357 text = ev_link_get_title (link);
358 g_object_unref (link);
361 gtk_tree_iter_free (iter);
364 gchar *normalized_text;
365 gchar *normalized_key;
366 gchar *case_normalized_text;
367 gchar *case_normalized_key;
368 gboolean retval = FALSE;
370 normalized_text = g_utf8_normalize (text, -1, G_NORMALIZE_ALL);
371 normalized_key = g_utf8_normalize (key, -1, G_NORMALIZE_ALL);
372 case_normalized_text = g_utf8_casefold (normalized_text, -1);
373 case_normalized_key = g_utf8_casefold (normalized_key, -1);
375 if (strstr (case_normalized_text, case_normalized_key))
378 g_free (normalized_text);
379 g_free (normalized_key);
380 g_free (case_normalized_text);
381 g_free (case_normalized_key);
389 /* user data to set on the widget. */
390 #define EPA_FILTER_MODEL_DATA "epa-filter-model"
393 build_new_tree_cb (GtkTreeModel *model,
398 GtkTreeModel *filter_model = GTK_TREE_MODEL (data);
400 EvLinkAction *action;
401 EvLinkActionType type;
403 gtk_tree_model_get (model, iter,
404 EV_DOCUMENT_LINKS_COLUMN_LINK, &link,
410 action = ev_link_get_action (link);
412 g_object_unref (link);
416 type = ev_link_action_get_action_type (action);
418 if (type == EV_LINK_ACTION_TYPE_GOTO_DEST) {
419 GtkTreeIter filter_iter;
421 gtk_list_store_append (GTK_LIST_STORE (filter_model), &filter_iter);
422 gtk_list_store_set (GTK_LIST_STORE (filter_model), &filter_iter,
427 g_object_unref (link);
432 static GtkTreeModel *
433 get_filter_model_from_model (GtkTreeModel *model)
435 GtkTreeModel *filter_model;
438 (GtkTreeModel *) g_object_get_data (G_OBJECT (model), EPA_FILTER_MODEL_DATA);
439 if (filter_model == NULL) {
440 filter_model = (GtkTreeModel *) gtk_list_store_new (1, GTK_TYPE_TREE_ITER);
442 gtk_tree_model_foreach (model,
445 g_object_set_data_full (G_OBJECT (model), EPA_FILTER_MODEL_DATA, filter_model, g_object_unref);
453 ev_page_action_widget_update_links_model (EvPageActionWidget *proxy, GtkTreeModel *model)
455 GtkTreeModel *filter_model;
456 GtkEntryCompletion *completion;
457 GtkCellRenderer *renderer;
463 proxy->model = model;
464 filter_model = get_filter_model_from_model (model);
466 completion = gtk_entry_completion_new ();
467 g_object_set (G_OBJECT (completion),
468 "popup-set-width", FALSE,
469 "model", filter_model,
472 g_signal_connect (completion, "match-selected", G_CALLBACK (match_selected_cb), proxy);
473 gtk_entry_completion_set_match_func (completion,
474 (GtkEntryCompletionMatchFunc) match_completion,
477 /* Set up the layout */
478 renderer = (GtkCellRenderer *)
479 g_object_new (GTK_TYPE_CELL_RENDERER_TEXT,
480 "ellipsize", PANGO_ELLIPSIZE_END,
483 gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (completion), renderer, TRUE);
484 gtk_cell_layout_set_cell_data_func (GTK_CELL_LAYOUT (completion),
486 (GtkCellLayoutDataFunc) display_completion_text,
488 gtk_entry_set_completion (GTK_ENTRY (proxy->entry), completion);
490 g_object_unref (completion);
494 ev_page_action_widget_grab_focus (EvPageActionWidget *proxy)
496 gtk_widget_grab_focus (proxy->entry);