home · contact · privacy
Applied new style and documentation rules to the "windows" library, also performing...
[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;        /* designated size of curses_win */
51 };
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 pad;         /* virtual 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 whose virtual screen at the
83  * beginning is sized just like the terminal screen. Note that emptiness is
84  * marked by WinMeta.chain_start=0. Other struct values are also initialized 0.
85  *
86  * TODO: Why is an *empty* virtual screen's size that of the terminal screen?
87  */
88 extern struct WinMeta init_win_meta(WINDOW * screen);
89
90
91
92 /* Create a window below inside "wmeta" titled "title" and appointing "func"()
93  * to interpret and draw the content stored at "data" if the window is visible.
94  *
95  * The start size for the Frame will be a width of 20 cells and a height one
96  * less than the height of the virtual screen (so as to fit the title bar on top
97  * of the window). Other values will be initialized to 0. The window will stay
98  * invisible until appended to the chain of visible windows via append_win().
99  *
100  * TODO: Why a default start width instead of passing a start width?
101  */
102 extern struct Win init_win(struct WinMeta * wmeta, char * title,
103                            void * data, void * func);
104
105
106
107 /* Append/suspend window "w" to/from chain of visible windows in "wmeta".
108  * Appended windows will become active. Suspended active windows will move the
109  * active window selection to their successor in the window chain or, failing
110  * that, their predecessor; if no window remains, none will be active.
111  */
112 extern void append_win(struct WinMeta * wmeta, struct Win * w);
113 extern void suspend_win(struct WinMeta * wmeta, struct Win * w);
114
115
116
117 /* Apply scrolling offset "new_offset" to virtual screen if it is sane, i.e.
118  * it's equal/greater zero and does not push the view (further) beyond the
119  * virtual screen's border. If the view is already beyond the virtual screen's
120  * border due to it having shrunk after suspension of windows, only allow view
121  * movement leftwards.
122  */
123 extern void reset_pad_offset(struct WinMeta * wmeta, uint16_t new_offset);
124
125
126
127 /* Apply new size "size" to the active window, but only if it provides for at
128  * least one cell width/height and is in height at least one cell smaller than
129  * the screen's vertical height (to provide space for the title bar).
130  */
131 extern void resize_active_win(struct WinMeta * wmeta, struct yx_uint16 size);
132
133
134
135 /* Cycle active window selection forwards (set dir="n") or backwards. Wrap
136  * around in the windows chain if start / end of it is met.
137  */
138 extern void cycle_active_win(struct WinMeta * wmeta, char dir);
139
140
141
142 /* Move active window forwards (set dir="f") or backwards (set dir="b"). Wrap
143  * around in the window chain if start / end of it is met.
144  */
145 extern void shift_active_win(struct WinMeta * wmeta, char dir);
146
147
148
149 /* Draw virtual screen including all windows. Also add scroll hints (a line
150  * stating that there is more to see on scrolling further into a certain
151  * direction) for where the edges of the terminal screen hit non-edges of and
152  * inside the virtual screen. Then update the terminal screen.
153  */
154 extern void draw_all_wins(struct WinMeta * wmeta);
155
156
157
158 /* Draw scroll hint (a line stating that there is more to see on scrolling
159  * further into a certain direction) into "frame" at position "pos" (describing
160  * a column or a row dependent on "dir" being *either* "<"/">" *or* something
161  * else). It will consist of a line of "dir" symbols bracketing a descriptive
162  * text stating the number of rows/columns further available beyond the hint.
163  */
164 extern void draw_scroll_hint(struct Frame * frame, uint16_t pos, uint32_t dist,
165                              char dir);
166
167
168
169 #endif