]> Pileus Git - lackey/blob - view/year.c
Move drawing code to screen.c, work on day view
[lackey] / view / year.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 #include <string.h>
19 #include <ncurses.h>
20
21 #include "util.h"
22 #include "date.h"
23 #include "event.h"
24 #include "screen.h"
25
26 /* Constants */
27 #define MW (2*7+6)
28
29 /* Static data */
30 static WINDOW *win;
31
32 /* Helper functions */
33 static void print_month(month_t month, int y, int x)
34 {
35         event_t    *event = EVENTS;
36         const char *name  = month_to_string(month);
37         const int   start = start_of_month(YEAR, month);
38         const char  days  = days_in_month(YEAR, month);
39         mvwprintw(win, y, x+MW/2-strlen(name)/2, "%s", name);
40         wmove(win, y+1, x);
41         for (int d = 0; d < 7; d++)
42                 wprintw(win, "%-3s", day_to_st(d));
43         for (int d = 0; d < days; d++) {
44                 int row = (start + d) / 7;
45                 int col = (start + d) % 7;
46
47                 int busy = 0;
48                 while (event && before(&event->start, YEAR, month, d, 24, 0)) {
49                         if (!before(&event->start, YEAR, month, d, 0, 0))
50                                 busy = 1;
51                         event = event->next;
52                 }
53
54                 int today = month == MONTH && d == DAY;
55
56                 int attr  = (busy  ? A_BOLD|A_UNDERLINE : A_DIM)
57                           | (today ? A_REVERSE          : 0    );
58
59                 wattron(win, attr);
60                 mvwprintw(win, y+2+row, x+col*3, "%2d", d+1);
61                 wattroff(win, attr);
62         }
63 }
64
65 /* Year init */
66 void year_init(WINDOW *_win)
67 {
68         win = _win;
69 }
70
71 /* Year size */
72 void year_size(int rows, int cols)
73 {
74 }
75
76 /* Year draw */
77 void year_draw(void)
78 {
79         int w = MW*3 + 2*3;
80         int x = COLS/2 - w/2;
81         int y = 0;
82         int h[4] = {};
83
84         /* Determine heights */
85         for (int m = 0; m < 12; m++) {
86                 int weeks = weeks_in_month(YEAR, m);
87                 h[m/3] = MAX(h[m/3], weeks+2);
88         }
89         int sum = h[0]+h[1]+h[2]+h[3];
90
91         /* Print Header */
92         mvwprintw(win, y++, COLS/2-2, "%d", YEAR);
93
94         /* Print Months */
95         for (int m = 0; m < 12; m++) {
96                 print_month(m, y, x);
97                 if (m % 3 == 2) {
98                         x  = COLS/2 - w/2;
99                         y += h[m/3]+1;
100                 } else {
101                         x += 3+MW;
102                 }
103         }
104
105         /* Print Lines */
106         y = 1;
107         mvwvline(win, y, x+(MW+3)*1-2, ACS_VLINE, sum+3);
108         mvwvline(win, y, x+(MW+3)*2-2, ACS_VLINE, sum+3);
109         for (int i = 0; i < 3; i++) {
110                 y += h[i];
111                 mvwhline(win, y, x,        ACS_HLINE, w);
112                 mvwaddch(win, y, x+(MW+3)*1-2, ACS_PLUS);
113                 mvwaddch(win, y, x+(MW+3)*2-2, ACS_PLUS);
114                 y++;
115         }
116 }
117
118 /* Year run */
119 int year_run(int key, mmask_t btn, int row, int col)
120 {
121         day_t d = DAY;
122         month_t m = MONTH;
123         year_t y = YEAR;
124         wday_t day = day_of_week(YEAR, MONTH, DAY);
125         int week = (start_of_month(y, m) + d) / 7;
126         int dir = 0;
127
128         /* Step years */
129         if (key == 'i')
130                 YEAR--;
131         if (key == 'o')
132                 YEAR++;
133
134         /* Get direction */
135         if (key == 'h' || key == 'k')
136                 dir = -1;
137         if (key == 'j' || key == 'l')
138                 dir =  1;
139
140         /* Step up/down */
141         if (key == 'j' || key == 'k') {
142                 for (int i = 0; i < 90/7; i++) {
143                         add_days(&y, &m, &d, dir*7);
144                         if (day_of_week(y, m, d) == day &&
145                             y == YEAR && m%3 == MONTH%3) {
146                                 MONTH = m;
147                                 DAY = d;
148                                 break;
149                         }
150                 }
151         }
152
153         /* Step left/right */
154         if (key == 'h' || key == 'l') {
155                 for (int i = 0; i < 90; i++) {
156                         add_days(&y, &m, &d, dir);
157                         if ((start_of_month(y, m) + d) / 7 == week &&
158                             y == YEAR && m/3 == MONTH/3) {
159                                 MONTH = m;
160                                 DAY = d;
161                                 break;
162                         }
163                 }
164         }
165
166         /* Refresh */
167         werase(win);
168         year_draw();
169         wrefresh(win);
170         return 0;
171 }