2015-04-16 16:55:32 +00:00
|
|
|
/*
|
|
|
|
GWEN
|
|
|
|
Copyright (c) 2010 Facepunch Studios
|
|
|
|
See license in Gwen.h
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "Gwen/Gwen.h"
|
|
|
|
#include "Gwen/Controls/TextBox.h"
|
|
|
|
#include "Gwen/Skin.h"
|
|
|
|
#include "Gwen/Utility.h"
|
|
|
|
#include "Gwen/Platform.h"
|
|
|
|
|
|
|
|
#include <math.h>
|
2019-03-23 19:45:59 +00:00
|
|
|
#include <cmath>
|
2015-04-16 16:55:32 +00:00
|
|
|
|
|
|
|
using namespace Gwen;
|
|
|
|
using namespace Gwen::Controls;
|
|
|
|
|
2018-09-23 21:17:31 +00:00
|
|
|
GWEN_CONTROL_CONSTRUCTOR(TextBox)
|
2015-04-16 16:55:32 +00:00
|
|
|
{
|
2018-09-23 21:17:31 +00:00
|
|
|
SetSize(200, 20);
|
2015-04-16 16:55:32 +00:00
|
|
|
|
2018-09-23 21:17:31 +00:00
|
|
|
SetMouseInputEnabled(true);
|
|
|
|
SetKeyboardInputEnabled(true);
|
2015-04-16 16:55:32 +00:00
|
|
|
|
2018-09-23 21:17:31 +00:00
|
|
|
SetAlignment(Pos::Left | Pos::CenterV);
|
|
|
|
SetTextPadding(Padding(4, 2, 4, 2));
|
2015-04-16 16:55:32 +00:00
|
|
|
|
|
|
|
m_iCursorPos = 0;
|
|
|
|
m_iCursorEnd = 0;
|
|
|
|
m_bSelectAll = false;
|
|
|
|
|
2018-09-23 21:17:31 +00:00
|
|
|
SetTextColor(Gwen::Color(50, 50, 50, 255)); // TODO: From Skin
|
2015-04-16 16:55:32 +00:00
|
|
|
|
2018-09-23 21:17:31 +00:00
|
|
|
SetTabable(true);
|
2015-04-16 16:55:32 +00:00
|
|
|
|
2018-09-23 21:17:31 +00:00
|
|
|
AddAccelerator(L"Ctrl + c", &TextBox::OnCopy);
|
|
|
|
AddAccelerator(L"Ctrl + x", &TextBox::OnCut);
|
|
|
|
AddAccelerator(L"Ctrl + v", &TextBox::OnPaste);
|
|
|
|
AddAccelerator(L"Ctrl + a", &TextBox::OnSelectAll);
|
2015-04-16 16:55:32 +00:00
|
|
|
}
|
|
|
|
|
2018-09-23 21:17:31 +00:00
|
|
|
bool TextBox::OnChar(Gwen::UnicodeChar c)
|
2015-04-16 16:55:32 +00:00
|
|
|
{
|
2018-09-23 21:17:31 +00:00
|
|
|
if (c == '\t') return false;
|
2015-04-16 16:55:32 +00:00
|
|
|
|
|
|
|
Gwen::UnicodeString str;
|
|
|
|
str += c;
|
|
|
|
|
2018-09-23 21:17:31 +00:00
|
|
|
InsertText(str);
|
2015-04-16 16:55:32 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-09-23 21:17:31 +00:00
|
|
|
void TextBox::InsertText(const Gwen::UnicodeString& strInsert)
|
2015-04-16 16:55:32 +00:00
|
|
|
{
|
|
|
|
// TODO: Make sure fits (implement maxlength)
|
|
|
|
|
2018-09-23 21:17:31 +00:00
|
|
|
if (HasSelection())
|
2015-04-16 16:55:32 +00:00
|
|
|
{
|
|
|
|
EraseSelection();
|
|
|
|
}
|
|
|
|
|
2018-09-23 21:17:31 +00:00
|
|
|
if (m_iCursorPos > TextLength()) m_iCursorPos = TextLength();
|
2015-04-16 16:55:32 +00:00
|
|
|
|
2018-09-23 21:17:31 +00:00
|
|
|
if (!IsTextAllowed(strInsert, m_iCursorPos))
|
2015-04-16 16:55:32 +00:00
|
|
|
return;
|
|
|
|
|
|
|
|
UnicodeString str = GetText();
|
2018-09-23 21:17:31 +00:00
|
|
|
str.insert(m_iCursorPos, strInsert);
|
|
|
|
SetText(str);
|
2015-04-16 16:55:32 +00:00
|
|
|
|
2018-09-23 21:17:31 +00:00
|
|
|
m_iCursorPos += (int)strInsert.size();
|
2015-04-16 16:55:32 +00:00
|
|
|
m_iCursorEnd = m_iCursorPos;
|
|
|
|
|
|
|
|
RefreshCursorBounds();
|
|
|
|
}
|
|
|
|
|
2018-09-23 21:17:31 +00:00
|
|
|
void TextBox::Render(Skin::Base* skin)
|
2015-04-16 16:55:32 +00:00
|
|
|
{
|
2018-09-23 21:17:31 +00:00
|
|
|
if (ShouldDrawBackground())
|
|
|
|
skin->DrawTextBox(this);
|
2015-04-16 16:55:32 +00:00
|
|
|
|
2018-09-23 21:17:31 +00:00
|
|
|
if (!HasFocus()) return;
|
2015-04-16 16:55:32 +00:00
|
|
|
|
|
|
|
// Draw selection.. if selected..
|
2018-09-23 21:17:31 +00:00
|
|
|
if (m_iCursorPos != m_iCursorEnd)
|
2015-04-16 16:55:32 +00:00
|
|
|
{
|
2018-09-23 21:17:31 +00:00
|
|
|
skin->GetRender()->SetDrawColor(Gwen::Color(50, 170, 255, 200));
|
|
|
|
skin->GetRender()->DrawFilledRect(m_rectSelectionBounds);
|
2015-04-16 16:55:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Draw caret
|
2019-03-23 19:45:59 +00:00
|
|
|
if (std::fmod(Gwen::Platform::GetTimeInSeconds() - m_fLastInputTime,
|
|
|
|
1.0f) > 0.5f)
|
|
|
|
skin->GetRender()->SetDrawColor(Gwen::Color(255, 255, 255, 255));
|
|
|
|
else
|
2018-09-23 21:17:31 +00:00
|
|
|
skin->GetRender()->SetDrawColor(Gwen::Color(0, 0, 0, 255));
|
2015-04-16 16:55:32 +00:00
|
|
|
|
2018-09-23 21:17:31 +00:00
|
|
|
skin->GetRender()->DrawFilledRect(m_rectCaretBounds);
|
2015-04-16 16:55:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void TextBox::RefreshCursorBounds()
|
|
|
|
{
|
|
|
|
m_fLastInputTime = Gwen::Platform::GetTimeInSeconds();
|
|
|
|
|
|
|
|
MakeCaratVisible();
|
|
|
|
|
2018-09-23 21:17:31 +00:00
|
|
|
Gwen::Point pA = GetCharacterPosition(m_iCursorPos);
|
|
|
|
Gwen::Point pB = GetCharacterPosition(m_iCursorEnd);
|
2015-04-16 16:55:32 +00:00
|
|
|
|
2018-09-23 21:17:31 +00:00
|
|
|
m_rectSelectionBounds.x = Utility::Min(pA.x, pB.x);
|
2015-04-16 16:55:32 +00:00
|
|
|
m_rectSelectionBounds.y = m_Text->Y() - 1;
|
2018-09-23 21:17:31 +00:00
|
|
|
m_rectSelectionBounds.w = Utility::Max(pA.x, pB.x) - m_rectSelectionBounds.x;
|
2015-04-16 16:55:32 +00:00
|
|
|
m_rectSelectionBounds.h = m_Text->Height() + 2;
|
|
|
|
|
|
|
|
m_rectCaretBounds.x = pA.x;
|
|
|
|
m_rectCaretBounds.y = m_Text->Y() - 1;
|
|
|
|
m_rectCaretBounds.w = 1;
|
|
|
|
m_rectCaretBounds.h = m_Text->Height() + 2;
|
2018-09-23 21:17:31 +00:00
|
|
|
|
2015-04-16 16:55:32 +00:00
|
|
|
Redraw();
|
|
|
|
}
|
|
|
|
|
2018-09-23 21:17:31 +00:00
|
|
|
void TextBox::OnPaste(Gwen::Controls::Base* /*pCtrl*/)
|
2015-04-16 16:55:32 +00:00
|
|
|
{
|
2018-09-23 21:17:31 +00:00
|
|
|
InsertText(Platform::GetClipboardText());
|
2015-04-16 16:55:32 +00:00
|
|
|
}
|
|
|
|
|
2018-09-23 21:17:31 +00:00
|
|
|
void TextBox::OnCopy(Gwen::Controls::Base* /*pCtrl*/)
|
2015-04-16 16:55:32 +00:00
|
|
|
{
|
2018-09-23 21:17:31 +00:00
|
|
|
if (!HasSelection()) return;
|
2015-04-16 16:55:32 +00:00
|
|
|
|
2018-09-23 21:17:31 +00:00
|
|
|
Platform::SetClipboardText(GetSelection());
|
2015-04-16 16:55:32 +00:00
|
|
|
}
|
|
|
|
|
2018-09-23 21:17:31 +00:00
|
|
|
void TextBox::OnCut(Gwen::Controls::Base* /*pCtrl*/)
|
2015-04-16 16:55:32 +00:00
|
|
|
{
|
2018-09-23 21:17:31 +00:00
|
|
|
if (!HasSelection()) return;
|
2015-04-16 16:55:32 +00:00
|
|
|
|
2018-09-23 21:17:31 +00:00
|
|
|
Platform::SetClipboardText(GetSelection());
|
2015-04-16 16:55:32 +00:00
|
|
|
EraseSelection();
|
|
|
|
}
|
|
|
|
|
2018-09-23 21:17:31 +00:00
|
|
|
void TextBox::OnSelectAll(Gwen::Controls::Base* /*pCtrl*/)
|
2015-04-16 16:55:32 +00:00
|
|
|
{
|
|
|
|
m_iCursorEnd = 0;
|
|
|
|
m_iCursorPos = TextLength();
|
|
|
|
|
|
|
|
RefreshCursorBounds();
|
|
|
|
}
|
|
|
|
|
2018-09-23 21:17:31 +00:00
|
|
|
void TextBox::OnMouseDoubleClickLeft(int /*x*/, int /*y*/)
|
|
|
|
{
|
|
|
|
OnSelectAll(this);
|
2015-04-16 16:55:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
UnicodeString TextBox::GetSelection()
|
|
|
|
{
|
2018-09-23 21:17:31 +00:00
|
|
|
if (!HasSelection()) return L"";
|
2015-04-16 16:55:32 +00:00
|
|
|
|
2018-09-23 21:17:31 +00:00
|
|
|
int iStart = Utility::Min(m_iCursorPos, m_iCursorEnd);
|
|
|
|
int iEnd = Utility::Max(m_iCursorPos, m_iCursorEnd);
|
2015-04-16 16:55:32 +00:00
|
|
|
|
|
|
|
const UnicodeString& str = GetText();
|
2018-09-23 21:17:31 +00:00
|
|
|
return str.substr(iStart, iEnd - iStart);
|
2015-04-16 16:55:32 +00:00
|
|
|
}
|
|
|
|
|
2018-09-23 21:17:31 +00:00
|
|
|
bool TextBox::OnKeyReturn(bool bDown)
|
2015-04-16 16:55:32 +00:00
|
|
|
{
|
2018-09-23 21:17:31 +00:00
|
|
|
if (bDown) return true;
|
|
|
|
|
2015-04-16 16:55:32 +00:00
|
|
|
OnEnter();
|
|
|
|
|
|
|
|
// Try to move to the next control, as if tab had been pressed
|
2018-09-23 21:17:31 +00:00
|
|
|
OnKeyTab(true);
|
2015-04-16 16:55:32 +00:00
|
|
|
|
|
|
|
// If we still have focus, blur it.
|
2018-09-23 21:17:31 +00:00
|
|
|
if (HasFocus())
|
2015-04-16 16:55:32 +00:00
|
|
|
{
|
|
|
|
Blur();
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-09-23 21:17:31 +00:00
|
|
|
bool TextBox::OnKeyBackspace(bool bDown)
|
2015-04-16 16:55:32 +00:00
|
|
|
{
|
2018-09-23 21:17:31 +00:00
|
|
|
if (!bDown) return true;
|
|
|
|
if (HasSelection())
|
2015-04-16 16:55:32 +00:00
|
|
|
{
|
|
|
|
EraseSelection();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-09-23 21:17:31 +00:00
|
|
|
if (m_iCursorPos == 0) return true;
|
2015-04-16 16:55:32 +00:00
|
|
|
|
2018-09-23 21:17:31 +00:00
|
|
|
DeleteText(m_iCursorPos - 1, 1);
|
2015-04-16 16:55:32 +00:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-09-23 21:17:31 +00:00
|
|
|
bool TextBox::OnKeyDelete(bool bDown)
|
2015-04-16 16:55:32 +00:00
|
|
|
{
|
2018-09-23 21:17:31 +00:00
|
|
|
if (!bDown) return true;
|
|
|
|
if (HasSelection())
|
2015-04-16 16:55:32 +00:00
|
|
|
{
|
|
|
|
EraseSelection();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-09-23 21:17:31 +00:00
|
|
|
if (m_iCursorPos >= TextLength()) return true;
|
2015-04-16 16:55:32 +00:00
|
|
|
|
2018-09-23 21:17:31 +00:00
|
|
|
DeleteText(m_iCursorPos, 1);
|
2015-04-16 16:55:32 +00:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-09-23 21:17:31 +00:00
|
|
|
bool TextBox::OnKeyLeft(bool bDown)
|
2015-04-16 16:55:32 +00:00
|
|
|
{
|
2018-09-23 21:17:31 +00:00
|
|
|
if (!bDown) return true;
|
2015-04-16 16:55:32 +00:00
|
|
|
|
2018-09-23 21:17:31 +00:00
|
|
|
if (m_iCursorPos > 0)
|
2015-04-16 16:55:32 +00:00
|
|
|
m_iCursorPos--;
|
|
|
|
|
2018-09-23 21:17:31 +00:00
|
|
|
if (!Gwen::Input::IsShiftDown())
|
2015-04-16 16:55:32 +00:00
|
|
|
{
|
|
|
|
m_iCursorEnd = m_iCursorPos;
|
|
|
|
}
|
|
|
|
|
|
|
|
RefreshCursorBounds();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-09-23 21:17:31 +00:00
|
|
|
bool TextBox::OnKeyRight(bool bDown)
|
2015-04-16 16:55:32 +00:00
|
|
|
{
|
2018-09-23 21:17:31 +00:00
|
|
|
if (!bDown) return true;
|
2015-04-16 16:55:32 +00:00
|
|
|
|
2018-09-23 21:17:31 +00:00
|
|
|
if (m_iCursorPos < TextLength())
|
2015-04-16 16:55:32 +00:00
|
|
|
m_iCursorPos++;
|
|
|
|
|
2018-09-23 21:17:31 +00:00
|
|
|
if (!Gwen::Input::IsShiftDown())
|
2015-04-16 16:55:32 +00:00
|
|
|
{
|
|
|
|
m_iCursorEnd = m_iCursorPos;
|
|
|
|
}
|
|
|
|
|
|
|
|
RefreshCursorBounds();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-09-23 21:17:31 +00:00
|
|
|
bool TextBox::OnKeyHome(bool bDown)
|
2015-04-16 16:55:32 +00:00
|
|
|
{
|
2018-09-23 21:17:31 +00:00
|
|
|
if (!bDown) return true;
|
2015-04-16 16:55:32 +00:00
|
|
|
m_iCursorPos = 0;
|
|
|
|
|
2018-09-23 21:17:31 +00:00
|
|
|
if (!Gwen::Input::IsShiftDown())
|
2015-04-16 16:55:32 +00:00
|
|
|
{
|
|
|
|
m_iCursorEnd = m_iCursorPos;
|
|
|
|
}
|
|
|
|
|
|
|
|
RefreshCursorBounds();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-09-23 21:17:31 +00:00
|
|
|
bool TextBox::OnKeyEnd(bool /*bDown*/)
|
2015-04-16 16:55:32 +00:00
|
|
|
{
|
|
|
|
m_iCursorPos = TextLength();
|
|
|
|
|
2018-09-23 21:17:31 +00:00
|
|
|
if (!Gwen::Input::IsShiftDown())
|
2015-04-16 16:55:32 +00:00
|
|
|
{
|
|
|
|
m_iCursorEnd = m_iCursorPos;
|
|
|
|
}
|
|
|
|
|
|
|
|
RefreshCursorBounds();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-09-23 21:17:31 +00:00
|
|
|
void TextBox::SetCursorPos(int i)
|
2015-04-16 16:55:32 +00:00
|
|
|
{
|
2018-09-23 21:17:31 +00:00
|
|
|
if (m_iCursorPos == i) return;
|
2015-04-16 16:55:32 +00:00
|
|
|
|
|
|
|
m_iCursorPos = i;
|
|
|
|
RefreshCursorBounds();
|
|
|
|
}
|
|
|
|
|
2018-09-23 21:17:31 +00:00
|
|
|
void TextBox::SetCursorEnd(int i)
|
2015-04-16 16:55:32 +00:00
|
|
|
{
|
2018-09-23 21:17:31 +00:00
|
|
|
if (m_iCursorEnd == i) return;
|
2015-04-16 16:55:32 +00:00
|
|
|
|
|
|
|
m_iCursorEnd = i;
|
|
|
|
RefreshCursorBounds();
|
|
|
|
}
|
|
|
|
|
2018-09-23 21:17:31 +00:00
|
|
|
void TextBox::DeleteText(int iStartPos, int iLength)
|
2015-04-16 16:55:32 +00:00
|
|
|
{
|
|
|
|
UnicodeString str = GetText();
|
2018-09-23 21:17:31 +00:00
|
|
|
str.erase(iStartPos, iLength);
|
|
|
|
SetText(str);
|
2015-04-16 16:55:32 +00:00
|
|
|
|
2018-09-23 21:17:31 +00:00
|
|
|
if (m_iCursorPos > iStartPos)
|
2015-04-16 16:55:32 +00:00
|
|
|
{
|
2018-09-23 21:17:31 +00:00
|
|
|
SetCursorPos(m_iCursorPos - iLength);
|
2015-04-16 16:55:32 +00:00
|
|
|
}
|
|
|
|
|
2018-09-23 21:17:31 +00:00
|
|
|
SetCursorEnd(m_iCursorPos);
|
2015-04-16 16:55:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool TextBox::HasSelection()
|
|
|
|
{
|
|
|
|
return m_iCursorPos != m_iCursorEnd;
|
|
|
|
}
|
|
|
|
|
|
|
|
void TextBox::EraseSelection()
|
|
|
|
{
|
2018-09-23 21:17:31 +00:00
|
|
|
int iStart = Utility::Min(m_iCursorPos, m_iCursorEnd);
|
|
|
|
int iEnd = Utility::Max(m_iCursorPos, m_iCursorEnd);
|
2015-04-16 16:55:32 +00:00
|
|
|
|
2018-09-23 21:17:31 +00:00
|
|
|
DeleteText(iStart, iEnd - iStart);
|
2015-04-16 16:55:32 +00:00
|
|
|
|
2018-09-23 21:17:31 +00:00
|
|
|
// Move the cursor to the start of the selection,
|
2015-04-16 16:55:32 +00:00
|
|
|
// since the end is probably outside of the string now.
|
|
|
|
m_iCursorPos = iStart;
|
|
|
|
m_iCursorEnd = iStart;
|
|
|
|
}
|
|
|
|
|
2018-09-23 21:17:31 +00:00
|
|
|
void TextBox::OnMouseClickLeft(int x, int y, bool bDown)
|
2015-04-16 16:55:32 +00:00
|
|
|
{
|
2018-09-23 21:17:31 +00:00
|
|
|
if (m_bSelectAll)
|
2015-04-16 16:55:32 +00:00
|
|
|
{
|
2018-09-23 21:17:31 +00:00
|
|
|
OnSelectAll(this);
|
2015-04-16 16:55:32 +00:00
|
|
|
m_bSelectAll = false;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-09-23 21:17:31 +00:00
|
|
|
int iChar = m_Text->GetClosestCharacter(m_Text->CanvasPosToLocal(Gwen::Point(x, y)));
|
2015-04-16 16:55:32 +00:00
|
|
|
|
2018-09-23 21:17:31 +00:00
|
|
|
if (bDown)
|
2015-04-16 16:55:32 +00:00
|
|
|
{
|
2018-09-23 21:17:31 +00:00
|
|
|
SetCursorPos(iChar);
|
2015-04-16 16:55:32 +00:00
|
|
|
|
2018-09-23 21:17:31 +00:00
|
|
|
if (!Gwen::Input::IsShiftDown())
|
|
|
|
SetCursorEnd(iChar);
|
2015-04-16 16:55:32 +00:00
|
|
|
|
|
|
|
Gwen::MouseFocus = this;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2018-09-23 21:17:31 +00:00
|
|
|
if (Gwen::MouseFocus == this)
|
2015-04-16 16:55:32 +00:00
|
|
|
{
|
2018-09-23 21:17:31 +00:00
|
|
|
SetCursorPos(iChar);
|
2015-04-16 16:55:32 +00:00
|
|
|
Gwen::MouseFocus = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-23 21:17:31 +00:00
|
|
|
void TextBox::OnMouseMoved(int x, int y, int /*deltaX*/, int /*deltaY*/)
|
2015-04-16 16:55:32 +00:00
|
|
|
{
|
2018-09-23 21:17:31 +00:00
|
|
|
if (Gwen::MouseFocus != this) return;
|
2015-04-16 16:55:32 +00:00
|
|
|
|
2018-09-23 21:17:31 +00:00
|
|
|
int iChar = m_Text->GetClosestCharacter(m_Text->CanvasPosToLocal(Gwen::Point(x, y)));
|
2015-04-16 16:55:32 +00:00
|
|
|
|
2018-09-23 21:17:31 +00:00
|
|
|
SetCursorPos(iChar);
|
2015-04-16 16:55:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void TextBox::MakeCaratVisible()
|
|
|
|
{
|
2018-09-23 21:17:31 +00:00
|
|
|
int iCaratPos = m_Text->GetCharacterPosition(m_iCursorPos).x;
|
2015-04-16 16:55:32 +00:00
|
|
|
|
|
|
|
// If the carat is already in a semi-good position, leave it.
|
|
|
|
{
|
|
|
|
int iRealCaratPos = iCaratPos + m_Text->X();
|
2018-09-23 21:17:31 +00:00
|
|
|
if (iRealCaratPos > Width() * 0.1f && iRealCaratPos < Width() * 0.9f)
|
2015-04-16 16:55:32 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// The ideal position is for the carat to be right in the middle
|
2018-09-23 21:17:31 +00:00
|
|
|
int idealx = -iCaratPos + Width() * 0.5f;
|
|
|
|
;
|
2015-04-16 16:55:32 +00:00
|
|
|
|
|
|
|
// Don't show too much whitespace to the right
|
2018-09-23 21:17:31 +00:00
|
|
|
if (idealx + m_Text->Width() < Width() - m_rTextPadding.right)
|
|
|
|
idealx = -m_Text->Width() + (Width() - m_rTextPadding.right);
|
2015-04-16 16:55:32 +00:00
|
|
|
|
|
|
|
// Or the left
|
2018-09-23 21:17:31 +00:00
|
|
|
if (idealx > m_rTextPadding.left)
|
2015-04-16 16:55:32 +00:00
|
|
|
idealx = m_rTextPadding.left;
|
|
|
|
|
2018-09-23 21:17:31 +00:00
|
|
|
m_Text->SetPos(idealx, m_Text->Y());
|
2015-04-16 16:55:32 +00:00
|
|
|
}
|
|
|
|
|
2018-09-23 21:17:31 +00:00
|
|
|
void TextBox::Layout(Skin::Base* skin)
|
2015-04-16 16:55:32 +00:00
|
|
|
{
|
2018-09-23 21:17:31 +00:00
|
|
|
BaseClass::Layout(skin);
|
2015-04-16 16:55:32 +00:00
|
|
|
|
|
|
|
RefreshCursorBounds();
|
|
|
|
}
|
|
|
|
|
|
|
|
void TextBox::OnTextChanged()
|
|
|
|
{
|
2018-09-23 21:17:31 +00:00
|
|
|
if (m_iCursorPos > TextLength()) m_iCursorPos = TextLength();
|
|
|
|
if (m_iCursorEnd > TextLength()) m_iCursorEnd = TextLength();
|
2015-04-16 16:55:32 +00:00
|
|
|
|
2018-09-23 21:17:31 +00:00
|
|
|
onTextChanged.Call(this);
|
2015-04-16 16:55:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void TextBox::OnEnter()
|
|
|
|
{
|
2018-09-23 21:17:31 +00:00
|
|
|
onReturnPressed.Call(this);
|
2015-04-16 16:55:32 +00:00
|
|
|
}
|