From 2eab37b96483bb928f213ae88fb91c6f22b7ff22 Mon Sep 17 00:00:00 2001 From: Andy Spencer Date: Thu, 4 Oct 2012 08:51:50 +0000 Subject: [PATCH] Add year view --- src/screen.c | 2 +- src/view/year.c | 68 +++++++++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 67 insertions(+), 3 deletions(-) diff --git a/src/screen.c b/src/screen.c index b02d068..1fdedc8 100644 --- a/src/screen.c +++ b/src/screen.c @@ -27,7 +27,7 @@ view_t views[] = { { "Help", help_init, help_draw, help_run, {KEY_F(8), '8', 'h', '?'} }, }; -int active = 2; +int active = 3; /* Local functions */ void draw_header(void) diff --git a/src/view/year.c b/src/view/year.c index 7e3e51a..dd9494d 100644 --- a/src/view/year.c +++ b/src/view/year.c @@ -1,8 +1,37 @@ +#include #include +#include "util.h" + +/* Macros */ +#define MAX(a,b) ((a) > (b) ? (a) : (b)) +#define MW (2*7+6) + /* Static data */ static WINDOW *win; +/* Test data */ +const static int YEAR = 2012; +const static int MONTH = SEP; +const static int DAY = 29; + +/* Helper functions */ +static void print_month(month_t month, int y, int x) +{ + const char *name = month_to_string(month); + const int start = start_of_month(YEAR, month); + const char days = days_in_month(YEAR, month); + mvwprintw(win, y, x+MW/2-strlen(name)/2, "%s", name); + wmove(win, y+1, x); + for (int d = 0; d < 7; d++) + wprintw(win, "%-3s", day_to_st(d)); + for (int d = 0; d < days; d++) { + int row = (start + d) / 7; + int col = (start + d) % 7; + mvwprintw(win, y+2+row, x+col*3, "%d", d+1); + } +} + /* Year init */ void year_init(WINDOW *_win) { @@ -12,8 +41,43 @@ void year_init(WINDOW *_win) /* Year draw */ void year_draw(void) { - mvwprintw(win, 0, 1, "%s\n", "year"); - wrefresh(win); + int w = MW*3 + 2*3; + int x = COLS/2 - w/2; + int y = 0; + int h[4] = {}; + + /* Determine heights */ + for (int m = 0; m < 12; m++) { + int weeks = weeks_in_month(YEAR, m); + h[m/3] = MAX(h[m/3], weeks+2); + } + int sum = h[0]+h[1]+h[2]+h[3]; + + /* Print Header */ + mvwprintw(win, y++, COLS/2-2, "%d", YEAR); + + /* Print Months */ + for (int m = 0; m < 12; m++) { + print_month(m, y, x); + if (m % 3 == 2) { + x = COLS/2 - w/2; + y += h[m/3]+1; + } else { + x += 3+MW; + } + } + + /* Print Lines */ + y = 1; + mvwvline(win, y, x+(MW+3)*1-2, ACS_VLINE, sum+3); + mvwvline(win, y, x+(MW+3)*2-2, ACS_VLINE, sum+3); + for (int i = 0; i < 3; i++) { + y += h[i]; + mvwhline(win, y, x, ACS_HLINE, w); + mvwaddch(win, y, x+(MW+3)*1-2, ACS_PLUS); + mvwaddch(win, y, x+(MW+3)*2-2, ACS_PLUS); + y++; + } } /* Year run */ -- 2.43.2