scintilla.cpp 25 KB

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