2
0

text_edit.cpp 256 KB

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