text_edit.cpp 186 KB

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