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