#include <gtk/gtkselection.h>
#include <gtk/gtkclipboard.h>
#include <gdk/gdkkeysyms.h>
+#include <libgnomevfs/gnome-vfs-utils.h>
#include "ev-marshal.h"
#include "ev-view.h"
#define EV_IS_VIEW_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), EV_TYPE_VIEW))
#define EV_VIEW_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), EV_TYPE_VIEW, EvViewClass))
+enum {
+ PROP_0,
+ PROP_STATUS,
+ PROP_FIND_STATUS
+};
+
enum {
TARGET_STRING,
TARGET_TEXT,
{ "UTF8_STRING", 0, TARGET_UTF8_STRING },
};
+typedef enum {
+ EV_VIEW_CURSOR_NORMAL,
+ EV_VIEW_CURSOR_LINK,
+ EV_VIEW_CURSOR_WAIT
+} EvViewCursor;
+
struct _EvView {
GtkWidget parent_instance;
EvDocument *document;
+ EvHistory *history;
GdkWindow *bin_window;
+
+ char *status;
+ char *find_status;
int scroll_x;
int scroll_y;
+ gboolean pressed_button;
gboolean has_selection;
+ GdkPoint selection_start;
GdkRectangle selection;
+ EvViewCursor cursor;
GtkAdjustment *hadjustment;
GtkAdjustment *vadjustment;
static void ev_view_set_scroll_adjustments (EvView *view,
GtkAdjustment *hadjustment,
GtkAdjustment *vadjustment);
-
+
G_DEFINE_TYPE (EvView, ev_view, GTK_TYPE_WIDGET)
/*** Helper functions ***/
if (view->document)
g_object_unref (view->document);
+ if (view->history)
+ g_object_unref (view->history);
+
ev_view_set_scroll_adjustments (view, NULL, NULL);
g_array_free (view->find_results, TRUE);
requisition->width = 10;
requisition->height = 10;
}
+
+ requisition->width += 2;
+ requisition->height += 2;
}
}
}
}
-static void
-update_window_backgrounds (EvView *view)
-{
- GtkWidget *widget = GTK_WIDGET (view);
-
- if (GTK_WIDGET_REALIZED (view)) {
- gdk_window_set_background (view->bin_window,
- &widget->style->base[GTK_WIDGET_STATE (widget)]);
- }
-}
-
static void
ev_view_realize (GtkWidget *widget)
{
GDK_BUTTON_RELEASE_MASK |
GDK_SCROLL_MASK |
GDK_KEY_PRESS_MASK |
- GDK_BUTTON1_MOTION_MASK;
+ GDK_POINTER_MOTION_MASK;
view->bin_window = gdk_window_new (widget->window,
&attributes,
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 (view->document) {
ev_document_set_target (view->document, view->bin_window);
*/
gtk_widget_queue_resize (widget);
}
-
- update_window_backgrounds (view);
}
static void
GTK_WIDGET_CLASS (ev_view_parent_class)->unrealize (widget);
}
-static void
-ev_view_style_set (GtkWidget *widget,
- GtkStyle *previous_style)
-{
- update_window_backgrounds (EV_VIEW (widget));
-}
-
-static void
-ev_view_state_changed (GtkWidget *widget,
- GtkStateType previous_state)
-{
- update_window_backgrounds (EV_VIEW (widget));
-}
-
static guint32
ev_gdk_color_to_rgb (const GdkColor *color)
{
GdkEventExpose *event)
{
EvView *view = EV_VIEW (widget);
- int i;
- int current_page;
+ int i, current_page;
+ int x_offset, y_offset;
const EvFindResult *results;
if (view->document == NULL)
return;
-
+
+ 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[i].highlight_area.width,
results[i].highlight_area.height);
#endif
- if (results[i].page_num == current_page)
+ if (results[i].page_num == current_page) {
+ GdkRectangle highlight_area_fixed;
+ highlight_area_fixed.x = results[i].highlight_area.x + x_offset + 1;
+ highlight_area_fixed.y = results[i].highlight_area.y + y_offset + 1;
+ highlight_area_fixed.width = results[i].highlight_area.width;
+ highlight_area_fixed.height = results[i].highlight_area.height;
+
draw_rubberband (widget, view->bin_window,
- &results[i].highlight_area);
+ &highlight_area_fixed);
+ }
++i;
}
gtk_widget_grab_focus (widget);
}
+ view->pressed_button = event->button;
+
switch (event->button) {
case 1:
if (view->has_selection) {
gtk_widget_queue_draw (widget);
}
- view->selection.x = event->x;
- view->selection.y = event->y;
- view->selection.width = 0;
- view->selection.height = 0;
+ view->selection_start.x = event->x;
+ view->selection_start.y = event->y;
break;
}
return TRUE;
}
+static char *
+status_message_from_link (EvLink *link)
+{
+ EvLinkType type;
+ char *msg;
+ int page;
+
+ type = ev_link_get_link_type (link);
+
+ switch (type) {
+ case EV_LINK_TYPE_TITLE:
+ 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);
+ break;
+ case EV_LINK_TYPE_EXTERNAL_URI:
+ msg = g_strdup (ev_link_get_uri (link));
+ break;
+ default:
+ msg = NULL;
+ }
+
+ 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");
+}
+
+static void
+ev_view_set_cursor (EvView *view, EvViewCursor new_cursor)
+{
+ GdkCursor *cursor = NULL;
+ GdkDisplay *display;
+ GtkWidget *widget;
+
+ 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;
+ }
+
+ if (cursor) {
+ gdk_window_set_cursor (widget->window, cursor);
+ gdk_cursor_unref (cursor);
+ gdk_flush();
+ }
+}
+
static gboolean
ev_view_motion_notify_event (GtkWidget *widget,
GdkEventMotion *event)
{
EvView *view = EV_VIEW (widget);
- view->has_selection = TRUE;
- view->selection.x = MIN (view->selection.x, event->x);
- view->selection.y = MIN (view->selection.y, event->y);
- view->selection.width = ABS (view->selection.x - event->x) + 1;
- view->selection.height = ABS (view->selection.y - event->y) + 1;
+ 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;
+
+ link = ev_document_get_link (view->document, event->x, event->y);
+ if (link) {
+ char *msg;
+
+ msg = status_message_from_link (link);
+ ev_view_set_status (view, msg);
+ ev_view_set_cursor (view, EV_VIEW_CURSOR_LINK);
+ g_free (msg);
+
+ 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);
+ }
+ }
+ }
gtk_widget_queue_draw (widget);
{
EvView *view = EV_VIEW (widget);
- ev_view_update_primary_selection (view);
+ 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);
+ }
+ }
return FALSE;
}
}
}
+static void
+ev_view_set_property (GObject *object,
+ guint prop_id,
+ const GValue *value,
+ GParamSpec *pspec)
+{
+ switch (prop_id)
+ {
+ /* Read only */
+ case PROP_STATUS:
+ case PROP_FIND_STATUS:
+ break;
+ }
+}
+
+static void
+ev_view_get_property (GObject *object,
+ guint prop_id,
+ GValue *value,
+ GParamSpec *pspec)
+{
+ EvView *view = EV_VIEW (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;
+ }
+}
+
static void
ev_view_class_init (EvViewClass *class)
{
GtkBindingSet *binding_set;
object_class->finalize = ev_view_finalize;
+ object_class->set_property = ev_view_set_property;
+ object_class->get_property = ev_view_get_property;
widget_class->expose_event = ev_view_expose_event;
widget_class->button_press_event = ev_view_button_press_event;
widget_class->size_allocate = ev_view_size_allocate;
widget_class->realize = ev_view_realize;
widget_class->unrealize = ev_view_unrealize;
- widget_class->style_set = ev_view_style_set;
- widget_class->state_changed = ev_view_state_changed;
gtk_object_class->destroy = ev_view_destroy;
class->set_scroll_adjustments = ev_view_set_scroll_adjustments;
ev_marshal_VOID__NONE,
G_TYPE_NONE, 0);
- g_signal_new ("find-status-changed",
- G_OBJECT_CLASS_TYPE (object_class),
- G_SIGNAL_RUN_LAST,
- 0,
- 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,
GTK_TYPE_SCROLL_TYPE,
G_TYPE_BOOLEAN);
+ g_object_class_install_property (object_class,
+ PROP_STATUS,
+ g_param_spec_string ("status",
+ "Status Message",
+ "The status message",
+ NULL,
+ G_PARAM_READABLE));
+
+ g_object_class_install_property (object_class,
+ PROP_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);
add_scroll_binding (binding_set, GDK_Left, GTK_SCROLL_STEP_BACKWARD, TRUE);
static void
ev_view_init (EvView *view)
{
- static const GdkColor white = { 0, 0xffff, 0xffff, 0xffff };
-
GTK_WIDGET_SET_FLAGS (view, GTK_CAN_FOCUS);
view->scale = 1.0;
+ view->pressed_button = -1;
+ view->cursor = EV_VIEW_CURSOR_NORMAL;
- gtk_widget_modify_bg (GTK_WIDGET (view), GTK_STATE_NORMAL, &white);
-
view->find_results = g_array_new (FALSE,
FALSE,
sizeof (EvFindResult));
view->next_page_with_result = 0;
}
+static char *
+ev_view_get_find_status_message (EvView *view)
+{
+ 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);
+ }
+}
+
static void
update_find_results (EvView *view)
{
counts_changed = TRUE;
}
- /* If there are no results at all, then the
- * results of ev_view_get_find_status_message() will change
- * to reflect the percent_complete so we have to emit the signal
- */
if (counts_changed ||
view->find_results->len == 0) {
- g_signal_emit_by_name (view,
- "find-status-changed");
+ char *message;
+
+ message = ev_view_get_find_status_message (view);
+ ev_view_set_find_status (view, message);
+ g_free (message);
}
}
gtk_widget_queue_draw (GTK_WIDGET (view));
}
+static void
+document_changed_callback (EvDocument *document,
+ EvView *view)
+{
+ gtk_widget_queue_draw (GTK_WIDGET (view));
+ ev_view_set_cursor (view, EV_VIEW_CURSOR_NORMAL);
+}
+
/*** Public API ***/
GtkWidget*
return g_object_new (EV_TYPE_VIEW, NULL);
}
-static void
-document_changed_callback (EvDocument *document,
- EvView *view)
-{
- gtk_widget_queue_draw (GTK_WIDGET (view));
-}
-
void
ev_view_set_document (EvView *view,
EvDocument *document)
g_return_if_fail (EV_IS_VIEW (view));
if (document != view->document) {
- int old_page = ev_view_get_page (view);
-
if (view->document) {
g_signal_handlers_disconnect_by_func (view->document,
found_results_callback,
if (view->document) {
g_object_ref (view->document);
- g_signal_connect (view->document,
- "found",
- G_CALLBACK (found_results_callback),
- view);
+ 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),
gtk_widget_queue_resize (GTK_WIDGET (view));
- if (old_page != ev_view_get_page (view))
- g_signal_emit (view, page_changed_signal, 0);
+ g_signal_emit (view, page_changed_signal, 0);
}
}
-void
-ev_view_set_page (EvView *view,
- int page)
+static void
+set_document_page (EvView *view, int page)
{
if (view->document) {
int old_page = ev_document_get_page (view->document);
- if (old_page != page)
+ int old_width, old_height;
+
+ ev_document_get_page_size (view->document,
+ &old_width, &old_height);
+
+ 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));
+
view->find_percent_complete = 0.0;
update_find_results (view);
}
}
}
+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);
+ break;
+ case EV_LINK_TYPE_EXTERNAL_URI:
+ uri = ev_link_get_uri (link);
+ gnome_vfs_url_show (uri);
+ break;
+ }
+}
+
+void
+ev_view_go_to_link (EvView *view, EvLink *link)
+{
+ EvLinkType type;
+
+ go_to_link (view, link);
+
+ type = ev_link_get_link_type (link);
+ if (type == EV_LINK_TYPE_PAGE) {
+ ev_history_add_link (view->history, link);
+ }
+}
+
+static void
+go_to_index (EvView *view, int index)
+{
+ EvLink *link;
+
+ link = ev_history_get_link_nth (view->history, index);
+ g_return_if_fail (link != NULL);
+
+ go_to_link (view, link);
+}
+
+gboolean
+ev_view_can_go_back (EvView *view)
+{
+ int index, n;
+
+ if (view->history == NULL) {
+ return FALSE;
+ }
+
+ index = ev_history_get_current_index (view->history);
+ n = ev_history_get_n_links (view->history);
+
+ if (n > 0) {
+ return index != MAX (0, index - 1);
+ } else {
+ return FALSE;
+ }
+}
+
+void
+ev_view_go_back (EvView *view)
+{
+ int index, n;
+
+ g_return_if_fail (EV_IS_HISTORY (view->history));
+
+ index = ev_history_get_current_index (view->history);
+ n = ev_history_get_n_links (view->history);
+
+ if (n > 0) {
+ index = MAX (0, index - 1);
+ ev_history_set_current_index (view->history, index);
+ }
+}
+
+gboolean
+ev_view_can_go_forward (EvView *view)
+{
+ int index, n;
+
+ if (view->history == NULL) {
+ return FALSE;
+ }
+
+ index = ev_history_get_current_index (view->history);
+ n = ev_history_get_n_links (view->history);
+
+ if (n > 0) {
+ return index != MIN (n - 1, index + 1);
+ } else {
+ return FALSE;
+ }
+}
+
+void
+ev_view_go_forward (EvView *view)
+{
+ int index, n;
+
+ g_return_if_fail (EV_IS_HISTORY (view->history));
+
+ index = ev_history_get_current_index (view->history);
+ n = ev_history_get_n_links (view->history);
+
+ if (n > 0) {
+ index = MIN (n - 1, index + 1);
+ ev_history_set_current_index (view->history, index);
+ }
+}
+
+
+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));
+
+ set_document_page (view, page);
+ ev_history_add_page (view->history, page);
+}
+
int
ev_view_get_page (EvView *view)
{
ev_view_zoom (view, scale, FALSE);
}
-char*
-ev_view_get_find_status_message (EvView *view)
+static void
+history_index_changed_cb (EvHistory *history,
+ GParamSpec *pspec,
+ EvView *view)
{
- 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);
+ int index;
+
+ index = ev_history_get_current_index (history);
+ go_to_index (view, index);
+}
+
+void
+ev_view_set_history (EvView *view,
+ EvHistory *history)
+{
+ if (view->history) {
+ g_object_unref (view->history);
}
+
+ view->history = g_object_ref (history);
+ ev_history_add_page (view->history, ev_view_get_page (view));
+
+ g_signal_connect (view->history, "notify::index",
+ G_CALLBACK (history_index_changed_cb),
+ view);
}
+
+const char *
+ev_view_get_status (EvView *view)
+{
+ g_return_val_if_fail (EV_IS_VIEW (view), NULL);
+
+ return view->status;
+}
+
+const char *
+ev_view_get_find_status (EvView *view)
+{
+ g_return_val_if_fail (EV_IS_VIEW (view), NULL);
+
+ return view->find_status;
+}
+
+