JsonEditor.cpp 99 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304
  1. #include "JsonEditor.h"
  2. #include "Font.h"
  3. #include "Globals.h"
  4. #include "Image.h"
  5. #include "KeyboardEvent.h"
  6. #include "MouseEvent.h"
  7. #include "Regex.h"
  8. #include "TextField.h"
  9. using namespace Framework;
  10. using namespace JSON;
  11. const char* errors[] = {"Unexpected start of object",
  12. "Unexpected start of array",
  13. "Unexpected value",
  14. "Unexpected ','",
  15. "Unexpected ':'",
  16. "Unexpected end of object",
  17. "Unexpected end of array",
  18. "Unexpected start of string",
  19. "Invalid json value",
  20. ""};
  21. class EditableJsonElementReference : public virtual AbstractElement,
  22. public virtual ReferenceCounter,
  23. public virtual AbstractBool,
  24. public virtual AbstractNumber,
  25. public virtual AbstractString,
  26. public virtual AbstractArray,
  27. public virtual AbstractObject
  28. {
  29. private:
  30. EditableJsonElement* root;
  31. RCArray<EditableJsonElementReference>* children;
  32. EditableJsonElementReference(EditableJsonElement* root,
  33. RCArray<EditableJsonElementReference>* children)
  34. : root(root),
  35. children(children)
  36. {}
  37. public:
  38. EditableJsonElementReference(EditableJsonElement* root)
  39. : root(root),
  40. children(new RCArray<EditableJsonElementReference>())
  41. {}
  42. ~EditableJsonElementReference()
  43. {
  44. children->release();
  45. }
  46. AbstractType getType() const override
  47. {
  48. if (root->hasError() || root->getValue().isEqual("null"))
  49. {
  50. return AbstractType::NULL_;
  51. }
  52. else if (root->getOpeningControllChar() == '{')
  53. {
  54. return AbstractType::OBJECT;
  55. }
  56. else if (root->getOpeningControllChar() == '[')
  57. {
  58. return AbstractType::ARRAY;
  59. }
  60. else if (root->getValue().isEqual("true")
  61. || root->getValue().isEqual("false"))
  62. {
  63. return AbstractType::BOOLEAN;
  64. }
  65. else if (root->getValue().hasAt(0, "\""))
  66. {
  67. return AbstractType::STRING;
  68. }
  69. else
  70. {
  71. return AbstractType::NUMBER;
  72. }
  73. }
  74. const AbstractBool* asAbstractBool() const override
  75. {
  76. return this;
  77. }
  78. const AbstractNumber* asAbstractNumber() const override
  79. {
  80. return this;
  81. }
  82. const AbstractString* asAbstractString() const override
  83. {
  84. return this;
  85. }
  86. const AbstractArray* asAbstractArray() const override
  87. {
  88. return this;
  89. }
  90. const AbstractObject* asAbstractObject() const override
  91. {
  92. return this;
  93. }
  94. Text toString() const override
  95. {
  96. return root->getRecursiveContent();
  97. }
  98. bool getBool() const override
  99. {
  100. return (bool)root->getValue();
  101. }
  102. double getNumber() const override
  103. {
  104. return (double)root->getValue();
  105. }
  106. Text getString() const override
  107. {
  108. Text txt = root->getValue().getText() + 1;
  109. txt.remove(txt.getLength() - 1, 1);
  110. return (double)root->getValue();
  111. }
  112. AbstractElement* zAbstractValue(int i) const override
  113. {
  114. EditableJsonElement* e = root->zFirstChildren();
  115. while (e && i > 0)
  116. {
  117. e = e->zMextSibling();
  118. i--;
  119. }
  120. if (e)
  121. {
  122. EditableJsonElementReference* result
  123. = new EditableJsonElementReference(e, children);
  124. children->add(result);
  125. return result;
  126. }
  127. return 0;
  128. }
  129. int getLength() const override
  130. {
  131. EditableJsonElement* e = root->zFirstChildren();
  132. int i = 0;
  133. while (e)
  134. {
  135. e = e->zMextSibling();
  136. i++;
  137. }
  138. return i;
  139. }
  140. AbstractElement* zAbstractValue(Text field) const override
  141. {
  142. EditableJsonElement* e = root->zFirstChildren();
  143. while (e)
  144. {
  145. if (e->getKey().isEqual(field))
  146. {
  147. EditableJsonElementReference* result
  148. = new EditableJsonElementReference(e, children);
  149. children->add(result);
  150. return result;
  151. }
  152. e = e->zMextSibling();
  153. }
  154. return 0;
  155. }
  156. int getFieldCount() const override
  157. {
  158. return getLength();
  159. }
  160. Text getFieldKey(int i) const override
  161. {
  162. EditableJsonElement* e = root->zFirstChildren();
  163. while (e && i > 0)
  164. {
  165. e = e->zMextSibling();
  166. i--;
  167. }
  168. if (e)
  169. {
  170. return e->getKey();
  171. }
  172. return 0;
  173. }
  174. bool hasValue(Text field) const override
  175. {
  176. EditableJsonElement* e = root->zFirstChildren();
  177. while (e)
  178. {
  179. if (e->getKey().isEqual(field))
  180. {
  181. return 1;
  182. }
  183. e = e->zMextSibling();
  184. }
  185. return 0;
  186. }
  187. };
  188. SyntaxError::SyntaxError(int start, int end, SyntaxErrorType errorType)
  189. : start(start),
  190. end(end),
  191. errorType(errorType)
  192. {}
  193. SyntaxError::SyntaxError()
  194. : start(-1),
  195. end(-1),
  196. errorType(SyntaxErrorType::OTHER)
  197. {}
  198. int SyntaxError::getStart() const
  199. {
  200. return start;
  201. }
  202. int SyntaxError::getEnd() const
  203. {
  204. return end;
  205. }
  206. SyntaxErrorType SyntaxError::getErrorType() const
  207. {
  208. return errorType;
  209. }
  210. Framework::JSON::ParserState::ParserState(
  211. JsonStructure parent, bool valueExpected)
  212. : index(0),
  213. keyStart(-1),
  214. keyEnd(0),
  215. valueStart(-1),
  216. valueEnd(0),
  217. errorStart(0),
  218. parent(parent),
  219. inString(0),
  220. escaped(0),
  221. colon(parent != JsonStructure::OBJECT),
  222. key(parent != JsonStructure::OBJECT),
  223. value(0),
  224. inValue(0),
  225. valueValidationNeeded(0),
  226. openingControllChar(0),
  227. closingControllChar(0),
  228. valueExpected(valueExpected)
  229. {}
  230. bool Framework::JSON::ParserState::next(
  231. const char* current, const char* next, SyntaxError& error)
  232. {
  233. if (current[0] == 0) return next == 0;
  234. bool stop = 0;
  235. if (current[0] == '{' && !inString)
  236. {
  237. stop = 1;
  238. if (!colon || value)
  239. {
  240. error.start = index;
  241. error.end = index + 1;
  242. error.errorType = SyntaxErrorType::UNEXPECTED_OBJECT_START;
  243. }
  244. value = 1;
  245. openingControllChar = current[0];
  246. }
  247. else if (current[0] == '[' && !inString)
  248. {
  249. stop = 1;
  250. if (!colon || value)
  251. {
  252. error.start = index;
  253. error.end = index + 1;
  254. error.errorType = SyntaxErrorType::UNEXPECTED_ARRAY_START;
  255. }
  256. value = 1;
  257. openingControllChar = current[0];
  258. }
  259. else if (current[0] == '}' && !inString)
  260. {
  261. stop = current[1] == ']' || current[1] == '}';
  262. if (!stop)
  263. {
  264. const char* n = current + 1;
  265. while (*n && (*n == ' ' || *n == '\t' || *n == '\r' || *n == '\n'))
  266. {
  267. n++;
  268. }
  269. stop = *n == ']' || *n == '}';
  270. if (!stop && next)
  271. {
  272. n = next;
  273. while (
  274. *n && (*n == ' ' || *n == '\t' || *n == '\r' || *n == '\n'))
  275. {
  276. n++;
  277. }
  278. stop = *n == ']' || *n == '}';
  279. }
  280. }
  281. if (parent != JsonStructure::OBJECT || (!value && valueExpected))
  282. {
  283. error.start = index;
  284. error.end = index + 1;
  285. error.errorType = SyntaxErrorType::UNEXPECTED_END_OF_OBJECT;
  286. }
  287. value = 1;
  288. closingControllChar = current[0];
  289. }
  290. else if (current[0] == ']' && !inString)
  291. {
  292. stop = current[1] == ']' || current[1] == '}';
  293. if (!stop)
  294. {
  295. const char* n = current + 1;
  296. while (*n && (*n == ' ' || *n == '\t' || *n == '\r' || *n == '\n'))
  297. {
  298. n++;
  299. }
  300. stop = *n == ']' || *n == '}';
  301. if (!stop && next)
  302. {
  303. n = next;
  304. while (
  305. *n && (*n == ' ' || *n == '\t' || *n == '\r' || *n == '\n'))
  306. {
  307. n++;
  308. }
  309. stop = *n == ']' || *n == '}';
  310. }
  311. }
  312. if (parent != JsonStructure::ARRAY || (!value && valueExpected))
  313. {
  314. error.start = index;
  315. error.end = index + 1;
  316. error.errorType = SyntaxErrorType::UNEXPECTED_END_OF_ARRAY;
  317. }
  318. value = 1;
  319. closingControllChar = current[0];
  320. }
  321. else if (current[0] == '"' && !escaped)
  322. {
  323. inString = !inString;
  324. if (inString)
  325. {
  326. if (!key)
  327. {
  328. keyStart = index + 1;
  329. }
  330. else if (colon && !value)
  331. {
  332. valueStart = index;
  333. }
  334. else
  335. {
  336. error.start = index;
  337. error.end = index + 1;
  338. error.errorType = SyntaxErrorType::UNEXPECTED_START_OF_STRING;
  339. }
  340. }
  341. else
  342. {
  343. if (!key)
  344. {
  345. keyEnd = index;
  346. key = 1;
  347. }
  348. else if (colon && !value)
  349. {
  350. valueEnd = index + 1;
  351. value = 1;
  352. }
  353. }
  354. }
  355. else if (current[0] == '\\' && inString)
  356. {
  357. escaped = !escaped;
  358. }
  359. else if (current[0] == ':' && !inString)
  360. {
  361. if (key && !colon)
  362. {
  363. colon = 1;
  364. }
  365. else
  366. {
  367. error.start = index;
  368. error.end = index + 1;
  369. error.errorType = SyntaxErrorType::UNEXPECTED_COLON;
  370. }
  371. }
  372. else if (current[0] == ',' && !inString)
  373. {
  374. stop = 1;
  375. if (!value)
  376. {
  377. error.start = index;
  378. error.end = index + 1;
  379. error.errorType = SyntaxErrorType::UNEXPECTED_COMMA;
  380. }
  381. }
  382. else
  383. {
  384. escaped = 0;
  385. if (!inString && current[0] != ' ' && current[0] != '\t'
  386. && current[0] != '\n' && current[0] != '\r')
  387. {
  388. if (colon && !value)
  389. {
  390. if (!inValue)
  391. {
  392. valueStart = index;
  393. inValue = 1;
  394. }
  395. }
  396. else
  397. {
  398. if (!inValue)
  399. {
  400. errorStart = index;
  401. inValue = 1;
  402. }
  403. }
  404. if (inValue
  405. && (current[1] == ':' || current[1] == '"' || current[1] == '{'
  406. || current[1] == '[' || current[1] == ']'
  407. || current[1] == '}' || current[1] == ','))
  408. {
  409. inValue = 0;
  410. if (colon && !value)
  411. {
  412. value = 1;
  413. valueEnd = index + 1;
  414. valueValidationNeeded = 1;
  415. }
  416. else
  417. {
  418. error.start = errorStart;
  419. error.end = index;
  420. error.errorType = SyntaxErrorType::UNEXPECTED_VALUE;
  421. }
  422. }
  423. }
  424. else if (inValue)
  425. {
  426. inValue = 0;
  427. if (colon && !value)
  428. {
  429. valueEnd = index;
  430. value = 1;
  431. valueValidationNeeded = 1;
  432. }
  433. else
  434. {
  435. error.start = errorStart;
  436. error.end = index;
  437. error.errorType = SyntaxErrorType::UNEXPECTED_VALUE;
  438. }
  439. }
  440. }
  441. index++;
  442. return stop;
  443. }
  444. EditableJsonElement::EditableJsonElement(Regex::Automata<char>* valueValidator)
  445. : ReferenceCounter(),
  446. content(),
  447. valueStart(-1),
  448. keyStart(-1),
  449. valueValidator(valueValidator),
  450. newLineCount(0)
  451. {
  452. children = 0;
  453. siblings = 0;
  454. lastChild = 0;
  455. parent = 0;
  456. hidden = false;
  457. parentStructure = JsonStructure::VALUE;
  458. }
  459. Framework::JSON::EditableJsonElement::EditableJsonElement(
  460. Regex::Automata<char>* valueValidator, const char* content)
  461. : EditableJsonElement(valueValidator)
  462. {
  463. setContent(content);
  464. valueValidator->release();
  465. }
  466. EditableJsonElement::~EditableJsonElement()
  467. {
  468. if (children)
  469. {
  470. children->release();
  471. }
  472. if (siblings)
  473. {
  474. siblings->release();
  475. }
  476. }
  477. void Framework::JSON::EditableJsonElement::setContentOnly(const char** content,
  478. int& startPos,
  479. Array<EditableJsonElement*>& afterList,
  480. Array<EditableJsonElement*>& deleteList)
  481. {
  482. errors.clear();
  483. int start = startPos;
  484. EditableJsonElement* before = zBefore(0, false, 0);
  485. ParserState p(parentStructure,
  486. before
  487. ? before->getContent()[before->getContent().getLength() - 1] == ','
  488. : 0);
  489. SyntaxError error;
  490. key = "";
  491. value = "";
  492. this->content = "";
  493. while (true)
  494. {
  495. const char* current = (*content) + startPos++;
  496. const char* next = 0;
  497. if (afterList.getEntryCount())
  498. {
  499. next = afterList.get(0)->getContent();
  500. }
  501. if (p.next(current, next, error))
  502. {
  503. break;
  504. }
  505. if (error.getErrorType() != SyntaxErrorType::OTHER)
  506. {
  507. errors.add(error);
  508. error = SyntaxError();
  509. }
  510. if (!*current)
  511. {
  512. startPos--;
  513. }
  514. if (!(*content)[startPos])
  515. {
  516. if (!afterList.getEntryCount())
  517. {
  518. break;
  519. }
  520. else
  521. {
  522. if (p.keyStart >= 0
  523. && (p.keyEnd < p.keyStart || p.keyEnd + start >= 0)
  524. && parentStructure == JsonStructure::OBJECT)
  525. {
  526. int s = p.keyStart + start < 0 ? 0 : p.keyStart + start;
  527. int e = p.keyEnd < p.keyStart ? p.index : p.keyEnd + start;
  528. key += Text(*content, s, e - s);
  529. }
  530. if (p.valueStart >= 0
  531. && (p.valueEnd < p.valueStart || p.valueEnd + start >= 0)
  532. && parentStructure == JsonStructure::OBJECT)
  533. {
  534. int s = p.valueStart + start < 0 ? 0 : p.valueStart + start;
  535. int e = p.valueEnd < p.valueStart ? p.index + start
  536. : p.valueEnd + start;
  537. value += Text(*content, s, e - s);
  538. }
  539. this->content += Text(*content,
  540. start < 0 ? 0 : start,
  541. start < 0 ? startPos : startPos - start);
  542. EditableJsonElement* next = afterList.get(0);
  543. int i = 0;
  544. afterList.remove(i);
  545. if (next->children)
  546. {
  547. afterList.add(next->children, i);
  548. i++;
  549. }
  550. if (next->siblings)
  551. {
  552. afterList.add(next->siblings, i);
  553. }
  554. next->disconnect();
  555. deleteList.add(next);
  556. *content = next->getContent();
  557. startPos = 0;
  558. start = -p.index;
  559. }
  560. }
  561. }
  562. if (error.getErrorType() != SyntaxErrorType::OTHER)
  563. {
  564. errors.add(error);
  565. error = SyntaxError();
  566. }
  567. if (p.keyStart >= 0 && (p.keyEnd < p.keyStart || p.keyEnd + start >= 0)
  568. && parentStructure == JsonStructure::OBJECT)
  569. {
  570. int s = p.keyStart + start < 0 ? 0 : p.keyStart + start;
  571. int e = p.keyEnd < p.keyStart ? p.index : p.keyEnd + start;
  572. key += Text(*content, s, e - s);
  573. }
  574. if (p.valueStart >= 0
  575. && (p.valueEnd < p.valueStart || p.valueEnd + start >= 0))
  576. {
  577. int s = p.valueStart + start < 0 ? 0 : p.valueStart + start;
  578. int e
  579. = p.valueEnd < p.valueStart ? p.index + start : p.valueEnd + start;
  580. value += Text(*content, s, e - s);
  581. }
  582. if (p.valueValidationNeeded)
  583. {
  584. if (!isValueValid(value, 0, value.getLength()))
  585. {
  586. errors.add(SyntaxError(
  587. p.valueStart, p.valueEnd, SyntaxErrorType::INVALID_VALUE));
  588. }
  589. }
  590. this->content += Text(*content,
  591. start < 0 ? 0 : start,
  592. start < 0 ? startPos : startPos - start);
  593. newLineCount = this->content.countOf('\n');
  594. openingControllChar = p.openingControllChar;
  595. closingControllChar = p.closingControllChar;
  596. valueStart = p.valueStart;
  597. keyStart = p.keyStart;
  598. if ((*content)[startPos]
  599. && ((*content)[startPos - 1] == '[' || (*content)[startPos - 1] == '{'))
  600. {
  601. EditableJsonElement* next = new EditableJsonElement(valueValidator);
  602. addChildren(next);
  603. next->setContentRecursive(content, startPos, afterList, deleteList);
  604. }
  605. // TODO: set parent structure of children
  606. // TODO: update tree
  607. }
  608. void Framework::JSON::EditableJsonElement::setContentRecursive(
  609. const char** content,
  610. int& startPos,
  611. Array<EditableJsonElement*>& afterList,
  612. Array<EditableJsonElement*>& deleteList)
  613. {
  614. setContentOnly(content, startPos, afterList, deleteList);
  615. char lastControl = closingControllChar;
  616. while ((*content)[startPos]
  617. && !((lastControl == ']' && parentStructure == JsonStructure::ARRAY)
  618. || (lastControl == '}'
  619. && parentStructure == JsonStructure::OBJECT)))
  620. {
  621. if (closingControllChar == ']'
  622. && parentStructure == JsonStructure::ARRAY)
  623. {
  624. break;
  625. }
  626. EditableJsonElement* next = new EditableJsonElement(valueValidator);
  627. addSibling(next);
  628. next->setContentOnly(content, startPos, afterList, deleteList);
  629. lastControl = next->getClosingControllChar();
  630. }
  631. }
  632. void Framework::JSON::EditableJsonElement::format(
  633. int indent, bool insertNewLine)
  634. {
  635. Text content = this->content;
  636. content.removeWhitespaceAfter(0);
  637. int index = 0;
  638. if (insertNewLine)
  639. {
  640. content.insert(0, "\n");
  641. for (int i = 0; i < indent; i++)
  642. {
  643. content.insert(1, " ");
  644. }
  645. index = indent * 4 + 1;
  646. }
  647. bool keyStart = false;
  648. bool keyEnd = false;
  649. bool colonFound = false;
  650. bool valueStart = false;
  651. bool valueEnd = false;
  652. bool escaped = false;
  653. for (int i = index; i < content.getLength(); i++)
  654. {
  655. if ((content[i] == '{' || content[i] == '[' || content[i] == ',')
  656. && !(keyStart && !keyEnd) && !(valueStart && !valueEnd))
  657. {
  658. if (content[i] == ',')
  659. {
  660. i -= content.removeWhitespaceBefore(i);
  661. }
  662. content.removeWhitespaceAfter(i + 1);
  663. }
  664. else if ((content[i] == '}' || content[i] == ']')
  665. && !(keyStart && !keyEnd) && !(valueStart && !valueEnd))
  666. {
  667. content.removeWhitespaceAfter(i + 1);
  668. if (!parent || parent->children != this)
  669. {
  670. i -= content.removeWhitespaceBefore(i);
  671. content.insert(i, '\n');
  672. for (int j = 0; j < indent - 1; j++)
  673. {
  674. content.insert(i + 1, " ");
  675. }
  676. i += 1 + (indent > 0 ? (indent - 1) * 4 : 0);
  677. }
  678. else
  679. {
  680. i -= content.removeWhitespaceBefore(i);
  681. }
  682. }
  683. else if (content[i] == '"' && !escaped)
  684. { // format whitespaces arround values and keys
  685. if (!keyStart)
  686. {
  687. keyStart = true;
  688. }
  689. else
  690. {
  691. if (!keyEnd)
  692. {
  693. keyEnd = true;
  694. content.removeWhitespaceAfter(i + 1);
  695. }
  696. else
  697. {
  698. if (colonFound)
  699. {
  700. if (!valueStart)
  701. {
  702. valueStart = true;
  703. }
  704. else if (!valueEnd)
  705. {
  706. valueEnd = true;
  707. content.removeWhitespaceAfter(i + 1);
  708. }
  709. }
  710. }
  711. }
  712. }
  713. else if (content[i] == ':' && !(keyStart && !keyEnd)
  714. && !(valueStart && !valueEnd))
  715. {
  716. if (!colonFound && keyEnd)
  717. {
  718. colonFound = true;
  719. content.removeWhitespaceAfter(i + 1);
  720. content.insert(i + 1, " ");
  721. }
  722. }
  723. else if (content[i] == '\t' && !(keyStart && !keyEnd)
  724. && !(valueStart && !valueEnd))
  725. {
  726. content.remove(i, 1);
  727. i--;
  728. }
  729. else if (content[i] == '\\')
  730. {
  731. if ((keyStart && !keyEnd) || (valueStart && !valueEnd))
  732. {
  733. escaped = !escaped;
  734. }
  735. }
  736. else
  737. {
  738. escaped = false;
  739. }
  740. }
  741. int count = 0;
  742. Array<EditableJsonElement*> empty;
  743. const char* tmp = content.getText();
  744. setContentOnly(&tmp, count, empty, empty);
  745. if (count < content.getLength())
  746. {
  747. throw "Illegal State Exception: tree changed after formatting";
  748. }
  749. if (children)
  750. {
  751. children->formatRecursive(indent + 1, true);
  752. }
  753. }
  754. void Framework::JSON::EditableJsonElement::formatRecursive(
  755. int indent, bool insertNewLine)
  756. {
  757. format(indent, insertNewLine);
  758. EditableJsonElement* tmp = siblings;
  759. while (tmp)
  760. {
  761. tmp->format(indent, true);
  762. tmp = tmp->siblings;
  763. }
  764. }
  765. bool Framework::JSON::EditableJsonElement::isValueValid(
  766. const char* content, int start, int end)
  767. {
  768. Text value(content, start, end - start);
  769. if (value.isEqual("null") || value.isEqual("true")
  770. || value.isEqual("false"))
  771. {
  772. return true;
  773. }
  774. RCArray<Regex::Result>* results
  775. = valueValidator->match(value, value.getLength());
  776. bool ok = results->getEntryCount() == 1;
  777. results->release();
  778. return ok;
  779. }
  780. void Framework::JSON::EditableJsonElement::setContent(
  781. const char* content, int& startPos, Array<EditableJsonElement*>& afterList)
  782. {
  783. if (children)
  784. {
  785. afterList.add(children);
  786. }
  787. if (siblings)
  788. {
  789. afterList.add(siblings);
  790. }
  791. EditableJsonElement* p = parent;
  792. EditableJsonElement* last = this;
  793. while (p)
  794. {
  795. if (p->siblings)
  796. {
  797. afterList.add(p->siblings);
  798. p->siblings = 0;
  799. }
  800. p->lastChild = last;
  801. last = p;
  802. p = p->parent;
  803. }
  804. lastChild = 0;
  805. children = 0;
  806. siblings = 0;
  807. if (parent)
  808. {
  809. parent->lastChild = this;
  810. }
  811. Array<EditableJsonElement*> deleteList;
  812. setContentRecursive(&content, startPos, afterList, deleteList);
  813. if (content[startPos])
  814. {
  815. EditableJsonElement* next = new EditableJsonElement(valueValidator);
  816. if (parent)
  817. {
  818. parent->addSibling(next);
  819. }
  820. else
  821. {
  822. addSibling(next);
  823. }
  824. next->setContent(content, startPos, afterList);
  825. }
  826. else if (afterList.getEntryCount())
  827. {
  828. if (openingControllChar)
  829. {
  830. while (afterList.getEntryCount()
  831. && (!lastChild
  832. || !((openingControllChar == '['
  833. && lastChild->closingControllChar == ']')
  834. || (openingControllChar == '{'
  835. && lastChild->closingControllChar == '}'))))
  836. {
  837. EditableJsonElement* next = afterList.get(0);
  838. afterList.remove(0);
  839. EditableJsonElement* end = next;
  840. while (end)
  841. {
  842. if ((end->closingControllChar == ']'
  843. && this->openingControllChar == '[')
  844. || (end->closingControllChar == '}'
  845. && this->openingControllChar == '{'))
  846. {
  847. if (end->siblings)
  848. {
  849. afterList.add(end->siblings, 0);
  850. end->siblings = 0;
  851. }
  852. break;
  853. }
  854. end = end->siblings;
  855. }
  856. addChildren(next);
  857. next->checkSyntax();
  858. while (next->siblings)
  859. {
  860. next = next->siblings;
  861. next->checkSyntax();
  862. }
  863. }
  864. }
  865. EditableJsonElement* p = parent;
  866. EditableJsonElement* c = this;
  867. while (afterList.getEntryCount())
  868. {
  869. if (p)
  870. {
  871. if (p->siblings)
  872. {
  873. afterList.add(p->siblings);
  874. p->siblings = 0;
  875. if (p->parent)
  876. {
  877. p->parent->lastChild = p;
  878. }
  879. }
  880. while (
  881. afterList.getEntryCount()
  882. && (!p->lastChild
  883. || !((p->openingControllChar == '['
  884. && p->lastChild->closingControllChar == ']')
  885. || (p->openingControllChar == '{'
  886. && p->lastChild->closingControllChar == '}'))))
  887. {
  888. EditableJsonElement* next = afterList.get(0);
  889. afterList.remove(0);
  890. EditableJsonElement* end = next;
  891. while (end)
  892. {
  893. if ((end->closingControllChar == ']'
  894. && p->openingControllChar == '[')
  895. || (end->closingControllChar == '}'
  896. && p->openingControllChar == '{'))
  897. {
  898. if (end->siblings)
  899. {
  900. afterList.add(end->siblings, 0);
  901. end->siblings = 0;
  902. }
  903. break;
  904. }
  905. end = end->siblings;
  906. }
  907. p->addChildren(next);
  908. next->checkSyntax();
  909. while (next->siblings)
  910. {
  911. next = next->siblings;
  912. next->checkSyntax();
  913. }
  914. }
  915. c = p;
  916. p = p->parent;
  917. }
  918. else
  919. {
  920. while (afterList.getEntryCount())
  921. {
  922. EditableJsonElement* next = afterList.get(0);
  923. afterList.remove(0);
  924. c->addSibling(next);
  925. next->checkSyntax();
  926. while (next->siblings)
  927. {
  928. next = next->siblings;
  929. next->checkSyntax();
  930. }
  931. }
  932. }
  933. }
  934. }
  935. for (EditableJsonElement* e : deleteList)
  936. {
  937. e->release();
  938. }
  939. }
  940. void Framework::JSON::EditableJsonElement::checkSyntax()
  941. {
  942. errors.clear();
  943. EditableJsonElement* before = zBefore(0, false, 0);
  944. ParserState p(parentStructure,
  945. before
  946. ? before->getContent()[before->getContent().getLength() - 1] == ','
  947. : 0);
  948. SyntaxError error;
  949. key = "";
  950. value = "";
  951. int pos = 0;
  952. while (pos < content.getLength())
  953. {
  954. const char* current = content.getText() + pos++;
  955. p.next(current, 0, error);
  956. if (error.getErrorType() != SyntaxErrorType::OTHER)
  957. {
  958. errors.add(error);
  959. error = SyntaxError();
  960. }
  961. }
  962. if (error.getErrorType() != SyntaxErrorType::OTHER)
  963. {
  964. errors.add(error);
  965. error = SyntaxError();
  966. }
  967. if (p.keyStart >= 0 && p.keyEnd >= p.keyStart
  968. && parentStructure == JsonStructure::OBJECT)
  969. {
  970. key += Text(content, p.keyStart, p.keyEnd - p.keyStart);
  971. }
  972. if (p.valueStart >= 0 && p.valueEnd >= p.valueStart)
  973. {
  974. value += Text(content, p.valueStart, p.valueEnd - p.valueStart);
  975. }
  976. if (p.valueValidationNeeded)
  977. {
  978. if (!isValueValid(value, 0, value.getLength()))
  979. {
  980. errors.add(SyntaxError(
  981. p.valueStart, p.valueEnd, SyntaxErrorType::INVALID_VALUE));
  982. }
  983. }
  984. openingControllChar = p.openingControllChar;
  985. closingControllChar = p.closingControllChar;
  986. valueStart = p.valueStart;
  987. keyStart = p.keyStart;
  988. }
  989. void EditableJsonElement::setContent(const char* content)
  990. {
  991. Array<EditableJsonElement*> afterList;
  992. int p = 0;
  993. setContent(content, p, afterList);
  994. }
  995. void EditableJsonElement::addChildren(EditableJsonElement* content)
  996. {
  997. JsonStructure currentStructure
  998. = openingControllChar == '['
  999. ? JsonStructure::ARRAY
  1000. : (openingControllChar == '{' ? JsonStructure::OBJECT
  1001. : JsonStructure::VALUE);
  1002. if (this->children)
  1003. {
  1004. this->lastChild->siblings = content;
  1005. }
  1006. else
  1007. {
  1008. this->children = content;
  1009. this->lastChild = content;
  1010. }
  1011. if (content->parent != this)
  1012. {
  1013. content->parent = this;
  1014. content->parentStructure = currentStructure;
  1015. for (; lastChild->siblings; lastChild = lastChild->siblings)
  1016. {
  1017. lastChild->siblings->parent = this;
  1018. lastChild->siblings->parentStructure = currentStructure;
  1019. }
  1020. }
  1021. while (lastChild->siblings)
  1022. {
  1023. lastChild = lastChild->siblings;
  1024. }
  1025. }
  1026. void Framework::JSON::EditableJsonElement::setNextSibling(
  1027. EditableJsonElement* content)
  1028. {
  1029. this->siblings = content;
  1030. EditableJsonElement* current = this;
  1031. if (content->parent != parent)
  1032. {
  1033. for (; current->siblings; current = current->siblings)
  1034. {
  1035. current->siblings->parent = parent;
  1036. current->siblings->parentStructure = parentStructure;
  1037. }
  1038. }
  1039. if (parent)
  1040. {
  1041. parent->lastChild = current;
  1042. while (parent->lastChild->siblings)
  1043. {
  1044. parent->lastChild = parent->lastChild->siblings;
  1045. }
  1046. }
  1047. }
  1048. void EditableJsonElement::addSibling(EditableJsonElement* content)
  1049. {
  1050. EditableJsonElement* current = parent ? parent->lastChild : this;
  1051. while (current->siblings)
  1052. {
  1053. current = current->siblings;
  1054. }
  1055. current->siblings = content;
  1056. if (content->parent != parent)
  1057. {
  1058. for (; current->siblings; current = current->siblings)
  1059. {
  1060. current->siblings->parent = parent;
  1061. current->siblings->parentStructure = parentStructure;
  1062. }
  1063. }
  1064. if (parent)
  1065. {
  1066. parent->lastChild = current;
  1067. while (parent->lastChild->siblings)
  1068. {
  1069. parent->lastChild = parent->lastChild->siblings;
  1070. }
  1071. }
  1072. }
  1073. void Framework::JSON::EditableJsonElement::format()
  1074. {
  1075. int indent = getIndent();
  1076. formatRecursive(indent, false);
  1077. }
  1078. const Text& Framework::JSON::EditableJsonElement::getContent()
  1079. {
  1080. return content;
  1081. }
  1082. Text Framework::JSON::EditableJsonElement::getRecursiveContent()
  1083. {
  1084. Text result
  1085. = content + (children ? children->getRecursiveContent() : Text(""));
  1086. EditableJsonElement* s = siblings;
  1087. while (s)
  1088. {
  1089. result += s->content
  1090. + (s->children ? s->children->getRecursiveContent() : Text(""));
  1091. s = s->siblings;
  1092. }
  1093. return result;
  1094. }
  1095. int Framework::JSON::EditableJsonElement::lineCount()
  1096. {
  1097. return newLineCount;
  1098. }
  1099. bool Framework::JSON::EditableJsonElement::lineCount(bool includeChildren,
  1100. bool includeSiblings,
  1101. EditableJsonElement* stop,
  1102. int& count)
  1103. {
  1104. count += newLineCount;
  1105. if (this == stop)
  1106. {
  1107. return false;
  1108. }
  1109. if (includeChildren && children)
  1110. {
  1111. if (!children->lineCount(true, true, stop, count))
  1112. {
  1113. return false;
  1114. }
  1115. }
  1116. if (includeSiblings && siblings)
  1117. {
  1118. return siblings->lineCount(true, true, stop, count);
  1119. };
  1120. return true;
  1121. }
  1122. bool Framework::JSON::EditableJsonElement::isHidden()
  1123. {
  1124. return hidden;
  1125. }
  1126. void Framework::JSON::EditableJsonElement::setHidden(bool hidden)
  1127. {
  1128. this->hidden = hidden;
  1129. }
  1130. EditableJsonElement* Framework::JSON::EditableJsonElement::zParent()
  1131. {
  1132. return parent;
  1133. }
  1134. EditableJsonElement* Framework::JSON::EditableJsonElement::zMextSibling()
  1135. {
  1136. return siblings;
  1137. }
  1138. EditableJsonElement* Framework::JSON::EditableJsonElement::zFirstChildren()
  1139. {
  1140. return children;
  1141. }
  1142. EditableJsonElement* Framework::JSON::EditableJsonElement::zLastChildren()
  1143. {
  1144. return lastChild;
  1145. }
  1146. char Framework::JSON::EditableJsonElement::getOpeningControllChar()
  1147. {
  1148. return openingControllChar;
  1149. }
  1150. char Framework::JSON::EditableJsonElement::getClosingControllChar()
  1151. {
  1152. return closingControllChar;
  1153. }
  1154. Text& Framework::JSON::EditableJsonElement::getKey()
  1155. {
  1156. return key;
  1157. }
  1158. Text& Framework::JSON::EditableJsonElement::getValue()
  1159. {
  1160. return value;
  1161. }
  1162. void Framework::JSON::EditableJsonElement::disconnect()
  1163. {
  1164. children = 0;
  1165. lastChild = 0;
  1166. siblings = 0;
  1167. parent = 0;
  1168. }
  1169. void Framework::JSON::EditableJsonElement::removeChild(
  1170. EditableJsonElement* zElement)
  1171. {
  1172. // TODO: needed?
  1173. if (children == zElement)
  1174. {
  1175. children = 0;
  1176. lastChild = 0;
  1177. }
  1178. else
  1179. {
  1180. EditableJsonElement* current = children;
  1181. while (current)
  1182. {
  1183. if (current->siblings == zElement)
  1184. {
  1185. current->siblings = 0;
  1186. if (current->parent)
  1187. {
  1188. current->parent->lastChild = current;
  1189. }
  1190. break;
  1191. }
  1192. current = current->siblings;
  1193. }
  1194. }
  1195. }
  1196. void Framework::JSON::EditableJsonElement::getWordBounds(
  1197. int pos, int* left, int* right)
  1198. {
  1199. bool escaped = false;
  1200. int lastStartStr = -1;
  1201. int lastBlank = 0;
  1202. bool found = false;
  1203. int len = content.getLength();
  1204. for (int i = 0; i < len; i++)
  1205. {
  1206. if ((content[i] == ' ' || content[i] == ',' || content[i] == '\n')
  1207. && lastStartStr == -1)
  1208. {
  1209. if (i >= pos)
  1210. {
  1211. if (left)
  1212. {
  1213. *left = lastBlank;
  1214. }
  1215. if (right)
  1216. {
  1217. *right = i;
  1218. }
  1219. found = true;
  1220. break;
  1221. }
  1222. lastBlank = i + 1;
  1223. }
  1224. else if (content[i] == '"')
  1225. {
  1226. if (lastStartStr == -1)
  1227. {
  1228. lastStartStr = i + 1;
  1229. if (i >= pos)
  1230. {
  1231. if (left)
  1232. {
  1233. *left = i;
  1234. }
  1235. if (right)
  1236. {
  1237. *right = i + 1;
  1238. }
  1239. found = true;
  1240. break;
  1241. }
  1242. }
  1243. else if (!escaped)
  1244. {
  1245. if (i >= pos)
  1246. {
  1247. if (left)
  1248. {
  1249. *left = lastStartStr;
  1250. }
  1251. if (right)
  1252. {
  1253. *right = i;
  1254. }
  1255. found = true;
  1256. break;
  1257. }
  1258. lastStartStr = -1;
  1259. }
  1260. }
  1261. else if (content[i] == '\\' && lastStartStr >= 0)
  1262. {
  1263. escaped = !escaped;
  1264. }
  1265. else
  1266. {
  1267. escaped = false;
  1268. }
  1269. }
  1270. if (!found && lastBlank >= 0)
  1271. {
  1272. if (left)
  1273. {
  1274. *left = lastBlank;
  1275. }
  1276. if (right)
  1277. {
  1278. *right = len;
  1279. }
  1280. }
  1281. }
  1282. bool Framework::JSON::EditableJsonElement::hasError(int column) const
  1283. {
  1284. for (const SyntaxError& error : errors)
  1285. {
  1286. if (error.getStart() <= column && error.getEnd() >= column)
  1287. {
  1288. return true;
  1289. }
  1290. }
  1291. return false;
  1292. }
  1293. SyntaxError Framework::JSON::EditableJsonElement::getError(int column) const
  1294. {
  1295. for (const SyntaxError& error : errors)
  1296. {
  1297. if (error.getStart() <= column && error.getEnd() >= column)
  1298. {
  1299. return error;
  1300. }
  1301. }
  1302. return SyntaxError();
  1303. }
  1304. bool Framework::JSON::EditableJsonElement::hasError() const
  1305. {
  1306. return errors.getEntryCount() > 0;
  1307. }
  1308. void Framework::JSON::EditableJsonElement::makeVisible()
  1309. {
  1310. EditableJsonElement* current = parent;
  1311. while (current)
  1312. {
  1313. current->setHidden(false);
  1314. current = current->zParent();
  1315. }
  1316. }
  1317. bool Framework::JSON::EditableJsonElement::isVisible() const
  1318. {
  1319. EditableJsonElement* current = parent;
  1320. while (current)
  1321. {
  1322. if (current->isHidden())
  1323. {
  1324. return false;
  1325. }
  1326. current = current->zParent();
  1327. }
  1328. return true;
  1329. }
  1330. int Framework::JSON::EditableJsonElement::getIndent() const
  1331. {
  1332. int indent = 0;
  1333. EditableJsonElement* tmp = parent;
  1334. while (tmp)
  1335. {
  1336. indent++;
  1337. tmp = tmp->parent;
  1338. }
  1339. return indent;
  1340. }
  1341. int Framework::JSON::EditableJsonElement::getColor(int index)
  1342. {
  1343. int color = 0xFFA0A0A0;
  1344. if (hasError(index))
  1345. {
  1346. color = 0xFFFF3030;
  1347. }
  1348. else if (keyStart >= 0 && index >= keyStart - 1
  1349. && index < keyStart + key.getLength() + 1)
  1350. {
  1351. color = 0xFF7070C0;
  1352. }
  1353. else if (valueStart >= 0 && index >= valueStart
  1354. && index < valueStart + value.getLength())
  1355. {
  1356. if (value[0] == '"')
  1357. {
  1358. color = 0xFF70C070;
  1359. }
  1360. else if (value.isEqual("null"))
  1361. {
  1362. color = 0xFFC0A070;
  1363. }
  1364. else if (value.isEqual("true") || value.isEqual("false"))
  1365. {
  1366. color = 0xFFC070C0;
  1367. }
  1368. else
  1369. {
  1370. color = 0xFF70C0C0;
  1371. }
  1372. }
  1373. return color;
  1374. }
  1375. void Framework::JSON::EditableJsonElement::removeUntil(
  1376. int startIndex, EditableJsonElement* end, int endIndex)
  1377. {
  1378. Text* result = content.getTeilText(0, startIndex);
  1379. if (end == this)
  1380. {
  1381. Text* tmp = content.getTeilText(endIndex);
  1382. result->append(*tmp);
  1383. tmp->release();
  1384. setContent(*result);
  1385. result->release();
  1386. return;
  1387. }
  1388. else
  1389. {
  1390. Array<EditableJsonElement*> toDelete;
  1391. Stack<EditableJsonElement*> stack;
  1392. if (siblings)
  1393. {
  1394. stack.push(siblings);
  1395. }
  1396. if (children)
  1397. {
  1398. stack.push(children);
  1399. }
  1400. if (!stack.getSize())
  1401. {
  1402. EditableJsonElement* p = parent;
  1403. while (p)
  1404. {
  1405. if (p->siblings)
  1406. {
  1407. stack.push(p->siblings);
  1408. break;
  1409. }
  1410. p = p->parent;
  1411. }
  1412. }
  1413. if (stack.getSize())
  1414. {
  1415. EditableJsonElement* next = stack.pop();
  1416. while (true)
  1417. {
  1418. if (next->siblings)
  1419. {
  1420. stack.push(siblings);
  1421. }
  1422. if (next->children)
  1423. {
  1424. stack.push(children);
  1425. }
  1426. if (!stack.getSize() && next != end)
  1427. {
  1428. EditableJsonElement* p = next->parent;
  1429. while (p)
  1430. {
  1431. if (p->siblings)
  1432. {
  1433. stack.push(p->siblings);
  1434. break;
  1435. }
  1436. p = p->parent;
  1437. }
  1438. }
  1439. if (next->parent)
  1440. {
  1441. next->parent->removeChild(next);
  1442. }
  1443. else
  1444. {
  1445. EditableJsonElement* p = this;
  1446. while (p->parent)
  1447. {
  1448. p = p->parent;
  1449. }
  1450. while (p->siblings != next)
  1451. {
  1452. p = p->siblings;
  1453. }
  1454. p->siblings = 0;
  1455. }
  1456. toDelete.add(next);
  1457. if (!stack.getSize())
  1458. {
  1459. next = 0;
  1460. break;
  1461. }
  1462. if (next == end)
  1463. {
  1464. break;
  1465. }
  1466. next = stack.pop();
  1467. }
  1468. if (next == end)
  1469. {
  1470. Text* tmp = next->content.getTeilText(endIndex);
  1471. result->append(*tmp);
  1472. }
  1473. for (EditableJsonElement* del : toDelete)
  1474. {
  1475. del->disconnect();
  1476. del->release();
  1477. }
  1478. Array<EditableJsonElement*> afterList;
  1479. while (stack.getSize())
  1480. {
  1481. afterList.add(stack.pop());
  1482. }
  1483. int p = 0;
  1484. setContent(*result, p, afterList);
  1485. }
  1486. else
  1487. {
  1488. setContent(*result);
  1489. result->release();
  1490. return;
  1491. }
  1492. }
  1493. }
  1494. EditableJsonElement* Framework::JSON::EditableJsonElement::zBefore(
  1495. EditableJsonElement* zRoot, bool onlyVisible, int* lineCount)
  1496. {
  1497. if (this == zRoot)
  1498. {
  1499. return 0;
  1500. }
  1501. EditableJsonElement* parent = this->parent;
  1502. EditableJsonElement* current = parent ? parent->children : zRoot;
  1503. EditableJsonElement* last = parent;
  1504. while (current)
  1505. {
  1506. if (current == this)
  1507. {
  1508. break;
  1509. }
  1510. last = current;
  1511. current = current->siblings;
  1512. }
  1513. if (last && last->children != this && (!last->hidden || !onlyVisible))
  1514. {
  1515. current = last->lastChild;
  1516. while (current)
  1517. {
  1518. last = current;
  1519. if (current->children)
  1520. {
  1521. if ((!current->hidden || !onlyVisible))
  1522. {
  1523. current = current->lastChild;
  1524. }
  1525. else if (*lineCount)
  1526. {
  1527. EditableJsonElement* e = current->children;
  1528. while (e)
  1529. {
  1530. int lc = 0;
  1531. e->lineCount(true, false, 0, lc);
  1532. *lineCount -= lc;
  1533. e = e->siblings;
  1534. }
  1535. break;
  1536. }
  1537. }
  1538. else
  1539. {
  1540. break;
  1541. }
  1542. }
  1543. }
  1544. else if (last && last->hidden && onlyVisible && lineCount)
  1545. {
  1546. EditableJsonElement* e = last->children;
  1547. while (e)
  1548. {
  1549. int lc = 0;
  1550. e->lineCount(true, false, 0, lc);
  1551. *lineCount -= lc;
  1552. e = e->siblings;
  1553. }
  1554. }
  1555. return last;
  1556. }
  1557. EditableJsonElement* Framework::JSON::EditableJsonElement::zAfter(
  1558. bool onlyVisible, int* lineCount)
  1559. {
  1560. EditableJsonElement* zElement = this;
  1561. if (zElement->children)
  1562. {
  1563. if (!zElement->hidden || !onlyVisible)
  1564. {
  1565. return zElement->children;
  1566. }
  1567. else if (lineCount)
  1568. {
  1569. EditableJsonElement* e = zElement->children;
  1570. while (e)
  1571. {
  1572. int lc = 0;
  1573. e->lineCount(true, false, 0, lc);
  1574. *lineCount += lc;
  1575. e = e->siblings;
  1576. }
  1577. }
  1578. }
  1579. if (zElement->siblings)
  1580. {
  1581. return zElement->siblings;
  1582. }
  1583. while (zElement->parent)
  1584. {
  1585. zElement = zElement->parent;
  1586. if (zElement->siblings)
  1587. {
  1588. return zElement->siblings;
  1589. }
  1590. }
  1591. return 0;
  1592. }
  1593. JsonEditor::JsonEditor(UIInit uiInit)
  1594. : DrawableBackground(),
  1595. uiInit(uiInit)
  1596. {
  1597. valueValidator = Regex::parse("^-?[0-9]+(\\.[0-9]+)?([eE][+-]?[0-9]+)?$");
  1598. validator = 0;
  1599. content = 0;
  1600. renderStart = {0, 0};
  1601. renderStartLine = 0;
  1602. lineCount = 0;
  1603. cursorPos.x = -1;
  1604. cursorPos.y = -1;
  1605. textCursor = {0, 0};
  1606. selectionStart = {0, 0};
  1607. renderStartOffset = 0;
  1608. selectionEnd = {0, 0};
  1609. textRenderer = new TextRenderer();
  1610. textRenderer->setFontSize(12);
  1611. textRenderer->setLineSpacing(2);
  1612. textRenderer->setCharSpacing(1);
  1613. pressed = 0;
  1614. time = 0;
  1615. drag = 0;
  1616. drawCursor = 0;
  1617. dragSart = {-1, -1};
  1618. pressedPos = {-1, -1};
  1619. dragStartPos = {0, -1};
  1620. timeSicePress = 0.0;
  1621. lastClickCursorPos = {0, 0};
  1622. tps = 0;
  1623. renderings = 0;
  1624. renderedLines = 0;
  1625. renderStopLine = 0;
  1626. errorDescription = uiInit.createTextField(uiInit.initParam);
  1627. errorDescription->setStyle(TextField::Style::Text | TextField::Style::Center
  1628. | TextField::Style::Border
  1629. | TextField::Style::Background
  1630. | TextField::Style::BAlpha);
  1631. errorDescription->removeStyle(TextField::Style::Visible);
  1632. errorDescription->setFontColor(0xFFFFFFFF);
  1633. errorDescription->setBorderColor(0xFFA0A0A0);
  1634. errorDescription->setBackgroundColor(0xA0000000);
  1635. hasSyntaxError = false;
  1636. }
  1637. JsonEditor::~JsonEditor()
  1638. {
  1639. if (content) content->release();
  1640. if (validator) validator->release();
  1641. textRenderer->release();
  1642. errorDescription->release();
  1643. }
  1644. void Framework::JSON::JsonEditor::doMouseEvent(MouseEvent& me, bool userRet)
  1645. {
  1646. LOCK({&cs});
  1647. rend |= cursorPos.x != me.mx || cursorPos.y != me.my;
  1648. cursorPos.x = me.mx;
  1649. cursorPos.y = me.my;
  1650. if (me.id == ME_RLeft)
  1651. {
  1652. pressedPos = cursorPos;
  1653. if (timeSicePress < 0.5)
  1654. {
  1655. if (lastClickCursorPos.line == textCursor.line
  1656. && lastClickCursorPos.column == textCursor.column
  1657. && lastClickCursorPos.line && lastClickCursorPos.column > 0)
  1658. {
  1659. EditableJsonElement* current = textCursor.line;
  1660. int start = 0;
  1661. int end = 0;
  1662. current->getWordBounds(textCursor.column, &start, &end);
  1663. selectionStart = {current, start};
  1664. selectionEnd = {current, end};
  1665. unifyPosition(selectionEnd);
  1666. textCursor = selectionEnd;
  1667. }
  1668. }
  1669. lastClickCursorPos = textCursor;
  1670. pressed = true;
  1671. drag = false;
  1672. rend = 1;
  1673. time = 0;
  1674. timeSicePress = 0.0;
  1675. }
  1676. if (me.id == ME_PLeft || (me.id == ME_Enter && getMouseState(M_Left)))
  1677. {
  1678. if (me.mx >= getInnerWidth() - 15)
  1679. {
  1680. int line = (int)(me.my / ((double)getInnerHeight() / lineCount));
  1681. if (line >= lineCount)
  1682. {
  1683. line = lineCount - 1;
  1684. }
  1685. scrollToLine(line + 1, Center);
  1686. }
  1687. if (!drag)
  1688. {
  1689. drag = true;
  1690. textCursor = getScreenPosition(cursorPos.x, cursorPos.y);
  1691. selectionStart = textCursor;
  1692. selectionEnd = textCursor;
  1693. dragSart = cursorPos;
  1694. dragStartPos = textCursor;
  1695. rend = 1;
  1696. time = 0;
  1697. }
  1698. }
  1699. if (me.id == ME_Move)
  1700. {
  1701. if (drag)
  1702. {
  1703. if (dragSart.x >= getInnerWidth() - 15)
  1704. {
  1705. int line
  1706. = (int)(me.my / ((double)getInnerHeight() / lineCount));
  1707. if (line >= lineCount)
  1708. {
  1709. line = lineCount - 1;
  1710. }
  1711. scrollToLine(line + 1, Center);
  1712. }
  1713. else
  1714. {
  1715. EditorPosition pos
  1716. = getScreenPosition(cursorPos.x, cursorPos.y);
  1717. if (pos.line == dragStartPos.line)
  1718. {
  1719. if (pos.column < dragStartPos.column)
  1720. {
  1721. selectionStart = pos;
  1722. selectionEnd = dragStartPos;
  1723. }
  1724. else if (pos.column > dragStartPos.column)
  1725. {
  1726. selectionStart = dragStartPos;
  1727. selectionEnd = pos;
  1728. }
  1729. }
  1730. else
  1731. {
  1732. if (cursorPos.y < dragSart.y)
  1733. {
  1734. selectionStart = pos;
  1735. selectionEnd = dragStartPos;
  1736. }
  1737. else if (cursorPos.y > dragSart.y)
  1738. {
  1739. selectionStart = dragStartPos;
  1740. selectionEnd = pos;
  1741. }
  1742. }
  1743. textCursor = pos;
  1744. }
  1745. }
  1746. }
  1747. if (me.id == ME_UScroll)
  1748. {
  1749. if (renderStart.column > 0)
  1750. {
  1751. const char* content = renderStart.line->getContent();
  1752. int index = renderStart.column - 2;
  1753. while (index > 0 && content[index] != '\n')
  1754. {
  1755. index--;
  1756. }
  1757. if (content[index] == '\n')
  1758. {
  1759. renderStart.column = index + 1;
  1760. rend = 1;
  1761. renderStartLine--;
  1762. unifyPosition(renderStart);
  1763. return;
  1764. }
  1765. }
  1766. while (true)
  1767. {
  1768. EditableJsonElement* next
  1769. = renderStart.line->zBefore(content, true, &renderStartLine);
  1770. if (next)
  1771. {
  1772. renderStart.line = next;
  1773. const char* content = next->getContent();
  1774. int index = next->getContent().getLength() - 1;
  1775. while (index > 0 && content[index] != '\n')
  1776. {
  1777. index--;
  1778. }
  1779. if (content[index] == '\n')
  1780. {
  1781. renderStart.column = index + 1;
  1782. rend = 1;
  1783. renderStartLine--;
  1784. unifyPosition(renderStart);
  1785. return;
  1786. }
  1787. }
  1788. else
  1789. {
  1790. renderStart.column = 0;
  1791. rend = 1;
  1792. renderStartLine = 1;
  1793. return;
  1794. }
  1795. }
  1796. }
  1797. if (me.id == ME_DScroll)
  1798. {
  1799. const char* content = renderStart.line->getContent();
  1800. int length = renderStart.line->getContent().getLength();
  1801. int index = renderStart.column;
  1802. while (index < length && content[index] != '\n')
  1803. {
  1804. index++;
  1805. }
  1806. if (content[index] == '\n')
  1807. {
  1808. renderStart.column = index + 1;
  1809. rend = 1;
  1810. renderStartLine++;
  1811. unifyPosition(renderStart);
  1812. return;
  1813. }
  1814. while (true)
  1815. {
  1816. EditableJsonElement* next
  1817. = renderStart.line->zAfter(true, &renderStartLine);
  1818. if (next)
  1819. {
  1820. renderStart.line = next;
  1821. const char* content = next->getContent();
  1822. int length = next->getContent().getLength();
  1823. int index = 0;
  1824. while (index < length && content[index] != '\n')
  1825. {
  1826. index++;
  1827. }
  1828. if (content[index] == '\n')
  1829. {
  1830. renderStart.column = index + 1;
  1831. rend = 1;
  1832. renderStartLine++;
  1833. unifyPosition(renderStart);
  1834. return;
  1835. }
  1836. }
  1837. else
  1838. {
  1839. return;
  1840. }
  1841. }
  1842. }
  1843. }
  1844. EditorPosition Framework::JSON::JsonEditor::getScreenPosition(
  1845. int localX, int localY)
  1846. {
  1847. if (!renderStart.line) return {0, 0};
  1848. int y = 0;
  1849. int lineNumberWidth = 0;
  1850. for (int i = lineCount; i > 0; i /= 10)
  1851. {
  1852. lineNumberWidth++;
  1853. }
  1854. lineNumberWidth = lineNumberWidth * textRenderer->getMaxCharWidth()
  1855. + textRenderer->getCharSpacing() * (lineNumberWidth - 1);
  1856. int x = lineNumberWidth + 26;
  1857. EditableJsonElement* current = renderStart.line;
  1858. int index = renderStart.column;
  1859. EditableJsonElement* last = current;
  1860. while (y < gr.y && current)
  1861. {
  1862. int length = current->getContent().getLength();
  1863. const char* content = current->getContent();
  1864. for (int i = index; i < length; i++)
  1865. {
  1866. if (y >= localY - textRenderer->getRowHeight()
  1867. - textRenderer->getLineSpacing()
  1868. && x >= localX - textRenderer->getCharWidth(content[i]) / 2
  1869. - textRenderer->getCharSpacing())
  1870. {
  1871. EditorPosition result = {current, i};
  1872. unifyPosition(result);
  1873. return result;
  1874. }
  1875. if (content[i] == '\n')
  1876. {
  1877. if (y >= localY - textRenderer->getRowHeight()
  1878. - textRenderer->getLineSpacing())
  1879. {
  1880. EditorPosition result = {current, i};
  1881. unifyPosition(result);
  1882. return result;
  1883. }
  1884. y += textRenderer->getRowHeight()
  1885. + textRenderer->getLineSpacing();
  1886. x = lineNumberWidth + 26;
  1887. }
  1888. else
  1889. {
  1890. x += textRenderer->getCharWidth(content[i])
  1891. + textRenderer->getCharSpacing();
  1892. }
  1893. }
  1894. last = current;
  1895. current = current->zAfter(true, 0);
  1896. index = 0;
  1897. }
  1898. return {last, last->getContent().getLength()};
  1899. }
  1900. void Framework::JSON::JsonEditor::deleteSelection()
  1901. {
  1902. if (selectionStart.line && selectionEnd.line)
  1903. {
  1904. selectionStart.line->removeUntil(
  1905. selectionStart.column, selectionEnd.line, selectionEnd.column);
  1906. selectionEnd = selectionStart;
  1907. textCursor = selectionStart;
  1908. fixTree(selectionStart.line);
  1909. if (dragStartPos.line)
  1910. {
  1911. dragStartPos = selectionStart;
  1912. }
  1913. if (lastClickCursorPos.line)
  1914. {
  1915. lastClickCursorPos = selectionStart;
  1916. }
  1917. }
  1918. }
  1919. void Framework::JSON::JsonEditor::unifyPosition(EditorPosition& pos)
  1920. {
  1921. while (pos.line && pos.column >= pos.line->getContent().getLength())
  1922. {
  1923. if (pos.line->zFirstChildren() && pos.line->isVisible())
  1924. {
  1925. pos = {pos.line->zFirstChildren(),
  1926. pos.column - pos.line->getContent().getLength()};
  1927. }
  1928. else if (pos.line->zMextSibling())
  1929. {
  1930. pos = {pos.line->zMextSibling(),
  1931. pos.column - pos.line->getContent().getLength()};
  1932. }
  1933. else
  1934. {
  1935. EditableJsonElement* p = pos.line->zParent();
  1936. while (p)
  1937. {
  1938. if (p->zMextSibling())
  1939. {
  1940. pos = {p->zMextSibling(), 0};
  1941. break;
  1942. }
  1943. p = p->zParent();
  1944. }
  1945. if (!p)
  1946. {
  1947. pos.column = pos.line->getContent().getLength();
  1948. break;
  1949. }
  1950. }
  1951. }
  1952. }
  1953. void Framework::JSON::JsonEditor::fixTree(EditableJsonElement* zElement)
  1954. {
  1955. EditableJsonElement* before = zElement->zBefore(content, false, 0);
  1956. int beforeLength = before ? before->getContent().getLength() : 0;
  1957. if (zElement->hasError() && before)
  1958. {
  1959. Text content = before->getContent();
  1960. before->setContent(content);
  1961. if (before->getContent().getLength() != beforeLength)
  1962. {
  1963. before->makeVisible();
  1964. }
  1965. }
  1966. if (textCursor.line == zElement)
  1967. {
  1968. textCursor.column += beforeLength;
  1969. textCursor.line = before ? before : zElement;
  1970. unifyPosition(textCursor);
  1971. }
  1972. if (renderStart.line == zElement)
  1973. {
  1974. renderStart.column += beforeLength;
  1975. renderStart.line = before ? before : zElement;
  1976. unifyPosition(renderStart);
  1977. }
  1978. }
  1979. void Framework::JSON::JsonEditor::setFont(Font* font)
  1980. {
  1981. textRenderer->setFontZ(font);
  1982. }
  1983. void Framework::JSON::JsonEditor::setFontSize(int size)
  1984. {
  1985. textRenderer->setFontSize(size);
  1986. textRenderer->setLineSpacing(size / 6);
  1987. textRenderer->setCharSpacing(size / 8);
  1988. }
  1989. void JsonEditor::setContent(Text content)
  1990. {
  1991. if (this->content)
  1992. {
  1993. this->content->release();
  1994. this->content = 0;
  1995. this->renderStart = {0, 0};
  1996. }
  1997. this->content = new EditableJsonElement(
  1998. dynamic_cast<Regex::Automata<char>*>(valueValidator->getThis()));
  1999. content.replace("\r", "");
  2000. content.replace("\t", " ");
  2001. this->content->setContent(content);
  2002. textCursor = {this->content, 0};
  2003. selectionStart = {this->content, 0};
  2004. selectionEnd = {this->content, 0};
  2005. format();
  2006. }
  2007. void Framework::JSON::JsonEditor::setContent(JSONValue* content)
  2008. {
  2009. setContent(content->toString());
  2010. content->release();
  2011. }
  2012. void Framework::JSON::JsonEditor::format()
  2013. {
  2014. if (content)
  2015. {
  2016. content->format();
  2017. lineCount = 0;
  2018. content->lineCount(true, true, 0, lineCount);
  2019. }
  2020. }
  2021. void Framework::JSON::JsonEditor::setValidator(
  2022. Validator::DataValidator* validator)
  2023. {
  2024. if (this->validator) this->validator->release();
  2025. this->validator = validator;
  2026. // if (content) validate(); TODO: validate attributes
  2027. }
  2028. void Framework::JSON::JsonEditor::doKeyboardEvent(KeyboardEvent& te)
  2029. {
  2030. LOCK({&cs});
  2031. if (!textCursor.line)
  2032. {
  2033. return;
  2034. }
  2035. bool shift = getKeyState(T_Shift);
  2036. bool strg = getKeyState(T_Strg);
  2037. if (te.id == TE_Press)
  2038. {
  2039. switch (te.virtualKey)
  2040. {
  2041. case T_Tab:
  2042. {
  2043. Text illegalStarts = "[]{},\": ";
  2044. if (shift)
  2045. {
  2046. bool lastStr
  2047. = textCursor.line->getContent().getLength()
  2048. > textCursor.column
  2049. ? textCursor.line->getContent()[textCursor.column]
  2050. == '"'
  2051. : false;
  2052. int nextColumn = textCursor.column - 1;
  2053. EditableJsonElement* nextLine = textCursor.line;
  2054. while (nextLine)
  2055. {
  2056. if (nextColumn < 0)
  2057. {
  2058. nextLine = nextLine->zBefore(content, true, 0);
  2059. if (!nextLine)
  2060. {
  2061. return;
  2062. }
  2063. nextColumn = nextLine->getContent().getLength();
  2064. }
  2065. int start = 0;
  2066. int end = 0;
  2067. nextLine->getWordBounds(nextColumn, &start, &end);
  2068. if ((start != end
  2069. && !illegalStarts.has(
  2070. nextLine->getContent()[start]))
  2071. || (lastStr
  2072. && nextLine->getContent()[nextColumn] == '"'))
  2073. {
  2074. if (lastStr
  2075. && nextLine->getContent()[nextColumn] == '"')
  2076. {
  2077. start = end;
  2078. }
  2079. if (selectionStart.column != start
  2080. || selectionStart.line != nextLine
  2081. || selectionEnd.column != end
  2082. || selectionEnd.line != nextLine)
  2083. {
  2084. selectionStart = {nextLine, start};
  2085. selectionEnd = {nextLine, end};
  2086. unifyPosition(selectionEnd);
  2087. textCursor = selectionEnd;
  2088. scrollToLine(textCursor, Top);
  2089. te.processed = true;
  2090. time = 0;
  2091. rend = 1;
  2092. return;
  2093. }
  2094. }
  2095. lastStr = nextLine->getContent()[nextColumn] == '"';
  2096. nextColumn--;
  2097. }
  2098. }
  2099. else
  2100. {
  2101. bool lastStr
  2102. = textCursor.line->getContent().getLength()
  2103. > textCursor.column
  2104. ? textCursor.line->getContent()[textCursor.column]
  2105. == '"'
  2106. : false;
  2107. int nextColumn = textCursor.column + 1;
  2108. EditableJsonElement* nextLine = textCursor.line;
  2109. while (nextLine)
  2110. {
  2111. if (nextColumn > nextLine->getContent().getLength())
  2112. {
  2113. nextLine = nextLine->zAfter(true, 0);
  2114. if (!nextLine)
  2115. {
  2116. return;
  2117. }
  2118. nextColumn = 0;
  2119. }
  2120. int start = 0;
  2121. int end = 0;
  2122. nextLine->getWordBounds(nextColumn, &start, &end);
  2123. if (((start != end
  2124. && !illegalStarts.has(
  2125. nextLine->getContent()[start]))
  2126. || (lastStr
  2127. && nextLine->getContent()[nextColumn]
  2128. == '"'))
  2129. && (selectionStart.column != start
  2130. || selectionStart.line != nextLine
  2131. || selectionEnd.column != end
  2132. || selectionEnd.line != nextLine))
  2133. {
  2134. selectionStart = {nextLine, start};
  2135. selectionEnd = {nextLine, end};
  2136. unifyPosition(selectionEnd);
  2137. textCursor = selectionEnd;
  2138. scrollToLine(textCursor, Bottom);
  2139. te.processed = true;
  2140. time = 0;
  2141. rend = 1;
  2142. return;
  2143. }
  2144. lastStr = nextLine->getContent()[nextColumn] == '"';
  2145. nextColumn++;
  2146. }
  2147. }
  2148. }
  2149. return;
  2150. case T_Entf:
  2151. if (selectionStart.line == selectionEnd.line
  2152. && selectionStart.column == selectionEnd.column)
  2153. {
  2154. if (textCursor.column
  2155. < textCursor.line->getContent().getLength())
  2156. {
  2157. Text content = textCursor.line->getContent();
  2158. content.remove(textCursor.column, textCursor.column + 1);
  2159. textCursor.line->setContent(content);
  2160. fixTree(textCursor.line);
  2161. }
  2162. }
  2163. else
  2164. {
  2165. deleteSelection();
  2166. }
  2167. break;
  2168. case T_BackSpace:
  2169. if (selectionStart.line == selectionEnd.line
  2170. && selectionStart.column == selectionEnd.column)
  2171. {
  2172. if (textCursor.column > 0)
  2173. {
  2174. Text content = textCursor.line->getContent();
  2175. content.remove(textCursor.column - 1, textCursor.column);
  2176. textCursor.line->setContent(content);
  2177. textCursor.column--;
  2178. fixTree(textCursor.line);
  2179. }
  2180. else
  2181. {
  2182. EditableJsonElement* before
  2183. = textCursor.line->zBefore(content, false, 0);
  2184. if (before)
  2185. {
  2186. before->makeVisible();
  2187. Text content = before->getContent();
  2188. content.remove(
  2189. content.getLength() - 1, content.getLength());
  2190. before->setContent(content);
  2191. textCursor = {before, content.getLength()};
  2192. fixTree(before);
  2193. scrollToLine(textCursor, Top);
  2194. }
  2195. }
  2196. }
  2197. else
  2198. {
  2199. deleteSelection();
  2200. }
  2201. break;
  2202. case T_Enter:
  2203. {
  2204. deleteSelection();
  2205. Text content = textCursor.line->getContent();
  2206. content.insert(textCursor.column, "\n");
  2207. textCursor.column += 1;
  2208. textCursor.line->setContent(content);
  2209. fixTree(textCursor.line);
  2210. scrollToLine(textCursor, Bottom);
  2211. }
  2212. break;
  2213. case T_Links:
  2214. {
  2215. int nextColumn = textCursor.column - 1;
  2216. EditableJsonElement* nextLine = textCursor.line;
  2217. while (nextColumn < 0)
  2218. {
  2219. nextLine = textCursor.line->zBefore(content, true, 0);
  2220. if (!nextLine)
  2221. {
  2222. return;
  2223. }
  2224. nextColumn = nextLine->getContent().getLength() - 1;
  2225. }
  2226. if (strg)
  2227. {
  2228. int start = 0;
  2229. nextLine->getWordBounds(nextColumn, &start, 0);
  2230. nextColumn = start;
  2231. }
  2232. if (shift)
  2233. {
  2234. if (textCursor.line == selectionStart.line
  2235. && textCursor.column == selectionStart.column)
  2236. {
  2237. selectionStart = {nextLine, nextColumn};
  2238. unifyPosition(selectionStart);
  2239. textCursor = selectionStart;
  2240. }
  2241. else
  2242. {
  2243. selectionEnd = {nextLine, nextColumn};
  2244. unifyPosition(selectionEnd);
  2245. textCursor = selectionEnd;
  2246. }
  2247. }
  2248. else
  2249. {
  2250. textCursor = {nextLine, nextColumn};
  2251. unifyPosition(textCursor);
  2252. selectionStart = textCursor;
  2253. selectionEnd = textCursor;
  2254. }
  2255. scrollToLine(textCursor, Top);
  2256. te.processed = true;
  2257. time = 0;
  2258. rend = 1;
  2259. return;
  2260. }
  2261. case T_Oben:
  2262. {
  2263. EditableJsonElement* current = textCursor.line;
  2264. int count = 0;
  2265. bool found = 0;
  2266. int index = textCursor.column - 1;
  2267. const char* content = current->getContent();
  2268. while (current)
  2269. {
  2270. while (index < 0)
  2271. {
  2272. current = current->zBefore(this->content, true, 0);
  2273. if (!current)
  2274. {
  2275. break;
  2276. }
  2277. content = current->getContent();
  2278. index = current->getContent().getLength() - 1;
  2279. }
  2280. if (!current)
  2281. {
  2282. break;
  2283. }
  2284. if (content[index] == '\n')
  2285. {
  2286. if (found)
  2287. {
  2288. break;
  2289. }
  2290. found = 1;
  2291. }
  2292. if (!found)
  2293. {
  2294. count++;
  2295. }
  2296. index--;
  2297. }
  2298. if (!current)
  2299. {
  2300. break;
  2301. }
  2302. index++;
  2303. while (current && count > 0)
  2304. {
  2305. while (index >= current->getContent().getLength())
  2306. {
  2307. current = current->zAfter(true, 0);
  2308. if (!current)
  2309. {
  2310. break;
  2311. }
  2312. content = current->getContent();
  2313. index = 0;
  2314. }
  2315. if (!current)
  2316. {
  2317. break;
  2318. }
  2319. if (content[index] == '\n')
  2320. {
  2321. break;
  2322. }
  2323. count--;
  2324. index++;
  2325. }
  2326. if (!current)
  2327. {
  2328. break;
  2329. }
  2330. if (shift)
  2331. {
  2332. if (textCursor.line == selectionStart.line
  2333. && textCursor.column == selectionStart.column)
  2334. {
  2335. selectionStart = {current, index};
  2336. unifyPosition(selectionStart);
  2337. textCursor = selectionStart;
  2338. }
  2339. else
  2340. {
  2341. selectionEnd = {current, index};
  2342. unifyPosition(selectionEnd);
  2343. textCursor = selectionEnd;
  2344. }
  2345. }
  2346. else
  2347. {
  2348. textCursor = {current, index};
  2349. unifyPosition(textCursor);
  2350. selectionStart = textCursor;
  2351. selectionEnd = textCursor;
  2352. }
  2353. scrollToLine(textCursor, Top);
  2354. te.processed = true;
  2355. time = 0;
  2356. rend = 1;
  2357. return;
  2358. }
  2359. case T_Rechts:
  2360. {
  2361. int nextColumn = textCursor.column + 1;
  2362. EditableJsonElement* nextLine = textCursor.line;
  2363. if (nextColumn > nextLine->getContent().getLength())
  2364. {
  2365. nextLine = textCursor.line->zAfter(true, 0);
  2366. if (!nextLine)
  2367. {
  2368. return;
  2369. }
  2370. nextColumn = 0;
  2371. }
  2372. if (strg)
  2373. {
  2374. int end = 0;
  2375. nextLine->getWordBounds(nextColumn, 0, &end);
  2376. nextColumn = end;
  2377. }
  2378. if (shift)
  2379. {
  2380. if (textCursor.line == selectionEnd.line
  2381. && textCursor.column == selectionEnd.column)
  2382. {
  2383. selectionEnd = {nextLine, nextColumn};
  2384. unifyPosition(selectionEnd);
  2385. textCursor = selectionEnd;
  2386. }
  2387. else
  2388. {
  2389. selectionStart = {nextLine, nextColumn};
  2390. unifyPosition(selectionStart);
  2391. textCursor = selectionStart;
  2392. }
  2393. }
  2394. else
  2395. {
  2396. textCursor = {nextLine, nextColumn};
  2397. unifyPosition(textCursor);
  2398. selectionStart = textCursor;
  2399. selectionEnd = textCursor;
  2400. }
  2401. scrollToLine(textCursor, Bottom);
  2402. te.processed = true;
  2403. time = 0;
  2404. rend = 1;
  2405. return;
  2406. }
  2407. case T_Unten:
  2408. {
  2409. EditableJsonElement* current = textCursor.line;
  2410. int count = 0;
  2411. int index = textCursor.column - 1;
  2412. const char* content = current->getContent();
  2413. while (current)
  2414. {
  2415. while (index < 0)
  2416. {
  2417. current = current->zBefore(this->content, true, 0);
  2418. if (!current)
  2419. {
  2420. break;
  2421. }
  2422. content = current->getContent();
  2423. index = current->getContent().getLength() - 1;
  2424. }
  2425. if (!current)
  2426. {
  2427. break;
  2428. }
  2429. if (content[index] == '\n')
  2430. {
  2431. break;
  2432. }
  2433. count++;
  2434. index--;
  2435. }
  2436. bool found = 0;
  2437. current = textCursor.line;
  2438. index = textCursor.column;
  2439. content = current->getContent();
  2440. while (current && (count > 0 || !found))
  2441. {
  2442. while (index >= current->getContent().getLength())
  2443. {
  2444. current = current->zAfter(true, 0);
  2445. if (!current)
  2446. {
  2447. break;
  2448. }
  2449. content = current->getContent();
  2450. index = 0;
  2451. }
  2452. if (found)
  2453. {
  2454. count--;
  2455. }
  2456. if (content[index] == '\n')
  2457. {
  2458. if (found)
  2459. {
  2460. break;
  2461. }
  2462. found = 1;
  2463. }
  2464. index++;
  2465. }
  2466. if (shift)
  2467. {
  2468. if (textCursor.line == selectionEnd.line
  2469. && textCursor.column == selectionEnd.column)
  2470. {
  2471. selectionEnd = {current, index};
  2472. unifyPosition(selectionEnd);
  2473. textCursor = selectionEnd;
  2474. }
  2475. else
  2476. {
  2477. selectionStart = {current, index};
  2478. unifyPosition(selectionStart);
  2479. textCursor = selectionStart;
  2480. }
  2481. }
  2482. else
  2483. {
  2484. textCursor = {current, index};
  2485. unifyPosition(textCursor);
  2486. selectionStart = textCursor;
  2487. selectionEnd = textCursor;
  2488. }
  2489. scrollToLine(textCursor, Bottom);
  2490. te.processed = true;
  2491. time = 0;
  2492. rend = 1;
  2493. return;
  2494. }
  2495. default:
  2496. if (strg)
  2497. {
  2498. if (te.virtualKey == 'c' || te.virtualKey == 'C')
  2499. {
  2500. Text content = getSelectedContent();
  2501. TextKopieren(content);
  2502. return;
  2503. }
  2504. else if (te.virtualKey == 'x' || te.virtualKey == 'X')
  2505. {
  2506. Text content = getSelectedContent();
  2507. TextKopieren(content);
  2508. deleteSelection();
  2509. scrollToLine(textCursor, Top);
  2510. return;
  2511. }
  2512. else if (te.virtualKey == 'v' || te.virtualKey == 'V')
  2513. {
  2514. deleteSelection();
  2515. EditableJsonElement* changed = textCursor.line;
  2516. Text content = changed->getContent();
  2517. const char* inserted = TextInsert();
  2518. content.insert(textCursor.column, inserted);
  2519. textCursor.column += textLength(inserted);
  2520. unifyPosition(textCursor);
  2521. scrollToLine(textCursor, Bottom);
  2522. break;
  2523. }
  2524. else if (te.virtualKey == 'a' || te.virtualKey == 'A')
  2525. {
  2526. selectionStart = {content, 0};
  2527. EditableJsonElement* element = content;
  2528. while (element->zMextSibling())
  2529. {
  2530. element = element->zMextSibling();
  2531. }
  2532. while (element->zLastChildren())
  2533. {
  2534. element = element->zLastChildren();
  2535. }
  2536. selectionEnd = {element, element->getContent().getLength()};
  2537. textCursor = selectionEnd;
  2538. te.processed = true;
  2539. time = 0;
  2540. rend = 1;
  2541. scrollToLine(textCursor, Bottom);
  2542. return;
  2543. }
  2544. else if (te.virtualKey == 'f' || te.virtualKey == 'F')
  2545. {
  2546. format();
  2547. return;
  2548. }
  2549. }
  2550. if (isWritable(te.key[0]))
  2551. {
  2552. deleteSelection();
  2553. Text content = textCursor.line->getContent();
  2554. content.insert(textCursor.column, te.key);
  2555. textCursor.column += textLength(te.key);
  2556. textCursor.line->setContent(content);
  2557. fixTree(textCursor.line);
  2558. }
  2559. else
  2560. {
  2561. return;
  2562. }
  2563. }
  2564. selectionStart = textCursor;
  2565. selectionEnd = textCursor;
  2566. te.processed = true;
  2567. lineCount = 0;
  2568. content->lineCount(true, true, 0, lineCount);
  2569. }
  2570. time = 0;
  2571. rend = 1;
  2572. }
  2573. bool Framework::JSON::JsonEditor::tick(double tickVal)
  2574. {
  2575. LOCK({&cs});
  2576. time += tickVal;
  2577. timeSicePress += tickVal;
  2578. if (time > 1.0)
  2579. {
  2580. tps = renderings / time;
  2581. time = 0.0;
  2582. renderings = 0;
  2583. }
  2584. rend |= drawCursor != (time < 0.5);
  2585. drawCursor = time < 0.5;
  2586. errorDescription->tick(tickVal);
  2587. return DrawableBackground::tick(tickVal);
  2588. }
  2589. void Framework::JSON::JsonEditor::render(Image& rObj)
  2590. {
  2591. LOCK({&cs});
  2592. renderings++;
  2593. renderedLines = 0;
  2594. errorDescription->removeStyle(TextField::Style::Visible);
  2595. DrawableBackground::render(rObj);
  2596. if (textRenderer->zFont() && content
  2597. && rObj.setDrawOptions(pos + Point(getBorderWidth(), getBorderWidth()),
  2598. Point(getInnerWidth(), getInnerHeight())))
  2599. {
  2600. if (!renderStart.line)
  2601. {
  2602. renderStart = {content, 0};
  2603. renderStartLine = 1;
  2604. }
  2605. int y = 0;
  2606. int lineNumberWidth = 0;
  2607. for (int i = lineCount; i > 0; i /= 10)
  2608. {
  2609. lineNumberWidth++;
  2610. }
  2611. lineNumberWidth
  2612. = lineNumberWidth * textRenderer->getMaxCharWidth()
  2613. + textRenderer->getCharSpacing() * (lineNumberWidth - 1);
  2614. bool selected = false;
  2615. EditableJsonElement* element = content;
  2616. while (element)
  2617. {
  2618. if (element == selectionStart.line)
  2619. {
  2620. if (element != renderStart.line)
  2621. {
  2622. selected = true;
  2623. }
  2624. else
  2625. {
  2626. if (selectionStart.column < renderStart.column)
  2627. {
  2628. selected = true;
  2629. }
  2630. }
  2631. }
  2632. if (element == selectionEnd.line)
  2633. {
  2634. if (element != renderStart.line)
  2635. {
  2636. selected = false;
  2637. break;
  2638. }
  2639. else
  2640. {
  2641. if (selectionEnd.column < renderStart.column)
  2642. {
  2643. selected = false;
  2644. }
  2645. }
  2646. }
  2647. if (element == renderStart.line)
  2648. {
  2649. break;
  2650. }
  2651. element = element->zAfter(false, 0);
  2652. }
  2653. int line = renderStartLine;
  2654. int x = lineNumberWidth + 26;
  2655. EditableJsonElement* current = renderStart.line;
  2656. EditableJsonElement* highlightStop = 0;
  2657. int index = renderStart.column;
  2658. bool treeBranchRendered = false;
  2659. bool highlighted = false;
  2660. int numberWidth = textRenderer->getTextWidth(Text(line));
  2661. textRenderer->renderText(
  2662. 5 + lineNumberWidth - numberWidth, y, Text(line), rObj, 0xFF707070);
  2663. bool whiteSpaceOnly = true;
  2664. while (y < gr.y && current)
  2665. {
  2666. const Text& content = current->getContent();
  2667. int column = index;
  2668. for (int i = index; i < content.getLength() && y < gr.y;
  2669. i++, column++)
  2670. {
  2671. if (selectionStart.line == current
  2672. && selectionStart.column == i)
  2673. {
  2674. selected = true;
  2675. }
  2676. if (selectionEnd.line == current && selectionEnd.column == i)
  2677. {
  2678. selected = false;
  2679. }
  2680. int color = current->getColor(i);
  2681. int cursorX = x - 1;
  2682. if (textCursor.line == current && textCursor.column == i
  2683. && drawCursor)
  2684. {
  2685. rObj.drawLineV(
  2686. cursorX, y, textRenderer->getFontSize(), 0xFFC0C0C0);
  2687. }
  2688. if (content[i] == '\n')
  2689. {
  2690. column = -1;
  2691. highlighted = false;
  2692. if (!treeBranchRendered)
  2693. {
  2694. // | in tree view
  2695. rObj.drawLineV(lineNumberWidth + 13,
  2696. y
  2697. - (line > 1 ? textRenderer->getFontSize() / 6
  2698. : 0),
  2699. textRenderer->getFontSize()
  2700. + (line > 1 ? textRenderer->getFontSize() / 6
  2701. : 0),
  2702. 0xFF808080);
  2703. if (highlightStop)
  2704. {
  2705. // hover effect in tree view
  2706. rObj.fillRegion(lineNumberWidth + 10,
  2707. y - textRenderer->getFontSize() / 6,
  2708. 7,
  2709. textRenderer->getFontSize()
  2710. + textRenderer->getFontSize() / 6,
  2711. 0xFF505050);
  2712. }
  2713. }
  2714. if (selected)
  2715. {
  2716. rObj.fillRegion(x,
  2717. y,
  2718. textRenderer->getTextWidth(" "),
  2719. textRenderer->getRowHeight()
  2720. + textRenderer->getLineSpacing(),
  2721. 0xFF502020);
  2722. }
  2723. x = lineNumberWidth + 26;
  2724. y += textRenderer->getRowHeight()
  2725. + textRenderer->getLineSpacing();
  2726. whiteSpaceOnly = true;
  2727. line++;
  2728. renderedLines++;
  2729. treeBranchRendered = false;
  2730. int numberWidth = textRenderer->getTextWidth(Text(line));
  2731. textRenderer->renderText(5 + lineNumberWidth - numberWidth,
  2732. y,
  2733. Text(line),
  2734. rObj,
  2735. 0xFF707070);
  2736. }
  2737. else
  2738. {
  2739. textRenderer->renderChar(x,
  2740. y,
  2741. content[i],
  2742. rObj,
  2743. color,
  2744. false,
  2745. selected,
  2746. 0xFF502020);
  2747. }
  2748. if (cursorPos.x > cursorX && cursorPos.x <= x
  2749. && cursorPos.y >= y
  2750. && cursorPos.y <= y + textRenderer->getRowHeight())
  2751. {
  2752. if (current->hasError(i))
  2753. {
  2754. const SyntaxError& error = current->getError(i);
  2755. errorDescription->setText(errors[error.getErrorType()]);
  2756. errorDescription->setSize(
  2757. errorDescription->getNeededWidth() + 6,
  2758. errorDescription->getNeededHeight() + 6);
  2759. errorDescription->setPosition(
  2760. cursorPos.x, cursorPos.y + 20);
  2761. if (errorDescription->getX()
  2762. + errorDescription->getWidth()
  2763. > getInnerWidth())
  2764. {
  2765. errorDescription->setX(
  2766. getInnerWidth() - errorDescription->getWidth()
  2767. - 5);
  2768. }
  2769. if (errorDescription->getY()
  2770. + errorDescription->getHeight()
  2771. > getInnerHeight())
  2772. {
  2773. errorDescription->setY(
  2774. getInnerHeight() - errorDescription->getHeight()
  2775. - 5);
  2776. }
  2777. errorDescription->addStyle(TextField::Style::Visible);
  2778. }
  2779. }
  2780. if (column % 4 == 0 && content[i] == ' ' && whiteSpaceOnly)
  2781. {
  2782. rObj.drawLineV(
  2783. cursorX + 1 + textRenderer->getTextWidth(" ") / 2,
  2784. y + 2,
  2785. textRenderer->getFontSize() - 4,
  2786. 0xFF353535);
  2787. }
  2788. whiteSpaceOnly &= content[i] == ' ' || content[i] == '\n';
  2789. }
  2790. if (selectionStart.line == current
  2791. && selectionStart.column == content.getLength())
  2792. {
  2793. selected = true;
  2794. }
  2795. if (selectionEnd.line == current
  2796. && selectionEnd.column == content.getLength())
  2797. {
  2798. selected = false;
  2799. }
  2800. if (textCursor.line == current
  2801. && textCursor.column == content.getLength() && drawCursor)
  2802. {
  2803. rObj.drawLineV(
  2804. x - 1, y, textRenderer->getFontSize(), 0xFFC0C0C0);
  2805. }
  2806. if (current->zFirstChildren())
  2807. {
  2808. if (pressed && pressedPos.x >= lineNumberWidth + 10
  2809. && pressedPos.x <= lineNumberWidth + 16
  2810. && pressedPos.y >= y + textRenderer->getFontSize() / 2 - 4
  2811. && pressedPos.y <= y + textRenderer->getFontSize() / 2 + 2)
  2812. {
  2813. pressed = false;
  2814. current->setHidden(!current->isHidden());
  2815. }
  2816. if (cursorPos.x >= lineNumberWidth + 10
  2817. && cursorPos.x <= lineNumberWidth + 16
  2818. && cursorPos.y >= y + textRenderer->getFontSize() / 2 - 4
  2819. && cursorPos.y <= y + textRenderer->getFontSize() / 2 + 2
  2820. && !current->isHidden())
  2821. {
  2822. highlightStop = current->zLastChildren();
  2823. }
  2824. if (current->isHidden())
  2825. {
  2826. if (!treeBranchRendered)
  2827. {
  2828. rObj.fillRegion(lineNumberWidth + 10,
  2829. y + textRenderer->getFontSize() / 2 - 4,
  2830. 6,
  2831. 6,
  2832. 0xFF000000);
  2833. rObj.drawLine(
  2834. Point(lineNumberWidth + 10,
  2835. y + textRenderer->getFontSize() / 2 - 4),
  2836. Point(lineNumberWidth + 16,
  2837. y + textRenderer->getFontSize() / 2 - 1),
  2838. 0xFF808080);
  2839. rObj.drawLine(
  2840. Point(lineNumberWidth + 10,
  2841. y + textRenderer->getFontSize() / 2 + 2),
  2842. Point(lineNumberWidth + 16,
  2843. y + textRenderer->getFontSize() / 2 - 1),
  2844. 0xFF808080);
  2845. }
  2846. for (char c : " ... ")
  2847. {
  2848. textRenderer->renderChar(x,
  2849. y,
  2850. c,
  2851. rObj,
  2852. 0xFF505050,
  2853. false,
  2854. selected,
  2855. 0xFF502020);
  2856. }
  2857. if (current->getOpeningControllChar() == '{')
  2858. {
  2859. textRenderer->renderChar(x,
  2860. y,
  2861. '}',
  2862. rObj,
  2863. 0xFF505050,
  2864. false,
  2865. selected,
  2866. 0xFF502020);
  2867. }
  2868. else if (current->getOpeningControllChar() == '[')
  2869. {
  2870. textRenderer->renderChar(x,
  2871. y,
  2872. ']',
  2873. rObj,
  2874. 0xFF505050,
  2875. false,
  2876. selected,
  2877. 0xFF502020);
  2878. }
  2879. }
  2880. else
  2881. {
  2882. if (!treeBranchRendered)
  2883. {
  2884. rObj.fillRegion(lineNumberWidth + 10,
  2885. y + textRenderer->getFontSize() / 2 - 4,
  2886. 6,
  2887. 6,
  2888. 0xFF000000);
  2889. rObj.drawLine(
  2890. Point(lineNumberWidth + 10,
  2891. y + textRenderer->getFontSize() / 2 - 4),
  2892. Point(lineNumberWidth + 13,
  2893. y + textRenderer->getFontSize() / 2 + 2),
  2894. 0xFF808080);
  2895. rObj.drawLine(
  2896. Point(lineNumberWidth + 16,
  2897. y + textRenderer->getFontSize() / 2 - 4),
  2898. Point(lineNumberWidth + 13,
  2899. y + textRenderer->getFontSize() / 2 + 2),
  2900. 0xFF808080);
  2901. }
  2902. }
  2903. treeBranchRendered = true;
  2904. }
  2905. if (!current->zMextSibling() && current->zParent())
  2906. {
  2907. // |_ in tree view
  2908. rObj.drawLineH(lineNumberWidth + 14,
  2909. y + textRenderer->getFontSize() - 1,
  2910. 3,
  2911. 0xFF808080);
  2912. }
  2913. rObj.drawLineV(x,
  2914. y,
  2915. textRenderer->getFontSize(),
  2916. 0xFF30C030); // TODO: debug tree seperator
  2917. if (current == highlightStop)
  2918. {
  2919. highlightStop = 0;
  2920. if (!treeBranchRendered)
  2921. {
  2922. // hover effect in tree view
  2923. rObj.fillRegion(lineNumberWidth + 10,
  2924. y - textRenderer->getFontSize() / 6,
  2925. 7,
  2926. textRenderer->getFontSize()
  2927. + textRenderer->getFontSize() / 6,
  2928. 0xFF505050);
  2929. highlighted = true;
  2930. }
  2931. }
  2932. current = current->zAfter(true, &line);
  2933. index = 0;
  2934. }
  2935. if (!treeBranchRendered && !highlighted)
  2936. {
  2937. // | in tree view
  2938. rObj.drawLineV(lineNumberWidth + 13,
  2939. y - (line > 1 ? textRenderer->getFontSize() / 6 : 0),
  2940. textRenderer->getFontSize()
  2941. + (line > 1 ? textRenderer->getFontSize() / 6 : 0),
  2942. 0xFF808080);
  2943. }
  2944. EditableJsonElement* resnderStopElement = current;
  2945. renderStopLine = line;
  2946. textRenderer->renderText(5,
  2947. y + textRenderer->getRowHeight() + textRenderer->getLineSpacing(),
  2948. Text("FPS: ") + Text(tps),
  2949. rObj,
  2950. 0xFFFFFFFF);
  2951. bool visible = false;
  2952. double sy = 0.0;
  2953. double yOffset = (double)getInnerHeight() / lineCount;
  2954. current = content;
  2955. EditableJsonElement* hiddenStop = 0;
  2956. while (current)
  2957. {
  2958. if (hiddenStop == current)
  2959. {
  2960. hiddenStop = 0;
  2961. }
  2962. if (current == renderStart.line)
  2963. {
  2964. visible = true;
  2965. }
  2966. if (current == resnderStopElement)
  2967. {
  2968. visible = false;
  2969. }
  2970. int f = 0xFF303030;
  2971. if (visible)
  2972. {
  2973. f = 0xFFA0A0A0;
  2974. }
  2975. if (hiddenStop)
  2976. {
  2977. f = 0xFF505050;
  2978. }
  2979. if (current->hasError())
  2980. {
  2981. f = 0xFFFF0000;
  2982. }
  2983. double size = current->lineCount() * yOffset;
  2984. if ((int)(sy + size) > (int)sy || current->hasError())
  2985. {
  2986. rObj.fillRegion(getInnerWidth() - 15,
  2987. (int)sy,
  2988. 15,
  2989. (int)(sy + MAX(yOffset, 1)),
  2990. f);
  2991. }
  2992. if (current->isHidden())
  2993. {
  2994. if (!hiddenStop)
  2995. {
  2996. hiddenStop = current->zAfter(true, 0);
  2997. }
  2998. }
  2999. current = current->zAfter(false, 0);
  3000. sy += size;
  3001. }
  3002. errorDescription->render(rObj);
  3003. rObj.releaseDrawOptions();
  3004. }
  3005. }
  3006. Text Framework::JSON::JsonEditor::getContent()
  3007. {
  3008. return content ? content->getRecursiveContent() : Text("");
  3009. }
  3010. Text Framework::JSON::JsonEditor::getSelectedContent()
  3011. {
  3012. Text result = "";
  3013. if (selectionStart.line && selectionEnd.line)
  3014. {
  3015. if (selectionStart.line == selectionEnd.line)
  3016. {
  3017. Text* tmp = selectionStart.line->getContent().getTeilText(
  3018. selectionStart.column, selectionEnd.column);
  3019. result.append(*tmp);
  3020. tmp->release();
  3021. }
  3022. else
  3023. {
  3024. Text* tmp = selectionStart.line->getContent().getTeilText(
  3025. selectionStart.column,
  3026. selectionStart.line->getContent().getLength());
  3027. result.append(*tmp);
  3028. tmp->release();
  3029. Stack<EditableJsonElement*> stack(0);
  3030. stack.push(selectionStart.line);
  3031. while (stack.getSize())
  3032. {
  3033. EditableJsonElement* current = stack.pop();
  3034. if (current->zMextSibling())
  3035. {
  3036. stack.push(current->zMextSibling());
  3037. }
  3038. else if (stack.getSize() == 0 && current->zParent())
  3039. {
  3040. EditableJsonElement* parent = current->zParent();
  3041. while (parent)
  3042. {
  3043. if (parent->zMextSibling())
  3044. {
  3045. stack.push(current->zParent()->zMextSibling());
  3046. break;
  3047. }
  3048. parent = parent->zParent();
  3049. }
  3050. }
  3051. if (current->zFirstChildren())
  3052. {
  3053. stack.push(current->zFirstChildren());
  3054. }
  3055. if (current == selectionEnd.line)
  3056. {
  3057. break;
  3058. }
  3059. if (current != selectionStart.line)
  3060. {
  3061. result += current->getContent();
  3062. }
  3063. }
  3064. tmp = selectionEnd.line->getContent().getTeilText(
  3065. 0, selectionEnd.column);
  3066. result.append(*tmp);
  3067. tmp->release();
  3068. }
  3069. }
  3070. return result;
  3071. }
  3072. JSONValue* Framework::JSON::JsonEditor::getValidContent()
  3073. {
  3074. return nullptr; // TODO
  3075. }
  3076. void Framework::JSON::JsonEditor::scrollToLine(int line, ScrollTargetPos pos)
  3077. {
  3078. if (pos == Top || pos == Bottom)
  3079. {
  3080. if (line >= renderStartLine && line < renderStopLine)
  3081. {
  3082. return; // line is already visible
  3083. }
  3084. }
  3085. EditableJsonElement* element = content;
  3086. if (line == 1)
  3087. {
  3088. scrollToLine(line, {content, 0}, pos);
  3089. return;
  3090. }
  3091. int l = 1;
  3092. int column = 0;
  3093. while (element)
  3094. {
  3095. int lineCount = element->lineCount();
  3096. if (lineCount + l >= line)
  3097. {
  3098. column = element->getContent().positionOf('\n', line - l - 1) + 1;
  3099. break;
  3100. }
  3101. l += lineCount;
  3102. EditableJsonElement* tmp = element->zAfter(true, &l);
  3103. if (l >= line)
  3104. {
  3105. column = element->getContent().positionOf('\n', lineCount - 1) + 1;
  3106. break;
  3107. }
  3108. element = tmp;
  3109. }
  3110. EditorPosition target = {element, column};
  3111. unifyPosition(target);
  3112. scrollToLine(line, target, pos);
  3113. }
  3114. void Framework::JSON::JsonEditor::scrollToLine(
  3115. EditorPosition target, ScrollTargetPos pos)
  3116. {
  3117. if (!target.line || !target.line->isVisible())
  3118. {
  3119. return;
  3120. }
  3121. int lineNumber = 0;
  3122. if (!content->lineCount(true, true, target.line, lineNumber))
  3123. {
  3124. Text* part = target.line->getContent().getTeilText(0, target.column);
  3125. lineNumber += part->countOf('\n');
  3126. part->release();
  3127. if (pos == Top || pos == Bottom)
  3128. {
  3129. if (lineNumber >= renderStartLine && lineNumber < renderStopLine)
  3130. {
  3131. return; // line is already visible
  3132. }
  3133. }
  3134. scrollToLine(lineNumber, target, pos);
  3135. }
  3136. }
  3137. void Framework::JSON::JsonEditor::scrollToLine(
  3138. int lineNum, EditorPosition target, ScrollTargetPos pos)
  3139. {
  3140. if (!target.line || !target.line->isVisible())
  3141. {
  3142. return;
  3143. }
  3144. if (pos == Top || pos == Bottom)
  3145. {
  3146. if (lineNum >= renderStartLine && lineNum < renderStopLine)
  3147. {
  3148. return; // line is already visible
  3149. }
  3150. }
  3151. if (pos == Top)
  3152. {
  3153. renderStart = target;
  3154. const char* content = renderStart.line->getContent();
  3155. while (
  3156. renderStart.column > 0 && content[renderStart.column - 1] != '\n')
  3157. {
  3158. renderStart.column--;
  3159. }
  3160. renderStartLine = lineNum;
  3161. }
  3162. else
  3163. {
  3164. int linesUntilStart
  3165. = pos == Bottom ? renderedLines - 1 : renderedLines / 2 - 1;
  3166. if (linesUntilStart >= lineNum)
  3167. {
  3168. renderStart = {content, 0};
  3169. renderStartLine = 1;
  3170. return;
  3171. }
  3172. EditableJsonElement* element = target.line;
  3173. EditableJsonElement* before = element;
  3174. while (linesUntilStart >= 0)
  3175. {
  3176. if (!before)
  3177. {
  3178. break;
  3179. }
  3180. element = before;
  3181. int newLines = 0;
  3182. if (element == target.line)
  3183. {
  3184. Text* part
  3185. = element->getContent().getTeilText(0, target.column);
  3186. newLines = part->countOf('\n');
  3187. part->release();
  3188. }
  3189. else
  3190. {
  3191. newLines = element->lineCount();
  3192. }
  3193. if (newLines > linesUntilStart)
  3194. {
  3195. renderStart = {element,
  3196. element->getContent().positionOf(
  3197. '\n', newLines - linesUntilStart - 1)
  3198. + 1};
  3199. unifyPosition(renderStart);
  3200. renderStartLine = lineNum - linesUntilStart;
  3201. return;
  3202. }
  3203. lineNum -= newLines;
  3204. linesUntilStart -= newLines;
  3205. before = element->zBefore(this->content, true, &lineNum);
  3206. }
  3207. renderStart = {element, 0};
  3208. renderStartLine = lineNum;
  3209. }
  3210. }