GDB (xrefs)
Loading...
Searching...
No Matches
tui-layout.h
Go to the documentation of this file.
1/* TUI layout window management.
2
3 Copyright (C) 1998-2023 Free Software Foundation, Inc.
4
5 Contributed by Hewlett-Packard Company.
6
7 This file is part of GDB.
8
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 3 of the License, or
12 (at your option) any later version.
13
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
18
19 You should have received a copy of the GNU General Public License
20 along with this program. If not, see <http://www.gnu.org/licenses/>. */
21
22#ifndef TUI_TUI_LAYOUT_H
23#define TUI_TUI_LAYOUT_H
24
25#include "ui-file.h"
26
27#include "tui/tui.h"
28#include "tui/tui-data.h"
29#include "gdbsupport/iterator-range.h"
30
31#include <unordered_map>
32
33/* Values that can be returned when handling a request to adjust a
34 window's size. */
36{
37 /* Requested window was not found here. */
39 /* Window was found but not handled. */
41 /* Window was found and handled. */
43};
44
45/* The basic object in a TUI layout. This represents a single piece
46 of screen real estate. Subclasses determine the exact
47 behavior. */
49{
50public:
51
53
54 virtual ~tui_layout_base () = default;
55
56 /* Clone this object. Ordinarily a layout is cloned before it is
57 used, so that any necessary modifications do not affect the
58 "skeleton" layout. */
59 virtual std::unique_ptr<tui_layout_base> clone () const = 0;
60
61 /* Change the size and location of this layout. When
62 PRESERVE_CMD_WIN_SIZE_P is true the current size of the TUI_CMD_WIN
63 is preserved, otherwise, the TUI_CMD_WIN will resize just like any
64 other window. */
65 virtual void apply (int x, int y, int width, int height,
66 bool preserve_cmd_win_size_p) = 0;
67
68 /* Return the minimum and maximum height or width of this layout.
69 HEIGHT is true to fetch height, false to fetch width. */
70 virtual void get_sizes (bool height, int *min_value, int *max_value) = 0;
71
72 /* True if the topmost (for vertical layouts), or the leftmost (for
73 horizontal layouts) item in this layout is boxed. */
74 virtual bool first_edge_has_border_p () const = 0;
75
76 /* True if the bottommost (for vertical layouts), or the rightmost (for
77 horizontal layouts) item in this layout is boxed. */
78 virtual bool last_edge_has_border_p () const = 0;
79
80 /* Return the name of this layout's window, or nullptr if this
81 layout does not represent a single window. */
82 virtual const char *get_name () const
83 {
84 return nullptr;
85 }
86
87 /* Set the height of the window named NAME to NEW_HEIGHT, updating
88 the sizes of the other windows around it. */
89 virtual tui_adjust_result set_height (const char *name, int new_height) = 0;
90
91 /* Set the width of the window named NAME to NEW_WIDTH, updating
92 the sizes of the other windows around it. */
93 virtual tui_adjust_result set_width (const char *name, int new_width) = 0;
94
95 /* Remove some windows from the layout, leaving the command window
96 and the window being passed in here. */
97 virtual void remove_windows (const char *name) = 0;
98
99 /* Replace the window named NAME in the layout with the window named
100 NEW_WINDOW. */
101 virtual void replace_window (const char *name, const char *new_window) = 0;
102
103 /* Append the specification to this window to OUTPUT. DEPTH is the
104 depth of this layout in the hierarchy (zero-based). */
105 virtual void specification (ui_file *output, int depth) = 0;
106
107 /* Return a FINGERPRINT string containing an abstract representation of
108 the location of the cmd window in this layout.
109
110 When called on a complete, top-level layout, the fingerprint will be a
111 non-empty string made of 'V' and 'H' characters, followed by a single
112 'C' character. Each 'V' and 'H' represents a vertical or horizontal
113 layout that must be passed through in order to find the cmd
114 window. A vertical or horizontal layout of just one window does not add
115 a 'V' or 'H' character.
116
117 Of course, layouts are built recursively, so, when called on a partial
118 layout, if this object represents a single window, then either the
119 empty string is returned (for non-cmd windows), or a string
120 containing a single 'C' is returned.
121
122 For object representing layouts, if the layout contains the cmd
123 window then we will get back a valid fingerprint string (may contain 'V'
124 and 'H', ends with 'C'), or, if this layout doesn't contain the cmd
125 window, an empty string is returned. */
126 virtual std::string layout_fingerprint () const = 0;
127
128 /* Add all windows to the WINDOWS vector. */
129 virtual void get_windows (std::vector<tui_win_info *> *windows) = 0;
130
131 /* The most recent space allocation. */
132 int x = 0;
133 int y = 0;
134 int width = 0;
135 int height = 0;
136
137protected:
138
139 tui_layout_base () = default;
140};
141
142/* A TUI layout object that displays a single window. The window is
143 given by name. */
145{
146public:
147
148 explicit tui_layout_window (const char *name)
149 : m_contents (name)
150 {
151 }
152
154
155 std::unique_ptr<tui_layout_base> clone () const override;
156
157 void apply (int x, int y, int width, int height,
158 bool preserve_cmd_win_size_p) override;
159
160 const char *get_name () const override
161 {
162 return m_contents.c_str ();
163 }
164
165 tui_adjust_result set_height (const char *name, int new_height) override
166 {
167 return m_contents == name ? FOUND : NOT_FOUND;
168 }
169
170 tui_adjust_result set_width (const char *name, int new_width) override
171 {
172 return m_contents == name ? FOUND : NOT_FOUND;
173 }
174
175 bool first_edge_has_border_p () const override;
176
177 bool last_edge_has_border_p () const override;
178
179 void remove_windows (const char *name) override
180 {
181 }
182
183 void replace_window (const char *name, const char *new_window) override;
184
185 void specification (ui_file *output, int depth) override;
186
187 std::string layout_fingerprint () const override;
188
189 /* See tui_layout_base::get_windows. */
190 void get_windows (std::vector<tui_win_info *> *windows) override
191 {
192 windows->push_back (m_window);
193 }
194
195protected:
196
197 void get_sizes (bool height, int *min_value, int *max_value) override;
198
199private:
200
201 /* Type of content to display. */
202 std::string m_contents;
203
204 /* When a layout is applied, this is updated to point to the window
205 object. */
207};
208
209/* A TUI layout that holds other layouts. */
211{
212public:
213
214 /* Create a new layout. If VERTICAL is true, then windows in this
215 layout will be arranged vertically. */
216 explicit tui_layout_split (bool vertical = true)
217 : m_vertical (vertical)
218 {
219 }
220
222
223 /* Add a new split layout to this layout. WEIGHT is the desired
224 size, which is relative to the other weights given in this
225 layout. */
226 void add_split (std::unique_ptr<tui_layout_split> &&layout, int weight);
227
228 /* Add a new window to this layout. NAME is the name of the window
229 to add. WEIGHT is the desired size, which is relative to the
230 other weights given in this layout. */
231 void add_window (const char *name, int weight);
232
233 std::unique_ptr<tui_layout_base> clone () const override;
234
235 void apply (int x, int y, int width, int height,
236 bool preserve_cmd_win_size_p) override;
237
238 tui_adjust_result set_height (const char *name, int new_height) override
239 {
240 /* Pass false as the final argument to indicate change of height. */
241 return set_size (name, new_height, false);
242 }
243
244 tui_adjust_result set_width (const char *name, int new_width) override
245 {
246 /* Pass true as the final argument to indicate change of width. */
247 return set_size (name, new_width, true);
248 }
249
250 bool first_edge_has_border_p () const override;
251
252 bool last_edge_has_border_p () const override;
253
254 void remove_windows (const char *name) override;
255
256 void replace_window (const char *name, const char *new_window) override;
257
258 void specification (ui_file *output, int depth) override;
259
260 std::string layout_fingerprint () const override;
261
262 /* See tui_layout_base::get_windows. */
263 void get_windows (std::vector<tui_win_info *> *windows) override
264 {
265 for (auto &item : m_splits)
266 item.layout->get_windows (windows);
267 }
268
269protected:
270
271 void get_sizes (bool height, int *min_value, int *max_value) override;
272
273private:
274
275 /* Used to implement set_height and set_width member functions. When
276 SET_WIDTH_P is true, set the width, otherwise, set the height of the
277 window named NAME to NEW_SIZE, updating the sizes of the other windows
278 around it as needed. The result indicates if the window NAME was
279 found and had its size adjusted, was found but was not adjusted, or
280 was not found at all. */
281 tui_adjust_result set_size (const char *name, int new_size,
282 bool set_width_p);
283
284 /* Set the weights from the current heights (when m_vertical is true) or
285 widths (when m_vertical is false). */
287
288 /* Structure used when resizing, or applying a layout. An instance of
289 this structure is created for each sub-layout. */
291 {
292 /* The calculated size for this sub-layout. */
293 int size;
294
295 /* The minimum and maximum sizes for this sub-layout, obtained by
296 calling the get_sizes member function. */
299
300 /* True if this window will share a box border with the previous
301 window in the list. */
303 };
304
305 /* Used for debug, prints the contents of INFO using tui_debug_printf.
306 Only call this when the global debug_tui is true. */
307 static void tui_debug_print_size_info (const std::vector<size_info> &info);
308
309 /* Used for debug, returns a string describing the current weight of each
310 sub-layout. */
311 std::string tui_debug_weights_to_string () const;
312
313 struct split
314 {
315 /* The requested weight. */
317 /* The layout. */
318 std::unique_ptr<tui_layout_base> layout;
319 };
320
321 /* The splits. */
322 std::vector<split> m_splits;
323
324 /* True if the windows in this split are arranged vertically. */
326};
327
328/* Add the specified window to the layout in a logical way. This
329 means setting up the most logical layout given the window to be
330 added. Only the source or disassembly window can be added this
331 way. */
332extern void tui_add_win_to_layout (enum tui_win_type);
333
334/* Set the initial layout. */
335extern void tui_set_initial_layout ();
336
337/* Switch to the next layout. */
338extern void tui_next_layout ();
339
340/* Show the register window. Like "layout regs". */
341extern void tui_regs_layout ();
342
343/* Remove some windows from the layout, leaving only the focused
344 window and the command window; if no window has the focus, then
345 some other window is chosen to remain. */
346extern void tui_remove_some_windows ();
347
348/* Apply the current layout. When PRESERVE_CMD_WIN_SIZE_P is true the
349 current size of the TUI_CMD_WIN is preserved, otherwise, the TUI_CMD_WIN
350 will resize just like any other window. */
351extern void tui_apply_current_layout (bool);
352
353/* Adjust the window height of WIN to NEW_HEIGHT. */
354extern void tui_adjust_window_height (struct tui_win_info *win,
355 int new_height);
356
357/* Adjust the window width of WIN to NEW_WIDTH. */
358extern void tui_adjust_window_width (struct tui_win_info *win,
359 int new_width);
360
361/* The type of a function that is used to create a TUI window. */
362
363typedef std::function<tui_win_info * (const char *name)> window_factory;
364
365/* The type for a data structure that maps a window name to that window's
366 factory function. */
367typedef std::unordered_map<std::string, window_factory> window_types_map;
368
369/* Register a new TUI window type. NAME is the name of the window
370 type. FACTORY is a function that can be called to instantiate the
371 window. */
372
373extern void tui_register_window (const char *name, window_factory &&factory);
374
375/* An iterator class that exposes just the window names from the
376 known_window_types map in tui-layout.c. This is just a wrapper around
377 an iterator of the underlying known_window_types map, but this just
378 exposes the window names. */
379
381{
382 using known_window_types_iterator = window_types_map::iterator;
383
385 : m_iter (std::move (iter))
386 { /* Nothing. */ }
387
389 {
390 ++m_iter;
391 return *this;
392 }
393
394 const std::string &operator* () const
395 { return (*m_iter).first; }
396
398 { return m_iter != other.m_iter; }
399
400private:
401
402 /* The underlying iterator. */
404};
405
406/* A range adapter that makes it possible to iterate over the names of all
407 known tui windows. */
408
410 = iterator_range<known_window_names_iterator>;
411
412/* Return a range that can be used to walk over the name of all known tui
413 windows in a range-for loop. */
414
416
417#endif /* TUI_TUI_LAYOUT_H */
const char *const name
virtual void remove_windows(const char *name)=0
virtual void replace_window(const char *name, const char *new_window)=0
virtual bool first_edge_has_border_p() const =0
virtual std::unique_ptr< tui_layout_base > clone() const =0
virtual ~tui_layout_base()=default
virtual bool last_edge_has_border_p() const =0
virtual void specification(ui_file *output, int depth)=0
virtual tui_adjust_result set_height(const char *name, int new_height)=0
virtual tui_adjust_result set_width(const char *name, int new_width)=0
virtual void get_windows(std::vector< tui_win_info * > *windows)=0
tui_layout_base()=default
virtual void get_sizes(bool height, int *min_value, int *max_value)=0
DISABLE_COPY_AND_ASSIGN(tui_layout_base)
virtual std::string layout_fingerprint() const =0
virtual void apply(int x, int y, int width, int height, bool preserve_cmd_win_size_p)=0
virtual const char * get_name() const
Definition tui-layout.h:82
DISABLE_COPY_AND_ASSIGN(tui_layout_split)
void specification(ui_file *output, int depth) override
void set_weights_from_sizes()
Definition tui-layout.c:629
std::string tui_debug_weights_to_string() const
Definition tui-layout.c:639
bool first_edge_has_border_p() const override
Definition tui-layout.c:609
void get_sizes(bool height, int *min_value, int *max_value) override
Definition tui-layout.c:576
static void tui_debug_print_size_info(const std::vector< size_info > &info)
Definition tui-layout.c:657
void get_windows(std::vector< tui_win_info * > *windows) override
Definition tui-layout.h:263
std::vector< split > m_splits
Definition tui-layout.h:322
std::unique_ptr< tui_layout_base > clone() const override
Definition tui-layout.c:561
void remove_windows(const char *name) override
tui_adjust_result set_size(const char *name, int new_size, bool set_width_p)
Definition tui-layout.c:671
tui_adjust_result set_width(const char *name, int new_width) override
Definition tui-layout.h:244
void add_split(std::unique_ptr< tui_layout_split > &&layout, int weight)
Definition tui-layout.c:541
tui_layout_split(bool vertical=true)
Definition tui-layout.h:216
void replace_window(const char *name, const char *new_window) override
void add_window(const char *name, int weight)
Definition tui-layout.c:551
bool last_edge_has_border_p() const override
Definition tui-layout.c:619
tui_adjust_result set_height(const char *name, int new_height) override
Definition tui-layout.h:238
void apply(int x, int y, int width, int height, bool preserve_cmd_win_size_p) override
Definition tui-layout.c:790
std::string layout_fingerprint() const override
void get_sizes(bool height, int *min_value, int *max_value) override
Definition tui-layout.c:461
const char * get_name() const override
Definition tui-layout.h:160
bool last_edge_has_border_p() const override
Definition tui-layout.c:497
DISABLE_COPY_AND_ASSIGN(tui_layout_window)
std::unique_ptr< tui_layout_base > clone() const override
Definition tui-layout.c:438
bool first_edge_has_border_p() const override
Definition tui-layout.c:488
void get_windows(std::vector< tui_win_info * > *windows) override
Definition tui-layout.h:190
tui_layout_window(const char *name)
Definition tui-layout.h:148
void replace_window(const char *name, const char *new_window) override
Definition tui-layout.c:506
tui_adjust_result set_width(const char *name, int new_width) override
Definition tui-layout.h:170
void apply(int x, int y, int width, int height, bool preserve_cmd_win_size_p) override
Definition tui-layout.c:447
std::string m_contents
Definition tui-layout.h:202
tui_win_info * m_window
Definition tui-layout.h:206
void specification(ui_file *output, int depth) override
Definition tui-layout.c:522
tui_adjust_result set_height(const char *name, int new_height) override
Definition tui-layout.h:165
void remove_windows(const char *name) override
Definition tui-layout.h:179
std::string layout_fingerprint() const override
Definition tui-layout.c:530
Definition aarch64.h:67
bool operator!=(const known_window_names_iterator &other) const
Definition tui-layout.h:397
known_window_names_iterator(known_window_types_iterator &&iter)
Definition tui-layout.h:384
const std::string & operator*() const
Definition tui-layout.h:394
window_types_map::iterator known_window_types_iterator
Definition tui-layout.h:382
known_window_names_iterator & operator++()
Definition tui-layout.h:388
known_window_types_iterator m_iter
Definition tui-layout.h:403
std::unique_ptr< tui_layout_base > layout
Definition tui-layout.h:318
void tui_regs_layout()
Definition tui-layout.c:245
void tui_set_initial_layout()
Definition tui-layout.c:223
void tui_register_window(const char *name, window_factory &&factory)
Definition tui-layout.c:403
std::unordered_map< std::string, window_factory > window_types_map
Definition tui-layout.h:367
void tui_adjust_window_height(struct tui_win_info *win, int new_height)
Definition tui-layout.c:120
void tui_apply_current_layout(bool)
Definition tui-layout.c:70
iterator_range< known_window_names_iterator > known_window_names_range
Definition tui-layout.h:409
void tui_remove_some_windows()
Definition tui-layout.c:268
void tui_next_layout()
Definition tui-layout.c:202
tui_adjust_result
Definition tui-layout.h:36
@ FOUND
Definition tui-layout.h:40
@ NOT_FOUND
Definition tui-layout.h:38
@ HANDLED
Definition tui-layout.h:42
void tui_add_win_to_layout(enum tui_win_type)
Definition tui-layout.c:155
std::function< tui_win_info *(const char *name) window_factory)
Definition tui-layout.h:363
known_window_names_range all_known_window_names()
Definition tui-layout.c:353
void tui_adjust_window_width(struct tui_win_info *win, int new_width)
Definition tui-layout.c:128
tui_win_type
Definition tui.h:53