From abc03bb86f0ed57fa56c7bdeaa2aabf1360555f0 Mon Sep 17 00:00:00 2001
From: Frederic Culot <calcurse@culot.org>
Date: Thu, 8 Oct 2009 16:28:05 +0000
Subject: More work on the weekly calendar view.

---
 src/calendar.c | 20 +++++++++++++++++-
 src/day.c      | 67 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++-
 src/day.h      |  3 ++-
 3 files changed, 87 insertions(+), 3 deletions(-)

(limited to 'src')

diff --git a/src/calendar.c b/src/calendar.c
index ff2c99d..72cc330 100755
--- a/src/calendar.c
+++ b/src/calendar.c
@@ -1,4 +1,4 @@
-/*	$calcurse: calendar.c,v 1.27 2009/08/25 14:51:41 culot Exp $	*/
+/*	$calcurse: calendar.c,v 1.28 2009/10/08 16:28:06 culot Exp $	*/
 
 /*
  * Calcurse - text-based organizer
@@ -483,12 +483,15 @@ draw_weekly_view (window_t *cwin, date_t *current_day, unsigned sunday_first)
   custom_apply_attr (cwin->p, ATTR_HIGHEST);      
   mvwprintw (cwin->p, 2, cwin->w - 9, "(# %02d)", weeknum);
   custom_remove_attr (cwin->p, ATTR_HIGHEST);
+
+#define DAYSLICESNO  6
   
   /* Now draw calendar view. */
   for (j = 0; j < WEEKINDAYS; j++)
     {
       date_t date;
       unsigned attr, item_this_day;
+      int i, slices[DAYSLICESNO];
       
       /* print the day names, with regards to the first day of the week */
       custom_apply_attr (cwin->p, ATTR_HIGHEST);      
@@ -520,10 +523,25 @@ draw_weekly_view (window_t *cwin, date_t *current_day, unsigned sunday_first)
       mvwprintw (cwin->p, 4, 2 + 4 * j, "%02d", t.tm_mday);
       if (attr)
         custom_remove_attr (cwin->p, attr);
+
+      /* Draw slices indicating appointment times. */
+      bzero (slices, DAYSLICESNO * sizeof *slices);
+      if (day_chk_busy_slices (date, DAYSLICESNO, slices))
+        {
+          for (i = 0; i < DAYSLICESNO; i++)
+            if (slices[i])
+              {
+                wattron (cwin->p, A_REVERSE);
+                mvwprintw (cwin->p, 5 + i, 3 + 4 * j, " ");                
+                wattroff (cwin->p, A_REVERSE);
+              }
+        }
       
       /* get next day */
       (void)date_change (&t, 0, 1);
     }
+  
+#undef DAYSLICESNO  
 }
 
 /* Function used to display the calendar panel. */
diff --git a/src/day.c b/src/day.c
index d936372..f8a1532 100755
--- a/src/day.c
+++ b/src/day.c
@@ -1,4 +1,4 @@
-/*	$calcurse: day.c,v 1.50 2009/07/12 16:22:00 culot Exp $	*/
+/*	$calcurse: day.c,v 1.51 2009/10/08 16:28:06 culot Exp $	*/
 
 /*
  * Calcurse - text-based organizer
@@ -520,6 +520,71 @@ day_check_if_item (date_t day)
   return (0);
 }
 
+static unsigned
+fill_slices (int *slices, int slicesno, int first, int last)
+{
+  int i;
+
+  if (first < 0 || last < first)
+    return 0;
+  
+  if (last >= slicesno)
+    last = slicesno - 1; /* Appointment spanning more than one day. */
+  
+  for (i = first; i <= last; i++)
+    slices[i] = 1;
+      
+  return 1;
+}
+
+/*
+ * Fill in the 'slices' vector given as an argument with 1 if there is an
+ * appointment in the corresponding time slice, 0 otherwise.
+ * A 24 hours day is divided into 'slicesno' number of time slices.
+ */
+unsigned
+day_chk_busy_slices (date_t day, int slicesno, int *slices)
+{
+  recur_apoint_llist_node_t *ra;
+  apoint_llist_node_t *a;
+  int slicelen;  
+  const long date = date2sec (day, 0, 0);
+
+  slicelen = DAYINSEC / slicesno;
+
+#define  SLICENUM(tsec)  ((tsec) / slicelen % slicesno)
+
+  pthread_mutex_lock (&(recur_alist_p->mutex));
+  for (ra = recur_alist_p->root; ra != 0; ra = ra->next)
+    if (recur_item_inday (ra->start, ra->exc, ra->rpt->type,
+			  ra->rpt->freq, ra->rpt->until, date))
+      {
+        if (!fill_slices (slices, slicesno, SLICENUM (ra->start),
+                          SLICENUM (ra->start + ra->dur)))
+          {
+            pthread_mutex_unlock (&(recur_alist_p->mutex));
+            return 0;
+          }
+      }
+  pthread_mutex_unlock (&(recur_alist_p->mutex));
+
+  pthread_mutex_lock (&(alist_p->mutex));
+  for (a = alist_p->root; a != 0; a = a->next)
+    if (apoint_inday (a, date))
+      {
+        if (!fill_slices (slices, slicesno, SLICENUM (a->start),
+                          SLICENUM (a->start + a->dur)))
+          {
+            pthread_mutex_unlock (&(alist_p->mutex));
+            return 0;
+          }
+      }
+  pthread_mutex_unlock (&(alist_p->mutex));
+
+#undef SLICENUM  
+  return 1;
+}
+
 /* Request the user to enter a new time. */
 static char *
 day_edit_time (long time)
diff --git a/src/day.h b/src/day.h
index e2db0a5..a1b69d3 100755
--- a/src/day.h
+++ b/src/day.h
@@ -1,4 +1,4 @@
-/*	$calcurse: day.h,v 1.24 2009/07/12 16:22:00 culot Exp $	*/
+/*	$calcurse: day.h,v 1.25 2009/10/08 16:28:06 culot Exp $	*/
 
 /*
  * Calcurse - text-based organizer
@@ -84,6 +84,7 @@ day_items_nb_t    *day_process_storage (date_t *, unsigned, day_items_nb_t *);
 void               day_write_pad (long, int, int, int);
 void               day_popup_item (void);
 int                day_check_if_item (date_t);
+unsigned           day_chk_busy_slices (date_t, int, int *);
 void               day_edit_item (conf_t *);
 int                day_erase_item (long, int, erase_flag_e);
 int                day_cut_item (long, int);
-- 
cgit v1.2.3-70-g09d2