text_edit.cpp 195 KB

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