text_edit.cpp 213 KB

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