scintilla.cpp 25 KB

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