home · contact · privacy
Fixed bug that led to endless loop in nearest_enemy_dir().
[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 carry
6  * any number of windows to be appeared, disappeared, resized and moved around.
7  * They have borders and a title bar and are positioned automatically.
8  *
9  * Windows can be any width between 1 and 2^16 cells. The virtual screen grows
10  * with them as needed -- but only horizontally and only up to 2^16 cells. Their
11  * height is limited by the height of the terminal screen (maximum 2^16 cells).
12  *
13  * Windows' positioning can be influenced only indirectly: by resizing them, and
14  * by shifting their relative position inside the (currently invisible) chain
15  * that the window manager treats their plurality as. The first window goes into
16  * the top left corner of the virtual screen. Further windows are fitted as
17  * left-aligned as possible below their (chain-wise) closest predecessor that
18  * thrones over enough space to contain them and that is open to the right. If
19  * that fails, they're fitted up-right to the window with the rightmost border.
20  *
21  * TODO: Think up a more intuitive window positioning algorithm.
22  */
23
24 #ifndef WINDOWS_H
25 #define WINDOWS_H
26
27 #include <stdint.h>    /* for uint8_t, uint16_t, uint32_t */
28 #include <ncurses.h>   /* for the WINDOW and chtype typedefs */
29 #include "yx_uint16.h" /* for yx_uint16 struct */
30
31
32
33 /* "Win" structs describe windows as frames located inside the virtual screen
34  * pad through which "winmaps" are visible, 2-dimensional maps of ncurses
35  * chtypes. If a winmap is bigger than its frame, scrolling hints will appear at
36  * the proper edges. Win structs are chained into a linked list of all the
37  * windows visible on the virtual screen and also contain pointers to what
38  * content is to be drawn inside the window, and by use of what method.
39  */
40 struct Win
41 {
42     struct Win * prev;  /* chain pointers; if 0, they mark the start or end  */
43     struct Win * next;  /* of the chain; if both are 0, Win is outside chain */
44     struct yx_uint16 framesize;   /* window frame size to see winmap through */
45     struct yx_uint16 start;       /* upper left corner of window in pad */
46     struct yx_uint16 center;      /* winmap cell to center frame on if smaller*/
47     char * title;                 /* title to be used in window title bar */
48     void (* draw) (struct Win *); /* function that draws/updates the  winmap */
49     chtype * winmap;              /* sequence of cells, sorted into lines ... */
50     struct yx_uint16 winmapsize;  /* ... with these geometry infos  */
51 };
52
53 /* The window manager's parent struct WinMeta contains the virtual screen,
54  * relates it to the terminal screen and anchors the chain of visible windows.
55  */
56 struct WinMeta
57 {
58     WINDOW * screen;          /* ncurses' pointer to the terminal screen */
59     WINDOW * pad;             /* ncurses pad of virtual screen */
60     uint16_t pad_offset;      /* number of cells view is moved to the right */
61     struct yx_uint16 padsize; /* virtual screen size */
62     struct Win * chain_start; /* first Win in chain; its _prev == 0 */
63     struct Win * chain_end;   /* last Win in chain; its _next == 0 */
64     struct Win * active;      /* Win highlighted/selected for manipulation */
65 };
66
67
68
69 /* Initialize empty "wmeta" on terminal screen. All struct members initialize to
70  * 0, except for .screen, the newly created virtual screen .pad and its .padsize
71  * (height: that of the terminal screen; width: 1 cell).
72  */
73 extern void init_win_meta();
74
75 /* Initialize a Win child "wp" of "wmeta" to "title", "height" and "width" and
76  * appoint "func"() as its .draw. Initialize other members to 0.
77  *
78  * Pass 0 for "width" to make the window as wide as the terminal screen. Pass 0
79  * for "height" for the maximum allowed height: one cell smaller than that of
80  * the terminal screen. Pass negative values for either of them to make the
81  * window width/height so many cells smaller than what 0 would set. The maximum
82  * allowed height is also applied for positive height values that exceed it or
83  * negative values that would reduce the window height to less than 1 cell.
84  */
85 extern void init_win(struct Win ** wp, char * title, int16_t height,
86                      int16_t width, void * func);
87
88 /* Free memory initialized Win/WinMeta structs; endwin() for the latter, too. */
89 extern void free_winmeta_and_endwin();
90 extern void free_win(struct Win * win);
91
92 /* Append/suspend window "w" to/from chain of visible windows below "wmeta".
93  * Appended windows will become active. Suspended active windows will move the
94  * active window selection to their successor in the window chain or, failing
95  * that, their predecessor, or, failing that, to 0 (no window active).
96  */
97 extern void append_win(struct Win * w);
98 extern void suspend_win(struct Win * w);
99
100 /* Apply scrolling offset "new_offset" to virtual screen if it is equal/greater
101  * 0 and does not push the view (further) beyond the virtual screen's border. If
102  * the view is already beyond the virtual screen's border due to it having
103  * shrunk after suspension of windows, only allow screen scrolling leftwards.
104  */
105 extern void reset_pad_offset(uint16_t new_offset);
106
107 /* Apply "size" to the active window if it provides a minimum size of 1x1 cells
108  * and is in height at least one cell smaller than the screen's vertical height
109  * (to provide space for the title bar). Does nothing if no window is active.
110  */
111 extern void resize_active_win(struct yx_uint16 size);
112
113 /* Cycle active window selection forwards ("dir" == "f") or backwards (any
114  * other "dir"). Wrap around in the windows chain if start / end of it is met.
115  * Does nothing if no window is active.
116  */
117 extern void cycle_active_win(char dir);
118
119 /* Move active window forwards ("dir" == "f") or backwards (any other "dir") in
120  * the window chain. Wrap around in the window chain if start / end of it is
121  * met. Does nothing if no window is active.
122  */
123 extern void shift_active_win(char dir);
124
125 /* Draw virtual screen and its windows. Add scroll hints where edges of terminal
126  * screen hit non-edges inside the virtual screen. Then update terminal screen.
127  */
128 extern void draw_all_wins();
129
130
131
132 #endif