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