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', '?'} },
56 static void draw_header(void)
59 attron(COLOR_PAIR(COLOR_TITLE));
60 for (int i = 0; i < N_ELEMENTS(views); i++) {
63 printw("%s ", views[i].name);
67 attroff(COLOR_PAIR(COLOR_TITLE));
68 mvhline(1, 0, ACS_HLINE, COLS);
72 static int get_color(const char *cat)
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 ;
80 /* Helper functions */
81 void event_box(WINDOW *win, event_t *event, int y, int x, int h, int w)
84 int s = y < 0 ? -y-1 : 0;
86 int color = get_color(event->cat);
88 if (color) wattron(win, COLOR_PAIR(color));
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);
100 if (color) wattroff(win, COLOR_PAIR(color));
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);
107 void event_line(WINDOW *win, event_t *event, int y, int x, int w, int full)
109 int color = get_color(event->cat);
111 if (color) wattron(win, COLOR_PAIR(color));
112 mvwaddch(win, y, x++, ACS_BLOCK);
113 if (color) wattroff(win, COLOR_PAIR(color));
116 mvwprintw(win, y, x, " %02d:%02d - ", event->start.hour, event->start.min);
120 const char *label = event->name ?: event->desc;
121 mvwprintw(win, y, x, "%-*.*s", w-1, w-1, label);
122 x += MIN(strlen(label), w-1);
124 if (full && event->loc) {
125 mvwprintw(win, y, x, " @ %s", event->loc);
129 void todo_line(WINDOW *win, todo_t *todo, int y, int x, int w, int full)
132 sprintf(perc, "%2d%%", todo->status);
134 int color = get_color(todo->cat);
135 if (color) wattron(win, COLOR_PAIR(color));
136 mvwaddch(win, y, 2, ACS_BLOCK);
137 if (color) wattroff(win, COLOR_PAIR(color));
139 mvwprintw(win, y, 4, "%04d-%02d-%02d %2d:%02d",
140 todo->due.year, todo->due.month+1, todo->due.day+1,
141 todo->due.hour, todo->due.min);
142 mvwprintw(win, y, 22, "%s",
143 todo->status == NEW ? "new" :
144 todo->status == DONE ? "done" : perc);
145 mvwprintw(win, y, 30, "%s: %s", todo->name, todo->desc);
151 for (int i = 0; i < N_ELEMENTS(views); i++) {
153 views[i].win = newwin(LINES-2, COLS, 2, 0);
154 views[i].init(views[i].win);
160 void view_resize(void)
162 for (int i = 0; i < N_ELEMENTS(views); i++) {
164 wresize(views[i].win, LINES-2, COLS);
166 views[i].size(LINES-2, COLS);
174 werase(views[active].win);
175 views[active].draw();
176 wrefresh(views[active].win);
180 int view_set(int num)
190 int view_run(int key, mmask_t btn, int row, int col)
192 /* Check for mouse events */
193 if (key == KEY_MOUSE && row == 0) {
195 for (int i = 0; i < N_ELEMENTS(views); i++) {
196 int end = start + strlen(views[i].name) - 1;
197 if (start <= col && col <= end && views[i].draw)
203 /* Check for view change */
204 for (int i = 0; i < N_ELEMENTS(views); i++) {
207 for (int j = 0; j < N_ELEMENTS(views[i].keys); j++)
208 if (views[i].keys[j] == key)
214 int shift = key == KEY_RIGHT ? +1 :
215 key == KEY_LEFT ? -1 : 0;
218 num += N_ELEMENTS(views);
219 num %= N_ELEMENTS(views);
221 return view_set(num);
224 /* Pass key to active view */
225 return views[active].run(key, btn, row, col);