]> Pileus Git - lackey/blob - views/day.c
Fix some resize issues
[lackey] / views / day.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 int     line;
30 static WINDOW *win;
31 static WINDOW *times;
32 static WINDOW *body;
33
34 /* Box packing helpers */
35 static void clear_old(event_t **list, int n, event_t *cur)
36 {
37         for (int i = 0; i < n; i++)
38                 if (list[i] && compare(&list[i]->end, &cur->start) <= 0)
39                         list[i] = NULL;
40 }
41
42 static int next_free(event_t **list, int n)
43 {
44         for (int i = 0; i < n; i++)
45                 if (!list[i])
46                         return i;
47         return n-1;
48 }
49
50 static int count_busy(event_t **list, int n)
51 {
52         int sum = 0;
53         for (int i = 0; i < n; i++)
54                 if (list[i])
55                         sum++;
56         return sum;
57 }
58
59 static int get_ncols(event_t *event, int n)
60 {
61         int ncols = 0;
62         event_t *preview[n];
63         memset(preview, 0, sizeof(preview));
64         for (event_t *cur = event; cur; cur = cur->next) {
65                 int col = next_free(preview, n);
66                 preview[col] = cur;
67                 ncols = MAX(ncols, col+1);
68                 if (cur->next)
69                         clear_old(preview, n, cur->next);
70                 if (count_busy(preview, n) == 0)
71                         break;
72         }
73         return ncols;
74 }
75
76 static int get_col(event_t **list, int n, event_t *event, int *ncols)
77 {
78         clear_old(list, n, event);
79
80         /* If there are current events, then recalculate
81          * ncols for the next series of events */
82         if (count_busy(list, n) == 0)
83                 *ncols = get_ncols(event, n);
84
85         /* Find next open slot */
86         int col = next_free(list, n);
87         list[col] = event;
88         return col;
89 }
90
91 /* Day init */
92 void day_init(WINDOW *_win)
93 {
94         win   = _win; //    lines    cols    y  x
95         times = derwin(win, LINES-2,      5, 0, 0);
96         body  = derwin(win, LINES-2, COLS-5, 0, 5);
97         line  = 10*4; // 10:00
98 }
99
100 /* Day size */
101 void day_size(int rows, int cols)
102 {
103         int hdr = 2-COMPACT;
104         wmvresize(times, hdr, 0, rows-hdr,      5);
105         wmvresize(body,  hdr, 5, rows-hdr, cols-5);
106 }
107
108 /* Day draw */
109 void day_draw(void)
110 {
111         const char *mstr = month_to_string(MONTH);
112         const char *dstr = day_to_string(day_of_week(YEAR, MONTH, DAY));
113
114         int y = !COMPACT+1;
115
116         /* Print Header */
117         if (COMPACT) wattron(win, A_REVERSE | A_BOLD);
118         mvwhline(win, 0, 0, ' ', COLS);
119         mvwprintw(win, 0, 0, "%s, %s %d", dstr, mstr, DAY+1);
120         mvwprintw(win, 0, COLS-10, "%d-%02d-%02d", YEAR, MONTH, DAY+1);
121         if (COMPACT) wattroff(win, A_REVERSE | A_BOLD);
122
123         /* Resize body */
124         wshrink(times, y);
125         wshrink(body,  y);
126         /* Print times */
127         mvwprintw(times, 0, 0, "%02d:%02d", ((line/4)-1)%12+1, (line*15)%60);
128         for (int h = 0; h < 24; h++)
129                 mvwprintw(times, h*4-line, 0, "%02d:%02d", (h-1)%12+1, 0);
130
131         /* Print events */
132         event_t *event = EVENTS;
133         event_t *active[10] = {};
134         int ncols = 0;
135         for (int h = 0; h < 24; h++)
136         for (int m = 0; m < 60; m+=15)
137         while (event && before(&event->start,
138                         YEAR, MONTH, DAY, h+(m+15)/60, (m+15)%60)) {
139                 if (!before(&event->start, YEAR, MONTH, DAY, h, m)) {
140                         int col    = get_col(active, N_ELEMENTS(active), event, &ncols);
141                         int left   = ROUND((col+0.0)*(COLS-6)/ncols) + 1;
142                         int right  = ROUND((col+1.0)*(COLS-6)/ncols) + 1;
143                         int row    = h*4 + m/15 - line;
144                         int height = (get_mins(&event->start, &event->end)-1)/15+1;
145                         event_box(body, event, row, left, height, right-left);
146                 }
147                 event = event->next;
148         }
149
150         /* Print lines */
151         if (!COMPACT)
152                 mvwhline(win, 1, 0, ACS_HLINE, COLS);
153         mvwvline(body, 0, 0, ACS_VLINE, LINES-4+COMPACT+COMPACT);
154 }
155
156 /* Day run */
157 int day_run(int key, mmask_t btn, int row, int col)
158 {
159         int days = 0, ref = 0;
160         switch (key)
161         {
162                 case 'h': ref = 1; days = -1; break;
163                 case 'l': ref = 1; days =  1; break;
164                 case 'i': ref = 1; days = -7; break;
165                 case 'o': ref = 1; days =  7; break;
166                 case 'k': ref = 1; line--;    break;
167                 case 'j': ref = 1; line++;    break;
168         }
169         line = CLAMP(line, 0, 24*4);
170         if (days)
171                 add_days(&YEAR, &MONTH, &DAY, days);
172         if (ref) {
173                 werase(win);
174                 day_draw();
175                 wrefresh(win);
176         }
177         return ref;
178 }