From dbe299f7b2412d318f0eda7c401feba68c8663e3 Mon Sep 17 00:00:00 2001 From: Marco Pesenti Gritti Date: Tue, 11 Jan 2005 11:21:52 +0000 Subject: Implement history dropdowns 2005-01-11 Marco Pesenti Gritti * shell/ev-history.c: (ev_history_get_property), (ev_history_set_property), (ev_history_class_init), (ev_history_add_page), (ev_history_set_current_index): * shell/ev-navigation-action.c: (ev_navigation_action_set_history), (activate_menu_item_cb), (new_history_menu_item), (build_menu), (ev_navigation_action_finalize): * shell/ev-navigation-action.h: * shell/ev-view.c: (ev_view_set_document), (ev_view_go_back), (ev_view_go_forward), (ev_view_get_find_status_message), (history_index_changed_cb), (ev_view_set_history): * shell/ev-view.h: * shell/ev-window.c: (update_total_pages), (ev_window_open), (update_current_page), (register_custom_actions): Implement history dropdowns --- (limited to 'shell') diff --git a/shell/ev-history.c b/shell/ev-history.c index a1e7954..08e2d1c 100644 --- a/shell/ev-history.c +++ b/shell/ev-history.c @@ -30,6 +30,11 @@ struct _EvHistoryPrivate int current_index; }; +enum { + PROP_0, + PROP_INDEX +}; + static void ev_history_init (EvHistory *history); static void ev_history_class_init (EvHistoryClass *class); @@ -66,14 +71,66 @@ ev_history_finalize (GObject *object) } static void +ev_history_get_property (GObject *object, guint prop_id, GValue *value, + GParamSpec *param_spec) +{ + EvHistory *self; + + self = EV_HISTORY (object); + + switch (prop_id) { + case PROP_INDEX: + g_value_set_int (value, self->priv->current_index); + break; + default: + G_OBJECT_WARN_INVALID_PROPERTY_ID (object, + prop_id, + param_spec); + break; + } +} + +static void +ev_history_set_property (GObject *object, guint prop_id, const GValue *value, + GParamSpec *param_spec) +{ + EvHistory *self; + + self = EV_HISTORY (object); + + switch (prop_id) { + case PROP_INDEX: + ev_history_set_current_index (self, g_value_get_int (value)); + break; + default: + G_OBJECT_WARN_INVALID_PROPERTY_ID (object, + prop_id, + param_spec); + break; + } +} + +static void ev_history_class_init (EvHistoryClass *class) { GObjectClass *object_class = G_OBJECT_CLASS (class); object_class->finalize = ev_history_finalize; + object_class->set_property = ev_history_set_property; + object_class->get_property = ev_history_get_property; parent_class = g_type_class_peek_parent (class); + g_object_class_install_property (object_class, + PROP_INDEX, + g_param_spec_int ("index", + "Current Index", + "The current index", + -1, + G_MAXINT, + 0, + G_PARAM_READWRITE)); + g_type_class_add_private (object_class, sizeof (EvHistoryPrivate)); } @@ -115,7 +172,7 @@ ev_history_add_page (EvHistory *history, int page) g_return_if_fail (EV_IS_HISTORY (history)); - title = g_strdup_printf (_("Page %d\n"), page); + title = g_strdup_printf (_("Page %d"), page); link = ev_link_new_page (title, page); g_free (title); @@ -156,6 +213,8 @@ ev_history_set_current_index (EvHistory *history, int index) g_return_if_fail (EV_IS_HISTORY (history)); history->priv->current_index = index; + + g_object_notify (G_OBJECT (history), "index"); } EvHistory * diff --git a/shell/ev-navigation-action.c b/shell/ev-navigation-action.c index 7b5269a..a4a13bc 100644 --- a/shell/ev-navigation-action.c +++ b/shell/ev-navigation-action.c @@ -37,6 +37,7 @@ struct _EvNavigationActionPrivate EvWindow *window; EvNavigationDirection direction; char *arrow_tooltip; + EvHistory *history; }; enum @@ -53,15 +54,87 @@ static GObjectClass *parent_class = NULL; G_DEFINE_TYPE (EvNavigationAction, ev_navigation_action, GTK_TYPE_ACTION) +#define MAX_LABEL_LENGTH 48 + #define EV_NAVIGATION_ACTION_GET_PRIVATE(object)(G_TYPE_INSTANCE_GET_PRIVATE ((object), EV_TYPE_NAVIGATION_ACTION, EvNavigationActionPrivate)) +void +ev_navigation_action_set_history (EvNavigationAction *action, + EvHistory *history) +{ + action->priv->history = history; + + g_object_add_weak_pointer (G_OBJECT (action->priv->history), + (gpointer *) &action->priv->history); +} + +static void +activate_menu_item_cb (GtkWidget *widget, EvNavigationAction *action) +{ + int index; + + g_return_if_fail (EV_IS_HISTORY (action->priv->history)); + + index = GPOINTER_TO_INT (g_object_get_data (G_OBJECT (widget), "index")); + ev_history_set_current_index (action->priv->history, index); +} + +static GtkWidget * +new_history_menu_item (EvNavigationAction *action, + EvLink *link, + int index) +{ + GtkLabel *label; + GtkWidget *item; + const char *title; + + title = ev_link_get_title (link); + item = gtk_image_menu_item_new_with_label (title); + g_object_set_data (G_OBJECT (item), "index", + GINT_TO_POINTER (index)); + + label = GTK_LABEL (GTK_BIN (item)->child); + gtk_label_set_ellipsize (label, PANGO_ELLIPSIZE_END); + gtk_label_set_max_width_chars (label, MAX_LABEL_LENGTH); + + g_signal_connect (item, "activate", + G_CALLBACK (activate_menu_item_cb), + action); + + gtk_widget_show (item); + + return item; +} + static GtkWidget * build_menu (EvNavigationAction *action) { GtkMenuShell *menu; + EvHistory *history = action->priv->history; + int start, end, i; menu = GTK_MENU_SHELL (gtk_menu_new ()); + if (history == NULL) { + return GTK_WIDGET (menu); + } + + if (action->priv->direction == EV_NAVIGATION_DIRECTION_BACK) { + start = 0; + end = ev_history_get_current_index (history); + } else { + start = ev_history_get_current_index (history) + 1; + end = ev_history_get_n_links (history); + } + + for (i = start; i < end; i++) { + EvLink *link = ev_history_get_link_nth (history, i); + GtkWidget *item; + + item = new_history_menu_item (action, link, i); + gtk_menu_shell_append (menu, item); + } + return GTK_WIDGET (menu); } @@ -122,6 +195,11 @@ ev_navigation_action_finalize (GObject *object) { EvNavigationAction *action = EV_NAVIGATION_ACTION (object); + if (action->priv->history) { + g_object_add_weak_pointer (G_OBJECT (action->priv->history), + (gpointer *) &action->priv->history); + } + g_free (action->priv->arrow_tooltip); parent_class->finalize (object); diff --git a/shell/ev-navigation-action.h b/shell/ev-navigation-action.h index 8bb7943..fe9b9d7 100644 --- a/shell/ev-navigation-action.h +++ b/shell/ev-navigation-action.h @@ -24,6 +24,8 @@ #include +#include "ev-history.h" + G_BEGIN_DECLS #define EV_TYPE_NAVIGATION_ACTION (ev_navigation_action_get_type ()) @@ -56,7 +58,9 @@ struct _EvNavigationActionClass GtkActionClass parent_class; }; -GType ev_navigation_action_get_type (void); +GType ev_navigation_action_get_type (void); +void ev_navigation_action_set_history (EvNavigationAction *action, + EvHistory *history); G_END_DECLS diff --git a/shell/ev-view.c b/shell/ev-view.c index cf2c469..4750b4d 100644 --- a/shell/ev-view.c +++ b/shell/ev-view.c @@ -28,7 +28,6 @@ #include "ev-marshal.h" #include "ev-view.h" #include "ev-document-find.h" -#include "ev-history.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)) @@ -905,12 +904,6 @@ ev_view_set_document (EvView *view, if (old_page != ev_view_get_page (view)) g_signal_emit (view, page_changed_signal, 0); - - if (view->history) { - g_object_unref (view->history); - } - view->history = ev_history_new (); - ev_history_add_page (view->history, ev_view_get_page (view)); } } @@ -975,7 +968,6 @@ ev_view_go_back (EvView *view) if (n > 0) { index = MAX (0, index - 1); ev_history_set_current_index (view->history, index); - go_to_index (view, index); } } @@ -992,7 +984,6 @@ ev_view_go_forward (EvView *view) if (n > 0) { index = MIN (n - 1, index + 1); ev_history_set_current_index (view->history, index); - go_to_index (view, index); } } @@ -1117,3 +1108,30 @@ ev_view_get_find_status_message (EvView *view) view->results_on_this_page); } } + +static void +history_index_changed_cb (EvHistory *history, + GParamSpec *pspec, + EvView *view) +{ + 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); +} diff --git a/shell/ev-view.h b/shell/ev-view.h index 9390263..97eb983 100644 --- a/shell/ev-view.h +++ b/shell/ev-view.h @@ -24,6 +24,7 @@ #include "ev-document.h" #include "ev-link.h" +#include "ev-history.h" G_BEGIN_DECLS @@ -39,6 +40,8 @@ GType ev_view_get_type (void) G_GNUC_CONST; GtkWidget* ev_view_new (void); void ev_view_set_document (EvView *view, EvDocument *document); +void ev_view_set_history (EvView *view, + EvHistory *history); /* Clipboard */ void ev_view_copy (EvView *view); diff --git a/shell/ev-window.c b/shell/ev-window.c index 64b80c3..443fff0 100644 --- a/shell/ev-window.c +++ b/shell/ev-window.c @@ -75,13 +75,16 @@ struct _EvWindowPrivate { guint help_message_cid; GtkWidget *exit_fullscreen_popup; char *uri; - GtkAction *page_action; EvDocument *document; gboolean fullscreen_mode; }; +#define NAVIGATION_BACK_ACTION "NavigationBack" +#define NAVIGATION_FORWARD_ACTION "NavigationForward" +#define PAGE_SELECTOR_ACTION "PageSelector" + #if 0 /* enable these to add support for signals */ static guint ev_window_signals [N_SIGNALS] = { 0 }; @@ -282,12 +285,13 @@ update_window_title (EvDocument *document, GParamSpec *pspec, EvWindow *ev_windo static void update_total_pages (EvWindow *ev_window) { - EvPageAction *page_action; + GtkAction *action; int pages; pages = ev_document_get_n_pages (ev_window->priv->document); - page_action = EV_PAGE_ACTION (ev_window->priv->page_action); - ev_page_action_set_total_pages (page_action, pages); + action = gtk_action_group_get_action + (ev_window->priv->action_group, PAGE_SELECTOR_ACTION); + ev_page_action_set_total_pages (EV_PAGE_ACTION (action), pages); } void @@ -319,14 +323,31 @@ ev_window_open (EvWindow *ev_window, const char *uri) ev_window, 0); if (ev_document_load (document, uri, &error)) { + EvHistory *history; + EvView *view = EV_VIEW (ev_window->priv->view); + EvSidebar *sidebar = EV_SIDEBAR (ev_window->priv->sidebar); + GtkAction *action; + if (ev_window->priv->document) g_object_unref (ev_window->priv->document); ev_window->priv->document = document; - ev_view_set_document (EV_VIEW (ev_window->priv->view), - document); - ev_sidebar_set_document (EV_SIDEBAR (ev_window->priv->sidebar), - document); + ev_view_set_document (view, document); + ev_sidebar_set_document (sidebar, document); + + history = ev_history_new (); + ev_view_set_history (view, history); + g_object_unref (history); + + action = gtk_action_group_get_action + (ev_window->priv->action_group, NAVIGATION_BACK_ACTION); + ev_navigation_action_set_history + (EV_NAVIGATION_ACTION (action), history); + + action = gtk_action_group_get_action + (ev_window->priv->action_group, NAVIGATION_FORWARD_ACTION); + ev_navigation_action_set_history + (EV_NAVIGATION_ACTION (action), history); update_total_pages (ev_window); update_action_sensitivity (ev_window); @@ -1068,12 +1089,14 @@ disconnect_proxy_cb (GtkUIManager *ui_manager, GtkAction *action, static void update_current_page (EvWindow *ev_window) { - EvPageAction *page_action; int page; + GtkAction *action; + + action = gtk_action_group_get_action + (ev_window->priv->action_group, PAGE_SELECTOR_ACTION); page = ev_view_get_page (EV_VIEW (ev_window->priv->view)); - page_action = EV_PAGE_ACTION (ev_window->priv->page_action); - ev_page_action_set_current_page (page_action, page); + ev_page_action_set_current_page (EV_PAGE_ACTION (action), page); } static void @@ -1342,7 +1365,7 @@ register_custom_actions (EvWindow *window, GtkActionGroup *group) GtkAction *action; action = g_object_new (EV_TYPE_NAVIGATION_ACTION, - "name", "NavigationBack", + "name", NAVIGATION_BACK_ACTION, "label", _("Back"), "stock_id", GTK_STOCK_GO_BACK, "tooltip", _("Go back"), @@ -1356,7 +1379,7 @@ register_custom_actions (EvWindow *window, GtkActionGroup *group) g_object_unref (action); action = g_object_new (EV_TYPE_NAVIGATION_ACTION, - "name", "NavigationForward", + "name", NAVIGATION_FORWARD_ACTION, "label", _("Forward"), "stock_id", GTK_STOCK_GO_FORWARD, "tooltip", _("Go forward"), @@ -1369,13 +1392,12 @@ register_custom_actions (EvWindow *window, GtkActionGroup *group) g_object_unref (action); action = g_object_new (EV_TYPE_PAGE_ACTION, - "name", "PageSelector", + "name", PAGE_SELECTOR_ACTION, "label", _("Page"), "tooltip", _("Select Page"), NULL); g_signal_connect (action, "goto_page", G_CALLBACK (goto_page_cb), window); - window->priv->page_action = action; gtk_action_group_add_action (group, action); g_object_unref (action); } -- cgit v0.9.1