]> Pileus Git - lackey/blob - views/todo.c
Add bit flags for event and todo line items
[lackey] / views / todo.c
1 /*
2  * Copyright (C) 2012-2013 Andy Spencer <andy753421@gmail.com>
3  *
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.
8  *
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.
13  *
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/>.
16  */
17
18 #include <ncurses.h>
19
20 #include "util.h"
21 #include "date.h"
22 #include "cal.h"
23 #include "view.h"
24
25 /* Static data */
26 static WINDOW *win;
27 static int     line;
28 static int     rows;
29
30 static int show_new      = 1;
31 static int show_started  = 1;
32 static int show_finished = 0;
33
34 /* Helper functions */
35 static int print_todos(WINDOW *win, int row, todo_t *todos, status_t low, status_t high)
36 {
37         int n = 0;
38         for (todo_t *cur = todos; cur; cur = cur->next)
39                 if (low <= cur->status && cur->status <= high)
40                         todo_line(win, cur, row+n++, 4, COLS-4, SHOW_DETAILS);
41         return n;
42 }
43
44 static int print_group(WINDOW *win, int row, todo_t *todos,
45                 int show, const char *label, status_t low, status_t high)
46 {
47         int n = 1;
48
49         /* Label */
50         wattron(win, A_BOLD);
51         mvwprintw(win, row, 0, "%s", label);
52         wattroff(win, A_BOLD);
53
54         /* Todos */
55         if (show)
56                 n = print_todos(win, row+1, todos, low, high);
57
58         /* Status */
59         if (!show)
60                 mvwprintw(win, row+1, 4, "[hidden]");
61         if (n == 0)
62                 mvwprintw(win, row+1, 4, "[no tasks]");
63
64         return row+1+MAX(n,1)+1;
65 }
66
67 /* Todo init */
68 void todo_init(WINDOW *_win)
69 {
70         win = _win;
71 }
72
73 /* Todo size */
74 void todo_size(int rows, int cols)
75 {
76 }
77
78 /* Todo draw */
79 void todo_draw(void)
80 {
81         int row = -line;
82
83         row = print_group(win, row, TODOS,
84                 show_new, "New Tasks", NEW, NEW);
85
86         row = print_group(win, row, TODOS,
87                 show_started, "Started Tasks", NEW+1, DONE-1);
88
89         row = print_group(win, row, TODOS,
90                 show_finished, "Finished Tasks", DONE, DONE);
91
92         rows = row+line-1;
93 }
94
95 /* Todo run */
96 int todo_run(int key, mmask_t btn, int row, int col)
97 {
98         int scroll = 0, ref = 0;
99         switch (key)
100         {
101                 case 'g': ref = 1; scroll = -line;    break;
102                 case 'G': ref = 1; scroll =  rows;    break;
103                 case 'j': ref = 1; scroll =  1;       break;
104                 case 'k': ref = 1; scroll = -1;       break;
105                 case 'n': ref = 1; show_new      ^= 1; break;
106                 case 's': ref = 1; show_started  ^= 1; break;
107                 case 'f': ref = 1; show_finished ^= 1; break;
108         }
109         line = CLAMP(line+scroll, 0, rows-1);
110         if (ref) {
111                 werase(win);
112                 todo_draw();
113                 wrefresh(win);
114         }
115         return ref;
116 }