]> Pileus Git - lackey/blob - view/week.c
bde7608ff031bf41d90c8262e632938bae46d0d3
[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 /* Week init */
30 void week_init(WINDOW *_win)
31 {
32         win = _win;
33 }
34
35 /* Week draw */
36 void week_draw(void)
37 {
38         int x = 6;
39         int y = 3;
40         const float hstep = (float)(COLS-x)/7.0;
41
42         /* Clear */
43         werase(win);
44
45         /* Get start of week */
46         year_t  year  = YEAR;
47         month_t month = MONTH;
48         day_t   day   = DAY;
49         int shift = day_of_week(year, month, day);
50         add_days(&year, &month, &day, -shift);
51
52         /* Print Header */
53         mvwprintw(win, 1, 0, "%s", month_to_str(MONTH));
54         for (int d = 0; d < 7; d++) {
55                 const char *str = hstep >= 10 ? day_to_string(d) : day_to_str(d);
56                 if (d == shift) wattron(win, A_BOLD);
57                 mvwprintw(win, 0, x+ROUND(d*hstep), "%02d/%02d", month+1, day+1);
58                 mvwprintw(win, 1, x+ROUND(d*hstep), "%s", str);
59                 if (d == shift) wattroff(win, A_BOLD);
60                 add_days(&year, &month, &day, 1);
61         }
62
63         /* Print times */
64         int start = 8;
65         for (int h = 0; h < (LINES-6)/4+1; h++)
66                 mvwprintw(win, 3+h*4, 0,"%02d:%02d", (start+h)%12, 0);
67
68         /* Print lines */
69         mvwhline(win, y-1, 0, ACS_HLINE, COLS);
70         for (int d = 0; d < 7; d++)
71                 mvwvline(win, y, x+ROUND(d*hstep)-1, ACS_VLINE, LINES-y-2);
72
73         /* Draw today */
74         int l = x+ROUND((shift+0)*hstep)-1;
75         int r = x+ROUND((shift+1)*hstep)-1;
76         mvwhline    (win, y-1, l, ACS_BLOCK, r-l+1);
77         mvwvline_set(win, y,   l, WACS_T_VLINE, LINES-y-2);
78         mvwvline_set(win, y,   r, WACS_T_VLINE, LINES-y-2);
79 }
80
81 /* Week run */
82 int week_run(int key, mmask_t btn, int row, int col)
83 {
84         int days = 0;
85         switch (key)
86         {
87                 case 'h': days = -1; break;
88                 case 'l': days =  1; break;
89                 case 'i': days = -7; break;
90                 case 'o': days =  7; break;
91         }
92         if (days) {
93                 add_days(&YEAR, &MONTH, &DAY, days);
94                 week_draw();
95                 wrefresh(win);
96         }
97         return 0;
98 }