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