text_edit.cpp 262 KB

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