]> Pileus Git - lackey/blob - view/month.c
3849492e9205388159d3eeeeeb686eb1a281983a
[lackey] / view / month.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 "util.h"
24 #include "date.h"
25
26 /* Static data */
27 static WINDOW *win;
28
29 /* Month init */
30 void month_init(WINDOW *_win)
31 {
32         win = _win;
33 }
34
35 /* Month size */
36 void month_size(int rows, int cols)
37 {
38 }
39
40 /* Month draw */
41 void month_draw(void)
42 {
43         const char *name  = month_to_string(MONTH);
44         const int   start = start_of_month(YEAR, MONTH);
45         const int   days  = days_in_month(YEAR, MONTH);
46         const int   weeks = weeks_in_month(YEAR, MONTH);
47         const float midpt = (float)COLS/2.0 - (strlen(name) + 1 + 4)/2.0;
48         const float hstep = (float)(COLS-1)/7.0;
49         const float vstep = (float)(LINES-6)/weeks;
50
51         /* Clear */
52         werase(win);
53
54         /* Print Header */
55         mvwprintw(win, 0, midpt, "%s %d", name, YEAR);
56         for (int d = 0; d < 7; d++) {
57                 const char *str = hstep >= 10 ? day_to_string(d+SUN) : day_to_str(d+SUN);
58                 mvwprintw(win, 1, ROUND(1+d*hstep), "%s", str);
59         }
60         mvwhline(win, 2, 0, ACS_HLINE, COLS);
61
62         /* Print days */
63         for (int d = 0; d < days; d++) {
64                 int row = (start + d) / 7;
65                 int col = (start + d) % 7;
66                 if (d == DAY) wattron(win, A_BOLD);
67                 mvwprintw(win, ROUND(4+row*vstep), ROUND(1+col*hstep), "%d", d+1);
68                 if (d == DAY) wattroff(win, A_BOLD);
69         }
70
71         /* Print lines */
72         for (int w = 1; w < weeks; w++)
73                 mvwhline(win, ROUND(3+w*vstep), 1, ACS_HLINE, COLS-2);
74         for (int d = 1; d < 7; d++) {
75                 int top = d >=  start             ? 0     : 1;
76                 int bot = d <= (start+days-1)%7+1 ? weeks : weeks-1;
77                 mvwvline(win, ROUND(4+top*vstep), ROUND(d*hstep),
78                                 ACS_VLINE, (bot-top)*vstep-1);
79                 for (int w = 1; w < weeks; w++) {
80                         int chr = w == top ? ACS_TTEE :
81                                   w == bot ? ACS_BTEE : ACS_PLUS;
82                         mvwaddch(win, ROUND(3+w*vstep), ROUND(d*hstep), chr);
83                 }
84         }
85
86         /* Draw today */
87         int col = day_of_week(YEAR, MONTH, DAY);
88         int row = (start+DAY) / 7;
89         int l = ROUND((col+0)*hstep);
90         int r = ROUND((col+1)*hstep);
91         int t = ROUND((row+0)*vstep+3);
92         int b = ROUND((row+1)*vstep+3);
93         mvwvline_set(win, t, l, WACS_T_VLINE, b-t);
94         mvwvline_set(win, t, r, WACS_T_VLINE, b-t);
95         mvwhline_set(win, t, l, WACS_T_HLINE, r-l);
96         mvwhline_set(win, b, l, WACS_T_HLINE, r-l);
97         mvwadd_wch(win, t, l, WACS_T_ULCORNER);
98         mvwadd_wch(win, t, r, WACS_T_URCORNER);
99         mvwadd_wch(win, b, l, WACS_T_LLCORNER);
100         mvwadd_wch(win, b, r, WACS_T_LRCORNER);
101 }
102
103 /* Month run */
104 int month_run(int key, mmask_t btn, int row, int col)
105 {
106         int days = 0, months = 0;
107         switch (key)
108         {
109                 case 'h': days   = -1; break;
110                 case 'j': days   =  7; break;
111                 case 'k': days   = -7; break;
112                 case 'l': days   =  1; break;
113                 case 'i': months = -1; break;
114                 case 'o': months =  1; break;
115         }
116         if (days || months) {
117                 add_days(&YEAR, &MONTH, &DAY, days);
118                 add_months(&YEAR, &MONTH, months);
119                 month_draw();
120                 wrefresh(win);
121         }
122         return 0;
123 }