text_edit.cpp 257 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821
  1. /**************************************************************************/
  2. /* text_edit.cpp */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  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 "label.h"
  41. #include "scene/main/window.h"
  42. ///////////////////////////////////////////////////////////////////////////////
  43. /// TEXT ///
  44. ///////////////////////////////////////////////////////////////////////////////
  45. void TextEdit::Text::set_font(const Ref<Font> &p_font) {
  46. if (font == p_font) {
  47. return;
  48. }
  49. font = p_font;
  50. is_dirty = true;
  51. }
  52. void TextEdit::Text::set_font_size(int p_font_size) {
  53. if (font_size == p_font_size) {
  54. return;
  55. }
  56. font_size = p_font_size;
  57. is_dirty = true;
  58. }
  59. void TextEdit::Text::set_tab_size(int p_tab_size) {
  60. if (tab_size == p_tab_size) {
  61. return;
  62. }
  63. tab_size = p_tab_size;
  64. tab_size_dirty = true;
  65. }
  66. int TextEdit::Text::get_tab_size() const {
  67. return tab_size;
  68. }
  69. void TextEdit::Text::set_direction_and_language(TextServer::Direction p_direction, const String &p_language) {
  70. if (direction == p_direction && language == p_language) {
  71. return;
  72. }
  73. direction = p_direction;
  74. language = p_language;
  75. is_dirty = true;
  76. }
  77. void TextEdit::Text::set_draw_control_chars(bool p_enabled) {
  78. if (draw_control_chars == p_enabled) {
  79. return;
  80. }
  81. draw_control_chars = p_enabled;
  82. is_dirty = true;
  83. }
  84. int TextEdit::Text::get_line_width(int p_line, int p_wrap_index) const {
  85. ERR_FAIL_INDEX_V(p_line, text.size(), 0);
  86. if (p_wrap_index != -1) {
  87. return text[p_line].data_buf->get_line_width(p_wrap_index);
  88. }
  89. return text[p_line].data_buf->get_size().x;
  90. }
  91. int TextEdit::Text::get_line_height() const {
  92. return line_height;
  93. }
  94. void TextEdit::Text::set_width(float p_width) {
  95. width = p_width;
  96. }
  97. float TextEdit::Text::get_width() const {
  98. return width;
  99. }
  100. int TextEdit::Text::get_line_wrap_amount(int p_line) const {
  101. ERR_FAIL_INDEX_V(p_line, text.size(), 0);
  102. return text[p_line].data_buf->get_line_count() - 1;
  103. }
  104. Vector<Vector2i> TextEdit::Text::get_line_wrap_ranges(int p_line) const {
  105. Vector<Vector2i> ret;
  106. ERR_FAIL_INDEX_V(p_line, text.size(), ret);
  107. for (int i = 0; i < text[p_line].data_buf->get_line_count(); i++) {
  108. ret.push_back(text[p_line].data_buf->get_line_range(i));
  109. }
  110. return ret;
  111. }
  112. const Ref<TextParagraph> TextEdit::Text::get_line_data(int p_line) const {
  113. ERR_FAIL_INDEX_V(p_line, text.size(), Ref<TextParagraph>());
  114. return text[p_line].data_buf;
  115. }
  116. _FORCE_INLINE_ const String &TextEdit::Text::operator[](int p_line) const {
  117. return text[p_line].data;
  118. }
  119. void TextEdit::Text::_calculate_line_height() {
  120. int height = 0;
  121. for (const Line &l : text) {
  122. // Found another line with the same height...nothing to update.
  123. if (l.height == line_height) {
  124. height = line_height;
  125. break;
  126. }
  127. height = MAX(height, l.height);
  128. }
  129. line_height = height;
  130. }
  131. void TextEdit::Text::_calculate_max_line_width() {
  132. int line_width = 0;
  133. for (const Line &l : text) {
  134. if (l.hidden) {
  135. continue;
  136. }
  137. // Found another line with the same width...nothing to update.
  138. if (l.width == max_width) {
  139. line_width = max_width;
  140. break;
  141. }
  142. line_width = MAX(line_width, l.width);
  143. }
  144. max_width = line_width;
  145. }
  146. void TextEdit::Text::invalidate_cache(int p_line, int p_column, bool p_text_changed, const String &p_ime_text, const Array &p_bidi_override) {
  147. ERR_FAIL_INDEX(p_line, text.size());
  148. if (font.is_null()) {
  149. return; // Not in tree?
  150. }
  151. if (p_text_changed) {
  152. text.write[p_line].data_buf->clear();
  153. }
  154. text.write[p_line].data_buf->set_width(width);
  155. text.write[p_line].data_buf->set_direction((TextServer::Direction)direction);
  156. text.write[p_line].data_buf->set_preserve_control(draw_control_chars);
  157. if (p_ime_text.length() > 0) {
  158. if (p_text_changed) {
  159. text.write[p_line].data_buf->add_string(p_ime_text, font, font_size, language);
  160. }
  161. if (!p_bidi_override.is_empty()) {
  162. TS->shaped_text_set_bidi_override(text.write[p_line].data_buf->get_rid(), p_bidi_override);
  163. }
  164. } else {
  165. if (p_text_changed) {
  166. text.write[p_line].data_buf->add_string(text[p_line].data, font, font_size, language);
  167. }
  168. if (!text[p_line].bidi_override.is_empty()) {
  169. TS->shaped_text_set_bidi_override(text.write[p_line].data_buf->get_rid(), text[p_line].bidi_override);
  170. }
  171. }
  172. if (!p_text_changed) {
  173. RID r = text.write[p_line].data_buf->get_rid();
  174. int spans = TS->shaped_get_span_count(r);
  175. for (int i = 0; i < spans; i++) {
  176. TS->shaped_set_span_update_font(r, i, font->get_rids(), font_size, font->get_opentype_features());
  177. }
  178. for (int i = 0; i < TextServer::SPACING_MAX; i++) {
  179. TS->shaped_text_set_spacing(r, TextServer::SpacingType(i), font->get_spacing(TextServer::SpacingType(i)));
  180. }
  181. }
  182. // Apply tab align.
  183. if (tab_size > 0) {
  184. Vector<float> tabs;
  185. tabs.push_back(font->get_char_size(' ', font_size).width * tab_size);
  186. text.write[p_line].data_buf->tab_align(tabs);
  187. }
  188. // Update height.
  189. const int old_height = text.write[p_line].height;
  190. const int wrap_amount = get_line_wrap_amount(p_line);
  191. int height = font_height;
  192. for (int i = 0; i <= wrap_amount; i++) {
  193. height = MAX(height, text[p_line].data_buf->get_line_size(i).y);
  194. }
  195. text.write[p_line].height = height;
  196. // If this line has shrunk, this may no longer the the tallest line.
  197. if (old_height == line_height && height < line_height) {
  198. _calculate_line_height();
  199. } else {
  200. line_height = MAX(height, line_height);
  201. }
  202. // Update width.
  203. const int old_width = text.write[p_line].width;
  204. int line_width = get_line_width(p_line);
  205. text.write[p_line].width = line_width;
  206. // If this line has shrunk, this may no longer the the longest line.
  207. if (old_width == max_width && line_width < max_width) {
  208. _calculate_max_line_width();
  209. } else if (!is_hidden(p_line)) {
  210. max_width = MAX(line_width, max_width);
  211. }
  212. }
  213. void TextEdit::Text::invalidate_all_lines() {
  214. for (int i = 0; i < text.size(); i++) {
  215. text.write[i].data_buf->set_width(width);
  216. if (tab_size_dirty) {
  217. if (tab_size > 0) {
  218. Vector<float> tabs;
  219. tabs.push_back(font->get_char_size(' ', font_size).width * tab_size);
  220. text.write[i].data_buf->tab_align(tabs);
  221. }
  222. // Tabs have changes, force width update.
  223. text.write[i].width = get_line_width(i);
  224. }
  225. }
  226. if (tab_size_dirty) {
  227. _calculate_max_line_width();
  228. tab_size_dirty = false;
  229. }
  230. }
  231. void TextEdit::Text::invalidate_font() {
  232. if (!is_dirty) {
  233. return;
  234. }
  235. max_width = -1;
  236. line_height = -1;
  237. if (font.is_valid() && font_size > 0) {
  238. font_height = font->get_height(font_size);
  239. }
  240. for (int i = 0; i < text.size(); i++) {
  241. invalidate_cache(i, -1, false);
  242. }
  243. is_dirty = false;
  244. }
  245. void TextEdit::Text::invalidate_all() {
  246. if (!is_dirty) {
  247. return;
  248. }
  249. max_width = -1;
  250. line_height = -1;
  251. if (font.is_valid() && font_size > 0) {
  252. font_height = font->get_height(font_size);
  253. }
  254. for (int i = 0; i < text.size(); i++) {
  255. invalidate_cache(i, -1, true);
  256. }
  257. is_dirty = false;
  258. }
  259. void TextEdit::Text::clear() {
  260. text.clear();
  261. max_width = -1;
  262. line_height = -1;
  263. Line line;
  264. line.gutters.resize(gutter_count);
  265. line.data = "";
  266. text.insert(0, line);
  267. invalidate_cache(0, -1, true);
  268. }
  269. int TextEdit::Text::get_max_width() const {
  270. return max_width;
  271. }
  272. void TextEdit::Text::set(int p_line, const String &p_text, const Array &p_bidi_override) {
  273. ERR_FAIL_INDEX(p_line, text.size());
  274. text.write[p_line].data = p_text;
  275. text.write[p_line].bidi_override = p_bidi_override;
  276. invalidate_cache(p_line, -1, true);
  277. }
  278. void TextEdit::Text::insert(int p_at, const Vector<String> &p_text, const Vector<Array> &p_bidi_override) {
  279. int new_line_count = p_text.size() - 1;
  280. if (new_line_count > 0) {
  281. text.resize(text.size() + new_line_count);
  282. for (int i = (text.size() - 1); i > p_at; i--) {
  283. if ((i - new_line_count) <= 0) {
  284. break;
  285. }
  286. text.write[i] = text[i - new_line_count];
  287. }
  288. }
  289. for (int i = 0; i < p_text.size(); i++) {
  290. if (i == 0) {
  291. set(p_at + i, p_text[i], p_bidi_override[i]);
  292. continue;
  293. }
  294. Line line;
  295. line.gutters.resize(gutter_count);
  296. line.data = p_text[i];
  297. line.bidi_override = p_bidi_override[i];
  298. text.write[p_at + i] = line;
  299. invalidate_cache(p_at + i, -1, true);
  300. }
  301. }
  302. void TextEdit::Text::remove_range(int p_from_line, int p_to_line) {
  303. if (p_from_line == p_to_line) {
  304. return;
  305. }
  306. bool dirty_height = false;
  307. bool dirty_width = false;
  308. for (int i = p_from_line; i < p_to_line; i++) {
  309. if (!dirty_height && text[i].height == line_height) {
  310. dirty_height = true;
  311. }
  312. if (!dirty_width && text[i].width == max_width) {
  313. dirty_width = true;
  314. }
  315. if (dirty_height && dirty_width) {
  316. break;
  317. }
  318. }
  319. int diff = (p_to_line - p_from_line);
  320. for (int i = p_to_line; i < text.size() - 1; i++) {
  321. text.write[(i - diff) + 1] = text[i + 1];
  322. }
  323. text.resize(text.size() - diff);
  324. if (dirty_height) {
  325. _calculate_line_height();
  326. }
  327. if (dirty_width) {
  328. _calculate_max_line_width();
  329. }
  330. }
  331. void TextEdit::Text::add_gutter(int p_at) {
  332. for (int i = 0; i < text.size(); i++) {
  333. if (p_at < 0 || p_at > gutter_count) {
  334. text.write[i].gutters.push_back(Gutter());
  335. } else {
  336. text.write[i].gutters.insert(p_at, Gutter());
  337. }
  338. }
  339. gutter_count++;
  340. }
  341. void TextEdit::Text::remove_gutter(int p_gutter) {
  342. for (int i = 0; i < text.size(); i++) {
  343. text.write[i].gutters.remove_at(p_gutter);
  344. }
  345. gutter_count--;
  346. }
  347. void TextEdit::Text::move_gutters(int p_from_line, int p_to_line) {
  348. text.write[p_to_line].gutters = text[p_from_line].gutters;
  349. text.write[p_from_line].gutters.clear();
  350. text.write[p_from_line].gutters.resize(gutter_count);
  351. }
  352. ///////////////////////////////////////////////////////////////////////////////
  353. /// TEXT EDIT ///
  354. ///////////////////////////////////////////////////////////////////////////////
  355. void TextEdit::_notification(int p_what) {
  356. switch (p_what) {
  357. case NOTIFICATION_ENTER_TREE: {
  358. _update_caches();
  359. if (caret_pos_dirty) {
  360. MessageQueue::get_singleton()->push_call(this, "_emit_caret_changed");
  361. }
  362. if (text_changed_dirty) {
  363. MessageQueue::get_singleton()->push_call(this, "_text_changed_emit");
  364. }
  365. _update_wrap_at_column(true);
  366. } break;
  367. case NOTIFICATION_RESIZED: {
  368. _update_scrollbars();
  369. _update_wrap_at_column();
  370. } break;
  371. case NOTIFICATION_VISIBILITY_CHANGED: {
  372. if (is_visible()) {
  373. call_deferred(SNAME("_update_scrollbars"));
  374. call_deferred(SNAME("_update_wrap_at_column"));
  375. }
  376. } break;
  377. case NOTIFICATION_LAYOUT_DIRECTION_CHANGED:
  378. case NOTIFICATION_TRANSLATION_CHANGED:
  379. case NOTIFICATION_THEME_CHANGED: {
  380. if (is_inside_tree()) {
  381. _update_caches();
  382. _update_wrap_at_column(true);
  383. }
  384. } break;
  385. case NOTIFICATION_WM_WINDOW_FOCUS_IN: {
  386. window_has_focus = true;
  387. draw_caret = true;
  388. queue_redraw();
  389. } break;
  390. case NOTIFICATION_WM_WINDOW_FOCUS_OUT: {
  391. window_has_focus = false;
  392. draw_caret = false;
  393. queue_redraw();
  394. } break;
  395. case NOTIFICATION_INTERNAL_PHYSICS_PROCESS: {
  396. if (scrolling && get_v_scroll() != target_v_scroll) {
  397. double target_y = target_v_scroll - get_v_scroll();
  398. double dist = abs(target_y);
  399. // To ensure minimap is responsive override the speed setting.
  400. double vel = ((target_y / dist) * ((minimap_clicked) ? 3000 : v_scroll_speed)) * get_physics_process_delta_time();
  401. // Prevent too small velocity to block scrolling
  402. if (Math::abs(vel) < v_scroll->get_step()) {
  403. vel = v_scroll->get_step() * SIGN(vel);
  404. }
  405. if (Math::abs(vel) >= dist) {
  406. set_v_scroll(target_v_scroll);
  407. scrolling = false;
  408. minimap_clicked = false;
  409. set_physics_process_internal(false);
  410. } else {
  411. set_v_scroll(get_v_scroll() + vel);
  412. }
  413. } else {
  414. scrolling = false;
  415. minimap_clicked = false;
  416. set_physics_process_internal(false);
  417. }
  418. } break;
  419. case NOTIFICATION_DRAW: {
  420. if (first_draw) {
  421. // Size may not be the final one, so attempts to ensure caret was visible may have failed.
  422. adjust_viewport_to_caret();
  423. first_draw = false;
  424. }
  425. /* Prevent the resource getting lost between the editor and game. */
  426. if (Engine::get_singleton()->is_editor_hint()) {
  427. if (syntax_highlighter.is_valid() && syntax_highlighter->get_text_edit() != this) {
  428. syntax_highlighter->set_text_edit(this);
  429. }
  430. }
  431. Size2 size = get_size();
  432. bool rtl = is_layout_rtl();
  433. if ((!has_focus() && !(menu && menu->has_focus())) || !window_has_focus) {
  434. draw_caret = false;
  435. }
  436. _update_scrollbars();
  437. RID ci = get_canvas_item();
  438. RenderingServer::get_singleton()->canvas_item_set_clip(get_canvas_item(), true);
  439. int xmargin_beg = style_normal->get_margin(SIDE_LEFT) + gutters_width + gutter_padding;
  440. int xmargin_end = size.width - style_normal->get_margin(SIDE_RIGHT);
  441. if (draw_minimap) {
  442. xmargin_end -= minimap_width;
  443. }
  444. // Let's do it easy for now.
  445. style_normal->draw(ci, Rect2(Point2(), size));
  446. if (!editable) {
  447. style_readonly->draw(ci, Rect2(Point2(), size));
  448. draw_caret = false;
  449. }
  450. if (has_focus()) {
  451. style_focus->draw(ci, Rect2(Point2(), size));
  452. }
  453. int visible_rows = get_visible_line_count() + 1;
  454. Color color = !editable ? font_readonly_color : font_color;
  455. if (background_color.a > 0.01) {
  456. RenderingServer::get_singleton()->canvas_item_add_rect(ci, Rect2(Point2i(), get_size()), background_color);
  457. }
  458. Vector<BraceMatchingData> brace_matching;
  459. if (highlight_matching_braces_enabled) {
  460. brace_matching.resize(carets.size());
  461. for (int caret = 0; caret < carets.size(); caret++) {
  462. if (get_caret_line(caret) < 0 || get_caret_line(caret) >= text.size() || get_caret_column(caret) < 0) {
  463. continue;
  464. }
  465. if (get_caret_column(caret) < text[get_caret_line(caret)].length()) {
  466. // Check for open.
  467. char32_t c = text[get_caret_line(caret)][get_caret_column(caret)];
  468. char32_t closec = 0;
  469. if (c == '[') {
  470. closec = ']';
  471. } else if (c == '{') {
  472. closec = '}';
  473. } else if (c == '(') {
  474. closec = ')';
  475. }
  476. if (closec != 0) {
  477. int stack = 1;
  478. for (int i = get_caret_line(caret); i < text.size(); i++) {
  479. int from = i == get_caret_line(caret) ? get_caret_column(caret) + 1 : 0;
  480. for (int j = from; j < text[i].length(); j++) {
  481. char32_t cc = text[i][j];
  482. // Ignore any brackets inside a string.
  483. if (cc == '"' || cc == '\'') {
  484. char32_t quotation = cc;
  485. do {
  486. j++;
  487. if (!(j < text[i].length())) {
  488. break;
  489. }
  490. cc = text[i][j];
  491. // Skip over escaped quotation marks inside strings.
  492. if (cc == '\\') {
  493. bool escaped = true;
  494. while (j + 1 < text[i].length() && text[i][j + 1] == '\\') {
  495. escaped = !escaped;
  496. j++;
  497. }
  498. if (escaped) {
  499. j++;
  500. continue;
  501. }
  502. }
  503. } while (cc != quotation);
  504. } else if (cc == c) {
  505. stack++;
  506. } else if (cc == closec) {
  507. stack--;
  508. }
  509. if (stack == 0) {
  510. brace_matching.write[caret].open_match_line = i;
  511. brace_matching.write[caret].open_match_column = j;
  512. brace_matching.write[caret].open_matching = true;
  513. break;
  514. }
  515. }
  516. if (brace_matching.write[caret].open_match_line != -1) {
  517. break;
  518. }
  519. }
  520. if (!brace_matching.write[caret].open_matching) {
  521. brace_matching.write[caret].open_mismatch = true;
  522. }
  523. }
  524. }
  525. if (get_caret_column(caret) > 0) {
  526. char32_t c = text[get_caret_line(caret)][get_caret_column(caret) - 1];
  527. char32_t closec = 0;
  528. if (c == ']') {
  529. closec = '[';
  530. } else if (c == '}') {
  531. closec = '{';
  532. } else if (c == ')') {
  533. closec = '(';
  534. }
  535. if (closec != 0) {
  536. int stack = 1;
  537. for (int i = get_caret_line(caret); i >= 0; i--) {
  538. int from = i == get_caret_line(caret) ? get_caret_column(caret) - 2 : text[i].length() - 1;
  539. for (int j = from; j >= 0; j--) {
  540. char32_t cc = text[i][j];
  541. // Ignore any brackets inside a string.
  542. if (cc == '"' || cc == '\'') {
  543. char32_t quotation = cc;
  544. do {
  545. j--;
  546. if (!(j >= 0)) {
  547. break;
  548. }
  549. cc = text[i][j];
  550. // Skip over escaped quotation marks inside strings.
  551. if (cc == quotation) {
  552. bool escaped = false;
  553. while (j - 1 >= 0 && text[i][j - 1] == '\\') {
  554. escaped = !escaped;
  555. j--;
  556. }
  557. if (escaped) {
  558. cc = '\\';
  559. continue;
  560. }
  561. }
  562. } while (cc != quotation);
  563. } else if (cc == c) {
  564. stack++;
  565. } else if (cc == closec) {
  566. stack--;
  567. }
  568. if (stack == 0) {
  569. brace_matching.write[caret].close_match_line = i;
  570. brace_matching.write[caret].close_match_column = j;
  571. brace_matching.write[caret].close_matching = true;
  572. break;
  573. }
  574. }
  575. if (brace_matching.write[caret].close_match_line != -1) {
  576. break;
  577. }
  578. }
  579. if (!brace_matching.write[caret].close_matching) {
  580. brace_matching.write[caret].close_mismatch = true;
  581. }
  582. }
  583. }
  584. }
  585. }
  586. bool draw_placeholder = text.size() == 1 && text[0].is_empty() && ime_text.is_empty();
  587. // Get the highlighted words.
  588. String highlighted_text = get_selected_text(0);
  589. // Check if highlighted words contain only whitespaces (tabs or spaces).
  590. bool only_whitespaces_highlighted = highlighted_text.strip_edges().is_empty();
  591. HashMap<int, HashSet<int>> caret_line_wrap_index_map;
  592. Vector<int> carets_wrap_index;
  593. carets_wrap_index.resize(carets.size());
  594. for (int i = 0; i < carets.size(); i++) {
  595. carets.write[i].visible = false;
  596. int wrap_index = get_caret_wrap_index(i);
  597. caret_line_wrap_index_map[get_caret_line(i)].insert(wrap_index);
  598. carets_wrap_index.write[i] = wrap_index;
  599. }
  600. int first_vis_line = get_first_visible_line() - 1;
  601. int draw_amount = visible_rows + (smooth_scroll_enabled ? 1 : 0);
  602. draw_amount += draw_placeholder ? placeholder_wraped_rows.size() - 1 : get_line_wrap_count(first_vis_line + 1);
  603. // Draw minimap.
  604. if (draw_minimap) {
  605. int minimap_visible_lines = get_minimap_visible_lines();
  606. int minimap_line_height = (minimap_char_size.y + minimap_line_spacing);
  607. int minimap_tab_size = minimap_char_size.x * text.get_tab_size();
  608. // calculate viewport size and y offset
  609. int viewport_height = (draw_amount - 1) * minimap_line_height;
  610. int control_height = _get_control_height() - viewport_height;
  611. int viewport_offset_y = round(get_scroll_pos_for_line(first_vis_line + 1) * control_height) / ((v_scroll->get_max() <= minimap_visible_lines) ? (minimap_visible_lines - draw_amount) : (v_scroll->get_max() - draw_amount));
  612. // calculate the first line.
  613. int num_lines_before = round((viewport_offset_y) / minimap_line_height);
  614. int minimap_line = (v_scroll->get_max() <= minimap_visible_lines) ? -1 : first_vis_line;
  615. if (minimap_line >= 0) {
  616. minimap_line -= get_next_visible_line_index_offset_from(first_vis_line, 0, -num_lines_before).x;
  617. minimap_line -= (minimap_line > 0 && smooth_scroll_enabled ? 1 : 0);
  618. }
  619. int minimap_draw_amount = minimap_visible_lines + get_line_wrap_count(minimap_line + 1);
  620. // Draw the minimap.
  621. // Add visual feedback when dragging or hovering the the visible area rectangle.
  622. float viewport_alpha;
  623. if (dragging_minimap) {
  624. viewport_alpha = 0.25;
  625. } else if (hovering_minimap) {
  626. viewport_alpha = 0.175;
  627. } else {
  628. viewport_alpha = 0.1;
  629. }
  630. const Color viewport_color = (background_color.get_v() < 0.5) ? Color(1, 1, 1, viewport_alpha) : Color(0, 0, 0, viewport_alpha);
  631. if (rtl) {
  632. 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);
  633. } else {
  634. RenderingServer::get_singleton()->canvas_item_add_rect(ci, Rect2((xmargin_end + 2), viewport_offset_y, minimap_width, viewport_height), viewport_color);
  635. }
  636. for (int i = 0; i < minimap_draw_amount; i++) {
  637. minimap_line++;
  638. if (minimap_line < 0 || minimap_line >= (int)text.size()) {
  639. break;
  640. }
  641. while (_is_line_hidden(minimap_line)) {
  642. minimap_line++;
  643. if (minimap_line < 0 || minimap_line >= (int)text.size()) {
  644. break;
  645. }
  646. }
  647. if (minimap_line < 0 || minimap_line >= (int)text.size()) {
  648. break;
  649. }
  650. Dictionary color_map = _get_line_syntax_highlighting(minimap_line);
  651. Color line_background_color = text.get_line_background_color(minimap_line);
  652. line_background_color.a *= 0.6;
  653. Color current_color = font_color;
  654. if (!editable) {
  655. current_color = font_readonly_color;
  656. }
  657. Vector<String> wrap_rows = get_line_wrapped_text(minimap_line);
  658. int line_wrap_amount = get_line_wrap_count(minimap_line);
  659. int last_wrap_column = 0;
  660. for (int line_wrap_index = 0; line_wrap_index < line_wrap_amount + 1; line_wrap_index++) {
  661. if (line_wrap_index != 0) {
  662. i++;
  663. if (i >= minimap_draw_amount) {
  664. break;
  665. }
  666. }
  667. const String &str = wrap_rows[line_wrap_index];
  668. int indent_px = line_wrap_index != 0 ? get_indent_level(minimap_line) : 0;
  669. if (indent_px >= wrap_at_column) {
  670. indent_px = 0;
  671. }
  672. indent_px = minimap_char_size.x * indent_px;
  673. if (line_wrap_index > 0) {
  674. last_wrap_column += wrap_rows[line_wrap_index - 1].length();
  675. }
  676. if (caret_line_wrap_index_map.has(minimap_line) && caret_line_wrap_index_map[minimap_line].has(line_wrap_index) && highlight_current_line) {
  677. if (rtl) {
  678. RenderingServer::get_singleton()->canvas_item_add_rect(ci, Rect2(size.width - (xmargin_end + 2) - minimap_width, i * 3, minimap_width, 2), current_line_color);
  679. } else {
  680. RenderingServer::get_singleton()->canvas_item_add_rect(ci, Rect2((xmargin_end + 2), i * 3, minimap_width, 2), current_line_color);
  681. }
  682. } else if (line_background_color != Color(0, 0, 0, 0)) {
  683. if (rtl) {
  684. RenderingServer::get_singleton()->canvas_item_add_rect(ci, Rect2(size.width - (xmargin_end + 2) - minimap_width, i * 3, minimap_width, 2), line_background_color);
  685. } else {
  686. RenderingServer::get_singleton()->canvas_item_add_rect(ci, Rect2((xmargin_end + 2), i * 3, minimap_width, 2), line_background_color);
  687. }
  688. }
  689. Color previous_color;
  690. int characters = 0;
  691. int tabs = 0;
  692. for (int j = 0; j < str.length(); j++) {
  693. const Variant *color_data = color_map.getptr(last_wrap_column + j);
  694. if (color_data != nullptr) {
  695. current_color = (color_data->operator Dictionary()).get("color", font_color);
  696. if (!editable) {
  697. current_color.a = font_readonly_color.a;
  698. }
  699. }
  700. color = current_color;
  701. if (j == 0) {
  702. previous_color = color;
  703. }
  704. int xpos = indent_px + ((xmargin_end + minimap_char_size.x) + (minimap_char_size.x * j)) + tabs;
  705. bool out_of_bounds = (xpos >= xmargin_end + minimap_width);
  706. bool whitespace = is_whitespace(str[j]);
  707. if (!whitespace) {
  708. characters++;
  709. if (j < str.length() - 1 && color == previous_color && !out_of_bounds) {
  710. continue;
  711. }
  712. // If we've changed color we are at the start of a new section, therefore we need to go back to the end
  713. // of the previous section to draw it, we'll also add the character back on.
  714. if (color != previous_color) {
  715. characters--;
  716. j--;
  717. if (str[j] == '\t') {
  718. tabs -= minimap_tab_size;
  719. }
  720. }
  721. }
  722. if (characters > 0) {
  723. previous_color.a *= 0.6;
  724. // take one for zero indexing, and if we hit whitespace / the end of a word.
  725. int chars = MAX(0, (j - (characters - 1)) - (whitespace ? 1 : 0)) + 1;
  726. int char_x_ofs = indent_px + ((xmargin_end + minimap_char_size.x) + (minimap_char_size.x * chars)) + tabs;
  727. if (rtl) {
  728. 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);
  729. } else {
  730. 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);
  731. }
  732. }
  733. if (out_of_bounds) {
  734. break;
  735. }
  736. if (str[j] == '\t') {
  737. tabs += minimap_tab_size;
  738. }
  739. previous_color = color;
  740. characters = 0;
  741. }
  742. }
  743. }
  744. }
  745. int top_limit_y = 0;
  746. int bottom_limit_y = get_size().height;
  747. if (!editable) {
  748. top_limit_y += style_readonly->get_margin(SIDE_TOP);
  749. bottom_limit_y -= style_readonly->get_margin(SIDE_BOTTOM);
  750. } else {
  751. top_limit_y += style_normal->get_margin(SIDE_TOP);
  752. bottom_limit_y -= style_normal->get_margin(SIDE_BOTTOM);
  753. }
  754. // Draw main text.
  755. line_drawing_cache.clear();
  756. int row_height = draw_placeholder ? placeholder_line_height + line_spacing : get_line_height();
  757. int line = first_vis_line;
  758. for (int i = 0; i < draw_amount; i++) {
  759. line++;
  760. if (line < 0 || line >= (int)text.size()) {
  761. continue;
  762. }
  763. while (_is_line_hidden(line)) {
  764. line++;
  765. if (line < 0 || line >= (int)text.size()) {
  766. break;
  767. }
  768. }
  769. if (line < 0 || line >= (int)text.size()) {
  770. continue;
  771. }
  772. LineDrawingCache cache_entry;
  773. Dictionary color_map = _get_line_syntax_highlighting(line);
  774. // Ensure we at least use the font color.
  775. Color current_color = !editable ? font_readonly_color : font_color;
  776. if (draw_placeholder) {
  777. current_color = font_placeholder_color;
  778. }
  779. const Ref<TextParagraph> ldata = draw_placeholder ? placeholder_data_buf : text.get_line_data(line);
  780. Vector<String> wrap_rows = draw_placeholder ? placeholder_wraped_rows : get_line_wrapped_text(line);
  781. int line_wrap_amount = draw_placeholder ? placeholder_wraped_rows.size() - 1 : get_line_wrap_count(line);
  782. for (int line_wrap_index = 0; line_wrap_index <= line_wrap_amount; line_wrap_index++) {
  783. if (line_wrap_index != 0) {
  784. i++;
  785. if (i >= draw_amount) {
  786. break;
  787. }
  788. }
  789. const String &str = wrap_rows[line_wrap_index];
  790. int char_margin = xmargin_beg - first_visible_col;
  791. int ofs_x = 0;
  792. int ofs_y = 0;
  793. if (!editable) {
  794. ofs_x = style_readonly->get_offset().x / 2;
  795. ofs_x -= style_normal->get_offset().x / 2;
  796. ofs_y = style_readonly->get_offset().y / 2;
  797. } else {
  798. ofs_y = style_normal->get_offset().y / 2;
  799. }
  800. ofs_y += i * row_height + line_spacing / 2;
  801. ofs_y -= first_visible_line_wrap_ofs * row_height;
  802. ofs_y -= _get_v_scroll_offset() * row_height;
  803. bool clipped = false;
  804. if (ofs_y + row_height < top_limit_y) {
  805. // Line is outside the top margin, clip current line.
  806. // Still need to go through the process to prepare color changes for next lines.
  807. clipped = true;
  808. }
  809. if (ofs_y > bottom_limit_y) {
  810. // Line is outside the bottom margin, clip any remaining text.
  811. i = draw_amount;
  812. break;
  813. }
  814. if (text.get_line_background_color(line) != Color(0, 0, 0, 0)) {
  815. if (rtl) {
  816. 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));
  817. } else {
  818. 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));
  819. }
  820. }
  821. if (str.length() == 0) {
  822. // Draw line background if empty as we won't loop at all.
  823. if (caret_line_wrap_index_map.has(line) && caret_line_wrap_index_map[line].has(line_wrap_index) && highlight_current_line) {
  824. if (rtl) {
  825. RenderingServer::get_singleton()->canvas_item_add_rect(ci, Rect2(size.width - ofs_x - xmargin_end, ofs_y, xmargin_end, row_height), current_line_color);
  826. } else {
  827. RenderingServer::get_singleton()->canvas_item_add_rect(ci, Rect2(ofs_x, ofs_y, xmargin_end, row_height), current_line_color);
  828. }
  829. }
  830. // Give visual indication of empty selected line.
  831. for (int c = 0; c < carets.size(); c++) {
  832. if (has_selection(c) && line >= get_selection_from_line(c) && line <= get_selection_to_line(c) && char_margin >= xmargin_beg) {
  833. float char_w = font->get_char_size(' ', font_size).width;
  834. if (rtl) {
  835. 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);
  836. } else {
  837. RenderingServer::get_singleton()->canvas_item_add_rect(ci, Rect2(xmargin_beg + ofs_x, ofs_y, char_w, row_height), selection_color);
  838. }
  839. }
  840. }
  841. } else {
  842. // 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.
  843. if (caret_line_wrap_index_map.has(line) && caret_line_wrap_index_map[line].has(line_wrap_index) && highlight_current_line) {
  844. if (rtl) {
  845. RenderingServer::get_singleton()->canvas_item_add_rect(ci, Rect2(size.width - ofs_x - xmargin_end, ofs_y, xmargin_end, row_height), current_line_color);
  846. } else {
  847. RenderingServer::get_singleton()->canvas_item_add_rect(ci, Rect2(ofs_x, ofs_y, xmargin_end, row_height), current_line_color);
  848. }
  849. }
  850. }
  851. if (line_wrap_index == 0) {
  852. // Only do these if we are on the first wrapped part of a line.
  853. cache_entry.y_offset = ofs_y;
  854. int gutter_offset = style_normal->get_margin(SIDE_LEFT);
  855. for (int g = 0; g < gutters.size(); g++) {
  856. const GutterInfo gutter = gutters[g];
  857. if (!gutter.draw || gutter.width <= 0) {
  858. continue;
  859. }
  860. switch (gutter.type) {
  861. case GUTTER_TYPE_STRING: {
  862. const String &txt = get_line_gutter_text(line, g);
  863. if (txt.is_empty()) {
  864. break;
  865. }
  866. Ref<TextLine> tl;
  867. tl.instantiate();
  868. tl->add_string(txt, font, font_size);
  869. int yofs = ofs_y + (row_height - tl->get_size().y) / 2;
  870. if (outline_size > 0 && outline_color.a > 0) {
  871. tl->draw_outline(ci, Point2(gutter_offset + ofs_x, yofs), outline_size, outline_color);
  872. }
  873. tl->draw(ci, Point2(gutter_offset + ofs_x, yofs), get_line_gutter_item_color(line, g));
  874. } break;
  875. case GUTTER_TYPE_ICON: {
  876. const Ref<Texture2D> icon = get_line_gutter_icon(line, g);
  877. if (icon.is_null()) {
  878. break;
  879. }
  880. Rect2 gutter_rect = Rect2(Point2i(gutter_offset, ofs_y), Size2i(gutter.width, row_height));
  881. int horizontal_padding = gutter_rect.size.x / 6;
  882. int vertical_padding = gutter_rect.size.y / 6;
  883. gutter_rect.position += Point2(horizontal_padding, vertical_padding);
  884. gutter_rect.size -= Point2(horizontal_padding, vertical_padding) * 2;
  885. // Correct icon aspect ratio.
  886. float icon_ratio = icon->get_width() / icon->get_height();
  887. float gutter_ratio = gutter_rect.size.x / gutter_rect.size.y;
  888. if (gutter_ratio > icon_ratio) {
  889. gutter_rect.size.x = floor(icon->get_width() * (gutter_rect.size.y / icon->get_height()));
  890. } else {
  891. gutter_rect.size.y = floor(icon->get_height() * (gutter_rect.size.x / icon->get_width()));
  892. }
  893. if (rtl) {
  894. gutter_rect.position.x = size.width - gutter_rect.position.x - gutter_rect.size.x;
  895. }
  896. icon->draw_rect(ci, gutter_rect, false, get_line_gutter_item_color(line, g));
  897. } break;
  898. case GUTTER_TYPE_CUSTOM: {
  899. if (gutter.custom_draw_callback.is_valid()) {
  900. Rect2i gutter_rect = Rect2i(Point2i(gutter_offset, ofs_y), Size2i(gutter.width, row_height));
  901. if (rtl) {
  902. gutter_rect.position.x = size.width - gutter_rect.position.x - gutter_rect.size.x;
  903. }
  904. Variant args[3] = { line, g, Rect2(gutter_rect) };
  905. const Variant *argp[] = { &args[0], &args[1], &args[2] };
  906. Callable::CallError ce;
  907. Variant ret;
  908. gutter.custom_draw_callback.callp(argp, 3, ret, ce);
  909. }
  910. } break;
  911. }
  912. gutter_offset += gutter.width;
  913. }
  914. }
  915. // Draw line.
  916. RID rid = ldata->get_line_rid(line_wrap_index);
  917. float text_height = TS->shaped_text_get_size(rid).y;
  918. if (rtl) {
  919. char_margin = size.width - char_margin - TS->shaped_text_get_size(rid).x;
  920. }
  921. for (int c = 0; c < carets.size(); c++) {
  922. if (!clipped && has_selection(c) && line >= get_selection_from_line(c) && line <= get_selection_to_line(c)) { // Selection
  923. int sel_from = (line > get_selection_from_line(c)) ? TS->shaped_text_get_range(rid).x : get_selection_from_column(c);
  924. int sel_to = (line < get_selection_to_line(c)) ? TS->shaped_text_get_range(rid).y : get_selection_to_column(c);
  925. Vector<Vector2> sel = TS->shaped_text_get_selection(rid, sel_from, sel_to);
  926. for (int j = 0; j < sel.size(); j++) {
  927. Rect2 rect = Rect2(sel[j].x + char_margin + ofs_x, ofs_y, sel[j].y - sel[j].x, row_height);
  928. if (rect.position.x + rect.size.x <= xmargin_beg || rect.position.x > xmargin_end) {
  929. continue;
  930. }
  931. if (rect.position.x < xmargin_beg) {
  932. rect.size.x -= (xmargin_beg - rect.position.x);
  933. rect.position.x = xmargin_beg;
  934. }
  935. if (rect.position.x + rect.size.x > xmargin_end) {
  936. rect.size.x = xmargin_end - rect.position.x;
  937. }
  938. draw_rect(rect, selection_color, true);
  939. }
  940. }
  941. }
  942. int start = TS->shaped_text_get_range(rid).x;
  943. if (!clipped && !search_text.is_empty()) { // Search highhlight
  944. int search_text_col = _get_column_pos_of_word(search_text, str, search_flags, 0);
  945. int search_text_len = search_text.length();
  946. while (search_text_col != -1) {
  947. Vector<Vector2> sel = TS->shaped_text_get_selection(rid, search_text_col + start, search_text_col + search_text_len + start);
  948. for (int j = 0; j < sel.size(); j++) {
  949. Rect2 rect = Rect2(sel[j].x + char_margin + ofs_x, ofs_y, sel[j].y - sel[j].x, row_height);
  950. if (rect.position.x + rect.size.x <= xmargin_beg || rect.position.x > xmargin_end) {
  951. continue;
  952. }
  953. if (rect.position.x < xmargin_beg) {
  954. rect.size.x -= (xmargin_beg - rect.position.x);
  955. rect.position.x = xmargin_beg;
  956. } else if (rect.position.x + rect.size.x > xmargin_end) {
  957. rect.size.x = xmargin_end - rect.position.x;
  958. }
  959. draw_rect(rect, search_result_color, true);
  960. draw_rect(rect, search_result_border_color, false);
  961. }
  962. search_text_col = _get_column_pos_of_word(search_text, str, search_flags, search_text_col + search_text_len);
  963. }
  964. }
  965. if (!clipped && highlight_all_occurrences && !only_whitespaces_highlighted && !highlighted_text.is_empty()) { // Highlight
  966. int highlighted_text_col = _get_column_pos_of_word(highlighted_text, str, SEARCH_MATCH_CASE | SEARCH_WHOLE_WORDS, 0);
  967. int highlighted_text_len = highlighted_text.length();
  968. while (highlighted_text_col != -1) {
  969. Vector<Vector2> sel = TS->shaped_text_get_selection(rid, highlighted_text_col + start, highlighted_text_col + highlighted_text_len + start);
  970. for (int j = 0; j < sel.size(); j++) {
  971. Rect2 rect = Rect2(sel[j].x + char_margin + ofs_x, ofs_y, sel[j].y - sel[j].x, row_height);
  972. if (rect.position.x + rect.size.x <= xmargin_beg || rect.position.x > xmargin_end) {
  973. continue;
  974. }
  975. if (rect.position.x < xmargin_beg) {
  976. rect.size.x -= (xmargin_beg - rect.position.x);
  977. rect.position.x = xmargin_beg;
  978. } else if (rect.position.x + rect.size.x > xmargin_end) {
  979. rect.size.x = xmargin_end - rect.position.x;
  980. }
  981. draw_rect(rect, word_highlighted_color);
  982. }
  983. highlighted_text_col = _get_column_pos_of_word(highlighted_text, str, SEARCH_MATCH_CASE | SEARCH_WHOLE_WORDS, highlighted_text_col + highlighted_text_len);
  984. }
  985. }
  986. if (!clipped && lookup_symbol_word.length() != 0) { // Highlight word
  987. if (is_ascii_char(lookup_symbol_word[0]) || lookup_symbol_word[0] == '_' || lookup_symbol_word[0] == '.') {
  988. int lookup_symbol_word_col = _get_column_pos_of_word(lookup_symbol_word, str, SEARCH_MATCH_CASE | SEARCH_WHOLE_WORDS, 0);
  989. int lookup_symbol_word_len = lookup_symbol_word.length();
  990. while (lookup_symbol_word_col != -1) {
  991. Vector<Vector2> sel = TS->shaped_text_get_selection(rid, lookup_symbol_word_col + start, lookup_symbol_word_col + lookup_symbol_word_len + start);
  992. for (int j = 0; j < sel.size(); j++) {
  993. Rect2 rect = Rect2(sel[j].x + char_margin + ofs_x, ofs_y + (line_spacing / 2), sel[j].y - sel[j].x, row_height);
  994. if (rect.position.x + rect.size.x <= xmargin_beg || rect.position.x > xmargin_end) {
  995. continue;
  996. }
  997. if (rect.position.x < xmargin_beg) {
  998. rect.size.x -= (xmargin_beg - rect.position.x);
  999. rect.position.x = xmargin_beg;
  1000. } else if (rect.position.x + rect.size.x > xmargin_end) {
  1001. rect.size.x = xmargin_end - rect.position.x;
  1002. }
  1003. rect.position.y += ceil(TS->shaped_text_get_ascent(rid)) + ceil(font->get_underline_position(font_size));
  1004. rect.size.y = MAX(1, font->get_underline_thickness(font_size));
  1005. draw_rect(rect, color);
  1006. }
  1007. lookup_symbol_word_col = _get_column_pos_of_word(lookup_symbol_word, str, SEARCH_MATCH_CASE | SEARCH_WHOLE_WORDS, lookup_symbol_word_col + lookup_symbol_word_len);
  1008. }
  1009. }
  1010. }
  1011. ofs_y += (row_height - text_height) / 2;
  1012. const Glyph *glyphs = TS->shaped_text_get_glyphs(rid);
  1013. int gl_size = TS->shaped_text_get_glyph_count(rid);
  1014. ofs_y += ldata->get_line_ascent(line_wrap_index);
  1015. int first_visible_char = TS->shaped_text_get_range(rid).y;
  1016. int last_visible_char = TS->shaped_text_get_range(rid).x;
  1017. float char_ofs = 0;
  1018. if (outline_size > 0 && outline_color.a > 0) {
  1019. for (int j = 0; j < gl_size; j++) {
  1020. for (int k = 0; k < glyphs[j].repeat; k++) {
  1021. if ((char_ofs + char_margin) >= xmargin_beg && (char_ofs + glyphs[j].advance + char_margin) <= xmargin_end) {
  1022. if (glyphs[j].font_rid != RID()) {
  1023. 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);
  1024. }
  1025. }
  1026. char_ofs += glyphs[j].advance;
  1027. }
  1028. if ((char_ofs + char_margin) >= xmargin_end) {
  1029. break;
  1030. }
  1031. }
  1032. char_ofs = 0;
  1033. }
  1034. for (int j = 0; j < gl_size; j++) {
  1035. int64_t color_start = -1;
  1036. for (const Variant *key = color_map.next(nullptr); key; key = color_map.next(key)) {
  1037. if (int64_t(*key) <= glyphs[j].start) {
  1038. color_start = *key;
  1039. } else {
  1040. break;
  1041. }
  1042. }
  1043. const Variant *color_data = (color_start >= 0) ? color_map.getptr(color_start) : nullptr;
  1044. if (color_data != nullptr) {
  1045. current_color = (color_data->operator Dictionary()).get("color", font_color);
  1046. if (!editable && current_color.a > font_readonly_color.a) {
  1047. current_color.a = font_readonly_color.a;
  1048. }
  1049. }
  1050. Color gl_color = current_color;
  1051. for (int c = 0; c < carets.size(); c++) {
  1052. if (has_selection(c) && line >= get_selection_from_line(c) && line <= get_selection_to_line(c)) { // Selection
  1053. int sel_from = (line > get_selection_from_line(c)) ? TS->shaped_text_get_range(rid).x : get_selection_from_column(c);
  1054. int sel_to = (line < get_selection_to_line(c)) ? TS->shaped_text_get_range(rid).y : get_selection_to_column(c);
  1055. if (glyphs[j].start >= sel_from && glyphs[j].end <= sel_to && use_selected_font_color) {
  1056. gl_color = font_selected_color;
  1057. }
  1058. }
  1059. }
  1060. float char_pos = char_ofs + char_margin + ofs_x;
  1061. if (char_pos >= xmargin_beg) {
  1062. if (highlight_matching_braces_enabled) {
  1063. for (int c = 0; c < carets.size(); c++) {
  1064. if ((brace_matching[c].open_match_line == line && brace_matching[c].open_match_column == glyphs[j].start) ||
  1065. (get_caret_column(c) == glyphs[j].start && get_caret_line(c) == line && carets_wrap_index[c] == line_wrap_index && (brace_matching[c].open_matching || brace_matching[c].open_mismatch))) {
  1066. if (brace_matching[c].open_mismatch) {
  1067. gl_color = brace_mismatch_color;
  1068. }
  1069. Rect2 rect = Rect2(char_pos, ofs_y + font->get_underline_position(font_size), glyphs[j].advance * glyphs[j].repeat, MAX(font->get_underline_thickness(font_size) * get_theme_default_base_scale(), 1));
  1070. draw_rect(rect, gl_color);
  1071. }
  1072. if ((brace_matching[c].close_match_line == line && brace_matching[c].close_match_column == glyphs[j].start) ||
  1073. (get_caret_column(c) == glyphs[j].start + 1 && get_caret_line(c) == line && carets_wrap_index[c] == line_wrap_index && (brace_matching[c].close_matching || brace_matching[c].close_mismatch))) {
  1074. if (brace_matching[c].close_mismatch) {
  1075. gl_color = brace_mismatch_color;
  1076. }
  1077. Rect2 rect = Rect2(char_pos, ofs_y + font->get_underline_position(font_size), glyphs[j].advance * glyphs[j].repeat, MAX(font->get_underline_thickness(font_size) * get_theme_default_base_scale(), 1));
  1078. draw_rect(rect, gl_color);
  1079. }
  1080. }
  1081. }
  1082. if (draw_tabs && ((glyphs[j].flags & TextServer::GRAPHEME_IS_TAB) == TextServer::GRAPHEME_IS_TAB)) {
  1083. int yofs = (text_height - tab_icon->get_height()) / 2 - ldata->get_line_ascent(line_wrap_index);
  1084. tab_icon->draw(ci, Point2(char_pos, ofs_y + yofs), gl_color);
  1085. } else if (draw_spaces && ((glyphs[j].flags & TextServer::GRAPHEME_IS_SPACE) == TextServer::GRAPHEME_IS_SPACE) && ((glyphs[j].flags & TextServer::GRAPHEME_IS_VIRTUAL) != TextServer::GRAPHEME_IS_VIRTUAL)) {
  1086. int yofs = (text_height - space_icon->get_height()) / 2 - ldata->get_line_ascent(line_wrap_index);
  1087. int xofs = (glyphs[j].advance * glyphs[j].repeat - space_icon->get_width()) / 2;
  1088. space_icon->draw(ci, Point2(char_pos + xofs, ofs_y + yofs), gl_color);
  1089. }
  1090. }
  1091. bool had_glyphs_drawn = false;
  1092. for (int k = 0; k < glyphs[j].repeat; k++) {
  1093. if (!clipped && (char_ofs + char_margin) >= xmargin_beg && (char_ofs + glyphs[j].advance + char_margin) <= xmargin_end) {
  1094. if (glyphs[j].font_rid != RID()) {
  1095. 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, gl_color);
  1096. had_glyphs_drawn = true;
  1097. } else if ((glyphs[j].flags & TextServer::GRAPHEME_IS_VIRTUAL) != TextServer::GRAPHEME_IS_VIRTUAL) {
  1098. 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, gl_color);
  1099. had_glyphs_drawn = true;
  1100. }
  1101. }
  1102. char_ofs += glyphs[j].advance;
  1103. }
  1104. if (had_glyphs_drawn) {
  1105. if (first_visible_char > glyphs[j].start) {
  1106. first_visible_char = glyphs[j].start;
  1107. } else if (last_visible_char < glyphs[j].end) {
  1108. last_visible_char = glyphs[j].end;
  1109. }
  1110. }
  1111. if ((char_ofs + char_margin) >= xmargin_end) {
  1112. break;
  1113. }
  1114. }
  1115. cache_entry.first_visible_chars.push_back(first_visible_char);
  1116. cache_entry.last_visible_chars.push_back(last_visible_char);
  1117. // is_line_folded
  1118. if (line_wrap_index == line_wrap_amount && line < text.size() - 1 && _is_line_hidden(line + 1)) {
  1119. int xofs = char_ofs + char_margin + ofs_x + (folded_eol_icon->get_width() / 2);
  1120. if (xofs >= xmargin_beg && xofs < xmargin_end) {
  1121. int yofs = (text_height - folded_eol_icon->get_height()) / 2 - ldata->get_line_ascent(line_wrap_index);
  1122. Color eol_color = code_folding_color;
  1123. eol_color.a = 1;
  1124. folded_eol_icon->draw(ci, Point2(xofs, ofs_y + yofs), eol_color);
  1125. }
  1126. }
  1127. // Carets.
  1128. // Prevent carets from disappearing at theme scales below 1.0 (if the caret width is 1).
  1129. const int caret_width = get_theme_constant(SNAME("caret_width")) * MAX(1, get_theme_default_base_scale());
  1130. for (int c = 0; c < carets.size(); c++) {
  1131. if (!clipped && get_caret_line(c) == line && carets_wrap_index[c] == line_wrap_index) {
  1132. carets.write[c].draw_pos.y = ofs_y + ldata->get_line_descent(line_wrap_index);
  1133. if (ime_text.is_empty() || ime_selection.y == 0) {
  1134. // Normal caret.
  1135. CaretInfo ts_caret;
  1136. if (!str.is_empty() || !ime_text.is_empty()) {
  1137. // Get carets.
  1138. ts_caret = TS->shaped_text_get_carets(rid, ime_text.is_empty() ? get_caret_column(c) : get_caret_column(c) + ime_selection.x);
  1139. } else {
  1140. // No carets, add one at the start.
  1141. int h = font->get_height(font_size);
  1142. if (rtl) {
  1143. ts_caret.l_dir = TextServer::DIRECTION_RTL;
  1144. ts_caret.l_caret = Rect2(Vector2(xmargin_end - char_margin + ofs_x, -h / 2), Size2(caret_width * 4, h));
  1145. } else {
  1146. ts_caret.l_dir = TextServer::DIRECTION_LTR;
  1147. ts_caret.l_caret = Rect2(Vector2(char_ofs, -h / 2), Size2(caret_width * 4, h));
  1148. }
  1149. }
  1150. 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())) {
  1151. carets.write[c].draw_pos.x = char_margin + ofs_x + ts_caret.l_caret.position.x;
  1152. } else {
  1153. carets.write[c].draw_pos.x = char_margin + ofs_x + ts_caret.t_caret.position.x;
  1154. }
  1155. if (get_caret_draw_pos(c).x >= xmargin_beg && get_caret_draw_pos(c).x < xmargin_end) {
  1156. carets.write[c].visible = true;
  1157. if (draw_caret || drag_caret_force_displayed) {
  1158. if (caret_type == CaretType::CARET_TYPE_BLOCK || overtype_mode) {
  1159. //Block or underline caret, draw trailing carets at full height.
  1160. int h = font->get_height(font_size);
  1161. if (ts_caret.t_caret != Rect2()) {
  1162. if (overtype_mode) {
  1163. ts_caret.t_caret.position.y = TS->shaped_text_get_descent(rid);
  1164. ts_caret.t_caret.size.y = caret_width;
  1165. } else {
  1166. ts_caret.t_caret.position.y = -TS->shaped_text_get_ascent(rid);
  1167. ts_caret.t_caret.size.y = h;
  1168. }
  1169. ts_caret.t_caret.position += Vector2(char_margin + ofs_x, ofs_y);
  1170. draw_rect(ts_caret.t_caret, caret_color, overtype_mode);
  1171. if (ts_caret.l_caret != Rect2() && ts_caret.l_dir != ts_caret.t_dir) {
  1172. // Draw split caret (leading part).
  1173. ts_caret.l_caret.position += Vector2(char_margin + ofs_x, ofs_y);
  1174. ts_caret.l_caret.size.x = caret_width;
  1175. draw_rect(ts_caret.l_caret, caret_color);
  1176. // Draw extra direction marker on top of split caret.
  1177. float d = (ts_caret.l_dir == TextServer::DIRECTION_LTR) ? 0.5 : -3;
  1178. Rect2 trect = Rect2(ts_caret.l_caret.position.x + d * caret_width, ts_caret.l_caret.position.y + ts_caret.l_caret.size.y - caret_width, 3 * caret_width, caret_width);
  1179. RenderingServer::get_singleton()->canvas_item_add_rect(ci, trect, caret_color);
  1180. }
  1181. } else { // End of the line.
  1182. if (gl_size > 0) {
  1183. // Adjust for actual line dimensions.
  1184. if (overtype_mode) {
  1185. ts_caret.l_caret.position.y = TS->shaped_text_get_descent(rid);
  1186. ts_caret.l_caret.size.y = caret_width;
  1187. } else {
  1188. ts_caret.l_caret.position.y = -TS->shaped_text_get_ascent(rid);
  1189. ts_caret.l_caret.size.y = h;
  1190. }
  1191. } else if (overtype_mode) {
  1192. ts_caret.l_caret.position.y += ts_caret.l_caret.size.y;
  1193. ts_caret.l_caret.size.y = caret_width;
  1194. }
  1195. if (Math::ceil(ts_caret.l_caret.position.x) >= TS->shaped_text_get_size(rid).x) {
  1196. ts_caret.l_caret.size.x = font->get_char_size('m', font_size).x;
  1197. } else {
  1198. ts_caret.l_caret.size.x = 3 * caret_width;
  1199. }
  1200. ts_caret.l_caret.position += Vector2(char_margin + ofs_x, ofs_y);
  1201. if (ts_caret.l_dir == TextServer::DIRECTION_RTL) {
  1202. ts_caret.l_caret.position.x -= ts_caret.l_caret.size.x;
  1203. }
  1204. draw_rect(ts_caret.l_caret, caret_color, overtype_mode);
  1205. }
  1206. } else {
  1207. // Normal caret.
  1208. if (ts_caret.l_caret != Rect2() && ts_caret.l_dir == TextServer::DIRECTION_AUTO) {
  1209. // Draw extra marker on top of mid caret.
  1210. Rect2 trect = Rect2(ts_caret.l_caret.position.x - 2.5 * caret_width, ts_caret.l_caret.position.y, 6 * caret_width, caret_width);
  1211. trect.position += Vector2(char_margin + ofs_x, ofs_y);
  1212. RenderingServer::get_singleton()->canvas_item_add_rect(ci, trect, caret_color);
  1213. } else if (ts_caret.l_caret != Rect2() && ts_caret.t_caret != Rect2() && ts_caret.l_dir != ts_caret.t_dir) {
  1214. // Draw extra direction marker on top of split caret.
  1215. float d = (ts_caret.l_dir == TextServer::DIRECTION_LTR) ? 0.5 : -3;
  1216. Rect2 trect = Rect2(ts_caret.l_caret.position.x + d * caret_width, ts_caret.l_caret.position.y + ts_caret.l_caret.size.y - caret_width, 3 * caret_width, caret_width);
  1217. trect.position += Vector2(char_margin + ofs_x, ofs_y);
  1218. RenderingServer::get_singleton()->canvas_item_add_rect(ci, trect, caret_color);
  1219. d = (ts_caret.t_dir == TextServer::DIRECTION_LTR) ? 0.5 : -3;
  1220. trect = Rect2(ts_caret.t_caret.position.x + d * caret_width, ts_caret.t_caret.position.y, 3 * caret_width, caret_width);
  1221. trect.position += Vector2(char_margin + ofs_x, ofs_y);
  1222. RenderingServer::get_singleton()->canvas_item_add_rect(ci, trect, caret_color);
  1223. }
  1224. ts_caret.l_caret.position += Vector2(char_margin + ofs_x, ofs_y);
  1225. ts_caret.l_caret.size.x = caret_width;
  1226. draw_rect(ts_caret.l_caret, caret_color);
  1227. ts_caret.t_caret.position += Vector2(char_margin + ofs_x, ofs_y);
  1228. ts_caret.t_caret.size.x = caret_width;
  1229. draw_rect(ts_caret.t_caret, caret_color);
  1230. }
  1231. }
  1232. }
  1233. }
  1234. if (!ime_text.is_empty()) {
  1235. {
  1236. // IME Intermediate text range.
  1237. Vector<Vector2> sel = TS->shaped_text_get_selection(rid, get_caret_column(c), get_caret_column(c) + ime_text.length());
  1238. for (int j = 0; j < sel.size(); j++) {
  1239. Rect2 rect = Rect2(sel[j].x + char_margin + ofs_x, ofs_y, sel[j].y - sel[j].x, text_height);
  1240. if (rect.position.x + rect.size.x <= xmargin_beg || rect.position.x > xmargin_end) {
  1241. continue;
  1242. }
  1243. if (rect.position.x < xmargin_beg) {
  1244. rect.size.x -= (xmargin_beg - rect.position.x);
  1245. rect.position.x = xmargin_beg;
  1246. } else if (rect.position.x + rect.size.x > xmargin_end) {
  1247. rect.size.x = xmargin_end - rect.position.x;
  1248. }
  1249. rect.size.y = caret_width;
  1250. draw_rect(rect, caret_color);
  1251. carets.write[c].draw_pos.x = rect.position.x;
  1252. }
  1253. }
  1254. {
  1255. // IME caret.
  1256. Vector<Vector2> sel = TS->shaped_text_get_selection(rid, get_caret_column(c) + ime_selection.x, get_caret_column(c) + ime_selection.x + ime_selection.y);
  1257. for (int j = 0; j < sel.size(); j++) {
  1258. Rect2 rect = Rect2(sel[j].x + char_margin + ofs_x, ofs_y, sel[j].y - sel[j].x, text_height);
  1259. if (rect.position.x + rect.size.x <= xmargin_beg || rect.position.x > xmargin_end) {
  1260. continue;
  1261. }
  1262. if (rect.position.x < xmargin_beg) {
  1263. rect.size.x -= (xmargin_beg - rect.position.x);
  1264. rect.position.x = xmargin_beg;
  1265. } else if (rect.position.x + rect.size.x > xmargin_end) {
  1266. rect.size.x = xmargin_end - rect.position.x;
  1267. }
  1268. rect.size.y = caret_width * 3;
  1269. draw_rect(rect, caret_color);
  1270. carets.write[c].draw_pos.x = rect.position.x;
  1271. }
  1272. }
  1273. }
  1274. }
  1275. }
  1276. }
  1277. if (!draw_placeholder) {
  1278. line_drawing_cache[line] = cache_entry;
  1279. }
  1280. }
  1281. if (has_focus()) {
  1282. if (get_viewport()->get_window_id() != DisplayServer::INVALID_WINDOW_ID && DisplayServer::get_singleton()->has_feature(DisplayServer::FEATURE_IME)) {
  1283. DisplayServer::get_singleton()->window_set_ime_active(true, get_viewport()->get_window_id());
  1284. Point2 pos = get_global_position() + get_caret_draw_pos();
  1285. if (get_window()->get_embedder()) {
  1286. pos += get_viewport()->get_popup_base_transform().get_origin();
  1287. }
  1288. DisplayServer::get_singleton()->window_set_ime_position(pos, get_viewport()->get_window_id());
  1289. }
  1290. }
  1291. } break;
  1292. case NOTIFICATION_FOCUS_ENTER: {
  1293. if (caret_blink_enabled) {
  1294. caret_blink_timer->start();
  1295. } else {
  1296. draw_caret = true;
  1297. }
  1298. if (get_viewport()->get_window_id() != DisplayServer::INVALID_WINDOW_ID && DisplayServer::get_singleton()->has_feature(DisplayServer::FEATURE_IME)) {
  1299. DisplayServer::get_singleton()->window_set_ime_active(true, get_viewport()->get_window_id());
  1300. Point2 pos = get_global_position() + get_caret_draw_pos();
  1301. if (get_window()->get_embedder()) {
  1302. pos += get_viewport()->get_popup_base_transform().get_origin();
  1303. }
  1304. DisplayServer::get_singleton()->window_set_ime_position(pos, get_viewport()->get_window_id());
  1305. }
  1306. if (DisplayServer::get_singleton()->has_feature(DisplayServer::FEATURE_VIRTUAL_KEYBOARD) && virtual_keyboard_enabled) {
  1307. int caret_start = -1;
  1308. int caret_end = -1;
  1309. if (!has_selection(0)) {
  1310. String full_text = _base_get_text(0, 0, get_caret_line(), get_caret_column());
  1311. caret_start = full_text.length();
  1312. } else {
  1313. String pre_text = _base_get_text(0, 0, get_selection_from_line(), get_selection_from_column());
  1314. String post_text = get_selected_text(0);
  1315. caret_start = pre_text.length();
  1316. caret_end = caret_start + post_text.length();
  1317. }
  1318. DisplayServer::get_singleton()->virtual_keyboard_show(get_text(), get_global_rect(), DisplayServer::KEYBOARD_TYPE_MULTILINE, -1, caret_start, caret_end);
  1319. }
  1320. } break;
  1321. case NOTIFICATION_FOCUS_EXIT: {
  1322. if (caret_blink_enabled) {
  1323. caret_blink_timer->stop();
  1324. }
  1325. if (get_viewport()->get_window_id() != DisplayServer::INVALID_WINDOW_ID && DisplayServer::get_singleton()->has_feature(DisplayServer::FEATURE_IME)) {
  1326. DisplayServer::get_singleton()->window_set_ime_position(Point2(), get_viewport()->get_window_id());
  1327. DisplayServer::get_singleton()->window_set_ime_active(false, get_viewport()->get_window_id());
  1328. }
  1329. if (!ime_text.is_empty()) {
  1330. ime_text = "";
  1331. ime_selection = Point2();
  1332. for (int i = 0; i < carets.size(); i++) {
  1333. text.invalidate_cache(get_caret_line(i), get_caret_column(i), true, ime_text);
  1334. }
  1335. }
  1336. if (DisplayServer::get_singleton()->has_feature(DisplayServer::FEATURE_VIRTUAL_KEYBOARD) && virtual_keyboard_enabled) {
  1337. DisplayServer::get_singleton()->virtual_keyboard_hide();
  1338. }
  1339. if (deselect_on_focus_loss_enabled && !selection_drag_attempt) {
  1340. deselect();
  1341. }
  1342. } break;
  1343. case MainLoop::NOTIFICATION_OS_IME_UPDATE: {
  1344. if (has_focus()) {
  1345. ime_text = DisplayServer::get_singleton()->ime_get_text();
  1346. ime_selection = DisplayServer::get_singleton()->ime_get_selection();
  1347. if (!ime_text.is_empty() && has_selection()) {
  1348. delete_selection();
  1349. }
  1350. for (int i = 0; i < carets.size(); i++) {
  1351. String t;
  1352. if (get_caret_column(i) >= 0) {
  1353. t = text[get_caret_line(i)].substr(0, get_caret_column(i)) + ime_text + text[get_caret_line(i)].substr(get_caret_column(i), text[get_caret_line(i)].length());
  1354. } else {
  1355. t = ime_text;
  1356. }
  1357. text.invalidate_cache(get_caret_line(i), get_caret_column(i), true, t, structured_text_parser(st_parser, st_args, t));
  1358. }
  1359. queue_redraw();
  1360. }
  1361. } break;
  1362. case NOTIFICATION_DRAG_BEGIN: {
  1363. selecting_mode = SelectionMode::SELECTION_MODE_NONE;
  1364. drag_action = true;
  1365. dragging_minimap = false;
  1366. dragging_selection = false;
  1367. can_drag_minimap = false;
  1368. click_select_held->stop();
  1369. } break;
  1370. case NOTIFICATION_DRAG_END: {
  1371. if (is_drag_successful()) {
  1372. if (selection_drag_attempt) {
  1373. selection_drag_attempt = false;
  1374. if (is_editable() && !Input::get_singleton()->is_key_pressed(Key::CTRL)) {
  1375. delete_selection();
  1376. } else if (deselect_on_focus_loss_enabled) {
  1377. deselect();
  1378. }
  1379. }
  1380. } else {
  1381. selection_drag_attempt = false;
  1382. }
  1383. drag_action = false;
  1384. drag_caret_force_displayed = false;
  1385. } break;
  1386. }
  1387. }
  1388. void TextEdit::unhandled_key_input(const Ref<InputEvent> &p_event) {
  1389. Ref<InputEventKey> k = p_event;
  1390. if (k.is_valid()) {
  1391. if (!k->is_pressed()) {
  1392. return;
  1393. }
  1394. // Handle Unicode (with modifiers active, process after shortcuts).
  1395. if (has_focus() && editable && (k->get_unicode() >= 32)) {
  1396. handle_unicode_input(k->get_unicode());
  1397. accept_event();
  1398. }
  1399. }
  1400. }
  1401. bool TextEdit::alt_input(const Ref<InputEvent> &p_gui_input) {
  1402. Ref<InputEventKey> k = p_gui_input;
  1403. if (k.is_valid()) {
  1404. if (!k->is_pressed()) {
  1405. if (alt_start && k->get_keycode() == Key::ALT) {
  1406. alt_start = false;
  1407. if ((alt_code > 0x31 && alt_code < 0xd800) || (alt_code > 0xdfff && alt_code <= 0x10ffff)) {
  1408. handle_unicode_input(alt_code);
  1409. }
  1410. return true;
  1411. }
  1412. return false;
  1413. }
  1414. if (k->is_alt_pressed()) {
  1415. if (!alt_start) {
  1416. if (k->get_keycode() == Key::KP_ADD) {
  1417. alt_start = true;
  1418. alt_code = 0;
  1419. return true;
  1420. }
  1421. } else {
  1422. if (k->get_keycode() >= Key::KEY_0 && k->get_keycode() <= Key::KEY_9) {
  1423. alt_code = alt_code << 4;
  1424. alt_code += (uint32_t)(k->get_keycode() - Key::KEY_0);
  1425. }
  1426. if (k->get_keycode() >= Key::KP_0 && k->get_keycode() <= Key::KP_9) {
  1427. alt_code = alt_code << 4;
  1428. alt_code += (uint32_t)(k->get_keycode() - Key::KP_0);
  1429. }
  1430. if (k->get_keycode() >= Key::A && k->get_keycode() <= Key::F) {
  1431. alt_code = alt_code << 4;
  1432. alt_code += (uint32_t)(k->get_keycode() - Key::A) + 10;
  1433. }
  1434. return true;
  1435. }
  1436. }
  1437. }
  1438. return false;
  1439. }
  1440. void TextEdit::gui_input(const Ref<InputEvent> &p_gui_input) {
  1441. ERR_FAIL_COND(p_gui_input.is_null());
  1442. double prev_v_scroll = v_scroll->get_value();
  1443. double prev_h_scroll = h_scroll->get_value();
  1444. Ref<InputEventMouseButton> mb = p_gui_input;
  1445. if (mb.is_valid()) {
  1446. Vector2i mpos = mb->get_position();
  1447. if (is_layout_rtl()) {
  1448. mpos.x = get_size().x - mpos.x;
  1449. }
  1450. if (ime_text.length() != 0) {
  1451. // Ignore mouse clicks in IME input mode.
  1452. return;
  1453. }
  1454. if (mb->is_pressed()) {
  1455. if (mb->get_button_index() == MouseButton::WHEEL_UP && !mb->is_command_or_control_pressed()) {
  1456. if (mb->is_shift_pressed()) {
  1457. h_scroll->set_value(h_scroll->get_value() - (100 * mb->get_factor()));
  1458. } else if (mb->is_alt_pressed()) {
  1459. // Scroll 5 times as fast as normal (like in Visual Studio Code).
  1460. _scroll_up(15 * mb->get_factor());
  1461. } else if (v_scroll->is_visible()) {
  1462. // Scroll 3 lines.
  1463. _scroll_up(3 * mb->get_factor());
  1464. }
  1465. }
  1466. if (mb->get_button_index() == MouseButton::WHEEL_DOWN && !mb->is_command_or_control_pressed()) {
  1467. if (mb->is_shift_pressed()) {
  1468. h_scroll->set_value(h_scroll->get_value() + (100 * mb->get_factor()));
  1469. } else if (mb->is_alt_pressed()) {
  1470. // Scroll 5 times as fast as normal (like in Visual Studio Code).
  1471. _scroll_down(15 * mb->get_factor());
  1472. } else if (v_scroll->is_visible()) {
  1473. // Scroll 3 lines.
  1474. _scroll_down(3 * mb->get_factor());
  1475. }
  1476. }
  1477. if (mb->get_button_index() == MouseButton::WHEEL_LEFT) {
  1478. h_scroll->set_value(h_scroll->get_value() - (100 * mb->get_factor()));
  1479. }
  1480. if (mb->get_button_index() == MouseButton::WHEEL_RIGHT) {
  1481. h_scroll->set_value(h_scroll->get_value() + (100 * mb->get_factor()));
  1482. }
  1483. if (mb->get_button_index() == MouseButton::LEFT) {
  1484. _reset_caret_blink_timer();
  1485. Point2i pos = get_line_column_at_pos(mpos);
  1486. int row = pos.y;
  1487. int col = pos.x;
  1488. int left_margin = style_normal->get_margin(SIDE_LEFT);
  1489. for (int i = 0; i < gutters.size(); i++) {
  1490. if (!gutters[i].draw || gutters[i].width <= 0) {
  1491. continue;
  1492. }
  1493. if (mpos.x >= left_margin && mpos.x <= left_margin + gutters[i].width) {
  1494. emit_signal(SNAME("gutter_clicked"), row, i);
  1495. return;
  1496. }
  1497. left_margin += gutters[i].width;
  1498. }
  1499. // minimap
  1500. if (draw_minimap) {
  1501. _update_minimap_click();
  1502. if (dragging_minimap) {
  1503. return;
  1504. }
  1505. }
  1506. int caret = carets.size() - 1;
  1507. int prev_col = get_caret_column(caret);
  1508. int prev_line = get_caret_line(caret);
  1509. const int triple_click_timeout = 600;
  1510. const int triple_click_tolerance = 5;
  1511. bool is_triple_click = (!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);
  1512. if (!is_mouse_over_selection() && !mb->is_double_click() && !is_triple_click) {
  1513. if (mb->is_alt_pressed()) {
  1514. prev_line = row;
  1515. prev_col = col;
  1516. caret = add_caret(row, col);
  1517. if (caret == -1) {
  1518. return;
  1519. }
  1520. carets.write[caret].selection.selecting_line = row;
  1521. carets.write[caret].selection.selecting_column = col;
  1522. last_dblclk = 0;
  1523. } else if (!mb->is_shift_pressed()) {
  1524. caret = 0;
  1525. remove_secondary_carets();
  1526. }
  1527. }
  1528. set_caret_line(row, false, true, 0, caret);
  1529. set_caret_column(col, false, caret);
  1530. selection_drag_attempt = false;
  1531. if (selecting_enabled && mb->is_shift_pressed() && (get_caret_column(caret) != prev_col || get_caret_line(caret) != prev_line)) {
  1532. if (!has_selection(caret)) {
  1533. carets.write[caret].selection.active = true;
  1534. selecting_mode = SelectionMode::SELECTION_MODE_POINTER;
  1535. carets.write[caret].selection.from_column = prev_col;
  1536. carets.write[caret].selection.from_line = prev_line;
  1537. carets.write[caret].selection.to_column = carets[caret].column;
  1538. carets.write[caret].selection.to_line = carets[caret].line;
  1539. if (carets[caret].selection.from_line > carets[caret].selection.to_line || (carets[caret].selection.from_line == carets[caret].selection.to_line && carets[caret].selection.from_column > carets[caret].selection.to_column)) {
  1540. SWAP(carets.write[caret].selection.from_column, carets.write[caret].selection.to_column);
  1541. SWAP(carets.write[caret].selection.from_line, carets.write[caret].selection.to_line);
  1542. carets.write[caret].selection.shiftclick_left = false;
  1543. } else {
  1544. carets.write[caret].selection.shiftclick_left = true;
  1545. }
  1546. carets.write[caret].selection.selecting_line = prev_line;
  1547. carets.write[caret].selection.selecting_column = prev_col;
  1548. caret_index_edit_dirty = true;
  1549. merge_overlapping_carets();
  1550. queue_redraw();
  1551. } else {
  1552. if (carets[caret].line < carets[caret].selection.selecting_line || (carets[caret].line == carets[caret].selection.selecting_line && carets[caret].column < carets[caret].selection.selecting_column)) {
  1553. if (carets[caret].selection.shiftclick_left) {
  1554. carets.write[caret].selection.shiftclick_left = !carets[caret].selection.shiftclick_left;
  1555. }
  1556. carets.write[caret].selection.from_column = carets[caret].column;
  1557. carets.write[caret].selection.from_line = carets[caret].line;
  1558. } else if (carets[caret].line > carets[caret].selection.selecting_line || (carets[caret].line == carets[caret].selection.selecting_line && carets[caret].column > carets[caret].selection.selecting_column)) {
  1559. if (!carets[caret].selection.shiftclick_left) {
  1560. SWAP(carets.write[caret].selection.from_column, carets.write[caret].selection.to_column);
  1561. SWAP(carets.write[caret].selection.from_line, carets.write[caret].selection.to_line);
  1562. carets.write[caret].selection.shiftclick_left = !carets[caret].selection.shiftclick_left;
  1563. }
  1564. carets.write[caret].selection.to_column = carets[caret].column;
  1565. carets.write[caret].selection.to_line = carets[caret].line;
  1566. } else {
  1567. deselect(caret);
  1568. }
  1569. caret_index_edit_dirty = true;
  1570. merge_overlapping_carets();
  1571. queue_redraw();
  1572. }
  1573. } else if (drag_and_drop_selection_enabled && is_mouse_over_selection()) {
  1574. set_selection_mode(SelectionMode::SELECTION_MODE_NONE, get_selection_line(caret), get_selection_column(caret), caret);
  1575. // We use the main caret for dragging, so reset this one.
  1576. set_caret_line(prev_line, false, true, 0, caret);
  1577. set_caret_column(prev_col, false, caret);
  1578. selection_drag_attempt = true;
  1579. } else if (caret == 0) {
  1580. deselect();
  1581. set_selection_mode(SelectionMode::SELECTION_MODE_POINTER, row, col);
  1582. }
  1583. if (is_triple_click) {
  1584. // Triple-click select line.
  1585. selecting_mode = SelectionMode::SELECTION_MODE_LINE;
  1586. selection_drag_attempt = false;
  1587. _update_selection_mode_line();
  1588. last_dblclk = 0;
  1589. } else if (mb->is_double_click() && text[get_caret_line(caret)].length()) {
  1590. // Double-click select word.
  1591. selecting_mode = SelectionMode::SELECTION_MODE_WORD;
  1592. _update_selection_mode_word();
  1593. last_dblclk = OS::get_singleton()->get_ticks_msec();
  1594. last_dblclk_pos = mb->get_position();
  1595. }
  1596. queue_redraw();
  1597. }
  1598. if (is_middle_mouse_paste_enabled() && mb->get_button_index() == MouseButton::MIDDLE && DisplayServer::get_singleton()->has_feature(DisplayServer::FEATURE_CLIPBOARD_PRIMARY)) {
  1599. paste_primary_clipboard();
  1600. }
  1601. if (mb->get_button_index() == MouseButton::RIGHT && (context_menu_enabled || is_move_caret_on_right_click_enabled())) {
  1602. _reset_caret_blink_timer();
  1603. Point2i pos = get_line_column_at_pos(mpos);
  1604. int row = pos.y;
  1605. int col = pos.x;
  1606. int caret = carets.size() - 1;
  1607. if (is_move_caret_on_right_click_enabled()) {
  1608. if (has_selection(caret)) {
  1609. int from_line = get_selection_from_line(caret);
  1610. int to_line = get_selection_to_line(caret);
  1611. int from_column = get_selection_from_column(caret);
  1612. int to_column = get_selection_to_column(caret);
  1613. if (row < from_line || row > to_line || (row == from_line && col < from_column) || (row == to_line && col > to_column)) {
  1614. // Right click is outside the selected text.
  1615. deselect(caret);
  1616. }
  1617. }
  1618. if (!has_selection(caret)) {
  1619. set_caret_line(row, true, false, 0, caret);
  1620. set_caret_column(col, true, caret);
  1621. }
  1622. merge_overlapping_carets();
  1623. }
  1624. if (context_menu_enabled) {
  1625. _update_context_menu();
  1626. menu->set_position(get_screen_position() + mpos);
  1627. menu->reset_size();
  1628. menu->popup();
  1629. grab_focus();
  1630. }
  1631. }
  1632. } else {
  1633. if (mb->get_button_index() == MouseButton::LEFT) {
  1634. if (selection_drag_attempt && is_mouse_over_selection()) {
  1635. remove_secondary_carets();
  1636. Point2i pos = get_line_column_at_pos(get_local_mouse_pos());
  1637. set_caret_line(pos.y, false, true, 0, 0);
  1638. set_caret_column(pos.x, true, 0);
  1639. deselect();
  1640. }
  1641. dragging_minimap = false;
  1642. dragging_selection = false;
  1643. can_drag_minimap = false;
  1644. click_select_held->stop();
  1645. if (!drag_action) {
  1646. selection_drag_attempt = false;
  1647. }
  1648. if (DisplayServer::get_singleton()->has_feature(DisplayServer::FEATURE_CLIPBOARD_PRIMARY)) {
  1649. DisplayServer::get_singleton()->clipboard_set_primary(get_selected_text());
  1650. }
  1651. }
  1652. // Notify to show soft keyboard.
  1653. notification(NOTIFICATION_FOCUS_ENTER);
  1654. }
  1655. }
  1656. const Ref<InputEventPanGesture> pan_gesture = p_gui_input;
  1657. if (pan_gesture.is_valid()) {
  1658. const real_t delta = pan_gesture->get_delta().y;
  1659. if (delta < 0) {
  1660. _scroll_up(-delta);
  1661. } else {
  1662. _scroll_down(delta);
  1663. }
  1664. h_scroll->set_value(h_scroll->get_value() + pan_gesture->get_delta().x * 100);
  1665. if (v_scroll->get_value() != prev_v_scroll || h_scroll->get_value() != prev_h_scroll) {
  1666. accept_event(); // Accept event if scroll changed.
  1667. }
  1668. return;
  1669. }
  1670. Ref<InputEventMouseMotion> mm = p_gui_input;
  1671. if (mm.is_valid()) {
  1672. Vector2i mpos = mm->get_position();
  1673. if (is_layout_rtl()) {
  1674. mpos.x = get_size().x - mpos.x;
  1675. }
  1676. if (mm->get_button_mask().has_flag(MouseButtonMask::LEFT) && get_viewport()->gui_get_drag_data() == Variant()) { // Ignore if dragging.
  1677. _reset_caret_blink_timer();
  1678. if (draw_minimap && !dragging_selection) {
  1679. _update_minimap_drag();
  1680. }
  1681. if (!dragging_minimap) {
  1682. switch (selecting_mode) {
  1683. case SelectionMode::SELECTION_MODE_POINTER: {
  1684. _update_selection_mode_pointer();
  1685. } break;
  1686. case SelectionMode::SELECTION_MODE_WORD: {
  1687. _update_selection_mode_word();
  1688. } break;
  1689. case SelectionMode::SELECTION_MODE_LINE: {
  1690. _update_selection_mode_line();
  1691. } break;
  1692. default: {
  1693. break;
  1694. }
  1695. }
  1696. }
  1697. }
  1698. // Check if user is hovering a different gutter, and update if yes.
  1699. Vector2i current_hovered_gutter = Vector2i(-1, -1);
  1700. int left_margin = style_normal->get_margin(SIDE_LEFT);
  1701. if (mpos.x <= left_margin + gutters_width + gutter_padding) {
  1702. int hovered_row = get_line_column_at_pos(mpos).y;
  1703. for (int i = 0; i < gutters.size(); i++) {
  1704. if (!gutters[i].draw || gutters[i].width <= 0) {
  1705. continue;
  1706. }
  1707. if (mpos.x >= left_margin && mpos.x < left_margin + gutters[i].width) {
  1708. // We are in this gutter i's horizontal area.
  1709. current_hovered_gutter = Vector2i(i, hovered_row);
  1710. break;
  1711. }
  1712. left_margin += gutters[i].width;
  1713. }
  1714. }
  1715. if (current_hovered_gutter != hovered_gutter) {
  1716. hovered_gutter = current_hovered_gutter;
  1717. queue_redraw();
  1718. }
  1719. if (drag_action && can_drop_data(mpos, get_viewport()->gui_get_drag_data())) {
  1720. drag_caret_force_displayed = true;
  1721. Point2i pos = get_line_column_at_pos(get_local_mouse_pos());
  1722. set_caret_line(pos.y, false, true, 0, 0);
  1723. set_caret_column(pos.x, true, 0);
  1724. dragging_selection = true;
  1725. }
  1726. }
  1727. if (draw_minimap && !dragging_selection) {
  1728. _update_minimap_hover();
  1729. }
  1730. if (v_scroll->get_value() != prev_v_scroll || h_scroll->get_value() != prev_h_scroll) {
  1731. accept_event(); // Accept event if scroll changed.
  1732. }
  1733. Ref<InputEventKey> k = p_gui_input;
  1734. if (k.is_valid()) {
  1735. if (alt_input(p_gui_input)) {
  1736. accept_event();
  1737. return;
  1738. }
  1739. if (!k->is_pressed()) {
  1740. return;
  1741. }
  1742. // If a modifier has been pressed, and nothing else, return.
  1743. if (k->get_keycode() == Key::CTRL || k->get_keycode() == Key::ALT || k->get_keycode() == Key::SHIFT || k->get_keycode() == Key::META) {
  1744. return;
  1745. }
  1746. _reset_caret_blink_timer();
  1747. // Allow unicode handling if:
  1748. // * No Modifiers are pressed (except shift)
  1749. bool allow_unicode_handling = !(k->is_command_or_control_pressed() || k->is_ctrl_pressed() || k->is_alt_pressed() || k->is_meta_pressed());
  1750. // Check and handle all built in shortcuts.
  1751. // NEWLINES.
  1752. if (k->is_action("ui_text_newline_above", true)) {
  1753. _new_line(false, true);
  1754. accept_event();
  1755. return;
  1756. }
  1757. if (k->is_action("ui_text_newline_blank", true)) {
  1758. _new_line(false);
  1759. accept_event();
  1760. return;
  1761. }
  1762. if (k->is_action("ui_text_newline", true)) {
  1763. _new_line();
  1764. accept_event();
  1765. return;
  1766. }
  1767. // BACKSPACE AND DELETE.
  1768. if (k->is_action("ui_text_backspace_all_to_left", true)) {
  1769. _do_backspace(false, true);
  1770. accept_event();
  1771. return;
  1772. }
  1773. if (k->is_action("ui_text_backspace_word", true)) {
  1774. _do_backspace(true);
  1775. accept_event();
  1776. return;
  1777. }
  1778. if (k->is_action("ui_text_backspace", true)) {
  1779. _do_backspace();
  1780. accept_event();
  1781. return;
  1782. }
  1783. if (k->is_action("ui_text_delete_all_to_right", true)) {
  1784. _delete(false, true);
  1785. accept_event();
  1786. return;
  1787. }
  1788. if (k->is_action("ui_text_delete_word", true)) {
  1789. _delete(true);
  1790. accept_event();
  1791. return;
  1792. }
  1793. if (k->is_action("ui_text_delete", true)) {
  1794. _delete();
  1795. accept_event();
  1796. return;
  1797. }
  1798. // SCROLLING.
  1799. if (k->is_action("ui_text_scroll_up", true)) {
  1800. _scroll_lines_up();
  1801. accept_event();
  1802. return;
  1803. }
  1804. if (k->is_action("ui_text_scroll_down", true)) {
  1805. _scroll_lines_down();
  1806. accept_event();
  1807. return;
  1808. }
  1809. if (is_shortcut_keys_enabled()) {
  1810. // SELECT ALL, SELECT WORD UNDER CARET, ADD SELECTION FOR NEXT OCCURRENCE,
  1811. // CLEAR CARETS AND SELECTIONS, CUT, COPY, PASTE.
  1812. if (k->is_action("ui_text_select_all", true)) {
  1813. select_all();
  1814. accept_event();
  1815. return;
  1816. }
  1817. if (k->is_action("ui_text_select_word_under_caret", true)) {
  1818. select_word_under_caret();
  1819. accept_event();
  1820. return;
  1821. }
  1822. if (k->is_action("ui_text_add_selection_for_next_occurrence", true)) {
  1823. add_selection_for_next_occurrence();
  1824. accept_event();
  1825. return;
  1826. }
  1827. if (k->is_action("ui_text_clear_carets_and_selection", true)) {
  1828. // Since the default shortcut is ESC, accepts the event only if it's actually performed.
  1829. if (_clear_carets_and_selection()) {
  1830. accept_event();
  1831. return;
  1832. }
  1833. }
  1834. if (k->is_action("ui_cut", true)) {
  1835. cut();
  1836. accept_event();
  1837. return;
  1838. }
  1839. if (k->is_action("ui_copy", true)) {
  1840. copy();
  1841. accept_event();
  1842. return;
  1843. }
  1844. if (k->is_action("ui_paste", true)) {
  1845. paste();
  1846. accept_event();
  1847. return;
  1848. }
  1849. // UNDO/REDO.
  1850. if (k->is_action("ui_undo", true)) {
  1851. undo();
  1852. accept_event();
  1853. return;
  1854. }
  1855. if (k->is_action("ui_redo", true)) {
  1856. redo();
  1857. accept_event();
  1858. return;
  1859. }
  1860. if (k->is_action("ui_text_caret_add_below", true)) {
  1861. add_caret_at_carets(true);
  1862. accept_event();
  1863. return;
  1864. }
  1865. if (k->is_action("ui_text_caret_add_above", true)) {
  1866. add_caret_at_carets(false);
  1867. accept_event();
  1868. return;
  1869. }
  1870. }
  1871. // MISC.
  1872. if (k->is_action("ui_menu", true)) {
  1873. if (context_menu_enabled) {
  1874. _update_context_menu();
  1875. adjust_viewport_to_caret();
  1876. menu->set_position(get_screen_position() + get_caret_draw_pos());
  1877. menu->reset_size();
  1878. menu->popup();
  1879. menu->grab_focus();
  1880. }
  1881. accept_event();
  1882. return;
  1883. }
  1884. if (k->is_action("ui_text_toggle_insert_mode", true)) {
  1885. set_overtype_mode_enabled(!overtype_mode);
  1886. accept_event();
  1887. return;
  1888. }
  1889. if (k->is_action("ui_swap_input_direction", true)) {
  1890. _swap_current_input_direction();
  1891. accept_event();
  1892. return;
  1893. }
  1894. // CARET MOVEMENT
  1895. k = k->duplicate();
  1896. bool shift_pressed = k->is_shift_pressed();
  1897. // Remove shift or else actions will not match. Use above variable for selection.
  1898. k->set_shift_pressed(false);
  1899. // CARET MOVEMENT - LEFT, RIGHT.
  1900. if (k->is_action("ui_text_caret_word_left", true)) {
  1901. _move_caret_left(shift_pressed, true);
  1902. accept_event();
  1903. return;
  1904. }
  1905. if (k->is_action("ui_text_caret_left", true)) {
  1906. _move_caret_left(shift_pressed, false);
  1907. accept_event();
  1908. return;
  1909. }
  1910. if (k->is_action("ui_text_caret_word_right", true)) {
  1911. _move_caret_right(shift_pressed, true);
  1912. accept_event();
  1913. return;
  1914. }
  1915. if (k->is_action("ui_text_caret_right", true)) {
  1916. _move_caret_right(shift_pressed, false);
  1917. accept_event();
  1918. return;
  1919. }
  1920. // CARET MOVEMENT - UP, DOWN.
  1921. if (k->is_action("ui_text_caret_up", true)) {
  1922. _move_caret_up(shift_pressed);
  1923. accept_event();
  1924. return;
  1925. }
  1926. if (k->is_action("ui_text_caret_down", true)) {
  1927. _move_caret_down(shift_pressed);
  1928. accept_event();
  1929. return;
  1930. }
  1931. // CARET MOVEMENT - DOCUMENT START/END.
  1932. if (k->is_action("ui_text_caret_document_start", true)) { // && shift_pressed) {
  1933. _move_caret_document_start(shift_pressed);
  1934. accept_event();
  1935. return;
  1936. }
  1937. if (k->is_action("ui_text_caret_document_end", true)) { // && shift_pressed) {
  1938. _move_caret_document_end(shift_pressed);
  1939. accept_event();
  1940. return;
  1941. }
  1942. // CARET MOVEMENT - LINE START/END.
  1943. if (k->is_action("ui_text_caret_line_start", true)) {
  1944. _move_caret_to_line_start(shift_pressed);
  1945. accept_event();
  1946. return;
  1947. }
  1948. if (k->is_action("ui_text_caret_line_end", true)) {
  1949. _move_caret_to_line_end(shift_pressed);
  1950. accept_event();
  1951. return;
  1952. }
  1953. // CARET MOVEMENT - PAGE UP/DOWN.
  1954. if (k->is_action("ui_text_caret_page_up", true)) {
  1955. _move_caret_page_up(shift_pressed);
  1956. accept_event();
  1957. return;
  1958. }
  1959. if (k->is_action("ui_text_caret_page_down", true)) {
  1960. _move_caret_page_down(shift_pressed);
  1961. accept_event();
  1962. return;
  1963. }
  1964. // Handle tab as it has no set unicode value.
  1965. if (k->is_action("ui_text_indent", true)) {
  1966. if (editable) {
  1967. insert_text_at_caret("\t");
  1968. }
  1969. accept_event();
  1970. return;
  1971. }
  1972. // Handle Unicode (if no modifiers active).
  1973. if (allow_unicode_handling && editable && k->get_unicode() >= 32) {
  1974. handle_unicode_input(k->get_unicode());
  1975. accept_event();
  1976. return;
  1977. }
  1978. }
  1979. }
  1980. /* Input actions. */
  1981. void TextEdit::_swap_current_input_direction() {
  1982. if (input_direction == TEXT_DIRECTION_LTR) {
  1983. input_direction = TEXT_DIRECTION_RTL;
  1984. } else {
  1985. input_direction = TEXT_DIRECTION_LTR;
  1986. }
  1987. for (int i = 0; i < carets.size(); i++) {
  1988. set_caret_column(get_caret_column(i), i == 0, i);
  1989. }
  1990. queue_redraw();
  1991. }
  1992. void TextEdit::_new_line(bool p_split_current_line, bool p_above) {
  1993. if (!editable) {
  1994. return;
  1995. }
  1996. begin_complex_operation();
  1997. Vector<int> caret_edit_order = get_caret_index_edit_order();
  1998. for (const int &i : caret_edit_order) {
  1999. bool first_line = false;
  2000. if (!p_split_current_line) {
  2001. deselect(i);
  2002. if (p_above) {
  2003. if (get_caret_line(i) > 0) {
  2004. set_caret_line(get_caret_line(i) - 1, false, true, 0, i);
  2005. set_caret_column(text[get_caret_line(i)].length(), i == 0, i);
  2006. } else {
  2007. set_caret_column(0, i == 0, i);
  2008. first_line = true;
  2009. }
  2010. } else {
  2011. set_caret_column(text[get_caret_line(i)].length(), i == 0, i);
  2012. }
  2013. }
  2014. insert_text_at_caret("\n", i);
  2015. if (first_line) {
  2016. set_caret_line(0, i == 0, true, 0, i);
  2017. }
  2018. }
  2019. end_complex_operation();
  2020. }
  2021. void TextEdit::_move_caret_left(bool p_select, bool p_move_by_word) {
  2022. for (int i = 0; i < carets.size(); i++) {
  2023. // Handle selection.
  2024. if (p_select) {
  2025. _pre_shift_selection(i);
  2026. } else if (has_selection(i) && !p_move_by_word) {
  2027. // If a selection is active, move caret to start of selection.
  2028. set_caret_line(get_selection_from_line(i), false, true, 0, i);
  2029. set_caret_column(get_selection_from_column(i), i == 0, i);
  2030. deselect(i);
  2031. continue;
  2032. } else {
  2033. deselect(i);
  2034. }
  2035. if (p_move_by_word) {
  2036. int cc = get_caret_column(i);
  2037. // 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.
  2038. if (cc == 0 && get_caret_line(i) > 0) {
  2039. set_caret_line(get_caret_line(i) - 1, false, true, 0, i);
  2040. set_caret_column(text[get_caret_line(i)].length(), i == 0, i);
  2041. } else {
  2042. PackedInt32Array words = TS->shaped_text_get_word_breaks(text.get_line_data(get_caret_line(i))->get_rid());
  2043. if (words.is_empty() || cc <= words[0]) {
  2044. // This solves the scenario where there are no words but glyfs that can be ignored.
  2045. cc = 0;
  2046. } else {
  2047. for (int j = words.size() - 2; j >= 0; j = j - 2) {
  2048. if (words[j] < cc) {
  2049. cc = words[j];
  2050. break;
  2051. }
  2052. }
  2053. }
  2054. set_caret_column(cc, i == 0, i);
  2055. }
  2056. } else {
  2057. // 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.
  2058. if (get_caret_column(i) == 0) {
  2059. if (get_caret_line(i) > 0) {
  2060. set_caret_line(get_caret_line(i) - get_next_visible_line_offset_from(CLAMP(get_caret_line(i) - 1, 0, text.size() - 1), -1), false, true, 0, i);
  2061. set_caret_column(text[get_caret_line(i)].length(), i == 0, i);
  2062. }
  2063. } else {
  2064. if (caret_mid_grapheme_enabled) {
  2065. set_caret_column(get_caret_column(i) - 1, i == 0, i);
  2066. } else {
  2067. set_caret_column(TS->shaped_text_prev_grapheme_pos(text.get_line_data(get_caret_line(i))->get_rid(), get_caret_column(i)), i == 0, i);
  2068. }
  2069. }
  2070. }
  2071. if (p_select) {
  2072. _post_shift_selection(i);
  2073. }
  2074. }
  2075. merge_overlapping_carets();
  2076. }
  2077. void TextEdit::_move_caret_right(bool p_select, bool p_move_by_word) {
  2078. for (int i = 0; i < carets.size(); i++) {
  2079. // Handle selection
  2080. if (p_select) {
  2081. _pre_shift_selection(i);
  2082. } else if (has_selection(i) && !p_move_by_word) {
  2083. // If a selection is active, move caret to end of selection
  2084. set_caret_line(get_selection_to_line(i), false, true, 0, i);
  2085. set_caret_column(get_selection_to_column(i), i == 0, i);
  2086. deselect(i);
  2087. continue;
  2088. } else {
  2089. deselect(i);
  2090. }
  2091. if (p_move_by_word) {
  2092. int cc = get_caret_column(i);
  2093. // If the caret is at the end of the line, and not on the last line, move it down to the beginning of the next line.
  2094. if (cc == text[get_caret_line(i)].length() && get_caret_line(i) < text.size() - 1) {
  2095. set_caret_line(get_caret_line(i) + 1, false, true, 0, i);
  2096. set_caret_column(0, i == 0, i);
  2097. } else {
  2098. PackedInt32Array words = TS->shaped_text_get_word_breaks(text.get_line_data(get_caret_line(i))->get_rid());
  2099. if (words.is_empty() || cc >= words[words.size() - 1]) {
  2100. // This solves the scenario where there are no words but glyfs that can be ignored.
  2101. cc = text[get_caret_line(i)].length();
  2102. } else {
  2103. for (int j = 1; j < words.size(); j = j + 2) {
  2104. if (words[j] > cc) {
  2105. cc = words[j];
  2106. break;
  2107. }
  2108. }
  2109. }
  2110. set_caret_column(cc, i == 0, i);
  2111. }
  2112. } else {
  2113. // If we are at the end of the line, move the caret to the next line down.
  2114. if (get_caret_column(i) == text[get_caret_line(i)].length()) {
  2115. if (get_caret_line(i) < text.size() - 1) {
  2116. set_caret_line(get_caret_line(i) + get_next_visible_line_offset_from(CLAMP(get_caret_line(i) + 1, 0, text.size() - 1), 1), false, false, 0, i);
  2117. set_caret_column(0, i == 0, i);
  2118. }
  2119. } else {
  2120. if (caret_mid_grapheme_enabled) {
  2121. set_caret_column(get_caret_column(i) + 1, i == 0, i);
  2122. } else {
  2123. set_caret_column(TS->shaped_text_next_grapheme_pos(text.get_line_data(get_caret_line(i))->get_rid(), get_caret_column(i)), i == 0, i);
  2124. }
  2125. }
  2126. }
  2127. if (p_select) {
  2128. _post_shift_selection(i);
  2129. }
  2130. }
  2131. merge_overlapping_carets();
  2132. }
  2133. void TextEdit::_move_caret_up(bool p_select) {
  2134. for (int i = 0; i < carets.size(); i++) {
  2135. if (p_select) {
  2136. _pre_shift_selection(i);
  2137. } else {
  2138. deselect(i);
  2139. }
  2140. int cur_wrap_index = get_caret_wrap_index(i);
  2141. if (cur_wrap_index > 0) {
  2142. set_caret_line(get_caret_line(i), true, false, cur_wrap_index - 1, i);
  2143. } else if (get_caret_line(i) == 0) {
  2144. set_caret_column(0, i == 0, i);
  2145. } else {
  2146. int new_line = get_caret_line(i) - get_next_visible_line_offset_from(get_caret_line(i) - 1, -1);
  2147. if (is_line_wrapped(new_line)) {
  2148. set_caret_line(new_line, i == 0, false, get_line_wrap_count(new_line), i);
  2149. } else {
  2150. set_caret_line(new_line, i == 0, false, 0, i);
  2151. }
  2152. }
  2153. if (p_select) {
  2154. _post_shift_selection(i);
  2155. }
  2156. }
  2157. merge_overlapping_carets();
  2158. }
  2159. void TextEdit::_move_caret_down(bool p_select) {
  2160. for (int i = 0; i < carets.size(); i++) {
  2161. if (p_select) {
  2162. _pre_shift_selection(i);
  2163. } else {
  2164. deselect(i);
  2165. }
  2166. int cur_wrap_index = get_caret_wrap_index(i);
  2167. if (cur_wrap_index < get_line_wrap_count(get_caret_line(i))) {
  2168. set_caret_line(get_caret_line(i), i == 0, false, cur_wrap_index + 1, i);
  2169. } else if (get_caret_line(i) == get_last_unhidden_line()) {
  2170. set_caret_column(text[get_caret_line(i)].length());
  2171. } else {
  2172. int new_line = get_caret_line(i) + get_next_visible_line_offset_from(CLAMP(get_caret_line(i) + 1, 0, text.size() - 1), 1);
  2173. set_caret_line(new_line, i == 0, false, 0, i);
  2174. }
  2175. if (p_select) {
  2176. _post_shift_selection(i);
  2177. }
  2178. }
  2179. merge_overlapping_carets();
  2180. }
  2181. void TextEdit::_move_caret_to_line_start(bool p_select) {
  2182. for (int i = 0; i < carets.size(); i++) {
  2183. if (p_select) {
  2184. _pre_shift_selection(i);
  2185. } else {
  2186. deselect(i);
  2187. }
  2188. // Move caret column to start of wrapped row and then to start of text.
  2189. Vector<String> rows = get_line_wrapped_text(get_caret_line(i));
  2190. int wi = get_caret_wrap_index(i);
  2191. int row_start_col = 0;
  2192. for (int j = 0; j < wi; j++) {
  2193. row_start_col += rows[j].length();
  2194. }
  2195. if (get_caret_column(i) == row_start_col || wi == 0) {
  2196. // Compute whitespace symbols sequence length.
  2197. int current_line_whitespace_len = get_first_non_whitespace_column(get_caret_line(i));
  2198. if (get_caret_column(i) == current_line_whitespace_len) {
  2199. set_caret_column(0, i == 0, i);
  2200. } else {
  2201. set_caret_column(current_line_whitespace_len, i == 0, i);
  2202. }
  2203. } else {
  2204. set_caret_column(row_start_col, i == 0, i);
  2205. }
  2206. if (p_select) {
  2207. _post_shift_selection(i);
  2208. }
  2209. }
  2210. merge_overlapping_carets();
  2211. }
  2212. void TextEdit::_move_caret_to_line_end(bool p_select) {
  2213. for (int i = 0; i < carets.size(); i++) {
  2214. if (p_select) {
  2215. _pre_shift_selection(i);
  2216. } else {
  2217. deselect(i);
  2218. }
  2219. // Move caret column to end of wrapped row and then to end of text.
  2220. Vector<String> rows = get_line_wrapped_text(get_caret_line(i));
  2221. int wi = get_caret_wrap_index(i);
  2222. int row_end_col = -1;
  2223. for (int j = 0; j < wi + 1; j++) {
  2224. row_end_col += rows[j].length();
  2225. }
  2226. if (wi == rows.size() - 1 || get_caret_column(i) == row_end_col) {
  2227. set_caret_column(text[get_caret_line(i)].length(), i == 0, i);
  2228. } else {
  2229. set_caret_column(row_end_col, i == 0, i);
  2230. }
  2231. carets.write[i].last_fit_x = INT_MAX;
  2232. if (p_select) {
  2233. _post_shift_selection(i);
  2234. }
  2235. }
  2236. merge_overlapping_carets();
  2237. }
  2238. void TextEdit::_move_caret_page_up(bool p_select) {
  2239. for (int i = 0; i < carets.size(); i++) {
  2240. if (p_select) {
  2241. _pre_shift_selection(i);
  2242. } else {
  2243. deselect(i);
  2244. }
  2245. Point2i next_line = get_next_visible_line_index_offset_from(get_caret_line(i), get_caret_wrap_index(i), -get_visible_line_count());
  2246. int n_line = get_caret_line(i) - next_line.x + 1;
  2247. set_caret_line(n_line, i == 0, false, next_line.y, i);
  2248. if (p_select) {
  2249. _post_shift_selection(i);
  2250. }
  2251. }
  2252. merge_overlapping_carets();
  2253. }
  2254. void TextEdit::_move_caret_page_down(bool p_select) {
  2255. for (int i = 0; i < carets.size(); i++) {
  2256. if (p_select) {
  2257. _pre_shift_selection(i);
  2258. } else {
  2259. deselect(i);
  2260. }
  2261. Point2i next_line = get_next_visible_line_index_offset_from(get_caret_line(i), get_caret_wrap_index(i), get_visible_line_count());
  2262. int n_line = get_caret_line(i) + next_line.x - 1;
  2263. set_caret_line(n_line, i == 0, false, next_line.y, i);
  2264. if (p_select) {
  2265. _post_shift_selection(i);
  2266. }
  2267. }
  2268. merge_overlapping_carets();
  2269. }
  2270. void TextEdit::_do_backspace(bool p_word, bool p_all_to_left) {
  2271. if (!editable) {
  2272. return;
  2273. }
  2274. start_action(EditAction::ACTION_BACKSPACE);
  2275. Vector<int> carets_to_remove;
  2276. Vector<int> caret_edit_order = get_caret_index_edit_order();
  2277. for (int i = 0; i < caret_edit_order.size(); i++) {
  2278. int caret_idx = caret_edit_order[i];
  2279. if (get_caret_column(caret_idx) == 0 && get_caret_line(caret_idx) == 0 && !has_selection(caret_idx)) {
  2280. continue;
  2281. }
  2282. if (has_selection(caret_idx) || (!p_all_to_left && !p_word) || get_caret_column(caret_idx) == 0) {
  2283. backspace(caret_idx);
  2284. continue;
  2285. }
  2286. if (p_all_to_left) {
  2287. int caret_current_column = get_caret_column(caret_idx);
  2288. set_caret_column(0, caret_idx == 0, caret_idx);
  2289. _remove_text(get_caret_line(caret_idx), 0, get_caret_line(caret_idx), caret_current_column);
  2290. adjust_carets_after_edit(caret_idx, get_caret_line(caret_idx), caret_current_column, get_caret_line(caret_idx), get_caret_column(caret_idx));
  2291. // Check for any overlapping carets since we removed the entire line.
  2292. for (int j = i + 1; j < caret_edit_order.size(); j++) {
  2293. // Selection only end on this line, only the one as carets cannot overlap.
  2294. if (has_selection(caret_edit_order[j]) && get_selection_from_line(caret_edit_order[j]) != get_caret_line(caret_idx) && get_selection_to_line(caret_edit_order[j]) == get_caret_line(caret_idx)) {
  2295. carets.write[caret_edit_order[j]].selection.to_column = 0;
  2296. break;
  2297. }
  2298. // Check for caret.
  2299. if (get_caret_line(caret_edit_order[j]) != get_caret_line(caret_idx) || (has_selection(caret_edit_order[j]) && get_selection_from_line(caret_edit_order[j]) != get_caret_line(caret_idx))) {
  2300. break;
  2301. }
  2302. deselect(caret_edit_order[j]);
  2303. carets_to_remove.push_back(caret_edit_order[j]);
  2304. set_caret_column(0, caret_idx == 0, caret_idx);
  2305. i = j;
  2306. }
  2307. continue;
  2308. }
  2309. if (p_word) {
  2310. // Save here as the caret may change when resolving overlaps.
  2311. int from_column = get_caret_column(caret_idx);
  2312. int column = get_caret_column(caret_idx);
  2313. // Check for the case "<word><space><caret>" and ignore the space.
  2314. // No need to check for column being 0 since it is checked above.
  2315. if (is_whitespace(text[get_caret_line(caret_idx)][get_caret_column(caret_idx) - 1])) {
  2316. column -= 1;
  2317. }
  2318. // Get a list with the indices of the word bounds of the given text line.
  2319. const PackedInt32Array words = TS->shaped_text_get_word_breaks(text.get_line_data(get_caret_line(caret_idx))->get_rid());
  2320. if (words.is_empty() || column <= words[0]) {
  2321. // If "words" is empty, meaning no words are left, we can remove everything until the beginning of the line.
  2322. column = 0;
  2323. } else {
  2324. // Otherwise search for the first word break that is smaller than the index from we're currently deleting.
  2325. for (int c = words.size() - 2; c >= 0; c = c - 2) {
  2326. if (words[c] < column) {
  2327. column = words[c];
  2328. break;
  2329. }
  2330. }
  2331. }
  2332. // Check for any other carets in this range.
  2333. int overlapping_caret_index = -1;
  2334. for (int j = i + 1; j < caret_edit_order.size(); j++) {
  2335. // Check caret and selection in on the right line.
  2336. if (get_caret_line(caret_edit_order[j]) != get_caret_line(caret_idx) && (!has_selection(caret_edit_order[j]) || get_selection_to_line(caret_edit_order[j]) != get_caret_line(caret_idx))) {
  2337. break;
  2338. }
  2339. // If it has a selection, check it ends with in the range.
  2340. if ((has_selection(caret_edit_order[j]) && get_selection_to_column(caret_edit_order[j]) < column)) {
  2341. break;
  2342. }
  2343. // If it has a selection and it starts outside our word, we need to adjust the selection, and handle it later to prevent overlap.
  2344. if ((has_selection(caret_edit_order[j]) && get_selection_from_column(caret_edit_order[j]) < column)) {
  2345. carets.write[caret_edit_order[j]].selection.to_column = column;
  2346. overlapping_caret_index = caret_edit_order[j];
  2347. break;
  2348. }
  2349. // Otherwise we can remove it.
  2350. if (get_caret_column(caret_edit_order[j]) > column || (has_selection(caret_edit_order[j]) && get_selection_from_column(caret_edit_order[j]) > column)) {
  2351. deselect(caret_edit_order[j]);
  2352. carets_to_remove.push_back(caret_edit_order[j]);
  2353. set_caret_column(0, caret_idx == 0, caret_idx);
  2354. i = j;
  2355. }
  2356. }
  2357. _remove_text(get_caret_line(caret_idx), column, get_caret_line(caret_idx), from_column);
  2358. set_caret_line(get_caret_line(caret_idx), false, true, 0, caret_idx);
  2359. set_caret_column(column, caret_idx == 0, caret_idx);
  2360. adjust_carets_after_edit(caret_idx, get_caret_line(caret_idx), column, get_caret_line(caret_idx), from_column);
  2361. // Now we can clean up the overlapping caret.
  2362. if (overlapping_caret_index != -1) {
  2363. backspace(overlapping_caret_index);
  2364. i++;
  2365. carets_to_remove.push_back(overlapping_caret_index);
  2366. set_caret_column(get_caret_column(overlapping_caret_index), caret_idx == 0, caret_idx);
  2367. }
  2368. continue;
  2369. }
  2370. }
  2371. // Sort and remove backwards to preserve indexes.
  2372. carets_to_remove.sort();
  2373. for (int i = carets_to_remove.size() - 1; i >= 0; i--) {
  2374. remove_caret(carets_to_remove[i]);
  2375. }
  2376. end_action();
  2377. }
  2378. void TextEdit::_delete(bool p_word, bool p_all_to_right) {
  2379. if (!editable) {
  2380. return;
  2381. }
  2382. start_action(EditAction::ACTION_DELETE);
  2383. Vector<int> carets_to_remove;
  2384. Vector<int> caret_edit_order = get_caret_index_edit_order();
  2385. for (int i = 0; i < caret_edit_order.size(); i++) {
  2386. int caret_idx = caret_edit_order[i];
  2387. if (has_selection(caret_idx)) {
  2388. delete_selection(caret_idx);
  2389. continue;
  2390. }
  2391. int curline_len = text[get_caret_line(caret_idx)].length();
  2392. if (get_caret_line(caret_idx) == text.size() - 1 && get_caret_column(caret_idx) == curline_len) {
  2393. continue; // Last line, last column: Nothing to do.
  2394. }
  2395. int next_line = get_caret_column(caret_idx) < curline_len ? get_caret_line(caret_idx) : get_caret_line(caret_idx) + 1;
  2396. int next_column;
  2397. if (p_all_to_right) {
  2398. // Get caret furthest to the left
  2399. for (int j = i + 1; j < caret_edit_order.size(); j++) {
  2400. if (get_caret_line(caret_edit_order[j]) != get_caret_line(caret_idx)) {
  2401. break;
  2402. }
  2403. if (has_selection(caret_edit_order[j]) && get_selection_from_line(caret_edit_order[j]) != get_caret_line(caret_idx)) {
  2404. break;
  2405. }
  2406. if (!has_selection(caret_edit_order[j])) {
  2407. i = j;
  2408. caret_idx = caret_edit_order[i];
  2409. }
  2410. }
  2411. if (get_caret_column(caret_idx) == curline_len) {
  2412. continue;
  2413. }
  2414. // Delete everything to right of caret
  2415. next_column = curline_len;
  2416. next_line = get_caret_line(caret_idx);
  2417. // Remove overlapping carets.
  2418. for (int j = i - 1; j >= 0; j--) {
  2419. if (get_caret_line(caret_edit_order[j]) != get_caret_line(caret_idx)) {
  2420. break;
  2421. }
  2422. carets_to_remove.push_back(caret_edit_order[j]);
  2423. }
  2424. } else if (p_word && get_caret_column(caret_idx) < curline_len - 1) {
  2425. // Delete next word to right of caret
  2426. int line = get_caret_line(caret_idx);
  2427. int column = get_caret_column(caret_idx);
  2428. PackedInt32Array words = TS->shaped_text_get_word_breaks(text.get_line_data(line)->get_rid());
  2429. for (int j = 1; j < words.size(); j = j + 2) {
  2430. if (words[j] > column) {
  2431. column = words[j];
  2432. break;
  2433. }
  2434. }
  2435. next_line = line;
  2436. next_column = column;
  2437. // Remove overlapping carets.
  2438. for (int j = i - 1; j >= 0; j--) {
  2439. if (get_caret_line(caret_edit_order[j]) != get_caret_line(caret_idx)) {
  2440. break;
  2441. }
  2442. if (get_caret_column(caret_edit_order[j]) > column) {
  2443. break;
  2444. }
  2445. carets_to_remove.push_back(caret_edit_order[j]);
  2446. }
  2447. } else {
  2448. // Delete one character
  2449. if (caret_mid_grapheme_enabled) {
  2450. next_column = get_caret_column(caret_idx) < curline_len ? (get_caret_column(caret_idx) + 1) : 0;
  2451. } else {
  2452. next_column = get_caret_column(caret_idx) < curline_len ? TS->shaped_text_next_grapheme_pos(text.get_line_data(get_caret_line(caret_idx))->get_rid(), (get_caret_column(caret_idx))) : 0;
  2453. }
  2454. // Remove overlapping carets.
  2455. if (i > 0) {
  2456. int prev_caret_idx = caret_edit_order[i - 1];
  2457. if (get_caret_line(prev_caret_idx) == next_line && get_caret_column(prev_caret_idx) == next_column) {
  2458. carets_to_remove.push_back(prev_caret_idx);
  2459. }
  2460. }
  2461. }
  2462. _remove_text(get_caret_line(caret_idx), get_caret_column(caret_idx), next_line, next_column);
  2463. adjust_carets_after_edit(caret_idx, get_caret_line(caret_idx), get_caret_column(caret_idx), next_line, next_column);
  2464. }
  2465. // Sort and remove backwards to preserve indexes.
  2466. carets_to_remove.sort();
  2467. for (int i = carets_to_remove.size() - 1; i >= 0; i--) {
  2468. remove_caret(carets_to_remove[i]);
  2469. }
  2470. // If we are deleting from the end of a line, due to column preservation we could still overlap with another caret.
  2471. merge_overlapping_carets();
  2472. end_action();
  2473. queue_redraw();
  2474. }
  2475. void TextEdit::_move_caret_document_start(bool p_select) {
  2476. remove_secondary_carets();
  2477. if (p_select) {
  2478. _pre_shift_selection(0);
  2479. } else {
  2480. deselect();
  2481. }
  2482. set_caret_line(0, false);
  2483. set_caret_column(0);
  2484. if (p_select) {
  2485. _post_shift_selection(0);
  2486. }
  2487. }
  2488. void TextEdit::_move_caret_document_end(bool p_select) {
  2489. remove_secondary_carets();
  2490. if (p_select) {
  2491. _pre_shift_selection(0);
  2492. } else {
  2493. deselect();
  2494. }
  2495. set_caret_line(get_last_unhidden_line(), true, false, 9999);
  2496. set_caret_column(text[get_caret_line()].length());
  2497. if (p_select) {
  2498. _post_shift_selection(0);
  2499. }
  2500. }
  2501. bool TextEdit::_clear_carets_and_selection() {
  2502. if (get_caret_count() > 1) {
  2503. remove_secondary_carets();
  2504. return true;
  2505. }
  2506. if (has_selection()) {
  2507. deselect();
  2508. return true;
  2509. }
  2510. return false;
  2511. }
  2512. void TextEdit::_get_above_below_caret_line_column(int p_old_line, int p_old_wrap_index, int p_old_column, bool p_below, int &p_new_line, int &p_new_column, int p_last_fit_x) const {
  2513. if (p_last_fit_x == -1) {
  2514. p_last_fit_x = _get_column_x_offset_for_line(p_old_column, p_old_line, p_old_column);
  2515. }
  2516. // Calculate the new line and wrap index
  2517. p_new_line = p_old_line;
  2518. int caret_wrap_index = p_old_wrap_index;
  2519. if (p_below) {
  2520. if (caret_wrap_index < get_line_wrap_count(p_new_line)) {
  2521. caret_wrap_index++;
  2522. } else {
  2523. p_new_line++;
  2524. caret_wrap_index = 0;
  2525. }
  2526. } else {
  2527. if (caret_wrap_index == 0) {
  2528. p_new_line--;
  2529. caret_wrap_index = get_line_wrap_count(p_new_line);
  2530. } else {
  2531. caret_wrap_index--;
  2532. }
  2533. }
  2534. // Boundary checks
  2535. if (p_new_line < 0) {
  2536. p_new_line = 0;
  2537. }
  2538. if (p_new_line >= text.size()) {
  2539. p_new_line = text.size() - 1;
  2540. }
  2541. p_new_column = _get_char_pos_for_line(p_last_fit_x, p_new_line, caret_wrap_index);
  2542. if (p_new_column != 0 && get_line_wrapping_mode() != LineWrappingMode::LINE_WRAPPING_NONE && caret_wrap_index < get_line_wrap_count(p_new_line)) {
  2543. Vector<String> rows = get_line_wrapped_text(p_new_line);
  2544. int row_end_col = 0;
  2545. for (int i = 0; i < caret_wrap_index + 1; i++) {
  2546. row_end_col += rows[i].length();
  2547. }
  2548. if (p_new_column >= row_end_col) {
  2549. p_new_column -= 1;
  2550. }
  2551. }
  2552. }
  2553. void TextEdit::_update_placeholder() {
  2554. if (font.is_null() || font_size <= 0) {
  2555. return; // Not in tree?
  2556. }
  2557. // Placeholder is generally smaller then text documents, and updates less so this should be fast enough for now.
  2558. placeholder_data_buf->clear();
  2559. placeholder_data_buf->set_width(text.get_width());
  2560. placeholder_data_buf->set_direction((TextServer::Direction)text_direction);
  2561. placeholder_data_buf->set_preserve_control(draw_control_chars);
  2562. placeholder_data_buf->add_string(placeholder_text, font, font_size, language);
  2563. placeholder_bidi_override = structured_text_parser(st_parser, st_args, placeholder_text);
  2564. if (placeholder_bidi_override.is_empty()) {
  2565. TS->shaped_text_set_bidi_override(placeholder_data_buf->get_rid(), placeholder_bidi_override);
  2566. }
  2567. if (get_tab_size() > 0) {
  2568. Vector<float> tabs;
  2569. tabs.push_back(font->get_char_size(' ', font_size).width * get_tab_size());
  2570. placeholder_data_buf->tab_align(tabs);
  2571. }
  2572. // Update height.
  2573. const int wrap_amount = placeholder_data_buf->get_line_count() - 1;
  2574. placeholder_line_height = font->get_height(font_size);
  2575. for (int i = 0; i <= wrap_amount; i++) {
  2576. placeholder_line_height = MAX(placeholder_line_height, placeholder_data_buf->get_line_size(i).y);
  2577. }
  2578. // Update width.
  2579. placeholder_max_width = placeholder_data_buf->get_size().x;
  2580. // Update wrapped rows.
  2581. placeholder_wraped_rows.clear();
  2582. for (int i = 0; i <= wrap_amount; i++) {
  2583. Vector2i line_range = placeholder_data_buf->get_line_range(i);
  2584. placeholder_wraped_rows.push_back(placeholder_text.substr(line_range.x, line_range.y - line_range.x));
  2585. }
  2586. }
  2587. void TextEdit::_update_caches() {
  2588. /* Internal API for CodeEdit. */
  2589. brace_mismatch_color = get_theme_color(SNAME("brace_mismatch_color"), SNAME("CodeEdit"));
  2590. code_folding_color = get_theme_color(SNAME("code_folding_color"), SNAME("CodeEdit"));
  2591. folded_eol_icon = get_theme_icon(SNAME("folded_eol_icon"), SNAME("CodeEdit"));
  2592. /* Search */
  2593. search_result_color = get_theme_color(SNAME("search_result_color"));
  2594. search_result_border_color = get_theme_color(SNAME("search_result_border_color"));
  2595. /* Caret */
  2596. caret_color = get_theme_color(SNAME("caret_color"));
  2597. caret_background_color = get_theme_color(SNAME("caret_background_color"));
  2598. /* Selection */
  2599. font_selected_color = get_theme_color(SNAME("font_selected_color"));
  2600. selection_color = get_theme_color(SNAME("selection_color"));
  2601. use_selected_font_color = font_selected_color != Color(0, 0, 0, 0);
  2602. /* Visual. */
  2603. style_normal = get_theme_stylebox(SNAME("normal"));
  2604. style_focus = get_theme_stylebox(SNAME("focus"));
  2605. style_readonly = get_theme_stylebox(SNAME("read_only"));
  2606. tab_icon = get_theme_icon(SNAME("tab"));
  2607. space_icon = get_theme_icon(SNAME("space"));
  2608. font = get_theme_font(SNAME("font"));
  2609. font_size = get_theme_font_size(SNAME("font_size"));
  2610. font_color = get_theme_color(SNAME("font_color"));
  2611. font_readonly_color = get_theme_color(SNAME("font_readonly_color"));
  2612. font_placeholder_color = get_theme_color(SNAME("font_placeholder_color"));
  2613. outline_size = get_theme_constant(SNAME("outline_size"));
  2614. outline_color = get_theme_color(SNAME("font_outline_color"));
  2615. line_spacing = get_theme_constant(SNAME("line_spacing"));
  2616. background_color = get_theme_color(SNAME("background_color"));
  2617. current_line_color = get_theme_color(SNAME("current_line_color"));
  2618. word_highlighted_color = get_theme_color(SNAME("word_highlighted_color"));
  2619. /* Text properties. */
  2620. TextServer::Direction dir;
  2621. if (text_direction == Control::TEXT_DIRECTION_INHERITED) {
  2622. dir = is_layout_rtl() ? TextServer::DIRECTION_RTL : TextServer::DIRECTION_LTR;
  2623. } else {
  2624. dir = (TextServer::Direction)text_direction;
  2625. }
  2626. text.set_direction_and_language(dir, (!language.is_empty()) ? language : TranslationServer::get_singleton()->get_tool_locale());
  2627. text.set_draw_control_chars(draw_control_chars);
  2628. text.set_font(font);
  2629. text.set_font_size(font_size);
  2630. text.invalidate_font();
  2631. _update_placeholder();
  2632. /* Syntax highlighting. */
  2633. if (syntax_highlighter.is_valid()) {
  2634. syntax_highlighter->set_text_edit(this);
  2635. }
  2636. }
  2637. /* General overrides. */
  2638. Size2 TextEdit::get_minimum_size() const {
  2639. Size2 size = style_normal->get_minimum_size();
  2640. if (fit_content_height) {
  2641. size.y += content_height_cache;
  2642. }
  2643. return size;
  2644. }
  2645. bool TextEdit::is_text_field() const {
  2646. return true;
  2647. }
  2648. Variant TextEdit::get_drag_data(const Point2 &p_point) {
  2649. Variant ret = Control::get_drag_data(p_point);
  2650. if (ret != Variant()) {
  2651. return ret;
  2652. }
  2653. if (has_selection() && selection_drag_attempt) {
  2654. String t = get_selected_text();
  2655. Label *l = memnew(Label);
  2656. l->set_text(t);
  2657. set_drag_preview(l);
  2658. return t;
  2659. }
  2660. return Variant();
  2661. }
  2662. bool TextEdit::can_drop_data(const Point2 &p_point, const Variant &p_data) const {
  2663. bool drop_override = Control::can_drop_data(p_point, p_data); // In case user wants to drop custom data.
  2664. if (drop_override) {
  2665. return drop_override;
  2666. }
  2667. return is_editable() && p_data.get_type() == Variant::STRING;
  2668. }
  2669. void TextEdit::drop_data(const Point2 &p_point, const Variant &p_data) {
  2670. Control::drop_data(p_point, p_data);
  2671. if (p_data.get_type() == Variant::STRING && is_editable()) {
  2672. Point2i pos = get_line_column_at_pos(get_local_mouse_pos());
  2673. int caret_row_tmp = pos.y;
  2674. int caret_column_tmp = pos.x;
  2675. if (selection_drag_attempt) {
  2676. selection_drag_attempt = false;
  2677. if (!is_mouse_over_selection(!Input::get_singleton()->is_key_pressed(Key::CTRL))) {
  2678. // Set caret back at selection for undo / redo.
  2679. set_caret_line(get_selection_to_line(), false, false);
  2680. set_caret_column(get_selection_to_column());
  2681. begin_complex_operation();
  2682. if (!Input::get_singleton()->is_key_pressed(Key::CTRL)) {
  2683. if (caret_row_tmp > get_selection_to_line()) {
  2684. caret_row_tmp = caret_row_tmp - (get_selection_to_line() - get_selection_from_line());
  2685. } else if (caret_row_tmp == get_selection_to_line() && caret_column_tmp >= get_selection_to_column()) {
  2686. caret_column_tmp = caret_column_tmp - (get_selection_to_column() - get_selection_from_column());
  2687. }
  2688. delete_selection();
  2689. } else {
  2690. deselect();
  2691. }
  2692. remove_secondary_carets();
  2693. set_caret_line(caret_row_tmp, true, false);
  2694. set_caret_column(caret_column_tmp);
  2695. insert_text_at_caret(p_data);
  2696. end_complex_operation();
  2697. }
  2698. } else if (is_mouse_over_selection()) {
  2699. remove_secondary_carets();
  2700. caret_row_tmp = get_selection_from_line();
  2701. caret_column_tmp = get_selection_from_column();
  2702. set_caret_line(caret_row_tmp, true, false);
  2703. set_caret_column(caret_column_tmp);
  2704. insert_text_at_caret(p_data);
  2705. grab_focus();
  2706. } else {
  2707. remove_secondary_carets();
  2708. deselect();
  2709. set_caret_line(caret_row_tmp, true, false);
  2710. set_caret_column(caret_column_tmp);
  2711. insert_text_at_caret(p_data);
  2712. grab_focus();
  2713. }
  2714. if (caret_row_tmp != get_caret_line() || caret_column_tmp != get_caret_column()) {
  2715. select(caret_row_tmp, caret_column_tmp, get_caret_line(), get_caret_column());
  2716. }
  2717. }
  2718. }
  2719. Control::CursorShape TextEdit::get_cursor_shape(const Point2 &p_pos) const {
  2720. Point2i pos = get_line_column_at_pos(p_pos);
  2721. int row = pos.y;
  2722. int left_margin = style_normal->get_margin(SIDE_LEFT);
  2723. int gutter = left_margin + gutters_width;
  2724. if (p_pos.x < gutter) {
  2725. for (int i = 0; i < gutters.size(); i++) {
  2726. if (!gutters[i].draw) {
  2727. continue;
  2728. }
  2729. if (p_pos.x >= left_margin && p_pos.x < left_margin + gutters[i].width) {
  2730. if (gutters[i].clickable || is_line_gutter_clickable(row, i)) {
  2731. return CURSOR_POINTING_HAND;
  2732. }
  2733. }
  2734. left_margin += gutters[i].width;
  2735. }
  2736. return CURSOR_ARROW;
  2737. }
  2738. int xmargin_end = get_size().width - style_normal->get_margin(SIDE_RIGHT);
  2739. if (draw_minimap && p_pos.x > xmargin_end - minimap_width && p_pos.x <= xmargin_end) {
  2740. return CURSOR_ARROW;
  2741. }
  2742. return get_default_cursor_shape();
  2743. }
  2744. String TextEdit::get_tooltip(const Point2 &p_pos) const {
  2745. if (!tooltip_callback.is_valid()) {
  2746. return Control::get_tooltip(p_pos);
  2747. }
  2748. Point2i pos = get_line_column_at_pos(p_pos);
  2749. int row = pos.y;
  2750. int col = pos.x;
  2751. String s = text[row];
  2752. if (s.length() == 0) {
  2753. return Control::get_tooltip(p_pos);
  2754. }
  2755. int beg, end;
  2756. if (select_word(s, col, beg, end)) {
  2757. Variant args[1] = { s.substr(beg, end - beg) };
  2758. const Variant *argp[] = { &args[0] };
  2759. Callable::CallError ce;
  2760. Variant ret;
  2761. tooltip_callback.callp(argp, 1, ret, ce);
  2762. ERR_FAIL_COND_V_MSG(ce.error != Callable::CallError::CALL_OK, "", "Failed to call custom tooltip.");
  2763. return ret;
  2764. }
  2765. return Control::get_tooltip(p_pos);
  2766. }
  2767. void TextEdit::set_tooltip_request_func(const Callable &p_tooltip_callback) {
  2768. tooltip_callback = p_tooltip_callback;
  2769. }
  2770. /* Text */
  2771. // Text properties.
  2772. bool TextEdit::has_ime_text() const {
  2773. return !ime_text.is_empty();
  2774. }
  2775. void TextEdit::set_editable(const bool p_editable) {
  2776. if (editable == p_editable) {
  2777. return;
  2778. }
  2779. editable = p_editable;
  2780. queue_redraw();
  2781. }
  2782. bool TextEdit::is_editable() const {
  2783. return editable;
  2784. }
  2785. void TextEdit::set_text_direction(Control::TextDirection p_text_direction) {
  2786. ERR_FAIL_COND((int)p_text_direction < -1 || (int)p_text_direction > 3);
  2787. if (text_direction != p_text_direction) {
  2788. text_direction = p_text_direction;
  2789. if (text_direction != TEXT_DIRECTION_AUTO && text_direction != TEXT_DIRECTION_INHERITED) {
  2790. input_direction = text_direction;
  2791. }
  2792. TextServer::Direction dir;
  2793. if (text_direction == Control::TEXT_DIRECTION_INHERITED) {
  2794. dir = is_layout_rtl() ? TextServer::DIRECTION_RTL : TextServer::DIRECTION_LTR;
  2795. } else {
  2796. dir = (TextServer::Direction)text_direction;
  2797. }
  2798. text.set_direction_and_language(dir, (!language.is_empty()) ? language : TranslationServer::get_singleton()->get_tool_locale());
  2799. text.invalidate_font();
  2800. _update_placeholder();
  2801. if (menu_dir) {
  2802. menu_dir->set_item_checked(menu_dir->get_item_index(MENU_DIR_INHERITED), text_direction == TEXT_DIRECTION_INHERITED);
  2803. menu_dir->set_item_checked(menu_dir->get_item_index(MENU_DIR_AUTO), text_direction == TEXT_DIRECTION_AUTO);
  2804. menu_dir->set_item_checked(menu_dir->get_item_index(MENU_DIR_LTR), text_direction == TEXT_DIRECTION_LTR);
  2805. menu_dir->set_item_checked(menu_dir->get_item_index(MENU_DIR_RTL), text_direction == TEXT_DIRECTION_RTL);
  2806. }
  2807. queue_redraw();
  2808. }
  2809. }
  2810. Control::TextDirection TextEdit::get_text_direction() const {
  2811. return text_direction;
  2812. }
  2813. void TextEdit::set_language(const String &p_language) {
  2814. if (language != p_language) {
  2815. language = p_language;
  2816. TextServer::Direction dir;
  2817. if (text_direction == Control::TEXT_DIRECTION_INHERITED) {
  2818. dir = is_layout_rtl() ? TextServer::DIRECTION_RTL : TextServer::DIRECTION_LTR;
  2819. } else {
  2820. dir = (TextServer::Direction)text_direction;
  2821. }
  2822. text.set_direction_and_language(dir, (!language.is_empty()) ? language : TranslationServer::get_singleton()->get_tool_locale());
  2823. text.invalidate_all();
  2824. _update_placeholder();
  2825. queue_redraw();
  2826. }
  2827. }
  2828. String TextEdit::get_language() const {
  2829. return language;
  2830. }
  2831. void TextEdit::set_structured_text_bidi_override(TextServer::StructuredTextParser p_parser) {
  2832. if (st_parser != p_parser) {
  2833. st_parser = p_parser;
  2834. for (int i = 0; i < text.size(); i++) {
  2835. text.set(i, text[i], structured_text_parser(st_parser, st_args, text[i]));
  2836. }
  2837. queue_redraw();
  2838. }
  2839. }
  2840. TextServer::StructuredTextParser TextEdit::get_structured_text_bidi_override() const {
  2841. return st_parser;
  2842. }
  2843. void TextEdit::set_structured_text_bidi_override_options(Array p_args) {
  2844. if (st_args == p_args) {
  2845. return;
  2846. }
  2847. st_args = p_args;
  2848. for (int i = 0; i < text.size(); i++) {
  2849. text.set(i, text[i], structured_text_parser(st_parser, st_args, text[i]));
  2850. }
  2851. queue_redraw();
  2852. }
  2853. Array TextEdit::get_structured_text_bidi_override_options() const {
  2854. return st_args;
  2855. }
  2856. void TextEdit::set_tab_size(const int p_size) {
  2857. ERR_FAIL_COND_MSG(p_size <= 0, "Tab size must be greater than 0.");
  2858. if (p_size == text.get_tab_size()) {
  2859. return;
  2860. }
  2861. text.set_tab_size(p_size);
  2862. text.invalidate_all_lines();
  2863. _update_placeholder();
  2864. queue_redraw();
  2865. }
  2866. int TextEdit::get_tab_size() const {
  2867. return text.get_tab_size();
  2868. }
  2869. // User controls
  2870. void TextEdit::set_overtype_mode_enabled(const bool p_enabled) {
  2871. if (overtype_mode == p_enabled) {
  2872. return;
  2873. }
  2874. overtype_mode = p_enabled;
  2875. queue_redraw();
  2876. }
  2877. bool TextEdit::is_overtype_mode_enabled() const {
  2878. return overtype_mode;
  2879. }
  2880. void TextEdit::set_context_menu_enabled(bool p_enabled) {
  2881. context_menu_enabled = p_enabled;
  2882. }
  2883. bool TextEdit::is_context_menu_enabled() const {
  2884. return context_menu_enabled;
  2885. }
  2886. void TextEdit::set_shortcut_keys_enabled(bool p_enabled) {
  2887. shortcut_keys_enabled = p_enabled;
  2888. }
  2889. bool TextEdit::is_shortcut_keys_enabled() const {
  2890. return shortcut_keys_enabled;
  2891. }
  2892. void TextEdit::set_virtual_keyboard_enabled(bool p_enabled) {
  2893. virtual_keyboard_enabled = p_enabled;
  2894. }
  2895. bool TextEdit::is_virtual_keyboard_enabled() const {
  2896. return virtual_keyboard_enabled;
  2897. }
  2898. void TextEdit::set_middle_mouse_paste_enabled(bool p_enabled) {
  2899. middle_mouse_paste_enabled = p_enabled;
  2900. }
  2901. bool TextEdit::is_middle_mouse_paste_enabled() const {
  2902. return middle_mouse_paste_enabled;
  2903. }
  2904. // Text manipulation
  2905. void TextEdit::clear() {
  2906. setting_text = true;
  2907. _clear();
  2908. setting_text = false;
  2909. emit_signal(SNAME("text_set"));
  2910. }
  2911. void TextEdit::_clear() {
  2912. if (editable && undo_enabled) {
  2913. remove_secondary_carets();
  2914. _move_caret_document_start(false);
  2915. begin_complex_operation();
  2916. _remove_text(0, 0, MAX(0, get_line_count() - 1), MAX(get_line(MAX(get_line_count() - 1, 0)).size() - 1, 0));
  2917. insert_text_at_caret("");
  2918. text.clear();
  2919. end_complex_operation();
  2920. return;
  2921. }
  2922. // Cannot merge with above, as we are not part of the tree on creation.
  2923. int old_text_size = text.size();
  2924. clear_undo_history();
  2925. text.clear();
  2926. remove_secondary_carets();
  2927. set_caret_line(0, false);
  2928. set_caret_column(0);
  2929. first_visible_col = 0;
  2930. first_visible_line = 0;
  2931. first_visible_line_wrap_ofs = 0;
  2932. carets.write[0].last_fit_x = 0;
  2933. deselect();
  2934. emit_signal(SNAME("lines_edited_from"), old_text_size, 0);
  2935. }
  2936. void TextEdit::set_text(const String &p_text) {
  2937. setting_text = true;
  2938. if (!undo_enabled) {
  2939. _clear();
  2940. insert_text_at_caret(p_text);
  2941. }
  2942. if (undo_enabled) {
  2943. remove_secondary_carets();
  2944. set_caret_line(0);
  2945. set_caret_column(0);
  2946. begin_complex_operation();
  2947. deselect();
  2948. _remove_text(0, 0, MAX(0, get_line_count() - 1), MAX(get_line(MAX(get_line_count() - 1, 0)).size() - 1, 0));
  2949. insert_text_at_caret(p_text);
  2950. end_complex_operation();
  2951. }
  2952. set_caret_line(0);
  2953. set_caret_column(0);
  2954. queue_redraw();
  2955. setting_text = false;
  2956. emit_signal(SNAME("text_set"));
  2957. }
  2958. String TextEdit::get_text() const {
  2959. StringBuilder ret_text;
  2960. const int text_size = text.size();
  2961. for (int i = 0; i < text_size; i++) {
  2962. ret_text += text[i];
  2963. if (i != text_size - 1) {
  2964. ret_text += "\n";
  2965. }
  2966. }
  2967. return ret_text.as_string();
  2968. }
  2969. int TextEdit::get_line_count() const {
  2970. return text.size();
  2971. }
  2972. void TextEdit::set_placeholder(const String &p_text) {
  2973. if (placeholder_text == p_text) {
  2974. return;
  2975. }
  2976. placeholder_text = p_text;
  2977. _update_placeholder();
  2978. queue_redraw();
  2979. }
  2980. String TextEdit::get_placeholder() const {
  2981. return placeholder_text;
  2982. }
  2983. void TextEdit::set_line(int p_line, const String &p_new_text) {
  2984. if (p_line < 0 || p_line >= text.size()) {
  2985. return;
  2986. }
  2987. begin_complex_operation();
  2988. _remove_text(p_line, 0, p_line, text[p_line].length());
  2989. _insert_text(p_line, 0, p_new_text);
  2990. for (int i = 0; i < carets.size(); i++) {
  2991. if (get_caret_line(i) == p_line && get_caret_column(i) > p_new_text.length()) {
  2992. set_caret_column(p_new_text.length(), false, i);
  2993. }
  2994. if (has_selection(i) && p_line == get_selection_to_line(i) && get_selection_to_column(i) > text[p_line].length()) {
  2995. carets.write[i].selection.to_column = text[p_line].length();
  2996. }
  2997. }
  2998. end_complex_operation();
  2999. }
  3000. String TextEdit::get_line(int p_line) const {
  3001. if (p_line < 0 || p_line >= text.size()) {
  3002. return "";
  3003. }
  3004. return text[p_line];
  3005. }
  3006. int TextEdit::get_line_width(int p_line, int p_wrap_index) const {
  3007. ERR_FAIL_INDEX_V(p_line, text.size(), 0);
  3008. ERR_FAIL_COND_V(p_wrap_index > get_line_wrap_count(p_line), 0);
  3009. return text.get_line_width(p_line, p_wrap_index);
  3010. }
  3011. int TextEdit::get_line_height() const {
  3012. return text.get_line_height() + line_spacing;
  3013. }
  3014. int TextEdit::get_indent_level(int p_line) const {
  3015. ERR_FAIL_INDEX_V(p_line, text.size(), 0);
  3016. int tab_count = 0;
  3017. int whitespace_count = 0;
  3018. int line_length = text[p_line].size();
  3019. for (int i = 0; i < line_length - 1; i++) {
  3020. if (text[p_line][i] == '\t') {
  3021. tab_count++;
  3022. } else if (text[p_line][i] == ' ') {
  3023. whitespace_count++;
  3024. } else {
  3025. break;
  3026. }
  3027. }
  3028. return tab_count * text.get_tab_size() + whitespace_count;
  3029. }
  3030. int TextEdit::get_first_non_whitespace_column(int p_line) const {
  3031. ERR_FAIL_INDEX_V(p_line, text.size(), 0);
  3032. int col = 0;
  3033. while (col < text[p_line].length() && is_whitespace(text[p_line][col])) {
  3034. col++;
  3035. }
  3036. return col;
  3037. }
  3038. void TextEdit::swap_lines(int p_from_line, int p_to_line) {
  3039. ERR_FAIL_INDEX(p_from_line, text.size());
  3040. ERR_FAIL_INDEX(p_to_line, text.size());
  3041. String tmp = get_line(p_from_line);
  3042. String tmp2 = get_line(p_to_line);
  3043. begin_complex_operation();
  3044. set_line(p_to_line, tmp);
  3045. set_line(p_from_line, tmp2);
  3046. end_complex_operation();
  3047. }
  3048. void TextEdit::insert_line_at(int p_at, const String &p_text) {
  3049. ERR_FAIL_INDEX(p_at, text.size());
  3050. _insert_text(p_at, 0, p_text + "\n");
  3051. for (int i = 0; i < carets.size(); i++) {
  3052. if (get_caret_line(i) >= p_at) {
  3053. // offset caret when located after inserted line
  3054. set_caret_line(get_caret_line(i) + 1, false, true, 0, i);
  3055. }
  3056. if (has_selection(i)) {
  3057. if (get_selection_from_line(i) >= p_at) {
  3058. // offset selection when located after inserted line
  3059. select(get_selection_from_line(i) + 1, get_selection_from_column(i), get_selection_to_line(i) + 1, get_selection_to_column(i), i);
  3060. } else if (get_selection_to_line(i) >= p_at) {
  3061. // extend selection that includes inserted line
  3062. select(get_selection_from_line(i), get_selection_from_column(i), get_selection_to_line(i) + 1, get_selection_to_column(i), i);
  3063. }
  3064. }
  3065. }
  3066. // Need to apply the above adjustments to the undo / redo carets.
  3067. current_op.end_carets = carets;
  3068. queue_redraw();
  3069. }
  3070. void TextEdit::insert_text_at_caret(const String &p_text, int p_caret) {
  3071. ERR_FAIL_COND(p_caret > carets.size());
  3072. begin_complex_operation();
  3073. Vector<int> caret_edit_order = get_caret_index_edit_order();
  3074. for (const int &i : caret_edit_order) {
  3075. if (p_caret != -1 && p_caret != i) {
  3076. continue;
  3077. }
  3078. delete_selection(i);
  3079. int from_line = get_caret_line(i);
  3080. int from_col = get_caret_column(i);
  3081. int new_column, new_line;
  3082. _insert_text(from_line, from_col, p_text, &new_line, &new_column);
  3083. _update_scrollbars();
  3084. set_caret_line(new_line, false, true, 0, i);
  3085. set_caret_column(new_column, i == 0, i);
  3086. adjust_carets_after_edit(i, new_line, new_column, from_line, from_col);
  3087. }
  3088. if (!ime_text.is_empty()) {
  3089. for (int i = 0; i < carets.size(); i++) {
  3090. String t;
  3091. if (get_caret_column(i) >= 0) {
  3092. t = text[get_caret_line(i)].substr(0, get_caret_column(i)) + ime_text + text[get_caret_line(i)].substr(get_caret_column(i), text[get_caret_line(i)].length());
  3093. } else {
  3094. t = ime_text;
  3095. }
  3096. text.invalidate_cache(get_caret_line(i), get_caret_column(i), true, t, structured_text_parser(st_parser, st_args, t));
  3097. }
  3098. }
  3099. end_complex_operation();
  3100. queue_redraw();
  3101. }
  3102. void TextEdit::remove_text(int p_from_line, int p_from_column, int p_to_line, int p_to_column) {
  3103. ERR_FAIL_INDEX(p_from_line, text.size());
  3104. ERR_FAIL_INDEX(p_from_column, text[p_from_line].length() + 1);
  3105. ERR_FAIL_INDEX(p_to_line, text.size());
  3106. ERR_FAIL_INDEX(p_to_column, text[p_to_line].length() + 1);
  3107. ERR_FAIL_COND(p_to_line < p_from_line);
  3108. ERR_FAIL_COND(p_to_line == p_from_line && p_to_column < p_from_column);
  3109. _remove_text(p_from_line, p_from_column, p_to_line, p_to_column);
  3110. }
  3111. int TextEdit::get_last_unhidden_line() const {
  3112. // Returns the last line in the text that is not hidden.
  3113. if (!_is_hiding_enabled()) {
  3114. return text.size() - 1;
  3115. }
  3116. int last_line;
  3117. for (last_line = text.size() - 1; last_line > 0; last_line--) {
  3118. if (!_is_line_hidden(last_line)) {
  3119. break;
  3120. }
  3121. }
  3122. return last_line;
  3123. }
  3124. int TextEdit::get_next_visible_line_offset_from(int p_line_from, int p_visible_amount) const {
  3125. // Returns the number of lines (hidden and unhidden) from p_line_from to (p_line_from + visible_amount of unhidden lines).
  3126. ERR_FAIL_INDEX_V(p_line_from, text.size(), ABS(p_visible_amount));
  3127. if (!_is_hiding_enabled()) {
  3128. return ABS(p_visible_amount);
  3129. }
  3130. int num_visible = 0;
  3131. int num_total = 0;
  3132. if (p_visible_amount >= 0) {
  3133. for (int i = p_line_from; i < text.size(); i++) {
  3134. num_total++;
  3135. if (!_is_line_hidden(i)) {
  3136. num_visible++;
  3137. }
  3138. if (num_visible >= p_visible_amount) {
  3139. break;
  3140. }
  3141. }
  3142. } else {
  3143. p_visible_amount = ABS(p_visible_amount);
  3144. for (int i = p_line_from; i >= 0; i--) {
  3145. num_total++;
  3146. if (!_is_line_hidden(i)) {
  3147. num_visible++;
  3148. }
  3149. if (num_visible >= p_visible_amount) {
  3150. break;
  3151. }
  3152. }
  3153. }
  3154. return num_total;
  3155. }
  3156. Point2i TextEdit::get_next_visible_line_index_offset_from(int p_line_from, int p_wrap_index_from, int p_visible_amount) const {
  3157. // 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).
  3158. // Wrap index is set to the wrap index of the last line.
  3159. int wrap_index = 0;
  3160. ERR_FAIL_INDEX_V(p_line_from, text.size(), Point2i(ABS(p_visible_amount), 0));
  3161. if (!_is_hiding_enabled() && get_line_wrapping_mode() == LineWrappingMode::LINE_WRAPPING_NONE) {
  3162. return Point2i(ABS(p_visible_amount), 0);
  3163. }
  3164. int num_visible = 0;
  3165. int num_total = 0;
  3166. if (p_visible_amount == 0) {
  3167. num_total = 0;
  3168. wrap_index = 0;
  3169. } else if (p_visible_amount > 0) {
  3170. int i;
  3171. num_visible -= p_wrap_index_from;
  3172. for (i = p_line_from; i < text.size(); i++) {
  3173. num_total++;
  3174. if (!_is_line_hidden(i)) {
  3175. num_visible++;
  3176. num_visible += get_line_wrap_count(i);
  3177. }
  3178. if (num_visible >= p_visible_amount) {
  3179. break;
  3180. }
  3181. }
  3182. wrap_index = get_line_wrap_count(MIN(i, text.size() - 1)) - MAX(0, num_visible - p_visible_amount);
  3183. // If we are a hidden line, then we are the last line as we cannot reach "p_visible_amount".
  3184. // This means we need to backtrack to get last visible line.
  3185. // Currently, line 0 cannot be hidden so this should always be valid.
  3186. int line = (p_line_from + num_total) - 1;
  3187. if (_is_line_hidden(line)) {
  3188. Point2i backtrack = get_next_visible_line_index_offset_from(line, 0, -1);
  3189. num_total = num_total - (backtrack.x - 1);
  3190. wrap_index = backtrack.y;
  3191. }
  3192. } else {
  3193. p_visible_amount = ABS(p_visible_amount);
  3194. int i;
  3195. num_visible -= get_line_wrap_count(p_line_from) - p_wrap_index_from;
  3196. for (i = p_line_from; i >= 0; i--) {
  3197. num_total++;
  3198. if (!_is_line_hidden(i)) {
  3199. num_visible++;
  3200. num_visible += get_line_wrap_count(i);
  3201. }
  3202. if (num_visible >= p_visible_amount) {
  3203. break;
  3204. }
  3205. }
  3206. wrap_index = MAX(0, num_visible - p_visible_amount);
  3207. }
  3208. wrap_index = MAX(wrap_index, 0);
  3209. return Point2i(num_total, wrap_index);
  3210. }
  3211. // Overridable actions
  3212. void TextEdit::handle_unicode_input(const uint32_t p_unicode, int p_caret) {
  3213. if (GDVIRTUAL_CALL(_handle_unicode_input, p_unicode, p_caret)) {
  3214. return;
  3215. }
  3216. _handle_unicode_input_internal(p_unicode, p_caret);
  3217. }
  3218. void TextEdit::backspace(int p_caret) {
  3219. if (GDVIRTUAL_CALL(_backspace, p_caret)) {
  3220. return;
  3221. }
  3222. _backspace_internal(p_caret);
  3223. }
  3224. void TextEdit::cut(int p_caret) {
  3225. if (GDVIRTUAL_CALL(_cut, p_caret)) {
  3226. return;
  3227. }
  3228. _cut_internal(p_caret);
  3229. }
  3230. void TextEdit::copy(int p_caret) {
  3231. if (GDVIRTUAL_CALL(_copy, p_caret)) {
  3232. return;
  3233. }
  3234. _copy_internal(p_caret);
  3235. }
  3236. void TextEdit::paste(int p_caret) {
  3237. if (GDVIRTUAL_CALL(_paste, p_caret)) {
  3238. return;
  3239. }
  3240. _paste_internal(p_caret);
  3241. }
  3242. void TextEdit::paste_primary_clipboard(int p_caret) {
  3243. if (GDVIRTUAL_CALL(_paste_primary_clipboard, p_caret)) {
  3244. return;
  3245. }
  3246. _paste_primary_clipboard_internal(p_caret);
  3247. }
  3248. // Context menu.
  3249. PopupMenu *TextEdit::get_menu() const {
  3250. if (!menu) {
  3251. const_cast<TextEdit *>(this)->_generate_context_menu();
  3252. }
  3253. return menu;
  3254. }
  3255. bool TextEdit::is_menu_visible() const {
  3256. return menu && menu->is_visible();
  3257. }
  3258. void TextEdit::menu_option(int p_option) {
  3259. switch (p_option) {
  3260. case MENU_CUT: {
  3261. cut();
  3262. } break;
  3263. case MENU_COPY: {
  3264. copy();
  3265. } break;
  3266. case MENU_PASTE: {
  3267. paste();
  3268. } break;
  3269. case MENU_CLEAR: {
  3270. if (editable) {
  3271. clear();
  3272. }
  3273. } break;
  3274. case MENU_SELECT_ALL: {
  3275. select_all();
  3276. } break;
  3277. case MENU_UNDO: {
  3278. undo();
  3279. } break;
  3280. case MENU_REDO: {
  3281. redo();
  3282. } break;
  3283. case MENU_DIR_INHERITED: {
  3284. set_text_direction(TEXT_DIRECTION_INHERITED);
  3285. } break;
  3286. case MENU_DIR_AUTO: {
  3287. set_text_direction(TEXT_DIRECTION_AUTO);
  3288. } break;
  3289. case MENU_DIR_LTR: {
  3290. set_text_direction(TEXT_DIRECTION_LTR);
  3291. } break;
  3292. case MENU_DIR_RTL: {
  3293. set_text_direction(TEXT_DIRECTION_RTL);
  3294. } break;
  3295. case MENU_DISPLAY_UCC: {
  3296. set_draw_control_chars(!get_draw_control_chars());
  3297. } break;
  3298. case MENU_INSERT_LRM: {
  3299. if (editable) {
  3300. insert_text_at_caret(String::chr(0x200E));
  3301. }
  3302. } break;
  3303. case MENU_INSERT_RLM: {
  3304. if (editable) {
  3305. insert_text_at_caret(String::chr(0x200F));
  3306. }
  3307. } break;
  3308. case MENU_INSERT_LRE: {
  3309. if (editable) {
  3310. insert_text_at_caret(String::chr(0x202A));
  3311. }
  3312. } break;
  3313. case MENU_INSERT_RLE: {
  3314. if (editable) {
  3315. insert_text_at_caret(String::chr(0x202B));
  3316. }
  3317. } break;
  3318. case MENU_INSERT_LRO: {
  3319. if (editable) {
  3320. insert_text_at_caret(String::chr(0x202D));
  3321. }
  3322. } break;
  3323. case MENU_INSERT_RLO: {
  3324. if (editable) {
  3325. insert_text_at_caret(String::chr(0x202E));
  3326. }
  3327. } break;
  3328. case MENU_INSERT_PDF: {
  3329. if (editable) {
  3330. insert_text_at_caret(String::chr(0x202C));
  3331. }
  3332. } break;
  3333. case MENU_INSERT_ALM: {
  3334. if (editable) {
  3335. insert_text_at_caret(String::chr(0x061C));
  3336. }
  3337. } break;
  3338. case MENU_INSERT_LRI: {
  3339. if (editable) {
  3340. insert_text_at_caret(String::chr(0x2066));
  3341. }
  3342. } break;
  3343. case MENU_INSERT_RLI: {
  3344. if (editable) {
  3345. insert_text_at_caret(String::chr(0x2067));
  3346. }
  3347. } break;
  3348. case MENU_INSERT_FSI: {
  3349. if (editable) {
  3350. insert_text_at_caret(String::chr(0x2068));
  3351. }
  3352. } break;
  3353. case MENU_INSERT_PDI: {
  3354. if (editable) {
  3355. insert_text_at_caret(String::chr(0x2069));
  3356. }
  3357. } break;
  3358. case MENU_INSERT_ZWJ: {
  3359. if (editable) {
  3360. insert_text_at_caret(String::chr(0x200D));
  3361. }
  3362. } break;
  3363. case MENU_INSERT_ZWNJ: {
  3364. if (editable) {
  3365. insert_text_at_caret(String::chr(0x200C));
  3366. }
  3367. } break;
  3368. case MENU_INSERT_WJ: {
  3369. if (editable) {
  3370. insert_text_at_caret(String::chr(0x2060));
  3371. }
  3372. } break;
  3373. case MENU_INSERT_SHY: {
  3374. if (editable) {
  3375. insert_text_at_caret(String::chr(0x00AD));
  3376. }
  3377. }
  3378. }
  3379. }
  3380. /* Versioning */
  3381. void TextEdit::start_action(EditAction p_action) {
  3382. if (current_action != p_action) {
  3383. if (current_action != EditAction::ACTION_NONE) {
  3384. in_action = false;
  3385. pending_action_end = false;
  3386. end_complex_operation();
  3387. }
  3388. if (p_action != EditAction::ACTION_NONE) {
  3389. in_action = true;
  3390. begin_complex_operation();
  3391. }
  3392. } else if (current_action != EditAction::ACTION_NONE) {
  3393. pending_action_end = false;
  3394. }
  3395. current_action = p_action;
  3396. }
  3397. void TextEdit::end_action() {
  3398. if (current_action != EditAction::ACTION_NONE) {
  3399. pending_action_end = true;
  3400. }
  3401. }
  3402. TextEdit::EditAction TextEdit::get_current_action() const {
  3403. return current_action;
  3404. }
  3405. void TextEdit::begin_complex_operation() {
  3406. _push_current_op();
  3407. if (complex_operation_count == 0) {
  3408. next_operation_is_complex = true;
  3409. current_op.start_carets = carets;
  3410. }
  3411. complex_operation_count++;
  3412. }
  3413. void TextEdit::end_complex_operation() {
  3414. _push_current_op();
  3415. complex_operation_count = MAX(complex_operation_count - 1, 0);
  3416. if (complex_operation_count > 0) {
  3417. return;
  3418. }
  3419. ERR_FAIL_COND(undo_stack.size() == 0);
  3420. undo_stack.back()->get().end_carets = carets;
  3421. if (undo_stack.back()->get().chain_forward) {
  3422. undo_stack.back()->get().chain_forward = false;
  3423. return;
  3424. }
  3425. undo_stack.back()->get().chain_backward = true;
  3426. }
  3427. bool TextEdit::has_undo() const {
  3428. if (undo_stack_pos == nullptr) {
  3429. int pending = current_op.type == TextOperation::TYPE_NONE ? 0 : 1;
  3430. return undo_stack.size() + pending > 0;
  3431. }
  3432. return undo_stack_pos != undo_stack.front();
  3433. }
  3434. bool TextEdit::has_redo() const {
  3435. return undo_stack_pos != nullptr;
  3436. }
  3437. void TextEdit::undo() {
  3438. if (!editable) {
  3439. return;
  3440. }
  3441. if (in_action) {
  3442. pending_action_end = true;
  3443. }
  3444. _push_current_op();
  3445. if (undo_stack_pos == nullptr) {
  3446. if (!undo_stack.size()) {
  3447. return; // Nothing to undo.
  3448. }
  3449. undo_stack_pos = undo_stack.back();
  3450. } else if (undo_stack_pos == undo_stack.front()) {
  3451. return; // At the bottom of the undo stack.
  3452. } else {
  3453. undo_stack_pos = undo_stack_pos->prev();
  3454. }
  3455. deselect();
  3456. TextOperation op = undo_stack_pos->get();
  3457. _do_text_op(op, true);
  3458. current_op.version = op.prev_version;
  3459. if (undo_stack_pos->get().chain_backward) {
  3460. while (true) {
  3461. ERR_BREAK(!undo_stack_pos->prev());
  3462. undo_stack_pos = undo_stack_pos->prev();
  3463. op = undo_stack_pos->get();
  3464. _do_text_op(op, true);
  3465. current_op.version = op.prev_version;
  3466. if (undo_stack_pos->get().chain_forward) {
  3467. break;
  3468. }
  3469. }
  3470. }
  3471. _update_scrollbars();
  3472. bool dirty_carets = carets.size() != undo_stack_pos->get().start_carets.size();
  3473. if (!dirty_carets) {
  3474. for (int i = 0; i < carets.size(); i++) {
  3475. if (carets[i].line != undo_stack_pos->get().start_carets[i].line || carets[i].column != undo_stack_pos->get().start_carets[i].column) {
  3476. dirty_carets = true;
  3477. break;
  3478. }
  3479. }
  3480. }
  3481. carets = undo_stack_pos->get().start_carets;
  3482. if (dirty_carets && !caret_pos_dirty) {
  3483. if (is_inside_tree()) {
  3484. MessageQueue::get_singleton()->push_call(this, "_emit_caret_changed");
  3485. }
  3486. caret_pos_dirty = true;
  3487. }
  3488. adjust_viewport_to_caret();
  3489. }
  3490. void TextEdit::redo() {
  3491. if (!editable) {
  3492. return;
  3493. }
  3494. if (in_action) {
  3495. pending_action_end = true;
  3496. }
  3497. _push_current_op();
  3498. if (undo_stack_pos == nullptr) {
  3499. return; // Nothing to do.
  3500. }
  3501. deselect();
  3502. TextOperation op = undo_stack_pos->get();
  3503. _do_text_op(op, false);
  3504. current_op.version = op.version;
  3505. if (undo_stack_pos->get().chain_forward) {
  3506. while (true) {
  3507. ERR_BREAK(!undo_stack_pos->next());
  3508. undo_stack_pos = undo_stack_pos->next();
  3509. op = undo_stack_pos->get();
  3510. _do_text_op(op, false);
  3511. current_op.version = op.version;
  3512. if (undo_stack_pos->get().chain_backward) {
  3513. break;
  3514. }
  3515. }
  3516. }
  3517. _update_scrollbars();
  3518. bool dirty_carets = carets.size() != undo_stack_pos->get().end_carets.size();
  3519. if (!dirty_carets) {
  3520. for (int i = 0; i < carets.size(); i++) {
  3521. if (carets[i].line != undo_stack_pos->get().end_carets[i].line || carets[i].column != undo_stack_pos->get().end_carets[i].column) {
  3522. dirty_carets = true;
  3523. break;
  3524. }
  3525. }
  3526. }
  3527. carets = undo_stack_pos->get().end_carets;
  3528. undo_stack_pos = undo_stack_pos->next();
  3529. if (dirty_carets && !caret_pos_dirty) {
  3530. if (is_inside_tree()) {
  3531. MessageQueue::get_singleton()->push_call(this, "_emit_caret_changed");
  3532. }
  3533. caret_pos_dirty = true;
  3534. }
  3535. adjust_viewport_to_caret();
  3536. }
  3537. void TextEdit::clear_undo_history() {
  3538. saved_version = 0;
  3539. current_op.type = TextOperation::TYPE_NONE;
  3540. undo_stack_pos = nullptr;
  3541. undo_stack.clear();
  3542. }
  3543. bool TextEdit::is_insert_text_operation() const {
  3544. return (current_op.type == TextOperation::TYPE_INSERT || current_action == EditAction::ACTION_TYPING);
  3545. }
  3546. void TextEdit::tag_saved_version() {
  3547. saved_version = get_version();
  3548. }
  3549. uint32_t TextEdit::get_version() const {
  3550. return current_op.version;
  3551. }
  3552. uint32_t TextEdit::get_saved_version() const {
  3553. return saved_version;
  3554. }
  3555. /* Search */
  3556. void TextEdit::set_search_text(const String &p_search_text) {
  3557. search_text = p_search_text;
  3558. }
  3559. void TextEdit::set_search_flags(uint32_t p_flags) {
  3560. search_flags = p_flags;
  3561. }
  3562. Point2i TextEdit::search(const String &p_key, uint32_t p_search_flags, int p_from_line, int p_from_column) const {
  3563. if (p_key.length() == 0) {
  3564. return Point2(-1, -1);
  3565. }
  3566. ERR_FAIL_INDEX_V(p_from_line, text.size(), Point2i(-1, -1));
  3567. ERR_FAIL_INDEX_V(p_from_column, text[p_from_line].length() + 1, Point2i(-1, -1));
  3568. // Search through the whole document, but start by current line.
  3569. int line = p_from_line;
  3570. int pos = -1;
  3571. for (int i = 0; i < text.size() + 1; i++) {
  3572. if (line < 0) {
  3573. line = text.size() - 1;
  3574. }
  3575. if (line == text.size()) {
  3576. line = 0;
  3577. }
  3578. String text_line = text[line];
  3579. int from_column = 0;
  3580. if (line == p_from_line) {
  3581. if (i == text.size()) {
  3582. // Wrapped.
  3583. if (p_search_flags & SEARCH_BACKWARDS) {
  3584. from_column = text_line.length();
  3585. } else {
  3586. from_column = 0;
  3587. }
  3588. } else {
  3589. from_column = p_from_column;
  3590. }
  3591. } else {
  3592. if (p_search_flags & SEARCH_BACKWARDS) {
  3593. from_column = text_line.length() - 1;
  3594. } else {
  3595. from_column = 0;
  3596. }
  3597. }
  3598. pos = -1;
  3599. int pos_from = (p_search_flags & SEARCH_BACKWARDS) ? text_line.length() : 0;
  3600. int last_pos = -1;
  3601. while (true) {
  3602. if (p_search_flags & SEARCH_BACKWARDS) {
  3603. while ((last_pos = (p_search_flags & SEARCH_MATCH_CASE) ? text_line.rfind(p_key, pos_from) : text_line.rfindn(p_key, pos_from)) != -1) {
  3604. if (last_pos <= from_column) {
  3605. pos = last_pos;
  3606. break;
  3607. }
  3608. pos_from = last_pos - p_key.length();
  3609. if (pos_from < 0) {
  3610. break;
  3611. }
  3612. }
  3613. } else {
  3614. while ((last_pos = (p_search_flags & SEARCH_MATCH_CASE) ? text_line.find(p_key, pos_from) : text_line.findn(p_key, pos_from)) != -1) {
  3615. if (last_pos >= from_column) {
  3616. pos = last_pos;
  3617. break;
  3618. }
  3619. pos_from = last_pos + p_key.length();
  3620. }
  3621. }
  3622. bool is_match = true;
  3623. if (pos != -1 && (p_search_flags & SEARCH_WHOLE_WORDS)) {
  3624. // Validate for whole words.
  3625. if (pos > 0 && !is_symbol(text_line[pos - 1])) {
  3626. is_match = false;
  3627. } else if (pos + p_key.length() < text_line.length() && !is_symbol(text_line[pos + p_key.length()])) {
  3628. is_match = false;
  3629. }
  3630. }
  3631. if (pos_from == -1) {
  3632. pos = -1;
  3633. }
  3634. if (is_match || last_pos == -1 || pos == -1) {
  3635. break;
  3636. }
  3637. pos_from = (p_search_flags & SEARCH_BACKWARDS) ? pos - 1 : pos + 1;
  3638. pos = -1;
  3639. }
  3640. if (pos != -1) {
  3641. break;
  3642. }
  3643. if (p_search_flags & SEARCH_BACKWARDS) {
  3644. line--;
  3645. } else {
  3646. line++;
  3647. }
  3648. }
  3649. return (pos == -1) ? Point2i(-1, -1) : Point2i(pos, line);
  3650. }
  3651. /* Mouse */
  3652. Point2 TextEdit::get_local_mouse_pos() const {
  3653. Point2 mp = get_local_mouse_position();
  3654. if (is_layout_rtl()) {
  3655. mp.x = get_size().width - mp.x;
  3656. }
  3657. return mp;
  3658. }
  3659. String TextEdit::get_word_at_pos(const Vector2 &p_pos) const {
  3660. Point2i pos = get_line_column_at_pos(p_pos);
  3661. int row = pos.y;
  3662. int col = pos.x;
  3663. String s = text[row];
  3664. if (s.length() == 0) {
  3665. return "";
  3666. }
  3667. int beg, end;
  3668. if (select_word(s, col, beg, end)) {
  3669. bool inside_quotes = false;
  3670. char32_t selected_quote = '\0';
  3671. int qbegin = 0, qend = 0;
  3672. for (int i = 0; i < s.length(); i++) {
  3673. if (s[i] == '"' || s[i] == '\'') {
  3674. if (i == 0 || s[i - 1] != '\\') {
  3675. if (inside_quotes && selected_quote == s[i]) {
  3676. qend = i;
  3677. inside_quotes = false;
  3678. selected_quote = '\0';
  3679. if (col >= qbegin && col <= qend) {
  3680. return s.substr(qbegin, qend - qbegin);
  3681. }
  3682. } else if (!inside_quotes) {
  3683. qbegin = i + 1;
  3684. inside_quotes = true;
  3685. selected_quote = s[i];
  3686. }
  3687. }
  3688. }
  3689. }
  3690. return s.substr(beg, end - beg);
  3691. }
  3692. return String();
  3693. }
  3694. Point2i TextEdit::get_line_column_at_pos(const Point2i &p_pos, bool p_allow_out_of_bounds) const {
  3695. float rows = p_pos.y;
  3696. rows -= style_normal->get_margin(SIDE_TOP);
  3697. rows /= get_line_height();
  3698. rows += _get_v_scroll_offset();
  3699. int first_vis_line = get_first_visible_line();
  3700. int row = first_vis_line + Math::floor(rows);
  3701. int wrap_index = 0;
  3702. if (get_line_wrapping_mode() != LineWrappingMode::LINE_WRAPPING_NONE || _is_hiding_enabled()) {
  3703. Point2i f_ofs = get_next_visible_line_index_offset_from(first_vis_line, first_visible_line_wrap_ofs, rows + (1 * SIGN(rows)));
  3704. wrap_index = f_ofs.y;
  3705. if (rows < 0) {
  3706. row = first_vis_line - (f_ofs.x - 1);
  3707. } else {
  3708. row = first_vis_line + (f_ofs.x - 1);
  3709. }
  3710. }
  3711. if (row < 0) {
  3712. row = 0;
  3713. }
  3714. if (row >= text.size()) {
  3715. row = text.size() - 1;
  3716. }
  3717. int visible_lines = get_visible_line_count_in_range(first_vis_line, row);
  3718. if (rows > visible_lines) {
  3719. if (!p_allow_out_of_bounds) {
  3720. return Point2i(-1, -1);
  3721. }
  3722. return Point2i(text[row].length(), row);
  3723. }
  3724. int col = 0;
  3725. int colx = p_pos.x - (style_normal->get_margin(SIDE_LEFT) + gutters_width + gutter_padding);
  3726. colx += first_visible_col;
  3727. col = _get_char_pos_for_line(colx, row, wrap_index);
  3728. if (get_line_wrapping_mode() != LineWrappingMode::LINE_WRAPPING_NONE && wrap_index < get_line_wrap_count(row)) {
  3729. // Move back one if we are at the end of the row.
  3730. Vector<String> rows2 = get_line_wrapped_text(row);
  3731. int row_end_col = 0;
  3732. for (int i = 0; i < wrap_index + 1; i++) {
  3733. row_end_col += rows2[i].length();
  3734. }
  3735. if (col >= row_end_col) {
  3736. col -= 1;
  3737. }
  3738. }
  3739. RID text_rid = text.get_line_data(row)->get_line_rid(wrap_index);
  3740. if (is_layout_rtl()) {
  3741. colx = TS->shaped_text_get_size(text_rid).x - colx;
  3742. }
  3743. col = TS->shaped_text_hit_test_position(text_rid, colx);
  3744. return Point2i(col, row);
  3745. }
  3746. Point2i TextEdit::get_pos_at_line_column(int p_line, int p_column) const {
  3747. Rect2i rect = get_rect_at_line_column(p_line, p_column);
  3748. return rect.position.x == -1 ? rect.position : rect.position + Vector2i(0, get_line_height());
  3749. }
  3750. Rect2i TextEdit::get_rect_at_line_column(int p_line, int p_column) const {
  3751. ERR_FAIL_INDEX_V(p_line, text.size(), Rect2i(-1, -1, 0, 0));
  3752. ERR_FAIL_COND_V(p_column < 0, Rect2i(-1, -1, 0, 0));
  3753. ERR_FAIL_COND_V(p_column > text[p_line].length(), Rect2i(-1, -1, 0, 0));
  3754. if (line_drawing_cache.size() == 0 || !line_drawing_cache.has(p_line)) {
  3755. // Line is not in the cache, which means it's outside of the viewing area.
  3756. return Rect2i(-1, -1, 0, 0);
  3757. }
  3758. LineDrawingCache cache_entry = line_drawing_cache[p_line];
  3759. int wrap_index = get_line_wrap_index_at_column(p_line, p_column);
  3760. if (wrap_index >= cache_entry.first_visible_chars.size()) {
  3761. // Line seems to be wrapped beyond the viewable area.
  3762. return Rect2i(-1, -1, 0, 0);
  3763. }
  3764. int first_visible_char = cache_entry.first_visible_chars[wrap_index];
  3765. int last_visible_char = cache_entry.last_visible_chars[wrap_index];
  3766. if (p_column < first_visible_char || p_column > last_visible_char) {
  3767. // Character is outside of the viewing area, no point calculating its position.
  3768. return Rect2i(-1, -1, 0, 0);
  3769. }
  3770. Point2i pos, size;
  3771. pos.y = cache_entry.y_offset + get_line_height() * wrap_index;
  3772. pos.x = get_total_gutter_width() + style_normal->get_margin(SIDE_LEFT) - get_h_scroll();
  3773. RID text_rid = text.get_line_data(p_line)->get_line_rid(wrap_index);
  3774. Vector2 col_bounds = TS->shaped_text_get_grapheme_bounds(text_rid, p_column);
  3775. pos.x += col_bounds.x;
  3776. size.x = col_bounds.y - col_bounds.x;
  3777. size.y = get_line_height();
  3778. return Rect2i(pos, size);
  3779. }
  3780. int TextEdit::get_minimap_line_at_pos(const Point2i &p_pos) const {
  3781. float rows = p_pos.y;
  3782. rows -= style_normal->get_margin(SIDE_TOP);
  3783. rows /= (minimap_char_size.y + minimap_line_spacing);
  3784. rows += _get_v_scroll_offset();
  3785. // calculate visible lines
  3786. int minimap_visible_lines = get_minimap_visible_lines();
  3787. int visible_rows = get_visible_line_count() + 1;
  3788. int first_vis_line = get_first_visible_line() - 1;
  3789. int draw_amount = visible_rows + (smooth_scroll_enabled ? 1 : 0);
  3790. draw_amount += get_line_wrap_count(first_vis_line + 1);
  3791. int minimap_line_height = (minimap_char_size.y + minimap_line_spacing);
  3792. // calculate viewport size and y offset
  3793. int viewport_height = (draw_amount - 1) * minimap_line_height;
  3794. int control_height = _get_control_height() - viewport_height;
  3795. int viewport_offset_y = round(get_scroll_pos_for_line(first_vis_line + 1) * control_height) / ((v_scroll->get_max() <= minimap_visible_lines) ? (minimap_visible_lines - draw_amount) : (v_scroll->get_max() - draw_amount));
  3796. // calculate the first line.
  3797. int num_lines_before = round((viewport_offset_y) / minimap_line_height);
  3798. int minimap_line = (v_scroll->get_max() <= minimap_visible_lines) ? -1 : first_vis_line;
  3799. if (first_vis_line > 0 && minimap_line >= 0) {
  3800. minimap_line -= get_next_visible_line_index_offset_from(first_vis_line, 0, -num_lines_before).x;
  3801. minimap_line -= (minimap_line > 0 && smooth_scroll_enabled ? 1 : 0);
  3802. }
  3803. if (minimap_line < 0) {
  3804. minimap_line = 0;
  3805. }
  3806. int row = minimap_line + Math::floor(rows);
  3807. if (get_line_wrapping_mode() != LineWrappingMode::LINE_WRAPPING_NONE || _is_hiding_enabled()) {
  3808. int f_ofs = get_next_visible_line_index_offset_from(minimap_line, first_visible_line_wrap_ofs, rows + (1 * SIGN(rows))).x - 1;
  3809. if (rows < 0) {
  3810. row = minimap_line - f_ofs;
  3811. } else {
  3812. row = minimap_line + f_ofs;
  3813. }
  3814. }
  3815. if (row < 0) {
  3816. row = 0;
  3817. }
  3818. if (row >= text.size()) {
  3819. row = text.size() - 1;
  3820. }
  3821. return row;
  3822. }
  3823. bool TextEdit::is_dragging_cursor() const {
  3824. return dragging_selection || dragging_minimap;
  3825. }
  3826. bool TextEdit::is_mouse_over_selection(bool p_edges, int p_caret) const {
  3827. for (int i = 0; i < carets.size(); i++) {
  3828. if (p_caret != -1 && p_caret != i) {
  3829. continue;
  3830. }
  3831. if (!has_selection(i)) {
  3832. continue;
  3833. }
  3834. Point2i pos = get_line_column_at_pos(get_local_mouse_pos());
  3835. int row = pos.y;
  3836. int col = pos.x;
  3837. if (p_edges) {
  3838. if ((row == get_selection_from_line(i) && col == get_selection_from_column(i)) || (row == get_selection_to_line(i) && col == get_selection_to_column(i))) {
  3839. return true;
  3840. }
  3841. }
  3842. if (row >= get_selection_from_line(i) && row <= get_selection_to_line(i) && (row > get_selection_from_line(i) || col > get_selection_from_column(i)) && (row < get_selection_to_line(i) || col < get_selection_to_column(i))) {
  3843. return true;
  3844. }
  3845. }
  3846. return false;
  3847. }
  3848. /* Caret */
  3849. void TextEdit::set_caret_type(CaretType p_type) {
  3850. if (caret_type == p_type) {
  3851. return;
  3852. }
  3853. caret_type = p_type;
  3854. queue_redraw();
  3855. }
  3856. TextEdit::CaretType TextEdit::get_caret_type() const {
  3857. return caret_type;
  3858. }
  3859. void TextEdit::set_caret_blink_enabled(const bool p_enabled) {
  3860. if (caret_blink_enabled == p_enabled) {
  3861. return;
  3862. }
  3863. caret_blink_enabled = p_enabled;
  3864. if (has_focus()) {
  3865. if (p_enabled) {
  3866. caret_blink_timer->start();
  3867. } else {
  3868. caret_blink_timer->stop();
  3869. }
  3870. }
  3871. draw_caret = true;
  3872. }
  3873. bool TextEdit::is_caret_blink_enabled() const {
  3874. return caret_blink_enabled;
  3875. }
  3876. float TextEdit::get_caret_blink_interval() const {
  3877. return caret_blink_timer->get_wait_time();
  3878. }
  3879. void TextEdit::set_caret_blink_interval(const float p_interval) {
  3880. ERR_FAIL_COND(p_interval <= 0);
  3881. caret_blink_timer->set_wait_time(p_interval);
  3882. }
  3883. void TextEdit::set_move_caret_on_right_click_enabled(const bool p_enabled) {
  3884. move_caret_on_right_click = p_enabled;
  3885. }
  3886. bool TextEdit::is_move_caret_on_right_click_enabled() const {
  3887. return move_caret_on_right_click;
  3888. }
  3889. void TextEdit::set_caret_mid_grapheme_enabled(const bool p_enabled) {
  3890. caret_mid_grapheme_enabled = p_enabled;
  3891. }
  3892. bool TextEdit::is_caret_mid_grapheme_enabled() const {
  3893. return caret_mid_grapheme_enabled;
  3894. }
  3895. void TextEdit::set_multiple_carets_enabled(bool p_enabled) {
  3896. multi_carets_enabled = p_enabled;
  3897. if (!multi_carets_enabled) {
  3898. remove_secondary_carets();
  3899. }
  3900. }
  3901. bool TextEdit::is_multiple_carets_enabled() const {
  3902. return multi_carets_enabled;
  3903. }
  3904. int TextEdit::add_caret(int p_line, int p_col) {
  3905. if (!multi_carets_enabled) {
  3906. return -1;
  3907. }
  3908. p_line = CLAMP(p_line, 0, text.size() - 1);
  3909. p_col = CLAMP(p_col, 0, get_line(p_line).length());
  3910. for (int i = 0; i < carets.size(); i++) {
  3911. if (get_caret_line(i) == p_line && get_caret_column(i) == p_col) {
  3912. return -1;
  3913. }
  3914. if (has_selection(i)) {
  3915. if (p_line >= get_selection_from_line(i) && p_line <= get_selection_to_line(i) && (p_line > get_selection_from_line(i) || p_col >= get_selection_from_column(i)) && (p_line < get_selection_to_line(i) || p_col <= get_selection_to_column(i))) {
  3916. return -1;
  3917. }
  3918. }
  3919. }
  3920. carets.push_back(Caret());
  3921. set_caret_line(p_line, false, false, 0, carets.size() - 1);
  3922. set_caret_column(p_col, false, carets.size() - 1);
  3923. caret_index_edit_dirty = true;
  3924. return carets.size() - 1;
  3925. }
  3926. void TextEdit::remove_caret(int p_caret) {
  3927. ERR_FAIL_COND_MSG(carets.size() <= 1, "The main caret should not be removed.");
  3928. ERR_FAIL_INDEX(p_caret, carets.size());
  3929. carets.remove_at(p_caret);
  3930. caret_index_edit_dirty = true;
  3931. }
  3932. void TextEdit::remove_secondary_carets() {
  3933. carets.resize(1);
  3934. caret_index_edit_dirty = true;
  3935. queue_redraw();
  3936. }
  3937. void TextEdit::merge_overlapping_carets() {
  3938. Vector<int> caret_edit_order = get_caret_index_edit_order();
  3939. for (int i = 0; i < caret_edit_order.size() - 1; i++) {
  3940. int first_caret = caret_edit_order[i];
  3941. int second_caret = caret_edit_order[i + 1];
  3942. // Both have selection.
  3943. if (has_selection(first_caret) && has_selection(second_caret)) {
  3944. bool should_merge = false;
  3945. if (get_selection_from_line(first_caret) >= get_selection_from_line(second_caret) && get_selection_from_line(first_caret) <= get_selection_to_line(second_caret) && (get_selection_from_line(first_caret) > get_selection_from_line(second_caret) || get_selection_from_column(first_caret) >= get_selection_from_column(second_caret)) && (get_selection_from_line(first_caret) < get_selection_to_line(second_caret) || get_selection_from_column(first_caret) <= get_selection_to_column(second_caret))) {
  3946. should_merge = true;
  3947. }
  3948. if (get_selection_to_line(first_caret) >= get_selection_from_line(second_caret) && get_selection_to_line(first_caret) <= get_selection_to_line(second_caret) && (get_selection_to_line(first_caret) > get_selection_from_line(second_caret) || get_selection_to_column(first_caret) >= get_selection_from_column(second_caret)) && (get_selection_to_line(first_caret) < get_selection_to_line(second_caret) || get_selection_to_column(first_caret) <= get_selection_to_column(second_caret))) {
  3949. should_merge = true;
  3950. }
  3951. if (!should_merge) {
  3952. continue;
  3953. }
  3954. // Save the newest one for click+drag.
  3955. int caret_to_save = first_caret;
  3956. int caret_to_remove = second_caret;
  3957. if (first_caret < second_caret) {
  3958. caret_to_save = second_caret;
  3959. caret_to_remove = first_caret;
  3960. }
  3961. int from_line = MIN(get_selection_from_line(caret_to_save), get_selection_from_line(caret_to_remove));
  3962. int to_line = MAX(get_selection_to_line(caret_to_save), get_selection_to_line(caret_to_remove));
  3963. int from_col = get_selection_from_column(caret_to_save);
  3964. int to_col = get_selection_to_column(caret_to_save);
  3965. int selection_line = get_selection_line(caret_to_save);
  3966. int selection_col = get_selection_column(caret_to_save);
  3967. bool at_from = (get_caret_line(caret_to_save) == get_selection_from_line(caret_to_save) && get_caret_column(caret_to_save) == get_selection_from_column(caret_to_save));
  3968. if (at_from) {
  3969. if (get_selection_line(caret_to_remove) > get_selection_line(caret_to_save) || (get_selection_line(caret_to_remove) == get_selection_line(caret_to_save) && get_selection_column(caret_to_remove) >= get_selection_column(caret_to_save))) {
  3970. selection_line = get_selection_line(caret_to_remove);
  3971. selection_col = get_selection_column(caret_to_remove);
  3972. }
  3973. } else if (get_selection_line(caret_to_remove) < get_selection_line(caret_to_save) || (get_selection_line(caret_to_remove) == get_selection_line(caret_to_save) && get_selection_column(caret_to_remove) <= get_selection_column(caret_to_save))) {
  3974. selection_line = get_selection_line(caret_to_remove);
  3975. selection_col = get_selection_column(caret_to_remove);
  3976. }
  3977. if (get_selection_from_line(caret_to_remove) < get_selection_from_line(caret_to_save) || (get_selection_from_line(caret_to_remove) == get_selection_from_line(caret_to_save) && get_selection_from_column(caret_to_remove) <= get_selection_from_column(caret_to_save))) {
  3978. from_col = get_selection_from_column(caret_to_remove);
  3979. } else {
  3980. to_col = get_selection_to_column(caret_to_remove);
  3981. }
  3982. select(from_line, from_col, to_line, to_col, caret_to_save);
  3983. set_selection_mode(selecting_mode, selection_line, selection_col, caret_to_save);
  3984. set_caret_line((at_from ? from_line : to_line), caret_to_save == 0, true, 0, caret_to_save);
  3985. set_caret_column((at_from ? from_col : to_col), caret_to_save == 0, caret_to_save);
  3986. remove_caret(caret_to_remove);
  3987. i--;
  3988. caret_edit_order = get_caret_index_edit_order();
  3989. continue;
  3990. }
  3991. // Only first has selection.
  3992. if (has_selection(first_caret)) {
  3993. if (get_caret_line(second_caret) >= get_selection_from_line(first_caret) && get_caret_line(second_caret) <= get_selection_to_line(first_caret) && (get_caret_line(second_caret) > get_selection_from_line(first_caret) || get_caret_column(second_caret) >= get_selection_from_column(first_caret)) && (get_caret_line(second_caret) < get_selection_to_line(first_caret) || get_caret_column(second_caret) <= get_selection_to_column(first_caret))) {
  3994. remove_caret(second_caret);
  3995. caret_edit_order = get_caret_index_edit_order();
  3996. i--;
  3997. }
  3998. continue;
  3999. }
  4000. // Only second has Selection.
  4001. if (has_selection(second_caret)) {
  4002. if (get_caret_line(first_caret) >= get_selection_from_line(second_caret) && get_caret_line(first_caret) <= get_selection_to_line(second_caret) && (get_caret_line(first_caret) > get_selection_from_line(second_caret) || get_caret_column(first_caret) >= get_selection_from_column(second_caret)) && (get_caret_line(first_caret) < get_selection_to_line(second_caret) || get_caret_column(first_caret) <= get_selection_to_column(second_caret))) {
  4003. remove_caret(first_caret);
  4004. caret_edit_order = get_caret_index_edit_order();
  4005. i--;
  4006. }
  4007. continue;
  4008. }
  4009. // Both have no selection.
  4010. if (get_caret_line(first_caret) == get_caret_line(second_caret) && get_caret_column(first_caret) == get_caret_column(second_caret)) {
  4011. // Save the newest one for click+drag.
  4012. if (first_caret < second_caret) {
  4013. remove_caret(first_caret);
  4014. } else {
  4015. remove_caret(second_caret);
  4016. }
  4017. i--;
  4018. caret_edit_order = get_caret_index_edit_order();
  4019. continue;
  4020. }
  4021. }
  4022. }
  4023. int TextEdit::get_caret_count() const {
  4024. return carets.size();
  4025. }
  4026. void TextEdit::add_caret_at_carets(bool p_below) {
  4027. Vector<int> caret_edit_order = get_caret_index_edit_order();
  4028. for (const int &caret_index : caret_edit_order) {
  4029. const int caret_line = get_caret_line(caret_index);
  4030. const int caret_column = get_caret_column(caret_index);
  4031. // The last fit x will be cleared if the caret has a selection,
  4032. // but if it does not have a selection the last fit x will be
  4033. // transferred to the new caret
  4034. int caret_from_column = 0, caret_to_column = 0, caret_last_fit_x = carets[caret_index].last_fit_x;
  4035. if (has_selection(caret_index)) {
  4036. // If the selection goes over multiple lines, deselect it.
  4037. if (get_selection_from_line(caret_index) != get_selection_to_line(caret_index)) {
  4038. deselect(caret_index);
  4039. } else {
  4040. caret_from_column = get_selection_from_column(caret_index);
  4041. caret_to_column = get_selection_to_column(caret_index);
  4042. caret_last_fit_x = -1;
  4043. carets.write[caret_index].last_fit_x = _get_column_x_offset_for_line(caret_column, caret_line, caret_column);
  4044. }
  4045. }
  4046. // Get the line and column of the new caret as if you would move the caret by pressing the arrow keys
  4047. int new_caret_line, new_caret_column, new_caret_from_column = 0, new_caret_to_column = 0;
  4048. _get_above_below_caret_line_column(caret_line, get_caret_wrap_index(caret_index), caret_column, p_below, new_caret_line, new_caret_column, caret_last_fit_x);
  4049. // If the caret does have a selection calculate the new from and to columns
  4050. if (caret_from_column != caret_to_column) {
  4051. // We only need to calculate the selection columns if the column of the caret changed
  4052. if (caret_column != new_caret_column) {
  4053. int _; // unused placeholder for p_new_line
  4054. _get_above_below_caret_line_column(caret_line, get_caret_wrap_index(caret_index), caret_from_column, p_below, _, new_caret_from_column);
  4055. _get_above_below_caret_line_column(caret_line, get_caret_wrap_index(caret_index), caret_to_column, p_below, _, new_caret_to_column);
  4056. } else {
  4057. new_caret_from_column = caret_from_column;
  4058. new_caret_to_column = caret_to_column;
  4059. }
  4060. }
  4061. // Add the new caret
  4062. const int new_caret_index = add_caret(new_caret_line, new_caret_column);
  4063. if (new_caret_index == -1) {
  4064. continue;
  4065. }
  4066. // Also add the selection if there should be one
  4067. if (new_caret_from_column != new_caret_to_column) {
  4068. select(new_caret_line, new_caret_from_column, new_caret_line, new_caret_to_column, new_caret_index);
  4069. // Necessary to properly modify the selection after adding the new caret
  4070. carets.write[new_caret_index].selection.selecting_line = new_caret_line;
  4071. carets.write[new_caret_index].selection.selecting_column = new_caret_column == new_caret_from_column ? new_caret_to_column : new_caret_from_column;
  4072. continue;
  4073. }
  4074. // Copy the last fit x over
  4075. carets.write[new_caret_index].last_fit_x = carets[caret_index].last_fit_x;
  4076. }
  4077. merge_overlapping_carets();
  4078. queue_redraw();
  4079. }
  4080. Vector<int> TextEdit::get_caret_index_edit_order() {
  4081. if (!caret_index_edit_dirty) {
  4082. return caret_index_edit_order;
  4083. }
  4084. caret_index_edit_order.clear();
  4085. caret_index_edit_order.push_back(0);
  4086. for (int i = 1; i < carets.size(); i++) {
  4087. int j = 0;
  4088. int line = carets[i].selection.active ? carets[i].selection.to_line : carets[i].line;
  4089. int col = carets[i].selection.active ? carets[i].selection.to_column : carets[i].column;
  4090. for (; j < caret_index_edit_order.size(); j++) {
  4091. int idx = caret_index_edit_order[j];
  4092. int other_line = carets[idx].selection.active ? carets[idx].selection.to_line : carets[idx].line;
  4093. int other_col = carets[idx].selection.active ? carets[idx].selection.to_column : carets[idx].column;
  4094. if (line > other_line || (line == other_line && col > other_col)) {
  4095. break;
  4096. }
  4097. }
  4098. caret_index_edit_order.insert(j, i);
  4099. }
  4100. caret_index_edit_dirty = false;
  4101. return caret_index_edit_order;
  4102. }
  4103. void TextEdit::adjust_carets_after_edit(int p_caret, int p_from_line, int p_from_col, int p_to_line, int p_to_col) {
  4104. int edit_height = p_from_line - p_to_line;
  4105. int edit_size = ((edit_height == 0) ? p_from_col : 0) - p_to_col;
  4106. Vector<int> caret_edit_order = get_caret_index_edit_order();
  4107. for (int j = 0; j < caret_edit_order.size(); j++) {
  4108. if (caret_edit_order[j] == p_caret) {
  4109. return;
  4110. }
  4111. // Adjust caret.
  4112. // set_caret_line could adjust the column, so save here.
  4113. int cc = get_caret_column(caret_edit_order[j]);
  4114. if (edit_height != 0) {
  4115. set_caret_line(get_caret_line(caret_edit_order[j]) + edit_height, false, true, 0, caret_edit_order[j]);
  4116. }
  4117. if (get_caret_line(p_caret) == get_caret_line(caret_edit_order[j])) {
  4118. set_caret_column(cc + edit_size, false, caret_edit_order[j]);
  4119. }
  4120. // Adjust selection.
  4121. if (!has_selection(caret_edit_order[j])) {
  4122. continue;
  4123. }
  4124. if (edit_height != 0) {
  4125. carets.write[caret_edit_order[j]].selection.from_line += edit_height;
  4126. carets.write[caret_edit_order[j]].selection.to_line += edit_height;
  4127. }
  4128. if (get_caret_line(p_caret) == carets[caret_edit_order[j]].selection.from_line) {
  4129. carets.write[caret_edit_order[j]].selection.from_column += edit_size;
  4130. }
  4131. }
  4132. }
  4133. bool TextEdit::is_caret_visible(int p_caret) const {
  4134. ERR_FAIL_INDEX_V(p_caret, carets.size(), 0);
  4135. return carets[p_caret].visible;
  4136. }
  4137. Point2 TextEdit::get_caret_draw_pos(int p_caret) const {
  4138. ERR_FAIL_INDEX_V(p_caret, carets.size(), Point2(0, 0));
  4139. return carets[p_caret].draw_pos;
  4140. }
  4141. void TextEdit::set_caret_line(int p_line, bool p_adjust_viewport, bool p_can_be_hidden, int p_wrap_index, int p_caret) {
  4142. ERR_FAIL_INDEX(p_caret, carets.size());
  4143. if (setting_caret_line) {
  4144. return;
  4145. }
  4146. setting_caret_line = true;
  4147. if (p_line < 0) {
  4148. p_line = 0;
  4149. }
  4150. if (p_line >= text.size()) {
  4151. p_line = text.size() - 1;
  4152. }
  4153. if (!p_can_be_hidden) {
  4154. if (_is_line_hidden(CLAMP(p_line, 0, text.size() - 1))) {
  4155. int move_down = get_next_visible_line_offset_from(p_line, 1) - 1;
  4156. if (p_line + move_down <= text.size() - 1 && !_is_line_hidden(p_line + move_down)) {
  4157. p_line += move_down;
  4158. } else {
  4159. int move_up = get_next_visible_line_offset_from(p_line, -1) - 1;
  4160. if (p_line - move_up > 0 && !_is_line_hidden(p_line - move_up)) {
  4161. p_line -= move_up;
  4162. } else {
  4163. WARN_PRINT(("Caret set to hidden line " + itos(p_line) + " and there are no nonhidden lines."));
  4164. }
  4165. }
  4166. }
  4167. }
  4168. bool caret_moved = get_caret_line(p_caret) != p_line;
  4169. carets.write[p_caret].line = p_line;
  4170. int n_col = _get_char_pos_for_line(carets[p_caret].last_fit_x, p_line, p_wrap_index);
  4171. if (n_col != 0 && get_line_wrapping_mode() != LineWrappingMode::LINE_WRAPPING_NONE && p_wrap_index < get_line_wrap_count(p_line)) {
  4172. Vector<String> rows = get_line_wrapped_text(p_line);
  4173. int row_end_col = 0;
  4174. for (int i = 0; i < p_wrap_index + 1; i++) {
  4175. row_end_col += rows[i].length();
  4176. }
  4177. if (n_col >= row_end_col) {
  4178. n_col -= 1;
  4179. }
  4180. }
  4181. caret_moved = (caret_moved || get_caret_column(p_caret) != n_col);
  4182. carets.write[p_caret].column = n_col;
  4183. if (is_inside_tree() && p_adjust_viewport) {
  4184. adjust_viewport_to_caret(p_caret);
  4185. }
  4186. setting_caret_line = false;
  4187. if (caret_moved && !caret_pos_dirty) {
  4188. if (is_inside_tree()) {
  4189. MessageQueue::get_singleton()->push_call(this, "_emit_caret_changed");
  4190. }
  4191. caret_pos_dirty = true;
  4192. }
  4193. }
  4194. int TextEdit::get_caret_line(int p_caret) const {
  4195. ERR_FAIL_INDEX_V(p_caret, carets.size(), 0);
  4196. return carets[p_caret].line;
  4197. }
  4198. void TextEdit::set_caret_column(int p_col, bool p_adjust_viewport, int p_caret) {
  4199. ERR_FAIL_INDEX(p_caret, carets.size());
  4200. if (p_col < 0) {
  4201. p_col = 0;
  4202. }
  4203. if (p_col > get_line(get_caret_line(p_caret)).length()) {
  4204. p_col = get_line(get_caret_line(p_caret)).length();
  4205. }
  4206. bool caret_moved = get_caret_column(p_caret) != p_col;
  4207. carets.write[p_caret].column = p_col;
  4208. carets.write[p_caret].last_fit_x = _get_column_x_offset_for_line(get_caret_column(p_caret), get_caret_line(p_caret), get_caret_column(p_caret));
  4209. if (is_inside_tree() && p_adjust_viewport) {
  4210. adjust_viewport_to_caret(p_caret);
  4211. }
  4212. if (caret_moved && !caret_pos_dirty) {
  4213. if (is_inside_tree()) {
  4214. MessageQueue::get_singleton()->push_call(this, "_emit_caret_changed");
  4215. }
  4216. caret_pos_dirty = true;
  4217. }
  4218. }
  4219. int TextEdit::get_caret_column(int p_caret) const {
  4220. ERR_FAIL_INDEX_V(p_caret, carets.size(), 0);
  4221. return carets[p_caret].column;
  4222. }
  4223. int TextEdit::get_caret_wrap_index(int p_caret) const {
  4224. ERR_FAIL_INDEX_V(p_caret, carets.size(), 0);
  4225. return get_line_wrap_index_at_column(get_caret_line(p_caret), get_caret_column(p_caret));
  4226. }
  4227. String TextEdit::get_word_under_caret(int p_caret) const {
  4228. ERR_FAIL_COND_V(p_caret > carets.size(), "");
  4229. StringBuilder selected_text;
  4230. for (int c = 0; c < carets.size(); c++) {
  4231. if (p_caret != -1 && p_caret != c) {
  4232. continue;
  4233. }
  4234. PackedInt32Array words = TS->shaped_text_get_word_breaks(text.get_line_data(get_caret_line(c))->get_rid());
  4235. for (int i = 0; i < words.size(); i = i + 2) {
  4236. if (words[i] <= get_caret_column(c) && words[i + 1] > get_caret_column(c)) {
  4237. selected_text += text[get_caret_line(c)].substr(words[i], words[i + 1] - words[i]);
  4238. if (p_caret == -1 && c != carets.size() - 1) {
  4239. selected_text += "\n";
  4240. }
  4241. }
  4242. }
  4243. }
  4244. return selected_text.as_string();
  4245. }
  4246. /* Selection. */
  4247. void TextEdit::set_selecting_enabled(const bool p_enabled) {
  4248. if (selecting_enabled == p_enabled) {
  4249. return;
  4250. }
  4251. selecting_enabled = p_enabled;
  4252. if (!selecting_enabled) {
  4253. deselect();
  4254. }
  4255. }
  4256. bool TextEdit::is_selecting_enabled() const {
  4257. return selecting_enabled;
  4258. }
  4259. void TextEdit::set_deselect_on_focus_loss_enabled(const bool p_enabled) {
  4260. if (deselect_on_focus_loss_enabled == p_enabled) {
  4261. return;
  4262. }
  4263. deselect_on_focus_loss_enabled = p_enabled;
  4264. if (p_enabled && has_selection() && !has_focus()) {
  4265. deselect();
  4266. }
  4267. }
  4268. bool TextEdit::is_deselect_on_focus_loss_enabled() const {
  4269. return deselect_on_focus_loss_enabled;
  4270. }
  4271. void TextEdit::set_drag_and_drop_selection_enabled(const bool p_enabled) {
  4272. drag_and_drop_selection_enabled = p_enabled;
  4273. }
  4274. bool TextEdit::is_drag_and_drop_selection_enabled() const {
  4275. return drag_and_drop_selection_enabled;
  4276. }
  4277. void TextEdit::set_selection_mode(SelectionMode p_mode, int p_line, int p_column, int p_caret) {
  4278. ERR_FAIL_INDEX(p_caret, carets.size());
  4279. selecting_mode = p_mode;
  4280. if (p_line >= 0) {
  4281. ERR_FAIL_INDEX(p_line, text.size());
  4282. carets.write[p_caret].selection.selecting_line = p_line;
  4283. carets.write[p_caret].selection.selecting_column = CLAMP(carets[p_caret].selection.selecting_column, 0, text[carets[p_caret].selection.selecting_line].length());
  4284. }
  4285. if (p_column >= 0) {
  4286. ERR_FAIL_INDEX(carets[p_caret].selection.selecting_line, text.size());
  4287. ERR_FAIL_INDEX(p_column, text[carets[p_caret].selection.selecting_line].length() + 1);
  4288. carets.write[p_caret].selection.selecting_column = p_column;
  4289. }
  4290. }
  4291. TextEdit::SelectionMode TextEdit::get_selection_mode() const {
  4292. return selecting_mode;
  4293. }
  4294. void TextEdit::select_all() {
  4295. if (!selecting_enabled) {
  4296. return;
  4297. }
  4298. if (text.size() == 1 && text[0].length() == 0) {
  4299. return;
  4300. }
  4301. remove_secondary_carets();
  4302. select(0, 0, text.size() - 1, text[text.size() - 1].length());
  4303. set_selection_mode(SelectionMode::SELECTION_MODE_SHIFT, 0, 0);
  4304. carets.write[0].selection.shiftclick_left = true;
  4305. set_caret_line(get_selection_to_line(), false);
  4306. set_caret_column(get_selection_to_column(), false);
  4307. queue_redraw();
  4308. }
  4309. void TextEdit::select_word_under_caret(int p_caret) {
  4310. ERR_FAIL_COND(p_caret > carets.size());
  4311. if (!selecting_enabled) {
  4312. return;
  4313. }
  4314. if (text.size() == 1 && text[0].length() == 0) {
  4315. return;
  4316. }
  4317. for (int c = 0; c < carets.size(); c++) {
  4318. if (p_caret != -1 && p_caret != c) {
  4319. continue;
  4320. }
  4321. if (has_selection(c)) {
  4322. // Allow toggling selection by pressing the shortcut a second time.
  4323. // This is also usable as a general-purpose "deselect" shortcut after
  4324. // selecting anything.
  4325. deselect(c);
  4326. continue;
  4327. }
  4328. int begin = 0;
  4329. int end = 0;
  4330. const PackedInt32Array words = TS->shaped_text_get_word_breaks(text.get_line_data(get_caret_line(c))->get_rid());
  4331. for (int i = 0; i < words.size(); i = i + 2) {
  4332. if ((words[i] <= get_caret_column(c) && words[i + 1] >= get_caret_column(c)) || (i == words.size() - 2 && get_caret_column(c) == words[i + 1])) {
  4333. begin = words[i];
  4334. end = words[i + 1];
  4335. break;
  4336. }
  4337. }
  4338. // No word found.
  4339. if (begin == 0 && end == 0) {
  4340. continue;
  4341. }
  4342. select(get_caret_line(c), begin, get_caret_line(c), end, c);
  4343. // Move the caret to the end of the word for easier editing.
  4344. set_caret_column(end, false, c);
  4345. }
  4346. merge_overlapping_carets();
  4347. }
  4348. void TextEdit::add_selection_for_next_occurrence() {
  4349. if (!selecting_enabled || !is_multiple_carets_enabled()) {
  4350. return;
  4351. }
  4352. if (text.size() == 1 && text[0].length() == 0) {
  4353. return;
  4354. }
  4355. // Always use the last caret, to correctly search for
  4356. // the next occurrence that comes after this caret.
  4357. int caret = get_caret_count() - 1;
  4358. if (!has_selection(caret)) {
  4359. select_word_under_caret(caret);
  4360. return;
  4361. }
  4362. const String &highlighted_text = get_selected_text(caret);
  4363. int column = get_selection_from_column(caret) + 1;
  4364. int line = get_caret_line(caret);
  4365. const Point2i next_occurrence = search(highlighted_text, SEARCH_MATCH_CASE, line, column);
  4366. if (next_occurrence.x == -1 || next_occurrence.y == -1) {
  4367. return;
  4368. }
  4369. int to_column = get_selection_to_column(caret) + 1;
  4370. int end = next_occurrence.x + (to_column - column);
  4371. int new_caret = add_caret(next_occurrence.y, end);
  4372. if (new_caret != -1) {
  4373. select(next_occurrence.y, next_occurrence.x, next_occurrence.y, end, new_caret);
  4374. adjust_viewport_to_caret(new_caret);
  4375. merge_overlapping_carets();
  4376. }
  4377. }
  4378. void TextEdit::select(int p_from_line, int p_from_column, int p_to_line, int p_to_column, int p_caret) {
  4379. ERR_FAIL_INDEX(p_caret, carets.size());
  4380. if (!selecting_enabled) {
  4381. return;
  4382. }
  4383. if (p_from_line < 0) {
  4384. p_from_line = 0;
  4385. } else if (p_from_line >= text.size()) {
  4386. p_from_line = text.size() - 1;
  4387. }
  4388. if (p_from_column >= text[p_from_line].length()) {
  4389. p_from_column = text[p_from_line].length();
  4390. }
  4391. if (p_from_column < 0) {
  4392. p_from_column = 0;
  4393. }
  4394. if (p_to_line < 0) {
  4395. p_to_line = 0;
  4396. } else if (p_to_line >= text.size()) {
  4397. p_to_line = text.size() - 1;
  4398. }
  4399. if (p_to_column >= text[p_to_line].length()) {
  4400. p_to_column = text[p_to_line].length();
  4401. }
  4402. if (p_to_column < 0) {
  4403. p_to_column = 0;
  4404. }
  4405. carets.write[p_caret].selection.from_line = p_from_line;
  4406. carets.write[p_caret].selection.from_column = p_from_column;
  4407. carets.write[p_caret].selection.to_line = p_to_line;
  4408. carets.write[p_caret].selection.to_column = p_to_column;
  4409. carets.write[p_caret].selection.active = true;
  4410. if (carets[p_caret].selection.from_line == carets[p_caret].selection.to_line) {
  4411. if (carets[p_caret].selection.from_column == carets[p_caret].selection.to_column) {
  4412. carets.write[p_caret].selection.active = false;
  4413. } else if (carets[p_caret].selection.from_column > carets[p_caret].selection.to_column) {
  4414. carets.write[p_caret].selection.shiftclick_left = false;
  4415. SWAP(carets.write[p_caret].selection.from_column, carets.write[p_caret].selection.to_column);
  4416. } else {
  4417. carets.write[p_caret].selection.shiftclick_left = true;
  4418. }
  4419. } else if (carets[p_caret].selection.from_line > carets[p_caret].selection.to_line) {
  4420. carets.write[p_caret].selection.shiftclick_left = false;
  4421. SWAP(carets.write[p_caret].selection.from_line, carets.write[p_caret].selection.to_line);
  4422. SWAP(carets.write[p_caret].selection.from_column, carets.write[p_caret].selection.to_column);
  4423. } else {
  4424. carets.write[p_caret].selection.shiftclick_left = true;
  4425. }
  4426. caret_index_edit_dirty = true;
  4427. queue_redraw();
  4428. }
  4429. bool TextEdit::has_selection(int p_caret) const {
  4430. ERR_FAIL_COND_V(p_caret > carets.size(), false);
  4431. for (int i = 0; i < carets.size(); i++) {
  4432. if (p_caret != -1 && p_caret != i) {
  4433. continue;
  4434. }
  4435. if (carets[i].selection.active) {
  4436. return true;
  4437. }
  4438. }
  4439. return false;
  4440. }
  4441. String TextEdit::get_selected_text(int p_caret) {
  4442. ERR_FAIL_COND_V(p_caret > carets.size(), "");
  4443. StringBuilder selected_text;
  4444. Vector<int> caret_edit_order = get_caret_index_edit_order();
  4445. for (int i = caret_edit_order.size() - 1; i >= 0; i--) {
  4446. int caret_idx = caret_edit_order[i];
  4447. if (p_caret != -1 && p_caret != caret_idx) {
  4448. continue;
  4449. }
  4450. if (!has_selection(caret_idx)) {
  4451. continue;
  4452. }
  4453. selected_text += _base_get_text(get_selection_from_line(caret_idx), get_selection_from_column(caret_idx), get_selection_to_line(caret_idx), get_selection_to_column(caret_idx));
  4454. if (p_caret == -1 && i != 0) {
  4455. selected_text += "\n";
  4456. }
  4457. }
  4458. return selected_text.as_string();
  4459. }
  4460. int TextEdit::get_selection_line(int p_caret) const {
  4461. ERR_FAIL_INDEX_V(p_caret, carets.size(), -1);
  4462. ERR_FAIL_COND_V(!has_selection(p_caret), -1);
  4463. return carets[p_caret].selection.selecting_line;
  4464. }
  4465. int TextEdit::get_selection_column(int p_caret) const {
  4466. ERR_FAIL_INDEX_V(p_caret, carets.size(), -1);
  4467. ERR_FAIL_COND_V(!has_selection(p_caret), -1);
  4468. return carets[p_caret].selection.selecting_column;
  4469. }
  4470. int TextEdit::get_selection_from_line(int p_caret) const {
  4471. ERR_FAIL_INDEX_V(p_caret, carets.size(), -1);
  4472. ERR_FAIL_COND_V(!has_selection(p_caret), -1);
  4473. return carets[p_caret].selection.from_line;
  4474. }
  4475. int TextEdit::get_selection_from_column(int p_caret) const {
  4476. ERR_FAIL_INDEX_V(p_caret, carets.size(), -1);
  4477. ERR_FAIL_COND_V(!has_selection(p_caret), -1);
  4478. return carets[p_caret].selection.from_column;
  4479. }
  4480. int TextEdit::get_selection_to_line(int p_caret) const {
  4481. ERR_FAIL_INDEX_V(p_caret, carets.size(), -1);
  4482. ERR_FAIL_COND_V(!has_selection(p_caret), -1);
  4483. return carets[p_caret].selection.to_line;
  4484. }
  4485. int TextEdit::get_selection_to_column(int p_caret) const {
  4486. ERR_FAIL_INDEX_V(p_caret, carets.size(), -1);
  4487. ERR_FAIL_COND_V(!has_selection(p_caret), -1);
  4488. return carets[p_caret].selection.to_column;
  4489. }
  4490. void TextEdit::deselect(int p_caret) {
  4491. ERR_FAIL_COND(p_caret > carets.size());
  4492. for (int i = 0; i < carets.size(); i++) {
  4493. if (p_caret != -1 && p_caret != i) {
  4494. continue;
  4495. }
  4496. carets.write[i].selection.active = false;
  4497. }
  4498. caret_index_edit_dirty = true;
  4499. queue_redraw();
  4500. }
  4501. void TextEdit::delete_selection(int p_caret) {
  4502. ERR_FAIL_COND(p_caret > carets.size());
  4503. begin_complex_operation();
  4504. Vector<int> caret_edit_order = get_caret_index_edit_order();
  4505. for (const int &i : caret_edit_order) {
  4506. if (p_caret != -1 && p_caret != i) {
  4507. continue;
  4508. }
  4509. if (!has_selection(i)) {
  4510. continue;
  4511. }
  4512. selecting_mode = SelectionMode::SELECTION_MODE_NONE;
  4513. _remove_text(carets[i].selection.from_line, carets[i].selection.from_column, carets[i].selection.to_line, carets[i].selection.to_column);
  4514. set_caret_line(carets[i].selection.from_line, false, false, 0, i);
  4515. set_caret_column(carets[i].selection.from_column, i == 0, i);
  4516. carets.write[i].selection.active = false;
  4517. adjust_carets_after_edit(i, carets[i].selection.from_line, carets[i].selection.from_column, carets[i].selection.to_line, carets[i].selection.to_column);
  4518. }
  4519. end_complex_operation();
  4520. queue_redraw();
  4521. }
  4522. /* Line wrapping. */
  4523. void TextEdit::set_line_wrapping_mode(LineWrappingMode p_wrapping_mode) {
  4524. if (line_wrapping_mode != p_wrapping_mode) {
  4525. line_wrapping_mode = p_wrapping_mode;
  4526. _update_wrap_at_column(true);
  4527. }
  4528. }
  4529. TextEdit::LineWrappingMode TextEdit::get_line_wrapping_mode() const {
  4530. return line_wrapping_mode;
  4531. }
  4532. bool TextEdit::is_line_wrapped(int p_line) const {
  4533. ERR_FAIL_INDEX_V(p_line, text.size(), 0);
  4534. if (get_line_wrapping_mode() == LineWrappingMode::LINE_WRAPPING_NONE) {
  4535. return false;
  4536. }
  4537. return text.get_line_wrap_amount(p_line) > 0;
  4538. }
  4539. int TextEdit::get_line_wrap_count(int p_line) const {
  4540. ERR_FAIL_INDEX_V(p_line, text.size(), 0);
  4541. if (!is_line_wrapped(p_line)) {
  4542. return 0;
  4543. }
  4544. return text.get_line_wrap_amount(p_line);
  4545. }
  4546. int TextEdit::get_line_wrap_index_at_column(int p_line, int p_column) const {
  4547. ERR_FAIL_INDEX_V(p_line, text.size(), 0);
  4548. ERR_FAIL_COND_V(p_column < 0, 0);
  4549. ERR_FAIL_COND_V(p_column > text[p_line].length(), 0);
  4550. if (!is_line_wrapped(p_line)) {
  4551. return 0;
  4552. }
  4553. /* Loop through wraps in the line text until we get to the column. */
  4554. int wrap_index = 0;
  4555. int col = 0;
  4556. Vector<String> lines = get_line_wrapped_text(p_line);
  4557. for (int i = 0; i < lines.size(); i++) {
  4558. wrap_index = i;
  4559. String s = lines[wrap_index];
  4560. col += s.length();
  4561. if (col > p_column) {
  4562. break;
  4563. }
  4564. }
  4565. return wrap_index;
  4566. }
  4567. Vector<String> TextEdit::get_line_wrapped_text(int p_line) const {
  4568. ERR_FAIL_INDEX_V(p_line, text.size(), Vector<String>());
  4569. Vector<String> lines;
  4570. if (!is_line_wrapped(p_line)) {
  4571. lines.push_back(text[p_line]);
  4572. return lines;
  4573. }
  4574. const String &line_text = text[p_line];
  4575. Vector<Vector2i> line_ranges = text.get_line_wrap_ranges(p_line);
  4576. for (int i = 0; i < line_ranges.size(); i++) {
  4577. lines.push_back(line_text.substr(line_ranges[i].x, line_ranges[i].y - line_ranges[i].x));
  4578. }
  4579. return lines;
  4580. }
  4581. /* Viewport */
  4582. // Scrolling.
  4583. void TextEdit::set_smooth_scroll_enabled(const bool p_enabled) {
  4584. v_scroll->set_smooth_scroll_enabled(p_enabled);
  4585. smooth_scroll_enabled = p_enabled;
  4586. }
  4587. bool TextEdit::is_smooth_scroll_enabled() const {
  4588. return smooth_scroll_enabled;
  4589. }
  4590. void TextEdit::set_scroll_past_end_of_file_enabled(const bool p_enabled) {
  4591. if (scroll_past_end_of_file_enabled == p_enabled) {
  4592. return;
  4593. }
  4594. scroll_past_end_of_file_enabled = p_enabled;
  4595. queue_redraw();
  4596. }
  4597. bool TextEdit::is_scroll_past_end_of_file_enabled() const {
  4598. return scroll_past_end_of_file_enabled;
  4599. }
  4600. VScrollBar *TextEdit::get_v_scroll_bar() const {
  4601. return v_scroll;
  4602. }
  4603. HScrollBar *TextEdit::get_h_scroll_bar() const {
  4604. return h_scroll;
  4605. }
  4606. void TextEdit::set_v_scroll(double p_scroll) {
  4607. v_scroll->set_value(p_scroll);
  4608. int max_v_scroll = v_scroll->get_max() - v_scroll->get_page();
  4609. if (p_scroll >= max_v_scroll - 1.0) {
  4610. _scroll_moved(v_scroll->get_value());
  4611. }
  4612. }
  4613. double TextEdit::get_v_scroll() const {
  4614. return v_scroll->get_value();
  4615. }
  4616. void TextEdit::set_h_scroll(int p_scroll) {
  4617. if (p_scroll < 0) {
  4618. p_scroll = 0;
  4619. }
  4620. h_scroll->set_value(p_scroll);
  4621. }
  4622. int TextEdit::get_h_scroll() const {
  4623. return h_scroll->get_value();
  4624. }
  4625. void TextEdit::set_v_scroll_speed(float p_speed) {
  4626. // Prevent setting a vertical scroll speed value under 1.0
  4627. ERR_FAIL_COND(p_speed < 1.0);
  4628. v_scroll_speed = p_speed;
  4629. }
  4630. float TextEdit::get_v_scroll_speed() const {
  4631. return v_scroll_speed;
  4632. }
  4633. void TextEdit::set_fit_content_height_enabled(const bool p_enabled) {
  4634. if (fit_content_height == p_enabled) {
  4635. return;
  4636. }
  4637. fit_content_height = p_enabled;
  4638. update_minimum_size();
  4639. }
  4640. bool TextEdit::is_fit_content_height_enabled() const {
  4641. return fit_content_height;
  4642. }
  4643. double TextEdit::get_scroll_pos_for_line(int p_line, int p_wrap_index) const {
  4644. ERR_FAIL_INDEX_V(p_line, text.size(), 0);
  4645. ERR_FAIL_COND_V(p_wrap_index < 0, 0);
  4646. ERR_FAIL_COND_V(p_wrap_index > get_line_wrap_count(p_line), 0);
  4647. if (get_line_wrapping_mode() == LineWrappingMode::LINE_WRAPPING_NONE && !_is_hiding_enabled()) {
  4648. return p_line;
  4649. }
  4650. double new_line_scroll_pos = 0.0;
  4651. if (p_line > 0) {
  4652. new_line_scroll_pos = get_visible_line_count_in_range(0, MIN(p_line - 1, text.size() - 1));
  4653. }
  4654. new_line_scroll_pos += p_wrap_index;
  4655. return new_line_scroll_pos;
  4656. }
  4657. // Visible lines.
  4658. void TextEdit::set_line_as_first_visible(int p_line, int p_wrap_index) {
  4659. ERR_FAIL_INDEX(p_line, text.size());
  4660. ERR_FAIL_COND(p_wrap_index < 0);
  4661. ERR_FAIL_COND(p_wrap_index > get_line_wrap_count(p_line));
  4662. set_v_scroll(get_scroll_pos_for_line(p_line, p_wrap_index));
  4663. }
  4664. int TextEdit::get_first_visible_line() const {
  4665. return CLAMP(first_visible_line, 0, text.size() - 1);
  4666. }
  4667. void TextEdit::set_line_as_center_visible(int p_line, int p_wrap_index) {
  4668. ERR_FAIL_INDEX(p_line, text.size());
  4669. ERR_FAIL_COND(p_wrap_index < 0);
  4670. ERR_FAIL_COND(p_wrap_index > get_line_wrap_count(p_line));
  4671. int visible_rows = get_visible_line_count();
  4672. Point2i next_line = get_next_visible_line_index_offset_from(p_line, p_wrap_index, (-visible_rows / 2) - 1);
  4673. int first_line = p_line - next_line.x + 1;
  4674. if (first_line < 0) {
  4675. set_v_scroll(0);
  4676. return;
  4677. }
  4678. set_v_scroll(get_scroll_pos_for_line(first_line, next_line.y));
  4679. }
  4680. void TextEdit::set_line_as_last_visible(int p_line, int p_wrap_index) {
  4681. ERR_FAIL_INDEX(p_line, text.size());
  4682. ERR_FAIL_COND(p_wrap_index < 0);
  4683. ERR_FAIL_COND(p_wrap_index > get_line_wrap_count(p_line));
  4684. Point2i next_line = get_next_visible_line_index_offset_from(p_line, p_wrap_index, -get_visible_line_count() - 1);
  4685. int first_line = p_line - next_line.x + 1;
  4686. // Adding _get_visible_lines_offset is not 100% correct as we end up showing almost p_line + 1, however, it provides a
  4687. // better user experience. Therefore we need to special case < visible line count, else showing line 0 is impossible.
  4688. if (get_visible_line_count_in_range(0, p_line) < get_visible_line_count() + 1) {
  4689. set_v_scroll(0);
  4690. return;
  4691. }
  4692. set_v_scroll(get_scroll_pos_for_line(first_line, next_line.y) + _get_visible_lines_offset());
  4693. }
  4694. int TextEdit::get_last_full_visible_line() const {
  4695. int first_vis_line = get_first_visible_line();
  4696. int last_vis_line = 0;
  4697. last_vis_line = first_vis_line + get_next_visible_line_index_offset_from(first_vis_line, first_visible_line_wrap_ofs, get_visible_line_count()).x - 1;
  4698. last_vis_line = CLAMP(last_vis_line, 0, text.size() - 1);
  4699. return last_vis_line;
  4700. }
  4701. int TextEdit::get_last_full_visible_line_wrap_index() const {
  4702. int first_vis_line = get_first_visible_line();
  4703. return get_next_visible_line_index_offset_from(first_vis_line, first_visible_line_wrap_ofs, get_visible_line_count()).y;
  4704. }
  4705. int TextEdit::get_visible_line_count() const {
  4706. return _get_control_height() / get_line_height();
  4707. }
  4708. int TextEdit::get_visible_line_count_in_range(int p_from_line, int p_to_line) const {
  4709. ERR_FAIL_INDEX_V(p_from_line, text.size(), 0);
  4710. ERR_FAIL_INDEX_V(p_to_line, text.size(), 0);
  4711. // So we can handle inputs in whatever order
  4712. if (p_from_line > p_to_line) {
  4713. SWAP(p_from_line, p_to_line);
  4714. }
  4715. /* Returns the total number of (lines + wrapped - hidden). */
  4716. if (!_is_hiding_enabled() && get_line_wrapping_mode() == LineWrappingMode::LINE_WRAPPING_NONE) {
  4717. return (p_to_line - p_from_line) + 1;
  4718. }
  4719. int total_rows = 0;
  4720. for (int i = p_from_line; i <= p_to_line; i++) {
  4721. if (!text.is_hidden(i)) {
  4722. total_rows++;
  4723. total_rows += get_line_wrap_count(i);
  4724. }
  4725. }
  4726. return total_rows;
  4727. }
  4728. int TextEdit::get_total_visible_line_count() const {
  4729. return get_visible_line_count_in_range(0, text.size() - 1);
  4730. }
  4731. // Auto adjust
  4732. void TextEdit::adjust_viewport_to_caret(int p_caret) {
  4733. ERR_FAIL_INDEX(p_caret, carets.size());
  4734. // Make sure Caret is visible on the screen.
  4735. scrolling = false;
  4736. minimap_clicked = false;
  4737. int cur_line = get_caret_line(p_caret);
  4738. int cur_wrap = get_caret_wrap_index(p_caret);
  4739. int first_vis_line = get_first_visible_line();
  4740. int first_vis_wrap = first_visible_line_wrap_ofs;
  4741. int last_vis_line = get_last_full_visible_line();
  4742. int last_vis_wrap = get_last_full_visible_line_wrap_index();
  4743. if (cur_line < first_vis_line || (cur_line == first_vis_line && cur_wrap < first_vis_wrap)) {
  4744. // Caret is above screen.
  4745. set_line_as_first_visible(cur_line, cur_wrap);
  4746. } else if (cur_line > last_vis_line || (cur_line == last_vis_line && cur_wrap > last_vis_wrap)) {
  4747. // Caret is below screen.
  4748. set_line_as_last_visible(cur_line, cur_wrap);
  4749. }
  4750. int visible_width = get_size().width - style_normal->get_minimum_size().width - gutters_width - gutter_padding;
  4751. if (draw_minimap) {
  4752. visible_width -= minimap_width;
  4753. }
  4754. if (v_scroll->is_visible_in_tree()) {
  4755. visible_width -= v_scroll->get_combined_minimum_size().width;
  4756. }
  4757. visible_width -= 20; // Give it a little more space.
  4758. if (get_line_wrapping_mode() == LineWrappingMode::LINE_WRAPPING_NONE) {
  4759. // Adjust x offset.
  4760. Vector2i caret_pos;
  4761. // Get position of the start of caret.
  4762. if (ime_text.length() != 0 && ime_selection.x != 0) {
  4763. caret_pos.x = _get_column_x_offset_for_line(get_caret_column(p_caret) + ime_selection.x, get_caret_line(p_caret), get_caret_column(p_caret));
  4764. } else {
  4765. caret_pos.x = _get_column_x_offset_for_line(get_caret_column(p_caret), get_caret_line(p_caret), get_caret_column(p_caret));
  4766. }
  4767. // Get position of the end of caret.
  4768. if (ime_text.length() != 0) {
  4769. if (ime_selection.y != 0) {
  4770. caret_pos.y = _get_column_x_offset_for_line(get_caret_column(p_caret) + ime_selection.x + ime_selection.y, get_caret_line(p_caret), get_caret_column(p_caret));
  4771. } else {
  4772. caret_pos.y = _get_column_x_offset_for_line(get_caret_column(p_caret) + ime_text.size(), get_caret_line(p_caret), get_caret_column(p_caret));
  4773. }
  4774. } else {
  4775. caret_pos.y = caret_pos.x;
  4776. }
  4777. if (MAX(caret_pos.x, caret_pos.y) > (first_visible_col + visible_width)) {
  4778. first_visible_col = MAX(caret_pos.x, caret_pos.y) - visible_width + 1;
  4779. }
  4780. if (MIN(caret_pos.x, caret_pos.y) < first_visible_col) {
  4781. first_visible_col = MIN(caret_pos.x, caret_pos.y);
  4782. }
  4783. } else {
  4784. first_visible_col = 0;
  4785. }
  4786. h_scroll->set_value(first_visible_col);
  4787. queue_redraw();
  4788. }
  4789. void TextEdit::center_viewport_to_caret(int p_caret) {
  4790. ERR_FAIL_INDEX(p_caret, carets.size());
  4791. // Move viewport so the caret is in the center of the screen.
  4792. scrolling = false;
  4793. minimap_clicked = false;
  4794. set_line_as_center_visible(get_caret_line(p_caret), get_caret_wrap_index(p_caret));
  4795. int visible_width = get_size().width - style_normal->get_minimum_size().width - gutters_width - gutter_padding;
  4796. if (draw_minimap) {
  4797. visible_width -= minimap_width;
  4798. }
  4799. if (v_scroll->is_visible_in_tree()) {
  4800. visible_width -= v_scroll->get_combined_minimum_size().width;
  4801. }
  4802. visible_width -= 20; // Give it a little more space.
  4803. if (get_line_wrapping_mode() != LineWrappingMode::LINE_WRAPPING_NONE) {
  4804. // Center x offset.
  4805. Vector2i caret_pos;
  4806. // Get position of the start of caret.
  4807. if (ime_text.length() != 0 && ime_selection.x != 0) {
  4808. caret_pos.x = _get_column_x_offset_for_line(get_caret_column(p_caret) + ime_selection.x, get_caret_line(p_caret), get_caret_column(p_caret));
  4809. } else {
  4810. caret_pos.x = _get_column_x_offset_for_line(get_caret_column(p_caret), get_caret_line(p_caret), get_caret_column(p_caret));
  4811. }
  4812. // Get position of the end of caret.
  4813. if (ime_text.length() != 0) {
  4814. if (ime_selection.y != 0) {
  4815. caret_pos.y = _get_column_x_offset_for_line(get_caret_column(p_caret) + ime_selection.x + ime_selection.y, get_caret_line(p_caret), get_caret_column(p_caret));
  4816. } else {
  4817. caret_pos.y = _get_column_x_offset_for_line(get_caret_column(p_caret) + ime_text.size(), get_caret_line(p_caret), get_caret_column(p_caret));
  4818. }
  4819. } else {
  4820. caret_pos.y = caret_pos.x;
  4821. }
  4822. if (MAX(caret_pos.x, caret_pos.y) > (first_visible_col + visible_width)) {
  4823. first_visible_col = MAX(caret_pos.x, caret_pos.y) - visible_width + 1;
  4824. }
  4825. if (MIN(caret_pos.x, caret_pos.y) < first_visible_col) {
  4826. first_visible_col = MIN(caret_pos.x, caret_pos.y);
  4827. }
  4828. } else {
  4829. first_visible_col = 0;
  4830. }
  4831. h_scroll->set_value(first_visible_col);
  4832. queue_redraw();
  4833. }
  4834. /* Minimap */
  4835. void TextEdit::set_draw_minimap(bool p_enabled) {
  4836. if (draw_minimap == p_enabled) {
  4837. return;
  4838. }
  4839. draw_minimap = p_enabled;
  4840. _update_wrap_at_column();
  4841. queue_redraw();
  4842. }
  4843. bool TextEdit::is_drawing_minimap() const {
  4844. return draw_minimap;
  4845. }
  4846. void TextEdit::set_minimap_width(int p_minimap_width) {
  4847. if (minimap_width == p_minimap_width) {
  4848. return;
  4849. }
  4850. minimap_width = p_minimap_width;
  4851. _update_wrap_at_column();
  4852. queue_redraw();
  4853. }
  4854. int TextEdit::get_minimap_width() const {
  4855. return minimap_width;
  4856. }
  4857. int TextEdit::get_minimap_visible_lines() const {
  4858. return _get_control_height() / (minimap_char_size.y + minimap_line_spacing);
  4859. }
  4860. /* Gutters. */
  4861. void TextEdit::add_gutter(int p_at) {
  4862. if (p_at < 0 || p_at > gutters.size()) {
  4863. gutters.push_back(GutterInfo());
  4864. } else {
  4865. gutters.insert(p_at, GutterInfo());
  4866. }
  4867. text.add_gutter(p_at);
  4868. _update_gutter_width();
  4869. emit_signal(SNAME("gutter_added"));
  4870. queue_redraw();
  4871. }
  4872. void TextEdit::remove_gutter(int p_gutter) {
  4873. ERR_FAIL_INDEX(p_gutter, gutters.size());
  4874. gutters.remove_at(p_gutter);
  4875. text.remove_gutter(p_gutter);
  4876. _update_gutter_width();
  4877. emit_signal(SNAME("gutter_removed"));
  4878. queue_redraw();
  4879. }
  4880. int TextEdit::get_gutter_count() const {
  4881. return gutters.size();
  4882. }
  4883. void TextEdit::set_gutter_name(int p_gutter, const String &p_name) {
  4884. ERR_FAIL_INDEX(p_gutter, gutters.size());
  4885. gutters.write[p_gutter].name = p_name;
  4886. }
  4887. String TextEdit::get_gutter_name(int p_gutter) const {
  4888. ERR_FAIL_INDEX_V(p_gutter, gutters.size(), "");
  4889. return gutters[p_gutter].name;
  4890. }
  4891. void TextEdit::set_gutter_type(int p_gutter, GutterType p_type) {
  4892. ERR_FAIL_INDEX(p_gutter, gutters.size());
  4893. if (gutters[p_gutter].type == p_type) {
  4894. return;
  4895. }
  4896. gutters.write[p_gutter].type = p_type;
  4897. queue_redraw();
  4898. }
  4899. TextEdit::GutterType TextEdit::get_gutter_type(int p_gutter) const {
  4900. ERR_FAIL_INDEX_V(p_gutter, gutters.size(), GUTTER_TYPE_STRING);
  4901. return gutters[p_gutter].type;
  4902. }
  4903. void TextEdit::set_gutter_width(int p_gutter, int p_width) {
  4904. ERR_FAIL_INDEX(p_gutter, gutters.size());
  4905. if (gutters[p_gutter].width == p_width) {
  4906. return;
  4907. }
  4908. gutters.write[p_gutter].width = p_width;
  4909. _update_gutter_width();
  4910. }
  4911. int TextEdit::get_gutter_width(int p_gutter) const {
  4912. ERR_FAIL_INDEX_V(p_gutter, gutters.size(), -1);
  4913. return gutters[p_gutter].width;
  4914. }
  4915. int TextEdit::get_total_gutter_width() const {
  4916. return gutters_width + gutter_padding;
  4917. }
  4918. void TextEdit::set_gutter_draw(int p_gutter, bool p_draw) {
  4919. ERR_FAIL_INDEX(p_gutter, gutters.size());
  4920. if (gutters[p_gutter].draw == p_draw) {
  4921. return;
  4922. }
  4923. gutters.write[p_gutter].draw = p_draw;
  4924. _update_gutter_width();
  4925. }
  4926. bool TextEdit::is_gutter_drawn(int p_gutter) const {
  4927. ERR_FAIL_INDEX_V(p_gutter, gutters.size(), false);
  4928. return gutters[p_gutter].draw;
  4929. }
  4930. void TextEdit::set_gutter_clickable(int p_gutter, bool p_clickable) {
  4931. ERR_FAIL_INDEX(p_gutter, gutters.size());
  4932. if (gutters[p_gutter].clickable == p_clickable) {
  4933. return;
  4934. }
  4935. gutters.write[p_gutter].clickable = p_clickable;
  4936. queue_redraw();
  4937. }
  4938. bool TextEdit::is_gutter_clickable(int p_gutter) const {
  4939. ERR_FAIL_INDEX_V(p_gutter, gutters.size(), false);
  4940. return gutters[p_gutter].clickable;
  4941. }
  4942. void TextEdit::set_gutter_overwritable(int p_gutter, bool p_overwritable) {
  4943. ERR_FAIL_INDEX(p_gutter, gutters.size());
  4944. gutters.write[p_gutter].overwritable = p_overwritable;
  4945. }
  4946. bool TextEdit::is_gutter_overwritable(int p_gutter) const {
  4947. ERR_FAIL_INDEX_V(p_gutter, gutters.size(), false);
  4948. return gutters[p_gutter].overwritable;
  4949. }
  4950. void TextEdit::merge_gutters(int p_from_line, int p_to_line) {
  4951. ERR_FAIL_INDEX(p_from_line, text.size());
  4952. ERR_FAIL_INDEX(p_to_line, text.size());
  4953. if (p_from_line == p_to_line) {
  4954. return;
  4955. }
  4956. for (int i = 0; i < gutters.size(); i++) {
  4957. if (!gutters[i].overwritable) {
  4958. continue;
  4959. }
  4960. if (text.get_line_gutter_text(p_from_line, i) != "") {
  4961. text.set_line_gutter_text(p_to_line, i, text.get_line_gutter_text(p_from_line, i));
  4962. text.set_line_gutter_item_color(p_to_line, i, text.get_line_gutter_item_color(p_from_line, i));
  4963. }
  4964. if (text.get_line_gutter_icon(p_from_line, i).is_valid()) {
  4965. text.set_line_gutter_icon(p_to_line, i, text.get_line_gutter_icon(p_from_line, i));
  4966. text.set_line_gutter_item_color(p_to_line, i, text.get_line_gutter_item_color(p_from_line, i));
  4967. }
  4968. if (text.get_line_gutter_metadata(p_from_line, i) != "") {
  4969. text.set_line_gutter_metadata(p_to_line, i, text.get_line_gutter_metadata(p_from_line, i));
  4970. }
  4971. if (text.is_line_gutter_clickable(p_from_line, i)) {
  4972. text.set_line_gutter_clickable(p_to_line, i, true);
  4973. }
  4974. }
  4975. queue_redraw();
  4976. }
  4977. void TextEdit::set_gutter_custom_draw(int p_gutter, const Callable &p_draw_callback) {
  4978. ERR_FAIL_INDEX(p_gutter, gutters.size());
  4979. if (gutters[p_gutter].custom_draw_callback == p_draw_callback) {
  4980. return;
  4981. }
  4982. gutters.write[p_gutter].custom_draw_callback = p_draw_callback;
  4983. queue_redraw();
  4984. }
  4985. // Line gutters.
  4986. void TextEdit::set_line_gutter_metadata(int p_line, int p_gutter, const Variant &p_metadata) {
  4987. ERR_FAIL_INDEX(p_line, text.size());
  4988. ERR_FAIL_INDEX(p_gutter, gutters.size());
  4989. text.set_line_gutter_metadata(p_line, p_gutter, p_metadata);
  4990. }
  4991. Variant TextEdit::get_line_gutter_metadata(int p_line, int p_gutter) const {
  4992. ERR_FAIL_INDEX_V(p_line, text.size(), "");
  4993. ERR_FAIL_INDEX_V(p_gutter, gutters.size(), "");
  4994. return text.get_line_gutter_metadata(p_line, p_gutter);
  4995. }
  4996. void TextEdit::set_line_gutter_text(int p_line, int p_gutter, const String &p_text) {
  4997. ERR_FAIL_INDEX(p_line, text.size());
  4998. ERR_FAIL_INDEX(p_gutter, gutters.size());
  4999. if (text.get_line_gutter_text(p_line, p_gutter) == p_text) {
  5000. return;
  5001. }
  5002. text.set_line_gutter_text(p_line, p_gutter, p_text);
  5003. queue_redraw();
  5004. }
  5005. String TextEdit::get_line_gutter_text(int p_line, int p_gutter) const {
  5006. ERR_FAIL_INDEX_V(p_line, text.size(), "");
  5007. ERR_FAIL_INDEX_V(p_gutter, gutters.size(), "");
  5008. return text.get_line_gutter_text(p_line, p_gutter);
  5009. }
  5010. void TextEdit::set_line_gutter_icon(int p_line, int p_gutter, const Ref<Texture2D> &p_icon) {
  5011. ERR_FAIL_INDEX(p_line, text.size());
  5012. ERR_FAIL_INDEX(p_gutter, gutters.size());
  5013. if (text.get_line_gutter_icon(p_line, p_gutter) == p_icon) {
  5014. return;
  5015. }
  5016. text.set_line_gutter_icon(p_line, p_gutter, p_icon);
  5017. queue_redraw();
  5018. }
  5019. Ref<Texture2D> TextEdit::get_line_gutter_icon(int p_line, int p_gutter) const {
  5020. ERR_FAIL_INDEX_V(p_line, text.size(), Ref<Texture2D>());
  5021. ERR_FAIL_INDEX_V(p_gutter, gutters.size(), Ref<Texture2D>());
  5022. return text.get_line_gutter_icon(p_line, p_gutter);
  5023. }
  5024. void TextEdit::set_line_gutter_item_color(int p_line, int p_gutter, const Color &p_color) {
  5025. ERR_FAIL_INDEX(p_line, text.size());
  5026. ERR_FAIL_INDEX(p_gutter, gutters.size());
  5027. if (text.get_line_gutter_item_color(p_line, p_gutter) == p_color) {
  5028. return;
  5029. }
  5030. text.set_line_gutter_item_color(p_line, p_gutter, p_color);
  5031. queue_redraw();
  5032. }
  5033. Color TextEdit::get_line_gutter_item_color(int p_line, int p_gutter) const {
  5034. ERR_FAIL_INDEX_V(p_line, text.size(), Color());
  5035. ERR_FAIL_INDEX_V(p_gutter, gutters.size(), Color());
  5036. return text.get_line_gutter_item_color(p_line, p_gutter);
  5037. }
  5038. void TextEdit::set_line_gutter_clickable(int p_line, int p_gutter, bool p_clickable) {
  5039. ERR_FAIL_INDEX(p_line, text.size());
  5040. ERR_FAIL_INDEX(p_gutter, gutters.size());
  5041. text.set_line_gutter_clickable(p_line, p_gutter, p_clickable);
  5042. }
  5043. bool TextEdit::is_line_gutter_clickable(int p_line, int p_gutter) const {
  5044. ERR_FAIL_INDEX_V(p_line, text.size(), false);
  5045. ERR_FAIL_INDEX_V(p_gutter, gutters.size(), false);
  5046. return text.is_line_gutter_clickable(p_line, p_gutter);
  5047. }
  5048. // Line style
  5049. void TextEdit::set_line_background_color(int p_line, const Color &p_color) {
  5050. ERR_FAIL_INDEX(p_line, text.size());
  5051. if (text.get_line_background_color(p_line) == p_color) {
  5052. return;
  5053. }
  5054. text.set_line_background_color(p_line, p_color);
  5055. queue_redraw();
  5056. }
  5057. Color TextEdit::get_line_background_color(int p_line) const {
  5058. ERR_FAIL_INDEX_V(p_line, text.size(), Color());
  5059. return text.get_line_background_color(p_line);
  5060. }
  5061. /* Syntax Highlighting. */
  5062. void TextEdit::set_syntax_highlighter(Ref<SyntaxHighlighter> p_syntax_highlighter) {
  5063. if (syntax_highlighter == p_syntax_highlighter && syntax_highlighter.is_valid() == p_syntax_highlighter.is_valid()) {
  5064. return;
  5065. }
  5066. syntax_highlighter = p_syntax_highlighter;
  5067. if (syntax_highlighter.is_valid()) {
  5068. syntax_highlighter->set_text_edit(this);
  5069. }
  5070. queue_redraw();
  5071. }
  5072. Ref<SyntaxHighlighter> TextEdit::get_syntax_highlighter() const {
  5073. return syntax_highlighter;
  5074. }
  5075. /* Visual. */
  5076. void TextEdit::set_highlight_current_line(bool p_enabled) {
  5077. if (highlight_current_line == p_enabled) {
  5078. return;
  5079. }
  5080. highlight_current_line = p_enabled;
  5081. queue_redraw();
  5082. }
  5083. bool TextEdit::is_highlight_current_line_enabled() const {
  5084. return highlight_current_line;
  5085. }
  5086. void TextEdit::set_highlight_all_occurrences(const bool p_enabled) {
  5087. if (highlight_all_occurrences == p_enabled) {
  5088. return;
  5089. }
  5090. highlight_all_occurrences = p_enabled;
  5091. queue_redraw();
  5092. }
  5093. bool TextEdit::is_highlight_all_occurrences_enabled() const {
  5094. return highlight_all_occurrences;
  5095. }
  5096. void TextEdit::set_draw_control_chars(bool p_enabled) {
  5097. if (draw_control_chars != p_enabled) {
  5098. draw_control_chars = p_enabled;
  5099. if (menu) {
  5100. menu->set_item_checked(menu->get_item_index(MENU_DISPLAY_UCC), draw_control_chars);
  5101. }
  5102. text.set_draw_control_chars(draw_control_chars);
  5103. text.invalidate_font();
  5104. _update_placeholder();
  5105. queue_redraw();
  5106. }
  5107. }
  5108. bool TextEdit::get_draw_control_chars() const {
  5109. return draw_control_chars;
  5110. }
  5111. void TextEdit::set_draw_tabs(bool p_enabled) {
  5112. if (draw_tabs == p_enabled) {
  5113. return;
  5114. }
  5115. draw_tabs = p_enabled;
  5116. queue_redraw();
  5117. }
  5118. bool TextEdit::is_drawing_tabs() const {
  5119. return draw_tabs;
  5120. }
  5121. void TextEdit::set_draw_spaces(bool p_enabled) {
  5122. if (draw_spaces == p_enabled) {
  5123. return;
  5124. }
  5125. draw_spaces = p_enabled;
  5126. queue_redraw();
  5127. }
  5128. bool TextEdit::is_drawing_spaces() const {
  5129. return draw_spaces;
  5130. }
  5131. void TextEdit::_bind_methods() {
  5132. /*Internal. */
  5133. ClassDB::bind_method(D_METHOD("_text_changed_emit"), &TextEdit::_text_changed_emit);
  5134. /* Text */
  5135. // Text properties
  5136. ClassDB::bind_method(D_METHOD("has_ime_text"), &TextEdit::has_ime_text);
  5137. ClassDB::bind_method(D_METHOD("set_editable", "enabled"), &TextEdit::set_editable);
  5138. ClassDB::bind_method(D_METHOD("is_editable"), &TextEdit::is_editable);
  5139. ClassDB::bind_method(D_METHOD("set_text_direction", "direction"), &TextEdit::set_text_direction);
  5140. ClassDB::bind_method(D_METHOD("get_text_direction"), &TextEdit::get_text_direction);
  5141. ClassDB::bind_method(D_METHOD("set_language", "language"), &TextEdit::set_language);
  5142. ClassDB::bind_method(D_METHOD("get_language"), &TextEdit::get_language);
  5143. ClassDB::bind_method(D_METHOD("set_structured_text_bidi_override", "parser"), &TextEdit::set_structured_text_bidi_override);
  5144. ClassDB::bind_method(D_METHOD("get_structured_text_bidi_override"), &TextEdit::get_structured_text_bidi_override);
  5145. ClassDB::bind_method(D_METHOD("set_structured_text_bidi_override_options", "args"), &TextEdit::set_structured_text_bidi_override_options);
  5146. ClassDB::bind_method(D_METHOD("get_structured_text_bidi_override_options"), &TextEdit::get_structured_text_bidi_override_options);
  5147. ClassDB::bind_method(D_METHOD("set_tab_size", "size"), &TextEdit::set_tab_size);
  5148. ClassDB::bind_method(D_METHOD("get_tab_size"), &TextEdit::get_tab_size);
  5149. // User controls
  5150. ClassDB::bind_method(D_METHOD("set_overtype_mode_enabled", "enabled"), &TextEdit::set_overtype_mode_enabled);
  5151. ClassDB::bind_method(D_METHOD("is_overtype_mode_enabled"), &TextEdit::is_overtype_mode_enabled);
  5152. ClassDB::bind_method(D_METHOD("set_context_menu_enabled", "enabled"), &TextEdit::set_context_menu_enabled);
  5153. ClassDB::bind_method(D_METHOD("is_context_menu_enabled"), &TextEdit::is_context_menu_enabled);
  5154. ClassDB::bind_method(D_METHOD("set_shortcut_keys_enabled", "enabled"), &TextEdit::set_shortcut_keys_enabled);
  5155. ClassDB::bind_method(D_METHOD("is_shortcut_keys_enabled"), &TextEdit::is_shortcut_keys_enabled);
  5156. ClassDB::bind_method(D_METHOD("set_virtual_keyboard_enabled", "enabled"), &TextEdit::set_virtual_keyboard_enabled);
  5157. ClassDB::bind_method(D_METHOD("is_virtual_keyboard_enabled"), &TextEdit::is_virtual_keyboard_enabled);
  5158. ClassDB::bind_method(D_METHOD("set_middle_mouse_paste_enabled", "enabled"), &TextEdit::set_middle_mouse_paste_enabled);
  5159. ClassDB::bind_method(D_METHOD("is_middle_mouse_paste_enabled"), &TextEdit::is_middle_mouse_paste_enabled);
  5160. // Text manipulation
  5161. ClassDB::bind_method(D_METHOD("clear"), &TextEdit::clear);
  5162. ClassDB::bind_method(D_METHOD("set_text", "text"), &TextEdit::set_text);
  5163. ClassDB::bind_method(D_METHOD("get_text"), &TextEdit::get_text);
  5164. ClassDB::bind_method(D_METHOD("get_line_count"), &TextEdit::get_line_count);
  5165. ClassDB::bind_method(D_METHOD("set_placeholder", "text"), &TextEdit::set_placeholder);
  5166. ClassDB::bind_method(D_METHOD("get_placeholder"), &TextEdit::get_placeholder);
  5167. ClassDB::bind_method(D_METHOD("set_line", "line", "new_text"), &TextEdit::set_line);
  5168. ClassDB::bind_method(D_METHOD("get_line", "line"), &TextEdit::get_line);
  5169. ClassDB::bind_method(D_METHOD("get_line_width", "line", "wrap_index"), &TextEdit::get_line_width, DEFVAL(-1));
  5170. ClassDB::bind_method(D_METHOD("get_line_height"), &TextEdit::get_line_height);
  5171. ClassDB::bind_method(D_METHOD("get_indent_level", "line"), &TextEdit::get_indent_level);
  5172. ClassDB::bind_method(D_METHOD("get_first_non_whitespace_column", "line"), &TextEdit::get_first_non_whitespace_column);
  5173. ClassDB::bind_method(D_METHOD("swap_lines", "from_line", "to_line"), &TextEdit::swap_lines);
  5174. ClassDB::bind_method(D_METHOD("insert_line_at", "line", "text"), &TextEdit::insert_line_at);
  5175. ClassDB::bind_method(D_METHOD("insert_text_at_caret", "text", "caret_index"), &TextEdit::insert_text_at_caret, DEFVAL(-1));
  5176. ClassDB::bind_method(D_METHOD("remove_text", "from_line", "from_column", "to_line", "to_column"), &TextEdit::remove_text);
  5177. ClassDB::bind_method(D_METHOD("get_last_unhidden_line"), &TextEdit::get_last_unhidden_line);
  5178. ClassDB::bind_method(D_METHOD("get_next_visible_line_offset_from", "line", "visible_amount"), &TextEdit::get_next_visible_line_offset_from);
  5179. 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);
  5180. // Overridable actions
  5181. ClassDB::bind_method(D_METHOD("backspace", "caret_index"), &TextEdit::backspace, DEFVAL(-1));
  5182. ClassDB::bind_method(D_METHOD("cut", "caret_index"), &TextEdit::cut, DEFVAL(-1));
  5183. ClassDB::bind_method(D_METHOD("copy", "caret_index"), &TextEdit::copy, DEFVAL(-1));
  5184. ClassDB::bind_method(D_METHOD("paste", "caret_index"), &TextEdit::paste, DEFVAL(-1));
  5185. ClassDB::bind_method(D_METHOD("paste_primary_clipboard", "caret_index"), &TextEdit::paste_primary_clipboard, DEFVAL(-1));
  5186. GDVIRTUAL_BIND(_handle_unicode_input, "unicode_char", "caret_index")
  5187. GDVIRTUAL_BIND(_backspace, "caret_index")
  5188. GDVIRTUAL_BIND(_cut, "caret_index")
  5189. GDVIRTUAL_BIND(_copy, "caret_index")
  5190. GDVIRTUAL_BIND(_paste, "caret_index")
  5191. GDVIRTUAL_BIND(_paste_primary_clipboard, "caret_index")
  5192. // Context Menu
  5193. BIND_ENUM_CONSTANT(MENU_CUT);
  5194. BIND_ENUM_CONSTANT(MENU_COPY);
  5195. BIND_ENUM_CONSTANT(MENU_PASTE);
  5196. BIND_ENUM_CONSTANT(MENU_CLEAR);
  5197. BIND_ENUM_CONSTANT(MENU_SELECT_ALL);
  5198. BIND_ENUM_CONSTANT(MENU_UNDO);
  5199. BIND_ENUM_CONSTANT(MENU_REDO);
  5200. BIND_ENUM_CONSTANT(MENU_SUBMENU_TEXT_DIR);
  5201. BIND_ENUM_CONSTANT(MENU_DIR_INHERITED);
  5202. BIND_ENUM_CONSTANT(MENU_DIR_AUTO);
  5203. BIND_ENUM_CONSTANT(MENU_DIR_LTR);
  5204. BIND_ENUM_CONSTANT(MENU_DIR_RTL);
  5205. BIND_ENUM_CONSTANT(MENU_DISPLAY_UCC);
  5206. BIND_ENUM_CONSTANT(MENU_SUBMENU_INSERT_UCC);
  5207. BIND_ENUM_CONSTANT(MENU_INSERT_LRM);
  5208. BIND_ENUM_CONSTANT(MENU_INSERT_RLM);
  5209. BIND_ENUM_CONSTANT(MENU_INSERT_LRE);
  5210. BIND_ENUM_CONSTANT(MENU_INSERT_RLE);
  5211. BIND_ENUM_CONSTANT(MENU_INSERT_LRO);
  5212. BIND_ENUM_CONSTANT(MENU_INSERT_RLO);
  5213. BIND_ENUM_CONSTANT(MENU_INSERT_PDF);
  5214. BIND_ENUM_CONSTANT(MENU_INSERT_ALM);
  5215. BIND_ENUM_CONSTANT(MENU_INSERT_LRI);
  5216. BIND_ENUM_CONSTANT(MENU_INSERT_RLI);
  5217. BIND_ENUM_CONSTANT(MENU_INSERT_FSI);
  5218. BIND_ENUM_CONSTANT(MENU_INSERT_PDI);
  5219. BIND_ENUM_CONSTANT(MENU_INSERT_ZWJ);
  5220. BIND_ENUM_CONSTANT(MENU_INSERT_ZWNJ);
  5221. BIND_ENUM_CONSTANT(MENU_INSERT_WJ);
  5222. BIND_ENUM_CONSTANT(MENU_INSERT_SHY);
  5223. BIND_ENUM_CONSTANT(MENU_MAX);
  5224. /* Versioning */
  5225. BIND_ENUM_CONSTANT(ACTION_NONE);
  5226. BIND_ENUM_CONSTANT(ACTION_TYPING);
  5227. BIND_ENUM_CONSTANT(ACTION_BACKSPACE);
  5228. BIND_ENUM_CONSTANT(ACTION_DELETE);
  5229. ClassDB::bind_method(D_METHOD("start_action", "action"), &TextEdit::start_action);
  5230. ClassDB::bind_method(D_METHOD("end_action"), &TextEdit::end_complex_operation);
  5231. ClassDB::bind_method(D_METHOD("begin_complex_operation"), &TextEdit::begin_complex_operation);
  5232. ClassDB::bind_method(D_METHOD("end_complex_operation"), &TextEdit::end_complex_operation);
  5233. ClassDB::bind_method(D_METHOD("has_undo"), &TextEdit::has_undo);
  5234. ClassDB::bind_method(D_METHOD("has_redo"), &TextEdit::has_redo);
  5235. ClassDB::bind_method(D_METHOD("undo"), &TextEdit::undo);
  5236. ClassDB::bind_method(D_METHOD("redo"), &TextEdit::redo);
  5237. ClassDB::bind_method(D_METHOD("clear_undo_history"), &TextEdit::clear_undo_history);
  5238. ClassDB::bind_method(D_METHOD("tag_saved_version"), &TextEdit::tag_saved_version);
  5239. ClassDB::bind_method(D_METHOD("get_version"), &TextEdit::get_version);
  5240. ClassDB::bind_method(D_METHOD("get_saved_version"), &TextEdit::get_saved_version);
  5241. /* Search */
  5242. BIND_ENUM_CONSTANT(SEARCH_MATCH_CASE);
  5243. BIND_ENUM_CONSTANT(SEARCH_WHOLE_WORDS);
  5244. BIND_ENUM_CONSTANT(SEARCH_BACKWARDS);
  5245. ClassDB::bind_method(D_METHOD("set_search_text", "search_text"), &TextEdit::set_search_text);
  5246. ClassDB::bind_method(D_METHOD("set_search_flags", "flags"), &TextEdit::set_search_flags);
  5247. ClassDB::bind_method(D_METHOD("search", "text", "flags", "from_line", "from_colum"), &TextEdit::search);
  5248. /* Tooltip */
  5249. ClassDB::bind_method(D_METHOD("set_tooltip_request_func", "callback"), &TextEdit::set_tooltip_request_func);
  5250. /* Mouse */
  5251. ClassDB::bind_method(D_METHOD("get_local_mouse_pos"), &TextEdit::get_local_mouse_pos);
  5252. ClassDB::bind_method(D_METHOD("get_word_at_pos", "position"), &TextEdit::get_word_at_pos);
  5253. ClassDB::bind_method(D_METHOD("get_line_column_at_pos", "position", "allow_out_of_bounds"), &TextEdit::get_line_column_at_pos, DEFVAL(true));
  5254. ClassDB::bind_method(D_METHOD("get_pos_at_line_column", "line", "column"), &TextEdit::get_pos_at_line_column);
  5255. ClassDB::bind_method(D_METHOD("get_rect_at_line_column", "line", "column"), &TextEdit::get_rect_at_line_column);
  5256. ClassDB::bind_method(D_METHOD("get_minimap_line_at_pos", "position"), &TextEdit::get_minimap_line_at_pos);
  5257. ClassDB::bind_method(D_METHOD("is_dragging_cursor"), &TextEdit::is_dragging_cursor);
  5258. ClassDB::bind_method(D_METHOD("is_mouse_over_selection", "edges", "caret_index"), &TextEdit::is_mouse_over_selection, DEFVAL(-1));
  5259. /* Caret. */
  5260. BIND_ENUM_CONSTANT(CARET_TYPE_LINE);
  5261. BIND_ENUM_CONSTANT(CARET_TYPE_BLOCK);
  5262. // internal.
  5263. ClassDB::bind_method(D_METHOD("_emit_caret_changed"), &TextEdit::_emit_caret_changed);
  5264. ClassDB::bind_method(D_METHOD("set_caret_type", "type"), &TextEdit::set_caret_type);
  5265. ClassDB::bind_method(D_METHOD("get_caret_type"), &TextEdit::get_caret_type);
  5266. ClassDB::bind_method(D_METHOD("set_caret_blink_enabled", "enable"), &TextEdit::set_caret_blink_enabled);
  5267. ClassDB::bind_method(D_METHOD("is_caret_blink_enabled"), &TextEdit::is_caret_blink_enabled);
  5268. ClassDB::bind_method(D_METHOD("set_caret_blink_interval", "interval"), &TextEdit::set_caret_blink_interval);
  5269. ClassDB::bind_method(D_METHOD("get_caret_blink_interval"), &TextEdit::get_caret_blink_interval);
  5270. ClassDB::bind_method(D_METHOD("set_move_caret_on_right_click_enabled", "enable"), &TextEdit::set_move_caret_on_right_click_enabled);
  5271. ClassDB::bind_method(D_METHOD("is_move_caret_on_right_click_enabled"), &TextEdit::is_move_caret_on_right_click_enabled);
  5272. ClassDB::bind_method(D_METHOD("set_caret_mid_grapheme_enabled", "enabled"), &TextEdit::set_caret_mid_grapheme_enabled);
  5273. ClassDB::bind_method(D_METHOD("is_caret_mid_grapheme_enabled"), &TextEdit::is_caret_mid_grapheme_enabled);
  5274. ClassDB::bind_method(D_METHOD("set_multiple_carets_enabled", "enabled"), &TextEdit::set_multiple_carets_enabled);
  5275. ClassDB::bind_method(D_METHOD("is_multiple_carets_enabled"), &TextEdit::is_multiple_carets_enabled);
  5276. ClassDB::bind_method(D_METHOD("add_caret", "line", "col"), &TextEdit::add_caret);
  5277. ClassDB::bind_method(D_METHOD("remove_caret", "caret"), &TextEdit::remove_caret);
  5278. ClassDB::bind_method(D_METHOD("remove_secondary_carets"), &TextEdit::remove_secondary_carets);
  5279. ClassDB::bind_method(D_METHOD("merge_overlapping_carets"), &TextEdit::merge_overlapping_carets);
  5280. ClassDB::bind_method(D_METHOD("get_caret_count"), &TextEdit::get_caret_count);
  5281. ClassDB::bind_method(D_METHOD("add_caret_at_carets", "below"), &TextEdit::add_caret_at_carets);
  5282. ClassDB::bind_method(D_METHOD("get_caret_index_edit_order"), &TextEdit::get_caret_index_edit_order);
  5283. ClassDB::bind_method(D_METHOD("adjust_carets_after_edit", "caret", "from_line", "from_col", "to_line", "to_col"), &TextEdit::adjust_carets_after_edit);
  5284. ClassDB::bind_method(D_METHOD("is_caret_visible", "caret_index"), &TextEdit::is_caret_visible, DEFVAL(0));
  5285. ClassDB::bind_method(D_METHOD("get_caret_draw_pos", "caret_index"), &TextEdit::get_caret_draw_pos, DEFVAL(0));
  5286. ClassDB::bind_method(D_METHOD("set_caret_line", "line", "adjust_viewport", "can_be_hidden", "wrap_index", "caret_index"), &TextEdit::set_caret_line, DEFVAL(true), DEFVAL(true), DEFVAL(0), DEFVAL(0));
  5287. ClassDB::bind_method(D_METHOD("get_caret_line", "caret_index"), &TextEdit::get_caret_line, DEFVAL(0));
  5288. ClassDB::bind_method(D_METHOD("set_caret_column", "column", "adjust_viewport", "caret_index"), &TextEdit::set_caret_column, DEFVAL(true), DEFVAL(0));
  5289. ClassDB::bind_method(D_METHOD("get_caret_column", "caret_index"), &TextEdit::get_caret_column, DEFVAL(0));
  5290. ClassDB::bind_method(D_METHOD("get_caret_wrap_index", "caret_index"), &TextEdit::get_caret_wrap_index, DEFVAL(0));
  5291. ClassDB::bind_method(D_METHOD("get_word_under_caret", "caret_index"), &TextEdit::get_word_under_caret, DEFVAL(-1));
  5292. /* Selection. */
  5293. BIND_ENUM_CONSTANT(SELECTION_MODE_NONE);
  5294. BIND_ENUM_CONSTANT(SELECTION_MODE_SHIFT);
  5295. BIND_ENUM_CONSTANT(SELECTION_MODE_POINTER);
  5296. BIND_ENUM_CONSTANT(SELECTION_MODE_WORD);
  5297. BIND_ENUM_CONSTANT(SELECTION_MODE_LINE);
  5298. ClassDB::bind_method(D_METHOD("set_selecting_enabled", "enable"), &TextEdit::set_selecting_enabled);
  5299. ClassDB::bind_method(D_METHOD("is_selecting_enabled"), &TextEdit::is_selecting_enabled);
  5300. ClassDB::bind_method(D_METHOD("set_deselect_on_focus_loss_enabled", "enable"), &TextEdit::set_deselect_on_focus_loss_enabled);
  5301. ClassDB::bind_method(D_METHOD("is_deselect_on_focus_loss_enabled"), &TextEdit::is_deselect_on_focus_loss_enabled);
  5302. ClassDB::bind_method(D_METHOD("set_drag_and_drop_selection_enabled", "enable"), &TextEdit::set_drag_and_drop_selection_enabled);
  5303. ClassDB::bind_method(D_METHOD("is_drag_and_drop_selection_enabled"), &TextEdit::is_drag_and_drop_selection_enabled);
  5304. ClassDB::bind_method(D_METHOD("set_selection_mode", "mode", "line", "column", "caret_index"), &TextEdit::set_selection_mode, DEFVAL(-1), DEFVAL(-1), DEFVAL(0));
  5305. ClassDB::bind_method(D_METHOD("get_selection_mode"), &TextEdit::get_selection_mode);
  5306. ClassDB::bind_method(D_METHOD("select_all"), &TextEdit::select_all);
  5307. ClassDB::bind_method(D_METHOD("select_word_under_caret", "caret_index"), &TextEdit::select_word_under_caret, DEFVAL(-1));
  5308. ClassDB::bind_method(D_METHOD("add_selection_for_next_occurrence"), &TextEdit::add_selection_for_next_occurrence);
  5309. ClassDB::bind_method(D_METHOD("select", "from_line", "from_column", "to_line", "to_column", "caret_index"), &TextEdit::select, DEFVAL(0));
  5310. ClassDB::bind_method(D_METHOD("has_selection", "caret_index"), &TextEdit::has_selection, DEFVAL(-1));
  5311. ClassDB::bind_method(D_METHOD("get_selected_text", "caret_index"), &TextEdit::get_selected_text, DEFVAL(-1));
  5312. ClassDB::bind_method(D_METHOD("get_selection_line", "caret_index"), &TextEdit::get_selection_line, DEFVAL(0));
  5313. ClassDB::bind_method(D_METHOD("get_selection_column", "caret_index"), &TextEdit::get_selection_column, DEFVAL(0));
  5314. ClassDB::bind_method(D_METHOD("get_selection_from_line", "caret_index"), &TextEdit::get_selection_from_line, DEFVAL(0));
  5315. ClassDB::bind_method(D_METHOD("get_selection_from_column", "caret_index"), &TextEdit::get_selection_from_column, DEFVAL(0));
  5316. ClassDB::bind_method(D_METHOD("get_selection_to_line", "caret_index"), &TextEdit::get_selection_to_line, DEFVAL(0));
  5317. ClassDB::bind_method(D_METHOD("get_selection_to_column", "caret_index"), &TextEdit::get_selection_to_column, DEFVAL(0));
  5318. ClassDB::bind_method(D_METHOD("deselect", "caret_index"), &TextEdit::deselect, DEFVAL(-1));
  5319. ClassDB::bind_method(D_METHOD("delete_selection", "caret_index"), &TextEdit::delete_selection, DEFVAL(-1));
  5320. /* Line wrapping. */
  5321. BIND_ENUM_CONSTANT(LINE_WRAPPING_NONE);
  5322. BIND_ENUM_CONSTANT(LINE_WRAPPING_BOUNDARY);
  5323. // internal.
  5324. ClassDB::bind_method(D_METHOD("_update_wrap_at_column", "force"), &TextEdit::_update_wrap_at_column, DEFVAL(false));
  5325. ClassDB::bind_method(D_METHOD("set_line_wrapping_mode", "mode"), &TextEdit::set_line_wrapping_mode);
  5326. ClassDB::bind_method(D_METHOD("get_line_wrapping_mode"), &TextEdit::get_line_wrapping_mode);
  5327. ClassDB::bind_method(D_METHOD("is_line_wrapped", "line"), &TextEdit::is_line_wrapped);
  5328. ClassDB::bind_method(D_METHOD("get_line_wrap_count", "line"), &TextEdit::get_line_wrap_count);
  5329. ClassDB::bind_method(D_METHOD("get_line_wrap_index_at_column", "line", "column"), &TextEdit::get_line_wrap_index_at_column);
  5330. ClassDB::bind_method(D_METHOD("get_line_wrapped_text", "line"), &TextEdit::get_line_wrapped_text);
  5331. /* Viewport. */
  5332. // Scrolling.
  5333. ClassDB::bind_method(D_METHOD("set_smooth_scroll_enabled", "enable"), &TextEdit::set_smooth_scroll_enabled);
  5334. ClassDB::bind_method(D_METHOD("is_smooth_scroll_enabled"), &TextEdit::is_smooth_scroll_enabled);
  5335. ClassDB::bind_method(D_METHOD("get_v_scroll_bar"), &TextEdit::get_v_scroll_bar);
  5336. ClassDB::bind_method(D_METHOD("get_h_scroll_bar"), &TextEdit::get_h_scroll_bar);
  5337. ClassDB::bind_method(D_METHOD("set_v_scroll", "value"), &TextEdit::set_v_scroll);
  5338. ClassDB::bind_method(D_METHOD("get_v_scroll"), &TextEdit::get_v_scroll);
  5339. ClassDB::bind_method(D_METHOD("set_h_scroll", "value"), &TextEdit::set_h_scroll);
  5340. ClassDB::bind_method(D_METHOD("get_h_scroll"), &TextEdit::get_h_scroll);
  5341. ClassDB::bind_method(D_METHOD("set_scroll_past_end_of_file_enabled", "enable"), &TextEdit::set_scroll_past_end_of_file_enabled);
  5342. ClassDB::bind_method(D_METHOD("is_scroll_past_end_of_file_enabled"), &TextEdit::is_scroll_past_end_of_file_enabled);
  5343. ClassDB::bind_method(D_METHOD("set_v_scroll_speed", "speed"), &TextEdit::set_v_scroll_speed);
  5344. ClassDB::bind_method(D_METHOD("get_v_scroll_speed"), &TextEdit::get_v_scroll_speed);
  5345. ClassDB::bind_method(D_METHOD("set_fit_content_height_enabled", "enabled"), &TextEdit::set_fit_content_height_enabled);
  5346. ClassDB::bind_method(D_METHOD("is_fit_content_height_enabled"), &TextEdit::is_fit_content_height_enabled);
  5347. ClassDB::bind_method(D_METHOD("get_scroll_pos_for_line", "line", "wrap_index"), &TextEdit::get_scroll_pos_for_line, DEFVAL(0));
  5348. // Visible lines.
  5349. ClassDB::bind_method(D_METHOD("set_line_as_first_visible", "line", "wrap_index"), &TextEdit::set_line_as_first_visible, DEFVAL(0));
  5350. ClassDB::bind_method(D_METHOD("get_first_visible_line"), &TextEdit::get_first_visible_line);
  5351. ClassDB::bind_method(D_METHOD("set_line_as_center_visible", "line", "wrap_index"), &TextEdit::set_line_as_center_visible, DEFVAL(0));
  5352. ClassDB::bind_method(D_METHOD("set_line_as_last_visible", "line", "wrap_index"), &TextEdit::set_line_as_last_visible, DEFVAL(0));
  5353. ClassDB::bind_method(D_METHOD("get_last_full_visible_line"), &TextEdit::get_last_full_visible_line);
  5354. ClassDB::bind_method(D_METHOD("get_last_full_visible_line_wrap_index"), &TextEdit::get_last_full_visible_line_wrap_index);
  5355. ClassDB::bind_method(D_METHOD("get_visible_line_count"), &TextEdit::get_visible_line_count);
  5356. ClassDB::bind_method(D_METHOD("get_visible_line_count_in_range", "from_line", "to_line"), &TextEdit::get_visible_line_count_in_range);
  5357. ClassDB::bind_method(D_METHOD("get_total_visible_line_count"), &TextEdit::get_total_visible_line_count);
  5358. // Auto adjust
  5359. ClassDB::bind_method(D_METHOD("adjust_viewport_to_caret", "caret_index"), &TextEdit::adjust_viewport_to_caret, DEFVAL(0));
  5360. ClassDB::bind_method(D_METHOD("center_viewport_to_caret", "caret_index"), &TextEdit::center_viewport_to_caret, DEFVAL(0));
  5361. // Minimap
  5362. ClassDB::bind_method(D_METHOD("set_draw_minimap", "enabled"), &TextEdit::set_draw_minimap);
  5363. ClassDB::bind_method(D_METHOD("is_drawing_minimap"), &TextEdit::is_drawing_minimap);
  5364. ClassDB::bind_method(D_METHOD("set_minimap_width", "width"), &TextEdit::set_minimap_width);
  5365. ClassDB::bind_method(D_METHOD("get_minimap_width"), &TextEdit::get_minimap_width);
  5366. ClassDB::bind_method(D_METHOD("get_minimap_visible_lines"), &TextEdit::get_minimap_visible_lines);
  5367. /* Gutters. */
  5368. BIND_ENUM_CONSTANT(GUTTER_TYPE_STRING);
  5369. BIND_ENUM_CONSTANT(GUTTER_TYPE_ICON);
  5370. BIND_ENUM_CONSTANT(GUTTER_TYPE_CUSTOM);
  5371. ClassDB::bind_method(D_METHOD("add_gutter", "at"), &TextEdit::add_gutter, DEFVAL(-1));
  5372. ClassDB::bind_method(D_METHOD("remove_gutter", "gutter"), &TextEdit::remove_gutter);
  5373. ClassDB::bind_method(D_METHOD("get_gutter_count"), &TextEdit::get_gutter_count);
  5374. ClassDB::bind_method(D_METHOD("set_gutter_name", "gutter", "name"), &TextEdit::set_gutter_name);
  5375. ClassDB::bind_method(D_METHOD("get_gutter_name", "gutter"), &TextEdit::get_gutter_name);
  5376. ClassDB::bind_method(D_METHOD("set_gutter_type", "gutter", "type"), &TextEdit::set_gutter_type);
  5377. ClassDB::bind_method(D_METHOD("get_gutter_type", "gutter"), &TextEdit::get_gutter_type);
  5378. ClassDB::bind_method(D_METHOD("set_gutter_width", "gutter", "width"), &TextEdit::set_gutter_width);
  5379. ClassDB::bind_method(D_METHOD("get_gutter_width", "gutter"), &TextEdit::get_gutter_width);
  5380. ClassDB::bind_method(D_METHOD("set_gutter_draw", "gutter", "draw"), &TextEdit::set_gutter_draw);
  5381. ClassDB::bind_method(D_METHOD("is_gutter_drawn", "gutter"), &TextEdit::is_gutter_drawn);
  5382. ClassDB::bind_method(D_METHOD("set_gutter_clickable", "gutter", "clickable"), &TextEdit::set_gutter_clickable);
  5383. ClassDB::bind_method(D_METHOD("is_gutter_clickable", "gutter"), &TextEdit::is_gutter_clickable);
  5384. ClassDB::bind_method(D_METHOD("set_gutter_overwritable", "gutter", "overwritable"), &TextEdit::set_gutter_overwritable);
  5385. ClassDB::bind_method(D_METHOD("is_gutter_overwritable", "gutter"), &TextEdit::is_gutter_overwritable);
  5386. ClassDB::bind_method(D_METHOD("merge_gutters", "from_line", "to_line"), &TextEdit::merge_gutters);
  5387. ClassDB::bind_method(D_METHOD("set_gutter_custom_draw", "column", "draw_callback"), &TextEdit::set_gutter_custom_draw);
  5388. ClassDB::bind_method(D_METHOD("get_total_gutter_width"), &TextEdit::get_total_gutter_width);
  5389. // Line gutters.
  5390. ClassDB::bind_method(D_METHOD("set_line_gutter_metadata", "line", "gutter", "metadata"), &TextEdit::set_line_gutter_metadata);
  5391. ClassDB::bind_method(D_METHOD("get_line_gutter_metadata", "line", "gutter"), &TextEdit::get_line_gutter_metadata);
  5392. ClassDB::bind_method(D_METHOD("set_line_gutter_text", "line", "gutter", "text"), &TextEdit::set_line_gutter_text);
  5393. ClassDB::bind_method(D_METHOD("get_line_gutter_text", "line", "gutter"), &TextEdit::get_line_gutter_text);
  5394. ClassDB::bind_method(D_METHOD("set_line_gutter_icon", "line", "gutter", "icon"), &TextEdit::set_line_gutter_icon);
  5395. ClassDB::bind_method(D_METHOD("get_line_gutter_icon", "line", "gutter"), &TextEdit::get_line_gutter_icon);
  5396. ClassDB::bind_method(D_METHOD("set_line_gutter_item_color", "line", "gutter", "color"), &TextEdit::set_line_gutter_item_color);
  5397. ClassDB::bind_method(D_METHOD("get_line_gutter_item_color", "line", "gutter"), &TextEdit::get_line_gutter_item_color);
  5398. ClassDB::bind_method(D_METHOD("set_line_gutter_clickable", "line", "gutter", "clickable"), &TextEdit::set_line_gutter_clickable);
  5399. ClassDB::bind_method(D_METHOD("is_line_gutter_clickable", "line", "gutter"), &TextEdit::is_line_gutter_clickable);
  5400. // Line style
  5401. ClassDB::bind_method(D_METHOD("set_line_background_color", "line", "color"), &TextEdit::set_line_background_color);
  5402. ClassDB::bind_method(D_METHOD("get_line_background_color", "line"), &TextEdit::get_line_background_color);
  5403. /* Syntax Highlighting. */
  5404. ClassDB::bind_method(D_METHOD("set_syntax_highlighter", "syntax_highlighter"), &TextEdit::set_syntax_highlighter);
  5405. ClassDB::bind_method(D_METHOD("get_syntax_highlighter"), &TextEdit::get_syntax_highlighter);
  5406. /* Visual. */
  5407. ClassDB::bind_method(D_METHOD("set_highlight_current_line", "enabled"), &TextEdit::set_highlight_current_line);
  5408. ClassDB::bind_method(D_METHOD("is_highlight_current_line_enabled"), &TextEdit::is_highlight_current_line_enabled);
  5409. ClassDB::bind_method(D_METHOD("set_highlight_all_occurrences", "enabled"), &TextEdit::set_highlight_all_occurrences);
  5410. ClassDB::bind_method(D_METHOD("is_highlight_all_occurrences_enabled"), &TextEdit::is_highlight_all_occurrences_enabled);
  5411. ClassDB::bind_method(D_METHOD("get_draw_control_chars"), &TextEdit::get_draw_control_chars);
  5412. ClassDB::bind_method(D_METHOD("set_draw_control_chars", "enabled"), &TextEdit::set_draw_control_chars);
  5413. ClassDB::bind_method(D_METHOD("set_draw_tabs", "enabled"), &TextEdit::set_draw_tabs);
  5414. ClassDB::bind_method(D_METHOD("is_drawing_tabs"), &TextEdit::is_drawing_tabs);
  5415. ClassDB::bind_method(D_METHOD("set_draw_spaces", "enabled"), &TextEdit::set_draw_spaces);
  5416. ClassDB::bind_method(D_METHOD("is_drawing_spaces"), &TextEdit::is_drawing_spaces);
  5417. ClassDB::bind_method(D_METHOD("get_menu"), &TextEdit::get_menu);
  5418. ClassDB::bind_method(D_METHOD("is_menu_visible"), &TextEdit::is_menu_visible);
  5419. ClassDB::bind_method(D_METHOD("menu_option", "option"), &TextEdit::menu_option);
  5420. /* Inspector */
  5421. ADD_PROPERTY(PropertyInfo(Variant::STRING, "text", PROPERTY_HINT_MULTILINE_TEXT), "set_text", "get_text");
  5422. ADD_PROPERTY(PropertyInfo(Variant::STRING, "placeholder_text", PROPERTY_HINT_MULTILINE_TEXT), "set_placeholder", "get_placeholder");
  5423. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "editable"), "set_editable", "is_editable");
  5424. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "context_menu_enabled"), "set_context_menu_enabled", "is_context_menu_enabled");
  5425. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "shortcut_keys_enabled"), "set_shortcut_keys_enabled", "is_shortcut_keys_enabled");
  5426. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "selecting_enabled"), "set_selecting_enabled", "is_selecting_enabled");
  5427. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "deselect_on_focus_loss_enabled"), "set_deselect_on_focus_loss_enabled", "is_deselect_on_focus_loss_enabled");
  5428. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "drag_and_drop_selection_enabled"), "set_drag_and_drop_selection_enabled", "is_drag_and_drop_selection_enabled");
  5429. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "virtual_keyboard_enabled"), "set_virtual_keyboard_enabled", "is_virtual_keyboard_enabled");
  5430. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "middle_mouse_paste_enabled"), "set_middle_mouse_paste_enabled", "is_middle_mouse_paste_enabled");
  5431. ADD_PROPERTY(PropertyInfo(Variant::INT, "wrap_mode", PROPERTY_HINT_ENUM, "None,Boundary"), "set_line_wrapping_mode", "get_line_wrapping_mode");
  5432. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "highlight_all_occurrences"), "set_highlight_all_occurrences", "is_highlight_all_occurrences_enabled");
  5433. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "highlight_current_line"), "set_highlight_current_line", "is_highlight_current_line_enabled");
  5434. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "draw_control_chars"), "set_draw_control_chars", "get_draw_control_chars");
  5435. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "draw_tabs"), "set_draw_tabs", "is_drawing_tabs");
  5436. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "draw_spaces"), "set_draw_spaces", "is_drawing_spaces");
  5437. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "syntax_highlighter", PROPERTY_HINT_RESOURCE_TYPE, "SyntaxHighlighter", PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_ALWAYS_DUPLICATE), "set_syntax_highlighter", "get_syntax_highlighter");
  5438. ADD_GROUP("Scroll", "scroll_");
  5439. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "scroll_smooth"), "set_smooth_scroll_enabled", "is_smooth_scroll_enabled");
  5440. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "scroll_v_scroll_speed", PROPERTY_HINT_NONE, "suffix:px/s"), "set_v_scroll_speed", "get_v_scroll_speed");
  5441. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "scroll_past_end_of_file"), "set_scroll_past_end_of_file_enabled", "is_scroll_past_end_of_file_enabled");
  5442. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "scroll_vertical", PROPERTY_HINT_NONE, "suffix:px"), "set_v_scroll", "get_v_scroll");
  5443. ADD_PROPERTY(PropertyInfo(Variant::INT, "scroll_horizontal", PROPERTY_HINT_NONE, "suffix:px"), "set_h_scroll", "get_h_scroll");
  5444. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "scroll_fit_content_height"), "set_fit_content_height_enabled", "is_fit_content_height_enabled");
  5445. ADD_GROUP("Minimap", "minimap_");
  5446. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "minimap_draw"), "set_draw_minimap", "is_drawing_minimap");
  5447. ADD_PROPERTY(PropertyInfo(Variant::INT, "minimap_width", PROPERTY_HINT_NONE, "suffix:px"), "set_minimap_width", "get_minimap_width");
  5448. ADD_GROUP("Caret", "caret_");
  5449. ADD_PROPERTY(PropertyInfo(Variant::INT, "caret_type", PROPERTY_HINT_ENUM, "Line,Block"), "set_caret_type", "get_caret_type");
  5450. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "caret_blink"), "set_caret_blink_enabled", "is_caret_blink_enabled");
  5451. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "caret_blink_interval", PROPERTY_HINT_RANGE, "0.1,10,0.01,suffix:s"), "set_caret_blink_interval", "get_caret_blink_interval");
  5452. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "caret_move_on_right_click"), "set_move_caret_on_right_click_enabled", "is_move_caret_on_right_click_enabled");
  5453. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "caret_mid_grapheme"), "set_caret_mid_grapheme_enabled", "is_caret_mid_grapheme_enabled");
  5454. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "caret_multiple"), "set_multiple_carets_enabled", "is_multiple_carets_enabled");
  5455. ADD_GROUP("BiDi", "");
  5456. ADD_PROPERTY(PropertyInfo(Variant::INT, "text_direction", PROPERTY_HINT_ENUM, "Auto,Left-to-Right,Right-to-Left,Inherited"), "set_text_direction", "get_text_direction");
  5457. ADD_PROPERTY(PropertyInfo(Variant::STRING, "language", PROPERTY_HINT_LOCALE_ID, ""), "set_language", "get_language");
  5458. 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");
  5459. ADD_PROPERTY(PropertyInfo(Variant::ARRAY, "structured_text_bidi_override_options"), "set_structured_text_bidi_override_options", "get_structured_text_bidi_override_options");
  5460. /* Signals */
  5461. /* Core. */
  5462. ADD_SIGNAL(MethodInfo("text_set"));
  5463. ADD_SIGNAL(MethodInfo("text_changed"));
  5464. ADD_SIGNAL(MethodInfo("lines_edited_from", PropertyInfo(Variant::INT, "from_line"), PropertyInfo(Variant::INT, "to_line")));
  5465. /* Caret. */
  5466. ADD_SIGNAL(MethodInfo("caret_changed"));
  5467. /* Gutters. */
  5468. ADD_SIGNAL(MethodInfo("gutter_clicked", PropertyInfo(Variant::INT, "line"), PropertyInfo(Variant::INT, "gutter")));
  5469. ADD_SIGNAL(MethodInfo("gutter_added"));
  5470. ADD_SIGNAL(MethodInfo("gutter_removed"));
  5471. /* Settings. */
  5472. GLOBAL_DEF(PropertyInfo(Variant::FLOAT, "gui/timers/text_edit_idle_detect_sec", PROPERTY_HINT_RANGE, "0,10,0.01,or_greater"), 3);
  5473. GLOBAL_DEF(PropertyInfo(Variant::INT, "gui/common/text_edit_undo_stack_max_size", PROPERTY_HINT_RANGE, "0,10000,1,or_greater"), 1024);
  5474. }
  5475. /* Internal API for CodeEdit. */
  5476. // Line hiding.
  5477. void TextEdit::_set_hiding_enabled(bool p_enabled) {
  5478. if (hiding_enabled == p_enabled) {
  5479. return;
  5480. }
  5481. if (!p_enabled) {
  5482. _unhide_all_lines();
  5483. }
  5484. hiding_enabled = p_enabled;
  5485. queue_redraw();
  5486. }
  5487. bool TextEdit::_is_hiding_enabled() const {
  5488. return hiding_enabled;
  5489. }
  5490. bool TextEdit::_is_line_hidden(int p_line) const {
  5491. ERR_FAIL_INDEX_V(p_line, text.size(), false);
  5492. return text.is_hidden(p_line);
  5493. }
  5494. void TextEdit::_unhide_all_lines() {
  5495. for (int i = 0; i < text.size(); i++) {
  5496. text.set_hidden(i, false);
  5497. }
  5498. _update_scrollbars();
  5499. queue_redraw();
  5500. }
  5501. void TextEdit::_set_line_as_hidden(int p_line, bool p_hidden) {
  5502. ERR_FAIL_INDEX(p_line, text.size());
  5503. if (text.is_hidden(p_line) == p_hidden) {
  5504. return;
  5505. }
  5506. if (_is_hiding_enabled() || !p_hidden) {
  5507. text.set_hidden(p_line, p_hidden);
  5508. }
  5509. queue_redraw();
  5510. }
  5511. // Symbol lookup.
  5512. void TextEdit::_set_symbol_lookup_word(const String &p_symbol) {
  5513. if (lookup_symbol_word == p_symbol) {
  5514. return;
  5515. }
  5516. lookup_symbol_word = p_symbol;
  5517. queue_redraw();
  5518. }
  5519. /* Text manipulation */
  5520. // Overridable actions
  5521. void TextEdit::_handle_unicode_input_internal(const uint32_t p_unicode, int p_caret) {
  5522. ERR_FAIL_COND(p_caret > carets.size());
  5523. if (!editable) {
  5524. return;
  5525. }
  5526. start_action(EditAction::ACTION_TYPING);
  5527. Vector<int> caret_edit_order = get_caret_index_edit_order();
  5528. for (const int &i : caret_edit_order) {
  5529. if (p_caret != -1 && p_caret != i) {
  5530. continue;
  5531. }
  5532. /* Remove the old character if in insert mode and no selection. */
  5533. if (overtype_mode && !has_selection(i)) {
  5534. /* Make sure we don't try and remove empty space. */
  5535. int cl = get_caret_line(i);
  5536. int cc = get_caret_column(i);
  5537. if (cc < get_line(cl).length()) {
  5538. _remove_text(cl, cc, cl, cc + 1);
  5539. }
  5540. }
  5541. const char32_t chr[2] = { (char32_t)p_unicode, 0 };
  5542. insert_text_at_caret(chr, i);
  5543. }
  5544. end_action();
  5545. }
  5546. void TextEdit::_backspace_internal(int p_caret) {
  5547. ERR_FAIL_COND(p_caret > carets.size());
  5548. if (!editable) {
  5549. return;
  5550. }
  5551. if (has_selection(p_caret)) {
  5552. delete_selection(p_caret);
  5553. return;
  5554. }
  5555. begin_complex_operation();
  5556. Vector<int> caret_edit_order = get_caret_index_edit_order();
  5557. for (const int &i : caret_edit_order) {
  5558. if (p_caret != -1 && p_caret != i) {
  5559. continue;
  5560. }
  5561. int cc = get_caret_column(i);
  5562. int cl = get_caret_line(i);
  5563. if (cc == 0 && cl == 0) {
  5564. continue;
  5565. }
  5566. int prev_line = cc ? cl : cl - 1;
  5567. int prev_column = cc ? (cc - 1) : (text[cl - 1].length());
  5568. merge_gutters(prev_line, cl);
  5569. if (_is_line_hidden(cl)) {
  5570. _set_line_as_hidden(prev_line, true);
  5571. }
  5572. _remove_text(prev_line, prev_column, cl, cc);
  5573. set_caret_line(prev_line, false, true, 0, i);
  5574. set_caret_column(prev_column, i == 0, i);
  5575. adjust_carets_after_edit(i, prev_line, prev_column, cl, cc);
  5576. }
  5577. merge_overlapping_carets();
  5578. end_complex_operation();
  5579. }
  5580. void TextEdit::_cut_internal(int p_caret) {
  5581. ERR_FAIL_COND(p_caret > carets.size());
  5582. if (!editable) {
  5583. return;
  5584. }
  5585. if (has_selection(p_caret)) {
  5586. DisplayServer::get_singleton()->clipboard_set(get_selected_text(p_caret));
  5587. delete_selection(p_caret);
  5588. cut_copy_line = "";
  5589. return;
  5590. }
  5591. begin_complex_operation();
  5592. Vector<int> carets_to_remove;
  5593. StringBuilder clipboard;
  5594. // This is the exception and has to edit in reverse order else the string copied to the clipboard will be backwards.
  5595. Vector<int> caret_edit_order = get_caret_index_edit_order();
  5596. for (int i = caret_edit_order.size() - 1; i >= 0; i--) {
  5597. int caret_idx = caret_edit_order[i];
  5598. if (p_caret != -1 && p_caret != caret_idx) {
  5599. continue;
  5600. }
  5601. int cl = get_caret_line(caret_idx);
  5602. int cc = get_caret_column(caret_idx);
  5603. int indent_level = get_indent_level(cl);
  5604. double hscroll = get_h_scroll();
  5605. // Check for overlapping carets.
  5606. // We don't need to worry about selections as that is caught before this entire section.
  5607. for (int j = i - 1; j >= 0; j--) {
  5608. if (get_caret_line(caret_edit_order[j]) == cl) {
  5609. carets_to_remove.push_back(caret_edit_order[j]);
  5610. i = j;
  5611. }
  5612. }
  5613. clipboard += text[cl];
  5614. if (p_caret == -1 && caret_idx != 0) {
  5615. clipboard += "\n";
  5616. }
  5617. if (cl == 0 && get_line_count() > 1) {
  5618. _remove_text(cl, 0, cl + 1, 0);
  5619. adjust_carets_after_edit(caret_idx, cl, 0, cl + 1, text[cl].length());
  5620. } else {
  5621. _remove_text(cl, 0, cl, text[cl].length());
  5622. set_caret_column(0, false, caret_idx);
  5623. backspace(caret_idx);
  5624. set_caret_line(get_caret_line(caret_idx) + 1, caret_idx == 0, 0, 0, caret_idx);
  5625. }
  5626. // Correct the visually perceived caret column taking care of indentation level of the lines.
  5627. int diff_indent = indent_level - get_indent_level(get_caret_line(caret_idx));
  5628. cc += diff_indent;
  5629. if (diff_indent != 0) {
  5630. cc += diff_indent > 0 ? -1 : 1;
  5631. }
  5632. // Restore horizontal scroll and caret column modified by the backspace() call.
  5633. set_h_scroll(hscroll);
  5634. set_caret_column(cc, caret_idx == 0, caret_idx);
  5635. }
  5636. // Sort and remove backwards to preserve indexes.
  5637. carets_to_remove.sort();
  5638. for (int i = carets_to_remove.size() - 1; i >= 0; i--) {
  5639. remove_caret(carets_to_remove[i]);
  5640. }
  5641. end_complex_operation();
  5642. String clipboard_string = clipboard.as_string();
  5643. DisplayServer::get_singleton()->clipboard_set(clipboard_string);
  5644. cut_copy_line = clipboard_string;
  5645. }
  5646. void TextEdit::_copy_internal(int p_caret) {
  5647. ERR_FAIL_COND(p_caret > carets.size());
  5648. if (has_selection(p_caret)) {
  5649. DisplayServer::get_singleton()->clipboard_set(get_selected_text(p_caret));
  5650. cut_copy_line = "";
  5651. return;
  5652. }
  5653. StringBuilder clipboard;
  5654. Vector<int> caret_edit_order = get_caret_index_edit_order();
  5655. for (int i = caret_edit_order.size() - 1; i >= 0; i--) {
  5656. int caret_idx = caret_edit_order[i];
  5657. if (p_caret != -1 && p_caret != caret_idx) {
  5658. continue;
  5659. }
  5660. int cl = get_caret_line(caret_idx);
  5661. if (text[cl].length() != 0) {
  5662. clipboard += _base_get_text(cl, 0, cl, text[cl].length());
  5663. if (p_caret == -1 && i != 0) {
  5664. clipboard += "\n";
  5665. }
  5666. }
  5667. }
  5668. String clipboard_string = clipboard.as_string();
  5669. DisplayServer::get_singleton()->clipboard_set(clipboard_string);
  5670. cut_copy_line = clipboard_string;
  5671. }
  5672. void TextEdit::_paste_internal(int p_caret) {
  5673. ERR_FAIL_COND(p_caret > carets.size());
  5674. if (!editable) {
  5675. return;
  5676. }
  5677. String clipboard = DisplayServer::get_singleton()->clipboard_get();
  5678. Vector<String> clipboad_lines = clipboard.split("\n");
  5679. bool insert_line_per_caret = p_caret == -1 && carets.size() > 1 && clipboad_lines.size() == carets.size();
  5680. begin_complex_operation();
  5681. Vector<int> caret_edit_order = get_caret_index_edit_order();
  5682. int clipboad_line = clipboad_lines.size() - 1;
  5683. for (const int &i : caret_edit_order) {
  5684. if (p_caret != -1 && p_caret != i) {
  5685. continue;
  5686. }
  5687. if (has_selection(i)) {
  5688. delete_selection(i);
  5689. } else if (!cut_copy_line.is_empty() && cut_copy_line == clipboard) {
  5690. set_caret_column(0, i == 0, i);
  5691. String ins = "\n";
  5692. clipboard += ins;
  5693. }
  5694. if (insert_line_per_caret) {
  5695. clipboard = clipboad_lines[clipboad_line];
  5696. }
  5697. insert_text_at_caret(clipboard, i);
  5698. clipboad_line--;
  5699. }
  5700. end_complex_operation();
  5701. }
  5702. void TextEdit::_paste_primary_clipboard_internal(int p_caret) {
  5703. ERR_FAIL_COND(p_caret > carets.size());
  5704. if (!is_editable() || !DisplayServer::get_singleton()->has_feature(DisplayServer::FEATURE_CLIPBOARD_PRIMARY)) {
  5705. return;
  5706. }
  5707. String paste_buffer = DisplayServer::get_singleton()->clipboard_get_primary();
  5708. if (carets.size() == 1) {
  5709. Point2i pos = get_line_column_at_pos(get_local_mouse_pos());
  5710. deselect();
  5711. set_caret_line(pos.y, true, false);
  5712. set_caret_column(pos.x);
  5713. }
  5714. if (!paste_buffer.is_empty()) {
  5715. insert_text_at_caret(paste_buffer);
  5716. }
  5717. grab_focus();
  5718. }
  5719. // Context menu.
  5720. Key TextEdit::_get_menu_action_accelerator(const String &p_action) {
  5721. const List<Ref<InputEvent>> *events = InputMap::get_singleton()->action_get_events(p_action);
  5722. if (!events) {
  5723. return Key::NONE;
  5724. }
  5725. // Use first event in the list for the accelerator.
  5726. const List<Ref<InputEvent>>::Element *first_event = events->front();
  5727. if (!first_event) {
  5728. return Key::NONE;
  5729. }
  5730. const Ref<InputEventKey> event = first_event->get();
  5731. if (event.is_null()) {
  5732. return Key::NONE;
  5733. }
  5734. // Use physical keycode if non-zero
  5735. if (event->get_physical_keycode() != Key::NONE) {
  5736. return event->get_physical_keycode_with_modifiers();
  5737. } else {
  5738. return event->get_keycode_with_modifiers();
  5739. }
  5740. }
  5741. void TextEdit::_generate_context_menu() {
  5742. menu = memnew(PopupMenu);
  5743. add_child(menu, false, INTERNAL_MODE_FRONT);
  5744. menu_dir = memnew(PopupMenu);
  5745. menu_dir->set_name("DirMenu");
  5746. menu_dir->add_radio_check_item(RTR("Same as Layout Direction"), MENU_DIR_INHERITED);
  5747. menu_dir->add_radio_check_item(RTR("Auto-Detect Direction"), MENU_DIR_AUTO);
  5748. menu_dir->add_radio_check_item(RTR("Left-to-Right"), MENU_DIR_LTR);
  5749. menu_dir->add_radio_check_item(RTR("Right-to-Left"), MENU_DIR_RTL);
  5750. menu->add_child(menu_dir, false, INTERNAL_MODE_FRONT);
  5751. menu_ctl = memnew(PopupMenu);
  5752. menu_ctl->set_name("CTLMenu");
  5753. menu_ctl->add_item(RTR("Left-to-Right Mark (LRM)"), MENU_INSERT_LRM);
  5754. menu_ctl->add_item(RTR("Right-to-Left Mark (RLM)"), MENU_INSERT_RLM);
  5755. menu_ctl->add_item(RTR("Start of Left-to-Right Embedding (LRE)"), MENU_INSERT_LRE);
  5756. menu_ctl->add_item(RTR("Start of Right-to-Left Embedding (RLE)"), MENU_INSERT_RLE);
  5757. menu_ctl->add_item(RTR("Start of Left-to-Right Override (LRO)"), MENU_INSERT_LRO);
  5758. menu_ctl->add_item(RTR("Start of Right-to-Left Override (RLO)"), MENU_INSERT_RLO);
  5759. menu_ctl->add_item(RTR("Pop Direction Formatting (PDF)"), MENU_INSERT_PDF);
  5760. menu_ctl->add_separator();
  5761. menu_ctl->add_item(RTR("Arabic Letter Mark (ALM)"), MENU_INSERT_ALM);
  5762. menu_ctl->add_item(RTR("Left-to-Right Isolate (LRI)"), MENU_INSERT_LRI);
  5763. menu_ctl->add_item(RTR("Right-to-Left Isolate (RLI)"), MENU_INSERT_RLI);
  5764. menu_ctl->add_item(RTR("First Strong Isolate (FSI)"), MENU_INSERT_FSI);
  5765. menu_ctl->add_item(RTR("Pop Direction Isolate (PDI)"), MENU_INSERT_PDI);
  5766. menu_ctl->add_separator();
  5767. menu_ctl->add_item(RTR("Zero-Width Joiner (ZWJ)"), MENU_INSERT_ZWJ);
  5768. menu_ctl->add_item(RTR("Zero-Width Non-Joiner (ZWNJ)"), MENU_INSERT_ZWNJ);
  5769. menu_ctl->add_item(RTR("Word Joiner (WJ)"), MENU_INSERT_WJ);
  5770. menu_ctl->add_item(RTR("Soft Hyphen (SHY)"), MENU_INSERT_SHY);
  5771. menu->add_child(menu_ctl, false, INTERNAL_MODE_FRONT);
  5772. menu->add_item(RTR("Cut"), MENU_CUT);
  5773. menu->add_item(RTR("Copy"), MENU_COPY);
  5774. menu->add_item(RTR("Paste"), MENU_PASTE);
  5775. menu->add_separator();
  5776. menu->add_item(RTR("Select All"), MENU_SELECT_ALL);
  5777. menu->add_item(RTR("Clear"), MENU_CLEAR);
  5778. menu->add_separator();
  5779. menu->add_item(RTR("Undo"), MENU_UNDO);
  5780. menu->add_item(RTR("Redo"), MENU_REDO);
  5781. menu->add_separator();
  5782. menu->add_submenu_item(RTR("Text Writing Direction"), "DirMenu", MENU_SUBMENU_TEXT_DIR);
  5783. menu->add_separator();
  5784. menu->add_check_item(RTR("Display Control Characters"), MENU_DISPLAY_UCC);
  5785. menu->add_submenu_item(RTR("Insert Control Character"), "CTLMenu", MENU_SUBMENU_INSERT_UCC);
  5786. menu->connect("id_pressed", callable_mp(this, &TextEdit::menu_option));
  5787. menu_dir->connect("id_pressed", callable_mp(this, &TextEdit::menu_option));
  5788. menu_ctl->connect("id_pressed", callable_mp(this, &TextEdit::menu_option));
  5789. }
  5790. void TextEdit::_update_context_menu() {
  5791. if (!menu) {
  5792. _generate_context_menu();
  5793. }
  5794. int idx = -1;
  5795. #define MENU_ITEM_ACTION_DISABLED(m_menu, m_id, m_action, m_disabled) \
  5796. idx = m_menu->get_item_index(m_id); \
  5797. if (idx >= 0) { \
  5798. m_menu->set_item_accelerator(idx, shortcut_keys_enabled ? _get_menu_action_accelerator(m_action) : Key::NONE); \
  5799. m_menu->set_item_disabled(idx, m_disabled); \
  5800. }
  5801. #define MENU_ITEM_ACTION(m_menu, m_id, m_action) \
  5802. idx = m_menu->get_item_index(m_id); \
  5803. if (idx >= 0) { \
  5804. m_menu->set_item_accelerator(idx, shortcut_keys_enabled ? _get_menu_action_accelerator(m_action) : Key::NONE); \
  5805. }
  5806. #define MENU_ITEM_DISABLED(m_menu, m_id, m_disabled) \
  5807. idx = m_menu->get_item_index(m_id); \
  5808. if (idx >= 0) { \
  5809. m_menu->set_item_disabled(idx, m_disabled); \
  5810. }
  5811. #define MENU_ITEM_CHECKED(m_menu, m_id, m_checked) \
  5812. idx = m_menu->get_item_index(m_id); \
  5813. if (idx >= 0) { \
  5814. m_menu->set_item_checked(idx, m_checked); \
  5815. }
  5816. MENU_ITEM_ACTION_DISABLED(menu, MENU_CUT, "ui_cut", !editable)
  5817. MENU_ITEM_ACTION(menu, MENU_COPY, "ui_copy")
  5818. MENU_ITEM_ACTION_DISABLED(menu, MENU_PASTE, "ui_paste", !editable)
  5819. MENU_ITEM_ACTION_DISABLED(menu, MENU_SELECT_ALL, "ui_text_select_all", !selecting_enabled)
  5820. MENU_ITEM_DISABLED(menu, MENU_CLEAR, !editable)
  5821. MENU_ITEM_ACTION_DISABLED(menu, MENU_UNDO, "ui_undo", !editable || !has_undo())
  5822. MENU_ITEM_ACTION_DISABLED(menu, MENU_REDO, "ui_redo", !editable || !has_redo())
  5823. MENU_ITEM_CHECKED(menu_dir, MENU_DIR_INHERITED, text_direction == TEXT_DIRECTION_INHERITED)
  5824. MENU_ITEM_CHECKED(menu_dir, MENU_DIR_AUTO, text_direction == TEXT_DIRECTION_AUTO)
  5825. MENU_ITEM_CHECKED(menu_dir, MENU_DIR_LTR, text_direction == TEXT_DIRECTION_LTR)
  5826. MENU_ITEM_CHECKED(menu_dir, MENU_DIR_RTL, text_direction == TEXT_DIRECTION_RTL)
  5827. MENU_ITEM_CHECKED(menu, MENU_DISPLAY_UCC, draw_control_chars)
  5828. MENU_ITEM_DISABLED(menu, MENU_SUBMENU_INSERT_UCC, !editable)
  5829. #undef MENU_ITEM_ACTION_DISABLED
  5830. #undef MENU_ITEM_ACTION
  5831. #undef MENU_ITEM_DISABLED
  5832. #undef MENU_ITEM_CHECKED
  5833. }
  5834. /* Versioning */
  5835. void TextEdit::_push_current_op() {
  5836. if (pending_action_end) {
  5837. start_action(EditAction::ACTION_NONE);
  5838. return;
  5839. }
  5840. if (current_op.type == TextOperation::TYPE_NONE) {
  5841. return; // Nothing to do.
  5842. }
  5843. if (next_operation_is_complex) {
  5844. current_op.chain_forward = true;
  5845. next_operation_is_complex = false;
  5846. }
  5847. undo_stack.push_back(current_op);
  5848. current_op.type = TextOperation::TYPE_NONE;
  5849. current_op.text = "";
  5850. current_op.chain_forward = false;
  5851. if (undo_stack.size() > undo_stack_max_size) {
  5852. undo_stack.pop_front();
  5853. }
  5854. }
  5855. void TextEdit::_do_text_op(const TextOperation &p_op, bool p_reverse) {
  5856. ERR_FAIL_COND(p_op.type == TextOperation::TYPE_NONE);
  5857. bool insert = p_op.type == TextOperation::TYPE_INSERT;
  5858. if (p_reverse) {
  5859. insert = !insert;
  5860. }
  5861. if (insert) {
  5862. int check_line;
  5863. int check_column;
  5864. _base_insert_text(p_op.from_line, p_op.from_column, p_op.text, check_line, check_column);
  5865. ERR_FAIL_COND(check_line != p_op.to_line); // BUG.
  5866. ERR_FAIL_COND(check_column != p_op.to_column); // BUG.
  5867. } else {
  5868. _base_remove_text(p_op.from_line, p_op.from_column, p_op.to_line, p_op.to_column);
  5869. }
  5870. }
  5871. void TextEdit::_clear_redo() {
  5872. if (undo_stack_pos == nullptr) {
  5873. return; // Nothing to clear.
  5874. }
  5875. _push_current_op();
  5876. while (undo_stack_pos) {
  5877. List<TextOperation>::Element *elem = undo_stack_pos;
  5878. undo_stack_pos = undo_stack_pos->next();
  5879. undo_stack.erase(elem);
  5880. }
  5881. }
  5882. /* Search */
  5883. 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 {
  5884. int col = -1;
  5885. if (p_key.length() > 0 && p_search.length() > 0) {
  5886. if (p_from_column < 0 || p_from_column > p_search.length()) {
  5887. p_from_column = 0;
  5888. }
  5889. while (col == -1 && p_from_column <= p_search.length()) {
  5890. if (p_search_flags & SEARCH_MATCH_CASE) {
  5891. col = p_search.find(p_key, p_from_column);
  5892. } else {
  5893. col = p_search.findn(p_key, p_from_column);
  5894. }
  5895. // Whole words only.
  5896. if (col != -1 && p_search_flags & SEARCH_WHOLE_WORDS) {
  5897. p_from_column = col;
  5898. if (col > 0 && !is_symbol(p_search[col - 1])) {
  5899. col = -1;
  5900. } else if ((col + p_key.length()) < p_search.length() && !is_symbol(p_search[col + p_key.length()])) {
  5901. col = -1;
  5902. }
  5903. }
  5904. p_from_column += 1;
  5905. }
  5906. }
  5907. return col;
  5908. }
  5909. /* Mouse */
  5910. int TextEdit::_get_char_pos_for_line(int p_px, int p_line, int p_wrap_index) const {
  5911. ERR_FAIL_INDEX_V(p_line, text.size(), 0);
  5912. p_wrap_index = MIN(p_wrap_index, text.get_line_data(p_line)->get_line_count() - 1);
  5913. RID text_rid = text.get_line_data(p_line)->get_line_rid(p_wrap_index);
  5914. if (is_layout_rtl()) {
  5915. p_px = TS->shaped_text_get_size(text_rid).x - p_px;
  5916. }
  5917. return TS->shaped_text_hit_test_position(text_rid, p_px);
  5918. }
  5919. /* Caret */
  5920. void TextEdit::_emit_caret_changed() {
  5921. emit_signal(SNAME("caret_changed"));
  5922. caret_pos_dirty = false;
  5923. caret_index_edit_dirty = true;
  5924. }
  5925. void TextEdit::_reset_caret_blink_timer() {
  5926. if (!caret_blink_enabled) {
  5927. return;
  5928. }
  5929. draw_caret = true;
  5930. if (has_focus()) {
  5931. caret_blink_timer->stop();
  5932. caret_blink_timer->start();
  5933. queue_redraw();
  5934. }
  5935. }
  5936. void TextEdit::_toggle_draw_caret() {
  5937. draw_caret = !draw_caret;
  5938. if (is_visible_in_tree() && has_focus() && window_has_focus) {
  5939. queue_redraw();
  5940. }
  5941. }
  5942. int TextEdit::_get_column_x_offset_for_line(int p_char, int p_line, int p_column) const {
  5943. ERR_FAIL_INDEX_V(p_line, text.size(), 0);
  5944. int row = 0;
  5945. Vector<Vector2i> rows2 = text.get_line_wrap_ranges(p_line);
  5946. for (int i = 0; i < rows2.size(); i++) {
  5947. if ((p_char >= rows2[i].x) && (p_char <= rows2[i].y)) {
  5948. row = i;
  5949. break;
  5950. }
  5951. }
  5952. RID text_rid = text.get_line_data(p_line)->get_line_rid(row);
  5953. CaretInfo ts_caret = TS->shaped_text_get_carets(text_rid, p_column);
  5954. 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())) {
  5955. return ts_caret.l_caret.position.x;
  5956. } else {
  5957. return ts_caret.t_caret.position.x;
  5958. }
  5959. }
  5960. /* Selection */
  5961. void TextEdit::_click_selection_held() {
  5962. // Warning: is_mouse_button_pressed(MouseButton::LEFT) returns false for double+ clicks, so this doesn't work for MODE_WORD
  5963. // and MODE_LINE. However, moving the mouse triggers _gui_input, which calls these functions too, so that's not a huge problem.
  5964. // I'm unsure if there's an actual fix that doesn't have a ton of side effects.
  5965. if (Input::get_singleton()->is_mouse_button_pressed(MouseButton::LEFT) && get_selection_mode() != SelectionMode::SELECTION_MODE_NONE) {
  5966. switch (get_selection_mode()) {
  5967. case SelectionMode::SELECTION_MODE_POINTER: {
  5968. _update_selection_mode_pointer();
  5969. } break;
  5970. case SelectionMode::SELECTION_MODE_WORD: {
  5971. _update_selection_mode_word();
  5972. } break;
  5973. case SelectionMode::SELECTION_MODE_LINE: {
  5974. _update_selection_mode_line();
  5975. } break;
  5976. default: {
  5977. break;
  5978. }
  5979. }
  5980. } else {
  5981. click_select_held->stop();
  5982. }
  5983. }
  5984. void TextEdit::_update_selection_mode_pointer() {
  5985. dragging_selection = true;
  5986. Point2 mp = get_local_mouse_pos();
  5987. Point2i pos = get_line_column_at_pos(mp);
  5988. int line = pos.y;
  5989. int col = pos.x;
  5990. int caret_idx = carets.size() - 1;
  5991. select(carets[caret_idx].selection.selecting_line, carets[caret_idx].selection.selecting_column, line, col, caret_idx);
  5992. set_caret_line(line, false, true, 0, caret_idx);
  5993. set_caret_column(col, true, caret_idx);
  5994. queue_redraw();
  5995. click_select_held->start();
  5996. merge_overlapping_carets();
  5997. }
  5998. void TextEdit::_update_selection_mode_word() {
  5999. dragging_selection = true;
  6000. Point2 mp = get_local_mouse_pos();
  6001. Point2i pos = get_line_column_at_pos(mp);
  6002. int line = pos.y;
  6003. int col = pos.x;
  6004. int caret_idx = carets.size() - 1;
  6005. int caret_pos = CLAMP(col, 0, text[line].length());
  6006. int beg = caret_pos;
  6007. int end = beg;
  6008. PackedInt32Array words = TS->shaped_text_get_word_breaks(text.get_line_data(line)->get_rid());
  6009. for (int i = 0; i < words.size(); i = i + 2) {
  6010. if ((words[i] < caret_pos && words[i + 1] > caret_pos) || (i == words.size() - 2 && caret_pos == words[i + 1])) {
  6011. beg = words[i];
  6012. end = words[i + 1];
  6013. break;
  6014. }
  6015. }
  6016. /* Initial selection. */
  6017. if (!has_selection(caret_idx)) {
  6018. select(line, beg, line, end, caret_idx);
  6019. carets.write[caret_idx].selection.selecting_column = beg;
  6020. carets.write[caret_idx].selection.selected_word_beg = beg;
  6021. carets.write[caret_idx].selection.selected_word_end = end;
  6022. carets.write[caret_idx].selection.selected_word_origin = beg;
  6023. set_caret_line(line, false, true, 0, caret_idx);
  6024. set_caret_column(end, true, caret_idx);
  6025. } else {
  6026. if ((col <= carets[caret_idx].selection.selected_word_origin && line == get_selection_line(caret_idx)) || line < get_selection_line(caret_idx)) {
  6027. carets.write[caret_idx].selection.selecting_column = carets[caret_idx].selection.selected_word_end;
  6028. select(line, beg, get_selection_line(caret_idx), carets[caret_idx].selection.selected_word_end, caret_idx);
  6029. set_caret_line(line, false, true, 0, caret_idx);
  6030. set_caret_column(beg, true, caret_idx);
  6031. } else {
  6032. carets.write[caret_idx].selection.selecting_column = carets[caret_idx].selection.selected_word_beg;
  6033. select(get_selection_line(caret_idx), carets[caret_idx].selection.selected_word_beg, line, end, caret_idx);
  6034. set_caret_line(get_selection_to_line(caret_idx), false, true, 0, caret_idx);
  6035. set_caret_column(get_selection_to_column(caret_idx), true, caret_idx);
  6036. }
  6037. }
  6038. if (DisplayServer::get_singleton()->has_feature(DisplayServer::FEATURE_CLIPBOARD_PRIMARY)) {
  6039. DisplayServer::get_singleton()->clipboard_set_primary(get_selected_text());
  6040. }
  6041. queue_redraw();
  6042. click_select_held->start();
  6043. merge_overlapping_carets();
  6044. }
  6045. void TextEdit::_update_selection_mode_line() {
  6046. dragging_selection = true;
  6047. Point2 mp = get_local_mouse_pos();
  6048. Point2i pos = get_line_column_at_pos(mp);
  6049. int line = pos.y;
  6050. int col = pos.x;
  6051. int caret_idx = carets.size() - 1;
  6052. col = 0;
  6053. if (line < carets[caret_idx].selection.selecting_line) {
  6054. /* Caret is above us. */
  6055. set_caret_line(line - 1, false, true, 0, caret_idx);
  6056. carets.write[caret_idx].selection.selecting_column = text[get_selection_line(caret_idx)].length();
  6057. } else {
  6058. /* Caret is below us. */
  6059. set_caret_line(line + 1, false, true, 0, caret_idx);
  6060. carets.write[caret_idx].selection.selecting_column = 0;
  6061. col = text[line].length();
  6062. }
  6063. set_caret_column(0, false, caret_idx);
  6064. select(carets[caret_idx].selection.selecting_line, carets[caret_idx].selection.selecting_column, line, col, caret_idx);
  6065. if (DisplayServer::get_singleton()->has_feature(DisplayServer::FEATURE_CLIPBOARD_PRIMARY)) {
  6066. DisplayServer::get_singleton()->clipboard_set_primary(get_selected_text());
  6067. }
  6068. queue_redraw();
  6069. click_select_held->start();
  6070. merge_overlapping_carets();
  6071. }
  6072. void TextEdit::_pre_shift_selection(int p_caret) {
  6073. if (!selecting_enabled) {
  6074. return;
  6075. }
  6076. if (!has_selection(p_caret) || get_selection_mode() == SelectionMode::SELECTION_MODE_NONE) {
  6077. carets.write[p_caret].selection.active = true;
  6078. set_selection_mode(SelectionMode::SELECTION_MODE_SHIFT, get_caret_line(p_caret), get_caret_column(p_caret), p_caret);
  6079. return;
  6080. }
  6081. set_selection_mode(SelectionMode::SELECTION_MODE_SHIFT, get_selection_line(p_caret), get_selection_column(p_caret), p_caret);
  6082. }
  6083. void TextEdit::_post_shift_selection(int p_caret) {
  6084. if (!selecting_enabled) {
  6085. return;
  6086. }
  6087. if (has_selection(p_caret) && get_selection_mode() == SelectionMode::SELECTION_MODE_SHIFT) {
  6088. select(get_selection_line(p_caret), get_selection_column(p_caret), get_caret_line(p_caret), get_caret_column(p_caret), p_caret);
  6089. }
  6090. }
  6091. /* Line Wrapping */
  6092. void TextEdit::_update_wrap_at_column(bool p_force) {
  6093. int new_wrap_at = get_size().width - style_normal->get_minimum_size().width - gutters_width - gutter_padding;
  6094. if (draw_minimap) {
  6095. new_wrap_at -= minimap_width;
  6096. }
  6097. if (v_scroll->is_visible_in_tree()) {
  6098. new_wrap_at -= v_scroll->get_combined_minimum_size().width;
  6099. }
  6100. /* Give it a little more space. */
  6101. new_wrap_at -= wrap_right_offset;
  6102. if ((wrap_at_column != new_wrap_at) || p_force) {
  6103. wrap_at_column = new_wrap_at;
  6104. if (line_wrapping_mode) {
  6105. text.set_width(wrap_at_column);
  6106. } else {
  6107. text.set_width(-1);
  6108. }
  6109. text.invalidate_all_lines();
  6110. _update_placeholder();
  6111. }
  6112. // Update viewport.
  6113. int first_vis_line = get_first_visible_line();
  6114. if (is_line_wrapped(first_vis_line)) {
  6115. first_visible_line_wrap_ofs = MIN(first_visible_line_wrap_ofs, get_line_wrap_count(first_vis_line));
  6116. } else {
  6117. first_visible_line_wrap_ofs = 0;
  6118. }
  6119. set_line_as_first_visible(first_visible_line, first_visible_line_wrap_ofs);
  6120. }
  6121. /* Viewport. */
  6122. void TextEdit::_update_scrollbars() {
  6123. Size2 size = get_size();
  6124. Size2 hmin = h_scroll->get_combined_minimum_size();
  6125. Size2 vmin = v_scroll->get_combined_minimum_size();
  6126. v_scroll->set_begin(Point2(size.width - vmin.width, style_normal->get_margin(SIDE_TOP)));
  6127. v_scroll->set_end(Point2(size.width, size.height - style_normal->get_margin(SIDE_TOP) - style_normal->get_margin(SIDE_BOTTOM)));
  6128. h_scroll->set_begin(Point2(0, size.height - hmin.height));
  6129. h_scroll->set_end(Point2(size.width - vmin.width, size.height));
  6130. bool draw_placeholder = text.size() == 1 && text[0].length() == 0;
  6131. int visible_rows = get_visible_line_count();
  6132. int total_rows = draw_placeholder ? placeholder_wraped_rows.size() - 1 : get_total_visible_line_count();
  6133. if (scroll_past_end_of_file_enabled) {
  6134. total_rows += visible_rows - 1;
  6135. }
  6136. int visible_width = size.width - style_normal->get_minimum_size().width;
  6137. int total_width = (draw_placeholder ? placeholder_max_width : text.get_max_width()) + vmin.x + gutters_width + gutter_padding;
  6138. if (draw_minimap) {
  6139. total_width += minimap_width;
  6140. }
  6141. content_height_cache = MAX(total_rows, 1) * get_line_height();
  6142. if (fit_content_height) {
  6143. update_minimum_size();
  6144. }
  6145. updating_scrolls = true;
  6146. if (total_rows > visible_rows) {
  6147. v_scroll->show();
  6148. v_scroll->set_max(total_rows + _get_visible_lines_offset());
  6149. v_scroll->set_page(visible_rows + _get_visible_lines_offset());
  6150. if (smooth_scroll_enabled) {
  6151. v_scroll->set_step(0.25);
  6152. } else {
  6153. v_scroll->set_step(1);
  6154. }
  6155. set_v_scroll(get_v_scroll());
  6156. } else {
  6157. first_visible_line = 0;
  6158. first_visible_line_wrap_ofs = 0;
  6159. v_scroll->set_value(0);
  6160. v_scroll->set_max(0);
  6161. v_scroll->hide();
  6162. }
  6163. if (total_width > visible_width && get_line_wrapping_mode() == LineWrappingMode::LINE_WRAPPING_NONE) {
  6164. h_scroll->show();
  6165. h_scroll->set_max(total_width);
  6166. h_scroll->set_page(visible_width);
  6167. if (first_visible_col > (total_width - visible_width)) {
  6168. first_visible_col = (total_width - visible_width);
  6169. }
  6170. if (fabs(h_scroll->get_value() - (double)first_visible_col) >= 1) {
  6171. h_scroll->set_value(first_visible_col);
  6172. }
  6173. } else {
  6174. first_visible_col = 0;
  6175. h_scroll->set_value(0);
  6176. h_scroll->set_max(0);
  6177. h_scroll->hide();
  6178. }
  6179. updating_scrolls = false;
  6180. }
  6181. int TextEdit::_get_control_height() const {
  6182. int control_height = get_size().height;
  6183. control_height -= style_normal->get_minimum_size().height;
  6184. if (h_scroll->is_visible_in_tree()) {
  6185. control_height -= h_scroll->get_size().height;
  6186. }
  6187. return control_height;
  6188. }
  6189. void TextEdit::_v_scroll_input() {
  6190. scrolling = false;
  6191. minimap_clicked = false;
  6192. }
  6193. void TextEdit::_scroll_moved(double p_to_val) {
  6194. if (updating_scrolls) {
  6195. return;
  6196. }
  6197. if (h_scroll->is_visible_in_tree()) {
  6198. first_visible_col = h_scroll->get_value();
  6199. }
  6200. if (v_scroll->is_visible_in_tree()) {
  6201. // Set line ofs and wrap ofs.
  6202. bool draw_placeholder = text.size() == 1 && text[0].length() == 0;
  6203. int v_scroll_i = floor(get_v_scroll());
  6204. int sc = 0;
  6205. int n_line;
  6206. for (n_line = 0; n_line < text.size(); n_line++) {
  6207. if (!_is_line_hidden(n_line)) {
  6208. sc++;
  6209. sc += draw_placeholder ? placeholder_wraped_rows.size() - 1 : get_line_wrap_count(n_line);
  6210. if (sc > v_scroll_i) {
  6211. break;
  6212. }
  6213. }
  6214. }
  6215. n_line = MIN(n_line, text.size() - 1);
  6216. int line_wrap_amount = draw_placeholder ? placeholder_wraped_rows.size() - 1 : get_line_wrap_count(n_line);
  6217. int wi = line_wrap_amount - (sc - v_scroll_i - 1);
  6218. wi = CLAMP(wi, 0, line_wrap_amount);
  6219. first_visible_line = n_line;
  6220. first_visible_line_wrap_ofs = wi;
  6221. }
  6222. queue_redraw();
  6223. }
  6224. double TextEdit::_get_visible_lines_offset() const {
  6225. double total = _get_control_height();
  6226. total /= (double)get_line_height();
  6227. total = total - floor(total);
  6228. total = -CLAMP(total, 0.001, 1) + 1;
  6229. return total;
  6230. }
  6231. double TextEdit::_get_v_scroll_offset() const {
  6232. double val = get_v_scroll() - floor(get_v_scroll());
  6233. return CLAMP(val, 0, 1);
  6234. }
  6235. void TextEdit::_scroll_up(real_t p_delta) {
  6236. if (scrolling && smooth_scroll_enabled && SIGN(target_v_scroll - v_scroll->get_value()) != SIGN(-p_delta)) {
  6237. scrolling = false;
  6238. minimap_clicked = false;
  6239. }
  6240. if (scrolling) {
  6241. target_v_scroll = (target_v_scroll - p_delta);
  6242. } else {
  6243. target_v_scroll = (get_v_scroll() - p_delta);
  6244. }
  6245. if (smooth_scroll_enabled) {
  6246. if (target_v_scroll <= 0) {
  6247. target_v_scroll = 0;
  6248. }
  6249. if (Math::abs(target_v_scroll - v_scroll->get_value()) < 1.0) {
  6250. v_scroll->set_value(target_v_scroll);
  6251. } else {
  6252. scrolling = true;
  6253. set_physics_process_internal(true);
  6254. }
  6255. } else {
  6256. set_v_scroll(target_v_scroll);
  6257. }
  6258. }
  6259. void TextEdit::_scroll_down(real_t p_delta) {
  6260. if (scrolling && smooth_scroll_enabled && SIGN(target_v_scroll - v_scroll->get_value()) != SIGN(p_delta)) {
  6261. scrolling = false;
  6262. minimap_clicked = false;
  6263. }
  6264. if (scrolling) {
  6265. target_v_scroll = (target_v_scroll + p_delta);
  6266. } else {
  6267. target_v_scroll = (get_v_scroll() + p_delta);
  6268. }
  6269. if (smooth_scroll_enabled) {
  6270. int max_v_scroll = round(v_scroll->get_max() - v_scroll->get_page());
  6271. if (target_v_scroll > max_v_scroll) {
  6272. target_v_scroll = max_v_scroll;
  6273. }
  6274. if (Math::abs(target_v_scroll - v_scroll->get_value()) < 1.0) {
  6275. v_scroll->set_value(target_v_scroll);
  6276. } else {
  6277. scrolling = true;
  6278. set_physics_process_internal(true);
  6279. }
  6280. } else {
  6281. set_v_scroll(target_v_scroll);
  6282. }
  6283. }
  6284. void TextEdit::_scroll_lines_up() {
  6285. scrolling = false;
  6286. minimap_clicked = false;
  6287. // Adjust the vertical scroll.
  6288. set_v_scroll(get_v_scroll() - 1);
  6289. // Adjust the caret to viewport.
  6290. for (int i = 0; i < carets.size(); i++) {
  6291. if (has_selection(i)) {
  6292. continue;
  6293. }
  6294. int last_vis_line = get_last_full_visible_line();
  6295. int last_vis_wrap = get_last_full_visible_line_wrap_index();
  6296. if (get_caret_line(i) > last_vis_line || (get_caret_line(i) == last_vis_line && get_caret_wrap_index(i) > last_vis_wrap)) {
  6297. set_caret_line(last_vis_line, false, false, last_vis_wrap, i);
  6298. }
  6299. }
  6300. merge_overlapping_carets();
  6301. }
  6302. void TextEdit::_scroll_lines_down() {
  6303. scrolling = false;
  6304. minimap_clicked = false;
  6305. // Adjust the vertical scroll.
  6306. set_v_scroll(get_v_scroll() + 1);
  6307. // Adjust the caret to viewport.
  6308. for (int i = 0; i < carets.size(); i++) {
  6309. if (has_selection(i)) {
  6310. continue;
  6311. }
  6312. int first_vis_line = get_first_visible_line();
  6313. if (get_caret_line(i) < first_vis_line || (get_caret_line(i) == first_vis_line && get_caret_wrap_index(i) < first_visible_line_wrap_ofs)) {
  6314. set_caret_line(first_vis_line, false, false, first_visible_line_wrap_ofs, i);
  6315. }
  6316. }
  6317. merge_overlapping_carets();
  6318. }
  6319. // Minimap
  6320. void TextEdit::_update_minimap_hover() {
  6321. const Point2 mp = get_local_mouse_pos();
  6322. const int xmargin_end = get_size().width - style_normal->get_margin(SIDE_RIGHT);
  6323. const bool hovering_sidebar = mp.x > xmargin_end - minimap_width && mp.x < xmargin_end;
  6324. if (!hovering_sidebar) {
  6325. if (hovering_minimap) {
  6326. // Only redraw if the hovering status changed.
  6327. hovering_minimap = false;
  6328. queue_redraw();
  6329. }
  6330. // Return early to avoid running the operations below when not needed.
  6331. return;
  6332. }
  6333. const int row = get_minimap_line_at_pos(mp);
  6334. const bool new_hovering_minimap = row >= get_first_visible_line() && row <= get_last_full_visible_line();
  6335. if (new_hovering_minimap != hovering_minimap) {
  6336. // Only redraw if the hovering status changed.
  6337. hovering_minimap = new_hovering_minimap;
  6338. queue_redraw();
  6339. }
  6340. }
  6341. void TextEdit::_update_minimap_click() {
  6342. Point2 mp = get_local_mouse_pos();
  6343. int xmargin_end = get_size().width - style_normal->get_margin(SIDE_RIGHT);
  6344. if (!dragging_minimap && (mp.x < xmargin_end - minimap_width || mp.y > xmargin_end)) {
  6345. minimap_clicked = false;
  6346. return;
  6347. }
  6348. minimap_clicked = true;
  6349. dragging_minimap = true;
  6350. int row = get_minimap_line_at_pos(mp);
  6351. if (row >= get_first_visible_line() && (row < get_last_full_visible_line() || row >= (text.size() - 1))) {
  6352. minimap_scroll_ratio = v_scroll->get_as_ratio();
  6353. minimap_scroll_click_pos = mp.y;
  6354. can_drag_minimap = true;
  6355. return;
  6356. }
  6357. Point2i next_line = get_next_visible_line_index_offset_from(row, 0, -get_visible_line_count() / 2);
  6358. int first_line = row - next_line.x + 1;
  6359. double delta = get_scroll_pos_for_line(first_line, next_line.y) - get_v_scroll();
  6360. if (delta < 0) {
  6361. _scroll_up(-delta);
  6362. } else {
  6363. _scroll_down(delta);
  6364. }
  6365. }
  6366. void TextEdit::_update_minimap_drag() {
  6367. if (!can_drag_minimap) {
  6368. return;
  6369. }
  6370. int control_height = _get_control_height();
  6371. int scroll_height = v_scroll->get_max() * (minimap_char_size.y + minimap_line_spacing);
  6372. if (control_height > scroll_height) {
  6373. control_height = scroll_height;
  6374. }
  6375. Point2 mp = get_local_mouse_pos();
  6376. double diff = (mp.y - minimap_scroll_click_pos) / control_height;
  6377. v_scroll->set_as_ratio(minimap_scroll_ratio + diff);
  6378. }
  6379. /* Gutters. */
  6380. void TextEdit::_update_gutter_width() {
  6381. gutters_width = 0;
  6382. for (int i = 0; i < gutters.size(); i++) {
  6383. if (gutters[i].draw) {
  6384. gutters_width += gutters[i].width;
  6385. }
  6386. }
  6387. if (gutters_width > 0) {
  6388. gutter_padding = 2;
  6389. }
  6390. queue_redraw();
  6391. }
  6392. /* Syntax highlighting. */
  6393. Dictionary TextEdit::_get_line_syntax_highlighting(int p_line) {
  6394. return syntax_highlighter.is_null() && !setting_text ? Dictionary() : syntax_highlighter->get_line_syntax_highlighting(p_line);
  6395. }
  6396. /*** Super internal Core API. Everything builds on it. ***/
  6397. void TextEdit::_text_changed_emit() {
  6398. emit_signal(SNAME("text_changed"));
  6399. text_changed_dirty = false;
  6400. }
  6401. void TextEdit::_insert_text(int p_line, int p_char, const String &p_text, int *r_end_line, int *r_end_char) {
  6402. if (!setting_text && idle_detect->is_inside_tree()) {
  6403. idle_detect->start();
  6404. }
  6405. if (undo_enabled) {
  6406. _clear_redo();
  6407. }
  6408. int retline, retchar;
  6409. _base_insert_text(p_line, p_char, p_text, retline, retchar);
  6410. if (r_end_line) {
  6411. *r_end_line = retline;
  6412. }
  6413. if (r_end_char) {
  6414. *r_end_char = retchar;
  6415. }
  6416. if (!undo_enabled) {
  6417. return;
  6418. }
  6419. /* UNDO!! */
  6420. TextOperation op;
  6421. op.type = TextOperation::TYPE_INSERT;
  6422. op.from_line = p_line;
  6423. op.from_column = p_char;
  6424. op.to_line = retline;
  6425. op.to_column = retchar;
  6426. op.text = p_text;
  6427. op.version = ++version;
  6428. op.chain_forward = false;
  6429. op.chain_backward = false;
  6430. op.start_carets = carets;
  6431. op.end_carets = carets;
  6432. // See if it should just be set as current op.
  6433. if (current_op.type != op.type) {
  6434. op.prev_version = get_version();
  6435. _push_current_op();
  6436. current_op = op;
  6437. return; // Set as current op, return.
  6438. }
  6439. // See if it can be merged.
  6440. if (current_op.to_line != p_line || current_op.to_column != p_char) {
  6441. op.prev_version = get_version();
  6442. _push_current_op();
  6443. current_op = op;
  6444. return; // Set as current op, return.
  6445. }
  6446. // Merge current op.
  6447. current_op.text += p_text;
  6448. current_op.to_column = retchar;
  6449. current_op.to_line = retline;
  6450. current_op.version = op.version;
  6451. current_op.end_carets = carets;
  6452. }
  6453. void TextEdit::_remove_text(int p_from_line, int p_from_column, int p_to_line, int p_to_column) {
  6454. if (!setting_text && idle_detect->is_inside_tree()) {
  6455. idle_detect->start();
  6456. }
  6457. String txt;
  6458. if (undo_enabled) {
  6459. _clear_redo();
  6460. txt = _base_get_text(p_from_line, p_from_column, p_to_line, p_to_column);
  6461. }
  6462. _base_remove_text(p_from_line, p_from_column, p_to_line, p_to_column);
  6463. if (!undo_enabled) {
  6464. return;
  6465. }
  6466. /* UNDO! */
  6467. TextOperation op;
  6468. op.type = TextOperation::TYPE_REMOVE;
  6469. op.from_line = p_from_line;
  6470. op.from_column = p_from_column;
  6471. op.to_line = p_to_line;
  6472. op.to_column = p_to_column;
  6473. op.text = txt;
  6474. op.version = ++version;
  6475. op.chain_forward = false;
  6476. op.chain_backward = false;
  6477. op.start_carets = carets;
  6478. op.end_carets = carets;
  6479. // See if it should just be set as current op.
  6480. if (current_op.type != op.type) {
  6481. op.prev_version = get_version();
  6482. _push_current_op();
  6483. current_op = op;
  6484. return; // Set as current op, return.
  6485. }
  6486. // See if it can be merged.
  6487. if (current_op.from_line == p_to_line && current_op.from_column == p_to_column) {
  6488. // Backspace or similar.
  6489. current_op.text = txt + current_op.text;
  6490. current_op.from_line = p_from_line;
  6491. current_op.from_column = p_from_column;
  6492. current_op.end_carets = carets;
  6493. return; // Update current op.
  6494. }
  6495. op.prev_version = get_version();
  6496. _push_current_op();
  6497. current_op = op;
  6498. }
  6499. void TextEdit::_base_insert_text(int p_line, int p_char, const String &p_text, int &r_end_line, int &r_end_column) {
  6500. // Save for undo.
  6501. ERR_FAIL_INDEX(p_line, text.size());
  6502. ERR_FAIL_COND(p_char < 0);
  6503. /* STEP 1: Remove \r from source text and separate in substrings. */
  6504. const String text_to_insert = p_text.replace("\r", "");
  6505. Vector<String> substrings = text_to_insert.split("\n");
  6506. // Is this just a new empty line?
  6507. bool shift_first_line = p_char == 0 && substrings.size() == 2 && text_to_insert == "\n";
  6508. /* STEP 2: Add spaces if the char is greater than the end of the line. */
  6509. while (p_char > text[p_line].length()) {
  6510. text.set(p_line, text[p_line] + String::chr(' '), structured_text_parser(st_parser, st_args, text[p_line] + String::chr(' ')));
  6511. }
  6512. /* STEP 3: Separate dest string in pre and post text. */
  6513. String postinsert_text = text[p_line].substr(p_char, text[p_line].size());
  6514. substrings.write[0] = text[p_line].substr(0, p_char) + substrings[0];
  6515. substrings.write[substrings.size() - 1] += postinsert_text;
  6516. Vector<Array> bidi_override;
  6517. bidi_override.resize(substrings.size());
  6518. for (int i = 0; i < substrings.size(); i++) {
  6519. bidi_override.write[i] = structured_text_parser(st_parser, st_args, substrings[i]);
  6520. }
  6521. text.insert(p_line, substrings, bidi_override);
  6522. if (shift_first_line) {
  6523. text.move_gutters(p_line, p_line + 1);
  6524. text.set_hidden(p_line + 1, text.is_hidden(p_line));
  6525. text.set_hidden(p_line, false);
  6526. }
  6527. r_end_line = p_line + substrings.size() - 1;
  6528. r_end_column = text[r_end_line].length() - postinsert_text.length();
  6529. 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) ? carets[0].column : 0, r_end_column);
  6530. if (dir != TextServer::DIRECTION_AUTO) {
  6531. input_direction = (TextDirection)dir;
  6532. }
  6533. if (!text_changed_dirty && !setting_text) {
  6534. if (is_inside_tree()) {
  6535. MessageQueue::get_singleton()->push_call(this, "_text_changed_emit");
  6536. }
  6537. text_changed_dirty = true;
  6538. }
  6539. emit_signal(SNAME("lines_edited_from"), p_line, r_end_line);
  6540. }
  6541. String TextEdit::_base_get_text(int p_from_line, int p_from_column, int p_to_line, int p_to_column) const {
  6542. ERR_FAIL_INDEX_V(p_from_line, text.size(), String());
  6543. ERR_FAIL_INDEX_V(p_from_column, text[p_from_line].length() + 1, String());
  6544. ERR_FAIL_INDEX_V(p_to_line, text.size(), String());
  6545. ERR_FAIL_INDEX_V(p_to_column, text[p_to_line].length() + 1, String());
  6546. ERR_FAIL_COND_V(p_to_line < p_from_line, String()); // 'from > to'.
  6547. ERR_FAIL_COND_V(p_to_line == p_from_line && p_to_column < p_from_column, String()); // 'from > to'.
  6548. StringBuilder ret;
  6549. for (int i = p_from_line; i <= p_to_line; i++) {
  6550. int begin = (i == p_from_line) ? p_from_column : 0;
  6551. int end = (i == p_to_line) ? p_to_column : text[i].length();
  6552. if (i > p_from_line) {
  6553. ret += "\n";
  6554. }
  6555. ret += text[i].substr(begin, end - begin);
  6556. }
  6557. return ret.as_string();
  6558. }
  6559. void TextEdit::_base_remove_text(int p_from_line, int p_from_column, int p_to_line, int p_to_column) {
  6560. ERR_FAIL_INDEX(p_from_line, text.size());
  6561. ERR_FAIL_INDEX(p_from_column, text[p_from_line].length() + 1);
  6562. ERR_FAIL_INDEX(p_to_line, text.size());
  6563. ERR_FAIL_INDEX(p_to_column, text[p_to_line].length() + 1);
  6564. ERR_FAIL_COND(p_to_line < p_from_line); // 'from > to'.
  6565. ERR_FAIL_COND(p_to_line == p_from_line && p_to_column < p_from_column); // 'from > to'.
  6566. String pre_text = text[p_from_line].substr(0, p_from_column);
  6567. String post_text = text[p_to_line].substr(p_to_column, text[p_to_line].length());
  6568. text.remove_range(p_from_line, p_to_line);
  6569. text.set(p_from_line, pre_text + post_text, structured_text_parser(st_parser, st_args, pre_text + post_text));
  6570. if (!text_changed_dirty && !setting_text) {
  6571. if (is_inside_tree()) {
  6572. MessageQueue::get_singleton()->push_call(this, "_text_changed_emit");
  6573. }
  6574. text_changed_dirty = true;
  6575. }
  6576. emit_signal(SNAME("lines_edited_from"), p_to_line, p_from_line);
  6577. }
  6578. TextEdit::TextEdit(const String &p_placeholder) {
  6579. placeholder_data_buf.instantiate();
  6580. carets.push_back(Caret());
  6581. clear();
  6582. set_focus_mode(FOCUS_ALL);
  6583. _update_caches();
  6584. set_default_cursor_shape(CURSOR_IBEAM);
  6585. set_process_unhandled_key_input(true);
  6586. text.set_tab_size(text.get_tab_size());
  6587. h_scroll = memnew(HScrollBar);
  6588. v_scroll = memnew(VScrollBar);
  6589. add_child(h_scroll, false, INTERNAL_MODE_FRONT);
  6590. add_child(v_scroll, false, INTERNAL_MODE_FRONT);
  6591. h_scroll->connect("value_changed", callable_mp(this, &TextEdit::_scroll_moved));
  6592. v_scroll->connect("value_changed", callable_mp(this, &TextEdit::_scroll_moved));
  6593. v_scroll->connect("scrolling", callable_mp(this, &TextEdit::_v_scroll_input));
  6594. /* Caret. */
  6595. caret_blink_timer = memnew(Timer);
  6596. add_child(caret_blink_timer, false, INTERNAL_MODE_FRONT);
  6597. caret_blink_timer->set_wait_time(0.65);
  6598. caret_blink_timer->connect("timeout", callable_mp(this, &TextEdit::_toggle_draw_caret));
  6599. set_caret_blink_enabled(false);
  6600. /* Selection. */
  6601. click_select_held = memnew(Timer);
  6602. add_child(click_select_held, false, INTERNAL_MODE_FRONT);
  6603. click_select_held->set_wait_time(0.05);
  6604. click_select_held->connect("timeout", callable_mp(this, &TextEdit::_click_selection_held));
  6605. idle_detect = memnew(Timer);
  6606. add_child(idle_detect, false, INTERNAL_MODE_FRONT);
  6607. idle_detect->set_one_shot(true);
  6608. idle_detect->set_wait_time(GLOBAL_GET("gui/timers/text_edit_idle_detect_sec"));
  6609. idle_detect->connect("timeout", callable_mp(this, &TextEdit::_push_current_op));
  6610. undo_stack_max_size = GLOBAL_GET("gui/common/text_edit_undo_stack_max_size");
  6611. set_placeholder(p_placeholder);
  6612. set_editable(true);
  6613. }