gnome-dateedit patches



Here are some patches to gnome-dateedit.[ch] to add the ability to use
'struct tm' and 'GDate' in several new functions. It also uses g_date
functions
internally to increase i18n.
--- gnome-dateedit.h.old	Thu Apr 13 10:17:49 2000
+++ gnome-dateedit.h	Thu Apr 13 11:01:25 2000
@@ -46,15 +46,22 @@
 
 guint     gnome_date_edit_get_type        (void);
 GtkWidget *gnome_date_edit_new            (time_t the_time, int show_time, int use_24_format);
+GtkWidget *gnome_date_edit_new_tm         (struct tm *the_time, int show_time, int use_24_format);
+GtkWidget *gnome_date_edit_new_gdate      (GDate* the_time, int show_time, int use_24_format);
 GtkWidget *gnome_date_edit_new_flags      (time_t the_time, GnomeDateEditFlags flags);
 
 void      gnome_date_edit_set_time        (GnomeDateEdit *gde, time_t the_time);
+void      gnome_date_edit_set_time_tm     (GnomeDateEdit *gde, struct tm* the_time);
+void      gnome_date_edit_set_time_gdate  (GnomeDateEdit *gde, GDate* the_time);
 void      gnome_date_edit_set_popup_range (GnomeDateEdit *gde, int low_hour, int up_hour);
 time_t    gnome_date_edit_get_date        (GnomeDateEdit *gde);
+struct tm* gnome_date_edit_get_date_tm    (GnomeDateEdit *gde);
 void      gnome_date_edit_set_flags       (GnomeDateEdit *gde, GnomeDateEditFlags flags);
+GDate     *gnome_date_edit_get_date_gdate (GnomeDateEdit *gde);
 int       gnome_date_edit_get_flags       (GnomeDateEdit *gde);
 
 
 END_GNOME_DECLS
 
 #endif
+
--- gnome-dateedit.c.old	Thu Apr 13 10:11:01 2000
+++ gnome-dateedit.c	Thu Apr 13 11:07:02 2000
@@ -75,11 +75,12 @@
 {
 	char buffer [40];
 	gint year, month, day;
+	GDate* gdate;
 
 	gtk_calendar_get_date (calendar, &year, &month, &day);
-
-	/* FIXME: internationalize this - strftime()*/
-	g_snprintf (buffer, sizeof(buffer), "%d/%d/%d", month + 1, day, year);
+	gdate = g_date_new_dmy(day, month + 1, year);
+	g_date_strftime(buffer, sizeof(buffer), "%x", gdate);
+	g_date_free(gdate);
 	gtk_entry_set_text (GTK_ENTRY (gde->date_entry), buffer);
 	gtk_signal_emit (GTK_OBJECT (gde), date_edit_signals [DATE_CHANGED]);
 }
@@ -175,18 +176,13 @@
 {
 	struct tm mtm;
 	GdkCursor *cursor;
+	GDate *gdate;
 
-        /* This code is pretty much just copied from gtk_date_edit_get_date */
-      	sscanf (gtk_entry_get_text (GTK_ENTRY (gde->date_entry)), "%d/%d/%d",
-		&mtm.tm_mon, &mtm.tm_mday, &mtm.tm_year); /* FIXME: internationalize this - strptime()*/
-        
-        mtm.tm_mon--;
-
-	/* Hope the user does not actually mean years early in the A.D. days...
-	 * This date widget will obviously not work for a history program :-)
-	 */
-	if (mtm.tm_year >= 1900)
-		mtm.tm_year -= 1900;
+	gdate = g_date_new();
+	/* Should check for valid sate with g_date_valid(gdate) after parse */
+	g_date_set_parse(gdate, gtk_entry_get_text (GTK_ENTRY (gde->date_entry)));
+	g_date_to_struct_tm(gdate, &mtm);
+	g_date_free(gdate);
 
 	gtk_calendar_select_month (GTK_CALENDAR (gde->calendar), mtm.tm_mon, 1900 + mtm.tm_year);
         gtk_calendar_select_day (GTK_CALENDAR (gde->calendar), mtm.tm_mday);
@@ -384,6 +380,7 @@
 {
 	struct tm *mytm;
 	char buffer [40];
+	GDate* gdate;
 
 	g_return_if_fail(gde != NULL);
 
@@ -394,10 +391,40 @@
 	mytm = localtime (&the_time);
 
 	/* Set the date */
-	g_snprintf (buffer, sizeof(buffer), "%d/%d/%d",
-		    mytm->tm_mon + 1,
-		    mytm->tm_mday,
-		    1900 + mytm->tm_year);
+	gdate = g_date_new_dmy(mytm->tm_mday, mytm->tm_mon + 1, 1900 + mytm->tm_year);
+	g_date_strftime(buffer, sizeof(buffer), "%x", gdate);
+	g_date_free(gdate);
+	gtk_entry_set_text (GTK_ENTRY (gde->date_entry), buffer);
+
+	/* Set the time */
+	if (gde->flags & GNOME_DATE_EDIT_24_HR)
+		strftime (buffer, sizeof (buffer), "%H:%M", mytm);
+	else
+		strftime (buffer, sizeof (buffer), "%I:%M %p", mytm);
+	gtk_entry_set_text (GTK_ENTRY (gde->time_entry), buffer);
+}
+
+/**
+ * gnome_date_edit_set_time_tm:
+ * @gde: the GnomeDateEdit widget
+ * @mytm: The time and date that should be set on the widget
+ *
+ * Changes the displayed date and time in the GnomeDateEdit widget
+ * to be the one represented by @mytm.
+ */
+void
+gnome_date_edit_set_time_tm (GnomeDateEdit *gde, struct tm *mytm)
+{
+	char buffer [40];
+	GDate* gdate;
+
+	g_return_if_fail(gde != NULL);
+	g_return_if_fail(mytm != NULL);
+
+	/* Set the date */
+	gdate = g_date_new_dmy(mytm->tm_mday, mytm->tm_mon + 1, 1900 + mytm->tm_year);
+	g_date_strftime(buffer, sizeof(buffer), "%x", gdate);
+	g_date_free(gdate);
 	gtk_entry_set_text (GTK_ENTRY (gde->date_entry), buffer);
 
 	/* Set the time */
@@ -409,6 +436,27 @@
 }
 
 /**
+ * gnome_date_edit_set_time_gdate:
+ * @gde: the GnomeDateEdit widget
+ * @gdate: The time and date that should be set on the widget
+ *
+ * Changes the displayed date and time in the GnomeDateEdit widget
+ * to be the one represented by @gdate.
+ */
+void
+gnome_date_edit_set_time_gdate (GnomeDateEdit *gde, GDate *gdate)
+{
+	char buffer [40];
+
+	g_return_if_fail(gde != NULL);
+	g_return_if_fail(gdate != NULL);
+
+	/* Set the date */
+	g_date_strftime(buffer, sizeof(buffer), "%x", gdate);
+	gtk_entry_set_text (GTK_ENTRY (gde->date_entry), buffer);
+}
+
+/**
  * gnome_date_edit_set_popup_range:
  * @gde: The GnomeDateEdit widget
  * @low_hour: low boundary for the time-range display popup.
@@ -535,6 +583,51 @@
 }
 
 /**
+ * gnome_date_edit_new_tm:
+ * @the_time: date and time to be displayed on the widget
+ * @show_time: whether time should be displayed
+ * @use_24_format: whether 24-hour format is desired for the time display.
+ *
+ * Creates a new GnomeDateEdit widget which can be used to provide
+ * an easy to use way for entering dates and times.
+ * 
+ * Returns a GnomeDateEdit widget.
+ */
+GtkWidget *
+gnome_date_edit_new_tm (struct tm *the_time, int show_time, int use_24_format)
+{
+	time_t tdate;
+	tdate = mktime(the_time);
+	return gnome_date_edit_new_flags (tdate,
+					  ((show_time ? GNOME_DATE_EDIT_SHOW_TIME : 0)
+					   | (use_24_format ? GNOME_DATE_EDIT_24_HR : 0)));
+}
+
+/**
+ * gnome_date_edit_new_gdate:
+ * @the_time: date and time to be displayed on the widget
+ * @show_time: whether time should be displayed
+ * @use_24_format: whether 24-hour format is desired for the time display.
+ *
+ * Creates a new GnomeDateEdit widget which can be used to provide
+ * an easy to use way for entering dates and times.
+ * 
+ * Returns a GnomeDateEdit widget.
+ */
+GtkWidget *
+gnome_date_edit_new_gdate (GDate* the_time, int show_time, int use_24_format)
+{
+	struct tm tmdate;
+	time_t tdate;
+	g_date_to_struct_tm(the_time, &tmdate);
+	tdate = mktime(&tmdate);
+	return gnome_date_edit_new_flags (tdate,
+					  ((show_time ? GNOME_DATE_EDIT_SHOW_TIME : 0)
+					   | (use_24_format ? GNOME_DATE_EDIT_24_HR : 0)));
+}
+
+
+/**
  * gnome_date_edit_new_flags:
  * @the_time: The initial time for the date editor.
  * @flags: A bitmask of GnomeDateEditFlags values.
@@ -568,21 +661,17 @@
 {
 	struct tm tm = {0};
 	char *str, *flags = NULL;
+	GDate *gdate;
 
 	/* Assert, because we're just hosed if it's NULL */
 	g_assert(gde != NULL);
 	g_assert(GNOME_IS_DATE_EDIT(gde));
 	
-	sscanf (gtk_entry_get_text (GTK_ENTRY (gde->date_entry)), "%d/%d/%d",
-		&tm.tm_mon, &tm.tm_mday, &tm.tm_year); /* FIXME: internationalize this - strptime()*/
-
-	tm.tm_mon--;
-
-	/* Hope the user does not actually mean years early in the A.D. days...
-	 * This date widget will obviously not work for a history program :-)
-	 */
-	if (tm.tm_year >= 1900)
-		tm.tm_year -= 1900;
+	gdate = g_date_new();
+	/* Should check for valid sate with g_date_valid(gdate) after parse */
+	g_date_set_parse(gdate, gtk_entry_get_text (GTK_ENTRY (gde->date_entry)));
+	g_date_to_struct_tm(gdate, &tm);
+	g_date_free(gdate);
 
 	if (gde->flags & GNOME_DATE_EDIT_SHOW_TIME) {
 		char *tokp, *temp;
@@ -618,6 +707,35 @@
 }
 
 /**
+ * gnome_date_edit_get_date_tm:
+ * @gde: The GnomeDateEdit widget
+ *
+ * Returns the time entered in the GnomeDateEdit widget
+ */
+struct tm *
+gnome_date_edit_get_date_tm (GnomeDateEdit *gde)
+{
+	time_t rt = gnome_date_edit_get_date(gde);
+	struct tm *rtm = localtime(&rt);
+	return rtm;
+}
+
+/**
+ * gnome_date_edit_get_date_gdate:
+ * @gde: The GnomeDateEdit widget
+ *
+ * Returns the time entered in the GnomeDateEdit widget
+ */
+GDate     *
+gnome_date_edit_get_date_gdate (GnomeDateEdit *gde)
+{
+	time_t rt = gnome_date_edit_get_date(gde);
+	struct tm *rtm = localtime(&rt);
+	GDate* dt;
+	return dt;
+}
+
+/**
  * gnome_date_edit_set_flags:
  * @gde: The date editor widget whose flags should be changed.
  * @flags: The new bitmask of GnomeDateEditFlags values.
@@ -679,3 +797,5 @@
 
 	return gde->flags;
 }
+
+


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