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