* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
*/
+#include <math.h>
#include <gtk/gtkalignment.h>
#include <glib/gi18n.h>
#include <gtk/gtkbindings.h>
#include "ev-marshal.h"
#include "ev-view.h"
#include "ev-document-find.h"
+#include "ev-document-misc.h"
+#include "ev-debug.h"
+#include "ev-job-queue.h"
+#include "ev-page-cache.h"
+#include "ev-pixbuf-cache.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))
enum {
PROP_0,
PROP_STATUS,
- PROP_FIND_STATUS
+ PROP_FIND_STATUS,
+ PROP_CONTINUOUS,
+ PROP_DUAL_PAGE,
+ PROP_FULLSCREEN,
+ PROP_PRESENTATION,
+ PROP_SIZING_MODE,
+ PROP_ZOOM,
};
enum {
- TARGET_STRING,
- TARGET_TEXT,
- TARGET_COMPOUND_TEXT,
- TARGET_UTF8_STRING,
- TARGET_TEXT_BUFFER_CONTENTS
+ SIGNAL_SCROLL_VIEW,
+ SIGNAL_ZOOM_INVALID,
+ N_SIGNALS,
+};
+
+enum {
+ TARGET_STRING,
+ TARGET_TEXT,
+ TARGET_COMPOUND_TEXT,
+ TARGET_UTF8_STRING,
+ TARGET_TEXT_BUFFER_CONTENTS
};
static const GtkTargetEntry targets[] = {
{ "UTF8_STRING", 0, TARGET_UTF8_STRING },
};
+static guint signals[N_SIGNALS];
+
typedef enum {
EV_VIEW_CURSOR_NORMAL,
EV_VIEW_CURSOR_LINK,
- EV_VIEW_CURSOR_WAIT
+ EV_VIEW_CURSOR_WAIT,
+ EV_VIEW_CURSOR_HIDDEN,
+ EV_VIEW_CURSOR_DRAG
} EvViewCursor;
+#define ZOOM_IN_FACTOR 1.2
+#define ZOOM_OUT_FACTOR (1.0/ZOOM_IN_FACTOR)
+
+#define MIN_SCALE 0.05409
+#define MAX_SCALE 4.0
+
+typedef struct {
+ EvRectangle rect;
+ int page;
+} EvViewSelection;
+
+typedef struct {
+ gboolean dragging;
+ GdkPoint start;
+ gdouble hadj;
+ gdouble vadj;
+} DragInfo;
+
+typedef enum {
+ SCROLL_TO_KEEP_POSITION,
+ SCROLL_TO_CURRENT_PAGE,
+ SCROLL_TO_CENTER
+} PendingScroll;
+
struct _EvView {
GtkWidget parent_instance;
EvDocument *document;
- EvHistory *history;
-
- GdkWindow *bin_window;
char *status;
char *find_status;
-
- int scroll_x;
- int scroll_y;
+ gint scroll_x;
+ gint scroll_y;
+
+ DragInfo drag_info;
gboolean pressed_button;
- gboolean has_selection;
GdkPoint selection_start;
- GdkRectangle selection;
+ GList *selections;
EvViewCursor cursor;
GtkAdjustment *hadjustment;
GtkAdjustment *vadjustment;
- GArray *find_results;
- int results_on_this_page;
- int next_page_with_result;
- double find_percent_complete;
+ EvPageCache *page_cache;
+ EvPixbufCache *pixbuf_cache;
+
+ gint start_page;
+ gint end_page;
+ gint current_page;
+
+ EvJobRender *current_job;
+
+ int find_page;
+ int find_result;
+ int spacing;
double scale;
+ GtkBorder border;
+
+ gboolean continuous;
+ gboolean dual_page;
+ gboolean fullscreen;
+ gboolean presentation;
+ EvSizingMode sizing_mode;
+
+ PendingScroll pending_scroll;
+ gboolean pending_resize;
};
struct _EvViewClass {
void (*scroll_view) (EvView *view,
GtkScrollType scroll,
gboolean horizontal);
-
- /* Should this be notify::page? */
- void (*page_changed) (EvView *view);
+ void (*zoom_invalid) (EvView *view);
};
-static guint page_changed_signal = 0;
-
-static void ev_view_set_scroll_adjustments (EvView *view,
- GtkAdjustment *hadjustment,
- GtkAdjustment *vadjustment);
-
+/*** Scrolling ***/
+static void ev_view_set_scroll_adjustments (EvView *view,
+ 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 ev_view_set_scroll_adjustments (EvView *view,
+ GtkAdjustment *hadjustment,
+ GtkAdjustment *vadjustment);
+static void add_scroll_binding_keypad (GtkBindingSet *binding_set,
+ guint keyval,
+ GtkScrollType scroll,
+ gboolean horizontal);
+static void ev_view_scroll_view (EvView *view,
+ 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 gboolean get_page_extents (EvView *view,
+ gint page,
+ GdkRectangle *page_area,
+ GtkBorder *border);
+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 void get_bounding_box_size (EvView *view,
+ int *max_width,
+ int *max_height);
+static void find_page_at_location (EvView *view,
+ gdouble x,
+ gdouble y,
+ gint *page,
+ gint *x_offset,
+ gint *y_offset);
+
+/*** Hyperrefs ***/
+static EvLink* get_link_at_location (EvView *view,
+ gdouble x,
+ gdouble y);
+static void go_to_link (EvView *view,
+ EvLink *link);
+static char* status_message_from_link (EvView *view,
+ EvLink *link);
+
+/*** 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 void ev_view_unrealize (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_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);
+
+/*** Drawing ***/
+static guint32 ev_gdk_color_to_rgb (const GdkColor *color);
+static void draw_rubberband (GtkWidget *widget,
+ GdkWindow *window,
+ const GdkRectangle *rect,
+ guchar alpha);
+static void highlight_find_results (EvView *view,
+ int page);
+static void draw_one_page (EvView *view,
+ gint page,
+ GdkRectangle *page_area,
+ GtkBorder *border,
+ GdkRectangle *expose_area);
+
+/*** Callbacks ***/
+static void find_changed_cb (EvDocument *document,
+ int page,
+ EvView *view);
+static void job_finished_cb (EvPixbufCache *pixbuf_cache,
+ EvView *view);
+static void page_changed_cb (EvPageCache *page_cache,
+ int 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_set_property (GObject *object,
+ guint prop_id,
+ const GValue *value,
+ GParamSpec *pspec);
+static void ev_view_get_property (GObject *object,
+ guint prop_id,
+ GValue *value,
+ GParamSpec *pspec);
+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 (int doc_width,
+ int doc_height,
+ int target_width,
+ int target_height,
+ int vsb_width);
+static double zoom_for_size_best_fit (int doc_width,
+ int doc_height,
+ int target_width,
+ int target_height,
+ int vsb_width,
+ int hsb_width);
+static void ev_view_zoom_for_size_presentation (EvView *view,
+ int width,
+ int height);
+static void ev_view_zoom_for_size_continuous_and_dual_page (EvView *view,
+ int width,
+ int height,
+ int vsb_width,
+ int hsb_height);
+static void ev_view_zoom_for_size_continuous (EvView *view,
+ int width,
+ int height,
+ int vsb_width,
+ int hsb_height);
+static void ev_view_zoom_for_size_dual_page (EvView *view,
+ int width,
+ int height,
+ int vsb_width,
+ int hsb_height);
+static void ev_view_zoom_for_size_single_page (EvView *view,
+ int width,
+ int height,
+ int vsb_width,
+ int hsb_height);
+/*** Cursors ***/
+static GdkCursor* ev_view_create_invisible_cursor (void);
+static void ev_view_set_cursor (EvView *view,
+ EvViewCursor new_cursor);
+
+/*** Status messages ***/
+static void ev_view_set_status (EvView *view,
+ const char *message);
+static void update_find_status_message (EvView *view);
+static void ev_view_set_find_status (EvView *view,
+ const char *message);
+/*** Find ***/
+static void jump_to_find_result (EvView *view);
+static void jump_to_find_page (EvView *view);
+
+/*** Selection ***/
+static void compute_selections (EvView *view,
+ GdkRectangle *view_rect);
+static void clear_selection (EvView *view);
+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_WIDGET)
-/*** Helper functions ***/
-
static void
-view_update_adjustments (EvView *view)
+scroll_to_current_page (EvView *view, GtkOrientation orientation)
{
- int old_x = view->scroll_x;
- int old_y = view->scroll_y;
-
- if (view->hadjustment)
- view->scroll_x = view->hadjustment->value;
- else
- view->scroll_x = 0;
-
- if (view->vadjustment)
- view->scroll_y = view->vadjustment->value;
- else
- view->scroll_y = 0;
-
- if (GTK_WIDGET_REALIZED (view) &&
- (view->scroll_x != old_x || view->scroll_y != old_y)) {
- gdk_window_move (view->bin_window, - view->scroll_x, - view->scroll_y);
- gdk_window_process_updates (view->bin_window, TRUE);
+ int max_width, max_height, n_rows;
+
+ get_bounding_box_size (view, &max_width, &max_height);
+
+ if (orientation == GTK_ORIENTATION_VERTICAL) {
+ if (view->continuous) {
+ n_rows = view->dual_page ? view->current_page / 2 : view->current_page;
+
+ gtk_adjustment_clamp_page (view->vadjustment,
+ (max_height + view->spacing) * n_rows,
+ (max_height + view->spacing) * n_rows +
+ view->vadjustment->page_size);
+ } else {
+ gtk_adjustment_set_value (view->vadjustment,
+ view->vadjustment->lower);
+ }
+ } else {
+ if (view->dual_page) {
+ if (view->current_page % 2 == 0) {
+ gtk_adjustment_set_value (view->hadjustment,
+ view->hadjustment->lower);
+ } else {
+ gtk_adjustment_clamp_page (view->hadjustment,
+ view->hadjustment->lower +
+ max_width + view->spacing,
+ view->hadjustment->lower +
+ max_width + view->spacing +
+ view->hadjustment->page_size);
+ }
+ } else {
+ gtk_adjustment_set_value (view->hadjustment,
+ CLAMP (view->hadjustment->value,
+ view->hadjustment->lower,
+ view->hadjustment->upper -
+ view->hadjustment->page_size));
+ }
}
}
{
GtkWidget *widget = GTK_WIDGET (view);
GtkAdjustment *adjustment;
- gboolean value_changed = FALSE;
int requisition;
int allocation;
+ double factor;
+ gint new_value;
+
if (orientation == GTK_ORIENTATION_HORIZONTAL) {
requisition = widget->requisition.width;
allocation = widget->allocation.width;
if (!adjustment)
return;
-
+
+ factor = 1.0;
+ switch (view->pending_scroll) {
+ case SCROLL_TO_KEEP_POSITION:
+ factor = adjustment->value / adjustment->upper;
+ break;
+ case SCROLL_TO_CURRENT_PAGE:
+ break;
+ case SCROLL_TO_CENTER:
+ factor = (adjustment->value + adjustment->page_size * 0.5) / adjustment->upper;
+ break;
+ }
+
adjustment->page_size = allocation;
adjustment->step_increment = allocation * 0.1;
adjustment->page_increment = allocation * 0.9;
adjustment->lower = 0;
adjustment->upper = MAX (allocation, requisition);
- if (adjustment->value > adjustment->upper - adjustment->page_size) {
- adjustment->value = adjustment->upper - adjustment->page_size;
- value_changed = TRUE;
+ switch (view->pending_scroll) {
+ case SCROLL_TO_KEEP_POSITION:
+ new_value = CLAMP (adjustment->upper * factor, 0, adjustment->upper - adjustment->page_size);
+ gtk_adjustment_set_value (adjustment, (int)new_value);
+ break;
+ case SCROLL_TO_CURRENT_PAGE:
+ scroll_to_current_page (view, orientation);
+ break;
+ case SCROLL_TO_CENTER:
+ new_value = CLAMP (adjustment->upper * factor - adjustment->page_size * 0.5,
+ 0, adjustment->upper - adjustment->page_size);
+ gtk_adjustment_set_value (adjustment, (int)new_value);
+ break;
}
gtk_adjustment_changed (adjustment);
- if (value_changed)
- gtk_adjustment_value_changed (adjustment);
}
-/*** Virtual function implementations ***/
-
static void
-ev_view_finalize (GObject *object)
+view_update_range_and_current_page (EvView *view)
{
- EvView *view = EV_VIEW (object);
+ /* Presentation trumps all other modes */
+ if (view->presentation) {
+ view->start_page = view->current_page;
+ view->end_page = view->current_page;
+ } else if (view->continuous) {
+ GdkRectangle current_area, unused, page_area;
+ gint current_page;
+ gboolean found = FALSE;
+ int i;
+
+ get_bounding_box_size (view, &(page_area.width), &(page_area.height));
+ page_area.x = view->spacing;
+ page_area.y = view->spacing;
- if (view->document)
- g_object_unref (view->document);
+ if (view->hadjustment) {
+ current_area.x = view->hadjustment->value;
+ current_area.width = view->hadjustment->page_size;
+ } else {
+ current_area.x = page_area.x;
+ current_area.width = page_area.width;
+ }
- if (view->history)
- g_object_unref (view->history);
+ if (view->vadjustment) {
+ current_area.y = view->vadjustment->value;
+ current_area.height = view->vadjustment->page_size;
+ } else {
+ current_area.y = page_area.y;
+ current_area.height = page_area.height;
+ }
- ev_view_set_scroll_adjustments (view, NULL, NULL);
+ for (i = 0; i < ev_page_cache_get_n_pages (view->page_cache); i++) {
+ if (gdk_rectangle_intersect (¤t_area, &page_area, &unused)) {
+ if (! found) {
+ view->start_page = i;
+ found = TRUE;
+
+ }
+ view->end_page = i;
+ } else if (found) {
+ break;
+ }
+ if (view->dual_page) {
+ if (i % 2 == 0) {
+ page_area.x += page_area.width + view->spacing;
+ } else {
+ page_area.x = view->spacing;
+ page_area.y += page_area.height + view->spacing;
+ }
+ } else {
+ page_area.y += page_area.height + view->spacing;
+ }
+ }
- g_array_free (view->find_results, TRUE);
- view->find_results = NULL;
-
- G_OBJECT_CLASS (ev_view_parent_class)->finalize (object);
-}
+ current_page = ev_page_cache_get_current_page (view->page_cache);
-static void
-ev_view_destroy (GtkObject *object)
-{
- EvView *view = EV_VIEW (object);
+ if (current_page < view->start_page || current_page > view->end_page) {
+ view->current_page = view->start_page;
+ ev_page_cache_set_current_page (view->page_cache, view->start_page);
+ }
+ } else {
+ if (view->dual_page) {
+ if (view->current_page % 2 == 0) {
+ view->start_page = view->current_page;
+ if (view->current_page + 1 < ev_page_cache_get_n_pages (view->page_cache))
+ view->end_page = view->start_page + 1;
+ } 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;
+ }
+ }
- ev_view_set_scroll_adjustments (view, NULL, NULL);
-
- GTK_OBJECT_CLASS (ev_view_parent_class)->destroy (object);
+ ev_pixbuf_cache_set_page_range (view->pixbuf_cache,
+ view->start_page,
+ view->end_page,
+ view->scale);
}
static void
-ev_view_size_request (GtkWidget *widget,
- GtkRequisition *requisition)
+set_scroll_adjustment (EvView *view,
+ GtkOrientation orientation,
+ GtkAdjustment *adjustment)
{
- EvView *view = EV_VIEW (widget);
+ GtkAdjustment **to_set;
- if (GTK_WIDGET_REALIZED (widget)) {
- if (view->document) {
- ev_document_get_page_size (view->document,
- &requisition->width,
- &requisition->height);
- } else {
- requisition->width = 10;
- requisition->height = 10;
+ 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);
}
- requisition->width += 2;
- requisition->height += 2;
+ *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_size_allocate (GtkWidget *widget,
- GtkAllocation *allocation)
+ev_view_set_scroll_adjustments (EvView *view,
+ GtkAdjustment *hadjustment,
+ GtkAdjustment *vadjustment)
{
- EvView *view = EV_VIEW (widget);
+ set_scroll_adjustment (view, GTK_ORIENTATION_HORIZONTAL, hadjustment);
+ set_scroll_adjustment (view, GTK_ORIENTATION_VERTICAL, vadjustment);
- GTK_WIDGET_CLASS (ev_view_parent_class)->size_allocate (widget, allocation);
+ on_adjustment_value_changed (NULL, view);
+}
- view_set_adjustment_values (view, GTK_ORIENTATION_HORIZONTAL);
- view_set_adjustment_values (view, GTK_ORIENTATION_VERTICAL);
+static void
+add_scroll_binding_keypad (GtkBindingSet *binding_set,
+ guint keyval,
+ GtkScrollType scroll,
+ gboolean horizontal)
+{
+ guint keypad_keyval = keyval - GDK_Left + GDK_KP_Left;
+
+ gtk_binding_entry_add_signal (binding_set, keyval, 0,
+ "scroll_view", 2,
+ GTK_TYPE_SCROLL_TYPE, scroll,
+ G_TYPE_BOOLEAN, horizontal);
+ gtk_binding_entry_add_signal (binding_set, keypad_keyval, 0,
+ "scroll_view", 2,
+ GTK_TYPE_SCROLL_TYPE, scroll,
+ G_TYPE_BOOLEAN, horizontal);
+}
- if (GTK_WIDGET_REALIZED (widget)) {
- gdk_window_resize (view->bin_window,
- MAX (widget->allocation.width, widget->requisition.width),
- MAX (widget->allocation.height, widget->requisition.height));
+void
+ev_view_scroll (EvView *view,
+ EvScrollType scroll)
+{
+ GtkAdjustment *adjustment;
+ double value, increment;
+ gboolean first_page = FALSE;
+ gboolean last_page = FALSE;
+
+ /* Assign values for increment and vertical adjustment */
+ adjustment = view->vadjustment;
+ increment = adjustment->page_size * 0.75;
+ value = adjustment->value;
+
+ /* Assign boolean for first and last page */
+ if (view->current_page == 0)
+ first_page = TRUE;
+ if (view->current_page == ev_page_cache_get_n_pages (view->page_cache) - 1)
+ last_page = TRUE;
+
+ switch (scroll) {
+ case EV_SCROLL_PAGE_BACKWARD:
+ /* Do not jump backwards if at the first page */
+ if (value == (adjustment->lower) && first_page) {
+ /* Do nothing */
+ /* At the top of a page, assign the upper bound limit of previous page */
+ } else if (value == (adjustment->lower)) {
+ value = adjustment->upper - adjustment->page_size;
+ ev_page_cache_set_current_page (view->page_cache, view->current_page - 1);
+ /* Jump to the top */
+ } else {
+ value = MAX (value - increment, adjustment->lower);
+ }
+ break;
+ case EV_SCROLL_PAGE_FORWARD:
+ /* Do not jump forward if at the last page */
+ if (value == (adjustment->upper - adjustment->page_size) && last_page) {
+ /* Do nothing */
+ /* At the bottom of a page, assign the lower bound limit of next page */
+ } else if (value == (adjustment->upper - adjustment->page_size)) {
+ value = 0;
+ ev_page_cache_set_current_page (view->page_cache, view->current_page + 1);
+ /* Jump to the bottom */
+ } else {
+ value = MIN (value + increment, adjustment->upper - adjustment->page_size);
+ }
+ break;
+ default:
+ break;
}
+
+ gtk_adjustment_set_value (adjustment, value);
}
static void
-ev_view_realize (GtkWidget *widget)
+ev_view_scroll_view (EvView *view,
+ GtkScrollType scroll,
+ gboolean horizontal)
{
- EvView *view = EV_VIEW (widget);
- GdkWindowAttr attributes;
+ GtkAdjustment *adjustment;
+ double value;
- GTK_WIDGET_SET_FLAGS (widget, GTK_REALIZED);
-
- 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);
-
- attributes.x = widget->allocation.x;
- attributes.y = widget->allocation.y;
- attributes.width = widget->allocation.width;
- attributes.height = widget->allocation.height;
- attributes.event_mask = 0;
-
- widget->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 (widget->window, widget);
- widget->style = gtk_style_attach (widget->style, widget->window);
-
- attributes.x = 0;
- attributes.y = 0;
- attributes.width = MAX (widget->allocation.width, widget->requisition.width);
- attributes.height = MAX (widget->allocation.height, widget->requisition.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;
-
- view->bin_window = gdk_window_new (widget->window,
- &attributes,
- GDK_WA_X | GDK_WA_Y |
- GDK_WA_COLORMAP |
- GDK_WA_VISUAL);
- gdk_window_set_user_data (view->bin_window, widget);
- gdk_window_show (view->bin_window);
-
- widget->style = gtk_style_attach (widget->style, view->bin_window);
- gdk_window_set_background (view->bin_window, &widget->style->mid[widget->state]);
+ if (horizontal) {
+ adjustment = view->hadjustment;
+ } else {
+ adjustment = view->vadjustment;
+ }
- if (view->document) {
- ev_document_set_target (view->document, view->bin_window);
+ value = adjustment->value;
- /* We can't get page size without a target, so we have to
- * queue a size request at realization. Could be fixed
- * with EvDocument changes to allow setting a GdkScreen
- * without setting a target.
- */
- gtk_widget_queue_resize (widget);
+ switch (scroll) {
+ case GTK_SCROLL_STEP_BACKWARD:
+ value -= adjustment->step_increment;
+ break;
+ case GTK_SCROLL_STEP_FORWARD:
+ value += adjustment->step_increment;
+ break;
+ default:
+ break;
}
+
+ value = CLAMP (value, adjustment->lower,
+ adjustment->upper - adjustment->page_size);
+
+ gtk_adjustment_set_value (adjustment, value);
}
+#define MARGIN 5
+
static void
-ev_view_unrealize (GtkWidget *widget)
+ensure_rectangle_is_visible (EvView *view, GdkRectangle *rect)
{
- EvView *view = EV_VIEW (widget);
-
- if (view->document)
- ev_document_set_target (view->document, NULL);
+ GtkWidget *widget = GTK_WIDGET (view);
+ GtkAdjustment *adjustment;
+ int value;
+
+ adjustment = view->vadjustment;
+
+ if (rect->y < adjustment->value) {
+ value = MAX (adjustment->lower, rect->y - MARGIN);
+ gtk_adjustment_set_value (view->vadjustment, value);
+ } else if (rect->y + rect->height >
+ adjustment->value + widget->allocation.height) {
+ value = MIN (adjustment->upper, rect->y + rect->height -
+ widget->allocation.height + MARGIN);
+ gtk_adjustment_set_value (view->vadjustment, value);
+ }
- gdk_window_set_user_data (view->bin_window, NULL);
- gdk_window_destroy (view->bin_window);
- view->bin_window = NULL;
+ adjustment = view->hadjustment;
- GTK_WIDGET_CLASS (ev_view_parent_class)->unrealize (widget);
+ if (rect->x < adjustment->value) {
+ value = MAX (adjustment->lower, rect->x - MARGIN);
+ gtk_adjustment_set_value (view->hadjustment, value);
+ } else if (rect->x + rect->height >
+ adjustment->value + widget->allocation.width) {
+ value = MIN (adjustment->upper, rect->x + rect->width -
+ widget->allocation.width + MARGIN);
+ gtk_adjustment_set_value (view->hadjustment, value);
+ }
}
-static guint32
-ev_gdk_color_to_rgb (const GdkColor *color)
+/*** Geometry computations ***/
+
+static void
+compute_border (EvView *view, int width, int height, GtkBorder *border)
{
- guint32 result;
- result = (0xff0000 | (color->red & 0xff00));
- result <<= 8;
- result |= ((color->green & 0xff00) | (color->blue >> 8));
- return result;
+ if (view->presentation) {
+ border->left = 0;
+ border->right = 0;
+ border->top = 0;
+ border->bottom = 0;
+ } else {
+ ev_document_misc_get_page_border_size (width, height, border);
+ }
}
-static void
-draw_rubberband (GtkWidget *widget, GdkWindow *window, const GdkRectangle *rect)
+static gboolean
+get_page_extents (EvView *view,
+ gint page,
+ GdkRectangle *page_area,
+ GtkBorder *border)
{
- GdkGC *gc;
- GdkPixbuf *pixbuf;
- GdkColor *fill_color_gdk;
- guint fill_color;
+ GtkWidget *widget;
+ int width, height;
- fill_color_gdk = gdk_color_copy (>K_WIDGET (widget)->style->base[GTK_STATE_SELECTED]);
- fill_color = ev_gdk_color_to_rgb (fill_color_gdk) << 8 | 0x40;
+ widget = GTK_WIDGET (view);
+
+ /* Quick sanity check */
+ if (view->presentation) {
+ if (view->current_page != page)
+ return FALSE;
+ } else if (view->continuous) {
+ if (page < view->start_page ||
+ page > view->end_page)
+ return FALSE;
+ } else if (view->dual_page) {
+ if (ABS (page - view->current_page) > 1)
+ return FALSE;
+ } else {
+ if (view->current_page != page)
+ return FALSE;
+ }
- pixbuf = gdk_pixbuf_new (GDK_COLORSPACE_RGB, TRUE, 8,
- rect->width, rect->height);
- gdk_pixbuf_fill (pixbuf, fill_color);
+ /* Get the size of the page */
+ ev_page_cache_get_size (view->page_cache, page,
+ view->scale,
+ &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->presentation) {
+ page_area->x = (MAX (0, widget->allocation.width - width))/2;
+ page_area->y = (MAX (0, widget->allocation.height - height))/2;
+ } else if (view->continuous) {
+ gint max_width, max_height;
+ gint x, y;
+
+ get_bounding_box_size (view, &max_width, &max_height);
+ /* Get the location of the bounding box */
+ if (view->dual_page) {
+ x = view->spacing + (page % 2) * (max_width + view->spacing);
+ y = view->spacing + (page / 2) * (max_height + view->spacing);
+ x = x + MAX (0, widget->allocation.width - (max_width * 2 + view->spacing * 3))/2;
+ } else {
+ x = view->spacing;
+ y = view->spacing + page * (max_height + view->spacing);
+ x = x + MAX (0, widget->allocation.width - (max_width + view->spacing * 2))/2;
+ }
+ 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 ^ 1;
+
+ /* First, we get the bounding box of the two pages */
+ if (other_page < ev_page_cache_get_n_pages (view->page_cache)) {
+ ev_page_cache_get_size (view->page_cache,
+ page + 1,
+ view->scale,
+ &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);
- gdk_draw_pixbuf (window, NULL, pixbuf,
- 0, 0,
- rect->x,rect->y,
- rect->width, rect->height,
- GDK_RGB_DITHER_NONE,
- 0, 0);
+ /* Find the offsets */
+ x = view->spacing;
+ y = view->spacing;
- g_object_unref (pixbuf);
+ /* Adjust for being the left or right page */
+ if (page % 2 == 0)
+ x = x + max_width - width;
+ else
+ x = x + (max_width + overall_border.left + overall_border.right) + view->spacing;
- gc = gdk_gc_new (window);
- gdk_gc_set_rgb_fg_color (gc, fill_color_gdk);
- gdk_draw_rectangle (window, gc, FALSE,
- rect->x, rect->y,
- rect->width - 1,
- rect->height - 1);
- g_object_unref (gc);
+ y = y + (max_height - height)/2;
- gdk_color_free (fill_color_gdk);
-}
+ /* Adjust for extra allocation */
+ x = x + MAX (0, widget->allocation.width -
+ ((max_width + overall_border.left + overall_border.right) * 2 + view->spacing * 3))/2;
+ y = y + MAX (0, widget->allocation.height - (height + view->spacing * 2))/2;
+ } else {
+ x = view->spacing;
+ y = view->spacing;
-static void
-expose_bin_window (GtkWidget *widget,
- GdkEventExpose *event)
-{
- EvView *view = EV_VIEW (widget);
- int i, current_page;
- int x_offset, y_offset;
- const EvFindResult *results;
+ /* Adjust for extra allocation */
+ x = x + MAX (0, widget->allocation.width - (width + border->left + border->right + view->spacing * 2))/2;
+ y = y + MAX (0, widget->allocation.height - (height + border->top + border->bottom + view->spacing * 2))/2;
+ }
- if (view->document == NULL)
- return;
+ page_area->x = x;
+ page_area->y = y;
+ }
- x_offset = MAX (0, (widget->allocation.width -
- widget->requisition.width) / 2);
- y_offset = MAX (0, (widget->allocation.height -
- widget->requisition.height) / 2);
- gdk_draw_rectangle (view->bin_window,
- widget->style->black_gc,
- FALSE,
- x_offset,
- y_offset,
- widget->requisition.width - 1,
- widget->requisition.height - 1);
-
- ev_document_set_page_offset (view->document,
- x_offset + 1,
- y_offset + 1);
-
- ev_document_render (view->document,
- event->area.x, event->area.y,
- event->area.width, event->area.height);
-
- results = (EvFindResult*) view->find_results->data;
- current_page = ev_document_get_page (view->document);
- i = 0;
- while (i < view->find_results->len) {
-#if 0
- g_printerr ("highlighting result %d page %d at %d,%d %dx%d\n",
- i, results[i].page_num,
- results[i].highlight_area.x,
- results[i].highlight_area.y,
- results[i].highlight_area.width,
- results[i].highlight_area.height);
-#endif
- if (results[i].page_num == current_page)
- draw_rubberband (widget, view->bin_window,
- &results[i].highlight_area);
- ++i;
- }
+ return TRUE;
+}
- if (view->has_selection) {
- draw_rubberband (widget, view->bin_window, &view->selection);
- }
+static void
+view_rect_to_doc_rect (EvView *view,
+ GdkRectangle *view_rect,
+ GdkRectangle *page_area,
+ EvRectangle *doc_rect)
+{
+ doc_rect->x1 = floor ((view_rect->x - page_area->x) / view->scale);
+ doc_rect->y1 = floor ((view_rect->y - page_area->y) / view->scale);
+ doc_rect->x2 = doc_rect->x1 + ceil (view_rect->width / view->scale);
+ doc_rect->y2 = doc_rect->y1 + ceil (view_rect->height / view->scale);
}
-static gboolean
-ev_view_expose_event (GtkWidget *widget,
- GdkEventExpose *event)
+static void
+doc_rect_to_view_rect (EvView *view,
+ int page,
+ EvRectangle *doc_rect,
+ GdkRectangle *view_rect)
{
- EvView *view = EV_VIEW (widget);
+ GdkRectangle page_area;
+ GtkBorder border;
+ int width, height;
- if (event->window == view->bin_window)
- expose_bin_window (widget, event);
- else
- return GTK_WIDGET_CLASS (ev_view_parent_class)->expose_event (widget, event);
+ get_page_extents (view, page, &page_area, &border);
- return FALSE;
+ width = doc_rect->x2 - doc_rect->x1;
+ height = doc_rect->y2 - doc_rect->y1;
+ view_rect->x = floor (doc_rect->x1 * view->scale) + page_area.x;
+ view_rect->y = floor (doc_rect->y1 * view->scale) + page_area.y;
+ view_rect->width = ceil (width * view->scale);
+ view_rect->height = ceil (height * view->scale);
}
-void
-ev_view_select_all (EvView *ev_view)
+static void
+get_bounding_box_size (EvView *view, int *max_width, int *max_height)
{
- GtkWidget *widget = GTK_WIDGET (ev_view);
+ GtkBorder border;
+ int width, height;
- g_return_if_fail (EV_IS_VIEW (ev_view));
+ if (max_width) {
+ ev_page_cache_get_max_width_size (view->page_cache,
+ view->scale,
+ &width, &height);
+ compute_border (view, width, height, &border);
+ *max_width = width + border.left + border.right;
+ }
- ev_view->has_selection = TRUE;
- ev_view->selection.x = ev_view->selection.y = 0;
- ev_view->selection.width = widget->requisition.width;
- ev_view->selection.height = widget->requisition.height;
- gtk_widget_queue_draw (widget);
+ if (max_height) {
+ ev_page_cache_get_max_height_size (view->page_cache,
+ view->scale,
+ &width, &height);
+ compute_border (view, width, height, &border);
+ *max_height = height + border.top + border.bottom;
+ }
}
-void
-ev_view_copy (EvView *ev_view)
+static void
+find_page_at_location (EvView *view,
+ gdouble x,
+ gdouble y,
+ gint *page,
+ gint *x_offset,
+ gint *y_offset)
{
- GtkClipboard *clipboard;
- char *text;
+ int i;
- text = ev_document_get_text (ev_view->document, &ev_view->selection);
- clipboard = gtk_widget_get_clipboard (GTK_WIDGET (ev_view),
- GDK_SELECTION_CLIPBOARD);
- gtk_clipboard_set_text (clipboard, text, -1);
- g_free (text);
-}
+ if (view->document == NULL)
+ return;
-static void
-ev_view_primary_get_cb (GtkClipboard *clipboard,
- GtkSelectionData *selection_data,
- guint info,
- gpointer data)
-{
- EvView *ev_view = EV_VIEW (data);
- char *text;
+ g_assert (page);
+ g_assert (x_offset);
+ g_assert (y_offset);
+
+ for (i = view->start_page; i <= view->end_page; i++) {
+ GdkRectangle page_area;
+ GtkBorder border;
+
+ if (! 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;
+ }
+ }
- text = ev_document_get_text (ev_view->document, &ev_view->selection);
- gtk_selection_data_set_text (selection_data, text, -1);
+ *page = -1;
}
-static void
-ev_view_primary_clear_cb (GtkClipboard *clipboard,
- gpointer data)
+/*** Hyperref ***/
+static EvLink *
+get_link_at_location (EvView *view,
+ gdouble x,
+ gdouble y)
{
- EvView *ev_view = EV_VIEW (data);
+ gint page = -1;
+ gint x_offset = 0, y_offset = 0;
+ GList *link_mapping;
- ev_view->has_selection = FALSE;
-}
+ find_page_at_location (view, x, y, &page, &x_offset, &y_offset);
-static void
-ev_view_update_primary_selection (EvView *ev_view)
-{
- GtkClipboard *clipboard;
+ if (page == -1)
+ return NULL;
- clipboard = gtk_widget_get_clipboard (GTK_WIDGET (ev_view),
- GDK_SELECTION_PRIMARY);
+ link_mapping = ev_pixbuf_cache_get_link_mapping (view->pixbuf_cache, page);
- if (ev_view->has_selection) {
- if (!gtk_clipboard_set_with_owner (clipboard,
- targets,
- G_N_ELEMENTS (targets),
- ev_view_primary_get_cb,
- ev_view_primary_clear_cb,
- G_OBJECT (ev_view)))
- ev_view_primary_clear_cb (clipboard, ev_view);
- } else {
- if (gtk_clipboard_get_owner (clipboard) == G_OBJECT (ev_view))
- gtk_clipboard_clear (clipboard);
- }
+ if (link_mapping)
+ return ev_link_mapping_find (link_mapping, x_offset / view->scale, y_offset / view->scale);
+ else
+ return NULL;
}
-static gboolean
-ev_view_button_press_event (GtkWidget *widget,
- GdkEventButton *event)
+/* FIXME: standardize this sometime */
+static void
+go_to_link (EvView *view, EvLink *link)
{
- EvView *view = EV_VIEW (widget);
-
- if (!GTK_WIDGET_HAS_FOCUS (widget)) {
- gtk_widget_grab_focus (widget);
- }
-
- view->pressed_button = event->button;
+ EvLinkType type;
+ const char *uri;
+ int page;
- switch (event->button) {
- case 1:
- if (view->has_selection) {
- view->has_selection = FALSE;
- gtk_widget_queue_draw (widget);
- }
+ type = ev_link_get_link_type (link);
- view->selection_start.x = event->x;
- view->selection_start.y = event->y;
+ switch (type) {
+ case EV_LINK_TYPE_TITLE:
+ break;
+ case EV_LINK_TYPE_PAGE:
+ page = ev_link_get_page (link);
+ ev_page_cache_set_current_page (view->page_cache, page);
+ break;
+ case EV_LINK_TYPE_EXTERNAL_URI:
+ uri = ev_link_get_uri (link);
+ gnome_vfs_url_show (uri);
break;
}
-
- return TRUE;
}
static char *
-status_message_from_link (EvLink *link)
+status_message_from_link (EvView *view, EvLink *link)
{
EvLinkType type;
- char *msg;
- int page;
+ char *msg = NULL;
+ char *page_label;
type = ev_link_get_link_type (link);
-
+
switch (type) {
case EV_LINK_TYPE_TITLE:
- msg = g_strdup (ev_link_get_title (link));
+ if (ev_link_get_title (link))
+ msg = g_strdup (ev_link_get_title (link));
break;
case EV_LINK_TYPE_PAGE:
- page = ev_link_get_page (link);
- msg = g_strdup_printf (_("Go to page %d"), page);
+ page_label = ev_page_cache_get_page_label (view->page_cache, ev_link_get_page (link));
+ msg = g_strdup_printf (_("Go to page %s"), page_label);
+ g_free (page_label);
break;
case EV_LINK_TYPE_EXTERNAL_URI:
msg = g_strdup (ev_link_get_uri (link));
break;
default:
- msg = NULL;
+ break;
}
return msg;
}
-static void
-ev_view_set_status (EvView *view, const char *message)
-{
- g_return_if_fail (EV_IS_VIEW (view));
-
- if (message != view->status) {
- g_free (view->status);
- view->status = g_strdup (message);
- g_object_notify (G_OBJECT (view), "status");
- }
-}
-static void
-ev_view_set_find_status (EvView *view, const char *message)
-{
- g_return_if_fail (EV_IS_VIEW (view));
-
- g_free (view->find_status);
- view->find_status = g_strdup (message);
- g_object_notify (G_OBJECT (view), "find-status");
-}
+/*** GtkWidget implementation ***/
static void
-ev_view_set_cursor (EvView *view, EvViewCursor new_cursor)
+ev_view_size_request_continuous_dual_page (EvView *view,
+ GtkRequisition *requisition)
{
- GdkCursor *cursor = NULL;
- GdkDisplay *display;
- GtkWidget *widget;
+ int max_width, max_height;
+ int n_rows;
- if (view->cursor == new_cursor) {
- return;
- }
+ get_bounding_box_size (view, &max_width, &max_height);
- widget = gtk_widget_get_toplevel (GTK_WIDGET (view));
- display = gtk_widget_get_display (widget);
- view->cursor = new_cursor;
+ n_rows = (1 + ev_page_cache_get_n_pages (view->page_cache)) / 2;
- switch (new_cursor) {
- case EV_VIEW_CURSOR_NORMAL:
- gdk_window_set_cursor (widget->window, NULL);
- 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;
- }
+ requisition->width = (max_width * 2) + (view->spacing * 3);
+ requisition->height = max_height * n_rows + (view->spacing * (n_rows + 1));
- if (cursor) {
- gdk_window_set_cursor (widget->window, cursor);
- gdk_cursor_unref (cursor);
- gdk_flush();
+ if (view->sizing_mode == EV_SIZING_FIT_WIDTH) {
+ requisition->width = 1;
+ } else if (view->sizing_mode == EV_SIZING_BEST_FIT) {
+ requisition->width = 1;
+ /* FIXME: This could actually be set on one page docs or docs
+ * with a strange aspect ratio. */
+ /* requisition->height = 1;*/
}
}
-static gboolean
-ev_view_motion_notify_event (GtkWidget *widget,
- GdkEventMotion *event)
+static void
+ev_view_size_request_continuous (EvView *view,
+ GtkRequisition *requisition)
{
- EvView *view = EV_VIEW (widget);
+ int max_width, max_height;
+ int n_pages;
- if (view->pressed_button > 0) {
- view->has_selection = TRUE;
- view->selection.x = MIN (view->selection_start.x, event->x);
- view->selection.y = MIN (view->selection_start.y, event->y);
- view->selection.width = ABS (view->selection_start.x - event->x) + 1;
- view->selection.height = ABS (view->selection_start.y - event->y) + 1;
- } else if (view->document) {
- EvLink *link;
+ get_bounding_box_size (view, &max_width, &max_height);
- link = ev_document_get_link (view->document, event->x, event->y);
- if (link) {
- char *msg;
+ n_pages = ev_page_cache_get_n_pages (view->page_cache);
- msg = status_message_from_link (link);
- ev_view_set_status (view, msg);
- ev_view_set_cursor (view, EV_VIEW_CURSOR_LINK);
- g_free (msg);
+ requisition->width = max_width + (view->spacing * 2);
+ requisition->height = max_height * n_pages + (view->spacing * (n_pages + 1));
- g_object_unref (link);
- } else {
- ev_view_set_status (view, NULL);
- if (view->cursor == EV_VIEW_CURSOR_LINK) {
- ev_view_set_cursor (view, EV_VIEW_CURSOR_NORMAL);
- }
- }
+ if (view->sizing_mode == EV_SIZING_FIT_WIDTH) {
+ requisition->width = 1;
+ } else if (view->sizing_mode == EV_SIZING_BEST_FIT) {
+ requisition->width = 1;
+ /* FIXME: This could actually be set on one page docs or docs
+ * with a strange aspect ratio. */
+ /* requisition->height = 1;*/
}
-
- gtk_widget_queue_draw (widget);
-
- return TRUE;
}
-static gboolean
-ev_view_button_release_event (GtkWidget *widget,
- GdkEventButton *event)
+static void
+ev_view_size_request_dual_page (EvView *view,
+ GtkRequisition *requisition)
{
- EvView *view = EV_VIEW (widget);
-
- view->pressed_button = -1;
-
- if (view->has_selection) {
- ev_view_update_primary_selection (view);
- } else if (view->document) {
- EvLink *link;
-
- link = ev_document_get_link (view->document,
- event->x,
- event->y);
- if (link) {
- ev_view_go_to_link (view, link);
- g_object_unref (link);
+ GtkBorder border;
+ gint width, height;
+
+ /* Find the largest of the two. */
+ ev_page_cache_get_size (view->page_cache,
+ view->current_page,
+ view->scale,
+ &width, &height);
+ if (view->current_page + 1 < ev_page_cache_get_n_pages (view->page_cache)) {
+ gint width_2, height_2;
+ ev_page_cache_get_size (view->page_cache,
+ view->current_page + 1,
+ view->scale,
+ &width_2, &height_2);
+ if (width_2 > width) {
+ width = width_2;
+ height = height_2;
}
}
-
- return FALSE;
+ compute_border (view, width, height, &border);
+
+ requisition->width = ((width + border.left + border.right) * 2) +
+ (view->spacing * 3);
+ requisition->height = (height + border.top + border.bottom) +
+ (view->spacing * 2);
+
+ if (view->sizing_mode == EV_SIZING_FIT_WIDTH) {
+ requisition->width = 1;
+ } else if (view->sizing_mode == EV_SIZING_BEST_FIT) {
+ requisition->width = 1;
+ requisition->height = 1;
+ }
}
static void
-on_adjustment_value_changed (GtkAdjustment *adjustment,
- EvView *view)
+ev_view_size_request_single_page (EvView *view,
+ GtkRequisition *requisition)
{
- view_update_adjustments (view);
+ GtkBorder border;
+ gint width, height;
+
+ ev_page_cache_get_size (view->page_cache,
+ view->current_page,
+ view->scale,
+ &width, &height);
+ compute_border (view, width, height, &border);
+
+ requisition->width = width + border.left + border.right + (2 * view->spacing);
+ requisition->height = height + border.top + border.bottom + (2 * view->spacing);
+
+ if (view->sizing_mode == EV_SIZING_FIT_WIDTH) {
+ requisition->width = 1;
+ requisition->height = height + border.top + border.bottom + (2 * view->spacing);
+ } else if (view->sizing_mode == EV_SIZING_BEST_FIT) {
+ requisition->width = 1;
+ requisition->height = 1;
+ }
}
static void
-set_scroll_adjustment (EvView *view,
- GtkOrientation orientation,
- GtkAdjustment *adjustment)
+ev_view_size_request (GtkWidget *widget,
+ GtkRequisition *requisition)
{
- 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);
+ EvView *view = EV_VIEW (widget);
- if (*to_set) {
- g_object_ref (*to_set);
- g_signal_connect (*to_set, "value_changed",
- G_CALLBACK (on_adjustment_value_changed), view);
- }
+ if (view->document == NULL) {
+ requisition->width = 1;
+ requisition->height = 1;
+ return;
}
-}
-
-static void
-ev_view_set_scroll_adjustments (EvView *view,
- GtkAdjustment *hadjustment,
- GtkAdjustment *vadjustment)
-{
- set_scroll_adjustment (view, GTK_ORIENTATION_HORIZONTAL, hadjustment);
- set_scroll_adjustment (view, GTK_ORIENTATION_VERTICAL, vadjustment);
- view_update_adjustments (view);
-}
+ if (view->presentation) {
+ requisition->width = 1;
+ requisition->height = 1;
+ return;
+ }
-static void
-add_scroll_binding (GtkBindingSet *binding_set,
- guint keyval,
- GtkScrollType scroll,
- gboolean horizontal)
-{
- guint keypad_keyval = keyval - GDK_Left + GDK_KP_Left;
-
- gtk_binding_entry_add_signal (binding_set, keyval, 0,
- "scroll_view", 2,
- GTK_TYPE_SCROLL_TYPE, scroll,
- G_TYPE_BOOLEAN, horizontal);
- gtk_binding_entry_add_signal (binding_set, keypad_keyval, 0,
- "scroll_view", 2,
- GTK_TYPE_SCROLL_TYPE, scroll,
- G_TYPE_BOOLEAN, horizontal);
+ if (view->continuous && view->dual_page)
+ ev_view_size_request_continuous_dual_page (view, requisition);
+ else if (view->continuous)
+ ev_view_size_request_continuous (view, requisition);
+ else if (view->dual_page)
+ ev_view_size_request_dual_page (view, requisition);
+ else
+ ev_view_size_request_single_page (view, requisition);
}
static void
-ev_view_scroll_view (EvView *view,
- GtkScrollType scroll,
- gboolean horizontal)
+ev_view_size_allocate (GtkWidget *widget,
+ GtkAllocation *allocation)
{
- if (scroll == GTK_SCROLL_PAGE_BACKWARD) {
- ev_view_set_page (view, ev_view_get_page (view) - 1);
- } else if (scroll == GTK_SCROLL_PAGE_FORWARD) {
- ev_view_set_page (view, ev_view_get_page (view) + 1);
- } else {
- GtkAdjustment *adjustment;
- double value;
-
- if (horizontal) {
- adjustment = view->hadjustment;
- } else {
- adjustment = view->vadjustment;
- }
+ EvView *view = EV_VIEW (widget);
+
+ if (view->sizing_mode == EV_SIZING_FIT_WIDTH ||
+ view->sizing_mode == EV_SIZING_BEST_FIT) {
- value = adjustment->value;
+ g_signal_emit (view, signals[SIGNAL_ZOOM_INVALID], 0);
+
+ ev_view_size_request (widget, &widget->requisition);
+ }
+
+ view_set_adjustment_values (view, GTK_ORIENTATION_HORIZONTAL);
+ view_set_adjustment_values (view, GTK_ORIENTATION_VERTICAL);
- switch (scroll) {
- case GTK_SCROLL_STEP_BACKWARD:
- value -= adjustment->step_increment;
- break;
- case GTK_SCROLL_STEP_FORWARD:
- value += adjustment->step_increment;
- break;
- default:
- break;
- }
+ view->pending_scroll = SCROLL_TO_KEEP_POSITION;
+ view->pending_resize = FALSE;
- value = CLAMP (value, adjustment->lower,
- adjustment->upper - adjustment->page_size);
+ if (view->document)
+ view_update_range_and_current_page (view);
- gtk_adjustment_set_value (adjustment, value);
- }
+ GTK_WIDGET_CLASS (ev_view_parent_class)->size_allocate (widget, allocation);
}
static void
-ev_view_set_property (GObject *object,
- guint prop_id,
- const GValue *value,
- GParamSpec *pspec)
+ev_view_realize (GtkWidget *widget)
{
- switch (prop_id)
- {
- /* Read only */
- case PROP_STATUS:
- case PROP_FIND_STATUS:
- break;
- }
-}
+ EvView *view = EV_VIEW (widget);
+ GdkWindowAttr attributes;
+
+ GTK_WIDGET_SET_FLAGS (widget, GTK_REALIZED);
+
+
+ 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);
+
+ attributes.x = widget->allocation.x;
+ attributes.y = widget->allocation.y;
+ attributes.width = widget->allocation.width;
+ attributes.height = widget->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_LEAVE_NOTIFY_MASK;
+
+ widget->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 (widget->window, widget);
+ widget->style = gtk_style_attach (widget->style, widget->window);
+
+ if (view->presentation)
+ gdk_window_set_background (widget->window, &widget->style->black);
+ else
+ gdk_window_set_background (widget->window, &widget->style->mid [GTK_STATE_NORMAL]);
+}
+
+static void
+ev_view_unrealize (GtkWidget *widget)
+{
+ GTK_WIDGET_CLASS (ev_view_parent_class)->unrealize (widget);
+}
+
+static gboolean
+ev_view_scroll_event (GtkWidget *widget, GdkEventScroll *event)
+{
+ EvView *view = EV_VIEW (widget);
+
+ if ((event->state & GDK_CONTROL_MASK) != 0) {
+
+ ev_view_set_sizing_mode (view, 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;
+ }
+
+ if ((event->state & GDK_SHIFT_MASK) != 0) {
+ if (event->direction == GDK_SCROLL_UP)
+ event->direction = GDK_SCROLL_LEFT;
+ if (event->direction == GDK_SCROLL_DOWN)
+ event->direction = GDK_SCROLL_RIGHT;
+ }
+
+ return FALSE;
+}
+
+static gboolean
+ev_view_expose_event (GtkWidget *widget,
+ GdkEventExpose *event)
+{
+ EvView *view = EV_VIEW (widget);
+ GdkRectangle rubberband;
+ GList *l;
+ int i;
+
+ if (view->document == NULL)
+ return FALSE;
+
+ for (i = view->start_page; i <= view->end_page; i++) {
+ GdkRectangle page_area;
+ GtkBorder border;
+
+ if (!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, &page_area, &border, &(event->area));
+
+ if (EV_IS_DOCUMENT_FIND (view->document)) {
+ highlight_find_results (view, i);
+ }
+ }
+
+ for (l = view->selections; l != NULL; l = l->next) {
+ EvViewSelection *selection = (EvViewSelection *)l->data;
+
+ doc_rect_to_view_rect (view, selection->page,
+ &selection->rect, &rubberband);
+ if (rubberband.width > 0 && rubberband.height > 0) {
+ draw_rubberband (GTK_WIDGET (view), GTK_WIDGET(view)->window,
+ &rubberband, 0x40);
+ }
+ }
+
+ return FALSE;
+}
+
+static gboolean
+ev_view_button_press_event (GtkWidget *widget,
+ GdkEventButton *event)
+{
+ EvView *view = EV_VIEW (widget);
+
+ if (!GTK_WIDGET_HAS_FOCUS (widget)) {
+ gtk_widget_grab_focus (widget);
+ }
+
+ view->pressed_button = event->button;
+
+ switch (event->button) {
+ case 1:
+ if (view->selections) {
+ clear_selection (view);
+ gtk_widget_queue_draw (widget);
+ }
+
+ view->selection_start.x = event->x;
+ view->selection_start.y = event->y;
+ 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;
+ }
+
+ return FALSE;
+}
+
+static gboolean
+ev_view_motion_notify_event (GtkWidget *widget,
+ GdkEventMotion *event)
+{
+ EvView *view = EV_VIEW (widget);
+
+ if (view->pressed_button == 1) {
+ GdkRectangle selection;
+
+ selection.x = MIN (view->selection_start.x, event->x) + view->scroll_x;
+ selection.y = MIN (view->selection_start.y, event->y) + view->scroll_y;
+ selection.width = ABS (view->selection_start.x - event->x) + 1;
+ selection.height = ABS (view->selection_start.y - event->y) + 1;
+
+ compute_selections (view, &selection);
+
+ gtk_widget_queue_draw (widget);
+
+ return TRUE;
+ } else if (view->pressed_button == 2) {
+ if (!view->drag_info.dragging) {
+ gboolean start;
+
+ 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.dragging = start;
+ }
+
+ if (view->drag_info.dragging) {
+ int dx, dy;
+ gdouble dhadj_value, dvadj_value;
+
+ dx = event->x_root - view->drag_info.start.x;
+ dy = event->y_root - view->drag_info.start.y;
+
+ dhadj_value = view->hadjustment->page_size *
+ (gdouble)dx / widget->allocation.width;
+ dvadj_value = view->vadjustment->page_size *
+ (gdouble)dy / widget->allocation.height;
+
+ /* clamp scrolling to visible area */
+ gtk_adjustment_set_value (view->hadjustment,
+ MIN(view->drag_info.hadj - dhadj_value,
+ view->hadjustment->upper -
+ view->hadjustment->page_size));
+ gtk_adjustment_set_value (view->vadjustment,
+ MIN(view->drag_info.vadj - dvadj_value,
+ view->vadjustment->upper -
+ view->vadjustment->page_size));
+
+ return TRUE;
+ }
+ } else if (view->pressed_button <= 0 && view->document) {
+ EvLink *link;
+
+ link = get_link_at_location (view, event->x + view->scroll_x, event->y + view->scroll_y);
+ if (link) {
+ char *msg;
+
+ msg = status_message_from_link (view, link);
+ ev_view_set_status (view, msg);
+ ev_view_set_cursor (view, EV_VIEW_CURSOR_LINK);
+ g_free (msg);
+ } else {
+ ev_view_set_status (view, NULL);
+ if (view->cursor == EV_VIEW_CURSOR_LINK) {
+ ev_view_set_cursor (view, EV_VIEW_CURSOR_NORMAL);
+ }
+ }
+ return TRUE;
+ }
+
+ return FALSE;
+}
+
+static gboolean
+ev_view_button_release_event (GtkWidget *widget,
+ GdkEventButton *event)
+{
+ EvView *view = EV_VIEW (widget);
+
+ if (view->pressed_button == 2) {
+ ev_view_set_cursor (view, EV_VIEW_CURSOR_NORMAL);
+ }
+
+ view->pressed_button = -1;
+ view->drag_info.dragging = FALSE;
+
+ if (view->selections) {
+ ev_view_update_primary_selection (view);
+ } else if (view->document) {
+ EvLink *link;
+
+ link = get_link_at_location (view, event->x + view->scroll_x, event->y + view->scroll_y);
+ if (link) {
+ go_to_link (view, link);
+ }
+ }
+
+ return FALSE;
+}
+
+/*** Drawing ***/
+
+static guint32
+ev_gdk_color_to_rgb (const GdkColor *color)
+{
+ guint32 result;
+ result = (0xff0000 | (color->red & 0xff00));
+ result <<= 8;
+ result |= ((color->green & 0xff00) | (color->blue >> 8));
+ return result;
+}
+
+static void
+draw_rubberband (GtkWidget *widget, GdkWindow *window,
+ const GdkRectangle *rect, guchar alpha)
+{
+ GdkGC *gc;
+ GdkPixbuf *pixbuf;
+ GdkColor *fill_color_gdk;
+ guint fill_color;
+
+ fill_color_gdk = gdk_color_copy (>K_WIDGET (widget)->style->base[GTK_STATE_SELECTED]);
+ fill_color = ev_gdk_color_to_rgb (fill_color_gdk) << 8 | alpha;
+
+ pixbuf = gdk_pixbuf_new (GDK_COLORSPACE_RGB, TRUE, 8,
+ rect->width, rect->height);
+ gdk_pixbuf_fill (pixbuf, fill_color);
+
+ gdk_draw_pixbuf (window, NULL, pixbuf,
+ 0, 0,
+ rect->x - EV_VIEW (widget)->scroll_x, rect->y - EV_VIEW (widget)->scroll_y,
+ rect->width, rect->height,
+ GDK_RGB_DITHER_NONE,
+ 0, 0);
+
+ g_object_unref (pixbuf);
+
+ gc = gdk_gc_new (window);
+ gdk_gc_set_rgb_fg_color (gc, fill_color_gdk);
+ gdk_draw_rectangle (window, gc, FALSE,
+ rect->x - EV_VIEW (widget)->scroll_x, rect->y - EV_VIEW (widget)->scroll_y,
+ rect->width - 1,
+ rect->height - 1);
+ g_object_unref (gc);
+
+ gdk_color_free (fill_color_gdk);
+}
+
+
+static void
+highlight_find_results (EvView *view, int page)
+{
+ EvDocumentFind *find;
+ int i, results = 0;
+
+ g_return_if_fail (EV_IS_DOCUMENT_FIND (view->document));
+
+ find = EV_DOCUMENT_FIND (view->document);
+
+ results = ev_document_find_get_n_results (find, page);
+
+ for (i = 0; i < results; i++) {
+ EvRectangle rectangle;
+ GdkRectangle view_rectangle;
+ guchar alpha;
+
+ if (i == view->find_result && page == view->find_page) {
+ alpha = 0x90;
+ } else {
+ alpha = 0x20;
+ }
+
+ ev_document_find_get_result (find, page,
+ i, &rectangle);
+ doc_rect_to_view_rect (view, page, &rectangle, &view_rectangle);
+ draw_rubberband (GTK_WIDGET (view), GTK_WIDGET(view)->window,
+ &view_rectangle, alpha);
+ }
+}
+
+static void
+draw_one_page (EvView *view,
+ gint page,
+ GdkRectangle *page_area,
+ GtkBorder *border,
+ GdkRectangle *expose_area)
+{
+ gint width, height;
+ GdkPixbuf *scaled_image;
+ GdkPixbuf *current_pixbuf;
+ GdkRectangle overlap;
+ GdkRectangle real_page_area;
+
+ g_assert (view->document);
+ if (! gdk_rectangle_intersect (page_area, expose_area, &overlap))
+ return;
+
+ ev_page_cache_get_size (view->page_cache,
+ page, view->scale,
+ &width, &height);
+ /* 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);
+
+ ev_document_misc_paint_one_page (GTK_WIDGET(view)->window,
+ GTK_WIDGET (view),
+ page_area, border);
+
+ if (gdk_rectangle_intersect (&real_page_area, expose_area, &overlap)) {
+ current_pixbuf = ev_pixbuf_cache_get_pixbuf (view->pixbuf_cache, page);
+
+ if (current_pixbuf == NULL)
+ scaled_image = NULL;
+ else if (width == gdk_pixbuf_get_width (current_pixbuf) &&
+ height == gdk_pixbuf_get_height (current_pixbuf))
+ scaled_image = g_object_ref (current_pixbuf);
+ else
+ /* FIXME: We don't want to scale the whole area, just the right
+ * area of it */
+ scaled_image = gdk_pixbuf_scale_simple (current_pixbuf,
+ width, height,
+ GDK_INTERP_NEAREST);
+
+ if (scaled_image) {
+ gdk_draw_pixbuf (GTK_WIDGET(view)->window,
+ GTK_WIDGET (view)->style->fg_gc[GTK_STATE_NORMAL],
+ scaled_image,
+ overlap.x - real_page_area.x,
+ overlap.y - real_page_area.y,
+ overlap.x, overlap.y,
+ overlap.width, overlap.height,
+ GDK_RGB_DITHER_NORMAL,
+ 0, 0);
+ g_object_unref (scaled_image);
+ }
+ }
+}
+
+/*** GObject functions ***/
+
+static void
+ev_view_finalize (GObject *object)
+{
+ EvView *view = EV_VIEW (object);
+
+ LOG ("Finalize");
+
+ g_free (view->status);
+ g_free (view->find_status);
+
+ clear_selection (view);
+
+ G_OBJECT_CLASS (ev_view_parent_class)->finalize (object);
+}
+
+static void
+ev_view_destroy (GtkObject *object)
+{
+ EvView *view = EV_VIEW (object);
+
+ if (view->document) {
+ g_object_unref (view->document);
+ view->document = NULL;
+ }
+ if (view->pixbuf_cache) {
+ g_object_unref (view->pixbuf_cache);
+ view->pixbuf_cache = NULL;
+ }
+ ev_view_set_scroll_adjustments (view, NULL, NULL);
+
+ GTK_OBJECT_CLASS (ev_view_parent_class)->destroy (object);
+}
+
+static void
+ev_view_set_property (GObject *object,
+ guint prop_id,
+ const GValue *value,
+ GParamSpec *pspec)
+{
+ EvView *view = EV_VIEW (object);
+
+ switch (prop_id)
+ {
+ case PROP_CONTINUOUS:
+ ev_view_set_continuous (view, g_value_get_boolean (value));
+ break;
+ case PROP_DUAL_PAGE:
+ ev_view_set_dual_page (view, g_value_get_boolean (value));
+ break;
+ case PROP_FULLSCREEN:
+ ev_view_set_fullscreen (view, g_value_get_boolean (value));
+ break;
+ case PROP_PRESENTATION:
+ ev_view_set_presentation (view, g_value_get_boolean (value));
+ break;
+ case PROP_SIZING_MODE:
+ ev_view_set_sizing_mode (view, g_value_get_enum (value));
+ break;
+ case PROP_ZOOM:
+ ev_view_set_zoom (view, g_value_get_double (value), FALSE);
+ break;
+ default:
+ G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
+ }
+}
static void
ev_view_get_property (GObject *object,
switch (prop_id)
{
- case PROP_STATUS:
- g_value_set_string (value, view->status);
- break;
- case PROP_FIND_STATUS:
- g_value_set_string (value, view->status);
- break;
+ case PROP_STATUS:
+ g_value_set_string (value, view->status);
+ break;
+ case PROP_FIND_STATUS:
+ g_value_set_string (value, view->status);
+ break;
+ case PROP_CONTINUOUS:
+ g_value_set_boolean (value, view->continuous);
+ break;
+ case PROP_DUAL_PAGE:
+ g_value_set_boolean (value, view->dual_page);
+ break;
+ case PROP_FULLSCREEN:
+ g_value_set_boolean (value, view->fullscreen);
+ break;
+ case PROP_PRESENTATION:
+ g_value_set_boolean (value, view->presentation);
+ break;
+ case PROP_SIZING_MODE:
+ g_value_set_enum (value, view->sizing_mode);
+ break;
+ case PROP_ZOOM:
+ g_value_set_double (value, view->scale);
+ break;
+ default:
+ G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
}
}
widget_class->size_allocate = ev_view_size_allocate;
widget_class->realize = ev_view_realize;
widget_class->unrealize = ev_view_unrealize;
+ widget_class->scroll_event = ev_view_scroll_event;
gtk_object_class->destroy = ev_view_destroy;
class->set_scroll_adjustments = ev_view_set_scroll_adjustments;
class->scroll_view = ev_view_scroll_view;
- widget_class->set_scroll_adjustments_signal = g_signal_new ("set-scroll-adjustments",
- G_OBJECT_CLASS_TYPE (object_class),
- G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
- G_STRUCT_OFFSET (EvViewClass, set_scroll_adjustments),
- NULL, NULL,
- ev_marshal_VOID__OBJECT_OBJECT,
- G_TYPE_NONE, 2,
- GTK_TYPE_ADJUSTMENT,
- GTK_TYPE_ADJUSTMENT);
- page_changed_signal = g_signal_new ("page-changed",
- G_OBJECT_CLASS_TYPE (object_class),
- G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
- G_STRUCT_OFFSET (EvViewClass, page_changed),
- NULL, NULL,
- ev_marshal_VOID__NONE,
- G_TYPE_NONE, 0);
-
- g_signal_new ("scroll_view",
- G_TYPE_FROM_CLASS (object_class),
- G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
- G_STRUCT_OFFSET (EvViewClass, scroll_view),
- NULL, NULL,
- ev_marshal_VOID__ENUM_BOOLEAN,
- G_TYPE_NONE, 2,
- GTK_TYPE_SCROLL_TYPE,
- G_TYPE_BOOLEAN);
+ widget_class->set_scroll_adjustments_signal =
+ g_signal_new ("set-scroll-adjustments",
+ G_OBJECT_CLASS_TYPE (object_class),
+ G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
+ G_STRUCT_OFFSET (EvViewClass, set_scroll_adjustments),
+ NULL, NULL,
+ ev_marshal_VOID__OBJECT_OBJECT,
+ G_TYPE_NONE, 2,
+ GTK_TYPE_ADJUSTMENT,
+ GTK_TYPE_ADJUSTMENT);
+
+ signals[SIGNAL_SCROLL_VIEW] = g_signal_new ("scroll-view",
+ G_TYPE_FROM_CLASS (object_class),
+ G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
+ G_STRUCT_OFFSET (EvViewClass, scroll_view),
+ NULL, NULL,
+ ev_marshal_VOID__ENUM_BOOLEAN,
+ G_TYPE_NONE, 2,
+ GTK_TYPE_SCROLL_TYPE,
+ G_TYPE_BOOLEAN);
+
+ signals[SIGNAL_ZOOM_INVALID] = g_signal_new ("zoom-invalid",
+ G_TYPE_FROM_CLASS (object_class),
+ G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
+ G_STRUCT_OFFSET (EvViewClass, zoom_invalid),
+ NULL, NULL,
+ ev_marshal_VOID__VOID,
+ G_TYPE_NONE, 0, G_TYPE_NONE);
g_object_class_install_property (object_class,
PROP_STATUS,
G_PARAM_READABLE));
g_object_class_install_property (object_class,
- PROP_STATUS,
+ PROP_FIND_STATUS,
g_param_spec_string ("find-status",
"Find Status Message",
"The find status message",
NULL,
G_PARAM_READABLE));
- binding_set = gtk_binding_set_by_class (class);
+ g_object_class_install_property (object_class,
+ PROP_CONTINUOUS,
+ g_param_spec_boolean ("continuous",
+ "Continuous",
+ "Continuous scrolling mode",
+ TRUE,
+ G_PARAM_READWRITE));
+
+ g_object_class_install_property (object_class,
+ PROP_DUAL_PAGE,
+ g_param_spec_boolean ("dual-page",
+ "Dual Page",
+ "Two pages visible at once",
+ FALSE,
+ G_PARAM_READWRITE));
+ g_object_class_install_property (object_class,
+ PROP_FULLSCREEN,
+ g_param_spec_boolean ("fullscreen",
+ "Full Screen",
+ "Draw page in a fullscreen fashion",
+ FALSE,
+ G_PARAM_READWRITE));
+ g_object_class_install_property (object_class,
+ PROP_PRESENTATION,
+ g_param_spec_boolean ("presentation",
+ "Presentation",
+ "Draw page in presentation mode",
+ TRUE,
+ G_PARAM_READWRITE));
+
+ g_object_class_install_property (object_class,
+ PROP_SIZING_MODE,
+ g_param_spec_enum ("sizing-mode",
+ "Sizing Mode",
+ "Sizing Mode",
+ EV_TYPE_SIZING_MODE,
+ EV_SIZING_FIT_WIDTH,
+ G_PARAM_READWRITE));
+
+ g_object_class_install_property (object_class,
+ PROP_ZOOM,
+ g_param_spec_double ("zoom",
+ "Zoom factor",
+ "Zoom factor",
+ MIN_SCALE,
+ MAX_SCALE,
+ 1.0,
+ G_PARAM_READWRITE));
- add_scroll_binding (binding_set, GDK_Left, GTK_SCROLL_STEP_BACKWARD, TRUE);
- add_scroll_binding (binding_set, GDK_Right, GTK_SCROLL_STEP_FORWARD, TRUE);
- add_scroll_binding (binding_set, GDK_Up, GTK_SCROLL_STEP_BACKWARD, FALSE);
- add_scroll_binding (binding_set, GDK_Down, GTK_SCROLL_STEP_FORWARD, FALSE);
+ binding_set = gtk_binding_set_by_class (class);
- add_scroll_binding (binding_set, GDK_Page_Up, GTK_SCROLL_PAGE_BACKWARD, FALSE);
- add_scroll_binding (binding_set, GDK_Page_Down, GTK_SCROLL_PAGE_FORWARD, FALSE);
+ add_scroll_binding_keypad (binding_set, GDK_Left, GTK_SCROLL_STEP_BACKWARD, TRUE);
+ add_scroll_binding_keypad (binding_set, GDK_Right, GTK_SCROLL_STEP_FORWARD, TRUE);
+ add_scroll_binding_keypad (binding_set, GDK_Up, GTK_SCROLL_STEP_BACKWARD, FALSE);
+ add_scroll_binding_keypad (binding_set, GDK_Down, GTK_SCROLL_STEP_FORWARD, FALSE);
}
static void
{
GTK_WIDGET_SET_FLAGS (view, GTK_CAN_FOCUS);
+ view->spacing = 5;
view->scale = 1.0;
+ view->current_page = 0;
view->pressed_button = -1;
view->cursor = EV_VIEW_CURSOR_NORMAL;
+ view->drag_info.dragging = FALSE;
+
+ view->continuous = TRUE;
+ view->dual_page = FALSE;
+ view->presentation = FALSE;
+ view->fullscreen = FALSE;
+ view->sizing_mode = EV_SIZING_FIT_WIDTH;
+ view->pending_scroll = SCROLL_TO_KEEP_POSITION;
+}
+
+/*** Callbacks ***/
+
+static void
+find_changed_cb (EvDocument *document, int page, EvView *view)
+{
+ jump_to_find_page (view);
+ jump_to_find_result (view);
+ update_find_status_message (view);
+
+ if (view->current_page == page)
+ gtk_widget_queue_draw (GTK_WIDGET (view));
+}
+
+static void
+job_finished_cb (EvPixbufCache *pixbuf_cache,
+ EvView *view)
+{
+ gtk_widget_queue_draw (GTK_WIDGET (view));
+}
+
+static void
+page_changed_cb (EvPageCache *page_cache,
+ int new_page,
+ EvView *view)
+{
+ if (view->current_page != new_page) {
+
+ view->current_page = new_page;
+ view->pending_scroll = SCROLL_TO_CURRENT_PAGE;
+ gtk_widget_queue_resize (GTK_WIDGET (view));
+
+ if (EV_IS_DOCUMENT_FIND (view->document)) {
+ view->find_page = new_page;
+ view->find_result = 0;
+ update_find_status_message (view);
+ }
+ }
+}
+
+static void on_adjustment_value_changed (GtkAdjustment *adjustment,
+ EvView *view)
+{
+ int dx = 0, dy = 0;
+
+ if (! GTK_WIDGET_REALIZED (view))
+ return;
+
+ if (view->hadjustment) {
+ dx = view->scroll_x - (int) view->hadjustment->value;
+ view->scroll_x = (int) view->hadjustment->value;
+ } else {
+ view->scroll_x = 0;
+ }
+
+ if (view->vadjustment) {
+ dy = view->scroll_y - (int) view->vadjustment->value;
+ view->scroll_y = (int) view->vadjustment->value;
+ } else {
+ view->scroll_y = 0;
+ }
+
+
+ if (view->pending_resize)
+ gtk_widget_queue_draw (GTK_WIDGET (view));
+ else
+ gdk_window_scroll (GTK_WIDGET (view)->window, dx, dy);
+
+
+ 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;
+}
+
+void
+ev_view_set_document (EvView *view,
+ EvDocument *document)
+{
+ g_return_if_fail (EV_IS_VIEW (view));
+
+ if (document != view->document) {
+ if (view->document) {
+ g_signal_handlers_disconnect_by_func (view->document,
+ find_changed_cb,
+ view);
+ g_object_unref (view->document);
+ view->page_cache = NULL;
+
+ }
+
+ view->document = document;
+ view->find_page = 0;
+ view->find_result = 0;
+
+ if (view->document) {
+ g_object_ref (view->document);
+ if (EV_IS_DOCUMENT_FIND (view->document)) {
+ g_signal_connect (view->document,
+ "find_changed",
+ G_CALLBACK (find_changed_cb),
+ view);
+ }
+ view->page_cache = ev_document_get_page_cache (view->document);
+ g_signal_connect (view->page_cache, "page-changed", G_CALLBACK (page_changed_cb), view);
+ view->pixbuf_cache = ev_pixbuf_cache_new (view->document);
+ g_signal_connect (view->pixbuf_cache, "job-finished", G_CALLBACK (job_finished_cb), view);
+ }
+
+ gtk_widget_queue_resize (GTK_WIDGET (view));
+ }
+}
+
+/*** Zoom and sizing mode ***/
+
+#define EPSILON 0.0000001
+void
+ev_view_set_zoom (EvView *view,
+ double factor,
+ gboolean relative)
+{
+ double scale;
+
+ if (relative)
+ scale = view->scale * factor;
+ else
+ scale = factor;
+
+ scale = CLAMP (scale, MIN_SCALE, MAX_SCALE);
+
+ if (ABS (view->scale - scale) < EPSILON)
+ return;
+
+ view->scale = scale;
+ view->pending_resize = TRUE;
+
+ gtk_widget_queue_resize (GTK_WIDGET (view));
+
+ g_object_notify (G_OBJECT (view), "zoom");
+}
+
+double
+ev_view_get_zoom (EvView *view)
+{
+ return view->scale;
+}
+
+void
+ev_view_set_continuous (EvView *view,
+ gboolean continuous)
+{
+ g_return_if_fail (EV_IS_VIEW (view));
+
+ continuous = continuous != FALSE;
+
+ if (view->continuous != continuous) {
+ view->continuous = continuous;
+ view->pending_scroll = SCROLL_TO_CURRENT_PAGE;
+ gtk_widget_queue_resize (GTK_WIDGET (view));
+ }
+
+ g_object_notify (G_OBJECT (view), "continuous");
+}
+
+void
+ev_view_set_dual_page (EvView *view,
+ gboolean dual_page)
+{
+ g_return_if_fail (EV_IS_VIEW (view));
+
+ dual_page = dual_page != FALSE;
+
+ if (view->dual_page == dual_page)
+ return;
+
+ view->pending_scroll = SCROLL_TO_CURRENT_PAGE;
+ view->dual_page = dual_page;
+ /* 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));
+
+ g_object_notify (G_OBJECT (view), "dual-page");
+}
+
+void
+ev_view_set_fullscreen (EvView *view,
+ gboolean fullscreen)
+{
+ g_return_if_fail (EV_IS_VIEW (view));
+
+ fullscreen = fullscreen != FALSE;
+
+ if (view->fullscreen != fullscreen) {
+ view->fullscreen = fullscreen;
+ gtk_widget_queue_resize (GTK_WIDGET (view));
+ }
+
+ g_object_notify (G_OBJECT (view), "fullscreen");
+}
+
+gboolean
+ev_view_get_fullscreen (EvView *view)
+{
+ g_return_val_if_fail (EV_IS_VIEW (view), FALSE);
+
+ return view->fullscreen;
+}
+
+void
+ev_view_set_presentation (EvView *view,
+ gboolean presentation)
+{
+ g_return_if_fail (EV_IS_VIEW (view));
+
+ presentation = presentation != FALSE;
+
+ if (view->presentation == presentation)
+ return;
+
+ view->presentation = presentation;
+ gtk_widget_queue_resize (GTK_WIDGET (view));
+ if (GTK_WIDGET_REALIZED (view)) {
+ if (view->presentation)
+ gdk_window_set_background (GTK_WIDGET(view)->window,
+ >K_WIDGET (view)->style->black);
+ else
+ gdk_window_set_background (GTK_WIDGET(view)->window,
+ >K_WIDGET (view)->style->mid [GTK_STATE_NORMAL]);
+ }
+
+
+ g_object_notify (G_OBJECT (view), "presentation");
+}
+
+gboolean
+ev_view_get_presentation (EvView *view)
+{
+ g_return_val_if_fail (EV_IS_VIEW (view), FALSE);
+
+ return view->presentation;
+}
+
+void
+ev_view_set_sizing_mode (EvView *view,
+ EvSizingMode sizing_mode)
+{
+ g_return_if_fail (EV_IS_VIEW (view));
+
+ if (view->sizing_mode == sizing_mode)
+ return;
+
+ view->sizing_mode = sizing_mode;
+ gtk_widget_queue_resize (GTK_WIDGET (view));
- view->find_results = g_array_new (FALSE,
- FALSE,
- sizeof (EvFindResult));
- view->results_on_this_page = 0;
- view->next_page_with_result = 0;
+ g_object_notify (G_OBJECT (view), "sizing-mode");
+}
+
+EvSizingMode
+ev_view_get_sizing_mode (EvView *view)
+{
+ g_return_val_if_fail (EV_IS_VIEW (view), EV_SIZING_FREE);
+
+ return view->sizing_mode;
+}
+
+gboolean
+ev_view_can_zoom_in (EvView *view)
+{
+ return view->scale * ZOOM_IN_FACTOR <= MAX_SCALE;
+}
+
+gboolean
+ev_view_can_zoom_out (EvView *view)
+{
+ return view->scale * ZOOM_OUT_FACTOR >= MIN_SCALE;
+}
+
+void
+ev_view_zoom_in (EvView *view)
+{
+ g_return_if_fail (view->sizing_mode == EV_SIZING_FREE);
+
+ view->pending_scroll = SCROLL_TO_CENTER;
+ ev_view_set_zoom (view, ZOOM_IN_FACTOR, TRUE);
+}
+
+void
+ev_view_zoom_out (EvView *view)
+{
+ g_return_if_fail (view->sizing_mode == EV_SIZING_FREE);
+
+ view->pending_scroll = SCROLL_TO_CENTER;
+ ev_view_set_zoom (view, ZOOM_OUT_FACTOR, TRUE);
+}
+
+static double
+zoom_for_size_fit_width (int doc_width,
+ int doc_height,
+ int target_width,
+ int target_height,
+ int vsb_width)
+{
+ double scale;
+
+ scale = (double)target_width / doc_width;
+
+ if (doc_height * scale > target_height)
+ scale = (double) (target_width - vsb_width) / doc_width;
+
+ return scale;
+}
+
+static double
+zoom_for_size_best_fit (int doc_width,
+ int doc_height,
+ int target_width,
+ int target_height,
+ int vsb_width,
+ int hsb_width)
+{
+ double w_scale;
+ double h_scale;
+
+ w_scale = (double)target_width / doc_width;
+ h_scale = (double)target_height / doc_height;
+
+ if (doc_height * w_scale > target_height)
+ w_scale = (double) (target_width - vsb_width) / doc_width;
+ if (doc_width * h_scale > target_width)
+ h_scale = (double) (target_height - hsb_width) / doc_height;
+
+ return MIN (w_scale, h_scale);
+}
+
+
+static void
+ev_view_zoom_for_size_presentation (EvView *view,
+ int width,
+ int height)
+{
+ int doc_width, doc_height;
+ gdouble scale;
+
+ ev_page_cache_get_size (view->page_cache,
+ view->current_page,
+ 1.0,
+ &doc_width,
+ &doc_height);
+ scale = zoom_for_size_best_fit (doc_width, doc_height, width, height, 0, 0);
+ ev_view_set_zoom (view, scale, FALSE);
+}
+
+static void
+ev_view_zoom_for_size_continuous_and_dual_page (EvView *view,
+ int width,
+ int height,
+ int vsb_width,
+ int hsb_height)
+{
+ int doc_width, doc_height;
+ GtkBorder border;
+ gdouble scale;
+
+ ev_page_cache_get_max_width_size (view->page_cache,
+ 1.0,
+ &doc_width, NULL);
+ ev_page_cache_get_max_height_size (view->page_cache,
+ 1.0,
+ NULL, &doc_height);
+ compute_border (view, doc_width, doc_height, &border);
+
+ doc_width = doc_width * 2;
+ width -= (2 * (border.left + border.right) + 3 * view->spacing);
+ height -= (border.top + border.bottom + 2 * view->spacing - 1);
+
+ /* FIXME: We really need to calculate the overall height here, not the
+ * page height. We assume there's always a vertical scrollbar for
+ * now. We need to fix this. */
+ if (view->sizing_mode == EV_SIZING_FIT_WIDTH)
+ scale = zoom_for_size_fit_width (doc_width, doc_height, width - vsb_width, height, 0);
+ else if (view->sizing_mode == EV_SIZING_BEST_FIT)
+ scale = zoom_for_size_best_fit (doc_width, doc_height, width - vsb_width, height, 0, hsb_height);
+ else
+ g_assert_not_reached ();
+
+ ev_view_set_zoom (view, scale, FALSE);
+}
+
+static void
+ev_view_zoom_for_size_continuous (EvView *view,
+ int width,
+ int height,
+ int vsb_width,
+ int hsb_height)
+{
+ int doc_width, doc_height;
+ GtkBorder border;
+ gdouble scale;
+
+ ev_page_cache_get_max_width_size (view->page_cache,
+ 1.0,
+ &doc_width, NULL);
+ ev_page_cache_get_max_height_size (view->page_cache,
+ 1.0,
+ NULL, &doc_height);
+ 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);
+
+ /* FIXME: We really need to calculate the overall height here, not the
+ * page height. We assume there's always a vertical scrollbar for
+ * now. We need to fix this. */
+ if (view->sizing_mode == EV_SIZING_FIT_WIDTH)
+ scale = zoom_for_size_fit_width (doc_width, doc_height, width - vsb_width, height, 0);
+ else if (view->sizing_mode == EV_SIZING_BEST_FIT)
+ scale = zoom_for_size_best_fit (doc_width, doc_height, width - vsb_width, height, 0, hsb_height);
+ else
+ g_assert_not_reached ();
+
+ ev_view_set_zoom (view, scale, FALSE);
+}
+
+static void
+ev_view_zoom_for_size_dual_page (EvView *view,
+ int width,
+ int height,
+ int vsb_width,
+ int hsb_height)
+{
+ GtkBorder border;
+ gint doc_width, doc_height;
+ gdouble scale;
+ gint other_page;
+
+ other_page = view->current_page ^ 1;
+
+ /* Find the largest of the two. */
+ ev_page_cache_get_size (view->page_cache,
+ view->current_page,
+ 1.0,
+ &doc_width, &doc_height);
+
+ if (other_page < ev_page_cache_get_n_pages (view->page_cache)) {
+ gint width_2, height_2;
+ ev_page_cache_get_size (view->page_cache,
+ other_page,
+ 1.0,
+ &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, doc_width, doc_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)
+ scale = zoom_for_size_fit_width (doc_width, doc_height, width, height, vsb_width);
+ else if (view->sizing_mode == EV_SIZING_BEST_FIT)
+ scale = zoom_for_size_best_fit (doc_width, doc_height, width, height, vsb_width, hsb_height);
+ else
+ g_assert_not_reached ();
+
+ ev_view_set_zoom (view, scale, FALSE);
+}
+
+static void
+ev_view_zoom_for_size_single_page (EvView *view,
+ int width,
+ int height,
+ int vsb_width,
+ int hsb_height)
+{
+ int doc_width, doc_height;
+ GtkBorder border;
+ gdouble scale;
+
+ ev_page_cache_get_size (view->page_cache,
+ view->current_page,
+ 1.0,
+ &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)
+ scale = zoom_for_size_fit_width (doc_width, doc_height, width, height, vsb_width);
+ else if (view->sizing_mode == EV_SIZING_BEST_FIT)
+ scale = zoom_for_size_best_fit (doc_width, doc_height, width, height, vsb_width, hsb_height);
+ else
+ g_assert_not_reached ();
+
+ ev_view_set_zoom (view, scale, FALSE);
+}
+
+void
+ev_view_set_zoom_for_size (EvView *view,
+ int width,
+ int height,
+ int vsb_width,
+ int hsb_height)
+{
+ 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->presentation)
+ ev_view_zoom_for_size_presentation (view, width, height);
+ else if (view->continuous && view->dual_page)
+ ev_view_zoom_for_size_continuous_and_dual_page (view, width, height, vsb_width, hsb_height);
+ else if (view->continuous)
+ ev_view_zoom_for_size_continuous (view, width, height, vsb_width, hsb_height);
+ else if (view->dual_page)
+ ev_view_zoom_for_size_dual_page (view, width, height, vsb_width, hsb_height);
+ else
+ ev_view_zoom_for_size_single_page (view, width, height, vsb_width, hsb_height);
+}
+
+/*** Status text messages ***/
+
+const char *
+ev_view_get_status (EvView *view)
+{
+ g_return_val_if_fail (EV_IS_VIEW (view), NULL);
+
+ return view->status;
}
-static char *
-ev_view_get_find_status_message (EvView *view)
+static void
+ev_view_set_status (EvView *view, const char *message)
{
- if (view->find_results->len == 0) {
- if (view->find_percent_complete >= (1.0 - 1e-10)) {
- return g_strdup (_("Not found"));
- } else {
- return g_strdup_printf (_("%3d%% remaining to search"),
- (int) ((1.0 - view->find_percent_complete) * 100));
- }
- } else if (view->results_on_this_page == 0) {
- g_assert (view->next_page_with_result != 0);
- return g_strdup_printf (_("Found on page %d"),
- view->next_page_with_result);
- } else {
- return g_strdup_printf (_("%d found on this page"),
- view->results_on_this_page);
+ g_return_if_fail (EV_IS_VIEW (view));
+
+ if (message != view->status) {
+ g_free (view->status);
+ view->status = g_strdup (message);
+ g_object_notify (G_OBJECT (view), "status");
}
}
static void
-update_find_results (EvView *view)
+update_find_status_message (EvView *view)
{
- const EvFindResult *results;
- int i;
- int on_this_page;
- int next_page_with_result;
- int earliest_page_with_result;
- int current_page;
- gboolean counts_changed;
-
- results = (EvFindResult*) view->find_results->data;
- current_page = ev_document_get_page (view->document);
- next_page_with_result = 0;
- on_this_page = 0;
- earliest_page_with_result = 0;
-
- i = 0;
- while (i < view->find_results->len) {
- if (results[i].page_num == current_page) {
- ++on_this_page;
+ char *message;
+
+ if (view->current_page == view->find_page) {
+ int results;
+
+ results = ev_document_find_get_n_results
+ (EV_DOCUMENT_FIND (view->document),
+ view->current_page);
+ /* TRANS: Sometimes this could be better translated as
+ "%d hit(s) on this page". Therefore this string
+ contains plural cases. */
+ message = g_strdup_printf (ngettext ("%d found on this page",
+ "%d found on this page",
+ results),
+ results);
+ } else {
+ double percent;
+
+ percent = ev_document_find_get_progress
+ (EV_DOCUMENT_FIND (view->document));
+ if (percent >= (1.0 - 1e-10)) {
+ message = g_strdup (_("Not found"));
} else {
- int delta = results[i].page_num - current_page;
-
- if (delta > 0 && /* result on later page */
- (next_page_with_result == 0 ||
- results[i].page_num < next_page_with_result))
- next_page_with_result = results[i].page_num;
-
- if (delta < 0 && /* result on a previous page */
- (earliest_page_with_result == 0 ||
- results[i].page_num < earliest_page_with_result))
- earliest_page_with_result = results[i].page_num;
+ message = g_strdup_printf (_("%3d%% remaining to search"),
+ (int) ((1.0 - percent) * 100));
}
- ++i;
- }
-
- /* If earliest page is just the current page, there is no earliest page */
- if (earliest_page_with_result == current_page)
- earliest_page_with_result = 0;
-
- /* If no next page, then wrap and the wrapped page is the next page */
- if (next_page_with_result == 0)
- next_page_with_result = earliest_page_with_result;
- counts_changed = FALSE;
- if (on_this_page != view->results_on_this_page ||
- next_page_with_result != view->next_page_with_result) {
- view->results_on_this_page = on_this_page;
- view->next_page_with_result = next_page_with_result;
- counts_changed = TRUE;
- }
-
- if (counts_changed ||
- view->find_results->len == 0) {
- char *message;
-
- message = ev_view_get_find_status_message (view);
- ev_view_set_find_status (view, message);
- g_free (message);
}
+ ev_view_set_find_status (view, message);
}
-static void
-found_results_callback (EvDocument *document,
- const EvFindResult *results,
- int n_results,
- double percent_complete,
- void *data)
-{
- EvView *view = EV_VIEW (data);
-
- g_array_set_size (view->find_results, 0);
-
- if (n_results > 0)
- g_array_append_vals (view->find_results,
- results, n_results);
-
-#if 0
- {
- int i;
-
- g_printerr ("%d results %d%%: ", n_results,
- (int) (percent_complete * 100));
- i = 0;
- while (i < n_results) {
- g_printerr ("%d ", results[i].page_num);
- ++i;
- }
- g_printerr ("\n");
- }
-#endif
+const char *
+ev_view_get_find_status (EvView *view)
+{
+ g_return_val_if_fail (EV_IS_VIEW (view), NULL);
- view->find_percent_complete = percent_complete;
- update_find_results (view);
-
- gtk_widget_queue_draw (GTK_WIDGET (view));
+ return view->find_status;
}
static void
-document_changed_callback (EvDocument *document,
- EvView *view)
+ev_view_set_find_status (EvView *view, const char *message)
{
- gtk_widget_queue_draw (GTK_WIDGET (view));
- ev_view_set_cursor (view, EV_VIEW_CURSOR_NORMAL);
-}
+ g_return_if_fail (EV_IS_VIEW (view));
-/*** Public API ***/
-
-GtkWidget*
-ev_view_new (void)
-{
- return g_object_new (EV_TYPE_VIEW, NULL);
+ g_free (view->find_status);
+ view->find_status = g_strdup (message);
+ g_object_notify (G_OBJECT (view), "find-status");
}
-void
-ev_view_set_document (EvView *view,
- EvDocument *document)
-{
- g_return_if_fail (EV_IS_VIEW (view));
-
- if (document != view->document) {
- if (view->document) {
- g_signal_handlers_disconnect_by_func (view->document,
- found_results_callback,
- view);
- g_array_set_size (view->find_results, 0);
- view->results_on_this_page = 0;
- view->next_page_with_result = 0;
+/*** Find ***/
- g_object_unref (view->document);
- }
+static void
+jump_to_find_result (EvView *view)
+{
+ EvDocumentFind *find = EV_DOCUMENT_FIND (view->document);
+ EvRectangle rect;
+ GdkRectangle view_rect;
+ int n_results;
+ int page = view->find_page;
- view->document = document;
+ n_results = ev_document_find_get_n_results (find, page);
- if (view->document) {
- g_object_ref (view->document);
- if (EV_IS_DOCUMENT_FIND (view->document))
- g_signal_connect (view->document,
- "found",
- G_CALLBACK (found_results_callback),
- view);
- g_signal_connect (view->document,
- "changed",
- G_CALLBACK (document_changed_callback),
- view);
- }
+ if (n_results > view->find_result) {
+ ev_document_find_get_result
+ (find, page, view->find_result, &rect);
- if (GTK_WIDGET_REALIZED (view))
- ev_document_set_target (view->document, view->bin_window);
-
- gtk_widget_queue_resize (GTK_WIDGET (view));
-
- g_signal_emit (view, page_changed_signal, 0);
+ doc_rect_to_view_rect (view, page, &rect, &view_rect);
+ ensure_rectangle_is_visible (view, &view_rect);
}
}
static void
-set_document_page (EvView *view, int page)
+jump_to_find_page (EvView *view)
{
- if (view->document) {
- int old_page = ev_document_get_page (view->document);
- int old_width, old_height;
+ int n_pages, i;
- ev_document_get_page_size (view->document,
- &old_width, &old_height);
+ n_pages = ev_page_cache_get_n_pages (view->page_cache);
- if (old_page != page) {
- ev_view_set_cursor (view, EV_VIEW_CURSOR_WAIT);
- ev_document_set_page (view->document, page);
- }
-
- if (old_page != ev_document_get_page (view->document)) {
- int width, height;
-
- g_signal_emit (view, page_changed_signal, 0);
-
- view->has_selection = FALSE;
- ev_document_get_page_size (view->document,
- &width, &height);
- if (width != old_width || height != old_height)
- gtk_widget_queue_resize (GTK_WIDGET (view));
+ for (i = 0; i < n_pages; i++) {
+ int has_results;
+ int page;
- view->find_percent_complete = 0.0;
- update_find_results (view);
+ page = i + view->find_page;
+ if (page >= n_pages) {
+ page = page - n_pages;
}
- }
-}
-
-static void
-go_to_link (EvView *view, EvLink *link)
-{
- EvLinkType type;
- const char *uri;
- int page;
- type = ev_link_get_link_type (link);
-
- switch (type) {
- case EV_LINK_TYPE_TITLE:
- break;
- case EV_LINK_TYPE_PAGE:
- page = ev_link_get_page (link);
- set_document_page (view, page);
+ has_results = ev_document_find_page_has_results
+ (EV_DOCUMENT_FIND (view->document), page);
+ if (has_results == -1) {
+ view->find_page = page;
break;
- case EV_LINK_TYPE_EXTERNAL_URI:
- uri = ev_link_get_uri (link);
- gnome_vfs_url_show (uri);
+ } else if (has_results == 1) {
+ ev_page_cache_set_current_page (view->page_cache, page);
+ jump_to_find_result (view);
break;
+ }
}
}
-void
-ev_view_go_to_link (EvView *view, EvLink *link)
+gboolean
+ev_view_can_find_next (EvView *view)
{
- EvLinkType type;
+ int n_results = 0;
- go_to_link (view, link);
+ if (EV_IS_DOCUMENT_FIND (view->document)) {
+ EvDocumentFind *find = EV_DOCUMENT_FIND (view->document);
- type = ev_link_get_link_type (link);
- if (type == EV_LINK_TYPE_PAGE) {
- ev_history_add_link (view->history, link);
+ n_results = ev_document_find_get_n_results (find, view->current_page);
}
+
+ return n_results > 0;
}
-static void
-go_to_index (EvView *view, int index)
+void
+ev_view_find_next (EvView *view)
{
- EvLink *link;
-
- link = ev_history_get_link_nth (view->history, index);
- g_return_if_fail (link != NULL);
+ EvPageCache *page_cache;
+ int n_results, n_pages;
+ EvDocumentFind *find = EV_DOCUMENT_FIND (view->document);
- go_to_link (view, link);
-}
+ page_cache = ev_document_get_page_cache (view->document);
+ n_results = ev_document_find_get_n_results (find, view->current_page);
-gboolean
-ev_view_can_go_back (EvView *view)
-{
- int index, n;
+ n_pages = ev_page_cache_get_n_pages (page_cache);
- if (view->history == NULL) {
- return FALSE;
- }
+ view->find_result++;
+
+ if (view->find_result >= n_results) {
+ view->find_result = 0;
+ view->find_page++;
- index = ev_history_get_current_index (view->history);
- n = ev_history_get_n_links (view->history);
+ if (view->find_page >= n_pages) {
+ view->find_page = 0;
+ }
- if (n > 0) {
- return index != MAX (0, index - 1);
+ jump_to_find_page (view);
} else {
- return FALSE;
+ jump_to_find_result (view);
+ gtk_widget_queue_draw (GTK_WIDGET (view));
}
}
void
-ev_view_go_back (EvView *view)
+ev_view_find_previous (EvView *view)
{
- int index, n;
+ int n_results, n_pages;
+ EvDocumentFind *find = EV_DOCUMENT_FIND (view->document);
+ EvPageCache *page_cache;
- g_return_if_fail (EV_IS_HISTORY (view->history));
+ page_cache = ev_document_get_page_cache (view->document);
- index = ev_history_get_current_index (view->history);
- n = ev_history_get_n_links (view->history);
+ n_results = ev_document_find_get_n_results (find, view->current_page);
- if (n > 0) {
- index = MAX (0, index - 1);
- ev_history_set_current_index (view->history, index);
- }
-}
+ n_pages = ev_page_cache_get_n_pages (page_cache);
-gboolean
-ev_view_can_go_forward (EvView *view)
-{
- int index, n;
+ view->find_result--;
- if (view->history == NULL) {
- return FALSE;
- }
+ if (view->find_result < 0) {
+ view->find_result = 0;
+ view->find_page--;
- index = ev_history_get_current_index (view->history);
- n = ev_history_get_n_links (view->history);
+ if (view->find_page < 0) {
+ view->find_page = n_pages - 1;
+ }
- if (n > 0) {
- return index != MIN (n - 1, index + 1);
+ jump_to_find_page (view);
} else {
- return FALSE;
+ jump_to_find_result (view);
+ gtk_widget_queue_draw (GTK_WIDGET (view));
}
}
-void
-ev_view_go_forward (EvView *view)
+/*** Selections ***/
+
+static void
+compute_selections (EvView *view, GdkRectangle *view_rect)
{
- int index, n;
+ int n_pages, i;
- g_return_if_fail (EV_IS_HISTORY (view->history));
+ clear_selection (view);
- index = ev_history_get_current_index (view->history);
- n = ev_history_get_n_links (view->history);
+ n_pages = ev_page_cache_get_n_pages (view->page_cache);
+ for (i = 0; i < n_pages; i++) {
+ GdkRectangle page_area;
+ GtkBorder border;
- if (n > 0) {
- index = MIN (n - 1, index + 1);
- ev_history_set_current_index (view->history, index);
- }
-}
+ if (get_page_extents (view, i, &page_area, &border)) {
+ GdkRectangle overlap;
+ if (gdk_rectangle_intersect (&page_area, view_rect, &overlap)) {
+ EvViewSelection *selection;
-void
-ev_view_set_page (EvView *view,
- int page)
-{
- g_return_if_fail (EV_IS_VIEW (view));
- g_return_if_fail (EV_IS_HISTORY (view->history));
+ selection = g_new0 (EvViewSelection, 1);
+ selection->page = i;
+ view_rect_to_doc_rect (view, &overlap, &page_area,
+ &(selection->rect));
- set_document_page (view, page);
- ev_history_add_page (view->history, page);
+ view->selections = g_list_append
+ (view->selections, selection);
+ }
+ }
+ }
}
-int
-ev_view_get_page (EvView *view)
+static void
+clear_selection (EvView *view)
{
- if (view->document)
- return ev_document_get_page (view->document);
- else
- return 1;
+ g_list_foreach (view->selections, (GFunc)g_free, NULL);
+ view->selections = NULL;
}
-#define ZOOM_IN_FACTOR 1.2
-#define ZOOM_OUT_FACTOR (1.0/ZOOM_IN_FACTOR)
-
-#define MIN_SCALE 0.05409
-#define MAX_SCALE 18.4884
-static void
-ev_view_zoom (EvView *view,
- double factor,
- gboolean relative)
+void
+ev_view_select_all (EvView *view)
{
- double scale;
-
- if (relative)
- scale = view->scale * factor;
- else
- scale = factor;
+ int n_pages, i;
- view->scale = CLAMP (scale, MIN_SCALE, MAX_SCALE);
+ clear_selection (view);
- ev_document_set_scale (view->document, view->scale);
+ n_pages = ev_page_cache_get_n_pages (view->page_cache);
+ for (i = 0; i < n_pages; i++) {
+ int width, height;
+ EvViewSelection *selection;
- gtk_widget_queue_resize (GTK_WIDGET (view));
-}
+ ev_page_cache_get_size (view->page_cache,
+ i, 1.0, &width, &height);
-void
-ev_view_zoom_in (EvView *view)
-{
- ev_view_zoom (view, ZOOM_IN_FACTOR, TRUE);
-}
+ selection = g_new0 (EvViewSelection, 1);
+ selection->page = i;
+ selection->rect.x1 = selection->rect.y1 = 0;
+ selection->rect.x2 = width;
+ selection->rect.y2 = height;
-void
-ev_view_zoom_out (EvView *view)
-{
- ev_view_zoom (view, ZOOM_OUT_FACTOR, TRUE);
-}
+ view->selections = g_list_append (view->selections, selection);
+ }
-void
-ev_view_normal_size (EvView *view)
-{
- ev_view_zoom (view, 1.0, FALSE);
+ gtk_widget_queue_draw (GTK_WIDGET (view));
}
-void
-ev_view_best_fit (EvView *view)
+static char *
+get_selected_text (EvView *ev_view)
{
- double scale;
- int width, height;
+ GString *text;
+ GList *l;
- width = height = 0;
- ev_document_get_page_size (view->document, &width, &height);
+ text = g_string_new (NULL);
- scale = 1.0;
- if (width != 0 && height != 0) {
- double scale_w, scale_h;
+ ev_document_doc_mutex_lock ();
- scale_w = (double)GTK_WIDGET (view)->allocation.width * view->scale / width;
- scale_h = (double)GTK_WIDGET (view)->allocation.height * view->scale / height;
+ for (l = ev_view->selections; l != NULL; l = l->next) {
+ EvViewSelection *selection = (EvViewSelection *)l->data;
+ char *tmp;
- scale = (scale_w < scale_h) ? scale_w : scale_h;
+ tmp = ev_document_get_text (ev_view->document,
+ selection->page,
+ &selection->rect);
+ g_string_append (text, tmp);
+ g_free (tmp);
}
- ev_view_zoom (view, scale, FALSE);
+ ev_document_doc_mutex_unlock ();
+
+ return g_string_free (text, FALSE);
}
void
-ev_view_fit_width (EvView *view)
+ev_view_copy (EvView *ev_view)
{
- double scale = 1.0;
- int width;
+ GtkClipboard *clipboard;
+ char *text;
+
+ if (!ev_document_can_get_text (ev_view->document)) {
+ return;
+ }
+
+ text = get_selected_text (ev_view);
+ clipboard = gtk_widget_get_clipboard (GTK_WIDGET (ev_view),
+ GDK_SELECTION_CLIPBOARD);
+ gtk_clipboard_set_text (clipboard, text, -1);
+ g_free (text);
+}
- width = 0;
- ev_document_get_page_size (view->document, &width, NULL);
+static void
+ev_view_primary_get_cb (GtkClipboard *clipboard,
+ GtkSelectionData *selection_data,
+ guint info,
+ gpointer data)
+{
+ EvView *ev_view = EV_VIEW (data);
+ char *text;
- scale = 1.0;
- if (width != 0)
- scale = (double)GTK_WIDGET (view)->allocation.width * view->scale / width;
+ if (!ev_document_can_get_text (ev_view->document)) {
+ return;
+ }
- ev_view_zoom (view, scale, FALSE);
+ text = get_selected_text (ev_view);
+ gtk_selection_data_set_text (selection_data, text, -1);
+ g_free (text);
}
static void
-history_index_changed_cb (EvHistory *history,
- GParamSpec *pspec,
- EvView *view)
+ev_view_primary_clear_cb (GtkClipboard *clipboard,
+ gpointer data)
{
- int index;
+ EvView *view = EV_VIEW (data);
- index = ev_history_get_current_index (history);
- go_to_index (view, index);
+ clear_selection (view);
}
-void
-ev_view_set_history (EvView *view,
- EvHistory *history)
+static void
+ev_view_update_primary_selection (EvView *ev_view)
{
- if (view->history) {
- g_object_unref (view->history);
+ GtkClipboard *clipboard;
+
+ clipboard = gtk_widget_get_clipboard (GTK_WIDGET (ev_view),
+ GDK_SELECTION_PRIMARY);
+
+ if (ev_view->selections) {
+ if (!gtk_clipboard_set_with_owner (clipboard,
+ targets,
+ G_N_ELEMENTS (targets),
+ ev_view_primary_get_cb,
+ ev_view_primary_clear_cb,
+ G_OBJECT (ev_view)))
+ ev_view_primary_clear_cb (clipboard, ev_view);
+ } else {
+ if (gtk_clipboard_get_owner (clipboard) == G_OBJECT (ev_view))
+ gtk_clipboard_clear (clipboard);
}
+}
+
+/*** Cursor operations ***/
+
+static GdkCursor *
+ev_view_create_invisible_cursor(void)
+{
+ GdkBitmap *empty;
+ GdkColor black = { 0, 0, 0, 0 };
+ static char bits[] = { 0x00 };
- view->history = g_object_ref (history);
- ev_history_add_page (view->history, ev_view_get_page (view));
+ empty = gdk_bitmap_create_from_data (NULL, bits, 1, 1);
- g_signal_connect (view->history, "notify::index",
- G_CALLBACK (history_index_changed_cb),
- view);
+ return gdk_cursor_new_from_pixmap (empty, empty, &black, &black, 0, 0);
}
-const char *
-ev_view_get_status (EvView *view)
+static void
+ev_view_set_cursor (EvView *view, EvViewCursor new_cursor)
{
- g_return_val_if_fail (EV_IS_VIEW (view), NULL);
+ GdkCursor *cursor = NULL;
+ GdkDisplay *display;
+ GtkWidget *widget;
- return view->status;
+ if (view->cursor == new_cursor) {
+ return;
+ }
+
+ widget = gtk_widget_get_toplevel (GTK_WIDGET (view));
+ display = gtk_widget_get_display (widget);
+ view->cursor = new_cursor;
+
+ switch (new_cursor) {
+ case EV_VIEW_CURSOR_NORMAL:
+ gdk_window_set_cursor (widget->window, NULL);
+ 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 = ev_view_create_invisible_cursor ();
+ break;
+ case EV_VIEW_CURSOR_DRAG:
+ cursor = gdk_cursor_new_for_display (display, GDK_FLEUR);
+ break;
+ }
+
+ if (cursor) {
+ gdk_window_set_cursor (widget->window, cursor);
+ gdk_cursor_unref (cursor);
+ gdk_flush();
+ }
}
-const char *
-ev_view_get_find_status (EvView *view)
+void
+ev_view_hide_cursor (EvView *view)
{
- g_return_val_if_fail (EV_IS_VIEW (view), NULL);
+ ev_view_set_cursor (view, EV_VIEW_CURSOR_HIDDEN);
+}
- return view->find_status;
+void
+ev_view_show_cursor (EvView *view)
+{
+ ev_view_set_cursor (view, EV_VIEW_CURSOR_NORMAL);
}
+/*** Enum description for usage in signal ***/
+
+GType
+ev_sizing_mode_get_type (void)
+{
+ static GType etype = 0;
+ if (etype == 0) {
+ static const GEnumValue values[] = {
+ { EV_SIZING_FIT_WIDTH, "EV_SIZING_FIT_WIDTH", "fit-width" },
+ { EV_SIZING_BEST_FIT, "EV_SIZING_BEST_FIT", "best-fit" },
+ { EV_SIZING_FREE, "EV_SIZING_FREE", "free" },
+ { 0, NULL, NULL }
+ };
+ etype = g_enum_register_static ("EvSizingMode", values);
+ }
+ return etype;
+}