2008-08-28 Alejandro Pinheiro <apinheiro@igalia.com> * src/hildon-touch-selector...
[hildon] / src / hildon-date-editor.c
index 7c5b146..8bd4732 100644 (file)
@@ -1,4 +1,5 @@
-/*
+/* vim:set sw=4 expandtab cino=(0:
+ *
  * This file is a part of hildon
  *
  * Copyright (C) 2005, 2006 Nokia Corporation, all rights reserved.
  *
  * <example>
  * <programlisting>
- * gint y, m, d;
+ * guint y, m, d;
+ * GtkDialog *dialog;
+ * GtkWidget *date_editor;
  * <!-- -->
+ * dialog = GTK_DIALOG (gtk_dialog_new ());
  * date_editor = hildon_date_editor_new ();
  * <!-- -->
- * hildon_date_editor_get_date(date_editor, &amp;y, &amp;m, &amp;d);
+ * gtk_box_pack_start (GTK_BOX (dialog->vbox), gtk_label_new ("Choose a date"), FALSE, FALSE, 10);
+ * gtk_box_pack_start (GTK_BOX (dialog->vbox), date_editor, FALSE, FALSE, 10);
+ * gtk_dialog_add_button (dialog, "Close", GTK_RESPONSE_CANCEL);
+ * <!-- -->
+ * gtk_widget_show_all (GTK_WIDGET (dialog));
+ * gtk_dialog_run (dialog);
+ * <!-- -->
+ * hildon_date_editor_get_date (HILDON_DATE_EDITOR (date_editor), &amp;y, &amp;m, &amp;d);
+ * g_debug ("Date: &percnt;u-&percnt;u-&percnt;u", y, m, d);
  * <!-- -->
  * </programlisting>
  * </example>
 #include                                        <config.h>
 #endif
 
-#include                                        "hildon-date-editor.h"
-#include                                        <glib.h>
-#include                                        <gtk/gtk.h>
-#include                                        <gtk/gtkenums.h>
-#include                                        <gdk/gdkkeysyms.h>
 #include                                        <time.h>
 #include                                        <stdlib.h>
 #include                                        <stdio.h>
 #include                                        <string.h>
+#include                                        <libintl.h>
+#include                                        <gdk/gdkkeysyms.h>
+
+#include                                        "hildon-date-editor.h"
 #include                                        "hildon-calendar-popup.h"
 #include                                        "hildon-defines.h"
-#include                                        "hildon-private.h"
 #include                                        "hildon-marshalers.h"
 #include                                        "hildon-enum-types.h"
 #include                                        "hildon-time-editor.h"
 #include                                        "hildon-banner.h"
-#include                                        <libintl.h>
 #include                                        "hildon-date-editor-private.h"
+#include                                        "hildon-private.h"
 
 #define                                         _(string) dgettext("hildon-libs", string)
 
@@ -164,7 +174,9 @@ hildon_date_editor_size_allocate                (GtkWidget *widget,
 static void
 hildon_date_editor_size_request                 (GtkWidget *widget,
                                                  GtkRequisition *requisition);
-
+static gboolean
+hildon_date_editor_focus                        (GtkWidget *widget,
+                                                 GtkDirectionType direction);
 static gboolean
 hildon_date_editor_entry_select_all             (GtkWidget *widget);
 
@@ -234,7 +246,7 @@ hildon_date_editor_class_init                   (HildonDateEditorClass *editor_c
     gobject_class->get_property             = hildon_date_editor_get_property;
     widget_class->size_request              = hildon_date_editor_size_request;
     widget_class->size_allocate             = hildon_date_editor_size_allocate;
-    widget_class->focus                     = hildon_private_composite_focus;
+    widget_class->focus                     = hildon_date_editor_focus;
 
     container_class->forall                 = hildon_child_forall;
     GTK_OBJECT_CLASS(editor_class)->destroy = hildon_date_editor_destroy;
@@ -583,6 +595,7 @@ hildon_date_editor_set_property                 (GObject *object,
 
         case PROP_MIN_YEAR:
             val = g_value_get_uint (value);
+            priv->min_year = val;
             /* Clamp current year */
             if (hildon_date_editor_get_year (editor) < priv->min_year)
                 hildon_date_editor_set_year (editor, priv->min_year);
@@ -1072,10 +1085,13 @@ hildon_date_editor_entry_changed                (GtkEditable *ed,
 {
     GtkEntry *entry;
     gint error_code;
+    
     HildonDateEditorPrivate *priv;
+    priv = HILDON_DATE_EDITOR_GET_PRIVATE (HILDON_DATE_EDITOR (data));
 
     g_assert (GTK_IS_ENTRY (ed));
     g_assert (HILDON_IS_DATE_EDITOR (data));
+    g_assert (priv);
 
     entry = GTK_ENTRY (ed);
 
@@ -1085,11 +1101,11 @@ hildon_date_editor_entry_changed                (GtkEditable *ed,
         error_code = hildon_date_editor_entry_validate (GTK_WIDGET (entry), data);
         if (error_code == HILDON_DATE_TIME_ERROR_NO_ERROR)
         {
-            priv = HILDON_DATE_EDITOR_GET_PRIVATE (HILDON_DATE_EDITOR (data));
-            g_assert (priv);
             priv->skip_validation = TRUE;
             gtk_widget_child_focus (GTK_WIDGET (data), GTK_DIR_RIGHT);
         }
+    } else {
+        priv->skip_validation = FALSE;
     }
 }
 
@@ -1129,46 +1145,21 @@ hildon_date_editor_keypress                     (GtkWidget *widget,
 {
     HildonDateEditor *ed;
     HildonDateEditorPrivate *priv;
-    gint pos;
-    gboolean r;
 
     g_assert (HILDON_IS_DATE_EDITOR (data));
     g_assert (GTK_IS_ENTRY (widget));
 
     ed = HILDON_DATE_EDITOR (data);
-    priv = HILDON_DATE_EDITOR_GET_PRIVATE (ed);
-    pos = gtk_editable_get_position (GTK_EDITABLE (widget));
-    g_assert (priv);
-
-    /* Show error message in case the key pressed is not allowed 
-       (only digits and control characters are allowed )*/
-    if (!g_unichar_isdigit (event->keyval) && ! (event->keyval & 0xF000)) {
-        g_signal_emit (ed, date_editor_signals[DATE_ERROR], 0, HILDON_DATE_TIME_ERROR_INVALID_CHAR, &r);        
-        return TRUE;
-    }
 
     switch (event->keyval) {
-        case GDK_Left:
-            if (pos == 0) {
-                (void) gtk_widget_child_focus (GTK_WIDGET (data), GTK_DIR_LEFT);
-                return TRUE;
-            }
-            break;
-
-        case GDK_Right:
-            if (pos >= g_utf8_strlen (gtk_entry_get_text (GTK_ENTRY (widget)), -1)) {
-                (void) gtk_widget_child_focus (GTK_WIDGET (data), GTK_DIR_RIGHT);
-                return TRUE;
-            }
-            break;
         case GDK_Return:
         case GDK_ISO_Enter:
             /* Ignore return value, since we want to handle event at all times.
                otherwise vkb would popup when the keyrepeat starts. */
-            (void) hildon_date_editor_set_calendar_icon_state (ed, TRUE);
+            hildon_date_editor_set_calendar_icon_state (ed, TRUE);
             return TRUE;
-
         case GDK_Escape:
+            priv = HILDON_DATE_EDITOR_GET_PRIVATE (ed);
             priv->skip_validation = TRUE;
             break;
         default:
@@ -1296,6 +1287,7 @@ hildon_date_editor_size_allocate                (GtkWidget *widget,
     GtkRequisition req;
     GtkRequisition max_req;
     GList *iter;
+    gboolean rtl;
 
     g_assert (GTK_IS_WIDGET (widget));
     g_assert (allocation != NULL);
@@ -1303,6 +1295,7 @@ hildon_date_editor_size_allocate                (GtkWidget *widget,
     ed = HILDON_DATE_EDITOR (widget);
     priv = HILDON_DATE_EDITOR_GET_PRIVATE (ed);
 
+    rtl = (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL);
     widget->allocation = *allocation;
 
     gtk_widget_get_child_requisition (widget, &max_req);
@@ -1315,24 +1308,35 @@ hildon_date_editor_size_allocate                (GtkWidget *widget,
     f_alloc.x = img_alloc.x = allocation->x +
         MAX (allocation->width - max_req.width, 0) / 2;
 
-    /* allocate frame */
-    if (GTK_WIDGET_VISIBLE (priv->frame)) {
+    /* calculate allocations */
+    if (GTK_WIDGET_VISIBLE (widget)) {
+        /* allocate frame */
         gtk_widget_get_child_requisition (priv->frame, &req);
 
         f_alloc.width = req.width;
         f_alloc.height = max_req.height;
-        gtk_widget_size_allocate (priv->frame, &f_alloc);
-    }
 
-    /* allocate icon */
-    if (GTK_WIDGET_VISIBLE (priv->d_button_image)) {
+        /* allocate icon */
         gtk_widget_get_child_requisition (priv->d_button_image,
                 &req);
 
         img_alloc.x += f_alloc.width + HILDON_MARGIN_DEFAULT;
         img_alloc.width = req.width;
         img_alloc.height = max_req.height;
-        gtk_widget_size_allocate (priv->d_button_image, &img_alloc);
+
+        if (rtl)
+        {
+            img_alloc.x = f_alloc.x;
+            f_alloc.x  += img_alloc.width + HILDON_MARGIN_DEFAULT;
+        }
+
+        if (GTK_WIDGET_VISIBLE (priv->d_button_image)) {
+            gtk_widget_size_allocate (priv->d_button_image, &img_alloc);
+        }
+
+        if (GTK_WIDGET_VISIBLE (priv->frame)) {
+            gtk_widget_size_allocate (priv->frame, &f_alloc);
+        }
     }
 
     /* FIXME: We really should not alloc delimeters by hand (since they
@@ -1353,6 +1357,23 @@ hildon_date_editor_size_allocate                (GtkWidget *widget,
     }
 }
 
+static gboolean
+hildon_date_editor_focus                      (GtkWidget *widget,
+                                               GtkDirectionType direction)
+{
+  gboolean retval;
+  GtkDirectionType effective_direction;
+
+  g_assert (HILDON_IS_DATE_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;
+}
+
 /**
  * hildon_date_editor_set_year:
  * @editor: the @HildonDateEditor widget