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