text_edit.cpp 212 KB

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