home · contact · privacy
Scrolling pad now also has its own function with its own sanity checks.
[plomrogue] / windows.c
1 #include <stdlib.h>
2 #include <ncurses.h>
3 #include <string.h>
4 #include "windows.h"
5
6 struct WinMeta init_win_meta (WINDOW * screen) {
7 // Create and populate WinMeta struct with sane default values.
8   struct WinMeta win_meta;
9   win_meta.screen = screen;
10   win_meta.height = getmaxy(screen);
11   win_meta.width = getmaxx(screen);
12   win_meta.chain_start = 0;
13   win_meta.chain_end = 0;
14   win_meta.pad_offset = 0;
15   win_meta.pad = newpad(win_meta.height, 1);
16   return win_meta; }
17
18 void scroll_pad (struct WinMeta * win_meta, char dir) {
19 // Scroll pad left (if possible) or right.
20   if      ('+' == dir)
21     win_meta->pad_offset++;
22   else if ('-' == dir && win_meta->pad_offset > 0)
23     win_meta->pad_offset--; }
24
25 struct Win init_window (struct WinMeta * win_meta, char * title) {
26 // Create and populate Win struct with sane default values.
27   struct Win win;
28   win.prev = 0;
29   win.next = 0;
30   win.curses = 0;
31   win.title = title;
32   win.width = 20;
33   win.height = win_meta->height - 1;
34   return win; }
35
36 void append_window (struct WinMeta * win_meta, struct Win * win) {
37 // Append win to window chain. Set active, if first window. Update geometry of windows from new window on.
38   if (0 != win_meta->chain_start) {
39     win->prev = win_meta->chain_end;
40     win_meta->chain_end->next = win; }
41   else {
42     win_meta->active = win;
43     win_meta->chain_start = win; }
44   win_meta->chain_end = win;
45   update_windows(win_meta, win); }
46
47 void suspend_window (struct WinMeta * win_meta, struct Win * win) {
48 // Destroy win, suspend from window chain. Update geometry of following rows, as well as activity selection.
49   destroy_window(win);
50   if (win_meta->chain_start != win)    // Give win's position in the chain to element next to it in the chain.
51     win->prev->next = win->next;
52   else
53     win_meta->chain_start = win->next;
54   if (win_meta->chain_end != win) {                 // Let chain element next to win know its new predecessor.
55     win->next->prev = win->prev;
56     if (win_meta->active == win)                          // If win was active, shift active window pointer to
57       win_meta->active = win->next;                       // the next chain element, if that is a window ...
58     update_windows(win_meta, win->next); }
59   else {
60     win_meta->chain_end = win->prev;
61     if (win_meta->active == win)                                       // ... or else to the previous element.
62       win_meta->active = win->prev; }
63   win->prev = 0;
64   win->next = 0; }
65
66 struct yx place_window (struct WinMeta * win_meta, struct Win * win) {
67 // Based on position and sizes of previous window, find fitting place for current window.
68   struct yx start;
69   start.x = 0;                                     // if window is first in chain, place it on top-left corner
70   start.y = 1;
71   if (0 != win->prev) {
72     struct Win * win_top = win->prev;
73     while (getbegy(win_top->curses) != 1)
74       win_top = win_top->prev;                                   // else, default to placing window in new top
75     start.x = getbegx(win_top->curses) + win_top->width + 1;     // column to the right of the last one
76     int winprev_maxy = getbegy(win->prev->curses) + getmaxy(win->prev->curses);
77     if (win->width <= win->prev->width && win->height < win_meta->height - winprev_maxy) {
78       start.x = getbegx(win->prev->curses);                // place window below previous window if it fits
79       start.y = winprev_maxy + 1; }                        // vertically and is not wider than its predecessor
80     else {
81       struct Win * win_up = win->prev;
82       struct Win * win_upup = win_up;
83       int widthdiff;
84       while (win_up != win_top) {
85         win_upup = win_up->prev;
86         while (1) {
87           if (getbegy(win_up->curses) != getbegy(win_upup->curses))
88             break;
89           win_upup = win_upup->prev; }
90         winprev_maxy = getbegy(win_upup->curses) + getmaxy(win_upup->curses);
91         widthdiff = (getbegx(win_upup->curses) + win_upup->width) - (getbegx(win_up->curses) + win_up->width);
92         if (win->height < win_meta->height - winprev_maxy && win->width < widthdiff) {
93           start.x = getbegx(win_up->curses) + win_up->width + 1; // else try to open new sub column under last
94           start.y = winprev_maxy + 1;                            // window below which enough space remains
95           break; }
96         win_up = win_upup; } } }
97   return start; }
98
99 void update_windows (struct WinMeta * win_meta, struct Win * win) {
100 // Update geometry of win and its next of kin. Destroy (if visible), (re-)build window. If need, resize pad.
101   if (0 != win->curses)
102     destroy_window (win);
103   struct yx startyx = place_window(win_meta, win);
104   int lastwincol = 0;
105   struct Win * win_p = win_meta->chain_start;
106   while (win_p != 0) {
107     if (win_p != win && getbegx(win_p->curses) + win_p->width > lastwincol + 1)
108       lastwincol = getbegx(win_p->curses) + win_p->width - 1;
109     else if (win_p == win && startyx.x + win->width > lastwincol + 1)
110       lastwincol = startyx.x + win->width - 1;
111     win_p = win_p->next; }
112   if (getmaxx(win_meta->pad) != lastwincol) {
113     wresize(win_meta->pad, getmaxy(win_meta->pad), lastwincol + 2); }
114   win->curses = subpad(win_meta->pad, win->height, win->width, startyx.y, startyx.x);
115   if (0 != win->next)
116     update_windows (win_meta, win->next); }
117
118 void destroy_window (struct Win * win) {
119 // Delete window.
120   delwin(win->curses);
121   win->curses = 0; }
122
123 void draw_window_borders (struct Win * win, char active) {
124 // Draw borders of window win, including title. Decorate in a special way if window is marked as active.
125   int y, x;
126   for (y = getbegy(win->curses); y <= getbegy(win->curses) + win->height; y++) {
127     mvwaddch(wgetparent(win->curses), y, getbegx(win->curses) - 1, '|');
128     mvwaddch(wgetparent(win->curses), y, getbegx(win->curses) + win->width, '|'); }
129   for (x = getbegx(win->curses); x <= getbegx(win->curses) + win->width; x++) {
130     mvwaddch(wgetparent(win->curses), getbegy(win->curses) - 1, x, '-');
131     mvwaddch(wgetparent(win->curses), getbegy(win->curses) + win->height, x, '-'); }
132   char min_title_length_visible = 3;        // 1 char minimal, plus 2 chars for decoration left/right of title
133   if (win->width >= min_title_length_visible) {
134     int title_offset = 0;
135     if (win->width > strlen(win->title) + 2)
136       title_offset = (win->width - (strlen(win->title) + 2)) / 2;                     // + 2 is for decoration
137     int length_visible = strnlen(win->title, win->width - 2);
138     char title[length_visible + 3];
139     char decoration = ' ';
140     if (1 == active)
141       decoration = '$';
142     memcpy(title + 1, win->title, length_visible);
143     title[0] = title[length_visible + 1] = decoration;
144     title[length_visible + 2] = '\0';
145     mvwaddstr (wgetparent(win->curses), getbegy(win->curses)-1, getbegx(win->curses)+title_offset, title); } }
146
147 void draw_windows_borders (struct Win * win, struct Win * win_active, struct Corners * corners, int ccount) {
148 // Craw draw_window_borders() for all windows in chain from win on. Save current window's border corners.
149   char active = 0;
150   if (win == win_active)
151     active = 1;
152    draw_window_borders(win, active);
153   corners[ccount].tl.y = getbegy(win->curses) - 1;
154   corners[ccount].tl.x = getbegx(win->curses) - 1;
155   corners[ccount].tr.y = getbegy(win->curses) - 1;
156   corners[ccount].tr.x = getbegx(win->curses) + win->width;
157   corners[ccount].bl.y = getbegy(win->curses) + win->height;
158   corners[ccount].bl.x = getbegx(win->curses) - 1;
159   corners[ccount].br.y = getbegy(win->curses) + win->height;
160   corners[ccount].br.x = getbegx(win->curses) + win->width;
161   if (0 != win->next) {
162     draw_windows_borders (win->next, win_active, corners, ccount + 1); } }
163
164 void draw_windows (struct Win * win) {
165 // Draw contents of all windows in window chain from win on.
166   win->draw(win);
167   if (0 != win->next) {
168     draw_windows (win->next); } }
169
170 void draw_all_windows (struct WinMeta * win_meta) {
171 // Draw all windows and their borders.
172   erase();
173   wnoutrefresh(win_meta->screen);
174   werase(win_meta->pad);
175   if (win_meta->chain_start) {
176     int n_wins = 1;
177     struct Win * win_p = win_meta->chain_start;
178     while (0 != win_p->next) {
179       win_p = win_p->next;
180       n_wins++; }
181     struct Corners * all_corners = malloc(sizeof(struct Corners) * n_wins);
182     draw_windows (win_meta->chain_start);
183     draw_windows_borders (win_meta->chain_start, win_meta->active, all_corners, 0);
184     int i;
185     for (i = 0; i < n_wins; i++) {
186       mvwaddch(win_meta->pad, all_corners[i].tl.y, all_corners[i].tl.x, '+');
187       mvwaddch(win_meta->pad, all_corners[i].tr.y, all_corners[i].tr.x, '+');
188       mvwaddch(win_meta->pad, all_corners[i].bl.y, all_corners[i].bl.x, '+');
189       mvwaddch(win_meta->pad, all_corners[i].br.y, all_corners[i].br.x, '+'); }
190     pnoutrefresh(win_meta->pad, 0, win_meta->pad_offset, 0, 0, win_meta->height, win_meta->width - 1);
191     free(all_corners); }
192   doupdate(); }
193
194 void resize_window (struct WinMeta * win_meta, char change) {
195 // Grow or shrink currently active window. Correct its geometry and that of its followers.
196   if      (change == '-' && win_meta->active->height > 1)
197       win_meta->active->height--;
198   else if (change == '+' && win_meta->active->height < win_meta->height - 1)
199     win_meta->active->height++;
200   else if (change == '_' && win_meta->active->width > 1)
201       win_meta->active->width--;
202   else if (change == '*')
203     win_meta->active->width++;
204   update_windows(win_meta, win_meta->chain_start); }
205
206 void cycle_active_window (struct WinMeta * win_meta, char dir) {
207 // Cycle active window selection forwards (dir = 'n') or backwards.
208   if (0 != win_meta->active) {
209     if ('n' == dir) {
210       if (win_meta->active->next != 0)
211         win_meta->active = win_meta->active->next;
212       else
213         win_meta->active = win_meta->chain_start; }
214     else {
215       if (win_meta->active->prev != 0)
216         win_meta->active = win_meta->active->prev;
217       else
218         win_meta->active = win_meta->chain_end; } } }
219
220 void shift_window (struct WinMeta * win_meta, char dir) {
221 // Move active window forward/backward in window chain. If jumping beyond start/end, move to other chain end.
222   if (0 != win_meta->active && win_meta->chain_start != win_meta->chain_end && (dir == 'f' || dir == 'b')) {
223     int i, i_max;
224     struct Win * win_shift = win_meta->active;
225     char wrap = 0;
226     if ((dir == 'f' && win_shift == win_meta->chain_end)
227         || (dir == 'b' && win_shift == win_meta->chain_start))
228       wrap = 1;
229     struct Win * win_p, * win_p_next;
230     for (i_max = 1, win_p = win_meta->chain_start; win_p != win_meta->chain_end; i_max++)
231       win_p = win_p->next;
232     struct Win ** wins = malloc(i_max * sizeof(struct Win *));
233     for (i = 0, win_p = win_meta->chain_start; i < i_max; i++) {
234       win_p_next = win_p->next;
235       suspend_window(win_meta, win_p);
236       wins[i] = win_p;
237       win_p = win_p_next; }
238     if (wrap)
239       if (dir == 'f') {
240         append_window(win_meta, win_shift);
241         for (i = 0; i < i_max - 1; i++)
242           append_window(win_meta, wins[i]); }
243       else {
244         for (i = 1; i < i_max; i++)
245           append_window(win_meta, wins[i]);
246         append_window(win_meta, win_shift); }
247     else
248       for (i = 0; i < i_max; i++)
249         if ((dir == 'f' && win_shift == wins[i]) || (dir == 'b' && win_shift == wins[i+1])) {
250           append_window(win_meta, wins[i+1]);
251           append_window(win_meta, wins[i]);
252           i++; }
253         else
254           append_window(win_meta, wins[i]);
255     free(wins);
256     win_meta->active = win_shift; } }