/src/neomutt/gui/rootwin.c
Line | Count | Source (jump to first uncovered line) |
1 | | /** |
2 | | * @file |
3 | | * Root Window |
4 | | * |
5 | | * @authors |
6 | | * Copyright (C) 2021-2023 Richard Russon <rich@flatcap.org> |
7 | | * |
8 | | * @copyright |
9 | | * This program is free software: you can redistribute it and/or modify it under |
10 | | * the terms of the GNU General Public License as published by the Free Software |
11 | | * Foundation, either version 2 of the License, or (at your option) any later |
12 | | * version. |
13 | | * |
14 | | * This program is distributed in the hope that it will be useful, but WITHOUT |
15 | | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
16 | | * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more |
17 | | * details. |
18 | | * |
19 | | * You should have received a copy of the GNU General Public License along with |
20 | | * this program. If not, see <http://www.gnu.org/licenses/>. |
21 | | */ |
22 | | |
23 | | /** |
24 | | * @page gui_rootwin Root Window |
25 | | * |
26 | | * NeoMutt is built from a set of nested windows. Each window defines a region |
27 | | * of the screen which is responsible for a single concept. This could be a high-level |
28 | | * component like the @ref compose_dlg_compose, or a single element like the @ref index_ibar. |
29 | | * |
30 | | * The **Root Window** is (grand-)parent of all those windows. |
31 | | * |
32 | | * The Root Window is container window and not visible. |
33 | | * |
34 | | * ### Definitions |
35 | | * |
36 | | * Every window in the hierarchy is struct MuttWindow, however in these docs |
37 | | * they're often given different descriptions. |
38 | | * |
39 | | * - **Window**: |
40 | | * A region of the screen. A Window can be: fixed size; set to maximise |
41 | | * (as limited by its parent); set to minimise (around its children). |
42 | | * Everything below is also a Window. |
43 | | * |
44 | | * - **Dialog**: |
45 | | * A set of nested Windows that form an interactive component. This is the |
46 | | * main way that users interact with NeoMutt. e.g. @ref index_dlg_index, |
47 | | * @ref compose_dlg_compose. |
48 | | * |
49 | | * - **Panel** |
50 | | * A small sub-division of a Dialog. The Panels are sets of Windows that can |
51 | | * be reused in other Dialogs. |
52 | | * |
53 | | * - **Container**: |
54 | | * An invisible non-interactive Window used for shaping, aligning or limiting |
55 | | * the size of its children. |
56 | | * |
57 | | * - **Bar**: |
58 | | * A one-line high Window used for displaying help or status info, e.g. |
59 | | * @ref helpbar_helpbar, @ref index_ibar. |
60 | | * |
61 | | * ## Windows |
62 | | * |
63 | | * | Name | Type | Constructor | |
64 | | * | :---------- | :------- | :------------ | |
65 | | * | Root Window | #WT_ROOT | rootwin_new() | |
66 | | * |
67 | | * **Parent** |
68 | | * - None |
69 | | * |
70 | | * **Children** |
71 | | * - @ref helpbar_helpbar |
72 | | * - @ref gui_dialog |
73 | | * - @ref gui_msgwin |
74 | | * |
75 | | * ## Data |
76 | | * |
77 | | * The Root Window has no data. |
78 | | * |
79 | | * ## Events |
80 | | * |
81 | | * Once constructed, it is controlled by the following events: |
82 | | * |
83 | | * | Event Type | Handler | |
84 | | * | :---------- | :-------------------------------------------------- | |
85 | | * | #NT_CONFIG | rootwin_config_observer() | |
86 | | * | #NT_WINDOW | rootwin_window_observer() | |
87 | | * | SIGWINCH | rootwin_set_size() (called by mutt_resize_screen()) | |
88 | | * |
89 | | * The Root Window does not implement MuttWindow::recalc() or MuttWindow::repaint(). |
90 | | */ |
91 | | |
92 | | #include "config.h" |
93 | | #include <stdbool.h> |
94 | | #include <string.h> |
95 | | #include "mutt/lib.h" |
96 | | #include "config/lib.h" |
97 | | #include "core/lib.h" |
98 | | #include "helpbar/lib.h" |
99 | | #include "dialog.h" |
100 | | #include "msgcont.h" |
101 | | #include "msgwin.h" |
102 | | #include "mutt_window.h" |
103 | | |
104 | | void mutt_resize_screen(void); |
105 | | |
106 | | struct MuttWindow *RootWindow = NULL; ///< Parent of all Windows |
107 | | |
108 | | /** |
109 | | * rootwin_config_observer - Notification that a Config Variable has changed - Implements ::observer_t - @ingroup observer_api |
110 | | * |
111 | | * The Root Window is affected by changes to `$status_on_top`. |
112 | | */ |
113 | | static int rootwin_config_observer(struct NotifyCallback *nc) |
114 | 0 | { |
115 | 0 | if (nc->event_type != NT_CONFIG) |
116 | 0 | return 0; |
117 | 0 | if (!nc->global_data || !nc->event_data) |
118 | 0 | return -1; |
119 | | |
120 | 0 | struct EventConfig *ev_c = nc->event_data; |
121 | 0 | struct MuttWindow *win_root = nc->global_data; |
122 | |
|
123 | 0 | if (!mutt_str_equal(ev_c->name, "status_on_top")) |
124 | 0 | return 0; |
125 | | |
126 | 0 | struct MuttWindow *first = TAILQ_FIRST(&win_root->children); |
127 | 0 | if (!first) |
128 | 0 | return 0; |
129 | | |
130 | 0 | const bool c_status_on_top = cs_subset_bool(NeoMutt->sub, "status_on_top"); |
131 | 0 | if ((c_status_on_top && (first->type == WT_HELP_BAR)) || |
132 | 0 | (!c_status_on_top && (first->type != WT_HELP_BAR))) |
133 | 0 | { |
134 | | // Swap the HelpBar and the AllDialogsWindow |
135 | 0 | struct MuttWindow *next = TAILQ_NEXT(first, entries); |
136 | 0 | if (!next) |
137 | 0 | return 0; |
138 | 0 | TAILQ_REMOVE(&win_root->children, next, entries); |
139 | 0 | TAILQ_INSERT_HEAD(&win_root->children, next, entries); |
140 | |
|
141 | 0 | mutt_window_reflow(win_root); |
142 | 0 | mutt_debug(LL_DEBUG5, "config done, request WA_REFLOW\n"); |
143 | 0 | } |
144 | | |
145 | 0 | return 0; |
146 | 0 | } |
147 | | |
148 | | /** |
149 | | * rootwin_resize_observer - Notification that the terminal has been resized - Implements ::observer_t - @ingroup observer_api |
150 | | * |
151 | | * This function is triggered by SIGWINCH. |
152 | | */ |
153 | | static int rootwin_resize_observer(struct NotifyCallback *nc) |
154 | 0 | { |
155 | 0 | if (nc->event_type != NT_RESIZE) |
156 | 0 | return 0; |
157 | 0 | if (!nc->global_data) |
158 | 0 | return -1; |
159 | | |
160 | 0 | window_invalidate_all(); |
161 | 0 | mutt_resize_screen(); |
162 | |
|
163 | 0 | mutt_debug(LL_DEBUG5, "window resize done\n"); |
164 | 0 | return 0; |
165 | 0 | } |
166 | | |
167 | | /** |
168 | | * rootwin_window_observer - Notification that a Window has changed - Implements ::observer_t - @ingroup observer_api |
169 | | * |
170 | | * This function is triggered by changes to the windows. |
171 | | * |
172 | | * - Delete (this window): clean up the resources held by the Root Window |
173 | | */ |
174 | | static int rootwin_window_observer(struct NotifyCallback *nc) |
175 | 0 | { |
176 | 0 | if (nc->event_type != NT_WINDOW) |
177 | 0 | return 0; |
178 | 0 | if (!nc->global_data || !nc->event_data) |
179 | 0 | return -1; |
180 | 0 | if (nc->event_subtype != NT_WINDOW_DELETE) |
181 | 0 | return 0; |
182 | | |
183 | 0 | struct MuttWindow *win_root = nc->global_data; |
184 | 0 | struct EventWindow *ev_w = nc->event_data; |
185 | 0 | if (ev_w->win != win_root) |
186 | 0 | return 0; |
187 | | |
188 | 0 | notify_observer_remove(win_root->notify, rootwin_window_observer, win_root); |
189 | 0 | if (NeoMutt) |
190 | 0 | { |
191 | 0 | notify_observer_remove(NeoMutt->sub->notify, rootwin_config_observer, win_root); |
192 | 0 | notify_observer_remove(NeoMutt->notify_resize, rootwin_resize_observer, win_root); |
193 | 0 | } |
194 | |
|
195 | 0 | mutt_debug(LL_DEBUG5, "window delete done\n"); |
196 | 0 | return 0; |
197 | 0 | } |
198 | | |
199 | | /** |
200 | | * rootwin_cleanup - Free all the default Windows |
201 | | */ |
202 | | void rootwin_cleanup(void) |
203 | 0 | { |
204 | 0 | AllDialogsWindow = NULL; |
205 | 0 | MessageContainer = NULL; |
206 | 0 | mutt_window_free(&RootWindow); |
207 | 0 | } |
208 | | |
209 | | /** |
210 | | * rootwin_new - Create the default Windows |
211 | | * |
212 | | * Create the Help, Index, Status, Message and Sidebar Windows. |
213 | | */ |
214 | | void rootwin_new(void) |
215 | 0 | { |
216 | 0 | struct MuttWindow *win_root = mutt_window_new(WT_ROOT, MUTT_WIN_ORIENT_VERTICAL, |
217 | 0 | MUTT_WIN_SIZE_FIXED, 0, 0); |
218 | 0 | notify_set_parent(win_root->notify, NeoMutt->notify); |
219 | 0 | RootWindow = win_root; |
220 | |
|
221 | 0 | struct MuttWindow *win_helpbar = helpbar_new(); |
222 | 0 | struct MuttWindow *win_alldlgs = alldialogs_new(); |
223 | |
|
224 | 0 | const bool c_status_on_top = cs_subset_bool(NeoMutt->sub, "status_on_top"); |
225 | 0 | if (c_status_on_top) |
226 | 0 | { |
227 | 0 | mutt_window_add_child(win_root, win_alldlgs); |
228 | 0 | mutt_window_add_child(win_root, win_helpbar); |
229 | 0 | } |
230 | 0 | else |
231 | 0 | { |
232 | 0 | mutt_window_add_child(win_root, win_helpbar); |
233 | 0 | mutt_window_add_child(win_root, win_alldlgs); |
234 | 0 | } |
235 | |
|
236 | 0 | struct MuttWindow *win_cont = msgcont_new(); |
237 | 0 | struct MuttWindow *win_msg = msgwin_new(false); |
238 | 0 | mutt_window_add_child(win_cont, win_msg); |
239 | 0 | mutt_window_add_child(win_root, win_cont); |
240 | |
|
241 | 0 | notify_observer_add(NeoMutt->sub->notify, NT_CONFIG, rootwin_config_observer, win_root); |
242 | 0 | notify_observer_add(NeoMutt->notify_resize, NT_RESIZE, rootwin_resize_observer, win_root); |
243 | 0 | notify_observer_add(win_root->notify, NT_WINDOW, rootwin_window_observer, win_root); |
244 | 0 | } |
245 | | |
246 | | /** |
247 | | * rootwin_set_size - Set the dimensions of the Root Window |
248 | | * @param rows Number of rows on the screen |
249 | | * @param cols Number of columns on the screen |
250 | | * |
251 | | * This function is called after NeoMutt receives a SIGWINCH signal. |
252 | | */ |
253 | | void rootwin_set_size(int cols, int rows) |
254 | 0 | { |
255 | 0 | if (!RootWindow) |
256 | 0 | return; |
257 | | |
258 | 0 | bool changed = false; |
259 | |
|
260 | 0 | if (RootWindow->state.rows != rows) |
261 | 0 | { |
262 | 0 | RootWindow->state.rows = rows; |
263 | 0 | changed = true; |
264 | 0 | } |
265 | |
|
266 | 0 | if (RootWindow->state.cols != cols) |
267 | 0 | { |
268 | 0 | RootWindow->state.cols = cols; |
269 | 0 | changed = true; |
270 | 0 | } |
271 | |
|
272 | 0 | if (changed) |
273 | 0 | { |
274 | 0 | mutt_window_reflow(RootWindow); |
275 | 0 | } |
276 | 0 | } |