text_edit.cpp 190 KB

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