scintilla.cpp 25 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090
  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. 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. ImFont* m_font;
  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. return fi->m_font->GetCharAdvance((unsigned int)ch) * fi->m_scale;
  193. }
  194. virtual Scintilla::XYPOSITION Ascent(Scintilla::Font& _font) BX_OVERRIDE
  195. {
  196. FontInt* fi = (FontInt*)_font.GetID();
  197. return fi->m_font->Ascent * fi->m_scale;
  198. }
  199. virtual Scintilla::XYPOSITION Descent(Scintilla::Font& _font) BX_OVERRIDE
  200. {
  201. FontInt* fi = (FontInt*)_font.GetID();
  202. return -fi->m_font->Descent * fi->m_scale;
  203. }
  204. virtual Scintilla::XYPOSITION InternalLeading(Scintilla::Font& /*_font*/) BX_OVERRIDE
  205. {
  206. return 0;
  207. }
  208. virtual Scintilla::XYPOSITION ExternalLeading(Scintilla::Font& /*_font*/) BX_OVERRIDE
  209. {
  210. return 0;
  211. }
  212. virtual Scintilla::XYPOSITION Height(Scintilla::Font& _font) BX_OVERRIDE
  213. {
  214. return Ascent(_font) + Descent(_font);
  215. }
  216. virtual Scintilla::XYPOSITION AverageCharWidth(Scintilla::Font& _font) BX_OVERRIDE
  217. {
  218. return WidthChar(_font, 'n');
  219. }
  220. virtual void SetClip(Scintilla::PRectangle /*_rc*/) BX_OVERRIDE
  221. {
  222. }
  223. virtual void FlushCachedState() BX_OVERRIDE
  224. {
  225. }
  226. virtual void SetUnicodeMode(bool /*_unicodeMode*/) BX_OVERRIDE
  227. {
  228. }
  229. virtual void SetDBCSMode(int /*_codePage*/) BX_OVERRIDE
  230. {
  231. }
  232. private:
  233. void DrawTextBase(Scintilla::PRectangle _rc, Scintilla::Font& _font, float _ybase, const char* _str, int _len, Scintilla::ColourDesired _fore)
  234. {
  235. float xt = _rc.left;
  236. float yt = _ybase;
  237. uint32_t fore = (uint32_t)_fore.AsLong();
  238. FontInt* fi = (FontInt*)_font.GetID();
  239. ImVec2 pos = ImGui::GetCursorScreenPos();
  240. ImDrawList* drawList = ImGui::GetWindowDrawList();
  241. drawList->AddText(fi->m_font
  242. , fi->m_fontSize
  243. , ImVec2(xt + pos.x, yt + pos.y - fi->m_fontSize)
  244. , fore
  245. , _str
  246. , _str + _len
  247. );
  248. }
  249. Scintilla::ColourDesired m_penColour;
  250. };
  251. struct WindowInt
  252. {
  253. WindowInt()
  254. : m_show(false)
  255. {
  256. }
  257. Scintilla::PRectangle position;
  258. bool m_show;
  259. };
  260. WindowInt* AllocateWindowInt()
  261. {
  262. return new WindowInt;
  263. }
  264. inline WindowInt* GetWindow(Scintilla::WindowID id)
  265. {
  266. return (WindowInt*)id;
  267. }
  268. class ListBoxInt : public Scintilla::ListBox
  269. {
  270. public:
  271. ListBoxInt()
  272. : m_maxStrWidth(0)
  273. , m_lineHeight(10)
  274. , m_desiredVisibleRows(5)
  275. , m_aveCharWidth(8)
  276. , m_unicodeMode(false)
  277. {
  278. }
  279. ~ListBoxInt()
  280. {
  281. }
  282. virtual void SetFont(Scintilla::Font& /*_font*/) BX_OVERRIDE
  283. {
  284. }
  285. virtual void Create(Scintilla::Window& /*_parent*/, int /*_ctrlID*/, Scintilla::Point _location, int _lineHeight, bool _unicodeMode, int /*_technology*/) BX_OVERRIDE
  286. {
  287. m_location = _location;
  288. m_lineHeight = _lineHeight;
  289. m_unicodeMode = _unicodeMode;
  290. m_maxStrWidth = 0;
  291. wid = Scintilla::WindowID(4);
  292. }
  293. virtual void SetAverageCharWidth(int width) BX_OVERRIDE
  294. {
  295. m_aveCharWidth = width;
  296. }
  297. virtual void SetVisibleRows(int rows) BX_OVERRIDE
  298. {
  299. m_desiredVisibleRows = rows;
  300. }
  301. virtual int GetVisibleRows() const BX_OVERRIDE
  302. {
  303. return m_desiredVisibleRows;
  304. }
  305. virtual Scintilla::PRectangle GetDesiredRect() BX_OVERRIDE
  306. {
  307. Scintilla::PRectangle rc;
  308. rc.top = 0;
  309. rc.left = 0;
  310. rc.right = 350;
  311. rc.bottom = 140;
  312. return rc;
  313. }
  314. virtual int CaretFromEdge() BX_OVERRIDE
  315. {
  316. return 4 + 16;
  317. }
  318. virtual void Clear() BX_OVERRIDE
  319. {
  320. }
  321. virtual void Append(char* /*s*/, int /*type = -1*/) BX_OVERRIDE
  322. {
  323. }
  324. virtual int Length() BX_OVERRIDE
  325. {
  326. return 0;
  327. }
  328. virtual void Select(int /*n*/) BX_OVERRIDE
  329. {
  330. }
  331. virtual int GetSelection() BX_OVERRIDE
  332. {
  333. return 0;
  334. }
  335. virtual int Find(const char* /*prefix*/) BX_OVERRIDE
  336. {
  337. return 0;
  338. }
  339. virtual void GetValue(int /*n*/, char* value, int /*len*/) BX_OVERRIDE
  340. {
  341. value[0] = '\0';
  342. }
  343. virtual void RegisterImage(int /*type*/, const char* /*xpm_data*/) BX_OVERRIDE
  344. {
  345. }
  346. virtual void RegisterRGBAImage(int /*type*/, int /*width*/, int /*height*/, const unsigned char* /*pixelsImage*/) BX_OVERRIDE
  347. {
  348. }
  349. virtual void ClearRegisteredImages() BX_OVERRIDE
  350. {
  351. }
  352. virtual void SetDoubleClickAction(Scintilla::CallBackAction, void*) BX_OVERRIDE
  353. {
  354. }
  355. virtual void SetList(const char* /*list*/, char /*separator*/, char /*typesep*/) BX_OVERRIDE
  356. {
  357. }
  358. private:
  359. Scintilla::Point m_location;
  360. size_t m_maxStrWidth;
  361. int m_lineHeight;
  362. int m_desiredVisibleRows;
  363. int m_aveCharWidth;
  364. bool m_unicodeMode;
  365. };
  366. struct Editor : public Scintilla::ScintillaBase
  367. {
  368. public:
  369. Editor()
  370. : m_width(0)
  371. , m_height(0)
  372. , m_wheelVRotation(0)
  373. , m_wheelHRotation(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() BX_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*/) BX_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*/) BX_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() BX_OVERRIDE
  477. {
  478. }
  479. virtual void SetHorizontalScrollPos() BX_OVERRIDE
  480. {
  481. xOffset = 0;
  482. }
  483. virtual bool ModifyScrollBars(int /*nMax*/, int /*nPage*/) BX_OVERRIDE
  484. {
  485. return false;
  486. }
  487. void ClaimSelection()
  488. {
  489. }
  490. virtual void Copy() BX_OVERRIDE
  491. {
  492. }
  493. virtual void Paste() BX_OVERRIDE
  494. {
  495. }
  496. virtual void NotifyChange() BX_OVERRIDE
  497. {
  498. }
  499. virtual void NotifyParent(Scintilla::SCNotification /*scn*/) BX_OVERRIDE
  500. {
  501. }
  502. virtual void CopyToClipboard(const Scintilla::SelectionText& /*selectedText*/) BX_OVERRIDE
  503. {
  504. }
  505. virtual void SetMouseCapture(bool /*on*/) BX_OVERRIDE
  506. {
  507. }
  508. virtual bool HaveMouseCapture() BX_OVERRIDE
  509. {
  510. return false;
  511. }
  512. virtual sptr_t DefWndProc(unsigned int /*iMessage*/, uptr_t /*wParam*/, sptr_t /*lParam*/) BX_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, regionMax.y - cursorPos.y);
  525. Resize(0, 0, (int)size.x, (int)size.y);
  526. uint8_t modifiers = inputGetModifiersState();
  527. const bool shift = 0 != (modifiers & (entry::Modifier::LeftShift | entry::Modifier::RightShift) );
  528. const bool ctrl = 0 != (modifiers & (entry::Modifier::LeftCtrl | entry::Modifier::RightCtrl ) );
  529. const bool alt = 0 != (modifiers & (entry::Modifier::LeftAlt | entry::Modifier::RightAlt ) );
  530. if (ImGui::IsKeyPressed(entry::Key::Tab) )
  531. {
  532. Editor::KeyDown(SCK_TAB, shift, ctrl, alt);
  533. }
  534. else if (ImGui::IsKeyPressed(entry::Key::Left) )
  535. {
  536. Editor::KeyDown(SCK_LEFT, shift, ctrl, alt);
  537. }
  538. else if (ImGui::IsKeyPressed(entry::Key::Right) )
  539. {
  540. Editor::KeyDown(SCK_RIGHT, shift, ctrl, alt);
  541. }
  542. else if (ImGui::IsKeyPressed(entry::Key::Up) )
  543. {
  544. Editor::KeyDown(SCK_UP, shift, ctrl, alt);
  545. }
  546. else if (ImGui::IsKeyPressed(entry::Key::Down) )
  547. {
  548. Editor::KeyDown(SCK_DOWN, shift, ctrl, alt);
  549. }
  550. else if (ImGui::IsKeyPressed(entry::Key::Home) )
  551. {
  552. Editor::KeyDown(SCK_HOME, shift, ctrl, alt);
  553. }
  554. else if (ImGui::IsKeyPressed(entry::Key::End) )
  555. {
  556. Editor::KeyDown(SCK_END, shift, ctrl, alt);
  557. }
  558. else if (ImGui::IsKeyPressed(entry::Key::Delete) )
  559. {
  560. Editor::KeyDown(SCK_DELETE, shift, ctrl, alt);
  561. }
  562. else if (ImGui::IsKeyPressed(entry::Key::Backspace) )
  563. {
  564. Editor::KeyDown(SCK_BACK, shift, ctrl, alt); inputGetChar();
  565. }
  566. else if (ImGui::IsKeyPressed(entry::Key::Return) )
  567. {
  568. Editor::KeyDown(SCK_RETURN, shift, ctrl, alt); inputGetChar();
  569. }
  570. else if (ImGui::IsKeyPressed(entry::Key::Esc) )
  571. {
  572. Editor::KeyDown(SCK_ESCAPE, shift, ctrl, alt);
  573. }
  574. else if (ctrl && ImGui::IsKeyPressed(entry::Key::KeyA) )
  575. {
  576. Editor::KeyDown('A', shift, ctrl, alt); inputGetChar();
  577. }
  578. else if (ctrl && ImGui::IsKeyPressed(entry::Key::KeyC) )
  579. {
  580. Editor::KeyDown('C', shift, ctrl, alt); inputGetChar();
  581. }
  582. else if (ctrl && ImGui::IsKeyPressed(entry::Key::KeyV) )
  583. {
  584. Editor::KeyDown('V', shift, ctrl, alt); inputGetChar();
  585. }
  586. else if (ctrl && ImGui::IsKeyPressed(entry::Key::KeyX) )
  587. {
  588. Editor::KeyDown('X', shift, ctrl, alt); inputGetChar();
  589. }
  590. else if (ctrl && ImGui::IsKeyPressed(entry::Key::KeyY) )
  591. {
  592. Editor::KeyDown('Y', shift, ctrl, alt); inputGetChar();
  593. }
  594. else if (ctrl && ImGui::IsKeyPressed(entry::Key::KeyZ) )
  595. {
  596. Editor::KeyDown('Z', shift, ctrl, alt); inputGetChar();
  597. }
  598. else if (ctrl || alt)
  599. {
  600. // ignore...
  601. }
  602. else
  603. {
  604. for (const uint8_t* ch = inputGetChar(); NULL != ch; ch = inputGetChar() )
  605. {
  606. switch (*ch)
  607. {
  608. case '\b': Editor::KeyDown(SCK_BACK, shift, ctrl, alt); break;
  609. case '\n': Editor::KeyDown(SCK_RETURN, shift, ctrl, alt); break;
  610. default: Editor::AddCharUTF( (const char*)ch, 1); break;
  611. }
  612. }
  613. }
  614. if (ImGui::IsMouseClicked(0) )
  615. {
  616. ImGuiIO& io = ImGui::GetIO();
  617. Scintilla::Point pt = Scintilla::Point::FromInts( (int)io.MouseClickedPos[0].x, (int)io.MouseClickedPos[0].y);
  618. ButtonDown(pt, (unsigned int)io.MouseDownTime[0], false, false, false);
  619. }
  620. Tick();
  621. Scintilla::AutoSurface surfaceWindow(this);
  622. if (surfaceWindow)
  623. {
  624. Paint(surfaceWindow, GetClientRectangle() );
  625. surfaceWindow->Release();
  626. }
  627. }
  628. void setStyle(int style, Scintilla::ColourDesired fore, Scintilla::ColourDesired back = UINT32_MAX, int size = -1, const char* face = NULL)
  629. {
  630. command(SCI_STYLESETFORE, uptr_t(style), fore.AsLong() );
  631. command(SCI_STYLESETBACK, uptr_t(style), back.AsLong() );
  632. if (size >= 1)
  633. {
  634. command(SCI_STYLESETSIZE, uptr_t(style), size);
  635. }
  636. if (face)
  637. {
  638. command(SCI_STYLESETFONT, uptr_t(style), reinterpret_cast<sptr_t>(face) );
  639. }
  640. }
  641. private:
  642. int m_width;
  643. int m_height;
  644. int m_wheelVRotation;
  645. int m_wheelHRotation;
  646. Scintilla::ColourDesired m_searchResultIndication;
  647. Scintilla::ColourDesired m_filteredSearchResultIndication;
  648. Scintilla::ColourDesired m_occurrenceIndication;
  649. Scintilla::ColourDesired m_writeOccurrenceIndication;
  650. Scintilla::ColourDesired m_findScope;
  651. Scintilla::ColourDesired m_sourceHoverBackground;
  652. Scintilla::ColourDesired m_singleLineComment;
  653. Scintilla::ColourDesired m_multiLineComment;
  654. Scintilla::ColourDesired m_commentTaskTag;
  655. Scintilla::ColourDesired m_javadoc;
  656. Scintilla::ColourDesired m_javadocLink;
  657. Scintilla::ColourDesired m_javadocTag;
  658. Scintilla::ColourDesired m_javadocKeyword;
  659. Scintilla::ColourDesired m_class;
  660. Scintilla::ColourDesired m_interface;
  661. Scintilla::ColourDesired m_method;
  662. Scintilla::ColourDesired m_methodDeclaration;
  663. Scintilla::ColourDesired m_bracket;
  664. Scintilla::ColourDesired m_number;
  665. Scintilla::ColourDesired m_string;
  666. Scintilla::ColourDesired m_operator;
  667. Scintilla::ColourDesired m_keyword;
  668. Scintilla::ColourDesired m_annotation;
  669. Scintilla::ColourDesired m_staticMethod;
  670. Scintilla::ColourDesired m_localVariable;
  671. Scintilla::ColourDesired m_localVariableDeclaration;
  672. Scintilla::ColourDesired m_field;
  673. Scintilla::ColourDesired m_staticField;
  674. Scintilla::ColourDesired m_staticFinalField;
  675. Scintilla::ColourDesired m_deprecatedMember;
  676. Scintilla::ColourDesired m_background;
  677. Scintilla::ColourDesired m_currentLine;
  678. Scintilla::ColourDesired m_foreground;
  679. Scintilla::ColourDesired m_lineNumber;
  680. Scintilla::ColourDesired m_selectionBackground;
  681. Scintilla::ColourDesired m_selectionForeground;
  682. };
  683. ScintillaEditor* ScintillaEditor::create(int _width, int _height)
  684. {
  685. Editor* editor = IMGUI_NEW(Editor);
  686. editor->Initialise();
  687. editor->Resize(0, 0, _width, _height);
  688. return reinterpret_cast<ScintillaEditor*>(editor);
  689. }
  690. void ScintillaEditor::destroy(ScintillaEditor* _scintilla)
  691. {
  692. IMGUI_DELETE(Editor, _scintilla);
  693. }
  694. intptr_t ScintillaEditor::command(unsigned int _msg, uintptr_t _p0, intptr_t _p1)
  695. {
  696. Editor* editor = reinterpret_cast<Editor*>(this);
  697. return editor->command(_msg, _p0, _p1);
  698. }
  699. void ScintillaEditor::draw()
  700. {
  701. Editor* editor = reinterpret_cast<Editor*>(this);
  702. return editor->draw();
  703. }
  704. // Scintilla hooks
  705. namespace Scintilla
  706. {
  707. Font::Font()
  708. : fid(0)
  709. {
  710. }
  711. Font::~Font()
  712. {
  713. }
  714. void Font::Create(const FontParameters& fp)
  715. {
  716. FontInt* newFont = (FontInt*)ImGui::MemAlloc(sizeof(FontInt) );
  717. fid = newFont;
  718. newFont->m_font = ImGui::GetIO().Fonts->Fonts[0];
  719. newFont->m_fontSize = fp.size;
  720. newFont->m_scale = fp.size / newFont->m_font->FontSize;
  721. }
  722. void Font::Release()
  723. {
  724. if (fid)
  725. {
  726. ImGui::MemFree( (FontInt*)fid);
  727. }
  728. }
  729. ColourDesired Platform::Chrome()
  730. {
  731. return makeRgba(0xe0, 0xe0, 0xe0);
  732. }
  733. ColourDesired Platform::ChromeHighlight()
  734. {
  735. return makeRgba(0xff, 0xff, 0xff);
  736. }
  737. const char* Platform::DefaultFont()
  738. {
  739. return "";
  740. }
  741. int Platform::DefaultFontSize()
  742. {
  743. return 15;
  744. }
  745. unsigned int Platform::DoubleClickTime()
  746. {
  747. return 500;
  748. }
  749. bool Platform::MouseButtonBounce()
  750. {
  751. return true;
  752. }
  753. void Platform::Assert(const char* _error, const char* _filename, int _line)
  754. {
  755. DebugPrintf("%s(%d): %s", _filename, _line, _error);
  756. }
  757. int Platform::Minimum(int a, int b)
  758. {
  759. return (int)bx::uint32_imin(a, b);
  760. }
  761. int Platform::Maximum(int a, int b)
  762. {
  763. return (int)bx::uint32_imax(a, b);
  764. }
  765. int Platform::Clamp(int val, int minVal, int maxVal)
  766. {
  767. return (int)bx::uint32_iclamp(val, minVal, maxVal);
  768. }
  769. void Platform::DebugPrintf(const char* _format, ...)
  770. {
  771. char temp[8192];
  772. char* out = temp;
  773. va_list argList;
  774. va_start(argList, _format);
  775. int32_t len = bx::vsnprintf(out, sizeof(temp), _format, argList);
  776. if ( (int32_t)sizeof(temp) < len)
  777. {
  778. out = (char*)alloca(len+1);
  779. len = bx::vsnprintf(out, len, _format, argList);
  780. }
  781. va_end(argList);
  782. out[len] = '\0';
  783. bx::debugOutput(out);
  784. }
  785. Menu::Menu()
  786. : mid(0)
  787. {
  788. }
  789. void Menu::CreatePopUp()
  790. {
  791. Destroy();
  792. mid = MenuID(1);
  793. }
  794. void Menu::Destroy()
  795. {
  796. mid = 0;
  797. }
  798. void Menu::Show(Point /*_pt*/, Window& /*_w*/)
  799. {
  800. Destroy();
  801. }
  802. Surface* Surface::Allocate(int)
  803. {
  804. return IMGUI_NEW(SurfaceInt);
  805. }
  806. Window::~Window()
  807. {
  808. }
  809. void Window::Destroy()
  810. {
  811. if (wid)
  812. {
  813. Show(false);
  814. WindowInt* wi = GetWindow(wid);
  815. IMGUI_DELETE(WindowInt, wi);
  816. }
  817. wid = 0;
  818. }
  819. bool Window::HasFocus()
  820. {
  821. return true;
  822. }
  823. PRectangle Window::GetPosition()
  824. {
  825. if (0 == wid)
  826. {
  827. return PRectangle();
  828. }
  829. return GetWindow(wid)->position;
  830. }
  831. void Window::SetPosition(PRectangle rc)
  832. {
  833. GetWindow(wid)->position = rc;
  834. }
  835. void Window::SetPositionRelative(PRectangle _rc, Window /*_w*/)
  836. {
  837. SetPosition(_rc);
  838. }
  839. PRectangle Window::GetClientPosition()
  840. {
  841. if (0 == wid)
  842. {
  843. return PRectangle();
  844. }
  845. return GetWindow(wid)->position;
  846. }
  847. void Window::Show(bool _show)
  848. {
  849. if (0 != wid)
  850. {
  851. GetWindow(wid)->m_show = _show;
  852. }
  853. }
  854. void Window::InvalidateAll()
  855. {
  856. }
  857. void Window::InvalidateRectangle(PRectangle /*_rc*/)
  858. {
  859. }
  860. void Window::SetFont(Font& /*_font*/)
  861. {
  862. }
  863. void Window::SetCursor(Cursor /*_curs*/)
  864. {
  865. cursorLast = cursorText;
  866. }
  867. void Window::SetTitle(const char* /*_str*/)
  868. {
  869. }
  870. PRectangle Window::GetMonitorRect(Point /*_pt*/)
  871. {
  872. return PRectangle();
  873. }
  874. ListBox::ListBox()
  875. {
  876. }
  877. ListBox::~ListBox()
  878. {
  879. }
  880. ListBox* ListBox::Allocate()
  881. {
  882. return IMGUI_NEW(ListBoxInt);
  883. }
  884. } // namespace Scintilla
  885. ScintillaEditor* ImGuiScintilla(const char* _name, bool* _opened, const ImVec2& _size)
  886. {
  887. ScintillaEditor* sci = NULL;
  888. if (ImGui::Begin(_name, _opened, _size) )
  889. {
  890. ImGuiStorage* storage = ImGui::GetStateStorage();
  891. ImGuiID id = ImGui::GetID(_name);
  892. sci = (ScintillaEditor*)storage->GetVoidPtr(id);
  893. if (NULL == sci)
  894. {
  895. ImVec2 size = ImGui::GetWindowSize();
  896. sci = ScintillaEditor::create(size.x, size.y);
  897. storage->SetVoidPtr(id, (void*)sci);
  898. }
  899. sci->draw();
  900. }
  901. ImGui::End();
  902. return sci;
  903. }
  904. #endif // defined(SCI_NAMESPACE)