]> Pileus Git - lackey/blob - views/day.c
Move e key to views
[lackey] / views / day.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 #define _XOPEN_SOURCE_EXTENDED
19
20 #include <string.h>
21 #include <ncurses.h>
22
23 #include "util.h"
24 #include "date.h"
25 #include "cal.h"
26 #include "view.h"
27
28 /* Static data */
29 static int     line;
30 static WINDOW *win;
31 static WINDOW *times;
32 static WINDOW *body;
33
34 /* Box packing helpers */
35 static void clear_old(event_t **list, int n, event_t *cur)
36 {
37         for (int i = 0; i < n; i++)
38                 if (list[i] && compare(&list[i]->end, &cur->start) <= 0)
39                         list[i] = NULL;
40 }
41
42 static int next_free(event_t **list, int n)
43 {
44         for (int i = 0; i < n; i++)
45                 if (!list[i])
46                         return i;
47         return n-1;
48 }
49
50 static int count_busy(event_t **list, int n)
51 {
52         int sum = 0;
53         for (int i = 0; i < n; i++)
54                 if (list[i])
55                         sum++;
56         return sum;
57 }
58
59 static int get_ncols(event_t *event, int n)
60 {
61         int ncols = 0;
62         event_t *preview[n];
63         memset(preview, 0, sizeof(preview));
64         for (event_t *cur = event; cur; cur = cur->next) {
65                 int col = next_free(preview, n);
66                 preview[col] = cur;
67                 ncols = MAX(ncols, col+1);
68                 if (cur->next)
69                         clear_old(preview, n, cur->next);
70                 if (count_busy(preview, n) == 0)
71                         break;
72         }
73         return ncols;
74 }
75
76 int get_col(event_t **list, int n, event_t *event, int *ncols)
77 {
78         clear_old(list, n, event);
79
80         /* If there are current events, then recalculate
81          * ncols for the next series of events */
82         if (count_busy(list, n) == 0)
83                 *ncols = get_ncols(event, n);
84
85         /* Find next open slot */
86         int col = next_free(list, n);
87         list[col] = event;
88         return col;
89 }
90
91 /* Event setting helper functions */
92 void move_event(int events, int days, int *line)
93 {
94         /* Move forward/back in event list */
95         if (events && EVENT) {
96                 for (int i=0; i<events && EVENT->next &&
97                                 same_day(&EVENT->start, &EVENT->next->start); i++)
98                         EVENT = EVENT->next;
99                 for (int i=0; i>events && EVENT->next &&
100                                 same_day(&EVENT->start, &EVENT->prev->start); i--)
101                         EVENT = EVENT->prev;
102         }
103
104         /* Set current event */
105         if (days) {
106                 add_days(&SEL.year, &SEL.month, &SEL.day, days);
107                 date_t target = SEL;
108                 if (EVENT) {
109                         target.hour = EVENT->start.hour;
110                         target.min  = EVENT->start.min;
111                         target.sec  = EVENT->start.sec;
112                 }
113                 if ((EVENT = find_event(&target)))
114                         SEL = EVENT->start;
115         }
116
117         /* Update line */
118         if (line && EVENT && !all_day(&EVENT->start, &EVENT->end)) {
119                 int lines = LINES-4+COMPACT+COMPACT;
120                 int begin = EVENT->start.hour *  4
121                           + EVENT->start.min  / 15;
122                 if (begin+4 > *line+lines)
123                         *line = begin-lines+4;
124                 if (begin < *line)
125                         *line = begin;
126         }
127 }
128
129 /* Day init */
130 void day_init(WINDOW *_win)
131 {
132         win   = _win; //    lines    cols    y  x
133         times = derwin(win, LINES-2,      5, 0, 0);
134         body  = derwin(win, LINES-2, COLS-5, 0, 5);
135         line  = MORNING*4;
136 }
137
138 /* Day size */
139 void day_size(int rows, int cols)
140 {
141         int hdr = 2-COMPACT;
142         wmvresize(times, hdr, 0, rows-hdr,      5);
143         wmvresize(body,  hdr, 5, rows-hdr, cols-5);
144 }
145
146 /* Day draw */
147 void day_draw(void)
148 {
149         const char *mstr = month_to_string(SEL.month);
150         const char *dstr = day_to_string(day_of_week(SEL.year, SEL.month, SEL.day));
151
152         int y = !COMPACT+1;
153         event_t *event;
154
155         /* Load cal data */
156         cal_load(SEL.year, SEL.month, SEL.day, 1);
157
158         /* Print Header */
159         if (COMPACT) wattron(win, A_REVERSE | A_BOLD);
160         mvwhline(win, 0, 0, ' ', COLS);
161         mvwprintw(win, 0, 0, "%s, %s %d", dstr, mstr, SEL.day+1);
162         mvwprintw(win, 0, COLS-10, "%d-%02d-%02d", SEL.year, SEL.month+1, SEL.day+1);
163         if (COMPACT) wattroff(win, A_REVERSE | A_BOLD);
164
165         /* Print all day events */
166         event = EVENTS;
167         int allday = 0;
168         while (event && before(&event->start, SEL.year, SEL.month, SEL.day, 24, 0)) {
169                 if (!before(&event->end, SEL.year, SEL.month, SEL.day, 0, 1) &&
170                     get_mins(&event->start, &event->end) > 23*60)
171                         event_line(win, event, y+allday++, 6, COLS-6, SHOW_ACTIVE | SHOW_DETAILS);
172                 event = event->next;
173         }
174         if (allday && !COMPACT)
175                 allday++;
176
177         /* Resize body */
178         wshrink(times, y+allday);
179         wshrink(body,  y+allday);
180
181         /* Print times */
182         mvwprintw(times, 0, 0, "%02d:%02d", ((line/4)-1)%12+1, (line*15)%60);
183         for (int h = 0; h < 24; h++)
184                 mvwprintw(times, h*4-line, 0, "%02d:%02d", (h-1)%12+1, 0);
185
186         /* Print events */
187         if (!EVENT)
188                 EVENT = find_event(&SEL);
189         event = EVENTS;
190         event_t *active[10] = {};
191         int ncols = 0;
192         for (int h = 0; h < 24; h++)
193         for (int m = 0; m < 60; m+=15)
194         while (event && before(&event->start,
195                         SEL.year, SEL.month, SEL.day, h+(m+15)/60, (m+15)%60)) {
196                 if (!before(&event->start, SEL.year, SEL.month, SEL.day, h, m) &&
197                     get_mins(&event->start, &event->end) <= 23*60) {
198                         int col    = get_col(active, N_ELEMENTS(active), event, &ncols);
199                         int left   = ROUND((col+0.0)*(COLS-6)/ncols) + 1;
200                         int right  = ROUND((col+1.0)*(COLS-6)/ncols) + 1;
201                         int row    = h*4 + m/15 - line;
202                         int height = (get_mins(&event->start, &event->end)-1)/15+1;
203                         event_box(body, event, row, left, height, right-left);
204                 }
205                 event = event->next;
206         }
207
208         /* Print lines */
209         if (!COMPACT)
210                 mvwhline(win, 1, 0, ACS_HLINE, COLS);
211         if (!COMPACT && allday)
212                 mvwhline(win, allday+1, 0, ACS_HLINE, COLS);
213         mvwvline(body, 0, 0, ACS_VLINE, LINES-4+COMPACT+COMPACT);
214 }
215
216 /* Day run */
217 int day_run(int key, mmask_t btn, int row, int col)
218 {
219         int days = 0, events = 0, lines = 0;
220         switch (key) {
221                 case 'h':    days   = -1; break;
222                 case 'l':    days   =  1; break;
223                 case 'i':    days   = -7; break;
224                 case 'o':    days   =  7; break;
225                 case 'k':    events = -1; break;
226                 case 'j':    events =  1; break;
227                 case '\031': lines  = -1; break; // ctrl-y
228                 case '\005': lines  =  1; break; // ctrl-e
229                 case 'e':    view_edit(EDIT_EVENT); return 1;
230                 case '\012': view_edit(EDIT_EVENT); return 1; // enter
231                 default:     return 0; // not found
232         }
233
234         if (lines)
235                 line = CLAMP(line+lines, 0, 24*4);
236         if (days || events)
237                 move_event(events, days, &line);
238
239         werase(win);
240         day_draw();
241         wrefresh(win);
242         return 1;
243 }