]> www.fi.muni.cz Git - evince.git/commitdiff
Changed to use page labels now. Rewrote completely because I didn't
authorJonathan Blandford <jrb@redhat.com>
Fri, 1 Apr 2005 07:03:05 +0000 (07:03 +0000)
committerJonathan Blandford <jrb@src.gnome.org>
Fri, 1 Apr 2005 07:03:05 +0000 (07:03 +0000)
Fri Apr  1 01:59:39 2005  Jonathan Blandford  <jrb@redhat.com>

        * shell/ev-page-action.c: Changed to use page labels now.  Rewrote
        completely because I didn't understand GtkActions.  I should
        read Marco's code more carefully in the future.

        * backend/ev-page-cache.h (ev_page_cache_set_page_label): New
        function

ChangeLog
backend/ev-page-cache.c
backend/ev-page-cache.h
shell/ev-page-action.c
shell/ev-page-action.h
shell/ev-view.c
shell/ev-window.c

index 86e0b3f85ea468030895a7d6b3be1c9a8ece4cbd..739365c44c1692b68e5ac625b38cd57ce4a933df 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,12 @@
+Fri Apr  1 01:59:39 2005  Jonathan Blandford  <jrb@redhat.com>
+
+       * shell/ev-page-action.c: Changed to use page labels now.  Rewrote
+       completely because I didn't understand GtkActions.  I should
+       read Marco's code more carefully in the future.
+
+       * backend/ev-page-cache.h (ev_page_cache_set_page_label): New
+       function
+
 2005-03-31  Kristian Høgsberg  <krh@redhat.com>
 
        * pdf/ev-poppler.cc (pdf_document_get_links): Update to work with
index 204239476f87c8b74da7f24ffc8f4eed8a3da5f0..08d1be459029c4414cc8e1879b703d7560328b44 100644 (file)
@@ -1,5 +1,7 @@
 #include "ev-page-cache.h"
 #include "ev-job-queue.h"
+#include <stdlib.h>
+#include <string.h>
 
 typedef struct _EvPageCacheInfo
 {
@@ -181,6 +183,45 @@ ev_page_cache_set_current_page (EvPageCache *page_cache,
        g_signal_emit (page_cache, signals[PAGE_CHANGED], 0, page);
 }
 
+gboolean
+ev_page_cache_set_page_label (EvPageCache *page_cache,
+                             const char  *page_label)
+{
+       gint i, page;
+       long value;
+       char *endptr = NULL;
+       
+       g_return_val_if_fail (EV_IS_PAGE_CACHE (page_cache), FALSE);
+       g_return_val_if_fail (page_label != NULL, FALSE);
+
+       /* First, look for a literal label match */
+       for (i = 0; i < page_cache->n_pages; i ++) {
+               if (page_cache->page_labels[i] != NULL &&
+                   ! strcmp (page_label, page_cache->page_labels[i])) {
+                       ev_page_cache_set_current_page (page_cache, i);
+                       return TRUE;
+               }
+       }
+
+       /* Next, parse the label, and see if the number fits */
+       value = strtol (page_label, &endptr, 10);
+       if (endptr[0] == '\0') {
+               /* Page number is an integer */
+               page = MIN (G_MAXINT, value);
+
+               /* convert from a page label to a page offset */
+               page --;
+               if (page >= 0 &&
+                   page < page_cache->n_pages &&
+                   page_cache->page_labels[page] == NULL) {
+                       ev_page_cache_set_current_page (page_cache, page);
+                       return TRUE;
+               }
+       }
+
+       return FALSE;
+}
+
 void
 ev_page_cache_set_link (EvPageCache *page_cache,
                        EvLink      *link)
index bf5e3d142c0e06d6897352f282fb6d8f5b703cbb..24f52623b59e2a2384c94b5731b083f2fd723ec6 100644 (file)
@@ -45,6 +45,8 @@ char          *ev_page_cache_get_page_label  (EvPageCache  *page_cache,
 gint           ev_page_cache_get_current_page (EvPageCache *page_cache);
 void           ev_page_cache_set_current_page (EvPageCache *page_cache,
                                               int          page);
+gboolean       ev_page_cache_set_page_label   (EvPageCache *page_cache,
+                                              const char  *page_label);
 void           ev_page_cache_set_link         (EvPageCache *page_cache,
                                               EvLink      *link);
 gboolean       ev_page_cache_next_page        (EvPageCache *page_cache);         
index d6293c518b9f5ea9036f45cba18c23d0b8da807f..68cd71fd3ecd00b42f65d89fef4469cde4581243 100644 (file)
 #include <gtk/gtktoolitem.h>
 #include <gtk/gtklabel.h>
 #include <gtk/gtkhbox.h>
-#include <stdlib.h>
 
 struct _EvPageActionPrivate
 {
-       int current_page;
-       int total_pages;
+       EvPageCache *page_cache;
 };
 
-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
-};
-
-static guint signals[LAST_SIGNAL] = { 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))
 
-static void
-update_label (GtkAction *action, gpointer dummy, GtkWidget *proxy)
-{
-       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);
-}
-
-static void
-update_entry (EvPageAction *page_action, GtkWidget *entry)
-{
-       char *text;
+enum {
+       PROP_0,
+       PROP_PAGE_CACHE,
+};
 
-       text = g_strdup_printf ("%d", page_action->priv->current_page);
-       gtk_entry_set_text (GTK_ENTRY (entry), text);
-       g_free (text);
-}
+#define ENTRY_DATA      "epa-entry"
+#define PAGE_CACHE_DATA "epa-page-cache"
+#define SIGNAL_ID_DATA  "epa-signal-id"
 
 static void
-sync_entry (GtkAction *action, gpointer dummy, GtkWidget *proxy)
+page_changed_cb (EvPageCache *page_cache,
+                gint         page,
+                GtkWidget   *proxy)
 {
-       EvPageAction *page_action = EV_PAGE_ACTION (action);
        GtkWidget *entry;
 
-       entry = GTK_WIDGET (g_object_get_data (G_OBJECT (proxy), "entry"));
-       update_entry (page_action, entry);
+       entry = GTK_WIDGET (g_object_get_data (G_OBJECT (proxy), ENTRY_DATA));
+       if (page_cache != NULL) {
+               gchar *page_label = ev_page_cache_get_page_label (page_cache, page);
+               gtk_entry_set_text (GTK_ENTRY (entry), page_label);
+               gtk_editable_set_position (GTK_EDITABLE (entry), -1);
+               g_free (page_label);
+       } else {
+               gtk_entry_set_text (GTK_ENTRY (entry), "");
+       }
 }
 
 static void
 activate_cb (GtkWidget *entry, GtkAction *action)
 {
-       EvPageAction *page_action = EV_PAGE_ACTION (action);
+       EvPageAction *page = EV_PAGE_ACTION (action);
+       EvPageCache *page_cache;
        const char *text;
-       char *endptr;
-       int page = -1;
 
        text = gtk_entry_get_text (GTK_ENTRY (entry));
-       if (text) {
-               long value;
-
-               value = strtol (text, &endptr, 10);
-               if (endptr[0] == '\0') {
-                       /* Page number is an integer */
-                       page = MIN (G_MAXINT, value);
-               }
+       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);
        }
-
-       if (page > 0 && page <= page_action->priv->total_pages) {
-               g_signal_emit (action, signals[GOTO_PAGE_SIGNAL], 0, page);
-       } else {
-               update_entry (page_action, entry);
-       }
-}
-
-static void
-entry_size_request_cb (GtkWidget      *entry,
-                      GtkRequisition *requisition,
-                      GtkAction      *action)
-{
-       PangoContext *context;
-       PangoFontMetrics *metrics;
-       int digit_width;
-
-       context = gtk_widget_get_pango_context (entry);
-       metrics = pango_context_get_metrics
-                       (context, entry->style->font_desc,
-                        pango_context_get_language (context));
-
-       digit_width = pango_font_metrics_get_approximate_digit_width (metrics);
-       digit_width = PANGO_SCALE * ((digit_width + PANGO_SCALE - 1) / PANGO_SCALE);
-
-       pango_font_metrics_unref (metrics);
-
-       /* Space for 4 digits. Probably 3 would be enough but it doesnt
-          seem to possible to calculate entry borders without using
-          gtk private info */
-       requisition->width = PANGO_PIXELS (digit_width * 4);
 }
 
 static GtkWidget *
 create_tool_item (GtkAction *action)
 {
-       GtkWidget *hbox, *entry, *item, *label;
+       GtkWidget *hbox, *entry, *item;
 
        hbox = gtk_hbox_new (FALSE, 6);
        gtk_container_set_border_width (GTK_CONTAINER (hbox), 6); 
@@ -156,38 +105,59 @@ create_tool_item (GtkAction *action)
        gtk_widget_show (item);
 
        entry = gtk_entry_new ();
-       g_signal_connect (entry, "size_request",
-                         G_CALLBACK (entry_size_request_cb),
-                         action);
-       g_object_set_data (G_OBJECT (item), "entry", entry);
+       gtk_entry_set_width_chars (GTK_ENTRY (entry), 5);
+       g_object_set_data (G_OBJECT (item), ENTRY_DATA, entry);
        gtk_widget_show (entry);
 
        g_signal_connect (entry, "activate",
                          G_CALLBACK (activate_cb),
                          action);
 
-       label = gtk_label_new ("");
-       g_object_set_data (G_OBJECT (item), "label", label);
-       update_label (action, NULL, item);
-       gtk_widget_show (label);
-
        gtk_box_pack_start (GTK_BOX (hbox), entry, FALSE, FALSE, 0);
-       gtk_box_pack_start (GTK_BOX (hbox), label, FALSE, FALSE, 0);
        gtk_container_add (GTK_CONTAINER (item), hbox);
 
        return item;
 }
 
+static void
+update_page_cache (EvPageAction *page, gpointer dummy, GtkWidget *proxy)
+{
+       EvPageCache *page_cache;
+       EvPageCache *old_page_cache;
+       guint signal_id;
+
+       page_cache = page->priv->page_cache;
+       old_page_cache = (EvPageCache *) g_object_get_data (G_OBJECT (proxy), PAGE_CACHE_DATA);
+       signal_id = GPOINTER_TO_INT (g_object_get_data (G_OBJECT (proxy), SIGNAL_ID_DATA));
+
+       /* clear the old signal */
+       if (signal_id > 0 && old_page_cache)
+               g_signal_handler_disconnect (old_page_cache, signal_id);
+       
+       if (page_cache != NULL) {
+               signal_id = g_signal_connect (page_cache,
+                                             "page-changed",
+                                             G_CALLBACK (page_changed_cb),
+                                             proxy);
+               /* 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);
+       }
+       g_object_set_data (G_OBJECT (proxy), PAGE_CACHE_DATA, page_cache);
+       g_object_set_data (G_OBJECT (proxy), SIGNAL_ID_DATA, GINT_TO_POINTER (signal_id));
+}
+
 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),
-                                        proxy, 0);
-               g_signal_connect_object (action, "notify::current-page",
-                                        G_CALLBACK (sync_entry),
+       if (GTK_IS_TOOL_ITEM (proxy)) {
+               g_signal_connect_object (action, "notify::page-cache",
+                                        G_CALLBACK (update_page_cache),
                                         proxy, 0);
        }
 
@@ -195,65 +165,79 @@ connect_proxy (GtkAction *action, GtkWidget *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)
-{
-       G_OBJECT_CLASS (ev_page_action_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;
+  
+       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;
+       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;
+       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_document_get_page_cache (document);
+       
+       g_object_set (page, "page-cache", page_cache, NULL);
 }
 
-void
-ev_page_action_set_total_pages (EvPageAction *page, int total_pages)
+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
@@ -262,7 +246,7 @@ 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;
 
@@ -270,36 +254,13 @@ ev_page_action_class_init (EvPageActionClass *class)
        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);
-
-       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));
-
        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_PAGE_CACHE,
+                                        g_param_spec_object ("page-cache",
+                                                             "Page Cache",
+                                                             "Current page cache",
+                                                             EV_TYPE_PAGE_CACHE,
+                                                             G_PARAM_READWRITE));
 
        g_type_class_add_private (object_class, sizeof (EvPageActionPrivate));
 }
index 1695cb05536b11bb9f86579c17b0338016f2737f..295c3589c361650eae4e7e9f3151de60f4c97b01 100644 (file)
@@ -23,6 +23,7 @@
 #define EV_PAGE_ACTION_H
 
 #include <gtk/gtkaction.h>
+#include <ev-document.h>
 
 G_BEGIN_DECLS
 
@@ -54,10 +55,9 @@ struct _EvPageActionClass
 };
 
 GType ev_page_action_get_type         (void);
-void  ev_page_action_set_total_pages  (EvPageAction *page_action,
-                                      int           total_pages);
-void  ev_page_action_set_current_page (EvPageAction *page_action,
-                                      int           current_page);
+void  ev_page_action_set_document     (EvPageAction *page_action,
+                                      EvDocument   *document);
+
 
 G_END_DECLS
 
index d1bce99fc8d9735714e5cb4d0ad50a74499ec674..80958817a904dd9f49b1f4f7752a22e786f2f18e 100644 (file)
@@ -1106,7 +1106,7 @@ ev_view_class_init (EvViewClass *class)
                                                              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",
index 96074054fc531d6baaf82a470196672cc87e0e1b..9990f61144cf390abfb877b47bc32ee1908a01f7 100644 (file)
@@ -479,17 +479,6 @@ update_window_title (EvDocument *document, GParamSpec *pspec, EvWindow *ev_windo
        g_free (title);
 }
 
-static void
-update_total_pages (EvWindow *ev_window)
-{
-       GtkAction *action;
-       int pages;
-
-       pages = ev_page_cache_get_n_pages (ev_window->priv->page_cache);
-       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);
-}
-
 /* This function assumes that ev_window just had ev_window->document set.
  */
 static gboolean
@@ -518,12 +507,6 @@ page_changed_cb (EvPageCache *page_cache,
                 gint         page,
                 EvWindow    *ev_window)
 {
-       GtkAction *action;
-
-       action = gtk_action_group_get_action
-               (ev_window->priv->action_group, PAGE_SELECTOR_ACTION);
-
-       ev_page_action_set_current_page (EV_PAGE_ACTION (action), page);
        update_action_sensitivity (ev_window);
 }
 
@@ -534,6 +517,7 @@ ev_window_setup_document (EvWindow *ev_window)
        EvDocument *document;
        EvView *view = EV_VIEW (ev_window->priv->view);
        EvSidebar *sidebar = EV_SIDEBAR (ev_window->priv->sidebar);
+       GtkAction *action;
 
        document = ev_window->priv->document;
        ev_window->priv->page_cache = ev_document_get_page_cache (ev_window->priv->document);
@@ -554,7 +538,8 @@ ev_window_setup_document (EvWindow *ev_window)
        ev_view_set_document (view, document);
 
        update_window_title (document, NULL, ev_window);
-       update_total_pages (ev_window);
+       action = gtk_action_group_get_action (ev_window->priv->action_group, PAGE_SELECTOR_ACTION);
+       ev_page_action_set_document (EV_PAGE_ACTION (action), document);
        update_action_sensitivity (ev_window);
 }
 
@@ -2073,13 +2058,6 @@ static GtkRadioActionEntry page_view_entries[] = {
          PAGE_MODE_CONTINUOUS_PAGE }
 };
 
-static void
-goto_page_cb (GtkAction *action, int page_number, EvWindow *ev_window)
-{
-       ev_page_cache_set_current_page (ev_window->priv->page_cache,
-                                       page_number);
-}
-
 static void
 drag_data_received_cb (GtkWidget *widget, GdkDragContext *context,
                       gint x, gint y, GtkSelectionData *selection_data,
@@ -2108,8 +2086,6 @@ register_custom_actions (EvWindow *window, GtkActionGroup *group)
                               "label", _("Page"),
                               "tooltip", _("Select Page"),
                               NULL);
-       g_signal_connect (action, "goto_page",
-                         G_CALLBACK (goto_page_cb), window);
        gtk_action_group_add_action (group, action);
        g_object_unref (action);
 }