text_edit.cpp 248 KB

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