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)
68 wattron(win, A_BOLD | A_UNDERLINE);
69 mvwprintw(win, row, 0, "%c", label[0]);
70 wattroff(win, A_UNDERLINE);
71 mvwprintw(win, row, 1, "%s", &label[1]);
72 wattroff(win, A_BOLD);
76 n = print_todos(win, row+1, todos, low, high);
80 mvwprintw(win, row+1, 4, "[hidden]");
82 mvwprintw(win, row+1, 4, "[no tasks]");
84 return row+1+MAX(n,1)+1;
88 void todo_init(WINDOW *_win)
94 void todo_size(int rows, int cols)
106 row = print_group(win, row, TODOS,
107 show_new, "New Tasks", NEW, NEW);
109 row = print_group(win, row, TODOS,
110 show_started, "Started Tasks", NEW+1, DONE-1);
112 row = print_group(win, row, TODOS,
113 show_finished, "Finished Tasks", DONE, DONE);
119 int todo_run(int key, mmask_t btn, int row, int col)
121 int scroll = 0, move = 0, ref = 0;
124 case 'g': ref = 1; scroll = -line; break;
125 case 'G': ref = 1; scroll = rows; break;
126 case '\005': ref = 1; scroll = 1; break; // ctrl-e
127 case '\031': ref = 1; scroll = -1; break; // ctrl-y
128 case 'j': ref = 1; move = 1; break;
129 case 'k': ref = 1; move = -1; break;
130 case 'n': ref = 1; show_new ^= 1; break;
131 case 's': ref = 1; show_started ^= 1; break;
132 case 'f': ref = 1; show_finished ^= 1; break;
133 case 'e': view_edit(EDIT_TODO); return 1;
134 case '\012': view_edit(EDIT_TODO); return 1; // enter
137 /* Move more if we're on the edge of a group */
139 if (move < 0 && first) extra = -2;
140 if (move > 0 && last) extra = 2;
142 /* Scroll window when we move off screen */
143 int next = line + srow + move + extra;
144 int ymax = getmaxy(win)-1;
145 while (next-line < 0) line--;
146 while (next-line > ymax) line++;
148 /* Update line and cursor positions */
149 line = CLAMP(line+scroll, 0, rows-1);
150 cursor = CLAMP(cursor+move, 0, items-1);