[gtk/wip/baedert/for-master: 3/3] calendar: Return a GDateTime from get_date()



commit 97279bfa4db761e41210cf35172788447c8201a0
Author: Timm Bäder <mail baedert org>
Date:   Tue Feb 4 15:20:39 2020 +0100

    calendar: Return a GDateTime from get_date()

 gtk/gtkcalendar.c    | 30 ++++++-------------
 gtk/gtkcalendar.h    | 31 +++++++++-----------
 tests/testcalendar.c | 81 +++++++++++++++++++++++-----------------------------
 3 files changed, 58 insertions(+), 84 deletions(-)
---
diff --git a/gtk/gtkcalendar.c b/gtk/gtkcalendar.c
index b19320d5c7..1df7a5a80a 100644
--- a/gtk/gtkcalendar.c
+++ b/gtk/gtkcalendar.c
@@ -1997,33 +1997,19 @@ gtk_calendar_unmark_day (GtkCalendar *calendar,
 /**
  * gtk_calendar_get_date:
  * @calendar: a #GtkCalendar
- * @year: (out) (allow-none): location to store the year as a decimal
- *     number (e.g. 2011), or %NULL
- * @month: (out) (allow-none): location to store the month number
- *     (between 0 and 11), or %NULL
- * @day: (out) (allow-none): location to store the day number (between
- *     1 and 31), or %NULL
  *
- * Obtains the selected date from a #GtkCalendar.
+ * Returns: (transfer full): A #GDateTime representing the shown
+ *   year, month and the selected day, in the local time zone.
  */
-void
-gtk_calendar_get_date (GtkCalendar *calendar,
-                       guint       *year,
-                       guint       *month,
-                       guint       *day)
+GDateTime *
+gtk_calendar_get_date (GtkCalendar *self)
 {
-  GtkCalendarPrivate *priv = gtk_calendar_get_instance_private (calendar);
-
-  g_return_if_fail (GTK_IS_CALENDAR (calendar));
-
-  if (year)
-    *year = priv->year;
+  GtkCalendarPrivate *priv = gtk_calendar_get_instance_private (self);
 
-  if (month)
-    *month = priv->month;
+  g_return_val_if_fail (GTK_IS_CALENDAR (self), NULL);
 
-  if (day)
-    *day = priv->selected_day;
+  return g_date_time_new_local (priv->year, priv->month + 1, priv->selected_day,
+                                0, 0, 0);
 }
 
 /**
diff --git a/gtk/gtkcalendar.h b/gtk/gtkcalendar.h
index c1f0189832..edac48cadd 100644
--- a/gtk/gtkcalendar.h
+++ b/gtk/gtkcalendar.h
@@ -51,12 +51,12 @@ GDK_AVAILABLE_IN_ALL
 GtkWidget* gtk_calendar_new            (void);
 
 GDK_AVAILABLE_IN_ALL
-void       gtk_calendar_select_month                (GtkCalendar *self,
-                                                     GDateTime   *date);
+void          gtk_calendar_select_month                (GtkCalendar *self,
+                                                        GDateTime   *date);
 
 GDK_AVAILABLE_IN_ALL
-void       gtk_calendar_select_day                  (GtkCalendar *self,
-                                                     GDateTime   *date);
+void          gtk_calendar_select_day                  (GtkCalendar *self,
+                                                        GDateTime   *date);
 
 GDK_AVAILABLE_IN_ALL
 void       gtk_calendar_mark_day       (GtkCalendar *calendar,
@@ -68,26 +68,23 @@ GDK_AVAILABLE_IN_ALL
 void      gtk_calendar_clear_marks     (GtkCalendar *calendar);
 
 GDK_AVAILABLE_IN_ALL
-void       gtk_calendar_set_show_week_numbers       (GtkCalendar *self,
-                                                     gboolean     value);
+void          gtk_calendar_set_show_week_numbers       (GtkCalendar *self,
+                                                        gboolean     value);
 GDK_AVAILABLE_IN_ALL
-gboolean   gtk_calendar_get_show_week_numbers       (GtkCalendar *self);
+gboolean      gtk_calendar_get_show_week_numbers       (GtkCalendar *self);
 GDK_AVAILABLE_IN_ALL
-void       gtk_calendar_set_show_heading            (GtkCalendar *self,
-                                                     gboolean     value);
+void          gtk_calendar_set_show_heading            (GtkCalendar *self,
+                                                        gboolean     value);
 GDK_AVAILABLE_IN_ALL
-gboolean   gtk_calendar_get_show_heading            (GtkCalendar *self);
+gboolean      gtk_calendar_get_show_heading            (GtkCalendar *self);
 GDK_AVAILABLE_IN_ALL
-void       gtk_calendar_set_show_day_names          (GtkCalendar *self,
-                                                     gboolean     value);
+void          gtk_calendar_set_show_day_names          (GtkCalendar *self,
+                                                        gboolean     value);
 GDK_AVAILABLE_IN_ALL
-gboolean   gtk_calendar_get_show_day_names          (GtkCalendar *self);
+gboolean      gtk_calendar_get_show_day_names          (GtkCalendar *self);
 
 GDK_AVAILABLE_IN_ALL
-void      gtk_calendar_get_date        (GtkCalendar *calendar,
-                                        guint       *year,
-                                        guint       *month,
-                                        guint       *day);
+GDateTime *   gtk_calendar_get_date                    (GtkCalendar *self);
 
 GDK_AVAILABLE_IN_ALL
 gboolean   gtk_calendar_get_day_is_marked      (GtkCalendar    *calendar,
diff --git a/tests/testcalendar.c b/tests/testcalendar.c
index f3bce328cd..b78c91fb2f 100644
--- a/tests/testcalendar.c
+++ b/tests/testcalendar.c
@@ -48,26 +48,19 @@ enum
  * GtkCalendar
  */
 
-static void
+static char *
 calendar_date_to_string (CalendarData *data,
-                             char         *buffer,
-                             gint          buff_len)
+                         char         *format)
 {
-  GDate *date;
-  guint year, month, day;
+  GDateTime *date;
+  char *str;
 
-  gtk_calendar_get_date (GTK_CALENDAR (data->calendar_widget),
-                         &year, &month, &day);
-  if (g_date_valid_dmy (day, month + 1, year))
-    {
-      date = g_date_new_dmy (day, month + 1, year);
-      g_date_strftime (buffer, buff_len-1, "%x", date);
-      g_date_free (date);
-    }
-  else
-    {
-      g_snprintf (buffer, buff_len - 1, "%d/%d/%d (invalid)", month + 1, day, year);
-    }
+  date = gtk_calendar_get_date (GTK_CALENDAR (data->calendar_widget));
+  str = g_date_time_format (date, format);
+
+  g_date_time_unref (date);
+
+  return str;
 }
 
 static void
@@ -88,78 +81,76 @@ static void
 calendar_month_changed (GtkWidget    *widget,
                              CalendarData *data)
 {
-  char buffer[256] = "month_changed: ";
-
-  calendar_date_to_string (data, buffer+15, 256-15);
-  calendar_set_signal_strings (buffer, data);
+  char *str = calendar_date_to_string (data, "month-changed: %c");
+  calendar_set_signal_strings (str, data);
+  g_free (str);
 }
 
 static void
 calendar_day_selected (GtkWidget    *widget,
                             CalendarData *data)
 {
-  char buffer[256] = "day_selected: ";
-
-  calendar_date_to_string (data, buffer+14, 256-14);
-  calendar_set_signal_strings (buffer, data);
+  char *str = calendar_date_to_string (data, "day-selected: %c");
+  calendar_set_signal_strings (str, data);
+  g_free (str);
 }
 
 static void
 calendar_day_selected_double_click (GtkWidget    *widget,
                                          CalendarData *data)
 {
-  char buffer[256] = "day_selected_double_click: ";
+  char *str;
   guint day;
+  GDateTime *date;
 
-  calendar_date_to_string (data, buffer+27, 256-27);
-  calendar_set_signal_strings (buffer, data);
-  gtk_calendar_get_date (GTK_CALENDAR (data->calendar_widget),
-                         NULL, NULL, &day);
+  str = calendar_date_to_string (data, "day-selected-double-click: %c");
+  calendar_set_signal_strings (str, data);
+  date = gtk_calendar_get_date (GTK_CALENDAR (data->calendar_widget));
+  day = g_date_time_get_day_of_month (date);
 
   if (gtk_calendar_get_day_is_marked (GTK_CALENDAR (data->calendar_widget), day))
     gtk_calendar_unmark_day (GTK_CALENDAR (data->calendar_widget), day);
   else
     gtk_calendar_mark_day (GTK_CALENDAR (data->calendar_widget), day);
+
+  g_date_time_unref (date);
 }
 
 static void
 calendar_prev_month (GtkWidget    *widget,
                           CalendarData *data)
 {
-  char buffer[256] = "prev_month: ";
-
-  calendar_date_to_string (data, buffer+12, 256-12);
-  calendar_set_signal_strings (buffer, data);
+  char *str = calendar_date_to_string (data, "prev-month: %c");
+  calendar_set_signal_strings (str, data);
+  g_free (str);
 }
 
 static void
 calendar_next_month (GtkWidget    *widget,
                      CalendarData *data)
 {
-  char buffer[256] = "next_month: ";
+  char *str = calendar_date_to_string (data, "next-month: %c");
+  calendar_set_signal_strings (str, data);
+  g_free (str);
 
-  calendar_date_to_string (data, buffer+12, 256-12);
-  calendar_set_signal_strings (buffer, data);
 }
 
 static void
 calendar_prev_year (GtkWidget    *widget,
                     CalendarData *data)
 {
-  char buffer[256] = "prev_year: ";
-
-  calendar_date_to_string (data, buffer+11, 256-11);
-  calendar_set_signal_strings (buffer, data);
+  char *str = calendar_date_to_string (data, "prev-year: %c");
+  calendar_set_signal_strings (str, data);
+  g_free (str);
 }
 
 static void
 calendar_next_year (GtkWidget    *widget,
                     CalendarData *data)
 {
-  char buffer[256] = "next_year: ";
-
-  calendar_date_to_string (data, buffer+11, 256-11);
-  calendar_set_signal_strings (buffer, data);
+  char *str = calendar_date_to_string (data, "next-year: %c");
+  calendar_set_signal_strings (str, data);
+  g_free (str);
 }
 
 static void


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]