]> Pileus Git - lackey/blob - views/week.c
9f972521ad8c220f994d5bcc8ca5fe51b8ab15f7
[lackey] / views / week.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 #define _XOPEN_SOURCE_EXTENDED
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 int     line;
29 static WINDOW *win;
30 static WINDOW *times;
31 static WINDOW *body;
32
33 /* Week init */
34 void week_init(WINDOW *_win)
35 {
36         win   = _win; //    lines    cols    y  x
37         times = derwin(win, LINES-2,      5, 0, 0);
38         body  = derwin(win, LINES-2, COLS-5, 0, 5);
39         line  = MORNING*4;
40 }
41
42 /* Week size */
43 void week_size(int rows, int cols)
44 {
45         int hdr = 3-COMPACT;
46         wmvresize(times, hdr, 0, rows-hdr,      5);
47         wmvresize(body,  hdr, 5, rows-hdr, cols-5);
48 }
49
50 /* Week draw */
51 void week_draw(void)
52 {
53         int x = 6;
54         int y = 3 - COMPACT;
55         const float hstep = (float)(COLS-x)/7.0;
56         event_t *event;
57
58         /* Get start of week */
59         year_t  year  = SEL.year;
60         month_t month = SEL.month;
61         day_t   day   = SEL.day;
62         int shift = day_of_week(year, month, day);
63         add_days(&year, &month, &day, -shift);
64
65         /* Load cal data */
66         cal_load(year, month, day, 7);
67
68         /* For today */
69         int l = ROUND((shift+0)*hstep);
70         int r = ROUND((shift+1)*hstep);
71
72         /* Print Header */
73         int rev = COMPACT ? A_REVERSE | A_BOLD : 0;
74         wattron(win, rev);
75         mvwprintw(win, 0, 0, "%-*s",  COLS, month_to_str(SEL.month));
76         mvwprintw(win, 1, 0, "%-0*d", COLS, SEL.year);
77         wattroff(win, rev);
78         mvwhline(win, 0, x+l, ' ', r-l-1);
79         mvwhline(win, 1, x+l, ' ', r-l-1);
80         wattron(win, rev);
81         for (int d = 0; d < 7; d++) {
82                 const char *str = hstep >= 10 ? day_to_string(d) : day_to_str(d);
83                 if (d == shift) wattrset(win, A_BOLD);
84                 mvwprintw(win, 0, x+ROUND(d*hstep), "%s", str);
85                 mvwprintw(win, 1, x+ROUND(d*hstep), "%02d/%02d", month+1, day+1);
86                 if (d == shift) wattrset(win, rev);
87                 add_days(&year, &month, &day, 1);
88         }
89         wattroff(win, rev);
90
91         /* Print all day events */
92         int allday = 0;
93         event = EVENTS;
94         add_days(&year, &month, &day, -7);
95         for (int d = 0; d <  7; d++) {
96                 int n = 0;
97                 while (event && before(&event->start, year, month, day, 24, 0)) {
98                         if (!before(&event->end, year, month, day, 0, 1) &&
99                             get_mins(&event->start, &event->end) > 23*60) {
100                                 int s = ROUND(d*hstep);
101                                 int w = ROUND((d+1)*hstep) - 1 - s;
102                                 event_line(win, event, y+n++, x+s, w, 0);
103                         }
104                         event = event->next;
105                         if (n > allday)
106                                 allday = n;
107                 }
108                 add_days(&year,&month,&day,1);
109         }
110         if (allday && !COMPACT)
111                 allday++;
112
113         /* Resize body */
114         wshrink(times, y+allday-!COMPACT);
115         wshrink(body,  y+allday-!COMPACT);
116
117         /* Print times */
118         mvwprintw(times, !COMPACT, 0, "%02d:%02d", ((line/4)-1)%12+1, (line*15)%60);
119         for (int h = 0; h < 24; h++)
120                 mvwprintw(times, !COMPACT+h*4-line, 0, "%02d:%02d", (h-1)%12+1, 0);
121
122         /* Print events */
123         event = EVENTS;
124         add_days(&year, &month, &day, -7);
125         for (int d = 0; d <  7; d++, add_days(&year,&month,&day,1))
126         for (int h = 0; h < 24; h++)
127         for (int m = 0; m < 60; m+=15)
128         while (event && before(&event->start,
129                         year, month, day, h+(m+15)/60, (m+15)%60)) {
130                 if (!before(&event->start, year, month, day, h, m) &&
131                     get_mins(&event->start, &event->end) <= 23*60) {
132                         int y = h*4 + m/15 - line + !COMPACT;
133                         int x = ROUND(d*hstep) + 1;
134                         int h = (get_mins(&event->start, &event->end)-1)/15+1;
135                         int w = ROUND((d+1)*hstep) - x;
136                         event_box(body, event, y, x, h, w);
137                 }
138                 event = event->next;
139         }
140
141         /* Print header lines */
142         if (!COMPACT)
143                 mvwhline(win, y-1, 0, ACS_HLINE, COLS);
144         if (!COMPACT && allday)
145                 mvwhline(win, y-1+allday, 0, ACS_HLINE, COLS);
146
147         /* Print day lines */
148         for (int d = 0; d < 7; d++)
149                 mvwvline(body, !COMPACT, ROUND(d*hstep), ACS_VLINE, LINES-y-2+COMPACT-allday);
150         mvwvline_set(body, 0, l, WACS_T_VLINE, LINES-y-1+COMPACT);
151         mvwvline_set(body, 0, r, WACS_T_VLINE, LINES-y-1+COMPACT);
152         for (int h = (line+3)/4; h < 24; h++) {
153                 mvwadd_wch(body, h*4-line+!COMPACT, l, WACS_T_LTEE);
154                 mvwadd_wch(body, h*4-line+!COMPACT, r, WACS_T_RTEE);
155         }
156         if (!COMPACT)
157                 mvwhline(body, 0, l, ACS_BLOCK, r-l+1);
158 }
159
160 /* Week run */
161 int week_run(int key, mmask_t btn, int row, int col)
162 {
163         int days = 0, ref = 0;
164         switch (key)
165         {
166                 case 'h': ref = 1; days = -1; break;
167                 case 'l': ref = 1; days =  1; break;
168                 case 'i': ref = 1; days = -7; break;
169                 case 'o': ref = 1; days =  7; break;
170                 case 'k': ref = 1; line--;    break;
171                 case 'j': ref = 1; line++;    break;
172         }
173         line = CLAMP(line, 0, 24*4);
174         if (days)
175                 add_days(&SEL.year, &SEL.month, &SEL.day, days);
176         if (ref) {
177                 werase(win);
178                 week_draw();
179                 wrefresh(win);
180         }
181         return ref;
182 }