text_edit.cpp 178 KB

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