scintilla.cpp 26 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124
  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 - 32
  522. , regionMax.y - cursorPos.y
  523. );
  524. Resize(0, 0, (int)size.x, (int)size.y);
  525. uint8_t modifiers = inputGetModifiersState();
  526. const bool shift = 0 != (modifiers & (entry::Modifier::LeftShift | entry::Modifier::RightShift) );
  527. const bool ctrl = 0 != (modifiers & (entry::Modifier::LeftCtrl | entry::Modifier::RightCtrl ) );
  528. const bool alt = 0 != (modifiers & (entry::Modifier::LeftAlt | entry::Modifier::RightAlt ) );
  529. if (ImGui::IsKeyPressed(entry::Key::Tab) )
  530. {
  531. Editor::KeyDown(SCK_TAB, shift, ctrl, alt);
  532. }
  533. else if (ImGui::IsKeyPressed(entry::Key::Left) )
  534. {
  535. Editor::KeyDown(SCK_LEFT, shift, ctrl, alt);
  536. }
  537. else if (ImGui::IsKeyPressed(entry::Key::Right) )
  538. {
  539. Editor::KeyDown(SCK_RIGHT, shift, ctrl, alt);
  540. }
  541. else if (ImGui::IsKeyPressed(entry::Key::Up) )
  542. {
  543. Editor::KeyDown(SCK_UP, shift, ctrl, alt);
  544. }
  545. else if (ImGui::IsKeyPressed(entry::Key::Down) )
  546. {
  547. Editor::KeyDown(SCK_DOWN, shift, ctrl, alt);
  548. }
  549. else if (ImGui::IsKeyPressed(entry::Key::PageUp) )
  550. {
  551. Editor::KeyDown(SCK_PRIOR, shift, ctrl, alt);
  552. }
  553. else if (ImGui::IsKeyPressed(entry::Key::PageDown) )
  554. {
  555. Editor::KeyDown(SCK_NEXT, shift, ctrl, alt);
  556. }
  557. else if (ImGui::IsKeyPressed(entry::Key::Home) )
  558. {
  559. Editor::KeyDown(SCK_HOME, shift, ctrl, alt);
  560. }
  561. else if (ImGui::IsKeyPressed(entry::Key::End) )
  562. {
  563. Editor::KeyDown(SCK_END, shift, ctrl, alt);
  564. }
  565. else if (ImGui::IsKeyPressed(entry::Key::Delete) )
  566. {
  567. Editor::KeyDown(SCK_DELETE, shift, ctrl, alt);
  568. }
  569. else if (ImGui::IsKeyPressed(entry::Key::Backspace) )
  570. {
  571. Editor::KeyDown(SCK_BACK, shift, ctrl, alt); inputGetChar();
  572. }
  573. else if (ImGui::IsKeyPressed(entry::Key::Return) )
  574. {
  575. Editor::KeyDown(SCK_RETURN, shift, ctrl, alt); inputGetChar();
  576. }
  577. else if (ImGui::IsKeyPressed(entry::Key::Esc) )
  578. {
  579. Editor::KeyDown(SCK_ESCAPE, shift, ctrl, alt);
  580. }
  581. else if (ctrl && ImGui::IsKeyPressed(entry::Key::KeyA) )
  582. {
  583. Editor::KeyDown('A', shift, ctrl, alt); inputGetChar();
  584. }
  585. else if (ctrl && ImGui::IsKeyPressed(entry::Key::KeyC) )
  586. {
  587. Editor::KeyDown('C', shift, ctrl, alt); inputGetChar();
  588. }
  589. else if (ctrl && ImGui::IsKeyPressed(entry::Key::KeyV) )
  590. {
  591. Editor::KeyDown('V', shift, ctrl, alt); inputGetChar();
  592. }
  593. else if (ctrl && ImGui::IsKeyPressed(entry::Key::KeyX) )
  594. {
  595. Editor::KeyDown('X', shift, ctrl, alt); inputGetChar();
  596. }
  597. else if (ctrl && ImGui::IsKeyPressed(entry::Key::KeyY) )
  598. {
  599. Editor::KeyDown('Y', shift, ctrl, alt); inputGetChar();
  600. }
  601. else if (ctrl && ImGui::IsKeyPressed(entry::Key::KeyZ) )
  602. {
  603. Editor::KeyDown('Z', shift, ctrl, alt); inputGetChar();
  604. }
  605. else if (ctrl || alt)
  606. {
  607. // ignore...
  608. }
  609. else
  610. {
  611. for (const uint8_t* ch = inputGetChar(); NULL != ch; ch = inputGetChar() )
  612. {
  613. switch (*ch)
  614. {
  615. case '\b': Editor::KeyDown(SCK_BACK, shift, ctrl, alt); break;
  616. case '\n': Editor::KeyDown(SCK_RETURN, shift, ctrl, alt); break;
  617. default: Editor::AddCharUTF( (const char*)ch, 1); break;
  618. }
  619. }
  620. }
  621. int32_t lineCount = int32_t(command(SCI_GETLINECOUNT) );
  622. int32_t firstVisibleLine = int32_t(command(SCI_GETFIRSTVISIBLELINE) );
  623. float fontHeight = ImGui::GetWindowFontSize();
  624. if (ImGui::IsMouseClicked(0) )
  625. {
  626. ImGuiIO& io = ImGui::GetIO();
  627. Scintilla::Point pt = Scintilla::Point::FromInts( (int)io.MouseClickedPos[0].x, (int)io.MouseClickedPos[0].y);
  628. ButtonDown(pt, (unsigned int)io.MouseDownDuration[0], false, false, false);
  629. }
  630. Tick();
  631. ImGui::BeginGroup();
  632. ImGui::BeginChild("##editor", ImVec2(size.x, size.y-20) );
  633. Scintilla::AutoSurface surfaceWindow(this);
  634. if (surfaceWindow)
  635. {
  636. Paint(surfaceWindow, GetClientRectangle() );
  637. surfaceWindow->Release();
  638. }
  639. ImGui::EndChild();
  640. ImGui::SameLine();
  641. ImGui::BeginChild("##scroll");
  642. ImGuiListClipper clipper;
  643. clipper.Begin(lineCount, fontHeight*2.0f);
  644. if (m_lastFirstVisibleLine != firstVisibleLine)
  645. {
  646. m_lastFirstVisibleLine = firstVisibleLine;
  647. ImGui::SetScrollY(firstVisibleLine * fontHeight*2.0f);
  648. }
  649. else if (firstVisibleLine != clipper.DisplayStart)
  650. {
  651. command(SCI_SETFIRSTVISIBLELINE, clipper.DisplayStart);
  652. }
  653. clipper.End();
  654. ImGui::EndChild();
  655. ImGui::EndGroup();
  656. }
  657. void setStyle(int style, Scintilla::ColourDesired fore, Scintilla::ColourDesired back = UINT32_MAX, int size = -1, const char* face = NULL)
  658. {
  659. command(SCI_STYLESETFORE, uptr_t(style), fore.AsLong() );
  660. command(SCI_STYLESETBACK, uptr_t(style), back.AsLong() );
  661. if (size >= 1)
  662. {
  663. command(SCI_STYLESETSIZE, uptr_t(style), size);
  664. }
  665. if (face)
  666. {
  667. command(SCI_STYLESETFONT, uptr_t(style), reinterpret_cast<sptr_t>(face) );
  668. }
  669. }
  670. private:
  671. int m_width;
  672. int m_height;
  673. int m_wheelVRotation;
  674. int m_wheelHRotation;
  675. int m_lastFirstVisibleLine;
  676. Scintilla::ColourDesired m_searchResultIndication;
  677. Scintilla::ColourDesired m_filteredSearchResultIndication;
  678. Scintilla::ColourDesired m_occurrenceIndication;
  679. Scintilla::ColourDesired m_writeOccurrenceIndication;
  680. Scintilla::ColourDesired m_findScope;
  681. Scintilla::ColourDesired m_sourceHoverBackground;
  682. Scintilla::ColourDesired m_singleLineComment;
  683. Scintilla::ColourDesired m_multiLineComment;
  684. Scintilla::ColourDesired m_commentTaskTag;
  685. Scintilla::ColourDesired m_javadoc;
  686. Scintilla::ColourDesired m_javadocLink;
  687. Scintilla::ColourDesired m_javadocTag;
  688. Scintilla::ColourDesired m_javadocKeyword;
  689. Scintilla::ColourDesired m_class;
  690. Scintilla::ColourDesired m_interface;
  691. Scintilla::ColourDesired m_method;
  692. Scintilla::ColourDesired m_methodDeclaration;
  693. Scintilla::ColourDesired m_bracket;
  694. Scintilla::ColourDesired m_number;
  695. Scintilla::ColourDesired m_string;
  696. Scintilla::ColourDesired m_operator;
  697. Scintilla::ColourDesired m_keyword;
  698. Scintilla::ColourDesired m_annotation;
  699. Scintilla::ColourDesired m_staticMethod;
  700. Scintilla::ColourDesired m_localVariable;
  701. Scintilla::ColourDesired m_localVariableDeclaration;
  702. Scintilla::ColourDesired m_field;
  703. Scintilla::ColourDesired m_staticField;
  704. Scintilla::ColourDesired m_staticFinalField;
  705. Scintilla::ColourDesired m_deprecatedMember;
  706. Scintilla::ColourDesired m_background;
  707. Scintilla::ColourDesired m_currentLine;
  708. Scintilla::ColourDesired m_foreground;
  709. Scintilla::ColourDesired m_lineNumber;
  710. Scintilla::ColourDesired m_selectionBackground;
  711. Scintilla::ColourDesired m_selectionForeground;
  712. };
  713. ScintillaEditor* ScintillaEditor::create(int _width, int _height)
  714. {
  715. Editor* editor = IMGUI_NEW(Editor);
  716. editor->Initialise();
  717. editor->Resize(0, 0, _width, _height);
  718. return reinterpret_cast<ScintillaEditor*>(editor);
  719. }
  720. void ScintillaEditor::destroy(ScintillaEditor* _scintilla)
  721. {
  722. IMGUI_DELETE(Editor, _scintilla);
  723. }
  724. intptr_t ScintillaEditor::command(unsigned int _msg, uintptr_t _p0, intptr_t _p1)
  725. {
  726. Editor* editor = reinterpret_cast<Editor*>(this);
  727. return editor->command(_msg, _p0, _p1);
  728. }
  729. void ScintillaEditor::draw()
  730. {
  731. Editor* editor = reinterpret_cast<Editor*>(this);
  732. return editor->draw();
  733. }
  734. // Scintilla hooks
  735. namespace Scintilla
  736. {
  737. Font::Font()
  738. : fid(0)
  739. {
  740. }
  741. Font::~Font()
  742. {
  743. }
  744. void Font::Create(const FontParameters& fp)
  745. {
  746. FontInt* newFont = (FontInt*)ImGui::MemAlloc(sizeof(FontInt) );
  747. fid = newFont;
  748. newFont->m_font = ImGui::GetIO().Fonts->Fonts[0];
  749. newFont->m_fontSize = fp.size;
  750. newFont->m_scale = fp.size / newFont->m_font->FontSize;
  751. }
  752. void Font::Release()
  753. {
  754. if (fid)
  755. {
  756. ImGui::MemFree( (FontInt*)fid);
  757. }
  758. }
  759. ColourDesired Platform::Chrome()
  760. {
  761. return makeRgba(0xe0, 0xe0, 0xe0);
  762. }
  763. ColourDesired Platform::ChromeHighlight()
  764. {
  765. return makeRgba(0xff, 0xff, 0xff);
  766. }
  767. const char* Platform::DefaultFont()
  768. {
  769. return "";
  770. }
  771. int Platform::DefaultFontSize()
  772. {
  773. return 15;
  774. }
  775. unsigned int Platform::DoubleClickTime()
  776. {
  777. return 500;
  778. }
  779. bool Platform::MouseButtonBounce()
  780. {
  781. return true;
  782. }
  783. void Platform::Assert(const char* _error, const char* _filename, int _line)
  784. {
  785. DebugPrintf("%s(%d): %s", _filename, _line, _error);
  786. }
  787. int Platform::Minimum(int a, int b)
  788. {
  789. return (int)bx::uint32_imin(a, b);
  790. }
  791. int Platform::Maximum(int a, int b)
  792. {
  793. return (int)bx::uint32_imax(a, b);
  794. }
  795. int Platform::Clamp(int val, int minVal, int maxVal)
  796. {
  797. return (int)bx::uint32_iclamp(val, minVal, maxVal);
  798. }
  799. void Platform::DebugPrintf(const char* _format, ...)
  800. {
  801. char temp[8192];
  802. char* out = temp;
  803. va_list argList;
  804. va_start(argList, _format);
  805. int32_t len = bx::vsnprintf(out, sizeof(temp), _format, argList);
  806. if ( (int32_t)sizeof(temp) < len)
  807. {
  808. out = (char*)alloca(len+1);
  809. len = bx::vsnprintf(out, len, _format, argList);
  810. }
  811. va_end(argList);
  812. out[len] = '\0';
  813. bx::debugOutput(out);
  814. }
  815. Menu::Menu()
  816. : mid(0)
  817. {
  818. }
  819. void Menu::CreatePopUp()
  820. {
  821. Destroy();
  822. mid = MenuID(1);
  823. }
  824. void Menu::Destroy()
  825. {
  826. mid = 0;
  827. }
  828. void Menu::Show(Point /*_pt*/, Window& /*_w*/)
  829. {
  830. Destroy();
  831. }
  832. Surface* Surface::Allocate(int)
  833. {
  834. return IMGUI_NEW(SurfaceInt);
  835. }
  836. Window::~Window()
  837. {
  838. }
  839. void Window::Destroy()
  840. {
  841. if (wid)
  842. {
  843. Show(false);
  844. WindowInt* wi = GetWindow(wid);
  845. IMGUI_DELETE(WindowInt, wi);
  846. }
  847. wid = 0;
  848. }
  849. bool Window::HasFocus()
  850. {
  851. return true;
  852. }
  853. PRectangle Window::GetPosition()
  854. {
  855. if (0 == wid)
  856. {
  857. return PRectangle();
  858. }
  859. return GetWindow(wid)->position;
  860. }
  861. void Window::SetPosition(PRectangle rc)
  862. {
  863. GetWindow(wid)->position = rc;
  864. }
  865. void Window::SetPositionRelative(PRectangle _rc, Window /*_w*/)
  866. {
  867. SetPosition(_rc);
  868. }
  869. PRectangle Window::GetClientPosition()
  870. {
  871. if (0 == wid)
  872. {
  873. return PRectangle();
  874. }
  875. return GetWindow(wid)->position;
  876. }
  877. void Window::Show(bool _show)
  878. {
  879. if (0 != wid)
  880. {
  881. GetWindow(wid)->m_show = _show;
  882. }
  883. }
  884. void Window::InvalidateAll()
  885. {
  886. }
  887. void Window::InvalidateRectangle(PRectangle /*_rc*/)
  888. {
  889. }
  890. void Window::SetFont(Font& /*_font*/)
  891. {
  892. }
  893. void Window::SetCursor(Cursor /*_curs*/)
  894. {
  895. cursorLast = cursorText;
  896. }
  897. void Window::SetTitle(const char* /*_str*/)
  898. {
  899. }
  900. PRectangle Window::GetMonitorRect(Point /*_pt*/)
  901. {
  902. return PRectangle();
  903. }
  904. ListBox::ListBox()
  905. {
  906. }
  907. ListBox::~ListBox()
  908. {
  909. }
  910. ListBox* ListBox::Allocate()
  911. {
  912. return IMGUI_NEW(ListBoxInt);
  913. }
  914. } // namespace Scintilla
  915. ScintillaEditor* ImGuiScintilla(const char* _name, bool* _opened, const ImVec2& _size)
  916. {
  917. ScintillaEditor* sci = NULL;
  918. if (ImGui::Begin(_name, _opened, _size) )
  919. {
  920. ImGuiStorage* storage = ImGui::GetStateStorage();
  921. ImGuiID id = ImGui::GetID(_name);
  922. sci = (ScintillaEditor*)storage->GetVoidPtr(id);
  923. if (NULL == sci)
  924. {
  925. ImVec2 size = ImGui::GetWindowSize();
  926. sci = ScintillaEditor::create(size.x, size.y);
  927. storage->SetVoidPtr(id, (void*)sci);
  928. }
  929. sci->draw();
  930. }
  931. ImGui::End();
  932. return sci;
  933. }
  934. #endif // defined(SCI_NAMESPACE)