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