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