scintilla.cpp 26 KB

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