Expanded TextField interface

This commit is contained in:
Jack Andersen 2015-12-27 12:42:45 -10:00
parent 48f6c61126
commit 13863d3256
8 changed files with 458 additions and 95 deletions

View File

@ -63,6 +63,15 @@ public:
IControlBinding* setControlBinding(IControlBinding* controlBinding); IControlBinding* setControlBinding(IControlBinding* controlBinding);
}; };
class ITextInputView : public Control, public boo::ITextInputCallback
{
protected:
static std::recursive_mutex m_textInputLk;
ITextInputView(ViewResources& res, View& parentView,
IControlBinding* controlBinding)
: Control(res, parentView, controlBinding) {}
};
} }
#endif // SPECTER_CONTROL_HPP #endif // SPECTER_CONTROL_HPP

View File

@ -252,6 +252,8 @@ struct DeferredWindowEvents : public boo::IWindowCallback
m_cmds.back().m_fragment = str; m_cmds.back().m_fragment = str;
} }
boo::ITextInputCallback* getTextInputCallback() {return m_rec.getTextInputCallback();}
void dispatchEvents() void dispatchEvents()
{ {
std::unique_lock<std::mutex> lk(m_mt); std::unique_lock<std::mutex> lk(m_mt);

View File

@ -27,7 +27,7 @@ class RootView : public View
bool m_destroyed = false; bool m_destroyed = false;
IViewManager& m_viewMan; IViewManager& m_viewMan;
ViewResources* m_viewRes; ViewResources* m_viewRes;
View* m_activeTextView = nullptr; ITextInputView* m_activeTextView = nullptr;
View* m_activeDragView = nullptr; View* m_activeDragView = nullptr;
DeferredWindowEvents<RootView> m_events; DeferredWindowEvents<RootView> m_events;
@ -57,6 +57,7 @@ public:
void modKeyDown(boo::EModifierKey mod, bool isRepeat); void modKeyDown(boo::EModifierKey mod, bool isRepeat);
void modKeyUp(boo::EModifierKey mod); void modKeyUp(boo::EModifierKey mod);
void utf8FragmentDown(const std::string& str); void utf8FragmentDown(const std::string& str);
boo::ITextInputCallback* getTextInputCallback() {return m_activeTextView;}
void dispatchEvents() {m_events.dispatchEvents();} void dispatchEvents() {m_events.dispatchEvents();}
void draw(boo::IGraphicsCommandQueue* gfxQ); void draw(boo::IGraphicsCommandQueue* gfxQ);
@ -68,7 +69,7 @@ public:
const ThemeData& themeData() const {return m_viewRes->m_theme;} const ThemeData& themeData() const {return m_viewRes->m_theme;}
View* setContentView(View* view); View* setContentView(View* view);
void setActiveTextView(View* textView) void setActiveTextView(ITextInputView* textView)
{ {
if (m_activeTextView) if (m_activeTextView)
m_activeTextView->setActive(false); m_activeTextView->setActive(false);

View File

@ -3,13 +3,18 @@
#include "Control.hpp" #include "Control.hpp"
#include "TextView.hpp" #include "TextView.hpp"
#include <boo/IWindow.hpp>
namespace Specter namespace Specter
{ {
class TextField : public Control class TextField : public ITextInputView
{ {
bool m_hasTextSet = false;
bool m_hasMarkSet = false;
std::string m_textStr; std::string m_textStr;
std::string m_deferredTextStr;
std::string m_deferredMarkStr;
std::unique_ptr<TextView> m_text; std::unique_ptr<TextView> m_text;
SolidShaderVert m_verts[32]; SolidShaderVert m_verts[32];
@ -20,9 +25,26 @@ class TextField : public Control
int m_nomWidth = 0; int m_nomWidth = 0;
int m_nomHeight = 0; int m_nomHeight = 0;
bool m_hasSelectionClear = false;
bool m_hasSelectionSet = false;
bool m_hasCursorSet = false;
size_t m_selectionStart = 0; size_t m_selectionStart = 0;
size_t m_deferredSelectionStart = 0;
size_t m_selectionCount = 0; size_t m_selectionCount = 0;
size_t m_deferredSelectionCount = 0;
size_t m_markReplStart = 0;
size_t m_deferredMarkReplStart = 0;
size_t m_markReplCount = 0;
size_t m_deferredMarkReplCount = 0;
size_t m_markSelStart = 0;
size_t m_deferredMarkSelStart = 0;
size_t m_markSelCount = 0;
size_t m_deferredMarkSelCount = 0;
size_t m_cursorPos = 0; size_t m_cursorPos = 0;
size_t m_deferredCursorPos = SIZE_MAX;
size_t m_cursorFrames = 0; size_t m_cursorFrames = 0;
size_t m_clickFrames = 15; size_t m_clickFrames = 15;
size_t m_clickFrames2 = 15; size_t m_clickFrames2 = 15;
@ -53,6 +75,22 @@ public:
void charKeyDown(unsigned long, boo::EModifierKey, bool); void charKeyDown(unsigned long, boo::EModifierKey, bool);
void specialKeyDown(boo::ESpecialKey, boo::EModifierKey, bool); void specialKeyDown(boo::ESpecialKey, boo::EModifierKey, bool);
void utf8FragmentDown(const std::string&); void utf8FragmentDown(const std::string&);
bool hasMarkedText() const;
std::pair<int,int> markedRange() const;
std::pair<int,int> selectedRange() const;
void setMarkedText(const std::string& str,
const std::pair<int,int>& selectedRange,
const std::pair<int,int>& replacementRange);
void unmarkText();
std::string substringForRange(const std::pair<int,int>& range,
std::pair<int,int>& actualRange) const;
void insertText(const std::string& str, const std::pair<int,int>& range);
int characterIndexAtPoint(const boo::SWindowCoord& point) const;
boo::SWindowRect rectForCharacterRange(const std::pair<int,int>& range,
std::pair<int,int>& actualRange) const;
void think(); void think();
void resized(const boo::SWindowRect& rootView, const boo::SWindowRect& sub); void resized(const boo::SWindowRect& rootView, const boo::SWindowRect& sub);
void draw(boo::IGraphicsCommandQueue* gfxQ); void draw(boo::IGraphicsCommandQueue* gfxQ);
@ -73,6 +111,16 @@ public:
m_viewVertBlockBuf->load(&m_viewVertBlock, sizeof(ViewBlock)); m_viewVertBlockBuf->load(&m_viewVertBlock, sizeof(ViewBlock));
m_text->setMultiplyColor(color); m_text->setMultiplyColor(color);
} }
private:
void _setCursorPos();
void _reallySetCursorPos(size_t pos);
void _setSelectionRange();
void _reallySetSelectionRange(size_t start, size_t len);
void _reallySetMarkRange(size_t start, size_t len);
void _clearSelectionRange();
void _setText();
void _setMarkedText();
}; };
} }

View File

@ -14,6 +14,7 @@ class ThemeData
{ {
Zeus::CColor m_uiText = Zeus::CColor::skWhite; Zeus::CColor m_uiText = Zeus::CColor::skWhite;
Zeus::CColor m_fieldText = Zeus::CColor::skBlack; Zeus::CColor m_fieldText = Zeus::CColor::skBlack;
Zeus::CColor m_fieldMarkedText = {0.25, 0.25, 0.25, 1.0};
Zeus::CColor m_selectedFieldText = Zeus::CColor::skWhite; Zeus::CColor m_selectedFieldText = Zeus::CColor::skWhite;
Zeus::CColor m_vpBg = {0.2, 0.2, 0.2, 1.0}; Zeus::CColor m_vpBg = {0.2, 0.2, 0.2, 1.0};
@ -40,10 +41,12 @@ class ThemeData
Zeus::CColor m_textfield2Disabled = {0.7823, 0.7823, 0.7823, 0.5}; Zeus::CColor m_textfield2Disabled = {0.7823, 0.7823, 0.7823, 0.5};
Zeus::CColor m_textfield1Disabled = {0.5725, 0.5725, 0.5725, 0.5}; Zeus::CColor m_textfield1Disabled = {0.5725, 0.5725, 0.5725, 0.5};
Zeus::CColor m_textfieldSelection = {0.2725, 0.2725, 0.2725, 1.0}; Zeus::CColor m_textfieldSelection = {0.2725, 0.2725, 0.2725, 1.0};
Zeus::CColor m_textfieldMarkSelection = {1.0, 1.0, 0.2725, 1.0};
public: public:
virtual const Zeus::CColor& uiText() const {return m_uiText;} virtual const Zeus::CColor& uiText() const {return m_uiText;}
virtual const Zeus::CColor& fieldText() const {return m_fieldText;} virtual const Zeus::CColor& fieldText() const {return m_fieldText;}
virtual const Zeus::CColor& fieldMarkedText() const {return m_fieldMarkedText;}
virtual const Zeus::CColor& selectedFieldText() const {return m_selectedFieldText;} virtual const Zeus::CColor& selectedFieldText() const {return m_selectedFieldText;}
virtual const Zeus::CColor& viewportBackground() const {return m_vpBg;} virtual const Zeus::CColor& viewportBackground() const {return m_vpBg;}
@ -70,6 +73,7 @@ public:
virtual const Zeus::CColor& textfield1Disabled() const {return m_textfield1Disabled;} virtual const Zeus::CColor& textfield1Disabled() const {return m_textfield1Disabled;}
virtual const Zeus::CColor& textfield2Disabled() const {return m_textfield2Disabled;} virtual const Zeus::CColor& textfield2Disabled() const {return m_textfield2Disabled;}
virtual const Zeus::CColor& textfieldSelection() const {return m_textfieldSelection;} virtual const Zeus::CColor& textfieldSelection() const {return m_textfieldSelection;}
virtual const Zeus::CColor& textfieldMarkSelection() const {return m_textfieldMarkSelection;}
}; };
class ViewResources class ViewResources

View File

@ -30,4 +30,6 @@ void Control::mouseLeave(const boo::SWindowCoord&)
{ {
} }
std::recursive_mutex ITextInputView::m_textInputLk;
} }

View File

@ -7,7 +7,7 @@ namespace Specter
static LogVisor::LogModule Log("Specter::RootView"); static LogVisor::LogModule Log("Specter::RootView");
RootView::RootView(IViewManager& viewMan, ViewResources& res, boo::IWindow* window) RootView::RootView(IViewManager& viewMan, ViewResources& res, boo::IWindow* window)
: View(res), m_window(window), m_events(*this), m_viewMan(viewMan), m_viewRes(&res) : View(res), m_window(window), m_viewMan(viewMan), m_viewRes(&res), m_events(*this)
{ {
window->setCallback(&m_events); window->setCallback(&m_events);
boo::SWindowRect rect = window->getWindowFrame(); boo::SWindowRect rect = window->getWindowFrame();
@ -108,16 +108,16 @@ void RootView::charKeyDown(unsigned long charCode, boo::EModifierKey mods, bool
{ {
if (m_view) if (m_view)
m_view->charKeyDown(charCode, mods, isRepeat); m_view->charKeyDown(charCode, mods, isRepeat);
if (m_activeTextView) //if (m_activeTextView)
m_activeTextView->charKeyDown(charCode, mods, isRepeat); // m_activeTextView->charKeyDown(charCode, mods, isRepeat);
} }
void RootView::charKeyUp(unsigned long charCode, boo::EModifierKey mods) void RootView::charKeyUp(unsigned long charCode, boo::EModifierKey mods)
{ {
if (m_view) if (m_view)
m_view->charKeyUp(charCode, mods); m_view->charKeyUp(charCode, mods);
if (m_activeTextView) //if (m_activeTextView)
m_activeTextView->charKeyUp(charCode, mods); // m_activeTextView->charKeyUp(charCode, mods);
} }
void RootView::specialKeyDown(boo::ESpecialKey key, boo::EModifierKey mods, bool isRepeat) void RootView::specialKeyDown(boo::ESpecialKey key, boo::EModifierKey mods, bool isRepeat)
@ -159,8 +159,8 @@ void RootView::modKeyUp(boo::EModifierKey mod)
void RootView::utf8FragmentDown(const std::string& str) void RootView::utf8FragmentDown(const std::string& str)
{ {
if (m_activeTextView) //if (m_activeTextView)
m_activeTextView->utf8FragmentDown(str); // m_activeTextView->utf8FragmentDown(str);
} }
View* RootView::setContentView(View* view) View* RootView::setContentView(View* view)

View File

@ -6,7 +6,7 @@ namespace Specter
{ {
TextField::TextField(ViewResources& res, View& parentView, IStringBinding* strBind) TextField::TextField(ViewResources& res, View& parentView, IStringBinding* strBind)
: Control(res, parentView, strBind) : ITextInputView(res, parentView, strBind)
{ {
m_bVertsBuf = res.m_factory->newDynamicBuffer(boo::BufferUse::Vertex, sizeof(SolidShaderVert), 32); m_bVertsBuf = res.m_factory->newDynamicBuffer(boo::BufferUse::Vertex, sizeof(SolidShaderVert), 32);
@ -48,15 +48,83 @@ TextField::TextField(ViewResources& res, View& parentView, IStringBinding* strBi
setText("テスト"); setText("テスト");
} }
void TextField::_setText()
{
if (m_hasTextSet)
{
_clearSelectionRange();
m_textStr = m_deferredTextStr;
m_text->typesetGlyphs(m_textStr, rootView().themeData().fieldText());
m_hasTextSet = false;
if (m_deferredMarkStr.size())
m_hasMarkSet = true;
}
}
void TextField::_setMarkedText()
{
if (m_hasMarkSet)
{
m_markReplStart = m_deferredMarkReplStart;
m_markReplCount = m_deferredMarkReplCount;
m_markSelStart = m_deferredMarkSelStart;
m_markSelCount = m_deferredMarkSelCount;
size_t repPoint;
size_t repEnd;
if (m_selectionCount)
{
repPoint = m_selectionStart;
repEnd = m_selectionStart;
}
else
{
repPoint = m_cursorPos;
repEnd = m_cursorPos;
}
if (m_markReplStart != SIZE_MAX)
{
repPoint += m_markReplStart;
repEnd += m_markReplStart + m_markReplCount;
}
size_t len = UTF8Iterator(m_textStr.cbegin()).countTo(m_textStr.cend());
repPoint = std::min(repPoint, len);
repEnd = std::min(repEnd, len);
std::string compStr(m_textStr.cbegin(), (UTF8Iterator(m_textStr.cbegin()) + repPoint).iter());
compStr += m_deferredMarkStr;
compStr += std::string((UTF8Iterator(m_textStr.cbegin()) + repEnd).iter(), m_textStr.cend());
m_text->typesetGlyphs(compStr, rootView().themeData().fieldText());
size_t pos = m_cursorPos;
if (m_deferredMarkStr.size())
pos += m_markSelStart;
if (m_markSelCount)
_reallySetMarkRange(pos, m_markSelCount);
else
_reallySetCursorPos(pos);
std::vector<TextView::RenderGlyph>& glyphs = m_text->accessGlyphs();
size_t defLen = UTF8Iterator(m_deferredMarkStr.cbegin()).countTo(m_deferredMarkStr.cend());
for (auto it=glyphs.begin()+repPoint ; it<glyphs.begin()+repPoint+defLen ; ++it)
it->m_color = rootView().themeData().fieldMarkedText();
m_text->updateGlyphs();
m_hasMarkSet = false;
}
}
void TextField::setText(const std::string& str) void TextField::setText(const std::string& str)
{ {
clearSelectionRange(); std::unique_lock<std::recursive_mutex> lk(m_textInputLk);
UTF8Iterator it(str.cbegin()); UTF8Iterator it(str.cbegin());
for (; it.iter() != str.cend() ; ++it) for (; it.iter() != str.cend() ; ++it)
if (*it.iter() == '\n') if (*it.iter() == '\n')
break; break;
m_textStr.assign(str.cbegin(), it.iter()); m_deferredTextStr.assign(str.cbegin(), it.iter());
m_text->typesetGlyphs(m_textStr, rootView().themeData().fieldText()); m_hasTextSet = true;
} }
void TextField::setInactive() void TextField::setInactive()
@ -91,13 +159,15 @@ void TextField::setDisabled()
void TextField::mouseDown(const boo::SWindowCoord& coord, boo::EMouseButton button, boo::EModifierKey mod) void TextField::mouseDown(const boo::SWindowCoord& coord, boo::EMouseButton button, boo::EModifierKey mod)
{ {
std::unique_lock<std::recursive_mutex> lk(m_textInputLk);
if (!m_active) if (!m_active)
{ {
rootView().setActiveTextView(this); rootView().setActiveTextView(this);
} }
else if (m_clickFrames2 < 15) else if (m_clickFrames2 < 15)
{ {
setSelectionRange(0, m_text->accessGlyphs().size()); size_t len = UTF8Iterator(m_textStr.cbegin()).countTo(m_textStr.cend());
setSelectionRange(0, len);
} }
else if (m_clickFrames < 15) else if (m_clickFrames < 15)
{ {
@ -128,6 +198,7 @@ void TextField::mouseMove(const boo::SWindowCoord& coord)
{ {
if (m_dragging != 0) if (m_dragging != 0)
{ {
std::unique_lock<std::recursive_mutex> lk(m_textInputLk);
size_t thisPos = m_text->reverseSelectGlyph(coord.pixel[0] - m_text->subRect().location[0]); size_t thisPos = m_text->reverseSelectGlyph(coord.pixel[0] - m_text->subRect().location[0]);
size_t minPos = std::min(m_dragStart, thisPos); size_t minPos = std::min(m_dragStart, thisPos);
size_t maxPos = std::max(m_dragStart, thisPos); size_t maxPos = std::max(m_dragStart, thisPos);
@ -152,6 +223,8 @@ void TextField::mouseLeave(const boo::SWindowCoord& coord)
void TextField::clipboardCopy() void TextField::clipboardCopy()
{ {
std::unique_lock<std::recursive_mutex> lk(m_textInputLk);
if (!m_selectionCount) if (!m_selectionCount)
return; return;
@ -164,25 +237,41 @@ void TextField::clipboardCopy()
(uint8_t*)&*begin.iter(), end.iter() - begin.iter()); (uint8_t*)&*begin.iter(), end.iter() - begin.iter());
} }
static std::string SanitizeUTF8TextLine(const char* string, size_t len)
{
const char* it = string;
utf8proc_int32_t ch;
utf8proc_ssize_t sz;
std::string ret;
ret.reserve(len);
for (sz = utf8proc_iterate((utf8proc_uint8_t*)it, -1, &ch);
it < string+len;
it += sz, sz = utf8proc_iterate((utf8proc_uint8_t*)it, -1, &ch))
{
if (sz <= 0)
break;
if (ch >= 0x20)
ret.append(it, sz);
}
return ret;
}
void TextField::clipboardPaste() void TextField::clipboardPaste()
{ {
std::unique_lock<std::recursive_mutex> lk(m_textInputLk);
size_t retSz; size_t retSz;
std::unique_ptr<uint8_t[]> retData = std::unique_ptr<uint8_t[]> retData =
rootView().window()->clipboardPaste(boo::EClipboardType::UTF8String, retSz); rootView().window()->clipboardPaste(boo::EClipboardType::UTF8String, retSz);
std::string saniData = SanitizeUTF8TextLine((char*)retData.get(), retSz);
if (retData && retSz) if (retData && saniData.size())
{ {
if (m_selectionCount) if (m_selectionCount)
{ {
std::string newStr(m_textStr.cbegin(), (UTF8Iterator(m_textStr.cbegin()) + m_selectionStart).iter()); std::string newStr(m_textStr.cbegin(), (UTF8Iterator(m_textStr.cbegin()) + m_selectionStart).iter());
newStr.append((char*)retData.get(), retSz); newStr.append(saniData);
UTF8Iterator countIt(newStr.cbegin()); size_t newSel = UTF8Iterator(newStr.cbegin()).countTo(newStr.cend());
size_t newSel = 0;
while (countIt.iter() < newStr.cend())
{
++newSel;
++countIt;
}
newStr.append((UTF8Iterator(m_textStr.cbegin()) + m_selectionStart + m_selectionCount).iter(), m_textStr.cend()); newStr.append((UTF8Iterator(m_textStr.cbegin()) + m_selectionStart + m_selectionCount).iter(), m_textStr.cend());
setText(newStr); setText(newStr);
setCursorPos(newSel); setCursorPos(newSel);
@ -190,14 +279,8 @@ void TextField::clipboardPaste()
else else
{ {
std::string newStr(m_textStr.cbegin(), (UTF8Iterator(m_textStr.cbegin()) + m_cursorPos).iter()); std::string newStr(m_textStr.cbegin(), (UTF8Iterator(m_textStr.cbegin()) + m_cursorPos).iter());
newStr.append((char*)retData.get(), retSz); newStr.append(saniData);
UTF8Iterator countIt(newStr.cbegin()); size_t newSel = UTF8Iterator(newStr.cbegin()).countTo(newStr.cend());
size_t newSel = 0;
while (countIt.iter() < newStr.cend())
{
++newSel;
++countIt;
}
newStr.append((UTF8Iterator(m_textStr.cbegin()) + m_cursorPos).iter(), m_textStr.cend()); newStr.append((UTF8Iterator(m_textStr.cbegin()) + m_cursorPos).iter(), m_textStr.cend());
setText(newStr); setText(newStr);
setCursorPos(newSel); setCursorPos(newSel);
@ -207,6 +290,9 @@ void TextField::clipboardPaste()
void TextField::charKeyDown(unsigned long charCode, boo::EModifierKey mods, bool isRepeat) void TextField::charKeyDown(unsigned long charCode, boo::EModifierKey mods, bool isRepeat)
{ {
if (charCode < 0x20)
return;
if ((mods & (boo::EModifierKey::Ctrl|boo::EModifierKey::Command)) != boo::EModifierKey::None) if ((mods & (boo::EModifierKey::Ctrl|boo::EModifierKey::Command)) != boo::EModifierKey::None)
{ {
if (charCode == 'c' || charCode == 'C') if (charCode == 'c' || charCode == 'C')
@ -223,7 +309,8 @@ void TextField::charKeyDown(unsigned long charCode, boo::EModifierKey mods, bool
utf8proc_ssize_t sz = utf8proc_encode_char(charCode, theChar); utf8proc_ssize_t sz = utf8proc_encode_char(charCode, theChar);
if (sz > 0) if (sz > 0)
newStr += (char*)theChar; newStr += (char*)theChar;
newStr.append((UTF8Iterator(m_textStr.cbegin()) + m_selectionStart + m_selectionCount).iter(), m_textStr.cend()); newStr.append((UTF8Iterator(m_textStr.cbegin()) + m_selectionStart + m_selectionCount).iter(),
m_textStr.cend());
size_t selStart = m_selectionStart; size_t selStart = m_selectionStart;
setText(newStr); setText(newStr);
setCursorPos(selStart + 1); setCursorPos(selStart + 1);
@ -243,6 +330,10 @@ void TextField::charKeyDown(unsigned long charCode, boo::EModifierKey mods, bool
void TextField::specialKeyDown(boo::ESpecialKey key, boo::EModifierKey mods, bool isRepeat) void TextField::specialKeyDown(boo::ESpecialKey key, boo::EModifierKey mods, bool isRepeat)
{ {
std::unique_lock<std::recursive_mutex> lk(m_textInputLk);
if (m_deferredMarkStr.size())
return;
if (key == boo::ESpecialKey::Left) if (key == boo::ESpecialKey::Left)
{ {
if ((mods & boo::EModifierKey::Shift) != boo::EModifierKey::None) if ((mods & boo::EModifierKey::Shift) != boo::EModifierKey::None)
@ -273,7 +364,8 @@ void TextField::specialKeyDown(boo::ESpecialKey key, boo::EModifierKey mods, boo
{ {
if ((mods & boo::EModifierKey::Shift) != boo::EModifierKey::None) if ((mods & boo::EModifierKey::Shift) != boo::EModifierKey::None)
{ {
if (m_cursorPos < m_text->accessGlyphs().size()) size_t len = UTF8Iterator(m_textStr.cbegin()).countTo(m_textStr.cend());
if (m_cursorPos < len)
{ {
size_t origPos = m_cursorPos; size_t origPos = m_cursorPos;
if (m_selectionCount) if (m_selectionCount)
@ -315,6 +407,7 @@ void TextField::specialKeyDown(boo::ESpecialKey key, boo::EModifierKey mods, boo
} }
else if (key == boo::ESpecialKey::Delete) else if (key == boo::ESpecialKey::Delete)
{ {
size_t len = UTF8Iterator(m_textStr.cbegin()).countTo(m_textStr.cend());
if (m_selectionCount) if (m_selectionCount)
{ {
std::string newStr(m_textStr.cbegin(), (UTF8Iterator(m_textStr.cbegin()) + m_selectionStart).iter()); std::string newStr(m_textStr.cbegin(), (UTF8Iterator(m_textStr.cbegin()) + m_selectionStart).iter());
@ -323,7 +416,7 @@ void TextField::specialKeyDown(boo::ESpecialKey key, boo::EModifierKey mods, boo
setText(newStr); setText(newStr);
setCursorPos(selStart); setCursorPos(selStart);
} }
else if (m_cursorPos < m_text->accessGlyphs().size()) else if (m_cursorPos < len)
{ {
std::string newStr(m_textStr.cbegin(), (UTF8Iterator(m_textStr.cbegin()) + m_cursorPos).iter()); std::string newStr(m_textStr.cbegin(), (UTF8Iterator(m_textStr.cbegin()) + m_cursorPos).iter());
newStr.append((UTF8Iterator(m_textStr.cbegin()) + (m_cursorPos+1)).iter(), m_textStr.cend()); newStr.append((UTF8Iterator(m_textStr.cbegin()) + (m_cursorPos+1)).iter(), m_textStr.cend());
@ -335,17 +428,12 @@ void TextField::specialKeyDown(boo::ESpecialKey key, boo::EModifierKey mods, boo
void TextField::utf8FragmentDown(const std::string& str) void TextField::utf8FragmentDown(const std::string& str)
{ {
std::string saniStr = SanitizeUTF8TextLine(str.data(), str.size());
if (m_selectionCount) if (m_selectionCount)
{ {
std::string newStr(m_textStr.cbegin(), (UTF8Iterator(m_textStr.cbegin()) + m_selectionStart).iter()); std::string newStr(m_textStr.cbegin(), (UTF8Iterator(m_textStr.cbegin()) + m_selectionStart).iter());
newStr += str; newStr += saniStr;
UTF8Iterator countIt(newStr.cbegin()); size_t newSel = UTF8Iterator(newStr.cbegin()).countTo(newStr.cend());
size_t newSel = 0;
while (countIt.iter() < newStr.cend())
{
++newSel;
++countIt;
}
newStr.append((UTF8Iterator(m_textStr.cbegin()) + m_selectionStart + m_selectionCount).iter(), m_textStr.cend()); newStr.append((UTF8Iterator(m_textStr.cbegin()) + m_selectionStart + m_selectionCount).iter(), m_textStr.cend());
setText(newStr); setText(newStr);
setCursorPos(newSel); setCursorPos(newSel);
@ -353,29 +441,153 @@ void TextField::utf8FragmentDown(const std::string& str)
else else
{ {
std::string newStr(m_textStr.cbegin(), (UTF8Iterator(m_textStr.cbegin()) + m_cursorPos).iter()); std::string newStr(m_textStr.cbegin(), (UTF8Iterator(m_textStr.cbegin()) + m_cursorPos).iter());
newStr += str; newStr += saniStr;
UTF8Iterator countIt(newStr.cbegin()); size_t newSel = UTF8Iterator(newStr.cbegin()).countTo(newStr.cend());
size_t newSel = 0;
while (countIt.iter() < newStr.cend())
{
++newSel;
++countIt;
}
newStr.append((UTF8Iterator(m_textStr.cbegin()) + m_cursorPos).iter(), m_textStr.cend()); newStr.append((UTF8Iterator(m_textStr.cbegin()) + m_cursorPos).iter(), m_textStr.cend());
setText(newStr); setText(newStr);
setCursorPos(newSel); setCursorPos(newSel);
} }
} }
bool TextField::hasMarkedText() const
{
std::unique_lock<std::recursive_mutex> lk(m_textInputLk);
return m_deferredMarkStr.size() != 0;
}
std::pair<int,int> TextField::markedRange() const
{
std::unique_lock<std::recursive_mutex> lk(m_textInputLk);
if (m_deferredMarkStr.empty())
return {-1, 0};
return {m_cursorPos, UTF8Iterator(m_deferredMarkStr.cbegin()).countTo(m_deferredMarkStr.cend())};
}
std::pair<int,int> TextField::selectedRange() const
{
std::unique_lock<std::recursive_mutex> lk(m_textInputLk);
if (!m_deferredSelectionCount)
return {-1, 0};
return {m_deferredSelectionStart, m_deferredSelectionCount};
}
void TextField::setMarkedText(const std::string& str,
const std::pair<int,int>& selectedRange,
const std::pair<int,int>& replacementRange)
{
std::unique_lock<std::recursive_mutex> lk(m_textInputLk);
m_deferredMarkStr = SanitizeUTF8TextLine(str.data(), str.size());
m_deferredMarkSelStart = selectedRange.first;
m_deferredMarkSelCount = selectedRange.second;
m_deferredMarkReplStart = replacementRange.first;
m_deferredMarkReplCount = replacementRange.second;
m_hasMarkSet = true;
}
void TextField::unmarkText()
{
std::unique_lock<std::recursive_mutex> lk(m_textInputLk);
m_deferredMarkStr.clear();
m_deferredMarkReplStart = 0;
m_deferredMarkReplCount = 0;
m_deferredMarkSelStart = 0;
m_deferredMarkSelCount = 0;
m_hasMarkSet = true;
}
std::string TextField::substringForRange(const std::pair<int,int>& range,
std::pair<int,int>& actualRange) const
{
std::unique_lock<std::recursive_mutex> lk(m_textInputLk);
UTF8Iterator begin(m_deferredTextStr.cbegin());
size_t curLen = UTF8Iterator(m_deferredTextStr.cbegin()).countTo(m_deferredTextStr.cend());
if (range.first >= curLen)
return std::string();
begin += range.first;
size_t endIdx = std::min(size_t(range.first + range.second), curLen);
UTF8Iterator end(m_deferredTextStr.cbegin());
end += endIdx;
actualRange.first = range.first;
actualRange.second = endIdx;
return std::string(begin.iter(), end.iter());
}
void TextField::insertText(const std::string& str, const std::pair<int,int>& range)
{
std::string saniStr = SanitizeUTF8TextLine(str.data(), str.size());
std::unique_lock<std::recursive_mutex> lk(m_textInputLk);
size_t curLen = UTF8Iterator(m_deferredTextStr.cbegin()).countTo(m_deferredTextStr.cend());
if (range.first < 0 || range.first >= curLen)
{
size_t beginPos = m_deferredCursorPos;
if (m_selectionCount)
beginPos = m_selectionCount;
beginPos = std::min(beginPos, curLen);
std::string newStr(m_deferredTextStr.cbegin(), (UTF8Iterator(m_deferredTextStr.cbegin())+beginPos).iter());
newStr += saniStr;
size_t newPos = UTF8Iterator(newStr.cbegin()).countTo(newStr.cend());
newStr += std::string((UTF8Iterator(m_deferredTextStr.cbegin())+beginPos).iter(), m_deferredTextStr.cend());
setText(newStr);
setCursorPos(newPos);
unmarkText();
return;
}
std::string newStr(m_deferredTextStr.cbegin(), (UTF8Iterator(m_deferredTextStr.cbegin()) + range.first).iter());
newStr += saniStr;
size_t newSel = UTF8Iterator(newStr.cbegin()).countTo(newStr.cend());
size_t endIdx = range.first + range.second;
if (endIdx >= newSel)
endIdx = newSel - 1;
newStr.append((UTF8Iterator(m_deferredTextStr.cbegin()) + endIdx).iter(), m_deferredTextStr.cend());
setText(newStr);
setCursorPos(newSel);
unmarkText();
}
int TextField::characterIndexAtPoint(const boo::SWindowCoord& point) const
{
std::unique_lock<std::recursive_mutex> lk(m_textInputLk);
return m_text->reverseSelectGlyph(point.pixel[0]);
}
boo::SWindowRect TextField::rectForCharacterRange(const std::pair<int,int>& range,
std::pair<int,int>& actualRange) const
{
std::unique_lock<std::recursive_mutex> lk(m_textInputLk);
UTF8Iterator begin(m_textStr.cbegin());
size_t curLen = UTF8Iterator(m_textStr.cbegin()).countTo(m_textStr.cend());
if (range.first >= curLen)
{
const std::vector<TextView::RenderGlyph>& glyphs = m_text->accessGlyphs();
const TextView::RenderGlyph& g = glyphs.back();
return {{subRect().location[0] + int(g.m_pos[3][0]), subRect().location[1] + int(g.m_pos[3][1])}};
}
begin += range.first;
size_t endIdx = std::min(size_t(range.first + range.second), curLen);
UTF8Iterator end(m_textStr.cbegin());
end += endIdx;
actualRange.first = range.first;
actualRange.second = endIdx;
const std::vector<TextView::RenderGlyph>& glyphs = m_text->accessGlyphs();
const TextView::RenderGlyph& g1 = glyphs[range.first];
const TextView::RenderGlyph& g2 = glyphs[endIdx];
fprintf(stderr, "Ret %d %d\n", subRect().location[0] + int(g1.m_pos[1][0]), subRect().location[1] + int(g1.m_pos[1][1]));
return {{subRect().location[0] + int(g1.m_pos[1][0]), subRect().location[1] + int(g1.m_pos[1][1])},
{int(g2.m_pos[3][0]-g1.m_pos[1][0]), int(g2.m_pos[0][1]-g1.m_pos[1][1])}};
}
void TextField::think() void TextField::think()
{ {
++m_cursorFrames; ++m_cursorFrames;
++m_clickFrames; ++m_clickFrames;
++m_clickFrames2; ++m_clickFrames2;
std::unique_lock<std::recursive_mutex> lk(m_textInputLk);
_setText();
_setSelectionRange();
_clearSelectionRange();
_setCursorPos();
_setMarkedText();
} }
void TextField::setActive(bool active) void TextField::setActive(bool active)
{ {
std::unique_lock<std::recursive_mutex> lk(m_textInputLk);
m_active = active; m_active = active;
if (!active) if (!active)
{ {
@ -383,49 +595,65 @@ void TextField::setActive(bool active)
rootView().window()->claimKeyboardFocus(nullptr); rootView().window()->claimKeyboardFocus(nullptr);
} }
else if (!m_selectionCount) else if (!m_selectionCount)
setSelectionRange(0, m_text->accessGlyphs().size()); {
size_t len = UTF8Iterator(m_textStr.cbegin()).countTo(m_textStr.cend());
setSelectionRange(0, len);
}
} }
void TextField::setCursorPos(size_t pos) void TextField::_reallySetCursorPos(size_t pos)
{ {
clearSelectionRange();
m_cursorPos = std::min(pos, m_text->accessGlyphs().size());
m_cursorFrames = 0;
float pf = rootView().viewRes().pixelFactor(); float pf = rootView().viewRes().pixelFactor();
int offset1 = 4 * pf + m_text->queryReverseAdvance(m_cursorPos); int offset1 = 4 * pf + m_text->queryReverseAdvance(pos);
int offset2 = offset1 + 2 * pf; int offset2 = offset1 + 2 * pf;
const Zeus::CColor& selColor = rootView().viewRes().themeData().textfieldSelection();
m_verts[28].m_pos.assign(offset1, 18 * pf, 0); m_verts[28].m_pos.assign(offset1, 18 * pf, 0);
m_verts[28].m_color = selColor;
m_verts[29].m_pos.assign(offset1, 4 * pf, 0); m_verts[29].m_pos.assign(offset1, 4 * pf, 0);
m_verts[29].m_color = selColor;
m_verts[30].m_pos.assign(offset2, 18 * pf, 0); m_verts[30].m_pos.assign(offset2, 18 * pf, 0);
m_verts[30].m_color = selColor;
m_verts[31].m_pos.assign(offset2, 4 * pf, 0); m_verts[31].m_pos.assign(offset2, 4 * pf, 0);
m_verts[31].m_color = selColor;
m_bVertsBuf->load(m_verts, sizeof(m_verts)); m_bVertsBuf->load(m_verts, sizeof(m_verts));
int focusRect[2] = {subRect().location[0] + offset1, subRect().location[1]}; int focusRect[2] = {subRect().location[0] + offset1, subRect().location[1]};
rootView().window()->claimKeyboardFocus(focusRect); rootView().window()->claimKeyboardFocus(focusRect);
} }
void TextField::setSelectionRange(size_t start, size_t count) void TextField::_setCursorPos()
{ {
if (!count) if (m_hasCursorSet)
{ {
setCursorPos(start); m_hasSelectionClear = true;
return; _clearSelectionRange();
m_cursorPos = std::min(m_deferredCursorPos, UTF8Iterator(m_textStr.cbegin()).countTo(m_textStr.cend()));
m_deferredCursorPos = m_cursorPos;
m_cursorFrames = 0;
_reallySetCursorPos(m_cursorPos);
m_hasCursorSet = false;
} }
}
m_selectionStart = std::min(start, m_text->accessGlyphs().size()-1); void TextField::setCursorPos(size_t pos)
m_selectionCount = std::min(count, m_text->accessGlyphs().size()-m_selectionStart); {
std::unique_lock<std::recursive_mutex> lk(m_textInputLk);
m_deferredCursorPos = pos;
m_hasCursorSet = true;
}
void TextField::_reallySetSelectionRange(size_t start, size_t len)
{
ViewResources& res = rootView().viewRes(); ViewResources& res = rootView().viewRes();
float pf = res.pixelFactor(); float pf = res.pixelFactor();
int offset1 = 5 * pf; int offset1 = 5 * pf;
int offset2 = offset1; int offset2 = offset1;
std::vector<TextView::RenderGlyph>& glyphs = m_text->accessGlyphs(); std::vector<TextView::RenderGlyph>& glyphs = m_text->accessGlyphs();
offset1 += glyphs[m_selectionStart].m_pos[0][0]; offset1 += glyphs[start].m_pos[0][0];
offset2 += glyphs[m_selectionStart+m_selectionCount-1].m_pos[2][0]; offset2 += glyphs[start+len-1].m_pos[2][0];
for (size_t i=0 ; i<glyphs.size() ; ++i) for (size_t i=0 ; i<glyphs.size() ; ++i)
{ {
if (i >= m_selectionStart && i < m_selectionStart + m_selectionCount) if (i >= start && i < start + len)
glyphs[i].m_color = rootView().themeData().selectedFieldText(); glyphs[i].m_color = rootView().themeData().selectedFieldText();
else else
glyphs[i].m_color = rootView().themeData().fieldText(); glyphs[i].m_color = rootView().themeData().fieldText();
@ -442,15 +670,84 @@ void TextField::setSelectionRange(size_t start, size_t count)
rootView().window()->claimKeyboardFocus(focusRect); rootView().window()->claimKeyboardFocus(focusRect);
} }
void TextField::_reallySetMarkRange(size_t start, size_t len)
{
ViewResources& res = rootView().viewRes();
float pf = res.pixelFactor();
int offset1 = 5 * pf;
int offset2 = offset1;
std::vector<TextView::RenderGlyph>& glyphs = m_text->accessGlyphs();
offset1 += glyphs[start].m_pos[0][0];
offset2 += glyphs[start+len-1].m_pos[2][0];
const Zeus::CColor& selColor = rootView().themeData().textfieldMarkSelection();
m_verts[28].m_pos.assign(offset1, 18 * pf, 0);
m_verts[28].m_color = selColor;
m_verts[29].m_pos.assign(offset1, 4 * pf, 0);
m_verts[29].m_color = selColor;
m_verts[30].m_pos.assign(offset2, 18 * pf, 0);
m_verts[30].m_color = selColor;
m_verts[31].m_pos.assign(offset2, 4 * pf, 0);
m_verts[31].m_color = selColor;
m_bVertsBuf->load(m_verts, sizeof(m_verts));
int focusRect[2] = {subRect().location[0] + offset1, subRect().location[1]};
rootView().window()->claimKeyboardFocus(focusRect);
}
void TextField::_setSelectionRange()
{
if (m_hasSelectionSet)
{
size_t len = UTF8Iterator(m_textStr.cbegin()).countTo(m_textStr.cend());
m_selectionStart = std::min(m_deferredSelectionStart, len-1);
m_deferredSelectionStart = m_selectionStart;
m_selectionCount = std::min(m_deferredSelectionCount, len-m_selectionStart);
m_deferredSelectionCount = m_selectionCount;
_reallySetSelectionRange(m_selectionStart, m_selectionCount);
m_hasSelectionSet = false;
}
}
void TextField::setSelectionRange(size_t start, size_t count)
{
std::unique_lock<std::recursive_mutex> lk(m_textInputLk);
if (!count)
{
setCursorPos(start);
return;
}
m_deferredSelectionStart = start;
m_deferredSelectionCount = count;
m_hasSelectionSet = true;
m_hasSelectionClear = false;
}
void TextField::_clearSelectionRange()
{
if (m_hasSelectionClear)
{
m_selectionStart = 0;
m_selectionCount = 0;
std::vector<TextView::RenderGlyph>& glyphs = m_text->accessGlyphs();
for (size_t i=0 ; i<glyphs.size() ; ++i)
glyphs[i].m_color = rootView().themeData().fieldText();
m_text->updateGlyphs();
m_hasSelectionClear = false;
}
}
void TextField::clearSelectionRange() void TextField::clearSelectionRange()
{ {
m_selectionStart = 0; std::unique_lock<std::recursive_mutex> lk(m_textInputLk);
m_selectionCount = 0; m_deferredSelectionStart = 0;
m_deferredSelectionCount = 0;
std::vector<TextView::RenderGlyph>& glyphs = m_text->accessGlyphs(); m_hasSelectionClear = true;
for (size_t i=0 ; i<glyphs.size() ; ++i) m_hasSelectionSet = false;
glyphs[i].m_color = rootView().themeData().fieldText();
m_text->updateGlyphs();
} }
void TextField::resized(const boo::SWindowRect& root, const boo::SWindowRect& sub) void TextField::resized(const boo::SWindowRect& root, const boo::SWindowRect& sub)
@ -514,7 +811,7 @@ void TextField::draw(boo::IGraphicsCommandQueue* gfxQ)
gfxQ->draw(0, 28); gfxQ->draw(0, 28);
if (m_active) if (m_active)
{ {
if (!m_selectionCount) if (!m_selectionCount && !m_markSelCount)
{ {
if (m_cursorFrames % 60 < 30) if (m_cursorFrames % 60 < 30)
gfxQ->draw(28, 4); gfxQ->draw(28, 4);