text_edit.cpp 186 KB

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