]> Pileus Git - lackey/blob - views/events.c
Cleanup headers and whitespace
[lackey] / views / events.c
1 /*
2  * Copyright (C) 2012 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
54         int      row   = 0;
55         int      count = 0;
56         event_t *event = EVENTS;
57         while (event && (count < min || compare(&event->start, &end) < 0)) {
58                 if (compare(&start, &event->start) <= 0) {
59                         date_t next = event->start;
60                         int newdate = cur.year  != next.year  ||
61                                       cur.month != next.month ||
62                                       cur.day   != next.day   ;
63                         int newtime = cur.hour  != next.hour  ||
64                                       cur.min   != next.min   ;
65                         if ((newdate || newtime) && row != 0)
66                                 row++;
67                         if (newdate && row != 0)
68                                 row++;
69                         if (newdate) {
70                                 wday_t wday = day_of_week(next.year, next.month, next.day);
71                                 wattron(win, A_UNDERLINE);
72                                 mvwprintw(win, row-line, 0,  "%04d-%02d-%02d",
73                                         next.year, next.month+1, next.day+1);
74                                 mvwprintw(win, row-line, 13, "%s, %s %d",
75                                         day_to_string(wday), month_to_string(next.month), next.day);
76                                 wattroff(win, A_UNDERLINE);
77                                 row++;
78                         }
79                         event_line(win, event, row++-line, 4, COLS-2, 1);
80                         if (event->name && event->desc)
81                                 mvwprintw(win, row++-line, 14, "%s", event->desc);
82                         cur = next;
83                         count += 1;
84                 }
85                 event  = event->next;
86         }
87         rows = row;
88 }
89
90 /* Events run */
91 int events_run(int key, mmask_t btn, int row, int col)
92 {
93         int scroll = 0;
94         switch (key)
95         {
96                 case 'g': scroll = -line;    break;
97                 case 'G': scroll =  rows;    break;
98                 case 'j': scroll =  1;       break;
99                 case 'k': scroll = -1;       break;
100                 case 'd': scroll =  LINES/2; break;
101                 case 'u': scroll = -LINES/2; break;
102                 case 'D': scroll =  LINES;   break;
103                 case 'U': scroll = -LINES;   break;
104         }
105         line = CLAMP(line+scroll, 0, rows-1);
106         if (scroll) {
107                 werase(win);
108                 events_draw();
109                 wrefresh(win);
110         }
111         return 0;
112 }