PEBL 2.2
Psychology Experiment Building Language - Cross-platform psychological experiment development system
TextEditor Class Reference

#include <TextEditor.h>

Classes

struct  Breakpoint
 
struct  Coordinates
 
struct  Glyph
 
struct  Identifier
 
struct  LanguageDefinition
 

Public Types

enum class  PaletteIndex {
  Default , Keyword , Number , String ,
  CharLiteral , Punctuation , Preprocessor , Identifier ,
  KnownIdentifier , PreprocIdentifier , Comment , MultiLineComment ,
  Background , Cursor , Selection , ErrorMarker ,
  Breakpoint , LineNumber , CurrentLineFill , CurrentLineFillInactive ,
  CurrentLineEdge , Max
}
 
enum class  SelectionMode { Normal , Word , Line }
 
typedef std::string String
 
typedef std::unordered_map< std::string, IdentifierIdentifiers
 
typedef std::unordered_set< std::string > Keywords
 
typedef std::map< int, std::string > ErrorMarkers
 
typedef std::unordered_set< int > Breakpoints
 
typedef std::array< ImU32,(unsigned) PaletteIndex::MaxPalette
 
typedef uint8_t Char
 
typedef std::vector< GlyphLine
 
typedef std::vector< LineLines
 

Public Member Functions

 TextEditor ()
 
 ~TextEditor ()
 
void SetLanguageDefinition (const LanguageDefinition &aLanguageDef)
 
const LanguageDefinitionGetLanguageDefinition () const
 
const PaletteGetPalette () const
 
void SetPalette (const Palette &aValue)
 
void SetErrorMarkers (const ErrorMarkers &aMarkers)
 
void SetBreakpoints (const Breakpoints &aMarkers)
 
void Render (const char *aTitle, const ImVec2 &aSize=ImVec2(), bool aBorder=false)
 
void SetText (const std::string &aText)
 
std::string GetText () const
 
void SetTextLines (const std::vector< std::string > &aLines)
 
std::vector< std::string > GetTextLines () const
 
std::string GetSelectedText () const
 
std::string GetCurrentLineText () const
 
int GetTotalLines () const
 
bool IsOverwrite () const
 
void SetReadOnly (bool aValue)
 
bool IsReadOnly () const
 
bool IsTextChanged () const
 
bool IsCursorPositionChanged () const
 
bool IsColorizerEnabled () const
 
void SetColorizerEnable (bool aValue)
 
Coordinates GetCursorPosition () const
 
void SetCursorPosition (const Coordinates &aPosition)
 
void SetHandleMouseInputs (bool aValue)
 
bool IsHandleMouseInputsEnabled () const
 
void SetHandleKeyboardInputs (bool aValue)
 
bool IsHandleKeyboardInputsEnabled () const
 
void SetImGuiChildIgnored (bool aValue)
 
bool IsImGuiChildIgnored () const
 
void SetShowWhitespaces (bool aValue)
 
bool IsShowingWhitespaces () const
 
void SetTabSize (int aValue)
 
int GetTabSize () const
 
void InsertText (const std::string &aValue)
 
void InsertText (const char *aValue)
 
void MoveUp (int aAmount=1, bool aSelect=false)
 
void MoveDown (int aAmount=1, bool aSelect=false)
 
void MoveLeft (int aAmount=1, bool aSelect=false, bool aWordMode=false)
 
void MoveRight (int aAmount=1, bool aSelect=false, bool aWordMode=false)
 
void MoveTop (bool aSelect=false)
 
void MoveBottom (bool aSelect=false)
 
void MoveHome (bool aSelect=false)
 
void MoveEnd (bool aSelect=false)
 
void SetSelectionStart (const Coordinates &aPosition)
 
void SetSelectionEnd (const Coordinates &aPosition)
 
void SetSelection (const Coordinates &aStart, const Coordinates &aEnd, SelectionMode aMode=SelectionMode::Normal)
 
void SelectWordUnderCursor ()
 
void SelectAll ()
 
bool HasSelection () const
 
void Copy ()
 
void Cut ()
 
void Paste ()
 
void Delete ()
 
bool CanUndo () const
 
bool CanRedo () const
 
void Undo (int aSteps=1)
 
void Redo (int aSteps=1)
 

Static Public Member Functions

static const PaletteGetDarkPalette ()
 
static const PaletteGetLightPalette ()
 
static const PaletteGetRetroBluePalette ()
 

Detailed Description

Definition at line 13 of file TextEditor.h.

Member Typedef Documentation

◆ Breakpoints

typedef std::unordered_set<int> TextEditor::Breakpoints

Definition at line 132 of file TextEditor.h.

◆ Char

typedef uint8_t TextEditor::Char

Definition at line 134 of file TextEditor.h.

◆ ErrorMarkers

typedef std::map<int, std::string> TextEditor::ErrorMarkers

Definition at line 131 of file TextEditor.h.

◆ Identifiers

typedef std::unordered_map<std::string, Identifier> TextEditor::Identifiers

Definition at line 129 of file TextEditor.h.

◆ Keywords

typedef std::unordered_set<std::string> TextEditor::Keywords

Definition at line 130 of file TextEditor.h.

◆ Line

typedef std::vector<Glyph> TextEditor::Line

Definition at line 148 of file TextEditor.h.

◆ Lines

typedef std::vector<Line> TextEditor::Lines

Definition at line 149 of file TextEditor.h.

◆ Palette

typedef std::array<ImU32, (unsigned)PaletteIndex::Max> TextEditor::Palette

Definition at line 133 of file TextEditor.h.

◆ String

typedef std::string TextEditor::String

Definition at line 128 of file TextEditor.h.

Member Enumeration Documentation

◆ PaletteIndex

enum class TextEditor::PaletteIndex
strong
Enumerator
Default 
Keyword 
Number 
String 
CharLiteral 
Punctuation 
Preprocessor 
Identifier 
KnownIdentifier 
PreprocIdentifier 
Comment 
MultiLineComment 
Background 
Cursor 
Selection 
ErrorMarker 
Breakpoint 
LineNumber 
CurrentLineFill 
CurrentLineFillInactive 
CurrentLineEdge 
Max 

Definition at line 16 of file TextEditor.h.

◆ SelectionMode

enum class TextEditor::SelectionMode
strong
Enumerator
Normal 
Word 
Line 

Definition at line 42 of file TextEditor.h.

43 {
44 Normal,
45 Word,
46 Line
47 };
std::vector< Glyph > Line
Definition TextEditor.h:148

Constructor & Destructor Documentation

◆ TextEditor()

TextEditor::TextEditor ( )

Definition at line 26 of file TextEditor.cpp.

27 : mLineSpacing(1.0f)
28 , mUndoIndex(0)
29 , mTabSize(4)
30 , mOverwrite(false)
31 , mReadOnly(false)
32 , mWithinRender(false)
33 , mScrollToCursor(false)
34 , mScrollToTop(false)
35 , mTextChanged(false)
36 , mColorizerEnabled(true)
37 , mTextStart(20.0f)
38 , mLeftMargin(10)
39 , mCursorPositionChanged(false)
40 , mColorRangeMin(0)
41 , mColorRangeMax(0)
42 , mSelectionMode(SelectionMode::Normal)
43 , mCheckComments(true)
44 , mLastClick(-1.0f)
45 , mHandleKeyboardInputs(true)
46 , mHandleMouseInputs(true)
47 , mIgnoreImGuiChild(false)
48 , mShowWhitespaces(true)
49 , mStartTime(std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::system_clock::now().time_since_epoch()).count())
50{
53 mLines.push_back(Line());
54}
static const Palette & GetDarkPalette()
void SetPalette(const Palette &aValue)
void SetLanguageDefinition(const LanguageDefinition &aLanguageDef)
static const LanguageDefinition & HLSL()
int count
Definition test.cpp:12

References GetDarkPalette(), TextEditor::LanguageDefinition::HLSL(), SetLanguageDefinition(), and SetPalette().

◆ ~TextEditor()

TextEditor::~TextEditor ( )

Definition at line 56 of file TextEditor.cpp.

57{
58}

Member Function Documentation

◆ CanRedo()

bool TextEditor::CanRedo ( ) const

Definition at line 1988 of file TextEditor.cpp.

1989{
1990 return !mReadOnly && mUndoIndex < (int)mUndoBuffer.size();
1991}

Referenced by Redo().

◆ CanUndo()

bool TextEditor::CanUndo ( ) const

Definition at line 1983 of file TextEditor.cpp.

1984{
1985 return !mReadOnly && mUndoIndex > 0;
1986}

Referenced by Undo().

◆ Copy()

void TextEditor::Copy ( )

Definition at line 1909 of file TextEditor.cpp.

1910{
1911 if (HasSelection())
1912 {
1913 ImGui::SetClipboardText(GetSelectedText().c_str());
1914 }
1915 else
1916 {
1917 if (!mLines.empty())
1918 {
1919 std::string str;
1920 auto& line = mLines[GetActualCursorCoordinates().mLine];
1921 for (auto& g : line)
1922 str.push_back(g.mChar);
1923 ImGui::SetClipboardText(str.c_str());
1924 }
1925 }
1926}
bool HasSelection() const
std::string GetSelectedText() const

References GetSelectedText(), HasSelection(), and TextEditor::Coordinates::mLine.

Referenced by Cut().

◆ Cut()

void TextEditor::Cut ( )

Definition at line 1928 of file TextEditor.cpp.

1929{
1930 if (IsReadOnly())
1931 {
1932 Copy();
1933 }
1934 else
1935 {
1936 if (HasSelection())
1937 {
1938 UndoRecord u;
1939 u.mBefore = mState;
1940 u.mRemoved = GetSelectedText();
1941 u.mRemovedStart = mState.mSelectionStart;
1942 u.mRemovedEnd = mState.mSelectionEnd;
1943
1944 Copy();
1945 DeleteSelection();
1946
1947 u.mAfter = mState;
1948 AddUndo(u);
1949 }
1950 }
1951}
bool IsReadOnly() const
Definition TextEditor.h:211

References Copy(), GetSelectedText(), HasSelection(), and IsReadOnly().

◆ Delete()

void TextEditor::Delete ( )

Definition at line 1757 of file TextEditor.cpp.

1758{
1759 assert(!mReadOnly);
1760
1761 if (mLines.empty())
1762 return;
1763
1764 UndoRecord u;
1765 u.mBefore = mState;
1766
1767 if (HasSelection())
1768 {
1769 u.mRemoved = GetSelectedText();
1770 u.mRemovedStart = mState.mSelectionStart;
1771 u.mRemovedEnd = mState.mSelectionEnd;
1772
1773 DeleteSelection();
1774 }
1775 else
1776 {
1777 auto pos = GetActualCursorCoordinates();
1778 SetCursorPosition(pos);
1779 auto& line = mLines[pos.mLine];
1780
1781 if (pos.mColumn == GetLineMaxColumn(pos.mLine))
1782 {
1783 if (pos.mLine == (int)mLines.size() - 1)
1784 return;
1785
1786 u.mRemoved = '\n';
1787 u.mRemovedStart = u.mRemovedEnd = GetActualCursorCoordinates();
1788 Advance(u.mRemovedEnd);
1789
1790 auto& nextLine = mLines[pos.mLine + 1];
1791 line.insert(line.end(), nextLine.begin(), nextLine.end());
1792 RemoveLine(pos.mLine + 1);
1793 }
1794 else
1795 {
1796 auto cindex = GetCharacterIndex(pos);
1797 u.mRemovedStart = u.mRemovedEnd = GetActualCursorCoordinates();
1798 u.mRemovedEnd.mColumn++;
1799 u.mRemoved = GetText(u.mRemovedStart, u.mRemovedEnd);
1800
1801 auto d = UTF8CharLength(line[cindex].mChar);
1802 while (d-- > 0 && cindex < (int)line.size())
1803 line.erase(line.begin() + cindex);
1804 }
1805
1806 mTextChanged = true;
1807
1808 Colorize(pos.mLine, 1);
1809 }
1810
1811 u.mAfter = mState;
1812 AddUndo(u);
1813}
std::string GetText() const
void SetCursorPosition(const Coordinates &aPosition)

References GetSelectedText(), GetText(), HasSelection(), and SetCursorPosition().

◆ GetCurrentLineText()

std::string TextEditor::GetCurrentLineText ( ) const

Definition at line 2121 of file TextEditor.cpp.

2122{
2123 auto lineLength = GetLineMaxColumn(mState.mCursorPosition.mLine);
2124 return GetText(
2125 Coordinates(mState.mCursorPosition.mLine, 0),
2126 Coordinates(mState.mCursorPosition.mLine, lineLength));
2127}

References GetText().

◆ GetCursorPosition()

Coordinates TextEditor::GetCursorPosition ( ) const
inline

Definition at line 218 of file TextEditor.h.

218{ return GetActualCursorCoordinates(); }

Referenced by SelectWordUnderCursor().

◆ GetDarkPalette()

const TextEditor::Palette & TextEditor::GetDarkPalette ( )
static

Definition at line 2005 of file TextEditor.cpp.

2006{
2007 const static Palette p = { {
2008 0xff7f7f7f, // Default
2009 0xffd69c56, // Keyword
2010 0xff00ff00, // Number
2011 0xff7070e0, // String
2012 0xff70a0e0, // Char literal
2013 0xffffffff, // Punctuation
2014 0xff408080, // Preprocessor
2015 0xffaaaaaa, // Identifier
2016 0xff9bc64d, // Known identifier
2017 0xffc040a0, // Preproc identifier
2018 0xff206020, // Comment (single line)
2019 0xff406020, // Comment (multi line)
2020 0xff101010, // Background
2021 0xffe0e0e0, // Cursor
2022 0x80a06020, // Selection
2023 0x800020ff, // ErrorMarker
2024 0x40f08000, // Breakpoint
2025 0xff707000, // Line number
2026 0x40000000, // Current line fill
2027 0x40808080, // Current line fill (inactive)
2028 0x40a0a0a0, // Current line edge
2029 } };
2030 return p;
2031}
std::array< ImU32,(unsigned) PaletteIndex::Max > Palette
Definition TextEditor.h:133

Referenced by LauncherUI::LauncherUI(), and TextEditor().

◆ GetLanguageDefinition()

const LanguageDefinition & TextEditor::GetLanguageDefinition ( ) const
inline

Definition at line 189 of file TextEditor.h.

189{ return mLanguageDefinition; }

◆ GetLightPalette()

const TextEditor::Palette & TextEditor::GetLightPalette ( )
static

Definition at line 2033 of file TextEditor.cpp.

2034{
2035 const static Palette p = { {
2036 0xff7f7f7f, // None
2037 0xffff0c06, // Keyword
2038 0xff008000, // Number
2039 0xff2020a0, // String
2040 0xff304070, // Char literal
2041 0xff000000, // Punctuation
2042 0xff406060, // Preprocessor
2043 0xff404040, // Identifier
2044 0xff606010, // Known identifier
2045 0xffc040a0, // Preproc identifier
2046 0xff205020, // Comment (single line)
2047 0xff405020, // Comment (multi line)
2048 0xffffffff, // Background
2049 0xff000000, // Cursor
2050 0x80600000, // Selection
2051 0xa00010ff, // ErrorMarker
2052 0x80f08000, // Breakpoint
2053 0xff505000, // Line number
2054 0x40000000, // Current line fill
2055 0x40808080, // Current line fill (inactive)
2056 0x40000000, // Current line edge
2057 } };
2058 return p;
2059}

◆ GetPalette()

const Palette & TextEditor::GetPalette ( ) const
inline

Definition at line 191 of file TextEditor.h.

191{ return mPaletteBase; }

◆ GetRetroBluePalette()

const TextEditor::Palette & TextEditor::GetRetroBluePalette ( )
static

Definition at line 2061 of file TextEditor.cpp.

2062{
2063 const static Palette p = { {
2064 0xff00ffff, // None
2065 0xffffff00, // Keyword
2066 0xff00ff00, // Number
2067 0xff808000, // String
2068 0xff808000, // Char literal
2069 0xffffffff, // Punctuation
2070 0xff008000, // Preprocessor
2071 0xff00ffff, // Identifier
2072 0xffffffff, // Known identifier
2073 0xffff00ff, // Preproc identifier
2074 0xff808080, // Comment (single line)
2075 0xff404040, // Comment (multi line)
2076 0xff800000, // Background
2077 0xff0080ff, // Cursor
2078 0x80ffff00, // Selection
2079 0xa00000ff, // ErrorMarker
2080 0x80ff8000, // Breakpoint
2081 0xff808000, // Line number
2082 0x40000000, // Current line fill
2083 0x40808080, // Current line fill (inactive)
2084 0x40000000, // Current line edge
2085 } };
2086 return p;
2087}

◆ GetSelectedText()

std::string TextEditor::GetSelectedText ( ) const

Definition at line 2116 of file TextEditor.cpp.

2117{
2118 return GetText(mState.mSelectionStart, mState.mSelectionEnd);
2119}

References GetText().

Referenced by Copy(), Cut(), Delete(), and Paste().

◆ GetTabSize()

int TextEditor::GetTabSize ( ) const
inline

Definition at line 234 of file TextEditor.h.

234{ return mTabSize; }

◆ GetText()

std::string TextEditor::GetText ( ) const

Definition at line 2090 of file TextEditor.cpp.

2091{
2092 return GetText(Coordinates(), Coordinates((int)mLines.size(), 0));
2093}

References GetText().

Referenced by Delete(), GetCurrentLineText(), GetSelectedText(), and GetText().

◆ GetTextLines()

std::vector< std::string > TextEditor::GetTextLines ( ) const

Definition at line 2095 of file TextEditor.cpp.

2096{
2097 std::vector<std::string> result;
2098
2099 result.reserve(mLines.size());
2100
2101 for (auto & line : mLines)
2102 {
2103 std::string text;
2104
2105 text.resize(line.size());
2106
2107 for (size_t i = 0; i < line.size(); ++i)
2108 text[i] = line[i].mChar;
2109
2110 result.emplace_back(std::move(text));
2111 }
2112
2113 return result;
2114}

◆ GetTotalLines()

int TextEditor::GetTotalLines ( ) const
inline

Definition at line 207 of file TextEditor.h.

207{ return (int)mLines.size(); }

◆ HasSelection()

bool TextEditor::HasSelection ( ) const

Definition at line 1904 of file TextEditor.cpp.

1905{
1906 return mState.mSelectionEnd > mState.mSelectionStart;
1907}

Referenced by Copy(), Cut(), Delete(), and Paste().

◆ InsertText() [1/2]

void TextEditor::InsertText ( const char *  aValue)

Definition at line 1468 of file TextEditor.cpp.

1469{
1470 if (aValue == nullptr)
1471 return;
1472
1473 auto pos = GetActualCursorCoordinates();
1474 auto start = std::min(pos, mState.mSelectionStart);
1475 int totalLines = pos.mLine - start.mLine;
1476
1477 totalLines += InsertTextAt(pos, aValue);
1478
1479 SetSelection(pos, pos);
1480 SetCursorPosition(pos);
1481 Colorize(start.mLine - 1, totalLines + 2);
1482}
void SetSelection(const Coordinates &aStart, const Coordinates &aEnd, SelectionMode aMode=SelectionMode::Normal)

References SetCursorPosition(), and SetSelection().

◆ InsertText() [2/2]

void TextEditor::InsertText ( const std::string &  aValue)

Definition at line 1463 of file TextEditor.cpp.

1464{
1465 InsertText(aValue.c_str());
1466}
void InsertText(const std::string &aValue)

References InsertText().

Referenced by InsertText(), and Paste().

◆ IsColorizerEnabled()

bool TextEditor::IsColorizerEnabled ( ) const
inline

Definition at line 215 of file TextEditor.h.

215{ return mColorizerEnabled; }

◆ IsCursorPositionChanged()

bool TextEditor::IsCursorPositionChanged ( ) const
inline

Definition at line 213 of file TextEditor.h.

213{ return mCursorPositionChanged; }

◆ IsHandleKeyboardInputsEnabled()

bool TextEditor::IsHandleKeyboardInputsEnabled ( ) const
inline

Definition at line 225 of file TextEditor.h.

225{ return mHandleKeyboardInputs; }

◆ IsHandleMouseInputsEnabled()

bool TextEditor::IsHandleMouseInputsEnabled ( ) const
inline

Definition at line 222 of file TextEditor.h.

222{ return mHandleKeyboardInputs; }

◆ IsImGuiChildIgnored()

bool TextEditor::IsImGuiChildIgnored ( ) const
inline

Definition at line 228 of file TextEditor.h.

228{ return mIgnoreImGuiChild; }

◆ IsOverwrite()

bool TextEditor::IsOverwrite ( ) const
inline

Definition at line 208 of file TextEditor.h.

208{ return mOverwrite; }

◆ IsReadOnly()

bool TextEditor::IsReadOnly ( ) const
inline

Definition at line 211 of file TextEditor.h.

211{ return mReadOnly; }

Referenced by Cut(), and Paste().

◆ IsShowingWhitespaces()

bool TextEditor::IsShowingWhitespaces ( ) const
inline

Definition at line 231 of file TextEditor.h.

231{ return mShowWhitespaces; }

◆ IsTextChanged()

bool TextEditor::IsTextChanged ( ) const
inline

Definition at line 212 of file TextEditor.h.

212{ return mTextChanged; }

◆ MoveBottom()

void TextEditor::MoveBottom ( bool  aSelect = false)

◆ MoveDown()

void TextEditor::MoveDown ( int  aAmount = 1,
bool  aSelect = false 
)

Definition at line 1524 of file TextEditor.cpp.

1525{
1526 assert(mState.mCursorPosition.mColumn >= 0);
1527 auto oldPos = mState.mCursorPosition;
1528 mState.mCursorPosition.mLine = std::max(0, std::min((int)mLines.size() - 1, mState.mCursorPosition.mLine + aAmount));
1529
1530 if (mState.mCursorPosition != oldPos)
1531 {
1532 if (aSelect)
1533 {
1534 if (oldPos == mInteractiveEnd)
1535 mInteractiveEnd = mState.mCursorPosition;
1536 else if (oldPos == mInteractiveStart)
1537 mInteractiveStart = mState.mCursorPosition;
1538 else
1539 {
1540 mInteractiveStart = oldPos;
1541 mInteractiveEnd = mState.mCursorPosition;
1542 }
1543 }
1544 else
1545 mInteractiveStart = mInteractiveEnd = mState.mCursorPosition;
1546 SetSelection(mInteractiveStart, mInteractiveEnd);
1547
1548 EnsureCursorVisible();
1549 }
1550}

References SetSelection().

◆ MoveEnd()

void TextEditor::MoveEnd ( bool  aSelect = false)

Definition at line 1732 of file TextEditor.cpp.

1733{
1734 auto oldPos = mState.mCursorPosition;
1735 SetCursorPosition(Coordinates(mState.mCursorPosition.mLine, GetLineMaxColumn(oldPos.mLine)));
1736
1737 if (mState.mCursorPosition != oldPos)
1738 {
1739 if (aSelect)
1740 {
1741 if (oldPos == mInteractiveEnd)
1742 mInteractiveEnd = mState.mCursorPosition;
1743 else if (oldPos == mInteractiveStart)
1744 mInteractiveStart = mState.mCursorPosition;
1745 else
1746 {
1747 mInteractiveStart = oldPos;
1748 mInteractiveEnd = mState.mCursorPosition;
1749 }
1750 }
1751 else
1752 mInteractiveStart = mInteractiveEnd = mState.mCursorPosition;
1753 SetSelection(mInteractiveStart, mInteractiveEnd);
1754 }
1755}

References SetCursorPosition(), and SetSelection().

◆ MoveHome()

void TextEditor::MoveHome ( bool  aSelect = false)

Definition at line 1707 of file TextEditor.cpp.

1708{
1709 auto oldPos = mState.mCursorPosition;
1710 SetCursorPosition(Coordinates(mState.mCursorPosition.mLine, 0));
1711
1712 if (mState.mCursorPosition != oldPos)
1713 {
1714 if (aSelect)
1715 {
1716 if (oldPos == mInteractiveStart)
1717 mInteractiveStart = mState.mCursorPosition;
1718 else if (oldPos == mInteractiveEnd)
1719 mInteractiveEnd = mState.mCursorPosition;
1720 else
1721 {
1722 mInteractiveStart = mState.mCursorPosition;
1723 mInteractiveEnd = oldPos;
1724 }
1725 }
1726 else
1727 mInteractiveStart = mInteractiveEnd = mState.mCursorPosition;
1728 SetSelection(mInteractiveStart, mInteractiveEnd);
1729 }
1730}

References SetCursorPosition(), and SetSelection().

◆ MoveLeft()

void TextEditor::MoveLeft ( int  aAmount = 1,
bool  aSelect = false,
bool  aWordMode = false 
)

Definition at line 1557 of file TextEditor.cpp.

1558{
1559 if (mLines.empty())
1560 return;
1561
1562 auto oldPos = mState.mCursorPosition;
1563 mState.mCursorPosition = GetActualCursorCoordinates();
1564 auto line = mState.mCursorPosition.mLine;
1565 auto cindex = GetCharacterIndex(mState.mCursorPosition);
1566
1567 while (aAmount-- > 0)
1568 {
1569 if (cindex == 0)
1570 {
1571 if (line > 0)
1572 {
1573 --line;
1574 if ((int)mLines.size() > line)
1575 cindex = (int)mLines[line].size();
1576 else
1577 cindex = 0;
1578 }
1579 }
1580 else
1581 {
1582 --cindex;
1583 if (cindex > 0)
1584 {
1585 if ((int)mLines.size() > line)
1586 {
1587 while (cindex > 0 && IsUTFSequence(mLines[line][cindex].mChar))
1588 --cindex;
1589 }
1590 }
1591 }
1592
1593 mState.mCursorPosition = Coordinates(line, GetCharacterColumn(line, cindex));
1594 if (aWordMode)
1595 {
1596 mState.mCursorPosition = FindWordStart(mState.mCursorPosition);
1597 cindex = GetCharacterIndex(mState.mCursorPosition);
1598 }
1599 }
1600
1601 mState.mCursorPosition = Coordinates(line, GetCharacterColumn(line, cindex));
1602
1603 assert(mState.mCursorPosition.mColumn >= 0);
1604 if (aSelect)
1605 {
1606 if (oldPos == mInteractiveStart)
1607 mInteractiveStart = mState.mCursorPosition;
1608 else if (oldPos == mInteractiveEnd)
1609 mInteractiveEnd = mState.mCursorPosition;
1610 else
1611 {
1612 mInteractiveStart = mState.mCursorPosition;
1613 mInteractiveEnd = oldPos;
1614 }
1615 }
1616 else
1617 mInteractiveStart = mInteractiveEnd = mState.mCursorPosition;
1618 SetSelection(mInteractiveStart, mInteractiveEnd, aSelect && aWordMode ? SelectionMode::Word : SelectionMode::Normal);
1619
1620 EnsureCursorVisible();
1621}

References Normal, SetSelection(), and Word.

◆ MoveRight()

void TextEditor::MoveRight ( int  aAmount = 1,
bool  aSelect = false,
bool  aWordMode = false 
)

Definition at line 1623 of file TextEditor.cpp.

1624{
1625 auto oldPos = mState.mCursorPosition;
1626
1627 if (mLines.empty() || oldPos.mLine >= mLines.size())
1628 return;
1629
1630 auto cindex = GetCharacterIndex(mState.mCursorPosition);
1631 while (aAmount-- > 0)
1632 {
1633 auto lindex = mState.mCursorPosition.mLine;
1634 auto& line = mLines[lindex];
1635
1636 if (cindex >= line.size())
1637 {
1638 if (mState.mCursorPosition.mLine < mLines.size() - 1)
1639 {
1640 mState.mCursorPosition.mLine = std::max(0, std::min((int)mLines.size() - 1, mState.mCursorPosition.mLine + 1));
1641 mState.mCursorPosition.mColumn = 0;
1642 }
1643 else
1644 return;
1645 }
1646 else
1647 {
1648 cindex += UTF8CharLength(line[cindex].mChar);
1649 mState.mCursorPosition = Coordinates(lindex, GetCharacterColumn(lindex, cindex));
1650 if (aWordMode)
1651 mState.mCursorPosition = FindNextWord(mState.mCursorPosition);
1652 }
1653 }
1654
1655 if (aSelect)
1656 {
1657 if (oldPos == mInteractiveEnd)
1658 mInteractiveEnd = SanitizeCoordinates(mState.mCursorPosition);
1659 else if (oldPos == mInteractiveStart)
1660 mInteractiveStart = mState.mCursorPosition;
1661 else
1662 {
1663 mInteractiveStart = oldPos;
1664 mInteractiveEnd = mState.mCursorPosition;
1665 }
1666 }
1667 else
1668 mInteractiveStart = mInteractiveEnd = mState.mCursorPosition;
1669 SetSelection(mInteractiveStart, mInteractiveEnd, aSelect && aWordMode ? SelectionMode::Word : SelectionMode::Normal);
1670
1671 EnsureCursorVisible();
1672}

References Normal, SetSelection(), and Word.

◆ MoveTop()

void TextEditor::MoveTop ( bool  aSelect = false)

Definition at line 1674 of file TextEditor.cpp.

1675{
1676 auto oldPos = mState.mCursorPosition;
1677 SetCursorPosition(Coordinates(0, 0));
1678
1679 if (mState.mCursorPosition != oldPos)
1680 {
1681 if (aSelect)
1682 {
1683 mInteractiveEnd = oldPos;
1684 mInteractiveStart = mState.mCursorPosition;
1685 }
1686 else
1687 mInteractiveStart = mInteractiveEnd = mState.mCursorPosition;
1688 SetSelection(mInteractiveStart, mInteractiveEnd);
1689 }
1690}

References SetCursorPosition(), and SetSelection().

◆ MoveUp()

void TextEditor::MoveUp ( int  aAmount = 1,
bool  aSelect = false 
)

Definition at line 1498 of file TextEditor.cpp.

1499{
1500 auto oldPos = mState.mCursorPosition;
1501 mState.mCursorPosition.mLine = std::max(0, mState.mCursorPosition.mLine - aAmount);
1502 if (oldPos != mState.mCursorPosition)
1503 {
1504 if (aSelect)
1505 {
1506 if (oldPos == mInteractiveStart)
1507 mInteractiveStart = mState.mCursorPosition;
1508 else if (oldPos == mInteractiveEnd)
1509 mInteractiveEnd = mState.mCursorPosition;
1510 else
1511 {
1512 mInteractiveStart = mState.mCursorPosition;
1513 mInteractiveEnd = oldPos;
1514 }
1515 }
1516 else
1517 mInteractiveStart = mInteractiveEnd = mState.mCursorPosition;
1518 SetSelection(mInteractiveStart, mInteractiveEnd);
1519
1520 EnsureCursorVisible();
1521 }
1522}

References SetSelection().

◆ Paste()

void TextEditor::Paste ( )

Definition at line 1953 of file TextEditor.cpp.

1954{
1955 if (IsReadOnly())
1956 return;
1957
1958 auto clipText = ImGui::GetClipboardText();
1959 if (clipText != nullptr && strlen(clipText) > 0)
1960 {
1961 UndoRecord u;
1962 u.mBefore = mState;
1963
1964 if (HasSelection())
1965 {
1966 u.mRemoved = GetSelectedText();
1967 u.mRemovedStart = mState.mSelectionStart;
1968 u.mRemovedEnd = mState.mSelectionEnd;
1969 DeleteSelection();
1970 }
1971
1972 u.mAdded = clipText;
1973 u.mAddedStart = GetActualCursorCoordinates();
1974
1975 InsertText(clipText);
1976
1977 u.mAddedEnd = GetActualCursorCoordinates();
1978 u.mAfter = mState;
1979 AddUndo(u);
1980 }
1981}

References GetSelectedText(), HasSelection(), InsertText(), and IsReadOnly().

◆ Redo()

void TextEditor::Redo ( int  aSteps = 1)

Definition at line 1999 of file TextEditor.cpp.

2000{
2001 while (CanRedo() && aSteps-- > 0)
2002 mUndoBuffer[mUndoIndex++].Redo(this);
2003}
bool CanRedo() const

References CanRedo().

◆ Render()

void TextEditor::Render ( const char *  aTitle,
const ImVec2 &  aSize = ImVec2(),
bool  aBorder = false 
)

Definition at line 1120 of file TextEditor.cpp.

1121{
1122 mWithinRender = true;
1123 mTextChanged = false;
1124 mCursorPositionChanged = false;
1125
1126 ImGui::PushStyleColor(ImGuiCol_ChildBg, ImGui::ColorConvertU32ToFloat4(mPalette[(int)PaletteIndex::Background]));
1127 ImGui::PushStyleVar(ImGuiStyleVar_ItemSpacing, ImVec2(0.0f, 0.0f));
1128 if (!mIgnoreImGuiChild)
1129 ImGui::BeginChild(aTitle, aSize, aBorder, ImGuiWindowFlags_HorizontalScrollbar | ImGuiWindowFlags_AlwaysHorizontalScrollbar | ImGuiWindowFlags_NoMove);
1130
1131 if (mHandleKeyboardInputs)
1132 {
1133 HandleKeyboardInputs();
1134 ImGui::PushItemFlag(ImGuiItemFlags_NoTabStop, false);
1135 }
1136
1137 if (mHandleMouseInputs)
1138 HandleMouseInputs();
1139
1140 ColorizeInternal();
1141 Render();
1142
1143 if (mHandleKeyboardInputs)
1144 ImGui::PopItemFlag();
1145
1146 if (!mIgnoreImGuiChild)
1147 ImGui::EndChild();
1148
1149 ImGui::PopStyleVar();
1150 ImGui::PopStyleColor();
1151
1152 mWithinRender = false;
1153}
void Render(const char *aTitle, const ImVec2 &aSize=ImVec2(), bool aBorder=false)

References Background, and Render().

Referenced by Render().

◆ SelectAll()

void TextEditor::SelectAll ( )

Definition at line 1899 of file TextEditor.cpp.

1900{
1901 SetSelection(Coordinates(0, 0), Coordinates((int)mLines.size(), 0));
1902}

References SetSelection().

◆ SelectWordUnderCursor()

void TextEditor::SelectWordUnderCursor ( )

Definition at line 1893 of file TextEditor.cpp.

1894{
1895 auto c = GetCursorPosition();
1896 SetSelection(FindWordStart(c), FindWordEnd(c));
1897}
Coordinates GetCursorPosition() const
Definition TextEditor.h:218

References GetCursorPosition(), and SetSelection().

◆ SetBreakpoints()

void TextEditor::SetBreakpoints ( const Breakpoints aMarkers)
inline

Definition at line 195 of file TextEditor.h.

195{ mBreakpoints = aMarkers; }

◆ SetColorizerEnable()

void TextEditor::SetColorizerEnable ( bool  aValue)

Definition at line 1391 of file TextEditor.cpp.

1392{
1393 mColorizerEnabled = aValue;
1394}

◆ SetCursorPosition()

void TextEditor::SetCursorPosition ( const Coordinates aPosition)

Definition at line 1396 of file TextEditor.cpp.

1397{
1398 if (mState.mCursorPosition != aPosition)
1399 {
1400 mState.mCursorPosition = aPosition;
1401 mCursorPositionChanged = true;
1402 EnsureCursorVisible();
1403 }
1404}

Referenced by Delete(), InsertText(), MoveEnd(), MoveHome(), and MoveTop().

◆ SetErrorMarkers()

void TextEditor::SetErrorMarkers ( const ErrorMarkers aMarkers)
inline

Definition at line 194 of file TextEditor.h.

194{ mErrorMarkers = aMarkers; }

◆ SetHandleKeyboardInputs()

void TextEditor::SetHandleKeyboardInputs ( bool  aValue)
inline

Definition at line 224 of file TextEditor.h.

224{ mHandleKeyboardInputs = aValue;}

◆ SetHandleMouseInputs()

void TextEditor::SetHandleMouseInputs ( bool  aValue)
inline

Definition at line 221 of file TextEditor.h.

221{ mHandleMouseInputs = aValue;}

◆ SetImGuiChildIgnored()

void TextEditor::SetImGuiChildIgnored ( bool  aValue)
inline

Definition at line 227 of file TextEditor.h.

227{ mIgnoreImGuiChild = aValue;}

◆ SetLanguageDefinition()

void TextEditor::SetLanguageDefinition ( const LanguageDefinition aLanguageDef)

Definition at line 60 of file TextEditor.cpp.

61{
62 mLanguageDefinition = aLanguageDef;
63 mRegexList.clear();
64
65 for (auto& r : mLanguageDefinition.mTokenRegexStrings)
66 mRegexList.push_back(std::make_pair(std::regex(r.first, std::regex_constants::optimize), r.second));
67
68 Colorize();
69}

References TextEditor::LanguageDefinition::mTokenRegexStrings.

Referenced by LauncherUI::LauncherUI(), and TextEditor().

◆ SetPalette()

void TextEditor::SetPalette ( const Palette aValue)

Definition at line 71 of file TextEditor.cpp.

72{
73 mPaletteBase = aValue;
74}

Referenced by LauncherUI::LauncherUI(), and TextEditor().

◆ SetReadOnly()

void TextEditor::SetReadOnly ( bool  aValue)

Definition at line 1386 of file TextEditor.cpp.

1387{
1388 mReadOnly = aValue;
1389}

◆ SetSelection()

void TextEditor::SetSelection ( const Coordinates aStart,
const Coordinates aEnd,
SelectionMode  aMode = SelectionMode::Normal 
)

Definition at line 1420 of file TextEditor.cpp.

1421{
1422 auto oldSelStart = mState.mSelectionStart;
1423 auto oldSelEnd = mState.mSelectionEnd;
1424
1425 mState.mSelectionStart = SanitizeCoordinates(aStart);
1426 mState.mSelectionEnd = SanitizeCoordinates(aEnd);
1427 if (mState.mSelectionStart > mState.mSelectionEnd)
1428 std::swap(mState.mSelectionStart, mState.mSelectionEnd);
1429
1430 switch (aMode)
1431 {
1433 break;
1435 {
1436 mState.mSelectionStart = FindWordStart(mState.mSelectionStart);
1437 if (!IsOnWordBoundary(mState.mSelectionEnd))
1438 mState.mSelectionEnd = FindWordEnd(FindWordStart(mState.mSelectionEnd));
1439 break;
1440 }
1442 {
1443 const auto lineNo = mState.mSelectionEnd.mLine;
1444 const auto lineSize = (size_t)lineNo < mLines.size() ? mLines[lineNo].size() : 0;
1445 mState.mSelectionStart = Coordinates(mState.mSelectionStart.mLine, 0);
1446 mState.mSelectionEnd = Coordinates(lineNo, GetLineMaxColumn(lineNo));
1447 break;
1448 }
1449 default:
1450 break;
1451 }
1452
1453 if (mState.mSelectionStart != oldSelStart ||
1454 mState.mSelectionEnd != oldSelEnd)
1455 mCursorPositionChanged = true;
1456}

References Line, Normal, and Word.

Referenced by InsertText(), MoveDown(), MoveEnd(), MoveHome(), MoveLeft(), MoveRight(), MoveTop(), MoveUp(), SelectAll(), and SelectWordUnderCursor().

◆ SetSelectionEnd()

void TextEditor::SetSelectionEnd ( const Coordinates aPosition)

Definition at line 1413 of file TextEditor.cpp.

1414{
1415 mState.mSelectionEnd = SanitizeCoordinates(aPosition);
1416 if (mState.mSelectionStart > mState.mSelectionEnd)
1417 std::swap(mState.mSelectionStart, mState.mSelectionEnd);
1418}

◆ SetSelectionStart()

void TextEditor::SetSelectionStart ( const Coordinates aPosition)

Definition at line 1406 of file TextEditor.cpp.

1407{
1408 mState.mSelectionStart = SanitizeCoordinates(aPosition);
1409 if (mState.mSelectionStart > mState.mSelectionEnd)
1410 std::swap(mState.mSelectionStart, mState.mSelectionEnd);
1411}

◆ SetShowWhitespaces()

void TextEditor::SetShowWhitespaces ( bool  aValue)
inline

Definition at line 230 of file TextEditor.h.

230{ mShowWhitespaces = aValue; }

Referenced by LauncherUI::LauncherUI().

◆ SetTabSize()

void TextEditor::SetTabSize ( int  aValue)

Definition at line 1458 of file TextEditor.cpp.

1459{
1460 mTabSize = std::max(0, std::min(32, aValue));
1461}

Referenced by LauncherUI::LauncherUI().

◆ SetText()

void TextEditor::SetText ( const std::string &  aText)

Definition at line 1155 of file TextEditor.cpp.

1156{
1157 mLines.clear();
1158 mLines.emplace_back(Line());
1159 for (auto chr : aText)
1160 {
1161 if (chr == '\r')
1162 {
1163 // ignore the carriage return character
1164 }
1165 else if (chr == '\n')
1166 mLines.emplace_back(Line());
1167 else
1168 {
1169 mLines.back().emplace_back(Glyph(chr, PaletteIndex::Default));
1170 }
1171 }
1172
1173 mTextChanged = true;
1174 mScrollToTop = true;
1175
1176 mUndoBuffer.clear();
1177 mUndoIndex = 0;
1178
1179 Colorize();
1180}

References Default.

◆ SetTextLines()

void TextEditor::SetTextLines ( const std::vector< std::string > &  aLines)

Definition at line 1182 of file TextEditor.cpp.

1183{
1184 mLines.clear();
1185
1186 if (aLines.empty())
1187 {
1188 mLines.emplace_back(Line());
1189 }
1190 else
1191 {
1192 mLines.resize(aLines.size());
1193
1194 for (size_t i = 0; i < aLines.size(); ++i)
1195 {
1196 const std::string & aLine = aLines[i];
1197
1198 mLines[i].reserve(aLine.size());
1199 for (size_t j = 0; j < aLine.size(); ++j)
1200 mLines[i].emplace_back(Glyph(aLine[j], PaletteIndex::Default));
1201 }
1202 }
1203
1204 mTextChanged = true;
1205 mScrollToTop = true;
1206
1207 mUndoBuffer.clear();
1208 mUndoIndex = 0;
1209
1210 Colorize();
1211}

References Default.

◆ Undo()

void TextEditor::Undo ( int  aSteps = 1)

Definition at line 1993 of file TextEditor.cpp.

1994{
1995 while (CanUndo() && aSteps-- > 0)
1996 mUndoBuffer[--mUndoIndex].Undo(this);
1997}
bool CanUndo() const

References CanUndo().


The documentation for this class was generated from the following files: