From cba0788b434fd7e19a1fd65e526cc1dd8cdfda9d Mon Sep 17 00:00:00 2001 From: Andrea Zagli Date: Tue, 31 May 2011 20:19:01 +0200 Subject: [PATCH] Bugfixes and adjustments. --- configure.ac | 2 +- src/gtkdateentry.c | 88 ++++++++++++++++++++++++++++++------ src/gtkdateentry.h | 3 +- src/gtkformwidgetdateentry.c | 8 ++-- tests/date_entry.c | 40 +++++++++++++++- 5 files changed, 119 insertions(+), 22 deletions(-) diff --git a/configure.ac b/configure.ac index 6a27c74..5d507b5 100644 --- a/configure.ac +++ b/configure.ac @@ -1,5 +1,5 @@ AC_PREREQ(2.59) -AC_INIT([GtkDateEntry], [0.0.3], [azagli@libero.it]) +AC_INIT([GtkDateEntry], [0.0.4], [azagli@libero.it]) AC_CONFIG_SRCDIR([src/gtkdateentry.c]) AC_CONFIG_HEADER([config.h]) diff --git a/src/gtkdateentry.c b/src/gtkdateentry.c index 7f445c9..b347904 100644 --- a/src/gtkdateentry.c +++ b/src/gtkdateentry.c @@ -461,9 +461,11 @@ const gchar if (gtk_widget_get_visible (priv->spnHours)) { ret = g_strconcat (ret, - g_strdup_printf (" %02d:%02d:%02d", + g_strdup_printf (" %02d%s%02d%s%02d", gtk_spin_button_get_value_as_int (GTK_SPIN_BUTTON (priv->spnHours)), + priv->time_separator, gtk_spin_button_get_value_as_int (GTK_SPIN_BUTTON (priv->spnMinutes)), + priv->time_separator, gtk_spin_button_get_value_as_int (GTK_SPIN_BUTTON (priv->spnSeconds))), NULL); } @@ -483,19 +485,33 @@ const gchar const gchar *gtk_date_entry_get_strf (GtkDateEntry *date, const gchar *format, - const gchar *separator) + const gchar *separator, + const gchar *time_separator) { - gchar *fmt, *sep, *ret = ""; - gint i; - GDate *gdate = (GDate *)gtk_date_entry_get_gdate (date); + gchar *fmt; + gchar *sep; + gchar *tsep; + + gchar *str_date; + gchar *str_time; + + guint l; + guint i; + + g_return_val_if_fail (GTK_IS_DATE_ENTRY (date), ""); + + struct tm *tm_date = gtk_date_entry_get_tm (date); GtkDateEntryPrivate *priv = GTK_DATE_ENTRY_GET_PRIVATE (date); - if (gdate == NULL) + if (tm_date == NULL) { return ""; } + str_date = g_strdup (""); + str_time = g_strdup (""); + if (format == NULL) { fmt = priv->format; @@ -504,6 +520,7 @@ const gchar { fmt = g_strdup (format); } + if (separator == NULL) { sep = priv->separator; @@ -513,30 +530,71 @@ const gchar sep = (gchar *)separator; } - for (i = 0; i < 3; i++) + if (time_separator == NULL) + { + tsep = priv->time_separator; + } + else + { + tsep = (gchar *)time_separator; + } + + l = strlen (fmt); + for (i = 0; i < l; i++) { switch (fmt[i]) { case 'd': - ret = g_strjoin (NULL, ret, g_strdup_printf ("%02d", (int)g_date_get_day (gdate)), NULL); + if (strlen (str_date) > 0) + { + str_date = g_strconcat (str_date, sep, NULL); + } + str_date = g_strconcat (str_date, g_strdup_printf ("%02d", tm_date->tm_mday, NULL)); break; case 'm': - ret = g_strjoin (NULL, ret, g_strdup_printf ("%02d", (int)g_date_get_month (gdate)), NULL); + if (strlen (str_date) > 0) + { + str_date = g_strconcat (str_date, sep, NULL); + } + str_date = g_strconcat (str_date, g_strdup_printf ("%02d", tm_date->tm_mon + 1, NULL)); break; case 'Y': - ret = g_strjoin (NULL, ret, g_strdup_printf ("%04d", (int)g_date_get_year (gdate)), NULL); + if (strlen (str_date) > 0) + { + str_date = g_strconcat (str_date, sep, NULL); + } + str_date = g_strconcat (str_date, g_strdup_printf ("%04d", tm_date->tm_year + 1900, NULL)); break; - } - if (i < 2) - { - ret = g_strjoin (NULL, ret, g_strdup_printf ("%s", sep), NULL); + case 'H': + if (strlen (str_time) > 0) + { + str_time = g_strconcat (str_time, tsep, NULL); + } + str_time = g_strconcat (str_time, g_strdup_printf ("%02d", tm_date->tm_hour, NULL)); + break; + + case 'M': + if (strlen (str_time) > 0) + { + str_time = g_strconcat (str_time, tsep, NULL); + } + str_time = g_strconcat (str_time, g_strdup_printf ("%02d", tm_date->tm_min, NULL)); + break; + + case 'S': + if (strlen (str_time) > 0) + { + str_time = g_strconcat (str_time, tsep, NULL); + } + str_time = g_strconcat (str_time, g_strdup_printf ("%02d", tm_date->tm_sec, NULL)); + break; } } - return (const gchar *)ret; + return (const gchar *)g_strstrip (g_strdup_printf ("%s %s", str_date, str_time)); } /** diff --git a/src/gtkdateentry.h b/src/gtkdateentry.h index db3910a..511c304 100644 --- a/src/gtkdateentry.h +++ b/src/gtkdateentry.h @@ -69,7 +69,8 @@ gboolean gtk_date_entry_set_format (GtkDateEntry *date, const gchar *gtk_date_entry_get_text (GtkDateEntry *date); const gchar *gtk_date_entry_get_strf (GtkDateEntry *date, const gchar *format, - const gchar *separator); + const gchar *separator, + const gchar *time_separator); struct tm *gtk_date_entry_get_tm (GtkDateEntry *date); GDate *gtk_date_entry_get_gdate (GtkDateEntry *date); diff --git a/src/gtkformwidgetdateentry.c b/src/gtkformwidgetdateentry.c index 702c5de..1791456 100644 --- a/src/gtkformwidgetdateentry.c +++ b/src/gtkformwidgetdateentry.c @@ -1,5 +1,5 @@ /* - * Copyright (C) 2010 Andrea Zagli + * Copyright (C) 2010-2011 Andrea Zagli * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public @@ -93,7 +93,7 @@ gchar GtkWidget *w = gtk_form_widget_get_widget (fwidget); - return g_strdup (gtk_date_entry_get_strf (GTK_DATE_ENTRY (w), "Ymd", "-")); + return g_strdup (gtk_date_entry_get_strf (GTK_DATE_ENTRY (w), "Ymd HMS", "-", ":")); } /** @@ -153,7 +153,7 @@ gtk_form_widget_date_entry_set_property (GObject *object, default: G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec); break; - } + } } static void @@ -171,5 +171,5 @@ gtk_form_widget_date_entry_get_property (GObject *object, default: G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec); break; - } + } } diff --git a/tests/date_entry.c b/tests/date_entry.c index b4b6aa6..bbea89d 100644 --- a/tests/date_entry.c +++ b/tests/date_entry.c @@ -38,6 +38,9 @@ GtkWidget *txtSetStrf; GtkWidget *txtSetStrfFormat; GtkWidget *txtSetStrfSep; GtkWidget *btnSetStrf; +GtkWidget *txtGetStrfFormat; +GtkWidget *txtGetStrf; +GtkWidget *btnGetStrf; GtkWidget *txtGetText; GtkWidget *btnGetText; @@ -82,6 +85,17 @@ on_btnSetStrf_clicked (GtkButton *button, NULL); } +static void +on_btnGetStrf_clicked (GtkButton *button, + gpointer user_data) +{ + gtk_entry_set_text (GTK_ENTRY (txtGetStrf), + gtk_date_entry_get_strf (GTK_DATE_ENTRY (date), + (const gchar *)gtk_entry_get_text (GTK_ENTRY (txtGetStrfFormat)), + NULL, + NULL)); +} + static void on_btnGetText_clicked (GtkButton *button, gpointer user_data) @@ -152,7 +166,7 @@ main (int argc, char **argv) gtk_window_set_title (GTK_WINDOW (window), "GtkDateEntry Test"); g_signal_connect (G_OBJECT (window), "destroy", - G_CALLBACK (gtk_main_quit), NULL); + G_CALLBACK (gtk_main_quit), NULL); table = gtk_table_new (5, 3, FALSE); gtk_container_add (GTK_CONTAINER (window), table); @@ -251,6 +265,30 @@ main (int argc, char **argv) g_signal_connect (G_OBJECT (btnSetStrf), "clicked", G_CALLBACK (on_btnSetStrf_clicked), NULL); + x = 0; + y++; + label = gtk_label_new ("Get strf"); + gtk_table_attach (GTK_TABLE (table), label, x, x + 1, y, y + 1, GTK_EXPAND | GTK_FILL, GTK_EXPAND | GTK_FILL, 0, 0); + gtk_widget_show (label); + + x++; + txtGetStrfFormat = gtk_entry_new (); + gtk_table_attach (GTK_TABLE (table), txtGetStrfFormat, x, x + 1, y, y + 1, GTK_EXPAND | GTK_FILL, GTK_EXPAND | GTK_FILL, 0, 0); + gtk_widget_show (txtGetStrfFormat); + + x++; + txtGetStrf = gtk_entry_new (); + gtk_table_attach (GTK_TABLE (table), txtGetStrf, x, x + 1, y, y + 1, GTK_EXPAND | GTK_FILL, GTK_EXPAND | GTK_FILL, 0, 0); + gtk_widget_show (txtGetStrf); + + x++; + btnGetStrf = gtk_button_new_with_label ("get_strf"); + gtk_table_attach (GTK_TABLE (table), btnGetStrf, x, x + 1, y, y + 1, GTK_EXPAND | GTK_FILL, GTK_EXPAND | GTK_FILL, 0, 0); + gtk_widget_show (btnGetStrf); + + g_signal_connect (G_OBJECT (btnGetStrf), "clicked", + G_CALLBACK (on_btnGetStrf_clicked), NULL); + x = 0; y++; label = gtk_label_new ("Text"); -- 2.49.0