Update to 14.0-beta1

This commit is contained in:
dP
2024-02-04 02:18:17 +05:30
parent 79037e2c65
commit 33ef333b57
1325 changed files with 138465 additions and 70987 deletions

View File

@@ -25,7 +25,7 @@
#include "safeguards.h"
std::string _keyboard_opt[2];
static WChar _keyboard[2][OSK_KEYBOARD_ENTRIES];
static char32_t _keyboard[2][OSK_KEYBOARD_ENTRIES];
enum KeyStateBits {
KEYS_NONE,
@@ -37,12 +37,12 @@ static byte _keystate = KEYS_NONE;
struct OskWindow : public Window {
StringID caption; ///< the caption for this window.
QueryString *qs; ///< text-input
int text_btn; ///< widget number of parent's text field
WidgetID text_btn; ///< widget number of parent's text field
Textbuf *text; ///< pointer to parent's textbuffer (to update caret position)
char *orig_str_buf; ///< Original string.
std::string orig_str; ///< Original string.
bool shift; ///< Is the shift effectively pressed?
OskWindow(WindowDesc *desc, Window *parent, int button) : Window(desc)
OskWindow(WindowDesc *desc, Window *parent, WidgetID button) : Window(desc)
{
this->parent = parent;
assert(parent != nullptr);
@@ -50,15 +50,15 @@ struct OskWindow : public Window {
NWidgetCore *par_wid = parent->GetWidget<NWidgetCore>(button);
assert(par_wid != nullptr);
assert(parent->querystrings.Contains(button));
this->qs = parent->querystrings.Find(button)->second;
assert(parent->querystrings.count(button) != 0);
this->qs = parent->querystrings.find(button)->second;
this->caption = (par_wid->widget_data != STR_NULL) ? par_wid->widget_data : this->qs->caption;
this->text_btn = button;
this->text = &this->qs->text;
this->querystrings[WID_OSK_TEXT] = this->qs;
/* make a copy in case we need to reset later */
this->orig_str_buf = stredup(this->qs->text.buf);
this->orig_str = this->qs->text.buf;
this->InitNested(0);
this->SetFocusedWidget(WID_OSK_TEXT);
@@ -69,11 +69,6 @@ struct OskWindow : public Window {
this->UpdateOskState();
}
~OskWindow()
{
free(this->orig_str_buf);
}
/**
* Only show valid characters; do not show characters that would
* only insert a space when we have a spacebar to do that or
@@ -93,12 +88,12 @@ struct OskWindow : public Window {
this->SetWidgetLoweredState(WID_OSK_CAPS, HasBit(_keystate, KEYS_CAPS));
}
void SetStringParameters(int widget) const override
void SetStringParameters(WidgetID widget) const override
{
if (widget == WID_OSK_CAPTION) SetDParam(0, this->caption);
}
void DrawWidget(const Rect &r, int widget) const override
void DrawWidget(const Rect &r, WidgetID widget) const override
{
if (widget < WID_OSK_LETTERS) return;
@@ -106,11 +101,11 @@ struct OskWindow : public Window {
DrawCharCentered(_keyboard[this->shift][widget], r, TC_BLACK);
}
void OnClick(Point pt, int widget, int click_count) override
void OnClick([[maybe_unused]] Point pt, WidgetID widget, [[maybe_unused]] int click_count) override
{
/* clicked a letter */
if (widget >= WID_OSK_LETTERS) {
WChar c = _keyboard[this->shift][widget - WID_OSK_LETTERS];
char32_t c = _keyboard[this->shift][widget - WID_OSK_LETTERS];
if (!IsValidChar(c, this->qs->text.afilter)) return;
@@ -162,7 +157,7 @@ struct OskWindow : public Window {
break;
case WID_OSK_OK:
if (this->qs->orig == nullptr || strcmp(this->qs->text.buf, this->qs->orig) != 0) {
if (!this->qs->orig.has_value() || this->qs->text.buf != this->qs->orig) {
/* pass information by simulating a button press on parent window */
if (this->qs->ok_button >= 0) {
this->parent->OnClick(pt, this->qs->ok_button, 1);
@@ -179,7 +174,7 @@ struct OskWindow : public Window {
/* Window gets deleted when the parent window removes itself. */
return;
} else { // or reset to original string
qs->text.Assign(this->orig_str_buf);
qs->text.Assign(this->orig_str);
qs->text.MovePos(WKC_END);
this->OnEditboxChanged(WID_OSK_TEXT);
this->Close();
@@ -188,14 +183,16 @@ struct OskWindow : public Window {
}
}
void OnEditboxChanged(int widget) override
void OnEditboxChanged(WidgetID widget) override
{
this->SetWidgetDirty(WID_OSK_TEXT);
this->parent->OnEditboxChanged(this->text_btn);
this->parent->SetWidgetDirty(this->text_btn);
if (widget == WID_OSK_TEXT) {
this->SetWidgetDirty(WID_OSK_TEXT);
this->parent->OnEditboxChanged(this->text_btn);
this->parent->SetWidgetDirty(this->text_btn);
}
}
void OnInvalidateData(int data = 0, bool gui_scope = true) override
void OnInvalidateData([[maybe_unused]] int data = 0, [[maybe_unused]] bool gui_scope = true) override
{
if (!gui_scope) return;
this->SetWidgetDirty(WID_OSK_TEXT);
@@ -223,109 +220,106 @@ static const int KEY_PADDING = 6; // Vertical padding for remaining key rows
* @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.
* @param biggest_index Collected biggest widget index so far.
* @note Key width is measured in 1/2 keys to allow for 1/2 key shifting between rows.
*/
static void AddKey(NWidgetHorizontal *hor, int pad_y, int num_half, WidgetType widtype, int widnum, uint16 widdata, int *biggest_index)
static void AddKey(std::unique_ptr<NWidgetHorizontal> &hor, int pad_y, int num_half, WidgetType widtype, WidgetID widnum, uint16_t widdata)
{
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;
NWidgetSpacer *spc = new NWidgetSpacer(key_width, 0);
auto spc = std::make_unique<NWidgetSpacer>(key_width, 0);
spc->SetMinimalTextLines(1, pad_y, FS_NORMAL);
hor->Add(spc);
hor->Add(std::move(spc));
} else {
if (!hor->IsEmpty()) {
NWidgetSpacer *spc = new NWidgetSpacer(INTER_KEY_SPACE, 0);
auto spc = std::make_unique<NWidgetSpacer>(INTER_KEY_SPACE, 0);
spc->SetMinimalTextLines(1, pad_y, FS_NORMAL);
hor->Add(spc);
hor->Add(std::move(spc));
}
NWidgetLeaf *leaf = new NWidgetLeaf(widtype, COLOUR_GREY, widnum, widdata, STR_NULL);
auto leaf = std::make_unique<NWidgetLeaf>(widtype, COLOUR_GREY, widnum, widdata, STR_NULL);
leaf->SetMinimalSize(key_width, 0);
leaf->SetMinimalTextLines(1, pad_y, FS_NORMAL);
hor->Add(leaf);
hor->Add(std::move(leaf));
}
*biggest_index = std::max(*biggest_index, widnum);
}
/** Construct the top row keys (cancel, ok, backspace). */
static NWidgetBase *MakeTopKeys(int *biggest_index)
static std::unique_ptr<NWidgetBase> MakeTopKeys()
{
NWidgetHorizontal *hor = new NWidgetHorizontal();
auto hor = std::make_unique<NWidgetHorizontal>();
AddKey(hor, TOP_KEY_PADDING, 6 * 2, WWT_TEXTBTN, WID_OSK_CANCEL, STR_BUTTON_CANCEL, biggest_index);
AddKey(hor, TOP_KEY_PADDING, 6 * 2, WWT_TEXTBTN, WID_OSK_OK, STR_BUTTON_OK, biggest_index);
AddKey(hor, TOP_KEY_PADDING, 2 * 2, WWT_PUSHIMGBTN, WID_OSK_BACKSPACE, SPR_OSK_BACKSPACE, biggest_index);
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);
return hor;
}
/** Construct the row containing the digit keys. */
static NWidgetBase *MakeNumberKeys(int *biggest_index)
static std::unique_ptr<NWidgetBase> MakeNumberKeys()
{
NWidgetHorizontal *hor = new NWidgetHorizontalLTR();
std::unique_ptr<NWidgetHorizontal> hor = std::make_unique<NWidgetHorizontalLTR>();
for (int widnum = WID_OSK_NUMBERS_FIRST; widnum <= WID_OSK_NUMBERS_LAST; widnum++) {
AddKey(hor, KEY_PADDING, 2, WWT_PUSHBTN, widnum, 0x0, biggest_index);
for (WidgetID widnum = WID_OSK_NUMBERS_FIRST; widnum <= WID_OSK_NUMBERS_LAST; widnum++) {
AddKey(hor, KEY_PADDING, 2, WWT_PUSHBTN, widnum, 0x0);
}
return hor;
}
/** Construct the qwerty row keys. */
static NWidgetBase *MakeQwertyKeys(int *biggest_index)
static std::unique_ptr<NWidgetBase> MakeQwertyKeys()
{
NWidgetHorizontal *hor = new NWidgetHorizontalLTR();
std::unique_ptr<NWidgetHorizontal> hor = std::make_unique<NWidgetHorizontalLTR>();
AddKey(hor, KEY_PADDING, 3, WWT_PUSHIMGBTN, WID_OSK_SPECIAL, SPR_OSK_SPECIAL, biggest_index);
for (int widnum = WID_OSK_QWERTY_FIRST; widnum <= WID_OSK_QWERTY_LAST; widnum++) {
AddKey(hor, KEY_PADDING, 2, WWT_PUSHBTN, widnum, 0x0, biggest_index);
AddKey(hor, KEY_PADDING, 3, WWT_PUSHIMGBTN, WID_OSK_SPECIAL, SPR_OSK_SPECIAL);
for (WidgetID widnum = WID_OSK_QWERTY_FIRST; widnum <= WID_OSK_QWERTY_LAST; widnum++) {
AddKey(hor, KEY_PADDING, 2, WWT_PUSHBTN, widnum, 0x0);
}
AddKey(hor, KEY_PADDING, 1, NWID_SPACER, 0, 0, biggest_index);
AddKey(hor, KEY_PADDING, 1, NWID_SPACER, 0, 0);
return hor;
}
/** Construct the asdfg row keys. */
static NWidgetBase *MakeAsdfgKeys(int *biggest_index)
static std::unique_ptr<NWidgetBase> MakeAsdfgKeys()
{
NWidgetHorizontal *hor = new NWidgetHorizontalLTR();
std::unique_ptr<NWidgetHorizontal> hor = std::make_unique<NWidgetHorizontalLTR>();
AddKey(hor, KEY_PADDING, 4, WWT_IMGBTN, WID_OSK_CAPS, SPR_OSK_CAPS, biggest_index);
for (int widnum = WID_OSK_ASDFG_FIRST; widnum <= WID_OSK_ASDFG_LAST; widnum++) {
AddKey(hor, KEY_PADDING, 2, WWT_PUSHBTN, widnum, 0x0, biggest_index);
AddKey(hor, KEY_PADDING, 4, WWT_IMGBTN, WID_OSK_CAPS, SPR_OSK_CAPS);
for (WidgetID widnum = WID_OSK_ASDFG_FIRST; widnum <= WID_OSK_ASDFG_LAST; widnum++) {
AddKey(hor, KEY_PADDING, 2, WWT_PUSHBTN, widnum, 0x0);
}
return hor;
}
/** Construct the zxcvb row keys. */
static NWidgetBase *MakeZxcvbKeys(int *biggest_index)
static std::unique_ptr<NWidgetBase> MakeZxcvbKeys()
{
NWidgetHorizontal *hor = new NWidgetHorizontalLTR();
std::unique_ptr<NWidgetHorizontal> hor = std::make_unique<NWidgetHorizontalLTR>();
AddKey(hor, KEY_PADDING, 3, WWT_IMGBTN, WID_OSK_SHIFT, SPR_OSK_SHIFT, biggest_index);
for (int widnum = WID_OSK_ZXCVB_FIRST; widnum <= WID_OSK_ZXCVB_LAST; widnum++) {
AddKey(hor, KEY_PADDING, 2, WWT_PUSHBTN, widnum, 0x0, biggest_index);
AddKey(hor, KEY_PADDING, 3, WWT_IMGBTN, WID_OSK_SHIFT, SPR_OSK_SHIFT);
for (WidgetID widnum = WID_OSK_ZXCVB_FIRST; widnum <= WID_OSK_ZXCVB_LAST; widnum++) {
AddKey(hor, KEY_PADDING, 2, WWT_PUSHBTN, widnum, 0x0);
}
AddKey(hor, KEY_PADDING, 1, NWID_SPACER, 0, 0, biggest_index);
AddKey(hor, KEY_PADDING, 1, NWID_SPACER, 0, 0);
return hor;
}
/** Construct the spacebar row keys. */
static NWidgetBase *MakeSpacebarKeys(int *biggest_index)
static std::unique_ptr<NWidgetBase> MakeSpacebarKeys()
{
NWidgetHorizontal *hor = new NWidgetHorizontal();
auto hor = std::make_unique<NWidgetHorizontal>();
AddKey(hor, KEY_PADDING, 8, NWID_SPACER, 0, 0, biggest_index);
AddKey(hor, KEY_PADDING, 13, WWT_PUSHTXTBTN, WID_OSK_SPACE, STR_EMPTY, biggest_index);
AddKey(hor, KEY_PADDING, 3, NWID_SPACER, 0, 0, biggest_index);
AddKey(hor, KEY_PADDING, 2, WWT_PUSHIMGBTN, WID_OSK_LEFT, SPR_OSK_LEFT, biggest_index);
AddKey(hor, KEY_PADDING, 2, WWT_PUSHIMGBTN, WID_OSK_RIGHT, SPR_OSK_RIGHT, biggest_index);
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);
return hor;
}
static const NWidgetPart _nested_osk_widgets[] = {
NWidget(WWT_CAPTION, COLOUR_GREY, WID_OSK_CAPTION), SetDataTip(STR_WHITE_STRING, STR_NULL),
static constexpr NWidgetPart _nested_osk_widgets[] = {
NWidget(WWT_CAPTION, COLOUR_GREY, WID_OSK_CAPTION), SetDataTip(STR_JUST_STRING, STR_NULL), SetTextStyle(TC_WHITE),
NWidget(WWT_PANEL, COLOUR_GREY),
NWidget(WWT_EDITBOX, COLOUR_GREY, WID_OSK_TEXT), SetMinimalSize(252, 12), SetPadding(2, 2, 2, 2),
EndContainer(),
@@ -339,11 +333,11 @@ static const NWidgetPart _nested_osk_widgets[] = {
EndContainer(),
};
static WindowDesc _osk_desc(
WDP_CENTER, "query_osk", 0, 0,
static WindowDesc _osk_desc(__FILE__, __LINE__,
WDP_CENTER, nullptr, 0, 0,
WC_OSK, WC_NONE,
0,
_nested_osk_widgets, lengthof(_nested_osk_widgets)
std::begin(_nested_osk_widgets), std::end(_nested_osk_widgets)
);
/**
@@ -352,27 +346,18 @@ static WindowDesc _osk_desc(
*/
void GetKeyboardLayout()
{
char keyboard[2][OSK_KEYBOARD_ENTRIES * 4 + 1];
char errormark[2][OSK_KEYBOARD_ENTRIES + 1]; // used for marking invalid chars
std::string keyboard[2];
std::string errormark[2]; // used for marking invalid chars
bool has_error = false; // true when an invalid char is detected
if (_keyboard_opt[0].empty()) {
GetString(keyboard[0], STR_OSK_KEYBOARD_LAYOUT, lastof(keyboard[0]));
} else {
strecpy(keyboard[0], _keyboard_opt[0].c_str(), lastof(keyboard[0]));
}
if (_keyboard_opt[1].empty()) {
GetString(keyboard[1], STR_OSK_KEYBOARD_LAYOUT_CAPS, lastof(keyboard[1]));
} else {
strecpy(keyboard[1], _keyboard_opt[1].c_str(), lastof(keyboard[1]));
}
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];
for (uint j = 0; j < 2; j++) {
const char *kbd = keyboard[j];
auto kbd = keyboard[j].begin();
bool ended = false;
for (uint i = 0; i < OSK_KEYBOARD_ENTRIES; i++) {
_keyboard[j][i] = Utf8Consume(&kbd);
_keyboard[j][i] = Utf8Consume(kbd);
/* Be lenient when the last characters are missing (is quite normal) */
if (_keyboard[j][i] == '\0' || ended) {
@@ -382,21 +367,21 @@ void GetKeyboardLayout()
}
if (IsPrintable(_keyboard[j][i])) {
errormark[j][i] = ' ';
errormark[j] += ' ';
} else {
has_error = true;
errormark[j][i] = '^';
errormark[j] += '^';
_keyboard[j][i] = ' ';
}
}
}
if (has_error) {
ShowInfoF("The keyboard layout you selected contains invalid chars. Please check those chars marked with ^.");
ShowInfoF("Normal keyboard: %s", keyboard[0]);
ShowInfoF(" %s", errormark[0]);
ShowInfoF("Caps Lock: %s", keyboard[1]);
ShowInfoF(" %s", errormark[1]);
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]);
}
}
@@ -405,7 +390,7 @@ void GetKeyboardLayout()
* @param parent pointer to the Window where this keyboard originated from
* @param button widget number of parent's textbox
*/
void ShowOnScreenKeyboard(Window *parent, int button)
void ShowOnScreenKeyboard(Window *parent, WidgetID button)
{
CloseWindowById(WC_OSK, 0);
@@ -420,13 +405,12 @@ void ShowOnScreenKeyboard(Window *parent, int button)
* @param parent window that just updated its original text
* @param button widget number of parent's textbox to update
*/
void UpdateOSKOriginalText(const Window *parent, int button)
void UpdateOSKOriginalText(const Window *parent, WidgetID button)
{
OskWindow *osk = dynamic_cast<OskWindow *>(FindWindowById(WC_OSK, 0));
if (osk == nullptr || osk->parent != parent || osk->text_btn != button) return;
free(osk->orig_str_buf);
osk->orig_str_buf = stredup(osk->qs->text.buf);
osk->orig_str = osk->qs->text.buf;
osk->SetDirty();
}
@@ -437,7 +421,7 @@ void UpdateOSKOriginalText(const Window *parent, int button)
* @param button Editbox of \a w to check for
* @return true if the OSK is opened for \a button.
*/
bool IsOSKOpenedFor(const Window *w, int button)
bool IsOSKOpenedFor(const Window *w, WidgetID button)
{
OskWindow *osk = dynamic_cast<OskWindow *>(FindWindowById(WC_OSK, 0));
return osk != nullptr && osk->parent == w && osk->text_btn == button;