]> Pileus Git - lackey/blob - views/week.c
Add compact mode and view config data
[lackey] / views / week.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 "cal.h"
26 #include "view.h"
27
28 /* Static data */
29 static int     line;
30 static WINDOW *win;
31 static WINDOW *head;
32 static WINDOW *times;
33 static WINDOW *body;
34
35 /* Week init */
36 void week_init(WINDOW *_win)
37 {
38         win   = _win; //  lines  cols  y  x
39         head  = derwin(win,         2, COLS,   0, 0);
40         times = derwin(win, LINES-2-3,      5, 3, 0);
41         body  = derwin(win, LINES-2-3, COLS-6, 3, 6);
42         line  = 10*4; // 10:00
43 }
44
45 /* Week size */
46 void week_size(int rows, int cols)
47 {
48         int hdr = 3-COMPACT;
49         wresize(head,  2,        cols  );
50         wresize(times, rows-hdr,      5);
51         wresize(body,  rows-hdr, cols-6);
52         mvderwin(times, hdr, 0);
53         mvderwin(body,  hdr, 6);
54 }
55
56 /* Week draw */
57 void week_draw(void)
58 {
59         int x = 6;
60         int y = 3 - COMPACT;
61         const float hstep = (float)(COLS-x)/7.0;
62
63         /* Get start of week */
64         year_t  year  = YEAR;
65         month_t month = MONTH;
66         day_t   day   = DAY;
67         int shift = day_of_week(year, month, day);
68         add_days(&year, &month, &day, -shift);
69
70         /* For today */
71         int l = x+ROUND((shift+0)*hstep)-1;
72         int r = x+ROUND((shift+1)*hstep)-1;
73
74         /* Print Header */
75         int rev = COMPACT ? A_REVERSE | A_BOLD : 0;
76         wattron(head, rev);
77         mvwprintw(head, 0, 0, "%-*s",   COLS, month_to_str(MONTH));
78         mvwprintw(head, 1, 0, "%-0*d", COLS, YEAR);
79         wattroff(head, rev);
80         mvwhline(head,  0, l+1, ' ', r-l-1);
81         mvwhline(head,  1, l+1, ' ', r-l-1);
82         wattron(head, rev);
83         for (int d = 0; d < 7; d++) {
84                 const char *str = hstep >= 10 ? day_to_string(d) : day_to_str(d);
85                 if (d == shift) wattrset(head, A_BOLD);
86                 mvwprintw(head, 0, x+ROUND(d*hstep), "%s", str);
87                 mvwprintw(head, 1, x+ROUND(d*hstep), "%02d/%02d", month+1, day+1);
88                 if (d == shift) wattrset(head, rev);
89                 add_days(&year, &month, &day, 1);
90         }
91         wattroff(head, rev);
92
93         /* Print times */
94         mvwprintw(times, 0, 0, "%02d:%02d", ((line/4)-1)%12+1, (line*15)%60);
95         for (int h = 0; h < 24; h++)
96                 mvwprintw(times, h*4-line, 0, "%02d:%02d", (h-1)%12+1, 0);
97
98         /* Print events */
99         event_t *event = EVENTS;
100         add_days(&year, &month, &day, -7);
101         for (int d = 0; d <  7; d++, add_days(&year,&month,&day,1))
102         for (int h = 0; h < 24; h++)
103         for (int m = 0; m < 60; m+=15)
104         while (event && before(&event->start,
105                         year, month, day, h+(m+15)/60, (m+15)%60)) {
106                 if (!before(&event->start, year, month, day, h, m)) {
107                         int y = h*4 + m/15 - line;
108                         int x = ROUND(d*hstep);
109                         int h = (get_mins(&event->start, &event->end)-1)/15+1;
110                         int w = ROUND((d+1)*hstep) - 1 - x;
111                         event_box(body, event, y, x, h, w);
112                 }
113                 event = event->next;
114         }
115
116         /* Print lines */
117         if (!COMPACT)
118                 mvwhline(win, y-1, 0, ACS_HLINE, COLS);
119         for (int d = 0; d < 7; d++)
120                 mvwvline(win, y, x+ROUND(d*hstep)-1, ACS_VLINE, LINES-y-2+COMPACT);
121
122         /* Draw today */
123         if (!COMPACT)
124                 mvwhline(win, y-1, l, ACS_BLOCK, r-l+1);
125         mvwvline_set(win, y,   l, WACS_T_VLINE, LINES-y-2);
126         mvwvline_set(win, y,   r, WACS_T_VLINE, LINES-y-2);
127         for (int h = (line+3)/4; h < 24; h++) {
128                 mvwadd_wch(win, y+h*4-line, l, WACS_T_LTEE);
129                 mvwadd_wch(win, y+h*4-line, r, WACS_T_RTEE);
130         }
131 }
132
133 /* Week run */
134 int week_run(int key, mmask_t btn, int row, int col)
135 {
136         int days = 0, ref = 0;
137         switch (key)
138         {
139                 case 'h': ref = 1; days = -1; break;
140                 case 'l': ref = 1; days =  1; break;
141                 case 'i': ref = 1; days = -7; break;
142                 case 'o': ref = 1; days =  7; break;
143                 case 'k': ref = 1; line--;    break;
144                 case 'j': ref = 1; line++;    break;
145         }
146         line = CLAMP(line, 0, 24*4);
147         if (days)
148                 add_days(&YEAR, &MONTH, &DAY, days);
149         if (ref) {
150                 werase(win);
151                 week_draw();
152                 wrefresh(win);
153         }
154         return ref;
155 }