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