home · contact · privacy
Slightly re-structured and re-styled a for-loop for greater understandability.
[plomrogue] / src / windows.c
1 /* windows.c */
2
3 #include "windows.h"
4 #include <stdint.h>    /* for uint16_t, uint32_t */
5 #include <ncurses.h>   /* for LOTS of stuff */
6 #include <stdlib.h>    /* for malloc(), free() */
7 #include <string.h>    /* for strlen(), memcpy() */
8 #include "yx_uint16.h" /* for yx_uint16 coordinates */
9
10
11
12 /* Stores a window's border corners. This is a helper to draw_all_wins() (and
13  * filled by its helper draw_wins_borders()) which draws the horizontal and
14  * vertical lines of all windows' borders first and the corner characters of
15  * all windows only afterwards (so that corners are not overwritten by lines).
16  * This delay of corner drawing necessitates temporarily storing their
17  * coordinates (harvested during the previous border drawing activities) in a
18  * series of such Corners structs to be released at the end.
19  *
20  * TODO: Maybe replace this complicated method by dropping the harvesting of
21  * corners from draw_wins_borders() and instead collecting them in a second
22  * border drawing cycle that repeats some cycles but works in a much more
23  * straightforward way.
24  */
25 struct Corners
26 {
27     struct yx_uint16 tl;
28     struct yx_uint16 tr;
29     struct yx_uint16 bl;
30     struct yx_uint16 br;
31 };
32
33
34
35 /* Fit virtual screen's width to minimum width demanded by current windows'
36  * geometries.
37  */
38 static void refit_pad(struct WinMeta * wmeta);
39
40
41
42 /* Update geometry (sizes, positions) of window "w" and its successors in the
43  * window chain. For the positioning algorithm place_win() is used.
44  */
45 static void update_wins(struct WinMeta * wmeta, struct Win * w);
46 static void place_win(struct WinMeta * wmeta, struct Win * w);
47
48
49
50 /* Destroy window "w"'s ncurses window (and set w.Frame.curses_win to 0). */
51 static void destroy_win(struct Win * w);
52
53
54
55 /* Draw contents of all windows in window chain from window "w" onwards. */
56 static void draw_wins(struct Win * w);
57
58
59
60 /* draw_win_borders() Draws the vertical and horizontal borders of window "w"
61  * sans corners, and draws the top border line as the windows' title bar
62  * (highlighted if the window is described active by "active" being set).
63  * draw_wins_borders().
64  *
65  * draw_wins_borders() calls draw_win_borders() recursively on all windows from
66  * "w" on. It also fills "corners" with coordinates of each window's corners,
67  * iterating over its Corners structs via the "i" index incremented by 1 over
68  * each handled window. "w_active" is a pointer to the one window that
69  * draw_win_borders() is supposed to handle as the active window.
70  */
71 static void draw_win_borders(struct Win * w, char active);
72 static void draw_wins_borders(struct Win * w, struct Win * w_active,
73                               struct Corners * corners, uint16_t i);
74
75
76
77 static void refit_pad(struct WinMeta * wmeta)
78 {
79     /* Determine rightmost window column. */
80     uint16_t lastwincol = 0;
81     struct Win * w_p = wmeta->chain_start;
82     while (w_p != 0)
83     {
84         if (w_p->start.x + w_p->frame.size.x > lastwincol + 1)
85         {
86             lastwincol = w_p->start.x + w_p->frame.size.x - 1;
87         }
88         w_p = w_p->next;
89     }
90
91     /* Only resize the pad if the rightmost window column has changed. */
92     if (getmaxx(wmeta->pad.curses_win) != lastwincol)
93     {
94         wresize(wmeta->pad.curses_win,
95                 getmaxy(wmeta->pad.curses_win), lastwincol + 2);
96     }
97 }
98
99
100
101 static void update_wins (struct WinMeta * wmeta, struct Win * w)
102 {
103     if (0 != w->frame.curses_win)
104     {
105         destroy_win (w);
106     }
107     place_win(wmeta, w);
108     refit_pad(wmeta);
109     w->frame.curses_win = subpad(wmeta->pad.curses_win,
110                                  w->frame.size.y, w->frame.size.x,
111                                  w->start.y, w->start.x);
112     if (0 != w->next)
113     {
114         update_wins (wmeta, w->next);
115     }
116 }
117
118
119
120 static void place_win (struct WinMeta * wmeta, struct Win * w)
121 {
122     /* First window goes into the upper-left corner. */
123     w->start.x = 0;
124     w->start.y = 1;                             /* Leave space for title bar. */
125     if (0 != w->prev)
126     {
127
128         /* Non-first window fallbacks to: fit rightwards of rightmost border. */
129         struct Win * w_top = w->prev;
130         while (w_top->start.y != 1)
131         {
132             w_top = w_top->prev;
133         }
134         w->start.x = w_top->start.x + w_top->frame.size.x + 1;
135
136         /* Fit window below its predecessor if that one directly thrones over
137          * empty space wide and high enough.
138          */
139         uint16_t w_prev_maxy = w->prev->start.y
140                                + getmaxy(w->prev->frame.curses_win);
141         if (   w->frame.size.x <= w->prev->frame.size.x
142             && w->frame.size.y <  wmeta->pad.size.y - w_prev_maxy)
143         {
144             w->start.x = w->prev->start.x;
145             w->start.y = w_prev_maxy + 1;
146         }
147
148         /* Failing that, try to open a new sub column below the nearest
149          * predecessor window that thrones over enough empty space.
150          */
151         else
152         {
153             struct Win * w_up = w->prev;
154             struct Win * w_upup = w_up;
155             uint16_t widthdiff;
156             while (w_up != w_top)
157             {
158                 w_upup = w_up->prev;
159                 while (1)
160                 {
161                     if (w_up->start.y != w_upup->start.y)
162                     {
163                         break;
164                     }
165                     w_upup = w_upup->prev;
166                 }
167                 w_prev_maxy = w_upup->start.y
168                               + getmaxy(w_upup->frame.curses_win);
169                 widthdiff = (w_upup->start.x + w_upup->frame.size.x)
170                             - (w_up->start.x + w_up->frame.size.x);
171                 if (   w->frame.size.y < wmeta->pad.size.y - w_prev_maxy
172                     && w->frame.size.x < widthdiff)
173                 {
174                     w->start.x = w_up->start.x + w_up->frame.size.x + 1 ;
175                     w->start.y = w_prev_maxy + 1;
176                     break;
177                 }
178                 w_up = w_upup;
179             }
180         }
181     }
182 }
183
184
185
186 static void destroy_win (struct Win * w)
187 {
188     delwin(w->frame.curses_win);
189     w->frame.curses_win = 0;
190 }
191
192
193
194 static void draw_wins (struct Win * w)
195 {
196     w->draw(w);
197     if (0 != w->next)
198     {
199         draw_wins (w->next);
200     }
201 }
202
203
204
205 static void draw_win_borders(struct Win * w, char active)
206 {
207     /* Draw vertical and horizontal border lines. */
208     uint16_t y, x;
209     for (y = w->start.y; y <= w->start.y + w->frame.size.y; y++)
210     {
211         mvwaddch(wgetparent(w->frame.curses_win), y, w->start.x - 1, '|');
212         mvwaddch(wgetparent(w->frame.curses_win),
213                  y, w->start.x + w->frame.size.x, '|');
214     }
215     for (x = w->start.x; x <= w->start.x + w->frame.size.x; x++)
216     {
217         mvwaddch(wgetparent(w->frame.curses_win), w->start.y - 1, x, '-');
218         mvwaddch(wgetparent(w->frame.curses_win),
219                  w->start.y + w->frame.size.y, x, '-');
220     }
221
222     /* Draw as much as possible of the title into center of top border line. */
223     char min_title_length_visible = 3;  /* min. 1 char + 2 padding/decoration */
224     if (w->frame.size.x >= min_title_length_visible)
225     {
226         uint16_t title_offset = 0;
227         if (w->frame.size.x > strlen(w->title) + 2)
228         {
229             title_offset = (w->frame.size.x - (strlen(w->title) + 2)) / 2;
230         }                                     /* +2 is for padding/decoration */
231         uint16_t length_visible = strnlen(w->title, w->frame.size.x - 2);
232         char title[length_visible + 3];
233         char decoration = ' ';
234         if (1 == active)
235         {
236             decoration = '$';
237         }
238         memcpy(title + 1, w->title, length_visible);
239         title[0] = title[length_visible + 1] = decoration;
240         title[length_visible + 2] = '\0';
241         mvwaddstr(wgetparent(w->frame.curses_win),
242                   w->start.y - 1, w->start.x + title_offset, title);
243     }
244 }
245
246
247
248 static void draw_wins_borders(struct Win * w, struct Win * w_active,
249                               struct Corners * corners, uint16_t i)
250 {
251     char active = 0;
252     if (w == w_active)
253     {
254         active = 1;
255     }
256     draw_win_borders(w, active);
257     corners[i].tl.y = w->start.y - 1;
258     corners[i].tl.x = w->start.x - 1;
259     corners[i].tr.y = w->start.y - 1;
260     corners[i].tr.x = w->start.x + w->frame.size.x;
261     corners[i].bl.y = w->start.y + w->frame.size.y;
262     corners[i].bl.x = w->start.x - 1;
263     corners[i].br.y = w->start.y + w->frame.size.y;
264     corners[i].br.x = w->start.x + w->frame.size.x;
265     if (0 != w->next)
266     {
267         draw_wins_borders (w->next, w_active, corners, i + 1);
268     }
269 }
270
271
272
273 extern struct WinMeta init_win_meta(WINDOW * screen)
274 {
275     struct WinMeta wmeta;
276     wmeta.screen         = screen;
277     wmeta.pad.size.y     = getmaxy(screen);
278     wmeta.pad.size.x     = getmaxx(screen);
279     wmeta.chain_start    = 0;
280     wmeta.chain_end      = 0;
281     wmeta.pad_offset     = 0;
282     wmeta.pad.curses_win = newpad(wmeta.pad.size.y, 1);
283     wmeta.active         = 0;
284     return wmeta;
285 }
286
287
288
289 extern struct Win init_win(struct WinMeta * wmeta, char * title,
290                            void * data, void * func)
291 {
292     struct Win w;
293     w.prev             = 0;
294     w.next             = 0;
295     w.frame.curses_win = 0;
296     w.title            = title;
297     w.frame.size.x     = 20;
298     w.frame.size.y     = wmeta->pad.size.y - 1;
299     w.data             = data;
300     w.draw             = func;
301     return w;
302 }
303
304
305
306 extern void append_win(struct WinMeta * wmeta, struct Win * w)
307 {
308     if (0 != wmeta->chain_start)
309     {
310         w->prev = wmeta->chain_end;
311         wmeta->chain_end->next = w;
312     }
313     else
314     {
315         wmeta->active = w;
316         wmeta->chain_start = w;
317     }
318     wmeta->chain_end = w;
319     update_wins(wmeta, w);
320 }
321
322
323
324 extern void suspend_win(struct WinMeta * wmeta, struct Win * w)
325 {
326     destroy_win(w);
327
328     if (wmeta->chain_start != w)
329     {
330         w->prev->next = w->next;
331     }
332     else
333     {
334         wmeta->chain_start = w->next;
335     }
336     char pad_refitted = 0;
337     if (wmeta->chain_end != w)
338     {
339         w->next->prev = w->prev;
340         if (wmeta->active == w)
341         {
342             wmeta->active = w->next;
343         }
344         update_wins(wmeta, w->next); /* Positioning of successor windows may  */
345         pad_refitted = 1;            /* be affected / need correction. Note   */
346     }                                /* that update_wins() already refits the */
347     else                             /* pad, voiding later need for that.     */
348     {
349         wmeta->chain_end = w->prev;
350         if (wmeta->active == w)
351         {
352             wmeta->active = w->prev;
353         }
354     }
355
356     w->prev = 0;
357     w->next = 0;
358
359     if (0 == pad_refitted)
360     {
361         refit_pad(wmeta);
362     }
363 }
364
365
366
367 extern void reset_pad_offset(struct WinMeta * wmeta, uint16_t new_offset)
368 {
369     if (new_offset >= 0
370         && (new_offset < wmeta->pad_offset
371             || new_offset + wmeta->pad.size.x < getmaxx(wmeta->pad.curses_win)))
372     {
373         wmeta->pad_offset = new_offset;
374     }
375 }
376
377
378
379 extern void resize_active_win(struct WinMeta * wmeta, struct yx_uint16 size)
380 {
381     if (0 != wmeta->active
382         && size.x > 0 && size.y > 0
383         && size.y < wmeta->pad.size.y)
384     {
385         wmeta->active->frame.size = size;
386         update_wins(wmeta, wmeta->chain_start);   /* Positioning of successor */
387     }                                             /* windows may be affected. */
388 }
389
390
391
392 extern void cycle_active_win(struct WinMeta * wmeta, char dir)
393 {
394     if (0 != wmeta->active)
395     {
396         if ('n' == dir)
397         {
398             if (wmeta->active->next != 0)
399             {
400                 wmeta->active = wmeta->active->next;
401             }
402             else
403             {
404                 wmeta->active = wmeta->chain_start;
405             }
406         }
407         else
408         {
409             if (wmeta->active->prev != 0)
410             {
411                 wmeta->active = wmeta->active->prev;
412             }
413             else
414             {
415                 wmeta->active = wmeta->chain_end;
416             }
417         }
418     }
419 }
420
421
422
423 extern void shift_active_win(struct WinMeta * wmeta, char dir)
424 {
425     if (0 != wmeta->active                        /* No shifting with less    */
426         && wmeta->chain_start != wmeta->chain_end /* than one window visible. */
427         && (dir == 'f' || dir == 'b'))
428     {
429         struct Win * w_shift = wmeta->active, * w_p, * w_p_next;
430
431         /* Check if shifting will lead to wrapping. */
432         char wrap = 0;
433         if (   (dir == 'f' && w_shift == wmeta->chain_end)
434             || (dir == 'b' && w_shift == wmeta->chain_start))
435         {
436             wrap = 1;
437         }
438
439         /* Suspend all visible windows. */
440         uint16_t i, i_max;
441         for (w_p = wmeta->chain_start, i_max = 1;
442              w_p != wmeta->chain_end;
443              w_p = w_p->next)
444         {
445             i_max++;
446         }
447         struct Win ** wins = malloc(i_max * sizeof(struct Win *));
448         for (i = 0, w_p = wmeta->chain_start; i < i_max; i++)
449         {
450             w_p_next = w_p->next;
451             suspend_win(wmeta, w_p);
452             wins[i] = w_p;
453             w_p = w_p_next;
454         }
455
456         /* Re-append all previously visible windows in the new order. */
457         if (wrap)
458         {
459             if (dir == 'f')
460             {
461                 append_win(wmeta, w_shift);
462                 for (i = 0; i < i_max - 1; i++)
463                 {
464                     append_win(wmeta, wins[i]);
465                 }
466             }
467             else
468             {
469                 for (i = 1; i < i_max; i++)
470                 {
471                     append_win(wmeta, wins[i]);
472                 }
473                 append_win(wmeta, w_shift);
474             }
475         }
476         else
477         {
478             for (i = 0; i < i_max; i++)
479             {
480                 if (   (dir == 'f' && w_shift == wins[i])
481                     || (dir == 'b' && w_shift == wins[i+1]))
482                 {
483                     append_win(wmeta, wins[i+1]);
484                     append_win(wmeta, wins[i]);
485                     i++;
486                 }
487                 else
488                 {
489                     append_win(wmeta, wins[i]);
490                 }
491             }
492         }
493         free(wins);
494
495         wmeta->active = w_shift;      /* TODO: Is this necessary? If so, why? */
496     }
497 }
498
499
500
501 extern void draw_all_wins(struct WinMeta * wmeta)
502 {
503     /* Empty everything before filling it a-new. */
504     erase();
505     wnoutrefresh(wmeta->screen);
506     werase(wmeta->pad.curses_win);
507     if (wmeta->chain_start)
508     {
509
510         /* Only draw the windows' *contents* first. */
511         draw_wins (wmeta->chain_start);
512
513         /* Draw windows' borders. Lines first, then line crossings / corners. */
514         uint16_t n_wins = 1, i;
515         struct Win * win_p = wmeta->chain_start;
516         while (0 != win_p->next)
517         {
518             win_p = win_p->next;
519             n_wins++;
520         }
521         struct Corners * all_corners = malloc(sizeof(struct Corners) * n_wins);
522         draw_wins_borders (wmeta->chain_start, wmeta->active, all_corners, 0);
523         for (i = 0; i < n_wins; i++)
524         {
525             mvwaddch(wmeta->pad.curses_win,
526                      all_corners[i].tl.y, all_corners[i].tl.x, '+');
527             mvwaddch(wmeta->pad.curses_win,
528                      all_corners[i].tr.y, all_corners[i].tr.x, '+');
529             mvwaddch(wmeta->pad.curses_win,
530                      all_corners[i].bl.y, all_corners[i].bl.x, '+');
531             mvwaddch(wmeta->pad.curses_win,
532                      all_corners[i].br.y, all_corners[i].br.x, '+');
533         }
534         free(all_corners);
535
536         /* Draw virtual screen scroll hints. */
537         if (wmeta->pad_offset > 0)
538         {
539             draw_scroll_hint(&wmeta->pad,
540                              wmeta->pad_offset, wmeta->pad_offset + 1, '<');
541         }
542         if (wmeta->pad_offset + wmeta->pad.size.x
543             < getmaxx(wmeta->pad.curses_win) - 1)
544         {
545             draw_scroll_hint(&wmeta->pad,
546                              wmeta->pad_offset + wmeta->pad.size.x - 1,
547                              getmaxx(wmeta->pad.curses_win)
548                              - (wmeta->pad_offset + wmeta->pad.size.x), '>');
549         }
550
551         /* Write virtual screen segment to be shown on physical screen into */
552         /* ncurses screen buffer. */
553         pnoutrefresh(wmeta->pad.curses_win, 0, wmeta->pad_offset, 0, 0,
554                      wmeta->pad.size.y, wmeta->pad.size.x-1);
555     }
556
557     /* Only at the end write accumulated changes to the physical screen. */
558     doupdate();
559 }
560
561
562
563 extern void draw_scroll_hint(struct Frame * frame, uint16_t pos, uint32_t dist,
564                              char dir)
565 {
566     /* Decide on alignment (vertical/horizontal?), thereby scroll hint text. */
567     char * more = "more";
568     char * unit_cols = "columns";
569     char * unit_rows = "lines";
570     uint16_t dsc_space = frame->size.x;
571     char * unit = unit_rows;
572     if ('<' == dir || '>' == dir)
573     {
574         dsc_space = frame->size.y;
575         unit = unit_cols;
576     }
577     char * scrolldsc = malloc((4 * sizeof(char)) + strlen(more) + strlen(unit)
578                               + 10);                /* 10 = uint32 max strlen */
579     sprintf(scrolldsc, " %d %s %s ", dist, more, unit);
580
581     /* Decide on offset of the description text inside the scroll hint line. */
582     char offset = 1, q;
583     if (dsc_space > strlen(scrolldsc) + 1)
584     {
585         offset = (dsc_space - strlen(scrolldsc)) / 2;
586     }
587
588     /* Draw scroll hint line as dir symbols bracketing description text. */
589     chtype symbol;
590     for (q = 0; q < dsc_space; q++)
591     {
592         if (q >= offset && q < strlen(scrolldsc) + offset)
593         {
594             symbol = scrolldsc[q - offset] | A_REVERSE;
595         }
596         else
597         {
598             symbol = dir | A_REVERSE;
599         }
600         if ('<' == dir || '>' == dir)
601         {
602             mvwaddch(frame->curses_win, q, pos, symbol);
603         }
604         else
605         {
606             mvwaddch(frame->curses_win, pos, q, symbol);
607         }
608     }
609
610     free(scrolldsc);
611 }