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