1 /*
2  *  Copyright (C) 2003, 2004 Marco Pesenti Gritti
3  *  Copyright (C) 2003, 2004 Christian Persch
4  *
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)
8  *  any later version.
9  *
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.
14  *
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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18  *
19  */
20 
21 #include "config.h"
22 
23 #include <string.h>
24 #include <glib/gi18n.h>
25 #include <gtk/gtk.h>
26 
27 #include <xreader-document.h>
28 #include "ev-page-action.h"
29 #include "ev-page-action-widget.h"
30 
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);
34 
35 enum
36 {
37 	WIDGET_ACTIVATE_LINK,
38 	WIDGET_N_SIGNALS
39 };
40 
41 struct _EvPageActionWidget
42 {
43 	GtkToolItem parent;
44 
45 	EvDocument *document;
46 	EvDocumentModel *doc_model;
47 
48 	GtkWidget *entry;
49 	GtkWidget *label;
50 	guint signal_id;
51 	GtkTreeModel *filter_model;
52 	GtkTreeModel *model;
53 };
54 
55 static guint widget_signals[WIDGET_N_SIGNALS] = {0, };
56 
G_DEFINE_TYPE(EvPageActionWidget,ev_page_action_widget,GTK_TYPE_TOOL_ITEM)57 G_DEFINE_TYPE (EvPageActionWidget, ev_page_action_widget, GTK_TYPE_TOOL_ITEM)
58 
59 static gboolean
60 show_page_number_in_pages_label (EvPageActionWidget *action_widget,
61                                  gint                page)
62 {
63         gchar   *page_label;
64         gboolean retval;
65 
66         if (!ev_document_has_text_page_labels (action_widget->document))
67                 return FALSE;
68 
69         page_label = g_strdup_printf ("%d", page + 1);
70         retval = g_strcmp0 (page_label, gtk_entry_get_text (GTK_ENTRY (action_widget->entry))) != 0;
71         g_free (page_label);
72 
73         return retval;
74 }
75 
76 static void
update_pages_label(EvPageActionWidget * action_widget,gint page)77 update_pages_label (EvPageActionWidget *action_widget,
78 		    gint                page)
79 {
80 	char *label_text;
81 	gint n_pages;
82 
83 	n_pages = ev_document_get_n_pages (action_widget->document);
84 	if (show_page_number_in_pages_label (action_widget, page))
85 		label_text = g_strdup_printf (_("(%d of %d)"), page + 1, n_pages);
86 	else
87 		label_text = g_strdup_printf (_("of %d"), n_pages);
88 	gtk_entry_set_text (GTK_ENTRY (action_widget->label), label_text);
89 	g_free (label_text);
90 }
91 
92 static void
ev_page_action_widget_set_current_page(EvPageActionWidget * action_widget,gint page)93 ev_page_action_widget_set_current_page (EvPageActionWidget *action_widget,
94 					gint                page)
95 {
96 	if (page >= 0) {
97 		gchar *page_label;
98 
99 		page_label = ev_document_get_page_label (action_widget->document, page);
100 		gtk_entry_set_text (GTK_ENTRY (action_widget->entry), page_label);
101 		gtk_editable_set_position (GTK_EDITABLE (action_widget->entry), -1);
102 		g_free (page_label);
103 
104 	} else {
105 		gtk_entry_set_text (GTK_ENTRY (action_widget->entry), "");
106 	}
107 
108 	update_pages_label (action_widget, page);
109 }
110 
111 static void
ev_page_action_widget_update_max_width(EvPageActionWidget * action_widget)112 ev_page_action_widget_update_max_width (EvPageActionWidget *action_widget)
113 {
114         gchar *max_label;
115         gint   n_pages;
116         gint   max_label_len;
117         gchar *max_page_label;
118         gchar *max_page_numeric_label;
119 
120         n_pages = ev_document_get_n_pages (action_widget->document);
121 
122         max_page_label = ev_document_get_page_label (action_widget->document, n_pages - 1);
123         max_page_numeric_label = g_strdup_printf ("%d", n_pages);
124         if (strcmp (max_page_label, max_page_numeric_label) != 0)
125                 max_label = g_strdup_printf (_("(%d of %d)"), n_pages, n_pages);
126         else
127                 max_label = g_strdup_printf (_("of %d"), n_pages);
128         g_free (max_page_label);
129 
130         gtk_entry_set_width_chars (GTK_ENTRY (action_widget->label), strlen (max_label));
131         g_free (max_label);
132 
133         max_label_len = ev_document_get_max_label_len (action_widget->document);
134         gtk_entry_set_width_chars (GTK_ENTRY (action_widget->entry),
135                                    CLAMP (max_label_len, strlen (max_page_numeric_label) + 1, 12));
136         g_free (max_page_numeric_label);
137 }
138 
139 static void
page_changed_cb(EvDocumentModel * model,gint old_page,gint new_page,EvPageActionWidget * action_widget)140 page_changed_cb (EvDocumentModel    *model,
141 		 gint                old_page,
142 		 gint                new_page,
143 		 EvPageActionWidget *action_widget)
144 {
145 	ev_page_action_widget_set_current_page (action_widget, new_page);
146 }
147 
148 static gboolean
page_scroll_cb(EvPageActionWidget * action_widget,GdkEventScroll * event)149 page_scroll_cb (EvPageActionWidget *action_widget, GdkEventScroll *event)
150 {
151 	EvDocumentModel *model = action_widget->doc_model;
152 	gint pageno;
153 
154 	pageno = ev_document_model_get_page (model);
155 	if ((event->direction == GDK_SCROLL_DOWN) &&
156 	    (pageno < ev_document_get_n_pages (action_widget->document) - 1))
157 		pageno++;
158 	if ((event->direction == GDK_SCROLL_UP) && (pageno > 0))
159 		pageno--;
160 	ev_document_model_set_page (model, pageno);
161 
162 	return TRUE;
163 }
164 
165 static void
activate_cb(EvPageActionWidget * action_widget)166 activate_cb (EvPageActionWidget *action_widget)
167 {
168 	EvDocumentModel *model;
169 	const char *text;
170 	EvLinkDest *link_dest;
171 	EvLinkAction *link_action;
172 	EvLink *link;
173 	gchar *link_text;
174 	gint current_page;
175 
176 	model = action_widget->doc_model;
177 	current_page = ev_document_model_get_page (model);
178 
179 	text = gtk_entry_get_text (GTK_ENTRY (action_widget->entry));
180 
181 	link_dest = ev_link_dest_new_page_label (text);
182 	link_action = ev_link_action_new_dest (link_dest);
183 	link_text = g_strdup_printf ("Page: %s", text);
184 	link = ev_link_new (link_text, link_action);
185 
186 	g_signal_emit (action_widget, widget_signals[WIDGET_ACTIVATE_LINK], 0, link);
187 
188 	g_object_unref (link_dest);
189 	g_object_unref (link_action);
190 	g_object_unref (link);
191 	g_free (link_text);
192 
193 	if (current_page == ev_document_model_get_page (model))
194 		ev_page_action_widget_set_current_page (action_widget, current_page);
195 }
196 
197 static gboolean
focus_out_cb(EvPageActionWidget * action_widget)198 focus_out_cb (EvPageActionWidget *action_widget)
199 {
200 	ev_page_action_widget_set_current_page (action_widget, ev_document_model_get_page (action_widget->doc_model));
201 
202 	return FALSE;
203 }
204 
205 static void
ev_page_action_widget_init(EvPageActionWidget * action_widget)206 ev_page_action_widget_init (EvPageActionWidget *action_widget)
207 {
208 	GtkWidget *hbox;
209 	AtkObject *obj;
210 	GtkStyleContext *style_context;
211 
212 	hbox = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 0);
213 	style_context = gtk_widget_get_style_context (hbox);
214 	gtk_style_context_add_class (style_context, GTK_STYLE_CLASS_RAISED);
215 	gtk_style_context_add_class (style_context, GTK_STYLE_CLASS_LINKED);
216 	action_widget->entry = gtk_entry_new ();
217 	gtk_widget_add_events (action_widget->entry, GDK_BUTTON_MOTION_MASK | GDK_SCROLL_MASK);
218 	gtk_entry_set_width_chars (GTK_ENTRY (action_widget->entry), 5);
219 	gtk_entry_set_text (GTK_ENTRY (action_widget->entry), "");
220 	g_signal_connect_swapped (action_widget->entry, "scroll-event",
221 				  G_CALLBACK (page_scroll_cb),
222 				  action_widget);
223 	g_signal_connect_swapped (action_widget->entry, "activate",
224 				  G_CALLBACK (activate_cb),
225 				  action_widget);
226 	g_signal_connect_swapped (action_widget->entry, "focus-out-event",
227 							  G_CALLBACK (focus_out_cb), action_widget);
228 
229 	obj = gtk_widget_get_accessible (action_widget->entry);
230 	atk_object_set_name (obj, "page-label-entry");
231 
232 	gtk_box_pack_start (GTK_BOX (hbox), action_widget->entry,
233 			    FALSE, FALSE, 0);
234 	gtk_widget_show (action_widget->entry);
235 
236 	action_widget->label = gtk_entry_new ();
237 	gtk_widget_set_sensitive (action_widget->label, FALSE);
238 	gtk_entry_set_width_chars (GTK_ENTRY (action_widget->label), 5);
239 	gtk_box_pack_start (GTK_BOX (hbox), action_widget->label,
240 			    FALSE, FALSE, 0);
241 	gtk_widget_show (action_widget->label);
242 
243 	g_object_set (G_OBJECT (action_widget), "margin-left", 8, "margin-right", 8, NULL);
244 	gtk_container_add (GTK_CONTAINER (action_widget), hbox);
245 	gtk_widget_show (hbox);
246 
247 	gtk_widget_show (GTK_WIDGET (action_widget));
248 }
249 
250 static void
ev_page_action_widget_document_changed_cb(EvDocumentModel * model,GParamSpec * pspec,EvPageActionWidget * action_widget)251 ev_page_action_widget_document_changed_cb (EvDocumentModel    *model,
252 					   GParamSpec         *pspec,
253 					   EvPageActionWidget *action_widget)
254 {
255 	EvDocument *document = ev_document_model_get_document (model);
256 
257 	g_object_ref (document);
258 	if (action_widget->document)
259 		g_object_unref (action_widget->document);
260 	action_widget->document = document;
261 
262 	if (action_widget->signal_id > 0) {
263 		g_signal_handler_disconnect (action_widget->doc_model,
264 					     action_widget->signal_id);
265 		action_widget->signal_id = 0;
266 	}
267 	action_widget->signal_id =
268 		g_signal_connect_object (action_widget->doc_model,
269 					 "page-changed",
270 					 G_CALLBACK (page_changed_cb),
271 					 action_widget, 0);
272 
273 	ev_page_action_widget_set_current_page (action_widget,
274 						ev_document_model_get_page (model));
275 	ev_page_action_widget_update_max_width (action_widget);
276 }
277 
278 void
ev_page_action_widget_set_model(EvPageActionWidget * action_widget,EvDocumentModel * model)279 ev_page_action_widget_set_model (EvPageActionWidget *action_widget,
280 				 EvDocumentModel    *model)
281 {
282 	if (action_widget->doc_model) {
283 		g_object_remove_weak_pointer (G_OBJECT (action_widget->doc_model),
284 					      (gpointer)&action_widget->doc_model);
285 	}
286 	action_widget->doc_model = model;
287 	g_object_add_weak_pointer (G_OBJECT (model),
288 				   (gpointer)&action_widget->doc_model);
289 
290 	g_signal_connect (model, "notify::document",
291 			  G_CALLBACK (ev_page_action_widget_document_changed_cb),
292 			  action_widget);
293 }
294 
295 static void
ev_page_action_widget_finalize(GObject * object)296 ev_page_action_widget_finalize (GObject *object)
297 {
298 	EvPageActionWidget *action_widget = EV_PAGE_ACTION_WIDGET (object);
299 
300 	if (action_widget->doc_model != NULL) {
301 		if (action_widget->signal_id > 0) {
302 			if (g_signal_handler_is_connected(action_widget->doc_model,
303 							  action_widget->signal_id))
304 				g_signal_handler_disconnect (action_widget->doc_model,
305 							     action_widget->signal_id);
306 			action_widget->signal_id = 0;
307 		}
308 		g_object_remove_weak_pointer (G_OBJECT (action_widget->doc_model),
309 					      (gpointer)&action_widget->doc_model);
310 		action_widget->doc_model = NULL;
311 	}
312 
313 	if (action_widget->document) {
314 		g_object_unref (action_widget->document);
315 		action_widget->document = NULL;
316 	}
317 
318 	G_OBJECT_CLASS (ev_page_action_widget_parent_class)->finalize (object);
319 }
320 
321 static void
ev_page_action_widget_class_init(EvPageActionWidgetClass * class)322 ev_page_action_widget_class_init (EvPageActionWidgetClass *class)
323 {
324 	GObjectClass *object_class = G_OBJECT_CLASS (class);
325 
326 	object_class->finalize = ev_page_action_widget_finalize;
327 
328 	widget_signals[WIDGET_ACTIVATE_LINK] =
329 		g_signal_new ("activate_link",
330 			      G_OBJECT_CLASS_TYPE (object_class),
331 			      G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
332 			      G_STRUCT_OFFSET (EvPageActionWidgetClass, activate_link),
333 			      NULL, NULL,
334 			      g_cclosure_marshal_VOID__OBJECT,
335 			      G_TYPE_NONE, 1,
336 			      G_TYPE_OBJECT);
337 
338 }
339 
340 static gboolean
match_selected_cb(GtkEntryCompletion * completion,GtkTreeModel * filter_model,GtkTreeIter * filter_iter,EvPageActionWidget * proxy)341 match_selected_cb (GtkEntryCompletion *completion,
342 		   GtkTreeModel       *filter_model,
343 		   GtkTreeIter        *filter_iter,
344 		   EvPageActionWidget *proxy)
345 {
346 	EvLink *link;
347 	GtkTreeIter *iter;
348 
349 	gtk_tree_model_get (filter_model, filter_iter,
350 			    0, &iter,
351 			    -1);
352 	gtk_tree_model_get (proxy->model, iter,
353 			    EV_DOCUMENT_LINKS_COLUMN_LINK, &link,
354 			    -1);
355 
356 	g_signal_emit (proxy, widget_signals[WIDGET_ACTIVATE_LINK], 0, link);
357 
358 	if (link)
359 		g_object_unref (link);
360 
361 	gtk_tree_iter_free (iter);
362 
363 	return TRUE;
364 }
365 
366 
367 static void
display_completion_text(GtkCellLayout * cell_layout,GtkCellRenderer * renderer,GtkTreeModel * filter_model,GtkTreeIter * filter_iter,EvPageActionWidget * proxy)368 display_completion_text (GtkCellLayout      *cell_layout,
369 			 GtkCellRenderer    *renderer,
370 			 GtkTreeModel       *filter_model,
371 			 GtkTreeIter        *filter_iter,
372 			 EvPageActionWidget *proxy)
373 {
374 	EvLink *link;
375 	GtkTreeIter *iter;
376 
377 	gtk_tree_model_get (filter_model, filter_iter,
378 			    0, &iter,
379 			    -1);
380 	gtk_tree_model_get (proxy->model, iter,
381 			    EV_DOCUMENT_LINKS_COLUMN_LINK, &link,
382 			    -1);
383 
384 	g_object_set (renderer, "text", ev_link_get_title (link), NULL);
385 
386 	if (link)
387 		g_object_unref (link);
388 
389 	gtk_tree_iter_free (iter);
390 }
391 
392 static gboolean
match_completion(GtkEntryCompletion * completion,const gchar * key,GtkTreeIter * filter_iter,EvPageActionWidget * proxy)393 match_completion (GtkEntryCompletion *completion,
394 		  const gchar        *key,
395 		  GtkTreeIter        *filter_iter,
396 		  EvPageActionWidget *proxy)
397 {
398 	EvLink *link;
399 	GtkTreeIter *iter;
400 	const gchar *text = NULL;
401 
402 	gtk_tree_model_get (gtk_entry_completion_get_model (completion),
403 			    filter_iter,
404 			    0, &iter,
405 			    -1);
406 	gtk_tree_model_get (proxy->model, iter,
407 			    EV_DOCUMENT_LINKS_COLUMN_LINK, &link,
408 			    -1);
409 
410 
411 	if (link) {
412 		text = ev_link_get_title (link);
413 		g_object_unref (link);
414 	}
415 
416 	gtk_tree_iter_free (iter);
417 
418 	if (text && key) {
419 		gchar *normalized_text;
420 		gchar *normalized_key;
421 		gchar *case_normalized_text;
422 		gchar *case_normalized_key;
423 		gboolean retval = FALSE;
424 
425 		normalized_text = g_utf8_normalize (text, -1, G_NORMALIZE_ALL);
426 		normalized_key = g_utf8_normalize (key, -1, G_NORMALIZE_ALL);
427 		case_normalized_text = g_utf8_casefold (normalized_text, -1);
428 		case_normalized_key = g_utf8_casefold (normalized_key, -1);
429 
430 		if (strstr (case_normalized_text, case_normalized_key))
431 			retval = TRUE;
432 
433 		g_free (normalized_text);
434 		g_free (normalized_key);
435 		g_free (case_normalized_text);
436 		g_free (case_normalized_key);
437 
438 		return retval;
439 	}
440 
441 	return FALSE;
442 }
443 
444 /* user data to set on the widget. */
445 #define EPA_FILTER_MODEL_DATA "epa-filter-model"
446 
447 static gboolean
build_new_tree_cb(GtkTreeModel * model,GtkTreePath * path,GtkTreeIter * iter,gpointer data)448 build_new_tree_cb (GtkTreeModel *model,
449 		   GtkTreePath  *path,
450 		   GtkTreeIter  *iter,
451 		   gpointer      data)
452 {
453 	GtkTreeModel *filter_model = GTK_TREE_MODEL (data);
454 	EvLink *link;
455 	EvLinkAction *action;
456 	EvLinkActionType type;
457 
458 	gtk_tree_model_get (model, iter,
459 			    EV_DOCUMENT_LINKS_COLUMN_LINK, &link,
460 			    -1);
461 
462 	if (!link)
463 		return FALSE;
464 
465 	action = ev_link_get_action (link);
466 	if (!action) {
467 		g_object_unref (link);
468 		return FALSE;
469 	}
470 
471 	type = ev_link_action_get_action_type (action);
472 
473 	if (type == EV_LINK_ACTION_TYPE_GOTO_DEST) {
474 		GtkTreeIter filter_iter;
475 
476 		gtk_list_store_append (GTK_LIST_STORE (filter_model), &filter_iter);
477 		gtk_list_store_set (GTK_LIST_STORE (filter_model), &filter_iter,
478 				    0, iter,
479 				    -1);
480 	}
481 
482 	g_object_unref (link);
483 
484 	return FALSE;
485 }
486 
487 static GtkTreeModel *
get_filter_model_from_model(GtkTreeModel * model)488 get_filter_model_from_model (GtkTreeModel *model)
489 {
490 	GtkTreeModel *filter_model;
491 
492 	filter_model =
493 		(GtkTreeModel *) g_object_get_data (G_OBJECT (model), EPA_FILTER_MODEL_DATA);
494 	if (filter_model == NULL) {
495 		filter_model = (GtkTreeModel *) gtk_list_store_new (1, GTK_TYPE_TREE_ITER);
496 
497 		gtk_tree_model_foreach (model,
498 					build_new_tree_cb,
499 					filter_model);
500 		g_object_set_data_full (G_OBJECT (model), EPA_FILTER_MODEL_DATA, filter_model, g_object_unref);
501 	}
502 
503 	return filter_model;
504 }
505 
506 
507 void
ev_page_action_widget_update_links_model(EvPageActionWidget * proxy,GtkTreeModel * model)508 ev_page_action_widget_update_links_model (EvPageActionWidget *proxy, GtkTreeModel *model)
509 {
510 	GtkTreeModel *filter_model;
511 	GtkEntryCompletion *completion;
512 	GtkCellRenderer *renderer;
513 
514 	if (!model)
515 		return;
516 
517 	/* Magik */
518 	proxy->model = model;
519 	filter_model = get_filter_model_from_model (model);
520 
521 	completion = gtk_entry_completion_new ();
522 	g_object_set (G_OBJECT (completion),
523 		      "popup-set-width", FALSE,
524 		      "model", filter_model,
525 		      NULL);
526 
527 	g_signal_connect (completion, "match-selected", G_CALLBACK (match_selected_cb), proxy);
528 	gtk_entry_completion_set_match_func (completion,
529 					     (GtkEntryCompletionMatchFunc) match_completion,
530 					     proxy, NULL);
531 
532 	/* Set up the layout */
533 	renderer = (GtkCellRenderer *)
534 		g_object_new (GTK_TYPE_CELL_RENDERER_TEXT,
535 			      "ellipsize", PANGO_ELLIPSIZE_END,
536 			      "width_chars", 30,
537 			      NULL);
538 	gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (completion), renderer, TRUE);
539 	gtk_cell_layout_set_cell_data_func (GTK_CELL_LAYOUT (completion),
540 					    renderer,
541 					    (GtkCellLayoutDataFunc) display_completion_text,
542 					    proxy, NULL);
543 	gtk_entry_set_completion (GTK_ENTRY (proxy->entry), completion);
544 
545 	g_object_unref (completion);
546 }
547 
548 void
ev_page_action_widget_grab_focus(EvPageActionWidget * proxy)549 ev_page_action_widget_grab_focus (EvPageActionWidget *proxy)
550 {
551 	gtk_widget_grab_focus (proxy->entry);
552 }
553 
554