text_edit.cpp 283 KB

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