home · contact · privacy
Made the hierarchy between terminal screen, virtual screen, "pad", its frame etc...
[plomrogue] / src / windows.h
1 /* windows.h
2  *
3  * A tiled window manager for the terminal.
4  *
5  * It provides a virtual screen that can be scrolled horizontally and may
6  * contain any number of windows that can be appeared, disappeared, resized and
7  * (somewhat) moved around. They have borders and a title bar and are positioned
8  * (in a bizarre fashion, see below) automatically.
9  *
10  * Windows can be almost any width (number has to fit into 16 bits); the virtual
11  * screen grows with them as needed -- but only horizontally. Their height is
12  * limited by the height of the terminal screen.
13  *
14  * Positioning of windows can only indirectly be influenced: by resizing them,
15  * and by shifting their relative position inside the (currently invisible)
16  * chain that the window manager treats their plurality as. The first window
17  * goes into the upper left corner of the virtual screen. Further visible
18  * windows are fitted left-aligned below their (chain-wise) closest predecessor
19  * that thrones over enough space to fit them in; failing that, they are placed
20  * to the right of the window with the rightmost border.
21  *
22  * TODO: Think up a more intuitive window positioning algorithm or at least make
23  * the chain that windows are positioned by visible.
24  *
25  * TODO: Ensure there are only windows as many / as big as fit into the maximum
26  * size of the virtual screen.
27  */
28
29 #ifndef WINDOWS_H
30 #define WINDOWS_H
31
32
33
34 #include <stdint.h>    /* for uint16_t, uint32_t */
35 #include <ncurses.h>   /* for the WINDOW typedef */
36 #include "yx_uint16.h" /* for yx_uint16 coordinates */
37
38
39 /* Individual windows consist of potential (real if window is visible) ncurses
40  * WINDOWs wrapped inside Frame structs (that keep a window's designated size
41  * even when it is invisible) wrapped inside metadata-rich Win structs. Win
42  * structs are chained into a linked list of all the windows visible on the
43  * virtual screen and also contain pointers to what content is to be drawn
44  * inside the window, and by use of what method.
45  */
46
47 struct Frame
48 {
49     WINDOW * curses_win;
50     struct yx_uint16 size;  /* the size curses_win fits into; for the virtual */
51 };                          /* screen padframe: the terminal screen size      */
52
53 struct Win
54 {
55     struct Win * prev;            /* prev=next=0 if Win is outside the chain */
56     struct Win * next;
57     struct yx_uint16 start;       /* upper left corner (of WINDOW or border?) */
58     struct Frame frame;
59     char * title;                 /* title to be shown on window border top */
60     void (* draw) (struct Win *); /* function to draw window content ("data") */
61     void * data;                  /* content to be drawn; draw() knows how */
62 };
63
64
65
66 /* The window manager's parent struct WinMeta provides the virtual screen and a
67  * representation of the terminal screen. It also anchors start and end of the
68  * windows chain.
69  */
70 struct WinMeta
71 {
72     WINDOW * screen;          /* terminal screen */
73     uint16_t pad_offset;      /* number of cells view is moved to the right */
74     struct Frame padframe;    /* virtual screen fitted into terminal screen */
75     struct Win * chain_start; /* first Win, whose .prev shall point to 0 */
76     struct Win * chain_end;   /* last Win, whose .next shall point to 0 */
77     struct Win * active;      /* window highlighted/selected for manipulation */
78 };
79
80
81
82 /* Create on the terminal "screen" an empty WinMeta. Note that emptiness is
83  * marked by WinMeta.chain_start=0. Other struct values are also initialized 0,
84  * except for the virtual screen (terminal screen height, width = 1) and its
85  * terminal-sized frame.
86  */
87 extern struct WinMeta init_win_meta(WINDOW * screen);
88
89
90
91 /* Create a window below inside "wmeta" titled "title" and appointing "func"()
92  * to interpret and draw the content stored at "data" if the window is visible.
93  *
94  * The start size for the Frame will be a width of 20 cells and a height one
95  * less than the height of the virtual screen (so as to fit the title bar on top
96  * of the window). Other values will be initialized to 0. The window will stay
97  * invisible until appended to the chain of visible windows via append_win().
98  *
99  * TODO: Why a default start width instead of passing a start width?
100  */
101 extern struct Win init_win(struct WinMeta * wmeta, char * title,
102                            void * data, void * func);
103
104
105
106 /* Append/suspend window "w" to/from chain of visible windows in "wmeta".
107  * Appended windows will become active. Suspended active windows will move the
108  * active window selection to their successor in the window chain or, failing
109  * that, their predecessor; if no window remains, none will be active.
110  */
111 extern void append_win(struct WinMeta * wmeta, struct Win * w);
112 extern void suspend_win(struct WinMeta * wmeta, struct Win * w);
113
114
115
116 /* Apply scrolling offset "new_offset" to virtual screen if it is sane, i.e.
117  * it's equal/greater zero and does not push the view (further) beyond the
118  * virtual screen's border. If the view is already beyond the virtual screen's
119  * border due to it having shrunk after suspension of windows, only allow view
120  * movement leftwards.
121  */
122 extern void reset_pad_offset(struct WinMeta * wmeta, uint16_t new_offset);
123
124
125
126 /* Apply new size "size" to the active window, but only if it provides for at
127  * least one cell width/height and is in height at least one cell smaller than
128  * the screen's vertical height (to provide space for the title bar).
129  */
130 extern void resize_active_win(struct WinMeta * wmeta, struct yx_uint16 size);
131
132
133
134 /* Cycle active window selection forwards (set dir="n") or backwards. Wrap
135  * around in the windows chain if start / end of it is met.
136  */
137 extern void cycle_active_win(struct WinMeta * wmeta, char dir);
138
139
140
141 /* Move active window forwards (set dir="f") or backwards (set dir="b"). Wrap
142  * around in the window chain if start / end of it is met.
143  */
144 extern void shift_active_win(struct WinMeta * wmeta, char dir);
145
146
147
148 /* Draw virtual screen including all windows. Also add scroll hints (a line
149  * stating that there is more to see on scrolling further into a certain
150  * direction) for where the edges of the terminal screen hit non-edges of and
151  * inside the virtual screen. Then update the terminal screen.
152  */
153 extern void draw_all_wins(struct WinMeta * wmeta);
154
155
156
157 /* Draw scroll hint (a line stating that there is more to see on scrolling
158  * further into a certain direction) into "frame" at position "pos" (describing
159  * a column or a row dependent on "dir" being *either* "<"/">" *or* something
160  * else). It will consist of a line of "dir" symbols bracketing a descriptive
161  * text stating the number of rows/columns further available beyond the hint.
162  */
163 extern void draw_scroll_hint(struct Frame * frame, uint16_t pos, uint32_t dist,
164                              char dir);
165
166
167
168 #endif