2009-08-21 20:21:05 +00:00
|
|
|
/*
|
|
|
|
* This file is part of OpenTTD.
|
|
|
|
* 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.
|
|
|
|
* 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.
|
|
|
|
* 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/>.
|
|
|
|
*/
|
|
|
|
|
2008-03-26 10:08:17 +00:00
|
|
|
/** @file osk_gui.cpp The On Screen Keyboard GUI */
|
|
|
|
|
|
|
|
#include "stdafx.h"
|
|
|
|
#include "string_func.h"
|
|
|
|
#include "strings_func.h"
|
|
|
|
#include "debug.h"
|
|
|
|
#include "window_func.h"
|
|
|
|
#include "gfx_func.h"
|
2008-05-11 12:26:20 +00:00
|
|
|
#include "querystring_gui.h"
|
2013-08-05 20:37:02 +00:00
|
|
|
#include "video/video_driver.hpp"
|
2021-03-15 22:37:35 +00:00
|
|
|
#include "zoom_func.h"
|
2008-03-26 10:08:17 +00:00
|
|
|
|
2011-12-15 22:22:55 +00:00
|
|
|
#include "widgets/osk_widget.h"
|
|
|
|
|
2008-03-26 10:08:17 +00:00
|
|
|
#include "table/sprites.h"
|
|
|
|
#include "table/strings.h"
|
|
|
|
|
2014-04-23 20:13:33 +00:00
|
|
|
#include "safeguards.h"
|
|
|
|
|
2021-04-28 15:32:33 +00:00
|
|
|
std::string _keyboard_opt[2];
|
2023-05-08 17:01:06 +00:00
|
|
|
static char32_t _keyboard[2][OSK_KEYBOARD_ENTRIES];
|
2008-03-26 10:08:17 +00:00
|
|
|
|
2010-05-13 10:14:29 +00:00
|
|
|
enum KeyStateBits {
|
2008-03-26 10:08:17 +00:00
|
|
|
KEYS_NONE,
|
|
|
|
KEYS_SHIFT,
|
|
|
|
KEYS_CAPS
|
|
|
|
};
|
|
|
|
static byte _keystate = KEYS_NONE;
|
|
|
|
|
2008-05-11 12:26:20 +00:00
|
|
|
struct OskWindow : public Window {
|
2008-05-15 18:26:45 +00:00
|
|
|
StringID caption; ///< the caption for this window.
|
2008-05-11 12:26:20 +00:00
|
|
|
QueryString *qs; ///< text-input
|
2023-12-29 19:11:59 +00:00
|
|
|
WidgetID text_btn; ///< widget number of parent's text field
|
2008-05-11 12:26:20 +00:00
|
|
|
Textbuf *text; ///< pointer to parent's textbuffer (to update caret position)
|
2023-05-10 19:05:19 +00:00
|
|
|
std::string orig_str; ///< Original string.
|
2009-10-25 11:31:12 +00:00
|
|
|
bool shift; ///< Is the shift effectively pressed?
|
2008-05-11 12:26:20 +00:00
|
|
|
|
2024-01-14 16:47:10 +00:00
|
|
|
OskWindow(WindowDesc *desc, Window *parent, WidgetID button) : Window(desc)
|
2008-05-11 12:26:20 +00:00
|
|
|
{
|
|
|
|
this->parent = parent;
|
2019-04-10 21:07:06 +00:00
|
|
|
assert(parent != nullptr);
|
2008-05-11 12:26:20 +00:00
|
|
|
|
2009-11-15 13:36:30 +00:00
|
|
|
NWidgetCore *par_wid = parent->GetWidget<NWidgetCore>(button);
|
2019-04-10 21:07:06 +00:00
|
|
|
assert(par_wid != nullptr);
|
2008-05-11 12:26:20 +00:00
|
|
|
|
2023-05-16 19:50:41 +00:00
|
|
|
assert(parent->querystrings.count(button) != 0);
|
|
|
|
this->qs = parent->querystrings.find(button)->second;
|
2012-11-14 22:50:35 +00:00
|
|
|
this->caption = (par_wid->widget_data != STR_NULL) ? par_wid->widget_data : this->qs->caption;
|
2008-05-11 12:26:20 +00:00
|
|
|
this->text_btn = button;
|
2012-11-14 22:50:35 +00:00
|
|
|
this->text = &this->qs->text;
|
2012-11-28 21:14:28 +00:00
|
|
|
this->querystrings[WID_OSK_TEXT] = this->qs;
|
2008-05-11 12:26:20 +00:00
|
|
|
|
|
|
|
/* make a copy in case we need to reset later */
|
2023-05-10 19:05:19 +00:00
|
|
|
this->orig_str = this->qs->text.buf;
|
2008-05-11 12:26:20 +00:00
|
|
|
|
2013-05-26 19:23:42 +00:00
|
|
|
this->InitNested(0);
|
2012-11-28 21:14:28 +00:00
|
|
|
this->SetFocusedWidget(WID_OSK_TEXT);
|
2009-10-25 11:31:12 +00:00
|
|
|
|
2008-05-11 12:26:20 +00:00
|
|
|
/* Not needed by default. */
|
2011-12-16 18:27:39 +00:00
|
|
|
this->DisableWidget(WID_OSK_SPECIAL);
|
2008-05-11 12:26:20 +00:00
|
|
|
|
2009-10-25 11:31:12 +00:00
|
|
|
this->UpdateOskState();
|
2008-03-26 10:08:17 +00:00
|
|
|
}
|
|
|
|
|
2008-05-11 12:26:20 +00:00
|
|
|
/**
|
|
|
|
* Only show valid characters; do not show characters that would
|
|
|
|
* only insert a space when we have a spacebar to do that or
|
|
|
|
* characters that are not allowed to be entered.
|
|
|
|
*/
|
2009-10-25 11:31:12 +00:00
|
|
|
void UpdateOskState()
|
2008-05-11 12:26:20 +00:00
|
|
|
{
|
2009-10-25 11:31:12 +00:00
|
|
|
this->shift = HasBit(_keystate, KEYS_CAPS) ^ HasBit(_keystate, KEYS_SHIFT);
|
|
|
|
|
2008-05-11 12:26:20 +00:00
|
|
|
for (uint i = 0; i < OSK_KEYBOARD_ENTRIES; i++) {
|
2011-12-16 18:27:39 +00:00
|
|
|
this->SetWidgetDisabledState(WID_OSK_LETTERS + i,
|
2013-03-17 13:04:10 +00:00
|
|
|
!IsValidChar(_keyboard[this->shift][i], this->qs->text.afilter) || _keyboard[this->shift][i] == ' ');
|
2008-05-11 12:26:20 +00:00
|
|
|
}
|
2013-03-17 13:04:10 +00:00
|
|
|
this->SetWidgetDisabledState(WID_OSK_SPACE, !IsValidChar(' ', this->qs->text.afilter));
|
2008-05-11 12:26:20 +00:00
|
|
|
|
2011-12-16 18:27:39 +00:00
|
|
|
this->SetWidgetLoweredState(WID_OSK_SHIFT, HasBit(_keystate, KEYS_SHIFT));
|
|
|
|
this->SetWidgetLoweredState(WID_OSK_CAPS, HasBit(_keystate, KEYS_CAPS));
|
2009-10-25 11:31:12 +00:00
|
|
|
}
|
2008-03-26 10:08:17 +00:00
|
|
|
|
2023-12-29 19:11:59 +00:00
|
|
|
void SetStringParameters(WidgetID widget) const override
|
2009-10-25 11:31:12 +00:00
|
|
|
{
|
2011-12-16 18:27:39 +00:00
|
|
|
if (widget == WID_OSK_CAPTION) SetDParam(0, this->caption);
|
2009-10-25 11:31:12 +00:00
|
|
|
}
|
2008-03-26 10:08:17 +00:00
|
|
|
|
2023-12-29 19:11:59 +00:00
|
|
|
void DrawWidget(const Rect &r, WidgetID widget) const override
|
2009-10-25 11:31:12 +00:00
|
|
|
{
|
2011-12-16 18:27:39 +00:00
|
|
|
if (widget < WID_OSK_LETTERS) return;
|
2008-05-11 12:26:20 +00:00
|
|
|
|
2011-12-16 18:27:39 +00:00
|
|
|
widget -= WID_OSK_LETTERS;
|
2021-04-22 00:17:30 +00:00
|
|
|
DrawCharCentered(_keyboard[this->shift][widget], r, TC_BLACK);
|
2009-10-25 11:31:12 +00:00
|
|
|
}
|
|
|
|
|
2023-12-29 19:11:59 +00:00
|
|
|
void OnClick([[maybe_unused]] Point pt, WidgetID widget, [[maybe_unused]] int click_count) override
|
2008-05-11 12:26:20 +00:00
|
|
|
{
|
|
|
|
/* clicked a letter */
|
2011-12-16 18:27:39 +00:00
|
|
|
if (widget >= WID_OSK_LETTERS) {
|
2023-05-08 17:01:06 +00:00
|
|
|
char32_t c = _keyboard[this->shift][widget - WID_OSK_LETTERS];
|
2008-03-26 10:08:17 +00:00
|
|
|
|
2013-03-17 13:04:10 +00:00
|
|
|
if (!IsValidChar(c, this->qs->text.afilter)) return;
|
2008-03-26 10:08:17 +00:00
|
|
|
|
2012-11-28 20:54:56 +00:00
|
|
|
if (this->qs->text.InsertChar(c)) this->OnEditboxChanged(WID_OSK_TEXT);
|
2008-03-26 10:08:17 +00:00
|
|
|
|
2008-05-11 12:26:20 +00:00
|
|
|
if (HasBit(_keystate, KEYS_SHIFT)) {
|
|
|
|
ToggleBit(_keystate, KEYS_SHIFT);
|
2012-11-28 21:03:34 +00:00
|
|
|
this->UpdateOskState();
|
2008-05-11 12:26:20 +00:00
|
|
|
this->SetDirty();
|
2008-03-26 10:08:17 +00:00
|
|
|
}
|
2008-05-11 12:26:20 +00:00
|
|
|
return;
|
2008-03-26 10:08:17 +00:00
|
|
|
}
|
|
|
|
|
2008-05-11 12:26:20 +00:00
|
|
|
switch (widget) {
|
2011-12-16 18:27:39 +00:00
|
|
|
case WID_OSK_BACKSPACE:
|
2012-11-28 20:54:56 +00:00
|
|
|
if (this->qs->text.DeleteChar(WKC_BACKSPACE)) this->OnEditboxChanged(WID_OSK_TEXT);
|
2008-05-11 12:26:20 +00:00
|
|
|
break;
|
2008-03-26 10:08:17 +00:00
|
|
|
|
2011-12-16 18:27:39 +00:00
|
|
|
case WID_OSK_SPECIAL:
|
2008-05-11 12:26:20 +00:00
|
|
|
/*
|
|
|
|
* Anything device specific can go here.
|
|
|
|
* The button itself is hidden by default, and when you need it you
|
|
|
|
* can not hide it in the create event.
|
|
|
|
*/
|
|
|
|
break;
|
2008-03-26 10:08:17 +00:00
|
|
|
|
2011-12-16 18:27:39 +00:00
|
|
|
case WID_OSK_CAPS:
|
2008-05-11 12:26:20 +00:00
|
|
|
ToggleBit(_keystate, KEYS_CAPS);
|
2009-10-25 11:31:12 +00:00
|
|
|
this->UpdateOskState();
|
2008-05-11 12:26:20 +00:00
|
|
|
this->SetDirty();
|
|
|
|
break;
|
2008-03-26 10:08:17 +00:00
|
|
|
|
2011-12-16 18:27:39 +00:00
|
|
|
case WID_OSK_SHIFT:
|
2008-05-11 12:26:20 +00:00
|
|
|
ToggleBit(_keystate, KEYS_SHIFT);
|
2009-10-25 11:31:12 +00:00
|
|
|
this->UpdateOskState();
|
2008-05-11 12:26:20 +00:00
|
|
|
this->SetDirty();
|
|
|
|
break;
|
2008-03-26 10:08:17 +00:00
|
|
|
|
2011-12-16 18:27:39 +00:00
|
|
|
case WID_OSK_SPACE:
|
2012-11-28 20:54:56 +00:00
|
|
|
if (this->qs->text.InsertChar(' ')) this->OnEditboxChanged(WID_OSK_TEXT);
|
2008-03-26 10:08:17 +00:00
|
|
|
break;
|
|
|
|
|
2011-12-16 18:27:39 +00:00
|
|
|
case WID_OSK_LEFT:
|
2012-11-28 20:54:56 +00:00
|
|
|
if (this->qs->text.MovePos(WKC_LEFT)) this->InvalidateData();
|
2008-05-11 12:26:20 +00:00
|
|
|
break;
|
|
|
|
|
2011-12-16 18:27:39 +00:00
|
|
|
case WID_OSK_RIGHT:
|
2012-11-28 20:54:56 +00:00
|
|
|
if (this->qs->text.MovePos(WKC_RIGHT)) this->InvalidateData();
|
2008-05-11 12:26:20 +00:00
|
|
|
break;
|
|
|
|
|
2011-12-16 18:27:39 +00:00
|
|
|
case WID_OSK_OK:
|
2023-05-10 19:05:19 +00:00
|
|
|
if (!this->qs->orig.has_value() || this->qs->text.buf != this->qs->orig) {
|
2008-05-11 12:26:20 +00:00
|
|
|
/* pass information by simulating a button press on parent window */
|
2012-11-13 21:46:54 +00:00
|
|
|
if (this->qs->ok_button >= 0) {
|
|
|
|
this->parent->OnClick(pt, this->qs->ok_button, 1);
|
2008-05-14 02:32:35 +00:00
|
|
|
/* Window gets deleted when the parent window removes itself. */
|
|
|
|
return;
|
2008-03-26 10:08:17 +00:00
|
|
|
}
|
2008-05-11 12:26:20 +00:00
|
|
|
}
|
2021-05-15 21:12:25 +00:00
|
|
|
this->Close();
|
2008-05-11 12:26:20 +00:00
|
|
|
break;
|
|
|
|
|
2011-12-16 18:27:39 +00:00
|
|
|
case WID_OSK_CANCEL:
|
2012-11-13 21:46:54 +00:00
|
|
|
if (this->qs->cancel_button >= 0) { // pass a cancel event to the parent window
|
|
|
|
this->parent->OnClick(pt, this->qs->cancel_button, 1);
|
2008-05-11 12:26:20 +00:00
|
|
|
/* Window gets deleted when the parent window removes itself. */
|
2008-05-14 02:32:35 +00:00
|
|
|
return;
|
2008-05-11 12:26:20 +00:00
|
|
|
} else { // or reset to original string
|
2023-05-10 19:05:19 +00:00
|
|
|
qs->text.Assign(this->orig_str);
|
2012-06-04 15:30:29 +00:00
|
|
|
qs->text.MovePos(WKC_END);
|
2012-11-28 20:54:56 +00:00
|
|
|
this->OnEditboxChanged(WID_OSK_TEXT);
|
2021-05-15 21:12:25 +00:00
|
|
|
this->Close();
|
2008-05-11 12:26:20 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2009-02-06 11:57:25 +00:00
|
|
|
}
|
|
|
|
|
2023-12-29 19:11:59 +00:00
|
|
|
void OnEditboxChanged(WidgetID widget) override
|
2009-02-06 11:57:25 +00:00
|
|
|
{
|
2023-09-16 21:25:17 +00:00
|
|
|
if (widget == WID_OSK_TEXT) {
|
|
|
|
this->SetWidgetDirty(WID_OSK_TEXT);
|
|
|
|
this->parent->OnEditboxChanged(this->text_btn);
|
|
|
|
this->parent->SetWidgetDirty(this->text_btn);
|
|
|
|
}
|
2008-03-26 10:08:17 +00:00
|
|
|
}
|
2008-05-11 12:26:20 +00:00
|
|
|
|
2023-09-16 20:20:53 +00:00
|
|
|
void OnInvalidateData([[maybe_unused]] int data = 0, [[maybe_unused]] bool gui_scope = true) override
|
2008-05-28 15:28:27 +00:00
|
|
|
{
|
2011-03-13 21:31:29 +00:00
|
|
|
if (!gui_scope) return;
|
2011-12-16 18:27:39 +00:00
|
|
|
this->SetWidgetDirty(WID_OSK_TEXT);
|
2012-11-28 20:54:56 +00:00
|
|
|
this->parent->SetWidgetDirty(this->text_btn);
|
2008-05-28 15:28:27 +00:00
|
|
|
}
|
2012-11-28 21:14:28 +00:00
|
|
|
|
2023-06-12 07:42:02 +00:00
|
|
|
void OnFocusLost(bool closing) override
|
2012-11-28 21:14:28 +00:00
|
|
|
{
|
2014-04-28 21:06:51 +00:00
|
|
|
VideoDriver::GetInstance()->EditBoxLostFocus();
|
2023-06-12 07:42:02 +00:00
|
|
|
if (!closing) this->Close();
|
2012-11-28 21:14:28 +00:00
|
|
|
}
|
2008-05-11 12:26:20 +00:00
|
|
|
};
|
2008-03-26 10:08:17 +00:00
|
|
|
|
2009-04-26 16:51:50 +00:00
|
|
|
static const int HALF_KEY_WIDTH = 7; // Width of 1/2 key in pixels.
|
|
|
|
static const int INTER_KEY_SPACE = 2; // Number of pixels between two keys.
|
|
|
|
|
2021-04-22 00:19:54 +00:00
|
|
|
static const int TOP_KEY_PADDING = 2; // Vertical padding for the top row of keys.
|
|
|
|
static const int KEY_PADDING = 6; // Vertical padding for remaining key rows.
|
|
|
|
|
2009-04-26 16:51:50 +00:00
|
|
|
/**
|
|
|
|
* Add a key widget to a row of the keyboard.
|
|
|
|
* @param hor Row container to add key widget to.
|
2021-04-22 00:19:54 +00:00
|
|
|
* @param pad_y Vertical padding of the key (all keys in a row should have equal padding).
|
2009-09-19 09:51:14 +00:00
|
|
|
* @param num_half Number of 1/2 key widths that this key has.
|
2009-04-26 16:51:50 +00:00
|
|
|
* @param widtype Widget type of the key. Must be either \c NWID_SPACER for an invisible key, or a \c WWT_* widget.
|
|
|
|
* @param widnum Widget number of the key.
|
|
|
|
* @param widdata Data value of the key widget.
|
|
|
|
* @note Key width is measured in 1/2 keys to allow for 1/2 key shifting between rows.
|
|
|
|
*/
|
2023-12-30 07:36:22 +00:00
|
|
|
static void AddKey(std::unique_ptr<NWidgetHorizontal> &hor, int pad_y, int num_half, WidgetType widtype, WidgetID widnum, uint16_t widdata)
|
2009-04-26 16:51:50 +00:00
|
|
|
{
|
|
|
|
int key_width = HALF_KEY_WIDTH + (INTER_KEY_SPACE + HALF_KEY_WIDTH) * (num_half - 1);
|
|
|
|
|
|
|
|
if (widtype == NWID_SPACER) {
|
|
|
|
if (!hor->IsEmpty()) key_width += INTER_KEY_SPACE;
|
2023-12-30 07:36:22 +00:00
|
|
|
auto spc = std::make_unique<NWidgetSpacer>(key_width, 0);
|
2021-04-22 00:19:54 +00:00
|
|
|
spc->SetMinimalTextLines(1, pad_y, FS_NORMAL);
|
2023-12-30 07:36:22 +00:00
|
|
|
hor->Add(std::move(spc));
|
2009-04-26 16:51:50 +00:00
|
|
|
} else {
|
|
|
|
if (!hor->IsEmpty()) {
|
2023-12-30 07:36:22 +00:00
|
|
|
auto spc = std::make_unique<NWidgetSpacer>(INTER_KEY_SPACE, 0);
|
2021-04-22 00:19:54 +00:00
|
|
|
spc->SetMinimalTextLines(1, pad_y, FS_NORMAL);
|
2023-12-30 07:36:22 +00:00
|
|
|
hor->Add(std::move(spc));
|
2009-04-26 16:51:50 +00:00
|
|
|
}
|
2023-12-30 07:36:22 +00:00
|
|
|
auto leaf = std::make_unique<NWidgetLeaf>(widtype, COLOUR_GREY, widnum, widdata, STR_NULL);
|
2021-04-22 00:19:54 +00:00
|
|
|
leaf->SetMinimalSize(key_width, 0);
|
|
|
|
leaf->SetMinimalTextLines(1, pad_y, FS_NORMAL);
|
2023-12-30 07:36:22 +00:00
|
|
|
hor->Add(std::move(leaf));
|
2009-04-26 16:51:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Construct the top row keys (cancel, ok, backspace). */
|
2023-12-30 07:36:22 +00:00
|
|
|
static std::unique_ptr<NWidgetBase> MakeTopKeys()
|
2009-04-26 16:51:50 +00:00
|
|
|
{
|
2023-12-30 07:36:22 +00:00
|
|
|
auto hor = std::make_unique<NWidgetHorizontal>();
|
2009-04-26 16:51:50 +00:00
|
|
|
|
2023-10-16 10:13:36 +00:00
|
|
|
AddKey(hor, TOP_KEY_PADDING, 6 * 2, WWT_TEXTBTN, WID_OSK_CANCEL, STR_BUTTON_CANCEL);
|
|
|
|
AddKey(hor, TOP_KEY_PADDING, 6 * 2, WWT_TEXTBTN, WID_OSK_OK, STR_BUTTON_OK );
|
|
|
|
AddKey(hor, TOP_KEY_PADDING, 2 * 2, WWT_PUSHIMGBTN, WID_OSK_BACKSPACE, SPR_OSK_BACKSPACE);
|
2009-04-26 16:51:50 +00:00
|
|
|
return hor;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Construct the row containing the digit keys. */
|
2023-12-30 07:36:22 +00:00
|
|
|
static std::unique_ptr<NWidgetBase> MakeNumberKeys()
|
2009-04-26 16:51:50 +00:00
|
|
|
{
|
2023-12-30 07:36:22 +00:00
|
|
|
std::unique_ptr<NWidgetHorizontal> hor = std::make_unique<NWidgetHorizontalLTR>();
|
2009-04-26 16:51:50 +00:00
|
|
|
|
2023-12-29 19:11:59 +00:00
|
|
|
for (WidgetID widnum = WID_OSK_NUMBERS_FIRST; widnum <= WID_OSK_NUMBERS_LAST; widnum++) {
|
2023-10-16 10:13:36 +00:00
|
|
|
AddKey(hor, KEY_PADDING, 2, WWT_PUSHBTN, widnum, 0x0);
|
2009-04-26 16:51:50 +00:00
|
|
|
}
|
|
|
|
return hor;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Construct the qwerty row keys. */
|
2023-12-30 07:36:22 +00:00
|
|
|
static std::unique_ptr<NWidgetBase> MakeQwertyKeys()
|
2009-04-26 16:51:50 +00:00
|
|
|
{
|
2023-12-30 07:36:22 +00:00
|
|
|
std::unique_ptr<NWidgetHorizontal> hor = std::make_unique<NWidgetHorizontalLTR>();
|
2009-04-26 16:51:50 +00:00
|
|
|
|
2023-10-16 10:13:36 +00:00
|
|
|
AddKey(hor, KEY_PADDING, 3, WWT_PUSHIMGBTN, WID_OSK_SPECIAL, SPR_OSK_SPECIAL);
|
2023-12-29 19:11:59 +00:00
|
|
|
for (WidgetID widnum = WID_OSK_QWERTY_FIRST; widnum <= WID_OSK_QWERTY_LAST; widnum++) {
|
2023-10-16 10:13:36 +00:00
|
|
|
AddKey(hor, KEY_PADDING, 2, WWT_PUSHBTN, widnum, 0x0);
|
2009-04-26 16:51:50 +00:00
|
|
|
}
|
2023-10-16 10:13:36 +00:00
|
|
|
AddKey(hor, KEY_PADDING, 1, NWID_SPACER, 0, 0);
|
2009-04-26 16:51:50 +00:00
|
|
|
return hor;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Construct the asdfg row keys. */
|
2023-12-30 07:36:22 +00:00
|
|
|
static std::unique_ptr<NWidgetBase> MakeAsdfgKeys()
|
2009-04-26 16:51:50 +00:00
|
|
|
{
|
2023-12-30 07:36:22 +00:00
|
|
|
std::unique_ptr<NWidgetHorizontal> hor = std::make_unique<NWidgetHorizontalLTR>();
|
2009-04-26 16:51:50 +00:00
|
|
|
|
2023-10-16 10:13:36 +00:00
|
|
|
AddKey(hor, KEY_PADDING, 4, WWT_IMGBTN, WID_OSK_CAPS, SPR_OSK_CAPS);
|
2023-12-29 19:11:59 +00:00
|
|
|
for (WidgetID widnum = WID_OSK_ASDFG_FIRST; widnum <= WID_OSK_ASDFG_LAST; widnum++) {
|
2023-10-16 10:13:36 +00:00
|
|
|
AddKey(hor, KEY_PADDING, 2, WWT_PUSHBTN, widnum, 0x0);
|
2009-04-26 16:51:50 +00:00
|
|
|
}
|
|
|
|
return hor;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Construct the zxcvb row keys. */
|
2023-12-30 07:36:22 +00:00
|
|
|
static std::unique_ptr<NWidgetBase> MakeZxcvbKeys()
|
2009-04-26 16:51:50 +00:00
|
|
|
{
|
2023-12-30 07:36:22 +00:00
|
|
|
std::unique_ptr<NWidgetHorizontal> hor = std::make_unique<NWidgetHorizontalLTR>();
|
2009-04-26 16:51:50 +00:00
|
|
|
|
2023-10-16 10:13:36 +00:00
|
|
|
AddKey(hor, KEY_PADDING, 3, WWT_IMGBTN, WID_OSK_SHIFT, SPR_OSK_SHIFT);
|
2023-12-29 19:11:59 +00:00
|
|
|
for (WidgetID widnum = WID_OSK_ZXCVB_FIRST; widnum <= WID_OSK_ZXCVB_LAST; widnum++) {
|
2023-10-16 10:13:36 +00:00
|
|
|
AddKey(hor, KEY_PADDING, 2, WWT_PUSHBTN, widnum, 0x0);
|
2009-04-26 16:51:50 +00:00
|
|
|
}
|
2023-10-16 10:13:36 +00:00
|
|
|
AddKey(hor, KEY_PADDING, 1, NWID_SPACER, 0, 0);
|
2009-04-26 16:51:50 +00:00
|
|
|
return hor;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Construct the spacebar row keys. */
|
2023-12-30 07:36:22 +00:00
|
|
|
static std::unique_ptr<NWidgetBase> MakeSpacebarKeys()
|
2009-04-26 16:51:50 +00:00
|
|
|
{
|
2023-12-30 07:36:22 +00:00
|
|
|
auto hor = std::make_unique<NWidgetHorizontal>();
|
2009-04-26 16:51:50 +00:00
|
|
|
|
2023-10-16 10:13:36 +00:00
|
|
|
AddKey(hor, KEY_PADDING, 8, NWID_SPACER, 0, 0);
|
|
|
|
AddKey(hor, KEY_PADDING, 13, WWT_PUSHTXTBTN, WID_OSK_SPACE, STR_EMPTY);
|
|
|
|
AddKey(hor, KEY_PADDING, 3, NWID_SPACER, 0, 0);
|
|
|
|
AddKey(hor, KEY_PADDING, 2, WWT_PUSHIMGBTN, WID_OSK_LEFT, SPR_OSK_LEFT);
|
|
|
|
AddKey(hor, KEY_PADDING, 2, WWT_PUSHIMGBTN, WID_OSK_RIGHT, SPR_OSK_RIGHT);
|
2009-04-26 16:51:50 +00:00
|
|
|
return hor;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2024-01-15 22:49:24 +00:00
|
|
|
static constexpr NWidgetPart _nested_osk_widgets[] = {
|
2023-04-25 09:03:48 +00:00
|
|
|
NWidget(WWT_CAPTION, COLOUR_GREY, WID_OSK_CAPTION), SetDataTip(STR_JUST_STRING, STR_NULL), SetTextStyle(TC_WHITE),
|
2009-11-24 21:13:36 +00:00
|
|
|
NWidget(WWT_PANEL, COLOUR_GREY),
|
2011-12-16 18:27:39 +00:00
|
|
|
NWidget(WWT_EDITBOX, COLOUR_GREY, WID_OSK_TEXT), SetMinimalSize(252, 12), SetPadding(2, 2, 2, 2),
|
2009-04-26 16:51:50 +00:00
|
|
|
EndContainer(),
|
2009-11-24 21:13:36 +00:00
|
|
|
NWidget(WWT_PANEL, COLOUR_GREY), SetPIP(5, 2, 3),
|
2009-04-26 16:51:50 +00:00
|
|
|
NWidgetFunction(MakeTopKeys), SetPadding(0, 3, 0, 3),
|
|
|
|
NWidgetFunction(MakeNumberKeys), SetPadding(0, 3, 0, 3),
|
|
|
|
NWidgetFunction(MakeQwertyKeys), SetPadding(0, 3, 0, 3),
|
|
|
|
NWidgetFunction(MakeAsdfgKeys), SetPadding(0, 3, 0, 3),
|
|
|
|
NWidgetFunction(MakeZxcvbKeys), SetPadding(0, 3, 0, 3),
|
|
|
|
NWidgetFunction(MakeSpacebarKeys), SetPadding(0, 3, 0, 3),
|
|
|
|
EndContainer(),
|
|
|
|
};
|
|
|
|
|
2023-11-02 19:33:01 +00:00
|
|
|
static WindowDesc _osk_desc(__FILE__, __LINE__,
|
2023-07-13 15:41:06 +00:00
|
|
|
WDP_CENTER, nullptr, 0, 0,
|
2008-03-26 10:08:17 +00:00
|
|
|
WC_OSK, WC_NONE,
|
2012-11-11 16:10:43 +00:00
|
|
|
0,
|
2023-09-03 20:54:13 +00:00
|
|
|
std::begin(_nested_osk_widgets), std::end(_nested_osk_widgets)
|
2009-03-15 15:12:06 +00:00
|
|
|
);
|
2008-03-26 10:08:17 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Retrieve keyboard layout from language string or (if set) config file.
|
|
|
|
* Also check for invalid characters.
|
|
|
|
*/
|
|
|
|
void GetKeyboardLayout()
|
|
|
|
{
|
2023-05-31 15:16:31 +00:00
|
|
|
std::string keyboard[2];
|
|
|
|
std::string errormark[2]; // used for marking invalid chars
|
2008-03-26 10:08:17 +00:00
|
|
|
bool has_error = false; // true when an invalid char is detected
|
|
|
|
|
2023-05-31 15:16:31 +00:00
|
|
|
keyboard[0] = _keyboard_opt[0].empty() ? GetString(STR_OSK_KEYBOARD_LAYOUT) : _keyboard_opt[0];
|
|
|
|
keyboard[1] = _keyboard_opt[1].empty() ? GetString(STR_OSK_KEYBOARD_LAYOUT_CAPS) : _keyboard_opt[1];
|
2008-03-26 10:08:17 +00:00
|
|
|
|
|
|
|
for (uint j = 0; j < 2; j++) {
|
2023-05-31 15:16:31 +00:00
|
|
|
auto kbd = keyboard[j].begin();
|
2008-03-26 11:50:53 +00:00
|
|
|
bool ended = false;
|
2008-03-26 10:08:17 +00:00
|
|
|
for (uint i = 0; i < OSK_KEYBOARD_ENTRIES; i++) {
|
2023-05-31 15:16:31 +00:00
|
|
|
_keyboard[j][i] = Utf8Consume(kbd);
|
2008-03-26 10:08:17 +00:00
|
|
|
|
2008-03-26 11:50:53 +00:00
|
|
|
/* Be lenient when the last characters are missing (is quite normal) */
|
|
|
|
if (_keyboard[j][i] == '\0' || ended) {
|
|
|
|
ended = true;
|
|
|
|
_keyboard[j][i] = ' ';
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2008-03-26 10:08:17 +00:00
|
|
|
if (IsPrintable(_keyboard[j][i])) {
|
2023-05-31 15:16:31 +00:00
|
|
|
errormark[j] += ' ';
|
2008-03-26 10:08:17 +00:00
|
|
|
} else {
|
|
|
|
has_error = true;
|
2023-05-31 15:16:31 +00:00
|
|
|
errormark[j] += '^';
|
2008-03-26 10:08:17 +00:00
|
|
|
_keyboard[j][i] = ' ';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (has_error) {
|
2023-04-18 17:41:29 +00:00
|
|
|
ShowInfo("The keyboard layout you selected contains invalid chars. Please check those chars marked with ^.");
|
|
|
|
ShowInfo("Normal keyboard: {}", keyboard[0]);
|
|
|
|
ShowInfo(" {}", errormark[0]);
|
|
|
|
ShowInfo("Caps Lock: {}", keyboard[1]);
|
|
|
|
ShowInfo(" {}", errormark[1]);
|
2008-03-26 10:08:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2008-05-19 09:24:03 +00:00
|
|
|
* Show the on-screen keyboard (osk) associated with a given textbox
|
2008-03-26 10:08:17 +00:00
|
|
|
* @param parent pointer to the Window where this keyboard originated from
|
|
|
|
* @param button widget number of parent's textbox
|
|
|
|
*/
|
2023-12-29 19:11:59 +00:00
|
|
|
void ShowOnScreenKeyboard(Window *parent, WidgetID button)
|
2008-03-26 10:08:17 +00:00
|
|
|
{
|
2021-05-17 13:46:38 +00:00
|
|
|
CloseWindowById(WC_OSK, 0);
|
2008-03-26 10:08:17 +00:00
|
|
|
|
|
|
|
GetKeyboardLayout();
|
2012-11-13 21:46:54 +00:00
|
|
|
new OskWindow(&_osk_desc, parent, button);
|
2008-03-26 10:08:17 +00:00
|
|
|
}
|
2009-10-10 12:47:04 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Updates the original text of the OSK so when the 'parent' changes the
|
|
|
|
* original and you press on cancel you won't get the 'old' original text
|
|
|
|
* but the updated one.
|
2019-09-29 20:27:32 +00:00
|
|
|
* @param parent window that just updated its original text
|
2009-10-10 12:47:04 +00:00
|
|
|
* @param button widget number of parent's textbox to update
|
|
|
|
*/
|
2023-12-29 19:11:59 +00:00
|
|
|
void UpdateOSKOriginalText(const Window *parent, WidgetID button)
|
2009-10-10 12:47:04 +00:00
|
|
|
{
|
|
|
|
OskWindow *osk = dynamic_cast<OskWindow *>(FindWindowById(WC_OSK, 0));
|
2019-04-10 21:07:06 +00:00
|
|
|
if (osk == nullptr || osk->parent != parent || osk->text_btn != button) return;
|
2009-10-10 12:47:04 +00:00
|
|
|
|
2023-05-10 19:05:19 +00:00
|
|
|
osk->orig_str = osk->qs->text.buf;
|
2009-10-10 12:47:04 +00:00
|
|
|
|
|
|
|
osk->SetDirty();
|
|
|
|
}
|
2012-11-28 21:14:28 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Check whether the OSK is opened for a specific editbox.
|
2018-10-28 02:17:36 +00:00
|
|
|
* @param w Window to check for
|
2012-11-28 21:14:28 +00:00
|
|
|
* @param button Editbox of \a w to check for
|
2018-10-28 02:17:36 +00:00
|
|
|
* @return true if the OSK is opened for \a button.
|
2012-11-28 21:14:28 +00:00
|
|
|
*/
|
2023-12-29 19:11:59 +00:00
|
|
|
bool IsOSKOpenedFor(const Window *w, WidgetID button)
|
2012-11-28 21:14:28 +00:00
|
|
|
{
|
|
|
|
OskWindow *osk = dynamic_cast<OskWindow *>(FindWindowById(WC_OSK, 0));
|
2019-04-10 21:07:06 +00:00
|
|
|
return osk != nullptr && osk->parent == w && osk->text_btn == button;
|
2012-11-28 21:14:28 +00:00
|
|
|
}
|