1 /* src/client/wincontrol.c
3 * This file is part of PlomRogue. PlomRogue is licensed under the GPL version 3
4 * or any later version. For details on its copyright, license, and warranties,
5 * see the file NOTICE in the root directory of the PlomRogue source package.
8 #include "wincontrol.h"
9 #include <ncurses.h> /* getmaxx(), getmaxy(), wresize() */
10 #include <stddef.h> /* NULL */
11 #include <stdint.h> /* uint8_t, uint32_t, UINT16_MAX */
12 #include <stdio.h> /* sprintf() */
13 #include <stdlib.h> /* free() */
14 #include <string.h> /* memcpy(), memset(), strchr(), strlen() */
15 #include "../common/rexit.h" /* exit_err(), exit_trouble() */
16 #include "../common/try_malloc.h" /* try_malloc() */
17 #include "windows.h" /* Win,yx_uint16, get_win_by_id(),get_win_pos_in_order() */
18 #include "world.h" /* global world */
22 /* Get Win before window identified by "c" or NULL if there is none. */
23 static struct Win * get_win_before(char c);
25 /* Make .v_screen just wide enough to contain all visible windows. */
26 static void refit_v_screen();
28 /* Update geometry (sizes, positions) of window "w" and its successors in the
29 * window chain. Use place_win() for the positioning algorithm.
31 static void update_wins(struct Win * w);
32 static void place_win(struct Win * w);
34 /* Write "win"'s size back to .target_(height/width) as per .target_*_type. */
35 static void set_win_target_size(struct Win * win);
37 /* Append/suspend window "w" to/from chain of visible windows. Appended windows
38 * will become active. Suspended active windows will move the active window
39 * selection to their successor in the window chain or, failing that, their
40 * predecessor, or, failing that, to 0 (no window active).
42 static void append_win(struct Win * w);
43 static void suspend_win(struct Win * w);
47 extern struct Win * get_win_before(char c)
49 uint8_t i = get_win_pos_in_order(c);
52 return get_win_by_id(world.winDB.order[i - 1]);
59 static void refit_v_screen()
61 /* Determine rightmost window column. */
62 uint32_t lastwcol = 0;
63 struct Win * wp = get_win_by_id(world.winDB.order[0]);
66 if ((uint32_t) wp->start.x + (uint32_t) wp->frame_size.x > lastwcol + 1)
68 lastwcol = (uint32_t) wp->start.x + (uint32_t) wp->frame_size.x - 1;
70 wp = get_win_after(wp->id);
73 /* Only resize .v_screen if the rightmost window column has changed. */
74 char * err_s = "refit_v_screen() grows virtual screen beyond legal sizes.";
75 char * err_m = "refit_v_screen() triggers memory alloc error in wresize().";
76 if ((uint32_t) getmaxx(world.winDB.v_screen) + 1 != lastwcol)
78 uint8_t t = (lastwcol + 2 > UINT16_MAX);
80 t = wresize(world.winDB.v_screen, getmaxy(world.winDB.v_screen),
88 static void update_wins(struct Win * w)
92 struct Win * next = get_win_after(w->id);
101 static void place_win(struct Win * w)
103 uint8_t sep = 1; /* Width of inter-window borders and title bars. */
105 /* If w is first window, it goes into the top left corner. */
107 w->start.y = 0 + sep;
108 struct Win * w_prev = get_win_before(w->id);
112 /* If not, get w's next predecessor starting a new stack on the screen
113 * top, fit w's top left corner to that predecessor's top right corner.
115 struct Win * w_top = w_prev;
116 for (; w_top->start.y != 0 + sep; w_top = get_win_before(w_top->id));
117 w->start.x = w_top->start.x + w_top->frame_size.x + sep;
119 /* If enough space is found below w's predecessor, fit w's top left
120 * corner to that predecessor's bottom left corner.
122 uint16_t next_free_y = w_prev->start.y + w_prev->frame_size.y + sep;
123 if ( w->frame_size.x <= w_prev->frame_size.x
124 && w->frame_size.y <= world.winDB.v_screen_size.y - next_free_y)
126 w->start.x = w_prev->start.x;
127 w->start.y = next_free_y;
131 /* If that fails, try to fit w's top left corner to the top right corner
132 * of its next predecessor w_test 1) below w_top (w's next predecessor
133 * starting a new stack on the screen top) 2) and the most rightward
134 * lower neighbor of a window w_high, itself throning over enough free
135 * space for w to fit below it, rightwards of its lower neighbor w_test.
137 struct Win * w_test = w_prev;
139 while (w_test != w_top)
141 for (w_high = get_win_before(w_test->id); /* Walk down chain */
142 w_test->start.y <= w_high->start.y; /* until w_high starts */
143 w_high = get_win_before(w_high->id)); /* higher than w_test. */
144 next_free_y = w_high->start.y + w_high->frame_size.y + sep;
145 uint16_t first_free_x = w_test->start.x + w_test->frame_size.x +sep;
146 uint16_t last_free_x = w_high->start.x + w_high->frame_size.x;
147 if ( w->frame_size.y <= world.winDB.v_screen_size.y - next_free_y
148 && w->frame_size.x <= last_free_x - first_free_x)
150 w->start.x = first_free_x;
151 w->start.y = next_free_y;
161 static void set_win_target_size(struct Win * w)
163 if (0 == w->target_height_type)
165 w->target_height = w->frame_size.y;
167 else if (1 == w->target_height_type)
169 w->target_height = w->frame_size.y - world.winDB.v_screen_size.y +1;
171 if (0 == w->target_width_type)
173 w->target_width = w->frame_size.x;
175 else if (1 == w->target_width_type)
177 w->target_width = w->frame_size.x - world.winDB.v_screen_size.x;
183 static void append_win(struct Win * w)
185 uint8_t old_size = strlen(world.winDB.order) + 1;
186 char * new_order = try_malloc(old_size + 1, __func__);
187 memcpy(new_order, world.winDB.order, old_size - 1);
188 new_order[old_size - 1] = w->id;
189 new_order[old_size] = '\0';
190 free(world.winDB.order);
191 world.winDB.order = new_order;
192 world.winDB.active = w->id;
198 static void suspend_win(struct Win * w)
200 uint8_t new_size = strlen(world.winDB.order);
201 char * new_order = try_malloc(new_size, __func__);
202 uint8_t i = get_win_pos_in_order(w->id);
203 char next_char = world.winDB.order[i + 1];
204 world.winDB.order[i] = '\0';
205 char * second_part = &world.winDB.order[i + 1];
206 int test = sprintf(new_order, "%s%s", world.winDB.order, second_part);
207 exit_trouble(test < 0, __func__, "sprintf");
208 free(world.winDB.order);
209 world.winDB.order = new_order;
210 world.winDB.active = world.winDB.order[i];
211 if (!world.winDB.order[i] && 0 < i)
213 world.winDB.active = world.winDB.order[i - 1];
215 if (world.winDB.order[i])
217 update_wins(get_win_by_id(next_char)); /* Already calls */
218 return; /* refit_v_screen(), so leave. */
225 extern void toggle_window(char id)
227 struct Win * win = get_win_by_id(id);
228 if (!strchr(world.winDB.order, id))
238 extern void toggle_winconfig()
240 if (!world.winDB.active)
244 struct Win * w = get_win_by_id(world.winDB.active);
248 w->target_center = w->center;
249 memset(&w->center, 0, sizeof(struct yx_uint16));
252 else if (1 == w->view)
259 w->center = w->target_center;
264 extern void toggle_win_size_type(char axis)
266 struct Win * w = get_win_by_id(world.winDB.active);
269 w->target_height_type = (0 == w->target_height_type);
270 set_win_target_size(w);
273 w->target_width_type = ( 0 == w->target_width_type
274 && w->frame_size.x <= world.winDB.v_screen_size.x);
275 set_win_target_size(w);
280 extern void toggle_linebreak_type()
282 struct Win * w = get_win_by_id(world.winDB.active);
283 if (0 == w->linebreak)
287 else if (1 == w->linebreak)
291 else if (2 == w->linebreak)
299 extern void resize_active_win(char change)
301 if (world.winDB.active)
303 struct Win * w = get_win_by_id(world.winDB.active);
304 if (change == '-' && w->frame_size.y > 1)
308 else if (change == '_' && w->frame_size.x > 1)
312 else if ( change == '+'
313 && w->frame_size.y < world.winDB.v_screen_size.y - 1)
317 else if (change == '*' && w->frame_size.y < UINT16_MAX)
321 if ( 1 == w->target_width_type
322 && w->frame_size.x > world.winDB.v_screen_size.x)
324 w->target_width_type = 0;
326 set_win_target_size(w);
333 extern void shift_active_win(char dir)
335 uint8_t len_order = strlen(world.winDB.order);
338 char * tmp = try_malloc(len_order + 1, __func__);
339 tmp[len_order] = '\0';
340 uint8_t pos = get_win_pos_in_order(world.winDB.active);
343 if (pos == len_order - 1)
345 memcpy(tmp + 1, world.winDB.order, len_order - 1);
346 tmp[0] = world.winDB.active;
347 memcpy(world.winDB.order, tmp, len_order + 1);
351 world.winDB.order[pos] = world.winDB.order[pos + 1];
352 world.winDB.order[pos + 1] = world.winDB.active;
359 memcpy(tmp, world.winDB.order + 1, len_order - 1);
360 tmp[len_order - 1] = world.winDB.active;
361 memcpy(world.winDB.order, tmp, len_order + 1);
365 world.winDB.order[pos] = world.winDB.order[pos - 1];
366 world.winDB.order[pos - 1] = world.winDB.active;
370 update_wins(get_win_by_id(world.winDB.order[0]));
376 extern void scroll_v_screen(char dir)
379 && world.winDB.v_screen_offset + world.winDB.v_screen_size.x + 1
380 < getmaxx(world.winDB.v_screen))
382 world.winDB.v_screen_offset++;
385 && world.winDB.v_screen_offset > 0)
387 world.winDB.v_screen_offset--;
393 extern void cycle_active_win(char dir)
395 uint8_t len_order = strlen(world.winDB.order);
398 uint8_t pos = get_win_pos_in_order(world.winDB.active);
401 world.winDB.active = world.winDB.order[pos + 1];
402 if ('\0' == world.winDB.active)
404 world.winDB.active = world.winDB.order[0];
410 world.winDB.active = world.winDB.order[pos - 1];
413 world.winDB.active = world.winDB.order[len_order - 1];