home · contact · privacy
Moved allocation and freeing of Win structs into windows library.
[plomrogue] / src / windows.c
1 /* windows.c */
2
3 #include "windows.h"
4 #include <stdint.h>    /* for uint8_t, uint16_t, uint32_t, UINT16_MAX */
5 #include <ncurses.h>   /* for typedefs WINDOW, chtype, wresize(), getmaxx(), */
6                        /* getmaxy(), supbad(), delwin(), mvwaddch(),         */
7                        /* mvwaddstr(), newpad(), wnoutrefres(), erase(),     */
8                        /* werase(), pnoutrefresh(), doupdate()               */
9 #include <stdlib.h>    /* for malloc(), free() */
10 #include <string.h>    /* for strlen(), strnlen(), memcpy() */
11 #include "yx_uint16.h" /* for yx_uint16 coordinates */
12
13
14
15 /* Fit virtual screen's width to minimum width demanded by current windows'
16  * geometries.
17  */
18 static uint8_t refit_pad(struct WinMeta * wmeta);
19
20
21
22 /* Update geometry (sizes, positions) of window "w" and its successors in the
23  * window chain. For the positioning algorithm place_win() is used.
24  */
25 static uint8_t update_wins(struct WinMeta * wmeta, struct Win * w);
26 static void place_win(struct WinMeta * wmeta, struct Win * w);
27
28
29
30 /* Destroy window "w"'s ncurses WINDOW (and set w.Frame.curses_win to 0). */
31 static void destroy_win(struct Win * w);
32
33
34
35 /* Draw contents of all windows in window chain from window "w" onwards. */
36 static void draw_wins(struct Win * w);
37
38
39
40 /* draw_win_borderlines() draws the vertical and horizontal borders of window
41  * "w" sans corners into the virtual screen "pad", and draws the top border
42  * line as the windows' title bar (highlighted if the window is described
43  * active by "active" being == 1).
44  *
45  * draw_wins_borderlines() calls draw_win_borderlines() recursively on all
46  * windows from "w" on. "w_active" is a pointer to the one window that
47  * draw_win_borderlines() is supposed to handle as the active window.
48  *
49  * Finally, draw_wins_bordercorners() draws into "pad" the borders of window "w"
50  * and all its successors.
51  */
52 static void draw_win_borderlines(struct Win * w, char active, WINDOW * pad);
53 static void draw_wins_borderlines(struct Win * w, struct Win * w_active,
54                                   WINDOW * pad);
55 static void draw_wins_bordercorners(struct Win * w, WINDOW * pad);
56
57
58
59 /* Shift active window forwards / backwards in window chain. */
60 static void shift_win_forward(struct WinMeta * wmeta);
61 static void shift_win_backward(struct WinMeta * wmeta);
62
63
64
65 static uint8_t refit_pad(struct WinMeta * wmeta)
66 {
67     /* Determine rightmost window column. */
68     uint32_t lastwincol = 0;
69     struct Win * w_p = wmeta->_chain_start;
70     while (w_p != 0)
71     {
72         if ((uint32_t) w_p->_start.x + (uint32_t) w_p->frame.size.x
73             > lastwincol + 1)
74         {
75             lastwincol = (uint32_t) w_p->_start.x
76                          + (uint32_t) w_p->frame.size.x - 1;
77         }
78         w_p = w_p->_next;
79     }
80
81     /* Only resize the pad if the rightmost window column has changed. */
82     if (getmaxx(wmeta->padframe.curses_win) != lastwincol)
83     {
84         if (lastwincol + 2 > UINT16_MAX)
85         {
86             return 2;
87         }
88         return (ERR == wresize(wmeta->padframe.curses_win,
89                                getmaxy(wmeta->padframe.curses_win),
90                                lastwincol + 2));
91     }
92     return 0;
93 }
94
95
96
97 static uint8_t update_wins(struct WinMeta * wmeta, struct Win * w)
98 {
99     if (0 != w->frame.curses_win)
100     {
101         destroy_win(w);
102     }
103     place_win(wmeta, w);
104     uint8_t test_refit = refit_pad(wmeta);
105     if (0 != test_refit)
106     {
107         return test_refit;
108     }
109     WINDOW * subpad_test = subpad(wmeta->padframe.curses_win,
110                                   w->frame.size.y, w->frame.size.x,
111                                   w->_start.y, w->_start.x);
112     if (NULL == subpad_test)
113     {
114         return 1;
115     }
116     w->frame.curses_win = subpad_test;
117     if (0 != w->_next)
118     {
119         return update_wins(wmeta, w->_next);
120     }
121     return 0;
122 }
123
124
125
126 static void place_win(struct WinMeta * wmeta, struct Win * w)
127 {
128     /* First window goes into the upper-left corner. */
129     w->_start.x = 0;
130     w->_start.y = 1;                            /* Leave space for title bar. */
131     if (0 != w->_prev)
132     {
133
134         /* Non-first window fallbacks to: fit rightwards of rightmost border. */
135         struct Win * w_top = w->_prev;
136         while (w_top->_start.y != 1)
137         {
138             w_top = w_top->_prev;
139         }
140         w->_start.x = w_top->_start.x + w_top->frame.size.x + 1;
141
142         /* Fit window below its predecessor if that one directly thrones over
143          * empty space wide and high enough.
144          */
145         uint16_t w_prev_maxy = w->_prev->_start.y
146                                + getmaxy(w->_prev->frame.curses_win);
147         if (   w->frame.size.x <= w->_prev->frame.size.x
148             && w->frame.size.y <  wmeta->padframe.size.y - w_prev_maxy)
149         {
150             w->_start.x = w->_prev->_start.x;
151             w->_start.y = w_prev_maxy + 1;
152         }
153
154         /* Failing that, try to open a new sub column below the nearest
155          * predecessor window that thrones over enough empty space.
156          */
157         else
158         {
159             struct Win * w_up = w->_prev;
160             struct Win * w_upup = w_up;
161             uint16_t widthdiff;
162             while (w_up != w_top)
163             {
164                 w_upup = w_up->_prev;
165                 while (1)
166                 {
167                     if (w_up->_start.y != w_upup->_start.y)
168                     {
169                         break;
170                     }
171                     w_upup = w_upup->_prev;
172                 }
173                 w_prev_maxy = w_upup->_start.y
174                               + getmaxy(w_upup->frame.curses_win);
175                 widthdiff = (w_upup->_start.x + w_upup->frame.size.x)
176                             - (w_up->_start.x + w_up->frame.size.x);
177                 if (   w->frame.size.y < wmeta->padframe.size.y - w_prev_maxy
178                     && w->frame.size.x < widthdiff)
179                 {
180                     w->_start.x = w_up->_start.x + w_up->frame.size.x + 1 ;
181                     w->_start.y = w_prev_maxy + 1;
182                     break;
183                 }
184                 w_up = w_upup;
185             }
186         }
187     }
188 }
189
190
191
192 static void destroy_win(struct Win * w)
193 {
194     delwin(w->frame.curses_win);
195     w->frame.curses_win = 0;
196 }
197
198
199
200 static void draw_wins(struct Win * w)
201 {
202     w->_draw(w);
203     if (0 != w->_next)
204     {
205         draw_wins(w->_next);
206     }
207 }
208
209
210
211 static void draw_win_borderlines(struct Win * w, char active, WINDOW * pad)
212 {
213     /* Draw vertical and horizontal border lines. */
214     uint16_t y, x;
215     for (y = w->_start.y; y <= w->_start.y + w->frame.size.y; y++)
216     {
217         mvwaddch(pad, y, w->_start.x - 1,               '|');
218         mvwaddch(pad, y, w->_start.x + w->frame.size.x, '|');
219     }
220     for (x = w->_start.x; x <= w->_start.x + w->frame.size.x; x++)
221     {
222         mvwaddch(pad, w->_start.y - 1,               x, '-');
223         mvwaddch(pad, w->_start.y + w->frame.size.y, x, '-');
224     }
225
226     /* Draw as much as possible of the title into center of top border line. */
227     char min_title_length_visible = 3;  /* min. 1 char + 2 padding/decoration */
228     if (w->frame.size.x >= min_title_length_visible)
229     {
230         uint16_t title_offset = 0;
231         if (w->frame.size.x > strlen(w->_title) + 2)
232         {
233             title_offset = (w->frame.size.x - (strlen(w->_title) + 2)) / 2;
234         }                                    /* +2 is for padding/decoration */
235         uint16_t length_visible = strnlen(w->_title, w->frame.size.x - 2);
236         char title[length_visible + 3];
237         char decoration = ' ';
238         if (1 == active)
239         {
240             decoration = '$';
241         }
242         memcpy(title + 1, w->_title, length_visible);
243         title[0] = title[length_visible + 1] = decoration;
244         title[length_visible + 2] = '\0';
245         mvwaddstr(pad, w->_start.y - 1, w->_start.x + title_offset, title);
246     }
247 }
248
249
250
251 static void draw_wins_borderlines(struct Win * w, struct Win * w_active,
252                                   WINDOW * pad)
253 {
254     char active = 0;
255     if (w == w_active)
256     {
257         active = 1;
258     }
259     draw_win_borderlines(w, active, pad);
260     if (0 != w->_next)
261     {
262         draw_wins_borderlines(w->_next, w_active, pad);
263     }
264 }
265
266
267
268 static void draw_wins_bordercorners(struct Win * w, WINDOW * pad)
269 {
270     mvwaddch(pad, w->_start.y - 1, w->_start.x - 1, '+');
271     mvwaddch(pad, w->_start.y - 1, w->_start.x + w->frame.size.x, '+');
272     mvwaddch(pad, w->_start.y + w->frame.size.y, w->_start.x - 1, '+');
273     mvwaddch(pad,
274              w->_start.y + w->frame.size.y, w->_start.x + w->frame.size.x, '+');
275     if (0 != w->_next)
276     {
277         draw_wins_bordercorners(w->_next, pad);
278     }
279 }
280
281
282
283 static void shift_win_forward(struct WinMeta * wmeta)
284 {
285     if (wmeta->active == wmeta->_chain_end)
286     {
287         wmeta->_chain_end = wmeta->active->_prev;
288         wmeta->_chain_end->_next = 0;
289         wmeta->active->_next = wmeta->_chain_start;
290         wmeta->active->_next->_prev = wmeta->active;
291         wmeta->_chain_start = wmeta->active;
292         wmeta->_chain_start->_prev = 0;
293     }
294     else
295     {
296         struct Win * old_prev = wmeta->active->_prev;
297         struct Win * old_next = wmeta->active->_next;
298         if (wmeta->_chain_end == wmeta->active->_next)
299         {
300             wmeta->_chain_end = wmeta->active;
301             wmeta->active->_next = 0;
302         }
303         else
304         {
305             wmeta->active->_next = old_next->_next;
306             wmeta->active->_next->_prev = wmeta->active;
307         }
308         if (wmeta->_chain_start == wmeta->active)
309         {
310             wmeta->_chain_start = old_next;
311         }
312         else
313         {
314             old_prev->_next = old_next;
315         }
316         old_next->_prev = old_prev;
317         old_next->_next = wmeta->active;
318         wmeta->active->_prev = old_next;
319     }
320 }
321
322
323
324 static void shift_win_backward(struct WinMeta * wmeta)
325 {
326     if (wmeta->active == wmeta->_chain_start)
327     {
328         wmeta->_chain_start = wmeta->active->_next;
329         wmeta->_chain_start->_prev = 0;
330         wmeta->active->_prev = wmeta->_chain_end;
331         wmeta->active->_prev->_next = wmeta->active;
332         wmeta->_chain_end = wmeta->active;
333         wmeta->_chain_end->_next = 0;
334     }
335     else
336     {
337         struct Win * old_prev = wmeta->active->_prev;
338         struct Win * old_next = wmeta->active->_next;
339         if (wmeta->_chain_start == wmeta->active->_prev)
340         {
341             wmeta->_chain_start = wmeta->active;
342             wmeta->active->_prev = 0;
343         }
344         else
345         {
346             wmeta->active->_prev = old_prev->_prev;
347             wmeta->active->_prev->_next = wmeta->active;
348         }
349         if (wmeta->_chain_end == wmeta->active)
350         {
351             wmeta->_chain_end = old_prev;
352         }
353         else
354         {
355             old_next->_prev = old_prev;
356         }
357         old_prev->_next = old_next;
358         old_prev->_prev = wmeta->active;
359         wmeta->active->_next = old_prev;
360     }
361 }
362
363
364
365 extern uint8_t init_win_meta(WINDOW * screen, struct WinMeta * wmeta)
366 {
367     wmeta->_screen             = screen;
368     uint32_t maxy_test         = getmaxy(screen);
369     uint32_t maxx_test         = getmaxx(screen);
370     if (maxy_test > UINT16_MAX || maxx_test > UINT16_MAX)
371     {
372         return 2;
373     }
374     wmeta->padframe.size.y     = maxy_test;
375     wmeta->padframe.size.x     = maxx_test;
376     wmeta->_chain_start        = 0;
377     wmeta->_chain_end          = 0;
378     wmeta->pad_offset          = 0;
379     WINDOW * pad_test          = newpad(wmeta->padframe.size.y, 1);
380     if (NULL == pad_test)
381     {
382         return 1;
383     }
384     wmeta->padframe.curses_win = pad_test;
385     wmeta->active              = 0;
386     return 0;
387 }
388
389
390
391 extern uint8_t init_win(struct WinMeta * wmeta, struct Win ** wp, char * title,
392                         int16_t height, int16_t width,
393                         void * data, void * func)
394 {
395     struct Win * w = malloc(sizeof(struct Win));
396     if (NULL == w)
397     {
398         return 1;
399     }
400     w->_prev             = 0;
401     w->_next             = 0;
402     w->frame.curses_win  = 0;
403     w->_title             = malloc(strlen(title) + 1);
404     if (NULL == w->_title)
405     {
406         return 1;
407     }
408     sprintf(w->_title, "%s", title);
409     w->data              = data;
410     w->_draw             = func;
411     if      (0 < width)
412     {
413         w->frame.size.x = width;
414     }
415     else if (0 > width)
416     {
417         w->frame.size.x = wmeta->padframe.size.x + width;
418     }
419     else
420     {
421         w->frame.size.x = wmeta->padframe.size.x;
422     }
423     if      (0 < height && height <= wmeta->padframe.size.y - 1)
424     {
425         w->frame.size.y = height;
426     }
427     else if (0 > height && wmeta->padframe.size.y + (height - 1) > 0)
428     {
429         w->frame.size.y = wmeta->padframe.size.y + (height - 1);
430     }
431     else
432     {
433         w->frame.size.y = wmeta->padframe.size.y - 1;
434     }
435     *wp = w;
436     return 0;
437 }
438
439
440
441 extern void free_win(struct Win * win)
442 {
443     free(win->_title);
444     free(win);
445 }
446
447
448
449 extern uint8_t append_win(struct WinMeta * wmeta, struct Win * w)
450 {
451     if (0 != wmeta->_chain_start)
452     {
453         w->_prev = wmeta->_chain_end;
454         wmeta->_chain_end->_next = w;
455     }
456     else
457     {
458         wmeta->active = w;
459         wmeta->_chain_start = w;
460     }
461     wmeta->_chain_end = w;
462     return update_wins(wmeta, w);
463 }
464
465
466
467 extern uint8_t suspend_win(struct WinMeta * wmeta, struct Win * w)
468 {
469     destroy_win(w);
470
471     if (wmeta->_chain_start != w)
472     {
473         w->_prev->_next = w->_next;
474     }
475     else
476     {
477         wmeta->_chain_start = w->_next;
478     }
479     char pad_refitted = 0;
480     if (wmeta->_chain_end != w)
481     {
482         w->_next->_prev = w->_prev;
483         if (wmeta->active == w)
484         {
485             wmeta->active = w->_next;
486         }
487         uint8_t test = update_wins(wmeta, w->_next);/* Positioning of         */
488         if (0 != test)                              /* successor windows may  */
489         {                                           /* be affected / need     */
490             return test;                            /* correction. Note that  */
491         }                                           /* update_wins() already  */
492         pad_refitted = 1;                           /* refits the pad, voiding*/
493     }                                               /* later need for that.   */
494     else
495     {
496         wmeta->_chain_end = w->_prev;
497         if (wmeta->active == w)
498         {
499             wmeta->active = w->_prev;
500         }
501     }
502
503     w->_prev = 0;
504     w->_next = 0;
505
506     if (0 == pad_refitted)
507     {
508         return refit_pad(wmeta);
509     }
510     return 0;
511 }
512
513
514
515 extern void reset_pad_offset(struct WinMeta * wmeta, uint16_t new_offset)
516 {
517     if (new_offset >= 0
518         && (new_offset < wmeta->pad_offset
519             || new_offset + wmeta->padframe.size.x
520                < getmaxx(wmeta->padframe.curses_win)))
521     {
522         wmeta->pad_offset = new_offset;
523     }
524 }
525
526
527
528 extern uint8_t resize_active_win(struct WinMeta * wmeta, struct yx_uint16 size)
529 {
530     if (0 != wmeta->active
531         && size.x > 0 && size.y > 0
532         && size.y < wmeta->padframe.size.y)
533     {
534         wmeta->active->frame.size = size;
535         return update_wins(wmeta, wmeta->active); /* Positioning of following */
536     }                                             /* windows may be affected. */
537     return 0;
538 }
539
540
541
542 extern void cycle_active_win(struct WinMeta * wmeta, char dir)
543 {
544     if (0 != wmeta->active)
545     {
546         if ('f' == dir)
547         {
548             if (wmeta->active->_next != 0)
549             {
550                 wmeta->active = wmeta->active->_next;
551             }
552             else
553             {
554                 wmeta->active = wmeta->_chain_start;
555             }
556         }
557         else
558         {
559             if (wmeta->active->_prev != 0)
560             {
561                 wmeta->active = wmeta->active->_prev;
562             }
563             else
564             {
565                 wmeta->active = wmeta->_chain_end;
566             }
567         }
568     }
569 }
570
571
572
573 extern uint8_t shift_active_win(struct WinMeta * wmeta, char dir)
574 {
575     if (   0 == wmeta->active                        /* No shifting with < 2  */
576         || wmeta->_chain_start == wmeta->_chain_end) /* windows visible.      */
577     {
578         return 0;
579     }
580     if ('f' == dir)
581     {
582         shift_win_forward(wmeta);
583     }
584     else
585     {
586         shift_win_backward(wmeta);
587     }
588     return update_wins(wmeta, wmeta->_chain_start);
589 }
590
591
592
593 extern uint8_t draw_all_wins(struct WinMeta * wmeta)
594 {
595     /* Empty everything before filling it a-new. */
596     erase();
597     wnoutrefresh(wmeta->_screen);
598     werase(wmeta->padframe.curses_win);
599     if (wmeta->_chain_start)
600     {
601
602         /* Draw windows' contents first, then their borders. */
603         draw_wins(wmeta->_chain_start);
604         draw_wins_borderlines(wmeta->_chain_start, wmeta->active,
605                               wmeta->padframe.curses_win);
606         draw_wins_bordercorners(wmeta->_chain_start,wmeta->padframe.curses_win);
607
608         /* Draw virtual screen scroll hints. */
609         if (wmeta->pad_offset > 0)
610         {
611             if (draw_scroll_hint(&wmeta->padframe,
612                                  wmeta->pad_offset, wmeta->pad_offset + 1, '<'))
613             {
614                 return 1;
615             }
616         }
617         if (wmeta->pad_offset + wmeta->padframe.size.x
618             < getmaxx(wmeta->padframe.curses_win) - 1)
619         {
620             if (draw_scroll_hint(&wmeta->padframe,
621                                  wmeta->pad_offset + wmeta->padframe.size.x - 1,
622                                  getmaxx(wmeta->padframe.curses_win)
623                                  - (wmeta->pad_offset + wmeta->padframe.size.x),
624                                  '>'))
625             {
626                 return 1;
627             }
628         }
629
630         /* Write virtual screen segment to be shown on physical screen into */
631         /* ncurses screen buffer. */
632         pnoutrefresh(wmeta->padframe.curses_win, 0, wmeta->pad_offset, 0, 0,
633                      wmeta->padframe.size.y, wmeta->padframe.size.x-1);
634     }
635
636     /* Only at the end write accumulated changes to the physical screen. */
637     doupdate();
638     return 0;
639 }
640
641
642
643 extern uint8_t draw_scroll_hint(struct Frame * frame, uint16_t pos,
644                                 uint32_t dist, char dir)
645 {
646     /* Decide on alignment (vertical/horizontal?), thereby scroll hint text. */
647     char * more = "more";
648     char * unit_cols = "columns";
649     char * unit_rows = "lines";
650     uint16_t dsc_space = frame->size.x;
651     char * unit = unit_rows;
652     if ('<' == dir || '>' == dir)
653     {
654         dsc_space = frame->size.y;
655         unit = unit_cols;
656     }
657     char * scrolldsc = malloc((4 * sizeof(char)) + strlen(more) + strlen(unit)
658                               + 10);                /* 10 = uint32 max strlen */
659     if (NULL == scrolldsc)
660     {
661         return 1;
662     }
663     sprintf(scrolldsc, " %d %s %s ", dist, more, unit);
664
665     /* Decide on offset of the description text inside the scroll hint line. */
666     char offset = 1, q;
667     if (dsc_space > strlen(scrolldsc) + 1)
668     {
669         offset = (dsc_space - strlen(scrolldsc)) / 2;
670     }
671
672     /* Draw scroll hint line as dir symbols bracketing description text. */
673     chtype symbol;
674     for (q = 0; q < dsc_space; q++)
675     {
676         if (q >= offset && q < strlen(scrolldsc) + offset)
677         {
678             symbol = scrolldsc[q - offset] | A_REVERSE;
679         }
680         else
681         {
682             symbol = dir | A_REVERSE;
683         }
684         if ('<' == dir || '>' == dir)
685         {
686             mvwaddch(frame->curses_win, q, pos, symbol);
687         }
688         else
689         {
690             mvwaddch(frame->curses_win, pos, q, symbol);
691         }
692     }
693
694     free(scrolldsc);
695     return 0;
696 }