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