scintilla.cpp 25 KB

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