diff --git a/gtk/gtkcalendar.c b/gtk/gtkcalendar.c index c1716e604d..5e1ee2f9d0 100644 --- a/gtk/gtkcalendar.c +++ b/gtk/gtkcalendar.c @@ -1150,42 +1150,17 @@ gtk_calendar_set_property (GObject *object, GParamSpec *pspec) { GtkCalendar *calendar = GTK_CALENDAR (object); - GDateTime *date; switch (prop_id) { case PROP_YEAR: - date = g_date_time_new_local (g_value_get_int (value), - g_date_time_get_month (calendar->date), - g_date_time_get_day_of_month (calendar->date), - 0, 0, 0); - if (date) - { - calendar_select_day_internal (calendar, date, TRUE); - g_date_time_unref (date); - } + gtk_calendar_set_year (calendar, g_value_get_int (value)); break; case PROP_MONTH: - date = g_date_time_new_local (g_date_time_get_year (calendar->date), - g_value_get_int (value) + 1, - g_date_time_get_day_of_month (calendar->date), - 0, 0, 0); - if (date) - { - calendar_select_day_internal (calendar, date, TRUE); - g_date_time_unref (date); - } + gtk_calendar_set_month (calendar, g_value_get_int (value)); break; case PROP_DAY: - date = g_date_time_new_local (g_date_time_get_year (calendar->date), - g_date_time_get_month (calendar->date), - g_value_get_int (value), - 0, 0, 0); - if (date) - { - calendar_select_day_internal (calendar, date, TRUE); - g_date_time_unref (date); - } + gtk_calendar_set_day (calendar, g_value_get_int (value)); break; case PROP_SHOW_HEADING: gtk_calendar_set_show_heading (calendar, g_value_get_boolean (value)); @@ -1213,13 +1188,13 @@ gtk_calendar_get_property (GObject *object, switch (prop_id) { case PROP_YEAR: - g_value_set_int (value, g_date_time_get_year (calendar->date)); + g_value_set_int (value, gtk_calendar_get_year (calendar)); break; case PROP_MONTH: - g_value_set_int (value, g_date_time_get_month (calendar->date) - 1); + g_value_set_int (value, gtk_calendar_get_month (calendar)); break; case PROP_DAY: - g_value_set_int (value, g_date_time_get_day_of_month (calendar->date)); + g_value_set_int (value, gtk_calendar_get_day (calendar)); break; case PROP_SHOW_HEADING: g_value_set_boolean (value, gtk_calendar_get_show_heading (calendar)); @@ -1795,3 +1770,166 @@ gtk_calendar_get_show_day_names (GtkCalendar *self) return self->show_day_names; } + +/** + * gtk_calendar_set_day: + * @self: a `GtkCalendar` + * @day: The desired day for the selected date (as a number between 1 and 31). + * + * Sets the day for the selected date. + * + * The new date must be valid. For example, setting 31 for the day when the + * month is February, fails. + * + * Since: 4.14 + */ +void +gtk_calendar_set_day (GtkCalendar *self, + int day) +{ + GDateTime *date; + + g_return_if_fail (GTK_IS_CALENDAR (self)); + g_return_if_fail (day >= 1 && day <= 31); + + if (day == g_date_time_get_day_of_month (self->date)) + return; + + date = g_date_time_new_local (g_date_time_get_year (self->date), + g_date_time_get_month (self->date), + day, + 0, 0, 0.0); + g_return_if_fail (date != NULL); + + calendar_select_day_internal (self, date, TRUE); + g_date_time_unref (date); + + g_object_notify (G_OBJECT (self), "day"); +} + +/** + * gtk_calendar_get_day: + * @self: a `GtkCalendar` + * + * Gets the day of the selected date. + * + * Returns: the day of the selected date. + * + * Since: 4.14 + */ +int +gtk_calendar_get_day (GtkCalendar *self) +{ + g_return_val_if_fail (GTK_IS_CALENDAR (self), -1); + + return g_date_time_get_day_of_month (self->date); +} + +/** + * gtk_calendar_set_month: + * @self: a `GtkCalendar` + * @month: The desired month for the selected date (as a number between 0 and 11). + * + * Sets the month for the selected date. + * + * The new date must be valid. For example, setting 1 (February) for the month + * when the day is 31, fails. + * + * Since: 4.14 + */ +void +gtk_calendar_set_month (GtkCalendar *self, + int month) +{ + GDateTime *date; + + g_return_if_fail (GTK_IS_CALENDAR (self)); + g_return_if_fail (month >= 0 && month <= 11); + + if (month == g_date_time_get_month (self->date) - 1) + return; + + date = g_date_time_new_local (g_date_time_get_year (self->date), + month + 1, + g_date_time_get_day_of_month (self->date), + 0, 0, 0.0); + g_return_if_fail (date != NULL); + + calendar_select_day_internal (self, date, TRUE); + g_date_time_unref (date); + + g_object_notify (G_OBJECT (self), "month"); +} + +/** + * gtk_calendar_get_month: + * @self: a `GtkCalendar` + * + * Gets the month of the selected date. + * + * Returns: The month of the selected date (as a number between 0 and 11). + * + * Since: 4.14 + */ +int +gtk_calendar_get_month (GtkCalendar *self) +{ + g_return_val_if_fail (GTK_IS_CALENDAR (self), -1); + + return g_date_time_get_month (self->date) - 1; +} + +/** + * gtk_calendar_set_year: + * @self: a `GtkCalendar` + * @year: The desired year for the selected date (within [struct@GLib.DateTime] + * limits, i.e. from 0001 to 9999). + * + * Sets the year for the selected date. + * + * The new date must be valid. For example, setting 2023 for the year when then + * the date is 2024-02-29, fails. + * + * Since: 4.14 + */ +void +gtk_calendar_set_year (GtkCalendar *self, + int year) +{ + GDateTime *date; + + g_return_if_fail (GTK_IS_CALENDAR (self)); + g_return_if_fail (year >= 1 && year <= 9999); + + if (year == g_date_time_get_year (self->date)) + return; + + date = g_date_time_new_local (year, + g_date_time_get_month (self->date), + g_date_time_get_day_of_month (self->date), + 0, 0, 0.0); + g_return_if_fail (date != NULL); + + calendar_select_day_internal (self, date, TRUE); + g_date_time_unref (date); + + g_object_notify (G_OBJECT (self), "year"); +} + +/** + * gtk_calendar_get_year: + * @self: a `GtkCalendar` + * + * Gets the year of the selected date. + * + * Returns: the year of the selected date. + * + * Since: 4.14 + */ +int +gtk_calendar_get_year (GtkCalendar *self) +{ + g_return_val_if_fail (GTK_IS_CALENDAR (self), -1); + + return g_date_time_get_year (self->date); +} diff --git a/gtk/gtkcalendar.h b/gtk/gtkcalendar.h index 0e3475b4b7..7cceecf32c 100644 --- a/gtk/gtkcalendar.h +++ b/gtk/gtkcalendar.h @@ -78,6 +78,27 @@ void gtk_calendar_set_show_day_names (GtkCalendar *self, GDK_AVAILABLE_IN_ALL gboolean gtk_calendar_get_show_day_names (GtkCalendar *self); +GDK_AVAILABLE_IN_4_14 +void gtk_calendar_set_day (GtkCalendar *self, + int day); + +GDK_AVAILABLE_IN_4_14 +int gtk_calendar_get_day (GtkCalendar *self); + +GDK_AVAILABLE_IN_4_14 +void gtk_calendar_set_month (GtkCalendar *self, + int month); + +GDK_AVAILABLE_IN_4_14 +int gtk_calendar_get_month (GtkCalendar *self); + +GDK_AVAILABLE_IN_4_14 +void gtk_calendar_set_year (GtkCalendar *self, + int year); + +GDK_AVAILABLE_IN_4_14 +int gtk_calendar_get_year (GtkCalendar *self); + GDK_AVAILABLE_IN_ALL GDateTime * gtk_calendar_get_date (GtkCalendar *self); diff --git a/testsuite/gtk/calendar.c b/testsuite/gtk/calendar.c index 809260d0da..202e138845 100644 --- a/testsuite/gtk/calendar.c +++ b/testsuite/gtk/calendar.c @@ -1,7 +1,25 @@ #include static void -test_calendar_set_day (void) +test_calendar_get_set_properties (void) +{ + GtkWidget *calendar; + int year, month, day; + + calendar = gtk_calendar_new (); + g_object_set (calendar, "year", 2024, NULL); + g_object_get (calendar, "year", &year, NULL); + g_assert_cmpint (year, ==, 2024); + g_object_set (calendar, "month", 0, NULL); /* January */ + g_object_get (calendar, "month", &month, NULL); + g_assert_cmpint (month, ==, 0); + g_object_set (calendar, "day", 15, NULL); + g_object_get (calendar, "day", &day, NULL); + g_assert_cmpint (day, ==, 15); +} + +static void +test_calendar_select_day (void) { GtkWidget *cal; GTimeZone *tz; @@ -26,7 +44,7 @@ test_calendar_set_day (void) } static void -test_calendar_properties (void) +test_calendar_get_date (void) { GtkWidget *cal; GDateTime *dt2; @@ -47,14 +65,54 @@ test_calendar_properties (void) g_date_time_unref (dt2); } +static void +test_calendar_set_get_year (void) +{ + GtkWidget *calendar; + int year; + + calendar = gtk_calendar_new (); + gtk_calendar_set_year (GTK_CALENDAR (calendar), 2024); + year = gtk_calendar_get_year (GTK_CALENDAR (calendar)); + g_assert_cmpint (year, ==, 2024); +} + +static void +test_calendar_set_get_month (void) +{ + GtkWidget *calendar; + int month; + + calendar = gtk_calendar_new (); + gtk_calendar_set_month (GTK_CALENDAR (calendar), 1); /* February */ + month = gtk_calendar_get_month (GTK_CALENDAR (calendar)); + g_assert_cmpint (month, ==, 1); +} + +static void +test_calendar_set_get_day (void) +{ + GtkWidget *calendar; + int day; + + calendar = gtk_calendar_new (); + gtk_calendar_set_day (GTK_CALENDAR (calendar), 10); + day = gtk_calendar_get_day (GTK_CALENDAR (calendar)); + g_assert_cmpint (day, ==, 10); +} + int main (int argc, char *argv[]) { gtk_init (); (g_test_init) (&argc, &argv, NULL); - g_test_add_func ("/calendar/set_day", test_calendar_set_day); - g_test_add_func ("/calendar/properties", test_calendar_properties); + g_test_add_func ("/calendar/get_set_properties", test_calendar_get_set_properties); + g_test_add_func ("/calendar/select_day", test_calendar_select_day); + g_test_add_func ("/calendar/test_calendar_get_date", test_calendar_get_date); + g_test_add_func ("/calendar/set_get_day", test_calendar_set_get_day); + g_test_add_func ("/calendar/set_get_month", test_calendar_set_get_month); + g_test_add_func ("/calendar/set_get_year", test_calendar_set_get_year); return g_test_run (); }