2 * Copyright (C) 2012-2013 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/>.
28 static int line; // scroll offset
29 static int rows; // number of rows
31 static int srow; // selected row
32 static int cursor; // index of the selected row
33 static int items; // number of items displayed
34 static int first; // cursor on first item in group
35 static int last; // cursor on last item in group
37 static int show_new = 1;
38 static int show_started = 1;
39 static int show_finished = 0;
41 /* Helper functions */
42 static int print_todos(WINDOW *win, int row, todo_t *todos, status_t low, status_t high)
45 for (todo_t *cur = todos; cur; cur = cur->next) {
46 if (low <= cur->status && cur->status <= high) {
49 if (items++ == cursor) {
56 todo_line(win, cur, row+n++, 4, COLS-4, SHOW_DETAILS | SHOW_ACTIVE);
62 static int print_group(WINDOW *win, int row, todo_t *todos,
63 int show, const char *label, status_t low, status_t high)
69 mvwprintw(win, row, 0, "%s", label);
70 wattroff(win, A_BOLD);
74 n = print_todos(win, row+1, todos, low, high);
78 mvwprintw(win, row+1, 4, "[hidden]");
80 mvwprintw(win, row+1, 4, "[no tasks]");
82 return row+1+MAX(n,1)+1;
86 void todo_init(WINDOW *_win)
92 void todo_size(int rows, int cols)
104 row = print_group(win, row, TODOS,
105 show_new, "New Tasks", NEW, NEW);
107 row = print_group(win, row, TODOS,
108 show_started, "Started Tasks", NEW+1, DONE-1);
110 row = print_group(win, row, TODOS,
111 show_finished, "Finished Tasks", DONE, DONE);
117 int todo_run(int key, mmask_t btn, int row, int col)
119 int scroll = 0, move = 0, ref = 0;
122 case 'g': ref = 1; scroll = -line; break;
123 case 'G': ref = 1; scroll = rows; break;
124 case '\005': ref = 1; scroll = 1; break; // ctrl-e
125 case '\031': ref = 1; scroll = -1; break; // ctrl-y
126 case 'j': ref = 1; move = 1; break;
127 case 'k': ref = 1; move = -1; break;
128 case 'n': ref = 1; show_new ^= 1; break;
129 case 's': ref = 1; show_started ^= 1; break;
130 case 'f': ref = 1; show_finished ^= 1; break;
131 case 'e': view_edit(EDIT_TODO); return 1;
132 case '\012': view_edit(EDIT_TODO); return 1; // enter
135 /* Move more if we're on the edge of a group */
137 if (move < 0 && first) extra = -2;
138 if (move > 0 && last) extra = 2;
140 /* Scroll window when we move off screen */
141 int next = line + srow + move + extra;
142 int ymax = getmaxy(win)-1;
143 while (next-line < 0) line--;
144 while (next-line > ymax) line++;
146 /* Update line and cursor positions */
147 line = CLAMP(line+scroll, 0, rows-1);
148 cursor = CLAMP(cursor+move, 0, items-1);