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 { "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', '?'} },
58 static void draw_header(void)
61 attron(COLOR_PAIR(COLOR_TITLE));
62 for (int i = 0; i < N_ELEMENTS(views); i++) {
65 printw("%s ", views[i].name);
69 attroff(COLOR_PAIR(COLOR_TITLE));
71 mvhline(1, 0, ACS_HLINE, COLS);
75 static int get_color(const char *cat)
77 return cat == NULL ? 0 :
78 match(cat, "class") ? COLOR_CLASS :
79 match(cat, "ec") ? COLOR_EC :
80 match(cat, "work") ? COLOR_WORK : COLOR_OTHER ;
83 /* Helper functions */
84 void event_box(WINDOW *win, event_t *event, int y, int x, int h, int w)
87 int s = y < 0 ? -y-1 : 0;
89 int color = get_color(event->cat);
91 if (color) wattron(win, COLOR_PAIR(color));
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);
103 if (color) wattroff(win, COLOR_PAIR(color));
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);
110 void event_line(WINDOW *win, event_t *event, int y, int x, int w, int full)
112 int color = get_color(event->cat);
114 if (color) wattron(win, COLOR_PAIR(color));
115 mvwaddch(win, y, x++, ACS_BLOCK);
116 if (color) wattroff(win, COLOR_PAIR(color));
119 if (all_day(&event->start, &event->end))
120 mvwprintw(win, y, x, " [all day] -");
122 mvwprintw(win, y, x, " %2d:%02d-%2d:%02d -",
123 event->start.hour, event->start.min,
124 event->end.hour, event->end.min);
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);
132 if (full && event->loc) {
133 mvwprintw(win, y, x, " @ %s", event->loc);
137 void todo_line(WINDOW *win, todo_t *todo, int y, int x, int w, int full)
141 sprintf(perc, "%2d%%", todo->status);
143 int cat = get_color(todo->cat);
144 int status = todo->status == NEW ? COLOR_NEW :
145 todo->status == DONE ? COLOR_DONE : COLOR_WIP;
147 sprintf(desc, "%s", todo->name ?: todo->desc ?: "");
148 strsub(desc, '\n', ';');
151 if (cat) wattron(win, COLOR_PAIR(cat));
152 mvwaddch(win, y, x, ACS_BLOCK);
153 if (cat) wattroff(win, COLOR_PAIR(cat));
157 if (no_date(&todo->due))
158 mvwprintw(win, y, x, "[no due date]");
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);
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));
173 /* Print description */
174 mvwprintw(win, y, x, "%s", desc);
177 /* Curses functions */
178 void wmvresize(WINDOW *win, int top, int left, int rows, int cols)
180 int y = getpary(win);
182 mvderwin(win, top, left);
183 wresize(win, rows, cols);
185 mvderwin(win, top, left);
188 void wshrink(WINDOW *win, int top)
190 int x = getparx(win);
191 int y = getpary(win);
192 int r = getmaxy(win);
193 int c = getmaxx(win);
194 int rows = r + (y - top);
195 if (top < y) mvderwin(win, top, x);
196 if (rows != r) wresize(win, rows, c);
197 if (top > y) mvderwin(win, top, x);
203 int hdr = COMPACT ? 1 : 2;
204 for (int i = 0; i < N_ELEMENTS(views); i++) {
206 views[i].win = newwin(LINES-hdr, COLS, hdr, 0);
207 views[i].init(views[i].win);
210 views[i].size(LINES-hdr, COLS);
215 void view_resize(void)
217 int hdr = COMPACT ? 1 : 2;
218 for (int i = 0; i < N_ELEMENTS(views); i++) {
220 wresize(views[i].win, LINES-hdr, COLS);
221 mvwin(views[i].win, hdr, 0);
224 views[i].size(LINES-hdr, COLS);
232 werase(views[ACTIVE].win);
233 views[ACTIVE].draw();
234 wrefresh(views[ACTIVE].win);
238 int view_set(int num)
248 int view_run(int key, mmask_t btn, int row, int col)
250 /* Check for compact mode toggle */
258 /* Check for mouse events */
259 if (key == KEY_MOUSE && row == 0) {
261 for (int i = 0; i < N_ELEMENTS(views); i++) {
262 int end = start + strlen(views[i].name) - 1;
263 if (start <= col && col <= end && views[i].draw)
269 /* Check for view change */
270 for (int i = 0; i < N_ELEMENTS(views); i++) {
273 for (int j = 0; j < N_ELEMENTS(views[i].keys); j++)
274 if (views[i].keys[j] == key)
280 int shift = key == KEY_RIGHT ? +1 :
281 key == KEY_LEFT ? -1 : 0;
284 num += N_ELEMENTS(views);
285 num %= N_ELEMENTS(views);
287 return view_set(num);
290 /* Pass key to active view */
291 return views[ACTIVE].run(key, btn, row, col);