From 0942888b4a4c86d8446eff38b81fe948d3295f40 Mon Sep 17 00:00:00 2001 From: Pietro Gagliardi Date: Wed, 4 May 2016 23:18:48 -0400 Subject: [PATCH] Set the current time on the date-time picker. I think we're good for now... --- _wip/gtkdtp/dtpwidget.c | 79 ++++++++++++++++++++++++++++++++--------- 1 file changed, 62 insertions(+), 17 deletions(-) diff --git a/_wip/gtkdtp/dtpwidget.c b/_wip/gtkdtp/dtpwidget.c index 9bb7dc4b..c6414989 100644 --- a/_wip/gtkdtp/dtpwidget.c +++ b/_wip/gtkdtp/dtpwidget.c @@ -1,7 +1,8 @@ // 4 september 2015 #include "dtp.h" -// TODO imitate gnome-calendar's day/month/year entries +// TODO imitate gnome-calendar's day/month/year entries? +// TODO 24-hour time #define dateTimePickerWidgetType (dateTimePickerWidget_get_type()) #define dateTimePickerWidget(obj) (G_TYPE_CHECK_INSTANCE_CAST((obj), dateTimePickerWidgetType, dateTimePickerWidget)) @@ -30,6 +31,11 @@ struct dateTimePickerWidget { GtkWidget *seconds; GtkWidget *ampm; + gulong hoursBlock; + gulong minutesBlock; + gulong secondsBlock; + gulong ampmBlock; + GdkDevice *keyboard; GdkDevice *mouse; }; @@ -48,14 +54,26 @@ static int realSpinValue(GtkSpinButton *spinButton) return (int) gtk_adjustment_get_value(adj); } +static void setRealSpinValue(GtkSpinButton *spinButton, int value, gulong block) +{ + GtkAdjustment *adj; + + g_signal_handler_block(spinButton, block); + adj = gtk_spin_button_get_adjustment(spinButton); + gtk_adjustment_set_value(adj, value); + g_signal_handler_unblock(spinButton, block); +} + static GDateTime *selected(dateTimePickerWidget *d) { // choose a day for which all times are likely to be valid for the default date in case we're only dealing with time guint year = 1970, month = 1, day = 1; guint hour = 0, minute = 0, second = 0; - if (d->hasDate) + if (d->hasDate) { gtk_calendar_get_date(GTK_CALENDAR(d->calendar), &year, &month, &day); + month++; // GtkCalendar/GDateTime differences + } if (d->hasTime) { hour = realSpinValue(GTK_SPIN_BUTTON(d->hours)); if (realSpinValue(GTK_SPIN_BUTTON(d->ampm)) != 0) @@ -69,20 +87,25 @@ static GDateTime *selected(dateTimePickerWidget *d) static void setLabel(dateTimePickerWidget *d) { GDateTime *dt; - const char *fmt; + char *fmt; char *msg; + gboolean free; dt = selected(d); - // TODO switch to D_FMT + " " + T_FMT? - if (d->hasDate && d->hasTime) - fmt = nl_langinfo(D_T_FMT); - else if (d->hasDate) + free = FALSE; + if (d->hasDate && d->hasTime) { + // don't use D_T_FMT; that's too verbose + fmt = g_strdup_printf("%s %s", nl_langinfo(D_FMT), nl_langinfo(T_FMT)); + free = TRUE; + } else if (d->hasDate) fmt = nl_langinfo(D_FMT); else fmt = nl_langinfo(T_FMT); msg = g_date_time_format(dt, fmt); gtk_button_set_label(GTK_BUTTON(d), msg); g_free(msg); + if (free) + g_free(fmt); g_date_time_unref(dt); } @@ -333,7 +356,7 @@ static void spinboxChanged(GtkSpinButton *sb, gpointer data) dateTimeChanged(d); } -static GtkWidget *newSpinbox(dateTimePickerWidget *d, int min, int max, gint (*input)(GtkSpinButton *, gpointer, gpointer), gboolean (*output)(GtkSpinButton *, gpointer)) +static GtkWidget *newSpinbox(dateTimePickerWidget *d, int min, int max, gint (*input)(GtkSpinButton *, gpointer, gpointer), gboolean (*output)(GtkSpinButton *, gpointer), gulong *block) { GtkWidget *sb; @@ -341,7 +364,7 @@ static GtkWidget *newSpinbox(dateTimePickerWidget *d, int min, int max, gint (*i gtk_spin_button_set_digits(GTK_SPIN_BUTTON(sb), 0); gtk_spin_button_set_wrap(GTK_SPIN_BUTTON(sb), TRUE); gtk_orientable_set_orientation(GTK_ORIENTABLE(sb), GTK_ORIENTATION_VERTICAL); - g_signal_connect(sb, "value-changed", G_CALLBACK(spinboxChanged), d); + *block = g_signal_connect(sb, "value-changed", G_CALLBACK(spinboxChanged), d); if (input != NULL) g_signal_connect(sb, "input", G_CALLBACK(input), NULL); if (output != NULL) @@ -370,7 +393,11 @@ static void setTimeOnly(dateTimePickerWidget *d) static void dateTimePickerWidget_init(dateTimePickerWidget *d) { - // TODO give a more pronounced shadow + GDateTime *dt; + gint year, month, day; + gint hour; + gulong calendarBlock; + d->window = gtk_window_new(GTK_WINDOW_POPUP); gtk_window_set_resizable(GTK_WINDOW(d->window), FALSE); gtk_window_set_attached_to(GTK_WINDOW(d->window), GTK_WIDGET(d)); @@ -384,31 +411,33 @@ static void dateTimePickerWidget_init(dateTimePickerWidget *d) // TODO focus_on_map()? gtk_window_set_has_resize_grip(GTK_WINDOW(d->window), FALSE); gtk_container_set_border_width(GTK_CONTAINER(d->window), 12); + // and make it stand out a bit + gtk_style_context_add_class(gtk_widget_get_style_context(d->window), "frame"); d->box = gtk_box_new(GTK_ORIENTATION_VERTICAL, 6); gtk_container_add(GTK_CONTAINER(d->window), d->box); d->calendar = gtk_calendar_new(); - g_signal_connect(d->calendar, "day-selected", G_CALLBACK(dateChanged), d); + calendarBlock = g_signal_connect(d->calendar, "day-selected", G_CALLBACK(dateChanged), d); gtk_container_add(GTK_CONTAINER(d->box), d->calendar); d->timebox = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 6); gtk_widget_set_valign(d->timebox, GTK_ALIGN_CENTER); gtk_container_add(GTK_CONTAINER(d->box), d->timebox); - d->hours = newSpinbox(d, 0, 11, hoursSpinboxInput, hoursSpinboxOutput); + d->hours = newSpinbox(d, 0, 11, hoursSpinboxInput, hoursSpinboxOutput, &(d->hoursBlock)); gtk_container_add(GTK_CONTAINER(d->timebox), d->hours); gtk_container_add(GTK_CONTAINER(d->timebox), gtk_label_new(":")); - d->minutes = newSpinbox(d, 0, 59, NULL, zeroPadSpinbox); + d->minutes = newSpinbox(d, 0, 59, NULL, zeroPadSpinbox, &(d->minutesBlock)); gtk_container_add(GTK_CONTAINER(d->timebox), d->minutes); gtk_container_add(GTK_CONTAINER(d->timebox), gtk_label_new(":")); - d->seconds = newSpinbox(d, 0, 59, NULL, zeroPadSpinbox); + d->seconds = newSpinbox(d, 0, 59, NULL, zeroPadSpinbox, &(d->secondsBlock)); gtk_container_add(GTK_CONTAINER(d->timebox), d->seconds); // TODO this should be the case, but that interferes with grabs @@ -418,8 +447,7 @@ static void dateTimePickerWidget_init(dateTimePickerWidget *d) gtk_combo_box_text_append(GTK_COMBO_BOX_TEXT(d->ampm), NULL, "AM"); gtk_combo_box_text_append(GTK_COMBO_BOX_TEXT(d->ampm), NULL, "PM"); #endif - // TODO why does pressing the button work oddly? - d->ampm = newSpinbox(d, 0, 1, ampmSpinboxInput, ampmSpinboxOutput); + d->ampm = newSpinbox(d, 0, 1, ampmSpinboxInput, ampmSpinboxOutput, &(d->ampmBlock)); gtk_spin_button_set_numeric(GTK_SPIN_BUTTON(d->ampm), FALSE); gtk_widget_set_valign(d->ampm, GTK_ALIGN_CENTER); gtk_container_add(GTK_CONTAINER(d->timebox), d->ampm); @@ -436,7 +464,24 @@ static void dateTimePickerWidget_init(dateTimePickerWidget *d) d->hasTime = TRUE; d->hasDate = TRUE; - // TODO set current time to now + // set the current date/time + // notice how we block signals from firing + dt = g_date_time_new_now_local(); + g_date_time_get_ymd(dt, &year, &month, &day); + month--; // GDateTime/GtkCalendar differences + g_signal_handler_block(d->calendar, calendarBlock); + gtk_calendar_select_month(GTK_CALENDAR(d->calendar), month, year); + gtk_calendar_select_day(GTK_CALENDAR(d->calendar), day); + g_signal_handler_unblock(d->calendar, calendarBlock); + hour = g_date_time_get_hour(dt); + if (hour >= 12) { + hour -= 12; + setRealSpinValue(GTK_SPIN_BUTTON(d->ampm), 1, d->ampmBlock); + } + setRealSpinValue(GTK_SPIN_BUTTON(d->hours), hour, d->hoursBlock); + setRealSpinValue(GTK_SPIN_BUTTON(d->minutes), g_date_time_get_minute(dt), d->minutesBlock); + setRealSpinValue(GTK_SPIN_BUTTON(d->seconds), g_date_time_get_seconds(dt), d->secondsBlock); + g_date_time_unref(dt); } static void dateTimePickerWidget_dispose(GObject *obj)