33 #include <glib/gi18n.h>
34 #include <gdk/gdkkeysyms.h>
38 #include "gnc-cell-renderer-date.h"
39 #include "gnc-cell-renderer-popup-entry.h"
51 static void gcrd_set_property (GObject *
object,
56 static void gcrd_get_property (GObject *
object,
61 static void gcrd_today_clicked (GtkWidget *button,
64 static void gcrd_selected_double_click (GtkWidget *calendar,
67 static void gcrd_cancel_clicked (GtkWidget *popup_window,
70 static void gcrd_ok_clicked (GtkWidget *popup_window,
73 static void gcrd_day_selected (GtkWidget *popup_window,
76 GtkCellEditable *gcrd_start_editing (GtkCellRenderer *cell,
80 GdkRectangle *background_area,
81 GdkRectangle *cell_area,
82 GtkCellRendererState flags);
94 gboolean gcrd_time2dmy (
time64 raw_time, gint *day, gint *month, gint *year);
95 static time64 gcrd_dmy2time (gint day, gint month, gint year);
98 static gchar * gcrd_time2dmy_string (
time64 raw_time);
99 static time64 gcrd_string_dmy2time (
const gchar *date_string);
105 gnc_cell_renderer_date_get_type (
void)
107 static GType cell_text_type = 0;
109 if (!cell_text_type) {
110 static const GTypeInfo cell_text_info = {
114 (GClassInitFunc) gcrd_class_init,
119 (GInstanceInitFunc) gcrd_init,
122 cell_text_type = g_type_register_static (GNC_TYPE_CELL_RENDERER_POPUP,
123 "GncCellRendererDate",
128 return cell_text_type;
140 popup = GNC_CELL_RENDERER_POPUP (date);
142 frame = gtk_frame_new (NULL);
143 gtk_container_add (GTK_CONTAINER (popup->popup_window), frame);
144 gtk_frame_set_shadow_type (GTK_FRAME (frame), GTK_SHADOW_OUT);
146 vbox = gtk_vbox_new (FALSE, 6);
147 gtk_container_add (GTK_CONTAINER (frame), vbox);
148 gtk_container_set_border_width (GTK_CONTAINER (vbox), 6);
150 date->calendar = gtk_calendar_new ();
151 popup->focus_window = date->calendar;
152 gtk_box_pack_start (GTK_BOX (vbox), date->calendar, TRUE, TRUE, 0);
154 date->button_box = gtk_hbutton_box_new ();
155 gtk_box_set_spacing (GTK_BOX (date->button_box), 6);
156 gtk_box_pack_start (GTK_BOX (vbox), date->button_box, FALSE, FALSE, 0);
158 button = gtk_button_new_with_label (_(
"Cancel"));
159 gtk_container_add (GTK_CONTAINER (date->button_box), button);
160 g_signal_connect (button,
"clicked",
161 G_CALLBACK (gcrd_cancel_clicked),
164 date->today_button = gtk_button_new_with_label (_(
"Today"));
165 gtk_container_add (GTK_CONTAINER (date->button_box), date->today_button);
166 g_signal_connect (date->today_button,
"clicked",
167 G_CALLBACK (gcrd_today_clicked),
170 button = gtk_button_new_with_label (_(
"Select"));
171 gtk_container_add (GTK_CONTAINER (date->button_box), button);
172 g_signal_connect (button,
"clicked",
173 G_CALLBACK (gcrd_ok_clicked),
176 g_signal_connect (date->calendar,
"day-selected",
177 G_CALLBACK (gcrd_day_selected),
179 g_signal_connect (date->calendar,
"day-selected-double-click",
180 G_CALLBACK (gcrd_selected_double_click),
186 gtk_widget_show_all (frame);
193 GtkCellRendererClass *cell_class;
194 GObjectClass *gobject_class;
196 popup_class = GNC_CELL_RENDERER_POPUP_CLASS (klass);
197 cell_class = GTK_CELL_RENDERER_CLASS (klass);
198 parent_class = GNC_CELL_RENDERER_POPUP_CLASS (g_type_class_peek_parent (klass));
199 gobject_class = G_OBJECT_CLASS (klass);
201 gobject_class->set_property = gcrd_set_property;
202 gobject_class->get_property = gcrd_get_property;
204 cell_class->start_editing = gcrd_start_editing;
206 popup_class->show_popup = gcrd_show;
207 popup_class->hide_popup = gcrd_hide;
209 g_object_class_install_property (
212 g_param_spec_boolean (
"use-buttons",
221 gcrd_set_property (GObject *
object,
228 date = GNC_CELL_RENDERER_DATE (
object);
231 case PROP_USE_BUTTONS:
232 date->use_buttons = g_value_get_boolean (value);
234 if (date->use_buttons)
235 gtk_widget_show (date->button_box);
237 gtk_widget_hide (date->button_box);
241 G_OBJECT_WARN_INVALID_PROPERTY_ID (
object, param_id, pspec);
247 gcrd_get_property (GObject *
object,
254 date = GNC_CELL_RENDERER_DATE (
object);
257 case PROP_USE_BUTTONS:
258 g_value_set_boolean (value, date->use_buttons);
262 G_OBJECT_WARN_INVALID_PROPERTY_ID (
object, param_id, pspec);
268 gcrd_start_editing (GtkCellRenderer *cell,
272 GdkRectangle *background_area,
273 GdkRectangle *cell_area,
274 GtkCellRendererState flags)
276 GNC_CELL_RENDERER_POPUP (cell)->editing_canceled = FALSE;
278 if (GTK_CELL_RENDERER_CLASS (parent_class)->start_editing) {
279 return GTK_CELL_RENDERER_CLASS (parent_class)->start_editing (
296 if (parent_class->hide_popup) {
297 parent_class->hide_popup (cell);
316 if (parent_class->show_popup) {
317 parent_class->show_popup (cell,
323 date = GNC_CELL_RENDERER_DATE (cell);
325 text = gnc_popup_entry_get_text (GNC_POPUP_ENTRY (GNC_CELL_RENDERER_POPUP (cell)->editable));
327 if (!(g_strcmp0(text,
"")))
330 gcrd_time2dmy ( date->time, &day, &month, &year);
334 date->time = gcrd_string_dmy2time (text);
335 gcrd_time2dmy ( date->time, &day, &month, &year);
338 gtk_calendar_clear_marks (GTK_CALENDAR (date->calendar));
339 gtk_calendar_select_month (GTK_CALENDAR (date->calendar), month - 1, year);
341 gtk_calendar_select_day (GTK_CALENDAR (date->calendar), day);
342 gtk_calendar_mark_day (GTK_CALENDAR (date->calendar), day);
347 gnc_cell_renderer_date_new (gboolean use_buttons)
351 cell = g_object_new (GNC_TYPE_CELL_RENDERER_DATE,
352 "use-buttons", use_buttons,
355 return GTK_CELL_RENDERER (cell);
362 gint year, month, day;
366 gcrd_time2dmy ( today, &day, &month, &year);
368 gtk_calendar_clear_marks (GTK_CALENDAR (cell->calendar));
369 gtk_calendar_select_month (GTK_CALENDAR (cell->calendar), month - 1, year);
370 gtk_calendar_select_day (GTK_CALENDAR (cell->calendar), day);
371 gtk_calendar_mark_day (GTK_CALENDAR (cell->calendar), day);
379 popup = GNC_CELL_RENDERER_POPUP (cell);
381 gcrd_ok_clicked (popup->popup_window, cell);
389 popup = GNC_CELL_RENDERER_POPUP (cell);
391 popup->editing_canceled = TRUE;
392 gnc_cell_renderer_popup_hide (popup);
400 popup = GNC_CELL_RENDERER_POPUP (cell);
402 gcrd_day_selected (popup_window, cell);
404 popup->editing_canceled = FALSE;
405 gnc_cell_renderer_popup_hide (popup);
417 gtk_calendar_get_date (GTK_CALENDAR (cell->calendar),
422 t = gcrd_dmy2time ( day, month + 1, year);
426 str = gcrd_time2dmy_string (t);
428 gnc_popup_entry_set_text (
429 GNC_POPUP_ENTRY (GNC_CELL_RENDERER_POPUP (cell)->editable), str);
435 gcrd_grab_on_window (GdkWindow *window,
436 guint32 activate_time)
438 if ((gdk_pointer_grab (window, TRUE,
439 GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK |
440 GDK_POINTER_MOTION_MASK,
441 NULL, NULL, activate_time) == 0)) {
442 if (gdk_keyboard_grab (window, TRUE,
446 gdk_pointer_ungrab (activate_time);
457 gcrd_time2dmy (
time64 raw_time, gint *day, gint *month, gint *year)
459 struct tm * timeinfo;
463 *day = timeinfo->tm_mday;
464 *month = timeinfo->tm_mon + 1;
465 *year = timeinfo->tm_year + 1900;
472 gcrd_dmy2time (gint day, gint month, gint year)
476 memset (&when, 0,
sizeof (when));
477 when.tm_year = year - 1900;
478 when.tm_mon = month - 1 ;
487 gcrd_time2dmy_string (
time64 raw_time)
495 gcrd_string_dmy2time (
const gchar *date_string)
497 gint year = 0, month = 0, day = 0;
502 memset (&when, 0,
sizeof (when));
503 when.tm_year = year - 1900;
504 when.tm_mon = month - 1 ;
Date and Time handling routines.
void gnc_tm_free(struct tm *time)
free a struct tm* created with gnc_localtime() or gnc_gmtime()
char * qof_print_date(time64 secs)
time64 gnc_mktime(struct tm *time)
calculate seconds from the epoch given a time struct
struct tm * gnc_localtime(const time64 *secs)
fill out a time struct from a 64-bit time value.
gboolean qof_scan_date(const char *buff, int *day, int *month, int *year)
time64 gnc_time(time64 *tbuf)
get the current local time