text_edit.cpp 178 KB

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