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