scintilla.cpp 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086
  1. /*
  2. * Copyright 2011-2015 Branimir Karadzic. All rights reserved.
  3. * License: https://github.com/bkaradzic/bgfx/blob/master/LICENSE
  4. */
  5. #if defined(SCI_NAMESPACE)
  6. #include <algorithm>
  7. #include <map>
  8. #include <vector>
  9. #include "scintilla/include/Platform.h"
  10. #include "scintilla/include/Scintilla.h"
  11. #include "scintilla/include/ILexer.h"
  12. #include "scintilla/include/SciLexer.h"
  13. #include "scintilla/lexlib/LexerModule.h"
  14. #include "scintilla/src/SplitVector.h"
  15. #include "scintilla/src/Partitioning.h"
  16. #include "scintilla/src/RunStyles.h"
  17. #include "scintilla/src/Catalogue.h"
  18. #include "scintilla/src/ContractionState.h"
  19. #include "scintilla/src/CellBuffer.h"
  20. #include "scintilla/src/KeyMap.h"
  21. #include "scintilla/src/Indicator.h"
  22. #include "scintilla/src/XPM.h"
  23. #include "scintilla/src/LineMarker.h"
  24. #include "scintilla/src/Style.h"
  25. #include "scintilla/src/ViewStyle.h"
  26. #include "scintilla/src/Decoration.h"
  27. #include "scintilla/src/CharClassify.h"
  28. #include "scintilla/src/CaseFolder.h"
  29. #include "scintilla/src/Document.h"
  30. #include "scintilla/src/Selection.h"
  31. #include "scintilla/src/PositionCache.h"
  32. #include "scintilla/src/EditModel.h"
  33. #include "scintilla/src/MarginView.h"
  34. #include "scintilla/src/EditView.h"
  35. #include "scintilla/src/Editor.h"
  36. #include "scintilla/src/AutoComplete.h"
  37. #include "scintilla/src/CallTip.h"
  38. #include "scintilla/src/ScintillaBase.h"
  39. #include <bx/debug.h>
  40. #include <bx/macros.h>
  41. #include <bx/string.h>
  42. #include <bx/uint32_t.h>
  43. #define STBTT_DEF extern
  44. #include <stb/stb_truetype.h>
  45. #include "imgui.h"
  46. #include "../bgfx_utils.h"
  47. #include "scintilla.h"
  48. #include "../entry/input.h"
  49. #define IMGUI_NEW(type) new (ImGui::MemAlloc(sizeof(type) ) ) type
  50. #define IMGUI_DELETE(type, obj) reinterpret_cast<type*>(obj)->~type(), ImGui::MemFree(obj)
  51. const stbtt_fontinfo& getFontInfo(int index);
  52. static void fillRectangle(Scintilla::PRectangle _rc, Scintilla::ColourDesired _color)
  53. {
  54. const uint32_t abgr = (uint32_t)_color.AsLong();
  55. ImVec2 pos = ImGui::GetCursorScreenPos();
  56. ImDrawList* drawList = ImGui::GetWindowDrawList();
  57. drawList->AddDrawCmd();
  58. drawList->AddRectFilled(
  59. ImVec2(_rc.left + pos.x, _rc.top + pos.y)
  60. , ImVec2(_rc.right + pos.x, _rc.bottom + pos.y)
  61. , abgr
  62. );
  63. }
  64. static inline uint32_t makeRgba(uint32_t r, uint32_t g, uint32_t b, uint32_t a = 0xFF)
  65. {
  66. return a << 24 | b << 16 | g << 8 | r;
  67. }
  68. struct FontInt
  69. {
  70. stbtt_fontinfo m_fontInfo;
  71. float m_scale;
  72. float m_fontSize;
  73. };
  74. class SurfaceInt : public Scintilla::Surface
  75. {
  76. public:
  77. SurfaceInt()
  78. {
  79. }
  80. virtual ~SurfaceInt()
  81. {
  82. }
  83. virtual void Init(Scintilla::WindowID /*_wid*/) BX_OVERRIDE
  84. {
  85. }
  86. virtual void Init(Scintilla::SurfaceID /*_sid*/, Scintilla::WindowID /*_wid*/) BX_OVERRIDE
  87. {
  88. }
  89. virtual void InitPixMap(int /*_width*/, int /*_height*/, Scintilla::Surface* /*_surface*/, Scintilla::WindowID /*_wid*/) BX_OVERRIDE
  90. {
  91. }
  92. virtual void Release() BX_OVERRIDE
  93. {
  94. }
  95. virtual bool Initialised() BX_OVERRIDE
  96. {
  97. return true;
  98. }
  99. virtual void PenColour(Scintilla::ColourDesired /*_fore*/) BX_OVERRIDE
  100. {
  101. }
  102. virtual int LogPixelsY() BX_OVERRIDE
  103. {
  104. return 72;
  105. }
  106. virtual int DeviceHeightFont(int /*points*/) BX_OVERRIDE
  107. {
  108. return 1500;
  109. }
  110. virtual void MoveTo(int _x, int _y) BX_OVERRIDE
  111. {
  112. BX_UNUSED(_x, _y);
  113. }
  114. virtual void LineTo(int _x, int _y) BX_OVERRIDE
  115. {
  116. BX_UNUSED(_x, _y);
  117. }
  118. virtual void Polygon(Scintilla::Point *pts, int npts, Scintilla::ColourDesired fore, Scintilla::ColourDesired back) BX_OVERRIDE
  119. {
  120. BX_UNUSED(pts, npts, fore, back);
  121. }
  122. virtual void RectangleDraw(Scintilla::PRectangle rc, Scintilla::ColourDesired fore, Scintilla::ColourDesired back) BX_OVERRIDE
  123. {
  124. BX_UNUSED(fore);
  125. FillRectangle(rc, back);
  126. }
  127. virtual void FillRectangle(Scintilla::PRectangle rc, Scintilla::ColourDesired back) BX_OVERRIDE
  128. {
  129. fillRectangle(rc, back);
  130. }
  131. virtual void FillRectangle(Scintilla::PRectangle rc, Scintilla::Surface& surfacePattern) BX_OVERRIDE
  132. {
  133. BX_UNUSED(rc, surfacePattern);
  134. }
  135. virtual void RoundedRectangle(Scintilla::PRectangle rc, Scintilla::ColourDesired fore, Scintilla::ColourDesired back) BX_OVERRIDE
  136. {
  137. BX_UNUSED(rc, fore, back);
  138. }
  139. virtual void AlphaRectangle(Scintilla::PRectangle _rc, int /*_cornerSize*/, Scintilla::ColourDesired _fill, int _alphaFill, Scintilla::ColourDesired /*_outline*/, int /*_alphaOutline*/, int /*_flags*/) BX_OVERRIDE
  140. {
  141. unsigned int back = 0
  142. | (uint32_t)( (_fill.AsLong() & 0xffffff)
  143. | ( (_alphaFill & 0xff) << 24) )
  144. ;
  145. FillRectangle(_rc, Scintilla::ColourDesired(back) );
  146. }
  147. virtual void DrawRGBAImage(Scintilla::PRectangle /*_rc*/, int /*_width*/, int /*_height*/, const unsigned char* /*_pixelsImage*/) BX_OVERRIDE
  148. {
  149. }
  150. virtual void Ellipse(Scintilla::PRectangle rc, Scintilla::ColourDesired fore, Scintilla::ColourDesired /*back*/) BX_OVERRIDE
  151. {
  152. FillRectangle(rc, fore);
  153. }
  154. virtual void Copy(Scintilla::PRectangle /*_rc*/, Scintilla::Point /*_from*/, Scintilla::Surface& /*_surfaceSource*/) BX_OVERRIDE
  155. {
  156. }
  157. 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
  158. {
  159. BX_UNUSED(back);
  160. DrawTextBase(rc, _font, ybase, s, len, fore);
  161. }
  162. 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
  163. {
  164. BX_UNUSED(back);
  165. DrawTextBase(rc, _font, ybase, s, len, fore);
  166. }
  167. virtual void DrawTextTransparent(Scintilla::PRectangle rc, Scintilla::Font& _font, Scintilla::XYPOSITION ybase, const char *s, int len, Scintilla::ColourDesired fore) BX_OVERRIDE
  168. {
  169. DrawTextBase(rc, _font, ybase, s, len, fore);
  170. }
  171. virtual void MeasureWidths(Scintilla::Font& /*_font*/, const char* _str, int _len, Scintilla::XYPOSITION* _positions) BX_OVERRIDE
  172. {
  173. float position = 0;
  174. ImFont* imFont = ImGui::GetWindowFont();
  175. while (_len--)
  176. {
  177. int advance;
  178. const ImFont::Glyph* glyph = imFont->FindGlyph( (unsigned short)*_str++);
  179. advance = glyph->XAdvance;
  180. position += advance;
  181. *_positions++ = position;
  182. }
  183. }
  184. virtual Scintilla::XYPOSITION WidthText(Scintilla::Font& /*_font*/, const char* _str, int _len) BX_OVERRIDE
  185. {
  186. ImVec2 t = ImGui::CalcTextSize(_str, _str + _len);
  187. return t.x;
  188. }
  189. virtual Scintilla::XYPOSITION WidthChar(Scintilla::Font& _font, char ch) BX_OVERRIDE
  190. {
  191. FontInt* fi = (FontInt*)_font.GetID();
  192. int advance, leftBearing;
  193. stbtt_GetCodepointHMetrics(&fi->m_fontInfo, ch, &advance, &leftBearing);
  194. return advance * fi->m_scale;
  195. }
  196. virtual Scintilla::XYPOSITION Ascent(Scintilla::Font& _font) BX_OVERRIDE
  197. {
  198. FontInt* fi = (FontInt*)_font.GetID();
  199. int ascent, descent, lineGap;
  200. stbtt_GetFontVMetrics(&fi->m_fontInfo, &ascent, &descent, &lineGap);
  201. return ascent * fi->m_scale;
  202. }
  203. virtual Scintilla::XYPOSITION Descent(Scintilla::Font& _font) BX_OVERRIDE
  204. {
  205. int ascent, descent, lineGap;
  206. FontInt* fi = (FontInt*)_font.GetID();
  207. stbtt_GetFontVMetrics(&fi->m_fontInfo, &ascent, &descent, &lineGap);
  208. return -descent * fi->m_scale;
  209. }
  210. virtual Scintilla::XYPOSITION InternalLeading(Scintilla::Font& /*_font*/) BX_OVERRIDE
  211. {
  212. return 0;
  213. }
  214. virtual Scintilla::XYPOSITION ExternalLeading(Scintilla::Font& _font) BX_OVERRIDE
  215. {
  216. FontInt* fi = (FontInt*)_font.GetID();
  217. int ascent, descent, lineGap;
  218. stbtt_GetFontVMetrics(&fi->m_fontInfo, &ascent, &descent, &lineGap);
  219. return lineGap * fi->m_scale;
  220. }
  221. virtual Scintilla::XYPOSITION Height(Scintilla::Font& _font) BX_OVERRIDE
  222. {
  223. return Ascent(_font) + Descent(_font);
  224. }
  225. virtual Scintilla::XYPOSITION AverageCharWidth(Scintilla::Font& _font) BX_OVERRIDE
  226. {
  227. return WidthChar(_font, 'n');
  228. }
  229. virtual void SetClip(Scintilla::PRectangle /*_rc*/) BX_OVERRIDE
  230. {
  231. }
  232. virtual void FlushCachedState() BX_OVERRIDE
  233. {
  234. }
  235. virtual void SetUnicodeMode(bool /*_unicodeMode*/) BX_OVERRIDE
  236. {
  237. }
  238. virtual void SetDBCSMode(int /*_codePage*/) BX_OVERRIDE
  239. {
  240. }
  241. private:
  242. void DrawTextBase(Scintilla::PRectangle _rc, Scintilla::Font& _font, float _ybase, const char* _str, int _len, Scintilla::ColourDesired _fore)
  243. {
  244. float xt = _rc.left;
  245. float yt = _ybase;
  246. uint32_t fore = (uint32_t)_fore.AsLong();
  247. FontInt* fi = (FontInt*)_font.GetID();
  248. ImVec2 pos = ImGui::GetCursorScreenPos();
  249. ImFont* imFont = ImGui::GetWindowFont();
  250. ImDrawList* drawList = ImGui::GetWindowDrawList();
  251. drawList->AddText(imFont
  252. , fi->m_fontSize
  253. , ImVec2(xt + pos.x, yt + pos.y - fi->m_fontSize)
  254. , fore
  255. , _str
  256. , _str + _len
  257. );
  258. }
  259. Scintilla::ColourDesired m_penColour;
  260. };
  261. struct WindowInt
  262. {
  263. WindowInt()
  264. : m_show(false)
  265. {
  266. }
  267. Scintilla::PRectangle position;
  268. bool m_show;
  269. };
  270. WindowInt* AllocateWindowInt()
  271. {
  272. return new WindowInt;
  273. }
  274. inline WindowInt* GetWindow(Scintilla::WindowID id)
  275. {
  276. return (WindowInt*)id;
  277. }
  278. class ListBoxInt : public Scintilla::ListBox
  279. {
  280. public:
  281. ListBoxInt::ListBoxInt()
  282. : m_lineHeight(10)
  283. , m_unicodeMode(false)
  284. , m_desiredVisibleRows(5)
  285. , m_aveCharWidth(8)
  286. , m_maxStrWidth(0)
  287. {
  288. }
  289. ListBoxInt::~ListBoxInt()
  290. {
  291. }
  292. virtual void SetFont(Scintilla::Font& /*_font*/) BX_OVERRIDE
  293. {
  294. }
  295. virtual void Create(Scintilla::Window& /*_parent*/, int /*_ctrlID*/, Scintilla::Point _location, int _lineHeight, bool _unicodeMode, int /*_technology*/) BX_OVERRIDE
  296. {
  297. m_location = _location;
  298. m_lineHeight = _lineHeight;
  299. m_unicodeMode = _unicodeMode;
  300. m_maxStrWidth = 0;
  301. wid = Scintilla::WindowID(4);
  302. }
  303. virtual void SetAverageCharWidth(int width) BX_OVERRIDE
  304. {
  305. m_aveCharWidth = width;
  306. }
  307. virtual void SetVisibleRows(int rows) BX_OVERRIDE
  308. {
  309. m_desiredVisibleRows = rows;
  310. }
  311. virtual int GetVisibleRows() const BX_OVERRIDE
  312. {
  313. return m_desiredVisibleRows;
  314. }
  315. virtual Scintilla::PRectangle GetDesiredRect() BX_OVERRIDE
  316. {
  317. Scintilla::PRectangle rc;
  318. rc.top = 0;
  319. rc.left = 0;
  320. rc.right = 350;
  321. rc.bottom = 140;
  322. return rc;
  323. }
  324. virtual int CaretFromEdge() BX_OVERRIDE
  325. {
  326. return 4 + 16;
  327. }
  328. virtual void Clear() BX_OVERRIDE
  329. {
  330. }
  331. virtual void Append(char* /*s*/, int /*type = -1*/) BX_OVERRIDE
  332. {
  333. }
  334. virtual int Length() BX_OVERRIDE
  335. {
  336. return 0;
  337. }
  338. virtual void Select(int /*n*/) BX_OVERRIDE
  339. {
  340. }
  341. virtual int GetSelection() BX_OVERRIDE
  342. {
  343. return 0;
  344. }
  345. virtual int Find(const char* /*prefix*/) BX_OVERRIDE
  346. {
  347. return 0;
  348. }
  349. virtual void GetValue(int /*n*/, char* value, int /*len*/) BX_OVERRIDE
  350. {
  351. value[0] = '\0';
  352. }
  353. virtual void RegisterImage(int /*type*/, const char* /*xpm_data*/) BX_OVERRIDE
  354. {
  355. }
  356. virtual void RegisterRGBAImage(int /*type*/, int /*width*/, int /*height*/, const unsigned char* /*pixelsImage*/) BX_OVERRIDE
  357. {
  358. }
  359. virtual void ClearRegisteredImages() BX_OVERRIDE
  360. {
  361. }
  362. virtual void SetDoubleClickAction(Scintilla::CallBackAction, void*) BX_OVERRIDE
  363. {
  364. }
  365. virtual void SetList(const char* /*list*/, char /*separator*/, char /*typesep*/) BX_OVERRIDE
  366. {
  367. }
  368. private:
  369. Scintilla::Point m_location;
  370. size_t m_maxStrWidth;
  371. int m_lineHeight;
  372. int m_desiredVisibleRows;
  373. int m_aveCharWidth;
  374. bool m_unicodeMode;
  375. };
  376. struct ScEditor : public Scintilla::ScintillaBase
  377. {
  378. public:
  379. ScEditor()
  380. : m_width(0)
  381. , m_height(0)
  382. , m_wheelVRotation(0)
  383. , m_wheelHRotation(0)
  384. , m_foreground(0xffffffff)
  385. , m_lineNumber(0xff00ffff)
  386. , m_searchResultIndication(0xff5A5A5A)
  387. , m_filteredSearchResultIndication(0xff5a5a5a)
  388. , m_occurrenceIndication(0xff5a5a5a)
  389. , m_writeOccurrenceIndication(0xff5a5a5a)
  390. , m_findScope(0xffddf0ff)
  391. , m_sourceHoverBackground(0xff000000)
  392. , m_singleLineComment(0xffa8a8a8)
  393. , m_multiLineComment(0xffa8a8a8)
  394. , m_commentTaskTag(0xffa8a8a8)
  395. , m_javadoc(0xffa8a8a8)
  396. , m_javadocLink(0xff548fa0)
  397. , m_javadocTag(0xffa8a8a8)
  398. , m_javadocKeyword(0xffea9c77)
  399. , m_class(0xfff9f9f9)
  400. , m_interface(0xfff9f9f9)
  401. , m_method(0xfff9f9f9)
  402. , m_methodDeclaration(0xfff9f9f9)
  403. , m_bracket(0xfff9f9f9)
  404. , m_number(0xfff9f9f9)
  405. , m_string(0xff76ba53)
  406. , m_operator(0xfff9f9f9)
  407. , m_keyword(0xffea9c77)
  408. , m_annotation(0xffa020f0)
  409. , m_staticMethod(0xfff9f9f9)
  410. , m_localVariable(0xff4b9ce9)
  411. , m_localVariableDeclaration(0xff4b9ce9)
  412. , m_field(0xff4b9ce9)
  413. , m_staticField(0xff4b9ce9)
  414. , m_staticFinalField(0xff4b9ce9)
  415. , m_deprecatedMember(0xfff9f9f9)
  416. {
  417. }
  418. virtual ~ScEditor()
  419. {
  420. }
  421. virtual void Initialise() BX_OVERRIDE
  422. {
  423. wMain = AllocateWindowInt();
  424. ImGuiIO& io = ImGui::GetIO();
  425. wMain.SetPosition(Scintilla::PRectangle::FromInts(0, 0, int(io.DisplaySize.x), int(io.DisplaySize.y) ) );
  426. view.bufferedDraw = false;
  427. command(SCI_SETLEXER, SCLEX_CPP);
  428. command(SCI_SETSTYLEBITS, 7);
  429. const int fontSize = 15;
  430. const char* fontName = "";
  431. setStyle(STYLE_DEFAULT, m_foreground, m_background, fontSize, fontName);
  432. command(SCI_STYLECLEARALL);
  433. setStyle(STYLE_INDENTGUIDE, 0xffc0c0c0, m_background, fontSize, fontName);
  434. setStyle(STYLE_BRACELIGHT, m_bracket, m_background, fontSize, fontName);
  435. setStyle(STYLE_BRACEBAD, m_bracket, m_background, fontSize, fontName);
  436. setStyle(STYLE_LINENUMBER, m_lineNumber, 0xd0333333, fontSize, fontName);
  437. setStyle(SCE_C_DEFAULT, m_foreground, m_background, fontSize, fontName);
  438. setStyle(SCE_C_STRING, m_string, m_background);
  439. setStyle(SCE_C_IDENTIFIER, m_method, m_background);
  440. setStyle(SCE_C_CHARACTER, m_string, m_background);
  441. setStyle(SCE_C_WORD, m_keyword, m_background);
  442. setStyle(SCE_C_WORD2, m_keyword, m_background);
  443. setStyle(SCE_C_GLOBALCLASS, m_class, m_background);
  444. setStyle(SCE_C_PREPROCESSOR, m_annotation, m_background);
  445. setStyle(SCE_C_NUMBER, m_number, m_background);
  446. setStyle(SCE_C_OPERATOR, m_operator, m_background);
  447. setStyle(SCE_C_COMMENT, m_multiLineComment, m_background);
  448. setStyle(SCE_C_COMMENTLINE, m_singleLineComment, m_background);
  449. setStyle(SCE_C_COMMENTDOC, m_multiLineComment, m_background);
  450. command(SCI_SETSELBACK, 1, m_background.AsLong() );
  451. command(SCI_SETCARETFORE, UINT32_MAX, 0);
  452. command(SCI_SETCARETLINEVISIBLE, 1);
  453. command(SCI_SETCARETLINEBACK, UINT32_MAX);
  454. command(SCI_SETCARETLINEBACKALPHA, 0x20);
  455. command(SCI_SETUSETABS, 1);
  456. command(SCI_SETTABWIDTH, 4);
  457. command(SCI_SETINDENTATIONGUIDES, SC_IV_REAL);
  458. command(SCI_MARKERSETBACK, 0, 0xff6a6a6a);
  459. command(SCI_MARKERSETFORE, 0, 0xff0000ff);
  460. command(SCI_SETMARGINWIDTHN, 0, 44);
  461. command(SCI_SETMARGINTYPEN, 1, SC_MARGIN_SYMBOL);
  462. command(SCI_SETMARGINMASKN, 1, ~SC_MASK_FOLDERS);
  463. command(SCI_RGBAIMAGESETSCALE, 100);
  464. command(SCI_SETMARGINWIDTHN, 1, 0);
  465. command(SCI_MARKERDEFINE, 0, SC_MARK_RGBAIMAGE);
  466. SetFocusState(true);
  467. CaretSetPeriod(0);
  468. }
  469. virtual void CreateCallTipWindow(Scintilla::PRectangle /*_rc*/) BX_OVERRIDE
  470. {
  471. if (!ct.wCallTip.Created() )
  472. {
  473. ct.wCallTip = AllocateWindowInt();
  474. ct.wDraw = ct.wCallTip;
  475. }
  476. }
  477. virtual void AddToPopUp(const char* /*_label*/, int /*_cmd*/, bool /*_enabled*/) BX_OVERRIDE
  478. {
  479. }
  480. void Resize(int /*_x*/, int /*_y*/, int _width, int _height)
  481. {
  482. m_width = _width;
  483. m_height = _height;
  484. wMain.SetPosition(Scintilla::PRectangle::FromInts(0, 0, m_width, m_height) );
  485. }
  486. virtual void SetVerticalScrollPos() BX_OVERRIDE
  487. {
  488. }
  489. virtual void SetHorizontalScrollPos() BX_OVERRIDE
  490. {
  491. xOffset = 0;
  492. }
  493. virtual bool ModifyScrollBars(int /*nMax*/, int /*nPage*/) BX_OVERRIDE
  494. {
  495. return false;
  496. }
  497. void ClaimSelection()
  498. {
  499. }
  500. virtual void Copy() BX_OVERRIDE
  501. {
  502. }
  503. virtual void Paste() BX_OVERRIDE
  504. {
  505. }
  506. virtual void NotifyChange() BX_OVERRIDE
  507. {
  508. }
  509. virtual void NotifyParent(Scintilla::SCNotification /*scn*/) BX_OVERRIDE
  510. {
  511. }
  512. virtual void CopyToClipboard(const Scintilla::SelectionText& /*selectedText*/) BX_OVERRIDE
  513. {
  514. }
  515. virtual void SetMouseCapture(bool /*on*/) BX_OVERRIDE
  516. {
  517. }
  518. virtual bool HaveMouseCapture() BX_OVERRIDE
  519. {
  520. return false;
  521. }
  522. virtual sptr_t DefWndProc(unsigned int /*iMessage*/, uptr_t /*wParam*/, sptr_t /*lParam*/) BX_OVERRIDE
  523. {
  524. return 0;
  525. }
  526. intptr_t command(unsigned int _msg, uintptr_t _p0 = 0, intptr_t _p1 = 0)
  527. {
  528. return WndProc(_msg, _p0, _p1);
  529. }
  530. void draw()
  531. {
  532. ImVec2 cursorPos = ImGui::GetCursorPos();
  533. ImVec2 regionMax = ImGui::GetWindowContentRegionMax();
  534. ImVec2 size = ImVec2(regionMax.x - cursorPos.x, regionMax.y - cursorPos.y);
  535. Resize(0, 0, (int)size.x, (int)size.y);
  536. uint8_t modifiers = inputGetModifiersState();
  537. const bool shift = 0 != (modifiers & (entry::Modifier::LeftShift | entry::Modifier::RightShift) );
  538. const bool ctrl = 0 != (modifiers & (entry::Modifier::LeftCtrl | entry::Modifier::RightCtrl ) );
  539. const bool alt = 0 != (modifiers & (entry::Modifier::LeftAlt | entry::Modifier::RightAlt ) );
  540. if (ImGui::IsKeyPressed(ImGuiKey_Tab) )
  541. {
  542. Editor::KeyDown(SCK_TAB, shift, ctrl, alt);
  543. }
  544. else if (ImGui::IsKeyPressed(ImGuiKey_LeftArrow) )
  545. {
  546. Editor::KeyDown(SCK_LEFT, shift, ctrl, alt);
  547. }
  548. else if (ImGui::IsKeyPressed(ImGuiKey_RightArrow) )
  549. {
  550. Editor::KeyDown(SCK_RIGHT, shift, ctrl, alt);
  551. }
  552. else if (ImGui::IsKeyPressed(ImGuiKey_UpArrow) )
  553. {
  554. Editor::KeyDown(SCK_UP, shift, ctrl, alt);
  555. }
  556. else if (ImGui::IsKeyPressed(ImGuiKey_DownArrow) )
  557. {
  558. Editor::KeyDown(SCK_DOWN, shift, ctrl, alt);
  559. }
  560. else if (ImGui::IsKeyPressed(ImGuiKey_Home) )
  561. {
  562. Editor::KeyDown(SCK_HOME, shift, ctrl, alt);
  563. }
  564. else if (ImGui::IsKeyPressed(ImGuiKey_End) )
  565. {
  566. Editor::KeyDown(SCK_END, shift, ctrl, alt);
  567. }
  568. else if (ImGui::IsKeyPressed(ImGuiKey_Delete) )
  569. {
  570. Editor::KeyDown(SCK_DELETE, shift, ctrl, alt);
  571. }
  572. else if (ImGui::IsKeyPressed(ImGuiKey_Backspace) )
  573. {
  574. Editor::KeyDown(SCK_BACK, shift, ctrl, alt); inputGetChar();
  575. }
  576. else if (ImGui::IsKeyPressed(ImGuiKey_Enter) )
  577. {
  578. Editor::KeyDown(SCK_RETURN, shift, ctrl, alt); inputGetChar();
  579. }
  580. else if (ImGui::IsKeyPressed(ImGuiKey_Escape) )
  581. {
  582. Editor::KeyDown(SCK_ESCAPE, shift, ctrl, alt);
  583. }
  584. else if (ctrl && ImGui::IsKeyPressed(ImGuiKey_A) )
  585. {
  586. Editor::KeyDown('A', shift, ctrl, alt); inputGetChar();
  587. }
  588. else if (ctrl && ImGui::IsKeyPressed(ImGuiKey_C) )
  589. {
  590. Editor::KeyDown('C', shift, ctrl, alt); inputGetChar();
  591. }
  592. else if (ctrl && ImGui::IsKeyPressed(ImGuiKey_V) )
  593. {
  594. Editor::KeyDown('V', shift, ctrl, alt); inputGetChar();
  595. }
  596. else if (ctrl && ImGui::IsKeyPressed(ImGuiKey_X) )
  597. {
  598. Editor::KeyDown('X', shift, ctrl, alt); inputGetChar();
  599. }
  600. else if (ctrl && ImGui::IsKeyPressed(ImGuiKey_Y) )
  601. {
  602. Editor::KeyDown('Y', shift, ctrl, alt); inputGetChar();
  603. }
  604. else if (ctrl && ImGui::IsKeyPressed(ImGuiKey_Z) )
  605. {
  606. Editor::KeyDown('Z', shift, ctrl, alt); inputGetChar();
  607. }
  608. else if (ctrl || alt)
  609. {
  610. // ignore...
  611. }
  612. else
  613. {
  614. for (const uint8_t* ch = inputGetChar(); NULL != ch; ch = inputGetChar() )
  615. {
  616. switch (*ch)
  617. {
  618. case '\b': Editor::KeyDown(SCK_BACK, shift, ctrl, alt); break;
  619. case '\n': Editor::KeyDown(SCK_RETURN, shift, ctrl, alt); break;
  620. default: Editor::AddCharUTF( (const char*)ch, 1); break;
  621. }
  622. }
  623. }
  624. if (ImGui::IsMouseClicked(0) )
  625. {
  626. ImGuiIO& io = ImGui::GetIO();
  627. Scintilla::Point pt = Scintilla::Point::FromInts( (int)io.MouseClickedPos[0].x, (int)io.MouseClickedPos[0].y);
  628. ButtonDown(pt, (unsigned int)io.MouseDownTime[0], false, false, false);
  629. }
  630. Tick();
  631. Scintilla::AutoSurface surfaceWindow(this);
  632. if (surfaceWindow)
  633. {
  634. Paint(surfaceWindow, GetClientRectangle() );
  635. surfaceWindow->Release();
  636. }
  637. }
  638. void setStyle(int style, Scintilla::ColourDesired fore, Scintilla::ColourDesired back = UINT32_MAX, int size = -1, const char* face = NULL)
  639. {
  640. command(SCI_STYLESETFORE, uptr_t(style), fore.AsLong() );
  641. command(SCI_STYLESETBACK, uptr_t(style), back.AsLong() );
  642. if (size >= 1)
  643. {
  644. command(SCI_STYLESETSIZE, uptr_t(style), size);
  645. }
  646. if (face)
  647. {
  648. command(SCI_STYLESETFONT, uptr_t(style), reinterpret_cast<sptr_t>(face) );
  649. }
  650. }
  651. private:
  652. int m_width;
  653. int m_height;
  654. int m_wheelVRotation;
  655. int m_wheelHRotation;
  656. Scintilla::ColourDesired m_searchResultIndication;
  657. Scintilla::ColourDesired m_filteredSearchResultIndication;
  658. Scintilla::ColourDesired m_occurrenceIndication;
  659. Scintilla::ColourDesired m_writeOccurrenceIndication;
  660. Scintilla::ColourDesired m_findScope;
  661. Scintilla::ColourDesired m_sourceHoverBackground;
  662. Scintilla::ColourDesired m_singleLineComment;
  663. Scintilla::ColourDesired m_multiLineComment;
  664. Scintilla::ColourDesired m_commentTaskTag;
  665. Scintilla::ColourDesired m_javadoc;
  666. Scintilla::ColourDesired m_javadocLink;
  667. Scintilla::ColourDesired m_javadocTag;
  668. Scintilla::ColourDesired m_javadocKeyword;
  669. Scintilla::ColourDesired m_class;
  670. Scintilla::ColourDesired m_interface;
  671. Scintilla::ColourDesired m_method;
  672. Scintilla::ColourDesired m_methodDeclaration;
  673. Scintilla::ColourDesired m_bracket;
  674. Scintilla::ColourDesired m_number;
  675. Scintilla::ColourDesired m_string;
  676. Scintilla::ColourDesired m_operator;
  677. Scintilla::ColourDesired m_keyword;
  678. Scintilla::ColourDesired m_annotation;
  679. Scintilla::ColourDesired m_staticMethod;
  680. Scintilla::ColourDesired m_localVariable;
  681. Scintilla::ColourDesired m_localVariableDeclaration;
  682. Scintilla::ColourDesired m_field;
  683. Scintilla::ColourDesired m_staticField;
  684. Scintilla::ColourDesired m_staticFinalField;
  685. Scintilla::ColourDesired m_deprecatedMember;
  686. Scintilla::ColourDesired m_background;
  687. Scintilla::ColourDesired m_currentLine;
  688. Scintilla::ColourDesired m_foreground;
  689. Scintilla::ColourDesired m_lineNumber;
  690. Scintilla::ColourDesired m_selectionBackground;
  691. Scintilla::ColourDesired m_selectionForeground;
  692. };
  693. ScintillaEditor* ScintillaEditor::create(int _width, int _height)
  694. {
  695. ScEditor* editor = IMGUI_NEW(ScEditor);
  696. editor->Initialise();
  697. editor->Resize(0, 0, _width, _height);
  698. return reinterpret_cast<ScintillaEditor*>(editor);
  699. }
  700. void ScintillaEditor::destroy(ScintillaEditor* _scintilla)
  701. {
  702. IMGUI_DELETE(ScEditor, _scintilla);
  703. }
  704. intptr_t ScintillaEditor::command(unsigned int _msg, uintptr_t _p0, intptr_t _p1)
  705. {
  706. ScEditor* editor = reinterpret_cast<ScEditor*>(this);
  707. return editor->command(_msg, _p0, _p1);
  708. }
  709. void ScintillaEditor::draw()
  710. {
  711. ScEditor* editor = reinterpret_cast<ScEditor*>(this);
  712. return editor->draw();
  713. }
  714. // Scintilla hooks
  715. namespace Scintilla
  716. {
  717. Font::Font()
  718. : fid(0)
  719. {
  720. }
  721. Font::~Font()
  722. {
  723. }
  724. void Font::Create(const FontParameters& fp)
  725. {
  726. FontInt* newFont = (FontInt*)ImGui::MemAlloc(sizeof(FontInt) );
  727. fid = newFont;
  728. const stbtt_fontinfo& fontInfo = getFontInfo(0);
  729. memcpy(&newFont->m_fontInfo, &fontInfo, sizeof(stbtt_fontinfo) );
  730. newFont->m_scale = stbtt_ScaleForPixelHeight(&fontInfo, fp.size);
  731. newFont->m_fontSize = fp.size;
  732. }
  733. void Font::Release()
  734. {
  735. if (fid)
  736. {
  737. ImGui::MemFree( (FontInt*)fid);
  738. }
  739. }
  740. ColourDesired Platform::Chrome()
  741. {
  742. return makeRgba(0xe0, 0xe0, 0xe0);
  743. }
  744. ColourDesired Platform::ChromeHighlight()
  745. {
  746. return makeRgba(0xff, 0xff, 0xff);
  747. }
  748. const char* Platform::DefaultFont()
  749. {
  750. return "";
  751. }
  752. int Platform::DefaultFontSize()
  753. {
  754. return 15;
  755. }
  756. unsigned int Platform::DoubleClickTime()
  757. {
  758. return 500;
  759. }
  760. bool Platform::MouseButtonBounce()
  761. {
  762. return true;
  763. }
  764. void Platform::Assert(const char* _error, const char* _filename, int _line)
  765. {
  766. DebugPrintf("%s(%d): %s", _filename, _line, _error);
  767. }
  768. int Platform::Minimum(int a, int b)
  769. {
  770. return (int)bx::uint32_imin(a, b);
  771. }
  772. int Platform::Maximum(int a, int b)
  773. {
  774. return (int)bx::uint32_imax(a, b);
  775. }
  776. int Platform::Clamp(int val, int minVal, int maxVal)
  777. {
  778. return (int)bx::uint32_iclamp(val, minVal, maxVal);
  779. }
  780. void Platform::DebugPrintf(const char* _format, ...)
  781. {
  782. char temp[8192];
  783. char* out = temp;
  784. va_list argList;
  785. va_start(argList, _format);
  786. int32_t len = bx::vsnprintf(out, sizeof(temp), _format, argList);
  787. if ( (int32_t)sizeof(temp) < len)
  788. {
  789. out = (char*)alloca(len+1);
  790. len = bx::vsnprintf(out, len, _format, argList);
  791. }
  792. va_end(argList);
  793. out[len] = '\0';
  794. bx::debugOutput(out);
  795. }
  796. Menu::Menu()
  797. : mid(0)
  798. {
  799. }
  800. void Menu::CreatePopUp()
  801. {
  802. Destroy();
  803. mid = MenuID(1);
  804. }
  805. void Menu::Destroy()
  806. {
  807. mid = 0;
  808. }
  809. void Menu::Show(Point /*_pt*/, Window& /*_w*/)
  810. {
  811. Destroy();
  812. }
  813. Surface* Surface::Allocate(int)
  814. {
  815. return IMGUI_NEW(SurfaceInt);
  816. }
  817. Window::~Window()
  818. {
  819. }
  820. void Window::Destroy()
  821. {
  822. if (wid)
  823. {
  824. Show(false);
  825. WindowInt* wi = GetWindow(wid);
  826. IMGUI_DELETE(WindowInt, wi);
  827. }
  828. wid = 0;
  829. }
  830. bool Window::HasFocus()
  831. {
  832. return true;
  833. }
  834. PRectangle Window::GetPosition()
  835. {
  836. if (0 == wid)
  837. {
  838. return PRectangle();
  839. }
  840. return GetWindow(wid)->position;
  841. }
  842. void Window::SetPosition(PRectangle rc)
  843. {
  844. GetWindow(wid)->position = rc;
  845. }
  846. void Window::SetPositionRelative(PRectangle rc, Window w)
  847. {
  848. SetPosition(rc);
  849. }
  850. PRectangle Window::GetClientPosition()
  851. {
  852. if (0 == wid)
  853. {
  854. return PRectangle();
  855. }
  856. return GetWindow(wid)->position;
  857. }
  858. void Window::Show(bool _show)
  859. {
  860. if (0 != wid)
  861. {
  862. GetWindow(wid)->m_show = _show;
  863. }
  864. }
  865. void Window::InvalidateAll()
  866. {
  867. }
  868. void Window::InvalidateRectangle(PRectangle /*_rc*/)
  869. {
  870. }
  871. void Window::SetFont(Font& /*_font*/)
  872. {
  873. }
  874. void Window::SetCursor(Cursor /*_curs*/)
  875. {
  876. cursorLast = cursorText;
  877. }
  878. void Window::SetTitle(const char* /*_str*/)
  879. {
  880. }
  881. PRectangle Window::GetMonitorRect(Point /*_pt*/)
  882. {
  883. return PRectangle();
  884. }
  885. ListBox::ListBox()
  886. {
  887. }
  888. ListBox::~ListBox()
  889. {
  890. }
  891. ListBox* ListBox::Allocate()
  892. {
  893. return IMGUI_NEW(ListBoxInt);
  894. }
  895. } // namespace Scintilla
  896. #endif // defined(SCI_NAMESPACE)