From 19a02d772c5753208c667b37a57527796ac6dc75 Mon Sep 17 00:00:00 2001 From: Andy Spencer Date: Sun, 14 Oct 2012 21:02:18 +0000 Subject: [PATCH] Move drawing code to screen.c, work on day view --- doc/screen.txt | 20 +++++++-------- src/date.c | 6 ++--- src/screen.c | 50 +++++++++++++++++++++++++++++++++++- src/screen.h | 4 +++ src/util.c | 13 ++-------- src/util.h | 3 --- view/day.c | 69 ++++++++++++++++++++++++++++++++++++++------------ view/month.c | 21 ++------------- view/week.c | 54 ++++++++++----------------------------- view/year.c | 4 +-- 10 files changed, 137 insertions(+), 107 deletions(-) diff --git a/doc/screen.txt b/doc/screen.txt index 293f2d0..4fc812a 100644 --- a/doc/screen.txt +++ b/doc/screen.txt @@ -1,6 +1,6 @@ Day view ┌─────────────────────────────────────────────────────────────────────┐ - │ Day Week Month Year │ Todo Notes │ Settings Help │ + │ Day Week Month Year │ Events Todo │ Settings Help │ │ ─────────────────────────────────────────────────────────────────── │ │ 2012-09-27 Saturday September 27 │ │ ─────────────────────────────────────────────────────────────────── │ @@ -34,7 +34,7 @@ Day view Week view ┌─────────────────────────────────────────────────────────────────────┐ - │ Day Week Month Year │ Todo Notes │ Settings Help │ + │ Day Week Month Year │ Events Todo │ Settings Help │ │ ─────────────────────────────────────────────────────────────────── │ │ 09/24 09/25 09/26 09/27 09/28 │ │ Monday Tuesday Wednesday Thursday Friday │ @@ -65,7 +65,7 @@ Week view Month view ┌─────────────────────────────────────────────────────────────────────┐ - │ Day Week Month Year │ Todo Notes │ Settings Help │ + │ Day Week Month Year │ Events Todo │ Settings Help │ │ ─────────────────────────────────────────────────────────────────── │ │ September 2012 │ │ Sun Mon Tue Wed Thu Fri Sat │ @@ -98,7 +98,7 @@ Month view Year view ┌─────────────────────────────────────────────────────────────────────┐ - │ Day Week Month Year │ Todo Notes │ Settings Help │ + │ Day Week Month Year │ Events Todo │ Settings Help │ │ ─────────────────────────────────────────────────────────────────── │ │ 2012 │ │ January │ February │ March │ @@ -137,9 +137,9 @@ Year view │ │ └─────────────────────────────────────────────────────────────────────┘ -Notes view +Events view ┌─────────────────────────────────────────────────────────────────────┐ - │ Day Week Month Year │ Todo Notes │ Settings Help │ + │ Day Week Month Year │ Events Todo │ Settings Help │ │ ─────────────────────────────────────────────────────────────────── │ │ │ │ 2012-09-24 │ @@ -166,9 +166,9 @@ Notes view └─────────────────────────────────────────────────────────────────────┘ -Notes view +Todo view ┌─────────────────────────────────────────────────────────────────────┐ - │ Day Week Month Year │ Todo Notes │ Settings Help │ + │ Day Week Month Year │ Events Todo │ Settings Help │ │ ─────────────────────────────────────────────────────────────────── │ │ │ │ ??? │ @@ -181,7 +181,7 @@ Notes view Settings view ┌─────────────────────────────────────────────────────────────────────┐ - │ Day Week Month Year │ Todo Notes │ Settings Help │ + │ Day Week Month Year │ Events Todo │ Settings Help │ │ ─────────────────────────────────────────────────────────────────── │ │ │ │ ??? │ @@ -195,7 +195,7 @@ Settings view Help view ┌─────────────────────────────────────────────────────────────────────┐ - │ Day Week Month Year │ Todo Notes │ Settings Help │ + │ Day Week Month Year │ Events Todo │ Settings Help │ │ ─────────────────────────────────────────────────────────────────── │ │ │ │ ??? │ diff --git a/src/date.c b/src/date.c index f55ae34..9503b4c 100644 --- a/src/date.c +++ b/src/date.c @@ -38,9 +38,9 @@ void date_init(void) DAY = tm->tm_mday-1; /* Testing */ - //YEAR = 2009; - //MONTH = MAY; - //DAY = 1; + //YEAR = 2008; + //MONTH = OCT; + //DAY = 21; } /* Time functions */ diff --git a/src/screen.c b/src/screen.c index 1a7dd4b..0cbbd88 100644 --- a/src/screen.c +++ b/src/screen.c @@ -15,11 +15,14 @@ * along with this program. If not, see . */ +#define _XOPEN_SOURCE_EXTENDED + #include #include #include "util.h" #include "date.h" +#include "event.h" #include "screen.h" /* Types */ @@ -47,7 +50,7 @@ view_t views[] = { { "Help", help_init, help_size, help_draw, help_run, {KEY_F(8), '8', '?'} }, }; -int active = 2; +int active = 0; /* Local functions */ void draw_header(void) @@ -66,6 +69,51 @@ void draw_header(void) refresh(); } +/* Helper functions */ +void event_box(WINDOW *win, event_t *event, int y, int x, int h, int w) +{ + int l = 0; + int s = y < 0 ? -y-1 : 0; + + int color = event->cat == NULL ? 0 : + !strcmp(event->cat, "class") ? COLOR_CLASS : + !strcmp(event->cat, "ec") ? COLOR_EC : + !strcmp(event->cat, "work") ? COLOR_WORK : COLOR_OTHER ; + + if (color) wattron(win, COLOR_PAIR(color)); + + if (h >= 2) mvwhline_set(win, y, x+1, WACS_T_HLINE, w-2); + if (h <= 1) mvwadd_wch(win, y, x, WACS_BULLET); + if (h >= 2) mvwadd_wch(win, y, x, WACS_T_ULCORNER); + if (h >= 2) mvwadd_wch(win, y, x+w-1, WACS_T_URCORNER); + if (h >= 3) mvwvline_set(win, y+1+s, x, WACS_T_VLINE, h-2-s); + if (h >= 3) mvwvline_set(win, y+1+s, x+w-1, WACS_T_VLINE, h-2-s); + if (h >= 2) mvwadd_wch(win, y+h-1, x, WACS_T_LLCORNER); + if (h >= 2) mvwadd_wch(win, y+h-1, x+w-1, WACS_T_LRCORNER); + if (h >= 2) mvwhline_set(win, y+h-1, x+1, WACS_T_HLINE, w-2); + + if (color) wattroff(win, COLOR_PAIR(color)); + + if (lname) mvwprintw(win, y+l++, x+1, "%.*s", w-2, event->name); + if (lloc) mvwprintw(win, y+l++, x+1, "@ %-*.*s", w-4, w-4, event->loc); + if (ldesc) mvwprintw(win, y+l++, x+1, "%-*.*s", w-2, w-2, event->desc); +} + +void event_line(WINDOW *win, event_t *event, int y, int x, int w) +{ + int color = event->cat == NULL ? 0 : + !strcmp(event->cat, "class") ? COLOR_CLASS : + !strcmp(event->cat, "ec") ? COLOR_EC : + !strcmp(event->cat, "work") ? COLOR_WORK : COLOR_OTHER ; + + if (color) wattron(win, COLOR_PAIR(color)); + mvwaddch(win, y, x+0, ACS_BLOCK); + if (color) wattroff(win, COLOR_PAIR(color)); + + mvwprintw(win, y, x+1, "%-*.*s", w-1, w-1, event->name); +} + + /* Screen init */ void screen_init(void) { diff --git a/src/screen.h b/src/screen.h index 46cdf94..cf83b6e 100644 --- a/src/screen.h +++ b/src/screen.h @@ -24,6 +24,10 @@ #define COLOR_WORK 5 #define COLOR_OTHER 6 +/* Helper functions */ +void event_box(WINDOW *win, event_t *event, int y, int x, int h, int w); +void event_line(WINDOW *win, event_t *event, int y, int x, int w); + /* Screen functions */ void screen_init(void); void screen_resize(void); diff --git a/src/util.c b/src/util.c index f13ad44..86bc373 100644 --- a/src/util.c +++ b/src/util.c @@ -21,6 +21,8 @@ #include #include +#include "date.h" +#include "event.h" #include "screen.h" /* Static data */ @@ -32,17 +34,6 @@ void util_init(void) debug_fd = fopen("/tmp/lackey.log", "w+"); } -/* Misc functions */ -char *sdup(const char *str) -{ - if (str == NULL) - return NULL; - int len = strlen(str); - char *dup = malloc(len+1); - memcpy(dup, str, len+1); - return dup; -} - /* Debugging functions */ int debug(char *fmt, ...) { diff --git a/src/util.h b/src/util.h index 25e6d8d..b40f8b3 100644 --- a/src/util.h +++ b/src/util.h @@ -25,9 +25,6 @@ /* Debug functions */ void util_init(void); -/* Misc functions */ -char *sdup(const char *str); - /* Debug functions */ #ifdef DEBUG int debug(char *fmt, ...); diff --git a/view/day.c b/view/day.c index 37f9e2f..027474b 100644 --- a/view/day.c +++ b/view/day.c @@ -15,22 +15,39 @@ * along with this program. If not, see . */ +#define _XOPEN_SOURCE_EXTENDED + +#include #include +#include "util.h" #include "date.h" +#include "event.h" +#include "screen.h" /* Static data */ +static int line; static WINDOW *win; +static WINDOW *head; +static WINDOW *times; +static WINDOW *body; /* Day init */ void day_init(WINDOW *_win) { - win = _win; + win = _win; // lines cols y x + head = derwin(win, 1, COLS, 0, 0); + times = derwin(win, LINES-2-2, 5, 2, 0); + body = derwin(win, LINES-2-2, COLS-6, 2, 6); + line = 10*4; // 10:00 } /* Day size */ void day_size(int rows, int cols) { + wresize(head, 1, cols ); + wresize(times, rows-2, 5); + wresize(body, rows-2, cols-6); } /* Day draw */ @@ -39,34 +56,54 @@ void day_draw(void) const char *mstr = month_to_string(MONTH); const char *dstr = day_to_string(day_of_week(YEAR, MONTH, DAY)); - /* Clear */ - werase(win); - /* Print Header */ - mvwprintw(win, 0, 0, "%s, %s %d", dstr, mstr, DAY+1); - mvwprintw(win, 0, COLS-10, "%d-%02d-%02d", YEAR, MONTH, DAY+1); - mvwhline(win, 1, 0, ACS_HLINE, COLS); + mvwprintw(head, 0, 0, "%s, %s %d", dstr, mstr, DAY+1); + mvwprintw(head, 0, COLS-10, "%d-%02d-%02d", YEAR, MONTH, DAY+1); /* Print times */ - int start = 8; - for (int h = 0; h < (LINES-5)/4+1; h++) - mvwprintw(win, 2+h*4, 0,"%02d:%02d", (start+h)%12, 0); + mvwprintw(times, 0, 0, "%02d:%02d", ((line/4)-1)%12+1, (line*15)%60); + for (int h = 0; h < 24; h++) + mvwprintw(times, h*4-line, 0, "%02d:%02d", (h-1)%12+1, 0); + + /* Print events */ + event_t *event = EVENTS; + for (int h = 0; h < 24; h++) + for (int m = 0; m < 60; m+=15) + while (event && before(&event->start, + YEAR, MONTH, DAY, h+(m+15)/60, (m+15)%60)) { + if (!before(&event->start, YEAR, MONTH, DAY, h, m)) { + int y = h*4 + m/15 - line; + int x = 0; + int h = (get_mins(&event->start, &event->end)-1)/15+1; + int w = COLS-6; + event_box(body, event, y, x, h, w); + } + event = event->next; + } + + /* Print lines */ + mvwhline(win, 1, 0, ACS_HLINE, COLS); mvwvline(win, 2, 5, ACS_VLINE, LINES-4); } /* Day run */ int day_run(int key, mmask_t btn, int row, int col) { - int days = 0; + int days = 0, ref = 0; switch (key) { - case 'h': days = -1; break; - case 'l': days = 1; break; - case 'i': days = -7; break; - case 'o': days = 7; break; + case 'h': ref = 1; days = -1; break; + case 'l': ref = 1; days = 1; break; + case 'i': ref = 1; days = -7; break; + case 'o': ref = 1; days = 7; break; + case 'k': ref = 1; line--; break; + case 'j': ref = 1; line++; break; } - if (days) { + line = CLAMP(line, 0, 24*4); + if (days) add_days(&YEAR, &MONTH, &DAY, days); + if (ref) { + werase(win); day_draw(); wrefresh(win); } diff --git a/view/month.c b/view/month.c index b8e3eb1..2179f4f 100644 --- a/view/month.c +++ b/view/month.c @@ -28,21 +28,6 @@ /* Static data */ static WINDOW *win; -/* Local functions */ -static void print_event(event_t *event, int y, int x, int w) -{ - int color = event->cat == NULL ? 0 : - !strcmp(event->cat, "class") ? COLOR_CLASS : - !strcmp(event->cat, "ec") ? COLOR_EC : - !strcmp(event->cat, "work") ? COLOR_WORK : COLOR_OTHER ; - - if (color) wattron(win, COLOR_PAIR(color)); - mvwaddch(win, y, x+0, ACS_BLOCK); - if (color) wattroff(win, COLOR_PAIR(color)); - - mvwprintw(win, y, x+1, "%-*.*s", w-1, w-1, event->name); -} - /* Month init */ void month_init(WINDOW *_win) { @@ -65,9 +50,6 @@ void month_draw(void) const float hstep = (float)(COLS-1)/7.0; const float vstep = (float)(LINES-6)/weeks; - /* Clear */ - werase(win); - /* Print Header */ mvwprintw(win, 0, midpt, "%s %d", name, YEAR); for (int d = 0; d < 7; d++) { @@ -95,7 +77,7 @@ void month_draw(void) while (event && before(&event->start, YEAR, MONTH, d, 24, 0)) { if (!before(&event->start, YEAR, MONTH, d, 0, 0)){ if (y == e) mvwhline(win, y, x-3, ACS_DARROW, 2); - if (y <= e) print_event(event, y, x, w); + if (y <= e) event_line(win, event, y, x, w); y++; } event = event->next; @@ -150,6 +132,7 @@ int month_run(int key, mmask_t btn, int row, int col) if (days || months) { add_days(&YEAR, &MONTH, &DAY, days); add_months(&YEAR, &MONTH, months); + werase(win); month_draw(); wrefresh(win); } diff --git a/view/week.c b/view/week.c index 98d12f3..e9d7c04 100644 --- a/view/week.c +++ b/view/week.c @@ -32,42 +32,6 @@ static WINDOW *head; static WINDOW *times; static WINDOW *body; -/* Local functions */ -static void print_event(event_t *event, wday_t day, hour_t hour, min_t min, float hstep) -{ - int x = ROUND(day*hstep); - int y = hour*4 + min/15 - line; - int w = ROUND((day+1)*hstep) - 1 - x; - int h = (get_mins(&event->start, &event->end)-1)/15+1; - int l = 0; - int s = y < 0 ? -y-1 : 0; - - int color = event->cat == NULL ? 0 : - !strcmp(event->cat, "class") ? COLOR_CLASS : - !strcmp(event->cat, "ec") ? COLOR_EC : - !strcmp(event->cat, "work") ? COLOR_WORK : COLOR_OTHER ; - - if (color) wattron(body, COLOR_PAIR(color)); - - if (h >= 2) mvwhline_set(body, y, x+1, WACS_T_HLINE, w-2); - if (h <= 1) mvwadd_wch(body, y, x, WACS_BULLET); - if (h >= 2) mvwadd_wch(body, y, x, WACS_T_ULCORNER); - if (h >= 2) mvwadd_wch(body, y, x+w-1, WACS_T_URCORNER); - if (h >= 3) mvwvline_set(body, y+1+s, x, WACS_T_VLINE, h-2-s); - if (h >= 3) mvwvline_set(body, y+1+s, x+w-1, WACS_T_VLINE, h-2-s); - if (h >= 2) mvwadd_wch(body, y+h-1, x, WACS_T_LLCORNER); - if (h >= 2) mvwadd_wch(body, y+h-1, x+w-1, WACS_T_LRCORNER); - if (h >= 2) mvwhline_set(body, y+h-1, x+1, WACS_T_HLINE, w-2); - - if (color) wattroff(body, COLOR_PAIR(color)); - - if (lname) mvwprintw(body, y+l++, x+1, "%.*s", w-2, event->name); - if (lloc) mvwprintw(body, y+l++, x+1, "@ %-*.*s", w-4, w-4, event->loc); - if (ldesc) mvwprintw(body, y+l++, x+1, "%-*.*s", w-2, w-2, event->desc); - - debug("week: event = %s\n", event->name); -} - /* Week init */ void week_init(WINDOW *_win) { @@ -93,9 +57,6 @@ void week_draw(void) int y = 3; const float hstep = (float)(COLS-x)/7.0; - /* Clear */ - werase(win); - /* Get start of week */ year_t year = YEAR; month_t month = MONTH; @@ -128,8 +89,13 @@ void week_draw(void) for (int m = 0; m < 60; m+=15) while (event && before(&event->start, year, month, day, h+(m+15)/60, (m+15)%60)) { - if (!before(&event->start, year, month, day, h, m)) - print_event(event, d, h, m, hstep); + if (!before(&event->start, year, month, day, h, m)) { + int y = h*4 + m/15 - line; + int x = ROUND(d*hstep); + int h = (get_mins(&event->start, &event->end)-1)/15+1; + int w = ROUND((d+1)*hstep) - 1 - x; + event_box(body, event, y, x, h, w); + } event = event->next; } @@ -138,12 +104,17 @@ void week_draw(void) for (int d = 0; d < 7; d++) mvwvline(win, y, x+ROUND(d*hstep)-1, ACS_VLINE, LINES-y-2); + /* Draw today */ int l = x+ROUND((shift+0)*hstep)-1; int r = x+ROUND((shift+1)*hstep)-1; mvwhline (win, y-1, l, ACS_BLOCK, r-l+1); mvwvline_set(win, y, l, WACS_T_VLINE, LINES-y-2); mvwvline_set(win, y, r, WACS_T_VLINE, LINES-y-2); + for (int h = (line+3)/4; h < 24; h++) { + mvwadd_wch(win, y+h*4-line, l, WACS_T_LTEE); + mvwadd_wch(win, y+h*4-line, r, WACS_T_RTEE); + } } /* Week run */ @@ -163,6 +134,7 @@ int week_run(int key, mmask_t btn, int row, int col) if (days) add_days(&YEAR, &MONTH, &DAY, days); if (ref) { + werase(win); week_draw(); wrefresh(win); } diff --git a/view/year.c b/view/year.c index 17a5c92..086c0e8 100644 --- a/view/year.c +++ b/view/year.c @@ -81,9 +81,6 @@ void year_draw(void) int y = 0; int h[4] = {}; - /* Clear */ - werase(win); - /* Determine heights */ for (int m = 0; m < 12; m++) { int weeks = weeks_in_month(YEAR, m); @@ -167,6 +164,7 @@ int year_run(int key, mmask_t btn, int row, int col) } /* Refresh */ + werase(win); year_draw(); wrefresh(win); return 0; -- 2.43.2