text_edit.cpp 311 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276
  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 "text_edit.compat.inc"
  32. #include "core/config/project_settings.h"
  33. #include "core/input/input.h"
  34. #include "core/input/input_map.h"
  35. #include "core/object/script_language.h"
  36. #include "core/os/keyboard.h"
  37. #include "core/os/os.h"
  38. #include "core/string/alt_codes.h"
  39. #include "core/string/string_builder.h"
  40. #include "core/string/translation_server.h"
  41. #include "scene/gui/label.h"
  42. #include "scene/main/window.h"
  43. #include "scene/theme/theme_db.h"
  44. ///////////////////////////////////////////////////////////////////////////////
  45. /// TEXT ///
  46. ///////////////////////////////////////////////////////////////////////////////
  47. void TextEdit::Text::set_font(const Ref<Font> &p_font) {
  48. if (font == p_font) {
  49. return;
  50. }
  51. font = p_font;
  52. is_dirty = true;
  53. }
  54. void TextEdit::Text::set_font_size(int p_font_size) {
  55. if (font_size == p_font_size) {
  56. return;
  57. }
  58. font_size = p_font_size;
  59. is_dirty = true;
  60. }
  61. void TextEdit::Text::set_tab_size(int p_tab_size) {
  62. if (tab_size == p_tab_size) {
  63. return;
  64. }
  65. tab_size = p_tab_size;
  66. tab_size_dirty = true;
  67. }
  68. int TextEdit::Text::get_tab_size() const {
  69. return tab_size;
  70. }
  71. void TextEdit::Text::set_indent_wrapped_lines(bool p_enabled) {
  72. if (indent_wrapped_lines == p_enabled) {
  73. return;
  74. }
  75. indent_wrapped_lines = p_enabled;
  76. tab_size_dirty = true;
  77. }
  78. bool TextEdit::Text::is_indent_wrapped_lines() const {
  79. return indent_wrapped_lines;
  80. }
  81. void TextEdit::Text::set_direction_and_language(TextServer::Direction p_direction, const String &p_language) {
  82. if (direction == p_direction && language == p_language) {
  83. return;
  84. }
  85. direction = p_direction;
  86. language = p_language;
  87. is_dirty = true;
  88. }
  89. void TextEdit::Text::set_draw_control_chars(bool p_enabled) {
  90. if (draw_control_chars == p_enabled) {
  91. return;
  92. }
  93. draw_control_chars = p_enabled;
  94. is_dirty = true;
  95. }
  96. void TextEdit::Text::set_inline_object_parser(const Callable &p_parser) {
  97. inline_object_parser = p_parser;
  98. is_dirty = true;
  99. invalidate_all();
  100. }
  101. int TextEdit::Text::get_line_width(int p_line, int p_wrap_index) const {
  102. ERR_FAIL_INDEX_V(p_line, text.size(), 0);
  103. if (p_wrap_index != -1) {
  104. return text[p_line].data_buf->get_line_width(p_wrap_index);
  105. }
  106. return text[p_line].data_buf->get_size().x;
  107. }
  108. int TextEdit::Text::get_max_width() const {
  109. if (max_line_width_dirty) {
  110. int new_max_line_width = 0;
  111. for (const Line &l : text) {
  112. if (l.hidden) {
  113. continue;
  114. }
  115. new_max_line_width = MAX(new_max_line_width, l.width);
  116. }
  117. max_line_width = new_max_line_width;
  118. }
  119. return max_line_width;
  120. }
  121. int TextEdit::Text::get_line_height() const {
  122. if (max_line_height_dirty) {
  123. int new_max_line_height = 0;
  124. for (const Line &l : text) {
  125. if (l.hidden) {
  126. continue;
  127. }
  128. new_max_line_height = MAX(new_max_line_height, l.height);
  129. }
  130. max_line_height = new_max_line_height;
  131. }
  132. return max_line_height;
  133. }
  134. void TextEdit::Text::set_width(float p_width) {
  135. width = p_width;
  136. }
  137. float TextEdit::Text::get_width() const {
  138. return width;
  139. }
  140. void TextEdit::Text::set_brk_flags(BitField<TextServer::LineBreakFlag> p_flags) {
  141. brk_flags = p_flags;
  142. }
  143. BitField<TextServer::LineBreakFlag> TextEdit::Text::get_brk_flags() const {
  144. return brk_flags;
  145. }
  146. int TextEdit::Text::get_line_wrap_amount(int p_line) const {
  147. ERR_FAIL_INDEX_V(p_line, text.size(), 0);
  148. return text[p_line].line_count - 1;
  149. }
  150. Vector<Vector2i> TextEdit::Text::get_line_wrap_ranges(int p_line) const {
  151. Vector<Vector2i> ret;
  152. ERR_FAIL_INDEX_V(p_line, text.size(), ret);
  153. Ref<TextParagraph> data_buf = text[p_line].data_buf;
  154. int line_count = data_buf->get_line_count();
  155. for (int i = 0; i < line_count; i++) {
  156. ret.push_back(data_buf->get_line_range(i));
  157. }
  158. return ret;
  159. }
  160. const Ref<TextParagraph> TextEdit::Text::get_line_data(int p_line) const {
  161. ERR_FAIL_INDEX_V(p_line, text.size(), Ref<TextParagraph>());
  162. return text[p_line].data_buf;
  163. }
  164. float TextEdit::Text::get_indent_offset(int p_line, bool p_rtl) const {
  165. ERR_FAIL_INDEX_V(p_line, text.size(), 0);
  166. Line &text_line = text.write[p_line];
  167. if (text_line.indent_ofs < 0.0) {
  168. int char_count = 0;
  169. int line_length = text_line.data.size();
  170. for (int i = 0; i < line_length - 1; i++) {
  171. if (text_line.data[i] == '\t') {
  172. char_count++;
  173. } else if (text_line.data[i] == ' ') {
  174. char_count++;
  175. } else {
  176. break;
  177. }
  178. }
  179. RID text_rid = text_line.data_buf->get_line_rid(0);
  180. float offset = (p_rtl) ? TS->shaped_text_get_size(text_rid).x : 0;
  181. Vector<Vector2> sel = TS->shaped_text_get_selection(text_rid, 0, char_count);
  182. for (const Vector2 v : sel) {
  183. if (p_rtl) {
  184. offset = MIN(v.x, MIN(v.y, offset));
  185. } else {
  186. offset = MAX(v.x, MAX(v.y, offset));
  187. }
  188. }
  189. text_line.indent_ofs = (p_rtl) ? TS->shaped_text_get_size(text_rid).x - offset : offset;
  190. }
  191. return text_line.indent_ofs;
  192. }
  193. _FORCE_INLINE_ const String &TextEdit::Text::operator[](int p_line) const {
  194. static const String empty;
  195. ERR_FAIL_INDEX_V(p_line, text.size(), empty);
  196. return text[p_line].data;
  197. }
  198. _FORCE_INLINE_ const String &TextEdit::Text::get_text_with_ime(int p_line) const {
  199. if (!text[p_line].ime_data.is_empty()) {
  200. return text[p_line].ime_data;
  201. } else {
  202. return text[p_line].data;
  203. }
  204. }
  205. const Vector<RID> TextEdit::Text::get_accessibility_elements(int p_line) {
  206. ERR_FAIL_INDEX_V(p_line, text.size(), Vector<RID>());
  207. return text[p_line].accessibility_text_root_element;
  208. }
  209. void TextEdit::Text::update_accessibility(int p_line, RID p_root) {
  210. ERR_FAIL_INDEX(p_line, text.size());
  211. Line &l = text.write[p_line];
  212. if (l.accessibility_text_root_element.is_empty()) {
  213. for (int i = 0; i < l.data_buf->get_line_count(); i++) {
  214. RID rid = DisplayServer::get_singleton()->accessibility_create_sub_text_edit_elements(p_root, l.data_buf->get_line_rid(i), max_line_height, p_line);
  215. l.accessibility_text_root_element.push_back(rid);
  216. }
  217. }
  218. }
  219. inline bool is_inline_info_valid(const Variant &p_info) {
  220. if (p_info.get_type() != Variant::DICTIONARY) {
  221. return false;
  222. }
  223. Dictionary info = p_info;
  224. if (!info.get_valid("column").is_num() || !info.get_valid("width_ratio").is_num()) {
  225. return false;
  226. }
  227. return true;
  228. }
  229. void TextEdit::Text::invalidate_cache(int p_line, bool p_text_changed) {
  230. ERR_FAIL_INDEX(p_line, text.size());
  231. Line &l = text.write[p_line];
  232. for (const RID rid : l.accessibility_text_root_element) {
  233. if (rid.is_valid()) {
  234. DisplayServer::get_singleton()->accessibility_free_element(rid);
  235. }
  236. }
  237. l.accessibility_text_root_element.clear();
  238. if (font.is_null()) {
  239. return; // Not in tree?
  240. }
  241. Line &text_line = text.write[p_line];
  242. if (p_text_changed) {
  243. text_line.data_buf->clear();
  244. }
  245. BitField<TextServer::LineBreakFlag> flags = brk_flags;
  246. if (indent_wrapped_lines) {
  247. flags.set_flag(TextServer::BREAK_TRIM_INDENT);
  248. }
  249. text_line.data_buf->set_width(width);
  250. text_line.data_buf->set_direction((TextServer::Direction)direction);
  251. text_line.data_buf->set_break_flags(flags);
  252. text_line.data_buf->set_preserve_control(draw_control_chars);
  253. text_line.data_buf->set_custom_punctuation(get_enabled_word_separators());
  254. text_line.indent_ofs = -1.0;
  255. const String &text_with_ime = (!text_line.ime_data.is_empty()) ? text_line.ime_data : text_line.data;
  256. const Array &bidi_override_with_ime = (!text_line.ime_data.is_empty()) ? text_line.ime_bidi_override : text_line.bidi_override;
  257. if (p_text_changed) {
  258. int from = 0;
  259. if (inline_object_parser.is_valid()) {
  260. // Insert inline object.
  261. Variant parsed_result = inline_object_parser.call(text_with_ime);
  262. if (parsed_result.is_array()) {
  263. Array object_infos = parsed_result;
  264. for (Variant val : object_infos) {
  265. if (!is_inline_info_valid(val)) {
  266. continue;
  267. }
  268. Dictionary info = val;
  269. int start = info["column"];
  270. float width_ratio = info["width_ratio"];
  271. String left_string = text_with_ime.substr(from, start - from);
  272. text_line.data_buf->add_string(left_string, font, font_size, language);
  273. text_line.data_buf->add_object(info, Vector2(font_height * width_ratio, font_height), INLINE_ALIGNMENT_TOP, 0);
  274. from = start;
  275. }
  276. }
  277. }
  278. String remaining_string = text_with_ime.substr(from);
  279. text_line.data_buf->add_string(remaining_string, font, font_size, language);
  280. } else {
  281. // Update inline object sizes.
  282. for (int i = 0; i < text_line.data_buf->get_line_count(); i++) {
  283. for (Variant key : text_line.data_buf->get_line_objects(i)) {
  284. if (!is_inline_info_valid(key)) {
  285. continue;
  286. }
  287. Dictionary info = key;
  288. float width_ratio = info["width_ratio"];
  289. text_line.data_buf->resize_object(info, Vector2(font_height * width_ratio, font_height), INLINE_ALIGNMENT_TOP, 0);
  290. }
  291. }
  292. }
  293. if (!bidi_override_with_ime.is_empty()) {
  294. TS->shaped_text_set_bidi_override(text_line.data_buf->get_rid(), bidi_override_with_ime);
  295. }
  296. if (!p_text_changed) {
  297. RID r = text_line.data_buf->get_rid();
  298. int spans = TS->shaped_get_span_count(r);
  299. for (int i = 0; i < spans; i++) {
  300. TS->shaped_set_span_update_font(r, i, font->get_rids(), font_size, font->get_opentype_features());
  301. }
  302. }
  303. // Apply tab align.
  304. if (tab_size > 0) {
  305. Vector<float> tabs;
  306. tabs.push_back(font->get_char_size(' ', font_size).width * tab_size);
  307. text_line.data_buf->tab_align(tabs);
  308. }
  309. // Update wrap amount.
  310. const int old_line_count = text_line.line_count;
  311. text_line.line_count = text_line.data_buf->get_line_count();
  312. if (!text_line.hidden && text_line.line_count != old_line_count) {
  313. total_visible_line_count += text_line.line_count - old_line_count;
  314. }
  315. // Update height.
  316. const int old_height = text_line.height;
  317. text_line.height = font_height;
  318. for (int i = 0; i < text_line.line_count; i++) {
  319. text_line.height = MAX(text_line.height, text_line.data_buf->get_line_size(i).y);
  320. }
  321. // If this line has shrunk, this may no longer be the tallest line.
  322. if (!text_line.hidden) {
  323. if (old_height == max_line_height && text_line.height < old_height) {
  324. max_line_height_dirty = true;
  325. } else {
  326. max_line_height = MAX(text_line.height, max_line_height);
  327. }
  328. }
  329. // Update width.
  330. const int old_width = text_line.width;
  331. text_line.width = get_line_width(p_line);
  332. if (!text_line.hidden) {
  333. // If this line has shrunk, this may no longer be the longest line.
  334. if (old_width == max_line_width && text_line.width < old_width) {
  335. max_line_width_dirty = true;
  336. } else {
  337. max_line_width = MAX(text_line.width, max_line_width);
  338. }
  339. }
  340. }
  341. void TextEdit::Text::invalidate_all_lines() {
  342. for (int i = 0; i < text.size(); i++) {
  343. if (tab_size_dirty) {
  344. if (tab_size > 0) {
  345. Vector<float> tabs;
  346. tabs.push_back(font->get_char_size(' ', font_size).width * tab_size);
  347. text[i].data_buf->tab_align(tabs);
  348. }
  349. }
  350. invalidate_cache(i, false);
  351. }
  352. tab_size_dirty = false;
  353. }
  354. void TextEdit::Text::invalidate_font() {
  355. if (!is_dirty) {
  356. return;
  357. }
  358. max_line_width_dirty = true;
  359. max_line_height_dirty = true;
  360. if (font.is_valid() && font_size > 0) {
  361. font_height = font->get_height(font_size);
  362. }
  363. for (int i = 0; i < text.size(); i++) {
  364. invalidate_cache(i, false);
  365. }
  366. is_dirty = false;
  367. }
  368. void TextEdit::Text::invalidate_all() {
  369. if (!is_dirty) {
  370. return;
  371. }
  372. max_line_width_dirty = true;
  373. max_line_height_dirty = true;
  374. if (font.is_valid() && font_size > 0) {
  375. font_height = font->get_height(font_size);
  376. }
  377. for (int i = 0; i < text.size(); i++) {
  378. invalidate_cache(i, true);
  379. }
  380. is_dirty = false;
  381. }
  382. void TextEdit::Text::clear() {
  383. text.clear();
  384. max_line_width_dirty = true;
  385. max_line_height_dirty = true;
  386. total_visible_line_count = 0;
  387. Line line;
  388. line.gutters.resize(gutter_count);
  389. text.insert(0, line);
  390. invalidate_cache(0, true);
  391. }
  392. int TextEdit::Text::get_total_visible_line_count() const {
  393. return total_visible_line_count;
  394. }
  395. void TextEdit::Text::set(int p_line, const String &p_text, const Array &p_bidi_override) {
  396. ERR_FAIL_INDEX(p_line, text.size());
  397. text.write[p_line].data = p_text;
  398. text.write[p_line].ime_data = String();
  399. text.write[p_line].bidi_override = p_bidi_override;
  400. text.write[p_line].ime_bidi_override.clear();
  401. invalidate_cache(p_line, true);
  402. }
  403. void TextEdit::Text::set_ime(int p_line, const String &p_text, const Array &p_bidi_override) {
  404. ERR_FAIL_INDEX(p_line, text.size());
  405. text.write[p_line].ime_data = p_text;
  406. text.write[p_line].ime_bidi_override = p_bidi_override;
  407. invalidate_cache(p_line, true);
  408. }
  409. void TextEdit::Text::set_hidden(int p_line, bool p_hidden) {
  410. ERR_FAIL_INDEX(p_line, text.size());
  411. Line &text_line = text.write[p_line];
  412. if (text_line.hidden == p_hidden) {
  413. return;
  414. }
  415. text_line.hidden = p_hidden;
  416. if (p_hidden) {
  417. total_visible_line_count -= text_line.line_count;
  418. if (text_line.width == max_line_width) {
  419. max_line_width_dirty = true;
  420. }
  421. if (text_line.height == max_line_height) {
  422. max_line_height_dirty = true;
  423. }
  424. } else {
  425. total_visible_line_count += text_line.line_count;
  426. max_line_width = MAX(text_line.width, max_line_width);
  427. max_line_height = MAX(text_line.height, max_line_height);
  428. }
  429. }
  430. bool TextEdit::Text::is_hidden(int p_line) const {
  431. ERR_FAIL_INDEX_V(p_line, text.size(), true);
  432. return text[p_line].hidden;
  433. }
  434. void TextEdit::Text::insert(int p_at, const Vector<String> &p_text, const Vector<Array> &p_bidi_override) {
  435. ERR_FAIL_INDEX(p_at, text.size() + 1);
  436. int new_line_count = p_text.size() - 1;
  437. if (new_line_count > 0) {
  438. text.resize(text.size() + new_line_count);
  439. for (int i = (text.size() - 1); i > p_at; i--) {
  440. if ((i - new_line_count) <= 0) {
  441. break;
  442. }
  443. text.write[i] = text[i - new_line_count];
  444. }
  445. }
  446. for (int i = 0; i < p_text.size(); i++) {
  447. if (i == 0) {
  448. set(p_at + i, p_text[i], p_bidi_override[i]);
  449. continue;
  450. }
  451. Line line;
  452. line.gutters.resize(gutter_count);
  453. line.data = p_text[i];
  454. line.bidi_override = p_bidi_override[i];
  455. text.write[p_at + i] = line;
  456. invalidate_cache(p_at + i, true);
  457. }
  458. }
  459. void TextEdit::Text::remove_range(int p_from_line, int p_to_line) {
  460. if (p_from_line == p_to_line) {
  461. return;
  462. }
  463. for (int i = p_from_line + 1; i <= p_to_line; i++) {
  464. const Line &text_line = text[i];
  465. if (text_line.hidden) {
  466. continue;
  467. }
  468. if (text_line.height == max_line_height) {
  469. max_line_height_dirty = true;
  470. }
  471. if (text_line.width == max_line_width) {
  472. max_line_width_dirty = true;
  473. }
  474. total_visible_line_count -= text_line.line_count;
  475. }
  476. int diff = p_to_line - p_from_line;
  477. for (int i = p_to_line + 1; i < text.size(); i++) {
  478. text.write[i - diff] = text[i];
  479. }
  480. text.resize(text.size() - diff);
  481. ERR_FAIL_COND(total_visible_line_count < 0); // BUG
  482. }
  483. void TextEdit::Text::add_gutter(int p_at) {
  484. for (int i = 0; i < text.size(); i++) {
  485. if (p_at < 0 || p_at > gutter_count) {
  486. text.write[i].gutters.push_back(Gutter());
  487. } else {
  488. text.write[i].gutters.insert(p_at, Gutter());
  489. }
  490. }
  491. gutter_count++;
  492. }
  493. void TextEdit::Text::remove_gutter(int p_gutter) {
  494. ERR_FAIL_INDEX(p_gutter, text.size());
  495. for (int i = 0; i < text.size(); i++) {
  496. text.write[i].gutters.remove_at(p_gutter);
  497. }
  498. gutter_count--;
  499. }
  500. void TextEdit::Text::move_gutters(int p_from_line, int p_to_line) {
  501. ERR_FAIL_INDEX(p_from_line, text.size());
  502. ERR_FAIL_INDEX(p_to_line, text.size());
  503. text.write[p_to_line].gutters = text[p_from_line].gutters;
  504. text.write[p_from_line].gutters.clear();
  505. text.write[p_from_line].gutters.resize(gutter_count);
  506. }
  507. void TextEdit::Text::set_use_default_word_separators(bool p_enabled) {
  508. if (use_default_word_separators == p_enabled) {
  509. return;
  510. }
  511. use_default_word_separators = p_enabled;
  512. invalidate_all_lines();
  513. }
  514. void TextEdit::Text::set_use_custom_word_separators(bool p_enabled) {
  515. if (use_custom_word_separators == p_enabled) {
  516. return;
  517. }
  518. use_custom_word_separators = p_enabled;
  519. invalidate_all_lines();
  520. }
  521. bool TextEdit::Text::is_default_word_separators_enabled() const {
  522. return use_default_word_separators;
  523. }
  524. bool TextEdit::Text::is_custom_word_separators_enabled() const {
  525. return use_custom_word_separators;
  526. }
  527. String TextEdit::Text::get_custom_word_separators() const {
  528. return custom_word_separators;
  529. }
  530. String TextEdit::Text::get_default_word_separators() const {
  531. String concat_separators = "!\"#$%&'()*+,-./:;<=>?@[\\]^`{|}~";
  532. for (char32_t ch = 0x2000; ch <= 0x206F; ++ch) { // General punctuation block.
  533. concat_separators += ch;
  534. }
  535. for (char32_t ch = 0x3000; ch <= 0x303F; ++ch) { // CJK punctuation block.
  536. concat_separators += ch;
  537. }
  538. return concat_separators;
  539. }
  540. // Get default and/or custom word separators depending on the option enabled.
  541. String TextEdit::Text::get_enabled_word_separators() const {
  542. String all_separators;
  543. if (use_default_word_separators) {
  544. all_separators += get_default_word_separators();
  545. }
  546. if (use_custom_word_separators) {
  547. all_separators += get_custom_word_separators();
  548. }
  549. return all_separators;
  550. }
  551. ///////////////////////////////////////////////////////////////////////////////
  552. /// TEXT EDIT ///
  553. ///////////////////////////////////////////////////////////////////////////////
  554. void TextEdit::_accessibility_action_set_selection(const Variant &p_data) {
  555. Dictionary new_selection = p_data;
  556. RID sel_start = new_selection["start_element"];
  557. Vector2i sel_start_line = DisplayServer::get_singleton()->accessibility_element_get_meta(sel_start);
  558. int sel_start_pos = new_selection["start_char"];
  559. RID sel_end = new_selection["end_element"];
  560. Vector2i sel_end_line = DisplayServer::get_singleton()->accessibility_element_get_meta(sel_end);
  561. int sel_end_pos = new_selection["end_char"];
  562. remove_secondary_carets();
  563. select(sel_start_line.x, sel_start_pos, sel_end_line.x, sel_end_pos, 0);
  564. }
  565. void TextEdit::_accessibility_action_replace_selected(const Variant &p_data) {
  566. String new_text = p_data;
  567. insert_text_at_caret(new_text);
  568. }
  569. void TextEdit::_accessibility_action_set_value(const Variant &p_data) {
  570. String new_text = p_data;
  571. set_text(new_text);
  572. }
  573. void TextEdit::_accessibility_action_menu(const Variant &p_data) {
  574. if (context_menu_enabled) {
  575. _update_context_menu();
  576. adjust_viewport_to_caret();
  577. menu->set_position(get_screen_position() + get_caret_draw_pos());
  578. menu->reset_size();
  579. menu->popup();
  580. menu->grab_focus();
  581. }
  582. }
  583. void TextEdit::_accessibility_scroll_down(const Variant &p_data) {
  584. if ((DisplayServer::AccessibilityScrollUnit)p_data == DisplayServer::SCROLL_UNIT_ITEM) {
  585. v_scroll->set_value(v_scroll->get_value() + v_scroll->get_page() / 4);
  586. } else {
  587. v_scroll->set_value(v_scroll->get_value() + v_scroll->get_page());
  588. }
  589. queue_accessibility_update();
  590. }
  591. void TextEdit::_accessibility_scroll_left(const Variant &p_data) {
  592. if ((DisplayServer::AccessibilityScrollUnit)p_data == DisplayServer::SCROLL_UNIT_ITEM) {
  593. h_scroll->set_value(h_scroll->get_value() - h_scroll->get_page() / 4);
  594. } else {
  595. h_scroll->set_value(h_scroll->get_value() - h_scroll->get_page());
  596. }
  597. queue_accessibility_update();
  598. }
  599. void TextEdit::_accessibility_scroll_right(const Variant &p_data) {
  600. if ((DisplayServer::AccessibilityScrollUnit)p_data == DisplayServer::SCROLL_UNIT_ITEM) {
  601. h_scroll->set_value(h_scroll->get_value() + h_scroll->get_page() / 4);
  602. } else {
  603. h_scroll->set_value(h_scroll->get_value() + h_scroll->get_page());
  604. }
  605. queue_accessibility_update();
  606. }
  607. void TextEdit::_accessibility_scroll_up(const Variant &p_data) {
  608. if ((DisplayServer::AccessibilityScrollUnit)p_data == DisplayServer::SCROLL_UNIT_ITEM) {
  609. v_scroll->set_value(v_scroll->get_value() - v_scroll->get_page() / 4);
  610. } else {
  611. v_scroll->set_value(v_scroll->get_value() - v_scroll->get_page());
  612. }
  613. queue_accessibility_update();
  614. }
  615. void TextEdit::_accessibility_scroll_set(const Variant &p_data) {
  616. const Point2 &pos = p_data;
  617. h_scroll->set_value(pos.x);
  618. v_scroll->set_value(pos.y);
  619. queue_accessibility_update();
  620. }
  621. void TextEdit::_accessibility_action_scroll_into_view(const Variant &p_data, int p_line, int p_wrap) {
  622. double delta = get_scroll_pos_for_line(p_line, p_wrap) - get_v_scroll();
  623. if (delta < 0) {
  624. _scroll_up(-delta, false);
  625. } else {
  626. _scroll_down(delta, false);
  627. }
  628. }
  629. void TextEdit::_notification(int p_what) {
  630. switch (p_what) {
  631. case NOTIFICATION_EXIT_TREE:
  632. case NOTIFICATION_ACCESSIBILITY_INVALIDATE: {
  633. text.clear_accessibility();
  634. accessibility_text_root_element_nl = RID();
  635. } break;
  636. case NOTIFICATION_ACCESSIBILITY_UPDATE: {
  637. RID ae = get_accessibility_element();
  638. ERR_FAIL_COND(ae.is_null());
  639. DisplayServer::get_singleton()->accessibility_update_set_role(ae, DisplayServer::AccessibilityRole::ROLE_MULTILINE_TEXT_FIELD);
  640. if (text.size() == 1 && text[0].is_empty()) {
  641. DisplayServer::get_singleton()->accessibility_update_set_placeholder(ae, atr(placeholder_text));
  642. }
  643. if (!placeholder_text.is_empty() && get_accessibility_name().is_empty()) {
  644. DisplayServer::get_singleton()->accessibility_update_set_name(ae, atr(placeholder_text));
  645. }
  646. DisplayServer::get_singleton()->accessibility_update_set_flag(ae, DisplayServer::AccessibilityFlags::FLAG_READONLY, !editable);
  647. DisplayServer::get_singleton()->accessibility_update_add_action(ae, DisplayServer::AccessibilityAction::ACTION_SET_TEXT_SELECTION, callable_mp(this, &TextEdit::_accessibility_action_set_selection));
  648. DisplayServer::get_singleton()->accessibility_update_add_action(ae, DisplayServer::AccessibilityAction::ACTION_REPLACE_SELECTED_TEXT, callable_mp(this, &TextEdit::_accessibility_action_replace_selected));
  649. DisplayServer::get_singleton()->accessibility_update_add_action(ae, DisplayServer::AccessibilityAction::ACTION_SET_VALUE, callable_mp(this, &TextEdit::_accessibility_action_set_value));
  650. DisplayServer::get_singleton()->accessibility_update_add_action(ae, DisplayServer::AccessibilityAction::ACTION_SHOW_CONTEXT_MENU, callable_mp(this, &TextEdit::_accessibility_action_menu));
  651. DisplayServer::get_singleton()->accessibility_update_add_action(ae, DisplayServer::AccessibilityAction::ACTION_SCROLL_DOWN, callable_mp(this, &TextEdit::_accessibility_scroll_down));
  652. DisplayServer::get_singleton()->accessibility_update_add_action(ae, DisplayServer::AccessibilityAction::ACTION_SCROLL_LEFT, callable_mp(this, &TextEdit::_accessibility_scroll_left));
  653. DisplayServer::get_singleton()->accessibility_update_add_action(ae, DisplayServer::AccessibilityAction::ACTION_SCROLL_RIGHT, callable_mp(this, &TextEdit::_accessibility_scroll_right));
  654. DisplayServer::get_singleton()->accessibility_update_add_action(ae, DisplayServer::AccessibilityAction::ACTION_SCROLL_UP, callable_mp(this, &TextEdit::_accessibility_scroll_up));
  655. DisplayServer::get_singleton()->accessibility_update_add_action(ae, DisplayServer::AccessibilityAction::ACTION_SET_SCROLL_OFFSET, callable_mp(this, &TextEdit::_accessibility_scroll_set));
  656. int first_vis_line = get_first_visible_line();
  657. int row_height = get_line_height();
  658. int xmargin_beg = Math::ceil(theme_cache.style_normal->get_margin(SIDE_LEFT)) + gutters_width + gutter_padding;
  659. Size2 size = get_size();
  660. bool rtl = is_layout_rtl();
  661. int lines_drawn = 0;
  662. RID selection_start;
  663. RID selection_end;
  664. for (int i = 0; i < text.size(); i++) {
  665. text.update_accessibility(i, ae);
  666. const Ref<TextParagraph> &ac_buf = text.get_line_data(i);
  667. const Vector<RID> &text_aes = text.get_accessibility_elements(i);
  668. int first_indent_line = 0;
  669. float indent_ofs = 0.0;
  670. if (text.is_indent_wrapped_lines()) {
  671. _get_wrapped_indent_level(i, first_indent_line);
  672. indent_ofs = MIN(text.get_indent_offset(i, rtl), wrap_at_column * 0.6);
  673. }
  674. for (int j = 0; j < text_aes.size(); j++) {
  675. float text_off_x = 0.0;
  676. float text_off_y = 0.0;
  677. if (!editable) {
  678. text_off_x = theme_cache.style_readonly->get_offset().x / 2;
  679. text_off_x -= theme_cache.style_normal->get_offset().x / 2;
  680. text_off_y = theme_cache.style_readonly->get_offset().y / 2;
  681. } else {
  682. text_off_y = theme_cache.style_normal->get_offset().y / 2;
  683. }
  684. text_off_y += (lines_drawn + j) * row_height + theme_cache.line_spacing / 2;
  685. text_off_y -= (first_vis_line + first_visible_line_wrap_ofs) * row_height;
  686. text_off_y -= _get_v_scroll_offset() * row_height;
  687. float wrap_indent = j > first_indent_line ? indent_ofs : 0.0;
  688. int char_margin = xmargin_beg - first_visible_col;
  689. if (rtl) {
  690. char_margin = size.width - char_margin - ac_buf->get_line_width(j) - wrap_indent;
  691. } else {
  692. char_margin += wrap_indent;
  693. }
  694. DisplayServer::get_singleton()->accessibility_update_set_flag(text_aes[j], DisplayServer::AccessibilityFlags::FLAG_HIDDEN, _is_line_hidden(i));
  695. Transform2D tr;
  696. tr.set_origin(Point2(char_margin + text_off_x, text_off_y));
  697. DisplayServer::get_singleton()->accessibility_update_set_transform(text_aes[j], tr);
  698. DisplayServer::get_singleton()->accessibility_update_set_name(text_aes[j], vformat(RTR("Line %d"), i));
  699. DisplayServer::get_singleton()->accessibility_element_set_meta(text_aes[j], Vector2i(i, j));
  700. DisplayServer::get_singleton()->accessibility_update_add_action(text_aes[j], DisplayServer::AccessibilityAction::ACTION_SCROLL_INTO_VIEW, callable_mp(this, &TextEdit::_accessibility_action_scroll_into_view).bind(i, j));
  701. }
  702. lines_drawn += ac_buf->get_line_count();
  703. }
  704. if (accessibility_text_root_element_nl.is_null()) {
  705. accessibility_text_root_element_nl = DisplayServer::get_singleton()->accessibility_create_sub_text_edit_elements(ae, RID(), get_line_height());
  706. }
  707. // Selection.
  708. if (carets.size() > 0) {
  709. if (carets[0].selection.active) {
  710. int start_wrap = get_line_wrap_index_at_column(carets[0].selection.origin_line, carets[0].selection.origin_column);
  711. RID start_rid = text.get_accessibility_elements(carets[0].selection.origin_line)[start_wrap];
  712. int end_wrap = get_line_wrap_index_at_column(carets[0].line, carets[0].column);
  713. RID end_rid = text.get_accessibility_elements(carets[0].line)[end_wrap];
  714. DisplayServer::get_singleton()->accessibility_update_set_text_selection(ae, start_rid, carets[0].selection.origin_column, end_rid, carets[0].column);
  715. } else {
  716. int caret_wrap = get_line_wrap_index_at_column(carets[0].line, carets[0].column);
  717. RID caret_rid = text.get_accessibility_elements(carets[0].line)[caret_wrap];
  718. DisplayServer::get_singleton()->accessibility_update_set_text_selection(ae, caret_rid, carets[0].column, caret_rid, carets[0].column);
  719. }
  720. }
  721. } break;
  722. case NOTIFICATION_POSTINITIALIZE: {
  723. _update_caches();
  724. } break;
  725. case NOTIFICATION_ENTER_TREE: {
  726. _update_caches();
  727. if (caret_pos_dirty) {
  728. callable_mp(this, &TextEdit::_emit_caret_changed).call_deferred();
  729. }
  730. if (text_changed_dirty) {
  731. callable_mp(this, &TextEdit::_emit_text_changed).call_deferred();
  732. }
  733. _update_wrap_at_column(true);
  734. } break;
  735. case NOTIFICATION_RESIZED: {
  736. _update_scrollbars();
  737. _update_wrap_at_column();
  738. } break;
  739. case NOTIFICATION_VISIBILITY_CHANGED: {
  740. if (is_visible()) {
  741. callable_mp(this, &TextEdit::_update_scrollbars).call_deferred();
  742. callable_mp(this, &TextEdit::_update_wrap_at_column).call_deferred(false);
  743. }
  744. } break;
  745. case NOTIFICATION_LAYOUT_DIRECTION_CHANGED:
  746. case NOTIFICATION_TRANSLATION_CHANGED:
  747. case NOTIFICATION_THEME_CHANGED: {
  748. if (is_inside_tree()) {
  749. _update_caches();
  750. _update_wrap_at_column(true);
  751. }
  752. } break;
  753. case NOTIFICATION_WM_WINDOW_FOCUS_IN: {
  754. window_has_focus = true;
  755. draw_caret = true;
  756. queue_redraw();
  757. } break;
  758. case NOTIFICATION_WM_WINDOW_FOCUS_OUT: {
  759. window_has_focus = false;
  760. draw_caret = false;
  761. queue_redraw();
  762. } break;
  763. case NOTIFICATION_INTERNAL_PROCESS: {
  764. if (scrolling && get_v_scroll() != target_v_scroll) {
  765. double target_y = target_v_scroll - get_v_scroll();
  766. double dist = std::abs(target_y);
  767. // To ensure minimap is responsive override the speed setting.
  768. double vel = ((target_y / dist) * ((minimap_clicked) ? 3000 : v_scroll_speed)) * get_process_delta_time();
  769. // Prevent small velocities from blocking scrolling.
  770. if (Math::abs(vel) < v_scroll->get_step()) {
  771. vel = v_scroll->get_step() * SIGN(vel);
  772. }
  773. if (Math::abs(vel) >= dist) {
  774. set_v_scroll(target_v_scroll);
  775. scrolling = false;
  776. minimap_clicked = false;
  777. set_process_internal(false);
  778. } else {
  779. set_v_scroll(get_v_scroll() + vel);
  780. }
  781. } else {
  782. scrolling = false;
  783. minimap_clicked = false;
  784. set_process_internal(false);
  785. }
  786. } break;
  787. case NOTIFICATION_DRAW: {
  788. if (first_draw) {
  789. // Size may not be the final one, so attempts to ensure caret was visible may have failed.
  790. adjust_viewport_to_caret();
  791. first_draw = false;
  792. }
  793. /* Prevent the resource getting lost between the editor and game. */
  794. if (Engine::get_singleton()->is_editor_hint()) {
  795. if (syntax_highlighter.is_valid() && syntax_highlighter->get_text_edit() != this) {
  796. syntax_highlighter->set_text_edit(this);
  797. }
  798. }
  799. Size2 size = get_size();
  800. bool rtl = is_layout_rtl();
  801. if ((!has_focus() && !(menu && menu->has_focus())) || !window_has_focus) {
  802. draw_caret = false;
  803. }
  804. _update_scrollbars();
  805. RID ci = get_canvas_item();
  806. int xmargin_beg = Math::ceil(theme_cache.style_normal->get_margin(SIDE_LEFT)) + gutters_width + gutter_padding;
  807. int xmargin_end = size.width - Math::ceil(theme_cache.style_normal->get_margin(SIDE_RIGHT));
  808. if (draw_minimap) {
  809. xmargin_end -= minimap_width;
  810. }
  811. // Let's do it easy for now.
  812. theme_cache.style_normal->draw(ci, Rect2(Point2(), size));
  813. if (!editable) {
  814. theme_cache.style_readonly->draw(ci, Rect2(Point2(), size));
  815. draw_caret = is_drawing_caret_when_editable_disabled();
  816. }
  817. if (has_focus()) {
  818. theme_cache.style_focus->draw(ci, Rect2(Point2(), size));
  819. }
  820. int visible_rows = get_visible_line_count() + 1;
  821. if (theme_cache.background_color.a > 0.01) {
  822. RenderingServer::get_singleton()->canvas_item_add_rect(ci, Rect2(Point2i(), get_size()), theme_cache.background_color);
  823. }
  824. Vector<BraceMatchingData> brace_matching;
  825. if (highlight_matching_braces_enabled) {
  826. brace_matching.resize(get_caret_count());
  827. for (int caret = 0; caret < get_caret_count(); caret++) {
  828. BraceMatchingData &brace_match = brace_matching.write[caret];
  829. if (get_caret_line(caret) < 0 || get_caret_line(caret) >= text.size() || get_caret_column(caret) < 0) {
  830. continue;
  831. }
  832. if (get_caret_column(caret) < text[get_caret_line(caret)].length()) {
  833. // Check for open.
  834. char32_t c = text[get_caret_line(caret)][get_caret_column(caret)];
  835. char32_t closec = 0;
  836. if (c == '[') {
  837. closec = ']';
  838. } else if (c == '{') {
  839. closec = '}';
  840. } else if (c == '(') {
  841. closec = ')';
  842. }
  843. if (closec != 0) {
  844. int stack = 1;
  845. for (int i = get_caret_line(caret); i < text.size(); i++) {
  846. int from = i == get_caret_line(caret) ? get_caret_column(caret) + 1 : 0;
  847. for (int j = from; j < text[i].length(); j++) {
  848. char32_t cc = text[i][j];
  849. // Ignore any brackets inside a string.
  850. if (cc == '"' || cc == '\'') {
  851. char32_t quotation = cc;
  852. do {
  853. j++;
  854. if (!(j < text[i].length())) {
  855. break;
  856. }
  857. cc = text[i][j];
  858. // Skip over escaped quotation marks inside strings.
  859. if (cc == '\\') {
  860. bool escaped = true;
  861. while (j + 1 < text[i].length() && text[i][j + 1] == '\\') {
  862. escaped = !escaped;
  863. j++;
  864. }
  865. if (escaped) {
  866. j++;
  867. continue;
  868. }
  869. }
  870. } while (cc != quotation);
  871. } else if (cc == c) {
  872. stack++;
  873. } else if (cc == closec) {
  874. stack--;
  875. }
  876. if (stack == 0) {
  877. brace_match.open_match_line = i;
  878. brace_match.open_match_column = j;
  879. brace_match.open_matching = true;
  880. break;
  881. }
  882. }
  883. if (brace_match.open_match_line != -1) {
  884. break;
  885. }
  886. }
  887. if (!brace_match.open_matching) {
  888. brace_match.open_mismatch = true;
  889. }
  890. }
  891. }
  892. if (get_caret_column(caret) > 0) {
  893. char32_t c = text[get_caret_line(caret)][get_caret_column(caret) - 1];
  894. char32_t closec = 0;
  895. if (c == ']') {
  896. closec = '[';
  897. } else if (c == '}') {
  898. closec = '{';
  899. } else if (c == ')') {
  900. closec = '(';
  901. }
  902. if (closec != 0) {
  903. int stack = 1;
  904. for (int i = get_caret_line(caret); i >= 0; i--) {
  905. int from = i == get_caret_line(caret) ? get_caret_column(caret) - 2 : text[i].length() - 1;
  906. for (int j = from; j >= 0; j--) {
  907. char32_t cc = text[i][j];
  908. // Ignore any brackets inside a string.
  909. if (cc == '"' || cc == '\'') {
  910. char32_t quotation = cc;
  911. do {
  912. j--;
  913. if (!(j >= 0)) {
  914. break;
  915. }
  916. cc = text[i][j];
  917. // Skip over escaped quotation marks inside strings.
  918. if (cc == quotation) {
  919. bool escaped = false;
  920. while (j - 1 >= 0 && text[i][j - 1] == '\\') {
  921. escaped = !escaped;
  922. j--;
  923. }
  924. if (escaped) {
  925. cc = '\\';
  926. continue;
  927. }
  928. }
  929. } while (cc != quotation);
  930. } else if (cc == c) {
  931. stack++;
  932. } else if (cc == closec) {
  933. stack--;
  934. }
  935. if (stack == 0) {
  936. brace_match.close_match_line = i;
  937. brace_match.close_match_column = j;
  938. brace_match.close_matching = true;
  939. break;
  940. }
  941. }
  942. if (brace_match.close_match_line != -1) {
  943. break;
  944. }
  945. }
  946. if (!brace_match.close_matching) {
  947. brace_match.close_mismatch = true;
  948. }
  949. }
  950. }
  951. }
  952. }
  953. bool draw_placeholder = _using_placeholder();
  954. // Get the highlighted words.
  955. String highlighted_text = get_selected_text(0);
  956. // Check if highlighted words contain only whitespaces (tabs or spaces).
  957. bool only_whitespaces_highlighted = highlighted_text.strip_edges().is_empty();
  958. Vector<Pair<int, int>> highlighted_lines;
  959. highlighted_lines.resize(carets.size());
  960. Vector<int> carets_wrap_index;
  961. carets_wrap_index.resize(carets.size());
  962. for (int i = 0; i < carets.size(); i++) {
  963. carets.write[i].visible = false;
  964. int wrap_index = get_caret_wrap_index(i);
  965. highlighted_lines.write[i] = Pair<int, int>(get_caret_line(i), wrap_index);
  966. carets_wrap_index.write[i] = wrap_index;
  967. }
  968. int first_vis_line = get_first_visible_line() - 1;
  969. int draw_amount = visible_rows + 1;
  970. draw_amount += draw_placeholder ? placeholder_wrapped_rows.size() - 1 : get_line_wrap_count(first_vis_line + 1);
  971. // Draw minimap.
  972. if (draw_minimap) {
  973. int minimap_visible_lines = get_minimap_visible_lines();
  974. int minimap_line_height = (minimap_char_size.y + minimap_line_spacing);
  975. int tab_size = text.get_tab_size();
  976. // Calculate viewport size and y offset.
  977. int viewport_height = (draw_amount - 1) * minimap_line_height;
  978. int control_height = _get_control_height() - viewport_height;
  979. int viewport_offset_y = std::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));
  980. // Calculate the first line.
  981. int num_lines_before = std::round((viewport_offset_y) / minimap_line_height);
  982. int minimap_line = (v_scroll->get_max() <= minimap_visible_lines) ? -1 : first_vis_line;
  983. if (minimap_line >= 0) {
  984. minimap_line -= get_next_visible_line_index_offset_from(first_vis_line, 0, -num_lines_before).x;
  985. minimap_line -= (minimap_line > 0 && smooth_scroll_enabled ? 1 : 0);
  986. }
  987. int minimap_draw_amount = minimap_visible_lines + get_line_wrap_count(minimap_line + 1);
  988. // Draw the minimap.
  989. // Add visual feedback when dragging or hovering the visible area rectangle.
  990. Color viewport_color = theme_cache.caret_color;
  991. if (dragging_minimap) {
  992. viewport_color.a = 0.25;
  993. } else if (hovering_minimap) {
  994. viewport_color.a = 0.175;
  995. } else {
  996. viewport_color.a = 0.1;
  997. }
  998. if (rtl) {
  999. 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);
  1000. } else {
  1001. RenderingServer::get_singleton()->canvas_item_add_rect(ci, Rect2((xmargin_end + 2), viewport_offset_y, minimap_width, viewport_height), viewport_color);
  1002. }
  1003. for (int i = 0; i < minimap_draw_amount; i++) {
  1004. minimap_line++;
  1005. if (minimap_line < 0 || minimap_line >= (int)text.size()) {
  1006. break;
  1007. }
  1008. while (_is_line_hidden(minimap_line)) {
  1009. minimap_line++;
  1010. if (minimap_line < 0 || minimap_line >= (int)text.size()) {
  1011. break;
  1012. }
  1013. }
  1014. if (minimap_line < 0 || minimap_line >= (int)text.size()) {
  1015. break;
  1016. }
  1017. const Vector<Pair<int64_t, Color>> color_map = _get_line_syntax_highlighting(minimap_line);
  1018. Color line_background_color = text.get_line_background_color(minimap_line);
  1019. // Make non-default background colors more visible, such as error markers.
  1020. // If a line background color is being applied, like in an error marker, the alpha is set to 1.0.
  1021. // Else, it stays zero.
  1022. line_background_color.a = 1.0 * (line_background_color != Color(0, 0, 0, 0));
  1023. Color current_color = editable ? theme_cache.font_color : theme_cache.font_readonly_color;
  1024. const Vector<String> wrap_rows = get_line_wrapped_text(minimap_line);
  1025. int line_wrap_amount = get_line_wrap_count(minimap_line);
  1026. int last_wrap_column = 0;
  1027. int first_indent_line = 0;
  1028. float wrap_indent_line = 0.0;
  1029. if (text.is_indent_wrapped_lines()) {
  1030. wrap_indent_line = _get_wrapped_indent_level(minimap_line, first_indent_line);
  1031. wrap_indent_line = MIN(wrap_indent_line, (minimap_width / minimap_char_size.x) * 0.6);
  1032. }
  1033. for (int line_wrap_index = 0; line_wrap_index < line_wrap_amount + 1; line_wrap_index++) {
  1034. if (line_wrap_index != 0) {
  1035. i++;
  1036. if (i >= minimap_draw_amount) {
  1037. break;
  1038. }
  1039. }
  1040. const String &str = wrap_rows[line_wrap_index];
  1041. int indent_px = line_wrap_index > first_indent_line ? wrap_indent_line : 0.0;
  1042. if (indent_px >= wrap_at_column) {
  1043. indent_px = 0;
  1044. }
  1045. indent_px = minimap_char_size.x * indent_px;
  1046. if (line_wrap_index > 0) {
  1047. last_wrap_column += wrap_rows[line_wrap_index - 1].length();
  1048. }
  1049. if (highlight_current_line && highlighted_lines.has(Pair<int, int>(minimap_line, line_wrap_index))) {
  1050. if (rtl) {
  1051. 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);
  1052. } else {
  1053. RenderingServer::get_singleton()->canvas_item_add_rect(ci, Rect2((xmargin_end + 2), i * 3, minimap_width, 2), theme_cache.current_line_color);
  1054. }
  1055. } else if (line_background_color.a > 0) {
  1056. if (rtl) {
  1057. RenderingServer::get_singleton()->canvas_item_add_rect(ci, Rect2(size.width - (xmargin_end + 2) - minimap_width, i * 3, minimap_width, 2), line_background_color);
  1058. } else {
  1059. RenderingServer::get_singleton()->canvas_item_add_rect(ci, Rect2((xmargin_end + 2), i * 3, minimap_width, 2), line_background_color);
  1060. }
  1061. }
  1062. Color next_color = current_color;
  1063. int characters = 0;
  1064. int tab_alignment = 0;
  1065. int xpos = xmargin_end + 2 + indent_px;
  1066. for (int j = 0; j < str.length(); j++) {
  1067. bool next_is_whitespace = false;
  1068. bool next_is_tab = false;
  1069. // Get the number of characters to draw together.
  1070. for (characters = 0; j + characters < str.length(); characters++) {
  1071. int next_char_index = j + characters;
  1072. for (const Pair<int64_t, Color> &color_data : color_map) {
  1073. if (last_wrap_column + next_char_index >= color_data.first) {
  1074. next_color = color_data.second;
  1075. if (!editable) {
  1076. next_color.a = theme_cache.font_readonly_color.a;
  1077. }
  1078. next_color.a *= 0.6;
  1079. } else {
  1080. break;
  1081. }
  1082. }
  1083. if (characters == 0) {
  1084. current_color = next_color;
  1085. }
  1086. if (next_color != current_color) {
  1087. break;
  1088. }
  1089. next_is_whitespace = is_whitespace(str[next_char_index]);
  1090. if (next_is_whitespace) {
  1091. if (str[next_char_index] == '\t') {
  1092. next_is_tab = true;
  1093. }
  1094. break;
  1095. }
  1096. bool out_of_bounds = xpos + minimap_char_size.x * characters >= xmargin_end + minimap_width;
  1097. if (out_of_bounds) {
  1098. break;
  1099. }
  1100. }
  1101. if (!next_is_whitespace && characters == 0) {
  1102. break;
  1103. }
  1104. if (characters > 0) {
  1105. if (rtl) {
  1106. RenderingServer::get_singleton()->canvas_item_add_rect(ci, Rect2(Point2(size.width - xpos - minimap_char_size.x * characters, minimap_line_height * i), Point2(minimap_char_size.x * characters, minimap_char_size.y)), current_color);
  1107. } else {
  1108. RenderingServer::get_singleton()->canvas_item_add_rect(ci, Rect2(Point2(xpos, minimap_line_height * i), Point2(minimap_char_size.x * characters, minimap_char_size.y)), current_color);
  1109. }
  1110. }
  1111. j += characters - 1;
  1112. xpos += minimap_char_size.x * characters;
  1113. tab_alignment += characters;
  1114. if (next_is_whitespace) {
  1115. if (next_is_tab) {
  1116. tab_alignment %= tab_size;
  1117. xpos += minimap_char_size.x * (tab_size - tab_alignment);
  1118. tab_alignment = 0;
  1119. } else {
  1120. xpos += minimap_char_size.x;
  1121. tab_alignment += 1;
  1122. }
  1123. j += 1;
  1124. }
  1125. if (xpos >= xmargin_end + minimap_width) {
  1126. // Out of bounds.
  1127. break;
  1128. }
  1129. }
  1130. }
  1131. }
  1132. }
  1133. int top_limit_y = 0;
  1134. int bottom_limit_y = get_size().height;
  1135. if (!editable) {
  1136. top_limit_y += theme_cache.style_readonly->get_margin(SIDE_TOP);
  1137. bottom_limit_y -= theme_cache.style_readonly->get_margin(SIDE_BOTTOM);
  1138. } else {
  1139. top_limit_y += theme_cache.style_normal->get_margin(SIDE_TOP);
  1140. bottom_limit_y -= theme_cache.style_normal->get_margin(SIDE_BOTTOM);
  1141. }
  1142. // Draw guidelines.
  1143. _draw_guidelines();
  1144. // Draw main text.
  1145. line_drawing_cache.clear();
  1146. int row_height = draw_placeholder ? placeholder_line_height + theme_cache.line_spacing : get_line_height();
  1147. int line = first_vis_line;
  1148. for (int i = 0; i < draw_amount; i++) {
  1149. line++;
  1150. if (line < 0 || line >= (int)text.size()) {
  1151. continue;
  1152. }
  1153. while (_is_line_hidden(line)) {
  1154. line++;
  1155. if (line < 0 || line >= (int)text.size()) {
  1156. break;
  1157. }
  1158. }
  1159. if (line < 0 || line >= (int)text.size()) {
  1160. continue;
  1161. }
  1162. LineDrawingCache cache_entry;
  1163. const Vector<Pair<int64_t, Color>> color_map = _get_line_syntax_highlighting(line);
  1164. // Ensure we at least use the font color.
  1165. Color current_color = !editable ? theme_cache.font_readonly_color : theme_cache.font_color;
  1166. if (draw_placeholder) {
  1167. current_color = theme_cache.font_placeholder_color;
  1168. }
  1169. const Ref<TextParagraph> ldata = draw_placeholder ? placeholder_data_buf : text.get_line_data(line);
  1170. const Vector<String> wrap_rows = draw_placeholder ? placeholder_wrapped_rows : get_line_wrapped_text(line);
  1171. int line_wrap_amount = draw_placeholder ? placeholder_wrapped_rows.size() - 1 : get_line_wrap_count(line);
  1172. int first_indent_line = 0;
  1173. float indent_ofs = 0.0;
  1174. if (text.is_indent_wrapped_lines()) {
  1175. _get_wrapped_indent_level(line, first_indent_line);
  1176. indent_ofs = MIN(text.get_indent_offset(line, rtl), wrap_at_column * 0.6);
  1177. }
  1178. for (int line_wrap_index = 0; line_wrap_index <= line_wrap_amount; line_wrap_index++) {
  1179. if (line_wrap_index != 0) {
  1180. i++;
  1181. if (i >= draw_amount) {
  1182. break;
  1183. }
  1184. }
  1185. const String &str = wrap_rows[line_wrap_index];
  1186. int char_margin = xmargin_beg - first_visible_col;
  1187. int ofs_x = 0;
  1188. int ofs_y = 0;
  1189. if (!editable) {
  1190. ofs_x = theme_cache.style_readonly->get_offset().x / 2;
  1191. ofs_x -= theme_cache.style_normal->get_offset().x / 2;
  1192. ofs_y = theme_cache.style_readonly->get_offset().y / 2;
  1193. } else {
  1194. ofs_y = theme_cache.style_normal->get_offset().y / 2;
  1195. }
  1196. ofs_y += i * row_height + theme_cache.line_spacing / 2;
  1197. ofs_y -= first_visible_line_wrap_ofs * row_height;
  1198. ofs_y -= _get_v_scroll_offset() * row_height;
  1199. bool clipped = false;
  1200. if (ofs_y + row_height < top_limit_y) {
  1201. // Line is outside the top margin, clip current line.
  1202. // Still need to go through the process to prepare color changes for next lines.
  1203. clipped = true;
  1204. }
  1205. if (ofs_y > bottom_limit_y) {
  1206. // Line is outside the bottom margin, clip any remaining text.
  1207. i = draw_amount;
  1208. break;
  1209. }
  1210. if (text.get_line_background_color(line).a > 0.0) {
  1211. if (rtl) {
  1212. RS::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));
  1213. } else {
  1214. RS::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));
  1215. }
  1216. }
  1217. // Draw current line highlight.
  1218. if (highlight_current_line && highlighted_lines.has(Pair<int, int>(line, line_wrap_index))) {
  1219. if (rtl) {
  1220. RS::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);
  1221. } else {
  1222. RS::get_singleton()->canvas_item_add_rect(ci, Rect2(ofs_x, ofs_y, xmargin_end, row_height), theme_cache.current_line_color);
  1223. }
  1224. }
  1225. if (line_wrap_index == 0) {
  1226. // Only do these if we are on the first wrapped part of a line.
  1227. cache_entry.y_offset = ofs_y;
  1228. int gutter_offset = Math::ceil(theme_cache.style_normal->get_margin(SIDE_LEFT));
  1229. for (int g = 0; g < gutters.size(); g++) {
  1230. const GutterInfo &gutter = gutters[g];
  1231. if (!gutter.draw || gutter.width <= 0) {
  1232. continue;
  1233. }
  1234. switch (gutter.type) {
  1235. case GUTTER_TYPE_STRING: {
  1236. const String &txt = get_line_gutter_text(line, g);
  1237. if (txt.is_empty()) {
  1238. break;
  1239. }
  1240. Ref<TextLine> tl;
  1241. tl.instantiate();
  1242. tl->add_string(txt, theme_cache.font, theme_cache.font_size);
  1243. int yofs = ofs_y + (row_height - tl->get_size().y) / 2;
  1244. if (theme_cache.outline_size > 0 && theme_cache.outline_color.a > 0) {
  1245. tl->draw_outline(ci, Point2(gutter_offset + ofs_x, yofs), theme_cache.outline_size, theme_cache.outline_color);
  1246. }
  1247. tl->draw(ci, Point2(gutter_offset + ofs_x, yofs), get_line_gutter_item_color(line, g));
  1248. } break;
  1249. case GUTTER_TYPE_ICON: {
  1250. const Ref<Texture2D> icon = get_line_gutter_icon(line, g);
  1251. if (icon.is_null()) {
  1252. break;
  1253. }
  1254. Rect2 gutter_rect = Rect2(Point2i(gutter_offset, ofs_y), Size2i(gutter.width, row_height));
  1255. int horizontal_padding = gutter_rect.size.x / 6;
  1256. int vertical_padding = gutter_rect.size.y / 6;
  1257. gutter_rect.position += Point2(horizontal_padding, vertical_padding);
  1258. gutter_rect.size -= Point2(horizontal_padding, vertical_padding) * 2;
  1259. // Correct icon aspect ratio.
  1260. float icon_ratio = icon->get_width() / icon->get_height();
  1261. float gutter_ratio = gutter_rect.size.x / gutter_rect.size.y;
  1262. if (gutter_ratio > icon_ratio) {
  1263. gutter_rect.size.x = std::floor(icon->get_width() * (gutter_rect.size.y / icon->get_height()));
  1264. } else {
  1265. gutter_rect.size.y = std::floor(icon->get_height() * (gutter_rect.size.x / icon->get_width()));
  1266. }
  1267. if (rtl) {
  1268. gutter_rect.position.x = size.width - gutter_rect.position.x - gutter_rect.size.x;
  1269. }
  1270. icon->draw_rect(ci, gutter_rect, false, get_line_gutter_item_color(line, g));
  1271. } break;
  1272. case GUTTER_TYPE_CUSTOM: {
  1273. if (gutter.custom_draw_callback.is_valid()) {
  1274. Rect2i gutter_rect = Rect2i(Point2i(gutter_offset, ofs_y), Size2i(gutter.width, row_height));
  1275. if (rtl) {
  1276. gutter_rect.position.x = size.width - gutter_rect.position.x - gutter_rect.size.x;
  1277. }
  1278. gutter.custom_draw_callback.call(line, g, Rect2(gutter_rect));
  1279. }
  1280. } break;
  1281. }
  1282. gutter_offset += gutter.width;
  1283. }
  1284. }
  1285. // Draw line.
  1286. RID rid = ldata->get_line_rid(line_wrap_index);
  1287. float text_height = TS->shaped_text_get_size(rid).y;
  1288. float wrap_indent = line_wrap_index > first_indent_line ? indent_ofs : 0.0;
  1289. if (rtl) {
  1290. char_margin = size.width - char_margin - TS->shaped_text_get_size(rid).x - wrap_indent;
  1291. } else {
  1292. char_margin += wrap_indent;
  1293. }
  1294. // Validate inline objects.
  1295. Vector<Dictionary> object_keys;
  1296. if (inline_object_drawer.is_valid()) {
  1297. for (Variant k : ldata->get_line_objects(line_wrap_index)) {
  1298. if (!is_inline_info_valid(k)) {
  1299. continue;
  1300. }
  1301. object_keys.push_back(k);
  1302. }
  1303. }
  1304. // Draw selections.
  1305. float char_w = theme_cache.font->get_char_size(' ', theme_cache.font_size).width;
  1306. for (int c = 0; c < get_caret_count(); c++) {
  1307. if (!clipped && has_selection(c) && line >= get_selection_from_line(c) && line <= get_selection_to_line(c)) {
  1308. int sel_from = (line > get_selection_from_line(c)) ? TS->shaped_text_get_range(rid).x : get_selection_from_column(c);
  1309. int sel_to = (line < get_selection_to_line(c)) ? TS->shaped_text_get_range(rid).y : get_selection_to_column(c);
  1310. Vector<Vector2> sel = TS->shaped_text_get_selection(rid, sel_from, sel_to);
  1311. // Show selection at the end of line.
  1312. if (line_wrap_index == line_wrap_amount && line < get_selection_to_line(c)) {
  1313. if (rtl) {
  1314. sel.push_back(Vector2(-char_w, 0));
  1315. } else {
  1316. float line_end = TS->shaped_text_get_size(rid).width;
  1317. sel.push_back(Vector2(line_end, line_end + char_w));
  1318. }
  1319. }
  1320. // Show selection for inline objects.
  1321. for (Dictionary info : object_keys) {
  1322. int info_column = info["column"];
  1323. if (info_column >= sel_from && info_column < sel_to) {
  1324. Rect2 orect = TS->shaped_text_get_object_rect(rid, info);
  1325. sel.push_back(Vector2(orect.position.x, orect.position.x + orect.size.x));
  1326. }
  1327. }
  1328. for (int j = 0; j < sel.size(); j++) {
  1329. Rect2 rect = Rect2(Math::ceil(sel[j].x) + char_margin + ofs_x, ofs_y, Math::ceil(sel[j].y) - Math::ceil(sel[j].x), row_height);
  1330. if (rect.position.x + rect.size.x <= xmargin_beg || rect.position.x > xmargin_end) {
  1331. continue;
  1332. }
  1333. if (rect.position.x < xmargin_beg) {
  1334. rect.size.x -= (xmargin_beg - rect.position.x);
  1335. rect.position.x = xmargin_beg;
  1336. }
  1337. if (rect.position.x + rect.size.x > xmargin_end) {
  1338. rect.size.x = xmargin_end - rect.position.x;
  1339. }
  1340. RS::get_singleton()->canvas_item_add_rect(ci, rect, theme_cache.selection_color);
  1341. }
  1342. }
  1343. }
  1344. int start = TS->shaped_text_get_range(rid).x;
  1345. if (!clipped && !search_text.is_empty()) { // Search highlight
  1346. int search_text_col = _get_column_pos_of_word(search_text, str, search_flags, 0);
  1347. int search_text_len = search_text.length();
  1348. while (search_text_col != -1) {
  1349. const Vector<Vector2> sel = TS->shaped_text_get_selection(rid, search_text_col + start, search_text_col + search_text_len + start);
  1350. for (int j = 0; j < sel.size(); j++) {
  1351. Rect2 rect = Rect2(sel[j].x + char_margin + ofs_x, ofs_y, sel[j].y - sel[j].x, row_height);
  1352. if (rect.position.x + rect.size.x <= xmargin_beg || rect.position.x > xmargin_end) {
  1353. continue;
  1354. }
  1355. if (rect.position.x < xmargin_beg) {
  1356. rect.size.x -= (xmargin_beg - rect.position.x);
  1357. rect.position.x = xmargin_beg;
  1358. } else if (rect.position.x + rect.size.x > xmargin_end) {
  1359. rect.size.x = xmargin_end - rect.position.x;
  1360. }
  1361. RS::get_singleton()->canvas_item_add_rect(ci, rect, theme_cache.search_result_color);
  1362. draw_rect(rect, theme_cache.search_result_border_color, false);
  1363. }
  1364. search_text_col = _get_column_pos_of_word(search_text, str, search_flags, search_text_col + search_text_len);
  1365. }
  1366. }
  1367. if (!clipped && highlight_all_occurrences && !only_whitespaces_highlighted && !highlighted_text.is_empty()) { // Highlight
  1368. int highlighted_text_col = _get_column_pos_of_word(highlighted_text, str, SEARCH_MATCH_CASE | SEARCH_WHOLE_WORDS, 0);
  1369. int highlighted_text_len = highlighted_text.length();
  1370. while (highlighted_text_col != -1) {
  1371. const Vector<Vector2> sel = TS->shaped_text_get_selection(rid, highlighted_text_col + start, highlighted_text_col + highlighted_text_len + start);
  1372. for (int j = 0; j < sel.size(); j++) {
  1373. Rect2 rect = Rect2(sel[j].x + char_margin + ofs_x, ofs_y, sel[j].y - sel[j].x, row_height);
  1374. if (rect.position.x + rect.size.x <= xmargin_beg || rect.position.x > xmargin_end) {
  1375. continue;
  1376. }
  1377. if (rect.position.x < xmargin_beg) {
  1378. rect.size.x -= (xmargin_beg - rect.position.x);
  1379. rect.position.x = xmargin_beg;
  1380. } else if (rect.position.x + rect.size.x > xmargin_end) {
  1381. rect.size.x = xmargin_end - rect.position.x;
  1382. }
  1383. RS::get_singleton()->canvas_item_add_rect(ci, rect, theme_cache.word_highlighted_color);
  1384. }
  1385. highlighted_text_col = _get_column_pos_of_word(highlighted_text, str, SEARCH_MATCH_CASE | SEARCH_WHOLE_WORDS, highlighted_text_col + highlighted_text_len);
  1386. }
  1387. }
  1388. if (!clipped && lookup_symbol_word.length() != 0) { // Highlight word
  1389. if (is_unicode_identifier_start(lookup_symbol_word[0]) || lookup_symbol_word[0] == '.') {
  1390. Color highlight_underline_color = !editable ? theme_cache.font_readonly_color : theme_cache.font_color;
  1391. int lookup_symbol_word_col = _get_column_pos_of_word(lookup_symbol_word, str, SEARCH_MATCH_CASE | SEARCH_WHOLE_WORDS, 0);
  1392. int lookup_symbol_word_len = lookup_symbol_word.length();
  1393. while (lookup_symbol_word_col != -1) {
  1394. const Vector<Vector2> sel = TS->shaped_text_get_selection(rid, lookup_symbol_word_col + start, lookup_symbol_word_col + lookup_symbol_word_len + start);
  1395. for (int j = 0; j < sel.size(); j++) {
  1396. 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);
  1397. if (rect.position.x + rect.size.x <= xmargin_beg || rect.position.x > xmargin_end) {
  1398. continue;
  1399. }
  1400. if (rect.position.x < xmargin_beg) {
  1401. rect.size.x -= (xmargin_beg - rect.position.x);
  1402. rect.position.x = xmargin_beg;
  1403. } else if (rect.position.x + rect.size.x > xmargin_end) {
  1404. rect.size.x = xmargin_end - rect.position.x;
  1405. }
  1406. rect.position.y += std::ceil(TS->shaped_text_get_ascent(rid)) + std::ceil(theme_cache.font->get_underline_position(theme_cache.font_size));
  1407. rect.size.y = MAX(1, theme_cache.font->get_underline_thickness(theme_cache.font_size));
  1408. RS::get_singleton()->canvas_item_add_rect(ci, rect, highlight_underline_color);
  1409. }
  1410. 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);
  1411. }
  1412. }
  1413. }
  1414. ofs_y += (row_height - text_height) / 2;
  1415. const Glyph *glyphs = TS->shaped_text_get_glyphs(rid);
  1416. int gl_size = TS->shaped_text_get_glyph_count(rid);
  1417. ofs_y += ldata->get_line_ascent(line_wrap_index);
  1418. int first_visible_char = TS->shaped_text_get_range(rid).y;
  1419. int last_visible_char = TS->shaped_text_get_range(rid).x;
  1420. float char_ofs = 0;
  1421. if (theme_cache.outline_size > 0 && theme_cache.outline_color.a > 0) {
  1422. for (int j = 0; j < gl_size; j++) {
  1423. for (int k = 0; k < glyphs[j].repeat; k++) {
  1424. if ((char_ofs + char_margin) >= xmargin_beg && (char_ofs + glyphs[j].advance + char_margin) <= xmargin_end) {
  1425. if (glyphs[j].font_rid != RID()) {
  1426. 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);
  1427. }
  1428. }
  1429. char_ofs += glyphs[j].advance;
  1430. }
  1431. if ((char_ofs + char_margin) >= xmargin_end) {
  1432. break;
  1433. }
  1434. }
  1435. char_ofs = 0;
  1436. }
  1437. // Draw inline objects.
  1438. for (Dictionary k : object_keys) {
  1439. Rect2 col_rect = TS->shaped_text_get_object_rect(rid, k);
  1440. col_rect.position += Vector2(char_margin + ofs_x, ofs_y);
  1441. if (!clipped && (col_rect.position.x) >= xmargin_beg && (col_rect.position.x + col_rect.size.x) <= xmargin_end) {
  1442. inline_object_drawer.call(k, col_rect);
  1443. }
  1444. }
  1445. for (int j = 0; j < gl_size; j++) {
  1446. for (const Pair<int64_t, Color> &color_data : color_map) {
  1447. if (color_data.first <= glyphs[j].start) {
  1448. current_color = color_data.second;
  1449. if (!editable && current_color.a > theme_cache.font_readonly_color.a) {
  1450. current_color.a = theme_cache.font_readonly_color.a;
  1451. }
  1452. } else {
  1453. break;
  1454. }
  1455. }
  1456. Color gl_color = current_color;
  1457. for (int c = 0; c < get_caret_count(); c++) {
  1458. if (has_selection(c) && line >= get_selection_from_line(c) && line <= get_selection_to_line(c)) { // Selection
  1459. int sel_from = (line > get_selection_from_line(c)) ? TS->shaped_text_get_range(rid).x : get_selection_from_column(c);
  1460. int sel_to = (line < get_selection_to_line(c)) ? TS->shaped_text_get_range(rid).y : get_selection_to_column(c);
  1461. if (glyphs[j].start >= sel_from && glyphs[j].end <= sel_to && use_selected_font_color) {
  1462. gl_color = theme_cache.font_selected_color;
  1463. }
  1464. }
  1465. }
  1466. float char_pos = char_ofs + char_margin + ofs_x;
  1467. if (char_pos >= xmargin_beg) {
  1468. if (highlight_matching_braces_enabled) {
  1469. for (int c = 0; c < get_caret_count(); c++) {
  1470. const BraceMatchingData &brace_match = brace_matching[c];
  1471. if ((brace_match.open_match_line == line && brace_match.open_match_column == glyphs[j].start) ||
  1472. (get_caret_column(c) == glyphs[j].start && get_caret_line(c) == line && carets_wrap_index[c] == line_wrap_index && (brace_match.open_matching || brace_match.open_mismatch))) {
  1473. if (brace_match.open_mismatch) {
  1474. gl_color = _get_brace_mismatch_color();
  1475. }
  1476. 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));
  1477. RS::get_singleton()->canvas_item_add_rect(ci, rect, gl_color);
  1478. }
  1479. if ((brace_match.close_match_line == line && brace_match.close_match_column == glyphs[j].start) ||
  1480. (get_caret_column(c) == glyphs[j].start + 1 && get_caret_line(c) == line && carets_wrap_index[c] == line_wrap_index && (brace_match.close_matching || brace_match.close_mismatch))) {
  1481. if (brace_match.close_mismatch) {
  1482. gl_color = _get_brace_mismatch_color();
  1483. }
  1484. 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));
  1485. RS::get_singleton()->canvas_item_add_rect(ci, rect, gl_color);
  1486. }
  1487. }
  1488. }
  1489. if (draw_tabs && ((glyphs[j].flags & TextServer::GRAPHEME_IS_TAB) == TextServer::GRAPHEME_IS_TAB)) {
  1490. int yofs = (text_height - theme_cache.tab_icon->get_height()) / 2 - ldata->get_line_ascent(line_wrap_index);
  1491. theme_cache.tab_icon->draw(ci, Point2(char_pos, ofs_y + yofs), gl_color);
  1492. } 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)) {
  1493. int yofs = (text_height - theme_cache.space_icon->get_height()) / 2 - ldata->get_line_ascent(line_wrap_index);
  1494. int xofs = (glyphs[j].advance * glyphs[j].repeat - theme_cache.space_icon->get_width()) / 2;
  1495. theme_cache.space_icon->draw(ci, Point2(char_pos + xofs, ofs_y + yofs), gl_color);
  1496. }
  1497. }
  1498. bool had_glyphs_drawn = false;
  1499. for (int k = 0; k < glyphs[j].repeat; k++) {
  1500. if (!clipped && (char_ofs + char_margin) >= xmargin_beg && (char_ofs + glyphs[j].advance + char_margin) <= xmargin_end) {
  1501. if (glyphs[j].font_rid != RID()) {
  1502. 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);
  1503. had_glyphs_drawn = true;
  1504. } 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)) {
  1505. 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);
  1506. had_glyphs_drawn = true;
  1507. }
  1508. }
  1509. char_ofs += glyphs[j].advance;
  1510. }
  1511. if (had_glyphs_drawn) {
  1512. if (first_visible_char > glyphs[j].start) {
  1513. first_visible_char = glyphs[j].start;
  1514. }
  1515. if (last_visible_char < glyphs[j].end) {
  1516. last_visible_char = glyphs[j].end;
  1517. }
  1518. }
  1519. if ((char_ofs + char_margin) >= xmargin_end) {
  1520. break;
  1521. }
  1522. }
  1523. cache_entry.first_visible_chars.push_back(first_visible_char);
  1524. cache_entry.last_visible_chars.push_back(last_visible_char);
  1525. // is_line_folded
  1526. if (line_wrap_index == line_wrap_amount && line < text.size() - 1 && _is_line_hidden(line + 1)) {
  1527. int xofs = char_ofs + char_margin + ofs_x + (_get_folded_eol_icon()->get_width() / 2);
  1528. if (xofs >= xmargin_beg && xofs < xmargin_end) {
  1529. int yofs = (text_height - _get_folded_eol_icon()->get_height()) / 2 - ldata->get_line_ascent(line_wrap_index);
  1530. Color eol_color = _get_code_folding_color();
  1531. eol_color.a = 1;
  1532. _get_folded_eol_icon()->draw(ci, Point2(xofs, ofs_y + yofs), eol_color);
  1533. }
  1534. }
  1535. // Carets.
  1536. // Prevent carets from disappearing at theme scales below 1.0 (if the caret width is 1).
  1537. const int caret_width = theme_cache.caret_width * MAX(1, theme_cache.base_scale);
  1538. for (int c = 0; c < carets.size(); c++) {
  1539. if (!clipped && get_caret_line(c) == line && carets_wrap_index[c] == line_wrap_index) {
  1540. carets.write[c].draw_pos.y = ofs_y + ldata->get_line_descent(line_wrap_index);
  1541. if (ime_text.is_empty() || ime_selection.y == 0) {
  1542. // Normal caret.
  1543. CaretInfo ts_caret;
  1544. if (!str.is_empty() || !ime_text.is_empty()) {
  1545. // Get carets.
  1546. ts_caret = TS->shaped_text_get_carets(rid, ime_text.is_empty() ? get_caret_column(c) : get_caret_column(c) + ime_selection.x);
  1547. } else {
  1548. // No carets, add one at the start.
  1549. int h = theme_cache.font->get_height(theme_cache.font_size);
  1550. if (rtl) {
  1551. ts_caret.l_dir = TextServer::DIRECTION_RTL;
  1552. ts_caret.l_caret = Rect2(Vector2(TS->shaped_text_get_size(rid).x, -h / 2), Size2(caret_width * 4, h));
  1553. } else {
  1554. ts_caret.l_dir = TextServer::DIRECTION_LTR;
  1555. ts_caret.l_caret = Rect2(Vector2(char_ofs, -h / 2), Size2(caret_width * 4, h));
  1556. }
  1557. }
  1558. 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())) {
  1559. carets.write[c].draw_pos.x = char_margin + ofs_x + ts_caret.l_caret.position.x;
  1560. } else {
  1561. carets.write[c].draw_pos.x = char_margin + ofs_x + ts_caret.t_caret.position.x;
  1562. }
  1563. if (get_caret_draw_pos(c).x >= xmargin_beg && get_caret_draw_pos(c).x <= xmargin_end) {
  1564. carets.write[c].visible = true;
  1565. if (draw_caret || drag_caret_force_displayed) {
  1566. if (caret_type == CaretType::CARET_TYPE_BLOCK || overtype_mode) {
  1567. //Block or underline caret, draw trailing carets at full height.
  1568. int h = theme_cache.font->get_height(theme_cache.font_size);
  1569. if (ts_caret.t_caret != Rect2()) {
  1570. if (overtype_mode) {
  1571. ts_caret.t_caret.position.y = TS->shaped_text_get_descent(rid);
  1572. ts_caret.t_caret.size.y = caret_width;
  1573. } else {
  1574. ts_caret.t_caret.position.y = -TS->shaped_text_get_ascent(rid);
  1575. ts_caret.t_caret.size.y = h;
  1576. }
  1577. ts_caret.t_caret.position += Vector2(char_margin + ofs_x, ofs_y);
  1578. draw_rect(ts_caret.t_caret, theme_cache.caret_color, overtype_mode);
  1579. if (ts_caret.l_caret != Rect2() && ts_caret.l_dir != ts_caret.t_dir) {
  1580. // Draw split caret (leading part).
  1581. ts_caret.l_caret.position += Vector2(char_margin + ofs_x, ofs_y);
  1582. ts_caret.l_caret.size.x = caret_width;
  1583. RS::get_singleton()->canvas_item_add_rect(ci, ts_caret.l_caret, theme_cache.caret_color);
  1584. // Draw extra direction marker on top of split caret.
  1585. float d = (ts_caret.l_dir == TextServer::DIRECTION_LTR) ? 0.5 : -3;
  1586. 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);
  1587. RS::get_singleton()->canvas_item_add_rect(ci, trect, theme_cache.caret_color);
  1588. }
  1589. } else { // End of the line.
  1590. if (gl_size > 0) {
  1591. // Adjust for actual line dimensions.
  1592. if (overtype_mode) {
  1593. ts_caret.l_caret.position.y = TS->shaped_text_get_descent(rid);
  1594. ts_caret.l_caret.size.y = caret_width;
  1595. } else {
  1596. ts_caret.l_caret.position.y = -TS->shaped_text_get_ascent(rid);
  1597. ts_caret.l_caret.size.y = h;
  1598. }
  1599. } else if (overtype_mode) {
  1600. ts_caret.l_caret.position.y += ts_caret.l_caret.size.y;
  1601. ts_caret.l_caret.size.y = caret_width;
  1602. }
  1603. if (Math::ceil(ts_caret.l_caret.position.x) >= TS->shaped_text_get_size(rid).x) {
  1604. ts_caret.l_caret.size.x = theme_cache.font->get_char_size('m', theme_cache.font_size).x;
  1605. } else {
  1606. ts_caret.l_caret.size.x = 3 * caret_width;
  1607. }
  1608. ts_caret.l_caret.position += Vector2(char_margin + ofs_x, ofs_y);
  1609. if (ts_caret.l_dir == TextServer::DIRECTION_RTL) {
  1610. ts_caret.l_caret.position.x -= ts_caret.l_caret.size.x;
  1611. }
  1612. draw_rect(ts_caret.l_caret, theme_cache.caret_color, overtype_mode);
  1613. }
  1614. } else {
  1615. // Normal caret.
  1616. if (ts_caret.l_caret != Rect2() && ts_caret.l_dir == TextServer::DIRECTION_AUTO) {
  1617. // Draw extra marker on top of mid caret.
  1618. Rect2 trect = Rect2(ts_caret.l_caret.position.x - 2.5 * caret_width, ts_caret.l_caret.position.y, 6 * caret_width, caret_width);
  1619. trect.position += Vector2(char_margin + ofs_x, ofs_y);
  1620. RS::get_singleton()->canvas_item_add_rect(ci, trect, theme_cache.caret_color);
  1621. } else if (ts_caret.l_caret != Rect2() && ts_caret.t_caret != Rect2() && ts_caret.l_dir != ts_caret.t_dir) {
  1622. // Draw extra direction marker on top of split caret.
  1623. float d = (ts_caret.l_dir == TextServer::DIRECTION_LTR) ? 0.5 : -3;
  1624. 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);
  1625. trect.position += Vector2(char_margin + ofs_x, ofs_y);
  1626. RS::get_singleton()->canvas_item_add_rect(ci, trect, theme_cache.caret_color);
  1627. d = (ts_caret.t_dir == TextServer::DIRECTION_LTR) ? 0.5 : -3;
  1628. trect = Rect2(ts_caret.t_caret.position.x + d * caret_width, ts_caret.t_caret.position.y, 3 * caret_width, caret_width);
  1629. trect.position += Vector2(char_margin + ofs_x, ofs_y);
  1630. RS::get_singleton()->canvas_item_add_rect(ci, trect, theme_cache.caret_color);
  1631. }
  1632. ts_caret.l_caret.position += Vector2(char_margin + ofs_x, ofs_y);
  1633. ts_caret.l_caret.size.x = caret_width;
  1634. RS::get_singleton()->canvas_item_add_rect(ci, ts_caret.l_caret, theme_cache.caret_color);
  1635. ts_caret.t_caret.position += Vector2(char_margin + ofs_x, ofs_y);
  1636. ts_caret.t_caret.size.x = caret_width;
  1637. RS::get_singleton()->canvas_item_add_rect(ci, ts_caret.t_caret, theme_cache.caret_color);
  1638. }
  1639. }
  1640. }
  1641. }
  1642. if (!ime_text.is_empty()) {
  1643. {
  1644. // IME Intermediate text range.
  1645. const Vector<Vector2> sel = TS->shaped_text_get_selection(rid, get_caret_column(c), get_caret_column(c) + ime_text.length());
  1646. for (int j = 0; j < sel.size(); j++) {
  1647. Rect2 rect = Rect2(sel[j].x + char_margin + ofs_x, ofs_y, sel[j].y - sel[j].x, text_height);
  1648. if (rect.position.x + rect.size.x <= xmargin_beg || rect.position.x > xmargin_end) {
  1649. continue;
  1650. }
  1651. if (rect.position.x < xmargin_beg) {
  1652. rect.size.x -= (xmargin_beg - rect.position.x);
  1653. rect.position.x = xmargin_beg;
  1654. } else if (rect.position.x + rect.size.x > xmargin_end) {
  1655. rect.size.x = xmargin_end - rect.position.x;
  1656. }
  1657. rect.size.y = caret_width;
  1658. draw_rect(rect, theme_cache.caret_color);
  1659. carets.write[c].draw_pos.x = rect.position.x;
  1660. }
  1661. }
  1662. if (ime_selection.y > 0) {
  1663. // IME caret.
  1664. const 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);
  1665. for (int j = 0; j < sel.size(); j++) {
  1666. Rect2 rect = Rect2(sel[j].x + char_margin + ofs_x, ofs_y, sel[j].y - sel[j].x, text_height);
  1667. if (rect.position.x + rect.size.x <= xmargin_beg || rect.position.x > xmargin_end) {
  1668. continue;
  1669. }
  1670. if (rect.position.x < xmargin_beg) {
  1671. rect.size.x -= (xmargin_beg - rect.position.x);
  1672. rect.position.x = xmargin_beg;
  1673. } else if (rect.position.x + rect.size.x > xmargin_end) {
  1674. rect.size.x = xmargin_end - rect.position.x;
  1675. }
  1676. rect.size.y = caret_width * 3;
  1677. draw_rect(rect, theme_cache.caret_color);
  1678. carets.write[c].draw_pos.x = rect.position.x;
  1679. }
  1680. }
  1681. }
  1682. }
  1683. }
  1684. }
  1685. if (!draw_placeholder) {
  1686. line_drawing_cache[line] = cache_entry;
  1687. }
  1688. }
  1689. if (has_focus()) {
  1690. _update_ime_window_position();
  1691. }
  1692. } break;
  1693. case NOTIFICATION_FOCUS_ENTER: {
  1694. if (caret_blink_enabled) {
  1695. caret_blink_timer->start();
  1696. } else {
  1697. draw_caret = true;
  1698. }
  1699. if (editable && virtual_keyboard_show_on_focus) {
  1700. _show_virtual_keyboard();
  1701. }
  1702. } break;
  1703. case NOTIFICATION_FOCUS_EXIT: {
  1704. if (caret_blink_enabled) {
  1705. caret_blink_timer->stop();
  1706. }
  1707. apply_ime();
  1708. if (DisplayServer::get_singleton()->has_feature(DisplayServer::FEATURE_VIRTUAL_KEYBOARD) && virtual_keyboard_enabled) {
  1709. DisplayServer::get_singleton()->virtual_keyboard_hide();
  1710. }
  1711. if (deselect_on_focus_loss_enabled && !selection_drag_attempt) {
  1712. deselect();
  1713. }
  1714. } break;
  1715. case MainLoop::NOTIFICATION_OS_IME_UPDATE: {
  1716. if (has_focus()) {
  1717. const String &new_ime_text = DisplayServer::get_singleton()->ime_get_text();
  1718. const Vector2i &new_ime_selection = DisplayServer::get_singleton()->ime_get_selection();
  1719. if (ime_text == new_ime_text && ime_selection == new_ime_selection) {
  1720. break;
  1721. }
  1722. bool had_ime_text = has_ime_text();
  1723. ime_text = new_ime_text;
  1724. ime_selection = new_ime_selection;
  1725. if (!had_ime_text && has_ime_text()) {
  1726. _cancel_drag_and_drop_text();
  1727. }
  1728. if (has_ime_text() && has_selection()) {
  1729. set_selection_mode(SELECTION_MODE_NONE);
  1730. delete_selection();
  1731. }
  1732. _update_ime_text();
  1733. adjust_viewport_to_caret(0);
  1734. queue_accessibility_update();
  1735. queue_redraw();
  1736. }
  1737. } break;
  1738. case NOTIFICATION_DRAG_BEGIN: {
  1739. set_selection_mode(SelectionMode::SELECTION_MODE_NONE);
  1740. drag_action = true;
  1741. dragging_minimap = false;
  1742. dragging_selection = false;
  1743. can_drag_minimap = false;
  1744. click_select_held->stop();
  1745. } break;
  1746. case NOTIFICATION_DRAG_END: {
  1747. remove_drag_caret();
  1748. if (selection_drag_attempt && is_drag_successful()) {
  1749. // Dropped elsewhere.
  1750. if (is_editable() && !Input::get_singleton()->is_key_pressed(Key::CMD_OR_CTRL)) {
  1751. delete_selection();
  1752. } else if (deselect_on_focus_loss_enabled) {
  1753. deselect();
  1754. }
  1755. }
  1756. selection_drag_attempt = false;
  1757. drag_action = false;
  1758. drag_caret_force_displayed = false;
  1759. dragging_selection = false;
  1760. } break;
  1761. case NOTIFICATION_MOUSE_EXIT_SELF: {
  1762. if (drag_caret_force_displayed) {
  1763. drag_caret_force_displayed = false;
  1764. queue_redraw();
  1765. }
  1766. if (hovered_gutter != Vector2i(-1, -1)) {
  1767. hovered_gutter = Vector2i(-1, -1);
  1768. queue_redraw();
  1769. }
  1770. } break;
  1771. }
  1772. }
  1773. void TextEdit::unhandled_key_input(const Ref<InputEvent> &p_event) {
  1774. Ref<InputEventKey> k = p_event;
  1775. if (k.is_valid()) {
  1776. if (!k->is_pressed()) {
  1777. return;
  1778. }
  1779. // Handle Unicode (with modifiers active, process after shortcuts).
  1780. if (has_focus() && editable && (k->get_unicode() >= 32)) {
  1781. handle_unicode_input(k->get_unicode());
  1782. accept_event();
  1783. }
  1784. }
  1785. }
  1786. bool TextEdit::alt_input(const Ref<InputEvent> &p_gui_input) {
  1787. Ref<InputEventKey> k = p_gui_input;
  1788. if (k.is_valid()) {
  1789. // Start Unicode Alt input (hold).
  1790. if (k->is_alt_pressed() && k->get_keycode() == Key::KP_ADD && !alt_start && !alt_start_no_hold) {
  1791. if (has_selection()) {
  1792. delete_selection();
  1793. }
  1794. alt_start = true;
  1795. alt_code = 0;
  1796. alt_mode = ALT_INPUT_UNICODE;
  1797. ime_text = "u";
  1798. ime_selection = Vector2i(0, -1);
  1799. _update_ime_text();
  1800. return true;
  1801. }
  1802. // Start Unicode input (press).
  1803. if (k->is_action("ui_unicode_start", true) && !alt_start && !alt_start_no_hold) {
  1804. if (has_selection()) {
  1805. delete_selection();
  1806. }
  1807. alt_start_no_hold = true;
  1808. alt_code = 0;
  1809. alt_mode = ALT_INPUT_UNICODE;
  1810. ime_text = "u";
  1811. ime_selection = Vector2i(0, -1);
  1812. _update_ime_text();
  1813. return true;
  1814. }
  1815. // Start OEM Alt input (hold).
  1816. if (k->is_alt_pressed() && k->get_keycode() >= Key::KP_1 && k->get_keycode() <= Key::KP_9 && !alt_start && !alt_start_no_hold) {
  1817. if (has_selection()) {
  1818. delete_selection();
  1819. }
  1820. alt_start = true;
  1821. alt_code = (uint32_t)(k->get_keycode() - Key::KP_0);
  1822. alt_mode = ALT_INPUT_OEM;
  1823. ime_text = vformat("o%s", String::num_int64(alt_code, 10));
  1824. ime_selection = Vector2i(0, -1);
  1825. _update_ime_text();
  1826. return true;
  1827. }
  1828. // Start Windows Alt input (hold).
  1829. if (k->is_alt_pressed() && k->get_keycode() == Key::KP_0 && !alt_start && !alt_start_no_hold) {
  1830. if (has_selection()) {
  1831. delete_selection();
  1832. }
  1833. alt_start = true;
  1834. alt_mode = ALT_INPUT_WIN;
  1835. alt_code = 0;
  1836. ime_text = "w";
  1837. ime_selection = Vector2i(0, -1);
  1838. _update_ime_text();
  1839. return true;
  1840. }
  1841. // Update Unicode input.
  1842. if (k->is_pressed() && ((k->is_alt_pressed() && alt_start) || alt_start_no_hold)) {
  1843. if (k->get_keycode() >= Key::KEY_0 && k->get_keycode() <= Key::KEY_9) {
  1844. if (alt_mode == ALT_INPUT_UNICODE) {
  1845. alt_code = alt_code << 4;
  1846. } else {
  1847. alt_code = alt_code * 10;
  1848. }
  1849. alt_code += (uint32_t)(k->get_keycode() - Key::KEY_0);
  1850. } else if (k->get_keycode() >= Key::KP_0 && k->get_keycode() <= Key::KP_9) {
  1851. if (alt_mode == ALT_INPUT_UNICODE) {
  1852. alt_code = alt_code << 4;
  1853. } else {
  1854. alt_code = alt_code * 10;
  1855. }
  1856. alt_code += (uint32_t)(k->get_keycode() - Key::KP_0);
  1857. } else if (alt_mode == ALT_INPUT_UNICODE && k->get_keycode() >= Key::A && k->get_keycode() <= Key::F) {
  1858. alt_code = alt_code << 4;
  1859. alt_code += (uint32_t)(k->get_keycode() - Key::A) + 10;
  1860. } else if ((Key)k->get_unicode() >= Key::KEY_0 && (Key)k->get_unicode() <= Key::KEY_9) {
  1861. if (alt_mode == ALT_INPUT_UNICODE) {
  1862. alt_code = alt_code << 4;
  1863. } else {
  1864. alt_code = alt_code * 10;
  1865. }
  1866. alt_code += (uint32_t)((Key)k->get_unicode() - Key::KEY_0);
  1867. } else if (alt_mode == ALT_INPUT_UNICODE && (Key)k->get_unicode() >= Key::A && (Key)k->get_unicode() <= Key::F) {
  1868. alt_code = alt_code << 4;
  1869. alt_code += (uint32_t)((Key)k->get_unicode() - Key::A) + 10;
  1870. } else if (k->get_physical_keycode() >= Key::KEY_0 && k->get_physical_keycode() <= Key::KEY_9) {
  1871. if (alt_mode == ALT_INPUT_UNICODE) {
  1872. alt_code = alt_code << 4;
  1873. } else {
  1874. alt_code = alt_code * 10;
  1875. }
  1876. alt_code += (uint32_t)(k->get_physical_keycode() - Key::KEY_0);
  1877. }
  1878. if (k->get_keycode() == Key::BACKSPACE) {
  1879. if (alt_mode == ALT_INPUT_UNICODE) {
  1880. alt_code = alt_code >> 4;
  1881. } else {
  1882. alt_code = alt_code / 10;
  1883. }
  1884. }
  1885. if (alt_code > 0x10ffff) {
  1886. alt_code = 0x10ffff;
  1887. }
  1888. if (alt_code > 0) {
  1889. if (alt_mode == ALT_INPUT_UNICODE) {
  1890. ime_text = vformat("u%s", String::num_int64(alt_code, 16, true));
  1891. } else if (alt_mode == ALT_INPUT_OEM) {
  1892. ime_text = vformat("o%s", String::num_int64(alt_code, 10));
  1893. } else if (alt_mode == ALT_INPUT_WIN) {
  1894. ime_text = vformat("w%s", String::num_int64(alt_code, 10));
  1895. }
  1896. } else {
  1897. if (alt_mode == ALT_INPUT_UNICODE) {
  1898. ime_text = "u";
  1899. } else if (alt_mode == ALT_INPUT_OEM) {
  1900. ime_text = "o";
  1901. } else if (alt_mode == ALT_INPUT_WIN) {
  1902. ime_text = "w";
  1903. }
  1904. }
  1905. ime_selection = Vector2i(0, -1);
  1906. _update_ime_text();
  1907. return true;
  1908. }
  1909. // Submit Unicode input.
  1910. if ((!k->is_pressed() && alt_start && k->get_keycode() == Key::ALT) || (alt_start_no_hold && (k->is_action("ui_text_submit", true) || k->is_action("ui_accept", true)))) {
  1911. alt_start = false;
  1912. alt_start_no_hold = false;
  1913. if ((alt_code > 0x31 && alt_code < 0xd800) || (alt_code > 0xdfff)) {
  1914. ime_text = String();
  1915. ime_selection = Vector2i();
  1916. if (alt_mode == ALT_INPUT_UNICODE) {
  1917. if ((alt_code > 0x31 && alt_code < 0xd800) || (alt_code > 0xdfff)) {
  1918. handle_unicode_input(alt_code);
  1919. }
  1920. } else if (alt_mode == ALT_INPUT_OEM) {
  1921. if (alt_code > 0x00 && alt_code <= 0xff) {
  1922. handle_unicode_input(alt_code_oem437[alt_code]);
  1923. } else if ((alt_code > 0xff && alt_code < 0xd800) || (alt_code > 0xdfff)) {
  1924. handle_unicode_input(alt_code);
  1925. }
  1926. } else if (alt_mode == ALT_INPUT_WIN) {
  1927. if (alt_code > 0x00 && alt_code <= 0xff) {
  1928. handle_unicode_input(alt_code_cp1252[alt_code]);
  1929. } else if ((alt_code > 0xff && alt_code < 0xd800) || (alt_code > 0xdfff)) {
  1930. handle_unicode_input(alt_code);
  1931. }
  1932. }
  1933. alt_mode = ALT_INPUT_NONE;
  1934. } else {
  1935. ime_text = String();
  1936. ime_selection = Vector2i();
  1937. }
  1938. _update_ime_text();
  1939. return true;
  1940. }
  1941. // Cancel Unicode input.
  1942. if (alt_start_no_hold && k->is_action("ui_cancel", true)) {
  1943. alt_start = false;
  1944. alt_start_no_hold = false;
  1945. alt_mode = ALT_INPUT_NONE;
  1946. ime_text = String();
  1947. ime_selection = Vector2i();
  1948. _update_ime_text();
  1949. return true;
  1950. }
  1951. }
  1952. return false;
  1953. }
  1954. void TextEdit::gui_input(const Ref<InputEvent> &p_gui_input) {
  1955. ERR_FAIL_COND(p_gui_input.is_null());
  1956. double prev_v_scroll = v_scroll->get_value();
  1957. double prev_h_scroll = h_scroll->get_value();
  1958. Ref<InputEventMouseButton> mb = p_gui_input;
  1959. if (mb.is_valid()) {
  1960. Vector2i mpos = mb->get_position();
  1961. if (is_layout_rtl()) {
  1962. mpos.x = get_size().x - mpos.x;
  1963. }
  1964. if (mb->is_pressed()) {
  1965. if (mb->get_button_index() == MouseButton::WHEEL_UP && !mb->is_command_or_control_pressed()) {
  1966. if (mb->is_shift_pressed()) {
  1967. h_scroll->set_value(h_scroll->get_value() - (100 * mb->get_factor()));
  1968. queue_accessibility_update();
  1969. } else if (mb->is_alt_pressed()) {
  1970. // Scroll 5 times as fast as normal (like in Visual Studio Code).
  1971. _scroll_up(15 * mb->get_factor(), true);
  1972. } else if (v_scroll->is_visible()) {
  1973. // Scroll 3 lines.
  1974. _scroll_up(3 * mb->get_factor(), true);
  1975. }
  1976. }
  1977. if (mb->get_button_index() == MouseButton::WHEEL_DOWN && !mb->is_command_or_control_pressed()) {
  1978. if (mb->is_shift_pressed()) {
  1979. h_scroll->set_value(h_scroll->get_value() + (100 * mb->get_factor()));
  1980. queue_accessibility_update();
  1981. } else if (mb->is_alt_pressed()) {
  1982. // Scroll 5 times as fast as normal (like in Visual Studio Code).
  1983. _scroll_down(15 * mb->get_factor(), true);
  1984. } else if (v_scroll->is_visible()) {
  1985. // Scroll 3 lines.
  1986. _scroll_down(3 * mb->get_factor(), true);
  1987. }
  1988. }
  1989. if (mb->get_button_index() == MouseButton::WHEEL_LEFT) {
  1990. h_scroll->set_value(h_scroll->get_value() - (100 * mb->get_factor()));
  1991. queue_accessibility_update();
  1992. }
  1993. if (mb->get_button_index() == MouseButton::WHEEL_RIGHT) {
  1994. h_scroll->set_value(h_scroll->get_value() + (100 * mb->get_factor()));
  1995. queue_accessibility_update();
  1996. }
  1997. if (mb->get_button_index() == MouseButton::LEFT) {
  1998. _reset_caret_blink_timer();
  1999. apply_ime();
  2000. Point2i pos = get_line_column_at_pos(mpos);
  2001. int line = pos.y;
  2002. int col = pos.x;
  2003. // Gutters.
  2004. Vector2i current_hovered_gutter = _get_hovered_gutter(mpos);
  2005. if (current_hovered_gutter != hovered_gutter) {
  2006. hovered_gutter = current_hovered_gutter;
  2007. queue_redraw();
  2008. }
  2009. if (hovered_gutter != Vector2i(-1, -1)) {
  2010. emit_signal(SNAME("gutter_clicked"), hovered_gutter.y, hovered_gutter.x);
  2011. return;
  2012. }
  2013. int left_margin = Math::ceil(theme_cache.style_normal->get_margin(SIDE_LEFT));
  2014. if (mpos.x < left_margin + gutters_width + gutter_padding) {
  2015. return;
  2016. }
  2017. // Minimap.
  2018. if (draw_minimap) {
  2019. _update_minimap_click();
  2020. if (dragging_minimap) {
  2021. return;
  2022. }
  2023. }
  2024. // Update caret.
  2025. int caret = carets.size() - 1;
  2026. int prev_col = get_caret_column(caret);
  2027. int prev_line = get_caret_line(caret);
  2028. int mouse_over_selection_caret = get_selection_at_line_column(line, col, true);
  2029. const int triple_click_timeout = 600;
  2030. const int triple_click_tolerance = 5;
  2031. 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);
  2032. if (!mb->is_double_click() && !is_triple_click) {
  2033. if (mb->is_alt_pressed()) {
  2034. prev_line = line;
  2035. prev_col = col;
  2036. // Remove caret at clicked location.
  2037. if (get_caret_count() > 1) {
  2038. // Deselect if clicked on caret or its selection.
  2039. int clicked_caret = get_selection_at_line_column(line, col, true, false);
  2040. if (clicked_caret != -1) {
  2041. remove_caret(clicked_caret);
  2042. last_dblclk = 0;
  2043. return;
  2044. }
  2045. }
  2046. if (mouse_over_selection_caret >= 0) {
  2047. // Did not remove selection under mouse, don't add a new caret.
  2048. return;
  2049. }
  2050. // Create new caret at clicked location.
  2051. caret = add_caret(line, col);
  2052. if (caret == -1) {
  2053. return;
  2054. }
  2055. queue_accessibility_update();
  2056. last_dblclk = 0;
  2057. } else if (!mb->is_shift_pressed()) {
  2058. if (drag_and_drop_selection_enabled && mouse_over_selection_caret >= 0) {
  2059. // Try to drag and drop.
  2060. set_selection_mode(SelectionMode::SELECTION_MODE_NONE);
  2061. selection_drag_attempt = true;
  2062. drag_and_drop_origin_caret_index = mouse_over_selection_caret;
  2063. last_dblclk = 0;
  2064. // Don't update caret until we know if it is not drag and drop.
  2065. return;
  2066. } else {
  2067. // A regular click clears all other carets.
  2068. caret = 0;
  2069. remove_secondary_carets();
  2070. deselect();
  2071. }
  2072. }
  2073. _push_current_op();
  2074. set_caret_line(line, false, true, -1, caret);
  2075. set_caret_column(col, false, caret);
  2076. selection_drag_attempt = false;
  2077. bool caret_moved = get_caret_column(caret) != prev_col || get_caret_line(caret) != prev_line;
  2078. if (selecting_enabled && mb->is_shift_pressed() && !has_selection(caret) && caret_moved) {
  2079. // Select from the previous caret position.
  2080. select(prev_line, prev_col, line, col, caret);
  2081. }
  2082. // Start regular select mode.
  2083. set_selection_mode(SelectionMode::SELECTION_MODE_POINTER);
  2084. _update_selection_mode_pointer(true);
  2085. } else if (is_triple_click) {
  2086. // Start triple-click select line mode.
  2087. set_selection_mode(SelectionMode::SELECTION_MODE_LINE);
  2088. _update_selection_mode_line(true);
  2089. last_dblclk = 0;
  2090. } else if (mb->is_double_click()) {
  2091. // Start double-click select word mode.
  2092. set_selection_mode(SelectionMode::SELECTION_MODE_WORD);
  2093. _update_selection_mode_word(true);
  2094. last_dblclk = OS::get_singleton()->get_ticks_msec();
  2095. last_dblclk_pos = mb->get_position();
  2096. }
  2097. // Click inline objects.
  2098. if (inline_object_click_handler.is_valid()) {
  2099. int xmargin_beg = Math::ceil(theme_cache.style_normal->get_margin(SIDE_LEFT)) + gutters_width + gutter_padding;
  2100. int wrap_i = get_line_wrap_index_at_column(pos.y, pos.x);
  2101. const float wrap_indent = _get_wrap_indent_offset(pos.y, wrap_i, is_layout_rtl());
  2102. Ref<TextParagraph> ldata = text.get_line_data(line);
  2103. for (const Variant &inline_key : ldata->get_line_objects(wrap_i)) {
  2104. if (!is_inline_info_valid(inline_key)) {
  2105. continue;
  2106. }
  2107. Dictionary info = inline_key.duplicate();
  2108. info["line"] = line;
  2109. Rect2 obj_rect = ldata->get_line_object_rect(wrap_i, inline_key);
  2110. obj_rect.position.x += xmargin_beg + wrap_indent - first_visible_col;
  2111. if (mpos.x > obj_rect.position.x && mpos.x < obj_rect.get_end().x) {
  2112. Rect2 col_rect = get_rect_at_line_column(line, col);
  2113. col_rect.position += get_screen_position() + Vector2(col_rect.size.x, 0);
  2114. col_rect.size = obj_rect.size;
  2115. set_selection_mode(TextEdit::SelectionMode::SELECTION_MODE_NONE);
  2116. inline_object_click_handler.call(info, col_rect);
  2117. break;
  2118. }
  2119. }
  2120. }
  2121. queue_accessibility_update();
  2122. queue_redraw();
  2123. }
  2124. if (is_middle_mouse_paste_enabled() && mb->get_button_index() == MouseButton::MIDDLE && DisplayServer::get_singleton()->has_feature(DisplayServer::FEATURE_CLIPBOARD_PRIMARY)) {
  2125. apply_ime();
  2126. paste_primary_clipboard();
  2127. }
  2128. if (mb->get_button_index() == MouseButton::RIGHT && (context_menu_enabled || is_move_caret_on_right_click_enabled())) {
  2129. _push_current_op();
  2130. _reset_caret_blink_timer();
  2131. apply_ime();
  2132. _cancel_drag_and_drop_text();
  2133. set_selection_mode(SELECTION_MODE_NONE);
  2134. Point2i pos = get_line_column_at_pos(mpos);
  2135. int mouse_line = pos.y;
  2136. int mouse_column = pos.x;
  2137. if (is_move_caret_on_right_click_enabled()) {
  2138. bool selection_clicked = get_selection_at_line_column(mouse_line, mouse_column, true) >= 0;
  2139. if (!selection_clicked) {
  2140. deselect();
  2141. remove_secondary_carets();
  2142. set_caret_line(mouse_line, false, false, -1);
  2143. set_caret_column(mouse_column);
  2144. }
  2145. }
  2146. if (context_menu_enabled) {
  2147. _update_context_menu();
  2148. menu->set_position(get_screen_transform().xform(mpos));
  2149. menu->reset_size();
  2150. menu->popup();
  2151. grab_focus();
  2152. }
  2153. }
  2154. } else {
  2155. if (has_ime_text()) {
  2156. // Ignore mouse up in IME input mode.
  2157. return;
  2158. }
  2159. if (mb->get_button_index() == MouseButton::LEFT) {
  2160. if (!drag_action && selection_drag_attempt && is_mouse_over_selection()) {
  2161. // This is not a drag and drop attempt, update the caret.
  2162. selection_drag_attempt = false;
  2163. remove_secondary_carets();
  2164. deselect();
  2165. Point2i pos = get_line_column_at_pos(get_local_mouse_pos());
  2166. set_caret_line(pos.y, false, true, -1, 0);
  2167. set_caret_column(pos.x, true, 0);
  2168. }
  2169. dragging_minimap = false;
  2170. dragging_selection = false;
  2171. can_drag_minimap = false;
  2172. click_select_held->stop();
  2173. if (DisplayServer::get_singleton()->has_feature(DisplayServer::FEATURE_CLIPBOARD_PRIMARY)) {
  2174. DisplayServer::get_singleton()->clipboard_set_primary(get_selected_text());
  2175. }
  2176. }
  2177. if (editable) {
  2178. _show_virtual_keyboard();
  2179. }
  2180. }
  2181. }
  2182. const Ref<InputEventPanGesture> pan_gesture = p_gui_input;
  2183. if (pan_gesture.is_valid()) {
  2184. const real_t delta = pan_gesture->get_delta().y;
  2185. if (delta < 0) {
  2186. _scroll_up(-delta, false);
  2187. } else {
  2188. _scroll_down(delta, false);
  2189. }
  2190. h_scroll->set_value(h_scroll->get_value() + pan_gesture->get_delta().x * 100);
  2191. if (v_scroll->get_value() != prev_v_scroll || h_scroll->get_value() != prev_h_scroll) {
  2192. accept_event(); // Accept event if scroll changed.
  2193. }
  2194. queue_accessibility_update();
  2195. return;
  2196. }
  2197. Ref<InputEventMouseMotion> mm = p_gui_input;
  2198. if (mm.is_valid()) {
  2199. Vector2i mpos = mm->get_position();
  2200. if (is_layout_rtl()) {
  2201. mpos.x = get_size().x - mpos.x;
  2202. }
  2203. if (mm->get_button_mask().has_flag(MouseButtonMask::LEFT) && get_viewport()->gui_get_drag_data() == Variant()) {
  2204. // Update if not in drag and drop.
  2205. _reset_caret_blink_timer();
  2206. if (draw_minimap && !dragging_selection) {
  2207. _update_minimap_drag();
  2208. }
  2209. if (!dragging_minimap && !has_ime_text()) {
  2210. switch (selecting_mode) {
  2211. case SelectionMode::SELECTION_MODE_POINTER: {
  2212. _update_selection_mode_pointer();
  2213. } break;
  2214. case SelectionMode::SELECTION_MODE_WORD: {
  2215. _update_selection_mode_word();
  2216. } break;
  2217. case SelectionMode::SELECTION_MODE_LINE: {
  2218. _update_selection_mode_line();
  2219. } break;
  2220. default: {
  2221. break;
  2222. }
  2223. }
  2224. }
  2225. }
  2226. // Update hovered gutter.
  2227. Vector2i current_hovered_gutter = _get_hovered_gutter(mpos);
  2228. if (current_hovered_gutter != hovered_gutter) {
  2229. hovered_gutter = current_hovered_gutter;
  2230. queue_redraw();
  2231. }
  2232. if (drag_action && can_drop_data(mpos, get_viewport()->gui_get_drag_data())) {
  2233. apply_ime();
  2234. // Update drag and drop caret.
  2235. drag_caret_force_displayed = true;
  2236. Point2i pos = get_line_column_at_pos(get_local_mouse_pos());
  2237. if (drag_caret_index == -1) {
  2238. // Force create a new caret for drag and drop.
  2239. carets.push_back(Caret());
  2240. drag_caret_index = carets.size() - 1;
  2241. }
  2242. drag_caret_force_displayed = true;
  2243. set_caret_line(pos.y, false, true, -1, drag_caret_index);
  2244. set_caret_column(pos.x, true, drag_caret_index);
  2245. dragging_selection = true;
  2246. }
  2247. }
  2248. if (draw_minimap && !dragging_selection) {
  2249. _update_minimap_hover();
  2250. }
  2251. if (v_scroll->get_value() != prev_v_scroll || h_scroll->get_value() != prev_h_scroll) {
  2252. accept_event(); // Accept event if scroll changed.
  2253. }
  2254. Ref<InputEventKey> k = p_gui_input;
  2255. if (k.is_valid()) {
  2256. if (alt_input(p_gui_input)) {
  2257. accept_event();
  2258. return;
  2259. }
  2260. if (!k->is_pressed()) {
  2261. return;
  2262. }
  2263. // If a modifier has been pressed, and nothing else, return.
  2264. 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) {
  2265. return;
  2266. }
  2267. _cancel_drag_and_drop_text();
  2268. _reset_caret_blink_timer();
  2269. // Allow unicode handling if:
  2270. // * No modifiers are pressed (except Shift and CapsLock)
  2271. bool allow_unicode_handling = !(k->is_ctrl_pressed() || k->is_alt_pressed() || k->is_meta_pressed());
  2272. // Check and handle all built-in shortcuts.
  2273. // NEWLINES.
  2274. if (k->is_action("ui_text_newline_above", true)) {
  2275. _new_line(false, true);
  2276. accept_event();
  2277. return;
  2278. }
  2279. if (k->is_action("ui_text_newline_blank", true)) {
  2280. _new_line(false);
  2281. accept_event();
  2282. return;
  2283. }
  2284. if (k->is_action("ui_text_newline", true)) {
  2285. _new_line();
  2286. accept_event();
  2287. return;
  2288. }
  2289. // BACKSPACE AND DELETE.
  2290. if (k->is_action("ui_text_backspace_all_to_left", true)) {
  2291. _do_backspace(false, true);
  2292. accept_event();
  2293. return;
  2294. }
  2295. if (k->is_action("ui_text_backspace_word", true)) {
  2296. _do_backspace(true);
  2297. accept_event();
  2298. return;
  2299. }
  2300. if (k->is_action("ui_text_backspace", true)) {
  2301. _do_backspace();
  2302. accept_event();
  2303. return;
  2304. }
  2305. if (k->is_action("ui_text_delete_all_to_right", true)) {
  2306. _delete(false, true);
  2307. accept_event();
  2308. return;
  2309. }
  2310. if (k->is_action("ui_text_delete_word", true)) {
  2311. _delete(true);
  2312. accept_event();
  2313. return;
  2314. }
  2315. if (k->is_action("ui_text_delete", true)) {
  2316. _delete();
  2317. accept_event();
  2318. return;
  2319. }
  2320. // SCROLLING.
  2321. if (k->is_action("ui_text_scroll_up", true)) {
  2322. _scroll_lines_up();
  2323. accept_event();
  2324. return;
  2325. }
  2326. if (k->is_action("ui_text_scroll_down", true)) {
  2327. _scroll_lines_down();
  2328. accept_event();
  2329. return;
  2330. }
  2331. if (is_shortcut_keys_enabled()) {
  2332. // SELECT ALL, SELECT WORD UNDER CARET, ADD SELECTION FOR NEXT OCCURRENCE, SKIP SELECTION FOR NEXT OCCURRENCE,
  2333. // CLEAR CARETS AND SELECTIONS, CUT, COPY, PASTE.
  2334. if (k->is_action("ui_text_select_all", true)) {
  2335. select_all();
  2336. accept_event();
  2337. return;
  2338. }
  2339. if (k->is_action("ui_text_select_word_under_caret", true)) {
  2340. select_word_under_caret();
  2341. accept_event();
  2342. return;
  2343. }
  2344. if (k->is_action("ui_text_add_selection_for_next_occurrence", true)) {
  2345. add_selection_for_next_occurrence();
  2346. accept_event();
  2347. return;
  2348. }
  2349. if (k->is_action("ui_text_skip_selection_for_next_occurrence", true)) {
  2350. skip_selection_for_next_occurrence();
  2351. accept_event();
  2352. return;
  2353. }
  2354. if (k->is_action("ui_text_clear_carets_and_selection", true)) {
  2355. // Since the default shortcut is ESC, accepts the event only if it's actually performed.
  2356. if (_clear_carets_and_selection()) {
  2357. accept_event();
  2358. return;
  2359. }
  2360. }
  2361. if (k->is_action("ui_cut", true)) {
  2362. cut();
  2363. accept_event();
  2364. return;
  2365. }
  2366. if (k->is_action("ui_copy", true)) {
  2367. copy();
  2368. accept_event();
  2369. return;
  2370. }
  2371. if (k->is_action("ui_paste", true)) {
  2372. paste();
  2373. accept_event();
  2374. return;
  2375. }
  2376. // UNDO/REDO.
  2377. if (k->is_action("ui_undo", true)) {
  2378. undo();
  2379. accept_event();
  2380. return;
  2381. }
  2382. if (k->is_action("ui_redo", true)) {
  2383. redo();
  2384. accept_event();
  2385. return;
  2386. }
  2387. if (k->is_action("ui_text_caret_add_below", true)) {
  2388. add_caret_at_carets(true);
  2389. accept_event();
  2390. return;
  2391. }
  2392. if (k->is_action("ui_text_caret_add_above", true)) {
  2393. add_caret_at_carets(false);
  2394. accept_event();
  2395. return;
  2396. }
  2397. }
  2398. // MISC.
  2399. if (k->is_action("ui_menu", true)) {
  2400. _push_current_op();
  2401. if (context_menu_enabled) {
  2402. _update_context_menu();
  2403. adjust_viewport_to_caret();
  2404. menu->set_position(get_screen_transform().xform(get_caret_draw_pos()));
  2405. menu->reset_size();
  2406. menu->popup();
  2407. menu->grab_focus();
  2408. }
  2409. accept_event();
  2410. return;
  2411. }
  2412. if (k->is_action("ui_text_toggle_insert_mode", true)) {
  2413. set_overtype_mode_enabled(!overtype_mode);
  2414. accept_event();
  2415. return;
  2416. }
  2417. if (k->is_action("ui_swap_input_direction", true)) {
  2418. _swap_current_input_direction();
  2419. accept_event();
  2420. return;
  2421. }
  2422. // CARET MOVEMENT
  2423. k = k->duplicate();
  2424. bool shift_pressed = k->is_shift_pressed();
  2425. // Remove shift or else actions will not match. Use above variable for selection.
  2426. k->set_shift_pressed(false);
  2427. // CARET MOVEMENT - LEFT, RIGHT.
  2428. if (k->is_action("ui_text_caret_word_left", true)) {
  2429. _move_caret_left(shift_pressed, true);
  2430. accept_event();
  2431. return;
  2432. }
  2433. if (k->is_action("ui_text_caret_left", true)) {
  2434. _move_caret_left(shift_pressed, false);
  2435. accept_event();
  2436. return;
  2437. }
  2438. if (k->is_action("ui_text_caret_word_right", true)) {
  2439. _move_caret_right(shift_pressed, true);
  2440. accept_event();
  2441. return;
  2442. }
  2443. if (k->is_action("ui_text_caret_right", true)) {
  2444. _move_caret_right(shift_pressed, false);
  2445. accept_event();
  2446. return;
  2447. }
  2448. // CARET MOVEMENT - UP, DOWN.
  2449. if (k->is_action("ui_text_caret_up", true)) {
  2450. _move_caret_up(shift_pressed);
  2451. accept_event();
  2452. return;
  2453. }
  2454. if (k->is_action("ui_text_caret_down", true)) {
  2455. _move_caret_down(shift_pressed);
  2456. accept_event();
  2457. return;
  2458. }
  2459. // CARET MOVEMENT - DOCUMENT START/END.
  2460. if (k->is_action("ui_text_caret_document_start", true)) { // && shift_pressed) {
  2461. _move_caret_document_start(shift_pressed);
  2462. accept_event();
  2463. return;
  2464. }
  2465. if (k->is_action("ui_text_caret_document_end", true)) { // && shift_pressed) {
  2466. _move_caret_document_end(shift_pressed);
  2467. accept_event();
  2468. return;
  2469. }
  2470. // CARET MOVEMENT - LINE START/END.
  2471. if (k->is_action("ui_text_caret_line_start", true)) {
  2472. _move_caret_to_line_start(shift_pressed);
  2473. accept_event();
  2474. return;
  2475. }
  2476. if (k->is_action("ui_text_caret_line_end", true)) {
  2477. _move_caret_to_line_end(shift_pressed);
  2478. accept_event();
  2479. return;
  2480. }
  2481. // CARET MOVEMENT - PAGE UP/DOWN.
  2482. if (k->is_action("ui_text_caret_page_up", true)) {
  2483. _move_caret_page_up(shift_pressed);
  2484. accept_event();
  2485. return;
  2486. }
  2487. if (k->is_action("ui_text_caret_page_down", true)) {
  2488. _move_caret_page_down(shift_pressed);
  2489. accept_event();
  2490. return;
  2491. }
  2492. // Toggle Tab mode.
  2493. if (k->is_action("ui_focus_mode", true)) {
  2494. tab_input_mode = !tab_input_mode;
  2495. accept_event();
  2496. return;
  2497. }
  2498. // Handle tab as it has no set unicode value.
  2499. if (tab_input_mode && k->is_action("ui_text_indent", true)) {
  2500. if (editable) {
  2501. insert_text_at_caret("\t");
  2502. }
  2503. accept_event();
  2504. return;
  2505. }
  2506. // Handle Unicode (if no modifiers active).
  2507. if (allow_unicode_handling && editable && k->get_unicode() >= 32) {
  2508. handle_unicode_input(k->get_unicode());
  2509. accept_event();
  2510. return;
  2511. }
  2512. }
  2513. }
  2514. /* Input actions. */
  2515. void TextEdit::_swap_current_input_direction() {
  2516. if (input_direction == TEXT_DIRECTION_LTR) {
  2517. input_direction = TEXT_DIRECTION_RTL;
  2518. } else {
  2519. input_direction = TEXT_DIRECTION_LTR;
  2520. }
  2521. for (int i = 0; i < carets.size(); i++) {
  2522. set_caret_column(get_caret_column(i), i == 0, i);
  2523. }
  2524. queue_redraw();
  2525. }
  2526. void TextEdit::_new_line(bool p_split_current_line, bool p_above) {
  2527. if (!editable) {
  2528. return;
  2529. }
  2530. begin_complex_operation();
  2531. begin_multicaret_edit();
  2532. for (int i = 0; i < get_caret_count(); i++) {
  2533. if (multicaret_edit_ignore_caret(i)) {
  2534. continue;
  2535. }
  2536. if (p_split_current_line) {
  2537. insert_text_at_caret("\n", i);
  2538. } else {
  2539. int line = get_caret_line(i);
  2540. insert_text("\n", line, p_above ? 0 : text[line].length(), p_above, p_above);
  2541. deselect(i);
  2542. set_caret_line(p_above ? line : line + 1, false, true, -1, i);
  2543. set_caret_column(0, i == 0, i);
  2544. }
  2545. }
  2546. end_multicaret_edit();
  2547. end_complex_operation();
  2548. }
  2549. void TextEdit::_move_caret_left(bool p_select, bool p_move_by_word) {
  2550. _push_current_op();
  2551. for (int i = 0; i < get_caret_count(); i++) {
  2552. // Handle selection.
  2553. if (p_select) {
  2554. _pre_shift_selection(i);
  2555. } else if (has_selection(i) && !p_move_by_word) {
  2556. // If a selection is active, move caret to start of selection.
  2557. set_caret_line(get_selection_from_line(i), false, true, -1, i);
  2558. set_caret_column(get_selection_from_column(i), i == 0, i);
  2559. deselect(i);
  2560. continue;
  2561. } else {
  2562. deselect(i);
  2563. }
  2564. if (p_move_by_word) {
  2565. int cc = get_caret_column(i);
  2566. // 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.
  2567. if (cc == 0 && get_caret_line(i) > 0) {
  2568. set_caret_line(get_caret_line(i) - 1, false, true, -1, i);
  2569. set_caret_column(text[get_caret_line(i)].length(), i == 0, i);
  2570. } else {
  2571. PackedInt32Array words = TS->shaped_text_get_word_breaks(text.get_line_data(get_caret_line(i))->get_rid());
  2572. if (words.is_empty() || cc <= words[0]) {
  2573. // Move to the start when there are no more words.
  2574. cc = 0;
  2575. } else {
  2576. for (int j = words.size() - 2; j >= 0; j = j - 2) {
  2577. if (words[j] < cc) {
  2578. cc = words[j];
  2579. break;
  2580. }
  2581. }
  2582. }
  2583. set_caret_column(cc, i == 0, i);
  2584. }
  2585. } else {
  2586. // 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.
  2587. if (get_caret_column(i) == 0) {
  2588. if (get_caret_line(i) > 0) {
  2589. int new_caret_line = get_caret_line(i) - get_next_visible_line_offset_from(CLAMP(get_caret_line(i) - 1, 0, text.size() - 1), -1);
  2590. set_caret_line(new_caret_line, false, true, -1, i);
  2591. set_caret_column(text[get_caret_line(i)].length(), i == 0, i);
  2592. }
  2593. } else {
  2594. if (caret_mid_grapheme_enabled) {
  2595. set_caret_column(get_caret_column(i) - 1, i == 0, i);
  2596. } else {
  2597. set_caret_column(TS->shaped_text_prev_character_pos(text.get_line_data(get_caret_line(i))->get_rid(), get_caret_column(i)), i == 0, i);
  2598. }
  2599. }
  2600. }
  2601. }
  2602. merge_overlapping_carets();
  2603. }
  2604. void TextEdit::_move_caret_right(bool p_select, bool p_move_by_word) {
  2605. _push_current_op();
  2606. for (int i = 0; i < get_caret_count(); i++) {
  2607. // Handle selection.
  2608. if (p_select) {
  2609. _pre_shift_selection(i);
  2610. } else if (has_selection(i) && !p_move_by_word) {
  2611. // If a selection is active, move caret to end of selection.
  2612. set_caret_line(get_selection_to_line(i), false, true, -1, i);
  2613. set_caret_column(get_selection_to_column(i), i == 0, i);
  2614. deselect(i);
  2615. continue;
  2616. } else {
  2617. deselect(i);
  2618. }
  2619. if (p_move_by_word) {
  2620. int cc = get_caret_column(i);
  2621. // 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.
  2622. if (cc == text[get_caret_line(i)].length() && get_caret_line(i) < text.size() - 1) {
  2623. set_caret_line(get_caret_line(i) + 1, false, true, -1, i);
  2624. set_caret_column(0, i == 0, i);
  2625. } else {
  2626. PackedInt32Array words = TS->shaped_text_get_word_breaks(text.get_line_data(get_caret_line(i))->get_rid());
  2627. if (words.is_empty() || cc >= words[words.size() - 1]) {
  2628. // Move to the end when there are no more words.
  2629. cc = text[get_caret_line(i)].length();
  2630. } else {
  2631. for (int j = 1; j < words.size(); j = j + 2) {
  2632. if (words[j] > cc) {
  2633. cc = words[j];
  2634. break;
  2635. }
  2636. }
  2637. }
  2638. set_caret_column(cc, i == 0, i);
  2639. }
  2640. } else {
  2641. // If we are at the end of the line, move the caret to the next line down.
  2642. if (get_caret_column(i) == text[get_caret_line(i)].length()) {
  2643. if (get_caret_line(i) < text.size() - 1) {
  2644. int new_caret_line = get_caret_line(i) + get_next_visible_line_offset_from(CLAMP(get_caret_line(i) + 1, 0, text.size() - 1), 1);
  2645. set_caret_line(new_caret_line, false, false, -1, i);
  2646. set_caret_column(0, i == 0, i);
  2647. }
  2648. } else {
  2649. if (caret_mid_grapheme_enabled) {
  2650. set_caret_column(get_caret_column(i) + 1, i == 0, i);
  2651. } else {
  2652. set_caret_column(TS->shaped_text_next_character_pos(text.get_line_data(get_caret_line(i))->get_rid(), get_caret_column(i)), i == 0, i);
  2653. }
  2654. }
  2655. }
  2656. }
  2657. merge_overlapping_carets();
  2658. }
  2659. void TextEdit::_move_caret_up(bool p_select) {
  2660. _push_current_op();
  2661. for (int i = 0; i < get_caret_count(); i++) {
  2662. if (p_select) {
  2663. _pre_shift_selection(i);
  2664. } else {
  2665. deselect(i);
  2666. }
  2667. int cur_wrap_index = get_caret_wrap_index(i);
  2668. if (cur_wrap_index > 0) {
  2669. set_caret_line(get_caret_line(i), true, false, cur_wrap_index - 1, i);
  2670. } else if (get_caret_line(i) == 0) {
  2671. set_caret_column(0, i == 0, i);
  2672. } else {
  2673. int new_line = get_caret_line(i) - get_next_visible_line_offset_from(get_caret_line(i) - 1, -1);
  2674. if (is_line_wrapped(new_line)) {
  2675. set_caret_line(new_line, i == 0, false, get_line_wrap_count(new_line), i);
  2676. } else {
  2677. set_caret_line(new_line, i == 0, false, 0, i);
  2678. }
  2679. }
  2680. }
  2681. merge_overlapping_carets();
  2682. }
  2683. void TextEdit::_move_caret_down(bool p_select) {
  2684. _push_current_op();
  2685. for (int i = 0; i < get_caret_count(); i++) {
  2686. if (p_select) {
  2687. _pre_shift_selection(i);
  2688. } else {
  2689. deselect(i);
  2690. }
  2691. int cur_wrap_index = get_caret_wrap_index(i);
  2692. if (cur_wrap_index < get_line_wrap_count(get_caret_line(i))) {
  2693. set_caret_line(get_caret_line(i), i == 0, false, cur_wrap_index + 1, i);
  2694. } else if (get_caret_line(i) == get_last_unhidden_line()) {
  2695. set_caret_column(text[get_caret_line(i)].length());
  2696. } else {
  2697. int new_line = get_caret_line(i) + get_next_visible_line_offset_from(CLAMP(get_caret_line(i) + 1, 0, text.size() - 1), 1);
  2698. set_caret_line(new_line, i == 0, false, 0, i);
  2699. }
  2700. }
  2701. merge_overlapping_carets();
  2702. }
  2703. void TextEdit::_move_caret_to_line_start(bool p_select) {
  2704. _push_current_op();
  2705. for (int i = 0; i < get_caret_count(); i++) {
  2706. if (p_select) {
  2707. _pre_shift_selection(i);
  2708. } else {
  2709. deselect(i);
  2710. }
  2711. // Move caret column to start of wrapped row and then to start of text.
  2712. Vector<String> rows = get_line_wrapped_text(get_caret_line(i));
  2713. int wi = get_caret_wrap_index(i);
  2714. int row_start_col = 0;
  2715. for (int j = 0; j < wi; j++) {
  2716. row_start_col += rows[j].length();
  2717. }
  2718. if (get_caret_column(i) == row_start_col || wi == 0) {
  2719. // Compute whitespace symbols sequence length.
  2720. int current_line_whitespace_len = get_first_non_whitespace_column(get_caret_line(i));
  2721. if (get_caret_column(i) == current_line_whitespace_len) {
  2722. set_caret_column(0, i == 0, i);
  2723. } else {
  2724. set_caret_column(current_line_whitespace_len, i == 0, i);
  2725. }
  2726. } else {
  2727. set_caret_column(row_start_col, i == 0, i);
  2728. }
  2729. }
  2730. merge_overlapping_carets();
  2731. }
  2732. void TextEdit::_move_caret_to_line_end(bool p_select) {
  2733. _push_current_op();
  2734. for (int i = 0; i < get_caret_count(); i++) {
  2735. if (p_select) {
  2736. _pre_shift_selection(i);
  2737. } else {
  2738. deselect(i);
  2739. }
  2740. // Move caret column to end of wrapped row and then to end of text.
  2741. Vector<String> rows = get_line_wrapped_text(get_caret_line(i));
  2742. int wi = get_caret_wrap_index(i);
  2743. int row_end_col = -1;
  2744. for (int j = 0; j < wi + 1; j++) {
  2745. row_end_col += rows[j].length();
  2746. }
  2747. if (wi == rows.size() - 1 || get_caret_column(i) == row_end_col) {
  2748. set_caret_column(text[get_caret_line(i)].length(), i == 0, i);
  2749. } else {
  2750. set_caret_column(row_end_col, i == 0, i);
  2751. }
  2752. }
  2753. merge_overlapping_carets();
  2754. }
  2755. void TextEdit::_move_caret_page_up(bool p_select) {
  2756. _push_current_op();
  2757. for (int i = 0; i < get_caret_count(); i++) {
  2758. if (p_select) {
  2759. _pre_shift_selection(i);
  2760. } else {
  2761. deselect(i);
  2762. }
  2763. Point2i next_line = get_next_visible_line_index_offset_from(get_caret_line(i), get_caret_wrap_index(i), -get_visible_line_count());
  2764. int n_line = get_caret_line(i) - next_line.x + 1;
  2765. set_caret_line(n_line, i == 0, false, next_line.y, i);
  2766. }
  2767. merge_overlapping_carets();
  2768. }
  2769. void TextEdit::_move_caret_page_down(bool p_select) {
  2770. _push_current_op();
  2771. for (int i = 0; i < get_caret_count(); i++) {
  2772. if (p_select) {
  2773. _pre_shift_selection(i);
  2774. } else {
  2775. deselect(i);
  2776. }
  2777. Point2i next_line = get_next_visible_line_index_offset_from(get_caret_line(i), get_caret_wrap_index(i), get_visible_line_count());
  2778. int n_line = get_caret_line(i) + next_line.x - 1;
  2779. set_caret_line(n_line, i == 0, false, next_line.y, i);
  2780. }
  2781. merge_overlapping_carets();
  2782. }
  2783. void TextEdit::_do_backspace(bool p_word, bool p_all_to_left) {
  2784. if (!editable) {
  2785. return;
  2786. }
  2787. start_action(EditAction::ACTION_BACKSPACE);
  2788. begin_multicaret_edit();
  2789. Vector<int> sorted_carets = get_sorted_carets();
  2790. sorted_carets.reverse();
  2791. for (int i = 0; i < sorted_carets.size(); i++) {
  2792. int caret_index = sorted_carets[i];
  2793. if (multicaret_edit_ignore_caret(caret_index)) {
  2794. continue;
  2795. }
  2796. if (get_caret_column(caret_index) == 0 && get_caret_line(caret_index) == 0 && !has_selection(caret_index)) {
  2797. continue;
  2798. }
  2799. if (has_selection(caret_index) || (!p_all_to_left && !p_word) || get_caret_column(caret_index) == 0) {
  2800. backspace(caret_index);
  2801. continue;
  2802. }
  2803. if (p_all_to_left) {
  2804. // Remove everything to left of caret to the start of the line.
  2805. int caret_current_column = get_caret_column(caret_index);
  2806. _remove_text(get_caret_line(caret_index), 0, get_caret_line(caret_index), caret_current_column);
  2807. collapse_carets(get_caret_line(caret_index), 0, get_caret_line(caret_index), caret_current_column);
  2808. set_caret_column(0, caret_index == 0, caret_index);
  2809. _offset_carets_after(get_caret_line(caret_index), caret_current_column, get_caret_line(caret_index), 0);
  2810. continue;
  2811. }
  2812. if (p_word) {
  2813. // Remove text to the start of the word left of the caret.
  2814. int from_column = get_caret_column(caret_index);
  2815. int column = get_caret_column(caret_index);
  2816. // Check for the case "<word><space><caret>" and ignore the space.
  2817. // No need to check for column being 0 since it is checked above.
  2818. if (is_whitespace(text[get_caret_line(caret_index)][get_caret_column(caret_index) - 1])) {
  2819. column -= 1;
  2820. }
  2821. // Get a list with the indices of the word bounds of the given text line.
  2822. const PackedInt32Array words = TS->shaped_text_get_word_breaks(text.get_line_data(get_caret_line(caret_index))->get_rid());
  2823. if (words.is_empty() || column <= words[0]) {
  2824. // Delete to the start when there are no more words.
  2825. column = 0;
  2826. } else {
  2827. // Otherwise search for the first word break that is smaller than the index from we're currently deleting.
  2828. for (int c = words.size() - 2; c >= 0; c = c - 2) {
  2829. if (words[c] < column) {
  2830. column = words[c];
  2831. break;
  2832. }
  2833. }
  2834. }
  2835. _remove_text(get_caret_line(caret_index), column, get_caret_line(caret_index), from_column);
  2836. collapse_carets(get_caret_line(caret_index), column, get_caret_line(caret_index), from_column);
  2837. set_caret_column(column, caret_index == 0, caret_index);
  2838. _offset_carets_after(get_caret_line(caret_index), from_column, get_caret_line(caret_index), column);
  2839. }
  2840. }
  2841. end_multicaret_edit();
  2842. end_action();
  2843. }
  2844. void TextEdit::_delete(bool p_word, bool p_all_to_right) {
  2845. if (!editable) {
  2846. return;
  2847. }
  2848. start_action(EditAction::ACTION_DELETE);
  2849. begin_multicaret_edit();
  2850. Vector<int> sorted_carets = get_sorted_carets();
  2851. for (int i = 0; i < sorted_carets.size(); i++) {
  2852. int caret_index = sorted_carets[i];
  2853. if (multicaret_edit_ignore_caret(caret_index)) {
  2854. continue;
  2855. }
  2856. if (has_selection(caret_index)) {
  2857. delete_selection(caret_index);
  2858. continue;
  2859. }
  2860. int curline_len = text[get_caret_line(caret_index)].length();
  2861. if (get_caret_line(caret_index) == text.size() - 1 && get_caret_column(caret_index) == curline_len) {
  2862. continue; // Last line, last column: Nothing to do.
  2863. }
  2864. int next_line = get_caret_column(caret_index) < curline_len ? get_caret_line(caret_index) : get_caret_line(caret_index) + 1;
  2865. int next_column;
  2866. if (p_all_to_right) {
  2867. if (get_caret_column(caret_index) == curline_len) {
  2868. continue;
  2869. }
  2870. // Delete everything to right of caret.
  2871. next_column = curline_len;
  2872. next_line = get_caret_line(caret_index);
  2873. } else if (p_word && get_caret_column(caret_index) < curline_len - 1) {
  2874. // Delete next word to right of caret.
  2875. int line = get_caret_line(caret_index);
  2876. int column = get_caret_column(caret_index);
  2877. PackedInt32Array words = TS->shaped_text_get_word_breaks(text.get_line_data(line)->get_rid());
  2878. if (words.is_empty() || column >= words[words.size() - 1]) {
  2879. // Delete to the end when there are no more words.
  2880. column = text[get_caret_line(i)].length();
  2881. } else {
  2882. for (int j = 1; j < words.size(); j = j + 2) {
  2883. if (words[j] > column) {
  2884. column = words[j];
  2885. break;
  2886. }
  2887. }
  2888. }
  2889. next_line = line;
  2890. next_column = column;
  2891. } else {
  2892. // Delete one character.
  2893. if (caret_mid_grapheme_enabled) {
  2894. next_column = get_caret_column(caret_index) < curline_len ? (get_caret_column(caret_index) + 1) : 0;
  2895. } else {
  2896. next_column = get_caret_column(caret_index) < curline_len ? TS->shaped_text_next_character_pos(text.get_line_data(get_caret_line(caret_index))->get_rid(), (get_caret_column(caret_index))) : 0;
  2897. }
  2898. }
  2899. _remove_text(get_caret_line(caret_index), get_caret_column(caret_index), next_line, next_column);
  2900. collapse_carets(get_caret_line(caret_index), get_caret_column(caret_index), next_line, next_column);
  2901. _offset_carets_after(next_line, next_column, get_caret_line(caret_index), get_caret_column(caret_index));
  2902. }
  2903. end_multicaret_edit();
  2904. end_action();
  2905. }
  2906. void TextEdit::_move_caret_document_start(bool p_select) {
  2907. remove_secondary_carets();
  2908. if (p_select) {
  2909. _pre_shift_selection(0);
  2910. } else {
  2911. deselect();
  2912. }
  2913. set_caret_line(0, false, true, -1);
  2914. set_caret_column(0);
  2915. }
  2916. void TextEdit::_move_caret_document_end(bool p_select) {
  2917. remove_secondary_carets();
  2918. if (p_select) {
  2919. _pre_shift_selection(0);
  2920. } else {
  2921. deselect();
  2922. }
  2923. set_caret_line(get_last_unhidden_line(), true, false, -1);
  2924. set_caret_column(text[get_caret_line()].length());
  2925. }
  2926. bool TextEdit::_clear_carets_and_selection() {
  2927. _push_current_op();
  2928. if (get_caret_count() > 1) {
  2929. remove_secondary_carets();
  2930. return true;
  2931. }
  2932. if (has_selection()) {
  2933. deselect();
  2934. return true;
  2935. }
  2936. return false;
  2937. }
  2938. void TextEdit::_update_placeholder() {
  2939. if (theme_cache.font.is_null() || theme_cache.font_size <= 0) {
  2940. return; // Not in tree?
  2941. }
  2942. const String placeholder_translated = atr(placeholder_text);
  2943. // Placeholder is generally smaller then text documents, and updates less so this should be fast enough for now.
  2944. placeholder_data_buf->clear();
  2945. placeholder_data_buf->set_width(text.get_width());
  2946. BitField<TextServer::LineBreakFlag> flags = text.get_brk_flags();
  2947. if (text.is_indent_wrapped_lines()) {
  2948. flags.set_flag(TextServer::BREAK_TRIM_INDENT);
  2949. }
  2950. placeholder_data_buf->set_break_flags(flags);
  2951. if (text_direction == Control::TEXT_DIRECTION_INHERITED) {
  2952. placeholder_data_buf->set_direction(is_layout_rtl() ? TextServer::DIRECTION_RTL : TextServer::DIRECTION_LTR);
  2953. } else {
  2954. placeholder_data_buf->set_direction((TextServer::Direction)text_direction);
  2955. }
  2956. placeholder_data_buf->set_preserve_control(draw_control_chars);
  2957. placeholder_data_buf->add_string(placeholder_translated, theme_cache.font, theme_cache.font_size, language);
  2958. placeholder_bidi_override = structured_text_parser(st_parser, st_args, placeholder_translated);
  2959. if (placeholder_bidi_override.is_empty()) {
  2960. TS->shaped_text_set_bidi_override(placeholder_data_buf->get_rid(), placeholder_bidi_override);
  2961. }
  2962. if (get_tab_size() > 0) {
  2963. Vector<float> tabs;
  2964. tabs.push_back(theme_cache.font->get_char_size(' ', theme_cache.font_size).width * get_tab_size());
  2965. placeholder_data_buf->tab_align(tabs);
  2966. }
  2967. // Update height.
  2968. const int wrap_amount = placeholder_data_buf->get_line_count() - 1;
  2969. placeholder_line_height = theme_cache.font->get_height(theme_cache.font_size);
  2970. for (int i = 0; i <= wrap_amount; i++) {
  2971. placeholder_line_height = MAX(placeholder_line_height, placeholder_data_buf->get_line_size(i).y);
  2972. }
  2973. // Update width.
  2974. placeholder_max_width = placeholder_data_buf->get_size().x;
  2975. // Update wrapped rows.
  2976. placeholder_wrapped_rows.clear();
  2977. for (int i = 0; i <= wrap_amount; i++) {
  2978. Vector2i line_range = placeholder_data_buf->get_line_range(i);
  2979. placeholder_wrapped_rows.push_back(placeholder_translated.substr(line_range.x, line_range.y - line_range.x));
  2980. }
  2981. }
  2982. bool TextEdit::_using_placeholder() const {
  2983. return text.size() == 1 && text[0].is_empty() && ime_text.is_empty();
  2984. }
  2985. void TextEdit::_update_theme_item_cache() {
  2986. Control::_update_theme_item_cache();
  2987. theme_cache.base_scale = get_theme_default_base_scale();
  2988. use_selected_font_color = theme_cache.font_selected_color != Color(0, 0, 0, 0);
  2989. if (text.get_line_height() + theme_cache.line_spacing < 1) {
  2990. WARN_PRINT("Line height is too small, please increase font_size and/or line_spacing");
  2991. }
  2992. }
  2993. void TextEdit::_update_caches() {
  2994. /* Text properties. */
  2995. TextServer::Direction dir;
  2996. if (text_direction == Control::TEXT_DIRECTION_INHERITED) {
  2997. dir = is_layout_rtl() ? TextServer::DIRECTION_RTL : TextServer::DIRECTION_LTR;
  2998. } else {
  2999. dir = (TextServer::Direction)text_direction;
  3000. }
  3001. text.set_direction_and_language(dir, (!language.is_empty()) ? language : TranslationServer::get_singleton()->get_tool_locale());
  3002. text.set_draw_control_chars(draw_control_chars);
  3003. text.set_font(theme_cache.font);
  3004. text.set_font_size(theme_cache.font_size);
  3005. text.invalidate_font();
  3006. _update_placeholder();
  3007. /* Syntax highlighting. */
  3008. if (syntax_highlighter.is_valid()) {
  3009. syntax_highlighter->set_text_edit(this);
  3010. }
  3011. _clear_syntax_highlighting_cache();
  3012. queue_accessibility_update();
  3013. }
  3014. void TextEdit::_close_ime_window() {
  3015. DisplayServer::WindowID wid = get_window() ? get_window()->get_window_id() : DisplayServer::INVALID_WINDOW_ID;
  3016. if (wid == DisplayServer::INVALID_WINDOW_ID || !DisplayServer::get_singleton()->has_feature(DisplayServer::FEATURE_IME)) {
  3017. return;
  3018. }
  3019. DisplayServer::get_singleton()->window_set_ime_position(Point2(), wid);
  3020. DisplayServer::get_singleton()->window_set_ime_active(false, wid);
  3021. }
  3022. void TextEdit::_update_ime_window_position() {
  3023. DisplayServer::WindowID wid = get_window() ? get_window()->get_window_id() : DisplayServer::INVALID_WINDOW_ID;
  3024. if (wid == DisplayServer::INVALID_WINDOW_ID || !DisplayServer::get_singleton()->has_feature(DisplayServer::FEATURE_IME)) {
  3025. return;
  3026. }
  3027. DisplayServer::get_singleton()->window_set_ime_active(true, wid);
  3028. Point2 pos = get_global_position() + get_caret_draw_pos();
  3029. if (get_window()->get_embedder()) {
  3030. pos += get_viewport()->get_popup_base_transform().get_origin();
  3031. }
  3032. // Take into account the window's transform.
  3033. pos = get_window()->get_screen_transform().xform(pos);
  3034. // The window will move to the updated position the next time the IME is updated, not immediately.
  3035. DisplayServer::get_singleton()->window_set_ime_position(pos, wid);
  3036. }
  3037. void TextEdit::_update_ime_text() {
  3038. if (has_ime_text()) {
  3039. // Update text to visually include IME text.
  3040. for (int i = 0; i < get_caret_count(); i++) {
  3041. int l = get_caret_line(i);
  3042. String text_with_ime = text[l].substr(0, get_caret_column(i)) + ime_text + text[l].substr(get_caret_column(i));
  3043. text.set_ime(l, text_with_ime, structured_text_parser(st_parser, st_args, text_with_ime));
  3044. emit_signal(SNAME("lines_edited_from"), l, l);
  3045. }
  3046. } else {
  3047. // Reset text.
  3048. for (int i = 0; i < get_caret_count(); i++) {
  3049. int l = get_caret_line(i);
  3050. text.set_ime(l, String(), Array());
  3051. emit_signal(SNAME("lines_edited_from"), l, l);
  3052. }
  3053. }
  3054. _clear_syntax_highlighting_cache();
  3055. queue_accessibility_update();
  3056. queue_redraw();
  3057. }
  3058. void TextEdit::_show_virtual_keyboard() {
  3059. _update_ime_window_position();
  3060. if (virtual_keyboard_enabled && DisplayServer::get_singleton()->has_feature(DisplayServer::FEATURE_VIRTUAL_KEYBOARD)) {
  3061. int caret_start = -1;
  3062. int caret_end = -1;
  3063. if (!has_selection(0)) {
  3064. String full_text = _base_get_text(0, 0, get_caret_line(), get_caret_column());
  3065. caret_start = full_text.length();
  3066. } else {
  3067. String pre_text = _base_get_text(0, 0, get_selection_from_line(), get_selection_from_column());
  3068. String post_text = get_selected_text(0);
  3069. caret_start = pre_text.length();
  3070. caret_end = caret_start + post_text.length();
  3071. }
  3072. DisplayServer::get_singleton()->virtual_keyboard_show(get_text(), get_global_rect(), DisplayServer::KEYBOARD_TYPE_MULTILINE, -1, caret_start, caret_end);
  3073. }
  3074. }
  3075. /* General overrides. */
  3076. Size2 TextEdit::get_minimum_size() const {
  3077. Size2 size = theme_cache.style_normal->get_minimum_size();
  3078. if (fit_content_height) {
  3079. size.y += content_size_cache.y;
  3080. }
  3081. if (fit_content_width) {
  3082. size.x += content_size_cache.x;
  3083. }
  3084. return size;
  3085. }
  3086. bool TextEdit::is_text_field() const {
  3087. return true;
  3088. }
  3089. Variant TextEdit::get_drag_data(const Point2 &p_point) {
  3090. Variant ret = Control::get_drag_data(p_point);
  3091. if (ret != Variant()) {
  3092. return ret;
  3093. }
  3094. if (has_selection() && selection_drag_attempt) {
  3095. String t = get_selected_text();
  3096. Label *l = memnew(Label);
  3097. l->set_auto_translate_mode(AUTO_TRANSLATE_MODE_DISABLED); // Don't translate user input.
  3098. l->set_focus_mode(FOCUS_ACCESSIBILITY);
  3099. l->set_text(t);
  3100. set_drag_preview(l);
  3101. return t;
  3102. }
  3103. return Variant();
  3104. }
  3105. bool TextEdit::can_drop_data(const Point2 &p_point, const Variant &p_data) const {
  3106. bool drop_override = Control::can_drop_data(p_point, p_data); // In case user wants to drop custom data.
  3107. if (drop_override) {
  3108. return drop_override;
  3109. }
  3110. return is_editable() && p_data.is_string();
  3111. }
  3112. void TextEdit::drop_data(const Point2 &p_point, const Variant &p_data) {
  3113. Control::drop_data(p_point, p_data);
  3114. if (p_point == Vector2(Math::INF, Math::INF)) {
  3115. insert_text_at_caret(p_data);
  3116. } else if (p_data.is_string() && is_editable()) {
  3117. Point2i pos = get_line_column_at_pos(get_local_mouse_pos());
  3118. int drop_at_line = pos.y;
  3119. int drop_at_column = pos.x;
  3120. int selection_index = get_selection_at_line_column(drop_at_line, drop_at_column, !Input::get_singleton()->is_key_pressed(Key::CMD_OR_CTRL));
  3121. // Remove drag caret before the complex operation starts so it won't appear in undo.
  3122. remove_caret(drag_caret_index);
  3123. if (selection_drag_attempt && selection_index >= 0 && selection_index == drag_and_drop_origin_caret_index) {
  3124. // Dropped onto original selection, do nothing.
  3125. selection_drag_attempt = false;
  3126. return;
  3127. }
  3128. begin_complex_operation();
  3129. begin_multicaret_edit();
  3130. if (selection_drag_attempt) {
  3131. // Drop from self.
  3132. selection_drag_attempt = false;
  3133. if (!Input::get_singleton()->is_key_pressed(Key::CMD_OR_CTRL)) {
  3134. // Delete all selections.
  3135. int temp_caret = add_caret(drop_at_line, drop_at_column);
  3136. delete_selection();
  3137. // Use a temporary caret to update the drop at position.
  3138. drop_at_line = get_caret_line(temp_caret);
  3139. drop_at_column = get_caret_column(temp_caret);
  3140. }
  3141. }
  3142. remove_secondary_carets();
  3143. deselect();
  3144. // Insert the dragged text.
  3145. set_caret_line(drop_at_line, true, false, -1);
  3146. set_caret_column(drop_at_column);
  3147. insert_text_at_caret(p_data);
  3148. select(drop_at_line, drop_at_column, get_caret_line(), get_caret_column());
  3149. grab_focus();
  3150. adjust_viewport_to_caret();
  3151. end_multicaret_edit();
  3152. end_complex_operation();
  3153. }
  3154. }
  3155. Control::CursorShape TextEdit::get_cursor_shape(const Point2 &p_pos) const {
  3156. if (dragging_selection) {
  3157. return get_default_cursor_shape();
  3158. }
  3159. if (dragging_minimap) {
  3160. return CURSOR_ARROW;
  3161. }
  3162. Vector2i current_hovered_gutter = _get_hovered_gutter(p_pos);
  3163. if (current_hovered_gutter != Vector2i(-1, -1)) {
  3164. if (gutters[current_hovered_gutter.x].clickable || is_line_gutter_clickable(current_hovered_gutter.y, current_hovered_gutter.x)) {
  3165. return CURSOR_POINTING_HAND;
  3166. } else {
  3167. return CURSOR_ARROW;
  3168. }
  3169. }
  3170. int left_margin = Math::ceil(theme_cache.style_normal->get_margin(SIDE_LEFT));
  3171. if (p_pos.x < left_margin + gutters_width + gutter_padding) {
  3172. return CURSOR_ARROW;
  3173. }
  3174. int xmargin_end = get_size().width - Math::ceil(theme_cache.style_normal->get_margin(SIDE_RIGHT));
  3175. if (draw_minimap && p_pos.x > xmargin_end - minimap_width && p_pos.x <= xmargin_end) {
  3176. return CURSOR_ARROW;
  3177. }
  3178. // Hover inline objects.
  3179. if (inline_object_click_handler.is_valid()) {
  3180. Point2i pos = get_line_column_at_pos(p_pos);
  3181. int xmargin_beg = Math::ceil(theme_cache.style_normal->get_margin(SIDE_LEFT)) + gutters_width + gutter_padding;
  3182. int wrap_i = get_line_wrap_index_at_column(pos.y, pos.x);
  3183. const float wrap_indent = _get_wrap_indent_offset(pos.y, wrap_i, is_layout_rtl());
  3184. Ref<TextParagraph> ldata = text.get_line_data(pos.y);
  3185. for (Variant k : ldata->get_line_objects(wrap_i)) {
  3186. if (!is_inline_info_valid(k)) {
  3187. continue;
  3188. }
  3189. Rect2 obj_rect = ldata->get_line_object_rect(wrap_i, k);
  3190. obj_rect.position.x += xmargin_beg + wrap_indent - first_visible_col;
  3191. if (p_pos.x > obj_rect.position.x && p_pos.x < obj_rect.get_end().x) {
  3192. return CURSOR_POINTING_HAND;
  3193. }
  3194. }
  3195. }
  3196. return get_default_cursor_shape();
  3197. }
  3198. String TextEdit::get_tooltip(const Point2 &p_pos) const {
  3199. if (!tooltip_callback.is_valid()) {
  3200. return Control::get_tooltip(p_pos);
  3201. }
  3202. Point2i pos = get_line_column_at_pos(p_pos);
  3203. int line = pos.y;
  3204. int col = pos.x;
  3205. const String &text_line = text[line];
  3206. if (text_line.is_empty()) {
  3207. return Control::get_tooltip(p_pos);
  3208. }
  3209. const PackedInt32Array words = TS->shaped_text_get_word_breaks(text.get_line_data(line)->get_rid());
  3210. for (int i = 0; i < words.size(); i = i + 2) {
  3211. if (words[i] <= col && words[i + 1] >= col) {
  3212. Variant args[1] = { text_line.substr(words[i], words[i + 1] - words[i]) };
  3213. const Variant *argp[] = { &args[0] };
  3214. Callable::CallError ce;
  3215. Variant ret;
  3216. tooltip_callback.callp(argp, 1, ret, ce);
  3217. ERR_FAIL_COND_V_MSG(ce.error != Callable::CallError::CALL_OK, "", "Failed to call custom tooltip.");
  3218. return ret;
  3219. }
  3220. }
  3221. return Control::get_tooltip(p_pos);
  3222. }
  3223. void TextEdit::set_tooltip_request_func(const Callable &p_tooltip_callback) {
  3224. tooltip_callback = p_tooltip_callback;
  3225. queue_accessibility_update();
  3226. }
  3227. /* Text */
  3228. // Text properties.
  3229. bool TextEdit::has_ime_text() const {
  3230. return !ime_text.is_empty();
  3231. }
  3232. void TextEdit::cancel_ime() {
  3233. if (!has_ime_text()) {
  3234. _close_ime_window();
  3235. return;
  3236. }
  3237. ime_text = String();
  3238. ime_selection = Vector2i();
  3239. alt_start = false;
  3240. alt_start_no_hold = false;
  3241. _close_ime_window();
  3242. _update_ime_text();
  3243. }
  3244. void TextEdit::apply_ime() {
  3245. if (!has_ime_text()) {
  3246. _close_ime_window();
  3247. return;
  3248. }
  3249. // Force apply the current IME text.
  3250. if (alt_start || alt_start_no_hold) {
  3251. cancel_ime();
  3252. if ((alt_code > 0x31 && alt_code < 0xd800) || (alt_code > 0xdfff && alt_code <= 0x10ffff)) {
  3253. handle_unicode_input(alt_code);
  3254. }
  3255. } else {
  3256. String insert_ime_text = ime_text;
  3257. cancel_ime();
  3258. insert_text_at_caret(insert_ime_text);
  3259. }
  3260. }
  3261. void TextEdit::set_editable(bool p_editable) {
  3262. if (editable == p_editable) {
  3263. return;
  3264. }
  3265. editable = p_editable;
  3266. queue_accessibility_update();
  3267. queue_redraw();
  3268. }
  3269. bool TextEdit::is_editable() const {
  3270. return editable;
  3271. }
  3272. void TextEdit::set_text_direction(Control::TextDirection p_text_direction) {
  3273. ERR_FAIL_COND((int)p_text_direction < -1 || (int)p_text_direction > 3);
  3274. if (text_direction != p_text_direction) {
  3275. text_direction = p_text_direction;
  3276. if (text_direction != TEXT_DIRECTION_AUTO && text_direction != TEXT_DIRECTION_INHERITED) {
  3277. input_direction = text_direction;
  3278. }
  3279. TextServer::Direction dir;
  3280. if (text_direction == Control::TEXT_DIRECTION_INHERITED) {
  3281. dir = is_layout_rtl() ? TextServer::DIRECTION_RTL : TextServer::DIRECTION_LTR;
  3282. } else {
  3283. dir = (TextServer::Direction)text_direction;
  3284. }
  3285. text.set_direction_and_language(dir, (!language.is_empty()) ? language : TranslationServer::get_singleton()->get_tool_locale());
  3286. text.invalidate_font();
  3287. _update_placeholder();
  3288. if (menu_dir) {
  3289. menu_dir->set_item_checked(menu_dir->get_item_index(MENU_DIR_INHERITED), text_direction == TEXT_DIRECTION_INHERITED);
  3290. menu_dir->set_item_checked(menu_dir->get_item_index(MENU_DIR_AUTO), text_direction == TEXT_DIRECTION_AUTO);
  3291. menu_dir->set_item_checked(menu_dir->get_item_index(MENU_DIR_LTR), text_direction == TEXT_DIRECTION_LTR);
  3292. menu_dir->set_item_checked(menu_dir->get_item_index(MENU_DIR_RTL), text_direction == TEXT_DIRECTION_RTL);
  3293. }
  3294. queue_accessibility_update();
  3295. queue_redraw();
  3296. }
  3297. }
  3298. Control::TextDirection TextEdit::get_text_direction() const {
  3299. return text_direction;
  3300. }
  3301. void TextEdit::set_language(const String &p_language) {
  3302. if (language != p_language) {
  3303. language = p_language;
  3304. TextServer::Direction dir;
  3305. if (text_direction == Control::TEXT_DIRECTION_INHERITED) {
  3306. dir = is_layout_rtl() ? TextServer::DIRECTION_RTL : TextServer::DIRECTION_LTR;
  3307. } else {
  3308. dir = (TextServer::Direction)text_direction;
  3309. }
  3310. text.set_direction_and_language(dir, (!language.is_empty()) ? language : TranslationServer::get_singleton()->get_tool_locale());
  3311. text.invalidate_all();
  3312. _update_placeholder();
  3313. queue_accessibility_update();
  3314. queue_redraw();
  3315. }
  3316. }
  3317. String TextEdit::get_language() const {
  3318. return language;
  3319. }
  3320. void TextEdit::set_structured_text_bidi_override(TextServer::StructuredTextParser p_parser) {
  3321. if (st_parser != p_parser) {
  3322. st_parser = p_parser;
  3323. for (int i = 0; i < text.size(); i++) {
  3324. text.set(i, text[i], structured_text_parser(st_parser, st_args, text[i]));
  3325. }
  3326. queue_accessibility_update();
  3327. queue_redraw();
  3328. }
  3329. }
  3330. TextServer::StructuredTextParser TextEdit::get_structured_text_bidi_override() const {
  3331. return st_parser;
  3332. }
  3333. void TextEdit::set_structured_text_bidi_override_options(Array p_args) {
  3334. if (st_args == p_args) {
  3335. return;
  3336. }
  3337. st_args = p_args;
  3338. for (int i = 0; i < text.size(); i++) {
  3339. text.set(i, text[i], structured_text_parser(st_parser, st_args, text[i]));
  3340. }
  3341. queue_accessibility_update();
  3342. queue_redraw();
  3343. }
  3344. Array TextEdit::get_structured_text_bidi_override_options() const {
  3345. return st_args;
  3346. }
  3347. void TextEdit::set_tab_size(const int p_size) {
  3348. ERR_FAIL_COND_MSG(p_size <= 0, "Tab size must be greater than 0.");
  3349. if (p_size == text.get_tab_size()) {
  3350. return;
  3351. }
  3352. text.set_tab_size(p_size);
  3353. text.invalidate_all_lines();
  3354. _update_placeholder();
  3355. queue_accessibility_update();
  3356. queue_redraw();
  3357. }
  3358. int TextEdit::get_tab_size() const {
  3359. return text.get_tab_size();
  3360. }
  3361. void TextEdit::set_indent_wrapped_lines(bool p_enabled) {
  3362. if (text.is_indent_wrapped_lines() == p_enabled) {
  3363. return;
  3364. }
  3365. text.set_indent_wrapped_lines(p_enabled);
  3366. text.invalidate_all_lines();
  3367. _update_placeholder();
  3368. queue_redraw();
  3369. }
  3370. bool TextEdit::is_indent_wrapped_lines() const {
  3371. return text.is_indent_wrapped_lines();
  3372. }
  3373. void TextEdit::set_tab_input_mode(bool p_enabled) {
  3374. tab_input_mode = p_enabled;
  3375. }
  3376. bool TextEdit::get_tab_input_mode() const {
  3377. return tab_input_mode;
  3378. }
  3379. // User controls
  3380. void TextEdit::set_overtype_mode_enabled(bool p_enabled) {
  3381. if (overtype_mode == p_enabled) {
  3382. return;
  3383. }
  3384. overtype_mode = p_enabled;
  3385. queue_redraw();
  3386. }
  3387. bool TextEdit::is_overtype_mode_enabled() const {
  3388. return overtype_mode;
  3389. }
  3390. void TextEdit::set_context_menu_enabled(bool p_enabled) {
  3391. context_menu_enabled = p_enabled;
  3392. }
  3393. bool TextEdit::is_context_menu_enabled() const {
  3394. return context_menu_enabled;
  3395. }
  3396. void TextEdit::show_emoji_and_symbol_picker() {
  3397. _update_ime_window_position();
  3398. DisplayServer::get_singleton()->show_emoji_and_symbol_picker();
  3399. }
  3400. void TextEdit::set_emoji_menu_enabled(bool p_enabled) {
  3401. if (emoji_menu_enabled != p_enabled) {
  3402. emoji_menu_enabled = p_enabled;
  3403. _update_context_menu();
  3404. }
  3405. }
  3406. bool TextEdit::is_emoji_menu_enabled() const {
  3407. return emoji_menu_enabled;
  3408. }
  3409. void TextEdit::set_backspace_deletes_composite_character_enabled(bool p_enabled) {
  3410. backspace_deletes_composite_character_enabled = p_enabled;
  3411. }
  3412. bool TextEdit::is_backspace_deletes_composite_character_enabled() const {
  3413. return backspace_deletes_composite_character_enabled;
  3414. }
  3415. void TextEdit::set_shortcut_keys_enabled(bool p_enabled) {
  3416. shortcut_keys_enabled = p_enabled;
  3417. }
  3418. bool TextEdit::is_shortcut_keys_enabled() const {
  3419. return shortcut_keys_enabled;
  3420. }
  3421. void TextEdit::set_virtual_keyboard_enabled(bool p_enabled) {
  3422. virtual_keyboard_enabled = p_enabled;
  3423. }
  3424. bool TextEdit::is_virtual_keyboard_enabled() const {
  3425. return virtual_keyboard_enabled;
  3426. }
  3427. void TextEdit::set_virtual_keyboard_show_on_focus(bool p_show_on_focus) {
  3428. virtual_keyboard_show_on_focus = p_show_on_focus;
  3429. }
  3430. bool TextEdit::get_virtual_keyboard_show_on_focus() const {
  3431. return virtual_keyboard_show_on_focus;
  3432. }
  3433. void TextEdit::set_middle_mouse_paste_enabled(bool p_enabled) {
  3434. middle_mouse_paste_enabled = p_enabled;
  3435. }
  3436. bool TextEdit::is_middle_mouse_paste_enabled() const {
  3437. return middle_mouse_paste_enabled;
  3438. }
  3439. void TextEdit::set_empty_selection_clipboard_enabled(bool p_enabled) {
  3440. empty_selection_clipboard_enabled = p_enabled;
  3441. }
  3442. bool TextEdit::is_empty_selection_clipboard_enabled() const {
  3443. return empty_selection_clipboard_enabled;
  3444. }
  3445. // Text manipulation
  3446. void TextEdit::clear() {
  3447. setting_text = true;
  3448. _clear();
  3449. setting_text = false;
  3450. emit_signal(SNAME("text_set"));
  3451. }
  3452. void TextEdit::_clear() {
  3453. if (editable && undo_enabled) {
  3454. remove_secondary_carets();
  3455. _move_caret_document_start(false);
  3456. begin_complex_operation();
  3457. _remove_text(0, 0, MAX(0, get_line_count() - 1), MAX(get_line(MAX(get_line_count() - 1, 0)).size() - 1, 0));
  3458. insert_text_at_caret("");
  3459. text.clear();
  3460. end_complex_operation();
  3461. return;
  3462. }
  3463. // Cannot merge with above, as we are not part of the tree on creation.
  3464. int old_text_size = text.size();
  3465. clear_undo_history();
  3466. text.clear();
  3467. remove_secondary_carets();
  3468. set_caret_line(0, false, true, -1);
  3469. set_caret_column(0);
  3470. first_visible_col = 0;
  3471. first_visible_line = 0;
  3472. first_visible_line_wrap_ofs = 0;
  3473. carets.write[0].last_fit_x = 0;
  3474. deselect();
  3475. emit_signal(SNAME("lines_edited_from"), old_text_size, 0);
  3476. }
  3477. void TextEdit::set_text(const String &p_text) {
  3478. setting_text = true;
  3479. if (!undo_enabled) {
  3480. _clear();
  3481. insert_text_at_caret(p_text);
  3482. }
  3483. if (undo_enabled) {
  3484. remove_secondary_carets();
  3485. set_caret_line(0);
  3486. set_caret_column(0);
  3487. begin_complex_operation();
  3488. deselect();
  3489. _remove_text(0, 0, MAX(0, get_line_count() - 1), MAX(get_line(MAX(get_line_count() - 1, 0)).size() - 1, 0));
  3490. insert_text_at_caret(p_text);
  3491. end_complex_operation();
  3492. }
  3493. set_caret_line(0);
  3494. set_caret_column(0);
  3495. queue_accessibility_update();
  3496. queue_redraw();
  3497. setting_text = false;
  3498. emit_signal(SNAME("text_set"));
  3499. }
  3500. String TextEdit::get_text() const {
  3501. StringBuilder ret_text;
  3502. const int text_size = text.size();
  3503. for (int i = 0; i < text_size; i++) {
  3504. ret_text += text[i];
  3505. if (i != text_size - 1) {
  3506. ret_text += "\n";
  3507. }
  3508. }
  3509. return ret_text.as_string();
  3510. }
  3511. int TextEdit::get_line_count() const {
  3512. return text.size();
  3513. }
  3514. void TextEdit::set_placeholder(const String &p_text) {
  3515. if (placeholder_text == p_text) {
  3516. return;
  3517. }
  3518. placeholder_text = p_text;
  3519. _update_placeholder();
  3520. queue_accessibility_update();
  3521. queue_redraw();
  3522. }
  3523. String TextEdit::get_placeholder() const {
  3524. return placeholder_text;
  3525. }
  3526. void TextEdit::set_line(int p_line, const String &p_new_text) {
  3527. if (p_line < 0 || p_line >= text.size()) {
  3528. return;
  3529. }
  3530. begin_complex_operation();
  3531. int old_column = text[p_line].length();
  3532. // Set the affected carets column to update their last offset x.
  3533. for (int i = 0; i < get_caret_count(); i++) {
  3534. if (_is_line_col_in_range(get_caret_line(i), get_caret_column(i), p_line, 0, p_line, old_column)) {
  3535. set_caret_column(get_caret_column(i), false, i);
  3536. }
  3537. if (has_selection(i) && _is_line_col_in_range(get_selection_origin_line(i), get_selection_origin_column(i), p_line, 0, p_line, old_column)) {
  3538. set_selection_origin_column(get_selection_origin_column(i), i);
  3539. }
  3540. }
  3541. _remove_text(p_line, 0, p_line, old_column);
  3542. int new_line, new_column;
  3543. _insert_text(p_line, 0, p_new_text, &new_line, &new_column);
  3544. // Don't offset carets that were on the old line.
  3545. _offset_carets_after(p_line, old_column, new_line, new_column, false, false);
  3546. // Set the caret lines to update the column to match visually.
  3547. for (int i = 0; i < get_caret_count(); i++) {
  3548. if (_is_line_col_in_range(get_caret_line(i), get_caret_column(i), p_line, 0, p_line, old_column)) {
  3549. set_caret_line(get_caret_line(i), false, true, 0, i);
  3550. }
  3551. if (has_selection(i) && _is_line_col_in_range(get_selection_origin_line(i), get_selection_origin_column(i), p_line, 0, p_line, old_column)) {
  3552. set_selection_origin_line(get_selection_origin_line(i), true, 0, i);
  3553. }
  3554. }
  3555. merge_overlapping_carets();
  3556. end_complex_operation();
  3557. }
  3558. String TextEdit::get_line(int p_line) const {
  3559. if (p_line < 0 || p_line >= text.size()) {
  3560. return String();
  3561. }
  3562. return text[p_line];
  3563. }
  3564. String TextEdit::get_line_with_ime(int p_line) const {
  3565. if (p_line < 0 || p_line >= text.size()) {
  3566. return String();
  3567. }
  3568. return text.get_text_with_ime(p_line);
  3569. }
  3570. int TextEdit::get_line_width(int p_line, int p_wrap_index) const {
  3571. ERR_FAIL_INDEX_V(p_line, text.size(), 0);
  3572. ERR_FAIL_COND_V(p_wrap_index > get_line_wrap_count(p_line), 0);
  3573. return text.get_line_width(p_line, p_wrap_index);
  3574. }
  3575. int TextEdit::get_line_height() const {
  3576. return MAX(text.get_line_height() + theme_cache.line_spacing, 1);
  3577. }
  3578. int TextEdit::_get_wrapped_indent_level(int p_line, int &r_first_wrap) const {
  3579. ERR_FAIL_INDEX_V(p_line, text.size(), 0);
  3580. const Vector<Vector2i> wr = text.get_line_wrap_ranges(p_line);
  3581. r_first_wrap = 0;
  3582. int tab_count = 0;
  3583. int whitespace_count = 0;
  3584. int line_length = text[p_line].size();
  3585. for (int i = 0; i < line_length - 1; i++) {
  3586. if (r_first_wrap < wr.size() && i >= wr[r_first_wrap].y) {
  3587. tab_count = 0;
  3588. whitespace_count = 0;
  3589. r_first_wrap++;
  3590. }
  3591. if (text[p_line][i] == '\t') {
  3592. tab_count++;
  3593. } else if (text[p_line][i] == ' ') {
  3594. whitespace_count++;
  3595. } else {
  3596. break;
  3597. }
  3598. }
  3599. return tab_count * text.get_tab_size() + whitespace_count;
  3600. }
  3601. float TextEdit::_get_wrap_indent_offset(int p_line, int p_wrap_index, bool p_rtl) const {
  3602. if (!text.is_indent_wrapped_lines()) {
  3603. return 0;
  3604. }
  3605. int first_indent_line = 0;
  3606. _get_wrapped_indent_level(p_line, first_indent_line);
  3607. if (p_wrap_index > first_indent_line) {
  3608. return MIN(text.get_indent_offset(p_line, p_rtl), wrap_at_column * 0.6);
  3609. }
  3610. return 0;
  3611. }
  3612. int TextEdit::get_indent_level(int p_line) const {
  3613. ERR_FAIL_INDEX_V(p_line, text.size(), 0);
  3614. int tab_count = 0;
  3615. int whitespace_count = 0;
  3616. int line_length = text[p_line].size();
  3617. for (int i = 0; i < line_length - 1; i++) {
  3618. if (text[p_line][i] == '\t') {
  3619. tab_count++;
  3620. } else if (text[p_line][i] == ' ') {
  3621. whitespace_count++;
  3622. } else {
  3623. break;
  3624. }
  3625. }
  3626. return tab_count * text.get_tab_size() + whitespace_count;
  3627. }
  3628. int TextEdit::get_first_non_whitespace_column(int p_line) const {
  3629. ERR_FAIL_INDEX_V(p_line, text.size(), 0);
  3630. int col = 0;
  3631. while (col < text[p_line].length() && is_whitespace(text[p_line][col])) {
  3632. col++;
  3633. }
  3634. return col;
  3635. }
  3636. void TextEdit::swap_lines(int p_from_line, int p_to_line) {
  3637. ERR_FAIL_INDEX(p_from_line, text.size());
  3638. ERR_FAIL_INDEX(p_to_line, text.size());
  3639. if (p_from_line == p_to_line) {
  3640. return;
  3641. }
  3642. String from_line_text = get_line(p_from_line);
  3643. String to_line_text = get_line(p_to_line);
  3644. begin_complex_operation();
  3645. begin_multicaret_edit();
  3646. // Don't use set_line to avoid clamping and updating carets.
  3647. _remove_text(p_to_line, 0, p_to_line, text[p_to_line].length());
  3648. _insert_text(p_to_line, 0, from_line_text);
  3649. _remove_text(p_from_line, 0, p_from_line, text[p_from_line].length());
  3650. _insert_text(p_from_line, 0, to_line_text);
  3651. // Swap carets.
  3652. for (int i = 0; i < get_caret_count(); i++) {
  3653. bool selected = has_selection(i);
  3654. if (get_caret_line(i) == p_from_line || get_caret_line(i) == p_to_line) {
  3655. int caret_new_line = get_caret_line(i) == p_from_line ? p_to_line : p_from_line;
  3656. int caret_column = get_caret_column(i);
  3657. set_caret_line(caret_new_line, false, true, -1, i);
  3658. set_caret_column(caret_column, false, i);
  3659. }
  3660. if (selected && (get_selection_origin_line(i) == p_from_line || get_selection_origin_line(i) == p_to_line)) {
  3661. int origin_new_line = get_selection_origin_line(i) == p_from_line ? p_to_line : p_from_line;
  3662. int origin_column = get_selection_origin_column(i);
  3663. select(origin_new_line, origin_column, get_caret_line(i), get_caret_column(i), i);
  3664. }
  3665. }
  3666. // If only part of a selection was changed, it may now overlap.
  3667. merge_overlapping_carets();
  3668. end_multicaret_edit();
  3669. end_complex_operation();
  3670. }
  3671. void TextEdit::insert_line_at(int p_line, const String &p_text) {
  3672. ERR_FAIL_INDEX(p_line, text.size());
  3673. // Use a complex operation so subsequent calls aren't merged together.
  3674. begin_complex_operation();
  3675. int new_line, new_column;
  3676. _insert_text(p_line, 0, p_text + "\n", &new_line, &new_column);
  3677. _offset_carets_after(p_line, 0, new_line, new_column);
  3678. end_complex_operation();
  3679. }
  3680. void TextEdit::remove_line_at(int p_line, bool p_move_carets_down) {
  3681. ERR_FAIL_INDEX(p_line, text.size());
  3682. if (get_line_count() == 1) {
  3683. // Only one line, just remove contents.
  3684. begin_complex_operation();
  3685. int line_length = get_line(p_line).length();
  3686. _remove_text(p_line, 0, p_line, line_length);
  3687. collapse_carets(p_line, 0, p_line, line_length, true);
  3688. end_complex_operation();
  3689. return;
  3690. }
  3691. begin_complex_operation();
  3692. bool is_last_line = p_line == get_line_count() - 1;
  3693. int from_line = is_last_line ? p_line - 1 : p_line;
  3694. int next_line = is_last_line ? p_line : p_line + 1;
  3695. int from_column = is_last_line ? get_line(from_line).length() : 0;
  3696. int next_column = is_last_line ? get_line(next_line).length() : 0;
  3697. if ((!is_last_line && p_move_carets_down) || (p_line != 0 && !p_move_carets_down)) {
  3698. // Set the carets column to update their last offset x.
  3699. for (int i = 0; i < get_caret_count(); i++) {
  3700. if (get_caret_line(i) == p_line) {
  3701. set_caret_column(get_caret_column(i), false, i);
  3702. }
  3703. if (has_selection(i) && get_selection_origin_line(i) == p_line) {
  3704. set_selection_origin_column(get_selection_origin_column(i), i);
  3705. }
  3706. }
  3707. }
  3708. // Remove line.
  3709. _remove_text(from_line, from_column, next_line, next_column);
  3710. begin_multicaret_edit();
  3711. if ((is_last_line && p_move_carets_down) || (p_line == 0 && !p_move_carets_down)) {
  3712. // Collapse carets.
  3713. collapse_carets(from_line, from_column, next_line, next_column, true);
  3714. } else {
  3715. // Move carets to visually line up.
  3716. int target_line = p_move_carets_down ? p_line : p_line - 1;
  3717. for (int i = 0; i < get_caret_count(); i++) {
  3718. bool selected = has_selection(i);
  3719. if (get_caret_line(i) == p_line) {
  3720. set_caret_line(target_line, i == 0, true, 0, i);
  3721. }
  3722. if (selected && get_selection_origin_line(i) == p_line) {
  3723. set_selection_origin_line(target_line, true, 0, i);
  3724. select(get_selection_origin_line(i), get_selection_origin_column(i), get_caret_line(i), get_caret_column(i), i);
  3725. }
  3726. }
  3727. merge_overlapping_carets();
  3728. }
  3729. _offset_carets_after(next_line, next_column, from_line, from_column);
  3730. end_multicaret_edit();
  3731. end_complex_operation();
  3732. queue_accessibility_update();
  3733. queue_redraw();
  3734. }
  3735. void TextEdit::insert_text_at_caret(const String &p_text, int p_caret) {
  3736. ERR_FAIL_COND(p_caret >= get_caret_count() || p_caret < -1);
  3737. begin_complex_operation();
  3738. begin_multicaret_edit();
  3739. for (int i = 0; i < get_caret_count(); i++) {
  3740. if (p_caret != -1 && p_caret != i) {
  3741. continue;
  3742. }
  3743. if (p_caret == -1 && multicaret_edit_ignore_caret(i)) {
  3744. continue;
  3745. }
  3746. delete_selection(i);
  3747. int from_line = get_caret_line(i);
  3748. int from_col = get_caret_column(i);
  3749. int new_line, new_column;
  3750. _insert_text(from_line, from_col, p_text, &new_line, &new_column);
  3751. _update_scrollbars();
  3752. _offset_carets_after(from_line, from_col, new_line, new_column);
  3753. set_caret_line(new_line, false, true, -1, i);
  3754. set_caret_column(new_column, i == 0, i);
  3755. }
  3756. if (has_ime_text()) {
  3757. _update_ime_text();
  3758. }
  3759. end_multicaret_edit();
  3760. end_complex_operation();
  3761. }
  3762. void TextEdit::insert_text(const String &p_text, int p_line, int p_column, bool p_before_selection_begin, bool p_before_selection_end) {
  3763. ERR_FAIL_INDEX(p_line, text.size());
  3764. ERR_FAIL_INDEX(p_column, text[p_line].length() + 1);
  3765. begin_complex_operation();
  3766. int new_line, new_column;
  3767. _insert_text(p_line, p_column, p_text, &new_line, &new_column);
  3768. _offset_carets_after(p_line, p_column, new_line, new_column, p_before_selection_begin, p_before_selection_end);
  3769. end_complex_operation();
  3770. }
  3771. void TextEdit::remove_text(int p_from_line, int p_from_column, int p_to_line, int p_to_column) {
  3772. ERR_FAIL_INDEX(p_from_line, text.size());
  3773. ERR_FAIL_INDEX(p_from_column, text[p_from_line].length() + 1);
  3774. ERR_FAIL_INDEX(p_to_line, text.size());
  3775. ERR_FAIL_INDEX(p_to_column, text[p_to_line].length() + 1);
  3776. ERR_FAIL_COND(p_to_line < p_from_line);
  3777. ERR_FAIL_COND(p_to_line == p_from_line && p_to_column < p_from_column);
  3778. begin_complex_operation();
  3779. _remove_text(p_from_line, p_from_column, p_to_line, p_to_column);
  3780. collapse_carets(p_from_line, p_from_column, p_to_line, p_to_column);
  3781. _offset_carets_after(p_to_line, p_to_column, p_from_line, p_from_column);
  3782. end_complex_operation();
  3783. }
  3784. int TextEdit::get_last_unhidden_line() const {
  3785. // Returns the last line in the text that is not hidden.
  3786. if (!_is_hiding_enabled()) {
  3787. return text.size() - 1;
  3788. }
  3789. int last_line;
  3790. for (last_line = text.size() - 1; last_line > 0; last_line--) {
  3791. if (!_is_line_hidden(last_line)) {
  3792. break;
  3793. }
  3794. }
  3795. return last_line;
  3796. }
  3797. int TextEdit::get_next_visible_line_offset_from(int p_line_from, int p_visible_amount) const {
  3798. // Returns the number of lines (hidden and unhidden) from p_line_from to (p_line_from + visible_amount of unhidden lines).
  3799. ERR_FAIL_INDEX_V(p_line_from, text.size(), Math::abs(p_visible_amount));
  3800. if (!_is_hiding_enabled()) {
  3801. return Math::abs(p_visible_amount);
  3802. }
  3803. int num_visible = 0;
  3804. int num_total = 0;
  3805. if (p_visible_amount >= 0) {
  3806. for (int i = p_line_from; i < text.size(); i++) {
  3807. num_total++;
  3808. if (!_is_line_hidden(i)) {
  3809. num_visible++;
  3810. }
  3811. if (num_visible >= p_visible_amount) {
  3812. break;
  3813. }
  3814. }
  3815. } else {
  3816. p_visible_amount = Math::abs(p_visible_amount);
  3817. for (int i = p_line_from; i >= 0; i--) {
  3818. num_total++;
  3819. if (!_is_line_hidden(i)) {
  3820. num_visible++;
  3821. }
  3822. if (num_visible >= p_visible_amount) {
  3823. break;
  3824. }
  3825. }
  3826. }
  3827. return num_total;
  3828. }
  3829. Point2i TextEdit::get_next_visible_line_index_offset_from(int p_line_from, int p_wrap_index_from, int p_visible_amount) const {
  3830. // 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).
  3831. // Wrap index is set to the wrap index of the last line.
  3832. int wrap_index = 0;
  3833. ERR_FAIL_INDEX_V(p_line_from, text.size(), Point2i(Math::abs(p_visible_amount), 0));
  3834. if (!_is_hiding_enabled() && get_line_wrapping_mode() == LineWrappingMode::LINE_WRAPPING_NONE) {
  3835. return Point2i(Math::abs(p_visible_amount), 0);
  3836. }
  3837. int num_visible = 0;
  3838. int num_total = 0;
  3839. if (p_visible_amount == 0) {
  3840. num_total = 0;
  3841. wrap_index = 0;
  3842. } else if (p_visible_amount > 0) {
  3843. int i;
  3844. num_visible -= p_wrap_index_from;
  3845. for (i = p_line_from; i < text.size(); i++) {
  3846. num_total++;
  3847. if (!_is_line_hidden(i)) {
  3848. num_visible++;
  3849. num_visible += get_line_wrap_count(i);
  3850. }
  3851. if (num_visible >= p_visible_amount) {
  3852. break;
  3853. }
  3854. }
  3855. wrap_index = get_line_wrap_count(MIN(i, text.size() - 1)) - MAX(0, num_visible - p_visible_amount);
  3856. // If we are a hidden line, then we are the last line as we cannot reach "p_visible_amount".
  3857. // This means we need to backtrack to get last visible line.
  3858. // Currently, line 0 cannot be hidden so this should always be valid.
  3859. int line = (p_line_from + num_total) - 1;
  3860. if (_is_line_hidden(line)) {
  3861. Point2i backtrack = get_next_visible_line_index_offset_from(line, 0, -1);
  3862. num_total = num_total - (backtrack.x - 1);
  3863. wrap_index = backtrack.y;
  3864. }
  3865. } else {
  3866. p_visible_amount = Math::abs(p_visible_amount);
  3867. int i;
  3868. num_visible -= get_line_wrap_count(p_line_from) - p_wrap_index_from;
  3869. for (i = p_line_from; i >= 0; i--) {
  3870. num_total++;
  3871. if (!_is_line_hidden(i)) {
  3872. num_visible++;
  3873. num_visible += get_line_wrap_count(i);
  3874. }
  3875. if (num_visible >= p_visible_amount) {
  3876. break;
  3877. }
  3878. }
  3879. wrap_index = MAX(0, num_visible - p_visible_amount);
  3880. }
  3881. wrap_index = MAX(wrap_index, 0);
  3882. return Point2i(num_total, wrap_index);
  3883. }
  3884. void TextEdit::set_inline_object_handlers(const Callable &p_parser, const Callable &p_drawer, const Callable &p_click_handler) {
  3885. inline_object_drawer = p_drawer;
  3886. inline_object_click_handler = p_click_handler;
  3887. text.set_inline_object_parser(p_parser);
  3888. }
  3889. // Overridable actions
  3890. void TextEdit::handle_unicode_input(const uint32_t p_unicode, int p_caret) {
  3891. if (GDVIRTUAL_CALL(_handle_unicode_input, p_unicode, p_caret)) {
  3892. return;
  3893. }
  3894. _handle_unicode_input_internal(p_unicode, p_caret);
  3895. }
  3896. void TextEdit::backspace(int p_caret) {
  3897. if (GDVIRTUAL_CALL(_backspace, p_caret)) {
  3898. return;
  3899. }
  3900. _backspace_internal(p_caret);
  3901. }
  3902. void TextEdit::cut(int p_caret) {
  3903. if (GDVIRTUAL_CALL(_cut, p_caret)) {
  3904. return;
  3905. }
  3906. _cut_internal(p_caret);
  3907. }
  3908. void TextEdit::copy(int p_caret) {
  3909. if (GDVIRTUAL_CALL(_copy, p_caret)) {
  3910. return;
  3911. }
  3912. _copy_internal(p_caret);
  3913. }
  3914. void TextEdit::paste(int p_caret) {
  3915. if (GDVIRTUAL_CALL(_paste, p_caret)) {
  3916. return;
  3917. }
  3918. _paste_internal(p_caret);
  3919. }
  3920. void TextEdit::paste_primary_clipboard(int p_caret) {
  3921. if (GDVIRTUAL_CALL(_paste_primary_clipboard, p_caret)) {
  3922. return;
  3923. }
  3924. _paste_primary_clipboard_internal(p_caret);
  3925. }
  3926. // Context menu.
  3927. PopupMenu *TextEdit::get_menu() const {
  3928. if (!menu) {
  3929. const_cast<TextEdit *>(this)->_generate_context_menu();
  3930. }
  3931. return menu;
  3932. }
  3933. bool TextEdit::is_menu_visible() const {
  3934. return menu && menu->is_visible();
  3935. }
  3936. void TextEdit::menu_option(int p_option) {
  3937. switch (p_option) {
  3938. case MENU_CUT: {
  3939. cut();
  3940. } break;
  3941. case MENU_COPY: {
  3942. copy();
  3943. } break;
  3944. case MENU_PASTE: {
  3945. paste();
  3946. } break;
  3947. case MENU_CLEAR: {
  3948. if (editable) {
  3949. clear();
  3950. }
  3951. } break;
  3952. case MENU_SELECT_ALL: {
  3953. select_all();
  3954. } break;
  3955. case MENU_UNDO: {
  3956. undo();
  3957. } break;
  3958. case MENU_REDO: {
  3959. redo();
  3960. } break;
  3961. case MENU_DIR_INHERITED: {
  3962. set_text_direction(TEXT_DIRECTION_INHERITED);
  3963. } break;
  3964. case MENU_DIR_AUTO: {
  3965. set_text_direction(TEXT_DIRECTION_AUTO);
  3966. } break;
  3967. case MENU_DIR_LTR: {
  3968. set_text_direction(TEXT_DIRECTION_LTR);
  3969. } break;
  3970. case MENU_DIR_RTL: {
  3971. set_text_direction(TEXT_DIRECTION_RTL);
  3972. } break;
  3973. case MENU_DISPLAY_UCC: {
  3974. set_draw_control_chars(!get_draw_control_chars());
  3975. } break;
  3976. case MENU_INSERT_LRM: {
  3977. if (editable) {
  3978. insert_text_at_caret(String::chr(0x200E));
  3979. }
  3980. } break;
  3981. case MENU_INSERT_RLM: {
  3982. if (editable) {
  3983. insert_text_at_caret(String::chr(0x200F));
  3984. }
  3985. } break;
  3986. case MENU_INSERT_LRE: {
  3987. if (editable) {
  3988. insert_text_at_caret(String::chr(0x202A));
  3989. }
  3990. } break;
  3991. case MENU_INSERT_RLE: {
  3992. if (editable) {
  3993. insert_text_at_caret(String::chr(0x202B));
  3994. }
  3995. } break;
  3996. case MENU_INSERT_LRO: {
  3997. if (editable) {
  3998. insert_text_at_caret(String::chr(0x202D));
  3999. }
  4000. } break;
  4001. case MENU_INSERT_RLO: {
  4002. if (editable) {
  4003. insert_text_at_caret(String::chr(0x202E));
  4004. }
  4005. } break;
  4006. case MENU_INSERT_PDF: {
  4007. if (editable) {
  4008. insert_text_at_caret(String::chr(0x202C));
  4009. }
  4010. } break;
  4011. case MENU_INSERT_ALM: {
  4012. if (editable) {
  4013. insert_text_at_caret(String::chr(0x061C));
  4014. }
  4015. } break;
  4016. case MENU_INSERT_LRI: {
  4017. if (editable) {
  4018. insert_text_at_caret(String::chr(0x2066));
  4019. }
  4020. } break;
  4021. case MENU_INSERT_RLI: {
  4022. if (editable) {
  4023. insert_text_at_caret(String::chr(0x2067));
  4024. }
  4025. } break;
  4026. case MENU_INSERT_FSI: {
  4027. if (editable) {
  4028. insert_text_at_caret(String::chr(0x2068));
  4029. }
  4030. } break;
  4031. case MENU_INSERT_PDI: {
  4032. if (editable) {
  4033. insert_text_at_caret(String::chr(0x2069));
  4034. }
  4035. } break;
  4036. case MENU_INSERT_ZWJ: {
  4037. if (editable) {
  4038. insert_text_at_caret(String::chr(0x200D));
  4039. }
  4040. } break;
  4041. case MENU_INSERT_ZWNJ: {
  4042. if (editable) {
  4043. insert_text_at_caret(String::chr(0x200C));
  4044. }
  4045. } break;
  4046. case MENU_INSERT_WJ: {
  4047. if (editable) {
  4048. insert_text_at_caret(String::chr(0x2060));
  4049. }
  4050. } break;
  4051. case MENU_INSERT_SHY: {
  4052. if (editable) {
  4053. insert_text_at_caret(String::chr(0x00AD));
  4054. }
  4055. } break;
  4056. case MENU_EMOJI_AND_SYMBOL: {
  4057. show_emoji_and_symbol_picker();
  4058. } break;
  4059. }
  4060. }
  4061. /* Versioning */
  4062. void TextEdit::start_action(EditAction p_action) {
  4063. if (current_action != p_action) {
  4064. if (current_action != EditAction::ACTION_NONE) {
  4065. in_action = false;
  4066. pending_action_end = false;
  4067. end_complex_operation();
  4068. }
  4069. if (p_action != EditAction::ACTION_NONE) {
  4070. in_action = true;
  4071. begin_complex_operation();
  4072. }
  4073. } else if (current_action != EditAction::ACTION_NONE) {
  4074. pending_action_end = false;
  4075. }
  4076. current_action = p_action;
  4077. }
  4078. void TextEdit::end_action() {
  4079. if (current_action != EditAction::ACTION_NONE) {
  4080. pending_action_end = true;
  4081. queue_accessibility_update();
  4082. }
  4083. }
  4084. TextEdit::EditAction TextEdit::get_current_action() const {
  4085. return current_action;
  4086. }
  4087. void TextEdit::begin_complex_operation() {
  4088. _push_current_op();
  4089. if (complex_operation_count == 0) {
  4090. next_operation_is_complex = true;
  4091. current_op.start_carets = carets;
  4092. }
  4093. complex_operation_count++;
  4094. }
  4095. void TextEdit::end_complex_operation() {
  4096. _push_current_op();
  4097. queue_accessibility_update();
  4098. complex_operation_count = MAX(complex_operation_count - 1, 0);
  4099. if (complex_operation_count > 0) {
  4100. return;
  4101. }
  4102. if (undo_stack.is_empty()) {
  4103. return;
  4104. }
  4105. undo_stack.back()->get().end_carets = carets;
  4106. if (undo_stack.back()->get().chain_forward) {
  4107. undo_stack.back()->get().chain_forward = false;
  4108. return;
  4109. }
  4110. undo_stack.back()->get().chain_backward = true;
  4111. }
  4112. bool TextEdit::has_undo() const {
  4113. if (undo_stack_pos == nullptr) {
  4114. int pending = current_op.type == TextOperation::TYPE_NONE ? 0 : 1;
  4115. return undo_stack.size() + pending > 0;
  4116. }
  4117. return undo_stack_pos != undo_stack.front();
  4118. }
  4119. bool TextEdit::has_redo() const {
  4120. return undo_stack_pos != nullptr;
  4121. }
  4122. void TextEdit::undo() {
  4123. if (!editable) {
  4124. return;
  4125. }
  4126. if (in_action) {
  4127. pending_action_end = true;
  4128. }
  4129. _push_current_op();
  4130. if (undo_stack_pos == nullptr) {
  4131. if (undo_stack.is_empty()) {
  4132. return; // Nothing to undo.
  4133. }
  4134. undo_stack_pos = undo_stack.back();
  4135. } else if (undo_stack_pos == undo_stack.front()) {
  4136. return; // At the bottom of the undo stack.
  4137. } else {
  4138. undo_stack_pos = undo_stack_pos->prev();
  4139. }
  4140. deselect();
  4141. TextOperation op = undo_stack_pos->get();
  4142. _do_text_op(op, true);
  4143. current_op.version = op.prev_version;
  4144. if (undo_stack_pos->get().chain_backward) {
  4145. // This was part of a complex operation, undo until the chain forward at the start of the complex operation.
  4146. while (true) {
  4147. ERR_BREAK(!undo_stack_pos->prev());
  4148. undo_stack_pos = undo_stack_pos->prev();
  4149. op = undo_stack_pos->get();
  4150. _do_text_op(op, true);
  4151. current_op.version = op.prev_version;
  4152. if (undo_stack_pos->get().chain_forward) {
  4153. break;
  4154. }
  4155. }
  4156. }
  4157. _update_scrollbars();
  4158. bool dirty_carets = get_caret_count() != undo_stack_pos->get().start_carets.size();
  4159. if (!dirty_carets) {
  4160. for (int i = 0; i < get_caret_count(); i++) {
  4161. if (carets[i].line != undo_stack_pos->get().start_carets[i].line || carets[i].column != undo_stack_pos->get().start_carets[i].column) {
  4162. dirty_carets = true;
  4163. break;
  4164. }
  4165. }
  4166. }
  4167. carets = undo_stack_pos->get().start_carets;
  4168. _unhide_carets();
  4169. if (dirty_carets) {
  4170. _caret_changed();
  4171. _selection_changed();
  4172. }
  4173. adjust_viewport_to_caret();
  4174. queue_accessibility_update();
  4175. }
  4176. void TextEdit::redo() {
  4177. if (!editable) {
  4178. return;
  4179. }
  4180. if (in_action) {
  4181. pending_action_end = true;
  4182. }
  4183. _push_current_op();
  4184. if (!has_redo()) {
  4185. return; // Nothing to do.
  4186. }
  4187. deselect();
  4188. TextOperation op = undo_stack_pos->get();
  4189. _do_text_op(op, false);
  4190. current_op.version = op.version;
  4191. if (undo_stack_pos->get().chain_forward) {
  4192. // This was part of a complex operation, redo until the chain backward at the end of the complex operation.
  4193. while (true) {
  4194. ERR_BREAK(!undo_stack_pos->next());
  4195. undo_stack_pos = undo_stack_pos->next();
  4196. op = undo_stack_pos->get();
  4197. _do_text_op(op, false);
  4198. current_op.version = op.version;
  4199. if (undo_stack_pos->get().chain_backward) {
  4200. break;
  4201. }
  4202. }
  4203. }
  4204. _update_scrollbars();
  4205. bool dirty_carets = get_caret_count() != undo_stack_pos->get().end_carets.size();
  4206. if (!dirty_carets) {
  4207. for (int i = 0; i < get_caret_count(); i++) {
  4208. if (carets[i].line != undo_stack_pos->get().end_carets[i].line || carets[i].column != undo_stack_pos->get().end_carets[i].column) {
  4209. dirty_carets = true;
  4210. break;
  4211. }
  4212. }
  4213. }
  4214. carets = undo_stack_pos->get().end_carets;
  4215. undo_stack_pos = undo_stack_pos->next();
  4216. _unhide_carets();
  4217. if (dirty_carets) {
  4218. _caret_changed();
  4219. _selection_changed();
  4220. }
  4221. adjust_viewport_to_caret();
  4222. queue_accessibility_update();
  4223. }
  4224. void TextEdit::clear_undo_history() {
  4225. saved_version = 0;
  4226. current_op.type = TextOperation::TYPE_NONE;
  4227. undo_stack_pos = nullptr;
  4228. undo_stack.clear();
  4229. }
  4230. bool TextEdit::is_insert_text_operation() const {
  4231. return (current_op.type == TextOperation::TYPE_INSERT || current_action == EditAction::ACTION_TYPING);
  4232. }
  4233. void TextEdit::tag_saved_version() {
  4234. saved_version = get_version();
  4235. }
  4236. uint32_t TextEdit::get_version() const {
  4237. return current_op.version;
  4238. }
  4239. uint32_t TextEdit::get_saved_version() const {
  4240. return saved_version;
  4241. }
  4242. /* Search */
  4243. void TextEdit::set_search_text(const String &p_search_text) {
  4244. search_text = p_search_text;
  4245. }
  4246. void TextEdit::set_search_flags(uint32_t p_flags) {
  4247. search_flags = p_flags;
  4248. }
  4249. Point2i TextEdit::search(const String &p_key, uint32_t p_search_flags, int p_from_line, int p_from_column) const {
  4250. if (p_key.is_empty()) {
  4251. return Point2(-1, -1);
  4252. }
  4253. ERR_FAIL_INDEX_V(p_from_line, text.size(), Point2i(-1, -1));
  4254. ERR_FAIL_INDEX_V(p_from_column, text[p_from_line].length() + 1, Point2i(-1, -1));
  4255. const bool key_start_is_symbol = is_symbol(p_key[0]);
  4256. const bool key_end_is_symbol = is_symbol(p_key[p_key.length() - 1]);
  4257. // Search the whole document, starting from the current line.
  4258. // We'll auto-wrap through the start / end to search every line.
  4259. int current_line = p_from_line;
  4260. int current_column = p_from_column;
  4261. // + 1 because we'll search p_from_line twice - starting from p_from_column, and then again at the very end.
  4262. for (int i = 0; i < text.size() + 1; i++) {
  4263. const String &text_line = text[current_line];
  4264. // Search the current line as often as necessary.
  4265. while (true) {
  4266. if (p_search_flags & SEARCH_BACKWARDS) {
  4267. current_column = (p_search_flags & SEARCH_MATCH_CASE) ? text_line.rfind(p_key, current_column) : text_line.rfindn(p_key, current_column);
  4268. } else {
  4269. current_column = (p_search_flags & SEARCH_MATCH_CASE) ? text_line.find(p_key, current_column) : text_line.findn(p_key, current_column);
  4270. }
  4271. if (current_column == -1) {
  4272. break; // Nothing else found on the current line.
  4273. }
  4274. bool is_match = true;
  4275. if (p_search_flags & SEARCH_WHOLE_WORDS) {
  4276. // Validate for whole words.
  4277. if (!key_start_is_symbol && current_column > 0 && !is_symbol(text_line[current_column - 1])) {
  4278. is_match = false;
  4279. } else if (!key_end_is_symbol && current_column + p_key.length() < text_line.length() && !is_symbol(text_line[current_column + p_key.length()])) {
  4280. is_match = false;
  4281. }
  4282. }
  4283. if (is_match) {
  4284. // Found the string!
  4285. return Point2i(current_column, current_line);
  4286. }
  4287. // Advance past the current occurrence.
  4288. current_column += p_search_flags & SEARCH_BACKWARDS ? -1 : 1;
  4289. }
  4290. // Prepare for next iteration.
  4291. if (p_search_flags & SEARCH_BACKWARDS) {
  4292. current_column = -1;
  4293. current_line--;
  4294. if (current_line < 0) {
  4295. // Searched the whole document backwards; wrap to end.
  4296. current_line = text.size() - 1;
  4297. }
  4298. } else {
  4299. current_line++;
  4300. current_column = 0;
  4301. if (current_line == text.size()) {
  4302. // Searched the whole document forwards; wrap to start.
  4303. current_line = 0;
  4304. }
  4305. }
  4306. }
  4307. // Nothing found!
  4308. return Point2i(-1, -1);
  4309. }
  4310. /* Mouse */
  4311. Point2 TextEdit::get_local_mouse_pos() const {
  4312. Point2 mp = get_local_mouse_position();
  4313. if (is_layout_rtl()) {
  4314. mp.x = get_size().width - mp.x;
  4315. }
  4316. return mp;
  4317. }
  4318. String TextEdit::get_word_at_pos(const Vector2 &p_pos) const {
  4319. Point2i pos = get_line_column_at_pos(p_pos, false, false);
  4320. int line = pos.y;
  4321. int col = pos.x;
  4322. return get_word(line, col);
  4323. }
  4324. String TextEdit::get_word(int p_line, int p_column) const {
  4325. if (p_line < 0 || p_column < 0) {
  4326. return String();
  4327. }
  4328. ERR_FAIL_INDEX_V(p_line, text.size(), String());
  4329. const String &text_line = text[p_line];
  4330. if (text_line.is_empty()) {
  4331. return String();
  4332. }
  4333. ERR_FAIL_INDEX_V(p_column, text_line.size() + 1, String());
  4334. const PackedInt32Array words = TS->shaped_text_get_word_breaks(text.get_line_data(p_line)->get_rid());
  4335. for (int i = 0; i < words.size(); i = i + 2) {
  4336. if (words[i] <= p_column && words[i + 1] >= p_column) {
  4337. return text_line.substr(words[i], words[i + 1] - words[i]);
  4338. }
  4339. }
  4340. return String();
  4341. }
  4342. Point2i TextEdit::get_line_column_at_pos(const Point2i &p_pos, bool p_clamp_line, bool p_clamp_column) const {
  4343. float rows = p_pos.y - theme_cache.style_normal->get_margin(SIDE_TOP);
  4344. if (!editable) {
  4345. rows -= theme_cache.style_readonly->get_offset().y / 2;
  4346. rows += theme_cache.style_normal->get_offset().y / 2;
  4347. }
  4348. rows /= get_line_height();
  4349. rows += _get_v_scroll_offset();
  4350. int first_vis_line = get_first_visible_line();
  4351. int row = first_vis_line + Math::floor(rows);
  4352. int wrap_index = 0;
  4353. if (get_line_wrapping_mode() != LineWrappingMode::LINE_WRAPPING_NONE || _is_hiding_enabled()) {
  4354. Point2i f_ofs = get_next_visible_line_index_offset_from(first_vis_line, first_visible_line_wrap_ofs, rows + (1 * SIGN(rows)));
  4355. wrap_index = f_ofs.y;
  4356. if (rows < 0) {
  4357. row = first_vis_line - (f_ofs.x - 1);
  4358. } else {
  4359. row = first_vis_line + (f_ofs.x - 1);
  4360. }
  4361. }
  4362. row = CLAMP(row, 0, text.size() - 1);
  4363. int visible_lines = get_visible_line_count_in_range(first_vis_line, row);
  4364. if (rows > visible_lines) {
  4365. if (p_clamp_line) {
  4366. return Point2i(text[row].length(), row);
  4367. }
  4368. return Point2i(-1, -1);
  4369. }
  4370. int colx = p_pos.x - (Math::ceil(theme_cache.style_normal->get_margin(SIDE_LEFT)) + gutters_width + gutter_padding);
  4371. colx += first_visible_col;
  4372. if (!editable) {
  4373. colx -= theme_cache.style_readonly->get_offset().x / 2;
  4374. colx += theme_cache.style_normal->get_offset().x / 2;
  4375. }
  4376. RID text_rid = text.get_line_data(row)->get_line_rid(wrap_index);
  4377. bool rtl = is_layout_rtl();
  4378. const float wrap_indent = _get_wrap_indent_offset(row, wrap_index, rtl);
  4379. if (rtl) {
  4380. colx = TS->shaped_text_get_size(text_rid).x - colx + wrap_indent;
  4381. } else {
  4382. colx -= wrap_indent;
  4383. }
  4384. if (!p_clamp_column && (colx < 0 || colx > TS->shaped_text_get_size(text_rid).x)) {
  4385. return Point2i(-1, -1);
  4386. }
  4387. int col = TS->shaped_text_hit_test_position(text_rid, colx);
  4388. if (!caret_mid_grapheme_enabled) {
  4389. col = TS->shaped_text_closest_character_pos(text_rid, col);
  4390. }
  4391. return Point2i(col, row);
  4392. }
  4393. Point2i TextEdit::get_pos_at_line_column(int p_line, int p_column) const {
  4394. Rect2i rect = get_rect_at_line_column(p_line, p_column);
  4395. return rect.position.x == -1 ? rect.position : rect.position + Vector2i(0, get_line_height());
  4396. }
  4397. Rect2i TextEdit::get_rect_at_line_column(int p_line, int p_column) const {
  4398. ERR_FAIL_INDEX_V(p_line, text.size(), Rect2i(-1, -1, 0, 0));
  4399. ERR_FAIL_COND_V(p_column < 0, Rect2i(-1, -1, 0, 0));
  4400. ERR_FAIL_COND_V(p_column > text[p_line].length(), Rect2i(-1, -1, 0, 0));
  4401. if (text.size() == 1 && text[0].is_empty()) {
  4402. // The TextEdit is empty.
  4403. return Rect2i();
  4404. }
  4405. if (line_drawing_cache.is_empty() || !line_drawing_cache.has(p_line)) {
  4406. // Line is not in the cache, which means it's outside of the viewing area.
  4407. return Rect2i(-1, -1, 0, 0);
  4408. }
  4409. LineDrawingCache cache_entry = line_drawing_cache[p_line];
  4410. int wrap_index = get_line_wrap_index_at_column(p_line, p_column);
  4411. if (wrap_index >= cache_entry.first_visible_chars.size()) {
  4412. // Line seems to be wrapped beyond the viewable area.
  4413. return Rect2i(-1, -1, 0, 0);
  4414. }
  4415. int first_visible_char = cache_entry.first_visible_chars[wrap_index];
  4416. int last_visible_char = cache_entry.last_visible_chars[wrap_index];
  4417. if (p_column < first_visible_char || p_column > last_visible_char) {
  4418. // Character is outside of the viewing area, no point calculating its position.
  4419. return Rect2i(-1, -1, 0, 0);
  4420. }
  4421. const float wrap_indent = _get_wrap_indent_offset(p_line, wrap_index, is_layout_rtl());
  4422. Point2i pos, size;
  4423. pos.y = cache_entry.y_offset + get_line_height() * wrap_index;
  4424. pos.x = get_total_gutter_width() + Math::ceil(theme_cache.style_normal->get_margin(SIDE_LEFT)) + wrap_indent - get_h_scroll();
  4425. RID text_rid = text.get_line_data(p_line)->get_line_rid(wrap_index);
  4426. Vector2 col_bounds = TS->shaped_text_get_grapheme_bounds(text_rid, p_column);
  4427. pos.x += col_bounds.x;
  4428. size.x = col_bounds.y - col_bounds.x;
  4429. size.y = get_line_height();
  4430. return Rect2i(pos, size);
  4431. }
  4432. int TextEdit::get_minimap_line_at_pos(const Point2i &p_pos) const {
  4433. float rows = p_pos.y;
  4434. rows -= theme_cache.style_normal->get_margin(SIDE_TOP);
  4435. rows /= (minimap_char_size.y + minimap_line_spacing);
  4436. rows += _get_v_scroll_offset();
  4437. // Calculate visible lines.
  4438. int minimap_visible_lines = get_minimap_visible_lines();
  4439. int visible_rows = get_visible_line_count() + 1;
  4440. int first_vis_line = get_first_visible_line() - 1;
  4441. int draw_amount = visible_rows + 1;
  4442. draw_amount += get_line_wrap_count(first_vis_line + 1);
  4443. int minimap_line_height = (minimap_char_size.y + minimap_line_spacing);
  4444. // Calculate viewport size and y offset.
  4445. int viewport_height = (draw_amount - 1) * minimap_line_height;
  4446. int control_height = _get_control_height() - viewport_height;
  4447. int viewport_offset_y = std::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));
  4448. // Calculate the first line.
  4449. int num_lines_before = std::round((viewport_offset_y) / minimap_line_height);
  4450. int minimap_line = (v_scroll->get_max() <= minimap_visible_lines) ? -1 : first_vis_line;
  4451. if (first_vis_line > 0 && minimap_line >= 0) {
  4452. minimap_line -= get_next_visible_line_index_offset_from(first_vis_line, 0, -num_lines_before).x;
  4453. minimap_line -= (minimap_line > 0 && smooth_scroll_enabled ? 1 : 0);
  4454. }
  4455. if (minimap_line < 0) {
  4456. minimap_line = 0;
  4457. }
  4458. int row = minimap_line + Math::floor(rows);
  4459. if (get_line_wrapping_mode() != LineWrappingMode::LINE_WRAPPING_NONE || _is_hiding_enabled()) {
  4460. int f_ofs = get_next_visible_line_index_offset_from(minimap_line, first_visible_line_wrap_ofs, rows + (1 * SIGN(rows))).x - 1;
  4461. if (rows < 0) {
  4462. row = minimap_line - f_ofs;
  4463. } else {
  4464. row = minimap_line + f_ofs;
  4465. }
  4466. }
  4467. row = CLAMP(row, 0, text.size() - 1);
  4468. return row;
  4469. }
  4470. bool TextEdit::is_dragging_cursor() const {
  4471. return dragging_selection || dragging_minimap;
  4472. }
  4473. bool TextEdit::is_mouse_over_selection(bool p_edges, int p_caret) const {
  4474. Point2i pos = get_line_column_at_pos(get_local_mouse_pos());
  4475. int line = pos.y;
  4476. int column = pos.x;
  4477. if ((p_caret == -1 && get_selection_at_line_column(line, column, p_edges) != -1) || (p_caret != -1 && _selection_contains(p_caret, line, column, p_edges))) {
  4478. return true;
  4479. }
  4480. return false;
  4481. }
  4482. /* Caret */
  4483. void TextEdit::set_caret_type(CaretType p_type) {
  4484. if (caret_type == p_type) {
  4485. return;
  4486. }
  4487. caret_type = p_type;
  4488. queue_redraw();
  4489. }
  4490. TextEdit::CaretType TextEdit::get_caret_type() const {
  4491. return caret_type;
  4492. }
  4493. void TextEdit::set_caret_blink_enabled(bool p_enabled) {
  4494. if (caret_blink_enabled == p_enabled) {
  4495. return;
  4496. }
  4497. caret_blink_enabled = p_enabled;
  4498. if (has_focus()) {
  4499. if (p_enabled) {
  4500. caret_blink_timer->start();
  4501. } else {
  4502. caret_blink_timer->stop();
  4503. }
  4504. }
  4505. draw_caret = true;
  4506. }
  4507. bool TextEdit::is_caret_blink_enabled() const {
  4508. return caret_blink_enabled;
  4509. }
  4510. float TextEdit::get_caret_blink_interval() const {
  4511. return caret_blink_timer->get_wait_time();
  4512. }
  4513. void TextEdit::set_caret_blink_interval(const float p_interval) {
  4514. ERR_FAIL_COND(p_interval <= 0);
  4515. caret_blink_timer->set_wait_time(p_interval);
  4516. }
  4517. void TextEdit::set_draw_caret_when_editable_disabled(bool p_enable) {
  4518. if (draw_caret_when_editable_disabled == p_enable) {
  4519. return;
  4520. }
  4521. draw_caret_when_editable_disabled = p_enable;
  4522. queue_redraw();
  4523. }
  4524. bool TextEdit::is_drawing_caret_when_editable_disabled() const {
  4525. return draw_caret_when_editable_disabled;
  4526. }
  4527. void TextEdit::set_move_caret_on_right_click_enabled(bool p_enabled) {
  4528. move_caret_on_right_click = p_enabled;
  4529. }
  4530. bool TextEdit::is_move_caret_on_right_click_enabled() const {
  4531. return move_caret_on_right_click;
  4532. }
  4533. void TextEdit::set_caret_mid_grapheme_enabled(bool p_enabled) {
  4534. caret_mid_grapheme_enabled = p_enabled;
  4535. }
  4536. bool TextEdit::is_caret_mid_grapheme_enabled() const {
  4537. return caret_mid_grapheme_enabled;
  4538. }
  4539. void TextEdit::set_multiple_carets_enabled(bool p_enabled) {
  4540. multi_carets_enabled = p_enabled;
  4541. if (!multi_carets_enabled) {
  4542. remove_secondary_carets();
  4543. multicaret_edit_count = 0;
  4544. multicaret_edit_ignore_carets.clear();
  4545. multicaret_edit_merge_queued = false;
  4546. }
  4547. }
  4548. bool TextEdit::is_multiple_carets_enabled() const {
  4549. return multi_carets_enabled;
  4550. }
  4551. int TextEdit::add_caret(int p_line, int p_column) {
  4552. if (!multi_carets_enabled) {
  4553. return -1;
  4554. }
  4555. _cancel_drag_and_drop_text();
  4556. p_line = CLAMP(p_line, 0, text.size() - 1);
  4557. p_column = CLAMP(p_column, 0, get_line(p_line).length());
  4558. if (!is_in_mulitcaret_edit()) {
  4559. // Carets cannot overlap.
  4560. if (get_selection_at_line_column(p_line, p_column, true, false) != -1) {
  4561. return -1;
  4562. }
  4563. }
  4564. carets.push_back(Caret());
  4565. int new_index = carets.size() - 1;
  4566. set_caret_line(p_line, false, false, -1, new_index);
  4567. set_caret_column(p_column, false, new_index);
  4568. _caret_changed(new_index);
  4569. if (is_in_mulitcaret_edit()) {
  4570. multicaret_edit_ignore_carets.insert(new_index);
  4571. merge_overlapping_carets();
  4572. }
  4573. return new_index;
  4574. }
  4575. void TextEdit::remove_caret(int p_caret) {
  4576. ERR_FAIL_COND_MSG(carets.size() <= 1, "The main caret should not be removed.");
  4577. ERR_FAIL_INDEX(p_caret, carets.size());
  4578. _caret_changed(p_caret);
  4579. carets.remove_at(p_caret);
  4580. if (drag_caret_index >= 0) {
  4581. if (p_caret == drag_caret_index) {
  4582. drag_caret_index = -1;
  4583. } else if (p_caret < drag_caret_index) {
  4584. drag_caret_index -= 1;
  4585. }
  4586. }
  4587. }
  4588. void TextEdit::remove_drag_caret() {
  4589. if (drag_caret_index >= 0) {
  4590. if (drag_caret_index < carets.size()) {
  4591. remove_caret(drag_caret_index);
  4592. }
  4593. drag_caret_index = -1;
  4594. }
  4595. }
  4596. void TextEdit::remove_secondary_carets() {
  4597. if (carets.size() == 1) {
  4598. return;
  4599. }
  4600. _caret_changed();
  4601. carets.resize(1);
  4602. if (drag_caret_index >= 0) {
  4603. drag_caret_index = -1;
  4604. }
  4605. queue_accessibility_update();
  4606. }
  4607. int TextEdit::get_caret_count() const {
  4608. // Don't include drag caret.
  4609. if (drag_caret_index >= 0) {
  4610. return carets.size() - 1;
  4611. }
  4612. return carets.size();
  4613. }
  4614. void TextEdit::add_caret_at_carets(bool p_below) {
  4615. if (!multi_carets_enabled) {
  4616. return;
  4617. }
  4618. const int last_line_max_wrap = get_line_wrap_count(text.size() - 1);
  4619. set_selection_mode(SELECTION_MODE_NONE);
  4620. begin_multicaret_edit();
  4621. int view_target_caret = -1;
  4622. int view_line = p_below ? -1 : INT_MAX;
  4623. int num_carets = get_caret_count();
  4624. for (int i = 0; i < num_carets; i++) {
  4625. const int caret_line = get_caret_line(i);
  4626. const int caret_column = get_caret_column(i);
  4627. bool is_selected = has_selection(i) || carets[i].last_fit_x != carets[i].selection.origin_last_fit_x;
  4628. const int selection_origin_line = get_selection_origin_line(i);
  4629. const int selection_origin_column = get_selection_origin_column(i);
  4630. const int caret_wrap_index = get_caret_wrap_index(i);
  4631. const int selection_origin_wrap_index = !is_selected ? -1 : get_line_wrap_index_at_column(selection_origin_line, selection_origin_column);
  4632. if (caret_line == 0 && !p_below && (caret_wrap_index == 0 || selection_origin_wrap_index == 0)) {
  4633. // Can't add above the first line.
  4634. continue;
  4635. }
  4636. if (caret_line == text.size() - 1 && p_below && (caret_wrap_index == last_line_max_wrap || selection_origin_wrap_index == last_line_max_wrap)) {
  4637. // Can't add below the last line.
  4638. continue;
  4639. }
  4640. // Add a new caret.
  4641. int new_caret_index = add_caret(caret_line, caret_column);
  4642. ERR_FAIL_COND_MSG(new_caret_index < 0, "Failed to add a caret.");
  4643. // Copy the selection origin and last fit.
  4644. set_selection_origin_line(selection_origin_line, true, -1, new_caret_index);
  4645. set_selection_origin_column(selection_origin_column, new_caret_index);
  4646. carets.write[new_caret_index].last_fit_x = carets[i].last_fit_x;
  4647. carets.write[new_caret_index].selection.origin_last_fit_x = carets[i].selection.origin_last_fit_x;
  4648. // Move the caret up or down one visible line.
  4649. if (!p_below) {
  4650. // Move caret up.
  4651. if (caret_wrap_index > 0) {
  4652. set_caret_line(caret_line, false, false, caret_wrap_index - 1, new_caret_index);
  4653. } else {
  4654. int new_line = caret_line - get_next_visible_line_offset_from(caret_line - 1, -1);
  4655. if (is_line_wrapped(new_line)) {
  4656. set_caret_line(new_line, false, false, get_line_wrap_count(new_line), new_caret_index);
  4657. } else {
  4658. set_caret_line(new_line, false, false, 0, new_caret_index);
  4659. }
  4660. }
  4661. // Move selection origin up.
  4662. if (is_selected) {
  4663. if (selection_origin_wrap_index > 0) {
  4664. set_selection_origin_line(caret_line, false, selection_origin_wrap_index - 1, new_caret_index);
  4665. } else {
  4666. int new_line = selection_origin_line - get_next_visible_line_offset_from(selection_origin_line - 1, -1);
  4667. if (is_line_wrapped(new_line)) {
  4668. set_selection_origin_line(new_line, false, get_line_wrap_count(new_line), new_caret_index);
  4669. } else {
  4670. set_selection_origin_line(new_line, false, 0, new_caret_index);
  4671. }
  4672. }
  4673. }
  4674. if (get_caret_line(new_caret_index) < view_line) {
  4675. view_line = get_caret_line(new_caret_index);
  4676. view_target_caret = new_caret_index;
  4677. }
  4678. } else {
  4679. // Move caret down.
  4680. if (caret_wrap_index < get_line_wrap_count(caret_line)) {
  4681. set_caret_line(caret_line, false, false, caret_wrap_index + 1, new_caret_index);
  4682. } else {
  4683. int new_line = caret_line + get_next_visible_line_offset_from(CLAMP(caret_line + 1, 0, text.size() - 1), 1);
  4684. set_caret_line(new_line, false, false, 0, new_caret_index);
  4685. }
  4686. // Move selection origin down.
  4687. if (is_selected) {
  4688. if (selection_origin_wrap_index < get_line_wrap_count(selection_origin_line)) {
  4689. set_selection_origin_line(selection_origin_line, false, selection_origin_wrap_index + 1, new_caret_index);
  4690. } else {
  4691. int new_line = selection_origin_line + get_next_visible_line_offset_from(CLAMP(selection_origin_line + 1, 0, text.size() - 1), 1);
  4692. set_selection_origin_line(new_line, false, 0, new_caret_index);
  4693. }
  4694. }
  4695. if (get_caret_line(new_caret_index) > view_line) {
  4696. view_line = get_caret_line(new_caret_index);
  4697. view_target_caret = new_caret_index;
  4698. }
  4699. }
  4700. if (is_selected) {
  4701. // Make sure selection is active.
  4702. select(get_selection_origin_line(new_caret_index), get_selection_origin_column(new_caret_index), get_caret_line(new_caret_index), get_caret_column(new_caret_index), new_caret_index);
  4703. carets.write[new_caret_index].last_fit_x = carets[i].last_fit_x;
  4704. carets.write[new_caret_index].selection.origin_last_fit_x = carets[i].selection.origin_last_fit_x;
  4705. }
  4706. bool check_edges = !has_selection(0) || !has_selection(new_caret_index);
  4707. bool will_merge_with_main_caret = _selection_contains(0, get_caret_line(new_caret_index), get_caret_column(new_caret_index), check_edges, false) || _selection_contains(new_caret_index, get_caret_line(0), get_caret_column(0), check_edges, false);
  4708. if (will_merge_with_main_caret) {
  4709. // Move next to the main caret so it stays the main caret after merging.
  4710. Caret new_caret = carets[new_caret_index];
  4711. carets.remove_at(new_caret_index);
  4712. carets.insert(0, new_caret);
  4713. i++;
  4714. }
  4715. }
  4716. // Show the topmost caret if added above or bottommost caret if added below.
  4717. if (view_target_caret >= 0 && view_target_caret < get_caret_count()) {
  4718. adjust_viewport_to_caret(view_target_caret);
  4719. }
  4720. merge_overlapping_carets();
  4721. end_multicaret_edit();
  4722. }
  4723. struct _CaretSortComparator {
  4724. _FORCE_INLINE_ bool operator()(const Vector3i &a, const Vector3i &b) const {
  4725. // x is column, y is line, z is caret index.
  4726. if (a.y == b.y) {
  4727. return a.x < b.x;
  4728. }
  4729. return a.y < b.y;
  4730. }
  4731. };
  4732. Vector<int> TextEdit::get_sorted_carets(bool p_include_ignored_carets) const {
  4733. // Returns caret indexes sorted by selection start or caret position from top to bottom of text.
  4734. Vector<Vector3i> caret_line_col_indexes;
  4735. for (int i = 0; i < get_caret_count(); i++) {
  4736. if (!p_include_ignored_carets && multicaret_edit_ignore_caret(i)) {
  4737. continue;
  4738. }
  4739. caret_line_col_indexes.push_back(Vector3i(get_selection_from_column(i), get_selection_from_line(i), i));
  4740. }
  4741. caret_line_col_indexes.sort_custom<_CaretSortComparator>();
  4742. Vector<int> sorted;
  4743. sorted.resize(caret_line_col_indexes.size());
  4744. for (int i = 0; i < caret_line_col_indexes.size(); i++) {
  4745. sorted.set(i, caret_line_col_indexes[i].z);
  4746. }
  4747. return sorted;
  4748. }
  4749. void TextEdit::collapse_carets(int p_from_line, int p_from_column, int p_to_line, int p_to_column, bool p_inclusive) {
  4750. // Collapse carets in the selected range to the from position.
  4751. // Clamp the collapse target position.
  4752. int collapse_line = CLAMP(p_from_line, 0, text.size() - 1);
  4753. int collapse_column = CLAMP(p_from_column, 0, text[collapse_line].length());
  4754. // Swap the lines if they are in the wrong order.
  4755. if (p_from_line > p_to_line) {
  4756. SWAP(p_from_line, p_to_line);
  4757. SWAP(p_from_column, p_to_column);
  4758. }
  4759. if (p_from_line == p_to_line && p_from_column > p_to_column) {
  4760. SWAP(p_from_column, p_to_column);
  4761. }
  4762. bool any_collapsed = false;
  4763. // Intentionally includes carets in the multicaret_edit_ignore list so that they are moved together.
  4764. for (int i = 0; i < get_caret_count(); i++) {
  4765. bool is_caret_in = _is_line_col_in_range(get_caret_line(i), get_caret_column(i), p_from_line, p_from_column, p_to_line, p_to_column, p_inclusive);
  4766. if (!has_selection(i)) {
  4767. if (is_caret_in) {
  4768. // Caret was in the collapsed area.
  4769. set_caret_line(collapse_line, false, true, -1, i);
  4770. set_caret_column(collapse_column, false, i);
  4771. if (is_in_mulitcaret_edit() && get_caret_count() > 1) {
  4772. multicaret_edit_ignore_carets.insert(i);
  4773. }
  4774. any_collapsed = true;
  4775. }
  4776. } else {
  4777. bool is_origin_in = _is_line_col_in_range(get_selection_origin_line(i), get_selection_origin_column(i), p_from_line, p_from_column, p_to_line, p_to_column, p_inclusive);
  4778. if (is_caret_in && is_origin_in) {
  4779. // Selection was completely encapsulated.
  4780. deselect(i);
  4781. set_caret_line(collapse_line, false, true, -1, i);
  4782. set_caret_column(collapse_column, false, i);
  4783. if (is_in_mulitcaret_edit() && get_caret_count() > 1) {
  4784. multicaret_edit_ignore_carets.insert(i);
  4785. }
  4786. any_collapsed = true;
  4787. } else if (is_caret_in) {
  4788. // Only caret was inside.
  4789. set_caret_line(collapse_line, false, true, -1, i);
  4790. set_caret_column(collapse_column, false, i);
  4791. any_collapsed = true;
  4792. } else if (is_origin_in) {
  4793. // Only selection origin was inside.
  4794. set_selection_origin_line(collapse_line, true, -1, i);
  4795. set_selection_origin_column(collapse_column, i);
  4796. any_collapsed = true;
  4797. }
  4798. }
  4799. if (!p_inclusive && !any_collapsed) {
  4800. if ((get_caret_line(i) == collapse_line && get_caret_column(i) == collapse_column) || (get_selection_origin_line(i) == collapse_line && get_selection_origin_column(i) == collapse_column)) {
  4801. // Make sure to queue a merge, even if we didn't include it.
  4802. any_collapsed = true;
  4803. }
  4804. }
  4805. }
  4806. if (any_collapsed) {
  4807. merge_overlapping_carets();
  4808. }
  4809. }
  4810. void TextEdit::merge_overlapping_carets() {
  4811. if (is_in_mulitcaret_edit()) {
  4812. // Queue merge to be performed the end of the multicaret edit.
  4813. multicaret_edit_merge_queued = true;
  4814. return;
  4815. }
  4816. multicaret_edit_merge_queued = false;
  4817. multicaret_edit_ignore_carets.clear();
  4818. if (get_caret_count() == 1) {
  4819. return;
  4820. }
  4821. Vector<int> sorted_carets = get_sorted_carets(true);
  4822. for (int i = 0; i < sorted_carets.size() - 1; i++) {
  4823. int first_caret = sorted_carets[i];
  4824. int second_caret = sorted_carets[i + 1];
  4825. bool merge_carets;
  4826. if (!has_selection(first_caret) || !has_selection(second_caret)) {
  4827. // Merge if touching.
  4828. merge_carets = get_selection_from_line(second_caret) < get_selection_to_line(first_caret) || (get_selection_from_line(second_caret) == get_selection_to_line(first_caret) && get_selection_from_column(second_caret) <= get_selection_to_column(first_caret));
  4829. } else {
  4830. // Merge two selections if overlapping.
  4831. merge_carets = get_selection_from_line(second_caret) < get_selection_to_line(first_caret) || (get_selection_from_line(second_caret) == get_selection_to_line(first_caret) && get_selection_from_column(second_caret) < get_selection_to_column(first_caret));
  4832. }
  4833. if (!merge_carets) {
  4834. continue;
  4835. }
  4836. // Save the newest one for Click + Drag.
  4837. int caret_to_save = first_caret;
  4838. int caret_to_remove = second_caret;
  4839. if (first_caret < second_caret) {
  4840. caret_to_save = second_caret;
  4841. caret_to_remove = first_caret;
  4842. }
  4843. if (get_selection_from_line(caret_to_save) != get_selection_from_line(caret_to_remove) || get_selection_to_line(caret_to_save) != get_selection_to_line(caret_to_remove) || get_selection_from_column(caret_to_save) != get_selection_from_column(caret_to_remove) || get_selection_to_column(caret_to_save) != get_selection_to_column(caret_to_remove)) {
  4844. // Selections are not the same, merge them into one bigger selection.
  4845. int new_from_line = MIN(get_selection_from_line(caret_to_remove), get_selection_from_line(caret_to_save));
  4846. int new_to_line = MAX(get_selection_to_line(caret_to_remove), get_selection_to_line(caret_to_save));
  4847. int new_from_col;
  4848. int new_to_col;
  4849. if (get_selection_from_line(caret_to_remove) < get_selection_from_line(caret_to_save)) {
  4850. new_from_col = get_selection_from_column(caret_to_remove);
  4851. } else if (get_selection_from_line(caret_to_remove) > get_selection_from_line(caret_to_save)) {
  4852. new_from_col = get_selection_from_column(caret_to_save);
  4853. } else {
  4854. new_from_col = MIN(get_selection_from_column(caret_to_remove), get_selection_from_column(caret_to_save));
  4855. }
  4856. if (get_selection_to_line(caret_to_remove) < get_selection_to_line(caret_to_save)) {
  4857. new_to_col = get_selection_to_column(caret_to_save);
  4858. } else if (get_selection_to_line(caret_to_remove) > get_selection_to_line(caret_to_save)) {
  4859. new_to_col = get_selection_to_column(caret_to_remove);
  4860. } else {
  4861. new_to_col = MAX(get_selection_to_column(caret_to_remove), get_selection_to_column(caret_to_save));
  4862. }
  4863. // Use the direction from the last caret or the saved one.
  4864. int caret_dir_to_copy;
  4865. if (has_selection(caret_to_remove) && has_selection(caret_to_save)) {
  4866. caret_dir_to_copy = caret_to_remove == get_caret_count() - 1 ? caret_to_remove : caret_to_save;
  4867. } else {
  4868. caret_dir_to_copy = !has_selection(caret_to_remove) ? caret_to_save : caret_to_remove;
  4869. }
  4870. if (is_caret_after_selection_origin(caret_dir_to_copy)) {
  4871. select(new_from_line, new_from_col, new_to_line, new_to_col, caret_to_save);
  4872. } else {
  4873. select(new_to_line, new_to_col, new_from_line, new_from_col, caret_to_save);
  4874. }
  4875. }
  4876. if (caret_to_save == 0) {
  4877. adjust_viewport_to_caret(caret_to_save);
  4878. }
  4879. remove_caret(caret_to_remove);
  4880. // Update the rest of the sorted list.
  4881. for (int j = i; j < sorted_carets.size(); j++) {
  4882. if (sorted_carets[j] > caret_to_remove) {
  4883. // Shift the index since a caret before it was removed.
  4884. sorted_carets.write[j] -= 1;
  4885. }
  4886. }
  4887. // Remove the caret from the sorted array.
  4888. sorted_carets.remove_at(caret_to_remove == first_caret ? i : i + 1);
  4889. // Process the caret again, since it and the next caret might also overlap.
  4890. i--;
  4891. }
  4892. }
  4893. // Starts a multicaret edit operation. Call this before iterating over the carets and call [end_multicaret_edit] afterwards.
  4894. void TextEdit::begin_multicaret_edit() {
  4895. if (!multi_carets_enabled) {
  4896. return;
  4897. }
  4898. multicaret_edit_count++;
  4899. }
  4900. void TextEdit::end_multicaret_edit() {
  4901. if (!multi_carets_enabled) {
  4902. return;
  4903. }
  4904. if (multicaret_edit_count > 0) {
  4905. multicaret_edit_count--;
  4906. }
  4907. if (multicaret_edit_count != 0) {
  4908. return;
  4909. }
  4910. // This was the last multicaret edit operation.
  4911. if (multicaret_edit_merge_queued) {
  4912. merge_overlapping_carets();
  4913. }
  4914. multicaret_edit_ignore_carets.clear();
  4915. }
  4916. bool TextEdit::is_in_mulitcaret_edit() const {
  4917. return multicaret_edit_count > 0;
  4918. }
  4919. bool TextEdit::multicaret_edit_ignore_caret(int p_caret) const {
  4920. return multicaret_edit_ignore_carets.has(p_caret);
  4921. }
  4922. bool TextEdit::is_caret_visible(int p_caret) const {
  4923. ERR_FAIL_INDEX_V(p_caret, carets.size(), false);
  4924. return carets[p_caret].visible;
  4925. }
  4926. Point2 TextEdit::get_caret_draw_pos(int p_caret) const {
  4927. ERR_FAIL_INDEX_V(p_caret, carets.size(), Point2(0, 0));
  4928. return carets[p_caret].draw_pos;
  4929. }
  4930. void TextEdit::set_caret_line(int p_line, bool p_adjust_viewport, bool p_can_be_hidden, int p_wrap_index, int p_caret) {
  4931. ERR_FAIL_INDEX(p_caret, carets.size());
  4932. if (setting_caret_line) {
  4933. return;
  4934. }
  4935. setting_caret_line = true;
  4936. p_line = CLAMP(p_line, 0, text.size() - 1);
  4937. if (!p_can_be_hidden) {
  4938. if (_is_line_hidden(p_line)) {
  4939. int move_down = get_next_visible_line_offset_from(p_line, 1) - 1;
  4940. if (p_line + move_down <= text.size() - 1 && !_is_line_hidden(p_line + move_down)) {
  4941. p_line += move_down;
  4942. } else {
  4943. int move_up = get_next_visible_line_offset_from(p_line, -1) - 1;
  4944. if (p_line - move_up > 0 && !_is_line_hidden(p_line - move_up)) {
  4945. p_line -= move_up;
  4946. } else {
  4947. WARN_PRINT("Caret set to hidden line " + itos(p_line) + " and there are no nonhidden lines.");
  4948. }
  4949. }
  4950. }
  4951. }
  4952. bool caret_moved = get_caret_line(p_caret) != p_line;
  4953. carets.write[p_caret].line = p_line;
  4954. int n_col;
  4955. if (p_wrap_index >= 0) {
  4956. // Keep caret in same visual x position it was at previously.
  4957. n_col = _get_char_pos_for_line(carets[p_caret].last_fit_x, p_line, p_wrap_index);
  4958. if (n_col != 0 && get_line_wrapping_mode() != LineWrappingMode::LINE_WRAPPING_NONE && p_wrap_index < get_line_wrap_count(p_line)) {
  4959. // Offset by one to not go past the end of the wrapped line.
  4960. if (n_col >= text.get_line_wrap_ranges(p_line)[p_wrap_index].y) {
  4961. n_col -= 1;
  4962. }
  4963. }
  4964. } else {
  4965. // Clamp the column.
  4966. n_col = MIN(get_caret_column(p_caret), get_line(p_line).length());
  4967. }
  4968. caret_moved = (caret_moved || get_caret_column(p_caret) != n_col);
  4969. carets.write[p_caret].column = n_col;
  4970. // Unselect if the caret moved to the selection origin.
  4971. if (p_wrap_index >= 0 && has_selection(p_caret) && get_caret_line(p_caret) == get_selection_origin_line(p_caret) && get_caret_column(p_caret) == get_selection_origin_column(p_caret)) {
  4972. deselect(p_caret);
  4973. }
  4974. if (is_inside_tree() && p_adjust_viewport) {
  4975. adjust_viewport_to_caret(p_caret);
  4976. }
  4977. setting_caret_line = false;
  4978. if (caret_moved) {
  4979. _caret_changed(p_caret);
  4980. }
  4981. queue_accessibility_update();
  4982. }
  4983. int TextEdit::get_caret_line(int p_caret) const {
  4984. ERR_FAIL_INDEX_V(p_caret, carets.size(), 0);
  4985. return carets[p_caret].line;
  4986. }
  4987. void TextEdit::set_caret_column(int p_column, bool p_adjust_viewport, int p_caret) {
  4988. ERR_FAIL_INDEX(p_caret, carets.size());
  4989. p_column = CLAMP(p_column, 0, get_line(get_caret_line(p_caret)).length());
  4990. bool caret_moved = get_caret_column(p_caret) != p_column;
  4991. carets.write[p_caret].column = p_column;
  4992. 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));
  4993. if (!has_selection(p_caret)) {
  4994. // Set the selection origin last fit x to be the same, so we can tell if there was a selection.
  4995. carets.write[p_caret].selection.origin_last_fit_x = carets[p_caret].last_fit_x;
  4996. }
  4997. // Unselect if the caret moved to the selection origin.
  4998. if (has_selection(p_caret) && get_caret_line(p_caret) == get_selection_origin_line(p_caret) && get_caret_column(p_caret) == get_selection_origin_column(p_caret)) {
  4999. deselect(p_caret);
  5000. }
  5001. if (is_inside_tree() && p_adjust_viewport) {
  5002. adjust_viewport_to_caret(p_caret);
  5003. }
  5004. if (caret_moved) {
  5005. _caret_changed(p_caret);
  5006. }
  5007. queue_accessibility_update();
  5008. }
  5009. int TextEdit::get_caret_column(int p_caret) const {
  5010. ERR_FAIL_INDEX_V(p_caret, carets.size(), 0);
  5011. return carets[p_caret].column;
  5012. }
  5013. int TextEdit::get_caret_wrap_index(int p_caret) const {
  5014. ERR_FAIL_INDEX_V(p_caret, carets.size(), 0);
  5015. return get_line_wrap_index_at_column(get_caret_line(p_caret), get_caret_column(p_caret));
  5016. }
  5017. String TextEdit::get_word_under_caret(int p_caret) const {
  5018. ERR_FAIL_COND_V(p_caret >= carets.size() || p_caret < -1, "");
  5019. StringBuilder selected_text;
  5020. for (int c = 0; c < carets.size(); c++) {
  5021. if (p_caret != -1 && p_caret != c) {
  5022. continue;
  5023. }
  5024. PackedInt32Array words = TS->shaped_text_get_word_breaks(text.get_line_data(get_caret_line(c))->get_rid());
  5025. for (int i = 0; i < words.size(); i = i + 2) {
  5026. if (words[i] <= get_caret_column(c) && words[i + 1] > get_caret_column(c)) {
  5027. selected_text += text[get_caret_line(c)].substr(words[i], words[i + 1] - words[i]);
  5028. if (p_caret == -1 && c != carets.size() - 1) {
  5029. selected_text += "\n";
  5030. }
  5031. }
  5032. }
  5033. }
  5034. return selected_text.as_string();
  5035. }
  5036. /* Selection. */
  5037. void TextEdit::set_selecting_enabled(bool p_enabled) {
  5038. if (selecting_enabled == p_enabled) {
  5039. return;
  5040. }
  5041. selecting_enabled = p_enabled;
  5042. if (!selecting_enabled) {
  5043. deselect();
  5044. }
  5045. }
  5046. bool TextEdit::is_selecting_enabled() const {
  5047. return selecting_enabled;
  5048. }
  5049. void TextEdit::set_deselect_on_focus_loss_enabled(bool p_enabled) {
  5050. if (deselect_on_focus_loss_enabled == p_enabled) {
  5051. return;
  5052. }
  5053. deselect_on_focus_loss_enabled = p_enabled;
  5054. if (p_enabled && has_selection() && !has_focus()) {
  5055. deselect();
  5056. }
  5057. }
  5058. bool TextEdit::is_deselect_on_focus_loss_enabled() const {
  5059. return deselect_on_focus_loss_enabled;
  5060. }
  5061. void TextEdit::set_drag_and_drop_selection_enabled(bool p_enabled) {
  5062. drag_and_drop_selection_enabled = p_enabled;
  5063. }
  5064. bool TextEdit::is_drag_and_drop_selection_enabled() const {
  5065. return drag_and_drop_selection_enabled;
  5066. }
  5067. void TextEdit::set_selection_mode(SelectionMode p_mode) {
  5068. selecting_mode = p_mode;
  5069. }
  5070. TextEdit::SelectionMode TextEdit::get_selection_mode() const {
  5071. return selecting_mode;
  5072. }
  5073. void TextEdit::select_all() {
  5074. _push_current_op();
  5075. if (!selecting_enabled) {
  5076. return;
  5077. }
  5078. if (text.size() == 1 && text[0].is_empty()) {
  5079. return;
  5080. }
  5081. remove_secondary_carets();
  5082. set_selection_mode(SelectionMode::SELECTION_MODE_SHIFT);
  5083. select(0, 0, text.size() - 1, text[text.size() - 1].length());
  5084. }
  5085. void TextEdit::select_word_under_caret(int p_caret) {
  5086. ERR_FAIL_COND(p_caret >= carets.size() || p_caret < -1);
  5087. _push_current_op();
  5088. if (!selecting_enabled) {
  5089. return;
  5090. }
  5091. if (text.size() == 1 && text[0].is_empty()) {
  5092. return;
  5093. }
  5094. set_selection_mode(SELECTION_MODE_NONE);
  5095. for (int c = 0; c < carets.size(); c++) {
  5096. if (p_caret != -1 && p_caret != c) {
  5097. continue;
  5098. }
  5099. if (has_selection(c)) {
  5100. // Allow toggling selection by pressing the shortcut a second time.
  5101. // This is also usable as a general-purpose "deselect" shortcut after
  5102. // selecting anything.
  5103. deselect(c);
  5104. continue;
  5105. }
  5106. int begin = 0;
  5107. int end = 0;
  5108. const PackedInt32Array words = TS->shaped_text_get_word_breaks(text.get_line_data(get_caret_line(c))->get_rid());
  5109. for (int i = 0; i < words.size(); i = i + 2) {
  5110. 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])) {
  5111. begin = words[i];
  5112. end = words[i + 1];
  5113. break;
  5114. }
  5115. }
  5116. // No word found.
  5117. if (begin == 0 && end == 0) {
  5118. continue;
  5119. }
  5120. select(get_caret_line(c), begin, get_caret_line(c), end, c);
  5121. }
  5122. merge_overlapping_carets();
  5123. }
  5124. void TextEdit::add_selection_for_next_occurrence() {
  5125. if (!selecting_enabled || !is_multiple_carets_enabled()) {
  5126. return;
  5127. }
  5128. if (text.size() == 1 && text[0].is_empty()) {
  5129. return;
  5130. }
  5131. _push_current_op();
  5132. // Always use the last caret, to correctly search for
  5133. // the next occurrence that comes after this caret.
  5134. int caret = get_caret_count() - 1;
  5135. if (!has_selection(caret)) {
  5136. select_word_under_caret(caret);
  5137. return;
  5138. }
  5139. set_selection_mode(SELECTION_MODE_NONE);
  5140. const String &highlighted_text = get_selected_text(caret);
  5141. int column = get_selection_from_column(caret) + 1;
  5142. int line = get_selection_from_line(caret);
  5143. const Point2i next_occurrence = search(highlighted_text, SEARCH_MATCH_CASE, line, column);
  5144. if (next_occurrence.x == -1 || next_occurrence.y == -1) {
  5145. return;
  5146. }
  5147. int to_column = get_selection_to_column(caret) + 1;
  5148. int end = next_occurrence.x + (to_column - column);
  5149. int new_caret = add_caret(next_occurrence.y, end);
  5150. if (new_caret != -1) {
  5151. select(next_occurrence.y, next_occurrence.x, next_occurrence.y, end, new_caret);
  5152. _unhide_carets();
  5153. adjust_viewport_to_caret(new_caret);
  5154. merge_overlapping_carets();
  5155. }
  5156. }
  5157. void TextEdit::skip_selection_for_next_occurrence() {
  5158. if (!selecting_enabled) {
  5159. return;
  5160. }
  5161. if (text.size() == 1 && text[0].is_empty()) {
  5162. return;
  5163. }
  5164. set_selection_mode(SELECTION_MODE_NONE);
  5165. // Always use the last caret, to correctly search for
  5166. // the next occurrence that comes after this caret.
  5167. int caret = get_caret_count() - 1;
  5168. // Supports getting the text under caret without selecting it.
  5169. // It allows to use this shortcut to simply jump to the next (under caret) word.
  5170. // Due to const and &(reference) presence, ternary operator is a way to avoid errors and warnings.
  5171. const String &searched_text = has_selection(caret) ? get_selected_text(caret) : get_word_under_caret(caret);
  5172. int column = get_selection_from_column(caret) + 1;
  5173. int line = get_selection_from_line(caret);
  5174. const Point2i next_occurrence = search(searched_text, SEARCH_MATCH_CASE, line, column);
  5175. if (next_occurrence.x == -1 || next_occurrence.y == -1) {
  5176. return;
  5177. }
  5178. int to_column = get_selection_to_column(caret) + 1;
  5179. int end = next_occurrence.x + (to_column - column);
  5180. int new_caret = add_caret(next_occurrence.y, end);
  5181. if (new_caret != -1) {
  5182. select(next_occurrence.y, next_occurrence.x, next_occurrence.y, end, new_caret);
  5183. _unhide_carets();
  5184. adjust_viewport_to_caret(new_caret);
  5185. merge_overlapping_carets();
  5186. }
  5187. // Deselect word under previous caret.
  5188. if (has_selection(caret)) {
  5189. select_word_under_caret(caret);
  5190. }
  5191. // Remove previous caret.
  5192. if (get_caret_count() > 1) {
  5193. remove_caret(caret);
  5194. }
  5195. }
  5196. void TextEdit::select(int p_origin_line, int p_origin_column, int p_caret_line, int p_caret_column, int p_caret) {
  5197. ERR_FAIL_INDEX(p_caret, get_caret_count());
  5198. p_caret_line = CLAMP(p_caret_line, 0, text.size() - 1);
  5199. p_caret_column = CLAMP(p_caret_column, 0, text[p_caret_line].length());
  5200. set_caret_line(p_caret_line, false, true, -1, p_caret);
  5201. set_caret_column(p_caret_column, false, p_caret);
  5202. if (!selecting_enabled) {
  5203. return;
  5204. }
  5205. p_origin_line = CLAMP(p_origin_line, 0, text.size() - 1);
  5206. p_origin_column = CLAMP(p_origin_column, 0, text[p_origin_line].length());
  5207. set_selection_origin_line(p_origin_line, true, -1, p_caret);
  5208. set_selection_origin_column(p_origin_column, p_caret);
  5209. bool had_selection = has_selection(p_caret);
  5210. bool activate = p_origin_line != p_caret_line || p_origin_column != p_caret_column;
  5211. carets.write[p_caret].selection.active = activate;
  5212. if (had_selection != activate) {
  5213. _selection_changed(p_caret);
  5214. }
  5215. queue_accessibility_update();
  5216. queue_redraw();
  5217. }
  5218. bool TextEdit::has_selection(int p_caret) const {
  5219. ERR_FAIL_COND_V(p_caret >= carets.size() || p_caret < -1, false);
  5220. if (p_caret >= 0) {
  5221. return carets[p_caret].selection.active;
  5222. }
  5223. for (int i = 0; i < carets.size(); i++) {
  5224. if (carets[i].selection.active) {
  5225. return true;
  5226. }
  5227. }
  5228. return false;
  5229. }
  5230. String TextEdit::get_selected_text(int p_caret) {
  5231. ERR_FAIL_COND_V(p_caret >= carets.size() || p_caret < -1, "");
  5232. if (p_caret >= 0) {
  5233. if (!has_selection(p_caret)) {
  5234. return "";
  5235. }
  5236. return _base_get_text(get_selection_from_line(p_caret), get_selection_from_column(p_caret), get_selection_to_line(p_caret), get_selection_to_column(p_caret));
  5237. }
  5238. StringBuilder selected_text;
  5239. Vector<int> sorted_carets = get_sorted_carets();
  5240. for (int i = 0; i < sorted_carets.size(); i++) {
  5241. int caret_index = sorted_carets[i];
  5242. if (!has_selection(caret_index)) {
  5243. continue;
  5244. }
  5245. if (selected_text.get_string_length() != 0) {
  5246. selected_text += "\n";
  5247. }
  5248. selected_text += _base_get_text(get_selection_from_line(caret_index), get_selection_from_column(caret_index), get_selection_to_line(caret_index), get_selection_to_column(caret_index));
  5249. }
  5250. return selected_text.as_string();
  5251. }
  5252. int TextEdit::get_selection_at_line_column(int p_line, int p_column, bool p_include_edges, bool p_only_selections) const {
  5253. // Return the caret index of the found selection, or -1.
  5254. for (int i = 0; i < get_caret_count(); i++) {
  5255. if (_selection_contains(i, p_line, p_column, p_include_edges, p_only_selections)) {
  5256. return i;
  5257. }
  5258. }
  5259. return -1;
  5260. }
  5261. Vector<Point2i> TextEdit::get_line_ranges_from_carets(bool p_only_selections, bool p_merge_adjacent) const {
  5262. // Get a series of line ranges that cover all lines that have a caret or selection.
  5263. // For each Point2i range, x is the first line and y is the last line.
  5264. Vector<Point2i> ret;
  5265. int last_to_line = INT_MIN;
  5266. Vector<int> sorted_carets = get_sorted_carets();
  5267. for (int i = 0; i < sorted_carets.size(); i++) {
  5268. int caret_index = sorted_carets[i];
  5269. if (p_only_selections && !has_selection(caret_index)) {
  5270. continue;
  5271. }
  5272. Point2i range = Point2i(get_selection_from_line(caret_index), get_selection_to_line(caret_index));
  5273. if (has_selection(caret_index) && get_selection_to_column(caret_index) == 0) {
  5274. // Dont include selection end line if it ends at column 0.
  5275. range.y--;
  5276. }
  5277. if (range.x == last_to_line || (p_merge_adjacent && range.x - 1 == last_to_line)) {
  5278. // Merge if starts on the same line or adjacent line.
  5279. ret.write[ret.size() - 1].y = range.y;
  5280. } else {
  5281. ret.append(range);
  5282. }
  5283. last_to_line = range.y;
  5284. }
  5285. return ret;
  5286. }
  5287. TypedArray<Vector2i> TextEdit::get_line_ranges_from_carets_typed_array(bool p_only_selections, bool p_merge_adjacent) const {
  5288. // Wrapper for `get_line_ranges_from_carets` to return a datatype that can be exposed.
  5289. TypedArray<Vector2i> ret;
  5290. Vector<Point2i> ranges = get_line_ranges_from_carets(p_only_selections, p_merge_adjacent);
  5291. for (const Point2i &range : ranges) {
  5292. ret.push_back(range);
  5293. }
  5294. return ret;
  5295. }
  5296. void TextEdit::set_selection_origin_line(int p_line, bool p_can_be_hidden, int p_wrap_index, int p_caret) {
  5297. if (!selecting_enabled) {
  5298. return;
  5299. }
  5300. ERR_FAIL_INDEX(p_caret, carets.size());
  5301. p_line = CLAMP(p_line, 0, text.size() - 1);
  5302. if (!p_can_be_hidden) {
  5303. if (_is_line_hidden(p_line)) {
  5304. int move_down = get_next_visible_line_offset_from(p_line, 1) - 1;
  5305. if (p_line + move_down <= text.size() - 1 && !_is_line_hidden(p_line + move_down)) {
  5306. p_line += move_down;
  5307. } else {
  5308. int move_up = get_next_visible_line_offset_from(p_line, -1) - 1;
  5309. if (p_line - move_up > 0 && !_is_line_hidden(p_line - move_up)) {
  5310. p_line -= move_up;
  5311. } else {
  5312. WARN_PRINT("Selection origin set to hidden line " + itos(p_line) + " and there are no nonhidden lines.");
  5313. }
  5314. }
  5315. }
  5316. }
  5317. bool selection_moved = get_selection_origin_line(p_caret) != p_line;
  5318. carets.write[p_caret].selection.origin_line = p_line;
  5319. int n_col;
  5320. if (p_wrap_index >= 0) {
  5321. // Keep selection origin in same visual x position it was at previously.
  5322. n_col = _get_char_pos_for_line(carets[p_caret].selection.origin_last_fit_x, p_line, p_wrap_index);
  5323. if (n_col != 0 && get_line_wrapping_mode() != LineWrappingMode::LINE_WRAPPING_NONE && p_wrap_index < get_line_wrap_count(p_line)) {
  5324. // Offset by one to not go past the end of the wrapped line.
  5325. if (n_col >= text.get_line_wrap_ranges(p_line)[p_wrap_index].y) {
  5326. n_col -= 1;
  5327. }
  5328. }
  5329. } else {
  5330. // Clamp the column.
  5331. n_col = MIN(get_selection_origin_column(p_caret), get_line(p_line).length());
  5332. }
  5333. selection_moved = (selection_moved || get_selection_origin_column(p_caret) != n_col);
  5334. carets.write[p_caret].selection.origin_column = n_col;
  5335. // Unselect if the selection origin moved to the caret.
  5336. if (p_wrap_index >= 0 && has_selection(p_caret) && get_caret_line(p_caret) == get_selection_origin_line(p_caret) && get_caret_column(p_caret) == get_selection_origin_column(p_caret)) {
  5337. deselect(p_caret);
  5338. }
  5339. if (selection_moved && has_selection(p_caret)) {
  5340. _selection_changed(p_caret);
  5341. }
  5342. }
  5343. void TextEdit::set_selection_origin_column(int p_column, int p_caret) {
  5344. if (!selecting_enabled) {
  5345. return;
  5346. }
  5347. ERR_FAIL_INDEX(p_caret, carets.size());
  5348. p_column = CLAMP(p_column, 0, get_line(get_selection_origin_line(p_caret)).length());
  5349. bool selection_moved = get_selection_origin_column(p_caret) != p_column;
  5350. carets.write[p_caret].selection.origin_column = p_column;
  5351. carets.write[p_caret].selection.origin_last_fit_x = _get_column_x_offset_for_line(get_selection_origin_column(p_caret), get_selection_origin_line(p_caret), get_selection_origin_column(p_caret));
  5352. // Unselect if the selection origin moved to the caret.
  5353. if (has_selection(p_caret) && get_caret_line(p_caret) == get_selection_origin_line(p_caret) && get_caret_column(p_caret) == get_selection_origin_column(p_caret)) {
  5354. deselect(p_caret);
  5355. }
  5356. if (selection_moved && has_selection(p_caret)) {
  5357. _selection_changed(p_caret);
  5358. }
  5359. }
  5360. int TextEdit::get_selection_origin_line(int p_caret) const {
  5361. ERR_FAIL_INDEX_V(p_caret, carets.size(), -1);
  5362. return carets[p_caret].selection.origin_line;
  5363. }
  5364. int TextEdit::get_selection_origin_column(int p_caret) const {
  5365. ERR_FAIL_INDEX_V(p_caret, carets.size(), -1);
  5366. return carets[p_caret].selection.origin_column;
  5367. }
  5368. int TextEdit::get_next_composite_character_column(int p_line, int p_column) const {
  5369. ERR_FAIL_INDEX_V(p_line, text.size(), -1);
  5370. ERR_FAIL_INDEX_V(p_column, text[p_line].length() + 1, -1);
  5371. if (p_column == text[p_line].length()) {
  5372. return p_column;
  5373. } else {
  5374. return TS->shaped_text_next_character_pos(text.get_line_data(p_line)->get_rid(), (p_column));
  5375. }
  5376. }
  5377. int TextEdit::get_previous_composite_character_column(int p_line, int p_column) const {
  5378. ERR_FAIL_INDEX_V(p_line, text.size(), -1);
  5379. ERR_FAIL_INDEX_V(p_column, text[p_line].length() + 1, -1);
  5380. if (p_column == 0) {
  5381. return 0;
  5382. } else {
  5383. return TS->shaped_text_prev_character_pos(text.get_line_data(p_line)->get_rid(), p_column);
  5384. }
  5385. }
  5386. int TextEdit::get_selection_from_line(int p_caret) const {
  5387. ERR_FAIL_INDEX_V(p_caret, carets.size(), -1);
  5388. if (!has_selection(p_caret)) {
  5389. return carets[p_caret].line;
  5390. }
  5391. return MIN(carets[p_caret].selection.origin_line, carets[p_caret].line);
  5392. }
  5393. int TextEdit::get_selection_from_column(int p_caret) const {
  5394. ERR_FAIL_INDEX_V(p_caret, carets.size(), -1);
  5395. if (!has_selection(p_caret)) {
  5396. return carets[p_caret].column;
  5397. }
  5398. if (carets[p_caret].selection.origin_line < carets[p_caret].line) {
  5399. return carets[p_caret].selection.origin_column;
  5400. } else if (carets[p_caret].selection.origin_line > carets[p_caret].line) {
  5401. return carets[p_caret].column;
  5402. } else {
  5403. return MIN(carets[p_caret].selection.origin_column, carets[p_caret].column);
  5404. }
  5405. }
  5406. int TextEdit::get_selection_to_line(int p_caret) const {
  5407. ERR_FAIL_INDEX_V(p_caret, carets.size(), -1);
  5408. if (!has_selection(p_caret)) {
  5409. return carets[p_caret].line;
  5410. }
  5411. return MAX(carets[p_caret].selection.origin_line, carets[p_caret].line);
  5412. }
  5413. int TextEdit::get_selection_to_column(int p_caret) const {
  5414. ERR_FAIL_INDEX_V(p_caret, carets.size(), -1);
  5415. if (!has_selection(p_caret)) {
  5416. return carets[p_caret].column;
  5417. }
  5418. if (carets[p_caret].selection.origin_line < carets[p_caret].line) {
  5419. return carets[p_caret].column;
  5420. } else if (carets[p_caret].selection.origin_line > carets[p_caret].line) {
  5421. return carets[p_caret].selection.origin_column;
  5422. } else {
  5423. return MAX(carets[p_caret].selection.origin_column, carets[p_caret].column);
  5424. }
  5425. }
  5426. bool TextEdit::is_caret_after_selection_origin(int p_caret) const {
  5427. ERR_FAIL_INDEX_V(p_caret, carets.size(), false);
  5428. if (!has_selection(p_caret)) {
  5429. return true;
  5430. }
  5431. return carets[p_caret].line > carets[p_caret].selection.origin_line || (carets[p_caret].line == carets[p_caret].selection.origin_line && carets[p_caret].column >= carets[p_caret].selection.origin_column);
  5432. }
  5433. void TextEdit::deselect(int p_caret) {
  5434. ERR_FAIL_COND(p_caret >= carets.size() || p_caret < -1);
  5435. bool selection_changed = false;
  5436. if (p_caret >= 0) {
  5437. selection_changed = carets.write[p_caret].selection.active;
  5438. carets.write[p_caret].selection.active = false;
  5439. } else {
  5440. for (int i = 0; i < carets.size(); i++) {
  5441. selection_changed |= carets.write[i].selection.active;
  5442. carets.write[i].selection.active = false;
  5443. }
  5444. }
  5445. if (selection_changed) {
  5446. _selection_changed(p_caret);
  5447. }
  5448. queue_accessibility_update();
  5449. queue_redraw();
  5450. }
  5451. void TextEdit::delete_selection(int p_caret) {
  5452. ERR_FAIL_COND(p_caret >= get_caret_count() || p_caret < -1);
  5453. begin_complex_operation();
  5454. begin_multicaret_edit();
  5455. for (int i = 0; i < get_caret_count(); i++) {
  5456. if (p_caret != -1 && p_caret != i) {
  5457. continue;
  5458. }
  5459. if (p_caret == -1 && multicaret_edit_ignore_caret(i)) {
  5460. continue;
  5461. }
  5462. if (!has_selection(i)) {
  5463. continue;
  5464. }
  5465. int selection_from_line = get_selection_from_line(i);
  5466. int selection_from_column = get_selection_from_column(i);
  5467. int selection_to_line = get_selection_to_line(i);
  5468. int selection_to_column = get_selection_to_column(i);
  5469. _remove_text(selection_from_line, selection_from_column, selection_to_line, selection_to_column);
  5470. _offset_carets_after(selection_to_line, selection_to_column, selection_from_line, selection_from_column);
  5471. merge_overlapping_carets();
  5472. deselect(i);
  5473. set_caret_line(selection_from_line, false, false, -1, i);
  5474. set_caret_column(selection_from_column, i == 0, i);
  5475. }
  5476. end_multicaret_edit();
  5477. end_complex_operation();
  5478. }
  5479. /* Line wrapping. */
  5480. void TextEdit::set_line_wrapping_mode(LineWrappingMode p_wrapping_mode) {
  5481. if (line_wrapping_mode != p_wrapping_mode) {
  5482. line_wrapping_mode = p_wrapping_mode;
  5483. _update_wrap_at_column(true);
  5484. queue_accessibility_update();
  5485. queue_redraw();
  5486. }
  5487. }
  5488. TextEdit::LineWrappingMode TextEdit::get_line_wrapping_mode() const {
  5489. return line_wrapping_mode;
  5490. }
  5491. void TextEdit::set_autowrap_mode(TextServer::AutowrapMode p_mode) {
  5492. if (autowrap_mode == p_mode) {
  5493. return;
  5494. }
  5495. autowrap_mode = p_mode;
  5496. if (get_line_wrapping_mode() != LineWrappingMode::LINE_WRAPPING_NONE) {
  5497. _update_wrap_at_column(true);
  5498. queue_accessibility_update();
  5499. queue_redraw();
  5500. }
  5501. }
  5502. TextServer::AutowrapMode TextEdit::get_autowrap_mode() const {
  5503. return autowrap_mode;
  5504. }
  5505. bool TextEdit::is_line_wrapped(int p_line) const {
  5506. ERR_FAIL_INDEX_V(p_line, text.size(), false);
  5507. if (get_line_wrapping_mode() == LineWrappingMode::LINE_WRAPPING_NONE) {
  5508. return false;
  5509. }
  5510. return text.get_line_wrap_amount(p_line) > 0;
  5511. }
  5512. int TextEdit::get_line_wrap_count(int p_line) const {
  5513. ERR_FAIL_INDEX_V(p_line, text.size(), 0);
  5514. if (!is_line_wrapped(p_line)) {
  5515. return 0;
  5516. }
  5517. return text.get_line_wrap_amount(p_line);
  5518. }
  5519. int TextEdit::get_line_wrap_index_at_column(int p_line, int p_column) const {
  5520. ERR_FAIL_INDEX_V(p_line, text.size(), 0);
  5521. ERR_FAIL_COND_V(p_column < 0, 0);
  5522. ERR_FAIL_COND_V(p_column > text[p_line].length(), 0);
  5523. if (!is_line_wrapped(p_line)) {
  5524. return 0;
  5525. }
  5526. /* Loop through wraps in the line text until we get to the column. */
  5527. int wrap_index = 0;
  5528. int col = 0;
  5529. Vector<String> lines = get_line_wrapped_text(p_line);
  5530. for (int i = 0; i < lines.size(); i++) {
  5531. wrap_index = i;
  5532. col += lines[wrap_index].length();
  5533. if (col > p_column) {
  5534. break;
  5535. }
  5536. }
  5537. return wrap_index;
  5538. }
  5539. Vector<String> TextEdit::get_line_wrapped_text(int p_line) const {
  5540. ERR_FAIL_INDEX_V(p_line, text.size(), Vector<String>());
  5541. Vector<String> lines;
  5542. if (!is_line_wrapped(p_line)) {
  5543. lines.push_back(text[p_line]);
  5544. return lines;
  5545. }
  5546. const String &line_text = text[p_line];
  5547. Vector<Vector2i> line_ranges = text.get_line_wrap_ranges(p_line);
  5548. for (int i = 0; i < line_ranges.size(); i++) {
  5549. lines.push_back(line_text.substr(line_ranges[i].x, line_ranges[i].y - line_ranges[i].x));
  5550. }
  5551. return lines;
  5552. }
  5553. /* Viewport */
  5554. // Scrolling.
  5555. void TextEdit::set_smooth_scroll_enabled(bool p_enabled) {
  5556. v_scroll->set_smooth_scroll_enabled(p_enabled);
  5557. smooth_scroll_enabled = p_enabled;
  5558. }
  5559. bool TextEdit::is_smooth_scroll_enabled() const {
  5560. return smooth_scroll_enabled;
  5561. }
  5562. void TextEdit::set_scroll_past_end_of_file_enabled(bool p_enabled) {
  5563. if (scroll_past_end_of_file_enabled == p_enabled) {
  5564. return;
  5565. }
  5566. scroll_past_end_of_file_enabled = p_enabled;
  5567. queue_redraw();
  5568. }
  5569. bool TextEdit::is_scroll_past_end_of_file_enabled() const {
  5570. return scroll_past_end_of_file_enabled;
  5571. }
  5572. VScrollBar *TextEdit::get_v_scroll_bar() const {
  5573. return v_scroll;
  5574. }
  5575. HScrollBar *TextEdit::get_h_scroll_bar() const {
  5576. return h_scroll;
  5577. }
  5578. void TextEdit::set_v_scroll(double p_scroll) {
  5579. v_scroll->set_value(p_scroll);
  5580. int max_v_scroll = v_scroll->get_max() - v_scroll->get_page();
  5581. if (p_scroll >= max_v_scroll - 1.0) {
  5582. _scroll_moved(v_scroll->get_value());
  5583. }
  5584. queue_accessibility_update();
  5585. }
  5586. double TextEdit::get_v_scroll() const {
  5587. return v_scroll->get_value();
  5588. }
  5589. void TextEdit::set_h_scroll(int p_scroll) {
  5590. if (p_scroll < 0) {
  5591. p_scroll = 0;
  5592. }
  5593. h_scroll->set_value(p_scroll);
  5594. queue_accessibility_update();
  5595. }
  5596. int TextEdit::get_h_scroll() const {
  5597. return h_scroll->get_value();
  5598. }
  5599. void TextEdit::set_v_scroll_speed(float p_speed) {
  5600. // Prevent setting a vertical scroll speed value under 1.
  5601. ERR_FAIL_COND(p_speed < 1.0);
  5602. v_scroll_speed = p_speed;
  5603. }
  5604. float TextEdit::get_v_scroll_speed() const {
  5605. return v_scroll_speed;
  5606. }
  5607. void TextEdit::set_fit_content_height_enabled(bool p_enabled) {
  5608. if (fit_content_height == p_enabled) {
  5609. return;
  5610. }
  5611. fit_content_height = p_enabled;
  5612. update_minimum_size();
  5613. }
  5614. bool TextEdit::is_fit_content_height_enabled() const {
  5615. return fit_content_height;
  5616. }
  5617. void TextEdit::set_fit_content_width_enabled(bool p_enabled) {
  5618. if (fit_content_width == p_enabled) {
  5619. return;
  5620. }
  5621. fit_content_width = p_enabled;
  5622. update_minimum_size();
  5623. }
  5624. bool TextEdit::is_fit_content_width_enabled() const {
  5625. return fit_content_width;
  5626. }
  5627. double TextEdit::get_scroll_pos_for_line(int p_line, int p_wrap_index) const {
  5628. ERR_FAIL_INDEX_V(p_line, text.size(), 0);
  5629. ERR_FAIL_COND_V(p_wrap_index < 0, 0);
  5630. ERR_FAIL_COND_V(p_wrap_index > get_line_wrap_count(p_line), 0);
  5631. if (get_line_wrapping_mode() == LineWrappingMode::LINE_WRAPPING_NONE && !_is_hiding_enabled()) {
  5632. return p_line;
  5633. }
  5634. double new_line_scroll_pos = 0.0;
  5635. if (p_line > 0) {
  5636. new_line_scroll_pos = get_visible_line_count_in_range(0, MIN(p_line - 1, text.size() - 1));
  5637. }
  5638. new_line_scroll_pos += p_wrap_index;
  5639. return new_line_scroll_pos;
  5640. }
  5641. // Visible lines.
  5642. void TextEdit::set_line_as_first_visible(int p_line, int p_wrap_index) {
  5643. ERR_FAIL_INDEX(p_line, text.size());
  5644. ERR_FAIL_COND(p_wrap_index < 0);
  5645. ERR_FAIL_COND(p_wrap_index > get_line_wrap_count(p_line));
  5646. set_v_scroll(get_scroll_pos_for_line(p_line, p_wrap_index));
  5647. scrolling = false;
  5648. minimap_clicked = false;
  5649. }
  5650. int TextEdit::get_first_visible_line() const {
  5651. return CLAMP(first_visible_line, 0, text.size() - 1);
  5652. }
  5653. void TextEdit::set_line_as_center_visible(int p_line, int p_wrap_index) {
  5654. ERR_FAIL_INDEX(p_line, text.size());
  5655. ERR_FAIL_COND(p_wrap_index < 0);
  5656. ERR_FAIL_COND(p_wrap_index > get_line_wrap_count(p_line));
  5657. scrolling = false;
  5658. minimap_clicked = false;
  5659. int visible_rows = get_visible_line_count();
  5660. Point2i next_line = get_next_visible_line_index_offset_from(p_line, p_wrap_index, (-visible_rows / 2) - 1);
  5661. int first_line = p_line - next_line.x + 1;
  5662. if (first_line < 0) {
  5663. set_v_scroll(0);
  5664. return;
  5665. }
  5666. set_v_scroll(get_scroll_pos_for_line(first_line, next_line.y));
  5667. }
  5668. void TextEdit::set_line_as_last_visible(int p_line, int p_wrap_index) {
  5669. ERR_FAIL_INDEX(p_line, text.size());
  5670. ERR_FAIL_COND(p_wrap_index < 0);
  5671. ERR_FAIL_COND(p_wrap_index > get_line_wrap_count(p_line));
  5672. scrolling = false;
  5673. minimap_clicked = false;
  5674. Point2i next_line = get_next_visible_line_index_offset_from(p_line, p_wrap_index, -get_visible_line_count() - 1);
  5675. int first_line = p_line - next_line.x + 1;
  5676. // Adding _get_visible_lines_offset is not 100% correct as we end up showing almost p_line + 1, however, it provides a
  5677. // better user experience. Therefore we need to special case < visible line count, else showing line 0 is impossible.
  5678. if (get_visible_line_count_in_range(0, p_line) < get_visible_line_count() + 1) {
  5679. set_v_scroll(0);
  5680. return;
  5681. }
  5682. set_v_scroll(Math::round(get_scroll_pos_for_line(first_line, next_line.y) + _get_visible_lines_offset()));
  5683. }
  5684. int TextEdit::get_last_full_visible_line() const {
  5685. int first_vis_line = get_first_visible_line();
  5686. int last_vis_line = 0;
  5687. 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;
  5688. last_vis_line = CLAMP(last_vis_line, 0, text.size() - 1);
  5689. return last_vis_line;
  5690. }
  5691. int TextEdit::get_last_full_visible_line_wrap_index() const {
  5692. int first_vis_line = get_first_visible_line();
  5693. return get_next_visible_line_index_offset_from(first_vis_line, first_visible_line_wrap_ofs, get_visible_line_count()).y;
  5694. }
  5695. int TextEdit::get_visible_line_count() const {
  5696. return _get_control_height() / get_line_height();
  5697. }
  5698. int TextEdit::get_visible_line_count_in_range(int p_from_line, int p_to_line) const {
  5699. ERR_FAIL_INDEX_V(p_from_line, text.size(), 0);
  5700. ERR_FAIL_INDEX_V(p_to_line, text.size(), 0);
  5701. // So we can handle inputs in whatever order.
  5702. if (p_from_line > p_to_line) {
  5703. SWAP(p_from_line, p_to_line);
  5704. }
  5705. // Returns the total number of (lines + wrapped - hidden).
  5706. if (!_is_hiding_enabled() && get_line_wrapping_mode() == LineWrappingMode::LINE_WRAPPING_NONE) {
  5707. return (p_to_line - p_from_line) + 1;
  5708. }
  5709. int total_rows = 0;
  5710. for (int i = p_from_line; i <= p_to_line; i++) {
  5711. if (!text.is_hidden(i)) {
  5712. total_rows++;
  5713. total_rows += get_line_wrap_count(i);
  5714. }
  5715. }
  5716. return total_rows;
  5717. }
  5718. int TextEdit::get_total_visible_line_count() const {
  5719. return text.get_total_visible_line_count();
  5720. }
  5721. // Auto adjust.
  5722. void TextEdit::adjust_viewport_to_caret(int p_caret) {
  5723. ERR_FAIL_INDEX(p_caret, carets.size());
  5724. // Move viewport so the caret is visible on the screen vertically.
  5725. int cur_line = get_caret_line(p_caret);
  5726. int cur_wrap = get_caret_wrap_index(p_caret);
  5727. int first_vis_line = get_first_visible_line();
  5728. int first_vis_wrap = first_visible_line_wrap_ofs;
  5729. int last_vis_line = get_last_full_visible_line();
  5730. int last_vis_wrap = get_last_full_visible_line_wrap_index();
  5731. if (cur_line < first_vis_line || (cur_line == first_vis_line && cur_wrap < first_vis_wrap)) {
  5732. // Caret is above screen.
  5733. set_line_as_first_visible(cur_line, cur_wrap);
  5734. } else if (cur_line > last_vis_line || (cur_line == last_vis_line && cur_wrap > last_vis_wrap)) {
  5735. // Caret is below screen.
  5736. set_line_as_last_visible(cur_line, cur_wrap);
  5737. }
  5738. _adjust_viewport_to_caret_horizontally(p_caret, false);
  5739. }
  5740. void TextEdit::center_viewport_to_caret(int p_caret) {
  5741. ERR_FAIL_INDEX(p_caret, carets.size());
  5742. // Move viewport so the caret is in the center of the screen vertically.
  5743. scrolling = false;
  5744. minimap_clicked = false;
  5745. set_line_as_center_visible(get_caret_line(p_caret), get_caret_wrap_index(p_caret));
  5746. _adjust_viewport_to_caret_horizontally(p_caret);
  5747. }
  5748. /* Minimap */
  5749. void TextEdit::set_draw_minimap(bool p_enabled) {
  5750. if (draw_minimap == p_enabled) {
  5751. return;
  5752. }
  5753. draw_minimap = p_enabled;
  5754. _update_wrap_at_column();
  5755. queue_redraw();
  5756. }
  5757. bool TextEdit::is_drawing_minimap() const {
  5758. return draw_minimap;
  5759. }
  5760. void TextEdit::set_minimap_width(int p_minimap_width) {
  5761. if (minimap_width == p_minimap_width) {
  5762. return;
  5763. }
  5764. minimap_width = p_minimap_width;
  5765. _update_wrap_at_column();
  5766. queue_redraw();
  5767. }
  5768. int TextEdit::get_minimap_width() const {
  5769. return minimap_width;
  5770. }
  5771. int TextEdit::get_minimap_visible_lines() const {
  5772. return _get_control_height() / (minimap_char_size.y + minimap_line_spacing);
  5773. }
  5774. /* Gutters. */
  5775. void TextEdit::add_gutter(int p_at) {
  5776. if (p_at < 0 || p_at > gutters.size()) {
  5777. gutters.push_back(GutterInfo());
  5778. } else {
  5779. gutters.insert(p_at, GutterInfo());
  5780. }
  5781. text.add_gutter(p_at);
  5782. _update_gutter_width();
  5783. emit_signal(SNAME("gutter_added"));
  5784. queue_redraw();
  5785. }
  5786. void TextEdit::remove_gutter(int p_gutter) {
  5787. ERR_FAIL_INDEX(p_gutter, gutters.size());
  5788. gutters.remove_at(p_gutter);
  5789. text.remove_gutter(p_gutter);
  5790. _update_gutter_width();
  5791. emit_signal(SNAME("gutter_removed"));
  5792. queue_redraw();
  5793. }
  5794. int TextEdit::get_gutter_count() const {
  5795. return gutters.size();
  5796. }
  5797. void TextEdit::set_gutter_name(int p_gutter, const String &p_name) {
  5798. ERR_FAIL_INDEX(p_gutter, gutters.size());
  5799. gutters.write[p_gutter].name = p_name;
  5800. }
  5801. String TextEdit::get_gutter_name(int p_gutter) const {
  5802. ERR_FAIL_INDEX_V(p_gutter, gutters.size(), "");
  5803. return gutters[p_gutter].name;
  5804. }
  5805. void TextEdit::set_gutter_type(int p_gutter, GutterType p_type) {
  5806. ERR_FAIL_INDEX(p_gutter, gutters.size());
  5807. if (gutters[p_gutter].type == p_type) {
  5808. return;
  5809. }
  5810. gutters.write[p_gutter].type = p_type;
  5811. queue_redraw();
  5812. }
  5813. TextEdit::GutterType TextEdit::get_gutter_type(int p_gutter) const {
  5814. ERR_FAIL_INDEX_V(p_gutter, gutters.size(), GUTTER_TYPE_STRING);
  5815. return gutters[p_gutter].type;
  5816. }
  5817. void TextEdit::set_gutter_width(int p_gutter, int p_width) {
  5818. ERR_FAIL_INDEX(p_gutter, gutters.size());
  5819. if (gutters[p_gutter].width == p_width) {
  5820. return;
  5821. }
  5822. gutters.write[p_gutter].width = p_width;
  5823. _update_gutter_width();
  5824. }
  5825. int TextEdit::get_gutter_width(int p_gutter) const {
  5826. ERR_FAIL_INDEX_V(p_gutter, gutters.size(), -1);
  5827. return gutters[p_gutter].width;
  5828. }
  5829. int TextEdit::get_total_gutter_width() const {
  5830. return gutters_width + gutter_padding;
  5831. }
  5832. void TextEdit::set_gutter_draw(int p_gutter, bool p_draw) {
  5833. ERR_FAIL_INDEX(p_gutter, gutters.size());
  5834. if (gutters[p_gutter].draw == p_draw) {
  5835. return;
  5836. }
  5837. gutters.write[p_gutter].draw = p_draw;
  5838. _update_gutter_width();
  5839. }
  5840. bool TextEdit::is_gutter_drawn(int p_gutter) const {
  5841. ERR_FAIL_INDEX_V(p_gutter, gutters.size(), false);
  5842. return gutters[p_gutter].draw;
  5843. }
  5844. void TextEdit::set_gutter_clickable(int p_gutter, bool p_clickable) {
  5845. ERR_FAIL_INDEX(p_gutter, gutters.size());
  5846. if (gutters[p_gutter].clickable == p_clickable) {
  5847. return;
  5848. }
  5849. gutters.write[p_gutter].clickable = p_clickable;
  5850. queue_redraw();
  5851. }
  5852. bool TextEdit::is_gutter_clickable(int p_gutter) const {
  5853. ERR_FAIL_INDEX_V(p_gutter, gutters.size(), false);
  5854. return gutters[p_gutter].clickable;
  5855. }
  5856. void TextEdit::set_gutter_overwritable(int p_gutter, bool p_overwritable) {
  5857. ERR_FAIL_INDEX(p_gutter, gutters.size());
  5858. gutters.write[p_gutter].overwritable = p_overwritable;
  5859. }
  5860. bool TextEdit::is_gutter_overwritable(int p_gutter) const {
  5861. ERR_FAIL_INDEX_V(p_gutter, gutters.size(), false);
  5862. return gutters[p_gutter].overwritable;
  5863. }
  5864. void TextEdit::merge_gutters(int p_from_line, int p_to_line) {
  5865. ERR_FAIL_INDEX(p_from_line, text.size());
  5866. ERR_FAIL_INDEX(p_to_line, text.size());
  5867. if (p_from_line == p_to_line) {
  5868. return;
  5869. }
  5870. for (int i = 0; i < gutters.size(); i++) {
  5871. if (!gutters[i].overwritable) {
  5872. continue;
  5873. }
  5874. if (text.get_line_gutter_text(p_from_line, i) != "") {
  5875. text.set_line_gutter_text(p_to_line, i, text.get_line_gutter_text(p_from_line, i));
  5876. text.set_line_gutter_item_color(p_to_line, i, text.get_line_gutter_item_color(p_from_line, i));
  5877. }
  5878. if (text.get_line_gutter_icon(p_from_line, i).is_valid()) {
  5879. text.set_line_gutter_icon(p_to_line, i, text.get_line_gutter_icon(p_from_line, i));
  5880. text.set_line_gutter_item_color(p_to_line, i, text.get_line_gutter_item_color(p_from_line, i));
  5881. }
  5882. if (text.get_line_gutter_metadata(p_from_line, i) != "") {
  5883. text.set_line_gutter_metadata(p_to_line, i, text.get_line_gutter_metadata(p_from_line, i));
  5884. }
  5885. if (text.is_line_gutter_clickable(p_from_line, i)) {
  5886. text.set_line_gutter_clickable(p_to_line, i, true);
  5887. }
  5888. }
  5889. queue_redraw();
  5890. }
  5891. void TextEdit::set_gutter_custom_draw(int p_gutter, const Callable &p_draw_callback) {
  5892. ERR_FAIL_INDEX(p_gutter, gutters.size());
  5893. if (gutters[p_gutter].custom_draw_callback == p_draw_callback) {
  5894. return;
  5895. }
  5896. gutters.write[p_gutter].custom_draw_callback = p_draw_callback;
  5897. queue_redraw();
  5898. }
  5899. // Line gutters.
  5900. void TextEdit::set_line_gutter_metadata(int p_line, int p_gutter, const Variant &p_metadata) {
  5901. ERR_FAIL_INDEX(p_line, text.size());
  5902. ERR_FAIL_INDEX(p_gutter, gutters.size());
  5903. text.set_line_gutter_metadata(p_line, p_gutter, p_metadata);
  5904. }
  5905. Variant TextEdit::get_line_gutter_metadata(int p_line, int p_gutter) const {
  5906. ERR_FAIL_INDEX_V(p_line, text.size(), "");
  5907. ERR_FAIL_INDEX_V(p_gutter, gutters.size(), "");
  5908. return text.get_line_gutter_metadata(p_line, p_gutter);
  5909. }
  5910. void TextEdit::set_line_gutter_text(int p_line, int p_gutter, const String &p_text) {
  5911. ERR_FAIL_INDEX(p_line, text.size());
  5912. ERR_FAIL_INDEX(p_gutter, gutters.size());
  5913. if (text.get_line_gutter_text(p_line, p_gutter) == p_text) {
  5914. return;
  5915. }
  5916. text.set_line_gutter_text(p_line, p_gutter, p_text);
  5917. queue_redraw();
  5918. }
  5919. String TextEdit::get_line_gutter_text(int p_line, int p_gutter) const {
  5920. ERR_FAIL_INDEX_V(p_line, text.size(), "");
  5921. ERR_FAIL_INDEX_V(p_gutter, gutters.size(), "");
  5922. return text.get_line_gutter_text(p_line, p_gutter);
  5923. }
  5924. void TextEdit::set_line_gutter_icon(int p_line, int p_gutter, const Ref<Texture2D> &p_icon) {
  5925. ERR_FAIL_INDEX(p_line, text.size());
  5926. ERR_FAIL_INDEX(p_gutter, gutters.size());
  5927. if (text.get_line_gutter_icon(p_line, p_gutter) == p_icon) {
  5928. return;
  5929. }
  5930. text.set_line_gutter_icon(p_line, p_gutter, p_icon);
  5931. queue_redraw();
  5932. }
  5933. Ref<Texture2D> TextEdit::get_line_gutter_icon(int p_line, int p_gutter) const {
  5934. ERR_FAIL_INDEX_V(p_line, text.size(), Ref<Texture2D>());
  5935. ERR_FAIL_INDEX_V(p_gutter, gutters.size(), Ref<Texture2D>());
  5936. return text.get_line_gutter_icon(p_line, p_gutter);
  5937. }
  5938. void TextEdit::set_line_gutter_item_color(int p_line, int p_gutter, const Color &p_color) {
  5939. ERR_FAIL_INDEX(p_line, text.size());
  5940. ERR_FAIL_INDEX(p_gutter, gutters.size());
  5941. if (text.get_line_gutter_item_color(p_line, p_gutter) == p_color) {
  5942. return;
  5943. }
  5944. text.set_line_gutter_item_color(p_line, p_gutter, p_color);
  5945. queue_redraw();
  5946. }
  5947. Color TextEdit::get_line_gutter_item_color(int p_line, int p_gutter) const {
  5948. ERR_FAIL_INDEX_V(p_line, text.size(), Color());
  5949. ERR_FAIL_INDEX_V(p_gutter, gutters.size(), Color());
  5950. return text.get_line_gutter_item_color(p_line, p_gutter);
  5951. }
  5952. void TextEdit::set_line_gutter_clickable(int p_line, int p_gutter, bool p_clickable) {
  5953. ERR_FAIL_INDEX(p_line, text.size());
  5954. ERR_FAIL_INDEX(p_gutter, gutters.size());
  5955. text.set_line_gutter_clickable(p_line, p_gutter, p_clickable);
  5956. }
  5957. bool TextEdit::is_line_gutter_clickable(int p_line, int p_gutter) const {
  5958. ERR_FAIL_INDEX_V(p_line, text.size(), false);
  5959. ERR_FAIL_INDEX_V(p_gutter, gutters.size(), false);
  5960. return text.is_line_gutter_clickable(p_line, p_gutter);
  5961. }
  5962. // Line style
  5963. void TextEdit::set_line_background_color(int p_line, const Color &p_color) {
  5964. ERR_FAIL_INDEX(p_line, text.size());
  5965. if (text.get_line_background_color(p_line) == p_color) {
  5966. return;
  5967. }
  5968. text.set_line_background_color(p_line, p_color);
  5969. queue_redraw();
  5970. }
  5971. Color TextEdit::get_line_background_color(int p_line) const {
  5972. ERR_FAIL_INDEX_V(p_line, text.size(), Color());
  5973. return text.get_line_background_color(p_line);
  5974. }
  5975. /* Syntax Highlighting. */
  5976. void TextEdit::set_syntax_highlighter(Ref<SyntaxHighlighter> p_syntax_highlighter) {
  5977. if (syntax_highlighter == p_syntax_highlighter && syntax_highlighter.is_valid() == p_syntax_highlighter.is_valid()) {
  5978. return;
  5979. }
  5980. syntax_highlighter = p_syntax_highlighter;
  5981. if (syntax_highlighter.is_valid()) {
  5982. syntax_highlighter->set_text_edit(this);
  5983. }
  5984. _clear_syntax_highlighting_cache();
  5985. queue_redraw();
  5986. }
  5987. Ref<SyntaxHighlighter> TextEdit::get_syntax_highlighter() const {
  5988. return syntax_highlighter;
  5989. }
  5990. /* Visual. */
  5991. void TextEdit::set_highlight_current_line(bool p_enabled) {
  5992. if (highlight_current_line == p_enabled) {
  5993. return;
  5994. }
  5995. highlight_current_line = p_enabled;
  5996. queue_redraw();
  5997. }
  5998. bool TextEdit::is_highlight_current_line_enabled() const {
  5999. return highlight_current_line;
  6000. }
  6001. void TextEdit::set_highlight_all_occurrences(bool p_enabled) {
  6002. if (highlight_all_occurrences == p_enabled) {
  6003. return;
  6004. }
  6005. highlight_all_occurrences = p_enabled;
  6006. queue_redraw();
  6007. }
  6008. bool TextEdit::is_highlight_all_occurrences_enabled() const {
  6009. return highlight_all_occurrences;
  6010. }
  6011. void TextEdit::set_use_default_word_separators(bool p_enabled) {
  6012. text.set_use_default_word_separators(p_enabled);
  6013. }
  6014. bool TextEdit::is_default_word_separators_enabled() const {
  6015. return text.is_default_word_separators_enabled();
  6016. }
  6017. // Set word separators. Combine default separators with custom separators if those options are enabled.
  6018. void TextEdit::set_custom_word_separators(const String &p_separators) {
  6019. text.set_custom_word_separators(p_separators);
  6020. }
  6021. void TextEdit::Text::set_custom_word_separators(const String &p_separators) {
  6022. if (custom_word_separators == p_separators) {
  6023. return;
  6024. }
  6025. custom_word_separators = p_separators;
  6026. invalidate_all_lines();
  6027. }
  6028. bool TextEdit::is_custom_word_separators_enabled() const {
  6029. return text.is_custom_word_separators_enabled();
  6030. }
  6031. String TextEdit::get_custom_word_separators() const {
  6032. return text.get_custom_word_separators();
  6033. }
  6034. // Enable or disable custom word separators.
  6035. void TextEdit::set_use_custom_word_separators(bool p_enabled) {
  6036. text.set_use_custom_word_separators(p_enabled);
  6037. }
  6038. String TextEdit::get_default_word_separators() const {
  6039. return text.get_default_word_separators();
  6040. }
  6041. void TextEdit::set_draw_control_chars(bool p_enabled) {
  6042. if (draw_control_chars != p_enabled) {
  6043. draw_control_chars = p_enabled;
  6044. if (menu) {
  6045. menu->set_item_checked(menu->get_item_index(MENU_DISPLAY_UCC), draw_control_chars);
  6046. }
  6047. text.set_draw_control_chars(draw_control_chars);
  6048. text.invalidate_font();
  6049. _update_placeholder();
  6050. queue_accessibility_update();
  6051. queue_redraw();
  6052. }
  6053. }
  6054. bool TextEdit::get_draw_control_chars() const {
  6055. return draw_control_chars;
  6056. }
  6057. void TextEdit::set_draw_tabs(bool p_enabled) {
  6058. if (draw_tabs == p_enabled) {
  6059. return;
  6060. }
  6061. draw_tabs = p_enabled;
  6062. queue_redraw();
  6063. }
  6064. bool TextEdit::is_drawing_tabs() const {
  6065. return draw_tabs;
  6066. }
  6067. void TextEdit::set_draw_spaces(bool p_enabled) {
  6068. if (draw_spaces == p_enabled) {
  6069. return;
  6070. }
  6071. draw_spaces = p_enabled;
  6072. queue_redraw();
  6073. }
  6074. bool TextEdit::is_drawing_spaces() const {
  6075. return draw_spaces;
  6076. }
  6077. Color TextEdit::get_font_color() const {
  6078. return theme_cache.font_color;
  6079. }
  6080. void TextEdit::_bind_methods() {
  6081. /* Text */
  6082. // Text properties
  6083. ClassDB::bind_method(D_METHOD("has_ime_text"), &TextEdit::has_ime_text);
  6084. ClassDB::bind_method(D_METHOD("cancel_ime"), &TextEdit::cancel_ime);
  6085. ClassDB::bind_method(D_METHOD("apply_ime"), &TextEdit::apply_ime);
  6086. ClassDB::bind_method(D_METHOD("set_editable", "enabled"), &TextEdit::set_editable);
  6087. ClassDB::bind_method(D_METHOD("is_editable"), &TextEdit::is_editable);
  6088. ClassDB::bind_method(D_METHOD("set_text_direction", "direction"), &TextEdit::set_text_direction);
  6089. ClassDB::bind_method(D_METHOD("get_text_direction"), &TextEdit::get_text_direction);
  6090. ClassDB::bind_method(D_METHOD("set_language", "language"), &TextEdit::set_language);
  6091. ClassDB::bind_method(D_METHOD("get_language"), &TextEdit::get_language);
  6092. ClassDB::bind_method(D_METHOD("set_structured_text_bidi_override", "parser"), &TextEdit::set_structured_text_bidi_override);
  6093. ClassDB::bind_method(D_METHOD("get_structured_text_bidi_override"), &TextEdit::get_structured_text_bidi_override);
  6094. ClassDB::bind_method(D_METHOD("set_structured_text_bidi_override_options", "args"), &TextEdit::set_structured_text_bidi_override_options);
  6095. ClassDB::bind_method(D_METHOD("get_structured_text_bidi_override_options"), &TextEdit::get_structured_text_bidi_override_options);
  6096. ClassDB::bind_method(D_METHOD("set_tab_size", "size"), &TextEdit::set_tab_size);
  6097. ClassDB::bind_method(D_METHOD("get_tab_size"), &TextEdit::get_tab_size);
  6098. ClassDB::bind_method(D_METHOD("set_indent_wrapped_lines", "enabled"), &TextEdit::set_indent_wrapped_lines);
  6099. ClassDB::bind_method(D_METHOD("is_indent_wrapped_lines"), &TextEdit::is_indent_wrapped_lines);
  6100. ClassDB::bind_method(D_METHOD("set_tab_input_mode", "enabled"), &TextEdit::set_tab_input_mode);
  6101. ClassDB::bind_method(D_METHOD("get_tab_input_mode"), &TextEdit::get_tab_input_mode);
  6102. // User controls
  6103. ClassDB::bind_method(D_METHOD("set_overtype_mode_enabled", "enabled"), &TextEdit::set_overtype_mode_enabled);
  6104. ClassDB::bind_method(D_METHOD("is_overtype_mode_enabled"), &TextEdit::is_overtype_mode_enabled);
  6105. ClassDB::bind_method(D_METHOD("set_context_menu_enabled", "enabled"), &TextEdit::set_context_menu_enabled);
  6106. ClassDB::bind_method(D_METHOD("is_context_menu_enabled"), &TextEdit::is_context_menu_enabled);
  6107. ClassDB::bind_method(D_METHOD("set_emoji_menu_enabled", "enable"), &TextEdit::set_emoji_menu_enabled);
  6108. ClassDB::bind_method(D_METHOD("is_emoji_menu_enabled"), &TextEdit::is_emoji_menu_enabled);
  6109. ClassDB::bind_method(D_METHOD("set_backspace_deletes_composite_character_enabled", "enable"), &TextEdit::set_backspace_deletes_composite_character_enabled);
  6110. ClassDB::bind_method(D_METHOD("is_backspace_deletes_composite_character_enabled"), &TextEdit::is_backspace_deletes_composite_character_enabled);
  6111. ClassDB::bind_method(D_METHOD("set_shortcut_keys_enabled", "enabled"), &TextEdit::set_shortcut_keys_enabled);
  6112. ClassDB::bind_method(D_METHOD("is_shortcut_keys_enabled"), &TextEdit::is_shortcut_keys_enabled);
  6113. ClassDB::bind_method(D_METHOD("set_virtual_keyboard_enabled", "enabled"), &TextEdit::set_virtual_keyboard_enabled);
  6114. ClassDB::bind_method(D_METHOD("is_virtual_keyboard_enabled"), &TextEdit::is_virtual_keyboard_enabled);
  6115. ClassDB::bind_method(D_METHOD("set_virtual_keyboard_show_on_focus", "show_on_focus"), &TextEdit::set_virtual_keyboard_show_on_focus);
  6116. ClassDB::bind_method(D_METHOD("get_virtual_keyboard_show_on_focus"), &TextEdit::get_virtual_keyboard_show_on_focus);
  6117. ClassDB::bind_method(D_METHOD("set_middle_mouse_paste_enabled", "enabled"), &TextEdit::set_middle_mouse_paste_enabled);
  6118. ClassDB::bind_method(D_METHOD("is_middle_mouse_paste_enabled"), &TextEdit::is_middle_mouse_paste_enabled);
  6119. ClassDB::bind_method(D_METHOD("set_empty_selection_clipboard_enabled", "enabled"), &TextEdit::set_empty_selection_clipboard_enabled);
  6120. ClassDB::bind_method(D_METHOD("is_empty_selection_clipboard_enabled"), &TextEdit::is_empty_selection_clipboard_enabled);
  6121. // Text manipulation
  6122. ClassDB::bind_method(D_METHOD("clear"), &TextEdit::clear);
  6123. ClassDB::bind_method(D_METHOD("set_text", "text"), &TextEdit::set_text);
  6124. ClassDB::bind_method(D_METHOD("get_text"), &TextEdit::get_text);
  6125. ClassDB::bind_method(D_METHOD("get_line_count"), &TextEdit::get_line_count);
  6126. ClassDB::bind_method(D_METHOD("set_placeholder", "text"), &TextEdit::set_placeholder);
  6127. ClassDB::bind_method(D_METHOD("get_placeholder"), &TextEdit::get_placeholder);
  6128. ClassDB::bind_method(D_METHOD("set_line", "line", "new_text"), &TextEdit::set_line);
  6129. ClassDB::bind_method(D_METHOD("get_line", "line"), &TextEdit::get_line);
  6130. ClassDB::bind_method(D_METHOD("get_line_with_ime", "line"), &TextEdit::get_line_with_ime);
  6131. ClassDB::bind_method(D_METHOD("get_line_width", "line", "wrap_index"), &TextEdit::get_line_width, DEFVAL(-1));
  6132. ClassDB::bind_method(D_METHOD("get_line_height"), &TextEdit::get_line_height);
  6133. ClassDB::bind_method(D_METHOD("get_indent_level", "line"), &TextEdit::get_indent_level);
  6134. ClassDB::bind_method(D_METHOD("get_first_non_whitespace_column", "line"), &TextEdit::get_first_non_whitespace_column);
  6135. ClassDB::bind_method(D_METHOD("swap_lines", "from_line", "to_line"), &TextEdit::swap_lines);
  6136. ClassDB::bind_method(D_METHOD("insert_line_at", "line", "text"), &TextEdit::insert_line_at);
  6137. ClassDB::bind_method(D_METHOD("remove_line_at", "line", "move_carets_down"), &TextEdit::remove_line_at, DEFVAL(true));
  6138. ClassDB::bind_method(D_METHOD("insert_text_at_caret", "text", "caret_index"), &TextEdit::insert_text_at_caret, DEFVAL(-1));
  6139. ClassDB::bind_method(D_METHOD("insert_text", "text", "line", "column", "before_selection_begin", "before_selection_end"), &TextEdit::insert_text, DEFVAL(true), DEFVAL(false));
  6140. ClassDB::bind_method(D_METHOD("remove_text", "from_line", "from_column", "to_line", "to_column"), &TextEdit::remove_text);
  6141. ClassDB::bind_method(D_METHOD("get_last_unhidden_line"), &TextEdit::get_last_unhidden_line);
  6142. ClassDB::bind_method(D_METHOD("get_next_visible_line_offset_from", "line", "visible_amount"), &TextEdit::get_next_visible_line_offset_from);
  6143. 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);
  6144. // Overridable actions
  6145. ClassDB::bind_method(D_METHOD("backspace", "caret_index"), &TextEdit::backspace, DEFVAL(-1));
  6146. ClassDB::bind_method(D_METHOD("cut", "caret_index"), &TextEdit::cut, DEFVAL(-1));
  6147. ClassDB::bind_method(D_METHOD("copy", "caret_index"), &TextEdit::copy, DEFVAL(-1));
  6148. ClassDB::bind_method(D_METHOD("paste", "caret_index"), &TextEdit::paste, DEFVAL(-1));
  6149. ClassDB::bind_method(D_METHOD("paste_primary_clipboard", "caret_index"), &TextEdit::paste_primary_clipboard, DEFVAL(-1));
  6150. GDVIRTUAL_BIND(_handle_unicode_input, "unicode_char", "caret_index")
  6151. GDVIRTUAL_BIND(_backspace, "caret_index")
  6152. GDVIRTUAL_BIND(_cut, "caret_index")
  6153. GDVIRTUAL_BIND(_copy, "caret_index")
  6154. GDVIRTUAL_BIND(_paste, "caret_index")
  6155. GDVIRTUAL_BIND(_paste_primary_clipboard, "caret_index")
  6156. // Context Menu
  6157. BIND_ENUM_CONSTANT(MENU_CUT);
  6158. BIND_ENUM_CONSTANT(MENU_COPY);
  6159. BIND_ENUM_CONSTANT(MENU_PASTE);
  6160. BIND_ENUM_CONSTANT(MENU_CLEAR);
  6161. BIND_ENUM_CONSTANT(MENU_SELECT_ALL);
  6162. BIND_ENUM_CONSTANT(MENU_UNDO);
  6163. BIND_ENUM_CONSTANT(MENU_REDO);
  6164. BIND_ENUM_CONSTANT(MENU_SUBMENU_TEXT_DIR);
  6165. BIND_ENUM_CONSTANT(MENU_DIR_INHERITED);
  6166. BIND_ENUM_CONSTANT(MENU_DIR_AUTO);
  6167. BIND_ENUM_CONSTANT(MENU_DIR_LTR);
  6168. BIND_ENUM_CONSTANT(MENU_DIR_RTL);
  6169. BIND_ENUM_CONSTANT(MENU_DISPLAY_UCC);
  6170. BIND_ENUM_CONSTANT(MENU_SUBMENU_INSERT_UCC);
  6171. BIND_ENUM_CONSTANT(MENU_INSERT_LRM);
  6172. BIND_ENUM_CONSTANT(MENU_INSERT_RLM);
  6173. BIND_ENUM_CONSTANT(MENU_INSERT_LRE);
  6174. BIND_ENUM_CONSTANT(MENU_INSERT_RLE);
  6175. BIND_ENUM_CONSTANT(MENU_INSERT_LRO);
  6176. BIND_ENUM_CONSTANT(MENU_INSERT_RLO);
  6177. BIND_ENUM_CONSTANT(MENU_INSERT_PDF);
  6178. BIND_ENUM_CONSTANT(MENU_INSERT_ALM);
  6179. BIND_ENUM_CONSTANT(MENU_INSERT_LRI);
  6180. BIND_ENUM_CONSTANT(MENU_INSERT_RLI);
  6181. BIND_ENUM_CONSTANT(MENU_INSERT_FSI);
  6182. BIND_ENUM_CONSTANT(MENU_INSERT_PDI);
  6183. BIND_ENUM_CONSTANT(MENU_INSERT_ZWJ);
  6184. BIND_ENUM_CONSTANT(MENU_INSERT_ZWNJ);
  6185. BIND_ENUM_CONSTANT(MENU_INSERT_WJ);
  6186. BIND_ENUM_CONSTANT(MENU_INSERT_SHY);
  6187. BIND_ENUM_CONSTANT(MENU_EMOJI_AND_SYMBOL);
  6188. BIND_ENUM_CONSTANT(MENU_MAX);
  6189. /* Versioning */
  6190. BIND_ENUM_CONSTANT(ACTION_NONE);
  6191. BIND_ENUM_CONSTANT(ACTION_TYPING);
  6192. BIND_ENUM_CONSTANT(ACTION_BACKSPACE);
  6193. BIND_ENUM_CONSTANT(ACTION_DELETE);
  6194. ClassDB::bind_method(D_METHOD("start_action", "action"), &TextEdit::start_action);
  6195. ClassDB::bind_method(D_METHOD("end_action"), &TextEdit::end_complex_operation);
  6196. ClassDB::bind_method(D_METHOD("begin_complex_operation"), &TextEdit::begin_complex_operation);
  6197. ClassDB::bind_method(D_METHOD("end_complex_operation"), &TextEdit::end_complex_operation);
  6198. ClassDB::bind_method(D_METHOD("has_undo"), &TextEdit::has_undo);
  6199. ClassDB::bind_method(D_METHOD("has_redo"), &TextEdit::has_redo);
  6200. ClassDB::bind_method(D_METHOD("undo"), &TextEdit::undo);
  6201. ClassDB::bind_method(D_METHOD("redo"), &TextEdit::redo);
  6202. ClassDB::bind_method(D_METHOD("clear_undo_history"), &TextEdit::clear_undo_history);
  6203. ClassDB::bind_method(D_METHOD("tag_saved_version"), &TextEdit::tag_saved_version);
  6204. ClassDB::bind_method(D_METHOD("get_version"), &TextEdit::get_version);
  6205. ClassDB::bind_method(D_METHOD("get_saved_version"), &TextEdit::get_saved_version);
  6206. /* Search */
  6207. BIND_ENUM_CONSTANT(SEARCH_MATCH_CASE);
  6208. BIND_ENUM_CONSTANT(SEARCH_WHOLE_WORDS);
  6209. BIND_ENUM_CONSTANT(SEARCH_BACKWARDS);
  6210. ClassDB::bind_method(D_METHOD("set_search_text", "search_text"), &TextEdit::set_search_text);
  6211. ClassDB::bind_method(D_METHOD("set_search_flags", "flags"), &TextEdit::set_search_flags);
  6212. ClassDB::bind_method(D_METHOD("search", "text", "flags", "from_line", "from_column"), &TextEdit::search);
  6213. /* Tooltip */
  6214. ClassDB::bind_method(D_METHOD("set_tooltip_request_func", "callback"), &TextEdit::set_tooltip_request_func);
  6215. /* Mouse */
  6216. ClassDB::bind_method(D_METHOD("get_local_mouse_pos"), &TextEdit::get_local_mouse_pos);
  6217. ClassDB::bind_method(D_METHOD("get_word_at_pos", "position"), &TextEdit::get_word_at_pos);
  6218. ClassDB::bind_method(D_METHOD("get_line_column_at_pos", "position", "clamp_line", "clamp_column"), &TextEdit::get_line_column_at_pos, DEFVAL(true), DEFVAL(true));
  6219. ClassDB::bind_method(D_METHOD("get_pos_at_line_column", "line", "column"), &TextEdit::get_pos_at_line_column);
  6220. ClassDB::bind_method(D_METHOD("get_rect_at_line_column", "line", "column"), &TextEdit::get_rect_at_line_column);
  6221. ClassDB::bind_method(D_METHOD("get_minimap_line_at_pos", "position"), &TextEdit::get_minimap_line_at_pos);
  6222. ClassDB::bind_method(D_METHOD("is_dragging_cursor"), &TextEdit::is_dragging_cursor);
  6223. ClassDB::bind_method(D_METHOD("is_mouse_over_selection", "edges", "caret_index"), &TextEdit::is_mouse_over_selection, DEFVAL(-1));
  6224. /* Caret. */
  6225. BIND_ENUM_CONSTANT(CARET_TYPE_LINE);
  6226. BIND_ENUM_CONSTANT(CARET_TYPE_BLOCK);
  6227. ClassDB::bind_method(D_METHOD("set_caret_type", "type"), &TextEdit::set_caret_type);
  6228. ClassDB::bind_method(D_METHOD("get_caret_type"), &TextEdit::get_caret_type);
  6229. ClassDB::bind_method(D_METHOD("set_caret_blink_enabled", "enable"), &TextEdit::set_caret_blink_enabled);
  6230. ClassDB::bind_method(D_METHOD("is_caret_blink_enabled"), &TextEdit::is_caret_blink_enabled);
  6231. ClassDB::bind_method(D_METHOD("set_caret_blink_interval", "interval"), &TextEdit::set_caret_blink_interval);
  6232. ClassDB::bind_method(D_METHOD("get_caret_blink_interval"), &TextEdit::get_caret_blink_interval);
  6233. ClassDB::bind_method(D_METHOD("set_draw_caret_when_editable_disabled", "enable"), &TextEdit::set_draw_caret_when_editable_disabled);
  6234. ClassDB::bind_method(D_METHOD("is_drawing_caret_when_editable_disabled"), &TextEdit::is_drawing_caret_when_editable_disabled);
  6235. ClassDB::bind_method(D_METHOD("set_move_caret_on_right_click_enabled", "enable"), &TextEdit::set_move_caret_on_right_click_enabled);
  6236. ClassDB::bind_method(D_METHOD("is_move_caret_on_right_click_enabled"), &TextEdit::is_move_caret_on_right_click_enabled);
  6237. ClassDB::bind_method(D_METHOD("set_caret_mid_grapheme_enabled", "enabled"), &TextEdit::set_caret_mid_grapheme_enabled);
  6238. ClassDB::bind_method(D_METHOD("is_caret_mid_grapheme_enabled"), &TextEdit::is_caret_mid_grapheme_enabled);
  6239. ClassDB::bind_method(D_METHOD("set_multiple_carets_enabled", "enabled"), &TextEdit::set_multiple_carets_enabled);
  6240. ClassDB::bind_method(D_METHOD("is_multiple_carets_enabled"), &TextEdit::is_multiple_carets_enabled);
  6241. ClassDB::bind_method(D_METHOD("add_caret", "line", "column"), &TextEdit::add_caret);
  6242. ClassDB::bind_method(D_METHOD("remove_caret", "caret"), &TextEdit::remove_caret);
  6243. ClassDB::bind_method(D_METHOD("remove_secondary_carets"), &TextEdit::remove_secondary_carets);
  6244. ClassDB::bind_method(D_METHOD("get_caret_count"), &TextEdit::get_caret_count);
  6245. ClassDB::bind_method(D_METHOD("add_caret_at_carets", "below"), &TextEdit::add_caret_at_carets);
  6246. ClassDB::bind_method(D_METHOD("get_sorted_carets", "include_ignored_carets"), &TextEdit::get_sorted_carets, DEFVAL(false));
  6247. ClassDB::bind_method(D_METHOD("collapse_carets", "from_line", "from_column", "to_line", "to_column", "inclusive"), &TextEdit::collapse_carets, DEFVAL(false));
  6248. ClassDB::bind_method(D_METHOD("merge_overlapping_carets"), &TextEdit::merge_overlapping_carets);
  6249. ClassDB::bind_method(D_METHOD("begin_multicaret_edit"), &TextEdit::begin_multicaret_edit);
  6250. ClassDB::bind_method(D_METHOD("end_multicaret_edit"), &TextEdit::end_multicaret_edit);
  6251. ClassDB::bind_method(D_METHOD("is_in_mulitcaret_edit"), &TextEdit::is_in_mulitcaret_edit);
  6252. ClassDB::bind_method(D_METHOD("multicaret_edit_ignore_caret", "caret_index"), &TextEdit::multicaret_edit_ignore_caret);
  6253. ClassDB::bind_method(D_METHOD("is_caret_visible", "caret_index"), &TextEdit::is_caret_visible, DEFVAL(0));
  6254. ClassDB::bind_method(D_METHOD("get_caret_draw_pos", "caret_index"), &TextEdit::get_caret_draw_pos, DEFVAL(0));
  6255. 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));
  6256. ClassDB::bind_method(D_METHOD("get_caret_line", "caret_index"), &TextEdit::get_caret_line, DEFVAL(0));
  6257. ClassDB::bind_method(D_METHOD("set_caret_column", "column", "adjust_viewport", "caret_index"), &TextEdit::set_caret_column, DEFVAL(true), DEFVAL(0));
  6258. ClassDB::bind_method(D_METHOD("get_caret_column", "caret_index"), &TextEdit::get_caret_column, DEFVAL(0));
  6259. ClassDB::bind_method(D_METHOD("get_next_composite_character_column", "line", "column"), &TextEdit::get_next_composite_character_column);
  6260. ClassDB::bind_method(D_METHOD("get_previous_composite_character_column", "line", "column"), &TextEdit::get_previous_composite_character_column);
  6261. ClassDB::bind_method(D_METHOD("get_caret_wrap_index", "caret_index"), &TextEdit::get_caret_wrap_index, DEFVAL(0));
  6262. ClassDB::bind_method(D_METHOD("get_word_under_caret", "caret_index"), &TextEdit::get_word_under_caret, DEFVAL(-1));
  6263. ClassDB::bind_method(D_METHOD("set_use_default_word_separators", "enabled"), &TextEdit::set_use_default_word_separators);
  6264. ClassDB::bind_method(D_METHOD("is_default_word_separators_enabled"), &TextEdit::is_default_word_separators_enabled);
  6265. ClassDB::bind_method(D_METHOD("set_use_custom_word_separators", "enabled"), &TextEdit::set_use_custom_word_separators);
  6266. ClassDB::bind_method(D_METHOD("is_custom_word_separators_enabled"), &TextEdit::is_custom_word_separators_enabled);
  6267. ClassDB::bind_method(D_METHOD("set_custom_word_separators", "custom_word_separators"), &TextEdit::set_custom_word_separators);
  6268. ClassDB::bind_method(D_METHOD("get_custom_word_separators"), &TextEdit::get_custom_word_separators);
  6269. /* Selection. */
  6270. BIND_ENUM_CONSTANT(SELECTION_MODE_NONE);
  6271. BIND_ENUM_CONSTANT(SELECTION_MODE_SHIFT);
  6272. BIND_ENUM_CONSTANT(SELECTION_MODE_POINTER);
  6273. BIND_ENUM_CONSTANT(SELECTION_MODE_WORD);
  6274. BIND_ENUM_CONSTANT(SELECTION_MODE_LINE);
  6275. ClassDB::bind_method(D_METHOD("set_selecting_enabled", "enable"), &TextEdit::set_selecting_enabled);
  6276. ClassDB::bind_method(D_METHOD("is_selecting_enabled"), &TextEdit::is_selecting_enabled);
  6277. ClassDB::bind_method(D_METHOD("set_deselect_on_focus_loss_enabled", "enable"), &TextEdit::set_deselect_on_focus_loss_enabled);
  6278. ClassDB::bind_method(D_METHOD("is_deselect_on_focus_loss_enabled"), &TextEdit::is_deselect_on_focus_loss_enabled);
  6279. ClassDB::bind_method(D_METHOD("set_drag_and_drop_selection_enabled", "enable"), &TextEdit::set_drag_and_drop_selection_enabled);
  6280. ClassDB::bind_method(D_METHOD("is_drag_and_drop_selection_enabled"), &TextEdit::is_drag_and_drop_selection_enabled);
  6281. ClassDB::bind_method(D_METHOD("set_selection_mode", "mode"), &TextEdit::set_selection_mode);
  6282. ClassDB::bind_method(D_METHOD("get_selection_mode"), &TextEdit::get_selection_mode);
  6283. ClassDB::bind_method(D_METHOD("select_all"), &TextEdit::select_all);
  6284. ClassDB::bind_method(D_METHOD("select_word_under_caret", "caret_index"), &TextEdit::select_word_under_caret, DEFVAL(-1));
  6285. ClassDB::bind_method(D_METHOD("add_selection_for_next_occurrence"), &TextEdit::add_selection_for_next_occurrence);
  6286. ClassDB::bind_method(D_METHOD("skip_selection_for_next_occurrence"), &TextEdit::skip_selection_for_next_occurrence);
  6287. ClassDB::bind_method(D_METHOD("select", "origin_line", "origin_column", "caret_line", "caret_column", "caret_index"), &TextEdit::select, DEFVAL(0));
  6288. ClassDB::bind_method(D_METHOD("has_selection", "caret_index"), &TextEdit::has_selection, DEFVAL(-1));
  6289. ClassDB::bind_method(D_METHOD("get_selected_text", "caret_index"), &TextEdit::get_selected_text, DEFVAL(-1));
  6290. ClassDB::bind_method(D_METHOD("get_selection_at_line_column", "line", "column", "include_edges", "only_selections"), &TextEdit::get_selection_at_line_column, DEFVAL(true), DEFVAL(true));
  6291. ClassDB::bind_method(D_METHOD("get_line_ranges_from_carets", "only_selections", "merge_adjacent"), &TextEdit::get_line_ranges_from_carets_typed_array, DEFVAL(false), DEFVAL(true));
  6292. ClassDB::bind_method(D_METHOD("get_selection_origin_line", "caret_index"), &TextEdit::get_selection_origin_line, DEFVAL(0));
  6293. ClassDB::bind_method(D_METHOD("get_selection_origin_column", "caret_index"), &TextEdit::get_selection_origin_column, DEFVAL(0));
  6294. ClassDB::bind_method(D_METHOD("set_selection_origin_line", "line", "can_be_hidden", "wrap_index", "caret_index"), &TextEdit::set_selection_origin_line, DEFVAL(true), DEFVAL(-1), DEFVAL(0));
  6295. ClassDB::bind_method(D_METHOD("set_selection_origin_column", "column", "caret_index"), &TextEdit::set_selection_origin_column, DEFVAL(0));
  6296. ClassDB::bind_method(D_METHOD("get_selection_from_line", "caret_index"), &TextEdit::get_selection_from_line, DEFVAL(0));
  6297. ClassDB::bind_method(D_METHOD("get_selection_from_column", "caret_index"), &TextEdit::get_selection_from_column, DEFVAL(0));
  6298. ClassDB::bind_method(D_METHOD("get_selection_to_line", "caret_index"), &TextEdit::get_selection_to_line, DEFVAL(0));
  6299. ClassDB::bind_method(D_METHOD("get_selection_to_column", "caret_index"), &TextEdit::get_selection_to_column, DEFVAL(0));
  6300. ClassDB::bind_method(D_METHOD("is_caret_after_selection_origin", "caret_index"), &TextEdit::is_caret_after_selection_origin, DEFVAL(0));
  6301. ClassDB::bind_method(D_METHOD("deselect", "caret_index"), &TextEdit::deselect, DEFVAL(-1));
  6302. ClassDB::bind_method(D_METHOD("delete_selection", "caret_index"), &TextEdit::delete_selection, DEFVAL(-1));
  6303. /* Line wrapping. */
  6304. BIND_ENUM_CONSTANT(LINE_WRAPPING_NONE);
  6305. BIND_ENUM_CONSTANT(LINE_WRAPPING_BOUNDARY);
  6306. ClassDB::bind_method(D_METHOD("set_line_wrapping_mode", "mode"), &TextEdit::set_line_wrapping_mode);
  6307. ClassDB::bind_method(D_METHOD("get_line_wrapping_mode"), &TextEdit::get_line_wrapping_mode);
  6308. ClassDB::bind_method(D_METHOD("set_autowrap_mode", "autowrap_mode"), &TextEdit::set_autowrap_mode);
  6309. ClassDB::bind_method(D_METHOD("get_autowrap_mode"), &TextEdit::get_autowrap_mode);
  6310. ClassDB::bind_method(D_METHOD("is_line_wrapped", "line"), &TextEdit::is_line_wrapped);
  6311. ClassDB::bind_method(D_METHOD("get_line_wrap_count", "line"), &TextEdit::get_line_wrap_count);
  6312. ClassDB::bind_method(D_METHOD("get_line_wrap_index_at_column", "line", "column"), &TextEdit::get_line_wrap_index_at_column);
  6313. ClassDB::bind_method(D_METHOD("get_line_wrapped_text", "line"), &TextEdit::get_line_wrapped_text);
  6314. /* Viewport. */
  6315. // Scrolling.
  6316. ClassDB::bind_method(D_METHOD("set_smooth_scroll_enabled", "enable"), &TextEdit::set_smooth_scroll_enabled);
  6317. ClassDB::bind_method(D_METHOD("is_smooth_scroll_enabled"), &TextEdit::is_smooth_scroll_enabled);
  6318. ClassDB::bind_method(D_METHOD("get_v_scroll_bar"), &TextEdit::get_v_scroll_bar);
  6319. ClassDB::bind_method(D_METHOD("get_h_scroll_bar"), &TextEdit::get_h_scroll_bar);
  6320. ClassDB::bind_method(D_METHOD("set_v_scroll", "value"), &TextEdit::set_v_scroll);
  6321. ClassDB::bind_method(D_METHOD("get_v_scroll"), &TextEdit::get_v_scroll);
  6322. ClassDB::bind_method(D_METHOD("set_h_scroll", "value"), &TextEdit::set_h_scroll);
  6323. ClassDB::bind_method(D_METHOD("get_h_scroll"), &TextEdit::get_h_scroll);
  6324. ClassDB::bind_method(D_METHOD("set_scroll_past_end_of_file_enabled", "enable"), &TextEdit::set_scroll_past_end_of_file_enabled);
  6325. ClassDB::bind_method(D_METHOD("is_scroll_past_end_of_file_enabled"), &TextEdit::is_scroll_past_end_of_file_enabled);
  6326. ClassDB::bind_method(D_METHOD("set_v_scroll_speed", "speed"), &TextEdit::set_v_scroll_speed);
  6327. ClassDB::bind_method(D_METHOD("get_v_scroll_speed"), &TextEdit::get_v_scroll_speed);
  6328. ClassDB::bind_method(D_METHOD("set_fit_content_height_enabled", "enabled"), &TextEdit::set_fit_content_height_enabled);
  6329. ClassDB::bind_method(D_METHOD("is_fit_content_height_enabled"), &TextEdit::is_fit_content_height_enabled);
  6330. ClassDB::bind_method(D_METHOD("set_fit_content_width_enabled", "enabled"), &TextEdit::set_fit_content_width_enabled);
  6331. ClassDB::bind_method(D_METHOD("is_fit_content_width_enabled"), &TextEdit::is_fit_content_width_enabled);
  6332. ClassDB::bind_method(D_METHOD("get_scroll_pos_for_line", "line", "wrap_index"), &TextEdit::get_scroll_pos_for_line, DEFVAL(0));
  6333. // Visible lines.
  6334. ClassDB::bind_method(D_METHOD("set_line_as_first_visible", "line", "wrap_index"), &TextEdit::set_line_as_first_visible, DEFVAL(0));
  6335. ClassDB::bind_method(D_METHOD("get_first_visible_line"), &TextEdit::get_first_visible_line);
  6336. ClassDB::bind_method(D_METHOD("set_line_as_center_visible", "line", "wrap_index"), &TextEdit::set_line_as_center_visible, DEFVAL(0));
  6337. ClassDB::bind_method(D_METHOD("set_line_as_last_visible", "line", "wrap_index"), &TextEdit::set_line_as_last_visible, DEFVAL(0));
  6338. ClassDB::bind_method(D_METHOD("get_last_full_visible_line"), &TextEdit::get_last_full_visible_line);
  6339. ClassDB::bind_method(D_METHOD("get_last_full_visible_line_wrap_index"), &TextEdit::get_last_full_visible_line_wrap_index);
  6340. ClassDB::bind_method(D_METHOD("get_visible_line_count"), &TextEdit::get_visible_line_count);
  6341. ClassDB::bind_method(D_METHOD("get_visible_line_count_in_range", "from_line", "to_line"), &TextEdit::get_visible_line_count_in_range);
  6342. ClassDB::bind_method(D_METHOD("get_total_visible_line_count"), &TextEdit::get_total_visible_line_count);
  6343. // Auto adjust
  6344. ClassDB::bind_method(D_METHOD("adjust_viewport_to_caret", "caret_index"), &TextEdit::adjust_viewport_to_caret, DEFVAL(0));
  6345. ClassDB::bind_method(D_METHOD("center_viewport_to_caret", "caret_index"), &TextEdit::center_viewport_to_caret, DEFVAL(0));
  6346. // Minimap
  6347. ClassDB::bind_method(D_METHOD("set_draw_minimap", "enabled"), &TextEdit::set_draw_minimap);
  6348. ClassDB::bind_method(D_METHOD("is_drawing_minimap"), &TextEdit::is_drawing_minimap);
  6349. ClassDB::bind_method(D_METHOD("set_minimap_width", "width"), &TextEdit::set_minimap_width);
  6350. ClassDB::bind_method(D_METHOD("get_minimap_width"), &TextEdit::get_minimap_width);
  6351. ClassDB::bind_method(D_METHOD("get_minimap_visible_lines"), &TextEdit::get_minimap_visible_lines);
  6352. /* Gutters. */
  6353. BIND_ENUM_CONSTANT(GUTTER_TYPE_STRING);
  6354. BIND_ENUM_CONSTANT(GUTTER_TYPE_ICON);
  6355. BIND_ENUM_CONSTANT(GUTTER_TYPE_CUSTOM);
  6356. ClassDB::bind_method(D_METHOD("add_gutter", "at"), &TextEdit::add_gutter, DEFVAL(-1));
  6357. ClassDB::bind_method(D_METHOD("remove_gutter", "gutter"), &TextEdit::remove_gutter);
  6358. ClassDB::bind_method(D_METHOD("get_gutter_count"), &TextEdit::get_gutter_count);
  6359. ClassDB::bind_method(D_METHOD("set_gutter_name", "gutter", "name"), &TextEdit::set_gutter_name);
  6360. ClassDB::bind_method(D_METHOD("get_gutter_name", "gutter"), &TextEdit::get_gutter_name);
  6361. ClassDB::bind_method(D_METHOD("set_gutter_type", "gutter", "type"), &TextEdit::set_gutter_type);
  6362. ClassDB::bind_method(D_METHOD("get_gutter_type", "gutter"), &TextEdit::get_gutter_type);
  6363. ClassDB::bind_method(D_METHOD("set_gutter_width", "gutter", "width"), &TextEdit::set_gutter_width);
  6364. ClassDB::bind_method(D_METHOD("get_gutter_width", "gutter"), &TextEdit::get_gutter_width);
  6365. ClassDB::bind_method(D_METHOD("set_gutter_draw", "gutter", "draw"), &TextEdit::set_gutter_draw);
  6366. ClassDB::bind_method(D_METHOD("is_gutter_drawn", "gutter"), &TextEdit::is_gutter_drawn);
  6367. ClassDB::bind_method(D_METHOD("set_gutter_clickable", "gutter", "clickable"), &TextEdit::set_gutter_clickable);
  6368. ClassDB::bind_method(D_METHOD("is_gutter_clickable", "gutter"), &TextEdit::is_gutter_clickable);
  6369. ClassDB::bind_method(D_METHOD("set_gutter_overwritable", "gutter", "overwritable"), &TextEdit::set_gutter_overwritable);
  6370. ClassDB::bind_method(D_METHOD("is_gutter_overwritable", "gutter"), &TextEdit::is_gutter_overwritable);
  6371. ClassDB::bind_method(D_METHOD("merge_gutters", "from_line", "to_line"), &TextEdit::merge_gutters);
  6372. ClassDB::bind_method(D_METHOD("set_gutter_custom_draw", "column", "draw_callback"), &TextEdit::set_gutter_custom_draw);
  6373. ClassDB::bind_method(D_METHOD("get_total_gutter_width"), &TextEdit::get_total_gutter_width);
  6374. // Line gutters.
  6375. ClassDB::bind_method(D_METHOD("set_line_gutter_metadata", "line", "gutter", "metadata"), &TextEdit::set_line_gutter_metadata);
  6376. ClassDB::bind_method(D_METHOD("get_line_gutter_metadata", "line", "gutter"), &TextEdit::get_line_gutter_metadata);
  6377. ClassDB::bind_method(D_METHOD("set_line_gutter_text", "line", "gutter", "text"), &TextEdit::set_line_gutter_text);
  6378. ClassDB::bind_method(D_METHOD("get_line_gutter_text", "line", "gutter"), &TextEdit::get_line_gutter_text);
  6379. ClassDB::bind_method(D_METHOD("set_line_gutter_icon", "line", "gutter", "icon"), &TextEdit::set_line_gutter_icon);
  6380. ClassDB::bind_method(D_METHOD("get_line_gutter_icon", "line", "gutter"), &TextEdit::get_line_gutter_icon);
  6381. ClassDB::bind_method(D_METHOD("set_line_gutter_item_color", "line", "gutter", "color"), &TextEdit::set_line_gutter_item_color);
  6382. ClassDB::bind_method(D_METHOD("get_line_gutter_item_color", "line", "gutter"), &TextEdit::get_line_gutter_item_color);
  6383. ClassDB::bind_method(D_METHOD("set_line_gutter_clickable", "line", "gutter", "clickable"), &TextEdit::set_line_gutter_clickable);
  6384. ClassDB::bind_method(D_METHOD("is_line_gutter_clickable", "line", "gutter"), &TextEdit::is_line_gutter_clickable);
  6385. // Line style
  6386. ClassDB::bind_method(D_METHOD("set_line_background_color", "line", "color"), &TextEdit::set_line_background_color);
  6387. ClassDB::bind_method(D_METHOD("get_line_background_color", "line"), &TextEdit::get_line_background_color);
  6388. /* Syntax Highlighting. */
  6389. ClassDB::bind_method(D_METHOD("set_syntax_highlighter", "syntax_highlighter"), &TextEdit::set_syntax_highlighter);
  6390. ClassDB::bind_method(D_METHOD("get_syntax_highlighter"), &TextEdit::get_syntax_highlighter);
  6391. /* Visual. */
  6392. ClassDB::bind_method(D_METHOD("set_highlight_current_line", "enabled"), &TextEdit::set_highlight_current_line);
  6393. ClassDB::bind_method(D_METHOD("is_highlight_current_line_enabled"), &TextEdit::is_highlight_current_line_enabled);
  6394. ClassDB::bind_method(D_METHOD("set_highlight_all_occurrences", "enabled"), &TextEdit::set_highlight_all_occurrences);
  6395. ClassDB::bind_method(D_METHOD("is_highlight_all_occurrences_enabled"), &TextEdit::is_highlight_all_occurrences_enabled);
  6396. ClassDB::bind_method(D_METHOD("get_draw_control_chars"), &TextEdit::get_draw_control_chars);
  6397. ClassDB::bind_method(D_METHOD("set_draw_control_chars", "enabled"), &TextEdit::set_draw_control_chars);
  6398. ClassDB::bind_method(D_METHOD("set_draw_tabs", "enabled"), &TextEdit::set_draw_tabs);
  6399. ClassDB::bind_method(D_METHOD("is_drawing_tabs"), &TextEdit::is_drawing_tabs);
  6400. ClassDB::bind_method(D_METHOD("set_draw_spaces", "enabled"), &TextEdit::set_draw_spaces);
  6401. ClassDB::bind_method(D_METHOD("is_drawing_spaces"), &TextEdit::is_drawing_spaces);
  6402. ClassDB::bind_method(D_METHOD("get_menu"), &TextEdit::get_menu);
  6403. ClassDB::bind_method(D_METHOD("is_menu_visible"), &TextEdit::is_menu_visible);
  6404. ClassDB::bind_method(D_METHOD("menu_option", "option"), &TextEdit::menu_option);
  6405. /* Deprecated */
  6406. #ifndef DISABLE_DEPRECATED
  6407. ClassDB::bind_method(D_METHOD("adjust_carets_after_edit", "caret", "from_line", "from_col", "to_line", "to_col"), &TextEdit::adjust_carets_after_edit);
  6408. ClassDB::bind_method(D_METHOD("get_caret_index_edit_order"), &TextEdit::get_caret_index_edit_order);
  6409. ClassDB::bind_method(D_METHOD("get_selection_line", "caret_index"), &TextEdit::get_selection_line, DEFVAL(0));
  6410. ClassDB::bind_method(D_METHOD("get_selection_column", "caret_index"), &TextEdit::get_selection_column, DEFVAL(0));
  6411. #endif
  6412. /* Inspector */
  6413. ADD_PROPERTY(PropertyInfo(Variant::STRING, "text", PROPERTY_HINT_MULTILINE_TEXT), "set_text", "get_text");
  6414. ADD_PROPERTY(PropertyInfo(Variant::STRING, "placeholder_text", PROPERTY_HINT_MULTILINE_TEXT), "set_placeholder", "get_placeholder");
  6415. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "editable"), "set_editable", "is_editable");
  6416. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "context_menu_enabled"), "set_context_menu_enabled", "is_context_menu_enabled");
  6417. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "emoji_menu_enabled"), "set_emoji_menu_enabled", "is_emoji_menu_enabled");
  6418. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "backspace_deletes_composite_character_enabled"), "set_backspace_deletes_composite_character_enabled", "is_backspace_deletes_composite_character_enabled");
  6419. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "shortcut_keys_enabled"), "set_shortcut_keys_enabled", "is_shortcut_keys_enabled");
  6420. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "selecting_enabled"), "set_selecting_enabled", "is_selecting_enabled");
  6421. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "deselect_on_focus_loss_enabled"), "set_deselect_on_focus_loss_enabled", "is_deselect_on_focus_loss_enabled");
  6422. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "drag_and_drop_selection_enabled"), "set_drag_and_drop_selection_enabled", "is_drag_and_drop_selection_enabled");
  6423. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "virtual_keyboard_enabled"), "set_virtual_keyboard_enabled", "is_virtual_keyboard_enabled");
  6424. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "virtual_keyboard_show_on_focus"), "set_virtual_keyboard_show_on_focus", "get_virtual_keyboard_show_on_focus");
  6425. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "middle_mouse_paste_enabled"), "set_middle_mouse_paste_enabled", "is_middle_mouse_paste_enabled");
  6426. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "empty_selection_clipboard_enabled"), "set_empty_selection_clipboard_enabled", "is_empty_selection_clipboard_enabled");
  6427. ADD_PROPERTY(PropertyInfo(Variant::INT, "wrap_mode", PROPERTY_HINT_ENUM, "None,Boundary"), "set_line_wrapping_mode", "get_line_wrapping_mode");
  6428. ADD_PROPERTY(PropertyInfo(Variant::INT, "autowrap_mode", PROPERTY_HINT_ENUM, "Arbitrary:1,Word:2,Word (Smart):3"), "set_autowrap_mode", "get_autowrap_mode");
  6429. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "indent_wrapped_lines"), "set_indent_wrapped_lines", "is_indent_wrapped_lines");
  6430. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "tab_input_mode"), "set_tab_input_mode", "get_tab_input_mode");
  6431. ADD_GROUP("Scroll", "scroll_");
  6432. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "scroll_smooth"), "set_smooth_scroll_enabled", "is_smooth_scroll_enabled");
  6433. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "scroll_v_scroll_speed", PROPERTY_HINT_NONE, "suffix:lines/s"), "set_v_scroll_speed", "get_v_scroll_speed");
  6434. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "scroll_past_end_of_file"), "set_scroll_past_end_of_file_enabled", "is_scroll_past_end_of_file_enabled");
  6435. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "scroll_vertical", PROPERTY_HINT_NONE, "suffix:lines"), "set_v_scroll", "get_v_scroll");
  6436. ADD_PROPERTY(PropertyInfo(Variant::INT, "scroll_horizontal", PROPERTY_HINT_NONE, "suffix:px"), "set_h_scroll", "get_h_scroll");
  6437. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "scroll_fit_content_height"), "set_fit_content_height_enabled", "is_fit_content_height_enabled");
  6438. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "scroll_fit_content_width"), "set_fit_content_width_enabled", "is_fit_content_width_enabled");
  6439. ADD_GROUP("Minimap", "minimap_");
  6440. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "minimap_draw"), "set_draw_minimap", "is_drawing_minimap");
  6441. ADD_PROPERTY(PropertyInfo(Variant::INT, "minimap_width", PROPERTY_HINT_NONE, "suffix:px"), "set_minimap_width", "get_minimap_width");
  6442. ADD_GROUP("Caret", "caret_");
  6443. ADD_PROPERTY(PropertyInfo(Variant::INT, "caret_type", PROPERTY_HINT_ENUM, "Line,Block"), "set_caret_type", "get_caret_type");
  6444. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "caret_blink"), "set_caret_blink_enabled", "is_caret_blink_enabled");
  6445. 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");
  6446. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "caret_draw_when_editable_disabled"), "set_draw_caret_when_editable_disabled", "is_drawing_caret_when_editable_disabled");
  6447. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "caret_move_on_right_click"), "set_move_caret_on_right_click_enabled", "is_move_caret_on_right_click_enabled");
  6448. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "caret_mid_grapheme"), "set_caret_mid_grapheme_enabled", "is_caret_mid_grapheme_enabled");
  6449. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "caret_multiple"), "set_multiple_carets_enabled", "is_multiple_carets_enabled");
  6450. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "use_default_word_separators"), "set_use_default_word_separators", "is_default_word_separators_enabled");
  6451. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "use_custom_word_separators"), "set_use_custom_word_separators", "is_custom_word_separators_enabled");
  6452. ADD_PROPERTY(PropertyInfo(Variant::STRING, "custom_word_separators"), "set_custom_word_separators", "get_custom_word_separators");
  6453. ADD_GROUP("Highlighting", "");
  6454. 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");
  6455. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "highlight_all_occurrences"), "set_highlight_all_occurrences", "is_highlight_all_occurrences_enabled");
  6456. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "highlight_current_line"), "set_highlight_current_line", "is_highlight_current_line_enabled");
  6457. ADD_GROUP("Visual Whitespace", "draw_");
  6458. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "draw_control_chars"), "set_draw_control_chars", "get_draw_control_chars");
  6459. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "draw_tabs"), "set_draw_tabs", "is_drawing_tabs");
  6460. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "draw_spaces"), "set_draw_spaces", "is_drawing_spaces");
  6461. ADD_GROUP("BiDi", "");
  6462. ADD_PROPERTY(PropertyInfo(Variant::INT, "text_direction", PROPERTY_HINT_ENUM, "Auto,Left-to-Right,Right-to-Left,Inherited"), "set_text_direction", "get_text_direction");
  6463. ADD_PROPERTY(PropertyInfo(Variant::STRING, "language", PROPERTY_HINT_LOCALE_ID, ""), "set_language", "get_language");
  6464. 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");
  6465. ADD_PROPERTY(PropertyInfo(Variant::ARRAY, "structured_text_bidi_override_options"), "set_structured_text_bidi_override_options", "get_structured_text_bidi_override_options");
  6466. /* Signals */
  6467. /* Core. */
  6468. ADD_SIGNAL(MethodInfo("text_set"));
  6469. ADD_SIGNAL(MethodInfo("text_changed"));
  6470. ADD_SIGNAL(MethodInfo("lines_edited_from", PropertyInfo(Variant::INT, "from_line"), PropertyInfo(Variant::INT, "to_line")));
  6471. /* Caret. */
  6472. ADD_SIGNAL(MethodInfo("caret_changed"));
  6473. /* Gutters. */
  6474. ADD_SIGNAL(MethodInfo("gutter_clicked", PropertyInfo(Variant::INT, "line"), PropertyInfo(Variant::INT, "gutter")));
  6475. ADD_SIGNAL(MethodInfo("gutter_added"));
  6476. ADD_SIGNAL(MethodInfo("gutter_removed"));
  6477. // Theme items
  6478. /* Search */
  6479. BIND_THEME_ITEM(Theme::DATA_TYPE_COLOR, TextEdit, search_result_color);
  6480. BIND_THEME_ITEM(Theme::DATA_TYPE_COLOR, TextEdit, search_result_border_color);
  6481. /* Caret */
  6482. BIND_THEME_ITEM(Theme::DATA_TYPE_CONSTANT, TextEdit, caret_width);
  6483. BIND_THEME_ITEM(Theme::DATA_TYPE_COLOR, TextEdit, caret_color);
  6484. BIND_THEME_ITEM(Theme::DATA_TYPE_COLOR, TextEdit, caret_background_color);
  6485. /* Selection */
  6486. BIND_THEME_ITEM(Theme::DATA_TYPE_COLOR, TextEdit, font_selected_color);
  6487. BIND_THEME_ITEM(Theme::DATA_TYPE_COLOR, TextEdit, selection_color);
  6488. /* Other visuals */
  6489. BIND_THEME_ITEM_CUSTOM(Theme::DATA_TYPE_STYLEBOX, TextEdit, style_normal, "normal");
  6490. BIND_THEME_ITEM_CUSTOM(Theme::DATA_TYPE_STYLEBOX, TextEdit, style_focus, "focus");
  6491. BIND_THEME_ITEM_CUSTOM(Theme::DATA_TYPE_STYLEBOX, TextEdit, style_readonly, "read_only");
  6492. BIND_THEME_ITEM_CUSTOM(Theme::DATA_TYPE_ICON, TextEdit, tab_icon, "tab");
  6493. BIND_THEME_ITEM_CUSTOM(Theme::DATA_TYPE_ICON, TextEdit, space_icon, "space");
  6494. BIND_THEME_ITEM(Theme::DATA_TYPE_FONT, TextEdit, font);
  6495. BIND_THEME_ITEM(Theme::DATA_TYPE_FONT_SIZE, TextEdit, font_size);
  6496. BIND_THEME_ITEM(Theme::DATA_TYPE_COLOR, TextEdit, font_color);
  6497. BIND_THEME_ITEM(Theme::DATA_TYPE_COLOR, TextEdit, font_readonly_color);
  6498. BIND_THEME_ITEM(Theme::DATA_TYPE_COLOR, TextEdit, font_placeholder_color);
  6499. BIND_THEME_ITEM(Theme::DATA_TYPE_CONSTANT, TextEdit, outline_size);
  6500. BIND_THEME_ITEM_CUSTOM(Theme::DATA_TYPE_COLOR, TextEdit, outline_color, "font_outline_color");
  6501. BIND_THEME_ITEM(Theme::DATA_TYPE_CONSTANT, TextEdit, line_spacing);
  6502. BIND_THEME_ITEM(Theme::DATA_TYPE_COLOR, TextEdit, background_color);
  6503. BIND_THEME_ITEM(Theme::DATA_TYPE_COLOR, TextEdit, current_line_color);
  6504. BIND_THEME_ITEM(Theme::DATA_TYPE_COLOR, TextEdit, word_highlighted_color);
  6505. /* Settings. */
  6506. GLOBAL_DEF(PropertyInfo(Variant::FLOAT, "gui/timers/text_edit_idle_detect_sec", PROPERTY_HINT_RANGE, "0,10,0.01,or_greater"), 3);
  6507. GLOBAL_DEF(PropertyInfo(Variant::INT, "gui/common/text_edit_undo_stack_max_size", PROPERTY_HINT_RANGE, "0,10000,1,or_greater"), 1024);
  6508. /* Dependencies */
  6509. ADD_CLASS_DEPENDENCY("HScrollBar");
  6510. ADD_CLASS_DEPENDENCY("PopupMenu");
  6511. ADD_CLASS_DEPENDENCY("Timer");
  6512. ADD_CLASS_DEPENDENCY("VScrollBar");
  6513. }
  6514. /* Internal API for CodeEdit. */
  6515. // Line hiding.
  6516. void TextEdit::_set_hiding_enabled(bool p_enabled) {
  6517. if (hiding_enabled == p_enabled) {
  6518. return;
  6519. }
  6520. if (!p_enabled) {
  6521. _unhide_all_lines();
  6522. }
  6523. hiding_enabled = p_enabled;
  6524. queue_accessibility_update();
  6525. queue_redraw();
  6526. }
  6527. bool TextEdit::_is_hiding_enabled() const {
  6528. return hiding_enabled;
  6529. }
  6530. bool TextEdit::_is_line_hidden(int p_line) const {
  6531. ERR_FAIL_INDEX_V(p_line, text.size(), false);
  6532. return text.is_hidden(p_line);
  6533. }
  6534. void TextEdit::_unhide_all_lines() {
  6535. for (int i = 0; i < text.size(); i++) {
  6536. text.set_hidden(i, false);
  6537. }
  6538. _update_scrollbars();
  6539. queue_accessibility_update();
  6540. queue_redraw();
  6541. }
  6542. void TextEdit::_unhide_carets() {
  6543. // Override for functionality.
  6544. }
  6545. void TextEdit::_set_line_as_hidden(int p_line, bool p_hidden) {
  6546. ERR_FAIL_INDEX(p_line, text.size());
  6547. if (text.is_hidden(p_line) == p_hidden) {
  6548. return;
  6549. }
  6550. if (_is_hiding_enabled() || !p_hidden) {
  6551. text.set_hidden(p_line, p_hidden);
  6552. }
  6553. queue_accessibility_update();
  6554. queue_redraw();
  6555. }
  6556. // Symbol lookup.
  6557. void TextEdit::_set_symbol_lookup_word(const String &p_symbol) {
  6558. if (lookup_symbol_word == p_symbol) {
  6559. return;
  6560. }
  6561. lookup_symbol_word = p_symbol;
  6562. queue_redraw();
  6563. }
  6564. /* Text manipulation */
  6565. // Overridable actions
  6566. void TextEdit::_handle_unicode_input_internal(const uint32_t p_unicode, int p_caret) {
  6567. ERR_FAIL_COND(p_caret >= get_caret_count() || p_caret < -1);
  6568. if (!editable) {
  6569. return;
  6570. }
  6571. start_action(EditAction::ACTION_TYPING);
  6572. begin_multicaret_edit();
  6573. for (int i = 0; i < get_caret_count(); i++) {
  6574. if (p_caret == -1 && multicaret_edit_ignore_caret(i)) {
  6575. continue;
  6576. }
  6577. if (p_caret != -1 && p_caret != i) {
  6578. continue;
  6579. }
  6580. // Remove the old character if in insert mode and no selection.
  6581. if (overtype_mode && !has_selection(i)) {
  6582. // Make sure we don't try and remove empty space.
  6583. int cl = get_caret_line(i);
  6584. int cc = get_caret_column(i);
  6585. if (cc < get_line(cl).length()) {
  6586. _remove_text(cl, cc, cl, cc + 1);
  6587. }
  6588. }
  6589. const char32_t chr[2] = { (char32_t)p_unicode, 0 };
  6590. insert_text_at_caret(chr, i);
  6591. }
  6592. end_multicaret_edit();
  6593. end_action();
  6594. }
  6595. void TextEdit::_backspace_internal(int p_caret) {
  6596. ERR_FAIL_COND(p_caret >= get_caret_count() || p_caret < -1);
  6597. if (!editable) {
  6598. return;
  6599. }
  6600. if (has_selection(p_caret)) {
  6601. delete_selection(p_caret);
  6602. return;
  6603. }
  6604. begin_complex_operation();
  6605. begin_multicaret_edit();
  6606. for (int i = 0; i < get_caret_count(); i++) {
  6607. if (p_caret == -1 && multicaret_edit_ignore_caret(i)) {
  6608. continue;
  6609. }
  6610. if (p_caret != -1 && p_caret != i) {
  6611. continue;
  6612. }
  6613. int to_line = get_caret_line(i);
  6614. int to_column = get_caret_column(i);
  6615. if (to_column == 0 && to_line == 0) {
  6616. continue;
  6617. }
  6618. int from_line = to_column > 0 ? to_line : to_line - 1;
  6619. int from_column = 0;
  6620. if (to_column == 0) {
  6621. from_column = text[to_line - 1].length();
  6622. } else if (caret_mid_grapheme_enabled || !backspace_deletes_composite_character_enabled) {
  6623. from_column = to_column - 1;
  6624. } else {
  6625. from_column = get_previous_composite_character_column(to_line, to_column);
  6626. }
  6627. merge_gutters(from_line, to_line);
  6628. _remove_text(from_line, from_column, to_line, to_column);
  6629. collapse_carets(from_line, from_column, to_line, to_column);
  6630. _offset_carets_after(to_line, to_column, from_line, from_column);
  6631. set_caret_line(from_line, false, true, -1, i);
  6632. set_caret_column(from_column, i == 0, i);
  6633. }
  6634. end_multicaret_edit();
  6635. end_complex_operation();
  6636. }
  6637. void TextEdit::_cut_internal(int p_caret) {
  6638. ERR_FAIL_COND(p_caret >= get_caret_count() || p_caret < -1);
  6639. _copy_internal(p_caret);
  6640. if (!editable) {
  6641. return;
  6642. }
  6643. if (has_selection(p_caret)) {
  6644. delete_selection(p_caret);
  6645. return;
  6646. }
  6647. if (!empty_selection_clipboard_enabled) {
  6648. return;
  6649. }
  6650. // Remove full lines.
  6651. begin_complex_operation();
  6652. begin_multicaret_edit();
  6653. Vector<Point2i> line_ranges;
  6654. if (p_caret == -1) {
  6655. line_ranges = get_line_ranges_from_carets();
  6656. } else {
  6657. line_ranges.push_back(Point2i(get_caret_line(p_caret), get_caret_line(p_caret)));
  6658. }
  6659. int line_offset = 0;
  6660. for (Point2i line_range : line_ranges) {
  6661. // Preserve carets on the last line.
  6662. remove_line_at(line_range.y + line_offset);
  6663. if (line_range.x != line_range.y) {
  6664. remove_text(line_range.x + line_offset, 0, line_range.y + line_offset, 0);
  6665. }
  6666. line_offset += line_range.x - line_range.y - 1;
  6667. }
  6668. end_multicaret_edit();
  6669. end_complex_operation();
  6670. }
  6671. void TextEdit::_copy_internal(int p_caret) {
  6672. ERR_FAIL_COND(p_caret >= get_caret_count() || p_caret < -1);
  6673. if (has_selection(p_caret)) {
  6674. DisplayServer::get_singleton()->clipboard_set(get_selected_text(p_caret));
  6675. cut_copy_line = "";
  6676. return;
  6677. }
  6678. if (!empty_selection_clipboard_enabled) {
  6679. return;
  6680. }
  6681. // Copy full lines.
  6682. StringBuilder clipboard;
  6683. Vector<Point2i> line_ranges;
  6684. if (p_caret == -1) {
  6685. // When there are multiple carets on a line, only copy it once.
  6686. line_ranges = get_line_ranges_from_carets(false, true);
  6687. } else {
  6688. line_ranges.push_back(Point2i(get_caret_line(p_caret), get_caret_line(p_caret)));
  6689. }
  6690. for (Point2i line_range : line_ranges) {
  6691. for (int i = line_range.x; i <= line_range.y; i++) {
  6692. if (text[i].length() != 0) {
  6693. clipboard += _base_get_text(i, 0, i, text[i].length());
  6694. }
  6695. clipboard += "\n";
  6696. }
  6697. }
  6698. String clipboard_string = clipboard.as_string();
  6699. DisplayServer::get_singleton()->clipboard_set(clipboard_string);
  6700. // Set the cut copy line so we know to paste as a line.
  6701. if (get_caret_count() == 1) {
  6702. cut_copy_line = clipboard_string;
  6703. } else {
  6704. cut_copy_line = "";
  6705. }
  6706. }
  6707. void TextEdit::_paste_internal(int p_caret) {
  6708. ERR_FAIL_COND(p_caret >= get_caret_count() || p_caret < -1);
  6709. if (!editable) {
  6710. return;
  6711. }
  6712. String clipboard = DisplayServer::get_singleton()->clipboard_get();
  6713. if (clipboard.is_empty()) {
  6714. // Nothing to paste.
  6715. return;
  6716. }
  6717. // Paste a full line. Ignore '\r' characters that may have been added to the clipboard by the OS.
  6718. if (get_caret_count() == 1 && !has_selection(0) && !cut_copy_line.is_empty() && cut_copy_line == clipboard.remove_char('\r')) {
  6719. insert_text(clipboard, get_caret_line(), 0);
  6720. return;
  6721. }
  6722. // Paste text at each caret or one line per caret.
  6723. Vector<String> clipboard_lines = clipboard.split("\n");
  6724. bool insert_line_per_caret = p_caret == -1 && get_caret_count() > 1 && clipboard_lines.size() == get_caret_count();
  6725. begin_complex_operation();
  6726. begin_multicaret_edit();
  6727. Vector<int> sorted_carets = get_sorted_carets();
  6728. for (int i = 0; i < sorted_carets.size(); i++) {
  6729. int caret_index = sorted_carets[i];
  6730. if (p_caret != -1 && p_caret != caret_index) {
  6731. continue;
  6732. }
  6733. if (has_selection(caret_index)) {
  6734. delete_selection(caret_index);
  6735. }
  6736. if (insert_line_per_caret) {
  6737. clipboard = clipboard_lines[i];
  6738. }
  6739. insert_text_at_caret(clipboard, caret_index);
  6740. }
  6741. end_multicaret_edit();
  6742. end_complex_operation();
  6743. }
  6744. void TextEdit::_paste_primary_clipboard_internal(int p_caret) {
  6745. ERR_FAIL_COND(p_caret >= get_caret_count() || p_caret < -1);
  6746. if (!is_editable() || !DisplayServer::get_singleton()->has_feature(DisplayServer::FEATURE_CLIPBOARD_PRIMARY)) {
  6747. return;
  6748. }
  6749. String paste_buffer = DisplayServer::get_singleton()->clipboard_get_primary();
  6750. if (get_caret_count() == 1) {
  6751. Point2i pos = get_line_column_at_pos(get_local_mouse_pos());
  6752. deselect();
  6753. set_caret_line(pos.y, true, false, -1);
  6754. set_caret_column(pos.x);
  6755. }
  6756. if (!paste_buffer.is_empty()) {
  6757. insert_text_at_caret(paste_buffer);
  6758. }
  6759. grab_focus();
  6760. }
  6761. // Context menu.
  6762. Key TextEdit::_get_menu_action_accelerator(const String &p_action) {
  6763. const List<Ref<InputEvent>> *events = InputMap::get_singleton()->action_get_events(p_action);
  6764. if (!events) {
  6765. return Key::NONE;
  6766. }
  6767. // Use first event in the list for the accelerator.
  6768. const List<Ref<InputEvent>>::Element *first_event = events->front();
  6769. if (!first_event) {
  6770. return Key::NONE;
  6771. }
  6772. const Ref<InputEventKey> event = first_event->get();
  6773. if (event.is_null()) {
  6774. return Key::NONE;
  6775. }
  6776. // Use physical keycode if non-zero.
  6777. if (event->get_physical_keycode() != Key::NONE) {
  6778. return event->get_physical_keycode_with_modifiers();
  6779. } else {
  6780. return event->get_keycode_with_modifiers();
  6781. }
  6782. }
  6783. void TextEdit::_generate_context_menu() {
  6784. menu = memnew(PopupMenu);
  6785. add_child(menu, false, INTERNAL_MODE_FRONT);
  6786. menu_dir = memnew(PopupMenu);
  6787. menu_dir->add_radio_check_item(ETR("Same as Layout Direction"), MENU_DIR_INHERITED);
  6788. menu_dir->add_radio_check_item(ETR("Auto-Detect Direction"), MENU_DIR_AUTO);
  6789. menu_dir->add_radio_check_item(ETR("Left-to-Right"), MENU_DIR_LTR);
  6790. menu_dir->add_radio_check_item(ETR("Right-to-Left"), MENU_DIR_RTL);
  6791. menu_ctl = memnew(PopupMenu);
  6792. menu_ctl->add_item(ETR("Left-to-Right Mark (LRM)"), MENU_INSERT_LRM);
  6793. menu_ctl->add_item(ETR("Right-to-Left Mark (RLM)"), MENU_INSERT_RLM);
  6794. menu_ctl->add_item(ETR("Start of Left-to-Right Embedding (LRE)"), MENU_INSERT_LRE);
  6795. menu_ctl->add_item(ETR("Start of Right-to-Left Embedding (RLE)"), MENU_INSERT_RLE);
  6796. menu_ctl->add_item(ETR("Start of Left-to-Right Override (LRO)"), MENU_INSERT_LRO);
  6797. menu_ctl->add_item(ETR("Start of Right-to-Left Override (RLO)"), MENU_INSERT_RLO);
  6798. menu_ctl->add_item(ETR("Pop Direction Formatting (PDF)"), MENU_INSERT_PDF);
  6799. menu_ctl->add_separator();
  6800. menu_ctl->add_item(ETR("Arabic Letter Mark (ALM)"), MENU_INSERT_ALM);
  6801. menu_ctl->add_item(ETR("Left-to-Right Isolate (LRI)"), MENU_INSERT_LRI);
  6802. menu_ctl->add_item(ETR("Right-to-Left Isolate (RLI)"), MENU_INSERT_RLI);
  6803. menu_ctl->add_item(ETR("First Strong Isolate (FSI)"), MENU_INSERT_FSI);
  6804. menu_ctl->add_item(ETR("Pop Direction Isolate (PDI)"), MENU_INSERT_PDI);
  6805. menu_ctl->add_separator();
  6806. menu_ctl->add_item(ETR("Zero-Width Joiner (ZWJ)"), MENU_INSERT_ZWJ);
  6807. menu_ctl->add_item(ETR("Zero-Width Non-Joiner (ZWNJ)"), MENU_INSERT_ZWNJ);
  6808. menu_ctl->add_item(ETR("Word Joiner (WJ)"), MENU_INSERT_WJ);
  6809. menu_ctl->add_item(ETR("Soft Hyphen (SHY)"), MENU_INSERT_SHY);
  6810. if (DisplayServer::get_singleton()->has_feature(DisplayServer::FEATURE_EMOJI_AND_SYMBOL_PICKER)) {
  6811. menu->add_item(ETR("Emoji & Symbols"), MENU_EMOJI_AND_SYMBOL);
  6812. menu->add_separator();
  6813. }
  6814. menu->add_item(ETR("Cut"), MENU_CUT);
  6815. menu->add_item(ETR("Copy"), MENU_COPY);
  6816. menu->add_item(ETR("Paste"), MENU_PASTE);
  6817. menu->add_separator();
  6818. menu->add_item(ETR("Select All"), MENU_SELECT_ALL);
  6819. menu->add_item(ETR("Clear"), MENU_CLEAR);
  6820. menu->add_separator();
  6821. menu->add_item(ETR("Undo"), MENU_UNDO);
  6822. menu->add_item(ETR("Redo"), MENU_REDO);
  6823. menu->add_separator();
  6824. menu->add_submenu_node_item(ETR("Text Writing Direction"), menu_dir, MENU_SUBMENU_TEXT_DIR);
  6825. menu->add_separator();
  6826. menu->add_check_item(ETR("Display Control Characters"), MENU_DISPLAY_UCC);
  6827. menu->add_submenu_node_item(ETR("Insert Control Character"), menu_ctl, MENU_SUBMENU_INSERT_UCC);
  6828. menu->connect(SceneStringName(id_pressed), callable_mp(this, &TextEdit::menu_option));
  6829. menu_dir->connect(SceneStringName(id_pressed), callable_mp(this, &TextEdit::menu_option));
  6830. menu_ctl->connect(SceneStringName(id_pressed), callable_mp(this, &TextEdit::menu_option));
  6831. }
  6832. void TextEdit::_update_context_menu() {
  6833. if (!menu) {
  6834. _generate_context_menu();
  6835. }
  6836. int idx = -1;
  6837. #define MENU_ITEM_ACTION_DISABLED(m_menu, m_id, m_action, m_disabled) \
  6838. idx = m_menu->get_item_index(m_id); \
  6839. if (idx >= 0) { \
  6840. m_menu->set_item_accelerator(idx, shortcut_keys_enabled ? _get_menu_action_accelerator(m_action) : Key::NONE); \
  6841. m_menu->set_item_disabled(idx, m_disabled); \
  6842. }
  6843. #define MENU_ITEM_ACTION(m_menu, m_id, m_action) \
  6844. idx = m_menu->get_item_index(m_id); \
  6845. if (idx >= 0) { \
  6846. m_menu->set_item_accelerator(idx, shortcut_keys_enabled ? _get_menu_action_accelerator(m_action) : Key::NONE); \
  6847. }
  6848. #define MENU_ITEM_DISABLED(m_menu, m_id, m_disabled) \
  6849. idx = m_menu->get_item_index(m_id); \
  6850. if (idx >= 0) { \
  6851. m_menu->set_item_disabled(idx, m_disabled); \
  6852. }
  6853. #define MENU_ITEM_CHECKED(m_menu, m_id, m_checked) \
  6854. idx = m_menu->get_item_index(m_id); \
  6855. if (idx >= 0) { \
  6856. m_menu->set_item_checked(idx, m_checked); \
  6857. }
  6858. if (DisplayServer::get_singleton()->has_feature(DisplayServer::FEATURE_EMOJI_AND_SYMBOL_PICKER)) {
  6859. MENU_ITEM_DISABLED(menu, MENU_EMOJI_AND_SYMBOL, !editable || !emoji_menu_enabled)
  6860. }
  6861. MENU_ITEM_ACTION_DISABLED(menu, MENU_CUT, "ui_cut", !editable)
  6862. MENU_ITEM_ACTION(menu, MENU_COPY, "ui_copy")
  6863. MENU_ITEM_ACTION_DISABLED(menu, MENU_PASTE, "ui_paste", !editable)
  6864. MENU_ITEM_ACTION_DISABLED(menu, MENU_SELECT_ALL, "ui_text_select_all", !selecting_enabled)
  6865. MENU_ITEM_DISABLED(menu, MENU_CLEAR, !editable)
  6866. MENU_ITEM_ACTION_DISABLED(menu, MENU_UNDO, "ui_undo", !editable || !has_undo())
  6867. MENU_ITEM_ACTION_DISABLED(menu, MENU_REDO, "ui_redo", !editable || !has_redo())
  6868. MENU_ITEM_CHECKED(menu_dir, MENU_DIR_INHERITED, text_direction == TEXT_DIRECTION_INHERITED)
  6869. MENU_ITEM_CHECKED(menu_dir, MENU_DIR_AUTO, text_direction == TEXT_DIRECTION_AUTO)
  6870. MENU_ITEM_CHECKED(menu_dir, MENU_DIR_LTR, text_direction == TEXT_DIRECTION_LTR)
  6871. MENU_ITEM_CHECKED(menu_dir, MENU_DIR_RTL, text_direction == TEXT_DIRECTION_RTL)
  6872. MENU_ITEM_CHECKED(menu, MENU_DISPLAY_UCC, draw_control_chars)
  6873. MENU_ITEM_DISABLED(menu, MENU_SUBMENU_INSERT_UCC, !editable)
  6874. #undef MENU_ITEM_ACTION_DISABLED
  6875. #undef MENU_ITEM_ACTION
  6876. #undef MENU_ITEM_DISABLED
  6877. #undef MENU_ITEM_CHECKED
  6878. }
  6879. /* Versioning */
  6880. void TextEdit::_push_current_op() {
  6881. if (pending_action_end) {
  6882. start_action(EditAction::ACTION_NONE);
  6883. return;
  6884. }
  6885. if (current_op.type == TextOperation::TYPE_NONE) {
  6886. return; // Nothing to do.
  6887. }
  6888. if (next_operation_is_complex) {
  6889. current_op.chain_forward = true;
  6890. next_operation_is_complex = false;
  6891. }
  6892. undo_stack.push_back(current_op);
  6893. current_op.type = TextOperation::TYPE_NONE;
  6894. current_op.text = "";
  6895. current_op.chain_forward = false;
  6896. if (undo_stack.size() > undo_stack_max_size) {
  6897. undo_stack.pop_front();
  6898. }
  6899. }
  6900. void TextEdit::_do_text_op(const TextOperation &p_op, bool p_reverse) {
  6901. ERR_FAIL_COND(p_op.type == TextOperation::TYPE_NONE);
  6902. bool insert = p_op.type == TextOperation::TYPE_INSERT;
  6903. if (p_reverse) {
  6904. insert = !insert;
  6905. }
  6906. if (insert) {
  6907. int check_line;
  6908. int check_column;
  6909. _base_insert_text(p_op.from_line, p_op.from_column, p_op.text, check_line, check_column);
  6910. ERR_FAIL_COND(check_line != p_op.to_line); // BUG.
  6911. ERR_FAIL_COND(check_column != p_op.to_column); // BUG.
  6912. } else {
  6913. _base_remove_text(p_op.from_line, p_op.from_column, p_op.to_line, p_op.to_column);
  6914. }
  6915. }
  6916. void TextEdit::_clear_redo() {
  6917. if (undo_stack_pos == nullptr) {
  6918. return; // Nothing to clear.
  6919. }
  6920. _push_current_op();
  6921. while (undo_stack_pos) {
  6922. List<TextOperation>::Element *elem = undo_stack_pos;
  6923. undo_stack_pos = undo_stack_pos->next();
  6924. undo_stack.erase(elem);
  6925. }
  6926. }
  6927. /* Search */
  6928. 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 {
  6929. int col = -1;
  6930. if (p_key.length() > 0 && p_search.length() > 0) {
  6931. if (p_from_column < 0 || p_from_column > p_search.length()) {
  6932. p_from_column = 0;
  6933. }
  6934. bool key_start_is_symbol = is_symbol(p_key[0]);
  6935. bool key_end_is_symbol = is_symbol(p_key[p_key.length() - 1]);
  6936. while (col == -1 && p_from_column <= p_search.length()) {
  6937. if (p_search_flags & SEARCH_MATCH_CASE) {
  6938. col = p_search.find(p_key, p_from_column);
  6939. } else {
  6940. col = p_search.findn(p_key, p_from_column);
  6941. }
  6942. // If not found, just break early to improve performance.
  6943. if (col == -1) {
  6944. break;
  6945. }
  6946. // Whole words only.
  6947. if (col != -1 && p_search_flags & SEARCH_WHOLE_WORDS) {
  6948. p_from_column = col;
  6949. if (!key_start_is_symbol && col > 0 && !is_symbol(p_search[col - 1])) {
  6950. col = -1;
  6951. } else if (!key_end_is_symbol && (col + p_key.length()) < p_search.length() && !is_symbol(p_search[col + p_key.length()])) {
  6952. col = -1;
  6953. }
  6954. }
  6955. p_from_column += 1;
  6956. }
  6957. }
  6958. return col;
  6959. }
  6960. /* Mouse */
  6961. int TextEdit::_get_char_pos_for_line(int p_px, int p_line, int p_wrap_index) const {
  6962. ERR_FAIL_INDEX_V(p_line, text.size(), 0);
  6963. p_wrap_index = MIN(p_wrap_index, text.get_line_data(p_line)->get_line_count() - 1);
  6964. RID text_rid = text.get_line_data(p_line)->get_line_rid(p_wrap_index);
  6965. const float wrap_indent = _get_wrap_indent_offset(p_line, p_wrap_index, is_layout_rtl());
  6966. if (is_layout_rtl()) {
  6967. p_px = TS->shaped_text_get_size(text_rid).x - p_px + wrap_indent;
  6968. } else {
  6969. p_px -= wrap_indent;
  6970. }
  6971. int ofs = TS->shaped_text_hit_test_position(text_rid, p_px);
  6972. if (!caret_mid_grapheme_enabled) {
  6973. ofs = TS->shaped_text_closest_character_pos(text_rid, ofs);
  6974. }
  6975. return ofs;
  6976. }
  6977. /* Caret */
  6978. void TextEdit::_caret_changed(int p_caret) {
  6979. queue_redraw();
  6980. if (has_selection(p_caret)) {
  6981. _selection_changed(p_caret);
  6982. }
  6983. if (caret_pos_dirty) {
  6984. return;
  6985. }
  6986. if (is_inside_tree()) {
  6987. callable_mp(this, &TextEdit::_emit_caret_changed).call_deferred();
  6988. }
  6989. caret_pos_dirty = true;
  6990. }
  6991. void TextEdit::_emit_caret_changed() {
  6992. emit_signal(SNAME("caret_changed"));
  6993. caret_pos_dirty = false;
  6994. }
  6995. void TextEdit::_reset_caret_blink_timer() {
  6996. if (!caret_blink_enabled) {
  6997. return;
  6998. }
  6999. draw_caret = true;
  7000. if (has_focus()) {
  7001. caret_blink_timer->stop();
  7002. caret_blink_timer->start();
  7003. queue_redraw();
  7004. }
  7005. }
  7006. void TextEdit::_toggle_draw_caret() {
  7007. draw_caret = !draw_caret;
  7008. if (is_visible_in_tree() && has_focus() && window_has_focus) {
  7009. queue_redraw();
  7010. }
  7011. }
  7012. int TextEdit::_get_column_x_offset_for_line(int p_char, int p_line, int p_column) const {
  7013. ERR_FAIL_INDEX_V(p_line, text.size(), 0);
  7014. int wrap_index = 0;
  7015. Vector<Vector2i> wrap_ranges = text.get_line_wrap_ranges(p_line);
  7016. for (int i = 0; i < wrap_ranges.size(); i++) {
  7017. if ((p_char >= wrap_ranges[i].x) && (p_char < wrap_ranges[i].y || (i == wrap_ranges.size() - 1 && p_char == wrap_ranges[i].y))) {
  7018. wrap_index = i;
  7019. break;
  7020. }
  7021. }
  7022. RID text_rid = text.get_line_data(p_line)->get_line_rid(wrap_index);
  7023. bool rtl = is_layout_rtl();
  7024. const float wrap_indent = _get_wrap_indent_offset(p_line, wrap_index, rtl);
  7025. CaretInfo ts_caret = TS->shaped_text_get_carets(text_rid, p_column);
  7026. 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())) {
  7027. return ts_caret.l_caret.position.x + (rtl ? -wrap_indent : wrap_indent);
  7028. } else {
  7029. return ts_caret.t_caret.position.x + (rtl ? -wrap_indent : wrap_indent);
  7030. }
  7031. }
  7032. bool TextEdit::_is_line_col_in_range(int p_line, int p_column, int p_from_line, int p_from_column, int p_to_line, int p_to_column, bool p_include_edges) const {
  7033. if (p_line >= p_from_line && p_line <= p_to_line && (p_line > p_from_line || p_column > p_from_column) && (p_line < p_to_line || p_column < p_to_column)) {
  7034. return true;
  7035. }
  7036. if (p_include_edges) {
  7037. if ((p_line == p_from_line && p_column == p_from_column) || (p_line == p_to_line && p_column == p_to_column)) {
  7038. return true;
  7039. }
  7040. }
  7041. return false;
  7042. }
  7043. void TextEdit::_offset_carets_after(int p_old_line, int p_old_column, int p_new_line, int p_new_column, bool p_include_selection_begin, bool p_include_selection_end) {
  7044. // Moves all carets at or after old_line and old_column.
  7045. // Called after deleting or inserting text so that the carets stay with the text they are at.
  7046. int edit_height = p_new_line - p_old_line;
  7047. int edit_size = p_new_column - p_old_column;
  7048. if (edit_height == 0 && edit_size == 0) {
  7049. return;
  7050. }
  7051. // Intentionally includes carets in the multicaret_edit_ignore list so that they are moved together.
  7052. for (int i = 0; i < get_caret_count(); i++) {
  7053. bool selected = has_selection(i);
  7054. bool caret_at_end = selected && is_caret_after_selection_origin(i);
  7055. bool include_caret_at = caret_at_end ? p_include_selection_end : p_include_selection_begin;
  7056. // Move caret.
  7057. int caret_line = get_caret_line(i);
  7058. int caret_column = get_caret_column(i);
  7059. bool caret_after = caret_line > p_old_line || (caret_line == p_old_line && caret_column > p_old_column);
  7060. bool caret_at = caret_line == p_old_line && caret_column == p_old_column;
  7061. if (caret_after || (caret_at && include_caret_at)) {
  7062. caret_line += edit_height;
  7063. if (caret_line == p_new_line) {
  7064. caret_column += edit_size;
  7065. }
  7066. if (edit_height != 0) {
  7067. set_caret_line(caret_line, false, true, -1, i);
  7068. }
  7069. set_caret_column(caret_column, false, i);
  7070. }
  7071. // Move selection origin.
  7072. if (!selected) {
  7073. continue;
  7074. }
  7075. bool include_selection_origin_at = !caret_at_end ? p_include_selection_end : p_include_selection_begin;
  7076. int selection_origin_line = get_selection_origin_line(i);
  7077. int selection_origin_column = get_selection_origin_column(i);
  7078. bool selection_origin_after = selection_origin_line > p_old_line || (selection_origin_line == p_old_line && selection_origin_column > p_old_column);
  7079. bool selection_origin_at = selection_origin_line == p_old_line && selection_origin_column == p_old_column;
  7080. if (selection_origin_after || (selection_origin_at && include_selection_origin_at)) {
  7081. selection_origin_line += edit_height;
  7082. if (selection_origin_line == p_new_line) {
  7083. selection_origin_column += edit_size;
  7084. }
  7085. select(selection_origin_line, selection_origin_column, caret_line, caret_column, i);
  7086. }
  7087. }
  7088. if (!p_include_selection_begin && p_include_selection_end && has_selection()) {
  7089. // It is possible that two adjacent selections now overlap.
  7090. merge_overlapping_carets();
  7091. }
  7092. }
  7093. void TextEdit::_cancel_drag_and_drop_text() {
  7094. // Cancel the drag operation if drag originated from here.
  7095. if (selection_drag_attempt && get_viewport()) {
  7096. get_viewport()->gui_cancel_drag();
  7097. }
  7098. }
  7099. /* Selection */
  7100. void TextEdit::_selection_changed(int p_caret) {
  7101. if (!selecting_enabled) {
  7102. return;
  7103. }
  7104. _cancel_drag_and_drop_text();
  7105. queue_accessibility_update();
  7106. queue_redraw();
  7107. }
  7108. void TextEdit::_click_selection_held() {
  7109. // Update the selection mode on a timer so it is updated when the view scrolls even if the mouse isn't moving.
  7110. if (!Input::get_singleton()->is_mouse_button_pressed(MouseButton::LEFT)) {
  7111. click_select_held->stop();
  7112. return;
  7113. }
  7114. switch (get_selection_mode()) {
  7115. case SelectionMode::SELECTION_MODE_POINTER: {
  7116. _update_selection_mode_pointer();
  7117. } break;
  7118. case SelectionMode::SELECTION_MODE_WORD: {
  7119. _update_selection_mode_word();
  7120. } break;
  7121. case SelectionMode::SELECTION_MODE_LINE: {
  7122. _update_selection_mode_line();
  7123. } break;
  7124. default: {
  7125. click_select_held->stop();
  7126. break;
  7127. }
  7128. }
  7129. }
  7130. void TextEdit::_update_selection_mode_pointer(bool p_initial) {
  7131. Point2 mp = get_local_mouse_pos();
  7132. Point2i pos = get_line_column_at_pos(mp);
  7133. int line = pos.y;
  7134. int column = pos.x;
  7135. int caret_index = get_caret_count() - 1;
  7136. if (p_initial && !has_selection(caret_index)) {
  7137. set_selection_origin_line(line, true, -1, caret_index);
  7138. set_selection_origin_column(column, caret_index);
  7139. // Set the word begin and end to the column in case the mode changes later.
  7140. carets.write[caret_index].selection.word_begin_column = column;
  7141. carets.write[caret_index].selection.word_end_column = column;
  7142. } else {
  7143. select(get_selection_origin_line(caret_index), get_selection_origin_column(caret_index), line, column, caret_index);
  7144. }
  7145. adjust_viewport_to_caret(caret_index);
  7146. if (has_selection(caret_index)) {
  7147. // Only set to true if any selection has been made.
  7148. dragging_selection = true;
  7149. }
  7150. click_select_held->start();
  7151. merge_overlapping_carets();
  7152. }
  7153. void TextEdit::_update_selection_mode_word(bool p_initial) {
  7154. dragging_selection = true;
  7155. Point2 mp = get_local_mouse_pos();
  7156. Point2i pos = get_line_column_at_pos(mp);
  7157. int line = pos.y;
  7158. int column = pos.x;
  7159. int caret_index = get_caret_count() - 1;
  7160. int caret_pos = CLAMP(column, 0, text[line].length());
  7161. int beg = caret_pos;
  7162. int end = beg;
  7163. PackedInt32Array words = TS->shaped_text_get_word_breaks(text.get_line_data(line)->get_rid());
  7164. for (int i = 0; i < words.size(); i = i + 2) {
  7165. if ((p_initial && words[i] <= caret_pos && words[i + 1] >= caret_pos) || (!p_initial && words[i] < caret_pos && words[i + 1] > caret_pos)) {
  7166. beg = words[i];
  7167. end = words[i + 1];
  7168. break;
  7169. }
  7170. }
  7171. if (p_initial && !has_selection(caret_index)) {
  7172. // Set the selection origin if there is no existing selection.
  7173. select(line, beg, line, end, caret_index);
  7174. carets.write[caret_index].selection.word_begin_column = beg;
  7175. carets.write[caret_index].selection.word_end_column = end;
  7176. } else {
  7177. // Expand the word selection to the mouse.
  7178. int origin_line = get_selection_origin_line(caret_index);
  7179. bool is_new_selection_dir_right = line > origin_line || (line == origin_line && column >= carets[caret_index].selection.word_begin_column);
  7180. int origin_col = is_new_selection_dir_right ? carets[caret_index].selection.word_begin_column : carets[caret_index].selection.word_end_column;
  7181. int caret_col = is_new_selection_dir_right ? end : beg;
  7182. // Expand the word selection only if the caret is not at the start of the selection.
  7183. if (column != carets[caret_index].selection.word_begin_column || line != origin_line || carets[caret_index].selection.word_begin_column == carets[caret_index].selection.word_end_column) {
  7184. select(origin_line, origin_col, line, caret_col, caret_index);
  7185. }
  7186. }
  7187. adjust_viewport_to_caret(caret_index);
  7188. if (DisplayServer::get_singleton()->has_feature(DisplayServer::FEATURE_CLIPBOARD_PRIMARY)) {
  7189. DisplayServer::get_singleton()->clipboard_set_primary(get_selected_text());
  7190. }
  7191. click_select_held->start();
  7192. merge_overlapping_carets();
  7193. }
  7194. void TextEdit::_update_selection_mode_line(bool p_initial) {
  7195. dragging_selection = true;
  7196. Point2 mp = get_local_mouse_pos();
  7197. Point2i pos = get_line_column_at_pos(mp);
  7198. int line = pos.y;
  7199. int caret_index = get_caret_count() - 1;
  7200. int origin_line = p_initial && !has_selection(caret_index) ? line : get_selection_origin_line();
  7201. bool line_below = line >= origin_line;
  7202. int origin_col = line_below ? 0 : get_line(origin_line).length();
  7203. int caret_line = line_below ? line + 1 : line;
  7204. int caret_col = caret_line < text.size() ? 0 : get_line(text.size() - 1).length();
  7205. select(origin_line, origin_col, caret_line, caret_col, caret_index);
  7206. adjust_viewport_to_caret(caret_index);
  7207. if (p_initial) {
  7208. // Set the word begin and end to the start and end of the origin line in case the mode changes later.
  7209. carets.write[caret_index].selection.word_begin_column = 0;
  7210. carets.write[caret_index].selection.word_end_column = get_line(origin_line).length();
  7211. }
  7212. if (DisplayServer::get_singleton()->has_feature(DisplayServer::FEATURE_CLIPBOARD_PRIMARY)) {
  7213. DisplayServer::get_singleton()->clipboard_set_primary(get_selected_text());
  7214. }
  7215. click_select_held->start();
  7216. merge_overlapping_carets();
  7217. }
  7218. void TextEdit::_pre_shift_selection(int p_caret) {
  7219. if (!selecting_enabled) {
  7220. return;
  7221. }
  7222. set_selection_mode(SelectionMode::SELECTION_MODE_SHIFT);
  7223. if (has_selection(p_caret)) {
  7224. return;
  7225. }
  7226. // Prepare selection to start at current caret position.
  7227. set_selection_origin_line(get_caret_line(p_caret), true, -1, p_caret);
  7228. set_selection_origin_column(get_caret_column(p_caret), p_caret);
  7229. carets.write[p_caret].selection.active = true;
  7230. carets.write[p_caret].selection.word_begin_column = get_caret_column(p_caret);
  7231. carets.write[p_caret].selection.word_end_column = get_caret_column(p_caret);
  7232. }
  7233. bool TextEdit::_selection_contains(int p_caret, int p_line, int p_column, bool p_include_edges, bool p_only_selections) const {
  7234. if (!has_selection(p_caret)) {
  7235. return !p_only_selections && p_line == get_caret_line(p_caret) && p_column == get_caret_column(p_caret);
  7236. }
  7237. return _is_line_col_in_range(p_line, p_column, get_selection_from_line(p_caret), get_selection_from_column(p_caret), get_selection_to_line(p_caret), get_selection_to_column(p_caret), p_include_edges);
  7238. }
  7239. /* Line Wrapping */
  7240. void TextEdit::_update_wrap_at_column(bool p_force) {
  7241. int new_wrap_at = get_size().width - theme_cache.style_normal->get_minimum_size().width - gutters_width - gutter_padding;
  7242. if (draw_minimap) {
  7243. new_wrap_at -= minimap_width;
  7244. }
  7245. if (v_scroll->is_visible_in_tree()) {
  7246. new_wrap_at -= v_scroll->get_combined_minimum_size().width;
  7247. }
  7248. /* Give it a little more space. */
  7249. new_wrap_at -= wrap_right_offset;
  7250. if ((wrap_at_column != new_wrap_at) || p_force) {
  7251. wrap_at_column = new_wrap_at;
  7252. if (line_wrapping_mode) {
  7253. BitField<TextServer::LineBreakFlag> autowrap_flags = TextServer::BREAK_MANDATORY;
  7254. switch (autowrap_mode) {
  7255. case TextServer::AUTOWRAP_WORD_SMART:
  7256. autowrap_flags = TextServer::BREAK_WORD_BOUND | TextServer::BREAK_ADAPTIVE | TextServer::BREAK_MANDATORY;
  7257. break;
  7258. case TextServer::AUTOWRAP_WORD:
  7259. autowrap_flags = TextServer::BREAK_WORD_BOUND | TextServer::BREAK_MANDATORY;
  7260. break;
  7261. case TextServer::AUTOWRAP_ARBITRARY:
  7262. autowrap_flags = TextServer::BREAK_GRAPHEME_BOUND | TextServer::BREAK_MANDATORY;
  7263. break;
  7264. case TextServer::AUTOWRAP_OFF:
  7265. break;
  7266. }
  7267. text.set_brk_flags(autowrap_flags);
  7268. text.set_width(wrap_at_column);
  7269. text.invalidate_all_lines();
  7270. _update_placeholder();
  7271. } else if (text.get_width() != -1) {
  7272. text.set_width(-1);
  7273. text.invalidate_all_lines();
  7274. _update_placeholder();
  7275. }
  7276. }
  7277. // Update viewport.
  7278. int first_vis_line = get_first_visible_line();
  7279. if (is_line_wrapped(first_vis_line)) {
  7280. first_visible_line_wrap_ofs = MIN(first_visible_line_wrap_ofs, get_line_wrap_count(first_vis_line));
  7281. } else {
  7282. first_visible_line_wrap_ofs = 0;
  7283. }
  7284. set_line_as_first_visible(first_visible_line, first_visible_line_wrap_ofs);
  7285. queue_accessibility_update();
  7286. }
  7287. /* Viewport. */
  7288. void TextEdit::_update_scrollbars() {
  7289. Size2 size = get_size();
  7290. Size2 hmin = h_scroll->get_combined_minimum_size();
  7291. Size2 vmin = v_scroll->get_combined_minimum_size();
  7292. v_scroll->set_begin(Point2(size.width - vmin.width, theme_cache.style_normal->get_margin(SIDE_TOP)));
  7293. 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)));
  7294. h_scroll->set_begin(Point2(0, size.height - hmin.height));
  7295. h_scroll->set_end(Point2(size.width - vmin.width, size.height));
  7296. bool draw_placeholder = _using_placeholder();
  7297. int visible_rows = get_visible_line_count();
  7298. int total_rows = draw_placeholder ? placeholder_wrapped_rows.size() : get_total_visible_line_count();
  7299. if ((scroll_past_end_of_file_enabled && !fit_content_height) || visible_rows == 0) {
  7300. total_rows += visible_rows - 1;
  7301. }
  7302. int visible_width = size.width - theme_cache.style_normal->get_minimum_size().width;
  7303. int total_width = (draw_placeholder ? placeholder_max_width : text.get_max_width()) + gutters_width + gutter_padding;
  7304. if (draw_minimap) {
  7305. total_width += minimap_width;
  7306. }
  7307. content_size_cache = Vector2i(total_width + 10, MAX(total_rows, 1) * get_line_height());
  7308. if (fit_content_height || fit_content_width) {
  7309. update_minimum_size();
  7310. }
  7311. updating_scrolls = true;
  7312. if (!fit_content_height && total_rows > visible_rows) {
  7313. v_scroll->show();
  7314. v_scroll->set_max(total_rows + _get_visible_lines_offset());
  7315. v_scroll->set_page(visible_rows + _get_visible_lines_offset());
  7316. set_v_scroll(get_v_scroll());
  7317. } else {
  7318. first_visible_line = 0;
  7319. first_visible_line_wrap_ofs = 0;
  7320. v_scroll->set_value(0);
  7321. v_scroll->set_max(0);
  7322. v_scroll->hide();
  7323. }
  7324. if (total_width > visible_width) {
  7325. h_scroll->show();
  7326. h_scroll->set_max(total_width);
  7327. h_scroll->set_page(visible_width);
  7328. if (first_visible_col > (total_width - visible_width)) {
  7329. first_visible_col = (total_width - visible_width);
  7330. }
  7331. if (std::fabs(h_scroll->get_value() - (double)first_visible_col) >= 1) {
  7332. h_scroll->set_value(first_visible_col);
  7333. }
  7334. } else {
  7335. first_visible_col = 0;
  7336. h_scroll->set_value(0);
  7337. h_scroll->set_max(0);
  7338. h_scroll->hide();
  7339. }
  7340. updating_scrolls = false;
  7341. }
  7342. int TextEdit::_get_control_height() const {
  7343. int control_height = get_size().height;
  7344. control_height -= theme_cache.style_normal->get_minimum_size().height;
  7345. if (h_scroll->is_visible_in_tree()) {
  7346. control_height -= h_scroll->get_size().height;
  7347. }
  7348. return control_height;
  7349. }
  7350. void TextEdit::_v_scroll_input() {
  7351. scrolling = false;
  7352. minimap_clicked = false;
  7353. }
  7354. void TextEdit::_scroll_moved(double p_to_val) {
  7355. if (updating_scrolls) {
  7356. return;
  7357. }
  7358. if (h_scroll->is_visible_in_tree()) {
  7359. first_visible_col = h_scroll->get_value();
  7360. }
  7361. if (v_scroll->is_visible_in_tree()) {
  7362. // Set line ofs and wrap ofs.
  7363. bool draw_placeholder = _using_placeholder();
  7364. int v_scroll_i = std::floor(get_v_scroll());
  7365. int sc = 0;
  7366. int n_line;
  7367. for (n_line = 0; n_line < text.size(); n_line++) {
  7368. if (!_is_line_hidden(n_line)) {
  7369. sc++;
  7370. sc += draw_placeholder ? placeholder_wrapped_rows.size() - 1 : get_line_wrap_count(n_line);
  7371. if (sc > v_scroll_i) {
  7372. break;
  7373. }
  7374. }
  7375. }
  7376. n_line = MIN(n_line, text.size() - 1);
  7377. int line_wrap_amount = draw_placeholder ? placeholder_wrapped_rows.size() - 1 : get_line_wrap_count(n_line);
  7378. int wi = line_wrap_amount - (sc - v_scroll_i - 1);
  7379. wi = CLAMP(wi, 0, line_wrap_amount);
  7380. first_visible_line = n_line;
  7381. first_visible_line_wrap_ofs = wi;
  7382. }
  7383. queue_accessibility_update();
  7384. queue_redraw();
  7385. }
  7386. double TextEdit::_get_visible_lines_offset() const {
  7387. double total = _get_control_height();
  7388. total /= (double)get_line_height();
  7389. total = total - std::floor(total);
  7390. total = -CLAMP(total, 0.001, 1) + 1;
  7391. return total;
  7392. }
  7393. double TextEdit::_get_v_scroll_offset() const {
  7394. double val = get_v_scroll() - std::floor(get_v_scroll());
  7395. return CLAMP(val, 0, 1);
  7396. }
  7397. void TextEdit::_scroll_up(real_t p_delta, bool p_animate) {
  7398. if (scrolling && smooth_scroll_enabled && SIGN(target_v_scroll - v_scroll->get_value()) != SIGN(-p_delta)) {
  7399. scrolling = false;
  7400. minimap_clicked = false;
  7401. }
  7402. if (scrolling) {
  7403. target_v_scroll = (target_v_scroll - p_delta);
  7404. } else {
  7405. target_v_scroll = (get_v_scroll() - p_delta);
  7406. }
  7407. if (smooth_scroll_enabled) {
  7408. if (target_v_scroll <= 0) {
  7409. target_v_scroll = 0;
  7410. }
  7411. if (!p_animate || Math::abs(target_v_scroll - v_scroll->get_value()) < 1.0) {
  7412. v_scroll->set_value(target_v_scroll);
  7413. queue_accessibility_update();
  7414. } else {
  7415. scrolling = true;
  7416. set_process_internal(true);
  7417. }
  7418. } else {
  7419. set_v_scroll(target_v_scroll);
  7420. }
  7421. }
  7422. void TextEdit::_scroll_down(real_t p_delta, bool p_animate) {
  7423. if (scrolling && smooth_scroll_enabled && SIGN(target_v_scroll - v_scroll->get_value()) != SIGN(p_delta)) {
  7424. scrolling = false;
  7425. minimap_clicked = false;
  7426. }
  7427. if (scrolling) {
  7428. target_v_scroll = (target_v_scroll + p_delta);
  7429. } else {
  7430. target_v_scroll = (get_v_scroll() + p_delta);
  7431. }
  7432. if (smooth_scroll_enabled) {
  7433. int max_v_scroll = std::round(v_scroll->get_max() - v_scroll->get_page());
  7434. if (target_v_scroll > max_v_scroll) {
  7435. target_v_scroll = max_v_scroll;
  7436. }
  7437. if (!p_animate || Math::abs(target_v_scroll - v_scroll->get_value()) < 1.0) {
  7438. v_scroll->set_value(target_v_scroll);
  7439. queue_accessibility_update();
  7440. } else {
  7441. scrolling = true;
  7442. set_process_internal(true);
  7443. }
  7444. } else {
  7445. set_v_scroll(target_v_scroll);
  7446. }
  7447. }
  7448. void TextEdit::_scroll_lines_up() {
  7449. scrolling = false;
  7450. minimap_clicked = false;
  7451. // Adjust the vertical scroll.
  7452. set_v_scroll(get_v_scroll() - 1);
  7453. // Adjust the caret to viewport.
  7454. for (int i = 0; i < carets.size(); i++) {
  7455. if (has_selection(i)) {
  7456. continue;
  7457. }
  7458. int last_vis_line = get_last_full_visible_line();
  7459. int last_vis_wrap = get_last_full_visible_line_wrap_index();
  7460. if (get_caret_line(i) > last_vis_line || (get_caret_line(i) == last_vis_line && get_caret_wrap_index(i) > last_vis_wrap)) {
  7461. set_caret_line(last_vis_line, false, false, last_vis_wrap, i);
  7462. }
  7463. }
  7464. merge_overlapping_carets();
  7465. }
  7466. void TextEdit::_scroll_lines_down() {
  7467. scrolling = false;
  7468. minimap_clicked = false;
  7469. // Adjust the vertical scroll.
  7470. set_v_scroll(get_v_scroll() + 1);
  7471. // Adjust the caret to viewport.
  7472. for (int i = 0; i < carets.size(); i++) {
  7473. if (has_selection(i)) {
  7474. continue;
  7475. }
  7476. int first_vis_line = get_first_visible_line();
  7477. 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)) {
  7478. set_caret_line(first_vis_line, false, false, first_visible_line_wrap_ofs, i);
  7479. }
  7480. }
  7481. merge_overlapping_carets();
  7482. }
  7483. void TextEdit::_adjust_viewport_to_caret_horizontally(int p_caret, bool p_maximize_selection) {
  7484. if (get_line_wrapping_mode() != LineWrappingMode::LINE_WRAPPING_NONE) {
  7485. first_visible_col = 0;
  7486. h_scroll->set_value(first_visible_col);
  7487. queue_redraw();
  7488. return;
  7489. }
  7490. int visible_width = get_size().width - theme_cache.style_normal->get_minimum_size().width - gutters_width - gutter_padding;
  7491. if (draw_minimap) {
  7492. visible_width -= minimap_width;
  7493. }
  7494. if (v_scroll->is_visible_in_tree()) {
  7495. visible_width -= v_scroll->get_combined_minimum_size().width;
  7496. }
  7497. visible_width -= 20; // Give it a little more space.
  7498. if (visible_width <= 0) {
  7499. // Not resized yet.
  7500. return;
  7501. }
  7502. int caret_start_pos;
  7503. int caret_end_pos;
  7504. bool prioritize_end = true;
  7505. // Get start and end position of the caret.
  7506. if (has_ime_text()) {
  7507. // Use the size of the IME.
  7508. int ime_start_column = get_caret_column(p_caret) + ime_selection.x;
  7509. caret_start_pos = _get_column_x_offset_for_line(ime_start_column, get_caret_line(p_caret), ime_start_column);
  7510. int ime_end_column = get_caret_column(p_caret) + (ime_selection.y > 0 ? ime_selection.x + ime_selection.y : ime_text.length());
  7511. caret_end_pos = _get_column_x_offset_for_line(ime_end_column, get_caret_line(p_caret), ime_end_column);
  7512. prioritize_end = false;
  7513. } else if (p_maximize_selection && has_selection(p_caret) && get_selection_from_line(p_caret) == get_selection_to_line(p_caret)) {
  7514. // Use selection if it is on one line.
  7515. caret_start_pos = _get_column_x_offset_for_line(get_selection_from_column(p_caret), get_caret_line(p_caret), get_selection_from_column(p_caret));
  7516. caret_end_pos = _get_column_x_offset_for_line(get_selection_to_column(p_caret), get_caret_line(p_caret), get_selection_to_column(p_caret));
  7517. prioritize_end = is_caret_after_selection_origin();
  7518. } else {
  7519. caret_start_pos = _get_column_x_offset_for_line(get_caret_column(p_caret), get_caret_line(p_caret), get_caret_column(p_caret));
  7520. caret_end_pos = caret_start_pos;
  7521. }
  7522. if (caret_start_pos > caret_end_pos) {
  7523. // For RTL text.
  7524. SWAP(caret_start_pos, caret_end_pos);
  7525. prioritize_end = !prioritize_end;
  7526. }
  7527. if (!prioritize_end && caret_end_pos > first_visible_col + visible_width) {
  7528. first_visible_col = caret_end_pos - visible_width + 1;
  7529. }
  7530. if (caret_start_pos < first_visible_col) {
  7531. first_visible_col = caret_start_pos;
  7532. }
  7533. if (prioritize_end && caret_end_pos > first_visible_col + visible_width) {
  7534. first_visible_col = caret_end_pos - visible_width + 1;
  7535. }
  7536. h_scroll->set_value(first_visible_col);
  7537. queue_accessibility_update();
  7538. queue_redraw();
  7539. }
  7540. // Minimap
  7541. void TextEdit::_update_minimap_hover() {
  7542. const Point2 mp = get_local_mouse_pos();
  7543. const int xmargin_end = get_size().width - Math::ceil(theme_cache.style_normal->get_margin(SIDE_RIGHT));
  7544. bool hovering_sidebar = mp.x > xmargin_end - minimap_width && mp.x < xmargin_end;
  7545. if (!hovering_sidebar) {
  7546. if (hovering_minimap) {
  7547. // Only redraw if the hovering status changed.
  7548. hovering_minimap = false;
  7549. queue_redraw();
  7550. }
  7551. // Return early to avoid running the operations below when not needed.
  7552. return;
  7553. }
  7554. const int row = get_minimap_line_at_pos(mp);
  7555. bool new_hovering_minimap = row >= get_first_visible_line() && row <= get_last_full_visible_line();
  7556. if (new_hovering_minimap != hovering_minimap) {
  7557. // Only redraw if the hovering status changed.
  7558. hovering_minimap = new_hovering_minimap;
  7559. queue_redraw();
  7560. }
  7561. }
  7562. void TextEdit::_update_minimap_click() {
  7563. Point2 mp = get_local_mouse_pos();
  7564. int xmargin_end = get_size().width - Math::ceil(theme_cache.style_normal->get_margin(SIDE_RIGHT));
  7565. if (!dragging_minimap && (mp.x < xmargin_end - minimap_width || mp.x > xmargin_end)) {
  7566. minimap_clicked = false;
  7567. return;
  7568. }
  7569. minimap_clicked = true;
  7570. dragging_minimap = true;
  7571. int row = get_minimap_line_at_pos(mp);
  7572. if (row >= get_first_visible_line() && (row < get_last_full_visible_line() || row >= (text.size() - 1))) {
  7573. minimap_scroll_ratio = v_scroll->get_as_ratio();
  7574. minimap_scroll_click_pos = mp.y;
  7575. can_drag_minimap = true;
  7576. return;
  7577. }
  7578. Point2i next_line = get_next_visible_line_index_offset_from(row, 0, -get_visible_line_count() / 2);
  7579. int first_line = MAX(0, row - next_line.x + 1);
  7580. double delta = get_scroll_pos_for_line(first_line, next_line.y) - get_v_scroll();
  7581. if (delta < 0) {
  7582. _scroll_up(-delta, true);
  7583. } else {
  7584. _scroll_down(delta, true);
  7585. }
  7586. }
  7587. void TextEdit::_update_minimap_drag() {
  7588. if (!can_drag_minimap) {
  7589. return;
  7590. }
  7591. int control_height = _get_control_height();
  7592. int scroll_height = v_scroll->get_max() * (minimap_char_size.y + minimap_line_spacing);
  7593. if (control_height > scroll_height) {
  7594. control_height = scroll_height;
  7595. }
  7596. Point2 mp = get_local_mouse_pos();
  7597. double diff = (mp.y - minimap_scroll_click_pos) / control_height;
  7598. v_scroll->set_as_ratio(minimap_scroll_ratio + diff);
  7599. }
  7600. /* Gutters. */
  7601. void TextEdit::_update_gutter_width() {
  7602. gutters_width = 0;
  7603. for (int i = 0; i < gutters.size(); i++) {
  7604. if (gutters[i].draw) {
  7605. gutters_width += gutters[i].width;
  7606. }
  7607. }
  7608. if (gutters_width > 0) {
  7609. gutter_padding = 2;
  7610. }
  7611. if (get_viewport()) {
  7612. hovered_gutter = _get_hovered_gutter(get_local_mouse_position());
  7613. }
  7614. queue_accessibility_update();
  7615. queue_redraw();
  7616. }
  7617. Vector2i TextEdit::_get_hovered_gutter(const Point2 &p_mouse_pos) const {
  7618. int left_margin = Math::ceil(theme_cache.style_normal->get_margin(SIDE_LEFT));
  7619. if (p_mouse_pos.x > left_margin + gutters_width + gutter_padding) {
  7620. return Vector2i(-1, -1);
  7621. }
  7622. int hovered_row = get_line_column_at_pos(p_mouse_pos, false).y;
  7623. if (hovered_row == -1) {
  7624. return Vector2i(-1, -1);
  7625. }
  7626. for (int i = 0; i < gutters.size(); i++) {
  7627. if (!gutters[i].draw || gutters[i].width <= 0) {
  7628. continue;
  7629. }
  7630. if (p_mouse_pos.x >= left_margin && p_mouse_pos.x < left_margin + gutters[i].width) {
  7631. return Vector2i(i, hovered_row);
  7632. }
  7633. left_margin += gutters[i].width;
  7634. }
  7635. return Vector2i(-1, -1);
  7636. }
  7637. /* Syntax highlighting. */
  7638. Vector<Pair<int64_t, Color>> TextEdit::_get_line_syntax_highlighting(int p_line) {
  7639. if (syntax_highlighter.is_null() || setting_text) {
  7640. return Vector<Pair<int64_t, Color>>();
  7641. }
  7642. HashMap<int, Vector<Pair<int64_t, Color>>>::Iterator E = syntax_highlighting_cache.find(p_line);
  7643. if (E) {
  7644. return E->value;
  7645. }
  7646. Dictionary color_map = syntax_highlighter->get_line_syntax_highlighting(p_line);
  7647. Vector<Pair<int64_t, Color>> result;
  7648. result.resize(color_map.size());
  7649. int i = 0;
  7650. for (const Variant *key = color_map.next(nullptr); key; key = color_map.next(key), i++) {
  7651. int64_t key_data = *key;
  7652. const Variant *color_data = color_map.getptr(*key);
  7653. Color color_value = editable ? theme_cache.font_color : theme_cache.font_readonly_color;
  7654. if (color_data != nullptr) {
  7655. color_value = (color_data->operator Dictionary()).get("color", color_value);
  7656. }
  7657. result.write[i] = Pair<int64_t, Color>(key_data, color_value);
  7658. }
  7659. syntax_highlighting_cache.insert(p_line, result);
  7660. return result;
  7661. }
  7662. void TextEdit::_clear_syntax_highlighting_cache() {
  7663. syntax_highlighting_cache.clear();
  7664. }
  7665. /* Deprecated. */
  7666. #ifndef DISABLE_DEPRECATED
  7667. Vector<int> TextEdit::get_caret_index_edit_order() {
  7668. Vector<int> carets_order = get_sorted_carets();
  7669. carets_order.reverse();
  7670. return carets_order;
  7671. }
  7672. 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) {
  7673. }
  7674. int TextEdit::get_selection_line(int p_caret) const {
  7675. return get_selection_origin_line(p_caret);
  7676. }
  7677. int TextEdit::get_selection_column(int p_caret) const {
  7678. return get_selection_origin_column(p_caret);
  7679. }
  7680. #endif
  7681. /*** Super internal Core API. Everything builds on it. ***/
  7682. void TextEdit::_text_changed() {
  7683. _clear_syntax_highlighting_cache();
  7684. _cancel_drag_and_drop_text();
  7685. queue_redraw();
  7686. if (text_changed_dirty || setting_text) {
  7687. return;
  7688. }
  7689. if (is_inside_tree()) {
  7690. callable_mp(this, &TextEdit::_emit_text_changed).call_deferred();
  7691. }
  7692. text_changed_dirty = true;
  7693. }
  7694. void TextEdit::_emit_text_changed() {
  7695. emit_signal(SceneStringName(text_changed));
  7696. text_changed_dirty = false;
  7697. }
  7698. void TextEdit::_insert_text(int p_line, int p_char, const String &p_text, int *r_end_line, int *r_end_char) {
  7699. if (!setting_text && idle_detect->is_inside_tree()) {
  7700. idle_detect->start();
  7701. }
  7702. if (undo_enabled) {
  7703. _clear_redo();
  7704. }
  7705. int retline, retchar;
  7706. _base_insert_text(p_line, p_char, p_text, retline, retchar);
  7707. if (r_end_line) {
  7708. *r_end_line = retline;
  7709. }
  7710. if (r_end_char) {
  7711. *r_end_char = retchar;
  7712. }
  7713. if (!undo_enabled) {
  7714. return;
  7715. }
  7716. /* UNDO!! */
  7717. TextOperation op;
  7718. op.type = TextOperation::TYPE_INSERT;
  7719. op.from_line = p_line;
  7720. op.from_column = p_char;
  7721. op.to_line = retline;
  7722. op.to_column = retchar;
  7723. op.text = p_text;
  7724. op.version = ++version;
  7725. op.chain_forward = false;
  7726. op.chain_backward = false;
  7727. if (next_operation_is_complex) {
  7728. op.start_carets = current_op.start_carets;
  7729. } else {
  7730. op.start_carets = carets;
  7731. }
  7732. op.end_carets = carets;
  7733. op.prev_version = get_version();
  7734. _push_current_op();
  7735. current_op = op;
  7736. }
  7737. void TextEdit::_remove_text(int p_from_line, int p_from_column, int p_to_line, int p_to_column) {
  7738. if (!setting_text && idle_detect->is_inside_tree()) {
  7739. idle_detect->start();
  7740. }
  7741. String txt;
  7742. if (undo_enabled) {
  7743. _clear_redo();
  7744. txt = _base_get_text(p_from_line, p_from_column, p_to_line, p_to_column);
  7745. }
  7746. _base_remove_text(p_from_line, p_from_column, p_to_line, p_to_column);
  7747. if (!undo_enabled) {
  7748. return;
  7749. }
  7750. /* UNDO! */
  7751. TextOperation op;
  7752. op.type = TextOperation::TYPE_REMOVE;
  7753. op.from_line = p_from_line;
  7754. op.from_column = p_from_column;
  7755. op.to_line = p_to_line;
  7756. op.to_column = p_to_column;
  7757. op.text = txt;
  7758. op.version = ++version;
  7759. op.chain_forward = false;
  7760. op.chain_backward = false;
  7761. if (next_operation_is_complex) {
  7762. op.start_carets = current_op.start_carets;
  7763. } else {
  7764. op.start_carets = carets;
  7765. }
  7766. op.end_carets = carets;
  7767. op.prev_version = get_version();
  7768. _push_current_op();
  7769. current_op = op;
  7770. }
  7771. void TextEdit::_base_insert_text(int p_line, int p_char, const String &p_text, int &r_end_line, int &r_end_column) {
  7772. // Save for undo.
  7773. ERR_FAIL_INDEX(p_line, text.size());
  7774. ERR_FAIL_COND(p_char < 0);
  7775. /* STEP 1: Remove \r from source text and separate in substrings. */
  7776. const String text_to_insert = p_text.remove_char('\r');
  7777. Vector<String> substrings = text_to_insert.split("\n");
  7778. // Is this just a new empty line?
  7779. bool shift_first_line = p_char == 0 && substrings.size() == 2 && text_to_insert == "\n";
  7780. /* STEP 2: Add spaces if the char is greater than the end of the line. */
  7781. while (p_char > text[p_line].length()) {
  7782. text.set(p_line, text[p_line] + String::chr(' '), structured_text_parser(st_parser, st_args, text[p_line] + String::chr(' ')));
  7783. }
  7784. /* STEP 3: Separate dest string in pre and post text. */
  7785. String postinsert_text = text[p_line].substr(p_char);
  7786. substrings.write[0] = text[p_line].substr(0, p_char) + substrings[0];
  7787. substrings.write[substrings.size() - 1] += postinsert_text;
  7788. Vector<Array> bidi_override;
  7789. bidi_override.resize(substrings.size());
  7790. for (int i = 0; i < substrings.size(); i++) {
  7791. bidi_override.write[i] = structured_text_parser(st_parser, st_args, substrings[i]);
  7792. }
  7793. text.insert(p_line, substrings, bidi_override);
  7794. if (shift_first_line) {
  7795. text.move_gutters(p_line, p_line + 1);
  7796. text.set_hidden(p_line + 1, text.is_hidden(p_line));
  7797. text.set_hidden(p_line, false);
  7798. }
  7799. r_end_line = p_line + substrings.size() - 1;
  7800. r_end_column = text[r_end_line].length() - postinsert_text.length();
  7801. 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);
  7802. if (dir != TextServer::DIRECTION_AUTO) {
  7803. input_direction = (TextDirection)dir;
  7804. }
  7805. _text_changed();
  7806. emit_signal(SNAME("lines_edited_from"), p_line, r_end_line);
  7807. }
  7808. String TextEdit::_base_get_text(int p_from_line, int p_from_column, int p_to_line, int p_to_column) const {
  7809. ERR_FAIL_INDEX_V(p_from_line, text.size(), String());
  7810. ERR_FAIL_INDEX_V(p_from_column, text[p_from_line].length() + 1, String());
  7811. ERR_FAIL_INDEX_V(p_to_line, text.size(), String());
  7812. ERR_FAIL_INDEX_V(p_to_column, text[p_to_line].length() + 1, String());
  7813. ERR_FAIL_COND_V(p_to_line < p_from_line, String()); // 'from > to'.
  7814. ERR_FAIL_COND_V(p_to_line == p_from_line && p_to_column < p_from_column, String()); // 'from > to'.
  7815. StringBuilder ret;
  7816. for (int i = p_from_line; i <= p_to_line; i++) {
  7817. int begin = (i == p_from_line) ? p_from_column : 0;
  7818. int end = (i == p_to_line) ? p_to_column : text[i].length();
  7819. if (i > p_from_line) {
  7820. ret += "\n";
  7821. }
  7822. ret += text[i].substr(begin, end - begin);
  7823. }
  7824. return ret.as_string();
  7825. }
  7826. void TextEdit::_base_remove_text(int p_from_line, int p_from_column, int p_to_line, int p_to_column) {
  7827. ERR_FAIL_INDEX(p_from_line, text.size());
  7828. ERR_FAIL_INDEX(p_from_column, text[p_from_line].length() + 1);
  7829. ERR_FAIL_INDEX(p_to_line, text.size());
  7830. ERR_FAIL_INDEX(p_to_column, text[p_to_line].length() + 1);
  7831. ERR_FAIL_COND(p_to_line < p_from_line); // 'from > to'.
  7832. ERR_FAIL_COND(p_to_line == p_from_line && p_to_column < p_from_column); // 'from > to'.
  7833. String pre_text = text[p_from_line].substr(0, p_from_column);
  7834. String post_text = text[p_to_line].substr(p_to_column);
  7835. text.remove_range(p_from_line, p_to_line);
  7836. text.set(p_from_line, pre_text + post_text, structured_text_parser(st_parser, st_args, pre_text + post_text));
  7837. _text_changed();
  7838. emit_signal(SNAME("lines_edited_from"), p_to_line, p_from_line);
  7839. }
  7840. TextEdit::TextEdit(const String &p_placeholder) {
  7841. placeholder_data_buf.instantiate();
  7842. carets.push_back(Caret());
  7843. clear();
  7844. set_focus_mode(FOCUS_ALL);
  7845. set_default_cursor_shape(CURSOR_IBEAM);
  7846. set_process_unhandled_key_input(true);
  7847. text.set_tab_size(text.get_tab_size());
  7848. h_scroll = memnew(HScrollBar);
  7849. v_scroll = memnew(VScrollBar);
  7850. add_child(h_scroll, false, INTERNAL_MODE_FRONT);
  7851. add_child(v_scroll, false, INTERNAL_MODE_FRONT);
  7852. h_scroll->connect(SceneStringName(value_changed), callable_mp(this, &TextEdit::_scroll_moved));
  7853. v_scroll->connect(SceneStringName(value_changed), callable_mp(this, &TextEdit::_scroll_moved));
  7854. v_scroll->connect("scrolling", callable_mp(this, &TextEdit::_v_scroll_input));
  7855. /* Caret. */
  7856. caret_blink_timer = memnew(Timer);
  7857. add_child(caret_blink_timer, false, INTERNAL_MODE_FRONT);
  7858. caret_blink_timer->set_wait_time(0.65);
  7859. caret_blink_timer->connect("timeout", callable_mp(this, &TextEdit::_toggle_draw_caret));
  7860. set_caret_blink_enabled(false);
  7861. /* Selection. */
  7862. click_select_held = memnew(Timer);
  7863. add_child(click_select_held, false, INTERNAL_MODE_FRONT);
  7864. click_select_held->set_wait_time(0.05);
  7865. click_select_held->connect("timeout", callable_mp(this, &TextEdit::_click_selection_held));
  7866. idle_detect = memnew(Timer);
  7867. add_child(idle_detect, false, INTERNAL_MODE_FRONT);
  7868. idle_detect->set_one_shot(true);
  7869. idle_detect->set_wait_time(GLOBAL_GET_CACHED(double, "gui/timers/text_edit_idle_detect_sec"));
  7870. idle_detect->connect("timeout", callable_mp(this, &TextEdit::_push_current_op));
  7871. undo_stack_max_size = GLOBAL_GET_CACHED(int, "gui/common/text_edit_undo_stack_max_size");
  7872. set_placeholder(p_placeholder);
  7873. set_clip_contents(true);
  7874. set_editable(true);
  7875. }