]> Pileus Git - lackey/blob - src/screen.c
Move drawing code to screen.c, work on day 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 #define _XOPEN_SOURCE_EXTENDED
19
20 #include <string.h>
21 #include <ncurses.h>
22
23 #include "util.h"
24 #include "date.h"
25 #include "event.h"
26 #include "screen.h"
27
28 /* Types */
29 typedef struct {
30         char   *name;
31         void  (*init)(WINDOW*);
32         void  (*size)(int,int);
33         void  (*draw)(void);
34         int   (*run)(int,mmask_t,int,int);
35         int     keys[8];
36         WINDOW *win;
37 } view_t;
38
39 /* Data */
40 view_t views[] = {
41         { "Day",      day_init,      day_size,      day_draw,      day_run,      {KEY_F(1), '1',    } },
42         { "Week",     week_init,     week_size,     week_draw,     week_run,     {KEY_F(2), '2',    } },
43         { "Month",    month_init,    month_size,    month_draw,    month_run,    {KEY_F(3), '3',    } },
44         { "Year",     year_init,     year_size,     year_draw,     year_run,     {KEY_F(4), '4',    } },
45         { "|",        NULL,          NULL,          NULL,          NULL,         {                  } },
46         { "Todo",     todo_init,     todo_size,     todo_draw,     todo_run,     {KEY_F(5), '5',    } },
47         { "Notes",    notes_init,    notes_size,    notes_draw,    notes_run,    {KEY_F(6), '6',    } },
48         { "|",        NULL,          NULL,          NULL,          NULL,         {                  } },
49         { "Settings", settings_init, settings_size, settings_draw, settings_run, {KEY_F(7), '7',    } },
50         { "Help",     help_init,     help_size,     help_draw,     help_run,     {KEY_F(8), '8', '?'} },
51 };
52
53 int active = 0;
54
55 /* Local functions */
56 void draw_header(void)
57 {
58         move(0, 0);
59         attron(COLOR_PAIR(COLOR_TITLE));
60         for (int i = 0; i < N_ELEMENTS(views); i++) {
61                 if (i == active)
62                         attron(A_BOLD);
63                 printw("%s ", views[i].name);
64                 if (i == active)
65                         attroff(A_BOLD);
66         }
67         attroff(COLOR_PAIR(COLOR_TITLE));
68         mvhline(1, 0, ACS_HLINE, COLS);
69         refresh();
70 }
71
72 /* Helper functions */
73 void event_box(WINDOW *win, event_t *event, int y, int x, int h, int w)
74 {
75         int l = 0;
76         int s = y < 0 ? -y-1 : 0;
77
78         int color = event->cat == NULL           ? 0           :
79                     !strcmp(event->cat, "class") ? COLOR_CLASS :
80                     !strcmp(event->cat, "ec")    ? COLOR_EC    :
81                     !strcmp(event->cat, "work")  ? COLOR_WORK  : COLOR_OTHER ;
82
83         if (color) wattron(win, COLOR_PAIR(color));
84
85         if (h >= 2) mvwhline_set(win, y,     x+1,   WACS_T_HLINE, w-2);
86         if (h <= 1) mvwadd_wch(win,   y,     x,     WACS_BULLET);
87         if (h >= 2) mvwadd_wch(win,   y,     x,     WACS_T_ULCORNER);
88         if (h >= 2) mvwadd_wch(win,   y,     x+w-1, WACS_T_URCORNER);
89         if (h >= 3) mvwvline_set(win, y+1+s, x,     WACS_T_VLINE, h-2-s);
90         if (h >= 3) mvwvline_set(win, y+1+s, x+w-1, WACS_T_VLINE, h-2-s);
91         if (h >= 2) mvwadd_wch(win,   y+h-1, x,     WACS_T_LLCORNER);
92         if (h >= 2) mvwadd_wch(win,   y+h-1, x+w-1, WACS_T_LRCORNER);
93         if (h >= 2) mvwhline_set(win, y+h-1, x+1,   WACS_T_HLINE, w-2);
94
95         if (color) wattroff(win, COLOR_PAIR(color));
96
97         if (l<h && event->name) mvwprintw(win, y+l++, x+1, "%.*s",     w-2,      event->name);
98         if (l<h && event->loc)  mvwprintw(win, y+l++, x+1, "@ %-*.*s", w-4, w-4, event->loc);
99         if (l<h && event->desc) mvwprintw(win, y+l++, x+1, "%-*.*s",   w-2, w-2, event->desc);
100 }
101
102 void event_line(WINDOW *win, event_t *event, int y, int x, int w)
103 {
104         int color = event->cat == NULL           ? 0           :
105                     !strcmp(event->cat, "class") ? COLOR_CLASS :
106                     !strcmp(event->cat, "ec")    ? COLOR_EC    :
107                     !strcmp(event->cat, "work")  ? COLOR_WORK  : COLOR_OTHER ;
108
109         if (color) wattron(win, COLOR_PAIR(color));
110         mvwaddch(win, y, x+0, ACS_BLOCK);
111         if (color) wattroff(win, COLOR_PAIR(color));
112
113         mvwprintw(win, y, x+1, "%-*.*s", w-1, w-1, event->name);
114 }
115
116
117 /* Screen init */
118 void screen_init(void)
119 {
120         for (int i = 0; i < N_ELEMENTS(views); i++) {
121                 if (views[i].init) {
122                         views[i].win = newwin(LINES-2, COLS, 2, 0);
123                         views[i].init(views[i].win);
124                 }
125         }
126 }
127
128 /* Screen draw */
129 void screen_resize(void)
130 {
131         for (int i = 0; i < N_ELEMENTS(views); i++) {
132                 if (views[i].win)
133                         wresize(views[i].win, LINES-2, COLS);
134                 if (views[i].size)
135                         views[i].size(LINES-2, COLS);
136         }
137 }
138
139 /* Screen draw */
140 void screen_draw(void)
141 {
142         draw_header();
143         werase(views[active].win);
144         views[active].draw();
145         wrefresh(views[active].win);
146 }
147
148 /* Screen set */
149 int screen_set(int num)
150 {
151         if (active != num) {
152                 active = num;
153                 screen_draw();
154         }
155         return 1;
156 }
157
158 /* Screen run */
159 int screen_run(int key, mmask_t btn, int row, int col)
160 {
161         /* Check for mouse events */
162         if (key == KEY_MOUSE && row == 0) {
163                 int start = 1;
164                 for (int i = 0; i < N_ELEMENTS(views); i++) {
165                         int end = start + strlen(views[i].name) - 1;
166                         if (start <= col && col <= end && views[i].draw)
167                                 return screen_set(i);
168                         start = end + 2;
169                 }
170         }
171
172         /* Check for view change */
173         for (int i = 0; i < N_ELEMENTS(views); i++) {
174                 if (i == active)
175                         continue;
176                 for (int j = 0; j < N_ELEMENTS(views[i].keys); j++)
177                         if (views[i].keys[j] == key)
178                                 return screen_set(i);
179         }
180
181         /* Shift windows */
182         int num   = active;
183         int shift = key == KEY_RIGHT ? +1 :
184                     key == KEY_LEFT  ? -1 : 0;
185         while (shift) {
186                 num += shift;
187                 num += N_ELEMENTS(views);
188                 num %= N_ELEMENTS(views);
189                 if (views[num].run)
190                         return screen_set(num);
191         }
192
193         /* Pass key to active view */
194         return views[active].run(key, btn, row, col);
195 }