Re: gtk-calendar



Tim Janik wrote:
> 
> On 9 Jun 2000, Havoc Pennington wrote:
> 
> >
> > Kevin Handy <kth@srv.net> writes:
> > > Is there any current plans to use glib date routines in this function?
> > >
> >
> > It's a very low priority, since it offers no functional improvement to
> > users.
> 
> it would be a good idea to convert the calendar over to
> 1) ease maintainance/avoid code duplication
> 2) give people usage examples of the glib date API (there aren't so many
>    out there that i'm aware of at least)
> 
> so if anyone has the time and nerve to do this, patches will be much
> apprechiated.
> 

Well, I've been playing with it, but still have a lot more work to go,
but
I have replaced a lot of the date functions with their g_date
equivelents.

First a patch to testgtk.c from gtk+-1.2.8 so I can play with this stuff

8225a8226,8258
> 
> /*
>  * Calendar Widget
>  */
> void
> create_calendar (void)
> {
>   static GtkWidget *window = NULL;
>   GtkWidget *cal;
> 
>   if (!window)
>     {
>       window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
>       gtk_widget_set_name (window, "calendar window");
> 
>       gtk_signal_connect (GTK_OBJECT (window), "destroy",
> 			  GTK_SIGNAL_FUNC(gtk_widget_destroyed),
> 			  &window);
> 
>       gtk_window_set_title (GTK_WINDOW (window), "test");
>       gtk_container_set_border_width (GTK_CONTAINER (window), 0);
> 
> 
>       cal = gtk_calendar_new ();
>       gtk_container_add (GTK_CONTAINER (window), cal);
>       gtk_widget_show (cal);
>     }
> 
>   if (!GTK_WIDGET_VISIBLE (window))
>     gtk_widget_show (window);
>   else
>     gtk_widget_destroy (window);
> }
8246a8280
>       { "calendar", create_calendar },


and attached is a patch against gtk+-1.2.8, so that you can tell me if
I am way off the mark before I get too far. I don't think that I have
changed any externally visible structs, calls, etc. I've only removed
various static functions/structures.

Note that I haven't done any extensive testing yet, but it seems to
work OK.
50,65d49
< #define and	    &&	    /* logical (boolean) operators: lower case */
< #define or	    ||
< 
< static const N_int month_length[2][13] =
< {
<   { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 },
<   { 0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }
< };
< 
< static const N_int days_in_months[2][14] =
< {
<   { 0, 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365 },
<   { 0, 0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335, 366 }
< };
< 
< static Z_long  calc_days(N_int year, N_int mm, N_int dd);
71,76d54
< static boolean 
< leap(N_int year)
< {
<   return((((year % 4) == 0) and ((year % 100) != 0)) or ((year % 400) == 0));
< }
< 
81,88c59,63
<   
<   days = calc_days(year, mm, dd);
<   if (days > 0L)
<     {
<       days--;
<       days %= 7L;
<       days++;
<     }
---
>   GDate* gd;
> 
>   gd = g_date_new_dmy(dd, mm, year);
>   days = g_date_weekday(gd);
>   g_date_free(gd);
94,103c69
<   return(52 + ((day_of_week(year,1,1)==4) or (day_of_week(year,12,31)==4)));
< }
< 
< static boolean 
< check_date(N_int year, N_int mm, N_int dd)
< {
<   if (year < 1) return(false);
<   if ((mm < 1) or (mm > 12)) return(false);
<   if ((dd < 1) or (dd > month_length[leap(year)][mm])) return(false);
<   return(true);
---
>   return(52 + ((day_of_week(year,1,1)==4) || (day_of_week(year,12,31)==4)));
116,132d81
< static Z_long 
< year_to_days(N_int year)
< {
<   return( year * 365L + (year / 4) - (year / 100) + (year / 400) );
< }
< 
< 
< static Z_long 
< calc_days(N_int year, N_int mm, N_int dd)
< {
<   boolean lp;
<   
<   if (year < 1) return(0L);
<   if ((mm < 1) or (mm > 12)) return(0L);
<   if ((dd < 1) or (dd > month_length[(lp = leap(year))][mm])) return(0L);
<   return( year_to_days(--year) + days_in_months[lp][mm] + dd );
< }
137c86
<   if (check_date(*year,mm,dd))
---
>   if (g_date_valid_dmy(dd,mm,*year))
156c105,113
<   return( calc_days(year2, mm2, dd2) - calc_days(year1, mm1, dd1) );
---
>   GDate *gd1, *gd2;
>   int days;
> 
>   gd1 = g_date_new_dmy(dd1, mm1, year1);
>   gd2 = g_date_new_dmy(dd2, mm2, year2);
>   days = g_date_julian(gd2) - g_date_julian(gd1);
>   g_date_free(gd1);
>   g_date_free(gd2);
>   return(days);
646c603
<   month_len = month_length[leap (calendar->year)][calendar->month + 1];
---
>   month_len = g_date_days_in_month(calendar->month + 1, calendar->year);
664c621
< 	calendar->selected_day = calendar->selected_day + 1 + month_length[leap (calendar->year)][calendar->month + 1];
---
> 	calendar->selected_day = calendar->selected_day + 1 + g_date_days_in_month(calendar->month + 1, calendar->year);
700c657
<   month_len = month_length[leap (calendar->year)][calendar->month + 1];
---
>   month_len = g_date_days_in_month(calendar->month + 1, calendar->year);
730c687
<   month_len = month_length[leap (calendar->year)][calendar->month + 1];
---
>   month_len = g_date_days_in_month(calendar->month + 1, calendar->year);
764c721
<   month_len = month_length[leap (calendar->year)][calendar->month + 1];
---
>   month_len = g_date_days_in_month(calendar->month + 1, calendar->year);
1958c1915
<   ndays_in_month = month_length[leap (year)][month];
---
>   ndays_in_month = g_date_days_in_month(month, year);
1970c1927
<     ndays_in_prev_month = month_length[leap (year)][month-1];
---
>     ndays_in_prev_month = g_date_days_in_month(month - 1, year);
1972c1929
<     ndays_in_prev_month = month_length[leap (year)][12];
---
>     ndays_in_prev_month = g_date_days_in_month(12, year - 1);


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]