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