diff --git a/src/misc_gui.cpp b/src/misc_gui.cpp index c25cea77a4..49a46e5b1c 100644 --- a/src/misc_gui.cpp +++ b/src/misc_gui.cpp @@ -971,7 +971,7 @@ struct QueryStringWindow : public Window 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->editbox.caption = caption; @@ -1033,7 +1033,7 @@ struct QueryStringWindow : public Window 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); this->parent->OnQueryTextFinished(this->editbox.text.buf); diff --git a/src/osk_gui.cpp b/src/osk_gui.cpp index 1cfbfc8ace..b6f946c791 100644 --- a/src/osk_gui.cpp +++ b/src/osk_gui.cpp @@ -39,7 +39,7 @@ struct OskWindow : public Window { QueryString *qs; ///< text-input int 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) @@ -58,7 +58,7 @@ struct OskWindow : public Window { 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 @@ -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(); @@ -425,8 +420,7 @@ void UpdateOSKOriginalText(const Window *parent, int button) OskWindow *osk = dynamic_cast(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(); } diff --git a/src/querystring_gui.h b/src/querystring_gui.h index 79eadc7993..bd8ccb7a88 100644 --- a/src/querystring_gui.h +++ b/src/querystring_gui.h @@ -27,7 +27,7 @@ struct QueryString { 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. Textbuf text; - const char *orig; + std::optional orig; bool handled; /** @@ -35,18 +35,10 @@ struct QueryString { * @param size Maximum size in bytes. * @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: void DrawEditBox(const Window *w, int wid) const; void ClickEditBox(Window *w, Point pt, int wid, int click_count, bool focus_changed);