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