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