From: Andrea Zagli Date: Sat, 30 Apr 2011 11:19:03 +0000 (+0200) Subject: Read format and separator from locale. X-Git-Tag: 0.0.3~4 X-Git-Url: https://saetta.ns0.it/gitweb?a=commitdiff_plain;h=5c3bc29e5dfc6e95d2d6ec55334904974820c25b;p=libgtkdateentry Read format and separator from locale. --- diff --git a/src/gtkdateentry.c b/src/gtkdateentry.c index b635c0c..eeb5979 100644 --- a/src/gtkdateentry.c +++ b/src/gtkdateentry.c @@ -20,6 +20,7 @@ */ #include +#include #include @@ -72,6 +73,9 @@ static void gtk_date_entry_get_property (GObject *object, GValue *value, GParamSpec *pspec); +static gchar *gtk_date_entry_get_separator_from_locale (); +static gchar *gtk_date_entry_get_format_from_locale (); + static GtkWidgetClass *parent_class = NULL; @@ -86,7 +90,7 @@ struct _GtkDateEntryPrivate GtkWidget *wCalendar; GtkWidget *calendar; - gchar separator; + gchar *separator; gchar *format; gboolean editable_with_calendar; }; @@ -130,8 +134,8 @@ gtk_date_entry_init (GtkDateEntry *date) /* TO DO * read separator and format from locale settings */ - priv->separator = '/'; - priv->format = g_strdup ("dmY"); + priv->separator = gtk_date_entry_get_separator_from_locale (); + priv->format = gtk_date_entry_get_format_from_locale (); priv->hbox = gtk_hbox_new (FALSE, 0); gtk_container_add (GTK_CONTAINER (date), priv->hbox); @@ -193,12 +197,38 @@ gtk_date_entry_init (GtkDateEntry *date) * Returns: The newly created #GtkDateEntry widget. */ GtkWidget -*gtk_date_entry_new (const gchar *format, const gchar separator, gboolean calendar_button_is_visible) +*gtk_date_entry_new (const gchar *format, const gchar *separator, gboolean calendar_button_is_visible) { + gchar *_format; + gchar *_separator; + GtkWidget *w = GTK_WIDGET (g_object_new (gtk_date_entry_get_type (), NULL)); - if (!gtk_date_entry_set_format (GTK_DATE_ENTRY (w), format)) return NULL; - gtk_date_entry_set_separator (GTK_DATE_ENTRY (w), separator); + if (format == NULL) + { + _format = gtk_date_entry_get_format_from_locale (); + } + else + { + _format = g_strdup (format); + } + if (!gtk_date_entry_set_format (GTK_DATE_ENTRY (w), _format)) + { + return NULL; + } + + if (separator == NULL) + { + _separator = gtk_date_entry_get_separator_from_locale (); + } + else + { + _separator = g_strdup (separator); + } + if (!gtk_date_entry_set_separator (GTK_DATE_ENTRY (w), _separator)) + { + return NULL; + } gtk_date_entry_set_calendar_button_visible (GTK_DATE_ENTRY (w), calendar_button_is_visible); @@ -212,16 +242,30 @@ GtkWidget * * Set the separator between day, month and year. */ -void -gtk_date_entry_set_separator (GtkDateEntry *date, const gchar separator) +gboolean +gtk_date_entry_set_separator (GtkDateEntry *date, const gchar *separator) { + gchar *_separator; + GDate *gdate = gtk_date_entry_get_gdate (date); GtkDateEntryPrivate *priv = GTK_DATE_ENTRY_GET_PRIVATE (date); - priv->separator = separator; + if (separator == NULL) + { + return FALSE; + } + _separator = g_strstrip (g_strdup (separator)); + if (strlen (_separator) != 1) + { + return FALSE; + } + + priv->separator = g_strdup (_separator); gtk_date_entry_change_mask (date); gtk_date_entry_set_date_gdate (date, gdate); + + g_free (_separator); } /** @@ -258,7 +302,7 @@ gtk_date_entry_set_format (GtkDateEntry *date, const gchar *format) for (i = 0; i < 3; i++) { - switch (format[i]) + switch (format_[i]) { case 'd': if (d) return FALSE; @@ -283,10 +327,12 @@ gtk_date_entry_set_format (GtkDateEntry *date, const gchar *format) gdate = gtk_date_entry_get_gdate (date); - priv->format = g_strdup (format); + priv->format = g_strdup (format_); gtk_date_entry_change_mask (date); gtk_date_entry_set_date_gdate (date, gdate); + g_free (format_); + return TRUE; } @@ -318,9 +364,9 @@ const gchar const gchar *gtk_date_entry_get_strf (GtkDateEntry *date, const gchar *format, - gchar separator) + const gchar *separator) { - gchar *fmt, sep, *ret = ""; + gchar *fmt, *sep, *ret = ""; gint i; GDate *gdate = (GDate *)gtk_date_entry_get_gdate (date); @@ -339,13 +385,13 @@ const gchar { fmt = g_strdup (format); } - if (separator == 0) + if (separator == NULL) { sep = priv->separator; } else { - sep = (gchar)separator; + sep = (gchar *)separator; } for (i = 0; i < 3; i++) @@ -367,7 +413,7 @@ const gchar if (i < 2) { - ret = g_strjoin (NULL, ret, g_strdup_printf ("%c", sep), NULL); + ret = g_strjoin (NULL, ret, g_strdup_printf ("%s", sep), NULL); } } @@ -491,10 +537,10 @@ gboolean gtk_date_entry_set_date_strf (GtkDateEntry *date, const gchar *str, const gchar *format, - const gchar separator) + const gchar *separator) { gchar *fmt; - gchar sep; + gchar *sep; GDateDay day; GDateMonth month; GDateYear year; @@ -511,13 +557,13 @@ gtk_date_entry_set_date_strf (GtkDateEntry *date, { fmt = g_strdup (format); } - if (separator == 0) + if (separator == NULL) { sep = priv->separator; } else { - sep = (gchar)separator; + sep = (gchar *)separator; } for (i = 0; i < 3; i++) @@ -600,7 +646,7 @@ gtk_date_entry_set_date_gdate (GtkDateEntry *date, const GDate *gdate) if (i < 2) { - txt = g_strjoin (NULL, txt, g_strdup_printf ("%c", priv->separator), NULL); + txt = g_strjoin (NULL, txt, g_strdup_printf ("%s", priv->separator), NULL); } } @@ -708,7 +754,7 @@ gtk_date_entry_change_mask (GtkDateEntry *date) } } - mask = g_strdup_printf ("%s%c%s%c%s", + mask = g_strdup_printf ("%s%s%s%s%s", format[0], priv->separator, format[1], @@ -985,3 +1031,72 @@ gtk_date_entry_size_allocate (GtkWidget *widget, gtk_widget_size_allocate (bin->child, &child_allocation); } } + +static gchar +*gtk_date_entry_get_separator_from_locale () +{ + gchar *fmt; + + gchar *lfmt; + guint l; + guint i; + + fmt = NULL; + + lfmt = nl_langinfo (D_FMT); + l = strlen (lfmt); + for (i = 0; i < l; i++) + { + switch (lfmt[i]) + { + case 'd': + case 'm': + case 'y': + case 'Y': + case '%': + break; + + default: + fmt = g_strdup_printf ("%c", lfmt[i]); + i = l; + break; + } + } + + return fmt; +} + +static gchar +*gtk_date_entry_get_format_from_locale () +{ + gchar *fmt; + + gchar *lfmt; + guint l; + guint i; + + fmt = NULL; + + lfmt = nl_langinfo (D_FMT); + l = strlen (lfmt); + for (i = 0; i < l; i++) + { + switch (lfmt[i]) + { + case 'd': + fmt = g_strconcat (fmt == NULL ? "" : fmt, "d", NULL); + break; + + case 'm': + fmt = g_strconcat (fmt == NULL ? "" : fmt, "m", NULL); + break; + + case 'y': + case 'Y': + fmt = g_strconcat (fmt == NULL ? "" : fmt, "Y", NULL); + break; + } + } + + return fmt; +} diff --git a/src/gtkdateentry.h b/src/gtkdateentry.h index 5212b17..81734d2 100644 --- a/src/gtkdateentry.h +++ b/src/gtkdateentry.h @@ -56,18 +56,18 @@ struct _GtkDateEntryClass GType gtk_date_entry_get_type (void) G_GNUC_CONST; GtkWidget *gtk_date_entry_new (const gchar *format, - const gchar separator, + const gchar *separator, gboolean calendar_button_is_visible); -void gtk_date_entry_set_separator (GtkDateEntry *date, - const gchar separator); +gboolean gtk_date_entry_set_separator (GtkDateEntry *date, + const gchar *separator); gboolean gtk_date_entry_set_format (GtkDateEntry *date, const gchar *format); const gchar *gtk_date_entry_get_text (GtkDateEntry *date); const gchar *gtk_date_entry_get_strf (GtkDateEntry *date, const gchar *format, - gchar separator); + const gchar *separator); struct tm *gtk_date_entry_get_tm (GtkDateEntry *date); GDate *gtk_date_entry_get_gdate (GtkDateEntry *date); @@ -75,7 +75,7 @@ GDate *gtk_date_entry_get_gdate (GtkDateEntry *date); gboolean gtk_date_entry_set_date_strf (GtkDateEntry *date, const gchar *str, const gchar *format, - const gchar separator); + const gchar *separator); void gtk_date_entry_set_date_tm (GtkDateEntry *date, const struct tm tmdate); diff --git a/tests/date_entry.c b/tests/date_entry.c index f538a6c..f3bbc80 100644 --- a/tests/date_entry.c +++ b/tests/date_entry.c @@ -43,7 +43,7 @@ on_btnSeparator_clicked (GtkButton *button, gpointer user_data) { gtk_date_entry_set_separator (GTK_DATE_ENTRY (date), - (const gchar)(gtk_entry_get_text (GTK_ENTRY (separator))[0])); + gtk_entry_get_text (GTK_ENTRY (separator))); } static void @@ -137,7 +137,7 @@ main (int argc, char **argv) gtk_table_attach (GTK_TABLE (table), label, 0, 1, 0, 1, GTK_EXPAND | GTK_FILL, GTK_EXPAND | GTK_FILL, 0, 0); gtk_widget_show (label); - date = gtk_date_entry_new ("dmY", '/', TRUE); + date = gtk_date_entry_new (NULL, NULL, TRUE); gtk_table_attach (GTK_TABLE (table), date, 1, 2, 0, 1, GTK_EXPAND | GTK_FILL, GTK_EXPAND | GTK_FILL, 0, 0); gtk_widget_show (date);