Translations update
[openttd/fttd.git] / src / console_gui.cpp
blob887dfda60cd616536611b04e2773b43f8040570b
1 /* $Id$ */
3 /*
4 * This file is part of OpenTTD.
5 * 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.
6 * 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.
7 * 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 <http://www.gnu.org/licenses/>.
8 */
10 /** @file console_gui.cpp Handling the GUI of the in-game console. */
12 #include "stdafx.h"
13 #include "textbuf_type.h"
14 #include "window_gui.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 "settings_type.h"
22 #include "console_func.h"
23 #include "rev.h"
24 #include "video/video_driver.hpp"
26 #include "widgets/console_widget.h"
28 #include "table/strings.h"
30 static const uint ICON_HISTORY_SIZE = 20;
31 static const uint ICON_LINE_SPACING = 2;
32 static const uint ICON_RIGHT_BORDERWIDTH = 10;
33 static const uint ICON_BOTTOM_BORDERWIDTH = 12;
35 /**
36 * Container for a single line of console output
38 struct IConsoleLine {
39 static IConsoleLine *front; ///< The front of the console backlog buffer
40 static int size; ///< The amount of items in the backlog
42 IConsoleLine *previous; ///< The previous console message.
43 char *buffer; ///< The data to store.
44 TextColour colour; ///< The colour of the line.
45 uint16 time; ///< The amount of time the line is in the backlog.
47 /**
48 * Initialize the console line.
49 * @param buffer the data to print.
50 * @param colour the colour of the line.
52 IConsoleLine(char *buffer, TextColour colour) :
53 previous(IConsoleLine::front),
54 buffer(buffer),
55 colour(colour),
56 time(0)
58 IConsoleLine::front = this;
59 IConsoleLine::size++;
62 /**
63 * Clear this console line and any further ones.
65 ~IConsoleLine()
67 IConsoleLine::size--;
68 free(buffer);
70 delete previous;
73 /**
74 * Get the index-ed item in the list.
76 static const IConsoleLine *Get(uint index)
78 const IConsoleLine *item = IConsoleLine::front;
79 while (index != 0 && item != NULL) {
80 index--;
81 item = item->previous;
84 return item;
87 /**
88 * Truncate the list removing everything older than/more than the amount
89 * as specified in the config file.
90 * As a side effect also increase the time the other lines have been in
91 * the list.
92 * @return true if and only if items got removed.
94 static bool Truncate()
96 IConsoleLine *cur = IConsoleLine::front;
97 if (cur == NULL) return false;
99 int count = 1;
100 for (IConsoleLine *item = cur->previous; item != NULL; count++, cur = item, item = item->previous) {
101 if (item->time > _settings_client.gui.console_backlog_timeout &&
102 count > _settings_client.gui.console_backlog_length) {
103 delete item;
104 cur->previous = NULL;
105 return true;
108 if (item->time != MAX_UVALUE(uint16)) item->time++;
111 return false;
115 * Reset the complete console line backlog.
117 static void Reset()
119 delete IConsoleLine::front;
120 IConsoleLine::front = NULL;
121 IConsoleLine::size = 0;
125 /* static */ IConsoleLine *IConsoleLine::front = NULL;
126 /* static */ int IConsoleLine::size = 0;
129 /* ** main console cmd buffer ** */
130 static Textbuf _iconsole_cmdline(ICON_CMDLN_SIZE);
131 static char *_iconsole_history[ICON_HISTORY_SIZE];
132 static int _iconsole_historypos;
133 IConsoleModes _iconsole_mode;
135 /* *************** *
136 * end of header *
137 * *************** */
139 static void IConsoleClearCommand()
141 memset(_iconsole_cmdline.buf, 0, ICON_CMDLN_SIZE);
142 _iconsole_cmdline.chars = _iconsole_cmdline.bytes = 1; // only terminating zero
143 _iconsole_cmdline.pixels = 0;
144 _iconsole_cmdline.caretpos = 0;
145 _iconsole_cmdline.caretxoffs = 0;
146 SetWindowDirty(WC_CONSOLE, 0);
149 static inline void IConsoleResetHistoryPos()
151 _iconsole_historypos = -1;
155 static const char *IConsoleHistoryAdd(const char *cmd);
156 static void IConsoleHistoryNavigate(int direction);
158 static const struct NWidgetPart _nested_console_window_widgets[] = {
159 NWidget(WWT_EMPTY, INVALID_COLOUR, WID_C_BACKGROUND), SetResize(1, 1),
162 static WindowDesc _console_window_desc(
163 WDP_MANUAL, NULL, 0, 0,
164 WC_CONSOLE, WC_NONE,
166 _nested_console_window_widgets, lengthof(_nested_console_window_widgets)
169 struct IConsoleWindow : Window
171 static int scroll;
172 int line_height; ///< Height of one line of text in the console.
173 int line_offset;
175 IConsoleWindow() : Window(&_console_window_desc)
177 _iconsole_mode = ICONSOLE_OPENED;
178 this->line_height = FONT_HEIGHT_NORMAL + ICON_LINE_SPACING;
179 this->line_offset = GetStringBoundingBox("] ").width + 5;
181 this->InitNested(0);
182 ResizeWindow(this, _screen.width, _screen.height / 3);
185 ~IConsoleWindow()
187 _iconsole_mode = ICONSOLE_CLOSED;
191 * Scroll the content of the console.
192 * @param amount Number of lines to scroll back.
194 void Scroll(int amount)
196 int max_scroll = max<int>(0, IConsoleLine::size + 1 - this->height / this->line_height);
197 IConsoleWindow::scroll = Clamp<int>(IConsoleWindow::scroll + amount, 0, max_scroll);
198 this->SetDirty();
201 virtual void OnPaint()
203 const int right = this->width - 5;
205 GfxFillRect(0, 0, this->width - 1, this->height - 1, PC_BLACK);
206 int ypos = this->height - this->line_height;
207 for (const IConsoleLine *print = IConsoleLine::Get(IConsoleWindow::scroll); print != NULL; print = print->previous) {
208 SetDParamStr(0, print->buffer);
209 ypos = DrawStringMultiLine(5, right, -this->line_height, ypos, STR_JUST_RAW_STRING, print->colour, SA_LEFT | SA_BOTTOM | SA_FORCE) - ICON_LINE_SPACING;
210 if (ypos < 0) break;
212 /* If the text is longer than the window, don't show the starting ']' */
213 int delta = this->width - this->line_offset - _iconsole_cmdline.pixels - ICON_RIGHT_BORDERWIDTH;
214 if (delta > 0) {
215 DrawString(5, right, this->height - this->line_height, "]", (TextColour)CC_COMMAND, SA_LEFT | SA_FORCE);
216 delta = 0;
219 /* If we have a marked area, draw a background highlight. */
220 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);
222 DrawString(this->line_offset + delta, right, this->height - this->line_height, _iconsole_cmdline.buf, (TextColour)CC_COMMAND, SA_LEFT | SA_FORCE);
224 if (_focused_window == this && _iconsole_cmdline.caret) {
225 DrawString(this->line_offset + delta + _iconsole_cmdline.caretxoffs, right, this->height - this->line_height, "_", TC_WHITE, SA_LEFT | SA_FORCE);
229 virtual void OnHundredthTick()
231 if (IConsoleLine::Truncate() &&
232 (IConsoleWindow::scroll > IConsoleLine::size)) {
233 IConsoleWindow::scroll = max(0, IConsoleLine::size - (this->height / this->line_height) + 1);
234 this->SetDirty();
238 virtual void OnMouseLoop()
240 if (_iconsole_cmdline.HandleCaret()) this->SetDirty();
243 virtual EventState OnKeyPress(WChar key, uint16 keycode)
245 if (_focused_window != this) return ES_NOT_HANDLED;
247 const int scroll_height = (this->height / this->line_height) - 1;
248 switch (keycode) {
249 case WKC_UP:
250 IConsoleHistoryNavigate(1);
251 this->SetDirty();
252 break;
254 case WKC_DOWN:
255 IConsoleHistoryNavigate(-1);
256 this->SetDirty();
257 break;
259 case WKC_SHIFT | WKC_PAGEDOWN:
260 this->Scroll(-scroll_height);
261 break;
263 case WKC_SHIFT | WKC_PAGEUP:
264 this->Scroll(scroll_height);
265 break;
267 case WKC_SHIFT | WKC_DOWN:
268 this->Scroll(-1);
269 break;
271 case WKC_SHIFT | WKC_UP:
272 this->Scroll(1);
273 break;
275 case WKC_BACKQUOTE:
276 IConsoleSwitch();
277 break;
279 case WKC_RETURN: case WKC_NUM_ENTER: {
280 /* We always want the ] at the left side; we always force these strings to be left
281 * aligned anyway. So enforce this in all cases by addding a left-to-right marker,
282 * otherwise it will be drawn at the wrong side with right-to-left texts. */
283 IConsolePrintF(CC_COMMAND, LRM "] %s", _iconsole_cmdline.buf);
284 const char *cmd = IConsoleHistoryAdd(_iconsole_cmdline.buf);
285 IConsoleClearCommand();
287 if (cmd != NULL) IConsoleCmdExec(cmd);
288 break;
291 case WKC_CTRL | WKC_RETURN:
292 _iconsole_mode = (_iconsole_mode == ICONSOLE_FULL) ? ICONSOLE_OPENED : ICONSOLE_FULL;
293 IConsoleResize(this);
294 MarkWholeScreenDirty();
295 break;
297 case (WKC_CTRL | 'L'):
298 IConsoleCmdExec("clear");
299 break;
301 default:
302 if (_iconsole_cmdline.HandleKeyPress(key, keycode) != HKPR_NOT_HANDLED) {
303 IConsoleWindow::scroll = 0;
304 IConsoleResetHistoryPos();
305 this->SetDirty();
306 } else {
307 return ES_NOT_HANDLED;
309 break;
311 return ES_HANDLED;
314 virtual void InsertTextString(int wid, const char *str, bool marked, const char *caret, const char *insert_location, const char *replacement_end)
316 if (_iconsole_cmdline.InsertString(str, marked, caret, insert_location, replacement_end)) {
317 IConsoleWindow::scroll = 0;
318 IConsoleResetHistoryPos();
319 this->SetDirty();
323 virtual const char *GetFocusedText() const
325 return _iconsole_cmdline.buf;
328 virtual const char *GetCaret() const
330 return _iconsole_cmdline.buf + _iconsole_cmdline.caretpos;
333 virtual const char *GetMarkedText(size_t *length) const
335 if (_iconsole_cmdline.markend == 0) return NULL;
337 *length = _iconsole_cmdline.markend - _iconsole_cmdline.markpos;
338 return _iconsole_cmdline.buf + _iconsole_cmdline.markpos;
341 virtual Point GetCaretPosition() const
343 int delta = min(this->width - this->line_offset - _iconsole_cmdline.pixels - ICON_RIGHT_BORDERWIDTH, 0);
344 Point pt = {this->line_offset + delta + _iconsole_cmdline.caretxoffs, this->height - this->line_height};
346 return pt;
349 virtual Rect GetTextBoundingRect(const char *from, const char *to) const
351 int delta = min(this->width - this->line_offset - _iconsole_cmdline.pixels - ICON_RIGHT_BORDERWIDTH, 0);
353 Point p1 = GetCharPosInString(_iconsole_cmdline.buf, from, FS_NORMAL);
354 Point p2 = from != to ? GetCharPosInString(_iconsole_cmdline.buf, from) : p1;
356 Rect r = {this->line_offset + delta + p1.x, this->height - this->line_height, this->line_offset + delta + p2.x, this->height};
357 return r;
360 virtual const char *GetTextCharacterAtPosition(const Point &pt) const
362 int delta = min(this->width - this->line_offset - _iconsole_cmdline.pixels - ICON_RIGHT_BORDERWIDTH, 0);
364 if (!IsInsideMM(pt.y, this->height - this->line_height, this->height)) return NULL;
366 return GetCharAtPosition(_iconsole_cmdline.buf, pt.x - delta);
369 virtual void OnMouseWheel(int wheel)
371 this->Scroll(-wheel);
374 virtual void OnFocusLost()
376 _video_driver->EditBoxLostFocus();
380 int IConsoleWindow::scroll = 0;
382 void IConsoleGUIInit()
384 IConsoleResetHistoryPos();
385 _iconsole_mode = ICONSOLE_CLOSED;
387 IConsoleLine::Reset();
388 memset(_iconsole_history, 0, sizeof(_iconsole_history));
390 IConsolePrintF(CC_WARNING, "OpenTTD Game Console Revision 7 - %s", _openttd_revision);
391 IConsolePrint(CC_WHITE, "------------------------------------");
392 IConsolePrint(CC_WHITE, "use \"help\" for more information");
393 IConsolePrint(CC_WHITE, "");
394 IConsoleClearCommand();
397 void IConsoleClearBuffer()
399 IConsoleLine::Reset();
402 void IConsoleGUIFree()
404 IConsoleClearBuffer();
407 /** Change the size of the in-game console window after the screen size changed, or the window state changed. */
408 void IConsoleResize(Window *w)
410 switch (_iconsole_mode) {
411 case ICONSOLE_OPENED:
412 w->height = _screen.height / 3;
413 w->width = _screen.width;
414 break;
415 case ICONSOLE_FULL:
416 w->height = _screen.height - ICON_BOTTOM_BORDERWIDTH;
417 w->width = _screen.width;
418 break;
419 default: return;
422 MarkWholeScreenDirty();
425 /** Toggle in-game console between opened and closed. */
426 void IConsoleSwitch()
428 switch (_iconsole_mode) {
429 case ICONSOLE_CLOSED:
430 new IConsoleWindow();
431 break;
433 case ICONSOLE_OPENED: case ICONSOLE_FULL:
434 DeleteWindowById(WC_CONSOLE, 0);
435 break;
438 MarkWholeScreenDirty();
441 /** Close the in-game console. */
442 void IConsoleClose()
444 if (_iconsole_mode == ICONSOLE_OPENED) IConsoleSwitch();
448 * Add the entered line into the history so you can look it back
449 * scroll, etc. Put it to the beginning as it is the latest text
450 * @param cmd Text to be entered into the 'history'
451 * @return the command to execute
453 static const char *IConsoleHistoryAdd(const char *cmd)
455 /* Strip all spaces at the begin */
456 while (IsWhitespace(*cmd)) cmd++;
458 /* Do not put empty command in history */
459 if (StrEmpty(cmd)) return NULL;
461 /* Do not put in history if command is same as previous */
462 if (_iconsole_history[0] == NULL || strcmp(_iconsole_history[0], cmd) != 0) {
463 free(_iconsole_history[ICON_HISTORY_SIZE - 1]);
464 memmove(&_iconsole_history[1], &_iconsole_history[0], sizeof(_iconsole_history[0]) * (ICON_HISTORY_SIZE - 1));
465 _iconsole_history[0] = strdup(cmd);
468 /* Reset the history position */
469 IConsoleResetHistoryPos();
470 return _iconsole_history[0];
474 * Navigate Up/Down in the history of typed commands
475 * @param direction Go further back in history (+1), go to recently typed commands (-1)
477 static void IConsoleHistoryNavigate(int direction)
479 if (_iconsole_history[0] == NULL) return; // Empty history
480 _iconsole_historypos = Clamp(_iconsole_historypos + direction, -1, ICON_HISTORY_SIZE - 1);
482 if (direction > 0 && _iconsole_history[_iconsole_historypos] == NULL) _iconsole_historypos--;
484 if (_iconsole_historypos == -1) {
485 _iconsole_cmdline.DeleteAll();
486 } else {
487 _iconsole_cmdline.Assign(_iconsole_history[_iconsole_historypos]);
492 * Handle the printing of text entered into the console or redirected there
493 * by any other means. Text can be redirected to other clients in a network game
494 * as well as to a logfile. If the network server is a dedicated server, all activities
495 * are also logged. All lines to print are added to a temporary buffer which can be
496 * used as a history to print them onscreen
497 * @param colour_code the colour of the command. Red in case of errors, etc.
498 * @param str the message entered or output on the console (notice, error, etc.)
500 void IConsoleGUIPrint(TextColour colour_code, char *str)
502 new IConsoleLine(str, colour_code);
503 SetWindowDirty(WC_CONSOLE, 0);
508 * Check whether the given TextColour is valid for console usage.
509 * @param c The text colour to compare to.
510 * @return true iff the TextColour is valid for console usage.
512 bool IsValidConsoleColour(TextColour c)
514 /* A normal text colour is used. */
515 if (!(c & TC_IS_PALETTE_COLOUR)) return TC_BEGIN <= c && c < TC_END;
517 /* A text colour from the palette is used; must be the company
518 * colour gradient, so it must be one of those. */
519 c &= ~TC_IS_PALETTE_COLOUR;
520 for (uint i = COLOUR_BEGIN; i < COLOUR_END; i++) {
521 if (_colour_gradient[i][4] == c) return true;
524 return false;