Skip to content

Commit

Permalink
support shifting
Browse files Browse the repository at this point in the history
  • Loading branch information
scheffle committed Feb 28, 2024
1 parent 14da98e commit 8bde223
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 0 deletions.
67 changes: 67 additions & 0 deletions vstgui/lib/ctexteditor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,7 @@ struct TextEditorView : public CView,
void setFindString (std::string_view utf8Text) const override;

// commandos
bool doShifting (bool right) const;
void selectAll () const;
bool doCut () const;
bool doCopy () const;
Expand Down Expand Up @@ -493,11 +494,15 @@ TextEditorView::TextEditorView (ITextEditorController* controller) : CView ({0,
u'g', VirtualKey::None, {ModifierKey::Control, ModifierKey::Shift}};
md.commandKeys[Index (Command::UseSelectionForFind)] = {
u'e', VirtualKey::None, {ModifierKey::Control}};
md.commandKeys[Index (Command::ShiftLeft)] = {u'[', VirtualKey::None, {ModifierKey::Control}};
md.commandKeys[Index (Command::ShiftRight)] = {u']', VirtualKey::None, {ModifierKey::Control}};
#else
md.commandKeys[Index (Command::FindNext)] = {0, VirtualKey::F3, {}};
md.commandKeys[Index (Command::FindPrevious)] = {0, VirtualKey::F3, {ModifierKey::Shift}};
md.commandKeys[Index (Command::UseSelectionForFind)] = {
0, VirtualKey::F3, {ModifierKey::Control}};
md.commandKeys[Index (Command::ShiftLeft)] = {0, VirtualKey::Tab, {ModifierKey::Shift}};
md.commandKeys[Index (Command::ShiftRight)] = {0, VirtualKey::Tab, {}};
#endif
md.commandKeys[Index (Command::ShowFindPanel)] = {
u'f', VirtualKey::None, {ModifierKey::Control}};
Expand Down Expand Up @@ -767,6 +772,10 @@ bool TextEditorView::canHandleCommand (Command cmd) const
// TODO:
return true;
}
case Command::ShiftLeft:
return true;
case Command::ShiftRight:
return true;
case Command::TakeFocus:
return true;
case Command::FindNext:
Expand All @@ -793,6 +802,10 @@ bool TextEditorView::handleCommand (Command cmd) const

switch (cmd)
{
case Command::ShiftLeft:
return doShifting (false);
case Command::ShiftRight:
return doShifting (true);
case Command::SelectAll:
selectAll ();
return true;
Expand Down Expand Up @@ -1953,6 +1966,60 @@ bool TextEditorView::callSTB (Proc proc) const
return false;
}

//------------------------------------------------------------------------
bool TextEditorView::doShifting (bool right) const
{
if (md.editState.select_start > md.editState.select_end)
std::swap (md.editState.select_start, md.editState.select_end);
auto lineStart =
findLine (md.model.lines.begin (), md.model.lines.end (), md.editState.select_start);
if (lineStart == md.model.lines.end ())
return false;
auto lineEnd = md.editState.select_start == md.editState.select_end
? lineStart
: findLine (lineStart, md.model.lines.end (), md.editState.select_end - 1);
if (lineEnd == md.model.lines.end ())
return false;
++lineEnd;
auto originCursor = md.editState.cursor;
auto originSelectStart = md.editState.select_start;
auto originSelectEnd = md.editState.select_end;
md.editState.select_start = md.editState.select_end = originCursor;
auto numChanges = 0;
if (right)
{
while (lineStart != lineEnd)
{
--lineEnd;
md.editState.cursor = static_cast<int> (lineEnd->range.start);
callSTB ([&] () { stb_textedit_key (this, &md.editState, u'\t'); });
++numChanges;
}
md.editState.select_start = originSelectStart + 1;
md.editState.select_end = originSelectEnd + numChanges;
md.editState.cursor = md.editState.select_start;
}
else
{
while (lineStart != lineEnd)
{
--lineEnd;
md.editState.cursor = static_cast<int> (lineEnd->range.start);
if (md.model.text[md.editState.cursor] == u'\t')
{
callSTB ([&] () { stb_textedit_key (this, &md.editState, STB_TEXTEDIT_K_DELETE); });
++numChanges;
}
}
md.editState.select_start = originSelectStart - (numChanges ? 1 : 0);
md.editState.select_end = originSelectEnd - numChanges;
md.editState.cursor = md.editState.select_start;
}
onCursorChanged (originCursor, md.editState.cursor);
onSelectionChanged (makeRange (md.editState));
return true;
}

//------------------------------------------------------------------------
void TextEditorView::selectAll () const
{
Expand Down
2 changes: 2 additions & 0 deletions vstgui/lib/ctexteditor.h
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,8 @@ struct ITextEditor
FindPrevious,
SelectAll,
UseSelectionForFind,
ShiftLeft,
ShiftRight,
ShowFindPanel,
TakeFocus,
};
Expand Down

0 comments on commit 8bde223

Please sign in to comment.