Skip to content

Commit

Permalink
UPDATE: add sketchy implementation for porting ColorTextEdtior
Browse files Browse the repository at this point in the history
Note that we've added an function `get_current_context_ptr()`
to pass pointer of `ImGuiContext` since we cannot access the actual
memory address of it by the value returned by a Cython-wrapped
`get_current_context()` function. (just like the reason mentioned
in [1], a C object will be wrapped by a Python object, so that
we cannot access its real address even if we have defined a `_ptr`
field in the Cython extension class)

Besides, those functions for setting/getting context should also
be implemented in `pyimgui` (i.e. `get_current_context_ptr()` and
`set_current_context_ptr()`, but at least the former one).

And note that this might not be a good solution since we have to
expose the real address of internal object, so that these functions
are classified as workarounds and we should figure out a better
ones in the future.

[1]: NaleRaphael/codememo#1
  • Loading branch information
NaleRaphael committed Oct 3, 2020
1 parent c3df847 commit f88122d
Show file tree
Hide file tree
Showing 5 changed files with 439 additions and 1 deletion.
124 changes: 124 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
# cpp files generated by Cython
colortextedit/*.cpp
colortextedit/*.h

# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]
*$py.class
*.dll

# os-specific stuff
.DS_Store

# imgui runtime configuration
imgui.ini

# C extensions
*.so

# Cython related
cython_debug/
imgui/*.cpp
imgui/*.h
imgui/*.c

# Distribution / packaging
.Python
env/
build/
develop-eggs/
dist/
downloads/
eggs/
.eggs/
lib/
lib64/
parts/
sdist/
var/
*.egg-info/
.installed.cfg
*.egg
completion.svg

# PyInstaller
# Usually these files are written by a python script from a template
# before PyInstaller builds the exe, so as to inject date/other infos into it.
*.manifest
*.spec

# Installer logs
pip-log.txt
pip-delete-this-directory.txt

# Unit test / coverage reports
htmlcov/
.tox/
.coverage
.coverage.*
.cache
nosetests.xml
coverage.xml
*,cover
.hypothesis/

# Translations
*.mo
*.pot

# Django stuff:
*.log
local_settings.py

# Flask stuff:
instance/
.webassets-cache

# Scrapy stuff:
.scrapy

# Sphinx documentation
doc/_build/
doc/build

# following is autogenerated
doc/source/readme.rst

# PyBuilder
target/

# IPython Notebook
.ipynb_checkpoints

# pyenv
.python-version

# celery beat schedule file
celerybeat-schedule

# dotenv
.env

# virtualenv
venv/
ENV/

# Spyder project settings
.spyderproject

# Rope project settings
.ropeproject

# Pycharm project settings
.idea/

# Visual Studio Code project settings
.vscode/

# Bootstrap marker
.bootstrapped

# Fonts
*.ttf
*.otf
2 changes: 2 additions & 0 deletions colortextedit/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
from colortextedit import texteditor
from colortextedit.texteditor import *
205 changes: 205 additions & 0 deletions colortextedit/ctextedit.pxd
Original file line number Diff line number Diff line change
@@ -0,0 +1,205 @@
# -*- coding: utf-8 -*-
# distutils: language = c++
# distutils: include_dirs = imgui-cpp imgui-colortextedit
from libcpp cimport bool

# See also: https://cython.readthedocs.io/en/latest/src/userguide/wrapping_CPlusPlus.html#standard-library
from libcpp.string cimport string
from libcpp.vector cimport vector
from libcpp.utility cimport pair


cdef extern from "imgui.h":
ctypedef struct ImDrawChannel
ctypedef struct ImDrawCmd
ctypedef struct ImDrawData
ctypedef struct ImDrawList
ctypedef struct ImDrawListSharedData
ctypedef struct ImDrawVert
ctypedef struct ImFont
ctypedef struct ImFontAtlas
ctypedef struct ImFontConfig
ctypedef struct ImColor
ctypedef struct ImGuiIO
ctypedef struct ImGuiOnceUponAFrame
ctypedef struct ImGuiStorage
ctypedef struct ImGuiTextFilter
ctypedef struct ImGuiTextBuffer
ctypedef struct ImGuiInputTextCallbackData
ctypedef struct ImGuiSizeCallbackData
ctypedef struct ImGuiListClipper
ctypedef struct ImGuiPayload
ctypedef struct ImGuiContext

ctypedef void* ImTextureID
ctypedef unsigned int ImU32
ctypedef unsigned int ImGuiID
ctypedef unsigned short ImWchar
ctypedef int ImGuiCol
ctypedef int ImGuiDataType
ctypedef int ImGuiDir
ctypedef int ImGuiCond
ctypedef int ImGuiKey
ctypedef int ImGuiNavInput
ctypedef int ImGuiMouseCursor
ctypedef int ImGuiStyleVar
ctypedef int ImDrawCornerFlags
ctypedef int ImDrawListFlags
ctypedef int ImFontAtlasFlags
ctypedef int ImGuiBackendFlags
ctypedef int ImGuiColorEditFlags
ctypedef int ImGuiColumnsFlags
ctypedef int ImGuiConfigFlags
ctypedef int ImGuiComboFlags
ctypedef int ImGuiDragDropFlags
ctypedef int ImGuiFocusedFlags
ctypedef int ImGuiHoveredFlags
ctypedef int ImGuiInputTextFlags
ctypedef int ImGuiSelectableFlags
ctypedef int ImGuiTreeNodeFlags
ctypedef int ImGuiWindowFlags
ctypedef int (*ImGuiInputTextCallback)(ImGuiInputTextCallbackData *data);
ctypedef void (*ImGuiSizeCallback)(ImGuiSizeCallbackData* data);

ctypedef struct ImVec2:
float x
float y

ctypedef struct ImVec4:
float x
float y
float z
float w


ctypedef struct ImGuiIO:
ImGuiConfigFlags ConfigFlags
ImGuiBackendFlags BackendFlags
ImVec2 DisplaySiz
float DeltaTime
float IniSavingRate
const char* IniFilename
const char* LogFilename
float MouseDoubleClickTime
float MouseDoubleClickMaxDist
float MouseDragThreshold
int* KeyMap
float KeyRepeatDelay
float KeyRepeatRate
void* UserData

ImFontAtlas* Fonts
float FontGlobalScale
bool FontAllowUserScaling
ImVec2 DisplayFramebufferScale
ImVec2 DisplayVisibleMin
ImVec2 DisplayVisibleMax
bool ConfigMacOSXBehaviors
bool ConfigInputTextCursorBlink
bool ConfigResizeWindowsFromEdges

const char* (*GetClipboardTextFn)(void* user_data) except +
void (*SetClipboardTextFn)(void* user_data, const char* text) except +
void* ClipboardUserData

void* (*MemAllocFn)(size_t sz) except +
void (*MemFreeFn)(void* ptr) except +
void (*ImeSetInputScreenPosFn)(int x, int y) except +
void* ImeWindowHandle

ImVec2 MousePos
bool MouseDown[5]
float MouseWheel
float MouseWheelH
bool MouseDrawCursor
bool KeyCtrl
bool KeyShift
bool KeyAlt
bool KeySuper
bool KeysDown[512]
ImWchar InputCharacters[16+1]

void AddInputCharacter(ImWchar c) except +
void AddInputCharactersUTF8(const char* utf8_chars) except +
void ClearInputCharacters() except +

bool WantCaptureMouse
bool WantCaptureKeyboard
bool WantTextInput
bool WantSetMousePos
bool WantSaveIniSettings
bool NavActive
bool NavVisible
float Framerate
int MetricsRenderVertices
int MetricsRenderIndices
int MetricsActiveWindows
ImVec2 MouseDelta


cdef extern from "imgui.h" namespace "ImGui":
ImGuiContext* CreateContext(
ImFontAtlas* shared_font_atlas
) except +
void DestroyContext(ImGuiContext* ctx) except +
ImGuiContext* GetCurrentContext() except +
void SetCurrentContext(ImGuiContext* ctx) except +
bool DebugCheckVersionAndDataLayout(const char* version_str, size_t sz_io, size_t sz_style, size_t sz_vec2, size_t sz_vec4, size_t sz_drawvert) except +


cdef extern from "TextEditor.h":
cdef cppclass TextEditor:
void SetLanguageDefinition(const LanguageDefinition& aLanguageDef) except +
const LanguageDefinition& GetLanguageDefinition() except +

const Palette& GetPalette() except +
void SetPalette(const Palette& aValue) except +

void Render(const char* aTitle, const ImVec2& aSize, bool aBorder) except +


cdef extern from "TextEditor.h" namespace "TextEditor":
cdef cppclass PaletteIndex:
pass

cdef cppclass SelectionMode:
pass

ctypedef pair[string, PaletteIndex] TokenRegexString
ctypedef vector[TokenRegexString] TokenRegexStrings
ctypedef struct Palette

ctypedef struct LanguageDefinition:
TokenRegexString TokenRegexString
TokenRegexStrings TokenRegexStrings


cdef extern from "TextEditor.h" namespace "PaletteIndex":
cdef PaletteIndex Default
cdef PaletteIndex KeyWord
cdef PaletteIndex Number
cdef PaletteIndex String
cdef PaletteIndex CharLiteral
cdef PaletteIndex Punctuation
cdef PaletteIndex Preprocessor
cdef PaletteIndex Identifier
cdef PaletteIndex KnownIdentifier
cdef PaletteIndex PreprocIdentifier
cdef PaletteIndex Comment
cdef PaletteIndex MultiLineComment
cdef PaletteIndex Background
cdef PaletteIndex Cursor
cdef PaletteIndex Selection
cdef PaletteIndex ErrorMarker
cdef PaletteIndex Breakpoint
cdef PaletteIndex LineNumber
cdef PaletteIndex CurrentLineFill
cdef PaletteIndex CurrentLineFillInactive
cdef PaletteIndex CurrentLineEdge
cdef PaletteIndex Max


cdef extern from "TextEditor.h" namespace "SelectionMode":
cdef SelectionMode Normal
cdef SelectionMode Word
cdef SelectionMode Line
Loading

0 comments on commit f88122d

Please sign in to comment.