JsonEditor.cpp 99 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300
  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. result->append(content.getTeilText(endIndex));
  1382. setContent(*result);
  1383. result->release();
  1384. return;
  1385. }
  1386. else
  1387. {
  1388. Array<EditableJsonElement*> toDelete;
  1389. Stack<EditableJsonElement*> stack;
  1390. if (siblings)
  1391. {
  1392. stack.push(siblings);
  1393. }
  1394. if (children)
  1395. {
  1396. stack.push(children);
  1397. }
  1398. if (!stack.getSize())
  1399. {
  1400. EditableJsonElement* p = parent;
  1401. while (p)
  1402. {
  1403. if (p->siblings)
  1404. {
  1405. stack.push(p->siblings);
  1406. break;
  1407. }
  1408. p = p->parent;
  1409. }
  1410. }
  1411. if (stack.getSize())
  1412. {
  1413. EditableJsonElement* next = stack.pop();
  1414. while (true)
  1415. {
  1416. if (next->siblings)
  1417. {
  1418. stack.push(siblings);
  1419. }
  1420. if (next->children)
  1421. {
  1422. stack.push(children);
  1423. }
  1424. if (!stack.getSize() && next != end)
  1425. {
  1426. EditableJsonElement* p = next->parent;
  1427. while (p)
  1428. {
  1429. if (p->siblings)
  1430. {
  1431. stack.push(p->siblings);
  1432. break;
  1433. }
  1434. p = p->parent;
  1435. }
  1436. }
  1437. if (next->parent)
  1438. {
  1439. next->parent->removeChild(next);
  1440. }
  1441. else
  1442. {
  1443. EditableJsonElement* p = this;
  1444. while (p->parent)
  1445. {
  1446. p = p->parent;
  1447. }
  1448. while (p->siblings != next)
  1449. {
  1450. p = p->siblings;
  1451. }
  1452. p->siblings = 0;
  1453. }
  1454. toDelete.add(next);
  1455. if (!stack.getSize())
  1456. {
  1457. next = 0;
  1458. break;
  1459. }
  1460. if (next == end)
  1461. {
  1462. break;
  1463. }
  1464. next = stack.pop();
  1465. }
  1466. if (next == end)
  1467. {
  1468. result->append(next->content.getTeilText(endIndex));
  1469. }
  1470. for (EditableJsonElement* del : toDelete)
  1471. {
  1472. del->disconnect();
  1473. del->release();
  1474. }
  1475. Array<EditableJsonElement*> afterList;
  1476. while (stack.getSize())
  1477. {
  1478. afterList.add(stack.pop());
  1479. }
  1480. int p = 0;
  1481. setContent(*result, p, afterList);
  1482. }
  1483. else
  1484. {
  1485. setContent(*result);
  1486. result->release();
  1487. return;
  1488. }
  1489. }
  1490. }
  1491. EditableJsonElement* Framework::JSON::EditableJsonElement::zBefore(
  1492. EditableJsonElement* zRoot, bool onlyVisible, int* lineCount)
  1493. {
  1494. if (this == zRoot)
  1495. {
  1496. return 0;
  1497. }
  1498. EditableJsonElement* parent = this->parent;
  1499. EditableJsonElement* current = parent ? parent->children : zRoot;
  1500. EditableJsonElement* last = parent;
  1501. while (current)
  1502. {
  1503. if (current == this)
  1504. {
  1505. break;
  1506. }
  1507. last = current;
  1508. current = current->siblings;
  1509. }
  1510. if (last && last->children != this && (!last->hidden || !onlyVisible))
  1511. {
  1512. current = last->lastChild;
  1513. while (current)
  1514. {
  1515. last = current;
  1516. if (current->children)
  1517. {
  1518. if ((!current->hidden || !onlyVisible))
  1519. {
  1520. current = current->lastChild;
  1521. }
  1522. else if (*lineCount)
  1523. {
  1524. EditableJsonElement* e = current->children;
  1525. while (e)
  1526. {
  1527. int lc = 0;
  1528. e->lineCount(true, false, 0, lc);
  1529. *lineCount -= lc;
  1530. e = e->siblings;
  1531. }
  1532. break;
  1533. }
  1534. }
  1535. else
  1536. {
  1537. break;
  1538. }
  1539. }
  1540. }
  1541. else if (last && last->hidden && onlyVisible && lineCount)
  1542. {
  1543. EditableJsonElement* e = last->children;
  1544. while (e)
  1545. {
  1546. int lc = 0;
  1547. e->lineCount(true, false, 0, lc);
  1548. *lineCount -= lc;
  1549. e = e->siblings;
  1550. }
  1551. }
  1552. return last;
  1553. }
  1554. EditableJsonElement* Framework::JSON::EditableJsonElement::zAfter(
  1555. bool onlyVisible, int* lineCount)
  1556. {
  1557. EditableJsonElement* zElement = this;
  1558. if (zElement->children)
  1559. {
  1560. if (!zElement->hidden || !onlyVisible)
  1561. {
  1562. return zElement->children;
  1563. }
  1564. else if (lineCount)
  1565. {
  1566. EditableJsonElement* e = zElement->children;
  1567. while (e)
  1568. {
  1569. int lc = 0;
  1570. e->lineCount(true, false, 0, lc);
  1571. *lineCount += lc;
  1572. e = e->siblings;
  1573. }
  1574. }
  1575. }
  1576. if (zElement->siblings)
  1577. {
  1578. return zElement->siblings;
  1579. }
  1580. while (zElement->parent)
  1581. {
  1582. zElement = zElement->parent;
  1583. if (zElement->siblings)
  1584. {
  1585. return zElement->siblings;
  1586. }
  1587. }
  1588. return 0;
  1589. }
  1590. JsonEditor::JsonEditor(UIInit uiInit)
  1591. : ZeichnungHintergrund(),
  1592. uiInit(uiInit)
  1593. {
  1594. valueValidator = Regex::parse("^-?[0-9]+(\\.[0-9]+)?([eE][+-]?[0-9]+)?$");
  1595. validator = 0;
  1596. content = 0;
  1597. renderStart = {0, 0};
  1598. renderStartLine = 0;
  1599. lineCount = 0;
  1600. cursorPos.x = -1;
  1601. cursorPos.y = -1;
  1602. textCursor = {0, 0};
  1603. selectionStart = {0, 0};
  1604. renderStartOffset = 0;
  1605. selectionEnd = {0, 0};
  1606. textRenderer = new TextRenderer();
  1607. textRenderer->setSchriftSize(12);
  1608. textRenderer->setZeilenAbstand(2);
  1609. textRenderer->setZeichenAbstand(1);
  1610. pressed = 0;
  1611. time = 0;
  1612. drag = 0;
  1613. drawCursor = 0;
  1614. dragSart = {-1, -1};
  1615. pressedPos = {-1, -1};
  1616. dragStartPos = {0, -1};
  1617. timeSicePress = 0.0;
  1618. lastClickCursorPos = {0, 0};
  1619. tps = 0;
  1620. renderings = 0;
  1621. renderedLines = 0;
  1622. renderStopLine = 0;
  1623. errorDescription = uiInit.createTextFeld(uiInit.initParam);
  1624. errorDescription->setStyle(TextFeld::Style::Text | TextFeld::Style::Center
  1625. | TextFeld::Style::Rahmen
  1626. | TextFeld::Style::Hintergrund
  1627. | TextFeld::Style::HAlpha);
  1628. errorDescription->removeStyle(TextFeld::Style::Sichtbar);
  1629. errorDescription->setSchriftFarbe(0xFFFFFFFF);
  1630. errorDescription->setRahmenFarbe(0xFFA0A0A0);
  1631. errorDescription->setHintergrundFarbe(0xA0000000);
  1632. hasSyntaxError = false;
  1633. }
  1634. JsonEditor::~JsonEditor()
  1635. {
  1636. if (content) content->release();
  1637. if (validator) validator->release();
  1638. textRenderer->release();
  1639. errorDescription->release();
  1640. }
  1641. void Framework::JSON::JsonEditor::doMausEreignis(MausEreignis& me, bool userRet)
  1642. {
  1643. LOCK(&cs);
  1644. rend |= cursorPos.x != me.mx || cursorPos.y != me.my;
  1645. cursorPos.x = me.mx;
  1646. cursorPos.y = me.my;
  1647. if (me.id == ME_RLinks)
  1648. {
  1649. pressedPos = cursorPos;
  1650. if (timeSicePress < 0.5)
  1651. {
  1652. if (lastClickCursorPos.line == textCursor.line
  1653. && lastClickCursorPos.column == textCursor.column
  1654. && lastClickCursorPos.line && lastClickCursorPos.column > 0)
  1655. {
  1656. EditableJsonElement* current = textCursor.line;
  1657. int start = 0;
  1658. int end = 0;
  1659. current->getWordBounds(textCursor.column, &start, &end);
  1660. selectionStart = {current, start};
  1661. selectionEnd = {current, end};
  1662. unifyPosition(selectionEnd);
  1663. textCursor = selectionEnd;
  1664. }
  1665. }
  1666. lastClickCursorPos = textCursor;
  1667. pressed = true;
  1668. drag = false;
  1669. rend = 1;
  1670. time = 0;
  1671. timeSicePress = 0.0;
  1672. }
  1673. if (me.id == ME_PLinks || (me.id == ME_Betritt && getMausStand(M_Links)))
  1674. {
  1675. if (me.mx >= getInnenBreite() - 15)
  1676. {
  1677. int line = (int)(me.my / ((double)getInnenHeight() / lineCount));
  1678. if (line >= lineCount)
  1679. {
  1680. line = lineCount - 1;
  1681. }
  1682. scrollToLine(line + 1, Center);
  1683. }
  1684. if (!drag)
  1685. {
  1686. drag = true;
  1687. textCursor = getScreenPosition(cursorPos.x, cursorPos.y);
  1688. selectionStart = textCursor;
  1689. selectionEnd = textCursor;
  1690. dragSart = cursorPos;
  1691. dragStartPos = textCursor;
  1692. rend = 1;
  1693. time = 0;
  1694. }
  1695. }
  1696. if (me.id == ME_Bewegung)
  1697. {
  1698. if (drag)
  1699. {
  1700. if (dragSart.x >= getInnenBreite() - 15)
  1701. {
  1702. int line
  1703. = (int)(me.my / ((double)getInnenHeight() / lineCount));
  1704. if (line >= lineCount)
  1705. {
  1706. line = lineCount - 1;
  1707. }
  1708. scrollToLine(line + 1, Center);
  1709. }
  1710. else
  1711. {
  1712. EditorPosition pos
  1713. = getScreenPosition(cursorPos.x, cursorPos.y);
  1714. if (pos.line == dragStartPos.line)
  1715. {
  1716. if (pos.column < dragStartPos.column)
  1717. {
  1718. selectionStart = pos;
  1719. selectionEnd = dragStartPos;
  1720. }
  1721. else if (pos.column > dragStartPos.column)
  1722. {
  1723. selectionStart = dragStartPos;
  1724. selectionEnd = pos;
  1725. }
  1726. }
  1727. else
  1728. {
  1729. if (cursorPos.y < dragSart.y)
  1730. {
  1731. selectionStart = pos;
  1732. selectionEnd = dragStartPos;
  1733. }
  1734. else if (cursorPos.y > dragSart.y)
  1735. {
  1736. selectionStart = dragStartPos;
  1737. selectionEnd = pos;
  1738. }
  1739. }
  1740. textCursor = pos;
  1741. }
  1742. }
  1743. }
  1744. if (me.id == ME_UScroll)
  1745. {
  1746. if (renderStart.column > 0)
  1747. {
  1748. const char* content = renderStart.line->getContent();
  1749. int index = renderStart.column - 2;
  1750. while (index > 0 && content[index] != '\n')
  1751. {
  1752. index--;
  1753. }
  1754. if (content[index] == '\n')
  1755. {
  1756. renderStart.column = index + 1;
  1757. rend = 1;
  1758. renderStartLine--;
  1759. unifyPosition(renderStart);
  1760. return;
  1761. }
  1762. }
  1763. while (true)
  1764. {
  1765. EditableJsonElement* next
  1766. = renderStart.line->zBefore(content, true, &renderStartLine);
  1767. if (next)
  1768. {
  1769. renderStart.line = next;
  1770. const char* content = next->getContent();
  1771. int index = next->getContent().getLength() - 1;
  1772. while (index > 0 && content[index] != '\n')
  1773. {
  1774. index--;
  1775. }
  1776. if (content[index] == '\n')
  1777. {
  1778. renderStart.column = index + 1;
  1779. rend = 1;
  1780. renderStartLine--;
  1781. unifyPosition(renderStart);
  1782. return;
  1783. }
  1784. }
  1785. else
  1786. {
  1787. renderStart.column = 0;
  1788. rend = 1;
  1789. renderStartLine = 1;
  1790. return;
  1791. }
  1792. }
  1793. }
  1794. if (me.id == ME_DScroll)
  1795. {
  1796. const char* content = renderStart.line->getContent();
  1797. int length = renderStart.line->getContent().getLength();
  1798. int index = renderStart.column;
  1799. while (index < length && content[index] != '\n')
  1800. {
  1801. index++;
  1802. }
  1803. if (content[index] == '\n')
  1804. {
  1805. renderStart.column = index + 1;
  1806. rend = 1;
  1807. renderStartLine++;
  1808. unifyPosition(renderStart);
  1809. return;
  1810. }
  1811. while (true)
  1812. {
  1813. EditableJsonElement* next
  1814. = renderStart.line->zAfter(true, &renderStartLine);
  1815. if (next)
  1816. {
  1817. renderStart.line = next;
  1818. const char* content = next->getContent();
  1819. int length = next->getContent().getLength();
  1820. int index = 0;
  1821. while (index < length && content[index] != '\n')
  1822. {
  1823. index++;
  1824. }
  1825. if (content[index] == '\n')
  1826. {
  1827. renderStart.column = index + 1;
  1828. rend = 1;
  1829. renderStartLine++;
  1830. unifyPosition(renderStart);
  1831. return;
  1832. }
  1833. }
  1834. else
  1835. {
  1836. return;
  1837. }
  1838. }
  1839. }
  1840. }
  1841. EditorPosition Framework::JSON::JsonEditor::getScreenPosition(
  1842. int localX, int localY)
  1843. {
  1844. if (!renderStart.line) return {0, 0};
  1845. int y = 0;
  1846. int lineNumberWidth = 0;
  1847. for (int i = lineCount; i > 0; i /= 10)
  1848. {
  1849. lineNumberWidth++;
  1850. }
  1851. lineNumberWidth = lineNumberWidth * textRenderer->getMaxCharWidth()
  1852. + textRenderer->getZeichenAbstand() * (lineNumberWidth - 1);
  1853. int x = lineNumberWidth + 26;
  1854. EditableJsonElement* current = renderStart.line;
  1855. int index = renderStart.column;
  1856. EditableJsonElement* last = current;
  1857. while (y < gr.y && current)
  1858. {
  1859. int length = current->getContent().getLength();
  1860. const char* content = current->getContent();
  1861. for (int i = index; i < length; i++)
  1862. {
  1863. if (y >= localY - textRenderer->getZeilenHeight()
  1864. - textRenderer->getZeilenAbstand()
  1865. && x >= localX - textRenderer->getCharWidth(content[i]) / 2
  1866. - textRenderer->getZeichenAbstand())
  1867. {
  1868. EditorPosition result = {current, i};
  1869. unifyPosition(result);
  1870. return result;
  1871. }
  1872. if (content[i] == '\n')
  1873. {
  1874. if (y >= localY - textRenderer->getZeilenHeight()
  1875. - textRenderer->getZeilenAbstand())
  1876. {
  1877. EditorPosition result = {current, i};
  1878. unifyPosition(result);
  1879. return result;
  1880. }
  1881. y += textRenderer->getZeilenHeight()
  1882. + textRenderer->getZeilenAbstand();
  1883. x = lineNumberWidth + 26;
  1884. }
  1885. else
  1886. {
  1887. x += textRenderer->getCharWidth(content[i])
  1888. + textRenderer->getZeichenAbstand();
  1889. }
  1890. }
  1891. last = current;
  1892. current = current->zAfter(true, 0);
  1893. index = 0;
  1894. }
  1895. return {last, last->getContent().getLength()};
  1896. }
  1897. void Framework::JSON::JsonEditor::deleteSelection()
  1898. {
  1899. if (selectionStart.line && selectionEnd.line)
  1900. {
  1901. selectionStart.line->removeUntil(
  1902. selectionStart.column, selectionEnd.line, selectionEnd.column);
  1903. selectionEnd = selectionStart;
  1904. textCursor = selectionStart;
  1905. fixTree(selectionStart.line);
  1906. if (dragStartPos.line)
  1907. {
  1908. dragStartPos = selectionStart;
  1909. }
  1910. if (lastClickCursorPos.line)
  1911. {
  1912. lastClickCursorPos = selectionStart;
  1913. }
  1914. }
  1915. }
  1916. void Framework::JSON::JsonEditor::unifyPosition(EditorPosition& pos)
  1917. {
  1918. while (pos.line && pos.column >= pos.line->getContent().getLength())
  1919. {
  1920. if (pos.line->zFirstChildren() && pos.line->isVisible())
  1921. {
  1922. pos = {pos.line->zFirstChildren(),
  1923. pos.column - pos.line->getContent().getLength()};
  1924. }
  1925. else if (pos.line->zMextSibling())
  1926. {
  1927. pos = {pos.line->zMextSibling(),
  1928. pos.column - pos.line->getContent().getLength()};
  1929. }
  1930. else
  1931. {
  1932. EditableJsonElement* p = pos.line->zParent();
  1933. while (p)
  1934. {
  1935. if (p->zMextSibling())
  1936. {
  1937. pos = {p->zMextSibling(), 0};
  1938. break;
  1939. }
  1940. p = p->zParent();
  1941. }
  1942. if (!p)
  1943. {
  1944. pos.column = pos.line->getContent().getLength();
  1945. break;
  1946. }
  1947. }
  1948. }
  1949. }
  1950. void Framework::JSON::JsonEditor::fixTree(EditableJsonElement* zElement)
  1951. {
  1952. EditableJsonElement* before = zElement->zBefore(content, false, 0);
  1953. int beforeLength = before ? before->getContent().getLength() : 0;
  1954. if (zElement->hasError() && before)
  1955. {
  1956. Text content = before->getContent();
  1957. before->setContent(content);
  1958. if (before->getContent().getLength() != beforeLength)
  1959. {
  1960. before->makeVisible();
  1961. }
  1962. }
  1963. if (textCursor.line == zElement)
  1964. {
  1965. textCursor.column += beforeLength;
  1966. textCursor.line = before ? before : zElement;
  1967. unifyPosition(textCursor);
  1968. }
  1969. if (renderStart.line == zElement)
  1970. {
  1971. renderStart.column += beforeLength;
  1972. renderStart.line = before ? before : zElement;
  1973. unifyPosition(renderStart);
  1974. }
  1975. }
  1976. void Framework::JSON::JsonEditor::setFont(Schrift* schrift)
  1977. {
  1978. textRenderer->setSchriftZ(schrift);
  1979. }
  1980. void Framework::JSON::JsonEditor::setFontSize(int size)
  1981. {
  1982. textRenderer->setSchriftSize(size);
  1983. textRenderer->setZeilenAbstand(size / 6);
  1984. textRenderer->setZeichenAbstand(size / 8);
  1985. }
  1986. void JsonEditor::setContent(Text content)
  1987. {
  1988. if (this->content)
  1989. {
  1990. this->content->release();
  1991. this->content = 0;
  1992. this->renderStart = {0, 0};
  1993. }
  1994. this->content = new EditableJsonElement(
  1995. dynamic_cast<Regex::Automata<char>*>(valueValidator->getThis()));
  1996. content.ersetzen("\r", "");
  1997. content.ersetzen("\t", " ");
  1998. this->content->setContent(content);
  1999. textCursor = {this->content, 0};
  2000. selectionStart = {this->content, 0};
  2001. selectionEnd = {this->content, 0};
  2002. format();
  2003. }
  2004. void Framework::JSON::JsonEditor::setContent(JSONValue* content)
  2005. {
  2006. setContent(content->toString());
  2007. content->release();
  2008. }
  2009. void Framework::JSON::JsonEditor::format()
  2010. {
  2011. if (content)
  2012. {
  2013. content->format();
  2014. lineCount = 0;
  2015. content->lineCount(true, true, 0, lineCount);
  2016. }
  2017. }
  2018. void Framework::JSON::JsonEditor::setValidator(
  2019. Validator::DataValidator* validator)
  2020. {
  2021. if (this->validator) this->validator->release();
  2022. this->validator = validator;
  2023. // if (content) validate(); TODO: validate attributes
  2024. }
  2025. void Framework::JSON::JsonEditor::doTastaturEreignis(TastaturEreignis& te)
  2026. {
  2027. LOCK(&cs);
  2028. if (!textCursor.line)
  2029. {
  2030. return;
  2031. }
  2032. bool shift = getTastenStand(T_Shift);
  2033. bool strg = getTastenStand(T_Strg);
  2034. if (te.id == TE_Press)
  2035. {
  2036. switch (te.virtualKey)
  2037. {
  2038. case T_Tab:
  2039. {
  2040. Text illegalStarts = "[]{},\": ";
  2041. if (shift)
  2042. {
  2043. bool lastStr
  2044. = textCursor.line->getContent().getLength()
  2045. > textCursor.column
  2046. ? textCursor.line->getContent()[textCursor.column]
  2047. == '"'
  2048. : false;
  2049. int nextColumn = textCursor.column - 1;
  2050. EditableJsonElement* nextLine = textCursor.line;
  2051. while (nextLine)
  2052. {
  2053. if (nextColumn < 0)
  2054. {
  2055. nextLine = nextLine->zBefore(content, true, 0);
  2056. if (!nextLine)
  2057. {
  2058. return;
  2059. }
  2060. nextColumn = nextLine->getContent().getLength();
  2061. }
  2062. int start = 0;
  2063. int end = 0;
  2064. nextLine->getWordBounds(nextColumn, &start, &end);
  2065. if ((start != end
  2066. && !illegalStarts.hat(
  2067. nextLine->getContent()[start]))
  2068. || (lastStr
  2069. && nextLine->getContent()[nextColumn] == '"'))
  2070. {
  2071. if (lastStr
  2072. && nextLine->getContent()[nextColumn] == '"')
  2073. {
  2074. start = end;
  2075. }
  2076. if (selectionStart.column != start
  2077. || selectionStart.line != nextLine
  2078. || selectionEnd.column != end
  2079. || selectionEnd.line != nextLine)
  2080. {
  2081. selectionStart = {nextLine, start};
  2082. selectionEnd = {nextLine, end};
  2083. unifyPosition(selectionEnd);
  2084. textCursor = selectionEnd;
  2085. scrollToLine(textCursor, Top);
  2086. te.verarbeitet = true;
  2087. time = 0;
  2088. rend = 1;
  2089. return;
  2090. }
  2091. }
  2092. lastStr = nextLine->getContent()[nextColumn] == '"';
  2093. nextColumn--;
  2094. }
  2095. }
  2096. else
  2097. {
  2098. bool lastStr
  2099. = textCursor.line->getContent().getLength()
  2100. > textCursor.column
  2101. ? textCursor.line->getContent()[textCursor.column]
  2102. == '"'
  2103. : false;
  2104. int nextColumn = textCursor.column + 1;
  2105. EditableJsonElement* nextLine = textCursor.line;
  2106. while (nextLine)
  2107. {
  2108. if (nextColumn > nextLine->getContent().getLength())
  2109. {
  2110. nextLine = nextLine->zAfter(true, 0);
  2111. if (!nextLine)
  2112. {
  2113. return;
  2114. }
  2115. nextColumn = 0;
  2116. }
  2117. int start = 0;
  2118. int end = 0;
  2119. nextLine->getWordBounds(nextColumn, &start, &end);
  2120. if (((start != end
  2121. && !illegalStarts.hat(
  2122. nextLine->getContent()[start]))
  2123. || (lastStr
  2124. && nextLine->getContent()[nextColumn]
  2125. == '"'))
  2126. && (selectionStart.column != start
  2127. || selectionStart.line != nextLine
  2128. || selectionEnd.column != end
  2129. || selectionEnd.line != nextLine))
  2130. {
  2131. selectionStart = {nextLine, start};
  2132. selectionEnd = {nextLine, end};
  2133. unifyPosition(selectionEnd);
  2134. textCursor = selectionEnd;
  2135. scrollToLine(textCursor, Bottom);
  2136. te.verarbeitet = true;
  2137. time = 0;
  2138. rend = 1;
  2139. return;
  2140. }
  2141. lastStr = nextLine->getContent()[nextColumn] == '"';
  2142. nextColumn++;
  2143. }
  2144. }
  2145. }
  2146. return;
  2147. case T_Entf:
  2148. if (selectionStart.line == selectionEnd.line
  2149. && selectionStart.column == selectionEnd.column)
  2150. {
  2151. if (textCursor.column
  2152. < textCursor.line->getContent().getLength())
  2153. {
  2154. Text content = textCursor.line->getContent();
  2155. content.remove(textCursor.column, textCursor.column + 1);
  2156. textCursor.line->setContent(content);
  2157. fixTree(textCursor.line);
  2158. }
  2159. }
  2160. else
  2161. {
  2162. deleteSelection();
  2163. }
  2164. break;
  2165. case T_BackSpace:
  2166. if (selectionStart.line == selectionEnd.line
  2167. && selectionStart.column == selectionEnd.column)
  2168. {
  2169. if (textCursor.column > 0)
  2170. {
  2171. Text content = textCursor.line->getContent();
  2172. content.remove(textCursor.column - 1, textCursor.column);
  2173. textCursor.line->setContent(content);
  2174. textCursor.column--;
  2175. fixTree(textCursor.line);
  2176. }
  2177. else
  2178. {
  2179. EditableJsonElement* before
  2180. = textCursor.line->zBefore(content, false, 0);
  2181. if (before)
  2182. {
  2183. before->makeVisible();
  2184. Text content = before->getContent();
  2185. content.remove(
  2186. content.getLength() - 1, content.getLength());
  2187. before->setContent(content);
  2188. textCursor = {before, content.getLength()};
  2189. fixTree(before);
  2190. scrollToLine(textCursor, Top);
  2191. }
  2192. }
  2193. }
  2194. else
  2195. {
  2196. deleteSelection();
  2197. }
  2198. break;
  2199. case T_Enter:
  2200. {
  2201. deleteSelection();
  2202. Text content = textCursor.line->getContent();
  2203. content.insert(textCursor.column, "\n");
  2204. textCursor.column += 1;
  2205. textCursor.line->setContent(content);
  2206. fixTree(textCursor.line);
  2207. scrollToLine(textCursor, Bottom);
  2208. }
  2209. break;
  2210. case T_Links:
  2211. {
  2212. int nextColumn = textCursor.column - 1;
  2213. EditableJsonElement* nextLine = textCursor.line;
  2214. while (nextColumn < 0)
  2215. {
  2216. nextLine = textCursor.line->zBefore(content, true, 0);
  2217. if (!nextLine)
  2218. {
  2219. return;
  2220. }
  2221. nextColumn = nextLine->getContent().getLength() - 1;
  2222. }
  2223. if (strg)
  2224. {
  2225. int start = 0;
  2226. nextLine->getWordBounds(nextColumn, &start, 0);
  2227. nextColumn = start;
  2228. }
  2229. if (shift)
  2230. {
  2231. if (textCursor.line == selectionStart.line
  2232. && textCursor.column == selectionStart.column)
  2233. {
  2234. selectionStart = {nextLine, nextColumn};
  2235. unifyPosition(selectionStart);
  2236. textCursor = selectionStart;
  2237. }
  2238. else
  2239. {
  2240. selectionEnd = {nextLine, nextColumn};
  2241. unifyPosition(selectionEnd);
  2242. textCursor = selectionEnd;
  2243. }
  2244. }
  2245. else
  2246. {
  2247. textCursor = {nextLine, nextColumn};
  2248. unifyPosition(textCursor);
  2249. selectionStart = textCursor;
  2250. selectionEnd = textCursor;
  2251. }
  2252. scrollToLine(textCursor, Top);
  2253. te.verarbeitet = true;
  2254. time = 0;
  2255. rend = 1;
  2256. return;
  2257. }
  2258. case T_Oben:
  2259. {
  2260. EditableJsonElement* current = textCursor.line;
  2261. int count = 0;
  2262. bool found = 0;
  2263. int index = textCursor.column - 1;
  2264. const char* content = current->getContent();
  2265. while (current)
  2266. {
  2267. while (index < 0)
  2268. {
  2269. current = current->zBefore(this->content, true, 0);
  2270. if (!current)
  2271. {
  2272. break;
  2273. }
  2274. content = current->getContent();
  2275. index = current->getContent().getLength() - 1;
  2276. }
  2277. if (!current)
  2278. {
  2279. break;
  2280. }
  2281. if (content[index] == '\n')
  2282. {
  2283. if (found)
  2284. {
  2285. break;
  2286. }
  2287. found = 1;
  2288. }
  2289. if (!found)
  2290. {
  2291. count++;
  2292. }
  2293. index--;
  2294. }
  2295. if (!current)
  2296. {
  2297. break;
  2298. }
  2299. index++;
  2300. while (current && count > 0)
  2301. {
  2302. while (index >= current->getContent().getLength())
  2303. {
  2304. current = current->zAfter(true, 0);
  2305. if (!current)
  2306. {
  2307. break;
  2308. }
  2309. content = current->getContent();
  2310. index = 0;
  2311. }
  2312. if (!current)
  2313. {
  2314. break;
  2315. }
  2316. if (content[index] == '\n')
  2317. {
  2318. break;
  2319. }
  2320. count--;
  2321. index++;
  2322. }
  2323. if (!current)
  2324. {
  2325. break;
  2326. }
  2327. if (shift)
  2328. {
  2329. if (textCursor.line == selectionStart.line
  2330. && textCursor.column == selectionStart.column)
  2331. {
  2332. selectionStart = {current, index};
  2333. unifyPosition(selectionStart);
  2334. textCursor = selectionStart;
  2335. }
  2336. else
  2337. {
  2338. selectionEnd = {current, index};
  2339. unifyPosition(selectionEnd);
  2340. textCursor = selectionEnd;
  2341. }
  2342. }
  2343. else
  2344. {
  2345. textCursor = {current, index};
  2346. unifyPosition(textCursor);
  2347. selectionStart = textCursor;
  2348. selectionEnd = textCursor;
  2349. }
  2350. scrollToLine(textCursor, Top);
  2351. te.verarbeitet = true;
  2352. time = 0;
  2353. rend = 1;
  2354. return;
  2355. }
  2356. case T_Rechts:
  2357. {
  2358. int nextColumn = textCursor.column + 1;
  2359. EditableJsonElement* nextLine = textCursor.line;
  2360. if (nextColumn > nextLine->getContent().getLength())
  2361. {
  2362. nextLine = textCursor.line->zAfter(true, 0);
  2363. if (!nextLine)
  2364. {
  2365. return;
  2366. }
  2367. nextColumn = 0;
  2368. }
  2369. if (strg)
  2370. {
  2371. int end = 0;
  2372. nextLine->getWordBounds(nextColumn, 0, &end);
  2373. nextColumn = end;
  2374. }
  2375. if (shift)
  2376. {
  2377. if (textCursor.line == selectionEnd.line
  2378. && textCursor.column == selectionEnd.column)
  2379. {
  2380. selectionEnd = {nextLine, nextColumn};
  2381. unifyPosition(selectionEnd);
  2382. textCursor = selectionEnd;
  2383. }
  2384. else
  2385. {
  2386. selectionStart = {nextLine, nextColumn};
  2387. unifyPosition(selectionStart);
  2388. textCursor = selectionStart;
  2389. }
  2390. }
  2391. else
  2392. {
  2393. textCursor = {nextLine, nextColumn};
  2394. unifyPosition(textCursor);
  2395. selectionStart = textCursor;
  2396. selectionEnd = textCursor;
  2397. }
  2398. scrollToLine(textCursor, Bottom);
  2399. te.verarbeitet = true;
  2400. time = 0;
  2401. rend = 1;
  2402. return;
  2403. }
  2404. case T_Unten:
  2405. {
  2406. EditableJsonElement* current = textCursor.line;
  2407. int count = 0;
  2408. int index = textCursor.column - 1;
  2409. const char* content = current->getContent();
  2410. while (current)
  2411. {
  2412. while (index < 0)
  2413. {
  2414. current = current->zBefore(this->content, true, 0);
  2415. if (!current)
  2416. {
  2417. break;
  2418. }
  2419. content = current->getContent();
  2420. index = current->getContent().getLength() - 1;
  2421. }
  2422. if (!current)
  2423. {
  2424. break;
  2425. }
  2426. if (content[index] == '\n')
  2427. {
  2428. break;
  2429. }
  2430. count++;
  2431. index--;
  2432. }
  2433. bool found = 0;
  2434. current = textCursor.line;
  2435. index = textCursor.column;
  2436. content = current->getContent();
  2437. while (current && (count > 0 || !found))
  2438. {
  2439. while (index >= current->getContent().getLength())
  2440. {
  2441. current = current->zAfter(true, 0);
  2442. if (!current)
  2443. {
  2444. break;
  2445. }
  2446. content = current->getContent();
  2447. index = 0;
  2448. }
  2449. if (found)
  2450. {
  2451. count--;
  2452. }
  2453. if (content[index] == '\n')
  2454. {
  2455. if (found)
  2456. {
  2457. break;
  2458. }
  2459. found = 1;
  2460. }
  2461. index++;
  2462. }
  2463. if (shift)
  2464. {
  2465. if (textCursor.line == selectionEnd.line
  2466. && textCursor.column == selectionEnd.column)
  2467. {
  2468. selectionEnd = {current, index};
  2469. unifyPosition(selectionEnd);
  2470. textCursor = selectionEnd;
  2471. }
  2472. else
  2473. {
  2474. selectionStart = {current, index};
  2475. unifyPosition(selectionStart);
  2476. textCursor = selectionStart;
  2477. }
  2478. }
  2479. else
  2480. {
  2481. textCursor = {current, index};
  2482. unifyPosition(textCursor);
  2483. selectionStart = textCursor;
  2484. selectionEnd = textCursor;
  2485. }
  2486. scrollToLine(textCursor, Bottom);
  2487. te.verarbeitet = true;
  2488. time = 0;
  2489. rend = 1;
  2490. return;
  2491. }
  2492. default:
  2493. if (strg)
  2494. {
  2495. if (te.virtualKey == 'c' || te.virtualKey == 'C')
  2496. {
  2497. Text content = getSelectedContent();
  2498. TextKopieren(content);
  2499. return;
  2500. }
  2501. else if (te.virtualKey == 'x' || te.virtualKey == 'X')
  2502. {
  2503. Text content = getSelectedContent();
  2504. TextKopieren(content);
  2505. deleteSelection();
  2506. scrollToLine(textCursor, Top);
  2507. return;
  2508. }
  2509. else if (te.virtualKey == 'v' || te.virtualKey == 'V')
  2510. {
  2511. deleteSelection();
  2512. EditableJsonElement* changed = textCursor.line;
  2513. Text content = changed->getContent();
  2514. const char* inserted = TextInsert();
  2515. content.insert(textCursor.column, inserted);
  2516. textCursor.column += textLength(inserted);
  2517. unifyPosition(textCursor);
  2518. scrollToLine(textCursor, Bottom);
  2519. break;
  2520. }
  2521. else if (te.virtualKey == 'a' || te.virtualKey == 'A')
  2522. {
  2523. selectionStart = {content, 0};
  2524. EditableJsonElement* element = content;
  2525. bool first = true;
  2526. while (element->zMextSibling())
  2527. {
  2528. element = element->zMextSibling();
  2529. }
  2530. while (element->zLastChildren())
  2531. {
  2532. element = element->zLastChildren();
  2533. }
  2534. selectionEnd = {element, element->getContent().getLength()};
  2535. textCursor = selectionEnd;
  2536. te.verarbeitet = true;
  2537. time = 0;
  2538. rend = 1;
  2539. scrollToLine(textCursor, Bottom);
  2540. return;
  2541. }
  2542. else if (te.virtualKey == 'f' || te.virtualKey == 'F')
  2543. {
  2544. format();
  2545. return;
  2546. }
  2547. }
  2548. if (istSchreibbar(te.taste[0]))
  2549. {
  2550. deleteSelection();
  2551. Text content = textCursor.line->getContent();
  2552. content.insert(textCursor.column, te.taste);
  2553. textCursor.column += textLength(te.taste);
  2554. textCursor.line->setContent(content);
  2555. fixTree(textCursor.line);
  2556. }
  2557. else
  2558. {
  2559. return;
  2560. }
  2561. }
  2562. selectionStart = textCursor;
  2563. selectionEnd = textCursor;
  2564. te.verarbeitet = true;
  2565. lineCount = 0;
  2566. content->lineCount(true, true, 0, lineCount);
  2567. }
  2568. time = 0;
  2569. rend = 1;
  2570. }
  2571. bool Framework::JSON::JsonEditor::tick(double tickVal)
  2572. {
  2573. LOCK(&cs);
  2574. time += tickVal;
  2575. timeSicePress += tickVal;
  2576. if (time > 1.0)
  2577. {
  2578. tps = renderings / time;
  2579. time = 0.0;
  2580. renderings = 0;
  2581. }
  2582. rend |= drawCursor != (time < 0.5);
  2583. drawCursor = time < 0.5;
  2584. errorDescription->tick(tickVal);
  2585. return ZeichnungHintergrund::tick(tickVal);
  2586. }
  2587. void Framework::JSON::JsonEditor::render(Bild& rObj)
  2588. {
  2589. LOCK(&cs);
  2590. renderings++;
  2591. renderedLines = 0;
  2592. errorDescription->removeStyle(TextFeld::Style::Sichtbar);
  2593. ZeichnungHintergrund::render(rObj);
  2594. if (textRenderer->zSchrift() && content
  2595. && rObj.setDrawOptions(
  2596. pos + Punkt(getRahmenBreite(), getRahmenBreite()),
  2597. Punkt(getInnenBreite(), getInnenHeight())))
  2598. {
  2599. if (!renderStart.line)
  2600. {
  2601. renderStart = {content, 0};
  2602. renderStartLine = 1;
  2603. }
  2604. int y = 0;
  2605. int lineNumberWidth = 0;
  2606. for (int i = lineCount; i > 0; i /= 10)
  2607. {
  2608. lineNumberWidth++;
  2609. }
  2610. lineNumberWidth
  2611. = lineNumberWidth * textRenderer->getMaxCharWidth()
  2612. + textRenderer->getZeichenAbstand() * (lineNumberWidth - 1);
  2613. bool selected = false;
  2614. EditableJsonElement* element = content;
  2615. while (element)
  2616. {
  2617. if (element == selectionStart.line)
  2618. {
  2619. if (element != renderStart.line)
  2620. {
  2621. selected = true;
  2622. }
  2623. else
  2624. {
  2625. if (selectionStart.column < renderStart.column)
  2626. {
  2627. selected = true;
  2628. }
  2629. }
  2630. }
  2631. if (element == selectionEnd.line)
  2632. {
  2633. if (element != renderStart.line)
  2634. {
  2635. selected = false;
  2636. break;
  2637. }
  2638. else
  2639. {
  2640. if (selectionEnd.column < renderStart.column)
  2641. {
  2642. selected = false;
  2643. }
  2644. }
  2645. }
  2646. if (element == renderStart.line)
  2647. {
  2648. break;
  2649. }
  2650. element = element->zAfter(false, 0);
  2651. }
  2652. int line = renderStartLine;
  2653. int x = lineNumberWidth + 26;
  2654. EditableJsonElement* current = renderStart.line;
  2655. EditableJsonElement* highlightStop = 0;
  2656. int index = renderStart.column;
  2657. bool treeBranchRendered = false;
  2658. bool highlighted = false;
  2659. int numberWidth = textRenderer->getTextBreite(Text(line));
  2660. textRenderer->renderText(
  2661. 5 + lineNumberWidth - numberWidth, y, Text(line), rObj, 0xFF707070);
  2662. bool whiteSpaceOnly = true;
  2663. while (y < gr.y && current)
  2664. {
  2665. const Text& content = current->getContent();
  2666. int column = index;
  2667. for (int i = index; i < content.getLength() && y < gr.y;
  2668. i++, column++)
  2669. {
  2670. if (selectionStart.line == current
  2671. && selectionStart.column == i)
  2672. {
  2673. selected = true;
  2674. }
  2675. if (selectionEnd.line == current && selectionEnd.column == i)
  2676. {
  2677. selected = false;
  2678. }
  2679. int color = current->getColor(i);
  2680. int cursorX = x - 1;
  2681. if (textCursor.line == current && textCursor.column == i
  2682. && drawCursor)
  2683. {
  2684. rObj.drawLinieV(
  2685. cursorX, y, textRenderer->getSchriftSize(), 0xFFC0C0C0);
  2686. }
  2687. if (content[i] == '\n')
  2688. {
  2689. column = -1;
  2690. highlighted = false;
  2691. if (!treeBranchRendered)
  2692. {
  2693. // | in tree view
  2694. rObj.drawLinieV(lineNumberWidth + 13,
  2695. y
  2696. - (line > 1 ? textRenderer->getSchriftSize() / 6
  2697. : 0),
  2698. textRenderer->getSchriftSize()
  2699. + (line > 1 ? textRenderer->getSchriftSize() / 6
  2700. : 0),
  2701. 0xFF808080);
  2702. if (highlightStop)
  2703. {
  2704. // hover effect in tree view
  2705. rObj.fillRegion(lineNumberWidth + 10,
  2706. y - textRenderer->getSchriftSize() / 6,
  2707. 7,
  2708. textRenderer->getSchriftSize()
  2709. + textRenderer->getSchriftSize() / 6,
  2710. 0xFF505050);
  2711. }
  2712. }
  2713. if (selected)
  2714. {
  2715. rObj.fillRegion(x,
  2716. y,
  2717. textRenderer->getTextBreite(" "),
  2718. textRenderer->getZeilenHeight()
  2719. + textRenderer->getZeilenAbstand(),
  2720. 0xFF502020);
  2721. }
  2722. x = lineNumberWidth + 26;
  2723. y += textRenderer->getZeilenHeight()
  2724. + textRenderer->getZeilenAbstand();
  2725. whiteSpaceOnly = true;
  2726. line++;
  2727. renderedLines++;
  2728. treeBranchRendered = false;
  2729. int numberWidth = textRenderer->getTextBreite(Text(line));
  2730. textRenderer->renderText(5 + lineNumberWidth - numberWidth,
  2731. y,
  2732. Text(line),
  2733. rObj,
  2734. 0xFF707070);
  2735. }
  2736. else
  2737. {
  2738. textRenderer->renderChar(x,
  2739. y,
  2740. content[i],
  2741. rObj,
  2742. color,
  2743. false,
  2744. selected,
  2745. 0xFF502020);
  2746. }
  2747. if (cursorPos.x > cursorX && cursorPos.x <= x
  2748. && cursorPos.y >= y
  2749. && cursorPos.y <= y + textRenderer->getZeilenHeight())
  2750. {
  2751. if (current->hasError(i))
  2752. {
  2753. const SyntaxError& error = current->getError(i);
  2754. errorDescription->setText(errors[error.getErrorType()]);
  2755. errorDescription->setSize(
  2756. errorDescription->getNeededWidth() + 6,
  2757. errorDescription->getNeededHeight() + 6);
  2758. errorDescription->setPosition(
  2759. cursorPos.x, cursorPos.y + 20);
  2760. if (errorDescription->getX()
  2761. + errorDescription->getBreite()
  2762. > getInnenBreite())
  2763. {
  2764. errorDescription->setX(
  2765. getInnenBreite() - errorDescription->getBreite()
  2766. - 5);
  2767. }
  2768. if (errorDescription->getY()
  2769. + errorDescription->getHeight()
  2770. > getInnenHeight())
  2771. {
  2772. errorDescription->setY(
  2773. getInnenHeight() - errorDescription->getHeight()
  2774. - 5);
  2775. }
  2776. errorDescription->addStyle(TextFeld::Style::Sichtbar);
  2777. }
  2778. }
  2779. if (column % 4 == 0 && content[i] == ' ' && whiteSpaceOnly)
  2780. {
  2781. rObj.drawLinieV(
  2782. cursorX + 1 + textRenderer->getTextBreite(" ") / 2,
  2783. y + 2,
  2784. textRenderer->getSchriftSize() - 4,
  2785. 0xFF353535);
  2786. }
  2787. whiteSpaceOnly &= content[i] == ' ' || content[i] == '\n';
  2788. }
  2789. if (selectionStart.line == current
  2790. && selectionStart.column == content.getLength())
  2791. {
  2792. selected = true;
  2793. }
  2794. if (selectionEnd.line == current
  2795. && selectionEnd.column == content.getLength())
  2796. {
  2797. selected = false;
  2798. }
  2799. if (textCursor.line == current
  2800. && textCursor.column == content.getLength() && drawCursor)
  2801. {
  2802. rObj.drawLinieV(
  2803. x - 1, y, textRenderer->getSchriftSize(), 0xFFC0C0C0);
  2804. }
  2805. if (current->zFirstChildren())
  2806. {
  2807. if (pressed && pressedPos.x >= lineNumberWidth + 10
  2808. && pressedPos.x <= lineNumberWidth + 16
  2809. && pressedPos.y
  2810. >= y + textRenderer->getSchriftSize() / 2 - 4
  2811. && pressedPos.y
  2812. <= y + textRenderer->getSchriftSize() / 2 + 2)
  2813. {
  2814. pressed = false;
  2815. current->setHidden(!current->isHidden());
  2816. }
  2817. if (cursorPos.x >= lineNumberWidth + 10
  2818. && cursorPos.x <= lineNumberWidth + 16
  2819. && cursorPos.y >= y + textRenderer->getSchriftSize() / 2 - 4
  2820. && cursorPos.y <= y + textRenderer->getSchriftSize() / 2 + 2
  2821. && !current->isHidden())
  2822. {
  2823. highlightStop = current->zLastChildren();
  2824. }
  2825. if (current->isHidden())
  2826. {
  2827. if (!treeBranchRendered)
  2828. {
  2829. rObj.fillRegion(lineNumberWidth + 10,
  2830. y + textRenderer->getSchriftSize() / 2 - 4,
  2831. 6,
  2832. 6,
  2833. 0xFF000000);
  2834. rObj.drawLinie(
  2835. Punkt(lineNumberWidth + 10,
  2836. y + textRenderer->getSchriftSize() / 2 - 4),
  2837. Punkt(lineNumberWidth + 16,
  2838. y + textRenderer->getSchriftSize() / 2 - 1),
  2839. 0xFF808080);
  2840. rObj.drawLinie(
  2841. Punkt(lineNumberWidth + 10,
  2842. y + textRenderer->getSchriftSize() / 2 + 2),
  2843. Punkt(lineNumberWidth + 16,
  2844. y + textRenderer->getSchriftSize() / 2 - 1),
  2845. 0xFF808080);
  2846. }
  2847. for (char c : " ... ")
  2848. {
  2849. textRenderer->renderChar(x,
  2850. y,
  2851. c,
  2852. rObj,
  2853. 0xFF505050,
  2854. false,
  2855. selected,
  2856. 0xFF502020);
  2857. }
  2858. if (current->getOpeningControllChar() == '{')
  2859. {
  2860. textRenderer->renderChar(x,
  2861. y,
  2862. '}',
  2863. rObj,
  2864. 0xFF505050,
  2865. false,
  2866. selected,
  2867. 0xFF502020);
  2868. }
  2869. else if (current->getOpeningControllChar() == '[')
  2870. {
  2871. textRenderer->renderChar(x,
  2872. y,
  2873. ']',
  2874. rObj,
  2875. 0xFF505050,
  2876. false,
  2877. selected,
  2878. 0xFF502020);
  2879. }
  2880. }
  2881. else
  2882. {
  2883. if (!treeBranchRendered)
  2884. {
  2885. rObj.fillRegion(lineNumberWidth + 10,
  2886. y + textRenderer->getSchriftSize() / 2 - 4,
  2887. 6,
  2888. 6,
  2889. 0xFF000000);
  2890. rObj.drawLinie(
  2891. Punkt(lineNumberWidth + 10,
  2892. y + textRenderer->getSchriftSize() / 2 - 4),
  2893. Punkt(lineNumberWidth + 13,
  2894. y + textRenderer->getSchriftSize() / 2 + 2),
  2895. 0xFF808080);
  2896. rObj.drawLinie(
  2897. Punkt(lineNumberWidth + 16,
  2898. y + textRenderer->getSchriftSize() / 2 - 4),
  2899. Punkt(lineNumberWidth + 13,
  2900. y + textRenderer->getSchriftSize() / 2 + 2),
  2901. 0xFF808080);
  2902. }
  2903. }
  2904. treeBranchRendered = true;
  2905. }
  2906. if (!current->zMextSibling() && current->zParent())
  2907. {
  2908. // |_ in tree view
  2909. rObj.drawLinieH(lineNumberWidth + 14,
  2910. y + textRenderer->getSchriftSize() - 1,
  2911. 3,
  2912. 0xFF808080);
  2913. }
  2914. rObj.drawLinieV(x,
  2915. y,
  2916. textRenderer->getSchriftSize(),
  2917. 0xFF30C030); // TODO: debug tree seperator
  2918. if (current == highlightStop)
  2919. {
  2920. highlightStop = 0;
  2921. if (!treeBranchRendered)
  2922. {
  2923. // hover effect in tree view
  2924. rObj.fillRegion(lineNumberWidth + 10,
  2925. y - textRenderer->getSchriftSize() / 6,
  2926. 7,
  2927. textRenderer->getSchriftSize()
  2928. + textRenderer->getSchriftSize() / 6,
  2929. 0xFF505050);
  2930. highlighted = true;
  2931. }
  2932. }
  2933. current = current->zAfter(true, &line);
  2934. index = 0;
  2935. }
  2936. if (!treeBranchRendered && !highlighted)
  2937. {
  2938. // | in tree view
  2939. rObj.drawLinieV(lineNumberWidth + 13,
  2940. y - (line > 1 ? textRenderer->getSchriftSize() / 6 : 0),
  2941. textRenderer->getSchriftSize()
  2942. + (line > 1 ? textRenderer->getSchriftSize() / 6 : 0),
  2943. 0xFF808080);
  2944. }
  2945. EditableJsonElement* resnderStopElement = current;
  2946. renderStopLine = line;
  2947. textRenderer->renderText(5,
  2948. y + textRenderer->getZeilenHeight()
  2949. + textRenderer->getZeilenAbstand(),
  2950. Text("FPS: ") + Text(tps),
  2951. rObj,
  2952. 0xFFFFFFFF);
  2953. bool visible = false;
  2954. double sy = 0.0;
  2955. double yOffset = (double)getInnenHeight() / lineCount;
  2956. current = content;
  2957. EditableJsonElement* hiddenStop = 0;
  2958. while (current)
  2959. {
  2960. if (hiddenStop == current)
  2961. {
  2962. hiddenStop = 0;
  2963. }
  2964. if (current == renderStart.line)
  2965. {
  2966. visible = true;
  2967. }
  2968. if (current == resnderStopElement)
  2969. {
  2970. visible = false;
  2971. }
  2972. int f = 0xFF303030;
  2973. if (visible)
  2974. {
  2975. f = 0xFFA0A0A0;
  2976. }
  2977. if (hiddenStop)
  2978. {
  2979. f = 0xFF505050;
  2980. }
  2981. if (current->hasError())
  2982. {
  2983. f = 0xFFFF0000;
  2984. }
  2985. double size = current->lineCount() * yOffset;
  2986. if ((int)(sy + size) > (int)sy || current->hasError())
  2987. {
  2988. rObj.fillRegion(getInnenBreite() - 15,
  2989. (int)sy,
  2990. 15,
  2991. (int)(sy + MAX(yOffset, 1)),
  2992. f);
  2993. }
  2994. if (current->isHidden())
  2995. {
  2996. if (!hiddenStop)
  2997. {
  2998. hiddenStop = current->zAfter(true, 0);
  2999. }
  3000. }
  3001. current = current->zAfter(false, 0);
  3002. sy += size;
  3003. }
  3004. errorDescription->render(rObj);
  3005. rObj.releaseDrawOptions();
  3006. }
  3007. }
  3008. Text Framework::JSON::JsonEditor::getContent()
  3009. {
  3010. return content ? content->getRecursiveContent() : Text("");
  3011. }
  3012. Text Framework::JSON::JsonEditor::getSelectedContent()
  3013. {
  3014. Text result = "";
  3015. if (selectionStart.line && selectionEnd.line)
  3016. {
  3017. if (selectionStart.line == selectionEnd.line)
  3018. {
  3019. result.append(selectionStart.line->getContent().getTeilText(
  3020. selectionStart.column, selectionEnd.column));
  3021. }
  3022. else
  3023. {
  3024. result.append(selectionStart.line->getContent().getTeilText(
  3025. selectionStart.column,
  3026. selectionStart.line->getContent().getLength()));
  3027. Stack<EditableJsonElement*> stack(0);
  3028. stack.push(selectionStart.line);
  3029. while (stack.getSize())
  3030. {
  3031. EditableJsonElement* current = stack.pop();
  3032. if (current->zMextSibling())
  3033. {
  3034. stack.push(current->zMextSibling());
  3035. }
  3036. else if (stack.getSize() == 0 && current->zParent())
  3037. {
  3038. EditableJsonElement* parent = current->zParent();
  3039. while (parent)
  3040. {
  3041. if (parent->zMextSibling())
  3042. {
  3043. stack.push(current->zParent()->zMextSibling());
  3044. break;
  3045. }
  3046. parent = parent->zParent();
  3047. }
  3048. }
  3049. if (current->zFirstChildren())
  3050. {
  3051. stack.push(current->zFirstChildren());
  3052. }
  3053. if (current == selectionEnd.line)
  3054. {
  3055. break;
  3056. }
  3057. if (current != selectionStart.line)
  3058. {
  3059. result += current->getContent();
  3060. }
  3061. }
  3062. result.append(selectionEnd.line->getContent().getTeilText(
  3063. 0, selectionEnd.column));
  3064. }
  3065. }
  3066. return result;
  3067. }
  3068. JSONValue* Framework::JSON::JsonEditor::getValidContent()
  3069. {
  3070. return nullptr; // TODO
  3071. }
  3072. void Framework::JSON::JsonEditor::scrollToLine(int line, ScrollTargetPos pos)
  3073. {
  3074. if (pos == Top || pos == Bottom)
  3075. {
  3076. if (line >= renderStartLine && line < renderStopLine)
  3077. {
  3078. return; // line is already visible
  3079. }
  3080. }
  3081. EditableJsonElement* element = content;
  3082. if (line == 1)
  3083. {
  3084. scrollToLine(line, {content, 0}, pos);
  3085. return;
  3086. }
  3087. int l = 1;
  3088. int column = 0;
  3089. while (element)
  3090. {
  3091. int lineCount = element->lineCount();
  3092. if (lineCount + l >= line)
  3093. {
  3094. column = element->getContent().positionVon('\n', line - l - 1) + 1;
  3095. break;
  3096. }
  3097. l += lineCount;
  3098. EditableJsonElement* tmp = element->zAfter(true, &l);
  3099. if (l >= line)
  3100. {
  3101. column = element->getContent().positionVon('\n', lineCount - 1) + 1;
  3102. break;
  3103. }
  3104. element = tmp;
  3105. }
  3106. EditorPosition target = {element, column};
  3107. unifyPosition(target);
  3108. scrollToLine(line, target, pos);
  3109. }
  3110. void Framework::JSON::JsonEditor::scrollToLine(
  3111. EditorPosition target, ScrollTargetPos pos)
  3112. {
  3113. if (!target.line || !target.line->isVisible())
  3114. {
  3115. return;
  3116. }
  3117. int lineNumber = 0;
  3118. if (!content->lineCount(true, true, target.line, lineNumber))
  3119. {
  3120. Text* part = target.line->getContent().getTeilText(0, target.column);
  3121. lineNumber += part->anzahlVon('\n');
  3122. part->release();
  3123. if (pos == Top || pos == Bottom)
  3124. {
  3125. if (lineNumber >= renderStartLine && lineNumber < renderStopLine)
  3126. {
  3127. return; // line is already visible
  3128. }
  3129. }
  3130. scrollToLine(lineNumber, target, pos);
  3131. }
  3132. }
  3133. void Framework::JSON::JsonEditor::scrollToLine(
  3134. int lineNum, EditorPosition target, ScrollTargetPos pos)
  3135. {
  3136. if (!target.line || !target.line->isVisible())
  3137. {
  3138. return;
  3139. }
  3140. if (pos == Top || pos == Bottom)
  3141. {
  3142. if (lineNum >= renderStartLine && lineNum < renderStopLine)
  3143. {
  3144. return; // line is already visible
  3145. }
  3146. }
  3147. if (pos == Top)
  3148. {
  3149. renderStart = target;
  3150. const char* content = renderStart.line->getContent();
  3151. while (
  3152. renderStart.column > 0 && content[renderStart.column - 1] != '\n')
  3153. {
  3154. renderStart.column--;
  3155. }
  3156. renderStartLine = lineNum;
  3157. }
  3158. else
  3159. {
  3160. int linesUntilStart
  3161. = pos == Bottom ? renderedLines - 1 : renderedLines / 2 - 1;
  3162. if (linesUntilStart >= lineNum)
  3163. {
  3164. renderStart = {content, 0};
  3165. renderStartLine = 1;
  3166. return;
  3167. }
  3168. EditableJsonElement* element = target.line;
  3169. EditableJsonElement* before = element;
  3170. while (linesUntilStart >= 0)
  3171. {
  3172. if (!before)
  3173. {
  3174. break;
  3175. }
  3176. element = before;
  3177. int newLines = 0;
  3178. if (element == target.line)
  3179. {
  3180. Text* part
  3181. = element->getContent().getTeilText(0, target.column);
  3182. newLines = part->anzahlVon('\n');
  3183. part->release();
  3184. }
  3185. else
  3186. {
  3187. newLines = element->lineCount();
  3188. }
  3189. if (newLines > linesUntilStart)
  3190. {
  3191. renderStart = {element,
  3192. element->getContent().positionVon(
  3193. '\n', newLines - linesUntilStart - 1)
  3194. + 1};
  3195. unifyPosition(renderStart);
  3196. renderStartLine = lineNum - linesUntilStart;
  3197. return;
  3198. }
  3199. lineNum -= newLines;
  3200. linesUntilStart -= newLines;
  3201. before = element->zBefore(this->content, true, &lineNum);
  3202. }
  3203. renderStart = {element, 0};
  3204. renderStartLine = lineNum;
  3205. }
  3206. }