home · contact · privacy
Restructured order of draw_all_windows() calls, heavily reduced number of superfluous...
[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;
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 void 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   win->start_x = 0; // if window is first in chain, place it on top-left corner
58   win->start_y = 0;
59   if (0 != win->prev) {
60     if (win->prev->height == win_meta->height)               // if prev window fills entire column, place win
61       win->start_x = win->prev->start_x + win->prev->width;  // in new column next to it
62     else {
63       struct Win * first_ceiling = win->prev;                        // first_ceiling determines column with;
64       while (first_ceiling->start_y != 0)                            // default: place window in new column
65         first_ceiling = first_ceiling->prev;                         // next to it
66       win->start_x = first_ceiling->start_x + first_ceiling->width;
67       if (first_ceiling->width >= win->width) {   // only place windows in prev column that fit into its width
68         struct Win * win_p = first_ceiling;
69         struct Win * lastrow_startwin = win_p;
70         while (win_p != win) {
71           if (win_p->start_x == first_ceiling->start_x)
72             lastrow_startwin = win_p;                              // try to fit window at the end of the last
73           win_p = win_p ->next; }                                  // row of windows inside the column; if
74         int lastcol_start = win->prev->start_x + win->prev->width; // that fails, try to open a new row below
75         if (win->width <= first_ceiling->start_x + first_ceiling->width - lastcol_start
76             && win->height <= lastrow_startwin->height) {
77           win->start_x = lastcol_start;
78           win->start_y = lastrow_startwin->start_y; }
79         else if (win->height <= win_meta->height - (lastrow_startwin->start_y + lastrow_startwin->height)
80                  && win->width <= first_ceiling->width) {
81           win->start_x = first_ceiling->start_x;
82           win->start_y = lastrow_startwin->start_y + lastrow_startwin->height; } } } } }
83
84 void update_windows (struct WinMeta * win_meta, struct Win * win) {
85 // Update geometry of win and its next of kin. Before, destroy window, if visible. After, (re-)build it.
86   if (0 != win->curses_win)
87     destroy_window (win);
88   place_window(win_meta, win);
89   if (win->start_y + win->height < win_meta->height) // dependent on window position,
90     win->border_down = 1;                            // append space for borders to be drawn
91   else
92     win->border_down = 0;
93   if (win->start_x > 0)
94     win->border_left = 1;
95   else
96     win->border_left = 0;
97   win->curses_win = newwin(win->height + win->border_down, win->width + win->border_left, win->start_y, win->start_x - win->border_left);
98   if (0 != win->next)
99     update_windows (win_meta, win->next); }
100
101 void destroy_window (struct Win * win) {
102 // Undraw and delete window.
103   undraw_window (win->curses_win);
104   delwin(win->curses_win);
105   win->curses_win = 0; }
106
107 void draw_windows (struct WinMeta * win_meta, struct Win * win) {
108 // Draw all windows from the current one on.
109   draw_window(win_meta, win);
110   if (0 != win->next)
111     draw_windows (win_meta, win->next); }
112
113 void draw_all_windows (struct WinMeta * win_meta) {
114 // Draw all windows from the chain start on.
115   if (win_meta->chain_start)
116     draw_windows (win_meta, win_meta->chain_start); }
117
118 void draw_window(struct WinMeta * win_meta, struct Win * win) {
119 // Draw win's content, including border and title (the latter dependent on space available for it).
120   char ls = '|';
121   char rs = '|';
122   char ts = '-';
123   char bs = '-';
124   char tl = '-';
125   char tr = '+';
126   char bl = '|';
127   char br = '|';
128   if (1 == win->border_down) {
129     bl = '+';
130     br = '+'; }
131   if (1 == win->border_left)
132     tl = '+';
133   wborder(win->curses_win, ls, rs, ts, bs, tl, tr, bl, br);
134   char min_title_length_visible = 3; // 1 char minimal, plus 2 chars for decoration left/right of title
135   if (win->width > min_title_length_visible) {
136     int title_length = strlen(win->title);
137     int title_offset = (((win->width) - (title_length + 2)) / 2) + win->border_left; // + 2 is for decoration
138     if (title_offset < win->border_left)
139       title_offset = win->border_left;
140     int length_visible = strnlen(win->title, win->width - min_title_length_visible);
141     char title[length_visible + 3];
142     char decoration = ' ';
143     if (win_meta->active == win)
144       decoration = '$';
145     memcpy(title + 1, win->title, length_visible);
146     title[0] = title[length_visible + 1] = decoration;
147     title[length_visible + 2] = '\0';
148     mvwaddstr(win->curses_win, 0, title_offset, title); }
149   if (win->height > 1 && win->width > 1) ;
150     win->draw(win);
151   wrefresh(win->curses_win); }
152
153 void undraw_window (WINDOW * win) {
154 // Fill entire window with whitespace.
155   int y, x;
156   for (y = 0; y <= win->_maxy; y++)
157     for (x = 0; x <= win->_maxx; x++)
158       mvwaddch(win, y, x, ' ');
159   wrefresh(win); }
160
161 void resize_window (struct WinMeta * win_meta, char change) {
162 // Grow or shrink currently active window. Correct its geometry and that of its followers.
163   if      (change == '-' && win_meta->active->height > 2)
164       win_meta->active->height--;
165   else if (change == '+' && win_meta->active->height < win_meta->height)
166     win_meta->active->height++;
167   else if (change == '_' && win_meta->active->width > 2)
168       win_meta->active->width--;
169   else if (change == '*')
170     win_meta->active->width++;
171   update_windows(win_meta, win_meta->chain_start); }
172
173 void cycle_active_window (struct WinMeta * win_meta, char dir) {
174 // Cycle active window selection forwards (dir = 'n') or backwards.
175   if ('n' == dir) {
176     if (win_meta->active->next != 0)
177       win_meta->active = win_meta->active->next;
178     else
179       win_meta->active = win_meta->chain_start; }
180   else {
181     if (win_meta->active->prev != 0)
182       win_meta->active = win_meta->active->prev;
183     else
184       win_meta->active = win_meta->chain_end; } }
185
186 void shift_window (struct WinMeta * win_meta, char dir) {
187 // Move active window forward/backward in window chain. If jumping beyond start/end, move to other chain end.
188   if (win_meta->chain_start != win_meta->chain_end && (dir == 'f' || dir == 'b')) {
189     int i, i_max;
190     struct Win * win_shift = win_meta->active;
191     char wrap = 0;
192     if ((dir == 'f' && win_shift == win_meta->chain_end)
193         || (dir == 'b' && win_shift == win_meta->chain_start))
194       wrap = 1;
195     struct Win * win_p, * win_p_next;
196     for (i_max = 1, win_p = win_meta->chain_start; win_p != win_meta->chain_end; i_max++, win_p = win_p->next);
197     struct Win ** wins = malloc(i_max * sizeof(struct Win *));
198     for (i = 0, win_p = win_meta->chain_start; i < i_max; i++) {
199       win_p_next = win_p->next;
200       suspend_window(win_meta, win_p);
201       wins[i] = win_p;
202       win_p = win_p_next; }
203     if (wrap)
204       if (dir == 'f') {
205         append_window(win_meta, win_shift);
206         for (i = 0; i < i_max - 1; i++)
207           append_window(win_meta, wins[i]); }
208       else {
209         for (i = 1; i < i_max; i++)
210           append_window(win_meta, wins[i]);
211         append_window(win_meta, win_shift); }
212     else
213       for (i = 0; i < i_max; i++)
214         if ((dir == 'f' && win_shift == wins[i]) || (dir == 'b' && win_shift == wins[i+1])) {
215           append_window(win_meta, wins[i+1]);
216           append_window(win_meta, wins[i]);
217           i++; }
218         else
219           append_window(win_meta, wins[i]);
220     free(wins);
221     win_meta->active = win_shift; } }