]> Pileus Git - lackey/blob - src/screen.c
Add month view
[lackey] / src / screen.c
1 /*
2  * Copyright (C) 2012 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 <string.h>
19 #include <ncurses.h>
20
21 #include "util.h"
22 #include "date.h"
23 #include "screen.h"
24
25 /* Types */
26 typedef struct {
27         char   *name;
28         void  (*init)(WINDOW*);
29         void  (*size)(int,int);
30         void  (*draw)(void);
31         int   (*run)(int,mmask_t,int,int);
32         int     keys[8];
33         WINDOW *win;
34 } view_t;
35
36 /* Data */
37 view_t views[] = {
38         { "Day",      day_init,      day_size,      day_draw,      day_run,      {KEY_F(1), '1',    } },
39         { "Week",     week_init,     week_size,     week_draw,     week_run,     {KEY_F(2), '2',    } },
40         { "Month",    month_init,    month_size,    month_draw,    month_run,    {KEY_F(3), '3',    } },
41         { "Year",     year_init,     year_size,     year_draw,     year_run,     {KEY_F(4), '4',    } },
42         { "|",        NULL,          NULL,          NULL,          NULL,         {                  } },
43         { "Todo",     todo_init,     todo_size,     todo_draw,     todo_run,     {KEY_F(5), '5',    } },
44         { "Notes",    notes_init,    notes_size,    notes_draw,    notes_run,    {KEY_F(6), '6',    } },
45         { "|",        NULL,          NULL,          NULL,          NULL,         {                  } },
46         { "Settings", settings_init, settings_size, settings_draw, settings_run, {KEY_F(7), '7',    } },
47         { "Help",     help_init,     help_size,     help_draw,     help_run,     {KEY_F(8), '8', '?'} },
48 };
49
50 int active = 2;
51
52 /* Local functions */
53 void draw_header(void)
54 {
55         move(0, 0);
56         attron(COLOR_PAIR(COLOR_TITLE));
57         for (int i = 0; i < N_ELEMENTS(views); i++) {
58                 if (i == active)
59                         attron(A_BOLD);
60                 printw("%s ", views[i].name);
61                 if (i == active)
62                         attroff(A_BOLD);
63         }
64         attroff(COLOR_PAIR(COLOR_TITLE));
65         mvhline(1, 0, ACS_HLINE, COLS);
66         refresh();
67 }
68
69 /* Screen init */
70 void screen_init(void)
71 {
72         for (int i = 0; i < N_ELEMENTS(views); i++) {
73                 if (views[i].init) {
74                         views[i].win = newwin(LINES-2, COLS, 2, 0);
75                         views[i].init(views[i].win);
76                 }
77         }
78 }
79
80 /* Screen draw */
81 void screen_resize(void)
82 {
83         for (int i = 0; i < N_ELEMENTS(views); i++) {
84                 if (views[i].win)
85                         wresize(views[i].win, LINES-2, COLS);
86                 if (views[i].size)
87                         views[i].size(LINES-2, COLS);
88         }
89 }
90
91 /* Screen draw */
92 void screen_draw(void)
93 {
94         draw_header();
95         werase(views[active].win);
96         views[active].draw();
97         wrefresh(views[active].win);
98 }
99
100 /* Screen set */
101 int screen_set(int num)
102 {
103         if (active != num) {
104                 active = num;
105                 screen_draw();
106         }
107         return 1;
108 }
109
110 /* Screen run */
111 int screen_run(int key, mmask_t btn, int row, int col)
112 {
113         /* Check for mouse events */
114         if (key == KEY_MOUSE && row == 0) {
115                 int start = 1;
116                 for (int i = 0; i < N_ELEMENTS(views); i++) {
117                         int end = start + strlen(views[i].name) - 1;
118                         if (start <= col && col <= end && views[i].draw)
119                                 return screen_set(i);
120                         start = end + 2;
121                 }
122         }
123
124         /* Check for view change */
125         for (int i = 0; i < N_ELEMENTS(views); i++) {
126                 if (i == active)
127                         continue;
128                 for (int j = 0; j < N_ELEMENTS(views[i].keys); j++)
129                         if (views[i].keys[j] == key)
130                                 return screen_set(i);
131         }
132
133         /* Shift windows */
134         int num   = active;
135         int shift = key == KEY_RIGHT ? +1 :
136                     key == KEY_LEFT  ? -1 : 0;
137         while (shift) {
138                 num += shift;
139                 num += N_ELEMENTS(views);
140                 num %= N_ELEMENTS(views);
141                 if (views[num].run)
142                         return screen_set(num);
143         }
144
145         /* Pass key to active view */
146         return views[active].run(key, btn, row, col);
147 }