home · contact · privacy
Corrected broken if.
[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 = screen->_maxy + 1;
11   win_meta.width = screen->_maxx + 1;
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_win = 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     if (win->prev->height == win_meta->height - 1)                      // if prev window fills entire column,
66       start.x = getbegx(win->prev->curses_win) + win->prev->width + 1;  // place win in new column next to it
67     else {
68       struct Win * first_ceiling = win->prev;                         // first_ceiling determines column with;
69       while (getbegy(first_ceiling->curses_win) != 1)                 // default: place window in new column
70         first_ceiling = first_ceiling->prev;                          // next to it
71       start.x = getbegx(first_ceiling->curses_win) + first_ceiling->width + 1;
72       if (first_ceiling->width >= win->width) {      // only place wins in prev column that fit into its width
73         struct Win * win_p = first_ceiling;
74         struct Win * lastrow_startwin = win_p;
75         while (win_p != win) {
76           if (getbegx(win_p->curses_win) == getbegx(first_ceiling->curses_win))
77             lastrow_startwin = win_p;               // try to fit window at the end of the last row of windows
78           win_p = win_p ->next; }                   // inside column; if failure, try opening a new row below
79         int lastcol_start = getbegx(win->prev->curses_win) + win->prev->width + 1;
80         if (win->width <= getbegx(first_ceiling->curses_win) + first_ceiling->width - lastcol_start
81             && win->height <= lastrow_startwin->height) {
82           start.x = lastcol_start;
83           start.y = getbegy(lastrow_startwin->curses_win); }
84         else if (win->height < win_meta->height - (getbegy(lastrow_startwin->curses_win) + lastrow_startwin->height)
85                  && win->width <= first_ceiling->width) {
86           start.x = getbegx(first_ceiling->curses_win);
87           start.y = getbegy(lastrow_startwin->curses_win) + lastrow_startwin->height + 1; } } } }
88   return start; }
89
90 void update_windows (struct WinMeta * win_meta, struct Win * win) {
91 // Update geometry of win and its next of kin. Destroy (if visible), (re-)build window. If need, resize pad.
92   if (0 != win->curses_win)
93     destroy_window (win);
94   struct yx startyx = place_window(win_meta, win);
95   int lastwincol = 0;
96   struct Win * win_p = win_meta->chain_start;
97   while (win_p != 0) {
98     if (win_p != win && getbegx(win_p->curses_win) + win_p->width > lastwincol + 1)
99       lastwincol = getbegx(win_p->curses_win) + win_p->width - 1;
100     else if (win_p == win && startyx.x + win->width > lastwincol + 1)
101       lastwincol = startyx.x + win->width - 1;
102     win_p = win_p->next; }
103   if (getmaxx(win_meta->pad) != lastwincol) {
104     wresize(win_meta->pad, getmaxy(win_meta->pad), lastwincol + 2); }
105   win->curses_win = subpad(win_meta->pad, win->height, win->width, startyx.y, startyx.x);
106   if (0 != win->next)
107     update_windows (win_meta, win->next); }
108
109 void destroy_window (struct Win * win) {
110 // Delete window.
111   delwin(win->curses_win);
112   win->curses_win = 0; }
113
114 void draw_window_borders (struct Win * win, char active) {
115 // Draw borders of window win, including title. Decorate in a special way if window is marked as active.
116   int y, x;
117   for (y = getbegy(win->curses_win); y <= getbegy(win->curses_win) + win->height; y++) {
118     mvwaddch(wgetparent(win->curses_win), y, getbegx(win->curses_win) - 1, '|');
119     mvwaddch(wgetparent(win->curses_win), y, getbegx(win->curses_win) + win->width, '|'); }
120   for (x = getbegx(win->curses_win); x <= getbegx(win->curses_win) + win->width; x++) {
121     mvwaddch(wgetparent(win->curses_win), getbegy(win->curses_win) - 1, x, '-');
122     mvwaddch(wgetparent(win->curses_win), getbegy(win->curses_win) + win->height, x, '-'); }
123   char min_title_length_visible = 3; // 1 char minimal, plus 2 chars for decoration left/right of title
124   if (win->width > min_title_length_visible) {
125     int title_length = strlen(win->title);
126     int title_offset = (((win->width) - (title_length + 2)) / 2); // + 2 is for decoration
127     int length_visible = strnlen(win->title, win->width - min_title_length_visible);
128     char title[length_visible + 3];
129     char decoration = ' ';
130     if (1 == active)
131       decoration = '$';
132     memcpy(title + 1, win->title, length_visible);
133     title[0] = title[length_visible + 1] = decoration;
134     title[length_visible + 2] = '\0';
135     mvwaddstr(wgetparent(win->curses_win), getbegy(win->curses_win) - 1, getbegx(win->curses_win) + title_offset, title); } }
136
137 void draw_windows_borders (struct Win * win, struct Win * win_active, struct Corners * corners, int ccount) {
138 // Craw draw_window_borders() for all windows in chain from win on. Save current window's border corners.
139   char active = 0;
140   if (win == win_active)
141     active = 1;
142    draw_window_borders(win, active);
143   corners[ccount].tl.y = getbegy(win->curses_win) - 1;
144   corners[ccount].tl.x = getbegx(win->curses_win) - 1;
145   corners[ccount].tr.y = getbegy(win->curses_win) - 1;
146   corners[ccount].tr.x = getbegx(win->curses_win) + win->width;
147   corners[ccount].bl.y = getbegy(win->curses_win) + win->height;
148   corners[ccount].bl.x = getbegx(win->curses_win) - 1;
149   corners[ccount].br.y = getbegy(win->curses_win) + win->height;
150   corners[ccount].br.x = getbegx(win->curses_win) + win->width;
151   if (0 != win->next) {
152     draw_windows_borders (win->next, win_active, corners, ccount + 1); } }
153
154 void draw_window(struct Win * win) {
155 // Draw window content if visible.
156   if (win->height > 1 && win->width > 1)
157     win->draw(win); }
158
159 void draw_windows (struct Win * win) {
160 // Draw contents of all windows in window chain from win on.
161   draw_window(win);
162   if (0 != win->next) {
163     draw_windows (win->next); } }
164
165 void draw_all_windows (struct WinMeta * win_meta) {
166 // Draw all windows and their borders.
167   erase();
168   wnoutrefresh(win_meta->screen);
169   werase(win_meta->pad);
170   if (win_meta->chain_start) {
171     int n_wins = 1;
172     struct Win * win_p = win_meta->chain_start;
173     while (0 != win_p->next) {
174       win_p = win_p->next;
175       n_wins++; }
176     struct Corners * all_corners = malloc(sizeof(struct Corners) * n_wins);
177     draw_windows (win_meta->chain_start);
178     draw_windows_borders (win_meta->chain_start, win_meta->active, all_corners, 0);
179     int i;
180     for (i = 0; i < n_wins; i++) {
181       mvwaddch(win_meta->pad, all_corners[i].tl.y, all_corners[i].tl.x, '+');
182       mvwaddch(win_meta->pad, all_corners[i].tr.y, all_corners[i].tr.x, '+');
183       mvwaddch(win_meta->pad, all_corners[i].bl.y, all_corners[i].bl.x, '+');
184       mvwaddch(win_meta->pad, all_corners[i].br.y, all_corners[i].br.x, '+'); }
185     pnoutrefresh(win_meta->pad, 0, win_meta->pad_offset, 0, 0, win_meta->height, win_meta->width - 1);
186     free(all_corners); }
187   doupdate(); }
188
189 void resize_window (struct WinMeta * win_meta, char change) {
190 // Grow or shrink currently active window. Correct its geometry and that of its followers.
191   if      (change == '-' && win_meta->active->height > 1)
192       win_meta->active->height--;
193   else if (change == '+' && win_meta->active->height < win_meta->height - 1)
194     win_meta->active->height++;
195   else if (change == '_' && win_meta->active->width > 1)
196       win_meta->active->width--;
197   else if (change == '*')
198     win_meta->active->width++;
199   update_windows(win_meta, win_meta->chain_start); }
200
201 void cycle_active_window (struct WinMeta * win_meta, char dir) {
202 // Cycle active window selection forwards (dir = 'n') or backwards.
203   if ('n' == dir) {
204     if (win_meta->active->next != 0)
205       win_meta->active = win_meta->active->next;
206     else
207       win_meta->active = win_meta->chain_start; }
208   else {
209     if (win_meta->active->prev != 0)
210       win_meta->active = win_meta->active->prev;
211     else
212       win_meta->active = win_meta->chain_end; } }
213
214 void shift_window (struct WinMeta * win_meta, char dir) {
215 // Move active window forward/backward in window chain. If jumping beyond start/end, move to other chain end.
216   if (win_meta->chain_start != win_meta->chain_end && (dir == 'f' || dir == 'b')) {
217     int i, i_max;
218     struct Win * win_shift = win_meta->active;
219     char wrap = 0;
220     if ((dir == 'f' && win_shift == win_meta->chain_end)
221         || (dir == 'b' && win_shift == win_meta->chain_start))
222       wrap = 1;
223     struct Win * win_p, * win_p_next;
224     for (i_max = 1, win_p = win_meta->chain_start; win_p != win_meta->chain_end; i_max++, win_p = win_p->next);
225     struct Win ** wins = malloc(i_max * sizeof(struct Win *));
226     for (i = 0, win_p = win_meta->chain_start; i < i_max; i++) {
227       win_p_next = win_p->next;
228       suspend_window(win_meta, win_p);
229       wins[i] = win_p;
230       win_p = win_p_next; }
231     if (wrap)
232       if (dir == 'f') {
233         append_window(win_meta, win_shift);
234         for (i = 0; i < i_max - 1; i++)
235           append_window(win_meta, wins[i]); }
236       else {
237         for (i = 1; i < i_max; i++)
238           append_window(win_meta, wins[i]);
239         append_window(win_meta, win_shift); }
240     else
241       for (i = 0; i < i_max; i++)
242         if ((dir == 'f' && win_shift == wins[i]) || (dir == 'b' && win_shift == wins[i+1])) {
243           append_window(win_meta, wins[i+1]);
244           append_window(win_meta, wins[i]);
245           i++; }
246         else
247           append_window(win_meta, wins[i]);
248     free(wins);
249     win_meta->active = win_shift; } }