X-Git-Url: https://www.fi.muni.cz/~kas/git//home/kas/public_html/git/?a=blobdiff_plain;ds=sidebyside;f=shell%2Fev-page-action.c;h=cc3615abf091164041f99d7d59607e9f1bdda7f7;hb=f675370b1c2f1e29dfb3a3b97eda5d46f6a4a4ae;hp=057b501fefe3429b64b2d49bdd4a2299aebb6ec4;hpb=fa65a5177e3045d1ddd3f0c7d60b831a5134d57a;p=evince.git diff --git a/shell/ev-page-action.c b/shell/ev-page-action.c index 057b501f..cc3615ab 100644 --- a/shell/ev-page-action.c +++ b/shell/ev-page-action.c @@ -16,39 +16,24 @@ * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * - * $Id$ */ +#ifdef HAVE_CONFIG_H #include "config.h" +#endif -#include "ev-page-action.h" -#include "ev-window.h" -#include "ev-document-links.h" - -#include -#include -#include -#include -#include #include +#include -typedef struct _EvPageActionWidget EvPageActionWidget; -typedef struct _EvPageActionWidgetClass EvPageActionWidgetClass; -struct _EvPageActionWidget -{ - GtkToolItem parent; - - GtkWidget *entry; - EvPageCache *page_cache; - guint signal_id; - GtkTreeModel *filter_model; - GtkTreeModel *model; -}; +#include +#include -struct _EvPageActionWidgetClass -{ - GtkToolItemClass parent_class; -}; +#include "ev-page-action.h" +#include "ev-page-cache.h" +#include "ev-window.h" +#include "ev-document-links.h" +#include "ev-page-action-widget.h" +#include "ev-marshal.h" struct _EvPageActionPrivate { @@ -57,28 +42,16 @@ struct _EvPageActionPrivate }; -/* Widget we pass back */ -static GType ev_page_action_widget_get_type (void); -static void ev_page_action_widget_init (EvPageActionWidget *action_widget); -static void ev_page_action_widget_class_init (EvPageActionWidgetClass *action_widget); - -G_DEFINE_TYPE (EvPageActionWidget, ev_page_action_widget, GTK_TYPE_TOOL_ITEM) - -static void -ev_page_action_widget_init (EvPageActionWidget *action_widget) -{ - -} +static void ev_page_action_init (EvPageAction *action); +static void ev_page_action_class_init (EvPageActionClass *class); -static void -ev_page_action_widget_class_init (EvPageActionWidgetClass *action_widget) +enum { -} - - + ACTIVATE_LINK, + N_SIGNALS +}; -static void ev_page_action_init (EvPageAction *action); -static void ev_page_action_class_init (EvPageActionClass *class); +static guint signals[N_SIGNALS] = {0, }; G_DEFINE_TYPE (EvPageAction, ev_page_action, GTK_TYPE_ACTION) @@ -90,8 +63,23 @@ enum { PROP_MODEL, }; -/* user data to set on the widget. */ -#define EPA_FILTER_MODEL_DATA "epa-filter-model" +static void +update_pages_label (EvPageActionWidget *proxy, + gint page, + EvPageCache *page_cache) +{ + char *label_text; + gint n_pages; + + n_pages = page_cache ? ev_page_cache_get_n_pages (page_cache) : 0; + if (page_cache && ev_page_cache_has_nonnumeric_page_labels (page_cache)) { + label_text = g_strdup_printf (_("(%d of %d)"), page + 1, n_pages); + } else { + label_text = g_strdup_printf (_("of %d"), n_pages); + } + gtk_label_set_text (GTK_LABEL (proxy->label), label_text); + g_free (label_text); +} static void page_changed_cb (EvPageCache *page_cache, @@ -100,14 +88,23 @@ page_changed_cb (EvPageCache *page_cache, { g_assert (proxy); - if (page_cache != NULL) { - gchar *page_label = ev_page_cache_get_page_label (page_cache, page); + if (page_cache != NULL && page >= 0) { + gchar *page_label; + + gtk_entry_set_width_chars (GTK_ENTRY (proxy->entry), + CLAMP (ev_page_cache_get_max_label_chars (page_cache), + 6, 12)); + + page_label = ev_page_cache_get_page_label (page_cache, page); gtk_entry_set_text (GTK_ENTRY (proxy->entry), page_label); gtk_editable_set_position (GTK_EDITABLE (proxy->entry), -1); g_free (page_label); + } else { gtk_entry_set_text (GTK_ENTRY (proxy->entry), ""); } + + update_pages_label (proxy, page, page_cache); } static void @@ -116,40 +113,84 @@ activate_cb (GtkWidget *entry, GtkAction *action) EvPageAction *page = EV_PAGE_ACTION (action); EvPageCache *page_cache; const char *text; + gchar *page_label; + + EvLinkDest *link_dest; + EvLinkAction *link_action; + EvLink *link; + gchar *link_text; text = gtk_entry_get_text (GTK_ENTRY (entry)); page_cache = page->priv->page_cache; - if (! ev_page_cache_set_page_label (page_cache, text)) { - /* rest the entry to the current page if we were unable to - * change it */ - gchar *page_label = - ev_page_cache_get_page_label (page_cache, - ev_page_cache_get_current_page (page_cache)); - gtk_entry_set_text (GTK_ENTRY (entry), page_label); - gtk_editable_set_position (GTK_EDITABLE (entry), -1); - g_free (page_label); - } + + link_dest = ev_link_dest_new_page_label (text); + link_action = ev_link_action_new_dest (link_dest); + link_text = g_strdup_printf ("Page: %s", text); + link = ev_link_new (link_text, link_action); + + g_signal_emit (action, signals[ACTIVATE_LINK], 0, link); + + g_object_unref (link); + g_free (link_text); + + /* rest the entry to the current page if we were unable to + * change it */ + page_label = ev_page_cache_get_page_label (page_cache, + ev_page_cache_get_current_page (page_cache)); + gtk_entry_set_text (GTK_ENTRY (entry), page_label); + gtk_editable_set_position (GTK_EDITABLE (entry), -1); + g_free (page_label); +} + +static gboolean page_scroll_cb(GtkWidget *widget, GdkEventScroll *event, EvPageAction* action) +{ + gint pageno; + + pageno = ev_page_cache_get_current_page (action->priv->page_cache); + if ((event->direction == GDK_SCROLL_DOWN) && + (pageno < ev_page_cache_get_n_pages(action->priv->page_cache) - 1)) + pageno++; + if ((event->direction == GDK_SCROLL_UP) && (pageno > 0)) + pageno--; + ev_page_cache_set_current_page (action->priv->page_cache, pageno); + + return TRUE; } static GtkWidget * create_tool_item (GtkAction *action) { EvPageActionWidget *proxy; + GtkWidget *hbox; + AtkObject *obj; proxy = g_object_new (ev_page_action_widget_get_type (), NULL); gtk_container_set_border_width (GTK_CONTAINER (proxy), 6); gtk_widget_show (GTK_WIDGET (proxy)); + hbox = gtk_hbox_new (FALSE, 0); + gtk_box_set_spacing (GTK_BOX (hbox), 6); + proxy->entry = gtk_entry_new (); + obj = gtk_widget_get_accessible (proxy->entry); + atk_object_set_name (obj, "page-label-entry"); + + g_signal_connect(proxy->entry, "scroll-event",G_CALLBACK(page_scroll_cb),action); + gtk_widget_add_events(GTK_WIDGET(proxy->entry),GDK_BUTTON_MOTION_MASK); gtk_entry_set_width_chars (GTK_ENTRY (proxy->entry), 5); + gtk_box_pack_start (GTK_BOX (hbox), proxy->entry, FALSE, FALSE, 0); gtk_widget_show (proxy->entry); - g_signal_connect (proxy->entry, "activate", G_CALLBACK (activate_cb), action); - gtk_container_add (GTK_CONTAINER (proxy), proxy->entry); + proxy->label = gtk_label_new (NULL); + gtk_box_pack_start (GTK_BOX (hbox), proxy->label, FALSE, FALSE, 0); + gtk_widget_show (proxy->label); + + gtk_container_add (GTK_CONTAINER (proxy), hbox); + gtk_widget_show (hbox); return GTK_WIDGET (proxy); } @@ -167,10 +208,10 @@ update_page_cache (EvPageAction *page, GParamSpec *pspec, EvPageActionWidget *pr g_signal_handler_disconnect (proxy->page_cache, proxy->signal_id); if (page_cache != NULL) { - signal_id = g_signal_connect (page_cache, - "page-changed", - G_CALLBACK (page_changed_cb), - proxy); + signal_id = g_signal_connect_object (page_cache, + "page-changed", + G_CALLBACK (page_changed_cb), + proxy, 0); /* Set the initial value */ page_changed_cb (page_cache, ev_page_cache_get_current_page (page_cache), @@ -180,187 +221,26 @@ update_page_cache (EvPageAction *page, GParamSpec *pspec, EvPageActionWidget *pr signal_id = 0; page_changed_cb (NULL, 0, proxy); } - proxy->page_cache = page_cache; + ev_page_action_widget_set_page_cache (proxy, page_cache); proxy->signal_id = signal_id; } -static gboolean -build_new_tree_cb (GtkTreeModel *model, - GtkTreePath *path, - GtkTreeIter *iter, - gpointer data) -{ - GtkTreeModel *filter_model = GTK_TREE_MODEL (data); - EvLink *link; - - gtk_tree_model_get (model, iter, - EV_DOCUMENT_LINKS_COLUMN_LINK, &link, - -1); - - if (link && ev_link_get_link_type (link) == EV_LINK_TYPE_PAGE) { - GtkTreeIter filter_iter; - - gtk_list_store_append (GTK_LIST_STORE (filter_model), &filter_iter); - gtk_list_store_set (GTK_LIST_STORE (filter_model), &filter_iter, - 0, iter, - -1); - } - - return FALSE; -} - -static GtkTreeModel * -get_filter_model_from_model (GtkTreeModel *model) -{ - GtkTreeModel *filter_model; - - filter_model = - (GtkTreeModel *) g_object_get_data (G_OBJECT (model), EPA_FILTER_MODEL_DATA); - if (filter_model == NULL) { - filter_model = (GtkTreeModel *) gtk_list_store_new (1, GTK_TYPE_TREE_ITER); - - gtk_tree_model_foreach (model, - build_new_tree_cb, - filter_model); - g_object_set_data_full (G_OBJECT (model), EPA_FILTER_MODEL_DATA, filter_model, g_object_unref); - } - - return filter_model; -} - -static gboolean -match_selected_cb (GtkEntryCompletion *completion, - GtkTreeModel *filter_model, - GtkTreeIter *filter_iter, - EvPageActionWidget *proxy) -{ - EvLink *link; - GtkTreeIter *iter; - - gtk_tree_model_get (filter_model, filter_iter, - 0, &iter, - -1); - gtk_tree_model_get (proxy->model, iter, - EV_DOCUMENT_LINKS_COLUMN_LINK, &link, - -1); - ev_page_cache_set_link (proxy->page_cache, link); - - return TRUE; -} - - static void -display_completion_text (GtkCellLayout *cell_layout, - GtkCellRenderer *renderer, - GtkTreeModel *filter_model, - GtkTreeIter *filter_iter, - EvPageActionWidget *proxy) +activate_link_cb (EvPageActionWidget *proxy, EvLink *link, EvPageAction *action) { - EvLink *link; - GtkTreeIter *iter; - - gtk_tree_model_get (filter_model, filter_iter, - 0, &iter, - -1); - gtk_tree_model_get (proxy->model, iter, - EV_DOCUMENT_LINKS_COLUMN_LINK, &link, - -1); - - g_object_set (renderer, "text", ev_link_get_title (link), NULL); + g_signal_emit (action, signals[ACTIVATE_LINK], 0, link); } -static gboolean -match_completion (GtkEntryCompletion *completion, - const gchar *key, - GtkTreeIter *filter_iter, - EvPageActionWidget *proxy) -{ - EvLink *link; - GtkTreeIter *iter; - const gchar *text = NULL; - - gtk_tree_model_get (gtk_entry_completion_get_model (completion), - filter_iter, - 0, &iter, - -1); - gtk_tree_model_get (proxy->model, iter, - EV_DOCUMENT_LINKS_COLUMN_LINK, &link, - -1); - - - if (link) - text = ev_link_get_title (link); - - if (text && key ) { - gchar *normalized_text; - gchar *normalized_key; - gchar *case_normalized_text; - gchar *case_normalized_key; - gboolean retval = FALSE; - - normalized_text = g_utf8_normalize (text, -1, G_NORMALIZE_ALL); - normalized_key = g_utf8_normalize (key, -1, G_NORMALIZE_ALL); - case_normalized_text = g_utf8_casefold (normalized_text, -1); - case_normalized_key = g_utf8_casefold (normalized_key, -1); - - if (strstr (case_normalized_text, case_normalized_key)) - retval = TRUE; - - g_free (normalized_text); - g_free (normalized_key); - g_free (case_normalized_text); - g_free (case_normalized_key); - - return retval; - } - - return FALSE; -} - - static void update_model (EvPageAction *page, GParamSpec *pspec, EvPageActionWidget *proxy) -{ +{ GtkTreeModel *model; - GtkTreeModel *filter_model; g_object_get (G_OBJECT (page), "model", &model, NULL); - if (model != NULL) { - /* Magik */ - GtkEntryCompletion *completion; - GtkCellRenderer *renderer; - - proxy->model = model; - filter_model = get_filter_model_from_model (model); - - completion = gtk_entry_completion_new (); - - /* popup-set-width is 2.7.0 only */ - g_object_set (G_OBJECT (completion), - "popup-set-width", FALSE, - "model", filter_model, - NULL); - - g_signal_connect (completion, "match-selected", G_CALLBACK (match_selected_cb), proxy); - gtk_entry_completion_set_match_func (completion, - (GtkEntryCompletionMatchFunc) match_completion, - proxy, NULL); - - /* Set up the layout */ - renderer = (GtkCellRenderer *) - g_object_new (GTK_TYPE_CELL_RENDERER_TEXT, - "ellipsize", PANGO_ELLIPSIZE_END, - "width_chars", 30, - NULL); - gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (completion), renderer, TRUE); - gtk_cell_layout_set_cell_data_func (GTK_CELL_LAYOUT (completion), - renderer, - (GtkCellLayoutDataFunc) display_completion_text, - proxy, NULL); - gtk_entry_set_completion (GTK_ENTRY (proxy->entry), completion); - } + + ev_page_action_widget_update_model (proxy, model); } static void @@ -370,13 +250,14 @@ connect_proxy (GtkAction *action, GtkWidget *proxy) g_signal_connect_object (action, "notify::page-cache", G_CALLBACK (update_page_cache), proxy, 0); - /* We only go through this whole rigmarole if we can set - * GtkEntryCompletion::popup-set-width */ - if (gtk_check_version (2, 7, 0) != NULL) { - g_signal_connect_object (action, "notify::model", - G_CALLBACK (update_model), - proxy, 0); - } + g_signal_connect (proxy, "activate_link", + G_CALLBACK (activate_link_cb), + action); + update_page_cache (EV_PAGE_ACTION (action), NULL, + EV_PAGE_ACTION_WIDGET (proxy)); + g_signal_connect_object (action, "notify::model", + G_CALLBACK (update_model), + proxy, 0); } GTK_ACTION_CLASS (ev_page_action_parent_class)->connect_proxy (action, proxy); @@ -457,7 +338,7 @@ ev_page_action_set_document (EvPageAction *page, EvDocument *document) EvPageCache *page_cache = NULL; if (document) - page_cache = ev_document_get_page_cache (document); + page_cache = ev_page_cache_get (document); g_object_set (page, "page-cache", page_cache, @@ -474,6 +355,22 @@ ev_page_action_set_model (EvPageAction *page_action, NULL); } +void +ev_page_action_grab_focus (EvPageAction *page_action) +{ + GSList *proxies; + + proxies = gtk_action_get_proxies (GTK_ACTION (page_action)); + for (; proxies != NULL; proxies = proxies->next) { + EvPageActionWidget *proxy; + + proxy = EV_PAGE_ACTION_WIDGET (proxies->data); + + if (GTK_WIDGET_MAPPED (GTK_WIDGET (proxy))) + gtk_widget_grab_focus (proxy->entry); + } +} + static void ev_page_action_init (EvPageAction *page) { @@ -494,6 +391,15 @@ ev_page_action_class_init (EvPageActionClass *class) action_class->create_tool_item = create_tool_item; action_class->connect_proxy = connect_proxy; + signals[ACTIVATE_LINK] = g_signal_new ("activate_link", + G_OBJECT_CLASS_TYPE (object_class), + G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION, + G_STRUCT_OFFSET (EvPageActionClass, activate_link), + NULL, NULL, + g_cclosure_marshal_VOID__OBJECT, + G_TYPE_NONE, 1, + G_TYPE_OBJECT); + g_object_class_install_property (object_class, PROP_PAGE_CACHE, g_param_spec_object ("page-cache",