JsonEditor.cpp 99 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309
  1. #include "JsonEditor.h"
  2. #include "Bild.h"
  3. #include "Globals.h"
  4. #include "MausEreignis.h"
  5. #include "Regex.h"
  6. #include "Schrift.h"
  7. #include "TastaturEreignis.h"
  8. #include "TextFeld.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().istGleich("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().istGleich("true")
  61. || root->getValue().istGleich("false"))
  62. {
  63. return AbstractType::BOOLEAN;
  64. }
  65. else if (root->getValue().hatAt(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().istGleich(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().istGleich(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. valueValidator(valueValidator),
  448. valueStart(-1),
  449. keyStart(-1),
  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.leeren();
  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.getEintragAnzahl())
  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.getEintragAnzahl())
  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.anzahlVon('\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.istGleich("null") || value.istGleich("true")
  770. || value.istGleich("false"))
  771. {
  772. return true;
  773. }
  774. RCArray<Regex::Result>* results
  775. = valueValidator->match(value, value.getLength());
  776. bool ok = results->getEintragAnzahl() == 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.getEintragAnzahl())
  827. {
  828. if (openingControllChar)
  829. {
  830. while (afterList.getEintragAnzahl()
  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.getEintragAnzahl())
  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.getEintragAnzahl()
  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.getEintragAnzahl())
  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.leeren();
  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.getEintragAnzahl() > 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.istGleich("null"))
  1361. {
  1362. color = 0xFFC0A070;
  1363. }
  1364. else if (value.istGleich("true") || value.istGleich("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. : ZeichnungHintergrund(),
  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->setSchriftSize(12);
  1611. textRenderer->setZeilenAbstand(2);
  1612. textRenderer->setZeichenAbstand(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.createTextFeld(uiInit.initParam);
  1627. errorDescription->setStyle(TextFeld::Style::Text | TextFeld::Style::Center
  1628. | TextFeld::Style::Rahmen
  1629. | TextFeld::Style::Hintergrund
  1630. | TextFeld::Style::HAlpha);
  1631. errorDescription->removeStyle(TextFeld::Style::Sichtbar);
  1632. errorDescription->setSchriftFarbe(0xFFFFFFFF);
  1633. errorDescription->setRahmenFarbe(0xFFA0A0A0);
  1634. errorDescription->setHintergrundFarbe(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::doMausEreignis(MausEreignis& 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_RLinks)
  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_PLinks || (me.id == ME_Betritt && getMausStand(M_Links)))
  1677. {
  1678. if (me.mx >= getInnenBreite() - 15)
  1679. {
  1680. int line = (int)(me.my / ((double)getInnenHeight() / 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_Bewegung)
  1700. {
  1701. if (drag)
  1702. {
  1703. if (dragSart.x >= getInnenBreite() - 15)
  1704. {
  1705. int line
  1706. = (int)(me.my / ((double)getInnenHeight() / 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->getZeichenAbstand() * (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->getZeilenHeight()
  1867. - textRenderer->getZeilenAbstand()
  1868. && x >= localX - textRenderer->getCharWidth(content[i]) / 2
  1869. - textRenderer->getZeichenAbstand())
  1870. {
  1871. EditorPosition result = {current, i};
  1872. unifyPosition(result);
  1873. return result;
  1874. }
  1875. if (content[i] == '\n')
  1876. {
  1877. if (y >= localY - textRenderer->getZeilenHeight()
  1878. - textRenderer->getZeilenAbstand())
  1879. {
  1880. EditorPosition result = {current, i};
  1881. unifyPosition(result);
  1882. return result;
  1883. }
  1884. y += textRenderer->getZeilenHeight()
  1885. + textRenderer->getZeilenAbstand();
  1886. x = lineNumberWidth + 26;
  1887. }
  1888. else
  1889. {
  1890. x += textRenderer->getCharWidth(content[i])
  1891. + textRenderer->getZeichenAbstand();
  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(Schrift* schrift)
  1980. {
  1981. textRenderer->setSchriftZ(schrift);
  1982. }
  1983. void Framework::JSON::JsonEditor::setFontSize(int size)
  1984. {
  1985. textRenderer->setSchriftSize(size);
  1986. textRenderer->setZeilenAbstand(size / 6);
  1987. textRenderer->setZeichenAbstand(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.ersetzen("\r", "");
  2000. content.ersetzen("\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::doTastaturEreignis(TastaturEreignis& te)
  2029. {
  2030. LOCK(&cs);
  2031. if (!textCursor.line)
  2032. {
  2033. return;
  2034. }
  2035. bool shift = getTastenStand(T_Shift);
  2036. bool strg = getTastenStand(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.hat(
  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.verarbeitet = 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.hat(
  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.verarbeitet = 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.verarbeitet = 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.verarbeitet = 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.verarbeitet = 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.verarbeitet = 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. bool first = true;
  2529. while (element->zMextSibling())
  2530. {
  2531. element = element->zMextSibling();
  2532. }
  2533. while (element->zLastChildren())
  2534. {
  2535. element = element->zLastChildren();
  2536. }
  2537. selectionEnd = {element, element->getContent().getLength()};
  2538. textCursor = selectionEnd;
  2539. te.verarbeitet = true;
  2540. time = 0;
  2541. rend = 1;
  2542. scrollToLine(textCursor, Bottom);
  2543. return;
  2544. }
  2545. else if (te.virtualKey == 'f' || te.virtualKey == 'F')
  2546. {
  2547. format();
  2548. return;
  2549. }
  2550. }
  2551. if (istSchreibbar(te.taste[0]))
  2552. {
  2553. deleteSelection();
  2554. Text content = textCursor.line->getContent();
  2555. content.insert(textCursor.column, te.taste);
  2556. textCursor.column += textLength(te.taste);
  2557. textCursor.line->setContent(content);
  2558. fixTree(textCursor.line);
  2559. }
  2560. else
  2561. {
  2562. return;
  2563. }
  2564. }
  2565. selectionStart = textCursor;
  2566. selectionEnd = textCursor;
  2567. te.verarbeitet = true;
  2568. lineCount = 0;
  2569. content->lineCount(true, true, 0, lineCount);
  2570. }
  2571. time = 0;
  2572. rend = 1;
  2573. }
  2574. bool Framework::JSON::JsonEditor::tick(double tickVal)
  2575. {
  2576. LOCK(&cs);
  2577. time += tickVal;
  2578. timeSicePress += tickVal;
  2579. if (time > 1.0)
  2580. {
  2581. tps = renderings / time;
  2582. time = 0.0;
  2583. renderings = 0;
  2584. }
  2585. rend |= drawCursor != (time < 0.5);
  2586. drawCursor = time < 0.5;
  2587. errorDescription->tick(tickVal);
  2588. return ZeichnungHintergrund::tick(tickVal);
  2589. }
  2590. void Framework::JSON::JsonEditor::render(Bild& rObj)
  2591. {
  2592. LOCK(&cs);
  2593. renderings++;
  2594. renderedLines = 0;
  2595. errorDescription->removeStyle(TextFeld::Style::Sichtbar);
  2596. ZeichnungHintergrund::render(rObj);
  2597. if (textRenderer->zSchrift() && content
  2598. && rObj.setDrawOptions(
  2599. pos + Punkt(getRahmenBreite(), getRahmenBreite()),
  2600. Punkt(getInnenBreite(), getInnenHeight())))
  2601. {
  2602. if (!renderStart.line)
  2603. {
  2604. renderStart = {content, 0};
  2605. renderStartLine = 1;
  2606. }
  2607. int y = 0;
  2608. int lineNumberWidth = 0;
  2609. for (int i = lineCount; i > 0; i /= 10)
  2610. {
  2611. lineNumberWidth++;
  2612. }
  2613. lineNumberWidth
  2614. = lineNumberWidth * textRenderer->getMaxCharWidth()
  2615. + textRenderer->getZeichenAbstand() * (lineNumberWidth - 1);
  2616. bool selected = false;
  2617. EditableJsonElement* element = content;
  2618. while (element)
  2619. {
  2620. if (element == selectionStart.line)
  2621. {
  2622. if (element != renderStart.line)
  2623. {
  2624. selected = true;
  2625. }
  2626. else
  2627. {
  2628. if (selectionStart.column < renderStart.column)
  2629. {
  2630. selected = true;
  2631. }
  2632. }
  2633. }
  2634. if (element == selectionEnd.line)
  2635. {
  2636. if (element != renderStart.line)
  2637. {
  2638. selected = false;
  2639. break;
  2640. }
  2641. else
  2642. {
  2643. if (selectionEnd.column < renderStart.column)
  2644. {
  2645. selected = false;
  2646. }
  2647. }
  2648. }
  2649. if (element == renderStart.line)
  2650. {
  2651. break;
  2652. }
  2653. element = element->zAfter(false, 0);
  2654. }
  2655. int line = renderStartLine;
  2656. int x = lineNumberWidth + 26;
  2657. EditableJsonElement* current = renderStart.line;
  2658. EditableJsonElement* highlightStop = 0;
  2659. int index = renderStart.column;
  2660. bool treeBranchRendered = false;
  2661. bool highlighted = false;
  2662. int numberWidth = textRenderer->getTextBreite(Text(line));
  2663. textRenderer->renderText(
  2664. 5 + lineNumberWidth - numberWidth, y, Text(line), rObj, 0xFF707070);
  2665. bool whiteSpaceOnly = true;
  2666. while (y < gr.y && current)
  2667. {
  2668. const Text& content = current->getContent();
  2669. int column = index;
  2670. for (int i = index; i < content.getLength() && y < gr.y;
  2671. i++, column++)
  2672. {
  2673. if (selectionStart.line == current
  2674. && selectionStart.column == i)
  2675. {
  2676. selected = true;
  2677. }
  2678. if (selectionEnd.line == current && selectionEnd.column == i)
  2679. {
  2680. selected = false;
  2681. }
  2682. int color = current->getColor(i);
  2683. int cursorX = x - 1;
  2684. if (textCursor.line == current && textCursor.column == i
  2685. && drawCursor)
  2686. {
  2687. rObj.drawLinieV(
  2688. cursorX, y, textRenderer->getSchriftSize(), 0xFFC0C0C0);
  2689. }
  2690. if (content[i] == '\n')
  2691. {
  2692. column = -1;
  2693. highlighted = false;
  2694. if (!treeBranchRendered)
  2695. {
  2696. // | in tree view
  2697. rObj.drawLinieV(lineNumberWidth + 13,
  2698. y
  2699. - (line > 1 ? textRenderer->getSchriftSize() / 6
  2700. : 0),
  2701. textRenderer->getSchriftSize()
  2702. + (line > 1 ? textRenderer->getSchriftSize() / 6
  2703. : 0),
  2704. 0xFF808080);
  2705. if (highlightStop)
  2706. {
  2707. // hover effect in tree view
  2708. rObj.fillRegion(lineNumberWidth + 10,
  2709. y - textRenderer->getSchriftSize() / 6,
  2710. 7,
  2711. textRenderer->getSchriftSize()
  2712. + textRenderer->getSchriftSize() / 6,
  2713. 0xFF505050);
  2714. }
  2715. }
  2716. if (selected)
  2717. {
  2718. rObj.fillRegion(x,
  2719. y,
  2720. textRenderer->getTextBreite(" "),
  2721. textRenderer->getZeilenHeight()
  2722. + textRenderer->getZeilenAbstand(),
  2723. 0xFF502020);
  2724. }
  2725. x = lineNumberWidth + 26;
  2726. y += textRenderer->getZeilenHeight()
  2727. + textRenderer->getZeilenAbstand();
  2728. whiteSpaceOnly = true;
  2729. line++;
  2730. renderedLines++;
  2731. treeBranchRendered = false;
  2732. int numberWidth = textRenderer->getTextBreite(Text(line));
  2733. textRenderer->renderText(5 + lineNumberWidth - numberWidth,
  2734. y,
  2735. Text(line),
  2736. rObj,
  2737. 0xFF707070);
  2738. }
  2739. else
  2740. {
  2741. textRenderer->renderChar(x,
  2742. y,
  2743. content[i],
  2744. rObj,
  2745. color,
  2746. false,
  2747. selected,
  2748. 0xFF502020);
  2749. }
  2750. if (cursorPos.x > cursorX && cursorPos.x <= x
  2751. && cursorPos.y >= y
  2752. && cursorPos.y <= y + textRenderer->getZeilenHeight())
  2753. {
  2754. if (current->hasError(i))
  2755. {
  2756. const SyntaxError& error = current->getError(i);
  2757. errorDescription->setText(errors[error.getErrorType()]);
  2758. errorDescription->setSize(
  2759. errorDescription->getNeededWidth() + 6,
  2760. errorDescription->getNeededHeight() + 6);
  2761. errorDescription->setPosition(
  2762. cursorPos.x, cursorPos.y + 20);
  2763. if (errorDescription->getX()
  2764. + errorDescription->getBreite()
  2765. > getInnenBreite())
  2766. {
  2767. errorDescription->setX(
  2768. getInnenBreite() - errorDescription->getBreite()
  2769. - 5);
  2770. }
  2771. if (errorDescription->getY()
  2772. + errorDescription->getHeight()
  2773. > getInnenHeight())
  2774. {
  2775. errorDescription->setY(
  2776. getInnenHeight() - errorDescription->getHeight()
  2777. - 5);
  2778. }
  2779. errorDescription->addStyle(TextFeld::Style::Sichtbar);
  2780. }
  2781. }
  2782. if (column % 4 == 0 && content[i] == ' ' && whiteSpaceOnly)
  2783. {
  2784. rObj.drawLinieV(
  2785. cursorX + 1 + textRenderer->getTextBreite(" ") / 2,
  2786. y + 2,
  2787. textRenderer->getSchriftSize() - 4,
  2788. 0xFF353535);
  2789. }
  2790. whiteSpaceOnly &= content[i] == ' ' || content[i] == '\n';
  2791. }
  2792. if (selectionStart.line == current
  2793. && selectionStart.column == content.getLength())
  2794. {
  2795. selected = true;
  2796. }
  2797. if (selectionEnd.line == current
  2798. && selectionEnd.column == content.getLength())
  2799. {
  2800. selected = false;
  2801. }
  2802. if (textCursor.line == current
  2803. && textCursor.column == content.getLength() && drawCursor)
  2804. {
  2805. rObj.drawLinieV(
  2806. x - 1, y, textRenderer->getSchriftSize(), 0xFFC0C0C0);
  2807. }
  2808. if (current->zFirstChildren())
  2809. {
  2810. if (pressed && pressedPos.x >= lineNumberWidth + 10
  2811. && pressedPos.x <= lineNumberWidth + 16
  2812. && pressedPos.y
  2813. >= y + textRenderer->getSchriftSize() / 2 - 4
  2814. && pressedPos.y
  2815. <= y + textRenderer->getSchriftSize() / 2 + 2)
  2816. {
  2817. pressed = false;
  2818. current->setHidden(!current->isHidden());
  2819. }
  2820. if (cursorPos.x >= lineNumberWidth + 10
  2821. && cursorPos.x <= lineNumberWidth + 16
  2822. && cursorPos.y >= y + textRenderer->getSchriftSize() / 2 - 4
  2823. && cursorPos.y <= y + textRenderer->getSchriftSize() / 2 + 2
  2824. && !current->isHidden())
  2825. {
  2826. highlightStop = current->zLastChildren();
  2827. }
  2828. if (current->isHidden())
  2829. {
  2830. if (!treeBranchRendered)
  2831. {
  2832. rObj.fillRegion(lineNumberWidth + 10,
  2833. y + textRenderer->getSchriftSize() / 2 - 4,
  2834. 6,
  2835. 6,
  2836. 0xFF000000);
  2837. rObj.drawLinie(
  2838. Punkt(lineNumberWidth + 10,
  2839. y + textRenderer->getSchriftSize() / 2 - 4),
  2840. Punkt(lineNumberWidth + 16,
  2841. y + textRenderer->getSchriftSize() / 2 - 1),
  2842. 0xFF808080);
  2843. rObj.drawLinie(
  2844. Punkt(lineNumberWidth + 10,
  2845. y + textRenderer->getSchriftSize() / 2 + 2),
  2846. Punkt(lineNumberWidth + 16,
  2847. y + textRenderer->getSchriftSize() / 2 - 1),
  2848. 0xFF808080);
  2849. }
  2850. for (char c : " ... ")
  2851. {
  2852. textRenderer->renderChar(x,
  2853. y,
  2854. c,
  2855. rObj,
  2856. 0xFF505050,
  2857. false,
  2858. selected,
  2859. 0xFF502020);
  2860. }
  2861. if (current->getOpeningControllChar() == '{')
  2862. {
  2863. textRenderer->renderChar(x,
  2864. y,
  2865. '}',
  2866. rObj,
  2867. 0xFF505050,
  2868. false,
  2869. selected,
  2870. 0xFF502020);
  2871. }
  2872. else if (current->getOpeningControllChar() == '[')
  2873. {
  2874. textRenderer->renderChar(x,
  2875. y,
  2876. ']',
  2877. rObj,
  2878. 0xFF505050,
  2879. false,
  2880. selected,
  2881. 0xFF502020);
  2882. }
  2883. }
  2884. else
  2885. {
  2886. if (!treeBranchRendered)
  2887. {
  2888. rObj.fillRegion(lineNumberWidth + 10,
  2889. y + textRenderer->getSchriftSize() / 2 - 4,
  2890. 6,
  2891. 6,
  2892. 0xFF000000);
  2893. rObj.drawLinie(
  2894. Punkt(lineNumberWidth + 10,
  2895. y + textRenderer->getSchriftSize() / 2 - 4),
  2896. Punkt(lineNumberWidth + 13,
  2897. y + textRenderer->getSchriftSize() / 2 + 2),
  2898. 0xFF808080);
  2899. rObj.drawLinie(
  2900. Punkt(lineNumberWidth + 16,
  2901. y + textRenderer->getSchriftSize() / 2 - 4),
  2902. Punkt(lineNumberWidth + 13,
  2903. y + textRenderer->getSchriftSize() / 2 + 2),
  2904. 0xFF808080);
  2905. }
  2906. }
  2907. treeBranchRendered = true;
  2908. }
  2909. if (!current->zMextSibling() && current->zParent())
  2910. {
  2911. // |_ in tree view
  2912. rObj.drawLinieH(lineNumberWidth + 14,
  2913. y + textRenderer->getSchriftSize() - 1,
  2914. 3,
  2915. 0xFF808080);
  2916. }
  2917. rObj.drawLinieV(x,
  2918. y,
  2919. textRenderer->getSchriftSize(),
  2920. 0xFF30C030); // TODO: debug tree seperator
  2921. if (current == highlightStop)
  2922. {
  2923. highlightStop = 0;
  2924. if (!treeBranchRendered)
  2925. {
  2926. // hover effect in tree view
  2927. rObj.fillRegion(lineNumberWidth + 10,
  2928. y - textRenderer->getSchriftSize() / 6,
  2929. 7,
  2930. textRenderer->getSchriftSize()
  2931. + textRenderer->getSchriftSize() / 6,
  2932. 0xFF505050);
  2933. highlighted = true;
  2934. }
  2935. }
  2936. current = current->zAfter(true, &line);
  2937. index = 0;
  2938. }
  2939. if (!treeBranchRendered && !highlighted)
  2940. {
  2941. // | in tree view
  2942. rObj.drawLinieV(lineNumberWidth + 13,
  2943. y - (line > 1 ? textRenderer->getSchriftSize() / 6 : 0),
  2944. textRenderer->getSchriftSize()
  2945. + (line > 1 ? textRenderer->getSchriftSize() / 6 : 0),
  2946. 0xFF808080);
  2947. }
  2948. EditableJsonElement* resnderStopElement = current;
  2949. renderStopLine = line;
  2950. textRenderer->renderText(5,
  2951. y + textRenderer->getZeilenHeight()
  2952. + textRenderer->getZeilenAbstand(),
  2953. Text("FPS: ") + Text(tps),
  2954. rObj,
  2955. 0xFFFFFFFF);
  2956. bool visible = false;
  2957. double sy = 0.0;
  2958. double yOffset = (double)getInnenHeight() / lineCount;
  2959. current = content;
  2960. EditableJsonElement* hiddenStop = 0;
  2961. while (current)
  2962. {
  2963. if (hiddenStop == current)
  2964. {
  2965. hiddenStop = 0;
  2966. }
  2967. if (current == renderStart.line)
  2968. {
  2969. visible = true;
  2970. }
  2971. if (current == resnderStopElement)
  2972. {
  2973. visible = false;
  2974. }
  2975. int f = 0xFF303030;
  2976. if (visible)
  2977. {
  2978. f = 0xFFA0A0A0;
  2979. }
  2980. if (hiddenStop)
  2981. {
  2982. f = 0xFF505050;
  2983. }
  2984. if (current->hasError())
  2985. {
  2986. f = 0xFFFF0000;
  2987. }
  2988. double size = current->lineCount() * yOffset;
  2989. if ((int)(sy + size) > (int)sy || current->hasError())
  2990. {
  2991. rObj.fillRegion(getInnenBreite() - 15,
  2992. (int)sy,
  2993. 15,
  2994. (int)(sy + MAX(yOffset, 1)),
  2995. f);
  2996. }
  2997. if (current->isHidden())
  2998. {
  2999. if (!hiddenStop)
  3000. {
  3001. hiddenStop = current->zAfter(true, 0);
  3002. }
  3003. }
  3004. current = current->zAfter(false, 0);
  3005. sy += size;
  3006. }
  3007. errorDescription->render(rObj);
  3008. rObj.releaseDrawOptions();
  3009. }
  3010. }
  3011. Text Framework::JSON::JsonEditor::getContent()
  3012. {
  3013. return content ? content->getRecursiveContent() : Text("");
  3014. }
  3015. Text Framework::JSON::JsonEditor::getSelectedContent()
  3016. {
  3017. Text result = "";
  3018. if (selectionStart.line && selectionEnd.line)
  3019. {
  3020. if (selectionStart.line == selectionEnd.line)
  3021. {
  3022. Text* tmp = selectionStart.line->getContent().getTeilText(
  3023. selectionStart.column, selectionEnd.column);
  3024. result.append(*tmp);
  3025. tmp->release();
  3026. }
  3027. else
  3028. {
  3029. Text* tmp = selectionStart.line->getContent().getTeilText(
  3030. selectionStart.column,
  3031. selectionStart.line->getContent().getLength());
  3032. result.append(*tmp);
  3033. tmp->release();
  3034. Stack<EditableJsonElement*> stack(0);
  3035. stack.push(selectionStart.line);
  3036. while (stack.getSize())
  3037. {
  3038. EditableJsonElement* current = stack.pop();
  3039. if (current->zMextSibling())
  3040. {
  3041. stack.push(current->zMextSibling());
  3042. }
  3043. else if (stack.getSize() == 0 && current->zParent())
  3044. {
  3045. EditableJsonElement* parent = current->zParent();
  3046. while (parent)
  3047. {
  3048. if (parent->zMextSibling())
  3049. {
  3050. stack.push(current->zParent()->zMextSibling());
  3051. break;
  3052. }
  3053. parent = parent->zParent();
  3054. }
  3055. }
  3056. if (current->zFirstChildren())
  3057. {
  3058. stack.push(current->zFirstChildren());
  3059. }
  3060. if (current == selectionEnd.line)
  3061. {
  3062. break;
  3063. }
  3064. if (current != selectionStart.line)
  3065. {
  3066. result += current->getContent();
  3067. }
  3068. }
  3069. tmp = selectionEnd.line->getContent().getTeilText(
  3070. 0, selectionEnd.column);
  3071. result.append(*tmp);
  3072. tmp->release();
  3073. }
  3074. }
  3075. return result;
  3076. }
  3077. JSONValue* Framework::JSON::JsonEditor::getValidContent()
  3078. {
  3079. return nullptr; // TODO
  3080. }
  3081. void Framework::JSON::JsonEditor::scrollToLine(int line, ScrollTargetPos pos)
  3082. {
  3083. if (pos == Top || pos == Bottom)
  3084. {
  3085. if (line >= renderStartLine && line < renderStopLine)
  3086. {
  3087. return; // line is already visible
  3088. }
  3089. }
  3090. EditableJsonElement* element = content;
  3091. if (line == 1)
  3092. {
  3093. scrollToLine(line, {content, 0}, pos);
  3094. return;
  3095. }
  3096. int l = 1;
  3097. int column = 0;
  3098. while (element)
  3099. {
  3100. int lineCount = element->lineCount();
  3101. if (lineCount + l >= line)
  3102. {
  3103. column = element->getContent().positionVon('\n', line - l - 1) + 1;
  3104. break;
  3105. }
  3106. l += lineCount;
  3107. EditableJsonElement* tmp = element->zAfter(true, &l);
  3108. if (l >= line)
  3109. {
  3110. column = element->getContent().positionVon('\n', lineCount - 1) + 1;
  3111. break;
  3112. }
  3113. element = tmp;
  3114. }
  3115. EditorPosition target = {element, column};
  3116. unifyPosition(target);
  3117. scrollToLine(line, target, pos);
  3118. }
  3119. void Framework::JSON::JsonEditor::scrollToLine(
  3120. EditorPosition target, ScrollTargetPos pos)
  3121. {
  3122. if (!target.line || !target.line->isVisible())
  3123. {
  3124. return;
  3125. }
  3126. int lineNumber = 0;
  3127. if (!content->lineCount(true, true, target.line, lineNumber))
  3128. {
  3129. Text* part = target.line->getContent().getTeilText(0, target.column);
  3130. lineNumber += part->anzahlVon('\n');
  3131. part->release();
  3132. if (pos == Top || pos == Bottom)
  3133. {
  3134. if (lineNumber >= renderStartLine && lineNumber < renderStopLine)
  3135. {
  3136. return; // line is already visible
  3137. }
  3138. }
  3139. scrollToLine(lineNumber, target, pos);
  3140. }
  3141. }
  3142. void Framework::JSON::JsonEditor::scrollToLine(
  3143. int lineNum, EditorPosition target, ScrollTargetPos pos)
  3144. {
  3145. if (!target.line || !target.line->isVisible())
  3146. {
  3147. return;
  3148. }
  3149. if (pos == Top || pos == Bottom)
  3150. {
  3151. if (lineNum >= renderStartLine && lineNum < renderStopLine)
  3152. {
  3153. return; // line is already visible
  3154. }
  3155. }
  3156. if (pos == Top)
  3157. {
  3158. renderStart = target;
  3159. const char* content = renderStart.line->getContent();
  3160. while (
  3161. renderStart.column > 0 && content[renderStart.column - 1] != '\n')
  3162. {
  3163. renderStart.column--;
  3164. }
  3165. renderStartLine = lineNum;
  3166. }
  3167. else
  3168. {
  3169. int linesUntilStart
  3170. = pos == Bottom ? renderedLines - 1 : renderedLines / 2 - 1;
  3171. if (linesUntilStart >= lineNum)
  3172. {
  3173. renderStart = {content, 0};
  3174. renderStartLine = 1;
  3175. return;
  3176. }
  3177. EditableJsonElement* element = target.line;
  3178. EditableJsonElement* before = element;
  3179. while (linesUntilStart >= 0)
  3180. {
  3181. if (!before)
  3182. {
  3183. break;
  3184. }
  3185. element = before;
  3186. int newLines = 0;
  3187. if (element == target.line)
  3188. {
  3189. Text* part
  3190. = element->getContent().getTeilText(0, target.column);
  3191. newLines = part->anzahlVon('\n');
  3192. part->release();
  3193. }
  3194. else
  3195. {
  3196. newLines = element->lineCount();
  3197. }
  3198. if (newLines > linesUntilStart)
  3199. {
  3200. renderStart = {element,
  3201. element->getContent().positionVon(
  3202. '\n', newLines - linesUntilStart - 1)
  3203. + 1};
  3204. unifyPosition(renderStart);
  3205. renderStartLine = lineNum - linesUntilStart;
  3206. return;
  3207. }
  3208. lineNum -= newLines;
  3209. linesUntilStart -= newLines;
  3210. before = element->zBefore(this->content, true, &lineNum);
  3211. }
  3212. renderStart = {element, 0};
  3213. renderStartLine = lineNum;
  3214. }
  3215. }