]> Pileus Git - lackey/blob - views/month.c
Convert YEAR/MONTH/DAY to Selection struct
[lackey] / views / month.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 <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 WINDOW *win;
30
31 /* Month init */
32 void month_init(WINDOW *_win)
33 {
34         win = _win;
35 }
36
37 /* Month size */
38 void month_size(int rows, int cols)
39 {
40 }
41
42 /* Month draw */
43 void month_draw(void)
44 {
45         const char *name  = month_to_string(SEL.month);
46         const int   start = start_of_month(SEL.year, SEL.month);
47         const int   days  = days_in_month(SEL.year, SEL.month);
48         const int   weeks = weeks_in_month(SEL.year, SEL.month);
49         const int   hdr   = COMPACT ? 3 : 4;
50         const float midpt = (float)COLS/2.0 - (strlen(name) + 1 + 4)/2.0;
51         const float hstep = (float)(COLS-1)/7.0;
52         const float vstep = (float)(LINES-2-hdr+COMPACT)/weeks;
53
54         /* Load cal data */
55         cal_load(SEL.year, SEL.month, 0, days);
56
57         /* Print Header */
58         if (COMPACT) wattron(win, A_REVERSE | A_BOLD);
59         if (COMPACT) mvwhline(win, 0, 0, ' ' | A_REVERSE | A_BOLD, COLS);
60         if (COMPACT) mvwhline(win, 1, 0, ' ' | A_REVERSE | A_BOLD, COLS);
61         mvwprintw(win, 0, midpt, "%s %d", name, SEL.year);
62         for (int d = 0; d < 7; d++) {
63                 const char *str = hstep >= 10 ? day_to_string(d+SUN) : day_to_str(d+SUN);
64                 mvwprintw(win, 1, ROUND(1+d*hstep), "%s", str);
65         }
66         if (COMPACT)  wattroff(win, A_REVERSE | A_BOLD);
67         if (!COMPACT) mvwhline(win, 2, 0, ACS_HLINE, COLS);
68
69         /* Print days */
70         for (int d = 0; d < days; d++) {
71                 int row = (start + d) / 7;
72                 int col = (start + d) % 7;
73                 if (d == SEL.day) wattron(win, A_BOLD);
74                 mvwprintw(win, ROUND(hdr+row*vstep), ROUND(1+col*hstep), "%d", d+1);
75                 if (d == SEL.day) wattroff(win, A_BOLD);
76         }
77
78         /* Print events */
79         event_t *event = EVENTS;
80         for (int d = 0; d < days; d++) {
81                 int y = ROUND(hdr+(((start + d) / 7)  )*vstep);
82                 int e = ROUND(hdr+(((start + d) / 7)+1)*vstep)-2;
83                 int x = ROUND(1  +(((start + d) % 7)  )*hstep)+3;
84                 int w = ROUND(1  +(((start + d) % 7)+1)*hstep)-x-1;
85                 while (event && before(&event->start, SEL.year, SEL.month, d, 24, 0)) {
86                         if (!before(&event->start, SEL.year, SEL.month, d, 0, 0)){
87                                 if (y == e) mvwhline(win, y, x-3, ACS_DARROW, 2);
88                                 if (y <= e) event_line(win, event, y, x, w, 0);
89                                 y++;
90                         }
91                         event = event->next;
92                 }
93         }
94
95         /* Print lines */
96         for (int w = 1; w < weeks; w++)
97                 mvwhline(win, ROUND(hdr-1+w*vstep), 1, ACS_HLINE, COLS-2);
98         for (int d = 1; d < 7; d++) {
99                 int top = d >=  start             ? 0     : 1;
100                 int bot = d <= (start+days-1)%7+1 ? weeks : weeks-1;
101                 mvwvline(win, ROUND(hdr+top*vstep), ROUND(d*hstep),
102                                 ACS_VLINE, (bot-top)*vstep);
103                 for (int w = 1; w < weeks; w++) {
104                         int chr = w == top ? ACS_TTEE :
105                                   w == bot ? ACS_BTEE : ACS_PLUS;
106                         mvwaddch(win, ROUND(hdr-1+w*vstep), ROUND(d*hstep), chr);
107                 }
108         }
109
110         /* Draw today */
111         int col = day_of_week(SEL.year, SEL.month, SEL.day);
112         int row = (start+SEL.day) / 7;
113         int l = ROUND((col+0)*hstep);
114         int r = ROUND((col+1)*hstep);
115         int t = ROUND((row+0)*vstep+hdr-1);
116         int b = ROUND((row+1)*vstep+hdr-1);
117         mvwvline_set(win, t, l, WACS_T_VLINE, b-t);
118         mvwvline_set(win, t, r, WACS_T_VLINE, b-t);
119         mvwhline_set(win, t, l, WACS_T_HLINE, r-l);
120         mvwhline_set(win, b, l, WACS_T_HLINE, r-l);
121         mvwadd_wch(win, t, l, WACS_T_ULCORNER);
122         mvwadd_wch(win, t, r, WACS_T_URCORNER);
123         mvwadd_wch(win, b, l, WACS_T_LLCORNER);
124         mvwadd_wch(win, b, r, WACS_T_LRCORNER);
125 }
126
127 /* Month run */
128 int month_run(int key, mmask_t btn, int row, int col)
129 {
130         int days = 0, months = 0;
131         switch (key)
132         {
133                 case 'h': days   = -1; break;
134                 case 'j': days   =  7; break;
135                 case 'k': days   = -7; break;
136                 case 'l': days   =  1; break;
137                 case 'i': months = -1; break;
138                 case 'o': months =  1; break;
139         }
140         if (days || months) {
141                 add_days(&SEL.year, &SEL.month, &SEL.day, days);
142                 add_months(&SEL.year, &SEL.month, months);
143                 werase(win);
144                 month_draw();
145                 wrefresh(win);
146         }
147         return days || months;
148 }