OpenTTD Source 20261112-master-gbcbec5e63f
console_gui.cpp
Go to the documentation of this file.
1/*
2 * This file is part of OpenTTD.
3 * OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
4 * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
5 * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <https://www.gnu.org/licenses/old-licenses/gpl-2.0>.
6 */
7
9
10#include "stdafx.h"
12#include "textbuf_type.h"
13#include "window_gui.h"
14#include "autocompletion.h"
15#include "console_gui.h"
16#include "console_internal.h"
17#include "window_func.h"
18#include "string_func.h"
19#include "strings_func.h"
20#include "gfx_func.h"
21#include "gfx_layout.h"
22#include "settings_type.h"
23#include "console_func.h"
24#include "rev.h"
26#include "timer/timer.h"
27#include "timer/timer_window.h"
28
30
31#include "table/strings.h"
32
33#include "safeguards.h"
34
35static const uint ICON_HISTORY_SIZE = 20;
36static const uint ICON_RIGHT_BORDERWIDTH = 10;
37static const uint ICON_BOTTOM_BORDERWIDTH = 12;
38
42struct IConsoleLine {
43 std::string buffer;
45 uint16_t time;
46
47 IConsoleLine() : buffer(), colour(TextColour::Begin), time(0)
48 {
49
50 }
51
58 buffer(std::move(buffer)),
60 time(0)
61 {
62 }
63};
64
66static std::deque<IConsoleLine> _iconsole_buffer;
67
68static bool TruncateBuffer();
69
70class ConsoleAutoCompletion final : public AutoCompletion {
71public:
72 using AutoCompletion::AutoCompletion;
73
74private:
75 std::vector<std::string> GetSuggestions(std::string_view prefix, std::string_view query) override
76 {
78 std::vector<std::string> suggestions;
79
80 /* We only suggest commands or aliases, so we only do it for the first token or an argument to help command. */
81 if (!prefix.empty() && prefix != "help") {
82 return suggestions;
83 }
84
85 for (const auto &[_, command] : IConsole::Commands()) {
86 if (command.name.starts_with(query)) {
87 suggestions.push_back(command.name);
88 }
89 }
90 for (const auto &[_, alias] : IConsole::Aliases()) {
91 if (alias.name.starts_with(query)) {
92 suggestions.push_back(alias.name);
93 }
94 }
95
96 return suggestions;
97 }
98
99 void ApplySuggestion(std::string_view prefix, std::string_view suggestion) override
100 {
101 this->textbuf->Assign(fmt::format("{}{} ", prefix, suggestion));
102 }
103};
104
107static ConsoleAutoCompletion _iconsole_tab_completion(&_iconsole_cmdline);
108static std::deque<std::string> _iconsole_history;
109static ptrdiff_t _iconsole_historypos;
111
112/* *************** *
113 * end of header *
114 * *************** */
115
116static void IConsoleClearCommand()
117{
118 _iconsole_cmdline.DeleteAll();
119 _iconsole_tab_completion.Reset();
120 SetWindowDirty(WindowClass::Console, 0);
121}
122
123static inline void IConsoleResetHistoryPos()
124{
125 _iconsole_historypos = -1;
126}
127
128
129static std::optional<std::string_view> IConsoleHistoryAdd(std::string_view cmd);
130static void IConsoleHistoryNavigate(int direction);
131
132static constexpr std::initializer_list<NWidgetPart> _nested_console_window_widgets = {
134};
135
138 WindowPosition::Manual, {}, 0, 0,
139 WindowClass::Console, WindowClass::None,
140 {},
141 _nested_console_window_widgets
142);
143
144struct IConsoleWindow : Window
145{
146 static size_t scroll;
147 int line_height = 0;
148 int line_offset = 0;
149 int cursor_width = 0;
150
151 IConsoleWindow() : Window(_console_window_desc)
152 {
154
155 this->InitNested(0);
156 ResizeWindow(this, _screen.width, _screen.height / 3);
157 }
158
159 void OnInit() override
160 {
161 this->line_height = GetCharacterHeight(FontSize::Normal) + WidgetDimensions::scaled.hsep_normal;
162 this->line_offset = GetStringBoundingBox("] ").width + WidgetDimensions::scaled.frametext.left;
163 this->cursor_width = GetCharacterWidth(FontSize::Normal, '_');
164 }
165
166 void Close([[maybe_unused]] int data = 0) override
167 {
170 this->Window::Close();
171 }
172
177 void Scroll(int amount)
178 {
179 if (amount < 0) {
180 size_t namount = static_cast<size_t>(-amount);
181 IConsoleWindow::scroll = (namount > IConsoleWindow::scroll) ? 0 : IConsoleWindow::scroll - namount;
182 } else {
183 assert(this->height >= 0 && this->line_height > 0);
184 size_t visible_lines = static_cast<size_t>(this->height / this->line_height);
185 size_t max_scroll = (visible_lines > _iconsole_buffer.size()) ? 0 : _iconsole_buffer.size() + 1 - visible_lines;
186 IConsoleWindow::scroll = std::min<size_t>(IConsoleWindow::scroll + amount, max_scroll);
187 }
188 this->SetDirty();
189 }
190
191 void OnPaint() override
192 {
193 const int right = this->width - WidgetDimensions::scaled.frametext.right;
194
195 GfxFillRect(0, 0, this->width - 1, this->height - 1, PC_BLACK);
196 int ypos = this->height - this->line_height - WidgetDimensions::scaled.hsep_normal;
197 for (size_t line_index = IConsoleWindow::scroll; line_index < _iconsole_buffer.size(); line_index++) {
198 const IConsoleLine &print = _iconsole_buffer[line_index];
199 ypos = DrawStringMultiLine(WidgetDimensions::scaled.frametext.left, right, -this->line_height, ypos, GetString(STR_JUST_RAW_STRING, print.buffer), print.colour, {AlignmentH::ForceLeft, AlignmentV::Bottom}) - WidgetDimensions::scaled.hsep_normal;
200 if (ypos < 0) break;
201 }
202 /* If the text is longer than the window, don't show the starting ']' */
203 int delta = this->width - WidgetDimensions::scaled.frametext.right - cursor_width - this->line_offset - _iconsole_cmdline.pixels - ICON_RIGHT_BORDERWIDTH;
204 if (delta > 0) {
205 DrawString(WidgetDimensions::scaled.frametext.left, right, this->height - this->line_height, "]", CC_COMMAND, AlignmentH::ForceLeft);
206 delta = 0;
207 }
208
209 /* If we have a marked area, draw a background highlight. */
210 if (_iconsole_cmdline.marklength != 0) GfxFillRect(this->line_offset + delta + _iconsole_cmdline.markxoffs, this->height - this->line_height, this->line_offset + delta + _iconsole_cmdline.markxoffs + _iconsole_cmdline.marklength, this->height - 1, PC_DARK_RED);
211
212 DrawString(this->line_offset + delta, right, this->height - this->line_height, _iconsole_cmdline.GetText(), CC_COMMAND, AlignmentH::ForceLeft);
213
214 if (_focused_window == this && _iconsole_cmdline.caret) {
215 DrawString(this->line_offset + delta + _iconsole_cmdline.caretxoffs, right, this->height - this->line_height, "_", TextColour::White, AlignmentH::ForceLeft);
216 }
217 }
218
220 const IntervalTimer<TimerWindow> truncate_interval = {std::chrono::seconds(3), [this](auto) {
221 assert(this->height >= 0 && this->line_height > 0);
222 size_t visible_lines = static_cast<size_t>(this->height / this->line_height);
223
224 if (TruncateBuffer() && IConsoleWindow::scroll + visible_lines > _iconsole_buffer.size()) {
225 size_t max_scroll = (visible_lines > _iconsole_buffer.size()) ? 0 : _iconsole_buffer.size() + 1 - visible_lines;
226 IConsoleWindow::scroll = std::min<size_t>(IConsoleWindow::scroll, max_scroll);
227 this->SetDirty();
228 }
229 }};
230
231 void OnMouseLoop() override
232 {
233 if (_iconsole_cmdline.HandleCaret()) this->SetDirty();
234 }
235
236 EventState OnKeyPress([[maybe_unused]] char32_t key, uint16_t keycode) override
237 {
238 if (_focused_window != this) return EventState::NotHandled;
239
240 const int scroll_height = (this->height / this->line_height) - 1;
241 switch (keycode) {
242 case WKC_UP:
244 this->SetDirty();
245 break;
246
247 case WKC_DOWN:
249 this->SetDirty();
250 break;
251
252 case WKC_SHIFT | WKC_PAGEDOWN:
253 this->Scroll(-scroll_height);
254 break;
255
256 case WKC_SHIFT | WKC_PAGEUP:
257 this->Scroll(scroll_height);
258 break;
259
260 case WKC_SHIFT | WKC_DOWN:
261 this->Scroll(-1);
262 break;
263
264 case WKC_SHIFT | WKC_UP:
265 this->Scroll(1);
266 break;
267
268 case WKC_BACKQUOTE:
270 break;
271
272 case WKC_RETURN: case WKC_NUM_ENTER: {
273 /* We always want the ] at the left side; we always force these strings to be left
274 * aligned anyway. So enforce this in all cases by adding a left-to-right marker,
275 * otherwise it will be drawn at the wrong side with right-to-left texts. */
276 IConsolePrint(CC_COMMAND, LRM "] {}", _iconsole_cmdline.GetText());
277 auto cmd = IConsoleHistoryAdd(_iconsole_cmdline.GetText());
278 IConsoleClearCommand();
279
280 if (cmd.has_value()) IConsoleCmdExec(*cmd);
281 break;
282 }
283
284 case WKC_CTRL | WKC_RETURN:
286 IConsoleResize(this);
288 break;
289
290 case (WKC_CTRL | 'L'):
291 IConsoleCmdExec("clear");
292 break;
293
294 case WKC_TAB:
295 if (_iconsole_tab_completion.AutoComplete()) {
296 this->SetDirty();
297 }
298 break;
299
300 default: {
301 HandleKeyPressResult handle_result = _iconsole_cmdline.HandleKeyPress(key, keycode);
302 if (handle_result != HandleKeyPressResult::NotHandled) {
303 if (handle_result == HandleKeyPressResult::Editing) {
304 _iconsole_tab_completion.Reset();
305 }
306 IConsoleWindow::scroll = 0;
307 IConsoleResetHistoryPos();
308 this->SetDirty();
309 } else {
311 }
312 break;
313 }
314 }
315 return EventState::Handled;
316 }
317
318 void InsertTextString(WidgetID, std::string_view str, bool marked, std::optional<size_t> caret, std::optional<size_t> insert_location, std::optional<size_t> replacement_end) override
319 {
320 if (_iconsole_cmdline.InsertString(str, marked, caret, insert_location, replacement_end)) {
321 _iconsole_tab_completion.Reset();
322 IConsoleWindow::scroll = 0;
323 IConsoleResetHistoryPos();
324 this->SetDirty();
325 }
326 }
327
328 const Textbuf *GetFocusedTextbuf() const override
329 {
330 return &_iconsole_cmdline;
331 }
332
333 Point GetCaretPosition() const override
334 {
335 int delta = std::min<int>(this->width - this->line_offset - _iconsole_cmdline.pixels - ICON_RIGHT_BORDERWIDTH, 0);
336 Point pt = {this->line_offset + delta + _iconsole_cmdline.caretxoffs, this->height - this->line_height};
337
338 return pt;
339 }
340
341 Rect GetTextBoundingRect(size_t from, size_t to) const override
342 {
343 int delta = std::min<int>(this->width - this->line_offset - _iconsole_cmdline.pixels - ICON_RIGHT_BORDERWIDTH, 0);
344
345 const auto p1 = GetCharPosInString(_iconsole_cmdline.GetText(), from, FontSize::Normal);
346 const auto p2 = from != to ? GetCharPosInString(_iconsole_cmdline.GetText(), to, FontSize::Normal) : p1;
347
348 Rect r = {this->line_offset + delta + p1.left, this->height - this->line_height, this->line_offset + delta + p2.right, this->height};
349 return r;
350 }
351
352 ptrdiff_t GetTextCharacterAtPosition(const Point &pt) const override
353 {
354 int delta = std::min<int>(this->width - this->line_offset - _iconsole_cmdline.pixels - ICON_RIGHT_BORDERWIDTH, 0);
355
356 if (!IsInsideMM(pt.y, this->height - this->line_height, this->height)) return -1;
357
358 return GetCharAtPosition(_iconsole_cmdline.GetText(), pt.x - delta);
359 }
360
361 void OnMouseWheel(int wheel, WidgetID widget) override
362 {
363 if (widget != WID_C_BACKGROUND) return;
364 this->Scroll(-wheel);
365 }
366
367 void OnFocus() override
368 {
370 }
371
372 void OnFocusLost(bool) override
373 {
375 }
376};
377
378size_t IConsoleWindow::scroll = 0;
379
380void IConsoleGUIInit()
381{
382 IConsoleResetHistoryPos();
384
385 IConsoleClearBuffer();
386
387 IConsolePrint(TextColour::LightBlue, "OpenTTD Game Console Revision 7 - {}", _openttd_revision);
388 IConsolePrint(CC_WHITE, "------------------------------------");
389 IConsolePrint(CC_WHITE, "use \"help\" for more information.");
391 IConsoleClearCommand();
392}
393
394void IConsoleClearBuffer()
395{
396 _iconsole_buffer.clear();
397}
398
399void IConsoleGUIFree()
400{
401 IConsoleClearBuffer();
402}
403
409{
410 switch (_iconsole_mode) {
412 w->height = _screen.height / 3;
413 w->width = _screen.width;
414 break;
416 w->height = _screen.height - ICON_BOTTOM_BORDERWIDTH;
417 w->width = _screen.width;
418 break;
419 default: return;
420 }
421
423}
424
427{
428 switch (_iconsole_mode) {
430 new IConsoleWindow();
431 break;
432
435 CloseWindowById(WindowClass::Console, 0);
436 break;
437 }
438
440}
441
447
454static std::optional<std::string_view> IConsoleHistoryAdd(std::string_view cmd)
455{
456 /* Strip all spaces at the begin */
457 while (!cmd.empty() && IsWhitespace(cmd[0])) cmd.remove_prefix(1);
458
459 /* Do not put empty command in history */
460 if (cmd.empty()) return std::nullopt;
461
462 /* Do not put in history if command is same as previous */
463 if (_iconsole_history.empty() || _iconsole_history.front() != cmd) {
464 _iconsole_history.emplace_front(cmd);
465 while (_iconsole_history.size() > ICON_HISTORY_SIZE) _iconsole_history.pop_back();
466 }
467
468 /* Reset the history position */
469 IConsoleResetHistoryPos();
470 return _iconsole_history.front();
471}
472
477static void IConsoleHistoryNavigate(int direction)
478{
479 if (_iconsole_history.empty()) return; // Empty history
480 _iconsole_historypos = Clamp<ptrdiff_t>(_iconsole_historypos + direction, -1, _iconsole_history.size() - 1);
481
482 if (_iconsole_historypos == -1) {
483 _iconsole_cmdline.DeleteAll();
484 } else {
485 _iconsole_cmdline.Assign(_iconsole_history[_iconsole_historypos]);
486 }
487 _iconsole_tab_completion.Reset();
488}
489
499void IConsoleGUIPrint(ExtendedTextColour colour_code, const std::string &str)
500{
501 _iconsole_buffer.push_front(IConsoleLine(str, colour_code));
502 SetWindowDirty(WindowClass::Console, 0);
503}
504
512static bool TruncateBuffer()
513{
514 bool need_truncation = false;
515 size_t count = 0;
516 for (IConsoleLine &line : _iconsole_buffer) {
517 count++;
518 line.time++;
519 if (line.time > _settings_client.gui.console_backlog_timeout && count > _settings_client.gui.console_backlog_length) {
520 /* Any messages after this are older and need to be truncated */
521 need_truncation = true;
522 break;
523 }
524 }
525
526 if (need_truncation) {
527 _iconsole_buffer.resize(count - 1);
528 }
529
530 return need_truncation;
531}
532
533
540{
541 /* A normal text colour is used. */
543
544 /* A text colour from the palette is used; must be the company
545 * colour gradient, so it must be one of those. */
546 for (Colours i : EnumRange(Colours::End)) {
547 if (ExtendedTextColour{GetColourGradient(i, Shade::Normal)} == c) return true;
548 }
549
550 return false;
551}
Generic auto-completion engine.
@ Begin
Used for iteration.
Definition cargotype.h:22
std::string_view query
Last token of the text. This is used to based the suggestions on.
std::string_view prefix
Prefix of the text before the last space.
constexpr bool Test(Tvalue_type value) const
Test if the value-th bit is set.
std::vector< std::string > GetSuggestions(std::string_view prefix, std::string_view query) override
Get suggestions for auto completion with the given 'query' input text.
void ApplySuggestion(std::string_view prefix, std::string_view suggestion) override
Format the given suggestion after the given prefix, and write that to the buffer.
Iterate a range of enum values.
An interval timer will fire every interval, and will continue to fire until it is deleted.
Definition timer.h:76
static const std::string_view WHITESPACE_NO_NEWLINE
ASCII whitespace characters, excluding new-line.
virtual void EditBoxLostFocus()
An edit box lost the input focus.
static VideoDriver * GetInstance()
Get the currently active instance of the video driver.
virtual void EditBoxGainedFocus()
An edit box gained the input focus.
static WidgetDimensions scaled
Widget dimensions scaled for current zoom level.
Definition window_gui.h:30
void IConsolePrint(ExtendedTextColour colour_code, const std::string &string)
Handle the printing of text entered into the console or redirected there by any other means.
Definition console.cpp:91
void IConsoleCmdExec(std::string_view command_string, const uint recurse_count)
Execute a given command passed to us.
Definition console.cpp:272
Console functions used outside of the console code.
IConsoleMode _iconsole_mode
Console modes.
static Textbuf _iconsole_cmdline(ICON_CMDLN_SIZE)
Main console cmd buffer.
void IConsoleGUIPrint(ExtendedTextColour colour_code, const std::string &str)
Handle the printing of text entered into the console or redirected there by any other means.
static std::optional< std::string_view > IConsoleHistoryAdd(std::string_view cmd)
Add the entered line into the history so you can look it back scroll, etc.
void IConsoleSwitch()
Toggle in-game console between opened and closed.
void IConsoleClose()
Close the in-game console.
static bool TruncateBuffer()
Remove old lines from the backlog buffer.
bool IsValidConsoleColour(ExtendedTextColour c)
Check whether the given TextColour is valid for console usage.
IConsoleMode _iconsole_mode
Console modes.
static WindowDesc _console_window_desc(WindowPosition::Manual, {}, 0, 0, WindowClass::Console, WindowClass::None, {}, _nested_console_window_widgets)
Window definition for the console window.
static std::deque< IConsoleLine > _iconsole_buffer
The console backlog buffer.
void IConsoleResize(Window *w)
Change the size of the in-game console window after the screen size changed, or the window state chan...
static void IConsoleHistoryNavigate(int direction)
Navigate Up/Down in the history of typed commands.
GUI related functions in the console.
Internally used functions for the console.
static const uint ICON_CMDLN_SIZE
maximum length of a typed in command
static const TextColour CC_WHITE
White console lines for various things such as the welcome.
static const TextColour CC_COMMAND
Colour for the console's commands.
IConsoleMode
Modes of the in-game console.
@ Closed
In-game console is closed.
@ Opened
In-game console is opened, upper 1/3 of the screen.
@ Full
In-game console is opened, whole screen.
Types related to the console widgets.
@ WID_C_BACKGROUND
Background of the console.
int GetCharacterHeight(FontSize size)
Get height of a character for a given font size.
Definition fontcache.cpp:88
@ ForceLeft
Force align to the left.
Dimension GetStringBoundingBox(std::string_view str, FontSize start_fontsize)
Return the string dimension in pixels.
Definition gfx.cpp:894
uint8_t GetCharacterWidth(FontSize size, char32_t key)
Return width of character glyph.
Definition gfx.cpp:1272
void GfxFillRect(int left, int top, int right, int bottom, const std::variant< PixelColour, PaletteID > &colour, FillRectMode mode)
Applies a certain FillRectMode-operation to a rectangle [left, right] x [top, bottom] on the screen.
Definition gfx.cpp:111
int DrawStringMultiLine(int left, int right, int top, int bottom, std::string_view str, ExtendedTextColour colour, Alignment align, bool underline, FontSize fontsize)
Draw string, possibly over multiple lines.
Definition gfx.cpp:782
int DrawString(int left, int right, int top, std::string_view str, ExtendedTextColour colour, Alignment align, bool underline, FontSize fontsize)
Draw string, possibly truncated to make it fit in its allocated space.
Definition gfx.cpp:663
Functions related to the gfx engine.
ParagraphLayouter::Position GetCharPosInString(std::string_view str, size_t pos, FontSize start_fontsize)
Get the leading corner of a character in a single-line string relative to the start of the string.
ptrdiff_t GetCharAtPosition(std::string_view str, int x, FontSize start_fontsize)
Get the character from a string that is drawn at a specific position.
Functions related to laying out the texts.
@ Normal
Index of the normal font in the font tables.
Definition gfx_type.h:249
Colours
One of 16 base colours used for companies and windows/widgets.
Definition gfx_type.h:283
@ Invalid
Invalid marker.
Definition gfx_type.h:302
@ End
End-of-array marker.
Definition gfx_type.h:301
@ IsPaletteColour
Colour value is already a real palette colour index, not an index of a StringColour.
Definition gfx_type.h:341
TextColour
Colour of the strings, see _string_colourmap in table/string_colours.h or docs/ottd-colourtext-palett...
Definition gfx_type.h:315
@ Begin
Marker for the begin of the range.
Definition gfx_type.h:316
@ White
White colour.
Definition gfx_type.h:330
@ LightBlue
Light blue colour.
Definition gfx_type.h:331
@ End
Marker for the end of the range.
Definition gfx_type.h:335
constexpr NWidgetPart NWidget(WidgetType tp, Colours col, WidgetID idx=INVALID_WIDGET)
Widget part function for starting a new 'real' widget.
constexpr NWidgetPart SetResize(int16_t dx, int16_t dy)
Widget part function for setting the resize step.
void SetDirty() const
Mark entire window as dirty (in need of re-paint).
Definition window.cpp:1013
void MarkWholeScreenDirty()
This function mark the whole screen as dirty.
Definition gfx.cpp:1548
#define Point
Macro that prevents name conflicts between included headers.
constexpr bool IsInsideMM(const size_t x, const size_t min, const size_t max) noexcept
Checks if a value is in an interval.
constexpr T Clamp(const T a, const T min, const T max)
Clamp a value between an interval.
Definition math_func.hpp:79
PixelColour GetColourGradient(Colours colour, Shade shade)
Get colour gradient palette index.
Definition palette.cpp:393
@ Normal
Normal colour shade.
static constexpr PixelColour PC_DARK_RED
Dark red palette colour.
static constexpr PixelColour PC_BLACK
Black palette colour.
Declaration of OTTD revision dependent variables.
A number of safeguards to prevent using unsafe methods.
ClientSettings _settings_client
The current settings for this game.
Definition settings.cpp:60
Types related to global configuration settings.
Definition of base types and functions in a cross-platform compatible way.
Parse strings.
Functions related to low-level strings.
bool IsWhitespace(char32_t c)
Check whether UNICODE character is whitespace or not, i.e.
#define LRM
A left-to-right marker, marks the next character as left-to-right.
Definition string_type.h:19
std::string GetString(StringID string)
Resolve the given StringID into a std::string with formatting but no parameters.
Definition strings.cpp:423
Functions related to OTTD's strings.
T y
Y coordinate.
T x
X coordinate.
value_type width
The width of the rectangle.
Container for the text colour and some text colour related flags for drawing.
Definition gfx_type.h:349
ExtendedTextColourFlags flags
The flags.
Definition gfx_type.h:364
TextColour colour
The colour.
Definition gfx_type.h:363
Container for a single line of console output.
ExtendedTextColour colour
The colour of the line.
IConsoleLine(std::string buffer, ExtendedTextColour colour)
Initialize the console line.
uint16_t time
The amount of time the line is in the backlog.
std::string buffer
The data to store.
void OnFocusLost(bool) override
The window has lost focus.
EventState OnKeyPress(char32_t key, uint16_t keycode) override
A key has been pressed.
void Scroll(int amount)
Scroll the content of the console.
const Textbuf * GetFocusedTextbuf() const override
Get the current input text buffer.
ptrdiff_t GetTextCharacterAtPosition(const Point &pt) const override
Get the character that is rendered at a position by the focused edit box.
Rect GetTextBoundingRect(size_t from, size_t to) const override
Get the bounding rectangle for a text range if an edit box has the focus.
void OnMouseLoop() override
Called for every mouse loop run, which is at least once per (game) tick.
void OnInit() override
Notification that the nested widget tree gets initialized.
void Close(int data=0) override
Hide the window and all its child windows, and mark them for a later deletion.
int line_height
Height of one line of text in the console.
void InsertTextString(WidgetID, std::string_view str, bool marked, std::optional< size_t > caret, std::optional< size_t > insert_location, std::optional< size_t > replacement_end) override
Insert a text string at the cursor position into the edit box widget.
void OnPaint() override
The window must be repainted.
const IntervalTimer< TimerWindow > truncate_interval
Check on a regular interval if the console buffer needs truncating.
Point GetCaretPosition() const override
Get the current caret position if an edit box has the focus.
void OnMouseWheel(int wheel, WidgetID widget) override
The mouse wheel has been turned.
void OnFocus() override
The window has gained focus.
Specification of a rectangle with absolute coordinates of all edges.
Helper/buffer for input fields.
High level window description.
Definition window_gui.h:171
Data structure for an opened window.
Definition window_gui.h:272
virtual void Close(int data=0)
Hide the window and all its child windows, and mark them for a later deletion.
Definition window.cpp:1150
Window(WindowDesc &desc)
Empty constructor, initialization has been moved to InitNested() called from the constructor of the d...
Definition window.cpp:1879
void InitNested(WindowNumber number=0)
Perform complete initialization of the Window with nested widgets, to allow use.
Definition window.cpp:1869
int height
Height of the window (number of pixels down in y direction).
Definition window_gui.h:311
int width
width of the window (number of pixels to the right in x direction)
Definition window_gui.h:310
Stuff related to text buffers.
HandleKeyPressResult
Return values for Textbuf::HandleKeypress.
@ Editing
Textbuf content changed.
@ NotHandled
Key does not affect editboxes.
Definition of Interval and OneShot timers.
Definition of the Window system.
Base of all video drivers.
@ WWT_EMPTY
Empty widget, place holder to reserve space in widget tree.
Definition widget_type.h:37
void CloseWindowById(WindowClass cls, WindowNumber number, bool force, int data)
Close a window by its class and window number (if it is open).
Definition window.cpp:1242
Window * _focused_window
Window that currently has focus.
Definition window.cpp:80
void ResizeWindow(Window *w, int delta_x, int delta_y, bool clamp_to_screen, bool schedule_resize)
Resize the window.
Definition window.cpp:2146
void SetWindowDirty(WindowClass cls, WindowNumber number)
Mark window as dirty (in need of repainting).
Definition window.cpp:3255
Window functions not directly related to making/drawing windows.
Functions, definitions and such used only by the GUI.
@ Manual
Manually align the window (so no automatic location finding).
Definition window_gui.h:144
int WidgetID
Widget ID.
Definition window_type.h:21
EventState
State of handling an event.
@ Handled
The passed event is handled.
@ NotHandled
The passed event is not handled.