Re: gtk-calendar



Tim Janik wrote:
> 
> On Mon, 12 Jun 2000, Kevin Handy wrote:
> 
> > 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.
> 
> please supply unified diffs (-u) for review.
> (just post it again, it's not that large).
> 
Oops, sorry. I've done some more work on it, so here is the current
versions.

Attached is the testgtk changes to add a calendar option, that probably
should have some buttons to control various options, and then the
gtkcalendar.c changes themselves.

I'm not sure what the function 'week_of_year' is really trying to do,
because it validates a date that should be already valid, and modifies
the year value through a pointer, which bothers me, but it was in the
original code so I left it in.
--- testgtk.c.old	Mon Jun 12 11:50:40 2000
+++ testgtk.c	Mon Jun 12 15:04:28 2000
@@ -8223,6 +8223,44 @@
     gtk_widget_destroy (window);
 }
 
+
+/*
+ * 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_calendar_display_options(GTK_CALENDAR(cal),
+        GTK_CALENDAR_SHOW_HEADING |
+        GTK_CALENDAR_SHOW_DAY_NAMES |
+        GTK_CALENDAR_SHOW_WEEK_NUMBERS);
+
+      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);
+}
 /*
  * Main Window and Exit
  */
@@ -8244,6 +8282,7 @@
     {
       { "button box", create_button_box },
       { "buttons", create_buttons },
+      { "calendar", create_calendar },
       { "check buttons", create_check_buttons },
       { "clist", create_clist},
       { "color selection", create_color_selection },
--- gtkcalendar.c.old	Mon Jun 12 10:41:10 2000
+++ gtkcalendar.c	Mon Jun 12 14:45:13 2000
@@ -42,118 +42,26 @@
  * them seperate in case we want to update them if a newer lib_date comes
  * out with fixes.  */
 
-typedef	 unsigned   int	    N_int;
-typedef	 unsigned   long    N_long;
-typedef	 signed	    long    Z_long;
-typedef enum { false = FALSE , true = TRUE } boolean;
-
-#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);
-static N_int   day_of_week(N_int year, N_int mm, N_int dd);
-static Z_long  dates_difference(N_int year1, N_int mm1, N_int dd1,
-				N_int year2, N_int mm2, N_int dd2);
-static N_int   weeks_in_year(N_int year);
-
-static boolean 
-leap(N_int year)
-{
-  return((((year % 4) == 0) and ((year % 100) != 0)) or ((year % 400) == 0));
-}
-
-static N_int 
-day_of_week(N_int year, N_int mm, N_int dd)
-{
-  Z_long  days;
-  
-  days = calc_days(year, mm, dd);
-  if (days > 0L)
-    {
-      days--;
-      days %= 7L;
-      days++;
-    }
-  return( (N_int) days );
-}
-
-static N_int weeks_in_year(N_int year)
-{
-  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);
-}
-
-static N_int 
-week_number(N_int year, N_int mm, N_int dd)
+static gboolean 
+week_of_year(guint *week, guint *year, guint mm, guint dd)
 {
-  N_int first;
-  
-  first = day_of_week(year,1,1) - 1;
-  return( (N_int) ( (dates_difference(year,1,1, year,mm,dd) + first) / 7L ) +
-	  (first < 4) );
-}
-
-static Z_long 
-year_to_days(N_int year)
-{
-  return( year * 365L + (year / 4) - (year / 100) + (year / 400) );
-}
+  GDate gd;
 
-
-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 );
-}
-
-static boolean 
-week_of_year(N_int *week, N_int *year, N_int mm, N_int dd)
-{
-  if (check_date(*year,mm,dd))
+  if (g_date_valid_dmy(dd,mm,*year))
     {
-      *week = week_number(*year,mm,dd);
+      g_date_clear (&gd, 1);
+      g_date_set_dmy(&gd, dd, mm, *year);
+      *week = g_date_sunday_week_of_year(&gd);
       if (*week == 0) 
-	*week = weeks_in_year(--(*year));
-      else if (*week > weeks_in_year(*year))
+	*week = g_date_sunday_weeks_in_year(--(*year));
+      else if (*week > g_date_sunday_weeks_in_year(*year))
 	{
 	  *week = 1;
 	  (*year)++;
 	}
-      return(true);
+      return(TRUE);
     }
-  return(false);
-}
-
-static Z_long 
-dates_difference(N_int year1, N_int mm1, N_int dd1,
-		 N_int year2, N_int mm2, N_int dd2)
-{
-  return( calc_days(year2, mm2, dd2) - calc_days(year1, mm1, dd1) );
+  return(FALSE);
 }
 
 /** END OF lib_date routines ************************************************/
@@ -643,7 +551,7 @@
   else 
     calendar->month--;
   
-  month_len = month_length[leap (calendar->year)][calendar->month + 1];
+  month_len = g_date_days_in_month(calendar->month + 1, calendar->year);
   
   gtk_calendar_freeze (calendar);
   gtk_calendar_compute_days (calendar);
@@ -661,7 +569,7 @@
   else
     {
       if (calendar->selected_day < 0)
-	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);
       gtk_calendar_select_day (calendar, calendar->selected_day);
     }
   
@@ -697,7 +605,7 @@
   gtk_signal_emit (GTK_OBJECT (calendar),
 		   gtk_calendar_signals[MONTH_CHANGED_SIGNAL]);
   
-  month_len = month_length[leap (calendar->year)][calendar->month + 1];
+  month_len = g_date_days_in_month(calendar->month + 1, calendar->year);
   
   if (month_len < calendar->selected_day)
     {
@@ -727,7 +635,7 @@
   gtk_signal_emit (GTK_OBJECT (calendar),
 		   gtk_calendar_signals[MONTH_CHANGED_SIGNAL]);
   
-  month_len = month_length[leap (calendar->year)][calendar->month + 1];
+  month_len = g_date_days_in_month(calendar->month + 1, calendar->year);
   
   if (month_len < calendar->selected_day)
     {
@@ -761,7 +669,7 @@
   gtk_signal_emit (GTK_OBJECT (calendar),
 		   gtk_calendar_signals[MONTH_CHANGED_SIGNAL]);
   
-  month_len = month_length[leap (calendar->year)][calendar->month + 1];
+  month_len = g_date_days_in_month(calendar->month + 1, calendar->year);
   
   if (month_len < calendar->selected_day)
     {
@@ -1948,6 +1856,7 @@
   gint row;
   gint col;
   gint day;
+  GDate gd;
   
   g_return_if_fail (calendar != NULL);
   g_return_if_fail (GTK_IS_CALENDAR (calendar));
@@ -1955,9 +1864,11 @@
   year = calendar->year;
   month = calendar->month + 1;
   
-  ndays_in_month = month_length[leap (year)][month];
+  ndays_in_month = g_date_days_in_month(month, year);
   
-  first_day = day_of_week (year, month, 1);
+  g_date_clear (&gd, 1);
+  g_date_set_dmy(&gd, 1, month, year);
+  first_day = g_date_weekday(&gd);
   
   if (calendar->display_flags & GTK_CALENDAR_WEEK_START_MONDAY)
     first_day--;
@@ -1967,9 +1878,9 @@
   
   /* Compute days of previous month */
   if (month > 1)
-    ndays_in_prev_month = month_length[leap (year)][month-1];
+    ndays_in_prev_month = g_date_days_in_month(month - 1, year);
   else
-    ndays_in_prev_month = month_length[leap (year)][12];
+    ndays_in_prev_month = g_date_days_in_month(12, year - 1);
   day = ndays_in_prev_month - first_day + 1;
   
   row = 0;


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