2 * Copyright (C) 2012-2013 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
33 void (*init)(WINDOW*);
34 void (*size)(int,int);
36 int (*run)(int,mmask_t,int,int);
42 #define VIEW(name, title, ...) \
43 void name##_init(WINDOW *win); \
44 void name##_size(int,int); \
45 void name##_draw(void); \
46 int name##_run(int,mmask_t,int,int); \
47 view_t name##_view = { \
58 VIEW(day, "Day", KEY_F(1), '1');
59 VIEW(week, "Week", KEY_F(2), '2');
60 VIEW(month, "Month", KEY_F(3), '3');
61 VIEW(year, "Year", KEY_F(4), '4');
62 VIEW(events, "Events", KEY_F(5), '5');
63 VIEW(todo, "Todo", KEY_F(6), '6');
64 VIEW(settings, "Settings", KEY_F(7), '7');
65 VIEW(help, "Help", KEY_F(8), '8');
69 view_t spacer = { "|", "|" };
72 &day_view, &week_view, &month_view, &year_view,
73 &events_view, &todo_view,
74 &settings_view, &help_view,
79 &day_view, &week_view, &month_view, &year_view,
80 &spacer, &events_view, &todo_view,
81 &spacer, &settings_view, &help_view
88 view_t *view = &day_view;
89 view_t *active = &day_view;
93 static void draw_header(void)
96 attron(COLOR_PAIR(COLOR_TITLE));
100 for (int i = 0; i < N_ELEMENTS(menu); i++) {
101 if (menu[i] == active)
103 printw("%s ", menu[i]->title);
104 if (menu[i] == active)
108 /* Draw popup window */
112 printw("[%s]", popup->title);
118 printw("%04d-%02d-%02d %02d:%02d:%02d",
119 NOW.year, NOW.month, NOW.day,
120 NOW.hour, NOW.min, NOW.sec);
122 attroff(COLOR_PAIR(COLOR_TITLE));
124 mvhline(1, 0, ACS_HLINE, COLS);
128 static int get_color(const char *cat)
130 return cat == NULL ? 0 :
131 match(cat, "class") ? COLOR_CLASS :
132 match(cat, "ec") ? COLOR_EC :
133 match(cat, "work") ? COLOR_WORK : COLOR_OTHER ;
136 static int set_view(view_t *_active, view_t *_popup)
138 view = _popup ?: _active;
139 if (active != _active) {
141 set_string("view", 0, "active", active->name);
144 if (popup != _popup) {
151 /* Curses functions */
152 void wmvresize(WINDOW *win, int top, int left, int rows, int cols)
154 int y = getpary(win);
156 mvderwin(win, top, left);
157 wresize(win, rows, cols);
159 mvderwin(win, top, left);
162 void wshrink(WINDOW *win, int top)
164 int x = getparx(win);
165 int y = getpary(win);
166 int r = getmaxy(win);
167 int c = getmaxx(win);
168 int rows = r + (y - top);
169 if (top < y) mvderwin(win, top, x);
170 if (rows != r) wresize(win, rows, c);
171 if (top > y) mvderwin(win, top, x);
174 /* Helper functions */
175 void event_box(WINDOW *win, event_t *event, int y, int x, int h, int w)
178 int s = y < 0 ? -y-1 : 0;
180 int color = get_color(event->cat);
182 if (color) wattron(win, COLOR_PAIR(color));
184 if (h >= 2) mvwhline_set(win, y, x+1, WACS_T_HLINE, w-2);
185 if (h <= 1) mvwadd_wch(win, y, x, WACS_BULLET);
186 if (h >= 2) mvwadd_wch(win, y, x, WACS_T_ULCORNER);
187 if (h >= 2) mvwadd_wch(win, y, x+w-1, WACS_T_URCORNER);
188 if (h >= 3) mvwvline_set(win, y+1+s, x, WACS_T_VLINE, h-2-s);
189 if (h >= 3) mvwvline_set(win, y+1+s, x+w-1, WACS_T_VLINE, h-2-s);
190 if (h >= 2) mvwadd_wch(win, y+h-1, x, WACS_T_LLCORNER);
191 if (h >= 2) mvwadd_wch(win, y+h-1, x+w-1, WACS_T_LRCORNER);
192 if (h >= 2) mvwhline_set(win, y+h-1, x+1, WACS_T_HLINE, w-2);
194 if (color) wattroff(win, COLOR_PAIR(color));
196 if (l<h && event->name) mvwprintw(win, y+l++, x+1, "%.*s", w-2, event->name);
197 if (l<h && event->loc) mvwprintw(win, y+l++, x+1, "@ %.*s", w-4, event->loc);
198 if (l<h && event->desc) mvwprintw(win, y+l++, x+1, "%.*s", w-2, event->desc);
201 void event_line(WINDOW *win, event_t *event, int y, int x, int w, int flags)
203 int color = get_color(event->cat);
205 if (color) wattron(win, COLOR_PAIR(color));
206 mvwaddch(win, y, x++, ACS_BLOCK);
207 if (color) wattroff(win, COLOR_PAIR(color));
209 if (flags & SHOW_ACTIVE)
210 wattron(win, A_REVERSE | A_BOLD);
211 if (flags & SHOW_DETAILS) {
212 if (all_day(&event->start, &event->end))
213 mvwprintw(win, y, x+1, "[all day] - ");
215 mvwprintw(win, y, x+1, "%2d:%02d-%2d:%02d - ",
216 event->start.hour, event->start.min,
217 event->end.hour, event->end.min);
222 const char *label = event->name ?: event->desc;
223 mvwprintw(win, y, x, "%-*.*s", w-1, w-1, label);
224 x += MIN(strlen(label), w-1);
226 if (flags & SHOW_DETAILS && event->loc) {
227 mvwprintw(win, y, x, " @ %s", event->loc);
229 if (flags & SHOW_ACTIVE)
230 wattroff(win, A_REVERSE | A_BOLD);
233 void todo_line(WINDOW *win, todo_t *todo, int y, int x, int w, int flags)
237 sprintf(perc, "%2d%%", todo->status);
239 int cat = get_color(todo->cat);
240 int status = todo->status == NEW ? COLOR_NEW :
241 todo->status == DONE ? COLOR_DONE : COLOR_WIP;
243 sprintf(desc, "%s", todo->name ?: todo->desc ?: "");
244 strsub(desc, '\n', ';');
247 if (cat) wattron(win, COLOR_PAIR(cat));
248 mvwaddch(win, y, x, ACS_BLOCK);
249 if (cat) wattroff(win, COLOR_PAIR(cat));
253 if (flags & SHOW_ACTIVE)
254 wattron(win, A_REVERSE | A_BOLD);
255 mvwhline(win, y, x, ' ', COLS-x);
258 if (no_date(&todo->due))
259 mvwprintw(win, y, x, "[no due date]");
261 mvwprintw(win, y, x, "%04d-%02d-%02d %2d:%02d",
262 todo->due.year, todo->due.month+1, todo->due.day+1,
263 todo->due.hour, todo->due.min);
267 if (status) wattron(win, COLOR_PAIR(status));
268 mvwprintw(win, y, x, "%s",
269 todo->status == NEW ? "new" :
270 todo->status == DONE ? "done" : perc);
271 if (status) wattroff(win, COLOR_PAIR(status));
274 /* Print description */
275 mvwprintw(win, y, x, "%s", desc);
278 if (flags & SHOW_ACTIVE)
279 wattroff(win, A_REVERSE | A_BOLD);
285 int hdr = COMPACT ? 1 : 2;
286 for (int i = 0; i < N_ELEMENTS(views); i++) {
287 views[i]->win = newwin(LINES-hdr, COLS, hdr, 0);
288 views[i]->init(views[i]->win);
289 views[i]->size(LINES-hdr, COLS);
294 void view_config(const char *group, const char *name, const char *key, const char *value)
296 if (match(group, "view")) {
297 if (match(key, "compact")) {
298 COMPACT = get_bool(value);
299 } else if (match(key, "active")) {
300 for (int i = 0; i < N_ELEMENTS(views); i++) {
301 if (match(value, views[i]->name)) {
303 view = active = views[i];
312 void view_resize(void)
314 int hdr = COMPACT ? 1 : 2;
315 for (int i = 0; i < N_ELEMENTS(views); i++) {
316 wresize(views[i]->win, LINES-hdr, COLS);
317 mvwin(views[i]->win, hdr, 0);
318 views[i]->size(LINES-hdr, COLS);
332 int view_run(int key, mmask_t btn, int row, int col)
334 /* Refresh timestamp */
337 /* Check for mouse events on the menu */
338 if (key == KEY_MOUSE && row == 0) {
340 for (int i = 0; i < N_ELEMENTS(menu); i++) {
341 int end = start + strlen(menu[i]->name) - 1;
342 if (start <= col && col <= end && menu[i]->draw)
343 return set_view(menu[i], NULL);
348 /* Look though menu for hotkeys */
349 for (int i = 0; i < N_ELEMENTS(menu); i++) {
350 for (int j = 0; j < N_ELEMENTS(menu[i]->keys); j++)
351 if (menu[i]->keys[j] == key)
352 return set_view(menu[i], NULL);
355 /* Shift windows with left/right keys */
356 int shift = key == KEY_RIGHT ? +1 :
357 key == KEY_LEFT ? -1 : 0;
360 for (int i = 0; i < N_ELEMENTS(menu); i++)
361 if (menu[i] == active)
365 num += N_ELEMENTS(menu);
366 num %= N_ELEMENTS(menu);
367 } while (menu[num] == &spacer);
368 return set_view(menu[num], NULL);
371 /* Handle other keys */
375 set_bool("view", 0, "compact", COMPACT);
379 case '\033': // escape
380 return set_view(active, NULL);
382 return set_view(active, &help_view);
384 return set_view(active, &edit_view);
387 /* Pass key to active view */
388 return view->run(key, btn, row, col);