home · contact · privacy
Unite all scroll hint drawing functions into one.
[plomrogue] / windows.c
1 #include <stdlib.h>
2 #include <stdint.h>
3 #include <ncurses.h>
4 #include <string.h>
5 #include "windows.h"
6
7 struct Corners {
8   struct yx_uint16 tl;
9   struct yx_uint16 tr;
10   struct yx_uint16 bl;
11   struct yx_uint16 br; };
12
13 static void refit_pad (struct WinMeta *);
14 static void place_window (struct WinMeta *, struct Win *);
15 static void update_windows (struct WinMeta *, struct Win *);
16 static void destroy_window (struct Win *);
17 static void draw_windows_borders (struct Win *, struct Win *, struct Corners *, uint16_t);
18 static void draw_window_borders (struct Win *, char);
19 static void draw_windows (struct Win *);
20
21 extern struct WinMeta init_win_meta (WINDOW * screen) {
22 // Create and populate WinMeta struct with sane default values.
23   struct WinMeta win_meta;
24   win_meta.screen = screen;
25   win_meta.pad.size.y = getmaxy(screen);
26   win_meta.pad.size.x = getmaxx(screen);
27   win_meta.chain_start = 0;
28   win_meta.chain_end = 0;
29   win_meta.pad_offset = 0;
30   win_meta.pad.curses_win = newpad(win_meta.pad.size.y, 1);
31   win_meta.active = 0;
32   return win_meta; }
33
34 extern struct Win init_window (struct WinMeta * win_meta, char * title, void * data, void * func) {
35 // Create and populate Win struct with sane default values.
36   struct Win win;
37   win.prev = 0;
38   win.next = 0;
39   win.frame.curses_win = 0;
40   win.title = title;
41   win.frame.size.x = 20;
42   win.frame.size.y = win_meta->pad.size.y - 1;
43   win.data = data;
44   win.draw = func;
45   return win; }
46
47 extern void append_window (struct WinMeta * win_meta, struct Win * win) {
48 // Append win to window chain. Set active, if first window. Update geometry of windows from new window on.
49   if (0 != win_meta->chain_start) {
50     win->prev = win_meta->chain_end;
51     win_meta->chain_end->next = win; }
52   else {
53     win_meta->active = win;
54     win_meta->chain_start = win; }
55   win_meta->chain_end = win;
56   update_windows(win_meta, win); }
57
58 static void refit_pad (struct WinMeta * win_meta) {
59 // Fit pad width to minimum width demanded by current windows' geometries.
60   uint16_t lastwincol = 0;
61   struct Win * win_p = win_meta->chain_start;
62   while (win_p != 0) {
63     if (win_p->start.x + win_p->frame.size.x > lastwincol + 1)
64       lastwincol = win_p->start.x + win_p->frame.size.x - 1;
65     win_p = win_p->next; }
66   if (getmaxx(win_meta->pad.curses_win) != lastwincol)
67     wresize(win_meta->pad.curses_win, getmaxy(win_meta->pad.curses_win), lastwincol + 2); }
68
69 extern void suspend_window (struct WinMeta * win_meta, struct Win * win) {
70 // Destroy win, suspend from chain. Update geometry of following rows and pad, as well as activity selection.
71   destroy_window(win);
72   if (win_meta->chain_start != win)    // Give win's position in the chain to element next to it in the chain.
73     win->prev->next = win->next;
74   else
75     win_meta->chain_start = win->next;
76   char pad_refitted = 0;
77   if (win_meta->chain_end != win) {                 // Let chain element next to win know its new predecessor.
78     win->next->prev = win->prev;
79     if (win_meta->active == win)                          // If win was active, shift active window pointer to
80       win_meta->active = win->next;                       // the next chain element, if that is a window ...
81     update_windows(win_meta, win->next);
82     pad_refitted = 1; }
83   else {
84     win_meta->chain_end = win->prev;
85     if (win_meta->active == win)                                       // ... or else to the previous element.
86       win_meta->active = win->prev; }
87   win->prev = 0;
88   win->next = 0;
89   if (0 == pad_refitted)                                                            // Refit pad if necessary.
90     refit_pad(win_meta); }
91
92 static void place_window (struct WinMeta * win_meta, struct Win * win) {
93 // Based on position and sizes of previous window, find fitting place for current window.
94   win->start.x = 0;                                // if window is first in chain, place it on top-left corner
95   win->start.y = 1;
96   if (0 != win->prev) {
97     struct Win * win_top = win->prev;
98     while (win_top->start.y != 1)
99       win_top = win_top->prev;                                   // else, default to placing window in new top
100     win->start.x = win_top->start.x + win_top->frame.size.x + 1; // column to the right of the last one
101     uint16_t winprev_maxy = win->prev->start.y + getmaxy(win->prev->frame.curses_win);
102     if (   win->frame.size.x <= win->prev->frame.size.x
103         && win->frame.size.y < win_meta->pad.size.y - winprev_maxy) {
104       win->start.x = win->prev->start.x;                   // place window below previous window if it fits
105       win->start.y = winprev_maxy + 1; }                   // vertically and is not wider than its predecessor
106     else {
107       struct Win * win_up = win->prev;
108       struct Win * win_upup = win_up;
109       uint16_t widthdiff;
110       while (win_up != win_top) {
111         win_upup = win_up->prev;
112         while (1) {
113           if (win_up->start.y != win_upup->start.y)
114             break;
115           win_upup = win_upup->prev; }
116         winprev_maxy = win_upup->start.y + getmaxy(win_upup->frame.curses_win);
117         widthdiff = (win_upup->start.x + win_upup->frame.size.x) - (win_up->start.x + win_up->frame.size.x);
118         if (win->frame.size.y < win_meta->pad.size.y - winprev_maxy && win->frame.size.x < widthdiff) {
119           win->start.x = win_up->start.x + win_up->frame.size.x + 1 ; // else try to open new sub column under
120           win->start.y = winprev_maxy + 1;                     // last window below which enough space remains
121           break; }
122         win_up = win_upup; } } } }
123
124 static void update_windows (struct WinMeta * win_meta, struct Win * win) {
125 // Update geometry of win and its next of kin. Destroy (if visible), (re-)build window. If need, resize pad.
126   if (0 != win->frame.curses_win)
127     destroy_window (win);
128   place_window(win_meta, win);
129   refit_pad(win_meta);
130   win->frame.curses_win = subpad(win_meta->pad.curses_win, win->frame.size.y, win->frame.size.x, win->start.y,
131                                  win->start.x);
132   if (0 != win->next)
133     update_windows (win_meta, win->next); }
134
135 static void destroy_window (struct Win * win) {
136 // Delete window.
137   delwin(win->frame.curses_win);
138   win->frame.curses_win = 0; }
139
140 static void draw_window_borders (struct Win * win, char active) {
141 // Draw borders of window win, including title. Decorate in a special way if window is marked as active.
142   uint16_t y, x;
143   for (y = win->start.y; y <= win->start.y + win->frame.size.y; y++) {
144     mvwaddch(wgetparent(win->frame.curses_win), y, win->start.x - 1, '|');
145     mvwaddch(wgetparent(win->frame.curses_win), y, win->start.x + win->frame.size.x, '|'); }
146   for (x = win->start.x; x <= win->start.x + win->frame.size.x; x++) {
147     mvwaddch(wgetparent(win->frame.curses_win), win->start.y - 1, x, '-');
148     mvwaddch(wgetparent(win->frame.curses_win), win->start.y + win->frame.size.y, x, '-'); }
149   char min_title_length_visible = 3;        // 1 char minimal, plus 2 chars for decoration left/right of title
150   if (win->frame.size.x >= min_title_length_visible) {
151     uint16_t title_offset = 0;
152     if (win->frame.size.x > strlen(win->title) + 2)
153       title_offset = (win->frame.size.x - (strlen(win->title) + 2)) / 2;              // + 2 is for decoration
154     uint16_t length_visible = strnlen(win->title, win->frame.size.x - 2);
155     char title[length_visible + 3];
156     char decoration = ' ';
157     if (1 == active)
158       decoration = '$';
159     memcpy(title + 1, win->title, length_visible);
160     title[0] = title[length_visible + 1] = decoration;
161     title[length_visible + 2] = '\0';
162     mvwaddstr(wgetparent(win->frame.curses_win), win->start.y - 1, win->start.x + title_offset, title); } }
163
164 static void draw_windows_borders (struct Win * win, struct Win * win_active, struct Corners * corners,
165                                   uint16_t ccount) {
166 // Craw draw_window_borders() for all windows in chain from win on. Save current window's border corners.
167   char active = 0;
168   if (win == win_active)
169     active = 1;
170   draw_window_borders(win, active);
171   corners[ccount].tl.y = win->start.y - 1;
172   corners[ccount].tl.x = win->start.x - 1;
173   corners[ccount].tr.y = win->start.y - 1;
174   corners[ccount].tr.x = win->start.x + win->frame.size.x;
175   corners[ccount].bl.y = win->start.y + win->frame.size.y;
176   corners[ccount].bl.x = win->start.x - 1;
177   corners[ccount].br.y = win->start.y + win->frame.size.y;
178   corners[ccount].br.x = win->start.x + win->frame.size.x;
179   if (0 != win->next) {
180     draw_windows_borders (win->next, win_active, corners, ccount + 1); } }
181
182 static void draw_windows (struct Win * win) {
183 // Draw contents of all windows in window chain from win on.
184   win->draw(win);
185   if (0 != win->next) {
186     draw_windows (win->next); } }
187
188 extern void draw_scroll_hint (struct Frame * frame, uint16_t pos, uint32_t dist, char dir) {
189 // Draw scroll hint into frame at pos (row or col dependend on dir), mark distance of dist cells into dir.
190   char more[] = "more";
191   char unit_cols[] = "columns";
192   char unit_rows[] = "lines";
193   uint16_t dsc_space = frame->size.x;
194   char * unit = unit_rows;
195   if ('<' == dir || '>' == dir) {
196     dsc_space = frame->size.y;
197     unit = unit_cols; }
198   char * scrolldsc = malloc((4 * sizeof(char)) + strlen(more) + strlen(unit) + 10); // 10: strlen for uint32_t
199   sprintf(scrolldsc, " %d %s %s ", dist, more, unit);
200   char offset = 1, q;
201   if (dsc_space > strlen(scrolldsc) + 1)
202     offset = (dsc_space - strlen(scrolldsc)) / 2;
203   chtype symbol;
204   for (q = 0; q < dsc_space; q++) {
205     if (q >= offset && q < strlen(scrolldsc) + offset)
206       symbol = scrolldsc[q - offset] | A_REVERSE;
207     else
208       symbol = dir | A_REVERSE;
209     if ('<' == dir || '>' == dir)
210       mvwaddch(frame->curses_win, q, pos, symbol);
211     else
212       mvwaddch(frame->curses_win, pos, q, symbol); }
213   free(scrolldsc); }
214
215 extern void draw_all_windows (struct WinMeta * win_meta) {
216 // Draw pad with all windows and their borders, plus scrolling hints.
217   erase();
218   wnoutrefresh(win_meta->screen);
219   werase(win_meta->pad.curses_win);
220   if (win_meta->chain_start) {
221     uint16_t n_wins = 1;
222     struct Win * win_p = win_meta->chain_start;
223     while (0 != win_p->next) {
224       win_p = win_p->next;
225       n_wins++; }
226     struct Corners * all_corners = malloc(sizeof(struct Corners) * n_wins);
227     draw_windows (win_meta->chain_start);
228     draw_windows_borders (win_meta->chain_start, win_meta->active, all_corners, 0);
229     uint16_t i;
230     for (i = 0; i < n_wins; i++) {
231       mvwaddch(win_meta->pad.curses_win, all_corners[i].tl.y, all_corners[i].tl.x, '+');
232       mvwaddch(win_meta->pad.curses_win, all_corners[i].tr.y, all_corners[i].tr.x, '+');
233       mvwaddch(win_meta->pad.curses_win, all_corners[i].bl.y, all_corners[i].bl.x, '+');
234       mvwaddch(win_meta->pad.curses_win, all_corners[i].br.y, all_corners[i].br.x, '+'); }
235     free(all_corners);
236     uint16_t y;
237     if (win_meta->pad_offset > 0)
238       draw_scroll_hint(&win_meta->pad, win_meta->pad_offset, win_meta->pad_offset + 1, '<');
239     if (win_meta->pad_offset + win_meta->pad.size.x < getmaxx(win_meta->pad.curses_win) - 1)
240       for (y = 0; y < win_meta->pad.size.y; y++)
241         draw_scroll_hint(&win_meta->pad, win_meta->pad_offset + win_meta->pad.size.x - 1,
242                          getmaxx(win_meta->pad.curses_win) - (win_meta->pad_offset + win_meta->pad.size.x),
243                                   '>');
244     pnoutrefresh(win_meta->pad.curses_win, 0, win_meta->pad_offset, 0, 0, win_meta->pad.size.y,
245                  win_meta->pad.size.x - 1); }
246   doupdate(); }
247
248 extern void resize_active_window (struct WinMeta * win_meta, uint16_t height, uint16_t width) {
249 // Grow or shrink currently active window. Correct its geometry and that of its followers.
250   if (0 != win_meta->active && width > 0 && height > 0 && height < win_meta->pad.size.y) {
251     win_meta->active->frame.size.y = height;
252     win_meta->active->frame.size.x = width;
253     update_windows(win_meta, win_meta->chain_start); } }
254
255 extern void cycle_active_window (struct WinMeta * win_meta, char dir) {
256 // Cycle active window selection forwards (dir = 'n') or backwards.
257   if (0 != win_meta->active) {
258     if ('n' == dir) {
259       if (win_meta->active->next != 0)
260         win_meta->active = win_meta->active->next;
261       else
262         win_meta->active = win_meta->chain_start; }
263     else {
264       if (win_meta->active->prev != 0)
265         win_meta->active = win_meta->active->prev;
266       else
267         win_meta->active = win_meta->chain_end; } } }
268
269 extern void shift_active_window (struct WinMeta * win_meta, char dir) {
270 // Move active window forward/backward in window chain. If jumping beyond start/end, move to other chain end.
271   if (0 != win_meta->active && win_meta->chain_start != win_meta->chain_end && (dir == 'f' || dir == 'b')) {
272     struct Win * win_shift = win_meta->active, * win_p, * win_p_next;
273     char wrap = 0;
274     if ((dir == 'f' && win_shift == win_meta->chain_end)
275         || (dir == 'b' && win_shift == win_meta->chain_start))
276       wrap = 1;
277     uint16_t i, i_max;
278     for (i_max = 1, win_p = win_meta->chain_start; win_p != win_meta->chain_end; i_max++)
279       win_p = win_p->next;
280     struct Win ** wins = malloc(i_max * sizeof(struct Win *));
281     for (i = 0, win_p = win_meta->chain_start; i < i_max; i++) {
282       win_p_next = win_p->next;
283       suspend_window(win_meta, win_p);
284       wins[i] = win_p;
285       win_p = win_p_next; }
286     if (wrap)
287       if (dir == 'f') {
288         append_window(win_meta, win_shift);
289         for (i = 0; i < i_max - 1; i++)
290           append_window(win_meta, wins[i]); }
291       else {
292         for (i = 1; i < i_max; i++)
293           append_window(win_meta, wins[i]);
294         append_window(win_meta, win_shift); }
295     else
296       for (i = 0; i < i_max; i++)
297         if ((dir == 'f' && win_shift == wins[i]) || (dir == 'b' && win_shift == wins[i+1])) {
298           append_window(win_meta, wins[i+1]);
299           append_window(win_meta, wins[i]);
300           i++; }
301         else
302           append_window(win_meta, wins[i]);
303     free(wins);
304     win_meta->active = win_shift; } }
305
306 extern void reset_pad_offset(struct WinMeta * win_meta, uint16_t new_offset) {
307 // Apply new_offset to windows pad, if it proves to be sane.
308   if (new_offset >= 0 && new_offset + win_meta->pad.size.x < getmaxx(win_meta->pad.curses_win))
309     win_meta->pad_offset = new_offset; }