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