diff options
author | Perberos <[email protected]> | 2011-11-09 18:17:43 -0300 |
---|---|---|
committer | Perberos <[email protected]> | 2011-11-09 18:17:43 -0300 |
commit | f6ce926719943751cf65cacde7fae050593eb2d6 (patch) | |
tree | 9224d1751678cf2d1fbd0431f128b711311c0287 /libview | |
download | atril-f6ce926719943751cf65cacde7fae050593eb2d6.tar.bz2 atril-f6ce926719943751cf65cacde7fae050593eb2d6.tar.xz |
inicial
Diffstat (limited to 'libview')
35 files changed, 19036 insertions, 0 deletions
diff --git a/libview/Makefile.am b/libview/Makefile.am new file mode 100644 index 00000000..fc1e6205 --- /dev/null +++ b/libview/Makefile.am @@ -0,0 +1,163 @@ +lib_LTLIBRARIES = libevview.la + +NOINST_H_FILES = \ + ev-annotation-window.h \ + ev-loading-window.h \ + ev-page-cache.h \ + ev-pixbuf-cache.h \ + ev-timeline.h \ + ev-transition-animation.h \ + ev-view-accessible.h \ + ev-view-cursor.h \ + ev-view-marshal.h \ + ev-view-private.h + +INST_H_SRC_FILES = \ + ev-document-model.h \ + ev-jobs.h \ + ev-job-scheduler.h \ + ev-print-operation.h \ + ev-stock-icons.h \ + ev-view.h \ + ev-view-presentation.h + +INST_H_FILES = \ + $(INST_H_SRC_FILES) \ + ev-view-type-builtins.h + +headerdir = $(includedir)/evince/$(EV_API_VERSION)/libview +header_DATA = $(INST_H_FILES) + +libevview_la_SOURCES = \ + ev-annotation-window.c \ + ev-document-model.c \ + ev-loading-window.c \ + ev-jobs.c \ + ev-job-scheduler.c \ + ev-page-cache.c \ + ev-pixbuf-cache.c \ + ev-print-operation.c \ + ev-stock-icons.c \ + ev-timeline.c \ + ev-transition-animation.c \ + ev-view.c \ + ev-view-accessible.c \ + ev-view-marshal.c \ + ev-view-cursor.c \ + ev-view-presentation.c \ + ev-view-type-builtins.c \ + $(NOINST_H_FILES) \ + $(INST_H_FILES) + +libevview_la_CPPFLAGS = \ + -DEVINCEDATADIR=\"$(pkgdatadir)\" \ + -DG_LOG_DOMAIN=\"EvinceView\" \ + -DMATELOCALEDIR=\"$(datadir)/locale\" \ + -DEVINCE_COMPILATION \ + -I$(top_srcdir) \ + -I$(top_srcdir)/libdocument \ + -I$(top_builddir) \ + $(AM_CPPFLAGS) + +libevview_la_CFLAGS = \ + $(LIBVIEW_CFLAGS) \ + $(WARN_CFLAGS) \ + $(DISABLE_DEPRECATED) \ + $(AM_CFLAGS) + +libevview_la_LDFLAGS = \ + -version-info $(EV_VIEW_LT_VERSION_INFO) \ + -no-undefined \ + -export-symbols-regex "^ev_*" \ + $(AM_LDFLAGS) + +libevview_la_LIBADD = \ + $(top_builddir)/libdocument/libevdocument.la \ + $(LIBVIEW_LIBS) + +BUILT_SOURCES = \ + ev-view-marshal.h \ + ev-view-marshal.c \ + ev-view-type-builtins.c \ + ev-view-type-builtins.h + +CLEANFILES = \ + $(BUILT_SOURCES) \ + stamp-ev-view-type-builtins.h \ + stamp-ev-view-type-builtins.c + +ev-view-marshal.h: $(srcdir)/ev-view-marshal.list + $(AM_V_GEN)$(GLIB_GENMARSHAL) --prefix=ev_view_marshal $(srcdir)/ev-view-marshal.list --header --internal > $@ + +ev-view-marshal.c: $(srcdir)/ev-view-marshal.list + echo '#include <config.h>' > ev-view-marshal.c + $(AM_V_GEN)$(GLIB_GENMARSHAL) --prefix=ev_view_marshal $(srcdir)/ev-view-marshal.list --header --internal --body >> $@ + +ev-view-type-builtins.h: stamp-ev-view-type-builtins.h + @true +stamp-ev-view-type-builtins.h: ev-view-type-builtins.h.template $(INST_H_SRC_FILES) + $(AM_V_GEN)$(GLIB_MKENUMS) --template $< $(filter-out $<,$^) > xgen-etbh \ + && (cmp -s xgen-etbh ev-view-type-builtins.h || cp xgen-etbh ev-view-type-builtins.h ) \ + && rm -f xgen-etbh \ + && echo timestamp > $(@F) + +ev-view-type-builtins.c: stamp-ev-view-type-builtins.c + @true +stamp-ev-view-type-builtins.c: ev-view-type-builtins.c.template $(INST_H_SRC_FILES) + $(AM_V_GEN)$(GLIB_MKENUMS) --template $< $(filter-out $<,$^) > xgen-etbc \ + && (cmp -s xgen-etbc ev-view-type-builtins.c || cp xgen-etbc ev-view-type-builtins.c ) \ + && rm -f xgen-etbc \ + && echo timestamp > $(@F) + +EXTRA_DIST = \ + ev-view-type-builtins.c.template \ + ev-view-type-builtins.h.template \ + ev-view-marshal.list + +# GObject Introspection + +if ENABLE_INTROSPECTION + +EvinceView-$(EV_API_VERSION).gir: libevview.la Makefile $(INST_H_FILES) $(filter %.c,$(libevview_la_SOURCES)) + $(AM_V_GEN) PKG_CONFIG_PATH=$(top_builddir):$$PKG_CONFIG_PATH \ + $(G_IR_SCANNER) -v --namespace EvinceView \ + --add-include-path=$(top_builddir)/libdocument \ + --strip-prefix=Ev \ + --nsversion=$(EV_API_VERSION) \ + --include=GLib-2.0 \ + --include=GObject-2.0 \ + --include=Gio-2.0 \ + --include=Gdk-2.0 \ + --include=GdkPixbuf-2.0 \ + --include=Gtk-2.0 \ + --include=EvinceDocument-$(EV_API_VERSION) \ + --library=evview \ + --libtool="$(LIBTOOL)" \ + --output $@ \ + --pkg evince-document-$(EV_API_VERSION) \ + --pkg evince-view-$(EV_API_VERSION) \ + -I$(top_srcdir) \ + -I$(top_builddir) \ + -DEVINCE_COMPILATION \ + $(filter %.h,$^) \ + $(filter %.c,$^) + +girdir = $(GIRDIR) +gir_DATA = EvinceView-$(EV_API_VERSION).gir + +typelibsdir = $(GIRTYPELIBDIR) +typelibs_DATA = EvinceView-$(EV_API_VERSION).typelib + +EXTRA_DIST += $(gir_DATA) +CLEANFILES += $(gir_DATA) $(typelibs_DATA) + +%.typelib: %.gir + $(AM_V_GEN) LD_LIBRARY_PATH=$${LD_LIBRARY_PATH:+$$LD_LIBRARY_PATH:}. $(G_IR_COMPILER) \ + --includedir=$(srcdir) \ + --includedir=$(top_builddir)/libdocument \ + $(G_IR_COMPILER_OPTS) \ + $< -o $@ + +endif # ENABLE_INTROSPECTION + +-include $(top_srcdir)/git.mk diff --git a/libview/ev-annotation-window.c b/libview/ev-annotation-window.c new file mode 100644 index 00000000..6ecfafc6 --- /dev/null +++ b/libview/ev-annotation-window.c @@ -0,0 +1,654 @@ +/* ev-annotation-window.c + * this file is part of evince, a mate document viewer + * + * Copyright (C) 2009 Carlos Garcia Campos <[email protected]> + * Copyright (C) 2007 IƱigo Martinez <[email protected]> + * + * Evince is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * Evince is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ + +#include "config.h" + +#include <string.h> + +#include "ev-annotation-window.h" +#include "ev-stock-icons.h" +#include "ev-view-marshal.h" +#include "ev-document-misc.h" + +enum { + PROP_0, + PROP_ANNOTATION, + PROP_PARENT +}; + +enum { + CLOSED, + MOVED, + N_SIGNALS +}; + +struct _EvAnnotationWindow { + GtkWindow base_instance; + + EvAnnotation *annotation; + GtkWindow *parent; + + GtkWidget *title; + GtkWidget *close_button; + GtkWidget *text_view; + GtkWidget *resize_se; + GtkWidget *resize_sw; + + gboolean is_open; + EvRectangle rect; + + gboolean in_move; + gint x; + gint y; + gint orig_x; + gint orig_y; +}; + +struct _EvAnnotationWindowClass { + GtkWindowClass base_class; + + void (* closed) (EvAnnotationWindow *window); + void (* moved) (EvAnnotationWindow *window, + gint x, + gint y); +}; + +static guint signals[N_SIGNALS]; + +G_DEFINE_TYPE (EvAnnotationWindow, ev_annotation_window, GTK_TYPE_WINDOW) + +#define EV_ICON_SIZE_ANNOT_WINDOW (ev_annotation_window_get_icon_size()) + +/* Cut and paste from gtkwindow.c */ +static void +send_focus_change (GtkWidget *widget, + gboolean in) +{ + GdkEvent *fevent = gdk_event_new (GDK_FOCUS_CHANGE); + + fevent->focus_change.type = GDK_FOCUS_CHANGE; + fevent->focus_change.window = gtk_widget_get_window (widget); + fevent->focus_change.in = in; + if (fevent->focus_change.window) + g_object_ref (fevent->focus_change.window); + + gtk_widget_send_focus_change (widget, fevent); + + gdk_event_free (fevent); +} + +static gdouble +get_screen_dpi (EvAnnotationWindow *window) +{ + GdkScreen *screen; + + screen = gtk_window_get_screen (GTK_WINDOW (window)); + return ev_document_misc_get_screen_dpi (screen); +} + +static GtkIconSize +ev_annotation_window_get_icon_size (void) +{ + static GtkIconSize icon_size = 0; + + if (G_UNLIKELY (icon_size == 0)) + icon_size = gtk_icon_size_register ("ev-icon-size-annot-window", 8, 8); + + return icon_size; +} + +static void +ev_annotation_window_sync_contents (EvAnnotationWindow *window) +{ + gchar *contents; + GtkTextIter start, end; + GtkTextBuffer *buffer; + EvAnnotation *annot = window->annotation; + + buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (window->text_view)); + gtk_text_buffer_get_bounds (buffer, &start, &end); + contents = gtk_text_buffer_get_text (buffer, &start, &end, FALSE); + ev_annotation_set_contents (annot, contents); + g_free (contents); +} + +static void +ev_annotation_window_set_color (EvAnnotationWindow *window, + GdkColor *color) +{ + GtkRcStyle *rc_style; + GdkColor gcolor; + + gcolor = *color; + + /* Allocate these colors */ + gdk_colormap_alloc_color (gtk_widget_get_colormap (GTK_WIDGET (window)), + &gcolor, FALSE, TRUE); + + /* Apply colors to style */ + rc_style = gtk_widget_get_modifier_style (GTK_WIDGET (window)); + rc_style->base[GTK_STATE_NORMAL] = gcolor; + rc_style->bg[GTK_STATE_PRELIGHT] = gcolor; + rc_style->bg[GTK_STATE_NORMAL] = gcolor; + rc_style->bg[GTK_STATE_ACTIVE] = gcolor; + rc_style->color_flags[GTK_STATE_PRELIGHT] = GTK_RC_BG; + rc_style->color_flags[GTK_STATE_NORMAL] = GTK_RC_BG | GTK_RC_BASE; + rc_style->color_flags[GTK_STATE_ACTIVE] = GTK_RC_BG; + + /* Apply the style to the widgets */ + g_object_ref (rc_style); + gtk_widget_modify_style (GTK_WIDGET (window), rc_style); + gtk_widget_modify_style (window->close_button, rc_style); + gtk_widget_modify_style (window->resize_se, rc_style); + gtk_widget_modify_style (window->resize_sw, rc_style); + g_object_unref (rc_style); +} + +static void +ev_annotation_window_label_changed (EvAnnotationMarkup *annot, + GParamSpec *pspec, + EvAnnotationWindow *window) +{ + const gchar *label = ev_annotation_markup_get_label (annot); + + gtk_window_set_title (GTK_WINDOW (window), label); + gtk_label_set_text (GTK_LABEL (window->title), label); +} + +static void +ev_annotation_window_color_changed (EvAnnotation *annot, + GParamSpec *pspec, + EvAnnotationWindow *window) +{ + GdkColor color; + + ev_annotation_get_color (annot, &color); + ev_annotation_window_set_color (window, &color); +} + +static void +ev_annotation_window_dispose (GObject *object) +{ + EvAnnotationWindow *window = EV_ANNOTATION_WINDOW (object); + + if (window->annotation) { + ev_annotation_window_sync_contents (window); + g_object_unref (window->annotation); + window->annotation = NULL; + } + + (* G_OBJECT_CLASS (ev_annotation_window_parent_class)->dispose) (object); +} + +static void +ev_annotation_window_set_property (GObject *object, + guint prop_id, + const GValue *value, + GParamSpec *pspec) +{ + EvAnnotationWindow *window = EV_ANNOTATION_WINDOW (object); + + switch (prop_id) { + case PROP_ANNOTATION: + window->annotation = g_value_dup_object (value); + break; + case PROP_PARENT: + window->parent = g_value_get_object (value); + break; + default: + G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); + } +} + +static gboolean +ev_annotation_window_resize (EvAnnotationWindow *window, + GdkEventButton *event, + GtkWidget *ebox) +{ + if (event->type == GDK_BUTTON_PRESS && event->button == 1) { + gtk_window_begin_resize_drag (GTK_WINDOW (window), + window->resize_sw == ebox ? + GDK_WINDOW_EDGE_SOUTH_WEST : + GDK_WINDOW_EDGE_SOUTH_EAST, + event->button, event->x_root, + event->y_root, event->time); + return TRUE; + } + + return FALSE; +} + +static void +ev_annotation_window_set_resize_cursor (GtkWidget *widget, + EvAnnotationWindow *window) +{ + GdkWindow *gdk_window = gtk_widget_get_window (widget); + + if (!gdk_window) + return; + + if (gtk_widget_is_sensitive (widget)) { + GdkDisplay *display = gtk_widget_get_display (widget); + GdkCursor *cursor; + + cursor = gdk_cursor_new_for_display (display, + widget == window->resize_sw ? + GDK_BOTTOM_LEFT_CORNER : + GDK_BOTTOM_RIGHT_CORNER); + gdk_window_set_cursor (gdk_window, cursor); + gdk_cursor_unref (cursor); + } else { + gdk_window_set_cursor (gdk_window, NULL); + } +} + +static gboolean +text_view_button_press (GtkWidget *widget, + GdkEventButton *event, + EvAnnotationWindow *window) +{ + ev_annotation_window_grab_focus (window); + + return FALSE; +} + +static void +ev_annotation_window_close (EvAnnotationWindow *window) +{ + gtk_widget_hide (GTK_WIDGET (window)); + g_signal_emit (window, signals[CLOSED], 0); +} + +static void +ev_annotation_window_init (EvAnnotationWindow *window) +{ + GtkWidget *vbox, *hbox; + GtkWidget *icon; + GtkWidget *swindow; + + gtk_widget_set_can_focus (GTK_WIDGET (window), TRUE); + + vbox = gtk_vbox_new (FALSE, 0); + + /* Title bar */ + hbox = gtk_hbox_new (FALSE, 0); + + icon = gtk_image_new (); /* FIXME: use the annot icon */ + gtk_box_pack_start (GTK_BOX (hbox), icon, FALSE, FALSE, 0); + gtk_widget_show (icon); + + window->title = gtk_label_new (NULL); + gtk_box_pack_start (GTK_BOX (hbox), window->title, TRUE, TRUE, 0); + gtk_widget_show (window->title); + + window->close_button = gtk_button_new (); + gtk_button_set_relief (GTK_BUTTON (window->close_button), GTK_RELIEF_NONE); + gtk_container_set_border_width (GTK_CONTAINER (window->close_button), 0); + g_signal_connect_swapped (window->close_button, "clicked", + G_CALLBACK (ev_annotation_window_close), + window); + icon = gtk_image_new_from_stock (EV_STOCK_CLOSE, EV_ICON_SIZE_ANNOT_WINDOW); + gtk_container_add (GTK_CONTAINER (window->close_button), icon); + gtk_widget_show (icon); + + gtk_box_pack_start (GTK_BOX (hbox), window->close_button, FALSE, FALSE, 0); + gtk_widget_show (window->close_button); + + gtk_box_pack_start (GTK_BOX (vbox), hbox, FALSE, FALSE, 0); + gtk_widget_show (hbox); + + /* Contents */ + swindow = gtk_scrolled_window_new (NULL, NULL); + gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (swindow), + GTK_POLICY_AUTOMATIC, + GTK_POLICY_AUTOMATIC); + window->text_view = gtk_text_view_new (); + gtk_text_view_set_wrap_mode (GTK_TEXT_VIEW (window->text_view), GTK_WRAP_WORD); + g_signal_connect (window->text_view, "button_press_event", + G_CALLBACK (text_view_button_press), + window); + gtk_container_add (GTK_CONTAINER (swindow), window->text_view); + gtk_widget_show (window->text_view); + + gtk_box_pack_start (GTK_BOX (vbox), swindow, TRUE, TRUE, 0); + gtk_widget_show (swindow); + + /* Resize bar */ + hbox = gtk_hbox_new (FALSE, 0); + + window->resize_sw = gtk_event_box_new (); + gtk_widget_add_events (window->resize_sw, GDK_BUTTON_PRESS_MASK); + g_signal_connect_swapped (window->resize_sw, "button-press-event", + G_CALLBACK (ev_annotation_window_resize), + window); + g_signal_connect (window->resize_sw, "realize", + G_CALLBACK (ev_annotation_window_set_resize_cursor), + window); + + icon = gtk_image_new_from_stock (EV_STOCK_RESIZE_SW, EV_ICON_SIZE_ANNOT_WINDOW); + gtk_container_add (GTK_CONTAINER (window->resize_sw), icon); + gtk_widget_show (icon); + gtk_box_pack_start (GTK_BOX (hbox), window->resize_sw, FALSE, FALSE, 0); + gtk_widget_show (window->resize_sw); + + window->resize_se = gtk_event_box_new (); + gtk_widget_add_events (window->resize_se, GDK_BUTTON_PRESS_MASK); + g_signal_connect_swapped (window->resize_se, "button-press-event", + G_CALLBACK (ev_annotation_window_resize), + window); + g_signal_connect (window->resize_se, "realize", + G_CALLBACK (ev_annotation_window_set_resize_cursor), + window); + + icon = gtk_image_new_from_stock (EV_STOCK_RESIZE_SE, EV_ICON_SIZE_ANNOT_WINDOW); + gtk_container_add (GTK_CONTAINER (window->resize_se), icon); + gtk_widget_show (icon); + gtk_box_pack_end (GTK_BOX (hbox), window->resize_se, FALSE, FALSE, 0); + gtk_widget_show (window->resize_se); + + gtk_box_pack_start (GTK_BOX (vbox), hbox, FALSE, FALSE, 0); + gtk_widget_show (hbox); + + gtk_container_add (GTK_CONTAINER (window), vbox); + gtk_container_set_border_width (GTK_CONTAINER (window), 0); + gtk_widget_show (vbox); + + gtk_widget_add_events (GTK_WIDGET (window), + GDK_BUTTON_PRESS_MASK | + GDK_KEY_PRESS_MASK); + gtk_widget_set_app_paintable (GTK_WIDGET (window), TRUE); + + gtk_container_set_border_width (GTK_CONTAINER (window), 2); + + gtk_window_set_accept_focus (GTK_WINDOW (window), TRUE); + gtk_window_set_decorated (GTK_WINDOW (window), FALSE); + gtk_window_set_skip_taskbar_hint (GTK_WINDOW (window), TRUE); + gtk_window_set_skip_pager_hint (GTK_WINDOW (window), TRUE); + gtk_window_set_resizable (GTK_WINDOW (window), TRUE); +} + +static GObject * +ev_annotation_window_constructor (GType type, + guint n_construct_properties, + GObjectConstructParam *construct_params) +{ + GObject *object; + EvAnnotationWindow *window; + EvAnnotation *annot; + EvAnnotationMarkup *markup; + const gchar *contents; + const gchar *label; + GdkColor color; + EvRectangle *rect; + gdouble scale; + + object = G_OBJECT_CLASS (ev_annotation_window_parent_class)->constructor (type, + n_construct_properties, + construct_params); + window = EV_ANNOTATION_WINDOW (object); + annot = window->annotation; + markup = EV_ANNOTATION_MARKUP (annot); + + gtk_window_set_transient_for (GTK_WINDOW (window), window->parent); + gtk_window_set_destroy_with_parent (GTK_WINDOW (window), FALSE); + + label = ev_annotation_markup_get_label (markup); + window->is_open = ev_annotation_markup_get_popup_is_open (markup); + ev_annotation_markup_get_rectangle (markup, &window->rect); + + rect = &window->rect; + + /* Rectangle is at doc resolution (72.0) */ + scale = get_screen_dpi (window) / 72.0; + gtk_window_resize (GTK_WINDOW (window), + (gint)((rect->x2 - rect->x1) * scale), + (gint)((rect->y2 - rect->y1) * scale)); + + ev_annotation_get_color (annot, &color); + ev_annotation_window_set_color (window, &color); + gtk_widget_set_name (GTK_WIDGET (window), ev_annotation_get_name (annot)); + gtk_window_set_title (GTK_WINDOW (window), label); + gtk_label_set_text (GTK_LABEL (window->title), label); + + contents = ev_annotation_get_contents (annot); + if (contents) { + GtkTextBuffer *buffer; + + buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (window->text_view)); + gtk_text_buffer_set_text (buffer, contents, -1); + } + + g_signal_connect (annot, "notify::label", + G_CALLBACK (ev_annotation_window_label_changed), + window); + g_signal_connect (annot, "notify::color", + G_CALLBACK (ev_annotation_window_color_changed), + window); + + return object; +} + +static gboolean +ev_annotation_window_button_press_event (GtkWidget *widget, + GdkEventButton *event) +{ + EvAnnotationWindow *window = EV_ANNOTATION_WINDOW (widget); + + if (event->type == GDK_BUTTON_PRESS && event->button == 1) { + window->in_move = TRUE; + window->x = event->x_root - event->x; + window->y = event->y_root - event->y; + gtk_window_begin_move_drag (GTK_WINDOW (widget), + event->button, + event->x_root, + event->y_root, + event->time); + return TRUE; + } + + return FALSE; +} + +static gboolean +ev_annotation_window_configure_event (GtkWidget *widget, + GdkEventConfigure *event) +{ + EvAnnotationWindow *window = EV_ANNOTATION_WINDOW (widget); + + if (window->in_move && + (window->x != event->x || window->y != event->y)) { + window->x = event->x; + window->y = event->y; + } + + return GTK_WIDGET_CLASS (ev_annotation_window_parent_class)->configure_event (widget, event); +} + +static gboolean +ev_annotation_window_focus_in_event (GtkWidget *widget, + GdkEventFocus *event) +{ + EvAnnotationWindow *window = EV_ANNOTATION_WINDOW (widget); + + if (window->in_move) { + if (window->orig_x != window->x || window->orig_y != window->y) { + window->orig_x = window->x; + window->orig_y = window->y; + g_signal_emit (window, signals[MOVED], 0, window->x, window->y); + } + window->in_move = FALSE; + } + + return FALSE; +} + +static gboolean +ev_annotation_window_focus_out_event (GtkWidget *widget, + GdkEventFocus *event) +{ + EvAnnotationWindow *window = EV_ANNOTATION_WINDOW (widget); + + ev_annotation_window_sync_contents (window); + + return FALSE; +} + +static void +ev_annotation_window_class_init (EvAnnotationWindowClass *klass) +{ + GObjectClass *g_object_class = G_OBJECT_CLASS (klass); + GtkWidgetClass *gtk_widget_class = GTK_WIDGET_CLASS (klass); + + g_object_class->constructor = ev_annotation_window_constructor; + g_object_class->set_property = ev_annotation_window_set_property; + g_object_class->dispose = ev_annotation_window_dispose; + + gtk_widget_class->button_press_event = ev_annotation_window_button_press_event; + gtk_widget_class->configure_event = ev_annotation_window_configure_event; + gtk_widget_class->focus_in_event = ev_annotation_window_focus_in_event; + gtk_widget_class->focus_out_event = ev_annotation_window_focus_out_event; + + g_object_class_install_property (g_object_class, + PROP_ANNOTATION, + g_param_spec_object ("annotation", + "Annotation", + "The annotation associated to the window", + EV_TYPE_ANNOTATION_MARKUP, + G_PARAM_WRITABLE | + G_PARAM_CONSTRUCT_ONLY)); + g_object_class_install_property (g_object_class, + PROP_PARENT, + g_param_spec_object ("parent", + "Parent", + "The parent window", + GTK_TYPE_WINDOW, + G_PARAM_WRITABLE | + G_PARAM_CONSTRUCT_ONLY)); + signals[CLOSED] = + g_signal_new ("closed", + G_TYPE_FROM_CLASS (g_object_class), + G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION, + G_STRUCT_OFFSET (EvAnnotationWindowClass, closed), + NULL, NULL, + g_cclosure_marshal_VOID__VOID, + G_TYPE_NONE, 0, G_TYPE_NONE); + signals[MOVED] = + g_signal_new ("moved", + G_TYPE_FROM_CLASS (g_object_class), + G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION, + G_STRUCT_OFFSET (EvAnnotationWindowClass, moved), + NULL, NULL, + ev_view_marshal_VOID__INT_INT, + G_TYPE_NONE, 2, + G_TYPE_INT, G_TYPE_INT); +} + +/* Public methods */ +GtkWidget * +ev_annotation_window_new (EvAnnotation *annot, + GtkWindow *parent) +{ + GtkWidget *window; + + g_return_val_if_fail (EV_IS_ANNOTATION_MARKUP (annot), NULL); + g_return_val_if_fail (GTK_IS_WINDOW (parent), NULL); + + window = g_object_new (EV_TYPE_ANNOTATION_WINDOW, + "annotation", annot, + "parent", parent, + NULL); + return window; +} + +EvAnnotation * +ev_annotation_window_get_annotation (EvAnnotationWindow *window) +{ + g_return_val_if_fail (EV_IS_ANNOTATION_WINDOW (window), NULL); + + return window->annotation; +} + +void +ev_annotation_window_set_annotation (EvAnnotationWindow *window, + EvAnnotation *annot) +{ + g_return_if_fail (EV_IS_ANNOTATION_WINDOW (window)); + g_return_if_fail (EV_IS_ANNOTATION (annot)); + + if (annot == window->annotation) + return; + + g_object_unref (window->annotation); + window->annotation = g_object_ref (annot); + ev_annotation_window_sync_contents (window); + g_object_notify (G_OBJECT (window), "annotation"); +} + +gboolean +ev_annotation_window_is_open (EvAnnotationWindow *window) +{ + g_return_val_if_fail (EV_IS_ANNOTATION_WINDOW (window), FALSE); + + return window->is_open; +} + +void +ev_annotation_window_get_rectangle (EvAnnotationWindow *window, + EvRectangle *rect) +{ + g_return_if_fail (EV_IS_ANNOTATION_WINDOW (window)); + g_return_if_fail (rect != NULL); + + *rect = window->rect; +} + +void +ev_annotation_window_set_rectangle (EvAnnotationWindow *window, + const EvRectangle *rect) +{ + g_return_if_fail (EV_IS_ANNOTATION_WINDOW (window)); + g_return_if_fail (rect != NULL); + + window->rect = *rect; +} + +void +ev_annotation_window_grab_focus (EvAnnotationWindow *window) +{ + g_return_if_fail (EV_IS_ANNOTATION_WINDOW (window)); + + if (!gtk_widget_has_focus (window->text_view)) { + gtk_widget_grab_focus (GTK_WIDGET (window)); + send_focus_change (window->text_view, TRUE); + } +} + +void +ev_annotation_window_ungrab_focus (EvAnnotationWindow *window) +{ + g_return_if_fail (EV_IS_ANNOTATION_WINDOW (window)); + + if (gtk_widget_has_focus (window->text_view)) { + send_focus_change (window->text_view, FALSE); + } + + ev_annotation_window_sync_contents (window); +} diff --git a/libview/ev-annotation-window.h b/libview/ev-annotation-window.h new file mode 100644 index 00000000..267a6a61 --- /dev/null +++ b/libview/ev-annotation-window.h @@ -0,0 +1,57 @@ +/* ev-annotation-window.h + * this file is part of evince, a mate document viewer + * + * Copyright (C) 2009 Carlos Garcia Campos <[email protected]> + * Copyright (C) 2007 IƱigo Martinez <[email protected]> + * + * Evince is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * Evince is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ + +#ifndef EV_ANNOTATION_WINDOW_H +#define EV_ANNOTATION_WINDOW_H + +#include <gtk/gtk.h> + +#include "ev-annotation.h" + +G_BEGIN_DECLS + +typedef struct _EvAnnotationWindow EvAnnotationWindow; +typedef struct _EvAnnotationWindowClass EvAnnotationWindowClass; + +#define EV_TYPE_ANNOTATION_WINDOW (ev_annotation_window_get_type()) +#define EV_ANNOTATION_WINDOW(object) (G_TYPE_CHECK_INSTANCE_CAST((object), EV_TYPE_ANNOTATION_WINDOW, EvAnnotationWindow)) +#define EV_ANNOTATION_WINDOW_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST((klass), EV_TYPE_ANNOTATION_WINDOW, EvAnnotationWindowClass)) +#define EV_IS_ANNOTATION_WINDOW(object) (G_TYPE_CHECK_INSTANCE_TYPE((object), EV_TYPE_ANNOTATION_WINDOW)) +#define EV_IS_ANNOTATION_WINDOW_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE((klass), EV_TYPE_ANNOTATION_WINDOW)) +#define EV_ANNOTATION_WINDOW_GET_CLASS(object) (G_TYPE_INSTANCE_GET_CLASS((object), EV_TYPE_ANNOTATION_WINDOW, EvAnnotationWindowClass)) + +GType ev_annotation_window_get_type (void) G_GNUC_CONST; +GtkWidget *ev_annotation_window_new (EvAnnotation *annot, + GtkWindow *parent); +EvAnnotation *ev_annotation_window_get_annotation (EvAnnotationWindow *window); +void ev_annotation_window_set_annotation (EvAnnotationWindow *window, + EvAnnotation *annot); +gboolean ev_annotation_window_is_open (EvAnnotationWindow *window); +void ev_annotation_window_get_rectangle (EvAnnotationWindow *window, + EvRectangle *rect); +void ev_annotation_window_set_rectangle (EvAnnotationWindow *window, + const EvRectangle *rect); +void ev_annotation_window_grab_focus (EvAnnotationWindow *window); +void ev_annotation_window_ungrab_focus (EvAnnotationWindow *window); + +G_END_DECLS + +#endif /* EV_ANNOTATION_WINDOW_H */ diff --git a/libview/ev-document-model.c b/libview/ev-document-model.c new file mode 100644 index 00000000..a15ab5ca --- /dev/null +++ b/libview/ev-document-model.c @@ -0,0 +1,571 @@ +/* this file is part of evince, a mate document viewer + * + * Copyright (C) 2009 Carlos Garcia Campos + * Copyright (C) 2005 Red Hat, Inc + * + * Evince is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * Evince is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ + +#include "config.h" + +#include "ev-document-model.h" +#include "ev-view-type-builtins.h" +#include "ev-view-marshal.h" + +struct _EvDocumentModel +{ + GObject base; + + EvDocument *document; + gint n_pages; + + gint page; + gint rotation; + gdouble scale; + EvSizingMode sizing_mode; + guint continuous : 1; + guint dual_page : 1; + guint fullscreen : 1; + guint inverted_colors : 1; + + gdouble max_scale; + gdouble min_scale; +}; + +struct _EvDocumentModelClass +{ + GObjectClass base_class; + + /* Signals */ + void (* page_changed) (EvDocumentModel *model, + gint old_page, + gint new_page); +}; + +enum { + PROP_0, + PROP_DOCUMENT, + PROP_PAGE, + PROP_ROTATION, + PROP_INVERTED_COLORS, + PROP_SCALE, + PROP_SIZING_MODE, + PROP_CONTINUOUS, + PROP_DUAL_PAGE, + PROP_FULLSCREEN +}; + +enum +{ + PAGE_CHANGED, + N_SIGNALS +}; + +static guint signals[N_SIGNALS] = { 0 }; + +G_DEFINE_TYPE (EvDocumentModel, ev_document_model, G_TYPE_OBJECT) + +static void +ev_document_model_finalize (GObject *object) +{ + EvDocumentModel *model = EV_DOCUMENT_MODEL (object); + + if (model->document) { + g_object_unref (model->document); + model->document = NULL; + } + + G_OBJECT_CLASS (ev_document_model_parent_class)->finalize (object); +} + +static void +ev_document_model_set_property (GObject *object, + guint prop_id, + const GValue *value, + GParamSpec *pspec) +{ + EvDocumentModel *model = EV_DOCUMENT_MODEL (object); + + switch (prop_id) { + case PROP_DOCUMENT: + ev_document_model_set_document (model, (EvDocument *)g_value_get_object (value)); + break; + case PROP_PAGE: + ev_document_model_set_page (model, g_value_get_int (value)); + break; + case PROP_ROTATION: + ev_document_model_set_rotation (model, g_value_get_int (value)); + break; + case PROP_INVERTED_COLORS: + ev_document_model_set_inverted_colors (model, g_value_get_boolean (value)); + break; + case PROP_SCALE: + ev_document_model_set_scale (model, g_value_get_double (value)); + break; + case PROP_SIZING_MODE: + ev_document_model_set_sizing_mode (model, g_value_get_enum (value)); + break; + case PROP_CONTINUOUS: + ev_document_model_set_continuous (model, g_value_get_boolean (value)); + break; + case PROP_DUAL_PAGE: + ev_document_model_set_dual_page (model, g_value_get_boolean (value)); + break; + case PROP_FULLSCREEN: + ev_document_model_set_fullscreen (model, g_value_get_boolean (value)); + break; + default: + G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); + } +} + +static void +ev_document_model_get_property (GObject *object, + guint prop_id, + GValue *value, + GParamSpec *pspec) +{ + EvDocumentModel *model = EV_DOCUMENT_MODEL (object); + + switch (prop_id) { + case PROP_DOCUMENT: + g_value_set_object (value, model->document); + break; + case PROP_PAGE: + g_value_set_int (value, model->page); + break; + case PROP_ROTATION: + g_value_set_int (value, model->rotation); + break; + case PROP_INVERTED_COLORS: + g_value_set_boolean (value, model->inverted_colors); + break; + case PROP_SCALE: + g_value_set_double (value, model->scale); + break; + case PROP_SIZING_MODE: + g_value_set_enum (value, model->sizing_mode); + break; + case PROP_CONTINUOUS: + g_value_set_boolean (value, ev_document_model_get_continuous (model)); + break; + case PROP_DUAL_PAGE: + g_value_set_boolean (value, ev_document_model_get_dual_page (model)); + break; + case PROP_FULLSCREEN: + g_value_set_boolean (value, ev_document_model_get_fullscreen (model)); + break; + default: + G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); + } +} + +static void +ev_document_model_class_init (EvDocumentModelClass *klass) +{ + GObjectClass *g_object_class = G_OBJECT_CLASS (klass); + + g_object_class->get_property = ev_document_model_get_property; + g_object_class->set_property = ev_document_model_set_property; + g_object_class->finalize = ev_document_model_finalize; + + /* Properties */ + g_object_class_install_property (g_object_class, + PROP_DOCUMENT, + g_param_spec_object ("document", + "Document", + "The current document", + EV_TYPE_DOCUMENT, + G_PARAM_READWRITE)); + g_object_class_install_property (g_object_class, + PROP_PAGE, + g_param_spec_int ("page", + "Page", + "Current page", + -1, G_MAXINT, -1, + G_PARAM_READWRITE)); + g_object_class_install_property (g_object_class, + PROP_ROTATION, + g_param_spec_int ("rotation", + "Rotation", + "Current rotation angle", + 0, 360, 0, + G_PARAM_READWRITE)); + g_object_class_install_property (g_object_class, + PROP_INVERTED_COLORS, + g_param_spec_boolean ("inverted-colors", + "Inverted Colors", + "Whether document is displayed with inverted colors", + FALSE, + G_PARAM_READWRITE)); + g_object_class_install_property (g_object_class, + PROP_SCALE, + g_param_spec_double ("scale", + "Scale", + "Current scale factor", + 0., G_MAXDOUBLE, 1., + G_PARAM_READWRITE)); + g_object_class_install_property (g_object_class, + PROP_SIZING_MODE, + g_param_spec_enum ("sizing-mode", + "Sizing Mode", + "Current sizing mode", + EV_TYPE_SIZING_MODE, + EV_SIZING_FIT_WIDTH, + G_PARAM_READWRITE)); + g_object_class_install_property (g_object_class, + PROP_CONTINUOUS, + g_param_spec_boolean ("continuous", + "Continuous", + "Whether document is displayed in continuous mode", + TRUE, + G_PARAM_READWRITE)); + g_object_class_install_property (g_object_class, + PROP_DUAL_PAGE, + g_param_spec_boolean ("dual-page", + "Dual Page", + "Whether document is displayed in dual page mode", + FALSE, + G_PARAM_READWRITE)); + g_object_class_install_property (g_object_class, + PROP_FULLSCREEN, + g_param_spec_boolean ("fullscreen", + "Fullscreen", + "Whether document is displayed in fullscreen mode", + FALSE, + G_PARAM_READWRITE)); + + /* Signals */ + signals [PAGE_CHANGED] = + g_signal_new ("page-changed", + EV_TYPE_DOCUMENT_MODEL, + G_SIGNAL_RUN_LAST, + G_STRUCT_OFFSET (EvDocumentModelClass, page_changed), + NULL, NULL, + ev_view_marshal_VOID__INT_INT, + G_TYPE_NONE, 2, + G_TYPE_INT, G_TYPE_INT); +} + +static void +ev_document_model_init (EvDocumentModel *model) +{ + model->page = -1; + model->scale = 1.; + model->sizing_mode = EV_SIZING_FIT_WIDTH; + model->continuous = TRUE; + model->inverted_colors = FALSE; + model->min_scale = 0.; + model->max_scale = G_MAXDOUBLE; +} + +EvDocumentModel * +ev_document_model_new (void) +{ + return g_object_new (EV_TYPE_DOCUMENT_MODEL, NULL); +} + +EvDocumentModel * +ev_document_model_new_with_document (EvDocument *document) +{ + g_return_val_if_fail (EV_IS_DOCUMENT (document), NULL); + + return g_object_new (EV_TYPE_DOCUMENT_MODEL, "document", document, NULL); +} + +void +ev_document_model_set_document (EvDocumentModel *model, + EvDocument *document) +{ + g_return_if_fail (EV_IS_DOCUMENT_MODEL (model)); + g_return_if_fail (EV_IS_DOCUMENT (document)); + + if (document == model->document) + return; + + if (model->document) + g_object_unref (model->document); + model->document = g_object_ref (document); + + model->n_pages = ev_document_get_n_pages (document); + ev_document_model_set_page (model, CLAMP (model->page, 0, + model->n_pages - 1)); + + g_object_notify (G_OBJECT (model), "document"); +} + +EvDocument * +ev_document_model_get_document (EvDocumentModel *model) +{ + g_return_val_if_fail (EV_IS_DOCUMENT_MODEL (model), NULL); + + return model->document; +} + +void +ev_document_model_set_page (EvDocumentModel *model, + gint page) +{ + gint old_page; + + g_return_if_fail (EV_IS_DOCUMENT_MODEL (model)); + + if (model->page == page) + return; + if (page < 0 || (model->document && page >= model->n_pages)) + return; + + old_page = model->page; + model->page = page; + g_signal_emit (model, signals[PAGE_CHANGED], 0, old_page, page); + + g_object_notify (G_OBJECT (model), "page"); +} + +void +ev_document_model_set_page_by_label (EvDocumentModel *model, + const gchar *page_label) +{ + gint page; + + g_return_if_fail (EV_IS_DOCUMENT_MODEL (model)); + g_return_if_fail (model->document != NULL); + + if (ev_document_find_page_by_label (model->document, page_label, &page)) + ev_document_model_set_page (model, page); +} + +gint +ev_document_model_get_page (EvDocumentModel *model) +{ + g_return_val_if_fail (EV_IS_DOCUMENT_MODEL (model), -1); + + return model->page; +} + +void +ev_document_model_set_scale (EvDocumentModel *model, + gdouble scale) +{ + g_return_if_fail (EV_IS_DOCUMENT_MODEL (model)); + + scale = CLAMP (scale, + model->sizing_mode == EV_SIZING_FREE ? + model->min_scale : 0, model->max_scale); + + if (scale == model->scale) + return; + + model->scale = scale; + + g_object_notify (G_OBJECT (model), "scale"); +} + +gdouble +ev_document_model_get_scale (EvDocumentModel *model) +{ + g_return_val_if_fail (EV_IS_DOCUMENT_MODEL (model), 1.0); + + return model->scale; +} + +void +ev_document_model_set_max_scale (EvDocumentModel *model, + gdouble max_scale) +{ + g_return_if_fail (EV_IS_DOCUMENT_MODEL (model)); + + if (max_scale == model->max_scale) + return; + + model->max_scale = max_scale; + + if (model->scale > max_scale) + ev_document_model_set_scale (model, max_scale); +} + +gdouble +ev_document_model_get_max_scale (EvDocumentModel *model) +{ + g_return_val_if_fail (EV_IS_DOCUMENT_MODEL (model), 1.0); + + return model->max_scale; +} + +void +ev_document_model_set_min_scale (EvDocumentModel *model, + gdouble min_scale) +{ + g_return_if_fail (EV_IS_DOCUMENT_MODEL (model)); + + if (min_scale == model->min_scale) + return; + + model->min_scale = min_scale; + + if (model->scale < min_scale) + ev_document_model_set_scale (model, min_scale); +} + +gdouble +ev_document_model_get_min_scale (EvDocumentModel *model) +{ + g_return_val_if_fail (EV_IS_DOCUMENT_MODEL (model), 0.); + + return model->min_scale; +} + +void +ev_document_model_set_sizing_mode (EvDocumentModel *model, + EvSizingMode mode) +{ + g_return_if_fail (EV_IS_DOCUMENT_MODEL (model)); + + if (mode == model->sizing_mode) + return; + + model->sizing_mode = mode; + + g_object_notify (G_OBJECT (model), "sizing-mode"); +} + +EvSizingMode +ev_document_model_get_sizing_mode (EvDocumentModel *model) +{ + g_return_val_if_fail (EV_IS_DOCUMENT_MODEL (model), EV_SIZING_FIT_WIDTH); + + return model->sizing_mode; +} + +void +ev_document_model_set_rotation (EvDocumentModel *model, + gint rotation) +{ + g_return_if_fail (EV_IS_DOCUMENT_MODEL (model)); + + if (rotation >= 360) + rotation -= 360; + else if (rotation < 0) + rotation += 360; + + if (rotation == model->rotation) + return; + + model->rotation = rotation; + + g_object_notify (G_OBJECT (model), "rotation"); +} + +gint +ev_document_model_get_rotation (EvDocumentModel *model) +{ + g_return_val_if_fail (EV_IS_DOCUMENT_MODEL (model), 0); + + return model->rotation; +} + +void +ev_document_model_set_inverted_colors (EvDocumentModel *model, + gboolean inverted_colors) +{ + g_return_if_fail (EV_IS_DOCUMENT_MODEL (model)); + + if (inverted_colors == model->inverted_colors) + return; + + model->inverted_colors = inverted_colors; + + g_object_notify (G_OBJECT (model), "inverted-colors"); +} + +gboolean +ev_document_model_get_inverted_colors (EvDocumentModel *model) +{ + g_return_val_if_fail (EV_IS_DOCUMENT_MODEL (model), FALSE); + + return model->inverted_colors; +} + +void +ev_document_model_set_continuous (EvDocumentModel *model, + gboolean continuous) +{ + g_return_if_fail (EV_IS_DOCUMENT_MODEL (model)); + + continuous = continuous != FALSE; + + if (continuous == model->continuous) + return; + + model->continuous = continuous; + + g_object_notify (G_OBJECT (model), "continuous"); +} + +gboolean +ev_document_model_get_continuous (EvDocumentModel *model) +{ + g_return_val_if_fail (EV_IS_DOCUMENT_MODEL (model), TRUE); + + return model->continuous; +} + +void +ev_document_model_set_dual_page (EvDocumentModel *model, + gboolean dual_page) +{ + g_return_if_fail (EV_IS_DOCUMENT_MODEL (model)); + + dual_page = dual_page != FALSE; + + if (dual_page == model->dual_page) + return; + + model->dual_page = dual_page; + + g_object_notify (G_OBJECT (model), "dual-page"); +} + +gboolean +ev_document_model_get_dual_page (EvDocumentModel *model) +{ + g_return_val_if_fail (EV_IS_DOCUMENT_MODEL (model), FALSE); + + return model->dual_page; +} + +void +ev_document_model_set_fullscreen (EvDocumentModel *model, + gboolean fullscreen) +{ + g_return_if_fail (EV_IS_DOCUMENT_MODEL (model)); + + fullscreen = fullscreen != FALSE; + + if (fullscreen == model->fullscreen) + return; + + model->fullscreen = fullscreen; + + g_object_notify (G_OBJECT (model), "fullscreen"); +} + +gboolean +ev_document_model_get_fullscreen (EvDocumentModel *model) +{ + g_return_val_if_fail (EV_IS_DOCUMENT_MODEL (model), FALSE); + + return model->fullscreen; +} diff --git a/libview/ev-document-model.h b/libview/ev-document-model.h new file mode 100644 index 00000000..05525ebe --- /dev/null +++ b/libview/ev-document-model.h @@ -0,0 +1,87 @@ +/* this file is part of evince, a mate document viewer + * + * Copyright (C) 2009 Carlos Garcia Campos + * + * Evince is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * Evince is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ + +#if !defined (__EV_EVINCE_VIEW_H_INSIDE__) && !defined (EVINCE_COMPILATION) +#error "Only <evince-view.h> can be included directly." +#endif + +#ifndef __EV_DOCUMENT_MODEL_H__ +#define __EV_DOCUMENT_MODEL_H__ + +#include <glib-object.h> +#include <evince-document.h> + +G_BEGIN_DECLS + +#define EV_TYPE_DOCUMENT_MODEL (ev_document_model_get_type ()) +#define EV_DOCUMENT_MODEL(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), EV_TYPE_DOCUMENT_MODEL, EvDocumentModel)) +#define EV_IS_DOCUMENT_MODEL(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), EV_TYPE_DOCUMENT_MODEL)) + +typedef enum { + EV_SIZING_BEST_FIT, + EV_SIZING_FIT_WIDTH, + EV_SIZING_FREE, +} EvSizingMode; + +typedef struct _EvDocumentModel EvDocumentModel; +typedef struct _EvDocumentModelClass EvDocumentModelClass; + +GType ev_document_model_get_type (void) G_GNUC_CONST; +EvDocumentModel *ev_document_model_new (void); +EvDocumentModel *ev_document_model_new_with_document (EvDocument *document); + +void ev_document_model_set_document (EvDocumentModel *model, + EvDocument *document); +EvDocument *ev_document_model_get_document (EvDocumentModel *model); +void ev_document_model_set_page (EvDocumentModel *model, + gint page); +void ev_document_model_set_page_by_label (EvDocumentModel *model, + const gchar *page_label); +gint ev_document_model_get_page (EvDocumentModel *model); +void ev_document_model_set_scale (EvDocumentModel *model, + gdouble scale); +gdouble ev_document_model_get_scale (EvDocumentModel *model); +void ev_document_model_set_max_scale (EvDocumentModel *model, + gdouble max_scale); +gdouble ev_document_model_get_max_scale (EvDocumentModel *model); +void ev_document_model_set_min_scale (EvDocumentModel *model, + gdouble min_scale); +gdouble ev_document_model_get_min_scale (EvDocumentModel *model); +void ev_document_model_set_sizing_mode (EvDocumentModel *model, + EvSizingMode mode); +EvSizingMode ev_document_model_get_sizing_mode (EvDocumentModel *model); +void ev_document_model_set_rotation (EvDocumentModel *model, + gint rotation); +gint ev_document_model_get_rotation (EvDocumentModel *model); +void ev_document_model_set_inverted_colors (EvDocumentModel *model, + gboolean inverted_colors); +gboolean ev_document_model_get_inverted_colors (EvDocumentModel *model); +void ev_document_model_set_continuous (EvDocumentModel *model, + gboolean continuous); +gboolean ev_document_model_get_continuous (EvDocumentModel *model); +void ev_document_model_set_dual_page (EvDocumentModel *model, + gboolean dual_page); +gboolean ev_document_model_get_dual_page (EvDocumentModel *model); +void ev_document_model_set_fullscreen (EvDocumentModel *model, + gboolean fullscreen); +gboolean ev_document_model_get_fullscreen (EvDocumentModel *model); + +G_END_DECLS + +#endif /* __EV_DOCUMENT_MODEL_H__ */ diff --git a/libview/ev-job-scheduler.c b/libview/ev-job-scheduler.c new file mode 100644 index 00000000..f874832f --- /dev/null +++ b/libview/ev-job-scheduler.c @@ -0,0 +1,305 @@ +/* ev-job-scheduler.c + * this file is part of evince, a mate document viewer + * + * Copyright (C) 2008 Carlos Garcia Campos <[email protected]> + * + * Evince is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * Evince is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ + +#include "ev-debug.h" +#include "ev-job-scheduler.h" + +typedef struct _EvSchedulerJob { + EvJob *job; + EvJobPriority priority; + GSList *job_link; +} EvSchedulerJob; + +G_LOCK_DEFINE_STATIC(job_list); +static GSList *job_list = NULL; + +static gpointer ev_job_thread_proxy (gpointer data); +static void ev_scheduler_thread_job_cancelled (EvSchedulerJob *job, + GCancellable *cancellable); + +/* EvJobQueue */ +static GQueue queue_urgent = G_QUEUE_INIT; +static GQueue queue_high = G_QUEUE_INIT; +static GQueue queue_low = G_QUEUE_INIT; +static GQueue queue_none = G_QUEUE_INIT; + +static GCond *job_queue_cond = NULL; +static GMutex *job_queue_mutex = NULL; +static GQueue *job_queue[EV_JOB_N_PRIORITIES] = { + &queue_urgent, + &queue_high, + &queue_low, + &queue_none +}; + +static void +ev_job_queue_push (EvSchedulerJob *job, + EvJobPriority priority) +{ + ev_debug_message (DEBUG_JOBS, "%s priority %d", EV_GET_TYPE_NAME (job->job), priority); + + g_mutex_lock (job_queue_mutex); + + g_queue_push_tail (job_queue[priority], job); + g_cond_broadcast (job_queue_cond); + + g_mutex_unlock (job_queue_mutex); +} + +static EvSchedulerJob * +ev_job_queue_get_next_unlocked (void) +{ + gint i; + EvSchedulerJob *job = NULL; + + for (i = EV_JOB_PRIORITY_URGENT; i < EV_JOB_N_PRIORITIES; i++) { + job = (EvSchedulerJob *) g_queue_pop_head (job_queue[i]); + if (job) + break; + } + + ev_debug_message (DEBUG_JOBS, "%s", job ? EV_GET_TYPE_NAME (job->job) : "No jobs in queue"); + + return job; +} + +static gpointer +ev_job_scheduler_init (gpointer data) +{ + job_queue_cond = g_cond_new (); + job_queue_mutex = g_mutex_new (); + g_thread_create (ev_job_thread_proxy, NULL, FALSE, NULL); + + return NULL; +} + +static void +ev_scheduler_job_list_add (EvSchedulerJob *job) +{ + ev_debug_message (DEBUG_JOBS, "%s", EV_GET_TYPE_NAME (job->job)); + + G_LOCK (job_list); + + job_list = g_slist_prepend (job_list, job); + job->job_link = job_list; + + G_UNLOCK (job_list); +} + +static void +ev_scheduler_job_list_remove (EvSchedulerJob *job) +{ + ev_debug_message (DEBUG_JOBS, "%s", EV_GET_TYPE_NAME (job->job)); + + G_LOCK (job_list); + + job_list = g_slist_delete_link (job_list, job->job_link); + + G_UNLOCK (job_list); +} + +static void +ev_scheduler_job_free (EvSchedulerJob *job) +{ + if (!job) + return; + + g_object_unref (job->job); + g_free (job); +} + +static void +ev_scheduler_job_destroy (EvSchedulerJob *job) +{ + ev_debug_message (DEBUG_JOBS, "%s", EV_GET_TYPE_NAME (job->job)); + + if (job->job->run_mode == EV_JOB_RUN_MAIN_LOOP) { + g_signal_handlers_disconnect_by_func (job->job, + G_CALLBACK (ev_scheduler_job_destroy), + job); + } else { + g_signal_handlers_disconnect_by_func (job->job->cancellable, + G_CALLBACK (ev_scheduler_thread_job_cancelled), + job); + } + + ev_scheduler_job_list_remove (job); + ev_scheduler_job_free (job); +} + +static void +ev_scheduler_thread_job_cancelled (EvSchedulerJob *job, + GCancellable *cancellable) +{ + GList *list; + + ev_debug_message (DEBUG_JOBS, "%s", EV_GET_TYPE_NAME (job->job)); + + g_mutex_lock (job_queue_mutex); + + /* If the job is not still running, + * remove it from the job queue and job list. + * If the job is currently running, it will be + * destroyed as soon as it finishes. + */ + list = g_queue_find (job_queue[job->priority], job); + if (list) { + g_queue_delete_link (job_queue[job->priority], list); + g_mutex_unlock (job_queue_mutex); + ev_scheduler_job_destroy (job); + } else { + g_mutex_unlock (job_queue_mutex); + } +} + +static void +ev_job_thread (EvJob *job) +{ + gboolean result; + + ev_debug_message (DEBUG_JOBS, "%s", EV_GET_TYPE_NAME (job)); + + do { + if (g_cancellable_is_cancelled (job->cancellable)) + result = FALSE; + else + result = ev_job_run (job); + } while (result); +} + +static gboolean +ev_job_idle (EvJob *job) +{ + ev_debug_message (DEBUG_JOBS, "%s", EV_GET_TYPE_NAME (job)); + + if (g_cancellable_is_cancelled (job->cancellable)) + return FALSE; + + return ev_job_run (job); +} + +static gpointer +ev_job_thread_proxy (gpointer data) +{ + while (TRUE) { + EvSchedulerJob *job; + + g_mutex_lock (job_queue_mutex); + job = ev_job_queue_get_next_unlocked (); + if (!job) { + g_cond_wait (job_queue_cond, job_queue_mutex); + g_mutex_unlock (job_queue_mutex); + continue; + } + g_mutex_unlock (job_queue_mutex); + + ev_job_thread (job->job); + ev_scheduler_job_destroy (job); + } + + return NULL; +} + +void +ev_job_scheduler_push_job (EvJob *job, + EvJobPriority priority) +{ + static GOnce once_init = G_ONCE_INIT; + EvSchedulerJob *s_job; + + g_once (&once_init, ev_job_scheduler_init, NULL); + + ev_debug_message (DEBUG_JOBS, "%s pirority %d", EV_GET_TYPE_NAME (job), priority); + + s_job = g_new0 (EvSchedulerJob, 1); + s_job->job = g_object_ref (job); + s_job->priority = priority; + + ev_scheduler_job_list_add (s_job); + + switch (ev_job_get_run_mode (job)) { + case EV_JOB_RUN_THREAD: + g_signal_connect_swapped (job->cancellable, "cancelled", + G_CALLBACK (ev_scheduler_thread_job_cancelled), + s_job); + ev_job_queue_push (s_job, priority); + break; + case EV_JOB_RUN_MAIN_LOOP: + g_signal_connect_swapped (job, "finished", + G_CALLBACK (ev_scheduler_job_destroy), + s_job); + g_signal_connect_swapped (job, "cancelled", + G_CALLBACK (ev_scheduler_job_destroy), + s_job); + g_idle_add_full (G_PRIORITY_DEFAULT_IDLE, + (GSourceFunc)ev_job_idle, + g_object_ref (job), + (GDestroyNotify)g_object_unref); + break; + default: + g_assert_not_reached (); + } +} + +void +ev_job_scheduler_update_job (EvJob *job, + EvJobPriority priority) +{ + GSList *l; + EvSchedulerJob *s_job = NULL; + gboolean need_resort = FALSE; + + /* Main loop jobs are scheduled inmediately */ + if (ev_job_get_run_mode (job) == EV_JOB_RUN_MAIN_LOOP) + return; + + ev_debug_message (DEBUG_JOBS, "%s pirority %d", EV_GET_TYPE_NAME (job), priority); + + G_LOCK (job_list); + + for (l = job_list; l; l = l->next) { + s_job = (EvSchedulerJob *)l->data; + + if (s_job->job == job) { + need_resort = (s_job->priority != priority); + break; + } + } + + G_UNLOCK (job_list); + + if (need_resort) { + GList *list; + + g_mutex_lock (job_queue_mutex); + + list = g_queue_find (job_queue[s_job->priority], s_job); + if (list) { + ev_debug_message (DEBUG_JOBS, "Moving job %s from pirority %d to %d", + EV_GET_TYPE_NAME (job), s_job->priority, priority); + g_queue_delete_link (job_queue[s_job->priority], list); + g_queue_push_tail (job_queue[priority], s_job); + g_cond_broadcast (job_queue_cond); + } + + g_mutex_unlock (job_queue_mutex); + } +} + diff --git a/libview/ev-job-scheduler.h b/libview/ev-job-scheduler.h new file mode 100644 index 00000000..b099a66f --- /dev/null +++ b/libview/ev-job-scheduler.h @@ -0,0 +1,48 @@ +/* ev-job-scheduler.h + * this file is part of evince, a mate document viewer + * + * Copyright (C) 2008 Carlos Garcia Campos <[email protected]> + * + * Evince is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * Evince is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ + +#if !defined (__EV_EVINCE_VIEW_H_INSIDE__) && !defined (EVINCE_COMPILATION) +#error "Only <evince-view.h> can be included directly." +#endif + +#ifndef EV_JOB_SCHEDULER_H +#define EV_JOB_SCHEDULER_H + +#include <glib.h> +#include "ev-jobs.h" + +G_BEGIN_DECLS + +typedef enum { + EV_JOB_PRIORITY_URGENT, /* Rendering current page range */ + EV_JOB_PRIORITY_HIGH, /* Rendering current thumbnail range */ + EV_JOB_PRIORITY_LOW, /* Rendering pages not in current range */ + EV_JOB_PRIORITY_NONE, /* Any other job: load, save, print, ... */ + EV_JOB_N_PRIORITIES +} EvJobPriority; + +void ev_job_scheduler_push_job (EvJob *job, + EvJobPriority priority); +void ev_job_scheduler_update_job (EvJob *job, + EvJobPriority priority); + +G_END_DECLS + +#endif /* EV_JOB_SCHEDULER_H */ diff --git a/libview/ev-jobs.c b/libview/ev-jobs.c new file mode 100644 index 00000000..ffb2d729 --- /dev/null +++ b/libview/ev-jobs.c @@ -0,0 +1,1577 @@ +/* this file is part of evince, a mate document viewer + * + * Copyright (C) 2008 Carlos Garcia Campos <[email protected]> + * Copyright (C) 2005 Red Hat, Inc + * + * Evince is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * Evince is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ + +#include <config.h> + +#include "ev-jobs.h" +#include "ev-document-thumbnails.h" +#include "ev-document-links.h" +#include "ev-document-images.h" +#include "ev-document-forms.h" +#include "ev-file-exporter.h" +#include "ev-document-factory.h" +#include "ev-document-misc.h" +#include "ev-file-helpers.h" +#include "ev-document-fonts.h" +#include "ev-document-security.h" +#include "ev-document-find.h" +#include "ev-document-layers.h" +#include "ev-document-print.h" +#include "ev-document-annotations.h" +#include "ev-document-attachments.h" +#include "ev-document-text.h" +#include "ev-debug.h" + +#include <errno.h> +#include <glib/gstdio.h> +#include <glib/gi18n-lib.h> +#include <unistd.h> + +static void ev_job_init (EvJob *job); +static void ev_job_class_init (EvJobClass *class); +static void ev_job_links_init (EvJobLinks *job); +static void ev_job_links_class_init (EvJobLinksClass *class); +static void ev_job_attachments_init (EvJobAttachments *job); +static void ev_job_attachments_class_init (EvJobAttachmentsClass *class); +static void ev_job_annots_init (EvJobAnnots *job); +static void ev_job_annots_class_init (EvJobAnnotsClass *class); +static void ev_job_render_init (EvJobRender *job); +static void ev_job_render_class_init (EvJobRenderClass *class); +static void ev_job_page_data_init (EvJobPageData *job); +static void ev_job_page_data_class_init (EvJobPageDataClass *class); +static void ev_job_thumbnail_init (EvJobThumbnail *job); +static void ev_job_thumbnail_class_init (EvJobThumbnailClass *class); +static void ev_job_load_init (EvJobLoad *job); +static void ev_job_load_class_init (EvJobLoadClass *class); +static void ev_job_save_init (EvJobSave *job); +static void ev_job_save_class_init (EvJobSaveClass *class); +static void ev_job_find_init (EvJobFind *job); +static void ev_job_find_class_init (EvJobFindClass *class); +static void ev_job_layers_init (EvJobLayers *job); +static void ev_job_layers_class_init (EvJobLayersClass *class); +static void ev_job_export_init (EvJobExport *job); +static void ev_job_export_class_init (EvJobExportClass *class); +static void ev_job_print_init (EvJobPrint *job); +static void ev_job_print_class_init (EvJobPrintClass *class); + +enum { + CANCELLED, + FINISHED, + LAST_SIGNAL +}; + +enum { + FONTS_UPDATED, + FONTS_LAST_SIGNAL +}; + +enum { + FIND_UPDATED, + FIND_LAST_SIGNAL +}; + +static guint job_signals[LAST_SIGNAL] = { 0 }; +static guint job_fonts_signals[FONTS_LAST_SIGNAL] = { 0 }; +static guint job_find_signals[FIND_LAST_SIGNAL] = { 0 }; + +G_DEFINE_ABSTRACT_TYPE (EvJob, ev_job, G_TYPE_OBJECT) +G_DEFINE_TYPE (EvJobLinks, ev_job_links, EV_TYPE_JOB) +G_DEFINE_TYPE (EvJobAttachments, ev_job_attachments, EV_TYPE_JOB) +G_DEFINE_TYPE (EvJobAnnots, ev_job_annots, EV_TYPE_JOB) +G_DEFINE_TYPE (EvJobRender, ev_job_render, EV_TYPE_JOB) +G_DEFINE_TYPE (EvJobPageData, ev_job_page_data, EV_TYPE_JOB) +G_DEFINE_TYPE (EvJobThumbnail, ev_job_thumbnail, EV_TYPE_JOB) +G_DEFINE_TYPE (EvJobFonts, ev_job_fonts, EV_TYPE_JOB) +G_DEFINE_TYPE (EvJobLoad, ev_job_load, EV_TYPE_JOB) +G_DEFINE_TYPE (EvJobSave, ev_job_save, EV_TYPE_JOB) +G_DEFINE_TYPE (EvJobFind, ev_job_find, EV_TYPE_JOB) +G_DEFINE_TYPE (EvJobLayers, ev_job_layers, EV_TYPE_JOB) +G_DEFINE_TYPE (EvJobExport, ev_job_export, EV_TYPE_JOB) +G_DEFINE_TYPE (EvJobPrint, ev_job_print, EV_TYPE_JOB) + +/* EvJob */ +static void +ev_job_init (EvJob *job) +{ + job->cancellable = g_cancellable_new (); +} + +static void +ev_job_dispose (GObject *object) +{ + EvJob *job; + + job = EV_JOB (object); + + if (job->document) { + g_object_unref (job->document); + job->document = NULL; + } + + if (job->cancellable) { + g_object_unref (job->cancellable); + job->cancellable = NULL; + } + + if (job->error) { + g_error_free (job->error); + job->error = NULL; + } + + (* G_OBJECT_CLASS (ev_job_parent_class)->dispose) (object); +} + +static void +ev_job_class_init (EvJobClass *class) +{ + GObjectClass *oclass; + + oclass = G_OBJECT_CLASS (class); + + oclass->dispose = ev_job_dispose; + + job_signals[CANCELLED] = + g_signal_new ("cancelled", + EV_TYPE_JOB, + G_SIGNAL_RUN_LAST, + G_STRUCT_OFFSET (EvJobClass, cancelled), + NULL, NULL, + g_cclosure_marshal_VOID__VOID, + G_TYPE_NONE, 0); + job_signals [FINISHED] = + g_signal_new ("finished", + EV_TYPE_JOB, + G_SIGNAL_RUN_FIRST, + G_STRUCT_OFFSET (EvJobClass, finished), + NULL, NULL, + g_cclosure_marshal_VOID__VOID, + G_TYPE_NONE, 0); +} + +static gboolean +emit_finished (EvJob *job) +{ + ev_debug_message (DEBUG_JOBS, "%s (%p)", EV_GET_TYPE_NAME (job), job); + + job->idle_finished_id = 0; + + if (job->cancelled) { + ev_debug_message (DEBUG_JOBS, "%s (%p) job was cancelled, do not emit finished", EV_GET_TYPE_NAME (job), job); + } else { + ev_profiler_stop (EV_PROFILE_JOBS, "%s (%p)", EV_GET_TYPE_NAME (job), job); + g_signal_emit (job, job_signals[FINISHED], 0); + } + + return FALSE; +} + +static void +ev_job_emit_finished (EvJob *job) +{ + ev_debug_message (DEBUG_JOBS, "%s (%p)", EV_GET_TYPE_NAME (job), job); + + if (g_cancellable_is_cancelled (job->cancellable)) { + ev_debug_message (DEBUG_JOBS, "%s (%p) job was cancelled, returning", EV_GET_TYPE_NAME (job), job); + return; + } + + job->finished = TRUE; + + if (job->run_mode == EV_JOB_RUN_THREAD) { + job->idle_finished_id = + g_idle_add_full (G_PRIORITY_DEFAULT_IDLE, + (GSourceFunc)emit_finished, + g_object_ref (job), + (GDestroyNotify)g_object_unref); + } else { + ev_profiler_stop (EV_PROFILE_JOBS, "%s (%p)", EV_GET_TYPE_NAME (job), job); + g_signal_emit (job, job_signals[FINISHED], 0); + } +} + +gboolean +ev_job_run (EvJob *job) +{ + EvJobClass *class = EV_JOB_GET_CLASS (job); + + return class->run (job); +} + +void +ev_job_cancel (EvJob *job) +{ + if (job->cancelled || (job->finished && job->idle_finished_id == 0)) + return; + + ev_debug_message (DEBUG_JOBS, "job %s (%p) cancelled", EV_GET_TYPE_NAME (job), job); + ev_profiler_stop (EV_PROFILE_JOBS, "%s (%p)", EV_GET_TYPE_NAME (job), job); + + /* This should never be called from a thread */ + job->cancelled = TRUE; + g_cancellable_cancel (job->cancellable); + g_signal_emit (job, job_signals[CANCELLED], 0); +} + +void +ev_job_failed (EvJob *job, + GQuark domain, + gint code, + const gchar *format, + ...) +{ + va_list args; + gchar *message; + + if (job->failed || job->finished) + return; + + ev_debug_message (DEBUG_JOBS, "job %s (%p) failed", EV_GET_TYPE_NAME (job), job); + + job->failed = TRUE; + + va_start (args, format); + message = g_strdup_vprintf (format, args); + va_end (args); + + job->error = g_error_new_literal (domain, code, message); + g_free (message); + + ev_job_emit_finished (job); +} + +void +ev_job_failed_from_error (EvJob *job, + GError *error) +{ + if (job->failed || job->finished) + return; + + ev_debug_message (DEBUG_JOBS, "job %s (%p) failed", EV_GET_TYPE_NAME (job), job); + + job->failed = TRUE; + job->error = g_error_copy (error); + + ev_job_emit_finished (job); +} + +void +ev_job_succeeded (EvJob *job) +{ + if (job->finished) + return; + + ev_debug_message (DEBUG_JOBS, "job %s (%p) succeeded", EV_GET_TYPE_NAME (job), job); + + job->failed = FALSE; + ev_job_emit_finished (job); +} + +gboolean +ev_job_is_finished (EvJob *job) +{ + return job->finished; +} + +gboolean +ev_job_is_failed (EvJob *job) +{ + return job->failed; +} + +EvJobRunMode +ev_job_get_run_mode (EvJob *job) +{ + return job->run_mode; +} + +void +ev_job_set_run_mode (EvJob *job, + EvJobRunMode run_mode) +{ + job->run_mode = run_mode; +} + +/* EvJobLinks */ +static void +ev_job_links_init (EvJobLinks *job) +{ + EV_JOB (job)->run_mode = EV_JOB_RUN_THREAD; +} + +static void +ev_job_links_dispose (GObject *object) +{ + EvJobLinks *job; + + ev_debug_message (DEBUG_JOBS, NULL); + + job = EV_JOB_LINKS (object); + + if (job->model) { + g_object_unref (job->model); + job->model = NULL; + } + + (* G_OBJECT_CLASS (ev_job_links_parent_class)->dispose) (object); +} + +static gboolean +ev_job_links_run (EvJob *job) +{ + EvJobLinks *job_links = EV_JOB_LINKS (job); + + ev_debug_message (DEBUG_JOBS, NULL); + ev_profiler_start (EV_PROFILE_JOBS, "%s (%p)", EV_GET_TYPE_NAME (job), job); + + ev_document_doc_mutex_lock (); + job_links->model = ev_document_links_get_links_model (EV_DOCUMENT_LINKS (job->document)); + ev_document_doc_mutex_unlock (); + + ev_job_succeeded (job); + + return FALSE; +} + +static void +ev_job_links_class_init (EvJobLinksClass *class) +{ + GObjectClass *oclass = G_OBJECT_CLASS (class); + EvJobClass *job_class = EV_JOB_CLASS (class); + + oclass->dispose = ev_job_links_dispose; + job_class->run = ev_job_links_run; +} + +EvJob * +ev_job_links_new (EvDocument *document) +{ + EvJob *job; + + ev_debug_message (DEBUG_JOBS, NULL); + + job = g_object_new (EV_TYPE_JOB_LINKS, NULL); + job->document = g_object_ref (document); + + return job; +} + +/* EvJobAttachments */ +static void +ev_job_attachments_init (EvJobAttachments *job) +{ + EV_JOB (job)->run_mode = EV_JOB_RUN_THREAD; +} + +static void +ev_job_attachments_dispose (GObject *object) +{ + EvJobAttachments *job; + + ev_debug_message (DEBUG_JOBS, NULL); + + job = EV_JOB_ATTACHMENTS (object); + + if (job->attachments) { + g_list_foreach (job->attachments, (GFunc)g_object_unref, NULL); + g_list_free (job->attachments); + job->attachments = NULL; + } + + (* G_OBJECT_CLASS (ev_job_attachments_parent_class)->dispose) (object); +} + +static gboolean +ev_job_attachments_run (EvJob *job) +{ + EvJobAttachments *job_attachments = EV_JOB_ATTACHMENTS (job); + + ev_debug_message (DEBUG_JOBS, NULL); + ev_profiler_start (EV_PROFILE_JOBS, "%s (%p)", EV_GET_TYPE_NAME (job), job); + + ev_document_doc_mutex_lock (); + job_attachments->attachments = + ev_document_attachments_get_attachments (EV_DOCUMENT_ATTACHMENTS (job->document)); + ev_document_doc_mutex_unlock (); + + ev_job_succeeded (job); + + return FALSE; +} + +static void +ev_job_attachments_class_init (EvJobAttachmentsClass *class) +{ + GObjectClass *oclass = G_OBJECT_CLASS (class); + EvJobClass *job_class = EV_JOB_CLASS (class); + + oclass->dispose = ev_job_attachments_dispose; + job_class->run = ev_job_attachments_run; +} + +EvJob * +ev_job_attachments_new (EvDocument *document) +{ + EvJob *job; + + ev_debug_message (DEBUG_JOBS, NULL); + + job = g_object_new (EV_TYPE_JOB_ATTACHMENTS, NULL); + job->document = g_object_ref (document); + + return job; +} + +/* EvJobAnnots */ +static void +ev_job_annots_init (EvJobAnnots *job) +{ + EV_JOB (job)->run_mode = EV_JOB_RUN_THREAD; +} + +static void +ev_job_annots_dispose (GObject *object) +{ + EvJobAnnots *job; + + ev_debug_message (DEBUG_JOBS, NULL); + + job = EV_JOB_ANNOTS (object); + + if (job->annots) { + g_list_foreach (job->annots, (GFunc)ev_mapping_list_unref, NULL); + g_list_free (job->annots); + job->annots = NULL; + } + + G_OBJECT_CLASS (ev_job_annots_parent_class)->dispose (object); +} + +static gboolean +ev_job_annots_run (EvJob *job) +{ + EvJobAnnots *job_annots = EV_JOB_ANNOTS (job); + gint i; + + ev_debug_message (DEBUG_JOBS, NULL); + ev_profiler_start (EV_PROFILE_JOBS, "%s (%p)", EV_GET_TYPE_NAME (job), job); + + ev_document_doc_mutex_lock (); + for (i = 0; i < ev_document_get_n_pages (job->document); i++) { + EvMappingList *mapping_list; + EvPage *page; + + page = ev_document_get_page (job->document, i); + mapping_list = ev_document_annotations_get_annotations (EV_DOCUMENT_ANNOTATIONS (job->document), + page); + g_object_unref (page); + + if (mapping_list) + job_annots->annots = g_list_prepend (job_annots->annots, mapping_list); + } + ev_document_doc_mutex_unlock (); + + job_annots->annots = g_list_reverse (job_annots->annots); + + ev_job_succeeded (job); + + return FALSE; +} + +static void +ev_job_annots_class_init (EvJobAnnotsClass *class) +{ + GObjectClass *oclass = G_OBJECT_CLASS (class); + EvJobClass *job_class = EV_JOB_CLASS (class); + + oclass->dispose = ev_job_annots_dispose; + job_class->run = ev_job_annots_run; +} + +EvJob * +ev_job_annots_new (EvDocument *document) +{ + EvJob *job; + + ev_debug_message (DEBUG_JOBS, NULL); + + job = g_object_new (EV_TYPE_JOB_ANNOTS, NULL); + job->document = g_object_ref (document); + + return job; +} + +/* EvJobRender */ +static void +ev_job_render_init (EvJobRender *job) +{ + EV_JOB (job)->run_mode = EV_JOB_RUN_THREAD; +} + +static void +ev_job_render_dispose (GObject *object) +{ + EvJobRender *job; + + job = EV_JOB_RENDER (object); + + ev_debug_message (DEBUG_JOBS, "page: %d (%p)", job->page, job); + + if (job->surface) { + cairo_surface_destroy (job->surface); + job->surface = NULL; + } + + if (job->selection) { + cairo_surface_destroy (job->selection); + job->selection = NULL; + } + + if (job->selection_region) { + cairo_region_destroy (job->selection_region); + job->selection_region = NULL; + } + + (* G_OBJECT_CLASS (ev_job_render_parent_class)->dispose) (object); +} + +static gboolean +ev_job_render_run (EvJob *job) +{ + EvJobRender *job_render = EV_JOB_RENDER (job); + EvPage *ev_page; + EvRenderContext *rc; + + ev_debug_message (DEBUG_JOBS, "page: %d (%p)", job_render->page, job); + ev_profiler_start (EV_PROFILE_JOBS, "%s (%p)", EV_GET_TYPE_NAME (job), job); + + ev_document_doc_mutex_lock (); + + ev_profiler_start (EV_PROFILE_JOBS, "Rendering page %d", job_render->page); + + ev_document_fc_mutex_lock (); + + ev_page = ev_document_get_page (job->document, job_render->page); + rc = ev_render_context_new (ev_page, job_render->rotation, job_render->scale); + g_object_unref (ev_page); + + job_render->surface = ev_document_render (job->document, rc); + /* If job was cancelled during the page rendering, + * we return now, so that the thread is finished ASAP + */ + if (g_cancellable_is_cancelled (job->cancellable)) { + ev_document_fc_mutex_unlock (); + ev_document_doc_mutex_unlock (); + g_object_unref (rc); + + return FALSE; + } + + if (job_render->include_selection && EV_IS_SELECTION (job->document)) { + ev_selection_render_selection (EV_SELECTION (job->document), + rc, + &(job_render->selection), + &(job_render->selection_points), + NULL, + job_render->selection_style, + &(job_render->text), &(job_render->base)); + job_render->selection_region = + ev_selection_get_selection_region (EV_SELECTION (job->document), + rc, + job_render->selection_style, + &(job_render->selection_points)); + } + + g_object_unref (rc); + + ev_document_fc_mutex_unlock (); + ev_document_doc_mutex_unlock (); + + ev_job_succeeded (job); + + return FALSE; +} + +static void +ev_job_render_class_init (EvJobRenderClass *class) +{ + GObjectClass *oclass = G_OBJECT_CLASS (class); + EvJobClass *job_class = EV_JOB_CLASS (class); + + oclass->dispose = ev_job_render_dispose; + job_class->run = ev_job_render_run; +} + +EvJob * +ev_job_render_new (EvDocument *document, + gint page, + gint rotation, + gdouble scale, + gint width, + gint height) +{ + EvJobRender *job; + + ev_debug_message (DEBUG_JOBS, "page: %d", page); + + job = g_object_new (EV_TYPE_JOB_RENDER, NULL); + + EV_JOB (job)->document = g_object_ref (document); + job->page = page; + job->rotation = rotation; + job->scale = scale; + job->target_width = width; + job->target_height = height; + + return EV_JOB (job); +} + +void +ev_job_render_set_selection_info (EvJobRender *job, + EvRectangle *selection_points, + EvSelectionStyle selection_style, + GdkColor *text, + GdkColor *base) +{ + job->include_selection = TRUE; + + job->selection_points = *selection_points; + job->selection_style = selection_style; + job->text = *text; + job->base = *base; +} + +/* EvJobPageData */ +static void +ev_job_page_data_init (EvJobPageData *job) +{ + EV_JOB (job)->run_mode = EV_JOB_RUN_THREAD; +} + +static gboolean +ev_job_page_data_run (EvJob *job) +{ + EvJobPageData *job_pd = EV_JOB_PAGE_DATA (job); + EvPage *ev_page; + + ev_debug_message (DEBUG_JOBS, "page: %d (%p)", job_pd->page, job); + ev_profiler_start (EV_PROFILE_JOBS, "%s (%p)", EV_GET_TYPE_NAME (job), job); + + ev_document_doc_mutex_lock (); + ev_page = ev_document_get_page (job->document, job_pd->page); + + if ((job_pd->flags & EV_PAGE_DATA_INCLUDE_TEXT_MAPPING) && EV_IS_DOCUMENT_TEXT (job->document)) + job_pd->text_mapping = + ev_document_text_get_text_mapping (EV_DOCUMENT_TEXT (job->document), ev_page); + if ((job_pd->flags & EV_PAGE_DATA_INCLUDE_TEXT) && EV_IS_DOCUMENT_TEXT (job->document)) + job_pd->text = + ev_document_text_get_text (EV_DOCUMENT_TEXT (job->document), ev_page); + if ((job_pd->flags & EV_PAGE_DATA_INCLUDE_TEXT_LAYOUT) && EV_IS_DOCUMENT_TEXT (job->document)) + ev_document_text_get_text_layout (EV_DOCUMENT_TEXT (job->document), + ev_page, + &(job_pd->text_layout), + &(job_pd->text_layout_length)); + if ((job_pd->flags & EV_PAGE_DATA_INCLUDE_LINKS) && EV_IS_DOCUMENT_LINKS (job->document)) + job_pd->link_mapping = + ev_document_links_get_links (EV_DOCUMENT_LINKS (job->document), ev_page); + if ((job_pd->flags & EV_PAGE_DATA_INCLUDE_FORMS) && EV_IS_DOCUMENT_FORMS (job->document)) + job_pd->form_field_mapping = + ev_document_forms_get_form_fields (EV_DOCUMENT_FORMS (job->document), + ev_page); + if ((job_pd->flags & EV_PAGE_DATA_INCLUDE_IMAGES) && EV_IS_DOCUMENT_IMAGES (job->document)) + job_pd->image_mapping = + ev_document_images_get_image_mapping (EV_DOCUMENT_IMAGES (job->document), + ev_page); + if ((job_pd->flags & EV_PAGE_DATA_INCLUDE_ANNOTS) && EV_IS_DOCUMENT_ANNOTATIONS (job->document)) + job_pd->annot_mapping = + ev_document_annotations_get_annotations (EV_DOCUMENT_ANNOTATIONS (job->document), + ev_page); + g_object_unref (ev_page); + ev_document_doc_mutex_unlock (); + + ev_job_succeeded (job); + + return FALSE; +} + +static void +ev_job_page_data_class_init (EvJobPageDataClass *class) +{ + EvJobClass *job_class = EV_JOB_CLASS (class); + + job_class->run = ev_job_page_data_run; +} + +EvJob * +ev_job_page_data_new (EvDocument *document, + gint page, + EvJobPageDataFlags flags) +{ + EvJobPageData *job; + + ev_debug_message (DEBUG_JOBS, "%d", page); + + job = g_object_new (EV_TYPE_JOB_PAGE_DATA, NULL); + + EV_JOB (job)->document = g_object_ref (document); + job->page = page; + job->flags = flags; + + return EV_JOB (job); +} + +/* EvJobThumbnail */ +static void +ev_job_thumbnail_init (EvJobThumbnail *job) +{ + EV_JOB (job)->run_mode = EV_JOB_RUN_THREAD; +} + +static void +ev_job_thumbnail_dispose (GObject *object) +{ + EvJobThumbnail *job; + + job = EV_JOB_THUMBNAIL (object); + + ev_debug_message (DEBUG_JOBS, "%d (%p)", job->page, job); + + if (job->thumbnail) { + g_object_unref (job->thumbnail); + job->thumbnail = NULL; + } + + (* G_OBJECT_CLASS (ev_job_thumbnail_parent_class)->dispose) (object); +} + +static gboolean +ev_job_thumbnail_run (EvJob *job) +{ + EvJobThumbnail *job_thumb = EV_JOB_THUMBNAIL (job); + EvRenderContext *rc; + EvPage *page; + + ev_debug_message (DEBUG_JOBS, "%d (%p)", job_thumb->page, job); + ev_profiler_start (EV_PROFILE_JOBS, "%s (%p)", EV_GET_TYPE_NAME (job), job); + + ev_document_doc_mutex_lock (); + + page = ev_document_get_page (job->document, job_thumb->page); + rc = ev_render_context_new (page, job_thumb->rotation, job_thumb->scale); + g_object_unref (page); + + job_thumb->thumbnail = ev_document_thumbnails_get_thumbnail (EV_DOCUMENT_THUMBNAILS (job->document), + rc, TRUE); + g_object_unref (rc); + ev_document_doc_mutex_unlock (); + + ev_job_succeeded (job); + + return FALSE; +} + +static void +ev_job_thumbnail_class_init (EvJobThumbnailClass *class) +{ + GObjectClass *oclass = G_OBJECT_CLASS (class); + EvJobClass *job_class = EV_JOB_CLASS (class); + + oclass->dispose = ev_job_thumbnail_dispose; + job_class->run = ev_job_thumbnail_run; +} + +EvJob * +ev_job_thumbnail_new (EvDocument *document, + gint page, + gint rotation, + gdouble scale) +{ + EvJobThumbnail *job; + + ev_debug_message (DEBUG_JOBS, "%d", page); + + job = g_object_new (EV_TYPE_JOB_THUMBNAIL, NULL); + + EV_JOB (job)->document = g_object_ref (document); + job->page = page; + job->rotation = rotation; + job->scale = scale; + + return EV_JOB (job); +} + +/* EvJobFonts */ +static void +ev_job_fonts_init (EvJobFonts *job) +{ + EV_JOB (job)->run_mode = EV_JOB_RUN_MAIN_LOOP; +} + +static gboolean +ev_job_fonts_run (EvJob *job) +{ + EvJobFonts *job_fonts = EV_JOB_FONTS (job); + EvDocumentFonts *fonts = EV_DOCUMENT_FONTS (job->document); + + ev_debug_message (DEBUG_JOBS, NULL); + + /* Do not block the main loop */ + if (!ev_document_doc_mutex_trylock ()) + return TRUE; + + if (!ev_document_fc_mutex_trylock ()) + return TRUE; + +#ifdef EV_ENABLE_DEBUG + /* We use the #ifdef in this case because of the if */ + if (ev_document_fonts_get_progress (fonts) == 0) + ev_profiler_start (EV_PROFILE_JOBS, "%s (%p)", EV_GET_TYPE_NAME (job), job); +#endif + + job_fonts->scan_completed = !ev_document_fonts_scan (fonts, 20); + g_signal_emit (job_fonts, job_fonts_signals[FONTS_UPDATED], 0, + ev_document_fonts_get_progress (fonts)); + + ev_document_fc_mutex_unlock (); + ev_document_doc_mutex_unlock (); + + if (job_fonts->scan_completed) + ev_job_succeeded (job); + + return !job_fonts->scan_completed; +} + +static void +ev_job_fonts_class_init (EvJobFontsClass *class) +{ + EvJobClass *job_class = EV_JOB_CLASS (class); + + job_class->run = ev_job_fonts_run; + + job_fonts_signals[FONTS_UPDATED] = + g_signal_new ("updated", + EV_TYPE_JOB_FONTS, + G_SIGNAL_RUN_LAST, + G_STRUCT_OFFSET (EvJobFontsClass, updated), + NULL, NULL, + g_cclosure_marshal_VOID__DOUBLE, + G_TYPE_NONE, + 1, G_TYPE_DOUBLE); +} + +EvJob * +ev_job_fonts_new (EvDocument *document) +{ + EvJobFonts *job; + + ev_debug_message (DEBUG_JOBS, NULL); + + job = g_object_new (EV_TYPE_JOB_FONTS, NULL); + + EV_JOB (job)->document = g_object_ref (document); + + return EV_JOB (job); +} + +/* EvJobLoad */ +static void +ev_job_load_init (EvJobLoad *job) +{ + EV_JOB (job)->run_mode = EV_JOB_RUN_THREAD; +} + +static void +ev_job_load_dispose (GObject *object) +{ + EvJobLoad *job = EV_JOB_LOAD (object); + + ev_debug_message (DEBUG_JOBS, "%s", job->uri); + + if (job->uri) { + g_free (job->uri); + job->uri = NULL; + } + + if (job->password) { + g_free (job->password); + job->password = NULL; + } + + (* G_OBJECT_CLASS (ev_job_load_parent_class)->dispose) (object); +} + +static gboolean +ev_job_load_run (EvJob *job) +{ + EvJobLoad *job_load = EV_JOB_LOAD (job); + GError *error = NULL; + + ev_debug_message (DEBUG_JOBS, "%s", job_load->uri); + ev_profiler_start (EV_PROFILE_JOBS, "%s (%p)", EV_GET_TYPE_NAME (job), job); + + ev_document_fc_mutex_lock (); + + /* This job may already have a document even if the job didn't complete + because, e.g., a password is required - if so, just reload rather than + creating a new instance */ + if (job->document) { + const gchar *uncompressed_uri; + + if (job_load->password) { + ev_document_security_set_password (EV_DOCUMENT_SECURITY (job->document), + job_load->password); + } + + job->failed = FALSE; + job->finished = FALSE; + g_clear_error (&job->error); + + uncompressed_uri = g_object_get_data (G_OBJECT (job->document), + "uri-uncompressed"); + ev_document_load (job->document, + uncompressed_uri ? uncompressed_uri : job_load->uri, + &error); + } else { + job->document = ev_document_factory_get_document (job_load->uri, + &error); + } + + ev_document_fc_mutex_unlock (); + + if (error) { + ev_job_failed_from_error (job, error); + g_error_free (error); + } else { + ev_job_succeeded (job); + } + + return FALSE; +} + +static void +ev_job_load_class_init (EvJobLoadClass *class) +{ + GObjectClass *oclass = G_OBJECT_CLASS (class); + EvJobClass *job_class = EV_JOB_CLASS (class); + + oclass->dispose = ev_job_load_dispose; + job_class->run = ev_job_load_run; +} + +EvJob * +ev_job_load_new (const gchar *uri) +{ + EvJobLoad *job; + + ev_debug_message (DEBUG_JOBS, "%s", uri); + + job = g_object_new (EV_TYPE_JOB_LOAD, NULL); + job->uri = g_strdup (uri); + + return EV_JOB (job); +} + +void +ev_job_load_set_uri (EvJobLoad *job, const gchar *uri) +{ + ev_debug_message (DEBUG_JOBS, "%s", uri); + + if (job->uri) + g_free (job->uri); + job->uri = g_strdup (uri); +} + +void +ev_job_load_set_password (EvJobLoad *job, const gchar *password) +{ + ev_debug_message (DEBUG_JOBS, NULL); + + if (job->password) + g_free (job->password); + job->password = password ? g_strdup (password) : NULL; +} + +/* EvJobSave */ +static void +ev_job_save_init (EvJobSave *job) +{ + EV_JOB (job)->run_mode = EV_JOB_RUN_THREAD; +} + +static void +ev_job_save_dispose (GObject *object) +{ + EvJobSave *job = EV_JOB_SAVE (object); + + ev_debug_message (DEBUG_JOBS, "%s", job->uri); + + if (job->uri) { + g_free (job->uri); + job->uri = NULL; + } + + if (job->document_uri) { + g_free (job->document_uri); + job->document_uri = NULL; + } + + (* G_OBJECT_CLASS (ev_job_save_parent_class)->dispose) (object); +} + +static gboolean +ev_job_save_run (EvJob *job) +{ + EvJobSave *job_save = EV_JOB_SAVE (job); + gint fd; + gchar *tmp_filename = NULL; + gchar *local_uri; + GError *error = NULL; + + ev_debug_message (DEBUG_JOBS, "uri: %s, document_uri: %s", job_save->uri, job_save->document_uri); + ev_profiler_start (EV_PROFILE_JOBS, "%s (%p)", EV_GET_TYPE_NAME (job), job); + + fd = ev_mkstemp ("saveacopy.XXXXXX", &tmp_filename, &error); + if (fd == -1) { + ev_job_failed_from_error (job, error); + g_error_free (error); + + return FALSE; + } + + ev_document_doc_mutex_lock (); + + /* Save document to temp filename */ + local_uri = g_filename_to_uri (tmp_filename, NULL, &error); + if (local_uri != NULL) { + ev_document_save (job->document, local_uri, &error); + } + + close (fd); + + ev_document_doc_mutex_unlock (); + + if (error) { + g_free (local_uri); + ev_job_failed_from_error (job, error); + g_error_free (error); + + return FALSE; + } + + /* If original document was compressed, + * compress it again before saving + */ + if (g_object_get_data (G_OBJECT (job->document), "uri-uncompressed")) { + EvCompressionType ctype = EV_COMPRESSION_NONE; + const gchar *ext; + gchar *uri_comp; + + ext = g_strrstr (job_save->document_uri, ".gz"); + if (ext && g_ascii_strcasecmp (ext, ".gz") == 0) + ctype = EV_COMPRESSION_GZIP; + + ext = g_strrstr (job_save->document_uri, ".bz2"); + if (ext && g_ascii_strcasecmp (ext, ".bz2") == 0) + ctype = EV_COMPRESSION_BZIP2; + + uri_comp = ev_file_compress (local_uri, ctype, &error); + g_free (local_uri); + g_unlink (tmp_filename); + + if (!uri_comp || error) { + local_uri = NULL; + } else { + local_uri = uri_comp; + } + } + + g_free (tmp_filename); + + if (error) { + g_free (local_uri); + ev_job_failed_from_error (job, error); + g_error_free (error); + + return FALSE; + } + + if (!local_uri) + return FALSE; + + ev_xfer_uri_simple (local_uri, job_save->uri, &error); + ev_tmp_uri_unlink (local_uri); + + if (error) { + ev_job_failed_from_error (job, error); + g_error_free (error); + } else { + ev_job_succeeded (job); + } + + return FALSE; +} + +static void +ev_job_save_class_init (EvJobSaveClass *class) +{ + GObjectClass *oclass = G_OBJECT_CLASS (class); + EvJobClass *job_class = EV_JOB_CLASS (class); + + oclass->dispose = ev_job_save_dispose; + job_class->run = ev_job_save_run; +} + +EvJob * +ev_job_save_new (EvDocument *document, + const gchar *uri, + const gchar *document_uri) +{ + EvJobSave *job; + + ev_debug_message (DEBUG_JOBS, "uri: %s, document_uri: %s", uri, document_uri); + + job = g_object_new (EV_TYPE_JOB_SAVE, NULL); + + EV_JOB (job)->document = g_object_ref (document); + job->uri = g_strdup (uri); + job->document_uri = g_strdup (document_uri); + + return EV_JOB (job); +} + +/* EvJobFind */ +static void +ev_job_find_init (EvJobFind *job) +{ + EV_JOB (job)->run_mode = EV_JOB_RUN_MAIN_LOOP; +} + +static void +ev_job_find_dispose (GObject *object) +{ + EvJobFind *job = EV_JOB_FIND (object); + + ev_debug_message (DEBUG_JOBS, NULL); + + if (job->text) { + g_free (job->text); + job->text = NULL; + } + + if (job->pages) { + gint i; + + for (i = 0; i < job->n_pages; i++) { + g_list_foreach (job->pages[i], (GFunc)ev_rectangle_free, NULL); + g_list_free (job->pages[i]); + } + + g_free (job->pages); + job->pages = NULL; + } + + (* G_OBJECT_CLASS (ev_job_find_parent_class)->dispose) (object); +} + +static gboolean +ev_job_find_run (EvJob *job) +{ + EvJobFind *job_find = EV_JOB_FIND (job); + EvDocumentFind *find = EV_DOCUMENT_FIND (job->document); + EvPage *ev_page; + GList *matches; + + ev_debug_message (DEBUG_JOBS, NULL); + + /* Do not block the main loop */ + if (!ev_document_doc_mutex_trylock ()) + return TRUE; + +#ifdef EV_ENABLE_DEBUG + /* We use the #ifdef in this case because of the if */ + if (job_find->current_page == job_find->start_page) + ev_profiler_start (EV_PROFILE_JOBS, "%s (%p)", EV_GET_TYPE_NAME (job), job); +#endif + + ev_page = ev_document_get_page (job->document, job_find->current_page); + matches = ev_document_find_find_text (find, ev_page, job_find->text, + job_find->case_sensitive); + g_object_unref (ev_page); + + ev_document_doc_mutex_unlock (); + + if (!job_find->has_results) + job_find->has_results = (matches != NULL); + + job_find->pages[job_find->current_page] = matches; + g_signal_emit (job_find, job_find_signals[FIND_UPDATED], 0, job_find->current_page); + + job_find->current_page = (job_find->current_page + 1) % job_find->n_pages; + if (job_find->current_page == job_find->start_page) { + ev_job_succeeded (job); + + return FALSE; + } + + return TRUE; +} + +static void +ev_job_find_class_init (EvJobFindClass *class) +{ + EvJobClass *job_class = EV_JOB_CLASS (class); + GObjectClass *gobject_class = G_OBJECT_CLASS (class); + + job_class->run = ev_job_find_run; + gobject_class->dispose = ev_job_find_dispose; + + job_find_signals[FIND_UPDATED] = + g_signal_new ("updated", + EV_TYPE_JOB_FIND, + G_SIGNAL_RUN_LAST, + G_STRUCT_OFFSET (EvJobFindClass, updated), + NULL, NULL, + g_cclosure_marshal_VOID__INT, + G_TYPE_NONE, + 1, G_TYPE_INT); +} + +EvJob * +ev_job_find_new (EvDocument *document, + gint start_page, + gint n_pages, + const gchar *text, + gboolean case_sensitive) +{ + EvJobFind *job; + + ev_debug_message (DEBUG_JOBS, NULL); + + job = g_object_new (EV_TYPE_JOB_FIND, NULL); + + EV_JOB (job)->document = g_object_ref (document); + job->start_page = start_page; + job->current_page = start_page; + job->n_pages = n_pages; + job->pages = g_new0 (GList *, n_pages); + job->text = g_strdup (text); + job->case_sensitive = case_sensitive; + job->has_results = FALSE; + + return EV_JOB (job); +} + +gint +ev_job_find_get_n_results (EvJobFind *job, + gint page) +{ + return g_list_length (job->pages[page]); +} + +gdouble +ev_job_find_get_progress (EvJobFind *job) +{ + gint pages_done; + + if (ev_job_is_finished (EV_JOB (job))) + return 1.0; + + if (job->current_page > job->start_page) { + pages_done = job->current_page - job->start_page + 1; + } else if (job->current_page == job->start_page) { + pages_done = job->n_pages; + } else { + pages_done = job->n_pages - job->start_page + job->current_page; + } + + return pages_done / (gdouble) job->n_pages; +} + +gboolean +ev_job_find_has_results (EvJobFind *job) +{ + return job->has_results; +} + +GList ** +ev_job_find_get_results (EvJobFind *job) +{ + return job->pages; +} + +/* EvJobLayers */ +static void +ev_job_layers_init (EvJobLayers *job) +{ + EV_JOB (job)->run_mode = EV_JOB_RUN_THREAD; +} + +static void +ev_job_layers_dispose (GObject *object) +{ + EvJobLayers *job; + + ev_debug_message (DEBUG_JOBS, NULL); + + job = EV_JOB_LAYERS (object); + + if (job->model) { + g_object_unref (job->model); + job->model = NULL; + } + + (* G_OBJECT_CLASS (ev_job_layers_parent_class)->dispose) (object); +} + +static gboolean +ev_job_layers_run (EvJob *job) +{ + EvJobLayers *job_layers = EV_JOB_LAYERS (job); + + ev_debug_message (DEBUG_JOBS, NULL); + ev_profiler_start (EV_PROFILE_JOBS, "%s (%p)", EV_GET_TYPE_NAME (job), job); + + ev_document_doc_mutex_lock (); + job_layers->model = ev_document_layers_get_layers (EV_DOCUMENT_LAYERS (job->document)); + ev_document_doc_mutex_unlock (); + + ev_job_succeeded (job); + + return FALSE; +} + +static void +ev_job_layers_class_init (EvJobLayersClass *class) +{ + GObjectClass *oclass = G_OBJECT_CLASS (class); + EvJobClass *job_class = EV_JOB_CLASS (class); + + oclass->dispose = ev_job_layers_dispose; + job_class->run = ev_job_layers_run; +} + +EvJob * +ev_job_layers_new (EvDocument *document) +{ + EvJob *job; + + ev_debug_message (DEBUG_JOBS, NULL); + + job = g_object_new (EV_TYPE_JOB_LAYERS, NULL); + job->document = g_object_ref (document); + + return job; +} + +/* EvJobExport */ +static void +ev_job_export_init (EvJobExport *job) +{ + EV_JOB (job)->run_mode = EV_JOB_RUN_THREAD; + job->page = -1; +} + +static void +ev_job_export_dispose (GObject *object) +{ + EvJobExport *job; + + ev_debug_message (DEBUG_JOBS, NULL); + + job = EV_JOB_EXPORT (object); + + if (job->rc) { + g_object_unref (job->rc); + job->rc = NULL; + } + + (* G_OBJECT_CLASS (ev_job_export_parent_class)->dispose) (object); +} + +static gboolean +ev_job_export_run (EvJob *job) +{ + EvJobExport *job_export = EV_JOB_EXPORT (job); + EvPage *ev_page; + + g_assert (job_export->page != -1); + + ev_debug_message (DEBUG_JOBS, NULL); + ev_profiler_start (EV_PROFILE_JOBS, "%s (%p)", EV_GET_TYPE_NAME (job), job); + + ev_document_doc_mutex_lock (); + + ev_page = ev_document_get_page (job->document, job_export->page); + if (job_export->rc) { + job->failed = FALSE; + job->finished = FALSE; + g_clear_error (&job->error); + + ev_render_context_set_page (job_export->rc, ev_page); + } else { + job_export->rc = ev_render_context_new (ev_page, 0, 1.0); + } + g_object_unref (ev_page); + + ev_file_exporter_do_page (EV_FILE_EXPORTER (job->document), job_export->rc); + + ev_document_doc_mutex_unlock (); + + ev_job_succeeded (job); + + return FALSE; +} + +static void +ev_job_export_class_init (EvJobExportClass *class) +{ + GObjectClass *oclass = G_OBJECT_CLASS (class); + EvJobClass *job_class = EV_JOB_CLASS (class); + + oclass->dispose = ev_job_export_dispose; + job_class->run = ev_job_export_run; +} + +EvJob * +ev_job_export_new (EvDocument *document) +{ + EvJob *job; + + ev_debug_message (DEBUG_JOBS, NULL); + + job = g_object_new (EV_TYPE_JOB_EXPORT, NULL); + job->document = g_object_ref (document); + + return job; +} + +void +ev_job_export_set_page (EvJobExport *job, + gint page) +{ + job->page = page; +} + +/* EvJobPrint */ +static void +ev_job_print_init (EvJobPrint *job) +{ + EV_JOB (job)->run_mode = EV_JOB_RUN_THREAD; + job->page = -1; +} + +static void +ev_job_print_dispose (GObject *object) +{ + EvJobPrint *job; + + ev_debug_message (DEBUG_JOBS, NULL); + + job = EV_JOB_PRINT (object); + + if (job->cr) { + cairo_destroy (job->cr); + job->cr = NULL; + } + + (* G_OBJECT_CLASS (ev_job_print_parent_class)->dispose) (object); +} + +static gboolean +ev_job_print_run (EvJob *job) +{ + EvJobPrint *job_print = EV_JOB_PRINT (job); + EvPage *ev_page; + cairo_status_t cr_status; + + g_assert (job_print->page != -1); + g_assert (job_print->cr != NULL); + + ev_debug_message (DEBUG_JOBS, NULL); + ev_profiler_start (EV_PROFILE_JOBS, "%s (%p)", EV_GET_TYPE_NAME (job), job); + + job->failed = FALSE; + job->finished = FALSE; + g_clear_error (&job->error); + + ev_document_doc_mutex_lock (); + + ev_page = ev_document_get_page (job->document, job_print->page); + ev_document_print_print_page (EV_DOCUMENT_PRINT (job->document), + ev_page, job_print->cr); + g_object_unref (ev_page); + + ev_document_doc_mutex_unlock (); + + cr_status = cairo_status (job_print->cr); + if (cr_status == CAIRO_STATUS_SUCCESS) { + ev_job_succeeded (job); + } else { + ev_job_failed (job, + GTK_PRINT_ERROR, + GTK_PRINT_ERROR_GENERAL, + _("Failed to print page %d: %s"), + job_print->page, + cairo_status_to_string (cr_status)); + } + + return FALSE; +} + +static void +ev_job_print_class_init (EvJobPrintClass *class) +{ + GObjectClass *oclass = G_OBJECT_CLASS (class); + EvJobClass *job_class = EV_JOB_CLASS (class); + + oclass->dispose = ev_job_print_dispose; + job_class->run = ev_job_print_run; +} + +EvJob * +ev_job_print_new (EvDocument *document) +{ + EvJob *job; + + ev_debug_message (DEBUG_JOBS, NULL); + + job = g_object_new (EV_TYPE_JOB_PRINT, NULL); + job->document = g_object_ref (document); + + return job; +} + +void +ev_job_print_set_page (EvJobPrint *job, + gint page) +{ + job->page = page; +} + +void +ev_job_print_set_cairo (EvJobPrint *job, + cairo_t *cr) +{ + if (job->cr == cr) + return; + + if (job->cr) + cairo_destroy (job->cr); + job->cr = cr ? cairo_reference (cr) : NULL; +} diff --git a/libview/ev-jobs.h b/libview/ev-jobs.h new file mode 100644 index 00000000..14b47f78 --- /dev/null +++ b/libview/ev-jobs.h @@ -0,0 +1,502 @@ +/* this file is part of evince, a mate document viewer + * + * Copyright (C) 2008 Carlos Garcia Campos <[email protected]> + * Copyright (C) 2005 Red Hat, Inc + * + * Evince is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * Evince is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ + +#if !defined (__EV_EVINCE_VIEW_H_INSIDE__) && !defined (EVINCE_COMPILATION) +#error "Only <evince-view.h> can be included directly." +#endif + +#ifndef __EV_JOBS_H__ +#define __EV_JOBS_H__ + +#include <gio/gio.h> +#include <gtk/gtk.h> +#include <cairo.h> + +#include <evince-document.h> + +G_BEGIN_DECLS + +typedef struct _EvJob EvJob; +typedef struct _EvJobClass EvJobClass; + +typedef struct _EvJobRender EvJobRender; +typedef struct _EvJobRenderClass EvJobRenderClass; + +typedef struct _EvJobPageData EvJobPageData; +typedef struct _EvJobPageDataClass EvJobPageDataClass; + +typedef struct _EvJobThumbnail EvJobThumbnail; +typedef struct _EvJobThumbnailClass EvJobThumbnailClass; + +typedef struct _EvJobLinks EvJobLinks; +typedef struct _EvJobLinksClass EvJobLinksClass; + +typedef struct _EvJobAttachments EvJobAttachments; +typedef struct _EvJobAttachmentsClass EvJobAttachmentsClass; + +typedef struct _EvJobAnnots EvJobAnnots; +typedef struct _EvJobAnnotsClass EvJobAnnotsClass; + +typedef struct _EvJobFonts EvJobFonts; +typedef struct _EvJobFontsClass EvJobFontsClass; + +typedef struct _EvJobLoad EvJobLoad; +typedef struct _EvJobLoadClass EvJobLoadClass; + +typedef struct _EvJobSave EvJobSave; +typedef struct _EvJobSaveClass EvJobSaveClass; + +typedef struct _EvJobFind EvJobFind; +typedef struct _EvJobFindClass EvJobFindClass; + +typedef struct _EvJobLayers EvJobLayers; +typedef struct _EvJobLayersClass EvJobLayersClass; + +typedef struct _EvJobExport EvJobExport; +typedef struct _EvJobExportClass EvJobExportClass; + +typedef struct _EvJobPrint EvJobPrint; +typedef struct _EvJobPrintClass EvJobPrintClass; + +#define EV_TYPE_JOB (ev_job_get_type()) +#define EV_JOB(object) (G_TYPE_CHECK_INSTANCE_CAST((object), EV_TYPE_JOB, EvJob)) +#define EV_JOB_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST((klass), EV_TYPE_JOB, EvJobClass)) +#define EV_IS_JOB(object) (G_TYPE_CHECK_INSTANCE_TYPE((object), EV_TYPE_JOB)) +#define EV_JOB_GET_CLASS(object) (G_TYPE_INSTANCE_GET_CLASS((object), EV_TYPE_JOB, EvJobClass)) + +#define EV_TYPE_JOB_LINKS (ev_job_links_get_type()) +#define EV_JOB_LINKS(object) (G_TYPE_CHECK_INSTANCE_CAST((object), EV_TYPE_JOB_LINKS, EvJobLinks)) +#define EV_JOB_LINKS_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST((klass), EV_TYPE_JOB_LINKS, EvJobLinksClass)) +#define EV_IS_JOB_LINKS(object) (G_TYPE_CHECK_INSTANCE_TYPE((object), EV_TYPE_JOB_LINKS)) + +#define EV_TYPE_JOB_ATTACHMENTS (ev_job_attachments_get_type()) +#define EV_JOB_ATTACHMENTS(object) (G_TYPE_CHECK_INSTANCE_CAST((object), EV_TYPE_JOB_ATTACHMENTS, EvJobAttachments)) +#define EV_JOB_ATTACHMENTS_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST((klass), EV_TYPE_JOB_ATTACHMENTS, EvJobAttachmentsClass)) +#define EV_IS_JOB_ATTACHMENTS(object) (G_TYPE_CHECK_INSTANCE_TYPE((object), EV_TYPE_JOB_ATTACHMENTS)) + +#define EV_TYPE_JOB_ANNOTS (ev_job_annots_get_type()) +#define EV_JOB_ANNOTS(object) (G_TYPE_CHECK_INSTANCE_CAST((object), EV_TYPE_JOB_ANNOTS, EvJobAnnots)) +#define EV_JOB_ANNOTS_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST((klass), EV_TYPE_JOB_ANNOTS, EvJobAnnotsClass)) +#define EV_IS_JOB_ANNOTS(object) (G_TYPE_CHECK_INSTANCE_TYPE((object), EV_TYPE_JOB_ANNOTS)) + +#define EV_TYPE_JOB_RENDER (ev_job_render_get_type()) +#define EV_JOB_RENDER(object) (G_TYPE_CHECK_INSTANCE_CAST((object), EV_TYPE_JOB_RENDER, EvJobRender)) +#define EV_JOB_RENDER_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST((klass), EV_TYPE_JOB_RENDER, EvJobRenderClass)) +#define EV_IS_JOB_RENDER(object) (G_TYPE_CHECK_INSTANCE_TYPE((object), EV_TYPE_JOB_RENDER)) + +#define EV_TYPE_JOB_PAGE_DATA (ev_job_page_data_get_type()) +#define EV_JOB_PAGE_DATA(object) (G_TYPE_CHECK_INSTANCE_CAST((object), EV_TYPE_JOB_PAGE_DATA, EvJobPageData)) +#define EV_JOB_PAGE_DATA_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST((klass), EV_TYPE_JOB_PAGE_DATA, EvJobPageDataClass)) +#define EV_IS_JOB_PAGE_DATA(object) (G_TYPE_CHECK_INSTANCE_TYPE((object), EV_TYPE_JOB_PAGE_DATA)) + +#define EV_TYPE_JOB_THUMBNAIL (ev_job_thumbnail_get_type()) +#define EV_JOB_THUMBNAIL(object) (G_TYPE_CHECK_INSTANCE_CAST((object), EV_TYPE_JOB_THUMBNAIL, EvJobThumbnail)) +#define EV_JOB_THUMBNAIL_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST((klass), EV_TYPE_JOB_THUMBNAIL, EvJobThumbnailClass)) +#define EV_IS_JOB_THUMBNAIL(object) (G_TYPE_CHECK_INSTANCE_TYPE((object), EV_TYPE_JOB_THUMBNAIL)) + +#define EV_TYPE_JOB_FONTS (ev_job_fonts_get_type()) +#define EV_JOB_FONTS(object) (G_TYPE_CHECK_INSTANCE_CAST((object), EV_TYPE_JOB_FONTS, EvJobFonts)) +#define EV_JOB_FONTS_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST((klass), EV_TYPE_JOB_FONTS, EvJobFontsClass)) +#define EV_IS_JOB_FONTS(object) (G_TYPE_CHECK_INSTANCE_TYPE((object), EV_TYPE_JOB_FONTS)) + +#define EV_TYPE_JOB_LOAD (ev_job_load_get_type()) +#define EV_JOB_LOAD(object) (G_TYPE_CHECK_INSTANCE_CAST((object), EV_TYPE_JOB_LOAD, EvJobLoad)) +#define EV_JOB_LOAD_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST((klass), EV_TYPE_JOB_LOAD, EvJobLoadClass)) +#define EV_IS_JOB_LOAD(object) (G_TYPE_CHECK_INSTANCE_TYPE((object), EV_TYPE_JOB_LOAD)) + +#define EV_TYPE_JOB_SAVE (ev_job_save_get_type()) +#define EV_JOB_SAVE(object) (G_TYPE_CHECK_INSTANCE_CAST((object), EV_TYPE_JOB_SAVE, EvJobSave)) +#define EV_JOB_SAVE_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST((klass), EV_TYPE_JOB_SAVE, EvJobSaveClass)) +#define EV_IS_JOB_SAVE(object) (G_TYPE_CHECK_INSTANCE_TYPE((object), EV_TYPE_JOB_SAVE)) + +#define EV_TYPE_JOB_FIND (ev_job_find_get_type()) +#define EV_JOB_FIND(object) (G_TYPE_CHECK_INSTANCE_CAST((object), EV_TYPE_JOB_FIND, EvJobFind)) +#define EV_JOB_FIND_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST((klass), EV_TYPE_JOB_FIND, EvJobFindClass)) +#define EV_IS_JOB_FIND(object) (G_TYPE_CHECK_INSTANCE_TYPE((object), EV_TYPE_JOB_FIND)) + +#define EV_TYPE_JOB_LAYERS (ev_job_layers_get_type()) +#define EV_JOB_LAYERS(object) (G_TYPE_CHECK_INSTANCE_CAST((object), EV_TYPE_JOB_LAYERS, EvJobLayers)) +#define EV_JOB_LAYERS_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST((klass), EV_TYPE_JOB_LAYERS, EvJobLayersClass)) +#define EV_IS_JOB_LAYERS(object) (G_TYPE_CHECK_INSTANCE_TYPE((object), EV_TYPE_JOB_LAYERS)) + +#define EV_TYPE_JOB_EXPORT (ev_job_export_get_type()) +#define EV_JOB_EXPORT(object) (G_TYPE_CHECK_INSTANCE_CAST((object), EV_TYPE_JOB_EXPORT, EvJobExport)) +#define EV_JOB_EXPORT_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST((klass), EV_TYPE_JOB_EXPORT, EvJobExportClass)) +#define EV_IS_JOB_EXPORT(object) (G_TYPE_CHECK_INSTANCE_TYPE((object), EV_TYPE_JOB_EXPORT)) + +#define EV_TYPE_JOB_PRINT (ev_job_print_get_type()) +#define EV_JOB_PRINT(object) (G_TYPE_CHECK_INSTANCE_CAST((object), EV_TYPE_JOB_PRINT, EvJobPrint)) +#define EV_JOB_PRINT_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST((klass), EV_TYPE_JOB_PRINT, EvJobPrintClass)) +#define EV_IS_JOB_PRINT(object) (G_TYPE_CHECK_INSTANCE_TYPE((object), EV_TYPE_JOB_PRINT)) + +typedef enum { + EV_JOB_RUN_THREAD, + EV_JOB_RUN_MAIN_LOOP +} EvJobRunMode; + +struct _EvJob +{ + GObject parent; + + EvDocument *document; + + EvJobRunMode run_mode; + + guint cancelled : 1; + guint finished : 1; + guint failed : 1; + + GError *error; + GCancellable *cancellable; + + guint idle_finished_id; + guint idle_cancelled_id; +}; + +struct _EvJobClass +{ + GObjectClass parent_class; + + gboolean (*run) (EvJob *job); + + /* Signals */ + void (* cancelled) (EvJob *job); + void (* finished) (EvJob *job); +}; + +struct _EvJobLinks +{ + EvJob parent; + + GtkTreeModel *model; +}; + +struct _EvJobLinksClass +{ + EvJobClass parent_class; +}; + +struct _EvJobAttachments +{ + EvJob parent; + + GList *attachments; +}; + +struct _EvJobAttachmentsClass +{ + EvJobClass parent_class; +}; + +struct _EvJobAnnots +{ + EvJob parent; + + GList *annots; +}; + +struct _EvJobAnnotsClass +{ + EvJobClass parent_class; +}; + +struct _EvJobRender +{ + EvJob parent; + + gint page; + gint rotation; + gdouble scale; + + gboolean page_ready; + gint target_width; + gint target_height; + cairo_surface_t *surface; + + gboolean include_selection; + cairo_surface_t *selection; + cairo_region_t *selection_region; + EvRectangle selection_points; + EvSelectionStyle selection_style; + GdkColor base; + GdkColor text; +}; + +struct _EvJobRenderClass +{ + EvJobClass parent_class; +}; + +typedef enum { + EV_PAGE_DATA_INCLUDE_NONE = 0, + EV_PAGE_DATA_INCLUDE_LINKS = 1 << 0, + EV_PAGE_DATA_INCLUDE_TEXT = 1 << 1, + EV_PAGE_DATA_INCLUDE_TEXT_MAPPING = 1 << 2, + EV_PAGE_DATA_INCLUDE_TEXT_LAYOUT = 1 << 3, + EV_PAGE_DATA_INCLUDE_IMAGES = 1 << 4, + EV_PAGE_DATA_INCLUDE_FORMS = 1 << 5, + EV_PAGE_DATA_INCLUDE_ANNOTS = 1 << 6, + EV_PAGE_DATA_INCLUDE_ALL = (1 << 7) - 1 +} EvJobPageDataFlags; + +struct _EvJobPageData +{ + EvJob parent; + + gint page; + EvJobPageDataFlags flags; + + EvMappingList *link_mapping; + EvMappingList *image_mapping; + EvMappingList *form_field_mapping; + EvMappingList *annot_mapping; + cairo_region_t *text_mapping; + gchar *text; + EvRectangle *text_layout; + guint text_layout_length; +}; + +struct _EvJobPageDataClass +{ + EvJobClass parent_class; +}; + +struct _EvJobThumbnail +{ + EvJob parent; + + gint page; + gint rotation; + gdouble scale; + + GdkPixbuf *thumbnail; +}; + +struct _EvJobThumbnailClass +{ + EvJobClass parent_class; +}; + +struct _EvJobFonts +{ + EvJob parent; + gboolean scan_completed; +}; + +struct _EvJobFontsClass +{ + EvJobClass parent_class; + + /* Signals */ + void (* updated) (EvJobFonts *job, + gdouble progress); +}; + +struct _EvJobLoad +{ + EvJob parent; + + gchar *uri; + gchar *password; +}; + +struct _EvJobLoadClass +{ + EvJobClass parent_class; +}; + +struct _EvJobSave +{ + EvJob parent; + + gchar *uri; + gchar *document_uri; +}; + +struct _EvJobSaveClass +{ + EvJobClass parent_class; +}; + +struct _EvJobFind +{ + EvJob parent; + + gint start_page; + gint current_page; + gint n_pages; + GList **pages; + gchar *text; + gboolean case_sensitive; + gboolean has_results; +}; + +struct _EvJobFindClass +{ + EvJobClass parent_class; + + /* Signals */ + void (* updated) (EvJobFind *job, + gint page); +}; + +struct _EvJobLayers +{ + EvJob parent; + + GtkTreeModel *model; +}; + +struct _EvJobLayersClass +{ + EvJobClass parent_class; +}; + +struct _EvJobExport +{ + EvJob parent; + + gint page; + EvRenderContext *rc; +}; + +struct _EvJobExportClass +{ + EvJobClass parent_class; +}; + +struct _EvJobPrint +{ + EvJob parent; + + gint page; + cairo_t *cr; +}; + +struct _EvJobPrintClass +{ + EvJobClass parent_class; +}; + +/* Base job class */ +GType ev_job_get_type (void) G_GNUC_CONST; +gboolean ev_job_run (EvJob *job); +void ev_job_cancel (EvJob *job); +void ev_job_failed (EvJob *job, + GQuark domain, + gint code, + const gchar *format, + ...); +void ev_job_failed_from_error (EvJob *job, + GError *error); +void ev_job_succeeded (EvJob *job); +gboolean ev_job_is_finished (EvJob *job); +gboolean ev_job_is_failed (EvJob *job); +EvJobRunMode ev_job_get_run_mode (EvJob *job); +void ev_job_set_run_mode (EvJob *job, + EvJobRunMode run_mode); + +/* EvJobLinks */ +GType ev_job_links_get_type (void) G_GNUC_CONST; +EvJob *ev_job_links_new (EvDocument *document); + +/* EvJobAttachments */ +GType ev_job_attachments_get_type (void) G_GNUC_CONST; +EvJob *ev_job_attachments_new (EvDocument *document); + +/* EvJobAnnots */ +GType ev_job_annots_get_type (void) G_GNUC_CONST; +EvJob *ev_job_annots_new (EvDocument *document); + +/* EvJobRender */ +GType ev_job_render_get_type (void) G_GNUC_CONST; +EvJob *ev_job_render_new (EvDocument *document, + gint page, + gint rotation, + gdouble scale, + gint width, + gint height); +void ev_job_render_set_selection_info (EvJobRender *job, + EvRectangle *selection_points, + EvSelectionStyle selection_style, + GdkColor *text, + GdkColor *base); +/* EvJobPageData */ +GType ev_job_page_data_get_type (void) G_GNUC_CONST; +EvJob *ev_job_page_data_new (EvDocument *document, + gint page, + EvJobPageDataFlags flags); + +/* EvJobThumbnail */ +GType ev_job_thumbnail_get_type (void) G_GNUC_CONST; +EvJob *ev_job_thumbnail_new (EvDocument *document, + gint page, + gint rotation, + gdouble scale); +/* EvJobFonts */ +GType ev_job_fonts_get_type (void) G_GNUC_CONST; +EvJob *ev_job_fonts_new (EvDocument *document); + +/* EvJobLoad */ +GType ev_job_load_get_type (void) G_GNUC_CONST; +EvJob *ev_job_load_new (const gchar *uri); +void ev_job_load_set_uri (EvJobLoad *load, + const gchar *uri); +void ev_job_load_set_password (EvJobLoad *job, + const gchar *password); + +/* EvJobSave */ +GType ev_job_save_get_type (void) G_GNUC_CONST; +EvJob *ev_job_save_new (EvDocument *document, + const gchar *uri, + const gchar *document_uri); +/* EvJobFind */ +GType ev_job_find_get_type (void) G_GNUC_CONST; +EvJob *ev_job_find_new (EvDocument *document, + gint start_page, + gint n_pages, + const gchar *text, + gboolean case_sensitive); +gint ev_job_find_get_n_results (EvJobFind *job, + gint pages); +gdouble ev_job_find_get_progress (EvJobFind *job); +gboolean ev_job_find_has_results (EvJobFind *job); +GList **ev_job_find_get_results (EvJobFind *job); + +/* EvJobLayers */ +GType ev_job_layers_get_type (void) G_GNUC_CONST; +EvJob *ev_job_layers_new (EvDocument *document); + +/* EvJobExport */ +GType ev_job_export_get_type (void) G_GNUC_CONST; +EvJob *ev_job_export_new (EvDocument *document); +void ev_job_export_set_page (EvJobExport *job, + gint page); +/* EvJobPrint */ +GType ev_job_print_get_type (void) G_GNUC_CONST; +EvJob *ev_job_print_new (EvDocument *document); +void ev_job_print_set_page (EvJobPrint *job, + gint page); +void ev_job_print_set_cairo (EvJobPrint *job, + cairo_t *cr); + +G_END_DECLS + +#endif /* __EV_JOBS_H__ */ diff --git a/libview/ev-loading-window.c b/libview/ev-loading-window.c new file mode 100644 index 00000000..e6352fe9 --- /dev/null +++ b/libview/ev-loading-window.c @@ -0,0 +1,278 @@ +/* ev-loading-window.c + * this file is part of evince, a mate document viewer + * + * Copyright (C) 2010 Carlos Garcia Campos <[email protected]> + * + * Evince is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * Evince is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ + +#include "config.h" + +#include <string.h> +#include <glib/gi18n.h> +#include "ev-loading-window.h" + +enum { + PROP_0, + PROP_PARENT +}; + +struct _EvLoadingWindow { + GtkWindow base_instance; + + GtkWindow *parent; + + gint x; + gint y; + gint width; + gint height; +}; + +struct _EvLoadingWindowClass { + GtkWindowClass base_class; +}; + +G_DEFINE_TYPE (EvLoadingWindow, ev_loading_window, GTK_TYPE_WINDOW) + +static void +ev_loading_window_set_property (GObject *object, + guint prop_id, + const GValue *value, + GParamSpec *pspec) +{ + EvLoadingWindow *window = EV_LOADING_WINDOW (object); + + switch (prop_id) { + case PROP_PARENT: + window->parent = g_value_get_object (value); + break; + default: + G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); + } +} + +static void +ev_loading_window_init (EvLoadingWindow *window) +{ + GtkWindow *gtk_window = GTK_WINDOW (window); + GtkWidget *widget = GTK_WIDGET (window); + GtkWidget *hbox; + GtkWidget *spinner; + GtkWidget *label; + GtkStyle *style; + GdkColor fg, bg; + const gchar *loading_text = _("Loadingā¦"); + const gchar *fg_color_name = "info_fg_color"; + const gchar *bg_color_name = "info_bg_color"; + + hbox = gtk_hbox_new (FALSE, 12); + + spinner = gtk_spinner_new (); + gtk_spinner_start (GTK_SPINNER (spinner)); + gtk_box_pack_start (GTK_BOX (hbox), spinner, FALSE, FALSE, 0); + gtk_widget_show (spinner); + + label = gtk_label_new (loading_text); + gtk_box_pack_start (GTK_BOX (hbox), label, FALSE, FALSE, 0); + gtk_widget_show (label); + + gtk_container_add (GTK_CONTAINER (window), hbox); + gtk_widget_show (hbox); + + gtk_widget_set_app_paintable (widget, TRUE); + + gtk_container_set_border_width (GTK_CONTAINER (window), 10); + + gtk_window_set_type_hint (gtk_window, GDK_WINDOW_TYPE_HINT_NOTIFICATION); + gtk_window_set_accept_focus (gtk_window, FALSE); + gtk_window_set_decorated (gtk_window, FALSE); + gtk_window_set_resizable (gtk_window, FALSE); + + style = gtk_widget_get_style (widget); + if (!gtk_style_lookup_color (style, fg_color_name, &fg) || + !gtk_style_lookup_color (style, bg_color_name, &bg)) { + fg.pixel = 0; + fg.red = 0xb800; + fg.green = 0xad00; + fg.blue = 0x9d00; + + bg.pixel = 0; + bg.red = 0xff00; + bg.green = 0xff00; + bg.blue = 0xbf00; + } + + if (!gdk_color_equal (&bg, &style->bg[GTK_STATE_NORMAL])) + gtk_widget_modify_bg (widget, GTK_STATE_NORMAL, &bg); + if (!gdk_color_equal (&fg, &style->fg[GTK_STATE_NORMAL])) + gtk_widget_modify_fg (widget, GTK_STATE_NORMAL, &fg); +} + +static GObject * +ev_loading_window_constructor (GType type, + guint n_construct_properties, + GObjectConstructParam *construct_params) +{ + GObject *object; + EvLoadingWindow *window; + GtkWindow *gtk_window; + + object = G_OBJECT_CLASS (ev_loading_window_parent_class)->constructor (type, + n_construct_properties, + construct_params); + window = EV_LOADING_WINDOW (object); + gtk_window = GTK_WINDOW (window); + + gtk_window_set_transient_for (gtk_window, window->parent); + gtk_window_set_destroy_with_parent (gtk_window, TRUE); + + return object; +} + +static void +_cairo_rounded_rectangle (cairo_t *cr, + gint width, + gint height, + gdouble radius) +{ + cairo_move_to (cr, radius, 0); + cairo_line_to (cr, width - radius, 0); + cairo_curve_to (cr, + width, 0, + width, 0, + width, + radius); + cairo_line_to (cr, width, height - radius); + cairo_curve_to (cr, + width,height, + width, height, + width - radius, + height); + cairo_line_to (cr, radius, height); + cairo_curve_to (cr, + 0, height, + 0, height, + 0, height - radius); + cairo_line_to (cr, 0, radius); + cairo_curve_to (cr, + 0, 0, + 0, 0, + radius, 0); +} + +static void +ev_loading_window_size_allocate (GtkWidget *widget, + GtkAllocation *allocation) +{ + EvLoadingWindow *window = EV_LOADING_WINDOW (widget); + GdkPixmap *mask; + cairo_t *cr; + double r; + + GTK_WIDGET_CLASS (ev_loading_window_parent_class)->size_allocate (widget, allocation); + + if (allocation->width == window->width && allocation->height == window->height) + return; + + window->width = allocation->width; + window->height = allocation->height; + + mask = gdk_pixmap_new (NULL, window->width, window->height, 1); + cr = gdk_cairo_create (GDK_DRAWABLE (mask)); + + cairo_save (cr); + cairo_rectangle (cr, 0, 0, window->width, window->height); + cairo_set_operator (cr, CAIRO_OPERATOR_CLEAR); + cairo_fill (cr); + cairo_restore (cr); + + cairo_set_source_rgb (cr, 1., 1., 1.); + r = MIN (window->width, window->height) / 2.; + _cairo_rounded_rectangle (cr, window->width, window->height, r); + cairo_fill (cr); + + cairo_destroy (cr); + + gtk_widget_shape_combine_mask (widget, mask, 0, 0); + g_object_unref (mask); +} + +static void +ev_loading_window_hide (GtkWidget *widget) +{ + EvLoadingWindow *window = EV_LOADING_WINDOW (widget); + + window->x = window->y = 0; + + GTK_WIDGET_CLASS (ev_loading_window_parent_class)->hide (widget); +} + +static void +ev_loading_window_class_init (EvLoadingWindowClass *klass) +{ + GObjectClass *g_object_class = G_OBJECT_CLASS (klass); + GtkWidgetClass *gtk_widget_class = GTK_WIDGET_CLASS (klass); + + g_object_class->constructor = ev_loading_window_constructor; + g_object_class->set_property = ev_loading_window_set_property; + + gtk_widget_class->size_allocate = ev_loading_window_size_allocate; + gtk_widget_class->hide = ev_loading_window_hide; + + g_object_class_install_property (g_object_class, + PROP_PARENT, + g_param_spec_object ("parent", + "Parent", + "The parent window", + GTK_TYPE_WINDOW, + G_PARAM_WRITABLE | + G_PARAM_CONSTRUCT_ONLY)); +} + +/* Public methods */ +GtkWidget * +ev_loading_window_new (GtkWindow *parent) +{ + GtkWidget *window; + + g_return_val_if_fail (GTK_IS_WINDOW (parent), NULL); + + window = g_object_new (EV_TYPE_LOADING_WINDOW, + "parent", parent, + NULL); + return window; +} + +void +ev_loading_window_get_size (EvLoadingWindow *window, + gint *width, + gint *height) +{ + if (width) *width = window->width; + if (height) *height = window->height; +} + +void +ev_loading_window_move (EvLoadingWindow *window, + gint x, + gint y) +{ + if (x == window->x && y == window->y) + return; + + window->x = x; + window->y = y; + gtk_window_move (GTK_WINDOW (window), x, y); +} diff --git a/libview/ev-loading-window.h b/libview/ev-loading-window.h new file mode 100644 index 00000000..fd8eee61 --- /dev/null +++ b/libview/ev-loading-window.h @@ -0,0 +1,49 @@ +/* ev-loading-window.h + * this file is part of evince, a mate document viewer + * + * Copyright (C) 2010 Carlos Garcia Campos <[email protected]> + * + * Evince is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * Evince is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ + +#ifndef EV_LOADING_WINDOW_H +#define EV_LOADING_WINDOW_H + +#include <gtk/gtk.h> + +G_BEGIN_DECLS + +typedef struct _EvLoadingWindow EvLoadingWindow; +typedef struct _EvLoadingWindowClass EvLoadingWindowClass; + +#define EV_TYPE_LOADING_WINDOW (ev_loading_window_get_type()) +#define EV_LOADING_WINDOW(object) (G_TYPE_CHECK_INSTANCE_CAST((object), EV_TYPE_LOADING_WINDOW, EvLoadingWindow)) +#define EV_LOADING_WINDOW_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST((klass), EV_TYPE_LOADING_WINDOW, EvLoadingWindowClass)) +#define EV_IS_LOADING_WINDOW(object) (G_TYPE_CHECK_INSTANCE_TYPE((object), EV_TYPE_LOADING_WINDOW)) +#define EV_IS_LOADING_WINDOW_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE((klass), EV_TYPE_LOADING_WINDOW)) +#define EV_LOADING_WINDOW_GET_CLASS(object) (G_TYPE_INSTANCE_GET_CLASS((object), EV_TYPE_LOADING_WINDOW, EvLoadingWindowClass)) + +GType ev_loading_window_get_type (void) G_GNUC_CONST; +GtkWidget *ev_loading_window_new (GtkWindow *parent); +void ev_loading_window_get_size (EvLoadingWindow *window, + gint *width, + gint *height); +void ev_loading_window_move (EvLoadingWindow *window, + gint x, + gint y); + +G_END_DECLS + +#endif /* EV_LOADING_WINDOW_H */ diff --git a/libview/ev-page-cache.c b/libview/ev-page-cache.c new file mode 100644 index 00000000..c4dc062c --- /dev/null +++ b/libview/ev-page-cache.c @@ -0,0 +1,521 @@ +/* this file is part of evince, a mate document viewer + * + * Copyright (C) 2009 Carlos Garcia Campos + * + * Evince is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * Evince is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ + +#include <config.h> + +#include <glib.h> +#include "ev-jobs.h" +#include "ev-job-scheduler.h" +#include "ev-mapping-list.h" +#include "ev-selection.h" +#include "ev-document-links.h" +#include "ev-document-forms.h" +#include "ev-document-images.h" +#include "ev-document-annotations.h" +#include "ev-document-text.h" +#include "ev-page-cache.h" + +typedef struct _EvPageCacheData { + EvJob *job; + gboolean done : 1; + gboolean dirty : 1; + EvJobPageDataFlags flags; + + EvMappingList *link_mapping; + EvMappingList *image_mapping; + EvMappingList *form_field_mapping; + EvMappingList *annot_mapping; + cairo_region_t *text_mapping; + EvRectangle *text_layout; + guint text_layout_length; + gchar *text; +} EvPageCacheData; + +struct _EvPageCache { + GObject parent; + + EvDocument *document; + EvPageCacheData *page_list; + gint n_pages; + + /* Current range */ + gint start_page; + gint end_page; + + EvJobPageDataFlags flags; +}; + +struct _EvPageCacheClass { + GObjectClass parent_class; +}; + +#define EV_PAGE_DATA_FLAGS_DEFAULT ( \ + EV_PAGE_DATA_INCLUDE_LINKS | \ + EV_PAGE_DATA_INCLUDE_TEXT_MAPPING | \ + EV_PAGE_DATA_INCLUDE_IMAGES | \ + EV_PAGE_DATA_INCLUDE_FORMS | \ + EV_PAGE_DATA_INCLUDE_ANNOTS) + + +static void job_page_data_finished_cb (EvJob *job, + EvPageCache *cache); +static void job_page_data_cancelled_cb (EvJob *job, + EvPageCacheData *data); + +G_DEFINE_TYPE (EvPageCache, ev_page_cache, G_TYPE_OBJECT) + +static void +ev_page_cache_data_free (EvPageCacheData *data) +{ + if (data->job) { + g_object_unref (data->job); + data->job = NULL; + } + + if (data->link_mapping) { + ev_mapping_list_unref (data->link_mapping); + data->link_mapping = NULL; + } + + if (data->image_mapping) { + ev_mapping_list_unref (data->image_mapping); + data->image_mapping = NULL; + } + + if (data->form_field_mapping) { + ev_mapping_list_unref (data->form_field_mapping); + data->form_field_mapping = NULL; + } + + if (data->annot_mapping) { + ev_mapping_list_unref (data->annot_mapping); + data->annot_mapping = NULL; + } + + if (data->text_mapping) { + cairo_region_destroy (data->text_mapping); + data->text_mapping = NULL; + } + + if (data->text_layout) { + g_free (data->text_layout); + data->text_layout = NULL; + data->text_layout_length = 0; + } + + if (data->text) { + g_free (data->text); + data->text = NULL; + } +} + +static void +ev_page_cache_finalize (GObject *object) +{ + EvPageCache *cache = EV_PAGE_CACHE (object); + gint i; + + if (cache->page_list) { + for (i = 0; i < cache->n_pages; i++) { + EvPageCacheData *data; + + data = &cache->page_list[i]; + + if (data->job) { + g_signal_handlers_disconnect_by_func (data->job, + G_CALLBACK (job_page_data_finished_cb), + cache); + g_signal_handlers_disconnect_by_func (data->job, + G_CALLBACK (job_page_data_cancelled_cb), + data); + } + ev_page_cache_data_free (data); + } + + g_free (cache->page_list); + cache->page_list = NULL; + cache->n_pages = 0; + } + + if (cache->document) { + g_object_unref (cache->document); + cache->document = NULL; + } + + G_OBJECT_CLASS (ev_page_cache_parent_class)->finalize (object); +} + +static void +ev_page_cache_init (EvPageCache *cache) +{ +} + +static void +ev_page_cache_class_init (EvPageCacheClass *klass) +{ + GObjectClass *g_object_class = G_OBJECT_CLASS (klass); + + g_object_class->finalize = ev_page_cache_finalize; +} + +static EvJobPageDataFlags +ev_page_cache_get_flags_for_data (EvPageCache *cache, + EvPageCacheData *data) +{ + EvJobPageDataFlags flags = EV_PAGE_DATA_INCLUDE_NONE; + + if (data->flags == cache->flags && !data->dirty) + return cache->flags; + + /* Flags changed or data is dirty */ + if (cache->flags & EV_PAGE_DATA_INCLUDE_LINKS) { + flags = (data->link_mapping) ? + flags & ~EV_PAGE_DATA_INCLUDE_LINKS : + flags | EV_PAGE_DATA_INCLUDE_LINKS; + } + + if (cache->flags & EV_PAGE_DATA_INCLUDE_IMAGES) { + flags = (data->image_mapping) ? + flags & ~EV_PAGE_DATA_INCLUDE_IMAGES : + flags | EV_PAGE_DATA_INCLUDE_IMAGES; + } + + if (cache->flags & EV_PAGE_DATA_INCLUDE_FORMS) { + flags = (data->form_field_mapping) ? + flags & ~EV_PAGE_DATA_INCLUDE_FORMS : + flags | EV_PAGE_DATA_INCLUDE_FORMS; + } + + if (cache->flags & EV_PAGE_DATA_INCLUDE_ANNOTS) { + flags = (data->annot_mapping) ? + flags & ~EV_PAGE_DATA_INCLUDE_ANNOTS : + flags | EV_PAGE_DATA_INCLUDE_ANNOTS; + } + + if (cache->flags & EV_PAGE_DATA_INCLUDE_TEXT_MAPPING) { + flags = (data->text_mapping) ? + flags & ~EV_PAGE_DATA_INCLUDE_TEXT_MAPPING : + flags | EV_PAGE_DATA_INCLUDE_TEXT_MAPPING; + } + + if (cache->flags & EV_PAGE_DATA_INCLUDE_TEXT) { + flags = (data->text) ? + flags & ~EV_PAGE_DATA_INCLUDE_TEXT : + flags | EV_PAGE_DATA_INCLUDE_TEXT; + } + + if (cache->flags & EV_PAGE_DATA_INCLUDE_TEXT_LAYOUT) { + flags = (data->text_layout_length > 0) ? + flags & ~EV_PAGE_DATA_INCLUDE_TEXT_LAYOUT : + flags | EV_PAGE_DATA_INCLUDE_TEXT_LAYOUT; + } + + return flags; +} + +EvPageCache * +ev_page_cache_new (EvDocument *document) +{ + EvPageCache *cache; + + g_return_val_if_fail (EV_IS_DOCUMENT (document), NULL); + + cache = EV_PAGE_CACHE (g_object_new (EV_TYPE_PAGE_CACHE, NULL)); + cache->document = g_object_ref (document); + cache->n_pages = ev_document_get_n_pages (document); + cache->flags = EV_PAGE_DATA_FLAGS_DEFAULT; + cache->page_list = g_new0 (EvPageCacheData, cache->n_pages); + + return cache; +} + +static void +job_page_data_finished_cb (EvJob *job, + EvPageCache *cache) +{ + EvJobPageData *job_data = EV_JOB_PAGE_DATA (job); + EvPageCacheData *data; + + data = &cache->page_list[job_data->page]; + + if (job_data->flags & EV_PAGE_DATA_INCLUDE_LINKS) + data->link_mapping = job_data->link_mapping; + if (job_data->flags & EV_PAGE_DATA_INCLUDE_IMAGES) + data->image_mapping = job_data->image_mapping; + if (job_data->flags & EV_PAGE_DATA_INCLUDE_FORMS) + data->form_field_mapping = job_data->form_field_mapping; + if (job_data->flags & EV_PAGE_DATA_INCLUDE_ANNOTS) + data->annot_mapping = job_data->annot_mapping; + if (job_data->flags & EV_PAGE_DATA_INCLUDE_TEXT_MAPPING) + data->text_mapping = job_data->text_mapping; + if (job_data->flags & EV_PAGE_DATA_INCLUDE_TEXT_LAYOUT) { + data->text_layout = job_data->text_layout; + data->text_layout_length = job_data->text_layout_length; + } + if (job_data->flags & EV_PAGE_DATA_INCLUDE_TEXT) + data->text = job_data->text; + data->done = TRUE; + data->dirty = FALSE; + + g_object_unref (data->job); + data->job = NULL; +} + +static void +job_page_data_cancelled_cb (EvJob *job, + EvPageCacheData *data) +{ + g_object_unref (data->job); + data->job = NULL; +} + +void +ev_page_cache_set_page_range (EvPageCache *cache, + gint start, + gint end) +{ + gint i; + + if (cache->flags == EV_PAGE_DATA_INCLUDE_NONE) + return; + + cache->start_page = start; + cache->end_page = end; + + for (i = start; i <= end; i++) { + EvPageCacheData *data = &cache->page_list[i]; + EvJobPageDataFlags flags; + + if (data->flags == cache->flags && !data->dirty && (data->done || data->job)) + continue; + + if (data->job) + ev_job_cancel (data->job); + + flags = ev_page_cache_get_flags_for_data (cache, data); + + data->flags = cache->flags; + data->job = ev_job_page_data_new (cache->document, i, flags); + g_signal_connect (data->job, "finished", + G_CALLBACK (job_page_data_finished_cb), + cache); + g_signal_connect (data->job, "cancelled", + G_CALLBACK (job_page_data_cancelled_cb), + data); + ev_job_scheduler_push_job (data->job, EV_JOB_PRIORITY_NONE); + } +} + +EvJobPageDataFlags +ev_page_cache_get_flags (EvPageCache *cache) +{ + return cache->flags; +} + +void +ev_page_cache_set_flags (EvPageCache *cache, + EvJobPageDataFlags flags) +{ + if (cache->flags == flags) + return; + + cache->flags = flags; + + /* Update the current range for new flags */ + ev_page_cache_set_page_range (cache, cache->start_page, cache->end_page); +} + +void +ev_page_cache_mark_dirty (EvPageCache *cache, + gint page) +{ + EvPageCacheData *data; + + g_return_if_fail (EV_IS_PAGE_CACHE (cache)); + + data = &cache->page_list[page]; + data->dirty = TRUE; + + /* Update the current range */ + ev_page_cache_set_page_range (cache, cache->start_page, cache->end_page); +} + +EvMappingList * +ev_page_cache_get_link_mapping (EvPageCache *cache, + gint page) +{ + EvPageCacheData *data; + + g_return_val_if_fail (EV_IS_PAGE_CACHE (cache), NULL); + g_return_val_if_fail (page >= 0 && page < cache->n_pages, NULL); + + if (!(cache->flags & EV_PAGE_DATA_INCLUDE_LINKS)) + return NULL; + + data = &cache->page_list[page]; + if (data->done) + return data->link_mapping; + + if (data->job) + return EV_JOB_PAGE_DATA (data->job)->link_mapping; + + return data->link_mapping; +} + +EvMappingList * +ev_page_cache_get_image_mapping (EvPageCache *cache, + gint page) +{ + EvPageCacheData *data; + + g_return_val_if_fail (EV_IS_PAGE_CACHE (cache), NULL); + g_return_val_if_fail (page >= 0 && page < cache->n_pages, NULL); + + if (!(cache->flags & EV_PAGE_DATA_INCLUDE_IMAGES)) + return NULL; + + data = &cache->page_list[page]; + if (data->done) + return data->image_mapping; + + if (data->job) + return EV_JOB_PAGE_DATA (data->job)->image_mapping; + + return data->image_mapping; +} + +EvMappingList * +ev_page_cache_get_form_field_mapping (EvPageCache *cache, + gint page) +{ + EvPageCacheData *data; + + g_return_val_if_fail (EV_IS_PAGE_CACHE (cache), NULL); + g_return_val_if_fail (page >= 0 && page < cache->n_pages, NULL); + + if (!(cache->flags & EV_PAGE_DATA_INCLUDE_FORMS)) + return NULL; + + data = &cache->page_list[page]; + if (data->done) + return data->form_field_mapping; + + if (data->job) + return EV_JOB_PAGE_DATA (data->job)->form_field_mapping; + + return data->form_field_mapping; +} + +EvMappingList * +ev_page_cache_get_annot_mapping (EvPageCache *cache, + gint page) +{ + EvPageCacheData *data; + + g_return_val_if_fail (EV_IS_PAGE_CACHE (cache), NULL); + g_return_val_if_fail (page >= 0 && page < cache->n_pages, NULL); + + if (!(cache->flags & EV_PAGE_DATA_INCLUDE_ANNOTS)) + return NULL; + + data = &cache->page_list[page]; + if (data->done) + return data->annot_mapping; + + if (data->job) + return EV_JOB_PAGE_DATA (data->job)->annot_mapping; + + return data->annot_mapping; +} + +cairo_region_t * +ev_page_cache_get_text_mapping (EvPageCache *cache, + gint page) +{ + EvPageCacheData *data; + + g_return_val_if_fail (EV_IS_PAGE_CACHE (cache), NULL); + g_return_val_if_fail (page >= 0 && page < cache->n_pages, NULL); + + if (!(cache->flags & EV_PAGE_DATA_INCLUDE_TEXT_MAPPING)) + return NULL; + + data = &cache->page_list[page]; + if (data->done) + return data->text_mapping; + + if (data->job) + return EV_JOB_PAGE_DATA (data->job)->text_mapping; + + return data->text_mapping; +} + +const gchar * +ev_page_cache_get_text (EvPageCache *cache, + gint page) +{ + EvPageCacheData *data; + + g_return_val_if_fail (EV_IS_PAGE_CACHE (cache), NULL); + g_return_val_if_fail (page >= 0 && page < cache->n_pages, NULL); + + if (!(cache->flags & EV_PAGE_DATA_INCLUDE_TEXT)) + return NULL; + + data = &cache->page_list[page]; + if (data->done) + return data->text; + + if (data->job) + return EV_JOB_PAGE_DATA (data->job)->text; + + return data->text; +} + +gboolean +ev_page_cache_get_text_layout (EvPageCache *cache, + gint page, + EvRectangle **areas, + guint *n_areas) +{ + EvPageCacheData *data; + + g_return_val_if_fail (EV_IS_PAGE_CACHE (cache), FALSE); + g_return_val_if_fail (page >= 0 && page < cache->n_pages, FALSE); + + if (!(cache->flags & EV_PAGE_DATA_INCLUDE_TEXT_LAYOUT)) + return FALSE; + + data = &cache->page_list[page]; + if (data->done) { + *areas = data->text_layout; + *n_areas = data->text_layout_length; + + return TRUE; + } + + if (data->job) { + *areas = EV_JOB_PAGE_DATA (data->job)->text_layout; + *n_areas = EV_JOB_PAGE_DATA (data->job)->text_layout_length; + + return TRUE; + } + + return FALSE; +} diff --git a/libview/ev-page-cache.h b/libview/ev-page-cache.h new file mode 100644 index 00000000..7432e44d --- /dev/null +++ b/libview/ev-page-cache.h @@ -0,0 +1,71 @@ +/* this file is part of evince, a mate document viewer + * + * Copyright (C) 2009 Carlos Garcia Campos + * + * Evince is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * Evince is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ + +#if !defined (__EV_EVINCE_VIEW_H_INSIDE__) && !defined (EVINCE_COMPILATION) +#error "Only <evince-view.h> can be included directly." +#endif + +#ifndef EV_PAGE_CACHE_H +#define EV_PAGE_CACHE_H + +#include <glib-object.h> +#include <gdk/gdk.h> +#include <evince-document.h> +#include <evince-view.h> + +G_BEGIN_DECLS + +#define EV_TYPE_PAGE_CACHE (ev_page_cache_get_type ()) +#define EV_PAGE_CACHE(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), EV_TYPE_PAGE_CACHE, EvPageCache)) +#define EV_IS_PAGE_CACHE(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), EV_TYPE_PAGE_CACHE)) + +typedef struct _EvPageCache EvPageCache; +typedef struct _EvPageCacheClass EvPageCacheClass; + +GType ev_page_cache_get_type (void) G_GNUC_CONST; +EvPageCache *ev_page_cache_new (EvDocument *document); + +void ev_page_cache_set_page_range (EvPageCache *cache, + gint start, + gint end); +EvJobPageDataFlags ev_page_cache_get_flags (EvPageCache *cache); +void ev_page_cache_set_flags (EvPageCache *cache, + EvJobPageDataFlags flags); +void ev_page_cache_mark_dirty (EvPageCache *cache, + gint page); +EvMappingList *ev_page_cache_get_link_mapping (EvPageCache *cache, + gint page); +EvMappingList *ev_page_cache_get_image_mapping (EvPageCache *cache, + gint page); +EvMappingList *ev_page_cache_get_form_field_mapping (EvPageCache *cache, + gint page); +EvMappingList *ev_page_cache_get_annot_mapping (EvPageCache *cache, + gint page); +cairo_region_t *ev_page_cache_get_text_mapping (EvPageCache *cache, + gint page); +const gchar *ev_page_cache_get_text (EvPageCache *cache, + gint page); +gboolean ev_page_cache_get_text_layout (EvPageCache *cache, + gint page, + EvRectangle **areas, + guint *n_areas); + +G_END_DECLS + +#endif /* EV_PAGE_CACHE_H */ diff --git a/libview/ev-pixbuf-cache.c b/libview/ev-pixbuf-cache.c new file mode 100644 index 00000000..367f70d7 --- /dev/null +++ b/libview/ev-pixbuf-cache.c @@ -0,0 +1,1192 @@ +#include <config.h> +#include "ev-pixbuf-cache.h" +#include "ev-job-scheduler.h" +#include "ev-view-private.h" + +typedef struct _CacheJobInfo +{ + EvJob *job; + gboolean page_ready; + + /* Region of the page that needs to be drawn */ + cairo_region_t *region; + + /* Data we get from rendering */ + cairo_surface_t *surface; + + /* Selection data. + * Selection_points are the coordinates encapsulated in selection. + * target_points is the target selection size. */ + EvRectangle selection_points; + EvRectangle target_points; + EvSelectionStyle selection_style; + gboolean points_set; + + cairo_surface_t *selection; + cairo_region_t *selection_region; +} CacheJobInfo; + +struct _EvPixbufCache +{ + GObject parent; + + /* We keep a link to our containing view just for style information. */ + GtkWidget *view; + EvDocument *document; + EvDocumentModel *model; + int start_page; + int end_page; + gboolean inverted_colors; + + gsize max_size; + + /* preload_cache_size is the number of pages prior to the current + * visible area that we cache. It's normally 1, but could be 2 in the + * case of twin pages. + */ + int preload_cache_size; + guint job_list_len; + + CacheJobInfo *prev_job; + CacheJobInfo *job_list; + CacheJobInfo *next_job; +}; + +struct _EvPixbufCacheClass +{ + GObjectClass parent_class; + + void (* job_finished) (EvPixbufCache *pixbuf_cache); +}; + + +enum +{ + JOB_FINISHED, + N_SIGNALS, +}; + +static guint signals[N_SIGNALS] = {0, }; + +static void ev_pixbuf_cache_init (EvPixbufCache *pixbuf_cache); +static void ev_pixbuf_cache_class_init (EvPixbufCacheClass *pixbuf_cache); +static void ev_pixbuf_cache_finalize (GObject *object); +static void ev_pixbuf_cache_dispose (GObject *object); +static void job_finished_cb (EvJob *job, + EvPixbufCache *pixbuf_cache); +static CacheJobInfo *find_job_cache (EvPixbufCache *pixbuf_cache, + int page); +static gboolean new_selection_surface_needed(EvPixbufCache *pixbuf_cache, + CacheJobInfo *job_info, + gint page, + gfloat scale); + + +/* These are used for iterating through the prev and next arrays */ +#define FIRST_VISIBLE_PREV(pixbuf_cache) \ + (MAX (0, pixbuf_cache->preload_cache_size - pixbuf_cache->start_page)) +#define VISIBLE_NEXT_LEN(pixbuf_cache) \ + (MIN(pixbuf_cache->preload_cache_size, ev_document_get_n_pages (pixbuf_cache->document) - (1 + pixbuf_cache->end_page))) +#define PAGE_CACHE_LEN(pixbuf_cache) \ + ((pixbuf_cache->end_page - pixbuf_cache->start_page) + 1) + +#define MAX_PRELOADED_PAGES 3 + +G_DEFINE_TYPE (EvPixbufCache, ev_pixbuf_cache, G_TYPE_OBJECT) + +static void +ev_pixbuf_cache_init (EvPixbufCache *pixbuf_cache) +{ + pixbuf_cache->start_page = -1; + pixbuf_cache->end_page = -1; +} + +static void +ev_pixbuf_cache_class_init (EvPixbufCacheClass *class) +{ + GObjectClass *object_class; + + object_class = G_OBJECT_CLASS (class); + + object_class->finalize = ev_pixbuf_cache_finalize; + object_class->dispose = ev_pixbuf_cache_dispose; + + signals[JOB_FINISHED] = + g_signal_new ("job-finished", + G_OBJECT_CLASS_TYPE (object_class), + G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION, + G_STRUCT_OFFSET (EvPixbufCacheClass, job_finished), + NULL, NULL, + g_cclosure_marshal_VOID__POINTER, + G_TYPE_NONE, 1, + G_TYPE_POINTER); +} + +static void +ev_pixbuf_cache_finalize (GObject *object) +{ + EvPixbufCache *pixbuf_cache; + + pixbuf_cache = EV_PIXBUF_CACHE (object); + + if (pixbuf_cache->job_list) { + g_slice_free1 (sizeof (CacheJobInfo) * pixbuf_cache->job_list_len, + pixbuf_cache->job_list); + pixbuf_cache->job_list = NULL; + } + if (pixbuf_cache->prev_job) { + g_slice_free1 (sizeof (CacheJobInfo) * pixbuf_cache->preload_cache_size, + pixbuf_cache->prev_job); + pixbuf_cache->prev_job = NULL; + } + if (pixbuf_cache->next_job) { + g_slice_free1 (sizeof (CacheJobInfo) * pixbuf_cache->preload_cache_size, + pixbuf_cache->next_job); + pixbuf_cache->next_job = NULL; + } + + g_object_unref (pixbuf_cache->model); + + G_OBJECT_CLASS (ev_pixbuf_cache_parent_class)->finalize (object); +} + +static void +dispose_cache_job_info (CacheJobInfo *job_info, + gpointer data) +{ + if (job_info == NULL) + return; + + if (job_info->job) { + g_signal_handlers_disconnect_by_func (job_info->job, + G_CALLBACK (job_finished_cb), + data); + ev_job_cancel (job_info->job); + g_object_unref (job_info->job); + job_info->job = NULL; + } + if (job_info->surface) { + cairo_surface_destroy (job_info->surface); + job_info->surface = NULL; + } + if (job_info->region) { + cairo_region_destroy (job_info->region); + job_info->region = NULL; + } + if (job_info->selection) { + cairo_surface_destroy (job_info->selection); + job_info->selection = NULL; + } + if (job_info->selection_region) { + cairo_region_destroy (job_info->selection_region); + job_info->selection_region = NULL; + } + + job_info->points_set = FALSE; +} + +static void +ev_pixbuf_cache_dispose (GObject *object) +{ + EvPixbufCache *pixbuf_cache; + int i; + + pixbuf_cache = EV_PIXBUF_CACHE (object); + + for (i = 0; i < pixbuf_cache->preload_cache_size; i++) { + dispose_cache_job_info (pixbuf_cache->prev_job + i, pixbuf_cache); + dispose_cache_job_info (pixbuf_cache->next_job + i, pixbuf_cache); + } + + for (i = 0; i < PAGE_CACHE_LEN (pixbuf_cache); i++) { + dispose_cache_job_info (pixbuf_cache->job_list + i, pixbuf_cache); + } + + G_OBJECT_CLASS (ev_pixbuf_cache_parent_class)->dispose (object); +} + + +EvPixbufCache * +ev_pixbuf_cache_new (GtkWidget *view, + EvDocumentModel *model, + gsize max_size) +{ + EvPixbufCache *pixbuf_cache; + + pixbuf_cache = (EvPixbufCache *) g_object_new (EV_TYPE_PIXBUF_CACHE, NULL); + /* This is a backlink, so we don't ref this */ + pixbuf_cache->view = view; + pixbuf_cache->model = g_object_ref (model); + pixbuf_cache->document = ev_document_model_get_document (model); + pixbuf_cache->max_size = max_size; + + return pixbuf_cache; +} + +void +ev_pixbuf_cache_set_max_size (EvPixbufCache *pixbuf_cache, + gsize max_size) +{ + if (pixbuf_cache->max_size == max_size) + return; + + if (pixbuf_cache->max_size > max_size) + ev_pixbuf_cache_clear (pixbuf_cache); + pixbuf_cache->max_size = max_size; +} + +static void +copy_job_to_job_info (EvJobRender *job_render, + CacheJobInfo *job_info, + EvPixbufCache *pixbuf_cache) +{ + if (job_info->surface) { + cairo_surface_destroy (job_info->surface); + } + job_info->surface = cairo_surface_reference (job_render->surface); + if (pixbuf_cache->inverted_colors) { + ev_document_misc_invert_surface (job_info->surface); + } + + job_info->points_set = FALSE; + if (job_render->include_selection) { + if (job_info->selection) { + cairo_surface_destroy (job_info->selection); + job_info->selection = NULL; + } + if (job_info->selection_region) { + cairo_region_destroy (job_info->selection_region); + job_info->selection_region = NULL; + } + + job_info->selection_points = job_render->selection_points; + job_info->selection_region = cairo_region_reference (job_render->selection_region); + job_info->selection = cairo_surface_reference (job_render->selection); + g_assert (job_info->selection_points.x1 >= 0); + job_info->points_set = TRUE; + } + + if (job_info->job) { + g_signal_handlers_disconnect_by_func (job_info->job, + G_CALLBACK (job_finished_cb), + pixbuf_cache); + ev_job_cancel (job_info->job); + g_object_unref (job_info->job); + job_info->job = NULL; + } + + job_info->page_ready = TRUE; +} + +static void +job_finished_cb (EvJob *job, + EvPixbufCache *pixbuf_cache) +{ + CacheJobInfo *job_info; + EvJobRender *job_render = EV_JOB_RENDER (job); + + /* If the job is outside of our interest, we silently discard it */ + if ((job_render->page < (pixbuf_cache->start_page - pixbuf_cache->preload_cache_size)) || + (job_render->page > (pixbuf_cache->end_page + pixbuf_cache->preload_cache_size))) { + g_object_unref (job); + return; + } + + job_info = find_job_cache (pixbuf_cache, job_render->page); + + copy_job_to_job_info (job_render, job_info, pixbuf_cache); + g_signal_emit (pixbuf_cache, signals[JOB_FINISHED], 0, job_info->region); +} + +/* This checks a job to see if the job would generate the right sized pixbuf + * given a scale. If it won't, it removes the job and clears it to NULL. + */ +static void +check_job_size_and_unref (EvPixbufCache *pixbuf_cache, + CacheJobInfo *job_info, + gfloat scale) +{ + gint width, height; + + g_assert (job_info); + + if (job_info->job == NULL) + return; + + _get_page_size_for_scale_and_rotation (job_info->job->document, + EV_JOB_RENDER (job_info->job)->page, + scale, + EV_JOB_RENDER (job_info->job)->rotation, + &width, &height); + if (width == EV_JOB_RENDER (job_info->job)->target_width && + height == EV_JOB_RENDER (job_info->job)->target_height) + return; + + g_signal_handlers_disconnect_by_func (job_info->job, + G_CALLBACK (job_finished_cb), + pixbuf_cache); + ev_job_cancel (job_info->job); + g_object_unref (job_info->job); + job_info->job = NULL; +} + +/* Do all function that copies a job from an older cache to it's position in the + * new cache. It clears the old job if it doesn't have a place. + */ +static void +move_one_job (CacheJobInfo *job_info, + EvPixbufCache *pixbuf_cache, + int page, + CacheJobInfo *new_job_list, + CacheJobInfo *new_prev_job, + CacheJobInfo *new_next_job, + int new_preload_cache_size, + int start_page, + int end_page, + gint priority) +{ + CacheJobInfo *target_page = NULL; + int page_offset; + gint new_priority; + + if (page < (start_page - new_preload_cache_size) || + page > (end_page + new_preload_cache_size)) { + dispose_cache_job_info (job_info, pixbuf_cache); + return; + } + + /* find the target page to copy it over to. */ + if (page < start_page) { + page_offset = (page - (start_page - new_preload_cache_size)); + + g_assert (page_offset >= 0 && + page_offset < new_preload_cache_size); + target_page = new_prev_job + page_offset; + new_priority = EV_JOB_PRIORITY_LOW; + } else if (page > end_page) { + page_offset = (page - (end_page + 1)); + + g_assert (page_offset >= 0 && + page_offset < new_preload_cache_size); + target_page = new_next_job + page_offset; + new_priority = EV_JOB_PRIORITY_LOW; + } else { + page_offset = page - start_page; + g_assert (page_offset >= 0 && + page_offset <= ((end_page - start_page) + 1)); + new_priority = EV_JOB_PRIORITY_URGENT; + target_page = new_job_list + page_offset; + } + + *target_page = *job_info; + job_info->job = NULL; + job_info->region = NULL; + job_info->surface = NULL; + + if (new_priority != priority && target_page->job) { + ev_job_scheduler_update_job (target_page->job, new_priority); + } +} + +static gsize +ev_pixbuf_cache_get_page_size (EvPixbufCache *pixbuf_cache, + gint page_index, + gdouble scale, + gint rotation) +{ + gint width, height; + + _get_page_size_for_scale_and_rotation (pixbuf_cache->document, + page_index, scale, rotation, + &width, &height); + return height * cairo_format_stride_for_width (CAIRO_FORMAT_RGB24, width); +} + +static gint +ev_pixbuf_cache_get_preload_size (EvPixbufCache *pixbuf_cache, + gint start_page, + gint end_page, + gdouble scale, + gint rotation) +{ + gsize range_size = 0; + gint new_preload_cache_size = 0; + gint i; + guint n_pages = ev_document_get_n_pages (pixbuf_cache->document); + + /* Get the size of the current range */ + for (i = start_page; i <= end_page; i++) { + range_size += ev_pixbuf_cache_get_page_size (pixbuf_cache, i, scale, rotation); + } + + if (range_size >= pixbuf_cache->max_size) + return new_preload_cache_size; + + i = 1; + while (((start_page - i > 0) || (end_page + i < n_pages)) && + new_preload_cache_size < MAX_PRELOADED_PAGES) { + gsize page_size; + gboolean updated = FALSE; + + if (end_page + i < n_pages) { + page_size = ev_pixbuf_cache_get_page_size (pixbuf_cache, end_page + i, + scale, rotation); + if (page_size + range_size <= pixbuf_cache->max_size) { + range_size += page_size; + new_preload_cache_size++; + updated = TRUE; + } else { + break; + } + } + + if (start_page - i > 0) { + page_size = ev_pixbuf_cache_get_page_size (pixbuf_cache, start_page - i, + scale, rotation); + if (page_size + range_size <= pixbuf_cache->max_size) { + range_size += page_size; + if (!updated) + new_preload_cache_size++; + } else { + break; + } + } + i++; + } + + return new_preload_cache_size; +} + +static void +ev_pixbuf_cache_update_range (EvPixbufCache *pixbuf_cache, + gint start_page, + gint end_page, + guint rotation, + gdouble scale) +{ + CacheJobInfo *new_job_list; + CacheJobInfo *new_prev_job = NULL; + CacheJobInfo *new_next_job = NULL; + gint new_preload_cache_size; + guint new_job_list_len; + int i, page; + + new_preload_cache_size = ev_pixbuf_cache_get_preload_size (pixbuf_cache, + start_page, + end_page, + scale, + rotation); + if (pixbuf_cache->start_page == start_page && + pixbuf_cache->end_page == end_page && + pixbuf_cache->preload_cache_size == new_preload_cache_size) + return; + + new_job_list_len = (end_page - start_page) + 1; + new_job_list = g_slice_alloc0 (sizeof (CacheJobInfo) * new_job_list_len); + if (new_preload_cache_size > 0) { + new_prev_job = g_slice_alloc0 (sizeof (CacheJobInfo) * new_preload_cache_size); + new_next_job = g_slice_alloc0 (sizeof (CacheJobInfo) * new_preload_cache_size); + } + + /* We go through each job in the old cache and either clear it or move + * it to a new location. */ + + /* Start with the prev cache. */ + page = pixbuf_cache->start_page - pixbuf_cache->preload_cache_size; + for (i = 0; i < pixbuf_cache->preload_cache_size; i++) { + if (page < 0) { + dispose_cache_job_info (pixbuf_cache->prev_job + i, pixbuf_cache); + } else { + move_one_job (pixbuf_cache->prev_job + i, + pixbuf_cache, page, + new_job_list, new_prev_job, new_next_job, + new_preload_cache_size, + start_page, end_page, EV_JOB_PRIORITY_LOW); + } + page ++; + } + + page = pixbuf_cache->start_page; + for (i = 0; i < PAGE_CACHE_LEN (pixbuf_cache) && page >= 0; i++) { + move_one_job (pixbuf_cache->job_list + i, + pixbuf_cache, page, + new_job_list, new_prev_job, new_next_job, + new_preload_cache_size, + start_page, end_page, EV_JOB_PRIORITY_URGENT); + page ++; + } + + for (i = 0; i < pixbuf_cache->preload_cache_size; i++) { + if (page >= ev_document_get_n_pages (pixbuf_cache->document)) { + dispose_cache_job_info (pixbuf_cache->next_job + i, pixbuf_cache); + } else { + move_one_job (pixbuf_cache->next_job + i, + pixbuf_cache, page, + new_job_list, new_prev_job, new_next_job, + new_preload_cache_size, + start_page, end_page, EV_JOB_PRIORITY_LOW); + } + page ++; + } + + if (pixbuf_cache->job_list) { + g_slice_free1 (sizeof (CacheJobInfo) * pixbuf_cache->job_list_len, + pixbuf_cache->job_list); + } + if (pixbuf_cache->prev_job) { + g_slice_free1 (sizeof (CacheJobInfo) * pixbuf_cache->preload_cache_size, + pixbuf_cache->prev_job); + } + if (pixbuf_cache->next_job) { + g_slice_free1 (sizeof (CacheJobInfo) * pixbuf_cache->preload_cache_size, + pixbuf_cache->next_job); + } + + pixbuf_cache->preload_cache_size = new_preload_cache_size; + pixbuf_cache->job_list_len = new_job_list_len; + + pixbuf_cache->job_list = new_job_list; + pixbuf_cache->prev_job = new_prev_job; + pixbuf_cache->next_job = new_next_job; + + pixbuf_cache->start_page = start_page; + pixbuf_cache->end_page = end_page; +} + +static CacheJobInfo * +find_job_cache (EvPixbufCache *pixbuf_cache, + int page) +{ + int page_offset; + + if (page < (pixbuf_cache->start_page - pixbuf_cache->preload_cache_size) || + page > (pixbuf_cache->end_page + pixbuf_cache->preload_cache_size)) + return NULL; + + if (page < pixbuf_cache->start_page) { + page_offset = (page - (pixbuf_cache->start_page - pixbuf_cache->preload_cache_size)); + + g_assert (page_offset >= 0 && + page_offset < pixbuf_cache->preload_cache_size); + return pixbuf_cache->prev_job + page_offset; + } + + if (page > pixbuf_cache->end_page) { + page_offset = (page - (pixbuf_cache->end_page + 1)); + + g_assert (page_offset >= 0 && + page_offset < pixbuf_cache->preload_cache_size); + return pixbuf_cache->next_job + page_offset; + } + + page_offset = page - pixbuf_cache->start_page; + g_assert (page_offset >= 0 && + page_offset <= PAGE_CACHE_LEN(pixbuf_cache)); + return pixbuf_cache->job_list + page_offset; +} + +static void +ev_pixbuf_cache_clear_job_sizes (EvPixbufCache *pixbuf_cache, + gfloat scale) +{ + int i; + + for (i = 0; i < PAGE_CACHE_LEN (pixbuf_cache); i++) { + check_job_size_and_unref (pixbuf_cache, pixbuf_cache->job_list + i, scale); + } + + for (i = 0; i < pixbuf_cache->preload_cache_size; i++) { + check_job_size_and_unref (pixbuf_cache, pixbuf_cache->prev_job + i, scale); + check_job_size_and_unref (pixbuf_cache, pixbuf_cache->next_job + i, scale); + } +} + +static void +get_selection_colors (GtkWidget *widget, GdkColor **text, GdkColor **base) +{ + GtkStyle *style = gtk_widget_get_style (widget); + + if (gtk_widget_has_focus (widget)) { + *text = &style->text [GTK_STATE_SELECTED]; + *base = &style->base [GTK_STATE_SELECTED]; + } else { + *text = &style->text [GTK_STATE_ACTIVE]; + *base = &style->base [GTK_STATE_ACTIVE]; + } +} + +static void +add_job (EvPixbufCache *pixbuf_cache, + CacheJobInfo *job_info, + cairo_region_t *region, + gint width, + gint height, + gint page, + gint rotation, + gfloat scale, + EvJobPriority priority) +{ + job_info->page_ready = FALSE; + + if (job_info->region) + cairo_region_destroy (job_info->region); + job_info->region = region ? cairo_region_reference (region) : NULL; + + job_info->job = ev_job_render_new (pixbuf_cache->document, + page, rotation, scale, + width, height); + + if (new_selection_surface_needed (pixbuf_cache, job_info, page, scale)) { + GdkColor *text, *base; + + gtk_widget_ensure_style (pixbuf_cache->view); + get_selection_colors (pixbuf_cache->view, &text, &base); + ev_job_render_set_selection_info (EV_JOB_RENDER (job_info->job), + &(job_info->target_points), + job_info->selection_style, + text, base); + } + + g_signal_connect (job_info->job, "finished", + G_CALLBACK (job_finished_cb), + pixbuf_cache); + ev_job_scheduler_push_job (job_info->job, priority); +} + +static void +add_job_if_needed (EvPixbufCache *pixbuf_cache, + CacheJobInfo *job_info, + gint page, + gint rotation, + gfloat scale, + EvJobPriority priority) +{ + gint width, height; + + if (job_info->job) + return; + + _get_page_size_for_scale_and_rotation (pixbuf_cache->document, + page, scale, rotation, + &width, &height); + + if (job_info->surface && + cairo_image_surface_get_width (job_info->surface) == width && + cairo_image_surface_get_height (job_info->surface) == height) + return; + + /* Free old surfaces for non visible pages */ + if (priority == EV_JOB_PRIORITY_LOW) { + if (job_info->surface) { + cairo_surface_destroy (job_info->surface); + job_info->surface = NULL; + } + + if (job_info->selection) { + cairo_surface_destroy (job_info->selection); + job_info->selection = NULL; + } + } + + add_job (pixbuf_cache, job_info, NULL, + width, height, page, rotation, scale, + priority); +} + +static void +ev_pixbuf_cache_add_jobs_if_needed (EvPixbufCache *pixbuf_cache, + gint rotation, + gfloat scale) +{ + CacheJobInfo *job_info; + int page; + int i; + + for (i = 0; i < PAGE_CACHE_LEN (pixbuf_cache); i++) { + job_info = (pixbuf_cache->job_list + i); + page = pixbuf_cache->start_page + i; + + add_job_if_needed (pixbuf_cache, job_info, + page, rotation, scale, + EV_JOB_PRIORITY_URGENT); + } + + for (i = FIRST_VISIBLE_PREV(pixbuf_cache); i < pixbuf_cache->preload_cache_size; i++) { + job_info = (pixbuf_cache->prev_job + i); + page = pixbuf_cache->start_page - pixbuf_cache->preload_cache_size + i; + + add_job_if_needed (pixbuf_cache, job_info, + page, rotation, scale, + EV_JOB_PRIORITY_LOW); + } + + for (i = 0; i < VISIBLE_NEXT_LEN(pixbuf_cache); i++) { + job_info = (pixbuf_cache->next_job + i); + page = pixbuf_cache->end_page + 1 + i; + + add_job_if_needed (pixbuf_cache, job_info, + page, rotation, scale, + EV_JOB_PRIORITY_LOW); + } + +} + +void +ev_pixbuf_cache_set_page_range (EvPixbufCache *pixbuf_cache, + gint start_page, + gint end_page, + GList *selection_list) +{ + gdouble scale = ev_document_model_get_scale (pixbuf_cache->model); + gint rotation = ev_document_model_get_rotation (pixbuf_cache->model); + + g_return_if_fail (EV_IS_PIXBUF_CACHE (pixbuf_cache)); + + g_return_if_fail (start_page >= 0 && start_page < ev_document_get_n_pages (pixbuf_cache->document)); + g_return_if_fail (end_page >= 0 && end_page < ev_document_get_n_pages (pixbuf_cache->document)); + g_return_if_fail (end_page >= start_page); + + /* First, resize the page_range as needed. We cull old pages + * mercilessly. */ + ev_pixbuf_cache_update_range (pixbuf_cache, start_page, end_page, rotation, scale); + + /* Then, we update the current jobs to see if any of them are the wrong + * size, we remove them if we need to. */ + ev_pixbuf_cache_clear_job_sizes (pixbuf_cache, scale); + + /* Next, we update the target selection for our pages */ + ev_pixbuf_cache_set_selection_list (pixbuf_cache, selection_list); + + /* Finally, we add the new jobs for all the sizes that don't have a + * pixbuf */ + ev_pixbuf_cache_add_jobs_if_needed (pixbuf_cache, rotation, scale); +} + +void +ev_pixbuf_cache_set_inverted_colors (EvPixbufCache *pixbuf_cache, + gboolean inverted_colors) +{ + gint i; + + if (pixbuf_cache->inverted_colors == inverted_colors) + return; + + pixbuf_cache->inverted_colors = inverted_colors; + + for (i = 0; i < pixbuf_cache->preload_cache_size; i++) { + CacheJobInfo *job_info; + + job_info = pixbuf_cache->prev_job + i; + if (job_info && job_info->surface) + ev_document_misc_invert_surface (job_info->surface); + + job_info = pixbuf_cache->next_job + i; + if (job_info && job_info->surface) + ev_document_misc_invert_surface (job_info->surface); + } + + for (i = 0; i < PAGE_CACHE_LEN (pixbuf_cache); i++) { + CacheJobInfo *job_info; + + job_info = pixbuf_cache->job_list + i; + if (job_info && job_info->surface) + ev_document_misc_invert_surface (job_info->surface); + } +} + +cairo_surface_t * +ev_pixbuf_cache_get_surface (EvPixbufCache *pixbuf_cache, + gint page) +{ + CacheJobInfo *job_info; + + job_info = find_job_cache (pixbuf_cache, page); + if (job_info == NULL) + return NULL; + + if (job_info->page_ready) + return job_info->surface; + + /* We don't need to wait for the idle to handle the callback */ + if (job_info->job && + EV_JOB_RENDER (job_info->job)->page_ready) { + copy_job_to_job_info (EV_JOB_RENDER (job_info->job), job_info, pixbuf_cache); + g_signal_emit (pixbuf_cache, signals[JOB_FINISHED], 0, job_info->region); + } + + return job_info->surface; +} + +static gboolean +new_selection_surface_needed (EvPixbufCache *pixbuf_cache, + CacheJobInfo *job_info, + gint page, + gfloat scale) +{ + if (job_info->selection) { + gint width, height; + gint selection_width, selection_height; + + _get_page_size_for_scale_and_rotation (pixbuf_cache->document, + page, scale, 0, + &width, &height); + + selection_width = cairo_image_surface_get_width (job_info->selection); + selection_height = cairo_image_surface_get_height (job_info->selection); + + if (width != selection_width || height != selection_height) + return TRUE; + } else { + if (job_info->points_set) + return TRUE; + } + + return FALSE; +} + +static void +clear_selection_if_needed (EvPixbufCache *pixbuf_cache, + CacheJobInfo *job_info, + gint page, + gfloat scale) +{ + if (new_selection_surface_needed (pixbuf_cache, job_info, page, scale)) { + if (job_info->selection) + cairo_surface_destroy (job_info->selection); + job_info->selection = NULL; + job_info->selection_points.x1 = -1; + } +} + +/* Clears the cache of jobs and pixbufs. + */ +void +ev_pixbuf_cache_clear (EvPixbufCache *pixbuf_cache) +{ + int i; + + if (!pixbuf_cache->job_list) + return; + + for (i = 0; i < pixbuf_cache->preload_cache_size; i++) { + dispose_cache_job_info (pixbuf_cache->prev_job + i, pixbuf_cache); + dispose_cache_job_info (pixbuf_cache->next_job + i, pixbuf_cache); + } + + for (i = 0; i < PAGE_CACHE_LEN (pixbuf_cache); i++) { + dispose_cache_job_info (pixbuf_cache->job_list + i, pixbuf_cache); + } +} + + +void +ev_pixbuf_cache_style_changed (EvPixbufCache *pixbuf_cache) +{ + gint i; + + if (!pixbuf_cache->job_list) + return; + + /* FIXME: doesn't update running jobs. */ + for (i = 0; i < pixbuf_cache->preload_cache_size; i++) { + CacheJobInfo *job_info; + + job_info = pixbuf_cache->prev_job + i; + if (job_info->selection) { + cairo_surface_destroy (job_info->selection); + job_info->selection = NULL; + } + + job_info = pixbuf_cache->next_job + i; + if (job_info->selection) { + cairo_surface_destroy (job_info->selection); + job_info->selection = NULL; + } + } + + for (i = 0; i < PAGE_CACHE_LEN (pixbuf_cache); i++) { + CacheJobInfo *job_info; + + job_info = pixbuf_cache->job_list + i; + if (job_info->selection) { + cairo_surface_destroy (job_info->selection); + job_info->selection = NULL; + } + } +} + +cairo_surface_t * +ev_pixbuf_cache_get_selection_surface (EvPixbufCache *pixbuf_cache, + gint page, + gfloat scale, + cairo_region_t **region) +{ + CacheJobInfo *job_info; + + /* the document does not implement the selection interface */ + if (!EV_IS_SELECTION (pixbuf_cache->document)) + return NULL; + + job_info = find_job_cache (pixbuf_cache, page); + if (job_info == NULL) + return NULL; + + /* No selection on this page */ + if (!job_info->points_set) + return NULL; + + /* If we have a running job, we just return what we have under the + * assumption that it'll be updated later and we can scale it as need + * be */ + if (job_info->job && EV_JOB_RENDER (job_info->job)->include_selection) + return job_info->selection; + + /* Now, lets see if we need to resize the image. If we do, we clear the + * old one. */ + clear_selection_if_needed (pixbuf_cache, job_info, page, scale); + + /* Finally, we see if the two scales are the same, and get a new pixbuf + * if needed. We do this synchronously for now. At some point, we + * _should_ be able to get rid of the doc_mutex, so the synchronicity + * doesn't kill us. Rendering a few glyphs should really be fast. + */ + if (ev_rect_cmp (&(job_info->target_points), &(job_info->selection_points))) { + EvRectangle *old_points; + GdkColor *text, *base; + EvRenderContext *rc; + EvPage *ev_page; + + /* we need to get a new selection pixbuf */ + ev_document_doc_mutex_lock (); + if (job_info->selection_points.x1 < 0) { + g_assert (job_info->selection == NULL); + old_points = NULL; + } else { + g_assert (job_info->selection != NULL); + old_points = &(job_info->selection_points); + } + + ev_page = ev_document_get_page (pixbuf_cache->document, page); + rc = ev_render_context_new (ev_page, 0, scale); + g_object_unref (ev_page); + + if (job_info->selection_region) + cairo_region_destroy (job_info->selection_region); + job_info->selection_region = + ev_selection_get_selection_region (EV_SELECTION (pixbuf_cache->document), + rc, job_info->selection_style, + &(job_info->target_points)); + + gtk_widget_ensure_style (pixbuf_cache->view); + + get_selection_colors (pixbuf_cache->view, &text, &base); + + ev_selection_render_selection (EV_SELECTION (pixbuf_cache->document), + rc, &(job_info->selection), + &(job_info->target_points), + old_points, + job_info->selection_style, + text, base); + job_info->selection_points = job_info->target_points; + g_object_unref (rc); + ev_document_doc_mutex_unlock (); + } + if (region) + *region = job_info->selection_region; + return job_info->selection; +} + +static void +update_job_selection (CacheJobInfo *job_info, + EvViewSelection *selection) +{ + job_info->points_set = TRUE; + job_info->target_points = selection->rect; + job_info->selection_style = selection->style; +} + +static void +clear_job_selection (CacheJobInfo *job_info) +{ + job_info->points_set = FALSE; + job_info->selection_points.x1 = -1; + + if (job_info->selection) { + cairo_surface_destroy (job_info->selection); + job_info->selection = NULL; + } +} + +/* This function will reset the selection on pages that no longer have them, and + * will update the target_selection on those that need it. It will _not_ free + * the previous selection_list -- that's up to caller to do. + */ +void +ev_pixbuf_cache_set_selection_list (EvPixbufCache *pixbuf_cache, + GList *selection_list) +{ + EvViewSelection *selection; + GList *list = selection_list; + int page; + int i; + + g_return_if_fail (EV_IS_PIXBUF_CACHE (pixbuf_cache)); + + if (!EV_IS_SELECTION (pixbuf_cache->document)) + return; + + /* We check each area to see what needs updating, and what needs freeing; */ + page = pixbuf_cache->start_page - pixbuf_cache->preload_cache_size; + for (i = 0; i < pixbuf_cache->preload_cache_size; i++) { + if (page < 0) { + page ++; + continue; + } + + selection = NULL; + while (list) { + if (((EvViewSelection *)list->data)->page == page) { + selection = list->data; + break; + } else if (((EvViewSelection *)list->data)->page > page) + break; + list = list->next; + } + + if (selection) + update_job_selection (pixbuf_cache->prev_job + i, selection); + else + clear_job_selection (pixbuf_cache->prev_job + i); + page ++; + } + + page = pixbuf_cache->start_page; + for (i = 0; i < PAGE_CACHE_LEN (pixbuf_cache); i++) { + selection = NULL; + while (list) { + if (((EvViewSelection *)list->data)->page == page) { + selection = list->data; + break; + } else if (((EvViewSelection *)list->data)->page > page) + break; + list = list->next; + } + + if (selection) + update_job_selection (pixbuf_cache->job_list + i, selection); + else + clear_job_selection (pixbuf_cache->job_list + i); + page ++; + } + + for (i = 0; i < pixbuf_cache->preload_cache_size; i++) { + if (page >= ev_document_get_n_pages (pixbuf_cache->document)) + break; + + selection = NULL; + while (list) { + if (((EvViewSelection *)list->data)->page == page) { + selection = list->data; + break; + } else if (((EvViewSelection *)list->data)->page > page) + break; + list = list->next; + } + + if (selection) + update_job_selection (pixbuf_cache->next_job + i, selection); + else + clear_job_selection (pixbuf_cache->next_job + i); + page ++; + } +} + + +/* Returns what the pixbuf cache thinks is */ + +GList * +ev_pixbuf_cache_get_selection_list (EvPixbufCache *pixbuf_cache) +{ + EvViewSelection *selection; + GList *retval = NULL; + int page; + int i; + + g_return_val_if_fail (EV_IS_PIXBUF_CACHE (pixbuf_cache), NULL); + + /* We check each area to see what needs updating, and what needs freeing; */ + page = pixbuf_cache->start_page - pixbuf_cache->preload_cache_size; + for (i = 0; i < pixbuf_cache->preload_cache_size; i++) { + if (page < 0) { + page ++; + continue; + } + + if (pixbuf_cache->prev_job[i].selection_points.x1 != -1) { + selection = g_new0 (EvViewSelection, 1); + selection->page = page; + selection->rect = pixbuf_cache->prev_job[i].selection_points; + if (pixbuf_cache->prev_job[i].selection_region) + selection->covered_region = cairo_region_reference (pixbuf_cache->prev_job[i].selection_region); + retval = g_list_append (retval, selection); + } + + page ++; + } + + page = pixbuf_cache->start_page; + for (i = 0; i < PAGE_CACHE_LEN (pixbuf_cache); i++) { + if (pixbuf_cache->job_list[i].selection_points.x1 != -1) { + selection = g_new0 (EvViewSelection, 1); + selection->page = page; + selection->rect = pixbuf_cache->job_list[i].selection_points; + if (pixbuf_cache->job_list[i].selection_region) + selection->covered_region = cairo_region_reference (pixbuf_cache->job_list[i].selection_region); + retval = g_list_append (retval, selection); + } + + page ++; + } + + for (i = 0; i < pixbuf_cache->preload_cache_size; i++) { + if (page >= ev_document_get_n_pages (pixbuf_cache->document)) + break; + + if (pixbuf_cache->next_job[i].selection_points.x1 != -1) { + selection = g_new0 (EvViewSelection, 1); + selection->page = page; + selection->rect = pixbuf_cache->next_job[i].selection_points; + if (pixbuf_cache->next_job[i].selection_region) + selection->covered_region = cairo_region_reference (pixbuf_cache->next_job[i].selection_region); + retval = g_list_append (retval, selection); + } + + page ++; + } + + return retval; +} + +void +ev_pixbuf_cache_reload_page (EvPixbufCache *pixbuf_cache, + cairo_region_t *region, + gint page, + gint rotation, + gdouble scale) +{ + CacheJobInfo *job_info; + gint width, height; + + job_info = find_job_cache (pixbuf_cache, page); + if (job_info == NULL) + return; + + _get_page_size_for_scale_and_rotation (pixbuf_cache->document, + page, scale, rotation, + &width, &height); + add_job (pixbuf_cache, job_info, region, + width, height, page, rotation, scale, + EV_JOB_PRIORITY_URGENT); +} + + diff --git a/libview/ev-pixbuf-cache.h b/libview/ev-pixbuf-cache.h new file mode 100644 index 00000000..67c2517e --- /dev/null +++ b/libview/ev-pixbuf-cache.h @@ -0,0 +1,89 @@ +/* this file is part of evince, a mate document viewer + * + * Copyright (C) 2005 Red Hat, Inc + * + * Evince is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * Evince is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ + +/* This File is basically an extention of EvView, and is out here just to keep + * ev-view.c from exploding. + */ + +#if !defined (__EV_EVINCE_VIEW_H_INSIDE__) && !defined (EVINCE_COMPILATION) +#error "Only <evince-view.h> can be included directly." +#endif + +#ifndef __EV_PIXBUF_CACHE_H__ +#define __EV_PIXBUF_CACHE_H__ + +#include <gtk/gtk.h> + +#include <evince-document.h> +#include <evince-view.h> + +G_BEGIN_DECLS + +#define EV_TYPE_PIXBUF_CACHE (ev_pixbuf_cache_get_type ()) +#define EV_PIXBUF_CACHE(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), EV_TYPE_PIXBUF_CACHE, EvPixbufCache)) +#define EV_IS_PIXBUF_CACHE(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), EV_TYPE_PIXBUF_CACHE)) + + + +/* The coordinates in the rect here are at scale == 1.0, so that we can ignore + * resizings. There is one per page, maximum. + */ +typedef struct { + int page; + EvRectangle rect; + cairo_region_t *covered_region; + EvSelectionStyle style; +} EvViewSelection; + +typedef struct _EvPixbufCache EvPixbufCache; +typedef struct _EvPixbufCacheClass EvPixbufCacheClass; + +GType ev_pixbuf_cache_get_type (void) G_GNUC_CONST; +EvPixbufCache *ev_pixbuf_cache_new (GtkWidget *view, + EvDocumentModel *model, + gsize max_size); +void ev_pixbuf_cache_set_max_size (EvPixbufCache *pixbuf_cache, + gsize max_size); +void ev_pixbuf_cache_set_page_range (EvPixbufCache *pixbuf_cache, + gint start_page, + gint end_page, + GList *selection_list); +cairo_surface_t *ev_pixbuf_cache_get_surface (EvPixbufCache *pixbuf_cache, + gint page); +void ev_pixbuf_cache_clear (EvPixbufCache *pixbuf_cache); +void ev_pixbuf_cache_style_changed (EvPixbufCache *pixbuf_cache); +void ev_pixbuf_cache_reload_page (EvPixbufCache *pixbuf_cache, + cairo_region_t *region, + gint page, + gint rotation, + gdouble scale); +void ev_pixbuf_cache_set_inverted_colors (EvPixbufCache *pixbuf_cache, + gboolean inverted_colors); +/* Selection */ +cairo_surface_t *ev_pixbuf_cache_get_selection_surface (EvPixbufCache *pixbuf_cache, + gint page, + gfloat scale, + cairo_region_t **region); +void ev_pixbuf_cache_set_selection_list (EvPixbufCache *pixbuf_cache, + GList *selection_list); +GList *ev_pixbuf_cache_get_selection_list (EvPixbufCache *pixbuf_cache); + +G_END_DECLS + +#endif /* __EV_PIXBUF_CACHE_H__ */ diff --git a/libview/ev-print-operation.c b/libview/ev-print-operation.c new file mode 100644 index 00000000..d913c7be --- /dev/null +++ b/libview/ev-print-operation.c @@ -0,0 +1,2028 @@ +/* this file is part of evince, a mate document viewer + * + * Copyright (C) 2008 Carlos Garcia Campos <[email protected]> + * + * Evince is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * Evince is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ + +#include <config.h> + +#include "ev-print-operation.h" + +#if GTKUNIXPRINT_ENABLED +#include <gtk/gtkunixprint.h> +#endif +#include <glib/gi18n.h> +#include <glib/gstdio.h> +#include <unistd.h> + +#include "ev-jobs.h" +#include "ev-job-scheduler.h" + +enum { + PROP_0, + PROP_DOCUMENT +}; + +enum { + DONE, + BEGIN_PRINT, + STATUS_CHANGED, + LAST_SIGNAL +}; + +static guint signals[LAST_SIGNAL] = { 0 }; + +struct _EvPrintOperation { + GObject parent; + + EvDocument *document; + + /* Progress */ + gchar *status; + gdouble progress; +}; + +struct _EvPrintOperationClass { + GObjectClass parent_class; + + void (* set_current_page) (EvPrintOperation *op, + gint current_page); + void (* set_print_settings) (EvPrintOperation *op, + GtkPrintSettings *print_settings); + GtkPrintSettings *(* get_print_settings) (EvPrintOperation *op); + void (* set_default_page_setup) (EvPrintOperation *op, + GtkPageSetup *page_setup); + GtkPageSetup *(* get_default_page_setup) (EvPrintOperation *op); + void (* set_job_name) (EvPrintOperation *op, + const gchar *job_name); + const gchar *(* get_job_name) (EvPrintOperation *op); + void (* run) (EvPrintOperation *op, + GtkWindow *parent); + void (* cancel) (EvPrintOperation *op); + void (* get_error) (EvPrintOperation *op, + GError **error); + void (* set_embed_page_setup) (EvPrintOperation *op, + gboolean embed); + gboolean (* get_embed_page_setup) (EvPrintOperation *op); + + /* signals */ + void (* done) (EvPrintOperation *op, + GtkPrintOperationResult result); + void (* begin_print) (EvPrintOperation *op); + void (* status_changed) (EvPrintOperation *op); +}; + +G_DEFINE_ABSTRACT_TYPE (EvPrintOperation, ev_print_operation, G_TYPE_OBJECT) + +static void +ev_print_operation_finalize (GObject *object) +{ + EvPrintOperation *op = EV_PRINT_OPERATION (object); + + if (op->document) { + g_object_unref (op->document); + op->document = NULL; + } + + if (op->status) { + g_free (op->status); + op->status = NULL; + } + + (* G_OBJECT_CLASS (ev_print_operation_parent_class)->finalize) (object); +} + +static void +ev_print_operation_set_property (GObject *object, + guint prop_id, + const GValue *value, + GParamSpec *pspec) +{ + EvPrintOperation *op = EV_PRINT_OPERATION (object); + + switch (prop_id) { + case PROP_DOCUMENT: + op->document = g_value_dup_object (value); + break; + default: + G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); + } +} + +static void +ev_print_operation_init (EvPrintOperation *op) +{ +} + +static void +ev_print_operation_class_init (EvPrintOperationClass *klass) +{ + GObjectClass *g_object_class = G_OBJECT_CLASS (klass); + + g_object_class->set_property = ev_print_operation_set_property; + g_object_class->finalize = ev_print_operation_finalize; + + g_object_class_install_property (g_object_class, + PROP_DOCUMENT, + g_param_spec_object ("document", + "Document", + "The document to print", + EV_TYPE_DOCUMENT, + G_PARAM_WRITABLE | + G_PARAM_CONSTRUCT_ONLY)); + signals[DONE] = + g_signal_new ("done", + G_TYPE_FROM_CLASS (g_object_class), + G_SIGNAL_RUN_LAST, + G_STRUCT_OFFSET (EvPrintOperationClass, done), + NULL, NULL, + g_cclosure_marshal_VOID__ENUM, + G_TYPE_NONE, 1, + GTK_TYPE_PRINT_OPERATION_RESULT); + signals[BEGIN_PRINT] = + g_signal_new ("begin_print", + G_TYPE_FROM_CLASS (g_object_class), + G_SIGNAL_RUN_LAST, + G_STRUCT_OFFSET (EvPrintOperationClass, begin_print), + NULL, NULL, + g_cclosure_marshal_VOID__VOID, + G_TYPE_NONE, 0); + signals[STATUS_CHANGED] = + g_signal_new ("status_changed", + G_TYPE_FROM_CLASS (g_object_class), + G_SIGNAL_RUN_LAST, + G_STRUCT_OFFSET (EvPrintOperationClass, status_changed), + NULL, NULL, + g_cclosure_marshal_VOID__VOID, + G_TYPE_NONE, 0); +} + +/* Public methods */ +void +ev_print_operation_set_current_page (EvPrintOperation *op, + gint current_page) +{ + EvPrintOperationClass *class = EV_PRINT_OPERATION_GET_CLASS (op); + + g_return_if_fail (EV_IS_PRINT_OPERATION (op)); + g_return_if_fail (current_page >= 0); + + class->set_current_page (op, current_page); +} + +void +ev_print_operation_set_print_settings (EvPrintOperation *op, + GtkPrintSettings *print_settings) +{ + EvPrintOperationClass *class = EV_PRINT_OPERATION_GET_CLASS (op); + + g_return_if_fail (EV_IS_PRINT_OPERATION (op)); + g_return_if_fail (GTK_IS_PRINT_SETTINGS (print_settings)); + + class->set_print_settings (op, print_settings); +} + +GtkPrintSettings * +ev_print_operation_get_print_settings (EvPrintOperation *op) +{ + EvPrintOperationClass *class = EV_PRINT_OPERATION_GET_CLASS (op); + + g_return_val_if_fail (EV_IS_PRINT_OPERATION (op), NULL); + + return class->get_print_settings (op); +} + +void +ev_print_operation_set_default_page_setup (EvPrintOperation *op, + GtkPageSetup *page_setup) +{ + EvPrintOperationClass *class = EV_PRINT_OPERATION_GET_CLASS (op); + + g_return_if_fail (EV_IS_PRINT_OPERATION (op)); + g_return_if_fail (GTK_IS_PAGE_SETUP (page_setup)); + + class->set_default_page_setup (op, page_setup); +} + +GtkPageSetup * +ev_print_operation_get_default_page_setup (EvPrintOperation *op) +{ + EvPrintOperationClass *class = EV_PRINT_OPERATION_GET_CLASS (op); + + g_return_val_if_fail (EV_IS_PRINT_OPERATION (op), NULL); + + return class->get_default_page_setup (op); +} + +void +ev_print_operation_set_job_name (EvPrintOperation *op, + const gchar *job_name) +{ + EvPrintOperationClass *class = EV_PRINT_OPERATION_GET_CLASS (op); + + g_return_if_fail (EV_IS_PRINT_OPERATION (op)); + g_return_if_fail (job_name != NULL); + + class->set_job_name (op, job_name); +} + +const gchar * +ev_print_operation_get_job_name (EvPrintOperation *op) +{ + EvPrintOperationClass *class = EV_PRINT_OPERATION_GET_CLASS (op); + + g_return_val_if_fail (EV_IS_PRINT_OPERATION (op), NULL); + + return class->get_job_name (op); +} + +void +ev_print_operation_run (EvPrintOperation *op, + GtkWindow *parent) +{ + EvPrintOperationClass *class = EV_PRINT_OPERATION_GET_CLASS (op); + + g_return_if_fail (EV_IS_PRINT_OPERATION (op)); + + class->run (op, parent); +} + +void +ev_print_operation_cancel (EvPrintOperation *op) +{ + EvPrintOperationClass *class = EV_PRINT_OPERATION_GET_CLASS (op); + + g_return_if_fail (EV_IS_PRINT_OPERATION (op)); + + class->cancel (op); +} + +void +ev_print_operation_get_error (EvPrintOperation *op, + GError **error) +{ + EvPrintOperationClass *class = EV_PRINT_OPERATION_GET_CLASS (op); + + g_return_if_fail (EV_IS_PRINT_OPERATION (op)); + + class->get_error (op, error); +} + +void +ev_print_operation_set_embed_page_setup (EvPrintOperation *op, + gboolean embed) +{ + EvPrintOperationClass *class = EV_PRINT_OPERATION_GET_CLASS (op); + + g_return_if_fail (EV_IS_PRINT_OPERATION (op)); + + class->set_embed_page_setup (op, embed); +} + +gboolean +ev_print_operation_get_embed_page_setup (EvPrintOperation *op) +{ + EvPrintOperationClass *class = EV_PRINT_OPERATION_GET_CLASS (op); + + g_return_val_if_fail (EV_IS_PRINT_OPERATION (op), FALSE); + + return class->get_embed_page_setup (op); +} + +const gchar * +ev_print_operation_get_status (EvPrintOperation *op) +{ + g_return_val_if_fail (EV_IS_PRINT_OPERATION (op), NULL); + + return op->status ? op->status : ""; +} + +gdouble +ev_print_operation_get_progress (EvPrintOperation *op) +{ + g_return_val_if_fail (EV_IS_PRINT_OPERATION (op), 0.0); + + return op->progress; +} + +static void +ev_print_operation_update_status (EvPrintOperation *op, + gint page, + gint n_pages, + gdouble progress) +{ + if (op->status && op->progress == progress) + return; + + g_free (op->status); + + if (page == -1) { + /* Initial state */ + op->status = g_strdup (_("Preparing to printā¦")); + } else if (page > n_pages) { + op->status = g_strdup (_("Finishingā¦")); + } else { + op->status = g_strdup_printf (_("Printing page %d of %dā¦"), + page, n_pages); + } + + op->progress = MIN (1.0, progress); + + g_signal_emit (op, signals[STATUS_CHANGED], 0); +} + +#if GTKUNIXPRINT_ENABLED + +/* Export interface */ +#define EV_TYPE_PRINT_OPERATION_EXPORT (ev_print_operation_export_get_type()) +#define EV_PRINT_OPERATION_EXPORT(object) (G_TYPE_CHECK_INSTANCE_CAST((object), EV_TYPE_PRINT_OPERATION_EXPORT, EvPrintOperationExport)) +#define EV_PRINT_OPERATION_EXPORT_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST((klass), EV_TYPE_PRINT_OPERATION_EXPORT, EvPrintOperationExportClass)) +#define EV_IS_PRINT_OPERATION_EXPORT(object) (G_TYPE_CHECK_INSTANCE_TYPE((object), EV_TYPE_PRINT_OPERATION_EXPORT)) + +typedef struct _EvPrintOperationExport EvPrintOperationExport; +typedef struct _EvPrintOperationExportClass EvPrintOperationExportClass; + +static GType ev_print_operation_export_get_type (void) G_GNUC_CONST; + +static void ev_print_operation_export_begin (EvPrintOperationExport *export); +static gboolean export_print_page (EvPrintOperationExport *export); +static void export_cancel (EvPrintOperationExport *export); + +struct _EvPrintOperationExport { + EvPrintOperation parent; + + GtkWindow *parent_window; + EvJob *job_export; + GError *error; + + gboolean print_preview; + gint n_pages; + gint current_page; + GtkPrinter *printer; + GtkPageSetup *page_setup; + GtkPrintSettings *print_settings; + GtkPageSet page_set; + gint copies; + guint collate : 1; + guint reverse : 1; + gint pages_per_sheet; + gint fd; + gchar *temp_file; + gchar *job_name; + gboolean embed_page_setup; + + guint idle_id; + + /* Context */ + EvFileExporterContext fc; + gint n_pages_to_print; + gint uncollated_copies; + gint collated_copies; + gint uncollated, collated, total; + + gint sheet, page_count; + + gint range, n_ranges; + GtkPageRange *ranges; + GtkPageRange one_range; + + gint page, start, end, inc; +}; + +struct _EvPrintOperationExportClass { + EvPrintOperationClass parent_class; +}; + +G_DEFINE_TYPE (EvPrintOperationExport, ev_print_operation_export, EV_TYPE_PRINT_OPERATION) + +/* Internal print queue */ +static GHashTable *print_queue = NULL; + +static void +queue_free (GQueue *queue) +{ + g_queue_foreach (queue, (GFunc)g_object_unref, NULL); + g_queue_free (queue); +} + +static void +ev_print_queue_init (void) +{ + if (G_UNLIKELY (print_queue == NULL)) { + print_queue = g_hash_table_new_full (g_direct_hash, + g_direct_equal, + NULL, + (GDestroyNotify)queue_free); + } +} + +static void +remove_document_queue (gpointer data, + GObject *document) +{ + if (print_queue) + g_hash_table_remove (print_queue, document); +} + +static gboolean +ev_print_queue_is_empty (EvDocument *document) +{ + GQueue *queue; + + queue = g_hash_table_lookup (print_queue, document); + return (!queue || g_queue_is_empty (queue)); +} + +static void +ev_print_queue_push (EvPrintOperation *op) +{ + GQueue *queue; + + queue = g_hash_table_lookup (print_queue, op->document); + if (!queue) { + queue = g_queue_new (); + g_hash_table_insert (print_queue, + op->document, + queue); + g_object_weak_ref (G_OBJECT (op->document), + (GWeakNotify)remove_document_queue, + NULL); + } + + g_queue_push_head (queue, g_object_ref (op)); +} + +static EvPrintOperation * +ev_print_queue_pop (EvDocument *document) +{ + EvPrintOperation *op; + GQueue *queue; + + queue = g_hash_table_lookup (print_queue, document); + if (!queue || g_queue_is_empty (queue)) + return NULL; + + op = g_queue_pop_tail (queue); + g_object_unref (op); + + return op; +} + +static EvPrintOperation * +ev_print_queue_peek (EvDocument *document) +{ + GQueue *queue; + + queue = g_hash_table_lookup (print_queue, document); + if (!queue || g_queue_is_empty (queue)) + return NULL; + + return g_queue_peek_tail (queue); +} + +static void +ev_print_operation_export_set_current_page (EvPrintOperation *op, + gint current_page) +{ + EvPrintOperationExport *export = EV_PRINT_OPERATION_EXPORT (op); + + g_return_if_fail (current_page < export->n_pages); + + export->current_page = current_page; +} + +static void +ev_print_operation_export_set_print_settings (EvPrintOperation *op, + GtkPrintSettings *print_settings) +{ + EvPrintOperationExport *export = EV_PRINT_OPERATION_EXPORT (op); + + if (print_settings == export->print_settings) + return; + + g_object_ref (print_settings); + if (export->print_settings) + g_object_unref (export->print_settings); + export->print_settings = print_settings; +} + +static GtkPrintSettings * +ev_print_operation_export_get_print_settings (EvPrintOperation *op) +{ + EvPrintOperationExport *export = EV_PRINT_OPERATION_EXPORT (op); + + return export->print_settings; +} + +static void +ev_print_operation_export_set_default_page_setup (EvPrintOperation *op, + GtkPageSetup *page_setup) +{ + EvPrintOperationExport *export = EV_PRINT_OPERATION_EXPORT (op); + + if (page_setup == export->page_setup) + return; + + g_object_ref (page_setup); + if (export->page_setup) + g_object_unref (export->page_setup); + export->page_setup = page_setup; +} + +static GtkPageSetup * +ev_print_operation_export_get_default_page_setup (EvPrintOperation *op) +{ + EvPrintOperationExport *export = EV_PRINT_OPERATION_EXPORT (op); + + return export->page_setup; +} + +static void +ev_print_operation_export_set_job_name (EvPrintOperation *op, + const gchar *job_name) +{ + EvPrintOperationExport *export = EV_PRINT_OPERATION_EXPORT (op); + + g_free (export->job_name); + export->job_name = g_strdup (job_name); +} + +static const gchar * +ev_print_operation_export_get_job_name (EvPrintOperation *op) +{ + EvPrintOperationExport *export = EV_PRINT_OPERATION_EXPORT (op); + + return export->job_name; +} + +static void +ev_print_operation_export_set_printer (EvPrintOperationExport *export, + GtkPrinter *printer) +{ + if (printer == export->printer) + return; + + g_object_ref (printer); + if (export->printer) + g_object_unref (export->printer); + export->printer = printer; +} + +static void +find_range (EvPrintOperationExport *export) +{ + GtkPageRange *range; + + range = &export->ranges[export->range]; + + if (export->inc < 0) { + export->start = range->end; + export->end = range->start - 1; + } else { + export->start = range->start; + export->end = range->end + 1; + } +} + +static gboolean +clamp_ranges (EvPrintOperationExport *export) +{ + gint num_of_correct_ranges = 0; + gint n_pages_to_print = 0; + gint i; + gboolean null_flag = FALSE; + + for (i = 0; i < export->n_ranges; i++) { + gint n_pages; + + if ((export->ranges[i].start >= 0) && + (export->ranges[i].start < export->n_pages) && + (export->ranges[i].end >= 0) && + (export->ranges[i].end < export->n_pages)) { + export->ranges[num_of_correct_ranges] = export->ranges[i]; + num_of_correct_ranges++; + } else if ((export->ranges[i].start >= 0) && + (export->ranges[i].start < export->n_pages) && + (export->ranges[i].end >= export->n_pages)) { + export->ranges[i].end = export->n_pages - 1; + export->ranges[num_of_correct_ranges] = export->ranges[i]; + num_of_correct_ranges++; + } else if ((export->ranges[i].end >= 0) && + (export->ranges[i].end < export->n_pages) && + (export->ranges[i].start < 0)) { + export->ranges[i].start = 0; + export->ranges[num_of_correct_ranges] = export->ranges[i]; + num_of_correct_ranges++; + } + + n_pages = export->ranges[i].end - export->ranges[i].start + 1; + if (export->page_set == GTK_PAGE_SET_ALL) { + n_pages_to_print += n_pages; + } else if (n_pages % 2 == 0) { + n_pages_to_print += n_pages / 2; + } else if (export->page_set == GTK_PAGE_SET_EVEN) { + if (n_pages==1 && export->ranges[i].start % 2 == 0) + null_flag = TRUE; + else + n_pages_to_print += export->ranges[i].start % 2 == 0 ? + n_pages / 2 : (n_pages / 2) + 1; + } else if (export->page_set == GTK_PAGE_SET_ODD) { + if (n_pages==1 && export->ranges[i].start % 2 != 0) + null_flag = TRUE; + else + n_pages_to_print += export->ranges[i].start % 2 == 0 ? + (n_pages / 2) + 1 : n_pages / 2; + } + } + + if (null_flag && !n_pages_to_print) { + return FALSE; + } else { + export->n_ranges = num_of_correct_ranges; + export->n_pages_to_print = n_pages_to_print; + return TRUE; + } +} + +static void +get_first_and_last_page (EvPrintOperationExport *export, + gint *first, + gint *last) +{ + gint i; + gint first_page = G_MAXINT; + gint last_page = G_MININT; + gint max_page = export->n_pages - 1; + + if (export->n_ranges == 0) { + *first = 0; + *last = max_page; + + return; + } + + for (i = 0; i < export->n_ranges; i++) { + if (export->ranges[i].start < first_page) + first_page = export->ranges[i].start; + if (export->ranges[i].end > last_page) + last_page = export->ranges[i].end; + } + + *first = MAX (0, first_page); + *last = MIN (max_page, last_page); +} + +static gboolean +export_print_inc_page (EvPrintOperationExport *export) +{ + do { + export->page += export->inc; + + /* note: when NOT collating, page_count is increased in export_print_page */ + if (export->collate) { + export->page_count++; + export->sheet = 1 + (export->page_count - 1) / export->pages_per_sheet; + } + + if (export->page == export->end) { + export->range += export->inc; + if (export->range == -1 || export->range == export->n_ranges) { + export->uncollated++; + + /* when printing multiple collated copies & multiple pages per sheet we want to + * prevent the next copy bleeding into the last sheet of the previous one + * we've reached the last range to be printed now, so this is the time to do it */ + if (export->pages_per_sheet > 1 && export->collate == 1 && + (export->page_count - 1) % export->pages_per_sheet != 0) { + + EvPrintOperation *op = EV_PRINT_OPERATION (export); + ev_document_doc_mutex_lock (); + + /* keep track of all blanks but only actualise those + * which are in the current odd / even sheet set */ + + export->page_count += export->pages_per_sheet - (export->page_count - 1) % export->pages_per_sheet; + if (export->page_set == GTK_PAGE_SET_ALL || + (export->page_set == GTK_PAGE_SET_EVEN && export->sheet % 2 == 0) || + (export->page_set == GTK_PAGE_SET_ODD && export->sheet % 2 == 1) ) { + ev_file_exporter_end_page (EV_FILE_EXPORTER (op->document)); + } + ev_document_doc_mutex_unlock (); + export->sheet = 1 + (export->page_count - 1) / export->pages_per_sheet; + } + + if (export->uncollated == export->uncollated_copies) + return FALSE; + + export->range = export->inc < 0 ? export->n_ranges - 1 : 0; + } + find_range (export); + export->page = export->start; + } + + /* in/decrement the page number until we reach the first page on the next EVEN or ODD sheet + * if we're not collating, we have to make sure that this is done only once! */ + } while ( export->collate == 1 && + ((export->page_set == GTK_PAGE_SET_EVEN && export->sheet % 2 == 1) || + (export->page_set == GTK_PAGE_SET_ODD && export->sheet % 2 == 0))); + + return TRUE; +} + +static void +ev_print_operation_export_clear_temp_file (EvPrintOperationExport *export) +{ + if (!export->temp_file) + return; + + g_unlink (export->temp_file); + g_free (export->temp_file); + export->temp_file = NULL; +} + +static void +ev_print_operation_export_run_next (EvPrintOperationExport *export) +{ + EvPrintOperation *op = EV_PRINT_OPERATION (export); + EvPrintOperation *next; + EvDocument *document; + + /* First pop the current job */ + document = op->document; + ev_print_queue_pop (document); + + next = ev_print_queue_peek (document); + if (next) + ev_print_operation_export_begin (EV_PRINT_OPERATION_EXPORT (next)); +} + +static void +gtk_print_job_finished (GtkPrintJob *print_job, + EvPrintOperationExport *export, + GError *error) +{ + EvPrintOperation *op = EV_PRINT_OPERATION (export); + + if (error) { + g_set_error_literal (&export->error, + GTK_PRINT_ERROR, + GTK_PRINT_ERROR_GENERAL, + error->message); + g_signal_emit (op, signals[DONE], 0, GTK_PRINT_OPERATION_RESULT_ERROR); + } else { + g_signal_emit (op, signals[DONE], 0, GTK_PRINT_OPERATION_RESULT_APPLY); + } + + ev_print_operation_export_clear_temp_file (export); + g_object_unref (print_job); + + ev_print_operation_export_run_next (export); +} + +static void +export_print_done (EvPrintOperationExport *export) +{ + EvPrintOperation *op = EV_PRINT_OPERATION (export); + GtkPrintSettings *settings; + EvFileExporterCapabilities capabilities; + GError *error = NULL; + + g_assert (export->temp_file != NULL); + + /* Some printers take into account some print settings, + * and others don't. However we have exported the document + * to a ps or pdf file according to such print settings. So, + * we want to send the exported file to printer with those + * settings set to default values. + */ + settings = gtk_print_settings_copy (export->print_settings); + capabilities = ev_file_exporter_get_capabilities (EV_FILE_EXPORTER (op->document)); + + gtk_print_settings_set_page_ranges (settings, NULL, 0); + gtk_print_settings_set_print_pages (settings, GTK_PRINT_PAGES_ALL); + if (capabilities & EV_FILE_EXPORTER_CAN_COPIES) + gtk_print_settings_set_n_copies (settings, 1); + if (capabilities & EV_FILE_EXPORTER_CAN_PAGE_SET) + gtk_print_settings_set_page_set (settings, GTK_PAGE_SET_ALL); + if (capabilities & EV_FILE_EXPORTER_CAN_SCALE) + gtk_print_settings_set_scale (settings, 1.0); + if (capabilities & EV_FILE_EXPORTER_CAN_COLLATE) + gtk_print_settings_set_collate (settings, FALSE); + if (capabilities & EV_FILE_EXPORTER_CAN_REVERSE) + gtk_print_settings_set_reverse (settings, FALSE); + if (capabilities & EV_FILE_EXPORTER_CAN_NUMBER_UP) { + gtk_print_settings_set_number_up (settings, 1); + gtk_print_settings_set_int (settings, "cups-"GTK_PRINT_SETTINGS_NUMBER_UP, 1); + } + + if (export->print_preview) { + GKeyFile *key_file; + gchar *data = NULL; + gsize data_len; + gchar *print_settings_file = NULL; + + key_file = g_key_file_new (); + + gtk_print_settings_to_key_file (settings, key_file, NULL); + gtk_page_setup_to_key_file (export->page_setup, key_file, NULL); + g_key_file_set_string (key_file, "Print Job", "title", export->job_name); + + data = g_key_file_to_data (key_file, &data_len, &error); + if (data) { + gint fd; + + fd = g_file_open_tmp ("print-settingsXXXXXX", &print_settings_file, &error); + if (!error) + g_file_set_contents (print_settings_file, data, data_len, &error); + close (fd); + + g_free (data); + } + + g_key_file_free (key_file); + + if (!error) { + gint argc; + gchar **argv; + gchar *cmd; + gchar *quoted_filename; + gchar *quoted_settings_filename; + + quoted_filename = g_shell_quote (export->temp_file); + quoted_settings_filename = g_shell_quote (print_settings_file); + cmd = g_strdup_printf ("evince-previewer --unlink-tempfile --print-settings %s %s", + quoted_settings_filename, quoted_filename); + + g_shell_parse_argv (cmd, &argc, &argv, &error); + + g_free (quoted_filename); + g_free (quoted_settings_filename); + g_free (cmd); + + if (!error) { + gdk_spawn_on_screen (gtk_window_get_screen (export->parent_window), + NULL, argv, NULL, + G_SPAWN_SEARCH_PATH, + NULL, NULL, NULL, + &error); + } + + g_strfreev (argv); + } + + if (error) { + if (print_settings_file) + g_unlink (print_settings_file); + g_free (print_settings_file); + } else { + g_signal_emit (op, signals[DONE], 0, GTK_PRINT_OPERATION_RESULT_APPLY); + /* temp_file will be deleted by the previewer */ + + ev_print_operation_export_run_next (export); + } + } else { + GtkPrintJob *job; + + job = gtk_print_job_new (export->job_name, + export->printer, + settings, + export->page_setup); + gtk_print_job_set_source_file (job, export->temp_file, &error); + if (!error){ + gtk_print_job_send (job, + (GtkPrintJobCompleteFunc)gtk_print_job_finished, + g_object_ref (export), + (GDestroyNotify)g_object_unref); + } + } + g_object_unref (settings); + + if (error) { + g_set_error_literal (&export->error, + GTK_PRINT_ERROR, + GTK_PRINT_ERROR_GENERAL, + error->message); + g_error_free (error); + ev_print_operation_export_clear_temp_file (export); + g_signal_emit (op, signals[DONE], 0, GTK_PRINT_OPERATION_RESULT_ERROR); + + ev_print_operation_export_run_next (export); + } +} + +static void +export_print_page_idle_finished (EvPrintOperationExport *export) +{ + export->idle_id = 0; +} + +static void +export_job_finished (EvJobExport *job, + EvPrintOperationExport *export) +{ + EvPrintOperation *op = EV_PRINT_OPERATION (export); + + if (export->pages_per_sheet == 1 || + ( export->page_count % export->pages_per_sheet == 0 && + ( export->page_set == GTK_PAGE_SET_ALL || + ( export->page_set == GTK_PAGE_SET_EVEN && export->sheet % 2 == 0 ) || + ( export->page_set == GTK_PAGE_SET_ODD && export->sheet % 2 == 1 ) ) ) ) { + + ev_document_doc_mutex_lock (); + ev_file_exporter_end_page (EV_FILE_EXPORTER (op->document)); + ev_document_doc_mutex_unlock (); + } + + /* Reschedule */ + export->idle_id = g_idle_add_full (G_PRIORITY_DEFAULT_IDLE, + (GSourceFunc)export_print_page, + export, + (GDestroyNotify)export_print_page_idle_finished); +} + +static void +export_job_cancelled (EvJobExport *job, + EvPrintOperationExport *export) +{ + export_cancel (export); +} + +static void +export_cancel (EvPrintOperationExport *export) +{ + EvPrintOperation *op = EV_PRINT_OPERATION (export); + + if (export->idle_id > 0) + g_source_remove (export->idle_id); + export->idle_id = 0; + + if (export->job_export) { + g_signal_handlers_disconnect_by_func (export->job_export, + export_job_finished, + export); + g_signal_handlers_disconnect_by_func (export->job_export, + export_job_cancelled, + export); + g_object_unref (export->job_export); + export->job_export = NULL; + } + + if (export->fd != -1) { + close (export->fd); + export->fd = -1; + } + + ev_print_operation_export_clear_temp_file (export); + + g_signal_emit (op, signals[DONE], 0, GTK_PRINT_OPERATION_RESULT_CANCEL); + + ev_print_operation_export_run_next (export); +} + +static void +update_progress (EvPrintOperationExport *export) +{ + EvPrintOperation *op = EV_PRINT_OPERATION (export); + + ev_print_operation_update_status (op, export->total, + export->n_pages_to_print, + export->total / (gdouble)export->n_pages_to_print); +} + +static gboolean +export_print_page (EvPrintOperationExport *export) +{ + EvPrintOperation *op = EV_PRINT_OPERATION (export); + + if (!export->temp_file) + return FALSE; /* cancelled */ + + export->total++; + export->collated++; + + /* note: when collating, page_count is increased in export_print_inc_page */ + if (!export->collate) { + export->page_count++; + export->sheet = 1 + (export->page_count - 1) / export->pages_per_sheet; + } + + if (export->collated == export->collated_copies) { + export->collated = 0; + if (!export_print_inc_page (export)) { + ev_document_doc_mutex_lock (); + ev_file_exporter_end (EV_FILE_EXPORTER (op->document)); + ev_document_doc_mutex_unlock (); + + close (export->fd); + export->fd = -1; + update_progress (export); + export_print_done (export); + + return FALSE; + } + } + + /* we're not collating and we've reached a sheet from the wrong sheet set */ + if (!export->collate && + ((export->page_set == GTK_PAGE_SET_EVEN && export->sheet % 2 != 0) || + (export->page_set == GTK_PAGE_SET_ODD && export->sheet % 2 != 1))) { + + do { + export->page_count++; + export->collated++; + export->sheet = 1 + (export->page_count - 1) / export->pages_per_sheet; + + if (export->collated == export->collated_copies) { + export->collated = 0; + + if (!export_print_inc_page (export)) { + ev_document_doc_mutex_lock (); + ev_file_exporter_end (EV_FILE_EXPORTER (op->document)); + ev_document_doc_mutex_unlock (); + + close (export->fd); + export->fd = -1; + + update_progress (export); + + export_print_done (export); + return FALSE; + } + } + + } while ((export->page_set == GTK_PAGE_SET_EVEN && export->sheet % 2 != 0) || + (export->page_set == GTK_PAGE_SET_ODD && export->sheet % 2 != 1)); + + } + + if (export->pages_per_sheet == 1 || + (export->page_count % export->pages_per_sheet == 1 && + (export->page_set == GTK_PAGE_SET_ALL || + (export->page_set == GTK_PAGE_SET_EVEN && export->sheet % 2 == 0) || + (export->page_set == GTK_PAGE_SET_ODD && export->sheet % 2 == 1)))) { + ev_document_doc_mutex_lock (); + ev_file_exporter_begin_page (EV_FILE_EXPORTER (op->document)); + ev_document_doc_mutex_unlock (); + } + + if (!export->job_export) { + export->job_export = ev_job_export_new (op->document); + g_signal_connect (export->job_export, "finished", + G_CALLBACK (export_job_finished), + (gpointer)export); + g_signal_connect (export->job_export, "cancelled", + G_CALLBACK (export_job_cancelled), + (gpointer)export); + } + + ev_job_export_set_page (EV_JOB_EXPORT (export->job_export), export->page); + ev_job_scheduler_push_job (export->job_export, EV_JOB_PRIORITY_NONE); + + update_progress (export); + + return FALSE; +} + +static void +ev_print_operation_export_begin (EvPrintOperationExport *export) +{ + EvPrintOperation *op = EV_PRINT_OPERATION (export); + + if (!export->temp_file) + return; /* cancelled */ + + ev_document_doc_mutex_lock (); + ev_file_exporter_begin (EV_FILE_EXPORTER (op->document), &export->fc); + ev_document_doc_mutex_unlock (); + + export->idle_id = g_idle_add_full (G_PRIORITY_DEFAULT_IDLE, + (GSourceFunc)export_print_page, + export, + (GDestroyNotify)export_print_page_idle_finished); +} + +static void +ev_print_operation_export_print_dialog_response_cb (GtkDialog *dialog, + gint response, + EvPrintOperationExport *export) +{ + GtkPrintPages print_pages; + GtkPrintSettings *print_settings; + GtkPageSetup *page_setup; + GtkPrinter *printer; + gdouble scale; + gdouble width; + gdouble height; + gint first_page; + gint last_page; + const gchar *file_format; + gchar *filename; + GError *error = NULL; + EvPrintOperation *op = EV_PRINT_OPERATION (export); + + if (response != GTK_RESPONSE_OK && + response != GTK_RESPONSE_APPLY) { + gtk_widget_destroy (GTK_WIDGET (dialog)); + g_signal_emit (op, signals[DONE], 0, GTK_PRINT_OPERATION_RESULT_CANCEL); + + return; + } + + export->print_preview = (response == GTK_RESPONSE_APPLY); + + printer = gtk_print_unix_dialog_get_selected_printer (GTK_PRINT_UNIX_DIALOG (dialog)); + ev_print_operation_export_set_printer (export, printer); + + print_settings = gtk_print_unix_dialog_get_settings (GTK_PRINT_UNIX_DIALOG (dialog)); + ev_print_operation_export_set_print_settings (op, print_settings); + + page_setup = gtk_print_unix_dialog_get_page_setup (GTK_PRINT_UNIX_DIALOG (dialog)); + ev_print_operation_export_set_default_page_setup (op, page_setup); + + if (!gtk_printer_accepts_ps (export->printer)) { + gtk_widget_destroy (GTK_WIDGET (dialog)); + + g_set_error_literal (&export->error, + GTK_PRINT_ERROR, + GTK_PRINT_ERROR_GENERAL, + _("Printing is not supported on this printer.")); + g_signal_emit (op, signals[DONE], 0, GTK_PRINT_OPERATION_RESULT_ERROR); + + return; + } + + file_format = gtk_print_settings_get (print_settings, GTK_PRINT_SETTINGS_OUTPUT_FILE_FORMAT); + + filename = g_strdup_printf ("evince_print.%s.XXXXXX", file_format != NULL ? file_format : ""); + export->fd = g_file_open_tmp (filename, &export->temp_file, &error); + g_free (filename); + if (export->fd <= -1) { + gtk_widget_destroy (GTK_WIDGET (dialog)); + + g_set_error_literal (&export->error, + GTK_PRINT_ERROR, + GTK_PRINT_ERROR_GENERAL, + error->message); + g_error_free (error); + g_signal_emit (op, signals[DONE], 0, GTK_PRINT_OPERATION_RESULT_ERROR); + + return; + } + + export->current_page = gtk_print_unix_dialog_get_current_page (GTK_PRINT_UNIX_DIALOG (dialog)); + export->page_set = gtk_print_settings_get_page_set (print_settings); + print_pages = gtk_print_settings_get_print_pages (print_settings); + + switch (print_pages) { + case GTK_PRINT_PAGES_CURRENT: + export->ranges = &export->one_range; + + export->ranges[0].start = export->current_page; + export->ranges[0].end = export->current_page; + export->n_ranges = 1; + + break; + case GTK_PRINT_PAGES_RANGES: { + gint i; + + export->ranges = gtk_print_settings_get_page_ranges (print_settings, &export->n_ranges); + for (i = 0; i < export->n_ranges; i++) + if (export->ranges[i].end == -1 || export->ranges[i].end >= export->n_pages) + export->ranges[i].end = export->n_pages - 1; + } + break; + default: + g_warning ("Unsupported print pages setting\n"); + case GTK_PRINT_PAGES_ALL: + export->ranges = &export->one_range; + + export->ranges[0].start = 0; + export->ranges[0].end = export->n_pages - 1; + export->n_ranges = 1; + + break; + } + + if (export->n_ranges < 1 || !clamp_ranges (export)) { + GtkWidget *message_dialog; + + message_dialog = gtk_message_dialog_new (GTK_WINDOW (dialog), + GTK_DIALOG_MODAL, + GTK_MESSAGE_WARNING, + GTK_BUTTONS_CLOSE, + "%s", _("Invalid page selection")); + gtk_window_set_title (GTK_WINDOW (message_dialog), _("Warning")); + gtk_message_dialog_format_secondary_text (GTK_MESSAGE_DIALOG (message_dialog), + "%s", _("Your print range selection does not include any pages")); + g_signal_connect (message_dialog, "response", + G_CALLBACK (gtk_widget_destroy), + NULL); + gtk_widget_show (message_dialog); + + return; + } else ev_print_operation_update_status (op, -1, -1, 0.0); + + width = gtk_page_setup_get_paper_width (page_setup, GTK_UNIT_POINTS); + height = gtk_page_setup_get_paper_height (page_setup, GTK_UNIT_POINTS); + scale = gtk_print_settings_get_scale (print_settings) * 0.01; + if (scale != 1.0) { + width *= scale; + height *= scale; + } + + export->pages_per_sheet = MAX (1, gtk_print_settings_get_number_up (print_settings)); + + export->copies = gtk_print_settings_get_n_copies (print_settings); + export->collate = gtk_print_settings_get_collate (print_settings); + export->reverse = gtk_print_settings_get_reverse (print_settings); + + if (export->collate) { + export->uncollated_copies = export->copies; + export->collated_copies = 1; + } else { + export->uncollated_copies = 1; + export->collated_copies = export->copies; + } + + if (export->reverse) { + export->range = export->n_ranges - 1; + export->inc = -1; + } else { + export->range = 0; + export->inc = 1; + } + find_range (export); + + export->page = export->start - export->inc; + export->collated = export->collated_copies - 1; + + get_first_and_last_page (export, &first_page, &last_page); + + export->fc.format = file_format && g_ascii_strcasecmp (file_format, "pdf") == 0 ? + EV_FILE_FORMAT_PDF : EV_FILE_FORMAT_PS; + export->fc.filename = export->temp_file; + export->fc.first_page = MIN (first_page, last_page); + export->fc.last_page = MAX (first_page, last_page); + export->fc.paper_width = width; + export->fc.paper_height = height; + export->fc.duplex = FALSE; + export->fc.pages_per_sheet = export->pages_per_sheet; + + if (ev_print_queue_is_empty (op->document)) + ev_print_operation_export_begin (export); + + ev_print_queue_push (op); + + g_signal_emit (op, signals[BEGIN_PRINT], 0); + + gtk_widget_destroy (GTK_WIDGET (dialog)); +} + +static void +ev_print_operation_export_run (EvPrintOperation *op, + GtkWindow *parent) +{ + EvPrintOperationExport *export = EV_PRINT_OPERATION_EXPORT (op); + GtkWidget *dialog; + GtkPrintCapabilities capabilities; + + ev_print_queue_init (); + + export->parent_window = parent; + export->error = NULL; + + dialog = gtk_print_unix_dialog_new (_("Print"), parent); + gtk_window_set_modal (GTK_WINDOW (dialog), TRUE); + + capabilities = GTK_PRINT_CAPABILITY_PREVIEW | + ev_file_exporter_get_capabilities (EV_FILE_EXPORTER (op->document)); + gtk_print_unix_dialog_set_manual_capabilities (GTK_PRINT_UNIX_DIALOG (dialog), + capabilities); + + gtk_print_unix_dialog_set_embed_page_setup (GTK_PRINT_UNIX_DIALOG (dialog), + export->embed_page_setup); + + gtk_print_unix_dialog_set_current_page (GTK_PRINT_UNIX_DIALOG (dialog), + export->current_page); + + gtk_print_unix_dialog_set_settings (GTK_PRINT_UNIX_DIALOG (dialog), + export->print_settings); + + if (export->page_setup) + gtk_print_unix_dialog_set_page_setup (GTK_PRINT_UNIX_DIALOG (dialog), + export->page_setup); + + g_signal_connect (dialog, "response", + G_CALLBACK (ev_print_operation_export_print_dialog_response_cb), + export); + + gtk_window_present (GTK_WINDOW (dialog)); +} + +static void +ev_print_operation_export_cancel (EvPrintOperation *op) +{ + EvPrintOperationExport *export = EV_PRINT_OPERATION_EXPORT (op); + + if (export->job_export && + !ev_job_is_finished (export->job_export)) { + ev_job_cancel (export->job_export); + } else { + export_cancel (export); + } +} + +static void +ev_print_operation_export_get_error (EvPrintOperation *op, + GError **error) +{ + EvPrintOperationExport *export = EV_PRINT_OPERATION_EXPORT (op); + + g_propagate_error (error, export->error); + export->error = NULL; +} + +static void +ev_print_operation_export_set_embed_page_setup (EvPrintOperation *op, + gboolean embed) +{ + EvPrintOperationExport *export = EV_PRINT_OPERATION_EXPORT (op); + + export->embed_page_setup = embed; +} + +static gboolean +ev_print_operation_export_get_embed_page_setup (EvPrintOperation *op) +{ + EvPrintOperationExport *export = EV_PRINT_OPERATION_EXPORT (op); + + return export->embed_page_setup; +} + +static void +ev_print_operation_export_finalize (GObject *object) +{ + EvPrintOperationExport *export = EV_PRINT_OPERATION_EXPORT (object); + + if (export->idle_id > 0) { + g_source_remove (export->idle_id); + export->idle_id = 0; + } + + if (export->fd != -1) { + close (export->fd); + export->fd = -1; + } + + if (export->ranges) { + if (export->ranges != &export->one_range) + g_free (export->ranges); + export->ranges = NULL; + export->n_ranges = 0; + } + + if (export->temp_file) { + g_free (export->temp_file); + export->temp_file = NULL; + } + + if (export->job_name) { + g_free (export->job_name); + export->job_name = NULL; + } + + if (export->job_export) { + if (!ev_job_is_finished (export->job_export)) + ev_job_cancel (export->job_export); + g_signal_handlers_disconnect_by_func (export->job_export, + export_job_finished, + export); + g_signal_handlers_disconnect_by_func (export->job_export, + export_job_cancelled, + export); + g_object_unref (export->job_export); + export->job_export = NULL; + } + + if (export->error) { + g_error_free (export->error); + export->error = NULL; + } + + if (export->print_settings) { + g_object_unref (export->print_settings); + export->print_settings = NULL; + } + + if (export->page_setup) { + g_object_unref (export->page_setup); + export->page_setup = NULL; + } + + if (export->printer) { + g_object_unref (export->printer); + export->printer = NULL; + } + + (* G_OBJECT_CLASS (ev_print_operation_export_parent_class)->finalize) (object); +} + +static void +ev_print_operation_export_init (EvPrintOperationExport *export) +{ + /* sheets are counted from 1 to be physical */ + export->sheet = 1; +} + +static GObject * +ev_print_operation_export_constructor (GType type, + guint n_construct_properties, + GObjectConstructParam *construct_params) +{ + GObject *object; + EvPrintOperationExport *export; + EvPrintOperation *op; + + object = G_OBJECT_CLASS (ev_print_operation_export_parent_class)->constructor (type, + n_construct_properties, + construct_params); + export = EV_PRINT_OPERATION_EXPORT (object); + op = EV_PRINT_OPERATION (object); + export->n_pages = ev_document_get_n_pages (op->document); + + return object; +} + +static void +ev_print_operation_export_class_init (EvPrintOperationExportClass *klass) +{ + GObjectClass *g_object_class = G_OBJECT_CLASS (klass); + EvPrintOperationClass *ev_print_op_class = EV_PRINT_OPERATION_CLASS (klass); + + ev_print_op_class->set_current_page = ev_print_operation_export_set_current_page; + ev_print_op_class->set_print_settings = ev_print_operation_export_set_print_settings; + ev_print_op_class->get_print_settings = ev_print_operation_export_get_print_settings; + ev_print_op_class->set_default_page_setup = ev_print_operation_export_set_default_page_setup; + ev_print_op_class->get_default_page_setup = ev_print_operation_export_get_default_page_setup; + ev_print_op_class->set_job_name = ev_print_operation_export_set_job_name; + ev_print_op_class->get_job_name = ev_print_operation_export_get_job_name; + ev_print_op_class->run = ev_print_operation_export_run; + ev_print_op_class->cancel = ev_print_operation_export_cancel; + ev_print_op_class->get_error = ev_print_operation_export_get_error; + ev_print_op_class->set_embed_page_setup = ev_print_operation_export_set_embed_page_setup; + ev_print_op_class->get_embed_page_setup = ev_print_operation_export_get_embed_page_setup; + + g_object_class->constructor = ev_print_operation_export_constructor; + g_object_class->finalize = ev_print_operation_export_finalize; +} + +#endif /* GTKUNIXPRINT_ENABLED */ + +/* Print to cairo interface */ +#define EV_TYPE_PRINT_OPERATION_PRINT (ev_print_operation_print_get_type()) +#define EV_PRINT_OPERATION_PRINT(object) (G_TYPE_CHECK_INSTANCE_CAST((object), EV_TYPE_PRINT_OPERATION_PRINT, EvPrintOperationPrint)) +#define EV_PRINT_OPERATION_PRINT_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST((klass), EV_TYPE_PRINT_OPERATION_PRINT, EvPrintOperationPrintClass)) +#define EV_IS_PRINT_OPERATION_PRINT(object) (G_TYPE_CHECK_INSTANCE_TYPE((object), EV_TYPE_PRINT_OPERATION_PRINT)) + +typedef struct _EvPrintOperationPrint EvPrintOperationPrint; +typedef struct _EvPrintOperationPrintClass EvPrintOperationPrintClass; + +static GType ev_print_operation_print_get_type (void) G_GNUC_CONST; + +typedef enum { + EV_SCALE_NONE, + EV_SCALE_SHRINK_TO_PRINTABLE_AREA, + EV_SCALE_FIT_TO_PRINTABLE_AREA +} EvPrintScale; + +#define EV_PRINT_SETTING_PAGE_SCALE "evince-print-setting-page-scale" +#define EV_PRINT_SETTING_AUTOROTATE "evince-print-setting-page-autorotate" +#define EV_PRINT_SETTING_PAGE_SIZE "evince-print-setting-page-size" + +struct _EvPrintOperationPrint { + EvPrintOperation parent; + + GtkPrintOperation *op; + gint n_pages_to_print; + gint total; + EvJob *job_print; + gchar *job_name; + + /* Page handling tab */ + GtkWidget *scale_combo; + EvPrintScale page_scale; + GtkWidget *autorotate_button; + gboolean autorotate; + GtkWidget *source_button; + gboolean use_source_size; +}; + +struct _EvPrintOperationPrintClass { + EvPrintOperationClass parent_class; +}; + +G_DEFINE_TYPE (EvPrintOperationPrint, ev_print_operation_print, EV_TYPE_PRINT_OPERATION) + +static void +ev_print_operation_print_set_current_page (EvPrintOperation *op, + gint current_page) +{ + EvPrintOperationPrint *print = EV_PRINT_OPERATION_PRINT (op); + + gtk_print_operation_set_current_page (print->op, current_page); +} + +static void +ev_print_operation_print_set_print_settings (EvPrintOperation *op, + GtkPrintSettings *print_settings) +{ + EvPrintOperationPrint *print = EV_PRINT_OPERATION_PRINT (op); + + gtk_print_operation_set_print_settings (print->op, print_settings); +} + +static GtkPrintSettings * +ev_print_operation_print_get_print_settings (EvPrintOperation *op) +{ + EvPrintOperationPrint *print = EV_PRINT_OPERATION_PRINT (op); + + return gtk_print_operation_get_print_settings (print->op); +} + +static void +ev_print_operation_print_set_default_page_setup (EvPrintOperation *op, + GtkPageSetup *page_setup) +{ + EvPrintOperationPrint *print = EV_PRINT_OPERATION_PRINT (op); + + gtk_print_operation_set_default_page_setup (print->op, page_setup); +} + +static GtkPageSetup * +ev_print_operation_print_get_default_page_setup (EvPrintOperation *op) +{ + EvPrintOperationPrint *print = EV_PRINT_OPERATION_PRINT (op); + + return gtk_print_operation_get_default_page_setup (print->op); +} + +static void +ev_print_operation_print_set_job_name (EvPrintOperation *op, + const gchar *job_name) +{ + EvPrintOperationPrint *print = EV_PRINT_OPERATION_PRINT (op); + + g_free (print->job_name); + print->job_name = g_strdup (job_name); + + gtk_print_operation_set_job_name (print->op, print->job_name); +} + +static const gchar * +ev_print_operation_print_get_job_name (EvPrintOperation *op) +{ + EvPrintOperationPrint *print = EV_PRINT_OPERATION_PRINT (op); + + if (!print->job_name) { + gchar *name; + + g_object_get (print->op, "job_name", &name, NULL); + print->job_name = name; + } + + return print->job_name; +} + +static void +ev_print_operation_print_run (EvPrintOperation *op, + GtkWindow *parent) +{ + EvPrintOperationPrint *print = EV_PRINT_OPERATION_PRINT (op); + + gtk_print_operation_run (print->op, + GTK_PRINT_OPERATION_ACTION_PRINT_DIALOG, + parent, NULL); +} + +static void +ev_print_operation_print_cancel (EvPrintOperation *op) +{ + EvPrintOperationPrint *print = EV_PRINT_OPERATION_PRINT (op); + + gtk_print_operation_cancel (print->op); +} + +static void +ev_print_operation_print_get_error (EvPrintOperation *op, + GError **error) +{ + EvPrintOperationPrint *print = EV_PRINT_OPERATION_PRINT (op); + + gtk_print_operation_get_error (print->op, error); +} + +static void +ev_print_operation_print_set_embed_page_setup (EvPrintOperation *op, + gboolean embed) +{ + EvPrintOperationPrint *print = EV_PRINT_OPERATION_PRINT (op); + + gtk_print_operation_set_embed_page_setup (print->op, embed); +} + +static gboolean +ev_print_operation_print_get_embed_page_setup (EvPrintOperation *op) +{ + EvPrintOperationPrint *print = EV_PRINT_OPERATION_PRINT (op); + + return gtk_print_operation_get_embed_page_setup (print->op); +} + +static void +ev_print_operation_print_begin_print (EvPrintOperationPrint *print, + GtkPrintContext *context) +{ + EvPrintOperation *op = EV_PRINT_OPERATION (print); + gint n_pages; + + n_pages = ev_document_get_n_pages (op->document); + gtk_print_operation_set_n_pages (print->op, n_pages); + ev_print_operation_update_status (op, -1, n_pages, 0); + + g_signal_emit (op, signals[BEGIN_PRINT], 0); +} + +static void +ev_print_operation_print_done (EvPrintOperationPrint *print, + GtkPrintOperationResult result) +{ + EvPrintOperation *op = EV_PRINT_OPERATION (print); + + ev_print_operation_update_status (op, 0, print->n_pages_to_print, 1.0); + + g_signal_emit (op, signals[DONE], 0, result); +} + +static void +ev_print_operation_print_status_changed (EvPrintOperationPrint *print) +{ + GtkPrintStatus status; + + status = gtk_print_operation_get_status (print->op); + if (status == GTK_PRINT_STATUS_GENERATING_DATA) + print->n_pages_to_print = gtk_print_operation_get_n_pages_to_print (print->op); +} + +static void +print_job_finished (EvJobPrint *job, + EvPrintOperationPrint *print) +{ + EvPrintOperation *op = EV_PRINT_OPERATION (print); + + gtk_print_operation_draw_page_finish (print->op); + + print->total++; + ev_print_operation_update_status (op, print->total, + print->n_pages_to_print, + print->total / (gdouble)print->n_pages_to_print); + ev_job_print_set_cairo (job, NULL); +} + +static void +print_job_cancelled (EvJobPrint *job, + EvPrintOperationPrint *print) +{ + gtk_print_operation_cancel (print->op); +} + +static void +ev_print_operation_print_request_page_setup (EvPrintOperationPrint *print, + GtkPrintContext *context, + gint page_nr, + GtkPageSetup *setup) +{ + EvPrintOperation *op = EV_PRINT_OPERATION (print); + gdouble width, height; + GtkPaperSize *paper_size; + + ev_document_get_page_size (op->document, page_nr, + &width, &height); + + if (print->use_source_size) { + paper_size = gtk_paper_size_new_custom ("custom", "custom", + width, height, GTK_UNIT_POINTS); + gtk_page_setup_set_paper_size_and_default_margins (setup, paper_size); + gtk_paper_size_free (paper_size); + } + + if (print->autorotate) { + if (width > height) + gtk_page_setup_set_orientation (setup, GTK_PAGE_ORIENTATION_LANDSCAPE); + else + gtk_page_setup_set_orientation (setup, GTK_PAGE_ORIENTATION_PORTRAIT); + } +} + +static void +_print_context_get_hard_margins (GtkPrintContext *context, + gdouble *top, + gdouble *bottom, + gdouble *left, + gdouble *right) +{ + if (!gtk_print_context_get_hard_margins (context, top, bottom, left, right)) { + *top = 0; + *bottom = 0; + *left = 0; + *right = 0; + } +} + +static void +ev_print_operation_print_draw_page (EvPrintOperationPrint *print, + GtkPrintContext *context, + gint page) +{ + EvPrintOperation *op = EV_PRINT_OPERATION (print); + cairo_t *cr; + gdouble cr_width, cr_height; + gdouble width, height, scale; + gdouble x_scale, y_scale; + gdouble top, bottom, left, right; + + gtk_print_operation_set_defer_drawing (print->op); + + if (!print->job_print) { + print->job_print = ev_job_print_new (op->document); + g_signal_connect (G_OBJECT (print->job_print), "finished", + G_CALLBACK (print_job_finished), + (gpointer)print); + g_signal_connect (G_OBJECT (print->job_print), "cancelled", + G_CALLBACK (print_job_cancelled), + (gpointer)print); + } + + ev_job_print_set_page (EV_JOB_PRINT (print->job_print), page); + + cr = gtk_print_context_get_cairo_context (context); + cr_width = gtk_print_context_get_width (context); + cr_height = gtk_print_context_get_height (context); + ev_document_get_page_size (op->document, page, &width, &height); + + if (print->page_scale == EV_SCALE_NONE) { + /* Center document page on the printed page */ + if (print->autorotate) + cairo_translate (cr, (cr_width - width) / 2, (cr_height - height) / 2); + } else { + _print_context_get_hard_margins (context, &top, &bottom, &left, &right); + + x_scale = (cr_width - left - right) / width; + y_scale = (cr_height - top - bottom) / height; + + if (x_scale < y_scale) + scale = x_scale; + else + scale = y_scale; + + if (print->autorotate) { + double left_right_sides, top_bottom_sides; + + cairo_translate (cr, (cr_width - scale * width) / 2, + (cr_height - scale * height) / 2); + + /* Ensure document page is within the margins. The + * scale guarantees the document will fit in the + * margins so we just need to check each side and + * if it overhangs the margin, translate it to the + * margin. */ + left_right_sides = (cr_width - width*scale)/2; + top_bottom_sides = (cr_height - height*scale)/2; + if (left_right_sides < left) + cairo_translate (cr, left - left_right_sides, 0); + + if (left_right_sides < right) + cairo_translate (cr, -(right - left_right_sides), 0); + + if (top_bottom_sides < top) + cairo_translate (cr, 0, top - top_bottom_sides); + + if (top_bottom_sides < bottom) + cairo_translate (cr, 0, -(bottom - top_bottom_sides)); + } else { + cairo_translate (cr, left, top); + } + + if (print->page_scale == EV_SCALE_FIT_TO_PRINTABLE_AREA || scale < 1.0) { + cairo_scale (cr, scale, scale); + } + } + + ev_job_print_set_cairo (EV_JOB_PRINT (print->job_print), cr); + ev_job_scheduler_push_job (print->job_print, EV_JOB_PRIORITY_NONE); +} + +static GObject * +ev_print_operation_print_create_custom_widget (EvPrintOperationPrint *print, + GtkPrintContext *context) +{ + GtkPrintSettings *settings; + GtkWidget *label; + GtkWidget *table; + EvPrintScale page_scale; + gboolean autorotate; + gboolean use_source_size; + + settings = gtk_print_operation_get_print_settings (print->op); + page_scale = gtk_print_settings_get_int_with_default (settings, EV_PRINT_SETTING_PAGE_SCALE, 1); + autorotate = gtk_print_settings_has_key (settings, EV_PRINT_SETTING_AUTOROTATE) ? + gtk_print_settings_get_bool (settings, EV_PRINT_SETTING_AUTOROTATE) : + TRUE; + use_source_size = gtk_print_settings_get_bool (settings, EV_PRINT_SETTING_PAGE_SIZE); + + table = gtk_table_new (3, 2, FALSE); + gtk_table_set_row_spacings (GTK_TABLE (table), 6); + gtk_table_set_col_spacings (GTK_TABLE (table), 12); + gtk_container_set_border_width (GTK_CONTAINER (table), 12); + + label = gtk_label_new (_("Page Scaling:")); + gtk_table_attach (GTK_TABLE (table), label, 0, 1, 0, 1, GTK_FILL, 0, 0, 0); + gtk_widget_show (label); + + print->scale_combo = gtk_combo_box_new_text (); + gtk_combo_box_append_text (GTK_COMBO_BOX (print->scale_combo), _("None")); + gtk_combo_box_append_text (GTK_COMBO_BOX (print->scale_combo), _("Shrink to Printable Area")); + gtk_combo_box_append_text (GTK_COMBO_BOX (print->scale_combo), _("Fit to Printable Area")); + gtk_combo_box_set_active (GTK_COMBO_BOX (print->scale_combo), page_scale); + gtk_widget_set_tooltip_text (print->scale_combo, + _("Scale document pages to fit the selected printer page. Select from one of the following:\n" + "\n" + "ā¢ \"None\": No page scaling is performed.\n" + "\n" + "ā¢ \"Shrink to Printable Area\": Document pages larger than the printable area" + " are reduced to fit the printable area of the printer page.\n" + "\n" + "ā¢ \"Fit to Printable Area\": Document pages are enlarged or reduced as" + " required to fit the printable area of the printer page.\n")); + gtk_table_attach (GTK_TABLE (table), print->scale_combo, 1, 2, 0, 1, GTK_FILL, 0, 0, 0); + gtk_widget_show (print->scale_combo); + + print->autorotate_button = gtk_check_button_new_with_label (_("Auto Rotate and Center")); + gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (print->autorotate_button), autorotate); + gtk_widget_set_tooltip_text (print->autorotate_button, + _("Rotate printer page orientation of each page to match orientation of each document page. " + "Document pages will be centered within the printer page.")); + gtk_table_attach (GTK_TABLE (table), print->autorotate_button, 0, 2, 1, 2, GTK_FILL, 0, 0, 0); + gtk_widget_show (print->autorotate_button); + + print->source_button = gtk_check_button_new_with_label (_("Select page size using document page size")); + gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (print->source_button), use_source_size); + gtk_widget_set_tooltip_text (print->source_button, _("When enabled, each page will be printed on " + "the same size paper as the document page.")); + gtk_table_attach (GTK_TABLE (table), print->source_button, 0, 2, 2, 3, GTK_FILL, 0, 0, 0); + gtk_widget_show (print->source_button); + + return G_OBJECT (table); +} + +static void +ev_print_operation_print_custom_widget_apply (EvPrintOperationPrint *print, + GtkPrintContext *context) +{ + GtkPrintSettings *settings; + + print->page_scale = gtk_combo_box_get_active (GTK_COMBO_BOX (print->scale_combo)); + print->autorotate = gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (print->autorotate_button)); + print->use_source_size = gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (print->source_button)); + settings = gtk_print_operation_get_print_settings (print->op); + gtk_print_settings_set_int (settings, EV_PRINT_SETTING_PAGE_SCALE, print->page_scale); + gtk_print_settings_set_bool (settings, EV_PRINT_SETTING_AUTOROTATE, print->autorotate); + gtk_print_settings_set_bool (settings, EV_PRINT_SETTING_PAGE_SIZE, print->use_source_size); +} + +static void +ev_print_operation_print_finalize (GObject *object) +{ + EvPrintOperationPrint *print = EV_PRINT_OPERATION_PRINT (object); + + if (print->op) { + g_object_unref (print->op); + print->op = NULL; + } + + if (print->job_name) { + g_free (print->job_name); + print->job_name = NULL; + } + + if (print->job_print) { + if (!ev_job_is_finished (print->job_print)) + ev_job_cancel (print->job_print); + g_signal_handlers_disconnect_by_func (print->job_print, + print_job_finished, + print); + g_signal_handlers_disconnect_by_func (print->job_print, + print_job_cancelled, + print); + g_object_unref (print->job_print); + print->job_print = NULL; + } + + (* G_OBJECT_CLASS (ev_print_operation_print_parent_class)->finalize) (object); +} + +static void +ev_print_operation_print_init (EvPrintOperationPrint *print) +{ + print->op = gtk_print_operation_new (); + g_signal_connect_swapped (print->op, "begin_print", + G_CALLBACK (ev_print_operation_print_begin_print), + print); + g_signal_connect_swapped (print->op, "done", + G_CALLBACK (ev_print_operation_print_done), + print); + g_signal_connect_swapped (print->op, "draw_page", + G_CALLBACK (ev_print_operation_print_draw_page), + print); + g_signal_connect_swapped (print->op, "status_changed", + G_CALLBACK (ev_print_operation_print_status_changed), + print); + g_signal_connect_swapped (print->op, "request_page_setup", + G_CALLBACK (ev_print_operation_print_request_page_setup), + print); + g_signal_connect_swapped (print->op, "create_custom_widget", + G_CALLBACK (ev_print_operation_print_create_custom_widget), + print); + g_signal_connect_swapped (print->op, "custom_widget_apply", + G_CALLBACK (ev_print_operation_print_custom_widget_apply), + print); + gtk_print_operation_set_allow_async (print->op, TRUE); + gtk_print_operation_set_use_full_page (print->op, TRUE); + gtk_print_operation_set_unit (print->op, GTK_UNIT_POINTS); + gtk_print_operation_set_custom_tab_label (print->op, _("Page Handling")); +} + +static void +ev_print_operation_print_class_init (EvPrintOperationPrintClass *klass) +{ + GObjectClass *g_object_class = G_OBJECT_CLASS (klass); + EvPrintOperationClass *ev_print_op_class = EV_PRINT_OPERATION_CLASS (klass); + + ev_print_op_class->set_current_page = ev_print_operation_print_set_current_page; + ev_print_op_class->set_print_settings = ev_print_operation_print_set_print_settings; + ev_print_op_class->get_print_settings = ev_print_operation_print_get_print_settings; + ev_print_op_class->set_default_page_setup = ev_print_operation_print_set_default_page_setup; + ev_print_op_class->get_default_page_setup = ev_print_operation_print_get_default_page_setup; + ev_print_op_class->set_job_name = ev_print_operation_print_set_job_name; + ev_print_op_class->get_job_name = ev_print_operation_print_get_job_name; + ev_print_op_class->run = ev_print_operation_print_run; + ev_print_op_class->cancel = ev_print_operation_print_cancel; + ev_print_op_class->get_error = ev_print_operation_print_get_error; + ev_print_op_class->set_embed_page_setup = ev_print_operation_print_set_embed_page_setup; + ev_print_op_class->get_embed_page_setup = ev_print_operation_print_get_embed_page_setup; + + g_object_class->finalize = ev_print_operation_print_finalize; +} + +gboolean +ev_print_operation_exists_for_document (EvDocument *document) +{ +#if GTKUNIXPRINT_ENABLED + return (EV_IS_FILE_EXPORTER(document) || EV_IS_DOCUMENT_PRINT(document)); +#else + return EV_IS_DOCUMENT_PRINT(document); +#endif /* GTKUNIXPRINT_ENABLED */ +} + +/* Factory method */ +EvPrintOperation * +ev_print_operation_new (EvDocument *document) +{ + EvPrintOperation *op = NULL; + + g_return_val_if_fail (ev_print_operation_exists_for_document (document), NULL); + + if (EV_IS_DOCUMENT_PRINT (document)) + op = EV_PRINT_OPERATION (g_object_new (EV_TYPE_PRINT_OPERATION_PRINT, + "document", document, NULL)); + else +#if GTKUNIXPRINT_ENABLED + op = EV_PRINT_OPERATION (g_object_new (EV_TYPE_PRINT_OPERATION_EXPORT, + "document", document, NULL)); +#else + op = NULL; +#endif + return op; +} diff --git a/libview/ev-print-operation.h b/libview/ev-print-operation.h new file mode 100644 index 00000000..c01a0995 --- /dev/null +++ b/libview/ev-print-operation.h @@ -0,0 +1,71 @@ +/* this file is part of evince, a mate document viewer + * + * Copyright (C) 2008 Carlos Garcia Campos <[email protected]> + * + * Evince is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * Evince is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ + +#if !defined (__EV_EVINCE_VIEW_H_INSIDE__) && !defined (EVINCE_COMPILATION) +#error "Only <evince-view.h> can be included directly." +#endif + +#ifndef __EV_PRINT_OPERATION_H__ +#define __EV_PRINT_OPERATION_H__ + +#include <gtk/gtk.h> +#include <glib-object.h> + +#include <evince-document.h> + +G_BEGIN_DECLS + +typedef struct _EvPrintOperation EvPrintOperation; +typedef struct _EvPrintOperationClass EvPrintOperationClass; + +#define EV_TYPE_PRINT_OPERATION (ev_print_operation_get_type()) +#define EV_PRINT_OPERATION(object) (G_TYPE_CHECK_INSTANCE_CAST((object), EV_TYPE_PRINT_OPERATION, EvPrintOperation)) +#define EV_PRINT_OPERATION_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST((klass), EV_TYPE_PRINT_OPERATION, EvPrintOperationClass)) +#define EV_IS_PRINT_OPERATION(object) (G_TYPE_CHECK_INSTANCE_TYPE((object), EV_TYPE_PRINT_OPERATION)) +#define EV_PRINT_OPERATION_GET_CLASS(object) (G_TYPE_INSTANCE_GET_CLASS((object), EV_TYPE_PRINT_OPERATION, EvPrintOperationClass)) + +GType ev_print_operation_get_type (void) G_GNUC_CONST; + +gboolean ev_print_operation_exists_for_document (EvDocument *document); +EvPrintOperation *ev_print_operation_new (EvDocument *document); +void ev_print_operation_set_current_page (EvPrintOperation *op, + gint current_page); +void ev_print_operation_set_print_settings (EvPrintOperation *op, + GtkPrintSettings *print_settings); +GtkPrintSettings *ev_print_operation_get_print_settings (EvPrintOperation *op); +void ev_print_operation_set_default_page_setup (EvPrintOperation *op, + GtkPageSetup *page_setup); +GtkPageSetup *ev_print_operation_get_default_page_setup (EvPrintOperation *op); +void ev_print_operation_set_job_name (EvPrintOperation *op, + const gchar *job_name); +const gchar *ev_print_operation_get_job_name (EvPrintOperation *op); +void ev_print_operation_run (EvPrintOperation *op, + GtkWindow *parent); +void ev_print_operation_cancel (EvPrintOperation *op); +void ev_print_operation_get_error (EvPrintOperation *op, + GError **error); +void ev_print_operation_set_embed_page_setup (EvPrintOperation *op, + gboolean embed); +gboolean ev_print_operation_get_embed_page_setup (EvPrintOperation *op); +const gchar *ev_print_operation_get_status (EvPrintOperation *op); +gdouble ev_print_operation_get_progress (EvPrintOperation *op); + +G_END_DECLS + +#endif /* __EV_PRINT_OPERATION_H__ */ diff --git a/libview/ev-stock-icons.c b/libview/ev-stock-icons.c new file mode 100644 index 00000000..47b3943e --- /dev/null +++ b/libview/ev-stock-icons.c @@ -0,0 +1,143 @@ +/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */ +/* Stock icons for Evince + * + * Copyright (C) 2003 Martin Kretzschmar + * + * Author: + * Martin Kretzschmar <[email protected]> + * + * Evince is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * Evince is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public + * License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ + +#include <config.h> + +#include <gtk/gtk.h> + +#include "ev-stock-icons.h" + +typedef struct { + char *stock_id; + char *icon; +} EvStockIcon; + +/* Evince stock icons */ +static const EvStockIcon stock_icons [] = { + { EV_STOCK_ZOOM, "zoom" }, + { EV_STOCK_ZOOM_PAGE, "zoom-fit-height" }, + { EV_STOCK_ZOOM_WIDTH, "zoom-fit-width" }, + { EV_STOCK_VIEW_DUAL, "view-page-facing" }, + { EV_STOCK_VIEW_CONTINUOUS, "view-page-continuous" }, + { EV_STOCK_ROTATE_LEFT, "object-rotate-left"}, + { EV_STOCK_ROTATE_RIGHT, "object-rotate-right"}, + { EV_STOCK_RUN_PRESENTATION, "x-office-presentation"}, + { EV_STOCK_VISIBLE, "eye"}, + { EV_STOCK_RESIZE_SE, "resize-se"}, + { EV_STOCK_RESIZE_SW, "resize-sw"}, + { EV_STOCK_CLOSE, "close"}, + { EV_STOCK_INVERTED_COLORS, "stock_filters-invert"}, + { EV_STOCK_ATTACHMENT, "mail-attachment"} +}; + +static gchar *ev_icons_path; + +static void +ev_stock_icons_add_icons_path_for_screen (GdkScreen *screen) +{ + GtkIconTheme *icon_theme; + + g_return_if_fail (ev_icons_path != NULL); + + icon_theme = screen ? gtk_icon_theme_get_for_screen (screen) : gtk_icon_theme_get_default (); + if (icon_theme) { + gchar **path = NULL; + gint n_paths; + gint i; + + /* GtkIconTheme will then look in Evince custom hicolor dir + * for icons as well as the standard search paths + */ + gtk_icon_theme_get_search_path (icon_theme, &path, &n_paths); + for (i = n_paths - 1; i >= 0; i--) { + if (g_ascii_strcasecmp (ev_icons_path, path[i]) == 0) + break; + } + + if (i < 0) + gtk_icon_theme_append_search_path (icon_theme, + ev_icons_path); + + g_strfreev (path); + } +} + +/** + * ev_stock_icons_init: + * + * Creates a new icon factory, adding the base stock icons to it. + */ +void +ev_stock_icons_init (void) +{ + GtkIconFactory *factory; + GtkIconSource *source; + gint i; +#ifdef G_OS_WIN32 + gchar *dir; + + dir = g_win32_get_package_installation_directory_of_module (NULL); + ev_icons_path = g_build_filename (dir, "share", "evince", "icons", NULL); + g_free (dir); +#else + ev_icons_path = g_build_filename (EVINCEDATADIR, "icons", NULL); +#endif + + factory = gtk_icon_factory_new (); + gtk_icon_factory_add_default (factory); + + source = gtk_icon_source_new (); + + for (i = 0; i < G_N_ELEMENTS (stock_icons); i++) { + GtkIconSet *set; + + gtk_icon_source_set_icon_name (source, stock_icons [i].icon); + + set = gtk_icon_set_new (); + gtk_icon_set_add_source (set, source); + + gtk_icon_factory_add (factory, stock_icons [i].stock_id, set); + gtk_icon_set_unref (set); + } + + gtk_icon_source_free (source); + + g_object_unref (G_OBJECT (factory)); + + ev_stock_icons_add_icons_path_for_screen (gdk_screen_get_default ()); +} + +void +ev_stock_icons_set_screen (GdkScreen *screen) +{ + g_return_if_fail (GDK_IS_SCREEN (screen)); + + ev_stock_icons_add_icons_path_for_screen (screen); +} + +void +ev_stock_icons_shutdown (void) +{ + g_free (ev_icons_path); +} + diff --git a/libview/ev-stock-icons.h b/libview/ev-stock-icons.h new file mode 100644 index 00000000..96ec26f6 --- /dev/null +++ b/libview/ev-stock-icons.h @@ -0,0 +1,57 @@ +/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */ +/* Stock icons for Evince + * + * Copyright (C) 2003 Martin Kretzschmar + * + * Author: + * Martin Kretzschmar <[email protected]> + * + * Evince is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * Evince is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public + * License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ + +#if !defined (__EV_EVINCE_VIEW_H_INSIDE__) && !defined (EVINCE_COMPILATION) +#error "Only <evince-view.h> can be included directly." +#endif + +#ifndef __EV_STOCK_ICONS_H__ +#define __EV_STOCK_ICONS_H__ + +#include <gdk/gdk.h> + +G_BEGIN_DECLS + +/* Evince stock icons */ +#define EV_STOCK_ZOOM "zoom" +#define EV_STOCK_ZOOM_PAGE "zoom-fit-height" +#define EV_STOCK_ZOOM_WIDTH "zoom-fit-width" +#define EV_STOCK_VIEW_DUAL "view-page-facing" +#define EV_STOCK_VIEW_CONTINUOUS "view-page-continuous" +#define EV_STOCK_ROTATE_LEFT "object-rotate-left" +#define EV_STOCK_ROTATE_RIGHT "object-rotate-right" +#define EV_STOCK_RUN_PRESENTATION "x-office-presentation" +#define EV_STOCK_VISIBLE "eye" +#define EV_STOCK_RESIZE_SE "resize-se" +#define EV_STOCK_RESIZE_SW "resize-sw" +#define EV_STOCK_CLOSE "close" +#define EV_STOCK_INVERTED_COLORS "inverted" +#define EV_STOCK_ATTACHMENT "mail-attachment" + +void ev_stock_icons_init (void); +void ev_stock_icons_shutdown (void); +void ev_stock_icons_set_screen (GdkScreen *screen); + +G_END_DECLS + +#endif /* __EV_STOCK_ICONS_H__ */ diff --git a/libview/ev-timeline.c b/libview/ev-timeline.c new file mode 100644 index 00000000..7491d6cf --- /dev/null +++ b/libview/ev-timeline.c @@ -0,0 +1,450 @@ +/* ev-timeline.c + * this file is part of evince, a mate document viewer + * + * Copyright (C) 2007 Carlos Garnacho <[email protected]> + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Library General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Library General Public License for more details. + * + * You should have received a copy of the GNU Library General Public + * License along with this library; if not, write to the + * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, + * Boston, MA 02110-1301, USA. + */ + +#include <glib.h> +#include <math.h> +#include <gdk/gdk.h> +#include "ev-timeline.h" + +#define EV_TIMELINE_GET_PRIV(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), EV_TYPE_TIMELINE, EvTimelinePriv)) +#define MSECS_PER_SEC 1000 +#define FRAME_INTERVAL(nframes) (MSECS_PER_SEC / nframes) +#define DEFAULT_FPS 30 + +typedef struct EvTimelinePriv EvTimelinePriv; + +struct EvTimelinePriv { + guint duration; + guint fps; + guint source_id; + + GTimer *timer; + + guint loop : 1; +}; + +enum { + PROP_0, + PROP_FPS, + PROP_DURATION, + PROP_LOOP +}; + +enum { + STARTED, + PAUSED, + FINISHED, + FRAME, + LAST_SIGNAL +}; + +static guint signals [LAST_SIGNAL] = { 0, }; + + +G_DEFINE_TYPE (EvTimeline, ev_timeline, G_TYPE_OBJECT) + + +static void +ev_timeline_init (EvTimeline *timeline) +{ + EvTimelinePriv *priv; + + priv = EV_TIMELINE_GET_PRIV (timeline); + + priv->fps = DEFAULT_FPS; + priv->duration = 0; +} + +static void +ev_timeline_set_property (GObject *object, + guint prop_id, + const GValue *value, + GParamSpec *pspec) +{ + EvTimeline *timeline; + + timeline = EV_TIMELINE (object); + + switch (prop_id) { + case PROP_FPS: + ev_timeline_set_fps (timeline, g_value_get_uint (value)); + break; + case PROP_DURATION: + ev_timeline_set_duration (timeline, g_value_get_uint (value)); + break; + case PROP_LOOP: + ev_timeline_set_loop (timeline, g_value_get_boolean (value)); + break; + default: + G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); + } +} + +static void +ev_timeline_get_property (GObject *object, + guint prop_id, + GValue *value, + GParamSpec *pspec) +{ + EvTimeline *timeline; + EvTimelinePriv *priv; + + timeline = EV_TIMELINE (object); + priv = EV_TIMELINE_GET_PRIV (timeline); + + switch (prop_id) { + case PROP_FPS: + g_value_set_uint (value, priv->fps); + break; + case PROP_DURATION: + g_value_set_uint (value, priv->duration); + break; + case PROP_LOOP: + g_value_set_boolean (value, priv->loop); + break; + default: + G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); + } +} + +static void +ev_timeline_finalize (GObject *object) +{ + EvTimelinePriv *priv; + + priv = EV_TIMELINE_GET_PRIV (object); + + if (priv->source_id) { + g_source_remove (priv->source_id); + priv->source_id = 0; + } + + if (priv->timer) + g_timer_destroy (priv->timer); + + G_OBJECT_CLASS (ev_timeline_parent_class)->finalize (object); +} + +static gboolean +ev_timeline_run_frame (EvTimeline *timeline) +{ + EvTimelinePriv *priv; + gdouble progress; + guint elapsed_time; + + GDK_THREADS_ENTER (); + + priv = EV_TIMELINE_GET_PRIV (timeline); + + elapsed_time = (guint) (g_timer_elapsed (priv->timer, NULL) * 1000); + progress = (gdouble) elapsed_time / priv->duration; + progress = CLAMP (progress, 0., 1.); + + g_signal_emit (timeline, signals [FRAME], 0, progress); + + if (progress >= 1.0) { + if (!priv->loop) { + if (priv->source_id) { + g_source_remove (priv->source_id); + priv->source_id = 0; + } + + g_signal_emit (timeline, signals [FINISHED], 0); + return FALSE; + } else { + ev_timeline_rewind (timeline); + } + } + + GDK_THREADS_LEAVE (); + + return TRUE; +} + +static void +ev_timeline_real_start (EvTimeline *timeline) +{ + EvTimelinePriv *priv; + + priv = EV_TIMELINE_GET_PRIV (timeline); + + if (!priv->source_id) { + if (priv->timer) + g_timer_continue (priv->timer); + else + priv->timer = g_timer_new (); + + /* sanity check */ + g_assert (priv->fps > 0); + + g_signal_emit (timeline, signals [STARTED], 0); + + priv->source_id = g_timeout_add (FRAME_INTERVAL (priv->fps), + (GSourceFunc) ev_timeline_run_frame, + timeline); + } +} + +static void +ev_timeline_class_init (EvTimelineClass *class) +{ + GObjectClass *object_class = G_OBJECT_CLASS (class); + + object_class->set_property = ev_timeline_set_property; + object_class->get_property = ev_timeline_get_property; + object_class->finalize = ev_timeline_finalize; + + class->start = ev_timeline_real_start; + + g_object_class_install_property (object_class, + PROP_FPS, + g_param_spec_uint ("fps", + "FPS", + "Frames per second for the timeline", + 1, + G_MAXUINT, + DEFAULT_FPS, + G_PARAM_READWRITE)); + g_object_class_install_property (object_class, + PROP_DURATION, + g_param_spec_uint ("duration", + "Animation Duration", + "Animation Duration", + 0, + G_MAXUINT, + 0, + G_PARAM_READWRITE)); + g_object_class_install_property (object_class, + PROP_LOOP, + g_param_spec_boolean ("loop", + "Loop", + "Whether the timeline loops or not", + FALSE, + G_PARAM_READWRITE)); + signals[STARTED] = + g_signal_new ("started", + G_TYPE_FROM_CLASS (object_class), + G_SIGNAL_RUN_LAST, + G_STRUCT_OFFSET (EvTimelineClass, started), + NULL, NULL, + g_cclosure_marshal_VOID__VOID, + G_TYPE_NONE, 0); + + signals[PAUSED] = + g_signal_new ("paused", + G_TYPE_FROM_CLASS (object_class), + G_SIGNAL_RUN_LAST, + G_STRUCT_OFFSET (EvTimelineClass, paused), + NULL, NULL, + g_cclosure_marshal_VOID__VOID, + G_TYPE_NONE, 0); + + signals[FINISHED] = + g_signal_new ("finished", + G_TYPE_FROM_CLASS (object_class), + G_SIGNAL_RUN_LAST, + G_STRUCT_OFFSET (EvTimelineClass, finished), + NULL, NULL, + g_cclosure_marshal_VOID__VOID, + G_TYPE_NONE, 0); + + signals[FRAME] = + g_signal_new ("frame", + G_TYPE_FROM_CLASS (object_class), + G_SIGNAL_RUN_LAST, + G_STRUCT_OFFSET (EvTimelineClass, frame), + NULL, NULL, + g_cclosure_marshal_VOID__DOUBLE, + G_TYPE_NONE, 1, + G_TYPE_DOUBLE); + + g_type_class_add_private (class, sizeof (EvTimelinePriv)); +} + +EvTimeline * +ev_timeline_new (guint duration) +{ + return g_object_new (EV_TYPE_TIMELINE, + "duration", duration, + NULL); +} + +void +ev_timeline_start (EvTimeline *timeline) +{ + g_return_if_fail (EV_IS_TIMELINE (timeline)); + + EV_TIMELINE_GET_CLASS (timeline)->start (timeline); +} + +void +ev_timeline_pause (EvTimeline *timeline) +{ + EvTimelinePriv *priv; + + g_return_if_fail (EV_IS_TIMELINE (timeline)); + + priv = EV_TIMELINE_GET_PRIV (timeline); + + if (priv->source_id) { + g_source_remove (priv->source_id); + priv->source_id = 0; + g_timer_stop (priv->timer); + g_signal_emit (timeline, signals [PAUSED], 0); + } +} + +void +ev_timeline_rewind (EvTimeline *timeline) +{ + EvTimelinePriv *priv; + + g_return_if_fail (EV_IS_TIMELINE (timeline)); + + priv = EV_TIMELINE_GET_PRIV (timeline); + + /* destroy and re-create timer if neccesary */ + if (priv->timer) { + g_timer_destroy (priv->timer); + + if (ev_timeline_is_running (timeline)) + priv->timer = g_timer_new (); + else + priv->timer = NULL; + } +} + +gboolean +ev_timeline_is_running (EvTimeline *timeline) +{ + EvTimelinePriv *priv; + + g_return_val_if_fail (EV_IS_TIMELINE (timeline), FALSE); + + priv = EV_TIMELINE_GET_PRIV (timeline); + + return (priv->source_id != 0); +} + +guint +ev_timeline_get_fps (EvTimeline *timeline) +{ + EvTimelinePriv *priv; + + g_return_val_if_fail (EV_IS_TIMELINE (timeline), 1); + + priv = EV_TIMELINE_GET_PRIV (timeline); + return priv->fps; +} + +void +ev_timeline_set_fps (EvTimeline *timeline, + guint fps) +{ + EvTimelinePriv *priv; + + g_return_if_fail (EV_IS_TIMELINE (timeline)); + + priv = EV_TIMELINE_GET_PRIV (timeline); + + priv->fps = fps; + + if (ev_timeline_is_running (timeline)) { + g_source_remove (priv->source_id); + priv->source_id = g_timeout_add (FRAME_INTERVAL (priv->fps), + (GSourceFunc) ev_timeline_run_frame, + timeline); + } + + g_object_notify (G_OBJECT (timeline), "fps"); +} + +gboolean +ev_timeline_get_loop (EvTimeline *timeline) +{ + EvTimelinePriv *priv; + + g_return_val_if_fail (EV_IS_TIMELINE (timeline), FALSE); + + priv = EV_TIMELINE_GET_PRIV (timeline); + return priv->loop; +} + +void +ev_timeline_set_loop (EvTimeline *timeline, + gboolean loop) +{ + EvTimelinePriv *priv; + + g_return_if_fail (EV_IS_TIMELINE (timeline)); + + priv = EV_TIMELINE_GET_PRIV (timeline); + priv->loop = loop; + + g_object_notify (G_OBJECT (timeline), "loop"); +} + +void +ev_timeline_set_duration (EvTimeline *timeline, + guint duration) +{ + EvTimelinePriv *priv; + + g_return_if_fail (EV_IS_TIMELINE (timeline)); + + priv = EV_TIMELINE_GET_PRIV (timeline); + + priv->duration = duration; + + g_object_notify (G_OBJECT (timeline), "duration"); +} + +guint +ev_timeline_get_duration (EvTimeline *timeline) +{ + EvTimelinePriv *priv; + + g_return_val_if_fail (EV_IS_TIMELINE (timeline), 0); + + priv = EV_TIMELINE_GET_PRIV (timeline); + + return priv->duration; +} + +gdouble +ev_timeline_get_progress (EvTimeline *timeline) +{ + EvTimelinePriv *priv; + gdouble progress; + guint elapsed_time; + + g_return_val_if_fail (EV_IS_TIMELINE (timeline), 0.0); + + priv = EV_TIMELINE_GET_PRIV (timeline); + + if (!priv->timer) + return 0.; + + elapsed_time = (guint) (g_timer_elapsed (priv->timer, NULL) * 1000); + progress = (gdouble) elapsed_time / priv->duration; + + return CLAMP (progress, 0., 1.); +} diff --git a/libview/ev-timeline.h b/libview/ev-timeline.h new file mode 100644 index 00000000..e7f9d625 --- /dev/null +++ b/libview/ev-timeline.h @@ -0,0 +1,90 @@ +/* ev-timeline.h + * this file is part of evince, a mate document viewer + * + * Copyright (C) 2007 Carlos Garnacho <[email protected]> + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Library General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Library General Public License for more details. + * + * You should have received a copy of the GNU Library General Public + * License along with this library; if not, write to the + * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, + * Boston, MA 02110-1301, USA. + */ + +#if !defined (EVINCE_COMPILATION) +#error "This is a private header." +#endif + +#ifndef __EV_TIMELINE_H__ +#define __EV_TIMELINE_H__ + +#include <glib-object.h> + +G_BEGIN_DECLS + +#define EV_TYPE_TIMELINE (ev_timeline_get_type ()) +#define EV_TIMELINE(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), EV_TYPE_TIMELINE, EvTimeline)) +#define EV_TIMELINE_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), EV_TYPE_TIMELINE, EvTimelineClass)) +#define EV_IS_TIMELINE(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), EV_TYPE_TIMELINE)) +#define EV_IS_TIMELINE_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), EV_TYPE_TIMELINE)) +#define EV_TIMELINE_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), EV_TYPE_TIMELINE, EvTimelineClass)) + +typedef struct EvTimeline EvTimeline; +typedef struct EvTimelineClass EvTimelineClass; + +struct EvTimeline { + GObject parent_instance; +}; + +struct EvTimelineClass { + GObjectClass parent_class; + + /* vmethods */ + void (* start) (EvTimeline *timeline); + + /* signals */ + void (* started) (EvTimeline *timeline); + void (* finished) (EvTimeline *timeline); + void (* paused) (EvTimeline *timeline); + + void (* frame) (EvTimeline *timeline, + gdouble progress); +}; + + +GType ev_timeline_get_type (void) G_GNUC_CONST; + +EvTimeline *ev_timeline_new (guint duration); + +void ev_timeline_start (EvTimeline *timeline); +void ev_timeline_pause (EvTimeline *timeline); +void ev_timeline_rewind (EvTimeline *timeline); + +gboolean ev_timeline_is_running (EvTimeline *timeline); + +guint ev_timeline_get_fps (EvTimeline *timeline); +void ev_timeline_set_fps (EvTimeline *timeline, + guint fps); + +gboolean ev_timeline_get_loop (EvTimeline *timeline); +void ev_timeline_set_loop (EvTimeline *timeline, + gboolean loop); + +guint ev_timeline_get_duration (EvTimeline *timeline); +void ev_timeline_set_duration (EvTimeline *timeline, + guint duration); + +gdouble ev_timeline_get_progress (EvTimeline *timeline); + + +G_END_DECLS + +#endif /* __EV_TIMELINE_H__ */ diff --git a/libview/ev-transition-animation.c b/libview/ev-transition-animation.c new file mode 100644 index 00000000..a767019b --- /dev/null +++ b/libview/ev-transition-animation.c @@ -0,0 +1,676 @@ +/* ev-transition-animation.c + * this file is part of evince, a mate document viewer + * + * Copyright (C) 2007 Carlos Garnacho <[email protected]> + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Library General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Library General Public License for more details. + * + * You should have received a copy of the GNU Library General Public + * License along with this library; if not, write to the + * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, + * Boston, MA 02110-1301, USA. + */ + +#include <cairo.h> +#include <gdk/gdk.h> +#include "ev-transition-animation.h" +#include "ev-timeline.h" + +#define EV_TRANSITION_ANIMATION_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), EV_TYPE_TRANSITION_ANIMATION, EvTransitionAnimationPriv)) +#define N_BLINDS 6 + +typedef struct EvTransitionAnimationPriv EvTransitionAnimationPriv; + +struct EvTransitionAnimationPriv { + EvTransitionEffect *effect; + cairo_surface_t *origin_surface; + cairo_surface_t *dest_surface; +}; + +enum { + PROP_0, + PROP_EFFECT, + PROP_ORIGIN_SURFACE, + PROP_DEST_SURFACE +}; + + +G_DEFINE_TYPE (EvTransitionAnimation, ev_transition_animation, EV_TYPE_TIMELINE) + + +static void +ev_transition_animation_init (EvTransitionAnimation *animation) +{ +} + +static void +ev_transition_animation_set_property (GObject *object, + guint prop_id, + const GValue *value, + GParamSpec *pspec) +{ + EvTransitionAnimationPriv *priv; + + priv = EV_TRANSITION_ANIMATION_GET_PRIVATE (object); + + switch (prop_id) { + case PROP_EFFECT: + if (priv->effect) + g_object_unref (priv->effect); + + priv->effect = g_value_dup_object (value); + break; + case PROP_ORIGIN_SURFACE: + ev_transition_animation_set_origin_surface (EV_TRANSITION_ANIMATION (object), + g_value_get_pointer (value)); + break; + case PROP_DEST_SURFACE: + ev_transition_animation_set_dest_surface (EV_TRANSITION_ANIMATION (object), + g_value_get_pointer (value)); + break; + default: + G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); + } +} + +static void +ev_transition_animation_get_property (GObject *object, + guint prop_id, + GValue *value, + GParamSpec *pspec) +{ + EvTransitionAnimationPriv *priv; + + priv = EV_TRANSITION_ANIMATION_GET_PRIVATE (object); + + switch (prop_id) { + case PROP_EFFECT: + g_value_set_object (value, priv->effect); + break; + case PROP_ORIGIN_SURFACE: + g_value_set_pointer (value, priv->origin_surface); + break; + case PROP_DEST_SURFACE: + g_value_set_pointer (value, priv->dest_surface); + break; + default: + G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); + } +} + +static void +ev_transition_animation_finalize (GObject *object) +{ + EvTransitionAnimationPriv *priv; + + priv = EV_TRANSITION_ANIMATION_GET_PRIVATE (object); + + if (priv->effect) + g_object_unref (priv->effect); + + if (priv->origin_surface) + cairo_surface_destroy (priv->origin_surface); + + if (priv->dest_surface) + cairo_surface_destroy (priv->dest_surface); + + G_OBJECT_CLASS (ev_transition_animation_parent_class)->finalize (object); +} + +static GObject * +ev_transition_animation_constructor (GType type, + guint n_construct_properties, + GObjectConstructParam *construct_params) +{ + GObject *object; + EvTransitionAnimationPriv *priv; + EvTransitionEffect *effect; + gint duration; + + object = G_OBJECT_CLASS (ev_transition_animation_parent_class)->constructor (type, + n_construct_properties, + construct_params); + + priv = EV_TRANSITION_ANIMATION_GET_PRIVATE (object); + effect = priv->effect; + + g_object_get (effect, "duration", &duration, NULL); + ev_timeline_set_duration (EV_TIMELINE (object), duration * 1000); + + return object; +} + +static void +ev_transition_animation_class_init (EvTransitionAnimationClass *klass) +{ + GObjectClass *object_class = G_OBJECT_CLASS (klass); + + object_class->set_property = ev_transition_animation_set_property; + object_class->get_property = ev_transition_animation_get_property; + object_class->finalize = ev_transition_animation_finalize; + object_class->constructor = ev_transition_animation_constructor; + + g_object_class_install_property (object_class, + PROP_EFFECT, + g_param_spec_object ("effect", + "Effect", + "Transition effect description", + EV_TYPE_TRANSITION_EFFECT, + G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE)); + g_object_class_install_property (object_class, + PROP_ORIGIN_SURFACE, + g_param_spec_pointer ("origin-surface", + "Origin surface", + "Cairo surface from which the animation will happen", + G_PARAM_READWRITE)); + g_object_class_install_property (object_class, + PROP_DEST_SURFACE, + g_param_spec_pointer ("dest-surface", + "Destination surface", + "Cairo surface to which the animation will happen", + G_PARAM_READWRITE)); + + g_type_class_add_private (klass, sizeof (EvTransitionAnimationPriv)); +} + +static void +paint_surface (cairo_t *cr, + cairo_surface_t *surface, + gdouble x_offset, + gdouble y_offset, + gdouble alpha, + GdkRectangle page_area) +{ + cairo_save (cr); + + gdk_cairo_rectangle (cr, &page_area); + cairo_clip (cr); + cairo_surface_set_device_offset (surface, x_offset, y_offset); + cairo_set_source_surface (cr, surface, 0, 0); + + if (alpha == 1.) + cairo_paint (cr); + else + cairo_paint_with_alpha (cr, alpha); + + cairo_restore (cr); +} + +/* animations */ +static void +ev_transition_animation_split (cairo_t *cr, + EvTransitionAnimation *animation, + EvTransitionEffect *effect, + gdouble progress, + GdkRectangle page_area) +{ + EvTransitionAnimationPriv *priv; + EvTransitionEffectAlignment alignment; + EvTransitionEffectDirection direction; + gint width, height; + + priv = EV_TRANSITION_ANIMATION_GET_PRIVATE (animation); + width = page_area.width; + height = page_area.height; + + g_object_get (effect, + "alignment", &alignment, + "direction", &direction, + NULL); + + if (direction == EV_TRANSITION_DIRECTION_INWARD) { + paint_surface (cr, priv->dest_surface, 0, 0, 1., page_area); + + if (alignment == EV_TRANSITION_ALIGNMENT_HORIZONTAL) { + cairo_rectangle (cr, + 0, + height * progress / 2, + width, + height * (1 - progress)); + } else { + cairo_rectangle (cr, + width * progress / 2, + 0, + width * (1 - progress), + height); + } + + cairo_clip (cr); + + paint_surface (cr, priv->origin_surface, 0, 0, 1., page_area); + } else { + paint_surface (cr, priv->origin_surface, 0, 0, 1., page_area); + + if (alignment == EV_TRANSITION_ALIGNMENT_HORIZONTAL) { + cairo_rectangle (cr, + 0, + (height / 2) - (height * progress / 2), + width, + height * progress); + } else { + cairo_rectangle (cr, + (width / 2) - (width * progress / 2), + 0, + width * progress, + height); + } + + cairo_clip (cr); + + paint_surface (cr, priv->dest_surface, 0, 0, 1., page_area); + } +} + +static void +ev_transition_animation_blinds (cairo_t *cr, + EvTransitionAnimation *animation, + EvTransitionEffect *effect, + gdouble progress, + GdkRectangle page_area) +{ + EvTransitionAnimationPriv *priv; + EvTransitionEffectAlignment alignment; + gint width, height, i; + + priv = EV_TRANSITION_ANIMATION_GET_PRIVATE (animation); + width = page_area.width; + height = page_area.height; + + g_object_get (effect, + "alignment", &alignment, + NULL); + + paint_surface (cr, priv->origin_surface, 0, 0, 1., page_area); + + for (i = 0; i < N_BLINDS; i++) { + cairo_save (cr); + + if (alignment == EV_TRANSITION_ALIGNMENT_HORIZONTAL) { + cairo_rectangle (cr, + 0, + height / N_BLINDS * i, + width, + height / N_BLINDS * progress); + } else { + cairo_rectangle (cr, + width / N_BLINDS * i, + 0, + width / N_BLINDS * progress, + height); + } + + cairo_clip (cr); + paint_surface (cr, priv->dest_surface, 0, 0, 1., page_area); + cairo_restore (cr); + } +} + +static void +ev_transition_animation_box (cairo_t *cr, + EvTransitionAnimation *animation, + EvTransitionEffect *effect, + gdouble progress, + GdkRectangle page_area) +{ + EvTransitionAnimationPriv *priv; + EvTransitionEffectDirection direction; + gint width, height; + + priv = EV_TRANSITION_ANIMATION_GET_PRIVATE (animation); + width = page_area.width; + height = page_area.height; + + g_object_get (effect, + "direction", &direction, + NULL); + + if (direction == EV_TRANSITION_DIRECTION_INWARD) { + paint_surface (cr, priv->dest_surface, 0, 0, 1., page_area); + + cairo_rectangle (cr, + width * progress / 2, + height * progress / 2, + width * (1 - progress), + height * (1 - progress)); + cairo_clip (cr); + + paint_surface (cr, priv->origin_surface, 0, 0, 1., page_area); + } else { + paint_surface (cr, priv->origin_surface, 0, 0, 1., page_area); + + cairo_rectangle (cr, + (width / 2) - (width * progress / 2), + (height / 2) - (height * progress / 2), + width * progress, + height * progress); + cairo_clip (cr); + + paint_surface (cr, priv->dest_surface, 0, 0, 1., page_area); + } +} + +static void +ev_transition_animation_wipe (cairo_t *cr, + EvTransitionAnimation *animation, + EvTransitionEffect *effect, + gdouble progress, + GdkRectangle page_area) +{ + EvTransitionAnimationPriv *priv; + gint width, height; + gint angle; + + priv = EV_TRANSITION_ANIMATION_GET_PRIVATE (animation); + width = page_area.width; + height = page_area.height; + + g_object_get (effect, + "angle", &angle, + NULL); + + paint_surface (cr, priv->origin_surface, 0, 0, 1., page_area); + + if (angle == 0) { + /* left to right */ + cairo_rectangle (cr, + 0, 0, + width * progress, + height); + } else if (angle <= 90) { + /* bottom to top */ + cairo_rectangle (cr, + 0, + height * (1 - progress), + width, + height * progress); + } else if (angle <= 180) { + /* right to left */ + cairo_rectangle (cr, + width * (1 - progress), + 0, + width * progress, + height); + } else if (angle <= 270) { + /* top to bottom */ + cairo_rectangle (cr, + 0, 0, + width, + height * progress); + } + + cairo_clip (cr); + + paint_surface (cr, priv->dest_surface, 0, 0, 1., page_area); +} + +static void +ev_transition_animation_dissolve (cairo_t *cr, + EvTransitionAnimation *animation, + EvTransitionEffect *effect, + gdouble progress, + GdkRectangle page_area) +{ + EvTransitionAnimationPriv *priv; + + priv = EV_TRANSITION_ANIMATION_GET_PRIVATE (animation); + + paint_surface (cr, priv->dest_surface, 0, 0, 1., page_area); + paint_surface (cr, priv->origin_surface, 0, 0, 1 - progress, page_area); +} + +static void +ev_transition_animation_push (cairo_t *cr, + EvTransitionAnimation *animation, + EvTransitionEffect *effect, + gdouble progress, + GdkRectangle page_area) +{ + EvTransitionAnimationPriv *priv; + gint width, height; + gint angle; + + priv = EV_TRANSITION_ANIMATION_GET_PRIVATE (animation); + width = page_area.width; + height = page_area.height; + + g_object_get (effect, + "angle", &angle, + NULL); + + if (angle == 0) { + /* left to right */ + paint_surface (cr, priv->origin_surface, - (width * progress), 0, 1., page_area); + paint_surface (cr, priv->dest_surface, width * (1 - progress), 0, 1., page_area); + } else { + /* top to bottom */ + paint_surface (cr, priv->origin_surface, 0, - (height * progress), 1., page_area); + paint_surface (cr, priv->dest_surface, 0, height * (1 - progress), 1., page_area); + } +} + +static void +ev_transition_animation_cover (cairo_t *cr, + EvTransitionAnimation *animation, + EvTransitionEffect *effect, + gdouble progress, + GdkRectangle page_area) +{ + EvTransitionAnimationPriv *priv; + gint width, height; + gint angle; + + priv = EV_TRANSITION_ANIMATION_GET_PRIVATE (animation); + width = page_area.width; + height = page_area.height; + + g_object_get (effect, + "angle", &angle, + NULL); + + paint_surface (cr, priv->origin_surface, 0, 0, 1., page_area); + + if (angle == 0) { + /* left to right */ + paint_surface (cr, priv->dest_surface, width * (1 - progress), 0, 1., page_area); + } else { + /* top to bottom */ + paint_surface (cr, priv->dest_surface, 0, height * (1 - progress), 1., page_area); + } +} + +static void +ev_transition_animation_uncover (cairo_t *cr, + EvTransitionAnimation *animation, + EvTransitionEffect *effect, + gdouble progress, + GdkRectangle page_area) +{ + EvTransitionAnimationPriv *priv; + gint width, height; + gint angle; + + priv = EV_TRANSITION_ANIMATION_GET_PRIVATE (animation); + width = page_area.width; + height = page_area.height; + + g_object_get (effect, + "angle", &angle, + NULL); + + paint_surface (cr, priv->dest_surface, 0, 0, 1., page_area); + + if (angle == 0) { + /* left to right */ + paint_surface (cr, priv->origin_surface, - (width * progress), 0, 1., page_area); + } else { + /* top to bottom */ + paint_surface (cr, priv->origin_surface, 0, - (height * progress), 1., page_area); + } +} + +static void +ev_transition_animation_fade (cairo_t *cr, + EvTransitionAnimation *animation, + EvTransitionEffect *effect, + gdouble progress, + GdkRectangle page_area) +{ + EvTransitionAnimationPriv *priv; + + priv = EV_TRANSITION_ANIMATION_GET_PRIVATE (animation); + + paint_surface (cr, priv->origin_surface, 0, 0, 1., page_area); + paint_surface (cr, priv->dest_surface, 0, 0, progress, page_area); +} + +void +ev_transition_animation_paint (EvTransitionAnimation *animation, + cairo_t *cr, + GdkRectangle page_area) +{ + EvTransitionAnimationPriv *priv; + EvTransitionEffectType type; + gdouble progress; + + g_return_if_fail (EV_IS_TRANSITION_ANIMATION (animation)); + + priv = EV_TRANSITION_ANIMATION_GET_PRIVATE (animation); + + if (!priv->dest_surface) { + /* animation is still not ready, paint the origin surface */ + paint_surface (cr, priv->origin_surface, 0, 0, 1., page_area); + return; + } + + g_object_get (priv->effect, "type", &type, NULL); + progress = ev_timeline_get_progress (EV_TIMELINE (animation)); + + switch (type) { + case EV_TRANSITION_EFFECT_REPLACE: + /* just paint the destination slide */ + paint_surface (cr, priv->dest_surface, 0, 0, 1., page_area); + break; + case EV_TRANSITION_EFFECT_SPLIT: + ev_transition_animation_split (cr, animation, priv->effect, progress, page_area); + break; + case EV_TRANSITION_EFFECT_BLINDS: + ev_transition_animation_blinds (cr, animation, priv->effect, progress, page_area); + break; + case EV_TRANSITION_EFFECT_BOX: + ev_transition_animation_box (cr, animation, priv->effect, progress, page_area); + break; + case EV_TRANSITION_EFFECT_WIPE: + ev_transition_animation_wipe (cr, animation, priv->effect, progress, page_area); + break; + case EV_TRANSITION_EFFECT_DISSOLVE: + ev_transition_animation_dissolve (cr, animation, priv->effect, progress, page_area); + break; + case EV_TRANSITION_EFFECT_PUSH: + ev_transition_animation_push (cr, animation, priv->effect, progress, page_area); + break; + case EV_TRANSITION_EFFECT_COVER: + ev_transition_animation_cover (cr, animation, priv->effect, progress, page_area); + break; + case EV_TRANSITION_EFFECT_UNCOVER: + ev_transition_animation_uncover (cr, animation, priv->effect, progress, page_area); + break; + case EV_TRANSITION_EFFECT_FADE: + ev_transition_animation_fade (cr, animation, priv->effect, progress, page_area); + break; + default: { + GEnumValue *enum_value; + + enum_value = g_enum_get_value (g_type_class_peek (EV_TYPE_TRANSITION_EFFECT_TYPE), type); + + g_warning ("Unimplemented transition animation: '%s', " + "please post a bug report in Evince bugzilla " + "(http://bugzilla.mate.org) with a testcase.", + enum_value->value_nick); + + /* just paint the destination slide */ + paint_surface (cr, priv->dest_surface, 0, 0, 1., page_area); + } + } +} + +EvTransitionAnimation * +ev_transition_animation_new (EvTransitionEffect *effect) +{ + g_return_val_if_fail (EV_IS_TRANSITION_EFFECT (effect), NULL); + + return g_object_new (EV_TYPE_TRANSITION_ANIMATION, + "effect", effect, + NULL); +} + +void +ev_transition_animation_set_origin_surface (EvTransitionAnimation *animation, + cairo_surface_t *origin_surface) +{ + EvTransitionAnimationPriv *priv; + cairo_surface_t *surface; + + g_return_if_fail (EV_IS_TRANSITION_ANIMATION (animation)); + + priv = EV_TRANSITION_ANIMATION_GET_PRIVATE (animation); + + if (priv->origin_surface == origin_surface) + return; + + surface = cairo_surface_reference (origin_surface); + + if (priv->origin_surface) + cairo_surface_destroy (priv->origin_surface); + + priv->origin_surface = surface; + g_object_notify (G_OBJECT (animation), "origin-surface"); + + if (priv->origin_surface && priv->dest_surface) + ev_timeline_start (EV_TIMELINE (animation)); +} + +void +ev_transition_animation_set_dest_surface (EvTransitionAnimation *animation, + cairo_surface_t *dest_surface) +{ + EvTransitionAnimationPriv *priv; + cairo_surface_t *surface; + + g_return_if_fail (EV_IS_TRANSITION_ANIMATION (animation)); + + priv = EV_TRANSITION_ANIMATION_GET_PRIVATE (animation); + + if (priv->dest_surface == dest_surface) + return; + + surface = cairo_surface_reference (dest_surface); + + if (priv->dest_surface) + cairo_surface_destroy (priv->dest_surface); + + priv->dest_surface = surface; + g_object_notify (G_OBJECT (animation), "dest-surface"); + + if (priv->origin_surface && priv->dest_surface) + ev_timeline_start (EV_TIMELINE (animation)); +} + +gboolean +ev_transition_animation_ready (EvTransitionAnimation *animation) +{ + EvTransitionAnimationPriv *priv; + + g_return_val_if_fail (EV_IS_TRANSITION_ANIMATION (animation), FALSE); + + priv = EV_TRANSITION_ANIMATION_GET_PRIVATE (animation); + + return (priv->origin_surface != NULL); +} diff --git a/libview/ev-transition-animation.h b/libview/ev-transition-animation.h new file mode 100644 index 00000000..3d17f28d --- /dev/null +++ b/libview/ev-transition-animation.h @@ -0,0 +1,73 @@ +/* ev-transition-animation.h + * this file is part of evince, a mate document viewer + * + * Copyright (C) 2007 Carlos Garnacho <[email protected]> + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Library General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Library General Public License for more details. + * + * You should have received a copy of the GNU Library General Public + * License along with this library; if not, write to the + * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, + * Boston, MA 02110-1301, USA. + */ + +#if !defined (EVINCE_COMPILATION) +#error "This is a private header." +#endif + +#ifndef __EV_TRANSITION_ANIMATION_H__ +#define __EV_TRANSITION_ANIMATION_H__ + +#include <evince-document.h> +#include "ev-timeline.h" +#include "ev-transition-effect.h" + +G_BEGIN_DECLS + +#define EV_TYPE_TRANSITION_ANIMATION (ev_transition_animation_get_type ()) +#define EV_TRANSITION_ANIMATION(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), EV_TYPE_TRANSITION_ANIMATION, EvTransitionAnimation)) +#define EV_TRANSITION_ANIMATION_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), EV_TYPE_TRANSITION_ANIMATION, EvTransitionAnimationClass)) +#define EV_IS_TRANSITION_ANIMATION(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), EV_TYPE_TRANSITION_ANIMATION)) +#define EV_IS_TRANSITION_ANIMATION_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), EV_TYPE_TRANSITION_ANIMATION)) +#define EV_TRANSITION_ANIMATION_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), EV_TYPE_TRANSITION_ANIMATION, EvTransitionAnimationClass)) + +typedef struct EvTransitionAnimation EvTransitionAnimation; +typedef struct EvTransitionAnimationClass EvTransitionAnimationClass; + +struct EvTransitionAnimation { + EvTimeline parent_instance; +}; + +struct EvTransitionAnimationClass { + EvTimelineClass parent_class; +}; + + +GType ev_transition_animation_get_type (void) G_GNUC_CONST; + +EvTransitionAnimation * ev_transition_animation_new (EvTransitionEffect *effect); + +void ev_transition_animation_set_origin_surface (EvTransitionAnimation *animation, + cairo_surface_t *origin_surface); +void ev_transition_animation_set_dest_surface (EvTransitionAnimation *animation, + cairo_surface_t *origin_surface); +gint ev_transition_animation_get_page_from (EvTransitionAnimation *animation); +gint ev_transition_animation_get_page_to (EvTransitionAnimation *animation); + +void ev_transition_animation_paint (EvTransitionAnimation *animation, + cairo_t *cr, + GdkRectangle page_area); +gboolean ev_transition_animation_ready (EvTransitionAnimation *animation); + + +G_END_DECLS + +#endif /* __EV_TRANSITION_ANIMATION_H__ */ diff --git a/libview/ev-view-accessible.c b/libview/ev-view-accessible.c new file mode 100644 index 00000000..9b41c919 --- /dev/null +++ b/libview/ev-view-accessible.c @@ -0,0 +1,911 @@ +/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8; c-indent-level: 8 -*- */ +/* this file is part of evince, a mate document viewer + * + * Copyright (C) 2004 Red Hat, Inc + * + * Evince is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * Evince is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ + +#include <math.h> +#include <config.h> +#include <glib/gi18n-lib.h> +#include <gtk/gtk.h> +#include <libgail-util/gail-util.h> + +#include "ev-selection.h" +#include "ev-page-cache.h" +#include "ev-view-accessible.h" +#include "ev-view-private.h" + +#define EV_TYPE_VIEW_ACCESSIBLE (ev_view_accessible_get_type ()) +#define EV_VIEW_ACCESSIBLE(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), EV_TYPE_VIEW_ACCESSIBLE, EvViewAccessible)) +#define EV_IS_VIEW_ACCESSIBLE(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), EV_TYPE_VIEW_ACCESSIBLE)) + +static GType ev_view_accessible_get_type (void); + +enum { + ACTION_SCROLL_UP, + ACTION_SCROLL_DOWN, + LAST_ACTION +}; + +static const gchar *const ev_view_accessible_action_names[] = +{ + N_("Scroll Up"), + N_("Scroll Down"), + NULL +}; + +static const gchar *const ev_view_accessible_action_descriptions[] = +{ + N_("Scroll View Up"), + N_("Scroll View Down"), + NULL +}; + +typedef struct { + /* Action */ + gchar *action_descriptions[LAST_ACTION]; + guint action_idle_handler; + GtkScrollType idle_scroll; + GtkTextBuffer *buffer; + guint current_page; +} EvViewAccessiblePriv; + +typedef GtkAccessibleClass EvViewAccessibleClass; +typedef GtkAccessible EvViewAccessible; + +#define EV_VIEW_ACCESSIBLE_GET_PRIVATE(inst) (G_TYPE_INSTANCE_GET_PRIVATE ((inst), EV_TYPE_VIEW_ACCESSIBLE, EvViewAccessiblePriv)) + +static void +ev_view_accessible_finalize (GObject *object) +{ + EvViewAccessiblePriv *priv = EV_VIEW_ACCESSIBLE_GET_PRIVATE (object); + int i; + + if (priv->action_idle_handler) + g_source_remove (priv->action_idle_handler); + for (i = 0; i < LAST_ACTION; i++) + g_free (priv->action_descriptions [i]); + if (priv->buffer) + g_object_unref (priv->buffer); + +} + +static void ev_view_accessible_class_init (EvViewAccessibleClass *klass) +{ + GObjectClass *object_class = G_OBJECT_CLASS (klass); + + object_class->finalize = ev_view_accessible_finalize; + + g_type_class_add_private (klass, sizeof (EvViewAccessiblePriv)); +} + +static GtkTextBuffer * +ev_view_accessible_get_text_buffer (EvViewAccessible *accessible, EvView *view) +{ + EvPageCache *page_cache; + const gchar *retval = NULL; + EvViewAccessiblePriv* priv = EV_VIEW_ACCESSIBLE_GET_PRIVATE (accessible); + + page_cache = view->page_cache; + if (!page_cache) { + return NULL; + } + + if (view->current_page == priv->current_page && priv->buffer) { + return priv->buffer; + } + + priv->current_page = view->current_page; + + if (!priv->buffer) { + priv->buffer = gtk_text_buffer_new (NULL); + } + + retval = ev_page_cache_get_text (page_cache, view->current_page); + gtk_text_buffer_set_text (priv->buffer, retval, -1); + + return priv->buffer; +} + +static gchar* +ev_view_accessible_get_text (AtkText *text, + gint start_pos, + gint end_pos) +{ + GtkWidget *widget; + GtkTextIter start, end; + GtkTextBuffer *buffer; + gchar *retval; + + widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (text)); + if (widget == NULL) + /* State is defunct */ + return NULL; + + buffer = ev_view_accessible_get_text_buffer (EV_VIEW_ACCESSIBLE (text), EV_VIEW (widget)); + if (!buffer) + return NULL; + + gtk_text_buffer_get_iter_at_offset (buffer, &start, start_pos); + gtk_text_buffer_get_iter_at_offset (buffer, &end, end_pos); + retval = gtk_text_buffer_get_text (buffer, &start, &end, FALSE); + + return retval; +} + +static gunichar +ev_view_accessible_get_character_at_offset (AtkText *text, + gint offset) +{ + GtkWidget *widget; + GtkTextIter start, end; + GtkTextBuffer *buffer; + gchar *string; + gunichar unichar; + + widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (text)); + if (widget == NULL) + /* State is defunct */ + return '\0'; + + buffer = ev_view_accessible_get_text_buffer (EV_VIEW_ACCESSIBLE (text), EV_VIEW (widget)); + if (!buffer) + return '\0'; + + if (offset >= gtk_text_buffer_get_char_count (buffer)) + return '\0'; + + gtk_text_buffer_get_iter_at_offset (buffer, &start, offset); + end = start; + gtk_text_iter_forward_char (&end); + string = gtk_text_buffer_get_slice (buffer, &start, &end, FALSE); + unichar = g_utf8_get_char (string); + g_free(string); + + return unichar; +} + +static gchar* +ev_view_accessible_get_text_before_offset (AtkText *text, + gint offset, + AtkTextBoundary boundary_type, + gint *start_offset, + gint *end_offset) +{ + GtkWidget *widget; + gpointer layout = NULL; + GailTextUtil *gail_text = NULL; + gchar *retval = NULL; + GtkTextBuffer *buffer; + + widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (text)); + if (widget == NULL) + /* State is defunct */ + return NULL; + + buffer = ev_view_accessible_get_text_buffer (EV_VIEW_ACCESSIBLE (text), EV_VIEW (widget)); + if (!buffer) + return NULL; + + gail_text = gail_text_util_new (); + gail_text_util_buffer_setup (gail_text, buffer); + retval = gail_text_util_get_text (gail_text, layout, + GAIL_BEFORE_OFFSET, boundary_type, + offset, start_offset, end_offset); + g_object_unref (gail_text); + + return retval; +} + +static gchar* +ev_view_accessible_get_text_at_offset (AtkText *text, + gint offset, + AtkTextBoundary boundary_type, + gint *start_offset, + gint *end_offset) +{ + GtkWidget *widget; + gpointer layout = NULL; + GailTextUtil *gail_text = NULL; + gchar *retval = NULL; + GtkTextBuffer *buffer; + + widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (text)); + if (widget == NULL) + /* State is defunct */ + return NULL; + + buffer = ev_view_accessible_get_text_buffer (EV_VIEW_ACCESSIBLE (text), EV_VIEW (widget)); + if (!buffer) + return NULL; + + gail_text = gail_text_util_new (); + gail_text_util_buffer_setup (gail_text, buffer); + retval = gail_text_util_get_text (gail_text, layout, + GAIL_AT_OFFSET, boundary_type, + offset, start_offset, end_offset); + g_object_unref (gail_text); + + return retval; +} + +static gchar* +ev_view_accessible_get_text_after_offset (AtkText *text, + gint offset, + AtkTextBoundary boundary_type, + gint *start_offset, + gint *end_offset) +{ + GtkWidget *widget; + gpointer layout = NULL; + GailTextUtil *gail_text = NULL; + gchar *retval = NULL; + GtkTextBuffer *buffer; + + widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (text)); + if (widget == NULL) + /* State is defunct */ + return NULL; + + buffer = ev_view_accessible_get_text_buffer (EV_VIEW_ACCESSIBLE (text), EV_VIEW (widget)); + if (!buffer) + return NULL; + + gail_text = gail_text_util_new (); + gail_text_util_buffer_setup (gail_text, buffer); + retval = gail_text_util_get_text (gail_text, layout, + GAIL_AFTER_OFFSET, boundary_type, + offset, start_offset, end_offset); + g_object_unref (gail_text); + + return retval; +} + +static gint +ev_view_accessible_get_character_count (AtkText *text) +{ + GtkWidget *widget; + GtkTextBuffer *buffer; + gint retval; + + widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (text)); + if (widget == NULL) + /* State is defunct */ + return 0; + + buffer = ev_view_accessible_get_text_buffer (EV_VIEW_ACCESSIBLE (text), EV_VIEW (widget)); + if (!buffer) + return 0; + + retval = gtk_text_buffer_get_char_count (buffer); + + return retval; +} + +static gint +ev_view_accessible_get_caret_offset (AtkText *text) +{ + GtkWidget *widget; + GtkTextBuffer *buffer; + GtkTextMark *cursor_mark; + GtkTextIter cursor_itr; + gint retval; + + widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (text)); + if (widget == NULL) + /* State is defunct */ + return 0; + + buffer = ev_view_accessible_get_text_buffer (EV_VIEW_ACCESSIBLE (text), EV_VIEW (widget)); + if (!buffer) + return 0; + + cursor_mark = gtk_text_buffer_get_insert (buffer); + gtk_text_buffer_get_iter_at_mark (buffer, &cursor_itr, cursor_mark); + retval = gtk_text_iter_get_offset (&cursor_itr); + + return retval; +} + +static gboolean +ev_view_accessible_set_caret_offset (AtkText *text, gint offset) +{ + GtkWidget *widget; + GtkTextBuffer *buffer; + GtkTextIter pos_itr; + + widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (text)); + if (widget == NULL) + /* State is defunct */ + return FALSE; + + buffer = ev_view_accessible_get_text_buffer (EV_VIEW_ACCESSIBLE (text), EV_VIEW (widget)); + if (!buffer) + return FALSE; + + gtk_text_buffer_get_iter_at_offset (buffer, &pos_itr, offset); + gtk_text_buffer_place_cursor (buffer, &pos_itr); + + return TRUE; +} + +static AtkAttributeSet* +ev_view_accessible_get_run_attributes (AtkText *text, + gint offset, + gint *start_offset, + gint *end_offset) +{ + GtkWidget *widget; + GtkTextBuffer *buffer; + AtkAttributeSet *retval; + + widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (text)); + if (widget == NULL) + /* State is defunct */ + return NULL; + + buffer = ev_view_accessible_get_text_buffer (EV_VIEW_ACCESSIBLE (text), EV_VIEW (widget)); + if (!buffer) + return NULL; + + retval = gail_misc_buffer_get_run_attributes (buffer, offset, + start_offset, end_offset); + + return retval; +} + +static AtkAttributeSet* +ev_view_accessible_get_default_attributes (AtkText *text) +{ + GtkWidget *widget; + + widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (text)); + if (widget == NULL) + /* State is defunct */ + return NULL; + return NULL; +} + +static void +ev_view_accessible_get_character_extents (AtkText *text, + gint offset, + gint *x, + gint *y, + gint *width, + gint *height, + AtkCoordType coords) +{ + GtkWidget *widget, *toplevel; + EvRectangle *areas = NULL; + EvRectangle *rect = NULL; + guint n_areas = 0; + EvPageCache *page_cache; + gint x_widget, y_widget, x_window, y_window; + gdouble scale; + GtkBorder border; + GdkRectangle page_area; + + widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (text)); + if (widget == NULL) + /* State is defunct */ + return; + + page_cache = EV_VIEW (widget)->page_cache; + if (!page_cache) + return; + + ev_view_get_page_extents (EV_VIEW (widget), EV_VIEW (widget)->current_page, + &page_area, &border); + + scale = EV_VIEW (widget)->scale; + ev_page_cache_get_text_layout (page_cache, EV_VIEW (widget)->current_page, &areas, &n_areas); + if (!areas) + return; + + if (offset > n_areas) + return; + + rect = areas + offset; + *x = (int)(rect->x1 * scale); + *y = (int)(rect->y1 * scale); + + *width = (int)(fabs (rect->x2 - rect->x1) * scale); + *height = (int)(fabs (rect->y2 - rect->y1) * scale); + + toplevel = gtk_widget_get_toplevel (widget); + gtk_widget_translate_coordinates (widget, toplevel, 0, 0, &x_widget, &y_widget); + *x += x_widget; + *y += y_widget; + + if (coords == ATK_XY_SCREEN) { + gdk_window_get_origin (gtk_widget_get_window (toplevel), &x_window, &y_window); + *x += x_window; + *y += y_window; + } + + *x -= EV_VIEW (widget)->scroll_x; + *y -= EV_VIEW (widget)->scroll_y; + + *x += page_area.x; + *y += page_area.y; +} + +static gint +ev_view_accessible_get_offset_at_point (AtkText *text, + gint x, + gint y, + AtkCoordType coords) +{ + GtkWidget *widget, *toplevel; + EvRectangle *areas = NULL; + EvRectangle *rect = NULL; + guint n_areas = 0; + guint i = 0; + EvPageCache *page_cache; + gint x_window, y_window, x_widget, y_widget; + gint offset=-1, rx, ry; + gdouble scale; + GtkBorder border; + GdkRectangle page_area; + + widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (text)); + if (widget == NULL) + /* State is defunct */ + return -1; + + page_cache = EV_VIEW (widget)->page_cache; + if (!page_cache) + return -1; + + ev_view_get_page_extents (EV_VIEW (widget), EV_VIEW (widget)->current_page, + &page_area, &border); + + scale = EV_VIEW (widget)->scale; + ev_page_cache_get_text_layout (page_cache, EV_VIEW (widget)->current_page, &areas, &n_areas); + if (!areas) + return -1; + + rx = x; + ry = y; + + rx -= page_area.x; + ry -= page_area.y; + + rx += EV_VIEW (widget)->scroll_x; + ry += EV_VIEW (widget)->scroll_y; + + toplevel = gtk_widget_get_toplevel (widget); + gtk_widget_translate_coordinates (widget, toplevel, 0, 0, &x_widget, &y_widget); + rx -= x_widget; + ry -= y_widget; + + if (coords == ATK_XY_SCREEN) { + gdk_window_get_origin (gtk_widget_get_window (toplevel), &x_window, &y_window); + rx -= x_window; + ry -= y_window; + } + + rx /= scale; + ry /= scale; + + for (i = 0; i < n_areas; i++) { + rect = areas + i; + if (rx >= rect->x1 && rx <= rect->x2 && + ry >= rect->y1 && ry <= rect->y2) + offset = i; + } + + return offset; +} + +static gint +ev_view_accessible_get_n_selections (AtkText *text) +{ + GtkWidget *widget; + GtkTextBuffer *buffer; + GtkTextIter start, end; + gint select_start, select_end; + + widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (text)); + if (widget == NULL) + /* State is defunct */ + return -1; + + buffer = ev_view_accessible_get_text_buffer (EV_VIEW_ACCESSIBLE (text), EV_VIEW (widget)); + if (!buffer) + return -1; + + gtk_text_buffer_get_selection_bounds (buffer, &start, &end); + select_start = gtk_text_iter_get_offset (&start); + select_end = gtk_text_iter_get_offset (&end); + + if (select_start != select_end) + return 1; + else + return 0; +} + +static gchar* +ev_view_accessible_get_selection (AtkText *text, + gint selection_num, + gint *start_pos, + gint *end_pos) +{ + GtkWidget *widget; + GtkTextBuffer *buffer; + GtkTextIter start, end; + gchar *retval = NULL; + + widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (text)); + if (widget == NULL) + /* State is defunct */ + return NULL; + + if (selection_num != 0) + return NULL; + + buffer = ev_view_accessible_get_text_buffer (EV_VIEW_ACCESSIBLE (text), EV_VIEW (widget)); + if (!buffer) + return NULL; + + gtk_text_buffer_get_selection_bounds (buffer, &start, &end); + *start_pos = gtk_text_iter_get_offset (&start); + *end_pos = gtk_text_iter_get_offset (&end); + + if (*start_pos != *end_pos) + retval = gtk_text_buffer_get_text (buffer, &start, &end, FALSE); + + return retval; +} + +static gboolean +ev_view_accessible_add_selection (AtkText *text, + gint start_pos, + gint end_pos) +{ + GtkWidget *widget; + GtkTextBuffer *buffer; + GtkTextIter pos_itr; + GtkTextIter start, end; + gint select_start, select_end; + gboolean retval = FALSE; + + widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (text)); + if (widget == NULL) + /* State is defunct */ + return FALSE; + + buffer = ev_view_accessible_get_text_buffer (EV_VIEW_ACCESSIBLE (text), EV_VIEW (widget)); + if (!buffer) + return FALSE; + + gtk_text_buffer_get_selection_bounds (buffer, &start, &end); + select_start = gtk_text_iter_get_offset (&start); + select_end = gtk_text_iter_get_offset (&end); + + /* If there is already a selection, then don't allow + * another to be added + */ + if (select_start == select_end) { + gtk_text_buffer_get_iter_at_offset (buffer, &pos_itr, start_pos); + gtk_text_buffer_move_mark_by_name (buffer, "selection_bound", &pos_itr); + gtk_text_buffer_get_iter_at_offset (buffer, &pos_itr, end_pos); + gtk_text_buffer_move_mark_by_name (buffer, "insert", &pos_itr); + + retval = TRUE; + } + + return retval; +} + +static gboolean +ev_view_accessible_remove_selection (AtkText *text, + gint selection_num) +{ + GtkWidget *widget; + GtkTextBuffer *buffer; + GtkTextMark *cursor_mark; + GtkTextIter cursor_itr; + GtkTextIter start, end; + gint select_start, select_end; + gboolean retval = FALSE; + + widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (text)); + if (widget == NULL) + /* State is defunct */ + return FALSE; + + buffer = ev_view_accessible_get_text_buffer (EV_VIEW_ACCESSIBLE (text), EV_VIEW (widget)); + if (!buffer) + return FALSE; + + gtk_text_buffer_get_selection_bounds(buffer, &start, &end); + select_start = gtk_text_iter_get_offset(&start); + select_end = gtk_text_iter_get_offset(&end); + + if (select_start != select_end) { + /* Setting the start & end of the selected region + * to the caret position turns off the selection. + */ + cursor_mark = gtk_text_buffer_get_insert (buffer); + gtk_text_buffer_get_iter_at_mark (buffer, &cursor_itr, cursor_mark); + gtk_text_buffer_move_mark_by_name (buffer, "selection_bound", &cursor_itr); + + retval = TRUE; + } + + return retval; +} + +static gboolean +ev_view_accessible_set_selection (AtkText *text, + gint selection_num, + gint start_pos, + gint end_pos) +{ + GtkWidget *widget; + GtkTextBuffer *buffer; + GtkTextIter pos_itr; + GtkTextIter start, end; + gint select_start, select_end; + gboolean retval = FALSE; + + widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (text)); + if (widget == NULL) + /* State is defunct */ + return FALSE; + + buffer = ev_view_accessible_get_text_buffer (EV_VIEW_ACCESSIBLE (text), EV_VIEW (widget)); + if (!buffer) + return FALSE; + + gtk_text_buffer_get_selection_bounds(buffer, &start, &end); + select_start = gtk_text_iter_get_offset(&start); + select_end = gtk_text_iter_get_offset(&end); + + if (select_start != select_end) { + gtk_text_buffer_get_iter_at_offset (buffer, &pos_itr, start_pos); + gtk_text_buffer_move_mark_by_name (buffer, "selection_bound", &pos_itr); + gtk_text_buffer_get_iter_at_offset (buffer, &pos_itr, end_pos); + gtk_text_buffer_move_mark_by_name (buffer, "insert", &pos_itr); + + retval = TRUE; + } + + return retval; +} + +static void ev_view_accessible_text_iface_init (AtkTextIface * iface) +{ + g_return_if_fail (iface != NULL); + + iface->get_text = ev_view_accessible_get_text; + iface->get_character_at_offset = ev_view_accessible_get_character_at_offset; + iface->get_text_before_offset = ev_view_accessible_get_text_before_offset; + iface->get_text_at_offset = ev_view_accessible_get_text_at_offset; + iface->get_text_after_offset = ev_view_accessible_get_text_after_offset; + iface->get_caret_offset = ev_view_accessible_get_caret_offset; + iface->set_caret_offset = ev_view_accessible_set_caret_offset; + iface->get_character_count = ev_view_accessible_get_character_count; + iface->get_n_selections = ev_view_accessible_get_n_selections; + iface->get_selection = ev_view_accessible_get_selection; + iface->add_selection = ev_view_accessible_add_selection; + iface->remove_selection = ev_view_accessible_remove_selection; + iface->set_selection = ev_view_accessible_set_selection; + iface->get_run_attributes = ev_view_accessible_get_run_attributes; + iface->get_default_attributes = ev_view_accessible_get_default_attributes; + iface->get_character_extents = ev_view_accessible_get_character_extents; + iface->get_offset_at_point = ev_view_accessible_get_offset_at_point; + return; +} + +static gboolean +ev_view_accessible_idle_do_action (gpointer data) +{ + EvViewAccessiblePriv* priv = EV_VIEW_ACCESSIBLE_GET_PRIVATE (data); + + ev_view_scroll (EV_VIEW (gtk_accessible_get_widget (GTK_ACCESSIBLE (data))), + priv->idle_scroll, + FALSE); + priv->action_idle_handler = 0; + return FALSE; +} + +static gboolean +ev_view_accessible_action_do_action (AtkAction *action, + gint i) +{ + EvViewAccessiblePriv* priv = EV_VIEW_ACCESSIBLE_GET_PRIVATE (action); + + if (gtk_accessible_get_widget (GTK_ACCESSIBLE (action)) == NULL) + return FALSE; + + if (priv->action_idle_handler) + return FALSE; + + switch (i) { + case ACTION_SCROLL_UP: + priv->idle_scroll = GTK_SCROLL_PAGE_BACKWARD; + break; + case ACTION_SCROLL_DOWN: + priv->idle_scroll = GTK_SCROLL_PAGE_FORWARD; + break; + default: + return FALSE; + } + priv->action_idle_handler = g_idle_add (ev_view_accessible_idle_do_action, + action); + return TRUE; +} + +static gint +ev_view_accessible_action_get_n_actions (AtkAction *action) +{ + return LAST_ACTION; +} + +static const gchar * +ev_view_accessible_action_get_description (AtkAction *action, + gint i) +{ + EvViewAccessiblePriv* priv = EV_VIEW_ACCESSIBLE_GET_PRIVATE (action); + + if (i < 0 || i >= LAST_ACTION) + return NULL; + + if (priv->action_descriptions[i]) + return priv->action_descriptions[i]; + else + return ev_view_accessible_action_descriptions[i]; +} + +static const gchar * +ev_view_accessible_action_get_name (AtkAction *action, + gint i) +{ + if (i < 0 || i >= LAST_ACTION) + return NULL; + + return ev_view_accessible_action_names[i]; +} + +static gboolean +ev_view_accessible_action_set_description (AtkAction *action, + gint i, + const gchar *description) +{ + EvViewAccessiblePriv* priv = EV_VIEW_ACCESSIBLE_GET_PRIVATE (action); + gchar *old_description; + + if (i < 0 || i >= LAST_ACTION) + return FALSE; + + old_description = priv->action_descriptions[i]; + priv->action_descriptions[i] = g_strdup (description); + g_free (old_description); + + return TRUE; +} + +static void ev_view_accessible_action_iface_init (AtkActionIface * iface) +{ + iface->do_action = ev_view_accessible_action_do_action; + iface->get_n_actions = ev_view_accessible_action_get_n_actions; + iface->get_description = ev_view_accessible_action_get_description; + iface->get_name = ev_view_accessible_action_get_name; + iface->set_description = ev_view_accessible_action_set_description; +} + +GType ev_view_accessible_get_type (void) +{ + static GType type = 0; + + if (G_UNLIKELY (type == 0)) { + GTypeInfo tinfo = { + 0, /* class size */ + (GBaseInitFunc) NULL, /* base init */ + (GBaseFinalizeFunc) NULL, /* base finalize */ + (GClassInitFunc) ev_view_accessible_class_init, /* class init */ + (GClassFinalizeFunc) NULL, /* class finalize */ + NULL, /* class data */ + 0, /* instance size */ + 0, /* nb preallocs */ + (GInstanceInitFunc) NULL, /* instance init */ + NULL /* value table */ + }; + + const GInterfaceInfo atk_text_info = { + (GInterfaceInitFunc) + ev_view_accessible_text_iface_init, + (GInterfaceFinalizeFunc) NULL, + NULL + }; + + const GInterfaceInfo atk_action_info = { + (GInterfaceInitFunc) + ev_view_accessible_action_iface_init, + (GInterfaceFinalizeFunc) NULL, + NULL + }; + /* + * Figure out the size of the class and instance + * we are deriving from + */ + AtkObjectFactory *factory; + GType derived_type; + GTypeQuery query; + GType derived_atk_type; + + derived_type = g_type_parent (EV_TYPE_VIEW); + factory = atk_registry_get_factory (atk_get_default_registry (), + derived_type); + derived_atk_type = atk_object_factory_get_accessible_type (factory); + + g_type_query (derived_atk_type, &query); + tinfo.class_size = query.class_size; + tinfo.instance_size = query.instance_size; + + type = g_type_register_static (derived_atk_type, "EvViewAccessible", + &tinfo, 0); + g_type_add_interface_static (type, ATK_TYPE_TEXT, + &atk_text_info); + g_type_add_interface_static (type, ATK_TYPE_ACTION, + &atk_action_info); + } + + return type; +} + +static AtkObject *ev_view_accessible_new(GObject * obj) +{ + AtkObject *accessible; + + g_return_val_if_fail(EV_IS_VIEW (obj), NULL); + + accessible = g_object_new (ev_view_accessible_get_type (), NULL); + atk_object_initialize (accessible, obj); + + atk_object_set_name (ATK_OBJECT (accessible), _("Document View")); + atk_object_set_role (ATK_OBJECT (accessible), ATK_ROLE_UNKNOWN); + + return accessible; +} + +typedef AtkObjectFactory EvViewAccessibleFactory; +typedef AtkObjectFactoryClass EvViewAccessibleFactoryClass; + +static void ev_view_accessible_factory_init (EvViewAccessibleFactory *factory) +{ +} + +static GType ev_view_accessible_factory_get_accessible_type(void) +{ + return ev_view_accessible_get_type(); +} + +static AtkObject *ev_view_accessible_factory_create_accessible (GObject * obj) +{ + return ev_view_accessible_new(obj); +} + +static void ev_view_accessible_factory_class_init (AtkObjectFactoryClass * klass) +{ + klass->create_accessible = ev_view_accessible_factory_create_accessible; + klass->get_accessible_type = ev_view_accessible_factory_get_accessible_type; +} + +G_DEFINE_TYPE (EvViewAccessibleFactory, ev_view_accessible_factory, ATK_TYPE_OBJECT_FACTORY) diff --git a/libview/ev-view-accessible.h b/libview/ev-view-accessible.h new file mode 100644 index 00000000..9ec7ed27 --- /dev/null +++ b/libview/ev-view-accessible.h @@ -0,0 +1,33 @@ +/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8; c-indent-level: 8 -*- */ +/* this file is part of evince, a mate document viewer + * + * Copyright (C) 2004 Red Hat, Inc + * + * Evince is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * Evince is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ + +#if !defined (EVINCE_COMPILATION) +#error "This is a private header." +#endif + +#ifndef __EV_VIEW_ACCESSIBLE_H__ +#define __EV_VIEW_ACCESSIBLE_H__ + +#include "ev-view.h" + +GType ev_view_accessible_factory_get_type (void); + +#endif /* __EV_VIEW_ACCESSIBLE_H__ */ + diff --git a/libview/ev-view-cursor.c b/libview/ev-view-cursor.c new file mode 100644 index 00000000..930e1f8e --- /dev/null +++ b/libview/ev-view-cursor.c @@ -0,0 +1,55 @@ +/* this file is part of evince, a mate document viewer + * + * Copyright (C) 2004 Red Hat, Inc + * + * Evince is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * Evince is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ + +#include "ev-view-cursor.h" + +GdkCursor * +ev_view_cursor_new (GdkDisplay *display, + EvViewCursor new_cursor) +{ + GdkCursor *cursor = NULL; + + switch (new_cursor) { + case EV_VIEW_CURSOR_NORMAL: + break; + case EV_VIEW_CURSOR_IBEAM: + cursor = gdk_cursor_new_for_display (display, GDK_XTERM); + break; + case EV_VIEW_CURSOR_LINK: + cursor = gdk_cursor_new_for_display (display, GDK_HAND2); + break; + case EV_VIEW_CURSOR_WAIT: + cursor = gdk_cursor_new_for_display (display, GDK_WATCH); + break; + case EV_VIEW_CURSOR_HIDDEN: + cursor = gdk_cursor_new_for_display (display, GDK_BLANK_CURSOR); + break; + case EV_VIEW_CURSOR_DRAG: + cursor = gdk_cursor_new_for_display (display, GDK_FLEUR); + break; + case EV_VIEW_CURSOR_AUTOSCROLL: + cursor = gdk_cursor_new_for_display (display, GDK_DOUBLE_ARROW); + break; + case EV_VIEW_CURSOR_ADD: + cursor = gdk_cursor_new_for_display (display, GDK_PLUS); + break; + } + + return cursor; +} diff --git a/libview/ev-view-cursor.h b/libview/ev-view-cursor.h new file mode 100644 index 00000000..9d5e8e53 --- /dev/null +++ b/libview/ev-view-cursor.h @@ -0,0 +1,47 @@ +/* this file is part of evince, a mate document viewer + * + * Copyright (C) 2004 Red Hat, Inc + * + * Evince is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * Evince is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ + +#if !defined (__EV_EVINCE_VIEW_H_INSIDE__) && !defined (EVINCE_COMPILATION) +#error "Only <evince-view.h> can be included directly." +#endif + +#ifndef __EV_VIEW_CURSOR_H__ +#define __EV_VIEW_CURSOR_H__ + +#include <gtk/gtk.h> + +G_BEGIN_DECLS + +typedef enum { + EV_VIEW_CURSOR_NORMAL, + EV_VIEW_CURSOR_IBEAM, + EV_VIEW_CURSOR_LINK, + EV_VIEW_CURSOR_WAIT, + EV_VIEW_CURSOR_HIDDEN, + EV_VIEW_CURSOR_DRAG, + EV_VIEW_CURSOR_AUTOSCROLL, + EV_VIEW_CURSOR_ADD +} EvViewCursor; + +GdkCursor *ev_view_cursor_new (GdkDisplay *display, + EvViewCursor cursor); + +G_END_DECLS + +#endif /* __EV_VIEW_CURSOR_H__ */ diff --git a/libview/ev-view-marshal.list b/libview/ev-view-marshal.list new file mode 100644 index 00000000..d7b0e75e --- /dev/null +++ b/libview/ev-view-marshal.list @@ -0,0 +1,2 @@ +VOID:ENUM,BOOLEAN +VOID:INT,INT diff --git a/libview/ev-view-presentation.c b/libview/ev-view-presentation.c new file mode 100644 index 00000000..31556fa2 --- /dev/null +++ b/libview/ev-view-presentation.c @@ -0,0 +1,1498 @@ +/* ev-view-presentation.c + * this file is part of evince, a mate document viewer + * + * Copyright (C) 2010 Carlos Garcia Campos <[email protected]> + * + * Evince is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * Evince is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ + +#include "config.h" + +#include <stdlib.h> +#include <glib/gi18n-lib.h> +#include <gdk/gdkkeysyms.h> + +#include "ev-view-presentation.h" +#include "ev-jobs.h" +#include "ev-job-scheduler.h" +#include "ev-transition-animation.h" +#include "ev-view-cursor.h" +#include "ev-page-cache.h" + +enum { + PROP_0, + PROP_DOCUMENT, + PROP_CURRENT_PAGE, + PROP_ROTATION, + PROP_INVERTED_COLORS +}; + +enum { + CHANGE_PAGE, + FINISHED, + N_SIGNALS +}; + +typedef enum { + EV_PRESENTATION_NORMAL, + EV_PRESENTATION_BLACK, + EV_PRESENTATION_WHITE, + EV_PRESENTATION_END +} EvPresentationState; + +struct _EvViewPresentation +{ + GtkWidget base; + + guint current_page; + cairo_surface_t *current_surface; + EvDocument *document; + guint rotation; + gboolean inverted_colors; + EvPresentationState state; + gdouble scale; + gint monitor_width; + gint monitor_height; + + /* Cursors */ + EvViewCursor cursor; + guint hide_cursor_timeout_id; + + /* Goto Window */ + GtkWidget *goto_window; + GtkWidget *goto_entry; + + /* Page Transition */ + guint trans_timeout_id; + + /* Animations */ + gboolean enable_animations; + EvTransitionAnimation *animation; + + /* Links */ + EvPageCache *page_cache; + + EvJob *prev_job; + EvJob *curr_job; + EvJob *next_job; +}; + +struct _EvViewPresentationClass +{ + GtkWidgetClass base_class; + + /* signals */ + void (* change_page) (EvViewPresentation *pview, + GtkScrollType scroll); + void (* finished) (EvViewPresentation *pview); +}; + +static guint signals[N_SIGNALS] = { 0 }; + +static void ev_view_presentation_next_page (EvViewPresentation *pview); +static void ev_view_presentation_previous_page (EvViewPresentation *pview); +static void ev_view_presentation_set_cursor_for_location (EvViewPresentation *pview, + gdouble x, + gdouble y); + +#define HIDE_CURSOR_TIMEOUT 5 + +G_DEFINE_TYPE (EvViewPresentation, ev_view_presentation, GTK_TYPE_WIDGET) + +static void +ev_view_presentation_set_normal (EvViewPresentation *pview) +{ + GtkWidget *widget = GTK_WIDGET (pview); + GtkStyle *style; + + if (pview->state == EV_PRESENTATION_NORMAL) + return; + + pview->state = EV_PRESENTATION_NORMAL; + style = gtk_widget_get_style (widget); + gdk_window_set_background (gtk_widget_get_window (widget), &style->black); + gtk_widget_queue_draw (widget); +} + +static void +ev_view_presentation_set_black (EvViewPresentation *pview) +{ + GtkWidget *widget = GTK_WIDGET (pview); + GtkStyle *style; + + if (pview->state == EV_PRESENTATION_BLACK) + return; + + pview->state = EV_PRESENTATION_BLACK; + style = gtk_widget_get_style (widget); + gdk_window_set_background (gtk_widget_get_window (widget), &style->black); + gtk_widget_queue_draw (widget); +} + +static void +ev_view_presentation_set_white (EvViewPresentation *pview) +{ + GtkWidget *widget = GTK_WIDGET (pview); + GtkStyle *style; + + if (pview->state == EV_PRESENTATION_WHITE) + return; + + pview->state = EV_PRESENTATION_WHITE; + style = gtk_widget_get_style (widget); + gdk_window_set_background (gtk_widget_get_window (widget), &style->white); + gtk_widget_queue_draw (widget); +} + +static void +ev_view_presentation_set_end (EvViewPresentation *pview) +{ + GtkWidget *widget = GTK_WIDGET (pview); + + if (pview->state == EV_PRESENTATION_END) + return; + + pview->state = EV_PRESENTATION_END; + gtk_widget_queue_draw (widget); +} + +static gdouble +ev_view_presentation_get_scale_for_page (EvViewPresentation *pview, + guint page) +{ + if (!ev_document_is_page_size_uniform (pview->document) || pview->scale == 0) { + gdouble width, height; + + ev_document_get_page_size (pview->document, page, &width, &height); + if (pview->rotation == 90 || pview->rotation == 270) { + gdouble tmp; + + tmp = width; + width = height; + height = tmp; + } + pview->scale = MIN (pview->monitor_width / width, pview->monitor_height / height); + } + + return pview->scale; +} + +static void +ev_view_presentation_get_page_area (EvViewPresentation *pview, + GdkRectangle *area) +{ + GtkWidget *widget = GTK_WIDGET (pview); + GtkAllocation allocation; + gdouble doc_width, doc_height; + gint view_width, view_height; + gdouble scale; + + ev_document_get_page_size (pview->document, + pview->current_page, + &doc_width, &doc_height); + scale = ev_view_presentation_get_scale_for_page (pview, pview->current_page); + + if (pview->rotation == 90 || pview->rotation == 270) { + view_width = (gint)((doc_height * scale) + 0.5); + view_height = (gint)((doc_width * scale) + 0.5); + } else { + view_width = (gint)((doc_width * scale) + 0.5); + view_height = (gint)((doc_height * scale) + 0.5); + } + + gtk_widget_get_allocation (widget, &allocation); + + area->x = (MAX (0, allocation.width - view_width)) / 2; + area->y = (MAX (0, allocation.height - view_height)) / 2; + area->width = view_width; + area->height = view_height; +} + +/* Page Transition */ +static gboolean +transition_next_page (EvViewPresentation *pview) +{ + ev_view_presentation_next_page (pview); + + return FALSE; +} + +static void +ev_view_presentation_transition_stop (EvViewPresentation *pview) +{ + if (pview->trans_timeout_id > 0) + g_source_remove (pview->trans_timeout_id); + pview->trans_timeout_id = 0; +} + +static void +ev_view_presentation_transition_start (EvViewPresentation *pview) +{ + gdouble duration; + + if (!EV_IS_DOCUMENT_TRANSITION (pview->document)) + return; + + ev_view_presentation_transition_stop (pview); + + duration = ev_document_transition_get_page_duration (EV_DOCUMENT_TRANSITION (pview->document), + pview->current_page); + if (duration >= 0) { + pview->trans_timeout_id = + g_timeout_add_seconds (duration, + (GSourceFunc) transition_next_page, + pview); + } +} + +/* Animations */ +static void +ev_view_presentation_animation_cancel (EvViewPresentation *pview) +{ + if (pview->animation) { + g_object_unref (pview->animation); + pview->animation = NULL; + } +} + +static void +ev_view_presentation_transition_animation_finish (EvViewPresentation *pview) +{ + ev_view_presentation_animation_cancel (pview); + ev_view_presentation_transition_start (pview); + gtk_widget_queue_draw (GTK_WIDGET (pview)); +} + +static void +ev_view_presentation_transition_animation_frame (EvViewPresentation *pview, + gdouble progress) +{ + gtk_widget_queue_draw (GTK_WIDGET (pview)); +} + +static void +ev_view_presentation_animation_start (EvViewPresentation *pview, + gint new_page) +{ + EvTransitionEffect *effect = NULL; + cairo_surface_t *surface; + gint jump; + + if (!pview->enable_animations) + return; + + if (pview->current_page == new_page) + return; + + effect = ev_document_transition_get_effect (EV_DOCUMENT_TRANSITION (pview->document), + new_page); + if (!effect) + return; + + pview->animation = ev_transition_animation_new (effect); + + surface = pview->curr_job ? EV_JOB_RENDER (pview->curr_job)->surface : NULL; + ev_transition_animation_set_origin_surface (pview->animation, + surface != NULL ? + surface : pview->current_surface); + + jump = new_page - pview->current_page; + if (jump == -1) + surface = pview->prev_job ? EV_JOB_RENDER (pview->prev_job)->surface : NULL; + else if (jump == 1) + surface = pview->next_job ? EV_JOB_RENDER (pview->next_job)->surface : NULL; + else + surface = NULL; + if (surface) + ev_transition_animation_set_dest_surface (pview->animation, surface); + + g_signal_connect_swapped (pview->animation, "frame", + G_CALLBACK (ev_view_presentation_transition_animation_frame), + pview); + g_signal_connect_swapped (pview->animation, "finished", + G_CALLBACK (ev_view_presentation_transition_animation_finish), + pview); +} + +/* Page Navigation */ +static void +job_finished_cb (EvJob *job, + EvViewPresentation *pview) +{ + EvJobRender *job_render = EV_JOB_RENDER (job); + + if (pview->inverted_colors) + ev_document_misc_invert_surface (job_render->surface); + + if (job != pview->curr_job) + return; + + if (pview->animation) { + ev_transition_animation_set_dest_surface (pview->animation, + job_render->surface); + } else { + ev_view_presentation_transition_start (pview); + gtk_widget_queue_draw (GTK_WIDGET (pview)); + } +} + +static EvJob * +ev_view_presentation_schedule_new_job (EvViewPresentation *pview, + gint page, + EvJobPriority priority) +{ + EvJob *job; + gdouble scale; + + if (page < 0 || page >= ev_document_get_n_pages (pview->document)) + return NULL; + + scale = ev_view_presentation_get_scale_for_page (pview, page); + job = ev_job_render_new (pview->document, page, pview->rotation, scale, 0, 0); + g_signal_connect (job, "finished", + G_CALLBACK (job_finished_cb), + pview); + ev_job_scheduler_push_job (job, priority); + + return job; +} + +static void +ev_view_presentation_delete_job (EvViewPresentation *pview, + EvJob *job) +{ + if (!job) + return; + + g_signal_handlers_disconnect_by_func (job, job_finished_cb, pview); + ev_job_cancel (job); + g_object_unref (job); +} + +static void +ev_view_presentation_update_current_page (EvViewPresentation *pview, + guint page) +{ + gint jump; + + if (page < 0 || page >= ev_document_get_n_pages (pview->document)) + return; + + ev_view_presentation_animation_cancel (pview); + ev_view_presentation_animation_start (pview, page); + + jump = page - pview->current_page; + + switch (jump) { + case 0: + if (!pview->curr_job) + pview->curr_job = ev_view_presentation_schedule_new_job (pview, page, EV_JOB_PRIORITY_URGENT); + if (!pview->next_job) + pview->next_job = ev_view_presentation_schedule_new_job (pview, page + 1, EV_JOB_PRIORITY_HIGH); + if (!pview->prev_job) + pview->prev_job = ev_view_presentation_schedule_new_job (pview, page - 1, EV_JOB_PRIORITY_LOW); + break; + case -1: + ev_view_presentation_delete_job (pview, pview->next_job); + pview->next_job = pview->curr_job; + pview->curr_job = pview->prev_job; + + if (!pview->curr_job) + pview->curr_job = ev_view_presentation_schedule_new_job (pview, page, EV_JOB_PRIORITY_URGENT); + else + ev_job_scheduler_update_job (pview->curr_job, EV_JOB_PRIORITY_URGENT); + pview->prev_job = ev_view_presentation_schedule_new_job (pview, page - 1, EV_JOB_PRIORITY_HIGH); + ev_job_scheduler_update_job (pview->next_job, EV_JOB_PRIORITY_LOW); + + break; + case 1: + ev_view_presentation_delete_job (pview, pview->prev_job); + pview->prev_job = pview->curr_job; + pview->curr_job = pview->next_job; + + if (!pview->curr_job) + pview->curr_job = ev_view_presentation_schedule_new_job (pview, page, EV_JOB_PRIORITY_URGENT); + else + ev_job_scheduler_update_job (pview->curr_job, EV_JOB_PRIORITY_URGENT); + pview->next_job = ev_view_presentation_schedule_new_job (pview, page + 1, EV_JOB_PRIORITY_HIGH); + ev_job_scheduler_update_job (pview->prev_job, EV_JOB_PRIORITY_LOW); + + break; + case -2: + ev_view_presentation_delete_job (pview, pview->next_job); + ev_view_presentation_delete_job (pview, pview->curr_job); + pview->next_job = pview->prev_job; + + pview->curr_job = ev_view_presentation_schedule_new_job (pview, page, EV_JOB_PRIORITY_URGENT); + pview->prev_job = ev_view_presentation_schedule_new_job (pview, page - 1, EV_JOB_PRIORITY_HIGH); + if (!pview->next_job) + pview->next_job = ev_view_presentation_schedule_new_job (pview, page + 1, EV_JOB_PRIORITY_LOW); + else + ev_job_scheduler_update_job (pview->next_job, EV_JOB_PRIORITY_LOW); + break; + case 2: + ev_view_presentation_delete_job (pview, pview->prev_job); + ev_view_presentation_delete_job (pview, pview->curr_job); + pview->prev_job = pview->next_job; + + pview->curr_job = ev_view_presentation_schedule_new_job (pview, page, EV_JOB_PRIORITY_URGENT); + pview->next_job = ev_view_presentation_schedule_new_job (pview, page + 1, EV_JOB_PRIORITY_HIGH); + if (!pview->prev_job) + pview->prev_job = ev_view_presentation_schedule_new_job (pview, page - 1, EV_JOB_PRIORITY_LOW); + else + ev_job_scheduler_update_job (pview->prev_job, EV_JOB_PRIORITY_LOW); + break; + default: + ev_view_presentation_delete_job (pview, pview->prev_job); + ev_view_presentation_delete_job (pview, pview->curr_job); + ev_view_presentation_delete_job (pview, pview->next_job); + + pview->curr_job = ev_view_presentation_schedule_new_job (pview, page, EV_JOB_PRIORITY_URGENT); + if (jump > 0) { + pview->next_job = ev_view_presentation_schedule_new_job (pview, page + 1, EV_JOB_PRIORITY_HIGH); + pview->prev_job = ev_view_presentation_schedule_new_job (pview, page - 1, EV_JOB_PRIORITY_LOW); + } else { + pview->prev_job = ev_view_presentation_schedule_new_job (pview, page - 1, EV_JOB_PRIORITY_HIGH); + pview->next_job = ev_view_presentation_schedule_new_job (pview, page + 1, EV_JOB_PRIORITY_LOW); + } + } + + pview->current_page = page; + + if (pview->page_cache) + ev_page_cache_set_page_range (pview->page_cache, page, page); + + if (pview->cursor != EV_VIEW_CURSOR_HIDDEN) { + gint x, y; + + gtk_widget_get_pointer (GTK_WIDGET (pview), &x, &y); + ev_view_presentation_set_cursor_for_location (pview, x, y); + } + + if (EV_JOB_RENDER (pview->curr_job)->surface) + gtk_widget_queue_draw (GTK_WIDGET (pview)); +} + +static void +ev_view_presentation_next_page (EvViewPresentation *pview) +{ + guint n_pages; + gint new_page; + + switch (pview->state) { + case EV_PRESENTATION_BLACK: + case EV_PRESENTATION_WHITE: + ev_view_presentation_set_normal (pview); + case EV_PRESENTATION_END: + return; + case EV_PRESENTATION_NORMAL: + break; + } + + n_pages = ev_document_get_n_pages (pview->document); + new_page = pview->current_page + 1; + + if (new_page == n_pages) + ev_view_presentation_set_end (pview); + else + ev_view_presentation_update_current_page (pview, new_page); +} + +static void +ev_view_presentation_previous_page (EvViewPresentation *pview) +{ + gint new_page = 0; + + switch (pview->state) { + case EV_PRESENTATION_BLACK: + case EV_PRESENTATION_WHITE: + ev_view_presentation_set_normal (pview); + return; + case EV_PRESENTATION_END: + pview->state = EV_PRESENTATION_NORMAL; + new_page = pview->current_page; + break; + case EV_PRESENTATION_NORMAL: + new_page = pview->current_page - 1; + break; + } + + ev_view_presentation_update_current_page (pview, new_page); +} + +/* Goto Window */ +#define KEY_IS_NUMERIC(keyval) \ + ((keyval >= GDK_0 && keyval <= GDK_9) || (keyval >= GDK_KP_0 && keyval <= GDK_KP_9)) + +/* Cut and paste from gtkwindow.c */ +static void +send_focus_change (GtkWidget *widget, + gboolean in) +{ + GdkEvent *fevent = gdk_event_new (GDK_FOCUS_CHANGE); + + fevent->focus_change.type = GDK_FOCUS_CHANGE; + fevent->focus_change.window = gtk_widget_get_window (widget); + fevent->focus_change.in = in; + if (fevent->focus_change.window) + g_object_ref (fevent->focus_change.window); + + gtk_widget_send_focus_change (widget, fevent); + + gdk_event_free (fevent); +} + +static void +ev_view_presentation_goto_window_hide (EvViewPresentation *pview) +{ + /* send focus-in event */ + send_focus_change (pview->goto_entry, FALSE); + gtk_widget_hide (pview->goto_window); + gtk_entry_set_text (GTK_ENTRY (pview->goto_entry), ""); +} + +static gboolean +ev_view_presentation_goto_window_delete_event (GtkWidget *widget, + GdkEventAny *event, + EvViewPresentation *pview) +{ + ev_view_presentation_goto_window_hide (pview); + + return TRUE; +} + +static gboolean +ev_view_presentation_goto_window_key_press_event (GtkWidget *widget, + GdkEventKey *event, + EvViewPresentation *pview) +{ + switch (event->keyval) { + case GDK_Escape: + case GDK_Tab: + case GDK_KP_Tab: + case GDK_ISO_Left_Tab: + ev_view_presentation_goto_window_hide (pview); + return TRUE; + case GDK_Return: + case GDK_KP_Enter: + case GDK_ISO_Enter: + case GDK_BackSpace: + case GDK_Delete: + return FALSE; + default: + if (!KEY_IS_NUMERIC (event->keyval)) + return TRUE; + } + + return FALSE; +} + +static gboolean +ev_view_presentation_goto_window_button_press_event (GtkWidget *widget, + GdkEventButton *event, + EvViewPresentation *pview) +{ + ev_view_presentation_goto_window_hide (pview); + + return TRUE; +} + +static void +ev_view_presentation_goto_entry_activate (GtkEntry *entry, + EvViewPresentation *pview) +{ + const gchar *text; + gint page; + + text = gtk_entry_get_text (entry); + page = atoi (text) - 1; + + ev_view_presentation_goto_window_hide (pview); + ev_view_presentation_update_current_page (pview, page); +} + +static void +ev_view_presentation_goto_window_create (EvViewPresentation *pview) +{ + GtkWidget *frame, *hbox, *label; + GtkWindow *toplevel, *goto_window; + + toplevel = GTK_WINDOW (gtk_widget_get_toplevel (GTK_WIDGET (pview))); + goto_window = GTK_WINDOW (pview->goto_window); + + if (pview->goto_window) { + if (gtk_window_has_group (toplevel)) + gtk_window_group_add_window (gtk_window_get_group (toplevel), goto_window); + else if (gtk_window_has_group (goto_window)) + gtk_window_group_remove_window (gtk_window_get_group (goto_window), goto_window); + + return; + } + + pview->goto_window = gtk_window_new (GTK_WINDOW_POPUP); + gtk_window_set_screen (goto_window, gtk_widget_get_screen (GTK_WIDGET (pview))); + + if (gtk_window_has_group (toplevel)) + gtk_window_group_add_window (gtk_window_get_group (toplevel), goto_window); + + gtk_window_set_modal (goto_window, TRUE); + + g_signal_connect (pview->goto_window, "delete_event", + G_CALLBACK (ev_view_presentation_goto_window_delete_event), + pview); + g_signal_connect (pview->goto_window, "key_press_event", + G_CALLBACK (ev_view_presentation_goto_window_key_press_event), + pview); + g_signal_connect (pview->goto_window, "button_press_event", + G_CALLBACK (ev_view_presentation_goto_window_button_press_event), + pview); + + frame = gtk_frame_new (NULL); + gtk_frame_set_shadow_type (GTK_FRAME (frame), GTK_SHADOW_ETCHED_IN); + gtk_container_add (GTK_CONTAINER (pview->goto_window), frame); + gtk_widget_show (frame); + + hbox = gtk_hbox_new (FALSE, 0); + gtk_container_set_border_width (GTK_CONTAINER (hbox), 3); + gtk_container_add (GTK_CONTAINER (frame), hbox); + gtk_widget_show (hbox); + + label = gtk_label_new (_("Jump to page:")); + gtk_box_pack_start (GTK_BOX (hbox), label, TRUE, TRUE, 3); + gtk_widget_show (label); + gtk_widget_realize (label); + + pview->goto_entry = gtk_entry_new (); + g_signal_connect (pview->goto_entry, "activate", + G_CALLBACK (ev_view_presentation_goto_entry_activate), + pview); + gtk_box_pack_start (GTK_BOX (hbox), pview->goto_entry, TRUE, TRUE, 0); + gtk_widget_show (pview->goto_entry); + gtk_widget_realize (pview->goto_entry); +} + +static void +ev_view_presentation_goto_entry_grab_focus (EvViewPresentation *pview) +{ + GtkWidgetClass *entry_parent_class; + + entry_parent_class = g_type_class_peek_parent (GTK_ENTRY_GET_CLASS (pview->goto_entry)); + (entry_parent_class->grab_focus) (pview->goto_entry); + + send_focus_change (pview->goto_entry, TRUE); +} + +static void +ev_view_presentation_goto_window_send_key_event (EvViewPresentation *pview, + GdkEvent *event) +{ + GdkEventKey *new_event; + GdkScreen *screen; + + /* Move goto window off screen */ + screen = gtk_widget_get_screen (GTK_WIDGET (pview)); + gtk_window_move (GTK_WINDOW (pview->goto_window), + gdk_screen_get_width (screen) + 1, + gdk_screen_get_height (screen) + 1); + gtk_widget_show (pview->goto_window); + + new_event = (GdkEventKey *) gdk_event_copy (event); + g_object_unref (new_event->window); + new_event->window = gtk_widget_get_window (pview->goto_window); + if (new_event->window) + g_object_ref (new_event->window); + gtk_widget_realize (pview->goto_window); + + gtk_widget_event (pview->goto_window, (GdkEvent *)new_event); + gdk_event_free ((GdkEvent *)new_event); + gtk_widget_hide (pview->goto_window); +} + +/* Links */ +static gboolean +ev_view_presentation_link_is_supported (EvViewPresentation *pview, + EvLink *link) +{ + EvLinkAction *action; + + action = ev_link_get_action (link); + if (!action) + return FALSE; + + switch (ev_link_action_get_action_type (action)) { + case EV_LINK_ACTION_TYPE_GOTO_DEST: + return ev_link_action_get_dest (action) != NULL; + case EV_LINK_ACTION_TYPE_NAMED: + return TRUE; + default: + return FALSE; + } + + return FALSE; +} + +static EvLink * +ev_view_presentation_get_link_at_location (EvViewPresentation *pview, + gdouble x, + gdouble y) +{ + GdkRectangle page_area; + EvMappingList *link_mapping; + EvLink *link; + gdouble width, height; + gdouble new_x, new_y; + gdouble scale; + + if (!pview->page_cache) + return NULL; + + ev_document_get_page_size (pview->document, pview->current_page, &width, &height); + ev_view_presentation_get_page_area (pview, &page_area); + scale = ev_view_presentation_get_scale_for_page (pview, pview->current_page); + x = (x - page_area.x) / scale; + y = (y - page_area.y) / scale; + switch (pview->rotation) { + case 0: + case 360: + new_x = x; + new_y = y; + break; + case 90: + new_x = y; + new_y = height - x; + break; + case 180: + new_x = width - x; + new_y = height - y; + break; + case 270: + new_x = width - y; + new_y = x; + break; + default: + g_assert_not_reached (); + } + + link_mapping = ev_page_cache_get_link_mapping (pview->page_cache, pview->current_page); + + link = link_mapping ? ev_mapping_list_get_data (link_mapping, new_x, new_y) : NULL; + + return link && ev_view_presentation_link_is_supported (pview, link) ? link : NULL; +} + +static void +ev_vew_presentation_goto_link_dest (EvViewPresentation *pview, + EvLink *link) +{ + EvLinkAction *action; + + action = ev_link_get_action (link); + + if (ev_link_action_get_action_type (action) == EV_LINK_ACTION_TYPE_NAMED) { + const gchar *name = ev_link_action_get_name (action); + + if (g_ascii_strcasecmp (name, "FirstPage") == 0) { + ev_view_presentation_update_current_page (pview, 0); + } else if (g_ascii_strcasecmp (name, "PrevPage") == 0) { + ev_view_presentation_update_current_page (pview, pview->current_page - 1); + } else if (g_ascii_strcasecmp (name, "NextPage") == 0) { + ev_view_presentation_update_current_page (pview, pview->current_page + 1); + } else if (g_ascii_strcasecmp (name, "LastPage") == 0) { + gint n_pages; + + n_pages = ev_document_get_n_pages (pview->document); + ev_view_presentation_update_current_page (pview, n_pages - 1); + } + } else { + EvLinkDest *dest; + gint page; + + dest = ev_link_action_get_dest (action); + page = ev_document_links_get_dest_page (EV_DOCUMENT_LINKS (pview->document), dest); + ev_view_presentation_update_current_page (pview, page); + } +} + +/* Cursors */ +static void +ev_view_presentation_set_cursor (EvViewPresentation *pview, + EvViewCursor view_cursor) +{ + GtkWidget *widget; + GdkCursor *cursor; + + if (pview->cursor == view_cursor) + return; + + widget = GTK_WIDGET (pview); + if (!gtk_widget_get_realized (widget)) + gtk_widget_realize (widget); + + pview->cursor = view_cursor; + + cursor = ev_view_cursor_new (gtk_widget_get_display (widget), view_cursor); + gdk_window_set_cursor (gtk_widget_get_window (widget), cursor); + gdk_flush (); + if (cursor) + gdk_cursor_unref (cursor); +} + +static void +ev_view_presentation_set_cursor_for_location (EvViewPresentation *pview, + gdouble x, + gdouble y) +{ + if (ev_view_presentation_get_link_at_location (pview, x, y)) + ev_view_presentation_set_cursor (pview, EV_VIEW_CURSOR_LINK); + else + ev_view_presentation_set_cursor (pview, EV_VIEW_CURSOR_NORMAL); +} + +static gboolean +hide_cursor_timeout_cb (EvViewPresentation *pview) +{ + ev_view_presentation_set_cursor (pview, EV_VIEW_CURSOR_HIDDEN); + pview->hide_cursor_timeout_id = 0; + + return FALSE; +} + +static void +ev_view_presentation_hide_cursor_timeout_stop (EvViewPresentation *pview) +{ + if (pview->hide_cursor_timeout_id > 0) + g_source_remove (pview->hide_cursor_timeout_id); + pview->hide_cursor_timeout_id = 0; +} + +static void +ev_view_presentation_hide_cursor_timeout_start (EvViewPresentation *pview) +{ + ev_view_presentation_hide_cursor_timeout_stop (pview); + pview->hide_cursor_timeout_id = + g_timeout_add_seconds (HIDE_CURSOR_TIMEOUT, + (GSourceFunc)hide_cursor_timeout_cb, + pview); +} + +static void +ev_view_presentation_update_current_surface (EvViewPresentation *pview, + cairo_surface_t *surface) +{ + if (!surface || pview->current_surface == surface) + return; + + cairo_surface_reference (surface); + if (pview->current_surface) + cairo_surface_destroy (pview->current_surface); + pview->current_surface = surface; +} + +static void +ev_view_presentation_destroy (GtkObject *object) +{ + EvViewPresentation *pview = EV_VIEW_PRESENTATION (object); + + if (pview->document) { + g_object_unref (pview->document); + pview->document = NULL; + } + + ev_view_presentation_animation_cancel (pview); + ev_view_presentation_transition_stop (pview); + ev_view_presentation_hide_cursor_timeout_stop (pview); + + if (pview->curr_job) { + ev_view_presentation_delete_job (pview, pview->curr_job); + pview->curr_job = NULL; + } + + if (pview->prev_job) { + ev_view_presentation_delete_job (pview, pview->prev_job); + pview->prev_job = NULL; + } + + if (pview->next_job) { + ev_view_presentation_delete_job (pview, pview->next_job); + pview->next_job = NULL; + } + + if (pview->current_surface) { + cairo_surface_destroy (pview->current_surface); + pview->current_surface = NULL; + } + + if (pview->page_cache) { + g_object_unref (pview->page_cache); + pview->page_cache = NULL; + } + + if (pview->goto_window) { + gtk_widget_destroy (pview->goto_window); + pview->goto_window = NULL; + pview->goto_entry = NULL; + } + + GTK_OBJECT_CLASS (ev_view_presentation_parent_class)->destroy (object); +} + +static void +ev_view_presentation_size_request (GtkWidget *widget, + GtkRequisition *requisition) +{ + requisition->width = 0; + requisition->height = 0; +} + +static void +ev_view_presentation_draw_end_page (EvViewPresentation *pview) +{ + GtkWidget *widget = GTK_WIDGET (pview); + PangoLayout *layout; + PangoFontDescription *font_desc; + gchar *markup; + GtkAllocation allocation; + GdkRectangle area = {0}; + const gchar *text = _("End of presentation. Click to exit."); + + if (pview->state != EV_PRESENTATION_END) + return; + + layout = gtk_widget_create_pango_layout (widget, NULL); + markup = g_strdup_printf ("<span foreground=\"white\">%s</span>", text); + pango_layout_set_markup (layout, markup, -1); + g_free (markup); + + font_desc = pango_font_description_new (); + pango_font_description_set_size (font_desc, 16 * PANGO_SCALE); + pango_layout_set_font_description (layout, font_desc); + + gtk_widget_get_allocation (widget, &allocation); + area.width = allocation.width; + area.height = allocation.height; + + gtk_paint_layout (gtk_widget_get_style (widget), + gtk_widget_get_window (widget), + gtk_widget_get_state (widget), + FALSE, + &area, + widget, + NULL, + 15, + 15, + layout); + + pango_font_description_free (font_desc); + g_object_unref (layout); +} + +static gboolean +ev_view_presentation_expose_event (GtkWidget *widget, + GdkEventExpose *event) +{ + EvViewPresentation *pview = EV_VIEW_PRESENTATION (widget); + GdkRectangle page_area; + GdkRectangle overlap; + cairo_surface_t *surface; + cairo_t *cr; + + switch (pview->state) { + case EV_PRESENTATION_END: + ev_view_presentation_draw_end_page (pview); + return FALSE; + case EV_PRESENTATION_BLACK: + case EV_PRESENTATION_WHITE: + return FALSE; + case EV_PRESENTATION_NORMAL: + break; + } + + if (pview->animation) { + if (ev_transition_animation_ready (pview->animation)) { + ev_view_presentation_get_page_area (pview, &page_area); + + cr = gdk_cairo_create (gtk_widget_get_window (widget)); + + /* normalize to x=0, y=0 */ + cairo_translate (cr, page_area.x, page_area.y); + page_area.x = page_area.y = 0; + + /* Try to fix rounding errors */ + page_area.width--; + + ev_transition_animation_paint (pview->animation, cr, page_area); + cairo_destroy (cr); + } + + return TRUE; + } + + surface = pview->curr_job ? EV_JOB_RENDER (pview->curr_job)->surface : NULL; + if (surface) { + ev_view_presentation_update_current_surface (pview, surface); + } else if (pview->current_surface) { + surface = pview->current_surface; + } else { + return FALSE; + } + + ev_view_presentation_get_page_area (pview, &page_area); + if (gdk_rectangle_intersect (&page_area, &(event->area), &overlap)) { + cr = gdk_cairo_create (gtk_widget_get_window (widget)); + + /* Try to fix rounding errors. See bug #438760 */ + if (overlap.width == page_area.width) + overlap.width--; + + cairo_rectangle (cr, overlap.x, overlap.y, overlap.width, overlap.height); + cairo_set_source_surface (cr, surface, page_area.x, page_area.y); + cairo_fill (cr); + cairo_destroy (cr); + } + + return FALSE; +} + +static gboolean +ev_view_presentation_key_press_event (GtkWidget *widget, + GdkEventKey *event) +{ + EvViewPresentation *pview = EV_VIEW_PRESENTATION (widget); + + if (pview->state == EV_PRESENTATION_END) + return gtk_bindings_activate_event (GTK_OBJECT (widget), event); + + switch (event->keyval) { + case GDK_b: + case GDK_B: + case GDK_period: + case GDK_KP_Decimal: + if (pview->state == EV_PRESENTATION_BLACK) + ev_view_presentation_set_normal (pview); + else + ev_view_presentation_set_black (pview); + + return TRUE; + case GDK_w: + case GDK_W: + if (pview->state == EV_PRESENTATION_WHITE) + ev_view_presentation_set_normal (pview); + else + ev_view_presentation_set_white (pview); + + return TRUE; + case GDK_Home: + if (pview->state == EV_PRESENTATION_NORMAL) { + ev_view_presentation_update_current_page (pview, 0); + return TRUE; + } + break; + case GDK_End: + if (pview->state == EV_PRESENTATION_NORMAL) { + gint page; + + page = ev_document_get_n_pages (pview->document) - 1; + ev_view_presentation_update_current_page (pview, page); + + return TRUE; + } + break; + default: + break; + } + + ev_view_presentation_set_normal (pview); + + if (ev_document_get_n_pages (pview->document) > 1 && KEY_IS_NUMERIC (event->keyval)) { + gint x, y; + + ev_view_presentation_goto_window_create (pview); + ev_view_presentation_goto_window_send_key_event (pview, (GdkEvent *)event); + gtk_widget_get_pointer (GTK_WIDGET (pview), &x, &y); + gtk_window_move (GTK_WINDOW (pview->goto_window), x, y); + gtk_widget_show (pview->goto_window); + ev_view_presentation_goto_entry_grab_focus (pview); + + return TRUE; + } + + return gtk_bindings_activate_event (GTK_OBJECT (widget), event); +} + +static gboolean +ev_view_presentation_button_release_event (GtkWidget *widget, + GdkEventButton *event) +{ + EvViewPresentation *pview = EV_VIEW_PRESENTATION (widget); + + switch (event->button) { + case 1: { + EvLink *link; + + if (pview->state == EV_PRESENTATION_END) { + g_signal_emit (pview, signals[FINISHED], 0, NULL); + + return FALSE; + } + + link = ev_view_presentation_get_link_at_location (pview, + event->x, + event->y); + if (link) + ev_vew_presentation_goto_link_dest (pview, link); + else + ev_view_presentation_next_page (pview); + } + break; + case 3: + ev_view_presentation_previous_page (pview); + break; + default: + break; + } + + return FALSE; +} + +static gint +ev_view_presentation_focus_out (GtkWidget *widget, + GdkEventFocus *event) +{ + EvViewPresentation *pview = EV_VIEW_PRESENTATION (widget); + + if (pview->goto_window) + ev_view_presentation_goto_window_hide (pview); + + return FALSE; +} + +static gboolean +ev_view_presentation_motion_notify_event (GtkWidget *widget, + GdkEventMotion *event) +{ + EvViewPresentation *pview = EV_VIEW_PRESENTATION (widget); + + ev_view_presentation_hide_cursor_timeout_start (pview); + ev_view_presentation_set_cursor_for_location (pview, event->x, event->y); + + return FALSE; +} + +static gboolean +init_presentation (GtkWidget *widget) +{ + EvViewPresentation *pview = EV_VIEW_PRESENTATION (widget); + GdkScreen *screen = gtk_widget_get_screen (widget); + GdkRectangle monitor; + gint monitor_num; + + monitor_num = gdk_screen_get_monitor_at_window (screen, gtk_widget_get_window (widget)); + gdk_screen_get_monitor_geometry (screen, monitor_num, &monitor); + pview->monitor_width = monitor.width; + pview->monitor_height = monitor.height; + + ev_view_presentation_update_current_page (pview, pview->current_page); + ev_view_presentation_hide_cursor_timeout_start (pview); + + return FALSE; +} + +static void +ev_view_presentation_realize (GtkWidget *widget) +{ + GdkWindow *window; + GtkStyle *style; + GdkWindowAttr attributes; + GtkAllocation allocation; + + gtk_widget_set_realized (widget, TRUE); + + attributes.window_type = GDK_WINDOW_CHILD; + attributes.wclass = GDK_INPUT_OUTPUT; + attributes.visual = gtk_widget_get_visual (widget); + attributes.colormap = gtk_widget_get_colormap (widget); + + gtk_widget_get_allocation (widget, &allocation); + attributes.x = allocation.x; + attributes.y = allocation.y; + attributes.width = allocation.width; + attributes.height = allocation.height; + attributes.event_mask = GDK_EXPOSURE_MASK | + GDK_BUTTON_PRESS_MASK | + GDK_BUTTON_RELEASE_MASK | + GDK_SCROLL_MASK | + GDK_KEY_PRESS_MASK | + GDK_POINTER_MOTION_MASK | + GDK_POINTER_MOTION_HINT_MASK | + GDK_ENTER_NOTIFY_MASK | + GDK_LEAVE_NOTIFY_MASK; + + window = gdk_window_new (gtk_widget_get_parent_window (widget), + &attributes, + GDK_WA_X | GDK_WA_Y | + GDK_WA_COLORMAP | + GDK_WA_VISUAL); + + gdk_window_set_user_data (window, widget); + gtk_widget_set_window (widget, window); + + gtk_widget_style_attach (widget); + style = gtk_widget_get_style (widget); + gdk_window_set_background (window, &style->black); + + g_idle_add ((GSourceFunc)init_presentation, widget); +} + +static void +ev_view_presentation_change_page (EvViewPresentation *pview, + GtkScrollType scroll) +{ + switch (scroll) { + case GTK_SCROLL_PAGE_FORWARD: + ev_view_presentation_next_page (pview); + break; + case GTK_SCROLL_PAGE_BACKWARD: + ev_view_presentation_previous_page (pview); + break; + default: + g_assert_not_reached (); + } +} + +static gboolean +ev_view_presentation_scroll_event (GtkWidget *widget, + GdkEventScroll *event) +{ + EvViewPresentation *pview = EV_VIEW_PRESENTATION (widget); + guint state; + + state = event->state & gtk_accelerator_get_default_mod_mask (); + if (state != 0) + return FALSE; + + switch (event->direction) { + case GDK_SCROLL_DOWN: + case GDK_SCROLL_RIGHT: + ev_view_presentation_change_page (pview, GTK_SCROLL_PAGE_FORWARD); + break; + case GDK_SCROLL_UP: + case GDK_SCROLL_LEFT: + ev_view_presentation_change_page (pview, GTK_SCROLL_PAGE_BACKWARD); + break; + } + + return TRUE; +} + + +static void +add_change_page_binding_keypad (GtkBindingSet *binding_set, + guint keyval, + GdkModifierType modifiers, + GtkScrollType scroll) +{ + guint keypad_keyval = keyval - GDK_Left + GDK_KP_Left; + + gtk_binding_entry_add_signal (binding_set, keyval, modifiers, + "change_page", 1, + GTK_TYPE_SCROLL_TYPE, scroll); + gtk_binding_entry_add_signal (binding_set, keypad_keyval, modifiers, + "change_page", 1, + GTK_TYPE_SCROLL_TYPE, scroll); +} + +static void +ev_view_presentation_set_property (GObject *object, + guint prop_id, + const GValue *value, + GParamSpec *pspec) +{ + EvViewPresentation *pview = EV_VIEW_PRESENTATION (object); + + switch (prop_id) { + case PROP_DOCUMENT: + pview->document = g_value_dup_object (value); + pview->enable_animations = EV_IS_DOCUMENT_TRANSITION (pview->document); + break; + case PROP_CURRENT_PAGE: + pview->current_page = g_value_get_uint (value); + break; + case PROP_ROTATION: + pview->rotation = g_value_get_uint (value); + break; + case PROP_INVERTED_COLORS: + pview->inverted_colors = g_value_get_boolean (value); + break; + default: + G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); + } +} + +static GObject * +ev_view_presentation_constructor (GType type, + guint n_construct_properties, + GObjectConstructParam *construct_params) +{ + GObject *object; + EvViewPresentation *pview; + + object = G_OBJECT_CLASS (ev_view_presentation_parent_class)->constructor (type, + n_construct_properties, + construct_params); + pview = EV_VIEW_PRESENTATION (object); + + if (EV_IS_DOCUMENT_LINKS (pview->document)) { + pview->page_cache = ev_page_cache_new (pview->document); + ev_page_cache_set_flags (pview->page_cache, EV_PAGE_DATA_INCLUDE_LINKS); + } + + return object; +} + +static void +ev_view_presentation_class_init (EvViewPresentationClass *klass) +{ + GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass); + GObjectClass *gobject_class = G_OBJECT_CLASS (klass); + GtkObjectClass *gtk_object_class = GTK_OBJECT_CLASS (klass); + GtkBindingSet *binding_set; + + klass->change_page = ev_view_presentation_change_page; + + widget_class->size_request = ev_view_presentation_size_request; + widget_class->realize = ev_view_presentation_realize; + widget_class->expose_event = ev_view_presentation_expose_event; + widget_class->key_press_event = ev_view_presentation_key_press_event; + widget_class->button_release_event = ev_view_presentation_button_release_event; + widget_class->focus_out_event = ev_view_presentation_focus_out; + widget_class->motion_notify_event = ev_view_presentation_motion_notify_event; + widget_class->scroll_event = ev_view_presentation_scroll_event; + + gtk_object_class->destroy = ev_view_presentation_destroy; + + gobject_class->constructor = ev_view_presentation_constructor; + gobject_class->set_property = ev_view_presentation_set_property; + + g_object_class_install_property (gobject_class, + PROP_DOCUMENT, + g_param_spec_object ("document", + "Document", + "Document", + EV_TYPE_DOCUMENT, + G_PARAM_WRITABLE | + G_PARAM_CONSTRUCT_ONLY)); + g_object_class_install_property (gobject_class, + PROP_CURRENT_PAGE, + g_param_spec_uint ("current_page", + "Current Page", + "The current page", + 0, G_MAXUINT, 0, + G_PARAM_WRITABLE | + G_PARAM_CONSTRUCT_ONLY)); + g_object_class_install_property (gobject_class, + PROP_ROTATION, + g_param_spec_uint ("rotation", + "Rotation", + "Current rotation angle", + 0, 360, 0, + G_PARAM_WRITABLE | + G_PARAM_CONSTRUCT_ONLY)); + g_object_class_install_property (gobject_class, + PROP_INVERTED_COLORS, + g_param_spec_boolean ("inverted_colors", + "Inverted Colors", + "Whether presentation is displayed with inverted colors", + FALSE, + G_PARAM_WRITABLE | + G_PARAM_CONSTRUCT_ONLY)); + + signals[CHANGE_PAGE] = + g_signal_new ("change_page", + G_OBJECT_CLASS_TYPE (gobject_class), + G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION, + G_STRUCT_OFFSET (EvViewPresentationClass, change_page), + NULL, NULL, + g_cclosure_marshal_VOID__ENUM, + G_TYPE_NONE, 1, + GTK_TYPE_SCROLL_TYPE); + signals[FINISHED] = + g_signal_new ("finished", + G_OBJECT_CLASS_TYPE (gobject_class), + G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION, + G_STRUCT_OFFSET (EvViewPresentationClass, finished), + NULL, NULL, + g_cclosure_marshal_VOID__VOID, + G_TYPE_NONE, 0, + G_TYPE_NONE); + + binding_set = gtk_binding_set_by_class (klass); + add_change_page_binding_keypad (binding_set, GDK_Left, 0, GTK_SCROLL_PAGE_BACKWARD); + add_change_page_binding_keypad (binding_set, GDK_Right, 0, GTK_SCROLL_PAGE_FORWARD); + add_change_page_binding_keypad (binding_set, GDK_Up, 0, GTK_SCROLL_PAGE_BACKWARD); + add_change_page_binding_keypad (binding_set, GDK_Down, 0, GTK_SCROLL_PAGE_FORWARD); + gtk_binding_entry_add_signal (binding_set, GDK_space, 0, + "change_page", 1, + GTK_TYPE_SCROLL_TYPE, GTK_SCROLL_PAGE_FORWARD); + gtk_binding_entry_add_signal (binding_set, GDK_BackSpace, 0, + "change_page", 1, + GTK_TYPE_SCROLL_TYPE, GTK_SCROLL_PAGE_BACKWARD); + gtk_binding_entry_add_signal (binding_set, GDK_Page_Down, 0, + "change_page", 1, + GTK_TYPE_SCROLL_TYPE, GTK_SCROLL_PAGE_FORWARD); + gtk_binding_entry_add_signal (binding_set, GDK_Page_Up, 0, + "change_page", 1, + GTK_TYPE_SCROLL_TYPE, GTK_SCROLL_PAGE_BACKWARD); + gtk_binding_entry_add_signal (binding_set, GDK_J, 0, + "change_page", 1, + GTK_TYPE_SCROLL_TYPE, GTK_SCROLL_PAGE_FORWARD); + gtk_binding_entry_add_signal (binding_set, GDK_H, 0, + "change_page", 1, + GTK_TYPE_SCROLL_TYPE, GTK_SCROLL_PAGE_BACKWARD); + gtk_binding_entry_add_signal (binding_set, GDK_L, 0, + "change_page", 1, + GTK_TYPE_SCROLL_TYPE, GTK_SCROLL_PAGE_FORWARD); + gtk_binding_entry_add_signal (binding_set, GDK_K, 0, + "change_page", 1, + GTK_TYPE_SCROLL_TYPE, GTK_SCROLL_PAGE_BACKWARD); +} + +static void +ev_view_presentation_init (EvViewPresentation *pview) +{ + gtk_widget_set_can_focus (GTK_WIDGET (pview), TRUE); +} + +GtkWidget * +ev_view_presentation_new (EvDocument *document, + guint current_page, + guint rotation, + gboolean inverted_colors) +{ + g_return_val_if_fail (EV_IS_DOCUMENT (document), NULL); + g_return_val_if_fail (current_page < ev_document_get_n_pages (document), NULL); + + return GTK_WIDGET (g_object_new (EV_TYPE_VIEW_PRESENTATION, + "document", document, + "current_page", current_page, + "rotation", rotation, + "inverted_colors", inverted_colors, + NULL)); +} + +guint +ev_view_presentation_get_current_page (EvViewPresentation *pview) +{ + return pview->current_page; +} diff --git a/libview/ev-view-presentation.h b/libview/ev-view-presentation.h new file mode 100644 index 00000000..3aaf6f06 --- /dev/null +++ b/libview/ev-view-presentation.h @@ -0,0 +1,51 @@ +/* ev-view-presentation.h + * this file is part of evince, a mate document viewer + * + * Copyright (C) 2010 Carlos Garcia Campos <[email protected]> + * + * Evince is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * Evince is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ + +#if !defined (__EV_EVINCE_VIEW_H_INSIDE__) && !defined (EVINCE_COMPILATION) +#error "Only <evince-view.h> can be included directly." +#endif + +#ifndef __EV_VIEW_PRESENTATION_H__ +#define __EV_VIEW_PRESENTATION_H__ + +#include <gtk/gtk.h> + +#include <evince-document.h> + +G_BEGIN_DECLS + +#define EV_TYPE_VIEW_PRESENTATION (ev_view_presentation_get_type ()) +#define EV_VIEW_PRESENTATION(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), EV_TYPE_VIEW_PRESENTATION, EvViewPresentation)) +#define EV_IS_VIEW_PRESENTATION(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), EV_TYPE_VIEW_PRESENTATION)) + +typedef struct _EvViewPresentation EvViewPresentation; +typedef struct _EvViewPresentationClass EvViewPresentationClass; + +GType ev_view_presentation_get_type (void) G_GNUC_CONST; + +GtkWidget *ev_view_presentation_new (EvDocument *document, + guint current_page, + guint rotation, + gboolean inverted_colors); +guint ev_view_presentation_get_current_page (EvViewPresentation *pview); + +G_END_DECLS + +#endif /* __EV_VIEW_PRESENTATION_H__ */ diff --git a/libview/ev-view-private.h b/libview/ev-view-private.h new file mode 100644 index 00000000..8ecd12db --- /dev/null +++ b/libview/ev-view-private.h @@ -0,0 +1,225 @@ +/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8; c-indent-level: 8 -*- */ +/* this file is part of evince, a mate document viewer + * + * Copyright (C) 2004 Red Hat, Inc + * + * Evince is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * Evince is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ + +#if !defined (EVINCE_COMPILATION) +#error "This is a private header." +#endif + +#ifndef __EV_VIEW_PRIVATE_H__ +#define __EV_VIEW_PRIVATE_H__ + +#include "ev-view.h" +#include "ev-document-model.h" +#include "ev-pixbuf-cache.h" +#include "ev-page-cache.h" +#include "ev-jobs.h" +#include "ev-image.h" +#include "ev-form-field.h" +#include "ev-selection.h" +#include "ev-view-cursor.h" + +#define DRAG_HISTORY 10 + +/* Information for middle clicking and moving around the doc */ +typedef struct { + gboolean in_drag; + GdkPoint start; + gdouble hadj; + gdouble vadj; + guint drag_timeout_id; + guint release_timeout_id; + GdkPoint buffer[DRAG_HISTORY]; + GdkPoint momentum; +} DragInfo; + +/* Autoscrolling */ +typedef struct { + gboolean autoscrolling; + guint last_y; + guint start_y; + guint timeout_id; +} AutoScrollInfo; + +/* Information for handling selection */ +typedef struct { + gboolean in_drag; + GdkPoint start; + gboolean in_selection; + GList *selections; + EvSelectionStyle style; +} SelectionInfo; + +/* Information for handling images DND */ +typedef struct { + gboolean in_drag; + GdkPoint start; + EvImage *image; +} ImageDNDInfo; + +/* Annotation popup windows */ +typedef struct { + GtkWidget *window; + guint page; + + /* Current position */ + gint x; + gint y; + + /* EvView root position */ + gint parent_x; + gint parent_y; + + /* Document coords */ + gdouble orig_x; + gdouble orig_y; + + gboolean visible; + gboolean moved; +} EvViewWindowChild; + +typedef enum { + SCROLL_TO_KEEP_POSITION, + SCROLL_TO_PAGE_POSITION, + SCROLL_TO_CENTER, + SCROLL_TO_FIND_LOCATION, +} PendingScroll; + +typedef struct _EvHeightToPageCache { + gint rotation; + gdouble *height_to_page; + gdouble *dual_height_to_page; +} EvHeightToPageCache; + +struct _EvView { + GtkLayout layout; + + EvDocument *document; + + /* Find */ + GList **find_pages; + gint find_result; + gboolean jump_to_find_result; + gboolean highlight_find_results; + + EvDocumentModel *model; + EvPixbufCache *pixbuf_cache; + gsize pixbuf_cache_size; + EvPageCache *page_cache; + EvHeightToPageCache *height_to_page_cache; + EvViewCursor cursor; + EvJobRender *current_job; + + GtkRequisition requisition; + gboolean internal_size_request; + + /* Scrolling */ + GtkAdjustment *hadjustment; + GtkAdjustment *vadjustment; + + gint scroll_x; + gint scroll_y; + + PendingScroll pending_scroll; + gboolean pending_resize; + EvPoint pending_point; + + /* Current geometry */ + + gint start_page; + gint end_page; + gint current_page; + + gint rotation; + gdouble scale; + gint spacing; + + gboolean loading; + gboolean continuous; + gboolean dual_page; + gboolean fullscreen; + EvSizingMode sizing_mode; + GtkWidget *loading_window; + guint loading_timeout; + + /* Common for button press handling */ + int pressed_button; + + /* Information for middle clicking and dragging around. */ + DragInfo drag_info; + + /* Autoscrolling */ + AutoScrollInfo scroll_info; + + /* Selection */ + GdkPoint motion; + guint selection_update_id; + guint selection_scroll_id; + + EvViewSelectionMode selection_mode; + SelectionInfo selection_info; + + /* Copy link address selection */ + EvLinkAction *link_selected; + + /* Image DND */ + ImageDNDInfo image_dnd_info; + + /* Annotations */ + GList *window_children; + EvViewWindowChild *window_child_focus; + EvMapping *focus_annotation; + gboolean adding_annot; + EvAnnotationType adding_annot_type; + + /* Synctex */ + EvMapping *synctex_result; + + /* Accessibility */ + gboolean a11y_enabled; +}; + +struct _EvViewClass { + GtkLayoutClass parent_class; + + void (*binding_activated) (EvView *view, + GtkScrollType scroll, + gboolean horizontal); + void (*handle_link) (EvView *view, + EvLink *link); + void (*external_link) (EvView *view, + EvLinkAction *action); + void (*popup_menu) (EvView *view, + GList *items); + void (*selection_changed) (EvView *view); + void (*sync_source) (EvView *view, + EvSourceLink *link); + void (*annot_added) (EvView *view, + EvAnnotation *annot); +}; + +void _get_page_size_for_scale_and_rotation (EvDocument *document, + gint page, + gdouble scale, + gint rotation, + gint *page_width, + gint *page_height); + +#endif /* __EV_VIEW_PRIVATE_H__ */ + diff --git a/libview/ev-view-type-builtins.c.template b/libview/ev-view-type-builtins.c.template new file mode 100644 index 00000000..01ca13af --- /dev/null +++ b/libview/ev-view-type-builtins.c.template @@ -0,0 +1,44 @@ +/*** BEGIN file-header ***/ +#include <config.h> + +#include "ev-view-type-builtins.h" + +/*** END file-header ***/ + +/*** BEGIN file-production ***/ +/* enumerations from "@filename@" */ +#include "@filename@" +/*** END file-production ***/ + + +/*** BEGIN value-header ***/ +GType +@enum_name@_get_type (void) +{ + static volatile gsize g_define_type_id__volatile = 0; + + if (g_once_init_enter (&g_define_type_id__volatile)) { + static const G@Type@Value values[] = { +/*** END value-header ***/ + +/*** BEGIN value-production ***/ + { @VALUENAME@, "@VALUENAME@", "@valuenick@" }, +/*** END value-production ***/ + +/*** BEGIN value-tail ***/ + { 0, NULL, NULL } + }; + GType g_define_type_id = \ + g_@type@_register_static (g_intern_static_string ("@EnumName@"), values); + + g_once_init_leave (&g_define_type_id__volatile, g_define_type_id); + } + + return g_define_type_id__volatile; +} + +/*** END value-tail ***/ + +/*** BEGIN file-tail ***/ + +/*** END file-tail ***/ diff --git a/libview/ev-view-type-builtins.h.template b/libview/ev-view-type-builtins.h.template new file mode 100644 index 00000000..c4ba4974 --- /dev/null +++ b/libview/ev-view-type-builtins.h.template @@ -0,0 +1,29 @@ +/*** BEGIN file-header ***/ + +#if !defined (__EV_EVINCE_VIEW_H_INSIDE__) && !defined (EVINCE_COMPILATION) +#error "Only <evince-view.h> can be included directly." +#endif + +#ifndef EV_VIEW_TYPE_BUILTINS_H +#define EV_VIEW_TYPE_BUILTINS_H + +#include <glib-object.h> + +G_BEGIN_DECLS +/*** END file-header ***/ + +/*** BEGIN file-production ***/ + +/* enumerations from "@filename@" */ +/*** END file-production ***/ + +/*** BEGIN value-header ***/ +GType @enum_name@_get_type (void) G_GNUC_CONST; +#define EV_TYPE_@ENUMSHORT@ (@enum_name@_get_type ()) +/*** END value-header ***/ + +/*** BEGIN file-tail ***/ +G_END_DECLS + +#endif /* !EV_VIEW_TYPE_BUILTINS_H */ +/*** END file-tail ***/ diff --git a/libview/ev-view.c b/libview/ev-view.c new file mode 100644 index 00000000..b47d6047 --- /dev/null +++ b/libview/ev-view.c @@ -0,0 +1,6274 @@ +/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8; c-indent-level: 8 -*- */ +/* this file is part of evince, a mate document viewer + * + * Copyright (C) 2004 Red Hat, Inc + * + * Evince is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * Evince is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ + +#include "config.h" + +#include <stdlib.h> +#include <math.h> +#include <string.h> + +#include <glib/gi18n-lib.h> +#include <gtk/gtk.h> +#include <gdk/gdkkeysyms.h> + +#include "ev-mapping-list.h" +#include "ev-document-forms.h" +#include "ev-document-images.h" +#include "ev-document-links.h" +#include "ev-document-misc.h" +#include "ev-pixbuf-cache.h" +#include "ev-page-cache.h" +#include "ev-view-marshal.h" +#include "ev-document-annotations.h" +#include "ev-annotation-window.h" +#include "ev-loading-window.h" +#include "ev-view.h" +#include "ev-view-accessible.h" +#include "ev-view-private.h" +#include "ev-view-type-builtins.h" + +#define EV_VIEW_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), EV_TYPE_VIEW, EvViewClass)) +#define EV_IS_VIEW_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), EV_TYPE_VIEW)) +#define EV_VIEW_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), EV_TYPE_VIEW, EvViewClass)) + +enum { + SIGNAL_BINDING_ACTIVATED, + SIGNAL_HANDLE_LINK, + SIGNAL_EXTERNAL_LINK, + SIGNAL_POPUP_MENU, + SIGNAL_SELECTION_CHANGED, + SIGNAL_SYNC_SOURCE, + SIGNAL_ANNOT_ADDED, + N_SIGNALS +}; + +enum { + TARGET_DND_URI, + TARGET_DND_TEXT, + TARGET_DND_IMAGE +}; + +static guint signals[N_SIGNALS]; + +typedef enum { + EV_VIEW_FIND_NEXT, + EV_VIEW_FIND_PREV +} EvViewFindDirection; + +#define ZOOM_IN_FACTOR 1.2 +#define ZOOM_OUT_FACTOR (1.0/ZOOM_IN_FACTOR) + +#define SCROLL_TIME 150 + +/*** Scrolling ***/ +static void ev_view_set_scroll_adjustments (GtkLayout *layout, + GtkAdjustment *hadjustment, + GtkAdjustment *vadjustment); +static void view_update_range_and_current_page (EvView *view); +static void set_scroll_adjustment (EvView *view, + GtkOrientation orientation, + GtkAdjustment *adjustment); +static void add_scroll_binding_keypad (GtkBindingSet *binding_set, + guint keyval, + GdkModifierType modifiers, + GtkScrollType scroll, + gboolean horizontal); +static void ensure_rectangle_is_visible (EvView *view, + GdkRectangle *rect); + +/*** Geometry computations ***/ +static void compute_border (EvView *view, + int width, + int height, + GtkBorder *border); +static void get_page_y_offset (EvView *view, + int page, + int *y_offset); +static void view_rect_to_doc_rect (EvView *view, + GdkRectangle *view_rect, + GdkRectangle *page_area, + EvRectangle *doc_rect); +static void doc_rect_to_view_rect (EvView *view, + int page, + EvRectangle *doc_rect, + GdkRectangle *view_rect); +static gboolean get_dual_even_left (EvView *view); +static void find_page_at_location (EvView *view, + gdouble x, + gdouble y, + gint *page, + gint *x_offset, + gint *y_offset); +static void doc_point_to_view_point (EvView *view, + int page, + EvPoint *doc_point, + GdkPoint *view_point); +/*** Hyperrefs ***/ +static EvLink * ev_view_get_link_at_location (EvView *view, + gdouble x, + gdouble y); +static char* tip_from_link (EvView *view, + EvLink *link); +/*** Forms ***/ +static EvFormField *ev_view_get_form_field_at_location (EvView *view, + gdouble x, + gdouble y); + +/*** Annotations ***/ +static EvAnnotation *ev_view_get_annotation_at_location (EvView *view, + gdouble x, + gdouble y); +static void show_annotation_windows (EvView *view, + gint page); +static void hide_annotation_windows (EvView *view, + gint page); +/*** GtkWidget implementation ***/ +static void ev_view_size_request_continuous_dual_page (EvView *view, + GtkRequisition *requisition); +static void ev_view_size_request_continuous (EvView *view, + GtkRequisition *requisition); +static void ev_view_size_request_dual_page (EvView *view, + GtkRequisition *requisition); +static void ev_view_size_request_single_page (EvView *view, + GtkRequisition *requisition); +static void ev_view_size_request (GtkWidget *widget, + GtkRequisition *requisition); +static void ev_view_size_allocate (GtkWidget *widget, + GtkAllocation *allocation); +static void ev_view_realize (GtkWidget *widget); +static gboolean ev_view_scroll_event (GtkWidget *widget, + GdkEventScroll *event); +static gboolean ev_view_expose_event (GtkWidget *widget, + GdkEventExpose *event); +static gboolean ev_view_popup_menu (GtkWidget *widget); +static gboolean ev_view_button_press_event (GtkWidget *widget, + GdkEventButton *event); +static gboolean ev_view_motion_notify_event (GtkWidget *widget, + GdkEventMotion *event); +static gboolean ev_view_button_release_event (GtkWidget *widget, + GdkEventButton *event); +static gboolean ev_view_enter_notify_event (GtkWidget *widget, + GdkEventCrossing *event); +static gboolean ev_view_leave_notify_event (GtkWidget *widget, + GdkEventCrossing *event); +static void ev_view_style_set (GtkWidget *widget, + GtkStyle *old_style); +static void ev_view_remove_all (EvView *view); + +static AtkObject *ev_view_get_accessible (GtkWidget *widget); + +/*** Drawing ***/ +static void highlight_find_results (EvView *view, + int page); +static void highlight_forward_search_results (EvView *view, + int page); +static void focus_annotation (EvView *view, + int page, + GdkRectangle *clip); +static void draw_one_page (EvView *view, + gint page, + cairo_t *cr, + GdkRectangle *page_area, + GtkBorder *border, + GdkRectangle *expose_area, + gboolean *page_ready); +static void show_loading_window (EvView *view); +static void hide_loading_window (EvView *view); +static void ev_view_reload_page (EvView *view, + gint page, + cairo_region_t *region); +static void ev_view_loading_window_move (EvView *view); + +/*** Callbacks ***/ +static void ev_view_change_page (EvView *view, + gint new_page); +static void job_finished_cb (EvPixbufCache *pixbuf_cache, + cairo_region_t *region, + EvView *view); +static void ev_view_page_changed_cb (EvDocumentModel *model, + gint old_page, + gint new_page, + EvView *view); +static void on_adjustment_value_changed (GtkAdjustment *adjustment, + EvView *view); + +/*** GObject ***/ +static void ev_view_finalize (GObject *object); +static void ev_view_destroy (GtkObject *object); +static void ev_view_class_init (EvViewClass *class); +static void ev_view_init (EvView *view); + +/*** Zoom and sizing ***/ +static double zoom_for_size_fit_width (gdouble doc_width, + gdouble doc_height, + int target_width, + int target_height); +static double zoom_for_size_fit_height (gdouble doc_width, + gdouble doc_height, + int target_width, + int target_height); +static double zoom_for_size_best_fit (gdouble doc_width, + gdouble doc_height, + int target_width, + int target_height); +static void ev_view_zoom_for_size (EvView *view, + int width, + int height); +static void ev_view_zoom_for_size_continuous_and_dual_page (EvView *view, + int width, + int height); +static void ev_view_zoom_for_size_continuous (EvView *view, + int width, + int height); +static void ev_view_zoom_for_size_dual_page (EvView *view, + int width, + int height); +static void ev_view_zoom_for_size_single_page (EvView *view, + int width, + int height); +/*** Cursors ***/ +static void ev_view_set_cursor (EvView *view, + EvViewCursor new_cursor); +static void ev_view_handle_cursor_over_xy (EvView *view, + gint x, + gint y); + +/*** Find ***/ +static gint ev_view_find_get_n_results (EvView *view, + gint page); +static EvRectangle *ev_view_find_get_result (EvView *view, + gint page, + gint result); +static void jump_to_find_result (EvView *view); +static void jump_to_find_page (EvView *view, + EvViewFindDirection direction, + gint shift); +/*** Selection ***/ +static void compute_selections (EvView *view, + EvSelectionStyle style, + GdkPoint *start, + GdkPoint *stop); +static void clear_selection (EvView *view); +static void clear_link_selected (EvView *view); +static void selection_free (EvViewSelection *selection); +static char* get_selected_text (EvView *ev_view); +static void ev_view_primary_get_cb (GtkClipboard *clipboard, + GtkSelectionData *selection_data, + guint info, + gpointer data); +static void ev_view_primary_clear_cb (GtkClipboard *clipboard, + gpointer data); +static void ev_view_update_primary_selection (EvView *ev_view); + +G_DEFINE_TYPE (EvView, ev_view, GTK_TYPE_LAYOUT) + +/* HeightToPage cache */ +#define EV_HEIGHT_TO_PAGE_CACHE_KEY "ev-height-to-page-cache" + +static void +build_height_to_page (EvHeightToPageCache *cache, + EvDocument *document, + gint rotation) +{ + gboolean swap, uniform, dual_even_left; + int i; + double uniform_height, page_height, next_page_height; + double saved_height; + gdouble u_width, u_height; + gint n_pages; + + swap = (rotation == 90 || rotation == 270); + + uniform = ev_document_is_page_size_uniform (document); + n_pages = ev_document_get_n_pages (document); + dual_even_left = (n_pages > 2); + + g_free (cache->height_to_page); + g_free (cache->dual_height_to_page); + + cache->rotation = rotation; + cache->height_to_page = g_new0 (gdouble, n_pages + 1); + cache->dual_height_to_page = g_new0 (gdouble, n_pages + 2); + + if (uniform) + ev_document_get_page_size (document, 0, &u_width, &u_height); + + saved_height = 0; + for (i = 0; i <= n_pages; i++) { + if (uniform) { + uniform_height = swap ? u_width : u_height; + cache->height_to_page[i] = i * uniform_height; + } else { + if (i < n_pages) { + gdouble w, h; + + ev_document_get_page_size (document, i, &w, &h); + page_height = swap ? w : h; + } else { + page_height = 0; + } + cache->height_to_page[i] = saved_height; + saved_height += page_height; + } + } + + if (dual_even_left && !uniform) { + gdouble w, h; + + ev_document_get_page_size (document, 0, &w, &h); + saved_height = swap ? w : h; + } else { + saved_height = 0; + } + + for (i = dual_even_left; i < n_pages + 2; i += 2) { + if (uniform) { + uniform_height = swap ? u_width : u_height; + cache->dual_height_to_page[i] = ((i + dual_even_left) / 2) * uniform_height; + if (i + 1 < n_pages + 2) + cache->dual_height_to_page[i + 1] = ((i + dual_even_left) / 2) * uniform_height; + } else { + if (i + 1 < n_pages) { + gdouble w, h; + + ev_document_get_page_size (document, i + 1, &w, &h); + next_page_height = swap ? w : h; + } else { + next_page_height = 0; + } + + if (i < n_pages) { + gdouble w, h; + + ev_document_get_page_size (document, i, &w, &h); + page_height = swap ? w : h; + } else { + page_height = 0; + } + + if (i + 1 < n_pages + 2) { + cache->dual_height_to_page[i] = saved_height; + cache->dual_height_to_page[i + 1] = saved_height; + saved_height += MAX(page_height, next_page_height); + } else { + cache->dual_height_to_page[i] = saved_height; + } + } + } +} + +static void +ev_height_to_page_cache_get_height (EvHeightToPageCache *cache, + EvDocument *document, + gint page, + gint rotation, + gdouble *height, + gdouble *dual_height) +{ + if (cache->rotation != rotation) + build_height_to_page (cache, document, rotation); + + *height = cache->height_to_page[page]; + *dual_height = cache->dual_height_to_page[page]; +} + +static void +ev_height_to_page_cache_free (EvHeightToPageCache *cache) +{ + if (cache->height_to_page) { + g_free (cache->height_to_page); + cache->height_to_page = NULL; + } + + if (cache->dual_height_to_page) { + g_free (cache->dual_height_to_page); + cache->dual_height_to_page = NULL; + } + g_free (cache); +} + +static EvHeightToPageCache * +ev_view_get_height_to_page_cache (EvView *view) +{ + EvHeightToPageCache *cache; + + if (!view->document) + return NULL; + + cache = g_object_get_data (G_OBJECT (view->document), EV_HEIGHT_TO_PAGE_CACHE_KEY); + if (!cache) { + cache = g_new0 (EvHeightToPageCache, 1); + build_height_to_page (cache, view->document, view->rotation); + g_object_set_data_full (G_OBJECT (view->document), + EV_HEIGHT_TO_PAGE_CACHE_KEY, + cache, + (GDestroyNotify)ev_height_to_page_cache_free); + } + + return cache; +} + +static void +ev_view_get_height_to_page (EvView *view, + gint page, + gint *height, + gint *dual_height) +{ + gdouble h, dh; + + if (!view->height_to_page_cache) + return; + + ev_height_to_page_cache_get_height (view->height_to_page_cache, + view->document, + page, + view->rotation, + &h, &dh); + if (height) + *height = (gint)(h * view->scale + 0.5); + + if (dual_height) + *dual_height = (gint)(dh * view->scale + 0.5); +} + +static gint +ev_view_get_scrollbar_size (EvView *view, + GtkOrientation orientation) +{ + GtkWidget *widget = GTK_WIDGET (view); + GtkWidget *sb; + GtkWidget *swindow = gtk_widget_get_parent (GTK_WIDGET (view)); + GtkAllocation allocation; + GtkRequisition req; + gint spacing; + + if (!GTK_IS_SCROLLED_WINDOW (swindow)) + return 0; + + gtk_widget_get_allocation (widget, &allocation); + + if (orientation == GTK_ORIENTATION_VERTICAL) { + if (allocation.height >= view->requisition.height) + sb = gtk_scrolled_window_get_vscrollbar (GTK_SCROLLED_WINDOW (swindow)); + else + return 0; + } else { + if (allocation.width >= view->requisition.width) + sb = gtk_scrolled_window_get_hscrollbar (GTK_SCROLLED_WINDOW (swindow)); + else + return 0; + } + + gtk_widget_style_get (swindow, "scrollbar_spacing", &spacing, NULL); + gtk_widget_size_request (sb, &req); + + return (orientation == GTK_ORIENTATION_VERTICAL ? req.width : req.height) + spacing; +} + +static void +scroll_to_point (EvView *view, + gdouble x, + gdouble y, + GtkOrientation orientation) +{ + gdouble page_size; + gdouble upper, lower; + + if (orientation == GTK_ORIENTATION_VERTICAL) { + page_size = gtk_adjustment_get_page_size (view->vadjustment); + upper = gtk_adjustment_get_upper (view->vadjustment); + lower = gtk_adjustment_get_lower (view->vadjustment); + + if (view->continuous) { + gtk_adjustment_clamp_page (view->vadjustment, + y - view->spacing / 2, + y + page_size); + } else { + gtk_adjustment_set_value (view->vadjustment, + CLAMP (y, lower, upper - page_size)); + } + } else { + page_size = gtk_adjustment_get_page_size (view->hadjustment); + upper = gtk_adjustment_get_upper (view->hadjustment); + lower = gtk_adjustment_get_lower (view->hadjustment); + + if (view->dual_page) { + gtk_adjustment_clamp_page (view->hadjustment, x, + x + page_size); + } else { + gtk_adjustment_set_value (view->hadjustment, + CLAMP (x, lower, upper - page_size)); + } + } +} + +static void +ev_view_scroll_to_page_position (EvView *view, GtkOrientation orientation) +{ + gdouble x, y; + + if (!view->document) + return; + + if ((orientation == GTK_ORIENTATION_VERTICAL && view->pending_point.y == 0) || + (orientation == GTK_ORIENTATION_HORIZONTAL && view->pending_point.x == 0)) { + GdkRectangle page_area; + GtkBorder border; + + ev_view_get_page_extents (view, view->current_page, &page_area, &border); + x = page_area.x; + y = page_area.y; + } else { + GdkPoint view_point; + + doc_point_to_view_point (view, view->current_page, + &view->pending_point, &view_point); + x = view_point.x; + y = view_point.y; + } + + scroll_to_point (view, x, y, orientation); +} + +static void +view_set_adjustment_values (EvView *view, + GtkOrientation orientation) +{ + GtkWidget *widget = GTK_WIDGET (view); + GtkAdjustment *adjustment; + GtkAllocation allocation; + int req_size; + int alloc_size; + gdouble page_size; + gdouble value; + gdouble upper; + double factor; + gint new_value; + + gtk_widget_get_allocation (widget, &allocation); + + if (orientation == GTK_ORIENTATION_HORIZONTAL) { + req_size = view->requisition.width; + alloc_size = allocation.width; + adjustment = view->hadjustment; + } else { + req_size = view->requisition.height; + alloc_size = allocation.height; + adjustment = view->vadjustment; + } + + if (!adjustment) + return; + + factor = 1.0; + value = gtk_adjustment_get_value (adjustment); + upper = gtk_adjustment_get_upper (adjustment); + page_size = gtk_adjustment_get_page_size (adjustment); + + switch (view->pending_scroll) { + case SCROLL_TO_KEEP_POSITION: + case SCROLL_TO_FIND_LOCATION: + factor = value / upper; + break; + case SCROLL_TO_PAGE_POSITION: + break; + case SCROLL_TO_CENTER: + factor = (value + page_size * 0.5) / upper; + break; + } + + upper = MAX (alloc_size, req_size); + page_size = alloc_size; + + gtk_adjustment_set_page_size (adjustment, page_size); + gtk_adjustment_set_step_increment (adjustment, alloc_size * 0.1); + gtk_adjustment_set_page_increment (adjustment, alloc_size * 0.9); + gtk_adjustment_set_lower (adjustment, 0); + gtk_adjustment_set_upper (adjustment, upper); + + /* + * We add 0.5 to the values before to average out our rounding errors. + */ + switch (view->pending_scroll) { + case SCROLL_TO_KEEP_POSITION: + case SCROLL_TO_FIND_LOCATION: + new_value = CLAMP (upper * factor + 0.5, 0, upper - page_size); + gtk_adjustment_set_value (adjustment, (int)new_value); + break; + case SCROLL_TO_PAGE_POSITION: + ev_view_scroll_to_page_position (view, orientation); + break; + case SCROLL_TO_CENTER: + new_value = CLAMP (upper * factor - page_size * 0.5 + 0.5, + 0, upper - page_size); + gtk_adjustment_set_value (adjustment, (int)new_value); + break; + } + + gtk_adjustment_changed (adjustment); +} + +static void +view_update_range_and_current_page (EvView *view) +{ + gint start = view->start_page; + gint end = view->end_page; + + if (ev_document_get_n_pages (view->document) <= 0 || + !ev_document_check_dimensions (view->document)) + return; + + if (view->continuous) { + GdkRectangle current_area, unused, page_area; + GtkBorder border; + gboolean found = FALSE; + gint area_max = -1, area; + gint best_current_page = -1; + int i, j = 0; + + if (!(view->vadjustment && view->hadjustment)) + return; + + current_area.x = gtk_adjustment_get_value (view->hadjustment); + current_area.width = gtk_adjustment_get_page_size (view->hadjustment); + current_area.y = gtk_adjustment_get_value (view->vadjustment); + current_area.height = gtk_adjustment_get_page_size (view->vadjustment); + + for (i = 0; i < ev_document_get_n_pages (view->document); i++) { + + ev_view_get_page_extents (view, i, &page_area, &border); + + if (gdk_rectangle_intersect (¤t_area, &page_area, &unused)) { + area = unused.width * unused.height; + + if (!found) { + area_max = area; + view->start_page = i; + found = TRUE; + best_current_page = i; + } + if (area > area_max) { + best_current_page = (area == area_max) ? MIN (i, best_current_page) : i; + area_max = area; + } + + view->end_page = i; + j = 0; + } else if (found && view->current_page <= view->end_page) { + if (view->dual_page && j < 1) { + /* In dual mode we stop searching + * after two consecutive non-visible pages. + */ + j++; + continue; + } + break; + } + } + + if (view->pending_scroll == SCROLL_TO_KEEP_POSITION) { + best_current_page = MAX (best_current_page, view->start_page); + + if (view->current_page != best_current_page) { + view->current_page = best_current_page; + hide_loading_window (view); + ev_document_model_set_page (view->model, best_current_page); + } + } + } else if (view->dual_page) { + if (view->current_page % 2 == get_dual_even_left (view)) { + view->start_page = view->current_page; + if (view->current_page + 1 < ev_document_get_n_pages (view->document)) + view->end_page = view->start_page + 1; + else + view->end_page = view->start_page; + } else { + if (view->current_page < 1) + view->start_page = view->current_page; + else + view->start_page = view->current_page - 1; + view->end_page = view->current_page; + } + } else { + view->start_page = view->current_page; + view->end_page = view->current_page; + } + + if (view->start_page == -1 || view->end_page == -1) + return; + + if (start != view->start_page || end != view->end_page) { + gint i; + + for (i = start; i < view->start_page && start != -1; i++) { + hide_annotation_windows (view, i); + } + + for (i = end; i > view->end_page && end != -1; i--) { + hide_annotation_windows (view, i); + } + } + + ev_page_cache_set_page_range (view->page_cache, + view->start_page, + view->end_page); + ev_pixbuf_cache_set_page_range (view->pixbuf_cache, + view->start_page, + view->end_page, + view->selection_info.selections); + + if (ev_pixbuf_cache_get_surface (view->pixbuf_cache, view->current_page)) + gtk_widget_queue_draw (GTK_WIDGET (view)); +} + +static void +set_scroll_adjustment (EvView *view, + GtkOrientation orientation, + GtkAdjustment *adjustment) +{ + GtkAdjustment **to_set; + + if (orientation == GTK_ORIENTATION_HORIZONTAL) + to_set = &view->hadjustment; + else + to_set = &view->vadjustment; + + if (*to_set != adjustment) { + if (*to_set) { + g_signal_handlers_disconnect_by_func (*to_set, + (gpointer) on_adjustment_value_changed, + view); + g_object_unref (*to_set); + } + + *to_set = adjustment; + view_set_adjustment_values (view, orientation); + + if (*to_set) { + g_object_ref (*to_set); + g_signal_connect (*to_set, "value_changed", + G_CALLBACK (on_adjustment_value_changed), view); + } + } +} + +static void +ev_view_set_scroll_adjustments (GtkLayout *layout, + GtkAdjustment *hadjustment, + GtkAdjustment *vadjustment) +{ + EvView *view = EV_VIEW (layout); + + set_scroll_adjustment (view, GTK_ORIENTATION_HORIZONTAL, hadjustment); + set_scroll_adjustment (view, GTK_ORIENTATION_VERTICAL, vadjustment); + + on_adjustment_value_changed (NULL, view); +} + +static void +add_scroll_binding_keypad (GtkBindingSet *binding_set, + guint keyval, + GdkModifierType modifiers, + GtkScrollType scroll, + gboolean horizontal) +{ + guint keypad_keyval = keyval - GDK_Left + GDK_KP_Left; + + gtk_binding_entry_add_signal (binding_set, keyval, modifiers, + "binding_activated", 2, + GTK_TYPE_SCROLL_TYPE, scroll, + G_TYPE_BOOLEAN, horizontal); + gtk_binding_entry_add_signal (binding_set, keypad_keyval, modifiers, + "binding_activated", 2, + GTK_TYPE_SCROLL_TYPE, scroll, + G_TYPE_BOOLEAN, horizontal); +} + +static gdouble +compute_scroll_increment (EvView *view, + GtkScrollType scroll) +{ + GtkWidget *widget = GTK_WIDGET (view); + GtkAdjustment *adjustment = view->vadjustment; + cairo_region_t *text_region, *region; + GtkAllocation allocation; + gint page; + GdkRectangle rect; + EvRectangle doc_rect; + GdkRectangle page_area; + GtkBorder border; + gdouble fraction = 1.0; + + if (scroll != GTK_SCROLL_PAGE_BACKWARD && scroll != GTK_SCROLL_PAGE_FORWARD) + return gtk_adjustment_get_page_size (adjustment); + + page = scroll == GTK_SCROLL_PAGE_BACKWARD ? view->start_page : view->end_page; + + text_region = ev_page_cache_get_text_mapping (view->page_cache, page); + if (!text_region || cairo_region_is_empty (text_region)) + return gtk_adjustment_get_page_size (adjustment); + + gtk_widget_get_allocation (widget, &allocation); + ev_view_get_page_extents (view, page, &page_area, &border); + rect.x = page_area.x + view->scroll_x; + rect.y = view->scroll_y + (scroll == GTK_SCROLL_PAGE_BACKWARD ? 5 : allocation.height - 5); + rect.width = page_area.width; + rect.height = 1; + view_rect_to_doc_rect (view, &rect, &page_area, &doc_rect); + + /* Convert the doc rectangle into a GdkRectangle */ + rect.x = doc_rect.x1; + rect.y = doc_rect.y1; + rect.width = doc_rect.x2 - doc_rect.x1; + rect.height = MAX (1, doc_rect.y2 - doc_rect.y1); + region = cairo_region_create_rectangle (&rect); + + cairo_region_intersect (region, text_region); + if (cairo_region_num_rectangles (region)) { + EvRenderContext *rc; + EvPage *ev_page; + cairo_region_t *sel_region; + + cairo_region_get_rectangle (region, 0, &rect); + ev_page = ev_document_get_page (view->document, page); + rc = ev_render_context_new (ev_page, view->rotation, view->scale); + g_object_unref (ev_page); + /* Get the selection region to know the height of the line */ + doc_rect.x1 = doc_rect.x2 = rect.x + 0.5; + doc_rect.y1 = doc_rect.y2 = rect.y + 0.5; + + ev_document_doc_mutex_lock (); + sel_region = ev_selection_get_selection_region (EV_SELECTION (view->document), + rc, EV_SELECTION_STYLE_LINE, + &doc_rect); + ev_document_doc_mutex_unlock (); + + g_object_unref (rc); + + if (cairo_region_num_rectangles (sel_region) > 0) { + cairo_region_get_rectangle (sel_region, 0, &rect); + fraction = 1 - (rect.height / gtk_adjustment_get_page_size (adjustment)); + } + cairo_region_destroy (sel_region); + } + cairo_region_destroy (region); + + return gtk_adjustment_get_page_size (adjustment) * fraction; + +} + +void +ev_view_scroll (EvView *view, + GtkScrollType scroll, + gboolean horizontal) +{ + GtkAdjustment *adjustment; + double value, increment; + gdouble upper, lower; + gdouble page_size; + gdouble step_increment; + gboolean first_page = FALSE; + gboolean last_page = FALSE; + + view->jump_to_find_result = FALSE; + + if (view->sizing_mode == EV_SIZING_BEST_FIT) { + switch (scroll) { + case GTK_SCROLL_PAGE_BACKWARD: + case GTK_SCROLL_STEP_BACKWARD: + ev_view_previous_page (view); + break; + case GTK_SCROLL_PAGE_FORWARD: + case GTK_SCROLL_STEP_FORWARD: + ev_view_next_page (view); + break; + default: + break; + } + return; + } + + /* Assign values for increment and vertical adjustment */ + adjustment = horizontal ? view->hadjustment : view->vadjustment; + value = gtk_adjustment_get_value (adjustment); + upper = gtk_adjustment_get_upper (adjustment); + lower = gtk_adjustment_get_lower (adjustment); + page_size = gtk_adjustment_get_page_size (adjustment); + step_increment = gtk_adjustment_get_step_increment (adjustment); + + /* Assign boolean for first and last page */ + if (view->current_page == 0) + first_page = TRUE; + if (view->current_page == ev_document_get_n_pages (view->document) - 1) + last_page = TRUE; + + switch (scroll) { + case GTK_SCROLL_PAGE_BACKWARD: + /* Do not jump backwards if at the first page */ + if (value == lower && first_page) { + /* Do nothing */ + /* At the top of a page, assign the upper bound limit of previous page */ + } else if (value == lower) { + value = upper - page_size; + ev_view_previous_page (view); + /* Jump to the top */ + } else { + increment = compute_scroll_increment (view, GTK_SCROLL_PAGE_BACKWARD); + value = MAX (value - increment, lower); + } + break; + case GTK_SCROLL_PAGE_FORWARD: + /* Do not jump forward if at the last page */ + if (value == (upper - page_size) && last_page) { + /* Do nothing */ + /* At the bottom of a page, assign the lower bound limit of next page */ + } else if (value == (upper - page_size)) { + value = 0; + ev_view_next_page (view); + /* Jump to the bottom */ + } else { + increment = compute_scroll_increment (view, GTK_SCROLL_PAGE_FORWARD); + value = MIN (value + increment, upper - page_size); + } + break; + case GTK_SCROLL_STEP_BACKWARD: + value -= step_increment; + break; + case GTK_SCROLL_STEP_FORWARD: + value += step_increment; + break; + case GTK_SCROLL_STEP_DOWN: + value -= step_increment / 10; + break; + case GTK_SCROLL_STEP_UP: + value += step_increment / 10; + break; + default: + break; + } + + value = CLAMP (value, lower, upper - page_size); + + gtk_adjustment_set_value (adjustment, value); +} + +#define MARGIN 5 + +static void +ensure_rectangle_is_visible (EvView *view, GdkRectangle *rect) +{ + GtkWidget *widget = GTK_WIDGET (view); + GtkAdjustment *adjustment; + GtkAllocation allocation; + gdouble adj_value; + int value; + + view->pending_scroll = SCROLL_TO_FIND_LOCATION; + + gtk_widget_get_allocation (widget, &allocation); + + adjustment = view->vadjustment; + adj_value = gtk_adjustment_get_value (adjustment); + + if (rect->y < adj_value) { + value = MAX (gtk_adjustment_get_lower (adjustment), rect->y - MARGIN); + gtk_adjustment_set_value (view->vadjustment, value); + } else if (rect->y + rect->height > adj_value + allocation.height) { + value = MIN (gtk_adjustment_get_upper (adjustment), rect->y + rect->height - + allocation.height + MARGIN); + gtk_adjustment_set_value (view->vadjustment, value); + } + + adjustment = view->hadjustment; + adj_value = gtk_adjustment_get_value (adjustment); + + if (rect->x < adj_value) { + value = MAX (gtk_adjustment_get_lower (adjustment), rect->x - MARGIN); + gtk_adjustment_set_value (view->hadjustment, value); + } else if (rect->x + rect->height > adj_value + allocation.width) { + value = MIN (gtk_adjustment_get_upper (adjustment), rect->x + rect->width - + allocation.width + MARGIN); + gtk_adjustment_set_value (view->hadjustment, value); + } + + gtk_widget_queue_resize (GTK_WIDGET (view)); +} + +/*** Geometry computations ***/ + +static void +compute_border (EvView *view, int width, int height, GtkBorder *border) +{ + ev_document_misc_get_page_border_size (width, height, border); +} + +void +_get_page_size_for_scale_and_rotation (EvDocument *document, + gint page, + gdouble scale, + gint rotation, + gint *page_width, + gint *page_height) +{ + gdouble w, h; + gint width, height; + + ev_document_get_page_size (document, page, &w, &h); + + width = (gint)(w * scale + 0.5); + height = (gint)(h * scale + 0.5); + + if (page_width) + *page_width = (rotation == 0 || rotation == 180) ? width : height; + if (page_height) + *page_height = (rotation == 0 || rotation == 180) ? height : width; +} + +static void +ev_view_get_page_size (EvView *view, + gint page, + gint *page_width, + gint *page_height) +{ + _get_page_size_for_scale_and_rotation (view->document, + page, + view->scale, + view->rotation, + page_width, + page_height); +} + +static void +ev_view_get_max_page_size (EvView *view, + gint *max_width, + gint *max_height) +{ + double w, h; + gint width, height; + + ev_document_get_max_page_size (view->document, &w, &h); + + width = (gint)(w * view->scale + 0.5); + height = (gint)(h * view->scale + 0.5); + + if (max_width) + *max_width = (view->rotation == 0 || view->rotation == 180) ? width : height; + if (max_height) + *max_height = (view->rotation == 0 || view->rotation == 180) ? height : width; +} + +static void +get_page_y_offset (EvView *view, int page, int *y_offset) +{ + int max_width, offset; + GtkBorder border; + + g_return_if_fail (y_offset != NULL); + + ev_view_get_max_page_size (view, &max_width, NULL); + compute_border (view, max_width, max_width, &border); + + if (view->dual_page) { + ev_view_get_height_to_page (view, page, NULL, &offset); + offset += ((page + get_dual_even_left (view)) / 2 + 1) * view->spacing + + ((page + get_dual_even_left (view)) / 2 ) * (border.top + border.bottom); + } else { + ev_view_get_height_to_page (view, page, &offset, NULL); + offset += (page + 1) * view->spacing + page * (border.top + border.bottom); + } + + *y_offset = offset; + return; +} + +gboolean +ev_view_get_page_extents (EvView *view, + gint page, + GdkRectangle *page_area, + GtkBorder *border) +{ + GtkWidget *widget; + int width, height; + GtkAllocation allocation; + + widget = GTK_WIDGET (view); + gtk_widget_get_allocation (widget, &allocation); + + /* Get the size of the page */ + ev_view_get_page_size (view, page, &width, &height); + compute_border (view, width, height, border); + page_area->width = width + border->left + border->right; + page_area->height = height + border->top + border->bottom; + + if (view->continuous) { + gint max_width; + gint x, y; + + ev_view_get_max_page_size (view, &max_width, NULL); + max_width = max_width + border->left + border->right; + /* Get the location of the bounding box */ + if (view->dual_page) { + x = view->spacing + ((page % 2 == get_dual_even_left (view)) ? 0 : 1) * (max_width + view->spacing); + x = x + MAX (0, allocation.width - (max_width * 2 + view->spacing * 3)) / 2; + if (page % 2 == get_dual_even_left (view)) + x = x + (max_width - width - border->left - border->right); + } else { + x = view->spacing; + x = x + MAX (0, allocation.width - (width + view->spacing * 2)) / 2; + } + + get_page_y_offset (view, page, &y); + + page_area->x = x; + page_area->y = y; + } else { + gint x, y; + if (view->dual_page) { + gint width_2, height_2; + gint max_width = width; + gint max_height = height; + GtkBorder overall_border; + gint other_page; + + other_page = (page % 2 == get_dual_even_left (view)) ? page + 1: page - 1; + + /* First, we get the bounding box of the two pages */ + if (other_page < ev_document_get_n_pages (view->document) + && (0 <= other_page)) { + ev_view_get_page_size (view, other_page, + &width_2, &height_2); + if (width_2 > width) + max_width = width_2; + if (height_2 > height) + max_height = height_2; + } + compute_border (view, max_width, max_height, &overall_border); + + /* Find the offsets */ + x = view->spacing; + y = view->spacing; + + /* Adjust for being the left or right page */ + if (page % 2 == get_dual_even_left (view)) + x = x + max_width - width; + else + x = x + (max_width + overall_border.left + overall_border.right) + view->spacing; + + y = y + (max_height - height)/2; + + /* Adjust for extra allocation */ + x = x + MAX (0, allocation.width - + ((max_width + overall_border.left + overall_border.right) * 2 + view->spacing * 3))/2; + y = y + MAX (0, allocation.height - (height + view->spacing * 2))/2; + } else { + x = view->spacing; + y = view->spacing; + + /* Adjust for extra allocation */ + x = x + MAX (0, allocation.width - (width + border->left + border->right + view->spacing * 2))/2; + y = y + MAX (0, allocation.height - (height + border->top + border->bottom + view->spacing * 2))/2; + } + + page_area->x = x; + page_area->y = y; + } + + return TRUE; +} + +static void +get_doc_page_size (EvView *view, + gint page, + gdouble *width, + gdouble *height) +{ + double w, h; + + ev_document_get_page_size (view->document, page, &w, &h); + if (view->rotation == 0 || view->rotation == 180) { + if (width) *width = w; + if (height) *height = h; + } else { + if (width) *width = h; + if (height) *height = w; + } +} + +static void +view_point_to_doc_point (EvView *view, + GdkPoint *view_point, + GdkRectangle *page_area, + double *doc_point_x, + double *doc_point_y) +{ + *doc_point_x = (double) (view_point->x - page_area->x) / view->scale; + *doc_point_y = (double) (view_point->y - page_area->y) / view->scale; +} + +static void +view_rect_to_doc_rect (EvView *view, + GdkRectangle *view_rect, + GdkRectangle *page_area, + EvRectangle *doc_rect) +{ + doc_rect->x1 = (double) (view_rect->x - page_area->x) / view->scale; + doc_rect->y1 = (double) (view_rect->y - page_area->y) / view->scale; + doc_rect->x2 = doc_rect->x1 + (double) view_rect->width / view->scale; + doc_rect->y2 = doc_rect->y1 + (double) view_rect->height / view->scale; +} + +static void +doc_point_to_view_point (EvView *view, + int page, + EvPoint *doc_point, + GdkPoint *view_point) +{ + GdkRectangle page_area; + GtkBorder border; + double x, y, view_x, view_y; + gdouble width, height; + + get_doc_page_size (view, page, &width, &height); + + if (view->rotation == 0) { + x = doc_point->x; + y = doc_point->y; + } else if (view->rotation == 90) { + x = width - doc_point->y; + y = doc_point->x; + } else if (view->rotation == 180) { + x = width - doc_point->x; + y = height - doc_point->y; + } else if (view->rotation == 270) { + x = doc_point->y; + y = height - doc_point->x; + } else { + g_assert_not_reached (); + } + + ev_view_get_page_extents (view, page, &page_area, &border); + + view_x = CLAMP (x * view->scale, 0, page_area.width); + view_y = CLAMP (y * view->scale, 0, page_area.height); + view_point->x = view_x + page_area.x; + view_point->y = view_y + page_area.y; +} + +static void +doc_rect_to_view_rect (EvView *view, + int page, + EvRectangle *doc_rect, + GdkRectangle *view_rect) +{ + GdkRectangle page_area; + GtkBorder border; + double x, y, w, h; + gdouble width, height; + + get_doc_page_size (view, page, &width, &height); + + if (view->rotation == 0) { + x = doc_rect->x1; + y = doc_rect->y1; + w = doc_rect->x2 - doc_rect->x1; + h = doc_rect->y2 - doc_rect->y1; + } else if (view->rotation == 90) { + x = width - doc_rect->y2; + y = doc_rect->x1; + w = doc_rect->y2 - doc_rect->y1; + h = doc_rect->x2 - doc_rect->x1; + } else if (view->rotation == 180) { + x = width - doc_rect->x2; + y = height - doc_rect->y2; + w = doc_rect->x2 - doc_rect->x1; + h = doc_rect->y2 - doc_rect->y1; + } else if (view->rotation == 270) { + x = doc_rect->y1; + y = height - doc_rect->x2; + w = doc_rect->y2 - doc_rect->y1; + h = doc_rect->x2 - doc_rect->x1; + } else { + g_assert_not_reached (); + } + + ev_view_get_page_extents (view, page, &page_area, &border); + + view_rect->x = x * view->scale + page_area.x; + view_rect->y = y * view->scale + page_area.y; + view_rect->width = w * view->scale; + view_rect->height = h * view->scale; +} + +static gboolean +get_dual_even_left (EvView *view) +{ + return (ev_document_get_n_pages (view->document) > 2); +} + +static void +find_page_at_location (EvView *view, + gdouble x, + gdouble y, + gint *page, + gint *x_offset, + gint *y_offset) +{ + int i; + + if (view->document == NULL) + return; + + g_assert (page); + g_assert (x_offset); + g_assert (y_offset); + + for (i = view->start_page; i >= 0 && i <= view->end_page; i++) { + GdkRectangle page_area; + GtkBorder border; + + if (! ev_view_get_page_extents (view, i, &page_area, &border)) + continue; + + if ((x >= page_area.x + border.left) && + (x < page_area.x + page_area.width - border.right) && + (y >= page_area.y + border.top) && + (y < page_area.y + page_area.height - border.bottom)) { + *page = i; + *x_offset = x - (page_area.x + border.left); + *y_offset = y - (page_area.y + border.top); + return; + } + } + + *page = -1; +} + +static gboolean +location_in_text (EvView *view, + gdouble x, + gdouble y) +{ + cairo_region_t *region; + gint page = -1; + gint x_offset = 0, y_offset = 0; + + find_page_at_location (view, x, y, &page, &x_offset, &y_offset); + + if (page == -1) + return FALSE; + + region = ev_page_cache_get_text_mapping (view->page_cache, page); + + if (region) + return cairo_region_contains_point (region, x_offset / view->scale, y_offset / view->scale); + else + return FALSE; +} + +static gboolean +location_in_selected_text (EvView *view, + gdouble x, + gdouble y) +{ + gint page = -1; + gint x_offset = 0, y_offset = 0; + EvViewSelection *selection; + GList *l = NULL; + + for (l = view->selection_info.selections; l != NULL; l = l->next) { + selection = (EvViewSelection *)l->data; + + find_page_at_location (view, x, y, &page, &x_offset, &y_offset); + + if (page != selection->page) + continue; + + if (selection->covered_region && + cairo_region_contains_point (selection->covered_region, x_offset, y_offset)) + return TRUE; + } + + return FALSE; +} + +static gboolean +get_doc_point_from_offset (EvView *view, + gint page, + gint x_offset, + gint y_offset, + gint *x_new, + gint *y_new) +{ + gdouble width, height; + double x, y; + + get_doc_page_size (view, page, &width, &height); + + x_offset = x_offset / view->scale; + y_offset = y_offset / view->scale; + + if (view->rotation == 0) { + x = x_offset; + y = y_offset; + } else if (view->rotation == 90) { + x = y_offset; + y = width - x_offset; + } else if (view->rotation == 180) { + x = width - x_offset; + y = height - y_offset; + } else if (view->rotation == 270) { + x = height - y_offset; + y = x_offset; + } else { + g_assert_not_reached (); + } + + *x_new = x; + *y_new = y; + + return TRUE; +} + +static gboolean +get_doc_point_from_location (EvView *view, + gdouble x, + gdouble y, + gint *page, + gint *x_new, + gint *y_new) +{ + gint x_offset = 0, y_offset = 0; + + x += view->scroll_x; + y += view->scroll_y; + find_page_at_location (view, x, y, page, &x_offset, &y_offset); + if (*page == -1) + return FALSE; + + return get_doc_point_from_offset (view, *page, x_offset, y_offset, x_new, y_new); +} + +static void +ev_view_get_area_from_mapping (EvView *view, + guint page, + EvMappingList *mapping_list, + gconstpointer data, + GdkRectangle *area) +{ + EvMapping *mapping; + + mapping = ev_mapping_list_find (mapping_list, data); + doc_rect_to_view_rect (view, page, &mapping->area, area); + area->x -= view->scroll_x; + area->y -= view->scroll_y; +} + + +/*** Hyperref ***/ +static EvLink * +ev_view_get_link_at_location (EvView *view, + gdouble x, + gdouble y) +{ + gint page = -1; + gint x_new = 0, y_new = 0; + EvMappingList *link_mapping; + + if (!EV_IS_DOCUMENT_LINKS (view->document)) + return NULL; + + if (!get_doc_point_from_location (view, x, y, &page, &x_new, &y_new)) + return NULL; + + link_mapping = ev_page_cache_get_link_mapping (view->page_cache, page); + + if (link_mapping) + return ev_mapping_list_get_data (link_mapping, x_new, y_new); + else + return NULL; +} + +static void +goto_fitr_dest (EvView *view, EvLinkDest *dest) +{ + EvPoint doc_point; + gdouble zoom, left, top; + gboolean change_left, change_top; + GtkAllocation allocation; + + gtk_widget_get_allocation (GTK_WIDGET (view), &allocation); + + left = ev_link_dest_get_left (dest, &change_left); + top = ev_link_dest_get_top (dest, &change_top); + + zoom = zoom_for_size_best_fit (ev_link_dest_get_right (dest) - left, + ev_link_dest_get_bottom (dest) - top, + allocation.width, + allocation.height); + + ev_document_model_set_sizing_mode (view->model, EV_SIZING_FREE); + ev_document_model_set_scale (view->model, zoom); + + doc_point.x = change_left ? left : 0; + doc_point.y = change_top ? top : 0; + view->pending_point = doc_point; + + ev_view_change_page (view, ev_link_dest_get_page (dest)); +} + +static void +goto_fitv_dest (EvView *view, EvLinkDest *dest) +{ + EvPoint doc_point; + gdouble doc_width, doc_height; + gint page; + double zoom, left; + gboolean change_left; + GtkAllocation allocation; + + gtk_widget_get_allocation (GTK_WIDGET (view), &allocation); + + page = ev_link_dest_get_page (dest); + ev_document_get_page_size (view->document, page, &doc_width, &doc_height); + + left = ev_link_dest_get_left (dest, &change_left); + doc_point.x = change_left ? left : 0; + doc_point.y = 0; + + zoom = zoom_for_size_fit_height (doc_width - doc_point.x , doc_height, + allocation.width, + allocation.height); + + ev_document_model_set_sizing_mode (view->model, EV_SIZING_FREE); + ev_document_model_set_scale (view->model, zoom); + + view->pending_point = doc_point; + + ev_view_change_page (view, page); +} + +static void +goto_fith_dest (EvView *view, EvLinkDest *dest) +{ + EvPoint doc_point; + gdouble doc_width, doc_height; + gint page; + gdouble zoom, top; + gboolean change_top; + GtkAllocation allocation; + + gtk_widget_get_allocation (GTK_WIDGET (view), &allocation); + + page = ev_link_dest_get_page (dest); + ev_document_get_page_size (view->document, page, &doc_width, &doc_height); + + top = ev_link_dest_get_top (dest, &change_top); + + doc_point.x = 0; + doc_point.y = change_top ? top : 0; + + zoom = zoom_for_size_fit_width (doc_width, top, + allocation.width, + allocation.height); + + ev_document_model_set_sizing_mode (view->model, EV_SIZING_FIT_WIDTH); + ev_document_model_set_scale (view->model, zoom); + + view->pending_point = doc_point; + + ev_view_change_page (view, page); +} + +static void +goto_fit_dest (EvView *view, EvLinkDest *dest) +{ + double zoom; + gdouble doc_width, doc_height; + int page; + GtkAllocation allocation; + + gtk_widget_get_allocation (GTK_WIDGET (view), &allocation); + + page = ev_link_dest_get_page (dest); + ev_document_get_page_size (view->document, page, &doc_width, &doc_height); + + zoom = zoom_for_size_best_fit (doc_width, doc_height, + allocation.width, + allocation.height); + + ev_document_model_set_sizing_mode (view->model, EV_SIZING_BEST_FIT); + ev_document_model_set_scale (view->model, zoom); + + ev_view_change_page (view, page); +} + +static void +goto_xyz_dest (EvView *view, EvLinkDest *dest) +{ + EvPoint doc_point; + gint page; + gdouble zoom, left, top; + gboolean change_zoom, change_left, change_top; + + zoom = ev_link_dest_get_zoom (dest, &change_zoom); + page = ev_link_dest_get_page (dest); + + if (change_zoom && zoom > 1) { + ev_document_model_set_sizing_mode (view->model, EV_SIZING_FREE); + ev_document_model_set_scale (view->model, zoom); + } + + left = ev_link_dest_get_left (dest, &change_left); + top = ev_link_dest_get_top (dest, &change_top); + + doc_point.x = change_left ? left : 0; + doc_point.y = change_top ? top : 0; + view->pending_point = doc_point; + + ev_view_change_page (view, page); +} + +static void +goto_dest (EvView *view, EvLinkDest *dest) +{ + EvLinkDestType type; + int page, n_pages, current_page; + + page = ev_link_dest_get_page (dest); + n_pages = ev_document_get_n_pages (view->document); + + if (page < 0 || page >= n_pages) + return; + + current_page = view->current_page; + + type = ev_link_dest_get_dest_type (dest); + + switch (type) { + case EV_LINK_DEST_TYPE_PAGE: + ev_document_model_set_page (view->model, page); + break; + case EV_LINK_DEST_TYPE_FIT: + goto_fit_dest (view, dest); + break; + case EV_LINK_DEST_TYPE_FITH: + goto_fith_dest (view, dest); + break; + case EV_LINK_DEST_TYPE_FITV: + goto_fitv_dest (view, dest); + break; + case EV_LINK_DEST_TYPE_FITR: + goto_fitr_dest (view, dest); + break; + case EV_LINK_DEST_TYPE_XYZ: + goto_xyz_dest (view, dest); + break; + case EV_LINK_DEST_TYPE_PAGE_LABEL: + ev_document_model_set_page_by_label (view->model, ev_link_dest_get_page_label (dest)); + break; + default: + g_assert_not_reached (); + } + + if (current_page != view->current_page) + ev_document_model_set_page (view->model, view->current_page); +} + +static void +ev_view_goto_dest (EvView *view, EvLinkDest *dest) +{ + EvLinkDestType type; + + type = ev_link_dest_get_dest_type (dest); + + if (type == EV_LINK_DEST_TYPE_NAMED) { + EvLinkDest *dest2; + const gchar *named_dest; + + named_dest = ev_link_dest_get_named_dest (dest); + dest2 = ev_document_links_find_link_dest (EV_DOCUMENT_LINKS (view->document), + named_dest); + if (dest2) { + goto_dest (view, dest2); + g_object_unref (dest2); + } + + return; + } + + goto_dest (view, dest); +} + +void +ev_view_handle_link (EvView *view, EvLink *link) +{ + EvLinkAction *action = NULL; + EvLinkActionType type; + + action = ev_link_get_action (link); + if (!action) + return; + + type = ev_link_action_get_action_type (action); + + switch (type) { + case EV_LINK_ACTION_TYPE_GOTO_DEST: { + EvLinkDest *dest; + + g_signal_emit (view, signals[SIGNAL_HANDLE_LINK], 0, link); + + dest = ev_link_action_get_dest (action); + ev_view_goto_dest (view, dest); + } + break; + case EV_LINK_ACTION_TYPE_GOTO_REMOTE: + case EV_LINK_ACTION_TYPE_EXTERNAL_URI: + case EV_LINK_ACTION_TYPE_LAUNCH: + case EV_LINK_ACTION_TYPE_NAMED: + g_signal_emit (view, signals[SIGNAL_EXTERNAL_LINK], 0, action); + break; + } +} + +static char * +tip_from_action_named (EvLinkAction *action) +{ + const gchar *name = ev_link_action_get_name (action); + + if (g_ascii_strcasecmp (name, "FirstPage") == 0) { + return g_strdup (_("Go to first page")); + } else if (g_ascii_strcasecmp (name, "PrevPage") == 0) { + return g_strdup (_("Go to previous page")); + } else if (g_ascii_strcasecmp (name, "NextPage") == 0) { + return g_strdup (_("Go to next page")); + } else if (g_ascii_strcasecmp (name, "LastPage") == 0) { + return g_strdup (_("Go to last page")); + } else if (g_ascii_strcasecmp (name, "GoToPage") == 0) { + return g_strdup (_("Go to page")); + } else if (g_ascii_strcasecmp (name, "Find") == 0) { + return g_strdup (_("Find")); + } + + return NULL; +} + +static char * +tip_from_link (EvView *view, EvLink *link) +{ + EvLinkAction *action; + EvLinkActionType type; + char *msg = NULL; + char *page_label; + const char *title; + + action = ev_link_get_action (link); + title = ev_link_get_title (link); + + if (!action) + return title ? g_strdup (title) : NULL; + + type = ev_link_action_get_action_type (action); + + switch (type) { + case EV_LINK_ACTION_TYPE_GOTO_DEST: + page_label = ev_document_links_get_dest_page_label (EV_DOCUMENT_LINKS (view->document), + ev_link_action_get_dest (action)); + if (page_label) { + msg = g_strdup_printf (_("Go to page %s"), page_label); + g_free (page_label); + } + break; + case EV_LINK_ACTION_TYPE_GOTO_REMOTE: + if (title) { + msg = g_strdup_printf (_("Go to %s on file ā%sā"), title, + ev_link_action_get_filename (action)); + } else { + msg = g_strdup_printf (_("Go to file ā%sā"), + ev_link_action_get_filename (action)); + } + break; + case EV_LINK_ACTION_TYPE_EXTERNAL_URI: + msg = g_strdup (ev_link_action_get_uri (action)); + break; + case EV_LINK_ACTION_TYPE_LAUNCH: + msg = g_strdup_printf (_("Launch %s"), + ev_link_action_get_filename (action)); + break; + case EV_LINK_ACTION_TYPE_NAMED: + msg = tip_from_action_named (action); + break; + default: + if (title) + msg = g_strdup (title); + break; + } + + return msg; +} + +static void +ev_view_handle_cursor_over_xy (EvView *view, gint x, gint y) +{ + EvLink *link; + EvFormField *field; + EvAnnotation *annot = NULL; + + if (view->cursor == EV_VIEW_CURSOR_HIDDEN) + return; + + if (view->adding_annot) { + if (view->cursor != EV_VIEW_CURSOR_ADD) + ev_view_set_cursor (view, EV_VIEW_CURSOR_ADD); + return; + } + + if (view->drag_info.in_drag) { + if (view->cursor != EV_VIEW_CURSOR_DRAG) + ev_view_set_cursor (view, EV_VIEW_CURSOR_DRAG); + return; + } + + if (view->scroll_info.autoscrolling) { + if (view->cursor != EV_VIEW_CURSOR_AUTOSCROLL) + ev_view_set_cursor (view, EV_VIEW_CURSOR_AUTOSCROLL); + return; + } + + link = ev_view_get_link_at_location (view, x, y); + if (link) { + ev_view_set_cursor (view, EV_VIEW_CURSOR_LINK); + } else if ((field = ev_view_get_form_field_at_location (view, x, y))) { + if (field->is_read_only) { + if (view->cursor == EV_VIEW_CURSOR_LINK || + view->cursor == EV_VIEW_CURSOR_IBEAM || + view->cursor == EV_VIEW_CURSOR_DRAG) + ev_view_set_cursor (view, EV_VIEW_CURSOR_NORMAL); + } else if (EV_IS_FORM_FIELD_TEXT (field)) { + ev_view_set_cursor (view, EV_VIEW_CURSOR_IBEAM); + } else { + ev_view_set_cursor (view, EV_VIEW_CURSOR_LINK); + } + } else if ((annot = ev_view_get_annotation_at_location (view, x, y))) { + ev_view_set_cursor (view, EV_VIEW_CURSOR_LINK); + } else if (location_in_text (view, x + view->scroll_x, y + view->scroll_y)) { + ev_view_set_cursor (view, EV_VIEW_CURSOR_IBEAM); + } else { + if (view->cursor == EV_VIEW_CURSOR_LINK || + view->cursor == EV_VIEW_CURSOR_IBEAM || + view->cursor == EV_VIEW_CURSOR_DRAG || + view->cursor == EV_VIEW_CURSOR_AUTOSCROLL || + view->cursor == EV_VIEW_CURSOR_ADD) + ev_view_set_cursor (view, EV_VIEW_CURSOR_NORMAL); + } + + if (link || annot) + g_object_set (view, "has-tooltip", TRUE, NULL); +} + +/*** Images ***/ +static EvImage * +ev_view_get_image_at_location (EvView *view, + gdouble x, + gdouble y) +{ + gint page = -1; + gint x_new = 0, y_new = 0; + EvMappingList *image_mapping; + + if (!EV_IS_DOCUMENT_IMAGES (view->document)) + return NULL; + + if (!get_doc_point_from_location (view, x, y, &page, &x_new, &y_new)) + return NULL; + + image_mapping = ev_page_cache_get_image_mapping (view->page_cache, page); + + if (image_mapping) + return ev_mapping_list_get_data (image_mapping, x_new, y_new); + else + return NULL; +} + +/*** Forms ***/ +static EvFormField * +ev_view_get_form_field_at_location (EvView *view, + gdouble x, + gdouble y) +{ + gint page = -1; + gint x_new = 0, y_new = 0; + EvMappingList *forms_mapping; + + if (!EV_IS_DOCUMENT_FORMS (view->document)) + return NULL; + + if (!get_doc_point_from_location (view, x, y, &page, &x_new, &y_new)) + return NULL; + + forms_mapping = ev_page_cache_get_form_field_mapping (view->page_cache, page); + + if (forms_mapping) + return ev_mapping_list_get_data (forms_mapping, x_new, y_new); + else + return NULL; +} + +static cairo_region_t * +ev_view_form_field_get_region (EvView *view, + EvFormField *field) +{ + GdkRectangle view_area; + EvMappingList *forms_mapping; + + forms_mapping = ev_page_cache_get_form_field_mapping (view->page_cache, + field->page->index); + ev_view_get_area_from_mapping (view, field->page->index, + forms_mapping, + field, &view_area); + + return cairo_region_create_rectangle (&view_area); +} + +static gboolean +ev_view_forms_remove_widgets (EvView *view) +{ + ev_view_remove_all (view); + + return FALSE; +} + +static void +ev_view_form_field_destroy (GtkWidget *widget, + EvView *view) +{ + g_idle_add ((GSourceFunc)ev_view_forms_remove_widgets, view); +} + +static GtkWidget * +ev_view_form_field_button_create_widget (EvView *view, + EvFormField *field) +{ + EvFormFieldButton *field_button = EV_FORM_FIELD_BUTTON (field); + cairo_region_t *field_region = NULL; + + switch (field_button->type) { + case EV_FORM_FIELD_BUTTON_PUSH: + return NULL; + case EV_FORM_FIELD_BUTTON_CHECK: + case EV_FORM_FIELD_BUTTON_RADIO: { + gboolean state; + EvMappingList *forms_mapping; + GList *l; + + state = ev_document_forms_form_field_button_get_state (EV_DOCUMENT_FORMS (view->document), + field); + + /* FIXME: it actually depends on NoToggleToOff flags */ + if (field_button->type == EV_FORM_FIELD_BUTTON_RADIO && + state && field_button->state) + return NULL; + + field_region = ev_view_form_field_get_region (view, field); + + /* For radio buttons and checkbox buttons that are in a set + * we need to update also the region for the current selected item + */ + forms_mapping = ev_page_cache_get_form_field_mapping (view->page_cache, + field->page->index); + for (l = ev_mapping_list_get_list (forms_mapping); l; l = g_list_next (l)) { + EvFormField *button = ((EvMapping *)(l->data))->data; + cairo_region_t *button_region; + + if (button->id == field->id) + continue; + + /* FIXME: only buttons in the same group should be updated */ + if (!EV_IS_FORM_FIELD_BUTTON (button) || + EV_FORM_FIELD_BUTTON (button)->type != field_button->type || + EV_FORM_FIELD_BUTTON (button)->state != TRUE) + continue; + + button_region = ev_view_form_field_get_region (view, button); + cairo_region_union (field_region, button_region); + cairo_region_destroy (button_region); + } + + ev_document_forms_form_field_button_set_state (EV_DOCUMENT_FORMS (view->document), + field, !state); + field_button->state = !state; + } + break; + } + + ev_view_reload_page (view, field->page->index, field_region); + cairo_region_destroy (field_region); + + return NULL; +} + +static void +ev_view_form_field_text_save (EvView *view, + GtkWidget *widget) +{ + EvFormField *field; + + if (!view->document) + return; + + field = g_object_get_data (G_OBJECT (widget), "form-field"); + + if (field->changed) { + EvFormFieldText *field_text = EV_FORM_FIELD_TEXT (field); + cairo_region_t *field_region; + + field_region = ev_view_form_field_get_region (view, field); + + ev_document_forms_form_field_text_set_text (EV_DOCUMENT_FORMS (view->document), + field, field_text->text); + field->changed = FALSE; + ev_view_reload_page (view, field->page->index, field_region); + cairo_region_destroy (field_region); + } +} + +static void +ev_view_form_field_text_changed (GtkWidget *widget, + EvFormField *field) +{ + EvFormFieldText *field_text = EV_FORM_FIELD_TEXT (field); + gchar *text = NULL; + + if (GTK_IS_ENTRY (widget)) { + text = g_strdup (gtk_entry_get_text (GTK_ENTRY (widget))); + } else if (GTK_IS_TEXT_BUFFER (widget)) { + GtkTextIter start, end; + + gtk_text_buffer_get_bounds (GTK_TEXT_BUFFER (widget), &start, &end); + text = gtk_text_buffer_get_text (GTK_TEXT_BUFFER (widget), + &start, &end, FALSE); + } + + if (!field_text->text || + (field_text->text && g_ascii_strcasecmp (field_text->text, text) != 0)) { + g_free (field_text->text); + field_text->text = text; + field->changed = TRUE; + } +} + +static GtkWidget * +ev_view_form_field_text_create_widget (EvView *view, + EvFormField *field) +{ + EvFormFieldText *field_text = EV_FORM_FIELD_TEXT (field); + GtkWidget *text = NULL; + gchar *txt; + + txt = ev_document_forms_form_field_text_get_text (EV_DOCUMENT_FORMS (view->document), + field); + + switch (field_text->type) { + case EV_FORM_FIELD_TEXT_FILE_SELECT: + /* TODO */ + case EV_FORM_FIELD_TEXT_NORMAL: + text = gtk_entry_new (); + gtk_entry_set_has_frame (GTK_ENTRY (text), FALSE); + gtk_entry_set_max_length (GTK_ENTRY (text), field_text->max_len); + gtk_entry_set_visibility (GTK_ENTRY (text), !field_text->is_password); + + if (txt) { + gtk_entry_set_text (GTK_ENTRY (text), txt); + g_free (txt); + } + + g_signal_connect (text, "changed", + G_CALLBACK (ev_view_form_field_text_changed), + field); + g_signal_connect_after (text, "activate", + G_CALLBACK (ev_view_form_field_destroy), + view); + break; + case EV_FORM_FIELD_TEXT_MULTILINE: { + GtkTextBuffer *buffer; + + text = gtk_text_view_new (); + buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (text)); + + if (txt) { + gtk_text_buffer_set_text (buffer, txt, -1); + g_free (txt); + } + + g_signal_connect (buffer, "changed", + G_CALLBACK (ev_view_form_field_text_changed), + field); + } + break; + } + + g_object_weak_ref (G_OBJECT (text), + (GWeakNotify)ev_view_form_field_text_save, + view); + + return text; +} + +static void +ev_view_form_field_choice_save (EvView *view, + GtkWidget *widget) +{ + EvFormField *field; + + if (!view->document) + return; + + field = g_object_get_data (G_OBJECT (widget), "form-field"); + + if (field->changed) { + GList *l; + EvFormFieldChoice *field_choice = EV_FORM_FIELD_CHOICE (field); + cairo_region_t *field_region; + + field_region = ev_view_form_field_get_region (view, field); + + if (field_choice->is_editable) { + ev_document_forms_form_field_choice_set_text (EV_DOCUMENT_FORMS (view->document), + field, field_choice->text); + } else { + ev_document_forms_form_field_choice_unselect_all (EV_DOCUMENT_FORMS (view->document), field); + for (l = field_choice->selected_items; l; l = g_list_next (l)) { + ev_document_forms_form_field_choice_select_item (EV_DOCUMENT_FORMS (view->document), + field, + GPOINTER_TO_INT (l->data)); + } + } + field->changed = FALSE; + ev_view_reload_page (view, field->page->index, field_region); + cairo_region_destroy (field_region); + } +} + +static void +ev_view_form_field_choice_changed (GtkWidget *widget, + EvFormField *field) +{ + EvFormFieldChoice *field_choice = EV_FORM_FIELD_CHOICE (field); + + if (GTK_IS_COMBO_BOX (widget)) { + gint item; + + item = gtk_combo_box_get_active (GTK_COMBO_BOX (widget)); + if (!field_choice->selected_items || + GPOINTER_TO_INT (field_choice->selected_items->data) != item) { + g_list_free (field_choice->selected_items); + field_choice->selected_items = NULL; + field_choice->selected_items = g_list_prepend (field_choice->selected_items, + GINT_TO_POINTER (item)); + field->changed = TRUE; + } + + if (GTK_IS_COMBO_BOX_ENTRY (widget)) { + gchar *text; + + text = gtk_combo_box_get_active_text (GTK_COMBO_BOX (widget)); + if (!field_choice->text || + (field_choice->text && g_ascii_strcasecmp (field_choice->text, text) != 0)) { + g_free (field_choice->text); + field_choice->text = text; + field->changed = TRUE; + } + } + } else if (GTK_IS_TREE_SELECTION (widget)) { + GtkTreeSelection *selection = GTK_TREE_SELECTION (widget); + GtkTreeModel *model; + GList *items, *l; + + items = gtk_tree_selection_get_selected_rows (selection, &model); + g_list_free (field_choice->selected_items); + field_choice->selected_items = NULL; + + for (l = items; l && l->data; l = g_list_next (l)) { + GtkTreeIter iter; + GtkTreePath *path = (GtkTreePath *)l->data; + gint item; + + gtk_tree_model_get_iter (model, &iter, path); + gtk_tree_model_get (model, &iter, 1, &item, -1); + + field_choice->selected_items = g_list_prepend (field_choice->selected_items, + GINT_TO_POINTER (item)); + + gtk_tree_path_free (path); + } + + g_list_free (items); + + field->changed = TRUE; + } +} + +static GtkWidget * +ev_view_form_field_choice_create_widget (EvView *view, + EvFormField *field) +{ + EvFormFieldChoice *field_choice = EV_FORM_FIELD_CHOICE (field); + GtkWidget *choice; + GtkTreeModel *model; + gint n_items, i; + gint selected_item = 0; + + n_items = ev_document_forms_form_field_choice_get_n_items (EV_DOCUMENT_FORMS (view->document), + field); + model = GTK_TREE_MODEL (gtk_list_store_new (2, G_TYPE_STRING, G_TYPE_INT)); + for (i = 0; i < n_items; i++) { + GtkTreeIter iter; + gchar *item; + + item = ev_document_forms_form_field_choice_get_item (EV_DOCUMENT_FORMS (view->document), + field, i); + if (ev_document_forms_form_field_choice_is_item_selected ( + EV_DOCUMENT_FORMS (view->document), field, i)) { + selected_item = i; + /* FIXME: we need a get_selected_items function in poppler */ + field_choice->selected_items = g_list_prepend (field_choice->selected_items, + GINT_TO_POINTER (i)); + } + + if (item) { + gtk_list_store_append (GTK_LIST_STORE (model), &iter); + gtk_list_store_set (GTK_LIST_STORE (model), &iter, + 0, item, + 1, i, + -1); + g_free (item); + } + } + + if (field_choice->type == EV_FORM_FIELD_CHOICE_LIST) { + GtkCellRenderer *renderer; + GtkWidget *tree_view; + GtkTreeSelection *selection; + + tree_view = gtk_tree_view_new_with_model (model); + gtk_tree_view_set_headers_visible (GTK_TREE_VIEW (tree_view), FALSE); + + selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (tree_view)); + if (field_choice->multi_select) { + gtk_tree_selection_set_mode (selection, GTK_SELECTION_MULTIPLE); + } + + /* TODO: set selected items */ + + renderer = gtk_cell_renderer_text_new (); + gtk_tree_view_insert_column_with_attributes (GTK_TREE_VIEW (tree_view), + 0, + "choix", renderer, + "text", 0, + NULL); + + choice = gtk_scrolled_window_new (NULL, NULL); + gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (choice), + GTK_POLICY_AUTOMATIC, + GTK_POLICY_AUTOMATIC); + gtk_container_add (GTK_CONTAINER (choice), tree_view); + gtk_widget_show (tree_view); + + g_signal_connect (selection, "changed", + G_CALLBACK (ev_view_form_field_choice_changed), + field); + g_signal_connect_after (selection, "changed", + G_CALLBACK (ev_view_form_field_destroy), + view); + } else if (field_choice->is_editable) { /* ComboBoxEntry */ + gchar *text; + + choice = gtk_combo_box_entry_new_with_model (model, 0); + text = ev_document_forms_form_field_choice_get_text (EV_DOCUMENT_FORMS (view->document), field); + if (text) { + gtk_entry_set_text (GTK_ENTRY (gtk_bin_get_child (GTK_BIN (choice))), text); + g_free (text); + } + + g_signal_connect (choice, "changed", + G_CALLBACK (ev_view_form_field_choice_changed), + field); + g_signal_connect_after (gtk_bin_get_child (GTK_BIN (choice)), + "activate", + G_CALLBACK (ev_view_form_field_destroy), + view); + } else { /* ComboBoxText */ + GtkCellRenderer *renderer; + + choice = gtk_combo_box_new_with_model (model); + renderer = gtk_cell_renderer_text_new (); + gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (choice), + renderer, TRUE); + gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (choice), + renderer, + "text", 0, + NULL); + gtk_combo_box_set_active (GTK_COMBO_BOX (choice), selected_item); + gtk_combo_box_popup (GTK_COMBO_BOX (choice)); + + g_signal_connect (choice, "changed", + G_CALLBACK (ev_view_form_field_choice_changed), + field); + g_signal_connect_after (choice, "changed", + G_CALLBACK (ev_view_form_field_destroy), + view); + } + + g_object_unref (model); + + g_object_weak_ref (G_OBJECT (choice), + (GWeakNotify)ev_view_form_field_choice_save, + view); + + return choice; +} + +static void +ev_view_handle_form_field (EvView *view, + EvFormField *field, + gdouble x, + gdouble y) +{ + GtkWidget *field_widget = NULL; + EvMappingList *form_field_mapping; + GdkRectangle view_area; + + if (field->is_read_only) + return; + + if (EV_IS_FORM_FIELD_BUTTON (field)) { + field_widget = ev_view_form_field_button_create_widget (view, field); + } else if (EV_IS_FORM_FIELD_TEXT (field)) { + field_widget = ev_view_form_field_text_create_widget (view, field); + } else if (EV_IS_FORM_FIELD_CHOICE (field)) { + field_widget = ev_view_form_field_choice_create_widget (view, field); + } else if (EV_IS_FORM_FIELD_SIGNATURE (field)) { + /* TODO */ + } + + /* Form field doesn't require a widget */ + if (!field_widget) + return; + + g_object_set_data_full (G_OBJECT (field_widget), "form-field", + g_object_ref (field), + (GDestroyNotify)g_object_unref); + + form_field_mapping = ev_page_cache_get_form_field_mapping (view->page_cache, + field->page->index); + ev_view_get_area_from_mapping (view, field->page->index, + form_field_mapping, + field, &view_area); + + gtk_layout_put (GTK_LAYOUT (view), field_widget, view_area.x, view_area.y); + gtk_widget_show (field_widget); + gtk_widget_grab_focus (field_widget); +} + +/* Annotations */ +static EvViewWindowChild * +ev_view_get_window_child (EvView *view, + GtkWidget *window) +{ + GList *children = view->window_children; + + while (children) { + EvViewWindowChild *child; + + child = (EvViewWindowChild *)children->data; + children = children->next; + + if (child->window == window) + return child; + } + + return NULL; +} + +static void +ev_view_window_child_move (EvView *view, + EvViewWindowChild *child, + gint x, + gint y) +{ + GtkAllocation allocation; + gint width, height; + + gtk_widget_get_allocation (GTK_WIDGET (view), &allocation); + gtk_window_get_size (GTK_WINDOW (child->window), &width, &height); + + child->x = x; + child->y = y; + gtk_window_move (GTK_WINDOW (child->window), + CLAMP (x, child->parent_x, + child->parent_x + allocation.width - width), + CLAMP (y, child->parent_y, + child->parent_y + allocation.height - height)); +} + +static void +ev_view_window_child_move_with_parent (EvView *view, + GtkWidget *window) +{ + EvViewWindowChild *child; + gint root_x, root_y; + + child = ev_view_get_window_child (view, window); + gdk_window_get_origin (gtk_widget_get_window (GTK_WIDGET (view)), + &root_x, &root_y); + if (root_x != child->parent_x || root_y != child->parent_y) { + gint dest_x, dest_y; + + dest_x = child->x + (root_x - child->parent_x); + dest_y = child->y + (root_y - child->parent_y); + child->parent_x = root_x; + child->parent_y = root_y; + ev_view_window_child_move (view, child, dest_x, dest_y); + } + + if (child->visible && !gtk_widget_get_visible (window)) + gtk_widget_show (window); +} + +static void +ev_view_window_child_put (EvView *view, + GtkWidget *window, + guint page, + gint x, + gint y, + gdouble orig_x, + gdouble orig_y) +{ + EvViewWindowChild *child; + gint root_x, root_y; + + gdk_window_get_origin (gtk_widget_get_window (GTK_WIDGET (view)), + &root_x, &root_y); + + child = g_new0 (EvViewWindowChild, 1); + child->window = window; + child->page = page; + child->orig_x = orig_x; + child->orig_y = orig_y; + child->parent_x = root_x; + child->parent_y = root_y; + child->visible = ev_annotation_window_is_open (EV_ANNOTATION_WINDOW (window)); + ev_view_window_child_move (view, child, x + root_x, y + root_y); + + if (child->visible) + gtk_widget_show (window); + else + gtk_widget_hide (window); + + view->window_children = g_list_append (view->window_children, child); +} + +static EvViewWindowChild * +ev_view_find_window_child_for_annot (EvView *view, + guint page, + EvAnnotation *annot) +{ + GList *children = view->window_children; + + while (children) { + EvViewWindowChild *child; + EvAnnotation *wannot; + + child = (EvViewWindowChild *)children->data; + children = children->next; + + if (child->page != page) + continue; + + wannot = ev_annotation_window_get_annotation (EV_ANNOTATION_WINDOW (child->window)); + if (ev_annotation_equal (wannot, annot)) + return child; + } + + return NULL; +} + +static void +ev_view_window_children_free (EvView *view) +{ + GList *l; + + if (!view->window_children) + return; + + for (l = view->window_children; l && l->data; l = g_list_next (l)) { + EvViewWindowChild *child; + + child = (EvViewWindowChild *)l->data; + gtk_widget_destroy (GTK_WIDGET (child->window)); + g_free (child); + } + g_list_free (view->window_children); + view->window_children = NULL; + view->window_child_focus = NULL; +} + +static void +annotation_window_grab_focus (GtkWidget *widget, + EvView *view) +{ + if (view->window_child_focus) + ev_annotation_window_ungrab_focus (EV_ANNOTATION_WINDOW (view->window_child_focus->window)); + view->window_child_focus = ev_view_get_window_child (view, widget); +} + +static void +annotation_window_closed (EvAnnotationWindow *window, + EvView *view) +{ + EvViewWindowChild *child; + + child = ev_view_get_window_child (view, GTK_WIDGET (window)); + child->visible = FALSE; +} + +static void +annotation_window_moved (EvAnnotationWindow *window, + gint x, + gint y, + EvView *view) +{ + EvViewWindowChild *child; + GdkRectangle page_area; + GtkBorder border; + GdkRectangle view_rect; + EvRectangle doc_rect; + gint width, height; + + child = ev_view_get_window_child (view, GTK_WIDGET (window)); + if (child->x == x && child->y == y) + return; + + child->moved = TRUE; + child->x = x; + child->y = y; + + /* Window has been moved by the user, + * we have to set a new origin in doc coords + */ + gtk_window_get_size (GTK_WINDOW (window), &width, &height); + view_rect.x = (x - child->parent_x) + view->scroll_x; + view_rect.y = (y - child->parent_y) + view->scroll_y; + view_rect.width = width; + view_rect.height = height; + + ev_view_get_page_extents (view, child->page, &page_area, &border); + view_rect_to_doc_rect (view, &view_rect, &page_area, &doc_rect); + child->orig_x = doc_rect.x1; + child->orig_y = doc_rect.y1; +} + +static void +ev_view_annotation_save_contents (EvView *view, + GParamSpec *pspec, + EvAnnotation *annot) +{ + if (!view->document) + return; + + ev_document_doc_mutex_lock (); + ev_document_annotations_save_annotation (EV_DOCUMENT_ANNOTATIONS (view->document), + annot, EV_ANNOTATIONS_SAVE_CONTENTS); + ev_document_doc_mutex_unlock (); +} + +static GtkWidget * +ev_view_create_annotation_window (EvView *view, + EvAnnotation *annot, + GtkWindow *parent) +{ + GtkWidget *window; + EvRectangle doc_rect; + GdkRectangle view_rect; + guint page; + + window = ev_annotation_window_new (annot, parent); + g_signal_connect (window, "grab_focus", + G_CALLBACK (annotation_window_grab_focus), + view); + g_signal_connect (window, "closed", + G_CALLBACK (annotation_window_closed), + view); + g_signal_connect (window, "moved", + G_CALLBACK (annotation_window_moved), + view); + g_signal_connect_swapped (annot, "notify::contents", + G_CALLBACK (ev_view_annotation_save_contents), + view); + g_object_set_data (G_OBJECT (annot), "popup", window); + + page = ev_annotation_get_page_index (annot); + ev_annotation_window_get_rectangle (EV_ANNOTATION_WINDOW (window), &doc_rect); + doc_rect_to_view_rect (view, page, &doc_rect, &view_rect); + view_rect.x -= view->scroll_x; + view_rect.y -= view->scroll_y; + + ev_view_window_child_put (view, window, page, + view_rect.x, view_rect.y, + doc_rect.x1, doc_rect.y1); + + return window; +} + +static void +show_annotation_windows (EvView *view, + gint page) +{ + EvMappingList *annots; + GList *l; + GtkWindow *parent; + + parent = GTK_WINDOW (gtk_widget_get_toplevel (GTK_WIDGET (view))); + + annots = ev_page_cache_get_annot_mapping (view->page_cache, page); + + for (l = ev_mapping_list_get_list (annots); l && l->data; l = g_list_next (l)) { + EvAnnotation *annot; + EvViewWindowChild *child; + GtkWidget *window; + + annot = ((EvMapping *)(l->data))->data; + + if (!EV_IS_ANNOTATION_MARKUP (annot)) + continue; + + if (!ev_annotation_markup_has_popup (EV_ANNOTATION_MARKUP (annot))) + continue; + + window = g_object_get_data (G_OBJECT (annot), "popup"); + if (window) { + ev_view_window_child_move_with_parent (view, window); + continue; + } + + /* Look if we already have a popup for this annot */ + child = ev_view_find_window_child_for_annot (view, page, annot); + window = child ? child->window : NULL; + if (window) { + ev_annotation_window_set_annotation (EV_ANNOTATION_WINDOW (window), annot); + g_object_set_data (G_OBJECT (annot), "popup", window); + ev_view_window_child_move_with_parent (view, window); + } else { + ev_view_create_annotation_window (view, annot, parent); + } + } +} + +static void +hide_annotation_windows (EvView *view, + gint page) +{ + EvMappingList *annots; + GList *l; + + annots = ev_page_cache_get_annot_mapping (view->page_cache, page); + + for (l = ev_mapping_list_get_list (annots); l && l->data; l = g_list_next (l)) { + EvAnnotation *annot; + GtkWidget *window; + + annot = ((EvMapping *)(l->data))->data; + + if (!EV_IS_ANNOTATION_MARKUP (annot)) + continue; + + window = g_object_get_data (G_OBJECT (annot), "popup"); + if (window) + gtk_widget_hide (window); + } +} + +static EvAnnotation * +ev_view_get_annotation_at_location (EvView *view, + gdouble x, + gdouble y) +{ + gint page = -1; + gint x_new = 0, y_new = 0; + EvMappingList *annotations_mapping; + + if (!EV_IS_DOCUMENT_ANNOTATIONS (view->document)) + return NULL; + + if (!get_doc_point_from_location (view, x, y, &page, &x_new, &y_new)) + return NULL; + + annotations_mapping = ev_page_cache_get_annot_mapping (view->page_cache, page); + + if (annotations_mapping) + return ev_mapping_list_get_data (annotations_mapping, x_new, y_new); + else + return NULL; +} + +static void +ev_view_annotation_show_popup_window (EvView *view, + GtkWidget *window) +{ + EvViewWindowChild *child; + + if (!window) + return; + + child = ev_view_get_window_child (view, window); + if (!child->visible) { + child->visible = TRUE; + ev_view_window_child_move (view, child, child->x, child->y); + gtk_widget_show (window); + } +} + +static void +ev_view_handle_annotation (EvView *view, + EvAnnotation *annot, + gdouble x, + gdouble y, + guint32 timestamp) +{ + if (EV_IS_ANNOTATION_MARKUP (annot)) { + GtkWidget *window; + + window = g_object_get_data (G_OBJECT (annot), "popup"); + ev_view_annotation_show_popup_window (view, window); + } + + if (EV_IS_ANNOTATION_ATTACHMENT (annot)) { + EvAttachment *attachment; + + attachment = ev_annotation_attachment_get_attachment (EV_ANNOTATION_ATTACHMENT (annot)); + if (attachment) { + GError *error = NULL; + + ev_attachment_open (attachment, + gtk_widget_get_screen (GTK_WIDGET (view)), + timestamp, + &error); + + if (error) { + g_warning ("%s", error->message); + g_error_free (error); + } + } + } +} + +static void +ev_view_create_annotation (EvView *view, + EvAnnotationType annot_type, + gint x, + gint y) +{ + EvAnnotation *annot; + GdkPoint point; + GdkRectangle page_area; + GtkBorder border; + EvRectangle doc_rect, popup_rect; + EvPage *page; + GdkColor color = { 0, 65535, 65535, 0 }; + GdkRectangle view_rect; + cairo_region_t *region; + + point.x = x; + point.y = y; + ev_view_get_page_extents (view, view->current_page, &page_area, &border); + view_point_to_doc_point (view, &point, &page_area, + &doc_rect.x1, &doc_rect.y1); + doc_rect.x2 = doc_rect.x1 + 24; + doc_rect.y2 = doc_rect.y1 + 24; + + ev_document_doc_mutex_lock (); + page = ev_document_get_page (view->document, view->current_page); + switch (annot_type) { + case EV_ANNOTATION_TYPE_TEXT: + annot = ev_annotation_text_new (page); + break; + case EV_ANNOTATION_TYPE_ATTACHMENT: + /* TODO */ + g_object_unref (page); + ev_document_doc_mutex_unlock (); + return; + default: + g_assert_not_reached (); + } + g_object_unref (page); + + ev_annotation_set_color (annot, &color); + + if (EV_IS_ANNOTATION_MARKUP (annot)) { + popup_rect.x1 = doc_rect.x2; + popup_rect.x2 = popup_rect.x1 + 200; + popup_rect.y1 = doc_rect.y2; + popup_rect.y2 = popup_rect.y1 + 150; + g_object_set (annot, + "rectangle", &popup_rect, + "has_popup", TRUE, + "popup_is_open", FALSE, + "label", g_get_real_name (), + "opacity", 1.0, + NULL); + } + ev_document_annotations_add_annotation (EV_DOCUMENT_ANNOTATIONS (view->document), + annot, &doc_rect); + ev_document_doc_mutex_unlock (); + + /* If the page didn't have annots, mark the cache as dirty */ + if (!ev_page_cache_get_annot_mapping (view->page_cache, view->current_page)) + ev_page_cache_mark_dirty (view->page_cache, view->current_page); + + if (EV_IS_ANNOTATION_MARKUP (annot)) { + GtkWindow *parent; + GtkWidget *window; + + parent = GTK_WINDOW (gtk_widget_get_toplevel (GTK_WIDGET (view))); + window = ev_view_create_annotation_window (view, annot, parent); + + /* Show the annot window the first time */ + ev_view_annotation_show_popup_window (view, window); + } + + doc_rect_to_view_rect (view, view->current_page, &doc_rect, &view_rect); + view_rect.x -= view->scroll_x; + view_rect.y -= view->scroll_y; + region = cairo_region_create_rectangle (&view_rect); + ev_view_reload_page (view, view->current_page, region); + cairo_region_destroy (region); + + g_signal_emit (view, signals[SIGNAL_ANNOT_ADDED], 0, annot); +} + +void +ev_view_focus_annotation (EvView *view, + EvMapping *annot_mapping) +{ + GdkRectangle view_rect; + EvAnnotation *annot; + guint page; + + if (!EV_IS_DOCUMENT_ANNOTATIONS (view->document)) + return; + + if (view->focus_annotation == annot_mapping) + return; + + view->focus_annotation = annot_mapping; + annot = (EvAnnotation *)annot_mapping->data; + + page = ev_annotation_get_page_index (annot); + ev_document_model_set_page (view->model, page); + + doc_rect_to_view_rect (view, page, + &annot_mapping->area, &view_rect); + ensure_rectangle_is_visible (view, &view_rect); + gtk_widget_queue_draw (GTK_WIDGET (view)); +} + +void +ev_view_begin_add_annotation (EvView *view, + EvAnnotationType annot_type) +{ + if (annot_type == EV_ANNOTATION_TYPE_UNKNOWN) + return; + + if (view->adding_annot) + return; + + view->adding_annot = TRUE; + view->adding_annot_type = annot_type; + ev_view_set_cursor (view, EV_VIEW_CURSOR_ADD); +} + +void +ev_view_cancel_add_annotation (EvView *view) +{ + gint x, y; + + if (!view->adding_annot) + return; + + view->adding_annot = FALSE; + gtk_widget_get_pointer (GTK_WIDGET (view), &x, &y); + ev_view_handle_cursor_over_xy (view, x, y); +} + +static gboolean +ev_view_synctex_backward_search (EvView *view, + gdouble x, + gdouble y) +{ + gint page = -1; + gint x_new = 0, y_new = 0; + EvSourceLink *link; + + if (!ev_document_has_synctex (view->document)) + return FALSE; + + if (!get_doc_point_from_location (view, x, y, &page, &x_new, &y_new)) + return FALSE; + + link = ev_document_synctex_backward_search (view->document, page, x_new, y_new); + if (link) { + g_signal_emit (view, signals[SIGNAL_SYNC_SOURCE], 0, link); + g_free (link); + + return TRUE; + } + + return FALSE; +} + +/*** GtkWidget implementation ***/ + +static void +ev_view_size_request_continuous_dual_page (EvView *view, + GtkRequisition *requisition) +{ + gint n_pages; + + n_pages = ev_document_get_n_pages (view->document) + 1; + get_page_y_offset (view, n_pages, &requisition->height); + + switch (view->sizing_mode) { + case EV_SIZING_FIT_WIDTH: + case EV_SIZING_BEST_FIT: + requisition->width = 1; + + break; + case EV_SIZING_FREE: { + gint max_width; + GtkBorder border; + + ev_view_get_max_page_size (view, &max_width, NULL); + compute_border (view, max_width, max_width, &border); + requisition->width = (max_width + border.left + border.right) * 2 + (view->spacing * 3); + } + break; + default: + g_assert_not_reached (); + } +} + +static void +ev_view_size_request_continuous (EvView *view, + GtkRequisition *requisition) +{ + gint n_pages; + + n_pages = ev_document_get_n_pages (view->document); + get_page_y_offset (view, n_pages, &requisition->height); + + switch (view->sizing_mode) { + case EV_SIZING_FIT_WIDTH: + case EV_SIZING_BEST_FIT: + requisition->width = 1; + + break; + case EV_SIZING_FREE: { + gint max_width; + GtkBorder border; + + ev_view_get_max_page_size (view, &max_width, NULL); + compute_border (view, max_width, max_width, &border); + requisition->width = max_width + (view->spacing * 2) + border.left + border.right; + } + break; + default: + g_assert_not_reached (); + } +} + +static void +ev_view_size_request_dual_page (EvView *view, + GtkRequisition *requisition) +{ + GtkBorder border; + gint width, height; + + if (view->sizing_mode == EV_SIZING_BEST_FIT) { + requisition->width = 1; + requisition->height = 1; + + return; + } + + /* Find the largest of the two. */ + ev_view_get_page_size (view, + view->current_page, + &width, &height); + if (view->current_page + 1 < ev_document_get_n_pages (view->document)) { + gint width_2, height_2; + ev_view_get_page_size (view, + view->current_page + 1, + &width_2, &height_2); + if (width_2 > width) { + width = width_2; + height = height_2; + } + } + compute_border (view, width, height, &border); + + requisition->width = view->sizing_mode == EV_SIZING_FIT_WIDTH ? 1 : + ((width + border.left + border.right) * 2) + (view->spacing * 3); + requisition->height = (height + border.top + border.bottom) + (view->spacing * 2); +} + +static void +ev_view_size_request_single_page (EvView *view, + GtkRequisition *requisition) +{ + GtkBorder border; + gint width, height; + + if (view->sizing_mode == EV_SIZING_BEST_FIT) { + requisition->width = 1; + requisition->height = 1; + + return; + } + + ev_view_get_page_size (view, view->current_page, &width, &height); + compute_border (view, width, height, &border); + + requisition->width = view->sizing_mode == EV_SIZING_FIT_WIDTH ? 1 : + width + border.left + border.right + (2 * view->spacing); + requisition->height = height + border.top + border.bottom + (2 * view->spacing); +} + +static void +ev_view_size_request (GtkWidget *widget, + GtkRequisition *requisition) +{ + EvView *view = EV_VIEW (widget); + + if (view->document == NULL) { + view->requisition.width = 1; + view->requisition.height = 1; + + *requisition = view->requisition; + + return; + } + + /* Get zoom for size here when not called from + * ev_view_size_allocate() + */ + if (!view->internal_size_request && + (view->sizing_mode == EV_SIZING_FIT_WIDTH || + view->sizing_mode == EV_SIZING_BEST_FIT)) { + GtkAllocation allocation; + + gtk_widget_get_allocation (widget, &allocation); + ev_view_zoom_for_size (view, + allocation.width, + allocation.height); + } + + if (view->continuous && view->dual_page) + ev_view_size_request_continuous_dual_page (view, &view->requisition); + else if (view->continuous) + ev_view_size_request_continuous (view, &view->requisition); + else if (view->dual_page) + ev_view_size_request_dual_page (view, &view->requisition); + else + ev_view_size_request_single_page (view, &view->requisition); + + *requisition = view->requisition; +} + +static void +ev_view_size_allocate (GtkWidget *widget, + GtkAllocation *allocation) +{ + EvView *view = EV_VIEW (widget); + GList *children, *l; + gint root_x, root_y; + + GTK_WIDGET_CLASS (ev_view_parent_class)->size_allocate (widget, allocation); + + if (!view->document) + return; + + if (view->sizing_mode == EV_SIZING_FIT_WIDTH || + view->sizing_mode == EV_SIZING_BEST_FIT) { + GtkRequisition req; + + ev_view_zoom_for_size (view, + allocation->width, + allocation->height); + view->internal_size_request = TRUE; + ev_view_size_request (widget, &req); + view->internal_size_request = FALSE; + } + + view_set_adjustment_values (view, GTK_ORIENTATION_HORIZONTAL); + view_set_adjustment_values (view, GTK_ORIENTATION_VERTICAL); + + if (view->document) + view_update_range_and_current_page (view); + + view->pending_scroll = SCROLL_TO_KEEP_POSITION; + view->pending_resize = FALSE; + view->pending_point.x = 0; + view->pending_point.y = 0; + + children = gtk_container_get_children (GTK_CONTAINER (widget)); + for (l = children; l && l->data; l = g_list_next (l)) { + EvFormField *field; + GdkRectangle view_area; + EvMappingList *form_field_mapping; + GtkAllocation child_allocation; + GtkRequisition child_requisition; + GtkWidget *child = (GtkWidget *)l->data; + + field = g_object_get_data (G_OBJECT (child), "form-field"); + if (!field) + continue; + + form_field_mapping = ev_page_cache_get_form_field_mapping (view->page_cache, + field->page->index); + ev_view_get_area_from_mapping (view, field->page->index, + form_field_mapping, + field, &view_area); + + gtk_widget_size_request (child, &child_requisition); + if (child_requisition.width != view_area.width || + child_requisition.height != view_area.height) + gtk_widget_set_size_request (child, view_area.width, view_area.height); + + gtk_container_child_get (GTK_CONTAINER (widget), + child, + "x", &child_allocation.x, + "y", &child_allocation.y, + NULL); + if (child_allocation.x != view_area.x || + child_allocation.y != view_area.y) { + gtk_layout_move (GTK_LAYOUT (widget), child, view_area.x, view_area.y); + } + } + g_list_free (children); + + if (view->window_children) + gdk_window_get_origin (gtk_widget_get_window (GTK_WIDGET (view)), + &root_x, &root_y); + + for (l = view->window_children; l && l->data; l = g_list_next (l)) { + EvViewWindowChild *child; + EvRectangle doc_rect; + GdkRectangle view_rect; + + child = (EvViewWindowChild *)l->data; + + ev_annotation_window_get_rectangle (EV_ANNOTATION_WINDOW (child->window), &doc_rect); + if (child->moved) { + doc_rect.x1 = child->orig_x; + doc_rect.y1 = child->orig_y; + } + doc_rect_to_view_rect (view, child->page, &doc_rect, &view_rect); + view_rect.x -= view->scroll_x; + view_rect.y -= view->scroll_y; + + if (view_rect.x != child->orig_x || view_rect.y != child->orig_y) { + child->parent_x = root_x; + child->parent_y = root_y; + ev_view_window_child_move (view, child, view_rect.x + root_x, view_rect.y + root_y); + } + } +} + +static void +ev_view_realize (GtkWidget *widget) +{ + EvView *view = EV_VIEW (widget); + GdkWindow *bin_window; + GtkStyle *style; + + if (GTK_WIDGET_CLASS (ev_view_parent_class)->realize) + (* GTK_WIDGET_CLASS (ev_view_parent_class)->realize) (widget); + + bin_window = gtk_layout_get_bin_window (GTK_LAYOUT (view)); + gdk_window_set_events (bin_window, + (gdk_window_get_events (bin_window) | + GDK_EXPOSURE_MASK | + GDK_BUTTON_PRESS_MASK | + GDK_BUTTON_RELEASE_MASK | + GDK_SCROLL_MASK | + GDK_KEY_PRESS_MASK | + GDK_POINTER_MOTION_MASK | + GDK_POINTER_MOTION_HINT_MASK | + GDK_ENTER_NOTIFY_MASK | + GDK_LEAVE_NOTIFY_MASK)); + + style = gtk_widget_get_style (widget); + gdk_window_set_background (bin_window, &style->mid[GTK_STATE_NORMAL]); + + on_adjustment_value_changed (NULL, view); +} + +static gboolean +ev_view_scroll_event (GtkWidget *widget, GdkEventScroll *event) +{ + EvView *view = EV_VIEW (widget); + guint state; + + state = event->state & gtk_accelerator_get_default_mod_mask (); + + if (state == GDK_CONTROL_MASK) { + ev_document_model_set_sizing_mode (view->model, EV_SIZING_FREE); + if (event->direction == GDK_SCROLL_UP || + event->direction == GDK_SCROLL_LEFT) { + if (ev_view_can_zoom_in (view)) { + ev_view_zoom_in (view); + } + } else { + if (ev_view_can_zoom_out (view)) { + ev_view_zoom_out (view); + } + } + + return TRUE; + } + + view->jump_to_find_result = FALSE; + + /* Shift+Wheel scrolls the in the perpendicular direction */ + if (state & GDK_SHIFT_MASK) { + if (event->direction == GDK_SCROLL_UP) + event->direction = GDK_SCROLL_LEFT; + else if (event->direction == GDK_SCROLL_LEFT) + event->direction = GDK_SCROLL_UP; + else if (event->direction == GDK_SCROLL_DOWN) + event->direction = GDK_SCROLL_RIGHT; + else if (event->direction == GDK_SCROLL_RIGHT) + event->direction = GDK_SCROLL_DOWN; + + event->state &= ~GDK_SHIFT_MASK; + state &= ~GDK_SHIFT_MASK; + } + + if (state == 0 && view->sizing_mode == EV_SIZING_BEST_FIT && !view->continuous) { + switch (event->direction) { + case GDK_SCROLL_DOWN: + case GDK_SCROLL_RIGHT: + ev_view_next_page (view); + break; + case GDK_SCROLL_UP: + case GDK_SCROLL_LEFT: + ev_view_previous_page (view); + break; + } + + return TRUE; + } + + return FALSE; +} + +static EvViewSelection * +find_selection_for_page (EvView *view, + gint page) +{ + GList *list; + + for (list = view->selection_info.selections; list != NULL; list = list->next) { + EvViewSelection *selection; + + selection = (EvViewSelection *) list->data; + + if (selection->page == page) + return selection; + } + + return NULL; +} + +static gboolean +ev_view_expose_event (GtkWidget *widget, + GdkEventExpose *event) +{ + EvView *view = EV_VIEW (widget); + GdkWindow *bin_window; + cairo_t *cr; + gint i; + + if (view->loading) { + show_loading_window (view); + } else if (view->loading_window && + gtk_widget_get_visible (view->loading_window)) { + ev_view_loading_window_move (view); + } + + if (view->document == NULL) + return FALSE; + + bin_window = gtk_layout_get_bin_window (GTK_LAYOUT (view)); + cr = gdk_cairo_create (bin_window); + + for (i = view->start_page; i >= 0 && i <= view->end_page; i++) { + GdkRectangle page_area; + GtkBorder border; + gboolean page_ready = TRUE; + + if (!ev_view_get_page_extents (view, i, &page_area, &border)) + continue; + + page_area.x -= view->scroll_x; + page_area.y -= view->scroll_y; + + draw_one_page (view, i, cr, &page_area, &border, &(event->area), &page_ready); + + if (page_ready && view->find_pages && view->highlight_find_results) + highlight_find_results (view, i); + if (page_ready && EV_IS_DOCUMENT_ANNOTATIONS (view->document)) + show_annotation_windows (view, i); + if (page_ready && view->focus_annotation) + focus_annotation (view, i, &event->area); + if (page_ready && view->synctex_result) + highlight_forward_search_results (view, i); + } + + cairo_destroy (cr); + + if (GTK_WIDGET_CLASS (ev_view_parent_class)->expose_event) + (* GTK_WIDGET_CLASS (ev_view_parent_class)->expose_event) (widget, event); + + return FALSE; +} + +static gboolean +ev_view_do_popup_menu (EvView *view, + gdouble x, + gdouble y) +{ + GList *items = NULL; + EvLink *link; + EvImage *image; + EvAnnotation *annot; + + image = ev_view_get_image_at_location (view, x, y); + if (image) + items = g_list_prepend (items, image); + + link = ev_view_get_link_at_location (view, x, y); + if (link) + items = g_list_prepend (items, link); + + annot = ev_view_get_annotation_at_location (view, x, y); + if (annot) + items = g_list_prepend (items, annot); + + g_signal_emit (view, signals[SIGNAL_POPUP_MENU], 0, items); + + g_list_free (items); + + return TRUE; +} + +static gboolean +ev_view_popup_menu (GtkWidget *widget) +{ + gint x, y; + + gtk_widget_get_pointer (widget, &x, &y); + return ev_view_do_popup_menu (EV_VIEW (widget), x, y); +} + +static void +get_link_area (EvView *view, + gint x, + gint y, + EvLink *link, + GdkRectangle *area) +{ + EvMappingList *link_mapping; + gint page; + gint x_offset = 0, y_offset = 0; + + x += view->scroll_x; + y += view->scroll_y; + + find_page_at_location (view, x, y, &page, &x_offset, &y_offset); + + link_mapping = ev_page_cache_get_link_mapping (view->page_cache, page); + ev_view_get_area_from_mapping (view, page, + link_mapping, + link, area); +} + +static void +get_annot_area (EvView *view, + gint x, + gint y, + EvAnnotation *annot, + GdkRectangle *area) +{ + EvMappingList *annot_mapping; + gint page; + gint x_offset = 0, y_offset = 0; + + x += view->scroll_x; + y += view->scroll_y; + + find_page_at_location (view, x, y, &page, &x_offset, &y_offset); + + annot_mapping = ev_page_cache_get_annot_mapping (view->page_cache, page); + ev_view_get_area_from_mapping (view, page, + annot_mapping, + annot, area); +} + +static gboolean +ev_view_query_tooltip (GtkWidget *widget, + gint x, + gint y, + gboolean keyboard_tip, + GtkTooltip *tooltip) +{ + EvView *view = EV_VIEW (widget); + EvLink *link; + EvAnnotation *annot; + gchar *text; + + annot = ev_view_get_annotation_at_location (view, x, y); + if (annot) { + const gchar *contents; + + if ((contents = ev_annotation_get_contents (annot))) { + GdkRectangle annot_area; + + get_annot_area (view, x, y, annot, &annot_area); + gtk_tooltip_set_text (tooltip, contents); + gtk_tooltip_set_tip_area (tooltip, &annot_area); + + return TRUE; + } + } + + link = ev_view_get_link_at_location (view, x, y); + if (!link) + return FALSE; + + text = tip_from_link (view, link); + if (text && g_utf8_validate (text, -1, NULL)) { + GdkRectangle link_area; + + get_link_area (view, x, y, link, &link_area); + gtk_tooltip_set_text (tooltip, text); + gtk_tooltip_set_tip_area (tooltip, &link_area); + } + g_free (text); + + return TRUE; +} + +static void +start_selection_for_event (EvView *view, + GdkEventButton *event) +{ + clear_selection (view); + + view->selection_info.start.x = event->x + view->scroll_x; + view->selection_info.start.y = event->y + view->scroll_y; + + switch (event->type) { + case GDK_2BUTTON_PRESS: + view->selection_info.style = EV_SELECTION_STYLE_WORD; + break; + case GDK_3BUTTON_PRESS: + view->selection_info.style = EV_SELECTION_STYLE_LINE; + break; + default: + view->selection_info.style = EV_SELECTION_STYLE_GLYPH; + return; + } + + /* In case of WORD or LINE, compute selections now */ + compute_selections (view, + view->selection_info.style, + &(view->selection_info.start), + &(view->selection_info.start)); +} + +static gboolean +ev_view_button_press_event (GtkWidget *widget, + GdkEventButton *event) +{ + EvView *view = EV_VIEW (widget); + + if (!view->document) + return FALSE; + + if (!gtk_widget_has_focus (widget)) { + gtk_widget_grab_focus (widget); + } + + if (view->window_child_focus) { + EvAnnotationWindow *window; + EvAnnotation *annot; + + window = EV_ANNOTATION_WINDOW (view->window_child_focus->window); + annot = ev_annotation_window_get_annotation (window); + ev_annotation_window_ungrab_focus (window); + view->window_child_focus = NULL; + } + + view->pressed_button = event->button; + view->selection_info.in_drag = FALSE; + + if (view->adding_annot) + return FALSE; + + if (view->scroll_info.autoscrolling) + return TRUE; + + switch (event->button) { + case 1: { + EvImage *image; + EvAnnotation *annot; + EvFormField *field; + + if (event->state & GDK_CONTROL_MASK) + return ev_view_synctex_backward_search (view, event->x , event->y); + + if (EV_IS_SELECTION (view->document) && view->selection_info.selections) { + if (event->type == GDK_3BUTTON_PRESS) { + start_selection_for_event (view, event); + } else if (location_in_selected_text (view, + event->x + view->scroll_x, + event->y + view->scroll_y)) { + view->selection_info.in_drag = TRUE; + } else { + start_selection_for_event (view, event); + } + + gtk_widget_queue_draw (widget); + } else if ((annot = ev_view_get_annotation_at_location (view, event->x, event->y))) { + ev_view_handle_annotation (view, annot, event->x, event->y, event->time); + } else if ((field = ev_view_get_form_field_at_location (view, event->x, event->y))) { + ev_view_remove_all (view); + ev_view_handle_form_field (view, field, event->x, event->y); + } else if (!location_in_text (view, event->x + view->scroll_x, event->y + view->scroll_y) && + (image = ev_view_get_image_at_location (view, event->x, event->y))) { + if (view->image_dnd_info.image) + g_object_unref (view->image_dnd_info.image); + view->image_dnd_info.image = g_object_ref (image); + view->image_dnd_info.in_drag = TRUE; + + view->image_dnd_info.start.x = event->x + view->scroll_x; + view->image_dnd_info.start.y = event->y + view->scroll_y; + } else { + ev_view_remove_all (view); + + if (view->synctex_result) { + g_free (view->synctex_result); + view->synctex_result = NULL; + gtk_widget_queue_draw (widget); + } + + if (view->focus_annotation) + view->focus_annotation = NULL; + + if (EV_IS_SELECTION (view->document)) + start_selection_for_event (view, event); + } + } + return TRUE; + case 2: + /* use root coordinates as reference point because + * scrolling changes window relative coordinates */ + view->drag_info.start.x = event->x_root; + view->drag_info.start.y = event->y_root; + view->drag_info.hadj = gtk_adjustment_get_value (view->hadjustment); + view->drag_info.vadj = gtk_adjustment_get_value (view->vadjustment); + + ev_view_set_cursor (view, EV_VIEW_CURSOR_DRAG); + + return TRUE; + case 3: + view->scroll_info.start_y = event->y; + return ev_view_do_popup_menu (view, event->x, event->y); + } + + return FALSE; +} + +static void +ev_view_remove_all (EvView *view) +{ + GList *children, *child; + + children = gtk_container_get_children (GTK_CONTAINER (view)); + for (child = children; child && child->data; child = g_list_next (child)) { + gtk_container_remove (GTK_CONTAINER (view), + GTK_WIDGET (child->data)); + } + g_list_free (children); +} + +/*** Drag and Drop ***/ +static void +ev_view_drag_data_get (GtkWidget *widget, + GdkDragContext *context, + GtkSelectionData *selection_data, + guint info, + guint time) +{ + EvView *view = EV_VIEW (widget); + + switch (info) { + case TARGET_DND_TEXT: + if (EV_IS_SELECTION (view->document) && + view->selection_info.selections) { + gchar *text; + + text = get_selected_text (view); + gtk_selection_data_set_text (selection_data, + text, + strlen (text)); + g_free (text); + } + break; + case TARGET_DND_IMAGE: + if (view->image_dnd_info.image) { + GdkPixbuf *pixbuf; + + ev_document_doc_mutex_lock (); + pixbuf = ev_document_images_get_image (EV_DOCUMENT_IMAGES (view->document), + view->image_dnd_info.image); + ev_document_doc_mutex_unlock (); + + gtk_selection_data_set_pixbuf (selection_data, pixbuf); + g_object_unref (pixbuf); + } + break; + case TARGET_DND_URI: + if (view->image_dnd_info.image) { + GdkPixbuf *pixbuf; + const gchar *tmp_uri; + gchar *uris[2]; + + ev_document_doc_mutex_lock (); + pixbuf = ev_document_images_get_image (EV_DOCUMENT_IMAGES (view->document), + view->image_dnd_info.image); + ev_document_doc_mutex_unlock (); + + tmp_uri = ev_image_save_tmp (view->image_dnd_info.image, pixbuf); + g_object_unref (pixbuf); + + uris[0] = (gchar *)tmp_uri; + uris[1] = NULL; + gtk_selection_data_set_uris (selection_data, uris); + } + } +} + +static gboolean +ev_view_drag_motion (GtkWidget *widget, + GdkDragContext *context, + gint x, + gint y, + guint time) +{ + if (gtk_drag_get_source_widget (context) == widget) + gdk_drag_status (context, 0, time); + else + gdk_drag_status (context, gdk_drag_context_get_suggested_action (context), time); + + return TRUE; +} + +static gboolean +selection_update_idle_cb (EvView *view) +{ + compute_selections (view, + view->selection_info.style, + &view->selection_info.start, + &view->motion); + view->selection_update_id = 0; + return FALSE; +} + +static gboolean +selection_scroll_timeout_cb (EvView *view) +{ + gint x, y, shift = 0; + GtkWidget *widget = GTK_WIDGET (view); + GtkAllocation allocation; + + gtk_widget_get_allocation (widget, &allocation); + gtk_widget_get_pointer (widget, &x, &y); + + if (y > allocation.height) { + shift = (y - allocation.height) / 2; + } else if (y < 0) { + shift = y / 2; + } + + if (shift) + gtk_adjustment_set_value (view->vadjustment, + CLAMP (gtk_adjustment_get_value (view->vadjustment) + shift, + gtk_adjustment_get_lower (view->vadjustment), + gtk_adjustment_get_upper (view->vadjustment) - + gtk_adjustment_get_page_size (view->vadjustment))); + + if (x > allocation.width) { + shift = (x - allocation.width) / 2; + } else if (x < 0) { + shift = x / 2; + } + + if (shift) + gtk_adjustment_set_value (view->hadjustment, + CLAMP (gtk_adjustment_get_value (view->hadjustment) + shift, + gtk_adjustment_get_lower (view->hadjustment), + gtk_adjustment_get_upper (view->hadjustment) - + gtk_adjustment_get_page_size (view->hadjustment))); + + return TRUE; +} + +static gboolean +ev_view_drag_update_momentum (EvView *view) +{ + int i; + if (!view->drag_info.in_drag) + return FALSE; + + for (i = DRAG_HISTORY - 1; i > 0; i--) { + view->drag_info.buffer[i].x = view->drag_info.buffer[i-1].x; + view->drag_info.buffer[i].y = view->drag_info.buffer[i-1].y; + } + + /* Momentum is a moving average of 10ms granularity over + * the last 100ms with each 10ms stored in buffer. + */ + + view->drag_info.momentum.x = (view->drag_info.buffer[DRAG_HISTORY - 1].x - view->drag_info.buffer[0].x); + view->drag_info.momentum.y = (view->drag_info.buffer[DRAG_HISTORY - 1].y - view->drag_info.buffer[0].y); + + return TRUE; +} + +static gboolean +ev_view_scroll_drag_release (EvView *view) +{ + gdouble dhadj_value, dvadj_value; + gdouble oldhadjustment, oldvadjustment; + gdouble h_page_size, v_page_size; + gdouble h_upper, v_upper; + GtkAllocation allocation; + + view->drag_info.momentum.x /= 1.2; + view->drag_info.momentum.y /= 1.2; /* Alter these constants to change "friction" */ + + gtk_widget_get_allocation (GTK_WIDGET (view), &allocation); + + h_page_size = gtk_adjustment_get_page_size (view->hadjustment); + v_page_size = gtk_adjustment_get_page_size (view->vadjustment); + + dhadj_value = h_page_size * + (gdouble)view->drag_info.momentum.x / allocation.width; + dvadj_value = v_page_size * + (gdouble)view->drag_info.momentum.y / allocation.height; + + oldhadjustment = gtk_adjustment_get_value (view->hadjustment); + oldvadjustment = gtk_adjustment_get_value (view->vadjustment); + + h_upper = gtk_adjustment_get_upper (view->hadjustment); + v_upper = gtk_adjustment_get_upper (view->vadjustment); + + /* When we reach the edges, we need either to absorb some momentum and bounce by + * multiplying it on -0.5 or stop scrolling by setting momentum to 0. */ + if (((oldhadjustment + dhadj_value) > (h_upper - h_page_size)) || + ((oldhadjustment + dhadj_value) < 0)) + view->drag_info.momentum.x = 0; + if (((oldvadjustment + dvadj_value) > (v_upper - v_page_size)) || + ((oldvadjustment + dvadj_value) < 0)) + view->drag_info.momentum.y = 0; + + gtk_adjustment_set_value (view->hadjustment, + MIN (oldhadjustment + dhadj_value, + h_upper - h_page_size)); + gtk_adjustment_set_value (view->vadjustment, + MIN (oldvadjustment + dvadj_value, + v_upper - v_page_size)); + + if (((view->drag_info.momentum.x < 1) && (view->drag_info.momentum.x > -1)) && + ((view->drag_info.momentum.y < 1) && (view->drag_info.momentum.y > -1))) + return FALSE; + else + return TRUE; +} + +static gboolean +ev_view_motion_notify_event (GtkWidget *widget, + GdkEventMotion *event) +{ + EvView *view = EV_VIEW (widget); + GdkWindow *bin_window; + gint x, y; + + if (!view->document) + return FALSE; + + bin_window = gtk_layout_get_bin_window (GTK_LAYOUT (view)); + + if (event->is_hint || event->window != bin_window) { + gtk_widget_get_pointer (widget, &x, &y); + } else { + x = event->x; + y = event->y; + } + + if (view->scroll_info.autoscrolling) { + view->scroll_info.last_y = y; + return TRUE; + } + + if (view->selection_info.in_drag) { + if (gtk_drag_check_threshold (widget, + view->selection_info.start.x, + view->selection_info.start.y, + x, y)) { + GtkTargetList *target_list = gtk_target_list_new (NULL, 0); + + gtk_target_list_add_text_targets (target_list, TARGET_DND_TEXT); + + gtk_drag_begin (widget, target_list, + GDK_ACTION_COPY, + 1, (GdkEvent *)event); + + view->selection_info.in_drag = FALSE; + + gtk_target_list_unref (target_list); + + return TRUE; + } + } else if (view->image_dnd_info.in_drag) { + if (gtk_drag_check_threshold (widget, + view->selection_info.start.x, + view->selection_info.start.y, + x, y)) { + GtkTargetList *target_list = gtk_target_list_new (NULL, 0); + + gtk_target_list_add_uri_targets (target_list, TARGET_DND_URI); + gtk_target_list_add_image_targets (target_list, TARGET_DND_IMAGE, TRUE); + + gtk_drag_begin (widget, target_list, + GDK_ACTION_COPY, + 1, (GdkEvent *)event); + + view->image_dnd_info.in_drag = FALSE; + + gtk_target_list_unref (target_list); + + return TRUE; + } + } + + switch (view->pressed_button) { + case 1: + /* For the Evince 0.4.x release, we limit selection to un-rotated + * documents only. + */ + if (view->rotation != 0) + return FALSE; + + /* Schedule timeout to scroll during selection and additionally + * scroll once to allow arbitrary speed. */ + if (!view->selection_scroll_id) + view->selection_scroll_id = g_timeout_add (SCROLL_TIME, + (GSourceFunc)selection_scroll_timeout_cb, + view); + else + selection_scroll_timeout_cb (view); + + view->selection_info.in_selection = TRUE; + view->motion.x = x + view->scroll_x; + view->motion.y = y + view->scroll_y; + + /* Queue an idle to handle the motion. We do this because + * handling any selection events in the motion could be slower + * than new motion events reach us. We always put it in the + * idle to make sure we catch up and don't visibly lag the + * mouse. */ + if (!view->selection_update_id) + view->selection_update_id = g_idle_add ((GSourceFunc)selection_update_idle_cb, view); + + return TRUE; + case 2: + if (!view->drag_info.in_drag) { + gboolean start; + int i; + + start = gtk_drag_check_threshold (widget, + view->drag_info.start.x, + view->drag_info.start.y, + event->x_root, + event->y_root); + view->drag_info.in_drag = start; + view->drag_info.drag_timeout_id = g_timeout_add (10, + (GSourceFunc)ev_view_drag_update_momentum, view); + /* Set 100 to choose how long it takes to build up momentum */ + /* Clear out previous momentum info: */ + for (i = 0; i < DRAG_HISTORY; i++) { + view->drag_info.buffer[i].x = event->x; + view->drag_info.buffer[i].y = event->y; + } + view->drag_info.momentum.x = 0; + view->drag_info.momentum.y = 0; + } + + if (view->drag_info.in_drag) { + int dx, dy; + gdouble dhadj_value, dvadj_value; + GtkAllocation allocation; + + view->drag_info.buffer[0].x = event->x; + view->drag_info.buffer[0].y = event->y; + + dx = event->x_root - view->drag_info.start.x; + dy = event->y_root - view->drag_info.start.y; + + gtk_widget_get_allocation (widget, &allocation); + + dhadj_value = gtk_adjustment_get_page_size (view->hadjustment) * + (gdouble)dx / allocation.width; + dvadj_value = gtk_adjustment_get_page_size (view->vadjustment) * + (gdouble)dy / allocation.height; + + /* clamp scrolling to visible area */ + gtk_adjustment_set_value (view->hadjustment, + MIN (view->drag_info.hadj - dhadj_value, + gtk_adjustment_get_upper (view->hadjustment) - + gtk_adjustment_get_page_size (view->hadjustment))); + gtk_adjustment_set_value (view->vadjustment, + MIN (view->drag_info.vadj - dvadj_value, + gtk_adjustment_get_upper (view->vadjustment) - + gtk_adjustment_get_page_size (view->vadjustment))); + + return TRUE; + } + + break; + default: + ev_view_handle_cursor_over_xy (view, x, y); + } + + return FALSE; +} + +static gboolean +ev_view_button_release_event (GtkWidget *widget, + GdkEventButton *event) +{ + EvView *view = EV_VIEW (widget); + EvLink *link = NULL; + + view->image_dnd_info.in_drag = FALSE; + + if (view->scroll_info.autoscrolling) { + ev_view_autoscroll_stop (view); + view->pressed_button = -1; + + return TRUE; + } + + if (view->pressed_button == 1 && event->state & GDK_CONTROL_MASK) { + view->pressed_button = -1; + return TRUE; + } + + if (view->drag_info.in_drag) { + view->drag_info.release_timeout_id = + g_timeout_add (20, + (GSourceFunc)ev_view_scroll_drag_release, view); + } + + if (view->document && !view->drag_info.in_drag && view->pressed_button != 3) { + link = ev_view_get_link_at_location (view, event->x, event->y); + } + + view->drag_info.in_drag = FALSE; + + if (view->adding_annot && view->pressed_button == 1) { + view->adding_annot = FALSE; + ev_view_handle_cursor_over_xy (view, event->x, event->y); + view->pressed_button = -1; + + ev_view_create_annotation (view, + view->adding_annot_type, + event->x + view->scroll_x, + event->y + view->scroll_y); + + return FALSE; + } + + if (view->pressed_button == 2) { + ev_view_handle_cursor_over_xy (view, event->x, event->y); + } + + view->pressed_button = -1; + + if (view->selection_scroll_id) { + g_source_remove (view->selection_scroll_id); + view->selection_scroll_id = 0; + } + if (view->selection_update_id) { + g_source_remove (view->selection_update_id); + view->selection_update_id = 0; + } + + if (view->selection_info.selections) { + clear_link_selected (view); + ev_view_update_primary_selection (view); + + if (view->selection_info.in_drag) { + clear_selection (view); + gtk_widget_queue_draw (widget); + } + + view->selection_info.in_drag = FALSE; + } else if (link) { + if (event->button == 2) { + EvLinkAction *action; + EvLinkActionType type; + + action = ev_link_get_action (link); + if (!action) + return FALSE; + + type = ev_link_action_get_action_type (action); + if (type == EV_LINK_ACTION_TYPE_GOTO_DEST) { + g_signal_emit (view, + signals[SIGNAL_EXTERNAL_LINK], + 0, action); + } + } else { + ev_view_handle_link (view, link); + } + } + + return FALSE; +} + +static gboolean +ev_view_key_press_event (GtkWidget *widget, + GdkEventKey *event) +{ + EvView *view = EV_VIEW (widget); + + if (!view->document) + return FALSE; + + if (!gtk_widget_has_focus (widget)) { + /* Forward key events to current focused window child */ + if (view->window_child_focus) { + GdkEventKey *new_event; + gboolean handled; + + new_event = (GdkEventKey *) gdk_event_copy ((GdkEvent *)event); + g_object_unref (new_event->window); + new_event->window = gtk_widget_get_window (view->window_child_focus->window); + if (new_event->window) + g_object_ref (new_event->window); + gtk_widget_realize (view->window_child_focus->window); + handled = gtk_widget_event (view->window_child_focus->window, (GdkEvent *)new_event); + gdk_event_free ((GdkEvent *)new_event); + + return handled; + } + + return FALSE; + } + + return gtk_bindings_activate_event (GTK_OBJECT (widget), event); +} + +static gint +ev_view_focus_in (GtkWidget *widget, + GdkEventFocus *event) +{ + if (EV_VIEW (widget)->pixbuf_cache) + ev_pixbuf_cache_style_changed (EV_VIEW (widget)->pixbuf_cache); + gtk_widget_queue_draw (widget); + + return FALSE; +} + +static gint +ev_view_focus_out (GtkWidget *widget, + GdkEventFocus *event) +{ + if (EV_VIEW (widget)->pixbuf_cache) + ev_pixbuf_cache_style_changed (EV_VIEW (widget)->pixbuf_cache); + gtk_widget_queue_draw (widget); + + return FALSE; +} + +static gboolean +ev_view_leave_notify_event (GtkWidget *widget, GdkEventCrossing *event) +{ + EvView *view = EV_VIEW (widget); + + if (view->cursor != EV_VIEW_CURSOR_NORMAL) + ev_view_set_cursor (view, EV_VIEW_CURSOR_NORMAL); + + return FALSE; +} + +static gboolean +ev_view_enter_notify_event (GtkWidget *widget, GdkEventCrossing *event) +{ + EvView *view = EV_VIEW (widget); + + ev_view_handle_cursor_over_xy (view, event->x, event->y); + + return FALSE; +} + +static void +ev_view_style_set (GtkWidget *widget, + GtkStyle *old_style) +{ + if (EV_VIEW (widget)->pixbuf_cache) + ev_pixbuf_cache_style_changed (EV_VIEW (widget)->pixbuf_cache); + + GTK_WIDGET_CLASS (ev_view_parent_class)->style_set (widget, old_style); +} + +/*** Drawing ***/ + +static void +draw_rubberband (EvView *view, + GdkWindow *window, + const GdkRectangle *rect, + gdouble alpha) +{ + GtkStyle *style; + GdkColor *fill_color_gdk; + gdouble r, g, b; + cairo_t *cr; + + style = gtk_widget_get_style (GTK_WIDGET (view)); + fill_color_gdk = gdk_color_copy (&style->base[GTK_STATE_SELECTED]); + r = fill_color_gdk->red / 65535.; + g = fill_color_gdk->green / 65535.; + b = fill_color_gdk->blue / 65535.; + + cr = gdk_cairo_create (window); + + cairo_set_source_rgba (cr, r, g, b, alpha); + cairo_rectangle (cr, + rect->x - view->scroll_x, + rect->y - view->scroll_y, + rect->width, rect->height); + cairo_fill_preserve (cr); + + cairo_set_line_width (cr, 0.5); + cairo_set_source_rgb (cr, r, g, b); + cairo_stroke (cr); + + cairo_destroy (cr); + + gdk_color_free (fill_color_gdk); +} + + +static void +highlight_find_results (EvView *view, int page) +{ + gint i, n_results = 0; + GdkWindow *bin_window; + + bin_window = gtk_layout_get_bin_window (GTK_LAYOUT (view)); + + n_results = ev_view_find_get_n_results (view, page); + + for (i = 0; i < n_results; i++) { + EvRectangle *rectangle; + GdkRectangle view_rectangle; + gdouble alpha; + + if (i == view->find_result && page == view->current_page) { + alpha = 0.6; + } else { + alpha = 0.3; + } + + rectangle = ev_view_find_get_result (view, page, i); + doc_rect_to_view_rect (view, page, rectangle, &view_rectangle); + draw_rubberband (view, bin_window, &view_rectangle, alpha); + } +} + +static void +highlight_forward_search_results (EvView *view, int page) +{ + GdkWindow *bin_window; + GdkRectangle rect; + cairo_t *cr; + EvMapping *mapping = view->synctex_result; + + if (GPOINTER_TO_INT (mapping->data) != page) + return; + + bin_window = gtk_layout_get_bin_window (GTK_LAYOUT (view)); + doc_rect_to_view_rect (view, page, &mapping->area, &rect); + + cr = gdk_cairo_create (bin_window); + cairo_set_source_rgb (cr, 1., 0., 0.); + cairo_rectangle (cr, + rect.x - view->scroll_x, + rect.y - view->scroll_y, + rect.width, rect.height); + cairo_stroke (cr); + cairo_destroy (cr); +} + +static void +focus_annotation (EvView *view, + gint page, + GdkRectangle *clip) +{ + GtkWidget *widget = GTK_WIDGET (view); + GdkRectangle rect; + EvMapping *mapping = view->focus_annotation; + EvAnnotation *annot = (EvAnnotation *)mapping->data; + + if (ev_annotation_get_page_index (annot) != page) + return; + + doc_rect_to_view_rect (view, page, &mapping->area, &rect); + gtk_paint_focus (gtk_widget_get_style (widget), + gtk_layout_get_bin_window (GTK_LAYOUT (view)), + gtk_widget_get_state (widget), + NULL, widget, NULL, + rect.x - view->scroll_x, + rect.y - view->scroll_y, + rect.width + 1, rect.height + 1); +} + +static void +ev_view_loading_window_move (EvView *view) +{ + GtkWidget *widget = GTK_WIDGET (view); + EvLoadingWindow *window = EV_LOADING_WINDOW (view->loading_window); + gint root_x, root_y; + gint window_width; + GtkAllocation allocation; + + gtk_widget_get_allocation (widget, &allocation); + gdk_window_get_origin (gtk_widget_get_window (widget), &root_x, &root_y); + ev_loading_window_get_size (window, &window_width, NULL); + + root_x += allocation.width - window_width - 10; + root_y += 10; + + ev_loading_window_move (window, root_x, root_y); +} + +static gboolean +show_loading_window_cb (EvView *view) +{ + if (!view->loading_window) { + GtkWindow *parent; + GdkScreen *screen; + + parent = GTK_WINDOW (gtk_widget_get_toplevel (GTK_WIDGET (view))); + view->loading_window = ev_loading_window_new (parent); + + /* Show the window off screen to get a valid size asap */ + screen = gtk_widget_get_screen (GTK_WIDGET (view)); + gtk_window_move (GTK_WINDOW (view->loading_window), + gdk_screen_get_width (screen) + 1, + gdk_screen_get_height (screen) + 1); + gtk_widget_show (view->loading_window); + } + + ev_view_loading_window_move (view); + + gtk_widget_show (view->loading_window); + + view->loading_timeout = 0; + + return FALSE; +} + +static void +show_loading_window (EvView *view) +{ + if (view->loading_window && gtk_widget_get_visible (view->loading_window)) { + ev_view_loading_window_move (view); + return; + } + + if (!view->loading_timeout) { + view->loading_timeout = + g_timeout_add_full (G_PRIORITY_LOW, + 0.5, (GSourceFunc)show_loading_window_cb, + view, NULL); + } +} + +static void +hide_loading_window (EvView *view) +{ + if (view->loading_timeout) { + g_source_remove (view->loading_timeout); + view->loading_timeout = 0; + } + + if (view->loading_window && gtk_widget_get_visible (view->loading_window)) { + gtk_widget_hide (view->loading_window); + } +} + +static void +draw_one_page (EvView *view, + gint page, + cairo_t *cr, + GdkRectangle *page_area, + GtkBorder *border, + GdkRectangle *expose_area, + gboolean *page_ready) +{ + GdkWindow *bin_window; + GdkRectangle overlap; + GdkRectangle real_page_area; + gint current_page; + gboolean inverted_colors; + + g_assert (view->document); + + if (! gdk_rectangle_intersect (page_area, expose_area, &overlap)) + return; + + /* Render the document itself */ + real_page_area = *page_area; + + real_page_area.x += border->left; + real_page_area.y += border->top; + real_page_area.width -= (border->left + border->right); + real_page_area.height -= (border->top + border->bottom); + *page_ready = TRUE; + + bin_window = gtk_layout_get_bin_window (GTK_LAYOUT (view)); + current_page = ev_document_model_get_page (view->model); + inverted_colors = ev_document_model_get_inverted_colors (view->model); + ev_document_misc_paint_one_page (cr, + GTK_WIDGET (view), + page_area, border, + page == current_page, + inverted_colors); + + if (gdk_rectangle_intersect (&real_page_area, expose_area, &overlap)) { + gint width, height; + gint page_width, page_height; + cairo_surface_t *page_surface = NULL; + gint selection_width, selection_height; + cairo_surface_t *selection_surface = NULL; + + page_surface = ev_pixbuf_cache_get_surface (view->pixbuf_cache, page); + + if (!page_surface) { + if (page == current_page) + show_loading_window (view); + + *page_ready = FALSE; + + return; + } + + if (page == current_page) + hide_loading_window (view); + + ev_view_get_page_size (view, page, &width, &height); + + page_width = cairo_image_surface_get_width (page_surface); + page_height = cairo_image_surface_get_height (page_surface); + + cairo_save (cr); + cairo_translate (cr, overlap.x, overlap.y); + + if (width != page_width || height != page_height) { + cairo_pattern_set_filter (cairo_get_source (cr), + CAIRO_FILTER_FAST); + cairo_scale (cr, + (gdouble)width / page_width, + (gdouble)height / page_height); + } + + cairo_surface_set_device_offset (page_surface, + overlap.x - real_page_area.x, + overlap.y - real_page_area.y); + cairo_set_source_surface (cr, page_surface, 0, 0); + cairo_paint (cr); + cairo_restore (cr); + + /* Get the selection pixbuf iff we have something to draw */ + if (find_selection_for_page (view, page) && + view->selection_mode == EV_VIEW_SELECTION_TEXT) { + selection_surface = + ev_pixbuf_cache_get_selection_surface (view->pixbuf_cache, + page, + view->scale, + NULL); + } + + if (!selection_surface) { + return; + } + + selection_width = cairo_image_surface_get_width (selection_surface); + selection_height = cairo_image_surface_get_height (selection_surface); + + cairo_save (cr); + cairo_translate (cr, overlap.x, overlap.y); + + if (width != selection_width || height != selection_height) { + cairo_pattern_set_filter (cairo_get_source (cr), + CAIRO_FILTER_FAST); + cairo_scale (cr, + (gdouble)width / selection_width, + (gdouble)height / selection_height); + } + + cairo_surface_set_device_offset (selection_surface, + overlap.x - real_page_area.x, + overlap.y - real_page_area.y); + + cairo_set_source_surface (cr, selection_surface, 0, 0); + cairo_paint (cr); + cairo_restore (cr); + } +} + +/*** GObject functions ***/ + +static void +ev_view_finalize (GObject *object) +{ + EvView *view = EV_VIEW (object); + + clear_selection (view); + clear_link_selected (view); + + if (view->synctex_result) { + g_free (view->synctex_result); + view->synctex_result = NULL; + } + + if (view->image_dnd_info.image) + g_object_unref (view->image_dnd_info.image); + view->image_dnd_info.image = NULL; + + G_OBJECT_CLASS (ev_view_parent_class)->finalize (object); +} + +static void +ev_view_destroy (GtkObject *object) +{ + EvView *view = EV_VIEW (object); + + if (view->model) { + g_object_unref (view->model); + view->model = NULL; + } + + if (view->pixbuf_cache) { + g_object_unref (view->pixbuf_cache); + view->pixbuf_cache = NULL; + } + + if (view->document) { + g_object_unref (view->document); + view->document = NULL; + } + + if (view->page_cache) { + g_object_unref (view->page_cache); + view->page_cache = NULL; + } + + ev_view_window_children_free (view); + + if (view->selection_scroll_id) { + g_source_remove (view->selection_scroll_id); + view->selection_scroll_id = 0; + } + + if (view->selection_update_id) { + g_source_remove (view->selection_update_id); + view->selection_update_id = 0; + } + + if (view->scroll_info.timeout_id) { + g_source_remove (view->scroll_info.timeout_id); + view->scroll_info.timeout_id = 0; + } + + if (view->drag_info.drag_timeout_id) { + g_source_remove (view->drag_info.drag_timeout_id); + view->drag_info.drag_timeout_id = 0; + } + + if (view->drag_info.release_timeout_id) { + g_source_remove (view->drag_info.release_timeout_id); + view->drag_info.release_timeout_id = 0; + } + + if (view->loading_timeout) { + g_source_remove (view->loading_timeout); + view->loading_timeout = 0; + } + + ev_view_set_scroll_adjustments (GTK_LAYOUT (view), NULL, NULL); + + GTK_OBJECT_CLASS (ev_view_parent_class)->destroy (object); +} + +static AtkObject * +ev_view_get_accessible (GtkWidget *widget) +{ + static gboolean first_time = TRUE; + + if (first_time) { + AtkObjectFactory *factory; + AtkRegistry *registry; + GType derived_type; + GType derived_atk_type; + + /* + * Figure out whether accessibility is enabled by looking at the + * type of the accessible object which would be created for + * the parent type of EvView. + */ + derived_type = g_type_parent (EV_TYPE_VIEW); + + registry = atk_get_default_registry (); + factory = atk_registry_get_factory (registry, + derived_type); + derived_atk_type = atk_object_factory_get_accessible_type (factory); + if (g_type_is_a (derived_atk_type, GTK_TYPE_ACCESSIBLE)) { + atk_registry_set_factory_type (registry, + EV_TYPE_VIEW, + ev_view_accessible_factory_get_type ()); + EV_VIEW (widget)->a11y_enabled = TRUE; + } + first_time = FALSE; + } + return GTK_WIDGET_CLASS (ev_view_parent_class)->get_accessible (widget); +} + +static void +ev_view_class_init (EvViewClass *class) +{ + GObjectClass *object_class = G_OBJECT_CLASS (class); + GtkObjectClass *gtk_object_class = GTK_OBJECT_CLASS (class); + GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (class); + GtkLayoutClass *layout_class = GTK_LAYOUT_CLASS (class); + GtkBindingSet *binding_set; + + object_class->finalize = ev_view_finalize; + + widget_class->expose_event = ev_view_expose_event; + widget_class->button_press_event = ev_view_button_press_event; + widget_class->motion_notify_event = ev_view_motion_notify_event; + widget_class->button_release_event = ev_view_button_release_event; + widget_class->key_press_event = ev_view_key_press_event; + widget_class->focus_in_event = ev_view_focus_in; + widget_class->focus_out_event = ev_view_focus_out; + widget_class->get_accessible = ev_view_get_accessible; + widget_class->size_request = ev_view_size_request; + widget_class->size_allocate = ev_view_size_allocate; + widget_class->realize = ev_view_realize; + widget_class->scroll_event = ev_view_scroll_event; + widget_class->enter_notify_event = ev_view_enter_notify_event; + widget_class->leave_notify_event = ev_view_leave_notify_event; + widget_class->style_set = ev_view_style_set; + widget_class->drag_data_get = ev_view_drag_data_get; + widget_class->drag_motion = ev_view_drag_motion; + widget_class->popup_menu = ev_view_popup_menu; + widget_class->query_tooltip = ev_view_query_tooltip; + + gtk_object_class->destroy = ev_view_destroy; + + layout_class->set_scroll_adjustments = ev_view_set_scroll_adjustments; + + class->binding_activated = ev_view_scroll; + + signals[SIGNAL_BINDING_ACTIVATED] = g_signal_new ("binding_activated", + G_TYPE_FROM_CLASS (object_class), + G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION, + G_STRUCT_OFFSET (EvViewClass, binding_activated), + NULL, NULL, + ev_view_marshal_VOID__ENUM_BOOLEAN, + G_TYPE_NONE, 2, + GTK_TYPE_SCROLL_TYPE, + G_TYPE_BOOLEAN); + signals[SIGNAL_HANDLE_LINK] = g_signal_new ("handle-link", + G_TYPE_FROM_CLASS (object_class), + G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION, + G_STRUCT_OFFSET (EvViewClass, handle_link), + NULL, NULL, + g_cclosure_marshal_VOID__OBJECT, + G_TYPE_NONE, 1, + G_TYPE_OBJECT); + signals[SIGNAL_EXTERNAL_LINK] = g_signal_new ("external-link", + G_TYPE_FROM_CLASS (object_class), + G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION, + G_STRUCT_OFFSET (EvViewClass, external_link), + NULL, NULL, + g_cclosure_marshal_VOID__OBJECT, + G_TYPE_NONE, 1, + G_TYPE_OBJECT); + signals[SIGNAL_POPUP_MENU] = g_signal_new ("popup", + G_TYPE_FROM_CLASS (object_class), + G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION, + G_STRUCT_OFFSET (EvViewClass, popup_menu), + NULL, NULL, + g_cclosure_marshal_VOID__POINTER, + G_TYPE_NONE, 1, + G_TYPE_POINTER); + signals[SIGNAL_SELECTION_CHANGED] = g_signal_new ("selection-changed", + G_TYPE_FROM_CLASS (object_class), + G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION, + G_STRUCT_OFFSET (EvViewClass, selection_changed), + NULL, NULL, + g_cclosure_marshal_VOID__VOID, + G_TYPE_NONE, 0, + G_TYPE_NONE); + signals[SIGNAL_SYNC_SOURCE] = g_signal_new ("sync-source", + G_TYPE_FROM_CLASS (object_class), + G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION, + G_STRUCT_OFFSET (EvViewClass, sync_source), + NULL, NULL, + g_cclosure_marshal_VOID__POINTER, + G_TYPE_NONE, 1, + G_TYPE_POINTER); + signals[SIGNAL_ANNOT_ADDED] = g_signal_new ("annot-added", + G_TYPE_FROM_CLASS (object_class), + G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION, + G_STRUCT_OFFSET (EvViewClass, annot_added), + NULL, NULL, + g_cclosure_marshal_VOID__OBJECT, + G_TYPE_NONE, 1, + EV_TYPE_ANNOTATION); + + binding_set = gtk_binding_set_by_class (class); + + add_scroll_binding_keypad (binding_set, GDK_Left, 0, GTK_SCROLL_STEP_BACKWARD, TRUE); + add_scroll_binding_keypad (binding_set, GDK_Right, 0, GTK_SCROLL_STEP_FORWARD, TRUE); + add_scroll_binding_keypad (binding_set, GDK_Left, GDK_MOD1_MASK, GTK_SCROLL_STEP_DOWN, TRUE); + add_scroll_binding_keypad (binding_set, GDK_Right, GDK_MOD1_MASK, GTK_SCROLL_STEP_UP, TRUE); + add_scroll_binding_keypad (binding_set, GDK_Up, 0, GTK_SCROLL_STEP_BACKWARD, FALSE); + add_scroll_binding_keypad (binding_set, GDK_Down, 0, GTK_SCROLL_STEP_FORWARD, FALSE); + add_scroll_binding_keypad (binding_set, GDK_Up, GDK_MOD1_MASK, GTK_SCROLL_STEP_DOWN, FALSE); + add_scroll_binding_keypad (binding_set, GDK_Down, GDK_MOD1_MASK, GTK_SCROLL_STEP_UP, FALSE); + gtk_binding_entry_add_signal (binding_set, GDK_H, 0, "binding_activated", 2, GTK_TYPE_SCROLL_TYPE, + GTK_SCROLL_STEP_BACKWARD, G_TYPE_BOOLEAN, TRUE); + gtk_binding_entry_add_signal (binding_set, GDK_J, 0, "binding_activated", 2, GTK_TYPE_SCROLL_TYPE, + GTK_SCROLL_STEP_FORWARD, G_TYPE_BOOLEAN, FALSE); + gtk_binding_entry_add_signal (binding_set, GDK_K, 0, "binding_activated", 2, GTK_TYPE_SCROLL_TYPE, + GTK_SCROLL_STEP_BACKWARD, G_TYPE_BOOLEAN, FALSE); + gtk_binding_entry_add_signal (binding_set, GDK_L, 0, "binding_activated", 2, GTK_TYPE_SCROLL_TYPE, + GTK_SCROLL_STEP_FORWARD, G_TYPE_BOOLEAN, TRUE); + +} + +static void +ev_view_init (EvView *view) +{ + gtk_widget_set_can_focus (GTK_WIDGET (view), TRUE); + + view->start_page = -1; + view->end_page = -1; + view->spacing = 5; + view->scale = 1.0; + view->current_page = 0; + view->pressed_button = -1; + view->cursor = EV_VIEW_CURSOR_NORMAL; + view->drag_info.in_drag = FALSE; + view->scroll_info.autoscrolling = FALSE; + view->selection_info.selections = NULL; + view->selection_info.in_selection = FALSE; + view->selection_info.in_drag = FALSE; + view->selection_mode = EV_VIEW_SELECTION_TEXT; + view->continuous = TRUE; + view->dual_page = FALSE; + view->fullscreen = FALSE; + view->sizing_mode = EV_SIZING_FIT_WIDTH; + view->pending_scroll = SCROLL_TO_KEEP_POSITION; + view->jump_to_find_result = TRUE; + view->highlight_find_results = FALSE; + + gtk_layout_set_hadjustment (GTK_LAYOUT (view), NULL); + gtk_layout_set_vadjustment (GTK_LAYOUT (view), NULL); +} + +/*** Callbacks ***/ + +static void +ev_view_change_page (EvView *view, + gint new_page) +{ + gint x, y; + + view->current_page = new_page; + view->pending_scroll = SCROLL_TO_PAGE_POSITION; + + hide_loading_window (view); + + gtk_widget_get_pointer (GTK_WIDGET (view), &x, &y); + ev_view_handle_cursor_over_xy (view, x, y); + + gtk_widget_queue_resize (GTK_WIDGET (view)); +} + +static void +job_finished_cb (EvPixbufCache *pixbuf_cache, + cairo_region_t *region, + EvView *view) +{ + if (region) { + GdkWindow *bin_window; + + bin_window = gtk_layout_get_bin_window (GTK_LAYOUT (view)); +#if GTK_CHECK_VERSION(2, 90, 5) + gdk_window_invalidate_region (bin_window, region, TRUE); +#else + { + GdkRegion *gdk_region = gdk_region_new (); + guint n_recs = cairo_region_num_rectangles (region); + guint i; + + for (i = 0; i < n_recs; i++) { + cairo_rectangle_int_t rect; + + cairo_region_get_rectangle (region, i, &rect); + gdk_region_union_with_rect (gdk_region, (GdkRectangle *)&rect); + } + gdk_window_invalidate_region (bin_window, gdk_region, TRUE); + gdk_region_destroy (gdk_region); + } +#endif + } else { + gtk_widget_queue_draw (GTK_WIDGET (view)); + } +} + +static void +ev_view_page_changed_cb (EvDocumentModel *model, + gint old_page, + gint new_page, + EvView *view) +{ + if (!view->document) + return; + + if (view->current_page != new_page) { + ev_view_change_page (view, new_page); + } else { + gtk_widget_queue_draw (GTK_WIDGET (view)); + } + + view->find_result = 0; +} + +static void +on_adjustment_value_changed (GtkAdjustment *adjustment, + EvView *view) +{ + int dx = 0, dy = 0; + gint x, y; + gint value; + GList *children, *l; + + if (!gtk_widget_get_realized (GTK_WIDGET (view))) + return; + + if (view->hadjustment) { + value = (gint) gtk_adjustment_get_value (view->hadjustment); + dx = view->scroll_x - value; + view->scroll_x = value; + } else { + view->scroll_x = 0; + } + + if (view->vadjustment) { + value = (gint) gtk_adjustment_get_value (view->vadjustment); + dy = view->scroll_y - value; + view->scroll_y = value; + } else { + view->scroll_y = 0; + } + + children = gtk_container_get_children (GTK_CONTAINER (view)); + for (l = children; l && l->data; l = g_list_next (l)) { + gint child_x, child_y; + GtkWidget *child = (GtkWidget *)l->data; + + gtk_container_child_get (GTK_CONTAINER (view), + child, + "x", &child_x, + "y", &child_y, + NULL); + gtk_layout_move (GTK_LAYOUT (view), child, child_x + dx, child_y + dy); + } + g_list_free (children); + + for (l = view->window_children; l && l->data; l = g_list_next (l)) { + EvViewWindowChild *child; + + child = (EvViewWindowChild *)l->data; + + ev_view_window_child_move (view, child, child->x + dx, child->y + dy); + } + + if (view->pending_resize) { + gtk_widget_queue_draw (GTK_WIDGET (view)); + } else { + GdkWindow *bin_window; + + bin_window = gtk_layout_get_bin_window (GTK_LAYOUT (view)); + gdk_window_scroll (bin_window, dx, dy); + } + + gtk_widget_get_pointer (GTK_WIDGET (view), &x, &y); + ev_view_handle_cursor_over_xy (view, x, y); + + if (view->document) + view_update_range_and_current_page (view); +} + +GtkWidget* +ev_view_new (void) +{ + GtkWidget *view; + + view = g_object_new (EV_TYPE_VIEW, NULL); + + return view; +} + +static void +setup_caches (EvView *view) +{ + gboolean inverted_colors; + + view->height_to_page_cache = ev_view_get_height_to_page_cache (view); + view->pixbuf_cache = ev_pixbuf_cache_new (GTK_WIDGET (view), view->model, view->pixbuf_cache_size); + view->page_cache = ev_page_cache_new (view->document); + if (view->a11y_enabled) { + EvJobPageDataFlags flags = ev_page_cache_get_flags (view->page_cache); + + ev_page_cache_set_flags (view->page_cache, + flags | + EV_PAGE_DATA_INCLUDE_TEXT_LAYOUT | + EV_PAGE_DATA_INCLUDE_TEXT); + } + inverted_colors = ev_document_model_get_inverted_colors (view->model); + ev_pixbuf_cache_set_inverted_colors (view->pixbuf_cache, inverted_colors); + g_signal_connect (view->pixbuf_cache, "job-finished", G_CALLBACK (job_finished_cb), view); +} + +static void +clear_caches (EvView *view) +{ + if (view->pixbuf_cache) { + g_object_unref (view->pixbuf_cache); + view->pixbuf_cache = NULL; + } + + if (view->page_cache) { + g_object_unref (view->page_cache); + view->page_cache = NULL; + } +} + +/** + * ev_view_set_page_cache_size: + * @view: + * @cache_size: + * + * Sets the maximum size in bytes that will be used to cache + * rendered pages. Use 0 to disable caching rendered pages. + * + * Note that this limit doesn't affect the current visible page range, + * which will always be rendered. In order to limit the total memory used + * you have to use ev_document_model_set_max_scale() too. + * + */ +void +ev_view_set_page_cache_size (EvView *view, + gsize cache_size) +{ + if (view->pixbuf_cache_size == cache_size) + return; + + view->pixbuf_cache_size = cache_size; + if (view->pixbuf_cache) + ev_pixbuf_cache_set_max_size (view->pixbuf_cache, cache_size); +} + +void +ev_view_set_loading (EvView *view, + gboolean loading) +{ + if (view->loading && !loading) + hide_loading_window (view); + view->loading = loading; + gtk_widget_queue_draw (GTK_WIDGET (view)); +} + +static gboolean +ev_view_autoscroll_cb (EvView *view) +{ + gdouble speed, value; + + /* If the user stops autoscrolling, autoscrolling will be + * set to false but the timeout will continue; stop the timeout: */ + if (!view->scroll_info.autoscrolling) { + view->scroll_info.timeout_id = 0; + return FALSE; + } + + if (view->scroll_info.last_y > view->scroll_info.start_y && + (view->scroll_info.last_y < view->scroll_info.start_y)) + return TRUE; + + /* Replace 100 with your speed of choice: The lower the faster. + * Replace 3 with another speed of choice: The higher, the faster it accelerated + * based on the distance of the starting point from the mouse + * (All also effected by the timeout interval of this callback) */ + + if (view->scroll_info.start_y > view->scroll_info.last_y) + speed = -pow ((((gdouble)view->scroll_info.start_y - view->scroll_info.last_y) / 100), 3); + else + speed = pow ((((gdouble)view->scroll_info.last_y - view->scroll_info.start_y) / 100), 3); + + value = gtk_adjustment_get_value (view->vadjustment); + value = CLAMP (value + speed, 0, + gtk_adjustment_get_upper (view->vadjustment) - + gtk_adjustment_get_page_size (view->vadjustment)); + gtk_adjustment_set_value (view->vadjustment, value); + + return TRUE; + +} + +void +ev_view_autoscroll_start (EvView *view) +{ + gint x, y; + + g_return_if_fail (EV_IS_VIEW (view)); + + if (view->scroll_info.autoscrolling) + return; + + view->scroll_info.autoscrolling = TRUE; + view->scroll_info.timeout_id = + g_timeout_add (20, (GSourceFunc)ev_view_autoscroll_cb, + view); + + gtk_widget_get_pointer (GTK_WIDGET (view), &x, &y); + ev_view_handle_cursor_over_xy (view, x, y); +} + +void +ev_view_autoscroll_stop (EvView *view) +{ + gint x, y; + + g_return_if_fail (EV_IS_VIEW (view)); + + if (!view->scroll_info.autoscrolling) + return; + + view->scroll_info.autoscrolling = FALSE; + if (view->scroll_info.timeout_id) { + g_source_remove (view->scroll_info.timeout_id); + view->scroll_info.timeout_id = 0; + } + + gtk_widget_get_pointer (GTK_WIDGET (view), &x, &y); + ev_view_handle_cursor_over_xy (view, x, y); +} + +static void +ev_view_document_changed_cb (EvDocumentModel *model, + GParamSpec *pspec, + EvView *view) +{ + EvDocument *document = ev_document_model_get_document (model); + + if (document != view->document) { + gint current_page; + + clear_caches (view); + + if (view->document) { + g_object_unref (view->document); + } + + view->document = document; + view->find_result = 0; + + if (view->document) { + view->loading = FALSE; + g_object_ref (view->document); + setup_caches (view); + } + + current_page = ev_document_model_get_page (model); + if (view->current_page != current_page) { + ev_view_change_page (view, current_page); + } else { + view->pending_scroll = SCROLL_TO_KEEP_POSITION; + gtk_widget_queue_resize (GTK_WIDGET (view)); + } + } +} + +static void +ev_view_rotation_changed_cb (EvDocumentModel *model, + GParamSpec *pspec, + EvView *view) +{ + gint rotation = ev_document_model_get_rotation (model); + + view->rotation = rotation; + + if (view->pixbuf_cache) { + ev_pixbuf_cache_clear (view->pixbuf_cache); + if (!ev_document_is_page_size_uniform (view->document)) + view->pending_scroll = SCROLL_TO_PAGE_POSITION; + gtk_widget_queue_resize (GTK_WIDGET (view)); + } + + ev_view_remove_all (view); + + if (rotation != 0) + clear_selection (view); +} + +static void +ev_view_inverted_colors_changed_cb (EvDocumentModel *model, + GParamSpec *pspec, + EvView *view) +{ + if (view->pixbuf_cache) { + gboolean inverted_colors; + + inverted_colors = ev_document_model_get_inverted_colors (model); + ev_pixbuf_cache_set_inverted_colors (view->pixbuf_cache, inverted_colors); + gtk_widget_queue_draw (GTK_WIDGET (view)); + } +} + +static void +ev_view_sizing_mode_changed_cb (EvDocumentModel *model, + GParamSpec *pspec, + EvView *view) +{ + EvSizingMode mode = ev_document_model_get_sizing_mode (model); + + view->sizing_mode = mode; + if (mode != EV_SIZING_FREE) + gtk_widget_queue_resize (GTK_WIDGET (view)); +} + +#define EPSILON 0.0000001 +static void +ev_view_scale_changed_cb (EvDocumentModel *model, + GParamSpec *pspec, + EvView *view) +{ + gdouble scale = ev_document_model_get_scale (model); + + if (ABS (view->scale - scale) < EPSILON) + return; + + view->scale = scale; + + view->pending_resize = TRUE; + if (view->sizing_mode == EV_SIZING_FREE) + gtk_widget_queue_resize (GTK_WIDGET (view)); +} + +static void +ev_view_continuous_changed_cb (EvDocumentModel *model, + GParamSpec *pspec, + EvView *view) +{ + gboolean continuous = ev_document_model_get_continuous (model); + + view->continuous = continuous; + view->pending_scroll = SCROLL_TO_PAGE_POSITION; + gtk_widget_queue_resize (GTK_WIDGET (view)); +} + +static void +ev_view_dual_page_changed_cb (EvDocumentModel *model, + GParamSpec *pspec, + EvView *view) +{ + gboolean dual_page = ev_document_model_get_dual_page (model); + + view->dual_page = dual_page; + view->pending_scroll = SCROLL_TO_PAGE_POSITION; + /* FIXME: if we're keeping the pixbuf cache around, we should extend the + * preload_cache_size to be 2 if dual_page is set. + */ + gtk_widget_queue_resize (GTK_WIDGET (view)); +} + +static void +ev_view_fullscreen_changed_cb (EvDocumentModel *model, + GParamSpec *pspec, + EvView *view) +{ + gboolean fullscreen = ev_document_model_get_fullscreen (model); + + view->fullscreen = fullscreen; + gtk_widget_queue_resize (GTK_WIDGET (view)); +} + +void +ev_view_set_model (EvView *view, + EvDocumentModel *model) +{ + g_return_if_fail (EV_IS_VIEW (view)); + g_return_if_fail (EV_IS_DOCUMENT_MODEL (model)); + + if (model == view->model) + return; + + if (view->model) { + g_signal_handlers_disconnect_by_func (view->model, + ev_view_document_changed_cb, + view); + g_signal_handlers_disconnect_by_func (view->model, + ev_view_page_changed_cb, + view); + g_object_unref (view->model); + } + view->model = g_object_ref (model); + + /* Initialize view from model */ + view->rotation = ev_document_model_get_rotation (view->model); + view->sizing_mode = ev_document_model_get_sizing_mode (view->model); + view->scale = ev_document_model_get_scale (view->model); + view->continuous = ev_document_model_get_continuous (view->model); + view->dual_page = ev_document_model_get_dual_page (view->model); + view->fullscreen = ev_document_model_get_fullscreen (view->model); + ev_view_document_changed_cb (view->model, NULL, view); + + g_signal_connect (view->model, "notify::document", + G_CALLBACK (ev_view_document_changed_cb), + view); + g_signal_connect (view->model, "notify::rotation", + G_CALLBACK (ev_view_rotation_changed_cb), + view); + g_signal_connect (view->model, "notify::inverted-colors", + G_CALLBACK (ev_view_inverted_colors_changed_cb), + view); + g_signal_connect (view->model, "notify::sizing-mode", + G_CALLBACK (ev_view_sizing_mode_changed_cb), + view); + g_signal_connect (view->model, "notify::scale", + G_CALLBACK (ev_view_scale_changed_cb), + view); + g_signal_connect (view->model, "notify::continuous", + G_CALLBACK (ev_view_continuous_changed_cb), + view); + g_signal_connect (view->model, "notify::dual-page", + G_CALLBACK (ev_view_dual_page_changed_cb), + view); + g_signal_connect (view->model, "notify::fullscreen", + G_CALLBACK (ev_view_fullscreen_changed_cb), + view); + g_signal_connect (view->model, "page-changed", + G_CALLBACK (ev_view_page_changed_cb), + view); +} + +static void +ev_view_reload_page (EvView *view, + gint page, + cairo_region_t *region) +{ + ev_pixbuf_cache_reload_page (view->pixbuf_cache, + region, + page, + view->rotation, + view->scale); +} + +void +ev_view_reload (EvView *view) +{ + ev_pixbuf_cache_clear (view->pixbuf_cache); + view_update_range_and_current_page (view); +} + +/*** Zoom and sizing mode ***/ + +gboolean +ev_view_can_zoom_in (EvView *view) +{ + return view->scale * ZOOM_IN_FACTOR <= ev_document_model_get_max_scale (view->model); +} + +gboolean +ev_view_can_zoom_out (EvView *view) +{ + return view->scale * ZOOM_OUT_FACTOR >= ev_document_model_get_min_scale (view->model); +} + +void +ev_view_zoom_in (EvView *view) +{ + gdouble scale; + + g_return_if_fail (view->sizing_mode == EV_SIZING_FREE); + + view->pending_scroll = SCROLL_TO_CENTER; + scale = ev_document_model_get_scale (view->model) * ZOOM_IN_FACTOR; + ev_document_model_set_scale (view->model, scale); +} + +void +ev_view_zoom_out (EvView *view) +{ + gdouble scale; + + g_return_if_fail (view->sizing_mode == EV_SIZING_FREE); + + view->pending_scroll = SCROLL_TO_CENTER; + scale = ev_document_model_get_scale (view->model) * ZOOM_OUT_FACTOR; + ev_document_model_set_scale (view->model, scale); +} + +static double +zoom_for_size_fit_width (gdouble doc_width, + gdouble doc_height, + int target_width, + int target_height) +{ + return (double)target_width / doc_width; +} + +static double +zoom_for_size_fit_height (gdouble doc_width, + gdouble doc_height, + int target_width, + int target_height) +{ + return (double)target_height / doc_height; +} + +static double +zoom_for_size_best_fit (gdouble doc_width, + gdouble doc_height, + int target_width, + int target_height) +{ + double w_scale; + double h_scale; + + w_scale = (double)target_width / doc_width; + h_scale = (double)target_height / doc_height; + + return MIN (w_scale, h_scale); +} + +static void +ev_view_zoom_for_size_continuous_and_dual_page (EvView *view, + int width, + int height) +{ + gdouble doc_width, doc_height; + GtkBorder border; + gdouble scale; + gint sb_size; + + ev_document_get_max_page_size (view->document, &doc_width, &doc_height); + if (view->rotation == 90 || view->rotation == 270) { + gdouble tmp; + + tmp = doc_width; + doc_width = doc_height; + doc_height = tmp; + } + + compute_border (view, doc_width, doc_height, &border); + + doc_width *= 2; + width -= (2 * (border.left + border.right) + 3 * view->spacing); + height -= (border.top + border.bottom + 2 * view->spacing - 1); + + sb_size = ev_view_get_scrollbar_size (view, GTK_ORIENTATION_VERTICAL); + + if (view->sizing_mode == EV_SIZING_FIT_WIDTH) { + scale = zoom_for_size_fit_width (doc_width, doc_height, width - sb_size, height); + } else if (view->sizing_mode == EV_SIZING_BEST_FIT) + scale = zoom_for_size_best_fit (doc_width, doc_height, width - sb_size, height); + else + g_assert_not_reached (); + + ev_document_model_set_scale (view->model, scale); +} + +static void +ev_view_zoom_for_size_continuous (EvView *view, + int width, + int height) +{ + gdouble doc_width, doc_height; + GtkBorder border; + gdouble scale; + gint sb_size; + + ev_document_get_max_page_size (view->document, &doc_width, &doc_height); + if (view->rotation == 90 || view->rotation == 270) { + gdouble tmp; + + tmp = doc_width; + doc_width = doc_height; + doc_height = tmp; + } + + compute_border (view, doc_width, doc_height, &border); + + width -= (border.left + border.right + 2 * view->spacing); + height -= (border.top + border.bottom + 2 * view->spacing - 1); + + sb_size = ev_view_get_scrollbar_size (view, GTK_ORIENTATION_VERTICAL); + + if (view->sizing_mode == EV_SIZING_FIT_WIDTH) { + scale = zoom_for_size_fit_width (doc_width, doc_height, width - sb_size, height); + } else if (view->sizing_mode == EV_SIZING_BEST_FIT) + scale = zoom_for_size_best_fit (doc_width, doc_height, width - sb_size, height); + else + g_assert_not_reached (); + + ev_document_model_set_scale (view->model, scale); +} + +static void +ev_view_zoom_for_size_dual_page (EvView *view, + int width, + int height) +{ + GtkBorder border; + gdouble doc_width, doc_height; + gdouble scale; + gint other_page; + + other_page = view->current_page ^ 1; + + /* Find the largest of the two. */ + get_doc_page_size (view, view->current_page, &doc_width, &doc_height); + if (other_page < ev_document_get_n_pages (view->document)) { + gdouble width_2, height_2; + + get_doc_page_size (view, other_page, &width_2, &height_2); + if (width_2 > doc_width) + doc_width = width_2; + if (height_2 > doc_height) + doc_height = height_2; + } + compute_border (view, width, height, &border); + + doc_width = doc_width * 2; + width -= ((border.left + border.right)* 2 + 3 * view->spacing); + height -= (border.top + border.bottom + 2 * view->spacing); + + if (view->sizing_mode == EV_SIZING_FIT_WIDTH) { + gint sb_size; + + sb_size = ev_view_get_scrollbar_size (view, GTK_ORIENTATION_VERTICAL); + scale = zoom_for_size_fit_width (doc_width, doc_height, width - sb_size, height); + } else if (view->sizing_mode == EV_SIZING_BEST_FIT) + scale = zoom_for_size_best_fit (doc_width, doc_height, width, height); + else + g_assert_not_reached (); + + ev_document_model_set_scale (view->model, scale); +} + +static void +ev_view_zoom_for_size_single_page (EvView *view, + int width, + int height) +{ + gdouble doc_width, doc_height; + GtkBorder border; + gdouble scale; + + get_doc_page_size (view, view->current_page, &doc_width, &doc_height); + + /* Get an approximate border */ + compute_border (view, width, height, &border); + + width -= (border.left + border.right + 2 * view->spacing); + height -= (border.top + border.bottom + 2 * view->spacing); + + if (view->sizing_mode == EV_SIZING_FIT_WIDTH) { + gint sb_size; + + sb_size = ev_view_get_scrollbar_size (view, GTK_ORIENTATION_VERTICAL); + scale = zoom_for_size_fit_width (doc_width, doc_height, width - sb_size, height); + } else if (view->sizing_mode == EV_SIZING_BEST_FIT) + scale = zoom_for_size_best_fit (doc_width, doc_height, width, height); + else + g_assert_not_reached (); + + ev_document_model_set_scale (view->model, scale); +} + +static void +ev_view_zoom_for_size (EvView *view, + int width, + int height) +{ + g_return_if_fail (EV_IS_VIEW (view)); + g_return_if_fail (view->sizing_mode == EV_SIZING_FIT_WIDTH || + view->sizing_mode == EV_SIZING_BEST_FIT); + g_return_if_fail (width >= 0); + g_return_if_fail (height >= 0); + + if (view->document == NULL) + return; + + if (view->continuous && view->dual_page) + ev_view_zoom_for_size_continuous_and_dual_page (view, width, height); + else if (view->continuous) + ev_view_zoom_for_size_continuous (view, width, height); + else if (view->dual_page) + ev_view_zoom_for_size_dual_page (view, width, height); + else + ev_view_zoom_for_size_single_page (view, width, height); +} + +/*** Find ***/ +static gint +ev_view_find_get_n_results (EvView *view, gint page) +{ + return view->find_pages ? g_list_length (view->find_pages[page]) : 0; +} + +static EvRectangle * +ev_view_find_get_result (EvView *view, gint page, gint result) +{ + return view->find_pages ? (EvRectangle *) g_list_nth_data (view->find_pages[page], result) : NULL; +} + +static void +jump_to_find_result (EvView *view) +{ + gint n_results; + gint page = view->current_page; + + n_results = ev_view_find_get_n_results (view, page); + + if (n_results > 0 && view->find_result < n_results) { + EvRectangle *rect; + GdkRectangle view_rect; + + rect = ev_view_find_get_result (view, page, view->find_result); + doc_rect_to_view_rect (view, page, rect, &view_rect); + ensure_rectangle_is_visible (view, &view_rect); + view->jump_to_find_result = FALSE; + } +} + +/** + * jump_to_find_page + * @view: #EvView instance + * @direction: Direction to look + * @shift: Shift from current page + * + * Jumps to the first page that has occurences of searched word. + * Uses a direction where to look and a shift from current page. + * + */ +static void +jump_to_find_page (EvView *view, EvViewFindDirection direction, gint shift) +{ + int n_pages, i; + + n_pages = ev_document_get_n_pages (view->document); + + for (i = 0; i < n_pages; i++) { + int page; + + if (direction == EV_VIEW_FIND_NEXT) + page = view->current_page + i; + else + page = view->current_page - i; + page += shift; + + if (page >= n_pages) { + page = page - n_pages; + } else if (page < 0) + page = page + n_pages; + + if (ev_view_find_get_n_results (view, page) > 0) { + ev_document_model_set_page (view->model, page); + break; + } + } +} + +void +ev_view_find_changed (EvView *view, GList **results, gint page) +{ + view->find_pages = results; + + if (view->jump_to_find_result == TRUE) { + jump_to_find_page (view, EV_VIEW_FIND_NEXT, 0); + jump_to_find_result (view); + } + + if (view->current_page == page) + gtk_widget_queue_draw (GTK_WIDGET (view)); +} + +void +ev_view_find_next (EvView *view) +{ + gint n_results; + + n_results = ev_view_find_get_n_results (view, view->current_page); + view->find_result++; + + if (view->find_result >= n_results) { + view->find_result = 0; + jump_to_find_page (view, EV_VIEW_FIND_NEXT, 1); + jump_to_find_result (view); + } else { + jump_to_find_result (view); + gtk_widget_queue_draw (GTK_WIDGET (view)); + } +} + +void +ev_view_find_previous (EvView *view) +{ + view->find_result--; + + if (view->find_result < 0) { + jump_to_find_page (view, EV_VIEW_FIND_PREV, -1); + view->find_result = MAX (0, ev_view_find_get_n_results (view, view->current_page) - 1); + jump_to_find_result (view); + } else { + jump_to_find_result (view); + gtk_widget_queue_draw (GTK_WIDGET (view)); + } +} + +void +ev_view_find_search_changed (EvView *view) +{ + /* search string has changed, focus on new search result */ + view->jump_to_find_result = TRUE; + view->find_pages = NULL; +} + +void +ev_view_find_set_highlight_search (EvView *view, gboolean value) +{ + view->highlight_find_results = value; + gtk_widget_queue_draw (GTK_WIDGET (view)); +} + +void +ev_view_find_cancel (EvView *view) +{ + view->find_pages = NULL; +} + +/*** Synctex ***/ +void +ev_view_highlight_forward_search (EvView *view, + EvSourceLink *link) +{ + EvMapping *mapping; + gint page; + GdkRectangle view_rect; + + if (!ev_document_has_synctex (view->document)) + return; + + mapping = ev_document_synctex_forward_search (view->document, link); + if (!mapping) + return; + + if (view->synctex_result) + g_free (view->synctex_result); + view->synctex_result = mapping; + + page = GPOINTER_TO_INT (mapping->data); + ev_document_model_set_page (view->model, page); + + doc_rect_to_view_rect (view, page, &mapping->area, &view_rect); + ensure_rectangle_is_visible (view, &view_rect); + gtk_widget_queue_draw (GTK_WIDGET (view)); +} + +/*** Selections ***/ + +/* compute_new_selection_rect/text calculates the area currently selected by + * view_rect. each handles a different mode; + */ +static GList * +compute_new_selection_rect (EvView *view, + GdkPoint *start, + GdkPoint *stop) +{ + GdkRectangle view_rect; + int n_pages, i; + GList *list = NULL; + + g_assert (view->selection_mode == EV_VIEW_SELECTION_RECTANGLE); + + view_rect.x = MIN (start->x, stop->x); + view_rect.y = MIN (start->y, stop->y); + view_rect.width = MAX (start->x, stop->x) - view_rect.x; + view_rect.width = MAX (start->y, stop->y) - view_rect.y; + + n_pages = ev_document_get_n_pages (view->document); + + for (i = 0; i < n_pages; i++) { + GdkRectangle page_area; + GtkBorder border; + + if (ev_view_get_page_extents (view, i, &page_area, &border)) { + GdkRectangle overlap; + + if (gdk_rectangle_intersect (&page_area, &view_rect, &overlap)) { + EvViewSelection *selection; + + selection = g_new0 (EvViewSelection, 1); + selection->page = i; + view_rect_to_doc_rect (view, &overlap, &page_area, + &(selection->rect)); + + list = g_list_append (list, selection); + } + } + } + + return list; +} + +static gboolean +gdk_rectangle_point_in (GdkRectangle *rectangle, + GdkPoint *point) +{ + return rectangle->x <= point->x && + rectangle->y <= point->y && + point->x < rectangle->x + rectangle->width && + point->y < rectangle->y + rectangle->height; +} + +static GList * +compute_new_selection_text (EvView *view, + EvSelectionStyle style, + GdkPoint *start, + GdkPoint *stop) +{ + int n_pages, i, first, last; + GList *list = NULL; + EvViewSelection *selection; + gdouble width, height; + int start_page, end_page; + + g_assert (view->selection_mode == EV_VIEW_SELECTION_TEXT); + + n_pages = ev_document_get_n_pages (view->document); + + /* First figure out the range of pages the selection + * affects. */ + first = n_pages; + last = 0; + if (view->continuous) { + start_page = 0; + end_page = n_pages; + } else if (view->dual_page) { + start_page = view->start_page; + end_page = view->end_page + 1; + } else { + start_page = view->current_page; + end_page = view->current_page + 1; + } + + for (i = start_page; i < end_page; i++) { + GdkRectangle page_area; + GtkBorder border; + + ev_view_get_page_extents (view, i, &page_area, &border); + if (gdk_rectangle_point_in (&page_area, start) || + gdk_rectangle_point_in (&page_area, stop)) { + if (first == n_pages) + first = i; + last = i; + } + + } + + /* Now create a list of EvViewSelection's for the affected + * pages. This could be an empty list, a list of just one + * page or a number of pages.*/ + for (i = first; i < last + 1; i++) { + GdkRectangle page_area; + GtkBorder border; + GdkPoint *point; + + get_doc_page_size (view, i, &width, &height); + + selection = g_new0 (EvViewSelection, 1); + selection->page = i; + selection->style = style; + selection->rect.x1 = selection->rect.y1 = 0; + selection->rect.x2 = width; + selection->rect.y2 = height; + + ev_view_get_page_extents (view, i, &page_area, &border); + + if (gdk_rectangle_point_in (&page_area, start)) + point = start; + else + point = stop; + + if (i == first) + view_point_to_doc_point (view, point, &page_area, + &selection->rect.x1, + &selection->rect.y1); + + /* If the selection is contained within just one page, + * make sure we don't write 'start' into both points + * in selection->rect. */ + if (first == last) + point = stop; + + if (i == last) + view_point_to_doc_point (view, point, &page_area, + &selection->rect.x2, + &selection->rect.y2); + + list = g_list_append (list, selection); + } + + return list; +} + +/* This function takes the newly calculated list, and figures out which regions + * have changed. It then queues a redraw approporiately. + */ +static void +merge_selection_region (EvView *view, + GList *new_list) +{ + GList *old_list; + GList *new_list_ptr, *old_list_ptr; + + /* Update the selection */ + old_list = ev_pixbuf_cache_get_selection_list (view->pixbuf_cache); + g_list_foreach (view->selection_info.selections, (GFunc)selection_free, NULL); + g_list_free (view->selection_info.selections); + view->selection_info.selections = new_list; + ev_pixbuf_cache_set_selection_list (view->pixbuf_cache, new_list); + g_signal_emit (view, signals[SIGNAL_SELECTION_CHANGED], 0, NULL); + + new_list_ptr = new_list; + old_list_ptr = old_list; + + while (new_list_ptr || old_list_ptr) { + EvViewSelection *old_sel, *new_sel; + int cur_page; + cairo_region_t *region = NULL; + + new_sel = (new_list_ptr) ? (new_list_ptr->data) : NULL; + old_sel = (old_list_ptr) ? (old_list_ptr->data) : NULL; + + /* Assume that the lists are in order, and we run through them + * comparing them, one page at a time. We come out with the + * first page we see. */ + if (new_sel && old_sel) { + if (new_sel->page < old_sel->page) { + new_list_ptr = new_list_ptr->next; + old_sel = NULL; + } else if (new_sel->page > old_sel->page) { + old_list_ptr = old_list_ptr->next; + new_sel = NULL; + } else { + new_list_ptr = new_list_ptr->next; + old_list_ptr = old_list_ptr->next; + } + } else if (new_sel) { + new_list_ptr = new_list_ptr->next; + } else if (old_sel) { + old_list_ptr = old_list_ptr->next; + } + + g_assert (new_sel || old_sel); + + /* is the page we're looking at on the screen?*/ + cur_page = new_sel ? new_sel->page : old_sel->page; + if (cur_page < view->start_page || cur_page > view->end_page) + continue; + + /* seed the cache with a new page. We are going to need the new + * region too. */ + if (new_sel) { + cairo_region_t *tmp_region = NULL; + + ev_pixbuf_cache_get_selection_surface (view->pixbuf_cache, + cur_page, + view->scale, + &tmp_region); + + if (tmp_region && !cairo_region_is_empty (tmp_region)) { + new_sel->covered_region = cairo_region_reference (tmp_region); + } + } + + /* Now we figure out what needs redrawing */ + if (old_sel && new_sel) { + if (old_sel->covered_region && new_sel->covered_region) { + /* We only want to redraw the areas that have + * changed, so we xor the old and new regions + * and redraw if it's different */ + region = cairo_region_copy (old_sel->covered_region); +#if CAIRO_VERSION >= CAIRO_VERSION_ENCODE(1, 9, 12) + cairo_region_xor (region, new_sel->covered_region); +#else + cairo_region_t *tbr; + tbr = cairo_region_copy (new_sel->covered_region); + + /* xor old_sel, new_sel*/ + cairo_region_subtract (tbr, region); + cairo_region_subtract (region, new_sel->covered_region); + cairo_region_union (region, tbr); + cairo_region_destroy (tbr); +#endif + + if (cairo_region_is_empty (region)) { + cairo_region_destroy (region); + region = NULL; + } else { + gint num_rectangles = cairo_region_num_rectangles (region); + GdkRectangle r; + + /* We need to make the damage region a little bigger + * because the edges of the old selection might change + */ + cairo_region_get_rectangle (region, 0, &r); + r.x -= 5; + r.width = 5; + cairo_region_union_rectangle (region, &r); + + cairo_region_get_rectangle (region, num_rectangles - 1, &r); + r.x += r.width; + r.width = 5; + cairo_region_union_rectangle (region, &r); + } + } else if (old_sel->covered_region) { + region = cairo_region_copy (old_sel->covered_region); + } else if (new_sel->covered_region) { + region = cairo_region_copy (new_sel->covered_region); + } + } else if (old_sel && !new_sel) { + if (old_sel->covered_region && !cairo_region_is_empty (old_sel->covered_region)) { + region = cairo_region_copy (old_sel->covered_region); + } + } else if (!old_sel && new_sel) { + if (new_sel->covered_region && !cairo_region_is_empty (new_sel->covered_region)) { + region = cairo_region_copy (new_sel->covered_region); + } + } else { + g_assert_not_reached (); + } + + /* Redraw the damaged region! */ + if (region) { + GdkWindow *bin_window; + GdkRectangle page_area; + GtkBorder border; + + bin_window = gtk_layout_get_bin_window (GTK_LAYOUT (view)); + ev_view_get_page_extents (view, cur_page, &page_area, &border); + cairo_region_translate (region, + page_area.x + border.left - view->scroll_x, + page_area.y + border.top - view->scroll_y); +#if GTK_CHECK_VERSION(2, 90, 5) + gdk_window_invalidate_region (bin_window, region, TRUE); +#else + { + GdkRegion *gdk_region = gdk_region_new (); + guint n_recs = cairo_region_num_rectangles (region); + guint i; + + for (i = 0; i < n_recs; i++) { + cairo_rectangle_int_t rect; + + cairo_region_get_rectangle (region, i, &rect); + gdk_region_union_with_rect (gdk_region, (GdkRectangle *)&rect); + } + gdk_window_invalidate_region (bin_window, gdk_region, TRUE); + gdk_region_destroy (gdk_region); + } +#endif + cairo_region_destroy (region); + } + } + + /* Free the old list, now that we're done with it. */ + g_list_foreach (old_list, (GFunc) selection_free, NULL); + g_list_free (old_list); +} + +static void +compute_selections (EvView *view, + EvSelectionStyle style, + GdkPoint *start, + GdkPoint *stop) +{ + GList *list; + + if (view->selection_mode == EV_VIEW_SELECTION_RECTANGLE) + list = compute_new_selection_rect (view, start, stop); + else + list = compute_new_selection_text (view, style, start, stop); + merge_selection_region (view, list); +} + +/* Free's the selection. It's up to the caller to queue redraws if needed. + */ +static void +selection_free (EvViewSelection *selection) +{ + if (selection->covered_region) + cairo_region_destroy (selection->covered_region); + g_free (selection); +} + +static void +clear_selection (EvView *view) +{ + if (view->selection_info.selections) { + g_list_foreach (view->selection_info.selections, (GFunc)selection_free, NULL); + g_list_free (view->selection_info.selections); + view->selection_info.selections = NULL; + + g_signal_emit (view, signals[SIGNAL_SELECTION_CHANGED], 0, NULL); + } + view->selection_info.in_selection = FALSE; + if (view->pixbuf_cache) + ev_pixbuf_cache_set_selection_list (view->pixbuf_cache, NULL); +} + +void +ev_view_select_all (EvView *view) +{ + GList *selections = NULL; + int n_pages, i; + + /* Disable selection on rotated pages for the 0.4.0 series */ + if (view->rotation != 0) + return; + + clear_selection (view); + + n_pages = ev_document_get_n_pages (view->document); + for (i = 0; i < n_pages; i++) { + gdouble width, height; + EvViewSelection *selection; + + get_doc_page_size (view, i, &width, &height); + + selection = g_new0 (EvViewSelection, 1); + selection->page = i; + selection->style = EV_SELECTION_STYLE_GLYPH; + selection->rect.x1 = selection->rect.y1 = 0; + selection->rect.x2 = width; + selection->rect.y2 = height; + + selections = g_list_append (selections, selection); + } + + merge_selection_region (view, selections); + gtk_widget_queue_draw (GTK_WIDGET (view)); +} + +gboolean +ev_view_get_has_selection (EvView *view) +{ + return view->selection_info.selections != NULL; +} + +static char * +get_selected_text (EvView *view) +{ + GString *text; + GList *l; + gchar *normalized_text; + + text = g_string_new (NULL); + + ev_document_doc_mutex_lock (); + + for (l = view->selection_info.selections; l != NULL; l = l->next) { + EvViewSelection *selection = (EvViewSelection *)l->data; + EvPage *page; + gchar *tmp; + + page = ev_document_get_page (view->document, selection->page); + tmp = ev_selection_get_selected_text (EV_SELECTION (view->document), + page, selection->style, + &(selection->rect)); + g_object_unref (page); + g_string_append (text, tmp); + g_free (tmp); + } + + ev_document_doc_mutex_unlock (); + + normalized_text = g_utf8_normalize (text->str, text->len, G_NORMALIZE_NFKC); + g_string_free (text, TRUE); + return normalized_text; +} + +static void +ev_view_clipboard_copy (EvView *view, + const gchar *text) +{ + GtkClipboard *clipboard; + + clipboard = gtk_widget_get_clipboard (GTK_WIDGET (view), + GDK_SELECTION_CLIPBOARD); + gtk_clipboard_set_text (clipboard, text, -1); +} + +void +ev_view_copy (EvView *ev_view) +{ + char *text; + + if (!EV_IS_SELECTION (ev_view->document)) + return; + + text = get_selected_text (ev_view); + ev_view_clipboard_copy (ev_view, text); + g_free (text); +} + +static void +ev_view_primary_get_cb (GtkClipboard *clipboard, + GtkSelectionData *selection_data, + guint info, + gpointer data) +{ + EvView *ev_view = EV_VIEW (data); + + if (ev_view->link_selected) { + gtk_selection_data_set_text (selection_data, + ev_link_action_get_uri (ev_view->link_selected), + -1); + } else if (EV_IS_SELECTION (ev_view->document) && + ev_view->selection_info.selections) { + gchar *text; + + text = get_selected_text (ev_view); + if (text) { + gtk_selection_data_set_text (selection_data, text, -1); + g_free (text); + } + } +} + +static void +ev_view_primary_clear_cb (GtkClipboard *clipboard, + gpointer data) +{ + EvView *view = EV_VIEW (data); + + clear_selection (view); + clear_link_selected (view); +} + +static void +ev_view_update_primary_selection (EvView *ev_view) +{ + GtkClipboard *clipboard; + + clipboard = gtk_widget_get_clipboard (GTK_WIDGET (ev_view), + GDK_SELECTION_PRIMARY); + + if (ev_view->selection_info.selections || ev_view->link_selected) { + GtkTargetList *target_list; + GtkTargetEntry *targets; + int n_targets; + + target_list = gtk_target_list_new (NULL, 0); + gtk_target_list_add_text_targets (target_list, 0); + targets = gtk_target_table_new_from_list (target_list, &n_targets); + gtk_target_list_unref (target_list); + + if (!gtk_clipboard_set_with_owner (clipboard, + targets, n_targets, + ev_view_primary_get_cb, + ev_view_primary_clear_cb, + G_OBJECT (ev_view))) + ev_view_primary_clear_cb (clipboard, ev_view); + + gtk_target_table_free (targets, n_targets); + } else { + if (gtk_clipboard_get_owner (clipboard) == G_OBJECT (ev_view)) + gtk_clipboard_clear (clipboard); + } +} + +static void +clear_link_selected (EvView *view) +{ + if (view->link_selected) { + g_object_unref (view->link_selected); + view->link_selected = NULL; + } +} + +void +ev_view_copy_link_address (EvView *view, + EvLinkAction *action) +{ + clear_link_selected (view); + + ev_view_clipboard_copy (view, ev_link_action_get_uri (action)); + + view->link_selected = g_object_ref (action); + ev_view_update_primary_selection (view); +} + +/*** Cursor operations ***/ +static void +ev_view_set_cursor (EvView *view, EvViewCursor new_cursor) +{ + GdkCursor *cursor = NULL; + GtkWidget *widget; + GdkWindow *bin_window; + + if (view->cursor == new_cursor) { + return; + } + + view->cursor = new_cursor; + + bin_window = gtk_layout_get_bin_window (GTK_LAYOUT (view)); + widget = gtk_widget_get_toplevel (GTK_WIDGET (view)); + cursor = ev_view_cursor_new (gtk_widget_get_display (widget), new_cursor); + gdk_window_set_cursor (bin_window, cursor); + gdk_flush (); + if (cursor) + gdk_cursor_unref (cursor); +} + +void +ev_view_hide_cursor (EvView *view) +{ + ev_view_set_cursor (view, EV_VIEW_CURSOR_HIDDEN); +} + +void +ev_view_show_cursor (EvView *view) +{ + ev_view_set_cursor (view, EV_VIEW_CURSOR_NORMAL); +} + +gboolean +ev_view_next_page (EvView *view) +{ + int page, n_pages; + + g_return_val_if_fail (EV_IS_VIEW (view), FALSE); + + if (!view->document) + return FALSE; + + page = ev_document_model_get_page (view->model); + n_pages = ev_document_get_n_pages (view->document); + + if (view->dual_page) + page = page + 2; + else + page = page + 1; + + if (page < n_pages) { + ev_document_model_set_page (view->model, page); + return TRUE; + } else if (view->dual_page && page == n_pages) { + ev_document_model_set_page (view->model, page - 1); + return TRUE; + } else { + return FALSE; + } +} + +gboolean +ev_view_previous_page (EvView *view) +{ + int page; + + g_return_val_if_fail (EV_IS_VIEW (view), FALSE); + + if (!view->document) + return FALSE; + + page = ev_document_model_get_page (view->model); + + if (view->dual_page) + page = page - 2; + else + page = page - 1; + + if (page >= 0) { + ev_document_model_set_page (view->model, page); + return TRUE; + } else if (view->dual_page && page == -1) { + ev_document_model_set_page (view->model, 0); + return TRUE; + } else { + return FALSE; + } +} + diff --git a/libview/ev-view.h b/libview/ev-view.h new file mode 100644 index 00000000..ccef4bb4 --- /dev/null +++ b/libview/ev-view.h @@ -0,0 +1,115 @@ +/* this file is part of evince, a mate document viewer + * + * Copyright (C) 2004 Red Hat, Inc + * + * Evince is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * Evince is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ + +#if !defined (__EV_EVINCE_VIEW_H_INSIDE__) && !defined (EVINCE_COMPILATION) +#error "Only <evince-view.h> can be included directly." +#endif + +#ifndef __EV_VIEW_H__ +#define __EV_VIEW_H__ + +#include <gtk/gtk.h> + +#include <evince-document.h> + +#include "ev-document-model.h" + +G_BEGIN_DECLS + +#define EV_TYPE_VIEW (ev_view_get_type ()) +#define EV_VIEW(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), EV_TYPE_VIEW, EvView)) +#define EV_IS_VIEW(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), EV_TYPE_VIEW)) + +typedef struct _EvView EvView; +typedef struct _EvViewClass EvViewClass; + +typedef enum { + EV_VIEW_SELECTION_TEXT, + EV_VIEW_SELECTION_RECTANGLE, +} EvViewSelectionMode; + +GType ev_view_get_type (void) G_GNUC_CONST; + +GtkWidget* ev_view_new (void); +void ev_view_set_model (EvView *view, + EvDocumentModel *model); +void ev_view_set_loading (EvView *view, + gboolean loading); +void ev_view_reload (EvView *view); +void ev_view_set_page_cache_size (EvView *view, + gsize cache_size); + +/* Clipboard */ +void ev_view_copy (EvView *view); +void ev_view_copy_link_address (EvView *view, + EvLinkAction *action); +void ev_view_select_all (EvView *view); +gboolean ev_view_get_has_selection (EvView *view); + +/* Page size */ +gboolean ev_view_can_zoom_in (EvView *view); +void ev_view_zoom_in (EvView *view); +gboolean ev_view_can_zoom_out (EvView *view); +void ev_view_zoom_out (EvView *view); + +/* Find */ +void ev_view_find_next (EvView *view); +void ev_view_find_previous (EvView *view); +void ev_view_find_search_changed (EvView *view); +void ev_view_find_set_highlight_search (EvView *view, + gboolean value); +void ev_view_find_changed (EvView *view, + GList **results, + gint page); +void ev_view_find_cancel (EvView *view); + +/* Synctex */ +void ev_view_highlight_forward_search (EvView *view, + EvSourceLink *link); + +/* Cursor */ +void ev_view_hide_cursor (EvView *view); +void ev_view_show_cursor (EvView *view); + +/* Navigation */ +void ev_view_scroll (EvView *view, + GtkScrollType scroll, + gboolean horizontal); +void ev_view_handle_link (EvView *view, + EvLink *link); +gboolean ev_view_next_page (EvView *view); +gboolean ev_view_previous_page (EvView *view); + +void ev_view_autoscroll_start (EvView *view); +void ev_view_autoscroll_stop (EvView *view); + +gboolean ev_view_get_page_extents (EvView *view, + gint page, + GdkRectangle *page_area, + GtkBorder *border); +/* Annotations */ +void ev_view_focus_annotation (EvView *view, + EvMapping *annot_mapping); +void ev_view_begin_add_annotation (EvView *view, + EvAnnotationType annot_type); +void ev_view_cancel_add_annotation (EvView *view); + +G_END_DECLS + +#endif /* __EV_VIEW_H__ */ |