]> Pileus Git - lackey/blob - views/week.c
18cc362739537b8c2730c27d5277b31cd80590b2
[lackey] / views / week.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 <ncurses.h>
21
22 #include "util.h"
23 #include "date.h"
24 #include "cal.h"
25 #include "view.h"
26
27 /* From day.c */
28 int get_col(event_t **list, int n, event_t *event, int *ncols);
29 void move_event(int events, int days, int *line);
30
31 /* Static data */
32 static int     line;
33 static WINDOW *win;
34 static WINDOW *times;
35 static WINDOW *body;
36
37 /* Week init */
38 void week_init(WINDOW *_win)
39 {
40         win   = _win; //    lines    cols    y  x
41         times = derwin(win, LINES-2,      5, 0, 0);
42         body  = derwin(win, LINES-2, COLS-5, 0, 5);
43         line  = MORNING*4;
44 }
45
46 /* Week size */
47 void week_size(int rows, int cols)
48 {
49         int hdr = 3-COMPACT;
50         wmvresize(times, hdr, 0, rows-hdr,      5);
51         wmvresize(body,  hdr, 5, rows-hdr, cols-5);
52 }
53
54 /* Week draw */
55 void week_draw(void)
56 {
57         int x = 6;
58         int y = 3 - COMPACT;
59         const float hstep = (float)(COLS-x)/7.0;
60         int ex, ey, ew, eh;
61         event_t *event, *ee;
62
63         /* Get start of week */
64         year_t  year  = SEL.year;
65         month_t month = SEL.month;
66         day_t   day   = SEL.day;
67         int shift = day_of_week(year, month, day);
68         add_days(&year, &month, &day, -shift);
69
70         /* Load cal data */
71         cal_load(year, month, day, 7);
72
73         /* For today */
74         int l = ROUND((shift+0)*hstep);
75         int r = ROUND((shift+1)*hstep);
76
77         /* Print Header */
78         int rev = COMPACT ? A_REVERSE | A_BOLD : 0;
79         wattron(win, rev);
80         mvwprintw(win, 0, 0, "%-*s",  COLS, month_to_str(SEL.month));
81         mvwprintw(win, 1, 0, "%-0*d", COLS, SEL.year);
82         wattroff(win, rev);
83         mvwhline(win, 0, x+l, ' ', r-l-1);
84         mvwhline(win, 1, x+l, ' ', r-l-1);
85         wattron(win, rev);
86         for (int d = 0; d < 7; d++) {
87                 const char *str = hstep >= 10 ? day_to_string(d) : day_to_str(d);
88                 if (d == shift) wattrset(win, A_BOLD);
89                 mvwprintw(win, 0, x+ROUND(d*hstep), "%s", str);
90                 mvwprintw(win, 1, x+ROUND(d*hstep), "%02d/%02d", month+1, day+1);
91                 if (d == shift) wattrset(win, rev);
92                 add_days(&year, &month, &day, 1);
93         }
94         wattroff(win, rev);
95
96         /* Print all day events */
97         int allday = 0;
98         event = EVENTS;
99         add_days(&year, &month, &day, -7);
100         for (int d = 0; d <  7; d++) {
101                 int n = 0;
102                 while (event && before(&event->start, year, month, day, 24, 0)) {
103                         if (!before(&event->end, year, month, day, 0, 1) &&
104                             get_mins(&event->start, &event->end) > 23*60) {
105                                 int s = ROUND(d*hstep);
106                                 int w = ROUND((d+1)*hstep) - 1 - s;
107                                 event_line(win, event, y+n++, x+s, w, SHOW_ACTIVE);
108                         }
109                         event = event->next;
110                         if (n > allday)
111                                 allday = n;
112                 }
113                 add_days(&year,&month,&day,1);
114         }
115         if (allday && !COMPACT)
116                 allday++;
117
118         /* Resize body */
119         wshrink(times, y+allday-!COMPACT);
120         wshrink(body,  y+allday-!COMPACT);
121
122         /* Print times */
123         mvwprintw(times, !COMPACT, 0, "%02d:%02d", ((line/4)-1)%12+1, (line*15)%60);
124         for (int h = 0; h < 24; h++)
125                 mvwprintw(times, !COMPACT+h*4-line, 0, "%02d:%02d", (h-1)%12+1, 0);
126
127         /* Print events */
128         if (!EVENT)
129                 EVENT = find_event(&SEL);
130         ee = NULL;
131         event = EVENTS;
132         add_days(&year, &month, &day, -7);
133         event_t *active[5] = {};
134         int ncols = 0;
135         for (int d = 0; d <  7; d++, add_days(&year,&month,&day,1))
136         for (int h = 0; h < 24; h++)
137         for (int m = 0; m < 60; m+=15)
138         while (event && before(&event->start,
139                         year, month, day, h+(m+15)/60, (m+15)%60)) {
140                 if (!before(&event->start, year, month, day, h, m) &&
141                     get_mins(&event->start, &event->end) <= 23*60) {
142                         int col = get_col(active, N_ELEMENTS(active), event, &ncols);
143                         int y = h*4 + m/15 - line + !COMPACT;
144                         int x = ROUND(d*hstep) + 1 + (col*3);
145                         int h = (get_mins(&event->start, &event->end)-1)/15+1;
146                         int w = ROUND((d+1)*hstep) - x - ((ncols-1)-col)*3;
147                         if (event == EVENT) {
148                                 ee = event; ex = x; ey = y; ew = w; eh = h;
149                         } else
150                                 event_box(body, event, y, x, h, w);
151                 }
152                 event = event->next;
153         }
154
155         /* Draw current event on top of other events */
156         if (ee)
157                 event_box(body, ee, ey, ex, eh, ew);
158
159         /* Print header lines */
160         if (!COMPACT)
161                 mvwhline(win, y-1, 0, ACS_HLINE, COLS);
162         if (!COMPACT && allday)
163                 mvwhline(win, y-1+allday, 0, ACS_HLINE, COLS);
164
165         /* Print day lines */
166         for (int d = 0; d < 7; d++)
167                 mvwvline(body, !COMPACT, ROUND(d*hstep), ACS_VLINE, LINES-y-2+COMPACT-allday);
168         mvwvline_set(body, 0, l, WACS_T_VLINE, LINES-y-1+COMPACT);
169         mvwvline_set(body, 0, r, WACS_T_VLINE, LINES-y-1+COMPACT);
170         for (int h = (line+3)/4; h < 24; h++) {
171                 mvwadd_wch(body, h*4-line+!COMPACT, l, WACS_T_LTEE);
172                 mvwadd_wch(body, h*4-line+!COMPACT, r, WACS_T_RTEE);
173         }
174         if (!COMPACT)
175                 mvwhline(body, 0, l, ACS_BLOCK, r-l+1);
176 }
177
178 /* Week run */
179 int week_run(int key, mmask_t btn, int row, int col)
180 {
181         int days = 0, events = 0, lines = 0;
182         switch (key) {
183                 case 'h':    days   = -1; break;
184                 case 'l':    days   =  1; break;
185                 case 'i':    days   = -7; break;
186                 case 'o':    days   =  7; break;
187                 case 'k':    events = -1; break;
188                 case 'j':    events =  1; break;
189                 case '\031': lines  = -1; break; // ctrl-y
190                 case '\005': lines  =  1; break; // ctrl-e
191                 case '\012': view_edit(EDIT_EVENT); return 1; // enter
192                 default:     return 0; // not found
193         }
194
195         if (lines)
196                 line = CLAMP(line+lines, 0, 24*4);
197         if (days || events)
198                 move_event(events, days, &line);
199
200         werase(win);
201         week_draw();
202         wrefresh(win);
203         return 1;
204 }