2 * Copyright (C) 2012 Andy Spencer <andy753421@gmail.com>
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.
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.
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/>.
18 #define _XOPEN_SOURCE_EXTENDED
32 void month_init(WINDOW *_win)
38 void month_size(int rows, int cols)
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;
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);
59 mvwhline(win, 2, 0, ACS_HLINE, COLS);
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);
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, 0);
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);
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);
120 int month_run(int key, mmask_t btn, int row, int col)
122 int days = 0, months = 0;
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;
132 if (days || months) {
133 add_days(&YEAR, &MONTH, &DAY, days);
134 add_months(&YEAR, &MONTH, months);