]> Pileus Git - lackey/blob - views/events.c
9d7434c3459dc98af79c5b9ea6f5c3125945c18e
[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 <string.h>
19 #include <ncurses.h>
20
21 #include "util.h"
22 #include "date.h"
23 #include "cal.h"
24 #include "view.h"
25
26 /* Static data */
27 static WINDOW *win;
28 static int     line;
29 static int     rows;
30
31 /* Events init */
32 void events_init(WINDOW *_win)
33 {
34         win = _win;
35 }
36
37 /* Events size */
38 void events_size(int rows, int cols)
39 {
40 }
41
42 /* Events draw */
43 void events_draw(void)
44 {
45         int days = 2*7;
46         int min  = 12;
47
48         date_t start = {SEL.year, SEL.month, SEL.day,    0, 0};
49         date_t cur   = {SEL.year, SEL.month, SEL.day-1,  0, 0};
50         date_t end   = {SEL.year, SEL.month, SEL.day,   24, 0};
51         add_days(&end.year, &end.month, &end.day, days);
52         cal_load(SEL.year, SEL.month, SEL.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
66                         /* Update events */
67                         if (EVENT == NULL || compare(&EVENT->start, &start) < 0)
68                                 EVENT = event;
69
70                         /* Spacing before the date */
71                         if ((newdate || newtime) && row != 0)
72                                 row++;
73                         if (newdate && row != 0)
74                                 row++;
75
76                         /* Print date */
77                         if (newdate) {
78                                 wday_t wday = day_of_week(next.year, next.month, next.day);
79                                 wattron(win, A_BOLD);
80                                 mvwprintw(win, row-line, 0,  "%04d-%02d-%02d",
81                                         next.year, next.month+1, next.day+1);
82                                 mvwprintw(win, row-line, 13, "%s, %s %d",
83                                         day_to_string(wday), month_to_string(next.month), next.day+1);
84                                 wattroff(win, A_BOLD);
85                                 row++;
86                         }
87
88                         /* Print event info */
89                         event_line(win, event, row++-line, 4, COLS-4,
90                                         SHOW_DETAILS | SHOW_ACTIVE);
91                         if (event->name && event->desc) {
92                                 int n = MIN(COLS-14, strcspn(event->desc, "\n"));
93                                 mvwprintw(win, row++-line, 14, "%.*s", n, event->desc);
94                         }
95
96                         cur = next;
97                         count += 1;
98                 }
99                 event  = event->next;
100         }
101         rows = row;
102 }
103
104 /* Events run */
105 int events_run(int key, mmask_t btn, int row, int col)
106 {
107         int scroll = 0, move = 0;
108         switch (key)
109         {
110                 case 'g':    scroll = -line;    break;
111                 case 'G':    scroll =  rows;    break;
112                 case '\005': scroll =  1;       break; // ctrl-e
113                 case '\031': scroll = -1;       break; // ctrl-y
114                 case 'd':    scroll =  LINES/2; break;
115                 case 'u':    scroll = -LINES/2; break;
116                 case 'D':    scroll =  LINES;   break;
117                 case 'U':    scroll = -LINES;   break;
118                 case 'j':    move   =  1;       break;
119                 case 'k':    move   = -1;       break;
120                 case '\012': // enter
121                         view_edit(EDIT_EVENT);
122                         return 1;
123         }
124         line   = CLAMP(line+scroll, 0, rows-1);
125         for (int i=0; i<move && EVENT && EVENT->next; i++)
126                 EVENT = EVENT->next;
127         for (int i=0; i>move && EVENT && EVENT->prev; i--)
128                 EVENT = EVENT->prev;
129         if (scroll || move) {
130                 werase(win);
131                 events_draw();
132                 wrefresh(win);
133         }
134         return scroll || move;
135 }