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