From 83ada79960e1b2bb755b5b8505cc57f42879d1d7 Mon Sep 17 00:00:00 2001 From: Dave Peticolas Date: Tue, 4 Jul 2000 19:39:09 +0000 Subject: [PATCH] Make date widget respond to the same accelerator keys as in the register. git-svn-id: svn+ssh://svn.gnucash.org/repo/gnucash/trunk@2535 57a11ea4-9604-0410-9ed3-97b8803252fd --- ChangeLog | 8 ++ src/engine/date.c | 68 ++++++++++++++--- src/engine/date.h | 2 + src/gnome/gnc-dateedit.c | 154 ++++++++++++++++++++++++++++++++++++--- src/register/datecell.c | 83 +++------------------ 5 files changed, 222 insertions(+), 93 deletions(-) diff --git a/ChangeLog b/ChangeLog index c3643fb0bc..41ba300cd9 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,11 @@ +2000-07-04 Dave Peticolas + + * src/engine/date.c (xaccValidateDate): move this function from + src/register/datecell.c to here, so it can be used elsewhere. + + * src/gnome/gnc-dateedit.c: make the widget respond to the same + accelerators as the register date cell. + 2000-06-28 Dave Peticolas * src/gnome/dialog-budget.c (entry_down_button_clicked): bug fix. diff --git a/src/engine/date.c b/src/engine/date.c index 9d4b304268..1019f810f0 100644 --- a/src/engine/date.c +++ b/src/engine/date.c @@ -346,17 +346,61 @@ xaccScanDateS (const char *str) return (xaccDMYToSec (day,month,year)); } +/* ================================================ */ +/* february default is 28, and patched below */ +static char days_in_month[12] = { 31,28,31,30,31,30,31,31,30,31,30,31 }; + +static void +xaccValidateDateInternal (struct tm *date, int recur) +{ + int day, month, year; + + /* avoid infinite recursion */ + if (1 < recur) return; + + day = date->tm_mday; + month = date->tm_mon + 1; + year = date->tm_year + 1900; + + /* adjust days in february for leap year */ + if (((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0)) { + days_in_month[1] = 29; + } else { + days_in_month[1] = 28; + } + + /* the "% 12" business is because month might not be valid!*/ + + while (day > days_in_month[(month+11) % 12]) { + day -= days_in_month[(month+11) % 12]; + month++; + } + while (day < 1) { + month--; + day += days_in_month[(month+11) % 12]; + } + while (month > 12) { + month -= 12; + year++; + } + while (month < 1) { + month += 12; + year--; + } + + date->tm_mday = day; + date->tm_mon = month - 1; + date->tm_year = year - 1900; + + /* do it again, in case leap-year scrolling messed things up */ + xaccValidateDateInternal (date, ++recur); +} + +void +xaccValidateDate (struct tm *date) +{ + xaccValidateDateInternal (date, 0); +} + /********************** END OF FILE *********************************\ \********************************************************************/ - - -/* - Local Variables: - tab-width: 2 - indent-tabs-mode: nil - mode: c-mode - c-indentation-style: gnu - eval: (c-set-offset 'block-open '-) - End: -*/ - diff --git a/src/engine/date.h b/src/engine/date.h index 1166bce05e..223bd67e8f 100644 --- a/src/engine/date.h +++ b/src/engine/date.h @@ -110,4 +110,6 @@ void xaccTransSetDateStr (Transaction *trans, char *str); time_t xaccDMYToSec (int day, int month, int year); time_t xaccScanDateS (const char *buff); +void xaccValidateDate (struct tm *date); + #endif /* __XACC_DATE_H__ */ diff --git a/src/gnome/gnc-dateedit.c b/src/gnome/gnc-dateedit.c index cb068556fc..b9a874de7e 100644 --- a/src/gnome/gnc-dateedit.c +++ b/src/gnome/gnc-dateedit.c @@ -58,6 +58,11 @@ static void gnc_date_edit_forall (GtkContainer *container, gboolean include_internals, GtkCallback callback, gpointer callbabck_data); +static struct tm gnc_date_edit_get_date_internal (GNCDateEdit *gde); +static int date_accel_key_press(GtkWidget *widget, + GdkEventKey *event, + gpointer data); + static GtkHBoxClass *parent_class; @@ -133,7 +138,7 @@ key_press_popup (GtkWidget *widget, GdkEventKey *event, gpointer data) GNCDateEdit *gde; if (event->keyval != GDK_Escape) - return FALSE; + return date_accel_key_press(widget, event, data); gde = data; gtk_signal_emit_stop_by_name (GTK_OBJECT (widget), "key_press_event"); @@ -473,6 +478,123 @@ gnc_date_edit_set_popup_range (GNCDateEdit *gde, int low_hour, int up_hour) fill_time_popup(NULL, gde); } +/* This code should be kept in sync with src/register/datecell.c */ +static int +date_accel_key_press(GtkWidget *widget, GdkEventKey *event, gpointer data) +{ + GNCDateEdit *gde = data; + struct tm tm; + + switch (event->keyval) { + case GDK_plus: + case GDK_KP_Add: + case GDK_equal: + case GDK_KP_Equal: + case GDK_underscore: + case GDK_minus: + case GDK_KP_Subtract: + case GDK_bracketright: + case GDK_braceright: + case GDK_bracketleft: + case GDK_braceleft: + case GDK_M: + case GDK_m: + case GDK_H: + case GDK_h: + case GDK_Y: + case GDK_y: + case GDK_R: + case GDK_r: + case GDK_T: + case GDK_t: + break; + default: + return FALSE; + } + + gtk_signal_emit_stop_by_name (GTK_OBJECT (widget), "key_press_event"); + + tm = gnc_date_edit_get_date_internal (gde); + + switch (event->keyval) { + case GDK_plus: + case GDK_KP_Add: + case GDK_equal: + case GDK_KP_Equal: + /* increment day */ + tm.tm_mday ++; + break; + + case GDK_underscore: + case GDK_minus: + case GDK_KP_Subtract: + /* decrement day */ + tm.tm_mday --; + break; + + case GDK_bracketright: + case GDK_braceright: + /* increment month */ + tm.tm_mon ++; + break; + + case GDK_bracketleft: + case GDK_braceleft: + /* decrement month */ + tm.tm_mon --; + break; + + case GDK_M: + case GDK_m: + /* beginning of month */ + tm.tm_mday = 1; + break; + + case GDK_H: + case GDK_h: + /* end of month */ + tm.tm_mon ++; + tm.tm_mday = 0; + break; + + case GDK_Y: + case GDK_y: + /* beginning of year */ + tm.tm_mday = 1; + tm.tm_mon = 0; + break; + + case GDK_R: + case GDK_r: + /* end of year */ + tm.tm_mday = 31; + tm.tm_mon = 11; + break; + + case GDK_T: + case GDK_t: { + /* today */ + time_t secs; + struct tm *now; + + time (&secs); + now = localtime (&secs); + tm = *now; + break; + } + } + + xaccValidateDate(&tm); + + gnc_date_edit_set_time(gde, mktime(&tm)); + + gtk_calendar_select_month (GTK_CALENDAR (gde->calendar), tm.tm_mon, + 1900 + tm.tm_year); + gtk_calendar_select_day (GTK_CALENDAR (gde->calendar), tm.tm_mday); + + return TRUE; +} + static void create_children (GNCDateEdit *gde) { @@ -484,7 +606,9 @@ create_children (GNCDateEdit *gde) gtk_widget_set_usize (gde->date_entry, 90, 0); gtk_box_pack_start (GTK_BOX (gde), gde->date_entry, TRUE, TRUE, 0); gtk_widget_show (gde->date_entry); - + gtk_signal_connect (GTK_OBJECT (gde->date_entry), "key_press_event", + GTK_SIGNAL_FUNC(date_accel_key_press), gde); + gde->date_button = gtk_button_new (); gtk_signal_connect (GTK_OBJECT (gde->date_button), "clicked", GTK_SIGNAL_FUNC (select_clicked), gde); @@ -607,14 +731,8 @@ gnc_date_edit_new_flags (time_t the_time, GNCDateEditFlags flags) return GTK_WIDGET (gde); } -/** - * gnc_date_edit_get_date: - * @gde: The GNCDateEdit widget - * - * Returns the time entered in the GNCDateEdit widget - */ -time_t -gnc_date_edit_get_date (GNCDateEdit *gde) +static struct tm +gnc_date_edit_get_date_internal (GNCDateEdit *gde) { struct tm tm = {0}; char *str, *flags = NULL; @@ -666,6 +784,22 @@ gnc_date_edit_get_date (GNCDateEdit *gde) tm.tm_isdst = -1; + return tm; +} + +/** + * gnc_date_edit_get_date: + * @gde: The GNCDateEdit widget + * + * Returns the time entered in the GNCDateEdit widget + */ +time_t +gnc_date_edit_get_date (GNCDateEdit *gde) +{ + struct tm tm; + + tm = gnc_date_edit_get_date_internal (gde); + return mktime (&tm); } diff --git a/src/register/datecell.c b/src/register/datecell.c index f06c99dbd3..73a11ddde8 100644 --- a/src/register/datecell.c +++ b/src/register/datecell.c @@ -36,11 +36,11 @@ #include #include -#include "../engine/date.h" /* hack alert -- don't include from engine directory */ +#include "date.h" /* hack alert -- don't include from engine directory */ +#include "util.h" #include "basiccell.h" #include "datecell.h" -#include "util.h" static void setDateCellValue (BasicCell *, const char *); @@ -67,60 +67,6 @@ xaccParseDate (struct tm *parsed, const char * datestr) parsed->tm_year = iyear-1900; } -/* ================================================ */ -/* february default is 28, and patched below */ -static -char days_in_month[12] = { 31,28,31,30,31,30,31,31,30,31,30,31 }; - -/* ================================================ */ - -static void -xaccValidateDate (struct tm *date, int recur) -{ - int day, month, year; - - /* avoid infinite recursion */ - if (1 < recur) return; - - day = date->tm_mday; - month = date->tm_mon + 1; - year = date->tm_year + 1900; - - /* adjust days in february for leap year */ - if (((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0)) { - days_in_month[1] = 29; - } else { - days_in_month[1] = 28; - } - - /* the "% 12" business is because month might not be valid!*/ - - while (day > days_in_month[(month+11) % 12]) { - day -= days_in_month[(month+11) % 12]; - month++; - } - while (day < 1) { - month--; - day += days_in_month[(month+11) % 12]; - } - while (month > 12) { - month -= 12; - year++; - } - while (month < 1) { - month += 12; - year--; - } - - date->tm_mday = day; - date->tm_mon = month - 1; - date->tm_year = year - 1900; - - /* do it again, in case leap-year scrolling messed things up */ - xaccValidateDate (date, ++recur); -} - - /* ================================================ */ static char * @@ -144,7 +90,7 @@ DateCellHelpValue(BasicCell *bcell) time.tm_year = cell->date.tm_year; } - xaccValidateDate(&time, GNC_F); + xaccValidateDate(&time); mktime(&time); strftime(string, sizeof(string), "%A %d %B %Y", &time); @@ -177,6 +123,7 @@ DateEnter (BasicCell *_cell, const char * curr, /* ================================================ */ +/* This code should be kept in sync with src/gnome/gnc-dateedit.c */ static const char * DateMV (BasicCell *_cell, const char *oldval, @@ -188,7 +135,6 @@ DateMV (BasicCell *_cell, { DateCell *cell = (DateCell *) _cell; gncBoolean accept = GNC_F; - gncBoolean accel = GNC_F; struct tm *date; char buff[30]; char *datestr; @@ -245,45 +191,42 @@ DateMV (BasicCell *_cell, case '=': /* increment day */ date->tm_mday ++; - accel = GNC_T; break; case '_': case '-': /* decrement day */ date->tm_mday --; - accel = GNC_T; break; case '}': case ']': /* increment month */ date->tm_mon ++; - accel = GNC_T; break; case '{': case '[': - /* decrment month */ + /* decrement month */ date->tm_mon --; - accel = GNC_T; break; case 'M': case 'm': - /* begining of month */ + /* beginning of month */ date->tm_mday = 1; break; case 'H': case 'h': /* end of month */ - date->tm_mday = days_in_month[date->tm_mon]; + date->tm_mon ++; + date->tm_mday = 0; break; case 'Y': case 'y': - /* begining of year */ + /* beginning of year */ date->tm_mday = 1; date->tm_mon = 0; break; @@ -312,9 +255,7 @@ DateMV (BasicCell *_cell, return NULL; } - if (accel) { - xaccValidateDate (date, 0); - } + xaccValidateDate (date); printDate (buff, date->tm_mday, date->tm_mon+1, date->tm_year+1900); @@ -446,7 +387,7 @@ xaccSetDateCellValue (DateCell *cell, int day, int mon, int year) dada.tm_mon = mon-1; dada.tm_year = year - 1900; - xaccValidateDate (&dada, 0); + xaccValidateDate (&dada); cell->date.tm_mday = dada.tm_mday; cell->date.tm_mon = dada.tm_mon; cell->date.tm_year = dada.tm_year; @@ -502,7 +443,7 @@ xaccSetDateCellValueSecsL (DateCell *cell, long long secs) stm = localtime (&rem); cell->date = *stm; cell->date.tm_year += 32 * yrs; - xaccValidateDate (&(cell->date), 0); + xaccValidateDate (&(cell->date)); } else { /* OK, time value is an unsigned 32-bit int */ time_t sicko;