text_edit.cpp 254 KB

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