Skip to content

Commit

Permalink
W_EDITBOX: Improve ending editing mode
Browse files Browse the repository at this point in the history
  • Loading branch information
past-due committed Oct 11, 2023
1 parent 9d15ce1 commit 5718147
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 6 deletions.
40 changes: 34 additions & 6 deletions lib/widget/editbox.cpp
Expand Up @@ -450,6 +450,7 @@ void W_EDITBOX::run(W_CONTEXT *psContext)
{
lockedScreen->setFocus(nullptr);
}
stopEditing();
debug(LOG_INPUT, "EditBox cursor return");
return;
break;
Expand All @@ -467,12 +468,16 @@ void W_EDITBOX::run(W_CONTEXT *psContext)
else
{
// hitting ESC while the editbox is empty ends editing mode
StopTextInput(this);
if (auto lockedScreen = screenPointer.lock())
{
lockedScreen->setFocus(nullptr);
}
inputLoseFocus(); // clear the input buffer.
stopEditing();
if (onEscHandler)
{
onEscHandler(*this);
}
return;
}
break;
Expand Down Expand Up @@ -609,25 +614,43 @@ void W_EDITBOX::clicked(W_CONTEXT *psContext, WIDGET_KEY)
}


/* Respond to loss of focus */
void W_EDITBOX::focusLost()
void W_EDITBOX::stopEditing()
{
ASSERT(!(state & WEDBS_DISABLE), "editBoxFocusLost: disabled edit box");
if (state & WEDBS_DISABLE) // disabled button.
{
return;
}

/* Note the edit state */
unsigned editState = state & WEDBS_MASK;

/* Only have anything to do if the widget is being edited */
if ((editState & WEDBS_MASK) == WEDBS_FIXED)
{
return;
}

/* Stop editing the widget */
state = WEDBS_FIXED;
printStart = 0;
fitStringStart();
StopTextInput(this);
dirty = true;
}

/* Respond to loss of focus */
void W_EDITBOX::focusLost()
{
ASSERT(!(state & WEDBS_DISABLE), "editBoxFocusLost: disabled edit box");

stopEditing();

if (auto lockedScreen = screenPointer.lock())
{
lockedScreen->setReturn(shared_from_this());
}
dirty = true;
}


/* Respond to a mouse moving over an edit box */
void W_EDITBOX::highlight(W_CONTEXT *)
{
Expand Down Expand Up @@ -750,3 +773,8 @@ void W_EDITBOX::setOnReturnHandler(const OnReturnHandler& func)
{
onRetHandler = func;
}

void W_EDITBOX::setOnEscapeHandler(const OnReturnHandler& func)
{
onEscHandler = func;
}
4 changes: 4 additions & 0 deletions lib/widget/editbox.h
Expand Up @@ -59,6 +59,8 @@ class W_EDITBOX : public WIDGET
void display(int xOffset, int yOffset) override;
void geometryChanged() override;

void stopEditing();

void setState(unsigned state) override;
WzString getString() const override;
void setString(WzString string) override;
Expand All @@ -69,6 +71,7 @@ class W_EDITBOX : public WIDGET

typedef std::function<void (W_EDITBOX&)> OnReturnHandler;
void setOnReturnHandler(const OnReturnHandler& func);
void setOnEscapeHandler(const OnReturnHandler& func);

UDWORD state; // The current edit box state
WzString aText; // The text in the edit box
Expand Down Expand Up @@ -100,6 +103,7 @@ class W_EDITBOX : public WIDGET
EditBoxDisplayCache displayCache;
bool suppressAudioCallback = false;
OnReturnHandler onRetHandler = nullptr;
OnReturnHandler onEscHandler = nullptr;
};

#endif // __INCLUDED_LIB_WIDGET_EDITBOX_H__

0 comments on commit 5718147

Please sign in to comment.