text_edit.cpp 259 KB

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