]> Pileus Git - lackey/blob - src/view/month.c
d5bb538bf95e0607e8927cf7521e27895fbb90dc
[lackey] / src / view / month.c
1 #include <string.h>
2 #include <ncurses.h>
3
4 #include "util.h"
5
6 /* Macros */
7 #define ROUND(x) ((int)((x)+0.5))
8
9 /* Static data */
10 static WINDOW *win;
11
12 /* Test data */
13 const static int YEAR  = 2012;
14 const static int MONTH = SEP;
15 const static int DAY   = 29;
16
17 /* Month init */
18 void month_init(WINDOW *_win)
19 {
20         win = _win;
21 }
22
23 /* Month draw */
24 void month_draw(void)
25 {
26         const char *name  = month_to_string(MONTH);
27         const int   start = start_of_month(YEAR, MONTH);
28         const int   days  = days_in_month(YEAR, MONTH);
29         const int   weeks = weeks_in_month(YEAR, MONTH);
30         const float midpt = (float)COLS/2.0 - (strlen(name) + 1 + 4)/2.0;
31         const float hstep = (float)COLS/7.0;
32         const float vstep = (float)(LINES-4)/weeks;
33
34         /* Print Header */
35         mvwprintw(win, 0, midpt, "%s %d", name, YEAR);
36         for (int d = 0; d < 7; d++)
37                 mvwprintw(win, 1, ROUND(d*hstep), "%s", day_to_str(d+SUN));
38         mvwhline(win, 2, 0, ACS_HLINE, COLS);
39
40         /* Print days */
41         for (int d = 0; d < days; d++) {
42                 int row = (start + d) / 7;
43                 int col = (start + d) % 7;
44                 mvwprintw(win, ROUND(3+row*vstep), ROUND(col*hstep), "%d", d+1);
45         }
46
47         /* Print lines */
48         for (int w = 1; w < weeks; w++)
49                 mvwhline(win, ROUND(2+w*vstep), 0, ACS_HLINE, COLS);
50         for (int d = 1; d < 7; d++) {
51                 int top = d >=  start         ? 0     : 1;
52                 int bot = d <= (start+days)%7 ? weeks : weeks-1;
53                 mvwvline(win, ROUND(3+top*vstep), ROUND(d*hstep-1),
54                                 ACS_VLINE, (bot-top)*vstep);
55                 for (int w = 1; w < weeks; w++) {
56                         int chr = w == top ? ACS_TTEE :
57                                   w == bot ? ACS_BTEE : ACS_PLUS;
58                         mvwaddch(win, ROUND(2+w*vstep), ROUND(d*hstep-1), chr);
59                 }
60         }
61 }
62
63 /* Month run */
64 int month_run(int key, mmask_t btn, int row, int col)
65 {
66         return 0;
67 }