]> Pileus Git - lackey/blobdiff - view/year.c
Add events view
[lackey] / view / year.c
index 19554e969ceb429d2a868f9b1775cfa387518919..086c0e8e2f4efb6a47082a542711c852239e7763 100644 (file)
@@ -1,8 +1,27 @@
+/*
+ * Copyright (C) 2012 Andy Spencer <andy753421@gmail.com>
+ * 
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ * 
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ * 
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
 #include <string.h>
 #include <ncurses.h>
 
-#include "main.h"
 #include "util.h"
+#include "date.h"
+#include "event.h"
+#include "screen.h"
 
 /* Constants */
 #define MW (2*7+6)
@@ -13,6 +32,7 @@ static WINDOW *win;
 /* Helper functions */
 static void print_month(month_t month, int y, int x)
 {
+       event_t    *event = EVENTS;
        const char *name  = month_to_string(month);
        const int   start = start_of_month(YEAR, month);
        const char  days  = days_in_month(YEAR, month);
@@ -23,7 +43,22 @@ static void print_month(month_t month, int y, int x)
        for (int d = 0; d < days; d++) {
                int row = (start + d) / 7;
                int col = (start + d) % 7;
-               mvwprintw(win, y+2+row, x+col*3, "%d", d+1);
+
+               int busy = 0;
+               while (event && before(&event->start, YEAR, month, d, 24, 0)) {
+                       if (!before(&event->start, YEAR, month, d, 0, 0))
+                               busy = 1;
+                       event = event->next;
+               }
+
+               int today = month == MONTH && d == DAY;
+
+               int attr  = (busy  ? A_BOLD|A_UNDERLINE : A_DIM)
+                         | (today ? A_REVERSE          : 0    );
+
+               wattron(win, attr);
+               mvwprintw(win, y+2+row, x+col*3, "%2d", d+1);
+               wattroff(win, attr);
        }
 }
 
@@ -33,6 +68,11 @@ void year_init(WINDOW *_win)
        win = _win;
 }
 
+/* Year size */
+void year_size(int rows, int cols)
+{
+}
+
 /* Year draw */
 void year_draw(void)
 {
@@ -78,5 +118,54 @@ void year_draw(void)
 /* Year run */
 int year_run(int key, mmask_t btn, int row, int col)
 {
+       day_t d = DAY;
+       month_t m = MONTH;
+       year_t y = YEAR;
+       wday_t day = day_of_week(YEAR, MONTH, DAY);
+       int week = (start_of_month(y, m) + d) / 7;
+       int dir = 0;
+
+       /* Step years */
+       if (key == 'i')
+               YEAR--;
+       if (key == 'o')
+               YEAR++;
+
+       /* Get direction */
+       if (key == 'h' || key == 'k')
+               dir = -1;
+       if (key == 'j' || key == 'l')
+               dir =  1;
+
+       /* Step up/down */
+       if (key == 'j' || key == 'k') {
+               for (int i = 0; i < 90/7; i++) {
+                       add_days(&y, &m, &d, dir*7);
+                       if (day_of_week(y, m, d) == day &&
+                           y == YEAR && m%3 == MONTH%3) {
+                               MONTH = m;
+                               DAY = d;
+                               break;
+                       }
+               }
+       }
+
+       /* Step left/right */
+       if (key == 'h' || key == 'l') {
+               for (int i = 0; i < 90; i++) {
+                       add_days(&y, &m, &d, dir);
+                       if ((start_of_month(y, m) + d) / 7 == week &&
+                           y == YEAR && m/3 == MONTH/3) {
+                               MONTH = m;
+                               DAY = d;
+                               break;
+                       }
+               }
+       }
+
+       /* Refresh */
+       werase(win);
+       year_draw();
+       wrefresh(win);
        return 0;
 }