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