]> Pileus Git - lackey/blob - view/day.c
Move drawing code to screen.c, work on day view
[lackey] / view / day.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 "event.h"
26 #include "screen.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 /* Day init */
36 void day_init(WINDOW *_win)
37 {
38         win   = _win; //  lines  cols  y  x
39         head  = derwin(win,         1, COLS,   0, 0);
40         times = derwin(win, LINES-2-2,      5, 2, 0);
41         body  = derwin(win, LINES-2-2, COLS-6, 2, 6);
42         line  = 10*4; // 10:00
43 }
44
45 /* Day size */
46 void day_size(int rows, int cols)
47 {
48         wresize(head,       1, cols  );
49         wresize(times, rows-2,      5);
50         wresize(body,  rows-2, cols-6);
51 }
52
53 /* Day draw */
54 void day_draw(void)
55 {
56         const char *mstr = month_to_string(MONTH);
57         const char *dstr = day_to_string(day_of_week(YEAR, MONTH, DAY));
58
59         /* Print Header */
60         mvwprintw(head, 0, 0, "%s, %s %d", dstr, mstr, DAY+1);
61         mvwprintw(head, 0, COLS-10, "%d-%02d-%02d", YEAR, MONTH, DAY+1);
62
63         /* Print times */
64         mvwprintw(times, 0, 0, "%02d:%02d", ((line/4)-1)%12+1, (line*15)%60);
65         for (int h = 0; h < 24; h++)
66                 mvwprintw(times, h*4-line, 0, "%02d:%02d", (h-1)%12+1, 0);
67
68         /* Print events */
69         event_t *event = EVENTS;
70         for (int h = 0; h < 24; h++)
71         for (int m = 0; m < 60; m+=15)
72         while (event && before(&event->start,
73                         YEAR, MONTH, DAY, h+(m+15)/60, (m+15)%60)) {
74                 if (!before(&event->start, YEAR, MONTH, DAY, h, m)) {
75                         int y = h*4 + m/15 - line;
76                         int x = 0;
77                         int h = (get_mins(&event->start, &event->end)-1)/15+1;
78                         int w = COLS-6;
79                         event_box(body, event, y, x, h, w);
80                 }
81                 event = event->next;
82         }
83
84         /* Print lines */
85         mvwhline(win, 1, 0, ACS_HLINE, COLS);
86         mvwvline(win, 2, 5, ACS_VLINE, LINES-4);
87 }
88
89 /* Day run */
90 int day_run(int key, mmask_t btn, int row, int col)
91 {
92         int days = 0, ref = 0;
93         switch (key)
94         {
95                 case 'h': ref = 1; days = -1; break;
96                 case 'l': ref = 1; days =  1; break;
97                 case 'i': ref = 1; days = -7; break;
98                 case 'o': ref = 1; days =  7; break;
99                 case 'k': ref = 1; line--;    break;
100                 case 'j': ref = 1; line++;    break;
101         }
102         line = CLAMP(line, 0, 24*4);
103         if (days)
104                 add_days(&YEAR, &MONTH, &DAY, days);
105         if (ref) {
106                 werase(win);
107                 day_draw();
108                 wrefresh(win);
109         }
110         return 0;
111 }