text_edit.cpp 191 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141
  1. /*************************************************************************/
  2. /* text_edit.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /*************************************************************************/
  30. #include "text_edit.h"
  31. #include "core/config/project_settings.h"
  32. #include "core/input/input.h"
  33. #include "core/input/input_map.h"
  34. #include "core/object/message_queue.h"
  35. #include "core/object/script_language.h"
  36. #include "core/os/keyboard.h"
  37. #include "core/os/os.h"
  38. #include "core/string/string_builder.h"
  39. #include "core/string/translation.h"
  40. #include "scene/main/window.h"
  41. static bool _is_text_char(char32_t c) {
  42. return !is_symbol(c);
  43. }
  44. static bool _is_whitespace(char32_t c) {
  45. return c == '\t' || c == ' ';
  46. }
  47. static bool _is_char(char32_t c) {
  48. return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || c == '_';
  49. }
  50. ///////////////////////////////////////////////////////////////////////////////
  51. /// TEXT ///
  52. ///////////////////////////////////////////////////////////////////////////////
  53. void TextEdit::Text::set_font(const Ref<Font> &p_font) {
  54. if (font == p_font) {
  55. return;
  56. }
  57. font = p_font;
  58. is_dirty = true;
  59. }
  60. void TextEdit::Text::set_font_size(int p_font_size) {
  61. if (font_size == p_font_size) {
  62. return;
  63. }
  64. font_size = p_font_size;
  65. is_dirty = true;
  66. }
  67. void TextEdit::Text::set_tab_size(int p_tab_size) {
  68. if (tab_size == p_tab_size) {
  69. return;
  70. }
  71. tab_size = p_tab_size;
  72. tab_size_dirty = true;
  73. }
  74. int TextEdit::Text::get_tab_size() const {
  75. return tab_size;
  76. }
  77. void TextEdit::Text::set_font_features(const Dictionary &p_features) {
  78. if (opentype_features.hash() == p_features.hash()) {
  79. return;
  80. }
  81. opentype_features = p_features;
  82. is_dirty = true;
  83. }
  84. void TextEdit::Text::set_direction_and_language(TextServer::Direction p_direction, const String &p_language) {
  85. if (direction == p_direction && language == p_language) {
  86. return;
  87. }
  88. direction = p_direction;
  89. language = p_language;
  90. is_dirty = true;
  91. }
  92. void TextEdit::Text::set_draw_control_chars(bool p_draw_control_chars) {
  93. if (draw_control_chars == p_draw_control_chars) {
  94. return;
  95. }
  96. draw_control_chars = p_draw_control_chars;
  97. is_dirty = true;
  98. }
  99. int TextEdit::Text::get_line_width(int p_line, int p_wrap_index) const {
  100. ERR_FAIL_INDEX_V(p_line, text.size(), 0);
  101. if (p_wrap_index != -1) {
  102. return text[p_line].data_buf->get_line_width(p_wrap_index);
  103. }
  104. return text[p_line].data_buf->get_size().x;
  105. }
  106. int TextEdit::Text::get_line_height() const {
  107. return line_height;
  108. }
  109. void TextEdit::Text::set_width(float p_width) {
  110. width = p_width;
  111. }
  112. int TextEdit::Text::get_line_wrap_amount(int p_line) const {
  113. ERR_FAIL_INDEX_V(p_line, text.size(), 0);
  114. return text[p_line].data_buf->get_line_count() - 1;
  115. }
  116. Vector<Vector2i> TextEdit::Text::get_line_wrap_ranges(int p_line) const {
  117. Vector<Vector2i> ret;
  118. ERR_FAIL_INDEX_V(p_line, text.size(), ret);
  119. for (int i = 0; i < text[p_line].data_buf->get_line_count(); i++) {
  120. ret.push_back(text[p_line].data_buf->get_line_range(i));
  121. }
  122. return ret;
  123. }
  124. const Ref<TextParagraph> TextEdit::Text::get_line_data(int p_line) const {
  125. ERR_FAIL_INDEX_V(p_line, text.size(), Ref<TextParagraph>());
  126. return text[p_line].data_buf;
  127. }
  128. _FORCE_INLINE_ const String &TextEdit::Text::operator[](int p_line) const {
  129. return text[p_line].data;
  130. }
  131. void TextEdit::Text::_calculate_line_height() {
  132. int height = 0;
  133. for (int i = 0; i < text.size(); i++) {
  134. // Found another line with the same height...nothing to update.
  135. if (text[i].height == line_height) {
  136. height = line_height;
  137. break;
  138. }
  139. height = MAX(height, text[i].height);
  140. }
  141. line_height = height;
  142. }
  143. void TextEdit::Text::_calculate_max_line_width() {
  144. int width = 0;
  145. for (int i = 0; i < text.size(); i++) {
  146. if (is_hidden(i)) {
  147. continue;
  148. }
  149. // Found another line with the same width...nothing to update.
  150. if (text[i].width == max_width) {
  151. width = max_width;
  152. break;
  153. }
  154. width = MAX(width, text[i].width);
  155. }
  156. max_width = width;
  157. }
  158. void TextEdit::Text::invalidate_cache(int p_line, int p_column, const String &p_ime_text, const Array &p_bidi_override) {
  159. ERR_FAIL_INDEX(p_line, text.size());
  160. if (font.is_null() || font_size <= 0) {
  161. return; // Not in tree?
  162. }
  163. text.write[p_line].data_buf->clear();
  164. text.write[p_line].data_buf->set_width(width);
  165. text.write[p_line].data_buf->set_direction((TextServer::Direction)direction);
  166. text.write[p_line].data_buf->set_preserve_control(draw_control_chars);
  167. if (p_ime_text.length() > 0) {
  168. text.write[p_line].data_buf->add_string(p_ime_text, font, font_size, opentype_features, language);
  169. if (!p_bidi_override.is_empty()) {
  170. TS->shaped_text_set_bidi_override(text.write[p_line].data_buf->get_rid(), p_bidi_override);
  171. }
  172. } else {
  173. text.write[p_line].data_buf->add_string(text[p_line].data, font, font_size, opentype_features, language);
  174. if (!text[p_line].bidi_override.is_empty()) {
  175. TS->shaped_text_set_bidi_override(text.write[p_line].data_buf->get_rid(), text[p_line].bidi_override);
  176. }
  177. }
  178. // Apply tab align.
  179. if (tab_size > 0) {
  180. Vector<float> tabs;
  181. tabs.push_back(font->get_char_size(' ', 0, font_size).width * tab_size);
  182. text.write[p_line].data_buf->tab_align(tabs);
  183. }
  184. // Update height.
  185. const int old_height = text.write[p_line].height;
  186. const int wrap_amount = get_line_wrap_amount(p_line);
  187. int height = font->get_height(font_size);
  188. for (int i = 0; i <= wrap_amount; i++) {
  189. height = MAX(height, text[p_line].data_buf->get_line_size(i).y);
  190. }
  191. text.write[p_line].height = height;
  192. // If this line has shrunk, this may no longer the the tallest line.
  193. if (old_height == line_height && height < line_height) {
  194. _calculate_line_height();
  195. } else {
  196. line_height = MAX(height, line_height);
  197. }
  198. // Update width.
  199. const int old_width = text.write[p_line].width;
  200. int width = get_line_width(p_line);
  201. text.write[p_line].width = width;
  202. // If this line has shrunk, this may no longer the the longest line.
  203. if (old_width == max_width && width < max_width) {
  204. _calculate_max_line_width();
  205. } else if (!is_hidden(p_line)) {
  206. max_width = MAX(width, max_width);
  207. }
  208. }
  209. void TextEdit::Text::invalidate_all_lines() {
  210. for (int i = 0; i < text.size(); i++) {
  211. text.write[i].data_buf->set_width(width);
  212. if (tab_size_dirty) {
  213. if (tab_size > 0) {
  214. Vector<float> tabs;
  215. tabs.push_back(font->get_char_size(' ', 0, font_size).width * tab_size);
  216. text.write[i].data_buf->tab_align(tabs);
  217. }
  218. // Tabs have changes, force width update.
  219. text.write[i].width = get_line_width(i);
  220. }
  221. }
  222. if (tab_size_dirty) {
  223. _calculate_max_line_width();
  224. tab_size_dirty = false;
  225. }
  226. }
  227. void TextEdit::Text::invalidate_all() {
  228. if (!is_dirty) {
  229. return;
  230. }
  231. for (int i = 0; i < text.size(); i++) {
  232. invalidate_cache(i);
  233. }
  234. is_dirty = false;
  235. }
  236. void TextEdit::Text::clear() {
  237. text.clear();
  238. insert(0, "", Array());
  239. }
  240. int TextEdit::Text::get_max_width() const {
  241. return max_width;
  242. }
  243. void TextEdit::Text::set(int p_line, const String &p_text, const Array &p_bidi_override) {
  244. ERR_FAIL_INDEX(p_line, text.size());
  245. text.write[p_line].data = p_text;
  246. text.write[p_line].bidi_override = p_bidi_override;
  247. invalidate_cache(p_line);
  248. }
  249. void TextEdit::Text::insert(int p_at, const String &p_text, const Array &p_bidi_override) {
  250. Line line;
  251. line.gutters.resize(gutter_count);
  252. line.hidden = false;
  253. line.data = p_text;
  254. line.bidi_override = p_bidi_override;
  255. text.insert(p_at, line);
  256. invalidate_cache(p_at);
  257. }
  258. void TextEdit::Text::remove(int p_at) {
  259. int height = text[p_at].height;
  260. int width = text[p_at].width;
  261. text.remove(p_at);
  262. // If this is the tallest line, we need to get the next tallest.
  263. if (height == line_height) {
  264. _calculate_line_height();
  265. }
  266. // If this is the longest line, we need to get the next longest.
  267. if (width == max_width) {
  268. _calculate_max_line_width();
  269. }
  270. }
  271. void TextEdit::Text::add_gutter(int p_at) {
  272. for (int i = 0; i < text.size(); i++) {
  273. if (p_at < 0 || p_at > gutter_count) {
  274. text.write[i].gutters.push_back(Gutter());
  275. } else {
  276. text.write[i].gutters.insert(p_at, Gutter());
  277. }
  278. }
  279. gutter_count++;
  280. }
  281. void TextEdit::Text::remove_gutter(int p_gutter) {
  282. for (int i = 0; i < text.size(); i++) {
  283. text.write[i].gutters.remove(p_gutter);
  284. }
  285. gutter_count--;
  286. }
  287. void TextEdit::Text::move_gutters(int p_from_line, int p_to_line) {
  288. text.write[p_to_line].gutters = text[p_from_line].gutters;
  289. text.write[p_from_line].gutters.clear();
  290. text.write[p_from_line].gutters.resize(gutter_count);
  291. }
  292. ///////////////////////////////////////////////////////////////////////////////
  293. /// TEXT EDIT ///
  294. ///////////////////////////////////////////////////////////////////////////////
  295. void TextEdit::_notification(int p_what) {
  296. switch (p_what) {
  297. case NOTIFICATION_ENTER_TREE: {
  298. _update_caches();
  299. if (caret_pos_dirty) {
  300. MessageQueue::get_singleton()->push_call(this, "_emit_caret_changed");
  301. }
  302. if (text_changed_dirty) {
  303. MessageQueue::get_singleton()->push_call(this, "_text_changed_emit");
  304. }
  305. _update_wrap_at_column(true);
  306. } break;
  307. case NOTIFICATION_RESIZED: {
  308. _update_scrollbars();
  309. _update_wrap_at_column();
  310. } break;
  311. case NOTIFICATION_VISIBILITY_CHANGED: {
  312. if (is_visible()) {
  313. call_deferred(SNAME("_update_scrollbars"));
  314. call_deferred(SNAME("_update_wrap_at_column"));
  315. }
  316. } break;
  317. case NOTIFICATION_LAYOUT_DIRECTION_CHANGED:
  318. case NOTIFICATION_TRANSLATION_CHANGED:
  319. case NOTIFICATION_THEME_CHANGED: {
  320. _update_caches();
  321. _update_wrap_at_column(true);
  322. } break;
  323. case NOTIFICATION_WM_WINDOW_FOCUS_IN: {
  324. window_has_focus = true;
  325. draw_caret = true;
  326. update();
  327. } break;
  328. case NOTIFICATION_WM_WINDOW_FOCUS_OUT: {
  329. window_has_focus = false;
  330. draw_caret = false;
  331. update();
  332. } break;
  333. case NOTIFICATION_INTERNAL_PHYSICS_PROCESS: {
  334. if (scrolling && get_v_scroll() != target_v_scroll) {
  335. double target_y = target_v_scroll - get_v_scroll();
  336. double dist = sqrt(target_y * target_y);
  337. // To ensure minimap is responsive override the speed setting.
  338. double vel = ((target_y / dist) * ((minimap_clicked) ? 3000 : v_scroll_speed)) * get_physics_process_delta_time();
  339. if (Math::abs(vel) >= dist) {
  340. set_v_scroll(target_v_scroll);
  341. scrolling = false;
  342. minimap_clicked = false;
  343. set_physics_process_internal(false);
  344. } else {
  345. set_v_scroll(get_v_scroll() + vel);
  346. }
  347. } else {
  348. scrolling = false;
  349. minimap_clicked = false;
  350. set_physics_process_internal(false);
  351. }
  352. } break;
  353. case NOTIFICATION_DRAW: {
  354. if (first_draw) {
  355. // Size may not be the final one, so attempts to ensure caret was visible may have failed.
  356. adjust_viewport_to_caret();
  357. first_draw = false;
  358. }
  359. /* Prevent the resource getting lost between the editor and game. */
  360. if (Engine::get_singleton()->is_editor_hint()) {
  361. if (syntax_highlighter.is_valid() && syntax_highlighter->get_text_edit() != this) {
  362. syntax_highlighter->set_text_edit(this);
  363. }
  364. }
  365. Size2 size = get_size();
  366. bool rtl = is_layout_rtl();
  367. if ((!has_focus() && !(menu && menu->has_focus())) || !window_has_focus) {
  368. draw_caret = false;
  369. }
  370. _update_scrollbars();
  371. RID ci = get_canvas_item();
  372. RenderingServer::get_singleton()->canvas_item_set_clip(get_canvas_item(), true);
  373. int xmargin_beg = style_normal->get_margin(SIDE_LEFT) + gutters_width + gutter_padding;
  374. int xmargin_end = size.width - style_normal->get_margin(SIDE_RIGHT);
  375. if (draw_minimap) {
  376. xmargin_end -= minimap_width;
  377. }
  378. // Let's do it easy for now.
  379. style_normal->draw(ci, Rect2(Point2(), size));
  380. if (!editable) {
  381. style_readonly->draw(ci, Rect2(Point2(), size));
  382. draw_caret = false;
  383. }
  384. if (has_focus()) {
  385. style_focus->draw(ci, Rect2(Point2(), size));
  386. }
  387. int visible_rows = get_visible_line_count() + 1;
  388. Color color = !editable ? font_readonly_color : font_color;
  389. if (background_color.a > 0.01) {
  390. RenderingServer::get_singleton()->canvas_item_add_rect(ci, Rect2(Point2i(), get_size()), background_color);
  391. }
  392. int brace_open_match_line = -1;
  393. int brace_open_match_column = -1;
  394. bool brace_open_matching = false;
  395. bool brace_open_mismatch = false;
  396. int brace_close_match_line = -1;
  397. int brace_close_match_column = -1;
  398. bool brace_close_matching = false;
  399. bool brace_close_mismatch = false;
  400. if (highlight_matching_braces_enabled && caret.line >= 0 && caret.line < text.size() && caret.column >= 0) {
  401. if (caret.column < text[caret.line].length()) {
  402. // Check for open.
  403. char32_t c = text[caret.line][caret.column];
  404. char32_t closec = 0;
  405. if (c == '[') {
  406. closec = ']';
  407. } else if (c == '{') {
  408. closec = '}';
  409. } else if (c == '(') {
  410. closec = ')';
  411. }
  412. if (closec != 0) {
  413. int stack = 1;
  414. for (int i = caret.line; i < text.size(); i++) {
  415. int from = i == caret.line ? caret.column + 1 : 0;
  416. for (int j = from; j < text[i].length(); j++) {
  417. char32_t cc = text[i][j];
  418. // Ignore any brackets inside a string.
  419. if (cc == '"' || cc == '\'') {
  420. char32_t quotation = cc;
  421. do {
  422. j++;
  423. if (!(j < text[i].length())) {
  424. break;
  425. }
  426. cc = text[i][j];
  427. // Skip over escaped quotation marks inside strings.
  428. if (cc == '\\') {
  429. bool escaped = true;
  430. while (j + 1 < text[i].length() && text[i][j + 1] == '\\') {
  431. escaped = !escaped;
  432. j++;
  433. }
  434. if (escaped) {
  435. j++;
  436. continue;
  437. }
  438. }
  439. } while (cc != quotation);
  440. } else if (cc == c) {
  441. stack++;
  442. } else if (cc == closec) {
  443. stack--;
  444. }
  445. if (stack == 0) {
  446. brace_open_match_line = i;
  447. brace_open_match_column = j;
  448. brace_open_matching = true;
  449. break;
  450. }
  451. }
  452. if (brace_open_match_line != -1) {
  453. break;
  454. }
  455. }
  456. if (!brace_open_matching) {
  457. brace_open_mismatch = true;
  458. }
  459. }
  460. }
  461. if (caret.column > 0) {
  462. char32_t c = text[caret.line][caret.column - 1];
  463. char32_t closec = 0;
  464. if (c == ']') {
  465. closec = '[';
  466. } else if (c == '}') {
  467. closec = '{';
  468. } else if (c == ')') {
  469. closec = '(';
  470. }
  471. if (closec != 0) {
  472. int stack = 1;
  473. for (int i = caret.line; i >= 0; i--) {
  474. int from = i == caret.line ? caret.column - 2 : text[i].length() - 1;
  475. for (int j = from; j >= 0; j--) {
  476. char32_t cc = text[i][j];
  477. // Ignore any brackets inside a string.
  478. if (cc == '"' || cc == '\'') {
  479. char32_t quotation = cc;
  480. do {
  481. j--;
  482. if (!(j >= 0)) {
  483. break;
  484. }
  485. cc = text[i][j];
  486. // Skip over escaped quotation marks inside strings.
  487. if (cc == quotation) {
  488. bool escaped = false;
  489. while (j - 1 >= 0 && text[i][j - 1] == '\\') {
  490. escaped = !escaped;
  491. j--;
  492. }
  493. if (escaped) {
  494. cc = '\\';
  495. continue;
  496. }
  497. }
  498. } while (cc != quotation);
  499. } else if (cc == c) {
  500. stack++;
  501. } else if (cc == closec) {
  502. stack--;
  503. }
  504. if (stack == 0) {
  505. brace_close_match_line = i;
  506. brace_close_match_column = j;
  507. brace_close_matching = true;
  508. break;
  509. }
  510. }
  511. if (brace_close_match_line != -1) {
  512. break;
  513. }
  514. }
  515. if (!brace_close_matching) {
  516. brace_close_mismatch = true;
  517. }
  518. }
  519. }
  520. }
  521. // Get the highlighted words.
  522. String highlighted_text = get_selected_text();
  523. // Check if highlighted words contain only whitespaces (tabs or spaces).
  524. bool only_whitespaces_highlighted = highlighted_text.strip_edges() == String();
  525. const int caret_wrap_index = get_caret_wrap_index();
  526. int first_visible_line = get_first_visible_line() - 1;
  527. int draw_amount = visible_rows + (smooth_scroll_enabled ? 1 : 0);
  528. draw_amount += get_line_wrap_count(first_visible_line + 1);
  529. // minimap
  530. if (draw_minimap) {
  531. int minimap_visible_lines = get_minimap_visible_lines();
  532. int minimap_line_height = (minimap_char_size.y + minimap_line_spacing);
  533. int minimap_tab_size = minimap_char_size.x * text.get_tab_size();
  534. // calculate viewport size and y offset
  535. int viewport_height = (draw_amount - 1) * minimap_line_height;
  536. int control_height = _get_control_height() - viewport_height;
  537. int viewport_offset_y = round(get_scroll_pos_for_line(first_visible_line + 1) * control_height) / ((v_scroll->get_max() <= minimap_visible_lines) ? (minimap_visible_lines - draw_amount) : (v_scroll->get_max() - draw_amount));
  538. // calculate the first line.
  539. int num_lines_before = round((viewport_offset_y) / minimap_line_height);
  540. int minimap_line = (v_scroll->get_max() <= minimap_visible_lines) ? -1 : first_visible_line;
  541. if (minimap_line >= 0) {
  542. minimap_line -= get_next_visible_line_index_offset_from(first_visible_line, 0, -num_lines_before).x;
  543. minimap_line -= (minimap_line > 0 && smooth_scroll_enabled ? 1 : 0);
  544. }
  545. int minimap_draw_amount = minimap_visible_lines + get_line_wrap_count(minimap_line + 1);
  546. // Draw the minimap.
  547. // Add visual feedback when dragging or hovering the the visible area rectangle.
  548. float viewport_alpha;
  549. if (dragging_minimap) {
  550. viewport_alpha = 0.25;
  551. } else if (hovering_minimap) {
  552. viewport_alpha = 0.175;
  553. } else {
  554. viewport_alpha = 0.1;
  555. }
  556. const Color viewport_color = (background_color.get_v() < 0.5) ? Color(1, 1, 1, viewport_alpha) : Color(0, 0, 0, viewport_alpha);
  557. if (rtl) {
  558. RenderingServer::get_singleton()->canvas_item_add_rect(ci, Rect2(size.width - (xmargin_end + 2) - minimap_width, viewport_offset_y, minimap_width, viewport_height), viewport_color);
  559. } else {
  560. RenderingServer::get_singleton()->canvas_item_add_rect(ci, Rect2((xmargin_end + 2), viewport_offset_y, minimap_width, viewport_height), viewport_color);
  561. }
  562. for (int i = 0; i < minimap_draw_amount; i++) {
  563. minimap_line++;
  564. if (minimap_line < 0 || minimap_line >= (int)text.size()) {
  565. break;
  566. }
  567. while (_is_line_hidden(minimap_line)) {
  568. minimap_line++;
  569. if (minimap_line < 0 || minimap_line >= (int)text.size()) {
  570. break;
  571. }
  572. }
  573. if (minimap_line < 0 || minimap_line >= (int)text.size()) {
  574. break;
  575. }
  576. Dictionary color_map = _get_line_syntax_highlighting(minimap_line);
  577. Color line_background_color = text.get_line_background_color(minimap_line);
  578. line_background_color.a *= 0.6;
  579. Color current_color = font_color;
  580. if (!editable) {
  581. current_color = font_readonly_color;
  582. }
  583. Vector<String> wrap_rows = get_line_wrapped_text(minimap_line);
  584. int line_wrap_amount = get_line_wrap_count(minimap_line);
  585. int last_wrap_column = 0;
  586. for (int line_wrap_index = 0; line_wrap_index < line_wrap_amount + 1; line_wrap_index++) {
  587. if (line_wrap_index != 0) {
  588. i++;
  589. if (i >= minimap_draw_amount) {
  590. break;
  591. }
  592. }
  593. const String &str = wrap_rows[line_wrap_index];
  594. int indent_px = line_wrap_index != 0 ? get_indent_level(minimap_line) : 0;
  595. if (indent_px >= wrap_at_column) {
  596. indent_px = 0;
  597. }
  598. indent_px = minimap_char_size.x * indent_px;
  599. if (line_wrap_index > 0) {
  600. last_wrap_column += wrap_rows[line_wrap_index - 1].length();
  601. }
  602. if (minimap_line == caret.line && caret_wrap_index == line_wrap_index && highlight_current_line) {
  603. if (rtl) {
  604. RenderingServer::get_singleton()->canvas_item_add_rect(ci, Rect2(size.width - (xmargin_end + 2) - minimap_width, i * 3, minimap_width, 2), current_line_color);
  605. } else {
  606. RenderingServer::get_singleton()->canvas_item_add_rect(ci, Rect2((xmargin_end + 2), i * 3, minimap_width, 2), current_line_color);
  607. }
  608. } else if (line_background_color != Color(0, 0, 0, 0)) {
  609. if (rtl) {
  610. RenderingServer::get_singleton()->canvas_item_add_rect(ci, Rect2(size.width - (xmargin_end + 2) - minimap_width, i * 3, minimap_width, 2), line_background_color);
  611. } else {
  612. RenderingServer::get_singleton()->canvas_item_add_rect(ci, Rect2((xmargin_end + 2), i * 3, minimap_width, 2), line_background_color);
  613. }
  614. }
  615. Color previous_color;
  616. int characters = 0;
  617. int tabs = 0;
  618. for (int j = 0; j < str.length(); j++) {
  619. const Variant *color_data = color_map.getptr(last_wrap_column + j);
  620. if (color_data != nullptr) {
  621. current_color = (color_data->operator Dictionary()).get("color", font_color);
  622. if (!editable) {
  623. current_color.a = font_readonly_color.a;
  624. }
  625. }
  626. color = current_color;
  627. if (j == 0) {
  628. previous_color = color;
  629. }
  630. int xpos = indent_px + ((xmargin_end + minimap_char_size.x) + (minimap_char_size.x * j)) + tabs;
  631. bool out_of_bounds = (xpos >= xmargin_end + minimap_width);
  632. bool is_whitespace = _is_whitespace(str[j]);
  633. if (!is_whitespace) {
  634. characters++;
  635. if (j < str.length() - 1 && color == previous_color && !out_of_bounds) {
  636. continue;
  637. }
  638. // If we've changed colour we are at the start of a new section, therefore we need to go back to the end
  639. // of the previous section to draw it, we'll also add the character back on.
  640. if (color != previous_color) {
  641. characters--;
  642. j--;
  643. if (str[j] == '\t') {
  644. tabs -= minimap_tab_size;
  645. }
  646. }
  647. }
  648. if (characters > 0) {
  649. previous_color.a *= 0.6;
  650. // take one for zero indexing, and if we hit whitespace / the end of a word.
  651. int chars = MAX(0, (j - (characters - 1)) - (is_whitespace ? 1 : 0)) + 1;
  652. int char_x_ofs = indent_px + ((xmargin_end + minimap_char_size.x) + (minimap_char_size.x * chars)) + tabs;
  653. if (rtl) {
  654. RenderingServer::get_singleton()->canvas_item_add_rect(ci, Rect2(Point2(size.width - char_x_ofs - minimap_char_size.x * characters, minimap_line_height * i), Point2(minimap_char_size.x * characters, minimap_char_size.y)), previous_color);
  655. } else {
  656. RenderingServer::get_singleton()->canvas_item_add_rect(ci, Rect2(Point2(char_x_ofs, minimap_line_height * i), Point2(minimap_char_size.x * characters, minimap_char_size.y)), previous_color);
  657. }
  658. }
  659. if (out_of_bounds) {
  660. break;
  661. }
  662. if (str[j] == '\t') {
  663. tabs += minimap_tab_size;
  664. }
  665. previous_color = color;
  666. characters = 0;
  667. }
  668. }
  669. }
  670. }
  671. int top_limit_y = 0;
  672. int bottom_limit_y = get_size().height;
  673. if (!editable) {
  674. top_limit_y += style_readonly->get_margin(SIDE_TOP);
  675. bottom_limit_y -= style_readonly->get_margin(SIDE_BOTTOM);
  676. } else {
  677. top_limit_y += style_normal->get_margin(SIDE_TOP);
  678. bottom_limit_y -= style_normal->get_margin(SIDE_BOTTOM);
  679. }
  680. // draw main text
  681. caret.visible = false;
  682. int row_height = get_line_height();
  683. int line = first_visible_line;
  684. for (int i = 0; i < draw_amount; i++) {
  685. line++;
  686. if (line < 0 || line >= (int)text.size()) {
  687. continue;
  688. }
  689. while (_is_line_hidden(line)) {
  690. line++;
  691. if (line < 0 || line >= (int)text.size()) {
  692. break;
  693. }
  694. }
  695. if (line < 0 || line >= (int)text.size()) {
  696. continue;
  697. }
  698. Dictionary color_map = _get_line_syntax_highlighting(line);
  699. // Ensure we at least use the font color.
  700. Color current_color = !editable ? font_readonly_color : font_color;
  701. const Ref<TextParagraph> ldata = text.get_line_data(line);
  702. Vector<String> wrap_rows = get_line_wrapped_text(line);
  703. int line_wrap_amount = get_line_wrap_count(line);
  704. for (int line_wrap_index = 0; line_wrap_index <= line_wrap_amount; line_wrap_index++) {
  705. if (line_wrap_index != 0) {
  706. i++;
  707. if (i >= draw_amount) {
  708. break;
  709. }
  710. }
  711. const String &str = wrap_rows[line_wrap_index];
  712. int char_margin = xmargin_beg - caret.x_ofs;
  713. int ofs_x = 0;
  714. int ofs_y = 0;
  715. if (!editable) {
  716. ofs_x = style_readonly->get_offset().x / 2;
  717. ofs_x -= style_normal->get_offset().x / 2;
  718. ofs_y = style_readonly->get_offset().y / 2;
  719. } else {
  720. ofs_y = style_normal->get_offset().y / 2;
  721. }
  722. ofs_y += i * row_height + line_spacing / 2;
  723. ofs_y -= caret.wrap_ofs * row_height;
  724. ofs_y -= _get_v_scroll_offset() * row_height;
  725. bool clipped = false;
  726. if (ofs_y + row_height < top_limit_y) {
  727. // Line is outside the top margin, clip current line.
  728. // Still need to go through the process to prepare color changes for next lines.
  729. clipped = true;
  730. }
  731. if (ofs_y > bottom_limit_y) {
  732. // Line is outside the bottom margin, clip any remaining text.
  733. i = draw_amount;
  734. break;
  735. }
  736. if (text.get_line_background_color(line) != Color(0, 0, 0, 0)) {
  737. if (rtl) {
  738. RenderingServer::get_singleton()->canvas_item_add_rect(ci, Rect2(size.width - ofs_x - xmargin_end, ofs_y, xmargin_end - xmargin_beg, row_height), text.get_line_background_color(line));
  739. } else {
  740. RenderingServer::get_singleton()->canvas_item_add_rect(ci, Rect2(xmargin_beg + ofs_x, ofs_y, xmargin_end - xmargin_beg, row_height), text.get_line_background_color(line));
  741. }
  742. }
  743. if (str.length() == 0) {
  744. // Draw line background if empty as we won't loop at all.
  745. if (line == caret.line && caret_wrap_index == line_wrap_index && highlight_current_line) {
  746. if (rtl) {
  747. RenderingServer::get_singleton()->canvas_item_add_rect(ci, Rect2(size.width - ofs_x - xmargin_end, ofs_y, xmargin_end, row_height), current_line_color);
  748. } else {
  749. RenderingServer::get_singleton()->canvas_item_add_rect(ci, Rect2(ofs_x, ofs_y, xmargin_end, row_height), current_line_color);
  750. }
  751. }
  752. // Give visual indication of empty selected line.
  753. if (selection.active && line >= selection.from_line && line <= selection.to_line && char_margin >= xmargin_beg) {
  754. int char_w = font->get_char_size(' ', 0, font_size).width;
  755. if (rtl) {
  756. RenderingServer::get_singleton()->canvas_item_add_rect(ci, Rect2(size.width - xmargin_beg - ofs_x - char_w, ofs_y, char_w, row_height), selection_color);
  757. } else {
  758. RenderingServer::get_singleton()->canvas_item_add_rect(ci, Rect2(xmargin_beg + ofs_x, ofs_y, char_w, row_height), selection_color);
  759. }
  760. }
  761. } else {
  762. // If it has text, then draw current line marker in the margin, as line number etc will draw over it, draw the rest of line marker later.
  763. if (line == caret.line && caret_wrap_index == line_wrap_index && highlight_current_line) {
  764. if (rtl) {
  765. RenderingServer::get_singleton()->canvas_item_add_rect(ci, Rect2(size.width - ofs_x - xmargin_end, ofs_y, xmargin_end, row_height), current_line_color);
  766. } else {
  767. RenderingServer::get_singleton()->canvas_item_add_rect(ci, Rect2(ofs_x, ofs_y, xmargin_end, row_height), current_line_color);
  768. }
  769. }
  770. }
  771. if (line_wrap_index == 0) {
  772. // Only do these if we are on the first wrapped part of a line.
  773. int gutter_offset = style_normal->get_margin(SIDE_LEFT);
  774. for (int g = 0; g < gutters.size(); g++) {
  775. const GutterInfo gutter = gutters[g];
  776. if (!gutter.draw || gutter.width <= 0) {
  777. continue;
  778. }
  779. switch (gutter.type) {
  780. case GUTTER_TYPE_STRING: {
  781. const String &text = get_line_gutter_text(line, g);
  782. if (text == "") {
  783. break;
  784. }
  785. Ref<TextLine> tl;
  786. tl.instantiate();
  787. tl->add_string(text, font, font_size);
  788. int yofs = ofs_y + (row_height - tl->get_size().y) / 2;
  789. if (outline_size > 0 && outline_color.a > 0) {
  790. tl->draw_outline(ci, Point2(gutter_offset + ofs_x, yofs), outline_size, outline_color);
  791. }
  792. tl->draw(ci, Point2(gutter_offset + ofs_x, yofs), get_line_gutter_item_color(line, g));
  793. } break;
  794. case GUTTER_TYPE_ICON: {
  795. const Ref<Texture2D> icon = get_line_gutter_icon(line, g);
  796. if (icon.is_null()) {
  797. break;
  798. }
  799. Rect2 gutter_rect = Rect2(Point2i(gutter_offset, ofs_y), Size2i(gutter.width, row_height));
  800. int horizontal_padding = gutter_rect.size.x / 6;
  801. int vertical_padding = gutter_rect.size.y / 6;
  802. gutter_rect.position += Point2(horizontal_padding, vertical_padding);
  803. gutter_rect.size -= Point2(horizontal_padding, vertical_padding) * 2;
  804. // Correct icon aspect ratio.
  805. float icon_ratio = icon->get_width() / icon->get_height();
  806. float gutter_ratio = gutter_rect.size.x / gutter_rect.size.y;
  807. if (gutter_ratio > icon_ratio) {
  808. gutter_rect.size.x = floor(icon->get_width() * (gutter_rect.size.y / icon->get_height()));
  809. } else {
  810. gutter_rect.size.y = floor(icon->get_height() * (gutter_rect.size.x / icon->get_width()));
  811. }
  812. if (rtl) {
  813. gutter_rect.position.x = size.width - gutter_rect.position.x - gutter_rect.size.x;
  814. }
  815. icon->draw_rect(ci, gutter_rect, false, get_line_gutter_item_color(line, g));
  816. } break;
  817. case GUTTER_TYPE_CUSTOM: {
  818. if (gutter.custom_draw_obj.is_valid()) {
  819. Object *cdo = ObjectDB::get_instance(gutter.custom_draw_obj);
  820. if (cdo) {
  821. Rect2i gutter_rect = Rect2i(Point2i(gutter_offset, ofs_y), Size2i(gutter.width, row_height));
  822. if (rtl) {
  823. gutter_rect.position.x = size.width - gutter_rect.position.x - gutter_rect.size.x;
  824. }
  825. cdo->call(gutter.custom_draw_callback, line, g, Rect2(gutter_rect));
  826. }
  827. }
  828. } break;
  829. }
  830. gutter_offset += gutter.width;
  831. }
  832. }
  833. // Draw line.
  834. RID rid = ldata->get_line_rid(line_wrap_index);
  835. float text_height = TS->shaped_text_get_size(rid).y + font->get_spacing(TextServer::SPACING_TOP) + font->get_spacing(TextServer::SPACING_BOTTOM);
  836. if (rtl) {
  837. char_margin = size.width - char_margin - TS->shaped_text_get_size(rid).x;
  838. }
  839. if (!clipped && selection.active && line >= selection.from_line && line <= selection.to_line) { // Selection
  840. int sel_from = (line > selection.from_line) ? TS->shaped_text_get_range(rid).x : selection.from_column;
  841. int sel_to = (line < selection.to_line) ? TS->shaped_text_get_range(rid).y : selection.to_column;
  842. Vector<Vector2> sel = TS->shaped_text_get_selection(rid, sel_from, sel_to);
  843. for (int j = 0; j < sel.size(); j++) {
  844. Rect2 rect = Rect2(sel[j].x + char_margin + ofs_x, ofs_y, sel[j].y - sel[j].x, row_height);
  845. if (rect.position.x + rect.size.x <= xmargin_beg || rect.position.x > xmargin_end) {
  846. continue;
  847. }
  848. if (rect.position.x < xmargin_beg) {
  849. rect.size.x -= (xmargin_beg - rect.position.x);
  850. rect.position.x = xmargin_beg;
  851. }
  852. if (rect.position.x + rect.size.x > xmargin_end) {
  853. rect.size.x = xmargin_end - rect.position.x;
  854. }
  855. draw_rect(rect, selection_color, true);
  856. }
  857. }
  858. int start = TS->shaped_text_get_range(rid).x;
  859. if (!clipped && !search_text.is_empty()) { // Search highhlight
  860. int search_text_col = _get_column_pos_of_word(search_text, str, search_flags, 0);
  861. while (search_text_col != -1) {
  862. Vector<Vector2> sel = TS->shaped_text_get_selection(rid, search_text_col + start, search_text_col + search_text.length() + start);
  863. for (int j = 0; j < sel.size(); j++) {
  864. Rect2 rect = Rect2(sel[j].x + char_margin + ofs_x, ofs_y, sel[j].y - sel[j].x, row_height);
  865. if (rect.position.x + rect.size.x <= xmargin_beg || rect.position.x > xmargin_end) {
  866. continue;
  867. }
  868. if (rect.position.x < xmargin_beg) {
  869. rect.size.x -= (xmargin_beg - rect.position.x);
  870. rect.position.x = xmargin_beg;
  871. } else if (rect.position.x + rect.size.x > xmargin_end) {
  872. rect.size.x = xmargin_end - rect.position.x;
  873. }
  874. draw_rect(rect, search_result_color, true);
  875. draw_rect(rect, search_result_border_color, false);
  876. }
  877. search_text_col = _get_column_pos_of_word(search_text, str, search_flags, search_text_col + 1);
  878. }
  879. }
  880. if (!clipped && highlight_all_occurrences && !only_whitespaces_highlighted && !highlighted_text.is_empty()) { // Highlight
  881. int highlighted_text_col = _get_column_pos_of_word(highlighted_text, str, SEARCH_MATCH_CASE | SEARCH_WHOLE_WORDS, 0);
  882. while (highlighted_text_col != -1) {
  883. Vector<Vector2> sel = TS->shaped_text_get_selection(rid, highlighted_text_col + start, highlighted_text_col + highlighted_text.length() + start);
  884. for (int j = 0; j < sel.size(); j++) {
  885. Rect2 rect = Rect2(sel[j].x + char_margin + ofs_x, ofs_y, sel[j].y - sel[j].x, row_height);
  886. if (rect.position.x + rect.size.x <= xmargin_beg || rect.position.x > xmargin_end) {
  887. continue;
  888. }
  889. if (rect.position.x < xmargin_beg) {
  890. rect.size.x -= (xmargin_beg - rect.position.x);
  891. rect.position.x = xmargin_beg;
  892. } else if (rect.position.x + rect.size.x > xmargin_end) {
  893. rect.size.x = xmargin_end - rect.position.x;
  894. }
  895. draw_rect(rect, word_highlighted_color);
  896. }
  897. highlighted_text_col = _get_column_pos_of_word(highlighted_text, str, SEARCH_MATCH_CASE | SEARCH_WHOLE_WORDS, highlighted_text_col + 1);
  898. }
  899. }
  900. if (!clipped && lookup_symbol_word.length() != 0) { // Highlight word
  901. if (_is_char(lookup_symbol_word[0]) || lookup_symbol_word[0] == '.') {
  902. int highlighted_word_col = _get_column_pos_of_word(lookup_symbol_word, str, SEARCH_MATCH_CASE | SEARCH_WHOLE_WORDS, 0);
  903. while (highlighted_word_col != -1) {
  904. Vector<Vector2> sel = TS->shaped_text_get_selection(rid, highlighted_word_col + start, highlighted_word_col + lookup_symbol_word.length() + start);
  905. for (int j = 0; j < sel.size(); j++) {
  906. Rect2 rect = Rect2(sel[j].x + char_margin + ofs_x, ofs_y, sel[j].y - sel[j].x, row_height);
  907. if (rect.position.x + rect.size.x <= xmargin_beg || rect.position.x > xmargin_end) {
  908. continue;
  909. }
  910. if (rect.position.x < xmargin_beg) {
  911. rect.size.x -= (xmargin_beg - rect.position.x);
  912. rect.position.x = xmargin_beg;
  913. } else if (rect.position.x + rect.size.x > xmargin_end) {
  914. rect.size.x = xmargin_end - rect.position.x;
  915. }
  916. rect.position.y = TS->shaped_text_get_ascent(rid) + font->get_underline_position(font_size);
  917. rect.size.y = font->get_underline_thickness(font_size);
  918. draw_rect(rect, font_selected_color);
  919. }
  920. highlighted_word_col = _get_column_pos_of_word(lookup_symbol_word, str, SEARCH_MATCH_CASE | SEARCH_WHOLE_WORDS, highlighted_word_col + 1);
  921. }
  922. }
  923. }
  924. ofs_y += (row_height - text_height) / 2;
  925. const Glyph *glyphs = TS->shaped_text_get_glyphs(rid);
  926. int gl_size = TS->shaped_text_get_glyph_count(rid);
  927. ofs_y += ldata->get_line_ascent(line_wrap_index);
  928. int char_ofs = 0;
  929. if (outline_size > 0 && outline_color.a > 0) {
  930. for (int j = 0; j < gl_size; j++) {
  931. for (int k = 0; k < glyphs[j].repeat; k++) {
  932. if ((char_ofs + char_margin) >= xmargin_beg && (char_ofs + glyphs[j].advance + char_margin) <= xmargin_end) {
  933. if (glyphs[j].font_rid != RID()) {
  934. TS->font_draw_glyph_outline(glyphs[j].font_rid, ci, glyphs[j].font_size, outline_size, Vector2(char_margin + char_ofs + ofs_x + glyphs[j].x_off, ofs_y + glyphs[j].y_off), glyphs[j].index, outline_color);
  935. }
  936. }
  937. char_ofs += glyphs[j].advance;
  938. }
  939. if ((char_ofs + char_margin) >= xmargin_end) {
  940. break;
  941. }
  942. }
  943. char_ofs = 0;
  944. }
  945. for (int j = 0; j < gl_size; j++) {
  946. const Variant *color_data = color_map.getptr(glyphs[j].start);
  947. if (color_data != nullptr) {
  948. current_color = (color_data->operator Dictionary()).get("color", font_color);
  949. if (!editable && current_color.a > font_readonly_color.a) {
  950. current_color.a = font_readonly_color.a;
  951. }
  952. }
  953. if (selection.active && line >= selection.from_line && line <= selection.to_line) { // Selection
  954. int sel_from = (line > selection.from_line) ? TS->shaped_text_get_range(rid).x : selection.from_column;
  955. int sel_to = (line < selection.to_line) ? TS->shaped_text_get_range(rid).y : selection.to_column;
  956. if (glyphs[j].start >= sel_from && glyphs[j].end <= sel_to && override_selected_font_color) {
  957. current_color = font_selected_color;
  958. }
  959. }
  960. int char_pos = char_ofs + char_margin + ofs_x;
  961. if (char_pos >= xmargin_beg) {
  962. if (highlight_matching_braces_enabled) {
  963. if ((brace_open_match_line == line && brace_open_match_column == glyphs[j].start) ||
  964. (caret.column == glyphs[j].start && caret.line == line && caret_wrap_index == line_wrap_index && (brace_open_matching || brace_open_mismatch))) {
  965. if (brace_open_mismatch) {
  966. current_color = brace_mismatch_color;
  967. }
  968. Rect2 rect = Rect2(char_pos, ofs_y + font->get_underline_position(font_size), glyphs[j].advance * glyphs[j].repeat, font->get_underline_thickness(font_size));
  969. draw_rect(rect, current_color);
  970. }
  971. if ((brace_close_match_line == line && brace_close_match_column == glyphs[j].start) ||
  972. (caret.column == glyphs[j].start + 1 && caret.line == line && caret_wrap_index == line_wrap_index && (brace_close_matching || brace_close_mismatch))) {
  973. if (brace_close_mismatch) {
  974. current_color = brace_mismatch_color;
  975. }
  976. Rect2 rect = Rect2(char_pos, ofs_y + font->get_underline_position(font_size), glyphs[j].advance * glyphs[j].repeat, font->get_underline_thickness(font_size));
  977. draw_rect(rect, current_color);
  978. }
  979. }
  980. if (draw_tabs && ((glyphs[j].flags & TextServer::GRAPHEME_IS_TAB) == TextServer::GRAPHEME_IS_TAB)) {
  981. int yofs = (text_height - tab_icon->get_height()) / 2 - ldata->get_line_ascent(line_wrap_index);
  982. tab_icon->draw(ci, Point2(char_pos, ofs_y + yofs), current_color);
  983. } else if (draw_spaces && ((glyphs[j].flags & TextServer::GRAPHEME_IS_SPACE) == TextServer::GRAPHEME_IS_SPACE)) {
  984. int yofs = (text_height - space_icon->get_height()) / 2 - ldata->get_line_ascent(line_wrap_index);
  985. int xofs = (glyphs[j].advance * glyphs[j].repeat - space_icon->get_width()) / 2;
  986. space_icon->draw(ci, Point2(char_pos + xofs, ofs_y + yofs), current_color);
  987. }
  988. }
  989. for (int k = 0; k < glyphs[j].repeat; k++) {
  990. if (!clipped && (char_ofs + char_margin) >= xmargin_beg && (char_ofs + glyphs[j].advance + char_margin) <= xmargin_end) {
  991. if (glyphs[j].font_rid != RID()) {
  992. TS->font_draw_glyph(glyphs[j].font_rid, ci, glyphs[j].font_size, Vector2(char_margin + char_ofs + ofs_x + glyphs[j].x_off, ofs_y + glyphs[j].y_off), glyphs[j].index, current_color);
  993. } else if ((glyphs[j].flags & TextServer::GRAPHEME_IS_VIRTUAL) != TextServer::GRAPHEME_IS_VIRTUAL) {
  994. TS->draw_hex_code_box(ci, glyphs[j].font_size, Vector2(char_margin + char_ofs + ofs_x + glyphs[j].x_off, ofs_y + glyphs[j].y_off), glyphs[j].index, current_color);
  995. }
  996. }
  997. char_ofs += glyphs[j].advance;
  998. }
  999. if ((char_ofs + char_margin) >= xmargin_end) {
  1000. break;
  1001. }
  1002. }
  1003. // is_line_folded
  1004. if (line_wrap_index == line_wrap_amount && line < text.size() - 1 && _is_line_hidden(line + 1)) {
  1005. int xofs = char_ofs + char_margin + ofs_x + (folded_eol_icon->get_width() / 2);
  1006. if (xofs >= xmargin_beg && xofs < xmargin_end) {
  1007. int yofs = (text_height - folded_eol_icon->get_height()) / 2 - ldata->get_line_ascent(line_wrap_index);
  1008. Color eol_color = code_folding_color;
  1009. eol_color.a = 1;
  1010. folded_eol_icon->draw(ci, Point2(xofs, ofs_y + yofs), eol_color);
  1011. }
  1012. }
  1013. // Carets.
  1014. int caret_width = Math::round(1 * get_theme_default_base_scale());
  1015. if (!clipped && caret.line == line && line_wrap_index == caret_wrap_index) {
  1016. caret.draw_pos.y = ofs_y + ldata->get_line_descent(line_wrap_index);
  1017. if (ime_text.length() == 0) {
  1018. CaretInfo ts_caret;
  1019. if (str.length() != 0) {
  1020. // Get carets.
  1021. ts_caret = TS->shaped_text_get_carets(rid, caret.column);
  1022. } else {
  1023. // No carets, add one at the start.
  1024. int h = font->get_height(font_size);
  1025. if (rtl) {
  1026. ts_caret.l_dir = TextServer::DIRECTION_RTL;
  1027. ts_caret.l_caret = Rect2(Vector2(xmargin_end - char_margin + ofs_x, -h / 2), Size2(caret_width * 4, h));
  1028. } else {
  1029. ts_caret.l_dir = TextServer::DIRECTION_LTR;
  1030. ts_caret.l_caret = Rect2(Vector2(char_ofs, -h / 2), Size2(caret_width * 4, h));
  1031. }
  1032. }
  1033. if ((ts_caret.l_caret != Rect2() && (ts_caret.l_dir == TextServer::DIRECTION_AUTO || ts_caret.l_dir == (TextServer::Direction)input_direction)) || (ts_caret.t_caret == Rect2())) {
  1034. caret.draw_pos.x = char_margin + ofs_x + ts_caret.l_caret.position.x;
  1035. } else {
  1036. caret.draw_pos.x = char_margin + ofs_x + ts_caret.t_caret.position.x;
  1037. }
  1038. if (caret.draw_pos.x >= xmargin_beg && caret.draw_pos.x < xmargin_end) {
  1039. caret.visible = true;
  1040. if (draw_caret) {
  1041. if (caret_type == CaretType::CARET_TYPE_BLOCK || overtype_mode) {
  1042. //Block or underline caret, draw trailing carets at full height.
  1043. int h = font->get_height(font_size);
  1044. if (ts_caret.t_caret != Rect2()) {
  1045. if (overtype_mode) {
  1046. ts_caret.t_caret.position.y = TS->shaped_text_get_descent(rid);
  1047. ts_caret.t_caret.size.y = caret_width;
  1048. } else {
  1049. ts_caret.t_caret.position.y = -TS->shaped_text_get_ascent(rid);
  1050. ts_caret.t_caret.size.y = h;
  1051. }
  1052. ts_caret.t_caret.position += Vector2(char_margin + ofs_x, ofs_y);
  1053. draw_rect(ts_caret.t_caret, caret_color, overtype_mode);
  1054. if (ts_caret.l_caret != Rect2() && ts_caret.l_dir != ts_caret.t_dir) {
  1055. ts_caret.l_caret.position += Vector2(char_margin + ofs_x, ofs_y);
  1056. ts_caret.l_caret.size.x = caret_width;
  1057. draw_rect(ts_caret.l_caret, caret_color * Color(1, 1, 1, 0.5));
  1058. }
  1059. } else { // End of the line.
  1060. if (gl_size > 0) {
  1061. // Adjust for actual line dimensions.
  1062. if (overtype_mode) {
  1063. ts_caret.l_caret.position.y = TS->shaped_text_get_descent(rid);
  1064. ts_caret.l_caret.size.y = caret_width;
  1065. } else {
  1066. ts_caret.l_caret.position.y = -TS->shaped_text_get_ascent(rid);
  1067. ts_caret.l_caret.size.y = h;
  1068. }
  1069. } else if (overtype_mode) {
  1070. ts_caret.l_caret.position.y += ts_caret.l_caret.size.y;
  1071. ts_caret.l_caret.size.y = caret_width;
  1072. }
  1073. if (ts_caret.l_caret.position.x >= TS->shaped_text_get_size(rid).x) {
  1074. ts_caret.l_caret.size.x = font->get_char_size('m', 0, font_size).x;
  1075. } else {
  1076. ts_caret.l_caret.size.x = 3 * caret_width;
  1077. }
  1078. ts_caret.l_caret.position += Vector2(char_margin + ofs_x, ofs_y);
  1079. if (ts_caret.l_dir == TextServer::DIRECTION_RTL) {
  1080. ts_caret.l_caret.position.x -= ts_caret.l_caret.size.x;
  1081. }
  1082. draw_rect(ts_caret.l_caret, caret_color, overtype_mode);
  1083. }
  1084. } else {
  1085. // Normal caret.
  1086. if (ts_caret.l_caret != Rect2() && ts_caret.l_dir == TextServer::DIRECTION_AUTO) {
  1087. // Draw extra marker on top of mid caret.
  1088. Rect2 trect = Rect2(ts_caret.l_caret.position.x - 3 * caret_width, ts_caret.l_caret.position.y, 6 * caret_width, caret_width);
  1089. trect.position += Vector2(char_margin + ofs_x, ofs_y);
  1090. RenderingServer::get_singleton()->canvas_item_add_rect(ci, trect, caret_color);
  1091. }
  1092. ts_caret.l_caret.position += Vector2(char_margin + ofs_x, ofs_y);
  1093. ts_caret.l_caret.size.x = caret_width;
  1094. draw_rect(ts_caret.l_caret, caret_color);
  1095. ts_caret.t_caret.position += Vector2(char_margin + ofs_x, ofs_y);
  1096. ts_caret.t_caret.size.x = caret_width;
  1097. draw_rect(ts_caret.t_caret, caret_color);
  1098. }
  1099. }
  1100. }
  1101. } else {
  1102. {
  1103. // IME Intermediate text range.
  1104. Vector<Vector2> sel = TS->shaped_text_get_selection(rid, caret.column, caret.column + ime_text.length());
  1105. for (int j = 0; j < sel.size(); j++) {
  1106. Rect2 rect = Rect2(sel[j].x + char_margin + ofs_x, ofs_y, sel[j].y - sel[j].x, text_height);
  1107. if (rect.position.x + rect.size.x <= xmargin_beg || rect.position.x > xmargin_end) {
  1108. continue;
  1109. }
  1110. if (rect.position.x < xmargin_beg) {
  1111. rect.size.x -= (xmargin_beg - rect.position.x);
  1112. rect.position.x = xmargin_beg;
  1113. } else if (rect.position.x + rect.size.x > xmargin_end) {
  1114. rect.size.x = xmargin_end - rect.position.x;
  1115. }
  1116. rect.size.y = caret_width;
  1117. draw_rect(rect, caret_color);
  1118. caret.draw_pos.x = rect.position.x;
  1119. }
  1120. }
  1121. {
  1122. // IME caret.
  1123. Vector<Vector2> sel = TS->shaped_text_get_selection(rid, caret.column + ime_selection.x, caret.column + ime_selection.x + ime_selection.y);
  1124. for (int j = 0; j < sel.size(); j++) {
  1125. Rect2 rect = Rect2(sel[j].x + char_margin + ofs_x, ofs_y, sel[j].y - sel[j].x, text_height);
  1126. if (rect.position.x + rect.size.x <= xmargin_beg || rect.position.x > xmargin_end) {
  1127. continue;
  1128. }
  1129. if (rect.position.x < xmargin_beg) {
  1130. rect.size.x -= (xmargin_beg - rect.position.x);
  1131. rect.position.x = xmargin_beg;
  1132. } else if (rect.position.x + rect.size.x > xmargin_end) {
  1133. rect.size.x = xmargin_end - rect.position.x;
  1134. }
  1135. rect.size.y = caret_width * 3;
  1136. draw_rect(rect, caret_color);
  1137. caret.draw_pos.x = rect.position.x;
  1138. }
  1139. }
  1140. }
  1141. }
  1142. }
  1143. }
  1144. if (has_focus()) {
  1145. if (get_viewport()->get_window_id() != DisplayServer::INVALID_WINDOW_ID && DisplayServer::get_singleton()->has_feature(DisplayServer::FEATURE_IME)) {
  1146. DisplayServer::get_singleton()->window_set_ime_active(true, get_viewport()->get_window_id());
  1147. DisplayServer::get_singleton()->window_set_ime_position(get_global_position() + caret.draw_pos, get_viewport()->get_window_id());
  1148. }
  1149. }
  1150. } break;
  1151. case NOTIFICATION_FOCUS_ENTER: {
  1152. if (caret_blink_enabled) {
  1153. caret_blink_timer->start();
  1154. } else {
  1155. draw_caret = true;
  1156. }
  1157. if (get_viewport()->get_window_id() != DisplayServer::INVALID_WINDOW_ID && DisplayServer::get_singleton()->has_feature(DisplayServer::FEATURE_IME)) {
  1158. DisplayServer::get_singleton()->window_set_ime_active(true, get_viewport()->get_window_id());
  1159. DisplayServer::get_singleton()->window_set_ime_position(get_global_position() + get_caret_draw_pos(), get_viewport()->get_window_id());
  1160. }
  1161. if (DisplayServer::get_singleton()->has_feature(DisplayServer::FEATURE_VIRTUAL_KEYBOARD) && virtual_keyboard_enabled) {
  1162. int caret_start = -1;
  1163. int caret_end = -1;
  1164. if (!selection.active) {
  1165. String full_text = _base_get_text(0, 0, caret.line, caret.column);
  1166. caret_start = full_text.length();
  1167. } else {
  1168. String pre_text = _base_get_text(0, 0, selection.from_line, selection.from_column);
  1169. String post_text = get_selected_text();
  1170. caret_start = pre_text.length();
  1171. caret_end = caret_start + post_text.length();
  1172. }
  1173. DisplayServer::get_singleton()->virtual_keyboard_show(get_text(), get_global_rect(), true, -1, caret_start, caret_end);
  1174. }
  1175. } break;
  1176. case NOTIFICATION_FOCUS_EXIT: {
  1177. if (caret_blink_enabled) {
  1178. caret_blink_timer->stop();
  1179. }
  1180. if (get_viewport()->get_window_id() != DisplayServer::INVALID_WINDOW_ID && DisplayServer::get_singleton()->has_feature(DisplayServer::FEATURE_IME)) {
  1181. DisplayServer::get_singleton()->window_set_ime_position(Point2(), get_viewport()->get_window_id());
  1182. DisplayServer::get_singleton()->window_set_ime_active(false, get_viewport()->get_window_id());
  1183. }
  1184. ime_text = "";
  1185. ime_selection = Point2();
  1186. text.invalidate_cache(caret.line, caret.column, ime_text);
  1187. if (DisplayServer::get_singleton()->has_feature(DisplayServer::FEATURE_VIRTUAL_KEYBOARD) && virtual_keyboard_enabled) {
  1188. DisplayServer::get_singleton()->virtual_keyboard_hide();
  1189. }
  1190. } break;
  1191. case MainLoop::NOTIFICATION_OS_IME_UPDATE: {
  1192. if (has_focus()) {
  1193. ime_text = DisplayServer::get_singleton()->ime_get_text();
  1194. ime_selection = DisplayServer::get_singleton()->ime_get_selection();
  1195. String t;
  1196. if (caret.column >= 0) {
  1197. t = text[caret.line].substr(0, caret.column) + ime_text + text[caret.line].substr(caret.column, text[caret.line].length());
  1198. } else {
  1199. t = ime_text;
  1200. }
  1201. text.invalidate_cache(caret.line, caret.column, t, structured_text_parser(st_parser, st_args, t));
  1202. update();
  1203. }
  1204. } break;
  1205. }
  1206. }
  1207. void TextEdit::gui_input(const Ref<InputEvent> &p_gui_input) {
  1208. ERR_FAIL_COND(p_gui_input.is_null());
  1209. double prev_v_scroll = v_scroll->get_value();
  1210. double prev_h_scroll = h_scroll->get_value();
  1211. Ref<InputEventMouseButton> mb = p_gui_input;
  1212. if (mb.is_valid()) {
  1213. Vector2i mpos = mb->get_position();
  1214. if (is_layout_rtl()) {
  1215. mpos.x = get_size().x - mpos.x;
  1216. }
  1217. if (ime_text.length() != 0) {
  1218. // Ignore mouse clicks in IME input mode.
  1219. return;
  1220. }
  1221. if (mb->is_pressed()) {
  1222. if (mb->get_button_index() == MOUSE_BUTTON_WHEEL_UP && !mb->is_command_pressed()) {
  1223. if (mb->is_shift_pressed()) {
  1224. h_scroll->set_value(h_scroll->get_value() - (100 * mb->get_factor()));
  1225. } else if (mb->is_alt_pressed()) {
  1226. // Scroll 5 times as fast as normal (like in Visual Studio Code).
  1227. _scroll_up(15 * mb->get_factor());
  1228. } else if (v_scroll->is_visible()) {
  1229. // Scroll 3 lines.
  1230. _scroll_up(3 * mb->get_factor());
  1231. }
  1232. }
  1233. if (mb->get_button_index() == MOUSE_BUTTON_WHEEL_DOWN && !mb->is_command_pressed()) {
  1234. if (mb->is_shift_pressed()) {
  1235. h_scroll->set_value(h_scroll->get_value() + (100 * mb->get_factor()));
  1236. } else if (mb->is_alt_pressed()) {
  1237. // Scroll 5 times as fast as normal (like in Visual Studio Code).
  1238. _scroll_down(15 * mb->get_factor());
  1239. } else if (v_scroll->is_visible()) {
  1240. // Scroll 3 lines.
  1241. _scroll_down(3 * mb->get_factor());
  1242. }
  1243. }
  1244. if (mb->get_button_index() == MOUSE_BUTTON_WHEEL_LEFT) {
  1245. h_scroll->set_value(h_scroll->get_value() - (100 * mb->get_factor()));
  1246. }
  1247. if (mb->get_button_index() == MOUSE_BUTTON_WHEEL_RIGHT) {
  1248. h_scroll->set_value(h_scroll->get_value() + (100 * mb->get_factor()));
  1249. }
  1250. if (mb->get_button_index() == MOUSE_BUTTON_LEFT) {
  1251. _reset_caret_blink_timer();
  1252. Point2i pos = get_line_column_at_pos(mpos);
  1253. int row = pos.y;
  1254. int col = pos.x;
  1255. int left_margin = style_normal->get_margin(SIDE_LEFT);
  1256. for (int i = 0; i < gutters.size(); i++) {
  1257. if (!gutters[i].draw || gutters[i].width <= 0) {
  1258. continue;
  1259. }
  1260. if (mpos.x > left_margin && mpos.x <= (left_margin + gutters[i].width) - 3) {
  1261. emit_signal(SNAME("gutter_clicked"), row, i);
  1262. return;
  1263. }
  1264. left_margin += gutters[i].width;
  1265. }
  1266. // minimap
  1267. if (draw_minimap) {
  1268. _update_minimap_click();
  1269. if (dragging_minimap) {
  1270. return;
  1271. }
  1272. }
  1273. int prev_col = caret.column;
  1274. int prev_line = caret.line;
  1275. set_caret_line(row, false, false);
  1276. set_caret_column(col);
  1277. if (mb->is_shift_pressed() && (caret.column != prev_col || caret.line != prev_line)) {
  1278. if (!selection.active) {
  1279. selection.active = true;
  1280. selection.selecting_mode = SelectionMode::SELECTION_MODE_POINTER;
  1281. selection.from_column = prev_col;
  1282. selection.from_line = prev_line;
  1283. selection.to_column = caret.column;
  1284. selection.to_line = caret.line;
  1285. if (selection.from_line > selection.to_line || (selection.from_line == selection.to_line && selection.from_column > selection.to_column)) {
  1286. SWAP(selection.from_column, selection.to_column);
  1287. SWAP(selection.from_line, selection.to_line);
  1288. selection.shiftclick_left = false;
  1289. } else {
  1290. selection.shiftclick_left = true;
  1291. }
  1292. selection.selecting_line = prev_line;
  1293. selection.selecting_column = prev_col;
  1294. update();
  1295. } else {
  1296. if (caret.line < selection.selecting_line || (caret.line == selection.selecting_line && caret.column < selection.selecting_column)) {
  1297. if (selection.shiftclick_left) {
  1298. selection.shiftclick_left = !selection.shiftclick_left;
  1299. }
  1300. selection.from_column = caret.column;
  1301. selection.from_line = caret.line;
  1302. } else if (caret.line > selection.selecting_line || (caret.line == selection.selecting_line && caret.column > selection.selecting_column)) {
  1303. if (!selection.shiftclick_left) {
  1304. SWAP(selection.from_column, selection.to_column);
  1305. SWAP(selection.from_line, selection.to_line);
  1306. selection.shiftclick_left = !selection.shiftclick_left;
  1307. }
  1308. selection.to_column = caret.column;
  1309. selection.to_line = caret.line;
  1310. } else {
  1311. selection.active = false;
  1312. }
  1313. update();
  1314. }
  1315. } else {
  1316. selection.active = false;
  1317. selection.selecting_mode = SelectionMode::SELECTION_MODE_POINTER;
  1318. selection.selecting_line = row;
  1319. selection.selecting_column = col;
  1320. }
  1321. const int triple_click_timeout = 600;
  1322. const int triple_click_tolerance = 5;
  1323. if (!mb->is_double_click() && (OS::get_singleton()->get_ticks_msec() - last_dblclk) < triple_click_timeout && mb->get_position().distance_to(last_dblclk_pos) < triple_click_tolerance) {
  1324. // Triple-click select line.
  1325. selection.selecting_mode = SelectionMode::SELECTION_MODE_LINE;
  1326. _update_selection_mode_line();
  1327. last_dblclk = 0;
  1328. } else if (mb->is_double_click() && text[caret.line].length()) {
  1329. // Double-click select word.
  1330. selection.selecting_mode = SelectionMode::SELECTION_MODE_WORD;
  1331. _update_selection_mode_word();
  1332. last_dblclk = OS::get_singleton()->get_ticks_msec();
  1333. last_dblclk_pos = mb->get_position();
  1334. }
  1335. update();
  1336. }
  1337. if (mb->get_button_index() == MOUSE_BUTTON_RIGHT && context_menu_enabled) {
  1338. _reset_caret_blink_timer();
  1339. Point2i pos = get_line_column_at_pos(mpos);
  1340. int row = pos.y;
  1341. int col = pos.x;
  1342. if (is_move_caret_on_right_click_enabled()) {
  1343. if (has_selection()) {
  1344. int from_line = get_selection_from_line();
  1345. int to_line = get_selection_to_line();
  1346. int from_column = get_selection_from_column();
  1347. int to_column = get_selection_to_column();
  1348. if (row < from_line || row > to_line || (row == from_line && col < from_column) || (row == to_line && col > to_column)) {
  1349. // Right click is outside the selected text.
  1350. deselect();
  1351. }
  1352. }
  1353. if (!has_selection()) {
  1354. set_caret_line(row, true, false);
  1355. set_caret_column(col);
  1356. }
  1357. }
  1358. _generate_context_menu();
  1359. menu->set_position(get_screen_transform().xform(mpos));
  1360. menu->set_size(Vector2(1, 1));
  1361. menu->popup();
  1362. grab_focus();
  1363. }
  1364. } else {
  1365. if (mb->get_button_index() == MOUSE_BUTTON_LEFT) {
  1366. dragging_minimap = false;
  1367. dragging_selection = false;
  1368. can_drag_minimap = false;
  1369. click_select_held->stop();
  1370. }
  1371. // Notify to show soft keyboard.
  1372. notification(NOTIFICATION_FOCUS_ENTER);
  1373. }
  1374. }
  1375. const Ref<InputEventPanGesture> pan_gesture = p_gui_input;
  1376. if (pan_gesture.is_valid()) {
  1377. const real_t delta = pan_gesture->get_delta().y;
  1378. if (delta < 0) {
  1379. _scroll_up(-delta);
  1380. } else {
  1381. _scroll_down(delta);
  1382. }
  1383. h_scroll->set_value(h_scroll->get_value() + pan_gesture->get_delta().x * 100);
  1384. if (v_scroll->get_value() != prev_v_scroll || h_scroll->get_value() != prev_h_scroll) {
  1385. accept_event(); // Accept event if scroll changed.
  1386. }
  1387. return;
  1388. }
  1389. Ref<InputEventMouseMotion> mm = p_gui_input;
  1390. if (mm.is_valid()) {
  1391. Vector2i mpos = mm->get_position();
  1392. if (is_layout_rtl()) {
  1393. mpos.x = get_size().x - mpos.x;
  1394. }
  1395. if (mm->get_button_mask() & MOUSE_BUTTON_MASK_LEFT && get_viewport()->gui_get_drag_data() == Variant()) { // Ignore if dragging.
  1396. _reset_caret_blink_timer();
  1397. if (draw_minimap && !dragging_selection) {
  1398. _update_minimap_drag();
  1399. }
  1400. if (!dragging_minimap) {
  1401. switch (selection.selecting_mode) {
  1402. case SelectionMode::SELECTION_MODE_POINTER: {
  1403. _update_selection_mode_pointer();
  1404. } break;
  1405. case SelectionMode::SELECTION_MODE_WORD: {
  1406. _update_selection_mode_word();
  1407. } break;
  1408. case SelectionMode::SELECTION_MODE_LINE: {
  1409. _update_selection_mode_line();
  1410. } break;
  1411. default: {
  1412. break;
  1413. }
  1414. }
  1415. }
  1416. }
  1417. // Check if user is hovering a different gutter, and update if yes.
  1418. Vector2i current_hovered_gutter = Vector2i(-1, -1);
  1419. int left_margin = style_normal->get_margin(SIDE_LEFT);
  1420. if (mpos.x <= left_margin + gutters_width + gutter_padding) {
  1421. int hovered_row = get_line_column_at_pos(mpos).y;
  1422. for (int i = 0; i < gutters.size(); i++) {
  1423. if (!gutters[i].draw || gutters[i].width <= 0) {
  1424. continue;
  1425. }
  1426. if (mpos.x > left_margin && mpos.x <= (left_margin + gutters[i].width) - 3) {
  1427. // We are in this gutter i's horizontal area.
  1428. current_hovered_gutter = Vector2i(i, hovered_row);
  1429. break;
  1430. }
  1431. left_margin += gutters[i].width;
  1432. }
  1433. }
  1434. if (current_hovered_gutter != hovered_gutter) {
  1435. hovered_gutter = current_hovered_gutter;
  1436. update();
  1437. }
  1438. }
  1439. if (draw_minimap && !dragging_selection) {
  1440. _update_minimap_hover();
  1441. }
  1442. if (v_scroll->get_value() != prev_v_scroll || h_scroll->get_value() != prev_h_scroll) {
  1443. accept_event(); // Accept event if scroll changed.
  1444. }
  1445. Ref<InputEventKey> k = p_gui_input;
  1446. if (k.is_valid()) {
  1447. if (!k->is_pressed()) {
  1448. return;
  1449. }
  1450. // If a modifier has been pressed, and nothing else, return.
  1451. if (k->get_keycode() == KEY_CTRL || k->get_keycode() == KEY_ALT || k->get_keycode() == KEY_SHIFT || k->get_keycode() == KEY_META) {
  1452. return;
  1453. }
  1454. _reset_caret_blink_timer();
  1455. // Allow unicode handling if:
  1456. // * No Modifiers are pressed (except shift)
  1457. bool allow_unicode_handling = !(k->is_command_pressed() || k->is_ctrl_pressed() || k->is_alt_pressed() || k->is_meta_pressed());
  1458. selection.selecting_text = false;
  1459. // Check and handle all built in shortcuts.
  1460. // NEWLINES.
  1461. if (k->is_action("ui_text_newline_above", true)) {
  1462. _new_line(false, true);
  1463. accept_event();
  1464. return;
  1465. }
  1466. if (k->is_action("ui_text_newline_blank", true)) {
  1467. _new_line(false);
  1468. accept_event();
  1469. return;
  1470. }
  1471. if (k->is_action("ui_text_newline", true)) {
  1472. _new_line();
  1473. accept_event();
  1474. return;
  1475. }
  1476. // BACKSPACE AND DELETE.
  1477. if (k->is_action("ui_text_backspace_all_to_left", true)) {
  1478. _do_backspace(false, true);
  1479. accept_event();
  1480. return;
  1481. }
  1482. if (k->is_action("ui_text_backspace_word", true)) {
  1483. _do_backspace(true);
  1484. accept_event();
  1485. return;
  1486. }
  1487. if (k->is_action("ui_text_backspace", true)) {
  1488. _do_backspace();
  1489. accept_event();
  1490. return;
  1491. }
  1492. if (k->is_action("ui_text_delete_all_to_right", true)) {
  1493. _delete(false, true);
  1494. accept_event();
  1495. return;
  1496. }
  1497. if (k->is_action("ui_text_delete_word", true)) {
  1498. _delete(true);
  1499. accept_event();
  1500. return;
  1501. }
  1502. if (k->is_action("ui_text_delete", true)) {
  1503. _delete();
  1504. accept_event();
  1505. return;
  1506. }
  1507. // SCROLLING.
  1508. if (k->is_action("ui_text_scroll_up", true)) {
  1509. _scroll_lines_up();
  1510. accept_event();
  1511. return;
  1512. }
  1513. if (k->is_action("ui_text_scroll_down", true)) {
  1514. _scroll_lines_down();
  1515. accept_event();
  1516. return;
  1517. }
  1518. // SELECT ALL, SELECT WORD UNDER CARET, CUT, COPY, PASTE.
  1519. if (k->is_action("ui_text_select_all", true)) {
  1520. select_all();
  1521. accept_event();
  1522. return;
  1523. }
  1524. if (k->is_action("ui_text_select_word_under_caret", true)) {
  1525. select_word_under_caret();
  1526. accept_event();
  1527. return;
  1528. }
  1529. if (k->is_action("ui_cut", true)) {
  1530. cut();
  1531. accept_event();
  1532. return;
  1533. }
  1534. if (k->is_action("ui_copy", true)) {
  1535. copy();
  1536. accept_event();
  1537. return;
  1538. }
  1539. if (k->is_action("ui_paste", true)) {
  1540. paste();
  1541. accept_event();
  1542. return;
  1543. }
  1544. // UNDO/REDO.
  1545. if (k->is_action("ui_undo", true)) {
  1546. undo();
  1547. accept_event();
  1548. return;
  1549. }
  1550. if (k->is_action("ui_redo", true)) {
  1551. redo();
  1552. accept_event();
  1553. return;
  1554. }
  1555. // MISC.
  1556. if (k->is_action("ui_menu", true)) {
  1557. if (context_menu_enabled) {
  1558. _generate_context_menu();
  1559. adjust_viewport_to_caret();
  1560. menu->set_position(get_screen_transform().xform(get_caret_draw_pos()));
  1561. menu->set_size(Vector2(1, 1));
  1562. menu->popup();
  1563. menu->grab_focus();
  1564. }
  1565. accept_event();
  1566. return;
  1567. }
  1568. if (k->is_action("ui_text_toggle_insert_mode", true)) {
  1569. set_overtype_mode_enabled(!overtype_mode);
  1570. accept_event();
  1571. return;
  1572. }
  1573. if (k->is_action("ui_swap_input_direction", true)) {
  1574. _swap_current_input_direction();
  1575. accept_event();
  1576. return;
  1577. }
  1578. // CARET MOVEMENT
  1579. k = k->duplicate();
  1580. bool shift_pressed = k->is_shift_pressed();
  1581. // Remove shift or else actions will not match. Use above variable for selection.
  1582. k->set_shift_pressed(false);
  1583. // CARET MOVEMENT - LEFT, RIGHT.
  1584. if (k->is_action("ui_text_caret_word_left", true)) {
  1585. _move_caret_left(shift_pressed, true);
  1586. accept_event();
  1587. return;
  1588. }
  1589. if (k->is_action("ui_text_caret_left", true)) {
  1590. _move_caret_left(shift_pressed, false);
  1591. accept_event();
  1592. return;
  1593. }
  1594. if (k->is_action("ui_text_caret_word_right", true)) {
  1595. _move_caret_right(shift_pressed, true);
  1596. accept_event();
  1597. return;
  1598. }
  1599. if (k->is_action("ui_text_caret_right", true)) {
  1600. _move_caret_right(shift_pressed, false);
  1601. accept_event();
  1602. return;
  1603. }
  1604. // CARET MOVEMENT - UP, DOWN.
  1605. if (k->is_action("ui_text_caret_up", true)) {
  1606. _move_caret_up(shift_pressed);
  1607. accept_event();
  1608. return;
  1609. }
  1610. if (k->is_action("ui_text_caret_down", true)) {
  1611. _move_caret_down(shift_pressed);
  1612. accept_event();
  1613. return;
  1614. }
  1615. // CARET MOVEMENT - DOCUMENT START/END.
  1616. if (k->is_action("ui_text_caret_document_start", true)) { // && shift_pressed) {
  1617. _move_caret_document_start(shift_pressed);
  1618. accept_event();
  1619. return;
  1620. }
  1621. if (k->is_action("ui_text_caret_document_end", true)) { // && shift_pressed) {
  1622. _move_caret_document_end(shift_pressed);
  1623. accept_event();
  1624. return;
  1625. }
  1626. // CARET MOVEMENT - LINE START/END.
  1627. if (k->is_action("ui_text_caret_line_start", true)) {
  1628. _move_caret_to_line_start(shift_pressed);
  1629. accept_event();
  1630. return;
  1631. }
  1632. if (k->is_action("ui_text_caret_line_end", true)) {
  1633. _move_caret_to_line_end(shift_pressed);
  1634. accept_event();
  1635. return;
  1636. }
  1637. // CARET MOVEMENT - PAGE UP/DOWN.
  1638. if (k->is_action("ui_text_caret_page_up", true)) {
  1639. _move_caret_page_up(shift_pressed);
  1640. accept_event();
  1641. return;
  1642. }
  1643. if (k->is_action("ui_text_caret_page_down", true)) {
  1644. _move_caret_page_down(shift_pressed);
  1645. accept_event();
  1646. return;
  1647. }
  1648. // Handle Unicode (if no modifiers active). Tab has a value of 0x09.
  1649. if (allow_unicode_handling && editable && (k->get_unicode() >= 32 || k->get_keycode() == KEY_TAB)) {
  1650. handle_unicode_input(k->get_unicode());
  1651. accept_event();
  1652. return;
  1653. }
  1654. }
  1655. }
  1656. /* Input actions. */
  1657. void TextEdit::_swap_current_input_direction() {
  1658. if (input_direction == TEXT_DIRECTION_LTR) {
  1659. input_direction = TEXT_DIRECTION_RTL;
  1660. } else {
  1661. input_direction = TEXT_DIRECTION_LTR;
  1662. }
  1663. set_caret_column(caret.column);
  1664. update();
  1665. }
  1666. void TextEdit::_new_line(bool p_split_current_line, bool p_above) {
  1667. if (!editable) {
  1668. return;
  1669. }
  1670. begin_complex_operation();
  1671. bool first_line = false;
  1672. if (!p_split_current_line) {
  1673. if (p_above) {
  1674. if (caret.line > 0) {
  1675. set_caret_line(caret.line - 1, false);
  1676. set_caret_column(text[caret.line].length());
  1677. } else {
  1678. set_caret_column(0);
  1679. first_line = true;
  1680. }
  1681. } else {
  1682. set_caret_column(text[caret.line].length());
  1683. }
  1684. }
  1685. insert_text_at_caret("\n");
  1686. if (first_line) {
  1687. set_caret_line(0);
  1688. }
  1689. end_complex_operation();
  1690. }
  1691. void TextEdit::_move_caret_left(bool p_select, bool p_move_by_word) {
  1692. // Handle selection
  1693. if (p_select) {
  1694. _pre_shift_selection();
  1695. } else if (selection.active && !p_move_by_word) {
  1696. // If a selection is active, move caret to start of selection
  1697. set_caret_line(selection.from_line);
  1698. set_caret_column(selection.from_column);
  1699. deselect();
  1700. return;
  1701. } else {
  1702. deselect();
  1703. }
  1704. if (p_move_by_word) {
  1705. int cc = caret.column;
  1706. if (cc == 0 && caret.line > 0) {
  1707. set_caret_line(caret.line - 1);
  1708. set_caret_column(text[caret.line].length());
  1709. } else {
  1710. PackedInt32Array words = TS->shaped_text_get_word_breaks(text.get_line_data(caret.line)->get_rid());
  1711. for (int i = words.size() - 2; i >= 0; i = i - 2) {
  1712. if (words[i] < cc) {
  1713. cc = words[i];
  1714. break;
  1715. }
  1716. }
  1717. set_caret_column(cc);
  1718. }
  1719. } else {
  1720. // If the caret is at the start of the line, and not on the first line, move it up to the end of the previous line.
  1721. if (caret.column == 0) {
  1722. if (caret.line > 0) {
  1723. set_caret_line(caret.line - get_next_visible_line_offset_from(CLAMP(caret.line - 1, 0, text.size() - 1), -1));
  1724. set_caret_column(text[caret.line].length());
  1725. }
  1726. } else {
  1727. if (caret_mid_grapheme_enabled) {
  1728. set_caret_column(get_caret_column() - 1);
  1729. } else {
  1730. set_caret_column(TS->shaped_text_prev_grapheme_pos(text.get_line_data(caret.line)->get_rid(), get_caret_column()));
  1731. }
  1732. }
  1733. }
  1734. if (p_select) {
  1735. _post_shift_selection();
  1736. }
  1737. }
  1738. void TextEdit::_move_caret_right(bool p_select, bool p_move_by_word) {
  1739. // Handle selection
  1740. if (p_select) {
  1741. _pre_shift_selection();
  1742. } else if (selection.active && !p_move_by_word) {
  1743. // If a selection is active, move caret to end of selection
  1744. set_caret_line(selection.to_line);
  1745. set_caret_column(selection.to_column);
  1746. deselect();
  1747. return;
  1748. } else {
  1749. deselect();
  1750. }
  1751. if (p_move_by_word) {
  1752. int cc = caret.column;
  1753. if (cc == text[caret.line].length() && caret.line < text.size() - 1) {
  1754. set_caret_line(caret.line + 1);
  1755. set_caret_column(0);
  1756. } else {
  1757. PackedInt32Array words = TS->shaped_text_get_word_breaks(text.get_line_data(caret.line)->get_rid());
  1758. for (int i = 1; i < words.size(); i = i + 2) {
  1759. if (words[i] > cc) {
  1760. cc = words[i];
  1761. break;
  1762. }
  1763. }
  1764. set_caret_column(cc);
  1765. }
  1766. } else {
  1767. // If we are at the end of the line, move the caret to the next line down.
  1768. if (caret.column == text[caret.line].length()) {
  1769. if (caret.line < text.size() - 1) {
  1770. set_caret_line(get_caret_line() + get_next_visible_line_offset_from(CLAMP(caret.line + 1, 0, text.size() - 1), 1), true, false);
  1771. set_caret_column(0);
  1772. }
  1773. } else {
  1774. if (caret_mid_grapheme_enabled) {
  1775. set_caret_column(get_caret_column() + 1);
  1776. } else {
  1777. set_caret_column(TS->shaped_text_next_grapheme_pos(text.get_line_data(caret.line)->get_rid(), get_caret_column()));
  1778. }
  1779. }
  1780. }
  1781. if (p_select) {
  1782. _post_shift_selection();
  1783. }
  1784. }
  1785. void TextEdit::_move_caret_up(bool p_select) {
  1786. if (p_select) {
  1787. _pre_shift_selection();
  1788. } else {
  1789. deselect();
  1790. }
  1791. int cur_wrap_index = get_caret_wrap_index();
  1792. if (cur_wrap_index > 0) {
  1793. set_caret_line(caret.line, true, false, cur_wrap_index - 1);
  1794. } else if (caret.line == 0) {
  1795. set_caret_column(0);
  1796. } else {
  1797. int new_line = caret.line - get_next_visible_line_offset_from(caret.line - 1, -1);
  1798. if (is_line_wrapped(new_line)) {
  1799. set_caret_line(new_line, true, false, get_line_wrap_count(new_line));
  1800. } else {
  1801. set_caret_line(new_line, true, false);
  1802. }
  1803. }
  1804. if (p_select) {
  1805. _post_shift_selection();
  1806. }
  1807. }
  1808. void TextEdit::_move_caret_down(bool p_select) {
  1809. if (p_select) {
  1810. _pre_shift_selection();
  1811. } else {
  1812. deselect();
  1813. }
  1814. int cur_wrap_index = get_caret_wrap_index();
  1815. if (cur_wrap_index < get_line_wrap_count(caret.line)) {
  1816. set_caret_line(caret.line, true, false, cur_wrap_index + 1);
  1817. } else if (caret.line == get_last_unhidden_line()) {
  1818. set_caret_column(text[caret.line].length());
  1819. } else {
  1820. int new_line = caret.line + get_next_visible_line_offset_from(CLAMP(caret.line + 1, 0, text.size() - 1), 1);
  1821. set_caret_line(new_line, true, false, 0);
  1822. }
  1823. if (p_select) {
  1824. _post_shift_selection();
  1825. }
  1826. }
  1827. void TextEdit::_move_caret_to_line_start(bool p_select) {
  1828. if (p_select) {
  1829. _pre_shift_selection();
  1830. } else {
  1831. deselect();
  1832. }
  1833. // Move caret column to start of wrapped row and then to start of text.
  1834. Vector<String> rows = get_line_wrapped_text(caret.line);
  1835. int wi = get_caret_wrap_index();
  1836. int row_start_col = 0;
  1837. for (int i = 0; i < wi; i++) {
  1838. row_start_col += rows[i].length();
  1839. }
  1840. if (caret.column == row_start_col || wi == 0) {
  1841. // Compute whitespace symbols sequence length.
  1842. int current_line_whitespace_len = 0;
  1843. while (current_line_whitespace_len < text[caret.line].length()) {
  1844. char32_t c = text[caret.line][current_line_whitespace_len];
  1845. if (c != '\t' && c != ' ') {
  1846. break;
  1847. }
  1848. current_line_whitespace_len++;
  1849. }
  1850. if (get_caret_column() == current_line_whitespace_len) {
  1851. set_caret_column(0);
  1852. } else {
  1853. set_caret_column(current_line_whitespace_len);
  1854. }
  1855. } else {
  1856. set_caret_column(row_start_col);
  1857. }
  1858. if (p_select) {
  1859. _post_shift_selection();
  1860. }
  1861. }
  1862. void TextEdit::_move_caret_to_line_end(bool p_select) {
  1863. if (p_select) {
  1864. _pre_shift_selection();
  1865. } else {
  1866. deselect();
  1867. }
  1868. // Move caret column to end of wrapped row and then to end of text.
  1869. Vector<String> rows = get_line_wrapped_text(caret.line);
  1870. int wi = get_caret_wrap_index();
  1871. int row_end_col = -1;
  1872. for (int i = 0; i < wi + 1; i++) {
  1873. row_end_col += rows[i].length();
  1874. }
  1875. if (wi == rows.size() - 1 || caret.column == row_end_col) {
  1876. set_caret_column(text[caret.line].length());
  1877. } else {
  1878. set_caret_column(row_end_col);
  1879. }
  1880. if (p_select) {
  1881. _post_shift_selection();
  1882. }
  1883. }
  1884. void TextEdit::_move_caret_page_up(bool p_select) {
  1885. if (p_select) {
  1886. _pre_shift_selection();
  1887. } else {
  1888. deselect();
  1889. }
  1890. Point2i next_line = get_next_visible_line_index_offset_from(caret.line, get_caret_wrap_index(), -get_visible_line_count());
  1891. int n_line = caret.line - next_line.x + 1;
  1892. set_caret_line(n_line, true, false, next_line.y);
  1893. if (p_select) {
  1894. _post_shift_selection();
  1895. }
  1896. }
  1897. void TextEdit::_move_caret_page_down(bool p_select) {
  1898. if (p_select) {
  1899. _pre_shift_selection();
  1900. } else {
  1901. deselect();
  1902. }
  1903. Point2i next_line = get_next_visible_line_index_offset_from(caret.line, get_caret_wrap_index(), get_visible_line_count());
  1904. int n_line = caret.line + next_line.x - 1;
  1905. set_caret_line(n_line, true, false, next_line.y);
  1906. if (p_select) {
  1907. _post_shift_selection();
  1908. }
  1909. }
  1910. void TextEdit::_do_backspace(bool p_word, bool p_all_to_left) {
  1911. if (!editable) {
  1912. return;
  1913. }
  1914. if (has_selection() || (!p_all_to_left && !p_word)) {
  1915. backspace();
  1916. return;
  1917. }
  1918. if (p_all_to_left) {
  1919. int caret_current_column = caret.column;
  1920. caret.column = 0;
  1921. _remove_text(caret.line, 0, caret.line, caret_current_column);
  1922. return;
  1923. }
  1924. if (p_word) {
  1925. int line = caret.line;
  1926. int column = caret.column;
  1927. PackedInt32Array words = TS->shaped_text_get_word_breaks(text.get_line_data(line)->get_rid());
  1928. for (int i = words.size() - 2; i >= 0; i = i - 2) {
  1929. if (words[i] < column) {
  1930. column = words[i];
  1931. break;
  1932. }
  1933. }
  1934. _remove_text(line, column, caret.line, caret.column);
  1935. set_caret_line(line, false);
  1936. set_caret_column(column);
  1937. return;
  1938. }
  1939. }
  1940. void TextEdit::_delete(bool p_word, bool p_all_to_right) {
  1941. if (!editable) {
  1942. return;
  1943. }
  1944. if (has_selection()) {
  1945. delete_selection();
  1946. return;
  1947. }
  1948. int curline_len = text[caret.line].length();
  1949. if (caret.line == text.size() - 1 && caret.column == curline_len) {
  1950. return; // Last line, last column: Nothing to do.
  1951. }
  1952. int next_line = caret.column < curline_len ? caret.line : caret.line + 1;
  1953. int next_column;
  1954. if (p_all_to_right) {
  1955. // Delete everything to right of caret
  1956. next_column = curline_len;
  1957. next_line = caret.line;
  1958. } else if (p_word && caret.column < curline_len - 1) {
  1959. // Delete next word to right of caret
  1960. int line = caret.line;
  1961. int column = caret.column;
  1962. PackedInt32Array words = TS->shaped_text_get_word_breaks(text.get_line_data(line)->get_rid());
  1963. for (int i = 1; i < words.size(); i = i + 2) {
  1964. if (words[i] > column) {
  1965. column = words[i];
  1966. break;
  1967. }
  1968. }
  1969. next_line = line;
  1970. next_column = column;
  1971. } else {
  1972. // Delete one character
  1973. if (caret_mid_grapheme_enabled) {
  1974. next_column = caret.column < curline_len ? (caret.column + 1) : 0;
  1975. } else {
  1976. next_column = caret.column < curline_len ? TS->shaped_text_next_grapheme_pos(text.get_line_data(caret.line)->get_rid(), (caret.column)) : 0;
  1977. }
  1978. }
  1979. _remove_text(caret.line, caret.column, next_line, next_column);
  1980. update();
  1981. }
  1982. void TextEdit::_move_caret_document_start(bool p_select) {
  1983. if (p_select) {
  1984. _pre_shift_selection();
  1985. } else {
  1986. deselect();
  1987. }
  1988. set_caret_line(0);
  1989. set_caret_column(0);
  1990. if (p_select) {
  1991. _post_shift_selection();
  1992. }
  1993. }
  1994. void TextEdit::_move_caret_document_end(bool p_select) {
  1995. if (p_select) {
  1996. _pre_shift_selection();
  1997. } else {
  1998. deselect();
  1999. }
  2000. set_caret_line(get_last_unhidden_line(), true, false, 9999);
  2001. set_caret_column(text[caret.line].length());
  2002. if (p_select) {
  2003. _post_shift_selection();
  2004. }
  2005. }
  2006. void TextEdit::_update_caches() {
  2007. /* Internal API for CodeEdit. */
  2008. brace_mismatch_color = get_theme_color(SNAME("brace_mismatch_color"), SNAME("CodeEdit"));
  2009. code_folding_color = get_theme_color(SNAME("code_folding_color"), SNAME("CodeEdit"));
  2010. folded_eol_icon = get_theme_icon(SNAME("folded_eol_icon"), SNAME("CodeEdit"));
  2011. /* Search */
  2012. search_result_color = get_theme_color(SNAME("search_result_color"));
  2013. search_result_border_color = get_theme_color(SNAME("search_result_border_color"));
  2014. /* Caret */
  2015. caret_color = get_theme_color(SNAME("caret_color"));
  2016. caret_background_color = get_theme_color(SNAME("caret_background_color"));
  2017. /* Selection */
  2018. font_selected_color = get_theme_color(SNAME("font_selected_color"));
  2019. selection_color = get_theme_color(SNAME("selection_color"));
  2020. /* Visual. */
  2021. style_normal = get_theme_stylebox(SNAME("normal"));
  2022. style_focus = get_theme_stylebox(SNAME("focus"));
  2023. style_readonly = get_theme_stylebox(SNAME("read_only"));
  2024. tab_icon = get_theme_icon(SNAME("tab"));
  2025. space_icon = get_theme_icon(SNAME("space"));
  2026. font = get_theme_font(SNAME("font"));
  2027. font_size = get_theme_font_size(SNAME("font_size"));
  2028. font_color = get_theme_color(SNAME("font_color"));
  2029. font_readonly_color = get_theme_color(SNAME("font_readonly_color"));
  2030. outline_size = get_theme_constant(SNAME("outline_size"));
  2031. outline_color = get_theme_color(SNAME("font_outline_color"));
  2032. line_spacing = get_theme_constant(SNAME("line_spacing"));
  2033. background_color = get_theme_color(SNAME("background_color"));
  2034. current_line_color = get_theme_color(SNAME("current_line_color"));
  2035. word_highlighted_color = get_theme_color(SNAME("word_highlighted_color"));
  2036. /* Text properties. */
  2037. TextServer::Direction dir;
  2038. if (text_direction == Control::TEXT_DIRECTION_INHERITED) {
  2039. dir = is_layout_rtl() ? TextServer::DIRECTION_RTL : TextServer::DIRECTION_LTR;
  2040. } else {
  2041. dir = (TextServer::Direction)text_direction;
  2042. }
  2043. text.set_direction_and_language(dir, (language != "") ? language : TranslationServer::get_singleton()->get_tool_locale());
  2044. text.set_font_features(opentype_features);
  2045. text.set_draw_control_chars(draw_control_chars);
  2046. text.set_font(font);
  2047. text.set_font_size(font_size);
  2048. text.invalidate_all();
  2049. /* Syntax highlighting. */
  2050. if (syntax_highlighter.is_valid()) {
  2051. syntax_highlighter->set_text_edit(this);
  2052. }
  2053. }
  2054. /* General overrides. */
  2055. Size2 TextEdit::get_minimum_size() const {
  2056. return style_normal->get_minimum_size();
  2057. }
  2058. bool TextEdit::is_text_field() const {
  2059. return true;
  2060. }
  2061. Control::CursorShape TextEdit::get_cursor_shape(const Point2 &p_pos) const {
  2062. Point2i pos = get_line_column_at_pos(p_pos);
  2063. int row = pos.y;
  2064. int left_margin = style_normal->get_margin(SIDE_LEFT);
  2065. int gutter = left_margin + gutters_width;
  2066. if (p_pos.x < gutter) {
  2067. for (int i = 0; i < gutters.size(); i++) {
  2068. if (!gutters[i].draw) {
  2069. continue;
  2070. }
  2071. if (p_pos.x > left_margin && p_pos.x <= (left_margin + gutters[i].width) - 3) {
  2072. if (gutters[i].clickable || is_line_gutter_clickable(row, i)) {
  2073. return CURSOR_POINTING_HAND;
  2074. }
  2075. }
  2076. left_margin += gutters[i].width;
  2077. }
  2078. return CURSOR_ARROW;
  2079. }
  2080. int xmargin_end = get_size().width - style_normal->get_margin(SIDE_RIGHT);
  2081. if (draw_minimap && p_pos.x > xmargin_end - minimap_width && p_pos.x <= xmargin_end) {
  2082. return CURSOR_ARROW;
  2083. }
  2084. return get_default_cursor_shape();
  2085. }
  2086. String TextEdit::get_tooltip(const Point2 &p_pos) const {
  2087. Object *tooltip_obj = ObjectDB::get_instance(tooltip_obj_id);
  2088. if (!tooltip_obj) {
  2089. return Control::get_tooltip(p_pos);
  2090. }
  2091. Point2i pos = get_line_column_at_pos(p_pos);
  2092. int row = pos.y;
  2093. int col = pos.x;
  2094. String s = text[row];
  2095. if (s.length() == 0) {
  2096. return Control::get_tooltip(p_pos);
  2097. }
  2098. int beg, end;
  2099. if (select_word(s, col, beg, end)) {
  2100. String tt = tooltip_obj->call(tooltip_func, s.substr(beg, end - beg), tooltip_ud);
  2101. return tt;
  2102. }
  2103. return Control::get_tooltip(p_pos);
  2104. }
  2105. void TextEdit::set_tooltip_request_func(Object *p_obj, const StringName &p_function, const Variant &p_udata) {
  2106. ERR_FAIL_NULL(p_obj);
  2107. tooltip_obj_id = p_obj->get_instance_id();
  2108. tooltip_func = p_function;
  2109. tooltip_ud = p_udata;
  2110. }
  2111. /* Text */
  2112. // Text properties.
  2113. bool TextEdit::has_ime_text() const {
  2114. return !ime_text.is_empty();
  2115. }
  2116. void TextEdit::set_editable(const bool p_editable) {
  2117. if (editable == p_editable) {
  2118. return;
  2119. }
  2120. editable = p_editable;
  2121. update();
  2122. }
  2123. bool TextEdit::is_editable() const {
  2124. return editable;
  2125. }
  2126. void TextEdit::set_text_direction(Control::TextDirection p_text_direction) {
  2127. ERR_FAIL_COND((int)p_text_direction < -1 || (int)p_text_direction > 3);
  2128. if (text_direction != p_text_direction) {
  2129. text_direction = p_text_direction;
  2130. if (text_direction != TEXT_DIRECTION_AUTO && text_direction != TEXT_DIRECTION_INHERITED) {
  2131. input_direction = text_direction;
  2132. }
  2133. TextServer::Direction dir;
  2134. if (text_direction == Control::TEXT_DIRECTION_INHERITED) {
  2135. dir = is_layout_rtl() ? TextServer::DIRECTION_RTL : TextServer::DIRECTION_LTR;
  2136. } else {
  2137. dir = (TextServer::Direction)text_direction;
  2138. }
  2139. text.set_direction_and_language(dir, (language != "") ? language : TranslationServer::get_singleton()->get_tool_locale());
  2140. text.invalidate_all();
  2141. if (menu_dir) {
  2142. menu_dir->set_item_checked(menu_dir->get_item_index(MENU_DIR_INHERITED), text_direction == TEXT_DIRECTION_INHERITED);
  2143. menu_dir->set_item_checked(menu_dir->get_item_index(MENU_DIR_AUTO), text_direction == TEXT_DIRECTION_AUTO);
  2144. menu_dir->set_item_checked(menu_dir->get_item_index(MENU_DIR_LTR), text_direction == TEXT_DIRECTION_LTR);
  2145. menu_dir->set_item_checked(menu_dir->get_item_index(MENU_DIR_RTL), text_direction == TEXT_DIRECTION_RTL);
  2146. }
  2147. update();
  2148. }
  2149. }
  2150. Control::TextDirection TextEdit::get_text_direction() const {
  2151. return text_direction;
  2152. }
  2153. void TextEdit::set_opentype_feature(const String &p_name, int p_value) {
  2154. int32_t tag = TS->name_to_tag(p_name);
  2155. if (!opentype_features.has(tag) || (int)opentype_features[tag] != p_value) {
  2156. opentype_features[tag] = p_value;
  2157. text.set_font_features(opentype_features);
  2158. text.invalidate_all();
  2159. update();
  2160. }
  2161. }
  2162. int TextEdit::get_opentype_feature(const String &p_name) const {
  2163. int32_t tag = TS->name_to_tag(p_name);
  2164. if (!opentype_features.has(tag)) {
  2165. return -1;
  2166. }
  2167. return opentype_features[tag];
  2168. }
  2169. void TextEdit::clear_opentype_features() {
  2170. opentype_features.clear();
  2171. text.set_font_features(opentype_features);
  2172. text.invalidate_all();
  2173. update();
  2174. }
  2175. void TextEdit::set_language(const String &p_language) {
  2176. if (language != p_language) {
  2177. language = p_language;
  2178. TextServer::Direction dir;
  2179. if (text_direction == Control::TEXT_DIRECTION_INHERITED) {
  2180. dir = is_layout_rtl() ? TextServer::DIRECTION_RTL : TextServer::DIRECTION_LTR;
  2181. } else {
  2182. dir = (TextServer::Direction)text_direction;
  2183. }
  2184. text.set_direction_and_language(dir, (language != "") ? language : TranslationServer::get_singleton()->get_tool_locale());
  2185. text.invalidate_all();
  2186. update();
  2187. }
  2188. }
  2189. String TextEdit::get_language() const {
  2190. return language;
  2191. }
  2192. void TextEdit::set_structured_text_bidi_override(Control::StructuredTextParser p_parser) {
  2193. if (st_parser != p_parser) {
  2194. st_parser = p_parser;
  2195. for (int i = 0; i < text.size(); i++) {
  2196. text.set(i, text[i], structured_text_parser(st_parser, st_args, text[i]));
  2197. }
  2198. update();
  2199. }
  2200. }
  2201. Control::StructuredTextParser TextEdit::get_structured_text_bidi_override() const {
  2202. return st_parser;
  2203. }
  2204. void TextEdit::set_structured_text_bidi_override_options(Array p_args) {
  2205. st_args = p_args;
  2206. for (int i = 0; i < text.size(); i++) {
  2207. text.set(i, text[i], structured_text_parser(st_parser, st_args, text[i]));
  2208. }
  2209. update();
  2210. }
  2211. Array TextEdit::get_structured_text_bidi_override_options() const {
  2212. return st_args;
  2213. }
  2214. void TextEdit::set_tab_size(const int p_size) {
  2215. ERR_FAIL_COND_MSG(p_size <= 0, "Tab size must be greater than 0.");
  2216. if (p_size == text.get_tab_size()) {
  2217. return;
  2218. }
  2219. text.set_tab_size(p_size);
  2220. text.invalidate_all_lines();
  2221. update();
  2222. }
  2223. int TextEdit::get_tab_size() const {
  2224. return text.get_tab_size();
  2225. }
  2226. // User controls
  2227. void TextEdit::set_overtype_mode_enabled(const bool p_enabled) {
  2228. overtype_mode = p_enabled;
  2229. update();
  2230. }
  2231. bool TextEdit::is_overtype_mode_enabled() const {
  2232. return overtype_mode;
  2233. }
  2234. void TextEdit::set_context_menu_enabled(bool p_enable) {
  2235. context_menu_enabled = p_enable;
  2236. }
  2237. bool TextEdit::is_context_menu_enabled() const {
  2238. return context_menu_enabled;
  2239. }
  2240. void TextEdit::set_shortcut_keys_enabled(bool p_enabled) {
  2241. shortcut_keys_enabled = p_enabled;
  2242. }
  2243. bool TextEdit::is_shortcut_keys_enabled() const {
  2244. return shortcut_keys_enabled;
  2245. }
  2246. void TextEdit::set_virtual_keyboard_enabled(bool p_enable) {
  2247. virtual_keyboard_enabled = p_enable;
  2248. }
  2249. bool TextEdit::is_virtual_keyboard_enabled() const {
  2250. return virtual_keyboard_enabled;
  2251. }
  2252. // Text manipulation
  2253. void TextEdit::clear() {
  2254. setting_text = true;
  2255. _clear();
  2256. setting_text = false;
  2257. emit_signal(SNAME("text_set"));
  2258. }
  2259. void TextEdit::_clear() {
  2260. clear_undo_history();
  2261. text.clear();
  2262. caret.column = 0;
  2263. caret.line = 0;
  2264. caret.x_ofs = 0;
  2265. caret.line_ofs = 0;
  2266. caret.wrap_ofs = 0;
  2267. caret.last_fit_x = 0;
  2268. selection.active = false;
  2269. }
  2270. void TextEdit::set_text(const String &p_text) {
  2271. setting_text = true;
  2272. if (!undo_enabled) {
  2273. _clear();
  2274. insert_text_at_caret(p_text);
  2275. }
  2276. if (undo_enabled) {
  2277. set_caret_line(0);
  2278. set_caret_column(0);
  2279. begin_complex_operation();
  2280. deselect();
  2281. _remove_text(0, 0, MAX(0, get_line_count() - 1), MAX(get_line(MAX(get_line_count() - 1, 0)).size() - 1, 0));
  2282. insert_text_at_caret(p_text);
  2283. end_complex_operation();
  2284. }
  2285. set_caret_line(0);
  2286. set_caret_column(0);
  2287. update();
  2288. setting_text = false;
  2289. emit_signal(SNAME("text_set"));
  2290. }
  2291. String TextEdit::get_text() const {
  2292. StringBuilder ret_text;
  2293. const int text_size = text.size();
  2294. for (int i = 0; i < text_size; i++) {
  2295. ret_text += text[i];
  2296. if (i != text_size - 1) {
  2297. ret_text += "\n";
  2298. }
  2299. }
  2300. return ret_text.as_string();
  2301. }
  2302. int TextEdit::get_line_count() const {
  2303. return text.size();
  2304. }
  2305. void TextEdit::set_line(int p_line, const String &p_new_text) {
  2306. if (p_line < 0 || p_line >= text.size()) {
  2307. return;
  2308. }
  2309. _remove_text(p_line, 0, p_line, text[p_line].length());
  2310. _insert_text(p_line, 0, p_new_text);
  2311. if (caret.line == p_line) {
  2312. caret.column = MIN(caret.column, p_new_text.length());
  2313. }
  2314. if (has_selection() && p_line == selection.to_line && selection.to_column > text[p_line].length()) {
  2315. selection.to_column = text[p_line].length();
  2316. }
  2317. }
  2318. String TextEdit::get_line(int p_line) const {
  2319. if (p_line < 0 || p_line >= text.size()) {
  2320. return "";
  2321. }
  2322. return text[p_line];
  2323. }
  2324. int TextEdit::get_line_width(int p_line, int p_wrap_index) const {
  2325. ERR_FAIL_INDEX_V(p_line, text.size(), 0);
  2326. ERR_FAIL_COND_V(p_wrap_index > get_line_wrap_count(p_line), 0);
  2327. return text.get_line_width(p_line, p_wrap_index);
  2328. }
  2329. int TextEdit::get_line_height() const {
  2330. return text.get_line_height() + line_spacing;
  2331. }
  2332. int TextEdit::get_indent_level(int p_line) const {
  2333. ERR_FAIL_INDEX_V(p_line, text.size(), 0);
  2334. int tab_count = 0;
  2335. int whitespace_count = 0;
  2336. int line_length = text[p_line].size();
  2337. for (int i = 0; i < line_length - 1; i++) {
  2338. if (text[p_line][i] == '\t') {
  2339. tab_count++;
  2340. } else if (text[p_line][i] == ' ') {
  2341. whitespace_count++;
  2342. } else {
  2343. break;
  2344. }
  2345. }
  2346. return tab_count * text.get_tab_size() + whitespace_count;
  2347. }
  2348. int TextEdit::get_first_non_whitespace_column(int p_line) const {
  2349. ERR_FAIL_INDEX_V(p_line, text.size(), 0);
  2350. int col = 0;
  2351. while (col < text[p_line].length() && _is_whitespace(text[p_line][col])) {
  2352. col++;
  2353. }
  2354. return col;
  2355. }
  2356. void TextEdit::swap_lines(int p_from_line, int p_to_line) {
  2357. ERR_FAIL_INDEX(p_from_line, text.size());
  2358. ERR_FAIL_INDEX(p_to_line, text.size());
  2359. String tmp = get_line(p_from_line);
  2360. String tmp2 = get_line(p_to_line);
  2361. set_line(p_to_line, tmp);
  2362. set_line(p_from_line, tmp2);
  2363. }
  2364. void TextEdit::insert_line_at(int p_at, const String &p_text) {
  2365. ERR_FAIL_INDEX(p_at, text.size());
  2366. _insert_text(p_at, 0, p_text + "\n");
  2367. if (caret.line >= p_at) {
  2368. // offset caret when located after inserted line
  2369. ++caret.line;
  2370. }
  2371. if (has_selection()) {
  2372. if (selection.from_line >= p_at) {
  2373. // offset selection when located after inserted line
  2374. ++selection.from_line;
  2375. ++selection.to_line;
  2376. } else if (selection.to_line >= p_at) {
  2377. // extend selection that includes inserted line
  2378. ++selection.to_line;
  2379. }
  2380. }
  2381. }
  2382. void TextEdit::insert_text_at_caret(const String &p_text) {
  2383. bool had_selection = has_selection();
  2384. if (had_selection) {
  2385. begin_complex_operation();
  2386. }
  2387. delete_selection();
  2388. int new_column, new_line;
  2389. _insert_text(caret.line, caret.column, p_text, &new_line, &new_column);
  2390. _update_scrollbars();
  2391. set_caret_line(new_line, false);
  2392. set_caret_column(new_column);
  2393. update();
  2394. if (had_selection) {
  2395. end_complex_operation();
  2396. }
  2397. }
  2398. void TextEdit::remove_text(int p_from_line, int p_from_column, int p_to_line, int p_to_column) {
  2399. ERR_FAIL_INDEX(p_from_line, text.size());
  2400. ERR_FAIL_INDEX(p_from_column, text[p_from_line].length() + 1);
  2401. ERR_FAIL_INDEX(p_to_line, text.size());
  2402. ERR_FAIL_INDEX(p_to_column, text[p_to_line].length() + 1);
  2403. ERR_FAIL_COND(p_to_line < p_from_line);
  2404. ERR_FAIL_COND(p_to_line == p_from_line && p_to_column < p_from_column);
  2405. _remove_text(p_from_line, p_from_column, p_to_line, p_to_column);
  2406. }
  2407. int TextEdit::get_last_unhidden_line() const {
  2408. // Returns the last line in the text that is not hidden.
  2409. if (!_is_hiding_enabled()) {
  2410. return text.size() - 1;
  2411. }
  2412. int last_line;
  2413. for (last_line = text.size() - 1; last_line > 0; last_line--) {
  2414. if (!_is_line_hidden(last_line)) {
  2415. break;
  2416. }
  2417. }
  2418. return last_line;
  2419. }
  2420. int TextEdit::get_next_visible_line_offset_from(int p_line_from, int p_visible_amount) const {
  2421. // Returns the number of lines (hidden and unhidden) from p_line_from to (p_line_from + visible_amount of unhidden lines).
  2422. ERR_FAIL_INDEX_V(p_line_from, text.size(), ABS(p_visible_amount));
  2423. if (!_is_hiding_enabled()) {
  2424. return ABS(p_visible_amount);
  2425. }
  2426. int num_visible = 0;
  2427. int num_total = 0;
  2428. if (p_visible_amount >= 0) {
  2429. for (int i = p_line_from; i < text.size(); i++) {
  2430. num_total++;
  2431. if (!_is_line_hidden(i)) {
  2432. num_visible++;
  2433. }
  2434. if (num_visible >= p_visible_amount) {
  2435. break;
  2436. }
  2437. }
  2438. } else {
  2439. p_visible_amount = ABS(p_visible_amount);
  2440. for (int i = p_line_from; i >= 0; i--) {
  2441. num_total++;
  2442. if (!_is_line_hidden(i)) {
  2443. num_visible++;
  2444. }
  2445. if (num_visible >= p_visible_amount) {
  2446. break;
  2447. }
  2448. }
  2449. }
  2450. return num_total;
  2451. }
  2452. Point2i TextEdit::get_next_visible_line_index_offset_from(int p_line_from, int p_wrap_index_from, int p_visible_amount) const {
  2453. // Returns the number of lines (hidden and unhidden) from (p_line_from + p_wrap_index_from) row to (p_line_from + visible_amount of unhidden and wrapped rows).
  2454. // Wrap index is set to the wrap index of the last line.
  2455. int wrap_index = 0;
  2456. ERR_FAIL_INDEX_V(p_line_from, text.size(), Point2i(ABS(p_visible_amount), 0));
  2457. if (!_is_hiding_enabled() && get_line_wrapping_mode() == LineWrappingMode::LINE_WRAPPING_NONE) {
  2458. return Point2i(ABS(p_visible_amount), 0);
  2459. }
  2460. int num_visible = 0;
  2461. int num_total = 0;
  2462. if (p_visible_amount == 0) {
  2463. num_total = 0;
  2464. wrap_index = 0;
  2465. } else if (p_visible_amount > 0) {
  2466. int i;
  2467. num_visible -= p_wrap_index_from;
  2468. for (i = p_line_from; i < text.size(); i++) {
  2469. num_total++;
  2470. if (!_is_line_hidden(i)) {
  2471. num_visible++;
  2472. num_visible += get_line_wrap_count(i);
  2473. }
  2474. if (num_visible >= p_visible_amount) {
  2475. break;
  2476. }
  2477. }
  2478. wrap_index = get_line_wrap_count(MIN(i, text.size() - 1)) - MAX(0, num_visible - p_visible_amount);
  2479. } else {
  2480. p_visible_amount = ABS(p_visible_amount);
  2481. int i;
  2482. num_visible -= get_line_wrap_count(p_line_from) - p_wrap_index_from;
  2483. for (i = p_line_from; i >= 0; i--) {
  2484. num_total++;
  2485. if (!_is_line_hidden(i)) {
  2486. num_visible++;
  2487. num_visible += get_line_wrap_count(i);
  2488. }
  2489. if (num_visible >= p_visible_amount) {
  2490. break;
  2491. }
  2492. }
  2493. wrap_index = MAX(0, num_visible - p_visible_amount);
  2494. }
  2495. wrap_index = MAX(wrap_index, 0);
  2496. return Point2i(num_total, wrap_index);
  2497. }
  2498. // Overridable actions
  2499. void TextEdit::handle_unicode_input(const uint32_t p_unicode) {
  2500. if (GDVIRTUAL_CALL(_handle_unicode_input, p_unicode)) {
  2501. return;
  2502. }
  2503. _handle_unicode_input_internal(p_unicode);
  2504. }
  2505. void TextEdit::backspace() {
  2506. if (GDVIRTUAL_CALL(_backspace)) {
  2507. return;
  2508. }
  2509. _backspace_internal();
  2510. }
  2511. void TextEdit::cut() {
  2512. if (GDVIRTUAL_CALL(_cut)) {
  2513. return;
  2514. }
  2515. _cut_internal();
  2516. }
  2517. void TextEdit::copy() {
  2518. if (GDVIRTUAL_CALL(_copy)) {
  2519. return;
  2520. }
  2521. _copy_internal();
  2522. }
  2523. void TextEdit::paste() {
  2524. if (GDVIRTUAL_CALL(_paste)) {
  2525. return;
  2526. }
  2527. _paste_internal();
  2528. }
  2529. // Context menu.
  2530. PopupMenu *TextEdit::get_menu() const {
  2531. const_cast<TextEdit *>(this)->_generate_context_menu();
  2532. return menu;
  2533. }
  2534. bool TextEdit::is_menu_visible() const {
  2535. return menu && menu->is_visible();
  2536. }
  2537. void TextEdit::menu_option(int p_option) {
  2538. switch (p_option) {
  2539. case MENU_CUT: {
  2540. cut();
  2541. } break;
  2542. case MENU_COPY: {
  2543. copy();
  2544. } break;
  2545. case MENU_PASTE: {
  2546. paste();
  2547. } break;
  2548. case MENU_CLEAR: {
  2549. if (editable) {
  2550. clear();
  2551. }
  2552. } break;
  2553. case MENU_SELECT_ALL: {
  2554. select_all();
  2555. } break;
  2556. case MENU_UNDO: {
  2557. undo();
  2558. } break;
  2559. case MENU_REDO: {
  2560. redo();
  2561. } break;
  2562. case MENU_DIR_INHERITED: {
  2563. set_text_direction(TEXT_DIRECTION_INHERITED);
  2564. } break;
  2565. case MENU_DIR_AUTO: {
  2566. set_text_direction(TEXT_DIRECTION_AUTO);
  2567. } break;
  2568. case MENU_DIR_LTR: {
  2569. set_text_direction(TEXT_DIRECTION_LTR);
  2570. } break;
  2571. case MENU_DIR_RTL: {
  2572. set_text_direction(TEXT_DIRECTION_RTL);
  2573. } break;
  2574. case MENU_DISPLAY_UCC: {
  2575. set_draw_control_chars(!get_draw_control_chars());
  2576. } break;
  2577. case MENU_INSERT_LRM: {
  2578. if (editable) {
  2579. insert_text_at_caret(String::chr(0x200E));
  2580. }
  2581. } break;
  2582. case MENU_INSERT_RLM: {
  2583. if (editable) {
  2584. insert_text_at_caret(String::chr(0x200F));
  2585. }
  2586. } break;
  2587. case MENU_INSERT_LRE: {
  2588. if (editable) {
  2589. insert_text_at_caret(String::chr(0x202A));
  2590. }
  2591. } break;
  2592. case MENU_INSERT_RLE: {
  2593. if (editable) {
  2594. insert_text_at_caret(String::chr(0x202B));
  2595. }
  2596. } break;
  2597. case MENU_INSERT_LRO: {
  2598. if (editable) {
  2599. insert_text_at_caret(String::chr(0x202D));
  2600. }
  2601. } break;
  2602. case MENU_INSERT_RLO: {
  2603. if (editable) {
  2604. insert_text_at_caret(String::chr(0x202E));
  2605. }
  2606. } break;
  2607. case MENU_INSERT_PDF: {
  2608. if (editable) {
  2609. insert_text_at_caret(String::chr(0x202C));
  2610. }
  2611. } break;
  2612. case MENU_INSERT_ALM: {
  2613. if (editable) {
  2614. insert_text_at_caret(String::chr(0x061C));
  2615. }
  2616. } break;
  2617. case MENU_INSERT_LRI: {
  2618. if (editable) {
  2619. insert_text_at_caret(String::chr(0x2066));
  2620. }
  2621. } break;
  2622. case MENU_INSERT_RLI: {
  2623. if (editable) {
  2624. insert_text_at_caret(String::chr(0x2067));
  2625. }
  2626. } break;
  2627. case MENU_INSERT_FSI: {
  2628. if (editable) {
  2629. insert_text_at_caret(String::chr(0x2068));
  2630. }
  2631. } break;
  2632. case MENU_INSERT_PDI: {
  2633. if (editable) {
  2634. insert_text_at_caret(String::chr(0x2069));
  2635. }
  2636. } break;
  2637. case MENU_INSERT_ZWJ: {
  2638. if (editable) {
  2639. insert_text_at_caret(String::chr(0x200D));
  2640. }
  2641. } break;
  2642. case MENU_INSERT_ZWNJ: {
  2643. if (editable) {
  2644. insert_text_at_caret(String::chr(0x200C));
  2645. }
  2646. } break;
  2647. case MENU_INSERT_WJ: {
  2648. if (editable) {
  2649. insert_text_at_caret(String::chr(0x2060));
  2650. }
  2651. } break;
  2652. case MENU_INSERT_SHY: {
  2653. if (editable) {
  2654. insert_text_at_caret(String::chr(0x00AD));
  2655. }
  2656. }
  2657. }
  2658. }
  2659. /* Versioning */
  2660. void TextEdit::begin_complex_operation() {
  2661. _push_current_op();
  2662. if (complex_operation_count == 0) {
  2663. next_operation_is_complex = true;
  2664. }
  2665. complex_operation_count++;
  2666. }
  2667. void TextEdit::end_complex_operation() {
  2668. _push_current_op();
  2669. ERR_FAIL_COND(undo_stack.size() == 0);
  2670. complex_operation_count = MAX(complex_operation_count - 1, 0);
  2671. if (complex_operation_count > 0) {
  2672. return;
  2673. }
  2674. if (undo_stack.back()->get().chain_forward) {
  2675. undo_stack.back()->get().chain_forward = false;
  2676. return;
  2677. }
  2678. undo_stack.back()->get().chain_backward = true;
  2679. }
  2680. bool TextEdit::has_undo() const {
  2681. if (undo_stack_pos == nullptr) {
  2682. int pending = current_op.type == TextOperation::TYPE_NONE ? 0 : 1;
  2683. return undo_stack.size() + pending > 0;
  2684. }
  2685. return undo_stack_pos != undo_stack.front();
  2686. }
  2687. bool TextEdit::has_redo() const {
  2688. return undo_stack_pos != nullptr;
  2689. }
  2690. void TextEdit::undo() {
  2691. if (!editable) {
  2692. return;
  2693. }
  2694. _push_current_op();
  2695. if (undo_stack_pos == nullptr) {
  2696. if (!undo_stack.size()) {
  2697. return; // Nothing to undo.
  2698. }
  2699. undo_stack_pos = undo_stack.back();
  2700. } else if (undo_stack_pos == undo_stack.front()) {
  2701. return; // At the bottom of the undo stack.
  2702. } else {
  2703. undo_stack_pos = undo_stack_pos->prev();
  2704. }
  2705. deselect();
  2706. TextOperation op = undo_stack_pos->get();
  2707. _do_text_op(op, true);
  2708. if (op.type != TextOperation::TYPE_INSERT && (op.from_line != op.to_line || op.to_column != op.from_column + 1)) {
  2709. select(op.from_line, op.from_column, op.to_line, op.to_column);
  2710. }
  2711. current_op.version = op.prev_version;
  2712. if (undo_stack_pos->get().chain_backward) {
  2713. while (true) {
  2714. ERR_BREAK(!undo_stack_pos->prev());
  2715. undo_stack_pos = undo_stack_pos->prev();
  2716. op = undo_stack_pos->get();
  2717. _do_text_op(op, true);
  2718. current_op.version = op.prev_version;
  2719. if (undo_stack_pos->get().chain_forward) {
  2720. break;
  2721. }
  2722. }
  2723. }
  2724. _update_scrollbars();
  2725. if (undo_stack_pos->get().type == TextOperation::TYPE_REMOVE) {
  2726. set_caret_line(undo_stack_pos->get().to_line, false);
  2727. set_caret_column(undo_stack_pos->get().to_column);
  2728. } else {
  2729. set_caret_line(undo_stack_pos->get().from_line, false);
  2730. set_caret_column(undo_stack_pos->get().from_column);
  2731. }
  2732. update();
  2733. }
  2734. void TextEdit::redo() {
  2735. if (!editable) {
  2736. return;
  2737. }
  2738. _push_current_op();
  2739. if (undo_stack_pos == nullptr) {
  2740. return; // Nothing to do.
  2741. }
  2742. deselect();
  2743. TextOperation op = undo_stack_pos->get();
  2744. _do_text_op(op, false);
  2745. current_op.version = op.version;
  2746. if (undo_stack_pos->get().chain_forward) {
  2747. while (true) {
  2748. ERR_BREAK(!undo_stack_pos->next());
  2749. undo_stack_pos = undo_stack_pos->next();
  2750. op = undo_stack_pos->get();
  2751. _do_text_op(op, false);
  2752. current_op.version = op.version;
  2753. if (undo_stack_pos->get().chain_backward) {
  2754. break;
  2755. }
  2756. }
  2757. }
  2758. _update_scrollbars();
  2759. set_caret_line(undo_stack_pos->get().to_line, false);
  2760. set_caret_column(undo_stack_pos->get().to_column);
  2761. undo_stack_pos = undo_stack_pos->next();
  2762. update();
  2763. }
  2764. void TextEdit::clear_undo_history() {
  2765. saved_version = 0;
  2766. current_op.type = TextOperation::TYPE_NONE;
  2767. undo_stack_pos = nullptr;
  2768. undo_stack.clear();
  2769. }
  2770. bool TextEdit::is_insert_text_operation() const {
  2771. return (current_op.type == TextOperation::TYPE_INSERT);
  2772. }
  2773. void TextEdit::tag_saved_version() {
  2774. saved_version = get_version();
  2775. }
  2776. uint32_t TextEdit::get_version() const {
  2777. return current_op.version;
  2778. }
  2779. uint32_t TextEdit::get_saved_version() const {
  2780. return saved_version;
  2781. }
  2782. /* Search */
  2783. void TextEdit::set_search_text(const String &p_search_text) {
  2784. search_text = p_search_text;
  2785. }
  2786. void TextEdit::set_search_flags(uint32_t p_flags) {
  2787. search_flags = p_flags;
  2788. }
  2789. Point2i TextEdit::search(const String &p_key, uint32_t p_search_flags, int p_from_line, int p_from_column) const {
  2790. if (p_key.length() == 0) {
  2791. return Point2(-1, -1);
  2792. }
  2793. ERR_FAIL_INDEX_V(p_from_line, text.size(), Point2i(-1, -1));
  2794. ERR_FAIL_INDEX_V(p_from_column, text[p_from_line].length() + 1, Point2i(-1, -1));
  2795. // Search through the whole document, but start by current line.
  2796. int line = p_from_line;
  2797. int pos = -1;
  2798. for (int i = 0; i < text.size() + 1; i++) {
  2799. if (line < 0) {
  2800. line = text.size() - 1;
  2801. }
  2802. if (line == text.size()) {
  2803. line = 0;
  2804. }
  2805. String text_line = text[line];
  2806. int from_column = 0;
  2807. if (line == p_from_line) {
  2808. if (i == text.size()) {
  2809. // Wrapped.
  2810. if (p_search_flags & SEARCH_BACKWARDS) {
  2811. from_column = text_line.length();
  2812. } else {
  2813. from_column = 0;
  2814. }
  2815. } else {
  2816. from_column = p_from_column;
  2817. }
  2818. } else {
  2819. if (p_search_flags & SEARCH_BACKWARDS) {
  2820. from_column = text_line.length() - 1;
  2821. } else {
  2822. from_column = 0;
  2823. }
  2824. }
  2825. pos = -1;
  2826. int pos_from = (p_search_flags & SEARCH_BACKWARDS) ? text_line.length() : 0;
  2827. int last_pos = -1;
  2828. while (true) {
  2829. if (p_search_flags & SEARCH_BACKWARDS) {
  2830. while ((last_pos = (p_search_flags & SEARCH_MATCH_CASE) ? text_line.rfind(p_key, pos_from) : text_line.rfindn(p_key, pos_from)) != -1) {
  2831. if (last_pos <= from_column) {
  2832. pos = last_pos;
  2833. break;
  2834. }
  2835. pos_from = last_pos - p_key.length();
  2836. if (pos_from < 0) {
  2837. break;
  2838. }
  2839. }
  2840. } else {
  2841. while ((last_pos = (p_search_flags & SEARCH_MATCH_CASE) ? text_line.find(p_key, pos_from) : text_line.findn(p_key, pos_from)) != -1) {
  2842. if (last_pos >= from_column) {
  2843. pos = last_pos;
  2844. break;
  2845. }
  2846. pos_from = last_pos + p_key.length();
  2847. }
  2848. }
  2849. bool is_match = true;
  2850. if (pos != -1 && (p_search_flags & SEARCH_WHOLE_WORDS)) {
  2851. // Validate for whole words.
  2852. if (pos > 0 && _is_text_char(text_line[pos - 1])) {
  2853. is_match = false;
  2854. } else if (pos + p_key.length() < text_line.length() && _is_text_char(text_line[pos + p_key.length()])) {
  2855. is_match = false;
  2856. }
  2857. }
  2858. if (pos_from == -1) {
  2859. pos = -1;
  2860. }
  2861. if (is_match || last_pos == -1 || pos == -1) {
  2862. break;
  2863. }
  2864. pos_from = (p_search_flags & SEARCH_BACKWARDS) ? pos - 1 : pos + 1;
  2865. pos = -1;
  2866. }
  2867. if (pos != -1) {
  2868. break;
  2869. }
  2870. if (p_search_flags & SEARCH_BACKWARDS) {
  2871. line--;
  2872. } else {
  2873. line++;
  2874. }
  2875. }
  2876. return (pos == -1) ? Point2i(-1, -1) : Point2i(pos, line);
  2877. }
  2878. /* Mouse */
  2879. Point2 TextEdit::get_local_mouse_pos() const {
  2880. Point2 mp = get_local_mouse_position();
  2881. if (is_layout_rtl()) {
  2882. mp.x = get_size().width - mp.x;
  2883. }
  2884. return mp;
  2885. }
  2886. String TextEdit::get_word_at_pos(const Vector2 &p_pos) const {
  2887. Point2i pos = get_line_column_at_pos(p_pos);
  2888. int row = pos.y;
  2889. int col = pos.x;
  2890. String s = text[row];
  2891. if (s.length() == 0) {
  2892. return "";
  2893. }
  2894. int beg, end;
  2895. if (select_word(s, col, beg, end)) {
  2896. bool inside_quotes = false;
  2897. char32_t selected_quote = '\0';
  2898. int qbegin = 0, qend = 0;
  2899. for (int i = 0; i < s.length(); i++) {
  2900. if (s[i] == '"' || s[i] == '\'') {
  2901. if (i == 0 || s[i - 1] != '\\') {
  2902. if (inside_quotes && selected_quote == s[i]) {
  2903. qend = i;
  2904. inside_quotes = false;
  2905. selected_quote = '\0';
  2906. if (col >= qbegin && col <= qend) {
  2907. return s.substr(qbegin, qend - qbegin);
  2908. }
  2909. } else if (!inside_quotes) {
  2910. qbegin = i + 1;
  2911. inside_quotes = true;
  2912. selected_quote = s[i];
  2913. }
  2914. }
  2915. }
  2916. }
  2917. return s.substr(beg, end - beg);
  2918. }
  2919. return String();
  2920. }
  2921. Point2i TextEdit::get_line_column_at_pos(const Point2i &p_pos) const {
  2922. float rows = p_pos.y;
  2923. rows -= style_normal->get_margin(SIDE_TOP);
  2924. rows /= get_line_height();
  2925. rows += _get_v_scroll_offset();
  2926. int first_vis_line = get_first_visible_line();
  2927. int row = first_vis_line + Math::floor(rows);
  2928. int wrap_index = 0;
  2929. if (get_line_wrapping_mode() != LineWrappingMode::LINE_WRAPPING_NONE || _is_hiding_enabled()) {
  2930. Point2i f_ofs = get_next_visible_line_index_offset_from(first_vis_line, caret.wrap_ofs, rows + (1 * SGN(rows)));
  2931. wrap_index = f_ofs.y;
  2932. if (rows < 0) {
  2933. row = first_vis_line - (f_ofs.x - 1);
  2934. } else {
  2935. row = first_vis_line + (f_ofs.x - 1);
  2936. }
  2937. }
  2938. if (row < 0) {
  2939. row = 0;
  2940. }
  2941. int col = 0;
  2942. if (row >= text.size()) {
  2943. row = text.size() - 1;
  2944. col = text[row].size();
  2945. } else {
  2946. int colx = p_pos.x - (style_normal->get_margin(SIDE_LEFT) + gutters_width + gutter_padding);
  2947. colx += caret.x_ofs;
  2948. col = _get_char_pos_for_line(colx, row, wrap_index);
  2949. if (get_line_wrapping_mode() != LineWrappingMode::LINE_WRAPPING_NONE && wrap_index < get_line_wrap_count(row)) {
  2950. // Move back one if we are at the end of the row.
  2951. Vector<String> rows2 = get_line_wrapped_text(row);
  2952. int row_end_col = 0;
  2953. for (int i = 0; i < wrap_index + 1; i++) {
  2954. row_end_col += rows2[i].length();
  2955. }
  2956. if (col >= row_end_col) {
  2957. col -= 1;
  2958. }
  2959. }
  2960. RID text_rid = text.get_line_data(row)->get_line_rid(wrap_index);
  2961. if (is_layout_rtl()) {
  2962. colx = TS->shaped_text_get_size(text_rid).x - colx;
  2963. }
  2964. col = TS->shaped_text_hit_test_position(text_rid, colx);
  2965. }
  2966. return Point2i(col, row);
  2967. }
  2968. int TextEdit::get_minimap_line_at_pos(const Point2i &p_pos) const {
  2969. float rows = p_pos.y;
  2970. rows -= style_normal->get_margin(SIDE_TOP);
  2971. rows /= (minimap_char_size.y + minimap_line_spacing);
  2972. rows += _get_v_scroll_offset();
  2973. // calculate visible lines
  2974. int minimap_visible_lines = get_minimap_visible_lines();
  2975. int visible_rows = get_visible_line_count() + 1;
  2976. int first_visible_line = get_first_visible_line() - 1;
  2977. int draw_amount = visible_rows + (smooth_scroll_enabled ? 1 : 0);
  2978. draw_amount += get_line_wrap_count(first_visible_line + 1);
  2979. int minimap_line_height = (minimap_char_size.y + minimap_line_spacing);
  2980. // calculate viewport size and y offset
  2981. int viewport_height = (draw_amount - 1) * minimap_line_height;
  2982. int control_height = _get_control_height() - viewport_height;
  2983. int viewport_offset_y = round(get_scroll_pos_for_line(first_visible_line + 1) * control_height) / ((v_scroll->get_max() <= minimap_visible_lines) ? (minimap_visible_lines - draw_amount) : (v_scroll->get_max() - draw_amount));
  2984. // calculate the first line.
  2985. int num_lines_before = round((viewport_offset_y) / minimap_line_height);
  2986. int minimap_line = (v_scroll->get_max() <= minimap_visible_lines) ? -1 : first_visible_line;
  2987. if (first_visible_line > 0 && minimap_line >= 0) {
  2988. minimap_line -= get_next_visible_line_index_offset_from(first_visible_line, 0, -num_lines_before).x;
  2989. minimap_line -= (minimap_line > 0 && smooth_scroll_enabled ? 1 : 0);
  2990. } else {
  2991. minimap_line = 0;
  2992. }
  2993. int row = minimap_line + Math::floor(rows);
  2994. if (get_line_wrapping_mode() != LineWrappingMode::LINE_WRAPPING_NONE || _is_hiding_enabled()) {
  2995. int f_ofs = get_next_visible_line_index_offset_from(minimap_line, caret.wrap_ofs, rows + (1 * SGN(rows))).x - 1;
  2996. if (rows < 0) {
  2997. row = minimap_line - f_ofs;
  2998. } else {
  2999. row = minimap_line + f_ofs;
  3000. }
  3001. }
  3002. if (row < 0) {
  3003. row = 0;
  3004. }
  3005. if (row >= text.size()) {
  3006. row = text.size() - 1;
  3007. }
  3008. return row;
  3009. }
  3010. bool TextEdit::is_dragging_cursor() const {
  3011. return dragging_selection || dragging_minimap;
  3012. }
  3013. /* Caret */
  3014. void TextEdit::set_caret_type(CaretType p_type) {
  3015. caret_type = p_type;
  3016. update();
  3017. }
  3018. TextEdit::CaretType TextEdit::get_caret_type() const {
  3019. return caret_type;
  3020. }
  3021. void TextEdit::set_caret_blink_enabled(const bool p_enabled) {
  3022. caret_blink_enabled = p_enabled;
  3023. if (has_focus()) {
  3024. if (p_enabled) {
  3025. caret_blink_timer->start();
  3026. } else {
  3027. caret_blink_timer->stop();
  3028. }
  3029. }
  3030. draw_caret = true;
  3031. }
  3032. bool TextEdit::is_caret_blink_enabled() const {
  3033. return caret_blink_enabled;
  3034. }
  3035. float TextEdit::get_caret_blink_speed() const {
  3036. return caret_blink_timer->get_wait_time();
  3037. }
  3038. void TextEdit::set_caret_blink_speed(const float p_speed) {
  3039. ERR_FAIL_COND(p_speed <= 0);
  3040. caret_blink_timer->set_wait_time(p_speed);
  3041. }
  3042. void TextEdit::set_move_caret_on_right_click_enabled(const bool p_enable) {
  3043. move_caret_on_right_click = p_enable;
  3044. }
  3045. bool TextEdit::is_move_caret_on_right_click_enabled() const {
  3046. return move_caret_on_right_click;
  3047. }
  3048. void TextEdit::set_caret_mid_grapheme_enabled(const bool p_enabled) {
  3049. caret_mid_grapheme_enabled = p_enabled;
  3050. }
  3051. bool TextEdit::is_caret_mid_grapheme_enabled() const {
  3052. return caret_mid_grapheme_enabled;
  3053. }
  3054. bool TextEdit::is_caret_visible() const {
  3055. return caret.visible;
  3056. }
  3057. Point2 TextEdit::get_caret_draw_pos() const {
  3058. return caret.draw_pos;
  3059. }
  3060. void TextEdit::set_caret_line(int p_line, bool p_adjust_viewport, bool p_can_be_hidden, int p_wrap_index) {
  3061. if (setting_caret_line) {
  3062. return;
  3063. }
  3064. setting_caret_line = true;
  3065. if (p_line < 0) {
  3066. p_line = 0;
  3067. }
  3068. if (p_line >= text.size()) {
  3069. p_line = text.size() - 1;
  3070. }
  3071. if (!p_can_be_hidden) {
  3072. if (_is_line_hidden(CLAMP(p_line, 0, text.size() - 1))) {
  3073. int move_down = get_next_visible_line_offset_from(p_line, 1) - 1;
  3074. if (p_line + move_down <= text.size() - 1 && !_is_line_hidden(p_line + move_down)) {
  3075. p_line += move_down;
  3076. } else {
  3077. int move_up = get_next_visible_line_offset_from(p_line, -1) - 1;
  3078. if (p_line - move_up > 0 && !_is_line_hidden(p_line - move_up)) {
  3079. p_line -= move_up;
  3080. } else {
  3081. WARN_PRINT(("Caret set to hidden line " + itos(p_line) + " and there are no nonhidden lines."));
  3082. }
  3083. }
  3084. }
  3085. }
  3086. caret.line = p_line;
  3087. int n_col = _get_char_pos_for_line(caret.last_fit_x, p_line, p_wrap_index);
  3088. if (n_col != 0 && get_line_wrapping_mode() != LineWrappingMode::LINE_WRAPPING_NONE && p_wrap_index < get_line_wrap_count(p_line)) {
  3089. Vector<String> rows = get_line_wrapped_text(p_line);
  3090. int row_end_col = 0;
  3091. for (int i = 0; i < p_wrap_index + 1; i++) {
  3092. row_end_col += rows[i].length();
  3093. }
  3094. if (n_col >= row_end_col) {
  3095. n_col -= 1;
  3096. }
  3097. }
  3098. caret.column = n_col;
  3099. if (p_adjust_viewport) {
  3100. adjust_viewport_to_caret();
  3101. }
  3102. setting_caret_line = false;
  3103. if (!caret_pos_dirty) {
  3104. if (is_inside_tree()) {
  3105. MessageQueue::get_singleton()->push_call(this, "_emit_caret_changed");
  3106. }
  3107. caret_pos_dirty = true;
  3108. }
  3109. }
  3110. int TextEdit::get_caret_line() const {
  3111. return caret.line;
  3112. }
  3113. void TextEdit::set_caret_column(int p_col, bool p_adjust_viewport) {
  3114. if (p_col < 0) {
  3115. p_col = 0;
  3116. }
  3117. caret.column = p_col;
  3118. if (caret.column > get_line(caret.line).length()) {
  3119. caret.column = get_line(caret.line).length();
  3120. }
  3121. caret.last_fit_x = _get_column_x_offset_for_line(caret.column, caret.line);
  3122. if (p_adjust_viewport) {
  3123. adjust_viewport_to_caret();
  3124. }
  3125. if (!caret_pos_dirty) {
  3126. if (is_inside_tree()) {
  3127. MessageQueue::get_singleton()->push_call(this, "_emit_caret_changed");
  3128. }
  3129. caret_pos_dirty = true;
  3130. }
  3131. }
  3132. int TextEdit::get_caret_column() const {
  3133. return caret.column;
  3134. }
  3135. int TextEdit::get_caret_wrap_index() const {
  3136. return get_line_wrap_index_at_column(caret.line, caret.column);
  3137. }
  3138. String TextEdit::get_word_under_caret() const {
  3139. PackedInt32Array words = TS->shaped_text_get_word_breaks(text.get_line_data(caret.line)->get_rid());
  3140. for (int i = 0; i < words.size(); i = i + 2) {
  3141. if (words[i] <= caret.column && words[i + 1] > caret.column) {
  3142. return text[caret.line].substr(words[i], words[i + 1] - words[i]);
  3143. }
  3144. }
  3145. return "";
  3146. }
  3147. /* Selection. */
  3148. void TextEdit::set_selecting_enabled(const bool p_enabled) {
  3149. selecting_enabled = p_enabled;
  3150. if (!selecting_enabled) {
  3151. deselect();
  3152. }
  3153. }
  3154. bool TextEdit::is_selecting_enabled() const {
  3155. return selecting_enabled;
  3156. }
  3157. void TextEdit::set_override_selected_font_color(bool p_override_selected_font_color) {
  3158. override_selected_font_color = p_override_selected_font_color;
  3159. }
  3160. bool TextEdit::is_overriding_selected_font_color() const {
  3161. return override_selected_font_color;
  3162. }
  3163. void TextEdit::set_selection_mode(SelectionMode p_mode, int p_line, int p_column) {
  3164. selection.selecting_mode = p_mode;
  3165. if (p_line >= 0) {
  3166. ERR_FAIL_INDEX(p_line, text.size());
  3167. selection.selecting_line = p_line;
  3168. }
  3169. if (p_column >= 0) {
  3170. ERR_FAIL_INDEX(p_column, text[selection.selecting_line].length());
  3171. selection.selecting_column = p_column;
  3172. }
  3173. }
  3174. TextEdit::SelectionMode TextEdit::get_selection_mode() const {
  3175. return selection.selecting_mode;
  3176. }
  3177. void TextEdit::select_all() {
  3178. if (!selecting_enabled) {
  3179. return;
  3180. }
  3181. if (text.size() == 1 && text[0].length() == 0) {
  3182. return;
  3183. }
  3184. selection.active = true;
  3185. selection.from_line = 0;
  3186. selection.from_column = 0;
  3187. selection.selecting_line = 0;
  3188. selection.selecting_column = 0;
  3189. selection.to_line = text.size() - 1;
  3190. selection.to_column = text[selection.to_line].length();
  3191. selection.selecting_mode = SelectionMode::SELECTION_MODE_SHIFT;
  3192. selection.shiftclick_left = true;
  3193. set_caret_line(selection.to_line, false);
  3194. set_caret_column(selection.to_column, false);
  3195. update();
  3196. }
  3197. void TextEdit::select_word_under_caret() {
  3198. if (!selecting_enabled) {
  3199. return;
  3200. }
  3201. if (text.size() == 1 && text[0].length() == 0) {
  3202. return;
  3203. }
  3204. if (selection.active) {
  3205. /* Allow toggling selection by pressing the shortcut a second time. */
  3206. /* This is also usable as a general-purpose "deselect" shortcut after */
  3207. /* selecting anything. */
  3208. deselect();
  3209. return;
  3210. }
  3211. int begin = 0;
  3212. int end = 0;
  3213. const PackedInt32Array words = TS->shaped_text_get_word_breaks(text.get_line_data(caret.line)->get_rid());
  3214. for (int i = 0; i < words.size(); i = i + 2) {
  3215. if ((words[i] < caret.column && words[i + 1] > caret.column) || (i == words.size() - 2 && caret.column == words[i + 1])) {
  3216. begin = words[i];
  3217. end = words[i + 1];
  3218. break;
  3219. }
  3220. }
  3221. select(caret.line, begin, caret.line, end);
  3222. /* Move the caret to the end of the word for easier editing. */
  3223. set_caret_column(end, false);
  3224. }
  3225. void TextEdit::select(int p_from_line, int p_from_column, int p_to_line, int p_to_column) {
  3226. if (!selecting_enabled) {
  3227. return;
  3228. }
  3229. if (p_from_line < 0) {
  3230. p_from_line = 0;
  3231. } else if (p_from_line >= text.size()) {
  3232. p_from_line = text.size() - 1;
  3233. }
  3234. if (p_from_column >= text[p_from_line].length()) {
  3235. p_from_column = text[p_from_line].length();
  3236. }
  3237. if (p_from_column < 0) {
  3238. p_from_column = 0;
  3239. }
  3240. if (p_to_line < 0) {
  3241. p_to_line = 0;
  3242. } else if (p_to_line >= text.size()) {
  3243. p_to_line = text.size() - 1;
  3244. }
  3245. if (p_to_column >= text[p_to_line].length()) {
  3246. p_to_column = text[p_to_line].length();
  3247. }
  3248. if (p_to_column < 0) {
  3249. p_to_column = 0;
  3250. }
  3251. selection.from_line = p_from_line;
  3252. selection.from_column = p_from_column;
  3253. selection.to_line = p_to_line;
  3254. selection.to_column = p_to_column;
  3255. selection.active = true;
  3256. if (selection.from_line == selection.to_line) {
  3257. if (selection.from_column == selection.to_column) {
  3258. selection.active = false;
  3259. } else if (selection.from_column > selection.to_column) {
  3260. selection.shiftclick_left = false;
  3261. SWAP(selection.from_column, selection.to_column);
  3262. } else {
  3263. selection.shiftclick_left = true;
  3264. }
  3265. } else if (selection.from_line > selection.to_line) {
  3266. selection.shiftclick_left = false;
  3267. SWAP(selection.from_line, selection.to_line);
  3268. SWAP(selection.from_column, selection.to_column);
  3269. } else {
  3270. selection.shiftclick_left = true;
  3271. }
  3272. update();
  3273. }
  3274. bool TextEdit::has_selection() const {
  3275. return selection.active;
  3276. }
  3277. String TextEdit::get_selected_text() const {
  3278. if (!selection.active) {
  3279. return "";
  3280. }
  3281. return _base_get_text(selection.from_line, selection.from_column, selection.to_line, selection.to_column);
  3282. }
  3283. int TextEdit::get_selection_line() const {
  3284. return selection.selecting_line;
  3285. }
  3286. int TextEdit::get_selection_column() const {
  3287. return selection.selecting_column;
  3288. }
  3289. int TextEdit::get_selection_from_line() const {
  3290. ERR_FAIL_COND_V(!selection.active, -1);
  3291. return selection.from_line;
  3292. }
  3293. int TextEdit::get_selection_from_column() const {
  3294. ERR_FAIL_COND_V(!selection.active, -1);
  3295. return selection.from_column;
  3296. }
  3297. int TextEdit::get_selection_to_line() const {
  3298. ERR_FAIL_COND_V(!selection.active, -1);
  3299. return selection.to_line;
  3300. }
  3301. int TextEdit::get_selection_to_column() const {
  3302. ERR_FAIL_COND_V(!selection.active, -1);
  3303. return selection.to_column;
  3304. }
  3305. void TextEdit::deselect() {
  3306. selection.active = false;
  3307. update();
  3308. }
  3309. void TextEdit::delete_selection() {
  3310. if (!has_selection()) {
  3311. return;
  3312. }
  3313. selection.active = false;
  3314. selection.selecting_mode = SelectionMode::SELECTION_MODE_NONE;
  3315. _remove_text(selection.from_line, selection.from_column, selection.to_line, selection.to_column);
  3316. set_caret_line(selection.from_line, false, false);
  3317. set_caret_column(selection.from_column);
  3318. update();
  3319. }
  3320. /* line wrapping. */
  3321. void TextEdit::set_line_wrapping_mode(LineWrappingMode p_wrapping_mode) {
  3322. if (line_wrapping_mode != p_wrapping_mode) {
  3323. line_wrapping_mode = p_wrapping_mode;
  3324. _update_wrap_at_column(true);
  3325. }
  3326. }
  3327. TextEdit::LineWrappingMode TextEdit::get_line_wrapping_mode() const {
  3328. return line_wrapping_mode;
  3329. }
  3330. bool TextEdit::is_line_wrapped(int p_line) const {
  3331. ERR_FAIL_INDEX_V(p_line, text.size(), 0);
  3332. if (get_line_wrapping_mode() == LineWrappingMode::LINE_WRAPPING_NONE) {
  3333. return false;
  3334. }
  3335. return text.get_line_wrap_amount(p_line) > 0;
  3336. }
  3337. int TextEdit::get_line_wrap_count(int p_line) const {
  3338. ERR_FAIL_INDEX_V(p_line, text.size(), 0);
  3339. if (!is_line_wrapped(p_line)) {
  3340. return 0;
  3341. }
  3342. return text.get_line_wrap_amount(p_line);
  3343. }
  3344. int TextEdit::get_line_wrap_index_at_column(int p_line, int p_column) const {
  3345. ERR_FAIL_INDEX_V(p_line, text.size(), 0);
  3346. ERR_FAIL_COND_V(p_column < 0, 0);
  3347. ERR_FAIL_COND_V(p_column > text[p_line].length(), 0);
  3348. if (!is_line_wrapped(p_line)) {
  3349. return 0;
  3350. }
  3351. /* Loop through wraps in the line text until we get to the column. */
  3352. int wrap_index = 0;
  3353. int col = 0;
  3354. Vector<String> lines = get_line_wrapped_text(p_line);
  3355. for (int i = 0; i < lines.size(); i++) {
  3356. wrap_index = i;
  3357. String s = lines[wrap_index];
  3358. col += s.length();
  3359. if (col > p_column) {
  3360. break;
  3361. }
  3362. }
  3363. return wrap_index;
  3364. }
  3365. Vector<String> TextEdit::get_line_wrapped_text(int p_line) const {
  3366. ERR_FAIL_INDEX_V(p_line, text.size(), Vector<String>());
  3367. Vector<String> lines;
  3368. if (!is_line_wrapped(p_line)) {
  3369. lines.push_back(text[p_line]);
  3370. return lines;
  3371. }
  3372. const String &line_text = text[p_line];
  3373. Vector<Vector2i> line_ranges = text.get_line_wrap_ranges(p_line);
  3374. for (int i = 0; i < line_ranges.size(); i++) {
  3375. lines.push_back(line_text.substr(line_ranges[i].x, line_ranges[i].y - line_ranges[i].x));
  3376. }
  3377. return lines;
  3378. }
  3379. /* Viewport */
  3380. // Scrolling.
  3381. void TextEdit::set_smooth_scroll_enabled(const bool p_enable) {
  3382. v_scroll->set_smooth_scroll_enabled(p_enable);
  3383. smooth_scroll_enabled = p_enable;
  3384. }
  3385. bool TextEdit::is_smooth_scroll_enabled() const {
  3386. return smooth_scroll_enabled;
  3387. }
  3388. void TextEdit::set_scroll_past_end_of_file_enabled(const bool p_enabled) {
  3389. scroll_past_end_of_file_enabled = p_enabled;
  3390. update();
  3391. }
  3392. bool TextEdit::is_scroll_past_end_of_file_enabled() const {
  3393. return scroll_past_end_of_file_enabled;
  3394. }
  3395. void TextEdit::set_v_scroll(double p_scroll) {
  3396. v_scroll->set_value(p_scroll);
  3397. int max_v_scroll = v_scroll->get_max() - v_scroll->get_page();
  3398. if (p_scroll >= max_v_scroll - 1.0) {
  3399. _scroll_moved(v_scroll->get_value());
  3400. }
  3401. }
  3402. double TextEdit::get_v_scroll() const {
  3403. return v_scroll->get_value();
  3404. }
  3405. void TextEdit::set_h_scroll(int p_scroll) {
  3406. if (p_scroll < 0) {
  3407. p_scroll = 0;
  3408. }
  3409. h_scroll->set_value(p_scroll);
  3410. }
  3411. int TextEdit::get_h_scroll() const {
  3412. return h_scroll->get_value();
  3413. }
  3414. void TextEdit::set_v_scroll_speed(float p_speed) {
  3415. v_scroll_speed = p_speed;
  3416. }
  3417. float TextEdit::get_v_scroll_speed() const {
  3418. return v_scroll_speed;
  3419. }
  3420. double TextEdit::get_scroll_pos_for_line(int p_line, int p_wrap_index) const {
  3421. ERR_FAIL_INDEX_V(p_line, text.size(), 0);
  3422. ERR_FAIL_COND_V(p_wrap_index < 0, 0);
  3423. ERR_FAIL_COND_V(p_wrap_index > get_line_wrap_count(p_line), 0);
  3424. if (get_line_wrapping_mode() == LineWrappingMode::LINE_WRAPPING_NONE && !_is_hiding_enabled()) {
  3425. return p_line;
  3426. }
  3427. // Count the number of visible lines up to this line.
  3428. double new_line_scroll_pos = 0.0;
  3429. int to = CLAMP(p_line, 0, text.size() - 1);
  3430. for (int i = 0; i < to; i++) {
  3431. if (!text.is_hidden(i)) {
  3432. new_line_scroll_pos++;
  3433. new_line_scroll_pos += get_line_wrap_count(i);
  3434. }
  3435. }
  3436. new_line_scroll_pos += p_wrap_index;
  3437. return new_line_scroll_pos;
  3438. }
  3439. // Visible lines.
  3440. void TextEdit::set_line_as_first_visible(int p_line, int p_wrap_index) {
  3441. ERR_FAIL_INDEX(p_line, text.size());
  3442. ERR_FAIL_COND(p_wrap_index < 0);
  3443. ERR_FAIL_COND(p_wrap_index > get_line_wrap_count(p_line));
  3444. set_v_scroll(get_scroll_pos_for_line(p_line, p_wrap_index));
  3445. }
  3446. int TextEdit::get_first_visible_line() const {
  3447. return CLAMP(caret.line_ofs, 0, text.size() - 1);
  3448. }
  3449. void TextEdit::set_line_as_center_visible(int p_line, int p_wrap_index) {
  3450. ERR_FAIL_INDEX(p_line, text.size());
  3451. ERR_FAIL_COND(p_wrap_index < 0);
  3452. ERR_FAIL_COND(p_wrap_index > get_line_wrap_count(p_line));
  3453. int visible_rows = get_visible_line_count();
  3454. Point2i next_line = get_next_visible_line_index_offset_from(p_line, p_wrap_index, -visible_rows / 2);
  3455. int first_line = p_line - next_line.x + 1;
  3456. set_v_scroll(get_scroll_pos_for_line(first_line, next_line.y));
  3457. }
  3458. void TextEdit::set_line_as_last_visible(int p_line, int p_wrap_index) {
  3459. ERR_FAIL_INDEX(p_line, text.size());
  3460. ERR_FAIL_COND(p_wrap_index < 0);
  3461. ERR_FAIL_COND(p_wrap_index > get_line_wrap_count(p_line));
  3462. Point2i next_line = get_next_visible_line_index_offset_from(p_line, p_wrap_index, -get_visible_line_count() - 1);
  3463. int first_line = p_line - next_line.x + 1;
  3464. set_v_scroll(get_scroll_pos_for_line(first_line, next_line.y) + _get_visible_lines_offset());
  3465. }
  3466. int TextEdit::get_last_full_visible_line() const {
  3467. int first_vis_line = get_first_visible_line();
  3468. int last_vis_line = 0;
  3469. last_vis_line = first_vis_line + get_next_visible_line_index_offset_from(first_vis_line, caret.wrap_ofs, get_visible_line_count()).x - 1;
  3470. last_vis_line = CLAMP(last_vis_line, 0, text.size() - 1);
  3471. return last_vis_line;
  3472. }
  3473. int TextEdit::get_last_full_visible_line_wrap_index() const {
  3474. int first_vis_line = get_first_visible_line();
  3475. return get_next_visible_line_index_offset_from(first_vis_line, caret.wrap_ofs, get_visible_line_count()).y;
  3476. }
  3477. int TextEdit::get_visible_line_count() const {
  3478. return _get_control_height() / get_line_height();
  3479. }
  3480. int TextEdit::get_total_visible_line_count() const {
  3481. /* Returns the total number of (lines + wraped - hidden). */
  3482. if (!_is_hiding_enabled() && get_line_wrapping_mode() == LineWrappingMode::LINE_WRAPPING_NONE) {
  3483. return text.size();
  3484. }
  3485. int total_rows = 0;
  3486. for (int i = 0; i < text.size(); i++) {
  3487. if (!text.is_hidden(i)) {
  3488. total_rows++;
  3489. total_rows += get_line_wrap_count(i);
  3490. }
  3491. }
  3492. return total_rows;
  3493. }
  3494. // Auto adjust
  3495. void TextEdit::adjust_viewport_to_caret() {
  3496. // Make sure Caret is visible on the screen.
  3497. scrolling = false;
  3498. minimap_clicked = false;
  3499. int cur_line = caret.line;
  3500. int cur_wrap = get_caret_wrap_index();
  3501. int first_vis_line = get_first_visible_line();
  3502. int first_vis_wrap = caret.wrap_ofs;
  3503. int last_vis_line = get_last_full_visible_line();
  3504. int last_vis_wrap = get_last_full_visible_line_wrap_index();
  3505. if (cur_line < first_vis_line || (cur_line == first_vis_line && cur_wrap < first_vis_wrap)) {
  3506. // Caret is above screen.
  3507. set_line_as_first_visible(cur_line, cur_wrap);
  3508. } else if (cur_line > last_vis_line || (cur_line == last_vis_line && cur_wrap > last_vis_wrap)) {
  3509. // Caret is below screen.
  3510. set_line_as_last_visible(cur_line, cur_wrap);
  3511. }
  3512. int visible_width = get_size().width - style_normal->get_minimum_size().width - gutters_width - gutter_padding;
  3513. if (draw_minimap) {
  3514. visible_width -= minimap_width;
  3515. }
  3516. if (v_scroll->is_visible_in_tree()) {
  3517. visible_width -= v_scroll->get_combined_minimum_size().width;
  3518. }
  3519. visible_width -= 20; // Give it a little more space.
  3520. if (get_line_wrapping_mode() == LineWrappingMode::LINE_WRAPPING_NONE) {
  3521. // Adjust x offset.
  3522. Vector2i caret_pos;
  3523. // Get position of the start of caret.
  3524. if (ime_text.length() != 0 && ime_selection.x != 0) {
  3525. caret_pos.x = _get_column_x_offset_for_line(caret.column + ime_selection.x, caret.line);
  3526. } else {
  3527. caret_pos.x = _get_column_x_offset_for_line(caret.column, caret.line);
  3528. }
  3529. // Get position of the end of caret.
  3530. if (ime_text.length() != 0) {
  3531. if (ime_selection.y != 0) {
  3532. caret_pos.y = _get_column_x_offset_for_line(caret.column + ime_selection.x + ime_selection.y, caret.line);
  3533. } else {
  3534. caret_pos.y = _get_column_x_offset_for_line(caret.column + ime_text.size(), caret.line);
  3535. }
  3536. } else {
  3537. caret_pos.y = caret_pos.x;
  3538. }
  3539. if (MAX(caret_pos.x, caret_pos.y) > (caret.x_ofs + visible_width)) {
  3540. caret.x_ofs = MAX(caret_pos.x, caret_pos.y) - visible_width + 1;
  3541. }
  3542. if (MIN(caret_pos.x, caret_pos.y) < caret.x_ofs) {
  3543. caret.x_ofs = MIN(caret_pos.x, caret_pos.y);
  3544. }
  3545. } else {
  3546. caret.x_ofs = 0;
  3547. }
  3548. h_scroll->set_value(caret.x_ofs);
  3549. update();
  3550. }
  3551. void TextEdit::center_viewport_to_caret() {
  3552. // Move viewport so the caret is in the center of the screen.
  3553. scrolling = false;
  3554. minimap_clicked = false;
  3555. set_line_as_center_visible(caret.line, get_caret_wrap_index());
  3556. int visible_width = get_size().width - style_normal->get_minimum_size().width - gutters_width - gutter_padding;
  3557. if (draw_minimap) {
  3558. visible_width -= minimap_width;
  3559. }
  3560. if (v_scroll->is_visible_in_tree()) {
  3561. visible_width -= v_scroll->get_combined_minimum_size().width;
  3562. }
  3563. visible_width -= 20; // Give it a little more space.
  3564. if (get_line_wrapping_mode() != LineWrappingMode::LINE_WRAPPING_NONE) {
  3565. // Center x offset.
  3566. Vector2i caret_pos;
  3567. // Get position of the start of caret.
  3568. if (ime_text.length() != 0 && ime_selection.x != 0) {
  3569. caret_pos.x = _get_column_x_offset_for_line(caret.column + ime_selection.x, caret.line);
  3570. } else {
  3571. caret_pos.x = _get_column_x_offset_for_line(caret.column, caret.line);
  3572. }
  3573. // Get position of the end of caret.
  3574. if (ime_text.length() != 0) {
  3575. if (ime_selection.y != 0) {
  3576. caret_pos.y = _get_column_x_offset_for_line(caret.column + ime_selection.x + ime_selection.y, caret.line);
  3577. } else {
  3578. caret_pos.y = _get_column_x_offset_for_line(caret.column + ime_text.size(), caret.line);
  3579. }
  3580. } else {
  3581. caret_pos.y = caret_pos.x;
  3582. }
  3583. if (MAX(caret_pos.x, caret_pos.y) > (caret.x_ofs + visible_width)) {
  3584. caret.x_ofs = MAX(caret_pos.x, caret_pos.y) - visible_width + 1;
  3585. }
  3586. if (MIN(caret_pos.x, caret_pos.y) < caret.x_ofs) {
  3587. caret.x_ofs = MIN(caret_pos.x, caret_pos.y);
  3588. }
  3589. } else {
  3590. caret.x_ofs = 0;
  3591. }
  3592. h_scroll->set_value(caret.x_ofs);
  3593. update();
  3594. }
  3595. /* Minimap */
  3596. void TextEdit::set_draw_minimap(bool p_draw) {
  3597. if (draw_minimap != p_draw) {
  3598. draw_minimap = p_draw;
  3599. _update_wrap_at_column();
  3600. }
  3601. update();
  3602. }
  3603. bool TextEdit::is_drawing_minimap() const {
  3604. return draw_minimap;
  3605. }
  3606. void TextEdit::set_minimap_width(int p_minimap_width) {
  3607. if (minimap_width != p_minimap_width) {
  3608. minimap_width = p_minimap_width;
  3609. _update_wrap_at_column();
  3610. }
  3611. update();
  3612. }
  3613. int TextEdit::get_minimap_width() const {
  3614. return minimap_width;
  3615. }
  3616. int TextEdit::get_minimap_visible_lines() const {
  3617. return _get_control_height() / (minimap_char_size.y + minimap_line_spacing);
  3618. }
  3619. /* Gutters. */
  3620. void TextEdit::add_gutter(int p_at) {
  3621. if (p_at < 0 || p_at > gutters.size()) {
  3622. gutters.push_back(GutterInfo());
  3623. } else {
  3624. gutters.insert(p_at, GutterInfo());
  3625. }
  3626. for (int i = 0; i < text.size() + 1; i++) {
  3627. text.add_gutter(p_at);
  3628. }
  3629. emit_signal(SNAME("gutter_added"));
  3630. update();
  3631. }
  3632. void TextEdit::remove_gutter(int p_gutter) {
  3633. ERR_FAIL_INDEX(p_gutter, gutters.size());
  3634. gutters.remove(p_gutter);
  3635. for (int i = 0; i < text.size() + 1; i++) {
  3636. text.remove_gutter(p_gutter);
  3637. }
  3638. emit_signal(SNAME("gutter_removed"));
  3639. update();
  3640. }
  3641. int TextEdit::get_gutter_count() const {
  3642. return gutters.size();
  3643. }
  3644. void TextEdit::set_gutter_name(int p_gutter, const String &p_name) {
  3645. ERR_FAIL_INDEX(p_gutter, gutters.size());
  3646. gutters.write[p_gutter].name = p_name;
  3647. }
  3648. String TextEdit::get_gutter_name(int p_gutter) const {
  3649. ERR_FAIL_INDEX_V(p_gutter, gutters.size(), "");
  3650. return gutters[p_gutter].name;
  3651. }
  3652. void TextEdit::set_gutter_type(int p_gutter, GutterType p_type) {
  3653. ERR_FAIL_INDEX(p_gutter, gutters.size());
  3654. gutters.write[p_gutter].type = p_type;
  3655. update();
  3656. }
  3657. TextEdit::GutterType TextEdit::get_gutter_type(int p_gutter) const {
  3658. ERR_FAIL_INDEX_V(p_gutter, gutters.size(), GUTTER_TYPE_STRING);
  3659. return gutters[p_gutter].type;
  3660. }
  3661. void TextEdit::set_gutter_width(int p_gutter, int p_width) {
  3662. ERR_FAIL_INDEX(p_gutter, gutters.size());
  3663. if (gutters[p_gutter].width == p_width) {
  3664. return;
  3665. }
  3666. gutters.write[p_gutter].width = p_width;
  3667. _update_gutter_width();
  3668. }
  3669. int TextEdit::get_gutter_width(int p_gutter) const {
  3670. ERR_FAIL_INDEX_V(p_gutter, gutters.size(), -1);
  3671. return gutters[p_gutter].width;
  3672. }
  3673. int TextEdit::get_total_gutter_width() const {
  3674. return gutters_width + gutter_padding;
  3675. }
  3676. void TextEdit::set_gutter_draw(int p_gutter, bool p_draw) {
  3677. ERR_FAIL_INDEX(p_gutter, gutters.size());
  3678. if (gutters[p_gutter].draw == p_draw) {
  3679. return;
  3680. }
  3681. gutters.write[p_gutter].draw = p_draw;
  3682. _update_gutter_width();
  3683. }
  3684. bool TextEdit::is_gutter_drawn(int p_gutter) const {
  3685. ERR_FAIL_INDEX_V(p_gutter, gutters.size(), false);
  3686. return gutters[p_gutter].draw;
  3687. }
  3688. void TextEdit::set_gutter_clickable(int p_gutter, bool p_clickable) {
  3689. ERR_FAIL_INDEX(p_gutter, gutters.size());
  3690. gutters.write[p_gutter].clickable = p_clickable;
  3691. update();
  3692. }
  3693. bool TextEdit::is_gutter_clickable(int p_gutter) const {
  3694. ERR_FAIL_INDEX_V(p_gutter, gutters.size(), false);
  3695. return gutters[p_gutter].clickable;
  3696. }
  3697. void TextEdit::set_gutter_overwritable(int p_gutter, bool p_overwritable) {
  3698. ERR_FAIL_INDEX(p_gutter, gutters.size());
  3699. gutters.write[p_gutter].overwritable = p_overwritable;
  3700. }
  3701. bool TextEdit::is_gutter_overwritable(int p_gutter) const {
  3702. ERR_FAIL_INDEX_V(p_gutter, gutters.size(), false);
  3703. return gutters[p_gutter].overwritable;
  3704. }
  3705. void TextEdit::merge_gutters(int p_from_line, int p_to_line) {
  3706. ERR_FAIL_INDEX(p_from_line, text.size());
  3707. ERR_FAIL_INDEX(p_to_line, text.size());
  3708. if (p_from_line == p_to_line) {
  3709. return;
  3710. }
  3711. for (int i = 0; i < gutters.size(); i++) {
  3712. if (!gutters[i].overwritable) {
  3713. continue;
  3714. }
  3715. if (text.get_line_gutter_text(p_from_line, i) != "") {
  3716. text.set_line_gutter_text(p_to_line, i, text.get_line_gutter_text(p_from_line, i));
  3717. text.set_line_gutter_item_color(p_to_line, i, text.get_line_gutter_item_color(p_from_line, i));
  3718. }
  3719. if (text.get_line_gutter_icon(p_from_line, i).is_valid()) {
  3720. text.set_line_gutter_icon(p_to_line, i, text.get_line_gutter_icon(p_from_line, i));
  3721. text.set_line_gutter_item_color(p_to_line, i, text.get_line_gutter_item_color(p_from_line, i));
  3722. }
  3723. if (text.get_line_gutter_metadata(p_from_line, i) != "") {
  3724. text.set_line_gutter_metadata(p_to_line, i, text.get_line_gutter_metadata(p_from_line, i));
  3725. }
  3726. if (text.is_line_gutter_clickable(p_from_line, i)) {
  3727. text.set_line_gutter_clickable(p_to_line, i, true);
  3728. }
  3729. }
  3730. update();
  3731. }
  3732. void TextEdit::set_gutter_custom_draw(int p_gutter, Object *p_object, const StringName &p_callback) {
  3733. ERR_FAIL_INDEX(p_gutter, gutters.size());
  3734. ERR_FAIL_NULL(p_object);
  3735. gutters.write[p_gutter].custom_draw_obj = p_object->get_instance_id();
  3736. gutters.write[p_gutter].custom_draw_callback = p_callback;
  3737. update();
  3738. }
  3739. // Line gutters.
  3740. void TextEdit::set_line_gutter_metadata(int p_line, int p_gutter, const Variant &p_metadata) {
  3741. ERR_FAIL_INDEX(p_line, text.size());
  3742. ERR_FAIL_INDEX(p_gutter, gutters.size());
  3743. text.set_line_gutter_metadata(p_line, p_gutter, p_metadata);
  3744. }
  3745. Variant TextEdit::get_line_gutter_metadata(int p_line, int p_gutter) const {
  3746. ERR_FAIL_INDEX_V(p_line, text.size(), "");
  3747. ERR_FAIL_INDEX_V(p_gutter, gutters.size(), "");
  3748. return text.get_line_gutter_metadata(p_line, p_gutter);
  3749. }
  3750. void TextEdit::set_line_gutter_text(int p_line, int p_gutter, const String &p_text) {
  3751. ERR_FAIL_INDEX(p_line, text.size());
  3752. ERR_FAIL_INDEX(p_gutter, gutters.size());
  3753. text.set_line_gutter_text(p_line, p_gutter, p_text);
  3754. update();
  3755. }
  3756. String TextEdit::get_line_gutter_text(int p_line, int p_gutter) const {
  3757. ERR_FAIL_INDEX_V(p_line, text.size(), "");
  3758. ERR_FAIL_INDEX_V(p_gutter, gutters.size(), "");
  3759. return text.get_line_gutter_text(p_line, p_gutter);
  3760. }
  3761. void TextEdit::set_line_gutter_icon(int p_line, int p_gutter, const Ref<Texture2D> &p_icon) {
  3762. ERR_FAIL_INDEX(p_line, text.size());
  3763. ERR_FAIL_INDEX(p_gutter, gutters.size());
  3764. text.set_line_gutter_icon(p_line, p_gutter, p_icon);
  3765. update();
  3766. }
  3767. Ref<Texture2D> TextEdit::get_line_gutter_icon(int p_line, int p_gutter) const {
  3768. ERR_FAIL_INDEX_V(p_line, text.size(), Ref<Texture2D>());
  3769. ERR_FAIL_INDEX_V(p_gutter, gutters.size(), Ref<Texture2D>());
  3770. return text.get_line_gutter_icon(p_line, p_gutter);
  3771. }
  3772. void TextEdit::set_line_gutter_item_color(int p_line, int p_gutter, const Color &p_color) {
  3773. ERR_FAIL_INDEX(p_line, text.size());
  3774. ERR_FAIL_INDEX(p_gutter, gutters.size());
  3775. text.set_line_gutter_item_color(p_line, p_gutter, p_color);
  3776. update();
  3777. }
  3778. Color TextEdit::get_line_gutter_item_color(int p_line, int p_gutter) const {
  3779. ERR_FAIL_INDEX_V(p_line, text.size(), Color());
  3780. ERR_FAIL_INDEX_V(p_gutter, gutters.size(), Color());
  3781. return text.get_line_gutter_item_color(p_line, p_gutter);
  3782. }
  3783. void TextEdit::set_line_gutter_clickable(int p_line, int p_gutter, bool p_clickable) {
  3784. ERR_FAIL_INDEX(p_line, text.size());
  3785. ERR_FAIL_INDEX(p_gutter, gutters.size());
  3786. text.set_line_gutter_clickable(p_line, p_gutter, p_clickable);
  3787. }
  3788. bool TextEdit::is_line_gutter_clickable(int p_line, int p_gutter) const {
  3789. ERR_FAIL_INDEX_V(p_line, text.size(), false);
  3790. ERR_FAIL_INDEX_V(p_gutter, gutters.size(), false);
  3791. return text.is_line_gutter_clickable(p_line, p_gutter);
  3792. }
  3793. // Line style
  3794. void TextEdit::set_line_background_color(int p_line, const Color &p_color) {
  3795. ERR_FAIL_INDEX(p_line, text.size());
  3796. text.set_line_background_color(p_line, p_color);
  3797. update();
  3798. }
  3799. Color TextEdit::get_line_background_color(int p_line) const {
  3800. ERR_FAIL_INDEX_V(p_line, text.size(), Color());
  3801. return text.get_line_background_color(p_line);
  3802. }
  3803. /* Syntax Highlighting. */
  3804. void TextEdit::set_syntax_highlighter(Ref<SyntaxHighlighter> p_syntax_highlighter) {
  3805. syntax_highlighter = p_syntax_highlighter;
  3806. if (syntax_highlighter.is_valid()) {
  3807. syntax_highlighter->set_text_edit(this);
  3808. }
  3809. update();
  3810. }
  3811. Ref<SyntaxHighlighter> TextEdit::get_syntax_highlighter() const {
  3812. return syntax_highlighter;
  3813. }
  3814. /* Visual. */
  3815. void TextEdit::set_highlight_current_line(bool p_enabled) {
  3816. highlight_current_line = p_enabled;
  3817. update();
  3818. }
  3819. bool TextEdit::is_highlight_current_line_enabled() const {
  3820. return highlight_current_line;
  3821. }
  3822. void TextEdit::set_highlight_all_occurrences(const bool p_enabled) {
  3823. highlight_all_occurrences = p_enabled;
  3824. update();
  3825. }
  3826. bool TextEdit::is_highlight_all_occurrences_enabled() const {
  3827. return highlight_all_occurrences;
  3828. }
  3829. void TextEdit::set_draw_control_chars(bool p_draw_control_chars) {
  3830. if (draw_control_chars != p_draw_control_chars) {
  3831. draw_control_chars = p_draw_control_chars;
  3832. if (menu) {
  3833. menu->set_item_checked(menu->get_item_index(MENU_DISPLAY_UCC), draw_control_chars);
  3834. }
  3835. text.set_draw_control_chars(draw_control_chars);
  3836. text.invalidate_all();
  3837. update();
  3838. }
  3839. }
  3840. bool TextEdit::get_draw_control_chars() const {
  3841. return draw_control_chars;
  3842. }
  3843. void TextEdit::set_draw_tabs(bool p_draw) {
  3844. draw_tabs = p_draw;
  3845. update();
  3846. }
  3847. bool TextEdit::is_drawing_tabs() const {
  3848. return draw_tabs;
  3849. }
  3850. void TextEdit::set_draw_spaces(bool p_draw) {
  3851. draw_spaces = p_draw;
  3852. update();
  3853. }
  3854. bool TextEdit::is_drawing_spaces() const {
  3855. return draw_spaces;
  3856. }
  3857. void TextEdit::_bind_methods() {
  3858. /*Internal. */
  3859. ClassDB::bind_method(D_METHOD("_text_changed_emit"), &TextEdit::_text_changed_emit);
  3860. /* Text */
  3861. // Text properties
  3862. ClassDB::bind_method(D_METHOD("has_ime_text"), &TextEdit::has_ime_text);
  3863. ClassDB::bind_method(D_METHOD("set_editable", "enable"), &TextEdit::set_editable);
  3864. ClassDB::bind_method(D_METHOD("is_editable"), &TextEdit::is_editable);
  3865. ClassDB::bind_method(D_METHOD("set_text_direction", "direction"), &TextEdit::set_text_direction);
  3866. ClassDB::bind_method(D_METHOD("get_text_direction"), &TextEdit::get_text_direction);
  3867. ClassDB::bind_method(D_METHOD("set_opentype_feature", "tag", "value"), &TextEdit::set_opentype_feature);
  3868. ClassDB::bind_method(D_METHOD("get_opentype_feature", "tag"), &TextEdit::get_opentype_feature);
  3869. ClassDB::bind_method(D_METHOD("clear_opentype_features"), &TextEdit::clear_opentype_features);
  3870. ClassDB::bind_method(D_METHOD("set_language", "language"), &TextEdit::set_language);
  3871. ClassDB::bind_method(D_METHOD("get_language"), &TextEdit::get_language);
  3872. ClassDB::bind_method(D_METHOD("set_structured_text_bidi_override", "parser"), &TextEdit::set_structured_text_bidi_override);
  3873. ClassDB::bind_method(D_METHOD("get_structured_text_bidi_override"), &TextEdit::get_structured_text_bidi_override);
  3874. ClassDB::bind_method(D_METHOD("set_structured_text_bidi_override_options", "args"), &TextEdit::set_structured_text_bidi_override_options);
  3875. ClassDB::bind_method(D_METHOD("get_structured_text_bidi_override_options"), &TextEdit::get_structured_text_bidi_override_options);
  3876. ClassDB::bind_method(D_METHOD("set_tab_size", "size"), &TextEdit::set_tab_size);
  3877. ClassDB::bind_method(D_METHOD("get_tab_size"), &TextEdit::get_tab_size);
  3878. // User controls
  3879. ClassDB::bind_method(D_METHOD("set_overtype_mode_enabled", "enabled"), &TextEdit::set_overtype_mode_enabled);
  3880. ClassDB::bind_method(D_METHOD("is_overtype_mode_enabled"), &TextEdit::is_overtype_mode_enabled);
  3881. ClassDB::bind_method(D_METHOD("set_context_menu_enabled", "enable"), &TextEdit::set_context_menu_enabled);
  3882. ClassDB::bind_method(D_METHOD("is_context_menu_enabled"), &TextEdit::is_context_menu_enabled);
  3883. ClassDB::bind_method(D_METHOD("set_shortcut_keys_enabled", "enable"), &TextEdit::set_shortcut_keys_enabled);
  3884. ClassDB::bind_method(D_METHOD("is_shortcut_keys_enabled"), &TextEdit::is_shortcut_keys_enabled);
  3885. ClassDB::bind_method(D_METHOD("set_virtual_keyboard_enabled", "enable"), &TextEdit::set_virtual_keyboard_enabled);
  3886. ClassDB::bind_method(D_METHOD("is_virtual_keyboard_enabled"), &TextEdit::is_virtual_keyboard_enabled);
  3887. // Text manipulation
  3888. ClassDB::bind_method(D_METHOD("clear"), &TextEdit::clear);
  3889. ClassDB::bind_method(D_METHOD("set_text", "text"), &TextEdit::set_text);
  3890. ClassDB::bind_method(D_METHOD("get_text"), &TextEdit::get_text);
  3891. ClassDB::bind_method(D_METHOD("get_line_count"), &TextEdit::get_line_count);
  3892. ClassDB::bind_method(D_METHOD("set_line", "line", "new_text"), &TextEdit::set_line);
  3893. ClassDB::bind_method(D_METHOD("get_line", "line"), &TextEdit::get_line);
  3894. ClassDB::bind_method(D_METHOD("get_line_width", "line", "wrap_index"), &TextEdit::get_line_width, DEFVAL(-1));
  3895. ClassDB::bind_method(D_METHOD("get_line_height"), &TextEdit::get_line_height);
  3896. ClassDB::bind_method(D_METHOD("get_indent_level", "line"), &TextEdit::get_indent_level);
  3897. ClassDB::bind_method(D_METHOD("get_first_non_whitespace_column", "line"), &TextEdit::get_first_non_whitespace_column);
  3898. ClassDB::bind_method(D_METHOD("swap_lines", "from_line", "to_line"), &TextEdit::swap_lines);
  3899. ClassDB::bind_method(D_METHOD("insert_line_at", "line", "text"), &TextEdit::insert_line_at);
  3900. ClassDB::bind_method(D_METHOD("insert_text_at_caret", "text"), &TextEdit::insert_text_at_caret);
  3901. ClassDB::bind_method(D_METHOD("remove_text", "from_line", "from_column", "to_line", "to_column"), &TextEdit::remove_text);
  3902. ClassDB::bind_method(D_METHOD("get_last_unhidden_line"), &TextEdit::get_last_unhidden_line);
  3903. ClassDB::bind_method(D_METHOD("get_next_visible_line_offset_from", "line", "visible_amount"), &TextEdit::get_next_visible_line_offset_from);
  3904. ClassDB::bind_method(D_METHOD("get_next_visible_line_index_offset_from", "line", "wrap_index", "visible_amount"), &TextEdit::get_next_visible_line_index_offset_from);
  3905. // Overridable actions
  3906. ClassDB::bind_method(D_METHOD("backspace"), &TextEdit::backspace);
  3907. ClassDB::bind_method(D_METHOD("cut"), &TextEdit::cut);
  3908. ClassDB::bind_method(D_METHOD("copy"), &TextEdit::copy);
  3909. ClassDB::bind_method(D_METHOD("paste"), &TextEdit::paste);
  3910. GDVIRTUAL_BIND(_handle_unicode_input, "unicode_char")
  3911. GDVIRTUAL_BIND(_backspace)
  3912. GDVIRTUAL_BIND(_cut)
  3913. GDVIRTUAL_BIND(_copy)
  3914. GDVIRTUAL_BIND(_paste)
  3915. // Context Menu
  3916. BIND_ENUM_CONSTANT(MENU_CUT);
  3917. BIND_ENUM_CONSTANT(MENU_COPY);
  3918. BIND_ENUM_CONSTANT(MENU_PASTE);
  3919. BIND_ENUM_CONSTANT(MENU_CLEAR);
  3920. BIND_ENUM_CONSTANT(MENU_SELECT_ALL);
  3921. BIND_ENUM_CONSTANT(MENU_UNDO);
  3922. BIND_ENUM_CONSTANT(MENU_REDO);
  3923. BIND_ENUM_CONSTANT(MENU_DIR_INHERITED);
  3924. BIND_ENUM_CONSTANT(MENU_DIR_AUTO);
  3925. BIND_ENUM_CONSTANT(MENU_DIR_LTR);
  3926. BIND_ENUM_CONSTANT(MENU_DIR_RTL);
  3927. BIND_ENUM_CONSTANT(MENU_DISPLAY_UCC);
  3928. BIND_ENUM_CONSTANT(MENU_INSERT_LRM);
  3929. BIND_ENUM_CONSTANT(MENU_INSERT_RLM);
  3930. BIND_ENUM_CONSTANT(MENU_INSERT_LRE);
  3931. BIND_ENUM_CONSTANT(MENU_INSERT_RLE);
  3932. BIND_ENUM_CONSTANT(MENU_INSERT_LRO);
  3933. BIND_ENUM_CONSTANT(MENU_INSERT_RLO);
  3934. BIND_ENUM_CONSTANT(MENU_INSERT_PDF);
  3935. BIND_ENUM_CONSTANT(MENU_INSERT_ALM);
  3936. BIND_ENUM_CONSTANT(MENU_INSERT_LRI);
  3937. BIND_ENUM_CONSTANT(MENU_INSERT_RLI);
  3938. BIND_ENUM_CONSTANT(MENU_INSERT_FSI);
  3939. BIND_ENUM_CONSTANT(MENU_INSERT_PDI);
  3940. BIND_ENUM_CONSTANT(MENU_INSERT_ZWJ);
  3941. BIND_ENUM_CONSTANT(MENU_INSERT_ZWNJ);
  3942. BIND_ENUM_CONSTANT(MENU_INSERT_WJ);
  3943. BIND_ENUM_CONSTANT(MENU_INSERT_SHY);
  3944. BIND_ENUM_CONSTANT(MENU_MAX);
  3945. /* Versioning */
  3946. ClassDB::bind_method(D_METHOD("begin_complex_operation"), &TextEdit::begin_complex_operation);
  3947. ClassDB::bind_method(D_METHOD("end_complex_operation"), &TextEdit::end_complex_operation);
  3948. ClassDB::bind_method(D_METHOD("has_undo"), &TextEdit::has_undo);
  3949. ClassDB::bind_method(D_METHOD("has_redo"), &TextEdit::has_redo);
  3950. ClassDB::bind_method(D_METHOD("undo"), &TextEdit::undo);
  3951. ClassDB::bind_method(D_METHOD("redo"), &TextEdit::redo);
  3952. ClassDB::bind_method(D_METHOD("clear_undo_history"), &TextEdit::clear_undo_history);
  3953. ClassDB::bind_method(D_METHOD("tag_saved_version"), &TextEdit::tag_saved_version);
  3954. ClassDB::bind_method(D_METHOD("get_version"), &TextEdit::get_version);
  3955. ClassDB::bind_method(D_METHOD("get_saved_version"), &TextEdit::get_saved_version);
  3956. /* Search */
  3957. BIND_ENUM_CONSTANT(SEARCH_MATCH_CASE);
  3958. BIND_ENUM_CONSTANT(SEARCH_WHOLE_WORDS);
  3959. BIND_ENUM_CONSTANT(SEARCH_BACKWARDS);
  3960. ClassDB::bind_method(D_METHOD("set_search_text", "search_text"), &TextEdit::set_search_text);
  3961. ClassDB::bind_method(D_METHOD("set_search_flags", "flags"), &TextEdit::set_search_flags);
  3962. ClassDB::bind_method(D_METHOD("search", "text", "flags", "from_line", "from_colum"), &TextEdit::search);
  3963. /* Tooltip */
  3964. ClassDB::bind_method(D_METHOD("set_tooltip_request_func", "object", "callback", "data"), &TextEdit::set_tooltip_request_func);
  3965. /* Mouse */
  3966. ClassDB::bind_method(D_METHOD("get_local_mouse_pos"), &TextEdit::get_local_mouse_pos);
  3967. ClassDB::bind_method(D_METHOD("get_word_at_pos", "position"), &TextEdit::get_word_at_pos);
  3968. ClassDB::bind_method(D_METHOD("get_line_column_at_pos", "position"), &TextEdit::get_line_column_at_pos);
  3969. ClassDB::bind_method(D_METHOD("get_minimap_line_at_pos", "position"), &TextEdit::get_minimap_line_at_pos);
  3970. ClassDB::bind_method(D_METHOD("is_dragging_cursor"), &TextEdit::is_dragging_cursor);
  3971. /* Caret. */
  3972. BIND_ENUM_CONSTANT(CARET_TYPE_LINE);
  3973. BIND_ENUM_CONSTANT(CARET_TYPE_BLOCK);
  3974. // internal.
  3975. ClassDB::bind_method(D_METHOD("_emit_caret_changed"), &TextEdit::_emit_caret_changed);
  3976. ClassDB::bind_method(D_METHOD("set_caret_type", "type"), &TextEdit::set_caret_type);
  3977. ClassDB::bind_method(D_METHOD("get_caret_type"), &TextEdit::get_caret_type);
  3978. ClassDB::bind_method(D_METHOD("set_caret_blink_enabled", "enable"), &TextEdit::set_caret_blink_enabled);
  3979. ClassDB::bind_method(D_METHOD("is_caret_blink_enabled"), &TextEdit::is_caret_blink_enabled);
  3980. ClassDB::bind_method(D_METHOD("set_caret_blink_speed", "blink_speed"), &TextEdit::set_caret_blink_speed);
  3981. ClassDB::bind_method(D_METHOD("get_caret_blink_speed"), &TextEdit::get_caret_blink_speed);
  3982. ClassDB::bind_method(D_METHOD("set_move_caret_on_right_click_enabled", "enable"), &TextEdit::set_move_caret_on_right_click_enabled);
  3983. ClassDB::bind_method(D_METHOD("is_move_caret_on_right_click_enabled"), &TextEdit::is_move_caret_on_right_click_enabled);
  3984. ClassDB::bind_method(D_METHOD("set_caret_mid_grapheme_enabled", "enabled"), &TextEdit::set_caret_mid_grapheme_enabled);
  3985. ClassDB::bind_method(D_METHOD("is_caret_mid_grapheme_enabled"), &TextEdit::is_caret_mid_grapheme_enabled);
  3986. ClassDB::bind_method(D_METHOD("is_caret_visible"), &TextEdit::is_caret_visible);
  3987. ClassDB::bind_method(D_METHOD("get_caret_draw_pos"), &TextEdit::get_caret_draw_pos);
  3988. ClassDB::bind_method(D_METHOD("set_caret_line", "line", "adjust_viewport", "can_be_hidden", "wrap_index"), &TextEdit::set_caret_line, DEFVAL(true), DEFVAL(true), DEFVAL(0));
  3989. ClassDB::bind_method(D_METHOD("get_caret_line"), &TextEdit::get_caret_line);
  3990. ClassDB::bind_method(D_METHOD("set_caret_column", "column", "adjust_viewport"), &TextEdit::set_caret_column, DEFVAL(true));
  3991. ClassDB::bind_method(D_METHOD("get_caret_column"), &TextEdit::get_caret_column);
  3992. ClassDB::bind_method(D_METHOD("get_caret_wrap_index"), &TextEdit::get_caret_wrap_index);
  3993. ClassDB::bind_method(D_METHOD("get_word_under_caret"), &TextEdit::get_word_under_caret);
  3994. /* Selection. */
  3995. BIND_ENUM_CONSTANT(SELECTION_MODE_NONE);
  3996. BIND_ENUM_CONSTANT(SELECTION_MODE_SHIFT);
  3997. BIND_ENUM_CONSTANT(SELECTION_MODE_POINTER);
  3998. BIND_ENUM_CONSTANT(SELECTION_MODE_WORD);
  3999. BIND_ENUM_CONSTANT(SELECTION_MODE_LINE);
  4000. ClassDB::bind_method(D_METHOD("set_selecting_enabled", "enable"), &TextEdit::set_selecting_enabled);
  4001. ClassDB::bind_method(D_METHOD("is_selecting_enabled"), &TextEdit::is_selecting_enabled);
  4002. ClassDB::bind_method(D_METHOD("set_override_selected_font_color", "override"), &TextEdit::set_override_selected_font_color);
  4003. ClassDB::bind_method(D_METHOD("is_overriding_selected_font_color"), &TextEdit::is_overriding_selected_font_color);
  4004. ClassDB::bind_method(D_METHOD("set_selection_mode", "mode", "line", "column"), &TextEdit::set_selection_mode, DEFVAL(-1), DEFVAL(-1));
  4005. ClassDB::bind_method(D_METHOD("get_selection_mode"), &TextEdit::get_selection_mode);
  4006. ClassDB::bind_method(D_METHOD("select_all"), &TextEdit::select_all);
  4007. ClassDB::bind_method(D_METHOD("select_word_under_caret"), &TextEdit::select_word_under_caret);
  4008. ClassDB::bind_method(D_METHOD("select", "from_line", "from_column", "to_line", "to_column"), &TextEdit::select);
  4009. ClassDB::bind_method(D_METHOD("has_selection"), &TextEdit::has_selection);
  4010. ClassDB::bind_method(D_METHOD("get_selected_text"), &TextEdit::get_selected_text);
  4011. ClassDB::bind_method(D_METHOD("get_selection_line"), &TextEdit::get_selection_line);
  4012. ClassDB::bind_method(D_METHOD("get_selection_column"), &TextEdit::get_selection_column);
  4013. ClassDB::bind_method(D_METHOD("get_selection_from_line"), &TextEdit::get_selection_from_line);
  4014. ClassDB::bind_method(D_METHOD("get_selection_from_column"), &TextEdit::get_selection_from_column);
  4015. ClassDB::bind_method(D_METHOD("get_selection_to_line"), &TextEdit::get_selection_to_line);
  4016. ClassDB::bind_method(D_METHOD("get_selection_to_column"), &TextEdit::get_selection_to_column);
  4017. ClassDB::bind_method(D_METHOD("deselect"), &TextEdit::deselect);
  4018. ClassDB::bind_method(D_METHOD("delete_selection"), &TextEdit::delete_selection);
  4019. /* line wrapping. */
  4020. BIND_ENUM_CONSTANT(LINE_WRAPPING_NONE);
  4021. BIND_ENUM_CONSTANT(LINE_WRAPPING_BOUNDARY);
  4022. // internal.
  4023. ClassDB::bind_method(D_METHOD("_update_wrap_at_column", "force"), &TextEdit::_update_wrap_at_column, DEFVAL(false));
  4024. ClassDB::bind_method(D_METHOD("set_line_wrapping_mode", "mode"), &TextEdit::set_line_wrapping_mode);
  4025. ClassDB::bind_method(D_METHOD("get_line_wrapping_mode"), &TextEdit::get_line_wrapping_mode);
  4026. ClassDB::bind_method(D_METHOD("is_line_wrapped", "line"), &TextEdit::is_line_wrapped);
  4027. ClassDB::bind_method(D_METHOD("get_line_wrap_count", "line"), &TextEdit::get_line_wrap_count);
  4028. ClassDB::bind_method(D_METHOD("get_line_wrap_index_at_column", "line", "column"), &TextEdit::get_line_wrap_index_at_column);
  4029. ClassDB::bind_method(D_METHOD("get_line_wrapped_text", "line"), &TextEdit::get_line_wrapped_text);
  4030. /* Viewport. */
  4031. // Scolling.
  4032. ClassDB::bind_method(D_METHOD("set_smooth_scroll_enable", "enable"), &TextEdit::set_smooth_scroll_enabled);
  4033. ClassDB::bind_method(D_METHOD("is_smooth_scroll_enabled"), &TextEdit::is_smooth_scroll_enabled);
  4034. ClassDB::bind_method(D_METHOD("set_v_scroll", "value"), &TextEdit::set_v_scroll);
  4035. ClassDB::bind_method(D_METHOD("get_v_scroll"), &TextEdit::get_v_scroll);
  4036. ClassDB::bind_method(D_METHOD("set_h_scroll", "value"), &TextEdit::set_h_scroll);
  4037. ClassDB::bind_method(D_METHOD("get_h_scroll"), &TextEdit::get_h_scroll);
  4038. ClassDB::bind_method(D_METHOD("set_scroll_past_end_of_file_enabled", "enable"), &TextEdit::set_scroll_past_end_of_file_enabled);
  4039. ClassDB::bind_method(D_METHOD("is_scroll_past_end_of_file_enabled"), &TextEdit::is_scroll_past_end_of_file_enabled);
  4040. ClassDB::bind_method(D_METHOD("set_v_scroll_speed", "speed"), &TextEdit::set_v_scroll_speed);
  4041. ClassDB::bind_method(D_METHOD("get_v_scroll_speed"), &TextEdit::get_v_scroll_speed);
  4042. ClassDB::bind_method(D_METHOD("get_scroll_pos_for_line", "line", "wrap_index"), &TextEdit::get_scroll_pos_for_line, DEFVAL(0));
  4043. // Visible lines.
  4044. ClassDB::bind_method(D_METHOD("set_line_as_first_visible", "line", "wrap_index"), &TextEdit::set_line_as_first_visible, DEFVAL(0));
  4045. ClassDB::bind_method(D_METHOD("get_first_visible_line"), &TextEdit::get_first_visible_line);
  4046. ClassDB::bind_method(D_METHOD("set_line_as_center_visible", "line", "wrap_index"), &TextEdit::set_line_as_center_visible, DEFVAL(0));
  4047. ClassDB::bind_method(D_METHOD("set_line_as_last_visible", "line", "wrap_index"), &TextEdit::set_line_as_last_visible, DEFVAL(0));
  4048. ClassDB::bind_method(D_METHOD("get_last_full_visible_line"), &TextEdit::get_last_full_visible_line);
  4049. ClassDB::bind_method(D_METHOD("get_last_full_visible_line_wrap_index"), &TextEdit::get_last_full_visible_line_wrap_index);
  4050. ClassDB::bind_method(D_METHOD("get_visible_line_count"), &TextEdit::get_visible_line_count);
  4051. ClassDB::bind_method(D_METHOD("get_total_visible_line_count"), &TextEdit::get_total_visible_line_count);
  4052. // Auto adjust
  4053. ClassDB::bind_method(D_METHOD("adjust_viewport_to_caret"), &TextEdit::adjust_viewport_to_caret);
  4054. ClassDB::bind_method(D_METHOD("center_viewport_to_caret"), &TextEdit::center_viewport_to_caret);
  4055. // Minimap
  4056. ClassDB::bind_method(D_METHOD("draw_minimap", "draw"), &TextEdit::set_draw_minimap);
  4057. ClassDB::bind_method(D_METHOD("is_drawing_minimap"), &TextEdit::is_drawing_minimap);
  4058. ClassDB::bind_method(D_METHOD("set_minimap_width", "width"), &TextEdit::set_minimap_width);
  4059. ClassDB::bind_method(D_METHOD("get_minimap_width"), &TextEdit::get_minimap_width);
  4060. ClassDB::bind_method(D_METHOD("get_minimap_visible_lines"), &TextEdit::get_minimap_visible_lines);
  4061. /* Gutters. */
  4062. BIND_ENUM_CONSTANT(GUTTER_TYPE_STRING);
  4063. BIND_ENUM_CONSTANT(GUTTER_TYPE_ICON);
  4064. BIND_ENUM_CONSTANT(GUTTER_TYPE_CUSTOM);
  4065. ClassDB::bind_method(D_METHOD("add_gutter", "at"), &TextEdit::add_gutter, DEFVAL(-1));
  4066. ClassDB::bind_method(D_METHOD("remove_gutter", "gutter"), &TextEdit::remove_gutter);
  4067. ClassDB::bind_method(D_METHOD("get_gutter_count"), &TextEdit::get_gutter_count);
  4068. ClassDB::bind_method(D_METHOD("set_gutter_name", "gutter", "name"), &TextEdit::set_gutter_name);
  4069. ClassDB::bind_method(D_METHOD("get_gutter_name", "gutter"), &TextEdit::get_gutter_name);
  4070. ClassDB::bind_method(D_METHOD("set_gutter_type", "gutter", "type"), &TextEdit::set_gutter_type);
  4071. ClassDB::bind_method(D_METHOD("get_gutter_type", "gutter"), &TextEdit::get_gutter_type);
  4072. ClassDB::bind_method(D_METHOD("set_gutter_width", "gutter", "width"), &TextEdit::set_gutter_width);
  4073. ClassDB::bind_method(D_METHOD("get_gutter_width", "gutter"), &TextEdit::get_gutter_width);
  4074. ClassDB::bind_method(D_METHOD("set_gutter_draw", "gutter", "draw"), &TextEdit::set_gutter_draw);
  4075. ClassDB::bind_method(D_METHOD("is_gutter_drawn", "gutter"), &TextEdit::is_gutter_drawn);
  4076. ClassDB::bind_method(D_METHOD("set_gutter_clickable", "gutter", "clickable"), &TextEdit::set_gutter_clickable);
  4077. ClassDB::bind_method(D_METHOD("is_gutter_clickable", "gutter"), &TextEdit::is_gutter_clickable);
  4078. ClassDB::bind_method(D_METHOD("set_gutter_overwritable", "gutter", "overwritable"), &TextEdit::set_gutter_overwritable);
  4079. ClassDB::bind_method(D_METHOD("is_gutter_overwritable", "gutter"), &TextEdit::is_gutter_overwritable);
  4080. ClassDB::bind_method(D_METHOD("merge_gutters", "from_line", "to_line"), &TextEdit::merge_gutters);
  4081. ClassDB::bind_method(D_METHOD("set_gutter_custom_draw", "column", "object", "callback"), &TextEdit::set_gutter_custom_draw);
  4082. ClassDB::bind_method(D_METHOD("get_total_gutter_width"), &TextEdit::get_total_gutter_width);
  4083. // Line gutters.
  4084. ClassDB::bind_method(D_METHOD("set_line_gutter_metadata", "line", "gutter", "metadata"), &TextEdit::set_line_gutter_metadata);
  4085. ClassDB::bind_method(D_METHOD("get_line_gutter_metadata", "line", "gutter"), &TextEdit::get_line_gutter_metadata);
  4086. ClassDB::bind_method(D_METHOD("set_line_gutter_text", "line", "gutter", "text"), &TextEdit::set_line_gutter_text);
  4087. ClassDB::bind_method(D_METHOD("get_line_gutter_text", "line", "gutter"), &TextEdit::get_line_gutter_text);
  4088. ClassDB::bind_method(D_METHOD("set_line_gutter_icon", "line", "gutter", "icon"), &TextEdit::set_line_gutter_icon);
  4089. ClassDB::bind_method(D_METHOD("get_line_gutter_icon", "line", "gutter"), &TextEdit::get_line_gutter_icon);
  4090. ClassDB::bind_method(D_METHOD("set_line_gutter_item_color", "line", "gutter", "color"), &TextEdit::set_line_gutter_item_color);
  4091. ClassDB::bind_method(D_METHOD("get_line_gutter_item_color", "line", "gutter"), &TextEdit::get_line_gutter_item_color);
  4092. ClassDB::bind_method(D_METHOD("set_line_gutter_clickable", "line", "gutter", "clickable"), &TextEdit::set_line_gutter_clickable);
  4093. ClassDB::bind_method(D_METHOD("is_line_gutter_clickable", "line", "gutter"), &TextEdit::is_line_gutter_clickable);
  4094. // Line style
  4095. ClassDB::bind_method(D_METHOD("set_line_background_color", "line", "color"), &TextEdit::set_line_background_color);
  4096. ClassDB::bind_method(D_METHOD("get_line_background_color", "line"), &TextEdit::get_line_background_color);
  4097. /* Syntax Highlighting. */
  4098. ClassDB::bind_method(D_METHOD("set_syntax_highlighter", "syntax_highlighter"), &TextEdit::set_syntax_highlighter);
  4099. ClassDB::bind_method(D_METHOD("get_syntax_highlighter"), &TextEdit::get_syntax_highlighter);
  4100. /* Visual. */
  4101. ClassDB::bind_method(D_METHOD("set_highlight_current_line", "enabled"), &TextEdit::set_highlight_current_line);
  4102. ClassDB::bind_method(D_METHOD("is_highlight_current_line_enabled"), &TextEdit::is_highlight_current_line_enabled);
  4103. ClassDB::bind_method(D_METHOD("set_highlight_all_occurrences", "enable"), &TextEdit::set_highlight_all_occurrences);
  4104. ClassDB::bind_method(D_METHOD("is_highlight_all_occurrences_enabled"), &TextEdit::is_highlight_all_occurrences_enabled);
  4105. ClassDB::bind_method(D_METHOD("get_draw_control_chars"), &TextEdit::get_draw_control_chars);
  4106. ClassDB::bind_method(D_METHOD("set_draw_control_chars", "enable"), &TextEdit::set_draw_control_chars);
  4107. ClassDB::bind_method(D_METHOD("set_draw_tabs"), &TextEdit::set_draw_tabs);
  4108. ClassDB::bind_method(D_METHOD("is_drawing_tabs"), &TextEdit::is_drawing_tabs);
  4109. ClassDB::bind_method(D_METHOD("set_draw_spaces"), &TextEdit::set_draw_spaces);
  4110. ClassDB::bind_method(D_METHOD("is_drawing_spaces"), &TextEdit::is_drawing_spaces);
  4111. ClassDB::bind_method(D_METHOD("get_menu"), &TextEdit::get_menu);
  4112. ClassDB::bind_method(D_METHOD("is_menu_visible"), &TextEdit::is_menu_visible);
  4113. ClassDB::bind_method(D_METHOD("menu_option", "option"), &TextEdit::menu_option);
  4114. /* Inspector */
  4115. ADD_PROPERTY(PropertyInfo(Variant::STRING, "text", PROPERTY_HINT_MULTILINE_TEXT), "set_text", "get_text");
  4116. ADD_PROPERTY(PropertyInfo(Variant::INT, "text_direction", PROPERTY_HINT_ENUM, "Auto,Left-to-Right,Right-to-Left,Inherited"), "set_text_direction", "get_text_direction");
  4117. ADD_PROPERTY(PropertyInfo(Variant::STRING, "language"), "set_language", "get_language");
  4118. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "editable"), "set_editable", "is_editable");
  4119. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "context_menu_enabled"), "set_context_menu_enabled", "is_context_menu_enabled");
  4120. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "shortcut_keys_enabled"), "set_shortcut_keys_enabled", "is_shortcut_keys_enabled");
  4121. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "selecting_enabled"), "set_selecting_enabled", "is_selecting_enabled");
  4122. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "virtual_keyboard_enabled"), "set_virtual_keyboard_enabled", "is_virtual_keyboard_enabled");
  4123. ADD_PROPERTY(PropertyInfo(Variant::INT, "wrap_mode", PROPERTY_HINT_ENUM, "None,Boundary"), "set_line_wrapping_mode", "get_line_wrapping_mode");
  4124. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "override_selected_font_color"), "set_override_selected_font_color", "is_overriding_selected_font_color");
  4125. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "highlight_all_occurrences"), "set_highlight_all_occurrences", "is_highlight_all_occurrences_enabled");
  4126. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "highlight_current_line"), "set_highlight_current_line", "is_highlight_current_line_enabled");
  4127. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "draw_control_chars"), "set_draw_control_chars", "get_draw_control_chars");
  4128. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "draw_tabs"), "set_draw_tabs", "is_drawing_tabs");
  4129. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "draw_spaces"), "set_draw_spaces", "is_drawing_spaces");
  4130. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "syntax_highlighter", PROPERTY_HINT_RESOURCE_TYPE, "SyntaxHighlighter", PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_DO_NOT_SHARE_ON_DUPLICATE), "set_syntax_highlighter", "get_syntax_highlighter");
  4131. ADD_GROUP("Scroll", "scroll_");
  4132. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "scroll_smooth"), "set_smooth_scroll_enable", "is_smooth_scroll_enabled");
  4133. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "scroll_v_scroll_speed"), "set_v_scroll_speed", "get_v_scroll_speed");
  4134. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "scroll_past_end_of_file"), "set_scroll_past_end_of_file_enabled", "is_scroll_past_end_of_file_enabled");
  4135. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "scroll_vertical"), "set_v_scroll", "get_v_scroll");
  4136. ADD_PROPERTY(PropertyInfo(Variant::INT, "scroll_horizontal"), "set_h_scroll", "get_h_scroll");
  4137. ADD_GROUP("Minimap", "minimap_");
  4138. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "minimap_draw"), "draw_minimap", "is_drawing_minimap");
  4139. ADD_PROPERTY(PropertyInfo(Variant::INT, "minimap_width"), "set_minimap_width", "get_minimap_width");
  4140. ADD_GROUP("Caret", "caret_");
  4141. ADD_PROPERTY(PropertyInfo(Variant::INT, "caret_type", PROPERTY_HINT_ENUM, "Line,Block"), "set_caret_type", "get_caret_type");
  4142. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "caret_blink"), "set_caret_blink_enabled", "is_caret_blink_enabled");
  4143. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "caret_blink_speed", PROPERTY_HINT_RANGE, "0.1,10,0.01"), "set_caret_blink_speed", "get_caret_blink_speed");
  4144. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "caret_move_on_right_click"), "set_move_caret_on_right_click_enabled", "is_move_caret_on_right_click_enabled");
  4145. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "caret_mid_grapheme"), "set_caret_mid_grapheme_enabled", "is_caret_mid_grapheme_enabled");
  4146. ADD_GROUP("Structured Text", "structured_text_");
  4147. ADD_PROPERTY(PropertyInfo(Variant::INT, "structured_text_bidi_override", PROPERTY_HINT_ENUM, "Default,URI,File,Email,List,None,Custom"), "set_structured_text_bidi_override", "get_structured_text_bidi_override");
  4148. ADD_PROPERTY(PropertyInfo(Variant::ARRAY, "structured_text_bidi_override_options"), "set_structured_text_bidi_override_options", "get_structured_text_bidi_override_options");
  4149. /* Signals */
  4150. /* Core. */
  4151. ADD_SIGNAL(MethodInfo("text_set"));
  4152. ADD_SIGNAL(MethodInfo("text_changed"));
  4153. ADD_SIGNAL(MethodInfo("lines_edited_from", PropertyInfo(Variant::INT, "from_line"), PropertyInfo(Variant::INT, "to_line")));
  4154. /* Caret. */
  4155. ADD_SIGNAL(MethodInfo("caret_changed"));
  4156. /* Gutters. */
  4157. ADD_SIGNAL(MethodInfo("gutter_clicked", PropertyInfo(Variant::INT, "line"), PropertyInfo(Variant::INT, "gutter")));
  4158. ADD_SIGNAL(MethodInfo("gutter_added"));
  4159. ADD_SIGNAL(MethodInfo("gutter_removed"));
  4160. /* Settings. */
  4161. GLOBAL_DEF("gui/timers/text_edit_idle_detect_sec", 3);
  4162. ProjectSettings::get_singleton()->set_custom_property_info("gui/timers/text_edit_idle_detect_sec", PropertyInfo(Variant::FLOAT, "gui/timers/text_edit_idle_detect_sec", PROPERTY_HINT_RANGE, "0,10,0.01,or_greater")); // No negative numbers.
  4163. GLOBAL_DEF("gui/common/text_edit_undo_stack_max_size", 1024);
  4164. ProjectSettings::get_singleton()->set_custom_property_info("gui/common/text_edit_undo_stack_max_size", PropertyInfo(Variant::INT, "gui/common/text_edit_undo_stack_max_size", PROPERTY_HINT_RANGE, "0,10000,1,or_greater")); // No negative numbers.
  4165. }
  4166. bool TextEdit::_set(const StringName &p_name, const Variant &p_value) {
  4167. String str = p_name;
  4168. if (str.begins_with("opentype_features/")) {
  4169. String name = str.get_slicec('/', 1);
  4170. int32_t tag = TS->name_to_tag(name);
  4171. double value = p_value;
  4172. if (value == -1) {
  4173. if (opentype_features.has(tag)) {
  4174. opentype_features.erase(tag);
  4175. text.set_font_features(opentype_features);
  4176. text.invalidate_all();
  4177. update();
  4178. }
  4179. } else {
  4180. if ((double)opentype_features[tag] != value) {
  4181. opentype_features[tag] = value;
  4182. text.set_font_features(opentype_features);
  4183. text.invalidate_all();
  4184. update();
  4185. }
  4186. }
  4187. notify_property_list_changed();
  4188. return true;
  4189. }
  4190. return false;
  4191. }
  4192. bool TextEdit::_get(const StringName &p_name, Variant &r_ret) const {
  4193. String str = p_name;
  4194. if (str.begins_with("opentype_features/")) {
  4195. String name = str.get_slicec('/', 1);
  4196. int32_t tag = TS->name_to_tag(name);
  4197. if (opentype_features.has(tag)) {
  4198. r_ret = opentype_features[tag];
  4199. return true;
  4200. } else {
  4201. r_ret = -1;
  4202. return true;
  4203. }
  4204. }
  4205. return false;
  4206. }
  4207. void TextEdit::_get_property_list(List<PropertyInfo> *p_list) const {
  4208. for (const Variant *ftr = opentype_features.next(nullptr); ftr != nullptr; ftr = opentype_features.next(ftr)) {
  4209. String name = TS->tag_to_name(*ftr);
  4210. p_list->push_back(PropertyInfo(Variant::FLOAT, "opentype_features/" + name));
  4211. }
  4212. p_list->push_back(PropertyInfo(Variant::NIL, "opentype_features/_new", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_EDITOR));
  4213. }
  4214. /* Internal API for CodeEdit. */
  4215. // Line hiding.
  4216. void TextEdit::_set_hiding_enabled(bool p_enabled) {
  4217. if (!p_enabled) {
  4218. _unhide_all_lines();
  4219. }
  4220. hiding_enabled = p_enabled;
  4221. update();
  4222. }
  4223. bool TextEdit::_is_hiding_enabled() const {
  4224. return hiding_enabled;
  4225. }
  4226. bool TextEdit::_is_line_hidden(int p_line) const {
  4227. ERR_FAIL_INDEX_V(p_line, text.size(), false);
  4228. return text.is_hidden(p_line);
  4229. }
  4230. void TextEdit::_unhide_all_lines() {
  4231. for (int i = 0; i < text.size(); i++) {
  4232. text.set_hidden(i, false);
  4233. }
  4234. _update_scrollbars();
  4235. update();
  4236. }
  4237. void TextEdit::_set_line_as_hidden(int p_line, bool p_hidden) {
  4238. ERR_FAIL_INDEX(p_line, text.size());
  4239. if (_is_hiding_enabled() || !p_hidden) {
  4240. text.set_hidden(p_line, p_hidden);
  4241. }
  4242. update();
  4243. }
  4244. // Symbol lookup.
  4245. void TextEdit::_set_symbol_lookup_word(const String &p_symbol) {
  4246. lookup_symbol_word = p_symbol;
  4247. update();
  4248. }
  4249. /* Text manipulation */
  4250. // Overridable actions
  4251. void TextEdit::_handle_unicode_input_internal(const uint32_t p_unicode) {
  4252. if (!editable) {
  4253. return;
  4254. }
  4255. bool had_selection = has_selection();
  4256. if (had_selection) {
  4257. begin_complex_operation();
  4258. delete_selection();
  4259. }
  4260. /* Remove the old character if in insert mode and no selection. */
  4261. if (overtype_mode && !had_selection) {
  4262. begin_complex_operation();
  4263. /* Make sure we don't try and remove empty space. */
  4264. int cl = get_caret_line();
  4265. int cc = get_caret_column();
  4266. if (cc < get_line(cl).length()) {
  4267. _remove_text(cl, cc, cl, cc + 1);
  4268. }
  4269. }
  4270. const char32_t chr[2] = { (char32_t)p_unicode, 0 };
  4271. insert_text_at_caret(chr);
  4272. if ((overtype_mode && !had_selection) || (had_selection)) {
  4273. end_complex_operation();
  4274. }
  4275. }
  4276. void TextEdit::_backspace_internal() {
  4277. if (!editable) {
  4278. return;
  4279. }
  4280. if (has_selection()) {
  4281. delete_selection();
  4282. return;
  4283. }
  4284. int cc = get_caret_column();
  4285. int cl = get_caret_line();
  4286. if (cc == 0 && cl == 0) {
  4287. return;
  4288. }
  4289. int prev_line = cc ? cl : cl - 1;
  4290. int prev_column = cc ? (cc - 1) : (text[cl - 1].length());
  4291. merge_gutters(prev_line, cl);
  4292. if (_is_line_hidden(cl)) {
  4293. _set_line_as_hidden(prev_line, true);
  4294. }
  4295. _remove_text(prev_line, prev_column, cl, cc);
  4296. set_caret_line(prev_line, false, true);
  4297. set_caret_column(prev_column);
  4298. }
  4299. void TextEdit::_cut_internal() {
  4300. if (!editable) {
  4301. return;
  4302. }
  4303. if (has_selection()) {
  4304. DisplayServer::get_singleton()->clipboard_set(get_selected_text());
  4305. delete_selection();
  4306. cut_copy_line = "";
  4307. return;
  4308. }
  4309. int cl = get_caret_line();
  4310. int cc = get_caret_column();
  4311. int indent_level = get_indent_level(cl);
  4312. double hscroll = get_h_scroll();
  4313. String clipboard = text[cl];
  4314. DisplayServer::get_singleton()->clipboard_set(clipboard);
  4315. set_caret_column(0);
  4316. if (cl == 0 && get_line_count() > 1) {
  4317. _remove_text(cl, 0, cl + 1, 0);
  4318. } else {
  4319. _remove_text(cl, 0, cl, text[cl].length());
  4320. backspace();
  4321. set_caret_line(get_caret_line() + 1);
  4322. }
  4323. // Correct the visualy perceived caret column taking care of identation level of the lines.
  4324. int diff_indent = indent_level - get_indent_level(get_caret_line());
  4325. cc += diff_indent;
  4326. if (diff_indent != 0) {
  4327. cc += diff_indent > 0 ? -1 : 1;
  4328. }
  4329. // Restore horizontal scroll and caret column modified by the backspace() call.
  4330. set_h_scroll(hscroll);
  4331. set_caret_column(cc);
  4332. cut_copy_line = clipboard;
  4333. }
  4334. void TextEdit::_copy_internal() {
  4335. if (has_selection()) {
  4336. DisplayServer::get_singleton()->clipboard_set(get_selected_text());
  4337. cut_copy_line = "";
  4338. return;
  4339. }
  4340. int cl = get_caret_line();
  4341. if (text[cl].length() != 0) {
  4342. String clipboard = _base_get_text(cl, 0, cl, text[cl].length());
  4343. DisplayServer::get_singleton()->clipboard_set(clipboard);
  4344. cut_copy_line = clipboard;
  4345. }
  4346. }
  4347. void TextEdit::_paste_internal() {
  4348. if (!editable) {
  4349. return;
  4350. }
  4351. String clipboard = DisplayServer::get_singleton()->clipboard_get();
  4352. begin_complex_operation();
  4353. if (has_selection()) {
  4354. delete_selection();
  4355. } else if (!cut_copy_line.is_empty() && cut_copy_line == clipboard) {
  4356. set_caret_column(0);
  4357. String ins = "\n";
  4358. clipboard += ins;
  4359. }
  4360. insert_text_at_caret(clipboard);
  4361. end_complex_operation();
  4362. }
  4363. /* Text. */
  4364. // Context menu.
  4365. void TextEdit::_generate_context_menu() {
  4366. if (!menu) {
  4367. menu = memnew(PopupMenu);
  4368. add_child(menu, false, INTERNAL_MODE_FRONT);
  4369. menu_dir = memnew(PopupMenu);
  4370. menu_dir->set_name("DirMenu");
  4371. menu_dir->add_radio_check_item(RTR("Same as layout direction"), MENU_DIR_INHERITED);
  4372. menu_dir->add_radio_check_item(RTR("Auto-detect direction"), MENU_DIR_AUTO);
  4373. menu_dir->add_radio_check_item(RTR("Left-to-right"), MENU_DIR_LTR);
  4374. menu_dir->add_radio_check_item(RTR("Right-to-left"), MENU_DIR_RTL);
  4375. menu->add_child(menu_dir, false, INTERNAL_MODE_FRONT);
  4376. menu_ctl = memnew(PopupMenu);
  4377. menu_ctl->set_name("CTLMenu");
  4378. menu_ctl->add_item(RTR("Left-to-right mark (LRM)"), MENU_INSERT_LRM);
  4379. menu_ctl->add_item(RTR("Right-to-left mark (RLM)"), MENU_INSERT_RLM);
  4380. menu_ctl->add_item(RTR("Start of left-to-right embedding (LRE)"), MENU_INSERT_LRE);
  4381. menu_ctl->add_item(RTR("Start of right-to-left embedding (RLE)"), MENU_INSERT_RLE);
  4382. menu_ctl->add_item(RTR("Start of left-to-right override (LRO)"), MENU_INSERT_LRO);
  4383. menu_ctl->add_item(RTR("Start of right-to-left override (RLO)"), MENU_INSERT_RLO);
  4384. menu_ctl->add_item(RTR("Pop direction formatting (PDF)"), MENU_INSERT_PDF);
  4385. menu_ctl->add_separator();
  4386. menu_ctl->add_item(RTR("Arabic letter mark (ALM)"), MENU_INSERT_ALM);
  4387. menu_ctl->add_item(RTR("Left-to-right isolate (LRI)"), MENU_INSERT_LRI);
  4388. menu_ctl->add_item(RTR("Right-to-left isolate (RLI)"), MENU_INSERT_RLI);
  4389. menu_ctl->add_item(RTR("First strong isolate (FSI)"), MENU_INSERT_FSI);
  4390. menu_ctl->add_item(RTR("Pop direction isolate (PDI)"), MENU_INSERT_PDI);
  4391. menu_ctl->add_separator();
  4392. menu_ctl->add_item(RTR("Zero width joiner (ZWJ)"), MENU_INSERT_ZWJ);
  4393. menu_ctl->add_item(RTR("Zero width non-joiner (ZWNJ)"), MENU_INSERT_ZWNJ);
  4394. menu_ctl->add_item(RTR("Word joiner (WJ)"), MENU_INSERT_WJ);
  4395. menu_ctl->add_item(RTR("Soft hyphen (SHY)"), MENU_INSERT_SHY);
  4396. menu->add_child(menu_ctl, false, INTERNAL_MODE_FRONT);
  4397. menu->connect("id_pressed", callable_mp(this, &TextEdit::menu_option));
  4398. menu_dir->connect("id_pressed", callable_mp(this, &TextEdit::menu_option));
  4399. menu_ctl->connect("id_pressed", callable_mp(this, &TextEdit::menu_option));
  4400. }
  4401. // Reorganize context menu.
  4402. menu->clear();
  4403. if (editable) {
  4404. menu->add_item(RTR("Cut"), MENU_CUT, is_shortcut_keys_enabled() ? _get_menu_action_accelerator("ui_cut") : 0);
  4405. }
  4406. menu->add_item(RTR("Copy"), MENU_COPY, is_shortcut_keys_enabled() ? _get_menu_action_accelerator("ui_copy") : 0);
  4407. if (editable) {
  4408. menu->add_item(RTR("Paste"), MENU_PASTE, is_shortcut_keys_enabled() ? _get_menu_action_accelerator("ui_paste") : 0);
  4409. }
  4410. menu->add_separator();
  4411. if (is_selecting_enabled()) {
  4412. menu->add_item(RTR("Select All"), MENU_SELECT_ALL, is_shortcut_keys_enabled() ? _get_menu_action_accelerator("ui_text_select_all") : 0);
  4413. }
  4414. if (editable) {
  4415. menu->add_item(RTR("Clear"), MENU_CLEAR);
  4416. menu->add_separator();
  4417. menu->add_item(RTR("Undo"), MENU_UNDO, is_shortcut_keys_enabled() ? _get_menu_action_accelerator("ui_undo") : 0);
  4418. menu->add_item(RTR("Redo"), MENU_REDO, is_shortcut_keys_enabled() ? _get_menu_action_accelerator("ui_redo") : 0);
  4419. }
  4420. menu->add_separator();
  4421. menu->add_submenu_item(RTR("Text writing direction"), "DirMenu");
  4422. menu->add_separator();
  4423. menu->add_check_item(RTR("Display control characters"), MENU_DISPLAY_UCC);
  4424. menu->set_item_checked(menu->get_item_index(MENU_DISPLAY_UCC), draw_control_chars);
  4425. if (editable) {
  4426. menu->add_submenu_item(RTR("Insert control character"), "CTLMenu");
  4427. }
  4428. menu_dir->set_item_checked(menu_dir->get_item_index(MENU_DIR_INHERITED), text_direction == TEXT_DIRECTION_INHERITED);
  4429. menu_dir->set_item_checked(menu_dir->get_item_index(MENU_DIR_AUTO), text_direction == TEXT_DIRECTION_AUTO);
  4430. menu_dir->set_item_checked(menu_dir->get_item_index(MENU_DIR_LTR), text_direction == TEXT_DIRECTION_LTR);
  4431. menu_dir->set_item_checked(menu_dir->get_item_index(MENU_DIR_RTL), text_direction == TEXT_DIRECTION_RTL);
  4432. if (editable) {
  4433. menu->set_item_disabled(menu->get_item_index(MENU_UNDO), !has_undo());
  4434. menu->set_item_disabled(menu->get_item_index(MENU_REDO), !has_redo());
  4435. }
  4436. }
  4437. int TextEdit::_get_menu_action_accelerator(const String &p_action) {
  4438. const List<Ref<InputEvent>> *events = InputMap::get_singleton()->action_get_events(p_action);
  4439. if (!events) {
  4440. return 0;
  4441. }
  4442. // Use first event in the list for the accelerator.
  4443. const List<Ref<InputEvent>>::Element *first_event = events->front();
  4444. if (!first_event) {
  4445. return 0;
  4446. }
  4447. const Ref<InputEventKey> event = first_event->get();
  4448. if (event.is_null()) {
  4449. return 0;
  4450. }
  4451. // Use physical keycode if non-zero
  4452. if (event->get_physical_keycode() != 0) {
  4453. return event->get_physical_keycode_with_modifiers();
  4454. } else {
  4455. return event->get_keycode_with_modifiers();
  4456. }
  4457. }
  4458. /* Versioning */
  4459. void TextEdit::_push_current_op() {
  4460. if (current_op.type == TextOperation::TYPE_NONE) {
  4461. return; // Nothing to do.
  4462. }
  4463. if (next_operation_is_complex) {
  4464. current_op.chain_forward = true;
  4465. next_operation_is_complex = false;
  4466. }
  4467. undo_stack.push_back(current_op);
  4468. current_op.type = TextOperation::TYPE_NONE;
  4469. current_op.text = "";
  4470. current_op.chain_forward = false;
  4471. if (undo_stack.size() > undo_stack_max_size) {
  4472. undo_stack.pop_front();
  4473. }
  4474. }
  4475. void TextEdit::_do_text_op(const TextOperation &p_op, bool p_reverse) {
  4476. ERR_FAIL_COND(p_op.type == TextOperation::TYPE_NONE);
  4477. bool insert = p_op.type == TextOperation::TYPE_INSERT;
  4478. if (p_reverse) {
  4479. insert = !insert;
  4480. }
  4481. if (insert) {
  4482. int check_line;
  4483. int check_column;
  4484. _base_insert_text(p_op.from_line, p_op.from_column, p_op.text, check_line, check_column);
  4485. ERR_FAIL_COND(check_line != p_op.to_line); // BUG.
  4486. ERR_FAIL_COND(check_column != p_op.to_column); // BUG.
  4487. } else {
  4488. _base_remove_text(p_op.from_line, p_op.from_column, p_op.to_line, p_op.to_column);
  4489. }
  4490. }
  4491. void TextEdit::_clear_redo() {
  4492. if (undo_stack_pos == nullptr) {
  4493. return; // Nothing to clear.
  4494. }
  4495. _push_current_op();
  4496. while (undo_stack_pos) {
  4497. List<TextOperation>::Element *elem = undo_stack_pos;
  4498. undo_stack_pos = undo_stack_pos->next();
  4499. undo_stack.erase(elem);
  4500. }
  4501. }
  4502. /* Search */
  4503. int TextEdit::_get_column_pos_of_word(const String &p_key, const String &p_search, uint32_t p_search_flags, int p_from_column) const {
  4504. int col = -1;
  4505. if (p_key.length() > 0 && p_search.length() > 0) {
  4506. if (p_from_column < 0 || p_from_column > p_search.length()) {
  4507. p_from_column = 0;
  4508. }
  4509. while (col == -1 && p_from_column <= p_search.length()) {
  4510. if (p_search_flags & SEARCH_MATCH_CASE) {
  4511. col = p_search.find(p_key, p_from_column);
  4512. } else {
  4513. col = p_search.findn(p_key, p_from_column);
  4514. }
  4515. // Whole words only.
  4516. if (col != -1 && p_search_flags & SEARCH_WHOLE_WORDS) {
  4517. p_from_column = col;
  4518. if (col > 0 && _is_text_char(p_search[col - 1])) {
  4519. col = -1;
  4520. } else if ((col + p_key.length()) < p_search.length() && _is_text_char(p_search[col + p_key.length()])) {
  4521. col = -1;
  4522. }
  4523. }
  4524. p_from_column += 1;
  4525. }
  4526. }
  4527. return col;
  4528. }
  4529. /* Mouse */
  4530. int TextEdit::_get_char_pos_for_line(int p_px, int p_line, int p_wrap_index) const {
  4531. ERR_FAIL_INDEX_V(p_line, text.size(), 0);
  4532. p_wrap_index = MIN(p_wrap_index, text.get_line_data(p_line)->get_line_count() - 1);
  4533. RID text_rid = text.get_line_data(p_line)->get_line_rid(p_wrap_index);
  4534. if (is_layout_rtl()) {
  4535. p_px = TS->shaped_text_get_size(text_rid).x - p_px;
  4536. }
  4537. return TS->shaped_text_hit_test_position(text_rid, p_px);
  4538. }
  4539. /* Caret */
  4540. void TextEdit::_emit_caret_changed() {
  4541. emit_signal(SNAME("caret_changed"));
  4542. caret_pos_dirty = false;
  4543. }
  4544. void TextEdit::_reset_caret_blink_timer() {
  4545. if (!caret_blink_enabled) {
  4546. return;
  4547. }
  4548. draw_caret = true;
  4549. if (has_focus()) {
  4550. caret_blink_timer->stop();
  4551. caret_blink_timer->start();
  4552. update();
  4553. }
  4554. }
  4555. void TextEdit::_toggle_draw_caret() {
  4556. draw_caret = !draw_caret;
  4557. if (is_visible_in_tree() && has_focus() && window_has_focus) {
  4558. update();
  4559. }
  4560. }
  4561. int TextEdit::_get_column_x_offset_for_line(int p_char, int p_line) const {
  4562. ERR_FAIL_INDEX_V(p_line, text.size(), 0);
  4563. int row = 0;
  4564. Vector<Vector2i> rows2 = text.get_line_wrap_ranges(p_line);
  4565. for (int i = 0; i < rows2.size(); i++) {
  4566. if ((p_char >= rows2[i].x) && (p_char < rows2[i].y)) {
  4567. row = i;
  4568. break;
  4569. }
  4570. }
  4571. RID text_rid = text.get_line_data(p_line)->get_line_rid(row);
  4572. CaretInfo ts_caret = TS->shaped_text_get_carets(text_rid, caret.column);
  4573. if ((ts_caret.l_caret != Rect2() && (ts_caret.l_dir == TextServer::DIRECTION_AUTO || ts_caret.l_dir == (TextServer::Direction)input_direction)) || (ts_caret.t_caret == Rect2())) {
  4574. return ts_caret.l_caret.position.x;
  4575. } else {
  4576. return ts_caret.t_caret.position.x;
  4577. }
  4578. }
  4579. /* Selection */
  4580. void TextEdit::_click_selection_held() {
  4581. // Warning: is_mouse_button_pressed(MOUSE_BUTTON_LEFT) returns false for double+ clicks, so this doesn't work for MODE_WORD
  4582. // and MODE_LINE. However, moving the mouse triggers _gui_input, which calls these functions too, so that's not a huge problem.
  4583. // I'm unsure if there's an actual fix that doesn't have a ton of side effects.
  4584. if (Input::get_singleton()->is_mouse_button_pressed(MOUSE_BUTTON_LEFT) && selection.selecting_mode != SelectionMode::SELECTION_MODE_NONE) {
  4585. switch (selection.selecting_mode) {
  4586. case SelectionMode::SELECTION_MODE_POINTER: {
  4587. _update_selection_mode_pointer();
  4588. } break;
  4589. case SelectionMode::SELECTION_MODE_WORD: {
  4590. _update_selection_mode_word();
  4591. } break;
  4592. case SelectionMode::SELECTION_MODE_LINE: {
  4593. _update_selection_mode_line();
  4594. } break;
  4595. default: {
  4596. break;
  4597. }
  4598. }
  4599. } else {
  4600. click_select_held->stop();
  4601. }
  4602. }
  4603. void TextEdit::_update_selection_mode_pointer() {
  4604. dragging_selection = true;
  4605. Point2 mp = get_local_mouse_pos();
  4606. Point2i pos = get_line_column_at_pos(mp);
  4607. int line = pos.y;
  4608. int col = pos.x;
  4609. select(selection.selecting_line, selection.selecting_column, line, col);
  4610. set_caret_line(line, false);
  4611. set_caret_column(col);
  4612. update();
  4613. click_select_held->start();
  4614. }
  4615. void TextEdit::_update_selection_mode_word() {
  4616. dragging_selection = true;
  4617. Point2 mp = get_local_mouse_pos();
  4618. Point2i pos = get_line_column_at_pos(mp);
  4619. int line = pos.y;
  4620. int col = pos.x;
  4621. int caret_pos = CLAMP(col, 0, text[line].length());
  4622. int beg = caret_pos;
  4623. int end = beg;
  4624. PackedInt32Array words = TS->shaped_text_get_word_breaks(text.get_line_data(line)->get_rid());
  4625. for (int i = 0; i < words.size(); i = i + 2) {
  4626. if ((words[i] < caret_pos && words[i + 1] > caret_pos) || (i == words.size() - 2 && caret_pos == words[i + 1])) {
  4627. beg = words[i];
  4628. end = words[i + 1];
  4629. break;
  4630. }
  4631. }
  4632. /* Initial selection. */
  4633. if (!selection.active) {
  4634. select(line, beg, line, end);
  4635. selection.selecting_column = beg;
  4636. selection.selected_word_beg = beg;
  4637. selection.selected_word_end = end;
  4638. selection.selected_word_origin = beg;
  4639. set_caret_line(selection.to_line, false);
  4640. set_caret_column(selection.to_column);
  4641. } else {
  4642. if ((col <= selection.selected_word_origin && line == selection.selecting_line) || line < selection.selecting_line) {
  4643. selection.selecting_column = selection.selected_word_end;
  4644. select(line, beg, selection.selecting_line, selection.selected_word_end);
  4645. set_caret_line(selection.from_line, false);
  4646. set_caret_column(selection.from_column);
  4647. } else {
  4648. selection.selecting_column = selection.selected_word_beg;
  4649. select(selection.selecting_line, selection.selected_word_beg, line, end);
  4650. set_caret_line(selection.to_line, false);
  4651. set_caret_column(selection.to_column);
  4652. }
  4653. }
  4654. update();
  4655. click_select_held->start();
  4656. }
  4657. void TextEdit::_update_selection_mode_line() {
  4658. dragging_selection = true;
  4659. Point2 mp = get_local_mouse_pos();
  4660. Point2i pos = get_line_column_at_pos(mp);
  4661. int line = pos.y;
  4662. int col = pos.x;
  4663. col = 0;
  4664. if (line < selection.selecting_line) {
  4665. /* Caret is above us. */
  4666. set_caret_line(line - 1, false);
  4667. selection.selecting_column = text[selection.selecting_line].length();
  4668. } else {
  4669. /* Caret is below us. */
  4670. set_caret_line(line + 1, false);
  4671. selection.selecting_column = 0;
  4672. col = text[line].length();
  4673. }
  4674. set_caret_column(0);
  4675. select(selection.selecting_line, selection.selecting_column, line, col);
  4676. update();
  4677. click_select_held->start();
  4678. }
  4679. void TextEdit::_pre_shift_selection() {
  4680. if (!selection.active || selection.selecting_mode == SelectionMode::SELECTION_MODE_NONE) {
  4681. selection.selecting_line = caret.line;
  4682. selection.selecting_column = caret.column;
  4683. selection.active = true;
  4684. }
  4685. selection.selecting_mode = SelectionMode::SELECTION_MODE_SHIFT;
  4686. }
  4687. void TextEdit::_post_shift_selection() {
  4688. if (selection.active && selection.selecting_mode == SelectionMode::SELECTION_MODE_SHIFT) {
  4689. select(selection.selecting_line, selection.selecting_column, caret.line, caret.column);
  4690. update();
  4691. }
  4692. selection.selecting_text = true;
  4693. }
  4694. /* Line Wrapping */
  4695. void TextEdit::_update_wrap_at_column(bool p_force) {
  4696. int new_wrap_at = get_size().width - style_normal->get_minimum_size().width - gutters_width - gutter_padding;
  4697. if (draw_minimap) {
  4698. new_wrap_at -= minimap_width;
  4699. }
  4700. if (v_scroll->is_visible_in_tree()) {
  4701. new_wrap_at -= v_scroll->get_combined_minimum_size().width;
  4702. }
  4703. /* Give it a little more space. */
  4704. new_wrap_at -= wrap_right_offset;
  4705. if ((wrap_at_column != new_wrap_at) || p_force) {
  4706. wrap_at_column = new_wrap_at;
  4707. if (line_wrapping_mode) {
  4708. text.set_width(wrap_at_column);
  4709. } else {
  4710. text.set_width(-1);
  4711. }
  4712. text.invalidate_all_lines();
  4713. }
  4714. _update_caret_wrap_offset();
  4715. }
  4716. void TextEdit::_update_caret_wrap_offset() {
  4717. int first_vis_line = get_first_visible_line();
  4718. if (is_line_wrapped(first_vis_line)) {
  4719. caret.wrap_ofs = MIN(caret.wrap_ofs, get_line_wrap_count(first_vis_line));
  4720. } else {
  4721. caret.wrap_ofs = 0;
  4722. }
  4723. set_line_as_first_visible(caret.line_ofs, caret.wrap_ofs);
  4724. }
  4725. /* Viewport. */
  4726. void TextEdit::_update_scrollbars() {
  4727. Size2 size = get_size();
  4728. Size2 hmin = h_scroll->get_combined_minimum_size();
  4729. Size2 vmin = v_scroll->get_combined_minimum_size();
  4730. v_scroll->set_begin(Point2(size.width - vmin.width, style_normal->get_margin(SIDE_TOP)));
  4731. v_scroll->set_end(Point2(size.width, size.height - style_normal->get_margin(SIDE_TOP) - style_normal->get_margin(SIDE_BOTTOM)));
  4732. h_scroll->set_begin(Point2(0, size.height - hmin.height));
  4733. h_scroll->set_end(Point2(size.width - vmin.width, size.height));
  4734. int visible_rows = get_visible_line_count();
  4735. int total_rows = get_total_visible_line_count();
  4736. if (scroll_past_end_of_file_enabled) {
  4737. total_rows += visible_rows - 1;
  4738. }
  4739. int visible_width = size.width - style_normal->get_minimum_size().width;
  4740. int total_width = text.get_max_width() + vmin.x + gutters_width + gutter_padding;
  4741. if (draw_minimap) {
  4742. total_width += minimap_width;
  4743. }
  4744. updating_scrolls = true;
  4745. if (total_rows > visible_rows) {
  4746. v_scroll->show();
  4747. v_scroll->set_max(total_rows + _get_visible_lines_offset());
  4748. v_scroll->set_page(visible_rows + _get_visible_lines_offset());
  4749. if (smooth_scroll_enabled) {
  4750. v_scroll->set_step(0.25);
  4751. } else {
  4752. v_scroll->set_step(1);
  4753. }
  4754. set_v_scroll(get_v_scroll());
  4755. } else {
  4756. caret.line_ofs = 0;
  4757. caret.wrap_ofs = 0;
  4758. v_scroll->set_value(0);
  4759. v_scroll->hide();
  4760. }
  4761. if (total_width > visible_width && get_line_wrapping_mode() == LineWrappingMode::LINE_WRAPPING_NONE) {
  4762. h_scroll->show();
  4763. h_scroll->set_max(total_width);
  4764. h_scroll->set_page(visible_width);
  4765. if (caret.x_ofs > (total_width - visible_width)) {
  4766. caret.x_ofs = (total_width - visible_width);
  4767. }
  4768. if (fabs(h_scroll->get_value() - (double)caret.x_ofs) >= 1) {
  4769. h_scroll->set_value(caret.x_ofs);
  4770. }
  4771. } else {
  4772. caret.x_ofs = 0;
  4773. h_scroll->set_value(0);
  4774. h_scroll->hide();
  4775. }
  4776. updating_scrolls = false;
  4777. }
  4778. int TextEdit::_get_control_height() const {
  4779. int control_height = get_size().height;
  4780. control_height -= style_normal->get_minimum_size().height;
  4781. if (h_scroll->is_visible_in_tree()) {
  4782. control_height -= h_scroll->get_size().height;
  4783. }
  4784. return control_height;
  4785. }
  4786. void TextEdit::_v_scroll_input() {
  4787. scrolling = false;
  4788. minimap_clicked = false;
  4789. }
  4790. void TextEdit::_scroll_moved(double p_to_val) {
  4791. if (updating_scrolls) {
  4792. return;
  4793. }
  4794. if (h_scroll->is_visible_in_tree()) {
  4795. caret.x_ofs = h_scroll->get_value();
  4796. }
  4797. if (v_scroll->is_visible_in_tree()) {
  4798. // Set line ofs and wrap ofs.
  4799. int v_scroll_i = floor(get_v_scroll());
  4800. int sc = 0;
  4801. int n_line;
  4802. for (n_line = 0; n_line < text.size(); n_line++) {
  4803. if (!_is_line_hidden(n_line)) {
  4804. sc++;
  4805. sc += get_line_wrap_count(n_line);
  4806. if (sc > v_scroll_i) {
  4807. break;
  4808. }
  4809. }
  4810. }
  4811. n_line = MIN(n_line, text.size() - 1);
  4812. int line_wrap_amount = get_line_wrap_count(n_line);
  4813. int wi = line_wrap_amount - (sc - v_scroll_i - 1);
  4814. wi = CLAMP(wi, 0, line_wrap_amount);
  4815. caret.line_ofs = n_line;
  4816. caret.wrap_ofs = wi;
  4817. }
  4818. update();
  4819. }
  4820. double TextEdit::_get_visible_lines_offset() const {
  4821. double total = _get_control_height();
  4822. total /= (double)get_line_height();
  4823. total = total - floor(total);
  4824. total = -CLAMP(total, 0.001, 1) + 1;
  4825. return total;
  4826. }
  4827. double TextEdit::_get_v_scroll_offset() const {
  4828. double val = get_v_scroll() - floor(get_v_scroll());
  4829. return CLAMP(val, 0, 1);
  4830. }
  4831. void TextEdit::_scroll_up(real_t p_delta) {
  4832. if (scrolling && smooth_scroll_enabled && SGN(target_v_scroll - v_scroll->get_value()) != SGN(-p_delta)) {
  4833. scrolling = false;
  4834. minimap_clicked = false;
  4835. }
  4836. if (scrolling) {
  4837. target_v_scroll = (target_v_scroll - p_delta);
  4838. } else {
  4839. target_v_scroll = (get_v_scroll() - p_delta);
  4840. }
  4841. if (smooth_scroll_enabled) {
  4842. if (target_v_scroll <= 0) {
  4843. target_v_scroll = 0;
  4844. }
  4845. if (Math::abs(target_v_scroll - v_scroll->get_value()) < 1.0) {
  4846. v_scroll->set_value(target_v_scroll);
  4847. } else {
  4848. scrolling = true;
  4849. set_physics_process_internal(true);
  4850. }
  4851. } else {
  4852. set_v_scroll(target_v_scroll);
  4853. }
  4854. }
  4855. void TextEdit::_scroll_down(real_t p_delta) {
  4856. if (scrolling && smooth_scroll_enabled && SGN(target_v_scroll - v_scroll->get_value()) != SGN(p_delta)) {
  4857. scrolling = false;
  4858. minimap_clicked = false;
  4859. }
  4860. if (scrolling) {
  4861. target_v_scroll = (target_v_scroll + p_delta);
  4862. } else {
  4863. target_v_scroll = (get_v_scroll() + p_delta);
  4864. }
  4865. if (smooth_scroll_enabled) {
  4866. int max_v_scroll = round(v_scroll->get_max() - v_scroll->get_page());
  4867. if (target_v_scroll > max_v_scroll) {
  4868. target_v_scroll = max_v_scroll;
  4869. }
  4870. if (Math::abs(target_v_scroll - v_scroll->get_value()) < 1.0) {
  4871. v_scroll->set_value(target_v_scroll);
  4872. } else {
  4873. scrolling = true;
  4874. set_physics_process_internal(true);
  4875. }
  4876. } else {
  4877. set_v_scroll(target_v_scroll);
  4878. }
  4879. }
  4880. void TextEdit::_scroll_lines_up() {
  4881. scrolling = false;
  4882. minimap_clicked = false;
  4883. // Adjust the vertical scroll.
  4884. set_v_scroll(get_v_scroll() - 1);
  4885. // Adjust the caret to viewport.
  4886. if (!selection.active) {
  4887. int cur_line = caret.line;
  4888. int cur_wrap = get_caret_wrap_index();
  4889. int last_vis_line = get_last_full_visible_line();
  4890. int last_vis_wrap = get_last_full_visible_line_wrap_index();
  4891. if (cur_line > last_vis_line || (cur_line == last_vis_line && cur_wrap > last_vis_wrap)) {
  4892. set_caret_line(last_vis_line, false, false, last_vis_wrap);
  4893. }
  4894. }
  4895. }
  4896. void TextEdit::_scroll_lines_down() {
  4897. scrolling = false;
  4898. minimap_clicked = false;
  4899. // Adjust the vertical scroll.
  4900. set_v_scroll(get_v_scroll() + 1);
  4901. // Adjust the caret to viewport.
  4902. if (!selection.active) {
  4903. int cur_line = caret.line;
  4904. int cur_wrap = get_caret_wrap_index();
  4905. int first_vis_line = get_first_visible_line();
  4906. int first_vis_wrap = caret.wrap_ofs;
  4907. if (cur_line < first_vis_line || (cur_line == first_vis_line && cur_wrap < first_vis_wrap)) {
  4908. set_caret_line(first_vis_line, false, false, first_vis_wrap);
  4909. }
  4910. }
  4911. }
  4912. // Minimap
  4913. void TextEdit::_update_minimap_hover() {
  4914. const Point2 mp = get_local_mouse_pos();
  4915. const int xmargin_end = get_size().width - style_normal->get_margin(SIDE_RIGHT);
  4916. const bool hovering_sidebar = mp.x > xmargin_end - minimap_width && mp.x < xmargin_end;
  4917. if (!hovering_sidebar) {
  4918. if (hovering_minimap) {
  4919. // Only redraw if the hovering status changed.
  4920. hovering_minimap = false;
  4921. update();
  4922. }
  4923. // Return early to avoid running the operations below when not needed.
  4924. return;
  4925. }
  4926. const int row = get_minimap_line_at_pos(mp);
  4927. const bool new_hovering_minimap = row >= get_first_visible_line() && row <= get_last_full_visible_line();
  4928. if (new_hovering_minimap != hovering_minimap) {
  4929. // Only redraw if the hovering status changed.
  4930. hovering_minimap = new_hovering_minimap;
  4931. update();
  4932. }
  4933. }
  4934. void TextEdit::_update_minimap_click() {
  4935. Point2 mp = get_local_mouse_pos();
  4936. int xmargin_end = get_size().width - style_normal->get_margin(SIDE_RIGHT);
  4937. if (!dragging_minimap && (mp.x < xmargin_end - minimap_width || mp.y > xmargin_end)) {
  4938. minimap_clicked = false;
  4939. return;
  4940. }
  4941. minimap_clicked = true;
  4942. dragging_minimap = true;
  4943. int row = get_minimap_line_at_pos(mp);
  4944. if (row >= get_first_visible_line() && (row < get_last_full_visible_line() || row >= (text.size() - 1))) {
  4945. minimap_scroll_ratio = v_scroll->get_as_ratio();
  4946. minimap_scroll_click_pos = mp.y;
  4947. can_drag_minimap = true;
  4948. return;
  4949. }
  4950. Point2i next_line = get_next_visible_line_index_offset_from(row, 0, -get_visible_line_count() / 2);
  4951. int first_line = row - next_line.x + 1;
  4952. double delta = get_scroll_pos_for_line(first_line, next_line.y) - get_v_scroll();
  4953. if (delta < 0) {
  4954. _scroll_up(-delta);
  4955. } else {
  4956. _scroll_down(delta);
  4957. }
  4958. }
  4959. void TextEdit::_update_minimap_drag() {
  4960. if (!can_drag_minimap) {
  4961. return;
  4962. }
  4963. int control_height = _get_control_height();
  4964. int scroll_height = v_scroll->get_max() * (minimap_char_size.y + minimap_line_spacing);
  4965. if (control_height > scroll_height) {
  4966. control_height = scroll_height;
  4967. }
  4968. Point2 mp = get_local_mouse_pos();
  4969. double diff = (mp.y - minimap_scroll_click_pos) / control_height;
  4970. v_scroll->set_as_ratio(minimap_scroll_ratio + diff);
  4971. }
  4972. /* Gutters. */
  4973. void TextEdit::_update_gutter_width() {
  4974. gutters_width = 0;
  4975. for (int i = 0; i < gutters.size(); i++) {
  4976. if (gutters[i].draw) {
  4977. gutters_width += gutters[i].width;
  4978. }
  4979. }
  4980. if (gutters_width > 0) {
  4981. gutter_padding = 2;
  4982. }
  4983. update();
  4984. }
  4985. /* Syntax highlighting. */
  4986. Dictionary TextEdit::_get_line_syntax_highlighting(int p_line) {
  4987. return syntax_highlighter.is_null() && !setting_text ? Dictionary() : syntax_highlighter->get_line_syntax_highlighting(p_line);
  4988. }
  4989. /*** Super internal Core API. Everything builds on it. ***/
  4990. void TextEdit::_text_changed_emit() {
  4991. emit_signal(SNAME("text_changed"));
  4992. text_changed_dirty = false;
  4993. }
  4994. void TextEdit::_insert_text(int p_line, int p_char, const String &p_text, int *r_end_line, int *r_end_char) {
  4995. if (!setting_text && idle_detect->is_inside_tree()) {
  4996. idle_detect->start();
  4997. }
  4998. if (undo_enabled) {
  4999. _clear_redo();
  5000. }
  5001. int retline, retchar;
  5002. _base_insert_text(p_line, p_char, p_text, retline, retchar);
  5003. if (r_end_line) {
  5004. *r_end_line = retline;
  5005. }
  5006. if (r_end_char) {
  5007. *r_end_char = retchar;
  5008. }
  5009. if (!undo_enabled) {
  5010. return;
  5011. }
  5012. /* UNDO!! */
  5013. TextOperation op;
  5014. op.type = TextOperation::TYPE_INSERT;
  5015. op.from_line = p_line;
  5016. op.from_column = p_char;
  5017. op.to_line = retline;
  5018. op.to_column = retchar;
  5019. op.text = p_text;
  5020. op.version = ++version;
  5021. op.chain_forward = false;
  5022. op.chain_backward = false;
  5023. // See if it should just be set as current op.
  5024. if (current_op.type != op.type) {
  5025. op.prev_version = get_version();
  5026. _push_current_op();
  5027. current_op = op;
  5028. return; // Set as current op, return.
  5029. }
  5030. // See if it can be merged.
  5031. if (current_op.to_line != p_line || current_op.to_column != p_char) {
  5032. op.prev_version = get_version();
  5033. _push_current_op();
  5034. current_op = op;
  5035. return; // Set as current op, return.
  5036. }
  5037. // Merge current op.
  5038. current_op.text += p_text;
  5039. current_op.to_column = retchar;
  5040. current_op.to_line = retline;
  5041. current_op.version = op.version;
  5042. }
  5043. void TextEdit::_remove_text(int p_from_line, int p_from_column, int p_to_line, int p_to_column) {
  5044. if (!setting_text && idle_detect->is_inside_tree()) {
  5045. idle_detect->start();
  5046. }
  5047. String text;
  5048. if (undo_enabled) {
  5049. _clear_redo();
  5050. text = _base_get_text(p_from_line, p_from_column, p_to_line, p_to_column);
  5051. }
  5052. _base_remove_text(p_from_line, p_from_column, p_to_line, p_to_column);
  5053. if (!undo_enabled) {
  5054. return;
  5055. }
  5056. /* UNDO! */
  5057. TextOperation op;
  5058. op.type = TextOperation::TYPE_REMOVE;
  5059. op.from_line = p_from_line;
  5060. op.from_column = p_from_column;
  5061. op.to_line = p_to_line;
  5062. op.to_column = p_to_column;
  5063. op.text = text;
  5064. op.version = ++version;
  5065. op.chain_forward = false;
  5066. op.chain_backward = false;
  5067. // See if it should just be set as current op.
  5068. if (current_op.type != op.type) {
  5069. op.prev_version = get_version();
  5070. _push_current_op();
  5071. current_op = op;
  5072. return; // Set as current op, return.
  5073. }
  5074. // See if it can be merged.
  5075. if (current_op.from_line == p_to_line && current_op.from_column == p_to_column) {
  5076. // Backspace or similar.
  5077. current_op.text = text + current_op.text;
  5078. current_op.from_line = p_from_line;
  5079. current_op.from_column = p_from_column;
  5080. return; // Update current op.
  5081. }
  5082. op.prev_version = get_version();
  5083. _push_current_op();
  5084. current_op = op;
  5085. }
  5086. void TextEdit::_base_insert_text(int p_line, int p_char, const String &p_text, int &r_end_line, int &r_end_column) {
  5087. // Save for undo.
  5088. ERR_FAIL_INDEX(p_line, text.size());
  5089. ERR_FAIL_COND(p_char < 0);
  5090. /* STEP 1: Remove \r from source text and separate in substrings. */
  5091. Vector<String> substrings = p_text.replace("\r", "").split("\n");
  5092. // Is this just a new empty line?
  5093. bool shift_first_line = p_char == 0 && p_text.replace("\r", "") == "\n";
  5094. /* STEP 2: Add spaces if the char is greater than the end of the line. */
  5095. while (p_char > text[p_line].length()) {
  5096. text.set(p_line, text[p_line] + String::chr(' '), structured_text_parser(st_parser, st_args, text[p_line] + String::chr(' ')));
  5097. }
  5098. /* STEP 3: Separate dest string in pre and post text. */
  5099. String preinsert_text = text[p_line].substr(0, p_char);
  5100. String postinsert_text = text[p_line].substr(p_char, text[p_line].size());
  5101. for (int j = 0; j < substrings.size(); j++) {
  5102. // Insert the substrings.
  5103. if (j == 0) {
  5104. text.set(p_line, preinsert_text + substrings[j], structured_text_parser(st_parser, st_args, preinsert_text + substrings[j]));
  5105. } else {
  5106. text.insert(p_line + j, substrings[j], structured_text_parser(st_parser, st_args, substrings[j]));
  5107. }
  5108. if (j == substrings.size() - 1) {
  5109. text.set(p_line + j, text[p_line + j] + postinsert_text, structured_text_parser(st_parser, st_args, text[p_line + j] + postinsert_text));
  5110. }
  5111. }
  5112. if (shift_first_line) {
  5113. text.move_gutters(p_line, p_line + 1);
  5114. text.set_hidden(p_line + 1, text.is_hidden(p_line));
  5115. text.set_hidden(p_line, false);
  5116. }
  5117. r_end_line = p_line + substrings.size() - 1;
  5118. r_end_column = text[r_end_line].length() - postinsert_text.length();
  5119. TextServer::Direction dir = TS->shaped_text_get_dominant_direction_in_range(text.get_line_data(r_end_line)->get_rid(), (r_end_line == p_line) ? caret.column : 0, r_end_column);
  5120. if (dir != TextServer::DIRECTION_AUTO) {
  5121. input_direction = (TextDirection)dir;
  5122. }
  5123. if (!text_changed_dirty && !setting_text) {
  5124. if (is_inside_tree()) {
  5125. MessageQueue::get_singleton()->push_call(this, "_text_changed_emit");
  5126. }
  5127. text_changed_dirty = true;
  5128. }
  5129. emit_signal(SNAME("lines_edited_from"), p_line, r_end_line);
  5130. }
  5131. String TextEdit::_base_get_text(int p_from_line, int p_from_column, int p_to_line, int p_to_column) const {
  5132. ERR_FAIL_INDEX_V(p_from_line, text.size(), String());
  5133. ERR_FAIL_INDEX_V(p_from_column, text[p_from_line].length() + 1, String());
  5134. ERR_FAIL_INDEX_V(p_to_line, text.size(), String());
  5135. ERR_FAIL_INDEX_V(p_to_column, text[p_to_line].length() + 1, String());
  5136. ERR_FAIL_COND_V(p_to_line < p_from_line, String()); // 'from > to'.
  5137. ERR_FAIL_COND_V(p_to_line == p_from_line && p_to_column < p_from_column, String()); // 'from > to'.
  5138. String ret;
  5139. for (int i = p_from_line; i <= p_to_line; i++) {
  5140. int begin = (i == p_from_line) ? p_from_column : 0;
  5141. int end = (i == p_to_line) ? p_to_column : text[i].length();
  5142. if (i > p_from_line) {
  5143. ret += "\n";
  5144. }
  5145. ret += text[i].substr(begin, end - begin);
  5146. }
  5147. return ret;
  5148. }
  5149. void TextEdit::_base_remove_text(int p_from_line, int p_from_column, int p_to_line, int p_to_column) {
  5150. ERR_FAIL_INDEX(p_from_line, text.size());
  5151. ERR_FAIL_INDEX(p_from_column, text[p_from_line].length() + 1);
  5152. ERR_FAIL_INDEX(p_to_line, text.size());
  5153. ERR_FAIL_INDEX(p_to_column, text[p_to_line].length() + 1);
  5154. ERR_FAIL_COND(p_to_line < p_from_line); // 'from > to'.
  5155. ERR_FAIL_COND(p_to_line == p_from_line && p_to_column < p_from_column); // 'from > to'.
  5156. String pre_text = text[p_from_line].substr(0, p_from_column);
  5157. String post_text = text[p_to_line].substr(p_to_column, text[p_to_line].length());
  5158. for (int i = p_from_line; i < p_to_line; i++) {
  5159. text.remove(p_from_line + 1);
  5160. }
  5161. text.set(p_from_line, pre_text + post_text, structured_text_parser(st_parser, st_args, pre_text + post_text));
  5162. if (!text_changed_dirty && !setting_text) {
  5163. if (is_inside_tree()) {
  5164. MessageQueue::get_singleton()->push_call(this, "_text_changed_emit");
  5165. }
  5166. text_changed_dirty = true;
  5167. }
  5168. emit_signal(SNAME("lines_edited_from"), p_to_line, p_from_line);
  5169. }
  5170. TextEdit::TextEdit() {
  5171. clear();
  5172. set_focus_mode(FOCUS_ALL);
  5173. _update_caches();
  5174. set_default_cursor_shape(CURSOR_IBEAM);
  5175. text.set_tab_size(text.get_tab_size());
  5176. h_scroll = memnew(HScrollBar);
  5177. v_scroll = memnew(VScrollBar);
  5178. add_child(h_scroll, false, INTERNAL_MODE_FRONT);
  5179. add_child(v_scroll, false, INTERNAL_MODE_FRONT);
  5180. h_scroll->connect("value_changed", callable_mp(this, &TextEdit::_scroll_moved));
  5181. v_scroll->connect("value_changed", callable_mp(this, &TextEdit::_scroll_moved));
  5182. v_scroll->connect("scrolling", callable_mp(this, &TextEdit::_v_scroll_input));
  5183. /* Caret. */
  5184. caret_blink_timer = memnew(Timer);
  5185. add_child(caret_blink_timer, false, INTERNAL_MODE_FRONT);
  5186. caret_blink_timer->set_wait_time(0.65);
  5187. caret_blink_timer->connect("timeout", callable_mp(this, &TextEdit::_toggle_draw_caret));
  5188. set_caret_blink_enabled(false);
  5189. /* Selection. */
  5190. click_select_held = memnew(Timer);
  5191. add_child(click_select_held, false, INTERNAL_MODE_FRONT);
  5192. click_select_held->set_wait_time(0.05);
  5193. click_select_held->connect("timeout", callable_mp(this, &TextEdit::_click_selection_held));
  5194. idle_detect = memnew(Timer);
  5195. add_child(idle_detect, false, INTERNAL_MODE_FRONT);
  5196. idle_detect->set_one_shot(true);
  5197. idle_detect->set_wait_time(GLOBAL_GET("gui/timers/text_edit_idle_detect_sec"));
  5198. idle_detect->connect("timeout", callable_mp(this, &TextEdit::_push_current_op));
  5199. undo_stack_max_size = GLOBAL_GET("gui/common/text_edit_undo_stack_max_size");
  5200. set_editable(true);
  5201. }