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