]> Pileus Git - lackey/blob - views/events.c
Convert YEAR/MONTH/DAY to Selection struct
[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 #include <ncurses.h>
19
20 #include "util.h"
21 #include "date.h"
22 #include "cal.h"
23 #include "view.h"
24
25 /* Static data */
26 static WINDOW *win;
27 static int     line;
28 static int     rows;
29
30 /* Events init */
31 void events_init(WINDOW *_win)
32 {
33         win = _win;
34 }
35
36 /* Events size */
37 void events_size(int rows, int cols)
38 {
39 }
40
41 /* Events draw */
42 void events_draw(void)
43 {
44         int days = 2*7;
45         int min  = 12;
46
47         date_t start = {SEL.year, SEL.month, SEL.day,    0, 0};
48         date_t cur   = {SEL.year, SEL.month, SEL.day-1,  0, 0};
49         date_t end   = {SEL.year, SEL.month, SEL.day,   24, 0};
50         add_days(&end.year, &end.month, &end.day, days);
51         cal_load(SEL.year, SEL.month, SEL.day, days);
52
53         int      row   = 0;
54         int      count = 0;
55         event_t *event = EVENTS;
56         while (event && (count < min || compare(&event->start, &end) < 0)) {
57                 if (compare(&start, &event->start) <= 0) {
58                         date_t next = event->start;
59                         int newdate = cur.year  != next.year  ||
60                                       cur.month != next.month ||
61                                       cur.day   != next.day   ;
62                         int newtime = cur.hour  != next.hour  ||
63                                       cur.min   != next.min   ;
64                         if ((newdate || newtime) && row != 0)
65                                 row++;
66                         if (newdate && row != 0)
67                                 row++;
68                         if (newdate) {
69                                 wday_t wday = day_of_week(next.year, next.month, next.day);
70                                 wattron(win, A_BOLD);
71                                 mvwprintw(win, row-line, 0,  "%04d-%02d-%02d",
72                                         next.year, next.month+1, next.day+1);
73                                 mvwprintw(win, row-line, 13, "%s, %s %d",
74                                         day_to_string(wday), month_to_string(next.month), next.day);
75                                 wattroff(win, A_BOLD);
76                                 row++;
77                         }
78                         event_line(win, event, row++-line, 4, COLS-4, SHOW_DETAILS);
79                         if (event->name && event->desc)
80                                 mvwprintw(win, row++-line, 14, "%s", event->desc);
81                         cur = next;
82                         count += 1;
83                 }
84                 event  = event->next;
85         }
86         rows = row;
87 }
88
89 /* Events run */
90 int events_run(int key, mmask_t btn, int row, int col)
91 {
92         int scroll = 0;
93         switch (key)
94         {
95                 case 'g': scroll = -line;    break;
96                 case 'G': scroll =  rows;    break;
97                 case 'j': scroll =  1;       break;
98                 case 'k': scroll = -1;       break;
99                 case 'd': scroll =  LINES/2; break;
100                 case 'u': scroll = -LINES/2; break;
101                 case 'D': scroll =  LINES;   break;
102                 case 'U': scroll = -LINES;   break;
103         }
104         line = CLAMP(line+scroll, 0, rows-1);
105         if (scroll) {
106                 werase(win);
107                 events_draw();
108                 wrefresh(win);
109         }
110         return scroll;
111 }