Use g_strdup_printf() instead of a fixed size buffer.
[hildon] / src / hildon-number-editor.c
index 7827734..994918e 100644 (file)
@@ -3,12 +3,12 @@
  *
  * Copyright (C) 2005, 2006 Nokia Corporation, all rights reserved.
  *
- * Contact: Michael Dominic Kostrzewa <michael.kostrzewa@nokia.com>
+ * Contact: Rodrigo Novo <rodrigo.novo@nokia.com>
  *
  * This library is free software; you can redistribute it and/or
  * modify it under the terms of the GNU Lesser General Public License
  * as published by the Free Software Foundation; version 2.1 of
- * the License.
+ * the License, or (at your option) any later version.
  *
  * This library is distributed in the hope that it will be useful, but
  * WITHOUT ANY WARRANTY; without even the implied warranty of
 
 /**
  * SECTION:hildon-number-editor
- * @short_description: A widget used to enter a number within a pre-defined range
+ * @short_description: A widget used to enter a number within a pre-defined range.
  *
  * HildonNumberEditor is used to enter a number from a specific range. 
  * There are two buttons to scroll the value in number field. 
  * Manual input is also possible.
+ *
+ * <note>
+ *   <para>
+ * #HildonNumberEditor has been deprecated since Hildon 2.2
+ * See <link linkend="hildon-migrating-number-widgets">Migrating Number Widgets</link>
+ * section to know how to migrate this deprecated widget.
+ *   </para>
+ * </note>
+ *
+ * <example>
+ * <title>HildonNumberEditor example</title>
+ * <programlisting>
+ * number_editor = hildon_number_editor_new (-250, 500);
+ * hildon_number_editor_set_range (number_editor, 0, 100);
+ * </programlisting>
+ * </example>
  */
 
+#undef                                          HILDON_DISABLE_DEPRECATED
+
 #ifdef                                          HAVE_CONFIG_H
 #include                                        <config.h>
 #endif
 
-#include                                        "hildon-number-editor.h"
-#include                                        <gdk/gdkkeysyms.h>
-#include                                        <gtk/gtk.h>
 #include                                        <string.h>
 #include                                        <stdio.h>
 #include                                        <stdlib.h>
+#include                                        <libintl.h>
+#include                                        <gdk/gdkkeysyms.h>
+
+#include                                        "hildon-number-editor.h"
 #include                                        "hildon-marshalers.h"
-#include                                        "hildon-composite-widget.h"
-#include                                        "hildon-input-mode-hint.h"
 #include                                        "hildon-defines.h"
 #include                                        "hildon-enum-types.h"
 #include                                        "hildon-banner.h"
-#include                                        <libintl.h>
 #include                                        "hildon-number-editor-private.h"
+#include                                        "hildon-private.h"
 
-#define                                         _(String) dgettext(PACKAGE, String)
+#define                                         _(String) dgettext("hildon-libs", String)
 
 /*Pixel spec defines*/
 #define                                         NUMBER_EDITOR_HEIGHT 30
@@ -89,6 +106,10 @@ hildon_number_editor_size_allocate              (GtkWidget *widget,
                                                  GtkAllocation *allocation);
 
 static gboolean
+hildon_number_editor_focus                      (GtkWidget *widget,
+                                                 GtkDirectionType direction);
+
+static gboolean
 hildon_number_editor_entry_keypress             (GtkWidget *widget, 
                                                  GdkEventKey *event,
                                                  gpointer data);
@@ -166,6 +187,13 @@ static GtkContainerClass*                       parent_class;
 
 static guint                                    HildonNumberEditor_signal[LAST_SIGNAL] = {0};
 
+/**
+ * hildon_number_editor_get_type:
+ *
+ * Returns GType for HildonNumberEditor.
+ *
+ * Returns: HildonNumberEditor type
+ */
 GType G_GNUC_CONST
 hildon_number_editor_get_type                   (void)
 {
@@ -204,9 +232,9 @@ hildon_number_editor_class_init                 (HildonNumberEditorClass *editor
 
     parent_class = g_type_class_peek_parent (editor_class);
 
-    widget_class->size_request = hildon_number_editor_size_request;
-    widget_class->size_allocate = hildon_number_editor_size_allocate;
-    widget_class->focus = hildon_composite_widget_focus;
+    widget_class->size_request              = hildon_number_editor_size_request;
+    widget_class->size_allocate             = hildon_number_editor_size_allocate;
+    widget_class->focus                     = hildon_number_editor_focus;
 
     editor_class->range_error = hildon_number_editor_range_error;
 
@@ -218,6 +246,11 @@ hildon_number_editor_class_init                 (HildonNumberEditorClass *editor
     gobject_class->set_property             = hildon_number_editor_set_property;
     gobject_class->get_property             = hildon_number_editor_get_property;
 
+    /**
+     * HildonNumberEditor:value:
+     *
+     * The current value of the number editor.
+     */
     g_object_class_install_property (gobject_class, PROP_VALUE,
             g_param_spec_int ("value",
                 "Value",
@@ -387,8 +420,10 @@ hildon_number_editor_init                       (HildonNumberEditor *editor)
             G_CALLBACK (hildon_number_editor_button_released),
             editor);
 
+#ifdef MAEMO_GTK 
     g_object_set (G_OBJECT (priv->num_entry),
-            "input-mode", HILDON_INPUT_MODE_HINT_NUMERIC, NULL);
+            "hildon-input-mode", HILDON_GTK_INPUT_MODE_NUMERIC, NULL);
+#endif
 
     gtk_widget_show (priv->num_entry);
     gtk_widget_show (priv->minus);
@@ -424,12 +459,12 @@ static void
 hildon_number_editor_real_set_value             (HildonNumberEditorPrivate *priv, 
                                                  gint value)
 {
-    /* FIXME: That looks REALLY bad */
-    gchar buffer[32];
+    gchar *buffer;
 
     /* Update text in entry to new value */
-    g_snprintf (buffer, sizeof (buffer), "%d", value);
+    buffer = g_strdup_printf ("%d", value);
     gtk_entry_set_text (GTK_ENTRY (priv->num_entry), buffer);
+    g_free (buffer);
 }
 
 static gboolean
@@ -464,7 +499,7 @@ hildon_number_editor_button_pressed             (GtkWidget *widget,
     /* Start repetition timer */
     if (! priv->button_event_id)
     {
-        do_mouse_timeout (editor);
+        change_numbers (editor, priv->button_type);
         priv->button_event_id = g_timeout_add (timeout,
                 (GSourceFunc) hildon_number_editor_start_timer,
                 editor);
@@ -482,7 +517,8 @@ hildon_number_editor_start_timer                (HildonNumberEditor *editor)
 
     priv = HILDON_NUMBER_EDITOR_GET_PRIVATE (editor);
     settings = gtk_settings_get_default ();
-    g_object_get (settings, "gtk-timeout-update", &timeout, NULL);
+    g_object_get (settings, "gtk-timeout-repeat", &timeout, NULL);
+    timeout *= 8;
 
     priv->button_event_id = g_timeout_add (timeout,
             (GSourceFunc) do_mouse_timeout,
@@ -593,23 +629,23 @@ hildon_number_editor_validate_value             (HildonNumberEditor *editor,
                  */
                 if (value > priv->end && (priv->end >= 0 || (priv->end < 0 && value >= 0)))
                 {
-                    error_code = MAXIMUM_VALUE_EXCEED;
+                    error_code = HILDON_NUMBER_EDITOR_ERROR_MAXIMUM_VALUE_EXCEED;
                     fixup_value = priv->end;
                 }
                 else if (value < priv->start && (priv->start < 0 || (priv->start >= 0 && value <= 0)))
                 {
-                    error_code = MINIMUM_VALUE_EXCEED;
+                    error_code = HILDON_NUMBER_EDITOR_ERROR_MINIMUM_VALUE_EXCEED;
                     fixup_value = priv->start;
                 }
             }
             else
             {
                 if (value > priv->end) {
-                    error_code = MAXIMUM_VALUE_EXCEED;
+                    error_code = HILDON_NUMBER_EDITOR_ERROR_MAXIMUM_VALUE_EXCEED;
                     fixup_value = priv->end;
                 }
                 else if (value < priv->start) {
-                    error_code = MINIMUM_VALUE_EXCEED;
+                    error_code = HILDON_NUMBER_EDITOR_ERROR_MINIMUM_VALUE_EXCEED;
                     fixup_value = priv->start;
                 }
             }
@@ -618,10 +654,10 @@ hildon_number_editor_validate_value             (HildonNumberEditor *editor,
            have plain '-', intermediate forms are allowed AND
            minimum bound is negative */
         else if (! allow_intermediate || strcmp (text, "-") != 0 || priv->start >= 0)
-            error_code = ERRONEOUS_VALUE;
+            error_code = HILDON_NUMBER_EDITOR_ERROR_ERRONEOUS_VALUE;
     }
     else if (! allow_intermediate)
-        error_code = ERRONEOUS_VALUE;
+        error_code = HILDON_NUMBER_EDITOR_ERROR_ERRONEOUS_VALUE;
 
     if (error_code != -1)
     {
@@ -739,6 +775,23 @@ hildon_number_editor_size_allocate              (GtkWidget *widget,
 }
 
 static gboolean
+hildon_number_editor_focus                      (GtkWidget *widget,
+                                                 GtkDirectionType direction)
+{
+  gboolean retval;
+  GtkDirectionType effective_direction;
+
+  g_assert (HILDON_IS_NUMBER_EDITOR (widget));
+
+  retval = hildon_private_composite_focus (widget, direction, &effective_direction);
+
+  if (retval == TRUE)
+    return GTK_WIDGET_CLASS (parent_class)->focus (widget, effective_direction);
+  else
+    return FALSE;
+}
+
+static gboolean
 hildon_number_editor_entry_focusout             (GtkWidget *widget, 
                                                  GdkEventFocus *event,
                                                  gpointer data)
@@ -807,15 +860,15 @@ hildon_number_editor_range_error                (HildonNumberEditor *editor,
     /* Construct error message */
     switch (type)
     {
-        case MAXIMUM_VALUE_EXCEED:
+        case HILDON_NUMBER_EDITOR_ERROR_MAXIMUM_VALUE_EXCEED:
             err_msg = g_strdup_printf (_("ckct_ib_maximum_value"), max, max);
             break;
 
-        case MINIMUM_VALUE_EXCEED:
+        case HILDON_NUMBER_EDITOR_ERROR_MINIMUM_VALUE_EXCEED:
             err_msg = g_strdup_printf (_("ckct_ib_minimum_value"), min, min);
             break;
 
-        case ERRONEOUS_VALUE:
+        case HILDON_NUMBER_EDITOR_ERROR_ERRONEOUS_VALUE:
             err_msg =
                 g_strdup_printf (_("ckct_ib_set_a_value_within_range"), min, max);
             break;
@@ -832,7 +885,6 @@ hildon_number_editor_range_error                (HildonNumberEditor *editor,
     return TRUE;
 }
 
-
 /**
  * hildon_number_editor_new:
  * @min: minimum accepted value