]> Pileus Git - lackey/blob - views/events.c
eec7ab002c50c5108e23ead3eccf9e81a7584325
[lackey] / views / events.c
1 /*
2  * Copyright (C) 2012-2013 Andy Spencer <andy753421@gmail.com>
3  *
4  * This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 3 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
16  */
17
18 #define DEBUG
19
20 #include <ncurses.h>
21
22 #include "util.h"
23 #include "date.h"
24 #include "cal.h"
25 #include "view.h"
26
27 /* Static data */
28 static WINDOW *win;
29 static int     line;
30 static int     rows;
31
32 /* Events init */
33 void events_init(WINDOW *_win)
34 {
35         win = _win;
36 }
37
38 /* Events size */
39 void events_size(int rows, int cols)
40 {
41 }
42
43 /* Events draw */
44 void events_draw(void)
45 {
46         int days = 2*7;
47         int min  = 12;
48
49         date_t start = {YEAR, MONTH, DAY,    0, 0};
50         date_t cur   = {YEAR, MONTH, DAY-1,  0, 0};
51         date_t end   = {YEAR, MONTH, DAY,   24, 0};
52         add_days(&end.year, &end.month, &end.day, days);
53         cal_load(YEAR, MONTH, DAY, days);
54
55         int      row   = 0;
56         int      count = 0;
57         event_t *event = EVENTS;
58         while (event && (count < min || compare(&event->start, &end) < 0)) {
59                 if (compare(&start, &event->start) <= 0) {
60                         date_t next = event->start;
61                         int newdate = cur.year  != next.year  ||
62                                       cur.month != next.month ||
63                                       cur.day   != next.day   ;
64                         int newtime = cur.hour  != next.hour  ||
65                                       cur.min   != next.min   ;
66                         if ((newdate || newtime) && row != 0)
67                                 row++;
68                         if (newdate && row != 0)
69                                 row++;
70                         if (newdate) {
71                                 wday_t wday = day_of_week(next.year, next.month, next.day);
72                                 wattron(win, A_BOLD);
73                                 mvwprintw(win, row-line, 0,  "%04d-%02d-%02d",
74                                         next.year, next.month+1, next.day+1);
75                                 mvwprintw(win, row-line, 13, "%s, %s %d",
76                                         day_to_string(wday), month_to_string(next.month), next.day);
77                                 wattroff(win, A_BOLD);
78                                 row++;
79                         }
80                         event_line(win, event, row++-line, 4, COLS-2, 1);
81                         if (event->name && event->desc)
82                                 mvwprintw(win, row++-line, 14, "%s", event->desc);
83                         cur = next;
84                         count += 1;
85                 }
86                 event  = event->next;
87         }
88         rows = row;
89 }
90
91 /* Events run */
92 int events_run(int key, mmask_t btn, int row, int col)
93 {
94         int scroll = 0;
95         switch (key)
96         {
97                 case 'g': scroll = -line;    break;
98                 case 'G': scroll =  rows;    break;
99                 case 'j': scroll =  1;       break;
100                 case 'k': scroll = -1;       break;
101                 case 'd': scroll =  LINES/2; break;
102                 case 'u': scroll = -LINES/2; break;
103                 case 'D': scroll =  LINES;   break;
104                 case 'U': scroll = -LINES;   break;
105         }
106         line = CLAMP(line+scroll, 0, rows-1);
107         if (scroll) {
108                 werase(win);
109                 events_draw();
110                 wrefresh(win);
111         }
112         return scroll;
113 }