home · contact · privacy
Uise uint32_t to test for a value going beyond UINT16_MAX.
[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 struct Win init_win(struct WinMeta * wmeta, char * title,
392                            uint16_t height, uint16_t width,
393                            void * data, void * func)
394 {
395     struct Win w;
396     w._prev             = 0;
397     w._next             = 0;
398     w.frame.curses_win  = 0;
399     w._title            = title;
400     w.data              = data;
401     w._draw             = func;
402     if (width > 0)
403     {
404         w.frame.size.x = width;
405     }
406     else
407     {
408         w.frame.size.x = 1;
409     }
410     if (height > 0 && height <= wmeta->padframe.size.y - 1)
411     {
412         w.frame.size.y = height;
413     }
414     else
415     {
416         w.frame.size.y = wmeta->padframe.size.y - 1;
417     }
418     return w;
419 }
420
421
422
423 extern uint8_t append_win(struct WinMeta * wmeta, struct Win * w)
424 {
425     if (0 != wmeta->_chain_start)
426     {
427         w->_prev = wmeta->_chain_end;
428         wmeta->_chain_end->_next = w;
429     }
430     else
431     {
432         wmeta->active = w;
433         wmeta->_chain_start = w;
434     }
435     wmeta->_chain_end = w;
436     return update_wins(wmeta, w);
437 }
438
439
440
441 extern uint8_t suspend_win(struct WinMeta * wmeta, struct Win * w)
442 {
443     destroy_win(w);
444
445     if (wmeta->_chain_start != w)
446     {
447         w->_prev->_next = w->_next;
448     }
449     else
450     {
451         wmeta->_chain_start = w->_next;
452     }
453     char pad_refitted = 0;
454     if (wmeta->_chain_end != w)
455     {
456         w->_next->_prev = w->_prev;
457         if (wmeta->active == w)
458         {
459             wmeta->active = w->_next;
460         }
461         uint8_t test = update_wins(wmeta, w->_next);/* Positioning of         */
462         if (0 != test)                              /* successor windows may  */
463         {                                           /* be affected / need     */
464             return test;                            /* correction. Note that  */
465         }                                           /* update_wins() already  */
466         pad_refitted = 1;                           /* refits the pad, voiding*/
467     }                                               /* later need for that.   */
468     else
469     {
470         wmeta->_chain_end = w->_prev;
471         if (wmeta->active == w)
472         {
473             wmeta->active = w->_prev;
474         }
475     }
476
477     w->_prev = 0;
478     w->_next = 0;
479
480     if (0 == pad_refitted)
481     {
482         return refit_pad(wmeta);
483     }
484     return 0;
485 }
486
487
488
489 extern void reset_pad_offset(struct WinMeta * wmeta, uint16_t new_offset)
490 {
491     if (new_offset >= 0
492         && (new_offset < wmeta->pad_offset
493             || new_offset + wmeta->padframe.size.x
494                < getmaxx(wmeta->padframe.curses_win)))
495     {
496         wmeta->pad_offset = new_offset;
497     }
498 }
499
500
501
502 extern uint8_t resize_active_win(struct WinMeta * wmeta, struct yx_uint16 size)
503 {
504     if (0 != wmeta->active
505         && size.x > 0 && size.y > 0
506         && size.y < wmeta->padframe.size.y)
507     {
508         wmeta->active->frame.size = size;
509         return update_wins(wmeta, wmeta->active); /* Positioning of following */
510     }                                             /* windows may be affected. */
511     return 0;
512 }
513
514
515
516 extern void cycle_active_win(struct WinMeta * wmeta, char dir)
517 {
518     if (0 != wmeta->active)
519     {
520         if ('f' == dir)
521         {
522             if (wmeta->active->_next != 0)
523             {
524                 wmeta->active = wmeta->active->_next;
525             }
526             else
527             {
528                 wmeta->active = wmeta->_chain_start;
529             }
530         }
531         else
532         {
533             if (wmeta->active->_prev != 0)
534             {
535                 wmeta->active = wmeta->active->_prev;
536             }
537             else
538             {
539                 wmeta->active = wmeta->_chain_end;
540             }
541         }
542     }
543 }
544
545
546
547 extern uint8_t shift_active_win(struct WinMeta * wmeta, char dir)
548 {
549     if (   0 == wmeta->active                        /* No shifting with < 2  */
550         || wmeta->_chain_start == wmeta->_chain_end) /* windows visible.      */
551     {
552         return 0;
553     }
554     if ('f' == dir)
555     {
556         shift_win_forward(wmeta);
557     }
558     else
559     {
560         shift_win_backward(wmeta);
561     }
562     return update_wins(wmeta, wmeta->_chain_start);
563 }
564
565
566
567 extern uint8_t draw_all_wins(struct WinMeta * wmeta)
568 {
569     /* Empty everything before filling it a-new. */
570     erase();
571     wnoutrefresh(wmeta->_screen);
572     werase(wmeta->padframe.curses_win);
573     if (wmeta->_chain_start)
574     {
575
576         /* Draw windows' contents first, then their borders. */
577         draw_wins(wmeta->_chain_start);
578         draw_wins_borderlines(wmeta->_chain_start, wmeta->active,
579                               wmeta->padframe.curses_win);
580         draw_wins_bordercorners(wmeta->_chain_start,wmeta->padframe.curses_win);
581
582         /* Draw virtual screen scroll hints. */
583         if (wmeta->pad_offset > 0)
584         {
585             if (draw_scroll_hint(&wmeta->padframe,
586                                  wmeta->pad_offset, wmeta->pad_offset + 1, '<'))
587             {
588                 return 1;
589             }
590         }
591         if (wmeta->pad_offset + wmeta->padframe.size.x
592             < getmaxx(wmeta->padframe.curses_win) - 1)
593         {
594             if (draw_scroll_hint(&wmeta->padframe,
595                                  wmeta->pad_offset + wmeta->padframe.size.x - 1,
596                                  getmaxx(wmeta->padframe.curses_win)
597                                  - (wmeta->pad_offset + wmeta->padframe.size.x),
598                                  '>'))
599             {
600                 return 1;
601             }
602         }
603
604         /* Write virtual screen segment to be shown on physical screen into */
605         /* ncurses screen buffer. */
606         pnoutrefresh(wmeta->padframe.curses_win, 0, wmeta->pad_offset, 0, 0,
607                      wmeta->padframe.size.y, wmeta->padframe.size.x-1);
608     }
609
610     /* Only at the end write accumulated changes to the physical screen. */
611     doupdate();
612     return 0;
613 }
614
615
616
617 extern uint8_t draw_scroll_hint(struct Frame * frame, uint16_t pos,
618                                 uint32_t dist, char dir)
619 {
620     /* Decide on alignment (vertical/horizontal?), thereby scroll hint text. */
621     char * more = "more";
622     char * unit_cols = "columns";
623     char * unit_rows = "lines";
624     uint16_t dsc_space = frame->size.x;
625     char * unit = unit_rows;
626     if ('<' == dir || '>' == dir)
627     {
628         dsc_space = frame->size.y;
629         unit = unit_cols;
630     }
631     char * scrolldsc = malloc((4 * sizeof(char)) + strlen(more) + strlen(unit)
632                               + 10);                /* 10 = uint32 max strlen */
633     if (NULL == scrolldsc)
634     {
635         return 1;
636     }
637     sprintf(scrolldsc, " %d %s %s ", dist, more, unit);
638
639     /* Decide on offset of the description text inside the scroll hint line. */
640     char offset = 1, q;
641     if (dsc_space > strlen(scrolldsc) + 1)
642     {
643         offset = (dsc_space - strlen(scrolldsc)) / 2;
644     }
645
646     /* Draw scroll hint line as dir symbols bracketing description text. */
647     chtype symbol;
648     for (q = 0; q < dsc_space; q++)
649     {
650         if (q >= offset && q < strlen(scrolldsc) + offset)
651         {
652             symbol = scrolldsc[q - offset] | A_REVERSE;
653         }
654         else
655         {
656             symbol = dir | A_REVERSE;
657         }
658         if ('<' == dir || '>' == dir)
659         {
660             mvwaddch(frame->curses_win, q, pos, symbol);
661         }
662         else
663         {
664             mvwaddch(frame->curses_win, pos, q, symbol);
665         }
666     }
667
668     free(scrolldsc);
669     return 0;
670 }