5bb5ef9d94180ad05841f390def6f449c7a8126a
[binutils-gdb.git] / gdb / tui / tui-data.h
1 /* TUI data manipulation routines.
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_DATA_H
23 #define TUI_TUI_DATA_H
24
25 #include "tui/tui.h"
26 #include "gdb_curses.h"
27 #include "observable.h"
28
29 /* A deleter that calls delwin. */
30 struct curses_deleter
31 {
32 void operator() (WINDOW *win) const
33 {
34 delwin (win);
35 }
36 };
37
38 #define MIN_WIN_HEIGHT 3
39
40 /* Generic window information. */
41 struct tui_win_info
42 {
43 protected:
44
45 tui_win_info () = default;
46 DISABLE_COPY_AND_ASSIGN (tui_win_info);
47
48 /* This is called after the window is resized, and should update the
49 window's contents. */
50 virtual void rerender ();
51
52 virtual void make_window ();
53
54 public:
55 tui_win_info (tui_win_info &&) = default;
56 virtual ~tui_win_info () = default;
57
58 /* Call to refresh this window. */
59 virtual void refresh_window ();
60
61 /* Make this window visible or invisible. */
62 virtual void make_visible (bool visible);
63
64 /* Return the name of this type of window. */
65 virtual const char *name () const = 0;
66
67 /* Compute the maximum height of this window. */
68 virtual int max_height () const;
69
70 /* Compute the minimum height of this window. */
71 virtual int min_height () const
72 {
73 return MIN_WIN_HEIGHT;
74 }
75
76 /* Compute the maximum width of this window. */
77 int max_width () const;
78
79 /* Compute the minimum width of this window. */
80 int min_width () const
81 {
82 return 3;
83 }
84
85 /* Return true if this window can be boxed. */
86 virtual bool can_box () const
87 {
88 return true;
89 }
90
91 /* Return the width of the box. */
92 int box_width () const
93 {
94 return can_box () ? 1 : 0;
95 }
96
97 /* Return the size of the box. */
98 int box_size () const
99 {
100 return 2 * box_width ();
101 }
102
103 /* Resize this window. The parameters are used to set the window's
104 size and position. */
105 virtual void resize (int height, int width,
106 int origin_x, int origin_y);
107
108 /* Return true if this window is visible. */
109 bool is_visible () const
110 {
111 return handle != nullptr && tui_active;
112 }
113
114 /* Return true if this window can accept the focus. */
115 virtual bool can_focus () const
116 {
117 return true;
118 }
119
120 /* Disable output until the next call to doupdate. */
121 void no_refresh ()
122 {
123 if (handle != nullptr)
124 wnoutrefresh (handle.get ());
125 }
126
127 /* Called after the tab width has been changed. */
128 virtual void update_tab_width ()
129 {
130 }
131
132 /* Set whether this window is highlighted. */
133 void set_highlight (bool highlight)
134 {
135 is_highlighted = highlight;
136 }
137
138 /* Methods to scroll the contents of this window. Note that they
139 are named with "_scroll" coming at the end because the more
140 obvious "scroll_forward" is defined as a macro in term.h. */
141 void forward_scroll (int num_to_scroll);
142 void backward_scroll (int num_to_scroll);
143 void left_scroll (int num_to_scroll);
144 void right_scroll (int num_to_scroll);
145
146 /* Return true if this window can be scrolled, false otherwise. */
147 virtual bool can_scroll () const
148 {
149 return true;
150 }
151
152 /* Called for each mouse click inside this window. Coordinates MOUSE_X
153 and MOUSE_Y are 0-based relative to the window, and MOUSE_BUTTON can
154 be 1 (left), 2 (middle), or 3 (right). */
155 virtual void click (int mouse_x, int mouse_y, int mouse_button)
156 {
157 }
158
159 void check_and_display_highlight_if_needed ();
160
161 /* A helper function to change the title and then redraw the
162 surrounding box, if needed. */
163 void set_title (std::string &&new_title);
164
165 /* Return a reference to the current window title. */
166 const std::string &title () const
167 { return m_title; }
168
169 /* Window handle. */
170 std::unique_ptr<WINDOW, curses_deleter> handle;
171 /* Window width. */
172 int width = 0;
173 /* Window height. */
174 int height = 0;
175 /* Origin of window. */
176 int x = 0;
177 int y = 0;
178
179 /* Is this window highlighted? */
180 bool is_highlighted = false;
181
182 protected:
183
184 /* Scroll the contents vertically. This is only called via
185 forward_scroll and backward_scroll. */
186 virtual void do_scroll_vertical (int num_to_scroll) = 0;
187
188 /* Scroll the contents horizontally. This is only called via
189 left_scroll and right_scroll. */
190 virtual void do_scroll_horizontal (int num_to_scroll) = 0;
191
192 private:
193 /* Window title to display. */
194 std::string m_title;
195 };
196
197 /* Constant definitions. */
198 #define SRC_NAME "src"
199 #define CMD_NAME "cmd"
200 #define DATA_NAME "regs"
201 #define DISASSEM_NAME "asm"
202 #define STATUS_NAME "status"
203
204 /* Global Data. */
205 extern struct tui_win_info *tui_win_list[MAX_MAJOR_WINDOWS];
206
207 #define TUI_SRC_WIN ((tui_source_window *) tui_win_list[SRC_WIN])
208 #define TUI_DISASM_WIN ((tui_disasm_window *) tui_win_list[DISASSEM_WIN])
209 #define TUI_DATA_WIN ((tui_data_window *) tui_win_list[DATA_WIN])
210 #define TUI_CMD_WIN ((tui_cmd_window *) tui_win_list[CMD_WIN])
211 #define TUI_STATUS_WIN ((tui_locator_window *) tui_win_list[STATUS_WIN])
212
213 /* All the windows that are currently instantiated, in layout
214 order. */
215 extern std::vector<tui_win_info *> tui_windows;
216
217 /* Return a range adapter for iterating over TUI windows. */
218 static inline std::vector<tui_win_info *> &
219 all_tui_windows ()
220 {
221 return tui_windows;
222 }
223
224 /* Data Manipulation Functions. */
225 extern int tui_term_height (void);
226 extern void tui_set_term_height_to (int);
227 extern int tui_term_width (void);
228 extern void tui_set_term_width_to (int);
229 extern struct tui_win_info *tui_win_with_focus (void);
230 extern bool tui_win_resized ();
231 extern void tui_set_win_resized_to (bool);
232
233 extern struct tui_win_info *tui_next_win (struct tui_win_info *);
234 extern struct tui_win_info *tui_prev_win (struct tui_win_info *);
235
236 extern unsigned int tui_tab_width;
237
238 #endif /* TUI_TUI_DATA_H */