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