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 static void print_event(event_t *event, int y, int x, int w)
34 int color = event->cat == NULL ? 0 :
35 !strcmp(event->cat, "class") ? COLOR_CLASS :
36 !strcmp(event->cat, "ec") ? COLOR_EC :
37 !strcmp(event->cat, "work") ? COLOR_WORK : COLOR_OTHER ;
39 if (color) wattron(win, COLOR_PAIR(color));
40 mvwaddch(win, y, x+0, ACS_BLOCK);
41 if (color) wattroff(win, COLOR_PAIR(color));
43 mvwprintw(win, y, x+1, "%-*.*s", w-1, w-1, event->name);
47 void month_init(WINDOW *_win)
53 void month_size(int rows, int cols)
60 const char *name = month_to_string(MONTH);
61 const int start = start_of_month(YEAR, MONTH);
62 const int days = days_in_month(YEAR, MONTH);
63 const int weeks = weeks_in_month(YEAR, MONTH);
64 const float midpt = (float)COLS/2.0 - (strlen(name) + 1 + 4)/2.0;
65 const float hstep = (float)(COLS-1)/7.0;
66 const float vstep = (float)(LINES-6)/weeks;
72 mvwprintw(win, 0, midpt, "%s %d", name, YEAR);
73 for (int d = 0; d < 7; d++) {
74 const char *str = hstep >= 10 ? day_to_string(d+SUN) : day_to_str(d+SUN);
75 mvwprintw(win, 1, ROUND(1+d*hstep), "%s", str);
77 mvwhline(win, 2, 0, ACS_HLINE, COLS);
80 for (int d = 0; d < days; d++) {
81 int row = (start + d) / 7;
82 int col = (start + d) % 7;
83 if (d == DAY) wattron(win, A_BOLD);
84 mvwprintw(win, ROUND(4+row*vstep), ROUND(1+col*hstep), "%d", d+1);
85 if (d == DAY) wattroff(win, A_BOLD);
89 event_t *event = EVENTS;
90 for (int d = 0; d < days; d++) {
91 int y = ROUND(4+(((start + d) / 7) )*vstep);
92 int e = ROUND(4+(((start + d) / 7)+1)*vstep)-2;
93 int x = ROUND(1+(((start + d) % 7) )*hstep)+3;
94 int w = ROUND(1+(((start + d) % 7)+1)*hstep)-x-1;
95 while (event && before(&event->start, YEAR, MONTH, d, 24, 0)) {
96 if (!before(&event->start, YEAR, MONTH, d, 0, 0)){
97 if (y == e) mvwhline(win, y, x-3, ACS_DARROW, 2);
98 if (y <= e) print_event(event, y, x, w);
106 for (int w = 1; w < weeks; w++)
107 mvwhline(win, ROUND(3+w*vstep), 1, ACS_HLINE, COLS-2);
108 for (int d = 1; d < 7; d++) {
109 int top = d >= start ? 0 : 1;
110 int bot = d <= (start+days-1)%7+1 ? weeks : weeks-1;
111 mvwvline(win, ROUND(4+top*vstep), ROUND(d*hstep),
112 ACS_VLINE, (bot-top)*vstep);
113 for (int w = 1; w < weeks; w++) {
114 int chr = w == top ? ACS_TTEE :
115 w == bot ? ACS_BTEE : ACS_PLUS;
116 mvwaddch(win, ROUND(3+w*vstep), ROUND(d*hstep), chr);
121 int col = day_of_week(YEAR, MONTH, DAY);
122 int row = (start+DAY) / 7;
123 int l = ROUND((col+0)*hstep);
124 int r = ROUND((col+1)*hstep);
125 int t = ROUND((row+0)*vstep+3);
126 int b = ROUND((row+1)*vstep+3);
127 mvwvline_set(win, t, l, WACS_T_VLINE, b-t);
128 mvwvline_set(win, t, r, WACS_T_VLINE, b-t);
129 mvwhline_set(win, t, l, WACS_T_HLINE, r-l);
130 mvwhline_set(win, b, l, WACS_T_HLINE, r-l);
131 mvwadd_wch(win, t, l, WACS_T_ULCORNER);
132 mvwadd_wch(win, t, r, WACS_T_URCORNER);
133 mvwadd_wch(win, b, l, WACS_T_LLCORNER);
134 mvwadd_wch(win, b, r, WACS_T_LRCORNER);
138 int month_run(int key, mmask_t btn, int row, int col)
140 int days = 0, months = 0;
143 case 'h': days = -1; break;
144 case 'j': days = 7; break;
145 case 'k': days = -7; break;
146 case 'l': days = 1; break;
147 case 'i': months = -1; break;
148 case 'o': months = 1; break;
150 if (days || months) {
151 add_days(&YEAR, &MONTH, &DAY, days);
152 add_months(&YEAR, &MONTH, months);