text_edit.cpp 210 KB

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