Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file not shown.
Empty file.
Binary file added .vs/ImGuiColorTextEdit/v17/.wsuo
Binary file not shown.
Binary file added .vs/ImGuiColorTextEdit/v17/Browse.VC.db
Binary file not shown.
Binary file not shown.
3 changes: 3 additions & 0 deletions .vs/ProjectSettings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"CurrentProjectSetting": "No Configurations"
}
7 changes: 7 additions & 0 deletions .vs/VSWorkspaceState.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"ExpandedNodes": [
""
],
"SelectedNode": "\\TextEditor.h",
"PreviewInSolutionExplorer": false
}
Binary file added .vs/slnx.sqlite
Binary file not shown.
38 changes: 36 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,43 @@ Syntax highlighting text editor for ImGui

![Screenshot](https://github.com/BalazsJako/ImGuiColorTextEdit/wiki/ImGuiTextEdit.png "Screenshot")

Demo project: https://github.com/BalazsJako/ColorTextEditorDemo

This started as my attempt to write a relatively simple widget which provides text editing functionality with syntax highlighting. Now there are other contributors who provide valuable additions.
To use in your own projects you really only need these files
ImGuiDebugPanel.cpp
LanguageDefinitions.cpp
TextEditor.cpp
TextEditor.h
TextEditorDemo.cpp
UnitTests.cpp




To run the demo

//clone imgui

// copy the ImGuiColorTextEdit folder into your desired example_... folder
// add .\ImGuiColorTextEdit to c++ include directory of the appropriate example... project (remember both debug and release)

// include TextEditor.h and the following definitions into example...\main.cpp
#include "TextEditor.h"
int TextEditorDemo();


// add a call to the demo into example...\main.cpp

// 4. Show a textEditor frame in a simple window.
TextEditorDemo();


It should look like this:
![Screenshot](TextEditorDemo.png "Demo")


This started as BalazsJakos attempt to write a relatively simple widget which provides text editing functionality with syntax highlighting. Now there are other contributors who provide valuable additions.
The project was dead for a very long time, but has now reforked and revitalised!


While it relies on Omar Cornut's https://github.com/ocornut/imgui, it does not follow the "pure" one widget - one function approach. Since the editor has to maintain a relatively complex and large internal state, it did not seem to be practical to try and enforce fully immediate mode. It stores its internal state in an object instance which is reused across frames.

Expand Down
14 changes: 8 additions & 6 deletions TextEditor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -344,11 +344,11 @@ TextEditor::Coordinates TextEditor::ScreenPosToCoordinates(const ImVec2& aPositi
{
auto& line = mLines.at(lineNo);

int columnIndex = 0;
//int columnIndex = 0;
std::string cumulatedString = "";
float columnWidth = 0.0f;
//float columnWidth = 0.0f;
float columnX = 0.0f;
int delta = 0;
//int delta = 0;

// First we find the hovered column coord.
for (size_t columnIndex = 0; columnIndex < line.size(); ++columnIndex)
Expand Down Expand Up @@ -1204,10 +1204,12 @@ void TextEditor::Render(bool aParentIsFocused)
}

// Draw line number (right aligned)
snprintf(buf, 16, "%d ", lineNo + 1);
if (mDrawLineNumbers) {
snprintf(buf, 16, "%d ", lineNo + 1);

auto lineNoWidth = ImGui::GetFont()->CalcTextSizeA(ImGui::GetFontSize(), FLT_MAX, -1.0f, buf, nullptr, nullptr).x;
drawList->AddText(ImVec2(lineStartScreenPos.x + mTextStart - lineNoWidth, lineStartScreenPos.y), mPalette[(int)PaletteIndex::LineNumber], buf);
auto lineNoWidth = ImGui::GetFont()->CalcTextSizeA(ImGui::GetFontSize(), FLT_MAX, -1.0f, buf, nullptr, nullptr).x;
drawList->AddText(ImVec2(lineStartScreenPos.x + mTextStart - lineNoWidth, lineStartScreenPos.y), mPalette[(int)PaletteIndex::LineNumber], buf);
}

std::vector<Coordinates> cursorCoordsInThisLine;
for (int c = 0; c <= mState.mCurrentCursor; c++)
Expand Down
52 changes: 52 additions & 0 deletions TextEditor.h
Original file line number Diff line number Diff line change
Expand Up @@ -340,6 +340,25 @@ class IMGUI_API TextEditor

void ImGuiDebugPanel(const std::string& panelName = "Debug");
void UnitTests();

inline bool needsInit() { return mNeedsInit; }
int Initialize();

inline void setLastSaveTime(uint64_t lastSaveTime = 0) { mLastSaveTime = lastSaveTime; }
inline uint64_t getLastSaveTime() { return mLastSaveTime; }
inline void setAutoSaveInterval(uint64_t autoSaveInterval = 0) { mAutoSaveInterval = autoSaveInterval; }
inline uint64_t getAutoSaveInterval() { return mAutoSaveInterval; }

inline void SetFilename(const std::string& aFilename) { mFilename = aFilename; }
inline std::string GetFilename() const { return mFilename; }

void MoveTo(const Coordinates& aPosition);
void SetFind(const std::string& aFindStr) { mFindStr = aFindStr; }
void Find();

inline void drawLineNumbers(bool dln = true) { mDrawLineNumbers = dln; }


private:
typedef std::vector<std::pair<std::regex, PaletteIndex>> RegexList;

Expand Down Expand Up @@ -492,4 +511,37 @@ class IMGUI_API TextEditor
uint64_t mStartTime;

float mLastClick;



bool mDrawLineNumbers = true;

uint64_t mLastSaveTime = 0;
uint64_t mAutoSaveInterval = 5;
std::string mFilename;
std::string mFindStr;

bool mNeedsInit = true;

};




enum textEditorFlags
{
textEditorFlags_None = 0,
textEditorFlags_NoStatusBar = 1 << 1, // Disable the status bar
textEditorFlags_StatusBarTop = 1 << 2, // Status bar on Top (else at bottom)
textEditorFlags_NoMenuBar = 1 << 3, // disable the menu-bar
textEditorFlags_MenuBarOutsideFrame = 1 << 4, // disable the menu-bar
textEditorFlags_ReadOnly = 1 << 5,
textEditorFlags_ReadOnly_OnceOnly = 1 << 6,
textEditorFlags_NoOpen = 1 << 7, // disable open
textEditorFlags_NoSave = 1 << 8, // disable save
textEditorFlags_AutoSave = 1 << 9,
textEditorFlags_NoQuit = 1 << 10, // disable quit
textEditorFlags_NoLineNumbers = 1 << 11, // don't display line numbers

textEditorFlags_NoDecoration = textEditorFlags_NoStatusBar | textEditorFlags_NoMenuBar | textEditorFlags_NoLineNumbers,
};
Loading