]> Pileus Git - lackey/blob - views/events.c
b23c0f55cfc7c19f8a24fd92fb42a01eeb657cb0
[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
65                         /* Update events */
66                         if (EVENT == NULL || compare(&EVENT->start, &start) < 0)
67                                 EVENT = event;
68
69                         /* Spacing before the date */
70                         if ((newdate || newtime) && row != 0)
71                                 row++;
72                         if (newdate && row != 0)
73                                 row++;
74
75                         /* Print date */
76                         if (newdate) {
77                                 wday_t wday = day_of_week(next.year, next.month, next.day);
78                                 wattron(win, A_BOLD);
79                                 mvwprintw(win, row-line, 0,  "%04d-%02d-%02d",
80                                         next.year, next.month+1, next.day+1);
81                                 mvwprintw(win, row-line, 13, "%s, %s %d",
82                                         day_to_string(wday), month_to_string(next.month), next.day+1);
83                                 wattroff(win, A_BOLD);
84                                 row++;
85                         }
86
87                         /* Print event info */
88                         event_line(win, event, row++-line, 4, COLS-4,
89                                         SHOW_DETAILS | SHOW_ACTIVE);
90                         if (event->name && event->desc)
91                                 mvwprintw(win, row++-line, 14, "%s", event->desc);
92
93                         cur = next;
94                         count += 1;
95                 }
96                 event  = event->next;
97         }
98         rows = row;
99 }
100
101 /* Events run */
102 int events_run(int key, mmask_t btn, int row, int col)
103 {
104         int scroll = 0, move = 0;
105         switch (key)
106         {
107                 case 'g':    scroll = -line;    break;
108                 case 'G':    scroll =  rows;    break;
109                 case '\005': scroll =  1;       break; // ctrl-e
110                 case '\031': scroll = -1;       break; // ctrl-y
111                 case 'd':    scroll =  LINES/2; break;
112                 case 'u':    scroll = -LINES/2; break;
113                 case 'D':    scroll =  LINES;   break;
114                 case 'U':    scroll = -LINES;   break;
115                 case 'j':    move   =  1;       break;
116                 case 'k':    move   = -1;       break;
117                 case '\012': // enter
118                         view_edit(EDIT_EVENT);
119                         return 1;
120         }
121         line   = CLAMP(line+scroll, 0, rows-1);
122         for (int i=0; i<move && EVENT && EVENT->next; i++)
123                 EVENT = EVENT->next;
124         for (int i=0; i>move && EVENT && EVENT->prev; i--)
125                 EVENT = EVENT->prev;
126         if (scroll || move) {
127                 werase(win);
128                 events_draw();
129                 wrefresh(win);
130         }
131         return scroll || move;
132 }