text_edit.cpp 195 KB

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