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
31 void (*init)(WINDOW*);
32 void (*size)(int,int);
34 int (*run)(int,mmask_t,int,int);
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)
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', '?'} },
75 static void draw_header(void)
78 attron(COLOR_PAIR(COLOR_TITLE));
79 for (int i = 0; i < N_ELEMENTS(views); i++) {
82 printw("%s ", views[i].name);
86 attroff(COLOR_PAIR(COLOR_TITLE));
88 mvhline(1, 0, ACS_HLINE, COLS);
92 static int get_color(const char *cat)
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 ;
100 /* Helper functions */
101 void event_box(WINDOW *win, event_t *event, int y, int x, int h, int w)
104 int s = y < 0 ? -y-1 : 0;
106 int color = get_color(event->cat);
108 if (color) wattron(win, COLOR_PAIR(color));
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);
120 if (color) wattroff(win, COLOR_PAIR(color));
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);
127 void event_line(WINDOW *win, event_t *event, int y, int x, int w, int full)
129 int color = get_color(event->cat);
131 if (color) wattron(win, COLOR_PAIR(color));
132 mvwaddch(win, y, x++, ACS_BLOCK);
133 if (color) wattroff(win, COLOR_PAIR(color));
136 if (all_day(&event->start, &event->end))
137 mvwprintw(win, y, x, " [all day] -");
139 mvwprintw(win, y, x, " %2d:%02d-%2d:%02d -",
140 event->start.hour, event->start.min,
141 event->end.hour, event->end.min);
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);
149 if (full && event->loc) {
150 mvwprintw(win, y, x, " @ %s", event->loc);
154 void todo_line(WINDOW *win, todo_t *todo, int y, int x, int w, int full)
158 sprintf(perc, "%2d%%", todo->status);
160 int cat = get_color(todo->cat);
161 int status = todo->status == NEW ? COLOR_NEW :
162 todo->status == DONE ? COLOR_DONE : COLOR_WIP;
164 sprintf(desc, "%s", todo->name ?: todo->desc ?: "");
165 strsub(desc, '\n', ';');
168 if (cat) wattron(win, COLOR_PAIR(cat));
169 mvwaddch(win, y, x, ACS_BLOCK);
170 if (cat) wattroff(win, COLOR_PAIR(cat));
174 if (no_date(&todo->due))
175 mvwprintw(win, y, x, "[no due date]");
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);
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));
190 /* Print description */
191 mvwprintw(win, y, x, "%s", desc);
194 /* Curses functions */
195 void wmvresize(WINDOW *win, int top, int left, int rows, int cols)
197 int y = getpary(win);
199 mvderwin(win, top, left);
200 wresize(win, rows, cols);
202 mvderwin(win, top, left);
205 void wshrink(WINDOW *win, int top)
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);
220 int hdr = COMPACT ? 1 : 2;
221 for (int i = 0; i < N_ELEMENTS(views); i++) {
223 views[i].win = newwin(LINES-hdr, COLS, hdr, 0);
224 views[i].init(views[i].win);
227 views[i].size(LINES-hdr, COLS);
232 void view_resize(void)
234 int hdr = COMPACT ? 1 : 2;
235 for (int i = 0; i < N_ELEMENTS(views); i++) {
237 wresize(views[i].win, LINES-hdr, COLS);
238 mvwin(views[i].win, hdr, 0);
241 views[i].size(LINES-hdr, COLS);
249 werase(views[ACTIVE].win);
250 views[ACTIVE].draw();
251 wrefresh(views[ACTIVE].win);
255 int view_set(int num)
265 int view_run(int key, mmask_t btn, int row, int col)
267 /* Check for compact mode toggle */
275 /* Check for mouse events */
276 if (key == KEY_MOUSE && row == 0) {
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)
286 /* Check for view change */
287 for (int i = 0; i < N_ELEMENTS(views); i++) {
290 for (int j = 0; j < N_ELEMENTS(views[i].keys); j++)
291 if (views[i].keys[j] == key)
297 int shift = key == KEY_RIGHT ? +1 :
298 key == KEY_LEFT ? -1 : 0;
301 num += N_ELEMENTS(views);
302 num %= N_ELEMENTS(views);
304 return view_set(num);
307 /* Pass key to active view */
308 return views[ACTIVE].run(key, btn, row, col);