]> www.fi.muni.cz Git - evince.git/blobdiff - shell/ev-page-action.c
Acquire lock document when drawing "Loading..." text so that only one
[evince.git] / shell / ev-page-action.c
index a13d61e2d05ff834f80762c30c7256e8dec058a2..7c28edc9a8c7ec9a4679b00208b46a41e5f83ed2 100644 (file)
  *  along with this program; if not, write to the Free Software
  *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  *
- *  $Id$
  */
 
 #include "config.h"
 
 #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"
 
 #include <glib/gi18n.h>
-#include <gtk/gtkspinbutton.h>
+#include <gtk/gtkentry.h>
 #include <gtk/gtktoolitem.h>
 #include <gtk/gtklabel.h>
 #include <gtk/gtkhbox.h>
+#include <string.h>
+#include <stdlib.h>
 
 struct _EvPageActionPrivate
 {
-       int current_page;
-       int total_pages;
+       EvPageCache *page_cache;
+       GtkTreeModel *model;
 };
 
-enum
-{
-       PROP_0,
-       PROP_CURRENT_PAGE,
-       PROP_TOTAL_PAGES
-};
 
 static void ev_page_action_init       (EvPageAction *action);
 static void ev_page_action_class_init (EvPageActionClass *class);
 
 enum
 {
-       GOTO_PAGE_SIGNAL,
-       LAST_SIGNAL
+       ACTIVATE_LINK,
+       ACTIVATE_LABEL,
+       N_SIGNALS
 };
 
-static guint signals[LAST_SIGNAL] = { 0 };
-
-static GObjectClass *parent_class = NULL;
+static guint signals[N_SIGNALS] = {0, };
 
 G_DEFINE_TYPE (EvPageAction, ev_page_action, GTK_TYPE_ACTION)
 
 #define EV_PAGE_ACTION_GET_PRIVATE(object)(G_TYPE_INSTANCE_GET_PRIVATE ((object), EV_TYPE_PAGE_ACTION, EvPageActionPrivate))
 
+enum {
+       PROP_0,
+       PROP_PAGE_CACHE,
+       PROP_MODEL,
+};
+
 static void
-update_label (GtkAction *action, gpointer dummy, GtkWidget *proxy)
+update_pages_label (EvPageActionWidget *proxy,
+                   gint                page,
+                   EvPageCache        *page_cache)
 {
-       EvPageAction *page = EV_PAGE_ACTION (action);
-       char *text;
-       GtkWidget *label;
-
-       label = GTK_WIDGET (g_object_get_data (G_OBJECT (proxy), "label"));
-
-       text = g_strdup_printf (_("of %d"), page->priv->total_pages);
-       gtk_label_set_text (GTK_LABEL (label), text);
+       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
-update_spin (GtkAction *action, gpointer dummy, GtkWidget *proxy)
+page_changed_cb (EvPageCache        *page_cache,
+                gint                page,
+                EvPageActionWidget *proxy)
 {
-       EvPageAction *page = EV_PAGE_ACTION (action);
-       GtkWidget *spin;
-       int value;
-
-       spin = GTK_WIDGET (g_object_get_data (G_OBJECT (proxy), "spin"));
-
-       value = gtk_spin_button_get_value (GTK_SPIN_BUTTON (spin));
-
-       if (value != page->priv->current_page)
-       {
-               gtk_spin_button_set_value (GTK_SPIN_BUTTON (spin),
-                                          page->priv->current_page);
+       g_assert (proxy);
+       
+       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
-value_changed_cb (GtkWidget *spin, GtkAction *action)
+activate_cb (GtkWidget *entry, GtkAction *action)
 {
-       int value;
-
-       value = gtk_spin_button_get_value (GTK_SPIN_BUTTON (spin));
-
-       g_signal_emit (action, signals[GOTO_PAGE_SIGNAL], 0, value);
+       EvPageAction *page = EV_PAGE_ACTION (action);
+       EvPageCache *page_cache;
+       const char *text;
+       gchar *page_label;
+       gint page_number;
+       gboolean changed;
+
+       text = gtk_entry_get_text (GTK_ENTRY (entry));
+       page_cache = page->priv->page_cache;
+
+       g_signal_emit (action, signals[ACTIVATE_LABEL], 0, text, &changed);
+
+       if (changed)
+               return;
+       
+       /* Check whether it's a valid page number */
+       page_number = atoi (text) - 1;
+       if (page_number >= 0 &&
+           page_number < ev_page_cache_get_n_pages (page_cache)) {
+               page_label = ev_page_cache_get_page_label (page_cache, page_number);
+               gtk_entry_set_text (GTK_ENTRY (entry), page_label);
+               gtk_editable_set_position (GTK_EDITABLE (entry), -1);
+
+               g_signal_emit (action, signals[ACTIVATE_LABEL], 0, page_label, &changed);
+               g_free (page_label);
+               
+               return;
+       }
+       
+       /* 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 GtkWidget *
 create_tool_item (GtkAction *action)
 {
-       GtkWidget *hbox, *spin, *item, *label;
+       EvPageActionWidget *proxy;
+       GtkWidget *hbox;
+
+       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 ();
+       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);
 
-       hbox = gtk_hbox_new (FALSE, 6);
-       gtk_container_set_border_width (GTK_CONTAINER (hbox), 6); 
+       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);
 
-       item = GTK_WIDGET (gtk_tool_item_new ());
-       gtk_widget_show (item);
+       return GTK_WIDGET (proxy);
+}
 
-       spin = gtk_spin_button_new_with_range (1, 9999, 1);
-       gtk_spin_button_set_digits (GTK_SPIN_BUTTON (spin), 0);
-       g_object_set_data (G_OBJECT (item), "spin", spin);
-       gtk_widget_show (spin);
+static void
+update_page_cache (EvPageAction *page, GParamSpec *pspec, EvPageActionWidget *proxy)
+{
+       EvPageCache *page_cache;
+       guint signal_id;
+
+       page_cache = page->priv->page_cache;
+
+       /* clear the old signal */
+       if (proxy->signal_id > 0 && proxy->page_cache)
+               g_signal_handler_disconnect (proxy->page_cache, proxy->signal_id);
+       
+       if (page_cache != NULL) {
+               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),
+                                proxy);
+       } else {
+               /* Or clear the entry */
+               signal_id = 0;
+               page_changed_cb (NULL, 0, proxy);
+       }
+       ev_page_action_widget_set_page_cache (proxy, page_cache);
+       proxy->signal_id = signal_id;
+}
 
-       g_signal_connect (spin, "value_changed",
-                         G_CALLBACK (value_changed_cb),
-                         action);
+static void
+activate_link_cb (EvPageActionWidget *proxy, EvLink *link, EvPageAction *action)
+{
+       g_signal_emit (action, signals[ACTIVATE_LINK], 0, link);
+}
 
-       label = gtk_label_new ("");
-       g_object_set_data (G_OBJECT (item), "label", label);
-       update_label (action, NULL, item);
-       gtk_widget_show (label);
+static void
+update_model (EvPageAction *page, GParamSpec *pspec, EvPageActionWidget *proxy)
+{      
+       GtkTreeModel *model;
 
-       gtk_box_pack_start (GTK_BOX (hbox), spin, FALSE, FALSE, 0);
-       gtk_box_pack_start (GTK_BOX (hbox), label, FALSE, FALSE, 0);
-       gtk_container_add (GTK_CONTAINER (item), hbox);
+       g_object_get (G_OBJECT (page),
+                     "model", &model,
+                     NULL);
 
-       return item;
+       ev_page_action_widget_update_model (proxy, model);
 }
 
 static void
 connect_proxy (GtkAction *action, GtkWidget *proxy)
 {
-       if (GTK_IS_TOOL_ITEM (proxy))
-       {
-               g_signal_connect_object (action, "notify::total-pages",
-                                        G_CALLBACK (update_label),
+       if (GTK_IS_TOOL_ITEM (proxy)) {
+               g_signal_connect_object (action, "notify::page-cache",
+                                        G_CALLBACK (update_page_cache),
                                         proxy, 0);
-               g_signal_connect_object (action, "notify::current-page",
-                                        G_CALLBACK (update_spin),
+               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 (parent_class)->connect_proxy (action, proxy);
+       GTK_ACTION_CLASS (ev_page_action_parent_class)->connect_proxy (action, proxy);
 }
 
 static void
-ev_page_action_init (EvPageAction *action)
+ev_page_action_dispose (GObject *object)
 {
-       action->priv = EV_PAGE_ACTION_GET_PRIVATE (action);
-}
+       EvPageAction *page = EV_PAGE_ACTION (object);
 
-static void
-ev_page_action_finalize (GObject *object)
-{
-       parent_class->finalize (object);
+       if (page->priv->page_cache) {
+               g_object_unref (page->priv->page_cache);
+               page->priv->page_cache = NULL;
+       }
+
+       G_OBJECT_CLASS (ev_page_action_parent_class)->dispose (object);
 }
 
 static void
-ev_page_action_set_property (GObject *object,
-                            guint prop_id,
+ev_page_action_set_property (GObject      *object,
+                            guint         prop_id,
                             const GValue *value,
-                            GParamSpec *pspec)
+                            GParamSpec   *pspec)
 {
-       EvPageAction *page = EV_PAGE_ACTION (object);
+       EvPageAction *page;
+       EvPageCache *page_cache;
+       GtkTreeModel *model;
+  
+       page = EV_PAGE_ACTION (object);
 
        switch (prop_id)
        {
-               case PROP_CURRENT_PAGE:
-                       page->priv->current_page = g_value_get_int (value);
-                       break;
-               case PROP_TOTAL_PAGES:
-                       page->priv->total_pages = g_value_get_int (value);
-                       break;
+       case PROP_PAGE_CACHE:
+               page_cache = page->priv->page_cache;
+               page->priv->page_cache = EV_PAGE_CACHE (g_value_dup_object (value));
+               if (page_cache)
+                       g_object_unref (page_cache);
+               break;
+       case PROP_MODEL:
+               model = page->priv->model;
+               page->priv->model = GTK_TREE_MODEL (g_value_dup_object (value));
+               if (model)
+                       g_object_unref (model);
+               break;
+       default:
+               G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
+               break;
        }
 }
 
 static void
-ev_page_action_get_property (GObject *object,
-                            guint prop_id,
-                            GValue *value,
+ev_page_action_get_property (GObject    *object,
+                            guint       prop_id,
+                            GValue     *value,
                             GParamSpec *pspec)
 {
-       EvPageAction *page = EV_PAGE_ACTION (object);
+       EvPageAction *page;
+  
+       page = EV_PAGE_ACTION (object);
 
        switch (prop_id)
        {
-               case PROP_CURRENT_PAGE:
-                       g_value_set_int (value, page->priv->current_page);
-                       break;
-               case PROP_TOTAL_PAGES:
-                       g_value_set_int (value, page->priv->total_pages);
-                       break;
+       case PROP_PAGE_CACHE:
+               g_value_set_object (value, page->priv->page_cache);
+               break;
+       case PROP_MODEL:
+               g_value_set_object (value, page->priv->model);
+               break;
+       default:
+               G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
+               break;
        }
 }
 
 void
-ev_page_action_set_current_page (EvPageAction *page, int current_page)
+ev_page_action_set_document (EvPageAction *page, EvDocument *document)
 {
-       g_object_set (page, "current-page", current_page, NULL);
+       EvPageCache *page_cache = NULL;
+
+       if (document)
+               page_cache = ev_page_cache_get (document);
+       
+       g_object_set (page,
+                     "page-cache", page_cache,
+                     "model", NULL,
+                     NULL);
 }
 
 void
-ev_page_action_set_total_pages (EvPageAction *page, int total_pages)
+ev_page_action_set_model (EvPageAction *page_action,
+                         GtkTreeModel *model)
+{
+       g_object_set (page_action,
+                     "model", model,
+                     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);
+               gtk_widget_grab_focus (proxy->entry);
+       }
+}
+
+static void
+ev_page_action_init (EvPageAction *page)
 {
-       g_object_set (page, "total-pages", total_pages, NULL);
+       page->priv = EV_PAGE_ACTION_GET_PRIVATE (page);
 }
 
 static void
@@ -218,46 +366,46 @@ ev_page_action_class_init (EvPageActionClass *class)
        GObjectClass *object_class = G_OBJECT_CLASS (class);
        GtkActionClass *action_class = GTK_ACTION_CLASS (class);
 
-       object_class->finalize = ev_page_action_finalize;
+       object_class->dispose = ev_page_action_dispose;
        object_class->set_property = ev_page_action_set_property;
        object_class->get_property = ev_page_action_get_property;
 
-       parent_class = g_type_class_peek_parent (class);
-
        action_class->toolbar_item_type = GTK_TYPE_TOOL_ITEM;
        action_class->create_tool_item = create_tool_item;
        action_class->connect_proxy = connect_proxy;
 
-       signals[GOTO_PAGE_SIGNAL] =
-               g_signal_new ("goto_page",
-                             G_OBJECT_CLASS_TYPE (object_class),
-                             G_SIGNAL_RUN_FIRST,
-                             G_STRUCT_OFFSET (EvPageActionClass, goto_page),
-                             NULL, NULL,
-                             g_cclosure_marshal_VOID__INT,
-                             G_TYPE_NONE,
-                             1,
-                             G_TYPE_INT);
+       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);
+       signals[ACTIVATE_LABEL] = g_signal_new ("activate_label",
+                                               G_OBJECT_CLASS_TYPE (object_class),
+                                               G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
+                                               G_STRUCT_OFFSET (EvPageActionClass, activate_label),
+                                               NULL, NULL,
+                                               ev_marshal_BOOLEAN__STRING,
+                                               G_TYPE_BOOLEAN, 1,
+                                               G_TYPE_STRING);
 
        g_object_class_install_property (object_class,
-                                        PROP_CURRENT_PAGE,
-                                        g_param_spec_int ("current-page",
-                                                          "Current Page",
-                                                          "The number of current page",
-                                                          0,
-                                                          G_MAXINT,
-                                                          0,
-                                                          G_PARAM_READWRITE));
+                                        PROP_PAGE_CACHE,
+                                        g_param_spec_object ("page-cache",
+                                                             "Page Cache",
+                                                             "Current page cache",
+                                                             EV_TYPE_PAGE_CACHE,
+                                                             G_PARAM_READWRITE));
 
        g_object_class_install_property (object_class,
-                                        PROP_TOTAL_PAGES,
-                                        g_param_spec_int ("total-pages",
-                                                          "Total Pages",
-                                                          "The total number of pages",
-                                                          0,
-                                                          G_MAXINT,
-                                                          0,
-                                                          G_PARAM_READWRITE));
+                                        PROP_MODEL,
+                                        g_param_spec_object ("model",
+                                                             "Model",
+                                                             "Current Model",
+                                                             GTK_TYPE_TREE_MODEL,
+                                                             G_PARAM_READWRITE));
 
        g_type_class_add_private (object_class, sizeof (EvPageActionPrivate));
 }