text_edit.cpp 262 KB

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