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
36 static void print_event(event_t *event, wday_t day, hour_t hour, min_t min, float hstep)
38 int x = ROUND(day*hstep);
39 int y = hour*4 + min/15 - line;
40 int w = ROUND((day+1)*hstep) - 1 - x;
41 int h = (get_mins(&event->start, &event->end)-1)/15+1;
43 int s = y < 0 ? -y-1 : 0;
45 int color = event->cat == NULL ? 0 :
46 !strcmp(event->cat, "class") ? COLOR_CLASS :
47 !strcmp(event->cat, "ec") ? COLOR_EC :
48 !strcmp(event->cat, "work") ? COLOR_WORK : COLOR_OTHER ;
50 if (color) wattron(body, COLOR_PAIR(color));
52 if (h >= 2) mvwhline_set(body, y, x+1, WACS_T_HLINE, w-2);
53 if (h <= 1) mvwadd_wch(body, y, x, WACS_BULLET);
54 if (h >= 2) mvwadd_wch(body, y, x, WACS_T_ULCORNER);
55 if (h >= 2) mvwadd_wch(body, y, x+w-1, WACS_T_URCORNER);
56 if (h >= 3) mvwvline_set(body, y+1+s, x, WACS_T_VLINE, h-2-s);
57 if (h >= 3) mvwvline_set(body, y+1+s, x+w-1, WACS_T_VLINE, h-2-s);
58 if (h >= 2) mvwadd_wch(body, y+h-1, x, WACS_T_LLCORNER);
59 if (h >= 2) mvwadd_wch(body, y+h-1, x+w-1, WACS_T_LRCORNER);
60 if (h >= 2) mvwhline_set(body, y+h-1, x+1, WACS_T_HLINE, w-2);
62 if (color) wattroff(body, COLOR_PAIR(color));
64 if (l<h && event->name) mvwprintw(body, y+l++, x+1, "%.*s", w-2, event->name);
65 if (l<h && event->loc) mvwprintw(body, y+l++, x+1, "@ %-*.*s", w-4, w-4, event->loc);
66 if (l<h && event->desc) mvwprintw(body, y+l++, x+1, "%-*.*s", w-2, w-2, event->desc);
68 debug("week: event = %s\n", event->name);
72 void week_init(WINDOW *_win)
74 win = _win; // lines cols y x
75 head = derwin(win, 2, COLS, 0, 0);
76 times = derwin(win, LINES-2-3, 5, 3, 0);
77 body = derwin(win, LINES-2-3, COLS-6, 3, 6);
82 void week_size(int rows, int cols)
84 wresize(head, 2, cols );
85 wresize(times, rows-3, 5);
86 wresize(body, rows-3, cols-6);
94 const float hstep = (float)(COLS-x)/7.0;
99 /* Get start of week */
101 month_t month = MONTH;
103 int shift = day_of_week(year, month, day);
104 add_days(&year, &month, &day, -shift);
107 mvwprintw(head, 0, 0, "%s", month_to_str(MONTH));
108 mvwprintw(head, 1, 0, "%04d", YEAR);
109 for (int d = 0; d < 7; d++) {
110 const char *str = hstep >= 10 ? day_to_string(d) : day_to_str(d);
111 if (d == shift) wattron(head, A_BOLD);
112 mvwprintw(head, 0, x+ROUND(d*hstep), "%s", str);
113 mvwprintw(head, 1, x+ROUND(d*hstep), "%02d/%02d", month+1, day+1);
114 if (d == shift) wattroff(head, A_BOLD);
115 add_days(&year, &month, &day, 1);
119 mvwprintw(times, 0, 0, "%02d:%02d", ((line/4)-1)%12+1, (line*15)%60);
120 for (int h = 0; h < 24; h++)
121 mvwprintw(times, h*4-line, 0, "%02d:%02d", (h-1)%12+1, 0);
124 event_t *event = EVENTS;
125 add_days(&year, &month, &day, -7);
126 for (int d = 0; d < 7; d++, add_days(&year,&month,&day,1))
127 for (int h = 0; h < 24; h++)
128 for (int m = 0; m < 60; m+=15)
129 while (event && before(&event->start,
130 year, month, day, h+(m+15)/60, (m+15)%60)) {
131 if (!before(&event->start, year, month, day, h, m))
132 print_event(event, d, h, m, hstep);
137 mvwhline(win, y-1, 0, ACS_HLINE, COLS);
138 for (int d = 0; d < 7; d++)
139 mvwvline(win, y, x+ROUND(d*hstep)-1, ACS_VLINE, LINES-y-2);
142 int l = x+ROUND((shift+0)*hstep)-1;
143 int r = x+ROUND((shift+1)*hstep)-1;
144 mvwhline (win, y-1, l, ACS_BLOCK, r-l+1);
145 mvwvline_set(win, y, l, WACS_T_VLINE, LINES-y-2);
146 mvwvline_set(win, y, r, WACS_T_VLINE, LINES-y-2);
150 int week_run(int key, mmask_t btn, int row, int col)
152 int days = 0, ref = 0;
155 case 'h': ref = 1; days = -1; break;
156 case 'l': ref = 1; days = 1; break;
157 case 'i': ref = 1; days = -7; break;
158 case 'o': ref = 1; days = 7; break;
159 case 'k': ref = 1; line--; break;
160 case 'j': ref = 1; line++; break;
162 line = CLAMP(line, 0, 24*4);
164 add_days(&YEAR, &MONTH, &DAY, days);