diff --git a/source/libublsettings-gtk3.c b/source/libublsettings-gtk3.c index 4b0533a..4712085 100644 --- a/source/libublsettings-gtk3.c +++ b/source/libublsettings-gtk3.c @@ -1174,5 +1174,61 @@ gboolean yon_gtk_tree_iter_get_from_combo_box_id(GtkComboBox *combo, GtkTreeMode return 1; } +int yon_calendar_set_orientation = 0; +void yon_calendar_set_date_orientation(int orientation){ + yon_calendar_set_orientation = orientation; +} + +void yon_on_date_selected(GtkWidget *self,GtkWidget *target){ + { + if (self){}; + unsigned int year=0, month=0, day=0; + gtk_calendar_get_date(GTK_CALENDAR(self),&year,&month,&day); + char *dy = yon_char_from_int(day); + char *mn = yon_char_from_int(month+1); + char *yr = yon_char_from_int(year); + if (month+1<10) mn = yon_char_append("0",mn); + if (day<10) dy = yon_char_append("0",dy); + + GDateTime *datetime = g_date_time_new_now_local(); + GDateTime *current_datetime = g_date_time_new_local(year,month+1,day,0,0,0); + int comparison = g_date_time_compare(current_datetime,datetime); + if ((comparison <0&&yon_calendar_set_orientation>0)||(comparison>0&&yon_calendar_set_orientation<0)){ + gtk_entry_set_text(GTK_ENTRY(target),g_date_time_format(datetime,"%Y-%m-%d")); + } else { + char *date_string = yon_char_unite(yr,"-",mn,"-",dy,NULL); + gtk_entry_set_text(GTK_ENTRY(target), date_string); + } + free(dy); + free(mn); + free(yr); + } +} + +void yon_on_popover_closed(GtkWidget *self){ + gtk_widget_destroy(self); +} + +void yon_calendar_popover_open(GtkEntry *TargetEntry,GtkWidget *PopupTarget){ + g_return_if_fail(GTK_IS_ENTRY(TargetEntry)); + g_return_if_fail(GTK_IS_WIDGET(PopupTarget)); + GtkWidget *popover = gtk_popover_new(PopupTarget); + GtkWidget *calendar = gtk_calendar_new(); + gtk_container_add(GTK_CONTAINER(popover),calendar); + gtk_widget_show_all(popover); + gtk_popover_popup(GTK_POPOVER(popover)); + + const char *date_str = gtk_entry_get_text(TargetEntry); + if (date_str&&strcmp(date_str,"")){ + int date_size; + config_str date_parsed = yon_char_parse((char*)date_str,&date_size,"-"); + gtk_calendar_select_day(GTK_CALENDAR(calendar),atoi(date_parsed[2])); + gtk_calendar_select_month(GTK_CALENDAR(calendar),atoi(date_parsed[1])-1,atoi(date_parsed[0])); + } + + g_signal_connect(G_OBJECT(calendar),"day-selected",G_CALLBACK(yon_on_date_selected),TargetEntry); + g_signal_connect(G_OBJECT(popover),"closed",G_CALLBACK(yon_on_popover_closed),NULL); + +} #endif \ No newline at end of file diff --git a/source/libublsettings-gtk3.h b/source/libublsettings-gtk3.h index c11fa39..ea45a40 100644 --- a/source/libublsettings-gtk3.h +++ b/source/libublsettings-gtk3.h @@ -469,4 +469,8 @@ void yon_gtk_tree_store_copy_full(GtkTreeStore *source, GtkTreeStore *destinatio gboolean yon_gtk_tree_store_find_full(GtkTreeModel *model, GtkTreeIter *iter, gint column, const gchar *target); -gboolean yon_gtk_tree_iter_get_from_combo_box_id(GtkComboBox *combo, GtkTreeModel *model, GtkTreeIter *iter); \ No newline at end of file +gboolean yon_gtk_tree_iter_get_from_combo_box_id(GtkComboBox *combo, GtkTreeModel *model, GtkTreeIter *iter); + +void yon_calendar_set_date_orientation(int orientation); + +void yon_calendar_popover_open(GtkEntry *TargetEntry,GtkWidget *PopupTarget); \ No newline at end of file