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