Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Fix text editor compat with other classes with pData vars
Signed-off-by: falkTX <falktx@falktx.com>
  • Loading branch information
falkTX committed Jul 11, 2023
1 parent dd50c72 commit ea26d28
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 25 deletions.
46 changes: 24 additions & 22 deletions opengl/DearImGuiColorTextEditor.cpp
@@ -1,6 +1,6 @@
/*
* Syntax highlighting text editor (for ImGui in DPF)
* Copyright (C) 2021 Filipe Coelho <falktx@falktx.com>
* Copyright (C) 2021-2023 Filipe Coelho <falktx@falktx.com>
* Copyright (c) 2017 BalazsJako
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
Expand Down Expand Up @@ -30,20 +30,22 @@
# include "DearImGuiColorTextEditor/TextEditor.h"
#endif

#include "Application.hpp"

#include <fstream>

START_NAMESPACE_DGL

// --------------------------------------------------------------------------------------------------------------------

template <class BaseWidget>
struct ImGuiTextEditor<BaseWidget>::PrivateData {
struct ImGuiTextEditor<BaseWidget>::TextEditorPrivateData {
ImGuiTextEditor<BaseWidget>* const self;
TextEditor editor;
std::string file;
bool showMenu;

explicit PrivateData(ImGuiTextEditor<BaseWidget>* const s)
explicit TextEditorPrivateData(ImGuiTextEditor<BaseWidget>* const s)
: self(s),
showMenu(false)
{
Expand Down Expand Up @@ -130,63 +132,63 @@ struct ImGuiTextEditor<BaseWidget>::PrivateData {
}
}

DISTRHO_DECLARE_NON_COPYABLE(PrivateData)
DISTRHO_DECLARE_NON_COPYABLE(TextEditorPrivateData)
};

// --------------------------------------------------------------------------------------------------------------------

template <>
ImGuiTextEditor<ImGuiSubWidget>::ImGuiTextEditor(Widget* const parent)
: ImGuiSubWidget(parent),
pData(new PrivateData(this)) {}
teData(new TextEditorPrivateData(this)) {}

template <>
ImGuiTextEditor<ImGuiTopLevelWidget>::ImGuiTextEditor(Window& windowToMapTo)
: ImGuiTopLevelWidget(windowToMapTo),
pData(new PrivateData(this)) {}
teData(new TextEditorPrivateData(this)) {}

template <class BaseWidget>
ImGuiTextEditor<BaseWidget>::~ImGuiTextEditor()
{
delete pData;
delete teData;
}

// --------------------------------------------------------------------------------------------------------------------

template <class BaseWidget>
void ImGuiTextEditor<BaseWidget>::setText(const std::string& text)
{
pData->editor.SetText(text);
teData->editor.SetText(text);
}

template <class BaseWidget>
std::string ImGuiTextEditor<BaseWidget>::getText() const
{
return pData->editor.GetText();
return teData->editor.GetText();
}

template <class BaseWidget>
void ImGuiTextEditor<BaseWidget>::setTextLines(const std::vector<std::string>& lines)
{
pData->editor.SetTextLines(lines);
teData->editor.SetTextLines(lines);
}

template <class BaseWidget>
std::vector<std::string> ImGuiTextEditor<BaseWidget>::getTextLines() const
{
return pData->editor.GetTextLines();
return teData->editor.GetTextLines();
}

template <class BaseWidget>
std::string ImGuiTextEditor<BaseWidget>::getSelectedText() const
{
return pData->editor.GetSelectedText();
return teData->editor.GetSelectedText();
}

template <class BaseWidget>
std::string ImGuiTextEditor<BaseWidget>::getCurrentLineText()const
{
return pData->editor.GetCurrentLineText();
return teData->editor.GetCurrentLineText();
}

// --------------------------------------------------------------------------------------------------------------------
Expand All @@ -195,25 +197,25 @@ template <class BaseWidget>
void ImGuiTextEditor<BaseWidget>::onImGuiDisplay()
{
ImGuiWindowFlags flags = ImGuiWindowFlags_NoTitleBar|ImGuiWindowFlags_NoResize;
if (pData->showMenu)
if (teData->showMenu)
flags |= ImGuiWindowFlags_MenuBar;

ImGui::SetNextWindowPos(ImVec2(0, 0));
ImGui::SetNextWindowSize(ImVec2(BaseWidget::getWidth(), BaseWidget::getHeight()));

if (ImGui::Begin("TextEdit", nullptr, flags))
{
if (pData->showMenu)
pData->renderMenuContent();
if (teData->showMenu)
teData->renderMenuContent();

TextEditor& editor(pData->editor);
TextEditor& editor(teData->editor);

const TextEditor::Coordinates cpos = editor.GetCursorPosition();

ImGui::Text("%6d/%-6d %6d lines | %s | %s | %s | %s", cpos.mLine + 1, cpos.mColumn + 1, editor.GetTotalLines(),
editor.IsOverwrite() ? "Ovr" : "Ins",
editor.CanUndo() ? "*" : " ",
editor.GetLanguageDefinition().mName.c_str(), pData->file.c_str());
editor.GetLanguageDefinition().mName.c_str(), teData->file.c_str());

editor.Render("TextEditor");
}
Expand All @@ -232,14 +234,14 @@ ImGuiTextEditorStandaloneWindow::ImGuiTextEditorStandaloneWindow(Application& ap
: StandaloneWindow(app),
editor(*this)
{
editor.pData->showMenu = true;
editor.teData->showMenu = true;
}

ImGuiTextEditorStandaloneWindow::ImGuiTextEditorStandaloneWindow(Application& app, Window& transientParentWindow)
: StandaloneWindow(app, transientParentWindow),
editor(*this)
{
editor.pData->showMenu = true;
editor.teData->showMenu = true;
}

void ImGuiTextEditorStandaloneWindow::onDisplay()
Expand All @@ -256,8 +258,8 @@ void ImGuiTextEditorStandaloneWindow::onFileSelected(const char* const filename)
if (t.good())
{
const std::string str((std::istreambuf_iterator<char>(t)), std::istreambuf_iterator<char>());
editor.pData->editor.SetText(str);
editor.pData->file = filename;
editor.teData->editor.SetText(str);
editor.teData->file = filename;
}
}

Expand Down
6 changes: 3 additions & 3 deletions opengl/DearImGuiColorTextEditor.hpp
@@ -1,6 +1,6 @@
/*
* Syntax highlighting text editor (for ImGui in DPF)
* Copyright (C) 2021 Filipe Coelho <falktx@falktx.com>
* Copyright (C) 2021-2023 Filipe Coelho <falktx@falktx.com>
* Copyright (c) 2017 BalazsJako
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
Expand Down Expand Up @@ -74,8 +74,8 @@ class ImGuiTextEditor : public BaseWidget
void onImGuiDisplay() override;

private:
struct PrivateData;
PrivateData* const pData;
struct TextEditorPrivateData;
TextEditorPrivateData* const teData;
friend class ImGuiTextEditorStandaloneWindow;

DISTRHO_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(ImGuiTextEditor)
Expand Down

0 comments on commit ea26d28

Please sign in to comment.