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