1 /* ev-view-presentation.c
2 * this file is part of evince, a gnome document viewer
4 * Copyright (C) 2010 Carlos Garcia Campos <carlosgc@gnome.org>
6 * Evince is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * Evince is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
24 #include <glib/gi18n-lib.h>
25 #include <gdk/gdkkeysyms.h>
27 #include "ev-view-presentation.h"
29 #include "ev-job-scheduler.h"
30 #include "ev-transition-animation.h"
31 #include "ev-view-cursor.h"
32 #include "ev-page-cache.h"
49 EV_PRESENTATION_NORMAL,
50 EV_PRESENTATION_BLACK,
51 EV_PRESENTATION_WHITE,
53 } EvPresentationState;
55 struct _EvViewPresentation
60 cairo_surface_t *current_surface;
63 gboolean inverted_colors;
64 EvPresentationState state;
71 guint hide_cursor_timeout_id;
74 GtkWidget *goto_window;
75 GtkWidget *goto_entry;
78 guint trans_timeout_id;
81 gboolean enable_animations;
82 EvTransitionAnimation *animation;
85 EvPageCache *page_cache;
92 struct _EvViewPresentationClass
94 GtkWidgetClass base_class;
97 void (* change_page) (EvViewPresentation *pview,
98 GtkScrollType scroll);
99 void (* finished) (EvViewPresentation *pview);
102 static guint signals[N_SIGNALS] = { 0 };
104 static void ev_view_presentation_next_page (EvViewPresentation *pview);
105 static void ev_view_presentation_previous_page (EvViewPresentation *pview);
106 static void ev_view_presentation_set_cursor_for_location (EvViewPresentation *pview,
110 #define HIDE_CURSOR_TIMEOUT 5
112 G_DEFINE_TYPE (EvViewPresentation, ev_view_presentation, GTK_TYPE_WIDGET)
115 ev_view_presentation_set_normal (EvViewPresentation *pview)
117 GtkWidget *widget = GTK_WIDGET (pview);
120 if (pview->state == EV_PRESENTATION_NORMAL)
123 pview->state = EV_PRESENTATION_NORMAL;
124 style = gtk_widget_get_style (widget);
125 gdk_window_set_background (gtk_widget_get_window (widget), &style->black);
126 gtk_widget_queue_draw (widget);
130 ev_view_presentation_set_black (EvViewPresentation *pview)
132 GtkWidget *widget = GTK_WIDGET (pview);
135 if (pview->state == EV_PRESENTATION_BLACK)
138 pview->state = EV_PRESENTATION_BLACK;
139 style = gtk_widget_get_style (widget);
140 gdk_window_set_background (gtk_widget_get_window (widget), &style->black);
141 gtk_widget_queue_draw (widget);
145 ev_view_presentation_set_white (EvViewPresentation *pview)
147 GtkWidget *widget = GTK_WIDGET (pview);
150 if (pview->state == EV_PRESENTATION_WHITE)
153 pview->state = EV_PRESENTATION_WHITE;
154 style = gtk_widget_get_style (widget);
155 gdk_window_set_background (gtk_widget_get_window (widget), &style->white);
156 gtk_widget_queue_draw (widget);
160 ev_view_presentation_set_end (EvViewPresentation *pview)
162 GtkWidget *widget = GTK_WIDGET (pview);
164 if (pview->state == EV_PRESENTATION_END)
167 pview->state = EV_PRESENTATION_END;
168 gtk_widget_queue_draw (widget);
172 ev_view_presentation_get_scale_for_page (EvViewPresentation *pview,
175 if (!ev_document_is_page_size_uniform (pview->document) || pview->scale == 0) {
176 gdouble width, height;
178 ev_document_get_page_size (pview->document, page, &width, &height);
179 if (pview->rotation == 90 || pview->rotation == 270) {
186 pview->scale = MIN (pview->monitor_width / width, pview->monitor_height / height);
193 ev_view_presentation_get_page_area (EvViewPresentation *pview,
196 GtkWidget *widget = GTK_WIDGET (pview);
197 GtkAllocation allocation;
198 gdouble doc_width, doc_height;
199 gint view_width, view_height;
202 ev_document_get_page_size (pview->document,
204 &doc_width, &doc_height);
205 scale = ev_view_presentation_get_scale_for_page (pview, pview->current_page);
207 if (pview->rotation == 90 || pview->rotation == 270) {
208 view_width = (gint)((doc_height * scale) + 0.5);
209 view_height = (gint)((doc_width * scale) + 0.5);
211 view_width = (gint)((doc_width * scale) + 0.5);
212 view_height = (gint)((doc_height * scale) + 0.5);
215 gtk_widget_get_allocation (widget, &allocation);
217 area->x = (MAX (0, allocation.width - view_width)) / 2;
218 area->y = (MAX (0, allocation.height - view_height)) / 2;
219 area->width = view_width;
220 area->height = view_height;
223 /* Page Transition */
225 transition_next_page (EvViewPresentation *pview)
227 ev_view_presentation_next_page (pview);
233 ev_view_presentation_transition_stop (EvViewPresentation *pview)
235 if (pview->trans_timeout_id > 0)
236 g_source_remove (pview->trans_timeout_id);
237 pview->trans_timeout_id = 0;
241 ev_view_presentation_transition_start (EvViewPresentation *pview)
245 if (!EV_IS_DOCUMENT_TRANSITION (pview->document))
248 ev_view_presentation_transition_stop (pview);
250 duration = ev_document_transition_get_page_duration (EV_DOCUMENT_TRANSITION (pview->document),
251 pview->current_page);
253 pview->trans_timeout_id =
254 g_timeout_add_seconds (duration,
255 (GSourceFunc) transition_next_page,
262 ev_view_presentation_animation_cancel (EvViewPresentation *pview)
264 if (pview->animation) {
265 g_object_unref (pview->animation);
266 pview->animation = NULL;
271 ev_view_presentation_transition_animation_finish (EvViewPresentation *pview)
273 ev_view_presentation_animation_cancel (pview);
274 ev_view_presentation_transition_start (pview);
275 gtk_widget_queue_draw (GTK_WIDGET (pview));
279 ev_view_presentation_transition_animation_frame (EvViewPresentation *pview,
282 gtk_widget_queue_draw (GTK_WIDGET (pview));
286 ev_view_presentation_animation_start (EvViewPresentation *pview,
289 EvTransitionEffect *effect = NULL;
290 cairo_surface_t *surface;
293 if (!pview->enable_animations)
296 if (pview->current_page == new_page)
299 effect = ev_document_transition_get_effect (EV_DOCUMENT_TRANSITION (pview->document),
304 pview->animation = ev_transition_animation_new (effect);
306 surface = pview->curr_job ? EV_JOB_RENDER (pview->curr_job)->surface : NULL;
307 ev_transition_animation_set_origin_surface (pview->animation,
309 surface : pview->current_surface);
311 jump = new_page - pview->current_page;
313 surface = pview->prev_job ? EV_JOB_RENDER (pview->prev_job)->surface : NULL;
315 surface = pview->next_job ? EV_JOB_RENDER (pview->next_job)->surface : NULL;
319 ev_transition_animation_set_dest_surface (pview->animation, surface);
321 g_signal_connect_swapped (pview->animation, "frame",
322 G_CALLBACK (ev_view_presentation_transition_animation_frame),
324 g_signal_connect_swapped (pview->animation, "finished",
325 G_CALLBACK (ev_view_presentation_transition_animation_finish),
329 /* Page Navigation */
331 job_finished_cb (EvJob *job,
332 EvViewPresentation *pview)
334 EvJobRender *job_render = EV_JOB_RENDER (job);
336 if (pview->inverted_colors)
337 ev_document_misc_invert_surface (job_render->surface);
339 if (job != pview->curr_job)
342 if (pview->animation) {
343 ev_transition_animation_set_dest_surface (pview->animation,
344 job_render->surface);
346 ev_view_presentation_transition_start (pview);
347 gtk_widget_queue_draw (GTK_WIDGET (pview));
352 ev_view_presentation_schedule_new_job (EvViewPresentation *pview,
354 EvJobPriority priority)
359 if (page < 0 || page >= ev_document_get_n_pages (pview->document))
362 scale = ev_view_presentation_get_scale_for_page (pview, page);
363 job = ev_job_render_new (pview->document, page, pview->rotation, scale, 0, 0);
364 g_signal_connect (job, "finished",
365 G_CALLBACK (job_finished_cb),
367 ev_job_scheduler_push_job (job, priority);
373 ev_view_presentation_delete_job (EvViewPresentation *pview,
379 g_signal_handlers_disconnect_by_func (job, job_finished_cb, pview);
381 g_object_unref (job);
385 ev_view_presentation_update_current_page (EvViewPresentation *pview,
390 if (page < 0 || page >= ev_document_get_n_pages (pview->document))
393 ev_view_presentation_animation_cancel (pview);
394 ev_view_presentation_animation_start (pview, page);
396 jump = page - pview->current_page;
400 if (!pview->curr_job)
401 pview->curr_job = ev_view_presentation_schedule_new_job (pview, page, EV_JOB_PRIORITY_URGENT);
402 if (!pview->next_job)
403 pview->next_job = ev_view_presentation_schedule_new_job (pview, page + 1, EV_JOB_PRIORITY_HIGH);
404 if (!pview->prev_job)
405 pview->prev_job = ev_view_presentation_schedule_new_job (pview, page - 1, EV_JOB_PRIORITY_LOW);
408 ev_view_presentation_delete_job (pview, pview->next_job);
409 pview->next_job = pview->curr_job;
410 pview->curr_job = pview->prev_job;
412 if (!pview->curr_job)
413 pview->curr_job = ev_view_presentation_schedule_new_job (pview, page, EV_JOB_PRIORITY_URGENT);
415 ev_job_scheduler_update_job (pview->curr_job, EV_JOB_PRIORITY_URGENT);
416 pview->prev_job = ev_view_presentation_schedule_new_job (pview, page - 1, EV_JOB_PRIORITY_HIGH);
417 ev_job_scheduler_update_job (pview->next_job, EV_JOB_PRIORITY_LOW);
421 ev_view_presentation_delete_job (pview, pview->prev_job);
422 pview->prev_job = pview->curr_job;
423 pview->curr_job = pview->next_job;
425 if (!pview->curr_job)
426 pview->curr_job = ev_view_presentation_schedule_new_job (pview, page, EV_JOB_PRIORITY_URGENT);
428 ev_job_scheduler_update_job (pview->curr_job, EV_JOB_PRIORITY_URGENT);
429 pview->next_job = ev_view_presentation_schedule_new_job (pview, page + 1, EV_JOB_PRIORITY_HIGH);
430 ev_job_scheduler_update_job (pview->prev_job, EV_JOB_PRIORITY_LOW);
434 ev_view_presentation_delete_job (pview, pview->next_job);
435 ev_view_presentation_delete_job (pview, pview->curr_job);
436 pview->next_job = pview->prev_job;
438 pview->curr_job = ev_view_presentation_schedule_new_job (pview, page, EV_JOB_PRIORITY_URGENT);
439 pview->prev_job = ev_view_presentation_schedule_new_job (pview, page - 1, EV_JOB_PRIORITY_HIGH);
440 if (!pview->next_job)
441 pview->next_job = ev_view_presentation_schedule_new_job (pview, page + 1, EV_JOB_PRIORITY_LOW);
443 ev_job_scheduler_update_job (pview->next_job, EV_JOB_PRIORITY_LOW);
446 ev_view_presentation_delete_job (pview, pview->prev_job);
447 ev_view_presentation_delete_job (pview, pview->curr_job);
448 pview->prev_job = pview->next_job;
450 pview->curr_job = ev_view_presentation_schedule_new_job (pview, page, EV_JOB_PRIORITY_URGENT);
451 pview->next_job = ev_view_presentation_schedule_new_job (pview, page + 1, EV_JOB_PRIORITY_HIGH);
452 if (!pview->prev_job)
453 pview->prev_job = ev_view_presentation_schedule_new_job (pview, page - 1, EV_JOB_PRIORITY_LOW);
455 ev_job_scheduler_update_job (pview->prev_job, EV_JOB_PRIORITY_LOW);
458 ev_view_presentation_delete_job (pview, pview->prev_job);
459 ev_view_presentation_delete_job (pview, pview->curr_job);
460 ev_view_presentation_delete_job (pview, pview->next_job);
462 pview->curr_job = ev_view_presentation_schedule_new_job (pview, page, EV_JOB_PRIORITY_URGENT);
464 pview->next_job = ev_view_presentation_schedule_new_job (pview, page + 1, EV_JOB_PRIORITY_HIGH);
465 pview->prev_job = ev_view_presentation_schedule_new_job (pview, page - 1, EV_JOB_PRIORITY_LOW);
467 pview->prev_job = ev_view_presentation_schedule_new_job (pview, page - 1, EV_JOB_PRIORITY_HIGH);
468 pview->next_job = ev_view_presentation_schedule_new_job (pview, page + 1, EV_JOB_PRIORITY_LOW);
472 pview->current_page = page;
474 if (pview->page_cache)
475 ev_page_cache_set_page_range (pview->page_cache, page, page);
477 if (pview->cursor != EV_VIEW_CURSOR_HIDDEN) {
480 gtk_widget_get_pointer (GTK_WIDGET (pview), &x, &y);
481 ev_view_presentation_set_cursor_for_location (pview, x, y);
484 if (EV_JOB_RENDER (pview->curr_job)->surface)
485 gtk_widget_queue_draw (GTK_WIDGET (pview));
489 ev_view_presentation_next_page (EvViewPresentation *pview)
494 switch (pview->state) {
495 case EV_PRESENTATION_BLACK:
496 case EV_PRESENTATION_WHITE:
497 ev_view_presentation_set_normal (pview);
498 case EV_PRESENTATION_END:
500 case EV_PRESENTATION_NORMAL:
504 n_pages = ev_document_get_n_pages (pview->document);
505 new_page = pview->current_page + 1;
507 if (new_page == n_pages)
508 ev_view_presentation_set_end (pview);
510 ev_view_presentation_update_current_page (pview, new_page);
514 ev_view_presentation_previous_page (EvViewPresentation *pview)
518 switch (pview->state) {
519 case EV_PRESENTATION_BLACK:
520 case EV_PRESENTATION_WHITE:
521 ev_view_presentation_set_normal (pview);
523 case EV_PRESENTATION_END:
524 pview->state = EV_PRESENTATION_NORMAL;
525 new_page = pview->current_page;
527 case EV_PRESENTATION_NORMAL:
528 new_page = pview->current_page - 1;
532 ev_view_presentation_update_current_page (pview, new_page);
536 #define KEY_IS_NUMERIC(keyval) \
537 ((keyval >= GDK_KEY_0 && keyval <= GDK_KEY_9) || (keyval >= GDK_KEY_KP_0 && keyval <= GDK_KEY_KP_9))
539 /* Cut and paste from gtkwindow.c */
541 send_focus_change (GtkWidget *widget,
544 GdkEvent *fevent = gdk_event_new (GDK_FOCUS_CHANGE);
546 fevent->focus_change.type = GDK_FOCUS_CHANGE;
547 fevent->focus_change.window = gtk_widget_get_window (widget);
548 fevent->focus_change.in = in;
549 if (fevent->focus_change.window)
550 g_object_ref (fevent->focus_change.window);
552 gtk_widget_send_focus_change (widget, fevent);
554 gdk_event_free (fevent);
558 ev_view_presentation_goto_window_hide (EvViewPresentation *pview)
560 /* send focus-in event */
561 send_focus_change (pview->goto_entry, FALSE);
562 gtk_widget_hide (pview->goto_window);
563 gtk_entry_set_text (GTK_ENTRY (pview->goto_entry), "");
567 ev_view_presentation_goto_window_delete_event (GtkWidget *widget,
569 EvViewPresentation *pview)
571 ev_view_presentation_goto_window_hide (pview);
577 ev_view_presentation_goto_window_key_press_event (GtkWidget *widget,
579 EvViewPresentation *pview)
581 switch (event->keyval) {
585 case GDK_KEY_ISO_Left_Tab:
586 ev_view_presentation_goto_window_hide (pview);
589 case GDK_KEY_KP_Enter:
590 case GDK_KEY_ISO_Enter:
591 case GDK_KEY_BackSpace:
595 if (!KEY_IS_NUMERIC (event->keyval))
603 ev_view_presentation_goto_window_button_press_event (GtkWidget *widget,
604 GdkEventButton *event,
605 EvViewPresentation *pview)
607 ev_view_presentation_goto_window_hide (pview);
613 ev_view_presentation_goto_entry_activate (GtkEntry *entry,
614 EvViewPresentation *pview)
619 text = gtk_entry_get_text (entry);
620 page = atoi (text) - 1;
622 ev_view_presentation_goto_window_hide (pview);
623 ev_view_presentation_update_current_page (pview, page);
627 ev_view_presentation_goto_window_create (EvViewPresentation *pview)
629 GtkWidget *frame, *hbox, *label;
630 GtkWindow *toplevel, *goto_window;
632 toplevel = GTK_WINDOW (gtk_widget_get_toplevel (GTK_WIDGET (pview)));
633 goto_window = GTK_WINDOW (pview->goto_window);
635 if (pview->goto_window) {
636 if (gtk_window_has_group (toplevel))
637 gtk_window_group_add_window (gtk_window_get_group (toplevel), goto_window);
638 else if (gtk_window_has_group (goto_window))
639 gtk_window_group_remove_window (gtk_window_get_group (goto_window), goto_window);
644 pview->goto_window = gtk_window_new (GTK_WINDOW_POPUP);
645 gtk_window_set_screen (goto_window, gtk_widget_get_screen (GTK_WIDGET (pview)));
647 if (gtk_window_has_group (toplevel))
648 gtk_window_group_add_window (gtk_window_get_group (toplevel), goto_window);
650 gtk_window_set_modal (goto_window, TRUE);
652 g_signal_connect (pview->goto_window, "delete_event",
653 G_CALLBACK (ev_view_presentation_goto_window_delete_event),
655 g_signal_connect (pview->goto_window, "key_press_event",
656 G_CALLBACK (ev_view_presentation_goto_window_key_press_event),
658 g_signal_connect (pview->goto_window, "button_press_event",
659 G_CALLBACK (ev_view_presentation_goto_window_button_press_event),
662 frame = gtk_frame_new (NULL);
663 gtk_frame_set_shadow_type (GTK_FRAME (frame), GTK_SHADOW_ETCHED_IN);
664 gtk_container_add (GTK_CONTAINER (pview->goto_window), frame);
665 gtk_widget_show (frame);
667 hbox = gtk_hbox_new (FALSE, 0);
668 gtk_container_set_border_width (GTK_CONTAINER (hbox), 3);
669 gtk_container_add (GTK_CONTAINER (frame), hbox);
670 gtk_widget_show (hbox);
672 label = gtk_label_new (_("Jump to page:"));
673 gtk_box_pack_start (GTK_BOX (hbox), label, TRUE, TRUE, 3);
674 gtk_widget_show (label);
675 gtk_widget_realize (label);
677 pview->goto_entry = gtk_entry_new ();
678 g_signal_connect (pview->goto_entry, "activate",
679 G_CALLBACK (ev_view_presentation_goto_entry_activate),
681 gtk_box_pack_start (GTK_BOX (hbox), pview->goto_entry, TRUE, TRUE, 0);
682 gtk_widget_show (pview->goto_entry);
683 gtk_widget_realize (pview->goto_entry);
687 ev_view_presentation_goto_entry_grab_focus (EvViewPresentation *pview)
689 GtkWidgetClass *entry_parent_class;
691 entry_parent_class = g_type_class_peek_parent (GTK_ENTRY_GET_CLASS (pview->goto_entry));
692 (entry_parent_class->grab_focus) (pview->goto_entry);
694 send_focus_change (pview->goto_entry, TRUE);
698 ev_view_presentation_goto_window_send_key_event (EvViewPresentation *pview,
701 GdkEventKey *new_event;
704 /* Move goto window off screen */
705 screen = gtk_widget_get_screen (GTK_WIDGET (pview));
706 gtk_window_move (GTK_WINDOW (pview->goto_window),
707 gdk_screen_get_width (screen) + 1,
708 gdk_screen_get_height (screen) + 1);
709 gtk_widget_show (pview->goto_window);
711 new_event = (GdkEventKey *) gdk_event_copy (event);
712 g_object_unref (new_event->window);
713 new_event->window = gtk_widget_get_window (pview->goto_window);
714 if (new_event->window)
715 g_object_ref (new_event->window);
716 gtk_widget_realize (pview->goto_window);
718 gtk_widget_event (pview->goto_window, (GdkEvent *)new_event);
719 gdk_event_free ((GdkEvent *)new_event);
720 gtk_widget_hide (pview->goto_window);
725 ev_view_presentation_link_is_supported (EvViewPresentation *pview,
728 EvLinkAction *action;
730 action = ev_link_get_action (link);
734 switch (ev_link_action_get_action_type (action)) {
735 case EV_LINK_ACTION_TYPE_GOTO_DEST:
736 return ev_link_action_get_dest (action) != NULL;
737 case EV_LINK_ACTION_TYPE_NAMED:
747 ev_view_presentation_get_link_at_location (EvViewPresentation *pview,
751 GdkRectangle page_area;
752 EvMappingList *link_mapping;
754 gdouble width, height;
755 gdouble new_x, new_y;
758 if (!pview->page_cache)
761 ev_document_get_page_size (pview->document, pview->current_page, &width, &height);
762 ev_view_presentation_get_page_area (pview, &page_area);
763 scale = ev_view_presentation_get_scale_for_page (pview, pview->current_page);
764 x = (x - page_area.x) / scale;
765 y = (y - page_area.y) / scale;
766 switch (pview->rotation) {
785 g_assert_not_reached ();
788 link_mapping = ev_page_cache_get_link_mapping (pview->page_cache, pview->current_page);
790 link = link_mapping ? ev_mapping_list_get_data (link_mapping, new_x, new_y) : NULL;
792 return link && ev_view_presentation_link_is_supported (pview, link) ? link : NULL;
796 ev_vew_presentation_goto_link_dest (EvViewPresentation *pview,
799 EvLinkAction *action;
801 action = ev_link_get_action (link);
803 if (ev_link_action_get_action_type (action) == EV_LINK_ACTION_TYPE_NAMED) {
804 const gchar *name = ev_link_action_get_name (action);
806 if (g_ascii_strcasecmp (name, "FirstPage") == 0) {
807 ev_view_presentation_update_current_page (pview, 0);
808 } else if (g_ascii_strcasecmp (name, "PrevPage") == 0) {
809 ev_view_presentation_update_current_page (pview, pview->current_page - 1);
810 } else if (g_ascii_strcasecmp (name, "NextPage") == 0) {
811 ev_view_presentation_update_current_page (pview, pview->current_page + 1);
812 } else if (g_ascii_strcasecmp (name, "LastPage") == 0) {
815 n_pages = ev_document_get_n_pages (pview->document);
816 ev_view_presentation_update_current_page (pview, n_pages - 1);
822 dest = ev_link_action_get_dest (action);
823 page = ev_document_links_get_dest_page (EV_DOCUMENT_LINKS (pview->document), dest);
824 ev_view_presentation_update_current_page (pview, page);
830 ev_view_presentation_set_cursor (EvViewPresentation *pview,
831 EvViewCursor view_cursor)
836 if (pview->cursor == view_cursor)
839 widget = GTK_WIDGET (pview);
840 if (!gtk_widget_get_realized (widget))
841 gtk_widget_realize (widget);
843 pview->cursor = view_cursor;
845 cursor = ev_view_cursor_new (gtk_widget_get_display (widget), view_cursor);
846 gdk_window_set_cursor (gtk_widget_get_window (widget), cursor);
849 gdk_cursor_unref (cursor);
853 ev_view_presentation_set_cursor_for_location (EvViewPresentation *pview,
857 if (ev_view_presentation_get_link_at_location (pview, x, y))
858 ev_view_presentation_set_cursor (pview, EV_VIEW_CURSOR_LINK);
860 ev_view_presentation_set_cursor (pview, EV_VIEW_CURSOR_NORMAL);
864 hide_cursor_timeout_cb (EvViewPresentation *pview)
866 ev_view_presentation_set_cursor (pview, EV_VIEW_CURSOR_HIDDEN);
867 pview->hide_cursor_timeout_id = 0;
873 ev_view_presentation_hide_cursor_timeout_stop (EvViewPresentation *pview)
875 if (pview->hide_cursor_timeout_id > 0)
876 g_source_remove (pview->hide_cursor_timeout_id);
877 pview->hide_cursor_timeout_id = 0;
881 ev_view_presentation_hide_cursor_timeout_start (EvViewPresentation *pview)
883 ev_view_presentation_hide_cursor_timeout_stop (pview);
884 pview->hide_cursor_timeout_id =
885 g_timeout_add_seconds (HIDE_CURSOR_TIMEOUT,
886 (GSourceFunc)hide_cursor_timeout_cb,
891 ev_view_presentation_update_current_surface (EvViewPresentation *pview,
892 cairo_surface_t *surface)
894 if (!surface || pview->current_surface == surface)
897 cairo_surface_reference (surface);
898 if (pview->current_surface)
899 cairo_surface_destroy (pview->current_surface);
900 pview->current_surface = surface;
904 ev_view_presentation_dispose (GObject *object)
906 EvViewPresentation *pview = EV_VIEW_PRESENTATION (object);
908 if (pview->document) {
909 g_object_unref (pview->document);
910 pview->document = NULL;
913 ev_view_presentation_animation_cancel (pview);
914 ev_view_presentation_transition_stop (pview);
915 ev_view_presentation_hide_cursor_timeout_stop (pview);
917 if (pview->curr_job) {
918 ev_view_presentation_delete_job (pview, pview->curr_job);
919 pview->curr_job = NULL;
922 if (pview->prev_job) {
923 ev_view_presentation_delete_job (pview, pview->prev_job);
924 pview->prev_job = NULL;
927 if (pview->next_job) {
928 ev_view_presentation_delete_job (pview, pview->next_job);
929 pview->next_job = NULL;
932 if (pview->current_surface) {
933 cairo_surface_destroy (pview->current_surface);
934 pview->current_surface = NULL;
937 if (pview->page_cache) {
938 g_object_unref (pview->page_cache);
939 pview->page_cache = NULL;
942 if (pview->goto_window) {
943 gtk_widget_destroy (pview->goto_window);
944 pview->goto_window = NULL;
945 pview->goto_entry = NULL;
948 G_OBJECT_CLASS (ev_view_presentation_parent_class)->dispose (object);
952 ev_view_presentation_get_preferred_width (GtkWidget *widget,
956 *minimum = *natural = 0;
960 ev_view_presentation_get_preferred_height (GtkWidget *widget,
964 *minimum = *natural = 0;
968 ev_view_presentation_draw_end_page (EvViewPresentation *pview,
971 GtkWidget *widget = GTK_WIDGET (pview);
973 PangoFontDescription *font_desc;
975 GtkAllocation allocation;
976 GdkRectangle area = {0};
977 const gchar *text = _("End of presentation. Click to exit.");
979 if (pview->state != EV_PRESENTATION_END)
982 layout = gtk_widget_create_pango_layout (widget, NULL);
983 markup = g_strdup_printf ("<span foreground=\"white\">%s</span>", text);
984 pango_layout_set_markup (layout, markup, -1);
987 font_desc = pango_font_description_new ();
988 pango_font_description_set_size (font_desc, 16 * PANGO_SCALE);
989 pango_layout_set_font_description (layout, font_desc);
991 gtk_widget_get_allocation (widget, &allocation);
992 area.width = allocation.width;
993 area.height = allocation.height;
995 gtk_paint_layout (gtk_widget_get_style (widget),
997 gtk_widget_get_state (widget),
1005 pango_font_description_free (font_desc);
1006 g_object_unref (layout);
1010 ev_view_presentation_draw (GtkWidget *widget,
1013 EvViewPresentation *pview = EV_VIEW_PRESENTATION (widget);
1014 GdkRectangle page_area;
1015 GdkRectangle overlap;
1016 cairo_surface_t *surface;
1017 GdkRectangle clip_rect;
1019 if (!gdk_cairo_get_clip_rectangle (cr, &clip_rect))
1022 switch (pview->state) {
1023 case EV_PRESENTATION_END:
1024 ev_view_presentation_draw_end_page (pview, cr);
1026 case EV_PRESENTATION_BLACK:
1027 case EV_PRESENTATION_WHITE:
1029 case EV_PRESENTATION_NORMAL:
1033 if (pview->animation) {
1034 if (ev_transition_animation_ready (pview->animation)) {
1035 ev_view_presentation_get_page_area (pview, &page_area);
1039 /* normalize to x=0, y=0 */
1040 cairo_translate (cr, page_area.x, page_area.y);
1041 page_area.x = page_area.y = 0;
1043 /* Try to fix rounding errors */
1046 ev_transition_animation_paint (pview->animation, cr, page_area);
1054 surface = pview->curr_job ? EV_JOB_RENDER (pview->curr_job)->surface : NULL;
1056 ev_view_presentation_update_current_surface (pview, surface);
1057 } else if (pview->current_surface) {
1058 surface = pview->current_surface;
1063 ev_view_presentation_get_page_area (pview, &page_area);
1064 if (gdk_rectangle_intersect (&page_area, &clip_rect, &overlap)) {
1067 /* Try to fix rounding errors. See bug #438760 */
1068 if (overlap.width == page_area.width)
1071 cairo_rectangle (cr, overlap.x, overlap.y, overlap.width, overlap.height);
1072 cairo_set_source_surface (cr, surface, page_area.x, page_area.y);
1082 ev_view_presentation_key_press_event (GtkWidget *widget,
1085 EvViewPresentation *pview = EV_VIEW_PRESENTATION (widget);
1087 if (pview->state == EV_PRESENTATION_END)
1088 return gtk_bindings_activate_event (G_OBJECT (widget), event);
1090 switch (event->keyval) {
1093 case GDK_KEY_period:
1094 case GDK_KEY_KP_Decimal:
1095 if (pview->state == EV_PRESENTATION_BLACK)
1096 ev_view_presentation_set_normal (pview);
1098 ev_view_presentation_set_black (pview);
1103 if (pview->state == EV_PRESENTATION_WHITE)
1104 ev_view_presentation_set_normal (pview);
1106 ev_view_presentation_set_white (pview);
1110 if (pview->state == EV_PRESENTATION_NORMAL) {
1111 ev_view_presentation_update_current_page (pview, 0);
1116 if (pview->state == EV_PRESENTATION_NORMAL) {
1119 page = ev_document_get_n_pages (pview->document) - 1;
1120 ev_view_presentation_update_current_page (pview, page);
1129 ev_view_presentation_set_normal (pview);
1131 if (ev_document_get_n_pages (pview->document) > 1 && KEY_IS_NUMERIC (event->keyval)) {
1134 ev_view_presentation_goto_window_create (pview);
1135 ev_view_presentation_goto_window_send_key_event (pview, (GdkEvent *)event);
1136 gtk_widget_get_pointer (GTK_WIDGET (pview), &x, &y);
1137 gtk_window_move (GTK_WINDOW (pview->goto_window), x, y);
1138 gtk_widget_show (pview->goto_window);
1139 ev_view_presentation_goto_entry_grab_focus (pview);
1144 return gtk_bindings_activate_event (G_OBJECT (widget), event);
1148 ev_view_presentation_button_release_event (GtkWidget *widget,
1149 GdkEventButton *event)
1151 EvViewPresentation *pview = EV_VIEW_PRESENTATION (widget);
1153 switch (event->button) {
1157 if (pview->state == EV_PRESENTATION_END) {
1158 g_signal_emit (pview, signals[FINISHED], 0, NULL);
1163 link = ev_view_presentation_get_link_at_location (pview,
1167 ev_vew_presentation_goto_link_dest (pview, link);
1169 ev_view_presentation_next_page (pview);
1173 ev_view_presentation_previous_page (pview);
1183 ev_view_presentation_focus_out (GtkWidget *widget,
1184 GdkEventFocus *event)
1186 EvViewPresentation *pview = EV_VIEW_PRESENTATION (widget);
1188 if (pview->goto_window)
1189 ev_view_presentation_goto_window_hide (pview);
1195 ev_view_presentation_motion_notify_event (GtkWidget *widget,
1196 GdkEventMotion *event)
1198 EvViewPresentation *pview = EV_VIEW_PRESENTATION (widget);
1200 ev_view_presentation_hide_cursor_timeout_start (pview);
1201 ev_view_presentation_set_cursor_for_location (pview, event->x, event->y);
1207 init_presentation (GtkWidget *widget)
1209 EvViewPresentation *pview = EV_VIEW_PRESENTATION (widget);
1210 GdkScreen *screen = gtk_widget_get_screen (widget);
1211 GdkRectangle monitor;
1214 monitor_num = gdk_screen_get_monitor_at_window (screen, gtk_widget_get_window (widget));
1215 gdk_screen_get_monitor_geometry (screen, monitor_num, &monitor);
1216 pview->monitor_width = monitor.width;
1217 pview->monitor_height = monitor.height;
1219 ev_view_presentation_update_current_page (pview, pview->current_page);
1220 ev_view_presentation_hide_cursor_timeout_start (pview);
1226 ev_view_presentation_realize (GtkWidget *widget)
1230 GdkWindowAttr attributes;
1231 GtkAllocation allocation;
1233 gtk_widget_set_realized (widget, TRUE);
1235 attributes.window_type = GDK_WINDOW_CHILD;
1236 attributes.wclass = GDK_INPUT_OUTPUT;
1237 attributes.visual = gtk_widget_get_visual (widget);
1239 gtk_widget_get_allocation (widget, &allocation);
1240 attributes.x = allocation.x;
1241 attributes.y = allocation.y;
1242 attributes.width = allocation.width;
1243 attributes.height = allocation.height;
1244 attributes.event_mask = GDK_EXPOSURE_MASK |
1245 GDK_BUTTON_PRESS_MASK |
1246 GDK_BUTTON_RELEASE_MASK |
1248 GDK_KEY_PRESS_MASK |
1249 GDK_POINTER_MOTION_MASK |
1250 GDK_POINTER_MOTION_HINT_MASK |
1251 GDK_ENTER_NOTIFY_MASK |
1252 GDK_LEAVE_NOTIFY_MASK;
1254 window = gdk_window_new (gtk_widget_get_parent_window (widget),
1256 GDK_WA_X | GDK_WA_Y |
1259 gdk_window_set_user_data (window, widget);
1260 gtk_widget_set_window (widget, window);
1262 gtk_widget_style_attach (widget);
1263 style = gtk_widget_get_style (widget);
1264 gdk_window_set_background (window, &style->black);
1266 g_idle_add ((GSourceFunc)init_presentation, widget);
1270 ev_view_presentation_change_page (EvViewPresentation *pview,
1271 GtkScrollType scroll)
1274 case GTK_SCROLL_PAGE_FORWARD:
1275 ev_view_presentation_next_page (pview);
1277 case GTK_SCROLL_PAGE_BACKWARD:
1278 ev_view_presentation_previous_page (pview);
1281 g_assert_not_reached ();
1286 ev_view_presentation_scroll_event (GtkWidget *widget,
1287 GdkEventScroll *event)
1289 EvViewPresentation *pview = EV_VIEW_PRESENTATION (widget);
1292 state = event->state & gtk_accelerator_get_default_mod_mask ();
1296 switch (event->direction) {
1297 case GDK_SCROLL_DOWN:
1298 case GDK_SCROLL_RIGHT:
1299 ev_view_presentation_change_page (pview, GTK_SCROLL_PAGE_FORWARD);
1302 case GDK_SCROLL_LEFT:
1303 ev_view_presentation_change_page (pview, GTK_SCROLL_PAGE_BACKWARD);
1312 add_change_page_binding_keypad (GtkBindingSet *binding_set,
1314 GdkModifierType modifiers,
1315 GtkScrollType scroll)
1317 guint keypad_keyval = keyval - GDK_KEY_Left + GDK_KEY_KP_Left;
1319 gtk_binding_entry_add_signal (binding_set, keyval, modifiers,
1321 GTK_TYPE_SCROLL_TYPE, scroll);
1322 gtk_binding_entry_add_signal (binding_set, keypad_keyval, modifiers,
1324 GTK_TYPE_SCROLL_TYPE, scroll);
1328 ev_view_presentation_set_property (GObject *object,
1330 const GValue *value,
1333 EvViewPresentation *pview = EV_VIEW_PRESENTATION (object);
1337 pview->document = g_value_dup_object (value);
1338 pview->enable_animations = EV_IS_DOCUMENT_TRANSITION (pview->document);
1340 case PROP_CURRENT_PAGE:
1341 pview->current_page = g_value_get_uint (value);
1344 pview->rotation = g_value_get_uint (value);
1346 case PROP_INVERTED_COLORS:
1347 pview->inverted_colors = g_value_get_boolean (value);
1350 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1355 ev_view_presentation_constructor (GType type,
1356 guint n_construct_properties,
1357 GObjectConstructParam *construct_params)
1360 EvViewPresentation *pview;
1362 object = G_OBJECT_CLASS (ev_view_presentation_parent_class)->constructor (type,
1363 n_construct_properties,
1365 pview = EV_VIEW_PRESENTATION (object);
1367 if (EV_IS_DOCUMENT_LINKS (pview->document)) {
1368 pview->page_cache = ev_page_cache_new (pview->document);
1369 ev_page_cache_set_flags (pview->page_cache, EV_PAGE_DATA_INCLUDE_LINKS);
1376 ev_view_presentation_class_init (EvViewPresentationClass *klass)
1378 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
1379 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
1380 GtkBindingSet *binding_set;
1382 klass->change_page = ev_view_presentation_change_page;
1384 gobject_class->dispose = ev_view_presentation_dispose;
1386 widget_class->get_preferred_width = ev_view_presentation_get_preferred_width;
1387 widget_class->get_preferred_height = ev_view_presentation_get_preferred_height;
1388 widget_class->realize = ev_view_presentation_realize;
1389 widget_class->draw = ev_view_presentation_draw;
1390 widget_class->key_press_event = ev_view_presentation_key_press_event;
1391 widget_class->button_release_event = ev_view_presentation_button_release_event;
1392 widget_class->focus_out_event = ev_view_presentation_focus_out;
1393 widget_class->motion_notify_event = ev_view_presentation_motion_notify_event;
1394 widget_class->scroll_event = ev_view_presentation_scroll_event;
1396 gobject_class->constructor = ev_view_presentation_constructor;
1397 gobject_class->set_property = ev_view_presentation_set_property;
1399 g_object_class_install_property (gobject_class,
1401 g_param_spec_object ("document",
1406 G_PARAM_CONSTRUCT_ONLY));
1407 g_object_class_install_property (gobject_class,
1409 g_param_spec_uint ("current_page",
1414 G_PARAM_CONSTRUCT_ONLY));
1415 g_object_class_install_property (gobject_class,
1417 g_param_spec_uint ("rotation",
1419 "Current rotation angle",
1422 G_PARAM_CONSTRUCT_ONLY));
1423 g_object_class_install_property (gobject_class,
1424 PROP_INVERTED_COLORS,
1425 g_param_spec_boolean ("inverted_colors",
1427 "Whether presentation is displayed with inverted colors",
1430 G_PARAM_CONSTRUCT_ONLY));
1432 signals[CHANGE_PAGE] =
1433 g_signal_new ("change_page",
1434 G_OBJECT_CLASS_TYPE (gobject_class),
1435 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
1436 G_STRUCT_OFFSET (EvViewPresentationClass, change_page),
1438 g_cclosure_marshal_VOID__ENUM,
1440 GTK_TYPE_SCROLL_TYPE);
1442 g_signal_new ("finished",
1443 G_OBJECT_CLASS_TYPE (gobject_class),
1444 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
1445 G_STRUCT_OFFSET (EvViewPresentationClass, finished),
1447 g_cclosure_marshal_VOID__VOID,
1451 binding_set = gtk_binding_set_by_class (klass);
1452 add_change_page_binding_keypad (binding_set, GDK_KEY_Left, 0, GTK_SCROLL_PAGE_BACKWARD);
1453 add_change_page_binding_keypad (binding_set, GDK_KEY_Right, 0, GTK_SCROLL_PAGE_FORWARD);
1454 add_change_page_binding_keypad (binding_set, GDK_KEY_Up, 0, GTK_SCROLL_PAGE_BACKWARD);
1455 add_change_page_binding_keypad (binding_set, GDK_KEY_Down, 0, GTK_SCROLL_PAGE_FORWARD);
1456 gtk_binding_entry_add_signal (binding_set, GDK_KEY_space, 0,
1458 GTK_TYPE_SCROLL_TYPE, GTK_SCROLL_PAGE_FORWARD);
1459 gtk_binding_entry_add_signal (binding_set, GDK_KEY_BackSpace, 0,
1461 GTK_TYPE_SCROLL_TYPE, GTK_SCROLL_PAGE_BACKWARD);
1462 gtk_binding_entry_add_signal (binding_set, GDK_KEY_Page_Down, 0,
1464 GTK_TYPE_SCROLL_TYPE, GTK_SCROLL_PAGE_FORWARD);
1465 gtk_binding_entry_add_signal (binding_set, GDK_KEY_Page_Up, 0,
1467 GTK_TYPE_SCROLL_TYPE, GTK_SCROLL_PAGE_BACKWARD);
1468 gtk_binding_entry_add_signal (binding_set, GDK_KEY_J, 0,
1470 GTK_TYPE_SCROLL_TYPE, GTK_SCROLL_PAGE_FORWARD);
1471 gtk_binding_entry_add_signal (binding_set, GDK_KEY_H, 0,
1473 GTK_TYPE_SCROLL_TYPE, GTK_SCROLL_PAGE_BACKWARD);
1474 gtk_binding_entry_add_signal (binding_set, GDK_KEY_L, 0,
1476 GTK_TYPE_SCROLL_TYPE, GTK_SCROLL_PAGE_FORWARD);
1477 gtk_binding_entry_add_signal (binding_set, GDK_KEY_K, 0,
1479 GTK_TYPE_SCROLL_TYPE, GTK_SCROLL_PAGE_BACKWARD);
1483 ev_view_presentation_init (EvViewPresentation *pview)
1485 gtk_widget_set_can_focus (GTK_WIDGET (pview), TRUE);
1489 ev_view_presentation_new (EvDocument *document,
1492 gboolean inverted_colors)
1494 g_return_val_if_fail (EV_IS_DOCUMENT (document), NULL);
1495 g_return_val_if_fail (current_page < ev_document_get_n_pages (document), NULL);
1497 return GTK_WIDGET (g_object_new (EV_TYPE_VIEW_PRESENTATION,
1498 "document", document,
1499 "current_page", current_page,
1500 "rotation", rotation,
1501 "inverted_colors", inverted_colors,
1506 ev_view_presentation_get_current_page (EvViewPresentation *pview)
1508 return pview->current_page;