bgfx/examples/common/imgui/scintilla.cpp

1129 lines
26 KiB
C++
Raw Normal View History

2015-06-09 03:07:35 +03:00
/*
2016-01-01 11:11:04 +03:00
* Copyright 2011-2016 Branimir Karadzic. All rights reserved.
2015-06-09 03:07:35 +03:00
* License: https://github.com/bkaradzic/bgfx/blob/master/LICENSE
*/
#if defined(SCI_NAMESPACE)
2015-08-21 07:55:53 +03:00
#include <bx/bx.h>
2015-06-09 03:07:35 +03:00
#include <algorithm>
#include <map>
2015-08-21 07:55:53 +03:00
#if BX_PLATFORM_EMSCRIPTEN
# include <compat/ctype.h>
#endif // BX_PLATFORM_EMSCRIPTEN
#include <string>
2015-06-09 03:07:35 +03:00
#include <vector>
2015-06-10 09:56:30 +03:00
#include <string.h>
2015-06-09 03:07:35 +03:00
#include "scintilla/include/Platform.h"
#include "scintilla/include/Scintilla.h"
#include "scintilla/include/ILexer.h"
#include "scintilla/include/SciLexer.h"
#include "scintilla/lexlib/LexerModule.h"
#include "scintilla/src/SplitVector.h"
#include "scintilla/src/Partitioning.h"
#include "scintilla/src/RunStyles.h"
#include "scintilla/src/Catalogue.h"
#include "scintilla/src/ContractionState.h"
#include "scintilla/src/CellBuffer.h"
#include "scintilla/src/KeyMap.h"
#include "scintilla/src/Indicator.h"
#include "scintilla/src/XPM.h"
#include "scintilla/src/LineMarker.h"
#include "scintilla/src/Style.h"
#include "scintilla/src/ViewStyle.h"
#include "scintilla/src/Decoration.h"
#include "scintilla/src/CharClassify.h"
#include "scintilla/src/CaseFolder.h"
#include "scintilla/src/Document.h"
#include "scintilla/src/Selection.h"
#include "scintilla/src/PositionCache.h"
#include "scintilla/src/EditModel.h"
#include "scintilla/src/MarginView.h"
#include "scintilla/src/EditView.h"
#include "scintilla/src/Editor.h"
#include "scintilla/src/AutoComplete.h"
#include "scintilla/src/CallTip.h"
#include "scintilla/src/ScintillaBase.h"
#include <bx/debug.h>
#include <bx/macros.h>
#include <bx/string.h>
#include <bx/uint32_t.h>
#define STBTT_DEF extern
#include <stb/stb_truetype.h>
#include "imgui.h"
#include "../bgfx_utils.h"
#include "scintilla.h"
#include "../entry/input.h"
#define IMGUI_NEW(type) new (ImGui::MemAlloc(sizeof(type) ) ) type
#define IMGUI_DELETE(type, obj) reinterpret_cast<type*>(obj)->~type(), ImGui::MemFree(obj)
static void fillRectangle(Scintilla::PRectangle _rc, Scintilla::ColourDesired _color)
{
const uint32_t abgr = (uint32_t)_color.AsLong();
ImVec2 pos = ImGui::GetCursorScreenPos();
ImDrawList* drawList = ImGui::GetWindowDrawList();
drawList->AddDrawCmd();
drawList->AddRectFilled(
ImVec2(_rc.left + pos.x, _rc.top + pos.y)
, ImVec2(_rc.right + pos.x, _rc.bottom + pos.y)
, abgr
);
}
static inline uint32_t makeRgba(uint32_t r, uint32_t g, uint32_t b, uint32_t a = 0xFF)
{
return a << 24 | b << 16 | g << 8 | r;
}
struct FontInt
{
2015-07-15 20:53:37 +03:00
ImFont* m_font;
float m_scale;
2015-06-09 03:07:35 +03:00
float m_fontSize;
};
class SurfaceInt : public Scintilla::Surface
{
public:
SurfaceInt()
{
}
virtual ~SurfaceInt()
{
}
virtual void Init(Scintilla::WindowID /*_wid*/) BX_OVERRIDE
{
}
virtual void Init(Scintilla::SurfaceID /*_sid*/, Scintilla::WindowID /*_wid*/) BX_OVERRIDE
{
}
virtual void InitPixMap(int /*_width*/, int /*_height*/, Scintilla::Surface* /*_surface*/, Scintilla::WindowID /*_wid*/) BX_OVERRIDE
{
}
virtual void Release() BX_OVERRIDE
{
}
virtual bool Initialised() BX_OVERRIDE
{
return true;
}
virtual void PenColour(Scintilla::ColourDesired /*_fore*/) BX_OVERRIDE
{
}
virtual int LogPixelsY() BX_OVERRIDE
{
return 72;
}
virtual int DeviceHeightFont(int /*points*/) BX_OVERRIDE
{
return 1500;
}
virtual void MoveTo(int _x, int _y) BX_OVERRIDE
{
BX_UNUSED(_x, _y);
}
virtual void LineTo(int _x, int _y) BX_OVERRIDE
{
BX_UNUSED(_x, _y);
}
virtual void Polygon(Scintilla::Point *pts, int npts, Scintilla::ColourDesired fore, Scintilla::ColourDesired back) BX_OVERRIDE
{
BX_UNUSED(pts, npts, fore, back);
}
virtual void RectangleDraw(Scintilla::PRectangle rc, Scintilla::ColourDesired fore, Scintilla::ColourDesired back) BX_OVERRIDE
{
BX_UNUSED(fore);
FillRectangle(rc, back);
}
virtual void FillRectangle(Scintilla::PRectangle rc, Scintilla::ColourDesired back) BX_OVERRIDE
{
fillRectangle(rc, back);
}
virtual void FillRectangle(Scintilla::PRectangle rc, Scintilla::Surface& surfacePattern) BX_OVERRIDE
{
BX_UNUSED(rc, surfacePattern);
}
virtual void RoundedRectangle(Scintilla::PRectangle rc, Scintilla::ColourDesired fore, Scintilla::ColourDesired back) BX_OVERRIDE
{
BX_UNUSED(rc, fore, back);
}
virtual void AlphaRectangle(Scintilla::PRectangle _rc, int /*_cornerSize*/, Scintilla::ColourDesired _fill, int _alphaFill, Scintilla::ColourDesired /*_outline*/, int /*_alphaOutline*/, int /*_flags*/) BX_OVERRIDE
{
unsigned int back = 0
| (uint32_t)( (_fill.AsLong() & 0xffffff)
| ( (_alphaFill & 0xff) << 24) )
;
FillRectangle(_rc, Scintilla::ColourDesired(back) );
}
virtual void DrawRGBAImage(Scintilla::PRectangle /*_rc*/, int /*_width*/, int /*_height*/, const unsigned char* /*_pixelsImage*/) BX_OVERRIDE
{
}
virtual void Ellipse(Scintilla::PRectangle rc, Scintilla::ColourDesired fore, Scintilla::ColourDesired /*back*/) BX_OVERRIDE
{
FillRectangle(rc, fore);
}
virtual void Copy(Scintilla::PRectangle /*_rc*/, Scintilla::Point /*_from*/, Scintilla::Surface& /*_surfaceSource*/) BX_OVERRIDE
{
}
virtual void DrawTextNoClip(Scintilla::PRectangle rc, Scintilla::Font& _font, Scintilla::XYPOSITION ybase, const char *s, int len, Scintilla::ColourDesired fore, Scintilla::ColourDesired back) BX_OVERRIDE
{
BX_UNUSED(back);
DrawTextBase(rc, _font, ybase, s, len, fore);
}
virtual void DrawTextClipped(Scintilla::PRectangle rc, Scintilla::Font& _font, Scintilla::XYPOSITION ybase, const char *s, int len, Scintilla::ColourDesired fore, Scintilla::ColourDesired back) BX_OVERRIDE
{
BX_UNUSED(back);
DrawTextBase(rc, _font, ybase, s, len, fore);
}
virtual void DrawTextTransparent(Scintilla::PRectangle rc, Scintilla::Font& _font, Scintilla::XYPOSITION ybase, const char *s, int len, Scintilla::ColourDesired fore) BX_OVERRIDE
{
DrawTextBase(rc, _font, ybase, s, len, fore);
}
virtual void MeasureWidths(Scintilla::Font& /*_font*/, const char* _str, int _len, Scintilla::XYPOSITION* _positions) BX_OVERRIDE
{
float position = 0;
ImFont* imFont = ImGui::GetWindowFont();
while (_len--)
{
2015-07-18 06:57:43 +03:00
position += imFont->GetCharAdvance( (unsigned short)*_str++);
2015-06-09 03:07:35 +03:00
*_positions++ = position;
}
}
virtual Scintilla::XYPOSITION WidthText(Scintilla::Font& /*_font*/, const char* _str, int _len) BX_OVERRIDE
{
ImVec2 t = ImGui::CalcTextSize(_str, _str + _len);
return t.x;
}
virtual Scintilla::XYPOSITION WidthChar(Scintilla::Font& _font, char ch) BX_OVERRIDE
{
FontInt* fi = (FontInt*)_font.GetID();
2015-07-17 06:28:43 +03:00
return fi->m_font->GetCharAdvance( (unsigned int)ch) * fi->m_scale;
2015-06-09 03:07:35 +03:00
}
virtual Scintilla::XYPOSITION Ascent(Scintilla::Font& _font) BX_OVERRIDE
{
FontInt* fi = (FontInt*)_font.GetID();
return fi->m_font->Ascent * fi->m_scale;
2015-06-09 03:07:35 +03:00
}
virtual Scintilla::XYPOSITION Descent(Scintilla::Font& _font) BX_OVERRIDE
{
FontInt* fi = (FontInt*)_font.GetID();
return -fi->m_font->Descent * fi->m_scale;
2015-06-09 03:07:35 +03:00
}
2015-07-15 20:53:37 +03:00
virtual Scintilla::XYPOSITION InternalLeading(Scintilla::Font& /*_font*/) BX_OVERRIDE
2015-06-09 03:07:35 +03:00
{
return 0;
}
virtual Scintilla::XYPOSITION ExternalLeading(Scintilla::Font& /*_font*/) BX_OVERRIDE
2015-06-09 03:07:35 +03:00
{
2015-07-15 20:53:37 +03:00
return 0;
2015-06-09 03:07:35 +03:00
}
virtual Scintilla::XYPOSITION Height(Scintilla::Font& _font) BX_OVERRIDE
{
return Ascent(_font) + Descent(_font);
}
virtual Scintilla::XYPOSITION AverageCharWidth(Scintilla::Font& _font) BX_OVERRIDE
{
return WidthChar(_font, 'n');
}
virtual void SetClip(Scintilla::PRectangle /*_rc*/) BX_OVERRIDE
{
}
virtual void FlushCachedState() BX_OVERRIDE
{
}
virtual void SetUnicodeMode(bool /*_unicodeMode*/) BX_OVERRIDE
{
}
virtual void SetDBCSMode(int /*_codePage*/) BX_OVERRIDE
{
}
private:
void DrawTextBase(Scintilla::PRectangle _rc, Scintilla::Font& _font, float _ybase, const char* _str, int _len, Scintilla::ColourDesired _fore)
{
float xt = _rc.left;
float yt = _ybase;
uint32_t fore = (uint32_t)_fore.AsLong();
FontInt* fi = (FontInt*)_font.GetID();
ImVec2 pos = ImGui::GetCursorScreenPos();
ImDrawList* drawList = ImGui::GetWindowDrawList();
drawList->AddText(fi->m_font
2015-06-09 03:07:35 +03:00
, fi->m_fontSize
, ImVec2(xt + pos.x, yt + pos.y - fi->m_fontSize)
, fore
, _str
, _str + _len
);
}
Scintilla::ColourDesired m_penColour;
};
struct WindowInt
{
WindowInt()
: m_show(false)
{
}
Scintilla::PRectangle position;
bool m_show;
};
WindowInt* AllocateWindowInt()
{
return new WindowInt;
}
inline WindowInt* GetWindow(Scintilla::WindowID id)
{
return (WindowInt*)id;
}
class ListBoxInt : public Scintilla::ListBox
{
public:
2015-06-10 09:56:30 +03:00
ListBoxInt()
: m_maxStrWidth(0)
, m_lineHeight(10)
2015-06-09 03:07:35 +03:00
, m_desiredVisibleRows(5)
, m_aveCharWidth(8)
2015-06-10 09:56:30 +03:00
, m_unicodeMode(false)
2015-06-09 03:07:35 +03:00
{
}
2015-06-10 09:56:30 +03:00
~ListBoxInt()
2015-06-09 03:07:35 +03:00
{
}
virtual void SetFont(Scintilla::Font& /*_font*/) BX_OVERRIDE
{
}
virtual void Create(Scintilla::Window& /*_parent*/, int /*_ctrlID*/, Scintilla::Point _location, int _lineHeight, bool _unicodeMode, int /*_technology*/) BX_OVERRIDE
{
m_location = _location;
m_lineHeight = _lineHeight;
m_unicodeMode = _unicodeMode;
m_maxStrWidth = 0;
wid = Scintilla::WindowID(4);
}
virtual void SetAverageCharWidth(int width) BX_OVERRIDE
{
m_aveCharWidth = width;
}
virtual void SetVisibleRows(int rows) BX_OVERRIDE
{
m_desiredVisibleRows = rows;
}
virtual int GetVisibleRows() const BX_OVERRIDE
{
return m_desiredVisibleRows;
}
virtual Scintilla::PRectangle GetDesiredRect() BX_OVERRIDE
{
Scintilla::PRectangle rc;
rc.top = 0;
rc.left = 0;
rc.right = 350;
rc.bottom = 140;
return rc;
}
virtual int CaretFromEdge() BX_OVERRIDE
{
return 4 + 16;
}
virtual void Clear() BX_OVERRIDE
{
}
virtual void Append(char* /*s*/, int /*type = -1*/) BX_OVERRIDE
{
}
virtual int Length() BX_OVERRIDE
{
return 0;
}
virtual void Select(int /*n*/) BX_OVERRIDE
{
}
virtual int GetSelection() BX_OVERRIDE
{
return 0;
}
virtual int Find(const char* /*prefix*/) BX_OVERRIDE
{
return 0;
}
virtual void GetValue(int /*n*/, char* value, int /*len*/) BX_OVERRIDE
{
value[0] = '\0';
}
virtual void RegisterImage(int /*type*/, const char* /*xpm_data*/) BX_OVERRIDE
{
}
virtual void RegisterRGBAImage(int /*type*/, int /*width*/, int /*height*/, const unsigned char* /*pixelsImage*/) BX_OVERRIDE
{
}
virtual void ClearRegisteredImages() BX_OVERRIDE
{
}
virtual void SetDoubleClickAction(Scintilla::CallBackAction, void*) BX_OVERRIDE
{
}
virtual void SetList(const char* /*list*/, char /*separator*/, char /*typesep*/) BX_OVERRIDE
{
}
private:
Scintilla::Point m_location;
size_t m_maxStrWidth;
int m_lineHeight;
int m_desiredVisibleRows;
int m_aveCharWidth;
bool m_unicodeMode;
};
2015-06-10 09:56:30 +03:00
struct Editor : public Scintilla::ScintillaBase
2015-06-09 03:07:35 +03:00
{
public:
2015-06-10 09:56:30 +03:00
Editor()
2015-06-09 03:07:35 +03:00
: m_width(0)
, m_height(0)
, m_searchResultIndication(0xff5A5A5A)
, m_filteredSearchResultIndication(0xff5a5a5a)
, m_occurrenceIndication(0xff5a5a5a)
, m_writeOccurrenceIndication(0xff5a5a5a)
, m_findScope(0xffddf0ff)
, m_sourceHoverBackground(0xff000000)
, m_singleLineComment(0xffa8a8a8)
, m_multiLineComment(0xffa8a8a8)
, m_commentTaskTag(0xffa8a8a8)
, m_javadoc(0xffa8a8a8)
, m_javadocLink(0xff548fa0)
, m_javadocTag(0xffa8a8a8)
, m_javadocKeyword(0xffea9c77)
, m_class(0xfff9f9f9)
, m_interface(0xfff9f9f9)
, m_method(0xfff9f9f9)
, m_methodDeclaration(0xfff9f9f9)
, m_bracket(0xfff9f9f9)
, m_number(0xfff9f9f9)
, m_string(0xff76ba53)
, m_operator(0xfff9f9f9)
, m_keyword(0xffea9c77)
, m_annotation(0xffa020f0)
, m_staticMethod(0xfff9f9f9)
, m_localVariable(0xff4b9ce9)
, m_localVariableDeclaration(0xff4b9ce9)
, m_field(0xff4b9ce9)
, m_staticField(0xff4b9ce9)
, m_staticFinalField(0xff4b9ce9)
, m_deprecatedMember(0xfff9f9f9)
2015-06-10 09:56:30 +03:00
, m_foreground(0xffffffff)
, m_lineNumber(0xff00ffff)
2015-06-09 03:07:35 +03:00
{
}
2015-06-10 09:56:30 +03:00
virtual ~Editor()
2015-06-09 03:07:35 +03:00
{
}
virtual void Initialise() BX_OVERRIDE
{
wMain = AllocateWindowInt();
ImGuiIO& io = ImGui::GetIO();
wMain.SetPosition(Scintilla::PRectangle::FromInts(0, 0, int(io.DisplaySize.x), int(io.DisplaySize.y) ) );
view.bufferedDraw = false;
command(SCI_SETLEXER, SCLEX_CPP);
command(SCI_SETSTYLEBITS, 7);
const int fontSize = 15;
const char* fontName = "";
setStyle(STYLE_DEFAULT, m_foreground, m_background, fontSize, fontName);
command(SCI_STYLECLEARALL);
setStyle(STYLE_INDENTGUIDE, 0xffc0c0c0, m_background, fontSize, fontName);
setStyle(STYLE_BRACELIGHT, m_bracket, m_background, fontSize, fontName);
setStyle(STYLE_BRACEBAD, m_bracket, m_background, fontSize, fontName);
setStyle(STYLE_LINENUMBER, m_lineNumber, 0xd0333333, fontSize, fontName);
setStyle(SCE_C_DEFAULT, m_foreground, m_background, fontSize, fontName);
setStyle(SCE_C_STRING, m_string, m_background);
setStyle(SCE_C_IDENTIFIER, m_method, m_background);
setStyle(SCE_C_CHARACTER, m_string, m_background);
setStyle(SCE_C_WORD, m_keyword, m_background);
setStyle(SCE_C_WORD2, m_keyword, m_background);
setStyle(SCE_C_GLOBALCLASS, m_class, m_background);
setStyle(SCE_C_PREPROCESSOR, m_annotation, m_background);
setStyle(SCE_C_NUMBER, m_number, m_background);
setStyle(SCE_C_OPERATOR, m_operator, m_background);
setStyle(SCE_C_COMMENT, m_multiLineComment, m_background);
setStyle(SCE_C_COMMENTLINE, m_singleLineComment, m_background);
setStyle(SCE_C_COMMENTDOC, m_multiLineComment, m_background);
command(SCI_SETSELBACK, 1, m_background.AsLong() );
command(SCI_SETCARETFORE, UINT32_MAX, 0);
command(SCI_SETCARETLINEVISIBLE, 1);
command(SCI_SETCARETLINEBACK, UINT32_MAX);
command(SCI_SETCARETLINEBACKALPHA, 0x20);
command(SCI_SETUSETABS, 1);
command(SCI_SETTABWIDTH, 4);
command(SCI_SETINDENTATIONGUIDES, SC_IV_REAL);
command(SCI_MARKERSETBACK, 0, 0xff6a6a6a);
command(SCI_MARKERSETFORE, 0, 0xff0000ff);
command(SCI_SETMARGINWIDTHN, 0, 44);
command(SCI_SETMARGINTYPEN, 1, SC_MARGIN_SYMBOL);
command(SCI_SETMARGINMASKN, 1, ~SC_MASK_FOLDERS);
command(SCI_RGBAIMAGESETSCALE, 100);
command(SCI_SETMARGINWIDTHN, 1, 0);
command(SCI_MARKERDEFINE, 0, SC_MARK_RGBAIMAGE);
SetFocusState(true);
CaretSetPeriod(0);
}
virtual void CreateCallTipWindow(Scintilla::PRectangle /*_rc*/) BX_OVERRIDE
{
if (!ct.wCallTip.Created() )
{
ct.wCallTip = AllocateWindowInt();
ct.wDraw = ct.wCallTip;
}
}
virtual void AddToPopUp(const char* /*_label*/, int /*_cmd*/, bool /*_enabled*/) BX_OVERRIDE
{
}
void Resize(int /*_x*/, int /*_y*/, int _width, int _height)
{
m_width = _width;
m_height = _height;
wMain.SetPosition(Scintilla::PRectangle::FromInts(0, 0, m_width, m_height) );
}
virtual void SetVerticalScrollPos() BX_OVERRIDE
{
}
virtual void SetHorizontalScrollPos() BX_OVERRIDE
{
xOffset = 0;
}
virtual bool ModifyScrollBars(int /*nMax*/, int /*nPage*/) BX_OVERRIDE
{
return false;
}
void ClaimSelection()
{
}
virtual void Copy() BX_OVERRIDE
{
}
virtual void Paste() BX_OVERRIDE
{
}
virtual void NotifyChange() BX_OVERRIDE
{
}
virtual void NotifyParent(Scintilla::SCNotification /*scn*/) BX_OVERRIDE
{
}
virtual void CopyToClipboard(const Scintilla::SelectionText& /*selectedText*/) BX_OVERRIDE
{
}
virtual void SetMouseCapture(bool /*on*/) BX_OVERRIDE
{
}
virtual bool HaveMouseCapture() BX_OVERRIDE
{
return false;
}
virtual sptr_t DefWndProc(unsigned int /*iMessage*/, uptr_t /*wParam*/, sptr_t /*lParam*/) BX_OVERRIDE
{
return 0;
}
2015-07-15 20:53:37 +03:00
intptr_t command(unsigned int _msg, uintptr_t _p0 = 0, intptr_t _p1 = 0)
2015-06-09 03:07:35 +03:00
{
return WndProc(_msg, _p0, _p1);
}
void draw()
{
ImVec2 cursorPos = ImGui::GetCursorPos();
ImVec2 regionMax = ImGui::GetWindowContentRegionMax();
2015-07-21 08:08:08 +03:00
ImVec2 size = ImVec2( regionMax.x - cursorPos.x - 32
, regionMax.y - cursorPos.y
);
2015-06-09 03:07:35 +03:00
Resize(0, 0, (int)size.x, (int)size.y);
uint8_t modifiers = inputGetModifiersState();
const bool shift = 0 != (modifiers & (entry::Modifier::LeftShift | entry::Modifier::RightShift) );
const bool ctrl = 0 != (modifiers & (entry::Modifier::LeftCtrl | entry::Modifier::RightCtrl ) );
const bool alt = 0 != (modifiers & (entry::Modifier::LeftAlt | entry::Modifier::RightAlt ) );
2015-06-10 23:21:59 +03:00
if (ImGui::IsKeyPressed(entry::Key::Tab) )
2015-06-09 03:07:35 +03:00
{
Editor::KeyDown(SCK_TAB, shift, ctrl, alt);
}
2015-06-10 23:21:59 +03:00
else if (ImGui::IsKeyPressed(entry::Key::Left) )
2015-06-09 03:07:35 +03:00
{
Editor::KeyDown(SCK_LEFT, shift, ctrl, alt);
}
2015-06-10 23:21:59 +03:00
else if (ImGui::IsKeyPressed(entry::Key::Right) )
2015-06-09 03:07:35 +03:00
{
Editor::KeyDown(SCK_RIGHT, shift, ctrl, alt);
}
2015-06-10 23:21:59 +03:00
else if (ImGui::IsKeyPressed(entry::Key::Up) )
2015-06-09 03:07:35 +03:00
{
Editor::KeyDown(SCK_UP, shift, ctrl, alt);
}
2015-06-10 23:21:59 +03:00
else if (ImGui::IsKeyPressed(entry::Key::Down) )
2015-06-09 03:07:35 +03:00
{
Editor::KeyDown(SCK_DOWN, shift, ctrl, alt);
}
2015-07-18 10:30:46 +03:00
else if (ImGui::IsKeyPressed(entry::Key::PageUp) )
{
Editor::KeyDown(SCK_PRIOR, shift, ctrl, alt);
}
else if (ImGui::IsKeyPressed(entry::Key::PageDown) )
{
Editor::KeyDown(SCK_NEXT, shift, ctrl, alt);
}
2015-06-10 23:21:59 +03:00
else if (ImGui::IsKeyPressed(entry::Key::Home) )
2015-06-09 03:07:35 +03:00
{
Editor::KeyDown(SCK_HOME, shift, ctrl, alt);
}
2015-06-10 23:21:59 +03:00
else if (ImGui::IsKeyPressed(entry::Key::End) )
2015-06-09 03:07:35 +03:00
{
Editor::KeyDown(SCK_END, shift, ctrl, alt);
}
2015-06-10 23:21:59 +03:00
else if (ImGui::IsKeyPressed(entry::Key::Delete) )
2015-06-09 03:07:35 +03:00
{
Editor::KeyDown(SCK_DELETE, shift, ctrl, alt);
}
2015-06-10 23:21:59 +03:00
else if (ImGui::IsKeyPressed(entry::Key::Backspace) )
2015-06-09 03:07:35 +03:00
{
Editor::KeyDown(SCK_BACK, shift, ctrl, alt); inputGetChar();
}
2015-06-10 23:21:59 +03:00
else if (ImGui::IsKeyPressed(entry::Key::Return) )
2015-06-09 03:07:35 +03:00
{
Editor::KeyDown(SCK_RETURN, shift, ctrl, alt); inputGetChar();
}
2015-06-10 23:21:59 +03:00
else if (ImGui::IsKeyPressed(entry::Key::Esc) )
2015-06-09 03:07:35 +03:00
{
Editor::KeyDown(SCK_ESCAPE, shift, ctrl, alt);
}
2015-06-10 23:21:59 +03:00
else if (ctrl && ImGui::IsKeyPressed(entry::Key::KeyA) )
2015-06-09 03:07:35 +03:00
{
Editor::KeyDown('A', shift, ctrl, alt); inputGetChar();
}
2015-06-10 23:21:59 +03:00
else if (ctrl && ImGui::IsKeyPressed(entry::Key::KeyC) )
2015-06-09 03:07:35 +03:00
{
Editor::KeyDown('C', shift, ctrl, alt); inputGetChar();
}
2015-06-10 23:21:59 +03:00
else if (ctrl && ImGui::IsKeyPressed(entry::Key::KeyV) )
2015-06-09 03:07:35 +03:00
{
Editor::KeyDown('V', shift, ctrl, alt); inputGetChar();
}
2015-06-10 23:21:59 +03:00
else if (ctrl && ImGui::IsKeyPressed(entry::Key::KeyX) )
2015-06-09 03:07:35 +03:00
{
Editor::KeyDown('X', shift, ctrl, alt); inputGetChar();
}
2015-06-10 23:21:59 +03:00
else if (ctrl && ImGui::IsKeyPressed(entry::Key::KeyY) )
2015-06-09 03:07:35 +03:00
{
Editor::KeyDown('Y', shift, ctrl, alt); inputGetChar();
}
2015-06-10 23:21:59 +03:00
else if (ctrl && ImGui::IsKeyPressed(entry::Key::KeyZ) )
2015-06-09 03:07:35 +03:00
{
Editor::KeyDown('Z', shift, ctrl, alt); inputGetChar();
}
else if (ctrl || alt)
{
// ignore...
}
else
{
for (const uint8_t* ch = inputGetChar(); NULL != ch; ch = inputGetChar() )
{
switch (*ch)
{
case '\b': Editor::KeyDown(SCK_BACK, shift, ctrl, alt); break;
case '\n': Editor::KeyDown(SCK_RETURN, shift, ctrl, alt); break;
default: Editor::AddCharUTF( (const char*)ch, 1); break;
}
}
}
2015-07-21 08:08:08 +03:00
int32_t lineCount = int32_t(command(SCI_GETLINECOUNT) );
int32_t firstVisibleLine = int32_t(command(SCI_GETFIRSTVISIBLELINE) );
float fontHeight = ImGui::GetWindowFontSize();
2015-06-09 03:07:35 +03:00
if (ImGui::IsMouseClicked(0) )
{
ImGuiIO& io = ImGui::GetIO();
Scintilla::Point pt = Scintilla::Point::FromInts( (int)io.MouseClickedPos[0].x, (int)io.MouseClickedPos[0].y);
2015-07-15 19:52:17 +03:00
ButtonDown(pt, (unsigned int)io.MouseDownDuration[0], false, false, false);
2015-06-09 03:07:35 +03:00
}
Tick();
2015-07-21 08:08:08 +03:00
ImGui::BeginGroup();
ImGui::BeginChild("##editor", ImVec2(size.x, size.y-20) );
Scintilla::AutoSurface surfaceWindow(this);
if (surfaceWindow)
{
Paint(surfaceWindow, GetClientRectangle() );
surfaceWindow->Release();
}
ImGui::EndChild();
ImGui::SameLine();
ImGui::BeginChild("##scroll");
ImGuiListClipper clipper;
clipper.Begin(lineCount, fontHeight*2.0f);
if (m_lastFirstVisibleLine != firstVisibleLine)
{
m_lastFirstVisibleLine = firstVisibleLine;
ImGui::SetScrollY(firstVisibleLine * fontHeight*2.0f);
}
else if (firstVisibleLine != clipper.DisplayStart)
{
command(SCI_SETFIRSTVISIBLELINE, clipper.DisplayStart);
}
clipper.End();
ImGui::EndChild();
ImGui::EndGroup();
2015-06-09 03:07:35 +03:00
}
void setStyle(int style, Scintilla::ColourDesired fore, Scintilla::ColourDesired back = UINT32_MAX, int size = -1, const char* face = NULL)
{
command(SCI_STYLESETFORE, uptr_t(style), fore.AsLong() );
command(SCI_STYLESETBACK, uptr_t(style), back.AsLong() );
if (size >= 1)
{
command(SCI_STYLESETSIZE, uptr_t(style), size);
}
if (face)
{
command(SCI_STYLESETFONT, uptr_t(style), reinterpret_cast<sptr_t>(face) );
}
}
private:
int m_width;
int m_height;
2015-07-21 08:08:08 +03:00
int m_lastFirstVisibleLine;
2015-06-09 03:07:35 +03:00
Scintilla::ColourDesired m_searchResultIndication;
Scintilla::ColourDesired m_filteredSearchResultIndication;
Scintilla::ColourDesired m_occurrenceIndication;
Scintilla::ColourDesired m_writeOccurrenceIndication;
Scintilla::ColourDesired m_findScope;
Scintilla::ColourDesired m_sourceHoverBackground;
Scintilla::ColourDesired m_singleLineComment;
Scintilla::ColourDesired m_multiLineComment;
Scintilla::ColourDesired m_commentTaskTag;
Scintilla::ColourDesired m_javadoc;
Scintilla::ColourDesired m_javadocLink;
Scintilla::ColourDesired m_javadocTag;
Scintilla::ColourDesired m_javadocKeyword;
Scintilla::ColourDesired m_class;
Scintilla::ColourDesired m_interface;
Scintilla::ColourDesired m_method;
Scintilla::ColourDesired m_methodDeclaration;
Scintilla::ColourDesired m_bracket;
Scintilla::ColourDesired m_number;
Scintilla::ColourDesired m_string;
Scintilla::ColourDesired m_operator;
Scintilla::ColourDesired m_keyword;
Scintilla::ColourDesired m_annotation;
Scintilla::ColourDesired m_staticMethod;
Scintilla::ColourDesired m_localVariable;
Scintilla::ColourDesired m_localVariableDeclaration;
Scintilla::ColourDesired m_field;
Scintilla::ColourDesired m_staticField;
Scintilla::ColourDesired m_staticFinalField;
Scintilla::ColourDesired m_deprecatedMember;
Scintilla::ColourDesired m_background;
Scintilla::ColourDesired m_currentLine;
Scintilla::ColourDesired m_foreground;
Scintilla::ColourDesired m_lineNumber;
Scintilla::ColourDesired m_selectionBackground;
Scintilla::ColourDesired m_selectionForeground;
};
ScintillaEditor* ScintillaEditor::create(int _width, int _height)
{
2015-06-10 09:56:30 +03:00
Editor* editor = IMGUI_NEW(Editor);
2015-06-09 03:07:35 +03:00
editor->Initialise();
editor->Resize(0, 0, _width, _height);
return reinterpret_cast<ScintillaEditor*>(editor);
}
void ScintillaEditor::destroy(ScintillaEditor* _scintilla)
{
2015-06-10 09:56:30 +03:00
IMGUI_DELETE(Editor, _scintilla);
2015-06-09 03:07:35 +03:00
}
intptr_t ScintillaEditor::command(unsigned int _msg, uintptr_t _p0, intptr_t _p1)
{
2015-06-10 09:56:30 +03:00
Editor* editor = reinterpret_cast<Editor*>(this);
2015-06-09 03:07:35 +03:00
return editor->command(_msg, _p0, _p1);
}
void ScintillaEditor::draw()
{
2015-06-10 09:56:30 +03:00
Editor* editor = reinterpret_cast<Editor*>(this);
2015-06-09 03:07:35 +03:00
return editor->draw();
}
// Scintilla hooks
namespace Scintilla
{
Font::Font()
: fid(0)
{
}
Font::~Font()
{
}
void Font::Create(const FontParameters& fp)
2015-07-15 20:53:37 +03:00
{
2015-06-09 03:07:35 +03:00
FontInt* newFont = (FontInt*)ImGui::MemAlloc(sizeof(FontInt) );
fid = newFont;
newFont->m_font = ImGui::GetIO().Fonts->Fonts[0];
2015-06-09 03:07:35 +03:00
newFont->m_fontSize = fp.size;
newFont->m_scale = fp.size / newFont->m_font->FontSize;
2015-06-09 03:07:35 +03:00
}
void Font::Release()
{
if (fid)
{
ImGui::MemFree( (FontInt*)fid);
}
}
ColourDesired Platform::Chrome()
{
return makeRgba(0xe0, 0xe0, 0xe0);
}
ColourDesired Platform::ChromeHighlight()
{
return makeRgba(0xff, 0xff, 0xff);
}
const char* Platform::DefaultFont()
{
return "";
}
int Platform::DefaultFontSize()
{
return 15;
}
unsigned int Platform::DoubleClickTime()
{
return 500;
}
bool Platform::MouseButtonBounce()
{
return true;
}
void Platform::Assert(const char* _error, const char* _filename, int _line)
{
DebugPrintf("%s(%d): %s", _filename, _line, _error);
}
int Platform::Minimum(int a, int b)
{
return (int)bx::uint32_imin(a, b);
}
int Platform::Maximum(int a, int b)
{
return (int)bx::uint32_imax(a, b);
}
int Platform::Clamp(int val, int minVal, int maxVal)
{
return (int)bx::uint32_iclamp(val, minVal, maxVal);
}
void Platform::DebugPrintf(const char* _format, ...)
{
char temp[8192];
char* out = temp;
va_list argList;
va_start(argList, _format);
int32_t len = bx::vsnprintf(out, sizeof(temp), _format, argList);
if ( (int32_t)sizeof(temp) < len)
{
out = (char*)alloca(len+1);
len = bx::vsnprintf(out, len, _format, argList);
}
va_end(argList);
out[len] = '\0';
bx::debugOutput(out);
}
Menu::Menu()
: mid(0)
{
}
void Menu::CreatePopUp()
{
Destroy();
mid = MenuID(1);
}
void Menu::Destroy()
{
mid = 0;
}
void Menu::Show(Point /*_pt*/, Window& /*_w*/)
{
Destroy();
}
Surface* Surface::Allocate(int)
{
return IMGUI_NEW(SurfaceInt);
}
Window::~Window()
{
}
void Window::Destroy()
{
if (wid)
{
Show(false);
WindowInt* wi = GetWindow(wid);
IMGUI_DELETE(WindowInt, wi);
}
wid = 0;
}
bool Window::HasFocus()
{
return true;
}
PRectangle Window::GetPosition()
{
if (0 == wid)
{
return PRectangle();
}
return GetWindow(wid)->position;
}
void Window::SetPosition(PRectangle rc)
{
GetWindow(wid)->position = rc;
}
2015-06-10 09:56:30 +03:00
void Window::SetPositionRelative(PRectangle _rc, Window /*_w*/)
2015-06-09 03:07:35 +03:00
{
2015-06-10 09:56:30 +03:00
SetPosition(_rc);
2015-06-09 03:07:35 +03:00
}
PRectangle Window::GetClientPosition()
{
if (0 == wid)
{
return PRectangle();
}
return GetWindow(wid)->position;
}
void Window::Show(bool _show)
{
if (0 != wid)
{
GetWindow(wid)->m_show = _show;
}
}
void Window::InvalidateAll()
{
}
void Window::InvalidateRectangle(PRectangle /*_rc*/)
{
}
void Window::SetFont(Font& /*_font*/)
{
}
void Window::SetCursor(Cursor /*_curs*/)
{
cursorLast = cursorText;
}
void Window::SetTitle(const char* /*_str*/)
{
}
PRectangle Window::GetMonitorRect(Point /*_pt*/)
{
return PRectangle();
}
ListBox::ListBox()
{
}
ListBox::~ListBox()
{
}
ListBox* ListBox::Allocate()
{
return IMGUI_NEW(ListBoxInt);
}
} // namespace Scintilla
2015-11-12 03:43:32 +03:00
ScintillaEditor* ImGuiScintilla(const char* _name, bool* /*_opened*/, const ImVec2& /*_size*/)
2015-07-15 20:53:37 +03:00
{
ScintillaEditor* sci = NULL;
2015-10-09 08:46:17 +03:00
// if (ImGui::Begin(_name, _opened, _size) )
2015-07-15 20:53:37 +03:00
{
ImGuiStorage* storage = ImGui::GetStateStorage();
ImGuiID id = ImGui::GetID(_name);
sci = (ScintillaEditor*)storage->GetVoidPtr(id);
if (NULL == sci)
{
ImVec2 size = ImGui::GetWindowSize();
sci = ScintillaEditor::create(size.x, size.y);
storage->SetVoidPtr(id, (void*)sci);
}
sci->draw();
}
2015-10-09 08:46:17 +03:00
// ImGui::End();
2015-07-15 20:53:37 +03:00
return sci;
}
2015-06-09 03:07:35 +03:00
#endif // defined(SCI_NAMESPACE)