text_edit.cpp 254 KB

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