2
0

text_edit.cpp 194 KB

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