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/>.
19 #define _XOPEN_SOURCE_EXTENDED
36 void (*init)(WINDOW*);
37 void (*size)(int,int);
39 int (*run)(int,mmask_t,int,int);
45 #define VIEW(name, title, ...) \
46 void name##_init(WINDOW *win); \
47 void name##_size(int,int); \
48 void name##_draw(void); \
49 int name##_run(int,mmask_t,int,int); \
50 view_t name##_view = { \
61 VIEW(day, "Day", KEY_F(1), '1');
62 VIEW(week, "Week", KEY_F(2), '2');
63 VIEW(month, "Month", KEY_F(3), '3');
64 VIEW(year, "Year", KEY_F(4), '4');
65 VIEW(events, "Events", KEY_F(5), '5');
66 VIEW(todo, "Todo", KEY_F(6), '6');
67 VIEW(settings, "Settings", KEY_F(7), '7');
68 VIEW(help, "Help", KEY_F(8), '8');
72 view_t spacer = { "|", "|" };
75 &day_view, &week_view, &month_view, &year_view,
76 &events_view, &todo_view,
77 &settings_view, &help_view,
82 &day_view, &week_view, &month_view, &year_view,
83 &spacer, &events_view, &todo_view,
84 &spacer, &settings_view, &help_view
92 edit_t EDIT = EDIT_NONE;
95 view_t *view = &day_view;
96 view_t *active = &day_view;
100 /* Local functions */
101 static void draw_header(void)
104 attron(COLOR_PAIR(COLOR_TITLE));
108 for (int i = 0; i < N_ELEMENTS(menu); i++) {
109 if (menu[i] == active)
111 printw("%s ", menu[i]->title);
112 if (menu[i] == active)
116 /* Draw popup window */
120 printw("[%s]", popup->title);
126 printw("%04d-%02d-%02d %02d:%02d:%02d",
127 NOW.year, NOW.month+1, NOW.day+1,
128 NOW.hour, NOW.min, NOW.sec);
130 attroff(COLOR_PAIR(COLOR_TITLE));
132 mvhline(1, 0, ACS_HLINE, COLS);
136 static int get_color(const char *cat)
138 return cat == NULL ? 0 :
139 match(cat, "class") ? COLOR_CLASS :
140 match(cat, "ec") ? COLOR_EC :
141 match(cat, "work") ? COLOR_WORK : COLOR_OTHER ;
144 static void update_sizes(void)
146 int hdr = COMPACT ? 1 : 2;
147 for (int i = 0; i < N_ELEMENTS(views); i++) {
148 wresize(views[i]->win, LINES-hdr, COLS);
149 mvwin(views[i]->win, hdr, 0);
150 views[i]->size(LINES-hdr, COLS);
154 static void draw_view(void)
162 static int set_view(view_t *_active, view_t *_popup)
164 view = _popup ?: _active;
165 if (active != _active) {
167 set_string("view", 0, "active", active->name);
170 if (popup != _popup) {
177 static int process(int key, mmask_t btn, int row, int col)
179 /* Refresh timestamp */
182 /* Check for mouse events on the menu */
183 if (key == KEY_MOUSE && row == 0) {
185 for (int i = 0; i < N_ELEMENTS(menu); i++) {
186 int end = start + strlen(menu[i]->name) - 1;
187 if (start <= col && col <= end && menu[i]->draw)
188 return set_view(menu[i], NULL);
193 /* Look though menu for hotkeys */
194 for (int i = 0; i < N_ELEMENTS(menu); i++) {
195 for (int j = 0; j < N_ELEMENTS(menu[i]->keys); j++)
196 if (menu[i]->keys[j] == key)
197 return set_view(menu[i], NULL);
200 /* Shift windows with left/right keys */
201 int shift = key == KEY_RIGHT ? +1 :
202 key == KEY_LEFT ? -1 : 0;
205 for (int i = 0; i < N_ELEMENTS(menu); i++)
206 if (menu[i] == active)
210 num += N_ELEMENTS(menu);
211 num %= N_ELEMENTS(menu);
212 } while (menu[num] == &spacer);
213 return set_view(menu[num], NULL);
216 /* Handle other keys */
224 case '\14': // Ctrl-L
230 case '\033': // escape
231 return set_view(active, NULL);
233 return set_view(active, &help_view);
236 /* Pass key to active view */
237 return view->run(key, btn, row, col);
240 /* Curses functions */
241 void wmvresize(WINDOW *win, int top, int left, int rows, int cols)
243 int y = getpary(win);
245 mvderwin(win, top, left);
246 wresize(win, rows, cols);
248 mvderwin(win, top, left);
251 void wshrink(WINDOW *win, int top)
253 int x = getparx(win);
254 int y = getpary(win);
255 int r = getmaxy(win);
256 int c = getmaxx(win);
257 int rows = r + (y - top);
258 if (top < y) mvderwin(win, top, x);
259 if (rows != r) wresize(win, rows, c);
260 if (top > y) mvderwin(win, top, x);
263 /* Helper functions */
264 void event_box(WINDOW *win, event_t *event, int y, int x, int h, int w)
267 int s = y < 0 ? -y-1 : 0;
269 int color = get_color(event->cat);
271 if (color) wattron(win, COLOR_PAIR(color));
273 if (h >= 2) mvwhline_set(win, y, x+1, WACS_T_HLINE, w-2);
274 if (h <= 1) mvwadd_wch(win, y, x, WACS_BULLET);
275 if (h >= 2) mvwadd_wch(win, y, x, WACS_T_ULCORNER);
276 if (h >= 2) mvwadd_wch(win, y, x+w-1, WACS_T_URCORNER);
277 if (h >= 3) mvwvline_set(win, y+1+s, x, WACS_T_VLINE, h-2-s);
278 if (h >= 3) mvwvline_set(win, y+1+s, x+w-1, WACS_T_VLINE, h-2-s);
279 if (h >= 2) mvwadd_wch(win, y+h-1, x, WACS_T_LLCORNER);
280 if (h >= 2) mvwadd_wch(win, y+h-1, x+w-1, WACS_T_LRCORNER);
281 if (h >= 2) mvwhline_set(win, y+h-1, x+1, WACS_T_HLINE, w-2);
283 for (i = 1; i < h-1; i++)
284 mvwhline(win, y+i, x+1, ' ', w-2);
286 if (color) wattroff(win, COLOR_PAIR(color));
288 if (event == EVENT) wattron(win, WA_BOLD | WA_REVERSE);
289 if (event == EVENT) mvwhline(win, y+s, x, ' ', w);
290 if (l<h && event->name) mvwprintw(win, y+l++, x+1, "%.*s", w-2, event->name);
291 if (event == EVENT) wattroff(win, WA_REVERSE);
292 if (l<h && event->loc) mvwprintw(win, y+l++, x+1, "@ %.*s", w-4, event->loc);
293 if (l<h && event->desc) mvwprintw(win, y+l++, x+1, "%.*s", w-2, event->desc);
294 if (event == EVENT) wattroff(win, WA_BOLD);
297 void event_line(WINDOW *win, event_t *event, int y, int x, int w, int flags)
299 int color = get_color(event->cat);
301 if (color) wattron(win, COLOR_PAIR(color));
302 mvwaddch(win, y, x++, ACS_BLOCK);
303 if (color) wattroff(win, COLOR_PAIR(color));
305 if (flags & SHOW_ACTIVE && event == EVENT)
306 wattron(win, A_REVERSE | A_BOLD);
307 if (flags & SHOW_DETAILS) {
308 if (all_day(&event->start, &event->end))
309 mvwprintw(win, y, x+1, "[all day] - ");
311 mvwprintw(win, y, x+1, "%2d:%02d-%2d:%02d - ",
312 event->start.hour, event->start.min,
313 event->end.hour, event->end.min);
318 const char *label = event->name ?: event->desc;
319 mvwprintw(win, y, x, "%-*.*s", w-1, w-1, label);
320 x += MIN(strlen(label), w-1);
322 if (flags & SHOW_DETAILS && event->loc) {
323 mvwprintw(win, y, x, " @ %s", event->loc);
325 if (flags & SHOW_ACTIVE && event == EVENT)
326 wattroff(win, A_REVERSE | A_BOLD);
329 void todo_line(WINDOW *win, todo_t *todo, int y, int x, int w, int flags)
333 sprintf(perc, "%2d%%", todo->status);
335 int cat = get_color(todo->cat);
336 int status = todo->status == NEW ? COLOR_NEW :
337 todo->status == DONE ? COLOR_DONE : COLOR_WIP;
339 sprintf(desc, "%s", todo->name ?: todo->desc ?: "");
340 strsub(desc, '\n', ';');
343 if (cat) wattron(win, COLOR_PAIR(cat));
344 mvwaddch(win, y, x, ACS_BLOCK);
345 if (cat) wattroff(win, COLOR_PAIR(cat));
349 if (flags & SHOW_ACTIVE && todo == TODO)
350 wattron(win, A_REVERSE | A_BOLD);
351 mvwhline(win, y, x, ' ', COLS-x);
354 if (no_date(&todo->due))
355 mvwprintw(win, y, x, "[no due date]");
357 mvwprintw(win, y, x, "%04d-%02d-%02d %2d:%02d",
358 todo->due.year, todo->due.month+1, todo->due.day+1,
359 todo->due.hour, todo->due.min);
363 if (status) wattron(win, COLOR_PAIR(status));
364 mvwprintw(win, y, x, "%s",
365 todo->status == NEW ? "new" :
366 todo->status == DONE ? "done" : perc);
367 if (status) wattroff(win, COLOR_PAIR(status));
370 /* Print description */
371 mvwprintw(win, y, x, "%s", desc);
374 if (flags & SHOW_ACTIVE && todo == TODO)
375 wattroff(win, A_REVERSE | A_BOLD);
381 /* Set default escape timeout */
382 if (!getenv("ESCDELAY"))
383 putenv("ESCDELAY=25");
386 setlocale(LC_ALL, "");
390 keypad(stdscr, TRUE);
394 use_default_colors();
395 mousemask(ALL_MOUSE_EVENTS, NULL);
397 init_pair(COLOR_TITLE, COLOR_GREEN, -1);
398 init_pair(COLOR_ERROR, COLOR_RED, -1);
400 init_pair(COLOR_NEW, COLOR_RED, -1);
401 init_pair(COLOR_WIP, COLOR_YELLOW, -1);
402 init_pair(COLOR_DONE, COLOR_GREEN, -1);
404 init_pair(COLOR_CLASS, COLOR_BLUE, -1);
405 init_pair(COLOR_EC, COLOR_GREEN, -1);
406 init_pair(COLOR_WORK, COLOR_MAGENTA, -1);
407 init_pair(COLOR_OTHER, COLOR_RED, -1);
412 for (int i = 0; i < N_ELEMENTS(views); i++) {
413 int hdr = COMPACT ? 1 : 2;
414 views[i]->win = newwin(LINES-hdr, COLS, hdr, 0);
415 views[i]->init(views[i]->win);
416 views[i]->size(LINES-hdr, COLS);
421 void view_config(const char *group, const char *name, const char *key, const char *value)
423 if (match(group, "view")) {
424 if (match(key, "compact")) {
425 COMPACT = get_bool(value);
426 } else if (match(key, "morning")) {
427 MORNING = get_number(value);
428 } else if (match(key, "active")) {
429 for (int i = 0; i < N_ELEMENTS(views); i++) {
430 if (match(value, views[i]->name)) {
432 view = active = views[i];
441 void view_edit(edit_t mode)
444 set_view(active, &edit_view);
449 /* Draw initial view */
458 if (chr == KEY_MOUSE)
459 if (getmouse(&btn) != OK)
461 if (process(chr, btn.bstate, btn.y, btn.x))
463 if (chr == ERR) // timeout
467 debug("main: Unhandled key - Dec %3d, Hex %02x, Oct %03o, Chr <%c>",
481 void view_debug(const char *fmt, va_list ap)
484 int rev = COMPACT ? A_BOLD : 0;
486 mvhline(LINES-2, 0, ACS_HLINE, COLS);
488 attron(COLOR_PAIR(COLOR_ERROR) | rev);
489 vwprintw(stdscr, fmt, ap);
490 attroff(COLOR_PAIR(COLOR_ERROR) | rev);