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