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