2 * Copyright (C) 2012 Andy Spencer <andy753421@gmail.com>
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.
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.
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/>.
18 #define _XOPEN_SOURCE_EXTENDED
32 void (*init)(WINDOW*);
33 void (*size)(int,int);
35 int (*run)(int,mmask_t,int,int);
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)
58 static const char *names[] = {
59 "day", "week", "month", "year",
60 "|", "events", "todo",
61 "|", "settings", "help",
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', '?'} },
82 static void draw_header(void)
85 attron(COLOR_PAIR(COLOR_TITLE));
86 for (int i = 0; i < N_ELEMENTS(views); i++) {
89 printw("%s ", views[i].name);
93 attroff(COLOR_PAIR(COLOR_TITLE));
95 mvhline(1, 0, ACS_HLINE, COLS);
99 static int get_color(const char *cat)
101 return cat == NULL ? 0 :
102 match(cat, "class") ? COLOR_CLASS :
103 match(cat, "ec") ? COLOR_EC :
104 match(cat, "work") ? COLOR_WORK : COLOR_OTHER ;
107 static int view_set(int num)
111 set_enum("view", 0, "active", ACTIVE,
112 names, N_ELEMENTS(names));
118 /* Curses functions */
119 void wmvresize(WINDOW *win, int top, int left, int rows, int cols)
121 int y = getpary(win);
123 mvderwin(win, top, left);
124 wresize(win, rows, cols);
126 mvderwin(win, top, left);
129 void wshrink(WINDOW *win, int top)
131 int x = getparx(win);
132 int y = getpary(win);
133 int r = getmaxy(win);
134 int c = getmaxx(win);
135 int rows = r + (y - top);
136 if (top < y) mvderwin(win, top, x);
137 if (rows != r) wresize(win, rows, c);
138 if (top > y) mvderwin(win, top, x);
141 /* Helper functions */
142 void event_box(WINDOW *win, event_t *event, int y, int x, int h, int w)
145 int s = y < 0 ? -y-1 : 0;
147 int color = get_color(event->cat);
149 if (color) wattron(win, COLOR_PAIR(color));
151 if (h >= 2) mvwhline_set(win, y, x+1, WACS_T_HLINE, w-2);
152 if (h <= 1) mvwadd_wch(win, y, x, WACS_BULLET);
153 if (h >= 2) mvwadd_wch(win, y, x, WACS_T_ULCORNER);
154 if (h >= 2) mvwadd_wch(win, y, x+w-1, WACS_T_URCORNER);
155 if (h >= 3) mvwvline_set(win, y+1+s, x, WACS_T_VLINE, h-2-s);
156 if (h >= 3) mvwvline_set(win, y+1+s, x+w-1, WACS_T_VLINE, h-2-s);
157 if (h >= 2) mvwadd_wch(win, y+h-1, x, WACS_T_LLCORNER);
158 if (h >= 2) mvwadd_wch(win, y+h-1, x+w-1, WACS_T_LRCORNER);
159 if (h >= 2) mvwhline_set(win, y+h-1, x+1, WACS_T_HLINE, w-2);
161 if (color) wattroff(win, COLOR_PAIR(color));
163 if (l<h && event->name) mvwprintw(win, y+l++, x+1, "%.*s", w-2, event->name);
164 if (l<h && event->loc) mvwprintw(win, y+l++, x+1, "@ %.*s", w-4, event->loc);
165 if (l<h && event->desc) mvwprintw(win, y+l++, x+1, "%.*s", w-2, event->desc);
168 void event_line(WINDOW *win, event_t *event, int y, int x, int w, int full)
170 int color = get_color(event->cat);
172 if (color) wattron(win, COLOR_PAIR(color));
173 mvwaddch(win, y, x++, ACS_BLOCK);
174 if (color) wattroff(win, COLOR_PAIR(color));
177 if (all_day(&event->start, &event->end))
178 mvwprintw(win, y, x, " [all day] -");
180 mvwprintw(win, y, x, " %2d:%02d-%2d:%02d -",
181 event->start.hour, event->start.min,
182 event->end.hour, event->end.min);
186 const char *label = event->name ?: event->desc;
187 mvwprintw(win, y, x, "%-*.*s", w-1, w-1, label);
188 x += MIN(strlen(label), w-1);
190 if (full && event->loc) {
191 mvwprintw(win, y, x, " @ %s", event->loc);
195 void todo_line(WINDOW *win, todo_t *todo, int y, int x, int w, int full)
199 sprintf(perc, "%2d%%", todo->status);
201 int cat = get_color(todo->cat);
202 int status = todo->status == NEW ? COLOR_NEW :
203 todo->status == DONE ? COLOR_DONE : COLOR_WIP;
205 sprintf(desc, "%s", todo->name ?: todo->desc ?: "");
206 strsub(desc, '\n', ';');
209 if (cat) wattron(win, COLOR_PAIR(cat));
210 mvwaddch(win, y, x, ACS_BLOCK);
211 if (cat) wattroff(win, COLOR_PAIR(cat));
215 if (no_date(&todo->due))
216 mvwprintw(win, y, x, "[no due date]");
218 mvwprintw(win, y, x, "%04d-%02d-%02d %2d:%02d",
219 todo->due.year, todo->due.month+1, todo->due.day+1,
220 todo->due.hour, todo->due.min);
224 if (status) wattron(win, COLOR_PAIR(status));
225 mvwprintw(win, y, x, "%s",
226 todo->status == NEW ? "new" :
227 todo->status == DONE ? "done" : perc);
228 if (status) wattroff(win, COLOR_PAIR(status));
231 /* Print description */
232 mvwprintw(win, y, x, "%s", desc);
238 int hdr = COMPACT ? 1 : 2;
239 for (int i = 0; i < N_ELEMENTS(views); i++) {
241 views[i].win = newwin(LINES-hdr, COLS, hdr, 0);
242 views[i].init(views[i].win);
245 views[i].size(LINES-hdr, COLS);
250 void view_config(const char *group, const char *name, const char *key, const char *value)
252 if (match(group, "view")) {
253 if (match(key, "compact"))
254 COMPACT = get_bool(value);
255 else if (match(key, "active"))
256 ACTIVE = get_enum(value, names, N_ELEMENTS(names));
261 void view_resize(void)
263 int hdr = COMPACT ? 1 : 2;
264 for (int i = 0; i < N_ELEMENTS(views); i++) {
266 wresize(views[i].win, LINES-hdr, COLS);
267 mvwin(views[i].win, hdr, 0);
270 views[i].size(LINES-hdr, COLS);
278 werase(views[ACTIVE].win);
279 views[ACTIVE].draw();
280 wrefresh(views[ACTIVE].win);
284 int view_run(int key, mmask_t btn, int row, int col)
286 /* Check for compact mode toggle */
289 set_bool("view", 0, "compact", COMPACT);
295 /* Check for mouse events */
296 if (key == KEY_MOUSE && row == 0) {
298 for (int i = 0; i < N_ELEMENTS(views); i++) {
299 int end = start + strlen(views[i].name) - 1;
300 if (start <= col && col <= end && views[i].draw)
306 /* Check for view change */
307 for (int i = 0; i < N_ELEMENTS(views); i++) {
310 for (int j = 0; j < N_ELEMENTS(views[i].keys); j++)
311 if (views[i].keys[j] == key)
317 int shift = key == KEY_RIGHT ? +1 :
318 key == KEY_LEFT ? -1 : 0;
321 num += N_ELEMENTS(views);
322 num %= N_ELEMENTS(views);
324 return view_set(num);
327 /* Pass key to active view */
328 return views[ACTIVE].run(key, btn, row, col);