text_edit.cpp 254 KB

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