text_edit.cpp 310 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260
  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 "text_edit.compat.inc"
  32. #include "core/config/project_settings.h"
  33. #include "core/input/input.h"
  34. #include "core/input/input_map.h"
  35. #include "core/object/script_language.h"
  36. #include "core/os/keyboard.h"
  37. #include "core/os/os.h"
  38. #include "core/string/alt_codes.h"
  39. #include "core/string/string_builder.h"
  40. #include "core/string/translation_server.h"
  41. #include "scene/gui/label.h"
  42. #include "scene/main/window.h"
  43. #include "scene/theme/theme_db.h"
  44. ///////////////////////////////////////////////////////////////////////////////
  45. /// TEXT ///
  46. ///////////////////////////////////////////////////////////////////////////////
  47. void TextEdit::Text::set_font(const Ref<Font> &p_font) {
  48. if (font == p_font) {
  49. return;
  50. }
  51. font = p_font;
  52. is_dirty = true;
  53. }
  54. void TextEdit::Text::set_font_size(int p_font_size) {
  55. if (font_size == p_font_size) {
  56. return;
  57. }
  58. font_size = p_font_size;
  59. is_dirty = true;
  60. }
  61. void TextEdit::Text::set_tab_size(int p_tab_size) {
  62. if (tab_size == p_tab_size) {
  63. return;
  64. }
  65. tab_size = p_tab_size;
  66. tab_size_dirty = true;
  67. }
  68. int TextEdit::Text::get_tab_size() const {
  69. return tab_size;
  70. }
  71. void TextEdit::Text::set_indent_wrapped_lines(bool p_enabled) {
  72. if (indent_wrapped_lines == p_enabled) {
  73. return;
  74. }
  75. indent_wrapped_lines = p_enabled;
  76. tab_size_dirty = true;
  77. }
  78. bool TextEdit::Text::is_indent_wrapped_lines() const {
  79. return indent_wrapped_lines;
  80. }
  81. void TextEdit::Text::set_direction_and_language(TextServer::Direction p_direction, const String &p_language) {
  82. if (direction == p_direction && language == p_language) {
  83. return;
  84. }
  85. direction = p_direction;
  86. language = p_language;
  87. is_dirty = true;
  88. }
  89. void TextEdit::Text::set_draw_control_chars(bool p_enabled) {
  90. if (draw_control_chars == p_enabled) {
  91. return;
  92. }
  93. draw_control_chars = p_enabled;
  94. is_dirty = true;
  95. }
  96. void TextEdit::Text::set_inline_object_parser(const Callable &p_parser) {
  97. inline_object_parser = p_parser;
  98. is_dirty = true;
  99. invalidate_all();
  100. }
  101. int TextEdit::Text::get_line_width(int p_line, int p_wrap_index) const {
  102. ERR_FAIL_INDEX_V(p_line, text.size(), 0);
  103. if (p_wrap_index != -1) {
  104. return text[p_line].data_buf->get_line_width(p_wrap_index);
  105. }
  106. return text[p_line].data_buf->get_size().x;
  107. }
  108. int TextEdit::Text::get_max_width() const {
  109. if (max_line_width_dirty) {
  110. int new_max_line_width = 0;
  111. for (const Line &l : text) {
  112. if (l.hidden) {
  113. continue;
  114. }
  115. new_max_line_width = MAX(new_max_line_width, l.width);
  116. }
  117. max_line_width = new_max_line_width;
  118. }
  119. return max_line_width;
  120. }
  121. int TextEdit::Text::get_line_height() const {
  122. if (max_line_height_dirty) {
  123. int new_max_line_height = 0;
  124. for (const Line &l : text) {
  125. if (l.hidden) {
  126. continue;
  127. }
  128. new_max_line_height = MAX(new_max_line_height, l.height);
  129. }
  130. max_line_height = new_max_line_height;
  131. }
  132. return max_line_height;
  133. }
  134. void TextEdit::Text::set_width(float p_width) {
  135. width = p_width;
  136. }
  137. float TextEdit::Text::get_width() const {
  138. return width;
  139. }
  140. void TextEdit::Text::set_brk_flags(BitField<TextServer::LineBreakFlag> p_flags) {
  141. brk_flags = p_flags;
  142. }
  143. BitField<TextServer::LineBreakFlag> TextEdit::Text::get_brk_flags() const {
  144. return brk_flags;
  145. }
  146. int TextEdit::Text::get_line_wrap_amount(int p_line) const {
  147. ERR_FAIL_INDEX_V(p_line, text.size(), 0);
  148. return text[p_line].line_count - 1;
  149. }
  150. Vector<Vector2i> TextEdit::Text::get_line_wrap_ranges(int p_line) const {
  151. Vector<Vector2i> ret;
  152. ERR_FAIL_INDEX_V(p_line, text.size(), ret);
  153. Ref<TextParagraph> data_buf = text[p_line].data_buf;
  154. int line_count = data_buf->get_line_count();
  155. for (int i = 0; i < line_count; i++) {
  156. ret.push_back(data_buf->get_line_range(i));
  157. }
  158. return ret;
  159. }
  160. const Ref<TextParagraph> TextEdit::Text::get_line_data(int p_line) const {
  161. ERR_FAIL_INDEX_V(p_line, text.size(), Ref<TextParagraph>());
  162. return text[p_line].data_buf;
  163. }
  164. float TextEdit::Text::get_indent_offset(int p_line, bool p_rtl) const {
  165. ERR_FAIL_INDEX_V(p_line, text.size(), 0);
  166. Line &text_line = text.write[p_line];
  167. if (text_line.indent_ofs < 0.0) {
  168. int char_count = 0;
  169. int line_length = text_line.data.size();
  170. for (int i = 0; i < line_length - 1; i++) {
  171. if (text_line.data[i] == '\t') {
  172. char_count++;
  173. } else if (text_line.data[i] == ' ') {
  174. char_count++;
  175. } else {
  176. break;
  177. }
  178. }
  179. RID text_rid = text_line.data_buf->get_line_rid(0);
  180. float offset = (p_rtl) ? TS->shaped_text_get_size(text_rid).x : 0;
  181. Vector<Vector2> sel = TS->shaped_text_get_selection(text_rid, 0, char_count);
  182. for (const Vector2 v : sel) {
  183. if (p_rtl) {
  184. offset = MIN(v.x, MIN(v.y, offset));
  185. } else {
  186. offset = MAX(v.x, MAX(v.y, offset));
  187. }
  188. }
  189. text_line.indent_ofs = (p_rtl) ? TS->shaped_text_get_size(text_rid).x - offset : offset;
  190. }
  191. return text_line.indent_ofs;
  192. }
  193. _FORCE_INLINE_ const String &TextEdit::Text::operator[](int p_line) const {
  194. static const String empty;
  195. ERR_FAIL_INDEX_V(p_line, text.size(), empty);
  196. return text[p_line].data;
  197. }
  198. _FORCE_INLINE_ const String &TextEdit::Text::get_text_with_ime(int p_line) const {
  199. if (!text[p_line].ime_data.is_empty()) {
  200. return text[p_line].ime_data;
  201. } else {
  202. return text[p_line].data;
  203. }
  204. }
  205. const Vector<RID> TextEdit::Text::get_accessibility_elements(int p_line) {
  206. ERR_FAIL_INDEX_V(p_line, text.size(), Vector<RID>());
  207. return text[p_line].accessibility_text_root_element;
  208. }
  209. void TextEdit::Text::update_accessibility(int p_line, RID p_root) {
  210. ERR_FAIL_INDEX(p_line, text.size());
  211. Line &l = text.write[p_line];
  212. if (l.accessibility_text_root_element.is_empty()) {
  213. for (int i = 0; i < l.data_buf->get_line_count(); i++) {
  214. RID rid = DisplayServer::get_singleton()->accessibility_create_sub_text_edit_elements(p_root, l.data_buf->get_line_rid(i), max_line_height, p_line);
  215. l.accessibility_text_root_element.push_back(rid);
  216. }
  217. }
  218. }
  219. inline bool is_inline_info_valid(const Variant &p_info) {
  220. if (p_info.get_type() != Variant::DICTIONARY) {
  221. return false;
  222. }
  223. Dictionary info = p_info;
  224. if (!info.get_valid("column").is_num() || !info.get_valid("width_ratio").is_num()) {
  225. return false;
  226. }
  227. return true;
  228. }
  229. void TextEdit::Text::invalidate_cache(int p_line, bool p_text_changed) {
  230. ERR_FAIL_INDEX(p_line, text.size());
  231. Line &l = text.write[p_line];
  232. for (const RID rid : l.accessibility_text_root_element) {
  233. if (rid.is_valid()) {
  234. DisplayServer::get_singleton()->accessibility_free_element(rid);
  235. }
  236. }
  237. l.accessibility_text_root_element.clear();
  238. if (font.is_null()) {
  239. return; // Not in tree?
  240. }
  241. Line &text_line = text.write[p_line];
  242. if (p_text_changed) {
  243. text_line.data_buf->clear();
  244. }
  245. BitField<TextServer::LineBreakFlag> flags = brk_flags;
  246. if (indent_wrapped_lines) {
  247. flags.set_flag(TextServer::BREAK_TRIM_INDENT);
  248. }
  249. text_line.data_buf->set_width(width);
  250. text_line.data_buf->set_direction((TextServer::Direction)direction);
  251. text_line.data_buf->set_break_flags(flags);
  252. text_line.data_buf->set_preserve_control(draw_control_chars);
  253. text_line.data_buf->set_custom_punctuation(get_enabled_word_separators());
  254. text_line.indent_ofs = -1.0;
  255. const String &text_with_ime = (!text_line.ime_data.is_empty()) ? text_line.ime_data : text_line.data;
  256. const Array &bidi_override_with_ime = (!text_line.ime_data.is_empty()) ? text_line.ime_bidi_override : text_line.bidi_override;
  257. if (p_text_changed) {
  258. int from = 0;
  259. if (inline_object_parser.is_valid()) {
  260. // Insert inline object.
  261. Variant parsed_result = inline_object_parser.call(text_with_ime);
  262. if (parsed_result.is_array()) {
  263. Array object_infos = parsed_result;
  264. for (Variant val : object_infos) {
  265. if (!is_inline_info_valid(val)) {
  266. continue;
  267. }
  268. Dictionary info = val;
  269. int start = info["column"];
  270. float width_ratio = info["width_ratio"];
  271. String left_string = text_with_ime.substr(from, start - from);
  272. text_line.data_buf->add_string(left_string, font, font_size, language);
  273. text_line.data_buf->add_object(info, Vector2(font_height * width_ratio, font_height), INLINE_ALIGNMENT_TOP, 0);
  274. from = start;
  275. }
  276. }
  277. }
  278. String remaining_string = text_with_ime.substr(from);
  279. text_line.data_buf->add_string(remaining_string, font, font_size, language);
  280. } else {
  281. // Update inline object sizes.
  282. for (int i = 0; i < text_line.data_buf->get_line_count(); i++) {
  283. for (Variant key : text_line.data_buf->get_line_objects(i)) {
  284. if (!is_inline_info_valid(key)) {
  285. continue;
  286. }
  287. Dictionary info = key;
  288. float width_ratio = info["width_ratio"];
  289. text_line.data_buf->resize_object(info, Vector2(font_height * width_ratio, font_height), INLINE_ALIGNMENT_TOP, 0);
  290. }
  291. }
  292. }
  293. if (!bidi_override_with_ime.is_empty()) {
  294. TS->shaped_text_set_bidi_override(text_line.data_buf->get_rid(), bidi_override_with_ime);
  295. }
  296. if (!p_text_changed) {
  297. RID r = text_line.data_buf->get_rid();
  298. int spans = TS->shaped_get_span_count(r);
  299. for (int i = 0; i < spans; i++) {
  300. TS->shaped_set_span_update_font(r, i, font->get_rids(), font_size, font->get_opentype_features());
  301. }
  302. }
  303. // Apply tab align.
  304. if (tab_size > 0) {
  305. Vector<float> tabs;
  306. tabs.push_back(font->get_char_size(' ', font_size).width * tab_size);
  307. text_line.data_buf->tab_align(tabs);
  308. }
  309. // Update wrap amount.
  310. const int old_line_count = text_line.line_count;
  311. text_line.line_count = text_line.data_buf->get_line_count();
  312. if (!text_line.hidden && text_line.line_count != old_line_count) {
  313. total_visible_line_count += text_line.line_count - old_line_count;
  314. }
  315. // Update height.
  316. const int old_height = text_line.height;
  317. text_line.height = font_height;
  318. for (int i = 0; i < text_line.line_count; i++) {
  319. text_line.height = MAX(text_line.height, text_line.data_buf->get_line_size(i).y);
  320. }
  321. // If this line has shrunk, this may no longer be the tallest line.
  322. if (!text_line.hidden) {
  323. if (old_height == max_line_height && text_line.height < old_height) {
  324. max_line_height_dirty = true;
  325. } else {
  326. max_line_height = MAX(text_line.height, max_line_height);
  327. }
  328. }
  329. // Update width.
  330. const int old_width = text_line.width;
  331. text_line.width = get_line_width(p_line);
  332. if (!text_line.hidden) {
  333. // If this line has shrunk, this may no longer be the longest line.
  334. if (old_width == max_line_width && text_line.width < old_width) {
  335. max_line_width_dirty = true;
  336. } else {
  337. max_line_width = MAX(text_line.width, max_line_width);
  338. }
  339. }
  340. }
  341. void TextEdit::Text::invalidate_all_lines() {
  342. for (int i = 0; i < text.size(); i++) {
  343. if (tab_size_dirty) {
  344. if (tab_size > 0) {
  345. Vector<float> tabs;
  346. tabs.push_back(font->get_char_size(' ', font_size).width * tab_size);
  347. text[i].data_buf->tab_align(tabs);
  348. }
  349. }
  350. invalidate_cache(i, false);
  351. }
  352. tab_size_dirty = false;
  353. }
  354. void TextEdit::Text::invalidate_font() {
  355. if (!is_dirty) {
  356. return;
  357. }
  358. max_line_width_dirty = true;
  359. max_line_height_dirty = true;
  360. if (font.is_valid() && font_size > 0) {
  361. font_height = font->get_height(font_size);
  362. }
  363. for (int i = 0; i < text.size(); i++) {
  364. invalidate_cache(i, false);
  365. }
  366. is_dirty = false;
  367. }
  368. void TextEdit::Text::invalidate_all() {
  369. if (!is_dirty) {
  370. return;
  371. }
  372. max_line_width_dirty = true;
  373. max_line_height_dirty = true;
  374. if (font.is_valid() && font_size > 0) {
  375. font_height = font->get_height(font_size);
  376. }
  377. for (int i = 0; i < text.size(); i++) {
  378. invalidate_cache(i, true);
  379. }
  380. is_dirty = false;
  381. }
  382. void TextEdit::Text::clear() {
  383. text.clear();
  384. max_line_width_dirty = true;
  385. max_line_height_dirty = true;
  386. total_visible_line_count = 0;
  387. Line line;
  388. line.gutters.resize(gutter_count);
  389. text.insert(0, line);
  390. invalidate_cache(0, true);
  391. }
  392. int TextEdit::Text::get_total_visible_line_count() const {
  393. return total_visible_line_count;
  394. }
  395. void TextEdit::Text::set(int p_line, const String &p_text, const Array &p_bidi_override) {
  396. ERR_FAIL_INDEX(p_line, text.size());
  397. text.write[p_line].data = p_text;
  398. text.write[p_line].ime_data = String();
  399. text.write[p_line].bidi_override = p_bidi_override;
  400. text.write[p_line].ime_bidi_override.clear();
  401. invalidate_cache(p_line, true);
  402. }
  403. void TextEdit::Text::set_ime(int p_line, const String &p_text, const Array &p_bidi_override) {
  404. ERR_FAIL_INDEX(p_line, text.size());
  405. text.write[p_line].ime_data = p_text;
  406. text.write[p_line].ime_bidi_override = p_bidi_override;
  407. invalidate_cache(p_line, true);
  408. }
  409. void TextEdit::Text::set_hidden(int p_line, bool p_hidden) {
  410. ERR_FAIL_INDEX(p_line, text.size());
  411. Line &text_line = text.write[p_line];
  412. if (text_line.hidden == p_hidden) {
  413. return;
  414. }
  415. text_line.hidden = p_hidden;
  416. if (p_hidden) {
  417. total_visible_line_count -= text_line.line_count;
  418. if (text_line.width == max_line_width) {
  419. max_line_width_dirty = true;
  420. }
  421. if (text_line.height == max_line_height) {
  422. max_line_height_dirty = true;
  423. }
  424. } else {
  425. total_visible_line_count += text_line.line_count;
  426. max_line_width = MAX(text_line.width, max_line_width);
  427. max_line_height = MAX(text_line.height, max_line_height);
  428. }
  429. }
  430. bool TextEdit::Text::is_hidden(int p_line) const {
  431. ERR_FAIL_INDEX_V(p_line, text.size(), true);
  432. return text[p_line].hidden;
  433. }
  434. void TextEdit::Text::insert(int p_at, const Vector<String> &p_text, const Vector<Array> &p_bidi_override) {
  435. ERR_FAIL_INDEX(p_at, text.size() + 1);
  436. int new_line_count = p_text.size() - 1;
  437. if (new_line_count > 0) {
  438. text.resize(text.size() + new_line_count);
  439. for (int i = (text.size() - 1); i > p_at; i--) {
  440. if ((i - new_line_count) <= 0) {
  441. break;
  442. }
  443. text.write[i] = text[i - new_line_count];
  444. }
  445. }
  446. for (int i = 0; i < p_text.size(); i++) {
  447. if (i == 0) {
  448. set(p_at + i, p_text[i], p_bidi_override[i]);
  449. continue;
  450. }
  451. Line line;
  452. line.gutters.resize(gutter_count);
  453. line.data = p_text[i];
  454. line.bidi_override = p_bidi_override[i];
  455. text.write[p_at + i] = line;
  456. invalidate_cache(p_at + i, true);
  457. }
  458. }
  459. void TextEdit::Text::remove_range(int p_from_line, int p_to_line) {
  460. if (p_from_line == p_to_line) {
  461. return;
  462. }
  463. for (int i = p_from_line + 1; i <= p_to_line; i++) {
  464. const Line &text_line = text[i];
  465. if (text_line.hidden) {
  466. continue;
  467. }
  468. if (text_line.height == max_line_height) {
  469. max_line_height_dirty = true;
  470. }
  471. if (text_line.width == max_line_width) {
  472. max_line_width_dirty = true;
  473. }
  474. total_visible_line_count -= text_line.line_count;
  475. }
  476. int diff = p_to_line - p_from_line;
  477. for (int i = p_to_line + 1; i < text.size(); i++) {
  478. text.write[i - diff] = text[i];
  479. }
  480. text.resize(text.size() - diff);
  481. ERR_FAIL_COND(total_visible_line_count < 0); // BUG
  482. }
  483. void TextEdit::Text::add_gutter(int p_at) {
  484. for (int i = 0; i < text.size(); i++) {
  485. if (p_at < 0 || p_at > gutter_count) {
  486. text.write[i].gutters.push_back(Gutter());
  487. } else {
  488. text.write[i].gutters.insert(p_at, Gutter());
  489. }
  490. }
  491. gutter_count++;
  492. }
  493. void TextEdit::Text::remove_gutter(int p_gutter) {
  494. ERR_FAIL_INDEX(p_gutter, text.size());
  495. for (int i = 0; i < text.size(); i++) {
  496. text.write[i].gutters.remove_at(p_gutter);
  497. }
  498. gutter_count--;
  499. }
  500. void TextEdit::Text::move_gutters(int p_from_line, int p_to_line) {
  501. ERR_FAIL_INDEX(p_from_line, text.size());
  502. ERR_FAIL_INDEX(p_to_line, text.size());
  503. text.write[p_to_line].gutters = text[p_from_line].gutters;
  504. text.write[p_from_line].gutters.clear();
  505. text.write[p_from_line].gutters.resize(gutter_count);
  506. }
  507. void TextEdit::Text::set_use_default_word_separators(bool p_enabled) {
  508. if (use_default_word_separators == p_enabled) {
  509. return;
  510. }
  511. use_default_word_separators = p_enabled;
  512. invalidate_all_lines();
  513. }
  514. void TextEdit::Text::set_use_custom_word_separators(bool p_enabled) {
  515. if (use_custom_word_separators == p_enabled) {
  516. return;
  517. }
  518. use_custom_word_separators = p_enabled;
  519. invalidate_all_lines();
  520. }
  521. bool TextEdit::Text::is_default_word_separators_enabled() const {
  522. return use_default_word_separators;
  523. }
  524. bool TextEdit::Text::is_custom_word_separators_enabled() const {
  525. return use_custom_word_separators;
  526. }
  527. String TextEdit::Text::get_custom_word_separators() const {
  528. return custom_word_separators;
  529. }
  530. String TextEdit::Text::get_default_word_separators() const {
  531. String concat_separators = "!\"#$%&'()*+,-./:;<=>?@[\\]^`{|}~";
  532. for (char32_t ch = 0x2000; ch <= 0x206F; ++ch) { // General punctuation block.
  533. concat_separators += ch;
  534. }
  535. for (char32_t ch = 0x3000; ch <= 0x303F; ++ch) { // CJK punctuation block.
  536. concat_separators += ch;
  537. }
  538. return concat_separators;
  539. }
  540. // Get default and/or custom word separators depending on the option enabled.
  541. String TextEdit::Text::get_enabled_word_separators() const {
  542. String all_separators;
  543. if (use_default_word_separators) {
  544. all_separators += get_default_word_separators();
  545. }
  546. if (use_custom_word_separators) {
  547. all_separators += get_custom_word_separators();
  548. }
  549. return all_separators;
  550. }
  551. ///////////////////////////////////////////////////////////////////////////////
  552. /// TEXT EDIT ///
  553. ///////////////////////////////////////////////////////////////////////////////
  554. void TextEdit::_accessibility_action_set_selection(const Variant &p_data) {
  555. Dictionary new_selection = p_data;
  556. RID sel_start = new_selection["start_element"];
  557. Vector2i sel_start_line = DisplayServer::get_singleton()->accessibility_element_get_meta(sel_start);
  558. int sel_start_pos = new_selection["start_char"];
  559. RID sel_end = new_selection["end_element"];
  560. Vector2i sel_end_line = DisplayServer::get_singleton()->accessibility_element_get_meta(sel_end);
  561. int sel_end_pos = new_selection["end_char"];
  562. remove_secondary_carets();
  563. select(sel_start_line.x, sel_start_pos, sel_end_line.x, sel_end_pos, 0);
  564. }
  565. void TextEdit::_accessibility_action_replace_selected(const Variant &p_data) {
  566. String new_text = p_data;
  567. insert_text_at_caret(new_text);
  568. }
  569. void TextEdit::_accessibility_action_set_value(const Variant &p_data) {
  570. String new_text = p_data;
  571. set_text(new_text);
  572. }
  573. void TextEdit::_accessibility_action_menu(const Variant &p_data) {
  574. if (context_menu_enabled) {
  575. _update_context_menu();
  576. adjust_viewport_to_caret();
  577. menu->set_position(get_screen_position() + get_caret_draw_pos());
  578. menu->reset_size();
  579. menu->popup();
  580. menu->grab_focus();
  581. }
  582. }
  583. void TextEdit::_accessibility_scroll_down(const Variant &p_data) {
  584. v_scroll->set_value(v_scroll->get_value() + v_scroll->get_page() / 4);
  585. queue_accessibility_update();
  586. }
  587. void TextEdit::_accessibility_scroll_left(const Variant &p_data) {
  588. h_scroll->set_value(h_scroll->get_value() - h_scroll->get_page() / 4);
  589. queue_accessibility_update();
  590. }
  591. void TextEdit::_accessibility_scroll_right(const Variant &p_data) {
  592. h_scroll->set_value(h_scroll->get_value() + h_scroll->get_page() / 4);
  593. queue_accessibility_update();
  594. }
  595. void TextEdit::_accessibility_scroll_up(const Variant &p_data) {
  596. v_scroll->set_value(v_scroll->get_value() - v_scroll->get_page() / 4);
  597. queue_accessibility_update();
  598. }
  599. void TextEdit::_accessibility_scroll_set(const Variant &p_data) {
  600. const Point2 &pos = p_data;
  601. h_scroll->set_value(pos.x);
  602. v_scroll->set_value(pos.y);
  603. queue_accessibility_update();
  604. }
  605. void TextEdit::_accessibility_action_scroll_into_view(const Variant &p_data, int p_line, int p_wrap) {
  606. double delta = get_scroll_pos_for_line(p_line, p_wrap) - get_v_scroll();
  607. if (delta < 0) {
  608. _scroll_up(-delta, false);
  609. } else {
  610. _scroll_down(delta, false);
  611. }
  612. }
  613. void TextEdit::_notification(int p_what) {
  614. switch (p_what) {
  615. case NOTIFICATION_EXIT_TREE:
  616. case NOTIFICATION_ACCESSIBILITY_INVALIDATE: {
  617. text.clear_accessibility();
  618. accessibility_text_root_element_nl = RID();
  619. } break;
  620. case NOTIFICATION_ACCESSIBILITY_UPDATE: {
  621. RID ae = get_accessibility_element();
  622. ERR_FAIL_COND(ae.is_null());
  623. DisplayServer::get_singleton()->accessibility_update_set_role(ae, DisplayServer::AccessibilityRole::ROLE_MULTILINE_TEXT_FIELD);
  624. if (text.size() == 1 && text[0].is_empty()) {
  625. DisplayServer::get_singleton()->accessibility_update_set_placeholder(ae, atr(placeholder_text));
  626. }
  627. if (!placeholder_text.is_empty() && get_accessibility_name().is_empty()) {
  628. DisplayServer::get_singleton()->accessibility_update_set_name(ae, atr(placeholder_text));
  629. }
  630. DisplayServer::get_singleton()->accessibility_update_set_flag(ae, DisplayServer::AccessibilityFlags::FLAG_READONLY, !editable);
  631. DisplayServer::get_singleton()->accessibility_update_add_action(ae, DisplayServer::AccessibilityAction::ACTION_SET_TEXT_SELECTION, callable_mp(this, &TextEdit::_accessibility_action_set_selection));
  632. DisplayServer::get_singleton()->accessibility_update_add_action(ae, DisplayServer::AccessibilityAction::ACTION_REPLACE_SELECTED_TEXT, callable_mp(this, &TextEdit::_accessibility_action_replace_selected));
  633. DisplayServer::get_singleton()->accessibility_update_add_action(ae, DisplayServer::AccessibilityAction::ACTION_SET_VALUE, callable_mp(this, &TextEdit::_accessibility_action_set_value));
  634. DisplayServer::get_singleton()->accessibility_update_add_action(ae, DisplayServer::AccessibilityAction::ACTION_SHOW_CONTEXT_MENU, callable_mp(this, &TextEdit::_accessibility_action_menu));
  635. DisplayServer::get_singleton()->accessibility_update_add_action(ae, DisplayServer::AccessibilityAction::ACTION_SCROLL_DOWN, callable_mp(this, &TextEdit::_accessibility_scroll_down));
  636. DisplayServer::get_singleton()->accessibility_update_add_action(ae, DisplayServer::AccessibilityAction::ACTION_SCROLL_LEFT, callable_mp(this, &TextEdit::_accessibility_scroll_left));
  637. DisplayServer::get_singleton()->accessibility_update_add_action(ae, DisplayServer::AccessibilityAction::ACTION_SCROLL_RIGHT, callable_mp(this, &TextEdit::_accessibility_scroll_right));
  638. DisplayServer::get_singleton()->accessibility_update_add_action(ae, DisplayServer::AccessibilityAction::ACTION_SCROLL_UP, callable_mp(this, &TextEdit::_accessibility_scroll_up));
  639. DisplayServer::get_singleton()->accessibility_update_add_action(ae, DisplayServer::AccessibilityAction::ACTION_SET_SCROLL_OFFSET, callable_mp(this, &TextEdit::_accessibility_scroll_set));
  640. int first_vis_line = get_first_visible_line();
  641. int row_height = get_line_height();
  642. int xmargin_beg = Math::ceil(theme_cache.style_normal->get_margin(SIDE_LEFT)) + gutters_width + gutter_padding;
  643. Size2 size = get_size();
  644. bool rtl = is_layout_rtl();
  645. int lines_drawn = 0;
  646. RID selection_start;
  647. RID selection_end;
  648. for (int i = 0; i < text.size(); i++) {
  649. text.update_accessibility(i, ae);
  650. const Ref<TextParagraph> &ac_buf = text.get_line_data(i);
  651. const Vector<RID> &text_aes = text.get_accessibility_elements(i);
  652. int first_indent_line = 0;
  653. float indent_ofs = 0.0;
  654. if (text.is_indent_wrapped_lines()) {
  655. _get_wrapped_indent_level(i, first_indent_line);
  656. indent_ofs = MIN(text.get_indent_offset(i, rtl), wrap_at_column * 0.6);
  657. }
  658. for (int j = 0; j < text_aes.size(); j++) {
  659. float text_off_x = 0.0;
  660. float text_off_y = 0.0;
  661. if (!editable) {
  662. text_off_x = theme_cache.style_readonly->get_offset().x / 2;
  663. text_off_x -= theme_cache.style_normal->get_offset().x / 2;
  664. text_off_y = theme_cache.style_readonly->get_offset().y / 2;
  665. } else {
  666. text_off_y = theme_cache.style_normal->get_offset().y / 2;
  667. }
  668. text_off_y += (lines_drawn + j) * row_height + theme_cache.line_spacing / 2;
  669. text_off_y -= (first_vis_line + first_visible_line_wrap_ofs) * row_height;
  670. text_off_y -= _get_v_scroll_offset() * row_height;
  671. float wrap_indent = j > first_indent_line ? indent_ofs : 0.0;
  672. int char_margin = xmargin_beg - first_visible_col;
  673. if (rtl) {
  674. char_margin = size.width - char_margin - ac_buf->get_line_width(j) - wrap_indent;
  675. } else {
  676. char_margin += wrap_indent;
  677. }
  678. DisplayServer::get_singleton()->accessibility_update_set_flag(text_aes[j], DisplayServer::AccessibilityFlags::FLAG_HIDDEN, _is_line_hidden(i));
  679. Transform2D tr;
  680. tr.set_origin(Point2(char_margin + text_off_x, text_off_y));
  681. DisplayServer::get_singleton()->accessibility_update_set_transform(text_aes[j], tr);
  682. DisplayServer::get_singleton()->accessibility_update_set_name(text_aes[j], vformat(RTR("Line %d"), i));
  683. DisplayServer::get_singleton()->accessibility_element_set_meta(text_aes[j], Vector2i(i, j));
  684. DisplayServer::get_singleton()->accessibility_update_add_action(text_aes[j], DisplayServer::AccessibilityAction::ACTION_SCROLL_INTO_VIEW, callable_mp(this, &TextEdit::_accessibility_action_scroll_into_view).bind(i, j));
  685. }
  686. lines_drawn += ac_buf->get_line_count();
  687. }
  688. if (accessibility_text_root_element_nl.is_null()) {
  689. accessibility_text_root_element_nl = DisplayServer::get_singleton()->accessibility_create_sub_text_edit_elements(ae, RID(), get_line_height());
  690. }
  691. // Selection.
  692. if (carets.size() > 0) {
  693. if (carets[0].selection.active) {
  694. int start_wrap = get_line_wrap_index_at_column(carets[0].selection.origin_line, carets[0].selection.origin_column);
  695. RID start_rid = text.get_accessibility_elements(carets[0].selection.origin_line)[start_wrap];
  696. int end_wrap = get_line_wrap_index_at_column(carets[0].line, carets[0].column);
  697. RID end_rid = text.get_accessibility_elements(carets[0].line)[end_wrap];
  698. DisplayServer::get_singleton()->accessibility_update_set_text_selection(ae, start_rid, carets[0].selection.origin_column, end_rid, carets[0].column);
  699. } else {
  700. int caret_wrap = get_line_wrap_index_at_column(carets[0].line, carets[0].column);
  701. RID caret_rid = text.get_accessibility_elements(carets[0].line)[caret_wrap];
  702. DisplayServer::get_singleton()->accessibility_update_set_text_selection(ae, caret_rid, carets[0].column, caret_rid, carets[0].column);
  703. }
  704. }
  705. } break;
  706. case NOTIFICATION_POSTINITIALIZE: {
  707. _update_caches();
  708. } break;
  709. case NOTIFICATION_ENTER_TREE: {
  710. _update_caches();
  711. if (caret_pos_dirty) {
  712. callable_mp(this, &TextEdit::_emit_caret_changed).call_deferred();
  713. }
  714. if (text_changed_dirty) {
  715. callable_mp(this, &TextEdit::_emit_text_changed).call_deferred();
  716. }
  717. _update_wrap_at_column(true);
  718. } break;
  719. case NOTIFICATION_RESIZED: {
  720. _update_scrollbars();
  721. _update_wrap_at_column();
  722. } break;
  723. case NOTIFICATION_VISIBILITY_CHANGED: {
  724. if (is_visible()) {
  725. callable_mp(this, &TextEdit::_update_scrollbars).call_deferred();
  726. callable_mp(this, &TextEdit::_update_wrap_at_column).call_deferred(false);
  727. }
  728. } break;
  729. case NOTIFICATION_LAYOUT_DIRECTION_CHANGED:
  730. case NOTIFICATION_TRANSLATION_CHANGED:
  731. case NOTIFICATION_THEME_CHANGED: {
  732. if (is_inside_tree()) {
  733. _update_caches();
  734. _update_wrap_at_column(true);
  735. }
  736. } break;
  737. case NOTIFICATION_WM_WINDOW_FOCUS_IN: {
  738. window_has_focus = true;
  739. draw_caret = true;
  740. queue_redraw();
  741. } break;
  742. case NOTIFICATION_WM_WINDOW_FOCUS_OUT: {
  743. window_has_focus = false;
  744. draw_caret = false;
  745. queue_redraw();
  746. } break;
  747. case NOTIFICATION_INTERNAL_PROCESS: {
  748. if (scrolling && get_v_scroll() != target_v_scroll) {
  749. double target_y = target_v_scroll - get_v_scroll();
  750. double dist = std::abs(target_y);
  751. // To ensure minimap is responsive override the speed setting.
  752. double vel = ((target_y / dist) * ((minimap_clicked) ? 3000 : v_scroll_speed)) * get_process_delta_time();
  753. // Prevent small velocities from blocking scrolling.
  754. if (Math::abs(vel) < v_scroll->get_step()) {
  755. vel = v_scroll->get_step() * SIGN(vel);
  756. }
  757. if (Math::abs(vel) >= dist) {
  758. set_v_scroll(target_v_scroll);
  759. scrolling = false;
  760. minimap_clicked = false;
  761. set_process_internal(false);
  762. } else {
  763. set_v_scroll(get_v_scroll() + vel);
  764. }
  765. } else {
  766. scrolling = false;
  767. minimap_clicked = false;
  768. set_process_internal(false);
  769. }
  770. } break;
  771. case NOTIFICATION_DRAW: {
  772. if (first_draw) {
  773. // Size may not be the final one, so attempts to ensure caret was visible may have failed.
  774. adjust_viewport_to_caret();
  775. first_draw = false;
  776. }
  777. /* Prevent the resource getting lost between the editor and game. */
  778. if (Engine::get_singleton()->is_editor_hint()) {
  779. if (syntax_highlighter.is_valid() && syntax_highlighter->get_text_edit() != this) {
  780. syntax_highlighter->set_text_edit(this);
  781. }
  782. }
  783. Size2 size = get_size();
  784. bool rtl = is_layout_rtl();
  785. if ((!has_focus() && !(menu && menu->has_focus())) || !window_has_focus) {
  786. draw_caret = false;
  787. }
  788. _update_scrollbars();
  789. RID ci = get_canvas_item();
  790. int xmargin_beg = Math::ceil(theme_cache.style_normal->get_margin(SIDE_LEFT)) + gutters_width + gutter_padding;
  791. int xmargin_end = size.width - Math::ceil(theme_cache.style_normal->get_margin(SIDE_RIGHT));
  792. if (draw_minimap) {
  793. xmargin_end -= minimap_width;
  794. }
  795. // Let's do it easy for now.
  796. theme_cache.style_normal->draw(ci, Rect2(Point2(), size));
  797. if (!editable) {
  798. theme_cache.style_readonly->draw(ci, Rect2(Point2(), size));
  799. draw_caret = is_drawing_caret_when_editable_disabled();
  800. }
  801. if (has_focus()) {
  802. theme_cache.style_focus->draw(ci, Rect2(Point2(), size));
  803. }
  804. int visible_rows = get_visible_line_count() + 1;
  805. if (theme_cache.background_color.a > 0.01) {
  806. RenderingServer::get_singleton()->canvas_item_add_rect(ci, Rect2(Point2i(), get_size()), theme_cache.background_color);
  807. }
  808. Vector<BraceMatchingData> brace_matching;
  809. if (highlight_matching_braces_enabled) {
  810. brace_matching.resize(get_caret_count());
  811. for (int caret = 0; caret < get_caret_count(); caret++) {
  812. BraceMatchingData &brace_match = brace_matching.write[caret];
  813. if (get_caret_line(caret) < 0 || get_caret_line(caret) >= text.size() || get_caret_column(caret) < 0) {
  814. continue;
  815. }
  816. if (get_caret_column(caret) < text[get_caret_line(caret)].length()) {
  817. // Check for open.
  818. char32_t c = text[get_caret_line(caret)][get_caret_column(caret)];
  819. char32_t closec = 0;
  820. if (c == '[') {
  821. closec = ']';
  822. } else if (c == '{') {
  823. closec = '}';
  824. } else if (c == '(') {
  825. closec = ')';
  826. }
  827. if (closec != 0) {
  828. int stack = 1;
  829. for (int i = get_caret_line(caret); i < text.size(); i++) {
  830. int from = i == get_caret_line(caret) ? get_caret_column(caret) + 1 : 0;
  831. for (int j = from; j < text[i].length(); j++) {
  832. char32_t cc = text[i][j];
  833. // Ignore any brackets inside a string.
  834. if (cc == '"' || cc == '\'') {
  835. char32_t quotation = cc;
  836. do {
  837. j++;
  838. if (!(j < text[i].length())) {
  839. break;
  840. }
  841. cc = text[i][j];
  842. // Skip over escaped quotation marks inside strings.
  843. if (cc == '\\') {
  844. bool escaped = true;
  845. while (j + 1 < text[i].length() && text[i][j + 1] == '\\') {
  846. escaped = !escaped;
  847. j++;
  848. }
  849. if (escaped) {
  850. j++;
  851. continue;
  852. }
  853. }
  854. } while (cc != quotation);
  855. } else if (cc == c) {
  856. stack++;
  857. } else if (cc == closec) {
  858. stack--;
  859. }
  860. if (stack == 0) {
  861. brace_match.open_match_line = i;
  862. brace_match.open_match_column = j;
  863. brace_match.open_matching = true;
  864. break;
  865. }
  866. }
  867. if (brace_match.open_match_line != -1) {
  868. break;
  869. }
  870. }
  871. if (!brace_match.open_matching) {
  872. brace_match.open_mismatch = true;
  873. }
  874. }
  875. }
  876. if (get_caret_column(caret) > 0) {
  877. char32_t c = text[get_caret_line(caret)][get_caret_column(caret) - 1];
  878. char32_t closec = 0;
  879. if (c == ']') {
  880. closec = '[';
  881. } else if (c == '}') {
  882. closec = '{';
  883. } else if (c == ')') {
  884. closec = '(';
  885. }
  886. if (closec != 0) {
  887. int stack = 1;
  888. for (int i = get_caret_line(caret); i >= 0; i--) {
  889. int from = i == get_caret_line(caret) ? get_caret_column(caret) - 2 : text[i].length() - 1;
  890. for (int j = from; j >= 0; j--) {
  891. char32_t cc = text[i][j];
  892. // Ignore any brackets inside a string.
  893. if (cc == '"' || cc == '\'') {
  894. char32_t quotation = cc;
  895. do {
  896. j--;
  897. if (!(j >= 0)) {
  898. break;
  899. }
  900. cc = text[i][j];
  901. // Skip over escaped quotation marks inside strings.
  902. if (cc == quotation) {
  903. bool escaped = false;
  904. while (j - 1 >= 0 && text[i][j - 1] == '\\') {
  905. escaped = !escaped;
  906. j--;
  907. }
  908. if (escaped) {
  909. cc = '\\';
  910. continue;
  911. }
  912. }
  913. } while (cc != quotation);
  914. } else if (cc == c) {
  915. stack++;
  916. } else if (cc == closec) {
  917. stack--;
  918. }
  919. if (stack == 0) {
  920. brace_match.close_match_line = i;
  921. brace_match.close_match_column = j;
  922. brace_match.close_matching = true;
  923. break;
  924. }
  925. }
  926. if (brace_match.close_match_line != -1) {
  927. break;
  928. }
  929. }
  930. if (!brace_match.close_matching) {
  931. brace_match.close_mismatch = true;
  932. }
  933. }
  934. }
  935. }
  936. }
  937. bool draw_placeholder = _using_placeholder();
  938. // Get the highlighted words.
  939. String highlighted_text = get_selected_text(0);
  940. // Check if highlighted words contain only whitespaces (tabs or spaces).
  941. bool only_whitespaces_highlighted = highlighted_text.strip_edges().is_empty();
  942. Vector<Pair<int, int>> highlighted_lines;
  943. highlighted_lines.resize(carets.size());
  944. Vector<int> carets_wrap_index;
  945. carets_wrap_index.resize(carets.size());
  946. for (int i = 0; i < carets.size(); i++) {
  947. carets.write[i].visible = false;
  948. int wrap_index = get_caret_wrap_index(i);
  949. highlighted_lines.write[i] = Pair<int, int>(get_caret_line(i), wrap_index);
  950. carets_wrap_index.write[i] = wrap_index;
  951. }
  952. int first_vis_line = get_first_visible_line() - 1;
  953. int draw_amount = visible_rows + 1;
  954. draw_amount += draw_placeholder ? placeholder_wrapped_rows.size() - 1 : get_line_wrap_count(first_vis_line + 1);
  955. // Draw minimap.
  956. if (draw_minimap) {
  957. int minimap_visible_lines = get_minimap_visible_lines();
  958. int minimap_line_height = (minimap_char_size.y + minimap_line_spacing);
  959. int tab_size = text.get_tab_size();
  960. // Calculate viewport size and y offset.
  961. int viewport_height = (draw_amount - 1) * minimap_line_height;
  962. int control_height = _get_control_height() - viewport_height;
  963. int viewport_offset_y = std::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));
  964. // Calculate the first line.
  965. int num_lines_before = std::round((viewport_offset_y) / minimap_line_height);
  966. int minimap_line = (v_scroll->get_max() <= minimap_visible_lines) ? -1 : first_vis_line;
  967. if (minimap_line >= 0) {
  968. minimap_line -= get_next_visible_line_index_offset_from(first_vis_line, 0, -num_lines_before).x;
  969. minimap_line -= (minimap_line > 0 && smooth_scroll_enabled ? 1 : 0);
  970. }
  971. int minimap_draw_amount = minimap_visible_lines + get_line_wrap_count(minimap_line + 1);
  972. // Draw the minimap.
  973. // Add visual feedback when dragging or hovering the visible area rectangle.
  974. Color viewport_color = theme_cache.caret_color;
  975. if (dragging_minimap) {
  976. viewport_color.a = 0.25;
  977. } else if (hovering_minimap) {
  978. viewport_color.a = 0.175;
  979. } else {
  980. viewport_color.a = 0.1;
  981. }
  982. if (rtl) {
  983. 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);
  984. } else {
  985. RenderingServer::get_singleton()->canvas_item_add_rect(ci, Rect2((xmargin_end + 2), viewport_offset_y, minimap_width, viewport_height), viewport_color);
  986. }
  987. for (int i = 0; i < minimap_draw_amount; i++) {
  988. minimap_line++;
  989. if (minimap_line < 0 || minimap_line >= (int)text.size()) {
  990. break;
  991. }
  992. while (_is_line_hidden(minimap_line)) {
  993. minimap_line++;
  994. if (minimap_line < 0 || minimap_line >= (int)text.size()) {
  995. break;
  996. }
  997. }
  998. if (minimap_line < 0 || minimap_line >= (int)text.size()) {
  999. break;
  1000. }
  1001. const Vector<Pair<int64_t, Color>> color_map = _get_line_syntax_highlighting(minimap_line);
  1002. Color line_background_color = text.get_line_background_color(minimap_line);
  1003. // Make non-default background colors more visible, such as error markers.
  1004. // If a line background color is being applied, like in an error marker, the alpha is set to 1.0.
  1005. // Else, it stays zero.
  1006. line_background_color.a = 1.0 * (line_background_color != Color(0, 0, 0, 0));
  1007. Color current_color = editable ? theme_cache.font_color : theme_cache.font_readonly_color;
  1008. const Vector<String> wrap_rows = get_line_wrapped_text(minimap_line);
  1009. int line_wrap_amount = get_line_wrap_count(minimap_line);
  1010. int last_wrap_column = 0;
  1011. int first_indent_line = 0;
  1012. float wrap_indent_line = 0.0;
  1013. if (text.is_indent_wrapped_lines()) {
  1014. wrap_indent_line = _get_wrapped_indent_level(minimap_line, first_indent_line);
  1015. wrap_indent_line = MIN(wrap_indent_line, (minimap_width / minimap_char_size.x) * 0.6);
  1016. }
  1017. for (int line_wrap_index = 0; line_wrap_index < line_wrap_amount + 1; line_wrap_index++) {
  1018. if (line_wrap_index != 0) {
  1019. i++;
  1020. if (i >= minimap_draw_amount) {
  1021. break;
  1022. }
  1023. }
  1024. const String &str = wrap_rows[line_wrap_index];
  1025. int indent_px = line_wrap_index > first_indent_line ? wrap_indent_line : 0.0;
  1026. if (indent_px >= wrap_at_column) {
  1027. indent_px = 0;
  1028. }
  1029. indent_px = minimap_char_size.x * indent_px;
  1030. if (line_wrap_index > 0) {
  1031. last_wrap_column += wrap_rows[line_wrap_index - 1].length();
  1032. }
  1033. if (highlight_current_line && highlighted_lines.has(Pair<int, int>(minimap_line, line_wrap_index))) {
  1034. if (rtl) {
  1035. RenderingServer::get_singleton()->canvas_item_add_rect(ci, Rect2(size.width - (xmargin_end + 2) - minimap_width, i * 3, minimap_width, 2), theme_cache.current_line_color);
  1036. } else {
  1037. RenderingServer::get_singleton()->canvas_item_add_rect(ci, Rect2((xmargin_end + 2), i * 3, minimap_width, 2), theme_cache.current_line_color);
  1038. }
  1039. } else if (line_background_color.a > 0) {
  1040. if (rtl) {
  1041. RenderingServer::get_singleton()->canvas_item_add_rect(ci, Rect2(size.width - (xmargin_end + 2) - minimap_width, i * 3, minimap_width, 2), line_background_color);
  1042. } else {
  1043. RenderingServer::get_singleton()->canvas_item_add_rect(ci, Rect2((xmargin_end + 2), i * 3, minimap_width, 2), line_background_color);
  1044. }
  1045. }
  1046. Color next_color = current_color;
  1047. int characters = 0;
  1048. int tab_alignment = 0;
  1049. int xpos = xmargin_end + 2 + indent_px;
  1050. for (int j = 0; j < str.length(); j++) {
  1051. bool next_is_whitespace = false;
  1052. bool next_is_tab = false;
  1053. // Get the number of characters to draw together.
  1054. for (characters = 0; j + characters < str.length(); characters++) {
  1055. int next_char_index = j + characters;
  1056. for (const Pair<int64_t, Color> &color_data : color_map) {
  1057. if (last_wrap_column + next_char_index >= color_data.first) {
  1058. next_color = color_data.second;
  1059. if (!editable) {
  1060. next_color.a = theme_cache.font_readonly_color.a;
  1061. }
  1062. next_color.a *= 0.6;
  1063. } else {
  1064. break;
  1065. }
  1066. }
  1067. if (characters == 0) {
  1068. current_color = next_color;
  1069. }
  1070. if (next_color != current_color) {
  1071. break;
  1072. }
  1073. next_is_whitespace = is_whitespace(str[next_char_index]);
  1074. if (next_is_whitespace) {
  1075. if (str[next_char_index] == '\t') {
  1076. next_is_tab = true;
  1077. }
  1078. break;
  1079. }
  1080. bool out_of_bounds = xpos + minimap_char_size.x * characters >= xmargin_end + minimap_width;
  1081. if (out_of_bounds) {
  1082. break;
  1083. }
  1084. }
  1085. if (!next_is_whitespace && characters == 0) {
  1086. break;
  1087. }
  1088. if (characters > 0) {
  1089. if (rtl) {
  1090. RenderingServer::get_singleton()->canvas_item_add_rect(ci, Rect2(Point2(size.width - xpos - minimap_char_size.x * characters, minimap_line_height * i), Point2(minimap_char_size.x * characters, minimap_char_size.y)), current_color);
  1091. } else {
  1092. RenderingServer::get_singleton()->canvas_item_add_rect(ci, Rect2(Point2(xpos, minimap_line_height * i), Point2(minimap_char_size.x * characters, minimap_char_size.y)), current_color);
  1093. }
  1094. }
  1095. j += characters - 1;
  1096. xpos += minimap_char_size.x * characters;
  1097. tab_alignment += characters;
  1098. if (next_is_whitespace) {
  1099. if (next_is_tab) {
  1100. tab_alignment %= tab_size;
  1101. xpos += minimap_char_size.x * (tab_size - tab_alignment);
  1102. tab_alignment = 0;
  1103. } else {
  1104. xpos += minimap_char_size.x;
  1105. tab_alignment += 1;
  1106. }
  1107. j += 1;
  1108. }
  1109. if (xpos >= xmargin_end + minimap_width) {
  1110. // Out of bounds.
  1111. break;
  1112. }
  1113. }
  1114. }
  1115. }
  1116. }
  1117. int top_limit_y = 0;
  1118. int bottom_limit_y = get_size().height;
  1119. if (!editable) {
  1120. top_limit_y += theme_cache.style_readonly->get_margin(SIDE_TOP);
  1121. bottom_limit_y -= theme_cache.style_readonly->get_margin(SIDE_BOTTOM);
  1122. } else {
  1123. top_limit_y += theme_cache.style_normal->get_margin(SIDE_TOP);
  1124. bottom_limit_y -= theme_cache.style_normal->get_margin(SIDE_BOTTOM);
  1125. }
  1126. // Draw guidelines.
  1127. _draw_guidelines();
  1128. // Draw main text.
  1129. line_drawing_cache.clear();
  1130. int row_height = draw_placeholder ? placeholder_line_height + theme_cache.line_spacing : get_line_height();
  1131. int line = first_vis_line;
  1132. for (int i = 0; i < draw_amount; i++) {
  1133. line++;
  1134. if (line < 0 || line >= (int)text.size()) {
  1135. continue;
  1136. }
  1137. while (_is_line_hidden(line)) {
  1138. line++;
  1139. if (line < 0 || line >= (int)text.size()) {
  1140. break;
  1141. }
  1142. }
  1143. if (line < 0 || line >= (int)text.size()) {
  1144. continue;
  1145. }
  1146. LineDrawingCache cache_entry;
  1147. const Vector<Pair<int64_t, Color>> color_map = _get_line_syntax_highlighting(line);
  1148. // Ensure we at least use the font color.
  1149. Color current_color = !editable ? theme_cache.font_readonly_color : theme_cache.font_color;
  1150. if (draw_placeholder) {
  1151. current_color = theme_cache.font_placeholder_color;
  1152. }
  1153. const Ref<TextParagraph> ldata = draw_placeholder ? placeholder_data_buf : text.get_line_data(line);
  1154. const Vector<String> wrap_rows = draw_placeholder ? placeholder_wrapped_rows : get_line_wrapped_text(line);
  1155. int line_wrap_amount = draw_placeholder ? placeholder_wrapped_rows.size() - 1 : get_line_wrap_count(line);
  1156. int first_indent_line = 0;
  1157. float indent_ofs = 0.0;
  1158. if (text.is_indent_wrapped_lines()) {
  1159. _get_wrapped_indent_level(line, first_indent_line);
  1160. indent_ofs = MIN(text.get_indent_offset(line, rtl), wrap_at_column * 0.6);
  1161. }
  1162. for (int line_wrap_index = 0; line_wrap_index <= line_wrap_amount; line_wrap_index++) {
  1163. if (line_wrap_index != 0) {
  1164. i++;
  1165. if (i >= draw_amount) {
  1166. break;
  1167. }
  1168. }
  1169. const String &str = wrap_rows[line_wrap_index];
  1170. int char_margin = xmargin_beg - first_visible_col;
  1171. int ofs_x = 0;
  1172. int ofs_y = 0;
  1173. if (!editable) {
  1174. ofs_x = theme_cache.style_readonly->get_offset().x / 2;
  1175. ofs_x -= theme_cache.style_normal->get_offset().x / 2;
  1176. ofs_y = theme_cache.style_readonly->get_offset().y / 2;
  1177. } else {
  1178. ofs_y = theme_cache.style_normal->get_offset().y / 2;
  1179. }
  1180. ofs_y += i * row_height + theme_cache.line_spacing / 2;
  1181. ofs_y -= first_visible_line_wrap_ofs * row_height;
  1182. ofs_y -= _get_v_scroll_offset() * row_height;
  1183. bool clipped = false;
  1184. if (ofs_y + row_height < top_limit_y) {
  1185. // Line is outside the top margin, clip current line.
  1186. // Still need to go through the process to prepare color changes for next lines.
  1187. clipped = true;
  1188. }
  1189. if (ofs_y > bottom_limit_y) {
  1190. // Line is outside the bottom margin, clip any remaining text.
  1191. i = draw_amount;
  1192. break;
  1193. }
  1194. if (text.get_line_background_color(line).a > 0.0) {
  1195. if (rtl) {
  1196. RS::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));
  1197. } else {
  1198. RS::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));
  1199. }
  1200. }
  1201. // Draw current line highlight.
  1202. if (highlight_current_line && highlighted_lines.has(Pair<int, int>(line, line_wrap_index))) {
  1203. if (rtl) {
  1204. RS::get_singleton()->canvas_item_add_rect(ci, Rect2(size.width - ofs_x - xmargin_end, ofs_y, xmargin_end, row_height), theme_cache.current_line_color);
  1205. } else {
  1206. RS::get_singleton()->canvas_item_add_rect(ci, Rect2(ofs_x, ofs_y, xmargin_end, row_height), theme_cache.current_line_color);
  1207. }
  1208. }
  1209. if (line_wrap_index == 0) {
  1210. // Only do these if we are on the first wrapped part of a line.
  1211. cache_entry.y_offset = ofs_y;
  1212. int gutter_offset = Math::ceil(theme_cache.style_normal->get_margin(SIDE_LEFT));
  1213. for (int g = 0; g < gutters.size(); g++) {
  1214. const GutterInfo &gutter = gutters[g];
  1215. if (!gutter.draw || gutter.width <= 0) {
  1216. continue;
  1217. }
  1218. switch (gutter.type) {
  1219. case GUTTER_TYPE_STRING: {
  1220. const String &txt = get_line_gutter_text(line, g);
  1221. if (txt.is_empty()) {
  1222. break;
  1223. }
  1224. Ref<TextLine> tl;
  1225. tl.instantiate();
  1226. tl->add_string(txt, theme_cache.font, theme_cache.font_size);
  1227. int yofs = ofs_y + (row_height - tl->get_size().y) / 2;
  1228. if (theme_cache.outline_size > 0 && theme_cache.outline_color.a > 0) {
  1229. tl->draw_outline(ci, Point2(gutter_offset + ofs_x, yofs), theme_cache.outline_size, theme_cache.outline_color);
  1230. }
  1231. tl->draw(ci, Point2(gutter_offset + ofs_x, yofs), get_line_gutter_item_color(line, g));
  1232. } break;
  1233. case GUTTER_TYPE_ICON: {
  1234. const Ref<Texture2D> icon = get_line_gutter_icon(line, g);
  1235. if (icon.is_null()) {
  1236. break;
  1237. }
  1238. Rect2 gutter_rect = Rect2(Point2i(gutter_offset, ofs_y), Size2i(gutter.width, row_height));
  1239. int horizontal_padding = gutter_rect.size.x / 6;
  1240. int vertical_padding = gutter_rect.size.y / 6;
  1241. gutter_rect.position += Point2(horizontal_padding, vertical_padding);
  1242. gutter_rect.size -= Point2(horizontal_padding, vertical_padding) * 2;
  1243. // Correct icon aspect ratio.
  1244. float icon_ratio = icon->get_width() / icon->get_height();
  1245. float gutter_ratio = gutter_rect.size.x / gutter_rect.size.y;
  1246. if (gutter_ratio > icon_ratio) {
  1247. gutter_rect.size.x = std::floor(icon->get_width() * (gutter_rect.size.y / icon->get_height()));
  1248. } else {
  1249. gutter_rect.size.y = std::floor(icon->get_height() * (gutter_rect.size.x / icon->get_width()));
  1250. }
  1251. if (rtl) {
  1252. gutter_rect.position.x = size.width - gutter_rect.position.x - gutter_rect.size.x;
  1253. }
  1254. icon->draw_rect(ci, gutter_rect, false, get_line_gutter_item_color(line, g));
  1255. } break;
  1256. case GUTTER_TYPE_CUSTOM: {
  1257. if (gutter.custom_draw_callback.is_valid()) {
  1258. Rect2i gutter_rect = Rect2i(Point2i(gutter_offset, ofs_y), Size2i(gutter.width, row_height));
  1259. if (rtl) {
  1260. gutter_rect.position.x = size.width - gutter_rect.position.x - gutter_rect.size.x;
  1261. }
  1262. gutter.custom_draw_callback.call(line, g, Rect2(gutter_rect));
  1263. }
  1264. } break;
  1265. }
  1266. gutter_offset += gutter.width;
  1267. }
  1268. }
  1269. // Draw line.
  1270. RID rid = ldata->get_line_rid(line_wrap_index);
  1271. float text_height = TS->shaped_text_get_size(rid).y;
  1272. float wrap_indent = line_wrap_index > first_indent_line ? indent_ofs : 0.0;
  1273. if (rtl) {
  1274. char_margin = size.width - char_margin - TS->shaped_text_get_size(rid).x - wrap_indent;
  1275. } else {
  1276. char_margin += wrap_indent;
  1277. }
  1278. // Validate inline objects.
  1279. Vector<Dictionary> object_keys;
  1280. if (inline_object_drawer.is_valid()) {
  1281. for (Variant k : ldata->get_line_objects(line_wrap_index)) {
  1282. if (!is_inline_info_valid(k)) {
  1283. continue;
  1284. }
  1285. object_keys.push_back(k);
  1286. }
  1287. }
  1288. // Draw selections.
  1289. float char_w = theme_cache.font->get_char_size(' ', theme_cache.font_size).width;
  1290. for (int c = 0; c < get_caret_count(); c++) {
  1291. if (!clipped && has_selection(c) && line >= get_selection_from_line(c) && line <= get_selection_to_line(c)) {
  1292. int sel_from = (line > get_selection_from_line(c)) ? TS->shaped_text_get_range(rid).x : get_selection_from_column(c);
  1293. int sel_to = (line < get_selection_to_line(c)) ? TS->shaped_text_get_range(rid).y : get_selection_to_column(c);
  1294. Vector<Vector2> sel = TS->shaped_text_get_selection(rid, sel_from, sel_to);
  1295. // Show selection at the end of line.
  1296. if (line_wrap_index == line_wrap_amount && line < get_selection_to_line(c)) {
  1297. if (rtl) {
  1298. sel.push_back(Vector2(-char_w, 0));
  1299. } else {
  1300. float line_end = TS->shaped_text_get_size(rid).width;
  1301. sel.push_back(Vector2(line_end, line_end + char_w));
  1302. }
  1303. }
  1304. // Show selection for inline objects.
  1305. for (Dictionary info : object_keys) {
  1306. int info_column = info["column"];
  1307. if (info_column >= sel_from && info_column < sel_to) {
  1308. Rect2 orect = TS->shaped_text_get_object_rect(rid, info);
  1309. sel.push_back(Vector2(orect.position.x, orect.position.x + orect.size.x));
  1310. }
  1311. }
  1312. for (int j = 0; j < sel.size(); j++) {
  1313. Rect2 rect = Rect2(Math::ceil(sel[j].x) + char_margin + ofs_x, ofs_y, Math::ceil(sel[j].y) - Math::ceil(sel[j].x), row_height);
  1314. if (rect.position.x + rect.size.x <= xmargin_beg || rect.position.x > xmargin_end) {
  1315. continue;
  1316. }
  1317. if (rect.position.x < xmargin_beg) {
  1318. rect.size.x -= (xmargin_beg - rect.position.x);
  1319. rect.position.x = xmargin_beg;
  1320. }
  1321. if (rect.position.x + rect.size.x > xmargin_end) {
  1322. rect.size.x = xmargin_end - rect.position.x;
  1323. }
  1324. RS::get_singleton()->canvas_item_add_rect(ci, rect, theme_cache.selection_color);
  1325. }
  1326. }
  1327. }
  1328. int start = TS->shaped_text_get_range(rid).x;
  1329. if (!clipped && !search_text.is_empty()) { // Search highlight
  1330. int search_text_col = _get_column_pos_of_word(search_text, str, search_flags, 0);
  1331. int search_text_len = search_text.length();
  1332. while (search_text_col != -1) {
  1333. const Vector<Vector2> sel = TS->shaped_text_get_selection(rid, search_text_col + start, search_text_col + search_text_len + start);
  1334. for (int j = 0; j < sel.size(); j++) {
  1335. Rect2 rect = Rect2(sel[j].x + char_margin + ofs_x, ofs_y, sel[j].y - sel[j].x, row_height);
  1336. if (rect.position.x + rect.size.x <= xmargin_beg || rect.position.x > xmargin_end) {
  1337. continue;
  1338. }
  1339. if (rect.position.x < xmargin_beg) {
  1340. rect.size.x -= (xmargin_beg - rect.position.x);
  1341. rect.position.x = xmargin_beg;
  1342. } else if (rect.position.x + rect.size.x > xmargin_end) {
  1343. rect.size.x = xmargin_end - rect.position.x;
  1344. }
  1345. RS::get_singleton()->canvas_item_add_rect(ci, rect, theme_cache.search_result_color);
  1346. draw_rect(rect, theme_cache.search_result_border_color, false);
  1347. }
  1348. search_text_col = _get_column_pos_of_word(search_text, str, search_flags, search_text_col + search_text_len);
  1349. }
  1350. }
  1351. if (!clipped && highlight_all_occurrences && !only_whitespaces_highlighted && !highlighted_text.is_empty()) { // Highlight
  1352. int highlighted_text_col = _get_column_pos_of_word(highlighted_text, str, SEARCH_MATCH_CASE | SEARCH_WHOLE_WORDS, 0);
  1353. int highlighted_text_len = highlighted_text.length();
  1354. while (highlighted_text_col != -1) {
  1355. const Vector<Vector2> sel = TS->shaped_text_get_selection(rid, highlighted_text_col + start, highlighted_text_col + highlighted_text_len + start);
  1356. for (int j = 0; j < sel.size(); j++) {
  1357. Rect2 rect = Rect2(sel[j].x + char_margin + ofs_x, ofs_y, sel[j].y - sel[j].x, row_height);
  1358. if (rect.position.x + rect.size.x <= xmargin_beg || rect.position.x > xmargin_end) {
  1359. continue;
  1360. }
  1361. if (rect.position.x < xmargin_beg) {
  1362. rect.size.x -= (xmargin_beg - rect.position.x);
  1363. rect.position.x = xmargin_beg;
  1364. } else if (rect.position.x + rect.size.x > xmargin_end) {
  1365. rect.size.x = xmargin_end - rect.position.x;
  1366. }
  1367. RS::get_singleton()->canvas_item_add_rect(ci, rect, theme_cache.word_highlighted_color);
  1368. }
  1369. highlighted_text_col = _get_column_pos_of_word(highlighted_text, str, SEARCH_MATCH_CASE | SEARCH_WHOLE_WORDS, highlighted_text_col + highlighted_text_len);
  1370. }
  1371. }
  1372. if (!clipped && lookup_symbol_word.length() != 0) { // Highlight word
  1373. if (is_unicode_identifier_start(lookup_symbol_word[0]) || lookup_symbol_word[0] == '.') {
  1374. Color highlight_underline_color = !editable ? theme_cache.font_readonly_color : theme_cache.font_color;
  1375. int lookup_symbol_word_col = _get_column_pos_of_word(lookup_symbol_word, str, SEARCH_MATCH_CASE | SEARCH_WHOLE_WORDS, 0);
  1376. int lookup_symbol_word_len = lookup_symbol_word.length();
  1377. while (lookup_symbol_word_col != -1) {
  1378. const Vector<Vector2> sel = TS->shaped_text_get_selection(rid, lookup_symbol_word_col + start, lookup_symbol_word_col + lookup_symbol_word_len + start);
  1379. for (int j = 0; j < sel.size(); j++) {
  1380. Rect2 rect = Rect2(sel[j].x + char_margin + ofs_x, ofs_y + (theme_cache.line_spacing / 2), sel[j].y - sel[j].x, row_height);
  1381. if (rect.position.x + rect.size.x <= xmargin_beg || rect.position.x > xmargin_end) {
  1382. continue;
  1383. }
  1384. if (rect.position.x < xmargin_beg) {
  1385. rect.size.x -= (xmargin_beg - rect.position.x);
  1386. rect.position.x = xmargin_beg;
  1387. } else if (rect.position.x + rect.size.x > xmargin_end) {
  1388. rect.size.x = xmargin_end - rect.position.x;
  1389. }
  1390. rect.position.y += std::ceil(TS->shaped_text_get_ascent(rid)) + std::ceil(theme_cache.font->get_underline_position(theme_cache.font_size));
  1391. rect.size.y = MAX(1, theme_cache.font->get_underline_thickness(theme_cache.font_size));
  1392. RS::get_singleton()->canvas_item_add_rect(ci, rect, highlight_underline_color);
  1393. }
  1394. 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);
  1395. }
  1396. }
  1397. }
  1398. ofs_y += (row_height - text_height) / 2;
  1399. const Glyph *glyphs = TS->shaped_text_get_glyphs(rid);
  1400. int gl_size = TS->shaped_text_get_glyph_count(rid);
  1401. ofs_y += ldata->get_line_ascent(line_wrap_index);
  1402. int first_visible_char = TS->shaped_text_get_range(rid).y;
  1403. int last_visible_char = TS->shaped_text_get_range(rid).x;
  1404. float char_ofs = 0;
  1405. if (theme_cache.outline_size > 0 && theme_cache.outline_color.a > 0) {
  1406. for (int j = 0; j < gl_size; j++) {
  1407. for (int k = 0; k < glyphs[j].repeat; k++) {
  1408. if ((char_ofs + char_margin) >= xmargin_beg && (char_ofs + glyphs[j].advance + char_margin) <= xmargin_end) {
  1409. if (glyphs[j].font_rid != RID()) {
  1410. TS->font_draw_glyph_outline(glyphs[j].font_rid, ci, glyphs[j].font_size, theme_cache.outline_size, Vector2(char_margin + char_ofs + ofs_x + glyphs[j].x_off, ofs_y + glyphs[j].y_off), glyphs[j].index, theme_cache.outline_color);
  1411. }
  1412. }
  1413. char_ofs += glyphs[j].advance;
  1414. }
  1415. if ((char_ofs + char_margin) >= xmargin_end) {
  1416. break;
  1417. }
  1418. }
  1419. char_ofs = 0;
  1420. }
  1421. // Draw inline objects.
  1422. for (Dictionary k : object_keys) {
  1423. Rect2 col_rect = TS->shaped_text_get_object_rect(rid, k);
  1424. col_rect.position += Vector2(char_margin + ofs_x, ofs_y);
  1425. if (!clipped && (col_rect.position.x) >= xmargin_beg && (col_rect.position.x + col_rect.size.x) <= xmargin_end) {
  1426. inline_object_drawer.call(k, col_rect);
  1427. }
  1428. }
  1429. for (int j = 0; j < gl_size; j++) {
  1430. for (const Pair<int64_t, Color> &color_data : color_map) {
  1431. if (color_data.first <= glyphs[j].start) {
  1432. current_color = color_data.second;
  1433. if (!editable && current_color.a > theme_cache.font_readonly_color.a) {
  1434. current_color.a = theme_cache.font_readonly_color.a;
  1435. }
  1436. } else {
  1437. break;
  1438. }
  1439. }
  1440. Color gl_color = current_color;
  1441. for (int c = 0; c < get_caret_count(); c++) {
  1442. if (has_selection(c) && line >= get_selection_from_line(c) && line <= get_selection_to_line(c)) { // Selection
  1443. int sel_from = (line > get_selection_from_line(c)) ? TS->shaped_text_get_range(rid).x : get_selection_from_column(c);
  1444. int sel_to = (line < get_selection_to_line(c)) ? TS->shaped_text_get_range(rid).y : get_selection_to_column(c);
  1445. if (glyphs[j].start >= sel_from && glyphs[j].end <= sel_to && use_selected_font_color) {
  1446. gl_color = theme_cache.font_selected_color;
  1447. }
  1448. }
  1449. }
  1450. float char_pos = char_ofs + char_margin + ofs_x;
  1451. if (char_pos >= xmargin_beg) {
  1452. if (highlight_matching_braces_enabled) {
  1453. for (int c = 0; c < get_caret_count(); c++) {
  1454. const BraceMatchingData &brace_match = brace_matching[c];
  1455. if ((brace_match.open_match_line == line && brace_match.open_match_column == glyphs[j].start) ||
  1456. (get_caret_column(c) == glyphs[j].start && get_caret_line(c) == line && carets_wrap_index[c] == line_wrap_index && (brace_match.open_matching || brace_match.open_mismatch))) {
  1457. if (brace_match.open_mismatch) {
  1458. gl_color = _get_brace_mismatch_color();
  1459. }
  1460. Rect2 rect = Rect2(char_pos, ofs_y + theme_cache.font->get_underline_position(theme_cache.font_size), glyphs[j].advance * glyphs[j].repeat, MAX(theme_cache.font->get_underline_thickness(theme_cache.font_size) * theme_cache.base_scale, 1));
  1461. RS::get_singleton()->canvas_item_add_rect(ci, rect, gl_color);
  1462. }
  1463. if ((brace_match.close_match_line == line && brace_match.close_match_column == glyphs[j].start) ||
  1464. (get_caret_column(c) == glyphs[j].start + 1 && get_caret_line(c) == line && carets_wrap_index[c] == line_wrap_index && (brace_match.close_matching || brace_match.close_mismatch))) {
  1465. if (brace_match.close_mismatch) {
  1466. gl_color = _get_brace_mismatch_color();
  1467. }
  1468. Rect2 rect = Rect2(char_pos, ofs_y + theme_cache.font->get_underline_position(theme_cache.font_size), glyphs[j].advance * glyphs[j].repeat, MAX(theme_cache.font->get_underline_thickness(theme_cache.font_size) * theme_cache.base_scale, 1));
  1469. RS::get_singleton()->canvas_item_add_rect(ci, rect, gl_color);
  1470. }
  1471. }
  1472. }
  1473. if (draw_tabs && ((glyphs[j].flags & TextServer::GRAPHEME_IS_TAB) == TextServer::GRAPHEME_IS_TAB)) {
  1474. int yofs = (text_height - theme_cache.tab_icon->get_height()) / 2 - ldata->get_line_ascent(line_wrap_index);
  1475. theme_cache.tab_icon->draw(ci, Point2(char_pos, ofs_y + yofs), gl_color);
  1476. } else if (draw_spaces && ((glyphs[j].flags & TextServer::GRAPHEME_IS_SPACE) == TextServer::GRAPHEME_IS_SPACE) && ((glyphs[j].flags & TextServer::GRAPHEME_IS_VIRTUAL) != TextServer::GRAPHEME_IS_VIRTUAL)) {
  1477. int yofs = (text_height - theme_cache.space_icon->get_height()) / 2 - ldata->get_line_ascent(line_wrap_index);
  1478. int xofs = (glyphs[j].advance * glyphs[j].repeat - theme_cache.space_icon->get_width()) / 2;
  1479. theme_cache.space_icon->draw(ci, Point2(char_pos + xofs, ofs_y + yofs), gl_color);
  1480. }
  1481. }
  1482. bool had_glyphs_drawn = false;
  1483. for (int k = 0; k < glyphs[j].repeat; k++) {
  1484. if (!clipped && (char_ofs + char_margin) >= xmargin_beg && (char_ofs + glyphs[j].advance + char_margin) <= xmargin_end) {
  1485. if (glyphs[j].font_rid != RID()) {
  1486. 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);
  1487. had_glyphs_drawn = true;
  1488. } else if (((glyphs[j].flags & TextServer::GRAPHEME_IS_VIRTUAL) != TextServer::GRAPHEME_IS_VIRTUAL) && ((glyphs[j].flags & TextServer::GRAPHEME_IS_EMBEDDED_OBJECT) != TextServer::GRAPHEME_IS_EMBEDDED_OBJECT)) {
  1489. 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);
  1490. had_glyphs_drawn = true;
  1491. }
  1492. }
  1493. char_ofs += glyphs[j].advance;
  1494. }
  1495. if (had_glyphs_drawn) {
  1496. if (first_visible_char > glyphs[j].start) {
  1497. first_visible_char = glyphs[j].start;
  1498. }
  1499. if (last_visible_char < glyphs[j].end) {
  1500. last_visible_char = glyphs[j].end;
  1501. }
  1502. }
  1503. if ((char_ofs + char_margin) >= xmargin_end) {
  1504. break;
  1505. }
  1506. }
  1507. cache_entry.first_visible_chars.push_back(first_visible_char);
  1508. cache_entry.last_visible_chars.push_back(last_visible_char);
  1509. // is_line_folded
  1510. if (line_wrap_index == line_wrap_amount && line < text.size() - 1 && _is_line_hidden(line + 1)) {
  1511. int xofs = char_ofs + char_margin + ofs_x + (_get_folded_eol_icon()->get_width() / 2);
  1512. if (xofs >= xmargin_beg && xofs < xmargin_end) {
  1513. int yofs = (text_height - _get_folded_eol_icon()->get_height()) / 2 - ldata->get_line_ascent(line_wrap_index);
  1514. Color eol_color = _get_code_folding_color();
  1515. eol_color.a = 1;
  1516. _get_folded_eol_icon()->draw(ci, Point2(xofs, ofs_y + yofs), eol_color);
  1517. }
  1518. }
  1519. // Carets.
  1520. // Prevent carets from disappearing at theme scales below 1.0 (if the caret width is 1).
  1521. const int caret_width = theme_cache.caret_width * MAX(1, theme_cache.base_scale);
  1522. for (int c = 0; c < carets.size(); c++) {
  1523. if (!clipped && get_caret_line(c) == line && carets_wrap_index[c] == line_wrap_index) {
  1524. carets.write[c].draw_pos.y = ofs_y + ldata->get_line_descent(line_wrap_index);
  1525. if (ime_text.is_empty() || ime_selection.y == 0) {
  1526. // Normal caret.
  1527. CaretInfo ts_caret;
  1528. if (!str.is_empty() || !ime_text.is_empty()) {
  1529. // Get carets.
  1530. ts_caret = TS->shaped_text_get_carets(rid, ime_text.is_empty() ? get_caret_column(c) : get_caret_column(c) + ime_selection.x);
  1531. } else {
  1532. // No carets, add one at the start.
  1533. int h = theme_cache.font->get_height(theme_cache.font_size);
  1534. if (rtl) {
  1535. ts_caret.l_dir = TextServer::DIRECTION_RTL;
  1536. ts_caret.l_caret = Rect2(Vector2(TS->shaped_text_get_size(rid).x, -h / 2), Size2(caret_width * 4, h));
  1537. } else {
  1538. ts_caret.l_dir = TextServer::DIRECTION_LTR;
  1539. ts_caret.l_caret = Rect2(Vector2(char_ofs, -h / 2), Size2(caret_width * 4, h));
  1540. }
  1541. }
  1542. 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())) {
  1543. carets.write[c].draw_pos.x = char_margin + ofs_x + ts_caret.l_caret.position.x;
  1544. } else {
  1545. carets.write[c].draw_pos.x = char_margin + ofs_x + ts_caret.t_caret.position.x;
  1546. }
  1547. if (get_caret_draw_pos(c).x >= xmargin_beg && get_caret_draw_pos(c).x <= xmargin_end) {
  1548. carets.write[c].visible = true;
  1549. if (draw_caret || drag_caret_force_displayed) {
  1550. if (caret_type == CaretType::CARET_TYPE_BLOCK || overtype_mode) {
  1551. //Block or underline caret, draw trailing carets at full height.
  1552. int h = theme_cache.font->get_height(theme_cache.font_size);
  1553. if (ts_caret.t_caret != Rect2()) {
  1554. if (overtype_mode) {
  1555. ts_caret.t_caret.position.y = TS->shaped_text_get_descent(rid);
  1556. ts_caret.t_caret.size.y = caret_width;
  1557. } else {
  1558. ts_caret.t_caret.position.y = -TS->shaped_text_get_ascent(rid);
  1559. ts_caret.t_caret.size.y = h;
  1560. }
  1561. ts_caret.t_caret.position += Vector2(char_margin + ofs_x, ofs_y);
  1562. draw_rect(ts_caret.t_caret, theme_cache.caret_color, overtype_mode);
  1563. if (ts_caret.l_caret != Rect2() && ts_caret.l_dir != ts_caret.t_dir) {
  1564. // Draw split caret (leading part).
  1565. ts_caret.l_caret.position += Vector2(char_margin + ofs_x, ofs_y);
  1566. ts_caret.l_caret.size.x = caret_width;
  1567. RS::get_singleton()->canvas_item_add_rect(ci, ts_caret.l_caret, theme_cache.caret_color);
  1568. // Draw extra direction marker on top of split caret.
  1569. float d = (ts_caret.l_dir == TextServer::DIRECTION_LTR) ? 0.5 : -3;
  1570. 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);
  1571. RS::get_singleton()->canvas_item_add_rect(ci, trect, theme_cache.caret_color);
  1572. }
  1573. } else { // End of the line.
  1574. if (gl_size > 0) {
  1575. // Adjust for actual line dimensions.
  1576. if (overtype_mode) {
  1577. ts_caret.l_caret.position.y = TS->shaped_text_get_descent(rid);
  1578. ts_caret.l_caret.size.y = caret_width;
  1579. } else {
  1580. ts_caret.l_caret.position.y = -TS->shaped_text_get_ascent(rid);
  1581. ts_caret.l_caret.size.y = h;
  1582. }
  1583. } else if (overtype_mode) {
  1584. ts_caret.l_caret.position.y += ts_caret.l_caret.size.y;
  1585. ts_caret.l_caret.size.y = caret_width;
  1586. }
  1587. if (Math::ceil(ts_caret.l_caret.position.x) >= TS->shaped_text_get_size(rid).x) {
  1588. ts_caret.l_caret.size.x = theme_cache.font->get_char_size('m', theme_cache.font_size).x;
  1589. } else {
  1590. ts_caret.l_caret.size.x = 3 * caret_width;
  1591. }
  1592. ts_caret.l_caret.position += Vector2(char_margin + ofs_x, ofs_y);
  1593. if (ts_caret.l_dir == TextServer::DIRECTION_RTL) {
  1594. ts_caret.l_caret.position.x -= ts_caret.l_caret.size.x;
  1595. }
  1596. draw_rect(ts_caret.l_caret, theme_cache.caret_color, overtype_mode);
  1597. }
  1598. } else {
  1599. // Normal caret.
  1600. if (ts_caret.l_caret != Rect2() && ts_caret.l_dir == TextServer::DIRECTION_AUTO) {
  1601. // Draw extra marker on top of mid caret.
  1602. Rect2 trect = Rect2(ts_caret.l_caret.position.x - 2.5 * caret_width, ts_caret.l_caret.position.y, 6 * caret_width, caret_width);
  1603. trect.position += Vector2(char_margin + ofs_x, ofs_y);
  1604. RS::get_singleton()->canvas_item_add_rect(ci, trect, theme_cache.caret_color);
  1605. } else if (ts_caret.l_caret != Rect2() && ts_caret.t_caret != Rect2() && ts_caret.l_dir != ts_caret.t_dir) {
  1606. // Draw extra direction marker on top of split caret.
  1607. float d = (ts_caret.l_dir == TextServer::DIRECTION_LTR) ? 0.5 : -3;
  1608. 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);
  1609. trect.position += Vector2(char_margin + ofs_x, ofs_y);
  1610. RS::get_singleton()->canvas_item_add_rect(ci, trect, theme_cache.caret_color);
  1611. d = (ts_caret.t_dir == TextServer::DIRECTION_LTR) ? 0.5 : -3;
  1612. trect = Rect2(ts_caret.t_caret.position.x + d * caret_width, ts_caret.t_caret.position.y, 3 * caret_width, caret_width);
  1613. trect.position += Vector2(char_margin + ofs_x, ofs_y);
  1614. RS::get_singleton()->canvas_item_add_rect(ci, trect, theme_cache.caret_color);
  1615. }
  1616. ts_caret.l_caret.position += Vector2(char_margin + ofs_x, ofs_y);
  1617. ts_caret.l_caret.size.x = caret_width;
  1618. RS::get_singleton()->canvas_item_add_rect(ci, ts_caret.l_caret, theme_cache.caret_color);
  1619. ts_caret.t_caret.position += Vector2(char_margin + ofs_x, ofs_y);
  1620. ts_caret.t_caret.size.x = caret_width;
  1621. RS::get_singleton()->canvas_item_add_rect(ci, ts_caret.t_caret, theme_cache.caret_color);
  1622. }
  1623. }
  1624. }
  1625. }
  1626. if (!ime_text.is_empty()) {
  1627. {
  1628. // IME Intermediate text range.
  1629. const Vector<Vector2> sel = TS->shaped_text_get_selection(rid, get_caret_column(c), get_caret_column(c) + ime_text.length());
  1630. for (int j = 0; j < sel.size(); j++) {
  1631. Rect2 rect = Rect2(sel[j].x + char_margin + ofs_x, ofs_y, sel[j].y - sel[j].x, text_height);
  1632. if (rect.position.x + rect.size.x <= xmargin_beg || rect.position.x > xmargin_end) {
  1633. continue;
  1634. }
  1635. if (rect.position.x < xmargin_beg) {
  1636. rect.size.x -= (xmargin_beg - rect.position.x);
  1637. rect.position.x = xmargin_beg;
  1638. } else if (rect.position.x + rect.size.x > xmargin_end) {
  1639. rect.size.x = xmargin_end - rect.position.x;
  1640. }
  1641. rect.size.y = caret_width;
  1642. draw_rect(rect, theme_cache.caret_color);
  1643. carets.write[c].draw_pos.x = rect.position.x;
  1644. }
  1645. }
  1646. if (ime_selection.y > 0) {
  1647. // IME caret.
  1648. const 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);
  1649. for (int j = 0; j < sel.size(); j++) {
  1650. Rect2 rect = Rect2(sel[j].x + char_margin + ofs_x, ofs_y, sel[j].y - sel[j].x, text_height);
  1651. if (rect.position.x + rect.size.x <= xmargin_beg || rect.position.x > xmargin_end) {
  1652. continue;
  1653. }
  1654. if (rect.position.x < xmargin_beg) {
  1655. rect.size.x -= (xmargin_beg - rect.position.x);
  1656. rect.position.x = xmargin_beg;
  1657. } else if (rect.position.x + rect.size.x > xmargin_end) {
  1658. rect.size.x = xmargin_end - rect.position.x;
  1659. }
  1660. rect.size.y = caret_width * 3;
  1661. draw_rect(rect, theme_cache.caret_color);
  1662. carets.write[c].draw_pos.x = rect.position.x;
  1663. }
  1664. }
  1665. }
  1666. }
  1667. }
  1668. }
  1669. if (!draw_placeholder) {
  1670. line_drawing_cache[line] = cache_entry;
  1671. }
  1672. }
  1673. if (has_focus()) {
  1674. _update_ime_window_position();
  1675. }
  1676. } break;
  1677. case NOTIFICATION_FOCUS_ENTER: {
  1678. if (caret_blink_enabled) {
  1679. caret_blink_timer->start();
  1680. } else {
  1681. draw_caret = true;
  1682. }
  1683. if (editable && virtual_keyboard_show_on_focus) {
  1684. _show_virtual_keyboard();
  1685. }
  1686. } break;
  1687. case NOTIFICATION_FOCUS_EXIT: {
  1688. if (caret_blink_enabled) {
  1689. caret_blink_timer->stop();
  1690. }
  1691. apply_ime();
  1692. if (DisplayServer::get_singleton()->has_feature(DisplayServer::FEATURE_VIRTUAL_KEYBOARD) && virtual_keyboard_enabled) {
  1693. DisplayServer::get_singleton()->virtual_keyboard_hide();
  1694. }
  1695. if (deselect_on_focus_loss_enabled && !selection_drag_attempt) {
  1696. deselect();
  1697. }
  1698. } break;
  1699. case MainLoop::NOTIFICATION_OS_IME_UPDATE: {
  1700. if (has_focus()) {
  1701. const String &new_ime_text = DisplayServer::get_singleton()->ime_get_text();
  1702. const Vector2i &new_ime_selection = DisplayServer::get_singleton()->ime_get_selection();
  1703. if (ime_text == new_ime_text && ime_selection == new_ime_selection) {
  1704. break;
  1705. }
  1706. bool had_ime_text = has_ime_text();
  1707. ime_text = new_ime_text;
  1708. ime_selection = new_ime_selection;
  1709. if (!had_ime_text && has_ime_text()) {
  1710. _cancel_drag_and_drop_text();
  1711. }
  1712. if (has_ime_text() && has_selection()) {
  1713. set_selection_mode(SELECTION_MODE_NONE);
  1714. delete_selection();
  1715. }
  1716. _update_ime_text();
  1717. adjust_viewport_to_caret(0);
  1718. queue_accessibility_update();
  1719. queue_redraw();
  1720. }
  1721. } break;
  1722. case NOTIFICATION_DRAG_BEGIN: {
  1723. set_selection_mode(SelectionMode::SELECTION_MODE_NONE);
  1724. drag_action = true;
  1725. dragging_minimap = false;
  1726. dragging_selection = false;
  1727. can_drag_minimap = false;
  1728. click_select_held->stop();
  1729. } break;
  1730. case NOTIFICATION_DRAG_END: {
  1731. remove_drag_caret();
  1732. if (selection_drag_attempt && is_drag_successful()) {
  1733. // Dropped elsewhere.
  1734. if (is_editable() && !Input::get_singleton()->is_key_pressed(Key::CMD_OR_CTRL)) {
  1735. delete_selection();
  1736. } else if (deselect_on_focus_loss_enabled) {
  1737. deselect();
  1738. }
  1739. }
  1740. selection_drag_attempt = false;
  1741. drag_action = false;
  1742. drag_caret_force_displayed = false;
  1743. dragging_selection = false;
  1744. } break;
  1745. case NOTIFICATION_MOUSE_EXIT_SELF: {
  1746. if (drag_caret_force_displayed) {
  1747. drag_caret_force_displayed = false;
  1748. queue_redraw();
  1749. }
  1750. if (hovered_gutter != Vector2i(-1, -1)) {
  1751. hovered_gutter = Vector2i(-1, -1);
  1752. queue_redraw();
  1753. }
  1754. } break;
  1755. }
  1756. }
  1757. void TextEdit::unhandled_key_input(const Ref<InputEvent> &p_event) {
  1758. Ref<InputEventKey> k = p_event;
  1759. if (k.is_valid()) {
  1760. if (!k->is_pressed()) {
  1761. return;
  1762. }
  1763. // Handle Unicode (with modifiers active, process after shortcuts).
  1764. if (has_focus() && editable && (k->get_unicode() >= 32)) {
  1765. handle_unicode_input(k->get_unicode());
  1766. accept_event();
  1767. }
  1768. }
  1769. }
  1770. bool TextEdit::alt_input(const Ref<InputEvent> &p_gui_input) {
  1771. Ref<InputEventKey> k = p_gui_input;
  1772. if (k.is_valid()) {
  1773. // Start Unicode Alt input (hold).
  1774. if (k->is_alt_pressed() && k->get_keycode() == Key::KP_ADD && !alt_start && !alt_start_no_hold) {
  1775. if (has_selection()) {
  1776. delete_selection();
  1777. }
  1778. alt_start = true;
  1779. alt_code = 0;
  1780. alt_mode = ALT_INPUT_UNICODE;
  1781. ime_text = "u";
  1782. ime_selection = Vector2i(0, -1);
  1783. _update_ime_text();
  1784. return true;
  1785. }
  1786. // Start Unicode input (press).
  1787. if (k->is_action("ui_unicode_start", true) && !alt_start && !alt_start_no_hold) {
  1788. if (has_selection()) {
  1789. delete_selection();
  1790. }
  1791. alt_start_no_hold = true;
  1792. alt_code = 0;
  1793. alt_mode = ALT_INPUT_UNICODE;
  1794. ime_text = "u";
  1795. ime_selection = Vector2i(0, -1);
  1796. _update_ime_text();
  1797. return true;
  1798. }
  1799. // Start OEM Alt input (hold).
  1800. if (k->is_alt_pressed() && k->get_keycode() >= Key::KP_1 && k->get_keycode() <= Key::KP_9 && !alt_start && !alt_start_no_hold) {
  1801. if (has_selection()) {
  1802. delete_selection();
  1803. }
  1804. alt_start = true;
  1805. alt_code = (uint32_t)(k->get_keycode() - Key::KP_0);
  1806. alt_mode = ALT_INPUT_OEM;
  1807. ime_text = vformat("o%s", String::num_int64(alt_code, 10));
  1808. ime_selection = Vector2i(0, -1);
  1809. _update_ime_text();
  1810. return true;
  1811. }
  1812. // Start Windows Alt input (hold).
  1813. if (k->is_alt_pressed() && k->get_keycode() == Key::KP_0 && !alt_start && !alt_start_no_hold) {
  1814. if (has_selection()) {
  1815. delete_selection();
  1816. }
  1817. alt_start = true;
  1818. alt_mode = ALT_INPUT_WIN;
  1819. alt_code = 0;
  1820. ime_text = "w";
  1821. ime_selection = Vector2i(0, -1);
  1822. _update_ime_text();
  1823. return true;
  1824. }
  1825. // Update Unicode input.
  1826. if (k->is_pressed() && ((k->is_alt_pressed() && alt_start) || alt_start_no_hold)) {
  1827. if (k->get_keycode() >= Key::KEY_0 && k->get_keycode() <= Key::KEY_9) {
  1828. if (alt_mode == ALT_INPUT_UNICODE) {
  1829. alt_code = alt_code << 4;
  1830. } else {
  1831. alt_code = alt_code * 10;
  1832. }
  1833. alt_code += (uint32_t)(k->get_keycode() - Key::KEY_0);
  1834. } else if (k->get_keycode() >= Key::KP_0 && k->get_keycode() <= Key::KP_9) {
  1835. if (alt_mode == ALT_INPUT_UNICODE) {
  1836. alt_code = alt_code << 4;
  1837. } else {
  1838. alt_code = alt_code * 10;
  1839. }
  1840. alt_code += (uint32_t)(k->get_keycode() - Key::KP_0);
  1841. } else if (alt_mode == ALT_INPUT_UNICODE && k->get_keycode() >= Key::A && k->get_keycode() <= Key::F) {
  1842. alt_code = alt_code << 4;
  1843. alt_code += (uint32_t)(k->get_keycode() - Key::A) + 10;
  1844. } else if ((Key)k->get_unicode() >= Key::KEY_0 && (Key)k->get_unicode() <= Key::KEY_9) {
  1845. if (alt_mode == ALT_INPUT_UNICODE) {
  1846. alt_code = alt_code << 4;
  1847. } else {
  1848. alt_code = alt_code * 10;
  1849. }
  1850. alt_code += (uint32_t)((Key)k->get_unicode() - Key::KEY_0);
  1851. } else if (alt_mode == ALT_INPUT_UNICODE && (Key)k->get_unicode() >= Key::A && (Key)k->get_unicode() <= Key::F) {
  1852. alt_code = alt_code << 4;
  1853. alt_code += (uint32_t)((Key)k->get_unicode() - Key::A) + 10;
  1854. } else if (k->get_physical_keycode() >= Key::KEY_0 && k->get_physical_keycode() <= Key::KEY_9) {
  1855. if (alt_mode == ALT_INPUT_UNICODE) {
  1856. alt_code = alt_code << 4;
  1857. } else {
  1858. alt_code = alt_code * 10;
  1859. }
  1860. alt_code += (uint32_t)(k->get_physical_keycode() - Key::KEY_0);
  1861. }
  1862. if (k->get_keycode() == Key::BACKSPACE) {
  1863. if (alt_mode == ALT_INPUT_UNICODE) {
  1864. alt_code = alt_code >> 4;
  1865. } else {
  1866. alt_code = alt_code / 10;
  1867. }
  1868. }
  1869. if (alt_code > 0x10ffff) {
  1870. alt_code = 0x10ffff;
  1871. }
  1872. if (alt_code > 0) {
  1873. if (alt_mode == ALT_INPUT_UNICODE) {
  1874. ime_text = vformat("u%s", String::num_int64(alt_code, 16, true));
  1875. } else if (alt_mode == ALT_INPUT_OEM) {
  1876. ime_text = vformat("o%s", String::num_int64(alt_code, 10));
  1877. } else if (alt_mode == ALT_INPUT_WIN) {
  1878. ime_text = vformat("w%s", String::num_int64(alt_code, 10));
  1879. }
  1880. } else {
  1881. if (alt_mode == ALT_INPUT_UNICODE) {
  1882. ime_text = "u";
  1883. } else if (alt_mode == ALT_INPUT_OEM) {
  1884. ime_text = "o";
  1885. } else if (alt_mode == ALT_INPUT_WIN) {
  1886. ime_text = "w";
  1887. }
  1888. }
  1889. ime_selection = Vector2i(0, -1);
  1890. _update_ime_text();
  1891. return true;
  1892. }
  1893. // Submit Unicode input.
  1894. if ((!k->is_pressed() && alt_start && k->get_keycode() == Key::ALT) || (alt_start_no_hold && (k->is_action("ui_text_submit", true) || k->is_action("ui_accept", true)))) {
  1895. alt_start = false;
  1896. alt_start_no_hold = false;
  1897. if ((alt_code > 0x31 && alt_code < 0xd800) || (alt_code > 0xdfff)) {
  1898. ime_text = String();
  1899. ime_selection = Vector2i();
  1900. if (alt_mode == ALT_INPUT_UNICODE) {
  1901. if ((alt_code > 0x31 && alt_code < 0xd800) || (alt_code > 0xdfff)) {
  1902. handle_unicode_input(alt_code);
  1903. }
  1904. } else if (alt_mode == ALT_INPUT_OEM) {
  1905. if (alt_code > 0x00 && alt_code <= 0xff) {
  1906. handle_unicode_input(alt_code_oem437[alt_code]);
  1907. } else if ((alt_code > 0xff && alt_code < 0xd800) || (alt_code > 0xdfff)) {
  1908. handle_unicode_input(alt_code);
  1909. }
  1910. } else if (alt_mode == ALT_INPUT_WIN) {
  1911. if (alt_code > 0x00 && alt_code <= 0xff) {
  1912. handle_unicode_input(alt_code_cp1252[alt_code]);
  1913. } else if ((alt_code > 0xff && alt_code < 0xd800) || (alt_code > 0xdfff)) {
  1914. handle_unicode_input(alt_code);
  1915. }
  1916. }
  1917. alt_mode = ALT_INPUT_NONE;
  1918. } else {
  1919. ime_text = String();
  1920. ime_selection = Vector2i();
  1921. }
  1922. _update_ime_text();
  1923. return true;
  1924. }
  1925. // Cancel Unicode input.
  1926. if (alt_start_no_hold && k->is_action("ui_cancel", true)) {
  1927. alt_start = false;
  1928. alt_start_no_hold = false;
  1929. alt_mode = ALT_INPUT_NONE;
  1930. ime_text = String();
  1931. ime_selection = Vector2i();
  1932. _update_ime_text();
  1933. return true;
  1934. }
  1935. }
  1936. return false;
  1937. }
  1938. void TextEdit::gui_input(const Ref<InputEvent> &p_gui_input) {
  1939. ERR_FAIL_COND(p_gui_input.is_null());
  1940. double prev_v_scroll = v_scroll->get_value();
  1941. double prev_h_scroll = h_scroll->get_value();
  1942. Ref<InputEventMouseButton> mb = p_gui_input;
  1943. if (mb.is_valid()) {
  1944. Vector2i mpos = mb->get_position();
  1945. if (is_layout_rtl()) {
  1946. mpos.x = get_size().x - mpos.x;
  1947. }
  1948. if (mb->is_pressed()) {
  1949. if (mb->get_button_index() == MouseButton::WHEEL_UP && !mb->is_command_or_control_pressed()) {
  1950. if (mb->is_shift_pressed()) {
  1951. h_scroll->set_value(h_scroll->get_value() - (100 * mb->get_factor()));
  1952. queue_accessibility_update();
  1953. } else if (mb->is_alt_pressed()) {
  1954. // Scroll 5 times as fast as normal (like in Visual Studio Code).
  1955. _scroll_up(15 * mb->get_factor(), true);
  1956. } else if (v_scroll->is_visible()) {
  1957. // Scroll 3 lines.
  1958. _scroll_up(3 * mb->get_factor(), true);
  1959. }
  1960. }
  1961. if (mb->get_button_index() == MouseButton::WHEEL_DOWN && !mb->is_command_or_control_pressed()) {
  1962. if (mb->is_shift_pressed()) {
  1963. h_scroll->set_value(h_scroll->get_value() + (100 * mb->get_factor()));
  1964. queue_accessibility_update();
  1965. } else if (mb->is_alt_pressed()) {
  1966. // Scroll 5 times as fast as normal (like in Visual Studio Code).
  1967. _scroll_down(15 * mb->get_factor(), true);
  1968. } else if (v_scroll->is_visible()) {
  1969. // Scroll 3 lines.
  1970. _scroll_down(3 * mb->get_factor(), true);
  1971. }
  1972. }
  1973. if (mb->get_button_index() == MouseButton::WHEEL_LEFT) {
  1974. h_scroll->set_value(h_scroll->get_value() - (100 * mb->get_factor()));
  1975. queue_accessibility_update();
  1976. }
  1977. if (mb->get_button_index() == MouseButton::WHEEL_RIGHT) {
  1978. h_scroll->set_value(h_scroll->get_value() + (100 * mb->get_factor()));
  1979. queue_accessibility_update();
  1980. }
  1981. if (mb->get_button_index() == MouseButton::LEFT) {
  1982. _reset_caret_blink_timer();
  1983. apply_ime();
  1984. Point2i pos = get_line_column_at_pos(mpos);
  1985. int line = pos.y;
  1986. int col = pos.x;
  1987. // Gutters.
  1988. Vector2i current_hovered_gutter = _get_hovered_gutter(mpos);
  1989. if (current_hovered_gutter != hovered_gutter) {
  1990. hovered_gutter = current_hovered_gutter;
  1991. queue_redraw();
  1992. }
  1993. if (hovered_gutter != Vector2i(-1, -1)) {
  1994. emit_signal(SNAME("gutter_clicked"), hovered_gutter.y, hovered_gutter.x);
  1995. return;
  1996. }
  1997. int left_margin = Math::ceil(theme_cache.style_normal->get_margin(SIDE_LEFT));
  1998. if (mpos.x < left_margin + gutters_width + gutter_padding) {
  1999. return;
  2000. }
  2001. // Minimap.
  2002. if (draw_minimap) {
  2003. _update_minimap_click();
  2004. if (dragging_minimap) {
  2005. return;
  2006. }
  2007. }
  2008. // Update caret.
  2009. int caret = carets.size() - 1;
  2010. int prev_col = get_caret_column(caret);
  2011. int prev_line = get_caret_line(caret);
  2012. int mouse_over_selection_caret = get_selection_at_line_column(line, col, true);
  2013. const int triple_click_timeout = 600;
  2014. const int triple_click_tolerance = 5;
  2015. 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);
  2016. if (!mb->is_double_click() && !is_triple_click) {
  2017. if (mb->is_alt_pressed()) {
  2018. prev_line = line;
  2019. prev_col = col;
  2020. // Remove caret at clicked location.
  2021. if (get_caret_count() > 1) {
  2022. // Deselect if clicked on caret or its selection.
  2023. int clicked_caret = get_selection_at_line_column(line, col, true, false);
  2024. if (clicked_caret != -1) {
  2025. remove_caret(clicked_caret);
  2026. last_dblclk = 0;
  2027. return;
  2028. }
  2029. }
  2030. if (mouse_over_selection_caret >= 0) {
  2031. // Did not remove selection under mouse, don't add a new caret.
  2032. return;
  2033. }
  2034. // Create new caret at clicked location.
  2035. caret = add_caret(line, col);
  2036. if (caret == -1) {
  2037. return;
  2038. }
  2039. queue_accessibility_update();
  2040. last_dblclk = 0;
  2041. } else if (!mb->is_shift_pressed()) {
  2042. if (drag_and_drop_selection_enabled && mouse_over_selection_caret >= 0) {
  2043. // Try to drag and drop.
  2044. set_selection_mode(SelectionMode::SELECTION_MODE_NONE);
  2045. selection_drag_attempt = true;
  2046. drag_and_drop_origin_caret_index = mouse_over_selection_caret;
  2047. last_dblclk = 0;
  2048. // Don't update caret until we know if it is not drag and drop.
  2049. return;
  2050. } else {
  2051. // A regular click clears all other carets.
  2052. caret = 0;
  2053. remove_secondary_carets();
  2054. deselect();
  2055. }
  2056. }
  2057. _push_current_op();
  2058. set_caret_line(line, false, true, -1, caret);
  2059. set_caret_column(col, false, caret);
  2060. selection_drag_attempt = false;
  2061. bool caret_moved = get_caret_column(caret) != prev_col || get_caret_line(caret) != prev_line;
  2062. if (selecting_enabled && mb->is_shift_pressed() && !has_selection(caret) && caret_moved) {
  2063. // Select from the previous caret position.
  2064. select(prev_line, prev_col, line, col, caret);
  2065. }
  2066. // Start regular select mode.
  2067. set_selection_mode(SelectionMode::SELECTION_MODE_POINTER);
  2068. _update_selection_mode_pointer(true);
  2069. } else if (is_triple_click) {
  2070. // Start triple-click select line mode.
  2071. set_selection_mode(SelectionMode::SELECTION_MODE_LINE);
  2072. _update_selection_mode_line(true);
  2073. last_dblclk = 0;
  2074. } else if (mb->is_double_click()) {
  2075. // Start double-click select word mode.
  2076. set_selection_mode(SelectionMode::SELECTION_MODE_WORD);
  2077. _update_selection_mode_word(true);
  2078. last_dblclk = OS::get_singleton()->get_ticks_msec();
  2079. last_dblclk_pos = mb->get_position();
  2080. }
  2081. // Click inline objects.
  2082. if (inline_object_click_handler.is_valid()) {
  2083. int xmargin_beg = Math::ceil(theme_cache.style_normal->get_margin(SIDE_LEFT)) + gutters_width + gutter_padding;
  2084. int wrap_i = get_line_wrap_index_at_column(pos.y, pos.x);
  2085. const float wrap_indent = _get_wrap_indent_offset(pos.y, wrap_i, is_layout_rtl());
  2086. Ref<TextParagraph> ldata = text.get_line_data(line);
  2087. for (const Variant &inline_key : ldata->get_line_objects(wrap_i)) {
  2088. if (!is_inline_info_valid(inline_key)) {
  2089. continue;
  2090. }
  2091. Dictionary info = inline_key.duplicate();
  2092. info["line"] = line;
  2093. Rect2 obj_rect = ldata->get_line_object_rect(wrap_i, inline_key);
  2094. obj_rect.position.x += xmargin_beg + wrap_indent - first_visible_col;
  2095. if (mpos.x > obj_rect.position.x && mpos.x < obj_rect.get_end().x) {
  2096. Rect2 col_rect = get_rect_at_line_column(line, col);
  2097. col_rect.position += get_screen_position() + Vector2(col_rect.size.x, 0);
  2098. col_rect.size = obj_rect.size;
  2099. set_selection_mode(TextEdit::SelectionMode::SELECTION_MODE_NONE);
  2100. inline_object_click_handler.call(info, col_rect);
  2101. break;
  2102. }
  2103. }
  2104. }
  2105. queue_accessibility_update();
  2106. queue_redraw();
  2107. }
  2108. if (is_middle_mouse_paste_enabled() && mb->get_button_index() == MouseButton::MIDDLE && DisplayServer::get_singleton()->has_feature(DisplayServer::FEATURE_CLIPBOARD_PRIMARY)) {
  2109. apply_ime();
  2110. paste_primary_clipboard();
  2111. }
  2112. if (mb->get_button_index() == MouseButton::RIGHT && (context_menu_enabled || is_move_caret_on_right_click_enabled())) {
  2113. _push_current_op();
  2114. _reset_caret_blink_timer();
  2115. apply_ime();
  2116. _cancel_drag_and_drop_text();
  2117. set_selection_mode(SELECTION_MODE_NONE);
  2118. Point2i pos = get_line_column_at_pos(mpos);
  2119. int mouse_line = pos.y;
  2120. int mouse_column = pos.x;
  2121. if (is_move_caret_on_right_click_enabled()) {
  2122. bool selection_clicked = get_selection_at_line_column(mouse_line, mouse_column, true) >= 0;
  2123. if (!selection_clicked) {
  2124. deselect();
  2125. remove_secondary_carets();
  2126. set_caret_line(mouse_line, false, false, -1);
  2127. set_caret_column(mouse_column);
  2128. }
  2129. }
  2130. if (context_menu_enabled) {
  2131. _update_context_menu();
  2132. menu->set_position(get_screen_transform().xform(mpos));
  2133. menu->reset_size();
  2134. menu->popup();
  2135. grab_focus();
  2136. }
  2137. }
  2138. } else {
  2139. if (has_ime_text()) {
  2140. // Ignore mouse up in IME input mode.
  2141. return;
  2142. }
  2143. if (mb->get_button_index() == MouseButton::LEFT) {
  2144. if (!drag_action && selection_drag_attempt && is_mouse_over_selection()) {
  2145. // This is not a drag and drop attempt, update the caret.
  2146. selection_drag_attempt = false;
  2147. remove_secondary_carets();
  2148. deselect();
  2149. Point2i pos = get_line_column_at_pos(get_local_mouse_pos());
  2150. set_caret_line(pos.y, false, true, -1, 0);
  2151. set_caret_column(pos.x, true, 0);
  2152. }
  2153. dragging_minimap = false;
  2154. dragging_selection = false;
  2155. can_drag_minimap = false;
  2156. click_select_held->stop();
  2157. if (DisplayServer::get_singleton()->has_feature(DisplayServer::FEATURE_CLIPBOARD_PRIMARY)) {
  2158. DisplayServer::get_singleton()->clipboard_set_primary(get_selected_text());
  2159. }
  2160. }
  2161. if (editable) {
  2162. _show_virtual_keyboard();
  2163. }
  2164. }
  2165. }
  2166. const Ref<InputEventPanGesture> pan_gesture = p_gui_input;
  2167. if (pan_gesture.is_valid()) {
  2168. const real_t delta = pan_gesture->get_delta().y;
  2169. if (delta < 0) {
  2170. _scroll_up(-delta, false);
  2171. } else {
  2172. _scroll_down(delta, false);
  2173. }
  2174. h_scroll->set_value(h_scroll->get_value() + pan_gesture->get_delta().x * 100);
  2175. if (v_scroll->get_value() != prev_v_scroll || h_scroll->get_value() != prev_h_scroll) {
  2176. accept_event(); // Accept event if scroll changed.
  2177. }
  2178. queue_accessibility_update();
  2179. return;
  2180. }
  2181. Ref<InputEventMouseMotion> mm = p_gui_input;
  2182. if (mm.is_valid()) {
  2183. Vector2i mpos = mm->get_position();
  2184. if (is_layout_rtl()) {
  2185. mpos.x = get_size().x - mpos.x;
  2186. }
  2187. if (mm->get_button_mask().has_flag(MouseButtonMask::LEFT) && get_viewport()->gui_get_drag_data() == Variant()) {
  2188. // Update if not in drag and drop.
  2189. _reset_caret_blink_timer();
  2190. if (draw_minimap && !dragging_selection) {
  2191. _update_minimap_drag();
  2192. }
  2193. if (!dragging_minimap && !has_ime_text()) {
  2194. switch (selecting_mode) {
  2195. case SelectionMode::SELECTION_MODE_POINTER: {
  2196. _update_selection_mode_pointer();
  2197. } break;
  2198. case SelectionMode::SELECTION_MODE_WORD: {
  2199. _update_selection_mode_word();
  2200. } break;
  2201. case SelectionMode::SELECTION_MODE_LINE: {
  2202. _update_selection_mode_line();
  2203. } break;
  2204. default: {
  2205. break;
  2206. }
  2207. }
  2208. }
  2209. }
  2210. // Update hovered gutter.
  2211. Vector2i current_hovered_gutter = _get_hovered_gutter(mpos);
  2212. if (current_hovered_gutter != hovered_gutter) {
  2213. hovered_gutter = current_hovered_gutter;
  2214. queue_redraw();
  2215. }
  2216. if (drag_action && can_drop_data(mpos, get_viewport()->gui_get_drag_data())) {
  2217. apply_ime();
  2218. // Update drag and drop caret.
  2219. drag_caret_force_displayed = true;
  2220. Point2i pos = get_line_column_at_pos(get_local_mouse_pos());
  2221. if (drag_caret_index == -1) {
  2222. // Force create a new caret for drag and drop.
  2223. carets.push_back(Caret());
  2224. drag_caret_index = carets.size() - 1;
  2225. }
  2226. drag_caret_force_displayed = true;
  2227. set_caret_line(pos.y, false, true, -1, drag_caret_index);
  2228. set_caret_column(pos.x, true, drag_caret_index);
  2229. dragging_selection = true;
  2230. }
  2231. }
  2232. if (draw_minimap && !dragging_selection) {
  2233. _update_minimap_hover();
  2234. }
  2235. if (v_scroll->get_value() != prev_v_scroll || h_scroll->get_value() != prev_h_scroll) {
  2236. accept_event(); // Accept event if scroll changed.
  2237. }
  2238. Ref<InputEventKey> k = p_gui_input;
  2239. if (k.is_valid()) {
  2240. if (alt_input(p_gui_input)) {
  2241. accept_event();
  2242. return;
  2243. }
  2244. if (!k->is_pressed()) {
  2245. return;
  2246. }
  2247. // If a modifier has been pressed, and nothing else, return.
  2248. if (k->get_keycode() == Key::CTRL || k->get_keycode() == Key::ALT || k->get_keycode() == Key::SHIFT || k->get_keycode() == Key::META || k->get_keycode() == Key::CAPSLOCK) {
  2249. return;
  2250. }
  2251. _cancel_drag_and_drop_text();
  2252. _reset_caret_blink_timer();
  2253. // Allow unicode handling if:
  2254. // * No modifiers are pressed (except Shift and CapsLock)
  2255. bool allow_unicode_handling = !(k->is_ctrl_pressed() || k->is_alt_pressed() || k->is_meta_pressed());
  2256. // Check and handle all built-in shortcuts.
  2257. // NEWLINES.
  2258. if (k->is_action("ui_text_newline_above", true)) {
  2259. _new_line(false, true);
  2260. accept_event();
  2261. return;
  2262. }
  2263. if (k->is_action("ui_text_newline_blank", true)) {
  2264. _new_line(false);
  2265. accept_event();
  2266. return;
  2267. }
  2268. if (k->is_action("ui_text_newline", true)) {
  2269. _new_line();
  2270. accept_event();
  2271. return;
  2272. }
  2273. // BACKSPACE AND DELETE.
  2274. if (k->is_action("ui_text_backspace_all_to_left", true)) {
  2275. _do_backspace(false, true);
  2276. accept_event();
  2277. return;
  2278. }
  2279. if (k->is_action("ui_text_backspace_word", true)) {
  2280. _do_backspace(true);
  2281. accept_event();
  2282. return;
  2283. }
  2284. if (k->is_action("ui_text_backspace", true)) {
  2285. _do_backspace();
  2286. accept_event();
  2287. return;
  2288. }
  2289. if (k->is_action("ui_text_delete_all_to_right", true)) {
  2290. _delete(false, true);
  2291. accept_event();
  2292. return;
  2293. }
  2294. if (k->is_action("ui_text_delete_word", true)) {
  2295. _delete(true);
  2296. accept_event();
  2297. return;
  2298. }
  2299. if (k->is_action("ui_text_delete", true)) {
  2300. _delete();
  2301. accept_event();
  2302. return;
  2303. }
  2304. // SCROLLING.
  2305. if (k->is_action("ui_text_scroll_up", true)) {
  2306. _scroll_lines_up();
  2307. accept_event();
  2308. return;
  2309. }
  2310. if (k->is_action("ui_text_scroll_down", true)) {
  2311. _scroll_lines_down();
  2312. accept_event();
  2313. return;
  2314. }
  2315. if (is_shortcut_keys_enabled()) {
  2316. // SELECT ALL, SELECT WORD UNDER CARET, ADD SELECTION FOR NEXT OCCURRENCE, SKIP SELECTION FOR NEXT OCCURRENCE,
  2317. // CLEAR CARETS AND SELECTIONS, CUT, COPY, PASTE.
  2318. if (k->is_action("ui_text_select_all", true)) {
  2319. select_all();
  2320. accept_event();
  2321. return;
  2322. }
  2323. if (k->is_action("ui_text_select_word_under_caret", true)) {
  2324. select_word_under_caret();
  2325. accept_event();
  2326. return;
  2327. }
  2328. if (k->is_action("ui_text_add_selection_for_next_occurrence", true)) {
  2329. add_selection_for_next_occurrence();
  2330. accept_event();
  2331. return;
  2332. }
  2333. if (k->is_action("ui_text_skip_selection_for_next_occurrence", true)) {
  2334. skip_selection_for_next_occurrence();
  2335. accept_event();
  2336. return;
  2337. }
  2338. if (k->is_action("ui_text_clear_carets_and_selection", true)) {
  2339. // Since the default shortcut is ESC, accepts the event only if it's actually performed.
  2340. if (_clear_carets_and_selection()) {
  2341. accept_event();
  2342. return;
  2343. }
  2344. }
  2345. if (k->is_action("ui_cut", true)) {
  2346. cut();
  2347. accept_event();
  2348. return;
  2349. }
  2350. if (k->is_action("ui_copy", true)) {
  2351. copy();
  2352. accept_event();
  2353. return;
  2354. }
  2355. if (k->is_action("ui_paste", true)) {
  2356. paste();
  2357. accept_event();
  2358. return;
  2359. }
  2360. // UNDO/REDO.
  2361. if (k->is_action("ui_undo", true)) {
  2362. undo();
  2363. accept_event();
  2364. return;
  2365. }
  2366. if (k->is_action("ui_redo", true)) {
  2367. redo();
  2368. accept_event();
  2369. return;
  2370. }
  2371. if (k->is_action("ui_text_caret_add_below", true)) {
  2372. add_caret_at_carets(true);
  2373. accept_event();
  2374. return;
  2375. }
  2376. if (k->is_action("ui_text_caret_add_above", true)) {
  2377. add_caret_at_carets(false);
  2378. accept_event();
  2379. return;
  2380. }
  2381. }
  2382. // MISC.
  2383. if (k->is_action("ui_menu", true)) {
  2384. _push_current_op();
  2385. if (context_menu_enabled) {
  2386. _update_context_menu();
  2387. adjust_viewport_to_caret();
  2388. menu->set_position(get_screen_transform().xform(get_caret_draw_pos()));
  2389. menu->reset_size();
  2390. menu->popup();
  2391. menu->grab_focus();
  2392. }
  2393. accept_event();
  2394. return;
  2395. }
  2396. if (k->is_action("ui_text_toggle_insert_mode", true)) {
  2397. set_overtype_mode_enabled(!overtype_mode);
  2398. accept_event();
  2399. return;
  2400. }
  2401. if (k->is_action("ui_swap_input_direction", true)) {
  2402. _swap_current_input_direction();
  2403. accept_event();
  2404. return;
  2405. }
  2406. // CARET MOVEMENT
  2407. k = k->duplicate();
  2408. bool shift_pressed = k->is_shift_pressed();
  2409. // Remove shift or else actions will not match. Use above variable for selection.
  2410. k->set_shift_pressed(false);
  2411. // CARET MOVEMENT - LEFT, RIGHT.
  2412. if (k->is_action("ui_text_caret_word_left", true)) {
  2413. _move_caret_left(shift_pressed, true);
  2414. accept_event();
  2415. return;
  2416. }
  2417. if (k->is_action("ui_text_caret_left", true)) {
  2418. _move_caret_left(shift_pressed, false);
  2419. accept_event();
  2420. return;
  2421. }
  2422. if (k->is_action("ui_text_caret_word_right", true)) {
  2423. _move_caret_right(shift_pressed, true);
  2424. accept_event();
  2425. return;
  2426. }
  2427. if (k->is_action("ui_text_caret_right", true)) {
  2428. _move_caret_right(shift_pressed, false);
  2429. accept_event();
  2430. return;
  2431. }
  2432. // CARET MOVEMENT - UP, DOWN.
  2433. if (k->is_action("ui_text_caret_up", true)) {
  2434. _move_caret_up(shift_pressed);
  2435. accept_event();
  2436. return;
  2437. }
  2438. if (k->is_action("ui_text_caret_down", true)) {
  2439. _move_caret_down(shift_pressed);
  2440. accept_event();
  2441. return;
  2442. }
  2443. // CARET MOVEMENT - DOCUMENT START/END.
  2444. if (k->is_action("ui_text_caret_document_start", true)) { // && shift_pressed) {
  2445. _move_caret_document_start(shift_pressed);
  2446. accept_event();
  2447. return;
  2448. }
  2449. if (k->is_action("ui_text_caret_document_end", true)) { // && shift_pressed) {
  2450. _move_caret_document_end(shift_pressed);
  2451. accept_event();
  2452. return;
  2453. }
  2454. // CARET MOVEMENT - LINE START/END.
  2455. if (k->is_action("ui_text_caret_line_start", true)) {
  2456. _move_caret_to_line_start(shift_pressed);
  2457. accept_event();
  2458. return;
  2459. }
  2460. if (k->is_action("ui_text_caret_line_end", true)) {
  2461. _move_caret_to_line_end(shift_pressed);
  2462. accept_event();
  2463. return;
  2464. }
  2465. // CARET MOVEMENT - PAGE UP/DOWN.
  2466. if (k->is_action("ui_text_caret_page_up", true)) {
  2467. _move_caret_page_up(shift_pressed);
  2468. accept_event();
  2469. return;
  2470. }
  2471. if (k->is_action("ui_text_caret_page_down", true)) {
  2472. _move_caret_page_down(shift_pressed);
  2473. accept_event();
  2474. return;
  2475. }
  2476. // Toggle Tab mode.
  2477. if (k->is_action("ui_focus_mode", true)) {
  2478. tab_input_mode = !tab_input_mode;
  2479. accept_event();
  2480. return;
  2481. }
  2482. // Handle tab as it has no set unicode value.
  2483. if (tab_input_mode && k->is_action("ui_text_indent", true)) {
  2484. if (editable) {
  2485. insert_text_at_caret("\t");
  2486. }
  2487. accept_event();
  2488. return;
  2489. }
  2490. // Handle Unicode (if no modifiers active).
  2491. if (allow_unicode_handling && editable && k->get_unicode() >= 32) {
  2492. handle_unicode_input(k->get_unicode());
  2493. accept_event();
  2494. return;
  2495. }
  2496. }
  2497. }
  2498. /* Input actions. */
  2499. void TextEdit::_swap_current_input_direction() {
  2500. if (input_direction == TEXT_DIRECTION_LTR) {
  2501. input_direction = TEXT_DIRECTION_RTL;
  2502. } else {
  2503. input_direction = TEXT_DIRECTION_LTR;
  2504. }
  2505. for (int i = 0; i < carets.size(); i++) {
  2506. set_caret_column(get_caret_column(i), i == 0, i);
  2507. }
  2508. queue_redraw();
  2509. }
  2510. void TextEdit::_new_line(bool p_split_current_line, bool p_above) {
  2511. if (!editable) {
  2512. return;
  2513. }
  2514. begin_complex_operation();
  2515. begin_multicaret_edit();
  2516. for (int i = 0; i < get_caret_count(); i++) {
  2517. if (multicaret_edit_ignore_caret(i)) {
  2518. continue;
  2519. }
  2520. if (p_split_current_line) {
  2521. insert_text_at_caret("\n", i);
  2522. } else {
  2523. int line = get_caret_line(i);
  2524. insert_text("\n", line, p_above ? 0 : text[line].length(), p_above, p_above);
  2525. deselect(i);
  2526. set_caret_line(p_above ? line : line + 1, false, true, -1, i);
  2527. set_caret_column(0, i == 0, i);
  2528. }
  2529. }
  2530. end_multicaret_edit();
  2531. end_complex_operation();
  2532. }
  2533. void TextEdit::_move_caret_left(bool p_select, bool p_move_by_word) {
  2534. _push_current_op();
  2535. for (int i = 0; i < get_caret_count(); i++) {
  2536. // Handle selection.
  2537. if (p_select) {
  2538. _pre_shift_selection(i);
  2539. } else if (has_selection(i) && !p_move_by_word) {
  2540. // If a selection is active, move caret to start of selection.
  2541. set_caret_line(get_selection_from_line(i), false, true, -1, i);
  2542. set_caret_column(get_selection_from_column(i), i == 0, i);
  2543. deselect(i);
  2544. continue;
  2545. } else {
  2546. deselect(i);
  2547. }
  2548. if (p_move_by_word) {
  2549. int cc = get_caret_column(i);
  2550. // 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.
  2551. if (cc == 0 && get_caret_line(i) > 0) {
  2552. set_caret_line(get_caret_line(i) - 1, false, true, -1, i);
  2553. set_caret_column(text[get_caret_line(i)].length(), i == 0, i);
  2554. } else {
  2555. PackedInt32Array words = TS->shaped_text_get_word_breaks(text.get_line_data(get_caret_line(i))->get_rid());
  2556. if (words.is_empty() || cc <= words[0]) {
  2557. // Move to the start when there are no more words.
  2558. cc = 0;
  2559. } else {
  2560. for (int j = words.size() - 2; j >= 0; j = j - 2) {
  2561. if (words[j] < cc) {
  2562. cc = words[j];
  2563. break;
  2564. }
  2565. }
  2566. }
  2567. set_caret_column(cc, i == 0, i);
  2568. }
  2569. } else {
  2570. // 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.
  2571. if (get_caret_column(i) == 0) {
  2572. if (get_caret_line(i) > 0) {
  2573. int new_caret_line = get_caret_line(i) - get_next_visible_line_offset_from(CLAMP(get_caret_line(i) - 1, 0, text.size() - 1), -1);
  2574. set_caret_line(new_caret_line, false, true, -1, i);
  2575. set_caret_column(text[get_caret_line(i)].length(), i == 0, i);
  2576. }
  2577. } else {
  2578. if (caret_mid_grapheme_enabled) {
  2579. set_caret_column(get_caret_column(i) - 1, i == 0, i);
  2580. } else {
  2581. set_caret_column(TS->shaped_text_prev_character_pos(text.get_line_data(get_caret_line(i))->get_rid(), get_caret_column(i)), i == 0, i);
  2582. }
  2583. }
  2584. }
  2585. }
  2586. merge_overlapping_carets();
  2587. }
  2588. void TextEdit::_move_caret_right(bool p_select, bool p_move_by_word) {
  2589. _push_current_op();
  2590. for (int i = 0; i < get_caret_count(); i++) {
  2591. // Handle selection.
  2592. if (p_select) {
  2593. _pre_shift_selection(i);
  2594. } else if (has_selection(i) && !p_move_by_word) {
  2595. // If a selection is active, move caret to end of selection.
  2596. set_caret_line(get_selection_to_line(i), false, true, -1, i);
  2597. set_caret_column(get_selection_to_column(i), i == 0, i);
  2598. deselect(i);
  2599. continue;
  2600. } else {
  2601. deselect(i);
  2602. }
  2603. if (p_move_by_word) {
  2604. int cc = get_caret_column(i);
  2605. // 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.
  2606. if (cc == text[get_caret_line(i)].length() && get_caret_line(i) < text.size() - 1) {
  2607. set_caret_line(get_caret_line(i) + 1, false, true, -1, i);
  2608. set_caret_column(0, i == 0, i);
  2609. } else {
  2610. PackedInt32Array words = TS->shaped_text_get_word_breaks(text.get_line_data(get_caret_line(i))->get_rid());
  2611. if (words.is_empty() || cc >= words[words.size() - 1]) {
  2612. // Move to the end when there are no more words.
  2613. cc = text[get_caret_line(i)].length();
  2614. } else {
  2615. for (int j = 1; j < words.size(); j = j + 2) {
  2616. if (words[j] > cc) {
  2617. cc = words[j];
  2618. break;
  2619. }
  2620. }
  2621. }
  2622. set_caret_column(cc, i == 0, i);
  2623. }
  2624. } else {
  2625. // If we are at the end of the line, move the caret to the next line down.
  2626. if (get_caret_column(i) == text[get_caret_line(i)].length()) {
  2627. if (get_caret_line(i) < text.size() - 1) {
  2628. int new_caret_line = get_caret_line(i) + get_next_visible_line_offset_from(CLAMP(get_caret_line(i) + 1, 0, text.size() - 1), 1);
  2629. set_caret_line(new_caret_line, false, false, -1, i);
  2630. set_caret_column(0, i == 0, i);
  2631. }
  2632. } else {
  2633. if (caret_mid_grapheme_enabled) {
  2634. set_caret_column(get_caret_column(i) + 1, i == 0, i);
  2635. } else {
  2636. set_caret_column(TS->shaped_text_next_character_pos(text.get_line_data(get_caret_line(i))->get_rid(), get_caret_column(i)), i == 0, i);
  2637. }
  2638. }
  2639. }
  2640. }
  2641. merge_overlapping_carets();
  2642. }
  2643. void TextEdit::_move_caret_up(bool p_select) {
  2644. _push_current_op();
  2645. for (int i = 0; i < get_caret_count(); i++) {
  2646. if (p_select) {
  2647. _pre_shift_selection(i);
  2648. } else {
  2649. deselect(i);
  2650. }
  2651. int cur_wrap_index = get_caret_wrap_index(i);
  2652. if (cur_wrap_index > 0) {
  2653. set_caret_line(get_caret_line(i), true, false, cur_wrap_index - 1, i);
  2654. } else if (get_caret_line(i) == 0) {
  2655. set_caret_column(0, i == 0, i);
  2656. } else {
  2657. int new_line = get_caret_line(i) - get_next_visible_line_offset_from(get_caret_line(i) - 1, -1);
  2658. if (is_line_wrapped(new_line)) {
  2659. set_caret_line(new_line, i == 0, false, get_line_wrap_count(new_line), i);
  2660. } else {
  2661. set_caret_line(new_line, i == 0, false, 0, i);
  2662. }
  2663. }
  2664. }
  2665. merge_overlapping_carets();
  2666. }
  2667. void TextEdit::_move_caret_down(bool p_select) {
  2668. _push_current_op();
  2669. for (int i = 0; i < get_caret_count(); i++) {
  2670. if (p_select) {
  2671. _pre_shift_selection(i);
  2672. } else {
  2673. deselect(i);
  2674. }
  2675. int cur_wrap_index = get_caret_wrap_index(i);
  2676. if (cur_wrap_index < get_line_wrap_count(get_caret_line(i))) {
  2677. set_caret_line(get_caret_line(i), i == 0, false, cur_wrap_index + 1, i);
  2678. } else if (get_caret_line(i) == get_last_unhidden_line()) {
  2679. set_caret_column(text[get_caret_line(i)].length());
  2680. } else {
  2681. int new_line = get_caret_line(i) + get_next_visible_line_offset_from(CLAMP(get_caret_line(i) + 1, 0, text.size() - 1), 1);
  2682. set_caret_line(new_line, i == 0, false, 0, i);
  2683. }
  2684. }
  2685. merge_overlapping_carets();
  2686. }
  2687. void TextEdit::_move_caret_to_line_start(bool p_select) {
  2688. _push_current_op();
  2689. for (int i = 0; i < get_caret_count(); i++) {
  2690. if (p_select) {
  2691. _pre_shift_selection(i);
  2692. } else {
  2693. deselect(i);
  2694. }
  2695. // Move caret column to start of wrapped row and then to start of text.
  2696. Vector<String> rows = get_line_wrapped_text(get_caret_line(i));
  2697. int wi = get_caret_wrap_index(i);
  2698. int row_start_col = 0;
  2699. for (int j = 0; j < wi; j++) {
  2700. row_start_col += rows[j].length();
  2701. }
  2702. if (get_caret_column(i) == row_start_col || wi == 0) {
  2703. // Compute whitespace symbols sequence length.
  2704. int current_line_whitespace_len = get_first_non_whitespace_column(get_caret_line(i));
  2705. if (get_caret_column(i) == current_line_whitespace_len) {
  2706. set_caret_column(0, i == 0, i);
  2707. } else {
  2708. set_caret_column(current_line_whitespace_len, i == 0, i);
  2709. }
  2710. } else {
  2711. set_caret_column(row_start_col, i == 0, i);
  2712. }
  2713. }
  2714. merge_overlapping_carets();
  2715. }
  2716. void TextEdit::_move_caret_to_line_end(bool p_select) {
  2717. _push_current_op();
  2718. for (int i = 0; i < get_caret_count(); i++) {
  2719. if (p_select) {
  2720. _pre_shift_selection(i);
  2721. } else {
  2722. deselect(i);
  2723. }
  2724. // Move caret column to end of wrapped row and then to end of text.
  2725. Vector<String> rows = get_line_wrapped_text(get_caret_line(i));
  2726. int wi = get_caret_wrap_index(i);
  2727. int row_end_col = -1;
  2728. for (int j = 0; j < wi + 1; j++) {
  2729. row_end_col += rows[j].length();
  2730. }
  2731. if (wi == rows.size() - 1 || get_caret_column(i) == row_end_col) {
  2732. set_caret_column(text[get_caret_line(i)].length(), i == 0, i);
  2733. } else {
  2734. set_caret_column(row_end_col, i == 0, i);
  2735. }
  2736. }
  2737. merge_overlapping_carets();
  2738. }
  2739. void TextEdit::_move_caret_page_up(bool p_select) {
  2740. _push_current_op();
  2741. for (int i = 0; i < get_caret_count(); i++) {
  2742. if (p_select) {
  2743. _pre_shift_selection(i);
  2744. } else {
  2745. deselect(i);
  2746. }
  2747. Point2i next_line = get_next_visible_line_index_offset_from(get_caret_line(i), get_caret_wrap_index(i), -get_visible_line_count());
  2748. int n_line = get_caret_line(i) - next_line.x + 1;
  2749. set_caret_line(n_line, i == 0, false, next_line.y, i);
  2750. }
  2751. merge_overlapping_carets();
  2752. }
  2753. void TextEdit::_move_caret_page_down(bool p_select) {
  2754. _push_current_op();
  2755. for (int i = 0; i < get_caret_count(); i++) {
  2756. if (p_select) {
  2757. _pre_shift_selection(i);
  2758. } else {
  2759. deselect(i);
  2760. }
  2761. Point2i next_line = get_next_visible_line_index_offset_from(get_caret_line(i), get_caret_wrap_index(i), get_visible_line_count());
  2762. int n_line = get_caret_line(i) + next_line.x - 1;
  2763. set_caret_line(n_line, i == 0, false, next_line.y, i);
  2764. }
  2765. merge_overlapping_carets();
  2766. }
  2767. void TextEdit::_do_backspace(bool p_word, bool p_all_to_left) {
  2768. if (!editable) {
  2769. return;
  2770. }
  2771. start_action(EditAction::ACTION_BACKSPACE);
  2772. begin_multicaret_edit();
  2773. Vector<int> sorted_carets = get_sorted_carets();
  2774. sorted_carets.reverse();
  2775. for (int i = 0; i < sorted_carets.size(); i++) {
  2776. int caret_index = sorted_carets[i];
  2777. if (multicaret_edit_ignore_caret(caret_index)) {
  2778. continue;
  2779. }
  2780. if (get_caret_column(caret_index) == 0 && get_caret_line(caret_index) == 0 && !has_selection(caret_index)) {
  2781. continue;
  2782. }
  2783. if (has_selection(caret_index) || (!p_all_to_left && !p_word) || get_caret_column(caret_index) == 0) {
  2784. backspace(caret_index);
  2785. continue;
  2786. }
  2787. if (p_all_to_left) {
  2788. // Remove everything to left of caret to the start of the line.
  2789. int caret_current_column = get_caret_column(caret_index);
  2790. _remove_text(get_caret_line(caret_index), 0, get_caret_line(caret_index), caret_current_column);
  2791. collapse_carets(get_caret_line(caret_index), 0, get_caret_line(caret_index), caret_current_column);
  2792. set_caret_column(0, caret_index == 0, caret_index);
  2793. _offset_carets_after(get_caret_line(caret_index), caret_current_column, get_caret_line(caret_index), 0);
  2794. continue;
  2795. }
  2796. if (p_word) {
  2797. // Remove text to the start of the word left of the caret.
  2798. int from_column = get_caret_column(caret_index);
  2799. int column = get_caret_column(caret_index);
  2800. // Check for the case "<word><space><caret>" and ignore the space.
  2801. // No need to check for column being 0 since it is checked above.
  2802. if (is_whitespace(text[get_caret_line(caret_index)][get_caret_column(caret_index) - 1])) {
  2803. column -= 1;
  2804. }
  2805. // Get a list with the indices of the word bounds of the given text line.
  2806. const PackedInt32Array words = TS->shaped_text_get_word_breaks(text.get_line_data(get_caret_line(caret_index))->get_rid());
  2807. if (words.is_empty() || column <= words[0]) {
  2808. // Delete to the start when there are no more words.
  2809. column = 0;
  2810. } else {
  2811. // Otherwise search for the first word break that is smaller than the index from we're currently deleting.
  2812. for (int c = words.size() - 2; c >= 0; c = c - 2) {
  2813. if (words[c] < column) {
  2814. column = words[c];
  2815. break;
  2816. }
  2817. }
  2818. }
  2819. _remove_text(get_caret_line(caret_index), column, get_caret_line(caret_index), from_column);
  2820. collapse_carets(get_caret_line(caret_index), column, get_caret_line(caret_index), from_column);
  2821. set_caret_column(column, caret_index == 0, caret_index);
  2822. _offset_carets_after(get_caret_line(caret_index), from_column, get_caret_line(caret_index), column);
  2823. }
  2824. }
  2825. end_multicaret_edit();
  2826. end_action();
  2827. }
  2828. void TextEdit::_delete(bool p_word, bool p_all_to_right) {
  2829. if (!editable) {
  2830. return;
  2831. }
  2832. start_action(EditAction::ACTION_DELETE);
  2833. begin_multicaret_edit();
  2834. Vector<int> sorted_carets = get_sorted_carets();
  2835. for (int i = 0; i < sorted_carets.size(); i++) {
  2836. int caret_index = sorted_carets[i];
  2837. if (multicaret_edit_ignore_caret(caret_index)) {
  2838. continue;
  2839. }
  2840. if (has_selection(caret_index)) {
  2841. delete_selection(caret_index);
  2842. continue;
  2843. }
  2844. int curline_len = text[get_caret_line(caret_index)].length();
  2845. if (get_caret_line(caret_index) == text.size() - 1 && get_caret_column(caret_index) == curline_len) {
  2846. continue; // Last line, last column: Nothing to do.
  2847. }
  2848. int next_line = get_caret_column(caret_index) < curline_len ? get_caret_line(caret_index) : get_caret_line(caret_index) + 1;
  2849. int next_column;
  2850. if (p_all_to_right) {
  2851. if (get_caret_column(caret_index) == curline_len) {
  2852. continue;
  2853. }
  2854. // Delete everything to right of caret.
  2855. next_column = curline_len;
  2856. next_line = get_caret_line(caret_index);
  2857. } else if (p_word && get_caret_column(caret_index) < curline_len - 1) {
  2858. // Delete next word to right of caret.
  2859. int line = get_caret_line(caret_index);
  2860. int column = get_caret_column(caret_index);
  2861. PackedInt32Array words = TS->shaped_text_get_word_breaks(text.get_line_data(line)->get_rid());
  2862. if (words.is_empty() || column >= words[words.size() - 1]) {
  2863. // Delete to the end when there are no more words.
  2864. column = text[get_caret_line(i)].length();
  2865. } else {
  2866. for (int j = 1; j < words.size(); j = j + 2) {
  2867. if (words[j] > column) {
  2868. column = words[j];
  2869. break;
  2870. }
  2871. }
  2872. }
  2873. next_line = line;
  2874. next_column = column;
  2875. } else {
  2876. // Delete one character.
  2877. if (caret_mid_grapheme_enabled) {
  2878. next_column = get_caret_column(caret_index) < curline_len ? (get_caret_column(caret_index) + 1) : 0;
  2879. } else {
  2880. next_column = get_caret_column(caret_index) < curline_len ? TS->shaped_text_next_character_pos(text.get_line_data(get_caret_line(caret_index))->get_rid(), (get_caret_column(caret_index))) : 0;
  2881. }
  2882. }
  2883. _remove_text(get_caret_line(caret_index), get_caret_column(caret_index), next_line, next_column);
  2884. collapse_carets(get_caret_line(caret_index), get_caret_column(caret_index), next_line, next_column);
  2885. _offset_carets_after(next_line, next_column, get_caret_line(caret_index), get_caret_column(caret_index));
  2886. }
  2887. end_multicaret_edit();
  2888. end_action();
  2889. }
  2890. void TextEdit::_move_caret_document_start(bool p_select) {
  2891. remove_secondary_carets();
  2892. if (p_select) {
  2893. _pre_shift_selection(0);
  2894. } else {
  2895. deselect();
  2896. }
  2897. set_caret_line(0, false, true, -1);
  2898. set_caret_column(0);
  2899. }
  2900. void TextEdit::_move_caret_document_end(bool p_select) {
  2901. remove_secondary_carets();
  2902. if (p_select) {
  2903. _pre_shift_selection(0);
  2904. } else {
  2905. deselect();
  2906. }
  2907. set_caret_line(get_last_unhidden_line(), true, false, -1);
  2908. set_caret_column(text[get_caret_line()].length());
  2909. }
  2910. bool TextEdit::_clear_carets_and_selection() {
  2911. _push_current_op();
  2912. if (get_caret_count() > 1) {
  2913. remove_secondary_carets();
  2914. return true;
  2915. }
  2916. if (has_selection()) {
  2917. deselect();
  2918. return true;
  2919. }
  2920. return false;
  2921. }
  2922. void TextEdit::_update_placeholder() {
  2923. if (theme_cache.font.is_null() || theme_cache.font_size <= 0) {
  2924. return; // Not in tree?
  2925. }
  2926. const String placeholder_translated = atr(placeholder_text);
  2927. // Placeholder is generally smaller then text documents, and updates less so this should be fast enough for now.
  2928. placeholder_data_buf->clear();
  2929. placeholder_data_buf->set_width(text.get_width());
  2930. BitField<TextServer::LineBreakFlag> flags = text.get_brk_flags();
  2931. if (text.is_indent_wrapped_lines()) {
  2932. flags.set_flag(TextServer::BREAK_TRIM_INDENT);
  2933. }
  2934. placeholder_data_buf->set_break_flags(flags);
  2935. if (text_direction == Control::TEXT_DIRECTION_INHERITED) {
  2936. placeholder_data_buf->set_direction(is_layout_rtl() ? TextServer::DIRECTION_RTL : TextServer::DIRECTION_LTR);
  2937. } else {
  2938. placeholder_data_buf->set_direction((TextServer::Direction)text_direction);
  2939. }
  2940. placeholder_data_buf->set_preserve_control(draw_control_chars);
  2941. placeholder_data_buf->add_string(placeholder_translated, theme_cache.font, theme_cache.font_size, language);
  2942. placeholder_bidi_override = structured_text_parser(st_parser, st_args, placeholder_translated);
  2943. if (placeholder_bidi_override.is_empty()) {
  2944. TS->shaped_text_set_bidi_override(placeholder_data_buf->get_rid(), placeholder_bidi_override);
  2945. }
  2946. if (get_tab_size() > 0) {
  2947. Vector<float> tabs;
  2948. tabs.push_back(theme_cache.font->get_char_size(' ', theme_cache.font_size).width * get_tab_size());
  2949. placeholder_data_buf->tab_align(tabs);
  2950. }
  2951. // Update height.
  2952. const int wrap_amount = placeholder_data_buf->get_line_count() - 1;
  2953. placeholder_line_height = theme_cache.font->get_height(theme_cache.font_size);
  2954. for (int i = 0; i <= wrap_amount; i++) {
  2955. placeholder_line_height = MAX(placeholder_line_height, placeholder_data_buf->get_line_size(i).y);
  2956. }
  2957. // Update width.
  2958. placeholder_max_width = placeholder_data_buf->get_size().x;
  2959. // Update wrapped rows.
  2960. placeholder_wrapped_rows.clear();
  2961. for (int i = 0; i <= wrap_amount; i++) {
  2962. Vector2i line_range = placeholder_data_buf->get_line_range(i);
  2963. placeholder_wrapped_rows.push_back(placeholder_translated.substr(line_range.x, line_range.y - line_range.x));
  2964. }
  2965. }
  2966. bool TextEdit::_using_placeholder() const {
  2967. return text.size() == 1 && text[0].is_empty() && ime_text.is_empty();
  2968. }
  2969. void TextEdit::_update_theme_item_cache() {
  2970. Control::_update_theme_item_cache();
  2971. theme_cache.base_scale = get_theme_default_base_scale();
  2972. use_selected_font_color = theme_cache.font_selected_color != Color(0, 0, 0, 0);
  2973. if (text.get_line_height() + theme_cache.line_spacing < 1) {
  2974. WARN_PRINT("Line height is too small, please increase font_size and/or line_spacing");
  2975. }
  2976. }
  2977. void TextEdit::_update_caches() {
  2978. /* Text properties. */
  2979. TextServer::Direction dir;
  2980. if (text_direction == Control::TEXT_DIRECTION_INHERITED) {
  2981. dir = is_layout_rtl() ? TextServer::DIRECTION_RTL : TextServer::DIRECTION_LTR;
  2982. } else {
  2983. dir = (TextServer::Direction)text_direction;
  2984. }
  2985. text.set_direction_and_language(dir, (!language.is_empty()) ? language : TranslationServer::get_singleton()->get_tool_locale());
  2986. text.set_draw_control_chars(draw_control_chars);
  2987. text.set_font(theme_cache.font);
  2988. text.set_font_size(theme_cache.font_size);
  2989. text.invalidate_font();
  2990. _update_placeholder();
  2991. /* Syntax highlighting. */
  2992. if (syntax_highlighter.is_valid()) {
  2993. syntax_highlighter->set_text_edit(this);
  2994. }
  2995. _clear_syntax_highlighting_cache();
  2996. queue_accessibility_update();
  2997. }
  2998. void TextEdit::_close_ime_window() {
  2999. DisplayServer::WindowID wid = get_window() ? get_window()->get_window_id() : DisplayServer::INVALID_WINDOW_ID;
  3000. if (wid == DisplayServer::INVALID_WINDOW_ID || !DisplayServer::get_singleton()->has_feature(DisplayServer::FEATURE_IME)) {
  3001. return;
  3002. }
  3003. DisplayServer::get_singleton()->window_set_ime_position(Point2(), wid);
  3004. DisplayServer::get_singleton()->window_set_ime_active(false, wid);
  3005. }
  3006. void TextEdit::_update_ime_window_position() {
  3007. DisplayServer::WindowID wid = get_window() ? get_window()->get_window_id() : DisplayServer::INVALID_WINDOW_ID;
  3008. if (wid == DisplayServer::INVALID_WINDOW_ID || !DisplayServer::get_singleton()->has_feature(DisplayServer::FEATURE_IME)) {
  3009. return;
  3010. }
  3011. DisplayServer::get_singleton()->window_set_ime_active(true, wid);
  3012. Point2 pos = get_global_position() + get_caret_draw_pos();
  3013. if (get_window()->get_embedder()) {
  3014. pos += get_viewport()->get_popup_base_transform().get_origin();
  3015. }
  3016. // Take into account the window's transform.
  3017. pos = get_window()->get_screen_transform().xform(pos);
  3018. // The window will move to the updated position the next time the IME is updated, not immediately.
  3019. DisplayServer::get_singleton()->window_set_ime_position(pos, wid);
  3020. }
  3021. void TextEdit::_update_ime_text() {
  3022. if (has_ime_text()) {
  3023. // Update text to visually include IME text.
  3024. for (int i = 0; i < get_caret_count(); i++) {
  3025. int l = get_caret_line(i);
  3026. String text_with_ime = text[l].substr(0, get_caret_column(i)) + ime_text + text[l].substr(get_caret_column(i));
  3027. text.set_ime(l, text_with_ime, structured_text_parser(st_parser, st_args, text_with_ime));
  3028. emit_signal(SNAME("lines_edited_from"), l, l);
  3029. }
  3030. } else {
  3031. // Reset text.
  3032. for (int i = 0; i < get_caret_count(); i++) {
  3033. int l = get_caret_line(i);
  3034. text.set_ime(l, String(), Array());
  3035. emit_signal(SNAME("lines_edited_from"), l, l);
  3036. }
  3037. }
  3038. _clear_syntax_highlighting_cache();
  3039. queue_accessibility_update();
  3040. queue_redraw();
  3041. }
  3042. void TextEdit::_show_virtual_keyboard() {
  3043. _update_ime_window_position();
  3044. if (virtual_keyboard_enabled && DisplayServer::get_singleton()->has_feature(DisplayServer::FEATURE_VIRTUAL_KEYBOARD)) {
  3045. int caret_start = -1;
  3046. int caret_end = -1;
  3047. if (!has_selection(0)) {
  3048. String full_text = _base_get_text(0, 0, get_caret_line(), get_caret_column());
  3049. caret_start = full_text.length();
  3050. } else {
  3051. String pre_text = _base_get_text(0, 0, get_selection_from_line(), get_selection_from_column());
  3052. String post_text = get_selected_text(0);
  3053. caret_start = pre_text.length();
  3054. caret_end = caret_start + post_text.length();
  3055. }
  3056. DisplayServer::get_singleton()->virtual_keyboard_show(get_text(), get_global_rect(), DisplayServer::KEYBOARD_TYPE_MULTILINE, -1, caret_start, caret_end);
  3057. }
  3058. }
  3059. /* General overrides. */
  3060. Size2 TextEdit::get_minimum_size() const {
  3061. Size2 size = theme_cache.style_normal->get_minimum_size();
  3062. if (fit_content_height) {
  3063. size.y += content_size_cache.y;
  3064. }
  3065. if (fit_content_width) {
  3066. size.x += content_size_cache.x;
  3067. }
  3068. return size;
  3069. }
  3070. bool TextEdit::is_text_field() const {
  3071. return true;
  3072. }
  3073. Variant TextEdit::get_drag_data(const Point2 &p_point) {
  3074. Variant ret = Control::get_drag_data(p_point);
  3075. if (ret != Variant()) {
  3076. return ret;
  3077. }
  3078. if (has_selection() && selection_drag_attempt) {
  3079. String t = get_selected_text();
  3080. Label *l = memnew(Label);
  3081. l->set_auto_translate_mode(AUTO_TRANSLATE_MODE_DISABLED); // Don't translate user input.
  3082. l->set_focus_mode(FOCUS_ACCESSIBILITY);
  3083. l->set_text(t);
  3084. set_drag_preview(l);
  3085. return t;
  3086. }
  3087. return Variant();
  3088. }
  3089. bool TextEdit::can_drop_data(const Point2 &p_point, const Variant &p_data) const {
  3090. bool drop_override = Control::can_drop_data(p_point, p_data); // In case user wants to drop custom data.
  3091. if (drop_override) {
  3092. return drop_override;
  3093. }
  3094. return is_editable() && p_data.is_string();
  3095. }
  3096. void TextEdit::drop_data(const Point2 &p_point, const Variant &p_data) {
  3097. Control::drop_data(p_point, p_data);
  3098. if (p_point == Vector2(Math::INF, Math::INF)) {
  3099. insert_text_at_caret(p_data);
  3100. } else if (p_data.is_string() && is_editable()) {
  3101. Point2i pos = get_line_column_at_pos(get_local_mouse_pos());
  3102. int drop_at_line = pos.y;
  3103. int drop_at_column = pos.x;
  3104. int selection_index = get_selection_at_line_column(drop_at_line, drop_at_column, !Input::get_singleton()->is_key_pressed(Key::CMD_OR_CTRL));
  3105. // Remove drag caret before the complex operation starts so it won't appear in undo.
  3106. remove_caret(drag_caret_index);
  3107. if (selection_drag_attempt && selection_index >= 0 && selection_index == drag_and_drop_origin_caret_index) {
  3108. // Dropped onto original selection, do nothing.
  3109. selection_drag_attempt = false;
  3110. return;
  3111. }
  3112. begin_complex_operation();
  3113. begin_multicaret_edit();
  3114. if (selection_drag_attempt) {
  3115. // Drop from self.
  3116. selection_drag_attempt = false;
  3117. if (!Input::get_singleton()->is_key_pressed(Key::CMD_OR_CTRL)) {
  3118. // Delete all selections.
  3119. int temp_caret = add_caret(drop_at_line, drop_at_column);
  3120. delete_selection();
  3121. // Use a temporary caret to update the drop at position.
  3122. drop_at_line = get_caret_line(temp_caret);
  3123. drop_at_column = get_caret_column(temp_caret);
  3124. }
  3125. }
  3126. remove_secondary_carets();
  3127. deselect();
  3128. // Insert the dragged text.
  3129. set_caret_line(drop_at_line, true, false, -1);
  3130. set_caret_column(drop_at_column);
  3131. insert_text_at_caret(p_data);
  3132. select(drop_at_line, drop_at_column, get_caret_line(), get_caret_column());
  3133. grab_focus();
  3134. adjust_viewport_to_caret();
  3135. end_multicaret_edit();
  3136. end_complex_operation();
  3137. }
  3138. }
  3139. Control::CursorShape TextEdit::get_cursor_shape(const Point2 &p_pos) const {
  3140. if (dragging_selection) {
  3141. return get_default_cursor_shape();
  3142. }
  3143. if (dragging_minimap) {
  3144. return CURSOR_ARROW;
  3145. }
  3146. Vector2i current_hovered_gutter = _get_hovered_gutter(p_pos);
  3147. if (current_hovered_gutter != Vector2i(-1, -1)) {
  3148. if (gutters[current_hovered_gutter.x].clickable || is_line_gutter_clickable(current_hovered_gutter.y, current_hovered_gutter.x)) {
  3149. return CURSOR_POINTING_HAND;
  3150. } else {
  3151. return CURSOR_ARROW;
  3152. }
  3153. }
  3154. int left_margin = Math::ceil(theme_cache.style_normal->get_margin(SIDE_LEFT));
  3155. if (p_pos.x < left_margin + gutters_width + gutter_padding) {
  3156. return CURSOR_ARROW;
  3157. }
  3158. int xmargin_end = get_size().width - Math::ceil(theme_cache.style_normal->get_margin(SIDE_RIGHT));
  3159. if (draw_minimap && p_pos.x > xmargin_end - minimap_width && p_pos.x <= xmargin_end) {
  3160. return CURSOR_ARROW;
  3161. }
  3162. // Hover inline objects.
  3163. if (inline_object_click_handler.is_valid()) {
  3164. Point2i pos = get_line_column_at_pos(p_pos);
  3165. int xmargin_beg = Math::ceil(theme_cache.style_normal->get_margin(SIDE_LEFT)) + gutters_width + gutter_padding;
  3166. int wrap_i = get_line_wrap_index_at_column(pos.y, pos.x);
  3167. const float wrap_indent = _get_wrap_indent_offset(pos.y, wrap_i, is_layout_rtl());
  3168. Ref<TextParagraph> ldata = text.get_line_data(pos.y);
  3169. for (Variant k : ldata->get_line_objects(wrap_i)) {
  3170. if (!is_inline_info_valid(k)) {
  3171. continue;
  3172. }
  3173. Rect2 obj_rect = ldata->get_line_object_rect(wrap_i, k);
  3174. obj_rect.position.x += xmargin_beg + wrap_indent - first_visible_col;
  3175. if (p_pos.x > obj_rect.position.x && p_pos.x < obj_rect.get_end().x) {
  3176. return CURSOR_POINTING_HAND;
  3177. }
  3178. }
  3179. }
  3180. return get_default_cursor_shape();
  3181. }
  3182. String TextEdit::get_tooltip(const Point2 &p_pos) const {
  3183. if (!tooltip_callback.is_valid()) {
  3184. return Control::get_tooltip(p_pos);
  3185. }
  3186. Point2i pos = get_line_column_at_pos(p_pos);
  3187. int line = pos.y;
  3188. int col = pos.x;
  3189. const String &text_line = text[line];
  3190. if (text_line.is_empty()) {
  3191. return Control::get_tooltip(p_pos);
  3192. }
  3193. const PackedInt32Array words = TS->shaped_text_get_word_breaks(text.get_line_data(line)->get_rid());
  3194. for (int i = 0; i < words.size(); i = i + 2) {
  3195. if (words[i] <= col && words[i + 1] >= col) {
  3196. Variant args[1] = { text_line.substr(words[i], words[i + 1] - words[i]) };
  3197. const Variant *argp[] = { &args[0] };
  3198. Callable::CallError ce;
  3199. Variant ret;
  3200. tooltip_callback.callp(argp, 1, ret, ce);
  3201. ERR_FAIL_COND_V_MSG(ce.error != Callable::CallError::CALL_OK, "", "Failed to call custom tooltip.");
  3202. return ret;
  3203. }
  3204. }
  3205. return Control::get_tooltip(p_pos);
  3206. }
  3207. void TextEdit::set_tooltip_request_func(const Callable &p_tooltip_callback) {
  3208. tooltip_callback = p_tooltip_callback;
  3209. queue_accessibility_update();
  3210. }
  3211. /* Text */
  3212. // Text properties.
  3213. bool TextEdit::has_ime_text() const {
  3214. return !ime_text.is_empty();
  3215. }
  3216. void TextEdit::cancel_ime() {
  3217. if (!has_ime_text()) {
  3218. _close_ime_window();
  3219. return;
  3220. }
  3221. ime_text = String();
  3222. ime_selection = Vector2i();
  3223. alt_start = false;
  3224. alt_start_no_hold = false;
  3225. _close_ime_window();
  3226. _update_ime_text();
  3227. }
  3228. void TextEdit::apply_ime() {
  3229. if (!has_ime_text()) {
  3230. _close_ime_window();
  3231. return;
  3232. }
  3233. // Force apply the current IME text.
  3234. if (alt_start || alt_start_no_hold) {
  3235. cancel_ime();
  3236. if ((alt_code > 0x31 && alt_code < 0xd800) || (alt_code > 0xdfff && alt_code <= 0x10ffff)) {
  3237. handle_unicode_input(alt_code);
  3238. }
  3239. } else {
  3240. String insert_ime_text = ime_text;
  3241. cancel_ime();
  3242. insert_text_at_caret(insert_ime_text);
  3243. }
  3244. }
  3245. void TextEdit::set_editable(bool p_editable) {
  3246. if (editable == p_editable) {
  3247. return;
  3248. }
  3249. editable = p_editable;
  3250. queue_accessibility_update();
  3251. queue_redraw();
  3252. }
  3253. bool TextEdit::is_editable() const {
  3254. return editable;
  3255. }
  3256. void TextEdit::set_text_direction(Control::TextDirection p_text_direction) {
  3257. ERR_FAIL_COND((int)p_text_direction < -1 || (int)p_text_direction > 3);
  3258. if (text_direction != p_text_direction) {
  3259. text_direction = p_text_direction;
  3260. if (text_direction != TEXT_DIRECTION_AUTO && text_direction != TEXT_DIRECTION_INHERITED) {
  3261. input_direction = text_direction;
  3262. }
  3263. TextServer::Direction dir;
  3264. if (text_direction == Control::TEXT_DIRECTION_INHERITED) {
  3265. dir = is_layout_rtl() ? TextServer::DIRECTION_RTL : TextServer::DIRECTION_LTR;
  3266. } else {
  3267. dir = (TextServer::Direction)text_direction;
  3268. }
  3269. text.set_direction_and_language(dir, (!language.is_empty()) ? language : TranslationServer::get_singleton()->get_tool_locale());
  3270. text.invalidate_font();
  3271. _update_placeholder();
  3272. if (menu_dir) {
  3273. menu_dir->set_item_checked(menu_dir->get_item_index(MENU_DIR_INHERITED), text_direction == TEXT_DIRECTION_INHERITED);
  3274. menu_dir->set_item_checked(menu_dir->get_item_index(MENU_DIR_AUTO), text_direction == TEXT_DIRECTION_AUTO);
  3275. menu_dir->set_item_checked(menu_dir->get_item_index(MENU_DIR_LTR), text_direction == TEXT_DIRECTION_LTR);
  3276. menu_dir->set_item_checked(menu_dir->get_item_index(MENU_DIR_RTL), text_direction == TEXT_DIRECTION_RTL);
  3277. }
  3278. queue_accessibility_update();
  3279. queue_redraw();
  3280. }
  3281. }
  3282. Control::TextDirection TextEdit::get_text_direction() const {
  3283. return text_direction;
  3284. }
  3285. void TextEdit::set_language(const String &p_language) {
  3286. if (language != p_language) {
  3287. language = p_language;
  3288. TextServer::Direction dir;
  3289. if (text_direction == Control::TEXT_DIRECTION_INHERITED) {
  3290. dir = is_layout_rtl() ? TextServer::DIRECTION_RTL : TextServer::DIRECTION_LTR;
  3291. } else {
  3292. dir = (TextServer::Direction)text_direction;
  3293. }
  3294. text.set_direction_and_language(dir, (!language.is_empty()) ? language : TranslationServer::get_singleton()->get_tool_locale());
  3295. text.invalidate_all();
  3296. _update_placeholder();
  3297. queue_accessibility_update();
  3298. queue_redraw();
  3299. }
  3300. }
  3301. String TextEdit::get_language() const {
  3302. return language;
  3303. }
  3304. void TextEdit::set_structured_text_bidi_override(TextServer::StructuredTextParser p_parser) {
  3305. if (st_parser != p_parser) {
  3306. st_parser = p_parser;
  3307. for (int i = 0; i < text.size(); i++) {
  3308. text.set(i, text[i], structured_text_parser(st_parser, st_args, text[i]));
  3309. }
  3310. queue_accessibility_update();
  3311. queue_redraw();
  3312. }
  3313. }
  3314. TextServer::StructuredTextParser TextEdit::get_structured_text_bidi_override() const {
  3315. return st_parser;
  3316. }
  3317. void TextEdit::set_structured_text_bidi_override_options(Array p_args) {
  3318. if (st_args == p_args) {
  3319. return;
  3320. }
  3321. st_args = p_args;
  3322. for (int i = 0; i < text.size(); i++) {
  3323. text.set(i, text[i], structured_text_parser(st_parser, st_args, text[i]));
  3324. }
  3325. queue_accessibility_update();
  3326. queue_redraw();
  3327. }
  3328. Array TextEdit::get_structured_text_bidi_override_options() const {
  3329. return st_args;
  3330. }
  3331. void TextEdit::set_tab_size(const int p_size) {
  3332. ERR_FAIL_COND_MSG(p_size <= 0, "Tab size must be greater than 0.");
  3333. if (p_size == text.get_tab_size()) {
  3334. return;
  3335. }
  3336. text.set_tab_size(p_size);
  3337. text.invalidate_all_lines();
  3338. _update_placeholder();
  3339. queue_accessibility_update();
  3340. queue_redraw();
  3341. }
  3342. int TextEdit::get_tab_size() const {
  3343. return text.get_tab_size();
  3344. }
  3345. void TextEdit::set_indent_wrapped_lines(bool p_enabled) {
  3346. if (text.is_indent_wrapped_lines() == p_enabled) {
  3347. return;
  3348. }
  3349. text.set_indent_wrapped_lines(p_enabled);
  3350. text.invalidate_all_lines();
  3351. _update_placeholder();
  3352. queue_redraw();
  3353. }
  3354. bool TextEdit::is_indent_wrapped_lines() const {
  3355. return text.is_indent_wrapped_lines();
  3356. }
  3357. void TextEdit::set_tab_input_mode(bool p_enabled) {
  3358. tab_input_mode = p_enabled;
  3359. }
  3360. bool TextEdit::get_tab_input_mode() const {
  3361. return tab_input_mode;
  3362. }
  3363. // User controls
  3364. void TextEdit::set_overtype_mode_enabled(bool p_enabled) {
  3365. if (overtype_mode == p_enabled) {
  3366. return;
  3367. }
  3368. overtype_mode = p_enabled;
  3369. queue_redraw();
  3370. }
  3371. bool TextEdit::is_overtype_mode_enabled() const {
  3372. return overtype_mode;
  3373. }
  3374. void TextEdit::set_context_menu_enabled(bool p_enabled) {
  3375. context_menu_enabled = p_enabled;
  3376. }
  3377. bool TextEdit::is_context_menu_enabled() const {
  3378. return context_menu_enabled;
  3379. }
  3380. void TextEdit::show_emoji_and_symbol_picker() {
  3381. _update_ime_window_position();
  3382. DisplayServer::get_singleton()->show_emoji_and_symbol_picker();
  3383. }
  3384. void TextEdit::set_emoji_menu_enabled(bool p_enabled) {
  3385. if (emoji_menu_enabled != p_enabled) {
  3386. emoji_menu_enabled = p_enabled;
  3387. _update_context_menu();
  3388. }
  3389. }
  3390. bool TextEdit::is_emoji_menu_enabled() const {
  3391. return emoji_menu_enabled;
  3392. }
  3393. void TextEdit::set_backspace_deletes_composite_character_enabled(bool p_enabled) {
  3394. backspace_deletes_composite_character_enabled = p_enabled;
  3395. }
  3396. bool TextEdit::is_backspace_deletes_composite_character_enabled() const {
  3397. return backspace_deletes_composite_character_enabled;
  3398. }
  3399. void TextEdit::set_shortcut_keys_enabled(bool p_enabled) {
  3400. shortcut_keys_enabled = p_enabled;
  3401. }
  3402. bool TextEdit::is_shortcut_keys_enabled() const {
  3403. return shortcut_keys_enabled;
  3404. }
  3405. void TextEdit::set_virtual_keyboard_enabled(bool p_enabled) {
  3406. virtual_keyboard_enabled = p_enabled;
  3407. }
  3408. bool TextEdit::is_virtual_keyboard_enabled() const {
  3409. return virtual_keyboard_enabled;
  3410. }
  3411. void TextEdit::set_virtual_keyboard_show_on_focus(bool p_show_on_focus) {
  3412. virtual_keyboard_show_on_focus = p_show_on_focus;
  3413. }
  3414. bool TextEdit::get_virtual_keyboard_show_on_focus() const {
  3415. return virtual_keyboard_show_on_focus;
  3416. }
  3417. void TextEdit::set_middle_mouse_paste_enabled(bool p_enabled) {
  3418. middle_mouse_paste_enabled = p_enabled;
  3419. }
  3420. bool TextEdit::is_middle_mouse_paste_enabled() const {
  3421. return middle_mouse_paste_enabled;
  3422. }
  3423. void TextEdit::set_empty_selection_clipboard_enabled(bool p_enabled) {
  3424. empty_selection_clipboard_enabled = p_enabled;
  3425. }
  3426. bool TextEdit::is_empty_selection_clipboard_enabled() const {
  3427. return empty_selection_clipboard_enabled;
  3428. }
  3429. // Text manipulation
  3430. void TextEdit::clear() {
  3431. setting_text = true;
  3432. _clear();
  3433. setting_text = false;
  3434. emit_signal(SNAME("text_set"));
  3435. }
  3436. void TextEdit::_clear() {
  3437. if (editable && undo_enabled) {
  3438. remove_secondary_carets();
  3439. _move_caret_document_start(false);
  3440. begin_complex_operation();
  3441. _remove_text(0, 0, MAX(0, get_line_count() - 1), MAX(get_line(MAX(get_line_count() - 1, 0)).size() - 1, 0));
  3442. insert_text_at_caret("");
  3443. text.clear();
  3444. end_complex_operation();
  3445. return;
  3446. }
  3447. // Cannot merge with above, as we are not part of the tree on creation.
  3448. int old_text_size = text.size();
  3449. clear_undo_history();
  3450. text.clear();
  3451. remove_secondary_carets();
  3452. set_caret_line(0, false, true, -1);
  3453. set_caret_column(0);
  3454. first_visible_col = 0;
  3455. first_visible_line = 0;
  3456. first_visible_line_wrap_ofs = 0;
  3457. carets.write[0].last_fit_x = 0;
  3458. deselect();
  3459. emit_signal(SNAME("lines_edited_from"), old_text_size, 0);
  3460. }
  3461. void TextEdit::set_text(const String &p_text) {
  3462. setting_text = true;
  3463. if (!undo_enabled) {
  3464. _clear();
  3465. insert_text_at_caret(p_text);
  3466. }
  3467. if (undo_enabled) {
  3468. remove_secondary_carets();
  3469. set_caret_line(0);
  3470. set_caret_column(0);
  3471. begin_complex_operation();
  3472. deselect();
  3473. _remove_text(0, 0, MAX(0, get_line_count() - 1), MAX(get_line(MAX(get_line_count() - 1, 0)).size() - 1, 0));
  3474. insert_text_at_caret(p_text);
  3475. end_complex_operation();
  3476. }
  3477. set_caret_line(0);
  3478. set_caret_column(0);
  3479. queue_accessibility_update();
  3480. queue_redraw();
  3481. setting_text = false;
  3482. emit_signal(SNAME("text_set"));
  3483. }
  3484. String TextEdit::get_text() const {
  3485. StringBuilder ret_text;
  3486. const int text_size = text.size();
  3487. for (int i = 0; i < text_size; i++) {
  3488. ret_text += text[i];
  3489. if (i != text_size - 1) {
  3490. ret_text += "\n";
  3491. }
  3492. }
  3493. return ret_text.as_string();
  3494. }
  3495. int TextEdit::get_line_count() const {
  3496. return text.size();
  3497. }
  3498. void TextEdit::set_placeholder(const String &p_text) {
  3499. if (placeholder_text == p_text) {
  3500. return;
  3501. }
  3502. placeholder_text = p_text;
  3503. _update_placeholder();
  3504. queue_accessibility_update();
  3505. queue_redraw();
  3506. }
  3507. String TextEdit::get_placeholder() const {
  3508. return placeholder_text;
  3509. }
  3510. void TextEdit::set_line(int p_line, const String &p_new_text) {
  3511. if (p_line < 0 || p_line >= text.size()) {
  3512. return;
  3513. }
  3514. begin_complex_operation();
  3515. int old_column = text[p_line].length();
  3516. // Set the affected carets column to update their last offset x.
  3517. for (int i = 0; i < get_caret_count(); i++) {
  3518. if (_is_line_col_in_range(get_caret_line(i), get_caret_column(i), p_line, 0, p_line, old_column)) {
  3519. set_caret_column(get_caret_column(i), false, i);
  3520. }
  3521. if (has_selection(i) && _is_line_col_in_range(get_selection_origin_line(i), get_selection_origin_column(i), p_line, 0, p_line, old_column)) {
  3522. set_selection_origin_column(get_selection_origin_column(i), i);
  3523. }
  3524. }
  3525. _remove_text(p_line, 0, p_line, old_column);
  3526. int new_line, new_column;
  3527. _insert_text(p_line, 0, p_new_text, &new_line, &new_column);
  3528. // Don't offset carets that were on the old line.
  3529. _offset_carets_after(p_line, old_column, new_line, new_column, false, false);
  3530. // Set the caret lines to update the column to match visually.
  3531. for (int i = 0; i < get_caret_count(); i++) {
  3532. if (_is_line_col_in_range(get_caret_line(i), get_caret_column(i), p_line, 0, p_line, old_column)) {
  3533. set_caret_line(get_caret_line(i), false, true, 0, i);
  3534. }
  3535. if (has_selection(i) && _is_line_col_in_range(get_selection_origin_line(i), get_selection_origin_column(i), p_line, 0, p_line, old_column)) {
  3536. set_selection_origin_line(get_selection_origin_line(i), true, 0, i);
  3537. }
  3538. }
  3539. merge_overlapping_carets();
  3540. end_complex_operation();
  3541. }
  3542. String TextEdit::get_line(int p_line) const {
  3543. if (p_line < 0 || p_line >= text.size()) {
  3544. return String();
  3545. }
  3546. return text[p_line];
  3547. }
  3548. String TextEdit::get_line_with_ime(int p_line) const {
  3549. if (p_line < 0 || p_line >= text.size()) {
  3550. return String();
  3551. }
  3552. return text.get_text_with_ime(p_line);
  3553. }
  3554. int TextEdit::get_line_width(int p_line, int p_wrap_index) const {
  3555. ERR_FAIL_INDEX_V(p_line, text.size(), 0);
  3556. ERR_FAIL_COND_V(p_wrap_index > get_line_wrap_count(p_line), 0);
  3557. return text.get_line_width(p_line, p_wrap_index);
  3558. }
  3559. int TextEdit::get_line_height() const {
  3560. return MAX(text.get_line_height() + theme_cache.line_spacing, 1);
  3561. }
  3562. int TextEdit::_get_wrapped_indent_level(int p_line, int &r_first_wrap) const {
  3563. ERR_FAIL_INDEX_V(p_line, text.size(), 0);
  3564. const Vector<Vector2i> wr = text.get_line_wrap_ranges(p_line);
  3565. r_first_wrap = 0;
  3566. int tab_count = 0;
  3567. int whitespace_count = 0;
  3568. int line_length = text[p_line].size();
  3569. for (int i = 0; i < line_length - 1; i++) {
  3570. if (r_first_wrap < wr.size() && i >= wr[r_first_wrap].y) {
  3571. tab_count = 0;
  3572. whitespace_count = 0;
  3573. r_first_wrap++;
  3574. }
  3575. if (text[p_line][i] == '\t') {
  3576. tab_count++;
  3577. } else if (text[p_line][i] == ' ') {
  3578. whitespace_count++;
  3579. } else {
  3580. break;
  3581. }
  3582. }
  3583. return tab_count * text.get_tab_size() + whitespace_count;
  3584. }
  3585. float TextEdit::_get_wrap_indent_offset(int p_line, int p_wrap_index, bool p_rtl) const {
  3586. if (!text.is_indent_wrapped_lines()) {
  3587. return 0;
  3588. }
  3589. int first_indent_line = 0;
  3590. _get_wrapped_indent_level(p_line, first_indent_line);
  3591. if (p_wrap_index > first_indent_line) {
  3592. return MIN(text.get_indent_offset(p_line, p_rtl), wrap_at_column * 0.6);
  3593. }
  3594. return 0;
  3595. }
  3596. int TextEdit::get_indent_level(int p_line) const {
  3597. ERR_FAIL_INDEX_V(p_line, text.size(), 0);
  3598. int tab_count = 0;
  3599. int whitespace_count = 0;
  3600. int line_length = text[p_line].size();
  3601. for (int i = 0; i < line_length - 1; i++) {
  3602. if (text[p_line][i] == '\t') {
  3603. tab_count++;
  3604. } else if (text[p_line][i] == ' ') {
  3605. whitespace_count++;
  3606. } else {
  3607. break;
  3608. }
  3609. }
  3610. return tab_count * text.get_tab_size() + whitespace_count;
  3611. }
  3612. int TextEdit::get_first_non_whitespace_column(int p_line) const {
  3613. ERR_FAIL_INDEX_V(p_line, text.size(), 0);
  3614. int col = 0;
  3615. while (col < text[p_line].length() && is_whitespace(text[p_line][col])) {
  3616. col++;
  3617. }
  3618. return col;
  3619. }
  3620. void TextEdit::swap_lines(int p_from_line, int p_to_line) {
  3621. ERR_FAIL_INDEX(p_from_line, text.size());
  3622. ERR_FAIL_INDEX(p_to_line, text.size());
  3623. if (p_from_line == p_to_line) {
  3624. return;
  3625. }
  3626. String from_line_text = get_line(p_from_line);
  3627. String to_line_text = get_line(p_to_line);
  3628. begin_complex_operation();
  3629. begin_multicaret_edit();
  3630. // Don't use set_line to avoid clamping and updating carets.
  3631. _remove_text(p_to_line, 0, p_to_line, text[p_to_line].length());
  3632. _insert_text(p_to_line, 0, from_line_text);
  3633. _remove_text(p_from_line, 0, p_from_line, text[p_from_line].length());
  3634. _insert_text(p_from_line, 0, to_line_text);
  3635. // Swap carets.
  3636. for (int i = 0; i < get_caret_count(); i++) {
  3637. bool selected = has_selection(i);
  3638. if (get_caret_line(i) == p_from_line || get_caret_line(i) == p_to_line) {
  3639. int caret_new_line = get_caret_line(i) == p_from_line ? p_to_line : p_from_line;
  3640. int caret_column = get_caret_column(i);
  3641. set_caret_line(caret_new_line, false, true, -1, i);
  3642. set_caret_column(caret_column, false, i);
  3643. }
  3644. if (selected && (get_selection_origin_line(i) == p_from_line || get_selection_origin_line(i) == p_to_line)) {
  3645. int origin_new_line = get_selection_origin_line(i) == p_from_line ? p_to_line : p_from_line;
  3646. int origin_column = get_selection_origin_column(i);
  3647. select(origin_new_line, origin_column, get_caret_line(i), get_caret_column(i), i);
  3648. }
  3649. }
  3650. // If only part of a selection was changed, it may now overlap.
  3651. merge_overlapping_carets();
  3652. end_multicaret_edit();
  3653. end_complex_operation();
  3654. }
  3655. void TextEdit::insert_line_at(int p_line, const String &p_text) {
  3656. ERR_FAIL_INDEX(p_line, text.size());
  3657. // Use a complex operation so subsequent calls aren't merged together.
  3658. begin_complex_operation();
  3659. int new_line, new_column;
  3660. _insert_text(p_line, 0, p_text + "\n", &new_line, &new_column);
  3661. _offset_carets_after(p_line, 0, new_line, new_column);
  3662. end_complex_operation();
  3663. }
  3664. void TextEdit::remove_line_at(int p_line, bool p_move_carets_down) {
  3665. ERR_FAIL_INDEX(p_line, text.size());
  3666. if (get_line_count() == 1) {
  3667. // Only one line, just remove contents.
  3668. begin_complex_operation();
  3669. int line_length = get_line(p_line).length();
  3670. _remove_text(p_line, 0, p_line, line_length);
  3671. collapse_carets(p_line, 0, p_line, line_length, true);
  3672. end_complex_operation();
  3673. return;
  3674. }
  3675. begin_complex_operation();
  3676. bool is_last_line = p_line == get_line_count() - 1;
  3677. int from_line = is_last_line ? p_line - 1 : p_line;
  3678. int next_line = is_last_line ? p_line : p_line + 1;
  3679. int from_column = is_last_line ? get_line(from_line).length() : 0;
  3680. int next_column = is_last_line ? get_line(next_line).length() : 0;
  3681. if ((!is_last_line && p_move_carets_down) || (p_line != 0 && !p_move_carets_down)) {
  3682. // Set the carets column to update their last offset x.
  3683. for (int i = 0; i < get_caret_count(); i++) {
  3684. if (get_caret_line(i) == p_line) {
  3685. set_caret_column(get_caret_column(i), false, i);
  3686. }
  3687. if (has_selection(i) && get_selection_origin_line(i) == p_line) {
  3688. set_selection_origin_column(get_selection_origin_column(i), i);
  3689. }
  3690. }
  3691. }
  3692. // Remove line.
  3693. _remove_text(from_line, from_column, next_line, next_column);
  3694. begin_multicaret_edit();
  3695. if ((is_last_line && p_move_carets_down) || (p_line == 0 && !p_move_carets_down)) {
  3696. // Collapse carets.
  3697. collapse_carets(from_line, from_column, next_line, next_column, true);
  3698. } else {
  3699. // Move carets to visually line up.
  3700. int target_line = p_move_carets_down ? p_line : p_line - 1;
  3701. for (int i = 0; i < get_caret_count(); i++) {
  3702. bool selected = has_selection(i);
  3703. if (get_caret_line(i) == p_line) {
  3704. set_caret_line(target_line, i == 0, true, 0, i);
  3705. }
  3706. if (selected && get_selection_origin_line(i) == p_line) {
  3707. set_selection_origin_line(target_line, true, 0, i);
  3708. select(get_selection_origin_line(i), get_selection_origin_column(i), get_caret_line(i), get_caret_column(i), i);
  3709. }
  3710. }
  3711. merge_overlapping_carets();
  3712. }
  3713. _offset_carets_after(next_line, next_column, from_line, from_column);
  3714. end_multicaret_edit();
  3715. end_complex_operation();
  3716. queue_accessibility_update();
  3717. queue_redraw();
  3718. }
  3719. void TextEdit::insert_text_at_caret(const String &p_text, int p_caret) {
  3720. ERR_FAIL_COND(p_caret >= get_caret_count() || p_caret < -1);
  3721. begin_complex_operation();
  3722. begin_multicaret_edit();
  3723. for (int i = 0; i < get_caret_count(); i++) {
  3724. if (p_caret != -1 && p_caret != i) {
  3725. continue;
  3726. }
  3727. if (p_caret == -1 && multicaret_edit_ignore_caret(i)) {
  3728. continue;
  3729. }
  3730. delete_selection(i);
  3731. int from_line = get_caret_line(i);
  3732. int from_col = get_caret_column(i);
  3733. int new_line, new_column;
  3734. _insert_text(from_line, from_col, p_text, &new_line, &new_column);
  3735. _update_scrollbars();
  3736. _offset_carets_after(from_line, from_col, new_line, new_column);
  3737. set_caret_line(new_line, false, true, -1, i);
  3738. set_caret_column(new_column, i == 0, i);
  3739. }
  3740. if (has_ime_text()) {
  3741. _update_ime_text();
  3742. }
  3743. end_multicaret_edit();
  3744. end_complex_operation();
  3745. }
  3746. void TextEdit::insert_text(const String &p_text, int p_line, int p_column, bool p_before_selection_begin, bool p_before_selection_end) {
  3747. ERR_FAIL_INDEX(p_line, text.size());
  3748. ERR_FAIL_INDEX(p_column, text[p_line].length() + 1);
  3749. begin_complex_operation();
  3750. int new_line, new_column;
  3751. _insert_text(p_line, p_column, p_text, &new_line, &new_column);
  3752. _offset_carets_after(p_line, p_column, new_line, new_column, p_before_selection_begin, p_before_selection_end);
  3753. end_complex_operation();
  3754. }
  3755. void TextEdit::remove_text(int p_from_line, int p_from_column, int p_to_line, int p_to_column) {
  3756. ERR_FAIL_INDEX(p_from_line, text.size());
  3757. ERR_FAIL_INDEX(p_from_column, text[p_from_line].length() + 1);
  3758. ERR_FAIL_INDEX(p_to_line, text.size());
  3759. ERR_FAIL_INDEX(p_to_column, text[p_to_line].length() + 1);
  3760. ERR_FAIL_COND(p_to_line < p_from_line);
  3761. ERR_FAIL_COND(p_to_line == p_from_line && p_to_column < p_from_column);
  3762. begin_complex_operation();
  3763. _remove_text(p_from_line, p_from_column, p_to_line, p_to_column);
  3764. collapse_carets(p_from_line, p_from_column, p_to_line, p_to_column);
  3765. _offset_carets_after(p_to_line, p_to_column, p_from_line, p_from_column);
  3766. end_complex_operation();
  3767. }
  3768. int TextEdit::get_last_unhidden_line() const {
  3769. // Returns the last line in the text that is not hidden.
  3770. if (!_is_hiding_enabled()) {
  3771. return text.size() - 1;
  3772. }
  3773. int last_line;
  3774. for (last_line = text.size() - 1; last_line > 0; last_line--) {
  3775. if (!_is_line_hidden(last_line)) {
  3776. break;
  3777. }
  3778. }
  3779. return last_line;
  3780. }
  3781. int TextEdit::get_next_visible_line_offset_from(int p_line_from, int p_visible_amount) const {
  3782. // Returns the number of lines (hidden and unhidden) from p_line_from to (p_line_from + visible_amount of unhidden lines).
  3783. ERR_FAIL_INDEX_V(p_line_from, text.size(), Math::abs(p_visible_amount));
  3784. if (!_is_hiding_enabled()) {
  3785. return Math::abs(p_visible_amount);
  3786. }
  3787. int num_visible = 0;
  3788. int num_total = 0;
  3789. if (p_visible_amount >= 0) {
  3790. for (int i = p_line_from; i < text.size(); i++) {
  3791. num_total++;
  3792. if (!_is_line_hidden(i)) {
  3793. num_visible++;
  3794. }
  3795. if (num_visible >= p_visible_amount) {
  3796. break;
  3797. }
  3798. }
  3799. } else {
  3800. p_visible_amount = Math::abs(p_visible_amount);
  3801. for (int i = p_line_from; i >= 0; i--) {
  3802. num_total++;
  3803. if (!_is_line_hidden(i)) {
  3804. num_visible++;
  3805. }
  3806. if (num_visible >= p_visible_amount) {
  3807. break;
  3808. }
  3809. }
  3810. }
  3811. return num_total;
  3812. }
  3813. Point2i TextEdit::get_next_visible_line_index_offset_from(int p_line_from, int p_wrap_index_from, int p_visible_amount) const {
  3814. // 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).
  3815. // Wrap index is set to the wrap index of the last line.
  3816. int wrap_index = 0;
  3817. ERR_FAIL_INDEX_V(p_line_from, text.size(), Point2i(Math::abs(p_visible_amount), 0));
  3818. if (!_is_hiding_enabled() && get_line_wrapping_mode() == LineWrappingMode::LINE_WRAPPING_NONE) {
  3819. return Point2i(Math::abs(p_visible_amount), 0);
  3820. }
  3821. int num_visible = 0;
  3822. int num_total = 0;
  3823. if (p_visible_amount == 0) {
  3824. num_total = 0;
  3825. wrap_index = 0;
  3826. } else if (p_visible_amount > 0) {
  3827. int i;
  3828. num_visible -= p_wrap_index_from;
  3829. for (i = p_line_from; i < text.size(); i++) {
  3830. num_total++;
  3831. if (!_is_line_hidden(i)) {
  3832. num_visible++;
  3833. num_visible += get_line_wrap_count(i);
  3834. }
  3835. if (num_visible >= p_visible_amount) {
  3836. break;
  3837. }
  3838. }
  3839. wrap_index = get_line_wrap_count(MIN(i, text.size() - 1)) - MAX(0, num_visible - p_visible_amount);
  3840. // If we are a hidden line, then we are the last line as we cannot reach "p_visible_amount".
  3841. // This means we need to backtrack to get last visible line.
  3842. // Currently, line 0 cannot be hidden so this should always be valid.
  3843. int line = (p_line_from + num_total) - 1;
  3844. if (_is_line_hidden(line)) {
  3845. Point2i backtrack = get_next_visible_line_index_offset_from(line, 0, -1);
  3846. num_total = num_total - (backtrack.x - 1);
  3847. wrap_index = backtrack.y;
  3848. }
  3849. } else {
  3850. p_visible_amount = Math::abs(p_visible_amount);
  3851. int i;
  3852. num_visible -= get_line_wrap_count(p_line_from) - p_wrap_index_from;
  3853. for (i = p_line_from; i >= 0; i--) {
  3854. num_total++;
  3855. if (!_is_line_hidden(i)) {
  3856. num_visible++;
  3857. num_visible += get_line_wrap_count(i);
  3858. }
  3859. if (num_visible >= p_visible_amount) {
  3860. break;
  3861. }
  3862. }
  3863. wrap_index = MAX(0, num_visible - p_visible_amount);
  3864. }
  3865. wrap_index = MAX(wrap_index, 0);
  3866. return Point2i(num_total, wrap_index);
  3867. }
  3868. void TextEdit::set_inline_object_handlers(const Callable &p_parser, const Callable &p_drawer, const Callable &p_click_handler) {
  3869. inline_object_drawer = p_drawer;
  3870. inline_object_click_handler = p_click_handler;
  3871. text.set_inline_object_parser(p_parser);
  3872. }
  3873. // Overridable actions
  3874. void TextEdit::handle_unicode_input(const uint32_t p_unicode, int p_caret) {
  3875. if (GDVIRTUAL_CALL(_handle_unicode_input, p_unicode, p_caret)) {
  3876. return;
  3877. }
  3878. _handle_unicode_input_internal(p_unicode, p_caret);
  3879. }
  3880. void TextEdit::backspace(int p_caret) {
  3881. if (GDVIRTUAL_CALL(_backspace, p_caret)) {
  3882. return;
  3883. }
  3884. _backspace_internal(p_caret);
  3885. }
  3886. void TextEdit::cut(int p_caret) {
  3887. if (GDVIRTUAL_CALL(_cut, p_caret)) {
  3888. return;
  3889. }
  3890. _cut_internal(p_caret);
  3891. }
  3892. void TextEdit::copy(int p_caret) {
  3893. if (GDVIRTUAL_CALL(_copy, p_caret)) {
  3894. return;
  3895. }
  3896. _copy_internal(p_caret);
  3897. }
  3898. void TextEdit::paste(int p_caret) {
  3899. if (GDVIRTUAL_CALL(_paste, p_caret)) {
  3900. return;
  3901. }
  3902. _paste_internal(p_caret);
  3903. }
  3904. void TextEdit::paste_primary_clipboard(int p_caret) {
  3905. if (GDVIRTUAL_CALL(_paste_primary_clipboard, p_caret)) {
  3906. return;
  3907. }
  3908. _paste_primary_clipboard_internal(p_caret);
  3909. }
  3910. // Context menu.
  3911. PopupMenu *TextEdit::get_menu() const {
  3912. if (!menu) {
  3913. const_cast<TextEdit *>(this)->_generate_context_menu();
  3914. }
  3915. return menu;
  3916. }
  3917. bool TextEdit::is_menu_visible() const {
  3918. return menu && menu->is_visible();
  3919. }
  3920. void TextEdit::menu_option(int p_option) {
  3921. switch (p_option) {
  3922. case MENU_CUT: {
  3923. cut();
  3924. } break;
  3925. case MENU_COPY: {
  3926. copy();
  3927. } break;
  3928. case MENU_PASTE: {
  3929. paste();
  3930. } break;
  3931. case MENU_CLEAR: {
  3932. if (editable) {
  3933. clear();
  3934. }
  3935. } break;
  3936. case MENU_SELECT_ALL: {
  3937. select_all();
  3938. } break;
  3939. case MENU_UNDO: {
  3940. undo();
  3941. } break;
  3942. case MENU_REDO: {
  3943. redo();
  3944. } break;
  3945. case MENU_DIR_INHERITED: {
  3946. set_text_direction(TEXT_DIRECTION_INHERITED);
  3947. } break;
  3948. case MENU_DIR_AUTO: {
  3949. set_text_direction(TEXT_DIRECTION_AUTO);
  3950. } break;
  3951. case MENU_DIR_LTR: {
  3952. set_text_direction(TEXT_DIRECTION_LTR);
  3953. } break;
  3954. case MENU_DIR_RTL: {
  3955. set_text_direction(TEXT_DIRECTION_RTL);
  3956. } break;
  3957. case MENU_DISPLAY_UCC: {
  3958. set_draw_control_chars(!get_draw_control_chars());
  3959. } break;
  3960. case MENU_INSERT_LRM: {
  3961. if (editable) {
  3962. insert_text_at_caret(String::chr(0x200E));
  3963. }
  3964. } break;
  3965. case MENU_INSERT_RLM: {
  3966. if (editable) {
  3967. insert_text_at_caret(String::chr(0x200F));
  3968. }
  3969. } break;
  3970. case MENU_INSERT_LRE: {
  3971. if (editable) {
  3972. insert_text_at_caret(String::chr(0x202A));
  3973. }
  3974. } break;
  3975. case MENU_INSERT_RLE: {
  3976. if (editable) {
  3977. insert_text_at_caret(String::chr(0x202B));
  3978. }
  3979. } break;
  3980. case MENU_INSERT_LRO: {
  3981. if (editable) {
  3982. insert_text_at_caret(String::chr(0x202D));
  3983. }
  3984. } break;
  3985. case MENU_INSERT_RLO: {
  3986. if (editable) {
  3987. insert_text_at_caret(String::chr(0x202E));
  3988. }
  3989. } break;
  3990. case MENU_INSERT_PDF: {
  3991. if (editable) {
  3992. insert_text_at_caret(String::chr(0x202C));
  3993. }
  3994. } break;
  3995. case MENU_INSERT_ALM: {
  3996. if (editable) {
  3997. insert_text_at_caret(String::chr(0x061C));
  3998. }
  3999. } break;
  4000. case MENU_INSERT_LRI: {
  4001. if (editable) {
  4002. insert_text_at_caret(String::chr(0x2066));
  4003. }
  4004. } break;
  4005. case MENU_INSERT_RLI: {
  4006. if (editable) {
  4007. insert_text_at_caret(String::chr(0x2067));
  4008. }
  4009. } break;
  4010. case MENU_INSERT_FSI: {
  4011. if (editable) {
  4012. insert_text_at_caret(String::chr(0x2068));
  4013. }
  4014. } break;
  4015. case MENU_INSERT_PDI: {
  4016. if (editable) {
  4017. insert_text_at_caret(String::chr(0x2069));
  4018. }
  4019. } break;
  4020. case MENU_INSERT_ZWJ: {
  4021. if (editable) {
  4022. insert_text_at_caret(String::chr(0x200D));
  4023. }
  4024. } break;
  4025. case MENU_INSERT_ZWNJ: {
  4026. if (editable) {
  4027. insert_text_at_caret(String::chr(0x200C));
  4028. }
  4029. } break;
  4030. case MENU_INSERT_WJ: {
  4031. if (editable) {
  4032. insert_text_at_caret(String::chr(0x2060));
  4033. }
  4034. } break;
  4035. case MENU_INSERT_SHY: {
  4036. if (editable) {
  4037. insert_text_at_caret(String::chr(0x00AD));
  4038. }
  4039. } break;
  4040. case MENU_EMOJI_AND_SYMBOL: {
  4041. show_emoji_and_symbol_picker();
  4042. } break;
  4043. }
  4044. }
  4045. /* Versioning */
  4046. void TextEdit::start_action(EditAction p_action) {
  4047. if (current_action != p_action) {
  4048. if (current_action != EditAction::ACTION_NONE) {
  4049. in_action = false;
  4050. pending_action_end = false;
  4051. end_complex_operation();
  4052. }
  4053. if (p_action != EditAction::ACTION_NONE) {
  4054. in_action = true;
  4055. begin_complex_operation();
  4056. }
  4057. } else if (current_action != EditAction::ACTION_NONE) {
  4058. pending_action_end = false;
  4059. }
  4060. current_action = p_action;
  4061. }
  4062. void TextEdit::end_action() {
  4063. if (current_action != EditAction::ACTION_NONE) {
  4064. pending_action_end = true;
  4065. queue_accessibility_update();
  4066. }
  4067. }
  4068. TextEdit::EditAction TextEdit::get_current_action() const {
  4069. return current_action;
  4070. }
  4071. void TextEdit::begin_complex_operation() {
  4072. _push_current_op();
  4073. if (complex_operation_count == 0) {
  4074. next_operation_is_complex = true;
  4075. current_op.start_carets = carets;
  4076. }
  4077. complex_operation_count++;
  4078. }
  4079. void TextEdit::end_complex_operation() {
  4080. _push_current_op();
  4081. queue_accessibility_update();
  4082. complex_operation_count = MAX(complex_operation_count - 1, 0);
  4083. if (complex_operation_count > 0) {
  4084. return;
  4085. }
  4086. if (undo_stack.is_empty()) {
  4087. return;
  4088. }
  4089. undo_stack.back()->get().end_carets = carets;
  4090. if (undo_stack.back()->get().chain_forward) {
  4091. undo_stack.back()->get().chain_forward = false;
  4092. return;
  4093. }
  4094. undo_stack.back()->get().chain_backward = true;
  4095. }
  4096. bool TextEdit::has_undo() const {
  4097. if (undo_stack_pos == nullptr) {
  4098. int pending = current_op.type == TextOperation::TYPE_NONE ? 0 : 1;
  4099. return undo_stack.size() + pending > 0;
  4100. }
  4101. return undo_stack_pos != undo_stack.front();
  4102. }
  4103. bool TextEdit::has_redo() const {
  4104. return undo_stack_pos != nullptr;
  4105. }
  4106. void TextEdit::undo() {
  4107. if (!editable) {
  4108. return;
  4109. }
  4110. if (in_action) {
  4111. pending_action_end = true;
  4112. }
  4113. _push_current_op();
  4114. if (undo_stack_pos == nullptr) {
  4115. if (undo_stack.is_empty()) {
  4116. return; // Nothing to undo.
  4117. }
  4118. undo_stack_pos = undo_stack.back();
  4119. } else if (undo_stack_pos == undo_stack.front()) {
  4120. return; // At the bottom of the undo stack.
  4121. } else {
  4122. undo_stack_pos = undo_stack_pos->prev();
  4123. }
  4124. deselect();
  4125. TextOperation op = undo_stack_pos->get();
  4126. _do_text_op(op, true);
  4127. current_op.version = op.prev_version;
  4128. if (undo_stack_pos->get().chain_backward) {
  4129. // This was part of a complex operation, undo until the chain forward at the start of the complex operation.
  4130. while (true) {
  4131. ERR_BREAK(!undo_stack_pos->prev());
  4132. undo_stack_pos = undo_stack_pos->prev();
  4133. op = undo_stack_pos->get();
  4134. _do_text_op(op, true);
  4135. current_op.version = op.prev_version;
  4136. if (undo_stack_pos->get().chain_forward) {
  4137. break;
  4138. }
  4139. }
  4140. }
  4141. _update_scrollbars();
  4142. bool dirty_carets = get_caret_count() != undo_stack_pos->get().start_carets.size();
  4143. if (!dirty_carets) {
  4144. for (int i = 0; i < get_caret_count(); i++) {
  4145. if (carets[i].line != undo_stack_pos->get().start_carets[i].line || carets[i].column != undo_stack_pos->get().start_carets[i].column) {
  4146. dirty_carets = true;
  4147. break;
  4148. }
  4149. }
  4150. }
  4151. carets = undo_stack_pos->get().start_carets;
  4152. _unhide_carets();
  4153. if (dirty_carets) {
  4154. _caret_changed();
  4155. _selection_changed();
  4156. }
  4157. adjust_viewport_to_caret();
  4158. queue_accessibility_update();
  4159. }
  4160. void TextEdit::redo() {
  4161. if (!editable) {
  4162. return;
  4163. }
  4164. if (in_action) {
  4165. pending_action_end = true;
  4166. }
  4167. _push_current_op();
  4168. if (!has_redo()) {
  4169. return; // Nothing to do.
  4170. }
  4171. deselect();
  4172. TextOperation op = undo_stack_pos->get();
  4173. _do_text_op(op, false);
  4174. current_op.version = op.version;
  4175. if (undo_stack_pos->get().chain_forward) {
  4176. // This was part of a complex operation, redo until the chain backward at the end of the complex operation.
  4177. while (true) {
  4178. ERR_BREAK(!undo_stack_pos->next());
  4179. undo_stack_pos = undo_stack_pos->next();
  4180. op = undo_stack_pos->get();
  4181. _do_text_op(op, false);
  4182. current_op.version = op.version;
  4183. if (undo_stack_pos->get().chain_backward) {
  4184. break;
  4185. }
  4186. }
  4187. }
  4188. _update_scrollbars();
  4189. bool dirty_carets = get_caret_count() != undo_stack_pos->get().end_carets.size();
  4190. if (!dirty_carets) {
  4191. for (int i = 0; i < get_caret_count(); i++) {
  4192. if (carets[i].line != undo_stack_pos->get().end_carets[i].line || carets[i].column != undo_stack_pos->get().end_carets[i].column) {
  4193. dirty_carets = true;
  4194. break;
  4195. }
  4196. }
  4197. }
  4198. carets = undo_stack_pos->get().end_carets;
  4199. undo_stack_pos = undo_stack_pos->next();
  4200. _unhide_carets();
  4201. if (dirty_carets) {
  4202. _caret_changed();
  4203. _selection_changed();
  4204. }
  4205. adjust_viewport_to_caret();
  4206. queue_accessibility_update();
  4207. }
  4208. void TextEdit::clear_undo_history() {
  4209. saved_version = 0;
  4210. current_op.type = TextOperation::TYPE_NONE;
  4211. undo_stack_pos = nullptr;
  4212. undo_stack.clear();
  4213. }
  4214. bool TextEdit::is_insert_text_operation() const {
  4215. return (current_op.type == TextOperation::TYPE_INSERT || current_action == EditAction::ACTION_TYPING);
  4216. }
  4217. void TextEdit::tag_saved_version() {
  4218. saved_version = get_version();
  4219. }
  4220. uint32_t TextEdit::get_version() const {
  4221. return current_op.version;
  4222. }
  4223. uint32_t TextEdit::get_saved_version() const {
  4224. return saved_version;
  4225. }
  4226. /* Search */
  4227. void TextEdit::set_search_text(const String &p_search_text) {
  4228. search_text = p_search_text;
  4229. }
  4230. void TextEdit::set_search_flags(uint32_t p_flags) {
  4231. search_flags = p_flags;
  4232. }
  4233. Point2i TextEdit::search(const String &p_key, uint32_t p_search_flags, int p_from_line, int p_from_column) const {
  4234. if (p_key.is_empty()) {
  4235. return Point2(-1, -1);
  4236. }
  4237. ERR_FAIL_INDEX_V(p_from_line, text.size(), Point2i(-1, -1));
  4238. ERR_FAIL_INDEX_V(p_from_column, text[p_from_line].length() + 1, Point2i(-1, -1));
  4239. const bool key_start_is_symbol = is_symbol(p_key[0]);
  4240. const bool key_end_is_symbol = is_symbol(p_key[p_key.length() - 1]);
  4241. // Search the whole document, starting from the current line.
  4242. // We'll auto-wrap through the start / end to search every line.
  4243. int current_line = p_from_line;
  4244. int current_column = p_from_column;
  4245. // + 1 because we'll search p_from_line twice - starting from p_from_column, and then again at the very end.
  4246. for (int i = 0; i < text.size() + 1; i++) {
  4247. const String &text_line = text[current_line];
  4248. // Search the current line as often as necessary.
  4249. while (true) {
  4250. if (p_search_flags & SEARCH_BACKWARDS) {
  4251. current_column = (p_search_flags & SEARCH_MATCH_CASE) ? text_line.rfind(p_key, current_column) : text_line.rfindn(p_key, current_column);
  4252. } else {
  4253. current_column = (p_search_flags & SEARCH_MATCH_CASE) ? text_line.find(p_key, current_column) : text_line.findn(p_key, current_column);
  4254. }
  4255. if (current_column == -1) {
  4256. break; // Nothing else found on the current line.
  4257. }
  4258. bool is_match = true;
  4259. if (p_search_flags & SEARCH_WHOLE_WORDS) {
  4260. // Validate for whole words.
  4261. if (!key_start_is_symbol && current_column > 0 && !is_symbol(text_line[current_column - 1])) {
  4262. is_match = false;
  4263. } else if (!key_end_is_symbol && current_column + p_key.length() < text_line.length() && !is_symbol(text_line[current_column + p_key.length()])) {
  4264. is_match = false;
  4265. }
  4266. }
  4267. if (is_match) {
  4268. // Found the string!
  4269. return Point2i(current_column, current_line);
  4270. }
  4271. // Advance past the current occurrence.
  4272. current_column += p_search_flags & SEARCH_BACKWARDS ? -1 : 1;
  4273. }
  4274. // Prepare for next iteration.
  4275. if (p_search_flags & SEARCH_BACKWARDS) {
  4276. current_column = -1;
  4277. current_line--;
  4278. if (current_line < 0) {
  4279. // Searched the whole document backwards; wrap to end.
  4280. current_line = text.size() - 1;
  4281. }
  4282. } else {
  4283. current_line++;
  4284. current_column = 0;
  4285. if (current_line == text.size()) {
  4286. // Searched the whole document forwards; wrap to start.
  4287. current_line = 0;
  4288. }
  4289. }
  4290. }
  4291. // Nothing found!
  4292. return Point2i(-1, -1);
  4293. }
  4294. /* Mouse */
  4295. Point2 TextEdit::get_local_mouse_pos() const {
  4296. Point2 mp = get_local_mouse_position();
  4297. if (is_layout_rtl()) {
  4298. mp.x = get_size().width - mp.x;
  4299. }
  4300. return mp;
  4301. }
  4302. String TextEdit::get_word_at_pos(const Vector2 &p_pos) const {
  4303. Point2i pos = get_line_column_at_pos(p_pos, false, false);
  4304. int line = pos.y;
  4305. int col = pos.x;
  4306. return get_word(line, col);
  4307. }
  4308. String TextEdit::get_word(int p_line, int p_column) const {
  4309. if (p_line < 0 || p_column < 0) {
  4310. return String();
  4311. }
  4312. ERR_FAIL_INDEX_V(p_line, text.size(), String());
  4313. const String &text_line = text[p_line];
  4314. if (text_line.is_empty()) {
  4315. return String();
  4316. }
  4317. ERR_FAIL_INDEX_V(p_column, text_line.size() + 1, String());
  4318. const PackedInt32Array words = TS->shaped_text_get_word_breaks(text.get_line_data(p_line)->get_rid());
  4319. for (int i = 0; i < words.size(); i = i + 2) {
  4320. if (words[i] <= p_column && words[i + 1] >= p_column) {
  4321. return text_line.substr(words[i], words[i + 1] - words[i]);
  4322. }
  4323. }
  4324. return String();
  4325. }
  4326. Point2i TextEdit::get_line_column_at_pos(const Point2i &p_pos, bool p_clamp_line, bool p_clamp_column) const {
  4327. float rows = p_pos.y - theme_cache.style_normal->get_margin(SIDE_TOP);
  4328. if (!editable) {
  4329. rows -= theme_cache.style_readonly->get_offset().y / 2;
  4330. rows += theme_cache.style_normal->get_offset().y / 2;
  4331. }
  4332. rows /= get_line_height();
  4333. rows += _get_v_scroll_offset();
  4334. int first_vis_line = get_first_visible_line();
  4335. int row = first_vis_line + Math::floor(rows);
  4336. int wrap_index = 0;
  4337. if (get_line_wrapping_mode() != LineWrappingMode::LINE_WRAPPING_NONE || _is_hiding_enabled()) {
  4338. Point2i f_ofs = get_next_visible_line_index_offset_from(first_vis_line, first_visible_line_wrap_ofs, rows + (1 * SIGN(rows)));
  4339. wrap_index = f_ofs.y;
  4340. if (rows < 0) {
  4341. row = first_vis_line - (f_ofs.x - 1);
  4342. } else {
  4343. row = first_vis_line + (f_ofs.x - 1);
  4344. }
  4345. }
  4346. row = CLAMP(row, 0, text.size() - 1);
  4347. int visible_lines = get_visible_line_count_in_range(first_vis_line, row);
  4348. if (rows > visible_lines) {
  4349. if (p_clamp_line) {
  4350. return Point2i(text[row].length(), row);
  4351. }
  4352. return Point2i(-1, -1);
  4353. }
  4354. int colx = p_pos.x - (Math::ceil(theme_cache.style_normal->get_margin(SIDE_LEFT)) + gutters_width + gutter_padding);
  4355. colx += first_visible_col;
  4356. if (!editable) {
  4357. colx -= theme_cache.style_readonly->get_offset().x / 2;
  4358. colx += theme_cache.style_normal->get_offset().x / 2;
  4359. }
  4360. RID text_rid = text.get_line_data(row)->get_line_rid(wrap_index);
  4361. bool rtl = is_layout_rtl();
  4362. const float wrap_indent = _get_wrap_indent_offset(row, wrap_index, rtl);
  4363. if (rtl) {
  4364. colx = TS->shaped_text_get_size(text_rid).x - colx + wrap_indent;
  4365. } else {
  4366. colx -= wrap_indent;
  4367. }
  4368. if (!p_clamp_column && (colx < 0 || colx > TS->shaped_text_get_size(text_rid).x)) {
  4369. return Point2i(-1, -1);
  4370. }
  4371. int col = TS->shaped_text_hit_test_position(text_rid, colx);
  4372. if (!caret_mid_grapheme_enabled) {
  4373. col = TS->shaped_text_closest_character_pos(text_rid, col);
  4374. }
  4375. return Point2i(col, row);
  4376. }
  4377. Point2i TextEdit::get_pos_at_line_column(int p_line, int p_column) const {
  4378. Rect2i rect = get_rect_at_line_column(p_line, p_column);
  4379. return rect.position.x == -1 ? rect.position : rect.position + Vector2i(0, get_line_height());
  4380. }
  4381. Rect2i TextEdit::get_rect_at_line_column(int p_line, int p_column) const {
  4382. ERR_FAIL_INDEX_V(p_line, text.size(), Rect2i(-1, -1, 0, 0));
  4383. ERR_FAIL_COND_V(p_column < 0, Rect2i(-1, -1, 0, 0));
  4384. ERR_FAIL_COND_V(p_column > text[p_line].length(), Rect2i(-1, -1, 0, 0));
  4385. if (text.size() == 1 && text[0].is_empty()) {
  4386. // The TextEdit is empty.
  4387. return Rect2i();
  4388. }
  4389. if (line_drawing_cache.is_empty() || !line_drawing_cache.has(p_line)) {
  4390. // Line is not in the cache, which means it's outside of the viewing area.
  4391. return Rect2i(-1, -1, 0, 0);
  4392. }
  4393. LineDrawingCache cache_entry = line_drawing_cache[p_line];
  4394. int wrap_index = get_line_wrap_index_at_column(p_line, p_column);
  4395. if (wrap_index >= cache_entry.first_visible_chars.size()) {
  4396. // Line seems to be wrapped beyond the viewable area.
  4397. return Rect2i(-1, -1, 0, 0);
  4398. }
  4399. int first_visible_char = cache_entry.first_visible_chars[wrap_index];
  4400. int last_visible_char = cache_entry.last_visible_chars[wrap_index];
  4401. if (p_column < first_visible_char || p_column > last_visible_char) {
  4402. // Character is outside of the viewing area, no point calculating its position.
  4403. return Rect2i(-1, -1, 0, 0);
  4404. }
  4405. const float wrap_indent = _get_wrap_indent_offset(p_line, wrap_index, is_layout_rtl());
  4406. Point2i pos, size;
  4407. pos.y = cache_entry.y_offset + get_line_height() * wrap_index;
  4408. pos.x = get_total_gutter_width() + Math::ceil(theme_cache.style_normal->get_margin(SIDE_LEFT)) + wrap_indent - get_h_scroll();
  4409. RID text_rid = text.get_line_data(p_line)->get_line_rid(wrap_index);
  4410. Vector2 col_bounds = TS->shaped_text_get_grapheme_bounds(text_rid, p_column);
  4411. pos.x += col_bounds.x;
  4412. size.x = col_bounds.y - col_bounds.x;
  4413. size.y = get_line_height();
  4414. return Rect2i(pos, size);
  4415. }
  4416. int TextEdit::get_minimap_line_at_pos(const Point2i &p_pos) const {
  4417. float rows = p_pos.y;
  4418. rows -= theme_cache.style_normal->get_margin(SIDE_TOP);
  4419. rows /= (minimap_char_size.y + minimap_line_spacing);
  4420. rows += _get_v_scroll_offset();
  4421. // Calculate visible lines.
  4422. int minimap_visible_lines = get_minimap_visible_lines();
  4423. int visible_rows = get_visible_line_count() + 1;
  4424. int first_vis_line = get_first_visible_line() - 1;
  4425. int draw_amount = visible_rows + 1;
  4426. draw_amount += get_line_wrap_count(first_vis_line + 1);
  4427. int minimap_line_height = (minimap_char_size.y + minimap_line_spacing);
  4428. // Calculate viewport size and y offset.
  4429. int viewport_height = (draw_amount - 1) * minimap_line_height;
  4430. int control_height = _get_control_height() - viewport_height;
  4431. int viewport_offset_y = std::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));
  4432. // Calculate the first line.
  4433. int num_lines_before = std::round((viewport_offset_y) / minimap_line_height);
  4434. int minimap_line = (v_scroll->get_max() <= minimap_visible_lines) ? -1 : first_vis_line;
  4435. if (first_vis_line > 0 && minimap_line >= 0) {
  4436. minimap_line -= get_next_visible_line_index_offset_from(first_vis_line, 0, -num_lines_before).x;
  4437. minimap_line -= (minimap_line > 0 && smooth_scroll_enabled ? 1 : 0);
  4438. }
  4439. if (minimap_line < 0) {
  4440. minimap_line = 0;
  4441. }
  4442. int row = minimap_line + Math::floor(rows);
  4443. if (get_line_wrapping_mode() != LineWrappingMode::LINE_WRAPPING_NONE || _is_hiding_enabled()) {
  4444. int f_ofs = get_next_visible_line_index_offset_from(minimap_line, first_visible_line_wrap_ofs, rows + (1 * SIGN(rows))).x - 1;
  4445. if (rows < 0) {
  4446. row = minimap_line - f_ofs;
  4447. } else {
  4448. row = minimap_line + f_ofs;
  4449. }
  4450. }
  4451. row = CLAMP(row, 0, text.size() - 1);
  4452. return row;
  4453. }
  4454. bool TextEdit::is_dragging_cursor() const {
  4455. return dragging_selection || dragging_minimap;
  4456. }
  4457. bool TextEdit::is_mouse_over_selection(bool p_edges, int p_caret) const {
  4458. Point2i pos = get_line_column_at_pos(get_local_mouse_pos());
  4459. int line = pos.y;
  4460. int column = pos.x;
  4461. if ((p_caret == -1 && get_selection_at_line_column(line, column, p_edges) != -1) || (p_caret != -1 && _selection_contains(p_caret, line, column, p_edges))) {
  4462. return true;
  4463. }
  4464. return false;
  4465. }
  4466. /* Caret */
  4467. void TextEdit::set_caret_type(CaretType p_type) {
  4468. if (caret_type == p_type) {
  4469. return;
  4470. }
  4471. caret_type = p_type;
  4472. queue_redraw();
  4473. }
  4474. TextEdit::CaretType TextEdit::get_caret_type() const {
  4475. return caret_type;
  4476. }
  4477. void TextEdit::set_caret_blink_enabled(bool p_enabled) {
  4478. if (caret_blink_enabled == p_enabled) {
  4479. return;
  4480. }
  4481. caret_blink_enabled = p_enabled;
  4482. if (has_focus()) {
  4483. if (p_enabled) {
  4484. caret_blink_timer->start();
  4485. } else {
  4486. caret_blink_timer->stop();
  4487. }
  4488. }
  4489. draw_caret = true;
  4490. }
  4491. bool TextEdit::is_caret_blink_enabled() const {
  4492. return caret_blink_enabled;
  4493. }
  4494. float TextEdit::get_caret_blink_interval() const {
  4495. return caret_blink_timer->get_wait_time();
  4496. }
  4497. void TextEdit::set_caret_blink_interval(const float p_interval) {
  4498. ERR_FAIL_COND(p_interval <= 0);
  4499. caret_blink_timer->set_wait_time(p_interval);
  4500. }
  4501. void TextEdit::set_draw_caret_when_editable_disabled(bool p_enable) {
  4502. if (draw_caret_when_editable_disabled == p_enable) {
  4503. return;
  4504. }
  4505. draw_caret_when_editable_disabled = p_enable;
  4506. queue_redraw();
  4507. }
  4508. bool TextEdit::is_drawing_caret_when_editable_disabled() const {
  4509. return draw_caret_when_editable_disabled;
  4510. }
  4511. void TextEdit::set_move_caret_on_right_click_enabled(bool p_enabled) {
  4512. move_caret_on_right_click = p_enabled;
  4513. }
  4514. bool TextEdit::is_move_caret_on_right_click_enabled() const {
  4515. return move_caret_on_right_click;
  4516. }
  4517. void TextEdit::set_caret_mid_grapheme_enabled(bool p_enabled) {
  4518. caret_mid_grapheme_enabled = p_enabled;
  4519. }
  4520. bool TextEdit::is_caret_mid_grapheme_enabled() const {
  4521. return caret_mid_grapheme_enabled;
  4522. }
  4523. void TextEdit::set_multiple_carets_enabled(bool p_enabled) {
  4524. multi_carets_enabled = p_enabled;
  4525. if (!multi_carets_enabled) {
  4526. remove_secondary_carets();
  4527. multicaret_edit_count = 0;
  4528. multicaret_edit_ignore_carets.clear();
  4529. multicaret_edit_merge_queued = false;
  4530. }
  4531. }
  4532. bool TextEdit::is_multiple_carets_enabled() const {
  4533. return multi_carets_enabled;
  4534. }
  4535. int TextEdit::add_caret(int p_line, int p_column) {
  4536. if (!multi_carets_enabled) {
  4537. return -1;
  4538. }
  4539. _cancel_drag_and_drop_text();
  4540. p_line = CLAMP(p_line, 0, text.size() - 1);
  4541. p_column = CLAMP(p_column, 0, get_line(p_line).length());
  4542. if (!is_in_mulitcaret_edit()) {
  4543. // Carets cannot overlap.
  4544. if (get_selection_at_line_column(p_line, p_column, true, false) != -1) {
  4545. return -1;
  4546. }
  4547. }
  4548. carets.push_back(Caret());
  4549. int new_index = carets.size() - 1;
  4550. set_caret_line(p_line, false, false, -1, new_index);
  4551. set_caret_column(p_column, false, new_index);
  4552. _caret_changed(new_index);
  4553. if (is_in_mulitcaret_edit()) {
  4554. multicaret_edit_ignore_carets.insert(new_index);
  4555. merge_overlapping_carets();
  4556. }
  4557. return new_index;
  4558. }
  4559. void TextEdit::remove_caret(int p_caret) {
  4560. ERR_FAIL_COND_MSG(carets.size() <= 1, "The main caret should not be removed.");
  4561. ERR_FAIL_INDEX(p_caret, carets.size());
  4562. _caret_changed(p_caret);
  4563. carets.remove_at(p_caret);
  4564. if (drag_caret_index >= 0) {
  4565. if (p_caret == drag_caret_index) {
  4566. drag_caret_index = -1;
  4567. } else if (p_caret < drag_caret_index) {
  4568. drag_caret_index -= 1;
  4569. }
  4570. }
  4571. }
  4572. void TextEdit::remove_drag_caret() {
  4573. if (drag_caret_index >= 0) {
  4574. if (drag_caret_index < carets.size()) {
  4575. remove_caret(drag_caret_index);
  4576. }
  4577. drag_caret_index = -1;
  4578. }
  4579. }
  4580. void TextEdit::remove_secondary_carets() {
  4581. if (carets.size() == 1) {
  4582. return;
  4583. }
  4584. _caret_changed();
  4585. carets.resize(1);
  4586. if (drag_caret_index >= 0) {
  4587. drag_caret_index = -1;
  4588. }
  4589. queue_accessibility_update();
  4590. }
  4591. int TextEdit::get_caret_count() const {
  4592. // Don't include drag caret.
  4593. if (drag_caret_index >= 0) {
  4594. return carets.size() - 1;
  4595. }
  4596. return carets.size();
  4597. }
  4598. void TextEdit::add_caret_at_carets(bool p_below) {
  4599. if (!multi_carets_enabled) {
  4600. return;
  4601. }
  4602. const int last_line_max_wrap = get_line_wrap_count(text.size() - 1);
  4603. set_selection_mode(SELECTION_MODE_NONE);
  4604. begin_multicaret_edit();
  4605. int view_target_caret = -1;
  4606. int view_line = p_below ? -1 : INT_MAX;
  4607. int num_carets = get_caret_count();
  4608. for (int i = 0; i < num_carets; i++) {
  4609. const int caret_line = get_caret_line(i);
  4610. const int caret_column = get_caret_column(i);
  4611. bool is_selected = has_selection(i) || carets[i].last_fit_x != carets[i].selection.origin_last_fit_x;
  4612. const int selection_origin_line = get_selection_origin_line(i);
  4613. const int selection_origin_column = get_selection_origin_column(i);
  4614. const int caret_wrap_index = get_caret_wrap_index(i);
  4615. const int selection_origin_wrap_index = !is_selected ? -1 : get_line_wrap_index_at_column(selection_origin_line, selection_origin_column);
  4616. if (caret_line == 0 && !p_below && (caret_wrap_index == 0 || selection_origin_wrap_index == 0)) {
  4617. // Can't add above the first line.
  4618. continue;
  4619. }
  4620. if (caret_line == text.size() - 1 && p_below && (caret_wrap_index == last_line_max_wrap || selection_origin_wrap_index == last_line_max_wrap)) {
  4621. // Can't add below the last line.
  4622. continue;
  4623. }
  4624. // Add a new caret.
  4625. int new_caret_index = add_caret(caret_line, caret_column);
  4626. ERR_FAIL_COND_MSG(new_caret_index < 0, "Failed to add a caret.");
  4627. // Copy the selection origin and last fit.
  4628. set_selection_origin_line(selection_origin_line, true, -1, new_caret_index);
  4629. set_selection_origin_column(selection_origin_column, new_caret_index);
  4630. carets.write[new_caret_index].last_fit_x = carets[i].last_fit_x;
  4631. carets.write[new_caret_index].selection.origin_last_fit_x = carets[i].selection.origin_last_fit_x;
  4632. // Move the caret up or down one visible line.
  4633. if (!p_below) {
  4634. // Move caret up.
  4635. if (caret_wrap_index > 0) {
  4636. set_caret_line(caret_line, false, false, caret_wrap_index - 1, new_caret_index);
  4637. } else {
  4638. int new_line = caret_line - get_next_visible_line_offset_from(caret_line - 1, -1);
  4639. if (is_line_wrapped(new_line)) {
  4640. set_caret_line(new_line, false, false, get_line_wrap_count(new_line), new_caret_index);
  4641. } else {
  4642. set_caret_line(new_line, false, false, 0, new_caret_index);
  4643. }
  4644. }
  4645. // Move selection origin up.
  4646. if (is_selected) {
  4647. if (selection_origin_wrap_index > 0) {
  4648. set_selection_origin_line(caret_line, false, selection_origin_wrap_index - 1, new_caret_index);
  4649. } else {
  4650. int new_line = selection_origin_line - get_next_visible_line_offset_from(selection_origin_line - 1, -1);
  4651. if (is_line_wrapped(new_line)) {
  4652. set_selection_origin_line(new_line, false, get_line_wrap_count(new_line), new_caret_index);
  4653. } else {
  4654. set_selection_origin_line(new_line, false, 0, new_caret_index);
  4655. }
  4656. }
  4657. }
  4658. if (get_caret_line(new_caret_index) < view_line) {
  4659. view_line = get_caret_line(new_caret_index);
  4660. view_target_caret = new_caret_index;
  4661. }
  4662. } else {
  4663. // Move caret down.
  4664. if (caret_wrap_index < get_line_wrap_count(caret_line)) {
  4665. set_caret_line(caret_line, false, false, caret_wrap_index + 1, new_caret_index);
  4666. } else {
  4667. int new_line = caret_line + get_next_visible_line_offset_from(CLAMP(caret_line + 1, 0, text.size() - 1), 1);
  4668. set_caret_line(new_line, false, false, 0, new_caret_index);
  4669. }
  4670. // Move selection origin down.
  4671. if (is_selected) {
  4672. if (selection_origin_wrap_index < get_line_wrap_count(selection_origin_line)) {
  4673. set_selection_origin_line(selection_origin_line, false, selection_origin_wrap_index + 1, new_caret_index);
  4674. } else {
  4675. int new_line = selection_origin_line + get_next_visible_line_offset_from(CLAMP(selection_origin_line + 1, 0, text.size() - 1), 1);
  4676. set_selection_origin_line(new_line, false, 0, new_caret_index);
  4677. }
  4678. }
  4679. if (get_caret_line(new_caret_index) > view_line) {
  4680. view_line = get_caret_line(new_caret_index);
  4681. view_target_caret = new_caret_index;
  4682. }
  4683. }
  4684. if (is_selected) {
  4685. // Make sure selection is active.
  4686. select(get_selection_origin_line(new_caret_index), get_selection_origin_column(new_caret_index), get_caret_line(new_caret_index), get_caret_column(new_caret_index), new_caret_index);
  4687. carets.write[new_caret_index].last_fit_x = carets[i].last_fit_x;
  4688. carets.write[new_caret_index].selection.origin_last_fit_x = carets[i].selection.origin_last_fit_x;
  4689. }
  4690. bool check_edges = !has_selection(0) || !has_selection(new_caret_index);
  4691. bool will_merge_with_main_caret = _selection_contains(0, get_caret_line(new_caret_index), get_caret_column(new_caret_index), check_edges, false) || _selection_contains(new_caret_index, get_caret_line(0), get_caret_column(0), check_edges, false);
  4692. if (will_merge_with_main_caret) {
  4693. // Move next to the main caret so it stays the main caret after merging.
  4694. Caret new_caret = carets[new_caret_index];
  4695. carets.remove_at(new_caret_index);
  4696. carets.insert(0, new_caret);
  4697. i++;
  4698. }
  4699. }
  4700. // Show the topmost caret if added above or bottommost caret if added below.
  4701. if (view_target_caret >= 0 && view_target_caret < get_caret_count()) {
  4702. adjust_viewport_to_caret(view_target_caret);
  4703. }
  4704. merge_overlapping_carets();
  4705. end_multicaret_edit();
  4706. }
  4707. struct _CaretSortComparator {
  4708. _FORCE_INLINE_ bool operator()(const Vector3i &a, const Vector3i &b) const {
  4709. // x is column, y is line, z is caret index.
  4710. if (a.y == b.y) {
  4711. return a.x < b.x;
  4712. }
  4713. return a.y < b.y;
  4714. }
  4715. };
  4716. Vector<int> TextEdit::get_sorted_carets(bool p_include_ignored_carets) const {
  4717. // Returns caret indexes sorted by selection start or caret position from top to bottom of text.
  4718. Vector<Vector3i> caret_line_col_indexes;
  4719. for (int i = 0; i < get_caret_count(); i++) {
  4720. if (!p_include_ignored_carets && multicaret_edit_ignore_caret(i)) {
  4721. continue;
  4722. }
  4723. caret_line_col_indexes.push_back(Vector3i(get_selection_from_column(i), get_selection_from_line(i), i));
  4724. }
  4725. caret_line_col_indexes.sort_custom<_CaretSortComparator>();
  4726. Vector<int> sorted;
  4727. sorted.resize(caret_line_col_indexes.size());
  4728. for (int i = 0; i < caret_line_col_indexes.size(); i++) {
  4729. sorted.set(i, caret_line_col_indexes[i].z);
  4730. }
  4731. return sorted;
  4732. }
  4733. void TextEdit::collapse_carets(int p_from_line, int p_from_column, int p_to_line, int p_to_column, bool p_inclusive) {
  4734. // Collapse carets in the selected range to the from position.
  4735. // Clamp the collapse target position.
  4736. int collapse_line = CLAMP(p_from_line, 0, text.size() - 1);
  4737. int collapse_column = CLAMP(p_from_column, 0, text[collapse_line].length());
  4738. // Swap the lines if they are in the wrong order.
  4739. if (p_from_line > p_to_line) {
  4740. SWAP(p_from_line, p_to_line);
  4741. SWAP(p_from_column, p_to_column);
  4742. }
  4743. if (p_from_line == p_to_line && p_from_column > p_to_column) {
  4744. SWAP(p_from_column, p_to_column);
  4745. }
  4746. bool any_collapsed = false;
  4747. // Intentionally includes carets in the multicaret_edit_ignore list so that they are moved together.
  4748. for (int i = 0; i < get_caret_count(); i++) {
  4749. bool is_caret_in = _is_line_col_in_range(get_caret_line(i), get_caret_column(i), p_from_line, p_from_column, p_to_line, p_to_column, p_inclusive);
  4750. if (!has_selection(i)) {
  4751. if (is_caret_in) {
  4752. // Caret was in the collapsed area.
  4753. set_caret_line(collapse_line, false, true, -1, i);
  4754. set_caret_column(collapse_column, false, i);
  4755. if (is_in_mulitcaret_edit() && get_caret_count() > 1) {
  4756. multicaret_edit_ignore_carets.insert(i);
  4757. }
  4758. any_collapsed = true;
  4759. }
  4760. } else {
  4761. bool is_origin_in = _is_line_col_in_range(get_selection_origin_line(i), get_selection_origin_column(i), p_from_line, p_from_column, p_to_line, p_to_column, p_inclusive);
  4762. if (is_caret_in && is_origin_in) {
  4763. // Selection was completely encapsulated.
  4764. deselect(i);
  4765. set_caret_line(collapse_line, false, true, -1, i);
  4766. set_caret_column(collapse_column, false, i);
  4767. if (is_in_mulitcaret_edit() && get_caret_count() > 1) {
  4768. multicaret_edit_ignore_carets.insert(i);
  4769. }
  4770. any_collapsed = true;
  4771. } else if (is_caret_in) {
  4772. // Only caret was inside.
  4773. set_caret_line(collapse_line, false, true, -1, i);
  4774. set_caret_column(collapse_column, false, i);
  4775. any_collapsed = true;
  4776. } else if (is_origin_in) {
  4777. // Only selection origin was inside.
  4778. set_selection_origin_line(collapse_line, true, -1, i);
  4779. set_selection_origin_column(collapse_column, i);
  4780. any_collapsed = true;
  4781. }
  4782. }
  4783. if (!p_inclusive && !any_collapsed) {
  4784. if ((get_caret_line(i) == collapse_line && get_caret_column(i) == collapse_column) || (get_selection_origin_line(i) == collapse_line && get_selection_origin_column(i) == collapse_column)) {
  4785. // Make sure to queue a merge, even if we didn't include it.
  4786. any_collapsed = true;
  4787. }
  4788. }
  4789. }
  4790. if (any_collapsed) {
  4791. merge_overlapping_carets();
  4792. }
  4793. }
  4794. void TextEdit::merge_overlapping_carets() {
  4795. if (is_in_mulitcaret_edit()) {
  4796. // Queue merge to be performed the end of the multicaret edit.
  4797. multicaret_edit_merge_queued = true;
  4798. return;
  4799. }
  4800. multicaret_edit_merge_queued = false;
  4801. multicaret_edit_ignore_carets.clear();
  4802. if (get_caret_count() == 1) {
  4803. return;
  4804. }
  4805. Vector<int> sorted_carets = get_sorted_carets(true);
  4806. for (int i = 0; i < sorted_carets.size() - 1; i++) {
  4807. int first_caret = sorted_carets[i];
  4808. int second_caret = sorted_carets[i + 1];
  4809. bool merge_carets;
  4810. if (!has_selection(first_caret) || !has_selection(second_caret)) {
  4811. // Merge if touching.
  4812. merge_carets = get_selection_from_line(second_caret) < get_selection_to_line(first_caret) || (get_selection_from_line(second_caret) == get_selection_to_line(first_caret) && get_selection_from_column(second_caret) <= get_selection_to_column(first_caret));
  4813. } else {
  4814. // Merge two selections if overlapping.
  4815. merge_carets = get_selection_from_line(second_caret) < get_selection_to_line(first_caret) || (get_selection_from_line(second_caret) == get_selection_to_line(first_caret) && get_selection_from_column(second_caret) < get_selection_to_column(first_caret));
  4816. }
  4817. if (!merge_carets) {
  4818. continue;
  4819. }
  4820. // Save the newest one for Click + Drag.
  4821. int caret_to_save = first_caret;
  4822. int caret_to_remove = second_caret;
  4823. if (first_caret < second_caret) {
  4824. caret_to_save = second_caret;
  4825. caret_to_remove = first_caret;
  4826. }
  4827. if (get_selection_from_line(caret_to_save) != get_selection_from_line(caret_to_remove) || get_selection_to_line(caret_to_save) != get_selection_to_line(caret_to_remove) || get_selection_from_column(caret_to_save) != get_selection_from_column(caret_to_remove) || get_selection_to_column(caret_to_save) != get_selection_to_column(caret_to_remove)) {
  4828. // Selections are not the same, merge them into one bigger selection.
  4829. int new_from_line = MIN(get_selection_from_line(caret_to_remove), get_selection_from_line(caret_to_save));
  4830. int new_to_line = MAX(get_selection_to_line(caret_to_remove), get_selection_to_line(caret_to_save));
  4831. int new_from_col;
  4832. int new_to_col;
  4833. if (get_selection_from_line(caret_to_remove) < get_selection_from_line(caret_to_save)) {
  4834. new_from_col = get_selection_from_column(caret_to_remove);
  4835. } else if (get_selection_from_line(caret_to_remove) > get_selection_from_line(caret_to_save)) {
  4836. new_from_col = get_selection_from_column(caret_to_save);
  4837. } else {
  4838. new_from_col = MIN(get_selection_from_column(caret_to_remove), get_selection_from_column(caret_to_save));
  4839. }
  4840. if (get_selection_to_line(caret_to_remove) < get_selection_to_line(caret_to_save)) {
  4841. new_to_col = get_selection_to_column(caret_to_save);
  4842. } else if (get_selection_to_line(caret_to_remove) > get_selection_to_line(caret_to_save)) {
  4843. new_to_col = get_selection_to_column(caret_to_remove);
  4844. } else {
  4845. new_to_col = MAX(get_selection_to_column(caret_to_remove), get_selection_to_column(caret_to_save));
  4846. }
  4847. // Use the direction from the last caret or the saved one.
  4848. int caret_dir_to_copy;
  4849. if (has_selection(caret_to_remove) && has_selection(caret_to_save)) {
  4850. caret_dir_to_copy = caret_to_remove == get_caret_count() - 1 ? caret_to_remove : caret_to_save;
  4851. } else {
  4852. caret_dir_to_copy = !has_selection(caret_to_remove) ? caret_to_save : caret_to_remove;
  4853. }
  4854. if (is_caret_after_selection_origin(caret_dir_to_copy)) {
  4855. select(new_from_line, new_from_col, new_to_line, new_to_col, caret_to_save);
  4856. } else {
  4857. select(new_to_line, new_to_col, new_from_line, new_from_col, caret_to_save);
  4858. }
  4859. }
  4860. if (caret_to_save == 0) {
  4861. adjust_viewport_to_caret(caret_to_save);
  4862. }
  4863. remove_caret(caret_to_remove);
  4864. // Update the rest of the sorted list.
  4865. for (int j = i; j < sorted_carets.size(); j++) {
  4866. if (sorted_carets[j] > caret_to_remove) {
  4867. // Shift the index since a caret before it was removed.
  4868. sorted_carets.write[j] -= 1;
  4869. }
  4870. }
  4871. // Remove the caret from the sorted array.
  4872. sorted_carets.remove_at(caret_to_remove == first_caret ? i : i + 1);
  4873. // Process the caret again, since it and the next caret might also overlap.
  4874. i--;
  4875. }
  4876. }
  4877. // Starts a multicaret edit operation. Call this before iterating over the carets and call [end_multicaret_edit] afterwards.
  4878. void TextEdit::begin_multicaret_edit() {
  4879. if (!multi_carets_enabled) {
  4880. return;
  4881. }
  4882. multicaret_edit_count++;
  4883. }
  4884. void TextEdit::end_multicaret_edit() {
  4885. if (!multi_carets_enabled) {
  4886. return;
  4887. }
  4888. if (multicaret_edit_count > 0) {
  4889. multicaret_edit_count--;
  4890. }
  4891. if (multicaret_edit_count != 0) {
  4892. return;
  4893. }
  4894. // This was the last multicaret edit operation.
  4895. if (multicaret_edit_merge_queued) {
  4896. merge_overlapping_carets();
  4897. }
  4898. multicaret_edit_ignore_carets.clear();
  4899. }
  4900. bool TextEdit::is_in_mulitcaret_edit() const {
  4901. return multicaret_edit_count > 0;
  4902. }
  4903. bool TextEdit::multicaret_edit_ignore_caret(int p_caret) const {
  4904. return multicaret_edit_ignore_carets.has(p_caret);
  4905. }
  4906. bool TextEdit::is_caret_visible(int p_caret) const {
  4907. ERR_FAIL_INDEX_V(p_caret, carets.size(), false);
  4908. return carets[p_caret].visible;
  4909. }
  4910. Point2 TextEdit::get_caret_draw_pos(int p_caret) const {
  4911. ERR_FAIL_INDEX_V(p_caret, carets.size(), Point2(0, 0));
  4912. return carets[p_caret].draw_pos;
  4913. }
  4914. void TextEdit::set_caret_line(int p_line, bool p_adjust_viewport, bool p_can_be_hidden, int p_wrap_index, int p_caret) {
  4915. ERR_FAIL_INDEX(p_caret, carets.size());
  4916. if (setting_caret_line) {
  4917. return;
  4918. }
  4919. setting_caret_line = true;
  4920. p_line = CLAMP(p_line, 0, text.size() - 1);
  4921. if (!p_can_be_hidden) {
  4922. if (_is_line_hidden(p_line)) {
  4923. int move_down = get_next_visible_line_offset_from(p_line, 1) - 1;
  4924. if (p_line + move_down <= text.size() - 1 && !_is_line_hidden(p_line + move_down)) {
  4925. p_line += move_down;
  4926. } else {
  4927. int move_up = get_next_visible_line_offset_from(p_line, -1) - 1;
  4928. if (p_line - move_up > 0 && !_is_line_hidden(p_line - move_up)) {
  4929. p_line -= move_up;
  4930. } else {
  4931. WARN_PRINT("Caret set to hidden line " + itos(p_line) + " and there are no nonhidden lines.");
  4932. }
  4933. }
  4934. }
  4935. }
  4936. bool caret_moved = get_caret_line(p_caret) != p_line;
  4937. carets.write[p_caret].line = p_line;
  4938. int n_col;
  4939. if (p_wrap_index >= 0) {
  4940. // Keep caret in same visual x position it was at previously.
  4941. n_col = _get_char_pos_for_line(carets[p_caret].last_fit_x, p_line, p_wrap_index);
  4942. if (n_col != 0 && get_line_wrapping_mode() != LineWrappingMode::LINE_WRAPPING_NONE && p_wrap_index < get_line_wrap_count(p_line)) {
  4943. // Offset by one to not go past the end of the wrapped line.
  4944. if (n_col >= text.get_line_wrap_ranges(p_line)[p_wrap_index].y) {
  4945. n_col -= 1;
  4946. }
  4947. }
  4948. } else {
  4949. // Clamp the column.
  4950. n_col = MIN(get_caret_column(p_caret), get_line(p_line).length());
  4951. }
  4952. caret_moved = (caret_moved || get_caret_column(p_caret) != n_col);
  4953. carets.write[p_caret].column = n_col;
  4954. // Unselect if the caret moved to the selection origin.
  4955. if (p_wrap_index >= 0 && has_selection(p_caret) && get_caret_line(p_caret) == get_selection_origin_line(p_caret) && get_caret_column(p_caret) == get_selection_origin_column(p_caret)) {
  4956. deselect(p_caret);
  4957. }
  4958. if (is_inside_tree() && p_adjust_viewport) {
  4959. adjust_viewport_to_caret(p_caret);
  4960. }
  4961. setting_caret_line = false;
  4962. if (caret_moved) {
  4963. _caret_changed(p_caret);
  4964. }
  4965. queue_accessibility_update();
  4966. }
  4967. int TextEdit::get_caret_line(int p_caret) const {
  4968. ERR_FAIL_INDEX_V(p_caret, carets.size(), 0);
  4969. return carets[p_caret].line;
  4970. }
  4971. void TextEdit::set_caret_column(int p_column, bool p_adjust_viewport, int p_caret) {
  4972. ERR_FAIL_INDEX(p_caret, carets.size());
  4973. p_column = CLAMP(p_column, 0, get_line(get_caret_line(p_caret)).length());
  4974. bool caret_moved = get_caret_column(p_caret) != p_column;
  4975. carets.write[p_caret].column = p_column;
  4976. 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));
  4977. if (!has_selection(p_caret)) {
  4978. // Set the selection origin last fit x to be the same, so we can tell if there was a selection.
  4979. carets.write[p_caret].selection.origin_last_fit_x = carets[p_caret].last_fit_x;
  4980. }
  4981. // Unselect if the caret moved to the selection origin.
  4982. if (has_selection(p_caret) && get_caret_line(p_caret) == get_selection_origin_line(p_caret) && get_caret_column(p_caret) == get_selection_origin_column(p_caret)) {
  4983. deselect(p_caret);
  4984. }
  4985. if (is_inside_tree() && p_adjust_viewport) {
  4986. adjust_viewport_to_caret(p_caret);
  4987. }
  4988. if (caret_moved) {
  4989. _caret_changed(p_caret);
  4990. }
  4991. queue_accessibility_update();
  4992. }
  4993. int TextEdit::get_caret_column(int p_caret) const {
  4994. ERR_FAIL_INDEX_V(p_caret, carets.size(), 0);
  4995. return carets[p_caret].column;
  4996. }
  4997. int TextEdit::get_caret_wrap_index(int p_caret) const {
  4998. ERR_FAIL_INDEX_V(p_caret, carets.size(), 0);
  4999. return get_line_wrap_index_at_column(get_caret_line(p_caret), get_caret_column(p_caret));
  5000. }
  5001. String TextEdit::get_word_under_caret(int p_caret) const {
  5002. ERR_FAIL_COND_V(p_caret >= carets.size() || p_caret < -1, "");
  5003. StringBuilder selected_text;
  5004. for (int c = 0; c < carets.size(); c++) {
  5005. if (p_caret != -1 && p_caret != c) {
  5006. continue;
  5007. }
  5008. PackedInt32Array words = TS->shaped_text_get_word_breaks(text.get_line_data(get_caret_line(c))->get_rid());
  5009. for (int i = 0; i < words.size(); i = i + 2) {
  5010. if (words[i] <= get_caret_column(c) && words[i + 1] > get_caret_column(c)) {
  5011. selected_text += text[get_caret_line(c)].substr(words[i], words[i + 1] - words[i]);
  5012. if (p_caret == -1 && c != carets.size() - 1) {
  5013. selected_text += "\n";
  5014. }
  5015. }
  5016. }
  5017. }
  5018. return selected_text.as_string();
  5019. }
  5020. /* Selection. */
  5021. void TextEdit::set_selecting_enabled(bool p_enabled) {
  5022. if (selecting_enabled == p_enabled) {
  5023. return;
  5024. }
  5025. selecting_enabled = p_enabled;
  5026. if (!selecting_enabled) {
  5027. deselect();
  5028. }
  5029. }
  5030. bool TextEdit::is_selecting_enabled() const {
  5031. return selecting_enabled;
  5032. }
  5033. void TextEdit::set_deselect_on_focus_loss_enabled(bool p_enabled) {
  5034. if (deselect_on_focus_loss_enabled == p_enabled) {
  5035. return;
  5036. }
  5037. deselect_on_focus_loss_enabled = p_enabled;
  5038. if (p_enabled && has_selection() && !has_focus()) {
  5039. deselect();
  5040. }
  5041. }
  5042. bool TextEdit::is_deselect_on_focus_loss_enabled() const {
  5043. return deselect_on_focus_loss_enabled;
  5044. }
  5045. void TextEdit::set_drag_and_drop_selection_enabled(bool p_enabled) {
  5046. drag_and_drop_selection_enabled = p_enabled;
  5047. }
  5048. bool TextEdit::is_drag_and_drop_selection_enabled() const {
  5049. return drag_and_drop_selection_enabled;
  5050. }
  5051. void TextEdit::set_selection_mode(SelectionMode p_mode) {
  5052. selecting_mode = p_mode;
  5053. }
  5054. TextEdit::SelectionMode TextEdit::get_selection_mode() const {
  5055. return selecting_mode;
  5056. }
  5057. void TextEdit::select_all() {
  5058. _push_current_op();
  5059. if (!selecting_enabled) {
  5060. return;
  5061. }
  5062. if (text.size() == 1 && text[0].is_empty()) {
  5063. return;
  5064. }
  5065. remove_secondary_carets();
  5066. set_selection_mode(SelectionMode::SELECTION_MODE_SHIFT);
  5067. select(0, 0, text.size() - 1, text[text.size() - 1].length());
  5068. }
  5069. void TextEdit::select_word_under_caret(int p_caret) {
  5070. ERR_FAIL_COND(p_caret >= carets.size() || p_caret < -1);
  5071. _push_current_op();
  5072. if (!selecting_enabled) {
  5073. return;
  5074. }
  5075. if (text.size() == 1 && text[0].is_empty()) {
  5076. return;
  5077. }
  5078. set_selection_mode(SELECTION_MODE_NONE);
  5079. for (int c = 0; c < carets.size(); c++) {
  5080. if (p_caret != -1 && p_caret != c) {
  5081. continue;
  5082. }
  5083. if (has_selection(c)) {
  5084. // Allow toggling selection by pressing the shortcut a second time.
  5085. // This is also usable as a general-purpose "deselect" shortcut after
  5086. // selecting anything.
  5087. deselect(c);
  5088. continue;
  5089. }
  5090. int begin = 0;
  5091. int end = 0;
  5092. const PackedInt32Array words = TS->shaped_text_get_word_breaks(text.get_line_data(get_caret_line(c))->get_rid());
  5093. for (int i = 0; i < words.size(); i = i + 2) {
  5094. 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])) {
  5095. begin = words[i];
  5096. end = words[i + 1];
  5097. break;
  5098. }
  5099. }
  5100. // No word found.
  5101. if (begin == 0 && end == 0) {
  5102. continue;
  5103. }
  5104. select(get_caret_line(c), begin, get_caret_line(c), end, c);
  5105. }
  5106. merge_overlapping_carets();
  5107. }
  5108. void TextEdit::add_selection_for_next_occurrence() {
  5109. if (!selecting_enabled || !is_multiple_carets_enabled()) {
  5110. return;
  5111. }
  5112. if (text.size() == 1 && text[0].is_empty()) {
  5113. return;
  5114. }
  5115. _push_current_op();
  5116. // Always use the last caret, to correctly search for
  5117. // the next occurrence that comes after this caret.
  5118. int caret = get_caret_count() - 1;
  5119. if (!has_selection(caret)) {
  5120. select_word_under_caret(caret);
  5121. return;
  5122. }
  5123. set_selection_mode(SELECTION_MODE_NONE);
  5124. const String &highlighted_text = get_selected_text(caret);
  5125. int column = get_selection_from_column(caret) + 1;
  5126. int line = get_selection_from_line(caret);
  5127. const Point2i next_occurrence = search(highlighted_text, SEARCH_MATCH_CASE, line, column);
  5128. if (next_occurrence.x == -1 || next_occurrence.y == -1) {
  5129. return;
  5130. }
  5131. int to_column = get_selection_to_column(caret) + 1;
  5132. int end = next_occurrence.x + (to_column - column);
  5133. int new_caret = add_caret(next_occurrence.y, end);
  5134. if (new_caret != -1) {
  5135. select(next_occurrence.y, next_occurrence.x, next_occurrence.y, end, new_caret);
  5136. _unhide_carets();
  5137. adjust_viewport_to_caret(new_caret);
  5138. merge_overlapping_carets();
  5139. }
  5140. }
  5141. void TextEdit::skip_selection_for_next_occurrence() {
  5142. if (!selecting_enabled) {
  5143. return;
  5144. }
  5145. if (text.size() == 1 && text[0].is_empty()) {
  5146. return;
  5147. }
  5148. set_selection_mode(SELECTION_MODE_NONE);
  5149. // Always use the last caret, to correctly search for
  5150. // the next occurrence that comes after this caret.
  5151. int caret = get_caret_count() - 1;
  5152. // Supports getting the text under caret without selecting it.
  5153. // It allows to use this shortcut to simply jump to the next (under caret) word.
  5154. // Due to const and &(reference) presence, ternary operator is a way to avoid errors and warnings.
  5155. const String &searched_text = has_selection(caret) ? get_selected_text(caret) : get_word_under_caret(caret);
  5156. int column = get_selection_from_column(caret) + 1;
  5157. int line = get_selection_from_line(caret);
  5158. const Point2i next_occurrence = search(searched_text, SEARCH_MATCH_CASE, line, column);
  5159. if (next_occurrence.x == -1 || next_occurrence.y == -1) {
  5160. return;
  5161. }
  5162. int to_column = get_selection_to_column(caret) + 1;
  5163. int end = next_occurrence.x + (to_column - column);
  5164. int new_caret = add_caret(next_occurrence.y, end);
  5165. if (new_caret != -1) {
  5166. select(next_occurrence.y, next_occurrence.x, next_occurrence.y, end, new_caret);
  5167. _unhide_carets();
  5168. adjust_viewport_to_caret(new_caret);
  5169. merge_overlapping_carets();
  5170. }
  5171. // Deselect word under previous caret.
  5172. if (has_selection(caret)) {
  5173. select_word_under_caret(caret);
  5174. }
  5175. // Remove previous caret.
  5176. if (get_caret_count() > 1) {
  5177. remove_caret(caret);
  5178. }
  5179. }
  5180. void TextEdit::select(int p_origin_line, int p_origin_column, int p_caret_line, int p_caret_column, int p_caret) {
  5181. ERR_FAIL_INDEX(p_caret, get_caret_count());
  5182. p_caret_line = CLAMP(p_caret_line, 0, text.size() - 1);
  5183. p_caret_column = CLAMP(p_caret_column, 0, text[p_caret_line].length());
  5184. set_caret_line(p_caret_line, false, true, -1, p_caret);
  5185. set_caret_column(p_caret_column, false, p_caret);
  5186. if (!selecting_enabled) {
  5187. return;
  5188. }
  5189. p_origin_line = CLAMP(p_origin_line, 0, text.size() - 1);
  5190. p_origin_column = CLAMP(p_origin_column, 0, text[p_origin_line].length());
  5191. set_selection_origin_line(p_origin_line, true, -1, p_caret);
  5192. set_selection_origin_column(p_origin_column, p_caret);
  5193. bool had_selection = has_selection(p_caret);
  5194. bool activate = p_origin_line != p_caret_line || p_origin_column != p_caret_column;
  5195. carets.write[p_caret].selection.active = activate;
  5196. if (had_selection != activate) {
  5197. _selection_changed(p_caret);
  5198. }
  5199. queue_accessibility_update();
  5200. queue_redraw();
  5201. }
  5202. bool TextEdit::has_selection(int p_caret) const {
  5203. ERR_FAIL_COND_V(p_caret >= carets.size() || p_caret < -1, false);
  5204. if (p_caret >= 0) {
  5205. return carets[p_caret].selection.active;
  5206. }
  5207. for (int i = 0; i < carets.size(); i++) {
  5208. if (carets[i].selection.active) {
  5209. return true;
  5210. }
  5211. }
  5212. return false;
  5213. }
  5214. String TextEdit::get_selected_text(int p_caret) {
  5215. ERR_FAIL_COND_V(p_caret >= carets.size() || p_caret < -1, "");
  5216. if (p_caret >= 0) {
  5217. if (!has_selection(p_caret)) {
  5218. return "";
  5219. }
  5220. return _base_get_text(get_selection_from_line(p_caret), get_selection_from_column(p_caret), get_selection_to_line(p_caret), get_selection_to_column(p_caret));
  5221. }
  5222. StringBuilder selected_text;
  5223. Vector<int> sorted_carets = get_sorted_carets();
  5224. for (int i = 0; i < sorted_carets.size(); i++) {
  5225. int caret_index = sorted_carets[i];
  5226. if (!has_selection(caret_index)) {
  5227. continue;
  5228. }
  5229. if (selected_text.get_string_length() != 0) {
  5230. selected_text += "\n";
  5231. }
  5232. selected_text += _base_get_text(get_selection_from_line(caret_index), get_selection_from_column(caret_index), get_selection_to_line(caret_index), get_selection_to_column(caret_index));
  5233. }
  5234. return selected_text.as_string();
  5235. }
  5236. int TextEdit::get_selection_at_line_column(int p_line, int p_column, bool p_include_edges, bool p_only_selections) const {
  5237. // Return the caret index of the found selection, or -1.
  5238. for (int i = 0; i < get_caret_count(); i++) {
  5239. if (_selection_contains(i, p_line, p_column, p_include_edges, p_only_selections)) {
  5240. return i;
  5241. }
  5242. }
  5243. return -1;
  5244. }
  5245. Vector<Point2i> TextEdit::get_line_ranges_from_carets(bool p_only_selections, bool p_merge_adjacent) const {
  5246. // Get a series of line ranges that cover all lines that have a caret or selection.
  5247. // For each Point2i range, x is the first line and y is the last line.
  5248. Vector<Point2i> ret;
  5249. int last_to_line = INT_MIN;
  5250. Vector<int> sorted_carets = get_sorted_carets();
  5251. for (int i = 0; i < sorted_carets.size(); i++) {
  5252. int caret_index = sorted_carets[i];
  5253. if (p_only_selections && !has_selection(caret_index)) {
  5254. continue;
  5255. }
  5256. Point2i range = Point2i(get_selection_from_line(caret_index), get_selection_to_line(caret_index));
  5257. if (has_selection(caret_index) && get_selection_to_column(caret_index) == 0) {
  5258. // Dont include selection end line if it ends at column 0.
  5259. range.y--;
  5260. }
  5261. if (range.x == last_to_line || (p_merge_adjacent && range.x - 1 == last_to_line)) {
  5262. // Merge if starts on the same line or adjacent line.
  5263. ret.write[ret.size() - 1].y = range.y;
  5264. } else {
  5265. ret.append(range);
  5266. }
  5267. last_to_line = range.y;
  5268. }
  5269. return ret;
  5270. }
  5271. TypedArray<Vector2i> TextEdit::get_line_ranges_from_carets_typed_array(bool p_only_selections, bool p_merge_adjacent) const {
  5272. // Wrapper for `get_line_ranges_from_carets` to return a datatype that can be exposed.
  5273. TypedArray<Vector2i> ret;
  5274. Vector<Point2i> ranges = get_line_ranges_from_carets(p_only_selections, p_merge_adjacent);
  5275. for (const Point2i &range : ranges) {
  5276. ret.push_back(range);
  5277. }
  5278. return ret;
  5279. }
  5280. void TextEdit::set_selection_origin_line(int p_line, bool p_can_be_hidden, int p_wrap_index, int p_caret) {
  5281. if (!selecting_enabled) {
  5282. return;
  5283. }
  5284. ERR_FAIL_INDEX(p_caret, carets.size());
  5285. p_line = CLAMP(p_line, 0, text.size() - 1);
  5286. if (!p_can_be_hidden) {
  5287. if (_is_line_hidden(p_line)) {
  5288. int move_down = get_next_visible_line_offset_from(p_line, 1) - 1;
  5289. if (p_line + move_down <= text.size() - 1 && !_is_line_hidden(p_line + move_down)) {
  5290. p_line += move_down;
  5291. } else {
  5292. int move_up = get_next_visible_line_offset_from(p_line, -1) - 1;
  5293. if (p_line - move_up > 0 && !_is_line_hidden(p_line - move_up)) {
  5294. p_line -= move_up;
  5295. } else {
  5296. WARN_PRINT("Selection origin set to hidden line " + itos(p_line) + " and there are no nonhidden lines.");
  5297. }
  5298. }
  5299. }
  5300. }
  5301. bool selection_moved = get_selection_origin_line(p_caret) != p_line;
  5302. carets.write[p_caret].selection.origin_line = p_line;
  5303. int n_col;
  5304. if (p_wrap_index >= 0) {
  5305. // Keep selection origin in same visual x position it was at previously.
  5306. n_col = _get_char_pos_for_line(carets[p_caret].selection.origin_last_fit_x, p_line, p_wrap_index);
  5307. if (n_col != 0 && get_line_wrapping_mode() != LineWrappingMode::LINE_WRAPPING_NONE && p_wrap_index < get_line_wrap_count(p_line)) {
  5308. // Offset by one to not go past the end of the wrapped line.
  5309. if (n_col >= text.get_line_wrap_ranges(p_line)[p_wrap_index].y) {
  5310. n_col -= 1;
  5311. }
  5312. }
  5313. } else {
  5314. // Clamp the column.
  5315. n_col = MIN(get_selection_origin_column(p_caret), get_line(p_line).length());
  5316. }
  5317. selection_moved = (selection_moved || get_selection_origin_column(p_caret) != n_col);
  5318. carets.write[p_caret].selection.origin_column = n_col;
  5319. // Unselect if the selection origin moved to the caret.
  5320. if (p_wrap_index >= 0 && has_selection(p_caret) && get_caret_line(p_caret) == get_selection_origin_line(p_caret) && get_caret_column(p_caret) == get_selection_origin_column(p_caret)) {
  5321. deselect(p_caret);
  5322. }
  5323. if (selection_moved && has_selection(p_caret)) {
  5324. _selection_changed(p_caret);
  5325. }
  5326. }
  5327. void TextEdit::set_selection_origin_column(int p_column, int p_caret) {
  5328. if (!selecting_enabled) {
  5329. return;
  5330. }
  5331. ERR_FAIL_INDEX(p_caret, carets.size());
  5332. p_column = CLAMP(p_column, 0, get_line(get_selection_origin_line(p_caret)).length());
  5333. bool selection_moved = get_selection_origin_column(p_caret) != p_column;
  5334. carets.write[p_caret].selection.origin_column = p_column;
  5335. carets.write[p_caret].selection.origin_last_fit_x = _get_column_x_offset_for_line(get_selection_origin_column(p_caret), get_selection_origin_line(p_caret), get_selection_origin_column(p_caret));
  5336. // Unselect if the selection origin moved to the caret.
  5337. if (has_selection(p_caret) && get_caret_line(p_caret) == get_selection_origin_line(p_caret) && get_caret_column(p_caret) == get_selection_origin_column(p_caret)) {
  5338. deselect(p_caret);
  5339. }
  5340. if (selection_moved && has_selection(p_caret)) {
  5341. _selection_changed(p_caret);
  5342. }
  5343. }
  5344. int TextEdit::get_selection_origin_line(int p_caret) const {
  5345. ERR_FAIL_INDEX_V(p_caret, carets.size(), -1);
  5346. return carets[p_caret].selection.origin_line;
  5347. }
  5348. int TextEdit::get_selection_origin_column(int p_caret) const {
  5349. ERR_FAIL_INDEX_V(p_caret, carets.size(), -1);
  5350. return carets[p_caret].selection.origin_column;
  5351. }
  5352. int TextEdit::get_next_composite_character_column(int p_line, int p_column) const {
  5353. ERR_FAIL_INDEX_V(p_line, text.size(), -1);
  5354. ERR_FAIL_INDEX_V(p_column, text[p_line].length() + 1, -1);
  5355. if (p_column == text[p_line].length()) {
  5356. return p_column;
  5357. } else {
  5358. return TS->shaped_text_next_character_pos(text.get_line_data(p_line)->get_rid(), (p_column));
  5359. }
  5360. }
  5361. int TextEdit::get_previous_composite_character_column(int p_line, int p_column) const {
  5362. ERR_FAIL_INDEX_V(p_line, text.size(), -1);
  5363. ERR_FAIL_INDEX_V(p_column, text[p_line].length() + 1, -1);
  5364. if (p_column == 0) {
  5365. return 0;
  5366. } else {
  5367. return TS->shaped_text_prev_character_pos(text.get_line_data(p_line)->get_rid(), p_column);
  5368. }
  5369. }
  5370. int TextEdit::get_selection_from_line(int p_caret) const {
  5371. ERR_FAIL_INDEX_V(p_caret, carets.size(), -1);
  5372. if (!has_selection(p_caret)) {
  5373. return carets[p_caret].line;
  5374. }
  5375. return MIN(carets[p_caret].selection.origin_line, carets[p_caret].line);
  5376. }
  5377. int TextEdit::get_selection_from_column(int p_caret) const {
  5378. ERR_FAIL_INDEX_V(p_caret, carets.size(), -1);
  5379. if (!has_selection(p_caret)) {
  5380. return carets[p_caret].column;
  5381. }
  5382. if (carets[p_caret].selection.origin_line < carets[p_caret].line) {
  5383. return carets[p_caret].selection.origin_column;
  5384. } else if (carets[p_caret].selection.origin_line > carets[p_caret].line) {
  5385. return carets[p_caret].column;
  5386. } else {
  5387. return MIN(carets[p_caret].selection.origin_column, carets[p_caret].column);
  5388. }
  5389. }
  5390. int TextEdit::get_selection_to_line(int p_caret) const {
  5391. ERR_FAIL_INDEX_V(p_caret, carets.size(), -1);
  5392. if (!has_selection(p_caret)) {
  5393. return carets[p_caret].line;
  5394. }
  5395. return MAX(carets[p_caret].selection.origin_line, carets[p_caret].line);
  5396. }
  5397. int TextEdit::get_selection_to_column(int p_caret) const {
  5398. ERR_FAIL_INDEX_V(p_caret, carets.size(), -1);
  5399. if (!has_selection(p_caret)) {
  5400. return carets[p_caret].column;
  5401. }
  5402. if (carets[p_caret].selection.origin_line < carets[p_caret].line) {
  5403. return carets[p_caret].column;
  5404. } else if (carets[p_caret].selection.origin_line > carets[p_caret].line) {
  5405. return carets[p_caret].selection.origin_column;
  5406. } else {
  5407. return MAX(carets[p_caret].selection.origin_column, carets[p_caret].column);
  5408. }
  5409. }
  5410. bool TextEdit::is_caret_after_selection_origin(int p_caret) const {
  5411. ERR_FAIL_INDEX_V(p_caret, carets.size(), false);
  5412. if (!has_selection(p_caret)) {
  5413. return true;
  5414. }
  5415. return carets[p_caret].line > carets[p_caret].selection.origin_line || (carets[p_caret].line == carets[p_caret].selection.origin_line && carets[p_caret].column >= carets[p_caret].selection.origin_column);
  5416. }
  5417. void TextEdit::deselect(int p_caret) {
  5418. ERR_FAIL_COND(p_caret >= carets.size() || p_caret < -1);
  5419. bool selection_changed = false;
  5420. if (p_caret >= 0) {
  5421. selection_changed = carets.write[p_caret].selection.active;
  5422. carets.write[p_caret].selection.active = false;
  5423. } else {
  5424. for (int i = 0; i < carets.size(); i++) {
  5425. selection_changed |= carets.write[i].selection.active;
  5426. carets.write[i].selection.active = false;
  5427. }
  5428. }
  5429. if (selection_changed) {
  5430. _selection_changed(p_caret);
  5431. }
  5432. queue_accessibility_update();
  5433. queue_redraw();
  5434. }
  5435. void TextEdit::delete_selection(int p_caret) {
  5436. ERR_FAIL_COND(p_caret >= get_caret_count() || p_caret < -1);
  5437. begin_complex_operation();
  5438. begin_multicaret_edit();
  5439. for (int i = 0; i < get_caret_count(); i++) {
  5440. if (p_caret != -1 && p_caret != i) {
  5441. continue;
  5442. }
  5443. if (p_caret == -1 && multicaret_edit_ignore_caret(i)) {
  5444. continue;
  5445. }
  5446. if (!has_selection(i)) {
  5447. continue;
  5448. }
  5449. int selection_from_line = get_selection_from_line(i);
  5450. int selection_from_column = get_selection_from_column(i);
  5451. int selection_to_line = get_selection_to_line(i);
  5452. int selection_to_column = get_selection_to_column(i);
  5453. _remove_text(selection_from_line, selection_from_column, selection_to_line, selection_to_column);
  5454. _offset_carets_after(selection_to_line, selection_to_column, selection_from_line, selection_from_column);
  5455. merge_overlapping_carets();
  5456. deselect(i);
  5457. set_caret_line(selection_from_line, false, false, -1, i);
  5458. set_caret_column(selection_from_column, i == 0, i);
  5459. }
  5460. end_multicaret_edit();
  5461. end_complex_operation();
  5462. }
  5463. /* Line wrapping. */
  5464. void TextEdit::set_line_wrapping_mode(LineWrappingMode p_wrapping_mode) {
  5465. if (line_wrapping_mode != p_wrapping_mode) {
  5466. line_wrapping_mode = p_wrapping_mode;
  5467. _update_wrap_at_column(true);
  5468. queue_accessibility_update();
  5469. queue_redraw();
  5470. }
  5471. }
  5472. TextEdit::LineWrappingMode TextEdit::get_line_wrapping_mode() const {
  5473. return line_wrapping_mode;
  5474. }
  5475. void TextEdit::set_autowrap_mode(TextServer::AutowrapMode p_mode) {
  5476. if (autowrap_mode == p_mode) {
  5477. return;
  5478. }
  5479. autowrap_mode = p_mode;
  5480. if (get_line_wrapping_mode() != LineWrappingMode::LINE_WRAPPING_NONE) {
  5481. _update_wrap_at_column(true);
  5482. queue_accessibility_update();
  5483. queue_redraw();
  5484. }
  5485. }
  5486. TextServer::AutowrapMode TextEdit::get_autowrap_mode() const {
  5487. return autowrap_mode;
  5488. }
  5489. bool TextEdit::is_line_wrapped(int p_line) const {
  5490. ERR_FAIL_INDEX_V(p_line, text.size(), false);
  5491. if (get_line_wrapping_mode() == LineWrappingMode::LINE_WRAPPING_NONE) {
  5492. return false;
  5493. }
  5494. return text.get_line_wrap_amount(p_line) > 0;
  5495. }
  5496. int TextEdit::get_line_wrap_count(int p_line) const {
  5497. ERR_FAIL_INDEX_V(p_line, text.size(), 0);
  5498. if (!is_line_wrapped(p_line)) {
  5499. return 0;
  5500. }
  5501. return text.get_line_wrap_amount(p_line);
  5502. }
  5503. int TextEdit::get_line_wrap_index_at_column(int p_line, int p_column) const {
  5504. ERR_FAIL_INDEX_V(p_line, text.size(), 0);
  5505. ERR_FAIL_COND_V(p_column < 0, 0);
  5506. ERR_FAIL_COND_V(p_column > text[p_line].length(), 0);
  5507. if (!is_line_wrapped(p_line)) {
  5508. return 0;
  5509. }
  5510. /* Loop through wraps in the line text until we get to the column. */
  5511. int wrap_index = 0;
  5512. int col = 0;
  5513. Vector<String> lines = get_line_wrapped_text(p_line);
  5514. for (int i = 0; i < lines.size(); i++) {
  5515. wrap_index = i;
  5516. col += lines[wrap_index].length();
  5517. if (col > p_column) {
  5518. break;
  5519. }
  5520. }
  5521. return wrap_index;
  5522. }
  5523. Vector<String> TextEdit::get_line_wrapped_text(int p_line) const {
  5524. ERR_FAIL_INDEX_V(p_line, text.size(), Vector<String>());
  5525. Vector<String> lines;
  5526. if (!is_line_wrapped(p_line)) {
  5527. lines.push_back(text[p_line]);
  5528. return lines;
  5529. }
  5530. const String &line_text = text[p_line];
  5531. Vector<Vector2i> line_ranges = text.get_line_wrap_ranges(p_line);
  5532. for (int i = 0; i < line_ranges.size(); i++) {
  5533. lines.push_back(line_text.substr(line_ranges[i].x, line_ranges[i].y - line_ranges[i].x));
  5534. }
  5535. return lines;
  5536. }
  5537. /* Viewport */
  5538. // Scrolling.
  5539. void TextEdit::set_smooth_scroll_enabled(bool p_enabled) {
  5540. v_scroll->set_smooth_scroll_enabled(p_enabled);
  5541. smooth_scroll_enabled = p_enabled;
  5542. }
  5543. bool TextEdit::is_smooth_scroll_enabled() const {
  5544. return smooth_scroll_enabled;
  5545. }
  5546. void TextEdit::set_scroll_past_end_of_file_enabled(bool p_enabled) {
  5547. if (scroll_past_end_of_file_enabled == p_enabled) {
  5548. return;
  5549. }
  5550. scroll_past_end_of_file_enabled = p_enabled;
  5551. queue_redraw();
  5552. }
  5553. bool TextEdit::is_scroll_past_end_of_file_enabled() const {
  5554. return scroll_past_end_of_file_enabled;
  5555. }
  5556. VScrollBar *TextEdit::get_v_scroll_bar() const {
  5557. return v_scroll;
  5558. }
  5559. HScrollBar *TextEdit::get_h_scroll_bar() const {
  5560. return h_scroll;
  5561. }
  5562. void TextEdit::set_v_scroll(double p_scroll) {
  5563. v_scroll->set_value(p_scroll);
  5564. int max_v_scroll = v_scroll->get_max() - v_scroll->get_page();
  5565. if (p_scroll >= max_v_scroll - 1.0) {
  5566. _scroll_moved(v_scroll->get_value());
  5567. }
  5568. queue_accessibility_update();
  5569. }
  5570. double TextEdit::get_v_scroll() const {
  5571. return v_scroll->get_value();
  5572. }
  5573. void TextEdit::set_h_scroll(int p_scroll) {
  5574. if (p_scroll < 0) {
  5575. p_scroll = 0;
  5576. }
  5577. h_scroll->set_value(p_scroll);
  5578. queue_accessibility_update();
  5579. }
  5580. int TextEdit::get_h_scroll() const {
  5581. return h_scroll->get_value();
  5582. }
  5583. void TextEdit::set_v_scroll_speed(float p_speed) {
  5584. // Prevent setting a vertical scroll speed value under 1.
  5585. ERR_FAIL_COND(p_speed < 1.0);
  5586. v_scroll_speed = p_speed;
  5587. }
  5588. float TextEdit::get_v_scroll_speed() const {
  5589. return v_scroll_speed;
  5590. }
  5591. void TextEdit::set_fit_content_height_enabled(bool p_enabled) {
  5592. if (fit_content_height == p_enabled) {
  5593. return;
  5594. }
  5595. fit_content_height = p_enabled;
  5596. update_minimum_size();
  5597. }
  5598. bool TextEdit::is_fit_content_height_enabled() const {
  5599. return fit_content_height;
  5600. }
  5601. void TextEdit::set_fit_content_width_enabled(bool p_enabled) {
  5602. if (fit_content_width == p_enabled) {
  5603. return;
  5604. }
  5605. fit_content_width = p_enabled;
  5606. update_minimum_size();
  5607. }
  5608. bool TextEdit::is_fit_content_width_enabled() const {
  5609. return fit_content_width;
  5610. }
  5611. double TextEdit::get_scroll_pos_for_line(int p_line, int p_wrap_index) const {
  5612. ERR_FAIL_INDEX_V(p_line, text.size(), 0);
  5613. ERR_FAIL_COND_V(p_wrap_index < 0, 0);
  5614. ERR_FAIL_COND_V(p_wrap_index > get_line_wrap_count(p_line), 0);
  5615. if (get_line_wrapping_mode() == LineWrappingMode::LINE_WRAPPING_NONE && !_is_hiding_enabled()) {
  5616. return p_line;
  5617. }
  5618. double new_line_scroll_pos = 0.0;
  5619. if (p_line > 0) {
  5620. new_line_scroll_pos = get_visible_line_count_in_range(0, MIN(p_line - 1, text.size() - 1));
  5621. }
  5622. new_line_scroll_pos += p_wrap_index;
  5623. return new_line_scroll_pos;
  5624. }
  5625. // Visible lines.
  5626. void TextEdit::set_line_as_first_visible(int p_line, int p_wrap_index) {
  5627. ERR_FAIL_INDEX(p_line, text.size());
  5628. ERR_FAIL_COND(p_wrap_index < 0);
  5629. ERR_FAIL_COND(p_wrap_index > get_line_wrap_count(p_line));
  5630. set_v_scroll(get_scroll_pos_for_line(p_line, p_wrap_index));
  5631. scrolling = false;
  5632. minimap_clicked = false;
  5633. }
  5634. int TextEdit::get_first_visible_line() const {
  5635. return CLAMP(first_visible_line, 0, text.size() - 1);
  5636. }
  5637. void TextEdit::set_line_as_center_visible(int p_line, int p_wrap_index) {
  5638. ERR_FAIL_INDEX(p_line, text.size());
  5639. ERR_FAIL_COND(p_wrap_index < 0);
  5640. ERR_FAIL_COND(p_wrap_index > get_line_wrap_count(p_line));
  5641. scrolling = false;
  5642. minimap_clicked = false;
  5643. int visible_rows = get_visible_line_count();
  5644. Point2i next_line = get_next_visible_line_index_offset_from(p_line, p_wrap_index, (-visible_rows / 2) - 1);
  5645. int first_line = p_line - next_line.x + 1;
  5646. if (first_line < 0) {
  5647. set_v_scroll(0);
  5648. return;
  5649. }
  5650. set_v_scroll(get_scroll_pos_for_line(first_line, next_line.y));
  5651. }
  5652. void TextEdit::set_line_as_last_visible(int p_line, int p_wrap_index) {
  5653. ERR_FAIL_INDEX(p_line, text.size());
  5654. ERR_FAIL_COND(p_wrap_index < 0);
  5655. ERR_FAIL_COND(p_wrap_index > get_line_wrap_count(p_line));
  5656. scrolling = false;
  5657. minimap_clicked = false;
  5658. Point2i next_line = get_next_visible_line_index_offset_from(p_line, p_wrap_index, -get_visible_line_count() - 1);
  5659. int first_line = p_line - next_line.x + 1;
  5660. // Adding _get_visible_lines_offset is not 100% correct as we end up showing almost p_line + 1, however, it provides a
  5661. // better user experience. Therefore we need to special case < visible line count, else showing line 0 is impossible.
  5662. if (get_visible_line_count_in_range(0, p_line) < get_visible_line_count() + 1) {
  5663. set_v_scroll(0);
  5664. return;
  5665. }
  5666. set_v_scroll(Math::round(get_scroll_pos_for_line(first_line, next_line.y) + _get_visible_lines_offset()));
  5667. }
  5668. int TextEdit::get_last_full_visible_line() const {
  5669. int first_vis_line = get_first_visible_line();
  5670. int last_vis_line = 0;
  5671. 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;
  5672. last_vis_line = CLAMP(last_vis_line, 0, text.size() - 1);
  5673. return last_vis_line;
  5674. }
  5675. int TextEdit::get_last_full_visible_line_wrap_index() const {
  5676. int first_vis_line = get_first_visible_line();
  5677. return get_next_visible_line_index_offset_from(first_vis_line, first_visible_line_wrap_ofs, get_visible_line_count()).y;
  5678. }
  5679. int TextEdit::get_visible_line_count() const {
  5680. return _get_control_height() / get_line_height();
  5681. }
  5682. int TextEdit::get_visible_line_count_in_range(int p_from_line, int p_to_line) const {
  5683. ERR_FAIL_INDEX_V(p_from_line, text.size(), 0);
  5684. ERR_FAIL_INDEX_V(p_to_line, text.size(), 0);
  5685. // So we can handle inputs in whatever order.
  5686. if (p_from_line > p_to_line) {
  5687. SWAP(p_from_line, p_to_line);
  5688. }
  5689. // Returns the total number of (lines + wrapped - hidden).
  5690. if (!_is_hiding_enabled() && get_line_wrapping_mode() == LineWrappingMode::LINE_WRAPPING_NONE) {
  5691. return (p_to_line - p_from_line) + 1;
  5692. }
  5693. int total_rows = 0;
  5694. for (int i = p_from_line; i <= p_to_line; i++) {
  5695. if (!text.is_hidden(i)) {
  5696. total_rows++;
  5697. total_rows += get_line_wrap_count(i);
  5698. }
  5699. }
  5700. return total_rows;
  5701. }
  5702. int TextEdit::get_total_visible_line_count() const {
  5703. return text.get_total_visible_line_count();
  5704. }
  5705. // Auto adjust.
  5706. void TextEdit::adjust_viewport_to_caret(int p_caret) {
  5707. ERR_FAIL_INDEX(p_caret, carets.size());
  5708. // Move viewport so the caret is visible on the screen vertically.
  5709. int cur_line = get_caret_line(p_caret);
  5710. int cur_wrap = get_caret_wrap_index(p_caret);
  5711. int first_vis_line = get_first_visible_line();
  5712. int first_vis_wrap = first_visible_line_wrap_ofs;
  5713. int last_vis_line = get_last_full_visible_line();
  5714. int last_vis_wrap = get_last_full_visible_line_wrap_index();
  5715. if (cur_line < first_vis_line || (cur_line == first_vis_line && cur_wrap < first_vis_wrap)) {
  5716. // Caret is above screen.
  5717. set_line_as_first_visible(cur_line, cur_wrap);
  5718. } else if (cur_line > last_vis_line || (cur_line == last_vis_line && cur_wrap > last_vis_wrap)) {
  5719. // Caret is below screen.
  5720. set_line_as_last_visible(cur_line, cur_wrap);
  5721. }
  5722. _adjust_viewport_to_caret_horizontally(p_caret, false);
  5723. }
  5724. void TextEdit::center_viewport_to_caret(int p_caret) {
  5725. ERR_FAIL_INDEX(p_caret, carets.size());
  5726. // Move viewport so the caret is in the center of the screen vertically.
  5727. scrolling = false;
  5728. minimap_clicked = false;
  5729. set_line_as_center_visible(get_caret_line(p_caret), get_caret_wrap_index(p_caret));
  5730. _adjust_viewport_to_caret_horizontally(p_caret);
  5731. }
  5732. /* Minimap */
  5733. void TextEdit::set_draw_minimap(bool p_enabled) {
  5734. if (draw_minimap == p_enabled) {
  5735. return;
  5736. }
  5737. draw_minimap = p_enabled;
  5738. _update_wrap_at_column();
  5739. queue_redraw();
  5740. }
  5741. bool TextEdit::is_drawing_minimap() const {
  5742. return draw_minimap;
  5743. }
  5744. void TextEdit::set_minimap_width(int p_minimap_width) {
  5745. if (minimap_width == p_minimap_width) {
  5746. return;
  5747. }
  5748. minimap_width = p_minimap_width;
  5749. _update_wrap_at_column();
  5750. queue_redraw();
  5751. }
  5752. int TextEdit::get_minimap_width() const {
  5753. return minimap_width;
  5754. }
  5755. int TextEdit::get_minimap_visible_lines() const {
  5756. return _get_control_height() / (minimap_char_size.y + minimap_line_spacing);
  5757. }
  5758. /* Gutters. */
  5759. void TextEdit::add_gutter(int p_at) {
  5760. if (p_at < 0 || p_at > gutters.size()) {
  5761. gutters.push_back(GutterInfo());
  5762. } else {
  5763. gutters.insert(p_at, GutterInfo());
  5764. }
  5765. text.add_gutter(p_at);
  5766. _update_gutter_width();
  5767. emit_signal(SNAME("gutter_added"));
  5768. queue_redraw();
  5769. }
  5770. void TextEdit::remove_gutter(int p_gutter) {
  5771. ERR_FAIL_INDEX(p_gutter, gutters.size());
  5772. gutters.remove_at(p_gutter);
  5773. text.remove_gutter(p_gutter);
  5774. _update_gutter_width();
  5775. emit_signal(SNAME("gutter_removed"));
  5776. queue_redraw();
  5777. }
  5778. int TextEdit::get_gutter_count() const {
  5779. return gutters.size();
  5780. }
  5781. void TextEdit::set_gutter_name(int p_gutter, const String &p_name) {
  5782. ERR_FAIL_INDEX(p_gutter, gutters.size());
  5783. gutters.write[p_gutter].name = p_name;
  5784. }
  5785. String TextEdit::get_gutter_name(int p_gutter) const {
  5786. ERR_FAIL_INDEX_V(p_gutter, gutters.size(), "");
  5787. return gutters[p_gutter].name;
  5788. }
  5789. void TextEdit::set_gutter_type(int p_gutter, GutterType p_type) {
  5790. ERR_FAIL_INDEX(p_gutter, gutters.size());
  5791. if (gutters[p_gutter].type == p_type) {
  5792. return;
  5793. }
  5794. gutters.write[p_gutter].type = p_type;
  5795. queue_redraw();
  5796. }
  5797. TextEdit::GutterType TextEdit::get_gutter_type(int p_gutter) const {
  5798. ERR_FAIL_INDEX_V(p_gutter, gutters.size(), GUTTER_TYPE_STRING);
  5799. return gutters[p_gutter].type;
  5800. }
  5801. void TextEdit::set_gutter_width(int p_gutter, int p_width) {
  5802. ERR_FAIL_INDEX(p_gutter, gutters.size());
  5803. if (gutters[p_gutter].width == p_width) {
  5804. return;
  5805. }
  5806. gutters.write[p_gutter].width = p_width;
  5807. _update_gutter_width();
  5808. }
  5809. int TextEdit::get_gutter_width(int p_gutter) const {
  5810. ERR_FAIL_INDEX_V(p_gutter, gutters.size(), -1);
  5811. return gutters[p_gutter].width;
  5812. }
  5813. int TextEdit::get_total_gutter_width() const {
  5814. return gutters_width + gutter_padding;
  5815. }
  5816. void TextEdit::set_gutter_draw(int p_gutter, bool p_draw) {
  5817. ERR_FAIL_INDEX(p_gutter, gutters.size());
  5818. if (gutters[p_gutter].draw == p_draw) {
  5819. return;
  5820. }
  5821. gutters.write[p_gutter].draw = p_draw;
  5822. _update_gutter_width();
  5823. }
  5824. bool TextEdit::is_gutter_drawn(int p_gutter) const {
  5825. ERR_FAIL_INDEX_V(p_gutter, gutters.size(), false);
  5826. return gutters[p_gutter].draw;
  5827. }
  5828. void TextEdit::set_gutter_clickable(int p_gutter, bool p_clickable) {
  5829. ERR_FAIL_INDEX(p_gutter, gutters.size());
  5830. if (gutters[p_gutter].clickable == p_clickable) {
  5831. return;
  5832. }
  5833. gutters.write[p_gutter].clickable = p_clickable;
  5834. queue_redraw();
  5835. }
  5836. bool TextEdit::is_gutter_clickable(int p_gutter) const {
  5837. ERR_FAIL_INDEX_V(p_gutter, gutters.size(), false);
  5838. return gutters[p_gutter].clickable;
  5839. }
  5840. void TextEdit::set_gutter_overwritable(int p_gutter, bool p_overwritable) {
  5841. ERR_FAIL_INDEX(p_gutter, gutters.size());
  5842. gutters.write[p_gutter].overwritable = p_overwritable;
  5843. }
  5844. bool TextEdit::is_gutter_overwritable(int p_gutter) const {
  5845. ERR_FAIL_INDEX_V(p_gutter, gutters.size(), false);
  5846. return gutters[p_gutter].overwritable;
  5847. }
  5848. void TextEdit::merge_gutters(int p_from_line, int p_to_line) {
  5849. ERR_FAIL_INDEX(p_from_line, text.size());
  5850. ERR_FAIL_INDEX(p_to_line, text.size());
  5851. if (p_from_line == p_to_line) {
  5852. return;
  5853. }
  5854. for (int i = 0; i < gutters.size(); i++) {
  5855. if (!gutters[i].overwritable) {
  5856. continue;
  5857. }
  5858. if (text.get_line_gutter_text(p_from_line, i) != "") {
  5859. text.set_line_gutter_text(p_to_line, i, text.get_line_gutter_text(p_from_line, i));
  5860. text.set_line_gutter_item_color(p_to_line, i, text.get_line_gutter_item_color(p_from_line, i));
  5861. }
  5862. if (text.get_line_gutter_icon(p_from_line, i).is_valid()) {
  5863. text.set_line_gutter_icon(p_to_line, i, text.get_line_gutter_icon(p_from_line, i));
  5864. text.set_line_gutter_item_color(p_to_line, i, text.get_line_gutter_item_color(p_from_line, i));
  5865. }
  5866. if (text.get_line_gutter_metadata(p_from_line, i) != "") {
  5867. text.set_line_gutter_metadata(p_to_line, i, text.get_line_gutter_metadata(p_from_line, i));
  5868. }
  5869. if (text.is_line_gutter_clickable(p_from_line, i)) {
  5870. text.set_line_gutter_clickable(p_to_line, i, true);
  5871. }
  5872. }
  5873. queue_redraw();
  5874. }
  5875. void TextEdit::set_gutter_custom_draw(int p_gutter, const Callable &p_draw_callback) {
  5876. ERR_FAIL_INDEX(p_gutter, gutters.size());
  5877. if (gutters[p_gutter].custom_draw_callback == p_draw_callback) {
  5878. return;
  5879. }
  5880. gutters.write[p_gutter].custom_draw_callback = p_draw_callback;
  5881. queue_redraw();
  5882. }
  5883. // Line gutters.
  5884. void TextEdit::set_line_gutter_metadata(int p_line, int p_gutter, const Variant &p_metadata) {
  5885. ERR_FAIL_INDEX(p_line, text.size());
  5886. ERR_FAIL_INDEX(p_gutter, gutters.size());
  5887. text.set_line_gutter_metadata(p_line, p_gutter, p_metadata);
  5888. }
  5889. Variant TextEdit::get_line_gutter_metadata(int p_line, int p_gutter) const {
  5890. ERR_FAIL_INDEX_V(p_line, text.size(), "");
  5891. ERR_FAIL_INDEX_V(p_gutter, gutters.size(), "");
  5892. return text.get_line_gutter_metadata(p_line, p_gutter);
  5893. }
  5894. void TextEdit::set_line_gutter_text(int p_line, int p_gutter, const String &p_text) {
  5895. ERR_FAIL_INDEX(p_line, text.size());
  5896. ERR_FAIL_INDEX(p_gutter, gutters.size());
  5897. if (text.get_line_gutter_text(p_line, p_gutter) == p_text) {
  5898. return;
  5899. }
  5900. text.set_line_gutter_text(p_line, p_gutter, p_text);
  5901. queue_redraw();
  5902. }
  5903. String TextEdit::get_line_gutter_text(int p_line, int p_gutter) const {
  5904. ERR_FAIL_INDEX_V(p_line, text.size(), "");
  5905. ERR_FAIL_INDEX_V(p_gutter, gutters.size(), "");
  5906. return text.get_line_gutter_text(p_line, p_gutter);
  5907. }
  5908. void TextEdit::set_line_gutter_icon(int p_line, int p_gutter, const Ref<Texture2D> &p_icon) {
  5909. ERR_FAIL_INDEX(p_line, text.size());
  5910. ERR_FAIL_INDEX(p_gutter, gutters.size());
  5911. if (text.get_line_gutter_icon(p_line, p_gutter) == p_icon) {
  5912. return;
  5913. }
  5914. text.set_line_gutter_icon(p_line, p_gutter, p_icon);
  5915. queue_redraw();
  5916. }
  5917. Ref<Texture2D> TextEdit::get_line_gutter_icon(int p_line, int p_gutter) const {
  5918. ERR_FAIL_INDEX_V(p_line, text.size(), Ref<Texture2D>());
  5919. ERR_FAIL_INDEX_V(p_gutter, gutters.size(), Ref<Texture2D>());
  5920. return text.get_line_gutter_icon(p_line, p_gutter);
  5921. }
  5922. void TextEdit::set_line_gutter_item_color(int p_line, int p_gutter, const Color &p_color) {
  5923. ERR_FAIL_INDEX(p_line, text.size());
  5924. ERR_FAIL_INDEX(p_gutter, gutters.size());
  5925. if (text.get_line_gutter_item_color(p_line, p_gutter) == p_color) {
  5926. return;
  5927. }
  5928. text.set_line_gutter_item_color(p_line, p_gutter, p_color);
  5929. queue_redraw();
  5930. }
  5931. Color TextEdit::get_line_gutter_item_color(int p_line, int p_gutter) const {
  5932. ERR_FAIL_INDEX_V(p_line, text.size(), Color());
  5933. ERR_FAIL_INDEX_V(p_gutter, gutters.size(), Color());
  5934. return text.get_line_gutter_item_color(p_line, p_gutter);
  5935. }
  5936. void TextEdit::set_line_gutter_clickable(int p_line, int p_gutter, bool p_clickable) {
  5937. ERR_FAIL_INDEX(p_line, text.size());
  5938. ERR_FAIL_INDEX(p_gutter, gutters.size());
  5939. text.set_line_gutter_clickable(p_line, p_gutter, p_clickable);
  5940. }
  5941. bool TextEdit::is_line_gutter_clickable(int p_line, int p_gutter) const {
  5942. ERR_FAIL_INDEX_V(p_line, text.size(), false);
  5943. ERR_FAIL_INDEX_V(p_gutter, gutters.size(), false);
  5944. return text.is_line_gutter_clickable(p_line, p_gutter);
  5945. }
  5946. // Line style
  5947. void TextEdit::set_line_background_color(int p_line, const Color &p_color) {
  5948. ERR_FAIL_INDEX(p_line, text.size());
  5949. if (text.get_line_background_color(p_line) == p_color) {
  5950. return;
  5951. }
  5952. text.set_line_background_color(p_line, p_color);
  5953. queue_redraw();
  5954. }
  5955. Color TextEdit::get_line_background_color(int p_line) const {
  5956. ERR_FAIL_INDEX_V(p_line, text.size(), Color());
  5957. return text.get_line_background_color(p_line);
  5958. }
  5959. /* Syntax Highlighting. */
  5960. void TextEdit::set_syntax_highlighter(Ref<SyntaxHighlighter> p_syntax_highlighter) {
  5961. if (syntax_highlighter == p_syntax_highlighter && syntax_highlighter.is_valid() == p_syntax_highlighter.is_valid()) {
  5962. return;
  5963. }
  5964. syntax_highlighter = p_syntax_highlighter;
  5965. if (syntax_highlighter.is_valid()) {
  5966. syntax_highlighter->set_text_edit(this);
  5967. }
  5968. _clear_syntax_highlighting_cache();
  5969. queue_redraw();
  5970. }
  5971. Ref<SyntaxHighlighter> TextEdit::get_syntax_highlighter() const {
  5972. return syntax_highlighter;
  5973. }
  5974. /* Visual. */
  5975. void TextEdit::set_highlight_current_line(bool p_enabled) {
  5976. if (highlight_current_line == p_enabled) {
  5977. return;
  5978. }
  5979. highlight_current_line = p_enabled;
  5980. queue_redraw();
  5981. }
  5982. bool TextEdit::is_highlight_current_line_enabled() const {
  5983. return highlight_current_line;
  5984. }
  5985. void TextEdit::set_highlight_all_occurrences(bool p_enabled) {
  5986. if (highlight_all_occurrences == p_enabled) {
  5987. return;
  5988. }
  5989. highlight_all_occurrences = p_enabled;
  5990. queue_redraw();
  5991. }
  5992. bool TextEdit::is_highlight_all_occurrences_enabled() const {
  5993. return highlight_all_occurrences;
  5994. }
  5995. void TextEdit::set_use_default_word_separators(bool p_enabled) {
  5996. text.set_use_default_word_separators(p_enabled);
  5997. }
  5998. bool TextEdit::is_default_word_separators_enabled() const {
  5999. return text.is_default_word_separators_enabled();
  6000. }
  6001. // Set word separators. Combine default separators with custom separators if those options are enabled.
  6002. void TextEdit::set_custom_word_separators(const String &p_separators) {
  6003. text.set_custom_word_separators(p_separators);
  6004. }
  6005. void TextEdit::Text::set_custom_word_separators(const String &p_separators) {
  6006. if (custom_word_separators == p_separators) {
  6007. return;
  6008. }
  6009. custom_word_separators = p_separators;
  6010. invalidate_all_lines();
  6011. }
  6012. bool TextEdit::is_custom_word_separators_enabled() const {
  6013. return text.is_custom_word_separators_enabled();
  6014. }
  6015. String TextEdit::get_custom_word_separators() const {
  6016. return text.get_custom_word_separators();
  6017. }
  6018. // Enable or disable custom word separators.
  6019. void TextEdit::set_use_custom_word_separators(bool p_enabled) {
  6020. text.set_use_custom_word_separators(p_enabled);
  6021. }
  6022. String TextEdit::get_default_word_separators() const {
  6023. return text.get_default_word_separators();
  6024. }
  6025. void TextEdit::set_draw_control_chars(bool p_enabled) {
  6026. if (draw_control_chars != p_enabled) {
  6027. draw_control_chars = p_enabled;
  6028. if (menu) {
  6029. menu->set_item_checked(menu->get_item_index(MENU_DISPLAY_UCC), draw_control_chars);
  6030. }
  6031. text.set_draw_control_chars(draw_control_chars);
  6032. text.invalidate_font();
  6033. _update_placeholder();
  6034. queue_accessibility_update();
  6035. queue_redraw();
  6036. }
  6037. }
  6038. bool TextEdit::get_draw_control_chars() const {
  6039. return draw_control_chars;
  6040. }
  6041. void TextEdit::set_draw_tabs(bool p_enabled) {
  6042. if (draw_tabs == p_enabled) {
  6043. return;
  6044. }
  6045. draw_tabs = p_enabled;
  6046. queue_redraw();
  6047. }
  6048. bool TextEdit::is_drawing_tabs() const {
  6049. return draw_tabs;
  6050. }
  6051. void TextEdit::set_draw_spaces(bool p_enabled) {
  6052. if (draw_spaces == p_enabled) {
  6053. return;
  6054. }
  6055. draw_spaces = p_enabled;
  6056. queue_redraw();
  6057. }
  6058. bool TextEdit::is_drawing_spaces() const {
  6059. return draw_spaces;
  6060. }
  6061. Color TextEdit::get_font_color() const {
  6062. return theme_cache.font_color;
  6063. }
  6064. void TextEdit::_bind_methods() {
  6065. /* Text */
  6066. // Text properties
  6067. ClassDB::bind_method(D_METHOD("has_ime_text"), &TextEdit::has_ime_text);
  6068. ClassDB::bind_method(D_METHOD("cancel_ime"), &TextEdit::cancel_ime);
  6069. ClassDB::bind_method(D_METHOD("apply_ime"), &TextEdit::apply_ime);
  6070. ClassDB::bind_method(D_METHOD("set_editable", "enabled"), &TextEdit::set_editable);
  6071. ClassDB::bind_method(D_METHOD("is_editable"), &TextEdit::is_editable);
  6072. ClassDB::bind_method(D_METHOD("set_text_direction", "direction"), &TextEdit::set_text_direction);
  6073. ClassDB::bind_method(D_METHOD("get_text_direction"), &TextEdit::get_text_direction);
  6074. ClassDB::bind_method(D_METHOD("set_language", "language"), &TextEdit::set_language);
  6075. ClassDB::bind_method(D_METHOD("get_language"), &TextEdit::get_language);
  6076. ClassDB::bind_method(D_METHOD("set_structured_text_bidi_override", "parser"), &TextEdit::set_structured_text_bidi_override);
  6077. ClassDB::bind_method(D_METHOD("get_structured_text_bidi_override"), &TextEdit::get_structured_text_bidi_override);
  6078. ClassDB::bind_method(D_METHOD("set_structured_text_bidi_override_options", "args"), &TextEdit::set_structured_text_bidi_override_options);
  6079. ClassDB::bind_method(D_METHOD("get_structured_text_bidi_override_options"), &TextEdit::get_structured_text_bidi_override_options);
  6080. ClassDB::bind_method(D_METHOD("set_tab_size", "size"), &TextEdit::set_tab_size);
  6081. ClassDB::bind_method(D_METHOD("get_tab_size"), &TextEdit::get_tab_size);
  6082. ClassDB::bind_method(D_METHOD("set_indent_wrapped_lines", "enabled"), &TextEdit::set_indent_wrapped_lines);
  6083. ClassDB::bind_method(D_METHOD("is_indent_wrapped_lines"), &TextEdit::is_indent_wrapped_lines);
  6084. ClassDB::bind_method(D_METHOD("set_tab_input_mode", "enabled"), &TextEdit::set_tab_input_mode);
  6085. ClassDB::bind_method(D_METHOD("get_tab_input_mode"), &TextEdit::get_tab_input_mode);
  6086. // User controls
  6087. ClassDB::bind_method(D_METHOD("set_overtype_mode_enabled", "enabled"), &TextEdit::set_overtype_mode_enabled);
  6088. ClassDB::bind_method(D_METHOD("is_overtype_mode_enabled"), &TextEdit::is_overtype_mode_enabled);
  6089. ClassDB::bind_method(D_METHOD("set_context_menu_enabled", "enabled"), &TextEdit::set_context_menu_enabled);
  6090. ClassDB::bind_method(D_METHOD("is_context_menu_enabled"), &TextEdit::is_context_menu_enabled);
  6091. ClassDB::bind_method(D_METHOD("set_emoji_menu_enabled", "enable"), &TextEdit::set_emoji_menu_enabled);
  6092. ClassDB::bind_method(D_METHOD("is_emoji_menu_enabled"), &TextEdit::is_emoji_menu_enabled);
  6093. ClassDB::bind_method(D_METHOD("set_backspace_deletes_composite_character_enabled", "enable"), &TextEdit::set_backspace_deletes_composite_character_enabled);
  6094. ClassDB::bind_method(D_METHOD("is_backspace_deletes_composite_character_enabled"), &TextEdit::is_backspace_deletes_composite_character_enabled);
  6095. ClassDB::bind_method(D_METHOD("set_shortcut_keys_enabled", "enabled"), &TextEdit::set_shortcut_keys_enabled);
  6096. ClassDB::bind_method(D_METHOD("is_shortcut_keys_enabled"), &TextEdit::is_shortcut_keys_enabled);
  6097. ClassDB::bind_method(D_METHOD("set_virtual_keyboard_enabled", "enabled"), &TextEdit::set_virtual_keyboard_enabled);
  6098. ClassDB::bind_method(D_METHOD("is_virtual_keyboard_enabled"), &TextEdit::is_virtual_keyboard_enabled);
  6099. ClassDB::bind_method(D_METHOD("set_virtual_keyboard_show_on_focus", "show_on_focus"), &TextEdit::set_virtual_keyboard_show_on_focus);
  6100. ClassDB::bind_method(D_METHOD("get_virtual_keyboard_show_on_focus"), &TextEdit::get_virtual_keyboard_show_on_focus);
  6101. ClassDB::bind_method(D_METHOD("set_middle_mouse_paste_enabled", "enabled"), &TextEdit::set_middle_mouse_paste_enabled);
  6102. ClassDB::bind_method(D_METHOD("is_middle_mouse_paste_enabled"), &TextEdit::is_middle_mouse_paste_enabled);
  6103. ClassDB::bind_method(D_METHOD("set_empty_selection_clipboard_enabled", "enabled"), &TextEdit::set_empty_selection_clipboard_enabled);
  6104. ClassDB::bind_method(D_METHOD("is_empty_selection_clipboard_enabled"), &TextEdit::is_empty_selection_clipboard_enabled);
  6105. // Text manipulation
  6106. ClassDB::bind_method(D_METHOD("clear"), &TextEdit::clear);
  6107. ClassDB::bind_method(D_METHOD("set_text", "text"), &TextEdit::set_text);
  6108. ClassDB::bind_method(D_METHOD("get_text"), &TextEdit::get_text);
  6109. ClassDB::bind_method(D_METHOD("get_line_count"), &TextEdit::get_line_count);
  6110. ClassDB::bind_method(D_METHOD("set_placeholder", "text"), &TextEdit::set_placeholder);
  6111. ClassDB::bind_method(D_METHOD("get_placeholder"), &TextEdit::get_placeholder);
  6112. ClassDB::bind_method(D_METHOD("set_line", "line", "new_text"), &TextEdit::set_line);
  6113. ClassDB::bind_method(D_METHOD("get_line", "line"), &TextEdit::get_line);
  6114. ClassDB::bind_method(D_METHOD("get_line_with_ime", "line"), &TextEdit::get_line_with_ime);
  6115. ClassDB::bind_method(D_METHOD("get_line_width", "line", "wrap_index"), &TextEdit::get_line_width, DEFVAL(-1));
  6116. ClassDB::bind_method(D_METHOD("get_line_height"), &TextEdit::get_line_height);
  6117. ClassDB::bind_method(D_METHOD("get_indent_level", "line"), &TextEdit::get_indent_level);
  6118. ClassDB::bind_method(D_METHOD("get_first_non_whitespace_column", "line"), &TextEdit::get_first_non_whitespace_column);
  6119. ClassDB::bind_method(D_METHOD("swap_lines", "from_line", "to_line"), &TextEdit::swap_lines);
  6120. ClassDB::bind_method(D_METHOD("insert_line_at", "line", "text"), &TextEdit::insert_line_at);
  6121. ClassDB::bind_method(D_METHOD("remove_line_at", "line", "move_carets_down"), &TextEdit::remove_line_at, DEFVAL(true));
  6122. ClassDB::bind_method(D_METHOD("insert_text_at_caret", "text", "caret_index"), &TextEdit::insert_text_at_caret, DEFVAL(-1));
  6123. ClassDB::bind_method(D_METHOD("insert_text", "text", "line", "column", "before_selection_begin", "before_selection_end"), &TextEdit::insert_text, DEFVAL(true), DEFVAL(false));
  6124. ClassDB::bind_method(D_METHOD("remove_text", "from_line", "from_column", "to_line", "to_column"), &TextEdit::remove_text);
  6125. ClassDB::bind_method(D_METHOD("get_last_unhidden_line"), &TextEdit::get_last_unhidden_line);
  6126. ClassDB::bind_method(D_METHOD("get_next_visible_line_offset_from", "line", "visible_amount"), &TextEdit::get_next_visible_line_offset_from);
  6127. 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);
  6128. // Overridable actions
  6129. ClassDB::bind_method(D_METHOD("backspace", "caret_index"), &TextEdit::backspace, DEFVAL(-1));
  6130. ClassDB::bind_method(D_METHOD("cut", "caret_index"), &TextEdit::cut, DEFVAL(-1));
  6131. ClassDB::bind_method(D_METHOD("copy", "caret_index"), &TextEdit::copy, DEFVAL(-1));
  6132. ClassDB::bind_method(D_METHOD("paste", "caret_index"), &TextEdit::paste, DEFVAL(-1));
  6133. ClassDB::bind_method(D_METHOD("paste_primary_clipboard", "caret_index"), &TextEdit::paste_primary_clipboard, DEFVAL(-1));
  6134. GDVIRTUAL_BIND(_handle_unicode_input, "unicode_char", "caret_index")
  6135. GDVIRTUAL_BIND(_backspace, "caret_index")
  6136. GDVIRTUAL_BIND(_cut, "caret_index")
  6137. GDVIRTUAL_BIND(_copy, "caret_index")
  6138. GDVIRTUAL_BIND(_paste, "caret_index")
  6139. GDVIRTUAL_BIND(_paste_primary_clipboard, "caret_index")
  6140. // Context Menu
  6141. BIND_ENUM_CONSTANT(MENU_CUT);
  6142. BIND_ENUM_CONSTANT(MENU_COPY);
  6143. BIND_ENUM_CONSTANT(MENU_PASTE);
  6144. BIND_ENUM_CONSTANT(MENU_CLEAR);
  6145. BIND_ENUM_CONSTANT(MENU_SELECT_ALL);
  6146. BIND_ENUM_CONSTANT(MENU_UNDO);
  6147. BIND_ENUM_CONSTANT(MENU_REDO);
  6148. BIND_ENUM_CONSTANT(MENU_SUBMENU_TEXT_DIR);
  6149. BIND_ENUM_CONSTANT(MENU_DIR_INHERITED);
  6150. BIND_ENUM_CONSTANT(MENU_DIR_AUTO);
  6151. BIND_ENUM_CONSTANT(MENU_DIR_LTR);
  6152. BIND_ENUM_CONSTANT(MENU_DIR_RTL);
  6153. BIND_ENUM_CONSTANT(MENU_DISPLAY_UCC);
  6154. BIND_ENUM_CONSTANT(MENU_SUBMENU_INSERT_UCC);
  6155. BIND_ENUM_CONSTANT(MENU_INSERT_LRM);
  6156. BIND_ENUM_CONSTANT(MENU_INSERT_RLM);
  6157. BIND_ENUM_CONSTANT(MENU_INSERT_LRE);
  6158. BIND_ENUM_CONSTANT(MENU_INSERT_RLE);
  6159. BIND_ENUM_CONSTANT(MENU_INSERT_LRO);
  6160. BIND_ENUM_CONSTANT(MENU_INSERT_RLO);
  6161. BIND_ENUM_CONSTANT(MENU_INSERT_PDF);
  6162. BIND_ENUM_CONSTANT(MENU_INSERT_ALM);
  6163. BIND_ENUM_CONSTANT(MENU_INSERT_LRI);
  6164. BIND_ENUM_CONSTANT(MENU_INSERT_RLI);
  6165. BIND_ENUM_CONSTANT(MENU_INSERT_FSI);
  6166. BIND_ENUM_CONSTANT(MENU_INSERT_PDI);
  6167. BIND_ENUM_CONSTANT(MENU_INSERT_ZWJ);
  6168. BIND_ENUM_CONSTANT(MENU_INSERT_ZWNJ);
  6169. BIND_ENUM_CONSTANT(MENU_INSERT_WJ);
  6170. BIND_ENUM_CONSTANT(MENU_INSERT_SHY);
  6171. BIND_ENUM_CONSTANT(MENU_EMOJI_AND_SYMBOL);
  6172. BIND_ENUM_CONSTANT(MENU_MAX);
  6173. /* Versioning */
  6174. BIND_ENUM_CONSTANT(ACTION_NONE);
  6175. BIND_ENUM_CONSTANT(ACTION_TYPING);
  6176. BIND_ENUM_CONSTANT(ACTION_BACKSPACE);
  6177. BIND_ENUM_CONSTANT(ACTION_DELETE);
  6178. ClassDB::bind_method(D_METHOD("start_action", "action"), &TextEdit::start_action);
  6179. ClassDB::bind_method(D_METHOD("end_action"), &TextEdit::end_complex_operation);
  6180. ClassDB::bind_method(D_METHOD("begin_complex_operation"), &TextEdit::begin_complex_operation);
  6181. ClassDB::bind_method(D_METHOD("end_complex_operation"), &TextEdit::end_complex_operation);
  6182. ClassDB::bind_method(D_METHOD("has_undo"), &TextEdit::has_undo);
  6183. ClassDB::bind_method(D_METHOD("has_redo"), &TextEdit::has_redo);
  6184. ClassDB::bind_method(D_METHOD("undo"), &TextEdit::undo);
  6185. ClassDB::bind_method(D_METHOD("redo"), &TextEdit::redo);
  6186. ClassDB::bind_method(D_METHOD("clear_undo_history"), &TextEdit::clear_undo_history);
  6187. ClassDB::bind_method(D_METHOD("tag_saved_version"), &TextEdit::tag_saved_version);
  6188. ClassDB::bind_method(D_METHOD("get_version"), &TextEdit::get_version);
  6189. ClassDB::bind_method(D_METHOD("get_saved_version"), &TextEdit::get_saved_version);
  6190. /* Search */
  6191. BIND_ENUM_CONSTANT(SEARCH_MATCH_CASE);
  6192. BIND_ENUM_CONSTANT(SEARCH_WHOLE_WORDS);
  6193. BIND_ENUM_CONSTANT(SEARCH_BACKWARDS);
  6194. ClassDB::bind_method(D_METHOD("set_search_text", "search_text"), &TextEdit::set_search_text);
  6195. ClassDB::bind_method(D_METHOD("set_search_flags", "flags"), &TextEdit::set_search_flags);
  6196. ClassDB::bind_method(D_METHOD("search", "text", "flags", "from_line", "from_column"), &TextEdit::search);
  6197. /* Tooltip */
  6198. ClassDB::bind_method(D_METHOD("set_tooltip_request_func", "callback"), &TextEdit::set_tooltip_request_func);
  6199. /* Mouse */
  6200. ClassDB::bind_method(D_METHOD("get_local_mouse_pos"), &TextEdit::get_local_mouse_pos);
  6201. ClassDB::bind_method(D_METHOD("get_word_at_pos", "position"), &TextEdit::get_word_at_pos);
  6202. ClassDB::bind_method(D_METHOD("get_line_column_at_pos", "position", "clamp_line", "clamp_column"), &TextEdit::get_line_column_at_pos, DEFVAL(true), DEFVAL(true));
  6203. ClassDB::bind_method(D_METHOD("get_pos_at_line_column", "line", "column"), &TextEdit::get_pos_at_line_column);
  6204. ClassDB::bind_method(D_METHOD("get_rect_at_line_column", "line", "column"), &TextEdit::get_rect_at_line_column);
  6205. ClassDB::bind_method(D_METHOD("get_minimap_line_at_pos", "position"), &TextEdit::get_minimap_line_at_pos);
  6206. ClassDB::bind_method(D_METHOD("is_dragging_cursor"), &TextEdit::is_dragging_cursor);
  6207. ClassDB::bind_method(D_METHOD("is_mouse_over_selection", "edges", "caret_index"), &TextEdit::is_mouse_over_selection, DEFVAL(-1));
  6208. /* Caret. */
  6209. BIND_ENUM_CONSTANT(CARET_TYPE_LINE);
  6210. BIND_ENUM_CONSTANT(CARET_TYPE_BLOCK);
  6211. ClassDB::bind_method(D_METHOD("set_caret_type", "type"), &TextEdit::set_caret_type);
  6212. ClassDB::bind_method(D_METHOD("get_caret_type"), &TextEdit::get_caret_type);
  6213. ClassDB::bind_method(D_METHOD("set_caret_blink_enabled", "enable"), &TextEdit::set_caret_blink_enabled);
  6214. ClassDB::bind_method(D_METHOD("is_caret_blink_enabled"), &TextEdit::is_caret_blink_enabled);
  6215. ClassDB::bind_method(D_METHOD("set_caret_blink_interval", "interval"), &TextEdit::set_caret_blink_interval);
  6216. ClassDB::bind_method(D_METHOD("get_caret_blink_interval"), &TextEdit::get_caret_blink_interval);
  6217. ClassDB::bind_method(D_METHOD("set_draw_caret_when_editable_disabled", "enable"), &TextEdit::set_draw_caret_when_editable_disabled);
  6218. ClassDB::bind_method(D_METHOD("is_drawing_caret_when_editable_disabled"), &TextEdit::is_drawing_caret_when_editable_disabled);
  6219. ClassDB::bind_method(D_METHOD("set_move_caret_on_right_click_enabled", "enable"), &TextEdit::set_move_caret_on_right_click_enabled);
  6220. ClassDB::bind_method(D_METHOD("is_move_caret_on_right_click_enabled"), &TextEdit::is_move_caret_on_right_click_enabled);
  6221. ClassDB::bind_method(D_METHOD("set_caret_mid_grapheme_enabled", "enabled"), &TextEdit::set_caret_mid_grapheme_enabled);
  6222. ClassDB::bind_method(D_METHOD("is_caret_mid_grapheme_enabled"), &TextEdit::is_caret_mid_grapheme_enabled);
  6223. ClassDB::bind_method(D_METHOD("set_multiple_carets_enabled", "enabled"), &TextEdit::set_multiple_carets_enabled);
  6224. ClassDB::bind_method(D_METHOD("is_multiple_carets_enabled"), &TextEdit::is_multiple_carets_enabled);
  6225. ClassDB::bind_method(D_METHOD("add_caret", "line", "column"), &TextEdit::add_caret);
  6226. ClassDB::bind_method(D_METHOD("remove_caret", "caret"), &TextEdit::remove_caret);
  6227. ClassDB::bind_method(D_METHOD("remove_secondary_carets"), &TextEdit::remove_secondary_carets);
  6228. ClassDB::bind_method(D_METHOD("get_caret_count"), &TextEdit::get_caret_count);
  6229. ClassDB::bind_method(D_METHOD("add_caret_at_carets", "below"), &TextEdit::add_caret_at_carets);
  6230. ClassDB::bind_method(D_METHOD("get_sorted_carets", "include_ignored_carets"), &TextEdit::get_sorted_carets, DEFVAL(false));
  6231. ClassDB::bind_method(D_METHOD("collapse_carets", "from_line", "from_column", "to_line", "to_column", "inclusive"), &TextEdit::collapse_carets, DEFVAL(false));
  6232. ClassDB::bind_method(D_METHOD("merge_overlapping_carets"), &TextEdit::merge_overlapping_carets);
  6233. ClassDB::bind_method(D_METHOD("begin_multicaret_edit"), &TextEdit::begin_multicaret_edit);
  6234. ClassDB::bind_method(D_METHOD("end_multicaret_edit"), &TextEdit::end_multicaret_edit);
  6235. ClassDB::bind_method(D_METHOD("is_in_mulitcaret_edit"), &TextEdit::is_in_mulitcaret_edit);
  6236. ClassDB::bind_method(D_METHOD("multicaret_edit_ignore_caret", "caret_index"), &TextEdit::multicaret_edit_ignore_caret);
  6237. ClassDB::bind_method(D_METHOD("is_caret_visible", "caret_index"), &TextEdit::is_caret_visible, DEFVAL(0));
  6238. ClassDB::bind_method(D_METHOD("get_caret_draw_pos", "caret_index"), &TextEdit::get_caret_draw_pos, DEFVAL(0));
  6239. 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));
  6240. ClassDB::bind_method(D_METHOD("get_caret_line", "caret_index"), &TextEdit::get_caret_line, DEFVAL(0));
  6241. ClassDB::bind_method(D_METHOD("set_caret_column", "column", "adjust_viewport", "caret_index"), &TextEdit::set_caret_column, DEFVAL(true), DEFVAL(0));
  6242. ClassDB::bind_method(D_METHOD("get_caret_column", "caret_index"), &TextEdit::get_caret_column, DEFVAL(0));
  6243. ClassDB::bind_method(D_METHOD("get_next_composite_character_column", "line", "column"), &TextEdit::get_next_composite_character_column);
  6244. ClassDB::bind_method(D_METHOD("get_previous_composite_character_column", "line", "column"), &TextEdit::get_previous_composite_character_column);
  6245. ClassDB::bind_method(D_METHOD("get_caret_wrap_index", "caret_index"), &TextEdit::get_caret_wrap_index, DEFVAL(0));
  6246. ClassDB::bind_method(D_METHOD("get_word_under_caret", "caret_index"), &TextEdit::get_word_under_caret, DEFVAL(-1));
  6247. ClassDB::bind_method(D_METHOD("set_use_default_word_separators", "enabled"), &TextEdit::set_use_default_word_separators);
  6248. ClassDB::bind_method(D_METHOD("is_default_word_separators_enabled"), &TextEdit::is_default_word_separators_enabled);
  6249. ClassDB::bind_method(D_METHOD("set_use_custom_word_separators", "enabled"), &TextEdit::set_use_custom_word_separators);
  6250. ClassDB::bind_method(D_METHOD("is_custom_word_separators_enabled"), &TextEdit::is_custom_word_separators_enabled);
  6251. ClassDB::bind_method(D_METHOD("set_custom_word_separators", "custom_word_separators"), &TextEdit::set_custom_word_separators);
  6252. ClassDB::bind_method(D_METHOD("get_custom_word_separators"), &TextEdit::get_custom_word_separators);
  6253. /* Selection. */
  6254. BIND_ENUM_CONSTANT(SELECTION_MODE_NONE);
  6255. BIND_ENUM_CONSTANT(SELECTION_MODE_SHIFT);
  6256. BIND_ENUM_CONSTANT(SELECTION_MODE_POINTER);
  6257. BIND_ENUM_CONSTANT(SELECTION_MODE_WORD);
  6258. BIND_ENUM_CONSTANT(SELECTION_MODE_LINE);
  6259. ClassDB::bind_method(D_METHOD("set_selecting_enabled", "enable"), &TextEdit::set_selecting_enabled);
  6260. ClassDB::bind_method(D_METHOD("is_selecting_enabled"), &TextEdit::is_selecting_enabled);
  6261. ClassDB::bind_method(D_METHOD("set_deselect_on_focus_loss_enabled", "enable"), &TextEdit::set_deselect_on_focus_loss_enabled);
  6262. ClassDB::bind_method(D_METHOD("is_deselect_on_focus_loss_enabled"), &TextEdit::is_deselect_on_focus_loss_enabled);
  6263. ClassDB::bind_method(D_METHOD("set_drag_and_drop_selection_enabled", "enable"), &TextEdit::set_drag_and_drop_selection_enabled);
  6264. ClassDB::bind_method(D_METHOD("is_drag_and_drop_selection_enabled"), &TextEdit::is_drag_and_drop_selection_enabled);
  6265. ClassDB::bind_method(D_METHOD("set_selection_mode", "mode"), &TextEdit::set_selection_mode);
  6266. ClassDB::bind_method(D_METHOD("get_selection_mode"), &TextEdit::get_selection_mode);
  6267. ClassDB::bind_method(D_METHOD("select_all"), &TextEdit::select_all);
  6268. ClassDB::bind_method(D_METHOD("select_word_under_caret", "caret_index"), &TextEdit::select_word_under_caret, DEFVAL(-1));
  6269. ClassDB::bind_method(D_METHOD("add_selection_for_next_occurrence"), &TextEdit::add_selection_for_next_occurrence);
  6270. ClassDB::bind_method(D_METHOD("skip_selection_for_next_occurrence"), &TextEdit::skip_selection_for_next_occurrence);
  6271. ClassDB::bind_method(D_METHOD("select", "origin_line", "origin_column", "caret_line", "caret_column", "caret_index"), &TextEdit::select, DEFVAL(0));
  6272. ClassDB::bind_method(D_METHOD("has_selection", "caret_index"), &TextEdit::has_selection, DEFVAL(-1));
  6273. ClassDB::bind_method(D_METHOD("get_selected_text", "caret_index"), &TextEdit::get_selected_text, DEFVAL(-1));
  6274. ClassDB::bind_method(D_METHOD("get_selection_at_line_column", "line", "column", "include_edges", "only_selections"), &TextEdit::get_selection_at_line_column, DEFVAL(true), DEFVAL(true));
  6275. ClassDB::bind_method(D_METHOD("get_line_ranges_from_carets", "only_selections", "merge_adjacent"), &TextEdit::get_line_ranges_from_carets_typed_array, DEFVAL(false), DEFVAL(true));
  6276. ClassDB::bind_method(D_METHOD("get_selection_origin_line", "caret_index"), &TextEdit::get_selection_origin_line, DEFVAL(0));
  6277. ClassDB::bind_method(D_METHOD("get_selection_origin_column", "caret_index"), &TextEdit::get_selection_origin_column, DEFVAL(0));
  6278. ClassDB::bind_method(D_METHOD("set_selection_origin_line", "line", "can_be_hidden", "wrap_index", "caret_index"), &TextEdit::set_selection_origin_line, DEFVAL(true), DEFVAL(-1), DEFVAL(0));
  6279. ClassDB::bind_method(D_METHOD("set_selection_origin_column", "column", "caret_index"), &TextEdit::set_selection_origin_column, DEFVAL(0));
  6280. ClassDB::bind_method(D_METHOD("get_selection_from_line", "caret_index"), &TextEdit::get_selection_from_line, DEFVAL(0));
  6281. ClassDB::bind_method(D_METHOD("get_selection_from_column", "caret_index"), &TextEdit::get_selection_from_column, DEFVAL(0));
  6282. ClassDB::bind_method(D_METHOD("get_selection_to_line", "caret_index"), &TextEdit::get_selection_to_line, DEFVAL(0));
  6283. ClassDB::bind_method(D_METHOD("get_selection_to_column", "caret_index"), &TextEdit::get_selection_to_column, DEFVAL(0));
  6284. ClassDB::bind_method(D_METHOD("is_caret_after_selection_origin", "caret_index"), &TextEdit::is_caret_after_selection_origin, DEFVAL(0));
  6285. ClassDB::bind_method(D_METHOD("deselect", "caret_index"), &TextEdit::deselect, DEFVAL(-1));
  6286. ClassDB::bind_method(D_METHOD("delete_selection", "caret_index"), &TextEdit::delete_selection, DEFVAL(-1));
  6287. /* Line wrapping. */
  6288. BIND_ENUM_CONSTANT(LINE_WRAPPING_NONE);
  6289. BIND_ENUM_CONSTANT(LINE_WRAPPING_BOUNDARY);
  6290. ClassDB::bind_method(D_METHOD("set_line_wrapping_mode", "mode"), &TextEdit::set_line_wrapping_mode);
  6291. ClassDB::bind_method(D_METHOD("get_line_wrapping_mode"), &TextEdit::get_line_wrapping_mode);
  6292. ClassDB::bind_method(D_METHOD("set_autowrap_mode", "autowrap_mode"), &TextEdit::set_autowrap_mode);
  6293. ClassDB::bind_method(D_METHOD("get_autowrap_mode"), &TextEdit::get_autowrap_mode);
  6294. ClassDB::bind_method(D_METHOD("is_line_wrapped", "line"), &TextEdit::is_line_wrapped);
  6295. ClassDB::bind_method(D_METHOD("get_line_wrap_count", "line"), &TextEdit::get_line_wrap_count);
  6296. ClassDB::bind_method(D_METHOD("get_line_wrap_index_at_column", "line", "column"), &TextEdit::get_line_wrap_index_at_column);
  6297. ClassDB::bind_method(D_METHOD("get_line_wrapped_text", "line"), &TextEdit::get_line_wrapped_text);
  6298. /* Viewport. */
  6299. // Scrolling.
  6300. ClassDB::bind_method(D_METHOD("set_smooth_scroll_enabled", "enable"), &TextEdit::set_smooth_scroll_enabled);
  6301. ClassDB::bind_method(D_METHOD("is_smooth_scroll_enabled"), &TextEdit::is_smooth_scroll_enabled);
  6302. ClassDB::bind_method(D_METHOD("get_v_scroll_bar"), &TextEdit::get_v_scroll_bar);
  6303. ClassDB::bind_method(D_METHOD("get_h_scroll_bar"), &TextEdit::get_h_scroll_bar);
  6304. ClassDB::bind_method(D_METHOD("set_v_scroll", "value"), &TextEdit::set_v_scroll);
  6305. ClassDB::bind_method(D_METHOD("get_v_scroll"), &TextEdit::get_v_scroll);
  6306. ClassDB::bind_method(D_METHOD("set_h_scroll", "value"), &TextEdit::set_h_scroll);
  6307. ClassDB::bind_method(D_METHOD("get_h_scroll"), &TextEdit::get_h_scroll);
  6308. ClassDB::bind_method(D_METHOD("set_scroll_past_end_of_file_enabled", "enable"), &TextEdit::set_scroll_past_end_of_file_enabled);
  6309. ClassDB::bind_method(D_METHOD("is_scroll_past_end_of_file_enabled"), &TextEdit::is_scroll_past_end_of_file_enabled);
  6310. ClassDB::bind_method(D_METHOD("set_v_scroll_speed", "speed"), &TextEdit::set_v_scroll_speed);
  6311. ClassDB::bind_method(D_METHOD("get_v_scroll_speed"), &TextEdit::get_v_scroll_speed);
  6312. ClassDB::bind_method(D_METHOD("set_fit_content_height_enabled", "enabled"), &TextEdit::set_fit_content_height_enabled);
  6313. ClassDB::bind_method(D_METHOD("is_fit_content_height_enabled"), &TextEdit::is_fit_content_height_enabled);
  6314. ClassDB::bind_method(D_METHOD("set_fit_content_width_enabled", "enabled"), &TextEdit::set_fit_content_width_enabled);
  6315. ClassDB::bind_method(D_METHOD("is_fit_content_width_enabled"), &TextEdit::is_fit_content_width_enabled);
  6316. ClassDB::bind_method(D_METHOD("get_scroll_pos_for_line", "line", "wrap_index"), &TextEdit::get_scroll_pos_for_line, DEFVAL(0));
  6317. // Visible lines.
  6318. ClassDB::bind_method(D_METHOD("set_line_as_first_visible", "line", "wrap_index"), &TextEdit::set_line_as_first_visible, DEFVAL(0));
  6319. ClassDB::bind_method(D_METHOD("get_first_visible_line"), &TextEdit::get_first_visible_line);
  6320. ClassDB::bind_method(D_METHOD("set_line_as_center_visible", "line", "wrap_index"), &TextEdit::set_line_as_center_visible, DEFVAL(0));
  6321. ClassDB::bind_method(D_METHOD("set_line_as_last_visible", "line", "wrap_index"), &TextEdit::set_line_as_last_visible, DEFVAL(0));
  6322. ClassDB::bind_method(D_METHOD("get_last_full_visible_line"), &TextEdit::get_last_full_visible_line);
  6323. ClassDB::bind_method(D_METHOD("get_last_full_visible_line_wrap_index"), &TextEdit::get_last_full_visible_line_wrap_index);
  6324. ClassDB::bind_method(D_METHOD("get_visible_line_count"), &TextEdit::get_visible_line_count);
  6325. ClassDB::bind_method(D_METHOD("get_visible_line_count_in_range", "from_line", "to_line"), &TextEdit::get_visible_line_count_in_range);
  6326. ClassDB::bind_method(D_METHOD("get_total_visible_line_count"), &TextEdit::get_total_visible_line_count);
  6327. // Auto adjust
  6328. ClassDB::bind_method(D_METHOD("adjust_viewport_to_caret", "caret_index"), &TextEdit::adjust_viewport_to_caret, DEFVAL(0));
  6329. ClassDB::bind_method(D_METHOD("center_viewport_to_caret", "caret_index"), &TextEdit::center_viewport_to_caret, DEFVAL(0));
  6330. // Minimap
  6331. ClassDB::bind_method(D_METHOD("set_draw_minimap", "enabled"), &TextEdit::set_draw_minimap);
  6332. ClassDB::bind_method(D_METHOD("is_drawing_minimap"), &TextEdit::is_drawing_minimap);
  6333. ClassDB::bind_method(D_METHOD("set_minimap_width", "width"), &TextEdit::set_minimap_width);
  6334. ClassDB::bind_method(D_METHOD("get_minimap_width"), &TextEdit::get_minimap_width);
  6335. ClassDB::bind_method(D_METHOD("get_minimap_visible_lines"), &TextEdit::get_minimap_visible_lines);
  6336. /* Gutters. */
  6337. BIND_ENUM_CONSTANT(GUTTER_TYPE_STRING);
  6338. BIND_ENUM_CONSTANT(GUTTER_TYPE_ICON);
  6339. BIND_ENUM_CONSTANT(GUTTER_TYPE_CUSTOM);
  6340. ClassDB::bind_method(D_METHOD("add_gutter", "at"), &TextEdit::add_gutter, DEFVAL(-1));
  6341. ClassDB::bind_method(D_METHOD("remove_gutter", "gutter"), &TextEdit::remove_gutter);
  6342. ClassDB::bind_method(D_METHOD("get_gutter_count"), &TextEdit::get_gutter_count);
  6343. ClassDB::bind_method(D_METHOD("set_gutter_name", "gutter", "name"), &TextEdit::set_gutter_name);
  6344. ClassDB::bind_method(D_METHOD("get_gutter_name", "gutter"), &TextEdit::get_gutter_name);
  6345. ClassDB::bind_method(D_METHOD("set_gutter_type", "gutter", "type"), &TextEdit::set_gutter_type);
  6346. ClassDB::bind_method(D_METHOD("get_gutter_type", "gutter"), &TextEdit::get_gutter_type);
  6347. ClassDB::bind_method(D_METHOD("set_gutter_width", "gutter", "width"), &TextEdit::set_gutter_width);
  6348. ClassDB::bind_method(D_METHOD("get_gutter_width", "gutter"), &TextEdit::get_gutter_width);
  6349. ClassDB::bind_method(D_METHOD("set_gutter_draw", "gutter", "draw"), &TextEdit::set_gutter_draw);
  6350. ClassDB::bind_method(D_METHOD("is_gutter_drawn", "gutter"), &TextEdit::is_gutter_drawn);
  6351. ClassDB::bind_method(D_METHOD("set_gutter_clickable", "gutter", "clickable"), &TextEdit::set_gutter_clickable);
  6352. ClassDB::bind_method(D_METHOD("is_gutter_clickable", "gutter"), &TextEdit::is_gutter_clickable);
  6353. ClassDB::bind_method(D_METHOD("set_gutter_overwritable", "gutter", "overwritable"), &TextEdit::set_gutter_overwritable);
  6354. ClassDB::bind_method(D_METHOD("is_gutter_overwritable", "gutter"), &TextEdit::is_gutter_overwritable);
  6355. ClassDB::bind_method(D_METHOD("merge_gutters", "from_line", "to_line"), &TextEdit::merge_gutters);
  6356. ClassDB::bind_method(D_METHOD("set_gutter_custom_draw", "column", "draw_callback"), &TextEdit::set_gutter_custom_draw);
  6357. ClassDB::bind_method(D_METHOD("get_total_gutter_width"), &TextEdit::get_total_gutter_width);
  6358. // Line gutters.
  6359. ClassDB::bind_method(D_METHOD("set_line_gutter_metadata", "line", "gutter", "metadata"), &TextEdit::set_line_gutter_metadata);
  6360. ClassDB::bind_method(D_METHOD("get_line_gutter_metadata", "line", "gutter"), &TextEdit::get_line_gutter_metadata);
  6361. ClassDB::bind_method(D_METHOD("set_line_gutter_text", "line", "gutter", "text"), &TextEdit::set_line_gutter_text);
  6362. ClassDB::bind_method(D_METHOD("get_line_gutter_text", "line", "gutter"), &TextEdit::get_line_gutter_text);
  6363. ClassDB::bind_method(D_METHOD("set_line_gutter_icon", "line", "gutter", "icon"), &TextEdit::set_line_gutter_icon);
  6364. ClassDB::bind_method(D_METHOD("get_line_gutter_icon", "line", "gutter"), &TextEdit::get_line_gutter_icon);
  6365. ClassDB::bind_method(D_METHOD("set_line_gutter_item_color", "line", "gutter", "color"), &TextEdit::set_line_gutter_item_color);
  6366. ClassDB::bind_method(D_METHOD("get_line_gutter_item_color", "line", "gutter"), &TextEdit::get_line_gutter_item_color);
  6367. ClassDB::bind_method(D_METHOD("set_line_gutter_clickable", "line", "gutter", "clickable"), &TextEdit::set_line_gutter_clickable);
  6368. ClassDB::bind_method(D_METHOD("is_line_gutter_clickable", "line", "gutter"), &TextEdit::is_line_gutter_clickable);
  6369. // Line style
  6370. ClassDB::bind_method(D_METHOD("set_line_background_color", "line", "color"), &TextEdit::set_line_background_color);
  6371. ClassDB::bind_method(D_METHOD("get_line_background_color", "line"), &TextEdit::get_line_background_color);
  6372. /* Syntax Highlighting. */
  6373. ClassDB::bind_method(D_METHOD("set_syntax_highlighter", "syntax_highlighter"), &TextEdit::set_syntax_highlighter);
  6374. ClassDB::bind_method(D_METHOD("get_syntax_highlighter"), &TextEdit::get_syntax_highlighter);
  6375. /* Visual. */
  6376. ClassDB::bind_method(D_METHOD("set_highlight_current_line", "enabled"), &TextEdit::set_highlight_current_line);
  6377. ClassDB::bind_method(D_METHOD("is_highlight_current_line_enabled"), &TextEdit::is_highlight_current_line_enabled);
  6378. ClassDB::bind_method(D_METHOD("set_highlight_all_occurrences", "enabled"), &TextEdit::set_highlight_all_occurrences);
  6379. ClassDB::bind_method(D_METHOD("is_highlight_all_occurrences_enabled"), &TextEdit::is_highlight_all_occurrences_enabled);
  6380. ClassDB::bind_method(D_METHOD("get_draw_control_chars"), &TextEdit::get_draw_control_chars);
  6381. ClassDB::bind_method(D_METHOD("set_draw_control_chars", "enabled"), &TextEdit::set_draw_control_chars);
  6382. ClassDB::bind_method(D_METHOD("set_draw_tabs", "enabled"), &TextEdit::set_draw_tabs);
  6383. ClassDB::bind_method(D_METHOD("is_drawing_tabs"), &TextEdit::is_drawing_tabs);
  6384. ClassDB::bind_method(D_METHOD("set_draw_spaces", "enabled"), &TextEdit::set_draw_spaces);
  6385. ClassDB::bind_method(D_METHOD("is_drawing_spaces"), &TextEdit::is_drawing_spaces);
  6386. ClassDB::bind_method(D_METHOD("get_menu"), &TextEdit::get_menu);
  6387. ClassDB::bind_method(D_METHOD("is_menu_visible"), &TextEdit::is_menu_visible);
  6388. ClassDB::bind_method(D_METHOD("menu_option", "option"), &TextEdit::menu_option);
  6389. /* Deprecated */
  6390. #ifndef DISABLE_DEPRECATED
  6391. ClassDB::bind_method(D_METHOD("adjust_carets_after_edit", "caret", "from_line", "from_col", "to_line", "to_col"), &TextEdit::adjust_carets_after_edit);
  6392. ClassDB::bind_method(D_METHOD("get_caret_index_edit_order"), &TextEdit::get_caret_index_edit_order);
  6393. ClassDB::bind_method(D_METHOD("get_selection_line", "caret_index"), &TextEdit::get_selection_line, DEFVAL(0));
  6394. ClassDB::bind_method(D_METHOD("get_selection_column", "caret_index"), &TextEdit::get_selection_column, DEFVAL(0));
  6395. #endif
  6396. /* Inspector */
  6397. ADD_PROPERTY(PropertyInfo(Variant::STRING, "text", PROPERTY_HINT_MULTILINE_TEXT), "set_text", "get_text");
  6398. ADD_PROPERTY(PropertyInfo(Variant::STRING, "placeholder_text", PROPERTY_HINT_MULTILINE_TEXT), "set_placeholder", "get_placeholder");
  6399. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "editable"), "set_editable", "is_editable");
  6400. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "context_menu_enabled"), "set_context_menu_enabled", "is_context_menu_enabled");
  6401. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "emoji_menu_enabled"), "set_emoji_menu_enabled", "is_emoji_menu_enabled");
  6402. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "backspace_deletes_composite_character_enabled"), "set_backspace_deletes_composite_character_enabled", "is_backspace_deletes_composite_character_enabled");
  6403. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "shortcut_keys_enabled"), "set_shortcut_keys_enabled", "is_shortcut_keys_enabled");
  6404. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "selecting_enabled"), "set_selecting_enabled", "is_selecting_enabled");
  6405. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "deselect_on_focus_loss_enabled"), "set_deselect_on_focus_loss_enabled", "is_deselect_on_focus_loss_enabled");
  6406. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "drag_and_drop_selection_enabled"), "set_drag_and_drop_selection_enabled", "is_drag_and_drop_selection_enabled");
  6407. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "virtual_keyboard_enabled"), "set_virtual_keyboard_enabled", "is_virtual_keyboard_enabled");
  6408. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "virtual_keyboard_show_on_focus"), "set_virtual_keyboard_show_on_focus", "get_virtual_keyboard_show_on_focus");
  6409. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "middle_mouse_paste_enabled"), "set_middle_mouse_paste_enabled", "is_middle_mouse_paste_enabled");
  6410. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "empty_selection_clipboard_enabled"), "set_empty_selection_clipboard_enabled", "is_empty_selection_clipboard_enabled");
  6411. ADD_PROPERTY(PropertyInfo(Variant::INT, "wrap_mode", PROPERTY_HINT_ENUM, "None,Boundary"), "set_line_wrapping_mode", "get_line_wrapping_mode");
  6412. ADD_PROPERTY(PropertyInfo(Variant::INT, "autowrap_mode", PROPERTY_HINT_ENUM, "Arbitrary:1,Word:2,Word (Smart):3"), "set_autowrap_mode", "get_autowrap_mode");
  6413. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "indent_wrapped_lines"), "set_indent_wrapped_lines", "is_indent_wrapped_lines");
  6414. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "tab_input_mode"), "set_tab_input_mode", "get_tab_input_mode");
  6415. ADD_GROUP("Scroll", "scroll_");
  6416. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "scroll_smooth"), "set_smooth_scroll_enabled", "is_smooth_scroll_enabled");
  6417. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "scroll_v_scroll_speed", PROPERTY_HINT_NONE, "suffix:lines/s"), "set_v_scroll_speed", "get_v_scroll_speed");
  6418. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "scroll_past_end_of_file"), "set_scroll_past_end_of_file_enabled", "is_scroll_past_end_of_file_enabled");
  6419. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "scroll_vertical", PROPERTY_HINT_NONE, "suffix:lines"), "set_v_scroll", "get_v_scroll");
  6420. ADD_PROPERTY(PropertyInfo(Variant::INT, "scroll_horizontal", PROPERTY_HINT_NONE, "suffix:px"), "set_h_scroll", "get_h_scroll");
  6421. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "scroll_fit_content_height"), "set_fit_content_height_enabled", "is_fit_content_height_enabled");
  6422. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "scroll_fit_content_width"), "set_fit_content_width_enabled", "is_fit_content_width_enabled");
  6423. ADD_GROUP("Minimap", "minimap_");
  6424. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "minimap_draw"), "set_draw_minimap", "is_drawing_minimap");
  6425. ADD_PROPERTY(PropertyInfo(Variant::INT, "minimap_width", PROPERTY_HINT_NONE, "suffix:px"), "set_minimap_width", "get_minimap_width");
  6426. ADD_GROUP("Caret", "caret_");
  6427. ADD_PROPERTY(PropertyInfo(Variant::INT, "caret_type", PROPERTY_HINT_ENUM, "Line,Block"), "set_caret_type", "get_caret_type");
  6428. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "caret_blink"), "set_caret_blink_enabled", "is_caret_blink_enabled");
  6429. 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");
  6430. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "caret_draw_when_editable_disabled"), "set_draw_caret_when_editable_disabled", "is_drawing_caret_when_editable_disabled");
  6431. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "caret_move_on_right_click"), "set_move_caret_on_right_click_enabled", "is_move_caret_on_right_click_enabled");
  6432. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "caret_mid_grapheme"), "set_caret_mid_grapheme_enabled", "is_caret_mid_grapheme_enabled");
  6433. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "caret_multiple"), "set_multiple_carets_enabled", "is_multiple_carets_enabled");
  6434. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "use_default_word_separators"), "set_use_default_word_separators", "is_default_word_separators_enabled");
  6435. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "use_custom_word_separators"), "set_use_custom_word_separators", "is_custom_word_separators_enabled");
  6436. ADD_PROPERTY(PropertyInfo(Variant::STRING, "custom_word_separators"), "set_custom_word_separators", "get_custom_word_separators");
  6437. ADD_GROUP("Highlighting", "");
  6438. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "syntax_highlighter", PROPERTY_HINT_RESOURCE_TYPE, "SyntaxHighlighter", PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_ALWAYS_DUPLICATE), "set_syntax_highlighter", "get_syntax_highlighter");
  6439. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "highlight_all_occurrences"), "set_highlight_all_occurrences", "is_highlight_all_occurrences_enabled");
  6440. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "highlight_current_line"), "set_highlight_current_line", "is_highlight_current_line_enabled");
  6441. ADD_GROUP("Visual Whitespace", "draw_");
  6442. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "draw_control_chars"), "set_draw_control_chars", "get_draw_control_chars");
  6443. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "draw_tabs"), "set_draw_tabs", "is_drawing_tabs");
  6444. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "draw_spaces"), "set_draw_spaces", "is_drawing_spaces");
  6445. ADD_GROUP("BiDi", "");
  6446. ADD_PROPERTY(PropertyInfo(Variant::INT, "text_direction", PROPERTY_HINT_ENUM, "Auto,Left-to-Right,Right-to-Left,Inherited"), "set_text_direction", "get_text_direction");
  6447. ADD_PROPERTY(PropertyInfo(Variant::STRING, "language", PROPERTY_HINT_LOCALE_ID, ""), "set_language", "get_language");
  6448. 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");
  6449. ADD_PROPERTY(PropertyInfo(Variant::ARRAY, "structured_text_bidi_override_options"), "set_structured_text_bidi_override_options", "get_structured_text_bidi_override_options");
  6450. /* Signals */
  6451. /* Core. */
  6452. ADD_SIGNAL(MethodInfo("text_set"));
  6453. ADD_SIGNAL(MethodInfo("text_changed"));
  6454. ADD_SIGNAL(MethodInfo("lines_edited_from", PropertyInfo(Variant::INT, "from_line"), PropertyInfo(Variant::INT, "to_line")));
  6455. /* Caret. */
  6456. ADD_SIGNAL(MethodInfo("caret_changed"));
  6457. /* Gutters. */
  6458. ADD_SIGNAL(MethodInfo("gutter_clicked", PropertyInfo(Variant::INT, "line"), PropertyInfo(Variant::INT, "gutter")));
  6459. ADD_SIGNAL(MethodInfo("gutter_added"));
  6460. ADD_SIGNAL(MethodInfo("gutter_removed"));
  6461. // Theme items
  6462. /* Search */
  6463. BIND_THEME_ITEM(Theme::DATA_TYPE_COLOR, TextEdit, search_result_color);
  6464. BIND_THEME_ITEM(Theme::DATA_TYPE_COLOR, TextEdit, search_result_border_color);
  6465. /* Caret */
  6466. BIND_THEME_ITEM(Theme::DATA_TYPE_CONSTANT, TextEdit, caret_width);
  6467. BIND_THEME_ITEM(Theme::DATA_TYPE_COLOR, TextEdit, caret_color);
  6468. BIND_THEME_ITEM(Theme::DATA_TYPE_COLOR, TextEdit, caret_background_color);
  6469. /* Selection */
  6470. BIND_THEME_ITEM(Theme::DATA_TYPE_COLOR, TextEdit, font_selected_color);
  6471. BIND_THEME_ITEM(Theme::DATA_TYPE_COLOR, TextEdit, selection_color);
  6472. /* Other visuals */
  6473. BIND_THEME_ITEM_CUSTOM(Theme::DATA_TYPE_STYLEBOX, TextEdit, style_normal, "normal");
  6474. BIND_THEME_ITEM_CUSTOM(Theme::DATA_TYPE_STYLEBOX, TextEdit, style_focus, "focus");
  6475. BIND_THEME_ITEM_CUSTOM(Theme::DATA_TYPE_STYLEBOX, TextEdit, style_readonly, "read_only");
  6476. BIND_THEME_ITEM_CUSTOM(Theme::DATA_TYPE_ICON, TextEdit, tab_icon, "tab");
  6477. BIND_THEME_ITEM_CUSTOM(Theme::DATA_TYPE_ICON, TextEdit, space_icon, "space");
  6478. BIND_THEME_ITEM(Theme::DATA_TYPE_FONT, TextEdit, font);
  6479. BIND_THEME_ITEM(Theme::DATA_TYPE_FONT_SIZE, TextEdit, font_size);
  6480. BIND_THEME_ITEM(Theme::DATA_TYPE_COLOR, TextEdit, font_color);
  6481. BIND_THEME_ITEM(Theme::DATA_TYPE_COLOR, TextEdit, font_readonly_color);
  6482. BIND_THEME_ITEM(Theme::DATA_TYPE_COLOR, TextEdit, font_placeholder_color);
  6483. BIND_THEME_ITEM(Theme::DATA_TYPE_CONSTANT, TextEdit, outline_size);
  6484. BIND_THEME_ITEM_CUSTOM(Theme::DATA_TYPE_COLOR, TextEdit, outline_color, "font_outline_color");
  6485. BIND_THEME_ITEM(Theme::DATA_TYPE_CONSTANT, TextEdit, line_spacing);
  6486. BIND_THEME_ITEM(Theme::DATA_TYPE_COLOR, TextEdit, background_color);
  6487. BIND_THEME_ITEM(Theme::DATA_TYPE_COLOR, TextEdit, current_line_color);
  6488. BIND_THEME_ITEM(Theme::DATA_TYPE_COLOR, TextEdit, word_highlighted_color);
  6489. /* Settings. */
  6490. GLOBAL_DEF(PropertyInfo(Variant::FLOAT, "gui/timers/text_edit_idle_detect_sec", PROPERTY_HINT_RANGE, "0,10,0.01,or_greater"), 3);
  6491. GLOBAL_DEF(PropertyInfo(Variant::INT, "gui/common/text_edit_undo_stack_max_size", PROPERTY_HINT_RANGE, "0,10000,1,or_greater"), 1024);
  6492. /* Dependencies */
  6493. ADD_CLASS_DEPENDENCY("HScrollBar");
  6494. ADD_CLASS_DEPENDENCY("PopupMenu");
  6495. ADD_CLASS_DEPENDENCY("Timer");
  6496. ADD_CLASS_DEPENDENCY("VScrollBar");
  6497. }
  6498. /* Internal API for CodeEdit. */
  6499. // Line hiding.
  6500. void TextEdit::_set_hiding_enabled(bool p_enabled) {
  6501. if (hiding_enabled == p_enabled) {
  6502. return;
  6503. }
  6504. if (!p_enabled) {
  6505. _unhide_all_lines();
  6506. }
  6507. hiding_enabled = p_enabled;
  6508. queue_accessibility_update();
  6509. queue_redraw();
  6510. }
  6511. bool TextEdit::_is_hiding_enabled() const {
  6512. return hiding_enabled;
  6513. }
  6514. bool TextEdit::_is_line_hidden(int p_line) const {
  6515. ERR_FAIL_INDEX_V(p_line, text.size(), false);
  6516. return text.is_hidden(p_line);
  6517. }
  6518. void TextEdit::_unhide_all_lines() {
  6519. for (int i = 0; i < text.size(); i++) {
  6520. text.set_hidden(i, false);
  6521. }
  6522. _update_scrollbars();
  6523. queue_accessibility_update();
  6524. queue_redraw();
  6525. }
  6526. void TextEdit::_unhide_carets() {
  6527. // Override for functionality.
  6528. }
  6529. void TextEdit::_set_line_as_hidden(int p_line, bool p_hidden) {
  6530. ERR_FAIL_INDEX(p_line, text.size());
  6531. if (text.is_hidden(p_line) == p_hidden) {
  6532. return;
  6533. }
  6534. if (_is_hiding_enabled() || !p_hidden) {
  6535. text.set_hidden(p_line, p_hidden);
  6536. }
  6537. queue_accessibility_update();
  6538. queue_redraw();
  6539. }
  6540. // Symbol lookup.
  6541. void TextEdit::_set_symbol_lookup_word(const String &p_symbol) {
  6542. if (lookup_symbol_word == p_symbol) {
  6543. return;
  6544. }
  6545. lookup_symbol_word = p_symbol;
  6546. queue_redraw();
  6547. }
  6548. /* Text manipulation */
  6549. // Overridable actions
  6550. void TextEdit::_handle_unicode_input_internal(const uint32_t p_unicode, int p_caret) {
  6551. ERR_FAIL_COND(p_caret >= get_caret_count() || p_caret < -1);
  6552. if (!editable) {
  6553. return;
  6554. }
  6555. start_action(EditAction::ACTION_TYPING);
  6556. begin_multicaret_edit();
  6557. for (int i = 0; i < get_caret_count(); i++) {
  6558. if (p_caret == -1 && multicaret_edit_ignore_caret(i)) {
  6559. continue;
  6560. }
  6561. if (p_caret != -1 && p_caret != i) {
  6562. continue;
  6563. }
  6564. // Remove the old character if in insert mode and no selection.
  6565. if (overtype_mode && !has_selection(i)) {
  6566. // Make sure we don't try and remove empty space.
  6567. int cl = get_caret_line(i);
  6568. int cc = get_caret_column(i);
  6569. if (cc < get_line(cl).length()) {
  6570. _remove_text(cl, cc, cl, cc + 1);
  6571. }
  6572. }
  6573. const char32_t chr[2] = { (char32_t)p_unicode, 0 };
  6574. insert_text_at_caret(chr, i);
  6575. }
  6576. end_multicaret_edit();
  6577. end_action();
  6578. }
  6579. void TextEdit::_backspace_internal(int p_caret) {
  6580. ERR_FAIL_COND(p_caret >= get_caret_count() || p_caret < -1);
  6581. if (!editable) {
  6582. return;
  6583. }
  6584. if (has_selection(p_caret)) {
  6585. delete_selection(p_caret);
  6586. return;
  6587. }
  6588. begin_complex_operation();
  6589. begin_multicaret_edit();
  6590. for (int i = 0; i < get_caret_count(); i++) {
  6591. if (p_caret == -1 && multicaret_edit_ignore_caret(i)) {
  6592. continue;
  6593. }
  6594. if (p_caret != -1 && p_caret != i) {
  6595. continue;
  6596. }
  6597. int to_line = get_caret_line(i);
  6598. int to_column = get_caret_column(i);
  6599. if (to_column == 0 && to_line == 0) {
  6600. continue;
  6601. }
  6602. int from_line = to_column > 0 ? to_line : to_line - 1;
  6603. int from_column = 0;
  6604. if (to_column == 0) {
  6605. from_column = text[to_line - 1].length();
  6606. } else if (caret_mid_grapheme_enabled || !backspace_deletes_composite_character_enabled) {
  6607. from_column = to_column - 1;
  6608. } else {
  6609. from_column = get_previous_composite_character_column(to_line, to_column);
  6610. }
  6611. merge_gutters(from_line, to_line);
  6612. _remove_text(from_line, from_column, to_line, to_column);
  6613. collapse_carets(from_line, from_column, to_line, to_column);
  6614. _offset_carets_after(to_line, to_column, from_line, from_column);
  6615. set_caret_line(from_line, false, true, -1, i);
  6616. set_caret_column(from_column, i == 0, i);
  6617. }
  6618. end_multicaret_edit();
  6619. end_complex_operation();
  6620. }
  6621. void TextEdit::_cut_internal(int p_caret) {
  6622. ERR_FAIL_COND(p_caret >= get_caret_count() || p_caret < -1);
  6623. _copy_internal(p_caret);
  6624. if (!editable) {
  6625. return;
  6626. }
  6627. if (has_selection(p_caret)) {
  6628. delete_selection(p_caret);
  6629. return;
  6630. }
  6631. if (!empty_selection_clipboard_enabled) {
  6632. return;
  6633. }
  6634. // Remove full lines.
  6635. begin_complex_operation();
  6636. begin_multicaret_edit();
  6637. Vector<Point2i> line_ranges;
  6638. if (p_caret == -1) {
  6639. line_ranges = get_line_ranges_from_carets();
  6640. } else {
  6641. line_ranges.push_back(Point2i(get_caret_line(p_caret), get_caret_line(p_caret)));
  6642. }
  6643. int line_offset = 0;
  6644. for (Point2i line_range : line_ranges) {
  6645. // Preserve carets on the last line.
  6646. remove_line_at(line_range.y + line_offset);
  6647. if (line_range.x != line_range.y) {
  6648. remove_text(line_range.x + line_offset, 0, line_range.y + line_offset, 0);
  6649. }
  6650. line_offset += line_range.x - line_range.y - 1;
  6651. }
  6652. end_multicaret_edit();
  6653. end_complex_operation();
  6654. }
  6655. void TextEdit::_copy_internal(int p_caret) {
  6656. ERR_FAIL_COND(p_caret >= get_caret_count() || p_caret < -1);
  6657. if (has_selection(p_caret)) {
  6658. DisplayServer::get_singleton()->clipboard_set(get_selected_text(p_caret));
  6659. cut_copy_line = "";
  6660. return;
  6661. }
  6662. if (!empty_selection_clipboard_enabled) {
  6663. return;
  6664. }
  6665. // Copy full lines.
  6666. StringBuilder clipboard;
  6667. Vector<Point2i> line_ranges;
  6668. if (p_caret == -1) {
  6669. // When there are multiple carets on a line, only copy it once.
  6670. line_ranges = get_line_ranges_from_carets(false, true);
  6671. } else {
  6672. line_ranges.push_back(Point2i(get_caret_line(p_caret), get_caret_line(p_caret)));
  6673. }
  6674. for (Point2i line_range : line_ranges) {
  6675. for (int i = line_range.x; i <= line_range.y; i++) {
  6676. if (text[i].length() != 0) {
  6677. clipboard += _base_get_text(i, 0, i, text[i].length());
  6678. }
  6679. clipboard += "\n";
  6680. }
  6681. }
  6682. String clipboard_string = clipboard.as_string();
  6683. DisplayServer::get_singleton()->clipboard_set(clipboard_string);
  6684. // Set the cut copy line so we know to paste as a line.
  6685. if (get_caret_count() == 1) {
  6686. cut_copy_line = clipboard_string;
  6687. } else {
  6688. cut_copy_line = "";
  6689. }
  6690. }
  6691. void TextEdit::_paste_internal(int p_caret) {
  6692. ERR_FAIL_COND(p_caret >= get_caret_count() || p_caret < -1);
  6693. if (!editable) {
  6694. return;
  6695. }
  6696. String clipboard = DisplayServer::get_singleton()->clipboard_get();
  6697. if (clipboard.is_empty()) {
  6698. // Nothing to paste.
  6699. return;
  6700. }
  6701. // Paste a full line. Ignore '\r' characters that may have been added to the clipboard by the OS.
  6702. if (get_caret_count() == 1 && !has_selection(0) && !cut_copy_line.is_empty() && cut_copy_line == clipboard.remove_char('\r')) {
  6703. insert_text(clipboard, get_caret_line(), 0);
  6704. return;
  6705. }
  6706. // Paste text at each caret or one line per caret.
  6707. Vector<String> clipboard_lines = clipboard.split("\n");
  6708. bool insert_line_per_caret = p_caret == -1 && get_caret_count() > 1 && clipboard_lines.size() == get_caret_count();
  6709. begin_complex_operation();
  6710. begin_multicaret_edit();
  6711. Vector<int> sorted_carets = get_sorted_carets();
  6712. for (int i = 0; i < sorted_carets.size(); i++) {
  6713. int caret_index = sorted_carets[i];
  6714. if (p_caret != -1 && p_caret != caret_index) {
  6715. continue;
  6716. }
  6717. if (has_selection(caret_index)) {
  6718. delete_selection(caret_index);
  6719. }
  6720. if (insert_line_per_caret) {
  6721. clipboard = clipboard_lines[i];
  6722. }
  6723. insert_text_at_caret(clipboard, caret_index);
  6724. }
  6725. end_multicaret_edit();
  6726. end_complex_operation();
  6727. }
  6728. void TextEdit::_paste_primary_clipboard_internal(int p_caret) {
  6729. ERR_FAIL_COND(p_caret >= get_caret_count() || p_caret < -1);
  6730. if (!is_editable() || !DisplayServer::get_singleton()->has_feature(DisplayServer::FEATURE_CLIPBOARD_PRIMARY)) {
  6731. return;
  6732. }
  6733. String paste_buffer = DisplayServer::get_singleton()->clipboard_get_primary();
  6734. if (get_caret_count() == 1) {
  6735. Point2i pos = get_line_column_at_pos(get_local_mouse_pos());
  6736. deselect();
  6737. set_caret_line(pos.y, true, false, -1);
  6738. set_caret_column(pos.x);
  6739. }
  6740. if (!paste_buffer.is_empty()) {
  6741. insert_text_at_caret(paste_buffer);
  6742. }
  6743. grab_focus();
  6744. }
  6745. // Context menu.
  6746. Key TextEdit::_get_menu_action_accelerator(const String &p_action) {
  6747. const List<Ref<InputEvent>> *events = InputMap::get_singleton()->action_get_events(p_action);
  6748. if (!events) {
  6749. return Key::NONE;
  6750. }
  6751. // Use first event in the list for the accelerator.
  6752. const List<Ref<InputEvent>>::Element *first_event = events->front();
  6753. if (!first_event) {
  6754. return Key::NONE;
  6755. }
  6756. const Ref<InputEventKey> event = first_event->get();
  6757. if (event.is_null()) {
  6758. return Key::NONE;
  6759. }
  6760. // Use physical keycode if non-zero.
  6761. if (event->get_physical_keycode() != Key::NONE) {
  6762. return event->get_physical_keycode_with_modifiers();
  6763. } else {
  6764. return event->get_keycode_with_modifiers();
  6765. }
  6766. }
  6767. void TextEdit::_generate_context_menu() {
  6768. menu = memnew(PopupMenu);
  6769. add_child(menu, false, INTERNAL_MODE_FRONT);
  6770. menu_dir = memnew(PopupMenu);
  6771. menu_dir->add_radio_check_item(ETR("Same as Layout Direction"), MENU_DIR_INHERITED);
  6772. menu_dir->add_radio_check_item(ETR("Auto-Detect Direction"), MENU_DIR_AUTO);
  6773. menu_dir->add_radio_check_item(ETR("Left-to-Right"), MENU_DIR_LTR);
  6774. menu_dir->add_radio_check_item(ETR("Right-to-Left"), MENU_DIR_RTL);
  6775. menu_ctl = memnew(PopupMenu);
  6776. menu_ctl->add_item(ETR("Left-to-Right Mark (LRM)"), MENU_INSERT_LRM);
  6777. menu_ctl->add_item(ETR("Right-to-Left Mark (RLM)"), MENU_INSERT_RLM);
  6778. menu_ctl->add_item(ETR("Start of Left-to-Right Embedding (LRE)"), MENU_INSERT_LRE);
  6779. menu_ctl->add_item(ETR("Start of Right-to-Left Embedding (RLE)"), MENU_INSERT_RLE);
  6780. menu_ctl->add_item(ETR("Start of Left-to-Right Override (LRO)"), MENU_INSERT_LRO);
  6781. menu_ctl->add_item(ETR("Start of Right-to-Left Override (RLO)"), MENU_INSERT_RLO);
  6782. menu_ctl->add_item(ETR("Pop Direction Formatting (PDF)"), MENU_INSERT_PDF);
  6783. menu_ctl->add_separator();
  6784. menu_ctl->add_item(ETR("Arabic Letter Mark (ALM)"), MENU_INSERT_ALM);
  6785. menu_ctl->add_item(ETR("Left-to-Right Isolate (LRI)"), MENU_INSERT_LRI);
  6786. menu_ctl->add_item(ETR("Right-to-Left Isolate (RLI)"), MENU_INSERT_RLI);
  6787. menu_ctl->add_item(ETR("First Strong Isolate (FSI)"), MENU_INSERT_FSI);
  6788. menu_ctl->add_item(ETR("Pop Direction Isolate (PDI)"), MENU_INSERT_PDI);
  6789. menu_ctl->add_separator();
  6790. menu_ctl->add_item(ETR("Zero-Width Joiner (ZWJ)"), MENU_INSERT_ZWJ);
  6791. menu_ctl->add_item(ETR("Zero-Width Non-Joiner (ZWNJ)"), MENU_INSERT_ZWNJ);
  6792. menu_ctl->add_item(ETR("Word Joiner (WJ)"), MENU_INSERT_WJ);
  6793. menu_ctl->add_item(ETR("Soft Hyphen (SHY)"), MENU_INSERT_SHY);
  6794. if (DisplayServer::get_singleton()->has_feature(DisplayServer::FEATURE_EMOJI_AND_SYMBOL_PICKER)) {
  6795. menu->add_item(ETR("Emoji & Symbols"), MENU_EMOJI_AND_SYMBOL);
  6796. menu->add_separator();
  6797. }
  6798. menu->add_item(ETR("Cut"), MENU_CUT);
  6799. menu->add_item(ETR("Copy"), MENU_COPY);
  6800. menu->add_item(ETR("Paste"), MENU_PASTE);
  6801. menu->add_separator();
  6802. menu->add_item(ETR("Select All"), MENU_SELECT_ALL);
  6803. menu->add_item(ETR("Clear"), MENU_CLEAR);
  6804. menu->add_separator();
  6805. menu->add_item(ETR("Undo"), MENU_UNDO);
  6806. menu->add_item(ETR("Redo"), MENU_REDO);
  6807. menu->add_separator();
  6808. menu->add_submenu_node_item(ETR("Text Writing Direction"), menu_dir, MENU_SUBMENU_TEXT_DIR);
  6809. menu->add_separator();
  6810. menu->add_check_item(ETR("Display Control Characters"), MENU_DISPLAY_UCC);
  6811. menu->add_submenu_node_item(ETR("Insert Control Character"), menu_ctl, MENU_SUBMENU_INSERT_UCC);
  6812. menu->connect(SceneStringName(id_pressed), callable_mp(this, &TextEdit::menu_option));
  6813. menu_dir->connect(SceneStringName(id_pressed), callable_mp(this, &TextEdit::menu_option));
  6814. menu_ctl->connect(SceneStringName(id_pressed), callable_mp(this, &TextEdit::menu_option));
  6815. }
  6816. void TextEdit::_update_context_menu() {
  6817. if (!menu) {
  6818. _generate_context_menu();
  6819. }
  6820. int idx = -1;
  6821. #define MENU_ITEM_ACTION_DISABLED(m_menu, m_id, m_action, m_disabled) \
  6822. idx = m_menu->get_item_index(m_id); \
  6823. if (idx >= 0) { \
  6824. m_menu->set_item_accelerator(idx, shortcut_keys_enabled ? _get_menu_action_accelerator(m_action) : Key::NONE); \
  6825. m_menu->set_item_disabled(idx, m_disabled); \
  6826. }
  6827. #define MENU_ITEM_ACTION(m_menu, m_id, m_action) \
  6828. idx = m_menu->get_item_index(m_id); \
  6829. if (idx >= 0) { \
  6830. m_menu->set_item_accelerator(idx, shortcut_keys_enabled ? _get_menu_action_accelerator(m_action) : Key::NONE); \
  6831. }
  6832. #define MENU_ITEM_DISABLED(m_menu, m_id, m_disabled) \
  6833. idx = m_menu->get_item_index(m_id); \
  6834. if (idx >= 0) { \
  6835. m_menu->set_item_disabled(idx, m_disabled); \
  6836. }
  6837. #define MENU_ITEM_CHECKED(m_menu, m_id, m_checked) \
  6838. idx = m_menu->get_item_index(m_id); \
  6839. if (idx >= 0) { \
  6840. m_menu->set_item_checked(idx, m_checked); \
  6841. }
  6842. if (DisplayServer::get_singleton()->has_feature(DisplayServer::FEATURE_EMOJI_AND_SYMBOL_PICKER)) {
  6843. MENU_ITEM_DISABLED(menu, MENU_EMOJI_AND_SYMBOL, !editable || !emoji_menu_enabled)
  6844. }
  6845. MENU_ITEM_ACTION_DISABLED(menu, MENU_CUT, "ui_cut", !editable)
  6846. MENU_ITEM_ACTION(menu, MENU_COPY, "ui_copy")
  6847. MENU_ITEM_ACTION_DISABLED(menu, MENU_PASTE, "ui_paste", !editable)
  6848. MENU_ITEM_ACTION_DISABLED(menu, MENU_SELECT_ALL, "ui_text_select_all", !selecting_enabled)
  6849. MENU_ITEM_DISABLED(menu, MENU_CLEAR, !editable)
  6850. MENU_ITEM_ACTION_DISABLED(menu, MENU_UNDO, "ui_undo", !editable || !has_undo())
  6851. MENU_ITEM_ACTION_DISABLED(menu, MENU_REDO, "ui_redo", !editable || !has_redo())
  6852. MENU_ITEM_CHECKED(menu_dir, MENU_DIR_INHERITED, text_direction == TEXT_DIRECTION_INHERITED)
  6853. MENU_ITEM_CHECKED(menu_dir, MENU_DIR_AUTO, text_direction == TEXT_DIRECTION_AUTO)
  6854. MENU_ITEM_CHECKED(menu_dir, MENU_DIR_LTR, text_direction == TEXT_DIRECTION_LTR)
  6855. MENU_ITEM_CHECKED(menu_dir, MENU_DIR_RTL, text_direction == TEXT_DIRECTION_RTL)
  6856. MENU_ITEM_CHECKED(menu, MENU_DISPLAY_UCC, draw_control_chars)
  6857. MENU_ITEM_DISABLED(menu, MENU_SUBMENU_INSERT_UCC, !editable)
  6858. #undef MENU_ITEM_ACTION_DISABLED
  6859. #undef MENU_ITEM_ACTION
  6860. #undef MENU_ITEM_DISABLED
  6861. #undef MENU_ITEM_CHECKED
  6862. }
  6863. /* Versioning */
  6864. void TextEdit::_push_current_op() {
  6865. if (pending_action_end) {
  6866. start_action(EditAction::ACTION_NONE);
  6867. return;
  6868. }
  6869. if (current_op.type == TextOperation::TYPE_NONE) {
  6870. return; // Nothing to do.
  6871. }
  6872. if (next_operation_is_complex) {
  6873. current_op.chain_forward = true;
  6874. next_operation_is_complex = false;
  6875. }
  6876. undo_stack.push_back(current_op);
  6877. current_op.type = TextOperation::TYPE_NONE;
  6878. current_op.text = "";
  6879. current_op.chain_forward = false;
  6880. if (undo_stack.size() > undo_stack_max_size) {
  6881. undo_stack.pop_front();
  6882. }
  6883. }
  6884. void TextEdit::_do_text_op(const TextOperation &p_op, bool p_reverse) {
  6885. ERR_FAIL_COND(p_op.type == TextOperation::TYPE_NONE);
  6886. bool insert = p_op.type == TextOperation::TYPE_INSERT;
  6887. if (p_reverse) {
  6888. insert = !insert;
  6889. }
  6890. if (insert) {
  6891. int check_line;
  6892. int check_column;
  6893. _base_insert_text(p_op.from_line, p_op.from_column, p_op.text, check_line, check_column);
  6894. ERR_FAIL_COND(check_line != p_op.to_line); // BUG.
  6895. ERR_FAIL_COND(check_column != p_op.to_column); // BUG.
  6896. } else {
  6897. _base_remove_text(p_op.from_line, p_op.from_column, p_op.to_line, p_op.to_column);
  6898. }
  6899. }
  6900. void TextEdit::_clear_redo() {
  6901. if (undo_stack_pos == nullptr) {
  6902. return; // Nothing to clear.
  6903. }
  6904. _push_current_op();
  6905. while (undo_stack_pos) {
  6906. List<TextOperation>::Element *elem = undo_stack_pos;
  6907. undo_stack_pos = undo_stack_pos->next();
  6908. undo_stack.erase(elem);
  6909. }
  6910. }
  6911. /* Search */
  6912. 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 {
  6913. int col = -1;
  6914. if (p_key.length() > 0 && p_search.length() > 0) {
  6915. if (p_from_column < 0 || p_from_column > p_search.length()) {
  6916. p_from_column = 0;
  6917. }
  6918. bool key_start_is_symbol = is_symbol(p_key[0]);
  6919. bool key_end_is_symbol = is_symbol(p_key[p_key.length() - 1]);
  6920. while (col == -1 && p_from_column <= p_search.length()) {
  6921. if (p_search_flags & SEARCH_MATCH_CASE) {
  6922. col = p_search.find(p_key, p_from_column);
  6923. } else {
  6924. col = p_search.findn(p_key, p_from_column);
  6925. }
  6926. // If not found, just break early to improve performance.
  6927. if (col == -1) {
  6928. break;
  6929. }
  6930. // Whole words only.
  6931. if (col != -1 && p_search_flags & SEARCH_WHOLE_WORDS) {
  6932. p_from_column = col;
  6933. if (!key_start_is_symbol && col > 0 && !is_symbol(p_search[col - 1])) {
  6934. col = -1;
  6935. } else if (!key_end_is_symbol && (col + p_key.length()) < p_search.length() && !is_symbol(p_search[col + p_key.length()])) {
  6936. col = -1;
  6937. }
  6938. }
  6939. p_from_column += 1;
  6940. }
  6941. }
  6942. return col;
  6943. }
  6944. /* Mouse */
  6945. int TextEdit::_get_char_pos_for_line(int p_px, int p_line, int p_wrap_index) const {
  6946. ERR_FAIL_INDEX_V(p_line, text.size(), 0);
  6947. p_wrap_index = MIN(p_wrap_index, text.get_line_data(p_line)->get_line_count() - 1);
  6948. RID text_rid = text.get_line_data(p_line)->get_line_rid(p_wrap_index);
  6949. const float wrap_indent = _get_wrap_indent_offset(p_line, p_wrap_index, is_layout_rtl());
  6950. if (is_layout_rtl()) {
  6951. p_px = TS->shaped_text_get_size(text_rid).x - p_px + wrap_indent;
  6952. } else {
  6953. p_px -= wrap_indent;
  6954. }
  6955. int ofs = TS->shaped_text_hit_test_position(text_rid, p_px);
  6956. if (!caret_mid_grapheme_enabled) {
  6957. ofs = TS->shaped_text_closest_character_pos(text_rid, ofs);
  6958. }
  6959. return ofs;
  6960. }
  6961. /* Caret */
  6962. void TextEdit::_caret_changed(int p_caret) {
  6963. queue_redraw();
  6964. if (has_selection(p_caret)) {
  6965. _selection_changed(p_caret);
  6966. }
  6967. if (caret_pos_dirty) {
  6968. return;
  6969. }
  6970. if (is_inside_tree()) {
  6971. callable_mp(this, &TextEdit::_emit_caret_changed).call_deferred();
  6972. }
  6973. caret_pos_dirty = true;
  6974. }
  6975. void TextEdit::_emit_caret_changed() {
  6976. emit_signal(SNAME("caret_changed"));
  6977. caret_pos_dirty = false;
  6978. }
  6979. void TextEdit::_reset_caret_blink_timer() {
  6980. if (!caret_blink_enabled) {
  6981. return;
  6982. }
  6983. draw_caret = true;
  6984. if (has_focus()) {
  6985. caret_blink_timer->stop();
  6986. caret_blink_timer->start();
  6987. queue_redraw();
  6988. }
  6989. }
  6990. void TextEdit::_toggle_draw_caret() {
  6991. draw_caret = !draw_caret;
  6992. if (is_visible_in_tree() && has_focus() && window_has_focus) {
  6993. queue_redraw();
  6994. }
  6995. }
  6996. int TextEdit::_get_column_x_offset_for_line(int p_char, int p_line, int p_column) const {
  6997. ERR_FAIL_INDEX_V(p_line, text.size(), 0);
  6998. int wrap_index = 0;
  6999. Vector<Vector2i> wrap_ranges = text.get_line_wrap_ranges(p_line);
  7000. for (int i = 0; i < wrap_ranges.size(); i++) {
  7001. if ((p_char >= wrap_ranges[i].x) && (p_char < wrap_ranges[i].y || (i == wrap_ranges.size() - 1 && p_char == wrap_ranges[i].y))) {
  7002. wrap_index = i;
  7003. break;
  7004. }
  7005. }
  7006. RID text_rid = text.get_line_data(p_line)->get_line_rid(wrap_index);
  7007. bool rtl = is_layout_rtl();
  7008. const float wrap_indent = _get_wrap_indent_offset(p_line, wrap_index, rtl);
  7009. CaretInfo ts_caret = TS->shaped_text_get_carets(text_rid, p_column);
  7010. 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())) {
  7011. return ts_caret.l_caret.position.x + (rtl ? -wrap_indent : wrap_indent);
  7012. } else {
  7013. return ts_caret.t_caret.position.x + (rtl ? -wrap_indent : wrap_indent);
  7014. }
  7015. }
  7016. bool TextEdit::_is_line_col_in_range(int p_line, int p_column, int p_from_line, int p_from_column, int p_to_line, int p_to_column, bool p_include_edges) const {
  7017. if (p_line >= p_from_line && p_line <= p_to_line && (p_line > p_from_line || p_column > p_from_column) && (p_line < p_to_line || p_column < p_to_column)) {
  7018. return true;
  7019. }
  7020. if (p_include_edges) {
  7021. if ((p_line == p_from_line && p_column == p_from_column) || (p_line == p_to_line && p_column == p_to_column)) {
  7022. return true;
  7023. }
  7024. }
  7025. return false;
  7026. }
  7027. void TextEdit::_offset_carets_after(int p_old_line, int p_old_column, int p_new_line, int p_new_column, bool p_include_selection_begin, bool p_include_selection_end) {
  7028. // Moves all carets at or after old_line and old_column.
  7029. // Called after deleting or inserting text so that the carets stay with the text they are at.
  7030. int edit_height = p_new_line - p_old_line;
  7031. int edit_size = p_new_column - p_old_column;
  7032. if (edit_height == 0 && edit_size == 0) {
  7033. return;
  7034. }
  7035. // Intentionally includes carets in the multicaret_edit_ignore list so that they are moved together.
  7036. for (int i = 0; i < get_caret_count(); i++) {
  7037. bool selected = has_selection(i);
  7038. bool caret_at_end = selected && is_caret_after_selection_origin(i);
  7039. bool include_caret_at = caret_at_end ? p_include_selection_end : p_include_selection_begin;
  7040. // Move caret.
  7041. int caret_line = get_caret_line(i);
  7042. int caret_column = get_caret_column(i);
  7043. bool caret_after = caret_line > p_old_line || (caret_line == p_old_line && caret_column > p_old_column);
  7044. bool caret_at = caret_line == p_old_line && caret_column == p_old_column;
  7045. if (caret_after || (caret_at && include_caret_at)) {
  7046. caret_line += edit_height;
  7047. if (caret_line == p_new_line) {
  7048. caret_column += edit_size;
  7049. }
  7050. if (edit_height != 0) {
  7051. set_caret_line(caret_line, false, true, -1, i);
  7052. }
  7053. set_caret_column(caret_column, false, i);
  7054. }
  7055. // Move selection origin.
  7056. if (!selected) {
  7057. continue;
  7058. }
  7059. bool include_selection_origin_at = !caret_at_end ? p_include_selection_end : p_include_selection_begin;
  7060. int selection_origin_line = get_selection_origin_line(i);
  7061. int selection_origin_column = get_selection_origin_column(i);
  7062. bool selection_origin_after = selection_origin_line > p_old_line || (selection_origin_line == p_old_line && selection_origin_column > p_old_column);
  7063. bool selection_origin_at = selection_origin_line == p_old_line && selection_origin_column == p_old_column;
  7064. if (selection_origin_after || (selection_origin_at && include_selection_origin_at)) {
  7065. selection_origin_line += edit_height;
  7066. if (selection_origin_line == p_new_line) {
  7067. selection_origin_column += edit_size;
  7068. }
  7069. select(selection_origin_line, selection_origin_column, caret_line, caret_column, i);
  7070. }
  7071. }
  7072. if (!p_include_selection_begin && p_include_selection_end && has_selection()) {
  7073. // It is possible that two adjacent selections now overlap.
  7074. merge_overlapping_carets();
  7075. }
  7076. }
  7077. void TextEdit::_cancel_drag_and_drop_text() {
  7078. // Cancel the drag operation if drag originated from here.
  7079. if (selection_drag_attempt && get_viewport()) {
  7080. get_viewport()->gui_cancel_drag();
  7081. }
  7082. }
  7083. /* Selection */
  7084. void TextEdit::_selection_changed(int p_caret) {
  7085. if (!selecting_enabled) {
  7086. return;
  7087. }
  7088. _cancel_drag_and_drop_text();
  7089. queue_accessibility_update();
  7090. queue_redraw();
  7091. }
  7092. void TextEdit::_click_selection_held() {
  7093. // Update the selection mode on a timer so it is updated when the view scrolls even if the mouse isn't moving.
  7094. if (!Input::get_singleton()->is_mouse_button_pressed(MouseButton::LEFT)) {
  7095. click_select_held->stop();
  7096. return;
  7097. }
  7098. switch (get_selection_mode()) {
  7099. case SelectionMode::SELECTION_MODE_POINTER: {
  7100. _update_selection_mode_pointer();
  7101. } break;
  7102. case SelectionMode::SELECTION_MODE_WORD: {
  7103. _update_selection_mode_word();
  7104. } break;
  7105. case SelectionMode::SELECTION_MODE_LINE: {
  7106. _update_selection_mode_line();
  7107. } break;
  7108. default: {
  7109. click_select_held->stop();
  7110. break;
  7111. }
  7112. }
  7113. }
  7114. void TextEdit::_update_selection_mode_pointer(bool p_initial) {
  7115. Point2 mp = get_local_mouse_pos();
  7116. Point2i pos = get_line_column_at_pos(mp);
  7117. int line = pos.y;
  7118. int column = pos.x;
  7119. int caret_index = get_caret_count() - 1;
  7120. if (p_initial && !has_selection(caret_index)) {
  7121. set_selection_origin_line(line, true, -1, caret_index);
  7122. set_selection_origin_column(column, caret_index);
  7123. // Set the word begin and end to the column in case the mode changes later.
  7124. carets.write[caret_index].selection.word_begin_column = column;
  7125. carets.write[caret_index].selection.word_end_column = column;
  7126. } else {
  7127. select(get_selection_origin_line(caret_index), get_selection_origin_column(caret_index), line, column, caret_index);
  7128. }
  7129. adjust_viewport_to_caret(caret_index);
  7130. if (has_selection(caret_index)) {
  7131. // Only set to true if any selection has been made.
  7132. dragging_selection = true;
  7133. }
  7134. click_select_held->start();
  7135. merge_overlapping_carets();
  7136. }
  7137. void TextEdit::_update_selection_mode_word(bool p_initial) {
  7138. dragging_selection = true;
  7139. Point2 mp = get_local_mouse_pos();
  7140. Point2i pos = get_line_column_at_pos(mp);
  7141. int line = pos.y;
  7142. int column = pos.x;
  7143. int caret_index = get_caret_count() - 1;
  7144. int caret_pos = CLAMP(column, 0, text[line].length());
  7145. int beg = caret_pos;
  7146. int end = beg;
  7147. PackedInt32Array words = TS->shaped_text_get_word_breaks(text.get_line_data(line)->get_rid());
  7148. for (int i = 0; i < words.size(); i = i + 2) {
  7149. if ((p_initial && words[i] <= caret_pos && words[i + 1] >= caret_pos) || (!p_initial && words[i] < caret_pos && words[i + 1] > caret_pos)) {
  7150. beg = words[i];
  7151. end = words[i + 1];
  7152. break;
  7153. }
  7154. }
  7155. if (p_initial && !has_selection(caret_index)) {
  7156. // Set the selection origin if there is no existing selection.
  7157. select(line, beg, line, end, caret_index);
  7158. carets.write[caret_index].selection.word_begin_column = beg;
  7159. carets.write[caret_index].selection.word_end_column = end;
  7160. } else {
  7161. // Expand the word selection to the mouse.
  7162. int origin_line = get_selection_origin_line(caret_index);
  7163. bool is_new_selection_dir_right = line > origin_line || (line == origin_line && column >= carets[caret_index].selection.word_begin_column);
  7164. int origin_col = is_new_selection_dir_right ? carets[caret_index].selection.word_begin_column : carets[caret_index].selection.word_end_column;
  7165. int caret_col = is_new_selection_dir_right ? end : beg;
  7166. // Expand the word selection only if the caret is not at the start of the selection.
  7167. if (column != carets[caret_index].selection.word_begin_column || line != origin_line || carets[caret_index].selection.word_begin_column == carets[caret_index].selection.word_end_column) {
  7168. select(origin_line, origin_col, line, caret_col, caret_index);
  7169. }
  7170. }
  7171. adjust_viewport_to_caret(caret_index);
  7172. if (DisplayServer::get_singleton()->has_feature(DisplayServer::FEATURE_CLIPBOARD_PRIMARY)) {
  7173. DisplayServer::get_singleton()->clipboard_set_primary(get_selected_text());
  7174. }
  7175. click_select_held->start();
  7176. merge_overlapping_carets();
  7177. }
  7178. void TextEdit::_update_selection_mode_line(bool p_initial) {
  7179. dragging_selection = true;
  7180. Point2 mp = get_local_mouse_pos();
  7181. Point2i pos = get_line_column_at_pos(mp);
  7182. int line = pos.y;
  7183. int caret_index = get_caret_count() - 1;
  7184. int origin_line = p_initial && !has_selection(caret_index) ? line : get_selection_origin_line();
  7185. bool line_below = line >= origin_line;
  7186. int origin_col = line_below ? 0 : get_line(origin_line).length();
  7187. int caret_line = line_below ? line + 1 : line;
  7188. int caret_col = caret_line < text.size() ? 0 : get_line(text.size() - 1).length();
  7189. select(origin_line, origin_col, caret_line, caret_col, caret_index);
  7190. adjust_viewport_to_caret(caret_index);
  7191. if (p_initial) {
  7192. // Set the word begin and end to the start and end of the origin line in case the mode changes later.
  7193. carets.write[caret_index].selection.word_begin_column = 0;
  7194. carets.write[caret_index].selection.word_end_column = get_line(origin_line).length();
  7195. }
  7196. if (DisplayServer::get_singleton()->has_feature(DisplayServer::FEATURE_CLIPBOARD_PRIMARY)) {
  7197. DisplayServer::get_singleton()->clipboard_set_primary(get_selected_text());
  7198. }
  7199. click_select_held->start();
  7200. merge_overlapping_carets();
  7201. }
  7202. void TextEdit::_pre_shift_selection(int p_caret) {
  7203. if (!selecting_enabled) {
  7204. return;
  7205. }
  7206. set_selection_mode(SelectionMode::SELECTION_MODE_SHIFT);
  7207. if (has_selection(p_caret)) {
  7208. return;
  7209. }
  7210. // Prepare selection to start at current caret position.
  7211. set_selection_origin_line(get_caret_line(p_caret), true, -1, p_caret);
  7212. set_selection_origin_column(get_caret_column(p_caret), p_caret);
  7213. carets.write[p_caret].selection.active = true;
  7214. carets.write[p_caret].selection.word_begin_column = get_caret_column(p_caret);
  7215. carets.write[p_caret].selection.word_end_column = get_caret_column(p_caret);
  7216. }
  7217. bool TextEdit::_selection_contains(int p_caret, int p_line, int p_column, bool p_include_edges, bool p_only_selections) const {
  7218. if (!has_selection(p_caret)) {
  7219. return !p_only_selections && p_line == get_caret_line(p_caret) && p_column == get_caret_column(p_caret);
  7220. }
  7221. return _is_line_col_in_range(p_line, p_column, get_selection_from_line(p_caret), get_selection_from_column(p_caret), get_selection_to_line(p_caret), get_selection_to_column(p_caret), p_include_edges);
  7222. }
  7223. /* Line Wrapping */
  7224. void TextEdit::_update_wrap_at_column(bool p_force) {
  7225. int new_wrap_at = get_size().width - theme_cache.style_normal->get_minimum_size().width - gutters_width - gutter_padding;
  7226. if (draw_minimap) {
  7227. new_wrap_at -= minimap_width;
  7228. }
  7229. if (v_scroll->is_visible_in_tree()) {
  7230. new_wrap_at -= v_scroll->get_combined_minimum_size().width;
  7231. }
  7232. /* Give it a little more space. */
  7233. new_wrap_at -= wrap_right_offset;
  7234. if ((wrap_at_column != new_wrap_at) || p_force) {
  7235. wrap_at_column = new_wrap_at;
  7236. if (line_wrapping_mode) {
  7237. BitField<TextServer::LineBreakFlag> autowrap_flags = TextServer::BREAK_MANDATORY;
  7238. switch (autowrap_mode) {
  7239. case TextServer::AUTOWRAP_WORD_SMART:
  7240. autowrap_flags = TextServer::BREAK_WORD_BOUND | TextServer::BREAK_ADAPTIVE | TextServer::BREAK_MANDATORY;
  7241. break;
  7242. case TextServer::AUTOWRAP_WORD:
  7243. autowrap_flags = TextServer::BREAK_WORD_BOUND | TextServer::BREAK_MANDATORY;
  7244. break;
  7245. case TextServer::AUTOWRAP_ARBITRARY:
  7246. autowrap_flags = TextServer::BREAK_GRAPHEME_BOUND | TextServer::BREAK_MANDATORY;
  7247. break;
  7248. case TextServer::AUTOWRAP_OFF:
  7249. break;
  7250. }
  7251. text.set_brk_flags(autowrap_flags);
  7252. text.set_width(wrap_at_column);
  7253. text.invalidate_all_lines();
  7254. _update_placeholder();
  7255. } else if (text.get_width() != -1) {
  7256. text.set_width(-1);
  7257. text.invalidate_all_lines();
  7258. _update_placeholder();
  7259. }
  7260. }
  7261. // Update viewport.
  7262. int first_vis_line = get_first_visible_line();
  7263. if (is_line_wrapped(first_vis_line)) {
  7264. first_visible_line_wrap_ofs = MIN(first_visible_line_wrap_ofs, get_line_wrap_count(first_vis_line));
  7265. } else {
  7266. first_visible_line_wrap_ofs = 0;
  7267. }
  7268. set_line_as_first_visible(first_visible_line, first_visible_line_wrap_ofs);
  7269. queue_accessibility_update();
  7270. }
  7271. /* Viewport. */
  7272. void TextEdit::_update_scrollbars() {
  7273. Size2 size = get_size();
  7274. Size2 hmin = h_scroll->get_combined_minimum_size();
  7275. Size2 vmin = v_scroll->get_combined_minimum_size();
  7276. v_scroll->set_begin(Point2(size.width - vmin.width, theme_cache.style_normal->get_margin(SIDE_TOP)));
  7277. v_scroll->set_end(Point2(size.width, size.height - theme_cache.style_normal->get_margin(SIDE_TOP) - theme_cache.style_normal->get_margin(SIDE_BOTTOM)));
  7278. h_scroll->set_begin(Point2(0, size.height - hmin.height));
  7279. h_scroll->set_end(Point2(size.width - vmin.width, size.height));
  7280. bool draw_placeholder = _using_placeholder();
  7281. int visible_rows = get_visible_line_count();
  7282. int total_rows = draw_placeholder ? placeholder_wrapped_rows.size() : get_total_visible_line_count();
  7283. if ((scroll_past_end_of_file_enabled && !fit_content_height) || visible_rows == 0) {
  7284. total_rows += visible_rows - 1;
  7285. }
  7286. int visible_width = size.width - theme_cache.style_normal->get_minimum_size().width;
  7287. int total_width = (draw_placeholder ? placeholder_max_width : text.get_max_width()) + gutters_width + gutter_padding;
  7288. if (draw_minimap) {
  7289. total_width += minimap_width;
  7290. }
  7291. content_size_cache = Vector2i(total_width + 10, MAX(total_rows, 1) * get_line_height());
  7292. if (fit_content_height || fit_content_width) {
  7293. update_minimum_size();
  7294. }
  7295. updating_scrolls = true;
  7296. if (!fit_content_height && total_rows > visible_rows) {
  7297. v_scroll->show();
  7298. v_scroll->set_max(total_rows + _get_visible_lines_offset());
  7299. v_scroll->set_page(visible_rows + _get_visible_lines_offset());
  7300. set_v_scroll(get_v_scroll());
  7301. } else {
  7302. first_visible_line = 0;
  7303. first_visible_line_wrap_ofs = 0;
  7304. v_scroll->set_value(0);
  7305. v_scroll->set_max(0);
  7306. v_scroll->hide();
  7307. }
  7308. if (total_width > visible_width) {
  7309. h_scroll->show();
  7310. h_scroll->set_max(total_width);
  7311. h_scroll->set_page(visible_width);
  7312. if (first_visible_col > (total_width - visible_width)) {
  7313. first_visible_col = (total_width - visible_width);
  7314. }
  7315. if (std::fabs(h_scroll->get_value() - (double)first_visible_col) >= 1) {
  7316. h_scroll->set_value(first_visible_col);
  7317. }
  7318. } else {
  7319. first_visible_col = 0;
  7320. h_scroll->set_value(0);
  7321. h_scroll->set_max(0);
  7322. h_scroll->hide();
  7323. }
  7324. updating_scrolls = false;
  7325. }
  7326. int TextEdit::_get_control_height() const {
  7327. int control_height = get_size().height;
  7328. control_height -= theme_cache.style_normal->get_minimum_size().height;
  7329. if (h_scroll->is_visible_in_tree()) {
  7330. control_height -= h_scroll->get_size().height;
  7331. }
  7332. return control_height;
  7333. }
  7334. void TextEdit::_v_scroll_input() {
  7335. scrolling = false;
  7336. minimap_clicked = false;
  7337. }
  7338. void TextEdit::_scroll_moved(double p_to_val) {
  7339. if (updating_scrolls) {
  7340. return;
  7341. }
  7342. if (h_scroll->is_visible_in_tree()) {
  7343. first_visible_col = h_scroll->get_value();
  7344. }
  7345. if (v_scroll->is_visible_in_tree()) {
  7346. // Set line ofs and wrap ofs.
  7347. bool draw_placeholder = _using_placeholder();
  7348. int v_scroll_i = std::floor(get_v_scroll());
  7349. int sc = 0;
  7350. int n_line;
  7351. for (n_line = 0; n_line < text.size(); n_line++) {
  7352. if (!_is_line_hidden(n_line)) {
  7353. sc++;
  7354. sc += draw_placeholder ? placeholder_wrapped_rows.size() - 1 : get_line_wrap_count(n_line);
  7355. if (sc > v_scroll_i) {
  7356. break;
  7357. }
  7358. }
  7359. }
  7360. n_line = MIN(n_line, text.size() - 1);
  7361. int line_wrap_amount = draw_placeholder ? placeholder_wrapped_rows.size() - 1 : get_line_wrap_count(n_line);
  7362. int wi = line_wrap_amount - (sc - v_scroll_i - 1);
  7363. wi = CLAMP(wi, 0, line_wrap_amount);
  7364. first_visible_line = n_line;
  7365. first_visible_line_wrap_ofs = wi;
  7366. }
  7367. queue_accessibility_update();
  7368. queue_redraw();
  7369. }
  7370. double TextEdit::_get_visible_lines_offset() const {
  7371. double total = _get_control_height();
  7372. total /= (double)get_line_height();
  7373. total = total - std::floor(total);
  7374. total = -CLAMP(total, 0.001, 1) + 1;
  7375. return total;
  7376. }
  7377. double TextEdit::_get_v_scroll_offset() const {
  7378. double val = get_v_scroll() - std::floor(get_v_scroll());
  7379. return CLAMP(val, 0, 1);
  7380. }
  7381. void TextEdit::_scroll_up(real_t p_delta, bool p_animate) {
  7382. if (scrolling && smooth_scroll_enabled && SIGN(target_v_scroll - v_scroll->get_value()) != SIGN(-p_delta)) {
  7383. scrolling = false;
  7384. minimap_clicked = false;
  7385. }
  7386. if (scrolling) {
  7387. target_v_scroll = (target_v_scroll - p_delta);
  7388. } else {
  7389. target_v_scroll = (get_v_scroll() - p_delta);
  7390. }
  7391. if (smooth_scroll_enabled) {
  7392. if (target_v_scroll <= 0) {
  7393. target_v_scroll = 0;
  7394. }
  7395. if (!p_animate || Math::abs(target_v_scroll - v_scroll->get_value()) < 1.0) {
  7396. v_scroll->set_value(target_v_scroll);
  7397. queue_accessibility_update();
  7398. } else {
  7399. scrolling = true;
  7400. set_process_internal(true);
  7401. }
  7402. } else {
  7403. set_v_scroll(target_v_scroll);
  7404. }
  7405. }
  7406. void TextEdit::_scroll_down(real_t p_delta, bool p_animate) {
  7407. if (scrolling && smooth_scroll_enabled && SIGN(target_v_scroll - v_scroll->get_value()) != SIGN(p_delta)) {
  7408. scrolling = false;
  7409. minimap_clicked = false;
  7410. }
  7411. if (scrolling) {
  7412. target_v_scroll = (target_v_scroll + p_delta);
  7413. } else {
  7414. target_v_scroll = (get_v_scroll() + p_delta);
  7415. }
  7416. if (smooth_scroll_enabled) {
  7417. int max_v_scroll = std::round(v_scroll->get_max() - v_scroll->get_page());
  7418. if (target_v_scroll > max_v_scroll) {
  7419. target_v_scroll = max_v_scroll;
  7420. }
  7421. if (!p_animate || Math::abs(target_v_scroll - v_scroll->get_value()) < 1.0) {
  7422. v_scroll->set_value(target_v_scroll);
  7423. queue_accessibility_update();
  7424. } else {
  7425. scrolling = true;
  7426. set_process_internal(true);
  7427. }
  7428. } else {
  7429. set_v_scroll(target_v_scroll);
  7430. }
  7431. }
  7432. void TextEdit::_scroll_lines_up() {
  7433. scrolling = false;
  7434. minimap_clicked = false;
  7435. // Adjust the vertical scroll.
  7436. set_v_scroll(get_v_scroll() - 1);
  7437. // Adjust the caret to viewport.
  7438. for (int i = 0; i < carets.size(); i++) {
  7439. if (has_selection(i)) {
  7440. continue;
  7441. }
  7442. int last_vis_line = get_last_full_visible_line();
  7443. int last_vis_wrap = get_last_full_visible_line_wrap_index();
  7444. if (get_caret_line(i) > last_vis_line || (get_caret_line(i) == last_vis_line && get_caret_wrap_index(i) > last_vis_wrap)) {
  7445. set_caret_line(last_vis_line, false, false, last_vis_wrap, i);
  7446. }
  7447. }
  7448. merge_overlapping_carets();
  7449. }
  7450. void TextEdit::_scroll_lines_down() {
  7451. scrolling = false;
  7452. minimap_clicked = false;
  7453. // Adjust the vertical scroll.
  7454. set_v_scroll(get_v_scroll() + 1);
  7455. // Adjust the caret to viewport.
  7456. for (int i = 0; i < carets.size(); i++) {
  7457. if (has_selection(i)) {
  7458. continue;
  7459. }
  7460. int first_vis_line = get_first_visible_line();
  7461. 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)) {
  7462. set_caret_line(first_vis_line, false, false, first_visible_line_wrap_ofs, i);
  7463. }
  7464. }
  7465. merge_overlapping_carets();
  7466. }
  7467. void TextEdit::_adjust_viewport_to_caret_horizontally(int p_caret, bool p_maximize_selection) {
  7468. if (get_line_wrapping_mode() != LineWrappingMode::LINE_WRAPPING_NONE) {
  7469. first_visible_col = 0;
  7470. h_scroll->set_value(first_visible_col);
  7471. queue_redraw();
  7472. return;
  7473. }
  7474. int visible_width = get_size().width - theme_cache.style_normal->get_minimum_size().width - gutters_width - gutter_padding;
  7475. if (draw_minimap) {
  7476. visible_width -= minimap_width;
  7477. }
  7478. if (v_scroll->is_visible_in_tree()) {
  7479. visible_width -= v_scroll->get_combined_minimum_size().width;
  7480. }
  7481. visible_width -= 20; // Give it a little more space.
  7482. if (visible_width <= 0) {
  7483. // Not resized yet.
  7484. return;
  7485. }
  7486. int caret_start_pos;
  7487. int caret_end_pos;
  7488. bool prioritize_end = true;
  7489. // Get start and end position of the caret.
  7490. if (has_ime_text()) {
  7491. // Use the size of the IME.
  7492. int ime_start_column = get_caret_column(p_caret) + ime_selection.x;
  7493. caret_start_pos = _get_column_x_offset_for_line(ime_start_column, get_caret_line(p_caret), ime_start_column);
  7494. int ime_end_column = get_caret_column(p_caret) + (ime_selection.y > 0 ? ime_selection.x + ime_selection.y : ime_text.length());
  7495. caret_end_pos = _get_column_x_offset_for_line(ime_end_column, get_caret_line(p_caret), ime_end_column);
  7496. prioritize_end = false;
  7497. } else if (p_maximize_selection && has_selection(p_caret) && get_selection_from_line(p_caret) == get_selection_to_line(p_caret)) {
  7498. // Use selection if it is on one line.
  7499. caret_start_pos = _get_column_x_offset_for_line(get_selection_from_column(p_caret), get_caret_line(p_caret), get_selection_from_column(p_caret));
  7500. caret_end_pos = _get_column_x_offset_for_line(get_selection_to_column(p_caret), get_caret_line(p_caret), get_selection_to_column(p_caret));
  7501. prioritize_end = is_caret_after_selection_origin();
  7502. } else {
  7503. caret_start_pos = _get_column_x_offset_for_line(get_caret_column(p_caret), get_caret_line(p_caret), get_caret_column(p_caret));
  7504. caret_end_pos = caret_start_pos;
  7505. }
  7506. if (caret_start_pos > caret_end_pos) {
  7507. // For RTL text.
  7508. SWAP(caret_start_pos, caret_end_pos);
  7509. prioritize_end = !prioritize_end;
  7510. }
  7511. if (!prioritize_end && caret_end_pos > first_visible_col + visible_width) {
  7512. first_visible_col = caret_end_pos - visible_width + 1;
  7513. }
  7514. if (caret_start_pos < first_visible_col) {
  7515. first_visible_col = caret_start_pos;
  7516. }
  7517. if (prioritize_end && caret_end_pos > first_visible_col + visible_width) {
  7518. first_visible_col = caret_end_pos - visible_width + 1;
  7519. }
  7520. h_scroll->set_value(first_visible_col);
  7521. queue_accessibility_update();
  7522. queue_redraw();
  7523. }
  7524. // Minimap
  7525. void TextEdit::_update_minimap_hover() {
  7526. const Point2 mp = get_local_mouse_pos();
  7527. const int xmargin_end = get_size().width - Math::ceil(theme_cache.style_normal->get_margin(SIDE_RIGHT));
  7528. bool hovering_sidebar = mp.x > xmargin_end - minimap_width && mp.x < xmargin_end;
  7529. if (!hovering_sidebar) {
  7530. if (hovering_minimap) {
  7531. // Only redraw if the hovering status changed.
  7532. hovering_minimap = false;
  7533. queue_redraw();
  7534. }
  7535. // Return early to avoid running the operations below when not needed.
  7536. return;
  7537. }
  7538. const int row = get_minimap_line_at_pos(mp);
  7539. bool new_hovering_minimap = row >= get_first_visible_line() && row <= get_last_full_visible_line();
  7540. if (new_hovering_minimap != hovering_minimap) {
  7541. // Only redraw if the hovering status changed.
  7542. hovering_minimap = new_hovering_minimap;
  7543. queue_redraw();
  7544. }
  7545. }
  7546. void TextEdit::_update_minimap_click() {
  7547. Point2 mp = get_local_mouse_pos();
  7548. int xmargin_end = get_size().width - Math::ceil(theme_cache.style_normal->get_margin(SIDE_RIGHT));
  7549. if (!dragging_minimap && (mp.x < xmargin_end - minimap_width || mp.x > xmargin_end)) {
  7550. minimap_clicked = false;
  7551. return;
  7552. }
  7553. minimap_clicked = true;
  7554. dragging_minimap = true;
  7555. int row = get_minimap_line_at_pos(mp);
  7556. if (row >= get_first_visible_line() && (row < get_last_full_visible_line() || row >= (text.size() - 1))) {
  7557. minimap_scroll_ratio = v_scroll->get_as_ratio();
  7558. minimap_scroll_click_pos = mp.y;
  7559. can_drag_minimap = true;
  7560. return;
  7561. }
  7562. Point2i next_line = get_next_visible_line_index_offset_from(row, 0, -get_visible_line_count() / 2);
  7563. int first_line = MAX(0, row - next_line.x + 1);
  7564. double delta = get_scroll_pos_for_line(first_line, next_line.y) - get_v_scroll();
  7565. if (delta < 0) {
  7566. _scroll_up(-delta, true);
  7567. } else {
  7568. _scroll_down(delta, true);
  7569. }
  7570. }
  7571. void TextEdit::_update_minimap_drag() {
  7572. if (!can_drag_minimap) {
  7573. return;
  7574. }
  7575. int control_height = _get_control_height();
  7576. int scroll_height = v_scroll->get_max() * (minimap_char_size.y + minimap_line_spacing);
  7577. if (control_height > scroll_height) {
  7578. control_height = scroll_height;
  7579. }
  7580. Point2 mp = get_local_mouse_pos();
  7581. double diff = (mp.y - minimap_scroll_click_pos) / control_height;
  7582. v_scroll->set_as_ratio(minimap_scroll_ratio + diff);
  7583. }
  7584. /* Gutters. */
  7585. void TextEdit::_update_gutter_width() {
  7586. gutters_width = 0;
  7587. for (int i = 0; i < gutters.size(); i++) {
  7588. if (gutters[i].draw) {
  7589. gutters_width += gutters[i].width;
  7590. }
  7591. }
  7592. if (gutters_width > 0) {
  7593. gutter_padding = 2;
  7594. }
  7595. if (get_viewport()) {
  7596. hovered_gutter = _get_hovered_gutter(get_local_mouse_position());
  7597. }
  7598. queue_accessibility_update();
  7599. queue_redraw();
  7600. }
  7601. Vector2i TextEdit::_get_hovered_gutter(const Point2 &p_mouse_pos) const {
  7602. int left_margin = Math::ceil(theme_cache.style_normal->get_margin(SIDE_LEFT));
  7603. if (p_mouse_pos.x > left_margin + gutters_width + gutter_padding) {
  7604. return Vector2i(-1, -1);
  7605. }
  7606. int hovered_row = get_line_column_at_pos(p_mouse_pos, false).y;
  7607. if (hovered_row == -1) {
  7608. return Vector2i(-1, -1);
  7609. }
  7610. for (int i = 0; i < gutters.size(); i++) {
  7611. if (!gutters[i].draw || gutters[i].width <= 0) {
  7612. continue;
  7613. }
  7614. if (p_mouse_pos.x >= left_margin && p_mouse_pos.x < left_margin + gutters[i].width) {
  7615. return Vector2i(i, hovered_row);
  7616. }
  7617. left_margin += gutters[i].width;
  7618. }
  7619. return Vector2i(-1, -1);
  7620. }
  7621. /* Syntax highlighting. */
  7622. Vector<Pair<int64_t, Color>> TextEdit::_get_line_syntax_highlighting(int p_line) {
  7623. if (syntax_highlighter.is_null() || setting_text) {
  7624. return Vector<Pair<int64_t, Color>>();
  7625. }
  7626. HashMap<int, Vector<Pair<int64_t, Color>>>::Iterator E = syntax_highlighting_cache.find(p_line);
  7627. if (E) {
  7628. return E->value;
  7629. }
  7630. Dictionary color_map = syntax_highlighter->get_line_syntax_highlighting(p_line);
  7631. Vector<Pair<int64_t, Color>> result;
  7632. result.resize(color_map.size());
  7633. int i = 0;
  7634. for (const Variant *key = color_map.next(nullptr); key; key = color_map.next(key), i++) {
  7635. int64_t key_data = *key;
  7636. const Variant *color_data = color_map.getptr(*key);
  7637. Color color_value = editable ? theme_cache.font_color : theme_cache.font_readonly_color;
  7638. if (color_data != nullptr) {
  7639. color_value = (color_data->operator Dictionary()).get("color", color_value);
  7640. }
  7641. result.write[i] = Pair<int64_t, Color>(key_data, color_value);
  7642. }
  7643. syntax_highlighting_cache.insert(p_line, result);
  7644. return result;
  7645. }
  7646. void TextEdit::_clear_syntax_highlighting_cache() {
  7647. syntax_highlighting_cache.clear();
  7648. }
  7649. /* Deprecated. */
  7650. #ifndef DISABLE_DEPRECATED
  7651. Vector<int> TextEdit::get_caret_index_edit_order() {
  7652. Vector<int> carets_order = get_sorted_carets();
  7653. carets_order.reverse();
  7654. return carets_order;
  7655. }
  7656. 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) {
  7657. }
  7658. int TextEdit::get_selection_line(int p_caret) const {
  7659. return get_selection_origin_line(p_caret);
  7660. }
  7661. int TextEdit::get_selection_column(int p_caret) const {
  7662. return get_selection_origin_column(p_caret);
  7663. }
  7664. #endif
  7665. /*** Super internal Core API. Everything builds on it. ***/
  7666. void TextEdit::_text_changed() {
  7667. _clear_syntax_highlighting_cache();
  7668. _cancel_drag_and_drop_text();
  7669. queue_redraw();
  7670. if (text_changed_dirty || setting_text) {
  7671. return;
  7672. }
  7673. if (is_inside_tree()) {
  7674. callable_mp(this, &TextEdit::_emit_text_changed).call_deferred();
  7675. }
  7676. text_changed_dirty = true;
  7677. }
  7678. void TextEdit::_emit_text_changed() {
  7679. emit_signal(SceneStringName(text_changed));
  7680. text_changed_dirty = false;
  7681. }
  7682. void TextEdit::_insert_text(int p_line, int p_char, const String &p_text, int *r_end_line, int *r_end_char) {
  7683. if (!setting_text && idle_detect->is_inside_tree()) {
  7684. idle_detect->start();
  7685. }
  7686. if (undo_enabled) {
  7687. _clear_redo();
  7688. }
  7689. int retline, retchar;
  7690. _base_insert_text(p_line, p_char, p_text, retline, retchar);
  7691. if (r_end_line) {
  7692. *r_end_line = retline;
  7693. }
  7694. if (r_end_char) {
  7695. *r_end_char = retchar;
  7696. }
  7697. if (!undo_enabled) {
  7698. return;
  7699. }
  7700. /* UNDO!! */
  7701. TextOperation op;
  7702. op.type = TextOperation::TYPE_INSERT;
  7703. op.from_line = p_line;
  7704. op.from_column = p_char;
  7705. op.to_line = retline;
  7706. op.to_column = retchar;
  7707. op.text = p_text;
  7708. op.version = ++version;
  7709. op.chain_forward = false;
  7710. op.chain_backward = false;
  7711. if (next_operation_is_complex) {
  7712. op.start_carets = current_op.start_carets;
  7713. } else {
  7714. op.start_carets = carets;
  7715. }
  7716. op.end_carets = carets;
  7717. op.prev_version = get_version();
  7718. _push_current_op();
  7719. current_op = op;
  7720. }
  7721. void TextEdit::_remove_text(int p_from_line, int p_from_column, int p_to_line, int p_to_column) {
  7722. if (!setting_text && idle_detect->is_inside_tree()) {
  7723. idle_detect->start();
  7724. }
  7725. String txt;
  7726. if (undo_enabled) {
  7727. _clear_redo();
  7728. txt = _base_get_text(p_from_line, p_from_column, p_to_line, p_to_column);
  7729. }
  7730. _base_remove_text(p_from_line, p_from_column, p_to_line, p_to_column);
  7731. if (!undo_enabled) {
  7732. return;
  7733. }
  7734. /* UNDO! */
  7735. TextOperation op;
  7736. op.type = TextOperation::TYPE_REMOVE;
  7737. op.from_line = p_from_line;
  7738. op.from_column = p_from_column;
  7739. op.to_line = p_to_line;
  7740. op.to_column = p_to_column;
  7741. op.text = txt;
  7742. op.version = ++version;
  7743. op.chain_forward = false;
  7744. op.chain_backward = false;
  7745. if (next_operation_is_complex) {
  7746. op.start_carets = current_op.start_carets;
  7747. } else {
  7748. op.start_carets = carets;
  7749. }
  7750. op.end_carets = carets;
  7751. op.prev_version = get_version();
  7752. _push_current_op();
  7753. current_op = op;
  7754. }
  7755. void TextEdit::_base_insert_text(int p_line, int p_char, const String &p_text, int &r_end_line, int &r_end_column) {
  7756. // Save for undo.
  7757. ERR_FAIL_INDEX(p_line, text.size());
  7758. ERR_FAIL_COND(p_char < 0);
  7759. /* STEP 1: Remove \r from source text and separate in substrings. */
  7760. const String text_to_insert = p_text.remove_char('\r');
  7761. Vector<String> substrings = text_to_insert.split("\n");
  7762. // Is this just a new empty line?
  7763. bool shift_first_line = p_char == 0 && substrings.size() == 2 && text_to_insert == "\n";
  7764. /* STEP 2: Add spaces if the char is greater than the end of the line. */
  7765. while (p_char > text[p_line].length()) {
  7766. text.set(p_line, text[p_line] + String::chr(' '), structured_text_parser(st_parser, st_args, text[p_line] + String::chr(' ')));
  7767. }
  7768. /* STEP 3: Separate dest string in pre and post text. */
  7769. String postinsert_text = text[p_line].substr(p_char);
  7770. substrings.write[0] = text[p_line].substr(0, p_char) + substrings[0];
  7771. substrings.write[substrings.size() - 1] += postinsert_text;
  7772. Vector<Array> bidi_override;
  7773. bidi_override.resize(substrings.size());
  7774. for (int i = 0; i < substrings.size(); i++) {
  7775. bidi_override.write[i] = structured_text_parser(st_parser, st_args, substrings[i]);
  7776. }
  7777. text.insert(p_line, substrings, bidi_override);
  7778. if (shift_first_line) {
  7779. text.move_gutters(p_line, p_line + 1);
  7780. text.set_hidden(p_line + 1, text.is_hidden(p_line));
  7781. text.set_hidden(p_line, false);
  7782. }
  7783. r_end_line = p_line + substrings.size() - 1;
  7784. r_end_column = text[r_end_line].length() - postinsert_text.length();
  7785. 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);
  7786. if (dir != TextServer::DIRECTION_AUTO) {
  7787. input_direction = (TextDirection)dir;
  7788. }
  7789. _text_changed();
  7790. emit_signal(SNAME("lines_edited_from"), p_line, r_end_line);
  7791. }
  7792. String TextEdit::_base_get_text(int p_from_line, int p_from_column, int p_to_line, int p_to_column) const {
  7793. ERR_FAIL_INDEX_V(p_from_line, text.size(), String());
  7794. ERR_FAIL_INDEX_V(p_from_column, text[p_from_line].length() + 1, String());
  7795. ERR_FAIL_INDEX_V(p_to_line, text.size(), String());
  7796. ERR_FAIL_INDEX_V(p_to_column, text[p_to_line].length() + 1, String());
  7797. ERR_FAIL_COND_V(p_to_line < p_from_line, String()); // 'from > to'.
  7798. ERR_FAIL_COND_V(p_to_line == p_from_line && p_to_column < p_from_column, String()); // 'from > to'.
  7799. StringBuilder ret;
  7800. for (int i = p_from_line; i <= p_to_line; i++) {
  7801. int begin = (i == p_from_line) ? p_from_column : 0;
  7802. int end = (i == p_to_line) ? p_to_column : text[i].length();
  7803. if (i > p_from_line) {
  7804. ret += "\n";
  7805. }
  7806. ret += text[i].substr(begin, end - begin);
  7807. }
  7808. return ret.as_string();
  7809. }
  7810. void TextEdit::_base_remove_text(int p_from_line, int p_from_column, int p_to_line, int p_to_column) {
  7811. ERR_FAIL_INDEX(p_from_line, text.size());
  7812. ERR_FAIL_INDEX(p_from_column, text[p_from_line].length() + 1);
  7813. ERR_FAIL_INDEX(p_to_line, text.size());
  7814. ERR_FAIL_INDEX(p_to_column, text[p_to_line].length() + 1);
  7815. ERR_FAIL_COND(p_to_line < p_from_line); // 'from > to'.
  7816. ERR_FAIL_COND(p_to_line == p_from_line && p_to_column < p_from_column); // 'from > to'.
  7817. String pre_text = text[p_from_line].substr(0, p_from_column);
  7818. String post_text = text[p_to_line].substr(p_to_column);
  7819. text.remove_range(p_from_line, p_to_line);
  7820. text.set(p_from_line, pre_text + post_text, structured_text_parser(st_parser, st_args, pre_text + post_text));
  7821. _text_changed();
  7822. emit_signal(SNAME("lines_edited_from"), p_to_line, p_from_line);
  7823. }
  7824. TextEdit::TextEdit(const String &p_placeholder) {
  7825. placeholder_data_buf.instantiate();
  7826. carets.push_back(Caret());
  7827. clear();
  7828. set_focus_mode(FOCUS_ALL);
  7829. set_default_cursor_shape(CURSOR_IBEAM);
  7830. set_process_unhandled_key_input(true);
  7831. text.set_tab_size(text.get_tab_size());
  7832. h_scroll = memnew(HScrollBar);
  7833. v_scroll = memnew(VScrollBar);
  7834. add_child(h_scroll, false, INTERNAL_MODE_FRONT);
  7835. add_child(v_scroll, false, INTERNAL_MODE_FRONT);
  7836. h_scroll->connect(SceneStringName(value_changed), callable_mp(this, &TextEdit::_scroll_moved));
  7837. v_scroll->connect(SceneStringName(value_changed), callable_mp(this, &TextEdit::_scroll_moved));
  7838. v_scroll->connect("scrolling", callable_mp(this, &TextEdit::_v_scroll_input));
  7839. /* Caret. */
  7840. caret_blink_timer = memnew(Timer);
  7841. add_child(caret_blink_timer, false, INTERNAL_MODE_FRONT);
  7842. caret_blink_timer->set_wait_time(0.65);
  7843. caret_blink_timer->connect("timeout", callable_mp(this, &TextEdit::_toggle_draw_caret));
  7844. set_caret_blink_enabled(false);
  7845. /* Selection. */
  7846. click_select_held = memnew(Timer);
  7847. add_child(click_select_held, false, INTERNAL_MODE_FRONT);
  7848. click_select_held->set_wait_time(0.05);
  7849. click_select_held->connect("timeout", callable_mp(this, &TextEdit::_click_selection_held));
  7850. idle_detect = memnew(Timer);
  7851. add_child(idle_detect, false, INTERNAL_MODE_FRONT);
  7852. idle_detect->set_one_shot(true);
  7853. idle_detect->set_wait_time(GLOBAL_GET_CACHED(double, "gui/timers/text_edit_idle_detect_sec"));
  7854. idle_detect->connect("timeout", callable_mp(this, &TextEdit::_push_current_op));
  7855. undo_stack_max_size = GLOBAL_GET_CACHED(int, "gui/common/text_edit_undo_stack_max_size");
  7856. set_placeholder(p_placeholder);
  7857. set_clip_contents(true);
  7858. set_editable(true);
  7859. }