]> Pileus Git - lackey/blob - view/year.c
Fix month drawing
[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 "main.h"
22 #include "util.h"
23
24 /* Constants */
25 #define MW (2*7+6)
26
27 /* Static data */
28 static WINDOW *win;
29
30 /* Helper functions */
31 static void print_month(month_t month, int y, int x)
32 {
33         const char *name  = month_to_string(month);
34         const int   start = start_of_month(YEAR, month);
35         const char  days  = days_in_month(YEAR, month);
36         mvwprintw(win, y, x+MW/2-strlen(name)/2, "%s", name);
37         wmove(win, y+1, x);
38         for (int d = 0; d < 7; d++)
39                 wprintw(win, "%-3s", day_to_st(d));
40         for (int d = 0; d < days; d++) {
41                 int row = (start + d) / 7;
42                 int col = (start + d) % 7;
43                 if (month == MONTH && d == DAY) wattron(win, A_REVERSE);
44                 mvwprintw(win, y+2+row, x+col*3, "%2d", d+1);
45                 if (month == MONTH && d == DAY) wattroff(win, A_REVERSE);
46         }
47 }
48
49 /* Year init */
50 void year_init(WINDOW *_win)
51 {
52         win = _win;
53 }
54
55 /* Year draw */
56 void year_draw(void)
57 {
58         int w = MW*3 + 2*3;
59         int x = COLS/2 - w/2;
60         int y = 0;
61         int h[4] = {};
62
63         /* Clear */
64         werase(win);
65
66         /* Determine heights */
67         for (int m = 0; m < 12; m++) {
68                 int weeks = weeks_in_month(YEAR, m);
69                 h[m/3] = MAX(h[m/3], weeks+2);
70         }
71         int sum = h[0]+h[1]+h[2]+h[3];
72
73         /* Print Header */
74         mvwprintw(win, y++, COLS/2-2, "%d", YEAR);
75
76         /* Print Months */
77         for (int m = 0; m < 12; m++) {
78                 print_month(m, y, x);
79                 if (m % 3 == 2) {
80                         x  = COLS/2 - w/2;
81                         y += h[m/3]+1;
82                 } else {
83                         x += 3+MW;
84                 }
85         }
86
87         /* Print Lines */
88         y = 1;
89         mvwvline(win, y, x+(MW+3)*1-2, ACS_VLINE, sum+3);
90         mvwvline(win, y, x+(MW+3)*2-2, ACS_VLINE, sum+3);
91         for (int i = 0; i < 3; i++) {
92                 y += h[i];
93                 mvwhline(win, y, x,        ACS_HLINE, w);
94                 mvwaddch(win, y, x+(MW+3)*1-2, ACS_PLUS);
95                 mvwaddch(win, y, x+(MW+3)*2-2, ACS_PLUS);
96                 y++;
97         }
98 }
99
100 /* Year run */
101 int year_run(int key, mmask_t btn, int row, int col)
102 {
103         wday_t day = day_of_week(YEAR, MONTH, DAY);
104         int days = 0, months = 0, years = 0;
105         switch (key)
106         {
107                 case 'k': days   = -7; break;
108                 case 'j': days   =  7; break;
109                 case 'h': days   = -1; break;
110                 case 'l': days   =  1; break;
111                 case 'i': years  = -1; break;
112                 case 'o': years  =  1; break;
113         }
114         if (day == SUN && days == -1) days = -22;
115         if (day == SAT && days ==  1) days =  22;
116         if (days || months || years) {
117                 add_days(&YEAR, &MONTH, &DAY, days);
118                 add_months(&YEAR, &MONTH, months);
119                 YEAR += years;
120                 year_draw();
121                 wrefresh(win);
122         }
123         return 0;
124 }