]> Pileus Git - lackey/blob - src/view.c
Add popup views
[lackey] / src / view.c
1 /*
2  * Copyright (C) 2012 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         char   *name;
32         void  (*init)(WINDOW*);
33         void  (*size)(int,int);
34         void  (*draw)(void);
35         int   (*run)(int,mmask_t,int,int);
36         int     keys[8];
37         WINDOW *win;
38 } view_t;
39
40 /* Macros */
41 #define VIEW(name)                     \
42         void name##_init(WINDOW *win); \
43         void name##_size(int,int);     \
44         void name##_draw(void);        \
45         int  name##_run(int,mmask_t,int,int)
46
47 /* Prototypes */
48 VIEW(day);
49 VIEW(week);
50 VIEW(month);
51 VIEW(year);
52 VIEW(events);
53 VIEW(todo);
54 VIEW(settings);
55 VIEW(help);
56
57 /* View data */
58 static const char *names[] = {
59         "day", "week", "month", "year",
60         "|", "events", "todo",
61         "|", "settings", "help",
62 };
63
64 view_t views[] = {
65         { "Day",      day_init,      day_size,      day_draw,      day_run,      {KEY_F(1), '1'} },
66         { "Week",     week_init,     week_size,     week_draw,     week_run,     {KEY_F(2), '2'} },
67         { "Month",    month_init,    month_size,    month_draw,    month_run,    {KEY_F(3), '3'} },
68         { "Year",     year_init,     year_size,     year_draw,     year_run,     {KEY_F(4), '4'} },
69         { "|",        NULL,          NULL,          NULL,          NULL,         {             } },
70         { "Events",   events_init,   events_size,   events_draw,   events_run,   {KEY_F(5), '5'} },
71         { "Todo",     todo_init,     todo_size,     todo_draw,     todo_run,     {KEY_F(6), '6'} },
72         { "|",        NULL,          NULL,          NULL,          NULL,         {             } },
73         { "Settings", settings_init, settings_size, settings_draw, settings_run, {KEY_F(7), '7'} },
74         { "Help",     help_init,     help_size,     help_draw,     help_run,     {KEY_F(8), '8'} },
75         { NULL,       NULL,          NULL,          NULL,          NULL,         {             } },
76 };
77
78 /* Config data */
79 int COMPACT = 0;
80 int ACTIVE  = 0;
81 int POPUP   = -1;
82
83 /* Local functions */
84 static void draw_header(void)
85 {
86         move(0, 0);
87         attron(COLOR_PAIR(COLOR_TITLE));
88         for (int i = 0; i < N_ELEMENTS(views); i++) {
89                 if (!views[i].name)
90                         break;
91                 if (i == ACTIVE)
92                         attron(A_BOLD);
93                 printw("%s ", views[i].name);
94                 if (i == ACTIVE)
95                         attroff(A_BOLD);
96         }
97         clrtoeol();
98         if (POPUP >= 0) {
99                 attron(A_BOLD);
100                 move(0, COLS-strlen(views[POPUP].name)-2);
101                 printw("[%s]", views[POPUP].name);
102                 attroff(A_BOLD);
103         }
104         attroff(COLOR_PAIR(COLOR_TITLE));
105         if (!COMPACT)
106                 mvhline(1, 0, ACS_HLINE, COLS);
107         refresh();
108 }
109
110 static int get_color(const char *cat)
111 {
112         return cat == NULL           ? 0           :
113                match(cat, "class") ? COLOR_CLASS :
114                match(cat, "ec")    ? COLOR_EC    :
115                match(cat, "work")  ? COLOR_WORK  : COLOR_OTHER ;
116 }
117
118 static int view_set(int active, int popup)
119 {
120         if (ACTIVE != active) {
121                 ACTIVE = active;
122                 set_enum("view", 0, "active", ACTIVE,
123                                 names, N_ELEMENTS(names));
124                 view_draw();
125         }
126         if (POPUP != popup) {
127                 POPUP = popup;
128                 view_draw();
129         }
130         return 1;
131 }
132
133 /* Curses functions */
134 void wmvresize(WINDOW *win, int top, int left, int rows, int cols)
135 {
136         int y = getpary(win);
137         if (top < y)
138                 mvderwin(win, top, left);
139         wresize(win, rows, cols);
140         if (top > y)
141                 mvderwin(win, top, left);
142 }
143
144 void wshrink(WINDOW *win, int top)
145 {
146         int x    = getparx(win);
147         int y    = getpary(win);
148         int r    = getmaxy(win);
149         int c    = getmaxx(win);
150         int rows = r + (y - top);
151         if (top  <  y) mvderwin(win, top, x);
152         if (rows != r) wresize(win, rows, c);
153         if (top  >  y) mvderwin(win, top, x);
154 }
155
156 /* Helper functions */
157 void event_box(WINDOW *win, event_t *event, int y, int x, int h, int w)
158 {
159         int l = 0;
160         int s = y < 0 ? -y-1 : 0;
161
162         int color = get_color(event->cat);
163
164         if (color) wattron(win, COLOR_PAIR(color));
165
166         if (h >= 2) mvwhline_set(win, y,     x+1,   WACS_T_HLINE, w-2);
167         if (h <= 1) mvwadd_wch(win,   y,     x,     WACS_BULLET);
168         if (h >= 2) mvwadd_wch(win,   y,     x,     WACS_T_ULCORNER);
169         if (h >= 2) mvwadd_wch(win,   y,     x+w-1, WACS_T_URCORNER);
170         if (h >= 3) mvwvline_set(win, y+1+s, x,     WACS_T_VLINE, h-2-s);
171         if (h >= 3) mvwvline_set(win, y+1+s, x+w-1, WACS_T_VLINE, h-2-s);
172         if (h >= 2) mvwadd_wch(win,   y+h-1, x,     WACS_T_LLCORNER);
173         if (h >= 2) mvwadd_wch(win,   y+h-1, x+w-1, WACS_T_LRCORNER);
174         if (h >= 2) mvwhline_set(win, y+h-1, x+1,   WACS_T_HLINE, w-2);
175
176         if (color) wattroff(win, COLOR_PAIR(color));
177
178         if (l<h && event->name) mvwprintw(win, y+l++, x+1, "%.*s",   w-2, event->name);
179         if (l<h && event->loc)  mvwprintw(win, y+l++, x+1, "@ %.*s", w-4, event->loc);
180         if (l<h && event->desc) mvwprintw(win, y+l++, x+1, "%.*s",   w-2, event->desc);
181 }
182
183 void event_line(WINDOW *win, event_t *event, int y, int x, int w, int full)
184 {
185         int color = get_color(event->cat);
186
187         if (color) wattron(win, COLOR_PAIR(color));
188         mvwaddch(win, y, x++, ACS_BLOCK);
189         if (color) wattroff(win, COLOR_PAIR(color));
190
191         if (full) {
192                 if (all_day(&event->start, &event->end))
193                         mvwprintw(win, y, x, " [all day]   -");
194                 else
195                         mvwprintw(win, y, x, " %2d:%02d-%2d:%02d -",
196                                         event->start.hour, event->start.min,
197                                         event->end.hour,   event->end.min);
198                 x += 15;
199         }
200         if (event->name) {
201                 const char *label = event->name ?: event->desc;
202                 mvwprintw(win, y, x, "%-*.*s", w-1, w-1, label);
203                 x += MIN(strlen(label), w-1);
204         }
205         if (full && event->loc) {
206                 mvwprintw(win, y, x, " @ %s", event->loc);
207         }
208 }
209
210 void todo_line(WINDOW *win, todo_t *todo, int y, int x, int w, int full)
211 {
212         char perc[16];
213         char desc[LINES];
214         sprintf(perc, "%2d%%", todo->status);
215
216         int cat    = get_color(todo->cat);
217         int status = todo->status == NEW  ? COLOR_NEW  :
218                      todo->status == DONE ? COLOR_DONE : COLOR_WIP;
219
220         sprintf(desc, "%s", todo->name ?: todo->desc ?: "");
221         strsub(desc, '\n', ';');
222
223         /* Print category */
224         if (cat) wattron(win, COLOR_PAIR(cat));
225         mvwaddch(win, y, x, ACS_BLOCK);
226         if (cat) wattroff(win, COLOR_PAIR(cat));
227         x += 2;
228
229         /* Print time */
230         if (no_date(&todo->due))
231                 mvwprintw(win, y, x, "[no due date]");
232         else
233                 mvwprintw(win, y, x, "%04d-%02d-%02d %2d:%02d",
234                                 todo->due.year, todo->due.month+1, todo->due.day+1,
235                                 todo->due.hour, todo->due.min);
236         x += 18;
237
238         /* Print status */
239         if (status) wattron(win, COLOR_PAIR(status));
240         mvwprintw(win, y, x, "%s",
241                 todo->status == NEW    ? "new"  :
242                 todo->status == DONE   ? "done" : perc);
243         if (status) wattroff(win, COLOR_PAIR(status));
244         x += 6;
245
246         /* Print description */
247         mvwprintw(win, y, x, "%s", desc);
248 }
249
250 /* View init */
251 void view_init(void)
252 {
253         int hdr = COMPACT ? 1 : 2;
254         for (int i = 0; i < N_ELEMENTS(views); i++) {
255                 if (views[i].init) {
256                         views[i].win = newwin(LINES-hdr, COLS, hdr, 0);
257                         views[i].init(views[i].win);
258                 }
259                 if (views[i].size)
260                         views[i].size(LINES-hdr, COLS);
261         }
262 }
263
264 /* Config parser */
265 void view_config(const char *group, const char *name, const char *key, const char *value)
266 {
267         if (match(group, "view")) {
268                 if (match(key, "compact"))
269                         COMPACT = get_bool(value);
270                 else if (match(key, "active"))
271                         ACTIVE = get_enum(value, names, N_ELEMENTS(names));
272         }
273 }
274
275 /* View draw */
276 void view_resize(void)
277 {
278         int hdr = COMPACT ? 1 : 2;
279         for (int i = 0; i < N_ELEMENTS(views); i++) {
280                 if (views[i].win) {
281                         wresize(views[i].win, LINES-hdr, COLS);
282                         mvwin(views[i].win, hdr, 0);
283                 }
284                 if (views[i].size)
285                         views[i].size(LINES-hdr, COLS);
286         }
287 }
288
289 /* View draw */
290 void view_draw(void)
291 {
292         int view = POPUP >= 0 ? POPUP : ACTIVE;
293         draw_header();
294         werase(views[view].win);
295         views[view].draw();
296         wrefresh(views[view].win);
297 }
298
299 /* View run */
300 int view_run(int key, mmask_t btn, int row, int col)
301 {
302         /* Check for compact mode toggle */
303         if (key == 'c') {
304                 COMPACT ^= 1;
305                 set_bool("view", 0, "compact", COMPACT);
306                 view_resize();
307                 view_draw();
308                 return 1;
309         }
310
311         /* Check for mouse events */
312         if (key == KEY_MOUSE && row == 0) {
313                 int start = 1;
314                 for (int i = 0; i < N_ELEMENTS(views); i++) {
315                         int end = start + strlen(views[i].name) - 1;
316                         if (start <= col && col <= end && views[i].draw)
317                                 return view_set(i, -1);
318                         start = end + 2;
319                 }
320         }
321
322         /* Check for view change */
323         for (int i = 0; i < N_ELEMENTS(views); i++) {
324                 if (i == ACTIVE)
325                         continue;
326                 for (int j = 0; j < N_ELEMENTS(views[i].keys); j++)
327                         if (views[i].keys[j] == key)
328                                 return view_set(i, -1);
329         }
330
331         /* Shift windows */
332         int num   = ACTIVE;
333         int shift = key == KEY_RIGHT ? +1 :
334                     key == KEY_LEFT  ? -1 : 0;
335         while (shift) {
336                 num += shift;
337                 num += N_ELEMENTS(views);
338                 num %= N_ELEMENTS(views);
339                 if (views[num].run)
340                         return view_set(num, -1);
341         }
342
343         /* Handle popup views */
344         switch (key) {
345                 case '\033': // escape
346                         return view_set(ACTIVE, -1);
347                 case '?':    // help
348                         return view_set(ACTIVE, 9);
349         }
350
351         /* Pass key to active view */
352         int view = POPUP >= 0 ? POPUP : ACTIVE;
353         return views[view].run(key, btn, row, col);
354 }