]> Pileus Git - lackey/blob - view/week.c
Start on events and calendars
[lackey] / view / week.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 _XOPEN_SOURCE_EXTENDED
19
20 #include <string.h>
21 #include <ncurses.h>
22
23 #include "main.h"
24 #include "util.h"
25
26 /* Static data */
27 static WINDOW *win;
28
29 /* Local functions */
30 static void print_event(event_t *event, wday_t day, hour_t hour, min_t min, float hstep)
31 {
32         int x = 6+ROUND(day*hstep);
33         int y = 3+hour*4;
34         int l = (event->end.min - event->start.min)/15;
35         mvwprintw(win, y, x, "%s", event->name);
36         debug("event: %s\n", event->name);
37 }
38
39 static int before(datetime_t *start, int year, int month, int day, int hour, int min)
40 {
41         int rval = start->year  < year  ? 1 : start->year  > year ? 0 :
42                    start->month < month ? 1 : start->month > month? 0 :
43                    start->day   < day   ? 1 : start->day   > day  ? 0 :
44                    start->hour  < hour  ? 1 : start->hour  > hour ? 0 :
45                    start->min   < min   ? 1 : start->min   > min  ? 0 : 0;
46         debug("%04d-%02d-%02d %02d:%02d < %04d-%02d-%02d %02d:%02d == %d\n",
47                         start->year, start->month, start->day, start->hour, start->min,
48                         year, month, day, hour, min, rval);
49         return rval;
50 }
51
52 /* Week init */
53 void week_init(WINDOW *_win)
54 {
55         win = _win;
56 }
57
58 /* Week draw */
59 void week_draw(void)
60 {
61         int x = 6;
62         int y = 3;
63         const float hstep = (float)(COLS-x)/7.0;
64
65         /* Clear */
66         werase(win);
67
68         /* Get start of week */
69         year_t  year  = YEAR;
70         month_t month = MONTH;
71         day_t   day   = DAY;
72         int shift = day_of_week(year, month, day);
73         add_days(&year, &month, &day, -shift);
74
75         /* Print Header */
76         mvwprintw(win, 1, 0, "%s", month_to_str(MONTH));
77         for (int d = 0; d < 7; d++) {
78                 const char *str = hstep >= 10 ? day_to_string(d) : day_to_str(d);
79                 if (d == shift) wattron(win, A_BOLD);
80                 mvwprintw(win, 0, x+ROUND(d*hstep), "%02d/%02d", month+1, day+1);
81                 mvwprintw(win, 1, x+ROUND(d*hstep), "%s", str);
82                 if (d == shift) wattroff(win, A_BOLD);
83                 add_days(&year, &month, &day, 1);
84         }
85
86         /* Print times */
87         hour_t start = 8;
88         for (int h = 0; h < (LINES-6)/4+1; h++)
89                 mvwprintw(win, 3+h*4, 0,"%02d:%02d", (start+h)%12, 0);
90
91         /* Print lines */
92         mvwhline(win, y-1, 0, ACS_HLINE, COLS);
93         for (int d = 0; d < 7; d++)
94                 mvwvline(win, y, x+ROUND(d*hstep)-1, ACS_VLINE, LINES-y-2);
95
96         /* Print events */
97         event_t *event = EVENTS;
98         add_days(&year, &month, &day, -7);
99         for (int d = 0; d <  7; d++) {
100                 for (int h = 0; h < (LINES-6)/4+1; h++) {
101                         for (int m = 0; m < 60; m+=15) {
102                                 while (event && before(&event->start, year, month, day, start+h+(m+15)/60, (m+15)%60)) {
103                                         if (!before(&event->start, year, month, day, start+h, m))
104                                                 print_event(event, d, h, m, hstep);
105                                         event = event->next;
106                                 }
107                         }
108                 }
109                 add_days(&year, &month, &day, 1);
110         }
111
112         /* Draw today */
113         int l = x+ROUND((shift+0)*hstep)-1;
114         int r = x+ROUND((shift+1)*hstep)-1;
115         mvwhline    (win, y-1, l, ACS_BLOCK, r-l+1);
116         mvwvline_set(win, y,   l, WACS_T_VLINE, LINES-y-2);
117         mvwvline_set(win, y,   r, WACS_T_VLINE, LINES-y-2);
118 }
119
120 /* Week run */
121 int week_run(int key, mmask_t btn, int row, int col)
122 {
123         int days = 0;
124         switch (key)
125         {
126                 case 'h': days = -1; break;
127                 case 'l': days =  1; break;
128                 case 'i': days = -7; break;
129                 case 'o': days =  7; break;
130         }
131         if (days) {
132                 add_days(&YEAR, &MONTH, &DAY, days);
133                 week_draw();
134                 wrefresh(win);
135         }
136         return 0;
137 }