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
34 void week_init(WINDOW *_win)
36 win = _win; // lines cols y x
37 times = derwin(win, LINES-2, 5, 0, 0);
38 body = derwin(win, LINES-2, COLS-5, 0, 5);
43 void week_size(int rows, int cols)
46 wmvresize(times, hdr, 0, rows-hdr, 5);
47 wmvresize(body, hdr, 5, rows-hdr, cols-5);
55 const float hstep = (float)(COLS-x)/7.0;
58 /* Get start of week */
60 month_t month = MONTH;
62 int shift = day_of_week(year, month, day);
63 add_days(&year, &month, &day, -shift);
66 int l = ROUND((shift+0)*hstep);
67 int r = ROUND((shift+1)*hstep);
70 int rev = COMPACT ? A_REVERSE | A_BOLD : 0;
72 mvwprintw(win, 0, 0, "%-*s", COLS, month_to_str(MONTH));
73 mvwprintw(win, 1, 0, "%-0*d", COLS, YEAR);
75 mvwhline(win, 0, x+l, ' ', r-l-1);
76 mvwhline(win, 1, x+l, ' ', r-l-1);
78 for (int d = 0; d < 7; d++) {
79 const char *str = hstep >= 10 ? day_to_string(d) : day_to_str(d);
80 if (d == shift) wattrset(win, A_BOLD);
81 mvwprintw(win, 0, x+ROUND(d*hstep), "%s", str);
82 mvwprintw(win, 1, x+ROUND(d*hstep), "%02d/%02d", month+1, day+1);
83 if (d == shift) wattrset(win, rev);
84 add_days(&year, &month, &day, 1);
88 /* Print all day events */
91 add_days(&year, &month, &day, -7);
92 for (int d = 0; d < 7; d++) {
94 while (event && before(&event->start, year, month, day, 24, 0)) {
95 if (!before(&event->end, year, month, day, 0, 1) &&
96 get_mins(&event->start, &event->end) > 23*60) {
97 int s = ROUND(d*hstep);
98 int w = ROUND((d+1)*hstep) - 1 - s;
99 event_line(win, event, y+n++, x+s, w, 0);
105 add_days(&year,&month,&day,1);
107 if (allday && !COMPACT)
111 wshrink(times, y+allday-!COMPACT);
112 wshrink(body, y+allday-!COMPACT);
115 mvwprintw(times, !COMPACT, 0, "%02d:%02d", ((line/4)-1)%12+1, (line*15)%60);
116 for (int h = 0; h < 24; h++)
117 mvwprintw(times, !COMPACT+h*4-line, 0, "%02d:%02d", (h-1)%12+1, 0);
121 add_days(&year, &month, &day, -7);
122 for (int d = 0; d < 7; d++, add_days(&year,&month,&day,1))
123 for (int h = 0; h < 24; h++)
124 for (int m = 0; m < 60; m+=15)
125 while (event && before(&event->start,
126 year, month, day, h+(m+15)/60, (m+15)%60)) {
127 if (!before(&event->start, year, month, day, h, m) &&
128 get_mins(&event->start, &event->end) <= 23*60) {
129 int y = h*4 + m/15 - line + !COMPACT;
130 int x = ROUND(d*hstep) + 1;
131 int h = (get_mins(&event->start, &event->end)-1)/15+1;
132 int w = ROUND((d+1)*hstep) - x;
133 event_box(body, event, y, x, h, w);
138 /* Print header lines */
140 mvwhline(win, y-1, 0, ACS_HLINE, COLS);
141 if (!COMPACT && allday)
142 mvwhline(win, y-1+allday, 0, ACS_HLINE, COLS);
144 /* Print day lines */
145 for (int d = 0; d < 7; d++)
146 mvwvline(body, !COMPACT, ROUND(d*hstep), ACS_VLINE, LINES-y-2+COMPACT-allday);
147 mvwvline_set(body, 0, l, WACS_T_VLINE, LINES-y-1+COMPACT);
148 mvwvline_set(body, 0, r, WACS_T_VLINE, LINES-y-1+COMPACT);
149 for (int h = (line+3)/4; h < 24; h++) {
150 mvwadd_wch(body, h*4-line+!COMPACT, l, WACS_T_LTEE);
151 mvwadd_wch(body, h*4-line+!COMPACT, r, WACS_T_RTEE);
154 mvwhline(body, 0, l, ACS_BLOCK, r-l+1);
158 int week_run(int key, mmask_t btn, int row, int col)
160 int days = 0, ref = 0;
163 case 'h': ref = 1; days = -1; break;
164 case 'l': ref = 1; days = 1; break;
165 case 'i': ref = 1; days = -7; break;
166 case 'o': ref = 1; days = 7; break;
167 case 'k': ref = 1; line--; break;
168 case 'j': ref = 1; line++; break;
170 line = CLAMP(line, 0, 24*4);
172 add_days(&YEAR, &MONTH, &DAY, days);