GTK4: gtk_calendar_select_x()
authorColin Clark <colin.clark@cclark.uk>
Thu, 21 Sep 2023 09:48:42 +0000 (10:48 +0100)
committerColin Clark <colin.clark@cclark.uk>
Thu, 21 Sep 2023 09:48:42 +0000 (10:48 +0100)
GTK4 migration

gtk_calendar_select_x() Use GDateTime structure

src/search.cc
src/ui-misc.cc
src/ui-misc.h

index 2dc9169..ee8fdd8 100644 (file)
@@ -2651,10 +2651,11 @@ static void search_start(SearchData *sd)
 static void search_start_cb(GtkWidget *, gpointer data)
 {
        auto sd = static_cast<SearchData *>(data);
-       GtkTreeViewColumn *column;
-       gchar *path;
-       gchar *entry_text;
        gchar *collection;
+       gchar *entry_text;
+       gchar *path;
+       GDateTime *date;
+       GtkTreeViewColumn *column;
 
        if (sd->search_folder_list)
                {
@@ -2714,8 +2715,17 @@ static void search_start_cb(GtkWidget *, gpointer data)
        g_list_free_full(sd->search_keyword_list, g_free);
        sd->search_keyword_list = keyword_list_pull(sd->entry_keywords);
 
-       date_selection_get(sd->date_sel, &sd->search_date_d, &sd->search_date_m, &sd->search_date_y);
-       date_selection_get(sd->date_sel_end, &sd->search_date_end_d, &sd->search_date_end_m, &sd->search_date_end_y);
+       date = date_selection_get(sd->date_sel);
+       sd->search_date_d = g_date_time_get_day_of_month(date);
+       sd->search_date_m = g_date_time_get_month(date);
+       sd->search_date_y = g_date_time_get_year(date);
+       g_date_time_unref(date);
+
+       date = date_selection_get(sd->date_sel_end);
+       sd->search_date_end_d = g_date_time_get_day_of_month(date);
+       sd->search_date_end_m = g_date_time_get_month(date);
+       sd->search_date_end_y = g_date_time_get_year(date);
+       g_date_time_unref(date);
 
        column = gtk_tree_view_get_column(GTK_TREE_VIEW(sd->result_view), SEARCH_COLUMN_DIMENSIONS - 1);
        gtk_tree_view_column_set_visible(column, sd->match_dimensions_enable);
index 9821b61..ef3644f 100644 (file)
@@ -835,10 +835,23 @@ static gboolean date_selection_popup_press_cb(GtkWidget *, GdkEventButton *event
 
 static void date_selection_popup_sync(DateSelection *ds)
 {
-       guint day, month, year;
+       guint day;
+       guint month;
+       guint year;
 
+#if HAVE_GTK4
+       GDateTime *date_selected;
+
+       date_selected = gtk_calendar_get_date(GTK_CALENDAR(ds->calendar));
+       g_date_time_get_ymd(date_selected, static_cast<guint>(&year), static_cast<guint>(&month), static_cast<guint>(&day));
+
+       g_date_time_unref(date_selected);
+#else
        gtk_calendar_get_date(GTK_CALENDAR(ds->calendar), &year, &month, &day);
-       date_selection_set(ds->box, day, month + 1, year);
+       /* month is range 0 to 11 */
+       month = month + 1;
+#endif
+       date_selection_set(ds->box, day, month, year);
 }
 
 static gboolean date_selection_popup_keypress_cb(GtkWidget *, GdkEventKey *event, gpointer data)
@@ -880,9 +893,9 @@ static void date_selection_doubleclick_cb(GtkWidget *, gpointer data)
 
 static void date_selection_popup(DateSelection *ds)
 {
-       gint x, y;
+       GDateTime *date;
        gint wx, wy;
-       gint day, month, year;
+       gint x, y;
        GtkAllocation button_allocation;
        GtkAllocation window_allocation;
 
@@ -899,9 +912,14 @@ static void date_selection_popup(DateSelection *ds)
        gtk_container_add(GTK_CONTAINER(ds->window), ds->calendar);
        gtk_widget_show(ds->calendar);
 
-       date_selection_get(ds->box, &day, &month, &year);
-       gtk_calendar_select_month(GTK_CALENDAR(ds->calendar), month - 1, year);
-       gtk_calendar_select_day(GTK_CALENDAR(ds->calendar), day);
+       date = date_selection_get(ds->box);
+#ifdef HAVE_GTK4
+       gtk_calendar_select_day(GTK_CALENDAR(ds->calendar), date);
+#else
+       gtk_calendar_select_month(GTK_CALENDAR(ds->calendar), g_date_time_get_month(date), g_date_time_get_year(date));
+       gtk_calendar_select_day(GTK_CALENDAR(ds->calendar), g_date_time_get_day_of_month(date));
+#endif
+       g_date_time_unref(date);
 
        g_signal_connect(G_OBJECT(ds->calendar), "day_selected",
                         G_CALLBACK(date_selection_day_cb), ds);
@@ -1060,17 +1078,34 @@ void date_selection_set(GtkWidget *widget, gint day, gint month, gint year)
        gtk_spin_button_set_value(GTK_SPIN_BUTTON(ds->spin_y), static_cast<gdouble>(year));
 }
 
-
-void date_selection_get(GtkWidget *widget, gint *day, gint *month, gint *year)
+/**
+ * @brief Returns date structure set to value of spin buttons
+ * @param widget #DateSelection
+ * @returns
+ *
+ * Free returned structure with g_date_time_unref();
+ */
+GDateTime *date_selection_get(GtkWidget *widget)
 {
        DateSelection *ds;
+       gint day;
+       gint month;
+       gint year;
+       GDateTime *date;
 
        ds = static_cast<DateSelection *>(g_object_get_data(G_OBJECT(widget), DATE_SELECION_KEY));
-       if (!ds) return;
+       if (!ds)
+               {
+               return nullptr;
+               }
+
+       day = gtk_spin_button_get_value(GTK_SPIN_BUTTON(ds->spin_d));
+       month = gtk_spin_button_get_value(GTK_SPIN_BUTTON(ds->spin_m));
+       year = gtk_spin_button_get_value(GTK_SPIN_BUTTON(ds->spin_y));
+
+       date = g_date_time_new_local(year, month, day, 0, 0, 0);
 
-       if (day) *day = gtk_spin_button_get_value(GTK_SPIN_BUTTON(ds->spin_d));
-       if (month) *month = gtk_spin_button_get_value(GTK_SPIN_BUTTON(ds->spin_m));
-       if (year) *year = gtk_spin_button_get_value(GTK_SPIN_BUTTON(ds->spin_y));
+       return date;
 }
 
 void date_selection_time_set(GtkWidget *widget, time_t t)
@@ -1092,7 +1127,7 @@ time_t date_selection_time_get_unused(GtkWidget *widget)
        gint month = 0;
        gint year = 0;
 
-       date_selection_get(widget, &day, &month ,&year);
+       date_selection_get(widget);
 
        lt.tm_sec = 0;
        lt.tm_min = 0;
index bae5418..ff67e67 100644 (file)
@@ -192,7 +192,7 @@ GtkWidget *pref_toolbar_spacer(GtkWidget *toolbar);
 GtkWidget *date_selection_new();
 
 void date_selection_set(GtkWidget *widget, gint day, gint month, gint year);
-void date_selection_get(GtkWidget *widget, gint *day, gint *month, gint *year);
+GDateTime *date_selection_get(GtkWidget *widget);
 
 void date_selection_time_set(GtkWidget *widget, time_t t);
 time_t date_selection_time_get(GtkWidget *widget);