text_edit.cpp 261 KB

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