mirror of https://github.com/OpenTTD/OpenTTD
Codechange: use std::string instead of char* for original editor strings
parent
7e1123c731
commit
6d1586dd49
|
@ -971,7 +971,7 @@ struct QueryStringWindow : public Window
|
||||||
|
|
||||||
this->editbox.text.UpdateSize();
|
this->editbox.text.UpdateSize();
|
||||||
|
|
||||||
if ((flags & QSF_ACCEPT_UNCHANGED) == 0) this->editbox.orig = stredup(this->editbox.text.buf);
|
if ((flags & QSF_ACCEPT_UNCHANGED) == 0) this->editbox.orig = this->editbox.text.buf;
|
||||||
|
|
||||||
this->querystrings[WID_QS_TEXT] = &this->editbox;
|
this->querystrings[WID_QS_TEXT] = &this->editbox;
|
||||||
this->editbox.caption = caption;
|
this->editbox.caption = caption;
|
||||||
|
@ -1033,7 +1033,7 @@ struct QueryStringWindow : public Window
|
||||||
|
|
||||||
void OnOk()
|
void OnOk()
|
||||||
{
|
{
|
||||||
if (this->editbox.orig == nullptr || strcmp(this->editbox.text.buf, this->editbox.orig) != 0) {
|
if (!this->editbox.orig.has_value() || this->editbox.text.buf != this->editbox.orig) {
|
||||||
assert(this->parent != nullptr);
|
assert(this->parent != nullptr);
|
||||||
|
|
||||||
this->parent->OnQueryTextFinished(this->editbox.text.buf);
|
this->parent->OnQueryTextFinished(this->editbox.text.buf);
|
||||||
|
|
|
@ -39,7 +39,7 @@ struct OskWindow : public Window {
|
||||||
QueryString *qs; ///< text-input
|
QueryString *qs; ///< text-input
|
||||||
int text_btn; ///< widget number of parent's text field
|
int text_btn; ///< widget number of parent's text field
|
||||||
Textbuf *text; ///< pointer to parent's textbuffer (to update caret position)
|
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?
|
bool shift; ///< Is the shift effectively pressed?
|
||||||
|
|
||||||
OskWindow(WindowDesc *desc, Window *parent, int button) : Window(desc)
|
OskWindow(WindowDesc *desc, Window *parent, int button) : Window(desc)
|
||||||
|
@ -58,7 +58,7 @@ struct OskWindow : public Window {
|
||||||
this->querystrings[WID_OSK_TEXT] = this->qs;
|
this->querystrings[WID_OSK_TEXT] = this->qs;
|
||||||
|
|
||||||
/* make a copy in case we need to reset later */
|
/* 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->InitNested(0);
|
||||||
this->SetFocusedWidget(WID_OSK_TEXT);
|
this->SetFocusedWidget(WID_OSK_TEXT);
|
||||||
|
@ -69,11 +69,6 @@ struct OskWindow : public Window {
|
||||||
this->UpdateOskState();
|
this->UpdateOskState();
|
||||||
}
|
}
|
||||||
|
|
||||||
~OskWindow()
|
|
||||||
{
|
|
||||||
free(this->orig_str_buf);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Only show valid characters; do not show characters that would
|
* Only show valid characters; do not show characters that would
|
||||||
* only insert a space when we have a spacebar to do that or
|
* only insert a space when we have a spacebar to do that or
|
||||||
|
@ -162,7 +157,7 @@ struct OskWindow : public Window {
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case WID_OSK_OK:
|
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 */
|
/* pass information by simulating a button press on parent window */
|
||||||
if (this->qs->ok_button >= 0) {
|
if (this->qs->ok_button >= 0) {
|
||||||
this->parent->OnClick(pt, this->qs->ok_button, 1);
|
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. */
|
/* Window gets deleted when the parent window removes itself. */
|
||||||
return;
|
return;
|
||||||
} else { // or reset to original string
|
} else { // or reset to original string
|
||||||
qs->text.Assign(this->orig_str_buf);
|
qs->text.Assign(this->orig_str);
|
||||||
qs->text.MovePos(WKC_END);
|
qs->text.MovePos(WKC_END);
|
||||||
this->OnEditboxChanged(WID_OSK_TEXT);
|
this->OnEditboxChanged(WID_OSK_TEXT);
|
||||||
this->Close();
|
this->Close();
|
||||||
|
@ -425,8 +420,7 @@ void UpdateOSKOriginalText(const Window *parent, int button)
|
||||||
OskWindow *osk = dynamic_cast<OskWindow *>(FindWindowById(WC_OSK, 0));
|
OskWindow *osk = dynamic_cast<OskWindow *>(FindWindowById(WC_OSK, 0));
|
||||||
if (osk == nullptr || osk->parent != parent || osk->text_btn != button) return;
|
if (osk == nullptr || osk->parent != parent || osk->text_btn != button) return;
|
||||||
|
|
||||||
free(osk->orig_str_buf);
|
osk->orig_str = osk->qs->text.buf;
|
||||||
osk->orig_str_buf = stredup(osk->qs->text.buf);
|
|
||||||
|
|
||||||
osk->SetDirty();
|
osk->SetDirty();
|
||||||
}
|
}
|
||||||
|
|
|
@ -27,7 +27,7 @@ struct QueryString {
|
||||||
int ok_button; ///< Widget button of parent window to simulate when pressing OK in OSK.
|
int ok_button; ///< Widget button of parent window to simulate when pressing OK in OSK.
|
||||||
int cancel_button; ///< Widget button of parent window to simulate when pressing CANCEL in OSK.
|
int cancel_button; ///< Widget button of parent window to simulate when pressing CANCEL in OSK.
|
||||||
Textbuf text;
|
Textbuf text;
|
||||||
const char *orig;
|
std::optional<std::string> orig;
|
||||||
bool handled;
|
bool handled;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -35,18 +35,10 @@ struct QueryString {
|
||||||
* @param size Maximum size in bytes.
|
* @param size Maximum size in bytes.
|
||||||
* @param chars Maximum size in chars.
|
* @param chars Maximum size in chars.
|
||||||
*/
|
*/
|
||||||
QueryString(uint16 size, uint16 chars = UINT16_MAX) : ok_button(ACTION_NOTHING), cancel_button(ACTION_DESELECT), text(size, chars), orig(nullptr)
|
QueryString(uint16 size, uint16 chars = UINT16_MAX) : ok_button(ACTION_NOTHING), cancel_button(ACTION_DESELECT), text(size, chars)
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Make sure everything gets freed.
|
|
||||||
*/
|
|
||||||
~QueryString()
|
|
||||||
{
|
|
||||||
free(this->orig);
|
|
||||||
}
|
|
||||||
|
|
||||||
public:
|
public:
|
||||||
void DrawEditBox(const Window *w, int wid) const;
|
void DrawEditBox(const Window *w, int wid) const;
|
||||||
void ClickEditBox(Window *w, Point pt, int wid, int click_count, bool focus_changed);
|
void ClickEditBox(Window *w, Point pt, int wid, int click_count, bool focus_changed);
|
||||||
|
|
Loading…
Reference in New Issue