From a3c03bdad8e8894092ffd6613354c9f408d5a8aa Mon Sep 17 00:00:00 2001 From: Marco Pesenti Gritti Date: Mon, 28 Feb 2005 10:53:52 +0000 Subject: Use an entry for the page control instead of spinbuttons 2005-02-28 Marco Pesenti Gritti * shell/ev-page-action.c: (update_entry), (sync_entry), (activate_cb), (entry_size_request_cb), (create_tool_item), (connect_proxy): Use an entry for the page control instead of spinbuttons --- (limited to 'shell/ev-page-action.c') diff --git a/shell/ev-page-action.c b/shell/ev-page-action.c index 9e32707..d6293c5 100644 --- a/shell/ev-page-action.c +++ b/shell/ev-page-action.c @@ -25,10 +25,11 @@ #include "ev-window.h" #include -#include +#include #include #include #include +#include struct _EvPageActionPrivate { @@ -72,45 +73,80 @@ update_label (GtkAction *action, gpointer dummy, GtkWidget *proxy) } static void -update_spin (GtkAction *action, gpointer dummy, GtkWidget *proxy) +update_entry (EvPageAction *page_action, GtkWidget *entry) { - EvPageAction *page = EV_PAGE_ACTION (action); - GtkWidget *spin; - int value; + char *text; - spin = GTK_WIDGET (g_object_get_data (G_OBJECT (proxy), "spin")); + text = g_strdup_printf ("%d", page_action->priv->current_page); + gtk_entry_set_text (GTK_ENTRY (entry), text); + g_free (text); +} - value = gtk_spin_button_get_value (GTK_SPIN_BUTTON (spin)); +static void +sync_entry (GtkAction *action, gpointer dummy, GtkWidget *proxy) +{ + EvPageAction *page_action = EV_PAGE_ACTION (action); + GtkWidget *entry; - if (value != page->priv->current_page ) - { - gtk_spin_button_set_value (GTK_SPIN_BUTTON (spin), - page->priv->current_page); - } + entry = GTK_WIDGET (g_object_get_data (G_OBJECT (proxy), "entry")); + update_entry (page_action, entry); } 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)); + EvPageAction *page_action = EV_PAGE_ACTION (action); + 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); + } + } - g_signal_emit (action, signals[GOTO_PAGE_SIGNAL], 0, value); + 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 -total_pages_changed_cb (EvPageAction *action, GParamSpec *pspec, - GtkSpinButton *spin) +entry_size_request_cb (GtkWidget *entry, + GtkRequisition *requisition, + GtkAction *action) { - gtk_spin_button_set_range (GTK_SPIN_BUTTON (spin), 1, - action->priv->total_pages); + 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, *spin, *item, *label; + GtkWidget *hbox, *entry, *item, *label; hbox = gtk_hbox_new (FALSE, 6); gtk_container_set_border_width (GTK_CONTAINER (hbox), 6); @@ -119,16 +155,15 @@ create_tool_item (GtkAction *action) item = GTK_WIDGET (gtk_tool_item_new ()); gtk_widget_show (item); - 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); + 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_widget_show (entry); - g_signal_connect (action, "notify::total-pages", - G_CALLBACK (total_pages_changed_cb), - spin); - g_signal_connect (spin, "value_changed", - G_CALLBACK (value_changed_cb), + g_signal_connect (entry, "activate", + G_CALLBACK (activate_cb), action); label = gtk_label_new (""); @@ -136,7 +171,7 @@ create_tool_item (GtkAction *action) update_label (action, NULL, item); gtk_widget_show (label); - gtk_box_pack_start (GTK_BOX (hbox), spin, FALSE, FALSE, 0); + 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); @@ -152,7 +187,7 @@ connect_proxy (GtkAction *action, GtkWidget *proxy) G_CALLBACK (update_label), proxy, 0); g_signal_connect_object (action, "notify::current-page", - G_CALLBACK (update_spin), + G_CALLBACK (sync_entry), proxy, 0); } -- cgit v0.9.1