]> Pileus Git - lackey/blob - src/view.c
8341e97aa7433a428aefec70f5246b152937a837
[lackey] / src / view.c
1 /*
2  * Copyright (C) 2012-2013 Andy Spencer <andy753421@gmail.com>
3  *
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.
8  *
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.
13  *
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/>.
16  */
17
18 #define _XOPEN_SOURCE_EXTENDED
19
20 #include <string.h>
21 #include <ncurses.h>
22
23 #include "util.h"
24 #include "conf.h"
25 #include "date.h"
26 #include "cal.h"
27 #include "view.h"
28
29 /* Types */
30 typedef struct {
31         const char *name;
32         const char *title;
33         void      (*init)(WINDOW*);
34         void      (*size)(int,int);
35         void      (*draw)(void);
36         int       (*run)(int,mmask_t,int,int);
37         int         keys[8];
38         WINDOW     *win;
39 } view_t;
40
41 /* Macros */
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 = {                \
48                 #name,                        \
49                 title,                        \
50                 name##_init,                  \
51                 name##_size,                  \
52                 name##_draw,                  \
53                 name##_run,                   \
54                 { __VA_ARGS__ }               \
55         }
56
57 /* Views */
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');
66 VIEW(edit,     "Edit");
67
68 /* View data */
69 view_t  spacer = { "|", "|" };
70
71 view_t *views[] = {
72         &day_view, &week_view, &month_view, &year_view,
73         &events_view, &todo_view,
74         &settings_view, &help_view,
75         &edit_view
76 };
77
78 view_t *menu[] = {
79         &day_view, &week_view, &month_view, &year_view,
80         &spacer, &events_view, &todo_view,
81         &spacer, &settings_view, &help_view
82 };
83
84 /* Config data */
85 int COMPACT = 0;
86
87 /* Global data */
88 edit_t EDIT = EDIT_NONE;
89
90 /* Local data */
91 view_t *view   = &day_view;
92 view_t *active = &day_view;
93 view_t *popup  = NULL;
94
95 /* Local functions */
96 static void draw_header(void)
97 {
98         move(0, 0);
99         attron(COLOR_PAIR(COLOR_TITLE));
100         clrtoeol();
101
102         /* Draw menu */
103         for (int i = 0; i < N_ELEMENTS(menu); i++) {
104                 if (menu[i] == active)
105                         attron(A_BOLD);
106                 printw("%s ", menu[i]->title);
107                 if (menu[i] == active)
108                         attroff(A_BOLD);
109         }
110
111         /* Draw popup window */
112         if (popup) {
113                 printw("| ");
114                 attron(A_BOLD);
115                 printw("[%s]", popup->title);
116                 attroff(A_BOLD);
117         }
118
119         /* Draw date */
120         move(0, COLS-19);
121         printw("%04d-%02d-%02d %02d:%02d:%02d",
122                         NOW.year, NOW.month+1, NOW.day+1,
123                         NOW.hour, NOW.min,     NOW.sec);
124
125         attroff(COLOR_PAIR(COLOR_TITLE));
126         if (!COMPACT)
127                 mvhline(1, 0, ACS_HLINE, COLS);
128         refresh();
129 }
130
131 static int get_color(const char *cat)
132 {
133         return cat == NULL           ? 0           :
134                match(cat, "class") ? COLOR_CLASS :
135                match(cat, "ec")    ? COLOR_EC    :
136                match(cat, "work")  ? COLOR_WORK  : COLOR_OTHER ;
137 }
138
139 static int set_view(view_t *_active, view_t *_popup)
140 {
141         view = _popup ?: _active;
142         if (active != _active) {
143                 active = _active;
144                 set_string("view", 0, "active", active->name);
145                 view_draw();
146         }
147         if (popup != _popup) {
148                 popup = _popup;
149                 view_draw();
150         }
151         return 1;
152 }
153
154 /* Curses functions */
155 void wmvresize(WINDOW *win, int top, int left, int rows, int cols)
156 {
157         int y = getpary(win);
158         if (top < y)
159                 mvderwin(win, top, left);
160         wresize(win, rows, cols);
161         if (top > y)
162                 mvderwin(win, top, left);
163 }
164
165 void wshrink(WINDOW *win, int top)
166 {
167         int x    = getparx(win);
168         int y    = getpary(win);
169         int r    = getmaxy(win);
170         int c    = getmaxx(win);
171         int rows = r + (y - top);
172         if (top  <  y) mvderwin(win, top, x);
173         if (rows != r) wresize(win, rows, c);
174         if (top  >  y) mvderwin(win, top, x);
175 }
176
177 /* Helper functions */
178 void event_box(WINDOW *win, event_t *event, int y, int x, int h, int w)
179 {
180         int l = 0;
181         int s = y < 0 ? -y-1 : 0;
182
183         int color = get_color(event->cat);
184
185         if (color) wattron(win, COLOR_PAIR(color));
186
187         if (h >= 2) mvwhline_set(win, y,     x+1,   WACS_T_HLINE, w-2);
188         if (h <= 1) mvwadd_wch(win,   y,     x,     WACS_BULLET);
189         if (h >= 2) mvwadd_wch(win,   y,     x,     WACS_T_ULCORNER);
190         if (h >= 2) mvwadd_wch(win,   y,     x+w-1, WACS_T_URCORNER);
191         if (h >= 3) mvwvline_set(win, y+1+s, x,     WACS_T_VLINE, h-2-s);
192         if (h >= 3) mvwvline_set(win, y+1+s, x+w-1, WACS_T_VLINE, h-2-s);
193         if (h >= 2) mvwadd_wch(win,   y+h-1, x,     WACS_T_LLCORNER);
194         if (h >= 2) mvwadd_wch(win,   y+h-1, x+w-1, WACS_T_LRCORNER);
195         if (h >= 2) mvwhline_set(win, y+h-1, x+1,   WACS_T_HLINE, w-2);
196
197         if (color) wattroff(win, COLOR_PAIR(color));
198
199         if (l<h && event->name) mvwprintw(win, y+l++, x+1, "%.*s",   w-2, event->name);
200         if (l<h && event->loc)  mvwprintw(win, y+l++, x+1, "@ %.*s", w-4, event->loc);
201         if (l<h && event->desc) mvwprintw(win, y+l++, x+1, "%.*s",   w-2, event->desc);
202 }
203
204 void event_line(WINDOW *win, event_t *event, int y, int x, int w, int flags)
205 {
206         int color = get_color(event->cat);
207
208         if (color) wattron(win, COLOR_PAIR(color));
209         mvwaddch(win, y, x++, ACS_BLOCK);
210         if (color) wattroff(win, COLOR_PAIR(color));
211
212         if (flags & SHOW_ACTIVE && event == EVENT)
213                 wattron(win, A_REVERSE | A_BOLD);
214         if (flags & SHOW_DETAILS) {
215                 if (all_day(&event->start, &event->end))
216                         mvwprintw(win, y, x+1, "[all day]   - ");
217                 else
218                         mvwprintw(win, y, x+1, "%2d:%02d-%2d:%02d - ",
219                                         event->start.hour, event->start.min,
220                                         event->end.hour,   event->end.min);
221                 x += 15;
222                 w -= 15;
223         }
224         if (event->name) {
225                 const char *label = event->name ?: event->desc;
226                 mvwprintw(win, y, x, "%-*.*s", w-1, w-1, label);
227                 x += MIN(strlen(label), w-1);
228         }
229         if (flags & SHOW_DETAILS && event->loc) {
230                 mvwprintw(win, y, x, " @ %s", event->loc);
231         }
232         if (flags & SHOW_ACTIVE && event == EVENT)
233                 wattroff(win, A_REVERSE | A_BOLD);
234 }
235
236 void todo_line(WINDOW *win, todo_t *todo, int y, int x, int w, int flags)
237 {
238         char perc[16];
239         char desc[LINES];
240         sprintf(perc, "%2d%%", todo->status);
241
242         int cat    = get_color(todo->cat);
243         int status = todo->status == NEW  ? COLOR_NEW  :
244                      todo->status == DONE ? COLOR_DONE : COLOR_WIP;
245
246         sprintf(desc, "%s", todo->name ?: todo->desc ?: "");
247         strsub(desc, '\n', ';');
248
249         /* Print category */
250         if (cat) wattron(win, COLOR_PAIR(cat));
251         mvwaddch(win, y, x, ACS_BLOCK);
252         if (cat) wattroff(win, COLOR_PAIR(cat));
253         x += 2;
254
255         /* Set background */
256         if (flags & SHOW_ACTIVE && todo == TODO)
257                 wattron(win, A_REVERSE | A_BOLD);
258         mvwhline(win, y, x, ' ', COLS-x);
259
260         /* Print time */
261         if (no_date(&todo->due))
262                 mvwprintw(win, y, x, "[no due date]");
263         else
264                 mvwprintw(win, y, x, "%04d-%02d-%02d %2d:%02d",
265                                 todo->due.year, todo->due.month+1, todo->due.day+1,
266                                 todo->due.hour, todo->due.min);
267         x += 18;
268
269         /* Print status */
270         if (status) wattron(win, COLOR_PAIR(status));
271         mvwprintw(win, y, x, "%s",
272                 todo->status == NEW    ? "new"  :
273                 todo->status == DONE   ? "done" : perc);
274         if (status) wattroff(win, COLOR_PAIR(status));
275         x += 6;
276
277         /* Print description */
278         mvwprintw(win, y, x, "%s", desc);
279
280         /* Reset flags */
281         if (flags & SHOW_ACTIVE && todo == TODO)
282                 wattroff(win, A_REVERSE | A_BOLD);
283 }
284
285 /* View init */
286 void view_init(void)
287 {
288         int hdr = COMPACT ? 1 : 2;
289         for (int i = 0; i < N_ELEMENTS(views); i++) {
290                 views[i]->win = newwin(LINES-hdr, COLS, hdr, 0);
291                 views[i]->init(views[i]->win);
292                 views[i]->size(LINES-hdr, COLS);
293         }
294 }
295
296 /* Config parser */
297 void view_config(const char *group, const char *name, const char *key, const char *value)
298 {
299         if (match(group, "view")) {
300                 if (match(key, "compact")) {
301                         COMPACT = get_bool(value);
302                 } else if (match(key, "active")) {
303                         for (int i = 0; i < N_ELEMENTS(views); i++) {
304                                 if (match(value, views[i]->name)) {
305                                         get_string(value);
306                                         view = active = views[i];
307                                         break;
308                                 }
309                         }
310                 }
311         }
312 }
313
314 /* View draw */
315 void view_resize(void)
316 {
317         int hdr = COMPACT ? 1 : 2;
318         for (int i = 0; i < N_ELEMENTS(views); i++) {
319                 wresize(views[i]->win, LINES-hdr, COLS);
320                 mvwin(views[i]->win, hdr, 0);
321                 views[i]->size(LINES-hdr, COLS);
322         }
323 }
324
325 /* View draw */
326 void view_draw(void)
327 {
328         draw_header();
329         werase(view->win);
330         view->draw();
331         wrefresh(view->win);
332 }
333
334 /* View run */
335 int view_run(int key, mmask_t btn, int row, int col)
336 {
337         /* Refresh timestamp */
338         draw_header();
339
340         /* Check for mouse events on the menu */
341         if (key == KEY_MOUSE && row == 0) {
342                 int start = 1;
343                 for (int i = 0; i < N_ELEMENTS(menu); i++) {
344                         int end = start + strlen(menu[i]->name) - 1;
345                         if (start <= col && col <= end && menu[i]->draw)
346                                 return set_view(menu[i], NULL);
347                         start = end + 2;
348                 }
349         }
350
351         /* Look though menu for hotkeys */
352         for (int i = 0; i < N_ELEMENTS(menu); i++) {
353                 for (int j = 0; j < N_ELEMENTS(menu[i]->keys); j++)
354                         if (menu[i]->keys[j] == key)
355                                 return set_view(menu[i], NULL);
356         }
357
358         /* Shift windows with left/right keys */
359         int shift = key == KEY_RIGHT ? +1 :
360                     key == KEY_LEFT  ? -1 : 0;
361         if (shift) {
362                 int num = 0;
363                 for (int i = 0; i < N_ELEMENTS(menu); i++)
364                         if (menu[i] == active)
365                                 num = i;
366                 do  {
367                         num += shift;
368                         num += N_ELEMENTS(menu);
369                         num %= N_ELEMENTS(menu);
370                 } while (menu[num] == &spacer);
371                 return set_view(menu[num], NULL);
372         }
373
374         /* Handle other keys */
375         switch (key) {
376                 case 'c':
377                         COMPACT ^= 1;
378                         set_bool("view", 0, "compact", COMPACT);
379                         view_resize();
380                         view_draw();
381                         return 1;
382                 case '\033': // escape
383                         return set_view(active, NULL);
384                 case '?':    // help
385                         return set_view(active, &help_view);
386                 case 'e':    // edit
387                         return set_view(active, &edit_view);
388         }
389
390         /* Pass key to active view */
391         return view->run(key, btn, row, col);
392 }
393
394 /* View event */
395 void view_edit(edit_t mode)
396 {
397         EDIT = mode;
398         set_view(active, &edit_view);
399 }